blob: a7a254af8f7de2eb2ad7ba58963c296c2ecc41cc [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 Kang1147b312023-06-08 23:25:57 +0000251func (scope *apiScope) sourceStubLibraryModuleName(baseName string) string {
252 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 Duffin0ff08bd2020-04-29 13:30:54 +0100271func (scope *apiScope) apiModuleName(baseName string) string {
Paul Duffindd9d0742020-05-08 15:52:37 +0100272 return baseName + ".api" + scope.moduleSuffix
Paul Duffin0ff08bd2020-04-29 13:30:54 +0100273}
274
Paul Duffin3375e352020-04-28 10:44:03 +0100275func (scope *apiScope) String() string {
276 return scope.name
277}
278
Paul Duffin958806b2022-05-16 13:10:47 +0000279// snapshotRelativeDir returns the snapshot directory into which the files related to scopes will
280// be stored.
281func (scope *apiScope) snapshotRelativeDir() string {
282 return filepath.Join("sdk_library", scope.name)
283}
284
285// snapshotRelativeCurrentApiTxtPath returns the snapshot path to the API .txt file for the named
286// library.
287func (scope *apiScope) snapshotRelativeCurrentApiTxtPath(name string) string {
288 return filepath.Join(scope.snapshotRelativeDir(), name+".txt")
289}
290
291// snapshotRelativeRemovedApiTxtPath returns the snapshot path to the removed API .txt file for the
292// named library.
293func (scope *apiScope) snapshotRelativeRemovedApiTxtPath(name string) string {
294 return filepath.Join(scope.snapshotRelativeDir(), name+"-removed.txt")
295}
296
Paul Duffind1b3a922020-01-22 11:57:20 +0000297type apiScopes []*apiScope
298
299func (scopes apiScopes) Strings(accessor func(*apiScope) string) []string {
300 var list []string
301 for _, scope := range scopes {
302 list = append(list, accessor(scope))
303 }
304 return list
305}
306
Jihoon Kanga96a7b12023-09-20 23:43:32 +0000307// Method that maps the apiScopes properties to the index of each apiScopes elements.
308// apiScopes property to be used as the key can be specified with the input accessor.
309// Only a string property of apiScope can be used as the key of the map.
310func (scopes apiScopes) MapToIndex(accessor func(*apiScope) string) map[string]int {
311 ret := make(map[string]int)
312 for i, scope := range scopes {
313 ret[accessor(scope)] = i
314 }
315 return ret
316}
317
Jihoon Kang98aa8fa2024-06-07 11:06:57 +0000318func (scopes apiScopes) ConvertStubsLibraryExportableToEverything(name string) string {
319 for _, scope := range scopes {
320 if strings.HasSuffix(name, scope.exportableStubsLibraryModuleNameSuffix()) {
321 return strings.TrimSuffix(name, scope.exportableStubsLibraryModuleNameSuffix()) +
322 scope.stubsLibraryModuleNameSuffix()
323 }
324 }
325 return name
326}
327
Jiyong Parkc678ad32018-04-10 13:07:10 +0900328var (
Paul Duffin46dc45a2020-05-14 15:39:10 +0100329 scopeByName = make(map[string]*apiScope)
330 allScopeNames []string
Paul Duffind1b3a922020-01-22 11:57:20 +0000331 apiScopePublic = initApiScope(&apiScope{
Paul Duffin3375e352020-04-28 10:44:03 +0100332 name: "public",
333
334 // Public scope is enabled by default for both legacy and non-legacy modes.
335 legacyEnabledStatus: func(module *SdkLibrary) bool {
336 return true
337 },
338 defaultEnabledStatus: true,
339
340 scopeSpecificProperties: func(module *SdkLibrary) *ApiScopeProperties {
341 return &module.sdkLibraryProperties.Public
342 },
Paul Duffind1b3a922020-01-22 11:57:20 +0000343 sdkVersion: "current",
Jihoon Kang1c92c3e2023-03-23 17:44:51 +0000344 kind: android.SdkPublic,
Paul Duffind1b3a922020-01-22 11:57:20 +0000345 })
346 apiScopeSystem = initApiScope(&apiScope{
Paul Duffin3375e352020-04-28 10:44:03 +0100347 name: "system",
348 extends: apiScopePublic,
349 legacyEnabledStatus: (*SdkLibrary).generateTestAndSystemScopesByDefault,
350 scopeSpecificProperties: func(module *SdkLibrary) *ApiScopeProperties {
351 return &module.sdkLibraryProperties.System
352 },
Paul Duffin15f34ef2020-07-20 18:04:44 +0100353 apiFilePrefix: "system-",
354 moduleSuffix: ".system",
355 sdkVersion: "system_current",
356 annotation: "android.annotation.SystemApi(client=android.annotation.SystemApi.Client.PRIVILEGED_APPS)",
Jihoon Kang1c92c3e2023-03-23 17:44:51 +0000357 kind: android.SdkSystem,
Paul Duffind1b3a922020-01-22 11:57:20 +0000358 })
359 apiScopeTest = initApiScope(&apiScope{
Paul Duffin3375e352020-04-28 10:44:03 +0100360 name: "test",
Anton Hansson4fe970f2020-10-09 10:16:49 +0100361 extends: apiScopeSystem,
Paul Duffin3375e352020-04-28 10:44:03 +0100362 legacyEnabledStatus: (*SdkLibrary).generateTestAndSystemScopesByDefault,
363 scopeSpecificProperties: func(module *SdkLibrary) *ApiScopeProperties {
364 return &module.sdkLibraryProperties.Test
365 },
Paul Duffin15f34ef2020-07-20 18:04:44 +0100366 apiFilePrefix: "test-",
367 moduleSuffix: ".test",
368 sdkVersion: "test_current",
369 annotation: "android.annotation.TestApi",
370 unstable: true,
Jihoon Kang1c92c3e2023-03-23 17:44:51 +0000371 kind: android.SdkTest,
Paul Duffind1b3a922020-01-22 11:57:20 +0000372 })
Paul Duffin8f265b92020-04-28 14:13:56 +0100373 apiScopeModuleLib = initApiScope(&apiScope{
Paul Duffin6b836ba2020-05-13 19:19:49 +0100374 name: "module-lib",
Paul Duffin8f265b92020-04-28 14:13:56 +0100375 extends: apiScopeSystem,
Paul Duffin0c5bae52020-06-02 13:00:08 +0100376 // The module-lib scope is disabled by default in legacy mode.
Paul Duffin8f265b92020-04-28 14:13:56 +0100377 //
378 // Enabling this would break existing usages.
379 legacyEnabledStatus: func(module *SdkLibrary) bool {
380 return false
381 },
382 scopeSpecificProperties: func(module *SdkLibrary) *ApiScopeProperties {
383 return &module.sdkLibraryProperties.Module_lib
384 },
385 apiFilePrefix: "module-lib-",
386 moduleSuffix: ".module_lib",
387 sdkVersion: "module_current",
Paul Duffin15f34ef2020-07-20 18:04:44 +0100388 annotation: "android.annotation.SystemApi(client=android.annotation.SystemApi.Client.MODULE_LIBRARIES)",
Jihoon Kang1c92c3e2023-03-23 17:44:51 +0000389 kind: android.SdkModule,
Paul Duffin8f265b92020-04-28 14:13:56 +0100390 })
Paul Duffin0c5bae52020-06-02 13:00:08 +0100391 apiScopeSystemServer = initApiScope(&apiScope{
392 name: "system-server",
393 extends: apiScopePublic,
Paul Duffind0b9fca2022-09-30 18:11:41 +0100394
395 // The system-server scope can access the module-lib scope.
396 //
397 // A module that provides a system-server API is appended to the standard bootclasspath that is
398 // used by the system server. So, it should be able to access module-lib APIs provided by
399 // libraries on the bootclasspath.
400 canAccess: apiScopeModuleLib,
401
Paul Duffin0c5bae52020-06-02 13:00:08 +0100402 // The system-server scope is disabled by default in legacy mode.
403 //
404 // Enabling this would break existing usages.
405 legacyEnabledStatus: func(module *SdkLibrary) bool {
406 return false
407 },
408 scopeSpecificProperties: func(module *SdkLibrary) *ApiScopeProperties {
409 return &module.sdkLibraryProperties.System_server
410 },
411 apiFilePrefix: "system-server-",
412 moduleSuffix: ".system_server",
413 sdkVersion: "system_server_current",
Paul Duffin15f34ef2020-07-20 18:04:44 +0100414 annotation: "android.annotation.SystemApi(client=android.annotation.SystemApi.Client.SYSTEM_SERVER)",
415 extraArgs: []string{
416 "--hide-annotation", "android.annotation.Hide",
Paul Duffin0c5bae52020-06-02 13:00:08 +0100417 // com.android.* classes are okay in this interface"
Paul Duffin15f34ef2020-07-20 18:04:44 +0100418 "--hide", "InternalClasses",
Paul Duffin0c5bae52020-06-02 13:00:08 +0100419 },
Jihoon Kang1c92c3e2023-03-23 17:44:51 +0000420 kind: android.SdkSystemServer,
Paul Duffin0c5bae52020-06-02 13:00:08 +0100421 })
Jihoon Kang98aa8fa2024-06-07 11:06:57 +0000422 AllApiScopes = apiScopes{
Paul Duffind1b3a922020-01-22 11:57:20 +0000423 apiScopePublic,
424 apiScopeSystem,
425 apiScopeTest,
Paul Duffin8f265b92020-04-28 14:13:56 +0100426 apiScopeModuleLib,
Paul Duffin0c5bae52020-06-02 13:00:08 +0100427 apiScopeSystemServer,
Paul Duffind1b3a922020-01-22 11:57:20 +0000428 }
Jihoon Kangb0f4c022024-08-06 00:15:25 +0000429 apiLibraryAdditionalProperties = map[string]string{
430 "legacy.i18n.module.platform.api": "i18n.module.public.api.stubs.source.api.contribution",
431 "stable.i18n.module.platform.api": "i18n.module.public.api.stubs.source.api.contribution",
432 "conscrypt.module.platform.api": "conscrypt.module.public.api.stubs.source.api.contribution",
Jihoon Kang0c705a42023-08-02 06:44:57 +0000433 }
Jiyong Parkc678ad32018-04-10 13:07:10 +0900434)
435
Jiyong Park82484c02018-04-23 21:41:26 +0900436var (
437 javaSdkLibrariesLock sync.Mutex
438)
439
Jiyong Parkc678ad32018-04-10 13:07:10 +0900440// TODO: these are big features that are currently missing
Jiyong Park1be96912018-05-28 18:02:19 +0900441// 1) disallowing linking to the runtime shared lib
442// 2) HTML generation
Jiyong Parkc678ad32018-04-10 13:07:10 +0900443
444func init() {
Paul Duffin43dc1cc2019-12-19 11:18:54 +0000445 RegisterSdkLibraryBuildComponents(android.InitRegistrationContext)
Jiyong Parkc678ad32018-04-10 13:07:10 +0900446
Jiyong Park82484c02018-04-23 21:41:26 +0900447 android.RegisterMakeVarsProvider(pctx, func(ctx android.MakeVarsContext) {
448 javaSdkLibraries := javaSdkLibraries(ctx.Config())
449 sort.Strings(*javaSdkLibraries)
450 ctx.Strict("JAVA_SDK_LIBRARIES", strings.Join(*javaSdkLibraries, " "))
451 })
Paul Duffindd46f712020-02-10 13:37:10 +0000452
453 // Register sdk member types.
Paul Duffin976b0e52021-04-27 23:20:26 +0100454 android.RegisterSdkMemberType(javaSdkLibrarySdkMemberType)
Jiyong Parkc678ad32018-04-10 13:07:10 +0900455}
456
Paul Duffin43dc1cc2019-12-19 11:18:54 +0000457func RegisterSdkLibraryBuildComponents(ctx android.RegistrationContext) {
458 ctx.RegisterModuleType("java_sdk_library", SdkLibraryFactory)
459 ctx.RegisterModuleType("java_sdk_library_import", sdkLibraryImportFactory)
460}
461
Paul Duffin3375e352020-04-28 10:44:03 +0100462// Properties associated with each api scope.
463type ApiScopeProperties struct {
464 // Indicates whether the api surface is generated.
465 //
466 // If this is set for any scope then all scopes must explicitly specify if they
467 // are enabled. This is to prevent new usages from depending on legacy behavior.
468 //
469 // Otherwise, if this is not set for any scope then the default behavior is
470 // scope specific so please refer to the scope specific property documentation.
471 Enabled *bool
Paul Duffin87a05a32020-05-12 11:50:28 +0100472
473 // The sdk_version to use for building the stubs.
474 //
475 // If not specified then it will use an sdk_version determined as follows:
Trevor Radcliffedf8aa1f2021-11-04 14:25:39 +0000476 //
Paul Duffin87a05a32020-05-12 11:50:28 +0100477 // 1) If the sdk_version specified on the java_sdk_library is none then this
Trevor Radcliffedf8aa1f2021-11-04 14:25:39 +0000478 // will be none. This is used for java_sdk_library instances that are used
479 // to create stubs that contribute to the core_current sdk version.
480 // 2) Otherwise, it is assumed that this library extends but does not
481 // contribute directly to a specific sdk_version and so this uses the
482 // sdk_version appropriate for the api scope. e.g. public will use
483 // sdk_version: current, system will use sdk_version: system_current, etc.
Paul Duffin87a05a32020-05-12 11:50:28 +0100484 //
485 // This does not affect the sdk_version used for either generating the stubs source
486 // or the API file. They both have to use the same sdk_version as is used for
487 // compiling the implementation library.
488 Sdk_version *string
Mark White9421c4c2023-08-10 00:07:03 +0000489
490 // Extra libs used when compiling stubs for this scope.
491 Libs []string
Paul Duffin3375e352020-04-28 10:44:03 +0100492}
493
Jiyong Parkc678ad32018-04-10 13:07:10 +0900494type sdkLibraryProperties struct {
Anton Hanssonf8ea3722021-09-16 14:24:13 +0100495 // List of source files that are needed to compile the API, but are not part of runtime library.
496 Api_srcs []string `android:"arch_variant"`
497
Paul Duffin5df79302020-05-16 15:52:12 +0100498 // Visibility for impl library module. If not specified then defaults to the
499 // visibility property.
500 Impl_library_visibility []string
501
Paul Duffin4911a892020-04-29 23:35:13 +0100502 // Visibility for stubs library modules. If not specified then defaults to the
503 // visibility property.
504 Stubs_library_visibility []string
505
506 // Visibility for stubs source modules. If not specified then defaults to the
507 // visibility property.
508 Stubs_source_visibility []string
509
Anton Hansson7f66efa2020-10-08 14:47:23 +0100510 // List of Java libraries that will be in the classpath when building the implementation lib
511 Impl_only_libs []string `android:"arch_variant"`
512
Paul Duffin77590a82022-04-28 14:13:30 +0000513 // List of Java libraries that will included in the implementation lib.
514 Impl_only_static_libs []string `android:"arch_variant"`
515
Sundong Ahnf043cf62018-06-25 16:04:37 +0900516 // List of Java libraries that will be in the classpath when building stubs
517 Stub_only_libs []string `android:"arch_variant"`
518
Anton Hanssondae54cd2021-04-21 16:30:10 +0100519 // List of Java libraries that will included in stub libraries
520 Stub_only_static_libs []string `android:"arch_variant"`
521
Paul Duffin7a586d32019-12-30 17:09:34 +0000522 // list of package names that will be documented and publicized as API.
523 // This allows the API to be restricted to a subset of the source files provided.
524 // If this is unspecified then all the source files will be treated as being part
525 // of the API.
Jiyong Parkc678ad32018-04-10 13:07:10 +0900526 Api_packages []string
527
Paul Duffin749f98f2019-12-30 17:23:46 +0000528 // the relative path to the directory containing the api specification files.
529 // Defaults to "api".
530 Api_dir *string
531
Paul Duffindfa131e2020-05-15 20:37:11 +0100532 // Determines whether a runtime implementation library is built; defaults to false.
533 //
534 // 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 +0200535 // it is as if shared_library: false, was set.
Paul Duffin43db9be2019-12-30 17:35:49 +0000536 Api_only *bool
537
Paul Duffin11512472019-02-11 15:55:17 +0000538 // local files that are used within user customized droiddoc options.
539 Droiddoc_option_files []string
540
Spandan Das93e95992021-07-29 18:26:39 +0000541 // additional droiddoc options.
Paul Duffin11512472019-02-11 15:55:17 +0000542 // Available variables for substitution:
543 //
544 // $(location <label>): the path to the droiddoc_option_files with name <label>
Sundong Ahndd567f92018-07-31 17:19:11 +0900545 Droiddoc_options []string
546
Paul Duffine22c2ab2020-05-20 19:35:27 +0100547 // is set to true, Metalava will allow framework SDK to contain annotations.
548 Annotations_enabled *bool
549
Sundong Ahn054b19a2018-10-19 13:46:09 +0900550 // a list of top-level directories containing files to merge qualifier annotations
551 // (i.e. those intended to be included in the stubs written) from.
552 Merge_annotations_dirs []string
553
554 // a list of top-level directories containing Java stub files to merge show/hide annotations from.
555 Merge_inclusion_annotations_dirs []string
556
Paul Duffin4f5c1ef2020-11-19 14:53:43 +0000557 // If set to true then don't create dist rules.
558 No_dist *bool
Sundong Ahn80a87b32019-05-13 15:02:50 +0900559
Paul Duffin31310252020-11-20 21:26:20 +0000560 // The stem for the artifacts that are copied to the dist, if not specified
561 // then defaults to the base module name.
562 //
563 // For each scope the following artifacts are copied to the apistubs/<scope>
564 // directory in the dist.
565 // * stubs impl jar -> <dist-stem>.jar
566 // * API specification file -> api/<dist-stem>.txt
567 // * Removed API specification file -> api/<dist-stem>-removed.txt
568 //
569 // Also used to construct the name of the filegroup (created by prebuilt_apis)
570 // that references the latest released API and remove API specification files.
571 // * API specification filegroup -> <dist-stem>.api.<scope>.latest
572 // * Removed API specification filegroup -> <dist-stem>-removed.api.<scope>.latest
Jaewoong Jung1a97ee02021-03-09 13:25:02 -0800573 // * API incompatibilities baseline filegroup -> <dist-stem>-incompatibilities.api.<scope>.latest
Paul Duffin31310252020-11-20 21:26:20 +0000574 Dist_stem *string
575
Colin Cross986b69a2021-06-01 13:13:40 -0700576 // The subdirectory for the artifacts that are copied to the dist directory. If not specified
Colin Cross3dd66252021-06-01 14:05:09 -0700577 // then defaults to "unknown". Should be set to "android" for anything that should be published
Colin Cross986b69a2021-06-01 13:13:40 -0700578 // in the public Android SDK.
579 Dist_group *string
580
Anton Hanssondff2c782020-12-21 17:10:01 +0000581 // A compatibility mode that allows historical API-tracking files to not exist.
582 // Do not use.
583 Unsafe_ignore_missing_latest_api bool
584
Paul Duffin3375e352020-04-28 10:44:03 +0100585 // indicates whether system and test apis should be generated.
586 Generate_system_and_test_apis bool `blueprint:"mutated"`
587
588 // The properties specific to the public api scope
589 //
590 // Unless explicitly specified by using public.enabled the public api scope is
591 // enabled by default in both legacy and non-legacy mode.
592 Public ApiScopeProperties
593
594 // The properties specific to the system api scope
595 //
596 // In legacy mode the system api scope is enabled by default when sdk_version
597 // is set to something other than "none".
598 //
599 // In non-legacy mode the system api scope is disabled by default.
600 System ApiScopeProperties
601
602 // The properties specific to the test api scope
603 //
604 // In legacy mode the test api scope is enabled by default when sdk_version
605 // is set to something other than "none".
606 //
607 // In non-legacy mode the test api scope is disabled by default.
608 Test ApiScopeProperties
Paul Duffin37e0b772019-12-30 17:20:10 +0000609
Paul Duffin0c5bae52020-06-02 13:00:08 +0100610 // The properties specific to the module-lib api scope
Paul Duffin8f265b92020-04-28 14:13:56 +0100611 //
Zi Wangb2179e32023-01-31 15:53:30 -0800612 // Unless explicitly specified by using module_lib.enabled the module_lib api
613 // scope is disabled by default.
Paul Duffin8f265b92020-04-28 14:13:56 +0100614 Module_lib ApiScopeProperties
615
Paul Duffin0c5bae52020-06-02 13:00:08 +0100616 // The properties specific to the system-server api scope
617 //
Zi Wangb2179e32023-01-31 15:53:30 -0800618 // Unless explicitly specified by using system_server.enabled the
619 // system_server api scope is disabled by default.
Paul Duffin0c5bae52020-06-02 13:00:08 +0100620 System_server ApiScopeProperties
621
Jiyong Park932cdfe2020-05-28 00:19:53 +0900622 // Determines if the stubs are preferred over the implementation library
623 // for linking, even when the client doesn't specify sdk_version. When this
624 // is set to true, such clients are provided with the widest API surface that
625 // this lib provides. Note however that this option doesn't affect the clients
626 // that are in the same APEX as this library. In that case, the clients are
627 // always linked with the implementation library. Default is false.
628 Default_to_stubs *bool
629
Paul Duffin160fe412020-05-10 19:32:20 +0100630 // Properties related to api linting.
631 Api_lint struct {
632 // Enable api linting.
633 Enabled *bool
Anton Hanssonfd1c0d22023-11-02 15:18:09 +0000634
635 // If API lint is enabled, this flag controls whether a set of legitimate lint errors
636 // are turned off. The default is true.
637 Legacy_errors_allowed *bool
Paul Duffin160fe412020-05-10 19:32:20 +0100638 }
639
Jihoon Kang6592e872023-12-19 01:13:16 +0000640 // a list of aconfig_declarations module names that the stubs generated in this module
641 // depend on.
642 Aconfig_declarations []string
643
Jihoon Kang48e2ac92024-07-29 21:18:46 +0000644 // Determines if the module generates the stubs from the api signature files
645 // instead of the source Java files. Defaults to true.
646 Build_from_text_stub *bool
647
Jiyong Parkc678ad32018-04-10 13:07:10 +0900648 // TODO: determines whether to create HTML doc or not
Paul Duffine8409952022-09-22 16:24:46 +0100649 // Html_doc *bool
Jiyong Parkc678ad32018-04-10 13:07:10 +0900650}
651
Paul Duffin0f8faff2020-05-20 16:18:00 +0100652// Paths to outputs from java_sdk_library and java_sdk_library_import.
653//
654// Fields that are android.Paths are always set (during GenerateAndroidBuildActions).
655// OptionalPaths are always set by java_sdk_library but may not be set by
656// java_sdk_library_import as not all instances provide that information.
Paul Duffind1b3a922020-01-22 11:57:20 +0000657type scopePaths struct {
Paul Duffin0f8faff2020-05-20 16:18:00 +0100658 // The path (represented as Paths for convenience when returning) to the stubs header jar.
659 //
660 // That is the jar that is created by turbine.
661 stubsHeaderPath android.Paths
662
663 // The path (represented as Paths for convenience when returning) to the stubs implementation jar.
664 //
665 // This is not the implementation jar, it still only contains stubs.
666 stubsImplPath android.Paths
667
Paul Duffin1267d872021-04-16 17:21:36 +0100668 // The dex jar for the stubs.
669 //
670 // This is not the implementation jar, it still only contains stubs.
Martin Stjernholm8be1e6d2021-09-15 03:34:04 +0100671 stubsDexJarPath OptionalDexJarPath
Paul Duffin1267d872021-04-16 17:21:36 +0100672
Jihoon Kangbd093452023-12-26 19:08:01 +0000673 // The exportable dex jar for the stubs.
674 // This is not the implementation jar, it still only contains stubs.
675 // Includes unflagged apis and flagged apis enabled by release configurations.
676 exportableStubsDexJarPath OptionalDexJarPath
677
Paul Duffin0f8faff2020-05-20 16:18:00 +0100678 // The API specification file, e.g. system_current.txt.
679 currentApiFilePath android.OptionalPath
680
681 // The specification of API elements removed since the last release.
682 removedApiFilePath android.OptionalPath
683
684 // The stubs source jar.
685 stubsSrcJar android.OptionalPath
Anton Hanssond78eb762021-09-21 15:25:12 +0100686
687 // Extracted annotations.
688 annotationsZip android.OptionalPath
Paul Duffin958806b2022-05-16 13:10:47 +0000689
690 // The path to the latest API file.
Jihoon Kang5623e542024-01-31 23:27:26 +0000691 latestApiPaths android.Paths
Paul Duffin958806b2022-05-16 13:10:47 +0000692
693 // The path to the latest removed API file.
Jihoon Kang5623e542024-01-31 23:27:26 +0000694 latestRemovedApiPaths android.Paths
Paul Duffind1b3a922020-01-22 11:57:20 +0000695}
696
Colin Crossdcf71b22021-02-01 13:59:03 -0800697func (paths *scopePaths) extractStubsLibraryInfoFromDependency(ctx android.ModuleContext, dep android.Module) error {
Colin Cross313aa542023-12-13 13:47:44 -0800698 if lib, ok := android.OtherModuleProvider(ctx, dep, JavaInfoProvider); ok {
Colin Crossdcf71b22021-02-01 13:59:03 -0800699 paths.stubsHeaderPath = lib.HeaderJars
700 paths.stubsImplPath = lib.ImplementationJars
Paul Duffin1267d872021-04-16 17:21:36 +0100701
702 libDep := dep.(UsesLibraryDependency)
Spandan Das59a4a2b2024-01-09 21:35:56 +0000703 paths.stubsDexJarPath = libDep.DexJarBuildPath(ctx)
Jihoon Kangbd093452023-12-26 19:08:01 +0000704 paths.exportableStubsDexJarPath = libDep.DexJarBuildPath(ctx)
705 return nil
706 } else {
707 return fmt.Errorf("expected module that has JavaInfoProvider, e.g. java_library")
708 }
709}
710
711func (paths *scopePaths) extractEverythingStubsLibraryInfoFromDependency(ctx android.ModuleContext, dep android.Module) error {
712 if lib, ok := android.OtherModuleProvider(ctx, dep, JavaInfoProvider); ok {
713 paths.stubsHeaderPath = lib.HeaderJars
Jihoon Kangf55a5f72024-01-08 08:56:20 +0000714 if !ctx.Config().ReleaseHiddenApiExportableStubs() {
715 paths.stubsImplPath = lib.ImplementationJars
716 }
Jihoon Kangbd093452023-12-26 19:08:01 +0000717
718 libDep := dep.(UsesLibraryDependency)
719 paths.stubsDexJarPath = libDep.DexJarBuildPath(ctx)
720 return nil
721 } else {
722 return fmt.Errorf("expected module that has JavaInfoProvider, e.g. java_library")
723 }
724}
725
726func (paths *scopePaths) extractExportableStubsLibraryInfoFromDependency(ctx android.ModuleContext, dep android.Module) error {
Jihoon Kangf55a5f72024-01-08 08:56:20 +0000727 if lib, ok := android.OtherModuleProvider(ctx, dep, JavaInfoProvider); ok {
728 if ctx.Config().ReleaseHiddenApiExportableStubs() {
729 paths.stubsImplPath = lib.ImplementationJars
730 }
731
Jihoon Kangbd093452023-12-26 19:08:01 +0000732 libDep := dep.(UsesLibraryDependency)
733 paths.exportableStubsDexJarPath = libDep.DexJarBuildPath(ctx)
Paul Duffinc8782502020-04-29 20:45:27 +0100734 return nil
735 } else {
Colin Crossdcf71b22021-02-01 13:59:03 -0800736 return fmt.Errorf("expected module that has JavaInfoProvider, e.g. java_library")
Paul Duffinc8782502020-04-29 20:45:27 +0100737 }
738}
739
Jihoon Kangee113282024-01-23 00:16:41 +0000740func (paths *scopePaths) treatDepAsApiStubsProvider(dep android.Module, action func(provider ApiStubsProvider) error) error {
Paul Duffin0ff08bd2020-04-29 13:30:54 +0100741 if apiStubsProvider, ok := dep.(ApiStubsProvider); ok {
Jihoon Kangee113282024-01-23 00:16:41 +0000742 err := action(apiStubsProvider)
743 if err != nil {
744 return err
745 }
Jihoon Kangf55a5f72024-01-08 08:56:20 +0000746 return nil
747 } else {
748 return fmt.Errorf("expected module that implements ExportableApiStubsSrcProvider, e.g. droidstubs")
749 }
750}
751
Jihoon Kangee113282024-01-23 00:16:41 +0000752func (paths *scopePaths) treatDepAsApiStubsSrcProvider(dep android.Module, action func(provider ApiStubsSrcProvider) error) error {
Paul Duffin0f8faff2020-05-20 16:18:00 +0100753 if apiStubsProvider, ok := dep.(ApiStubsSrcProvider); ok {
Jihoon Kangee113282024-01-23 00:16:41 +0000754 err := action(apiStubsProvider)
755 if err != nil {
756 return err
757 }
Paul Duffin0f8faff2020-05-20 16:18:00 +0100758 return nil
759 } else {
760 return fmt.Errorf("expected module that implements ApiStubsSrcProvider, e.g. droidstubs")
761 }
762}
763
Jihoon Kangee113282024-01-23 00:16:41 +0000764func (paths *scopePaths) extractApiInfoFromApiStubsProvider(provider ApiStubsProvider, stubsType StubsType) error {
765 var annotationsZip, currentApiFilePath, removedApiFilePath android.Path
766 annotationsZip, annotationsZipErr := provider.AnnotationsZip(stubsType)
767 currentApiFilePath, currentApiFilePathErr := provider.ApiFilePath(stubsType)
768 removedApiFilePath, removedApiFilePathErr := provider.RemovedApiFilePath(stubsType)
Paul Duffin0ff08bd2020-04-29 13:30:54 +0100769
Jihoon Kangee113282024-01-23 00:16:41 +0000770 combinedError := errors.Join(annotationsZipErr, currentApiFilePathErr, removedApiFilePathErr)
771
772 if combinedError == nil {
773 paths.annotationsZip = android.OptionalPathForPath(annotationsZip)
774 paths.currentApiFilePath = android.OptionalPathForPath(currentApiFilePath)
775 paths.removedApiFilePath = android.OptionalPathForPath(removedApiFilePath)
776 }
777 return combinedError
Jihoon Kangf55a5f72024-01-08 08:56:20 +0000778}
779
Jihoon Kangee113282024-01-23 00:16:41 +0000780func (paths *scopePaths) extractStubsSourceInfoFromApiStubsProviders(provider ApiStubsSrcProvider, stubsType StubsType) error {
781 stubsSrcJar, err := provider.StubsSrcJar(stubsType)
782 if err == nil {
783 paths.stubsSrcJar = android.OptionalPathForPath(stubsSrcJar)
784 }
785 return err
Jihoon Kangf55a5f72024-01-08 08:56:20 +0000786}
787
Colin Crossdcf71b22021-02-01 13:59:03 -0800788func (paths *scopePaths) extractStubsSourceInfoFromDep(ctx android.ModuleContext, dep android.Module) error {
Jihoon Kang2a26b132024-06-24 07:39:40 +0000789 stubsType := Everything
790 if ctx.Config().ReleaseHiddenApiExportableStubs() {
791 stubsType = Exportable
792 }
Jihoon Kangee113282024-01-23 00:16:41 +0000793 return paths.treatDepAsApiStubsSrcProvider(dep, func(provider ApiStubsSrcProvider) error {
Jihoon Kang2a26b132024-06-24 07:39:40 +0000794 return paths.extractStubsSourceInfoFromApiStubsProviders(provider, stubsType)
Paul Duffin0ff08bd2020-04-29 13:30:54 +0100795 })
796}
797
Colin Crossdcf71b22021-02-01 13:59:03 -0800798func (paths *scopePaths) extractStubsSourceAndApiInfoFromApiStubsProvider(ctx android.ModuleContext, dep android.Module) error {
Jihoon Kang2a26b132024-06-24 07:39:40 +0000799 stubsType := Everything
Jihoon Kangf55a5f72024-01-08 08:56:20 +0000800 if ctx.Config().ReleaseHiddenApiExportableStubs() {
Jihoon Kang2a26b132024-06-24 07:39:40 +0000801 stubsType = Exportable
Jihoon Kangf55a5f72024-01-08 08:56:20 +0000802 }
Jihoon Kangee113282024-01-23 00:16:41 +0000803 return paths.treatDepAsApiStubsProvider(dep, func(provider ApiStubsProvider) error {
Jihoon Kang2a26b132024-06-24 07:39:40 +0000804 extractApiInfoErr := paths.extractApiInfoFromApiStubsProvider(provider, stubsType)
805 extractStubsSourceInfoErr := paths.extractStubsSourceInfoFromApiStubsProviders(provider, stubsType)
Jihoon Kangee113282024-01-23 00:16:41 +0000806 return errors.Join(extractApiInfoErr, extractStubsSourceInfoErr)
Paul Duffin0ff08bd2020-04-29 13:30:54 +0100807 })
808}
809
Jihoon Kang5623e542024-01-31 23:27:26 +0000810func extractOutputPaths(dep android.Module) (android.Paths, error) {
Paul Duffin958806b2022-05-16 13:10:47 +0000811 var paths android.Paths
812 if sourceFileProducer, ok := dep.(android.SourceFileProducer); ok {
813 paths = sourceFileProducer.Srcs()
Jihoon Kang5623e542024-01-31 23:27:26 +0000814 return paths, nil
Paul Duffin958806b2022-05-16 13:10:47 +0000815 } else {
Jihoon Kang5623e542024-01-31 23:27:26 +0000816 return nil, fmt.Errorf("module %q does not produce source files", dep)
Paul Duffin958806b2022-05-16 13:10:47 +0000817 }
Paul Duffin958806b2022-05-16 13:10:47 +0000818}
819
820func (paths *scopePaths) extractLatestApiPath(ctx android.ModuleContext, dep android.Module) error {
Jihoon Kang5623e542024-01-31 23:27:26 +0000821 outputPaths, err := extractOutputPaths(dep)
822 paths.latestApiPaths = outputPaths
Paul Duffin958806b2022-05-16 13:10:47 +0000823 return err
824}
825
826func (paths *scopePaths) extractLatestRemovedApiPath(ctx android.ModuleContext, dep android.Module) error {
Jihoon Kang5623e542024-01-31 23:27:26 +0000827 outputPaths, err := extractOutputPaths(dep)
828 paths.latestRemovedApiPaths = outputPaths
Paul Duffin958806b2022-05-16 13:10:47 +0000829 return err
830}
831
Paul Duffin0ff08bd2020-04-29 13:30:54 +0100832type commonToSdkLibraryAndImportProperties struct {
Paul Duffin1b1e8062020-05-08 13:44:43 +0100833 // The naming scheme to use for the components that this module creates.
834 //
Paul Duffinee9ad5d2020-09-11 13:04:05 +0100835 // If not specified then it defaults to "default".
Paul Duffin1b1e8062020-05-08 13:44:43 +0100836 //
837 // This is a temporary mechanism to simplify conversion from separate modules for each
838 // component that follow a different naming pattern to the default one.
839 //
840 // TODO(b/155480189) - Remove once naming inconsistencies have been resolved.
Paul Duffin0ff08bd2020-04-29 13:30:54 +0100841 Naming_scheme *string
Paul Duffindfa131e2020-05-15 20:37:11 +0100842
843 // Specifies whether this module can be used as an Android shared library; defaults
844 // to true.
845 //
846 // An Android shared library is one that can be referenced in a <uses-library> element
847 // in an AndroidManifest.xml.
848 Shared_library *bool
Paul Duffina2ae7e02020-09-11 11:55:00 +0100849
850 // Files containing information about supported java doc tags.
851 Doctag_files []string `android:"path"`
Pedro Loureiro9956e5e2021-09-07 17:21:59 +0000852
853 // Signals that this shared library is part of the bootclasspath starting
854 // on the version indicated in this attribute.
855 //
856 // This will make platforms at this level and above to ignore
857 // <uses-library> tags with this library name because the library is already
858 // available
859 On_bootclasspath_since *string
860
861 // Signals that this shared library was part of the bootclasspath before
862 // (but not including) the version indicated in this attribute.
863 //
864 // The system will automatically add a <uses-library> tag with this library to
865 // apps that target any SDK less than the version indicated in this attribute.
866 On_bootclasspath_before *string
867
868 // Indicates that PackageManager should ignore this shared library if the
869 // platform is below the version indicated in this attribute.
870 //
871 // This means that the device won't recognise this library as installed.
872 Min_device_sdk *string
873
874 // Indicates that PackageManager should ignore this shared library if the
875 // platform is above the version indicated in this attribute.
876 //
877 // This means that the device won't recognise this library as installed.
878 Max_device_sdk *string
Paul Duffin0ff08bd2020-04-29 13:30:54 +0100879}
880
Paul Duffin71b33cc2021-06-23 11:39:47 +0100881// commonSdkLibraryAndImportModule defines the interface that must be provided by a module that
882// embeds the commonToSdkLibraryAndImport struct.
883type commonSdkLibraryAndImportModule interface {
Paul Duffind796f6f2022-11-23 23:06:05 +0000884 android.Module
Paul Duffin71b33cc2021-06-23 11:39:47 +0100885
Spandan Das23956d12024-01-19 00:22:22 +0000886 // Returns the name of the root java_sdk_library that creates the child stub libraries
887 // This is the `name` as it appears in Android.bp, and not the name in Soong's build graph
888 // (with the prebuilt_ prefix)
889 //
890 // e.g. in the following java_sdk_library_import
891 // java_sdk_library_import {
892 // name: "framework-foo.v1",
893 // source_module_name: "framework-foo",
894 // }
895 // the values returned by
896 // 1. Name(): prebuilt_framework-foo.v1 # unique
897 // 2. BaseModuleName(): framework-foo # the source
898 // 3. RootLibraryName: framework-foo.v1 # the undecordated `name` from Android.bp
899 RootLibraryName() string
900}
901
902func (m *SdkLibrary) RootLibraryName() string {
903 return m.BaseModuleName()
904}
905
906func (m *SdkLibraryImport) RootLibraryName() string {
907 // m.BaseModuleName refers to the source of the import
908 // use moduleBase.Name to get the name of the module as it appears in the .bp file
909 return m.ModuleBase.Name()
Paul Duffin71b33cc2021-06-23 11:39:47 +0100910}
911
Paul Duffin56d44902020-01-31 13:36:25 +0000912// Common code between sdk library and sdk library import
913type commonToSdkLibraryAndImport struct {
Paul Duffin71b33cc2021-06-23 11:39:47 +0100914 module commonSdkLibraryAndImportModule
Paul Duffinc3091c82020-05-08 14:16:20 +0100915
Paul Duffin56d44902020-01-31 13:36:25 +0000916 scopePaths map[*apiScope]*scopePaths
Paul Duffin1b1e8062020-05-08 13:44:43 +0100917
918 namingScheme sdkLibraryComponentNamingScheme
919
Paul Duffindfa131e2020-05-15 20:37:11 +0100920 commonSdkLibraryProperties commonToSdkLibraryAndImportProperties
Paul Duffin859fe962020-05-15 10:20:31 +0100921
Paul Duffina2ae7e02020-09-11 11:55:00 +0100922 // Paths to commonSdkLibraryProperties.Doctag_files
923 doctagPaths android.Paths
924
Paul Duffin859fe962020-05-15 10:20:31 +0100925 // Functionality related to this being used as a component of a java_sdk_library.
926 EmbeddableSdkLibraryComponent
Jihoon Kang8479dea2024-04-04 01:19:05 +0000927
928 // Path to the header jars of the implementation library
929 // This is non-empty only when api_only is false.
930 implLibraryHeaderJars android.Paths
Jihoon Kanga3a05462024-04-05 00:36:44 +0000931
932 // The reference to the implementation library created by the source module.
933 // Is nil if the source module does not exist.
934 implLibraryModule *Library
Paul Duffin56d44902020-01-31 13:36:25 +0000935}
936
Paul Duffin71b33cc2021-06-23 11:39:47 +0100937func (c *commonToSdkLibraryAndImport) initCommon(module commonSdkLibraryAndImportModule) {
938 c.module = module
Paul Duffin1b1e8062020-05-08 13:44:43 +0100939
Paul Duffin71b33cc2021-06-23 11:39:47 +0100940 module.AddProperties(&c.commonSdkLibraryProperties)
Paul Duffin859fe962020-05-15 10:20:31 +0100941
942 // Initialize this as an sdk library component.
Paul Duffin71b33cc2021-06-23 11:39:47 +0100943 c.initSdkLibraryComponent(module)
Paul Duffin1b1e8062020-05-08 13:44:43 +0100944}
945
946func (c *commonToSdkLibraryAndImport) initCommonAfterDefaultsApplied(ctx android.DefaultableHookContext) bool {
Paul Duffindfa131e2020-05-15 20:37:11 +0100947 schemeProperty := proptools.StringDefault(c.commonSdkLibraryProperties.Naming_scheme, "default")
Paul Duffin1b1e8062020-05-08 13:44:43 +0100948 switch schemeProperty {
949 case "default":
950 c.namingScheme = &defaultNamingScheme{}
951 default:
952 ctx.PropertyErrorf("naming_scheme", "expected 'default' but was %q", schemeProperty)
953 return false
954 }
955
Spandan Das23956d12024-01-19 00:22:22 +0000956 namePtr := proptools.StringPtr(c.module.RootLibraryName())
Paul Duffin3f0290e2021-06-30 18:25:36 +0100957 c.sdkLibraryComponentProperties.SdkLibraryName = namePtr
958
Paul Duffindfa131e2020-05-15 20:37:11 +0100959 // Only track this sdk library if this can be used as a shared library.
960 if c.sharedLibrary() {
961 // Use the name specified in the module definition as the owner.
Paul Duffin3f0290e2021-06-30 18:25:36 +0100962 c.sdkLibraryComponentProperties.SdkLibraryToImplicitlyTrack = namePtr
Paul Duffindfa131e2020-05-15 20:37:11 +0100963 }
Paul Duffin859fe962020-05-15 10:20:31 +0100964
Paul Duffin1b1e8062020-05-08 13:44:43 +0100965 return true
Paul Duffinc3091c82020-05-08 14:16:20 +0100966}
967
Paul Duffinea8f8082021-06-24 13:25:57 +0100968// uniqueApexVariations provides common implementation of the ApexModule.UniqueApexVariations
969// method.
970func (c *commonToSdkLibraryAndImport) uniqueApexVariations() bool {
971 // A java_sdk_library that is a shared library produces an XML file that makes the shared library
972 // usable from an AndroidManifest.xml's <uses-library> entry. That XML file contains the name of
973 // the APEX and so it needs a unique variation per APEX.
974 return c.sharedLibrary()
975}
976
Paul Duffina2ae7e02020-09-11 11:55:00 +0100977func (c *commonToSdkLibraryAndImport) generateCommonBuildActions(ctx android.ModuleContext) {
978 c.doctagPaths = android.PathsForModuleSrc(ctx, c.commonSdkLibraryProperties.Doctag_files)
979}
980
Jihoon Kanga3a05462024-04-05 00:36:44 +0000981func (c *commonToSdkLibraryAndImport) getImplLibraryModule() *Library {
982 return c.implLibraryModule
983}
984
Paul Duffineedc5d52020-06-12 17:46:39 +0100985// Module name of the runtime implementation library
986func (c *commonToSdkLibraryAndImport) implLibraryModuleName() string {
Spandan Das23956d12024-01-19 00:22:22 +0000987 return c.module.RootLibraryName() + ".impl"
Paul Duffineedc5d52020-06-12 17:46:39 +0100988}
989
990// Module name of the XML file for the lib
991func (c *commonToSdkLibraryAndImport) xmlPermissionsModuleName() string {
Spandan Das23956d12024-01-19 00:22:22 +0000992 return c.module.RootLibraryName() + sdkXmlFileSuffix
Paul Duffineedc5d52020-06-12 17:46:39 +0100993}
994
Paul Duffinc3091c82020-05-08 14:16:20 +0100995// Name of the java_library module that compiles the stubs source.
996func (c *commonToSdkLibraryAndImport) stubsLibraryModuleName(apiScope *apiScope) string {
Spandan Das23956d12024-01-19 00:22:22 +0000997 baseName := c.module.RootLibraryName()
Paul Duffin21787622022-11-25 12:48:20 +0000998 return c.namingScheme.stubsLibraryModuleName(apiScope, baseName)
Paul Duffinc3091c82020-05-08 14:16:20 +0100999}
1000
Jihoon Kangfa4a90d2023-12-20 02:53:38 +00001001// Name of the java_library module that compiles the exportable stubs source.
1002func (c *commonToSdkLibraryAndImport) exportableStubsLibraryModuleName(apiScope *apiScope) string {
Spandan Das23956d12024-01-19 00:22:22 +00001003 baseName := c.module.RootLibraryName()
Jihoon Kangfa4a90d2023-12-20 02:53:38 +00001004 return c.namingScheme.exportableStubsLibraryModuleName(apiScope, baseName)
1005}
1006
Paul Duffinc3091c82020-05-08 14:16:20 +01001007// Name of the droidstubs module that generates the stubs source and may also
1008// generate/check the API.
1009func (c *commonToSdkLibraryAndImport) stubsSourceModuleName(apiScope *apiScope) string {
Spandan Das23956d12024-01-19 00:22:22 +00001010 baseName := c.module.RootLibraryName()
Paul Duffin21787622022-11-25 12:48:20 +00001011 return c.namingScheme.stubsSourceModuleName(apiScope, baseName)
Paul Duffinc3091c82020-05-08 14:16:20 +01001012}
1013
Jihoon Kang1c92c3e2023-03-23 17:44:51 +00001014// Name of the java_api_library module that generates the from-text stubs source
1015// and compiles to a jar file.
1016func (c *commonToSdkLibraryAndImport) apiLibraryModuleName(apiScope *apiScope) string {
Spandan Das23956d12024-01-19 00:22:22 +00001017 baseName := c.module.RootLibraryName()
Jihoon Kang1c92c3e2023-03-23 17:44:51 +00001018 return c.namingScheme.apiLibraryModuleName(apiScope, baseName)
1019}
1020
Jihoon Kang1147b312023-06-08 23:25:57 +00001021// Name of the java_library module that compiles the stubs
1022// generated from source Java files.
Jihoon Kangfa4a90d2023-12-20 02:53:38 +00001023func (c *commonToSdkLibraryAndImport) sourceStubsLibraryModuleName(apiScope *apiScope) string {
Spandan Das23956d12024-01-19 00:22:22 +00001024 baseName := c.module.RootLibraryName()
Jihoon Kangfa4a90d2023-12-20 02:53:38 +00001025 return c.namingScheme.sourceStubsLibraryModuleName(apiScope, baseName)
1026}
1027
1028// Name of the java_library module that compiles the exportable stubs
1029// generated from source Java files.
1030func (c *commonToSdkLibraryAndImport) exportableSourceStubsLibraryModuleName(apiScope *apiScope) string {
Spandan Das23956d12024-01-19 00:22:22 +00001031 baseName := c.module.RootLibraryName()
Jihoon Kangfa4a90d2023-12-20 02:53:38 +00001032 return c.namingScheme.exportableSourceStubsLibraryModuleName(apiScope, baseName)
Jihoon Kang1147b312023-06-08 23:25:57 +00001033}
1034
Paul Duffin46dc45a2020-05-14 15:39:10 +01001035// The component names for different outputs of the java_sdk_library.
1036//
1037// They are similar to the names used for the child modules it creates
1038const (
1039 stubsSourceComponentName = "stubs.source"
1040
1041 apiTxtComponentName = "api.txt"
1042
1043 removedApiTxtComponentName = "removed-api.txt"
Anton Hanssond78eb762021-09-21 15:25:12 +01001044
1045 annotationsComponentName = "annotations.zip"
Paul Duffin46dc45a2020-05-14 15:39:10 +01001046)
1047
mrziwang9f7b9f42024-07-10 12:18:06 -07001048func (module *commonToSdkLibraryAndImport) setOutputFiles(ctx android.ModuleContext) {
1049 if module.doctagPaths != nil {
1050 ctx.SetOutputFiles(module.doctagPaths, ".doctags")
1051 }
1052 for _, scopeName := range android.SortedKeys(scopeByName) {
1053 paths := module.findScopePaths(scopeByName[scopeName])
1054 if paths == nil {
1055 continue
Paul Duffin46dc45a2020-05-14 15:39:10 +01001056 }
mrziwang9f7b9f42024-07-10 12:18:06 -07001057 componentToOutput := map[string]android.OptionalPath{
1058 stubsSourceComponentName: paths.stubsSrcJar,
1059 apiTxtComponentName: paths.currentApiFilePath,
1060 removedApiTxtComponentName: paths.removedApiFilePath,
1061 annotationsComponentName: paths.annotationsZip,
1062 }
1063 for _, component := range android.SortedKeys(componentToOutput) {
1064 if componentToOutput[component].Valid() {
1065 ctx.SetOutputFiles(android.Paths{componentToOutput[component].Path()}, "."+scopeName+"."+component)
Paul Duffina2ae7e02020-09-11 11:55:00 +01001066 }
1067 }
Paul Duffin46dc45a2020-05-14 15:39:10 +01001068 }
1069}
1070
Paul Duffin803a9562020-05-20 11:52:25 +01001071func (c *commonToSdkLibraryAndImport) getScopePathsCreateIfNeeded(scope *apiScope) *scopePaths {
Paul Duffin56d44902020-01-31 13:36:25 +00001072 if c.scopePaths == nil {
1073 c.scopePaths = make(map[*apiScope]*scopePaths)
1074 }
1075 paths := c.scopePaths[scope]
1076 if paths == nil {
1077 paths = &scopePaths{}
1078 c.scopePaths[scope] = paths
1079 }
1080
1081 return paths
1082}
1083
Paul Duffin803a9562020-05-20 11:52:25 +01001084func (c *commonToSdkLibraryAndImport) findScopePaths(scope *apiScope) *scopePaths {
1085 if c.scopePaths == nil {
1086 return nil
1087 }
1088
1089 return c.scopePaths[scope]
1090}
1091
1092// If this does not support the requested api scope then find the closest available
1093// scope it does support. Returns nil if no such scope is available.
1094func (c *commonToSdkLibraryAndImport) findClosestScopePath(scope *apiScope) *scopePaths {
Paul Duffind0b9fca2022-09-30 18:11:41 +01001095 for s := scope; s != nil; s = s.canAccess {
Paul Duffin803a9562020-05-20 11:52:25 +01001096 if paths := c.findScopePaths(s); paths != nil {
1097 return paths
1098 }
1099 }
1100
1101 // This should never happen outside tests as public should be the base scope for every
1102 // scope and is enabled by default.
1103 return nil
1104}
1105
Jiyong Parkf1691d22021-03-29 20:11:58 +09001106func (c *commonToSdkLibraryAndImport) selectHeaderJarsForSdkVersion(ctx android.BaseModuleContext, sdkVersion android.SdkSpec) android.Paths {
Paul Duffinb05d4292020-05-20 12:19:10 +01001107
1108 // If a specific numeric version has been requested then use prebuilt versions of the sdk.
Jiyong Park54105c42021-03-31 18:17:53 +09001109 if !sdkVersion.ApiLevel.IsPreview() {
Spandan Das23956d12024-01-19 00:22:22 +00001110 return PrebuiltJars(ctx, c.module.RootLibraryName(), sdkVersion)
Paul Duffinb05d4292020-05-20 12:19:10 +01001111 }
1112
Paul Duffin1267d872021-04-16 17:21:36 +01001113 paths := c.selectScopePaths(ctx, sdkVersion.Kind)
1114 if paths == nil {
1115 return nil
1116 }
1117
1118 return paths.stubsHeaderPath
1119}
1120
1121// selectScopePaths returns the *scopePaths appropriate for the specific kind.
1122//
1123// If the module does not support the specific kind then it will return the *scopePaths for the
1124// closest kind which is a subset of the requested kind. e.g. if requesting android.SdkModule then
1125// it will return *scopePaths for android.SdkSystem if available or android.SdkPublic of not.
1126func (c *commonToSdkLibraryAndImport) selectScopePaths(ctx android.BaseModuleContext, kind android.SdkKind) *scopePaths {
Paul Duffin32cf58a2021-05-18 16:32:50 +01001127 apiScope := sdkKindToApiScope(kind)
Paul Duffinb05d4292020-05-20 12:19:10 +01001128
Paul Duffin803a9562020-05-20 11:52:25 +01001129 paths := c.findClosestScopePath(apiScope)
1130 if paths == nil {
1131 var scopes []string
Jihoon Kang98aa8fa2024-06-07 11:06:57 +00001132 for _, s := range AllApiScopes {
Paul Duffin803a9562020-05-20 11:52:25 +01001133 if c.findScopePaths(s) != nil {
1134 scopes = append(scopes, s.name)
1135 }
1136 }
Spandan Das23956d12024-01-19 00:22:22 +00001137 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 +01001138 return nil
1139 }
1140
Paul Duffin1267d872021-04-16 17:21:36 +01001141 return paths
1142}
1143
Paul Duffin32cf58a2021-05-18 16:32:50 +01001144// sdkKindToApiScope maps from android.SdkKind to apiScope.
1145func sdkKindToApiScope(kind android.SdkKind) *apiScope {
1146 var apiScope *apiScope
1147 switch kind {
1148 case android.SdkSystem:
1149 apiScope = apiScopeSystem
1150 case android.SdkModule:
1151 apiScope = apiScopeModuleLib
1152 case android.SdkTest:
1153 apiScope = apiScopeTest
1154 case android.SdkSystemServer:
1155 apiScope = apiScopeSystemServer
1156 default:
1157 apiScope = apiScopePublic
1158 }
1159 return apiScope
1160}
1161
Paul Duffin1267d872021-04-16 17:21:36 +01001162// to satisfy SdkLibraryDependency interface
Martin Stjernholm8be1e6d2021-09-15 03:34:04 +01001163func (c *commonToSdkLibraryAndImport) SdkApiStubDexJar(ctx android.BaseModuleContext, kind android.SdkKind) OptionalDexJarPath {
Paul Duffin1267d872021-04-16 17:21:36 +01001164 paths := c.selectScopePaths(ctx, kind)
1165 if paths == nil {
Martin Stjernholm8be1e6d2021-09-15 03:34:04 +01001166 return makeUnsetDexJarPath()
Paul Duffin1267d872021-04-16 17:21:36 +01001167 }
1168
1169 return paths.stubsDexJarPath
Paul Duffinb05d4292020-05-20 12:19:10 +01001170}
1171
Paul Duffin32cf58a2021-05-18 16:32:50 +01001172// to satisfy SdkLibraryDependency interface
Jihoon Kangbd093452023-12-26 19:08:01 +00001173func (c *commonToSdkLibraryAndImport) SdkApiExportableStubDexJar(ctx android.BaseModuleContext, kind android.SdkKind) OptionalDexJarPath {
1174 paths := c.selectScopePaths(ctx, kind)
1175 if paths == nil {
1176 return makeUnsetDexJarPath()
1177 }
1178
1179 return paths.exportableStubsDexJarPath
1180}
1181
1182// to satisfy SdkLibraryDependency interface
Paul Duffin32cf58a2021-05-18 16:32:50 +01001183func (c *commonToSdkLibraryAndImport) SdkRemovedTxtFile(ctx android.BaseModuleContext, kind android.SdkKind) android.OptionalPath {
1184 apiScope := sdkKindToApiScope(kind)
1185 paths := c.findScopePaths(apiScope)
1186 if paths == nil {
1187 return android.OptionalPath{}
1188 }
1189
1190 return paths.removedApiFilePath
1191}
1192
Paul Duffin859fe962020-05-15 10:20:31 +01001193func (c *commonToSdkLibraryAndImport) sdkComponentPropertiesForChildLibrary() interface{} {
1194 componentProps := &struct {
Paul Duffin3f0290e2021-06-30 18:25:36 +01001195 SdkLibraryName *string
Paul Duffin859fe962020-05-15 10:20:31 +01001196 SdkLibraryToImplicitlyTrack *string
Paul Duffindfa131e2020-05-15 20:37:11 +01001197 }{}
1198
Spandan Das23956d12024-01-19 00:22:22 +00001199 namePtr := proptools.StringPtr(c.module.RootLibraryName())
Paul Duffin3f0290e2021-06-30 18:25:36 +01001200 componentProps.SdkLibraryName = namePtr
1201
Paul Duffindfa131e2020-05-15 20:37:11 +01001202 if c.sharedLibrary() {
Paul Duffin859fe962020-05-15 10:20:31 +01001203 // Mark the stubs library as being components of this java_sdk_library so that
1204 // any app that includes code which depends (directly or indirectly) on the stubs
1205 // library will have the appropriate <uses-library> invocation inserted into its
1206 // manifest if necessary.
Paul Duffin3f0290e2021-06-30 18:25:36 +01001207 componentProps.SdkLibraryToImplicitlyTrack = namePtr
Paul Duffin859fe962020-05-15 10:20:31 +01001208 }
1209
1210 return componentProps
1211}
1212
Paul Duffindfa131e2020-05-15 20:37:11 +01001213func (c *commonToSdkLibraryAndImport) sharedLibrary() bool {
1214 return proptools.BoolDefault(c.commonSdkLibraryProperties.Shared_library, true)
1215}
1216
Paul Duffinf4600f62021-05-13 22:34:45 +01001217// Check if the stub libraries should be compiled for dex
1218func (c *commonToSdkLibraryAndImport) stubLibrariesCompiledForDex() bool {
1219 // Always compile the dex file files for the stub libraries if they will be used on the
1220 // bootclasspath.
1221 return !c.sharedLibrary()
1222}
1223
Paul Duffin859fe962020-05-15 10:20:31 +01001224// Properties related to the use of a module as an component of a java_sdk_library.
1225type SdkLibraryComponentProperties struct {
Paul Duffin3f0290e2021-06-30 18:25:36 +01001226 // The name of the java_sdk_library/_import module.
1227 SdkLibraryName *string `blueprint:"mutated"`
Paul Duffin859fe962020-05-15 10:20:31 +01001228
1229 // The name of the java_sdk_library/_import to add to a <uses-library> entry
1230 // in the AndroidManifest.xml of any Android app that includes code that references
1231 // this module. If not set then no java_sdk_library/_import is tracked.
1232 SdkLibraryToImplicitlyTrack *string `blueprint:"mutated"`
1233}
1234
1235// Structure to be embedded in a module struct that needs to support the
1236// SdkLibraryComponentDependency interface.
1237type EmbeddableSdkLibraryComponent struct {
1238 sdkLibraryComponentProperties SdkLibraryComponentProperties
1239}
1240
Paul Duffin71b33cc2021-06-23 11:39:47 +01001241func (e *EmbeddableSdkLibraryComponent) initSdkLibraryComponent(module android.Module) {
1242 module.AddProperties(&e.sdkLibraryComponentProperties)
Paul Duffin859fe962020-05-15 10:20:31 +01001243}
1244
1245// to satisfy SdkLibraryComponentDependency
Paul Duffin3f0290e2021-06-30 18:25:36 +01001246func (e *EmbeddableSdkLibraryComponent) SdkLibraryName() *string {
1247 return e.sdkLibraryComponentProperties.SdkLibraryName
1248}
1249
1250// to satisfy SdkLibraryComponentDependency
Ulya Trafimovich39b437b2020-09-23 16:42:35 +01001251func (e *EmbeddableSdkLibraryComponent) OptionalSdkLibraryImplementation() *string {
Ulya Trafimovich78645fb2021-07-16 15:29:25 +01001252 // For shared libraries, this is the same as the SDK library name. If a Java library or app
1253 // depends on a component library (e.g. a stub library) it still needs to know the name of the
1254 // run-time library and the corresponding module that provides the implementation. This name is
1255 // passed to manifest_fixer (to be added to AndroidManifest.xml) and added to CLC (to be used
1256 // in dexpreopt).
1257 //
1258 // For non-shared SDK (component or not) libraries this returns `nil`, as they are not
1259 // <uses-library> and should not be added to the manifest or to CLC.
Ulya Trafimovich39b437b2020-09-23 16:42:35 +01001260 return e.sdkLibraryComponentProperties.SdkLibraryToImplicitlyTrack
1261}
1262
Paul Duffin859fe962020-05-15 10:20:31 +01001263// Implemented by modules that are (or possibly could be) a component of a java_sdk_library
1264// (including the java_sdk_library) itself.
1265type SdkLibraryComponentDependency interface {
Ulya Trafimovich31e444e2020-08-14 17:32:16 +01001266 UsesLibraryDependency
1267
Paul Duffin3f0290e2021-06-30 18:25:36 +01001268 // SdkLibraryName returns the name of the java_sdk_library/_import module.
1269 SdkLibraryName() *string
1270
Ulya Trafimovich39b437b2020-09-23 16:42:35 +01001271 // The name of the implementation library for the optional SDK library or nil, if there isn't one.
1272 OptionalSdkLibraryImplementation() *string
Paul Duffin859fe962020-05-15 10:20:31 +01001273}
1274
1275// Make sure that all the module types that are components of java_sdk_library/_import
1276// and which can be referenced (directly or indirectly) from an android app implement
1277// the SdkLibraryComponentDependency interface.
1278var _ SdkLibraryComponentDependency = (*Library)(nil)
1279var _ SdkLibraryComponentDependency = (*Import)(nil)
1280var _ SdkLibraryComponentDependency = (*SdkLibrary)(nil)
Paul Duffineedc5d52020-06-12 17:46:39 +01001281var _ SdkLibraryComponentDependency = (*SdkLibraryImport)(nil)
Paul Duffin859fe962020-05-15 10:20:31 +01001282
Paul Duffin32cf58a2021-05-18 16:32:50 +01001283// Provides access to sdk_version related files, e.g. header and implementation jars.
Paul Duffin859fe962020-05-15 10:20:31 +01001284type SdkLibraryDependency interface {
1285 SdkLibraryComponentDependency
1286
1287 // Get the header jars appropriate for the supplied sdk_version.
1288 //
1289 // These are turbine generated jars so they only change if the externals of the
1290 // class changes but it does not contain and implementation or JavaDoc.
Jiyong Parkf1691d22021-03-29 20:11:58 +09001291 SdkHeaderJars(ctx android.BaseModuleContext, sdkVersion android.SdkSpec) android.Paths
Paul Duffin859fe962020-05-15 10:20:31 +01001292
Jihoon Kangbd093452023-12-26 19:08:01 +00001293 // SdkApiStubDexJar returns the dex jar for the stubs for the prebuilt
1294 // java_sdk_library_import module. It is needed by the hiddenapi processing tool which
1295 // processes dex files.
Martin Stjernholm8be1e6d2021-09-15 03:34:04 +01001296 SdkApiStubDexJar(ctx android.BaseModuleContext, kind android.SdkKind) OptionalDexJarPath
Paul Duffinf4600f62021-05-13 22:34:45 +01001297
Jihoon Kangbd093452023-12-26 19:08:01 +00001298 // SdkApiExportableStubDexJar returns the exportable dex jar for the stubs for
1299 // java_sdk_library module. It is needed by the hiddenapi processing tool which processes
1300 // dex files.
1301 SdkApiExportableStubDexJar(ctx android.BaseModuleContext, kind android.SdkKind) OptionalDexJarPath
1302
Paul Duffin32cf58a2021-05-18 16:32:50 +01001303 // SdkRemovedTxtFile returns the optional path to the removed.txt file for the specified sdk kind.
1304 SdkRemovedTxtFile(ctx android.BaseModuleContext, kind android.SdkKind) android.OptionalPath
1305
Paul Duffinf4600f62021-05-13 22:34:45 +01001306 // sharedLibrary returns true if this can be used as a shared library.
1307 sharedLibrary() bool
Jihoon Kanga3a05462024-04-05 00:36:44 +00001308
1309 getImplLibraryModule() *Library
Paul Duffin859fe962020-05-15 10:20:31 +01001310}
1311
Inseob Kimc0907f12019-02-08 21:00:45 +09001312type SdkLibrary struct {
Sundong Ahn054b19a2018-10-19 13:46:09 +09001313 Library
Jiyong Parkc678ad32018-04-10 13:07:10 +09001314
Sundong Ahn054b19a2018-10-19 13:46:09 +09001315 sdkLibraryProperties sdkLibraryProperties
Jiyong Parkc678ad32018-04-10 13:07:10 +09001316
Paul Duffin3375e352020-04-28 10:44:03 +01001317 // Map from api scope to the scope specific property structure.
1318 scopeToProperties map[*apiScope]*ApiScopeProperties
1319
Paul Duffin56d44902020-01-31 13:36:25 +00001320 commonToSdkLibraryAndImport
Jihoon Kanga3a05462024-04-05 00:36:44 +00001321
1322 builtInstalledForApex []dexpreopterInstall
Jiyong Parkc678ad32018-04-10 13:07:10 +09001323}
1324
Inseob Kimc0907f12019-02-08 21:00:45 +09001325var _ SdkLibraryDependency = (*SdkLibrary)(nil)
Colin Cross897d2ed2019-02-11 14:03:51 -08001326
Paul Duffin3375e352020-04-28 10:44:03 +01001327func (module *SdkLibrary) generateTestAndSystemScopesByDefault() bool {
1328 return module.sdkLibraryProperties.Generate_system_and_test_apis
1329}
1330
Jihoon Kanga3a05462024-04-05 00:36:44 +00001331func (module *SdkLibrary) DexJarBuildPath(ctx android.ModuleErrorfContext) OptionalDexJarPath {
1332 if module.implLibraryModule != nil {
1333 return module.implLibraryModule.DexJarBuildPath(ctx)
1334 }
1335 return makeUnsetDexJarPath()
1336}
1337
1338func (module *SdkLibrary) DexJarInstallPath() android.Path {
1339 if module.implLibraryModule != nil {
1340 return module.implLibraryModule.DexJarInstallPath()
1341 }
1342 return nil
1343}
1344
Paul Duffin3375e352020-04-28 10:44:03 +01001345func (module *SdkLibrary) getGeneratedApiScopes(ctx android.EarlyModuleContext) apiScopes {
1346 // Check to see if any scopes have been explicitly enabled. If any have then all
1347 // must be.
1348 anyScopesExplicitlyEnabled := false
Jihoon Kang98aa8fa2024-06-07 11:06:57 +00001349 for _, scope := range AllApiScopes {
Paul Duffin3375e352020-04-28 10:44:03 +01001350 scopeProperties := module.scopeToProperties[scope]
1351 if scopeProperties.Enabled != nil {
1352 anyScopesExplicitlyEnabled = true
1353 break
1354 }
Paul Duffind1b3a922020-01-22 11:57:20 +00001355 }
Paul Duffin3375e352020-04-28 10:44:03 +01001356
1357 var generatedScopes apiScopes
1358 enabledScopes := make(map[*apiScope]struct{})
Jihoon Kang98aa8fa2024-06-07 11:06:57 +00001359 for _, scope := range AllApiScopes {
Paul Duffin3375e352020-04-28 10:44:03 +01001360 scopeProperties := module.scopeToProperties[scope]
1361 // If any scopes are explicitly enabled then ignore the legacy enabled status.
1362 // This is to ensure that any new usages of this module type do not rely on legacy
1363 // behaviour.
1364 defaultEnabledStatus := false
1365 if anyScopesExplicitlyEnabled {
1366 defaultEnabledStatus = scope.defaultEnabledStatus
1367 } else {
1368 defaultEnabledStatus = scope.legacyEnabledStatus(module)
1369 }
1370 enabled := proptools.BoolDefault(scopeProperties.Enabled, defaultEnabledStatus)
1371 if enabled {
1372 enabledScopes[scope] = struct{}{}
1373 generatedScopes = append(generatedScopes, scope)
1374 }
1375 }
1376
1377 // Now check to make sure that any scope that is extended by an enabled scope is also
1378 // enabled.
Jihoon Kang98aa8fa2024-06-07 11:06:57 +00001379 for _, scope := range AllApiScopes {
Paul Duffin3375e352020-04-28 10:44:03 +01001380 if _, ok := enabledScopes[scope]; ok {
1381 extends := scope.extends
1382 if extends != nil {
1383 if _, ok := enabledScopes[extends]; !ok {
1384 ctx.ModuleErrorf("enabled api scope %q depends on disabled scope %q", scope, extends)
1385 }
1386 }
1387 }
1388 }
1389
1390 return generatedScopes
Paul Duffind1b3a922020-01-22 11:57:20 +00001391}
1392
satayev758968a2021-12-06 11:42:40 +00001393var _ android.ModuleWithMinSdkVersionCheck = (*SdkLibrary)(nil)
1394
satayev8f088b02021-12-06 11:40:46 +00001395func (module *SdkLibrary) CheckMinSdkVersion(ctx android.ModuleContext) {
Jihoon Kanga3a05462024-04-05 00:36:44 +00001396 CheckMinSdkVersion(ctx, &module.Library)
1397}
1398
1399func CheckMinSdkVersion(ctx android.ModuleContext, module *Library) {
Spandan Das8c9ae7e2023-03-03 21:20:36 +00001400 android.CheckMinSdkVersion(ctx, module.MinSdkVersion(ctx), func(c android.ModuleContext, do android.PayloadDepsCallback) {
satayev8f088b02021-12-06 11:40:46 +00001401 ctx.WalkDeps(func(child android.Module, parent android.Module) bool {
1402 isExternal := !module.depIsInSameApex(ctx, child)
1403 if am, ok := child.(android.ApexModule); ok {
1404 if !do(ctx, parent, am, isExternal) {
1405 return false
1406 }
1407 }
1408 return !isExternal
1409 })
1410 })
1411}
1412
Paul Duffineedc5d52020-06-12 17:46:39 +01001413type sdkLibraryComponentTag struct {
1414 blueprint.BaseDependencyTag
1415 name string
1416}
1417
1418// Mark this tag so dependencies that use it are excluded from visibility enforcement.
1419func (t sdkLibraryComponentTag) ExcludeFromVisibilityEnforcement() {}
1420
1421var xmlPermissionsFileTag = sdkLibraryComponentTag{name: "xml-permissions-file"}
Paul Duffine74ac732020-02-06 13:51:46 +00001422
Jiyong Parke3833882020-02-17 17:28:10 +09001423func IsXmlPermissionsFileDepTag(depTag blueprint.DependencyTag) bool {
Paul Duffineedc5d52020-06-12 17:46:39 +01001424 if dt, ok := depTag.(sdkLibraryComponentTag); ok {
Jiyong Parke3833882020-02-17 17:28:10 +09001425 return dt == xmlPermissionsFileTag
1426 }
1427 return false
1428}
1429
Paul Duffineedc5d52020-06-12 17:46:39 +01001430var implLibraryTag = sdkLibraryComponentTag{name: "impl-library"}
Paul Duffin5df79302020-05-16 15:52:12 +01001431
Jeongik Chaaaa6dcd2024-05-22 00:41:28 +09001432var _ android.InstallNeededDependencyTag = sdkLibraryComponentTag{}
1433
Jihoon Kang46d66de2024-05-22 22:42:39 +00001434// To satisfy the CopyDirectlyInAnyApexTag interface. Implementation library of the sdk library
1435// in an apex is considered to be directly in the apex, as if it was listed in java_libs.
1436func (t sdkLibraryComponentTag) CopyDirectlyInAnyApex() {}
1437
1438var _ android.CopyDirectlyInAnyApexTag = implLibraryTag
1439
Jeongik Chaaaa6dcd2024-05-22 00:41:28 +09001440func (t sdkLibraryComponentTag) InstallDepNeeded() bool {
1441 return t.name == "xml-permissions-file" || t.name == "impl-library"
1442}
1443
Paul Duffin44f1d842020-06-26 20:17:02 +01001444// Add the dependencies on the child modules in the component deps mutator.
1445func (module *SdkLibrary) ComponentDepsMutator(ctx android.BottomUpMutatorContext) {
Paul Duffin3375e352020-04-28 10:44:03 +01001446 for _, apiScope := range module.getGeneratedApiScopes(ctx) {
Paul Duffind1b3a922020-01-22 11:57:20 +00001447 // Add dependencies to the stubs library
Spandan Das877f39d2023-03-29 16:19:51 +00001448 stubModuleName := module.stubsLibraryModuleName(apiScope)
Jihoon Kangbd093452023-12-26 19:08:01 +00001449 ctx.AddVariationDependencies(nil, apiScope.everythingStubsTag, stubModuleName)
Jihoon Kang1147b312023-06-08 23:25:57 +00001450
Jihoon Kangbd093452023-12-26 19:08:01 +00001451 exportableStubModuleName := module.exportableStubsLibraryModuleName(apiScope)
1452 ctx.AddVariationDependencies(nil, apiScope.exportableStubsTag, exportableStubModuleName)
Paul Duffind1b3a922020-01-22 11:57:20 +00001453
Paul Duffin15f34ef2020-07-20 18:04:44 +01001454 // Add a dependency on the stubs source in order to access both stubs source and api information.
1455 ctx.AddVariationDependencies(nil, apiScope.stubsSourceAndApiTag, module.stubsSourceModuleName(apiScope))
Paul Duffin958806b2022-05-16 13:10:47 +00001456
1457 if module.compareAgainstLatestApi(apiScope) {
1458 // Add dependencies on the latest finalized version of the API .txt file.
1459 latestApiModuleName := module.latestApiModuleName(apiScope)
1460 ctx.AddDependency(module, apiScope.latestApiModuleTag, latestApiModuleName)
1461
1462 // Add dependencies on the latest finalized version of the remove API .txt file.
1463 latestRemovedApiModuleName := module.latestRemovedApiModuleName(apiScope)
1464 ctx.AddDependency(module, apiScope.latestRemovedApiModuleTag, latestRemovedApiModuleName)
1465 }
Sundong Ahn054b19a2018-10-19 13:46:09 +09001466 }
1467
Paul Duffindfa131e2020-05-15 20:37:11 +01001468 if module.requiresRuntimeImplementationLibrary() {
Paul Duffin5df79302020-05-16 15:52:12 +01001469 // Add dependency to the rule for generating the implementation library.
1470 ctx.AddDependency(module, implLibraryTag, module.implLibraryModuleName())
1471
Paul Duffindfa131e2020-05-15 20:37:11 +01001472 if module.sharedLibrary() {
1473 // Add dependency to the rule for generating the xml permissions file
Paul Duffineedc5d52020-06-12 17:46:39 +01001474 ctx.AddDependency(module, xmlPermissionsFileTag, module.xmlPermissionsModuleName())
Paul Duffindfa131e2020-05-15 20:37:11 +01001475 }
Paul Duffin44f1d842020-06-26 20:17:02 +01001476 }
1477}
Paul Duffine74ac732020-02-06 13:51:46 +00001478
Paul Duffin44f1d842020-06-26 20:17:02 +01001479// Add other dependencies as normal.
1480func (module *SdkLibrary) DepsMutator(ctx android.BottomUpMutatorContext) {
Jihoon Kange4a90172024-07-18 22:49:08 +00001481 // If the module does not create an implementation library or defaults to stubs,
1482 // mark the top level sdk library as stubs module as the module will provide stubs via
1483 // "magic" when listed as a dependency in the Android.bp files.
1484 notCreateImplLib := proptools.Bool(module.sdkLibraryProperties.Api_only)
1485 preferStubs := proptools.Bool(module.sdkLibraryProperties.Default_to_stubs)
1486 module.properties.Is_stubs_module = proptools.BoolPtr(notCreateImplLib || preferStubs)
1487
Anton Hanssone77fccc2021-01-20 16:52:41 +00001488 var missingApiModules []string
1489 for _, apiScope := range module.getGeneratedApiScopes(ctx) {
1490 if apiScope.unstable {
1491 continue
1492 }
Paul Duffin958806b2022-05-16 13:10:47 +00001493 if m := module.latestApiModuleName(apiScope); !ctx.OtherModuleExists(m) {
Anton Hanssone77fccc2021-01-20 16:52:41 +00001494 missingApiModules = append(missingApiModules, m)
1495 }
Paul Duffin958806b2022-05-16 13:10:47 +00001496 if m := module.latestRemovedApiModuleName(apiScope); !ctx.OtherModuleExists(m) {
Anton Hanssone77fccc2021-01-20 16:52:41 +00001497 missingApiModules = append(missingApiModules, m)
1498 }
Paul Duffin958806b2022-05-16 13:10:47 +00001499 if m := module.latestIncompatibilitiesModuleName(apiScope); !ctx.OtherModuleExists(m) {
Jaewoong Jung1a97ee02021-03-09 13:25:02 -08001500 missingApiModules = append(missingApiModules, m)
1501 }
Anton Hanssone77fccc2021-01-20 16:52:41 +00001502 }
1503 if len(missingApiModules) != 0 && !module.sdkLibraryProperties.Unsafe_ignore_missing_latest_api {
1504 m := module.Name() + " is missing tracking files for previously released library versions.\n"
1505 m += "You need to do one of the following:\n"
1506 m += "- Add `unsafe_ignore_missing_latest_api: true` to your blueprint (to disable compat tracking)\n"
1507 m += "- Add a set of prebuilt txt files representing the last released version of this library for compat checking.\n"
1508 m += " (the current set of API files can be used as a seed for this compatibility tracking\n"
1509 m += "\n"
1510 m += "The following filegroup modules are missing:\n "
1511 m += strings.Join(missingApiModules, "\n ") + "\n"
1512 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."
1513 ctx.ModuleErrorf(m)
1514 }
Jiyong Parkc678ad32018-04-10 13:07:10 +09001515}
1516
Inseob Kimc0907f12019-02-08 21:00:45 +09001517func (module *SdkLibrary) GenerateAndroidBuildActions(ctx android.ModuleContext) {
Spandan Das5ae65ee2024-04-16 22:03:26 +00001518 if disableSourceApexVariant(ctx) {
1519 // Prebuilts are active, do not create the installation rules for the source javalib.
1520 // Even though the source javalib is not used, we need to hide it to prevent duplicate installation rules.
1521 // TODO (b/331665856): Implement a principled solution for this.
1522 module.HideFromMake()
1523 }
satayev8f088b02021-12-06 11:40:46 +00001524
Paul Duffina2ae7e02020-09-11 11:55:00 +01001525 module.generateCommonBuildActions(ctx)
1526
Jihoon Kanga3a05462024-04-05 00:36:44 +00001527 module.stem = proptools.StringDefault(module.overridableProperties.Stem, ctx.ModuleName())
1528
1529 module.provideHiddenAPIPropertyInfo(ctx)
Sundong Ahn054b19a2018-10-19 13:46:09 +09001530
Paul Duffinb97b1572021-04-29 21:50:40 +01001531 // Collate the components exported by this module. All scope specific modules are exported but
1532 // the impl and xml component modules are not.
1533 exportedComponents := map[string]struct{}{}
1534
Sundong Ahn57368eb2018-07-06 11:20:23 +09001535 // Record the paths to the header jars of the library (stubs and impl).
Paul Duffind1b3a922020-01-22 11:57:20 +00001536 // When this java_sdk_library is depended upon from others via "libs" property,
Jiyong Parkc678ad32018-04-10 13:07:10 +09001537 // the recorded paths will be returned depending on the link type of the caller.
1538 ctx.VisitDirectDeps(func(to android.Module) {
Jiyong Parkc678ad32018-04-10 13:07:10 +09001539 tag := ctx.OtherModuleDependencyTag(to)
1540
Paul Duffinc8782502020-04-29 20:45:27 +01001541 // Extract information from any of the scope specific dependencies.
1542 if scopeTag, ok := tag.(scopeDependencyTag); ok {
1543 apiScope := scopeTag.apiScope
Paul Duffin803a9562020-05-20 11:52:25 +01001544 scopePaths := module.getScopePathsCreateIfNeeded(apiScope)
Paul Duffinc8782502020-04-29 20:45:27 +01001545
1546 // Extract information from the dependency. The exact information extracted
1547 // is determined by the nature of the dependency which is determined by the tag.
1548 scopeTag.extractDepInfo(ctx, to, scopePaths)
Paul Duffinb97b1572021-04-29 21:50:40 +01001549
1550 exportedComponents[ctx.OtherModuleName(to)] = struct{}{}
Jihoon Kang4b9220a2024-08-22 22:11:04 +00001551
1552 ctx.Phony(ctx.ModuleName(), scopePaths.stubsHeaderPath...)
Sundong Ahn20e998b2018-07-24 11:19:26 +09001553 }
Jihoon Kang8479dea2024-04-04 01:19:05 +00001554
1555 if tag == implLibraryTag {
1556 if dep, ok := android.OtherModuleProvider(ctx, to, JavaInfoProvider); ok {
1557 module.implLibraryHeaderJars = append(module.implLibraryHeaderJars, dep.HeaderJars...)
Jihoon Kanga3a05462024-04-05 00:36:44 +00001558 module.implLibraryModule = to.(*Library)
1559 android.SetProvider(ctx, JavaInfoProvider, dep)
Jihoon Kang8479dea2024-04-04 01:19:05 +00001560 }
1561 }
Jiyong Parkc678ad32018-04-10 13:07:10 +09001562 })
Paul Duffinb97b1572021-04-29 21:50:40 +01001563
Jihoon Kanga3a05462024-04-05 00:36:44 +00001564 apexInfo, _ := android.ModuleProvider(ctx, android.ApexInfoProvider)
1565 if !apexInfo.IsForPlatform() {
1566 module.hideApexVariantFromMake = true
1567 }
1568
1569 if module.implLibraryModule != nil {
1570 if ctx.Device() {
1571 module.classesJarPaths = android.Paths{module.implLibraryModule.implementationJarFile}
1572 module.bootDexJarPath = module.implLibraryModule.bootDexJarPath
1573 module.uncompressDexState = module.implLibraryModule.uncompressDexState
1574 module.active = module.implLibraryModule.active
1575 }
1576
1577 module.outputFile = module.implLibraryModule.outputFile
1578 module.dexJarFile = makeDexJarPathFromPath(module.implLibraryModule.dexJarFile.Path())
1579 module.headerJarFile = module.implLibraryModule.headerJarFile
1580 module.implementationAndResourcesJar = module.implLibraryModule.implementationAndResourcesJar
1581 module.builtInstalledForApex = module.implLibraryModule.builtInstalledForApex
1582 module.dexpreopter.configPath = module.implLibraryModule.dexpreopter.configPath
1583 module.dexpreopter.outputProfilePathOnHost = module.implLibraryModule.dexpreopter.outputProfilePathOnHost
1584
Jihoon Kang34155e32024-05-20 19:08:49 +00001585 // Properties required for Library.AndroidMkEntries
1586 module.logtagsSrcs = module.implLibraryModule.logtagsSrcs
1587 module.dexpreopter.builtInstalled = module.implLibraryModule.dexpreopter.builtInstalled
1588 module.jacocoReportClassesFile = module.implLibraryModule.jacocoReportClassesFile
1589 module.dexer.proguardDictionary = module.implLibraryModule.dexer.proguardDictionary
1590 module.dexer.proguardUsageZip = module.implLibraryModule.dexer.proguardUsageZip
1591 module.linter.reports = module.implLibraryModule.linter.reports
Jihoon Kang629e2a32024-06-25 20:47:49 +00001592 module.linter.outputs.depSets = module.implLibraryModule.LintDepSets()
Jihoon Kang34155e32024-05-20 19:08:49 +00001593
Jihoon Kanga3a05462024-04-05 00:36:44 +00001594 if !module.Host() {
1595 module.hostdexInstallFile = module.implLibraryModule.hostdexInstallFile
1596 }
1597
1598 android.SetProvider(ctx, blueprint.SrcsFileProviderKey, blueprint.SrcsFileProviderData{SrcPaths: module.implLibraryModule.uniqueSrcFiles.Strings()})
1599 }
1600
Paul Duffinb97b1572021-04-29 21:50:40 +01001601 // Make the set of components exported by this module available for use elsewhere.
Cole Faust18994c72023-02-28 16:02:16 -08001602 exportedComponentInfo := android.ExportedComponentsInfo{Components: android.SortedKeys(exportedComponents)}
Colin Cross40213022023-12-13 15:19:49 -08001603 android.SetProvider(ctx, android.ExportedComponentsInfoProvider, exportedComponentInfo)
Paul Duffin958806b2022-05-16 13:10:47 +00001604
1605 // Provide additional information for inclusion in an sdk's generated .info file.
1606 additionalSdkInfo := map[string]interface{}{}
1607 additionalSdkInfo["dist_stem"] = module.distStem()
Paul Duffine8409952022-09-22 16:24:46 +01001608 baseModuleName := module.distStem()
Paul Duffin958806b2022-05-16 13:10:47 +00001609 scopes := map[string]interface{}{}
1610 additionalSdkInfo["scopes"] = scopes
1611 for scope, scopePaths := range module.scopePaths {
1612 scopeInfo := map[string]interface{}{}
1613 scopes[scope.name] = scopeInfo
1614 scopeInfo["current_api"] = scope.snapshotRelativeCurrentApiTxtPath(baseModuleName)
1615 scopeInfo["removed_api"] = scope.snapshotRelativeRemovedApiTxtPath(baseModuleName)
Jihoon Kang5623e542024-01-31 23:27:26 +00001616 if p := scopePaths.latestApiPaths; len(p) > 0 {
1617 // The last path in the list is the one that applies to this scope, the
1618 // preceding ones, if any, are for the scope(s) that it extends.
1619 scopeInfo["latest_api"] = p[len(p)-1].String()
Paul Duffin958806b2022-05-16 13:10:47 +00001620 }
Jihoon Kang5623e542024-01-31 23:27:26 +00001621 if p := scopePaths.latestRemovedApiPaths; len(p) > 0 {
1622 // The last path in the list is the one that applies to this scope, the
1623 // preceding ones, if any, are for the scope(s) that it extends.
1624 scopeInfo["latest_removed_api"] = p[len(p)-1].String()
Paul Duffin958806b2022-05-16 13:10:47 +00001625 }
1626 }
Colin Cross40213022023-12-13 15:19:49 -08001627 android.SetProvider(ctx, android.AdditionalSdkInfoProvider, android.AdditionalSdkInfo{additionalSdkInfo})
mrziwang9f7b9f42024-07-10 12:18:06 -07001628 module.setOutputFiles(ctx)
1629 if module.requiresRuntimeImplementationLibrary() && module.implLibraryModule != nil {
1630 setOutputFiles(ctx, module.implLibraryModule.Module)
1631 }
Jiyong Parkc678ad32018-04-10 13:07:10 +09001632}
1633
Jihoon Kanga3a05462024-04-05 00:36:44 +00001634func (module *SdkLibrary) BuiltInstalledForApex() []dexpreopterInstall {
1635 return module.builtInstalledForApex
1636}
1637
Jiyong Park0b0e1b92019-12-03 13:24:29 +09001638func (module *SdkLibrary) AndroidMkEntries() []android.AndroidMkEntries {
Paul Duffindfa131e2020-05-15 20:37:11 +01001639 if !module.requiresRuntimeImplementationLibrary() {
Paul Duffin43db9be2019-12-30 17:35:49 +00001640 return nil
1641 }
Jiyong Park0b0e1b92019-12-03 13:24:29 +09001642 entriesList := module.Library.AndroidMkEntries()
Jihoon Kanga3a05462024-04-05 00:36:44 +00001643 entries := &entriesList[0]
1644 entries.Required = append(entries.Required, module.implLibraryModuleName())
Yo Chiang07d75072020-06-05 17:43:19 +08001645 if module.sharedLibrary() {
Yo Chiang07d75072020-06-05 17:43:19 +08001646 entries.Required = append(entries.Required, module.xmlPermissionsModuleName())
1647 }
Jiyong Park0b0e1b92019-12-03 13:24:29 +09001648 return entriesList
Jiyong Park82484c02018-04-23 21:41:26 +09001649}
1650
Anton Hansson5fd5d242020-03-27 19:43:19 +00001651// The dist path of the stub artifacts
1652func (module *SdkLibrary) apiDistPath(apiScope *apiScope) string {
Colin Crossf0eace92021-06-02 13:02:23 -07001653 return path.Join("apistubs", module.distGroup(), apiScope.name)
Anton Hansson5fd5d242020-03-27 19:43:19 +00001654}
1655
Paul Duffin12ceb462019-12-24 20:31:31 +00001656// Get the sdk version for use when compiling the stubs library.
Paul Duffin780c5f42020-05-12 15:52:55 +01001657func (module *SdkLibrary) sdkVersionForStubsLibrary(mctx android.EarlyModuleContext, apiScope *apiScope) string {
Paul Duffin87a05a32020-05-12 11:50:28 +01001658 scopeProperties := module.scopeToProperties[apiScope]
1659 if scopeProperties.Sdk_version != nil {
1660 return proptools.String(scopeProperties.Sdk_version)
1661 }
1662
Jiyong Parkf1691d22021-03-29 20:11:58 +09001663 sdkDep := decodeSdkDep(mctx, android.SdkContext(&module.Library))
Paul Duffin12ceb462019-12-24 20:31:31 +00001664 if sdkDep.hasStandardLibs() {
1665 // If building against a standard sdk then use the sdk version appropriate for the scope.
Paul Duffind1b3a922020-01-22 11:57:20 +00001666 return apiScope.sdkVersion
Paul Duffin12ceb462019-12-24 20:31:31 +00001667 } else {
1668 // Otherwise, use no system module.
1669 return "none"
1670 }
1671}
1672
Paul Duffin31310252020-11-20 21:26:20 +00001673func (module *SdkLibrary) distStem() string {
1674 return proptools.StringDefault(module.sdkLibraryProperties.Dist_stem, module.BaseModuleName())
1675}
1676
Colin Cross986b69a2021-06-01 13:13:40 -07001677// distGroup returns the subdirectory of the dist path of the stub artifacts.
1678func (module *SdkLibrary) distGroup() string {
Colin Cross59b92bf2021-06-01 14:07:56 -07001679 return proptools.StringDefault(module.sdkLibraryProperties.Dist_group, "unknown")
Colin Cross986b69a2021-06-01 13:13:40 -07001680}
1681
Paul Duffin958806b2022-05-16 13:10:47 +00001682func latestPrebuiltApiModuleName(name string, apiScope *apiScope) string {
1683 return PrebuiltApiModuleName(name, apiScope.name, "latest")
1684}
1685
Jihoon Kang748a24d2024-03-20 21:29:39 +00001686func latestPrebuiltApiCombinedModuleName(name string, apiScope *apiScope) string {
1687 return PrebuiltApiCombinedModuleName(name, apiScope.name, "latest")
1688}
1689
Paul Duffind1b3a922020-01-22 11:57:20 +00001690func (module *SdkLibrary) latestApiFilegroupName(apiScope *apiScope) string {
Paul Duffin958806b2022-05-16 13:10:47 +00001691 return ":" + module.latestApiModuleName(apiScope)
1692}
1693
1694func (module *SdkLibrary) latestApiModuleName(apiScope *apiScope) string {
Jihoon Kang748a24d2024-03-20 21:29:39 +00001695 return latestPrebuiltApiCombinedModuleName(module.distStem(), apiScope)
Jiyong Park58c518b2018-05-12 22:29:12 +09001696}
Jiyong Parkc678ad32018-04-10 13:07:10 +09001697
Paul Duffind1b3a922020-01-22 11:57:20 +00001698func (module *SdkLibrary) latestRemovedApiFilegroupName(apiScope *apiScope) string {
Paul Duffin958806b2022-05-16 13:10:47 +00001699 return ":" + module.latestRemovedApiModuleName(apiScope)
1700}
1701
1702func (module *SdkLibrary) latestRemovedApiModuleName(apiScope *apiScope) string {
Jihoon Kang748a24d2024-03-20 21:29:39 +00001703 return latestPrebuiltApiCombinedModuleName(module.distStem()+"-removed", apiScope)
Jiyong Parkc678ad32018-04-10 13:07:10 +09001704}
1705
Jaewoong Jung1a97ee02021-03-09 13:25:02 -08001706func (module *SdkLibrary) latestIncompatibilitiesFilegroupName(apiScope *apiScope) string {
Paul Duffin958806b2022-05-16 13:10:47 +00001707 return ":" + module.latestIncompatibilitiesModuleName(apiScope)
1708}
1709
1710func (module *SdkLibrary) latestIncompatibilitiesModuleName(apiScope *apiScope) string {
1711 return latestPrebuiltApiModuleName(module.distStem()+"-incompatibilities", apiScope)
Jaewoong Jung1a97ee02021-03-09 13:25:02 -08001712}
1713
Jihoon Kang0c705a42023-08-02 06:44:57 +00001714// The listed modules' stubs contents do not match the corresponding txt files,
1715// but require additional api contributions to generate the full stubs.
1716// This method returns the name of the additional api contribution module
1717// for corresponding sdk_library modules.
1718func (module *SdkLibrary) apiLibraryAdditionalApiContribution() string {
1719 if val, ok := apiLibraryAdditionalProperties[module.Name()]; ok {
Jihoon Kangb0f4c022024-08-06 00:15:25 +00001720 return val
Jihoon Kang0c705a42023-08-02 06:44:57 +00001721 }
1722 return ""
1723}
1724
Anton Hansson944e77d2020-08-19 11:40:22 +01001725func childModuleVisibility(childVisibility []string) []string {
1726 if childVisibility == nil {
1727 // No child visibility set. The child will use the visibility of the sdk_library.
1728 return nil
1729 }
1730
1731 // Prepend an override to ignore the sdk_library's visibility, and rely on the child visibility.
1732 var visibility []string
1733 visibility = append(visibility, "//visibility:override")
1734 visibility = append(visibility, childVisibility...)
1735 return visibility
1736}
1737
Paul Duffin5df79302020-05-16 15:52:12 +01001738// Creates the implementation java library
1739func (module *SdkLibrary) createImplLibrary(mctx android.DefaultableHookContext) {
Anton Hansson944e77d2020-08-19 11:40:22 +01001740 visibility := childModuleVisibility(module.sdkLibraryProperties.Impl_library_visibility)
1741
Paul Duffin5df79302020-05-16 15:52:12 +01001742 props := struct {
Paul Duffin77590a82022-04-28 14:13:30 +00001743 Name *string
1744 Visibility []string
Paul Duffin77590a82022-04-28 14:13:30 +00001745 Libs []string
1746 Static_libs []string
1747 Apex_available []string
Jihoon Kanga3a05462024-04-05 00:36:44 +00001748 Stem *string
Paul Duffin5df79302020-05-16 15:52:12 +01001749 }{
1750 Name: proptools.StringPtr(module.implLibraryModuleName()),
Anton Hansson944e77d2020-08-19 11:40:22 +01001751 Visibility: visibility,
Jihoon Kanga3a05462024-04-05 00:36:44 +00001752
1753 Libs: append(module.properties.Libs, module.sdkLibraryProperties.Impl_only_libs...),
1754
1755 Static_libs: append(module.properties.Static_libs, module.sdkLibraryProperties.Impl_only_static_libs...),
Paul Duffin77590a82022-04-28 14:13:30 +00001756 // Pass the apex_available settings down so that the impl library can be statically
1757 // embedded within a library that is added to an APEX. Needed for updatable-media.
1758 Apex_available: module.ApexAvailable(),
Jihoon Kanga3a05462024-04-05 00:36:44 +00001759
1760 Stem: proptools.StringPtr(module.Name()),
Paul Duffin5df79302020-05-16 15:52:12 +01001761 }
1762
1763 properties := []interface{}{
1764 &module.properties,
1765 &module.protoProperties,
1766 &module.deviceProperties,
Liz Kammera7a64f32020-07-09 15:16:41 -07001767 &module.dexProperties,
Paul Duffin5df79302020-05-16 15:52:12 +01001768 &module.dexpreoptProperties,
Colin Cross014489c2020-06-02 20:09:13 -07001769 &module.linter.properties,
Spandan Dasb9c58352024-05-13 18:29:45 +00001770 &module.overridableProperties,
Paul Duffin5df79302020-05-16 15:52:12 +01001771 &props,
1772 module.sdkComponentPropertiesForChildLibrary(),
1773 }
1774 mctx.CreateModule(LibraryFactory, properties...)
1775}
1776
Jihoon Kangfa4a90d2023-12-20 02:53:38 +00001777type libraryProperties struct {
1778 Name *string
1779 Visibility []string
1780 Srcs []string
1781 Installable *bool
1782 Sdk_version *string
1783 System_modules *string
1784 Patch_module *string
1785 Libs []string
1786 Static_libs []string
1787 Compile_dex *bool
1788 Java_version *string
1789 Openjdk9 struct {
1790 Srcs []string
1791 Javacflags []string
1792 }
1793 Dist struct {
1794 Targets []string
1795 Dest *string
1796 Dir *string
1797 Tag *string
1798 }
Jihoon Kangfa3f0782024-08-21 20:42:18 +00001799 Is_stubs_module *bool
1800 Stub_contributing_api *string
Jihoon Kangfa4a90d2023-12-20 02:53:38 +00001801}
Jiyong Parkc678ad32018-04-10 13:07:10 +09001802
Jihoon Kangfa4a90d2023-12-20 02:53:38 +00001803func (module *SdkLibrary) stubsLibraryProps(mctx android.DefaultableHookContext, apiScope *apiScope) libraryProperties {
1804 props := libraryProperties{}
Jihoon Kang786df932023-09-07 01:18:31 +00001805 props.Visibility = []string{"//visibility:override", "//visibility:private"}
Jiyong Parkc678ad32018-04-10 13:07:10 +09001806 // sources are generated from the droiddoc
Paul Duffin12ceb462019-12-24 20:31:31 +00001807 sdkVersion := module.sdkVersionForStubsLibrary(mctx, apiScope)
Paul Duffin52d398a2019-06-11 12:31:14 +01001808 props.Sdk_version = proptools.StringPtr(sdkVersion)
Paul Duffina18abc22020-05-16 18:54:24 +01001809 props.System_modules = module.deviceProperties.System_modules
1810 props.Patch_module = module.properties.Patch_module
Paul Duffin367ab912019-12-23 19:40:36 +00001811 props.Installable = proptools.BoolPtr(false)
Sundong Ahn054b19a2018-10-19 13:46:09 +09001812 props.Libs = module.sdkLibraryProperties.Stub_only_libs
Mark White9421c4c2023-08-10 00:07:03 +00001813 props.Libs = append(props.Libs, module.scopeToProperties[apiScope].Libs...)
Anton Hanssondae54cd2021-04-21 16:30:10 +01001814 props.Static_libs = module.sdkLibraryProperties.Stub_only_static_libs
Paul Duffine22c2ab2020-05-20 19:35:27 +01001815 // The stub-annotations library contains special versions of the annotations
1816 // with CLASS retention policy, so that they're kept.
1817 if proptools.Bool(module.sdkLibraryProperties.Annotations_enabled) {
1818 props.Libs = append(props.Libs, "stub-annotations")
1819 }
Paul Duffina18abc22020-05-16 18:54:24 +01001820 props.Openjdk9.Srcs = module.properties.Openjdk9.Srcs
1821 props.Openjdk9.Javacflags = module.properties.Openjdk9.Javacflags
Anton Hansson83509b52020-05-21 09:21:57 +01001822 // We compile the stubs for 1.8 in line with the main android.jar stubs, and potential
1823 // interop with older developer tools that don't support 1.9.
1824 props.Java_version = proptools.StringPtr("1.8")
Jihoon Kangfe914ed2024-02-12 22:49:21 +00001825 props.Is_stubs_module = proptools.BoolPtr(true)
Jihoon Kangfa3f0782024-08-21 20:42:18 +00001826 props.Stub_contributing_api = proptools.StringPtr(apiScope.kind.String())
Paul Duffinf4600f62021-05-13 22:34:45 +01001827
Jihoon Kangfa4a90d2023-12-20 02:53:38 +00001828 return props
1829}
1830
1831// Creates a static java library that has API stubs
1832func (module *SdkLibrary) createStubsLibrary(mctx android.DefaultableHookContext, apiScope *apiScope) {
1833
1834 props := module.stubsLibraryProps(mctx, apiScope)
1835 props.Name = proptools.StringPtr(module.sourceStubsLibraryModuleName(apiScope))
1836 props.Srcs = []string{":" + module.stubsSourceModuleName(apiScope)}
1837
1838 mctx.CreateModule(LibraryFactory, &props, module.sdkComponentPropertiesForChildLibrary())
1839}
1840
1841// Create a static java library that compiles the "exportable" stubs
1842func (module *SdkLibrary) createExportableStubsLibrary(mctx android.DefaultableHookContext, apiScope *apiScope) {
1843 props := module.stubsLibraryProps(mctx, apiScope)
1844 props.Name = proptools.StringPtr(module.exportableSourceStubsLibraryModuleName(apiScope))
1845 props.Srcs = []string{":" + module.stubsSourceModuleName(apiScope) + "{.exportable}"}
1846
Paul Duffin859fe962020-05-15 10:20:31 +01001847 mctx.CreateModule(LibraryFactory, &props, module.sdkComponentPropertiesForChildLibrary())
Jiyong Parkc678ad32018-04-10 13:07:10 +09001848}
1849
Paul Duffin6d0886e2020-04-07 18:49:53 +01001850// Creates a droidstubs module that creates stubs source files from the given full source
Paul Duffinc8782502020-04-29 20:45:27 +01001851// files and also updates and checks the API specification files.
Paul Duffin15f34ef2020-07-20 18:04:44 +01001852func (module *SdkLibrary) createStubsSourcesAndApi(mctx android.DefaultableHookContext, apiScope *apiScope, name string, scopeSpecificDroidstubsArgs []string) {
Jiyong Parkc678ad32018-04-10 13:07:10 +09001853 props := struct {
Sundong Ahn054b19a2018-10-19 13:46:09 +09001854 Name *string
Paul Duffin4911a892020-04-29 23:35:13 +01001855 Visibility []string
Sundong Ahn054b19a2018-10-19 13:46:09 +09001856 Srcs []string
1857 Installable *bool
Paul Duffin52d398a2019-06-11 12:31:14 +01001858 Sdk_version *string
Jihoon Kang3198f3c2023-01-26 08:08:52 +00001859 Api_surface *string
Paul Duffin12ceb462019-12-24 20:31:31 +00001860 System_modules *string
Sundong Ahn054b19a2018-10-19 13:46:09 +09001861 Libs []string
Paul Duffin6877e6d2020-09-25 19:59:14 +01001862 Output_javadoc_comments *bool
Paul Duffin11512472019-02-11 15:55:17 +00001863 Arg_files []string
Sundong Ahn054b19a2018-10-19 13:46:09 +09001864 Args *string
Sundong Ahn054b19a2018-10-19 13:46:09 +09001865 Java_version *string
Paul Duffine22c2ab2020-05-20 19:35:27 +01001866 Annotations_enabled *bool
Sundong Ahn054b19a2018-10-19 13:46:09 +09001867 Merge_annotations_dirs []string
1868 Merge_inclusion_annotations_dirs []string
Paul Duffin0ff08bd2020-04-29 13:30:54 +01001869 Generate_stubs *bool
Anton Hanssone87b03d2020-12-21 15:29:34 +00001870 Previous_api *string
Jihoon Kang6592e872023-12-19 01:13:16 +00001871 Aconfig_declarations []string
Sundong Ahn054b19a2018-10-19 13:46:09 +09001872 Check_api struct {
Anton Hanssone6056152020-12-31 10:37:27 +00001873 Current ApiToCheck
1874 Last_released ApiToCheck
Paul Duffin160fe412020-05-10 19:32:20 +01001875
1876 Api_lint struct {
1877 Enabled *bool
1878 New_since *string
1879 Baseline_file *string
1880 }
Jiyong Park58c518b2018-05-12 22:29:12 +09001881 }
Sundong Ahn1b92c822018-05-29 11:35:17 +09001882 Aidl struct {
1883 Include_dirs []string
1884 Local_include_dirs []string
1885 }
Paul Duffin040e9062020-11-23 17:41:36 +00001886 Dists []android.Dist
Jiyong Parkc678ad32018-04-10 13:07:10 +09001887 }{}
1888
Paul Duffin7b78b4d2020-04-28 14:08:32 +01001889 // The stubs source processing uses the same compile time classpath when extracting the
1890 // API from the implementation library as it does when compiling it. i.e. the same
1891 // * sdk version
1892 // * system_modules
1893 // * libs (static_libs/libs)
Paul Duffin250e6192019-06-07 10:44:37 +01001894
Paul Duffin0ff08bd2020-04-29 13:30:54 +01001895 props.Name = proptools.StringPtr(name)
Anton Hansson944e77d2020-08-19 11:40:22 +01001896 props.Visibility = childModuleVisibility(module.sdkLibraryProperties.Stubs_source_visibility)
Paul Duffina18abc22020-05-16 18:54:24 +01001897 props.Srcs = append(props.Srcs, module.properties.Srcs...)
Anton Hanssonf8ea3722021-09-16 14:24:13 +01001898 props.Srcs = append(props.Srcs, module.sdkLibraryProperties.Api_srcs...)
Paul Duffina18abc22020-05-16 18:54:24 +01001899 props.Sdk_version = module.deviceProperties.Sdk_version
Jihoon Kang3198f3c2023-01-26 08:08:52 +00001900 props.Api_surface = &apiScope.name
Paul Duffina18abc22020-05-16 18:54:24 +01001901 props.System_modules = module.deviceProperties.System_modules
Jiyong Parkc678ad32018-04-10 13:07:10 +09001902 props.Installable = proptools.BoolPtr(false)
Sundong Ahne6f0b052018-06-05 16:46:14 +09001903 // A droiddoc module has only one Libs property and doesn't distinguish between
1904 // shared libs and static libs. So we need to add both of these libs to Libs property.
Paul Duffina18abc22020-05-16 18:54:24 +01001905 props.Libs = module.properties.Libs
1906 props.Libs = append(props.Libs, module.properties.Static_libs...)
Nikita Ioffed732da72022-11-21 12:38:25 +00001907 props.Libs = append(props.Libs, module.sdkLibraryProperties.Stub_only_libs...)
Mark White9421c4c2023-08-10 00:07:03 +00001908 props.Libs = append(props.Libs, module.scopeToProperties[apiScope].Libs...)
Paul Duffina18abc22020-05-16 18:54:24 +01001909 props.Aidl.Include_dirs = module.deviceProperties.Aidl.Include_dirs
1910 props.Aidl.Local_include_dirs = module.deviceProperties.Aidl.Local_include_dirs
1911 props.Java_version = module.properties.Java_version
Jiyong Parkc678ad32018-04-10 13:07:10 +09001912
Paul Duffine22c2ab2020-05-20 19:35:27 +01001913 props.Annotations_enabled = module.sdkLibraryProperties.Annotations_enabled
Sundong Ahn054b19a2018-10-19 13:46:09 +09001914 props.Merge_annotations_dirs = module.sdkLibraryProperties.Merge_annotations_dirs
1915 props.Merge_inclusion_annotations_dirs = module.sdkLibraryProperties.Merge_inclusion_annotations_dirs
Jihoon Kang6592e872023-12-19 01:13:16 +00001916 props.Aconfig_declarations = module.sdkLibraryProperties.Aconfig_declarations
Sundong Ahn054b19a2018-10-19 13:46:09 +09001917
Paul Duffin6d0886e2020-04-07 18:49:53 +01001918 droidstubsArgs := []string{}
Paul Duffin235ffff2019-12-24 10:41:30 +00001919 if len(module.sdkLibraryProperties.Api_packages) != 0 {
Paul Duffin6d0886e2020-04-07 18:49:53 +01001920 droidstubsArgs = append(droidstubsArgs, "--stub-packages "+strings.Join(module.sdkLibraryProperties.Api_packages, ":"))
Paul Duffin235ffff2019-12-24 10:41:30 +00001921 }
Paul Duffin6d0886e2020-04-07 18:49:53 +01001922 droidstubsArgs = append(droidstubsArgs, module.sdkLibraryProperties.Droiddoc_options...)
Anton Hanssonfd1c0d22023-11-02 15:18:09 +00001923 disabledWarnings := []string{"HiddenSuperclass"}
1924 if proptools.BoolDefault(module.sdkLibraryProperties.Api_lint.Legacy_errors_allowed, true) {
1925 disabledWarnings = append(disabledWarnings,
1926 "BroadcastBehavior",
1927 "DeprecationMismatch",
1928 "MissingPermission",
1929 "SdkConstant",
1930 "Todo",
1931 )
Paul Duffin235ffff2019-12-24 10:41:30 +00001932 }
Paul Duffin6d0886e2020-04-07 18:49:53 +01001933 droidstubsArgs = append(droidstubsArgs, android.JoinWithPrefix(disabledWarnings, "--hide "))
Sundong Ahnfb2721f2018-09-17 13:23:09 +09001934
Paul Duffin6877e6d2020-09-25 19:59:14 +01001935 // Output Javadoc comments for public scope.
1936 if apiScope == apiScopePublic {
1937 props.Output_javadoc_comments = proptools.BoolPtr(true)
1938 }
1939
Paul Duffin1fb487d2020-04-07 18:50:10 +01001940 // Add in scope specific arguments.
Paul Duffin0ff08bd2020-04-29 13:30:54 +01001941 droidstubsArgs = append(droidstubsArgs, scopeSpecificDroidstubsArgs...)
Paul Duffin11512472019-02-11 15:55:17 +00001942 props.Arg_files = module.sdkLibraryProperties.Droiddoc_option_files
Paul Duffin6d0886e2020-04-07 18:49:53 +01001943 props.Args = proptools.StringPtr(strings.Join(droidstubsArgs, " "))
Jiyong Parkc678ad32018-04-10 13:07:10 +09001944
Paul Duffin15f34ef2020-07-20 18:04:44 +01001945 // List of APIs identified from the provided source files are created. They are later
1946 // compared against to the not-yet-released (a.k.a current) list of APIs and to the
1947 // last-released (a.k.a numbered) list of API.
1948 currentApiFileName := apiScope.apiFilePrefix + "current.txt"
1949 removedApiFileName := apiScope.apiFilePrefix + "removed.txt"
1950 apiDir := module.getApiDir()
1951 currentApiFileName = path.Join(apiDir, currentApiFileName)
1952 removedApiFileName = path.Join(apiDir, removedApiFileName)
Jiyong Parkc678ad32018-04-10 13:07:10 +09001953
Paul Duffin15f34ef2020-07-20 18:04:44 +01001954 // check against the not-yet-release API
1955 props.Check_api.Current.Api_file = proptools.StringPtr(currentApiFileName)
1956 props.Check_api.Current.Removed_api_file = proptools.StringPtr(removedApiFileName)
Jiyong Park58c518b2018-05-12 22:29:12 +09001957
Paul Duffin958806b2022-05-16 13:10:47 +00001958 if module.compareAgainstLatestApi(apiScope) {
Paul Duffin15f34ef2020-07-20 18:04:44 +01001959 // check against the latest released API
1960 latestApiFilegroupName := proptools.StringPtr(module.latestApiFilegroupName(apiScope))
Anton Hanssone87b03d2020-12-21 15:29:34 +00001961 props.Previous_api = latestApiFilegroupName
Paul Duffin15f34ef2020-07-20 18:04:44 +01001962 props.Check_api.Last_released.Api_file = latestApiFilegroupName
1963 props.Check_api.Last_released.Removed_api_file = proptools.StringPtr(
1964 module.latestRemovedApiFilegroupName(apiScope))
Jaewoong Jung1a97ee02021-03-09 13:25:02 -08001965 props.Check_api.Last_released.Baseline_file = proptools.StringPtr(
1966 module.latestIncompatibilitiesFilegroupName(apiScope))
Paul Duffin160fe412020-05-10 19:32:20 +01001967
Paul Duffin15f34ef2020-07-20 18:04:44 +01001968 if proptools.Bool(module.sdkLibraryProperties.Api_lint.Enabled) {
1969 // Enable api lint.
1970 props.Check_api.Api_lint.Enabled = proptools.BoolPtr(true)
1971 props.Check_api.Api_lint.New_since = latestApiFilegroupName
Paul Duffin160fe412020-05-10 19:32:20 +01001972
Paul Duffin15f34ef2020-07-20 18:04:44 +01001973 // If it exists then pass a lint-baseline.txt through to droidstubs.
1974 baselinePath := path.Join(apiDir, apiScope.apiFilePrefix+"lint-baseline.txt")
1975 baselinePathRelativeToRoot := path.Join(mctx.ModuleDir(), baselinePath)
1976 paths, err := mctx.GlobWithDeps(baselinePathRelativeToRoot, nil)
1977 if err != nil {
1978 mctx.ModuleErrorf("error checking for presence of %s: %s", baselinePathRelativeToRoot, err)
1979 }
1980 if len(paths) == 1 {
1981 props.Check_api.Api_lint.Baseline_file = proptools.StringPtr(baselinePath)
1982 } else if len(paths) != 0 {
1983 mctx.ModuleErrorf("error checking for presence of %s: expected one path, found: %v", baselinePathRelativeToRoot, paths)
Paul Duffin160fe412020-05-10 19:32:20 +01001984 }
1985 }
Paul Duffin15f34ef2020-07-20 18:04:44 +01001986 }
Jiyong Park58c518b2018-05-12 22:29:12 +09001987
Paul Duffin15f34ef2020-07-20 18:04:44 +01001988 if !Bool(module.sdkLibraryProperties.No_dist) {
Paul Duffin040e9062020-11-23 17:41:36 +00001989 // Dist the api txt and removed api txt artifacts for sdk builds.
1990 distDir := proptools.StringPtr(path.Join(module.apiDistPath(apiScope), "api"))
Jihoon Kang02168052024-03-20 00:44:54 +00001991 stubsTypeTagPrefix := ""
1992 if mctx.Config().ReleaseHiddenApiExportableStubs() {
1993 stubsTypeTagPrefix = ".exportable"
1994 }
Paul Duffin040e9062020-11-23 17:41:36 +00001995 for _, p := range []struct {
1996 tag string
1997 pattern string
1998 }{
Jihoon Kangd1799f62024-02-20 23:01:38 +00001999 // "exportable" api files are copied to the dist directory instead of the
Jihoon Kang02168052024-03-20 00:44:54 +00002000 // "everything" api files when "RELEASE_HIDDEN_API_EXPORTABLE_STUBS" build flag
2001 // is set. Otherwise, the "everything" api files are copied to the dist directory.
2002 {tag: "%s.api.txt", pattern: "%s.txt"},
2003 {tag: "%s.removed-api.txt", pattern: "%s-removed.txt"},
Paul Duffin040e9062020-11-23 17:41:36 +00002004 } {
2005 props.Dists = append(props.Dists, android.Dist{
2006 Targets: []string{"sdk", "win_sdk"},
2007 Dir: distDir,
2008 Dest: proptools.StringPtr(fmt.Sprintf(p.pattern, module.distStem())),
Jihoon Kang02168052024-03-20 00:44:54 +00002009 Tag: proptools.StringPtr(fmt.Sprintf(p.tag, stubsTypeTagPrefix)),
Paul Duffin040e9062020-11-23 17:41:36 +00002010 })
2011 }
Anton Hansson5fd5d242020-03-27 19:43:19 +00002012 }
2013
Spandan Das2cc80ba2023-10-27 17:21:52 +00002014 mctx.CreateModule(DroidstubsFactory, &props, module.sdkComponentPropertiesForChildLibrary()).(*Droidstubs).CallHookIfAvailable(mctx)
Jiyong Parkc678ad32018-04-10 13:07:10 +09002015}
2016
Jihoon Kangb0f4c022024-08-06 00:15:25 +00002017func (module *SdkLibrary) createApiLibrary(mctx android.DefaultableHookContext, apiScope *apiScope) {
Jihoon Kang1c92c3e2023-03-23 17:44:51 +00002018 props := struct {
Jihoon Kangb0f4c022024-08-06 00:15:25 +00002019 Name *string
2020 Visibility []string
2021 Api_contributions []string
2022 Libs []string
2023 Static_libs []string
2024 System_modules *string
2025 Enable_validation *bool
2026 Stubs_type *string
2027 Sdk_version *string
2028 Previous_api *string
Jihoon Kang1c92c3e2023-03-23 17:44:51 +00002029 }{}
2030
2031 props.Name = proptools.StringPtr(module.apiLibraryModuleName(apiScope))
Jihoon Kang786df932023-09-07 01:18:31 +00002032 props.Visibility = []string{"//visibility:override", "//visibility:private"}
Jihoon Kang1c92c3e2023-03-23 17:44:51 +00002033
2034 apiContributions := []string{}
2035
2036 // Api surfaces are not independent of each other, but have subset relationships,
2037 // and so does the api files. To generate from-text stubs for api surfaces other than public,
2038 // all subset api domains' api_contriubtions must be added as well.
2039 scope := apiScope
2040 for scope != nil {
2041 apiContributions = append(apiContributions, module.stubsSourceModuleName(scope)+".api.contribution")
2042 scope = scope.extends
2043 }
Jihoon Kang0c705a42023-08-02 06:44:57 +00002044 if apiScope == apiScopePublic {
2045 additionalApiContribution := module.apiLibraryAdditionalApiContribution()
2046 if additionalApiContribution != "" {
2047 apiContributions = append(apiContributions, additionalApiContribution)
2048 }
2049 }
Jihoon Kang1c92c3e2023-03-23 17:44:51 +00002050
2051 props.Api_contributions = apiContributions
Jihoon Kangb0f4c022024-08-06 00:15:25 +00002052
2053 // Ensure that stub-annotations is added to the classpath before any other libs
2054 props.Libs = []string{"stub-annotations"}
2055 props.Libs = append(props.Libs, module.properties.Libs...)
2056 props.Libs = append(props.Libs, module.properties.Static_libs...)
Jihoon Kang1c92c3e2023-03-23 17:44:51 +00002057 props.Libs = append(props.Libs, module.sdkLibraryProperties.Stub_only_libs...)
Mark White9421c4c2023-08-10 00:07:03 +00002058 props.Libs = append(props.Libs, module.scopeToProperties[apiScope].Libs...)
Jihoon Kang1c92c3e2023-03-23 17:44:51 +00002059 props.Static_libs = module.sdkLibraryProperties.Stub_only_static_libs
Jihoon Kangd30ac8a2023-10-09 18:00:17 +00002060
Jihoon Kang4ec24872023-10-05 17:26:09 +00002061 props.System_modules = module.deviceProperties.System_modules
Jihoon Kang063ec002023-06-28 01:16:23 +00002062 props.Enable_validation = proptools.BoolPtr(true)
Jihoon Kang5d701272024-02-15 21:53:49 +00002063 props.Stubs_type = proptools.StringPtr("everything")
Jihoon Kang4ec24872023-10-05 17:26:09 +00002064
Jihoon Kangb0f4c022024-08-06 00:15:25 +00002065 if module.deviceProperties.Sdk_version != nil {
2066 props.Sdk_version = module.deviceProperties.Sdk_version
2067 }
2068
2069 if module.compareAgainstLatestApi(apiScope) {
2070 // check against the latest released API
2071 latestApiFilegroupName := proptools.StringPtr(module.latestApiFilegroupName(apiScope))
2072 props.Previous_api = latestApiFilegroupName
2073 }
2074
Spandan Das2cc80ba2023-10-27 17:21:52 +00002075 mctx.CreateModule(ApiLibraryFactory, &props, module.sdkComponentPropertiesForChildLibrary())
Jihoon Kang1c92c3e2023-03-23 17:44:51 +00002076}
2077
Jihoon Kang02168052024-03-20 00:44:54 +00002078func (module *SdkLibrary) topLevelStubsLibraryProps(mctx android.DefaultableHookContext, apiScope *apiScope, doDist bool) libraryProperties {
Jihoon Kangfa4a90d2023-12-20 02:53:38 +00002079 props := libraryProperties{}
2080
Jihoon Kang1147b312023-06-08 23:25:57 +00002081 props.Visibility = childModuleVisibility(module.sdkLibraryProperties.Stubs_library_visibility)
2082 sdkVersion := module.sdkVersionForStubsLibrary(mctx, apiScope)
2083 props.Sdk_version = proptools.StringPtr(sdkVersion)
2084
Jihoon Kang1147b312023-06-08 23:25:57 +00002085 props.System_modules = module.deviceProperties.System_modules
2086
Jihoon Kang1147b312023-06-08 23:25:57 +00002087 // The imports need to be compiled to dex if the java_sdk_library requests it.
2088 compileDex := module.dexProperties.Compile_dex
2089 if module.stubLibrariesCompiledForDex() {
2090 compileDex = proptools.BoolPtr(true)
2091 }
2092 props.Compile_dex = compileDex
2093
Jihoon Kangfa3f0782024-08-21 20:42:18 +00002094 props.Stub_contributing_api = proptools.StringPtr(apiScope.kind.String())
2095
Jihoon Kang02168052024-03-20 00:44:54 +00002096 if !Bool(module.sdkLibraryProperties.No_dist) && doDist {
2097 props.Dist.Targets = []string{"sdk", "win_sdk"}
2098 props.Dist.Dest = proptools.StringPtr(fmt.Sprintf("%v.jar", module.distStem()))
2099 props.Dist.Dir = proptools.StringPtr(module.apiDistPath(apiScope))
2100 props.Dist.Tag = proptools.StringPtr(".jar")
2101 }
Jihoon Kang85bc1932024-07-01 17:04:46 +00002102 props.Is_stubs_module = proptools.BoolPtr(true)
Jihoon Kang02168052024-03-20 00:44:54 +00002103
Jihoon Kangfa4a90d2023-12-20 02:53:38 +00002104 return props
2105}
2106
2107func (module *SdkLibrary) createTopLevelStubsLibrary(
Jihoon Kangb0f4c022024-08-06 00:15:25 +00002108 mctx android.DefaultableHookContext, apiScope *apiScope) {
Jihoon Kangfa4a90d2023-12-20 02:53:38 +00002109
Jihoon Kang02168052024-03-20 00:44:54 +00002110 // Dist the "everything" stubs when the RELEASE_HIDDEN_API_EXPORTABLE_STUBS build flag is false
2111 doDist := !mctx.Config().ReleaseHiddenApiExportableStubs()
2112 props := module.topLevelStubsLibraryProps(mctx, apiScope, doDist)
Jihoon Kangfa4a90d2023-12-20 02:53:38 +00002113 props.Name = proptools.StringPtr(module.stubsLibraryModuleName(apiScope))
2114
2115 // Add the stub compiling java_library/java_api_library as static lib based on build config
2116 staticLib := module.sourceStubsLibraryModuleName(apiScope)
Jihoon Kangb0f4c022024-08-06 00:15:25 +00002117 if mctx.Config().BuildFromTextStub() && module.ModuleBuildFromTextStubs() {
Jihoon Kangfa4a90d2023-12-20 02:53:38 +00002118 staticLib = module.apiLibraryModuleName(apiScope)
2119 }
2120 props.Static_libs = append(props.Static_libs, staticLib)
2121
2122 mctx.CreateModule(LibraryFactory, &props, module.sdkComponentPropertiesForChildLibrary())
2123}
2124
2125func (module *SdkLibrary) createTopLevelExportableStubsLibrary(
2126 mctx android.DefaultableHookContext, apiScope *apiScope) {
2127
Jihoon Kang02168052024-03-20 00:44:54 +00002128 // Dist the "exportable" stubs when the RELEASE_HIDDEN_API_EXPORTABLE_STUBS build flag is true
2129 doDist := mctx.Config().ReleaseHiddenApiExportableStubs()
2130 props := module.topLevelStubsLibraryProps(mctx, apiScope, doDist)
Jihoon Kangfa4a90d2023-12-20 02:53:38 +00002131 props.Name = proptools.StringPtr(module.exportableStubsLibraryModuleName(apiScope))
2132
Jihoon Kangfa4a90d2023-12-20 02:53:38 +00002133 staticLib := module.exportableSourceStubsLibraryModuleName(apiScope)
2134 props.Static_libs = append(props.Static_libs, staticLib)
2135
Jihoon Kang1147b312023-06-08 23:25:57 +00002136 mctx.CreateModule(LibraryFactory, &props, module.sdkComponentPropertiesForChildLibrary())
2137}
2138
Paul Duffin958806b2022-05-16 13:10:47 +00002139func (module *SdkLibrary) compareAgainstLatestApi(apiScope *apiScope) bool {
2140 return !(apiScope.unstable || module.sdkLibraryProperties.Unsafe_ignore_missing_latest_api)
2141}
2142
Paul Duffinea8f8082021-06-24 13:25:57 +01002143// Implements android.ApexModule
Jooyung Han5e9013b2020-03-10 06:23:13 +09002144func (module *SdkLibrary) DepIsInSameApex(mctx android.BaseModuleContext, dep android.Module) bool {
2145 depTag := mctx.OtherModuleDependencyTag(dep)
2146 if depTag == xmlPermissionsFileTag {
2147 return true
2148 }
Jihoon Kanga3a05462024-04-05 00:36:44 +00002149 if dep.Name() == module.implLibraryModuleName() {
2150 return true
2151 }
Jooyung Han5e9013b2020-03-10 06:23:13 +09002152 return module.Library.DepIsInSameApex(mctx, dep)
2153}
2154
Paul Duffinea8f8082021-06-24 13:25:57 +01002155// Implements android.ApexModule
2156func (module *SdkLibrary) UniqueApexVariations() bool {
2157 return module.uniqueApexVariations()
2158}
2159
Jihoon Kangb0f4c022024-08-06 00:15:25 +00002160func (module *SdkLibrary) ModuleBuildFromTextStubs() bool {
2161 return proptools.BoolDefault(module.sdkLibraryProperties.Build_from_text_stub, true)
Jihoon Kang80456fd2023-11-15 19:22:14 +00002162}
2163
Jiyong Parkc678ad32018-04-10 13:07:10 +09002164// Creates the xml file that publicizes the runtime library
Paul Duffinf0229202020-04-29 16:47:28 +01002165func (module *SdkLibrary) createXmlFile(mctx android.DefaultableHookContext) {
Spandan Das8c9ae7e2023-03-03 21:20:36 +00002166 moduleMinApiLevel := module.Library.MinSdkVersion(mctx)
Pedro Loureiroc3621422021-09-28 15:40:23 +00002167 var moduleMinApiLevelStr = moduleMinApiLevel.String()
2168 if moduleMinApiLevel == android.NoneApiLevel {
2169 moduleMinApiLevelStr = "current"
2170 }
Jiyong Parke3833882020-02-17 17:28:10 +09002171 props := struct {
Pedro Loureiroc3621422021-09-28 15:40:23 +00002172 Name *string
2173 Lib_name *string
2174 Apex_available []string
2175 On_bootclasspath_since *string
2176 On_bootclasspath_before *string
2177 Min_device_sdk *string
2178 Max_device_sdk *string
2179 Sdk_library_min_api_level *string
Jamie Garsidee570ace2023-11-27 12:07:36 +00002180 Uses_libs_dependencies []string
Jiyong Parke3833882020-02-17 17:28:10 +09002181 }{
Pedro Loureiroc3621422021-09-28 15:40:23 +00002182 Name: proptools.StringPtr(module.xmlPermissionsModuleName()),
2183 Lib_name: proptools.StringPtr(module.BaseModuleName()),
2184 Apex_available: module.ApexProperties.Apex_available,
2185 On_bootclasspath_since: module.commonSdkLibraryProperties.On_bootclasspath_since,
2186 On_bootclasspath_before: module.commonSdkLibraryProperties.On_bootclasspath_before,
2187 Min_device_sdk: module.commonSdkLibraryProperties.Min_device_sdk,
2188 Max_device_sdk: module.commonSdkLibraryProperties.Max_device_sdk,
2189 Sdk_library_min_api_level: &moduleMinApiLevelStr,
Jamie Garsidee570ace2023-11-27 12:07:36 +00002190 Uses_libs_dependencies: module.usesLibraryProperties.Uses_libs,
Jiyong Parkc678ad32018-04-10 13:07:10 +09002191 }
Jiyong Parke3833882020-02-17 17:28:10 +09002192
Jiyong Parke3833882020-02-17 17:28:10 +09002193 mctx.CreateModule(sdkLibraryXmlFactory, &props)
Jiyong Parkc678ad32018-04-10 13:07:10 +09002194}
2195
Jiyong Parkf1691d22021-03-29 20:11:58 +09002196func PrebuiltJars(ctx android.BaseModuleContext, baseName string, s android.SdkSpec) android.Paths {
Jiyong Park54105c42021-03-31 18:17:53 +09002197 var ver android.ApiLevel
Jiyong Parkf1691d22021-03-29 20:11:58 +09002198 var kind android.SdkKind
2199 if s.UsePrebuilt(ctx) {
Jiyong Park54105c42021-03-31 18:17:53 +09002200 ver = s.ApiLevel
Jiyong Parkf1691d22021-03-29 20:11:58 +09002201 kind = s.Kind
Jiyong Parkc678ad32018-04-10 13:07:10 +09002202 } else {
Jiyong Park6a927c42020-01-21 02:03:43 +09002203 // We don't have prebuilt SDK for the specific sdkVersion.
2204 // Instead of breaking the build, fallback to use "system_current"
Jiyong Park54105c42021-03-31 18:17:53 +09002205 ver = android.FutureApiLevel
Jiyong Parkf1691d22021-03-29 20:11:58 +09002206 kind = android.SdkSystem
Sundong Ahn054b19a2018-10-19 13:46:09 +09002207 }
Jiyong Park6a927c42020-01-21 02:03:43 +09002208
2209 dir := filepath.Join("prebuilts", "sdk", ver.String(), kind.String())
Paul Duffin50061512020-01-21 16:31:05 +00002210 jar := filepath.Join(dir, baseName+".jar")
Sundong Ahn054b19a2018-10-19 13:46:09 +09002211 jarPath := android.ExistentPathForSource(ctx, jar)
Sundong Ahnae418ac2019-02-28 15:01:28 +09002212 if !jarPath.Valid() {
Colin Cross07c88562020-01-07 09:34:44 -08002213 if ctx.Config().AllowMissingDependencies() {
2214 return android.Paths{android.PathForSource(ctx, jar)}
2215 } else {
Jiyong Parkf1691d22021-03-29 20:11:58 +09002216 ctx.PropertyErrorf("sdk_library", "invalid sdk version %q, %q does not exist", s.Raw, jar)
Colin Cross07c88562020-01-07 09:34:44 -08002217 }
Sundong Ahnae418ac2019-02-28 15:01:28 +09002218 return nil
2219 }
Sundong Ahn054b19a2018-10-19 13:46:09 +09002220 return android.Paths{jarPath.Path()}
2221}
2222
Colin Crossaede88c2020-08-11 12:17:01 -07002223// Check to see if the other module is within the same set of named APEXes as this module.
Paul Duffin9b879592020-05-26 13:21:35 +01002224//
2225// If either this or the other module are on the platform then this will return
2226// false.
Colin Cross56a83212020-09-15 18:30:11 -07002227func withinSameApexesAs(ctx android.BaseModuleContext, other android.Module) bool {
Colin Crossff694a82023-12-13 15:54:49 -08002228 apexInfo, _ := android.ModuleProvider(ctx, android.ApexInfoProvider)
Colin Cross313aa542023-12-13 13:47:44 -08002229 otherApexInfo, _ := android.OtherModuleProvider(ctx, other, android.ApexInfoProvider)
Jiyong Parkab50b072021-05-12 17:13:56 +09002230 return len(otherApexInfo.InApexVariants) > 0 && reflect.DeepEqual(apexInfo.InApexVariants, otherApexInfo.InApexVariants)
Paul Duffin9b879592020-05-26 13:21:35 +01002231}
2232
Jihoon Kang8479dea2024-04-04 01:19:05 +00002233func (module *SdkLibrary) sdkJars(ctx android.BaseModuleContext, sdkVersion android.SdkSpec) android.Paths {
Jiyong Park932cdfe2020-05-28 00:19:53 +09002234 // If the client doesn't set sdk_version, but if this library prefers stubs over
2235 // the impl library, let's provide the widest API surface possible. To do so,
2236 // force override sdk_version to module_current so that the closest possible API
2237 // surface could be found in selectHeaderJarsForSdkVersion
Jiyong Parkf1691d22021-03-29 20:11:58 +09002238 if module.defaultsToStubs() && !sdkVersion.Specified() {
Jiyong Park92315372021-04-02 08:45:46 +09002239 sdkVersion = android.SdkSpecFrom(ctx, "module_current")
Jiyong Park932cdfe2020-05-28 00:19:53 +09002240 }
Paul Duffind1b3a922020-01-22 11:57:20 +00002241
Paul Duffindaaa3322020-05-26 18:13:57 +01002242 // Only provide access to the implementation library if it is actually built.
2243 if module.requiresRuntimeImplementationLibrary() {
2244 // Check any special cases for java_sdk_library.
2245 //
2246 // Only allow access to the implementation library in the following condition:
2247 // * No sdk_version specified on the referencing module.
Paul Duffin9b879592020-05-26 13:21:35 +01002248 // * The referencing module is in the same apex as this.
Jiyong Parkf1691d22021-03-29 20:11:58 +09002249 if sdkVersion.Kind == android.SdkPrivate || withinSameApexesAs(ctx, module) {
Jihoon Kang8479dea2024-04-04 01:19:05 +00002250 return module.implLibraryHeaderJars
Sundong Ahn054b19a2018-10-19 13:46:09 +09002251 }
Jiyong Parkc678ad32018-04-10 13:07:10 +09002252 }
Paul Duffinb05d4292020-05-20 12:19:10 +01002253
Paul Duffin23970f42020-05-20 14:20:02 +01002254 return module.selectHeaderJarsForSdkVersion(ctx, sdkVersion)
Jiyong Parkc678ad32018-04-10 13:07:10 +09002255}
2256
Sundong Ahn241cd372018-07-13 16:16:44 +09002257// to satisfy SdkLibraryDependency interface
Jiyong Parkf1691d22021-03-29 20:11:58 +09002258func (module *SdkLibrary) SdkHeaderJars(ctx android.BaseModuleContext, sdkVersion android.SdkSpec) android.Paths {
Jihoon Kang8479dea2024-04-04 01:19:05 +00002259 return module.sdkJars(ctx, sdkVersion)
Sundong Ahn241cd372018-07-13 16:16:44 +09002260}
2261
Colin Cross571cccf2019-02-04 11:22:08 -08002262var javaSdkLibrariesKey = android.NewOnceKey("javaSdkLibraries")
2263
Jiyong Park82484c02018-04-23 21:41:26 +09002264func javaSdkLibraries(config android.Config) *[]string {
Colin Cross571cccf2019-02-04 11:22:08 -08002265 return config.Once(javaSdkLibrariesKey, func() interface{} {
Jiyong Park82484c02018-04-23 21:41:26 +09002266 return &[]string{}
2267 }).(*[]string)
2268}
2269
Paul Duffin749f98f2019-12-30 17:23:46 +00002270func (module *SdkLibrary) getApiDir() string {
2271 return proptools.StringDefault(module.sdkLibraryProperties.Api_dir, "api")
2272}
2273
Jiyong Parkc678ad32018-04-10 13:07:10 +09002274// For a java_sdk_library module, create internal modules for stubs, docs,
2275// runtime libs and xml file. If requested, the stubs and docs are created twice
2276// once for public API level and once for system API level
Paul Duffinf0229202020-04-29 16:47:28 +01002277func (module *SdkLibrary) CreateInternalModules(mctx android.DefaultableHookContext) {
2278 // If the module has been disabled then don't create any child modules.
Cole Fausta963b942024-04-11 17:43:00 -07002279 if !module.Enabled(mctx) {
Paul Duffinf0229202020-04-29 16:47:28 +01002280 return
2281 }
2282
Paul Duffina18abc22020-05-16 18:54:24 +01002283 if len(module.properties.Srcs) == 0 {
Inseob Kimc0907f12019-02-08 21:00:45 +09002284 mctx.PropertyErrorf("srcs", "java_sdk_library must specify srcs")
Jooyung Han58f26ab2019-12-18 15:34:32 +09002285 return
Inseob Kimc0907f12019-02-08 21:00:45 +09002286 }
2287
Paul Duffin37e0b772019-12-30 17:20:10 +00002288 // If this builds against standard libraries (i.e. is not part of the core libraries)
Paul Duffin4f5c1ef2020-11-19 14:53:43 +00002289 // then assume it provides both system and test apis.
Jiyong Parkf1691d22021-03-29 20:11:58 +09002290 sdkDep := decodeSdkDep(mctx, android.SdkContext(&module.Library))
Paul Duffin37e0b772019-12-30 17:20:10 +00002291 hasSystemAndTestApis := sdkDep.hasStandardLibs()
Paul Duffin3375e352020-04-28 10:44:03 +01002292 module.sdkLibraryProperties.Generate_system_and_test_apis = hasSystemAndTestApis
Paul Duffin4f5c1ef2020-11-19 14:53:43 +00002293
Jaewoong Jung18aefc12020-12-21 09:11:10 -08002294 missingCurrentApi := false
Inseob Kim8098faa2019-03-18 10:19:51 +09002295
Paul Duffin3375e352020-04-28 10:44:03 +01002296 generatedScopes := module.getGeneratedApiScopes(mctx)
Paul Duffind1b3a922020-01-22 11:57:20 +00002297
Paul Duffin749f98f2019-12-30 17:23:46 +00002298 apiDir := module.getApiDir()
Paul Duffin3375e352020-04-28 10:44:03 +01002299 for _, scope := range generatedScopes {
Inseob Kim8098faa2019-03-18 10:19:51 +09002300 for _, api := range []string{"current.txt", "removed.txt"} {
Paul Duffind1b3a922020-01-22 11:57:20 +00002301 path := path.Join(mctx.ModuleDir(), apiDir, scope.apiFilePrefix+api)
Inseob Kim8098faa2019-03-18 10:19:51 +09002302 p := android.ExistentPathForSource(mctx, path)
2303 if !p.Valid() {
Colin Cross18f840c2021-05-20 17:56:54 -07002304 if mctx.Config().AllowMissingDependencies() {
2305 mctx.AddMissingDependencies([]string{path})
2306 } else {
2307 mctx.ModuleErrorf("Current api file %#v doesn't exist", path)
2308 missingCurrentApi = true
2309 }
Inseob Kim8098faa2019-03-18 10:19:51 +09002310 }
2311 }
2312 }
2313
Jaewoong Jung18aefc12020-12-21 09:11:10 -08002314 if missingCurrentApi {
Inseob Kim8098faa2019-03-18 10:19:51 +09002315 script := "build/soong/scripts/gen-java-current-api-files.sh"
2316 p := android.ExistentPathForSource(mctx, script)
2317
2318 if !p.Valid() {
2319 panic(fmt.Sprintf("script file %s doesn't exist", script))
2320 }
2321
2322 mctx.ModuleErrorf("One or more current api files are missing. "+
2323 "You can update them by:\n"+
Paul Duffin37e0b772019-12-30 17:20:10 +00002324 "%s %q %s && m update-api",
Paul Duffind1b3a922020-01-22 11:57:20 +00002325 script, filepath.Join(mctx.ModuleDir(), apiDir),
Paul Duffin3375e352020-04-28 10:44:03 +01002326 strings.Join(generatedScopes.Strings(func(s *apiScope) string { return s.apiFilePrefix }), " "))
Inseob Kim8098faa2019-03-18 10:19:51 +09002327 return
2328 }
2329
Paul Duffin3375e352020-04-28 10:44:03 +01002330 for _, scope := range generatedScopes {
Paul Duffin15f34ef2020-07-20 18:04:44 +01002331 // Use the stubs source name for legacy reasons.
2332 module.createStubsSourcesAndApi(mctx, scope, module.stubsSourceModuleName(scope), scope.droidstubsArgs)
Paul Duffin0ff08bd2020-04-29 13:30:54 +01002333
Paul Duffind1b3a922020-01-22 11:57:20 +00002334 module.createStubsLibrary(mctx, scope)
Jihoon Kangfa4a90d2023-12-20 02:53:38 +00002335 module.createExportableStubsLibrary(mctx, scope)
Jihoon Kang1c92c3e2023-03-23 17:44:51 +00002336
Jihoon Kangb0f4c022024-08-06 00:15:25 +00002337 if mctx.Config().BuildFromTextStub() && module.ModuleBuildFromTextStubs() {
2338 module.createApiLibrary(mctx, scope)
Jihoon Kang0c705a42023-08-02 06:44:57 +00002339 }
Jihoon Kangb0f4c022024-08-06 00:15:25 +00002340 module.createTopLevelStubsLibrary(mctx, scope)
Jihoon Kangfa4a90d2023-12-20 02:53:38 +00002341 module.createTopLevelExportableStubsLibrary(mctx, scope)
Inseob Kimc0907f12019-02-08 21:00:45 +09002342 }
2343
Paul Duffindfa131e2020-05-15 20:37:11 +01002344 if module.requiresRuntimeImplementationLibrary() {
Paul Duffin5df79302020-05-16 15:52:12 +01002345 // Create child module to create an implementation library.
2346 //
2347 // This temporarily creates a second implementation library that can be explicitly
2348 // referenced.
2349 //
2350 // TODO(b/156618935) - update comment once only one implementation library is created.
2351 module.createImplLibrary(mctx)
2352
Paul Duffindfa131e2020-05-15 20:37:11 +01002353 // Only create an XML permissions file that declares the library as being usable
2354 // as a shared library if required.
2355 if module.sharedLibrary() {
2356 module.createXmlFile(mctx)
2357 }
Paul Duffin43db9be2019-12-30 17:35:49 +00002358
2359 // record java_sdk_library modules so that they are exported to make
2360 javaSdkLibraries := javaSdkLibraries(mctx.Config())
2361 javaSdkLibrariesLock.Lock()
2362 defer javaSdkLibrariesLock.Unlock()
2363 *javaSdkLibraries = append(*javaSdkLibraries, module.BaseModuleName())
2364 }
Anton Hansson7f66efa2020-10-08 14:47:23 +01002365
Paul Duffin77590a82022-04-28 14:13:30 +00002366 // 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 +01002367 module.properties.Libs = append(module.properties.Libs, module.sdkLibraryProperties.Impl_only_libs...)
Paul Duffin77590a82022-04-28 14:13:30 +00002368 module.properties.Static_libs = append(module.properties.Static_libs, module.sdkLibraryProperties.Impl_only_static_libs...)
Inseob Kimc0907f12019-02-08 21:00:45 +09002369}
2370
2371func (module *SdkLibrary) InitSdkLibraryProperties() {
Colin Crossce6734e2020-06-15 16:09:53 -07002372 module.addHostAndDeviceProperties()
2373 module.AddProperties(&module.sdkLibraryProperties)
Sundong Ahn054b19a2018-10-19 13:46:09 +09002374
Paul Duffin71b33cc2021-06-23 11:39:47 +01002375 module.initSdkLibraryComponent(module)
Paul Duffin859fe962020-05-15 10:20:31 +01002376
Paul Duffina18abc22020-05-16 18:54:24 +01002377 module.properties.Installable = proptools.BoolPtr(true)
2378 module.deviceProperties.IsSDKLibrary = true
Inseob Kimc0907f12019-02-08 21:00:45 +09002379}
Sundong Ahn054b19a2018-10-19 13:46:09 +09002380
Paul Duffindfa131e2020-05-15 20:37:11 +01002381func (module *SdkLibrary) requiresRuntimeImplementationLibrary() bool {
2382 return !proptools.Bool(module.sdkLibraryProperties.Api_only)
2383}
2384
Jiyong Park932cdfe2020-05-28 00:19:53 +09002385func (module *SdkLibrary) defaultsToStubs() bool {
2386 return proptools.Bool(module.sdkLibraryProperties.Default_to_stubs)
2387}
2388
Paul Duffin1b1e8062020-05-08 13:44:43 +01002389// Defines how to name the individual component modules the sdk library creates.
2390type sdkLibraryComponentNamingScheme interface {
2391 stubsLibraryModuleName(scope *apiScope, baseName string) string
2392
2393 stubsSourceModuleName(scope *apiScope, baseName string) string
Jihoon Kang1c92c3e2023-03-23 17:44:51 +00002394
2395 apiLibraryModuleName(scope *apiScope, baseName string) string
Jihoon Kang1147b312023-06-08 23:25:57 +00002396
Jihoon Kangfa4a90d2023-12-20 02:53:38 +00002397 sourceStubsLibraryModuleName(scope *apiScope, baseName string) string
2398
2399 exportableStubsLibraryModuleName(scope *apiScope, baseName string) string
2400
2401 exportableSourceStubsLibraryModuleName(scope *apiScope, baseName string) string
Paul Duffin1b1e8062020-05-08 13:44:43 +01002402}
2403
2404type defaultNamingScheme struct {
2405}
2406
2407func (s *defaultNamingScheme) stubsLibraryModuleName(scope *apiScope, baseName string) string {
2408 return scope.stubsLibraryModuleName(baseName)
2409}
2410
2411func (s *defaultNamingScheme) stubsSourceModuleName(scope *apiScope, baseName string) string {
2412 return scope.stubsSourceModuleName(baseName)
2413}
2414
Jihoon Kang1c92c3e2023-03-23 17:44:51 +00002415func (s *defaultNamingScheme) apiLibraryModuleName(scope *apiScope, baseName string) string {
2416 return scope.apiLibraryModuleName(baseName)
2417}
2418
Jihoon Kangfa4a90d2023-12-20 02:53:38 +00002419func (s *defaultNamingScheme) sourceStubsLibraryModuleName(scope *apiScope, baseName string) string {
Jihoon Kang1147b312023-06-08 23:25:57 +00002420 return scope.sourceStubLibraryModuleName(baseName)
2421}
2422
Jihoon Kangfa4a90d2023-12-20 02:53:38 +00002423func (s *defaultNamingScheme) exportableStubsLibraryModuleName(scope *apiScope, baseName string) string {
2424 return scope.exportableStubsLibraryModuleName(baseName)
2425}
2426
2427func (s *defaultNamingScheme) exportableSourceStubsLibraryModuleName(scope *apiScope, baseName string) string {
2428 return scope.exportableSourceStubsLibraryModuleName(baseName)
2429}
2430
Paul Duffin1b1e8062020-05-08 13:44:43 +01002431var _ sdkLibraryComponentNamingScheme = (*defaultNamingScheme)(nil)
2432
Jihoon Kangfa3f0782024-08-21 20:42:18 +00002433func moduleStubLinkType(j *Module) (stub bool, ret sdkLinkType) {
2434 kind := android.ToSdkKind(proptools.String(j.properties.Stub_contributing_api))
2435 switch kind {
2436 case android.SdkPublic:
Anton Hansson2d0c1942020-05-25 12:20:51 +01002437 return true, javaSdk
Jihoon Kangfa3f0782024-08-21 20:42:18 +00002438 case android.SdkSystem:
Anton Hansson2d0c1942020-05-25 12:20:51 +01002439 return true, javaSystem
Jihoon Kangfa3f0782024-08-21 20:42:18 +00002440 case android.SdkModule:
Anton Hansson2d0c1942020-05-25 12:20:51 +01002441 return true, javaModule
Jihoon Kangfa3f0782024-08-21 20:42:18 +00002442 case android.SdkTest:
Anton Hansson2d0c1942020-05-25 12:20:51 +01002443 return true, javaSystem
Jihoon Kangfa3f0782024-08-21 20:42:18 +00002444 case android.SdkSystemServer:
Jihoon Kang1147b312023-06-08 23:25:57 +00002445 return true, javaSystemServer
Jihoon Kangfa3f0782024-08-21 20:42:18 +00002446 // Default value for all modules other than java_sdk_library-generated stub submodules
2447 case android.SdkInvalid:
2448 return false, javaPlatform
2449 default:
2450 panic(fmt.Sprintf("stub_contributing_api set as an unsupported sdk kind %s", kind.String()))
Jihoon Kang1147b312023-06-08 23:25:57 +00002451 }
Anton Hansson2d0c1942020-05-25 12:20:51 +01002452}
2453
Jaewoong Jung4f158ee2019-07-11 10:05:35 -07002454// java_sdk_library is a special Java library that provides optional platform APIs to apps.
2455// In practice, it can be viewed as a combination of several modules: 1) stubs library that clients
2456// are linked against to, 2) droiddoc module that internally generates API stubs source files,
2457// 3) the real runtime shared library that implements the APIs, and 4) XML file for adding
2458// the runtime lib to the classpath at runtime if requested via <uses-library>.
Inseob Kimc0907f12019-02-08 21:00:45 +09002459func SdkLibraryFactory() android.Module {
2460 module := &SdkLibrary{}
Paul Duffinc3091c82020-05-08 14:16:20 +01002461
2462 // Initialize information common between source and prebuilt.
Paul Duffin71b33cc2021-06-23 11:39:47 +01002463 module.initCommon(module)
Paul Duffinc3091c82020-05-08 14:16:20 +01002464
Inseob Kimc0907f12019-02-08 21:00:45 +09002465 module.InitSdkLibraryProperties()
Jooyung Han58f26ab2019-12-18 15:34:32 +09002466 android.InitApexModule(module)
Sundong Ahn054b19a2018-10-19 13:46:09 +09002467 InitJavaModule(module, android.HostAndDeviceSupported)
Paul Duffin3375e352020-04-28 10:44:03 +01002468
2469 // Initialize the map from scope to scope specific properties.
2470 scopeToProperties := make(map[*apiScope]*ApiScopeProperties)
Jihoon Kang98aa8fa2024-06-07 11:06:57 +00002471 for _, scope := range AllApiScopes {
Paul Duffin3375e352020-04-28 10:44:03 +01002472 scopeToProperties[scope] = scope.scopeSpecificProperties(module)
2473 }
2474 module.scopeToProperties = scopeToProperties
2475
Paul Duffin4911a892020-04-29 23:35:13 +01002476 // Add the properties containing visibility rules so that they are checked.
Paul Duffin5df79302020-05-16 15:52:12 +01002477 android.AddVisibilityProperty(module, "impl_library_visibility", &module.sdkLibraryProperties.Impl_library_visibility)
Paul Duffin4911a892020-04-29 23:35:13 +01002478 android.AddVisibilityProperty(module, "stubs_library_visibility", &module.sdkLibraryProperties.Stubs_library_visibility)
2479 android.AddVisibilityProperty(module, "stubs_source_visibility", &module.sdkLibraryProperties.Stubs_source_visibility)
2480
Paul Duffin1b1e8062020-05-08 13:44:43 +01002481 module.SetDefaultableHook(func(ctx android.DefaultableHookContext) {
Paul Duffindfa131e2020-05-15 20:37:11 +01002482 // If no implementation is required then it cannot be used as a shared library
2483 // either.
2484 if !module.requiresRuntimeImplementationLibrary() {
2485 // If shared_library has been explicitly set to true then it is incompatible
2486 // with api_only: true.
2487 if proptools.Bool(module.commonSdkLibraryProperties.Shared_library) {
2488 ctx.PropertyErrorf("api_only/shared_library", "inconsistent settings, shared_library and api_only cannot both be true")
2489 }
2490 // Set shared_library: false.
2491 module.commonSdkLibraryProperties.Shared_library = proptools.BoolPtr(false)
2492 }
2493
Paul Duffin1b1e8062020-05-08 13:44:43 +01002494 if module.initCommonAfterDefaultsApplied(ctx) {
2495 module.CreateInternalModules(ctx)
2496 }
2497 })
Jiyong Parkc678ad32018-04-10 13:07:10 +09002498 return module
2499}
Colin Cross79c7c262019-04-17 11:11:46 -07002500
2501//
2502// SDK library prebuilts
2503//
2504
Paul Duffin56d44902020-01-31 13:36:25 +00002505// Properties associated with each api scope.
2506type sdkLibraryScopeProperties struct {
Colin Cross79c7c262019-04-17 11:11:46 -07002507 Jars []string `android:"path"`
2508
2509 Sdk_version *string
2510
Colin Cross79c7c262019-04-17 11:11:46 -07002511 // List of shared java libs that this module has dependencies to
2512 Libs []string
Paul Duffin3d1248c2020-04-09 00:10:17 +01002513
Paul Duffinc8782502020-04-29 20:45:27 +01002514 // The stubs source.
Paul Duffin3d1248c2020-04-09 00:10:17 +01002515 Stub_srcs []string `android:"path"`
Paul Duffin1fd005d2020-04-09 01:08:11 +01002516
2517 // The current.txt
Paul Duffin0f8faff2020-05-20 16:18:00 +01002518 Current_api *string `android:"path"`
Paul Duffin1fd005d2020-04-09 01:08:11 +01002519
2520 // The removed.txt
Paul Duffin0f8faff2020-05-20 16:18:00 +01002521 Removed_api *string `android:"path"`
Anton Hanssond78eb762021-09-21 15:25:12 +01002522
2523 // Annotation zip
2524 Annotations *string `android:"path"`
Colin Cross79c7c262019-04-17 11:11:46 -07002525}
2526
Paul Duffin56d44902020-01-31 13:36:25 +00002527type sdkLibraryImportProperties struct {
Paul Duffinfcfd7912020-01-31 17:54:30 +00002528 // List of shared java libs, common to all scopes, that this module has
2529 // dependencies to
2530 Libs []string
Paul Duffin1267d872021-04-16 17:21:36 +01002531
2532 // If set to true, compile dex files for the stubs. Defaults to false.
2533 Compile_dex *bool
Paul Duffin869de142021-07-15 14:14:41 +01002534
2535 // If not empty, classes are restricted to the specified packages and their sub-packages.
Paul Duffin869de142021-07-15 14:14:41 +01002536 Permitted_packages []string
Spandan Das23956d12024-01-19 00:22:22 +00002537
2538 // Name of the source soong module that gets shadowed by this prebuilt
2539 // If unspecified, follows the naming convention that the source module of
2540 // the prebuilt is Name() without "prebuilt_" prefix
2541 Source_module_name *string
Paul Duffin56d44902020-01-31 13:36:25 +00002542}
2543
Paul Duffineedc5d52020-06-12 17:46:39 +01002544type SdkLibraryImport struct {
Colin Cross79c7c262019-04-17 11:11:46 -07002545 android.ModuleBase
2546 android.DefaultableModuleBase
2547 prebuilt android.Prebuilt
Paul Duffindd46f712020-02-10 13:37:10 +00002548 android.ApexModuleBase
Colin Cross79c7c262019-04-17 11:11:46 -07002549
Paul Duffin37856732021-02-26 14:24:15 +00002550 hiddenAPI
Jiakai Zhang204356f2021-09-09 08:12:46 +00002551 dexpreopter
Paul Duffin37856732021-02-26 14:24:15 +00002552
Colin Cross79c7c262019-04-17 11:11:46 -07002553 properties sdkLibraryImportProperties
2554
Paul Duffin46a26a82020-04-07 19:27:04 +01002555 // Map from api scope to the scope specific property structure.
2556 scopeProperties map[*apiScope]*sdkLibraryScopeProperties
2557
Paul Duffin56d44902020-01-31 13:36:25 +00002558 commonToSdkLibraryAndImport
Paul Duffineedc5d52020-06-12 17:46:39 +01002559
Paul Duffineedc5d52020-06-12 17:46:39 +01002560 // The reference to the xml permissions module created by the source module.
2561 // Is nil if the source module does not exist.
2562 xmlPermissionsFileModule *sdkLibraryXml
Paul Duffin39853512021-02-26 11:09:39 +00002563
Jeongik Chad5fe8782021-07-08 01:13:11 +09002564 // Build path to the dex implementation jar obtained from the prebuilt_apex, if any.
Spandan Dasfae468e2023-12-12 23:23:53 +00002565 dexJarFile OptionalDexJarPath
2566 dexJarFileErr error
Jeongik Chad5fe8782021-07-08 01:13:11 +09002567
2568 // Expected install file path of the source module(sdk_library)
2569 // or dex implementation jar obtained from the prebuilt_apex, if any.
2570 installFile android.Path
Colin Cross79c7c262019-04-17 11:11:46 -07002571}
2572
Paul Duffineedc5d52020-06-12 17:46:39 +01002573var _ SdkLibraryDependency = (*SdkLibraryImport)(nil)
Colin Cross79c7c262019-04-17 11:11:46 -07002574
Paul Duffin46a26a82020-04-07 19:27:04 +01002575// The type of a structure that contains a field of type sdkLibraryScopeProperties
2576// for each apiscope in allApiScopes, e.g. something like:
Colin Crossd079e0b2022-08-16 10:27:33 -07002577//
2578// struct {
2579// Public sdkLibraryScopeProperties
2580// System sdkLibraryScopeProperties
2581// ...
2582// }
Paul Duffin46a26a82020-04-07 19:27:04 +01002583var allScopeStructType = createAllScopePropertiesStructType()
2584
2585// Dynamically create a structure type for each apiscope in allApiScopes.
2586func createAllScopePropertiesStructType() reflect.Type {
2587 var fields []reflect.StructField
Jihoon Kang98aa8fa2024-06-07 11:06:57 +00002588 for _, apiScope := range AllApiScopes {
Paul Duffin46a26a82020-04-07 19:27:04 +01002589 field := reflect.StructField{
2590 Name: apiScope.fieldName,
2591 Type: reflect.TypeOf(sdkLibraryScopeProperties{}),
2592 }
2593 fields = append(fields, field)
2594 }
2595
2596 return reflect.StructOf(fields)
2597}
2598
2599// Create an instance of the scope specific structure type and return a map
2600// from apiscope to a pointer to each scope specific field.
2601func createPropertiesInstance() (interface{}, map[*apiScope]*sdkLibraryScopeProperties) {
2602 allScopePropertiesPtr := reflect.New(allScopeStructType)
2603 allScopePropertiesStruct := allScopePropertiesPtr.Elem()
2604 scopeProperties := make(map[*apiScope]*sdkLibraryScopeProperties)
2605
Jihoon Kang98aa8fa2024-06-07 11:06:57 +00002606 for _, apiScope := range AllApiScopes {
Paul Duffin46a26a82020-04-07 19:27:04 +01002607 field := allScopePropertiesStruct.FieldByName(apiScope.fieldName)
2608 scopeProperties[apiScope] = field.Addr().Interface().(*sdkLibraryScopeProperties)
2609 }
2610
2611 return allScopePropertiesPtr.Interface(), scopeProperties
2612}
2613
Jaewoong Jung4f158ee2019-07-11 10:05:35 -07002614// java_sdk_library_import imports a prebuilt java_sdk_library.
Colin Cross79c7c262019-04-17 11:11:46 -07002615func sdkLibraryImportFactory() android.Module {
Paul Duffineedc5d52020-06-12 17:46:39 +01002616 module := &SdkLibraryImport{}
Colin Cross79c7c262019-04-17 11:11:46 -07002617
Paul Duffin46a26a82020-04-07 19:27:04 +01002618 allScopeProperties, scopeToProperties := createPropertiesInstance()
2619 module.scopeProperties = scopeToProperties
Jiakai Zhang9c4dc192023-02-09 00:09:24 +08002620 module.AddProperties(&module.properties, allScopeProperties, &module.importDexpreoptProperties)
Colin Cross79c7c262019-04-17 11:11:46 -07002621
Paul Duffinc3091c82020-05-08 14:16:20 +01002622 // Initialize information common between source and prebuilt.
Paul Duffin71b33cc2021-06-23 11:39:47 +01002623 module.initCommon(module)
Paul Duffinc3091c82020-05-08 14:16:20 +01002624
Paul Duffin0bdcb272020-02-06 15:24:57 +00002625 android.InitPrebuiltModule(module, &[]string{""})
Paul Duffindd46f712020-02-10 13:37:10 +00002626 android.InitApexModule(module)
Colin Cross79c7c262019-04-17 11:11:46 -07002627 InitJavaModule(module, android.HostAndDeviceSupported)
2628
Paul Duffin1b1e8062020-05-08 13:44:43 +01002629 module.SetDefaultableHook(func(mctx android.DefaultableHookContext) {
2630 if module.initCommonAfterDefaultsApplied(mctx) {
2631 module.createInternalModules(mctx)
2632 }
2633 })
Colin Cross79c7c262019-04-17 11:11:46 -07002634 return module
2635}
2636
Paul Duffin630b11e2021-07-15 13:35:26 +01002637var _ PermittedPackagesForUpdatableBootJars = (*SdkLibraryImport)(nil)
2638
2639func (module *SdkLibraryImport) PermittedPackagesForUpdatableBootJars() []string {
2640 return module.properties.Permitted_packages
2641}
2642
Paul Duffineedc5d52020-06-12 17:46:39 +01002643func (module *SdkLibraryImport) Prebuilt() *android.Prebuilt {
Colin Cross79c7c262019-04-17 11:11:46 -07002644 return &module.prebuilt
2645}
2646
Paul Duffineedc5d52020-06-12 17:46:39 +01002647func (module *SdkLibraryImport) Name() string {
Colin Cross79c7c262019-04-17 11:11:46 -07002648 return module.prebuilt.Name(module.ModuleBase.Name())
2649}
2650
Spandan Das23956d12024-01-19 00:22:22 +00002651func (module *SdkLibraryImport) BaseModuleName() string {
2652 return proptools.StringDefault(module.properties.Source_module_name, module.ModuleBase.Name())
2653}
2654
Paul Duffineedc5d52020-06-12 17:46:39 +01002655func (module *SdkLibraryImport) createInternalModules(mctx android.DefaultableHookContext) {
Colin Cross79c7c262019-04-17 11:11:46 -07002656
Paul Duffin50061512020-01-21 16:31:05 +00002657 // If the build is configured to use prebuilts then force this to be preferred.
Jeongik Cha816a23a2020-07-08 01:09:23 +09002658 if mctx.Config().AlwaysUsePrebuiltSdks() {
Paul Duffin50061512020-01-21 16:31:05 +00002659 module.prebuilt.ForcePrefer()
2660 }
2661
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
Paul Duffinbbb546b2020-04-09 00:07:11 +01002667 module.createJavaImportForStubs(mctx, apiScope, scopeProperties)
Paul Duffin3d1248c2020-04-09 00:10:17 +01002668
Paul Duffin0f8faff2020-05-20 16:18:00 +01002669 if len(scopeProperties.Stub_srcs) > 0 {
2670 module.createPrebuiltStubsSources(mctx, apiScope, scopeProperties)
2671 }
Jihoon Kang71c86832023-09-13 01:01:53 +00002672
2673 if scopeProperties.Current_api != nil {
2674 module.createPrebuiltApiContribution(mctx, apiScope, scopeProperties)
2675 }
Paul Duffin56d44902020-01-31 13:36:25 +00002676 }
Colin Cross79c7c262019-04-17 11:11:46 -07002677
2678 javaSdkLibraries := javaSdkLibraries(mctx.Config())
2679 javaSdkLibrariesLock.Lock()
2680 defer javaSdkLibrariesLock.Unlock()
2681 *javaSdkLibraries = append(*javaSdkLibraries, module.BaseModuleName())
2682}
2683
Paul Duffineedc5d52020-06-12 17:46:39 +01002684func (module *SdkLibraryImport) createJavaImportForStubs(mctx android.DefaultableHookContext, apiScope *apiScope, scopeProperties *sdkLibraryScopeProperties) {
Paul Duffinbbb546b2020-04-09 00:07:11 +01002685 // Creates a java import for the jar with ".stubs" suffix
2686 props := struct {
Spandan Das23956d12024-01-19 00:22:22 +00002687 Name *string
2688 Source_module_name *string
2689 Created_by_java_sdk_library_name *string
2690 Sdk_version *string
2691 Libs []string
2692 Jars []string
2693 Compile_dex *bool
Jihoon Kangfe914ed2024-02-12 22:49:21 +00002694 Is_stubs_module *bool
Paul Duffinbf4de042022-09-27 12:41:52 +01002695
2696 android.UserSuppliedPrebuiltProperties
Paul Duffinbbb546b2020-04-09 00:07:11 +01002697 }{}
Paul Duffinc3091c82020-05-08 14:16:20 +01002698 props.Name = proptools.StringPtr(module.stubsLibraryModuleName(apiScope))
Spandan Das23956d12024-01-19 00:22:22 +00002699 props.Source_module_name = proptools.StringPtr(apiScope.stubsLibraryModuleName(module.BaseModuleName()))
2700 props.Created_by_java_sdk_library_name = proptools.StringPtr(module.RootLibraryName())
Paul Duffinbbb546b2020-04-09 00:07:11 +01002701 props.Sdk_version = scopeProperties.Sdk_version
2702 // Prepend any of the libs from the legacy public properties to the libs for each of the
2703 // scopes to avoid having to duplicate them in each scope.
2704 props.Libs = append(module.properties.Libs, scopeProperties.Libs...)
2705 props.Jars = scopeProperties.Jars
Paul Duffin1dbe3ca2020-05-16 09:57:59 +01002706
Paul Duffin38b57852020-05-13 16:08:09 +01002707 // The imports are preferred if the java_sdk_library_import is preferred.
Paul Duffinbf4de042022-09-27 12:41:52 +01002708 props.CopyUserSuppliedPropertiesFromPrebuilt(&module.prebuilt)
Paul Duffin859fe962020-05-15 10:20:31 +01002709
Paul Duffin1267d872021-04-16 17:21:36 +01002710 // The imports need to be compiled to dex if the java_sdk_library_import requests it.
Paul Duffinf4600f62021-05-13 22:34:45 +01002711 compileDex := module.properties.Compile_dex
2712 if module.stubLibrariesCompiledForDex() {
2713 compileDex = proptools.BoolPtr(true)
2714 }
2715 props.Compile_dex = compileDex
Jihoon Kangfe914ed2024-02-12 22:49:21 +00002716 props.Is_stubs_module = proptools.BoolPtr(true)
Paul Duffin1267d872021-04-16 17:21:36 +01002717
Paul Duffin859fe962020-05-15 10:20:31 +01002718 mctx.CreateModule(ImportFactory, &props, module.sdkComponentPropertiesForChildLibrary())
Paul Duffinbbb546b2020-04-09 00:07:11 +01002719}
2720
Paul Duffineedc5d52020-06-12 17:46:39 +01002721func (module *SdkLibraryImport) createPrebuiltStubsSources(mctx android.DefaultableHookContext, apiScope *apiScope, scopeProperties *sdkLibraryScopeProperties) {
Paul Duffin3d1248c2020-04-09 00:10:17 +01002722 props := struct {
Spandan Das23956d12024-01-19 00:22:22 +00002723 Name *string
2724 Source_module_name *string
2725 Created_by_java_sdk_library_name *string
2726 Srcs []string
Paul Duffinbf4de042022-09-27 12:41:52 +01002727
2728 android.UserSuppliedPrebuiltProperties
Paul Duffin3d1248c2020-04-09 00:10:17 +01002729 }{}
Paul Duffinc3091c82020-05-08 14:16:20 +01002730 props.Name = proptools.StringPtr(module.stubsSourceModuleName(apiScope))
Spandan Das23956d12024-01-19 00:22:22 +00002731 props.Source_module_name = proptools.StringPtr(apiScope.stubsSourceModuleName(module.BaseModuleName()))
2732 props.Created_by_java_sdk_library_name = proptools.StringPtr(module.RootLibraryName())
Paul Duffin3d1248c2020-04-09 00:10:17 +01002733 props.Srcs = scopeProperties.Stub_srcs
Paul Duffin38b57852020-05-13 16:08:09 +01002734
2735 // The stubs source is preferred if the java_sdk_library_import is preferred.
Paul Duffinbf4de042022-09-27 12:41:52 +01002736 props.CopyUserSuppliedPropertiesFromPrebuilt(&module.prebuilt)
2737
Spandan Das2cc80ba2023-10-27 17:21:52 +00002738 mctx.CreateModule(PrebuiltStubsSourcesFactory, &props, module.sdkComponentPropertiesForChildLibrary())
Paul Duffin3d1248c2020-04-09 00:10:17 +01002739}
2740
Jihoon Kang71c86832023-09-13 01:01:53 +00002741func (module *SdkLibraryImport) createPrebuiltApiContribution(mctx android.DefaultableHookContext, apiScope *apiScope, scopeProperties *sdkLibraryScopeProperties) {
2742 api_file := scopeProperties.Current_api
2743 api_surface := &apiScope.name
2744
2745 props := struct {
Spandan Das23956d12024-01-19 00:22:22 +00002746 Name *string
2747 Source_module_name *string
2748 Created_by_java_sdk_library_name *string
2749 Api_surface *string
2750 Api_file *string
2751 Visibility []string
Jihoon Kang71c86832023-09-13 01:01:53 +00002752 }{}
2753
2754 props.Name = proptools.StringPtr(module.stubsSourceModuleName(apiScope) + ".api.contribution")
Spandan Das23956d12024-01-19 00:22:22 +00002755 props.Source_module_name = proptools.StringPtr(apiScope.stubsSourceModuleName(module.BaseModuleName()) + ".api.contribution")
2756 props.Created_by_java_sdk_library_name = proptools.StringPtr(module.RootLibraryName())
Jihoon Kang71c86832023-09-13 01:01:53 +00002757 props.Api_surface = api_surface
2758 props.Api_file = api_file
2759 props.Visibility = []string{"//visibility:override", "//visibility:public"}
2760
Spandan Das2cc80ba2023-10-27 17:21:52 +00002761 mctx.CreateModule(ApiContributionImportFactory, &props, module.sdkComponentPropertiesForChildLibrary())
Jihoon Kang71c86832023-09-13 01:01:53 +00002762}
2763
Paul Duffin44f1d842020-06-26 20:17:02 +01002764// Add the dependencies on the child module in the component deps mutator so that it
2765// creates references to the prebuilt and not the source modules.
2766func (module *SdkLibraryImport) ComponentDepsMutator(ctx android.BottomUpMutatorContext) {
Paul Duffin46a26a82020-04-07 19:27:04 +01002767 for apiScope, scopeProperties := range module.scopeProperties {
Paul Duffin56d44902020-01-31 13:36:25 +00002768 if len(scopeProperties.Jars) == 0 {
2769 continue
2770 }
2771
2772 // Add dependencies to the prebuilt stubs library
Jihoon Kangb7431552024-01-22 19:40:08 +00002773 ctx.AddVariationDependencies(nil, apiScope.prebuiltStubsTag, android.PrebuiltNameFromSource(module.stubsLibraryModuleName(apiScope)))
Paul Duffin0f8faff2020-05-20 16:18:00 +01002774
2775 if len(scopeProperties.Stub_srcs) > 0 {
2776 // Add dependencies to the prebuilt stubs source library
Paul Duffin864116c2021-04-02 10:24:13 +01002777 ctx.AddVariationDependencies(nil, apiScope.stubsSourceTag, android.PrebuiltNameFromSource(module.stubsSourceModuleName(apiScope)))
Paul Duffin0f8faff2020-05-20 16:18:00 +01002778 }
Paul Duffin56d44902020-01-31 13:36:25 +00002779 }
Paul Duffin44f1d842020-06-26 20:17:02 +01002780}
2781
2782// Add other dependencies as normal.
2783func (module *SdkLibraryImport) DepsMutator(ctx android.BottomUpMutatorContext) {
Paul Duffineedc5d52020-06-12 17:46:39 +01002784
2785 implName := module.implLibraryModuleName()
2786 if ctx.OtherModuleExists(implName) {
2787 ctx.AddVariationDependencies(nil, implLibraryTag, implName)
2788
2789 xmlPermissionsModuleName := module.xmlPermissionsModuleName()
2790 if module.sharedLibrary() && ctx.OtherModuleExists(xmlPermissionsModuleName) {
2791 // Add dependency to the rule for generating the xml permissions file
2792 ctx.AddDependency(module, xmlPermissionsFileTag, xmlPermissionsModuleName)
2793 }
2794 }
Colin Cross79c7c262019-04-17 11:11:46 -07002795}
2796
Jiyong Park45bf82e2020-12-15 22:29:02 +09002797var _ android.ApexModule = (*SdkLibraryImport)(nil)
2798
2799// Implements android.ApexModule
Paul Duffineedc5d52020-06-12 17:46:39 +01002800func (module *SdkLibraryImport) DepIsInSameApex(mctx android.BaseModuleContext, dep android.Module) bool {
2801 depTag := mctx.OtherModuleDependencyTag(dep)
2802 if depTag == xmlPermissionsFileTag {
2803 return true
2804 }
2805
2806 // None of the other dependencies of the java_sdk_library_import are in the same apex
2807 // as the one that references this module.
2808 return false
2809}
2810
Jiyong Park45bf82e2020-12-15 22:29:02 +09002811// Implements android.ApexModule
Dan Albertc8060532020-07-22 22:32:17 -07002812func (module *SdkLibraryImport) ShouldSupportSdkVersion(ctx android.BaseModuleContext,
2813 sdkVersion android.ApiLevel) error {
Jooyung Han749dc692020-04-15 11:03:39 +09002814 // we don't check prebuilt modules for sdk_version
2815 return nil
2816}
2817
Paul Duffinea8f8082021-06-24 13:25:57 +01002818// Implements android.ApexModule
2819func (module *SdkLibraryImport) UniqueApexVariations() bool {
2820 return module.uniqueApexVariations()
2821}
2822
Paul Duffin09817d62022-04-28 17:45:11 +01002823// MinSdkVersion - Implements hiddenAPIModule
Spandan Das8c9ae7e2023-03-03 21:20:36 +00002824func (module *SdkLibraryImport) MinSdkVersion(ctx android.EarlyModuleContext) android.ApiLevel {
2825 return android.NoneApiLevel
Paul Duffin09817d62022-04-28 17:45:11 +01002826}
2827
2828var _ hiddenAPIModule = (*SdkLibraryImport)(nil)
2829
Paul Duffineedc5d52020-06-12 17:46:39 +01002830func (module *SdkLibraryImport) GenerateAndroidBuildActions(ctx android.ModuleContext) {
Paul Duffina2ae7e02020-09-11 11:55:00 +01002831 module.generateCommonBuildActions(ctx)
2832
Jeongik Chad5fe8782021-07-08 01:13:11 +09002833 // Assume that source module(sdk_library) is installed in /<sdk_library partition>/framework
2834 module.installFile = android.PathForModuleInstall(ctx, "framework", module.Stem()+".jar")
2835
Paul Duffin0f8faff2020-05-20 16:18:00 +01002836 // Record the paths to the prebuilt stubs library and stubs source.
Colin Cross79c7c262019-04-17 11:11:46 -07002837 ctx.VisitDirectDeps(func(to android.Module) {
2838 tag := ctx.OtherModuleDependencyTag(to)
2839
Paul Duffin0f8faff2020-05-20 16:18:00 +01002840 // Extract information from any of the scope specific dependencies.
2841 if scopeTag, ok := tag.(scopeDependencyTag); ok {
2842 apiScope := scopeTag.apiScope
2843 scopePaths := module.getScopePathsCreateIfNeeded(apiScope)
2844
2845 // Extract information from the dependency. The exact information extracted
2846 // is determined by the nature of the dependency which is determined by the tag.
2847 scopeTag.extractDepInfo(ctx, to, scopePaths)
Paul Duffineedc5d52020-06-12 17:46:39 +01002848 } else if tag == implLibraryTag {
2849 if implLibrary, ok := to.(*Library); ok {
2850 module.implLibraryModule = implLibrary
2851 } else {
2852 ctx.ModuleErrorf("implementation library must be of type *java.Library but was %T", to)
2853 }
2854 } else if tag == xmlPermissionsFileTag {
2855 if xmlPermissionsFileModule, ok := to.(*sdkLibraryXml); ok {
2856 module.xmlPermissionsFileModule = xmlPermissionsFileModule
2857 } else {
2858 ctx.ModuleErrorf("xml permissions file module must be of type *sdkLibraryXml but was %T", to)
2859 }
Colin Cross79c7c262019-04-17 11:11:46 -07002860 }
2861 })
Paul Duffin0f8faff2020-05-20 16:18:00 +01002862
2863 // Populate the scope paths with information from the properties.
2864 for apiScope, scopeProperties := range module.scopeProperties {
2865 if len(scopeProperties.Jars) == 0 {
2866 continue
2867 }
2868
2869 paths := module.getScopePathsCreateIfNeeded(apiScope)
Anton Hanssond78eb762021-09-21 15:25:12 +01002870 paths.annotationsZip = android.OptionalPathForModuleSrc(ctx, scopeProperties.Annotations)
Paul Duffin0f8faff2020-05-20 16:18:00 +01002871 paths.currentApiFilePath = android.OptionalPathForModuleSrc(ctx, scopeProperties.Current_api)
2872 paths.removedApiFilePath = android.OptionalPathForModuleSrc(ctx, scopeProperties.Removed_api)
2873 }
Paul Duffin39853512021-02-26 11:09:39 +00002874
2875 if ctx.Device() {
2876 // If this is a variant created for a prebuilt_apex then use the dex implementation jar
2877 // obtained from the associated deapexer module.
Colin Crossff694a82023-12-13 15:54:49 -08002878 ai, _ := android.ModuleProvider(ctx, android.ApexInfoProvider)
Paul Duffin39853512021-02-26 11:09:39 +00002879 if ai.ForPrebuiltApex {
Paul Duffin39853512021-02-26 11:09:39 +00002880 // Get the path of the dex implementation jar from the `deapexer` module.
Spandan Dasfae468e2023-12-12 23:23:53 +00002881 di, err := android.FindDeapexerProviderForModule(ctx)
2882 if err != nil {
2883 // An error was found, possibly due to multiple apexes in the tree that export this library
2884 // Defer the error till a client tries to call DexJarBuildPath
2885 module.dexJarFileErr = err
Spandan Das3a392012024-01-17 18:26:27 +00002886 module.initHiddenAPIError(err)
Spandan Dasfae468e2023-12-12 23:23:53 +00002887 return
Martin Stjernholm44825602021-09-17 01:44:12 +01002888 }
Spandan Das5be63332023-12-13 00:06:32 +00002889 dexJarFileApexRootRelative := ApexRootRelativePathToJavaLib(module.BaseModuleName())
Jiakai Zhang81e46812023-02-08 21:56:07 +08002890 if dexOutputPath := di.PrebuiltExportPath(dexJarFileApexRootRelative); dexOutputPath != nil {
Martin Stjernholm8be1e6d2021-09-15 03:34:04 +01002891 dexJarFile := makeDexJarPathFromPath(dexOutputPath)
2892 module.dexJarFile = dexJarFile
Jiakai Zhang204356f2021-09-09 08:12:46 +00002893 installPath := android.PathForModuleInPartitionInstall(
Jiakai Zhang81e46812023-02-08 21:56:07 +08002894 ctx, "apex", ai.ApexVariationName, dexJarFileApexRootRelative)
Jiakai Zhang204356f2021-09-09 08:12:46 +00002895 module.installFile = installPath
Martin Stjernholm8be1e6d2021-09-15 03:34:04 +01002896 module.initHiddenAPI(ctx, dexJarFile, module.findScopePaths(apiScopePublic).stubsImplPath[0], nil)
Jiakai Zhang204356f2021-09-09 08:12:46 +00002897
Spandan Dase21a8d42024-01-23 23:56:29 +00002898 module.dexpreopter.installPath = module.dexpreopter.getInstallPath(ctx, android.RemoveOptionalPrebuiltPrefix(ctx.ModuleName()), installPath)
Jiakai Zhang204356f2021-09-09 08:12:46 +00002899 module.dexpreopter.isSDKLibrary = true
Spandan Dase21a8d42024-01-23 23:56:29 +00002900 module.dexpreopter.uncompressedDex = shouldUncompressDex(ctx, android.RemoveOptionalPrebuiltPrefix(ctx.ModuleName()), &module.dexpreopter)
Jiakai Zhang81e46812023-02-08 21:56:07 +08002901
2902 if profilePath := di.PrebuiltExportPath(dexJarFileApexRootRelative + ".prof"); profilePath != nil {
2903 module.dexpreopter.inputProfilePathOnHost = profilePath
2904 }
Paul Duffin39853512021-02-26 11:09:39 +00002905 } else {
2906 // This should never happen as a variant for a prebuilt_apex is only created if the
2907 // prebuilt_apex has been configured to export the java library dex file.
Martin Stjernholm44825602021-09-17 01:44:12 +01002908 ctx.ModuleErrorf("internal error: no dex implementation jar available from prebuilt APEX %s", di.ApexModuleName())
Paul Duffin39853512021-02-26 11:09:39 +00002909 }
2910 }
2911 }
mrziwang9f7b9f42024-07-10 12:18:06 -07002912
2913 module.setOutputFiles(ctx)
2914 if module.implLibraryModule != nil {
2915 setOutputFiles(ctx, module.implLibraryModule.Module)
2916 }
Colin Cross79c7c262019-04-17 11:11:46 -07002917}
2918
Jiyong Parkf1691d22021-03-29 20:11:58 +09002919func (module *SdkLibraryImport) sdkJars(ctx android.BaseModuleContext, sdkVersion android.SdkSpec, headerJars bool) android.Paths {
Paul Duffineedc5d52020-06-12 17:46:39 +01002920
2921 // For consistency with SdkLibrary make the implementation jar available to libraries that
2922 // are within the same APEX.
2923 implLibraryModule := module.implLibraryModule
Colin Cross56a83212020-09-15 18:30:11 -07002924 if implLibraryModule != nil && withinSameApexesAs(ctx, module) {
Paul Duffineedc5d52020-06-12 17:46:39 +01002925 if headerJars {
2926 return implLibraryModule.HeaderJars()
2927 } else {
2928 return implLibraryModule.ImplementationJars()
2929 }
2930 }
2931
Paul Duffin23970f42020-05-20 14:20:02 +01002932 return module.selectHeaderJarsForSdkVersion(ctx, sdkVersion)
Paul Duffin56d44902020-01-31 13:36:25 +00002933}
2934
Colin Cross79c7c262019-04-17 11:11:46 -07002935// to satisfy SdkLibraryDependency interface
Jiyong Parkf1691d22021-03-29 20:11:58 +09002936func (module *SdkLibraryImport) SdkHeaderJars(ctx android.BaseModuleContext, sdkVersion android.SdkSpec) android.Paths {
Colin Cross79c7c262019-04-17 11:11:46 -07002937 // This module is just a wrapper for the prebuilt stubs.
Paul Duffineedc5d52020-06-12 17:46:39 +01002938 return module.sdkJars(ctx, sdkVersion, true)
Colin Cross79c7c262019-04-17 11:11:46 -07002939}
2940
Ulya Trafimovichdbf31662020-12-17 12:07:54 +00002941// to satisfy UsesLibraryDependency interface
Spandan Das59a4a2b2024-01-09 21:35:56 +00002942func (module *SdkLibraryImport) DexJarBuildPath(ctx android.ModuleErrorfContext) OptionalDexJarPath {
Paul Duffin39853512021-02-26 11:09:39 +00002943 // The dex implementation jar extracted from the .apex file should be used in preference to the
2944 // source.
Spandan Dasfae468e2023-12-12 23:23:53 +00002945 if module.dexJarFileErr != nil {
Spandan Das59a4a2b2024-01-09 21:35:56 +00002946 ctx.ModuleErrorf(module.dexJarFileErr.Error())
Spandan Dasfae468e2023-12-12 23:23:53 +00002947 }
Martin Stjernholm8be1e6d2021-09-15 03:34:04 +01002948 if module.dexJarFile.IsSet() {
Paul Duffin39853512021-02-26 11:09:39 +00002949 return module.dexJarFile
2950 }
Paul Duffineedc5d52020-06-12 17:46:39 +01002951 if module.implLibraryModule == nil {
Martin Stjernholm8be1e6d2021-09-15 03:34:04 +01002952 return makeUnsetDexJarPath()
Paul Duffineedc5d52020-06-12 17:46:39 +01002953 } else {
Spandan Das59a4a2b2024-01-09 21:35:56 +00002954 return module.implLibraryModule.DexJarBuildPath(ctx)
Paul Duffineedc5d52020-06-12 17:46:39 +01002955 }
2956}
2957
Ulya Trafimovichdbf31662020-12-17 12:07:54 +00002958// to satisfy UsesLibraryDependency interface
Ulya Trafimovich31e444e2020-08-14 17:32:16 +01002959func (module *SdkLibraryImport) DexJarInstallPath() android.Path {
Jeongik Chad5fe8782021-07-08 01:13:11 +09002960 return module.installFile
Ulya Trafimovich31e444e2020-08-14 17:32:16 +01002961}
2962
Ulya Trafimovichdbf31662020-12-17 12:07:54 +00002963// to satisfy UsesLibraryDependency interface
2964func (module *SdkLibraryImport) ClassLoaderContexts() dexpreopt.ClassLoaderContextMap {
2965 return nil
2966}
2967
Paul Duffineedc5d52020-06-12 17:46:39 +01002968// to satisfy apex.javaDependency interface
2969func (module *SdkLibraryImport) JacocoReportClassesFile() android.Path {
2970 if module.implLibraryModule == nil {
2971 return nil
2972 } else {
2973 return module.implLibraryModule.JacocoReportClassesFile()
2974 }
2975}
2976
2977// to satisfy apex.javaDependency interface
Colin Cross08dca382020-07-21 20:31:17 -07002978func (module *SdkLibraryImport) LintDepSets() LintDepSets {
2979 if module.implLibraryModule == nil {
2980 return LintDepSets{}
2981 } else {
2982 return module.implLibraryModule.LintDepSets()
2983 }
2984}
2985
Spandan Das17854f52022-01-14 21:19:14 +00002986func (module *SdkLibraryImport) GetStrictUpdatabilityLinting() bool {
Jaewoong Jung476b9d62021-05-10 15:30:00 -07002987 if module.implLibraryModule == nil {
2988 return false
2989 } else {
Spandan Das17854f52022-01-14 21:19:14 +00002990 return module.implLibraryModule.GetStrictUpdatabilityLinting()
Jaewoong Jung476b9d62021-05-10 15:30:00 -07002991 }
2992}
2993
Spandan Das17854f52022-01-14 21:19:14 +00002994func (module *SdkLibraryImport) SetStrictUpdatabilityLinting(strictLinting bool) {
Jaewoong Jung476b9d62021-05-10 15:30:00 -07002995 if module.implLibraryModule != nil {
Spandan Das17854f52022-01-14 21:19:14 +00002996 module.implLibraryModule.SetStrictUpdatabilityLinting(strictLinting)
Jaewoong Jung476b9d62021-05-10 15:30:00 -07002997 }
2998}
2999
Colin Cross08dca382020-07-21 20:31:17 -07003000// to satisfy apex.javaDependency interface
Paul Duffineedc5d52020-06-12 17:46:39 +01003001func (module *SdkLibraryImport) Stem() string {
3002 return module.BaseModuleName()
Colin Cross79c7c262019-04-17 11:11:46 -07003003}
Jiyong Parke3833882020-02-17 17:28:10 +09003004
Paul Duffin44b481b2020-06-17 16:59:43 +01003005var _ ApexDependency = (*SdkLibraryImport)(nil)
3006
3007// to satisfy java.ApexDependency interface
3008func (module *SdkLibraryImport) HeaderJars() android.Paths {
3009 if module.implLibraryModule == nil {
3010 return nil
3011 } else {
3012 return module.implLibraryModule.HeaderJars()
3013 }
3014}
3015
3016// to satisfy java.ApexDependency interface
3017func (module *SdkLibraryImport) ImplementationAndResourcesJars() android.Paths {
3018 if module.implLibraryModule == nil {
3019 return nil
3020 } else {
3021 return module.implLibraryModule.ImplementationAndResourcesJars()
3022 }
3023}
3024
Jiakai Zhang204356f2021-09-09 08:12:46 +00003025// to satisfy java.DexpreopterInterface interface
3026func (module *SdkLibraryImport) IsInstallable() bool {
3027 return true
3028}
3029
Paul Duffinfef55002021-06-17 14:56:05 +01003030var _ android.RequiredFilesFromPrebuiltApex = (*SdkLibraryImport)(nil)
3031
Paul Duffinb4bbf2c2021-06-17 15:59:07 +01003032func (module *SdkLibraryImport) RequiredFilesFromPrebuiltApex(ctx android.BaseModuleContext) []string {
Paul Duffinfef55002021-06-17 14:56:05 +01003033 name := module.BaseModuleName()
Jiakai Zhang81e46812023-02-08 21:56:07 +08003034 return requiredFilesFromPrebuiltApexForImport(name, &module.dexpreopter)
Paul Duffinfef55002021-06-17 14:56:05 +01003035}
3036
Spandan Das2ea84dd2024-01-25 22:12:50 +00003037func (j *SdkLibraryImport) UseProfileGuidedDexpreopt() bool {
3038 return proptools.Bool(j.importDexpreoptProperties.Dex_preopt.Profile_guided)
3039}
3040
Jiyong Parke3833882020-02-17 17:28:10 +09003041// java_sdk_library_xml
Jiyong Parke3833882020-02-17 17:28:10 +09003042type sdkLibraryXml struct {
3043 android.ModuleBase
3044 android.DefaultableModuleBase
3045 android.ApexModuleBase
3046
3047 properties sdkLibraryXmlProperties
3048
3049 outputFilePath android.OutputPath
3050 installDirPath android.InstallPath
Colin Cross56a83212020-09-15 18:30:11 -07003051
3052 hideApexVariantFromMake bool
Jiyong Parke3833882020-02-17 17:28:10 +09003053}
3054
3055type sdkLibraryXmlProperties struct {
3056 // canonical name of the lib
3057 Lib_name *string
Pedro Loureiro9956e5e2021-09-07 17:21:59 +00003058
3059 // Signals that this shared library is part of the bootclasspath starting
3060 // on the version indicated in this attribute.
3061 //
3062 // This will make platforms at this level and above to ignore
3063 // <uses-library> tags with this library name because the library is already
3064 // available
3065 On_bootclasspath_since *string
3066
3067 // Signals that this shared library was part of the bootclasspath before
3068 // (but not including) the version indicated in this attribute.
3069 //
3070 // The system will automatically add a <uses-library> tag with this library to
3071 // apps that target any SDK less than the version indicated in this attribute.
3072 On_bootclasspath_before *string
3073
3074 // Indicates that PackageManager should ignore this shared library if the
3075 // platform is below the version indicated in this attribute.
3076 //
3077 // This means that the device won't recognise this library as installed.
3078 Min_device_sdk *string
3079
3080 // Indicates that PackageManager should ignore this shared library if the
3081 // platform is above the version indicated in this attribute.
3082 //
3083 // This means that the device won't recognise this library as installed.
3084 Max_device_sdk *string
Pedro Loureiroc3621422021-09-28 15:40:23 +00003085
3086 // The SdkLibrary's min api level as a string
3087 //
3088 // This value comes from the ApiLevel of the MinSdkVersion property.
3089 Sdk_library_min_api_level *string
Jamie Garsidee570ace2023-11-27 12:07:36 +00003090
3091 // Uses-libs dependencies that the shared library requires to work correctly.
3092 //
3093 // This will add dependency="foo:bar" to the <library> section.
3094 Uses_libs_dependencies []string
Jiyong Parke3833882020-02-17 17:28:10 +09003095}
3096
3097// java_sdk_library_xml builds the permission xml file for a java_sdk_library.
3098// Not to be used directly by users. java_sdk_library internally uses this.
3099func sdkLibraryXmlFactory() android.Module {
3100 module := &sdkLibraryXml{}
3101
3102 module.AddProperties(&module.properties)
3103
3104 android.InitApexModule(module)
3105 android.InitAndroidArchModule(module, android.DeviceSupported, android.MultilibCommon)
3106
3107 return module
3108}
3109
Colin Crossaede88c2020-08-11 12:17:01 -07003110func (module *sdkLibraryXml) UniqueApexVariations() bool {
3111 // sdkLibraryXml needs a unique variation per APEX because the generated XML file contains the path to the
3112 // mounted APEX, which contains the name of the APEX.
3113 return true
3114}
3115
Jiyong Parke3833882020-02-17 17:28:10 +09003116// from android.PrebuiltEtcModule
Jooyung Han0703fd82020-08-26 22:11:53 +09003117func (module *sdkLibraryXml) BaseDir() string {
3118 return "etc"
3119}
3120
3121// from android.PrebuiltEtcModule
Jiyong Parke3833882020-02-17 17:28:10 +09003122func (module *sdkLibraryXml) SubDir() string {
3123 return "permissions"
3124}
3125
ThiƩbaud Weksteen00e8b312024-03-18 14:06:00 +11003126var _ etc.PrebuiltEtcModule = (*sdkLibraryXml)(nil)
3127
Jiyong Parke3833882020-02-17 17:28:10 +09003128// from android.ApexModule
3129func (module *sdkLibraryXml) AvailableFor(what string) bool {
3130 return true
3131}
3132
3133func (module *sdkLibraryXml) DepsMutator(ctx android.BottomUpMutatorContext) {
3134 // do nothing
3135}
3136
Jiyong Park45bf82e2020-12-15 22:29:02 +09003137var _ android.ApexModule = (*sdkLibraryXml)(nil)
3138
3139// Implements android.ApexModule
Dan Albertc8060532020-07-22 22:32:17 -07003140func (module *sdkLibraryXml) ShouldSupportSdkVersion(ctx android.BaseModuleContext,
3141 sdkVersion android.ApiLevel) error {
Jooyung Han749dc692020-04-15 11:03:39 +09003142 // sdkLibraryXml doesn't need to be checked separately because java_sdk_library is checked
3143 return nil
3144}
3145
Jiyong Parke3833882020-02-17 17:28:10 +09003146// File path to the runtime implementation library
Colin Cross56a83212020-09-15 18:30:11 -07003147func (module *sdkLibraryXml) implPath(ctx android.ModuleContext) string {
Jiyong Parke3833882020-02-17 17:28:10 +09003148 implName := proptools.String(module.properties.Lib_name)
Colin Crossff694a82023-12-13 15:54:49 -08003149 if apexInfo, _ := android.ModuleProvider(ctx, android.ApexInfoProvider); !apexInfo.IsForPlatform() {
Colin Crosse07f2312020-08-13 11:24:56 -07003150 // TODO(b/146468504): ApexVariationName() is only a soong module name, not apex name.
Jiyong Parke3833882020-02-17 17:28:10 +09003151 // In most cases, this works fine. But when apex_name is set or override_apex is used
3152 // this can be wrong.
Spandan Das33bbeb22024-06-18 23:28:25 +00003153 return fmt.Sprintf("/apex/%s/javalib/%s.jar", apexInfo.BaseApexName, implName)
Jiyong Parke3833882020-02-17 17:28:10 +09003154 }
3155 partition := "system"
3156 if module.SocSpecific() {
3157 partition = "vendor"
3158 } else if module.DeviceSpecific() {
3159 partition = "odm"
3160 } else if module.ProductSpecific() {
3161 partition = "product"
3162 } else if module.SystemExtSpecific() {
3163 partition = "system_ext"
3164 }
3165 return "/" + partition + "/framework/" + implName + ".jar"
3166}
3167
Pedro Loureiro9956e5e2021-09-07 17:21:59 +00003168func formattedOptionalSdkLevelAttribute(ctx android.ModuleContext, attrName string, value *string) string {
3169 if value == nil {
3170 return ""
3171 }
3172 apiLevel, err := android.ApiLevelFromUser(ctx, *value)
3173 if err != nil {
Pedro Loureiroba6682f2021-10-29 09:32:32 +00003174 // attributes in bp files have underscores but in the xml have dashes.
3175 ctx.PropertyErrorf(strings.ReplaceAll(attrName, "-", "_"), err.Error())
Pedro Loureiro9956e5e2021-09-07 17:21:59 +00003176 return ""
3177 }
Pedro Loureirob638c622021-12-22 15:28:05 +00003178 if apiLevel.IsCurrent() {
3179 // passing "current" would always mean a future release, never the current (or the current in
3180 // progress) which means some conditions would never be triggered.
3181 ctx.PropertyErrorf(strings.ReplaceAll(attrName, "-", "_"),
3182 `"current" is not an allowed value for this attribute`)
3183 return ""
3184 }
Pedro Loureiro48991222022-06-17 20:01:21 +00003185 // "safeValue" is safe because it translates finalized codenames to a string
3186 // with their SDK int.
3187 safeValue := apiLevel.String()
3188 return formattedOptionalAttribute(attrName, &safeValue)
Pedro Loureiro9956e5e2021-09-07 17:21:59 +00003189}
3190
3191// formats an attribute for the xml permissions file if the value is not null
3192// returns empty string otherwise
3193func formattedOptionalAttribute(attrName string, value *string) string {
3194 if value == nil {
3195 return ""
3196 }
Paul Duffin1816cde2024-04-10 10:58:21 +01003197 return fmt.Sprintf(" %s=\"%s\"\n", attrName, *value)
Pedro Loureiro9956e5e2021-09-07 17:21:59 +00003198}
3199
Jamie Garsidee570ace2023-11-27 12:07:36 +00003200func formattedDependenciesAttribute(dependencies []string) string {
3201 if dependencies == nil {
3202 return ""
3203 }
Paul Duffin1816cde2024-04-10 10:58:21 +01003204 return fmt.Sprintf(" dependency=\"%s\"\n", strings.Join(dependencies, ":"))
Jamie Garsidee570ace2023-11-27 12:07:36 +00003205}
3206
Pedro Loureiro9956e5e2021-09-07 17:21:59 +00003207func (module *sdkLibraryXml) permissionsContents(ctx android.ModuleContext) string {
3208 libName := proptools.String(module.properties.Lib_name)
3209 libNameAttr := formattedOptionalAttribute("name", &libName)
3210 filePath := module.implPath(ctx)
3211 filePathAttr := formattedOptionalAttribute("file", &filePath)
Pedro Loureiroba6682f2021-10-29 09:32:32 +00003212 implicitFromAttr := formattedOptionalSdkLevelAttribute(ctx, "on-bootclasspath-since", module.properties.On_bootclasspath_since)
3213 implicitUntilAttr := formattedOptionalSdkLevelAttribute(ctx, "on-bootclasspath-before", module.properties.On_bootclasspath_before)
3214 minSdkAttr := formattedOptionalSdkLevelAttribute(ctx, "min-device-sdk", module.properties.Min_device_sdk)
3215 maxSdkAttr := formattedOptionalSdkLevelAttribute(ctx, "max-device-sdk", module.properties.Max_device_sdk)
Jamie Garsidee570ace2023-11-27 12:07:36 +00003216 dependenciesAttr := formattedDependenciesAttribute(module.properties.Uses_libs_dependencies)
Pedro Loureiro196d3e62021-12-22 19:53:01 +00003217 // <library> is understood in all android versions whereas <apex-library> is only understood from API T (and ignored before that).
3218 // 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 +00003219 var libraryTag string
3220 if module.properties.Min_device_sdk != nil {
Paul Duffin1816cde2024-04-10 10:58:21 +01003221 libraryTag = " <apex-library\n"
Pedro Loureiroc3621422021-09-28 15:40:23 +00003222 } else {
Paul Duffin1816cde2024-04-10 10:58:21 +01003223 libraryTag = " <library\n"
Pedro Loureiroc3621422021-09-28 15:40:23 +00003224 }
Pedro Loureiro9956e5e2021-09-07 17:21:59 +00003225
3226 return strings.Join([]string{
Paul Duffin1816cde2024-04-10 10:58:21 +01003227 "<?xml version=\"1.0\" encoding=\"utf-8\"?>\n",
3228 "<!-- Copyright (C) 2018 The Android Open Source Project\n",
3229 "\n",
3230 " Licensed under the Apache License, Version 2.0 (the \"License\");\n",
3231 " you may not use this file except in compliance with the License.\n",
3232 " You may obtain a copy of the License at\n",
3233 "\n",
3234 " http://www.apache.org/licenses/LICENSE-2.0\n",
3235 "\n",
3236 " Unless required by applicable law or agreed to in writing, software\n",
3237 " distributed under the License is distributed on an \"AS IS\" BASIS,\n",
3238 " WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\n",
3239 " See the License for the specific language governing permissions and\n",
3240 " limitations under the License.\n",
3241 "-->\n",
3242 "<permissions>\n",
Pedro Loureiroc3621422021-09-28 15:40:23 +00003243 libraryTag,
Pedro Loureiro9956e5e2021-09-07 17:21:59 +00003244 libNameAttr,
3245 filePathAttr,
3246 implicitFromAttr,
3247 implicitUntilAttr,
3248 minSdkAttr,
3249 maxSdkAttr,
Jamie Garsidee570ace2023-11-27 12:07:36 +00003250 dependenciesAttr,
Paul Duffin1816cde2024-04-10 10:58:21 +01003251 " />\n",
3252 "</permissions>\n",
3253 }, "")
Pedro Loureiro9956e5e2021-09-07 17:21:59 +00003254}
3255
Jiyong Parke3833882020-02-17 17:28:10 +09003256func (module *sdkLibraryXml) GenerateAndroidBuildActions(ctx android.ModuleContext) {
Colin Crossff694a82023-12-13 15:54:49 -08003257 apexInfo, _ := android.ModuleProvider(ctx, android.ApexInfoProvider)
3258 module.hideApexVariantFromMake = !apexInfo.IsForPlatform()
Colin Cross56a83212020-09-15 18:30:11 -07003259
Jiyong Parke3833882020-02-17 17:28:10 +09003260 libName := proptools.String(module.properties.Lib_name)
Pedro Loureiroc3621422021-09-28 15:40:23 +00003261 module.selfValidate(ctx)
Pedro Loureiro9956e5e2021-09-07 17:21:59 +00003262 xmlContent := module.permissionsContents(ctx)
Jiyong Parke3833882020-02-17 17:28:10 +09003263
3264 module.outputFilePath = android.PathForModuleOut(ctx, libName+".xml").OutputPath
Paul Duffin1816cde2024-04-10 10:58:21 +01003265 android.WriteFileRuleVerbatim(ctx, module.outputFilePath, xmlContent)
Jiyong Parke3833882020-02-17 17:28:10 +09003266
3267 module.installDirPath = android.PathForModuleInstall(ctx, "etc", module.SubDir())
Jeongik Cha00e09912024-04-23 05:07:13 +09003268 ctx.PackageFile(module.installDirPath, libName+".xml", module.outputFilePath)
mrziwange2346b82024-06-10 15:09:45 -07003269
3270 ctx.SetOutputFiles(android.OutputPaths{module.outputFilePath}.Paths(), "")
Jiyong Parke3833882020-02-17 17:28:10 +09003271}
3272
3273func (module *sdkLibraryXml) AndroidMkEntries() []android.AndroidMkEntries {
Colin Cross56a83212020-09-15 18:30:11 -07003274 if module.hideApexVariantFromMake {
satayev8f088b02021-12-06 11:40:46 +00003275 return []android.AndroidMkEntries{{
Jiyong Parke3833882020-02-17 17:28:10 +09003276 Disabled: true,
3277 }}
3278 }
3279
satayev8f088b02021-12-06 11:40:46 +00003280 return []android.AndroidMkEntries{{
Jiyong Parke3833882020-02-17 17:28:10 +09003281 Class: "ETC",
3282 OutputFile: android.OptionalPathForPath(module.outputFilePath),
3283 ExtraEntries: []android.AndroidMkExtraEntriesFunc{
Colin Crossaa255532020-07-03 13:18:24 -07003284 func(ctx android.AndroidMkExtraEntriesContext, entries *android.AndroidMkEntries) {
Jiyong Parke3833882020-02-17 17:28:10 +09003285 entries.SetString("LOCAL_MODULE_TAGS", "optional")
Colin Crossc68db4b2021-11-11 18:59:15 -08003286 entries.SetString("LOCAL_MODULE_PATH", module.installDirPath.String())
Jiyong Parke3833882020-02-17 17:28:10 +09003287 entries.SetString("LOCAL_INSTALLED_MODULE_STEM", module.outputFilePath.Base())
3288 },
3289 },
3290 }}
3291}
Paul Duffindd46f712020-02-10 13:37:10 +00003292
Pedro Loureiroc3621422021-09-28 15:40:23 +00003293func (module *sdkLibraryXml) selfValidate(ctx android.ModuleContext) {
3294 module.validateAtLeastTAttributes(ctx)
3295 module.validateMinAndMaxDeviceSdk(ctx)
3296 module.validateMinMaxDeviceSdkAndModuleMinSdk(ctx)
3297 module.validateOnBootclasspathBeforeRequirements(ctx)
3298}
3299
3300func (module *sdkLibraryXml) validateAtLeastTAttributes(ctx android.ModuleContext) {
3301 t := android.ApiLevelOrPanic(ctx, "Tiramisu")
3302 module.attrAtLeastT(ctx, t, module.properties.Min_device_sdk, "min_device_sdk")
3303 module.attrAtLeastT(ctx, t, module.properties.Max_device_sdk, "max_device_sdk")
3304 module.attrAtLeastT(ctx, t, module.properties.On_bootclasspath_before, "on_bootclasspath_before")
3305 module.attrAtLeastT(ctx, t, module.properties.On_bootclasspath_since, "on_bootclasspath_since")
3306}
3307
3308func (module *sdkLibraryXml) attrAtLeastT(ctx android.ModuleContext, t android.ApiLevel, attr *string, attrName string) {
3309 if attr != nil {
3310 if level, err := android.ApiLevelFromUser(ctx, *attr); err == nil {
3311 // we will inform the user of invalid inputs when we try to write the
3312 // permissions xml file so we don't need to do it here
3313 if t.GreaterThan(level) {
3314 ctx.PropertyErrorf(attrName, "Attribute value needs to be at least T")
3315 }
3316 }
3317 }
3318}
3319
3320func (module *sdkLibraryXml) validateMinAndMaxDeviceSdk(ctx android.ModuleContext) {
3321 if module.properties.Min_device_sdk != nil && module.properties.Max_device_sdk != nil {
3322 min, minErr := android.ApiLevelFromUser(ctx, *module.properties.Min_device_sdk)
3323 max, maxErr := android.ApiLevelFromUser(ctx, *module.properties.Max_device_sdk)
3324 if minErr == nil && maxErr == nil {
3325 // we will inform the user of invalid inputs when we try to write the
3326 // permissions xml file so we don't need to do it here
3327 if min.GreaterThan(max) {
3328 ctx.ModuleErrorf("min_device_sdk can't be greater than max_device_sdk")
3329 }
3330 }
3331 }
3332}
3333
3334func (module *sdkLibraryXml) validateMinMaxDeviceSdkAndModuleMinSdk(ctx android.ModuleContext) {
3335 moduleMinApi := android.ApiLevelOrPanic(ctx, *module.properties.Sdk_library_min_api_level)
3336 if module.properties.Min_device_sdk != nil {
3337 api, err := android.ApiLevelFromUser(ctx, *module.properties.Min_device_sdk)
3338 if err == nil {
3339 if moduleMinApi.GreaterThan(api) {
3340 ctx.PropertyErrorf("min_device_sdk", "Can't be less than module's min sdk (%s)", moduleMinApi)
3341 }
3342 }
3343 }
3344 if module.properties.Max_device_sdk != nil {
3345 api, err := android.ApiLevelFromUser(ctx, *module.properties.Max_device_sdk)
3346 if err == nil {
3347 if moduleMinApi.GreaterThan(api) {
3348 ctx.PropertyErrorf("max_device_sdk", "Can't be less than module's min sdk (%s)", moduleMinApi)
3349 }
3350 }
3351 }
3352}
3353
3354func (module *sdkLibraryXml) validateOnBootclasspathBeforeRequirements(ctx android.ModuleContext) {
3355 moduleMinApi := android.ApiLevelOrPanic(ctx, *module.properties.Sdk_library_min_api_level)
3356 if module.properties.On_bootclasspath_before != nil {
3357 t := android.ApiLevelOrPanic(ctx, "Tiramisu")
3358 // if we use the attribute, then we need to do this validation
3359 if moduleMinApi.LessThan(t) {
3360 // if minAPi is < T, then we need to have min_device_sdk (which only accepts T+)
3361 if module.properties.Min_device_sdk == nil {
3362 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")
3363 }
3364 }
3365 }
3366}
3367
Paul Duffindd46f712020-02-10 13:37:10 +00003368type sdkLibrarySdkMemberType struct {
3369 android.SdkMemberTypeBase
3370}
3371
Paul Duffin296701e2021-07-14 10:29:36 +01003372func (s *sdkLibrarySdkMemberType) AddDependencies(ctx android.SdkDependencyContext, dependencyTag blueprint.DependencyTag, names []string) {
3373 ctx.AddVariationDependencies(nil, dependencyTag, names...)
Paul Duffindd46f712020-02-10 13:37:10 +00003374}
3375
3376func (s *sdkLibrarySdkMemberType) IsInstance(module android.Module) bool {
3377 _, ok := module.(*SdkLibrary)
3378 return ok
3379}
3380
3381func (s *sdkLibrarySdkMemberType) AddPrebuiltModule(ctx android.SdkMemberContext, member android.SdkMember) android.BpModule {
3382 return ctx.SnapshotBuilder().AddPrebuiltModule(member, "java_sdk_library_import")
3383}
3384
3385func (s *sdkLibrarySdkMemberType) CreateVariantPropertiesStruct() android.SdkMemberProperties {
3386 return &sdkLibrarySdkMemberProperties{}
3387}
3388
Paul Duffin976b0e52021-04-27 23:20:26 +01003389var javaSdkLibrarySdkMemberType = &sdkLibrarySdkMemberType{
3390 android.SdkMemberTypeBase{
3391 PropertyName: "java_sdk_libs",
3392 SupportsSdk: true,
3393 },
3394}
3395
Paul Duffindd46f712020-02-10 13:37:10 +00003396type sdkLibrarySdkMemberProperties struct {
3397 android.SdkMemberPropertiesBase
3398
Paul Duffine8409952022-09-22 16:24:46 +01003399 // Stem name for files in the sdk snapshot.
3400 //
3401 // This is used to construct the path names of various sdk library files in the sdk snapshot to
3402 // make sure that they match the finalized versions of those files in prebuilts/sdk.
3403 //
3404 // This property is marked as keep so that it will be kept in all instances of this struct, will
3405 // not be cleared but will be copied to common structs. That is needed because this field is used
3406 // to construct many file names for other parts of this struct and so it needs to be present in
3407 // all structs. If it was not marked as keep then it would be cleared in some structs and so would
3408 // be unavailable for generating file names if there were other properties that were still set.
3409 Stem string `sdk:"keep"`
3410
Paul Duffindd46f712020-02-10 13:37:10 +00003411 // Scope to per scope properties.
Paul Duffin106a3a42022-01-27 16:39:06 +00003412 Scopes map[*apiScope]*scopeProperties
Paul Duffindd46f712020-02-10 13:37:10 +00003413
Paul Duffin3d1248c2020-04-09 00:10:17 +01003414 // The Java stubs source files.
3415 Stub_srcs []string
Paul Duffinf7a64332020-05-13 16:54:55 +01003416
3417 // The naming scheme.
3418 Naming_scheme *string
Paul Duffind7eb1c22020-05-26 20:57:10 +01003419
3420 // True if the java_sdk_library_import is for a shared library, false
3421 // otherwise.
3422 Shared_library *bool
Paul Duffina2ae7e02020-09-11 11:55:00 +01003423
Paul Duffin1267d872021-04-16 17:21:36 +01003424 // True if the stub imports should produce dex jars.
3425 Compile_dex *bool
3426
Paul Duffina2ae7e02020-09-11 11:55:00 +01003427 // The paths to the doctag files to add to the prebuilt.
3428 Doctag_paths android.Paths
Paul Duffin869de142021-07-15 14:14:41 +01003429
3430 Permitted_packages []string
Pedro Loureiro9956e5e2021-09-07 17:21:59 +00003431
3432 // Signals that this shared library is part of the bootclasspath starting
3433 // on the version indicated in this attribute.
3434 //
3435 // This will make platforms at this level and above to ignore
3436 // <uses-library> tags with this library name because the library is already
3437 // available
3438 On_bootclasspath_since *string
3439
3440 // Signals that this shared library was part of the bootclasspath before
3441 // (but not including) the version indicated in this attribute.
3442 //
3443 // The system will automatically add a <uses-library> tag with this library to
3444 // apps that target any SDK less than the version indicated in this attribute.
3445 On_bootclasspath_before *string
3446
3447 // Indicates that PackageManager should ignore this shared library if the
3448 // platform is below the version indicated in this attribute.
3449 //
3450 // This means that the device won't recognise this library as installed.
3451 Min_device_sdk *string
3452
3453 // Indicates that PackageManager should ignore this shared library if the
3454 // platform is above the version indicated in this attribute.
3455 //
3456 // This means that the device won't recognise this library as installed.
3457 Max_device_sdk *string
Jiakai Zhang9c4dc192023-02-09 00:09:24 +08003458
3459 DexPreoptProfileGuided *bool `supported_build_releases:"UpsideDownCake+"`
Paul Duffindd46f712020-02-10 13:37:10 +00003460}
3461
3462type scopeProperties struct {
Paul Duffin1fd005d2020-04-09 01:08:11 +01003463 Jars android.Paths
3464 StubsSrcJar android.Path
3465 CurrentApiFile android.Path
3466 RemovedApiFile android.Path
Paul Duffine7babdb2022-02-10 13:06:54 +00003467 AnnotationsZip android.Path `supported_build_releases:"Tiramisu+"`
Paul Duffin1fd005d2020-04-09 01:08:11 +01003468 SdkVersion string
Paul Duffindd46f712020-02-10 13:37:10 +00003469}
3470
3471func (s *sdkLibrarySdkMemberProperties) PopulateFromVariant(ctx android.SdkMemberContext, variant android.Module) {
3472 sdk := variant.(*SdkLibrary)
3473
Paul Duffine8409952022-09-22 16:24:46 +01003474 // Copy the stem name for files in the sdk snapshot.
3475 s.Stem = sdk.distStem()
3476
Paul Duffin106a3a42022-01-27 16:39:06 +00003477 s.Scopes = make(map[*apiScope]*scopeProperties)
Jihoon Kang98aa8fa2024-06-07 11:06:57 +00003478 for _, apiScope := range AllApiScopes {
Paul Duffin803a9562020-05-20 11:52:25 +01003479 paths := sdk.findScopePaths(apiScope)
3480 if paths == nil {
3481 continue
3482 }
3483
Paul Duffindd46f712020-02-10 13:37:10 +00003484 jars := paths.stubsImplPath
3485 if len(jars) > 0 {
3486 properties := scopeProperties{}
3487 properties.Jars = jars
Paul Duffin780c5f42020-05-12 15:52:55 +01003488 properties.SdkVersion = sdk.sdkVersionForStubsLibrary(ctx.SdkModuleContext(), apiScope)
Paul Duffin0f8faff2020-05-20 16:18:00 +01003489 properties.StubsSrcJar = paths.stubsSrcJar.Path()
Paul Duffin10269f12020-06-19 18:39:55 +01003490 if paths.currentApiFilePath.Valid() {
3491 properties.CurrentApiFile = paths.currentApiFilePath.Path()
3492 }
3493 if paths.removedApiFilePath.Valid() {
3494 properties.RemovedApiFile = paths.removedApiFilePath.Path()
3495 }
Anton Hanssond78eb762021-09-21 15:25:12 +01003496 // The annotations zip is only available for modules that set annotations_enabled: true.
3497 if paths.annotationsZip.Valid() {
3498 properties.AnnotationsZip = paths.annotationsZip.Path()
3499 }
Paul Duffin106a3a42022-01-27 16:39:06 +00003500 s.Scopes[apiScope] = &properties
Paul Duffindd46f712020-02-10 13:37:10 +00003501 }
3502 }
3503
Paul Duffindfa131e2020-05-15 20:37:11 +01003504 s.Naming_scheme = sdk.commonSdkLibraryProperties.Naming_scheme
Paul Duffind7eb1c22020-05-26 20:57:10 +01003505 s.Shared_library = proptools.BoolPtr(sdk.sharedLibrary())
Paul Duffin1267d872021-04-16 17:21:36 +01003506 s.Compile_dex = sdk.dexProperties.Compile_dex
Paul Duffina2ae7e02020-09-11 11:55:00 +01003507 s.Doctag_paths = sdk.doctagPaths
Paul Duffin869de142021-07-15 14:14:41 +01003508 s.Permitted_packages = sdk.PermittedPackagesForUpdatableBootJars()
Pedro Loureiro9956e5e2021-09-07 17:21:59 +00003509 s.On_bootclasspath_since = sdk.commonSdkLibraryProperties.On_bootclasspath_since
3510 s.On_bootclasspath_before = sdk.commonSdkLibraryProperties.On_bootclasspath_before
3511 s.Min_device_sdk = sdk.commonSdkLibraryProperties.Min_device_sdk
3512 s.Max_device_sdk = sdk.commonSdkLibraryProperties.Max_device_sdk
Jiakai Zhang9c4dc192023-02-09 00:09:24 +08003513
Jihoon Kanga3a05462024-04-05 00:36:44 +00003514 implLibrary := sdk.getImplLibraryModule()
3515 if implLibrary != nil && implLibrary.dexpreopter.dexpreoptProperties.Dex_preopt_result.Profile_guided {
Jiakai Zhang9c4dc192023-02-09 00:09:24 +08003516 s.DexPreoptProfileGuided = proptools.BoolPtr(true)
3517 }
Paul Duffindd46f712020-02-10 13:37:10 +00003518}
3519
3520func (s *sdkLibrarySdkMemberProperties) AddToPropertySet(ctx android.SdkMemberContext, propertySet android.BpPropertySet) {
Paul Duffinf7a64332020-05-13 16:54:55 +01003521 if s.Naming_scheme != nil {
3522 propertySet.AddProperty("naming_scheme", proptools.String(s.Naming_scheme))
3523 }
Paul Duffind7eb1c22020-05-26 20:57:10 +01003524 if s.Shared_library != nil {
3525 propertySet.AddProperty("shared_library", *s.Shared_library)
3526 }
Paul Duffin1267d872021-04-16 17:21:36 +01003527 if s.Compile_dex != nil {
3528 propertySet.AddProperty("compile_dex", *s.Compile_dex)
3529 }
Paul Duffin869de142021-07-15 14:14:41 +01003530 if len(s.Permitted_packages) > 0 {
3531 propertySet.AddProperty("permitted_packages", s.Permitted_packages)
3532 }
Jiakai Zhang9c4dc192023-02-09 00:09:24 +08003533 dexPreoptSet := propertySet.AddPropertySet("dex_preopt")
3534 if s.DexPreoptProfileGuided != nil {
3535 dexPreoptSet.AddProperty("profile_guided", proptools.Bool(s.DexPreoptProfileGuided))
3536 }
Paul Duffinf7a64332020-05-13 16:54:55 +01003537
Paul Duffine8409952022-09-22 16:24:46 +01003538 stem := s.Stem
3539
Jihoon Kang98aa8fa2024-06-07 11:06:57 +00003540 for _, apiScope := range AllApiScopes {
Paul Duffindd46f712020-02-10 13:37:10 +00003541 if properties, ok := s.Scopes[apiScope]; ok {
Paul Duffin6b836ba2020-05-13 19:19:49 +01003542 scopeSet := propertySet.AddPropertySet(apiScope.propertyName)
Paul Duffindd46f712020-02-10 13:37:10 +00003543
Paul Duffin958806b2022-05-16 13:10:47 +00003544 scopeDir := apiScope.snapshotRelativeDir()
Paul Duffin3d1248c2020-04-09 00:10:17 +01003545
Paul Duffindd46f712020-02-10 13:37:10 +00003546 var jars []string
3547 for _, p := range properties.Jars {
Paul Duffine8409952022-09-22 16:24:46 +01003548 dest := filepath.Join(scopeDir, stem+"-stubs.jar")
Paul Duffindd46f712020-02-10 13:37:10 +00003549 ctx.SnapshotBuilder().CopyToSnapshot(p, dest)
3550 jars = append(jars, dest)
3551 }
3552 scopeSet.AddProperty("jars", jars)
3553
Paul Duffin22628d52021-05-12 23:13:22 +01003554 if ctx.SdkModuleContext().Config().IsEnvTrue("SOONG_SDK_SNAPSHOT_USE_SRCJAR") {
3555 // Copy the stubs source jar into the snapshot zip as is.
Paul Duffine8409952022-09-22 16:24:46 +01003556 srcJarSnapshotPath := filepath.Join(scopeDir, stem+".srcjar")
Paul Duffin22628d52021-05-12 23:13:22 +01003557 ctx.SnapshotBuilder().CopyToSnapshot(properties.StubsSrcJar, srcJarSnapshotPath)
3558 scopeSet.AddProperty("stub_srcs", []string{srcJarSnapshotPath})
3559 } else {
3560 // Merge the stubs source jar into the snapshot zip so that when it is unpacked
3561 // the source files are also unpacked.
Paul Duffine8409952022-09-22 16:24:46 +01003562 snapshotRelativeDir := filepath.Join(scopeDir, stem+"_stub_sources")
Paul Duffin22628d52021-05-12 23:13:22 +01003563 ctx.SnapshotBuilder().UnzipToSnapshot(properties.StubsSrcJar, snapshotRelativeDir)
3564 scopeSet.AddProperty("stub_srcs", []string{snapshotRelativeDir})
3565 }
Paul Duffin3d1248c2020-04-09 00:10:17 +01003566
Paul Duffin1fd005d2020-04-09 01:08:11 +01003567 if properties.CurrentApiFile != nil {
Paul Duffine8409952022-09-22 16:24:46 +01003568 currentApiSnapshotPath := apiScope.snapshotRelativeCurrentApiTxtPath(stem)
Paul Duffin1fd005d2020-04-09 01:08:11 +01003569 ctx.SnapshotBuilder().CopyToSnapshot(properties.CurrentApiFile, currentApiSnapshotPath)
3570 scopeSet.AddProperty("current_api", currentApiSnapshotPath)
3571 }
3572
3573 if properties.RemovedApiFile != nil {
Paul Duffine8409952022-09-22 16:24:46 +01003574 removedApiSnapshotPath := apiScope.snapshotRelativeRemovedApiTxtPath(stem)
Paul Duffin3dbf9fd2020-06-02 13:00:02 +01003575 ctx.SnapshotBuilder().CopyToSnapshot(properties.RemovedApiFile, removedApiSnapshotPath)
Paul Duffin1fd005d2020-04-09 01:08:11 +01003576 scopeSet.AddProperty("removed_api", removedApiSnapshotPath)
3577 }
3578
Anton Hanssond78eb762021-09-21 15:25:12 +01003579 if properties.AnnotationsZip != nil {
Paul Duffine8409952022-09-22 16:24:46 +01003580 annotationsSnapshotPath := filepath.Join(scopeDir, stem+"_annotations.zip")
Anton Hanssond78eb762021-09-21 15:25:12 +01003581 ctx.SnapshotBuilder().CopyToSnapshot(properties.AnnotationsZip, annotationsSnapshotPath)
3582 scopeSet.AddProperty("annotations", annotationsSnapshotPath)
3583 }
3584
Paul Duffindd46f712020-02-10 13:37:10 +00003585 if properties.SdkVersion != "" {
3586 scopeSet.AddProperty("sdk_version", properties.SdkVersion)
3587 }
3588 }
3589 }
3590
Paul Duffina2ae7e02020-09-11 11:55:00 +01003591 if len(s.Doctag_paths) > 0 {
3592 dests := []string{}
3593 for _, p := range s.Doctag_paths {
3594 dest := filepath.Join("doctags", p.Rel())
3595 ctx.SnapshotBuilder().CopyToSnapshot(p, dest)
3596 dests = append(dests, dest)
3597 }
3598 propertySet.AddProperty("doctag_files", dests)
3599 }
Paul Duffindd46f712020-02-10 13:37:10 +00003600}
Spandan Dasdee1a742024-08-09 17:37:25 +00003601
3602// 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 -07003603func (s *SdkLibrary) IDEInfo(ctx android.BaseModuleContext, dpInfo *android.IdeInfo) {
3604 s.Library.IDEInfo(ctx, dpInfo)
Spandan Dasdee1a742024-08-09 17:37:25 +00003605 if s.implLibraryModule != nil {
3606 dpInfo.Deps = append(dpInfo.Deps, s.implLibraryModule.Name())
3607 } else {
3608 // This java_sdk_library does not have an implementation (it sets `api_only` to true).
3609 // Examples of this are `art.module.intra.core.api` (IntraCore api surface).
3610 // Return the "public" stubs for these.
3611 stubPaths := s.findClosestScopePath(apiScopePublic)
3612 if len(stubPaths.stubsHeaderPath) > 0 {
3613 dpInfo.Jars = append(dpInfo.Jars, stubPaths.stubsHeaderPath[0].String())
3614 }
3615 }
3616}