blob: 5ee713c153a3f295ad7beef46cfab77ee94619cd [file] [log] [blame]
Jiyong Parkc678ad32018-04-10 13:07:10 +09001// Copyright 2018 Google Inc. All rights reserved.
2//
3// Licensed under the Apache License, Version 2.0 (the "License");
4// you may not use this file except in compliance with the License.
5// You may obtain a copy of the License at
6//
7// http://www.apache.org/licenses/LICENSE-2.0
8//
9// Unless required by applicable law or agreed to in writing, software
10// distributed under the License is distributed on an "AS IS" BASIS,
11// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12// See the License for the specific language governing permissions and
13// limitations under the License.
14
15package java
16
17import (
Jihoon Kangee113282024-01-23 00:16:41 +000018 "errors"
Jiyong Parkc678ad32018-04-10 13:07:10 +090019 "fmt"
20 "path"
Sundong Ahn054b19a2018-10-19 13:46:09 +090021 "path/filepath"
Paul Duffin46a26a82020-04-07 19:27:04 +010022 "reflect"
Paul Duffin46dc45a2020-05-14 15:39:10 +010023 "regexp"
Jiyong Park82484c02018-04-23 21:41:26 +090024 "sort"
Jiyong Parkc678ad32018-04-10 13:07:10 +090025 "strings"
Jiyong Park82484c02018-04-23 21:41:26 +090026 "sync"
Jiyong Parkc678ad32018-04-10 13:07:10 +090027
Paul Duffind1b3a922020-01-22 11:57:20 +000028 "github.com/google/blueprint"
Jiyong Parkc678ad32018-04-10 13:07:10 +090029 "github.com/google/blueprint/proptools"
Paul Duffin46a26a82020-04-07 19:27:04 +010030
31 "android/soong/android"
Ulya Trafimovichdbf31662020-12-17 12:07:54 +000032 "android/soong/dexpreopt"
Jiyong Parkc678ad32018-04-10 13:07:10 +090033)
34
Jooyung Han58f26ab2019-12-18 15:34:32 +090035const (
Pedro Loureiro9956e5e2021-09-07 17:21:59 +000036 sdkXmlFileSuffix = ".xml"
Jiyong Parkc678ad32018-04-10 13:07:10 +090037)
38
Paul Duffind1b3a922020-01-22 11:57:20 +000039// A tag to associated a dependency with a specific api scope.
40type scopeDependencyTag struct {
41 blueprint.BaseDependencyTag
42 name string
43 apiScope *apiScope
Paul Duffinc8782502020-04-29 20:45:27 +010044
45 // Function for extracting appropriate path information from the dependency.
Colin Crossdcf71b22021-02-01 13:59:03 -080046 depInfoExtractor func(paths *scopePaths, ctx android.ModuleContext, dep android.Module) error
Paul Duffinc8782502020-04-29 20:45:27 +010047}
48
49// Extract tag specific information from the dependency.
50func (tag scopeDependencyTag) extractDepInfo(ctx android.ModuleContext, dep android.Module, paths *scopePaths) {
Colin Crossdcf71b22021-02-01 13:59:03 -080051 err := tag.depInfoExtractor(paths, ctx, dep)
Paul Duffinc8782502020-04-29 20:45:27 +010052 if err != nil {
53 ctx.ModuleErrorf("has an invalid {scopeDependencyTag: %s} dependency on module %s: %s", tag.name, ctx.OtherModuleName(dep), err.Error())
54 }
Paul Duffind1b3a922020-01-22 11:57:20 +000055}
56
Paul Duffin80342d72020-06-26 22:08:43 +010057var _ android.ReplaceSourceWithPrebuilt = (*scopeDependencyTag)(nil)
58
59func (tag scopeDependencyTag) ReplaceSourceWithPrebuilt() bool {
60 return false
61}
62
Paul Duffind1b3a922020-01-22 11:57:20 +000063// Provides information about an api scope, e.g. public, system, test.
64type apiScope struct {
65 // The name of the api scope, e.g. public, system, test
66 name string
67
Paul Duffin97b53b82020-05-05 14:40:52 +010068 // The api scope that this scope extends.
Paul Duffind0b9fca2022-09-30 18:11:41 +010069 //
70 // This organizes the scopes into an extension hierarchy.
71 //
72 // If set this means that the API provided by this scope includes the API provided by the scope
73 // set in this field.
Paul Duffin97b53b82020-05-05 14:40:52 +010074 extends *apiScope
75
Paul Duffind0b9fca2022-09-30 18:11:41 +010076 // The next api scope that a library that uses this scope can access.
77 //
78 // This organizes the scopes into an access hierarchy.
79 //
80 // If set this means that a library that can access this API can also access the API provided by
81 // the scope set in this field.
82 //
83 // A module that sets sdk_version: "<scope>_current" should have access to the <scope> API of
84 // every java_sdk_library that it depends on. If the library does not provide an API for <scope>
85 // then it will traverse up this access hierarchy to find an API that it does provide.
86 //
87 // If this is not set then it defaults to the scope set in extends.
88 canAccess *apiScope
89
Paul Duffin3375e352020-04-28 10:44:03 +010090 // The legacy enabled status for a specific scope can be dependent on other
91 // properties that have been specified on the library so it is provided by
92 // a function that can determine the status by examining those properties.
93 legacyEnabledStatus func(module *SdkLibrary) bool
94
95 // The default enabled status for non-legacy behavior, which is triggered by
96 // explicitly enabling at least one api scope.
97 defaultEnabledStatus bool
98
99 // Gets a pointer to the scope specific properties.
100 scopeSpecificProperties func(module *SdkLibrary) *ApiScopeProperties
101
Paul Duffin46a26a82020-04-07 19:27:04 +0100102 // The name of the field in the dynamically created structure.
103 fieldName string
104
Paul Duffin6b836ba2020-05-13 19:19:49 +0100105 // The name of the property in the java_sdk_library_import
106 propertyName string
107
Jihoon Kangb7431552024-01-22 19:40:08 +0000108 // The tag to use to depend on the prebuilt stubs library module
109 prebuiltStubsTag scopeDependencyTag
Paul Duffind1b3a922020-01-22 11:57:20 +0000110
Jihoon Kangbd093452023-12-26 19:08:01 +0000111 // The tag to use to depend on the everything stubs library module.
112 everythingStubsTag scopeDependencyTag
113
114 // The tag to use to depend on the exportable stubs library module.
115 exportableStubsTag scopeDependencyTag
116
Paul Duffin0ff08bd2020-04-29 13:30:54 +0100117 // The tag to use to depend on the stubs source module (if separate from the API module).
118 stubsSourceTag scopeDependencyTag
119
120 // The tag to use to depend on the API file generating module (if separate from the stubs source module).
121 apiFileTag scopeDependencyTag
122
Paul Duffinc8782502020-04-29 20:45:27 +0100123 // The tag to use to depend on the stubs source and API module.
124 stubsSourceAndApiTag scopeDependencyTag
Paul Duffind1b3a922020-01-22 11:57:20 +0000125
Paul Duffin958806b2022-05-16 13:10:47 +0000126 // The tag to use to depend on the module that provides the latest version of the API .txt file.
127 latestApiModuleTag scopeDependencyTag
128
129 // The tag to use to depend on the module that provides the latest version of the API removed.txt
130 // file.
131 latestRemovedApiModuleTag scopeDependencyTag
132
Paul Duffind1b3a922020-01-22 11:57:20 +0000133 // The scope specific prefix to add to the api file base of "current.txt" or "removed.txt".
134 apiFilePrefix string
135
Paul Duffind0b9fca2022-09-30 18:11:41 +0100136 // The scope specific suffix to add to the sdk library module name to construct a scope specific
Paul Duffind1b3a922020-01-22 11:57:20 +0000137 // module name.
138 moduleSuffix string
139
Paul Duffind1b3a922020-01-22 11:57:20 +0000140 // SDK version that the stubs library is built against. Note that this is always
141 // *current. Older stubs library built with a numbered SDK version is created from
142 // the prebuilt jar.
143 sdkVersion string
Paul Duffin1fb487d2020-04-07 18:50:10 +0100144
Paul Duffin15f34ef2020-07-20 18:04:44 +0100145 // The annotation that identifies this API level, empty for the public API scope.
146 annotation string
147
Paul Duffin1fb487d2020-04-07 18:50:10 +0100148 // Extra arguments to pass to droidstubs for this scope.
Paul Duffin0ff08bd2020-04-29 13:30:54 +0100149 //
Paul Duffin15f34ef2020-07-20 18:04:44 +0100150 // This is not used directly but is used to construct the droidstubsArgs.
151 extraArgs []string
Paul Duffin0ff08bd2020-04-29 13:30:54 +0100152
Paul Duffin15f34ef2020-07-20 18:04:44 +0100153 // The args that must be passed to droidstubs to generate the API and stubs source
154 // for this scope, constructed dynamically by initApiScope().
Paul Duffin0ff08bd2020-04-29 13:30:54 +0100155 //
156 // The API only includes the additional members that this scope adds over the scope
157 // that it extends.
Paul Duffin15f34ef2020-07-20 18:04:44 +0100158 //
159 // The stubs source must include the definitions of everything that is in this
160 // api scope and all the scopes that this one extends.
161 droidstubsArgs []string
Paul Duffin0ff08bd2020-04-29 13:30:54 +0100162
Anton Hansson6478ac12020-05-02 11:19:36 +0100163 // Whether the api scope can be treated as unstable, and should skip compat checks.
164 unstable bool
Jihoon Kang1c92c3e2023-03-23 17:44:51 +0000165
166 // Represents the SDK kind of this scope.
167 kind android.SdkKind
Paul Duffind1b3a922020-01-22 11:57:20 +0000168}
169
170// Initialize a scope, creating and adding appropriate dependency tags
171func initApiScope(scope *apiScope) *apiScope {
Paul Duffinc8782502020-04-29 20:45:27 +0100172 name := scope.name
Paul Duffin46dc45a2020-05-14 15:39:10 +0100173 scopeByName[name] = scope
174 allScopeNames = append(allScopeNames, name)
Paul Duffin6b836ba2020-05-13 19:19:49 +0100175 scope.propertyName = strings.ReplaceAll(name, "-", "_")
176 scope.fieldName = proptools.FieldNameForProperty(scope.propertyName)
Jihoon Kangb7431552024-01-22 19:40:08 +0000177 scope.prebuiltStubsTag = scopeDependencyTag{
Paul Duffinc8782502020-04-29 20:45:27 +0100178 name: name + "-stubs",
179 apiScope: scope,
180 depInfoExtractor: (*scopePaths).extractStubsLibraryInfoFromDependency,
Paul Duffind1b3a922020-01-22 11:57:20 +0000181 }
Jihoon Kangbd093452023-12-26 19:08:01 +0000182 scope.everythingStubsTag = scopeDependencyTag{
183 name: name + "-stubs-everything",
184 apiScope: scope,
185 depInfoExtractor: (*scopePaths).extractEverythingStubsLibraryInfoFromDependency,
186 }
187 scope.exportableStubsTag = scopeDependencyTag{
188 name: name + "-stubs-exportable",
189 apiScope: scope,
190 depInfoExtractor: (*scopePaths).extractExportableStubsLibraryInfoFromDependency,
191 }
Paul Duffin0ff08bd2020-04-29 13:30:54 +0100192 scope.stubsSourceTag = scopeDependencyTag{
193 name: name + "-stubs-source",
194 apiScope: scope,
195 depInfoExtractor: (*scopePaths).extractStubsSourceInfoFromDep,
196 }
197 scope.apiFileTag = scopeDependencyTag{
198 name: name + "-api",
199 apiScope: scope,
200 depInfoExtractor: (*scopePaths).extractApiInfoFromDep,
201 }
Paul Duffinc8782502020-04-29 20:45:27 +0100202 scope.stubsSourceAndApiTag = scopeDependencyTag{
203 name: name + "-stubs-source-and-api",
204 apiScope: scope,
205 depInfoExtractor: (*scopePaths).extractStubsSourceAndApiInfoFromApiStubsProvider,
Paul Duffind1b3a922020-01-22 11:57:20 +0000206 }
Paul Duffin958806b2022-05-16 13:10:47 +0000207 scope.latestApiModuleTag = scopeDependencyTag{
208 name: name + "-latest-api",
209 apiScope: scope,
210 depInfoExtractor: (*scopePaths).extractLatestApiPath,
211 }
212 scope.latestRemovedApiModuleTag = scopeDependencyTag{
213 name: name + "-latest-removed-api",
214 apiScope: scope,
215 depInfoExtractor: (*scopePaths).extractLatestRemovedApiPath,
216 }
Paul Duffin0ff08bd2020-04-29 13:30:54 +0100217
218 // To get the args needed to generate the stubs source append all the args from
219 // this scope and all the scopes it extends as each set of args adds additional
220 // members to the stubs.
Paul Duffin15f34ef2020-07-20 18:04:44 +0100221 var scopeSpecificArgs []string
222 if scope.annotation != "" {
223 scopeSpecificArgs = []string{"--show-annotation", scope.annotation}
Paul Duffin0ff08bd2020-04-29 13:30:54 +0100224 }
Paul Duffin15f34ef2020-07-20 18:04:44 +0100225 for s := scope; s != nil; s = s.extends {
226 scopeSpecificArgs = append(scopeSpecificArgs, s.extraArgs...)
Paul Duffin0ff08bd2020-04-29 13:30:54 +0100227
Paul Duffin15f34ef2020-07-20 18:04:44 +0100228 // Ensure that the generated stubs includes all the API elements from the API scope
229 // that this scope extends.
230 if s != scope && s.annotation != "" {
231 scopeSpecificArgs = append(scopeSpecificArgs, "--show-for-stub-purposes-annotation", s.annotation)
232 }
233 }
Paul Duffin0ff08bd2020-04-29 13:30:54 +0100234
Paul Duffind0b9fca2022-09-30 18:11:41 +0100235 // By default, a library that can access a scope can also access the scope it extends.
236 if scope.canAccess == nil {
237 scope.canAccess = scope.extends
238 }
239
Paul Duffin15f34ef2020-07-20 18:04:44 +0100240 // Escape any special characters in the arguments. This is needed because droidstubs
241 // passes these directly to the shell command.
242 scope.droidstubsArgs = proptools.ShellEscapeList(scopeSpecificArgs)
Paul Duffin0ff08bd2020-04-29 13:30:54 +0100243
Paul Duffind1b3a922020-01-22 11:57:20 +0000244 return scope
245}
246
Anton Hansson08f476b2021-04-07 15:32:19 +0100247func (scope *apiScope) stubsLibraryModuleNameSuffix() string {
248 return ".stubs" + scope.moduleSuffix
249}
250
Jihoon Kangfa4a90d2023-12-20 02:53:38 +0000251func (scope *apiScope) exportableStubsLibraryModuleNameSuffix() string {
252 return ".stubs.exportable" + scope.moduleSuffix
253}
254
Jihoon Kang1c92c3e2023-03-23 17:44:51 +0000255func (scope *apiScope) apiLibraryModuleName(baseName string) string {
256 return scope.stubsLibraryModuleName(baseName) + ".from-text"
257}
258
Jihoon Kang1147b312023-06-08 23:25:57 +0000259func (scope *apiScope) sourceStubLibraryModuleName(baseName string) string {
260 return scope.stubsLibraryModuleName(baseName) + ".from-source"
261}
262
Jihoon Kangfa4a90d2023-12-20 02:53:38 +0000263func (scope *apiScope) exportableSourceStubsLibraryModuleName(baseName string) string {
264 return scope.exportableStubsLibraryModuleName(baseName) + ".from-source"
265}
266
Paul Duffinc3091c82020-05-08 14:16:20 +0100267func (scope *apiScope) stubsLibraryModuleName(baseName string) string {
Anton Hansson08f476b2021-04-07 15:32:19 +0100268 return baseName + scope.stubsLibraryModuleNameSuffix()
Paul Duffind1b3a922020-01-22 11:57:20 +0000269}
270
Jihoon Kangfa4a90d2023-12-20 02:53:38 +0000271func (scope *apiScope) exportableStubsLibraryModuleName(baseName string) string {
272 return baseName + scope.exportableStubsLibraryModuleNameSuffix()
273}
274
Paul Duffinc8782502020-04-29 20:45:27 +0100275func (scope *apiScope) stubsSourceModuleName(baseName string) string {
Paul Duffindd9d0742020-05-08 15:52:37 +0100276 return baseName + ".stubs.source" + scope.moduleSuffix
Paul Duffind1b3a922020-01-22 11:57:20 +0000277}
278
Paul Duffin0ff08bd2020-04-29 13:30:54 +0100279func (scope *apiScope) apiModuleName(baseName string) string {
Paul Duffindd9d0742020-05-08 15:52:37 +0100280 return baseName + ".api" + scope.moduleSuffix
Paul Duffin0ff08bd2020-04-29 13:30:54 +0100281}
282
Paul Duffin3375e352020-04-28 10:44:03 +0100283func (scope *apiScope) String() string {
284 return scope.name
285}
286
Paul Duffin958806b2022-05-16 13:10:47 +0000287// snapshotRelativeDir returns the snapshot directory into which the files related to scopes will
288// be stored.
289func (scope *apiScope) snapshotRelativeDir() string {
290 return filepath.Join("sdk_library", scope.name)
291}
292
293// snapshotRelativeCurrentApiTxtPath returns the snapshot path to the API .txt file for the named
294// library.
295func (scope *apiScope) snapshotRelativeCurrentApiTxtPath(name string) string {
296 return filepath.Join(scope.snapshotRelativeDir(), name+".txt")
297}
298
299// snapshotRelativeRemovedApiTxtPath returns the snapshot path to the removed API .txt file for the
300// named library.
301func (scope *apiScope) snapshotRelativeRemovedApiTxtPath(name string) string {
302 return filepath.Join(scope.snapshotRelativeDir(), name+"-removed.txt")
303}
304
Paul Duffind1b3a922020-01-22 11:57:20 +0000305type apiScopes []*apiScope
306
307func (scopes apiScopes) Strings(accessor func(*apiScope) string) []string {
308 var list []string
309 for _, scope := range scopes {
310 list = append(list, accessor(scope))
311 }
312 return list
313}
314
Jihoon Kanga96a7b12023-09-20 23:43:32 +0000315// Method that maps the apiScopes properties to the index of each apiScopes elements.
316// apiScopes property to be used as the key can be specified with the input accessor.
317// Only a string property of apiScope can be used as the key of the map.
318func (scopes apiScopes) MapToIndex(accessor func(*apiScope) string) map[string]int {
319 ret := make(map[string]int)
320 for i, scope := range scopes {
321 ret[accessor(scope)] = i
322 }
323 return ret
324}
325
Jiyong Parkc678ad32018-04-10 13:07:10 +0900326var (
Paul Duffin46dc45a2020-05-14 15:39:10 +0100327 scopeByName = make(map[string]*apiScope)
328 allScopeNames []string
Paul Duffind1b3a922020-01-22 11:57:20 +0000329 apiScopePublic = initApiScope(&apiScope{
Paul Duffin3375e352020-04-28 10:44:03 +0100330 name: "public",
331
332 // Public scope is enabled by default for both legacy and non-legacy modes.
333 legacyEnabledStatus: func(module *SdkLibrary) bool {
334 return true
335 },
336 defaultEnabledStatus: true,
337
338 scopeSpecificProperties: func(module *SdkLibrary) *ApiScopeProperties {
339 return &module.sdkLibraryProperties.Public
340 },
Paul Duffind1b3a922020-01-22 11:57:20 +0000341 sdkVersion: "current",
Jihoon Kang1c92c3e2023-03-23 17:44:51 +0000342 kind: android.SdkPublic,
Paul Duffind1b3a922020-01-22 11:57:20 +0000343 })
344 apiScopeSystem = initApiScope(&apiScope{
Paul Duffin3375e352020-04-28 10:44:03 +0100345 name: "system",
346 extends: apiScopePublic,
347 legacyEnabledStatus: (*SdkLibrary).generateTestAndSystemScopesByDefault,
348 scopeSpecificProperties: func(module *SdkLibrary) *ApiScopeProperties {
349 return &module.sdkLibraryProperties.System
350 },
Paul Duffin15f34ef2020-07-20 18:04:44 +0100351 apiFilePrefix: "system-",
352 moduleSuffix: ".system",
353 sdkVersion: "system_current",
354 annotation: "android.annotation.SystemApi(client=android.annotation.SystemApi.Client.PRIVILEGED_APPS)",
Jihoon Kang1c92c3e2023-03-23 17:44:51 +0000355 kind: android.SdkSystem,
Paul Duffind1b3a922020-01-22 11:57:20 +0000356 })
357 apiScopeTest = initApiScope(&apiScope{
Paul Duffin3375e352020-04-28 10:44:03 +0100358 name: "test",
Anton Hansson4fe970f2020-10-09 10:16:49 +0100359 extends: apiScopeSystem,
Paul Duffin3375e352020-04-28 10:44:03 +0100360 legacyEnabledStatus: (*SdkLibrary).generateTestAndSystemScopesByDefault,
361 scopeSpecificProperties: func(module *SdkLibrary) *ApiScopeProperties {
362 return &module.sdkLibraryProperties.Test
363 },
Paul Duffin15f34ef2020-07-20 18:04:44 +0100364 apiFilePrefix: "test-",
365 moduleSuffix: ".test",
366 sdkVersion: "test_current",
367 annotation: "android.annotation.TestApi",
368 unstable: true,
Jihoon Kang1c92c3e2023-03-23 17:44:51 +0000369 kind: android.SdkTest,
Paul Duffind1b3a922020-01-22 11:57:20 +0000370 })
Paul Duffin8f265b92020-04-28 14:13:56 +0100371 apiScopeModuleLib = initApiScope(&apiScope{
Paul Duffin6b836ba2020-05-13 19:19:49 +0100372 name: "module-lib",
Paul Duffin8f265b92020-04-28 14:13:56 +0100373 extends: apiScopeSystem,
Paul Duffin0c5bae52020-06-02 13:00:08 +0100374 // The module-lib scope is disabled by default in legacy mode.
Paul Duffin8f265b92020-04-28 14:13:56 +0100375 //
376 // Enabling this would break existing usages.
377 legacyEnabledStatus: func(module *SdkLibrary) bool {
378 return false
379 },
380 scopeSpecificProperties: func(module *SdkLibrary) *ApiScopeProperties {
381 return &module.sdkLibraryProperties.Module_lib
382 },
383 apiFilePrefix: "module-lib-",
384 moduleSuffix: ".module_lib",
385 sdkVersion: "module_current",
Paul Duffin15f34ef2020-07-20 18:04:44 +0100386 annotation: "android.annotation.SystemApi(client=android.annotation.SystemApi.Client.MODULE_LIBRARIES)",
Jihoon Kang1c92c3e2023-03-23 17:44:51 +0000387 kind: android.SdkModule,
Paul Duffin8f265b92020-04-28 14:13:56 +0100388 })
Paul Duffin0c5bae52020-06-02 13:00:08 +0100389 apiScopeSystemServer = initApiScope(&apiScope{
390 name: "system-server",
391 extends: apiScopePublic,
Paul Duffind0b9fca2022-09-30 18:11:41 +0100392
393 // The system-server scope can access the module-lib scope.
394 //
395 // A module that provides a system-server API is appended to the standard bootclasspath that is
396 // used by the system server. So, it should be able to access module-lib APIs provided by
397 // libraries on the bootclasspath.
398 canAccess: apiScopeModuleLib,
399
Paul Duffin0c5bae52020-06-02 13:00:08 +0100400 // The system-server scope is disabled by default in legacy mode.
401 //
402 // Enabling this would break existing usages.
403 legacyEnabledStatus: func(module *SdkLibrary) bool {
404 return false
405 },
406 scopeSpecificProperties: func(module *SdkLibrary) *ApiScopeProperties {
407 return &module.sdkLibraryProperties.System_server
408 },
409 apiFilePrefix: "system-server-",
410 moduleSuffix: ".system_server",
411 sdkVersion: "system_server_current",
Paul Duffin15f34ef2020-07-20 18:04:44 +0100412 annotation: "android.annotation.SystemApi(client=android.annotation.SystemApi.Client.SYSTEM_SERVER)",
413 extraArgs: []string{
414 "--hide-annotation", "android.annotation.Hide",
Paul Duffin0c5bae52020-06-02 13:00:08 +0100415 // com.android.* classes are okay in this interface"
Paul Duffin15f34ef2020-07-20 18:04:44 +0100416 "--hide", "InternalClasses",
Paul Duffin0c5bae52020-06-02 13:00:08 +0100417 },
Jihoon Kang1c92c3e2023-03-23 17:44:51 +0000418 kind: android.SdkSystemServer,
Paul Duffin0c5bae52020-06-02 13:00:08 +0100419 })
Paul Duffind1b3a922020-01-22 11:57:20 +0000420 allApiScopes = apiScopes{
421 apiScopePublic,
422 apiScopeSystem,
423 apiScopeTest,
Paul Duffin8f265b92020-04-28 14:13:56 +0100424 apiScopeModuleLib,
Paul Duffin0c5bae52020-06-02 13:00:08 +0100425 apiScopeSystemServer,
Paul Duffind1b3a922020-01-22 11:57:20 +0000426 }
Jihoon Kang0c705a42023-08-02 06:44:57 +0000427 apiLibraryAdditionalProperties = map[string]struct {
428 FullApiSurfaceStubLib string
429 AdditionalApiContribution string
430 }{
431 "legacy.i18n.module.platform.api": {
432 FullApiSurfaceStubLib: "legacy.core.platform.api.stubs",
433 AdditionalApiContribution: "i18n.module.public.api.stubs.source.api.contribution",
434 },
435 "stable.i18n.module.platform.api": {
436 FullApiSurfaceStubLib: "stable.core.platform.api.stubs",
437 AdditionalApiContribution: "i18n.module.public.api.stubs.source.api.contribution",
438 },
439 "conscrypt.module.platform.api": {
440 FullApiSurfaceStubLib: "stable.core.platform.api.stubs",
441 AdditionalApiContribution: "conscrypt.module.public.api.stubs.source.api.contribution",
442 },
443 }
Jiyong Parkc678ad32018-04-10 13:07:10 +0900444)
445
Jiyong Park82484c02018-04-23 21:41:26 +0900446var (
447 javaSdkLibrariesLock sync.Mutex
448)
449
Jiyong Parkc678ad32018-04-10 13:07:10 +0900450// TODO: these are big features that are currently missing
Jiyong Park1be96912018-05-28 18:02:19 +0900451// 1) disallowing linking to the runtime shared lib
452// 2) HTML generation
Jiyong Parkc678ad32018-04-10 13:07:10 +0900453
454func init() {
Paul Duffin43dc1cc2019-12-19 11:18:54 +0000455 RegisterSdkLibraryBuildComponents(android.InitRegistrationContext)
Jiyong Parkc678ad32018-04-10 13:07:10 +0900456
Jiyong Park82484c02018-04-23 21:41:26 +0900457 android.RegisterMakeVarsProvider(pctx, func(ctx android.MakeVarsContext) {
458 javaSdkLibraries := javaSdkLibraries(ctx.Config())
459 sort.Strings(*javaSdkLibraries)
460 ctx.Strict("JAVA_SDK_LIBRARIES", strings.Join(*javaSdkLibraries, " "))
461 })
Paul Duffindd46f712020-02-10 13:37:10 +0000462
463 // Register sdk member types.
Paul Duffin976b0e52021-04-27 23:20:26 +0100464 android.RegisterSdkMemberType(javaSdkLibrarySdkMemberType)
Jiyong Parkc678ad32018-04-10 13:07:10 +0900465}
466
Paul Duffin43dc1cc2019-12-19 11:18:54 +0000467func RegisterSdkLibraryBuildComponents(ctx android.RegistrationContext) {
468 ctx.RegisterModuleType("java_sdk_library", SdkLibraryFactory)
469 ctx.RegisterModuleType("java_sdk_library_import", sdkLibraryImportFactory)
470}
471
Paul Duffin3375e352020-04-28 10:44:03 +0100472// Properties associated with each api scope.
473type ApiScopeProperties struct {
474 // Indicates whether the api surface is generated.
475 //
476 // If this is set for any scope then all scopes must explicitly specify if they
477 // are enabled. This is to prevent new usages from depending on legacy behavior.
478 //
479 // Otherwise, if this is not set for any scope then the default behavior is
480 // scope specific so please refer to the scope specific property documentation.
481 Enabled *bool
Paul Duffin87a05a32020-05-12 11:50:28 +0100482
483 // The sdk_version to use for building the stubs.
484 //
485 // If not specified then it will use an sdk_version determined as follows:
Trevor Radcliffedf8aa1f2021-11-04 14:25:39 +0000486 //
Paul Duffin87a05a32020-05-12 11:50:28 +0100487 // 1) If the sdk_version specified on the java_sdk_library is none then this
Trevor Radcliffedf8aa1f2021-11-04 14:25:39 +0000488 // will be none. This is used for java_sdk_library instances that are used
489 // to create stubs that contribute to the core_current sdk version.
490 // 2) Otherwise, it is assumed that this library extends but does not
491 // contribute directly to a specific sdk_version and so this uses the
492 // sdk_version appropriate for the api scope. e.g. public will use
493 // sdk_version: current, system will use sdk_version: system_current, etc.
Paul Duffin87a05a32020-05-12 11:50:28 +0100494 //
495 // This does not affect the sdk_version used for either generating the stubs source
496 // or the API file. They both have to use the same sdk_version as is used for
497 // compiling the implementation library.
498 Sdk_version *string
Mark White9421c4c2023-08-10 00:07:03 +0000499
500 // Extra libs used when compiling stubs for this scope.
501 Libs []string
Paul Duffin3375e352020-04-28 10:44:03 +0100502}
503
Jiyong Parkc678ad32018-04-10 13:07:10 +0900504type sdkLibraryProperties struct {
Anton Hanssonf8ea3722021-09-16 14:24:13 +0100505 // List of source files that are needed to compile the API, but are not part of runtime library.
506 Api_srcs []string `android:"arch_variant"`
507
Paul Duffin5df79302020-05-16 15:52:12 +0100508 // Visibility for impl library module. If not specified then defaults to the
509 // visibility property.
510 Impl_library_visibility []string
511
Paul Duffin4911a892020-04-29 23:35:13 +0100512 // Visibility for stubs library modules. If not specified then defaults to the
513 // visibility property.
514 Stubs_library_visibility []string
515
516 // Visibility for stubs source modules. If not specified then defaults to the
517 // visibility property.
518 Stubs_source_visibility []string
519
Anton Hansson7f66efa2020-10-08 14:47:23 +0100520 // List of Java libraries that will be in the classpath when building the implementation lib
521 Impl_only_libs []string `android:"arch_variant"`
522
Paul Duffin77590a82022-04-28 14:13:30 +0000523 // List of Java libraries that will included in the implementation lib.
524 Impl_only_static_libs []string `android:"arch_variant"`
525
Sundong Ahnf043cf62018-06-25 16:04:37 +0900526 // List of Java libraries that will be in the classpath when building stubs
527 Stub_only_libs []string `android:"arch_variant"`
528
Anton Hanssondae54cd2021-04-21 16:30:10 +0100529 // List of Java libraries that will included in stub libraries
530 Stub_only_static_libs []string `android:"arch_variant"`
531
Paul Duffin7a586d32019-12-30 17:09:34 +0000532 // list of package names that will be documented and publicized as API.
533 // This allows the API to be restricted to a subset of the source files provided.
534 // If this is unspecified then all the source files will be treated as being part
535 // of the API.
Jiyong Parkc678ad32018-04-10 13:07:10 +0900536 Api_packages []string
537
Jiyong Park5a2c9d72018-05-01 22:25:41 +0900538 // list of package names that must be hidden from the API
539 Hidden_api_packages []string
540
Paul Duffin749f98f2019-12-30 17:23:46 +0000541 // the relative path to the directory containing the api specification files.
542 // Defaults to "api".
543 Api_dir *string
544
Paul Duffindfa131e2020-05-15 20:37:11 +0100545 // Determines whether a runtime implementation library is built; defaults to false.
546 //
547 // If true then it also prevents the module from being used as a shared module, i.e.
MÃ¥rten Kongstad81d90952022-05-25 16:27:11 +0200548 // it is as if shared_library: false, was set.
Paul Duffin43db9be2019-12-30 17:35:49 +0000549 Api_only *bool
550
Paul Duffin11512472019-02-11 15:55:17 +0000551 // local files that are used within user customized droiddoc options.
552 Droiddoc_option_files []string
553
Spandan Das93e95992021-07-29 18:26:39 +0000554 // additional droiddoc options.
Paul Duffin11512472019-02-11 15:55:17 +0000555 // Available variables for substitution:
556 //
557 // $(location <label>): the path to the droiddoc_option_files with name <label>
Sundong Ahndd567f92018-07-31 17:19:11 +0900558 Droiddoc_options []string
559
Paul Duffine22c2ab2020-05-20 19:35:27 +0100560 // is set to true, Metalava will allow framework SDK to contain annotations.
561 Annotations_enabled *bool
562
Sundong Ahn054b19a2018-10-19 13:46:09 +0900563 // a list of top-level directories containing files to merge qualifier annotations
564 // (i.e. those intended to be included in the stubs written) from.
565 Merge_annotations_dirs []string
566
567 // a list of top-level directories containing Java stub files to merge show/hide annotations from.
568 Merge_inclusion_annotations_dirs []string
569
Paul Duffin4f5c1ef2020-11-19 14:53:43 +0000570 // If set to true then don't create dist rules.
571 No_dist *bool
Sundong Ahn80a87b32019-05-13 15:02:50 +0900572
Paul Duffin31310252020-11-20 21:26:20 +0000573 // The stem for the artifacts that are copied to the dist, if not specified
574 // then defaults to the base module name.
575 //
576 // For each scope the following artifacts are copied to the apistubs/<scope>
577 // directory in the dist.
578 // * stubs impl jar -> <dist-stem>.jar
579 // * API specification file -> api/<dist-stem>.txt
580 // * Removed API specification file -> api/<dist-stem>-removed.txt
581 //
582 // Also used to construct the name of the filegroup (created by prebuilt_apis)
583 // that references the latest released API and remove API specification files.
584 // * API specification filegroup -> <dist-stem>.api.<scope>.latest
585 // * Removed API specification filegroup -> <dist-stem>-removed.api.<scope>.latest
Jaewoong Jung1a97ee02021-03-09 13:25:02 -0800586 // * API incompatibilities baseline filegroup -> <dist-stem>-incompatibilities.api.<scope>.latest
Paul Duffin31310252020-11-20 21:26:20 +0000587 Dist_stem *string
588
Colin Cross986b69a2021-06-01 13:13:40 -0700589 // The subdirectory for the artifacts that are copied to the dist directory. If not specified
Colin Cross3dd66252021-06-01 14:05:09 -0700590 // then defaults to "unknown". Should be set to "android" for anything that should be published
Colin Cross986b69a2021-06-01 13:13:40 -0700591 // in the public Android SDK.
592 Dist_group *string
593
Anton Hanssondff2c782020-12-21 17:10:01 +0000594 // A compatibility mode that allows historical API-tracking files to not exist.
595 // Do not use.
596 Unsafe_ignore_missing_latest_api bool
597
Paul Duffin3375e352020-04-28 10:44:03 +0100598 // indicates whether system and test apis should be generated.
599 Generate_system_and_test_apis bool `blueprint:"mutated"`
600
601 // The properties specific to the public api scope
602 //
603 // Unless explicitly specified by using public.enabled the public api scope is
604 // enabled by default in both legacy and non-legacy mode.
605 Public ApiScopeProperties
606
607 // The properties specific to the system api scope
608 //
609 // In legacy mode the system api scope is enabled by default when sdk_version
610 // is set to something other than "none".
611 //
612 // In non-legacy mode the system api scope is disabled by default.
613 System ApiScopeProperties
614
615 // The properties specific to the test api scope
616 //
617 // In legacy mode the test api scope is enabled by default when sdk_version
618 // is set to something other than "none".
619 //
620 // In non-legacy mode the test api scope is disabled by default.
621 Test ApiScopeProperties
Paul Duffin37e0b772019-12-30 17:20:10 +0000622
Paul Duffin0c5bae52020-06-02 13:00:08 +0100623 // The properties specific to the module-lib api scope
Paul Duffin8f265b92020-04-28 14:13:56 +0100624 //
Zi Wangb2179e32023-01-31 15:53:30 -0800625 // Unless explicitly specified by using module_lib.enabled the module_lib api
626 // scope is disabled by default.
Paul Duffin8f265b92020-04-28 14:13:56 +0100627 Module_lib ApiScopeProperties
628
Paul Duffin0c5bae52020-06-02 13:00:08 +0100629 // The properties specific to the system-server api scope
630 //
Zi Wangb2179e32023-01-31 15:53:30 -0800631 // Unless explicitly specified by using system_server.enabled the
632 // system_server api scope is disabled by default.
Paul Duffin0c5bae52020-06-02 13:00:08 +0100633 System_server ApiScopeProperties
634
Jiyong Park932cdfe2020-05-28 00:19:53 +0900635 // Determines if the stubs are preferred over the implementation library
636 // for linking, even when the client doesn't specify sdk_version. When this
637 // is set to true, such clients are provided with the widest API surface that
638 // this lib provides. Note however that this option doesn't affect the clients
639 // that are in the same APEX as this library. In that case, the clients are
640 // always linked with the implementation library. Default is false.
641 Default_to_stubs *bool
642
Paul Duffin160fe412020-05-10 19:32:20 +0100643 // Properties related to api linting.
644 Api_lint struct {
645 // Enable api linting.
646 Enabled *bool
Anton Hanssonfd1c0d22023-11-02 15:18:09 +0000647
648 // If API lint is enabled, this flag controls whether a set of legitimate lint errors
649 // are turned off. The default is true.
650 Legacy_errors_allowed *bool
Paul Duffin160fe412020-05-10 19:32:20 +0100651 }
652
Jihoon Kang80456fd2023-11-15 19:22:14 +0000653 // Determines if the module contributes to any api surfaces.
654 // This property should be set to true only if the module is listed under
655 // frameworks-base-api.bootclasspath in frameworks/base/api/Android.bp.
656 // Otherwise, this property should be set to false.
657 // Defaults to false.
658 Contribute_to_android_api *bool
659
Jihoon Kang6592e872023-12-19 01:13:16 +0000660 // a list of aconfig_declarations module names that the stubs generated in this module
661 // depend on.
662 Aconfig_declarations []string
663
Jiyong Parkc678ad32018-04-10 13:07:10 +0900664 // TODO: determines whether to create HTML doc or not
Paul Duffine8409952022-09-22 16:24:46 +0100665 // Html_doc *bool
Jiyong Parkc678ad32018-04-10 13:07:10 +0900666}
667
Paul Duffin0f8faff2020-05-20 16:18:00 +0100668// Paths to outputs from java_sdk_library and java_sdk_library_import.
669//
670// Fields that are android.Paths are always set (during GenerateAndroidBuildActions).
671// OptionalPaths are always set by java_sdk_library but may not be set by
672// java_sdk_library_import as not all instances provide that information.
Paul Duffind1b3a922020-01-22 11:57:20 +0000673type scopePaths struct {
Paul Duffin0f8faff2020-05-20 16:18:00 +0100674 // The path (represented as Paths for convenience when returning) to the stubs header jar.
675 //
676 // That is the jar that is created by turbine.
677 stubsHeaderPath android.Paths
678
679 // The path (represented as Paths for convenience when returning) to the stubs implementation jar.
680 //
681 // This is not the implementation jar, it still only contains stubs.
682 stubsImplPath android.Paths
683
Paul Duffin1267d872021-04-16 17:21:36 +0100684 // The dex jar for the stubs.
685 //
686 // This is not the implementation jar, it still only contains stubs.
Martin Stjernholm8be1e6d2021-09-15 03:34:04 +0100687 stubsDexJarPath OptionalDexJarPath
Paul Duffin1267d872021-04-16 17:21:36 +0100688
Jihoon Kangbd093452023-12-26 19:08:01 +0000689 // The exportable dex jar for the stubs.
690 // This is not the implementation jar, it still only contains stubs.
691 // Includes unflagged apis and flagged apis enabled by release configurations.
692 exportableStubsDexJarPath OptionalDexJarPath
693
Paul Duffin0f8faff2020-05-20 16:18:00 +0100694 // The API specification file, e.g. system_current.txt.
695 currentApiFilePath android.OptionalPath
696
697 // The specification of API elements removed since the last release.
698 removedApiFilePath android.OptionalPath
699
700 // The stubs source jar.
701 stubsSrcJar android.OptionalPath
Anton Hanssond78eb762021-09-21 15:25:12 +0100702
703 // Extracted annotations.
704 annotationsZip android.OptionalPath
Paul Duffin958806b2022-05-16 13:10:47 +0000705
706 // The path to the latest API file.
707 latestApiPath android.OptionalPath
708
709 // The path to the latest removed API file.
710 latestRemovedApiPath android.OptionalPath
Paul Duffind1b3a922020-01-22 11:57:20 +0000711}
712
Colin Crossdcf71b22021-02-01 13:59:03 -0800713func (paths *scopePaths) extractStubsLibraryInfoFromDependency(ctx android.ModuleContext, dep android.Module) error {
Colin Cross313aa542023-12-13 13:47:44 -0800714 if lib, ok := android.OtherModuleProvider(ctx, dep, JavaInfoProvider); ok {
Colin Crossdcf71b22021-02-01 13:59:03 -0800715 paths.stubsHeaderPath = lib.HeaderJars
716 paths.stubsImplPath = lib.ImplementationJars
Paul Duffin1267d872021-04-16 17:21:36 +0100717
718 libDep := dep.(UsesLibraryDependency)
Spandan Das59a4a2b2024-01-09 21:35:56 +0000719 paths.stubsDexJarPath = libDep.DexJarBuildPath(ctx)
Jihoon Kangbd093452023-12-26 19:08:01 +0000720 paths.exportableStubsDexJarPath = libDep.DexJarBuildPath(ctx)
721 return nil
722 } else {
723 return fmt.Errorf("expected module that has JavaInfoProvider, e.g. java_library")
724 }
725}
726
727func (paths *scopePaths) extractEverythingStubsLibraryInfoFromDependency(ctx android.ModuleContext, dep android.Module) error {
728 if lib, ok := android.OtherModuleProvider(ctx, dep, JavaInfoProvider); ok {
729 paths.stubsHeaderPath = lib.HeaderJars
Jihoon Kangf55a5f72024-01-08 08:56:20 +0000730 if !ctx.Config().ReleaseHiddenApiExportableStubs() {
731 paths.stubsImplPath = lib.ImplementationJars
732 }
Jihoon Kangbd093452023-12-26 19:08:01 +0000733
734 libDep := dep.(UsesLibraryDependency)
735 paths.stubsDexJarPath = libDep.DexJarBuildPath(ctx)
736 return nil
737 } else {
738 return fmt.Errorf("expected module that has JavaInfoProvider, e.g. java_library")
739 }
740}
741
742func (paths *scopePaths) extractExportableStubsLibraryInfoFromDependency(ctx android.ModuleContext, dep android.Module) error {
Jihoon Kangf55a5f72024-01-08 08:56:20 +0000743 if lib, ok := android.OtherModuleProvider(ctx, dep, JavaInfoProvider); ok {
744 if ctx.Config().ReleaseHiddenApiExportableStubs() {
745 paths.stubsImplPath = lib.ImplementationJars
746 }
747
Jihoon Kangbd093452023-12-26 19:08:01 +0000748 libDep := dep.(UsesLibraryDependency)
749 paths.exportableStubsDexJarPath = libDep.DexJarBuildPath(ctx)
Paul Duffinc8782502020-04-29 20:45:27 +0100750 return nil
751 } else {
Colin Crossdcf71b22021-02-01 13:59:03 -0800752 return fmt.Errorf("expected module that has JavaInfoProvider, e.g. java_library")
Paul Duffinc8782502020-04-29 20:45:27 +0100753 }
754}
755
Jihoon Kangee113282024-01-23 00:16:41 +0000756func (paths *scopePaths) treatDepAsApiStubsProvider(dep android.Module, action func(provider ApiStubsProvider) error) error {
Paul Duffin0ff08bd2020-04-29 13:30:54 +0100757 if apiStubsProvider, ok := dep.(ApiStubsProvider); ok {
Jihoon Kangee113282024-01-23 00:16:41 +0000758 err := action(apiStubsProvider)
759 if err != nil {
760 return err
761 }
Jihoon Kangf55a5f72024-01-08 08:56:20 +0000762 return nil
763 } else {
764 return fmt.Errorf("expected module that implements ExportableApiStubsSrcProvider, e.g. droidstubs")
765 }
766}
767
Jihoon Kangee113282024-01-23 00:16:41 +0000768func (paths *scopePaths) treatDepAsApiStubsSrcProvider(dep android.Module, action func(provider ApiStubsSrcProvider) error) error {
Paul Duffin0f8faff2020-05-20 16:18:00 +0100769 if apiStubsProvider, ok := dep.(ApiStubsSrcProvider); ok {
Jihoon Kangee113282024-01-23 00:16:41 +0000770 err := action(apiStubsProvider)
771 if err != nil {
772 return err
773 }
Paul Duffin0f8faff2020-05-20 16:18:00 +0100774 return nil
775 } else {
776 return fmt.Errorf("expected module that implements ApiStubsSrcProvider, e.g. droidstubs")
777 }
778}
779
Jihoon Kangee113282024-01-23 00:16:41 +0000780func (paths *scopePaths) extractApiInfoFromApiStubsProvider(provider ApiStubsProvider, stubsType StubsType) error {
781 var annotationsZip, currentApiFilePath, removedApiFilePath android.Path
782 annotationsZip, annotationsZipErr := provider.AnnotationsZip(stubsType)
783 currentApiFilePath, currentApiFilePathErr := provider.ApiFilePath(stubsType)
784 removedApiFilePath, removedApiFilePathErr := provider.RemovedApiFilePath(stubsType)
Paul Duffin0ff08bd2020-04-29 13:30:54 +0100785
Jihoon Kangee113282024-01-23 00:16:41 +0000786 combinedError := errors.Join(annotationsZipErr, currentApiFilePathErr, removedApiFilePathErr)
787
788 if combinedError == nil {
789 paths.annotationsZip = android.OptionalPathForPath(annotationsZip)
790 paths.currentApiFilePath = android.OptionalPathForPath(currentApiFilePath)
791 paths.removedApiFilePath = android.OptionalPathForPath(removedApiFilePath)
792 }
793 return combinedError
Jihoon Kangf55a5f72024-01-08 08:56:20 +0000794}
795
Colin Crossdcf71b22021-02-01 13:59:03 -0800796func (paths *scopePaths) extractApiInfoFromDep(ctx android.ModuleContext, dep android.Module) error {
Jihoon Kangee113282024-01-23 00:16:41 +0000797 return paths.treatDepAsApiStubsProvider(dep, func(provider ApiStubsProvider) error {
798 return paths.extractApiInfoFromApiStubsProvider(provider, Everything)
Paul Duffin0ff08bd2020-04-29 13:30:54 +0100799 })
800}
801
Jihoon Kangee113282024-01-23 00:16:41 +0000802func (paths *scopePaths) extractStubsSourceInfoFromApiStubsProviders(provider ApiStubsSrcProvider, stubsType StubsType) error {
803 stubsSrcJar, err := provider.StubsSrcJar(stubsType)
804 if err == nil {
805 paths.stubsSrcJar = android.OptionalPathForPath(stubsSrcJar)
806 }
807 return err
Jihoon Kangf55a5f72024-01-08 08:56:20 +0000808}
809
Colin Crossdcf71b22021-02-01 13:59:03 -0800810func (paths *scopePaths) extractStubsSourceInfoFromDep(ctx android.ModuleContext, dep android.Module) error {
Jihoon Kangee113282024-01-23 00:16:41 +0000811 return paths.treatDepAsApiStubsSrcProvider(dep, func(provider ApiStubsSrcProvider) error {
812 return paths.extractStubsSourceInfoFromApiStubsProviders(provider, Everything)
Paul Duffin0ff08bd2020-04-29 13:30:54 +0100813 })
814}
815
Colin Crossdcf71b22021-02-01 13:59:03 -0800816func (paths *scopePaths) extractStubsSourceAndApiInfoFromApiStubsProvider(ctx android.ModuleContext, dep android.Module) error {
Jihoon Kangf55a5f72024-01-08 08:56:20 +0000817 if ctx.Config().ReleaseHiddenApiExportableStubs() {
Jihoon Kangee113282024-01-23 00:16:41 +0000818 return paths.treatDepAsApiStubsProvider(dep, func(provider ApiStubsProvider) error {
819 extractApiInfoErr := paths.extractApiInfoFromApiStubsProvider(provider, Exportable)
820 extractStubsSourceInfoErr := paths.extractStubsSourceInfoFromApiStubsProviders(provider, Exportable)
821 return errors.Join(extractApiInfoErr, extractStubsSourceInfoErr)
Jihoon Kangf55a5f72024-01-08 08:56:20 +0000822 })
823 }
Jihoon Kangee113282024-01-23 00:16:41 +0000824 return paths.treatDepAsApiStubsProvider(dep, func(provider ApiStubsProvider) error {
825 extractApiInfoErr := paths.extractApiInfoFromApiStubsProvider(provider, Everything)
826 extractStubsSourceInfoErr := paths.extractStubsSourceInfoFromApiStubsProviders(provider, Everything)
827 return errors.Join(extractApiInfoErr, extractStubsSourceInfoErr)
Paul Duffin0ff08bd2020-04-29 13:30:54 +0100828 })
829}
830
Paul Duffin958806b2022-05-16 13:10:47 +0000831func extractSingleOptionalOutputPath(dep android.Module) (android.OptionalPath, error) {
832 var paths android.Paths
833 if sourceFileProducer, ok := dep.(android.SourceFileProducer); ok {
834 paths = sourceFileProducer.Srcs()
835 } else {
836 return android.OptionalPath{}, fmt.Errorf("module %q does not produce source files", dep)
837 }
838 if len(paths) != 1 {
839 return android.OptionalPath{}, fmt.Errorf("expected one path from %q, got %q", dep, paths)
840 }
841 return android.OptionalPathForPath(paths[0]), nil
842}
843
844func (paths *scopePaths) extractLatestApiPath(ctx android.ModuleContext, dep android.Module) error {
845 outputPath, err := extractSingleOptionalOutputPath(dep)
846 paths.latestApiPath = outputPath
847 return err
848}
849
850func (paths *scopePaths) extractLatestRemovedApiPath(ctx android.ModuleContext, dep android.Module) error {
851 outputPath, err := extractSingleOptionalOutputPath(dep)
852 paths.latestRemovedApiPath = outputPath
853 return err
854}
855
Paul Duffin0ff08bd2020-04-29 13:30:54 +0100856type commonToSdkLibraryAndImportProperties struct {
Paul Duffin1b1e8062020-05-08 13:44:43 +0100857 // The naming scheme to use for the components that this module creates.
858 //
Paul Duffinee9ad5d2020-09-11 13:04:05 +0100859 // If not specified then it defaults to "default".
Paul Duffin1b1e8062020-05-08 13:44:43 +0100860 //
861 // This is a temporary mechanism to simplify conversion from separate modules for each
862 // component that follow a different naming pattern to the default one.
863 //
864 // TODO(b/155480189) - Remove once naming inconsistencies have been resolved.
Paul Duffin0ff08bd2020-04-29 13:30:54 +0100865 Naming_scheme *string
Paul Duffindfa131e2020-05-15 20:37:11 +0100866
867 // Specifies whether this module can be used as an Android shared library; defaults
868 // to true.
869 //
870 // An Android shared library is one that can be referenced in a <uses-library> element
871 // in an AndroidManifest.xml.
872 Shared_library *bool
Paul Duffina2ae7e02020-09-11 11:55:00 +0100873
874 // Files containing information about supported java doc tags.
875 Doctag_files []string `android:"path"`
Pedro Loureiro9956e5e2021-09-07 17:21:59 +0000876
877 // Signals that this shared library is part of the bootclasspath starting
878 // on the version indicated in this attribute.
879 //
880 // This will make platforms at this level and above to ignore
881 // <uses-library> tags with this library name because the library is already
882 // available
883 On_bootclasspath_since *string
884
885 // Signals that this shared library was part of the bootclasspath before
886 // (but not including) the version indicated in this attribute.
887 //
888 // The system will automatically add a <uses-library> tag with this library to
889 // apps that target any SDK less than the version indicated in this attribute.
890 On_bootclasspath_before *string
891
892 // Indicates that PackageManager should ignore this shared library if the
893 // platform is below the version indicated in this attribute.
894 //
895 // This means that the device won't recognise this library as installed.
896 Min_device_sdk *string
897
898 // Indicates that PackageManager should ignore this shared library if the
899 // platform is above the version indicated in this attribute.
900 //
901 // This means that the device won't recognise this library as installed.
902 Max_device_sdk *string
Paul Duffin0ff08bd2020-04-29 13:30:54 +0100903}
904
Paul Duffin71b33cc2021-06-23 11:39:47 +0100905// commonSdkLibraryAndImportModule defines the interface that must be provided by a module that
906// embeds the commonToSdkLibraryAndImport struct.
907type commonSdkLibraryAndImportModule interface {
Paul Duffind796f6f2022-11-23 23:06:05 +0000908 android.Module
Paul Duffin71b33cc2021-06-23 11:39:47 +0100909
910 BaseModuleName() string
911}
912
Paul Duffin56d44902020-01-31 13:36:25 +0000913// Common code between sdk library and sdk library import
914type commonToSdkLibraryAndImport struct {
Paul Duffin71b33cc2021-06-23 11:39:47 +0100915 module commonSdkLibraryAndImportModule
Paul Duffinc3091c82020-05-08 14:16:20 +0100916
Paul Duffin56d44902020-01-31 13:36:25 +0000917 scopePaths map[*apiScope]*scopePaths
Paul Duffin1b1e8062020-05-08 13:44:43 +0100918
919 namingScheme sdkLibraryComponentNamingScheme
920
Paul Duffindfa131e2020-05-15 20:37:11 +0100921 commonSdkLibraryProperties commonToSdkLibraryAndImportProperties
Paul Duffin859fe962020-05-15 10:20:31 +0100922
Paul Duffina2ae7e02020-09-11 11:55:00 +0100923 // Paths to commonSdkLibraryProperties.Doctag_files
924 doctagPaths android.Paths
925
Paul Duffin859fe962020-05-15 10:20:31 +0100926 // Functionality related to this being used as a component of a java_sdk_library.
927 EmbeddableSdkLibraryComponent
Paul Duffin56d44902020-01-31 13:36:25 +0000928}
929
Paul Duffin71b33cc2021-06-23 11:39:47 +0100930func (c *commonToSdkLibraryAndImport) initCommon(module commonSdkLibraryAndImportModule) {
931 c.module = module
Paul Duffin1b1e8062020-05-08 13:44:43 +0100932
Paul Duffin71b33cc2021-06-23 11:39:47 +0100933 module.AddProperties(&c.commonSdkLibraryProperties)
Paul Duffin859fe962020-05-15 10:20:31 +0100934
935 // Initialize this as an sdk library component.
Paul Duffin71b33cc2021-06-23 11:39:47 +0100936 c.initSdkLibraryComponent(module)
Paul Duffin1b1e8062020-05-08 13:44:43 +0100937}
938
939func (c *commonToSdkLibraryAndImport) initCommonAfterDefaultsApplied(ctx android.DefaultableHookContext) bool {
Paul Duffindfa131e2020-05-15 20:37:11 +0100940 schemeProperty := proptools.StringDefault(c.commonSdkLibraryProperties.Naming_scheme, "default")
Paul Duffin1b1e8062020-05-08 13:44:43 +0100941 switch schemeProperty {
942 case "default":
943 c.namingScheme = &defaultNamingScheme{}
944 default:
945 ctx.PropertyErrorf("naming_scheme", "expected 'default' but was %q", schemeProperty)
946 return false
947 }
948
Paul Duffin3f0290e2021-06-30 18:25:36 +0100949 namePtr := proptools.StringPtr(c.module.BaseModuleName())
950 c.sdkLibraryComponentProperties.SdkLibraryName = namePtr
951
Paul Duffindfa131e2020-05-15 20:37:11 +0100952 // Only track this sdk library if this can be used as a shared library.
953 if c.sharedLibrary() {
954 // Use the name specified in the module definition as the owner.
Paul Duffin3f0290e2021-06-30 18:25:36 +0100955 c.sdkLibraryComponentProperties.SdkLibraryToImplicitlyTrack = namePtr
Paul Duffindfa131e2020-05-15 20:37:11 +0100956 }
Paul Duffin859fe962020-05-15 10:20:31 +0100957
Paul Duffin1b1e8062020-05-08 13:44:43 +0100958 return true
Paul Duffinc3091c82020-05-08 14:16:20 +0100959}
960
Paul Duffinea8f8082021-06-24 13:25:57 +0100961// uniqueApexVariations provides common implementation of the ApexModule.UniqueApexVariations
962// method.
963func (c *commonToSdkLibraryAndImport) uniqueApexVariations() bool {
964 // A java_sdk_library that is a shared library produces an XML file that makes the shared library
965 // usable from an AndroidManifest.xml's <uses-library> entry. That XML file contains the name of
966 // the APEX and so it needs a unique variation per APEX.
967 return c.sharedLibrary()
968}
969
Paul Duffina2ae7e02020-09-11 11:55:00 +0100970func (c *commonToSdkLibraryAndImport) generateCommonBuildActions(ctx android.ModuleContext) {
971 c.doctagPaths = android.PathsForModuleSrc(ctx, c.commonSdkLibraryProperties.Doctag_files)
972}
973
Paul Duffineedc5d52020-06-12 17:46:39 +0100974// Module name of the runtime implementation library
975func (c *commonToSdkLibraryAndImport) implLibraryModuleName() string {
Paul Duffin71b33cc2021-06-23 11:39:47 +0100976 return c.module.BaseModuleName() + ".impl"
Paul Duffineedc5d52020-06-12 17:46:39 +0100977}
978
979// Module name of the XML file for the lib
980func (c *commonToSdkLibraryAndImport) xmlPermissionsModuleName() string {
Paul Duffin71b33cc2021-06-23 11:39:47 +0100981 return c.module.BaseModuleName() + sdkXmlFileSuffix
Paul Duffineedc5d52020-06-12 17:46:39 +0100982}
983
Paul Duffinc3091c82020-05-08 14:16:20 +0100984// Name of the java_library module that compiles the stubs source.
985func (c *commonToSdkLibraryAndImport) stubsLibraryModuleName(apiScope *apiScope) string {
Paul Duffinb97b1572021-04-29 21:50:40 +0100986 baseName := c.module.BaseModuleName()
Paul Duffin21787622022-11-25 12:48:20 +0000987 return c.namingScheme.stubsLibraryModuleName(apiScope, baseName)
Paul Duffinc3091c82020-05-08 14:16:20 +0100988}
989
Jihoon Kangfa4a90d2023-12-20 02:53:38 +0000990// Name of the java_library module that compiles the exportable stubs source.
991func (c *commonToSdkLibraryAndImport) exportableStubsLibraryModuleName(apiScope *apiScope) string {
992 baseName := c.module.BaseModuleName()
993 return c.namingScheme.exportableStubsLibraryModuleName(apiScope, baseName)
994}
995
Paul Duffinc3091c82020-05-08 14:16:20 +0100996// Name of the droidstubs module that generates the stubs source and may also
997// generate/check the API.
998func (c *commonToSdkLibraryAndImport) stubsSourceModuleName(apiScope *apiScope) string {
Paul Duffinb97b1572021-04-29 21:50:40 +0100999 baseName := c.module.BaseModuleName()
Paul Duffin21787622022-11-25 12:48:20 +00001000 return c.namingScheme.stubsSourceModuleName(apiScope, baseName)
Paul Duffinc3091c82020-05-08 14:16:20 +01001001}
1002
Jihoon Kang1c92c3e2023-03-23 17:44:51 +00001003// Name of the java_api_library module that generates the from-text stubs source
1004// and compiles to a jar file.
1005func (c *commonToSdkLibraryAndImport) apiLibraryModuleName(apiScope *apiScope) string {
1006 baseName := c.module.BaseModuleName()
1007 return c.namingScheme.apiLibraryModuleName(apiScope, baseName)
1008}
1009
Jihoon Kang1147b312023-06-08 23:25:57 +00001010// Name of the java_library module that compiles the stubs
1011// generated from source Java files.
Jihoon Kangfa4a90d2023-12-20 02:53:38 +00001012func (c *commonToSdkLibraryAndImport) sourceStubsLibraryModuleName(apiScope *apiScope) string {
Jihoon Kang1147b312023-06-08 23:25:57 +00001013 baseName := c.module.BaseModuleName()
Jihoon Kangfa4a90d2023-12-20 02:53:38 +00001014 return c.namingScheme.sourceStubsLibraryModuleName(apiScope, baseName)
1015}
1016
1017// Name of the java_library module that compiles the exportable stubs
1018// generated from source Java files.
1019func (c *commonToSdkLibraryAndImport) exportableSourceStubsLibraryModuleName(apiScope *apiScope) string {
1020 baseName := c.module.BaseModuleName()
1021 return c.namingScheme.exportableSourceStubsLibraryModuleName(apiScope, baseName)
Jihoon Kang1147b312023-06-08 23:25:57 +00001022}
1023
Paul Duffin46dc45a2020-05-14 15:39:10 +01001024// The component names for different outputs of the java_sdk_library.
1025//
1026// They are similar to the names used for the child modules it creates
1027const (
1028 stubsSourceComponentName = "stubs.source"
1029
1030 apiTxtComponentName = "api.txt"
1031
1032 removedApiTxtComponentName = "removed-api.txt"
Anton Hanssond78eb762021-09-21 15:25:12 +01001033
1034 annotationsComponentName = "annotations.zip"
Paul Duffin46dc45a2020-05-14 15:39:10 +01001035)
1036
1037// A regular expression to match tags that reference a specific stubs component.
1038//
1039// It will only match if given a valid scope and a valid component. It is verfy strict
1040// to ensure it does not accidentally match a similar looking tag that should be processed
1041// by the embedded Library.
1042var tagSplitter = func() *regexp.Regexp {
1043 // Given a list of literal string items returns a regular expression that will
1044 // match any one of the items.
1045 choice := func(items ...string) string {
1046 return `\Q` + strings.Join(items, `\E|\Q`) + `\E`
1047 }
1048
1049 // Regular expression to match one of the scopes.
1050 scopesRegexp := choice(allScopeNames...)
1051
1052 // Regular expression to match one of the components.
Anton Hanssond78eb762021-09-21 15:25:12 +01001053 componentsRegexp := choice(stubsSourceComponentName, apiTxtComponentName, removedApiTxtComponentName, annotationsComponentName)
Paul Duffin46dc45a2020-05-14 15:39:10 +01001054
1055 // Regular expression to match any combination of one scope and one component.
1056 return regexp.MustCompile(fmt.Sprintf(`^\.(%s)\.(%s)$`, scopesRegexp, componentsRegexp))
1057}()
1058
1059// For OutputFileProducer interface
1060//
Anton Hanssond78eb762021-09-21 15:25:12 +01001061// .<scope>.<component name>, for all ComponentNames (for example: .public.removed-api.txt)
Paul Duffin46dc45a2020-05-14 15:39:10 +01001062func (c *commonToSdkLibraryAndImport) commonOutputFiles(tag string) (android.Paths, error) {
1063 if groups := tagSplitter.FindStringSubmatch(tag); groups != nil {
1064 scopeName := groups[1]
1065 component := groups[2]
1066
1067 if scope, ok := scopeByName[scopeName]; ok {
1068 paths := c.findScopePaths(scope)
1069 if paths == nil {
Paul Duffin71b33cc2021-06-23 11:39:47 +01001070 return nil, fmt.Errorf("%q does not provide api scope %s", c.module.BaseModuleName(), scopeName)
Paul Duffin46dc45a2020-05-14 15:39:10 +01001071 }
1072
1073 switch component {
1074 case stubsSourceComponentName:
1075 if paths.stubsSrcJar.Valid() {
1076 return android.Paths{paths.stubsSrcJar.Path()}, nil
1077 }
1078
1079 case apiTxtComponentName:
1080 if paths.currentApiFilePath.Valid() {
1081 return android.Paths{paths.currentApiFilePath.Path()}, nil
1082 }
1083
1084 case removedApiTxtComponentName:
1085 if paths.removedApiFilePath.Valid() {
1086 return android.Paths{paths.removedApiFilePath.Path()}, nil
1087 }
Anton Hanssond78eb762021-09-21 15:25:12 +01001088
1089 case annotationsComponentName:
1090 if paths.annotationsZip.Valid() {
1091 return android.Paths{paths.annotationsZip.Path()}, nil
1092 }
Paul Duffin46dc45a2020-05-14 15:39:10 +01001093 }
1094
1095 return nil, fmt.Errorf("%s not available for api scope %s", component, scopeName)
1096 } else {
1097 return nil, fmt.Errorf("unknown scope %s in %s", scope, tag)
1098 }
1099
1100 } else {
Paul Duffina2ae7e02020-09-11 11:55:00 +01001101 switch tag {
1102 case ".doctags":
1103 if c.doctagPaths != nil {
1104 return c.doctagPaths, nil
1105 } else {
Paul Duffin71b33cc2021-06-23 11:39:47 +01001106 return nil, fmt.Errorf("no doctag_files specified on %s", c.module.BaseModuleName())
Paul Duffina2ae7e02020-09-11 11:55:00 +01001107 }
1108 }
Paul Duffin46dc45a2020-05-14 15:39:10 +01001109 return nil, nil
1110 }
1111}
1112
Paul Duffin803a9562020-05-20 11:52:25 +01001113func (c *commonToSdkLibraryAndImport) getScopePathsCreateIfNeeded(scope *apiScope) *scopePaths {
Paul Duffin56d44902020-01-31 13:36:25 +00001114 if c.scopePaths == nil {
1115 c.scopePaths = make(map[*apiScope]*scopePaths)
1116 }
1117 paths := c.scopePaths[scope]
1118 if paths == nil {
1119 paths = &scopePaths{}
1120 c.scopePaths[scope] = paths
1121 }
1122
1123 return paths
1124}
1125
Paul Duffin803a9562020-05-20 11:52:25 +01001126func (c *commonToSdkLibraryAndImport) findScopePaths(scope *apiScope) *scopePaths {
1127 if c.scopePaths == nil {
1128 return nil
1129 }
1130
1131 return c.scopePaths[scope]
1132}
1133
1134// If this does not support the requested api scope then find the closest available
1135// scope it does support. Returns nil if no such scope is available.
1136func (c *commonToSdkLibraryAndImport) findClosestScopePath(scope *apiScope) *scopePaths {
Paul Duffind0b9fca2022-09-30 18:11:41 +01001137 for s := scope; s != nil; s = s.canAccess {
Paul Duffin803a9562020-05-20 11:52:25 +01001138 if paths := c.findScopePaths(s); paths != nil {
1139 return paths
1140 }
1141 }
1142
1143 // This should never happen outside tests as public should be the base scope for every
1144 // scope and is enabled by default.
1145 return nil
1146}
1147
Jiyong Parkf1691d22021-03-29 20:11:58 +09001148func (c *commonToSdkLibraryAndImport) selectHeaderJarsForSdkVersion(ctx android.BaseModuleContext, sdkVersion android.SdkSpec) android.Paths {
Paul Duffinb05d4292020-05-20 12:19:10 +01001149
1150 // If a specific numeric version has been requested then use prebuilt versions of the sdk.
Jiyong Park54105c42021-03-31 18:17:53 +09001151 if !sdkVersion.ApiLevel.IsPreview() {
Paul Duffin71b33cc2021-06-23 11:39:47 +01001152 return PrebuiltJars(ctx, c.module.BaseModuleName(), sdkVersion)
Paul Duffinb05d4292020-05-20 12:19:10 +01001153 }
1154
Paul Duffin1267d872021-04-16 17:21:36 +01001155 paths := c.selectScopePaths(ctx, sdkVersion.Kind)
1156 if paths == nil {
1157 return nil
1158 }
1159
1160 return paths.stubsHeaderPath
1161}
1162
1163// selectScopePaths returns the *scopePaths appropriate for the specific kind.
1164//
1165// If the module does not support the specific kind then it will return the *scopePaths for the
1166// closest kind which is a subset of the requested kind. e.g. if requesting android.SdkModule then
1167// it will return *scopePaths for android.SdkSystem if available or android.SdkPublic of not.
1168func (c *commonToSdkLibraryAndImport) selectScopePaths(ctx android.BaseModuleContext, kind android.SdkKind) *scopePaths {
Paul Duffin32cf58a2021-05-18 16:32:50 +01001169 apiScope := sdkKindToApiScope(kind)
Paul Duffinb05d4292020-05-20 12:19:10 +01001170
Paul Duffin803a9562020-05-20 11:52:25 +01001171 paths := c.findClosestScopePath(apiScope)
1172 if paths == nil {
1173 var scopes []string
1174 for _, s := range allApiScopes {
1175 if c.findScopePaths(s) != nil {
1176 scopes = append(scopes, s.name)
1177 }
1178 }
Paul Duffin71b33cc2021-06-23 11:39:47 +01001179 ctx.ModuleErrorf("requires api scope %s from %s but it only has %q available", apiScope.name, c.module.BaseModuleName(), scopes)
Paul Duffin803a9562020-05-20 11:52:25 +01001180 return nil
1181 }
1182
Paul Duffin1267d872021-04-16 17:21:36 +01001183 return paths
1184}
1185
Paul Duffin32cf58a2021-05-18 16:32:50 +01001186// sdkKindToApiScope maps from android.SdkKind to apiScope.
1187func sdkKindToApiScope(kind android.SdkKind) *apiScope {
1188 var apiScope *apiScope
1189 switch kind {
1190 case android.SdkSystem:
1191 apiScope = apiScopeSystem
1192 case android.SdkModule:
1193 apiScope = apiScopeModuleLib
1194 case android.SdkTest:
1195 apiScope = apiScopeTest
1196 case android.SdkSystemServer:
1197 apiScope = apiScopeSystemServer
1198 default:
1199 apiScope = apiScopePublic
1200 }
1201 return apiScope
1202}
1203
Paul Duffin1267d872021-04-16 17:21:36 +01001204// to satisfy SdkLibraryDependency interface
Martin Stjernholm8be1e6d2021-09-15 03:34:04 +01001205func (c *commonToSdkLibraryAndImport) SdkApiStubDexJar(ctx android.BaseModuleContext, kind android.SdkKind) OptionalDexJarPath {
Paul Duffin1267d872021-04-16 17:21:36 +01001206 paths := c.selectScopePaths(ctx, kind)
1207 if paths == nil {
Martin Stjernholm8be1e6d2021-09-15 03:34:04 +01001208 return makeUnsetDexJarPath()
Paul Duffin1267d872021-04-16 17:21:36 +01001209 }
1210
1211 return paths.stubsDexJarPath
Paul Duffinb05d4292020-05-20 12:19:10 +01001212}
1213
Paul Duffin32cf58a2021-05-18 16:32:50 +01001214// to satisfy SdkLibraryDependency interface
Jihoon Kangbd093452023-12-26 19:08:01 +00001215func (c *commonToSdkLibraryAndImport) SdkApiExportableStubDexJar(ctx android.BaseModuleContext, kind android.SdkKind) OptionalDexJarPath {
1216 paths := c.selectScopePaths(ctx, kind)
1217 if paths == nil {
1218 return makeUnsetDexJarPath()
1219 }
1220
1221 return paths.exportableStubsDexJarPath
1222}
1223
1224// to satisfy SdkLibraryDependency interface
Paul Duffin32cf58a2021-05-18 16:32:50 +01001225func (c *commonToSdkLibraryAndImport) SdkRemovedTxtFile(ctx android.BaseModuleContext, kind android.SdkKind) android.OptionalPath {
1226 apiScope := sdkKindToApiScope(kind)
1227 paths := c.findScopePaths(apiScope)
1228 if paths == nil {
1229 return android.OptionalPath{}
1230 }
1231
1232 return paths.removedApiFilePath
1233}
1234
Paul Duffin859fe962020-05-15 10:20:31 +01001235func (c *commonToSdkLibraryAndImport) sdkComponentPropertiesForChildLibrary() interface{} {
1236 componentProps := &struct {
Paul Duffin3f0290e2021-06-30 18:25:36 +01001237 SdkLibraryName *string
Paul Duffin859fe962020-05-15 10:20:31 +01001238 SdkLibraryToImplicitlyTrack *string
Paul Duffindfa131e2020-05-15 20:37:11 +01001239 }{}
1240
Paul Duffin3f0290e2021-06-30 18:25:36 +01001241 namePtr := proptools.StringPtr(c.module.BaseModuleName())
1242 componentProps.SdkLibraryName = namePtr
1243
Paul Duffindfa131e2020-05-15 20:37:11 +01001244 if c.sharedLibrary() {
Paul Duffin859fe962020-05-15 10:20:31 +01001245 // Mark the stubs library as being components of this java_sdk_library so that
1246 // any app that includes code which depends (directly or indirectly) on the stubs
1247 // library will have the appropriate <uses-library> invocation inserted into its
1248 // manifest if necessary.
Paul Duffin3f0290e2021-06-30 18:25:36 +01001249 componentProps.SdkLibraryToImplicitlyTrack = namePtr
Paul Duffin859fe962020-05-15 10:20:31 +01001250 }
1251
1252 return componentProps
1253}
1254
Paul Duffindfa131e2020-05-15 20:37:11 +01001255func (c *commonToSdkLibraryAndImport) sharedLibrary() bool {
1256 return proptools.BoolDefault(c.commonSdkLibraryProperties.Shared_library, true)
1257}
1258
Paul Duffinf4600f62021-05-13 22:34:45 +01001259// Check if the stub libraries should be compiled for dex
1260func (c *commonToSdkLibraryAndImport) stubLibrariesCompiledForDex() bool {
1261 // Always compile the dex file files for the stub libraries if they will be used on the
1262 // bootclasspath.
1263 return !c.sharedLibrary()
1264}
1265
Paul Duffin859fe962020-05-15 10:20:31 +01001266// Properties related to the use of a module as an component of a java_sdk_library.
1267type SdkLibraryComponentProperties struct {
Paul Duffin3f0290e2021-06-30 18:25:36 +01001268 // The name of the java_sdk_library/_import module.
1269 SdkLibraryName *string `blueprint:"mutated"`
Paul Duffin859fe962020-05-15 10:20:31 +01001270
1271 // The name of the java_sdk_library/_import to add to a <uses-library> entry
1272 // in the AndroidManifest.xml of any Android app that includes code that references
1273 // this module. If not set then no java_sdk_library/_import is tracked.
1274 SdkLibraryToImplicitlyTrack *string `blueprint:"mutated"`
1275}
1276
1277// Structure to be embedded in a module struct that needs to support the
1278// SdkLibraryComponentDependency interface.
1279type EmbeddableSdkLibraryComponent struct {
1280 sdkLibraryComponentProperties SdkLibraryComponentProperties
1281}
1282
Paul Duffin71b33cc2021-06-23 11:39:47 +01001283func (e *EmbeddableSdkLibraryComponent) initSdkLibraryComponent(module android.Module) {
1284 module.AddProperties(&e.sdkLibraryComponentProperties)
Paul Duffin859fe962020-05-15 10:20:31 +01001285}
1286
1287// to satisfy SdkLibraryComponentDependency
Paul Duffin3f0290e2021-06-30 18:25:36 +01001288func (e *EmbeddableSdkLibraryComponent) SdkLibraryName() *string {
1289 return e.sdkLibraryComponentProperties.SdkLibraryName
1290}
1291
1292// to satisfy SdkLibraryComponentDependency
Ulya Trafimovich39b437b2020-09-23 16:42:35 +01001293func (e *EmbeddableSdkLibraryComponent) OptionalSdkLibraryImplementation() *string {
Ulya Trafimovich78645fb2021-07-16 15:29:25 +01001294 // For shared libraries, this is the same as the SDK library name. If a Java library or app
1295 // depends on a component library (e.g. a stub library) it still needs to know the name of the
1296 // run-time library and the corresponding module that provides the implementation. This name is
1297 // passed to manifest_fixer (to be added to AndroidManifest.xml) and added to CLC (to be used
1298 // in dexpreopt).
1299 //
1300 // For non-shared SDK (component or not) libraries this returns `nil`, as they are not
1301 // <uses-library> and should not be added to the manifest or to CLC.
Ulya Trafimovich39b437b2020-09-23 16:42:35 +01001302 return e.sdkLibraryComponentProperties.SdkLibraryToImplicitlyTrack
1303}
1304
Paul Duffin859fe962020-05-15 10:20:31 +01001305// Implemented by modules that are (or possibly could be) a component of a java_sdk_library
1306// (including the java_sdk_library) itself.
1307type SdkLibraryComponentDependency interface {
Ulya Trafimovich31e444e2020-08-14 17:32:16 +01001308 UsesLibraryDependency
1309
Paul Duffin3f0290e2021-06-30 18:25:36 +01001310 // SdkLibraryName returns the name of the java_sdk_library/_import module.
1311 SdkLibraryName() *string
1312
Ulya Trafimovich39b437b2020-09-23 16:42:35 +01001313 // The name of the implementation library for the optional SDK library or nil, if there isn't one.
1314 OptionalSdkLibraryImplementation() *string
Paul Duffin859fe962020-05-15 10:20:31 +01001315}
1316
1317// Make sure that all the module types that are components of java_sdk_library/_import
1318// and which can be referenced (directly or indirectly) from an android app implement
1319// the SdkLibraryComponentDependency interface.
1320var _ SdkLibraryComponentDependency = (*Library)(nil)
1321var _ SdkLibraryComponentDependency = (*Import)(nil)
1322var _ SdkLibraryComponentDependency = (*SdkLibrary)(nil)
Paul Duffineedc5d52020-06-12 17:46:39 +01001323var _ SdkLibraryComponentDependency = (*SdkLibraryImport)(nil)
Paul Duffin859fe962020-05-15 10:20:31 +01001324
Paul Duffin32cf58a2021-05-18 16:32:50 +01001325// Provides access to sdk_version related files, e.g. header and implementation jars.
Paul Duffin859fe962020-05-15 10:20:31 +01001326type SdkLibraryDependency interface {
1327 SdkLibraryComponentDependency
1328
1329 // Get the header jars appropriate for the supplied sdk_version.
1330 //
1331 // These are turbine generated jars so they only change if the externals of the
1332 // class changes but it does not contain and implementation or JavaDoc.
Jiyong Parkf1691d22021-03-29 20:11:58 +09001333 SdkHeaderJars(ctx android.BaseModuleContext, sdkVersion android.SdkSpec) android.Paths
Paul Duffin859fe962020-05-15 10:20:31 +01001334
1335 // Get the implementation jars appropriate for the supplied sdk version.
1336 //
1337 // These are either the implementation jar for the whole sdk library or the implementation
1338 // jars for the stubs. The latter should only be needed when generating JavaDoc as otherwise
1339 // they are identical to the corresponding header jars.
Jiyong Parkf1691d22021-03-29 20:11:58 +09001340 SdkImplementationJars(ctx android.BaseModuleContext, sdkVersion android.SdkSpec) android.Paths
Paul Duffin1267d872021-04-16 17:21:36 +01001341
Jihoon Kangbd093452023-12-26 19:08:01 +00001342 // SdkApiStubDexJar returns the dex jar for the stubs for the prebuilt
1343 // java_sdk_library_import module. It is needed by the hiddenapi processing tool which
1344 // processes dex files.
Martin Stjernholm8be1e6d2021-09-15 03:34:04 +01001345 SdkApiStubDexJar(ctx android.BaseModuleContext, kind android.SdkKind) OptionalDexJarPath
Paul Duffinf4600f62021-05-13 22:34:45 +01001346
Jihoon Kangbd093452023-12-26 19:08:01 +00001347 // SdkApiExportableStubDexJar returns the exportable dex jar for the stubs for
1348 // java_sdk_library module. It is needed by the hiddenapi processing tool which processes
1349 // dex files.
1350 SdkApiExportableStubDexJar(ctx android.BaseModuleContext, kind android.SdkKind) OptionalDexJarPath
1351
Paul Duffin32cf58a2021-05-18 16:32:50 +01001352 // SdkRemovedTxtFile returns the optional path to the removed.txt file for the specified sdk kind.
1353 SdkRemovedTxtFile(ctx android.BaseModuleContext, kind android.SdkKind) android.OptionalPath
1354
Paul Duffinf4600f62021-05-13 22:34:45 +01001355 // sharedLibrary returns true if this can be used as a shared library.
1356 sharedLibrary() bool
Paul Duffin859fe962020-05-15 10:20:31 +01001357}
1358
Inseob Kimc0907f12019-02-08 21:00:45 +09001359type SdkLibrary struct {
Sundong Ahn054b19a2018-10-19 13:46:09 +09001360 Library
Jiyong Parkc678ad32018-04-10 13:07:10 +09001361
Sundong Ahn054b19a2018-10-19 13:46:09 +09001362 sdkLibraryProperties sdkLibraryProperties
Jiyong Parkc678ad32018-04-10 13:07:10 +09001363
Paul Duffin3375e352020-04-28 10:44:03 +01001364 // Map from api scope to the scope specific property structure.
1365 scopeToProperties map[*apiScope]*ApiScopeProperties
1366
Paul Duffin56d44902020-01-31 13:36:25 +00001367 commonToSdkLibraryAndImport
Jiyong Parkc678ad32018-04-10 13:07:10 +09001368}
1369
Inseob Kimc0907f12019-02-08 21:00:45 +09001370var _ SdkLibraryDependency = (*SdkLibrary)(nil)
Colin Cross897d2ed2019-02-11 14:03:51 -08001371
Paul Duffin3375e352020-04-28 10:44:03 +01001372func (module *SdkLibrary) generateTestAndSystemScopesByDefault() bool {
1373 return module.sdkLibraryProperties.Generate_system_and_test_apis
1374}
1375
1376func (module *SdkLibrary) getGeneratedApiScopes(ctx android.EarlyModuleContext) apiScopes {
1377 // Check to see if any scopes have been explicitly enabled. If any have then all
1378 // must be.
1379 anyScopesExplicitlyEnabled := false
1380 for _, scope := range allApiScopes {
1381 scopeProperties := module.scopeToProperties[scope]
1382 if scopeProperties.Enabled != nil {
1383 anyScopesExplicitlyEnabled = true
1384 break
1385 }
Paul Duffind1b3a922020-01-22 11:57:20 +00001386 }
Paul Duffin3375e352020-04-28 10:44:03 +01001387
1388 var generatedScopes apiScopes
1389 enabledScopes := make(map[*apiScope]struct{})
1390 for _, scope := range allApiScopes {
1391 scopeProperties := module.scopeToProperties[scope]
1392 // If any scopes are explicitly enabled then ignore the legacy enabled status.
1393 // This is to ensure that any new usages of this module type do not rely on legacy
1394 // behaviour.
1395 defaultEnabledStatus := false
1396 if anyScopesExplicitlyEnabled {
1397 defaultEnabledStatus = scope.defaultEnabledStatus
1398 } else {
1399 defaultEnabledStatus = scope.legacyEnabledStatus(module)
1400 }
1401 enabled := proptools.BoolDefault(scopeProperties.Enabled, defaultEnabledStatus)
1402 if enabled {
1403 enabledScopes[scope] = struct{}{}
1404 generatedScopes = append(generatedScopes, scope)
1405 }
1406 }
1407
1408 // Now check to make sure that any scope that is extended by an enabled scope is also
1409 // enabled.
1410 for _, scope := range allApiScopes {
1411 if _, ok := enabledScopes[scope]; ok {
1412 extends := scope.extends
1413 if extends != nil {
1414 if _, ok := enabledScopes[extends]; !ok {
1415 ctx.ModuleErrorf("enabled api scope %q depends on disabled scope %q", scope, extends)
1416 }
1417 }
1418 }
1419 }
1420
1421 return generatedScopes
Paul Duffind1b3a922020-01-22 11:57:20 +00001422}
1423
satayev758968a2021-12-06 11:42:40 +00001424var _ android.ModuleWithMinSdkVersionCheck = (*SdkLibrary)(nil)
1425
satayev8f088b02021-12-06 11:40:46 +00001426func (module *SdkLibrary) CheckMinSdkVersion(ctx android.ModuleContext) {
Spandan Das8c9ae7e2023-03-03 21:20:36 +00001427 android.CheckMinSdkVersion(ctx, module.MinSdkVersion(ctx), func(c android.ModuleContext, do android.PayloadDepsCallback) {
satayev8f088b02021-12-06 11:40:46 +00001428 ctx.WalkDeps(func(child android.Module, parent android.Module) bool {
1429 isExternal := !module.depIsInSameApex(ctx, child)
1430 if am, ok := child.(android.ApexModule); ok {
1431 if !do(ctx, parent, am, isExternal) {
1432 return false
1433 }
1434 }
1435 return !isExternal
1436 })
1437 })
1438}
1439
Paul Duffineedc5d52020-06-12 17:46:39 +01001440type sdkLibraryComponentTag struct {
1441 blueprint.BaseDependencyTag
1442 name string
1443}
1444
1445// Mark this tag so dependencies that use it are excluded from visibility enforcement.
1446func (t sdkLibraryComponentTag) ExcludeFromVisibilityEnforcement() {}
1447
1448var xmlPermissionsFileTag = sdkLibraryComponentTag{name: "xml-permissions-file"}
Paul Duffine74ac732020-02-06 13:51:46 +00001449
Jiyong Parke3833882020-02-17 17:28:10 +09001450func IsXmlPermissionsFileDepTag(depTag blueprint.DependencyTag) bool {
Paul Duffineedc5d52020-06-12 17:46:39 +01001451 if dt, ok := depTag.(sdkLibraryComponentTag); ok {
Jiyong Parke3833882020-02-17 17:28:10 +09001452 return dt == xmlPermissionsFileTag
1453 }
1454 return false
1455}
1456
Paul Duffineedc5d52020-06-12 17:46:39 +01001457var implLibraryTag = sdkLibraryComponentTag{name: "impl-library"}
Paul Duffin5df79302020-05-16 15:52:12 +01001458
Paul Duffin44f1d842020-06-26 20:17:02 +01001459// Add the dependencies on the child modules in the component deps mutator.
1460func (module *SdkLibrary) ComponentDepsMutator(ctx android.BottomUpMutatorContext) {
Paul Duffin3375e352020-04-28 10:44:03 +01001461 for _, apiScope := range module.getGeneratedApiScopes(ctx) {
Paul Duffind1b3a922020-01-22 11:57:20 +00001462 // Add dependencies to the stubs library
Spandan Das877f39d2023-03-29 16:19:51 +00001463 stubModuleName := module.stubsLibraryModuleName(apiScope)
Jihoon Kangbd093452023-12-26 19:08:01 +00001464 ctx.AddVariationDependencies(nil, apiScope.everythingStubsTag, stubModuleName)
Jihoon Kang1147b312023-06-08 23:25:57 +00001465
Jihoon Kangbd093452023-12-26 19:08:01 +00001466 exportableStubModuleName := module.exportableStubsLibraryModuleName(apiScope)
1467 ctx.AddVariationDependencies(nil, apiScope.exportableStubsTag, exportableStubModuleName)
Paul Duffind1b3a922020-01-22 11:57:20 +00001468
Paul Duffin15f34ef2020-07-20 18:04:44 +01001469 // Add a dependency on the stubs source in order to access both stubs source and api information.
1470 ctx.AddVariationDependencies(nil, apiScope.stubsSourceAndApiTag, module.stubsSourceModuleName(apiScope))
Paul Duffin958806b2022-05-16 13:10:47 +00001471
1472 if module.compareAgainstLatestApi(apiScope) {
1473 // Add dependencies on the latest finalized version of the API .txt file.
1474 latestApiModuleName := module.latestApiModuleName(apiScope)
1475 ctx.AddDependency(module, apiScope.latestApiModuleTag, latestApiModuleName)
1476
1477 // Add dependencies on the latest finalized version of the remove API .txt file.
1478 latestRemovedApiModuleName := module.latestRemovedApiModuleName(apiScope)
1479 ctx.AddDependency(module, apiScope.latestRemovedApiModuleTag, latestRemovedApiModuleName)
1480 }
Sundong Ahn054b19a2018-10-19 13:46:09 +09001481 }
1482
Paul Duffindfa131e2020-05-15 20:37:11 +01001483 if module.requiresRuntimeImplementationLibrary() {
Paul Duffin5df79302020-05-16 15:52:12 +01001484 // Add dependency to the rule for generating the implementation library.
1485 ctx.AddDependency(module, implLibraryTag, module.implLibraryModuleName())
1486
Paul Duffindfa131e2020-05-15 20:37:11 +01001487 if module.sharedLibrary() {
1488 // Add dependency to the rule for generating the xml permissions file
Paul Duffineedc5d52020-06-12 17:46:39 +01001489 ctx.AddDependency(module, xmlPermissionsFileTag, module.xmlPermissionsModuleName())
Paul Duffindfa131e2020-05-15 20:37:11 +01001490 }
Paul Duffin44f1d842020-06-26 20:17:02 +01001491 }
1492}
Paul Duffine74ac732020-02-06 13:51:46 +00001493
Paul Duffin44f1d842020-06-26 20:17:02 +01001494// Add other dependencies as normal.
1495func (module *SdkLibrary) DepsMutator(ctx android.BottomUpMutatorContext) {
Anton Hanssone77fccc2021-01-20 16:52:41 +00001496 var missingApiModules []string
1497 for _, apiScope := range module.getGeneratedApiScopes(ctx) {
1498 if apiScope.unstable {
1499 continue
1500 }
Paul Duffin958806b2022-05-16 13:10:47 +00001501 if m := module.latestApiModuleName(apiScope); !ctx.OtherModuleExists(m) {
Anton Hanssone77fccc2021-01-20 16:52:41 +00001502 missingApiModules = append(missingApiModules, m)
1503 }
Paul Duffin958806b2022-05-16 13:10:47 +00001504 if m := module.latestRemovedApiModuleName(apiScope); !ctx.OtherModuleExists(m) {
Anton Hanssone77fccc2021-01-20 16:52:41 +00001505 missingApiModules = append(missingApiModules, m)
1506 }
Paul Duffin958806b2022-05-16 13:10:47 +00001507 if m := module.latestIncompatibilitiesModuleName(apiScope); !ctx.OtherModuleExists(m) {
Jaewoong Jung1a97ee02021-03-09 13:25:02 -08001508 missingApiModules = append(missingApiModules, m)
1509 }
Anton Hanssone77fccc2021-01-20 16:52:41 +00001510 }
1511 if len(missingApiModules) != 0 && !module.sdkLibraryProperties.Unsafe_ignore_missing_latest_api {
1512 m := module.Name() + " is missing tracking files for previously released library versions.\n"
1513 m += "You need to do one of the following:\n"
1514 m += "- Add `unsafe_ignore_missing_latest_api: true` to your blueprint (to disable compat tracking)\n"
1515 m += "- Add a set of prebuilt txt files representing the last released version of this library for compat checking.\n"
1516 m += " (the current set of API files can be used as a seed for this compatibility tracking\n"
1517 m += "\n"
1518 m += "The following filegroup modules are missing:\n "
1519 m += strings.Join(missingApiModules, "\n ") + "\n"
1520 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."
1521 ctx.ModuleErrorf(m)
1522 }
Paul Duffin44f1d842020-06-26 20:17:02 +01001523 if module.requiresRuntimeImplementationLibrary() {
Paul Duffindfa131e2020-05-15 20:37:11 +01001524 // Only add the deps for the library if it is actually going to be built.
1525 module.Library.deps(ctx)
1526 }
Jiyong Parkc678ad32018-04-10 13:07:10 +09001527}
1528
Paul Duffin46dc45a2020-05-14 15:39:10 +01001529func (module *SdkLibrary) OutputFiles(tag string) (android.Paths, error) {
1530 paths, err := module.commonOutputFiles(tag)
Colin Cross4acaea92021-12-10 23:05:02 +00001531 if paths != nil || err != nil {
Paul Duffin46dc45a2020-05-14 15:39:10 +01001532 return paths, err
1533 }
Colin Cross4acaea92021-12-10 23:05:02 +00001534 if module.requiresRuntimeImplementationLibrary() {
1535 return module.Library.OutputFiles(tag)
1536 }
1537 if tag == "" {
1538 return nil, nil
1539 }
1540 return nil, fmt.Errorf("unsupported module reference tag %q", tag)
Paul Duffin46dc45a2020-05-14 15:39:10 +01001541}
1542
Inseob Kimc0907f12019-02-08 21:00:45 +09001543func (module *SdkLibrary) GenerateAndroidBuildActions(ctx android.ModuleContext) {
satayev8f088b02021-12-06 11:40:46 +00001544 if proptools.String(module.deviceProperties.Min_sdk_version) != "" {
1545 module.CheckMinSdkVersion(ctx)
1546 }
1547
Paul Duffina2ae7e02020-09-11 11:55:00 +01001548 module.generateCommonBuildActions(ctx)
1549
Paul Duffindfa131e2020-05-15 20:37:11 +01001550 // Only build an implementation library if required.
1551 if module.requiresRuntimeImplementationLibrary() {
Paul Duffin43db9be2019-12-30 17:35:49 +00001552 module.Library.GenerateAndroidBuildActions(ctx)
1553 }
Sundong Ahn054b19a2018-10-19 13:46:09 +09001554
Paul Duffinb97b1572021-04-29 21:50:40 +01001555 // Collate the components exported by this module. All scope specific modules are exported but
1556 // the impl and xml component modules are not.
1557 exportedComponents := map[string]struct{}{}
1558
Sundong Ahn57368eb2018-07-06 11:20:23 +09001559 // Record the paths to the header jars of the library (stubs and impl).
Paul Duffind1b3a922020-01-22 11:57:20 +00001560 // When this java_sdk_library is depended upon from others via "libs" property,
Jiyong Parkc678ad32018-04-10 13:07:10 +09001561 // the recorded paths will be returned depending on the link type of the caller.
1562 ctx.VisitDirectDeps(func(to android.Module) {
Jiyong Parkc678ad32018-04-10 13:07:10 +09001563 tag := ctx.OtherModuleDependencyTag(to)
1564
Paul Duffinc8782502020-04-29 20:45:27 +01001565 // Extract information from any of the scope specific dependencies.
1566 if scopeTag, ok := tag.(scopeDependencyTag); ok {
1567 apiScope := scopeTag.apiScope
Paul Duffin803a9562020-05-20 11:52:25 +01001568 scopePaths := module.getScopePathsCreateIfNeeded(apiScope)
Paul Duffinc8782502020-04-29 20:45:27 +01001569
1570 // Extract information from the dependency. The exact information extracted
1571 // is determined by the nature of the dependency which is determined by the tag.
1572 scopeTag.extractDepInfo(ctx, to, scopePaths)
Paul Duffinb97b1572021-04-29 21:50:40 +01001573
1574 exportedComponents[ctx.OtherModuleName(to)] = struct{}{}
Sundong Ahn20e998b2018-07-24 11:19:26 +09001575 }
Jiyong Parkc678ad32018-04-10 13:07:10 +09001576 })
Paul Duffinb97b1572021-04-29 21:50:40 +01001577
1578 // Make the set of components exported by this module available for use elsewhere.
Cole Faust18994c72023-02-28 16:02:16 -08001579 exportedComponentInfo := android.ExportedComponentsInfo{Components: android.SortedKeys(exportedComponents)}
Colin Cross40213022023-12-13 15:19:49 -08001580 android.SetProvider(ctx, android.ExportedComponentsInfoProvider, exportedComponentInfo)
Paul Duffin958806b2022-05-16 13:10:47 +00001581
1582 // Provide additional information for inclusion in an sdk's generated .info file.
1583 additionalSdkInfo := map[string]interface{}{}
1584 additionalSdkInfo["dist_stem"] = module.distStem()
Paul Duffine8409952022-09-22 16:24:46 +01001585 baseModuleName := module.distStem()
Paul Duffin958806b2022-05-16 13:10:47 +00001586 scopes := map[string]interface{}{}
1587 additionalSdkInfo["scopes"] = scopes
1588 for scope, scopePaths := range module.scopePaths {
1589 scopeInfo := map[string]interface{}{}
1590 scopes[scope.name] = scopeInfo
1591 scopeInfo["current_api"] = scope.snapshotRelativeCurrentApiTxtPath(baseModuleName)
1592 scopeInfo["removed_api"] = scope.snapshotRelativeRemovedApiTxtPath(baseModuleName)
1593 if p := scopePaths.latestApiPath; p.Valid() {
1594 scopeInfo["latest_api"] = p.Path().String()
1595 }
1596 if p := scopePaths.latestRemovedApiPath; p.Valid() {
1597 scopeInfo["latest_removed_api"] = p.Path().String()
1598 }
1599 }
Colin Cross40213022023-12-13 15:19:49 -08001600 android.SetProvider(ctx, android.AdditionalSdkInfoProvider, android.AdditionalSdkInfo{additionalSdkInfo})
Jiyong Parkc678ad32018-04-10 13:07:10 +09001601}
1602
Jiyong Park0b0e1b92019-12-03 13:24:29 +09001603func (module *SdkLibrary) AndroidMkEntries() []android.AndroidMkEntries {
Paul Duffindfa131e2020-05-15 20:37:11 +01001604 if !module.requiresRuntimeImplementationLibrary() {
Paul Duffin43db9be2019-12-30 17:35:49 +00001605 return nil
1606 }
Jiyong Park0b0e1b92019-12-03 13:24:29 +09001607 entriesList := module.Library.AndroidMkEntries()
Yo Chiang07d75072020-06-05 17:43:19 +08001608 if module.sharedLibrary() {
1609 entries := &entriesList[0]
1610 entries.Required = append(entries.Required, module.xmlPermissionsModuleName())
1611 }
Jiyong Park0b0e1b92019-12-03 13:24:29 +09001612 return entriesList
Jiyong Park82484c02018-04-23 21:41:26 +09001613}
1614
Anton Hansson5fd5d242020-03-27 19:43:19 +00001615// The dist path of the stub artifacts
1616func (module *SdkLibrary) apiDistPath(apiScope *apiScope) string {
Colin Crossf0eace92021-06-02 13:02:23 -07001617 return path.Join("apistubs", module.distGroup(), apiScope.name)
Anton Hansson5fd5d242020-03-27 19:43:19 +00001618}
1619
Paul Duffin12ceb462019-12-24 20:31:31 +00001620// Get the sdk version for use when compiling the stubs library.
Paul Duffin780c5f42020-05-12 15:52:55 +01001621func (module *SdkLibrary) sdkVersionForStubsLibrary(mctx android.EarlyModuleContext, apiScope *apiScope) string {
Paul Duffin87a05a32020-05-12 11:50:28 +01001622 scopeProperties := module.scopeToProperties[apiScope]
1623 if scopeProperties.Sdk_version != nil {
1624 return proptools.String(scopeProperties.Sdk_version)
1625 }
1626
Jiyong Parkf1691d22021-03-29 20:11:58 +09001627 sdkDep := decodeSdkDep(mctx, android.SdkContext(&module.Library))
Paul Duffin12ceb462019-12-24 20:31:31 +00001628 if sdkDep.hasStandardLibs() {
1629 // If building against a standard sdk then use the sdk version appropriate for the scope.
Paul Duffind1b3a922020-01-22 11:57:20 +00001630 return apiScope.sdkVersion
Paul Duffin12ceb462019-12-24 20:31:31 +00001631 } else {
1632 // Otherwise, use no system module.
1633 return "none"
1634 }
1635}
1636
Paul Duffin31310252020-11-20 21:26:20 +00001637func (module *SdkLibrary) distStem() string {
1638 return proptools.StringDefault(module.sdkLibraryProperties.Dist_stem, module.BaseModuleName())
1639}
1640
Colin Cross986b69a2021-06-01 13:13:40 -07001641// distGroup returns the subdirectory of the dist path of the stub artifacts.
1642func (module *SdkLibrary) distGroup() string {
Colin Cross59b92bf2021-06-01 14:07:56 -07001643 return proptools.StringDefault(module.sdkLibraryProperties.Dist_group, "unknown")
Colin Cross986b69a2021-06-01 13:13:40 -07001644}
1645
Paul Duffin958806b2022-05-16 13:10:47 +00001646func latestPrebuiltApiModuleName(name string, apiScope *apiScope) string {
1647 return PrebuiltApiModuleName(name, apiScope.name, "latest")
1648}
1649
Paul Duffind1b3a922020-01-22 11:57:20 +00001650func (module *SdkLibrary) latestApiFilegroupName(apiScope *apiScope) string {
Paul Duffin958806b2022-05-16 13:10:47 +00001651 return ":" + module.latestApiModuleName(apiScope)
1652}
1653
1654func (module *SdkLibrary) latestApiModuleName(apiScope *apiScope) string {
1655 return latestPrebuiltApiModuleName(module.distStem(), apiScope)
Jiyong Park58c518b2018-05-12 22:29:12 +09001656}
Jiyong Parkc678ad32018-04-10 13:07:10 +09001657
Paul Duffind1b3a922020-01-22 11:57:20 +00001658func (module *SdkLibrary) latestRemovedApiFilegroupName(apiScope *apiScope) string {
Paul Duffin958806b2022-05-16 13:10:47 +00001659 return ":" + module.latestRemovedApiModuleName(apiScope)
1660}
1661
1662func (module *SdkLibrary) latestRemovedApiModuleName(apiScope *apiScope) string {
1663 return latestPrebuiltApiModuleName(module.distStem()+"-removed", apiScope)
Jiyong Parkc678ad32018-04-10 13:07:10 +09001664}
1665
Jaewoong Jung1a97ee02021-03-09 13:25:02 -08001666func (module *SdkLibrary) latestIncompatibilitiesFilegroupName(apiScope *apiScope) string {
Paul Duffin958806b2022-05-16 13:10:47 +00001667 return ":" + module.latestIncompatibilitiesModuleName(apiScope)
1668}
1669
1670func (module *SdkLibrary) latestIncompatibilitiesModuleName(apiScope *apiScope) string {
1671 return latestPrebuiltApiModuleName(module.distStem()+"-incompatibilities", apiScope)
Jaewoong Jung1a97ee02021-03-09 13:25:02 -08001672}
1673
Jihoon Kang1c92c3e2023-03-23 17:44:51 +00001674func (module *SdkLibrary) contributesToApiSurface(c android.Config) bool {
1675 _, exists := c.GetApiLibraries()[module.Name()]
1676 return exists
1677}
1678
Jihoon Kang0c705a42023-08-02 06:44:57 +00001679// The listed modules are the special java_sdk_libraries where apiScope.kind do not match the
1680// api surface that the module contribute to. For example, the public droidstubs and java_library
1681// do not contribute to the public api surface, but contributes to the core platform api surface.
1682// This method returns the full api surface stub lib that
1683// the generated java_api_library should depend on.
1684func (module *SdkLibrary) alternativeFullApiSurfaceStubLib() string {
1685 if val, ok := apiLibraryAdditionalProperties[module.Name()]; ok {
1686 return val.FullApiSurfaceStubLib
1687 }
1688 return ""
1689}
1690
1691// The listed modules' stubs contents do not match the corresponding txt files,
1692// but require additional api contributions to generate the full stubs.
1693// This method returns the name of the additional api contribution module
1694// for corresponding sdk_library modules.
1695func (module *SdkLibrary) apiLibraryAdditionalApiContribution() string {
1696 if val, ok := apiLibraryAdditionalProperties[module.Name()]; ok {
1697 return val.AdditionalApiContribution
1698 }
1699 return ""
1700}
1701
Anton Hansson944e77d2020-08-19 11:40:22 +01001702func childModuleVisibility(childVisibility []string) []string {
1703 if childVisibility == nil {
1704 // No child visibility set. The child will use the visibility of the sdk_library.
1705 return nil
1706 }
1707
1708 // Prepend an override to ignore the sdk_library's visibility, and rely on the child visibility.
1709 var visibility []string
1710 visibility = append(visibility, "//visibility:override")
1711 visibility = append(visibility, childVisibility...)
1712 return visibility
1713}
1714
Paul Duffin5df79302020-05-16 15:52:12 +01001715// Creates the implementation java library
1716func (module *SdkLibrary) createImplLibrary(mctx android.DefaultableHookContext) {
Anton Hansson944e77d2020-08-19 11:40:22 +01001717 visibility := childModuleVisibility(module.sdkLibraryProperties.Impl_library_visibility)
1718
Paul Duffin5df79302020-05-16 15:52:12 +01001719 props := struct {
Paul Duffin77590a82022-04-28 14:13:30 +00001720 Name *string
1721 Visibility []string
1722 Instrument bool
1723 Libs []string
1724 Static_libs []string
1725 Apex_available []string
Paul Duffin5df79302020-05-16 15:52:12 +01001726 }{
1727 Name: proptools.StringPtr(module.implLibraryModuleName()),
Anton Hansson944e77d2020-08-19 11:40:22 +01001728 Visibility: visibility,
Paul Duffin296cf332020-06-18 21:09:55 +01001729 // Set the instrument property to ensure it is instrumented when instrumentation is required.
1730 Instrument: true,
Anton Hansson7f66efa2020-10-08 14:47:23 +01001731 // Set the impl_only libs. Note that the module's "Libs" get appended as well, via the
1732 // addition of &module.properties below.
1733 Libs: module.sdkLibraryProperties.Impl_only_libs,
Paul Duffin77590a82022-04-28 14:13:30 +00001734 // Set the impl_only static libs. Note that the module's "static_libs" get appended as well, via the
1735 // addition of &module.properties below.
1736 Static_libs: module.sdkLibraryProperties.Impl_only_static_libs,
1737 // Pass the apex_available settings down so that the impl library can be statically
1738 // embedded within a library that is added to an APEX. Needed for updatable-media.
1739 Apex_available: module.ApexAvailable(),
Paul Duffin5df79302020-05-16 15:52:12 +01001740 }
1741
1742 properties := []interface{}{
1743 &module.properties,
1744 &module.protoProperties,
1745 &module.deviceProperties,
Liz Kammera7a64f32020-07-09 15:16:41 -07001746 &module.dexProperties,
Paul Duffin5df79302020-05-16 15:52:12 +01001747 &module.dexpreoptProperties,
Colin Cross014489c2020-06-02 20:09:13 -07001748 &module.linter.properties,
Paul Duffin5df79302020-05-16 15:52:12 +01001749 &props,
1750 module.sdkComponentPropertiesForChildLibrary(),
1751 }
1752 mctx.CreateModule(LibraryFactory, properties...)
1753}
1754
Jihoon Kangfa4a90d2023-12-20 02:53:38 +00001755type libraryProperties struct {
1756 Name *string
1757 Visibility []string
1758 Srcs []string
1759 Installable *bool
1760 Sdk_version *string
1761 System_modules *string
1762 Patch_module *string
1763 Libs []string
1764 Static_libs []string
1765 Compile_dex *bool
1766 Java_version *string
1767 Openjdk9 struct {
1768 Srcs []string
1769 Javacflags []string
1770 }
1771 Dist struct {
1772 Targets []string
1773 Dest *string
1774 Dir *string
1775 Tag *string
1776 }
1777}
Jiyong Parkc678ad32018-04-10 13:07:10 +09001778
Jihoon Kangfa4a90d2023-12-20 02:53:38 +00001779func (module *SdkLibrary) stubsLibraryProps(mctx android.DefaultableHookContext, apiScope *apiScope) libraryProperties {
1780 props := libraryProperties{}
Jihoon Kang786df932023-09-07 01:18:31 +00001781 props.Visibility = []string{"//visibility:override", "//visibility:private"}
Jiyong Parkc678ad32018-04-10 13:07:10 +09001782 // sources are generated from the droiddoc
Paul Duffin12ceb462019-12-24 20:31:31 +00001783 sdkVersion := module.sdkVersionForStubsLibrary(mctx, apiScope)
Paul Duffin52d398a2019-06-11 12:31:14 +01001784 props.Sdk_version = proptools.StringPtr(sdkVersion)
Paul Duffina18abc22020-05-16 18:54:24 +01001785 props.System_modules = module.deviceProperties.System_modules
1786 props.Patch_module = module.properties.Patch_module
Paul Duffin367ab912019-12-23 19:40:36 +00001787 props.Installable = proptools.BoolPtr(false)
Sundong Ahn054b19a2018-10-19 13:46:09 +09001788 props.Libs = module.sdkLibraryProperties.Stub_only_libs
Mark White9421c4c2023-08-10 00:07:03 +00001789 props.Libs = append(props.Libs, module.scopeToProperties[apiScope].Libs...)
Anton Hanssondae54cd2021-04-21 16:30:10 +01001790 props.Static_libs = module.sdkLibraryProperties.Stub_only_static_libs
Paul Duffine22c2ab2020-05-20 19:35:27 +01001791 // The stub-annotations library contains special versions of the annotations
1792 // with CLASS retention policy, so that they're kept.
1793 if proptools.Bool(module.sdkLibraryProperties.Annotations_enabled) {
1794 props.Libs = append(props.Libs, "stub-annotations")
1795 }
Paul Duffina18abc22020-05-16 18:54:24 +01001796 props.Openjdk9.Srcs = module.properties.Openjdk9.Srcs
1797 props.Openjdk9.Javacflags = module.properties.Openjdk9.Javacflags
Anton Hansson83509b52020-05-21 09:21:57 +01001798 // We compile the stubs for 1.8 in line with the main android.jar stubs, and potential
1799 // interop with older developer tools that don't support 1.9.
1800 props.Java_version = proptools.StringPtr("1.8")
Paul Duffinf4600f62021-05-13 22:34:45 +01001801
Jihoon Kangfa4a90d2023-12-20 02:53:38 +00001802 return props
1803}
1804
1805// Creates a static java library that has API stubs
1806func (module *SdkLibrary) createStubsLibrary(mctx android.DefaultableHookContext, apiScope *apiScope) {
1807
1808 props := module.stubsLibraryProps(mctx, apiScope)
1809 props.Name = proptools.StringPtr(module.sourceStubsLibraryModuleName(apiScope))
1810 props.Srcs = []string{":" + module.stubsSourceModuleName(apiScope)}
1811
1812 mctx.CreateModule(LibraryFactory, &props, module.sdkComponentPropertiesForChildLibrary())
1813}
1814
1815// Create a static java library that compiles the "exportable" stubs
1816func (module *SdkLibrary) createExportableStubsLibrary(mctx android.DefaultableHookContext, apiScope *apiScope) {
1817 props := module.stubsLibraryProps(mctx, apiScope)
1818 props.Name = proptools.StringPtr(module.exportableSourceStubsLibraryModuleName(apiScope))
1819 props.Srcs = []string{":" + module.stubsSourceModuleName(apiScope) + "{.exportable}"}
1820
Paul Duffin859fe962020-05-15 10:20:31 +01001821 mctx.CreateModule(LibraryFactory, &props, module.sdkComponentPropertiesForChildLibrary())
Jiyong Parkc678ad32018-04-10 13:07:10 +09001822}
1823
Paul Duffin6d0886e2020-04-07 18:49:53 +01001824// Creates a droidstubs module that creates stubs source files from the given full source
Paul Duffinc8782502020-04-29 20:45:27 +01001825// files and also updates and checks the API specification files.
Paul Duffin15f34ef2020-07-20 18:04:44 +01001826func (module *SdkLibrary) createStubsSourcesAndApi(mctx android.DefaultableHookContext, apiScope *apiScope, name string, scopeSpecificDroidstubsArgs []string) {
Jiyong Parkc678ad32018-04-10 13:07:10 +09001827 props := struct {
Sundong Ahn054b19a2018-10-19 13:46:09 +09001828 Name *string
Paul Duffin4911a892020-04-29 23:35:13 +01001829 Visibility []string
Sundong Ahn054b19a2018-10-19 13:46:09 +09001830 Srcs []string
1831 Installable *bool
Paul Duffin52d398a2019-06-11 12:31:14 +01001832 Sdk_version *string
Jihoon Kang3198f3c2023-01-26 08:08:52 +00001833 Api_surface *string
Paul Duffin12ceb462019-12-24 20:31:31 +00001834 System_modules *string
Sundong Ahn054b19a2018-10-19 13:46:09 +09001835 Libs []string
Paul Duffin6877e6d2020-09-25 19:59:14 +01001836 Output_javadoc_comments *bool
Paul Duffin11512472019-02-11 15:55:17 +00001837 Arg_files []string
Sundong Ahn054b19a2018-10-19 13:46:09 +09001838 Args *string
Sundong Ahn054b19a2018-10-19 13:46:09 +09001839 Java_version *string
Paul Duffine22c2ab2020-05-20 19:35:27 +01001840 Annotations_enabled *bool
Sundong Ahn054b19a2018-10-19 13:46:09 +09001841 Merge_annotations_dirs []string
1842 Merge_inclusion_annotations_dirs []string
Paul Duffin0ff08bd2020-04-29 13:30:54 +01001843 Generate_stubs *bool
Anton Hanssone87b03d2020-12-21 15:29:34 +00001844 Previous_api *string
Jihoon Kang6592e872023-12-19 01:13:16 +00001845 Aconfig_declarations []string
Sundong Ahn054b19a2018-10-19 13:46:09 +09001846 Check_api struct {
Anton Hanssone6056152020-12-31 10:37:27 +00001847 Current ApiToCheck
1848 Last_released ApiToCheck
Paul Duffin160fe412020-05-10 19:32:20 +01001849
1850 Api_lint struct {
1851 Enabled *bool
1852 New_since *string
1853 Baseline_file *string
1854 }
Jiyong Park58c518b2018-05-12 22:29:12 +09001855 }
Sundong Ahn1b92c822018-05-29 11:35:17 +09001856 Aidl struct {
1857 Include_dirs []string
1858 Local_include_dirs []string
1859 }
Paul Duffin040e9062020-11-23 17:41:36 +00001860 Dists []android.Dist
Jiyong Parkc678ad32018-04-10 13:07:10 +09001861 }{}
1862
Paul Duffin7b78b4d2020-04-28 14:08:32 +01001863 // The stubs source processing uses the same compile time classpath when extracting the
1864 // API from the implementation library as it does when compiling it. i.e. the same
1865 // * sdk version
1866 // * system_modules
1867 // * libs (static_libs/libs)
Paul Duffin250e6192019-06-07 10:44:37 +01001868
Paul Duffin0ff08bd2020-04-29 13:30:54 +01001869 props.Name = proptools.StringPtr(name)
Anton Hansson944e77d2020-08-19 11:40:22 +01001870 props.Visibility = childModuleVisibility(module.sdkLibraryProperties.Stubs_source_visibility)
Paul Duffina18abc22020-05-16 18:54:24 +01001871 props.Srcs = append(props.Srcs, module.properties.Srcs...)
Anton Hanssonf8ea3722021-09-16 14:24:13 +01001872 props.Srcs = append(props.Srcs, module.sdkLibraryProperties.Api_srcs...)
Paul Duffina18abc22020-05-16 18:54:24 +01001873 props.Sdk_version = module.deviceProperties.Sdk_version
Jihoon Kang3198f3c2023-01-26 08:08:52 +00001874 props.Api_surface = &apiScope.name
Paul Duffina18abc22020-05-16 18:54:24 +01001875 props.System_modules = module.deviceProperties.System_modules
Jiyong Parkc678ad32018-04-10 13:07:10 +09001876 props.Installable = proptools.BoolPtr(false)
Sundong Ahne6f0b052018-06-05 16:46:14 +09001877 // A droiddoc module has only one Libs property and doesn't distinguish between
1878 // shared libs and static libs. So we need to add both of these libs to Libs property.
Paul Duffina18abc22020-05-16 18:54:24 +01001879 props.Libs = module.properties.Libs
1880 props.Libs = append(props.Libs, module.properties.Static_libs...)
Nikita Ioffed732da72022-11-21 12:38:25 +00001881 props.Libs = append(props.Libs, module.sdkLibraryProperties.Stub_only_libs...)
Mark White9421c4c2023-08-10 00:07:03 +00001882 props.Libs = append(props.Libs, module.scopeToProperties[apiScope].Libs...)
Paul Duffina18abc22020-05-16 18:54:24 +01001883 props.Aidl.Include_dirs = module.deviceProperties.Aidl.Include_dirs
1884 props.Aidl.Local_include_dirs = module.deviceProperties.Aidl.Local_include_dirs
1885 props.Java_version = module.properties.Java_version
Jiyong Parkc678ad32018-04-10 13:07:10 +09001886
Paul Duffine22c2ab2020-05-20 19:35:27 +01001887 props.Annotations_enabled = module.sdkLibraryProperties.Annotations_enabled
Sundong Ahn054b19a2018-10-19 13:46:09 +09001888 props.Merge_annotations_dirs = module.sdkLibraryProperties.Merge_annotations_dirs
1889 props.Merge_inclusion_annotations_dirs = module.sdkLibraryProperties.Merge_inclusion_annotations_dirs
Jihoon Kang6592e872023-12-19 01:13:16 +00001890 props.Aconfig_declarations = module.sdkLibraryProperties.Aconfig_declarations
Sundong Ahn054b19a2018-10-19 13:46:09 +09001891
Paul Duffin6d0886e2020-04-07 18:49:53 +01001892 droidstubsArgs := []string{}
Paul Duffin235ffff2019-12-24 10:41:30 +00001893 if len(module.sdkLibraryProperties.Api_packages) != 0 {
Paul Duffin6d0886e2020-04-07 18:49:53 +01001894 droidstubsArgs = append(droidstubsArgs, "--stub-packages "+strings.Join(module.sdkLibraryProperties.Api_packages, ":"))
Paul Duffin235ffff2019-12-24 10:41:30 +00001895 }
1896 if len(module.sdkLibraryProperties.Hidden_api_packages) != 0 {
Paul Duffin6d0886e2020-04-07 18:49:53 +01001897 droidstubsArgs = append(droidstubsArgs,
Paul Duffin235ffff2019-12-24 10:41:30 +00001898 android.JoinWithPrefix(module.sdkLibraryProperties.Hidden_api_packages, " --hide-package "))
1899 }
Paul Duffin6d0886e2020-04-07 18:49:53 +01001900 droidstubsArgs = append(droidstubsArgs, module.sdkLibraryProperties.Droiddoc_options...)
Anton Hanssonfd1c0d22023-11-02 15:18:09 +00001901 disabledWarnings := []string{"HiddenSuperclass"}
1902 if proptools.BoolDefault(module.sdkLibraryProperties.Api_lint.Legacy_errors_allowed, true) {
1903 disabledWarnings = append(disabledWarnings,
1904 "BroadcastBehavior",
1905 "DeprecationMismatch",
1906 "MissingPermission",
1907 "SdkConstant",
1908 "Todo",
1909 )
Paul Duffin235ffff2019-12-24 10:41:30 +00001910 }
Paul Duffin6d0886e2020-04-07 18:49:53 +01001911 droidstubsArgs = append(droidstubsArgs, android.JoinWithPrefix(disabledWarnings, "--hide "))
Sundong Ahnfb2721f2018-09-17 13:23:09 +09001912
Paul Duffin6877e6d2020-09-25 19:59:14 +01001913 // Output Javadoc comments for public scope.
1914 if apiScope == apiScopePublic {
1915 props.Output_javadoc_comments = proptools.BoolPtr(true)
1916 }
1917
Paul Duffin1fb487d2020-04-07 18:50:10 +01001918 // Add in scope specific arguments.
Paul Duffin0ff08bd2020-04-29 13:30:54 +01001919 droidstubsArgs = append(droidstubsArgs, scopeSpecificDroidstubsArgs...)
Paul Duffin11512472019-02-11 15:55:17 +00001920 props.Arg_files = module.sdkLibraryProperties.Droiddoc_option_files
Paul Duffin6d0886e2020-04-07 18:49:53 +01001921 props.Args = proptools.StringPtr(strings.Join(droidstubsArgs, " "))
Jiyong Parkc678ad32018-04-10 13:07:10 +09001922
Paul Duffin15f34ef2020-07-20 18:04:44 +01001923 // List of APIs identified from the provided source files are created. They are later
1924 // compared against to the not-yet-released (a.k.a current) list of APIs and to the
1925 // last-released (a.k.a numbered) list of API.
1926 currentApiFileName := apiScope.apiFilePrefix + "current.txt"
1927 removedApiFileName := apiScope.apiFilePrefix + "removed.txt"
1928 apiDir := module.getApiDir()
1929 currentApiFileName = path.Join(apiDir, currentApiFileName)
1930 removedApiFileName = path.Join(apiDir, removedApiFileName)
Jiyong Parkc678ad32018-04-10 13:07:10 +09001931
Paul Duffin15f34ef2020-07-20 18:04:44 +01001932 // check against the not-yet-release API
1933 props.Check_api.Current.Api_file = proptools.StringPtr(currentApiFileName)
1934 props.Check_api.Current.Removed_api_file = proptools.StringPtr(removedApiFileName)
Jiyong Park58c518b2018-05-12 22:29:12 +09001935
Paul Duffin958806b2022-05-16 13:10:47 +00001936 if module.compareAgainstLatestApi(apiScope) {
Paul Duffin15f34ef2020-07-20 18:04:44 +01001937 // check against the latest released API
1938 latestApiFilegroupName := proptools.StringPtr(module.latestApiFilegroupName(apiScope))
Anton Hanssone87b03d2020-12-21 15:29:34 +00001939 props.Previous_api = latestApiFilegroupName
Paul Duffin15f34ef2020-07-20 18:04:44 +01001940 props.Check_api.Last_released.Api_file = latestApiFilegroupName
1941 props.Check_api.Last_released.Removed_api_file = proptools.StringPtr(
1942 module.latestRemovedApiFilegroupName(apiScope))
Jaewoong Jung1a97ee02021-03-09 13:25:02 -08001943 props.Check_api.Last_released.Baseline_file = proptools.StringPtr(
1944 module.latestIncompatibilitiesFilegroupName(apiScope))
Paul Duffin160fe412020-05-10 19:32:20 +01001945
Paul Duffin15f34ef2020-07-20 18:04:44 +01001946 if proptools.Bool(module.sdkLibraryProperties.Api_lint.Enabled) {
1947 // Enable api lint.
1948 props.Check_api.Api_lint.Enabled = proptools.BoolPtr(true)
1949 props.Check_api.Api_lint.New_since = latestApiFilegroupName
Paul Duffin160fe412020-05-10 19:32:20 +01001950
Paul Duffin15f34ef2020-07-20 18:04:44 +01001951 // If it exists then pass a lint-baseline.txt through to droidstubs.
1952 baselinePath := path.Join(apiDir, apiScope.apiFilePrefix+"lint-baseline.txt")
1953 baselinePathRelativeToRoot := path.Join(mctx.ModuleDir(), baselinePath)
1954 paths, err := mctx.GlobWithDeps(baselinePathRelativeToRoot, nil)
1955 if err != nil {
1956 mctx.ModuleErrorf("error checking for presence of %s: %s", baselinePathRelativeToRoot, err)
1957 }
1958 if len(paths) == 1 {
1959 props.Check_api.Api_lint.Baseline_file = proptools.StringPtr(baselinePath)
1960 } else if len(paths) != 0 {
1961 mctx.ModuleErrorf("error checking for presence of %s: expected one path, found: %v", baselinePathRelativeToRoot, paths)
Paul Duffin160fe412020-05-10 19:32:20 +01001962 }
1963 }
Paul Duffin15f34ef2020-07-20 18:04:44 +01001964 }
Jiyong Park58c518b2018-05-12 22:29:12 +09001965
Paul Duffin15f34ef2020-07-20 18:04:44 +01001966 if !Bool(module.sdkLibraryProperties.No_dist) {
Paul Duffin040e9062020-11-23 17:41:36 +00001967 // Dist the api txt and removed api txt artifacts for sdk builds.
1968 distDir := proptools.StringPtr(path.Join(module.apiDistPath(apiScope), "api"))
1969 for _, p := range []struct {
1970 tag string
1971 pattern string
1972 }{
1973 {tag: ".api.txt", pattern: "%s.txt"},
1974 {tag: ".removed-api.txt", pattern: "%s-removed.txt"},
1975 } {
1976 props.Dists = append(props.Dists, android.Dist{
1977 Targets: []string{"sdk", "win_sdk"},
1978 Dir: distDir,
1979 Dest: proptools.StringPtr(fmt.Sprintf(p.pattern, module.distStem())),
1980 Tag: proptools.StringPtr(p.tag),
1981 })
1982 }
Anton Hansson5fd5d242020-03-27 19:43:19 +00001983 }
1984
Spandan Das2cc80ba2023-10-27 17:21:52 +00001985 mctx.CreateModule(DroidstubsFactory, &props, module.sdkComponentPropertiesForChildLibrary()).(*Droidstubs).CallHookIfAvailable(mctx)
Jiyong Parkc678ad32018-04-10 13:07:10 +09001986}
1987
Jihoon Kang0c705a42023-08-02 06:44:57 +00001988func (module *SdkLibrary) createApiLibrary(mctx android.DefaultableHookContext, apiScope *apiScope, alternativeFullApiSurfaceStub string) {
Jihoon Kang1c92c3e2023-03-23 17:44:51 +00001989 props := struct {
Jihoon Kangca198c22023-06-22 23:13:51 +00001990 Name *string
1991 Visibility []string
1992 Api_contributions []string
1993 Libs []string
1994 Static_libs []string
1995 Full_api_surface_stub *string
Jihoon Kang4ec24872023-10-05 17:26:09 +00001996 System_modules *string
Jihoon Kang063ec002023-06-28 01:16:23 +00001997 Enable_validation *bool
Jihoon Kang1c92c3e2023-03-23 17:44:51 +00001998 }{}
1999
2000 props.Name = proptools.StringPtr(module.apiLibraryModuleName(apiScope))
Jihoon Kang786df932023-09-07 01:18:31 +00002001 props.Visibility = []string{"//visibility:override", "//visibility:private"}
Jihoon Kang1c92c3e2023-03-23 17:44:51 +00002002
2003 apiContributions := []string{}
2004
2005 // Api surfaces are not independent of each other, but have subset relationships,
2006 // and so does the api files. To generate from-text stubs for api surfaces other than public,
2007 // all subset api domains' api_contriubtions must be added as well.
2008 scope := apiScope
2009 for scope != nil {
2010 apiContributions = append(apiContributions, module.stubsSourceModuleName(scope)+".api.contribution")
2011 scope = scope.extends
2012 }
Jihoon Kang0c705a42023-08-02 06:44:57 +00002013 if apiScope == apiScopePublic {
2014 additionalApiContribution := module.apiLibraryAdditionalApiContribution()
2015 if additionalApiContribution != "" {
2016 apiContributions = append(apiContributions, additionalApiContribution)
2017 }
2018 }
Jihoon Kang1c92c3e2023-03-23 17:44:51 +00002019
2020 props.Api_contributions = apiContributions
2021 props.Libs = module.properties.Libs
2022 props.Libs = append(props.Libs, module.sdkLibraryProperties.Stub_only_libs...)
Mark White9421c4c2023-08-10 00:07:03 +00002023 props.Libs = append(props.Libs, module.scopeToProperties[apiScope].Libs...)
Jihoon Kang1c92c3e2023-03-23 17:44:51 +00002024 props.Libs = append(props.Libs, "stub-annotations")
2025 props.Static_libs = module.sdkLibraryProperties.Stub_only_static_libs
Jihoon Kange7ee2562023-07-25 05:51:46 +00002026 props.Full_api_surface_stub = proptools.StringPtr(apiScope.kind.DefaultJavaLibraryName())
Jihoon Kang0c705a42023-08-02 06:44:57 +00002027 if alternativeFullApiSurfaceStub != "" {
2028 props.Full_api_surface_stub = proptools.StringPtr(alternativeFullApiSurfaceStub)
2029 }
Jihoon Kang1c92c3e2023-03-23 17:44:51 +00002030
2031 // android_module_lib_stubs_current.from-text only comprises api contributions from art, conscrypt and i18n.
2032 // Thus, replace with android_module_lib_stubs_current_full.from-text, which comprises every api domains.
2033 if apiScope.kind == android.SdkModule {
Jihoon Kangca198c22023-06-22 23:13:51 +00002034 props.Full_api_surface_stub = proptools.StringPtr(apiScope.kind.DefaultJavaLibraryName() + "_full.from-text")
Jihoon Kang1c92c3e2023-03-23 17:44:51 +00002035 }
2036
Jihoon Kangd30ac8a2023-10-09 18:00:17 +00002037 // java_sdk_library modules that set sdk_version as none does not depend on other api
2038 // domains. Therefore, java_api_library created from such modules should not depend on
2039 // full_api_surface_stubs but create and compile stubs by the java_api_library module
2040 // itself.
2041 if module.SdkVersion(mctx).Kind == android.SdkNone {
2042 props.Full_api_surface_stub = nil
2043 }
2044
Jihoon Kang4ec24872023-10-05 17:26:09 +00002045 props.System_modules = module.deviceProperties.System_modules
Jihoon Kang063ec002023-06-28 01:16:23 +00002046 props.Enable_validation = proptools.BoolPtr(true)
Jihoon Kang4ec24872023-10-05 17:26:09 +00002047
Spandan Das2cc80ba2023-10-27 17:21:52 +00002048 mctx.CreateModule(ApiLibraryFactory, &props, module.sdkComponentPropertiesForChildLibrary())
Jihoon Kang1c92c3e2023-03-23 17:44:51 +00002049}
2050
Jihoon Kangfa4a90d2023-12-20 02:53:38 +00002051func (module *SdkLibrary) topLevelStubsLibraryProps(mctx android.DefaultableHookContext, apiScope *apiScope) libraryProperties {
2052 props := libraryProperties{}
2053
Jihoon Kang1147b312023-06-08 23:25:57 +00002054 props.Visibility = childModuleVisibility(module.sdkLibraryProperties.Stubs_library_visibility)
2055 sdkVersion := module.sdkVersionForStubsLibrary(mctx, apiScope)
2056 props.Sdk_version = proptools.StringPtr(sdkVersion)
2057
Jihoon Kang1147b312023-06-08 23:25:57 +00002058 props.System_modules = module.deviceProperties.System_modules
2059
Jihoon Kang1147b312023-06-08 23:25:57 +00002060 // The imports need to be compiled to dex if the java_sdk_library requests it.
2061 compileDex := module.dexProperties.Compile_dex
2062 if module.stubLibrariesCompiledForDex() {
2063 compileDex = proptools.BoolPtr(true)
2064 }
2065 props.Compile_dex = compileDex
2066
Jihoon Kangfa4a90d2023-12-20 02:53:38 +00002067 return props
2068}
2069
2070func (module *SdkLibrary) createTopLevelStubsLibrary(
2071 mctx android.DefaultableHookContext, apiScope *apiScope, contributesToApiSurface bool) {
2072
2073 props := module.topLevelStubsLibraryProps(mctx, apiScope)
2074 props.Name = proptools.StringPtr(module.stubsLibraryModuleName(apiScope))
2075
2076 // Add the stub compiling java_library/java_api_library as static lib based on build config
2077 staticLib := module.sourceStubsLibraryModuleName(apiScope)
2078 if mctx.Config().BuildFromTextStub() && contributesToApiSurface {
2079 staticLib = module.apiLibraryModuleName(apiScope)
2080 }
2081 props.Static_libs = append(props.Static_libs, staticLib)
2082
2083 mctx.CreateModule(LibraryFactory, &props, module.sdkComponentPropertiesForChildLibrary())
2084}
2085
2086func (module *SdkLibrary) createTopLevelExportableStubsLibrary(
2087 mctx android.DefaultableHookContext, apiScope *apiScope) {
2088
2089 props := module.topLevelStubsLibraryProps(mctx, apiScope)
2090 props.Name = proptools.StringPtr(module.exportableStubsLibraryModuleName(apiScope))
2091
2092 // Dist the class jar artifact for sdk builds.
2093 // "exportable" stubs are copied to dist for sdk builds instead of the "everything" stubs.
2094 if !Bool(module.sdkLibraryProperties.No_dist) {
2095 props.Dist.Targets = []string{"sdk", "win_sdk"}
2096 props.Dist.Dest = proptools.StringPtr(fmt.Sprintf("%v.jar", module.distStem()))
2097 props.Dist.Dir = proptools.StringPtr(module.apiDistPath(apiScope))
2098 props.Dist.Tag = proptools.StringPtr(".jar")
2099 }
2100
2101 staticLib := module.exportableSourceStubsLibraryModuleName(apiScope)
2102 props.Static_libs = append(props.Static_libs, staticLib)
2103
Jihoon Kang1147b312023-06-08 23:25:57 +00002104 mctx.CreateModule(LibraryFactory, &props, module.sdkComponentPropertiesForChildLibrary())
2105}
2106
Paul Duffin958806b2022-05-16 13:10:47 +00002107func (module *SdkLibrary) compareAgainstLatestApi(apiScope *apiScope) bool {
2108 return !(apiScope.unstable || module.sdkLibraryProperties.Unsafe_ignore_missing_latest_api)
2109}
2110
Paul Duffinea8f8082021-06-24 13:25:57 +01002111// Implements android.ApexModule
Jooyung Han5e9013b2020-03-10 06:23:13 +09002112func (module *SdkLibrary) DepIsInSameApex(mctx android.BaseModuleContext, dep android.Module) bool {
2113 depTag := mctx.OtherModuleDependencyTag(dep)
2114 if depTag == xmlPermissionsFileTag {
2115 return true
2116 }
2117 return module.Library.DepIsInSameApex(mctx, dep)
2118}
2119
Paul Duffinea8f8082021-06-24 13:25:57 +01002120// Implements android.ApexModule
2121func (module *SdkLibrary) UniqueApexVariations() bool {
2122 return module.uniqueApexVariations()
2123}
2124
Jihoon Kang80456fd2023-11-15 19:22:14 +00002125func (module *SdkLibrary) ContributeToApi() bool {
2126 return proptools.BoolDefault(module.sdkLibraryProperties.Contribute_to_android_api, false)
2127}
2128
Jiyong Parkc678ad32018-04-10 13:07:10 +09002129// Creates the xml file that publicizes the runtime library
Paul Duffinf0229202020-04-29 16:47:28 +01002130func (module *SdkLibrary) createXmlFile(mctx android.DefaultableHookContext) {
Spandan Das8c9ae7e2023-03-03 21:20:36 +00002131 moduleMinApiLevel := module.Library.MinSdkVersion(mctx)
Pedro Loureiroc3621422021-09-28 15:40:23 +00002132 var moduleMinApiLevelStr = moduleMinApiLevel.String()
2133 if moduleMinApiLevel == android.NoneApiLevel {
2134 moduleMinApiLevelStr = "current"
2135 }
Jiyong Parke3833882020-02-17 17:28:10 +09002136 props := struct {
Pedro Loureiroc3621422021-09-28 15:40:23 +00002137 Name *string
2138 Lib_name *string
2139 Apex_available []string
2140 On_bootclasspath_since *string
2141 On_bootclasspath_before *string
2142 Min_device_sdk *string
2143 Max_device_sdk *string
2144 Sdk_library_min_api_level *string
Jamie Garsidee570ace2023-11-27 12:07:36 +00002145 Uses_libs_dependencies []string
Jiyong Parke3833882020-02-17 17:28:10 +09002146 }{
Pedro Loureiroc3621422021-09-28 15:40:23 +00002147 Name: proptools.StringPtr(module.xmlPermissionsModuleName()),
2148 Lib_name: proptools.StringPtr(module.BaseModuleName()),
2149 Apex_available: module.ApexProperties.Apex_available,
2150 On_bootclasspath_since: module.commonSdkLibraryProperties.On_bootclasspath_since,
2151 On_bootclasspath_before: module.commonSdkLibraryProperties.On_bootclasspath_before,
2152 Min_device_sdk: module.commonSdkLibraryProperties.Min_device_sdk,
2153 Max_device_sdk: module.commonSdkLibraryProperties.Max_device_sdk,
2154 Sdk_library_min_api_level: &moduleMinApiLevelStr,
Jamie Garsidee570ace2023-11-27 12:07:36 +00002155 Uses_libs_dependencies: module.usesLibraryProperties.Uses_libs,
Jiyong Parkc678ad32018-04-10 13:07:10 +09002156 }
Jiyong Parke3833882020-02-17 17:28:10 +09002157
Jiyong Parke3833882020-02-17 17:28:10 +09002158 mctx.CreateModule(sdkLibraryXmlFactory, &props)
Jiyong Parkc678ad32018-04-10 13:07:10 +09002159}
2160
Jiyong Parkf1691d22021-03-29 20:11:58 +09002161func PrebuiltJars(ctx android.BaseModuleContext, baseName string, s android.SdkSpec) android.Paths {
Jiyong Park54105c42021-03-31 18:17:53 +09002162 var ver android.ApiLevel
Jiyong Parkf1691d22021-03-29 20:11:58 +09002163 var kind android.SdkKind
2164 if s.UsePrebuilt(ctx) {
Jiyong Park54105c42021-03-31 18:17:53 +09002165 ver = s.ApiLevel
Jiyong Parkf1691d22021-03-29 20:11:58 +09002166 kind = s.Kind
Jiyong Parkc678ad32018-04-10 13:07:10 +09002167 } else {
Jiyong Park6a927c42020-01-21 02:03:43 +09002168 // We don't have prebuilt SDK for the specific sdkVersion.
2169 // Instead of breaking the build, fallback to use "system_current"
Jiyong Park54105c42021-03-31 18:17:53 +09002170 ver = android.FutureApiLevel
Jiyong Parkf1691d22021-03-29 20:11:58 +09002171 kind = android.SdkSystem
Sundong Ahn054b19a2018-10-19 13:46:09 +09002172 }
Jiyong Park6a927c42020-01-21 02:03:43 +09002173
2174 dir := filepath.Join("prebuilts", "sdk", ver.String(), kind.String())
Paul Duffin50061512020-01-21 16:31:05 +00002175 jar := filepath.Join(dir, baseName+".jar")
Sundong Ahn054b19a2018-10-19 13:46:09 +09002176 jarPath := android.ExistentPathForSource(ctx, jar)
Sundong Ahnae418ac2019-02-28 15:01:28 +09002177 if !jarPath.Valid() {
Colin Cross07c88562020-01-07 09:34:44 -08002178 if ctx.Config().AllowMissingDependencies() {
2179 return android.Paths{android.PathForSource(ctx, jar)}
2180 } else {
Jiyong Parkf1691d22021-03-29 20:11:58 +09002181 ctx.PropertyErrorf("sdk_library", "invalid sdk version %q, %q does not exist", s.Raw, jar)
Colin Cross07c88562020-01-07 09:34:44 -08002182 }
Sundong Ahnae418ac2019-02-28 15:01:28 +09002183 return nil
2184 }
Sundong Ahn054b19a2018-10-19 13:46:09 +09002185 return android.Paths{jarPath.Path()}
2186}
2187
Colin Crossaede88c2020-08-11 12:17:01 -07002188// 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 +01002189//
2190// If either this or the other module are on the platform then this will return
2191// false.
Colin Cross56a83212020-09-15 18:30:11 -07002192func withinSameApexesAs(ctx android.BaseModuleContext, other android.Module) bool {
Colin Crossff694a82023-12-13 15:54:49 -08002193 apexInfo, _ := android.ModuleProvider(ctx, android.ApexInfoProvider)
Colin Cross313aa542023-12-13 13:47:44 -08002194 otherApexInfo, _ := android.OtherModuleProvider(ctx, other, android.ApexInfoProvider)
Jiyong Parkab50b072021-05-12 17:13:56 +09002195 return len(otherApexInfo.InApexVariants) > 0 && reflect.DeepEqual(apexInfo.InApexVariants, otherApexInfo.InApexVariants)
Paul Duffin9b879592020-05-26 13:21:35 +01002196}
2197
Jiyong Parkf1691d22021-03-29 20:11:58 +09002198func (module *SdkLibrary) sdkJars(ctx android.BaseModuleContext, sdkVersion android.SdkSpec, headerJars bool) android.Paths {
Jiyong Park932cdfe2020-05-28 00:19:53 +09002199 // If the client doesn't set sdk_version, but if this library prefers stubs over
2200 // the impl library, let's provide the widest API surface possible. To do so,
2201 // force override sdk_version to module_current so that the closest possible API
2202 // surface could be found in selectHeaderJarsForSdkVersion
Jiyong Parkf1691d22021-03-29 20:11:58 +09002203 if module.defaultsToStubs() && !sdkVersion.Specified() {
Jiyong Park92315372021-04-02 08:45:46 +09002204 sdkVersion = android.SdkSpecFrom(ctx, "module_current")
Jiyong Park932cdfe2020-05-28 00:19:53 +09002205 }
Paul Duffind1b3a922020-01-22 11:57:20 +00002206
Paul Duffindaaa3322020-05-26 18:13:57 +01002207 // Only provide access to the implementation library if it is actually built.
2208 if module.requiresRuntimeImplementationLibrary() {
2209 // Check any special cases for java_sdk_library.
2210 //
2211 // Only allow access to the implementation library in the following condition:
2212 // * No sdk_version specified on the referencing module.
Paul Duffin9b879592020-05-26 13:21:35 +01002213 // * The referencing module is in the same apex as this.
Jiyong Parkf1691d22021-03-29 20:11:58 +09002214 if sdkVersion.Kind == android.SdkPrivate || withinSameApexesAs(ctx, module) {
Paul Duffindaaa3322020-05-26 18:13:57 +01002215 if headerJars {
2216 return module.HeaderJars()
2217 } else {
2218 return module.ImplementationJars()
2219 }
Sundong Ahn054b19a2018-10-19 13:46:09 +09002220 }
Jiyong Parkc678ad32018-04-10 13:07:10 +09002221 }
Paul Duffinb05d4292020-05-20 12:19:10 +01002222
Paul Duffin23970f42020-05-20 14:20:02 +01002223 return module.selectHeaderJarsForSdkVersion(ctx, sdkVersion)
Jiyong Parkc678ad32018-04-10 13:07:10 +09002224}
2225
Sundong Ahn241cd372018-07-13 16:16:44 +09002226// to satisfy SdkLibraryDependency interface
Jiyong Parkf1691d22021-03-29 20:11:58 +09002227func (module *SdkLibrary) SdkHeaderJars(ctx android.BaseModuleContext, sdkVersion android.SdkSpec) android.Paths {
Paul Duffind1b3a922020-01-22 11:57:20 +00002228 return module.sdkJars(ctx, sdkVersion, true /*headerJars*/)
2229}
2230
2231// to satisfy SdkLibraryDependency interface
Jiyong Parkf1691d22021-03-29 20:11:58 +09002232func (module *SdkLibrary) SdkImplementationJars(ctx android.BaseModuleContext, sdkVersion android.SdkSpec) android.Paths {
Paul Duffind1b3a922020-01-22 11:57:20 +00002233 return module.sdkJars(ctx, sdkVersion, false /*headerJars*/)
Sundong Ahn241cd372018-07-13 16:16:44 +09002234}
2235
Colin Cross571cccf2019-02-04 11:22:08 -08002236var javaSdkLibrariesKey = android.NewOnceKey("javaSdkLibraries")
2237
Jiyong Park82484c02018-04-23 21:41:26 +09002238func javaSdkLibraries(config android.Config) *[]string {
Colin Cross571cccf2019-02-04 11:22:08 -08002239 return config.Once(javaSdkLibrariesKey, func() interface{} {
Jiyong Park82484c02018-04-23 21:41:26 +09002240 return &[]string{}
2241 }).(*[]string)
2242}
2243
Paul Duffin749f98f2019-12-30 17:23:46 +00002244func (module *SdkLibrary) getApiDir() string {
2245 return proptools.StringDefault(module.sdkLibraryProperties.Api_dir, "api")
2246}
2247
Jiyong Parkc678ad32018-04-10 13:07:10 +09002248// For a java_sdk_library module, create internal modules for stubs, docs,
2249// runtime libs and xml file. If requested, the stubs and docs are created twice
2250// once for public API level and once for system API level
Paul Duffinf0229202020-04-29 16:47:28 +01002251func (module *SdkLibrary) CreateInternalModules(mctx android.DefaultableHookContext) {
2252 // If the module has been disabled then don't create any child modules.
2253 if !module.Enabled() {
2254 return
2255 }
2256
Paul Duffina18abc22020-05-16 18:54:24 +01002257 if len(module.properties.Srcs) == 0 {
Inseob Kimc0907f12019-02-08 21:00:45 +09002258 mctx.PropertyErrorf("srcs", "java_sdk_library must specify srcs")
Jooyung Han58f26ab2019-12-18 15:34:32 +09002259 return
Inseob Kimc0907f12019-02-08 21:00:45 +09002260 }
2261
Paul Duffin37e0b772019-12-30 17:20:10 +00002262 // If this builds against standard libraries (i.e. is not part of the core libraries)
Paul Duffin4f5c1ef2020-11-19 14:53:43 +00002263 // then assume it provides both system and test apis.
Jiyong Parkf1691d22021-03-29 20:11:58 +09002264 sdkDep := decodeSdkDep(mctx, android.SdkContext(&module.Library))
Paul Duffin37e0b772019-12-30 17:20:10 +00002265 hasSystemAndTestApis := sdkDep.hasStandardLibs()
Paul Duffin3375e352020-04-28 10:44:03 +01002266 module.sdkLibraryProperties.Generate_system_and_test_apis = hasSystemAndTestApis
Paul Duffin4f5c1ef2020-11-19 14:53:43 +00002267
Jaewoong Jung18aefc12020-12-21 09:11:10 -08002268 missingCurrentApi := false
Inseob Kim8098faa2019-03-18 10:19:51 +09002269
Paul Duffin3375e352020-04-28 10:44:03 +01002270 generatedScopes := module.getGeneratedApiScopes(mctx)
Paul Duffind1b3a922020-01-22 11:57:20 +00002271
Paul Duffin749f98f2019-12-30 17:23:46 +00002272 apiDir := module.getApiDir()
Paul Duffin3375e352020-04-28 10:44:03 +01002273 for _, scope := range generatedScopes {
Inseob Kim8098faa2019-03-18 10:19:51 +09002274 for _, api := range []string{"current.txt", "removed.txt"} {
Paul Duffind1b3a922020-01-22 11:57:20 +00002275 path := path.Join(mctx.ModuleDir(), apiDir, scope.apiFilePrefix+api)
Inseob Kim8098faa2019-03-18 10:19:51 +09002276 p := android.ExistentPathForSource(mctx, path)
2277 if !p.Valid() {
Colin Cross18f840c2021-05-20 17:56:54 -07002278 if mctx.Config().AllowMissingDependencies() {
2279 mctx.AddMissingDependencies([]string{path})
2280 } else {
2281 mctx.ModuleErrorf("Current api file %#v doesn't exist", path)
2282 missingCurrentApi = true
2283 }
Inseob Kim8098faa2019-03-18 10:19:51 +09002284 }
2285 }
2286 }
2287
Jaewoong Jung18aefc12020-12-21 09:11:10 -08002288 if missingCurrentApi {
Inseob Kim8098faa2019-03-18 10:19:51 +09002289 script := "build/soong/scripts/gen-java-current-api-files.sh"
2290 p := android.ExistentPathForSource(mctx, script)
2291
2292 if !p.Valid() {
2293 panic(fmt.Sprintf("script file %s doesn't exist", script))
2294 }
2295
2296 mctx.ModuleErrorf("One or more current api files are missing. "+
2297 "You can update them by:\n"+
Paul Duffin37e0b772019-12-30 17:20:10 +00002298 "%s %q %s && m update-api",
Paul Duffind1b3a922020-01-22 11:57:20 +00002299 script, filepath.Join(mctx.ModuleDir(), apiDir),
Paul Duffin3375e352020-04-28 10:44:03 +01002300 strings.Join(generatedScopes.Strings(func(s *apiScope) string { return s.apiFilePrefix }), " "))
Inseob Kim8098faa2019-03-18 10:19:51 +09002301 return
2302 }
2303
Paul Duffin3375e352020-04-28 10:44:03 +01002304 for _, scope := range generatedScopes {
Paul Duffin15f34ef2020-07-20 18:04:44 +01002305 // Use the stubs source name for legacy reasons.
2306 module.createStubsSourcesAndApi(mctx, scope, module.stubsSourceModuleName(scope), scope.droidstubsArgs)
Paul Duffin0ff08bd2020-04-29 13:30:54 +01002307
Paul Duffind1b3a922020-01-22 11:57:20 +00002308 module.createStubsLibrary(mctx, scope)
Jihoon Kangfa4a90d2023-12-20 02:53:38 +00002309 module.createExportableStubsLibrary(mctx, scope)
Jihoon Kang1c92c3e2023-03-23 17:44:51 +00002310
Jihoon Kang0c705a42023-08-02 06:44:57 +00002311 alternativeFullApiSurfaceStubLib := ""
2312 if scope == apiScopePublic {
2313 alternativeFullApiSurfaceStubLib = module.alternativeFullApiSurfaceStubLib()
2314 }
2315 contributesToApiSurface := module.contributesToApiSurface(mctx.Config()) || alternativeFullApiSurfaceStubLib != ""
Jihoon Kang1147b312023-06-08 23:25:57 +00002316 if contributesToApiSurface {
Jihoon Kang0c705a42023-08-02 06:44:57 +00002317 module.createApiLibrary(mctx, scope, alternativeFullApiSurfaceStubLib)
Jihoon Kang1c92c3e2023-03-23 17:44:51 +00002318 }
Jihoon Kang1147b312023-06-08 23:25:57 +00002319
2320 module.createTopLevelStubsLibrary(mctx, scope, contributesToApiSurface)
Jihoon Kangfa4a90d2023-12-20 02:53:38 +00002321 module.createTopLevelExportableStubsLibrary(mctx, scope)
Inseob Kimc0907f12019-02-08 21:00:45 +09002322 }
2323
Paul Duffindfa131e2020-05-15 20:37:11 +01002324 if module.requiresRuntimeImplementationLibrary() {
Paul Duffin5df79302020-05-16 15:52:12 +01002325 // Create child module to create an implementation library.
2326 //
2327 // This temporarily creates a second implementation library that can be explicitly
2328 // referenced.
2329 //
2330 // TODO(b/156618935) - update comment once only one implementation library is created.
2331 module.createImplLibrary(mctx)
2332
Paul Duffindfa131e2020-05-15 20:37:11 +01002333 // Only create an XML permissions file that declares the library as being usable
2334 // as a shared library if required.
2335 if module.sharedLibrary() {
2336 module.createXmlFile(mctx)
2337 }
Paul Duffin43db9be2019-12-30 17:35:49 +00002338
2339 // record java_sdk_library modules so that they are exported to make
2340 javaSdkLibraries := javaSdkLibraries(mctx.Config())
2341 javaSdkLibrariesLock.Lock()
2342 defer javaSdkLibrariesLock.Unlock()
2343 *javaSdkLibraries = append(*javaSdkLibraries, module.BaseModuleName())
2344 }
Anton Hansson7f66efa2020-10-08 14:47:23 +01002345
Paul Duffin77590a82022-04-28 14:13:30 +00002346 // 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 +01002347 module.properties.Libs = append(module.properties.Libs, module.sdkLibraryProperties.Impl_only_libs...)
Paul Duffin77590a82022-04-28 14:13:30 +00002348 module.properties.Static_libs = append(module.properties.Static_libs, module.sdkLibraryProperties.Impl_only_static_libs...)
Inseob Kimc0907f12019-02-08 21:00:45 +09002349}
2350
2351func (module *SdkLibrary) InitSdkLibraryProperties() {
Colin Crossce6734e2020-06-15 16:09:53 -07002352 module.addHostAndDeviceProperties()
2353 module.AddProperties(&module.sdkLibraryProperties)
Sundong Ahn054b19a2018-10-19 13:46:09 +09002354
Paul Duffin71b33cc2021-06-23 11:39:47 +01002355 module.initSdkLibraryComponent(module)
Paul Duffin859fe962020-05-15 10:20:31 +01002356
Paul Duffina18abc22020-05-16 18:54:24 +01002357 module.properties.Installable = proptools.BoolPtr(true)
2358 module.deviceProperties.IsSDKLibrary = true
Inseob Kimc0907f12019-02-08 21:00:45 +09002359}
Sundong Ahn054b19a2018-10-19 13:46:09 +09002360
Paul Duffindfa131e2020-05-15 20:37:11 +01002361func (module *SdkLibrary) requiresRuntimeImplementationLibrary() bool {
2362 return !proptools.Bool(module.sdkLibraryProperties.Api_only)
2363}
2364
Jiyong Park932cdfe2020-05-28 00:19:53 +09002365func (module *SdkLibrary) defaultsToStubs() bool {
2366 return proptools.Bool(module.sdkLibraryProperties.Default_to_stubs)
2367}
2368
Paul Duffin1b1e8062020-05-08 13:44:43 +01002369// Defines how to name the individual component modules the sdk library creates.
2370type sdkLibraryComponentNamingScheme interface {
2371 stubsLibraryModuleName(scope *apiScope, baseName string) string
2372
2373 stubsSourceModuleName(scope *apiScope, baseName string) string
Jihoon Kang1c92c3e2023-03-23 17:44:51 +00002374
2375 apiLibraryModuleName(scope *apiScope, baseName string) string
Jihoon Kang1147b312023-06-08 23:25:57 +00002376
Jihoon Kangfa4a90d2023-12-20 02:53:38 +00002377 sourceStubsLibraryModuleName(scope *apiScope, baseName string) string
2378
2379 exportableStubsLibraryModuleName(scope *apiScope, baseName string) string
2380
2381 exportableSourceStubsLibraryModuleName(scope *apiScope, baseName string) string
Paul Duffin1b1e8062020-05-08 13:44:43 +01002382}
2383
2384type defaultNamingScheme struct {
2385}
2386
2387func (s *defaultNamingScheme) stubsLibraryModuleName(scope *apiScope, baseName string) string {
2388 return scope.stubsLibraryModuleName(baseName)
2389}
2390
2391func (s *defaultNamingScheme) stubsSourceModuleName(scope *apiScope, baseName string) string {
2392 return scope.stubsSourceModuleName(baseName)
2393}
2394
Jihoon Kang1c92c3e2023-03-23 17:44:51 +00002395func (s *defaultNamingScheme) apiLibraryModuleName(scope *apiScope, baseName string) string {
2396 return scope.apiLibraryModuleName(baseName)
2397}
2398
Jihoon Kangfa4a90d2023-12-20 02:53:38 +00002399func (s *defaultNamingScheme) sourceStubsLibraryModuleName(scope *apiScope, baseName string) string {
Jihoon Kang1147b312023-06-08 23:25:57 +00002400 return scope.sourceStubLibraryModuleName(baseName)
2401}
2402
Jihoon Kangfa4a90d2023-12-20 02:53:38 +00002403func (s *defaultNamingScheme) exportableStubsLibraryModuleName(scope *apiScope, baseName string) string {
2404 return scope.exportableStubsLibraryModuleName(baseName)
2405}
2406
2407func (s *defaultNamingScheme) exportableSourceStubsLibraryModuleName(scope *apiScope, baseName string) string {
2408 return scope.exportableSourceStubsLibraryModuleName(baseName)
2409}
2410
Paul Duffin1b1e8062020-05-08 13:44:43 +01002411var _ sdkLibraryComponentNamingScheme = (*defaultNamingScheme)(nil)
2412
Jihoon Kangfa4a90d2023-12-20 02:53:38 +00002413func hasStubsLibrarySuffix(name string, apiScope *apiScope) bool {
2414 return strings.HasSuffix(name, apiScope.stubsLibraryModuleNameSuffix()) ||
2415 strings.HasSuffix(name, apiScope.exportableStubsLibraryModuleNameSuffix())
2416}
2417
Jaewoong Jungbc15e3a2021-03-10 17:02:43 -08002418func moduleStubLinkType(name string) (stub bool, ret sdkLinkType) {
Jihoon Kang1147b312023-06-08 23:25:57 +00002419 name = strings.TrimSuffix(name, ".from-source")
2420
Anton Hansson2d0c1942020-05-25 12:20:51 +01002421 // This suffix-based approach is fragile and could potentially mis-trigger.
2422 // TODO(b/155164730): Clean this up when modules no longer reference sdk_lib stubs directly.
Jihoon Kangfa4a90d2023-12-20 02:53:38 +00002423 if hasStubsLibrarySuffix(name, apiScopePublic) {
Anton Hansson08f476b2021-04-07 15:32:19 +01002424 if name == "hwbinder.stubs" || name == "libcore_private.stubs" {
2425 // Due to a previous bug, these modules were not considered stubs, so we retain that.
2426 return false, javaPlatform
2427 }
Anton Hansson2d0c1942020-05-25 12:20:51 +01002428 return true, javaSdk
2429 }
Jihoon Kangfa4a90d2023-12-20 02:53:38 +00002430 if hasStubsLibrarySuffix(name, apiScopeSystem) {
Anton Hansson2d0c1942020-05-25 12:20:51 +01002431 return true, javaSystem
2432 }
Jihoon Kangfa4a90d2023-12-20 02:53:38 +00002433 if hasStubsLibrarySuffix(name, apiScopeModuleLib) {
Anton Hansson2d0c1942020-05-25 12:20:51 +01002434 return true, javaModule
2435 }
Jihoon Kangfa4a90d2023-12-20 02:53:38 +00002436 if hasStubsLibrarySuffix(name, apiScopeTest) {
Anton Hansson2d0c1942020-05-25 12:20:51 +01002437 return true, javaSystem
2438 }
Jihoon Kangfa4a90d2023-12-20 02:53:38 +00002439 if hasStubsLibrarySuffix(name, apiScopeSystemServer) {
Jihoon Kang1147b312023-06-08 23:25:57 +00002440 return true, javaSystemServer
2441 }
Anton Hansson2d0c1942020-05-25 12:20:51 +01002442 return false, javaPlatform
2443}
2444
Jaewoong Jung4f158ee2019-07-11 10:05:35 -07002445// java_sdk_library is a special Java library that provides optional platform APIs to apps.
2446// In practice, it can be viewed as a combination of several modules: 1) stubs library that clients
2447// are linked against to, 2) droiddoc module that internally generates API stubs source files,
2448// 3) the real runtime shared library that implements the APIs, and 4) XML file for adding
2449// the runtime lib to the classpath at runtime if requested via <uses-library>.
Inseob Kimc0907f12019-02-08 21:00:45 +09002450func SdkLibraryFactory() android.Module {
2451 module := &SdkLibrary{}
Paul Duffinc3091c82020-05-08 14:16:20 +01002452
2453 // Initialize information common between source and prebuilt.
Paul Duffin71b33cc2021-06-23 11:39:47 +01002454 module.initCommon(module)
Paul Duffinc3091c82020-05-08 14:16:20 +01002455
Inseob Kimc0907f12019-02-08 21:00:45 +09002456 module.InitSdkLibraryProperties()
Jooyung Han58f26ab2019-12-18 15:34:32 +09002457 android.InitApexModule(module)
Sundong Ahn054b19a2018-10-19 13:46:09 +09002458 InitJavaModule(module, android.HostAndDeviceSupported)
Paul Duffin3375e352020-04-28 10:44:03 +01002459
2460 // Initialize the map from scope to scope specific properties.
2461 scopeToProperties := make(map[*apiScope]*ApiScopeProperties)
2462 for _, scope := range allApiScopes {
2463 scopeToProperties[scope] = scope.scopeSpecificProperties(module)
2464 }
2465 module.scopeToProperties = scopeToProperties
2466
Paul Duffin4911a892020-04-29 23:35:13 +01002467 // Add the properties containing visibility rules so that they are checked.
Paul Duffin5df79302020-05-16 15:52:12 +01002468 android.AddVisibilityProperty(module, "impl_library_visibility", &module.sdkLibraryProperties.Impl_library_visibility)
Paul Duffin4911a892020-04-29 23:35:13 +01002469 android.AddVisibilityProperty(module, "stubs_library_visibility", &module.sdkLibraryProperties.Stubs_library_visibility)
2470 android.AddVisibilityProperty(module, "stubs_source_visibility", &module.sdkLibraryProperties.Stubs_source_visibility)
2471
Paul Duffin1b1e8062020-05-08 13:44:43 +01002472 module.SetDefaultableHook(func(ctx android.DefaultableHookContext) {
Paul Duffindfa131e2020-05-15 20:37:11 +01002473 // If no implementation is required then it cannot be used as a shared library
2474 // either.
2475 if !module.requiresRuntimeImplementationLibrary() {
2476 // If shared_library has been explicitly set to true then it is incompatible
2477 // with api_only: true.
2478 if proptools.Bool(module.commonSdkLibraryProperties.Shared_library) {
2479 ctx.PropertyErrorf("api_only/shared_library", "inconsistent settings, shared_library and api_only cannot both be true")
2480 }
2481 // Set shared_library: false.
2482 module.commonSdkLibraryProperties.Shared_library = proptools.BoolPtr(false)
2483 }
2484
Paul Duffin1b1e8062020-05-08 13:44:43 +01002485 if module.initCommonAfterDefaultsApplied(ctx) {
2486 module.CreateInternalModules(ctx)
2487 }
2488 })
Jiyong Parkc678ad32018-04-10 13:07:10 +09002489 return module
2490}
Colin Cross79c7c262019-04-17 11:11:46 -07002491
2492//
2493// SDK library prebuilts
2494//
2495
Paul Duffin56d44902020-01-31 13:36:25 +00002496// Properties associated with each api scope.
2497type sdkLibraryScopeProperties struct {
Colin Cross79c7c262019-04-17 11:11:46 -07002498 Jars []string `android:"path"`
2499
2500 Sdk_version *string
2501
Colin Cross79c7c262019-04-17 11:11:46 -07002502 // List of shared java libs that this module has dependencies to
2503 Libs []string
Paul Duffin3d1248c2020-04-09 00:10:17 +01002504
Paul Duffinc8782502020-04-29 20:45:27 +01002505 // The stubs source.
Paul Duffin3d1248c2020-04-09 00:10:17 +01002506 Stub_srcs []string `android:"path"`
Paul Duffin1fd005d2020-04-09 01:08:11 +01002507
2508 // The current.txt
Paul Duffin0f8faff2020-05-20 16:18:00 +01002509 Current_api *string `android:"path"`
Paul Duffin1fd005d2020-04-09 01:08:11 +01002510
2511 // The removed.txt
Paul Duffin0f8faff2020-05-20 16:18:00 +01002512 Removed_api *string `android:"path"`
Anton Hanssond78eb762021-09-21 15:25:12 +01002513
2514 // Annotation zip
2515 Annotations *string `android:"path"`
Colin Cross79c7c262019-04-17 11:11:46 -07002516}
2517
Paul Duffin56d44902020-01-31 13:36:25 +00002518type sdkLibraryImportProperties struct {
Paul Duffinfcfd7912020-01-31 17:54:30 +00002519 // List of shared java libs, common to all scopes, that this module has
2520 // dependencies to
2521 Libs []string
Paul Duffin1267d872021-04-16 17:21:36 +01002522
2523 // If set to true, compile dex files for the stubs. Defaults to false.
2524 Compile_dex *bool
Paul Duffin869de142021-07-15 14:14:41 +01002525
2526 // If not empty, classes are restricted to the specified packages and their sub-packages.
Paul Duffin869de142021-07-15 14:14:41 +01002527 Permitted_packages []string
Paul Duffin56d44902020-01-31 13:36:25 +00002528}
2529
Paul Duffineedc5d52020-06-12 17:46:39 +01002530type SdkLibraryImport struct {
Colin Cross79c7c262019-04-17 11:11:46 -07002531 android.ModuleBase
2532 android.DefaultableModuleBase
2533 prebuilt android.Prebuilt
Paul Duffindd46f712020-02-10 13:37:10 +00002534 android.ApexModuleBase
Colin Cross79c7c262019-04-17 11:11:46 -07002535
Paul Duffin37856732021-02-26 14:24:15 +00002536 hiddenAPI
Jiakai Zhang204356f2021-09-09 08:12:46 +00002537 dexpreopter
Paul Duffin37856732021-02-26 14:24:15 +00002538
Colin Cross79c7c262019-04-17 11:11:46 -07002539 properties sdkLibraryImportProperties
2540
Paul Duffin46a26a82020-04-07 19:27:04 +01002541 // Map from api scope to the scope specific property structure.
2542 scopeProperties map[*apiScope]*sdkLibraryScopeProperties
2543
Paul Duffin56d44902020-01-31 13:36:25 +00002544 commonToSdkLibraryAndImport
Paul Duffineedc5d52020-06-12 17:46:39 +01002545
2546 // The reference to the implementation library created by the source module.
2547 // Is nil if the source module does not exist.
2548 implLibraryModule *Library
2549
2550 // 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
2578 for _, apiScope := range allApiScopes {
2579 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
2596 for _, apiScope := range allApiScopes {
2597 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
Paul Duffineedc5d52020-06-12 17:46:39 +01002641func (module *SdkLibraryImport) createInternalModules(mctx android.DefaultableHookContext) {
Colin Cross79c7c262019-04-17 11:11:46 -07002642
Paul Duffin50061512020-01-21 16:31:05 +00002643 // If the build is configured to use prebuilts then force this to be preferred.
Jeongik Cha816a23a2020-07-08 01:09:23 +09002644 if mctx.Config().AlwaysUsePrebuiltSdks() {
Paul Duffin50061512020-01-21 16:31:05 +00002645 module.prebuilt.ForcePrefer()
2646 }
2647
Paul Duffin46a26a82020-04-07 19:27:04 +01002648 for apiScope, scopeProperties := range module.scopeProperties {
Paul Duffin56d44902020-01-31 13:36:25 +00002649 if len(scopeProperties.Jars) == 0 {
2650 continue
2651 }
2652
Paul Duffinbbb546b2020-04-09 00:07:11 +01002653 module.createJavaImportForStubs(mctx, apiScope, scopeProperties)
Paul Duffin3d1248c2020-04-09 00:10:17 +01002654
Paul Duffin0f8faff2020-05-20 16:18:00 +01002655 if len(scopeProperties.Stub_srcs) > 0 {
2656 module.createPrebuiltStubsSources(mctx, apiScope, scopeProperties)
2657 }
Jihoon Kang71c86832023-09-13 01:01:53 +00002658
2659 if scopeProperties.Current_api != nil {
2660 module.createPrebuiltApiContribution(mctx, apiScope, scopeProperties)
2661 }
Paul Duffin56d44902020-01-31 13:36:25 +00002662 }
Colin Cross79c7c262019-04-17 11:11:46 -07002663
2664 javaSdkLibraries := javaSdkLibraries(mctx.Config())
2665 javaSdkLibrariesLock.Lock()
2666 defer javaSdkLibrariesLock.Unlock()
2667 *javaSdkLibraries = append(*javaSdkLibraries, module.BaseModuleName())
2668}
2669
Paul Duffineedc5d52020-06-12 17:46:39 +01002670func (module *SdkLibraryImport) createJavaImportForStubs(mctx android.DefaultableHookContext, apiScope *apiScope, scopeProperties *sdkLibraryScopeProperties) {
Paul Duffinbbb546b2020-04-09 00:07:11 +01002671 // Creates a java import for the jar with ".stubs" suffix
2672 props := struct {
Paul Duffin1dbe3ca2020-05-16 09:57:59 +01002673 Name *string
2674 Sdk_version *string
2675 Libs []string
2676 Jars []string
Paul Duffin1267d872021-04-16 17:21:36 +01002677 Compile_dex *bool
Paul Duffinbf4de042022-09-27 12:41:52 +01002678
2679 android.UserSuppliedPrebuiltProperties
Paul Duffinbbb546b2020-04-09 00:07:11 +01002680 }{}
Paul Duffinc3091c82020-05-08 14:16:20 +01002681 props.Name = proptools.StringPtr(module.stubsLibraryModuleName(apiScope))
Paul Duffinbbb546b2020-04-09 00:07:11 +01002682 props.Sdk_version = scopeProperties.Sdk_version
2683 // Prepend any of the libs from the legacy public properties to the libs for each of the
2684 // scopes to avoid having to duplicate them in each scope.
2685 props.Libs = append(module.properties.Libs, scopeProperties.Libs...)
2686 props.Jars = scopeProperties.Jars
Paul Duffin1dbe3ca2020-05-16 09:57:59 +01002687
Paul Duffin38b57852020-05-13 16:08:09 +01002688 // The imports are preferred if the java_sdk_library_import is preferred.
Paul Duffinbf4de042022-09-27 12:41:52 +01002689 props.CopyUserSuppliedPropertiesFromPrebuilt(&module.prebuilt)
Paul Duffin859fe962020-05-15 10:20:31 +01002690
Paul Duffin1267d872021-04-16 17:21:36 +01002691 // The imports need to be compiled to dex if the java_sdk_library_import requests it.
Paul Duffinf4600f62021-05-13 22:34:45 +01002692 compileDex := module.properties.Compile_dex
2693 if module.stubLibrariesCompiledForDex() {
2694 compileDex = proptools.BoolPtr(true)
2695 }
2696 props.Compile_dex = compileDex
Paul Duffin1267d872021-04-16 17:21:36 +01002697
Paul Duffin859fe962020-05-15 10:20:31 +01002698 mctx.CreateModule(ImportFactory, &props, module.sdkComponentPropertiesForChildLibrary())
Paul Duffinbbb546b2020-04-09 00:07:11 +01002699}
2700
Paul Duffineedc5d52020-06-12 17:46:39 +01002701func (module *SdkLibraryImport) createPrebuiltStubsSources(mctx android.DefaultableHookContext, apiScope *apiScope, scopeProperties *sdkLibraryScopeProperties) {
Paul Duffin3d1248c2020-04-09 00:10:17 +01002702 props := struct {
Paul Duffinbf4de042022-09-27 12:41:52 +01002703 Name *string
2704 Srcs []string
2705
2706 android.UserSuppliedPrebuiltProperties
Paul Duffin3d1248c2020-04-09 00:10:17 +01002707 }{}
Paul Duffinc3091c82020-05-08 14:16:20 +01002708 props.Name = proptools.StringPtr(module.stubsSourceModuleName(apiScope))
Paul Duffin3d1248c2020-04-09 00:10:17 +01002709 props.Srcs = scopeProperties.Stub_srcs
Paul Duffin38b57852020-05-13 16:08:09 +01002710
2711 // The stubs source is preferred if the java_sdk_library_import is preferred.
Paul Duffinbf4de042022-09-27 12:41:52 +01002712 props.CopyUserSuppliedPropertiesFromPrebuilt(&module.prebuilt)
2713
Spandan Das2cc80ba2023-10-27 17:21:52 +00002714 mctx.CreateModule(PrebuiltStubsSourcesFactory, &props, module.sdkComponentPropertiesForChildLibrary())
Paul Duffin3d1248c2020-04-09 00:10:17 +01002715}
2716
Jihoon Kang71c86832023-09-13 01:01:53 +00002717func (module *SdkLibraryImport) createPrebuiltApiContribution(mctx android.DefaultableHookContext, apiScope *apiScope, scopeProperties *sdkLibraryScopeProperties) {
2718 api_file := scopeProperties.Current_api
2719 api_surface := &apiScope.name
2720
2721 props := struct {
2722 Name *string
2723 Api_surface *string
2724 Api_file *string
2725 Visibility []string
2726 }{}
2727
2728 props.Name = proptools.StringPtr(module.stubsSourceModuleName(apiScope) + ".api.contribution")
2729 props.Api_surface = api_surface
2730 props.Api_file = api_file
2731 props.Visibility = []string{"//visibility:override", "//visibility:public"}
2732
Spandan Das2cc80ba2023-10-27 17:21:52 +00002733 mctx.CreateModule(ApiContributionImportFactory, &props, module.sdkComponentPropertiesForChildLibrary())
Jihoon Kang71c86832023-09-13 01:01:53 +00002734}
2735
Paul Duffin44f1d842020-06-26 20:17:02 +01002736// Add the dependencies on the child module in the component deps mutator so that it
2737// creates references to the prebuilt and not the source modules.
2738func (module *SdkLibraryImport) ComponentDepsMutator(ctx android.BottomUpMutatorContext) {
Paul Duffin46a26a82020-04-07 19:27:04 +01002739 for apiScope, scopeProperties := range module.scopeProperties {
Paul Duffin56d44902020-01-31 13:36:25 +00002740 if len(scopeProperties.Jars) == 0 {
2741 continue
2742 }
2743
2744 // Add dependencies to the prebuilt stubs library
Jihoon Kangb7431552024-01-22 19:40:08 +00002745 ctx.AddVariationDependencies(nil, apiScope.prebuiltStubsTag, android.PrebuiltNameFromSource(module.stubsLibraryModuleName(apiScope)))
Paul Duffin0f8faff2020-05-20 16:18:00 +01002746
2747 if len(scopeProperties.Stub_srcs) > 0 {
2748 // Add dependencies to the prebuilt stubs source library
Paul Duffin864116c2021-04-02 10:24:13 +01002749 ctx.AddVariationDependencies(nil, apiScope.stubsSourceTag, android.PrebuiltNameFromSource(module.stubsSourceModuleName(apiScope)))
Paul Duffin0f8faff2020-05-20 16:18:00 +01002750 }
Paul Duffin56d44902020-01-31 13:36:25 +00002751 }
Paul Duffin44f1d842020-06-26 20:17:02 +01002752}
2753
2754// Add other dependencies as normal.
2755func (module *SdkLibraryImport) DepsMutator(ctx android.BottomUpMutatorContext) {
Paul Duffineedc5d52020-06-12 17:46:39 +01002756
2757 implName := module.implLibraryModuleName()
2758 if ctx.OtherModuleExists(implName) {
2759 ctx.AddVariationDependencies(nil, implLibraryTag, implName)
2760
2761 xmlPermissionsModuleName := module.xmlPermissionsModuleName()
2762 if module.sharedLibrary() && ctx.OtherModuleExists(xmlPermissionsModuleName) {
2763 // Add dependency to the rule for generating the xml permissions file
2764 ctx.AddDependency(module, xmlPermissionsFileTag, xmlPermissionsModuleName)
2765 }
2766 }
Colin Cross79c7c262019-04-17 11:11:46 -07002767}
2768
Jiyong Park45bf82e2020-12-15 22:29:02 +09002769var _ android.ApexModule = (*SdkLibraryImport)(nil)
2770
2771// Implements android.ApexModule
Paul Duffineedc5d52020-06-12 17:46:39 +01002772func (module *SdkLibraryImport) DepIsInSameApex(mctx android.BaseModuleContext, dep android.Module) bool {
2773 depTag := mctx.OtherModuleDependencyTag(dep)
2774 if depTag == xmlPermissionsFileTag {
2775 return true
2776 }
2777
2778 // None of the other dependencies of the java_sdk_library_import are in the same apex
2779 // as the one that references this module.
2780 return false
2781}
2782
Jiyong Park45bf82e2020-12-15 22:29:02 +09002783// Implements android.ApexModule
Dan Albertc8060532020-07-22 22:32:17 -07002784func (module *SdkLibraryImport) ShouldSupportSdkVersion(ctx android.BaseModuleContext,
2785 sdkVersion android.ApiLevel) error {
Jooyung Han749dc692020-04-15 11:03:39 +09002786 // we don't check prebuilt modules for sdk_version
2787 return nil
2788}
2789
Paul Duffinea8f8082021-06-24 13:25:57 +01002790// Implements android.ApexModule
2791func (module *SdkLibraryImport) UniqueApexVariations() bool {
2792 return module.uniqueApexVariations()
2793}
2794
Paul Duffin09817d62022-04-28 17:45:11 +01002795// MinSdkVersion - Implements hiddenAPIModule
Spandan Das8c9ae7e2023-03-03 21:20:36 +00002796func (module *SdkLibraryImport) MinSdkVersion(ctx android.EarlyModuleContext) android.ApiLevel {
2797 return android.NoneApiLevel
Paul Duffin09817d62022-04-28 17:45:11 +01002798}
2799
2800var _ hiddenAPIModule = (*SdkLibraryImport)(nil)
2801
Paul Duffineedc5d52020-06-12 17:46:39 +01002802func (module *SdkLibraryImport) OutputFiles(tag string) (android.Paths, error) {
Paul Duffin1e940d52022-04-29 14:21:25 +01002803 paths, err := module.commonOutputFiles(tag)
2804 if paths != nil || err != nil {
2805 return paths, err
2806 }
2807 if module.implLibraryModule != nil {
2808 return module.implLibraryModule.OutputFiles(tag)
2809 } else {
2810 return nil, nil
2811 }
Paul Duffin46dc45a2020-05-14 15:39:10 +01002812}
2813
Paul Duffineedc5d52020-06-12 17:46:39 +01002814func (module *SdkLibraryImport) GenerateAndroidBuildActions(ctx android.ModuleContext) {
Paul Duffina2ae7e02020-09-11 11:55:00 +01002815 module.generateCommonBuildActions(ctx)
2816
Jeongik Chad5fe8782021-07-08 01:13:11 +09002817 // Assume that source module(sdk_library) is installed in /<sdk_library partition>/framework
2818 module.installFile = android.PathForModuleInstall(ctx, "framework", module.Stem()+".jar")
2819
Paul Duffin0f8faff2020-05-20 16:18:00 +01002820 // Record the paths to the prebuilt stubs library and stubs source.
Colin Cross79c7c262019-04-17 11:11:46 -07002821 ctx.VisitDirectDeps(func(to android.Module) {
2822 tag := ctx.OtherModuleDependencyTag(to)
2823
Paul Duffin0f8faff2020-05-20 16:18:00 +01002824 // Extract information from any of the scope specific dependencies.
2825 if scopeTag, ok := tag.(scopeDependencyTag); ok {
2826 apiScope := scopeTag.apiScope
2827 scopePaths := module.getScopePathsCreateIfNeeded(apiScope)
2828
2829 // Extract information from the dependency. The exact information extracted
2830 // is determined by the nature of the dependency which is determined by the tag.
2831 scopeTag.extractDepInfo(ctx, to, scopePaths)
Paul Duffineedc5d52020-06-12 17:46:39 +01002832 } else if tag == implLibraryTag {
2833 if implLibrary, ok := to.(*Library); ok {
2834 module.implLibraryModule = implLibrary
2835 } else {
2836 ctx.ModuleErrorf("implementation library must be of type *java.Library but was %T", to)
2837 }
2838 } else if tag == xmlPermissionsFileTag {
2839 if xmlPermissionsFileModule, ok := to.(*sdkLibraryXml); ok {
2840 module.xmlPermissionsFileModule = xmlPermissionsFileModule
2841 } else {
2842 ctx.ModuleErrorf("xml permissions file module must be of type *sdkLibraryXml but was %T", to)
2843 }
Colin Cross79c7c262019-04-17 11:11:46 -07002844 }
2845 })
Paul Duffin0f8faff2020-05-20 16:18:00 +01002846
2847 // Populate the scope paths with information from the properties.
2848 for apiScope, scopeProperties := range module.scopeProperties {
2849 if len(scopeProperties.Jars) == 0 {
2850 continue
2851 }
2852
2853 paths := module.getScopePathsCreateIfNeeded(apiScope)
Anton Hanssond78eb762021-09-21 15:25:12 +01002854 paths.annotationsZip = android.OptionalPathForModuleSrc(ctx, scopeProperties.Annotations)
Paul Duffin0f8faff2020-05-20 16:18:00 +01002855 paths.currentApiFilePath = android.OptionalPathForModuleSrc(ctx, scopeProperties.Current_api)
2856 paths.removedApiFilePath = android.OptionalPathForModuleSrc(ctx, scopeProperties.Removed_api)
2857 }
Paul Duffin39853512021-02-26 11:09:39 +00002858
2859 if ctx.Device() {
2860 // If this is a variant created for a prebuilt_apex then use the dex implementation jar
2861 // obtained from the associated deapexer module.
Colin Crossff694a82023-12-13 15:54:49 -08002862 ai, _ := android.ModuleProvider(ctx, android.ApexInfoProvider)
Paul Duffin39853512021-02-26 11:09:39 +00002863 if ai.ForPrebuiltApex {
Paul Duffin39853512021-02-26 11:09:39 +00002864 // Get the path of the dex implementation jar from the `deapexer` module.
Spandan Dasfae468e2023-12-12 23:23:53 +00002865 di, err := android.FindDeapexerProviderForModule(ctx)
2866 if err != nil {
2867 // An error was found, possibly due to multiple apexes in the tree that export this library
2868 // Defer the error till a client tries to call DexJarBuildPath
2869 module.dexJarFileErr = err
Spandan Das3a392012024-01-17 18:26:27 +00002870 module.initHiddenAPIError(err)
Spandan Dasfae468e2023-12-12 23:23:53 +00002871 return
Martin Stjernholm44825602021-09-17 01:44:12 +01002872 }
Spandan Das5be63332023-12-13 00:06:32 +00002873 dexJarFileApexRootRelative := ApexRootRelativePathToJavaLib(module.BaseModuleName())
Jiakai Zhang81e46812023-02-08 21:56:07 +08002874 if dexOutputPath := di.PrebuiltExportPath(dexJarFileApexRootRelative); dexOutputPath != nil {
Martin Stjernholm8be1e6d2021-09-15 03:34:04 +01002875 dexJarFile := makeDexJarPathFromPath(dexOutputPath)
2876 module.dexJarFile = dexJarFile
Jiakai Zhang204356f2021-09-09 08:12:46 +00002877 installPath := android.PathForModuleInPartitionInstall(
Jiakai Zhang81e46812023-02-08 21:56:07 +08002878 ctx, "apex", ai.ApexVariationName, dexJarFileApexRootRelative)
Jiakai Zhang204356f2021-09-09 08:12:46 +00002879 module.installFile = installPath
Martin Stjernholm8be1e6d2021-09-15 03:34:04 +01002880 module.initHiddenAPI(ctx, dexJarFile, module.findScopePaths(apiScopePublic).stubsImplPath[0], nil)
Jiakai Zhang204356f2021-09-09 08:12:46 +00002881
Jiakai Zhang204356f2021-09-09 08:12:46 +00002882 module.dexpreopter.installPath = module.dexpreopter.getInstallPath(ctx, installPath)
2883 module.dexpreopter.isSDKLibrary = true
2884 module.dexpreopter.uncompressedDex = shouldUncompressDex(ctx, &module.dexpreopter)
Jiakai Zhang81e46812023-02-08 21:56:07 +08002885
2886 if profilePath := di.PrebuiltExportPath(dexJarFileApexRootRelative + ".prof"); profilePath != nil {
2887 module.dexpreopter.inputProfilePathOnHost = profilePath
2888 }
2889
2890 // Dexpreopting.
Jiakai Zhang204356f2021-09-09 08:12:46 +00002891 module.dexpreopt(ctx, dexOutputPath)
Paul Duffin39853512021-02-26 11:09:39 +00002892 } else {
2893 // This should never happen as a variant for a prebuilt_apex is only created if the
2894 // prebuilt_apex has been configured to export the java library dex file.
Martin Stjernholm44825602021-09-17 01:44:12 +01002895 ctx.ModuleErrorf("internal error: no dex implementation jar available from prebuilt APEX %s", di.ApexModuleName())
Paul Duffin39853512021-02-26 11:09:39 +00002896 }
2897 }
2898 }
Colin Cross79c7c262019-04-17 11:11:46 -07002899}
2900
Jiyong Parkf1691d22021-03-29 20:11:58 +09002901func (module *SdkLibraryImport) sdkJars(ctx android.BaseModuleContext, sdkVersion android.SdkSpec, headerJars bool) android.Paths {
Paul Duffineedc5d52020-06-12 17:46:39 +01002902
2903 // For consistency with SdkLibrary make the implementation jar available to libraries that
2904 // are within the same APEX.
2905 implLibraryModule := module.implLibraryModule
Colin Cross56a83212020-09-15 18:30:11 -07002906 if implLibraryModule != nil && withinSameApexesAs(ctx, module) {
Paul Duffineedc5d52020-06-12 17:46:39 +01002907 if headerJars {
2908 return implLibraryModule.HeaderJars()
2909 } else {
2910 return implLibraryModule.ImplementationJars()
2911 }
2912 }
2913
Paul Duffin23970f42020-05-20 14:20:02 +01002914 return module.selectHeaderJarsForSdkVersion(ctx, sdkVersion)
Paul Duffin56d44902020-01-31 13:36:25 +00002915}
2916
Colin Cross79c7c262019-04-17 11:11:46 -07002917// to satisfy SdkLibraryDependency interface
Jiyong Parkf1691d22021-03-29 20:11:58 +09002918func (module *SdkLibraryImport) SdkHeaderJars(ctx android.BaseModuleContext, sdkVersion android.SdkSpec) android.Paths {
Colin Cross79c7c262019-04-17 11:11:46 -07002919 // This module is just a wrapper for the prebuilt stubs.
Paul Duffineedc5d52020-06-12 17:46:39 +01002920 return module.sdkJars(ctx, sdkVersion, true)
Colin Cross79c7c262019-04-17 11:11:46 -07002921}
2922
2923// to satisfy SdkLibraryDependency interface
Jiyong Parkf1691d22021-03-29 20:11:58 +09002924func (module *SdkLibraryImport) SdkImplementationJars(ctx android.BaseModuleContext, sdkVersion android.SdkSpec) android.Paths {
Colin Cross79c7c262019-04-17 11:11:46 -07002925 // This module is just a wrapper for the stubs.
Paul Duffineedc5d52020-06-12 17:46:39 +01002926 return module.sdkJars(ctx, sdkVersion, false)
2927}
2928
Ulya Trafimovichdbf31662020-12-17 12:07:54 +00002929// to satisfy UsesLibraryDependency interface
Spandan Das59a4a2b2024-01-09 21:35:56 +00002930func (module *SdkLibraryImport) DexJarBuildPath(ctx android.ModuleErrorfContext) OptionalDexJarPath {
Paul Duffin39853512021-02-26 11:09:39 +00002931 // The dex implementation jar extracted from the .apex file should be used in preference to the
2932 // source.
Spandan Dasfae468e2023-12-12 23:23:53 +00002933 if module.dexJarFileErr != nil {
Spandan Das59a4a2b2024-01-09 21:35:56 +00002934 ctx.ModuleErrorf(module.dexJarFileErr.Error())
Spandan Dasfae468e2023-12-12 23:23:53 +00002935 }
Martin Stjernholm8be1e6d2021-09-15 03:34:04 +01002936 if module.dexJarFile.IsSet() {
Paul Duffin39853512021-02-26 11:09:39 +00002937 return module.dexJarFile
2938 }
Paul Duffineedc5d52020-06-12 17:46:39 +01002939 if module.implLibraryModule == nil {
Martin Stjernholm8be1e6d2021-09-15 03:34:04 +01002940 return makeUnsetDexJarPath()
Paul Duffineedc5d52020-06-12 17:46:39 +01002941 } else {
Spandan Das59a4a2b2024-01-09 21:35:56 +00002942 return module.implLibraryModule.DexJarBuildPath(ctx)
Paul Duffineedc5d52020-06-12 17:46:39 +01002943 }
2944}
2945
Ulya Trafimovichdbf31662020-12-17 12:07:54 +00002946// to satisfy UsesLibraryDependency interface
Ulya Trafimovich31e444e2020-08-14 17:32:16 +01002947func (module *SdkLibraryImport) DexJarInstallPath() android.Path {
Jeongik Chad5fe8782021-07-08 01:13:11 +09002948 return module.installFile
Ulya Trafimovich31e444e2020-08-14 17:32:16 +01002949}
2950
Ulya Trafimovichdbf31662020-12-17 12:07:54 +00002951// to satisfy UsesLibraryDependency interface
2952func (module *SdkLibraryImport) ClassLoaderContexts() dexpreopt.ClassLoaderContextMap {
2953 return nil
2954}
2955
Paul Duffineedc5d52020-06-12 17:46:39 +01002956// to satisfy apex.javaDependency interface
2957func (module *SdkLibraryImport) JacocoReportClassesFile() android.Path {
2958 if module.implLibraryModule == nil {
2959 return nil
2960 } else {
2961 return module.implLibraryModule.JacocoReportClassesFile()
2962 }
2963}
2964
2965// to satisfy apex.javaDependency interface
Colin Cross08dca382020-07-21 20:31:17 -07002966func (module *SdkLibraryImport) LintDepSets() LintDepSets {
2967 if module.implLibraryModule == nil {
2968 return LintDepSets{}
2969 } else {
2970 return module.implLibraryModule.LintDepSets()
2971 }
2972}
2973
Spandan Das17854f52022-01-14 21:19:14 +00002974func (module *SdkLibraryImport) GetStrictUpdatabilityLinting() bool {
Jaewoong Jung476b9d62021-05-10 15:30:00 -07002975 if module.implLibraryModule == nil {
2976 return false
2977 } else {
Spandan Das17854f52022-01-14 21:19:14 +00002978 return module.implLibraryModule.GetStrictUpdatabilityLinting()
Jaewoong Jung476b9d62021-05-10 15:30:00 -07002979 }
2980}
2981
Spandan Das17854f52022-01-14 21:19:14 +00002982func (module *SdkLibraryImport) SetStrictUpdatabilityLinting(strictLinting bool) {
Jaewoong Jung476b9d62021-05-10 15:30:00 -07002983 if module.implLibraryModule != nil {
Spandan Das17854f52022-01-14 21:19:14 +00002984 module.implLibraryModule.SetStrictUpdatabilityLinting(strictLinting)
Jaewoong Jung476b9d62021-05-10 15:30:00 -07002985 }
2986}
2987
Colin Cross08dca382020-07-21 20:31:17 -07002988// to satisfy apex.javaDependency interface
Paul Duffineedc5d52020-06-12 17:46:39 +01002989func (module *SdkLibraryImport) Stem() string {
2990 return module.BaseModuleName()
Colin Cross79c7c262019-04-17 11:11:46 -07002991}
Jiyong Parke3833882020-02-17 17:28:10 +09002992
Paul Duffin44b481b2020-06-17 16:59:43 +01002993var _ ApexDependency = (*SdkLibraryImport)(nil)
2994
2995// to satisfy java.ApexDependency interface
2996func (module *SdkLibraryImport) HeaderJars() android.Paths {
2997 if module.implLibraryModule == nil {
2998 return nil
2999 } else {
3000 return module.implLibraryModule.HeaderJars()
3001 }
3002}
3003
3004// to satisfy java.ApexDependency interface
3005func (module *SdkLibraryImport) ImplementationAndResourcesJars() android.Paths {
3006 if module.implLibraryModule == nil {
3007 return nil
3008 } else {
3009 return module.implLibraryModule.ImplementationAndResourcesJars()
3010 }
3011}
3012
Jiakai Zhang204356f2021-09-09 08:12:46 +00003013// to satisfy java.DexpreopterInterface interface
3014func (module *SdkLibraryImport) IsInstallable() bool {
3015 return true
3016}
3017
Paul Duffinfef55002021-06-17 14:56:05 +01003018var _ android.RequiredFilesFromPrebuiltApex = (*SdkLibraryImport)(nil)
3019
Paul Duffinb4bbf2c2021-06-17 15:59:07 +01003020func (module *SdkLibraryImport) RequiredFilesFromPrebuiltApex(ctx android.BaseModuleContext) []string {
Paul Duffinfef55002021-06-17 14:56:05 +01003021 name := module.BaseModuleName()
Jiakai Zhang81e46812023-02-08 21:56:07 +08003022 return requiredFilesFromPrebuiltApexForImport(name, &module.dexpreopter)
Paul Duffinfef55002021-06-17 14:56:05 +01003023}
3024
Jiyong Parke3833882020-02-17 17:28:10 +09003025// java_sdk_library_xml
Jiyong Parke3833882020-02-17 17:28:10 +09003026type sdkLibraryXml struct {
3027 android.ModuleBase
3028 android.DefaultableModuleBase
3029 android.ApexModuleBase
3030
3031 properties sdkLibraryXmlProperties
3032
3033 outputFilePath android.OutputPath
3034 installDirPath android.InstallPath
Colin Cross56a83212020-09-15 18:30:11 -07003035
3036 hideApexVariantFromMake bool
Jiyong Parke3833882020-02-17 17:28:10 +09003037}
3038
3039type sdkLibraryXmlProperties struct {
3040 // canonical name of the lib
3041 Lib_name *string
Pedro Loureiro9956e5e2021-09-07 17:21:59 +00003042
3043 // Signals that this shared library is part of the bootclasspath starting
3044 // on the version indicated in this attribute.
3045 //
3046 // This will make platforms at this level and above to ignore
3047 // <uses-library> tags with this library name because the library is already
3048 // available
3049 On_bootclasspath_since *string
3050
3051 // Signals that this shared library was part of the bootclasspath before
3052 // (but not including) the version indicated in this attribute.
3053 //
3054 // The system will automatically add a <uses-library> tag with this library to
3055 // apps that target any SDK less than the version indicated in this attribute.
3056 On_bootclasspath_before *string
3057
3058 // Indicates that PackageManager should ignore this shared library if the
3059 // platform is below the version indicated in this attribute.
3060 //
3061 // This means that the device won't recognise this library as installed.
3062 Min_device_sdk *string
3063
3064 // Indicates that PackageManager should ignore this shared library if the
3065 // platform is above the version indicated in this attribute.
3066 //
3067 // This means that the device won't recognise this library as installed.
3068 Max_device_sdk *string
Pedro Loureiroc3621422021-09-28 15:40:23 +00003069
3070 // The SdkLibrary's min api level as a string
3071 //
3072 // This value comes from the ApiLevel of the MinSdkVersion property.
3073 Sdk_library_min_api_level *string
Jamie Garsidee570ace2023-11-27 12:07:36 +00003074
3075 // Uses-libs dependencies that the shared library requires to work correctly.
3076 //
3077 // This will add dependency="foo:bar" to the <library> section.
3078 Uses_libs_dependencies []string
Jiyong Parke3833882020-02-17 17:28:10 +09003079}
3080
3081// java_sdk_library_xml builds the permission xml file for a java_sdk_library.
3082// Not to be used directly by users. java_sdk_library internally uses this.
3083func sdkLibraryXmlFactory() android.Module {
3084 module := &sdkLibraryXml{}
3085
3086 module.AddProperties(&module.properties)
3087
3088 android.InitApexModule(module)
3089 android.InitAndroidArchModule(module, android.DeviceSupported, android.MultilibCommon)
3090
3091 return module
3092}
3093
Colin Crossaede88c2020-08-11 12:17:01 -07003094func (module *sdkLibraryXml) UniqueApexVariations() bool {
3095 // sdkLibraryXml needs a unique variation per APEX because the generated XML file contains the path to the
3096 // mounted APEX, which contains the name of the APEX.
3097 return true
3098}
3099
Jiyong Parke3833882020-02-17 17:28:10 +09003100// from android.PrebuiltEtcModule
Jooyung Han0703fd82020-08-26 22:11:53 +09003101func (module *sdkLibraryXml) BaseDir() string {
3102 return "etc"
3103}
3104
3105// from android.PrebuiltEtcModule
Jiyong Parke3833882020-02-17 17:28:10 +09003106func (module *sdkLibraryXml) SubDir() string {
3107 return "permissions"
3108}
3109
3110// from android.PrebuiltEtcModule
3111func (module *sdkLibraryXml) OutputFile() android.OutputPath {
3112 return module.outputFilePath
3113}
3114
3115// from android.ApexModule
3116func (module *sdkLibraryXml) AvailableFor(what string) bool {
3117 return true
3118}
3119
3120func (module *sdkLibraryXml) DepsMutator(ctx android.BottomUpMutatorContext) {
3121 // do nothing
3122}
3123
Jiyong Park45bf82e2020-12-15 22:29:02 +09003124var _ android.ApexModule = (*sdkLibraryXml)(nil)
3125
3126// Implements android.ApexModule
Dan Albertc8060532020-07-22 22:32:17 -07003127func (module *sdkLibraryXml) ShouldSupportSdkVersion(ctx android.BaseModuleContext,
3128 sdkVersion android.ApiLevel) error {
Jooyung Han749dc692020-04-15 11:03:39 +09003129 // sdkLibraryXml doesn't need to be checked separately because java_sdk_library is checked
3130 return nil
3131}
3132
Jiyong Parke3833882020-02-17 17:28:10 +09003133// File path to the runtime implementation library
Colin Cross56a83212020-09-15 18:30:11 -07003134func (module *sdkLibraryXml) implPath(ctx android.ModuleContext) string {
Jiyong Parke3833882020-02-17 17:28:10 +09003135 implName := proptools.String(module.properties.Lib_name)
Colin Crossff694a82023-12-13 15:54:49 -08003136 if apexInfo, _ := android.ModuleProvider(ctx, android.ApexInfoProvider); !apexInfo.IsForPlatform() {
Colin Crosse07f2312020-08-13 11:24:56 -07003137 // TODO(b/146468504): ApexVariationName() is only a soong module name, not apex name.
Jiyong Parke3833882020-02-17 17:28:10 +09003138 // In most cases, this works fine. But when apex_name is set or override_apex is used
3139 // this can be wrong.
Colin Cross56a83212020-09-15 18:30:11 -07003140 return fmt.Sprintf("/apex/%s/javalib/%s.jar", apexInfo.ApexVariationName, implName)
Jiyong Parke3833882020-02-17 17:28:10 +09003141 }
3142 partition := "system"
3143 if module.SocSpecific() {
3144 partition = "vendor"
3145 } else if module.DeviceSpecific() {
3146 partition = "odm"
3147 } else if module.ProductSpecific() {
3148 partition = "product"
3149 } else if module.SystemExtSpecific() {
3150 partition = "system_ext"
3151 }
3152 return "/" + partition + "/framework/" + implName + ".jar"
3153}
3154
Pedro Loureiro9956e5e2021-09-07 17:21:59 +00003155func formattedOptionalSdkLevelAttribute(ctx android.ModuleContext, attrName string, value *string) string {
3156 if value == nil {
3157 return ""
3158 }
3159 apiLevel, err := android.ApiLevelFromUser(ctx, *value)
3160 if err != nil {
Pedro Loureiroba6682f2021-10-29 09:32:32 +00003161 // attributes in bp files have underscores but in the xml have dashes.
3162 ctx.PropertyErrorf(strings.ReplaceAll(attrName, "-", "_"), err.Error())
Pedro Loureiro9956e5e2021-09-07 17:21:59 +00003163 return ""
3164 }
Pedro Loureirob638c622021-12-22 15:28:05 +00003165 if apiLevel.IsCurrent() {
3166 // passing "current" would always mean a future release, never the current (or the current in
3167 // progress) which means some conditions would never be triggered.
3168 ctx.PropertyErrorf(strings.ReplaceAll(attrName, "-", "_"),
3169 `"current" is not an allowed value for this attribute`)
3170 return ""
3171 }
Pedro Loureiro48991222022-06-17 20:01:21 +00003172 // "safeValue" is safe because it translates finalized codenames to a string
3173 // with their SDK int.
3174 safeValue := apiLevel.String()
3175 return formattedOptionalAttribute(attrName, &safeValue)
Pedro Loureiro9956e5e2021-09-07 17:21:59 +00003176}
3177
3178// formats an attribute for the xml permissions file if the value is not null
3179// returns empty string otherwise
3180func formattedOptionalAttribute(attrName string, value *string) string {
3181 if value == nil {
3182 return ""
3183 }
3184 return fmt.Sprintf(` %s=\"%s\"\n`, attrName, *value)
3185}
3186
Jamie Garsidee570ace2023-11-27 12:07:36 +00003187func formattedDependenciesAttribute(dependencies []string) string {
3188 if dependencies == nil {
3189 return ""
3190 }
3191 return fmt.Sprintf(` dependency=\"%s\"\n`, strings.Join(dependencies, ":"))
3192}
3193
Pedro Loureiro9956e5e2021-09-07 17:21:59 +00003194func (module *sdkLibraryXml) permissionsContents(ctx android.ModuleContext) string {
3195 libName := proptools.String(module.properties.Lib_name)
3196 libNameAttr := formattedOptionalAttribute("name", &libName)
3197 filePath := module.implPath(ctx)
3198 filePathAttr := formattedOptionalAttribute("file", &filePath)
Pedro Loureiroba6682f2021-10-29 09:32:32 +00003199 implicitFromAttr := formattedOptionalSdkLevelAttribute(ctx, "on-bootclasspath-since", module.properties.On_bootclasspath_since)
3200 implicitUntilAttr := formattedOptionalSdkLevelAttribute(ctx, "on-bootclasspath-before", module.properties.On_bootclasspath_before)
3201 minSdkAttr := formattedOptionalSdkLevelAttribute(ctx, "min-device-sdk", module.properties.Min_device_sdk)
3202 maxSdkAttr := formattedOptionalSdkLevelAttribute(ctx, "max-device-sdk", module.properties.Max_device_sdk)
Jamie Garsidee570ace2023-11-27 12:07:36 +00003203 dependenciesAttr := formattedDependenciesAttribute(module.properties.Uses_libs_dependencies)
Pedro Loureiro196d3e62021-12-22 19:53:01 +00003204 // <library> is understood in all android versions whereas <apex-library> is only understood from API T (and ignored before that).
3205 // 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 +00003206 var libraryTag string
3207 if module.properties.Min_device_sdk != nil {
Pedro Loureiro196d3e62021-12-22 19:53:01 +00003208 libraryTag = ` <apex-library\n`
Pedro Loureiroc3621422021-09-28 15:40:23 +00003209 } else {
3210 libraryTag = ` <library\n`
3211 }
Pedro Loureiro9956e5e2021-09-07 17:21:59 +00003212
3213 return strings.Join([]string{
3214 `<?xml version=\"1.0\" encoding=\"utf-8\"?>\n`,
3215 `<!-- Copyright (C) 2018 The Android Open Source Project\n`,
3216 `\n`,
3217 ` Licensed under the Apache License, Version 2.0 (the \"License\");\n`,
3218 ` you may not use this file except in compliance with the License.\n`,
3219 ` You may obtain a copy of the License at\n`,
3220 `\n`,
3221 ` http://www.apache.org/licenses/LICENSE-2.0\n`,
3222 `\n`,
3223 ` Unless required by applicable law or agreed to in writing, software\n`,
3224 ` distributed under the License is distributed on an \"AS IS\" BASIS,\n`,
3225 ` WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\n`,
3226 ` See the License for the specific language governing permissions and\n`,
3227 ` limitations under the License.\n`,
3228 `-->\n`,
3229 `<permissions>\n`,
Pedro Loureiroc3621422021-09-28 15:40:23 +00003230 libraryTag,
Pedro Loureiro9956e5e2021-09-07 17:21:59 +00003231 libNameAttr,
3232 filePathAttr,
3233 implicitFromAttr,
3234 implicitUntilAttr,
3235 minSdkAttr,
3236 maxSdkAttr,
Jamie Garsidee570ace2023-11-27 12:07:36 +00003237 dependenciesAttr,
Pedro Loureiro9956e5e2021-09-07 17:21:59 +00003238 ` />\n`,
3239 `</permissions>\n`}, "")
3240}
3241
Jiyong Parke3833882020-02-17 17:28:10 +09003242func (module *sdkLibraryXml) GenerateAndroidBuildActions(ctx android.ModuleContext) {
Colin Crossff694a82023-12-13 15:54:49 -08003243 apexInfo, _ := android.ModuleProvider(ctx, android.ApexInfoProvider)
3244 module.hideApexVariantFromMake = !apexInfo.IsForPlatform()
Colin Cross56a83212020-09-15 18:30:11 -07003245
Jiyong Parke3833882020-02-17 17:28:10 +09003246 libName := proptools.String(module.properties.Lib_name)
Pedro Loureiroc3621422021-09-28 15:40:23 +00003247 module.selfValidate(ctx)
Pedro Loureiro9956e5e2021-09-07 17:21:59 +00003248 xmlContent := module.permissionsContents(ctx)
Jiyong Parke3833882020-02-17 17:28:10 +09003249
3250 module.outputFilePath = android.PathForModuleOut(ctx, libName+".xml").OutputPath
Colin Crossf1a035e2020-11-16 17:32:30 -08003251 rule := android.NewRuleBuilder(pctx, ctx)
Jiyong Parke3833882020-02-17 17:28:10 +09003252 rule.Command().
3253 Text("/bin/bash -c \"echo -e '" + xmlContent + "'\" > ").
3254 Output(module.outputFilePath)
3255
Colin Crossf1a035e2020-11-16 17:32:30 -08003256 rule.Build("java_sdk_xml", "Permission XML")
Jiyong Parke3833882020-02-17 17:28:10 +09003257
3258 module.installDirPath = android.PathForModuleInstall(ctx, "etc", module.SubDir())
3259}
3260
3261func (module *sdkLibraryXml) AndroidMkEntries() []android.AndroidMkEntries {
Colin Cross56a83212020-09-15 18:30:11 -07003262 if module.hideApexVariantFromMake {
satayev8f088b02021-12-06 11:40:46 +00003263 return []android.AndroidMkEntries{{
Jiyong Parke3833882020-02-17 17:28:10 +09003264 Disabled: true,
3265 }}
3266 }
3267
satayev8f088b02021-12-06 11:40:46 +00003268 return []android.AndroidMkEntries{{
Jiyong Parke3833882020-02-17 17:28:10 +09003269 Class: "ETC",
3270 OutputFile: android.OptionalPathForPath(module.outputFilePath),
3271 ExtraEntries: []android.AndroidMkExtraEntriesFunc{
Colin Crossaa255532020-07-03 13:18:24 -07003272 func(ctx android.AndroidMkExtraEntriesContext, entries *android.AndroidMkEntries) {
Jiyong Parke3833882020-02-17 17:28:10 +09003273 entries.SetString("LOCAL_MODULE_TAGS", "optional")
Colin Crossc68db4b2021-11-11 18:59:15 -08003274 entries.SetString("LOCAL_MODULE_PATH", module.installDirPath.String())
Jiyong Parke3833882020-02-17 17:28:10 +09003275 entries.SetString("LOCAL_INSTALLED_MODULE_STEM", module.outputFilePath.Base())
3276 },
3277 },
3278 }}
3279}
Paul Duffindd46f712020-02-10 13:37:10 +00003280
Pedro Loureiroc3621422021-09-28 15:40:23 +00003281func (module *sdkLibraryXml) selfValidate(ctx android.ModuleContext) {
3282 module.validateAtLeastTAttributes(ctx)
3283 module.validateMinAndMaxDeviceSdk(ctx)
3284 module.validateMinMaxDeviceSdkAndModuleMinSdk(ctx)
3285 module.validateOnBootclasspathBeforeRequirements(ctx)
3286}
3287
3288func (module *sdkLibraryXml) validateAtLeastTAttributes(ctx android.ModuleContext) {
3289 t := android.ApiLevelOrPanic(ctx, "Tiramisu")
3290 module.attrAtLeastT(ctx, t, module.properties.Min_device_sdk, "min_device_sdk")
3291 module.attrAtLeastT(ctx, t, module.properties.Max_device_sdk, "max_device_sdk")
3292 module.attrAtLeastT(ctx, t, module.properties.On_bootclasspath_before, "on_bootclasspath_before")
3293 module.attrAtLeastT(ctx, t, module.properties.On_bootclasspath_since, "on_bootclasspath_since")
3294}
3295
3296func (module *sdkLibraryXml) attrAtLeastT(ctx android.ModuleContext, t android.ApiLevel, attr *string, attrName string) {
3297 if attr != nil {
3298 if level, err := android.ApiLevelFromUser(ctx, *attr); err == nil {
3299 // we will inform the user of invalid inputs when we try to write the
3300 // permissions xml file so we don't need to do it here
3301 if t.GreaterThan(level) {
3302 ctx.PropertyErrorf(attrName, "Attribute value needs to be at least T")
3303 }
3304 }
3305 }
3306}
3307
3308func (module *sdkLibraryXml) validateMinAndMaxDeviceSdk(ctx android.ModuleContext) {
3309 if module.properties.Min_device_sdk != nil && module.properties.Max_device_sdk != nil {
3310 min, minErr := android.ApiLevelFromUser(ctx, *module.properties.Min_device_sdk)
3311 max, maxErr := android.ApiLevelFromUser(ctx, *module.properties.Max_device_sdk)
3312 if minErr == nil && maxErr == nil {
3313 // we will inform the user of invalid inputs when we try to write the
3314 // permissions xml file so we don't need to do it here
3315 if min.GreaterThan(max) {
3316 ctx.ModuleErrorf("min_device_sdk can't be greater than max_device_sdk")
3317 }
3318 }
3319 }
3320}
3321
3322func (module *sdkLibraryXml) validateMinMaxDeviceSdkAndModuleMinSdk(ctx android.ModuleContext) {
3323 moduleMinApi := android.ApiLevelOrPanic(ctx, *module.properties.Sdk_library_min_api_level)
3324 if module.properties.Min_device_sdk != nil {
3325 api, err := android.ApiLevelFromUser(ctx, *module.properties.Min_device_sdk)
3326 if err == nil {
3327 if moduleMinApi.GreaterThan(api) {
3328 ctx.PropertyErrorf("min_device_sdk", "Can't be less than module's min sdk (%s)", moduleMinApi)
3329 }
3330 }
3331 }
3332 if module.properties.Max_device_sdk != nil {
3333 api, err := android.ApiLevelFromUser(ctx, *module.properties.Max_device_sdk)
3334 if err == nil {
3335 if moduleMinApi.GreaterThan(api) {
3336 ctx.PropertyErrorf("max_device_sdk", "Can't be less than module's min sdk (%s)", moduleMinApi)
3337 }
3338 }
3339 }
3340}
3341
3342func (module *sdkLibraryXml) validateOnBootclasspathBeforeRequirements(ctx android.ModuleContext) {
3343 moduleMinApi := android.ApiLevelOrPanic(ctx, *module.properties.Sdk_library_min_api_level)
3344 if module.properties.On_bootclasspath_before != nil {
3345 t := android.ApiLevelOrPanic(ctx, "Tiramisu")
3346 // if we use the attribute, then we need to do this validation
3347 if moduleMinApi.LessThan(t) {
3348 // if minAPi is < T, then we need to have min_device_sdk (which only accepts T+)
3349 if module.properties.Min_device_sdk == nil {
3350 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")
3351 }
3352 }
3353 }
3354}
3355
Paul Duffindd46f712020-02-10 13:37:10 +00003356type sdkLibrarySdkMemberType struct {
3357 android.SdkMemberTypeBase
3358}
3359
Paul Duffin296701e2021-07-14 10:29:36 +01003360func (s *sdkLibrarySdkMemberType) AddDependencies(ctx android.SdkDependencyContext, dependencyTag blueprint.DependencyTag, names []string) {
3361 ctx.AddVariationDependencies(nil, dependencyTag, names...)
Paul Duffindd46f712020-02-10 13:37:10 +00003362}
3363
3364func (s *sdkLibrarySdkMemberType) IsInstance(module android.Module) bool {
3365 _, ok := module.(*SdkLibrary)
3366 return ok
3367}
3368
3369func (s *sdkLibrarySdkMemberType) AddPrebuiltModule(ctx android.SdkMemberContext, member android.SdkMember) android.BpModule {
3370 return ctx.SnapshotBuilder().AddPrebuiltModule(member, "java_sdk_library_import")
3371}
3372
3373func (s *sdkLibrarySdkMemberType) CreateVariantPropertiesStruct() android.SdkMemberProperties {
3374 return &sdkLibrarySdkMemberProperties{}
3375}
3376
Paul Duffin976b0e52021-04-27 23:20:26 +01003377var javaSdkLibrarySdkMemberType = &sdkLibrarySdkMemberType{
3378 android.SdkMemberTypeBase{
3379 PropertyName: "java_sdk_libs",
3380 SupportsSdk: true,
3381 },
3382}
3383
Paul Duffindd46f712020-02-10 13:37:10 +00003384type sdkLibrarySdkMemberProperties struct {
3385 android.SdkMemberPropertiesBase
3386
Paul Duffine8409952022-09-22 16:24:46 +01003387 // Stem name for files in the sdk snapshot.
3388 //
3389 // This is used to construct the path names of various sdk library files in the sdk snapshot to
3390 // make sure that they match the finalized versions of those files in prebuilts/sdk.
3391 //
3392 // This property is marked as keep so that it will be kept in all instances of this struct, will
3393 // not be cleared but will be copied to common structs. That is needed because this field is used
3394 // to construct many file names for other parts of this struct and so it needs to be present in
3395 // all structs. If it was not marked as keep then it would be cleared in some structs and so would
3396 // be unavailable for generating file names if there were other properties that were still set.
3397 Stem string `sdk:"keep"`
3398
Paul Duffindd46f712020-02-10 13:37:10 +00003399 // Scope to per scope properties.
Paul Duffin106a3a42022-01-27 16:39:06 +00003400 Scopes map[*apiScope]*scopeProperties
Paul Duffindd46f712020-02-10 13:37:10 +00003401
Paul Duffin3d1248c2020-04-09 00:10:17 +01003402 // The Java stubs source files.
3403 Stub_srcs []string
Paul Duffinf7a64332020-05-13 16:54:55 +01003404
3405 // The naming scheme.
3406 Naming_scheme *string
Paul Duffind7eb1c22020-05-26 20:57:10 +01003407
3408 // True if the java_sdk_library_import is for a shared library, false
3409 // otherwise.
3410 Shared_library *bool
Paul Duffina2ae7e02020-09-11 11:55:00 +01003411
Paul Duffin1267d872021-04-16 17:21:36 +01003412 // True if the stub imports should produce dex jars.
3413 Compile_dex *bool
3414
Paul Duffina2ae7e02020-09-11 11:55:00 +01003415 // The paths to the doctag files to add to the prebuilt.
3416 Doctag_paths android.Paths
Paul Duffin869de142021-07-15 14:14:41 +01003417
3418 Permitted_packages []string
Pedro Loureiro9956e5e2021-09-07 17:21:59 +00003419
3420 // Signals that this shared library is part of the bootclasspath starting
3421 // on the version indicated in this attribute.
3422 //
3423 // This will make platforms at this level and above to ignore
3424 // <uses-library> tags with this library name because the library is already
3425 // available
3426 On_bootclasspath_since *string
3427
3428 // Signals that this shared library was part of the bootclasspath before
3429 // (but not including) the version indicated in this attribute.
3430 //
3431 // The system will automatically add a <uses-library> tag with this library to
3432 // apps that target any SDK less than the version indicated in this attribute.
3433 On_bootclasspath_before *string
3434
3435 // Indicates that PackageManager should ignore this shared library if the
3436 // platform is below the version indicated in this attribute.
3437 //
3438 // This means that the device won't recognise this library as installed.
3439 Min_device_sdk *string
3440
3441 // Indicates that PackageManager should ignore this shared library if the
3442 // platform is above the version indicated in this attribute.
3443 //
3444 // This means that the device won't recognise this library as installed.
3445 Max_device_sdk *string
Jiakai Zhang9c4dc192023-02-09 00:09:24 +08003446
3447 DexPreoptProfileGuided *bool `supported_build_releases:"UpsideDownCake+"`
Paul Duffindd46f712020-02-10 13:37:10 +00003448}
3449
3450type scopeProperties struct {
Paul Duffin1fd005d2020-04-09 01:08:11 +01003451 Jars android.Paths
3452 StubsSrcJar android.Path
3453 CurrentApiFile android.Path
3454 RemovedApiFile android.Path
Paul Duffine7babdb2022-02-10 13:06:54 +00003455 AnnotationsZip android.Path `supported_build_releases:"Tiramisu+"`
Paul Duffin1fd005d2020-04-09 01:08:11 +01003456 SdkVersion string
Paul Duffindd46f712020-02-10 13:37:10 +00003457}
3458
3459func (s *sdkLibrarySdkMemberProperties) PopulateFromVariant(ctx android.SdkMemberContext, variant android.Module) {
3460 sdk := variant.(*SdkLibrary)
3461
Paul Duffine8409952022-09-22 16:24:46 +01003462 // Copy the stem name for files in the sdk snapshot.
3463 s.Stem = sdk.distStem()
3464
Paul Duffin106a3a42022-01-27 16:39:06 +00003465 s.Scopes = make(map[*apiScope]*scopeProperties)
Paul Duffindd46f712020-02-10 13:37:10 +00003466 for _, apiScope := range allApiScopes {
Paul Duffin803a9562020-05-20 11:52:25 +01003467 paths := sdk.findScopePaths(apiScope)
3468 if paths == nil {
3469 continue
3470 }
3471
Paul Duffindd46f712020-02-10 13:37:10 +00003472 jars := paths.stubsImplPath
3473 if len(jars) > 0 {
3474 properties := scopeProperties{}
3475 properties.Jars = jars
Paul Duffin780c5f42020-05-12 15:52:55 +01003476 properties.SdkVersion = sdk.sdkVersionForStubsLibrary(ctx.SdkModuleContext(), apiScope)
Paul Duffin0f8faff2020-05-20 16:18:00 +01003477 properties.StubsSrcJar = paths.stubsSrcJar.Path()
Paul Duffin10269f12020-06-19 18:39:55 +01003478 if paths.currentApiFilePath.Valid() {
3479 properties.CurrentApiFile = paths.currentApiFilePath.Path()
3480 }
3481 if paths.removedApiFilePath.Valid() {
3482 properties.RemovedApiFile = paths.removedApiFilePath.Path()
3483 }
Anton Hanssond78eb762021-09-21 15:25:12 +01003484 // The annotations zip is only available for modules that set annotations_enabled: true.
3485 if paths.annotationsZip.Valid() {
3486 properties.AnnotationsZip = paths.annotationsZip.Path()
3487 }
Paul Duffin106a3a42022-01-27 16:39:06 +00003488 s.Scopes[apiScope] = &properties
Paul Duffindd46f712020-02-10 13:37:10 +00003489 }
3490 }
3491
Paul Duffindfa131e2020-05-15 20:37:11 +01003492 s.Naming_scheme = sdk.commonSdkLibraryProperties.Naming_scheme
Paul Duffind7eb1c22020-05-26 20:57:10 +01003493 s.Shared_library = proptools.BoolPtr(sdk.sharedLibrary())
Paul Duffin1267d872021-04-16 17:21:36 +01003494 s.Compile_dex = sdk.dexProperties.Compile_dex
Paul Duffina2ae7e02020-09-11 11:55:00 +01003495 s.Doctag_paths = sdk.doctagPaths
Paul Duffin869de142021-07-15 14:14:41 +01003496 s.Permitted_packages = sdk.PermittedPackagesForUpdatableBootJars()
Pedro Loureiro9956e5e2021-09-07 17:21:59 +00003497 s.On_bootclasspath_since = sdk.commonSdkLibraryProperties.On_bootclasspath_since
3498 s.On_bootclasspath_before = sdk.commonSdkLibraryProperties.On_bootclasspath_before
3499 s.Min_device_sdk = sdk.commonSdkLibraryProperties.Min_device_sdk
3500 s.Max_device_sdk = sdk.commonSdkLibraryProperties.Max_device_sdk
Jiakai Zhang9c4dc192023-02-09 00:09:24 +08003501
3502 if sdk.dexpreopter.dexpreoptProperties.Dex_preopt_result.Profile_guided {
3503 s.DexPreoptProfileGuided = proptools.BoolPtr(true)
3504 }
Paul Duffindd46f712020-02-10 13:37:10 +00003505}
3506
3507func (s *sdkLibrarySdkMemberProperties) AddToPropertySet(ctx android.SdkMemberContext, propertySet android.BpPropertySet) {
Paul Duffinf7a64332020-05-13 16:54:55 +01003508 if s.Naming_scheme != nil {
3509 propertySet.AddProperty("naming_scheme", proptools.String(s.Naming_scheme))
3510 }
Paul Duffind7eb1c22020-05-26 20:57:10 +01003511 if s.Shared_library != nil {
3512 propertySet.AddProperty("shared_library", *s.Shared_library)
3513 }
Paul Duffin1267d872021-04-16 17:21:36 +01003514 if s.Compile_dex != nil {
3515 propertySet.AddProperty("compile_dex", *s.Compile_dex)
3516 }
Paul Duffin869de142021-07-15 14:14:41 +01003517 if len(s.Permitted_packages) > 0 {
3518 propertySet.AddProperty("permitted_packages", s.Permitted_packages)
3519 }
Jiakai Zhang9c4dc192023-02-09 00:09:24 +08003520 dexPreoptSet := propertySet.AddPropertySet("dex_preopt")
3521 if s.DexPreoptProfileGuided != nil {
3522 dexPreoptSet.AddProperty("profile_guided", proptools.Bool(s.DexPreoptProfileGuided))
3523 }
Paul Duffinf7a64332020-05-13 16:54:55 +01003524
Paul Duffine8409952022-09-22 16:24:46 +01003525 stem := s.Stem
3526
Paul Duffindd46f712020-02-10 13:37:10 +00003527 for _, apiScope := range allApiScopes {
3528 if properties, ok := s.Scopes[apiScope]; ok {
Paul Duffin6b836ba2020-05-13 19:19:49 +01003529 scopeSet := propertySet.AddPropertySet(apiScope.propertyName)
Paul Duffindd46f712020-02-10 13:37:10 +00003530
Paul Duffin958806b2022-05-16 13:10:47 +00003531 scopeDir := apiScope.snapshotRelativeDir()
Paul Duffin3d1248c2020-04-09 00:10:17 +01003532
Paul Duffindd46f712020-02-10 13:37:10 +00003533 var jars []string
3534 for _, p := range properties.Jars {
Paul Duffine8409952022-09-22 16:24:46 +01003535 dest := filepath.Join(scopeDir, stem+"-stubs.jar")
Paul Duffindd46f712020-02-10 13:37:10 +00003536 ctx.SnapshotBuilder().CopyToSnapshot(p, dest)
3537 jars = append(jars, dest)
3538 }
3539 scopeSet.AddProperty("jars", jars)
3540
Paul Duffin22628d52021-05-12 23:13:22 +01003541 if ctx.SdkModuleContext().Config().IsEnvTrue("SOONG_SDK_SNAPSHOT_USE_SRCJAR") {
3542 // Copy the stubs source jar into the snapshot zip as is.
Paul Duffine8409952022-09-22 16:24:46 +01003543 srcJarSnapshotPath := filepath.Join(scopeDir, stem+".srcjar")
Paul Duffin22628d52021-05-12 23:13:22 +01003544 ctx.SnapshotBuilder().CopyToSnapshot(properties.StubsSrcJar, srcJarSnapshotPath)
3545 scopeSet.AddProperty("stub_srcs", []string{srcJarSnapshotPath})
3546 } else {
3547 // Merge the stubs source jar into the snapshot zip so that when it is unpacked
3548 // the source files are also unpacked.
Paul Duffine8409952022-09-22 16:24:46 +01003549 snapshotRelativeDir := filepath.Join(scopeDir, stem+"_stub_sources")
Paul Duffin22628d52021-05-12 23:13:22 +01003550 ctx.SnapshotBuilder().UnzipToSnapshot(properties.StubsSrcJar, snapshotRelativeDir)
3551 scopeSet.AddProperty("stub_srcs", []string{snapshotRelativeDir})
3552 }
Paul Duffin3d1248c2020-04-09 00:10:17 +01003553
Paul Duffin1fd005d2020-04-09 01:08:11 +01003554 if properties.CurrentApiFile != nil {
Paul Duffine8409952022-09-22 16:24:46 +01003555 currentApiSnapshotPath := apiScope.snapshotRelativeCurrentApiTxtPath(stem)
Paul Duffin1fd005d2020-04-09 01:08:11 +01003556 ctx.SnapshotBuilder().CopyToSnapshot(properties.CurrentApiFile, currentApiSnapshotPath)
3557 scopeSet.AddProperty("current_api", currentApiSnapshotPath)
3558 }
3559
3560 if properties.RemovedApiFile != nil {
Paul Duffine8409952022-09-22 16:24:46 +01003561 removedApiSnapshotPath := apiScope.snapshotRelativeRemovedApiTxtPath(stem)
Paul Duffin3dbf9fd2020-06-02 13:00:02 +01003562 ctx.SnapshotBuilder().CopyToSnapshot(properties.RemovedApiFile, removedApiSnapshotPath)
Paul Duffin1fd005d2020-04-09 01:08:11 +01003563 scopeSet.AddProperty("removed_api", removedApiSnapshotPath)
3564 }
3565
Anton Hanssond78eb762021-09-21 15:25:12 +01003566 if properties.AnnotationsZip != nil {
Paul Duffine8409952022-09-22 16:24:46 +01003567 annotationsSnapshotPath := filepath.Join(scopeDir, stem+"_annotations.zip")
Anton Hanssond78eb762021-09-21 15:25:12 +01003568 ctx.SnapshotBuilder().CopyToSnapshot(properties.AnnotationsZip, annotationsSnapshotPath)
3569 scopeSet.AddProperty("annotations", annotationsSnapshotPath)
3570 }
3571
Paul Duffindd46f712020-02-10 13:37:10 +00003572 if properties.SdkVersion != "" {
3573 scopeSet.AddProperty("sdk_version", properties.SdkVersion)
3574 }
3575 }
3576 }
3577
Paul Duffina2ae7e02020-09-11 11:55:00 +01003578 if len(s.Doctag_paths) > 0 {
3579 dests := []string{}
3580 for _, p := range s.Doctag_paths {
3581 dest := filepath.Join("doctags", p.Rel())
3582 ctx.SnapshotBuilder().CopyToSnapshot(p, dest)
3583 dests = append(dests, dest)
3584 }
3585 propertySet.AddProperty("doctag_files", dests)
3586 }
Paul Duffindd46f712020-02-10 13:37:10 +00003587}