blob: 7373124f42abd655913ff34683736468a58241c7 [file] [log] [blame]
Jiyong Parkc678ad32018-04-10 13:07:10 +09001// Copyright 2018 Google Inc. All rights reserved.
2//
3// Licensed under the Apache License, Version 2.0 (the "License");
4// you may not use this file except in compliance with the License.
5// You may obtain a copy of the License at
6//
7// http://www.apache.org/licenses/LICENSE-2.0
8//
9// Unless required by applicable law or agreed to in writing, software
10// distributed under the License is distributed on an "AS IS" BASIS,
11// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12// See the License for the specific language governing permissions and
13// limitations under the License.
14
15package java
16
17import (
Jiyong Parkc678ad32018-04-10 13:07:10 +090018 "fmt"
19 "path"
Sundong Ahn054b19a2018-10-19 13:46:09 +090020 "path/filepath"
Paul Duffin46a26a82020-04-07 19:27:04 +010021 "reflect"
Paul Duffin46dc45a2020-05-14 15:39:10 +010022 "regexp"
Jiyong Park82484c02018-04-23 21:41:26 +090023 "sort"
Jiyong Parkc678ad32018-04-10 13:07:10 +090024 "strings"
Jiyong Park82484c02018-04-23 21:41:26 +090025 "sync"
Jiyong Parkc678ad32018-04-10 13:07:10 +090026
Paul Duffind1b3a922020-01-22 11:57:20 +000027 "github.com/google/blueprint"
Jiyong Parkc678ad32018-04-10 13:07:10 +090028 "github.com/google/blueprint/proptools"
Paul Duffin46a26a82020-04-07 19:27:04 +010029
30 "android/soong/android"
Ulya Trafimovichdbf31662020-12-17 12:07:54 +000031 "android/soong/dexpreopt"
Jiyong Parkc678ad32018-04-10 13:07:10 +090032)
33
Jooyung Han58f26ab2019-12-18 15:34:32 +090034const (
Pedro Loureiro9956e5e2021-09-07 17:21:59 +000035 sdkXmlFileSuffix = ".xml"
Jiyong Parkc678ad32018-04-10 13:07:10 +090036)
37
Paul Duffind1b3a922020-01-22 11:57:20 +000038// A tag to associated a dependency with a specific api scope.
39type scopeDependencyTag struct {
40 blueprint.BaseDependencyTag
41 name string
42 apiScope *apiScope
Paul Duffinc8782502020-04-29 20:45:27 +010043
44 // Function for extracting appropriate path information from the dependency.
Colin Crossdcf71b22021-02-01 13:59:03 -080045 depInfoExtractor func(paths *scopePaths, ctx android.ModuleContext, dep android.Module) error
Paul Duffinc8782502020-04-29 20:45:27 +010046}
47
48// Extract tag specific information from the dependency.
49func (tag scopeDependencyTag) extractDepInfo(ctx android.ModuleContext, dep android.Module, paths *scopePaths) {
Colin Crossdcf71b22021-02-01 13:59:03 -080050 err := tag.depInfoExtractor(paths, ctx, dep)
Paul Duffinc8782502020-04-29 20:45:27 +010051 if err != nil {
52 ctx.ModuleErrorf("has an invalid {scopeDependencyTag: %s} dependency on module %s: %s", tag.name, ctx.OtherModuleName(dep), err.Error())
53 }
Paul Duffind1b3a922020-01-22 11:57:20 +000054}
55
Paul Duffin80342d72020-06-26 22:08:43 +010056var _ android.ReplaceSourceWithPrebuilt = (*scopeDependencyTag)(nil)
57
58func (tag scopeDependencyTag) ReplaceSourceWithPrebuilt() bool {
59 return false
60}
61
Paul Duffind1b3a922020-01-22 11:57:20 +000062// Provides information about an api scope, e.g. public, system, test.
63type apiScope struct {
64 // The name of the api scope, e.g. public, system, test
65 name string
66
Paul Duffin97b53b82020-05-05 14:40:52 +010067 // The api scope that this scope extends.
Paul Duffind0b9fca2022-09-30 18:11:41 +010068 //
69 // This organizes the scopes into an extension hierarchy.
70 //
71 // If set this means that the API provided by this scope includes the API provided by the scope
72 // set in this field.
Paul Duffin97b53b82020-05-05 14:40:52 +010073 extends *apiScope
74
Paul Duffind0b9fca2022-09-30 18:11:41 +010075 // The next api scope that a library that uses this scope can access.
76 //
77 // This organizes the scopes into an access hierarchy.
78 //
79 // If set this means that a library that can access this API can also access the API provided by
80 // the scope set in this field.
81 //
82 // A module that sets sdk_version: "<scope>_current" should have access to the <scope> API of
83 // every java_sdk_library that it depends on. If the library does not provide an API for <scope>
84 // then it will traverse up this access hierarchy to find an API that it does provide.
85 //
86 // If this is not set then it defaults to the scope set in extends.
87 canAccess *apiScope
88
Paul Duffin3375e352020-04-28 10:44:03 +010089 // The legacy enabled status for a specific scope can be dependent on other
90 // properties that have been specified on the library so it is provided by
91 // a function that can determine the status by examining those properties.
92 legacyEnabledStatus func(module *SdkLibrary) bool
93
94 // The default enabled status for non-legacy behavior, which is triggered by
95 // explicitly enabling at least one api scope.
96 defaultEnabledStatus bool
97
98 // Gets a pointer to the scope specific properties.
99 scopeSpecificProperties func(module *SdkLibrary) *ApiScopeProperties
100
Paul Duffin46a26a82020-04-07 19:27:04 +0100101 // The name of the field in the dynamically created structure.
102 fieldName string
103
Paul Duffin6b836ba2020-05-13 19:19:49 +0100104 // The name of the property in the java_sdk_library_import
105 propertyName string
106
Jihoon Kangbd093452023-12-26 19:08:01 +0000107 // The tag to use to depend on the stubs library module if the parent module
108 // does not differentiate everything and exportable stubs (e.g. sdk_library_import).
Paul Duffind1b3a922020-01-22 11:57:20 +0000109 stubsTag scopeDependencyTag
110
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)
Paul Duffind1b3a922020-01-22 11:57:20 +0000177 scope.stubsTag = 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
Paul Duffin0ff08bd2020-04-29 13:30:54 +0100756func (paths *scopePaths) treatDepAsApiStubsProvider(dep android.Module, action func(provider ApiStubsProvider)) error {
757 if apiStubsProvider, ok := dep.(ApiStubsProvider); ok {
758 action(apiStubsProvider)
Paul Duffinc8782502020-04-29 20:45:27 +0100759 return nil
760 } else {
761 return fmt.Errorf("expected module that implements ApiStubsProvider, e.g. droidstubs")
762 }
763}
764
Jihoon Kangf55a5f72024-01-08 08:56:20 +0000765func (paths *scopePaths) treatDepAsExportableApiStubsProvider(dep android.Module, action func(provider ExportableApiStubsProvider)) error {
766 if exportableApiStubsProvider, ok := dep.(ExportableApiStubsProvider); ok {
767 action(exportableApiStubsProvider)
768 return nil
769 } else {
770 return fmt.Errorf("expected module that implements ExportableApiStubsSrcProvider, e.g. droidstubs")
771 }
772}
773
Paul Duffin0f8faff2020-05-20 16:18:00 +0100774func (paths *scopePaths) treatDepAsApiStubsSrcProvider(dep android.Module, action func(provider ApiStubsSrcProvider)) error {
775 if apiStubsProvider, ok := dep.(ApiStubsSrcProvider); ok {
776 action(apiStubsProvider)
777 return nil
778 } else {
779 return fmt.Errorf("expected module that implements ApiStubsSrcProvider, e.g. droidstubs")
780 }
781}
782
Paul Duffin0ff08bd2020-04-29 13:30:54 +0100783func (paths *scopePaths) extractApiInfoFromApiStubsProvider(provider ApiStubsProvider) {
Anton Hanssond78eb762021-09-21 15:25:12 +0100784 paths.annotationsZip = android.OptionalPathForPath(provider.AnnotationsZip())
Paul Duffin0f8faff2020-05-20 16:18:00 +0100785 paths.currentApiFilePath = android.OptionalPathForPath(provider.ApiFilePath())
786 paths.removedApiFilePath = android.OptionalPathForPath(provider.RemovedApiFilePath())
Paul Duffin0ff08bd2020-04-29 13:30:54 +0100787}
788
Jihoon Kangf55a5f72024-01-08 08:56:20 +0000789func (paths *scopePaths) extractApiInfoFromExportableApiStubsProvider(provider ExportableApiStubsProvider) {
790 paths.annotationsZip = android.OptionalPathForPath(provider.ExportableAnnotationsZip())
791 paths.currentApiFilePath = android.OptionalPathForPath(provider.ExportableApiFilePath())
792 paths.removedApiFilePath = android.OptionalPathForPath(provider.ExportableRemovedApiFilePath())
793}
794
Colin Crossdcf71b22021-02-01 13:59:03 -0800795func (paths *scopePaths) extractApiInfoFromDep(ctx android.ModuleContext, dep android.Module) error {
Paul Duffin0ff08bd2020-04-29 13:30:54 +0100796 return paths.treatDepAsApiStubsProvider(dep, func(provider ApiStubsProvider) {
797 paths.extractApiInfoFromApiStubsProvider(provider)
798 })
799}
800
Paul Duffin0f8faff2020-05-20 16:18:00 +0100801func (paths *scopePaths) extractStubsSourceInfoFromApiStubsProviders(provider ApiStubsSrcProvider) {
802 paths.stubsSrcJar = android.OptionalPathForPath(provider.StubsSrcJar())
Paul Duffin0ff08bd2020-04-29 13:30:54 +0100803}
804
Jihoon Kangf55a5f72024-01-08 08:56:20 +0000805func (paths *scopePaths) extractStubsSourceInfoFromExportableApiStubsProviders(provider ExportableApiStubsSrcProvider) {
806 paths.stubsSrcJar = android.OptionalPathForPath(provider.ExportableStubsSrcJar())
807}
808
Colin Crossdcf71b22021-02-01 13:59:03 -0800809func (paths *scopePaths) extractStubsSourceInfoFromDep(ctx android.ModuleContext, dep android.Module) error {
Paul Duffin0f8faff2020-05-20 16:18:00 +0100810 return paths.treatDepAsApiStubsSrcProvider(dep, func(provider ApiStubsSrcProvider) {
Paul Duffin0ff08bd2020-04-29 13:30:54 +0100811 paths.extractStubsSourceInfoFromApiStubsProviders(provider)
812 })
813}
814
Colin Crossdcf71b22021-02-01 13:59:03 -0800815func (paths *scopePaths) extractStubsSourceAndApiInfoFromApiStubsProvider(ctx android.ModuleContext, dep android.Module) error {
Jihoon Kangf55a5f72024-01-08 08:56:20 +0000816 if ctx.Config().ReleaseHiddenApiExportableStubs() {
817 return paths.treatDepAsExportableApiStubsProvider(dep, func(provider ExportableApiStubsProvider) {
818 paths.extractApiInfoFromExportableApiStubsProvider(provider)
819 paths.extractStubsSourceInfoFromExportableApiStubsProviders(provider)
820 })
821 }
Paul Duffin0ff08bd2020-04-29 13:30:54 +0100822 return paths.treatDepAsApiStubsProvider(dep, func(provider ApiStubsProvider) {
823 paths.extractApiInfoFromApiStubsProvider(provider)
824 paths.extractStubsSourceInfoFromApiStubsProviders(provider)
825 })
826}
827
Paul Duffin958806b2022-05-16 13:10:47 +0000828func extractSingleOptionalOutputPath(dep android.Module) (android.OptionalPath, error) {
829 var paths android.Paths
830 if sourceFileProducer, ok := dep.(android.SourceFileProducer); ok {
831 paths = sourceFileProducer.Srcs()
832 } else {
833 return android.OptionalPath{}, fmt.Errorf("module %q does not produce source files", dep)
834 }
835 if len(paths) != 1 {
836 return android.OptionalPath{}, fmt.Errorf("expected one path from %q, got %q", dep, paths)
837 }
838 return android.OptionalPathForPath(paths[0]), nil
839}
840
841func (paths *scopePaths) extractLatestApiPath(ctx android.ModuleContext, dep android.Module) error {
842 outputPath, err := extractSingleOptionalOutputPath(dep)
843 paths.latestApiPath = outputPath
844 return err
845}
846
847func (paths *scopePaths) extractLatestRemovedApiPath(ctx android.ModuleContext, dep android.Module) error {
848 outputPath, err := extractSingleOptionalOutputPath(dep)
849 paths.latestRemovedApiPath = outputPath
850 return err
851}
852
Paul Duffin0ff08bd2020-04-29 13:30:54 +0100853type commonToSdkLibraryAndImportProperties struct {
Paul Duffin1b1e8062020-05-08 13:44:43 +0100854 // The naming scheme to use for the components that this module creates.
855 //
Paul Duffinee9ad5d2020-09-11 13:04:05 +0100856 // If not specified then it defaults to "default".
Paul Duffin1b1e8062020-05-08 13:44:43 +0100857 //
858 // This is a temporary mechanism to simplify conversion from separate modules for each
859 // component that follow a different naming pattern to the default one.
860 //
861 // TODO(b/155480189) - Remove once naming inconsistencies have been resolved.
Paul Duffin0ff08bd2020-04-29 13:30:54 +0100862 Naming_scheme *string
Paul Duffindfa131e2020-05-15 20:37:11 +0100863
864 // Specifies whether this module can be used as an Android shared library; defaults
865 // to true.
866 //
867 // An Android shared library is one that can be referenced in a <uses-library> element
868 // in an AndroidManifest.xml.
869 Shared_library *bool
Paul Duffina2ae7e02020-09-11 11:55:00 +0100870
871 // Files containing information about supported java doc tags.
872 Doctag_files []string `android:"path"`
Pedro Loureiro9956e5e2021-09-07 17:21:59 +0000873
874 // Signals that this shared library is part of the bootclasspath starting
875 // on the version indicated in this attribute.
876 //
877 // This will make platforms at this level and above to ignore
878 // <uses-library> tags with this library name because the library is already
879 // available
880 On_bootclasspath_since *string
881
882 // Signals that this shared library was part of the bootclasspath before
883 // (but not including) the version indicated in this attribute.
884 //
885 // The system will automatically add a <uses-library> tag with this library to
886 // apps that target any SDK less than the version indicated in this attribute.
887 On_bootclasspath_before *string
888
889 // Indicates that PackageManager should ignore this shared library if the
890 // platform is below the version indicated in this attribute.
891 //
892 // This means that the device won't recognise this library as installed.
893 Min_device_sdk *string
894
895 // Indicates that PackageManager should ignore this shared library if the
896 // platform is above the version indicated in this attribute.
897 //
898 // This means that the device won't recognise this library as installed.
899 Max_device_sdk *string
Paul Duffin0ff08bd2020-04-29 13:30:54 +0100900}
901
Paul Duffin71b33cc2021-06-23 11:39:47 +0100902// commonSdkLibraryAndImportModule defines the interface that must be provided by a module that
903// embeds the commonToSdkLibraryAndImport struct.
904type commonSdkLibraryAndImportModule interface {
Paul Duffind796f6f2022-11-23 23:06:05 +0000905 android.Module
Paul Duffin71b33cc2021-06-23 11:39:47 +0100906
907 BaseModuleName() string
908}
909
Paul Duffin56d44902020-01-31 13:36:25 +0000910// Common code between sdk library and sdk library import
911type commonToSdkLibraryAndImport struct {
Paul Duffin71b33cc2021-06-23 11:39:47 +0100912 module commonSdkLibraryAndImportModule
Paul Duffinc3091c82020-05-08 14:16:20 +0100913
Paul Duffin56d44902020-01-31 13:36:25 +0000914 scopePaths map[*apiScope]*scopePaths
Paul Duffin1b1e8062020-05-08 13:44:43 +0100915
916 namingScheme sdkLibraryComponentNamingScheme
917
Paul Duffindfa131e2020-05-15 20:37:11 +0100918 commonSdkLibraryProperties commonToSdkLibraryAndImportProperties
Paul Duffin859fe962020-05-15 10:20:31 +0100919
Paul Duffina2ae7e02020-09-11 11:55:00 +0100920 // Paths to commonSdkLibraryProperties.Doctag_files
921 doctagPaths android.Paths
922
Paul Duffin859fe962020-05-15 10:20:31 +0100923 // Functionality related to this being used as a component of a java_sdk_library.
924 EmbeddableSdkLibraryComponent
Paul Duffin56d44902020-01-31 13:36:25 +0000925}
926
Paul Duffin71b33cc2021-06-23 11:39:47 +0100927func (c *commonToSdkLibraryAndImport) initCommon(module commonSdkLibraryAndImportModule) {
928 c.module = module
Paul Duffin1b1e8062020-05-08 13:44:43 +0100929
Paul Duffin71b33cc2021-06-23 11:39:47 +0100930 module.AddProperties(&c.commonSdkLibraryProperties)
Paul Duffin859fe962020-05-15 10:20:31 +0100931
932 // Initialize this as an sdk library component.
Paul Duffin71b33cc2021-06-23 11:39:47 +0100933 c.initSdkLibraryComponent(module)
Paul Duffin1b1e8062020-05-08 13:44:43 +0100934}
935
936func (c *commonToSdkLibraryAndImport) initCommonAfterDefaultsApplied(ctx android.DefaultableHookContext) bool {
Paul Duffindfa131e2020-05-15 20:37:11 +0100937 schemeProperty := proptools.StringDefault(c.commonSdkLibraryProperties.Naming_scheme, "default")
Paul Duffin1b1e8062020-05-08 13:44:43 +0100938 switch schemeProperty {
939 case "default":
940 c.namingScheme = &defaultNamingScheme{}
941 default:
942 ctx.PropertyErrorf("naming_scheme", "expected 'default' but was %q", schemeProperty)
943 return false
944 }
945
Paul Duffin3f0290e2021-06-30 18:25:36 +0100946 namePtr := proptools.StringPtr(c.module.BaseModuleName())
947 c.sdkLibraryComponentProperties.SdkLibraryName = namePtr
948
Paul Duffindfa131e2020-05-15 20:37:11 +0100949 // Only track this sdk library if this can be used as a shared library.
950 if c.sharedLibrary() {
951 // Use the name specified in the module definition as the owner.
Paul Duffin3f0290e2021-06-30 18:25:36 +0100952 c.sdkLibraryComponentProperties.SdkLibraryToImplicitlyTrack = namePtr
Paul Duffindfa131e2020-05-15 20:37:11 +0100953 }
Paul Duffin859fe962020-05-15 10:20:31 +0100954
Paul Duffin1b1e8062020-05-08 13:44:43 +0100955 return true
Paul Duffinc3091c82020-05-08 14:16:20 +0100956}
957
Paul Duffinea8f8082021-06-24 13:25:57 +0100958// uniqueApexVariations provides common implementation of the ApexModule.UniqueApexVariations
959// method.
960func (c *commonToSdkLibraryAndImport) uniqueApexVariations() bool {
961 // A java_sdk_library that is a shared library produces an XML file that makes the shared library
962 // usable from an AndroidManifest.xml's <uses-library> entry. That XML file contains the name of
963 // the APEX and so it needs a unique variation per APEX.
964 return c.sharedLibrary()
965}
966
Paul Duffina2ae7e02020-09-11 11:55:00 +0100967func (c *commonToSdkLibraryAndImport) generateCommonBuildActions(ctx android.ModuleContext) {
968 c.doctagPaths = android.PathsForModuleSrc(ctx, c.commonSdkLibraryProperties.Doctag_files)
969}
970
Paul Duffineedc5d52020-06-12 17:46:39 +0100971// Module name of the runtime implementation library
972func (c *commonToSdkLibraryAndImport) implLibraryModuleName() string {
Paul Duffin71b33cc2021-06-23 11:39:47 +0100973 return c.module.BaseModuleName() + ".impl"
Paul Duffineedc5d52020-06-12 17:46:39 +0100974}
975
976// Module name of the XML file for the lib
977func (c *commonToSdkLibraryAndImport) xmlPermissionsModuleName() string {
Paul Duffin71b33cc2021-06-23 11:39:47 +0100978 return c.module.BaseModuleName() + sdkXmlFileSuffix
Paul Duffineedc5d52020-06-12 17:46:39 +0100979}
980
Paul Duffinc3091c82020-05-08 14:16:20 +0100981// Name of the java_library module that compiles the stubs source.
982func (c *commonToSdkLibraryAndImport) stubsLibraryModuleName(apiScope *apiScope) string {
Paul Duffinb97b1572021-04-29 21:50:40 +0100983 baseName := c.module.BaseModuleName()
Paul Duffin21787622022-11-25 12:48:20 +0000984 return c.namingScheme.stubsLibraryModuleName(apiScope, baseName)
Paul Duffinc3091c82020-05-08 14:16:20 +0100985}
986
Jihoon Kangfa4a90d2023-12-20 02:53:38 +0000987// Name of the java_library module that compiles the exportable stubs source.
988func (c *commonToSdkLibraryAndImport) exportableStubsLibraryModuleName(apiScope *apiScope) string {
989 baseName := c.module.BaseModuleName()
990 return c.namingScheme.exportableStubsLibraryModuleName(apiScope, baseName)
991}
992
Paul Duffinc3091c82020-05-08 14:16:20 +0100993// Name of the droidstubs module that generates the stubs source and may also
994// generate/check the API.
995func (c *commonToSdkLibraryAndImport) stubsSourceModuleName(apiScope *apiScope) string {
Paul Duffinb97b1572021-04-29 21:50:40 +0100996 baseName := c.module.BaseModuleName()
Paul Duffin21787622022-11-25 12:48:20 +0000997 return c.namingScheme.stubsSourceModuleName(apiScope, baseName)
Paul Duffinc3091c82020-05-08 14:16:20 +0100998}
999
Jihoon Kang1c92c3e2023-03-23 17:44:51 +00001000// Name of the java_api_library module that generates the from-text stubs source
1001// and compiles to a jar file.
1002func (c *commonToSdkLibraryAndImport) apiLibraryModuleName(apiScope *apiScope) string {
1003 baseName := c.module.BaseModuleName()
1004 return c.namingScheme.apiLibraryModuleName(apiScope, baseName)
1005}
1006
Jihoon Kang1147b312023-06-08 23:25:57 +00001007// Name of the java_library module that compiles the stubs
1008// generated from source Java files.
Jihoon Kangfa4a90d2023-12-20 02:53:38 +00001009func (c *commonToSdkLibraryAndImport) sourceStubsLibraryModuleName(apiScope *apiScope) string {
Jihoon Kang1147b312023-06-08 23:25:57 +00001010 baseName := c.module.BaseModuleName()
Jihoon Kangfa4a90d2023-12-20 02:53:38 +00001011 return c.namingScheme.sourceStubsLibraryModuleName(apiScope, baseName)
1012}
1013
1014// Name of the java_library module that compiles the exportable stubs
1015// generated from source Java files.
1016func (c *commonToSdkLibraryAndImport) exportableSourceStubsLibraryModuleName(apiScope *apiScope) string {
1017 baseName := c.module.BaseModuleName()
1018 return c.namingScheme.exportableSourceStubsLibraryModuleName(apiScope, baseName)
Jihoon Kang1147b312023-06-08 23:25:57 +00001019}
1020
Paul Duffin46dc45a2020-05-14 15:39:10 +01001021// The component names for different outputs of the java_sdk_library.
1022//
1023// They are similar to the names used for the child modules it creates
1024const (
1025 stubsSourceComponentName = "stubs.source"
1026
1027 apiTxtComponentName = "api.txt"
1028
1029 removedApiTxtComponentName = "removed-api.txt"
Anton Hanssond78eb762021-09-21 15:25:12 +01001030
1031 annotationsComponentName = "annotations.zip"
Paul Duffin46dc45a2020-05-14 15:39:10 +01001032)
1033
1034// A regular expression to match tags that reference a specific stubs component.
1035//
1036// It will only match if given a valid scope and a valid component. It is verfy strict
1037// to ensure it does not accidentally match a similar looking tag that should be processed
1038// by the embedded Library.
1039var tagSplitter = func() *regexp.Regexp {
1040 // Given a list of literal string items returns a regular expression that will
1041 // match any one of the items.
1042 choice := func(items ...string) string {
1043 return `\Q` + strings.Join(items, `\E|\Q`) + `\E`
1044 }
1045
1046 // Regular expression to match one of the scopes.
1047 scopesRegexp := choice(allScopeNames...)
1048
1049 // Regular expression to match one of the components.
Anton Hanssond78eb762021-09-21 15:25:12 +01001050 componentsRegexp := choice(stubsSourceComponentName, apiTxtComponentName, removedApiTxtComponentName, annotationsComponentName)
Paul Duffin46dc45a2020-05-14 15:39:10 +01001051
1052 // Regular expression to match any combination of one scope and one component.
1053 return regexp.MustCompile(fmt.Sprintf(`^\.(%s)\.(%s)$`, scopesRegexp, componentsRegexp))
1054}()
1055
1056// For OutputFileProducer interface
1057//
Anton Hanssond78eb762021-09-21 15:25:12 +01001058// .<scope>.<component name>, for all ComponentNames (for example: .public.removed-api.txt)
Paul Duffin46dc45a2020-05-14 15:39:10 +01001059func (c *commonToSdkLibraryAndImport) commonOutputFiles(tag string) (android.Paths, error) {
1060 if groups := tagSplitter.FindStringSubmatch(tag); groups != nil {
1061 scopeName := groups[1]
1062 component := groups[2]
1063
1064 if scope, ok := scopeByName[scopeName]; ok {
1065 paths := c.findScopePaths(scope)
1066 if paths == nil {
Paul Duffin71b33cc2021-06-23 11:39:47 +01001067 return nil, fmt.Errorf("%q does not provide api scope %s", c.module.BaseModuleName(), scopeName)
Paul Duffin46dc45a2020-05-14 15:39:10 +01001068 }
1069
1070 switch component {
1071 case stubsSourceComponentName:
1072 if paths.stubsSrcJar.Valid() {
1073 return android.Paths{paths.stubsSrcJar.Path()}, nil
1074 }
1075
1076 case apiTxtComponentName:
1077 if paths.currentApiFilePath.Valid() {
1078 return android.Paths{paths.currentApiFilePath.Path()}, nil
1079 }
1080
1081 case removedApiTxtComponentName:
1082 if paths.removedApiFilePath.Valid() {
1083 return android.Paths{paths.removedApiFilePath.Path()}, nil
1084 }
Anton Hanssond78eb762021-09-21 15:25:12 +01001085
1086 case annotationsComponentName:
1087 if paths.annotationsZip.Valid() {
1088 return android.Paths{paths.annotationsZip.Path()}, nil
1089 }
Paul Duffin46dc45a2020-05-14 15:39:10 +01001090 }
1091
1092 return nil, fmt.Errorf("%s not available for api scope %s", component, scopeName)
1093 } else {
1094 return nil, fmt.Errorf("unknown scope %s in %s", scope, tag)
1095 }
1096
1097 } else {
Paul Duffina2ae7e02020-09-11 11:55:00 +01001098 switch tag {
1099 case ".doctags":
1100 if c.doctagPaths != nil {
1101 return c.doctagPaths, nil
1102 } else {
Paul Duffin71b33cc2021-06-23 11:39:47 +01001103 return nil, fmt.Errorf("no doctag_files specified on %s", c.module.BaseModuleName())
Paul Duffina2ae7e02020-09-11 11:55:00 +01001104 }
1105 }
Paul Duffin46dc45a2020-05-14 15:39:10 +01001106 return nil, nil
1107 }
1108}
1109
Paul Duffin803a9562020-05-20 11:52:25 +01001110func (c *commonToSdkLibraryAndImport) getScopePathsCreateIfNeeded(scope *apiScope) *scopePaths {
Paul Duffin56d44902020-01-31 13:36:25 +00001111 if c.scopePaths == nil {
1112 c.scopePaths = make(map[*apiScope]*scopePaths)
1113 }
1114 paths := c.scopePaths[scope]
1115 if paths == nil {
1116 paths = &scopePaths{}
1117 c.scopePaths[scope] = paths
1118 }
1119
1120 return paths
1121}
1122
Paul Duffin803a9562020-05-20 11:52:25 +01001123func (c *commonToSdkLibraryAndImport) findScopePaths(scope *apiScope) *scopePaths {
1124 if c.scopePaths == nil {
1125 return nil
1126 }
1127
1128 return c.scopePaths[scope]
1129}
1130
1131// If this does not support the requested api scope then find the closest available
1132// scope it does support. Returns nil if no such scope is available.
1133func (c *commonToSdkLibraryAndImport) findClosestScopePath(scope *apiScope) *scopePaths {
Paul Duffind0b9fca2022-09-30 18:11:41 +01001134 for s := scope; s != nil; s = s.canAccess {
Paul Duffin803a9562020-05-20 11:52:25 +01001135 if paths := c.findScopePaths(s); paths != nil {
1136 return paths
1137 }
1138 }
1139
1140 // This should never happen outside tests as public should be the base scope for every
1141 // scope and is enabled by default.
1142 return nil
1143}
1144
Jiyong Parkf1691d22021-03-29 20:11:58 +09001145func (c *commonToSdkLibraryAndImport) selectHeaderJarsForSdkVersion(ctx android.BaseModuleContext, sdkVersion android.SdkSpec) android.Paths {
Paul Duffinb05d4292020-05-20 12:19:10 +01001146
1147 // If a specific numeric version has been requested then use prebuilt versions of the sdk.
Jiyong Park54105c42021-03-31 18:17:53 +09001148 if !sdkVersion.ApiLevel.IsPreview() {
Paul Duffin71b33cc2021-06-23 11:39:47 +01001149 return PrebuiltJars(ctx, c.module.BaseModuleName(), sdkVersion)
Paul Duffinb05d4292020-05-20 12:19:10 +01001150 }
1151
Paul Duffin1267d872021-04-16 17:21:36 +01001152 paths := c.selectScopePaths(ctx, sdkVersion.Kind)
1153 if paths == nil {
1154 return nil
1155 }
1156
1157 return paths.stubsHeaderPath
1158}
1159
1160// selectScopePaths returns the *scopePaths appropriate for the specific kind.
1161//
1162// If the module does not support the specific kind then it will return the *scopePaths for the
1163// closest kind which is a subset of the requested kind. e.g. if requesting android.SdkModule then
1164// it will return *scopePaths for android.SdkSystem if available or android.SdkPublic of not.
1165func (c *commonToSdkLibraryAndImport) selectScopePaths(ctx android.BaseModuleContext, kind android.SdkKind) *scopePaths {
Paul Duffin32cf58a2021-05-18 16:32:50 +01001166 apiScope := sdkKindToApiScope(kind)
Paul Duffinb05d4292020-05-20 12:19:10 +01001167
Paul Duffin803a9562020-05-20 11:52:25 +01001168 paths := c.findClosestScopePath(apiScope)
1169 if paths == nil {
1170 var scopes []string
1171 for _, s := range allApiScopes {
1172 if c.findScopePaths(s) != nil {
1173 scopes = append(scopes, s.name)
1174 }
1175 }
Paul Duffin71b33cc2021-06-23 11:39:47 +01001176 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 +01001177 return nil
1178 }
1179
Paul Duffin1267d872021-04-16 17:21:36 +01001180 return paths
1181}
1182
Paul Duffin32cf58a2021-05-18 16:32:50 +01001183// sdkKindToApiScope maps from android.SdkKind to apiScope.
1184func sdkKindToApiScope(kind android.SdkKind) *apiScope {
1185 var apiScope *apiScope
1186 switch kind {
1187 case android.SdkSystem:
1188 apiScope = apiScopeSystem
1189 case android.SdkModule:
1190 apiScope = apiScopeModuleLib
1191 case android.SdkTest:
1192 apiScope = apiScopeTest
1193 case android.SdkSystemServer:
1194 apiScope = apiScopeSystemServer
1195 default:
1196 apiScope = apiScopePublic
1197 }
1198 return apiScope
1199}
1200
Paul Duffin1267d872021-04-16 17:21:36 +01001201// to satisfy SdkLibraryDependency interface
Martin Stjernholm8be1e6d2021-09-15 03:34:04 +01001202func (c *commonToSdkLibraryAndImport) SdkApiStubDexJar(ctx android.BaseModuleContext, kind android.SdkKind) OptionalDexJarPath {
Paul Duffin1267d872021-04-16 17:21:36 +01001203 paths := c.selectScopePaths(ctx, kind)
1204 if paths == nil {
Martin Stjernholm8be1e6d2021-09-15 03:34:04 +01001205 return makeUnsetDexJarPath()
Paul Duffin1267d872021-04-16 17:21:36 +01001206 }
1207
1208 return paths.stubsDexJarPath
Paul Duffinb05d4292020-05-20 12:19:10 +01001209}
1210
Paul Duffin32cf58a2021-05-18 16:32:50 +01001211// to satisfy SdkLibraryDependency interface
Jihoon Kangbd093452023-12-26 19:08:01 +00001212func (c *commonToSdkLibraryAndImport) SdkApiExportableStubDexJar(ctx android.BaseModuleContext, kind android.SdkKind) OptionalDexJarPath {
1213 paths := c.selectScopePaths(ctx, kind)
1214 if paths == nil {
1215 return makeUnsetDexJarPath()
1216 }
1217
1218 return paths.exportableStubsDexJarPath
1219}
1220
1221// to satisfy SdkLibraryDependency interface
Paul Duffin32cf58a2021-05-18 16:32:50 +01001222func (c *commonToSdkLibraryAndImport) SdkRemovedTxtFile(ctx android.BaseModuleContext, kind android.SdkKind) android.OptionalPath {
1223 apiScope := sdkKindToApiScope(kind)
1224 paths := c.findScopePaths(apiScope)
1225 if paths == nil {
1226 return android.OptionalPath{}
1227 }
1228
1229 return paths.removedApiFilePath
1230}
1231
Paul Duffin859fe962020-05-15 10:20:31 +01001232func (c *commonToSdkLibraryAndImport) sdkComponentPropertiesForChildLibrary() interface{} {
1233 componentProps := &struct {
Paul Duffin3f0290e2021-06-30 18:25:36 +01001234 SdkLibraryName *string
Paul Duffin859fe962020-05-15 10:20:31 +01001235 SdkLibraryToImplicitlyTrack *string
Paul Duffindfa131e2020-05-15 20:37:11 +01001236 }{}
1237
Paul Duffin3f0290e2021-06-30 18:25:36 +01001238 namePtr := proptools.StringPtr(c.module.BaseModuleName())
1239 componentProps.SdkLibraryName = namePtr
1240
Paul Duffindfa131e2020-05-15 20:37:11 +01001241 if c.sharedLibrary() {
Paul Duffin859fe962020-05-15 10:20:31 +01001242 // Mark the stubs library as being components of this java_sdk_library so that
1243 // any app that includes code which depends (directly or indirectly) on the stubs
1244 // library will have the appropriate <uses-library> invocation inserted into its
1245 // manifest if necessary.
Paul Duffin3f0290e2021-06-30 18:25:36 +01001246 componentProps.SdkLibraryToImplicitlyTrack = namePtr
Paul Duffin859fe962020-05-15 10:20:31 +01001247 }
1248
1249 return componentProps
1250}
1251
Paul Duffindfa131e2020-05-15 20:37:11 +01001252func (c *commonToSdkLibraryAndImport) sharedLibrary() bool {
1253 return proptools.BoolDefault(c.commonSdkLibraryProperties.Shared_library, true)
1254}
1255
Paul Duffinf4600f62021-05-13 22:34:45 +01001256// Check if the stub libraries should be compiled for dex
1257func (c *commonToSdkLibraryAndImport) stubLibrariesCompiledForDex() bool {
1258 // Always compile the dex file files for the stub libraries if they will be used on the
1259 // bootclasspath.
1260 return !c.sharedLibrary()
1261}
1262
Paul Duffin859fe962020-05-15 10:20:31 +01001263// Properties related to the use of a module as an component of a java_sdk_library.
1264type SdkLibraryComponentProperties struct {
Paul Duffin3f0290e2021-06-30 18:25:36 +01001265 // The name of the java_sdk_library/_import module.
1266 SdkLibraryName *string `blueprint:"mutated"`
Paul Duffin859fe962020-05-15 10:20:31 +01001267
1268 // The name of the java_sdk_library/_import to add to a <uses-library> entry
1269 // in the AndroidManifest.xml of any Android app that includes code that references
1270 // this module. If not set then no java_sdk_library/_import is tracked.
1271 SdkLibraryToImplicitlyTrack *string `blueprint:"mutated"`
1272}
1273
1274// Structure to be embedded in a module struct that needs to support the
1275// SdkLibraryComponentDependency interface.
1276type EmbeddableSdkLibraryComponent struct {
1277 sdkLibraryComponentProperties SdkLibraryComponentProperties
1278}
1279
Paul Duffin71b33cc2021-06-23 11:39:47 +01001280func (e *EmbeddableSdkLibraryComponent) initSdkLibraryComponent(module android.Module) {
1281 module.AddProperties(&e.sdkLibraryComponentProperties)
Paul Duffin859fe962020-05-15 10:20:31 +01001282}
1283
1284// to satisfy SdkLibraryComponentDependency
Paul Duffin3f0290e2021-06-30 18:25:36 +01001285func (e *EmbeddableSdkLibraryComponent) SdkLibraryName() *string {
1286 return e.sdkLibraryComponentProperties.SdkLibraryName
1287}
1288
1289// to satisfy SdkLibraryComponentDependency
Ulya Trafimovich39b437b2020-09-23 16:42:35 +01001290func (e *EmbeddableSdkLibraryComponent) OptionalSdkLibraryImplementation() *string {
Ulya Trafimovich78645fb2021-07-16 15:29:25 +01001291 // For shared libraries, this is the same as the SDK library name. If a Java library or app
1292 // depends on a component library (e.g. a stub library) it still needs to know the name of the
1293 // run-time library and the corresponding module that provides the implementation. This name is
1294 // passed to manifest_fixer (to be added to AndroidManifest.xml) and added to CLC (to be used
1295 // in dexpreopt).
1296 //
1297 // For non-shared SDK (component or not) libraries this returns `nil`, as they are not
1298 // <uses-library> and should not be added to the manifest or to CLC.
Ulya Trafimovich39b437b2020-09-23 16:42:35 +01001299 return e.sdkLibraryComponentProperties.SdkLibraryToImplicitlyTrack
1300}
1301
Paul Duffin859fe962020-05-15 10:20:31 +01001302// Implemented by modules that are (or possibly could be) a component of a java_sdk_library
1303// (including the java_sdk_library) itself.
1304type SdkLibraryComponentDependency interface {
Ulya Trafimovich31e444e2020-08-14 17:32:16 +01001305 UsesLibraryDependency
1306
Paul Duffin3f0290e2021-06-30 18:25:36 +01001307 // SdkLibraryName returns the name of the java_sdk_library/_import module.
1308 SdkLibraryName() *string
1309
Ulya Trafimovich39b437b2020-09-23 16:42:35 +01001310 // The name of the implementation library for the optional SDK library or nil, if there isn't one.
1311 OptionalSdkLibraryImplementation() *string
Paul Duffin859fe962020-05-15 10:20:31 +01001312}
1313
1314// Make sure that all the module types that are components of java_sdk_library/_import
1315// and which can be referenced (directly or indirectly) from an android app implement
1316// the SdkLibraryComponentDependency interface.
1317var _ SdkLibraryComponentDependency = (*Library)(nil)
1318var _ SdkLibraryComponentDependency = (*Import)(nil)
1319var _ SdkLibraryComponentDependency = (*SdkLibrary)(nil)
Paul Duffineedc5d52020-06-12 17:46:39 +01001320var _ SdkLibraryComponentDependency = (*SdkLibraryImport)(nil)
Paul Duffin859fe962020-05-15 10:20:31 +01001321
Paul Duffin32cf58a2021-05-18 16:32:50 +01001322// Provides access to sdk_version related files, e.g. header and implementation jars.
Paul Duffin859fe962020-05-15 10:20:31 +01001323type SdkLibraryDependency interface {
1324 SdkLibraryComponentDependency
1325
1326 // Get the header jars appropriate for the supplied sdk_version.
1327 //
1328 // These are turbine generated jars so they only change if the externals of the
1329 // class changes but it does not contain and implementation or JavaDoc.
Jiyong Parkf1691d22021-03-29 20:11:58 +09001330 SdkHeaderJars(ctx android.BaseModuleContext, sdkVersion android.SdkSpec) android.Paths
Paul Duffin859fe962020-05-15 10:20:31 +01001331
1332 // Get the implementation jars appropriate for the supplied sdk version.
1333 //
1334 // These are either the implementation jar for the whole sdk library or the implementation
1335 // jars for the stubs. The latter should only be needed when generating JavaDoc as otherwise
1336 // they are identical to the corresponding header jars.
Jiyong Parkf1691d22021-03-29 20:11:58 +09001337 SdkImplementationJars(ctx android.BaseModuleContext, sdkVersion android.SdkSpec) android.Paths
Paul Duffin1267d872021-04-16 17:21:36 +01001338
Jihoon Kangbd093452023-12-26 19:08:01 +00001339 // SdkApiStubDexJar returns the dex jar for the stubs for the prebuilt
1340 // java_sdk_library_import module. It is needed by the hiddenapi processing tool which
1341 // processes dex files.
Martin Stjernholm8be1e6d2021-09-15 03:34:04 +01001342 SdkApiStubDexJar(ctx android.BaseModuleContext, kind android.SdkKind) OptionalDexJarPath
Paul Duffinf4600f62021-05-13 22:34:45 +01001343
Jihoon Kangbd093452023-12-26 19:08:01 +00001344 // SdkApiExportableStubDexJar returns the exportable dex jar for the stubs for
1345 // java_sdk_library module. It is needed by the hiddenapi processing tool which processes
1346 // dex files.
1347 SdkApiExportableStubDexJar(ctx android.BaseModuleContext, kind android.SdkKind) OptionalDexJarPath
1348
Paul Duffin32cf58a2021-05-18 16:32:50 +01001349 // SdkRemovedTxtFile returns the optional path to the removed.txt file for the specified sdk kind.
1350 SdkRemovedTxtFile(ctx android.BaseModuleContext, kind android.SdkKind) android.OptionalPath
1351
Paul Duffinf4600f62021-05-13 22:34:45 +01001352 // sharedLibrary returns true if this can be used as a shared library.
1353 sharedLibrary() bool
Paul Duffin859fe962020-05-15 10:20:31 +01001354}
1355
Inseob Kimc0907f12019-02-08 21:00:45 +09001356type SdkLibrary struct {
Sundong Ahn054b19a2018-10-19 13:46:09 +09001357 Library
Jiyong Parkc678ad32018-04-10 13:07:10 +09001358
Sundong Ahn054b19a2018-10-19 13:46:09 +09001359 sdkLibraryProperties sdkLibraryProperties
Jiyong Parkc678ad32018-04-10 13:07:10 +09001360
Paul Duffin3375e352020-04-28 10:44:03 +01001361 // Map from api scope to the scope specific property structure.
1362 scopeToProperties map[*apiScope]*ApiScopeProperties
1363
Paul Duffin56d44902020-01-31 13:36:25 +00001364 commonToSdkLibraryAndImport
Jiyong Parkc678ad32018-04-10 13:07:10 +09001365}
1366
Inseob Kimc0907f12019-02-08 21:00:45 +09001367var _ SdkLibraryDependency = (*SdkLibrary)(nil)
Colin Cross897d2ed2019-02-11 14:03:51 -08001368
Paul Duffin3375e352020-04-28 10:44:03 +01001369func (module *SdkLibrary) generateTestAndSystemScopesByDefault() bool {
1370 return module.sdkLibraryProperties.Generate_system_and_test_apis
1371}
1372
1373func (module *SdkLibrary) getGeneratedApiScopes(ctx android.EarlyModuleContext) apiScopes {
1374 // Check to see if any scopes have been explicitly enabled. If any have then all
1375 // must be.
1376 anyScopesExplicitlyEnabled := false
1377 for _, scope := range allApiScopes {
1378 scopeProperties := module.scopeToProperties[scope]
1379 if scopeProperties.Enabled != nil {
1380 anyScopesExplicitlyEnabled = true
1381 break
1382 }
Paul Duffind1b3a922020-01-22 11:57:20 +00001383 }
Paul Duffin3375e352020-04-28 10:44:03 +01001384
1385 var generatedScopes apiScopes
1386 enabledScopes := make(map[*apiScope]struct{})
1387 for _, scope := range allApiScopes {
1388 scopeProperties := module.scopeToProperties[scope]
1389 // If any scopes are explicitly enabled then ignore the legacy enabled status.
1390 // This is to ensure that any new usages of this module type do not rely on legacy
1391 // behaviour.
1392 defaultEnabledStatus := false
1393 if anyScopesExplicitlyEnabled {
1394 defaultEnabledStatus = scope.defaultEnabledStatus
1395 } else {
1396 defaultEnabledStatus = scope.legacyEnabledStatus(module)
1397 }
1398 enabled := proptools.BoolDefault(scopeProperties.Enabled, defaultEnabledStatus)
1399 if enabled {
1400 enabledScopes[scope] = struct{}{}
1401 generatedScopes = append(generatedScopes, scope)
1402 }
1403 }
1404
1405 // Now check to make sure that any scope that is extended by an enabled scope is also
1406 // enabled.
1407 for _, scope := range allApiScopes {
1408 if _, ok := enabledScopes[scope]; ok {
1409 extends := scope.extends
1410 if extends != nil {
1411 if _, ok := enabledScopes[extends]; !ok {
1412 ctx.ModuleErrorf("enabled api scope %q depends on disabled scope %q", scope, extends)
1413 }
1414 }
1415 }
1416 }
1417
1418 return generatedScopes
Paul Duffind1b3a922020-01-22 11:57:20 +00001419}
1420
satayev758968a2021-12-06 11:42:40 +00001421var _ android.ModuleWithMinSdkVersionCheck = (*SdkLibrary)(nil)
1422
satayev8f088b02021-12-06 11:40:46 +00001423func (module *SdkLibrary) CheckMinSdkVersion(ctx android.ModuleContext) {
Spandan Das8c9ae7e2023-03-03 21:20:36 +00001424 android.CheckMinSdkVersion(ctx, module.MinSdkVersion(ctx), func(c android.ModuleContext, do android.PayloadDepsCallback) {
satayev8f088b02021-12-06 11:40:46 +00001425 ctx.WalkDeps(func(child android.Module, parent android.Module) bool {
1426 isExternal := !module.depIsInSameApex(ctx, child)
1427 if am, ok := child.(android.ApexModule); ok {
1428 if !do(ctx, parent, am, isExternal) {
1429 return false
1430 }
1431 }
1432 return !isExternal
1433 })
1434 })
1435}
1436
Paul Duffineedc5d52020-06-12 17:46:39 +01001437type sdkLibraryComponentTag struct {
1438 blueprint.BaseDependencyTag
1439 name string
1440}
1441
1442// Mark this tag so dependencies that use it are excluded from visibility enforcement.
1443func (t sdkLibraryComponentTag) ExcludeFromVisibilityEnforcement() {}
1444
1445var xmlPermissionsFileTag = sdkLibraryComponentTag{name: "xml-permissions-file"}
Paul Duffine74ac732020-02-06 13:51:46 +00001446
Jiyong Parke3833882020-02-17 17:28:10 +09001447func IsXmlPermissionsFileDepTag(depTag blueprint.DependencyTag) bool {
Paul Duffineedc5d52020-06-12 17:46:39 +01001448 if dt, ok := depTag.(sdkLibraryComponentTag); ok {
Jiyong Parke3833882020-02-17 17:28:10 +09001449 return dt == xmlPermissionsFileTag
1450 }
1451 return false
1452}
1453
Paul Duffineedc5d52020-06-12 17:46:39 +01001454var implLibraryTag = sdkLibraryComponentTag{name: "impl-library"}
Paul Duffin5df79302020-05-16 15:52:12 +01001455
Paul Duffin44f1d842020-06-26 20:17:02 +01001456// Add the dependencies on the child modules in the component deps mutator.
1457func (module *SdkLibrary) ComponentDepsMutator(ctx android.BottomUpMutatorContext) {
Paul Duffin3375e352020-04-28 10:44:03 +01001458 for _, apiScope := range module.getGeneratedApiScopes(ctx) {
Paul Duffind1b3a922020-01-22 11:57:20 +00001459 // Add dependencies to the stubs library
Spandan Das877f39d2023-03-29 16:19:51 +00001460 stubModuleName := module.stubsLibraryModuleName(apiScope)
Jihoon Kangbd093452023-12-26 19:08:01 +00001461 ctx.AddVariationDependencies(nil, apiScope.everythingStubsTag, stubModuleName)
Jihoon Kang1147b312023-06-08 23:25:57 +00001462
Jihoon Kangbd093452023-12-26 19:08:01 +00001463 exportableStubModuleName := module.exportableStubsLibraryModuleName(apiScope)
1464 ctx.AddVariationDependencies(nil, apiScope.exportableStubsTag, exportableStubModuleName)
Paul Duffind1b3a922020-01-22 11:57:20 +00001465
Paul Duffin15f34ef2020-07-20 18:04:44 +01001466 // Add a dependency on the stubs source in order to access both stubs source and api information.
1467 ctx.AddVariationDependencies(nil, apiScope.stubsSourceAndApiTag, module.stubsSourceModuleName(apiScope))
Paul Duffin958806b2022-05-16 13:10:47 +00001468
1469 if module.compareAgainstLatestApi(apiScope) {
1470 // Add dependencies on the latest finalized version of the API .txt file.
1471 latestApiModuleName := module.latestApiModuleName(apiScope)
1472 ctx.AddDependency(module, apiScope.latestApiModuleTag, latestApiModuleName)
1473
1474 // Add dependencies on the latest finalized version of the remove API .txt file.
1475 latestRemovedApiModuleName := module.latestRemovedApiModuleName(apiScope)
1476 ctx.AddDependency(module, apiScope.latestRemovedApiModuleTag, latestRemovedApiModuleName)
1477 }
Sundong Ahn054b19a2018-10-19 13:46:09 +09001478 }
1479
Paul Duffindfa131e2020-05-15 20:37:11 +01001480 if module.requiresRuntimeImplementationLibrary() {
Paul Duffin5df79302020-05-16 15:52:12 +01001481 // Add dependency to the rule for generating the implementation library.
1482 ctx.AddDependency(module, implLibraryTag, module.implLibraryModuleName())
1483
Paul Duffindfa131e2020-05-15 20:37:11 +01001484 if module.sharedLibrary() {
1485 // Add dependency to the rule for generating the xml permissions file
Paul Duffineedc5d52020-06-12 17:46:39 +01001486 ctx.AddDependency(module, xmlPermissionsFileTag, module.xmlPermissionsModuleName())
Paul Duffindfa131e2020-05-15 20:37:11 +01001487 }
Paul Duffin44f1d842020-06-26 20:17:02 +01001488 }
1489}
Paul Duffine74ac732020-02-06 13:51:46 +00001490
Paul Duffin44f1d842020-06-26 20:17:02 +01001491// Add other dependencies as normal.
1492func (module *SdkLibrary) DepsMutator(ctx android.BottomUpMutatorContext) {
Anton Hanssone77fccc2021-01-20 16:52:41 +00001493 var missingApiModules []string
1494 for _, apiScope := range module.getGeneratedApiScopes(ctx) {
1495 if apiScope.unstable {
1496 continue
1497 }
Paul Duffin958806b2022-05-16 13:10:47 +00001498 if m := module.latestApiModuleName(apiScope); !ctx.OtherModuleExists(m) {
Anton Hanssone77fccc2021-01-20 16:52:41 +00001499 missingApiModules = append(missingApiModules, m)
1500 }
Paul Duffin958806b2022-05-16 13:10:47 +00001501 if m := module.latestRemovedApiModuleName(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.latestIncompatibilitiesModuleName(apiScope); !ctx.OtherModuleExists(m) {
Jaewoong Jung1a97ee02021-03-09 13:25:02 -08001505 missingApiModules = append(missingApiModules, m)
1506 }
Anton Hanssone77fccc2021-01-20 16:52:41 +00001507 }
1508 if len(missingApiModules) != 0 && !module.sdkLibraryProperties.Unsafe_ignore_missing_latest_api {
1509 m := module.Name() + " is missing tracking files for previously released library versions.\n"
1510 m += "You need to do one of the following:\n"
1511 m += "- Add `unsafe_ignore_missing_latest_api: true` to your blueprint (to disable compat tracking)\n"
1512 m += "- Add a set of prebuilt txt files representing the last released version of this library for compat checking.\n"
1513 m += " (the current set of API files can be used as a seed for this compatibility tracking\n"
1514 m += "\n"
1515 m += "The following filegroup modules are missing:\n "
1516 m += strings.Join(missingApiModules, "\n ") + "\n"
1517 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."
1518 ctx.ModuleErrorf(m)
1519 }
Paul Duffin44f1d842020-06-26 20:17:02 +01001520 if module.requiresRuntimeImplementationLibrary() {
Paul Duffindfa131e2020-05-15 20:37:11 +01001521 // Only add the deps for the library if it is actually going to be built.
1522 module.Library.deps(ctx)
1523 }
Jiyong Parkc678ad32018-04-10 13:07:10 +09001524}
1525
Paul Duffin46dc45a2020-05-14 15:39:10 +01001526func (module *SdkLibrary) OutputFiles(tag string) (android.Paths, error) {
1527 paths, err := module.commonOutputFiles(tag)
Colin Cross4acaea92021-12-10 23:05:02 +00001528 if paths != nil || err != nil {
Paul Duffin46dc45a2020-05-14 15:39:10 +01001529 return paths, err
1530 }
Colin Cross4acaea92021-12-10 23:05:02 +00001531 if module.requiresRuntimeImplementationLibrary() {
1532 return module.Library.OutputFiles(tag)
1533 }
1534 if tag == "" {
1535 return nil, nil
1536 }
1537 return nil, fmt.Errorf("unsupported module reference tag %q", tag)
Paul Duffin46dc45a2020-05-14 15:39:10 +01001538}
1539
Inseob Kimc0907f12019-02-08 21:00:45 +09001540func (module *SdkLibrary) GenerateAndroidBuildActions(ctx android.ModuleContext) {
satayev8f088b02021-12-06 11:40:46 +00001541 if proptools.String(module.deviceProperties.Min_sdk_version) != "" {
1542 module.CheckMinSdkVersion(ctx)
1543 }
1544
Paul Duffina2ae7e02020-09-11 11:55:00 +01001545 module.generateCommonBuildActions(ctx)
1546
Paul Duffindfa131e2020-05-15 20:37:11 +01001547 // Only build an implementation library if required.
1548 if module.requiresRuntimeImplementationLibrary() {
Paul Duffin43db9be2019-12-30 17:35:49 +00001549 module.Library.GenerateAndroidBuildActions(ctx)
1550 }
Sundong Ahn054b19a2018-10-19 13:46:09 +09001551
Paul Duffinb97b1572021-04-29 21:50:40 +01001552 // Collate the components exported by this module. All scope specific modules are exported but
1553 // the impl and xml component modules are not.
1554 exportedComponents := map[string]struct{}{}
1555
Sundong Ahn57368eb2018-07-06 11:20:23 +09001556 // Record the paths to the header jars of the library (stubs and impl).
Paul Duffind1b3a922020-01-22 11:57:20 +00001557 // When this java_sdk_library is depended upon from others via "libs" property,
Jiyong Parkc678ad32018-04-10 13:07:10 +09001558 // the recorded paths will be returned depending on the link type of the caller.
1559 ctx.VisitDirectDeps(func(to android.Module) {
Jiyong Parkc678ad32018-04-10 13:07:10 +09001560 tag := ctx.OtherModuleDependencyTag(to)
1561
Paul Duffinc8782502020-04-29 20:45:27 +01001562 // Extract information from any of the scope specific dependencies.
1563 if scopeTag, ok := tag.(scopeDependencyTag); ok {
1564 apiScope := scopeTag.apiScope
Paul Duffin803a9562020-05-20 11:52:25 +01001565 scopePaths := module.getScopePathsCreateIfNeeded(apiScope)
Paul Duffinc8782502020-04-29 20:45:27 +01001566
1567 // Extract information from the dependency. The exact information extracted
1568 // is determined by the nature of the dependency which is determined by the tag.
1569 scopeTag.extractDepInfo(ctx, to, scopePaths)
Paul Duffinb97b1572021-04-29 21:50:40 +01001570
1571 exportedComponents[ctx.OtherModuleName(to)] = struct{}{}
Sundong Ahn20e998b2018-07-24 11:19:26 +09001572 }
Jiyong Parkc678ad32018-04-10 13:07:10 +09001573 })
Paul Duffinb97b1572021-04-29 21:50:40 +01001574
1575 // Make the set of components exported by this module available for use elsewhere.
Cole Faust18994c72023-02-28 16:02:16 -08001576 exportedComponentInfo := android.ExportedComponentsInfo{Components: android.SortedKeys(exportedComponents)}
Colin Cross40213022023-12-13 15:19:49 -08001577 android.SetProvider(ctx, android.ExportedComponentsInfoProvider, exportedComponentInfo)
Paul Duffin958806b2022-05-16 13:10:47 +00001578
1579 // Provide additional information for inclusion in an sdk's generated .info file.
1580 additionalSdkInfo := map[string]interface{}{}
1581 additionalSdkInfo["dist_stem"] = module.distStem()
Paul Duffine8409952022-09-22 16:24:46 +01001582 baseModuleName := module.distStem()
Paul Duffin958806b2022-05-16 13:10:47 +00001583 scopes := map[string]interface{}{}
1584 additionalSdkInfo["scopes"] = scopes
1585 for scope, scopePaths := range module.scopePaths {
1586 scopeInfo := map[string]interface{}{}
1587 scopes[scope.name] = scopeInfo
1588 scopeInfo["current_api"] = scope.snapshotRelativeCurrentApiTxtPath(baseModuleName)
1589 scopeInfo["removed_api"] = scope.snapshotRelativeRemovedApiTxtPath(baseModuleName)
1590 if p := scopePaths.latestApiPath; p.Valid() {
1591 scopeInfo["latest_api"] = p.Path().String()
1592 }
1593 if p := scopePaths.latestRemovedApiPath; p.Valid() {
1594 scopeInfo["latest_removed_api"] = p.Path().String()
1595 }
1596 }
Colin Cross40213022023-12-13 15:19:49 -08001597 android.SetProvider(ctx, android.AdditionalSdkInfoProvider, android.AdditionalSdkInfo{additionalSdkInfo})
Jiyong Parkc678ad32018-04-10 13:07:10 +09001598}
1599
Jiyong Park0b0e1b92019-12-03 13:24:29 +09001600func (module *SdkLibrary) AndroidMkEntries() []android.AndroidMkEntries {
Paul Duffindfa131e2020-05-15 20:37:11 +01001601 if !module.requiresRuntimeImplementationLibrary() {
Paul Duffin43db9be2019-12-30 17:35:49 +00001602 return nil
1603 }
Jiyong Park0b0e1b92019-12-03 13:24:29 +09001604 entriesList := module.Library.AndroidMkEntries()
Yo Chiang07d75072020-06-05 17:43:19 +08001605 if module.sharedLibrary() {
1606 entries := &entriesList[0]
1607 entries.Required = append(entries.Required, module.xmlPermissionsModuleName())
1608 }
Jiyong Park0b0e1b92019-12-03 13:24:29 +09001609 return entriesList
Jiyong Park82484c02018-04-23 21:41:26 +09001610}
1611
Anton Hansson5fd5d242020-03-27 19:43:19 +00001612// The dist path of the stub artifacts
1613func (module *SdkLibrary) apiDistPath(apiScope *apiScope) string {
Colin Crossf0eace92021-06-02 13:02:23 -07001614 return path.Join("apistubs", module.distGroup(), apiScope.name)
Anton Hansson5fd5d242020-03-27 19:43:19 +00001615}
1616
Paul Duffin12ceb462019-12-24 20:31:31 +00001617// Get the sdk version for use when compiling the stubs library.
Paul Duffin780c5f42020-05-12 15:52:55 +01001618func (module *SdkLibrary) sdkVersionForStubsLibrary(mctx android.EarlyModuleContext, apiScope *apiScope) string {
Paul Duffin87a05a32020-05-12 11:50:28 +01001619 scopeProperties := module.scopeToProperties[apiScope]
1620 if scopeProperties.Sdk_version != nil {
1621 return proptools.String(scopeProperties.Sdk_version)
1622 }
1623
Jiyong Parkf1691d22021-03-29 20:11:58 +09001624 sdkDep := decodeSdkDep(mctx, android.SdkContext(&module.Library))
Paul Duffin12ceb462019-12-24 20:31:31 +00001625 if sdkDep.hasStandardLibs() {
1626 // If building against a standard sdk then use the sdk version appropriate for the scope.
Paul Duffind1b3a922020-01-22 11:57:20 +00001627 return apiScope.sdkVersion
Paul Duffin12ceb462019-12-24 20:31:31 +00001628 } else {
1629 // Otherwise, use no system module.
1630 return "none"
1631 }
1632}
1633
Paul Duffin31310252020-11-20 21:26:20 +00001634func (module *SdkLibrary) distStem() string {
1635 return proptools.StringDefault(module.sdkLibraryProperties.Dist_stem, module.BaseModuleName())
1636}
1637
Colin Cross986b69a2021-06-01 13:13:40 -07001638// distGroup returns the subdirectory of the dist path of the stub artifacts.
1639func (module *SdkLibrary) distGroup() string {
Colin Cross59b92bf2021-06-01 14:07:56 -07001640 return proptools.StringDefault(module.sdkLibraryProperties.Dist_group, "unknown")
Colin Cross986b69a2021-06-01 13:13:40 -07001641}
1642
Paul Duffin958806b2022-05-16 13:10:47 +00001643func latestPrebuiltApiModuleName(name string, apiScope *apiScope) string {
1644 return PrebuiltApiModuleName(name, apiScope.name, "latest")
1645}
1646
Paul Duffind1b3a922020-01-22 11:57:20 +00001647func (module *SdkLibrary) latestApiFilegroupName(apiScope *apiScope) string {
Paul Duffin958806b2022-05-16 13:10:47 +00001648 return ":" + module.latestApiModuleName(apiScope)
1649}
1650
1651func (module *SdkLibrary) latestApiModuleName(apiScope *apiScope) string {
1652 return latestPrebuiltApiModuleName(module.distStem(), apiScope)
Jiyong Park58c518b2018-05-12 22:29:12 +09001653}
Jiyong Parkc678ad32018-04-10 13:07:10 +09001654
Paul Duffind1b3a922020-01-22 11:57:20 +00001655func (module *SdkLibrary) latestRemovedApiFilegroupName(apiScope *apiScope) string {
Paul Duffin958806b2022-05-16 13:10:47 +00001656 return ":" + module.latestRemovedApiModuleName(apiScope)
1657}
1658
1659func (module *SdkLibrary) latestRemovedApiModuleName(apiScope *apiScope) string {
1660 return latestPrebuiltApiModuleName(module.distStem()+"-removed", apiScope)
Jiyong Parkc678ad32018-04-10 13:07:10 +09001661}
1662
Jaewoong Jung1a97ee02021-03-09 13:25:02 -08001663func (module *SdkLibrary) latestIncompatibilitiesFilegroupName(apiScope *apiScope) string {
Paul Duffin958806b2022-05-16 13:10:47 +00001664 return ":" + module.latestIncompatibilitiesModuleName(apiScope)
1665}
1666
1667func (module *SdkLibrary) latestIncompatibilitiesModuleName(apiScope *apiScope) string {
1668 return latestPrebuiltApiModuleName(module.distStem()+"-incompatibilities", apiScope)
Jaewoong Jung1a97ee02021-03-09 13:25:02 -08001669}
1670
Jihoon Kang1c92c3e2023-03-23 17:44:51 +00001671func (module *SdkLibrary) contributesToApiSurface(c android.Config) bool {
1672 _, exists := c.GetApiLibraries()[module.Name()]
1673 return exists
1674}
1675
Jihoon Kang0c705a42023-08-02 06:44:57 +00001676// The listed modules are the special java_sdk_libraries where apiScope.kind do not match the
1677// api surface that the module contribute to. For example, the public droidstubs and java_library
1678// do not contribute to the public api surface, but contributes to the core platform api surface.
1679// This method returns the full api surface stub lib that
1680// the generated java_api_library should depend on.
1681func (module *SdkLibrary) alternativeFullApiSurfaceStubLib() string {
1682 if val, ok := apiLibraryAdditionalProperties[module.Name()]; ok {
1683 return val.FullApiSurfaceStubLib
1684 }
1685 return ""
1686}
1687
1688// The listed modules' stubs contents do not match the corresponding txt files,
1689// but require additional api contributions to generate the full stubs.
1690// This method returns the name of the additional api contribution module
1691// for corresponding sdk_library modules.
1692func (module *SdkLibrary) apiLibraryAdditionalApiContribution() string {
1693 if val, ok := apiLibraryAdditionalProperties[module.Name()]; ok {
1694 return val.AdditionalApiContribution
1695 }
1696 return ""
1697}
1698
Anton Hansson944e77d2020-08-19 11:40:22 +01001699func childModuleVisibility(childVisibility []string) []string {
1700 if childVisibility == nil {
1701 // No child visibility set. The child will use the visibility of the sdk_library.
1702 return nil
1703 }
1704
1705 // Prepend an override to ignore the sdk_library's visibility, and rely on the child visibility.
1706 var visibility []string
1707 visibility = append(visibility, "//visibility:override")
1708 visibility = append(visibility, childVisibility...)
1709 return visibility
1710}
1711
Paul Duffin5df79302020-05-16 15:52:12 +01001712// Creates the implementation java library
1713func (module *SdkLibrary) createImplLibrary(mctx android.DefaultableHookContext) {
Anton Hansson944e77d2020-08-19 11:40:22 +01001714 visibility := childModuleVisibility(module.sdkLibraryProperties.Impl_library_visibility)
1715
Paul Duffin5df79302020-05-16 15:52:12 +01001716 props := struct {
Paul Duffin77590a82022-04-28 14:13:30 +00001717 Name *string
1718 Visibility []string
1719 Instrument bool
1720 Libs []string
1721 Static_libs []string
1722 Apex_available []string
Paul Duffin5df79302020-05-16 15:52:12 +01001723 }{
1724 Name: proptools.StringPtr(module.implLibraryModuleName()),
Anton Hansson944e77d2020-08-19 11:40:22 +01001725 Visibility: visibility,
Paul Duffin296cf332020-06-18 21:09:55 +01001726 // Set the instrument property to ensure it is instrumented when instrumentation is required.
1727 Instrument: true,
Anton Hansson7f66efa2020-10-08 14:47:23 +01001728 // Set the impl_only libs. Note that the module's "Libs" get appended as well, via the
1729 // addition of &module.properties below.
1730 Libs: module.sdkLibraryProperties.Impl_only_libs,
Paul Duffin77590a82022-04-28 14:13:30 +00001731 // Set the impl_only static libs. Note that the module's "static_libs" get appended as well, via the
1732 // addition of &module.properties below.
1733 Static_libs: module.sdkLibraryProperties.Impl_only_static_libs,
1734 // Pass the apex_available settings down so that the impl library can be statically
1735 // embedded within a library that is added to an APEX. Needed for updatable-media.
1736 Apex_available: module.ApexAvailable(),
Paul Duffin5df79302020-05-16 15:52:12 +01001737 }
1738
1739 properties := []interface{}{
1740 &module.properties,
1741 &module.protoProperties,
1742 &module.deviceProperties,
Liz Kammera7a64f32020-07-09 15:16:41 -07001743 &module.dexProperties,
Paul Duffin5df79302020-05-16 15:52:12 +01001744 &module.dexpreoptProperties,
Colin Cross014489c2020-06-02 20:09:13 -07001745 &module.linter.properties,
Paul Duffin5df79302020-05-16 15:52:12 +01001746 &props,
1747 module.sdkComponentPropertiesForChildLibrary(),
1748 }
1749 mctx.CreateModule(LibraryFactory, properties...)
1750}
1751
Jihoon Kangfa4a90d2023-12-20 02:53:38 +00001752type libraryProperties struct {
1753 Name *string
1754 Visibility []string
1755 Srcs []string
1756 Installable *bool
1757 Sdk_version *string
1758 System_modules *string
1759 Patch_module *string
1760 Libs []string
1761 Static_libs []string
1762 Compile_dex *bool
1763 Java_version *string
1764 Openjdk9 struct {
1765 Srcs []string
1766 Javacflags []string
1767 }
1768 Dist struct {
1769 Targets []string
1770 Dest *string
1771 Dir *string
1772 Tag *string
1773 }
1774}
Jiyong Parkc678ad32018-04-10 13:07:10 +09001775
Jihoon Kangfa4a90d2023-12-20 02:53:38 +00001776func (module *SdkLibrary) stubsLibraryProps(mctx android.DefaultableHookContext, apiScope *apiScope) libraryProperties {
1777 props := libraryProperties{}
Jihoon Kang786df932023-09-07 01:18:31 +00001778 props.Visibility = []string{"//visibility:override", "//visibility:private"}
Jiyong Parkc678ad32018-04-10 13:07:10 +09001779 // sources are generated from the droiddoc
Paul Duffin12ceb462019-12-24 20:31:31 +00001780 sdkVersion := module.sdkVersionForStubsLibrary(mctx, apiScope)
Paul Duffin52d398a2019-06-11 12:31:14 +01001781 props.Sdk_version = proptools.StringPtr(sdkVersion)
Paul Duffina18abc22020-05-16 18:54:24 +01001782 props.System_modules = module.deviceProperties.System_modules
1783 props.Patch_module = module.properties.Patch_module
Paul Duffin367ab912019-12-23 19:40:36 +00001784 props.Installable = proptools.BoolPtr(false)
Sundong Ahn054b19a2018-10-19 13:46:09 +09001785 props.Libs = module.sdkLibraryProperties.Stub_only_libs
Mark White9421c4c2023-08-10 00:07:03 +00001786 props.Libs = append(props.Libs, module.scopeToProperties[apiScope].Libs...)
Anton Hanssondae54cd2021-04-21 16:30:10 +01001787 props.Static_libs = module.sdkLibraryProperties.Stub_only_static_libs
Paul Duffine22c2ab2020-05-20 19:35:27 +01001788 // The stub-annotations library contains special versions of the annotations
1789 // with CLASS retention policy, so that they're kept.
1790 if proptools.Bool(module.sdkLibraryProperties.Annotations_enabled) {
1791 props.Libs = append(props.Libs, "stub-annotations")
1792 }
Paul Duffina18abc22020-05-16 18:54:24 +01001793 props.Openjdk9.Srcs = module.properties.Openjdk9.Srcs
1794 props.Openjdk9.Javacflags = module.properties.Openjdk9.Javacflags
Anton Hansson83509b52020-05-21 09:21:57 +01001795 // We compile the stubs for 1.8 in line with the main android.jar stubs, and potential
1796 // interop with older developer tools that don't support 1.9.
1797 props.Java_version = proptools.StringPtr("1.8")
Paul Duffinf4600f62021-05-13 22:34:45 +01001798
Jihoon Kangfa4a90d2023-12-20 02:53:38 +00001799 return props
1800}
1801
1802// Creates a static java library that has API stubs
1803func (module *SdkLibrary) createStubsLibrary(mctx android.DefaultableHookContext, apiScope *apiScope) {
1804
1805 props := module.stubsLibraryProps(mctx, apiScope)
1806 props.Name = proptools.StringPtr(module.sourceStubsLibraryModuleName(apiScope))
1807 props.Srcs = []string{":" + module.stubsSourceModuleName(apiScope)}
1808
1809 mctx.CreateModule(LibraryFactory, &props, module.sdkComponentPropertiesForChildLibrary())
1810}
1811
1812// Create a static java library that compiles the "exportable" stubs
1813func (module *SdkLibrary) createExportableStubsLibrary(mctx android.DefaultableHookContext, apiScope *apiScope) {
1814 props := module.stubsLibraryProps(mctx, apiScope)
1815 props.Name = proptools.StringPtr(module.exportableSourceStubsLibraryModuleName(apiScope))
1816 props.Srcs = []string{":" + module.stubsSourceModuleName(apiScope) + "{.exportable}"}
1817
Paul Duffin859fe962020-05-15 10:20:31 +01001818 mctx.CreateModule(LibraryFactory, &props, module.sdkComponentPropertiesForChildLibrary())
Jiyong Parkc678ad32018-04-10 13:07:10 +09001819}
1820
Paul Duffin6d0886e2020-04-07 18:49:53 +01001821// Creates a droidstubs module that creates stubs source files from the given full source
Paul Duffinc8782502020-04-29 20:45:27 +01001822// files and also updates and checks the API specification files.
Paul Duffin15f34ef2020-07-20 18:04:44 +01001823func (module *SdkLibrary) createStubsSourcesAndApi(mctx android.DefaultableHookContext, apiScope *apiScope, name string, scopeSpecificDroidstubsArgs []string) {
Jiyong Parkc678ad32018-04-10 13:07:10 +09001824 props := struct {
Sundong Ahn054b19a2018-10-19 13:46:09 +09001825 Name *string
Paul Duffin4911a892020-04-29 23:35:13 +01001826 Visibility []string
Sundong Ahn054b19a2018-10-19 13:46:09 +09001827 Srcs []string
1828 Installable *bool
Paul Duffin52d398a2019-06-11 12:31:14 +01001829 Sdk_version *string
Jihoon Kang3198f3c2023-01-26 08:08:52 +00001830 Api_surface *string
Paul Duffin12ceb462019-12-24 20:31:31 +00001831 System_modules *string
Sundong Ahn054b19a2018-10-19 13:46:09 +09001832 Libs []string
Paul Duffin6877e6d2020-09-25 19:59:14 +01001833 Output_javadoc_comments *bool
Paul Duffin11512472019-02-11 15:55:17 +00001834 Arg_files []string
Sundong Ahn054b19a2018-10-19 13:46:09 +09001835 Args *string
Sundong Ahn054b19a2018-10-19 13:46:09 +09001836 Java_version *string
Paul Duffine22c2ab2020-05-20 19:35:27 +01001837 Annotations_enabled *bool
Sundong Ahn054b19a2018-10-19 13:46:09 +09001838 Merge_annotations_dirs []string
1839 Merge_inclusion_annotations_dirs []string
Paul Duffin0ff08bd2020-04-29 13:30:54 +01001840 Generate_stubs *bool
Anton Hanssone87b03d2020-12-21 15:29:34 +00001841 Previous_api *string
Jihoon Kang6592e872023-12-19 01:13:16 +00001842 Aconfig_declarations []string
Sundong Ahn054b19a2018-10-19 13:46:09 +09001843 Check_api struct {
Anton Hanssone6056152020-12-31 10:37:27 +00001844 Current ApiToCheck
1845 Last_released ApiToCheck
Paul Duffin160fe412020-05-10 19:32:20 +01001846
1847 Api_lint struct {
1848 Enabled *bool
1849 New_since *string
1850 Baseline_file *string
1851 }
Jiyong Park58c518b2018-05-12 22:29:12 +09001852 }
Sundong Ahn1b92c822018-05-29 11:35:17 +09001853 Aidl struct {
1854 Include_dirs []string
1855 Local_include_dirs []string
1856 }
Paul Duffin040e9062020-11-23 17:41:36 +00001857 Dists []android.Dist
Jiyong Parkc678ad32018-04-10 13:07:10 +09001858 }{}
1859
Paul Duffin7b78b4d2020-04-28 14:08:32 +01001860 // The stubs source processing uses the same compile time classpath when extracting the
1861 // API from the implementation library as it does when compiling it. i.e. the same
1862 // * sdk version
1863 // * system_modules
1864 // * libs (static_libs/libs)
Paul Duffin250e6192019-06-07 10:44:37 +01001865
Paul Duffin0ff08bd2020-04-29 13:30:54 +01001866 props.Name = proptools.StringPtr(name)
Anton Hansson944e77d2020-08-19 11:40:22 +01001867 props.Visibility = childModuleVisibility(module.sdkLibraryProperties.Stubs_source_visibility)
Paul Duffina18abc22020-05-16 18:54:24 +01001868 props.Srcs = append(props.Srcs, module.properties.Srcs...)
Anton Hanssonf8ea3722021-09-16 14:24:13 +01001869 props.Srcs = append(props.Srcs, module.sdkLibraryProperties.Api_srcs...)
Paul Duffina18abc22020-05-16 18:54:24 +01001870 props.Sdk_version = module.deviceProperties.Sdk_version
Jihoon Kang3198f3c2023-01-26 08:08:52 +00001871 props.Api_surface = &apiScope.name
Paul Duffina18abc22020-05-16 18:54:24 +01001872 props.System_modules = module.deviceProperties.System_modules
Jiyong Parkc678ad32018-04-10 13:07:10 +09001873 props.Installable = proptools.BoolPtr(false)
Sundong Ahne6f0b052018-06-05 16:46:14 +09001874 // A droiddoc module has only one Libs property and doesn't distinguish between
1875 // shared libs and static libs. So we need to add both of these libs to Libs property.
Paul Duffina18abc22020-05-16 18:54:24 +01001876 props.Libs = module.properties.Libs
1877 props.Libs = append(props.Libs, module.properties.Static_libs...)
Nikita Ioffed732da72022-11-21 12:38:25 +00001878 props.Libs = append(props.Libs, module.sdkLibraryProperties.Stub_only_libs...)
Mark White9421c4c2023-08-10 00:07:03 +00001879 props.Libs = append(props.Libs, module.scopeToProperties[apiScope].Libs...)
Paul Duffina18abc22020-05-16 18:54:24 +01001880 props.Aidl.Include_dirs = module.deviceProperties.Aidl.Include_dirs
1881 props.Aidl.Local_include_dirs = module.deviceProperties.Aidl.Local_include_dirs
1882 props.Java_version = module.properties.Java_version
Jiyong Parkc678ad32018-04-10 13:07:10 +09001883
Paul Duffine22c2ab2020-05-20 19:35:27 +01001884 props.Annotations_enabled = module.sdkLibraryProperties.Annotations_enabled
Sundong Ahn054b19a2018-10-19 13:46:09 +09001885 props.Merge_annotations_dirs = module.sdkLibraryProperties.Merge_annotations_dirs
1886 props.Merge_inclusion_annotations_dirs = module.sdkLibraryProperties.Merge_inclusion_annotations_dirs
Jihoon Kang6592e872023-12-19 01:13:16 +00001887 props.Aconfig_declarations = module.sdkLibraryProperties.Aconfig_declarations
Sundong Ahn054b19a2018-10-19 13:46:09 +09001888
Paul Duffin6d0886e2020-04-07 18:49:53 +01001889 droidstubsArgs := []string{}
Paul Duffin235ffff2019-12-24 10:41:30 +00001890 if len(module.sdkLibraryProperties.Api_packages) != 0 {
Paul Duffin6d0886e2020-04-07 18:49:53 +01001891 droidstubsArgs = append(droidstubsArgs, "--stub-packages "+strings.Join(module.sdkLibraryProperties.Api_packages, ":"))
Paul Duffin235ffff2019-12-24 10:41:30 +00001892 }
1893 if len(module.sdkLibraryProperties.Hidden_api_packages) != 0 {
Paul Duffin6d0886e2020-04-07 18:49:53 +01001894 droidstubsArgs = append(droidstubsArgs,
Paul Duffin235ffff2019-12-24 10:41:30 +00001895 android.JoinWithPrefix(module.sdkLibraryProperties.Hidden_api_packages, " --hide-package "))
1896 }
Paul Duffin6d0886e2020-04-07 18:49:53 +01001897 droidstubsArgs = append(droidstubsArgs, module.sdkLibraryProperties.Droiddoc_options...)
Anton Hanssonfd1c0d22023-11-02 15:18:09 +00001898 disabledWarnings := []string{"HiddenSuperclass"}
1899 if proptools.BoolDefault(module.sdkLibraryProperties.Api_lint.Legacy_errors_allowed, true) {
1900 disabledWarnings = append(disabledWarnings,
1901 "BroadcastBehavior",
1902 "DeprecationMismatch",
1903 "MissingPermission",
1904 "SdkConstant",
1905 "Todo",
1906 )
Paul Duffin235ffff2019-12-24 10:41:30 +00001907 }
Paul Duffin6d0886e2020-04-07 18:49:53 +01001908 droidstubsArgs = append(droidstubsArgs, android.JoinWithPrefix(disabledWarnings, "--hide "))
Sundong Ahnfb2721f2018-09-17 13:23:09 +09001909
Paul Duffin6877e6d2020-09-25 19:59:14 +01001910 // Output Javadoc comments for public scope.
1911 if apiScope == apiScopePublic {
1912 props.Output_javadoc_comments = proptools.BoolPtr(true)
1913 }
1914
Paul Duffin1fb487d2020-04-07 18:50:10 +01001915 // Add in scope specific arguments.
Paul Duffin0ff08bd2020-04-29 13:30:54 +01001916 droidstubsArgs = append(droidstubsArgs, scopeSpecificDroidstubsArgs...)
Paul Duffin11512472019-02-11 15:55:17 +00001917 props.Arg_files = module.sdkLibraryProperties.Droiddoc_option_files
Paul Duffin6d0886e2020-04-07 18:49:53 +01001918 props.Args = proptools.StringPtr(strings.Join(droidstubsArgs, " "))
Jiyong Parkc678ad32018-04-10 13:07:10 +09001919
Paul Duffin15f34ef2020-07-20 18:04:44 +01001920 // List of APIs identified from the provided source files are created. They are later
1921 // compared against to the not-yet-released (a.k.a current) list of APIs and to the
1922 // last-released (a.k.a numbered) list of API.
1923 currentApiFileName := apiScope.apiFilePrefix + "current.txt"
1924 removedApiFileName := apiScope.apiFilePrefix + "removed.txt"
1925 apiDir := module.getApiDir()
1926 currentApiFileName = path.Join(apiDir, currentApiFileName)
1927 removedApiFileName = path.Join(apiDir, removedApiFileName)
Jiyong Parkc678ad32018-04-10 13:07:10 +09001928
Paul Duffin15f34ef2020-07-20 18:04:44 +01001929 // check against the not-yet-release API
1930 props.Check_api.Current.Api_file = proptools.StringPtr(currentApiFileName)
1931 props.Check_api.Current.Removed_api_file = proptools.StringPtr(removedApiFileName)
Jiyong Park58c518b2018-05-12 22:29:12 +09001932
Paul Duffin958806b2022-05-16 13:10:47 +00001933 if module.compareAgainstLatestApi(apiScope) {
Paul Duffin15f34ef2020-07-20 18:04:44 +01001934 // check against the latest released API
1935 latestApiFilegroupName := proptools.StringPtr(module.latestApiFilegroupName(apiScope))
Anton Hanssone87b03d2020-12-21 15:29:34 +00001936 props.Previous_api = latestApiFilegroupName
Paul Duffin15f34ef2020-07-20 18:04:44 +01001937 props.Check_api.Last_released.Api_file = latestApiFilegroupName
1938 props.Check_api.Last_released.Removed_api_file = proptools.StringPtr(
1939 module.latestRemovedApiFilegroupName(apiScope))
Jaewoong Jung1a97ee02021-03-09 13:25:02 -08001940 props.Check_api.Last_released.Baseline_file = proptools.StringPtr(
1941 module.latestIncompatibilitiesFilegroupName(apiScope))
Paul Duffin160fe412020-05-10 19:32:20 +01001942
Paul Duffin15f34ef2020-07-20 18:04:44 +01001943 if proptools.Bool(module.sdkLibraryProperties.Api_lint.Enabled) {
1944 // Enable api lint.
1945 props.Check_api.Api_lint.Enabled = proptools.BoolPtr(true)
1946 props.Check_api.Api_lint.New_since = latestApiFilegroupName
Paul Duffin160fe412020-05-10 19:32:20 +01001947
Paul Duffin15f34ef2020-07-20 18:04:44 +01001948 // If it exists then pass a lint-baseline.txt through to droidstubs.
1949 baselinePath := path.Join(apiDir, apiScope.apiFilePrefix+"lint-baseline.txt")
1950 baselinePathRelativeToRoot := path.Join(mctx.ModuleDir(), baselinePath)
1951 paths, err := mctx.GlobWithDeps(baselinePathRelativeToRoot, nil)
1952 if err != nil {
1953 mctx.ModuleErrorf("error checking for presence of %s: %s", baselinePathRelativeToRoot, err)
1954 }
1955 if len(paths) == 1 {
1956 props.Check_api.Api_lint.Baseline_file = proptools.StringPtr(baselinePath)
1957 } else if len(paths) != 0 {
1958 mctx.ModuleErrorf("error checking for presence of %s: expected one path, found: %v", baselinePathRelativeToRoot, paths)
Paul Duffin160fe412020-05-10 19:32:20 +01001959 }
1960 }
Paul Duffin15f34ef2020-07-20 18:04:44 +01001961 }
Jiyong Park58c518b2018-05-12 22:29:12 +09001962
Paul Duffin15f34ef2020-07-20 18:04:44 +01001963 if !Bool(module.sdkLibraryProperties.No_dist) {
Paul Duffin040e9062020-11-23 17:41:36 +00001964 // Dist the api txt and removed api txt artifacts for sdk builds.
1965 distDir := proptools.StringPtr(path.Join(module.apiDistPath(apiScope), "api"))
1966 for _, p := range []struct {
1967 tag string
1968 pattern string
1969 }{
1970 {tag: ".api.txt", pattern: "%s.txt"},
1971 {tag: ".removed-api.txt", pattern: "%s-removed.txt"},
1972 } {
1973 props.Dists = append(props.Dists, android.Dist{
1974 Targets: []string{"sdk", "win_sdk"},
1975 Dir: distDir,
1976 Dest: proptools.StringPtr(fmt.Sprintf(p.pattern, module.distStem())),
1977 Tag: proptools.StringPtr(p.tag),
1978 })
1979 }
Anton Hansson5fd5d242020-03-27 19:43:19 +00001980 }
1981
Spandan Das2cc80ba2023-10-27 17:21:52 +00001982 mctx.CreateModule(DroidstubsFactory, &props, module.sdkComponentPropertiesForChildLibrary()).(*Droidstubs).CallHookIfAvailable(mctx)
Jiyong Parkc678ad32018-04-10 13:07:10 +09001983}
1984
Jihoon Kang0c705a42023-08-02 06:44:57 +00001985func (module *SdkLibrary) createApiLibrary(mctx android.DefaultableHookContext, apiScope *apiScope, alternativeFullApiSurfaceStub string) {
Jihoon Kang1c92c3e2023-03-23 17:44:51 +00001986 props := struct {
Jihoon Kangca198c22023-06-22 23:13:51 +00001987 Name *string
1988 Visibility []string
1989 Api_contributions []string
1990 Libs []string
1991 Static_libs []string
1992 Full_api_surface_stub *string
Jihoon Kang4ec24872023-10-05 17:26:09 +00001993 System_modules *string
Jihoon Kang063ec002023-06-28 01:16:23 +00001994 Enable_validation *bool
Jihoon Kang1c92c3e2023-03-23 17:44:51 +00001995 }{}
1996
1997 props.Name = proptools.StringPtr(module.apiLibraryModuleName(apiScope))
Jihoon Kang786df932023-09-07 01:18:31 +00001998 props.Visibility = []string{"//visibility:override", "//visibility:private"}
Jihoon Kang1c92c3e2023-03-23 17:44:51 +00001999
2000 apiContributions := []string{}
2001
2002 // Api surfaces are not independent of each other, but have subset relationships,
2003 // and so does the api files. To generate from-text stubs for api surfaces other than public,
2004 // all subset api domains' api_contriubtions must be added as well.
2005 scope := apiScope
2006 for scope != nil {
2007 apiContributions = append(apiContributions, module.stubsSourceModuleName(scope)+".api.contribution")
2008 scope = scope.extends
2009 }
Jihoon Kang0c705a42023-08-02 06:44:57 +00002010 if apiScope == apiScopePublic {
2011 additionalApiContribution := module.apiLibraryAdditionalApiContribution()
2012 if additionalApiContribution != "" {
2013 apiContributions = append(apiContributions, additionalApiContribution)
2014 }
2015 }
Jihoon Kang1c92c3e2023-03-23 17:44:51 +00002016
2017 props.Api_contributions = apiContributions
2018 props.Libs = module.properties.Libs
2019 props.Libs = append(props.Libs, module.sdkLibraryProperties.Stub_only_libs...)
Mark White9421c4c2023-08-10 00:07:03 +00002020 props.Libs = append(props.Libs, module.scopeToProperties[apiScope].Libs...)
Jihoon Kang1c92c3e2023-03-23 17:44:51 +00002021 props.Libs = append(props.Libs, "stub-annotations")
2022 props.Static_libs = module.sdkLibraryProperties.Stub_only_static_libs
Jihoon Kange7ee2562023-07-25 05:51:46 +00002023 props.Full_api_surface_stub = proptools.StringPtr(apiScope.kind.DefaultJavaLibraryName())
Jihoon Kang0c705a42023-08-02 06:44:57 +00002024 if alternativeFullApiSurfaceStub != "" {
2025 props.Full_api_surface_stub = proptools.StringPtr(alternativeFullApiSurfaceStub)
2026 }
Jihoon Kang1c92c3e2023-03-23 17:44:51 +00002027
2028 // android_module_lib_stubs_current.from-text only comprises api contributions from art, conscrypt and i18n.
2029 // Thus, replace with android_module_lib_stubs_current_full.from-text, which comprises every api domains.
2030 if apiScope.kind == android.SdkModule {
Jihoon Kangca198c22023-06-22 23:13:51 +00002031 props.Full_api_surface_stub = proptools.StringPtr(apiScope.kind.DefaultJavaLibraryName() + "_full.from-text")
Jihoon Kang1c92c3e2023-03-23 17:44:51 +00002032 }
2033
Jihoon Kangd30ac8a2023-10-09 18:00:17 +00002034 // java_sdk_library modules that set sdk_version as none does not depend on other api
2035 // domains. Therefore, java_api_library created from such modules should not depend on
2036 // full_api_surface_stubs but create and compile stubs by the java_api_library module
2037 // itself.
2038 if module.SdkVersion(mctx).Kind == android.SdkNone {
2039 props.Full_api_surface_stub = nil
2040 }
2041
Jihoon Kang4ec24872023-10-05 17:26:09 +00002042 props.System_modules = module.deviceProperties.System_modules
Jihoon Kang063ec002023-06-28 01:16:23 +00002043 props.Enable_validation = proptools.BoolPtr(true)
Jihoon Kang4ec24872023-10-05 17:26:09 +00002044
Spandan Das2cc80ba2023-10-27 17:21:52 +00002045 mctx.CreateModule(ApiLibraryFactory, &props, module.sdkComponentPropertiesForChildLibrary())
Jihoon Kang1c92c3e2023-03-23 17:44:51 +00002046}
2047
Jihoon Kangfa4a90d2023-12-20 02:53:38 +00002048func (module *SdkLibrary) topLevelStubsLibraryProps(mctx android.DefaultableHookContext, apiScope *apiScope) libraryProperties {
2049 props := libraryProperties{}
2050
Jihoon Kang1147b312023-06-08 23:25:57 +00002051 props.Visibility = childModuleVisibility(module.sdkLibraryProperties.Stubs_library_visibility)
2052 sdkVersion := module.sdkVersionForStubsLibrary(mctx, apiScope)
2053 props.Sdk_version = proptools.StringPtr(sdkVersion)
2054
Jihoon Kang1147b312023-06-08 23:25:57 +00002055 props.System_modules = module.deviceProperties.System_modules
2056
Jihoon Kang1147b312023-06-08 23:25:57 +00002057 // The imports need to be compiled to dex if the java_sdk_library requests it.
2058 compileDex := module.dexProperties.Compile_dex
2059 if module.stubLibrariesCompiledForDex() {
2060 compileDex = proptools.BoolPtr(true)
2061 }
2062 props.Compile_dex = compileDex
2063
Jihoon Kangfa4a90d2023-12-20 02:53:38 +00002064 return props
2065}
2066
2067func (module *SdkLibrary) createTopLevelStubsLibrary(
2068 mctx android.DefaultableHookContext, apiScope *apiScope, contributesToApiSurface bool) {
2069
2070 props := module.topLevelStubsLibraryProps(mctx, apiScope)
2071 props.Name = proptools.StringPtr(module.stubsLibraryModuleName(apiScope))
2072
2073 // Add the stub compiling java_library/java_api_library as static lib based on build config
2074 staticLib := module.sourceStubsLibraryModuleName(apiScope)
2075 if mctx.Config().BuildFromTextStub() && contributesToApiSurface {
2076 staticLib = module.apiLibraryModuleName(apiScope)
2077 }
2078 props.Static_libs = append(props.Static_libs, staticLib)
2079
2080 mctx.CreateModule(LibraryFactory, &props, module.sdkComponentPropertiesForChildLibrary())
2081}
2082
2083func (module *SdkLibrary) createTopLevelExportableStubsLibrary(
2084 mctx android.DefaultableHookContext, apiScope *apiScope) {
2085
2086 props := module.topLevelStubsLibraryProps(mctx, apiScope)
2087 props.Name = proptools.StringPtr(module.exportableStubsLibraryModuleName(apiScope))
2088
2089 // Dist the class jar artifact for sdk builds.
2090 // "exportable" stubs are copied to dist for sdk builds instead of the "everything" stubs.
2091 if !Bool(module.sdkLibraryProperties.No_dist) {
2092 props.Dist.Targets = []string{"sdk", "win_sdk"}
2093 props.Dist.Dest = proptools.StringPtr(fmt.Sprintf("%v.jar", module.distStem()))
2094 props.Dist.Dir = proptools.StringPtr(module.apiDistPath(apiScope))
2095 props.Dist.Tag = proptools.StringPtr(".jar")
2096 }
2097
2098 staticLib := module.exportableSourceStubsLibraryModuleName(apiScope)
2099 props.Static_libs = append(props.Static_libs, staticLib)
2100
Jihoon Kang1147b312023-06-08 23:25:57 +00002101 mctx.CreateModule(LibraryFactory, &props, module.sdkComponentPropertiesForChildLibrary())
2102}
2103
Paul Duffin958806b2022-05-16 13:10:47 +00002104func (module *SdkLibrary) compareAgainstLatestApi(apiScope *apiScope) bool {
2105 return !(apiScope.unstable || module.sdkLibraryProperties.Unsafe_ignore_missing_latest_api)
2106}
2107
Paul Duffinea8f8082021-06-24 13:25:57 +01002108// Implements android.ApexModule
Jooyung Han5e9013b2020-03-10 06:23:13 +09002109func (module *SdkLibrary) DepIsInSameApex(mctx android.BaseModuleContext, dep android.Module) bool {
2110 depTag := mctx.OtherModuleDependencyTag(dep)
2111 if depTag == xmlPermissionsFileTag {
2112 return true
2113 }
2114 return module.Library.DepIsInSameApex(mctx, dep)
2115}
2116
Paul Duffinea8f8082021-06-24 13:25:57 +01002117// Implements android.ApexModule
2118func (module *SdkLibrary) UniqueApexVariations() bool {
2119 return module.uniqueApexVariations()
2120}
2121
Jihoon Kang80456fd2023-11-15 19:22:14 +00002122func (module *SdkLibrary) ContributeToApi() bool {
2123 return proptools.BoolDefault(module.sdkLibraryProperties.Contribute_to_android_api, false)
2124}
2125
Jiyong Parkc678ad32018-04-10 13:07:10 +09002126// Creates the xml file that publicizes the runtime library
Paul Duffinf0229202020-04-29 16:47:28 +01002127func (module *SdkLibrary) createXmlFile(mctx android.DefaultableHookContext) {
Spandan Das8c9ae7e2023-03-03 21:20:36 +00002128 moduleMinApiLevel := module.Library.MinSdkVersion(mctx)
Pedro Loureiroc3621422021-09-28 15:40:23 +00002129 var moduleMinApiLevelStr = moduleMinApiLevel.String()
2130 if moduleMinApiLevel == android.NoneApiLevel {
2131 moduleMinApiLevelStr = "current"
2132 }
Jiyong Parke3833882020-02-17 17:28:10 +09002133 props := struct {
Pedro Loureiroc3621422021-09-28 15:40:23 +00002134 Name *string
2135 Lib_name *string
2136 Apex_available []string
2137 On_bootclasspath_since *string
2138 On_bootclasspath_before *string
2139 Min_device_sdk *string
2140 Max_device_sdk *string
2141 Sdk_library_min_api_level *string
Jamie Garsidee570ace2023-11-27 12:07:36 +00002142 Uses_libs_dependencies []string
Jiyong Parke3833882020-02-17 17:28:10 +09002143 }{
Pedro Loureiroc3621422021-09-28 15:40:23 +00002144 Name: proptools.StringPtr(module.xmlPermissionsModuleName()),
2145 Lib_name: proptools.StringPtr(module.BaseModuleName()),
2146 Apex_available: module.ApexProperties.Apex_available,
2147 On_bootclasspath_since: module.commonSdkLibraryProperties.On_bootclasspath_since,
2148 On_bootclasspath_before: module.commonSdkLibraryProperties.On_bootclasspath_before,
2149 Min_device_sdk: module.commonSdkLibraryProperties.Min_device_sdk,
2150 Max_device_sdk: module.commonSdkLibraryProperties.Max_device_sdk,
2151 Sdk_library_min_api_level: &moduleMinApiLevelStr,
Jamie Garsidee570ace2023-11-27 12:07:36 +00002152 Uses_libs_dependencies: module.usesLibraryProperties.Uses_libs,
Jiyong Parkc678ad32018-04-10 13:07:10 +09002153 }
Jiyong Parke3833882020-02-17 17:28:10 +09002154
Jiyong Parke3833882020-02-17 17:28:10 +09002155 mctx.CreateModule(sdkLibraryXmlFactory, &props)
Jiyong Parkc678ad32018-04-10 13:07:10 +09002156}
2157
Jiyong Parkf1691d22021-03-29 20:11:58 +09002158func PrebuiltJars(ctx android.BaseModuleContext, baseName string, s android.SdkSpec) android.Paths {
Jiyong Park54105c42021-03-31 18:17:53 +09002159 var ver android.ApiLevel
Jiyong Parkf1691d22021-03-29 20:11:58 +09002160 var kind android.SdkKind
2161 if s.UsePrebuilt(ctx) {
Jiyong Park54105c42021-03-31 18:17:53 +09002162 ver = s.ApiLevel
Jiyong Parkf1691d22021-03-29 20:11:58 +09002163 kind = s.Kind
Jiyong Parkc678ad32018-04-10 13:07:10 +09002164 } else {
Jiyong Park6a927c42020-01-21 02:03:43 +09002165 // We don't have prebuilt SDK for the specific sdkVersion.
2166 // Instead of breaking the build, fallback to use "system_current"
Jiyong Park54105c42021-03-31 18:17:53 +09002167 ver = android.FutureApiLevel
Jiyong Parkf1691d22021-03-29 20:11:58 +09002168 kind = android.SdkSystem
Sundong Ahn054b19a2018-10-19 13:46:09 +09002169 }
Jiyong Park6a927c42020-01-21 02:03:43 +09002170
2171 dir := filepath.Join("prebuilts", "sdk", ver.String(), kind.String())
Paul Duffin50061512020-01-21 16:31:05 +00002172 jar := filepath.Join(dir, baseName+".jar")
Sundong Ahn054b19a2018-10-19 13:46:09 +09002173 jarPath := android.ExistentPathForSource(ctx, jar)
Sundong Ahnae418ac2019-02-28 15:01:28 +09002174 if !jarPath.Valid() {
Colin Cross07c88562020-01-07 09:34:44 -08002175 if ctx.Config().AllowMissingDependencies() {
2176 return android.Paths{android.PathForSource(ctx, jar)}
2177 } else {
Jiyong Parkf1691d22021-03-29 20:11:58 +09002178 ctx.PropertyErrorf("sdk_library", "invalid sdk version %q, %q does not exist", s.Raw, jar)
Colin Cross07c88562020-01-07 09:34:44 -08002179 }
Sundong Ahnae418ac2019-02-28 15:01:28 +09002180 return nil
2181 }
Sundong Ahn054b19a2018-10-19 13:46:09 +09002182 return android.Paths{jarPath.Path()}
2183}
2184
Colin Crossaede88c2020-08-11 12:17:01 -07002185// 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 +01002186//
2187// If either this or the other module are on the platform then this will return
2188// false.
Colin Cross56a83212020-09-15 18:30:11 -07002189func withinSameApexesAs(ctx android.BaseModuleContext, other android.Module) bool {
Colin Crossff694a82023-12-13 15:54:49 -08002190 apexInfo, _ := android.ModuleProvider(ctx, android.ApexInfoProvider)
Colin Cross313aa542023-12-13 13:47:44 -08002191 otherApexInfo, _ := android.OtherModuleProvider(ctx, other, android.ApexInfoProvider)
Jiyong Parkab50b072021-05-12 17:13:56 +09002192 return len(otherApexInfo.InApexVariants) > 0 && reflect.DeepEqual(apexInfo.InApexVariants, otherApexInfo.InApexVariants)
Paul Duffin9b879592020-05-26 13:21:35 +01002193}
2194
Jiyong Parkf1691d22021-03-29 20:11:58 +09002195func (module *SdkLibrary) sdkJars(ctx android.BaseModuleContext, sdkVersion android.SdkSpec, headerJars bool) android.Paths {
Jiyong Park932cdfe2020-05-28 00:19:53 +09002196 // If the client doesn't set sdk_version, but if this library prefers stubs over
2197 // the impl library, let's provide the widest API surface possible. To do so,
2198 // force override sdk_version to module_current so that the closest possible API
2199 // surface could be found in selectHeaderJarsForSdkVersion
Jiyong Parkf1691d22021-03-29 20:11:58 +09002200 if module.defaultsToStubs() && !sdkVersion.Specified() {
Jiyong Park92315372021-04-02 08:45:46 +09002201 sdkVersion = android.SdkSpecFrom(ctx, "module_current")
Jiyong Park932cdfe2020-05-28 00:19:53 +09002202 }
Paul Duffind1b3a922020-01-22 11:57:20 +00002203
Paul Duffindaaa3322020-05-26 18:13:57 +01002204 // Only provide access to the implementation library if it is actually built.
2205 if module.requiresRuntimeImplementationLibrary() {
2206 // Check any special cases for java_sdk_library.
2207 //
2208 // Only allow access to the implementation library in the following condition:
2209 // * No sdk_version specified on the referencing module.
Paul Duffin9b879592020-05-26 13:21:35 +01002210 // * The referencing module is in the same apex as this.
Jiyong Parkf1691d22021-03-29 20:11:58 +09002211 if sdkVersion.Kind == android.SdkPrivate || withinSameApexesAs(ctx, module) {
Paul Duffindaaa3322020-05-26 18:13:57 +01002212 if headerJars {
2213 return module.HeaderJars()
2214 } else {
2215 return module.ImplementationJars()
2216 }
Sundong Ahn054b19a2018-10-19 13:46:09 +09002217 }
Jiyong Parkc678ad32018-04-10 13:07:10 +09002218 }
Paul Duffinb05d4292020-05-20 12:19:10 +01002219
Paul Duffin23970f42020-05-20 14:20:02 +01002220 return module.selectHeaderJarsForSdkVersion(ctx, sdkVersion)
Jiyong Parkc678ad32018-04-10 13:07:10 +09002221}
2222
Sundong Ahn241cd372018-07-13 16:16:44 +09002223// to satisfy SdkLibraryDependency interface
Jiyong Parkf1691d22021-03-29 20:11:58 +09002224func (module *SdkLibrary) SdkHeaderJars(ctx android.BaseModuleContext, sdkVersion android.SdkSpec) android.Paths {
Paul Duffind1b3a922020-01-22 11:57:20 +00002225 return module.sdkJars(ctx, sdkVersion, true /*headerJars*/)
2226}
2227
2228// to satisfy SdkLibraryDependency interface
Jiyong Parkf1691d22021-03-29 20:11:58 +09002229func (module *SdkLibrary) SdkImplementationJars(ctx android.BaseModuleContext, sdkVersion android.SdkSpec) android.Paths {
Paul Duffind1b3a922020-01-22 11:57:20 +00002230 return module.sdkJars(ctx, sdkVersion, false /*headerJars*/)
Sundong Ahn241cd372018-07-13 16:16:44 +09002231}
2232
Colin Cross571cccf2019-02-04 11:22:08 -08002233var javaSdkLibrariesKey = android.NewOnceKey("javaSdkLibraries")
2234
Jiyong Park82484c02018-04-23 21:41:26 +09002235func javaSdkLibraries(config android.Config) *[]string {
Colin Cross571cccf2019-02-04 11:22:08 -08002236 return config.Once(javaSdkLibrariesKey, func() interface{} {
Jiyong Park82484c02018-04-23 21:41:26 +09002237 return &[]string{}
2238 }).(*[]string)
2239}
2240
Paul Duffin749f98f2019-12-30 17:23:46 +00002241func (module *SdkLibrary) getApiDir() string {
2242 return proptools.StringDefault(module.sdkLibraryProperties.Api_dir, "api")
2243}
2244
Jiyong Parkc678ad32018-04-10 13:07:10 +09002245// For a java_sdk_library module, create internal modules for stubs, docs,
2246// runtime libs and xml file. If requested, the stubs and docs are created twice
2247// once for public API level and once for system API level
Paul Duffinf0229202020-04-29 16:47:28 +01002248func (module *SdkLibrary) CreateInternalModules(mctx android.DefaultableHookContext) {
2249 // If the module has been disabled then don't create any child modules.
2250 if !module.Enabled() {
2251 return
2252 }
2253
Paul Duffina18abc22020-05-16 18:54:24 +01002254 if len(module.properties.Srcs) == 0 {
Inseob Kimc0907f12019-02-08 21:00:45 +09002255 mctx.PropertyErrorf("srcs", "java_sdk_library must specify srcs")
Jooyung Han58f26ab2019-12-18 15:34:32 +09002256 return
Inseob Kimc0907f12019-02-08 21:00:45 +09002257 }
2258
Paul Duffin37e0b772019-12-30 17:20:10 +00002259 // If this builds against standard libraries (i.e. is not part of the core libraries)
Paul Duffin4f5c1ef2020-11-19 14:53:43 +00002260 // then assume it provides both system and test apis.
Jiyong Parkf1691d22021-03-29 20:11:58 +09002261 sdkDep := decodeSdkDep(mctx, android.SdkContext(&module.Library))
Paul Duffin37e0b772019-12-30 17:20:10 +00002262 hasSystemAndTestApis := sdkDep.hasStandardLibs()
Paul Duffin3375e352020-04-28 10:44:03 +01002263 module.sdkLibraryProperties.Generate_system_and_test_apis = hasSystemAndTestApis
Paul Duffin4f5c1ef2020-11-19 14:53:43 +00002264
Jaewoong Jung18aefc12020-12-21 09:11:10 -08002265 missingCurrentApi := false
Inseob Kim8098faa2019-03-18 10:19:51 +09002266
Paul Duffin3375e352020-04-28 10:44:03 +01002267 generatedScopes := module.getGeneratedApiScopes(mctx)
Paul Duffind1b3a922020-01-22 11:57:20 +00002268
Paul Duffin749f98f2019-12-30 17:23:46 +00002269 apiDir := module.getApiDir()
Paul Duffin3375e352020-04-28 10:44:03 +01002270 for _, scope := range generatedScopes {
Inseob Kim8098faa2019-03-18 10:19:51 +09002271 for _, api := range []string{"current.txt", "removed.txt"} {
Paul Duffind1b3a922020-01-22 11:57:20 +00002272 path := path.Join(mctx.ModuleDir(), apiDir, scope.apiFilePrefix+api)
Inseob Kim8098faa2019-03-18 10:19:51 +09002273 p := android.ExistentPathForSource(mctx, path)
2274 if !p.Valid() {
Colin Cross18f840c2021-05-20 17:56:54 -07002275 if mctx.Config().AllowMissingDependencies() {
2276 mctx.AddMissingDependencies([]string{path})
2277 } else {
2278 mctx.ModuleErrorf("Current api file %#v doesn't exist", path)
2279 missingCurrentApi = true
2280 }
Inseob Kim8098faa2019-03-18 10:19:51 +09002281 }
2282 }
2283 }
2284
Jaewoong Jung18aefc12020-12-21 09:11:10 -08002285 if missingCurrentApi {
Inseob Kim8098faa2019-03-18 10:19:51 +09002286 script := "build/soong/scripts/gen-java-current-api-files.sh"
2287 p := android.ExistentPathForSource(mctx, script)
2288
2289 if !p.Valid() {
2290 panic(fmt.Sprintf("script file %s doesn't exist", script))
2291 }
2292
2293 mctx.ModuleErrorf("One or more current api files are missing. "+
2294 "You can update them by:\n"+
Paul Duffin37e0b772019-12-30 17:20:10 +00002295 "%s %q %s && m update-api",
Paul Duffind1b3a922020-01-22 11:57:20 +00002296 script, filepath.Join(mctx.ModuleDir(), apiDir),
Paul Duffin3375e352020-04-28 10:44:03 +01002297 strings.Join(generatedScopes.Strings(func(s *apiScope) string { return s.apiFilePrefix }), " "))
Inseob Kim8098faa2019-03-18 10:19:51 +09002298 return
2299 }
2300
Paul Duffin3375e352020-04-28 10:44:03 +01002301 for _, scope := range generatedScopes {
Paul Duffin15f34ef2020-07-20 18:04:44 +01002302 // Use the stubs source name for legacy reasons.
2303 module.createStubsSourcesAndApi(mctx, scope, module.stubsSourceModuleName(scope), scope.droidstubsArgs)
Paul Duffin0ff08bd2020-04-29 13:30:54 +01002304
Paul Duffind1b3a922020-01-22 11:57:20 +00002305 module.createStubsLibrary(mctx, scope)
Jihoon Kangfa4a90d2023-12-20 02:53:38 +00002306 module.createExportableStubsLibrary(mctx, scope)
Jihoon Kang1c92c3e2023-03-23 17:44:51 +00002307
Jihoon Kang0c705a42023-08-02 06:44:57 +00002308 alternativeFullApiSurfaceStubLib := ""
2309 if scope == apiScopePublic {
2310 alternativeFullApiSurfaceStubLib = module.alternativeFullApiSurfaceStubLib()
2311 }
2312 contributesToApiSurface := module.contributesToApiSurface(mctx.Config()) || alternativeFullApiSurfaceStubLib != ""
Jihoon Kang1147b312023-06-08 23:25:57 +00002313 if contributesToApiSurface {
Jihoon Kang0c705a42023-08-02 06:44:57 +00002314 module.createApiLibrary(mctx, scope, alternativeFullApiSurfaceStubLib)
Jihoon Kang1c92c3e2023-03-23 17:44:51 +00002315 }
Jihoon Kang1147b312023-06-08 23:25:57 +00002316
2317 module.createTopLevelStubsLibrary(mctx, scope, contributesToApiSurface)
Jihoon Kangfa4a90d2023-12-20 02:53:38 +00002318 module.createTopLevelExportableStubsLibrary(mctx, scope)
Inseob Kimc0907f12019-02-08 21:00:45 +09002319 }
2320
Paul Duffindfa131e2020-05-15 20:37:11 +01002321 if module.requiresRuntimeImplementationLibrary() {
Paul Duffin5df79302020-05-16 15:52:12 +01002322 // Create child module to create an implementation library.
2323 //
2324 // This temporarily creates a second implementation library that can be explicitly
2325 // referenced.
2326 //
2327 // TODO(b/156618935) - update comment once only one implementation library is created.
2328 module.createImplLibrary(mctx)
2329
Paul Duffindfa131e2020-05-15 20:37:11 +01002330 // Only create an XML permissions file that declares the library as being usable
2331 // as a shared library if required.
2332 if module.sharedLibrary() {
2333 module.createXmlFile(mctx)
2334 }
Paul Duffin43db9be2019-12-30 17:35:49 +00002335
2336 // record java_sdk_library modules so that they are exported to make
2337 javaSdkLibraries := javaSdkLibraries(mctx.Config())
2338 javaSdkLibrariesLock.Lock()
2339 defer javaSdkLibrariesLock.Unlock()
2340 *javaSdkLibraries = append(*javaSdkLibraries, module.BaseModuleName())
2341 }
Anton Hansson7f66efa2020-10-08 14:47:23 +01002342
Paul Duffin77590a82022-04-28 14:13:30 +00002343 // 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 +01002344 module.properties.Libs = append(module.properties.Libs, module.sdkLibraryProperties.Impl_only_libs...)
Paul Duffin77590a82022-04-28 14:13:30 +00002345 module.properties.Static_libs = append(module.properties.Static_libs, module.sdkLibraryProperties.Impl_only_static_libs...)
Inseob Kimc0907f12019-02-08 21:00:45 +09002346}
2347
2348func (module *SdkLibrary) InitSdkLibraryProperties() {
Colin Crossce6734e2020-06-15 16:09:53 -07002349 module.addHostAndDeviceProperties()
2350 module.AddProperties(&module.sdkLibraryProperties)
Sundong Ahn054b19a2018-10-19 13:46:09 +09002351
Paul Duffin71b33cc2021-06-23 11:39:47 +01002352 module.initSdkLibraryComponent(module)
Paul Duffin859fe962020-05-15 10:20:31 +01002353
Paul Duffina18abc22020-05-16 18:54:24 +01002354 module.properties.Installable = proptools.BoolPtr(true)
2355 module.deviceProperties.IsSDKLibrary = true
Inseob Kimc0907f12019-02-08 21:00:45 +09002356}
Sundong Ahn054b19a2018-10-19 13:46:09 +09002357
Paul Duffindfa131e2020-05-15 20:37:11 +01002358func (module *SdkLibrary) requiresRuntimeImplementationLibrary() bool {
2359 return !proptools.Bool(module.sdkLibraryProperties.Api_only)
2360}
2361
Jiyong Park932cdfe2020-05-28 00:19:53 +09002362func (module *SdkLibrary) defaultsToStubs() bool {
2363 return proptools.Bool(module.sdkLibraryProperties.Default_to_stubs)
2364}
2365
Paul Duffin1b1e8062020-05-08 13:44:43 +01002366// Defines how to name the individual component modules the sdk library creates.
2367type sdkLibraryComponentNamingScheme interface {
2368 stubsLibraryModuleName(scope *apiScope, baseName string) string
2369
2370 stubsSourceModuleName(scope *apiScope, baseName string) string
Jihoon Kang1c92c3e2023-03-23 17:44:51 +00002371
2372 apiLibraryModuleName(scope *apiScope, baseName string) string
Jihoon Kang1147b312023-06-08 23:25:57 +00002373
Jihoon Kangfa4a90d2023-12-20 02:53:38 +00002374 sourceStubsLibraryModuleName(scope *apiScope, baseName string) string
2375
2376 exportableStubsLibraryModuleName(scope *apiScope, baseName string) string
2377
2378 exportableSourceStubsLibraryModuleName(scope *apiScope, baseName string) string
Paul Duffin1b1e8062020-05-08 13:44:43 +01002379}
2380
2381type defaultNamingScheme struct {
2382}
2383
2384func (s *defaultNamingScheme) stubsLibraryModuleName(scope *apiScope, baseName string) string {
2385 return scope.stubsLibraryModuleName(baseName)
2386}
2387
2388func (s *defaultNamingScheme) stubsSourceModuleName(scope *apiScope, baseName string) string {
2389 return scope.stubsSourceModuleName(baseName)
2390}
2391
Jihoon Kang1c92c3e2023-03-23 17:44:51 +00002392func (s *defaultNamingScheme) apiLibraryModuleName(scope *apiScope, baseName string) string {
2393 return scope.apiLibraryModuleName(baseName)
2394}
2395
Jihoon Kangfa4a90d2023-12-20 02:53:38 +00002396func (s *defaultNamingScheme) sourceStubsLibraryModuleName(scope *apiScope, baseName string) string {
Jihoon Kang1147b312023-06-08 23:25:57 +00002397 return scope.sourceStubLibraryModuleName(baseName)
2398}
2399
Jihoon Kangfa4a90d2023-12-20 02:53:38 +00002400func (s *defaultNamingScheme) exportableStubsLibraryModuleName(scope *apiScope, baseName string) string {
2401 return scope.exportableStubsLibraryModuleName(baseName)
2402}
2403
2404func (s *defaultNamingScheme) exportableSourceStubsLibraryModuleName(scope *apiScope, baseName string) string {
2405 return scope.exportableSourceStubsLibraryModuleName(baseName)
2406}
2407
Paul Duffin1b1e8062020-05-08 13:44:43 +01002408var _ sdkLibraryComponentNamingScheme = (*defaultNamingScheme)(nil)
2409
Jihoon Kangfa4a90d2023-12-20 02:53:38 +00002410func hasStubsLibrarySuffix(name string, apiScope *apiScope) bool {
2411 return strings.HasSuffix(name, apiScope.stubsLibraryModuleNameSuffix()) ||
2412 strings.HasSuffix(name, apiScope.exportableStubsLibraryModuleNameSuffix())
2413}
2414
Jaewoong Jungbc15e3a2021-03-10 17:02:43 -08002415func moduleStubLinkType(name string) (stub bool, ret sdkLinkType) {
Jihoon Kang1147b312023-06-08 23:25:57 +00002416 name = strings.TrimSuffix(name, ".from-source")
2417
Anton Hansson2d0c1942020-05-25 12:20:51 +01002418 // This suffix-based approach is fragile and could potentially mis-trigger.
2419 // TODO(b/155164730): Clean this up when modules no longer reference sdk_lib stubs directly.
Jihoon Kangfa4a90d2023-12-20 02:53:38 +00002420 if hasStubsLibrarySuffix(name, apiScopePublic) {
Anton Hansson08f476b2021-04-07 15:32:19 +01002421 if name == "hwbinder.stubs" || name == "libcore_private.stubs" {
2422 // Due to a previous bug, these modules were not considered stubs, so we retain that.
2423 return false, javaPlatform
2424 }
Anton Hansson2d0c1942020-05-25 12:20:51 +01002425 return true, javaSdk
2426 }
Jihoon Kangfa4a90d2023-12-20 02:53:38 +00002427 if hasStubsLibrarySuffix(name, apiScopeSystem) {
Anton Hansson2d0c1942020-05-25 12:20:51 +01002428 return true, javaSystem
2429 }
Jihoon Kangfa4a90d2023-12-20 02:53:38 +00002430 if hasStubsLibrarySuffix(name, apiScopeModuleLib) {
Anton Hansson2d0c1942020-05-25 12:20:51 +01002431 return true, javaModule
2432 }
Jihoon Kangfa4a90d2023-12-20 02:53:38 +00002433 if hasStubsLibrarySuffix(name, apiScopeTest) {
Anton Hansson2d0c1942020-05-25 12:20:51 +01002434 return true, javaSystem
2435 }
Jihoon Kangfa4a90d2023-12-20 02:53:38 +00002436 if hasStubsLibrarySuffix(name, apiScopeSystemServer) {
Jihoon Kang1147b312023-06-08 23:25:57 +00002437 return true, javaSystemServer
2438 }
Anton Hansson2d0c1942020-05-25 12:20:51 +01002439 return false, javaPlatform
2440}
2441
Jaewoong Jung4f158ee2019-07-11 10:05:35 -07002442// java_sdk_library is a special Java library that provides optional platform APIs to apps.
2443// In practice, it can be viewed as a combination of several modules: 1) stubs library that clients
2444// are linked against to, 2) droiddoc module that internally generates API stubs source files,
2445// 3) the real runtime shared library that implements the APIs, and 4) XML file for adding
2446// the runtime lib to the classpath at runtime if requested via <uses-library>.
Inseob Kimc0907f12019-02-08 21:00:45 +09002447func SdkLibraryFactory() android.Module {
2448 module := &SdkLibrary{}
Paul Duffinc3091c82020-05-08 14:16:20 +01002449
2450 // Initialize information common between source and prebuilt.
Paul Duffin71b33cc2021-06-23 11:39:47 +01002451 module.initCommon(module)
Paul Duffinc3091c82020-05-08 14:16:20 +01002452
Inseob Kimc0907f12019-02-08 21:00:45 +09002453 module.InitSdkLibraryProperties()
Jooyung Han58f26ab2019-12-18 15:34:32 +09002454 android.InitApexModule(module)
Sundong Ahn054b19a2018-10-19 13:46:09 +09002455 InitJavaModule(module, android.HostAndDeviceSupported)
Paul Duffin3375e352020-04-28 10:44:03 +01002456
2457 // Initialize the map from scope to scope specific properties.
2458 scopeToProperties := make(map[*apiScope]*ApiScopeProperties)
2459 for _, scope := range allApiScopes {
2460 scopeToProperties[scope] = scope.scopeSpecificProperties(module)
2461 }
2462 module.scopeToProperties = scopeToProperties
2463
Paul Duffin4911a892020-04-29 23:35:13 +01002464 // Add the properties containing visibility rules so that they are checked.
Paul Duffin5df79302020-05-16 15:52:12 +01002465 android.AddVisibilityProperty(module, "impl_library_visibility", &module.sdkLibraryProperties.Impl_library_visibility)
Paul Duffin4911a892020-04-29 23:35:13 +01002466 android.AddVisibilityProperty(module, "stubs_library_visibility", &module.sdkLibraryProperties.Stubs_library_visibility)
2467 android.AddVisibilityProperty(module, "stubs_source_visibility", &module.sdkLibraryProperties.Stubs_source_visibility)
2468
Paul Duffin1b1e8062020-05-08 13:44:43 +01002469 module.SetDefaultableHook(func(ctx android.DefaultableHookContext) {
Paul Duffindfa131e2020-05-15 20:37:11 +01002470 // If no implementation is required then it cannot be used as a shared library
2471 // either.
2472 if !module.requiresRuntimeImplementationLibrary() {
2473 // If shared_library has been explicitly set to true then it is incompatible
2474 // with api_only: true.
2475 if proptools.Bool(module.commonSdkLibraryProperties.Shared_library) {
2476 ctx.PropertyErrorf("api_only/shared_library", "inconsistent settings, shared_library and api_only cannot both be true")
2477 }
2478 // Set shared_library: false.
2479 module.commonSdkLibraryProperties.Shared_library = proptools.BoolPtr(false)
2480 }
2481
Paul Duffin1b1e8062020-05-08 13:44:43 +01002482 if module.initCommonAfterDefaultsApplied(ctx) {
2483 module.CreateInternalModules(ctx)
2484 }
2485 })
Jiyong Parkc678ad32018-04-10 13:07:10 +09002486 return module
2487}
Colin Cross79c7c262019-04-17 11:11:46 -07002488
2489//
2490// SDK library prebuilts
2491//
2492
Paul Duffin56d44902020-01-31 13:36:25 +00002493// Properties associated with each api scope.
2494type sdkLibraryScopeProperties struct {
Colin Cross79c7c262019-04-17 11:11:46 -07002495 Jars []string `android:"path"`
2496
2497 Sdk_version *string
2498
Colin Cross79c7c262019-04-17 11:11:46 -07002499 // List of shared java libs that this module has dependencies to
2500 Libs []string
Paul Duffin3d1248c2020-04-09 00:10:17 +01002501
Paul Duffinc8782502020-04-29 20:45:27 +01002502 // The stubs source.
Paul Duffin3d1248c2020-04-09 00:10:17 +01002503 Stub_srcs []string `android:"path"`
Paul Duffin1fd005d2020-04-09 01:08:11 +01002504
2505 // The current.txt
Paul Duffin0f8faff2020-05-20 16:18:00 +01002506 Current_api *string `android:"path"`
Paul Duffin1fd005d2020-04-09 01:08:11 +01002507
2508 // The removed.txt
Paul Duffin0f8faff2020-05-20 16:18:00 +01002509 Removed_api *string `android:"path"`
Anton Hanssond78eb762021-09-21 15:25:12 +01002510
2511 // Annotation zip
2512 Annotations *string `android:"path"`
Colin Cross79c7c262019-04-17 11:11:46 -07002513}
2514
Paul Duffin56d44902020-01-31 13:36:25 +00002515type sdkLibraryImportProperties struct {
Paul Duffinfcfd7912020-01-31 17:54:30 +00002516 // List of shared java libs, common to all scopes, that this module has
2517 // dependencies to
2518 Libs []string
Paul Duffin1267d872021-04-16 17:21:36 +01002519
2520 // If set to true, compile dex files for the stubs. Defaults to false.
2521 Compile_dex *bool
Paul Duffin869de142021-07-15 14:14:41 +01002522
2523 // If not empty, classes are restricted to the specified packages and their sub-packages.
Paul Duffin869de142021-07-15 14:14:41 +01002524 Permitted_packages []string
Paul Duffin56d44902020-01-31 13:36:25 +00002525}
2526
Paul Duffineedc5d52020-06-12 17:46:39 +01002527type SdkLibraryImport struct {
Colin Cross79c7c262019-04-17 11:11:46 -07002528 android.ModuleBase
2529 android.DefaultableModuleBase
2530 prebuilt android.Prebuilt
Paul Duffindd46f712020-02-10 13:37:10 +00002531 android.ApexModuleBase
Colin Cross79c7c262019-04-17 11:11:46 -07002532
Paul Duffin37856732021-02-26 14:24:15 +00002533 hiddenAPI
Jiakai Zhang204356f2021-09-09 08:12:46 +00002534 dexpreopter
Paul Duffin37856732021-02-26 14:24:15 +00002535
Colin Cross79c7c262019-04-17 11:11:46 -07002536 properties sdkLibraryImportProperties
2537
Paul Duffin46a26a82020-04-07 19:27:04 +01002538 // Map from api scope to the scope specific property structure.
2539 scopeProperties map[*apiScope]*sdkLibraryScopeProperties
2540
Paul Duffin56d44902020-01-31 13:36:25 +00002541 commonToSdkLibraryAndImport
Paul Duffineedc5d52020-06-12 17:46:39 +01002542
2543 // The reference to the implementation library created by the source module.
2544 // Is nil if the source module does not exist.
2545 implLibraryModule *Library
2546
2547 // The reference to the xml permissions module created by the source module.
2548 // Is nil if the source module does not exist.
2549 xmlPermissionsFileModule *sdkLibraryXml
Paul Duffin39853512021-02-26 11:09:39 +00002550
Jeongik Chad5fe8782021-07-08 01:13:11 +09002551 // Build path to the dex implementation jar obtained from the prebuilt_apex, if any.
Spandan Dasfae468e2023-12-12 23:23:53 +00002552 dexJarFile OptionalDexJarPath
2553 dexJarFileErr error
Jeongik Chad5fe8782021-07-08 01:13:11 +09002554
2555 // Expected install file path of the source module(sdk_library)
2556 // or dex implementation jar obtained from the prebuilt_apex, if any.
2557 installFile android.Path
Colin Cross79c7c262019-04-17 11:11:46 -07002558}
2559
Paul Duffineedc5d52020-06-12 17:46:39 +01002560var _ SdkLibraryDependency = (*SdkLibraryImport)(nil)
Colin Cross79c7c262019-04-17 11:11:46 -07002561
Paul Duffin46a26a82020-04-07 19:27:04 +01002562// The type of a structure that contains a field of type sdkLibraryScopeProperties
2563// for each apiscope in allApiScopes, e.g. something like:
Colin Crossd079e0b2022-08-16 10:27:33 -07002564//
2565// struct {
2566// Public sdkLibraryScopeProperties
2567// System sdkLibraryScopeProperties
2568// ...
2569// }
Paul Duffin46a26a82020-04-07 19:27:04 +01002570var allScopeStructType = createAllScopePropertiesStructType()
2571
2572// Dynamically create a structure type for each apiscope in allApiScopes.
2573func createAllScopePropertiesStructType() reflect.Type {
2574 var fields []reflect.StructField
2575 for _, apiScope := range allApiScopes {
2576 field := reflect.StructField{
2577 Name: apiScope.fieldName,
2578 Type: reflect.TypeOf(sdkLibraryScopeProperties{}),
2579 }
2580 fields = append(fields, field)
2581 }
2582
2583 return reflect.StructOf(fields)
2584}
2585
2586// Create an instance of the scope specific structure type and return a map
2587// from apiscope to a pointer to each scope specific field.
2588func createPropertiesInstance() (interface{}, map[*apiScope]*sdkLibraryScopeProperties) {
2589 allScopePropertiesPtr := reflect.New(allScopeStructType)
2590 allScopePropertiesStruct := allScopePropertiesPtr.Elem()
2591 scopeProperties := make(map[*apiScope]*sdkLibraryScopeProperties)
2592
2593 for _, apiScope := range allApiScopes {
2594 field := allScopePropertiesStruct.FieldByName(apiScope.fieldName)
2595 scopeProperties[apiScope] = field.Addr().Interface().(*sdkLibraryScopeProperties)
2596 }
2597
2598 return allScopePropertiesPtr.Interface(), scopeProperties
2599}
2600
Jaewoong Jung4f158ee2019-07-11 10:05:35 -07002601// java_sdk_library_import imports a prebuilt java_sdk_library.
Colin Cross79c7c262019-04-17 11:11:46 -07002602func sdkLibraryImportFactory() android.Module {
Paul Duffineedc5d52020-06-12 17:46:39 +01002603 module := &SdkLibraryImport{}
Colin Cross79c7c262019-04-17 11:11:46 -07002604
Paul Duffin46a26a82020-04-07 19:27:04 +01002605 allScopeProperties, scopeToProperties := createPropertiesInstance()
2606 module.scopeProperties = scopeToProperties
Jiakai Zhang9c4dc192023-02-09 00:09:24 +08002607 module.AddProperties(&module.properties, allScopeProperties, &module.importDexpreoptProperties)
Colin Cross79c7c262019-04-17 11:11:46 -07002608
Paul Duffinc3091c82020-05-08 14:16:20 +01002609 // Initialize information common between source and prebuilt.
Paul Duffin71b33cc2021-06-23 11:39:47 +01002610 module.initCommon(module)
Paul Duffinc3091c82020-05-08 14:16:20 +01002611
Paul Duffin0bdcb272020-02-06 15:24:57 +00002612 android.InitPrebuiltModule(module, &[]string{""})
Paul Duffindd46f712020-02-10 13:37:10 +00002613 android.InitApexModule(module)
Colin Cross79c7c262019-04-17 11:11:46 -07002614 InitJavaModule(module, android.HostAndDeviceSupported)
2615
Paul Duffin1b1e8062020-05-08 13:44:43 +01002616 module.SetDefaultableHook(func(mctx android.DefaultableHookContext) {
2617 if module.initCommonAfterDefaultsApplied(mctx) {
2618 module.createInternalModules(mctx)
2619 }
2620 })
Colin Cross79c7c262019-04-17 11:11:46 -07002621 return module
2622}
2623
Paul Duffin630b11e2021-07-15 13:35:26 +01002624var _ PermittedPackagesForUpdatableBootJars = (*SdkLibraryImport)(nil)
2625
2626func (module *SdkLibraryImport) PermittedPackagesForUpdatableBootJars() []string {
2627 return module.properties.Permitted_packages
2628}
2629
Paul Duffineedc5d52020-06-12 17:46:39 +01002630func (module *SdkLibraryImport) Prebuilt() *android.Prebuilt {
Colin Cross79c7c262019-04-17 11:11:46 -07002631 return &module.prebuilt
2632}
2633
Paul Duffineedc5d52020-06-12 17:46:39 +01002634func (module *SdkLibraryImport) Name() string {
Colin Cross79c7c262019-04-17 11:11:46 -07002635 return module.prebuilt.Name(module.ModuleBase.Name())
2636}
2637
Paul Duffineedc5d52020-06-12 17:46:39 +01002638func (module *SdkLibraryImport) createInternalModules(mctx android.DefaultableHookContext) {
Colin Cross79c7c262019-04-17 11:11:46 -07002639
Paul Duffin50061512020-01-21 16:31:05 +00002640 // If the build is configured to use prebuilts then force this to be preferred.
Jeongik Cha816a23a2020-07-08 01:09:23 +09002641 if mctx.Config().AlwaysUsePrebuiltSdks() {
Paul Duffin50061512020-01-21 16:31:05 +00002642 module.prebuilt.ForcePrefer()
2643 }
2644
Paul Duffin46a26a82020-04-07 19:27:04 +01002645 for apiScope, scopeProperties := range module.scopeProperties {
Paul Duffin56d44902020-01-31 13:36:25 +00002646 if len(scopeProperties.Jars) == 0 {
2647 continue
2648 }
2649
Paul Duffinbbb546b2020-04-09 00:07:11 +01002650 module.createJavaImportForStubs(mctx, apiScope, scopeProperties)
Paul Duffin3d1248c2020-04-09 00:10:17 +01002651
Paul Duffin0f8faff2020-05-20 16:18:00 +01002652 if len(scopeProperties.Stub_srcs) > 0 {
2653 module.createPrebuiltStubsSources(mctx, apiScope, scopeProperties)
2654 }
Jihoon Kang71c86832023-09-13 01:01:53 +00002655
2656 if scopeProperties.Current_api != nil {
2657 module.createPrebuiltApiContribution(mctx, apiScope, scopeProperties)
2658 }
Paul Duffin56d44902020-01-31 13:36:25 +00002659 }
Colin Cross79c7c262019-04-17 11:11:46 -07002660
2661 javaSdkLibraries := javaSdkLibraries(mctx.Config())
2662 javaSdkLibrariesLock.Lock()
2663 defer javaSdkLibrariesLock.Unlock()
2664 *javaSdkLibraries = append(*javaSdkLibraries, module.BaseModuleName())
2665}
2666
Paul Duffineedc5d52020-06-12 17:46:39 +01002667func (module *SdkLibraryImport) createJavaImportForStubs(mctx android.DefaultableHookContext, apiScope *apiScope, scopeProperties *sdkLibraryScopeProperties) {
Paul Duffinbbb546b2020-04-09 00:07:11 +01002668 // Creates a java import for the jar with ".stubs" suffix
2669 props := struct {
Paul Duffin1dbe3ca2020-05-16 09:57:59 +01002670 Name *string
2671 Sdk_version *string
2672 Libs []string
2673 Jars []string
Paul Duffin1267d872021-04-16 17:21:36 +01002674 Compile_dex *bool
Paul Duffinbf4de042022-09-27 12:41:52 +01002675
2676 android.UserSuppliedPrebuiltProperties
Paul Duffinbbb546b2020-04-09 00:07:11 +01002677 }{}
Paul Duffinc3091c82020-05-08 14:16:20 +01002678 props.Name = proptools.StringPtr(module.stubsLibraryModuleName(apiScope))
Paul Duffinbbb546b2020-04-09 00:07:11 +01002679 props.Sdk_version = scopeProperties.Sdk_version
2680 // Prepend any of the libs from the legacy public properties to the libs for each of the
2681 // scopes to avoid having to duplicate them in each scope.
2682 props.Libs = append(module.properties.Libs, scopeProperties.Libs...)
2683 props.Jars = scopeProperties.Jars
Paul Duffin1dbe3ca2020-05-16 09:57:59 +01002684
Paul Duffin38b57852020-05-13 16:08:09 +01002685 // The imports are preferred if the java_sdk_library_import is preferred.
Paul Duffinbf4de042022-09-27 12:41:52 +01002686 props.CopyUserSuppliedPropertiesFromPrebuilt(&module.prebuilt)
Paul Duffin859fe962020-05-15 10:20:31 +01002687
Paul Duffin1267d872021-04-16 17:21:36 +01002688 // The imports need to be compiled to dex if the java_sdk_library_import requests it.
Paul Duffinf4600f62021-05-13 22:34:45 +01002689 compileDex := module.properties.Compile_dex
2690 if module.stubLibrariesCompiledForDex() {
2691 compileDex = proptools.BoolPtr(true)
2692 }
2693 props.Compile_dex = compileDex
Paul Duffin1267d872021-04-16 17:21:36 +01002694
Paul Duffin859fe962020-05-15 10:20:31 +01002695 mctx.CreateModule(ImportFactory, &props, module.sdkComponentPropertiesForChildLibrary())
Paul Duffinbbb546b2020-04-09 00:07:11 +01002696}
2697
Paul Duffineedc5d52020-06-12 17:46:39 +01002698func (module *SdkLibraryImport) createPrebuiltStubsSources(mctx android.DefaultableHookContext, apiScope *apiScope, scopeProperties *sdkLibraryScopeProperties) {
Paul Duffin3d1248c2020-04-09 00:10:17 +01002699 props := struct {
Paul Duffinbf4de042022-09-27 12:41:52 +01002700 Name *string
2701 Srcs []string
2702
2703 android.UserSuppliedPrebuiltProperties
Paul Duffin3d1248c2020-04-09 00:10:17 +01002704 }{}
Paul Duffinc3091c82020-05-08 14:16:20 +01002705 props.Name = proptools.StringPtr(module.stubsSourceModuleName(apiScope))
Paul Duffin3d1248c2020-04-09 00:10:17 +01002706 props.Srcs = scopeProperties.Stub_srcs
Paul Duffin38b57852020-05-13 16:08:09 +01002707
2708 // The stubs source is preferred if the java_sdk_library_import is preferred.
Paul Duffinbf4de042022-09-27 12:41:52 +01002709 props.CopyUserSuppliedPropertiesFromPrebuilt(&module.prebuilt)
2710
Spandan Das2cc80ba2023-10-27 17:21:52 +00002711 mctx.CreateModule(PrebuiltStubsSourcesFactory, &props, module.sdkComponentPropertiesForChildLibrary())
Paul Duffin3d1248c2020-04-09 00:10:17 +01002712}
2713
Jihoon Kang71c86832023-09-13 01:01:53 +00002714func (module *SdkLibraryImport) createPrebuiltApiContribution(mctx android.DefaultableHookContext, apiScope *apiScope, scopeProperties *sdkLibraryScopeProperties) {
2715 api_file := scopeProperties.Current_api
2716 api_surface := &apiScope.name
2717
2718 props := struct {
2719 Name *string
2720 Api_surface *string
2721 Api_file *string
2722 Visibility []string
2723 }{}
2724
2725 props.Name = proptools.StringPtr(module.stubsSourceModuleName(apiScope) + ".api.contribution")
2726 props.Api_surface = api_surface
2727 props.Api_file = api_file
2728 props.Visibility = []string{"//visibility:override", "//visibility:public"}
2729
Spandan Das2cc80ba2023-10-27 17:21:52 +00002730 mctx.CreateModule(ApiContributionImportFactory, &props, module.sdkComponentPropertiesForChildLibrary())
Jihoon Kang71c86832023-09-13 01:01:53 +00002731}
2732
Paul Duffin44f1d842020-06-26 20:17:02 +01002733// Add the dependencies on the child module in the component deps mutator so that it
2734// creates references to the prebuilt and not the source modules.
2735func (module *SdkLibraryImport) ComponentDepsMutator(ctx android.BottomUpMutatorContext) {
Paul Duffin46a26a82020-04-07 19:27:04 +01002736 for apiScope, scopeProperties := range module.scopeProperties {
Paul Duffin56d44902020-01-31 13:36:25 +00002737 if len(scopeProperties.Jars) == 0 {
2738 continue
2739 }
2740
2741 // Add dependencies to the prebuilt stubs library
Paul Duffin864116c2021-04-02 10:24:13 +01002742 ctx.AddVariationDependencies(nil, apiScope.stubsTag, android.PrebuiltNameFromSource(module.stubsLibraryModuleName(apiScope)))
Paul Duffin0f8faff2020-05-20 16:18:00 +01002743
2744 if len(scopeProperties.Stub_srcs) > 0 {
2745 // Add dependencies to the prebuilt stubs source library
Paul Duffin864116c2021-04-02 10:24:13 +01002746 ctx.AddVariationDependencies(nil, apiScope.stubsSourceTag, android.PrebuiltNameFromSource(module.stubsSourceModuleName(apiScope)))
Paul Duffin0f8faff2020-05-20 16:18:00 +01002747 }
Paul Duffin56d44902020-01-31 13:36:25 +00002748 }
Paul Duffin44f1d842020-06-26 20:17:02 +01002749}
2750
2751// Add other dependencies as normal.
2752func (module *SdkLibraryImport) DepsMutator(ctx android.BottomUpMutatorContext) {
Paul Duffineedc5d52020-06-12 17:46:39 +01002753
2754 implName := module.implLibraryModuleName()
2755 if ctx.OtherModuleExists(implName) {
2756 ctx.AddVariationDependencies(nil, implLibraryTag, implName)
2757
2758 xmlPermissionsModuleName := module.xmlPermissionsModuleName()
2759 if module.sharedLibrary() && ctx.OtherModuleExists(xmlPermissionsModuleName) {
2760 // Add dependency to the rule for generating the xml permissions file
2761 ctx.AddDependency(module, xmlPermissionsFileTag, xmlPermissionsModuleName)
2762 }
2763 }
Colin Cross79c7c262019-04-17 11:11:46 -07002764}
2765
Jiyong Park45bf82e2020-12-15 22:29:02 +09002766var _ android.ApexModule = (*SdkLibraryImport)(nil)
2767
2768// Implements android.ApexModule
Paul Duffineedc5d52020-06-12 17:46:39 +01002769func (module *SdkLibraryImport) DepIsInSameApex(mctx android.BaseModuleContext, dep android.Module) bool {
2770 depTag := mctx.OtherModuleDependencyTag(dep)
2771 if depTag == xmlPermissionsFileTag {
2772 return true
2773 }
2774
2775 // None of the other dependencies of the java_sdk_library_import are in the same apex
2776 // as the one that references this module.
2777 return false
2778}
2779
Jiyong Park45bf82e2020-12-15 22:29:02 +09002780// Implements android.ApexModule
Dan Albertc8060532020-07-22 22:32:17 -07002781func (module *SdkLibraryImport) ShouldSupportSdkVersion(ctx android.BaseModuleContext,
2782 sdkVersion android.ApiLevel) error {
Jooyung Han749dc692020-04-15 11:03:39 +09002783 // we don't check prebuilt modules for sdk_version
2784 return nil
2785}
2786
Paul Duffinea8f8082021-06-24 13:25:57 +01002787// Implements android.ApexModule
2788func (module *SdkLibraryImport) UniqueApexVariations() bool {
2789 return module.uniqueApexVariations()
2790}
2791
Paul Duffin09817d62022-04-28 17:45:11 +01002792// MinSdkVersion - Implements hiddenAPIModule
Spandan Das8c9ae7e2023-03-03 21:20:36 +00002793func (module *SdkLibraryImport) MinSdkVersion(ctx android.EarlyModuleContext) android.ApiLevel {
2794 return android.NoneApiLevel
Paul Duffin09817d62022-04-28 17:45:11 +01002795}
2796
2797var _ hiddenAPIModule = (*SdkLibraryImport)(nil)
2798
Paul Duffineedc5d52020-06-12 17:46:39 +01002799func (module *SdkLibraryImport) OutputFiles(tag string) (android.Paths, error) {
Paul Duffin1e940d52022-04-29 14:21:25 +01002800 paths, err := module.commonOutputFiles(tag)
2801 if paths != nil || err != nil {
2802 return paths, err
2803 }
2804 if module.implLibraryModule != nil {
2805 return module.implLibraryModule.OutputFiles(tag)
2806 } else {
2807 return nil, nil
2808 }
Paul Duffin46dc45a2020-05-14 15:39:10 +01002809}
2810
Paul Duffineedc5d52020-06-12 17:46:39 +01002811func (module *SdkLibraryImport) GenerateAndroidBuildActions(ctx android.ModuleContext) {
Paul Duffina2ae7e02020-09-11 11:55:00 +01002812 module.generateCommonBuildActions(ctx)
2813
Jeongik Chad5fe8782021-07-08 01:13:11 +09002814 // Assume that source module(sdk_library) is installed in /<sdk_library partition>/framework
2815 module.installFile = android.PathForModuleInstall(ctx, "framework", module.Stem()+".jar")
2816
Paul Duffin0f8faff2020-05-20 16:18:00 +01002817 // Record the paths to the prebuilt stubs library and stubs source.
Colin Cross79c7c262019-04-17 11:11:46 -07002818 ctx.VisitDirectDeps(func(to android.Module) {
2819 tag := ctx.OtherModuleDependencyTag(to)
2820
Paul Duffin0f8faff2020-05-20 16:18:00 +01002821 // Extract information from any of the scope specific dependencies.
2822 if scopeTag, ok := tag.(scopeDependencyTag); ok {
2823 apiScope := scopeTag.apiScope
2824 scopePaths := module.getScopePathsCreateIfNeeded(apiScope)
2825
2826 // Extract information from the dependency. The exact information extracted
2827 // is determined by the nature of the dependency which is determined by the tag.
2828 scopeTag.extractDepInfo(ctx, to, scopePaths)
Paul Duffineedc5d52020-06-12 17:46:39 +01002829 } else if tag == implLibraryTag {
2830 if implLibrary, ok := to.(*Library); ok {
2831 module.implLibraryModule = implLibrary
2832 } else {
2833 ctx.ModuleErrorf("implementation library must be of type *java.Library but was %T", to)
2834 }
2835 } else if tag == xmlPermissionsFileTag {
2836 if xmlPermissionsFileModule, ok := to.(*sdkLibraryXml); ok {
2837 module.xmlPermissionsFileModule = xmlPermissionsFileModule
2838 } else {
2839 ctx.ModuleErrorf("xml permissions file module must be of type *sdkLibraryXml but was %T", to)
2840 }
Colin Cross79c7c262019-04-17 11:11:46 -07002841 }
2842 })
Paul Duffin0f8faff2020-05-20 16:18:00 +01002843
2844 // Populate the scope paths with information from the properties.
2845 for apiScope, scopeProperties := range module.scopeProperties {
2846 if len(scopeProperties.Jars) == 0 {
2847 continue
2848 }
2849
2850 paths := module.getScopePathsCreateIfNeeded(apiScope)
Anton Hanssond78eb762021-09-21 15:25:12 +01002851 paths.annotationsZip = android.OptionalPathForModuleSrc(ctx, scopeProperties.Annotations)
Paul Duffin0f8faff2020-05-20 16:18:00 +01002852 paths.currentApiFilePath = android.OptionalPathForModuleSrc(ctx, scopeProperties.Current_api)
2853 paths.removedApiFilePath = android.OptionalPathForModuleSrc(ctx, scopeProperties.Removed_api)
2854 }
Paul Duffin39853512021-02-26 11:09:39 +00002855
2856 if ctx.Device() {
2857 // If this is a variant created for a prebuilt_apex then use the dex implementation jar
2858 // obtained from the associated deapexer module.
Colin Crossff694a82023-12-13 15:54:49 -08002859 ai, _ := android.ModuleProvider(ctx, android.ApexInfoProvider)
Paul Duffin39853512021-02-26 11:09:39 +00002860 if ai.ForPrebuiltApex {
Paul Duffin39853512021-02-26 11:09:39 +00002861 // Get the path of the dex implementation jar from the `deapexer` module.
Spandan Dasfae468e2023-12-12 23:23:53 +00002862 di, err := android.FindDeapexerProviderForModule(ctx)
2863 if err != nil {
2864 // An error was found, possibly due to multiple apexes in the tree that export this library
2865 // Defer the error till a client tries to call DexJarBuildPath
2866 module.dexJarFileErr = err
Spandan Das3a392012024-01-17 18:26:27 +00002867 module.initHiddenAPIError(err)
Spandan Dasfae468e2023-12-12 23:23:53 +00002868 return
Martin Stjernholm44825602021-09-17 01:44:12 +01002869 }
Spandan Das5be63332023-12-13 00:06:32 +00002870 dexJarFileApexRootRelative := ApexRootRelativePathToJavaLib(module.BaseModuleName())
Jiakai Zhang81e46812023-02-08 21:56:07 +08002871 if dexOutputPath := di.PrebuiltExportPath(dexJarFileApexRootRelative); dexOutputPath != nil {
Martin Stjernholm8be1e6d2021-09-15 03:34:04 +01002872 dexJarFile := makeDexJarPathFromPath(dexOutputPath)
2873 module.dexJarFile = dexJarFile
Jiakai Zhang204356f2021-09-09 08:12:46 +00002874 installPath := android.PathForModuleInPartitionInstall(
Jiakai Zhang81e46812023-02-08 21:56:07 +08002875 ctx, "apex", ai.ApexVariationName, dexJarFileApexRootRelative)
Jiakai Zhang204356f2021-09-09 08:12:46 +00002876 module.installFile = installPath
Martin Stjernholm8be1e6d2021-09-15 03:34:04 +01002877 module.initHiddenAPI(ctx, dexJarFile, module.findScopePaths(apiScopePublic).stubsImplPath[0], nil)
Jiakai Zhang204356f2021-09-09 08:12:46 +00002878
Jiakai Zhang204356f2021-09-09 08:12:46 +00002879 module.dexpreopter.installPath = module.dexpreopter.getInstallPath(ctx, installPath)
2880 module.dexpreopter.isSDKLibrary = true
2881 module.dexpreopter.uncompressedDex = shouldUncompressDex(ctx, &module.dexpreopter)
Jiakai Zhang81e46812023-02-08 21:56:07 +08002882
2883 if profilePath := di.PrebuiltExportPath(dexJarFileApexRootRelative + ".prof"); profilePath != nil {
2884 module.dexpreopter.inputProfilePathOnHost = profilePath
2885 }
2886
2887 // Dexpreopting.
Jiakai Zhang204356f2021-09-09 08:12:46 +00002888 module.dexpreopt(ctx, dexOutputPath)
Paul Duffin39853512021-02-26 11:09:39 +00002889 } else {
2890 // This should never happen as a variant for a prebuilt_apex is only created if the
2891 // prebuilt_apex has been configured to export the java library dex file.
Martin Stjernholm44825602021-09-17 01:44:12 +01002892 ctx.ModuleErrorf("internal error: no dex implementation jar available from prebuilt APEX %s", di.ApexModuleName())
Paul Duffin39853512021-02-26 11:09:39 +00002893 }
2894 }
2895 }
Colin Cross79c7c262019-04-17 11:11:46 -07002896}
2897
Jiyong Parkf1691d22021-03-29 20:11:58 +09002898func (module *SdkLibraryImport) sdkJars(ctx android.BaseModuleContext, sdkVersion android.SdkSpec, headerJars bool) android.Paths {
Paul Duffineedc5d52020-06-12 17:46:39 +01002899
2900 // For consistency with SdkLibrary make the implementation jar available to libraries that
2901 // are within the same APEX.
2902 implLibraryModule := module.implLibraryModule
Colin Cross56a83212020-09-15 18:30:11 -07002903 if implLibraryModule != nil && withinSameApexesAs(ctx, module) {
Paul Duffineedc5d52020-06-12 17:46:39 +01002904 if headerJars {
2905 return implLibraryModule.HeaderJars()
2906 } else {
2907 return implLibraryModule.ImplementationJars()
2908 }
2909 }
2910
Paul Duffin23970f42020-05-20 14:20:02 +01002911 return module.selectHeaderJarsForSdkVersion(ctx, sdkVersion)
Paul Duffin56d44902020-01-31 13:36:25 +00002912}
2913
Colin Cross79c7c262019-04-17 11:11:46 -07002914// to satisfy SdkLibraryDependency interface
Jiyong Parkf1691d22021-03-29 20:11:58 +09002915func (module *SdkLibraryImport) SdkHeaderJars(ctx android.BaseModuleContext, sdkVersion android.SdkSpec) android.Paths {
Colin Cross79c7c262019-04-17 11:11:46 -07002916 // This module is just a wrapper for the prebuilt stubs.
Paul Duffineedc5d52020-06-12 17:46:39 +01002917 return module.sdkJars(ctx, sdkVersion, true)
Colin Cross79c7c262019-04-17 11:11:46 -07002918}
2919
2920// to satisfy SdkLibraryDependency interface
Jiyong Parkf1691d22021-03-29 20:11:58 +09002921func (module *SdkLibraryImport) SdkImplementationJars(ctx android.BaseModuleContext, sdkVersion android.SdkSpec) android.Paths {
Colin Cross79c7c262019-04-17 11:11:46 -07002922 // This module is just a wrapper for the stubs.
Paul Duffineedc5d52020-06-12 17:46:39 +01002923 return module.sdkJars(ctx, sdkVersion, false)
2924}
2925
Ulya Trafimovichdbf31662020-12-17 12:07:54 +00002926// to satisfy UsesLibraryDependency interface
Spandan Das59a4a2b2024-01-09 21:35:56 +00002927func (module *SdkLibraryImport) DexJarBuildPath(ctx android.ModuleErrorfContext) OptionalDexJarPath {
Paul Duffin39853512021-02-26 11:09:39 +00002928 // The dex implementation jar extracted from the .apex file should be used in preference to the
2929 // source.
Spandan Dasfae468e2023-12-12 23:23:53 +00002930 if module.dexJarFileErr != nil {
Spandan Das59a4a2b2024-01-09 21:35:56 +00002931 ctx.ModuleErrorf(module.dexJarFileErr.Error())
Spandan Dasfae468e2023-12-12 23:23:53 +00002932 }
Martin Stjernholm8be1e6d2021-09-15 03:34:04 +01002933 if module.dexJarFile.IsSet() {
Paul Duffin39853512021-02-26 11:09:39 +00002934 return module.dexJarFile
2935 }
Paul Duffineedc5d52020-06-12 17:46:39 +01002936 if module.implLibraryModule == nil {
Martin Stjernholm8be1e6d2021-09-15 03:34:04 +01002937 return makeUnsetDexJarPath()
Paul Duffineedc5d52020-06-12 17:46:39 +01002938 } else {
Spandan Das59a4a2b2024-01-09 21:35:56 +00002939 return module.implLibraryModule.DexJarBuildPath(ctx)
Paul Duffineedc5d52020-06-12 17:46:39 +01002940 }
2941}
2942
Ulya Trafimovichdbf31662020-12-17 12:07:54 +00002943// to satisfy UsesLibraryDependency interface
Ulya Trafimovich31e444e2020-08-14 17:32:16 +01002944func (module *SdkLibraryImport) DexJarInstallPath() android.Path {
Jeongik Chad5fe8782021-07-08 01:13:11 +09002945 return module.installFile
Ulya Trafimovich31e444e2020-08-14 17:32:16 +01002946}
2947
Ulya Trafimovichdbf31662020-12-17 12:07:54 +00002948// to satisfy UsesLibraryDependency interface
2949func (module *SdkLibraryImport) ClassLoaderContexts() dexpreopt.ClassLoaderContextMap {
2950 return nil
2951}
2952
Paul Duffineedc5d52020-06-12 17:46:39 +01002953// to satisfy apex.javaDependency interface
2954func (module *SdkLibraryImport) JacocoReportClassesFile() android.Path {
2955 if module.implLibraryModule == nil {
2956 return nil
2957 } else {
2958 return module.implLibraryModule.JacocoReportClassesFile()
2959 }
2960}
2961
2962// to satisfy apex.javaDependency interface
Colin Cross08dca382020-07-21 20:31:17 -07002963func (module *SdkLibraryImport) LintDepSets() LintDepSets {
2964 if module.implLibraryModule == nil {
2965 return LintDepSets{}
2966 } else {
2967 return module.implLibraryModule.LintDepSets()
2968 }
2969}
2970
Spandan Das17854f52022-01-14 21:19:14 +00002971func (module *SdkLibraryImport) GetStrictUpdatabilityLinting() bool {
Jaewoong Jung476b9d62021-05-10 15:30:00 -07002972 if module.implLibraryModule == nil {
2973 return false
2974 } else {
Spandan Das17854f52022-01-14 21:19:14 +00002975 return module.implLibraryModule.GetStrictUpdatabilityLinting()
Jaewoong Jung476b9d62021-05-10 15:30:00 -07002976 }
2977}
2978
Spandan Das17854f52022-01-14 21:19:14 +00002979func (module *SdkLibraryImport) SetStrictUpdatabilityLinting(strictLinting bool) {
Jaewoong Jung476b9d62021-05-10 15:30:00 -07002980 if module.implLibraryModule != nil {
Spandan Das17854f52022-01-14 21:19:14 +00002981 module.implLibraryModule.SetStrictUpdatabilityLinting(strictLinting)
Jaewoong Jung476b9d62021-05-10 15:30:00 -07002982 }
2983}
2984
Colin Cross08dca382020-07-21 20:31:17 -07002985// to satisfy apex.javaDependency interface
Paul Duffineedc5d52020-06-12 17:46:39 +01002986func (module *SdkLibraryImport) Stem() string {
2987 return module.BaseModuleName()
Colin Cross79c7c262019-04-17 11:11:46 -07002988}
Jiyong Parke3833882020-02-17 17:28:10 +09002989
Paul Duffin44b481b2020-06-17 16:59:43 +01002990var _ ApexDependency = (*SdkLibraryImport)(nil)
2991
2992// to satisfy java.ApexDependency interface
2993func (module *SdkLibraryImport) HeaderJars() android.Paths {
2994 if module.implLibraryModule == nil {
2995 return nil
2996 } else {
2997 return module.implLibraryModule.HeaderJars()
2998 }
2999}
3000
3001// to satisfy java.ApexDependency interface
3002func (module *SdkLibraryImport) ImplementationAndResourcesJars() android.Paths {
3003 if module.implLibraryModule == nil {
3004 return nil
3005 } else {
3006 return module.implLibraryModule.ImplementationAndResourcesJars()
3007 }
3008}
3009
Jiakai Zhang204356f2021-09-09 08:12:46 +00003010// to satisfy java.DexpreopterInterface interface
3011func (module *SdkLibraryImport) IsInstallable() bool {
3012 return true
3013}
3014
Paul Duffinfef55002021-06-17 14:56:05 +01003015var _ android.RequiredFilesFromPrebuiltApex = (*SdkLibraryImport)(nil)
3016
Paul Duffinb4bbf2c2021-06-17 15:59:07 +01003017func (module *SdkLibraryImport) RequiredFilesFromPrebuiltApex(ctx android.BaseModuleContext) []string {
Paul Duffinfef55002021-06-17 14:56:05 +01003018 name := module.BaseModuleName()
Jiakai Zhang81e46812023-02-08 21:56:07 +08003019 return requiredFilesFromPrebuiltApexForImport(name, &module.dexpreopter)
Paul Duffinfef55002021-06-17 14:56:05 +01003020}
3021
Jiyong Parke3833882020-02-17 17:28:10 +09003022// java_sdk_library_xml
Jiyong Parke3833882020-02-17 17:28:10 +09003023type sdkLibraryXml struct {
3024 android.ModuleBase
3025 android.DefaultableModuleBase
3026 android.ApexModuleBase
3027
3028 properties sdkLibraryXmlProperties
3029
3030 outputFilePath android.OutputPath
3031 installDirPath android.InstallPath
Colin Cross56a83212020-09-15 18:30:11 -07003032
3033 hideApexVariantFromMake bool
Jiyong Parke3833882020-02-17 17:28:10 +09003034}
3035
3036type sdkLibraryXmlProperties struct {
3037 // canonical name of the lib
3038 Lib_name *string
Pedro Loureiro9956e5e2021-09-07 17:21:59 +00003039
3040 // Signals that this shared library is part of the bootclasspath starting
3041 // on the version indicated in this attribute.
3042 //
3043 // This will make platforms at this level and above to ignore
3044 // <uses-library> tags with this library name because the library is already
3045 // available
3046 On_bootclasspath_since *string
3047
3048 // Signals that this shared library was part of the bootclasspath before
3049 // (but not including) the version indicated in this attribute.
3050 //
3051 // The system will automatically add a <uses-library> tag with this library to
3052 // apps that target any SDK less than the version indicated in this attribute.
3053 On_bootclasspath_before *string
3054
3055 // Indicates that PackageManager should ignore this shared library if the
3056 // platform is below the version indicated in this attribute.
3057 //
3058 // This means that the device won't recognise this library as installed.
3059 Min_device_sdk *string
3060
3061 // Indicates that PackageManager should ignore this shared library if the
3062 // platform is above the version indicated in this attribute.
3063 //
3064 // This means that the device won't recognise this library as installed.
3065 Max_device_sdk *string
Pedro Loureiroc3621422021-09-28 15:40:23 +00003066
3067 // The SdkLibrary's min api level as a string
3068 //
3069 // This value comes from the ApiLevel of the MinSdkVersion property.
3070 Sdk_library_min_api_level *string
Jamie Garsidee570ace2023-11-27 12:07:36 +00003071
3072 // Uses-libs dependencies that the shared library requires to work correctly.
3073 //
3074 // This will add dependency="foo:bar" to the <library> section.
3075 Uses_libs_dependencies []string
Jiyong Parke3833882020-02-17 17:28:10 +09003076}
3077
3078// java_sdk_library_xml builds the permission xml file for a java_sdk_library.
3079// Not to be used directly by users. java_sdk_library internally uses this.
3080func sdkLibraryXmlFactory() android.Module {
3081 module := &sdkLibraryXml{}
3082
3083 module.AddProperties(&module.properties)
3084
3085 android.InitApexModule(module)
3086 android.InitAndroidArchModule(module, android.DeviceSupported, android.MultilibCommon)
3087
3088 return module
3089}
3090
Colin Crossaede88c2020-08-11 12:17:01 -07003091func (module *sdkLibraryXml) UniqueApexVariations() bool {
3092 // sdkLibraryXml needs a unique variation per APEX because the generated XML file contains the path to the
3093 // mounted APEX, which contains the name of the APEX.
3094 return true
3095}
3096
Jiyong Parke3833882020-02-17 17:28:10 +09003097// from android.PrebuiltEtcModule
Jooyung Han0703fd82020-08-26 22:11:53 +09003098func (module *sdkLibraryXml) BaseDir() string {
3099 return "etc"
3100}
3101
3102// from android.PrebuiltEtcModule
Jiyong Parke3833882020-02-17 17:28:10 +09003103func (module *sdkLibraryXml) SubDir() string {
3104 return "permissions"
3105}
3106
3107// from android.PrebuiltEtcModule
3108func (module *sdkLibraryXml) OutputFile() android.OutputPath {
3109 return module.outputFilePath
3110}
3111
3112// from android.ApexModule
3113func (module *sdkLibraryXml) AvailableFor(what string) bool {
3114 return true
3115}
3116
3117func (module *sdkLibraryXml) DepsMutator(ctx android.BottomUpMutatorContext) {
3118 // do nothing
3119}
3120
Jiyong Park45bf82e2020-12-15 22:29:02 +09003121var _ android.ApexModule = (*sdkLibraryXml)(nil)
3122
3123// Implements android.ApexModule
Dan Albertc8060532020-07-22 22:32:17 -07003124func (module *sdkLibraryXml) ShouldSupportSdkVersion(ctx android.BaseModuleContext,
3125 sdkVersion android.ApiLevel) error {
Jooyung Han749dc692020-04-15 11:03:39 +09003126 // sdkLibraryXml doesn't need to be checked separately because java_sdk_library is checked
3127 return nil
3128}
3129
Jiyong Parke3833882020-02-17 17:28:10 +09003130// File path to the runtime implementation library
Colin Cross56a83212020-09-15 18:30:11 -07003131func (module *sdkLibraryXml) implPath(ctx android.ModuleContext) string {
Jiyong Parke3833882020-02-17 17:28:10 +09003132 implName := proptools.String(module.properties.Lib_name)
Colin Crossff694a82023-12-13 15:54:49 -08003133 if apexInfo, _ := android.ModuleProvider(ctx, android.ApexInfoProvider); !apexInfo.IsForPlatform() {
Colin Crosse07f2312020-08-13 11:24:56 -07003134 // TODO(b/146468504): ApexVariationName() is only a soong module name, not apex name.
Jiyong Parke3833882020-02-17 17:28:10 +09003135 // In most cases, this works fine. But when apex_name is set or override_apex is used
3136 // this can be wrong.
Colin Cross56a83212020-09-15 18:30:11 -07003137 return fmt.Sprintf("/apex/%s/javalib/%s.jar", apexInfo.ApexVariationName, implName)
Jiyong Parke3833882020-02-17 17:28:10 +09003138 }
3139 partition := "system"
3140 if module.SocSpecific() {
3141 partition = "vendor"
3142 } else if module.DeviceSpecific() {
3143 partition = "odm"
3144 } else if module.ProductSpecific() {
3145 partition = "product"
3146 } else if module.SystemExtSpecific() {
3147 partition = "system_ext"
3148 }
3149 return "/" + partition + "/framework/" + implName + ".jar"
3150}
3151
Pedro Loureiro9956e5e2021-09-07 17:21:59 +00003152func formattedOptionalSdkLevelAttribute(ctx android.ModuleContext, attrName string, value *string) string {
3153 if value == nil {
3154 return ""
3155 }
3156 apiLevel, err := android.ApiLevelFromUser(ctx, *value)
3157 if err != nil {
Pedro Loureiroba6682f2021-10-29 09:32:32 +00003158 // attributes in bp files have underscores but in the xml have dashes.
3159 ctx.PropertyErrorf(strings.ReplaceAll(attrName, "-", "_"), err.Error())
Pedro Loureiro9956e5e2021-09-07 17:21:59 +00003160 return ""
3161 }
Pedro Loureirob638c622021-12-22 15:28:05 +00003162 if apiLevel.IsCurrent() {
3163 // passing "current" would always mean a future release, never the current (or the current in
3164 // progress) which means some conditions would never be triggered.
3165 ctx.PropertyErrorf(strings.ReplaceAll(attrName, "-", "_"),
3166 `"current" is not an allowed value for this attribute`)
3167 return ""
3168 }
Pedro Loureiro48991222022-06-17 20:01:21 +00003169 // "safeValue" is safe because it translates finalized codenames to a string
3170 // with their SDK int.
3171 safeValue := apiLevel.String()
3172 return formattedOptionalAttribute(attrName, &safeValue)
Pedro Loureiro9956e5e2021-09-07 17:21:59 +00003173}
3174
3175// formats an attribute for the xml permissions file if the value is not null
3176// returns empty string otherwise
3177func formattedOptionalAttribute(attrName string, value *string) string {
3178 if value == nil {
3179 return ""
3180 }
3181 return fmt.Sprintf(` %s=\"%s\"\n`, attrName, *value)
3182}
3183
Jamie Garsidee570ace2023-11-27 12:07:36 +00003184func formattedDependenciesAttribute(dependencies []string) string {
3185 if dependencies == nil {
3186 return ""
3187 }
3188 return fmt.Sprintf(` dependency=\"%s\"\n`, strings.Join(dependencies, ":"))
3189}
3190
Pedro Loureiro9956e5e2021-09-07 17:21:59 +00003191func (module *sdkLibraryXml) permissionsContents(ctx android.ModuleContext) string {
3192 libName := proptools.String(module.properties.Lib_name)
3193 libNameAttr := formattedOptionalAttribute("name", &libName)
3194 filePath := module.implPath(ctx)
3195 filePathAttr := formattedOptionalAttribute("file", &filePath)
Pedro Loureiroba6682f2021-10-29 09:32:32 +00003196 implicitFromAttr := formattedOptionalSdkLevelAttribute(ctx, "on-bootclasspath-since", module.properties.On_bootclasspath_since)
3197 implicitUntilAttr := formattedOptionalSdkLevelAttribute(ctx, "on-bootclasspath-before", module.properties.On_bootclasspath_before)
3198 minSdkAttr := formattedOptionalSdkLevelAttribute(ctx, "min-device-sdk", module.properties.Min_device_sdk)
3199 maxSdkAttr := formattedOptionalSdkLevelAttribute(ctx, "max-device-sdk", module.properties.Max_device_sdk)
Jamie Garsidee570ace2023-11-27 12:07:36 +00003200 dependenciesAttr := formattedDependenciesAttribute(module.properties.Uses_libs_dependencies)
Pedro Loureiro196d3e62021-12-22 19:53:01 +00003201 // <library> is understood in all android versions whereas <apex-library> is only understood from API T (and ignored before that).
3202 // 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 +00003203 var libraryTag string
3204 if module.properties.Min_device_sdk != nil {
Pedro Loureiro196d3e62021-12-22 19:53:01 +00003205 libraryTag = ` <apex-library\n`
Pedro Loureiroc3621422021-09-28 15:40:23 +00003206 } else {
3207 libraryTag = ` <library\n`
3208 }
Pedro Loureiro9956e5e2021-09-07 17:21:59 +00003209
3210 return strings.Join([]string{
3211 `<?xml version=\"1.0\" encoding=\"utf-8\"?>\n`,
3212 `<!-- Copyright (C) 2018 The Android Open Source Project\n`,
3213 `\n`,
3214 ` Licensed under the Apache License, Version 2.0 (the \"License\");\n`,
3215 ` you may not use this file except in compliance with the License.\n`,
3216 ` You may obtain a copy of the License at\n`,
3217 `\n`,
3218 ` http://www.apache.org/licenses/LICENSE-2.0\n`,
3219 `\n`,
3220 ` Unless required by applicable law or agreed to in writing, software\n`,
3221 ` distributed under the License is distributed on an \"AS IS\" BASIS,\n`,
3222 ` WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\n`,
3223 ` See the License for the specific language governing permissions and\n`,
3224 ` limitations under the License.\n`,
3225 `-->\n`,
3226 `<permissions>\n`,
Pedro Loureiroc3621422021-09-28 15:40:23 +00003227 libraryTag,
Pedro Loureiro9956e5e2021-09-07 17:21:59 +00003228 libNameAttr,
3229 filePathAttr,
3230 implicitFromAttr,
3231 implicitUntilAttr,
3232 minSdkAttr,
3233 maxSdkAttr,
Jamie Garsidee570ace2023-11-27 12:07:36 +00003234 dependenciesAttr,
Pedro Loureiro9956e5e2021-09-07 17:21:59 +00003235 ` />\n`,
3236 `</permissions>\n`}, "")
3237}
3238
Jiyong Parke3833882020-02-17 17:28:10 +09003239func (module *sdkLibraryXml) GenerateAndroidBuildActions(ctx android.ModuleContext) {
Colin Crossff694a82023-12-13 15:54:49 -08003240 apexInfo, _ := android.ModuleProvider(ctx, android.ApexInfoProvider)
3241 module.hideApexVariantFromMake = !apexInfo.IsForPlatform()
Colin Cross56a83212020-09-15 18:30:11 -07003242
Jiyong Parke3833882020-02-17 17:28:10 +09003243 libName := proptools.String(module.properties.Lib_name)
Pedro Loureiroc3621422021-09-28 15:40:23 +00003244 module.selfValidate(ctx)
Pedro Loureiro9956e5e2021-09-07 17:21:59 +00003245 xmlContent := module.permissionsContents(ctx)
Jiyong Parke3833882020-02-17 17:28:10 +09003246
3247 module.outputFilePath = android.PathForModuleOut(ctx, libName+".xml").OutputPath
Colin Crossf1a035e2020-11-16 17:32:30 -08003248 rule := android.NewRuleBuilder(pctx, ctx)
Jiyong Parke3833882020-02-17 17:28:10 +09003249 rule.Command().
3250 Text("/bin/bash -c \"echo -e '" + xmlContent + "'\" > ").
3251 Output(module.outputFilePath)
3252
Colin Crossf1a035e2020-11-16 17:32:30 -08003253 rule.Build("java_sdk_xml", "Permission XML")
Jiyong Parke3833882020-02-17 17:28:10 +09003254
3255 module.installDirPath = android.PathForModuleInstall(ctx, "etc", module.SubDir())
3256}
3257
3258func (module *sdkLibraryXml) AndroidMkEntries() []android.AndroidMkEntries {
Colin Cross56a83212020-09-15 18:30:11 -07003259 if module.hideApexVariantFromMake {
satayev8f088b02021-12-06 11:40:46 +00003260 return []android.AndroidMkEntries{{
Jiyong Parke3833882020-02-17 17:28:10 +09003261 Disabled: true,
3262 }}
3263 }
3264
satayev8f088b02021-12-06 11:40:46 +00003265 return []android.AndroidMkEntries{{
Jiyong Parke3833882020-02-17 17:28:10 +09003266 Class: "ETC",
3267 OutputFile: android.OptionalPathForPath(module.outputFilePath),
3268 ExtraEntries: []android.AndroidMkExtraEntriesFunc{
Colin Crossaa255532020-07-03 13:18:24 -07003269 func(ctx android.AndroidMkExtraEntriesContext, entries *android.AndroidMkEntries) {
Jiyong Parke3833882020-02-17 17:28:10 +09003270 entries.SetString("LOCAL_MODULE_TAGS", "optional")
Colin Crossc68db4b2021-11-11 18:59:15 -08003271 entries.SetString("LOCAL_MODULE_PATH", module.installDirPath.String())
Jiyong Parke3833882020-02-17 17:28:10 +09003272 entries.SetString("LOCAL_INSTALLED_MODULE_STEM", module.outputFilePath.Base())
3273 },
3274 },
3275 }}
3276}
Paul Duffindd46f712020-02-10 13:37:10 +00003277
Pedro Loureiroc3621422021-09-28 15:40:23 +00003278func (module *sdkLibraryXml) selfValidate(ctx android.ModuleContext) {
3279 module.validateAtLeastTAttributes(ctx)
3280 module.validateMinAndMaxDeviceSdk(ctx)
3281 module.validateMinMaxDeviceSdkAndModuleMinSdk(ctx)
3282 module.validateOnBootclasspathBeforeRequirements(ctx)
3283}
3284
3285func (module *sdkLibraryXml) validateAtLeastTAttributes(ctx android.ModuleContext) {
3286 t := android.ApiLevelOrPanic(ctx, "Tiramisu")
3287 module.attrAtLeastT(ctx, t, module.properties.Min_device_sdk, "min_device_sdk")
3288 module.attrAtLeastT(ctx, t, module.properties.Max_device_sdk, "max_device_sdk")
3289 module.attrAtLeastT(ctx, t, module.properties.On_bootclasspath_before, "on_bootclasspath_before")
3290 module.attrAtLeastT(ctx, t, module.properties.On_bootclasspath_since, "on_bootclasspath_since")
3291}
3292
3293func (module *sdkLibraryXml) attrAtLeastT(ctx android.ModuleContext, t android.ApiLevel, attr *string, attrName string) {
3294 if attr != nil {
3295 if level, err := android.ApiLevelFromUser(ctx, *attr); err == nil {
3296 // we will inform the user of invalid inputs when we try to write the
3297 // permissions xml file so we don't need to do it here
3298 if t.GreaterThan(level) {
3299 ctx.PropertyErrorf(attrName, "Attribute value needs to be at least T")
3300 }
3301 }
3302 }
3303}
3304
3305func (module *sdkLibraryXml) validateMinAndMaxDeviceSdk(ctx android.ModuleContext) {
3306 if module.properties.Min_device_sdk != nil && module.properties.Max_device_sdk != nil {
3307 min, minErr := android.ApiLevelFromUser(ctx, *module.properties.Min_device_sdk)
3308 max, maxErr := android.ApiLevelFromUser(ctx, *module.properties.Max_device_sdk)
3309 if minErr == nil && maxErr == nil {
3310 // we will inform the user of invalid inputs when we try to write the
3311 // permissions xml file so we don't need to do it here
3312 if min.GreaterThan(max) {
3313 ctx.ModuleErrorf("min_device_sdk can't be greater than max_device_sdk")
3314 }
3315 }
3316 }
3317}
3318
3319func (module *sdkLibraryXml) validateMinMaxDeviceSdkAndModuleMinSdk(ctx android.ModuleContext) {
3320 moduleMinApi := android.ApiLevelOrPanic(ctx, *module.properties.Sdk_library_min_api_level)
3321 if module.properties.Min_device_sdk != nil {
3322 api, err := android.ApiLevelFromUser(ctx, *module.properties.Min_device_sdk)
3323 if err == nil {
3324 if moduleMinApi.GreaterThan(api) {
3325 ctx.PropertyErrorf("min_device_sdk", "Can't be less than module's min sdk (%s)", moduleMinApi)
3326 }
3327 }
3328 }
3329 if module.properties.Max_device_sdk != nil {
3330 api, err := android.ApiLevelFromUser(ctx, *module.properties.Max_device_sdk)
3331 if err == nil {
3332 if moduleMinApi.GreaterThan(api) {
3333 ctx.PropertyErrorf("max_device_sdk", "Can't be less than module's min sdk (%s)", moduleMinApi)
3334 }
3335 }
3336 }
3337}
3338
3339func (module *sdkLibraryXml) validateOnBootclasspathBeforeRequirements(ctx android.ModuleContext) {
3340 moduleMinApi := android.ApiLevelOrPanic(ctx, *module.properties.Sdk_library_min_api_level)
3341 if module.properties.On_bootclasspath_before != nil {
3342 t := android.ApiLevelOrPanic(ctx, "Tiramisu")
3343 // if we use the attribute, then we need to do this validation
3344 if moduleMinApi.LessThan(t) {
3345 // if minAPi is < T, then we need to have min_device_sdk (which only accepts T+)
3346 if module.properties.Min_device_sdk == nil {
3347 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")
3348 }
3349 }
3350 }
3351}
3352
Paul Duffindd46f712020-02-10 13:37:10 +00003353type sdkLibrarySdkMemberType struct {
3354 android.SdkMemberTypeBase
3355}
3356
Paul Duffin296701e2021-07-14 10:29:36 +01003357func (s *sdkLibrarySdkMemberType) AddDependencies(ctx android.SdkDependencyContext, dependencyTag blueprint.DependencyTag, names []string) {
3358 ctx.AddVariationDependencies(nil, dependencyTag, names...)
Paul Duffindd46f712020-02-10 13:37:10 +00003359}
3360
3361func (s *sdkLibrarySdkMemberType) IsInstance(module android.Module) bool {
3362 _, ok := module.(*SdkLibrary)
3363 return ok
3364}
3365
3366func (s *sdkLibrarySdkMemberType) AddPrebuiltModule(ctx android.SdkMemberContext, member android.SdkMember) android.BpModule {
3367 return ctx.SnapshotBuilder().AddPrebuiltModule(member, "java_sdk_library_import")
3368}
3369
3370func (s *sdkLibrarySdkMemberType) CreateVariantPropertiesStruct() android.SdkMemberProperties {
3371 return &sdkLibrarySdkMemberProperties{}
3372}
3373
Paul Duffin976b0e52021-04-27 23:20:26 +01003374var javaSdkLibrarySdkMemberType = &sdkLibrarySdkMemberType{
3375 android.SdkMemberTypeBase{
3376 PropertyName: "java_sdk_libs",
3377 SupportsSdk: true,
3378 },
3379}
3380
Paul Duffindd46f712020-02-10 13:37:10 +00003381type sdkLibrarySdkMemberProperties struct {
3382 android.SdkMemberPropertiesBase
3383
Paul Duffine8409952022-09-22 16:24:46 +01003384 // Stem name for files in the sdk snapshot.
3385 //
3386 // This is used to construct the path names of various sdk library files in the sdk snapshot to
3387 // make sure that they match the finalized versions of those files in prebuilts/sdk.
3388 //
3389 // This property is marked as keep so that it will be kept in all instances of this struct, will
3390 // not be cleared but will be copied to common structs. That is needed because this field is used
3391 // to construct many file names for other parts of this struct and so it needs to be present in
3392 // all structs. If it was not marked as keep then it would be cleared in some structs and so would
3393 // be unavailable for generating file names if there were other properties that were still set.
3394 Stem string `sdk:"keep"`
3395
Paul Duffindd46f712020-02-10 13:37:10 +00003396 // Scope to per scope properties.
Paul Duffin106a3a42022-01-27 16:39:06 +00003397 Scopes map[*apiScope]*scopeProperties
Paul Duffindd46f712020-02-10 13:37:10 +00003398
Paul Duffin3d1248c2020-04-09 00:10:17 +01003399 // The Java stubs source files.
3400 Stub_srcs []string
Paul Duffinf7a64332020-05-13 16:54:55 +01003401
3402 // The naming scheme.
3403 Naming_scheme *string
Paul Duffind7eb1c22020-05-26 20:57:10 +01003404
3405 // True if the java_sdk_library_import is for a shared library, false
3406 // otherwise.
3407 Shared_library *bool
Paul Duffina2ae7e02020-09-11 11:55:00 +01003408
Paul Duffin1267d872021-04-16 17:21:36 +01003409 // True if the stub imports should produce dex jars.
3410 Compile_dex *bool
3411
Paul Duffina2ae7e02020-09-11 11:55:00 +01003412 // The paths to the doctag files to add to the prebuilt.
3413 Doctag_paths android.Paths
Paul Duffin869de142021-07-15 14:14:41 +01003414
3415 Permitted_packages []string
Pedro Loureiro9956e5e2021-09-07 17:21:59 +00003416
3417 // Signals that this shared library is part of the bootclasspath starting
3418 // on the version indicated in this attribute.
3419 //
3420 // This will make platforms at this level and above to ignore
3421 // <uses-library> tags with this library name because the library is already
3422 // available
3423 On_bootclasspath_since *string
3424
3425 // Signals that this shared library was part of the bootclasspath before
3426 // (but not including) the version indicated in this attribute.
3427 //
3428 // The system will automatically add a <uses-library> tag with this library to
3429 // apps that target any SDK less than the version indicated in this attribute.
3430 On_bootclasspath_before *string
3431
3432 // Indicates that PackageManager should ignore this shared library if the
3433 // platform is below the version indicated in this attribute.
3434 //
3435 // This means that the device won't recognise this library as installed.
3436 Min_device_sdk *string
3437
3438 // Indicates that PackageManager should ignore this shared library if the
3439 // platform is above the version indicated in this attribute.
3440 //
3441 // This means that the device won't recognise this library as installed.
3442 Max_device_sdk *string
Jiakai Zhang9c4dc192023-02-09 00:09:24 +08003443
3444 DexPreoptProfileGuided *bool `supported_build_releases:"UpsideDownCake+"`
Paul Duffindd46f712020-02-10 13:37:10 +00003445}
3446
3447type scopeProperties struct {
Paul Duffin1fd005d2020-04-09 01:08:11 +01003448 Jars android.Paths
3449 StubsSrcJar android.Path
3450 CurrentApiFile android.Path
3451 RemovedApiFile android.Path
Paul Duffine7babdb2022-02-10 13:06:54 +00003452 AnnotationsZip android.Path `supported_build_releases:"Tiramisu+"`
Paul Duffin1fd005d2020-04-09 01:08:11 +01003453 SdkVersion string
Paul Duffindd46f712020-02-10 13:37:10 +00003454}
3455
3456func (s *sdkLibrarySdkMemberProperties) PopulateFromVariant(ctx android.SdkMemberContext, variant android.Module) {
3457 sdk := variant.(*SdkLibrary)
3458
Paul Duffine8409952022-09-22 16:24:46 +01003459 // Copy the stem name for files in the sdk snapshot.
3460 s.Stem = sdk.distStem()
3461
Paul Duffin106a3a42022-01-27 16:39:06 +00003462 s.Scopes = make(map[*apiScope]*scopeProperties)
Paul Duffindd46f712020-02-10 13:37:10 +00003463 for _, apiScope := range allApiScopes {
Paul Duffin803a9562020-05-20 11:52:25 +01003464 paths := sdk.findScopePaths(apiScope)
3465 if paths == nil {
3466 continue
3467 }
3468
Paul Duffindd46f712020-02-10 13:37:10 +00003469 jars := paths.stubsImplPath
3470 if len(jars) > 0 {
3471 properties := scopeProperties{}
3472 properties.Jars = jars
Paul Duffin780c5f42020-05-12 15:52:55 +01003473 properties.SdkVersion = sdk.sdkVersionForStubsLibrary(ctx.SdkModuleContext(), apiScope)
Paul Duffin0f8faff2020-05-20 16:18:00 +01003474 properties.StubsSrcJar = paths.stubsSrcJar.Path()
Paul Duffin10269f12020-06-19 18:39:55 +01003475 if paths.currentApiFilePath.Valid() {
3476 properties.CurrentApiFile = paths.currentApiFilePath.Path()
3477 }
3478 if paths.removedApiFilePath.Valid() {
3479 properties.RemovedApiFile = paths.removedApiFilePath.Path()
3480 }
Anton Hanssond78eb762021-09-21 15:25:12 +01003481 // The annotations zip is only available for modules that set annotations_enabled: true.
3482 if paths.annotationsZip.Valid() {
3483 properties.AnnotationsZip = paths.annotationsZip.Path()
3484 }
Paul Duffin106a3a42022-01-27 16:39:06 +00003485 s.Scopes[apiScope] = &properties
Paul Duffindd46f712020-02-10 13:37:10 +00003486 }
3487 }
3488
Paul Duffindfa131e2020-05-15 20:37:11 +01003489 s.Naming_scheme = sdk.commonSdkLibraryProperties.Naming_scheme
Paul Duffind7eb1c22020-05-26 20:57:10 +01003490 s.Shared_library = proptools.BoolPtr(sdk.sharedLibrary())
Paul Duffin1267d872021-04-16 17:21:36 +01003491 s.Compile_dex = sdk.dexProperties.Compile_dex
Paul Duffina2ae7e02020-09-11 11:55:00 +01003492 s.Doctag_paths = sdk.doctagPaths
Paul Duffin869de142021-07-15 14:14:41 +01003493 s.Permitted_packages = sdk.PermittedPackagesForUpdatableBootJars()
Pedro Loureiro9956e5e2021-09-07 17:21:59 +00003494 s.On_bootclasspath_since = sdk.commonSdkLibraryProperties.On_bootclasspath_since
3495 s.On_bootclasspath_before = sdk.commonSdkLibraryProperties.On_bootclasspath_before
3496 s.Min_device_sdk = sdk.commonSdkLibraryProperties.Min_device_sdk
3497 s.Max_device_sdk = sdk.commonSdkLibraryProperties.Max_device_sdk
Jiakai Zhang9c4dc192023-02-09 00:09:24 +08003498
3499 if sdk.dexpreopter.dexpreoptProperties.Dex_preopt_result.Profile_guided {
3500 s.DexPreoptProfileGuided = proptools.BoolPtr(true)
3501 }
Paul Duffindd46f712020-02-10 13:37:10 +00003502}
3503
3504func (s *sdkLibrarySdkMemberProperties) AddToPropertySet(ctx android.SdkMemberContext, propertySet android.BpPropertySet) {
Paul Duffinf7a64332020-05-13 16:54:55 +01003505 if s.Naming_scheme != nil {
3506 propertySet.AddProperty("naming_scheme", proptools.String(s.Naming_scheme))
3507 }
Paul Duffind7eb1c22020-05-26 20:57:10 +01003508 if s.Shared_library != nil {
3509 propertySet.AddProperty("shared_library", *s.Shared_library)
3510 }
Paul Duffin1267d872021-04-16 17:21:36 +01003511 if s.Compile_dex != nil {
3512 propertySet.AddProperty("compile_dex", *s.Compile_dex)
3513 }
Paul Duffin869de142021-07-15 14:14:41 +01003514 if len(s.Permitted_packages) > 0 {
3515 propertySet.AddProperty("permitted_packages", s.Permitted_packages)
3516 }
Jiakai Zhang9c4dc192023-02-09 00:09:24 +08003517 dexPreoptSet := propertySet.AddPropertySet("dex_preopt")
3518 if s.DexPreoptProfileGuided != nil {
3519 dexPreoptSet.AddProperty("profile_guided", proptools.Bool(s.DexPreoptProfileGuided))
3520 }
Paul Duffinf7a64332020-05-13 16:54:55 +01003521
Paul Duffine8409952022-09-22 16:24:46 +01003522 stem := s.Stem
3523
Paul Duffindd46f712020-02-10 13:37:10 +00003524 for _, apiScope := range allApiScopes {
3525 if properties, ok := s.Scopes[apiScope]; ok {
Paul Duffin6b836ba2020-05-13 19:19:49 +01003526 scopeSet := propertySet.AddPropertySet(apiScope.propertyName)
Paul Duffindd46f712020-02-10 13:37:10 +00003527
Paul Duffin958806b2022-05-16 13:10:47 +00003528 scopeDir := apiScope.snapshotRelativeDir()
Paul Duffin3d1248c2020-04-09 00:10:17 +01003529
Paul Duffindd46f712020-02-10 13:37:10 +00003530 var jars []string
3531 for _, p := range properties.Jars {
Paul Duffine8409952022-09-22 16:24:46 +01003532 dest := filepath.Join(scopeDir, stem+"-stubs.jar")
Paul Duffindd46f712020-02-10 13:37:10 +00003533 ctx.SnapshotBuilder().CopyToSnapshot(p, dest)
3534 jars = append(jars, dest)
3535 }
3536 scopeSet.AddProperty("jars", jars)
3537
Paul Duffin22628d52021-05-12 23:13:22 +01003538 if ctx.SdkModuleContext().Config().IsEnvTrue("SOONG_SDK_SNAPSHOT_USE_SRCJAR") {
3539 // Copy the stubs source jar into the snapshot zip as is.
Paul Duffine8409952022-09-22 16:24:46 +01003540 srcJarSnapshotPath := filepath.Join(scopeDir, stem+".srcjar")
Paul Duffin22628d52021-05-12 23:13:22 +01003541 ctx.SnapshotBuilder().CopyToSnapshot(properties.StubsSrcJar, srcJarSnapshotPath)
3542 scopeSet.AddProperty("stub_srcs", []string{srcJarSnapshotPath})
3543 } else {
3544 // Merge the stubs source jar into the snapshot zip so that when it is unpacked
3545 // the source files are also unpacked.
Paul Duffine8409952022-09-22 16:24:46 +01003546 snapshotRelativeDir := filepath.Join(scopeDir, stem+"_stub_sources")
Paul Duffin22628d52021-05-12 23:13:22 +01003547 ctx.SnapshotBuilder().UnzipToSnapshot(properties.StubsSrcJar, snapshotRelativeDir)
3548 scopeSet.AddProperty("stub_srcs", []string{snapshotRelativeDir})
3549 }
Paul Duffin3d1248c2020-04-09 00:10:17 +01003550
Paul Duffin1fd005d2020-04-09 01:08:11 +01003551 if properties.CurrentApiFile != nil {
Paul Duffine8409952022-09-22 16:24:46 +01003552 currentApiSnapshotPath := apiScope.snapshotRelativeCurrentApiTxtPath(stem)
Paul Duffin1fd005d2020-04-09 01:08:11 +01003553 ctx.SnapshotBuilder().CopyToSnapshot(properties.CurrentApiFile, currentApiSnapshotPath)
3554 scopeSet.AddProperty("current_api", currentApiSnapshotPath)
3555 }
3556
3557 if properties.RemovedApiFile != nil {
Paul Duffine8409952022-09-22 16:24:46 +01003558 removedApiSnapshotPath := apiScope.snapshotRelativeRemovedApiTxtPath(stem)
Paul Duffin3dbf9fd2020-06-02 13:00:02 +01003559 ctx.SnapshotBuilder().CopyToSnapshot(properties.RemovedApiFile, removedApiSnapshotPath)
Paul Duffin1fd005d2020-04-09 01:08:11 +01003560 scopeSet.AddProperty("removed_api", removedApiSnapshotPath)
3561 }
3562
Anton Hanssond78eb762021-09-21 15:25:12 +01003563 if properties.AnnotationsZip != nil {
Paul Duffine8409952022-09-22 16:24:46 +01003564 annotationsSnapshotPath := filepath.Join(scopeDir, stem+"_annotations.zip")
Anton Hanssond78eb762021-09-21 15:25:12 +01003565 ctx.SnapshotBuilder().CopyToSnapshot(properties.AnnotationsZip, annotationsSnapshotPath)
3566 scopeSet.AddProperty("annotations", annotationsSnapshotPath)
3567 }
3568
Paul Duffindd46f712020-02-10 13:37:10 +00003569 if properties.SdkVersion != "" {
3570 scopeSet.AddProperty("sdk_version", properties.SdkVersion)
3571 }
3572 }
3573 }
3574
Paul Duffina2ae7e02020-09-11 11:55:00 +01003575 if len(s.Doctag_paths) > 0 {
3576 dests := []string{}
3577 for _, p := range s.Doctag_paths {
3578 dest := filepath.Join("doctags", p.Rel())
3579 ctx.SnapshotBuilder().CopyToSnapshot(p, dest)
3580 dests = append(dests, dest)
3581 }
3582 propertySet.AddProperty("doctag_files", dests)
3583 }
Paul Duffindd46f712020-02-10 13:37:10 +00003584}