blob: 7daaca753275d42567ed6b35123905f7dffe192b [file] [log] [blame]
Jiyong Parkc678ad32018-04-10 13:07:10 +09001// Copyright 2018 Google Inc. All rights reserved.
2//
3// Licensed under the Apache License, Version 2.0 (the "License");
4// you may not use this file except in compliance with the License.
5// You may obtain a copy of the License at
6//
7// http://www.apache.org/licenses/LICENSE-2.0
8//
9// Unless required by applicable law or agreed to in writing, software
10// distributed under the License is distributed on an "AS IS" BASIS,
11// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12// See the License for the specific language governing permissions and
13// limitations under the License.
14
15package java
16
17import (
Jihoon Kangee113282024-01-23 00:16:41 +000018 "errors"
Jiyong Parkc678ad32018-04-10 13:07:10 +090019 "fmt"
20 "path"
Sundong Ahn054b19a2018-10-19 13:46:09 +090021 "path/filepath"
Paul Duffin46a26a82020-04-07 19:27:04 +010022 "reflect"
Jiyong Park82484c02018-04-23 21:41:26 +090023 "sort"
Jiyong Parkc678ad32018-04-10 13:07:10 +090024 "strings"
Jiyong Park82484c02018-04-23 21:41:26 +090025 "sync"
Jiyong Parkc678ad32018-04-10 13:07:10 +090026
Paul Duffind1b3a922020-01-22 11:57:20 +000027 "github.com/google/blueprint"
Jiyong Parkc678ad32018-04-10 13:07:10 +090028 "github.com/google/blueprint/proptools"
Paul Duffin46a26a82020-04-07 19:27:04 +010029
30 "android/soong/android"
Ulya Trafimovichdbf31662020-12-17 12:07:54 +000031 "android/soong/dexpreopt"
ThiƩbaud Weksteen00e8b312024-03-18 14:06:00 +110032 "android/soong/etc"
Jiyong Parkc678ad32018-04-10 13:07:10 +090033)
34
Jooyung Han58f26ab2019-12-18 15:34:32 +090035const (
Pedro Loureiro9956e5e2021-09-07 17:21:59 +000036 sdkXmlFileSuffix = ".xml"
Jiyong Parkc678ad32018-04-10 13:07:10 +090037)
38
Paul Duffind1b3a922020-01-22 11:57:20 +000039// A tag to associated a dependency with a specific api scope.
40type scopeDependencyTag struct {
41 blueprint.BaseDependencyTag
42 name string
43 apiScope *apiScope
Paul Duffinc8782502020-04-29 20:45:27 +010044
45 // Function for extracting appropriate path information from the dependency.
Colin Crossdcf71b22021-02-01 13:59:03 -080046 depInfoExtractor func(paths *scopePaths, ctx android.ModuleContext, dep android.Module) error
Paul Duffinc8782502020-04-29 20:45:27 +010047}
48
49// Extract tag specific information from the dependency.
50func (tag scopeDependencyTag) extractDepInfo(ctx android.ModuleContext, dep android.Module, paths *scopePaths) {
Colin Crossdcf71b22021-02-01 13:59:03 -080051 err := tag.depInfoExtractor(paths, ctx, dep)
Paul Duffinc8782502020-04-29 20:45:27 +010052 if err != nil {
53 ctx.ModuleErrorf("has an invalid {scopeDependencyTag: %s} dependency on module %s: %s", tag.name, ctx.OtherModuleName(dep), err.Error())
54 }
Paul Duffind1b3a922020-01-22 11:57:20 +000055}
56
Paul Duffin80342d72020-06-26 22:08:43 +010057var _ android.ReplaceSourceWithPrebuilt = (*scopeDependencyTag)(nil)
58
59func (tag scopeDependencyTag) ReplaceSourceWithPrebuilt() bool {
60 return false
61}
62
Paul Duffind1b3a922020-01-22 11:57:20 +000063// Provides information about an api scope, e.g. public, system, test.
64type apiScope struct {
65 // The name of the api scope, e.g. public, system, test
66 name string
67
Paul Duffin97b53b82020-05-05 14:40:52 +010068 // The api scope that this scope extends.
Paul Duffind0b9fca2022-09-30 18:11:41 +010069 //
70 // This organizes the scopes into an extension hierarchy.
71 //
72 // If set this means that the API provided by this scope includes the API provided by the scope
73 // set in this field.
Paul Duffin97b53b82020-05-05 14:40:52 +010074 extends *apiScope
75
Paul Duffind0b9fca2022-09-30 18:11:41 +010076 // The next api scope that a library that uses this scope can access.
77 //
78 // This organizes the scopes into an access hierarchy.
79 //
80 // If set this means that a library that can access this API can also access the API provided by
81 // the scope set in this field.
82 //
83 // A module that sets sdk_version: "<scope>_current" should have access to the <scope> API of
84 // every java_sdk_library that it depends on. If the library does not provide an API for <scope>
85 // then it will traverse up this access hierarchy to find an API that it does provide.
86 //
87 // If this is not set then it defaults to the scope set in extends.
88 canAccess *apiScope
89
Paul Duffin3375e352020-04-28 10:44:03 +010090 // The legacy enabled status for a specific scope can be dependent on other
91 // properties that have been specified on the library so it is provided by
92 // a function that can determine the status by examining those properties.
93 legacyEnabledStatus func(module *SdkLibrary) bool
94
95 // The default enabled status for non-legacy behavior, which is triggered by
96 // explicitly enabling at least one api scope.
97 defaultEnabledStatus bool
98
99 // Gets a pointer to the scope specific properties.
100 scopeSpecificProperties func(module *SdkLibrary) *ApiScopeProperties
101
Paul Duffin46a26a82020-04-07 19:27:04 +0100102 // The name of the field in the dynamically created structure.
103 fieldName string
104
Paul Duffin6b836ba2020-05-13 19:19:49 +0100105 // The name of the property in the java_sdk_library_import
106 propertyName string
107
Jihoon Kangb7431552024-01-22 19:40:08 +0000108 // The tag to use to depend on the prebuilt stubs library module
109 prebuiltStubsTag scopeDependencyTag
Paul Duffind1b3a922020-01-22 11:57:20 +0000110
Jihoon Kangbd093452023-12-26 19:08:01 +0000111 // The tag to use to depend on the everything stubs library module.
112 everythingStubsTag scopeDependencyTag
113
114 // The tag to use to depend on the exportable stubs library module.
115 exportableStubsTag scopeDependencyTag
116
Paul Duffin0ff08bd2020-04-29 13:30:54 +0100117 // The tag to use to depend on the stubs source module (if separate from the API module).
118 stubsSourceTag scopeDependencyTag
119
Paul Duffinc8782502020-04-29 20:45:27 +0100120 // The tag to use to depend on the stubs source and API module.
121 stubsSourceAndApiTag scopeDependencyTag
Paul Duffind1b3a922020-01-22 11:57:20 +0000122
Paul Duffin958806b2022-05-16 13:10:47 +0000123 // The tag to use to depend on the module that provides the latest version of the API .txt file.
124 latestApiModuleTag scopeDependencyTag
125
126 // The tag to use to depend on the module that provides the latest version of the API removed.txt
127 // file.
128 latestRemovedApiModuleTag scopeDependencyTag
129
Paul Duffind1b3a922020-01-22 11:57:20 +0000130 // The scope specific prefix to add to the api file base of "current.txt" or "removed.txt".
131 apiFilePrefix string
132
Paul Duffind0b9fca2022-09-30 18:11:41 +0100133 // The scope specific suffix to add to the sdk library module name to construct a scope specific
Paul Duffind1b3a922020-01-22 11:57:20 +0000134 // module name.
135 moduleSuffix string
136
Paul Duffind1b3a922020-01-22 11:57:20 +0000137 // SDK version that the stubs library is built against. Note that this is always
138 // *current. Older stubs library built with a numbered SDK version is created from
139 // the prebuilt jar.
140 sdkVersion string
Paul Duffin1fb487d2020-04-07 18:50:10 +0100141
Paul Duffin15f34ef2020-07-20 18:04:44 +0100142 // The annotation that identifies this API level, empty for the public API scope.
143 annotation string
144
Paul Duffin1fb487d2020-04-07 18:50:10 +0100145 // Extra arguments to pass to droidstubs for this scope.
Paul Duffin0ff08bd2020-04-29 13:30:54 +0100146 //
Paul Duffin15f34ef2020-07-20 18:04:44 +0100147 // This is not used directly but is used to construct the droidstubsArgs.
148 extraArgs []string
Paul Duffin0ff08bd2020-04-29 13:30:54 +0100149
Paul Duffin15f34ef2020-07-20 18:04:44 +0100150 // The args that must be passed to droidstubs to generate the API and stubs source
151 // for this scope, constructed dynamically by initApiScope().
Paul Duffin0ff08bd2020-04-29 13:30:54 +0100152 //
153 // The API only includes the additional members that this scope adds over the scope
154 // that it extends.
Paul Duffin15f34ef2020-07-20 18:04:44 +0100155 //
156 // The stubs source must include the definitions of everything that is in this
157 // api scope and all the scopes that this one extends.
158 droidstubsArgs []string
Paul Duffin0ff08bd2020-04-29 13:30:54 +0100159
Anton Hansson6478ac12020-05-02 11:19:36 +0100160 // Whether the api scope can be treated as unstable, and should skip compat checks.
161 unstable bool
Jihoon Kang1c92c3e2023-03-23 17:44:51 +0000162
163 // Represents the SDK kind of this scope.
164 kind android.SdkKind
Paul Duffind1b3a922020-01-22 11:57:20 +0000165}
166
167// Initialize a scope, creating and adding appropriate dependency tags
168func initApiScope(scope *apiScope) *apiScope {
Paul Duffinc8782502020-04-29 20:45:27 +0100169 name := scope.name
Paul Duffin46dc45a2020-05-14 15:39:10 +0100170 scopeByName[name] = scope
171 allScopeNames = append(allScopeNames, name)
Paul Duffin6b836ba2020-05-13 19:19:49 +0100172 scope.propertyName = strings.ReplaceAll(name, "-", "_")
173 scope.fieldName = proptools.FieldNameForProperty(scope.propertyName)
Jihoon Kangb7431552024-01-22 19:40:08 +0000174 scope.prebuiltStubsTag = scopeDependencyTag{
Paul Duffinc8782502020-04-29 20:45:27 +0100175 name: name + "-stubs",
176 apiScope: scope,
177 depInfoExtractor: (*scopePaths).extractStubsLibraryInfoFromDependency,
Paul Duffind1b3a922020-01-22 11:57:20 +0000178 }
Jihoon Kangbd093452023-12-26 19:08:01 +0000179 scope.everythingStubsTag = scopeDependencyTag{
180 name: name + "-stubs-everything",
181 apiScope: scope,
182 depInfoExtractor: (*scopePaths).extractEverythingStubsLibraryInfoFromDependency,
183 }
184 scope.exportableStubsTag = scopeDependencyTag{
185 name: name + "-stubs-exportable",
186 apiScope: scope,
187 depInfoExtractor: (*scopePaths).extractExportableStubsLibraryInfoFromDependency,
188 }
Paul Duffin0ff08bd2020-04-29 13:30:54 +0100189 scope.stubsSourceTag = scopeDependencyTag{
190 name: name + "-stubs-source",
191 apiScope: scope,
192 depInfoExtractor: (*scopePaths).extractStubsSourceInfoFromDep,
193 }
Paul Duffinc8782502020-04-29 20:45:27 +0100194 scope.stubsSourceAndApiTag = scopeDependencyTag{
195 name: name + "-stubs-source-and-api",
196 apiScope: scope,
197 depInfoExtractor: (*scopePaths).extractStubsSourceAndApiInfoFromApiStubsProvider,
Paul Duffind1b3a922020-01-22 11:57:20 +0000198 }
Paul Duffin958806b2022-05-16 13:10:47 +0000199 scope.latestApiModuleTag = scopeDependencyTag{
200 name: name + "-latest-api",
201 apiScope: scope,
202 depInfoExtractor: (*scopePaths).extractLatestApiPath,
203 }
204 scope.latestRemovedApiModuleTag = scopeDependencyTag{
205 name: name + "-latest-removed-api",
206 apiScope: scope,
207 depInfoExtractor: (*scopePaths).extractLatestRemovedApiPath,
208 }
Paul Duffin0ff08bd2020-04-29 13:30:54 +0100209
210 // To get the args needed to generate the stubs source append all the args from
211 // this scope and all the scopes it extends as each set of args adds additional
212 // members to the stubs.
Paul Duffin15f34ef2020-07-20 18:04:44 +0100213 var scopeSpecificArgs []string
214 if scope.annotation != "" {
215 scopeSpecificArgs = []string{"--show-annotation", scope.annotation}
Paul Duffin0ff08bd2020-04-29 13:30:54 +0100216 }
Paul Duffin15f34ef2020-07-20 18:04:44 +0100217 for s := scope; s != nil; s = s.extends {
218 scopeSpecificArgs = append(scopeSpecificArgs, s.extraArgs...)
Paul Duffin0ff08bd2020-04-29 13:30:54 +0100219
Paul Duffin15f34ef2020-07-20 18:04:44 +0100220 // Ensure that the generated stubs includes all the API elements from the API scope
221 // that this scope extends.
222 if s != scope && s.annotation != "" {
223 scopeSpecificArgs = append(scopeSpecificArgs, "--show-for-stub-purposes-annotation", s.annotation)
224 }
225 }
Paul Duffin0ff08bd2020-04-29 13:30:54 +0100226
Paul Duffind0b9fca2022-09-30 18:11:41 +0100227 // By default, a library that can access a scope can also access the scope it extends.
228 if scope.canAccess == nil {
229 scope.canAccess = scope.extends
230 }
231
Paul Duffin15f34ef2020-07-20 18:04:44 +0100232 // Escape any special characters in the arguments. This is needed because droidstubs
233 // passes these directly to the shell command.
234 scope.droidstubsArgs = proptools.ShellEscapeList(scopeSpecificArgs)
Paul Duffin0ff08bd2020-04-29 13:30:54 +0100235
Paul Duffind1b3a922020-01-22 11:57:20 +0000236 return scope
237}
238
Anton Hansson08f476b2021-04-07 15:32:19 +0100239func (scope *apiScope) stubsLibraryModuleNameSuffix() string {
240 return ".stubs" + scope.moduleSuffix
241}
242
Jihoon Kangfa4a90d2023-12-20 02:53:38 +0000243func (scope *apiScope) exportableStubsLibraryModuleNameSuffix() string {
244 return ".stubs.exportable" + scope.moduleSuffix
245}
246
Jihoon Kang1c92c3e2023-03-23 17:44:51 +0000247func (scope *apiScope) apiLibraryModuleName(baseName string) string {
248 return scope.stubsLibraryModuleName(baseName) + ".from-text"
249}
250
Jihoon Kang2261a822024-09-12 00:01:54 +0000251func (scope *apiScope) sourceStubsLibraryModuleName(baseName string) string {
Jihoon Kang1147b312023-06-08 23:25:57 +0000252 return scope.stubsLibraryModuleName(baseName) + ".from-source"
253}
254
Jihoon Kangfa4a90d2023-12-20 02:53:38 +0000255func (scope *apiScope) exportableSourceStubsLibraryModuleName(baseName string) string {
256 return scope.exportableStubsLibraryModuleName(baseName) + ".from-source"
257}
258
Paul Duffinc3091c82020-05-08 14:16:20 +0100259func (scope *apiScope) stubsLibraryModuleName(baseName string) string {
Anton Hansson08f476b2021-04-07 15:32:19 +0100260 return baseName + scope.stubsLibraryModuleNameSuffix()
Paul Duffind1b3a922020-01-22 11:57:20 +0000261}
262
Jihoon Kangfa4a90d2023-12-20 02:53:38 +0000263func (scope *apiScope) exportableStubsLibraryModuleName(baseName string) string {
264 return baseName + scope.exportableStubsLibraryModuleNameSuffix()
265}
266
Paul Duffinc8782502020-04-29 20:45:27 +0100267func (scope *apiScope) stubsSourceModuleName(baseName string) string {
Paul Duffindd9d0742020-05-08 15:52:37 +0100268 return baseName + ".stubs.source" + scope.moduleSuffix
Paul Duffind1b3a922020-01-22 11:57:20 +0000269}
270
Paul 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 Duffindfa131e2020-05-15 20:37:11 +0100833 // Specifies whether this module can be used as an Android shared library; defaults
834 // to true.
835 //
836 // An Android shared library is one that can be referenced in a <uses-library> element
837 // in an AndroidManifest.xml.
838 Shared_library *bool
Paul Duffina2ae7e02020-09-11 11:55:00 +0100839
840 // Files containing information about supported java doc tags.
841 Doctag_files []string `android:"path"`
Pedro Loureiro9956e5e2021-09-07 17:21:59 +0000842
843 // Signals that this shared library is part of the bootclasspath starting
844 // on the version indicated in this attribute.
845 //
846 // This will make platforms at this level and above to ignore
847 // <uses-library> tags with this library name because the library is already
848 // available
849 On_bootclasspath_since *string
850
851 // Signals that this shared library was part of the bootclasspath before
852 // (but not including) the version indicated in this attribute.
853 //
854 // The system will automatically add a <uses-library> tag with this library to
855 // apps that target any SDK less than the version indicated in this attribute.
856 On_bootclasspath_before *string
857
858 // Indicates that PackageManager should ignore this shared library if the
859 // platform is below the version indicated in this attribute.
860 //
861 // This means that the device won't recognise this library as installed.
862 Min_device_sdk *string
863
864 // Indicates that PackageManager should ignore this shared library if the
865 // platform is above the version indicated in this attribute.
866 //
867 // This means that the device won't recognise this library as installed.
868 Max_device_sdk *string
Paul Duffin0ff08bd2020-04-29 13:30:54 +0100869}
870
Paul Duffin71b33cc2021-06-23 11:39:47 +0100871// commonSdkLibraryAndImportModule defines the interface that must be provided by a module that
872// embeds the commonToSdkLibraryAndImport struct.
873type commonSdkLibraryAndImportModule interface {
Paul Duffind796f6f2022-11-23 23:06:05 +0000874 android.Module
Paul Duffin71b33cc2021-06-23 11:39:47 +0100875
Spandan Das23956d12024-01-19 00:22:22 +0000876 // Returns the name of the root java_sdk_library that creates the child stub libraries
877 // This is the `name` as it appears in Android.bp, and not the name in Soong's build graph
878 // (with the prebuilt_ prefix)
879 //
880 // e.g. in the following java_sdk_library_import
881 // java_sdk_library_import {
882 // name: "framework-foo.v1",
883 // source_module_name: "framework-foo",
884 // }
885 // the values returned by
886 // 1. Name(): prebuilt_framework-foo.v1 # unique
887 // 2. BaseModuleName(): framework-foo # the source
888 // 3. RootLibraryName: framework-foo.v1 # the undecordated `name` from Android.bp
889 RootLibraryName() string
890}
891
892func (m *SdkLibrary) RootLibraryName() string {
893 return m.BaseModuleName()
894}
895
896func (m *SdkLibraryImport) RootLibraryName() string {
897 // m.BaseModuleName refers to the source of the import
898 // use moduleBase.Name to get the name of the module as it appears in the .bp file
899 return m.ModuleBase.Name()
Paul Duffin71b33cc2021-06-23 11:39:47 +0100900}
901
Paul Duffin56d44902020-01-31 13:36:25 +0000902// Common code between sdk library and sdk library import
903type commonToSdkLibraryAndImport struct {
Paul Duffin71b33cc2021-06-23 11:39:47 +0100904 module commonSdkLibraryAndImportModule
Paul Duffinc3091c82020-05-08 14:16:20 +0100905
Paul Duffin56d44902020-01-31 13:36:25 +0000906 scopePaths map[*apiScope]*scopePaths
Paul Duffin1b1e8062020-05-08 13:44:43 +0100907
Paul Duffindfa131e2020-05-15 20:37:11 +0100908 commonSdkLibraryProperties commonToSdkLibraryAndImportProperties
Paul Duffin859fe962020-05-15 10:20:31 +0100909
Paul Duffina2ae7e02020-09-11 11:55:00 +0100910 // Paths to commonSdkLibraryProperties.Doctag_files
911 doctagPaths android.Paths
912
Paul Duffin859fe962020-05-15 10:20:31 +0100913 // Functionality related to this being used as a component of a java_sdk_library.
914 EmbeddableSdkLibraryComponent
Jihoon Kang8479dea2024-04-04 01:19:05 +0000915
916 // Path to the header jars of the implementation library
917 // This is non-empty only when api_only is false.
918 implLibraryHeaderJars android.Paths
Jihoon Kanga3a05462024-04-05 00:36:44 +0000919
920 // The reference to the implementation library created by the source module.
921 // Is nil if the source module does not exist.
922 implLibraryModule *Library
Paul Duffin56d44902020-01-31 13:36:25 +0000923}
924
Paul Duffin71b33cc2021-06-23 11:39:47 +0100925func (c *commonToSdkLibraryAndImport) initCommon(module commonSdkLibraryAndImportModule) {
926 c.module = module
Paul Duffin1b1e8062020-05-08 13:44:43 +0100927
Paul Duffin71b33cc2021-06-23 11:39:47 +0100928 module.AddProperties(&c.commonSdkLibraryProperties)
Paul Duffin859fe962020-05-15 10:20:31 +0100929
930 // Initialize this as an sdk library component.
Paul Duffin71b33cc2021-06-23 11:39:47 +0100931 c.initSdkLibraryComponent(module)
Paul Duffin1b1e8062020-05-08 13:44:43 +0100932}
933
934func (c *commonToSdkLibraryAndImport) initCommonAfterDefaultsApplied(ctx android.DefaultableHookContext) bool {
Spandan Das23956d12024-01-19 00:22:22 +0000935 namePtr := proptools.StringPtr(c.module.RootLibraryName())
Paul Duffin3f0290e2021-06-30 18:25:36 +0100936 c.sdkLibraryComponentProperties.SdkLibraryName = namePtr
937
Paul Duffindfa131e2020-05-15 20:37:11 +0100938 // Only track this sdk library if this can be used as a shared library.
939 if c.sharedLibrary() {
940 // Use the name specified in the module definition as the owner.
Paul Duffin3f0290e2021-06-30 18:25:36 +0100941 c.sdkLibraryComponentProperties.SdkLibraryToImplicitlyTrack = namePtr
Paul Duffindfa131e2020-05-15 20:37:11 +0100942 }
Paul Duffin859fe962020-05-15 10:20:31 +0100943
Paul Duffin1b1e8062020-05-08 13:44:43 +0100944 return true
Paul Duffinc3091c82020-05-08 14:16:20 +0100945}
946
Paul Duffinea8f8082021-06-24 13:25:57 +0100947// uniqueApexVariations provides common implementation of the ApexModule.UniqueApexVariations
948// method.
949func (c *commonToSdkLibraryAndImport) uniqueApexVariations() bool {
950 // A java_sdk_library that is a shared library produces an XML file that makes the shared library
951 // usable from an AndroidManifest.xml's <uses-library> entry. That XML file contains the name of
952 // the APEX and so it needs a unique variation per APEX.
953 return c.sharedLibrary()
954}
955
Paul Duffina2ae7e02020-09-11 11:55:00 +0100956func (c *commonToSdkLibraryAndImport) generateCommonBuildActions(ctx android.ModuleContext) {
957 c.doctagPaths = android.PathsForModuleSrc(ctx, c.commonSdkLibraryProperties.Doctag_files)
958}
959
Jihoon Kanga3a05462024-04-05 00:36:44 +0000960func (c *commonToSdkLibraryAndImport) getImplLibraryModule() *Library {
961 return c.implLibraryModule
962}
963
Paul Duffineedc5d52020-06-12 17:46:39 +0100964// Module name of the runtime implementation library
965func (c *commonToSdkLibraryAndImport) implLibraryModuleName() string {
Spandan Das23956d12024-01-19 00:22:22 +0000966 return c.module.RootLibraryName() + ".impl"
Paul Duffineedc5d52020-06-12 17:46:39 +0100967}
968
969// Module name of the XML file for the lib
970func (c *commonToSdkLibraryAndImport) xmlPermissionsModuleName() string {
Spandan Das23956d12024-01-19 00:22:22 +0000971 return c.module.RootLibraryName() + sdkXmlFileSuffix
Paul Duffineedc5d52020-06-12 17:46:39 +0100972}
973
Paul Duffinc3091c82020-05-08 14:16:20 +0100974// Name of the java_library module that compiles the stubs source.
975func (c *commonToSdkLibraryAndImport) stubsLibraryModuleName(apiScope *apiScope) string {
Spandan Das23956d12024-01-19 00:22:22 +0000976 baseName := c.module.RootLibraryName()
Jihoon Kang2261a822024-09-12 00:01:54 +0000977 return apiScope.stubsLibraryModuleName(baseName)
Paul Duffinc3091c82020-05-08 14:16:20 +0100978}
979
Jihoon Kangfa4a90d2023-12-20 02:53:38 +0000980// Name of the java_library module that compiles the exportable stubs source.
981func (c *commonToSdkLibraryAndImport) exportableStubsLibraryModuleName(apiScope *apiScope) string {
Spandan Das23956d12024-01-19 00:22:22 +0000982 baseName := c.module.RootLibraryName()
Jihoon Kang2261a822024-09-12 00:01:54 +0000983 return apiScope.exportableStubsLibraryModuleName(baseName)
Jihoon Kangfa4a90d2023-12-20 02:53:38 +0000984}
985
Paul Duffinc3091c82020-05-08 14:16:20 +0100986// Name of the droidstubs module that generates the stubs source and may also
987// generate/check the API.
988func (c *commonToSdkLibraryAndImport) stubsSourceModuleName(apiScope *apiScope) string {
Spandan Das23956d12024-01-19 00:22:22 +0000989 baseName := c.module.RootLibraryName()
Jihoon Kang2261a822024-09-12 00:01:54 +0000990 return apiScope.stubsSourceModuleName(baseName)
Paul Duffinc3091c82020-05-08 14:16:20 +0100991}
992
Jihoon Kang1c92c3e2023-03-23 17:44:51 +0000993// Name of the java_api_library module that generates the from-text stubs source
994// and compiles to a jar file.
995func (c *commonToSdkLibraryAndImport) apiLibraryModuleName(apiScope *apiScope) string {
Spandan Das23956d12024-01-19 00:22:22 +0000996 baseName := c.module.RootLibraryName()
Jihoon Kang2261a822024-09-12 00:01:54 +0000997 return apiScope.apiLibraryModuleName(baseName)
Jihoon Kang1c92c3e2023-03-23 17:44:51 +0000998}
999
Jihoon Kang1147b312023-06-08 23:25:57 +00001000// Name of the java_library module that compiles the stubs
1001// generated from source Java files.
Jihoon Kangfa4a90d2023-12-20 02:53:38 +00001002func (c *commonToSdkLibraryAndImport) sourceStubsLibraryModuleName(apiScope *apiScope) string {
Spandan Das23956d12024-01-19 00:22:22 +00001003 baseName := c.module.RootLibraryName()
Jihoon Kang2261a822024-09-12 00:01:54 +00001004 return apiScope.sourceStubsLibraryModuleName(baseName)
Jihoon Kangfa4a90d2023-12-20 02:53:38 +00001005}
1006
1007// Name of the java_library module that compiles the exportable stubs
1008// generated from source Java files.
1009func (c *commonToSdkLibraryAndImport) exportableSourceStubsLibraryModuleName(apiScope *apiScope) string {
Spandan Das23956d12024-01-19 00:22:22 +00001010 baseName := c.module.RootLibraryName()
Jihoon Kang2261a822024-09-12 00:01:54 +00001011 return apiScope.exportableSourceStubsLibraryModuleName(baseName)
Jihoon Kang1147b312023-06-08 23:25:57 +00001012}
1013
Paul Duffin46dc45a2020-05-14 15:39:10 +01001014// The component names for different outputs of the java_sdk_library.
1015//
1016// They are similar to the names used for the child modules it creates
1017const (
1018 stubsSourceComponentName = "stubs.source"
1019
1020 apiTxtComponentName = "api.txt"
1021
1022 removedApiTxtComponentName = "removed-api.txt"
Anton Hanssond78eb762021-09-21 15:25:12 +01001023
1024 annotationsComponentName = "annotations.zip"
Paul Duffin46dc45a2020-05-14 15:39:10 +01001025)
1026
mrziwang9f7b9f42024-07-10 12:18:06 -07001027func (module *commonToSdkLibraryAndImport) setOutputFiles(ctx android.ModuleContext) {
1028 if module.doctagPaths != nil {
1029 ctx.SetOutputFiles(module.doctagPaths, ".doctags")
1030 }
1031 for _, scopeName := range android.SortedKeys(scopeByName) {
1032 paths := module.findScopePaths(scopeByName[scopeName])
1033 if paths == nil {
1034 continue
Paul Duffin46dc45a2020-05-14 15:39:10 +01001035 }
mrziwang9f7b9f42024-07-10 12:18:06 -07001036 componentToOutput := map[string]android.OptionalPath{
1037 stubsSourceComponentName: paths.stubsSrcJar,
1038 apiTxtComponentName: paths.currentApiFilePath,
1039 removedApiTxtComponentName: paths.removedApiFilePath,
1040 annotationsComponentName: paths.annotationsZip,
1041 }
1042 for _, component := range android.SortedKeys(componentToOutput) {
1043 if componentToOutput[component].Valid() {
1044 ctx.SetOutputFiles(android.Paths{componentToOutput[component].Path()}, "."+scopeName+"."+component)
Paul Duffina2ae7e02020-09-11 11:55:00 +01001045 }
1046 }
Paul Duffin46dc45a2020-05-14 15:39:10 +01001047 }
1048}
1049
Paul Duffin803a9562020-05-20 11:52:25 +01001050func (c *commonToSdkLibraryAndImport) getScopePathsCreateIfNeeded(scope *apiScope) *scopePaths {
Paul Duffin56d44902020-01-31 13:36:25 +00001051 if c.scopePaths == nil {
1052 c.scopePaths = make(map[*apiScope]*scopePaths)
1053 }
1054 paths := c.scopePaths[scope]
1055 if paths == nil {
1056 paths = &scopePaths{}
1057 c.scopePaths[scope] = paths
1058 }
1059
1060 return paths
1061}
1062
Paul Duffin803a9562020-05-20 11:52:25 +01001063func (c *commonToSdkLibraryAndImport) findScopePaths(scope *apiScope) *scopePaths {
1064 if c.scopePaths == nil {
1065 return nil
1066 }
1067
1068 return c.scopePaths[scope]
1069}
1070
1071// If this does not support the requested api scope then find the closest available
1072// scope it does support. Returns nil if no such scope is available.
1073func (c *commonToSdkLibraryAndImport) findClosestScopePath(scope *apiScope) *scopePaths {
Paul Duffind0b9fca2022-09-30 18:11:41 +01001074 for s := scope; s != nil; s = s.canAccess {
Paul Duffin803a9562020-05-20 11:52:25 +01001075 if paths := c.findScopePaths(s); paths != nil {
1076 return paths
1077 }
1078 }
1079
1080 // This should never happen outside tests as public should be the base scope for every
1081 // scope and is enabled by default.
1082 return nil
1083}
1084
Jiyong Parkf1691d22021-03-29 20:11:58 +09001085func (c *commonToSdkLibraryAndImport) selectHeaderJarsForSdkVersion(ctx android.BaseModuleContext, sdkVersion android.SdkSpec) android.Paths {
Paul Duffinb05d4292020-05-20 12:19:10 +01001086
1087 // If a specific numeric version has been requested then use prebuilt versions of the sdk.
Jiyong Park54105c42021-03-31 18:17:53 +09001088 if !sdkVersion.ApiLevel.IsPreview() {
Spandan Das23956d12024-01-19 00:22:22 +00001089 return PrebuiltJars(ctx, c.module.RootLibraryName(), sdkVersion)
Paul Duffinb05d4292020-05-20 12:19:10 +01001090 }
1091
Paul Duffin1267d872021-04-16 17:21:36 +01001092 paths := c.selectScopePaths(ctx, sdkVersion.Kind)
1093 if paths == nil {
1094 return nil
1095 }
1096
1097 return paths.stubsHeaderPath
1098}
1099
1100// selectScopePaths returns the *scopePaths appropriate for the specific kind.
1101//
1102// If the module does not support the specific kind then it will return the *scopePaths for the
1103// closest kind which is a subset of the requested kind. e.g. if requesting android.SdkModule then
1104// it will return *scopePaths for android.SdkSystem if available or android.SdkPublic of not.
1105func (c *commonToSdkLibraryAndImport) selectScopePaths(ctx android.BaseModuleContext, kind android.SdkKind) *scopePaths {
Paul Duffin32cf58a2021-05-18 16:32:50 +01001106 apiScope := sdkKindToApiScope(kind)
Paul Duffinb05d4292020-05-20 12:19:10 +01001107
Paul Duffin803a9562020-05-20 11:52:25 +01001108 paths := c.findClosestScopePath(apiScope)
1109 if paths == nil {
1110 var scopes []string
Jihoon Kang98aa8fa2024-06-07 11:06:57 +00001111 for _, s := range AllApiScopes {
Paul Duffin803a9562020-05-20 11:52:25 +01001112 if c.findScopePaths(s) != nil {
1113 scopes = append(scopes, s.name)
1114 }
1115 }
Spandan Das23956d12024-01-19 00:22:22 +00001116 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 +01001117 return nil
1118 }
1119
Paul Duffin1267d872021-04-16 17:21:36 +01001120 return paths
1121}
1122
Paul Duffin32cf58a2021-05-18 16:32:50 +01001123// sdkKindToApiScope maps from android.SdkKind to apiScope.
1124func sdkKindToApiScope(kind android.SdkKind) *apiScope {
1125 var apiScope *apiScope
1126 switch kind {
1127 case android.SdkSystem:
1128 apiScope = apiScopeSystem
1129 case android.SdkModule:
1130 apiScope = apiScopeModuleLib
1131 case android.SdkTest:
1132 apiScope = apiScopeTest
1133 case android.SdkSystemServer:
1134 apiScope = apiScopeSystemServer
1135 default:
1136 apiScope = apiScopePublic
1137 }
1138 return apiScope
1139}
1140
Paul Duffin1267d872021-04-16 17:21:36 +01001141// to satisfy SdkLibraryDependency interface
Martin Stjernholm8be1e6d2021-09-15 03:34:04 +01001142func (c *commonToSdkLibraryAndImport) SdkApiStubDexJar(ctx android.BaseModuleContext, kind android.SdkKind) OptionalDexJarPath {
Paul Duffin1267d872021-04-16 17:21:36 +01001143 paths := c.selectScopePaths(ctx, kind)
1144 if paths == nil {
Martin Stjernholm8be1e6d2021-09-15 03:34:04 +01001145 return makeUnsetDexJarPath()
Paul Duffin1267d872021-04-16 17:21:36 +01001146 }
1147
1148 return paths.stubsDexJarPath
Paul Duffinb05d4292020-05-20 12:19:10 +01001149}
1150
Paul Duffin32cf58a2021-05-18 16:32:50 +01001151// to satisfy SdkLibraryDependency interface
Jihoon Kangbd093452023-12-26 19:08:01 +00001152func (c *commonToSdkLibraryAndImport) SdkApiExportableStubDexJar(ctx android.BaseModuleContext, kind android.SdkKind) OptionalDexJarPath {
1153 paths := c.selectScopePaths(ctx, kind)
1154 if paths == nil {
1155 return makeUnsetDexJarPath()
1156 }
1157
1158 return paths.exportableStubsDexJarPath
1159}
1160
1161// to satisfy SdkLibraryDependency interface
Paul Duffin32cf58a2021-05-18 16:32:50 +01001162func (c *commonToSdkLibraryAndImport) SdkRemovedTxtFile(ctx android.BaseModuleContext, kind android.SdkKind) android.OptionalPath {
1163 apiScope := sdkKindToApiScope(kind)
1164 paths := c.findScopePaths(apiScope)
1165 if paths == nil {
1166 return android.OptionalPath{}
1167 }
1168
1169 return paths.removedApiFilePath
1170}
1171
Paul Duffin859fe962020-05-15 10:20:31 +01001172func (c *commonToSdkLibraryAndImport) sdkComponentPropertiesForChildLibrary() interface{} {
1173 componentProps := &struct {
Paul Duffin3f0290e2021-06-30 18:25:36 +01001174 SdkLibraryName *string
Paul Duffin859fe962020-05-15 10:20:31 +01001175 SdkLibraryToImplicitlyTrack *string
Paul Duffindfa131e2020-05-15 20:37:11 +01001176 }{}
1177
Spandan Das23956d12024-01-19 00:22:22 +00001178 namePtr := proptools.StringPtr(c.module.RootLibraryName())
Paul Duffin3f0290e2021-06-30 18:25:36 +01001179 componentProps.SdkLibraryName = namePtr
1180
Paul Duffindfa131e2020-05-15 20:37:11 +01001181 if c.sharedLibrary() {
Paul Duffin859fe962020-05-15 10:20:31 +01001182 // Mark the stubs library as being components of this java_sdk_library so that
1183 // any app that includes code which depends (directly or indirectly) on the stubs
1184 // library will have the appropriate <uses-library> invocation inserted into its
1185 // manifest if necessary.
Paul Duffin3f0290e2021-06-30 18:25:36 +01001186 componentProps.SdkLibraryToImplicitlyTrack = namePtr
Paul Duffin859fe962020-05-15 10:20:31 +01001187 }
1188
1189 return componentProps
1190}
1191
Paul Duffindfa131e2020-05-15 20:37:11 +01001192func (c *commonToSdkLibraryAndImport) sharedLibrary() bool {
1193 return proptools.BoolDefault(c.commonSdkLibraryProperties.Shared_library, true)
1194}
1195
Paul Duffinf4600f62021-05-13 22:34:45 +01001196// Check if the stub libraries should be compiled for dex
1197func (c *commonToSdkLibraryAndImport) stubLibrariesCompiledForDex() bool {
1198 // Always compile the dex file files for the stub libraries if they will be used on the
1199 // bootclasspath.
1200 return !c.sharedLibrary()
1201}
1202
Paul Duffin859fe962020-05-15 10:20:31 +01001203// Properties related to the use of a module as an component of a java_sdk_library.
1204type SdkLibraryComponentProperties struct {
Paul Duffin3f0290e2021-06-30 18:25:36 +01001205 // The name of the java_sdk_library/_import module.
1206 SdkLibraryName *string `blueprint:"mutated"`
Paul Duffin859fe962020-05-15 10:20:31 +01001207
1208 // The name of the java_sdk_library/_import to add to a <uses-library> entry
1209 // in the AndroidManifest.xml of any Android app that includes code that references
1210 // this module. If not set then no java_sdk_library/_import is tracked.
1211 SdkLibraryToImplicitlyTrack *string `blueprint:"mutated"`
1212}
1213
1214// Structure to be embedded in a module struct that needs to support the
1215// SdkLibraryComponentDependency interface.
1216type EmbeddableSdkLibraryComponent struct {
1217 sdkLibraryComponentProperties SdkLibraryComponentProperties
1218}
1219
Paul Duffin71b33cc2021-06-23 11:39:47 +01001220func (e *EmbeddableSdkLibraryComponent) initSdkLibraryComponent(module android.Module) {
1221 module.AddProperties(&e.sdkLibraryComponentProperties)
Paul Duffin859fe962020-05-15 10:20:31 +01001222}
1223
1224// to satisfy SdkLibraryComponentDependency
Paul Duffin3f0290e2021-06-30 18:25:36 +01001225func (e *EmbeddableSdkLibraryComponent) SdkLibraryName() *string {
1226 return e.sdkLibraryComponentProperties.SdkLibraryName
1227}
1228
1229// to satisfy SdkLibraryComponentDependency
Ulya Trafimovich39b437b2020-09-23 16:42:35 +01001230func (e *EmbeddableSdkLibraryComponent) OptionalSdkLibraryImplementation() *string {
Ulya Trafimovich78645fb2021-07-16 15:29:25 +01001231 // For shared libraries, this is the same as the SDK library name. If a Java library or app
1232 // depends on a component library (e.g. a stub library) it still needs to know the name of the
1233 // run-time library and the corresponding module that provides the implementation. This name is
1234 // passed to manifest_fixer (to be added to AndroidManifest.xml) and added to CLC (to be used
1235 // in dexpreopt).
1236 //
1237 // For non-shared SDK (component or not) libraries this returns `nil`, as they are not
1238 // <uses-library> and should not be added to the manifest or to CLC.
Ulya Trafimovich39b437b2020-09-23 16:42:35 +01001239 return e.sdkLibraryComponentProperties.SdkLibraryToImplicitlyTrack
1240}
1241
Paul Duffin859fe962020-05-15 10:20:31 +01001242// Implemented by modules that are (or possibly could be) a component of a java_sdk_library
1243// (including the java_sdk_library) itself.
1244type SdkLibraryComponentDependency interface {
Ulya Trafimovich31e444e2020-08-14 17:32:16 +01001245 UsesLibraryDependency
1246
Paul Duffin3f0290e2021-06-30 18:25:36 +01001247 // SdkLibraryName returns the name of the java_sdk_library/_import module.
1248 SdkLibraryName() *string
1249
Ulya Trafimovich39b437b2020-09-23 16:42:35 +01001250 // The name of the implementation library for the optional SDK library or nil, if there isn't one.
1251 OptionalSdkLibraryImplementation() *string
Paul Duffin859fe962020-05-15 10:20:31 +01001252}
1253
1254// Make sure that all the module types that are components of java_sdk_library/_import
1255// and which can be referenced (directly or indirectly) from an android app implement
1256// the SdkLibraryComponentDependency interface.
1257var _ SdkLibraryComponentDependency = (*Library)(nil)
1258var _ SdkLibraryComponentDependency = (*Import)(nil)
1259var _ SdkLibraryComponentDependency = (*SdkLibrary)(nil)
Paul Duffineedc5d52020-06-12 17:46:39 +01001260var _ SdkLibraryComponentDependency = (*SdkLibraryImport)(nil)
Paul Duffin859fe962020-05-15 10:20:31 +01001261
Paul Duffin32cf58a2021-05-18 16:32:50 +01001262// Provides access to sdk_version related files, e.g. header and implementation jars.
Paul Duffin859fe962020-05-15 10:20:31 +01001263type SdkLibraryDependency interface {
1264 SdkLibraryComponentDependency
1265
1266 // Get the header jars appropriate for the supplied sdk_version.
1267 //
1268 // These are turbine generated jars so they only change if the externals of the
1269 // class changes but it does not contain and implementation or JavaDoc.
Jiyong Parkf1691d22021-03-29 20:11:58 +09001270 SdkHeaderJars(ctx android.BaseModuleContext, sdkVersion android.SdkSpec) android.Paths
Paul Duffin859fe962020-05-15 10:20:31 +01001271
Jihoon Kangbd093452023-12-26 19:08:01 +00001272 // SdkApiStubDexJar returns the dex jar for the stubs for the prebuilt
1273 // java_sdk_library_import module. It is needed by the hiddenapi processing tool which
1274 // processes dex files.
Martin Stjernholm8be1e6d2021-09-15 03:34:04 +01001275 SdkApiStubDexJar(ctx android.BaseModuleContext, kind android.SdkKind) OptionalDexJarPath
Paul Duffinf4600f62021-05-13 22:34:45 +01001276
Jihoon Kangbd093452023-12-26 19:08:01 +00001277 // SdkApiExportableStubDexJar returns the exportable dex jar for the stubs for
1278 // java_sdk_library module. It is needed by the hiddenapi processing tool which processes
1279 // dex files.
1280 SdkApiExportableStubDexJar(ctx android.BaseModuleContext, kind android.SdkKind) OptionalDexJarPath
1281
Paul Duffin32cf58a2021-05-18 16:32:50 +01001282 // SdkRemovedTxtFile returns the optional path to the removed.txt file for the specified sdk kind.
1283 SdkRemovedTxtFile(ctx android.BaseModuleContext, kind android.SdkKind) android.OptionalPath
1284
Paul Duffinf4600f62021-05-13 22:34:45 +01001285 // sharedLibrary returns true if this can be used as a shared library.
1286 sharedLibrary() bool
Jihoon Kanga3a05462024-04-05 00:36:44 +00001287
Jihoon Kang28c96572024-09-11 23:44:44 +00001288 // getImplLibraryModule returns the pointer to the implementation library submodule of this
1289 // sdk library.
Jihoon Kanga3a05462024-04-05 00:36:44 +00001290 getImplLibraryModule() *Library
Paul Duffin859fe962020-05-15 10:20:31 +01001291}
1292
Jihoon Kang28c96572024-09-11 23:44:44 +00001293type SdkLibraryInfo struct {
1294 // GeneratingLibs is the names of the library modules that this sdk library
1295 // generates. Note that this only includes the name of the modules that other modules can
1296 // depend on, and is not a holistic list of generated modules.
1297 GeneratingLibs []string
1298}
1299
1300var SdkLibraryInfoProvider = blueprint.NewProvider[SdkLibraryInfo]()
1301
1302func getGeneratingLibs(ctx android.ModuleContext, sdkVersion android.SdkSpec, sdkLibraryModuleName string, sdkInfo SdkLibraryInfo) []string {
1303 apiLevel := sdkVersion.ApiLevel
1304 if apiLevel.IsPreview() {
1305 return sdkInfo.GeneratingLibs
1306 }
1307
1308 generatingPrebuilts := []string{}
1309 for _, apiScope := range AllApiScopes {
1310 scopePrebuiltModuleName := prebuiltApiModuleName("sdk", sdkLibraryModuleName, apiScope.name, apiLevel.String())
1311 if ctx.OtherModuleExists(scopePrebuiltModuleName) {
1312 generatingPrebuilts = append(generatingPrebuilts, scopePrebuiltModuleName)
1313 }
1314 }
1315 return generatingPrebuilts
1316}
1317
Inseob Kimc0907f12019-02-08 21:00:45 +09001318type SdkLibrary struct {
Sundong Ahn054b19a2018-10-19 13:46:09 +09001319 Library
Jiyong Parkc678ad32018-04-10 13:07:10 +09001320
Sundong Ahn054b19a2018-10-19 13:46:09 +09001321 sdkLibraryProperties sdkLibraryProperties
Jiyong Parkc678ad32018-04-10 13:07:10 +09001322
Paul Duffin3375e352020-04-28 10:44:03 +01001323 // Map from api scope to the scope specific property structure.
1324 scopeToProperties map[*apiScope]*ApiScopeProperties
1325
Paul Duffin56d44902020-01-31 13:36:25 +00001326 commonToSdkLibraryAndImport
Jihoon Kanga3a05462024-04-05 00:36:44 +00001327
1328 builtInstalledForApex []dexpreopterInstall
Jiyong Parkc678ad32018-04-10 13:07:10 +09001329}
1330
Inseob Kimc0907f12019-02-08 21:00:45 +09001331var _ SdkLibraryDependency = (*SdkLibrary)(nil)
Colin Cross897d2ed2019-02-11 14:03:51 -08001332
Paul Duffin3375e352020-04-28 10:44:03 +01001333func (module *SdkLibrary) generateTestAndSystemScopesByDefault() bool {
1334 return module.sdkLibraryProperties.Generate_system_and_test_apis
1335}
1336
Jihoon Kanga3a05462024-04-05 00:36:44 +00001337func (module *SdkLibrary) DexJarBuildPath(ctx android.ModuleErrorfContext) OptionalDexJarPath {
1338 if module.implLibraryModule != nil {
1339 return module.implLibraryModule.DexJarBuildPath(ctx)
1340 }
1341 return makeUnsetDexJarPath()
1342}
1343
1344func (module *SdkLibrary) DexJarInstallPath() android.Path {
1345 if module.implLibraryModule != nil {
1346 return module.implLibraryModule.DexJarInstallPath()
1347 }
1348 return nil
1349}
1350
Paul Duffin3375e352020-04-28 10:44:03 +01001351func (module *SdkLibrary) getGeneratedApiScopes(ctx android.EarlyModuleContext) apiScopes {
1352 // Check to see if any scopes have been explicitly enabled. If any have then all
1353 // must be.
1354 anyScopesExplicitlyEnabled := false
Jihoon Kang98aa8fa2024-06-07 11:06:57 +00001355 for _, scope := range AllApiScopes {
Paul Duffin3375e352020-04-28 10:44:03 +01001356 scopeProperties := module.scopeToProperties[scope]
1357 if scopeProperties.Enabled != nil {
1358 anyScopesExplicitlyEnabled = true
1359 break
1360 }
Paul Duffind1b3a922020-01-22 11:57:20 +00001361 }
Paul Duffin3375e352020-04-28 10:44:03 +01001362
1363 var generatedScopes apiScopes
1364 enabledScopes := make(map[*apiScope]struct{})
Jihoon Kang98aa8fa2024-06-07 11:06:57 +00001365 for _, scope := range AllApiScopes {
Paul Duffin3375e352020-04-28 10:44:03 +01001366 scopeProperties := module.scopeToProperties[scope]
1367 // If any scopes are explicitly enabled then ignore the legacy enabled status.
1368 // This is to ensure that any new usages of this module type do not rely on legacy
1369 // behaviour.
1370 defaultEnabledStatus := false
1371 if anyScopesExplicitlyEnabled {
1372 defaultEnabledStatus = scope.defaultEnabledStatus
1373 } else {
1374 defaultEnabledStatus = scope.legacyEnabledStatus(module)
1375 }
1376 enabled := proptools.BoolDefault(scopeProperties.Enabled, defaultEnabledStatus)
1377 if enabled {
1378 enabledScopes[scope] = struct{}{}
1379 generatedScopes = append(generatedScopes, scope)
1380 }
1381 }
1382
1383 // Now check to make sure that any scope that is extended by an enabled scope is also
1384 // enabled.
Jihoon Kang98aa8fa2024-06-07 11:06:57 +00001385 for _, scope := range AllApiScopes {
Paul Duffin3375e352020-04-28 10:44:03 +01001386 if _, ok := enabledScopes[scope]; ok {
1387 extends := scope.extends
1388 if extends != nil {
1389 if _, ok := enabledScopes[extends]; !ok {
1390 ctx.ModuleErrorf("enabled api scope %q depends on disabled scope %q", scope, extends)
1391 }
1392 }
1393 }
1394 }
1395
1396 return generatedScopes
Paul Duffind1b3a922020-01-22 11:57:20 +00001397}
1398
satayev758968a2021-12-06 11:42:40 +00001399var _ android.ModuleWithMinSdkVersionCheck = (*SdkLibrary)(nil)
1400
satayev8f088b02021-12-06 11:40:46 +00001401func (module *SdkLibrary) CheckMinSdkVersion(ctx android.ModuleContext) {
Jihoon Kanga3a05462024-04-05 00:36:44 +00001402 CheckMinSdkVersion(ctx, &module.Library)
1403}
1404
1405func CheckMinSdkVersion(ctx android.ModuleContext, module *Library) {
Spandan Das8c9ae7e2023-03-03 21:20:36 +00001406 android.CheckMinSdkVersion(ctx, module.MinSdkVersion(ctx), func(c android.ModuleContext, do android.PayloadDepsCallback) {
satayev8f088b02021-12-06 11:40:46 +00001407 ctx.WalkDeps(func(child android.Module, parent android.Module) bool {
1408 isExternal := !module.depIsInSameApex(ctx, child)
1409 if am, ok := child.(android.ApexModule); ok {
1410 if !do(ctx, parent, am, isExternal) {
1411 return false
1412 }
1413 }
1414 return !isExternal
1415 })
1416 })
1417}
1418
Paul Duffineedc5d52020-06-12 17:46:39 +01001419type sdkLibraryComponentTag struct {
1420 blueprint.BaseDependencyTag
1421 name string
1422}
1423
1424// Mark this tag so dependencies that use it are excluded from visibility enforcement.
1425func (t sdkLibraryComponentTag) ExcludeFromVisibilityEnforcement() {}
1426
1427var xmlPermissionsFileTag = sdkLibraryComponentTag{name: "xml-permissions-file"}
Paul Duffine74ac732020-02-06 13:51:46 +00001428
Jiyong Parke3833882020-02-17 17:28:10 +09001429func IsXmlPermissionsFileDepTag(depTag blueprint.DependencyTag) bool {
Paul Duffineedc5d52020-06-12 17:46:39 +01001430 if dt, ok := depTag.(sdkLibraryComponentTag); ok {
Jiyong Parke3833882020-02-17 17:28:10 +09001431 return dt == xmlPermissionsFileTag
1432 }
1433 return false
1434}
1435
Paul Duffineedc5d52020-06-12 17:46:39 +01001436var implLibraryTag = sdkLibraryComponentTag{name: "impl-library"}
Paul Duffin5df79302020-05-16 15:52:12 +01001437
Jeongik Chaaaa6dcd2024-05-22 00:41:28 +09001438var _ android.InstallNeededDependencyTag = sdkLibraryComponentTag{}
1439
Jihoon Kang46d66de2024-05-22 22:42:39 +00001440// To satisfy the CopyDirectlyInAnyApexTag interface. Implementation library of the sdk library
1441// in an apex is considered to be directly in the apex, as if it was listed in java_libs.
1442func (t sdkLibraryComponentTag) CopyDirectlyInAnyApex() {}
1443
1444var _ android.CopyDirectlyInAnyApexTag = implLibraryTag
1445
Jeongik Chaaaa6dcd2024-05-22 00:41:28 +09001446func (t sdkLibraryComponentTag) InstallDepNeeded() bool {
1447 return t.name == "xml-permissions-file" || t.name == "impl-library"
1448}
1449
Paul Duffin44f1d842020-06-26 20:17:02 +01001450// Add the dependencies on the child modules in the component deps mutator.
1451func (module *SdkLibrary) ComponentDepsMutator(ctx android.BottomUpMutatorContext) {
Paul Duffin3375e352020-04-28 10:44:03 +01001452 for _, apiScope := range module.getGeneratedApiScopes(ctx) {
Paul Duffind1b3a922020-01-22 11:57:20 +00001453 // Add dependencies to the stubs library
Spandan Das877f39d2023-03-29 16:19:51 +00001454 stubModuleName := module.stubsLibraryModuleName(apiScope)
Jihoon Kangbd093452023-12-26 19:08:01 +00001455 ctx.AddVariationDependencies(nil, apiScope.everythingStubsTag, stubModuleName)
Jihoon Kang1147b312023-06-08 23:25:57 +00001456
Jihoon Kangbd093452023-12-26 19:08:01 +00001457 exportableStubModuleName := module.exportableStubsLibraryModuleName(apiScope)
1458 ctx.AddVariationDependencies(nil, apiScope.exportableStubsTag, exportableStubModuleName)
Paul Duffind1b3a922020-01-22 11:57:20 +00001459
Paul Duffin15f34ef2020-07-20 18:04:44 +01001460 // Add a dependency on the stubs source in order to access both stubs source and api information.
1461 ctx.AddVariationDependencies(nil, apiScope.stubsSourceAndApiTag, module.stubsSourceModuleName(apiScope))
Paul Duffin958806b2022-05-16 13:10:47 +00001462
1463 if module.compareAgainstLatestApi(apiScope) {
1464 // Add dependencies on the latest finalized version of the API .txt file.
1465 latestApiModuleName := module.latestApiModuleName(apiScope)
1466 ctx.AddDependency(module, apiScope.latestApiModuleTag, latestApiModuleName)
1467
1468 // Add dependencies on the latest finalized version of the remove API .txt file.
1469 latestRemovedApiModuleName := module.latestRemovedApiModuleName(apiScope)
1470 ctx.AddDependency(module, apiScope.latestRemovedApiModuleTag, latestRemovedApiModuleName)
1471 }
Sundong Ahn054b19a2018-10-19 13:46:09 +09001472 }
1473
Paul Duffindfa131e2020-05-15 20:37:11 +01001474 if module.requiresRuntimeImplementationLibrary() {
Paul Duffin5df79302020-05-16 15:52:12 +01001475 // Add dependency to the rule for generating the implementation library.
1476 ctx.AddDependency(module, implLibraryTag, module.implLibraryModuleName())
1477
Paul Duffindfa131e2020-05-15 20:37:11 +01001478 if module.sharedLibrary() {
1479 // Add dependency to the rule for generating the xml permissions file
Paul Duffineedc5d52020-06-12 17:46:39 +01001480 ctx.AddDependency(module, xmlPermissionsFileTag, module.xmlPermissionsModuleName())
Paul Duffindfa131e2020-05-15 20:37:11 +01001481 }
Paul Duffin44f1d842020-06-26 20:17:02 +01001482 }
1483}
Paul Duffine74ac732020-02-06 13:51:46 +00001484
Paul Duffin44f1d842020-06-26 20:17:02 +01001485// Add other dependencies as normal.
1486func (module *SdkLibrary) DepsMutator(ctx android.BottomUpMutatorContext) {
Jihoon Kange4a90172024-07-18 22:49:08 +00001487 // If the module does not create an implementation library or defaults to stubs,
1488 // mark the top level sdk library as stubs module as the module will provide stubs via
1489 // "magic" when listed as a dependency in the Android.bp files.
1490 notCreateImplLib := proptools.Bool(module.sdkLibraryProperties.Api_only)
1491 preferStubs := proptools.Bool(module.sdkLibraryProperties.Default_to_stubs)
1492 module.properties.Is_stubs_module = proptools.BoolPtr(notCreateImplLib || preferStubs)
1493
Anton Hanssone77fccc2021-01-20 16:52:41 +00001494 var missingApiModules []string
1495 for _, apiScope := range module.getGeneratedApiScopes(ctx) {
1496 if apiScope.unstable {
1497 continue
1498 }
Paul Duffin958806b2022-05-16 13:10:47 +00001499 if m := module.latestApiModuleName(apiScope); !ctx.OtherModuleExists(m) {
Anton Hanssone77fccc2021-01-20 16:52:41 +00001500 missingApiModules = append(missingApiModules, m)
1501 }
Paul Duffin958806b2022-05-16 13:10:47 +00001502 if m := module.latestRemovedApiModuleName(apiScope); !ctx.OtherModuleExists(m) {
Anton Hanssone77fccc2021-01-20 16:52:41 +00001503 missingApiModules = append(missingApiModules, m)
1504 }
Paul Duffin958806b2022-05-16 13:10:47 +00001505 if m := module.latestIncompatibilitiesModuleName(apiScope); !ctx.OtherModuleExists(m) {
Jaewoong Jung1a97ee02021-03-09 13:25:02 -08001506 missingApiModules = append(missingApiModules, m)
1507 }
Anton Hanssone77fccc2021-01-20 16:52:41 +00001508 }
1509 if len(missingApiModules) != 0 && !module.sdkLibraryProperties.Unsafe_ignore_missing_latest_api {
1510 m := module.Name() + " is missing tracking files for previously released library versions.\n"
1511 m += "You need to do one of the following:\n"
1512 m += "- Add `unsafe_ignore_missing_latest_api: true` to your blueprint (to disable compat tracking)\n"
1513 m += "- Add a set of prebuilt txt files representing the last released version of this library for compat checking.\n"
1514 m += " (the current set of API files can be used as a seed for this compatibility tracking\n"
1515 m += "\n"
1516 m += "The following filegroup modules are missing:\n "
1517 m += strings.Join(missingApiModules, "\n ") + "\n"
1518 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."
1519 ctx.ModuleErrorf(m)
1520 }
Jiyong Parkc678ad32018-04-10 13:07:10 +09001521}
1522
Inseob Kimc0907f12019-02-08 21:00:45 +09001523func (module *SdkLibrary) GenerateAndroidBuildActions(ctx android.ModuleContext) {
Spandan Das5ae65ee2024-04-16 22:03:26 +00001524 if disableSourceApexVariant(ctx) {
1525 // Prebuilts are active, do not create the installation rules for the source javalib.
1526 // Even though the source javalib is not used, we need to hide it to prevent duplicate installation rules.
1527 // TODO (b/331665856): Implement a principled solution for this.
1528 module.HideFromMake()
1529 }
satayev8f088b02021-12-06 11:40:46 +00001530
Paul Duffina2ae7e02020-09-11 11:55:00 +01001531 module.generateCommonBuildActions(ctx)
1532
Jihoon Kanga3a05462024-04-05 00:36:44 +00001533 module.stem = proptools.StringDefault(module.overridableProperties.Stem, ctx.ModuleName())
1534
1535 module.provideHiddenAPIPropertyInfo(ctx)
Sundong Ahn054b19a2018-10-19 13:46:09 +09001536
Paul Duffinb97b1572021-04-29 21:50:40 +01001537 // Collate the components exported by this module. All scope specific modules are exported but
1538 // the impl and xml component modules are not.
1539 exportedComponents := map[string]struct{}{}
1540
Sundong Ahn57368eb2018-07-06 11:20:23 +09001541 // Record the paths to the header jars of the library (stubs and impl).
Paul Duffind1b3a922020-01-22 11:57:20 +00001542 // When this java_sdk_library is depended upon from others via "libs" property,
Jiyong Parkc678ad32018-04-10 13:07:10 +09001543 // the recorded paths will be returned depending on the link type of the caller.
1544 ctx.VisitDirectDeps(func(to android.Module) {
Jiyong Parkc678ad32018-04-10 13:07:10 +09001545 tag := ctx.OtherModuleDependencyTag(to)
1546
Paul Duffinc8782502020-04-29 20:45:27 +01001547 // Extract information from any of the scope specific dependencies.
1548 if scopeTag, ok := tag.(scopeDependencyTag); ok {
1549 apiScope := scopeTag.apiScope
Paul Duffin803a9562020-05-20 11:52:25 +01001550 scopePaths := module.getScopePathsCreateIfNeeded(apiScope)
Paul Duffinc8782502020-04-29 20:45:27 +01001551
1552 // Extract information from the dependency. The exact information extracted
1553 // is determined by the nature of the dependency which is determined by the tag.
1554 scopeTag.extractDepInfo(ctx, to, scopePaths)
Paul Duffinb97b1572021-04-29 21:50:40 +01001555
1556 exportedComponents[ctx.OtherModuleName(to)] = struct{}{}
Jihoon Kang4b9220a2024-08-22 22:11:04 +00001557
1558 ctx.Phony(ctx.ModuleName(), scopePaths.stubsHeaderPath...)
Sundong Ahn20e998b2018-07-24 11:19:26 +09001559 }
Jihoon Kang8479dea2024-04-04 01:19:05 +00001560
1561 if tag == implLibraryTag {
1562 if dep, ok := android.OtherModuleProvider(ctx, to, JavaInfoProvider); ok {
1563 module.implLibraryHeaderJars = append(module.implLibraryHeaderJars, dep.HeaderJars...)
Jihoon Kanga3a05462024-04-05 00:36:44 +00001564 module.implLibraryModule = to.(*Library)
1565 android.SetProvider(ctx, JavaInfoProvider, dep)
Jihoon Kang8479dea2024-04-04 01:19:05 +00001566 }
1567 }
Jiyong Parkc678ad32018-04-10 13:07:10 +09001568 })
Paul Duffinb97b1572021-04-29 21:50:40 +01001569
Jihoon Kanga3a05462024-04-05 00:36:44 +00001570 apexInfo, _ := android.ModuleProvider(ctx, android.ApexInfoProvider)
1571 if !apexInfo.IsForPlatform() {
1572 module.hideApexVariantFromMake = true
1573 }
1574
1575 if module.implLibraryModule != nil {
1576 if ctx.Device() {
1577 module.classesJarPaths = android.Paths{module.implLibraryModule.implementationJarFile}
1578 module.bootDexJarPath = module.implLibraryModule.bootDexJarPath
1579 module.uncompressDexState = module.implLibraryModule.uncompressDexState
1580 module.active = module.implLibraryModule.active
1581 }
1582
1583 module.outputFile = module.implLibraryModule.outputFile
1584 module.dexJarFile = makeDexJarPathFromPath(module.implLibraryModule.dexJarFile.Path())
1585 module.headerJarFile = module.implLibraryModule.headerJarFile
1586 module.implementationAndResourcesJar = module.implLibraryModule.implementationAndResourcesJar
1587 module.builtInstalledForApex = module.implLibraryModule.builtInstalledForApex
1588 module.dexpreopter.configPath = module.implLibraryModule.dexpreopter.configPath
1589 module.dexpreopter.outputProfilePathOnHost = module.implLibraryModule.dexpreopter.outputProfilePathOnHost
1590
Jihoon Kang34155e32024-05-20 19:08:49 +00001591 // Properties required for Library.AndroidMkEntries
1592 module.logtagsSrcs = module.implLibraryModule.logtagsSrcs
1593 module.dexpreopter.builtInstalled = module.implLibraryModule.dexpreopter.builtInstalled
1594 module.jacocoReportClassesFile = module.implLibraryModule.jacocoReportClassesFile
1595 module.dexer.proguardDictionary = module.implLibraryModule.dexer.proguardDictionary
1596 module.dexer.proguardUsageZip = module.implLibraryModule.dexer.proguardUsageZip
1597 module.linter.reports = module.implLibraryModule.linter.reports
Jihoon Kang629e2a32024-06-25 20:47:49 +00001598 module.linter.outputs.depSets = module.implLibraryModule.LintDepSets()
Jihoon Kang34155e32024-05-20 19:08:49 +00001599
Jihoon Kanga3a05462024-04-05 00:36:44 +00001600 if !module.Host() {
1601 module.hostdexInstallFile = module.implLibraryModule.hostdexInstallFile
1602 }
1603
Colin Crossa6182ab2024-08-21 10:47:44 -07001604 if installFilesInfo, ok := android.OtherModuleProvider(ctx, module.implLibraryModule, android.InstallFilesProvider); ok {
1605 if installFilesInfo.CheckbuildTarget != nil {
1606 ctx.CheckbuildFile(installFilesInfo.CheckbuildTarget)
1607 }
1608 }
Jihoon Kanga3a05462024-04-05 00:36:44 +00001609 android.SetProvider(ctx, blueprint.SrcsFileProviderKey, blueprint.SrcsFileProviderData{SrcPaths: module.implLibraryModule.uniqueSrcFiles.Strings()})
1610 }
1611
Paul Duffinb97b1572021-04-29 21:50:40 +01001612 // Make the set of components exported by this module available for use elsewhere.
Cole Faust18994c72023-02-28 16:02:16 -08001613 exportedComponentInfo := android.ExportedComponentsInfo{Components: android.SortedKeys(exportedComponents)}
Colin Cross40213022023-12-13 15:19:49 -08001614 android.SetProvider(ctx, android.ExportedComponentsInfoProvider, exportedComponentInfo)
Paul Duffin958806b2022-05-16 13:10:47 +00001615
1616 // Provide additional information for inclusion in an sdk's generated .info file.
1617 additionalSdkInfo := map[string]interface{}{}
1618 additionalSdkInfo["dist_stem"] = module.distStem()
Paul Duffine8409952022-09-22 16:24:46 +01001619 baseModuleName := module.distStem()
Paul Duffin958806b2022-05-16 13:10:47 +00001620 scopes := map[string]interface{}{}
1621 additionalSdkInfo["scopes"] = scopes
1622 for scope, scopePaths := range module.scopePaths {
1623 scopeInfo := map[string]interface{}{}
1624 scopes[scope.name] = scopeInfo
1625 scopeInfo["current_api"] = scope.snapshotRelativeCurrentApiTxtPath(baseModuleName)
1626 scopeInfo["removed_api"] = scope.snapshotRelativeRemovedApiTxtPath(baseModuleName)
Jihoon Kang5623e542024-01-31 23:27:26 +00001627 if p := scopePaths.latestApiPaths; len(p) > 0 {
1628 // The last path in the list is the one that applies to this scope, the
1629 // preceding ones, if any, are for the scope(s) that it extends.
1630 scopeInfo["latest_api"] = p[len(p)-1].String()
Paul Duffin958806b2022-05-16 13:10:47 +00001631 }
Jihoon Kang5623e542024-01-31 23:27:26 +00001632 if p := scopePaths.latestRemovedApiPaths; len(p) > 0 {
1633 // The last path in the list is the one that applies to this scope, the
1634 // preceding ones, if any, are for the scope(s) that it extends.
1635 scopeInfo["latest_removed_api"] = p[len(p)-1].String()
Paul Duffin958806b2022-05-16 13:10:47 +00001636 }
1637 }
Colin Cross40213022023-12-13 15:19:49 -08001638 android.SetProvider(ctx, android.AdditionalSdkInfoProvider, android.AdditionalSdkInfo{additionalSdkInfo})
mrziwang9f7b9f42024-07-10 12:18:06 -07001639 module.setOutputFiles(ctx)
Jihoon Kang28c96572024-09-11 23:44:44 +00001640
1641 var generatingLibs []string
1642 for _, apiScope := range AllApiScopes {
1643 if _, ok := module.scopePaths[apiScope]; ok {
1644 generatingLibs = append(generatingLibs, module.stubsLibraryModuleName(apiScope))
1645 }
1646 }
1647
mrziwang9f7b9f42024-07-10 12:18:06 -07001648 if module.requiresRuntimeImplementationLibrary() && module.implLibraryModule != nil {
Jihoon Kang28c96572024-09-11 23:44:44 +00001649 generatingLibs = append(generatingLibs, module.implLibraryModuleName())
mrziwang9f7b9f42024-07-10 12:18:06 -07001650 setOutputFiles(ctx, module.implLibraryModule.Module)
1651 }
Jihoon Kang28c96572024-09-11 23:44:44 +00001652
1653 android.SetProvider(ctx, SdkLibraryInfoProvider, SdkLibraryInfo{
1654 GeneratingLibs: generatingLibs,
1655 })
Jiyong Parkc678ad32018-04-10 13:07:10 +09001656}
1657
Jihoon Kanga3a05462024-04-05 00:36:44 +00001658func (module *SdkLibrary) BuiltInstalledForApex() []dexpreopterInstall {
1659 return module.builtInstalledForApex
1660}
1661
Jiyong Park0b0e1b92019-12-03 13:24:29 +09001662func (module *SdkLibrary) AndroidMkEntries() []android.AndroidMkEntries {
Paul Duffindfa131e2020-05-15 20:37:11 +01001663 if !module.requiresRuntimeImplementationLibrary() {
Paul Duffin43db9be2019-12-30 17:35:49 +00001664 return nil
1665 }
Jiyong Park0b0e1b92019-12-03 13:24:29 +09001666 entriesList := module.Library.AndroidMkEntries()
Jihoon Kanga3a05462024-04-05 00:36:44 +00001667 entries := &entriesList[0]
1668 entries.Required = append(entries.Required, module.implLibraryModuleName())
Yo Chiang07d75072020-06-05 17:43:19 +08001669 if module.sharedLibrary() {
Yo Chiang07d75072020-06-05 17:43:19 +08001670 entries.Required = append(entries.Required, module.xmlPermissionsModuleName())
1671 }
Jiyong Park0b0e1b92019-12-03 13:24:29 +09001672 return entriesList
Jiyong Park82484c02018-04-23 21:41:26 +09001673}
1674
Anton Hansson5fd5d242020-03-27 19:43:19 +00001675// The dist path of the stub artifacts
1676func (module *SdkLibrary) apiDistPath(apiScope *apiScope) string {
Colin Crossf0eace92021-06-02 13:02:23 -07001677 return path.Join("apistubs", module.distGroup(), apiScope.name)
Anton Hansson5fd5d242020-03-27 19:43:19 +00001678}
1679
Paul Duffin12ceb462019-12-24 20:31:31 +00001680// Get the sdk version for use when compiling the stubs library.
Paul Duffin780c5f42020-05-12 15:52:55 +01001681func (module *SdkLibrary) sdkVersionForStubsLibrary(mctx android.EarlyModuleContext, apiScope *apiScope) string {
Paul Duffin87a05a32020-05-12 11:50:28 +01001682 scopeProperties := module.scopeToProperties[apiScope]
1683 if scopeProperties.Sdk_version != nil {
1684 return proptools.String(scopeProperties.Sdk_version)
1685 }
1686
Jiyong Parkf1691d22021-03-29 20:11:58 +09001687 sdkDep := decodeSdkDep(mctx, android.SdkContext(&module.Library))
Paul Duffin12ceb462019-12-24 20:31:31 +00001688 if sdkDep.hasStandardLibs() {
1689 // If building against a standard sdk then use the sdk version appropriate for the scope.
Paul Duffind1b3a922020-01-22 11:57:20 +00001690 return apiScope.sdkVersion
Paul Duffin12ceb462019-12-24 20:31:31 +00001691 } else {
1692 // Otherwise, use no system module.
1693 return "none"
1694 }
1695}
1696
Paul Duffin31310252020-11-20 21:26:20 +00001697func (module *SdkLibrary) distStem() string {
1698 return proptools.StringDefault(module.sdkLibraryProperties.Dist_stem, module.BaseModuleName())
1699}
1700
Colin Cross986b69a2021-06-01 13:13:40 -07001701// distGroup returns the subdirectory of the dist path of the stub artifacts.
1702func (module *SdkLibrary) distGroup() string {
Colin Cross59b92bf2021-06-01 14:07:56 -07001703 return proptools.StringDefault(module.sdkLibraryProperties.Dist_group, "unknown")
Colin Cross986b69a2021-06-01 13:13:40 -07001704}
1705
Paul Duffin958806b2022-05-16 13:10:47 +00001706func latestPrebuiltApiModuleName(name string, apiScope *apiScope) string {
1707 return PrebuiltApiModuleName(name, apiScope.name, "latest")
1708}
1709
Jihoon Kang748a24d2024-03-20 21:29:39 +00001710func latestPrebuiltApiCombinedModuleName(name string, apiScope *apiScope) string {
1711 return PrebuiltApiCombinedModuleName(name, apiScope.name, "latest")
1712}
1713
Paul Duffind1b3a922020-01-22 11:57:20 +00001714func (module *SdkLibrary) latestApiFilegroupName(apiScope *apiScope) string {
Paul Duffin958806b2022-05-16 13:10:47 +00001715 return ":" + module.latestApiModuleName(apiScope)
1716}
1717
1718func (module *SdkLibrary) latestApiModuleName(apiScope *apiScope) string {
Jihoon Kang748a24d2024-03-20 21:29:39 +00001719 return latestPrebuiltApiCombinedModuleName(module.distStem(), apiScope)
Jiyong Park58c518b2018-05-12 22:29:12 +09001720}
Jiyong Parkc678ad32018-04-10 13:07:10 +09001721
Paul Duffind1b3a922020-01-22 11:57:20 +00001722func (module *SdkLibrary) latestRemovedApiFilegroupName(apiScope *apiScope) string {
Paul Duffin958806b2022-05-16 13:10:47 +00001723 return ":" + module.latestRemovedApiModuleName(apiScope)
1724}
1725
1726func (module *SdkLibrary) latestRemovedApiModuleName(apiScope *apiScope) string {
Jihoon Kang748a24d2024-03-20 21:29:39 +00001727 return latestPrebuiltApiCombinedModuleName(module.distStem()+"-removed", apiScope)
Jiyong Parkc678ad32018-04-10 13:07:10 +09001728}
1729
Jaewoong Jung1a97ee02021-03-09 13:25:02 -08001730func (module *SdkLibrary) latestIncompatibilitiesFilegroupName(apiScope *apiScope) string {
Paul Duffin958806b2022-05-16 13:10:47 +00001731 return ":" + module.latestIncompatibilitiesModuleName(apiScope)
1732}
1733
1734func (module *SdkLibrary) latestIncompatibilitiesModuleName(apiScope *apiScope) string {
1735 return latestPrebuiltApiModuleName(module.distStem()+"-incompatibilities", apiScope)
Jaewoong Jung1a97ee02021-03-09 13:25:02 -08001736}
1737
Jihoon Kang0c705a42023-08-02 06:44:57 +00001738// The listed modules' stubs contents do not match the corresponding txt files,
1739// but require additional api contributions to generate the full stubs.
1740// This method returns the name of the additional api contribution module
1741// for corresponding sdk_library modules.
1742func (module *SdkLibrary) apiLibraryAdditionalApiContribution() string {
1743 if val, ok := apiLibraryAdditionalProperties[module.Name()]; ok {
Jihoon Kangb0f4c022024-08-06 00:15:25 +00001744 return val
Jihoon Kang0c705a42023-08-02 06:44:57 +00001745 }
1746 return ""
1747}
1748
Anton Hansson944e77d2020-08-19 11:40:22 +01001749func childModuleVisibility(childVisibility []string) []string {
1750 if childVisibility == nil {
1751 // No child visibility set. The child will use the visibility of the sdk_library.
1752 return nil
1753 }
1754
1755 // Prepend an override to ignore the sdk_library's visibility, and rely on the child visibility.
1756 var visibility []string
1757 visibility = append(visibility, "//visibility:override")
1758 visibility = append(visibility, childVisibility...)
1759 return visibility
1760}
1761
Paul Duffin5df79302020-05-16 15:52:12 +01001762// Creates the implementation java library
1763func (module *SdkLibrary) createImplLibrary(mctx android.DefaultableHookContext) {
Anton Hansson944e77d2020-08-19 11:40:22 +01001764 visibility := childModuleVisibility(module.sdkLibraryProperties.Impl_library_visibility)
1765
Cole Faustb7493472024-08-28 11:55:52 -07001766 staticLibs := module.properties.Static_libs.Clone()
1767 staticLibs.AppendSimpleValue(module.sdkLibraryProperties.Impl_only_static_libs)
Paul Duffin5df79302020-05-16 15:52:12 +01001768 props := struct {
Paul Duffin77590a82022-04-28 14:13:30 +00001769 Name *string
Cole Faust8eeae4b2024-09-12 11:51:04 -07001770 Enabled proptools.Configurable[bool]
Paul Duffin77590a82022-04-28 14:13:30 +00001771 Visibility []string
Paul Duffin77590a82022-04-28 14:13:30 +00001772 Libs []string
Cole Faustb7493472024-08-28 11:55:52 -07001773 Static_libs proptools.Configurable[[]string]
Paul Duffin77590a82022-04-28 14:13:30 +00001774 Apex_available []string
Jihoon Kanga3a05462024-04-05 00:36:44 +00001775 Stem *string
Paul Duffin5df79302020-05-16 15:52:12 +01001776 }{
1777 Name: proptools.StringPtr(module.implLibraryModuleName()),
Cole Faust8eeae4b2024-09-12 11:51:04 -07001778 Enabled: module.EnabledProperty(),
Anton Hansson944e77d2020-08-19 11:40:22 +01001779 Visibility: visibility,
Jihoon Kanga3a05462024-04-05 00:36:44 +00001780
1781 Libs: append(module.properties.Libs, module.sdkLibraryProperties.Impl_only_libs...),
1782
Cole Faustb7493472024-08-28 11:55:52 -07001783 Static_libs: staticLibs,
Paul Duffin77590a82022-04-28 14:13:30 +00001784 // Pass the apex_available settings down so that the impl library can be statically
1785 // embedded within a library that is added to an APEX. Needed for updatable-media.
1786 Apex_available: module.ApexAvailable(),
Jihoon Kanga3a05462024-04-05 00:36:44 +00001787
1788 Stem: proptools.StringPtr(module.Name()),
Paul Duffin5df79302020-05-16 15:52:12 +01001789 }
1790
1791 properties := []interface{}{
1792 &module.properties,
1793 &module.protoProperties,
1794 &module.deviceProperties,
Liz Kammera7a64f32020-07-09 15:16:41 -07001795 &module.dexProperties,
Paul Duffin5df79302020-05-16 15:52:12 +01001796 &module.dexpreoptProperties,
Colin Cross014489c2020-06-02 20:09:13 -07001797 &module.linter.properties,
Spandan Dasb9c58352024-05-13 18:29:45 +00001798 &module.overridableProperties,
Paul Duffin5df79302020-05-16 15:52:12 +01001799 &props,
1800 module.sdkComponentPropertiesForChildLibrary(),
1801 }
1802 mctx.CreateModule(LibraryFactory, properties...)
1803}
1804
Jihoon Kangfa4a90d2023-12-20 02:53:38 +00001805type libraryProperties struct {
1806 Name *string
Cole Faust8eeae4b2024-09-12 11:51:04 -07001807 Enabled proptools.Configurable[bool]
Jihoon Kangfa4a90d2023-12-20 02:53:38 +00001808 Visibility []string
1809 Srcs []string
1810 Installable *bool
1811 Sdk_version *string
1812 System_modules *string
1813 Patch_module *string
1814 Libs []string
1815 Static_libs []string
1816 Compile_dex *bool
1817 Java_version *string
1818 Openjdk9 struct {
1819 Srcs []string
1820 Javacflags []string
1821 }
1822 Dist struct {
1823 Targets []string
1824 Dest *string
1825 Dir *string
1826 Tag *string
1827 }
Jihoon Kangfa3f0782024-08-21 20:42:18 +00001828 Is_stubs_module *bool
1829 Stub_contributing_api *string
Jihoon Kangfa4a90d2023-12-20 02:53:38 +00001830}
Jiyong Parkc678ad32018-04-10 13:07:10 +09001831
Jihoon Kangfa4a90d2023-12-20 02:53:38 +00001832func (module *SdkLibrary) stubsLibraryProps(mctx android.DefaultableHookContext, apiScope *apiScope) libraryProperties {
1833 props := libraryProperties{}
Cole Faust8eeae4b2024-09-12 11:51:04 -07001834 props.Enabled = module.EnabledProperty()
Jihoon Kang786df932023-09-07 01:18:31 +00001835 props.Visibility = []string{"//visibility:override", "//visibility:private"}
Jiyong Parkc678ad32018-04-10 13:07:10 +09001836 // sources are generated from the droiddoc
Paul Duffin12ceb462019-12-24 20:31:31 +00001837 sdkVersion := module.sdkVersionForStubsLibrary(mctx, apiScope)
Paul Duffin52d398a2019-06-11 12:31:14 +01001838 props.Sdk_version = proptools.StringPtr(sdkVersion)
Paul Duffina18abc22020-05-16 18:54:24 +01001839 props.System_modules = module.deviceProperties.System_modules
1840 props.Patch_module = module.properties.Patch_module
Paul Duffin367ab912019-12-23 19:40:36 +00001841 props.Installable = proptools.BoolPtr(false)
Sundong Ahn054b19a2018-10-19 13:46:09 +09001842 props.Libs = module.sdkLibraryProperties.Stub_only_libs
Mark White9421c4c2023-08-10 00:07:03 +00001843 props.Libs = append(props.Libs, module.scopeToProperties[apiScope].Libs...)
Anton Hanssondae54cd2021-04-21 16:30:10 +01001844 props.Static_libs = module.sdkLibraryProperties.Stub_only_static_libs
Paul Duffine22c2ab2020-05-20 19:35:27 +01001845 // The stub-annotations library contains special versions of the annotations
1846 // with CLASS retention policy, so that they're kept.
1847 if proptools.Bool(module.sdkLibraryProperties.Annotations_enabled) {
1848 props.Libs = append(props.Libs, "stub-annotations")
1849 }
Paul Duffina18abc22020-05-16 18:54:24 +01001850 props.Openjdk9.Srcs = module.properties.Openjdk9.Srcs
1851 props.Openjdk9.Javacflags = module.properties.Openjdk9.Javacflags
Anton Hansson83509b52020-05-21 09:21:57 +01001852 // We compile the stubs for 1.8 in line with the main android.jar stubs, and potential
1853 // interop with older developer tools that don't support 1.9.
1854 props.Java_version = proptools.StringPtr("1.8")
Jihoon Kangfe914ed2024-02-12 22:49:21 +00001855 props.Is_stubs_module = proptools.BoolPtr(true)
Jihoon Kangfa3f0782024-08-21 20:42:18 +00001856 props.Stub_contributing_api = proptools.StringPtr(apiScope.kind.String())
Paul Duffinf4600f62021-05-13 22:34:45 +01001857
Jihoon Kangfa4a90d2023-12-20 02:53:38 +00001858 return props
1859}
1860
1861// Creates a static java library that has API stubs
1862func (module *SdkLibrary) createStubsLibrary(mctx android.DefaultableHookContext, apiScope *apiScope) {
1863
1864 props := module.stubsLibraryProps(mctx, apiScope)
1865 props.Name = proptools.StringPtr(module.sourceStubsLibraryModuleName(apiScope))
1866 props.Srcs = []string{":" + module.stubsSourceModuleName(apiScope)}
1867
1868 mctx.CreateModule(LibraryFactory, &props, module.sdkComponentPropertiesForChildLibrary())
1869}
1870
1871// Create a static java library that compiles the "exportable" stubs
1872func (module *SdkLibrary) createExportableStubsLibrary(mctx android.DefaultableHookContext, apiScope *apiScope) {
1873 props := module.stubsLibraryProps(mctx, apiScope)
1874 props.Name = proptools.StringPtr(module.exportableSourceStubsLibraryModuleName(apiScope))
1875 props.Srcs = []string{":" + module.stubsSourceModuleName(apiScope) + "{.exportable}"}
1876
Paul Duffin859fe962020-05-15 10:20:31 +01001877 mctx.CreateModule(LibraryFactory, &props, module.sdkComponentPropertiesForChildLibrary())
Jiyong Parkc678ad32018-04-10 13:07:10 +09001878}
1879
Paul Duffin6d0886e2020-04-07 18:49:53 +01001880// Creates a droidstubs module that creates stubs source files from the given full source
Paul Duffinc8782502020-04-29 20:45:27 +01001881// files and also updates and checks the API specification files.
Paul Duffin15f34ef2020-07-20 18:04:44 +01001882func (module *SdkLibrary) createStubsSourcesAndApi(mctx android.DefaultableHookContext, apiScope *apiScope, name string, scopeSpecificDroidstubsArgs []string) {
Jiyong Parkc678ad32018-04-10 13:07:10 +09001883 props := struct {
Sundong Ahn054b19a2018-10-19 13:46:09 +09001884 Name *string
Cole Faust8eeae4b2024-09-12 11:51:04 -07001885 Enabled proptools.Configurable[bool]
Paul Duffin4911a892020-04-29 23:35:13 +01001886 Visibility []string
Sundong Ahn054b19a2018-10-19 13:46:09 +09001887 Srcs []string
1888 Installable *bool
Paul Duffin52d398a2019-06-11 12:31:14 +01001889 Sdk_version *string
Jihoon Kang3198f3c2023-01-26 08:08:52 +00001890 Api_surface *string
Paul Duffin12ceb462019-12-24 20:31:31 +00001891 System_modules *string
Cole Faustb7493472024-08-28 11:55:52 -07001892 Libs proptools.Configurable[[]string]
Paul Duffin6877e6d2020-09-25 19:59:14 +01001893 Output_javadoc_comments *bool
Paul Duffin11512472019-02-11 15:55:17 +00001894 Arg_files []string
Sundong Ahn054b19a2018-10-19 13:46:09 +09001895 Args *string
Sundong Ahn054b19a2018-10-19 13:46:09 +09001896 Java_version *string
Paul Duffine22c2ab2020-05-20 19:35:27 +01001897 Annotations_enabled *bool
Sundong Ahn054b19a2018-10-19 13:46:09 +09001898 Merge_annotations_dirs []string
1899 Merge_inclusion_annotations_dirs []string
Paul Duffin0ff08bd2020-04-29 13:30:54 +01001900 Generate_stubs *bool
Anton Hanssone87b03d2020-12-21 15:29:34 +00001901 Previous_api *string
Jihoon Kang6592e872023-12-19 01:13:16 +00001902 Aconfig_declarations []string
Sundong Ahn054b19a2018-10-19 13:46:09 +09001903 Check_api struct {
Anton Hanssone6056152020-12-31 10:37:27 +00001904 Current ApiToCheck
1905 Last_released ApiToCheck
Paul Duffin160fe412020-05-10 19:32:20 +01001906
1907 Api_lint struct {
1908 Enabled *bool
1909 New_since *string
1910 Baseline_file *string
1911 }
Jiyong Park58c518b2018-05-12 22:29:12 +09001912 }
Sundong Ahn1b92c822018-05-29 11:35:17 +09001913 Aidl struct {
1914 Include_dirs []string
1915 Local_include_dirs []string
1916 }
Paul Duffin040e9062020-11-23 17:41:36 +00001917 Dists []android.Dist
Jiyong Parkc678ad32018-04-10 13:07:10 +09001918 }{}
1919
Paul Duffin7b78b4d2020-04-28 14:08:32 +01001920 // The stubs source processing uses the same compile time classpath when extracting the
1921 // API from the implementation library as it does when compiling it. i.e. the same
1922 // * sdk version
1923 // * system_modules
1924 // * libs (static_libs/libs)
Paul Duffin250e6192019-06-07 10:44:37 +01001925
Paul Duffin0ff08bd2020-04-29 13:30:54 +01001926 props.Name = proptools.StringPtr(name)
Cole Faust8eeae4b2024-09-12 11:51:04 -07001927 props.Enabled = module.EnabledProperty()
Anton Hansson944e77d2020-08-19 11:40:22 +01001928 props.Visibility = childModuleVisibility(module.sdkLibraryProperties.Stubs_source_visibility)
Paul Duffina18abc22020-05-16 18:54:24 +01001929 props.Srcs = append(props.Srcs, module.properties.Srcs...)
Anton Hanssonf8ea3722021-09-16 14:24:13 +01001930 props.Srcs = append(props.Srcs, module.sdkLibraryProperties.Api_srcs...)
Paul Duffina18abc22020-05-16 18:54:24 +01001931 props.Sdk_version = module.deviceProperties.Sdk_version
Jihoon Kang3198f3c2023-01-26 08:08:52 +00001932 props.Api_surface = &apiScope.name
Paul Duffina18abc22020-05-16 18:54:24 +01001933 props.System_modules = module.deviceProperties.System_modules
Jiyong Parkc678ad32018-04-10 13:07:10 +09001934 props.Installable = proptools.BoolPtr(false)
Sundong Ahne6f0b052018-06-05 16:46:14 +09001935 // A droiddoc module has only one Libs property and doesn't distinguish between
1936 // shared libs and static libs. So we need to add both of these libs to Libs property.
Cole Faustb7493472024-08-28 11:55:52 -07001937 props.Libs = proptools.NewConfigurable[[]string](nil, nil)
1938 props.Libs.AppendSimpleValue(module.properties.Libs)
1939 props.Libs.Append(module.properties.Static_libs)
1940 props.Libs.AppendSimpleValue(module.sdkLibraryProperties.Stub_only_libs)
1941 props.Libs.AppendSimpleValue(module.scopeToProperties[apiScope].Libs)
Paul Duffina18abc22020-05-16 18:54:24 +01001942 props.Aidl.Include_dirs = module.deviceProperties.Aidl.Include_dirs
1943 props.Aidl.Local_include_dirs = module.deviceProperties.Aidl.Local_include_dirs
1944 props.Java_version = module.properties.Java_version
Jiyong Parkc678ad32018-04-10 13:07:10 +09001945
Paul Duffine22c2ab2020-05-20 19:35:27 +01001946 props.Annotations_enabled = module.sdkLibraryProperties.Annotations_enabled
Sundong Ahn054b19a2018-10-19 13:46:09 +09001947 props.Merge_annotations_dirs = module.sdkLibraryProperties.Merge_annotations_dirs
1948 props.Merge_inclusion_annotations_dirs = module.sdkLibraryProperties.Merge_inclusion_annotations_dirs
Jihoon Kang6592e872023-12-19 01:13:16 +00001949 props.Aconfig_declarations = module.sdkLibraryProperties.Aconfig_declarations
Sundong Ahn054b19a2018-10-19 13:46:09 +09001950
Paul Duffin6d0886e2020-04-07 18:49:53 +01001951 droidstubsArgs := []string{}
Paul Duffin235ffff2019-12-24 10:41:30 +00001952 if len(module.sdkLibraryProperties.Api_packages) != 0 {
Paul Duffin6d0886e2020-04-07 18:49:53 +01001953 droidstubsArgs = append(droidstubsArgs, "--stub-packages "+strings.Join(module.sdkLibraryProperties.Api_packages, ":"))
Paul Duffin235ffff2019-12-24 10:41:30 +00001954 }
Paul Duffin6d0886e2020-04-07 18:49:53 +01001955 droidstubsArgs = append(droidstubsArgs, module.sdkLibraryProperties.Droiddoc_options...)
Anton Hanssonfd1c0d22023-11-02 15:18:09 +00001956 disabledWarnings := []string{"HiddenSuperclass"}
1957 if proptools.BoolDefault(module.sdkLibraryProperties.Api_lint.Legacy_errors_allowed, true) {
1958 disabledWarnings = append(disabledWarnings,
1959 "BroadcastBehavior",
1960 "DeprecationMismatch",
1961 "MissingPermission",
1962 "SdkConstant",
1963 "Todo",
1964 )
Paul Duffin235ffff2019-12-24 10:41:30 +00001965 }
Paul Duffin6d0886e2020-04-07 18:49:53 +01001966 droidstubsArgs = append(droidstubsArgs, android.JoinWithPrefix(disabledWarnings, "--hide "))
Sundong Ahnfb2721f2018-09-17 13:23:09 +09001967
Paul Duffin6877e6d2020-09-25 19:59:14 +01001968 // Output Javadoc comments for public scope.
1969 if apiScope == apiScopePublic {
1970 props.Output_javadoc_comments = proptools.BoolPtr(true)
1971 }
1972
Paul Duffin1fb487d2020-04-07 18:50:10 +01001973 // Add in scope specific arguments.
Paul Duffin0ff08bd2020-04-29 13:30:54 +01001974 droidstubsArgs = append(droidstubsArgs, scopeSpecificDroidstubsArgs...)
Paul Duffin11512472019-02-11 15:55:17 +00001975 props.Arg_files = module.sdkLibraryProperties.Droiddoc_option_files
Paul Duffin6d0886e2020-04-07 18:49:53 +01001976 props.Args = proptools.StringPtr(strings.Join(droidstubsArgs, " "))
Jiyong Parkc678ad32018-04-10 13:07:10 +09001977
Paul Duffin15f34ef2020-07-20 18:04:44 +01001978 // List of APIs identified from the provided source files are created. They are later
1979 // compared against to the not-yet-released (a.k.a current) list of APIs and to the
1980 // last-released (a.k.a numbered) list of API.
1981 currentApiFileName := apiScope.apiFilePrefix + "current.txt"
1982 removedApiFileName := apiScope.apiFilePrefix + "removed.txt"
1983 apiDir := module.getApiDir()
1984 currentApiFileName = path.Join(apiDir, currentApiFileName)
1985 removedApiFileName = path.Join(apiDir, removedApiFileName)
Jiyong Parkc678ad32018-04-10 13:07:10 +09001986
Paul Duffin15f34ef2020-07-20 18:04:44 +01001987 // check against the not-yet-release API
1988 props.Check_api.Current.Api_file = proptools.StringPtr(currentApiFileName)
1989 props.Check_api.Current.Removed_api_file = proptools.StringPtr(removedApiFileName)
Jiyong Park58c518b2018-05-12 22:29:12 +09001990
Paul Duffin958806b2022-05-16 13:10:47 +00001991 if module.compareAgainstLatestApi(apiScope) {
Paul Duffin15f34ef2020-07-20 18:04:44 +01001992 // check against the latest released API
1993 latestApiFilegroupName := proptools.StringPtr(module.latestApiFilegroupName(apiScope))
Anton Hanssone87b03d2020-12-21 15:29:34 +00001994 props.Previous_api = latestApiFilegroupName
Paul Duffin15f34ef2020-07-20 18:04:44 +01001995 props.Check_api.Last_released.Api_file = latestApiFilegroupName
1996 props.Check_api.Last_released.Removed_api_file = proptools.StringPtr(
1997 module.latestRemovedApiFilegroupName(apiScope))
Jaewoong Jung1a97ee02021-03-09 13:25:02 -08001998 props.Check_api.Last_released.Baseline_file = proptools.StringPtr(
1999 module.latestIncompatibilitiesFilegroupName(apiScope))
Paul Duffin160fe412020-05-10 19:32:20 +01002000
Paul Duffin15f34ef2020-07-20 18:04:44 +01002001 if proptools.Bool(module.sdkLibraryProperties.Api_lint.Enabled) {
2002 // Enable api lint.
2003 props.Check_api.Api_lint.Enabled = proptools.BoolPtr(true)
2004 props.Check_api.Api_lint.New_since = latestApiFilegroupName
Paul Duffin160fe412020-05-10 19:32:20 +01002005
Paul Duffin15f34ef2020-07-20 18:04:44 +01002006 // If it exists then pass a lint-baseline.txt through to droidstubs.
2007 baselinePath := path.Join(apiDir, apiScope.apiFilePrefix+"lint-baseline.txt")
2008 baselinePathRelativeToRoot := path.Join(mctx.ModuleDir(), baselinePath)
2009 paths, err := mctx.GlobWithDeps(baselinePathRelativeToRoot, nil)
2010 if err != nil {
2011 mctx.ModuleErrorf("error checking for presence of %s: %s", baselinePathRelativeToRoot, err)
2012 }
2013 if len(paths) == 1 {
2014 props.Check_api.Api_lint.Baseline_file = proptools.StringPtr(baselinePath)
2015 } else if len(paths) != 0 {
2016 mctx.ModuleErrorf("error checking for presence of %s: expected one path, found: %v", baselinePathRelativeToRoot, paths)
Paul Duffin160fe412020-05-10 19:32:20 +01002017 }
2018 }
Paul Duffin15f34ef2020-07-20 18:04:44 +01002019 }
Jiyong Park58c518b2018-05-12 22:29:12 +09002020
Paul Duffin15f34ef2020-07-20 18:04:44 +01002021 if !Bool(module.sdkLibraryProperties.No_dist) {
Paul Duffin040e9062020-11-23 17:41:36 +00002022 // Dist the api txt and removed api txt artifacts for sdk builds.
2023 distDir := proptools.StringPtr(path.Join(module.apiDistPath(apiScope), "api"))
Jihoon Kang02168052024-03-20 00:44:54 +00002024 stubsTypeTagPrefix := ""
2025 if mctx.Config().ReleaseHiddenApiExportableStubs() {
2026 stubsTypeTagPrefix = ".exportable"
2027 }
Paul Duffin040e9062020-11-23 17:41:36 +00002028 for _, p := range []struct {
2029 tag string
2030 pattern string
2031 }{
Jihoon Kangd1799f62024-02-20 23:01:38 +00002032 // "exportable" api files are copied to the dist directory instead of the
Jihoon Kang02168052024-03-20 00:44:54 +00002033 // "everything" api files when "RELEASE_HIDDEN_API_EXPORTABLE_STUBS" build flag
2034 // is set. Otherwise, the "everything" api files are copied to the dist directory.
2035 {tag: "%s.api.txt", pattern: "%s.txt"},
2036 {tag: "%s.removed-api.txt", pattern: "%s-removed.txt"},
Paul Duffin040e9062020-11-23 17:41:36 +00002037 } {
2038 props.Dists = append(props.Dists, android.Dist{
2039 Targets: []string{"sdk", "win_sdk"},
2040 Dir: distDir,
2041 Dest: proptools.StringPtr(fmt.Sprintf(p.pattern, module.distStem())),
Jihoon Kang02168052024-03-20 00:44:54 +00002042 Tag: proptools.StringPtr(fmt.Sprintf(p.tag, stubsTypeTagPrefix)),
Paul Duffin040e9062020-11-23 17:41:36 +00002043 })
2044 }
Anton Hansson5fd5d242020-03-27 19:43:19 +00002045 }
2046
Spandan Das2cc80ba2023-10-27 17:21:52 +00002047 mctx.CreateModule(DroidstubsFactory, &props, module.sdkComponentPropertiesForChildLibrary()).(*Droidstubs).CallHookIfAvailable(mctx)
Jiyong Parkc678ad32018-04-10 13:07:10 +09002048}
2049
Jihoon Kangb0f4c022024-08-06 00:15:25 +00002050func (module *SdkLibrary) createApiLibrary(mctx android.DefaultableHookContext, apiScope *apiScope) {
Jihoon Kang1c92c3e2023-03-23 17:44:51 +00002051 props := struct {
Jihoon Kangb0f4c022024-08-06 00:15:25 +00002052 Name *string
Cole Faust8eeae4b2024-09-12 11:51:04 -07002053 Enabled proptools.Configurable[bool]
Jihoon Kangb0f4c022024-08-06 00:15:25 +00002054 Visibility []string
2055 Api_contributions []string
Cole Faustb7493472024-08-28 11:55:52 -07002056 Libs proptools.Configurable[[]string]
Jihoon Kangb0f4c022024-08-06 00:15:25 +00002057 Static_libs []string
2058 System_modules *string
2059 Enable_validation *bool
2060 Stubs_type *string
2061 Sdk_version *string
2062 Previous_api *string
Jihoon Kang1c92c3e2023-03-23 17:44:51 +00002063 }{}
2064
2065 props.Name = proptools.StringPtr(module.apiLibraryModuleName(apiScope))
Cole Faust8eeae4b2024-09-12 11:51:04 -07002066 props.Enabled = module.EnabledProperty()
Jihoon Kang786df932023-09-07 01:18:31 +00002067 props.Visibility = []string{"//visibility:override", "//visibility:private"}
Jihoon Kang1c92c3e2023-03-23 17:44:51 +00002068
2069 apiContributions := []string{}
2070
2071 // Api surfaces are not independent of each other, but have subset relationships,
2072 // and so does the api files. To generate from-text stubs for api surfaces other than public,
2073 // all subset api domains' api_contriubtions must be added as well.
2074 scope := apiScope
2075 for scope != nil {
2076 apiContributions = append(apiContributions, module.stubsSourceModuleName(scope)+".api.contribution")
2077 scope = scope.extends
2078 }
Jihoon Kang0c705a42023-08-02 06:44:57 +00002079 if apiScope == apiScopePublic {
2080 additionalApiContribution := module.apiLibraryAdditionalApiContribution()
2081 if additionalApiContribution != "" {
2082 apiContributions = append(apiContributions, additionalApiContribution)
2083 }
2084 }
Jihoon Kang1c92c3e2023-03-23 17:44:51 +00002085
2086 props.Api_contributions = apiContributions
Jihoon Kangb0f4c022024-08-06 00:15:25 +00002087
2088 // Ensure that stub-annotations is added to the classpath before any other libs
Cole Faustb7493472024-08-28 11:55:52 -07002089 props.Libs = proptools.NewConfigurable[[]string](nil, nil)
2090 props.Libs.AppendSimpleValue([]string{"stub-annotations"})
2091 props.Libs.AppendSimpleValue(module.properties.Libs)
2092 props.Libs.Append(module.properties.Static_libs)
2093 props.Libs.AppendSimpleValue(module.sdkLibraryProperties.Stub_only_libs)
2094 props.Libs.AppendSimpleValue(module.scopeToProperties[apiScope].Libs)
Jihoon Kang1c92c3e2023-03-23 17:44:51 +00002095 props.Static_libs = module.sdkLibraryProperties.Stub_only_static_libs
Jihoon Kangd30ac8a2023-10-09 18:00:17 +00002096
Jihoon Kang4ec24872023-10-05 17:26:09 +00002097 props.System_modules = module.deviceProperties.System_modules
Jihoon Kang063ec002023-06-28 01:16:23 +00002098 props.Enable_validation = proptools.BoolPtr(true)
Jihoon Kang5d701272024-02-15 21:53:49 +00002099 props.Stubs_type = proptools.StringPtr("everything")
Jihoon Kang4ec24872023-10-05 17:26:09 +00002100
Jihoon Kangb0f4c022024-08-06 00:15:25 +00002101 if module.deviceProperties.Sdk_version != nil {
2102 props.Sdk_version = module.deviceProperties.Sdk_version
2103 }
2104
2105 if module.compareAgainstLatestApi(apiScope) {
2106 // check against the latest released API
2107 latestApiFilegroupName := proptools.StringPtr(module.latestApiFilegroupName(apiScope))
2108 props.Previous_api = latestApiFilegroupName
2109 }
2110
Spandan Das2cc80ba2023-10-27 17:21:52 +00002111 mctx.CreateModule(ApiLibraryFactory, &props, module.sdkComponentPropertiesForChildLibrary())
Jihoon Kang1c92c3e2023-03-23 17:44:51 +00002112}
2113
Jihoon Kang02168052024-03-20 00:44:54 +00002114func (module *SdkLibrary) topLevelStubsLibraryProps(mctx android.DefaultableHookContext, apiScope *apiScope, doDist bool) libraryProperties {
Jihoon Kangfa4a90d2023-12-20 02:53:38 +00002115 props := libraryProperties{}
2116
Cole Faust8eeae4b2024-09-12 11:51:04 -07002117 props.Enabled = module.EnabledProperty()
Jihoon Kang1147b312023-06-08 23:25:57 +00002118 props.Visibility = childModuleVisibility(module.sdkLibraryProperties.Stubs_library_visibility)
2119 sdkVersion := module.sdkVersionForStubsLibrary(mctx, apiScope)
2120 props.Sdk_version = proptools.StringPtr(sdkVersion)
2121
Jihoon Kang1147b312023-06-08 23:25:57 +00002122 props.System_modules = module.deviceProperties.System_modules
2123
Jihoon Kang1147b312023-06-08 23:25:57 +00002124 // The imports need to be compiled to dex if the java_sdk_library requests it.
2125 compileDex := module.dexProperties.Compile_dex
2126 if module.stubLibrariesCompiledForDex() {
2127 compileDex = proptools.BoolPtr(true)
2128 }
2129 props.Compile_dex = compileDex
2130
Jihoon Kangfa3f0782024-08-21 20:42:18 +00002131 props.Stub_contributing_api = proptools.StringPtr(apiScope.kind.String())
2132
Jihoon Kang02168052024-03-20 00:44:54 +00002133 if !Bool(module.sdkLibraryProperties.No_dist) && doDist {
2134 props.Dist.Targets = []string{"sdk", "win_sdk"}
2135 props.Dist.Dest = proptools.StringPtr(fmt.Sprintf("%v.jar", module.distStem()))
2136 props.Dist.Dir = proptools.StringPtr(module.apiDistPath(apiScope))
2137 props.Dist.Tag = proptools.StringPtr(".jar")
2138 }
Jihoon Kang85bc1932024-07-01 17:04:46 +00002139 props.Is_stubs_module = proptools.BoolPtr(true)
Jihoon Kang02168052024-03-20 00:44:54 +00002140
Jihoon Kangfa4a90d2023-12-20 02:53:38 +00002141 return props
2142}
2143
2144func (module *SdkLibrary) createTopLevelStubsLibrary(
Jihoon Kangb0f4c022024-08-06 00:15:25 +00002145 mctx android.DefaultableHookContext, apiScope *apiScope) {
Jihoon Kangfa4a90d2023-12-20 02:53:38 +00002146
Jihoon Kang02168052024-03-20 00:44:54 +00002147 // Dist the "everything" stubs when the RELEASE_HIDDEN_API_EXPORTABLE_STUBS build flag is false
2148 doDist := !mctx.Config().ReleaseHiddenApiExportableStubs()
2149 props := module.topLevelStubsLibraryProps(mctx, apiScope, doDist)
Jihoon Kangfa4a90d2023-12-20 02:53:38 +00002150 props.Name = proptools.StringPtr(module.stubsLibraryModuleName(apiScope))
2151
2152 // Add the stub compiling java_library/java_api_library as static lib based on build config
2153 staticLib := module.sourceStubsLibraryModuleName(apiScope)
Jihoon Kangb0f4c022024-08-06 00:15:25 +00002154 if mctx.Config().BuildFromTextStub() && module.ModuleBuildFromTextStubs() {
Jihoon Kangfa4a90d2023-12-20 02:53:38 +00002155 staticLib = module.apiLibraryModuleName(apiScope)
2156 }
2157 props.Static_libs = append(props.Static_libs, staticLib)
2158
2159 mctx.CreateModule(LibraryFactory, &props, module.sdkComponentPropertiesForChildLibrary())
2160}
2161
2162func (module *SdkLibrary) createTopLevelExportableStubsLibrary(
2163 mctx android.DefaultableHookContext, apiScope *apiScope) {
2164
Jihoon Kang02168052024-03-20 00:44:54 +00002165 // Dist the "exportable" stubs when the RELEASE_HIDDEN_API_EXPORTABLE_STUBS build flag is true
2166 doDist := mctx.Config().ReleaseHiddenApiExportableStubs()
2167 props := module.topLevelStubsLibraryProps(mctx, apiScope, doDist)
Jihoon Kangfa4a90d2023-12-20 02:53:38 +00002168 props.Name = proptools.StringPtr(module.exportableStubsLibraryModuleName(apiScope))
2169
Jihoon Kangfa4a90d2023-12-20 02:53:38 +00002170 staticLib := module.exportableSourceStubsLibraryModuleName(apiScope)
2171 props.Static_libs = append(props.Static_libs, staticLib)
2172
Jihoon Kang1147b312023-06-08 23:25:57 +00002173 mctx.CreateModule(LibraryFactory, &props, module.sdkComponentPropertiesForChildLibrary())
2174}
2175
Paul Duffin958806b2022-05-16 13:10:47 +00002176func (module *SdkLibrary) compareAgainstLatestApi(apiScope *apiScope) bool {
2177 return !(apiScope.unstable || module.sdkLibraryProperties.Unsafe_ignore_missing_latest_api)
2178}
2179
Paul Duffinea8f8082021-06-24 13:25:57 +01002180// Implements android.ApexModule
Jooyung Han5e9013b2020-03-10 06:23:13 +09002181func (module *SdkLibrary) DepIsInSameApex(mctx android.BaseModuleContext, dep android.Module) bool {
2182 depTag := mctx.OtherModuleDependencyTag(dep)
2183 if depTag == xmlPermissionsFileTag {
2184 return true
2185 }
Jihoon Kanga3a05462024-04-05 00:36:44 +00002186 if dep.Name() == module.implLibraryModuleName() {
2187 return true
2188 }
Jooyung Han5e9013b2020-03-10 06:23:13 +09002189 return module.Library.DepIsInSameApex(mctx, dep)
2190}
2191
Paul Duffinea8f8082021-06-24 13:25:57 +01002192// Implements android.ApexModule
2193func (module *SdkLibrary) UniqueApexVariations() bool {
2194 return module.uniqueApexVariations()
2195}
2196
Jihoon Kangb0f4c022024-08-06 00:15:25 +00002197func (module *SdkLibrary) ModuleBuildFromTextStubs() bool {
2198 return proptools.BoolDefault(module.sdkLibraryProperties.Build_from_text_stub, true)
Jihoon Kang80456fd2023-11-15 19:22:14 +00002199}
2200
Jiyong Parkc678ad32018-04-10 13:07:10 +09002201// Creates the xml file that publicizes the runtime library
Paul Duffinf0229202020-04-29 16:47:28 +01002202func (module *SdkLibrary) createXmlFile(mctx android.DefaultableHookContext) {
Spandan Das8c9ae7e2023-03-03 21:20:36 +00002203 moduleMinApiLevel := module.Library.MinSdkVersion(mctx)
Pedro Loureiroc3621422021-09-28 15:40:23 +00002204 var moduleMinApiLevelStr = moduleMinApiLevel.String()
2205 if moduleMinApiLevel == android.NoneApiLevel {
2206 moduleMinApiLevelStr = "current"
2207 }
Jiyong Parke3833882020-02-17 17:28:10 +09002208 props := struct {
Pedro Loureiroc3621422021-09-28 15:40:23 +00002209 Name *string
Cole Faust8eeae4b2024-09-12 11:51:04 -07002210 Enabled proptools.Configurable[bool]
Pedro Loureiroc3621422021-09-28 15:40:23 +00002211 Lib_name *string
2212 Apex_available []string
2213 On_bootclasspath_since *string
2214 On_bootclasspath_before *string
2215 Min_device_sdk *string
2216 Max_device_sdk *string
2217 Sdk_library_min_api_level *string
Jamie Garsidee570ace2023-11-27 12:07:36 +00002218 Uses_libs_dependencies []string
Jiyong Parke3833882020-02-17 17:28:10 +09002219 }{
Pedro Loureiroc3621422021-09-28 15:40:23 +00002220 Name: proptools.StringPtr(module.xmlPermissionsModuleName()),
Cole Faust8eeae4b2024-09-12 11:51:04 -07002221 Enabled: module.EnabledProperty(),
Pedro Loureiroc3621422021-09-28 15:40:23 +00002222 Lib_name: proptools.StringPtr(module.BaseModuleName()),
2223 Apex_available: module.ApexProperties.Apex_available,
2224 On_bootclasspath_since: module.commonSdkLibraryProperties.On_bootclasspath_since,
2225 On_bootclasspath_before: module.commonSdkLibraryProperties.On_bootclasspath_before,
2226 Min_device_sdk: module.commonSdkLibraryProperties.Min_device_sdk,
2227 Max_device_sdk: module.commonSdkLibraryProperties.Max_device_sdk,
2228 Sdk_library_min_api_level: &moduleMinApiLevelStr,
Jamie Garsidee570ace2023-11-27 12:07:36 +00002229 Uses_libs_dependencies: module.usesLibraryProperties.Uses_libs,
Jiyong Parkc678ad32018-04-10 13:07:10 +09002230 }
Jiyong Parke3833882020-02-17 17:28:10 +09002231
Jiyong Parke3833882020-02-17 17:28:10 +09002232 mctx.CreateModule(sdkLibraryXmlFactory, &props)
Jiyong Parkc678ad32018-04-10 13:07:10 +09002233}
2234
Jiyong Parkf1691d22021-03-29 20:11:58 +09002235func PrebuiltJars(ctx android.BaseModuleContext, baseName string, s android.SdkSpec) android.Paths {
Jiyong Park54105c42021-03-31 18:17:53 +09002236 var ver android.ApiLevel
Jiyong Parkf1691d22021-03-29 20:11:58 +09002237 var kind android.SdkKind
2238 if s.UsePrebuilt(ctx) {
Jiyong Park54105c42021-03-31 18:17:53 +09002239 ver = s.ApiLevel
Jiyong Parkf1691d22021-03-29 20:11:58 +09002240 kind = s.Kind
Jiyong Parkc678ad32018-04-10 13:07:10 +09002241 } else {
Jiyong Park6a927c42020-01-21 02:03:43 +09002242 // We don't have prebuilt SDK for the specific sdkVersion.
2243 // Instead of breaking the build, fallback to use "system_current"
Jiyong Park54105c42021-03-31 18:17:53 +09002244 ver = android.FutureApiLevel
Jiyong Parkf1691d22021-03-29 20:11:58 +09002245 kind = android.SdkSystem
Sundong Ahn054b19a2018-10-19 13:46:09 +09002246 }
Jiyong Park6a927c42020-01-21 02:03:43 +09002247
2248 dir := filepath.Join("prebuilts", "sdk", ver.String(), kind.String())
Paul Duffin50061512020-01-21 16:31:05 +00002249 jar := filepath.Join(dir, baseName+".jar")
Sundong Ahn054b19a2018-10-19 13:46:09 +09002250 jarPath := android.ExistentPathForSource(ctx, jar)
Sundong Ahnae418ac2019-02-28 15:01:28 +09002251 if !jarPath.Valid() {
Colin Cross07c88562020-01-07 09:34:44 -08002252 if ctx.Config().AllowMissingDependencies() {
2253 return android.Paths{android.PathForSource(ctx, jar)}
2254 } else {
Jiyong Parkf1691d22021-03-29 20:11:58 +09002255 ctx.PropertyErrorf("sdk_library", "invalid sdk version %q, %q does not exist", s.Raw, jar)
Colin Cross07c88562020-01-07 09:34:44 -08002256 }
Sundong Ahnae418ac2019-02-28 15:01:28 +09002257 return nil
2258 }
Sundong Ahn054b19a2018-10-19 13:46:09 +09002259 return android.Paths{jarPath.Path()}
2260}
2261
Colin Crossaede88c2020-08-11 12:17:01 -07002262// 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 +01002263//
2264// If either this or the other module are on the platform then this will return
2265// false.
Colin Cross56a83212020-09-15 18:30:11 -07002266func withinSameApexesAs(ctx android.BaseModuleContext, other android.Module) bool {
Colin Crossff694a82023-12-13 15:54:49 -08002267 apexInfo, _ := android.ModuleProvider(ctx, android.ApexInfoProvider)
Colin Cross313aa542023-12-13 13:47:44 -08002268 otherApexInfo, _ := android.OtherModuleProvider(ctx, other, android.ApexInfoProvider)
Jiyong Parkab50b072021-05-12 17:13:56 +09002269 return len(otherApexInfo.InApexVariants) > 0 && reflect.DeepEqual(apexInfo.InApexVariants, otherApexInfo.InApexVariants)
Paul Duffin9b879592020-05-26 13:21:35 +01002270}
2271
Jihoon Kang8479dea2024-04-04 01:19:05 +00002272func (module *SdkLibrary) sdkJars(ctx android.BaseModuleContext, sdkVersion android.SdkSpec) android.Paths {
Jiyong Park932cdfe2020-05-28 00:19:53 +09002273 // If the client doesn't set sdk_version, but if this library prefers stubs over
2274 // the impl library, let's provide the widest API surface possible. To do so,
2275 // force override sdk_version to module_current so that the closest possible API
2276 // surface could be found in selectHeaderJarsForSdkVersion
Jiyong Parkf1691d22021-03-29 20:11:58 +09002277 if module.defaultsToStubs() && !sdkVersion.Specified() {
Jiyong Park92315372021-04-02 08:45:46 +09002278 sdkVersion = android.SdkSpecFrom(ctx, "module_current")
Jiyong Park932cdfe2020-05-28 00:19:53 +09002279 }
Paul Duffind1b3a922020-01-22 11:57:20 +00002280
Paul Duffindaaa3322020-05-26 18:13:57 +01002281 // Only provide access to the implementation library if it is actually built.
2282 if module.requiresRuntimeImplementationLibrary() {
2283 // Check any special cases for java_sdk_library.
2284 //
2285 // Only allow access to the implementation library in the following condition:
2286 // * No sdk_version specified on the referencing module.
Paul Duffin9b879592020-05-26 13:21:35 +01002287 // * The referencing module is in the same apex as this.
Jiyong Parkf1691d22021-03-29 20:11:58 +09002288 if sdkVersion.Kind == android.SdkPrivate || withinSameApexesAs(ctx, module) {
Jihoon Kang8479dea2024-04-04 01:19:05 +00002289 return module.implLibraryHeaderJars
Sundong Ahn054b19a2018-10-19 13:46:09 +09002290 }
Jiyong Parkc678ad32018-04-10 13:07:10 +09002291 }
Paul Duffinb05d4292020-05-20 12:19:10 +01002292
Paul Duffin23970f42020-05-20 14:20:02 +01002293 return module.selectHeaderJarsForSdkVersion(ctx, sdkVersion)
Jiyong Parkc678ad32018-04-10 13:07:10 +09002294}
2295
Sundong Ahn241cd372018-07-13 16:16:44 +09002296// to satisfy SdkLibraryDependency interface
Jiyong Parkf1691d22021-03-29 20:11:58 +09002297func (module *SdkLibrary) SdkHeaderJars(ctx android.BaseModuleContext, sdkVersion android.SdkSpec) android.Paths {
Jihoon Kang8479dea2024-04-04 01:19:05 +00002298 return module.sdkJars(ctx, sdkVersion)
Sundong Ahn241cd372018-07-13 16:16:44 +09002299}
2300
Colin Cross571cccf2019-02-04 11:22:08 -08002301var javaSdkLibrariesKey = android.NewOnceKey("javaSdkLibraries")
2302
Jiyong Park82484c02018-04-23 21:41:26 +09002303func javaSdkLibraries(config android.Config) *[]string {
Colin Cross571cccf2019-02-04 11:22:08 -08002304 return config.Once(javaSdkLibrariesKey, func() interface{} {
Jiyong Park82484c02018-04-23 21:41:26 +09002305 return &[]string{}
2306 }).(*[]string)
2307}
2308
Paul Duffin749f98f2019-12-30 17:23:46 +00002309func (module *SdkLibrary) getApiDir() string {
2310 return proptools.StringDefault(module.sdkLibraryProperties.Api_dir, "api")
2311}
2312
Jiyong Parkc678ad32018-04-10 13:07:10 +09002313// For a java_sdk_library module, create internal modules for stubs, docs,
2314// runtime libs and xml file. If requested, the stubs and docs are created twice
2315// once for public API level and once for system API level
Paul Duffinf0229202020-04-29 16:47:28 +01002316func (module *SdkLibrary) CreateInternalModules(mctx android.DefaultableHookContext) {
Paul Duffina18abc22020-05-16 18:54:24 +01002317 if len(module.properties.Srcs) == 0 {
Inseob Kimc0907f12019-02-08 21:00:45 +09002318 mctx.PropertyErrorf("srcs", "java_sdk_library must specify srcs")
Jooyung Han58f26ab2019-12-18 15:34:32 +09002319 return
Inseob Kimc0907f12019-02-08 21:00:45 +09002320 }
2321
Paul Duffin37e0b772019-12-30 17:20:10 +00002322 // If this builds against standard libraries (i.e. is not part of the core libraries)
Paul Duffin4f5c1ef2020-11-19 14:53:43 +00002323 // then assume it provides both system and test apis.
Jiyong Parkf1691d22021-03-29 20:11:58 +09002324 sdkDep := decodeSdkDep(mctx, android.SdkContext(&module.Library))
Paul Duffin37e0b772019-12-30 17:20:10 +00002325 hasSystemAndTestApis := sdkDep.hasStandardLibs()
Paul Duffin3375e352020-04-28 10:44:03 +01002326 module.sdkLibraryProperties.Generate_system_and_test_apis = hasSystemAndTestApis
Paul Duffin4f5c1ef2020-11-19 14:53:43 +00002327
Jaewoong Jung18aefc12020-12-21 09:11:10 -08002328 missingCurrentApi := false
Inseob Kim8098faa2019-03-18 10:19:51 +09002329
Paul Duffin3375e352020-04-28 10:44:03 +01002330 generatedScopes := module.getGeneratedApiScopes(mctx)
Paul Duffind1b3a922020-01-22 11:57:20 +00002331
Paul Duffin749f98f2019-12-30 17:23:46 +00002332 apiDir := module.getApiDir()
Paul Duffin3375e352020-04-28 10:44:03 +01002333 for _, scope := range generatedScopes {
Inseob Kim8098faa2019-03-18 10:19:51 +09002334 for _, api := range []string{"current.txt", "removed.txt"} {
Paul Duffind1b3a922020-01-22 11:57:20 +00002335 path := path.Join(mctx.ModuleDir(), apiDir, scope.apiFilePrefix+api)
Inseob Kim8098faa2019-03-18 10:19:51 +09002336 p := android.ExistentPathForSource(mctx, path)
2337 if !p.Valid() {
Colin Cross18f840c2021-05-20 17:56:54 -07002338 if mctx.Config().AllowMissingDependencies() {
2339 mctx.AddMissingDependencies([]string{path})
2340 } else {
2341 mctx.ModuleErrorf("Current api file %#v doesn't exist", path)
2342 missingCurrentApi = true
2343 }
Inseob Kim8098faa2019-03-18 10:19:51 +09002344 }
2345 }
2346 }
2347
Jaewoong Jung18aefc12020-12-21 09:11:10 -08002348 if missingCurrentApi {
Inseob Kim8098faa2019-03-18 10:19:51 +09002349 script := "build/soong/scripts/gen-java-current-api-files.sh"
2350 p := android.ExistentPathForSource(mctx, script)
2351
2352 if !p.Valid() {
2353 panic(fmt.Sprintf("script file %s doesn't exist", script))
2354 }
2355
2356 mctx.ModuleErrorf("One or more current api files are missing. "+
2357 "You can update them by:\n"+
Paul Duffin37e0b772019-12-30 17:20:10 +00002358 "%s %q %s && m update-api",
Paul Duffind1b3a922020-01-22 11:57:20 +00002359 script, filepath.Join(mctx.ModuleDir(), apiDir),
Paul Duffin3375e352020-04-28 10:44:03 +01002360 strings.Join(generatedScopes.Strings(func(s *apiScope) string { return s.apiFilePrefix }), " "))
Inseob Kim8098faa2019-03-18 10:19:51 +09002361 return
2362 }
2363
Paul Duffin3375e352020-04-28 10:44:03 +01002364 for _, scope := range generatedScopes {
Paul Duffin15f34ef2020-07-20 18:04:44 +01002365 // Use the stubs source name for legacy reasons.
2366 module.createStubsSourcesAndApi(mctx, scope, module.stubsSourceModuleName(scope), scope.droidstubsArgs)
Paul Duffin0ff08bd2020-04-29 13:30:54 +01002367
Paul Duffind1b3a922020-01-22 11:57:20 +00002368 module.createStubsLibrary(mctx, scope)
Jihoon Kangfa4a90d2023-12-20 02:53:38 +00002369 module.createExportableStubsLibrary(mctx, scope)
Jihoon Kang1c92c3e2023-03-23 17:44:51 +00002370
Jihoon Kangb0f4c022024-08-06 00:15:25 +00002371 if mctx.Config().BuildFromTextStub() && module.ModuleBuildFromTextStubs() {
2372 module.createApiLibrary(mctx, scope)
Jihoon Kang0c705a42023-08-02 06:44:57 +00002373 }
Jihoon Kangb0f4c022024-08-06 00:15:25 +00002374 module.createTopLevelStubsLibrary(mctx, scope)
Jihoon Kangfa4a90d2023-12-20 02:53:38 +00002375 module.createTopLevelExportableStubsLibrary(mctx, scope)
Inseob Kimc0907f12019-02-08 21:00:45 +09002376 }
2377
Paul Duffindfa131e2020-05-15 20:37:11 +01002378 if module.requiresRuntimeImplementationLibrary() {
Paul Duffin5df79302020-05-16 15:52:12 +01002379 // Create child module to create an implementation library.
2380 //
2381 // This temporarily creates a second implementation library that can be explicitly
2382 // referenced.
2383 //
2384 // TODO(b/156618935) - update comment once only one implementation library is created.
2385 module.createImplLibrary(mctx)
2386
Paul Duffindfa131e2020-05-15 20:37:11 +01002387 // Only create an XML permissions file that declares the library as being usable
2388 // as a shared library if required.
2389 if module.sharedLibrary() {
2390 module.createXmlFile(mctx)
2391 }
Paul Duffin43db9be2019-12-30 17:35:49 +00002392
2393 // record java_sdk_library modules so that they are exported to make
2394 javaSdkLibraries := javaSdkLibraries(mctx.Config())
2395 javaSdkLibrariesLock.Lock()
2396 defer javaSdkLibrariesLock.Unlock()
2397 *javaSdkLibraries = append(*javaSdkLibraries, module.BaseModuleName())
2398 }
Anton Hansson7f66efa2020-10-08 14:47:23 +01002399
Paul Duffin77590a82022-04-28 14:13:30 +00002400 // 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 +01002401 module.properties.Libs = append(module.properties.Libs, module.sdkLibraryProperties.Impl_only_libs...)
Cole Faustb7493472024-08-28 11:55:52 -07002402 module.properties.Static_libs.AppendSimpleValue(module.sdkLibraryProperties.Impl_only_static_libs)
Inseob Kimc0907f12019-02-08 21:00:45 +09002403}
2404
2405func (module *SdkLibrary) InitSdkLibraryProperties() {
Colin Crossce6734e2020-06-15 16:09:53 -07002406 module.addHostAndDeviceProperties()
2407 module.AddProperties(&module.sdkLibraryProperties)
Sundong Ahn054b19a2018-10-19 13:46:09 +09002408
Paul Duffin71b33cc2021-06-23 11:39:47 +01002409 module.initSdkLibraryComponent(module)
Paul Duffin859fe962020-05-15 10:20:31 +01002410
Paul Duffina18abc22020-05-16 18:54:24 +01002411 module.properties.Installable = proptools.BoolPtr(true)
2412 module.deviceProperties.IsSDKLibrary = true
Inseob Kimc0907f12019-02-08 21:00:45 +09002413}
Sundong Ahn054b19a2018-10-19 13:46:09 +09002414
Paul Duffindfa131e2020-05-15 20:37:11 +01002415func (module *SdkLibrary) requiresRuntimeImplementationLibrary() bool {
2416 return !proptools.Bool(module.sdkLibraryProperties.Api_only)
2417}
2418
Jiyong Park932cdfe2020-05-28 00:19:53 +09002419func (module *SdkLibrary) defaultsToStubs() bool {
2420 return proptools.Bool(module.sdkLibraryProperties.Default_to_stubs)
2421}
2422
Jihoon Kangfa3f0782024-08-21 20:42:18 +00002423func moduleStubLinkType(j *Module) (stub bool, ret sdkLinkType) {
2424 kind := android.ToSdkKind(proptools.String(j.properties.Stub_contributing_api))
2425 switch kind {
2426 case android.SdkPublic:
Anton Hansson2d0c1942020-05-25 12:20:51 +01002427 return true, javaSdk
Jihoon Kangfa3f0782024-08-21 20:42:18 +00002428 case android.SdkSystem:
Anton Hansson2d0c1942020-05-25 12:20:51 +01002429 return true, javaSystem
Jihoon Kangfa3f0782024-08-21 20:42:18 +00002430 case android.SdkModule:
Anton Hansson2d0c1942020-05-25 12:20:51 +01002431 return true, javaModule
Jihoon Kangfa3f0782024-08-21 20:42:18 +00002432 case android.SdkTest:
Anton Hansson2d0c1942020-05-25 12:20:51 +01002433 return true, javaSystem
Jihoon Kangfa3f0782024-08-21 20:42:18 +00002434 case android.SdkSystemServer:
Jihoon Kang1147b312023-06-08 23:25:57 +00002435 return true, javaSystemServer
Jihoon Kangfa3f0782024-08-21 20:42:18 +00002436 // Default value for all modules other than java_sdk_library-generated stub submodules
2437 case android.SdkInvalid:
2438 return false, javaPlatform
2439 default:
2440 panic(fmt.Sprintf("stub_contributing_api set as an unsupported sdk kind %s", kind.String()))
Jihoon Kang1147b312023-06-08 23:25:57 +00002441 }
Anton Hansson2d0c1942020-05-25 12:20:51 +01002442}
2443
Jaewoong Jung4f158ee2019-07-11 10:05:35 -07002444// java_sdk_library is a special Java library that provides optional platform APIs to apps.
2445// In practice, it can be viewed as a combination of several modules: 1) stubs library that clients
2446// are linked against to, 2) droiddoc module that internally generates API stubs source files,
2447// 3) the real runtime shared library that implements the APIs, and 4) XML file for adding
2448// the runtime lib to the classpath at runtime if requested via <uses-library>.
Inseob Kimc0907f12019-02-08 21:00:45 +09002449func SdkLibraryFactory() android.Module {
2450 module := &SdkLibrary{}
Paul Duffinc3091c82020-05-08 14:16:20 +01002451
2452 // Initialize information common between source and prebuilt.
Paul Duffin71b33cc2021-06-23 11:39:47 +01002453 module.initCommon(module)
Paul Duffinc3091c82020-05-08 14:16:20 +01002454
Inseob Kimc0907f12019-02-08 21:00:45 +09002455 module.InitSdkLibraryProperties()
Jooyung Han58f26ab2019-12-18 15:34:32 +09002456 android.InitApexModule(module)
Sundong Ahn054b19a2018-10-19 13:46:09 +09002457 InitJavaModule(module, android.HostAndDeviceSupported)
Paul Duffin3375e352020-04-28 10:44:03 +01002458
2459 // Initialize the map from scope to scope specific properties.
2460 scopeToProperties := make(map[*apiScope]*ApiScopeProperties)
Jihoon Kang98aa8fa2024-06-07 11:06:57 +00002461 for _, scope := range AllApiScopes {
Paul Duffin3375e352020-04-28 10:44:03 +01002462 scopeToProperties[scope] = scope.scopeSpecificProperties(module)
2463 }
2464 module.scopeToProperties = scopeToProperties
2465
Paul Duffin4911a892020-04-29 23:35:13 +01002466 // Add the properties containing visibility rules so that they are checked.
Paul Duffin5df79302020-05-16 15:52:12 +01002467 android.AddVisibilityProperty(module, "impl_library_visibility", &module.sdkLibraryProperties.Impl_library_visibility)
Paul Duffin4911a892020-04-29 23:35:13 +01002468 android.AddVisibilityProperty(module, "stubs_library_visibility", &module.sdkLibraryProperties.Stubs_library_visibility)
2469 android.AddVisibilityProperty(module, "stubs_source_visibility", &module.sdkLibraryProperties.Stubs_source_visibility)
2470
Paul Duffin1b1e8062020-05-08 13:44:43 +01002471 module.SetDefaultableHook(func(ctx android.DefaultableHookContext) {
Paul Duffindfa131e2020-05-15 20:37:11 +01002472 // If no implementation is required then it cannot be used as a shared library
2473 // either.
2474 if !module.requiresRuntimeImplementationLibrary() {
2475 // If shared_library has been explicitly set to true then it is incompatible
2476 // with api_only: true.
2477 if proptools.Bool(module.commonSdkLibraryProperties.Shared_library) {
2478 ctx.PropertyErrorf("api_only/shared_library", "inconsistent settings, shared_library and api_only cannot both be true")
2479 }
2480 // Set shared_library: false.
2481 module.commonSdkLibraryProperties.Shared_library = proptools.BoolPtr(false)
2482 }
2483
Paul Duffin1b1e8062020-05-08 13:44:43 +01002484 if module.initCommonAfterDefaultsApplied(ctx) {
2485 module.CreateInternalModules(ctx)
2486 }
2487 })
Jiyong Parkc678ad32018-04-10 13:07:10 +09002488 return module
2489}
Colin Cross79c7c262019-04-17 11:11:46 -07002490
2491//
2492// SDK library prebuilts
2493//
2494
Paul Duffin56d44902020-01-31 13:36:25 +00002495// Properties associated with each api scope.
2496type sdkLibraryScopeProperties struct {
Colin Cross79c7c262019-04-17 11:11:46 -07002497 Jars []string `android:"path"`
2498
2499 Sdk_version *string
2500
Colin Cross79c7c262019-04-17 11:11:46 -07002501 // List of shared java libs that this module has dependencies to
2502 Libs []string
Paul Duffin3d1248c2020-04-09 00:10:17 +01002503
Paul Duffinc8782502020-04-29 20:45:27 +01002504 // The stubs source.
Paul Duffin3d1248c2020-04-09 00:10:17 +01002505 Stub_srcs []string `android:"path"`
Paul Duffin1fd005d2020-04-09 01:08:11 +01002506
2507 // The current.txt
Paul Duffin0f8faff2020-05-20 16:18:00 +01002508 Current_api *string `android:"path"`
Paul Duffin1fd005d2020-04-09 01:08:11 +01002509
2510 // The removed.txt
Paul Duffin0f8faff2020-05-20 16:18:00 +01002511 Removed_api *string `android:"path"`
Anton Hanssond78eb762021-09-21 15:25:12 +01002512
2513 // Annotation zip
2514 Annotations *string `android:"path"`
Colin Cross79c7c262019-04-17 11:11:46 -07002515}
2516
Paul Duffin56d44902020-01-31 13:36:25 +00002517type sdkLibraryImportProperties struct {
Paul Duffinfcfd7912020-01-31 17:54:30 +00002518 // List of shared java libs, common to all scopes, that this module has
2519 // dependencies to
2520 Libs []string
Paul Duffin1267d872021-04-16 17:21:36 +01002521
2522 // If set to true, compile dex files for the stubs. Defaults to false.
2523 Compile_dex *bool
Paul Duffin869de142021-07-15 14:14:41 +01002524
2525 // If not empty, classes are restricted to the specified packages and their sub-packages.
Paul Duffin869de142021-07-15 14:14:41 +01002526 Permitted_packages []string
Spandan Das23956d12024-01-19 00:22:22 +00002527
2528 // Name of the source soong module that gets shadowed by this prebuilt
2529 // If unspecified, follows the naming convention that the source module of
2530 // the prebuilt is Name() without "prebuilt_" prefix
2531 Source_module_name *string
Paul Duffin56d44902020-01-31 13:36:25 +00002532}
2533
Paul Duffineedc5d52020-06-12 17:46:39 +01002534type SdkLibraryImport struct {
Colin Cross79c7c262019-04-17 11:11:46 -07002535 android.ModuleBase
2536 android.DefaultableModuleBase
2537 prebuilt android.Prebuilt
Paul Duffindd46f712020-02-10 13:37:10 +00002538 android.ApexModuleBase
Colin Cross79c7c262019-04-17 11:11:46 -07002539
Paul Duffin37856732021-02-26 14:24:15 +00002540 hiddenAPI
Jiakai Zhang204356f2021-09-09 08:12:46 +00002541 dexpreopter
Paul Duffin37856732021-02-26 14:24:15 +00002542
Colin Cross79c7c262019-04-17 11:11:46 -07002543 properties sdkLibraryImportProperties
2544
Paul Duffin46a26a82020-04-07 19:27:04 +01002545 // Map from api scope to the scope specific property structure.
2546 scopeProperties map[*apiScope]*sdkLibraryScopeProperties
2547
Paul Duffin56d44902020-01-31 13:36:25 +00002548 commonToSdkLibraryAndImport
Paul Duffineedc5d52020-06-12 17:46:39 +01002549
Paul Duffineedc5d52020-06-12 17:46:39 +01002550 // The reference to the xml permissions module created by the source module.
2551 // Is nil if the source module does not exist.
2552 xmlPermissionsFileModule *sdkLibraryXml
Paul Duffin39853512021-02-26 11:09:39 +00002553
Jeongik Chad5fe8782021-07-08 01:13:11 +09002554 // Build path to the dex implementation jar obtained from the prebuilt_apex, if any.
Spandan Dasfae468e2023-12-12 23:23:53 +00002555 dexJarFile OptionalDexJarPath
2556 dexJarFileErr error
Jeongik Chad5fe8782021-07-08 01:13:11 +09002557
2558 // Expected install file path of the source module(sdk_library)
2559 // or dex implementation jar obtained from the prebuilt_apex, if any.
2560 installFile android.Path
Colin Cross79c7c262019-04-17 11:11:46 -07002561}
2562
Paul Duffineedc5d52020-06-12 17:46:39 +01002563var _ SdkLibraryDependency = (*SdkLibraryImport)(nil)
Colin Cross79c7c262019-04-17 11:11:46 -07002564
Paul Duffin46a26a82020-04-07 19:27:04 +01002565// The type of a structure that contains a field of type sdkLibraryScopeProperties
2566// for each apiscope in allApiScopes, e.g. something like:
Colin Crossd079e0b2022-08-16 10:27:33 -07002567//
2568// struct {
2569// Public sdkLibraryScopeProperties
2570// System sdkLibraryScopeProperties
2571// ...
2572// }
Paul Duffin46a26a82020-04-07 19:27:04 +01002573var allScopeStructType = createAllScopePropertiesStructType()
2574
2575// Dynamically create a structure type for each apiscope in allApiScopes.
2576func createAllScopePropertiesStructType() reflect.Type {
2577 var fields []reflect.StructField
Jihoon Kang98aa8fa2024-06-07 11:06:57 +00002578 for _, apiScope := range AllApiScopes {
Paul Duffin46a26a82020-04-07 19:27:04 +01002579 field := reflect.StructField{
2580 Name: apiScope.fieldName,
2581 Type: reflect.TypeOf(sdkLibraryScopeProperties{}),
2582 }
2583 fields = append(fields, field)
2584 }
2585
2586 return reflect.StructOf(fields)
2587}
2588
2589// Create an instance of the scope specific structure type and return a map
2590// from apiscope to a pointer to each scope specific field.
2591func createPropertiesInstance() (interface{}, map[*apiScope]*sdkLibraryScopeProperties) {
2592 allScopePropertiesPtr := reflect.New(allScopeStructType)
2593 allScopePropertiesStruct := allScopePropertiesPtr.Elem()
2594 scopeProperties := make(map[*apiScope]*sdkLibraryScopeProperties)
2595
Jihoon Kang98aa8fa2024-06-07 11:06:57 +00002596 for _, apiScope := range AllApiScopes {
Paul Duffin46a26a82020-04-07 19:27:04 +01002597 field := allScopePropertiesStruct.FieldByName(apiScope.fieldName)
2598 scopeProperties[apiScope] = field.Addr().Interface().(*sdkLibraryScopeProperties)
2599 }
2600
2601 return allScopePropertiesPtr.Interface(), scopeProperties
2602}
2603
Jaewoong Jung4f158ee2019-07-11 10:05:35 -07002604// java_sdk_library_import imports a prebuilt java_sdk_library.
Colin Cross79c7c262019-04-17 11:11:46 -07002605func sdkLibraryImportFactory() android.Module {
Paul Duffineedc5d52020-06-12 17:46:39 +01002606 module := &SdkLibraryImport{}
Colin Cross79c7c262019-04-17 11:11:46 -07002607
Paul Duffin46a26a82020-04-07 19:27:04 +01002608 allScopeProperties, scopeToProperties := createPropertiesInstance()
2609 module.scopeProperties = scopeToProperties
Jiakai Zhang9c4dc192023-02-09 00:09:24 +08002610 module.AddProperties(&module.properties, allScopeProperties, &module.importDexpreoptProperties)
Colin Cross79c7c262019-04-17 11:11:46 -07002611
Paul Duffinc3091c82020-05-08 14:16:20 +01002612 // Initialize information common between source and prebuilt.
Paul Duffin71b33cc2021-06-23 11:39:47 +01002613 module.initCommon(module)
Paul Duffinc3091c82020-05-08 14:16:20 +01002614
Paul Duffin0bdcb272020-02-06 15:24:57 +00002615 android.InitPrebuiltModule(module, &[]string{""})
Paul Duffindd46f712020-02-10 13:37:10 +00002616 android.InitApexModule(module)
Colin Cross79c7c262019-04-17 11:11:46 -07002617 InitJavaModule(module, android.HostAndDeviceSupported)
2618
Paul Duffin1b1e8062020-05-08 13:44:43 +01002619 module.SetDefaultableHook(func(mctx android.DefaultableHookContext) {
2620 if module.initCommonAfterDefaultsApplied(mctx) {
2621 module.createInternalModules(mctx)
2622 }
2623 })
Colin Cross79c7c262019-04-17 11:11:46 -07002624 return module
2625}
2626
Paul Duffin630b11e2021-07-15 13:35:26 +01002627var _ PermittedPackagesForUpdatableBootJars = (*SdkLibraryImport)(nil)
2628
2629func (module *SdkLibraryImport) PermittedPackagesForUpdatableBootJars() []string {
2630 return module.properties.Permitted_packages
2631}
2632
Paul Duffineedc5d52020-06-12 17:46:39 +01002633func (module *SdkLibraryImport) Prebuilt() *android.Prebuilt {
Colin Cross79c7c262019-04-17 11:11:46 -07002634 return &module.prebuilt
2635}
2636
Paul Duffineedc5d52020-06-12 17:46:39 +01002637func (module *SdkLibraryImport) Name() string {
Colin Cross79c7c262019-04-17 11:11:46 -07002638 return module.prebuilt.Name(module.ModuleBase.Name())
2639}
2640
Spandan Das23956d12024-01-19 00:22:22 +00002641func (module *SdkLibraryImport) BaseModuleName() string {
2642 return proptools.StringDefault(module.properties.Source_module_name, module.ModuleBase.Name())
2643}
2644
Paul Duffineedc5d52020-06-12 17:46:39 +01002645func (module *SdkLibraryImport) createInternalModules(mctx android.DefaultableHookContext) {
Colin Cross79c7c262019-04-17 11:11:46 -07002646
Paul Duffin50061512020-01-21 16:31:05 +00002647 // If the build is configured to use prebuilts then force this to be preferred.
Jeongik Cha816a23a2020-07-08 01:09:23 +09002648 if mctx.Config().AlwaysUsePrebuiltSdks() {
Paul Duffin50061512020-01-21 16:31:05 +00002649 module.prebuilt.ForcePrefer()
2650 }
2651
Paul Duffin46a26a82020-04-07 19:27:04 +01002652 for apiScope, scopeProperties := range module.scopeProperties {
Paul Duffin56d44902020-01-31 13:36:25 +00002653 if len(scopeProperties.Jars) == 0 {
2654 continue
2655 }
2656
Paul Duffinbbb546b2020-04-09 00:07:11 +01002657 module.createJavaImportForStubs(mctx, apiScope, scopeProperties)
Paul Duffin3d1248c2020-04-09 00:10:17 +01002658
Paul Duffin0f8faff2020-05-20 16:18:00 +01002659 if len(scopeProperties.Stub_srcs) > 0 {
2660 module.createPrebuiltStubsSources(mctx, apiScope, scopeProperties)
2661 }
Jihoon Kang71c86832023-09-13 01:01:53 +00002662
2663 if scopeProperties.Current_api != nil {
2664 module.createPrebuiltApiContribution(mctx, apiScope, scopeProperties)
2665 }
Paul Duffin56d44902020-01-31 13:36:25 +00002666 }
Colin Cross79c7c262019-04-17 11:11:46 -07002667
2668 javaSdkLibraries := javaSdkLibraries(mctx.Config())
2669 javaSdkLibrariesLock.Lock()
2670 defer javaSdkLibrariesLock.Unlock()
2671 *javaSdkLibraries = append(*javaSdkLibraries, module.BaseModuleName())
2672}
2673
Paul Duffineedc5d52020-06-12 17:46:39 +01002674func (module *SdkLibraryImport) createJavaImportForStubs(mctx android.DefaultableHookContext, apiScope *apiScope, scopeProperties *sdkLibraryScopeProperties) {
Paul Duffinbbb546b2020-04-09 00:07:11 +01002675 // Creates a java import for the jar with ".stubs" suffix
2676 props := struct {
Spandan Das23956d12024-01-19 00:22:22 +00002677 Name *string
2678 Source_module_name *string
2679 Created_by_java_sdk_library_name *string
2680 Sdk_version *string
2681 Libs []string
2682 Jars []string
2683 Compile_dex *bool
Jihoon Kangfe914ed2024-02-12 22:49:21 +00002684 Is_stubs_module *bool
Paul Duffinbf4de042022-09-27 12:41:52 +01002685
2686 android.UserSuppliedPrebuiltProperties
Paul Duffinbbb546b2020-04-09 00:07:11 +01002687 }{}
Paul Duffinc3091c82020-05-08 14:16:20 +01002688 props.Name = proptools.StringPtr(module.stubsLibraryModuleName(apiScope))
Spandan Das23956d12024-01-19 00:22:22 +00002689 props.Source_module_name = proptools.StringPtr(apiScope.stubsLibraryModuleName(module.BaseModuleName()))
2690 props.Created_by_java_sdk_library_name = proptools.StringPtr(module.RootLibraryName())
Paul Duffinbbb546b2020-04-09 00:07:11 +01002691 props.Sdk_version = scopeProperties.Sdk_version
2692 // Prepend any of the libs from the legacy public properties to the libs for each of the
2693 // scopes to avoid having to duplicate them in each scope.
2694 props.Libs = append(module.properties.Libs, scopeProperties.Libs...)
2695 props.Jars = scopeProperties.Jars
Paul Duffin1dbe3ca2020-05-16 09:57:59 +01002696
Paul Duffin38b57852020-05-13 16:08:09 +01002697 // The imports are preferred if the java_sdk_library_import is preferred.
Paul Duffinbf4de042022-09-27 12:41:52 +01002698 props.CopyUserSuppliedPropertiesFromPrebuilt(&module.prebuilt)
Paul Duffin859fe962020-05-15 10:20:31 +01002699
Paul Duffin1267d872021-04-16 17:21:36 +01002700 // The imports need to be compiled to dex if the java_sdk_library_import requests it.
Paul Duffinf4600f62021-05-13 22:34:45 +01002701 compileDex := module.properties.Compile_dex
2702 if module.stubLibrariesCompiledForDex() {
2703 compileDex = proptools.BoolPtr(true)
2704 }
2705 props.Compile_dex = compileDex
Jihoon Kangfe914ed2024-02-12 22:49:21 +00002706 props.Is_stubs_module = proptools.BoolPtr(true)
Paul Duffin1267d872021-04-16 17:21:36 +01002707
Paul Duffin859fe962020-05-15 10:20:31 +01002708 mctx.CreateModule(ImportFactory, &props, module.sdkComponentPropertiesForChildLibrary())
Paul Duffinbbb546b2020-04-09 00:07:11 +01002709}
2710
Paul Duffineedc5d52020-06-12 17:46:39 +01002711func (module *SdkLibraryImport) createPrebuiltStubsSources(mctx android.DefaultableHookContext, apiScope *apiScope, scopeProperties *sdkLibraryScopeProperties) {
Paul Duffin3d1248c2020-04-09 00:10:17 +01002712 props := struct {
Spandan Das23956d12024-01-19 00:22:22 +00002713 Name *string
2714 Source_module_name *string
2715 Created_by_java_sdk_library_name *string
2716 Srcs []string
Paul Duffinbf4de042022-09-27 12:41:52 +01002717
2718 android.UserSuppliedPrebuiltProperties
Paul Duffin3d1248c2020-04-09 00:10:17 +01002719 }{}
Paul Duffinc3091c82020-05-08 14:16:20 +01002720 props.Name = proptools.StringPtr(module.stubsSourceModuleName(apiScope))
Spandan Das23956d12024-01-19 00:22:22 +00002721 props.Source_module_name = proptools.StringPtr(apiScope.stubsSourceModuleName(module.BaseModuleName()))
2722 props.Created_by_java_sdk_library_name = proptools.StringPtr(module.RootLibraryName())
Paul Duffin3d1248c2020-04-09 00:10:17 +01002723 props.Srcs = scopeProperties.Stub_srcs
Paul Duffin38b57852020-05-13 16:08:09 +01002724
2725 // The stubs source is preferred if the java_sdk_library_import is preferred.
Paul Duffinbf4de042022-09-27 12:41:52 +01002726 props.CopyUserSuppliedPropertiesFromPrebuilt(&module.prebuilt)
2727
Spandan Das2cc80ba2023-10-27 17:21:52 +00002728 mctx.CreateModule(PrebuiltStubsSourcesFactory, &props, module.sdkComponentPropertiesForChildLibrary())
Paul Duffin3d1248c2020-04-09 00:10:17 +01002729}
2730
Jihoon Kang71c86832023-09-13 01:01:53 +00002731func (module *SdkLibraryImport) createPrebuiltApiContribution(mctx android.DefaultableHookContext, apiScope *apiScope, scopeProperties *sdkLibraryScopeProperties) {
2732 api_file := scopeProperties.Current_api
2733 api_surface := &apiScope.name
2734
2735 props := struct {
Spandan Das23956d12024-01-19 00:22:22 +00002736 Name *string
2737 Source_module_name *string
2738 Created_by_java_sdk_library_name *string
2739 Api_surface *string
2740 Api_file *string
2741 Visibility []string
Jihoon Kang71c86832023-09-13 01:01:53 +00002742 }{}
2743
2744 props.Name = proptools.StringPtr(module.stubsSourceModuleName(apiScope) + ".api.contribution")
Spandan Das23956d12024-01-19 00:22:22 +00002745 props.Source_module_name = proptools.StringPtr(apiScope.stubsSourceModuleName(module.BaseModuleName()) + ".api.contribution")
2746 props.Created_by_java_sdk_library_name = proptools.StringPtr(module.RootLibraryName())
Jihoon Kang71c86832023-09-13 01:01:53 +00002747 props.Api_surface = api_surface
2748 props.Api_file = api_file
2749 props.Visibility = []string{"//visibility:override", "//visibility:public"}
2750
Spandan Das2cc80ba2023-10-27 17:21:52 +00002751 mctx.CreateModule(ApiContributionImportFactory, &props, module.sdkComponentPropertiesForChildLibrary())
Jihoon Kang71c86832023-09-13 01:01:53 +00002752}
2753
Paul Duffin44f1d842020-06-26 20:17:02 +01002754// Add the dependencies on the child module in the component deps mutator so that it
2755// creates references to the prebuilt and not the source modules.
2756func (module *SdkLibraryImport) ComponentDepsMutator(ctx android.BottomUpMutatorContext) {
Paul Duffin46a26a82020-04-07 19:27:04 +01002757 for apiScope, scopeProperties := range module.scopeProperties {
Paul Duffin56d44902020-01-31 13:36:25 +00002758 if len(scopeProperties.Jars) == 0 {
2759 continue
2760 }
2761
2762 // Add dependencies to the prebuilt stubs library
Jihoon Kangb7431552024-01-22 19:40:08 +00002763 ctx.AddVariationDependencies(nil, apiScope.prebuiltStubsTag, android.PrebuiltNameFromSource(module.stubsLibraryModuleName(apiScope)))
Paul Duffin0f8faff2020-05-20 16:18:00 +01002764
2765 if len(scopeProperties.Stub_srcs) > 0 {
2766 // Add dependencies to the prebuilt stubs source library
Paul Duffin864116c2021-04-02 10:24:13 +01002767 ctx.AddVariationDependencies(nil, apiScope.stubsSourceTag, android.PrebuiltNameFromSource(module.stubsSourceModuleName(apiScope)))
Paul Duffin0f8faff2020-05-20 16:18:00 +01002768 }
Paul Duffin56d44902020-01-31 13:36:25 +00002769 }
Paul Duffin44f1d842020-06-26 20:17:02 +01002770}
2771
2772// Add other dependencies as normal.
2773func (module *SdkLibraryImport) DepsMutator(ctx android.BottomUpMutatorContext) {
Paul Duffineedc5d52020-06-12 17:46:39 +01002774
2775 implName := module.implLibraryModuleName()
2776 if ctx.OtherModuleExists(implName) {
2777 ctx.AddVariationDependencies(nil, implLibraryTag, implName)
2778
2779 xmlPermissionsModuleName := module.xmlPermissionsModuleName()
2780 if module.sharedLibrary() && ctx.OtherModuleExists(xmlPermissionsModuleName) {
2781 // Add dependency to the rule for generating the xml permissions file
2782 ctx.AddDependency(module, xmlPermissionsFileTag, xmlPermissionsModuleName)
2783 }
2784 }
Colin Cross79c7c262019-04-17 11:11:46 -07002785}
2786
Jiyong Park45bf82e2020-12-15 22:29:02 +09002787var _ android.ApexModule = (*SdkLibraryImport)(nil)
2788
2789// Implements android.ApexModule
Paul Duffineedc5d52020-06-12 17:46:39 +01002790func (module *SdkLibraryImport) DepIsInSameApex(mctx android.BaseModuleContext, dep android.Module) bool {
2791 depTag := mctx.OtherModuleDependencyTag(dep)
2792 if depTag == xmlPermissionsFileTag {
2793 return true
2794 }
2795
2796 // None of the other dependencies of the java_sdk_library_import are in the same apex
2797 // as the one that references this module.
2798 return false
2799}
2800
Jiyong Park45bf82e2020-12-15 22:29:02 +09002801// Implements android.ApexModule
Dan Albertc8060532020-07-22 22:32:17 -07002802func (module *SdkLibraryImport) ShouldSupportSdkVersion(ctx android.BaseModuleContext,
2803 sdkVersion android.ApiLevel) error {
Jooyung Han749dc692020-04-15 11:03:39 +09002804 // we don't check prebuilt modules for sdk_version
2805 return nil
2806}
2807
Paul Duffinea8f8082021-06-24 13:25:57 +01002808// Implements android.ApexModule
2809func (module *SdkLibraryImport) UniqueApexVariations() bool {
2810 return module.uniqueApexVariations()
2811}
2812
Paul Duffin09817d62022-04-28 17:45:11 +01002813// MinSdkVersion - Implements hiddenAPIModule
Spandan Das8c9ae7e2023-03-03 21:20:36 +00002814func (module *SdkLibraryImport) MinSdkVersion(ctx android.EarlyModuleContext) android.ApiLevel {
2815 return android.NoneApiLevel
Paul Duffin09817d62022-04-28 17:45:11 +01002816}
2817
2818var _ hiddenAPIModule = (*SdkLibraryImport)(nil)
2819
Paul Duffineedc5d52020-06-12 17:46:39 +01002820func (module *SdkLibraryImport) GenerateAndroidBuildActions(ctx android.ModuleContext) {
Paul Duffina2ae7e02020-09-11 11:55:00 +01002821 module.generateCommonBuildActions(ctx)
2822
Jeongik Chad5fe8782021-07-08 01:13:11 +09002823 // Assume that source module(sdk_library) is installed in /<sdk_library partition>/framework
2824 module.installFile = android.PathForModuleInstall(ctx, "framework", module.Stem()+".jar")
2825
Paul Duffin0f8faff2020-05-20 16:18:00 +01002826 // Record the paths to the prebuilt stubs library and stubs source.
Colin Cross79c7c262019-04-17 11:11:46 -07002827 ctx.VisitDirectDeps(func(to android.Module) {
2828 tag := ctx.OtherModuleDependencyTag(to)
2829
Paul Duffin0f8faff2020-05-20 16:18:00 +01002830 // Extract information from any of the scope specific dependencies.
2831 if scopeTag, ok := tag.(scopeDependencyTag); ok {
2832 apiScope := scopeTag.apiScope
2833 scopePaths := module.getScopePathsCreateIfNeeded(apiScope)
2834
2835 // Extract information from the dependency. The exact information extracted
2836 // is determined by the nature of the dependency which is determined by the tag.
2837 scopeTag.extractDepInfo(ctx, to, scopePaths)
Paul Duffineedc5d52020-06-12 17:46:39 +01002838 } else if tag == implLibraryTag {
2839 if implLibrary, ok := to.(*Library); ok {
2840 module.implLibraryModule = implLibrary
2841 } else {
2842 ctx.ModuleErrorf("implementation library must be of type *java.Library but was %T", to)
2843 }
2844 } else if tag == xmlPermissionsFileTag {
2845 if xmlPermissionsFileModule, ok := to.(*sdkLibraryXml); ok {
2846 module.xmlPermissionsFileModule = xmlPermissionsFileModule
2847 } else {
2848 ctx.ModuleErrorf("xml permissions file module must be of type *sdkLibraryXml but was %T", to)
2849 }
Colin Cross79c7c262019-04-17 11:11:46 -07002850 }
2851 })
Paul Duffin0f8faff2020-05-20 16:18:00 +01002852
2853 // Populate the scope paths with information from the properties.
2854 for apiScope, scopeProperties := range module.scopeProperties {
2855 if len(scopeProperties.Jars) == 0 {
2856 continue
2857 }
2858
2859 paths := module.getScopePathsCreateIfNeeded(apiScope)
Anton Hanssond78eb762021-09-21 15:25:12 +01002860 paths.annotationsZip = android.OptionalPathForModuleSrc(ctx, scopeProperties.Annotations)
Paul Duffin0f8faff2020-05-20 16:18:00 +01002861 paths.currentApiFilePath = android.OptionalPathForModuleSrc(ctx, scopeProperties.Current_api)
2862 paths.removedApiFilePath = android.OptionalPathForModuleSrc(ctx, scopeProperties.Removed_api)
2863 }
Paul Duffin39853512021-02-26 11:09:39 +00002864
2865 if ctx.Device() {
2866 // If this is a variant created for a prebuilt_apex then use the dex implementation jar
2867 // obtained from the associated deapexer module.
Colin Crossff694a82023-12-13 15:54:49 -08002868 ai, _ := android.ModuleProvider(ctx, android.ApexInfoProvider)
Paul Duffin39853512021-02-26 11:09:39 +00002869 if ai.ForPrebuiltApex {
Paul Duffin39853512021-02-26 11:09:39 +00002870 // Get the path of the dex implementation jar from the `deapexer` module.
Spandan Dasfae468e2023-12-12 23:23:53 +00002871 di, err := android.FindDeapexerProviderForModule(ctx)
2872 if err != nil {
2873 // An error was found, possibly due to multiple apexes in the tree that export this library
2874 // Defer the error till a client tries to call DexJarBuildPath
2875 module.dexJarFileErr = err
Spandan Das3a392012024-01-17 18:26:27 +00002876 module.initHiddenAPIError(err)
Spandan Dasfae468e2023-12-12 23:23:53 +00002877 return
Martin Stjernholm44825602021-09-17 01:44:12 +01002878 }
Spandan Das5be63332023-12-13 00:06:32 +00002879 dexJarFileApexRootRelative := ApexRootRelativePathToJavaLib(module.BaseModuleName())
Jiakai Zhang81e46812023-02-08 21:56:07 +08002880 if dexOutputPath := di.PrebuiltExportPath(dexJarFileApexRootRelative); dexOutputPath != nil {
Martin Stjernholm8be1e6d2021-09-15 03:34:04 +01002881 dexJarFile := makeDexJarPathFromPath(dexOutputPath)
2882 module.dexJarFile = dexJarFile
Jiakai Zhang204356f2021-09-09 08:12:46 +00002883 installPath := android.PathForModuleInPartitionInstall(
Jiakai Zhang81e46812023-02-08 21:56:07 +08002884 ctx, "apex", ai.ApexVariationName, dexJarFileApexRootRelative)
Jiakai Zhang204356f2021-09-09 08:12:46 +00002885 module.installFile = installPath
Martin Stjernholm8be1e6d2021-09-15 03:34:04 +01002886 module.initHiddenAPI(ctx, dexJarFile, module.findScopePaths(apiScopePublic).stubsImplPath[0], nil)
Jiakai Zhang204356f2021-09-09 08:12:46 +00002887
Spandan Dase21a8d42024-01-23 23:56:29 +00002888 module.dexpreopter.installPath = module.dexpreopter.getInstallPath(ctx, android.RemoveOptionalPrebuiltPrefix(ctx.ModuleName()), installPath)
Jiakai Zhang204356f2021-09-09 08:12:46 +00002889 module.dexpreopter.isSDKLibrary = true
Spandan Dase21a8d42024-01-23 23:56:29 +00002890 module.dexpreopter.uncompressedDex = shouldUncompressDex(ctx, android.RemoveOptionalPrebuiltPrefix(ctx.ModuleName()), &module.dexpreopter)
Jiakai Zhang81e46812023-02-08 21:56:07 +08002891
2892 if profilePath := di.PrebuiltExportPath(dexJarFileApexRootRelative + ".prof"); profilePath != nil {
2893 module.dexpreopter.inputProfilePathOnHost = profilePath
2894 }
Paul Duffin39853512021-02-26 11:09:39 +00002895 } else {
2896 // This should never happen as a variant for a prebuilt_apex is only created if the
2897 // prebuilt_apex has been configured to export the java library dex file.
Martin Stjernholm44825602021-09-17 01:44:12 +01002898 ctx.ModuleErrorf("internal error: no dex implementation jar available from prebuilt APEX %s", di.ApexModuleName())
Paul Duffin39853512021-02-26 11:09:39 +00002899 }
2900 }
2901 }
mrziwang9f7b9f42024-07-10 12:18:06 -07002902
Jihoon Kang28c96572024-09-11 23:44:44 +00002903 var generatingLibs []string
2904 for _, apiScope := range AllApiScopes {
2905 if scopeProperties, ok := module.scopeProperties[apiScope]; ok {
2906 if len(scopeProperties.Jars) == 0 {
2907 continue
2908 }
2909 generatingLibs = append(generatingLibs, module.stubsLibraryModuleName(apiScope))
2910 }
2911 }
2912
mrziwang9f7b9f42024-07-10 12:18:06 -07002913 module.setOutputFiles(ctx)
2914 if module.implLibraryModule != nil {
Jihoon Kang28c96572024-09-11 23:44:44 +00002915 generatingLibs = append(generatingLibs, module.implLibraryModuleName())
mrziwang9f7b9f42024-07-10 12:18:06 -07002916 setOutputFiles(ctx, module.implLibraryModule.Module)
2917 }
Jihoon Kang28c96572024-09-11 23:44:44 +00002918
2919 android.SetProvider(ctx, SdkLibraryInfoProvider, SdkLibraryInfo{
2920 GeneratingLibs: generatingLibs,
2921 })
Colin Cross79c7c262019-04-17 11:11:46 -07002922}
2923
Jiyong Parkf1691d22021-03-29 20:11:58 +09002924func (module *SdkLibraryImport) sdkJars(ctx android.BaseModuleContext, sdkVersion android.SdkSpec, headerJars bool) android.Paths {
Paul Duffineedc5d52020-06-12 17:46:39 +01002925
2926 // For consistency with SdkLibrary make the implementation jar available to libraries that
2927 // are within the same APEX.
2928 implLibraryModule := module.implLibraryModule
Colin Cross56a83212020-09-15 18:30:11 -07002929 if implLibraryModule != nil && withinSameApexesAs(ctx, module) {
Paul Duffineedc5d52020-06-12 17:46:39 +01002930 if headerJars {
2931 return implLibraryModule.HeaderJars()
2932 } else {
2933 return implLibraryModule.ImplementationJars()
2934 }
2935 }
2936
Paul Duffin23970f42020-05-20 14:20:02 +01002937 return module.selectHeaderJarsForSdkVersion(ctx, sdkVersion)
Paul Duffin56d44902020-01-31 13:36:25 +00002938}
2939
Colin Cross79c7c262019-04-17 11:11:46 -07002940// to satisfy SdkLibraryDependency interface
Jiyong Parkf1691d22021-03-29 20:11:58 +09002941func (module *SdkLibraryImport) SdkHeaderJars(ctx android.BaseModuleContext, sdkVersion android.SdkSpec) android.Paths {
Colin Cross79c7c262019-04-17 11:11:46 -07002942 // This module is just a wrapper for the prebuilt stubs.
Paul Duffineedc5d52020-06-12 17:46:39 +01002943 return module.sdkJars(ctx, sdkVersion, true)
Colin Cross79c7c262019-04-17 11:11:46 -07002944}
2945
Ulya Trafimovichdbf31662020-12-17 12:07:54 +00002946// to satisfy UsesLibraryDependency interface
Spandan Das59a4a2b2024-01-09 21:35:56 +00002947func (module *SdkLibraryImport) DexJarBuildPath(ctx android.ModuleErrorfContext) OptionalDexJarPath {
Paul Duffin39853512021-02-26 11:09:39 +00002948 // The dex implementation jar extracted from the .apex file should be used in preference to the
2949 // source.
Spandan Dasfae468e2023-12-12 23:23:53 +00002950 if module.dexJarFileErr != nil {
Spandan Das59a4a2b2024-01-09 21:35:56 +00002951 ctx.ModuleErrorf(module.dexJarFileErr.Error())
Spandan Dasfae468e2023-12-12 23:23:53 +00002952 }
Martin Stjernholm8be1e6d2021-09-15 03:34:04 +01002953 if module.dexJarFile.IsSet() {
Paul Duffin39853512021-02-26 11:09:39 +00002954 return module.dexJarFile
2955 }
Paul Duffineedc5d52020-06-12 17:46:39 +01002956 if module.implLibraryModule == nil {
Martin Stjernholm8be1e6d2021-09-15 03:34:04 +01002957 return makeUnsetDexJarPath()
Paul Duffineedc5d52020-06-12 17:46:39 +01002958 } else {
Spandan Das59a4a2b2024-01-09 21:35:56 +00002959 return module.implLibraryModule.DexJarBuildPath(ctx)
Paul Duffineedc5d52020-06-12 17:46:39 +01002960 }
2961}
2962
Ulya Trafimovichdbf31662020-12-17 12:07:54 +00002963// to satisfy UsesLibraryDependency interface
Ulya Trafimovich31e444e2020-08-14 17:32:16 +01002964func (module *SdkLibraryImport) DexJarInstallPath() android.Path {
Jeongik Chad5fe8782021-07-08 01:13:11 +09002965 return module.installFile
Ulya Trafimovich31e444e2020-08-14 17:32:16 +01002966}
2967
Ulya Trafimovichdbf31662020-12-17 12:07:54 +00002968// to satisfy UsesLibraryDependency interface
2969func (module *SdkLibraryImport) ClassLoaderContexts() dexpreopt.ClassLoaderContextMap {
2970 return nil
2971}
2972
Paul Duffineedc5d52020-06-12 17:46:39 +01002973// to satisfy apex.javaDependency interface
2974func (module *SdkLibraryImport) JacocoReportClassesFile() android.Path {
2975 if module.implLibraryModule == nil {
2976 return nil
2977 } else {
2978 return module.implLibraryModule.JacocoReportClassesFile()
2979 }
2980}
2981
2982// to satisfy apex.javaDependency interface
Colin Cross08dca382020-07-21 20:31:17 -07002983func (module *SdkLibraryImport) LintDepSets() LintDepSets {
2984 if module.implLibraryModule == nil {
2985 return LintDepSets{}
2986 } else {
2987 return module.implLibraryModule.LintDepSets()
2988 }
2989}
2990
Spandan Das17854f52022-01-14 21:19:14 +00002991func (module *SdkLibraryImport) GetStrictUpdatabilityLinting() bool {
Jaewoong Jung476b9d62021-05-10 15:30:00 -07002992 if module.implLibraryModule == nil {
2993 return false
2994 } else {
Spandan Das17854f52022-01-14 21:19:14 +00002995 return module.implLibraryModule.GetStrictUpdatabilityLinting()
Jaewoong Jung476b9d62021-05-10 15:30:00 -07002996 }
2997}
2998
Spandan Das17854f52022-01-14 21:19:14 +00002999func (module *SdkLibraryImport) SetStrictUpdatabilityLinting(strictLinting bool) {
Jaewoong Jung476b9d62021-05-10 15:30:00 -07003000 if module.implLibraryModule != nil {
Spandan Das17854f52022-01-14 21:19:14 +00003001 module.implLibraryModule.SetStrictUpdatabilityLinting(strictLinting)
Jaewoong Jung476b9d62021-05-10 15:30:00 -07003002 }
3003}
3004
Colin Cross08dca382020-07-21 20:31:17 -07003005// to satisfy apex.javaDependency interface
Paul Duffineedc5d52020-06-12 17:46:39 +01003006func (module *SdkLibraryImport) Stem() string {
3007 return module.BaseModuleName()
Colin Cross79c7c262019-04-17 11:11:46 -07003008}
Jiyong Parke3833882020-02-17 17:28:10 +09003009
Paul Duffin44b481b2020-06-17 16:59:43 +01003010var _ ApexDependency = (*SdkLibraryImport)(nil)
3011
3012// to satisfy java.ApexDependency interface
3013func (module *SdkLibraryImport) HeaderJars() android.Paths {
3014 if module.implLibraryModule == nil {
3015 return nil
3016 } else {
3017 return module.implLibraryModule.HeaderJars()
3018 }
3019}
3020
3021// to satisfy java.ApexDependency interface
3022func (module *SdkLibraryImport) ImplementationAndResourcesJars() android.Paths {
3023 if module.implLibraryModule == nil {
3024 return nil
3025 } else {
3026 return module.implLibraryModule.ImplementationAndResourcesJars()
3027 }
3028}
3029
Jiakai Zhang204356f2021-09-09 08:12:46 +00003030// to satisfy java.DexpreopterInterface interface
3031func (module *SdkLibraryImport) IsInstallable() bool {
3032 return true
3033}
3034
Paul Duffinfef55002021-06-17 14:56:05 +01003035var _ android.RequiredFilesFromPrebuiltApex = (*SdkLibraryImport)(nil)
3036
Paul Duffinb4bbf2c2021-06-17 15:59:07 +01003037func (module *SdkLibraryImport) RequiredFilesFromPrebuiltApex(ctx android.BaseModuleContext) []string {
Paul Duffinfef55002021-06-17 14:56:05 +01003038 name := module.BaseModuleName()
Jiakai Zhang81e46812023-02-08 21:56:07 +08003039 return requiredFilesFromPrebuiltApexForImport(name, &module.dexpreopter)
Paul Duffinfef55002021-06-17 14:56:05 +01003040}
3041
Spandan Das2ea84dd2024-01-25 22:12:50 +00003042func (j *SdkLibraryImport) UseProfileGuidedDexpreopt() bool {
3043 return proptools.Bool(j.importDexpreoptProperties.Dex_preopt.Profile_guided)
3044}
3045
Jiyong Parke3833882020-02-17 17:28:10 +09003046// java_sdk_library_xml
Jiyong Parke3833882020-02-17 17:28:10 +09003047type sdkLibraryXml struct {
3048 android.ModuleBase
3049 android.DefaultableModuleBase
3050 android.ApexModuleBase
3051
3052 properties sdkLibraryXmlProperties
3053
3054 outputFilePath android.OutputPath
3055 installDirPath android.InstallPath
Colin Cross56a83212020-09-15 18:30:11 -07003056
3057 hideApexVariantFromMake bool
Jiyong Parke3833882020-02-17 17:28:10 +09003058}
3059
3060type sdkLibraryXmlProperties struct {
3061 // canonical name of the lib
3062 Lib_name *string
Pedro Loureiro9956e5e2021-09-07 17:21:59 +00003063
3064 // Signals that this shared library is part of the bootclasspath starting
3065 // on the version indicated in this attribute.
3066 //
3067 // This will make platforms at this level and above to ignore
3068 // <uses-library> tags with this library name because the library is already
3069 // available
3070 On_bootclasspath_since *string
3071
3072 // Signals that this shared library was part of the bootclasspath before
3073 // (but not including) the version indicated in this attribute.
3074 //
3075 // The system will automatically add a <uses-library> tag with this library to
3076 // apps that target any SDK less than the version indicated in this attribute.
3077 On_bootclasspath_before *string
3078
3079 // Indicates that PackageManager should ignore this shared library if the
3080 // platform is below the version indicated in this attribute.
3081 //
3082 // This means that the device won't recognise this library as installed.
3083 Min_device_sdk *string
3084
3085 // Indicates that PackageManager should ignore this shared library if the
3086 // platform is above the version indicated in this attribute.
3087 //
3088 // This means that the device won't recognise this library as installed.
3089 Max_device_sdk *string
Pedro Loureiroc3621422021-09-28 15:40:23 +00003090
3091 // The SdkLibrary's min api level as a string
3092 //
3093 // This value comes from the ApiLevel of the MinSdkVersion property.
3094 Sdk_library_min_api_level *string
Jamie Garsidee570ace2023-11-27 12:07:36 +00003095
3096 // Uses-libs dependencies that the shared library requires to work correctly.
3097 //
3098 // This will add dependency="foo:bar" to the <library> section.
3099 Uses_libs_dependencies []string
Jiyong Parke3833882020-02-17 17:28:10 +09003100}
3101
3102// java_sdk_library_xml builds the permission xml file for a java_sdk_library.
3103// Not to be used directly by users. java_sdk_library internally uses this.
3104func sdkLibraryXmlFactory() android.Module {
3105 module := &sdkLibraryXml{}
3106
3107 module.AddProperties(&module.properties)
3108
3109 android.InitApexModule(module)
3110 android.InitAndroidArchModule(module, android.DeviceSupported, android.MultilibCommon)
3111
3112 return module
3113}
3114
Colin Crossaede88c2020-08-11 12:17:01 -07003115func (module *sdkLibraryXml) UniqueApexVariations() bool {
3116 // sdkLibraryXml needs a unique variation per APEX because the generated XML file contains the path to the
3117 // mounted APEX, which contains the name of the APEX.
3118 return true
3119}
3120
Jiyong Parke3833882020-02-17 17:28:10 +09003121// from android.PrebuiltEtcModule
Jooyung Han0703fd82020-08-26 22:11:53 +09003122func (module *sdkLibraryXml) BaseDir() string {
3123 return "etc"
3124}
3125
3126// from android.PrebuiltEtcModule
Jiyong Parke3833882020-02-17 17:28:10 +09003127func (module *sdkLibraryXml) SubDir() string {
3128 return "permissions"
3129}
3130
ThiƩbaud Weksteen00e8b312024-03-18 14:06:00 +11003131var _ etc.PrebuiltEtcModule = (*sdkLibraryXml)(nil)
3132
Jiyong Parke3833882020-02-17 17:28:10 +09003133// from android.ApexModule
3134func (module *sdkLibraryXml) AvailableFor(what string) bool {
3135 return true
3136}
3137
3138func (module *sdkLibraryXml) DepsMutator(ctx android.BottomUpMutatorContext) {
3139 // do nothing
3140}
3141
Jiyong Park45bf82e2020-12-15 22:29:02 +09003142var _ android.ApexModule = (*sdkLibraryXml)(nil)
3143
3144// Implements android.ApexModule
Dan Albertc8060532020-07-22 22:32:17 -07003145func (module *sdkLibraryXml) ShouldSupportSdkVersion(ctx android.BaseModuleContext,
3146 sdkVersion android.ApiLevel) error {
Jooyung Han749dc692020-04-15 11:03:39 +09003147 // sdkLibraryXml doesn't need to be checked separately because java_sdk_library is checked
3148 return nil
3149}
3150
Jiyong Parke3833882020-02-17 17:28:10 +09003151// File path to the runtime implementation library
Colin Cross56a83212020-09-15 18:30:11 -07003152func (module *sdkLibraryXml) implPath(ctx android.ModuleContext) string {
Jiyong Parke3833882020-02-17 17:28:10 +09003153 implName := proptools.String(module.properties.Lib_name)
Colin Crossff694a82023-12-13 15:54:49 -08003154 if apexInfo, _ := android.ModuleProvider(ctx, android.ApexInfoProvider); !apexInfo.IsForPlatform() {
Colin Crosse07f2312020-08-13 11:24:56 -07003155 // TODO(b/146468504): ApexVariationName() is only a soong module name, not apex name.
Jiyong Parke3833882020-02-17 17:28:10 +09003156 // In most cases, this works fine. But when apex_name is set or override_apex is used
3157 // this can be wrong.
Spandan Das33bbeb22024-06-18 23:28:25 +00003158 return fmt.Sprintf("/apex/%s/javalib/%s.jar", apexInfo.BaseApexName, implName)
Jiyong Parke3833882020-02-17 17:28:10 +09003159 }
3160 partition := "system"
3161 if module.SocSpecific() {
3162 partition = "vendor"
3163 } else if module.DeviceSpecific() {
3164 partition = "odm"
3165 } else if module.ProductSpecific() {
3166 partition = "product"
3167 } else if module.SystemExtSpecific() {
3168 partition = "system_ext"
3169 }
3170 return "/" + partition + "/framework/" + implName + ".jar"
3171}
3172
Pedro Loureiro9956e5e2021-09-07 17:21:59 +00003173func formattedOptionalSdkLevelAttribute(ctx android.ModuleContext, attrName string, value *string) string {
3174 if value == nil {
3175 return ""
3176 }
3177 apiLevel, err := android.ApiLevelFromUser(ctx, *value)
3178 if err != nil {
Pedro Loureiroba6682f2021-10-29 09:32:32 +00003179 // attributes in bp files have underscores but in the xml have dashes.
3180 ctx.PropertyErrorf(strings.ReplaceAll(attrName, "-", "_"), err.Error())
Pedro Loureiro9956e5e2021-09-07 17:21:59 +00003181 return ""
3182 }
Pedro Loureirob638c622021-12-22 15:28:05 +00003183 if apiLevel.IsCurrent() {
3184 // passing "current" would always mean a future release, never the current (or the current in
3185 // progress) which means some conditions would never be triggered.
3186 ctx.PropertyErrorf(strings.ReplaceAll(attrName, "-", "_"),
3187 `"current" is not an allowed value for this attribute`)
3188 return ""
3189 }
Pedro Loureiro48991222022-06-17 20:01:21 +00003190 // "safeValue" is safe because it translates finalized codenames to a string
3191 // with their SDK int.
3192 safeValue := apiLevel.String()
3193 return formattedOptionalAttribute(attrName, &safeValue)
Pedro Loureiro9956e5e2021-09-07 17:21:59 +00003194}
3195
3196// formats an attribute for the xml permissions file if the value is not null
3197// returns empty string otherwise
3198func formattedOptionalAttribute(attrName string, value *string) string {
3199 if value == nil {
3200 return ""
3201 }
Paul Duffin1816cde2024-04-10 10:58:21 +01003202 return fmt.Sprintf(" %s=\"%s\"\n", attrName, *value)
Pedro Loureiro9956e5e2021-09-07 17:21:59 +00003203}
3204
Jamie Garsidee570ace2023-11-27 12:07:36 +00003205func formattedDependenciesAttribute(dependencies []string) string {
3206 if dependencies == nil {
3207 return ""
3208 }
Paul Duffin1816cde2024-04-10 10:58:21 +01003209 return fmt.Sprintf(" dependency=\"%s\"\n", strings.Join(dependencies, ":"))
Jamie Garsidee570ace2023-11-27 12:07:36 +00003210}
3211
Pedro Loureiro9956e5e2021-09-07 17:21:59 +00003212func (module *sdkLibraryXml) permissionsContents(ctx android.ModuleContext) string {
3213 libName := proptools.String(module.properties.Lib_name)
3214 libNameAttr := formattedOptionalAttribute("name", &libName)
3215 filePath := module.implPath(ctx)
3216 filePathAttr := formattedOptionalAttribute("file", &filePath)
Pedro Loureiroba6682f2021-10-29 09:32:32 +00003217 implicitFromAttr := formattedOptionalSdkLevelAttribute(ctx, "on-bootclasspath-since", module.properties.On_bootclasspath_since)
3218 implicitUntilAttr := formattedOptionalSdkLevelAttribute(ctx, "on-bootclasspath-before", module.properties.On_bootclasspath_before)
3219 minSdkAttr := formattedOptionalSdkLevelAttribute(ctx, "min-device-sdk", module.properties.Min_device_sdk)
3220 maxSdkAttr := formattedOptionalSdkLevelAttribute(ctx, "max-device-sdk", module.properties.Max_device_sdk)
Jamie Garsidee570ace2023-11-27 12:07:36 +00003221 dependenciesAttr := formattedDependenciesAttribute(module.properties.Uses_libs_dependencies)
Pedro Loureiro196d3e62021-12-22 19:53:01 +00003222 // <library> is understood in all android versions whereas <apex-library> is only understood from API T (and ignored before that).
3223 // 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 +00003224 var libraryTag string
3225 if module.properties.Min_device_sdk != nil {
Paul Duffin1816cde2024-04-10 10:58:21 +01003226 libraryTag = " <apex-library\n"
Pedro Loureiroc3621422021-09-28 15:40:23 +00003227 } else {
Paul Duffin1816cde2024-04-10 10:58:21 +01003228 libraryTag = " <library\n"
Pedro Loureiroc3621422021-09-28 15:40:23 +00003229 }
Pedro Loureiro9956e5e2021-09-07 17:21:59 +00003230
3231 return strings.Join([]string{
Paul Duffin1816cde2024-04-10 10:58:21 +01003232 "<?xml version=\"1.0\" encoding=\"utf-8\"?>\n",
3233 "<!-- Copyright (C) 2018 The Android Open Source Project\n",
3234 "\n",
3235 " Licensed under the Apache License, Version 2.0 (the \"License\");\n",
3236 " you may not use this file except in compliance with the License.\n",
3237 " You may obtain a copy of the License at\n",
3238 "\n",
3239 " http://www.apache.org/licenses/LICENSE-2.0\n",
3240 "\n",
3241 " Unless required by applicable law or agreed to in writing, software\n",
3242 " distributed under the License is distributed on an \"AS IS\" BASIS,\n",
3243 " WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\n",
3244 " See the License for the specific language governing permissions and\n",
3245 " limitations under the License.\n",
3246 "-->\n",
3247 "<permissions>\n",
Pedro Loureiroc3621422021-09-28 15:40:23 +00003248 libraryTag,
Pedro Loureiro9956e5e2021-09-07 17:21:59 +00003249 libNameAttr,
3250 filePathAttr,
3251 implicitFromAttr,
3252 implicitUntilAttr,
3253 minSdkAttr,
3254 maxSdkAttr,
Jamie Garsidee570ace2023-11-27 12:07:36 +00003255 dependenciesAttr,
Paul Duffin1816cde2024-04-10 10:58:21 +01003256 " />\n",
3257 "</permissions>\n",
3258 }, "")
Pedro Loureiro9956e5e2021-09-07 17:21:59 +00003259}
3260
Jiyong Parke3833882020-02-17 17:28:10 +09003261func (module *sdkLibraryXml) GenerateAndroidBuildActions(ctx android.ModuleContext) {
Colin Crossff694a82023-12-13 15:54:49 -08003262 apexInfo, _ := android.ModuleProvider(ctx, android.ApexInfoProvider)
3263 module.hideApexVariantFromMake = !apexInfo.IsForPlatform()
Colin Cross56a83212020-09-15 18:30:11 -07003264
Jiyong Parke3833882020-02-17 17:28:10 +09003265 libName := proptools.String(module.properties.Lib_name)
Pedro Loureiroc3621422021-09-28 15:40:23 +00003266 module.selfValidate(ctx)
Pedro Loureiro9956e5e2021-09-07 17:21:59 +00003267 xmlContent := module.permissionsContents(ctx)
Jiyong Parke3833882020-02-17 17:28:10 +09003268
3269 module.outputFilePath = android.PathForModuleOut(ctx, libName+".xml").OutputPath
Paul Duffin1816cde2024-04-10 10:58:21 +01003270 android.WriteFileRuleVerbatim(ctx, module.outputFilePath, xmlContent)
Jiyong Parke3833882020-02-17 17:28:10 +09003271
3272 module.installDirPath = android.PathForModuleInstall(ctx, "etc", module.SubDir())
Jeongik Cha00e09912024-04-23 05:07:13 +09003273 ctx.PackageFile(module.installDirPath, libName+".xml", module.outputFilePath)
mrziwange2346b82024-06-10 15:09:45 -07003274
3275 ctx.SetOutputFiles(android.OutputPaths{module.outputFilePath}.Paths(), "")
Jiyong Parke3833882020-02-17 17:28:10 +09003276}
3277
3278func (module *sdkLibraryXml) AndroidMkEntries() []android.AndroidMkEntries {
Colin Cross56a83212020-09-15 18:30:11 -07003279 if module.hideApexVariantFromMake {
satayev8f088b02021-12-06 11:40:46 +00003280 return []android.AndroidMkEntries{{
Jiyong Parke3833882020-02-17 17:28:10 +09003281 Disabled: true,
3282 }}
3283 }
3284
satayev8f088b02021-12-06 11:40:46 +00003285 return []android.AndroidMkEntries{{
Jiyong Parke3833882020-02-17 17:28:10 +09003286 Class: "ETC",
3287 OutputFile: android.OptionalPathForPath(module.outputFilePath),
3288 ExtraEntries: []android.AndroidMkExtraEntriesFunc{
Colin Crossaa255532020-07-03 13:18:24 -07003289 func(ctx android.AndroidMkExtraEntriesContext, entries *android.AndroidMkEntries) {
Jiyong Parke3833882020-02-17 17:28:10 +09003290 entries.SetString("LOCAL_MODULE_TAGS", "optional")
Colin Crossc68db4b2021-11-11 18:59:15 -08003291 entries.SetString("LOCAL_MODULE_PATH", module.installDirPath.String())
Jiyong Parke3833882020-02-17 17:28:10 +09003292 entries.SetString("LOCAL_INSTALLED_MODULE_STEM", module.outputFilePath.Base())
3293 },
3294 },
3295 }}
3296}
Paul Duffindd46f712020-02-10 13:37:10 +00003297
Pedro Loureiroc3621422021-09-28 15:40:23 +00003298func (module *sdkLibraryXml) selfValidate(ctx android.ModuleContext) {
3299 module.validateAtLeastTAttributes(ctx)
3300 module.validateMinAndMaxDeviceSdk(ctx)
3301 module.validateMinMaxDeviceSdkAndModuleMinSdk(ctx)
3302 module.validateOnBootclasspathBeforeRequirements(ctx)
3303}
3304
3305func (module *sdkLibraryXml) validateAtLeastTAttributes(ctx android.ModuleContext) {
3306 t := android.ApiLevelOrPanic(ctx, "Tiramisu")
3307 module.attrAtLeastT(ctx, t, module.properties.Min_device_sdk, "min_device_sdk")
3308 module.attrAtLeastT(ctx, t, module.properties.Max_device_sdk, "max_device_sdk")
3309 module.attrAtLeastT(ctx, t, module.properties.On_bootclasspath_before, "on_bootclasspath_before")
3310 module.attrAtLeastT(ctx, t, module.properties.On_bootclasspath_since, "on_bootclasspath_since")
3311}
3312
3313func (module *sdkLibraryXml) attrAtLeastT(ctx android.ModuleContext, t android.ApiLevel, attr *string, attrName string) {
3314 if attr != nil {
3315 if level, err := android.ApiLevelFromUser(ctx, *attr); err == nil {
3316 // we will inform the user of invalid inputs when we try to write the
3317 // permissions xml file so we don't need to do it here
3318 if t.GreaterThan(level) {
3319 ctx.PropertyErrorf(attrName, "Attribute value needs to be at least T")
3320 }
3321 }
3322 }
3323}
3324
3325func (module *sdkLibraryXml) validateMinAndMaxDeviceSdk(ctx android.ModuleContext) {
3326 if module.properties.Min_device_sdk != nil && module.properties.Max_device_sdk != nil {
3327 min, minErr := android.ApiLevelFromUser(ctx, *module.properties.Min_device_sdk)
3328 max, maxErr := android.ApiLevelFromUser(ctx, *module.properties.Max_device_sdk)
3329 if minErr == nil && maxErr == nil {
3330 // we will inform the user of invalid inputs when we try to write the
3331 // permissions xml file so we don't need to do it here
3332 if min.GreaterThan(max) {
3333 ctx.ModuleErrorf("min_device_sdk can't be greater than max_device_sdk")
3334 }
3335 }
3336 }
3337}
3338
3339func (module *sdkLibraryXml) validateMinMaxDeviceSdkAndModuleMinSdk(ctx android.ModuleContext) {
3340 moduleMinApi := android.ApiLevelOrPanic(ctx, *module.properties.Sdk_library_min_api_level)
3341 if module.properties.Min_device_sdk != nil {
3342 api, err := android.ApiLevelFromUser(ctx, *module.properties.Min_device_sdk)
3343 if err == nil {
3344 if moduleMinApi.GreaterThan(api) {
3345 ctx.PropertyErrorf("min_device_sdk", "Can't be less than module's min sdk (%s)", moduleMinApi)
3346 }
3347 }
3348 }
3349 if module.properties.Max_device_sdk != nil {
3350 api, err := android.ApiLevelFromUser(ctx, *module.properties.Max_device_sdk)
3351 if err == nil {
3352 if moduleMinApi.GreaterThan(api) {
3353 ctx.PropertyErrorf("max_device_sdk", "Can't be less than module's min sdk (%s)", moduleMinApi)
3354 }
3355 }
3356 }
3357}
3358
3359func (module *sdkLibraryXml) validateOnBootclasspathBeforeRequirements(ctx android.ModuleContext) {
3360 moduleMinApi := android.ApiLevelOrPanic(ctx, *module.properties.Sdk_library_min_api_level)
3361 if module.properties.On_bootclasspath_before != nil {
3362 t := android.ApiLevelOrPanic(ctx, "Tiramisu")
3363 // if we use the attribute, then we need to do this validation
3364 if moduleMinApi.LessThan(t) {
3365 // if minAPi is < T, then we need to have min_device_sdk (which only accepts T+)
3366 if module.properties.Min_device_sdk == nil {
3367 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")
3368 }
3369 }
3370 }
3371}
3372
Paul Duffindd46f712020-02-10 13:37:10 +00003373type sdkLibrarySdkMemberType struct {
3374 android.SdkMemberTypeBase
3375}
3376
Paul Duffin296701e2021-07-14 10:29:36 +01003377func (s *sdkLibrarySdkMemberType) AddDependencies(ctx android.SdkDependencyContext, dependencyTag blueprint.DependencyTag, names []string) {
3378 ctx.AddVariationDependencies(nil, dependencyTag, names...)
Paul Duffindd46f712020-02-10 13:37:10 +00003379}
3380
3381func (s *sdkLibrarySdkMemberType) IsInstance(module android.Module) bool {
3382 _, ok := module.(*SdkLibrary)
3383 return ok
3384}
3385
3386func (s *sdkLibrarySdkMemberType) AddPrebuiltModule(ctx android.SdkMemberContext, member android.SdkMember) android.BpModule {
3387 return ctx.SnapshotBuilder().AddPrebuiltModule(member, "java_sdk_library_import")
3388}
3389
3390func (s *sdkLibrarySdkMemberType) CreateVariantPropertiesStruct() android.SdkMemberProperties {
3391 return &sdkLibrarySdkMemberProperties{}
3392}
3393
Paul Duffin976b0e52021-04-27 23:20:26 +01003394var javaSdkLibrarySdkMemberType = &sdkLibrarySdkMemberType{
3395 android.SdkMemberTypeBase{
3396 PropertyName: "java_sdk_libs",
3397 SupportsSdk: true,
3398 },
3399}
3400
Paul Duffindd46f712020-02-10 13:37:10 +00003401type sdkLibrarySdkMemberProperties struct {
3402 android.SdkMemberPropertiesBase
3403
Paul Duffine8409952022-09-22 16:24:46 +01003404 // Stem name for files in the sdk snapshot.
3405 //
3406 // This is used to construct the path names of various sdk library files in the sdk snapshot to
3407 // make sure that they match the finalized versions of those files in prebuilts/sdk.
3408 //
3409 // This property is marked as keep so that it will be kept in all instances of this struct, will
3410 // not be cleared but will be copied to common structs. That is needed because this field is used
3411 // to construct many file names for other parts of this struct and so it needs to be present in
3412 // all structs. If it was not marked as keep then it would be cleared in some structs and so would
3413 // be unavailable for generating file names if there were other properties that were still set.
3414 Stem string `sdk:"keep"`
3415
Paul Duffindd46f712020-02-10 13:37:10 +00003416 // Scope to per scope properties.
Paul Duffin106a3a42022-01-27 16:39:06 +00003417 Scopes map[*apiScope]*scopeProperties
Paul Duffindd46f712020-02-10 13:37:10 +00003418
Paul Duffin3d1248c2020-04-09 00:10:17 +01003419 // The Java stubs source files.
3420 Stub_srcs []string
Paul Duffinf7a64332020-05-13 16:54:55 +01003421
3422 // The naming scheme.
3423 Naming_scheme *string
Paul Duffind7eb1c22020-05-26 20:57:10 +01003424
3425 // True if the java_sdk_library_import is for a shared library, false
3426 // otherwise.
3427 Shared_library *bool
Paul Duffina2ae7e02020-09-11 11:55:00 +01003428
Paul Duffin1267d872021-04-16 17:21:36 +01003429 // True if the stub imports should produce dex jars.
3430 Compile_dex *bool
3431
Paul Duffina2ae7e02020-09-11 11:55:00 +01003432 // The paths to the doctag files to add to the prebuilt.
3433 Doctag_paths android.Paths
Paul Duffin869de142021-07-15 14:14:41 +01003434
3435 Permitted_packages []string
Pedro Loureiro9956e5e2021-09-07 17:21:59 +00003436
3437 // Signals that this shared library is part of the bootclasspath starting
3438 // on the version indicated in this attribute.
3439 //
3440 // This will make platforms at this level and above to ignore
3441 // <uses-library> tags with this library name because the library is already
3442 // available
3443 On_bootclasspath_since *string
3444
3445 // Signals that this shared library was part of the bootclasspath before
3446 // (but not including) the version indicated in this attribute.
3447 //
3448 // The system will automatically add a <uses-library> tag with this library to
3449 // apps that target any SDK less than the version indicated in this attribute.
3450 On_bootclasspath_before *string
3451
3452 // Indicates that PackageManager should ignore this shared library if the
3453 // platform is below the version indicated in this attribute.
3454 //
3455 // This means that the device won't recognise this library as installed.
3456 Min_device_sdk *string
3457
3458 // Indicates that PackageManager should ignore this shared library if the
3459 // platform is above the version indicated in this attribute.
3460 //
3461 // This means that the device won't recognise this library as installed.
3462 Max_device_sdk *string
Jiakai Zhang9c4dc192023-02-09 00:09:24 +08003463
3464 DexPreoptProfileGuided *bool `supported_build_releases:"UpsideDownCake+"`
Paul Duffindd46f712020-02-10 13:37:10 +00003465}
3466
3467type scopeProperties struct {
Paul Duffin1fd005d2020-04-09 01:08:11 +01003468 Jars android.Paths
3469 StubsSrcJar android.Path
3470 CurrentApiFile android.Path
3471 RemovedApiFile android.Path
Paul Duffine7babdb2022-02-10 13:06:54 +00003472 AnnotationsZip android.Path `supported_build_releases:"Tiramisu+"`
Paul Duffin1fd005d2020-04-09 01:08:11 +01003473 SdkVersion string
Paul Duffindd46f712020-02-10 13:37:10 +00003474}
3475
3476func (s *sdkLibrarySdkMemberProperties) PopulateFromVariant(ctx android.SdkMemberContext, variant android.Module) {
3477 sdk := variant.(*SdkLibrary)
3478
Paul Duffine8409952022-09-22 16:24:46 +01003479 // Copy the stem name for files in the sdk snapshot.
3480 s.Stem = sdk.distStem()
3481
Paul Duffin106a3a42022-01-27 16:39:06 +00003482 s.Scopes = make(map[*apiScope]*scopeProperties)
Jihoon Kang98aa8fa2024-06-07 11:06:57 +00003483 for _, apiScope := range AllApiScopes {
Paul Duffin803a9562020-05-20 11:52:25 +01003484 paths := sdk.findScopePaths(apiScope)
3485 if paths == nil {
3486 continue
3487 }
3488
Paul Duffindd46f712020-02-10 13:37:10 +00003489 jars := paths.stubsImplPath
3490 if len(jars) > 0 {
3491 properties := scopeProperties{}
3492 properties.Jars = jars
Paul Duffin780c5f42020-05-12 15:52:55 +01003493 properties.SdkVersion = sdk.sdkVersionForStubsLibrary(ctx.SdkModuleContext(), apiScope)
Paul Duffin0f8faff2020-05-20 16:18:00 +01003494 properties.StubsSrcJar = paths.stubsSrcJar.Path()
Paul Duffin10269f12020-06-19 18:39:55 +01003495 if paths.currentApiFilePath.Valid() {
3496 properties.CurrentApiFile = paths.currentApiFilePath.Path()
3497 }
3498 if paths.removedApiFilePath.Valid() {
3499 properties.RemovedApiFile = paths.removedApiFilePath.Path()
3500 }
Anton Hanssond78eb762021-09-21 15:25:12 +01003501 // The annotations zip is only available for modules that set annotations_enabled: true.
3502 if paths.annotationsZip.Valid() {
3503 properties.AnnotationsZip = paths.annotationsZip.Path()
3504 }
Paul Duffin106a3a42022-01-27 16:39:06 +00003505 s.Scopes[apiScope] = &properties
Paul Duffindd46f712020-02-10 13:37:10 +00003506 }
3507 }
3508
Paul Duffind7eb1c22020-05-26 20:57:10 +01003509 s.Shared_library = proptools.BoolPtr(sdk.sharedLibrary())
Paul Duffin1267d872021-04-16 17:21:36 +01003510 s.Compile_dex = sdk.dexProperties.Compile_dex
Paul Duffina2ae7e02020-09-11 11:55:00 +01003511 s.Doctag_paths = sdk.doctagPaths
Paul Duffin869de142021-07-15 14:14:41 +01003512 s.Permitted_packages = sdk.PermittedPackagesForUpdatableBootJars()
Pedro Loureiro9956e5e2021-09-07 17:21:59 +00003513 s.On_bootclasspath_since = sdk.commonSdkLibraryProperties.On_bootclasspath_since
3514 s.On_bootclasspath_before = sdk.commonSdkLibraryProperties.On_bootclasspath_before
3515 s.Min_device_sdk = sdk.commonSdkLibraryProperties.Min_device_sdk
3516 s.Max_device_sdk = sdk.commonSdkLibraryProperties.Max_device_sdk
Jiakai Zhang9c4dc192023-02-09 00:09:24 +08003517
Jihoon Kanga3a05462024-04-05 00:36:44 +00003518 implLibrary := sdk.getImplLibraryModule()
3519 if implLibrary != nil && implLibrary.dexpreopter.dexpreoptProperties.Dex_preopt_result.Profile_guided {
Jiakai Zhang9c4dc192023-02-09 00:09:24 +08003520 s.DexPreoptProfileGuided = proptools.BoolPtr(true)
3521 }
Paul Duffindd46f712020-02-10 13:37:10 +00003522}
3523
3524func (s *sdkLibrarySdkMemberProperties) AddToPropertySet(ctx android.SdkMemberContext, propertySet android.BpPropertySet) {
Paul Duffinf7a64332020-05-13 16:54:55 +01003525 if s.Naming_scheme != nil {
3526 propertySet.AddProperty("naming_scheme", proptools.String(s.Naming_scheme))
3527 }
Paul Duffind7eb1c22020-05-26 20:57:10 +01003528 if s.Shared_library != nil {
3529 propertySet.AddProperty("shared_library", *s.Shared_library)
3530 }
Paul Duffin1267d872021-04-16 17:21:36 +01003531 if s.Compile_dex != nil {
3532 propertySet.AddProperty("compile_dex", *s.Compile_dex)
3533 }
Paul Duffin869de142021-07-15 14:14:41 +01003534 if len(s.Permitted_packages) > 0 {
3535 propertySet.AddProperty("permitted_packages", s.Permitted_packages)
3536 }
Jiakai Zhang9c4dc192023-02-09 00:09:24 +08003537 dexPreoptSet := propertySet.AddPropertySet("dex_preopt")
3538 if s.DexPreoptProfileGuided != nil {
3539 dexPreoptSet.AddProperty("profile_guided", proptools.Bool(s.DexPreoptProfileGuided))
3540 }
Paul Duffinf7a64332020-05-13 16:54:55 +01003541
Paul Duffine8409952022-09-22 16:24:46 +01003542 stem := s.Stem
3543
Jihoon Kang98aa8fa2024-06-07 11:06:57 +00003544 for _, apiScope := range AllApiScopes {
Paul Duffindd46f712020-02-10 13:37:10 +00003545 if properties, ok := s.Scopes[apiScope]; ok {
Paul Duffin6b836ba2020-05-13 19:19:49 +01003546 scopeSet := propertySet.AddPropertySet(apiScope.propertyName)
Paul Duffindd46f712020-02-10 13:37:10 +00003547
Paul Duffin958806b2022-05-16 13:10:47 +00003548 scopeDir := apiScope.snapshotRelativeDir()
Paul Duffin3d1248c2020-04-09 00:10:17 +01003549
Paul Duffindd46f712020-02-10 13:37:10 +00003550 var jars []string
3551 for _, p := range properties.Jars {
Paul Duffine8409952022-09-22 16:24:46 +01003552 dest := filepath.Join(scopeDir, stem+"-stubs.jar")
Paul Duffindd46f712020-02-10 13:37:10 +00003553 ctx.SnapshotBuilder().CopyToSnapshot(p, dest)
3554 jars = append(jars, dest)
3555 }
3556 scopeSet.AddProperty("jars", jars)
3557
Paul Duffin22628d52021-05-12 23:13:22 +01003558 if ctx.SdkModuleContext().Config().IsEnvTrue("SOONG_SDK_SNAPSHOT_USE_SRCJAR") {
3559 // Copy the stubs source jar into the snapshot zip as is.
Paul Duffine8409952022-09-22 16:24:46 +01003560 srcJarSnapshotPath := filepath.Join(scopeDir, stem+".srcjar")
Paul Duffin22628d52021-05-12 23:13:22 +01003561 ctx.SnapshotBuilder().CopyToSnapshot(properties.StubsSrcJar, srcJarSnapshotPath)
3562 scopeSet.AddProperty("stub_srcs", []string{srcJarSnapshotPath})
3563 } else {
3564 // Merge the stubs source jar into the snapshot zip so that when it is unpacked
3565 // the source files are also unpacked.
Paul Duffine8409952022-09-22 16:24:46 +01003566 snapshotRelativeDir := filepath.Join(scopeDir, stem+"_stub_sources")
Paul Duffin22628d52021-05-12 23:13:22 +01003567 ctx.SnapshotBuilder().UnzipToSnapshot(properties.StubsSrcJar, snapshotRelativeDir)
3568 scopeSet.AddProperty("stub_srcs", []string{snapshotRelativeDir})
3569 }
Paul Duffin3d1248c2020-04-09 00:10:17 +01003570
Paul Duffin1fd005d2020-04-09 01:08:11 +01003571 if properties.CurrentApiFile != nil {
Paul Duffine8409952022-09-22 16:24:46 +01003572 currentApiSnapshotPath := apiScope.snapshotRelativeCurrentApiTxtPath(stem)
Paul Duffin1fd005d2020-04-09 01:08:11 +01003573 ctx.SnapshotBuilder().CopyToSnapshot(properties.CurrentApiFile, currentApiSnapshotPath)
3574 scopeSet.AddProperty("current_api", currentApiSnapshotPath)
3575 }
3576
3577 if properties.RemovedApiFile != nil {
Paul Duffine8409952022-09-22 16:24:46 +01003578 removedApiSnapshotPath := apiScope.snapshotRelativeRemovedApiTxtPath(stem)
Paul Duffin3dbf9fd2020-06-02 13:00:02 +01003579 ctx.SnapshotBuilder().CopyToSnapshot(properties.RemovedApiFile, removedApiSnapshotPath)
Paul Duffin1fd005d2020-04-09 01:08:11 +01003580 scopeSet.AddProperty("removed_api", removedApiSnapshotPath)
3581 }
3582
Anton Hanssond78eb762021-09-21 15:25:12 +01003583 if properties.AnnotationsZip != nil {
Paul Duffine8409952022-09-22 16:24:46 +01003584 annotationsSnapshotPath := filepath.Join(scopeDir, stem+"_annotations.zip")
Anton Hanssond78eb762021-09-21 15:25:12 +01003585 ctx.SnapshotBuilder().CopyToSnapshot(properties.AnnotationsZip, annotationsSnapshotPath)
3586 scopeSet.AddProperty("annotations", annotationsSnapshotPath)
3587 }
3588
Paul Duffindd46f712020-02-10 13:37:10 +00003589 if properties.SdkVersion != "" {
3590 scopeSet.AddProperty("sdk_version", properties.SdkVersion)
3591 }
3592 }
3593 }
3594
Paul Duffina2ae7e02020-09-11 11:55:00 +01003595 if len(s.Doctag_paths) > 0 {
3596 dests := []string{}
3597 for _, p := range s.Doctag_paths {
3598 dest := filepath.Join("doctags", p.Rel())
3599 ctx.SnapshotBuilder().CopyToSnapshot(p, dest)
3600 dests = append(dests, dest)
3601 }
3602 propertySet.AddProperty("doctag_files", dests)
3603 }
Paul Duffindd46f712020-02-10 13:37:10 +00003604}
Spandan Dasdee1a742024-08-09 17:37:25 +00003605
3606// 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 -07003607func (s *SdkLibrary) IDEInfo(ctx android.BaseModuleContext, dpInfo *android.IdeInfo) {
3608 s.Library.IDEInfo(ctx, dpInfo)
Spandan Dasdee1a742024-08-09 17:37:25 +00003609 if s.implLibraryModule != nil {
3610 dpInfo.Deps = append(dpInfo.Deps, s.implLibraryModule.Name())
3611 } else {
3612 // This java_sdk_library does not have an implementation (it sets `api_only` to true).
3613 // Examples of this are `art.module.intra.core.api` (IntraCore api surface).
3614 // Return the "public" stubs for these.
3615 stubPaths := s.findClosestScopePath(apiScopePublic)
3616 if len(stubPaths.stubsHeaderPath) > 0 {
3617 dpInfo.Jars = append(dpInfo.Jars, stubPaths.stubsHeaderPath[0].String())
3618 }
3619 }
3620}