blob: 38bd301f456f9243e7d948e47bf55d566f7f2e74 [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
Paul Duffind1b3a922020-01-22 11:57:20 +0000107 // The tag to use to depend on the stubs library module.
108 stubsTag scopeDependencyTag
109
Paul Duffin0ff08bd2020-04-29 13:30:54 +0100110 // The tag to use to depend on the stubs source module (if separate from the API module).
111 stubsSourceTag scopeDependencyTag
112
113 // The tag to use to depend on the API file generating module (if separate from the stubs source module).
114 apiFileTag scopeDependencyTag
115
Paul Duffinc8782502020-04-29 20:45:27 +0100116 // The tag to use to depend on the stubs source and API module.
117 stubsSourceAndApiTag scopeDependencyTag
Paul Duffind1b3a922020-01-22 11:57:20 +0000118
Paul Duffin958806b2022-05-16 13:10:47 +0000119 // The tag to use to depend on the module that provides the latest version of the API .txt file.
120 latestApiModuleTag scopeDependencyTag
121
122 // The tag to use to depend on the module that provides the latest version of the API removed.txt
123 // file.
124 latestRemovedApiModuleTag scopeDependencyTag
125
Paul Duffind1b3a922020-01-22 11:57:20 +0000126 // The scope specific prefix to add to the api file base of "current.txt" or "removed.txt".
127 apiFilePrefix string
128
Paul Duffind0b9fca2022-09-30 18:11:41 +0100129 // The scope specific suffix to add to the sdk library module name to construct a scope specific
Paul Duffind1b3a922020-01-22 11:57:20 +0000130 // module name.
131 moduleSuffix string
132
Paul Duffind1b3a922020-01-22 11:57:20 +0000133 // SDK version that the stubs library is built against. Note that this is always
134 // *current. Older stubs library built with a numbered SDK version is created from
135 // the prebuilt jar.
136 sdkVersion string
Paul Duffin1fb487d2020-04-07 18:50:10 +0100137
Paul Duffin15f34ef2020-07-20 18:04:44 +0100138 // The annotation that identifies this API level, empty for the public API scope.
139 annotation string
140
Paul Duffin1fb487d2020-04-07 18:50:10 +0100141 // Extra arguments to pass to droidstubs for this scope.
Paul Duffin0ff08bd2020-04-29 13:30:54 +0100142 //
Paul Duffin15f34ef2020-07-20 18:04:44 +0100143 // This is not used directly but is used to construct the droidstubsArgs.
144 extraArgs []string
Paul Duffin0ff08bd2020-04-29 13:30:54 +0100145
Paul Duffin15f34ef2020-07-20 18:04:44 +0100146 // The args that must be passed to droidstubs to generate the API and stubs source
147 // for this scope, constructed dynamically by initApiScope().
Paul Duffin0ff08bd2020-04-29 13:30:54 +0100148 //
149 // The API only includes the additional members that this scope adds over the scope
150 // that it extends.
Paul Duffin15f34ef2020-07-20 18:04:44 +0100151 //
152 // The stubs source must include the definitions of everything that is in this
153 // api scope and all the scopes that this one extends.
154 droidstubsArgs []string
Paul Duffin0ff08bd2020-04-29 13:30:54 +0100155
Anton Hansson6478ac12020-05-02 11:19:36 +0100156 // Whether the api scope can be treated as unstable, and should skip compat checks.
157 unstable bool
Jihoon Kang1c92c3e2023-03-23 17:44:51 +0000158
159 // Represents the SDK kind of this scope.
160 kind android.SdkKind
Paul Duffind1b3a922020-01-22 11:57:20 +0000161}
162
163// Initialize a scope, creating and adding appropriate dependency tags
164func initApiScope(scope *apiScope) *apiScope {
Paul Duffinc8782502020-04-29 20:45:27 +0100165 name := scope.name
Paul Duffin46dc45a2020-05-14 15:39:10 +0100166 scopeByName[name] = scope
167 allScopeNames = append(allScopeNames, name)
Paul Duffin6b836ba2020-05-13 19:19:49 +0100168 scope.propertyName = strings.ReplaceAll(name, "-", "_")
169 scope.fieldName = proptools.FieldNameForProperty(scope.propertyName)
Paul Duffind1b3a922020-01-22 11:57:20 +0000170 scope.stubsTag = scopeDependencyTag{
Paul Duffinc8782502020-04-29 20:45:27 +0100171 name: name + "-stubs",
172 apiScope: scope,
173 depInfoExtractor: (*scopePaths).extractStubsLibraryInfoFromDependency,
Paul Duffind1b3a922020-01-22 11:57:20 +0000174 }
Paul Duffin0ff08bd2020-04-29 13:30:54 +0100175 scope.stubsSourceTag = scopeDependencyTag{
176 name: name + "-stubs-source",
177 apiScope: scope,
178 depInfoExtractor: (*scopePaths).extractStubsSourceInfoFromDep,
179 }
180 scope.apiFileTag = scopeDependencyTag{
181 name: name + "-api",
182 apiScope: scope,
183 depInfoExtractor: (*scopePaths).extractApiInfoFromDep,
184 }
Paul Duffinc8782502020-04-29 20:45:27 +0100185 scope.stubsSourceAndApiTag = scopeDependencyTag{
186 name: name + "-stubs-source-and-api",
187 apiScope: scope,
188 depInfoExtractor: (*scopePaths).extractStubsSourceAndApiInfoFromApiStubsProvider,
Paul Duffind1b3a922020-01-22 11:57:20 +0000189 }
Paul Duffin958806b2022-05-16 13:10:47 +0000190 scope.latestApiModuleTag = scopeDependencyTag{
191 name: name + "-latest-api",
192 apiScope: scope,
193 depInfoExtractor: (*scopePaths).extractLatestApiPath,
194 }
195 scope.latestRemovedApiModuleTag = scopeDependencyTag{
196 name: name + "-latest-removed-api",
197 apiScope: scope,
198 depInfoExtractor: (*scopePaths).extractLatestRemovedApiPath,
199 }
Paul Duffin0ff08bd2020-04-29 13:30:54 +0100200
201 // To get the args needed to generate the stubs source append all the args from
202 // this scope and all the scopes it extends as each set of args adds additional
203 // members to the stubs.
Paul Duffin15f34ef2020-07-20 18:04:44 +0100204 var scopeSpecificArgs []string
205 if scope.annotation != "" {
206 scopeSpecificArgs = []string{"--show-annotation", scope.annotation}
Paul Duffin0ff08bd2020-04-29 13:30:54 +0100207 }
Paul Duffin15f34ef2020-07-20 18:04:44 +0100208 for s := scope; s != nil; s = s.extends {
209 scopeSpecificArgs = append(scopeSpecificArgs, s.extraArgs...)
Paul Duffin0ff08bd2020-04-29 13:30:54 +0100210
Paul Duffin15f34ef2020-07-20 18:04:44 +0100211 // Ensure that the generated stubs includes all the API elements from the API scope
212 // that this scope extends.
213 if s != scope && s.annotation != "" {
214 scopeSpecificArgs = append(scopeSpecificArgs, "--show-for-stub-purposes-annotation", s.annotation)
215 }
216 }
Paul Duffin0ff08bd2020-04-29 13:30:54 +0100217
Paul Duffind0b9fca2022-09-30 18:11:41 +0100218 // By default, a library that can access a scope can also access the scope it extends.
219 if scope.canAccess == nil {
220 scope.canAccess = scope.extends
221 }
222
Paul Duffin15f34ef2020-07-20 18:04:44 +0100223 // Escape any special characters in the arguments. This is needed because droidstubs
224 // passes these directly to the shell command.
225 scope.droidstubsArgs = proptools.ShellEscapeList(scopeSpecificArgs)
Paul Duffin0ff08bd2020-04-29 13:30:54 +0100226
Paul Duffind1b3a922020-01-22 11:57:20 +0000227 return scope
228}
229
Anton Hansson08f476b2021-04-07 15:32:19 +0100230func (scope *apiScope) stubsLibraryModuleNameSuffix() string {
231 return ".stubs" + scope.moduleSuffix
232}
233
Jihoon Kang1c92c3e2023-03-23 17:44:51 +0000234func (scope *apiScope) apiLibraryModuleName(baseName string) string {
235 return scope.stubsLibraryModuleName(baseName) + ".from-text"
236}
237
Jihoon Kang1147b312023-06-08 23:25:57 +0000238func (scope *apiScope) sourceStubLibraryModuleName(baseName string) string {
239 return scope.stubsLibraryModuleName(baseName) + ".from-source"
240}
241
Paul Duffinc3091c82020-05-08 14:16:20 +0100242func (scope *apiScope) stubsLibraryModuleName(baseName string) string {
Anton Hansson08f476b2021-04-07 15:32:19 +0100243 return baseName + scope.stubsLibraryModuleNameSuffix()
Paul Duffind1b3a922020-01-22 11:57:20 +0000244}
245
Paul Duffinc8782502020-04-29 20:45:27 +0100246func (scope *apiScope) stubsSourceModuleName(baseName string) string {
Paul Duffindd9d0742020-05-08 15:52:37 +0100247 return baseName + ".stubs.source" + scope.moduleSuffix
Paul Duffind1b3a922020-01-22 11:57:20 +0000248}
249
Paul Duffin0ff08bd2020-04-29 13:30:54 +0100250func (scope *apiScope) apiModuleName(baseName string) string {
Paul Duffindd9d0742020-05-08 15:52:37 +0100251 return baseName + ".api" + scope.moduleSuffix
Paul Duffin0ff08bd2020-04-29 13:30:54 +0100252}
253
Paul Duffin3375e352020-04-28 10:44:03 +0100254func (scope *apiScope) String() string {
255 return scope.name
256}
257
Paul Duffin958806b2022-05-16 13:10:47 +0000258// snapshotRelativeDir returns the snapshot directory into which the files related to scopes will
259// be stored.
260func (scope *apiScope) snapshotRelativeDir() string {
261 return filepath.Join("sdk_library", scope.name)
262}
263
264// snapshotRelativeCurrentApiTxtPath returns the snapshot path to the API .txt file for the named
265// library.
266func (scope *apiScope) snapshotRelativeCurrentApiTxtPath(name string) string {
267 return filepath.Join(scope.snapshotRelativeDir(), name+".txt")
268}
269
270// snapshotRelativeRemovedApiTxtPath returns the snapshot path to the removed API .txt file for the
271// named library.
272func (scope *apiScope) snapshotRelativeRemovedApiTxtPath(name string) string {
273 return filepath.Join(scope.snapshotRelativeDir(), name+"-removed.txt")
274}
275
Paul Duffind1b3a922020-01-22 11:57:20 +0000276type apiScopes []*apiScope
277
278func (scopes apiScopes) Strings(accessor func(*apiScope) string) []string {
279 var list []string
280 for _, scope := range scopes {
281 list = append(list, accessor(scope))
282 }
283 return list
284}
285
Jihoon Kanga96a7b12023-09-20 23:43:32 +0000286// Method that maps the apiScopes properties to the index of each apiScopes elements.
287// apiScopes property to be used as the key can be specified with the input accessor.
288// Only a string property of apiScope can be used as the key of the map.
289func (scopes apiScopes) MapToIndex(accessor func(*apiScope) string) map[string]int {
290 ret := make(map[string]int)
291 for i, scope := range scopes {
292 ret[accessor(scope)] = i
293 }
294 return ret
295}
296
Jiyong Parkc678ad32018-04-10 13:07:10 +0900297var (
Paul Duffin46dc45a2020-05-14 15:39:10 +0100298 scopeByName = make(map[string]*apiScope)
299 allScopeNames []string
Paul Duffind1b3a922020-01-22 11:57:20 +0000300 apiScopePublic = initApiScope(&apiScope{
Paul Duffin3375e352020-04-28 10:44:03 +0100301 name: "public",
302
303 // Public scope is enabled by default for both legacy and non-legacy modes.
304 legacyEnabledStatus: func(module *SdkLibrary) bool {
305 return true
306 },
307 defaultEnabledStatus: true,
308
309 scopeSpecificProperties: func(module *SdkLibrary) *ApiScopeProperties {
310 return &module.sdkLibraryProperties.Public
311 },
Paul Duffind1b3a922020-01-22 11:57:20 +0000312 sdkVersion: "current",
Jihoon Kang1c92c3e2023-03-23 17:44:51 +0000313 kind: android.SdkPublic,
Paul Duffind1b3a922020-01-22 11:57:20 +0000314 })
315 apiScopeSystem = initApiScope(&apiScope{
Paul Duffin3375e352020-04-28 10:44:03 +0100316 name: "system",
317 extends: apiScopePublic,
318 legacyEnabledStatus: (*SdkLibrary).generateTestAndSystemScopesByDefault,
319 scopeSpecificProperties: func(module *SdkLibrary) *ApiScopeProperties {
320 return &module.sdkLibraryProperties.System
321 },
Paul Duffin15f34ef2020-07-20 18:04:44 +0100322 apiFilePrefix: "system-",
323 moduleSuffix: ".system",
324 sdkVersion: "system_current",
325 annotation: "android.annotation.SystemApi(client=android.annotation.SystemApi.Client.PRIVILEGED_APPS)",
Jihoon Kang1c92c3e2023-03-23 17:44:51 +0000326 kind: android.SdkSystem,
Paul Duffind1b3a922020-01-22 11:57:20 +0000327 })
328 apiScopeTest = initApiScope(&apiScope{
Paul Duffin3375e352020-04-28 10:44:03 +0100329 name: "test",
Anton Hansson4fe970f2020-10-09 10:16:49 +0100330 extends: apiScopeSystem,
Paul Duffin3375e352020-04-28 10:44:03 +0100331 legacyEnabledStatus: (*SdkLibrary).generateTestAndSystemScopesByDefault,
332 scopeSpecificProperties: func(module *SdkLibrary) *ApiScopeProperties {
333 return &module.sdkLibraryProperties.Test
334 },
Paul Duffin15f34ef2020-07-20 18:04:44 +0100335 apiFilePrefix: "test-",
336 moduleSuffix: ".test",
337 sdkVersion: "test_current",
338 annotation: "android.annotation.TestApi",
339 unstable: true,
Jihoon Kang1c92c3e2023-03-23 17:44:51 +0000340 kind: android.SdkTest,
Paul Duffind1b3a922020-01-22 11:57:20 +0000341 })
Paul Duffin8f265b92020-04-28 14:13:56 +0100342 apiScopeModuleLib = initApiScope(&apiScope{
Paul Duffin6b836ba2020-05-13 19:19:49 +0100343 name: "module-lib",
Paul Duffin8f265b92020-04-28 14:13:56 +0100344 extends: apiScopeSystem,
Paul Duffin0c5bae52020-06-02 13:00:08 +0100345 // The module-lib scope is disabled by default in legacy mode.
Paul Duffin8f265b92020-04-28 14:13:56 +0100346 //
347 // Enabling this would break existing usages.
348 legacyEnabledStatus: func(module *SdkLibrary) bool {
349 return false
350 },
351 scopeSpecificProperties: func(module *SdkLibrary) *ApiScopeProperties {
352 return &module.sdkLibraryProperties.Module_lib
353 },
354 apiFilePrefix: "module-lib-",
355 moduleSuffix: ".module_lib",
356 sdkVersion: "module_current",
Paul Duffin15f34ef2020-07-20 18:04:44 +0100357 annotation: "android.annotation.SystemApi(client=android.annotation.SystemApi.Client.MODULE_LIBRARIES)",
Jihoon Kang1c92c3e2023-03-23 17:44:51 +0000358 kind: android.SdkModule,
Paul Duffin8f265b92020-04-28 14:13:56 +0100359 })
Paul Duffin0c5bae52020-06-02 13:00:08 +0100360 apiScopeSystemServer = initApiScope(&apiScope{
361 name: "system-server",
362 extends: apiScopePublic,
Paul Duffind0b9fca2022-09-30 18:11:41 +0100363
364 // The system-server scope can access the module-lib scope.
365 //
366 // A module that provides a system-server API is appended to the standard bootclasspath that is
367 // used by the system server. So, it should be able to access module-lib APIs provided by
368 // libraries on the bootclasspath.
369 canAccess: apiScopeModuleLib,
370
Paul Duffin0c5bae52020-06-02 13:00:08 +0100371 // The system-server scope is disabled by default in legacy mode.
372 //
373 // Enabling this would break existing usages.
374 legacyEnabledStatus: func(module *SdkLibrary) bool {
375 return false
376 },
377 scopeSpecificProperties: func(module *SdkLibrary) *ApiScopeProperties {
378 return &module.sdkLibraryProperties.System_server
379 },
380 apiFilePrefix: "system-server-",
381 moduleSuffix: ".system_server",
382 sdkVersion: "system_server_current",
Paul Duffin15f34ef2020-07-20 18:04:44 +0100383 annotation: "android.annotation.SystemApi(client=android.annotation.SystemApi.Client.SYSTEM_SERVER)",
384 extraArgs: []string{
385 "--hide-annotation", "android.annotation.Hide",
Paul Duffin0c5bae52020-06-02 13:00:08 +0100386 // com.android.* classes are okay in this interface"
Paul Duffin15f34ef2020-07-20 18:04:44 +0100387 "--hide", "InternalClasses",
Paul Duffin0c5bae52020-06-02 13:00:08 +0100388 },
Jihoon Kang1c92c3e2023-03-23 17:44:51 +0000389 kind: android.SdkSystemServer,
Paul Duffin0c5bae52020-06-02 13:00:08 +0100390 })
Paul Duffind1b3a922020-01-22 11:57:20 +0000391 allApiScopes = apiScopes{
392 apiScopePublic,
393 apiScopeSystem,
394 apiScopeTest,
Paul Duffin8f265b92020-04-28 14:13:56 +0100395 apiScopeModuleLib,
Paul Duffin0c5bae52020-06-02 13:00:08 +0100396 apiScopeSystemServer,
Paul Duffind1b3a922020-01-22 11:57:20 +0000397 }
Jihoon Kang0c705a42023-08-02 06:44:57 +0000398 apiLibraryAdditionalProperties = map[string]struct {
399 FullApiSurfaceStubLib string
400 AdditionalApiContribution string
401 }{
402 "legacy.i18n.module.platform.api": {
403 FullApiSurfaceStubLib: "legacy.core.platform.api.stubs",
404 AdditionalApiContribution: "i18n.module.public.api.stubs.source.api.contribution",
405 },
406 "stable.i18n.module.platform.api": {
407 FullApiSurfaceStubLib: "stable.core.platform.api.stubs",
408 AdditionalApiContribution: "i18n.module.public.api.stubs.source.api.contribution",
409 },
410 "conscrypt.module.platform.api": {
411 FullApiSurfaceStubLib: "stable.core.platform.api.stubs",
412 AdditionalApiContribution: "conscrypt.module.public.api.stubs.source.api.contribution",
413 },
414 }
Jiyong Parkc678ad32018-04-10 13:07:10 +0900415)
416
Jiyong Park82484c02018-04-23 21:41:26 +0900417var (
418 javaSdkLibrariesLock sync.Mutex
419)
420
Jiyong Parkc678ad32018-04-10 13:07:10 +0900421// TODO: these are big features that are currently missing
Jiyong Park1be96912018-05-28 18:02:19 +0900422// 1) disallowing linking to the runtime shared lib
423// 2) HTML generation
Jiyong Parkc678ad32018-04-10 13:07:10 +0900424
425func init() {
Paul Duffin43dc1cc2019-12-19 11:18:54 +0000426 RegisterSdkLibraryBuildComponents(android.InitRegistrationContext)
Jiyong Parkc678ad32018-04-10 13:07:10 +0900427
Jiyong Park82484c02018-04-23 21:41:26 +0900428 android.RegisterMakeVarsProvider(pctx, func(ctx android.MakeVarsContext) {
429 javaSdkLibraries := javaSdkLibraries(ctx.Config())
430 sort.Strings(*javaSdkLibraries)
431 ctx.Strict("JAVA_SDK_LIBRARIES", strings.Join(*javaSdkLibraries, " "))
432 })
Paul Duffindd46f712020-02-10 13:37:10 +0000433
434 // Register sdk member types.
Paul Duffin976b0e52021-04-27 23:20:26 +0100435 android.RegisterSdkMemberType(javaSdkLibrarySdkMemberType)
Jiyong Parkc678ad32018-04-10 13:07:10 +0900436}
437
Paul Duffin43dc1cc2019-12-19 11:18:54 +0000438func RegisterSdkLibraryBuildComponents(ctx android.RegistrationContext) {
439 ctx.RegisterModuleType("java_sdk_library", SdkLibraryFactory)
440 ctx.RegisterModuleType("java_sdk_library_import", sdkLibraryImportFactory)
441}
442
Paul Duffin3375e352020-04-28 10:44:03 +0100443// Properties associated with each api scope.
444type ApiScopeProperties struct {
445 // Indicates whether the api surface is generated.
446 //
447 // If this is set for any scope then all scopes must explicitly specify if they
448 // are enabled. This is to prevent new usages from depending on legacy behavior.
449 //
450 // Otherwise, if this is not set for any scope then the default behavior is
451 // scope specific so please refer to the scope specific property documentation.
452 Enabled *bool
Paul Duffin87a05a32020-05-12 11:50:28 +0100453
454 // The sdk_version to use for building the stubs.
455 //
456 // If not specified then it will use an sdk_version determined as follows:
Trevor Radcliffedf8aa1f2021-11-04 14:25:39 +0000457 //
Paul Duffin87a05a32020-05-12 11:50:28 +0100458 // 1) If the sdk_version specified on the java_sdk_library is none then this
Trevor Radcliffedf8aa1f2021-11-04 14:25:39 +0000459 // will be none. This is used for java_sdk_library instances that are used
460 // to create stubs that contribute to the core_current sdk version.
461 // 2) Otherwise, it is assumed that this library extends but does not
462 // contribute directly to a specific sdk_version and so this uses the
463 // sdk_version appropriate for the api scope. e.g. public will use
464 // sdk_version: current, system will use sdk_version: system_current, etc.
Paul Duffin87a05a32020-05-12 11:50:28 +0100465 //
466 // This does not affect the sdk_version used for either generating the stubs source
467 // or the API file. They both have to use the same sdk_version as is used for
468 // compiling the implementation library.
469 Sdk_version *string
Mark White9421c4c2023-08-10 00:07:03 +0000470
471 // Extra libs used when compiling stubs for this scope.
472 Libs []string
Paul Duffin3375e352020-04-28 10:44:03 +0100473}
474
Jiyong Parkc678ad32018-04-10 13:07:10 +0900475type sdkLibraryProperties struct {
Anton Hanssonf8ea3722021-09-16 14:24:13 +0100476 // List of source files that are needed to compile the API, but are not part of runtime library.
477 Api_srcs []string `android:"arch_variant"`
478
Paul Duffin5df79302020-05-16 15:52:12 +0100479 // Visibility for impl library module. If not specified then defaults to the
480 // visibility property.
481 Impl_library_visibility []string
482
Paul Duffin4911a892020-04-29 23:35:13 +0100483 // Visibility for stubs library modules. If not specified then defaults to the
484 // visibility property.
485 Stubs_library_visibility []string
486
487 // Visibility for stubs source modules. If not specified then defaults to the
488 // visibility property.
489 Stubs_source_visibility []string
490
Anton Hansson7f66efa2020-10-08 14:47:23 +0100491 // List of Java libraries that will be in the classpath when building the implementation lib
492 Impl_only_libs []string `android:"arch_variant"`
493
Paul Duffin77590a82022-04-28 14:13:30 +0000494 // List of Java libraries that will included in the implementation lib.
495 Impl_only_static_libs []string `android:"arch_variant"`
496
Sundong Ahnf043cf62018-06-25 16:04:37 +0900497 // List of Java libraries that will be in the classpath when building stubs
498 Stub_only_libs []string `android:"arch_variant"`
499
Anton Hanssondae54cd2021-04-21 16:30:10 +0100500 // List of Java libraries that will included in stub libraries
501 Stub_only_static_libs []string `android:"arch_variant"`
502
Paul Duffin7a586d32019-12-30 17:09:34 +0000503 // list of package names that will be documented and publicized as API.
504 // This allows the API to be restricted to a subset of the source files provided.
505 // If this is unspecified then all the source files will be treated as being part
506 // of the API.
Jiyong Parkc678ad32018-04-10 13:07:10 +0900507 Api_packages []string
508
Jiyong Park5a2c9d72018-05-01 22:25:41 +0900509 // list of package names that must be hidden from the API
510 Hidden_api_packages []string
511
Paul Duffin749f98f2019-12-30 17:23:46 +0000512 // the relative path to the directory containing the api specification files.
513 // Defaults to "api".
514 Api_dir *string
515
Paul Duffindfa131e2020-05-15 20:37:11 +0100516 // Determines whether a runtime implementation library is built; defaults to false.
517 //
518 // 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 +0200519 // it is as if shared_library: false, was set.
Paul Duffin43db9be2019-12-30 17:35:49 +0000520 Api_only *bool
521
Paul Duffin11512472019-02-11 15:55:17 +0000522 // local files that are used within user customized droiddoc options.
523 Droiddoc_option_files []string
524
Spandan Das93e95992021-07-29 18:26:39 +0000525 // additional droiddoc options.
Paul Duffin11512472019-02-11 15:55:17 +0000526 // Available variables for substitution:
527 //
528 // $(location <label>): the path to the droiddoc_option_files with name <label>
Sundong Ahndd567f92018-07-31 17:19:11 +0900529 Droiddoc_options []string
530
Paul Duffine22c2ab2020-05-20 19:35:27 +0100531 // is set to true, Metalava will allow framework SDK to contain annotations.
532 Annotations_enabled *bool
533
Sundong Ahn054b19a2018-10-19 13:46:09 +0900534 // a list of top-level directories containing files to merge qualifier annotations
535 // (i.e. those intended to be included in the stubs written) from.
536 Merge_annotations_dirs []string
537
538 // a list of top-level directories containing Java stub files to merge show/hide annotations from.
539 Merge_inclusion_annotations_dirs []string
540
Paul Duffin4f5c1ef2020-11-19 14:53:43 +0000541 // If set to true then don't create dist rules.
542 No_dist *bool
Sundong Ahn80a87b32019-05-13 15:02:50 +0900543
Paul Duffin31310252020-11-20 21:26:20 +0000544 // The stem for the artifacts that are copied to the dist, if not specified
545 // then defaults to the base module name.
546 //
547 // For each scope the following artifacts are copied to the apistubs/<scope>
548 // directory in the dist.
549 // * stubs impl jar -> <dist-stem>.jar
550 // * API specification file -> api/<dist-stem>.txt
551 // * Removed API specification file -> api/<dist-stem>-removed.txt
552 //
553 // Also used to construct the name of the filegroup (created by prebuilt_apis)
554 // that references the latest released API and remove API specification files.
555 // * API specification filegroup -> <dist-stem>.api.<scope>.latest
556 // * Removed API specification filegroup -> <dist-stem>-removed.api.<scope>.latest
Jaewoong Jung1a97ee02021-03-09 13:25:02 -0800557 // * API incompatibilities baseline filegroup -> <dist-stem>-incompatibilities.api.<scope>.latest
Paul Duffin31310252020-11-20 21:26:20 +0000558 Dist_stem *string
559
Colin Cross986b69a2021-06-01 13:13:40 -0700560 // The subdirectory for the artifacts that are copied to the dist directory. If not specified
Colin Cross3dd66252021-06-01 14:05:09 -0700561 // then defaults to "unknown". Should be set to "android" for anything that should be published
Colin Cross986b69a2021-06-01 13:13:40 -0700562 // in the public Android SDK.
563 Dist_group *string
564
Anton Hanssondff2c782020-12-21 17:10:01 +0000565 // A compatibility mode that allows historical API-tracking files to not exist.
566 // Do not use.
567 Unsafe_ignore_missing_latest_api bool
568
Paul Duffin3375e352020-04-28 10:44:03 +0100569 // indicates whether system and test apis should be generated.
570 Generate_system_and_test_apis bool `blueprint:"mutated"`
571
572 // The properties specific to the public api scope
573 //
574 // Unless explicitly specified by using public.enabled the public api scope is
575 // enabled by default in both legacy and non-legacy mode.
576 Public ApiScopeProperties
577
578 // The properties specific to the system api scope
579 //
580 // In legacy mode the system api scope is enabled by default when sdk_version
581 // is set to something other than "none".
582 //
583 // In non-legacy mode the system api scope is disabled by default.
584 System ApiScopeProperties
585
586 // The properties specific to the test api scope
587 //
588 // In legacy mode the test api scope is enabled by default when sdk_version
589 // is set to something other than "none".
590 //
591 // In non-legacy mode the test api scope is disabled by default.
592 Test ApiScopeProperties
Paul Duffin37e0b772019-12-30 17:20:10 +0000593
Paul Duffin0c5bae52020-06-02 13:00:08 +0100594 // The properties specific to the module-lib api scope
Paul Duffin8f265b92020-04-28 14:13:56 +0100595 //
Zi Wangb2179e32023-01-31 15:53:30 -0800596 // Unless explicitly specified by using module_lib.enabled the module_lib api
597 // scope is disabled by default.
Paul Duffin8f265b92020-04-28 14:13:56 +0100598 Module_lib ApiScopeProperties
599
Paul Duffin0c5bae52020-06-02 13:00:08 +0100600 // The properties specific to the system-server api scope
601 //
Zi Wangb2179e32023-01-31 15:53:30 -0800602 // Unless explicitly specified by using system_server.enabled the
603 // system_server api scope is disabled by default.
Paul Duffin0c5bae52020-06-02 13:00:08 +0100604 System_server ApiScopeProperties
605
Jiyong Park932cdfe2020-05-28 00:19:53 +0900606 // Determines if the stubs are preferred over the implementation library
607 // for linking, even when the client doesn't specify sdk_version. When this
608 // is set to true, such clients are provided with the widest API surface that
609 // this lib provides. Note however that this option doesn't affect the clients
610 // that are in the same APEX as this library. In that case, the clients are
611 // always linked with the implementation library. Default is false.
612 Default_to_stubs *bool
613
Paul Duffin160fe412020-05-10 19:32:20 +0100614 // Properties related to api linting.
615 Api_lint struct {
616 // Enable api linting.
617 Enabled *bool
Anton Hanssonfd1c0d22023-11-02 15:18:09 +0000618
619 // If API lint is enabled, this flag controls whether a set of legitimate lint errors
620 // are turned off. The default is true.
621 Legacy_errors_allowed *bool
Paul Duffin160fe412020-05-10 19:32:20 +0100622 }
623
Jihoon Kang80456fd2023-11-15 19:22:14 +0000624 // Determines if the module contributes to any api surfaces.
625 // This property should be set to true only if the module is listed under
626 // frameworks-base-api.bootclasspath in frameworks/base/api/Android.bp.
627 // Otherwise, this property should be set to false.
628 // Defaults to false.
629 Contribute_to_android_api *bool
630
Jiyong Parkc678ad32018-04-10 13:07:10 +0900631 // TODO: determines whether to create HTML doc or not
Paul Duffine8409952022-09-22 16:24:46 +0100632 // Html_doc *bool
Jiyong Parkc678ad32018-04-10 13:07:10 +0900633}
634
Paul Duffin0f8faff2020-05-20 16:18:00 +0100635// Paths to outputs from java_sdk_library and java_sdk_library_import.
636//
637// Fields that are android.Paths are always set (during GenerateAndroidBuildActions).
638// OptionalPaths are always set by java_sdk_library but may not be set by
639// java_sdk_library_import as not all instances provide that information.
Paul Duffind1b3a922020-01-22 11:57:20 +0000640type scopePaths struct {
Paul Duffin0f8faff2020-05-20 16:18:00 +0100641 // The path (represented as Paths for convenience when returning) to the stubs header jar.
642 //
643 // That is the jar that is created by turbine.
644 stubsHeaderPath android.Paths
645
646 // The path (represented as Paths for convenience when returning) to the stubs implementation jar.
647 //
648 // This is not the implementation jar, it still only contains stubs.
649 stubsImplPath android.Paths
650
Paul Duffin1267d872021-04-16 17:21:36 +0100651 // The dex jar for the stubs.
652 //
653 // This is not the implementation jar, it still only contains stubs.
Martin Stjernholm8be1e6d2021-09-15 03:34:04 +0100654 stubsDexJarPath OptionalDexJarPath
Paul Duffin1267d872021-04-16 17:21:36 +0100655
Paul Duffin0f8faff2020-05-20 16:18:00 +0100656 // The API specification file, e.g. system_current.txt.
657 currentApiFilePath android.OptionalPath
658
659 // The specification of API elements removed since the last release.
660 removedApiFilePath android.OptionalPath
661
662 // The stubs source jar.
663 stubsSrcJar android.OptionalPath
Anton Hanssond78eb762021-09-21 15:25:12 +0100664
665 // Extracted annotations.
666 annotationsZip android.OptionalPath
Paul Duffin958806b2022-05-16 13:10:47 +0000667
668 // The path to the latest API file.
669 latestApiPath android.OptionalPath
670
671 // The path to the latest removed API file.
672 latestRemovedApiPath android.OptionalPath
Paul Duffind1b3a922020-01-22 11:57:20 +0000673}
674
Colin Crossdcf71b22021-02-01 13:59:03 -0800675func (paths *scopePaths) extractStubsLibraryInfoFromDependency(ctx android.ModuleContext, dep android.Module) error {
Colin Cross313aa542023-12-13 13:47:44 -0800676 if lib, ok := android.OtherModuleProvider(ctx, dep, JavaInfoProvider); ok {
Colin Crossdcf71b22021-02-01 13:59:03 -0800677 paths.stubsHeaderPath = lib.HeaderJars
678 paths.stubsImplPath = lib.ImplementationJars
Paul Duffin1267d872021-04-16 17:21:36 +0100679
680 libDep := dep.(UsesLibraryDependency)
681 paths.stubsDexJarPath = libDep.DexJarBuildPath()
Paul Duffinc8782502020-04-29 20:45:27 +0100682 return nil
683 } else {
Colin Crossdcf71b22021-02-01 13:59:03 -0800684 return fmt.Errorf("expected module that has JavaInfoProvider, e.g. java_library")
Paul Duffinc8782502020-04-29 20:45:27 +0100685 }
686}
687
Paul Duffin0ff08bd2020-04-29 13:30:54 +0100688func (paths *scopePaths) treatDepAsApiStubsProvider(dep android.Module, action func(provider ApiStubsProvider)) error {
689 if apiStubsProvider, ok := dep.(ApiStubsProvider); ok {
690 action(apiStubsProvider)
Paul Duffinc8782502020-04-29 20:45:27 +0100691 return nil
692 } else {
693 return fmt.Errorf("expected module that implements ApiStubsProvider, e.g. droidstubs")
694 }
695}
696
Paul Duffin0f8faff2020-05-20 16:18:00 +0100697func (paths *scopePaths) treatDepAsApiStubsSrcProvider(dep android.Module, action func(provider ApiStubsSrcProvider)) error {
698 if apiStubsProvider, ok := dep.(ApiStubsSrcProvider); ok {
699 action(apiStubsProvider)
700 return nil
701 } else {
702 return fmt.Errorf("expected module that implements ApiStubsSrcProvider, e.g. droidstubs")
703 }
704}
705
Paul Duffin0ff08bd2020-04-29 13:30:54 +0100706func (paths *scopePaths) extractApiInfoFromApiStubsProvider(provider ApiStubsProvider) {
Anton Hanssond78eb762021-09-21 15:25:12 +0100707 paths.annotationsZip = android.OptionalPathForPath(provider.AnnotationsZip())
Paul Duffin0f8faff2020-05-20 16:18:00 +0100708 paths.currentApiFilePath = android.OptionalPathForPath(provider.ApiFilePath())
709 paths.removedApiFilePath = android.OptionalPathForPath(provider.RemovedApiFilePath())
Paul Duffin0ff08bd2020-04-29 13:30:54 +0100710}
711
Colin Crossdcf71b22021-02-01 13:59:03 -0800712func (paths *scopePaths) extractApiInfoFromDep(ctx android.ModuleContext, dep android.Module) error {
Paul Duffin0ff08bd2020-04-29 13:30:54 +0100713 return paths.treatDepAsApiStubsProvider(dep, func(provider ApiStubsProvider) {
714 paths.extractApiInfoFromApiStubsProvider(provider)
715 })
716}
717
Paul Duffin0f8faff2020-05-20 16:18:00 +0100718func (paths *scopePaths) extractStubsSourceInfoFromApiStubsProviders(provider ApiStubsSrcProvider) {
719 paths.stubsSrcJar = android.OptionalPathForPath(provider.StubsSrcJar())
Paul Duffin0ff08bd2020-04-29 13:30:54 +0100720}
721
Colin Crossdcf71b22021-02-01 13:59:03 -0800722func (paths *scopePaths) extractStubsSourceInfoFromDep(ctx android.ModuleContext, dep android.Module) error {
Paul Duffin0f8faff2020-05-20 16:18:00 +0100723 return paths.treatDepAsApiStubsSrcProvider(dep, func(provider ApiStubsSrcProvider) {
Paul Duffin0ff08bd2020-04-29 13:30:54 +0100724 paths.extractStubsSourceInfoFromApiStubsProviders(provider)
725 })
726}
727
Colin Crossdcf71b22021-02-01 13:59:03 -0800728func (paths *scopePaths) extractStubsSourceAndApiInfoFromApiStubsProvider(ctx android.ModuleContext, dep android.Module) error {
Paul Duffin0ff08bd2020-04-29 13:30:54 +0100729 return paths.treatDepAsApiStubsProvider(dep, func(provider ApiStubsProvider) {
730 paths.extractApiInfoFromApiStubsProvider(provider)
731 paths.extractStubsSourceInfoFromApiStubsProviders(provider)
732 })
733}
734
Paul Duffin958806b2022-05-16 13:10:47 +0000735func extractSingleOptionalOutputPath(dep android.Module) (android.OptionalPath, error) {
736 var paths android.Paths
737 if sourceFileProducer, ok := dep.(android.SourceFileProducer); ok {
738 paths = sourceFileProducer.Srcs()
739 } else {
740 return android.OptionalPath{}, fmt.Errorf("module %q does not produce source files", dep)
741 }
742 if len(paths) != 1 {
743 return android.OptionalPath{}, fmt.Errorf("expected one path from %q, got %q", dep, paths)
744 }
745 return android.OptionalPathForPath(paths[0]), nil
746}
747
748func (paths *scopePaths) extractLatestApiPath(ctx android.ModuleContext, dep android.Module) error {
749 outputPath, err := extractSingleOptionalOutputPath(dep)
750 paths.latestApiPath = outputPath
751 return err
752}
753
754func (paths *scopePaths) extractLatestRemovedApiPath(ctx android.ModuleContext, dep android.Module) error {
755 outputPath, err := extractSingleOptionalOutputPath(dep)
756 paths.latestRemovedApiPath = outputPath
757 return err
758}
759
Paul Duffin0ff08bd2020-04-29 13:30:54 +0100760type commonToSdkLibraryAndImportProperties struct {
Paul Duffin1b1e8062020-05-08 13:44:43 +0100761 // The naming scheme to use for the components that this module creates.
762 //
Paul Duffinee9ad5d2020-09-11 13:04:05 +0100763 // If not specified then it defaults to "default".
Paul Duffin1b1e8062020-05-08 13:44:43 +0100764 //
765 // This is a temporary mechanism to simplify conversion from separate modules for each
766 // component that follow a different naming pattern to the default one.
767 //
768 // TODO(b/155480189) - Remove once naming inconsistencies have been resolved.
Paul Duffin0ff08bd2020-04-29 13:30:54 +0100769 Naming_scheme *string
Paul Duffindfa131e2020-05-15 20:37:11 +0100770
771 // Specifies whether this module can be used as an Android shared library; defaults
772 // to true.
773 //
774 // An Android shared library is one that can be referenced in a <uses-library> element
775 // in an AndroidManifest.xml.
776 Shared_library *bool
Paul Duffina2ae7e02020-09-11 11:55:00 +0100777
778 // Files containing information about supported java doc tags.
779 Doctag_files []string `android:"path"`
Pedro Loureiro9956e5e2021-09-07 17:21:59 +0000780
781 // Signals that this shared library is part of the bootclasspath starting
782 // on the version indicated in this attribute.
783 //
784 // This will make platforms at this level and above to ignore
785 // <uses-library> tags with this library name because the library is already
786 // available
787 On_bootclasspath_since *string
788
789 // Signals that this shared library was part of the bootclasspath before
790 // (but not including) the version indicated in this attribute.
791 //
792 // The system will automatically add a <uses-library> tag with this library to
793 // apps that target any SDK less than the version indicated in this attribute.
794 On_bootclasspath_before *string
795
796 // Indicates that PackageManager should ignore this shared library if the
797 // platform is below the version indicated in this attribute.
798 //
799 // This means that the device won't recognise this library as installed.
800 Min_device_sdk *string
801
802 // Indicates that PackageManager should ignore this shared library if the
803 // platform is above the version indicated in this attribute.
804 //
805 // This means that the device won't recognise this library as installed.
806 Max_device_sdk *string
Paul Duffin0ff08bd2020-04-29 13:30:54 +0100807}
808
Paul Duffin71b33cc2021-06-23 11:39:47 +0100809// commonSdkLibraryAndImportModule defines the interface that must be provided by a module that
810// embeds the commonToSdkLibraryAndImport struct.
811type commonSdkLibraryAndImportModule interface {
Paul Duffind796f6f2022-11-23 23:06:05 +0000812 android.Module
Paul Duffin71b33cc2021-06-23 11:39:47 +0100813
814 BaseModuleName() string
815}
816
Paul Duffin56d44902020-01-31 13:36:25 +0000817// Common code between sdk library and sdk library import
818type commonToSdkLibraryAndImport struct {
Paul Duffin71b33cc2021-06-23 11:39:47 +0100819 module commonSdkLibraryAndImportModule
Paul Duffinc3091c82020-05-08 14:16:20 +0100820
Paul Duffin56d44902020-01-31 13:36:25 +0000821 scopePaths map[*apiScope]*scopePaths
Paul Duffin1b1e8062020-05-08 13:44:43 +0100822
823 namingScheme sdkLibraryComponentNamingScheme
824
Paul Duffindfa131e2020-05-15 20:37:11 +0100825 commonSdkLibraryProperties commonToSdkLibraryAndImportProperties
Paul Duffin859fe962020-05-15 10:20:31 +0100826
Paul Duffina2ae7e02020-09-11 11:55:00 +0100827 // Paths to commonSdkLibraryProperties.Doctag_files
828 doctagPaths android.Paths
829
Paul Duffin859fe962020-05-15 10:20:31 +0100830 // Functionality related to this being used as a component of a java_sdk_library.
831 EmbeddableSdkLibraryComponent
Paul Duffin56d44902020-01-31 13:36:25 +0000832}
833
Paul Duffin71b33cc2021-06-23 11:39:47 +0100834func (c *commonToSdkLibraryAndImport) initCommon(module commonSdkLibraryAndImportModule) {
835 c.module = module
Paul Duffin1b1e8062020-05-08 13:44:43 +0100836
Paul Duffin71b33cc2021-06-23 11:39:47 +0100837 module.AddProperties(&c.commonSdkLibraryProperties)
Paul Duffin859fe962020-05-15 10:20:31 +0100838
839 // Initialize this as an sdk library component.
Paul Duffin71b33cc2021-06-23 11:39:47 +0100840 c.initSdkLibraryComponent(module)
Paul Duffin1b1e8062020-05-08 13:44:43 +0100841}
842
843func (c *commonToSdkLibraryAndImport) initCommonAfterDefaultsApplied(ctx android.DefaultableHookContext) bool {
Paul Duffindfa131e2020-05-15 20:37:11 +0100844 schemeProperty := proptools.StringDefault(c.commonSdkLibraryProperties.Naming_scheme, "default")
Paul Duffin1b1e8062020-05-08 13:44:43 +0100845 switch schemeProperty {
846 case "default":
847 c.namingScheme = &defaultNamingScheme{}
848 default:
849 ctx.PropertyErrorf("naming_scheme", "expected 'default' but was %q", schemeProperty)
850 return false
851 }
852
Paul Duffin3f0290e2021-06-30 18:25:36 +0100853 namePtr := proptools.StringPtr(c.module.BaseModuleName())
854 c.sdkLibraryComponentProperties.SdkLibraryName = namePtr
855
Paul Duffindfa131e2020-05-15 20:37:11 +0100856 // Only track this sdk library if this can be used as a shared library.
857 if c.sharedLibrary() {
858 // Use the name specified in the module definition as the owner.
Paul Duffin3f0290e2021-06-30 18:25:36 +0100859 c.sdkLibraryComponentProperties.SdkLibraryToImplicitlyTrack = namePtr
Paul Duffindfa131e2020-05-15 20:37:11 +0100860 }
Paul Duffin859fe962020-05-15 10:20:31 +0100861
Paul Duffin1b1e8062020-05-08 13:44:43 +0100862 return true
Paul Duffinc3091c82020-05-08 14:16:20 +0100863}
864
Paul Duffinea8f8082021-06-24 13:25:57 +0100865// uniqueApexVariations provides common implementation of the ApexModule.UniqueApexVariations
866// method.
867func (c *commonToSdkLibraryAndImport) uniqueApexVariations() bool {
868 // A java_sdk_library that is a shared library produces an XML file that makes the shared library
869 // usable from an AndroidManifest.xml's <uses-library> entry. That XML file contains the name of
870 // the APEX and so it needs a unique variation per APEX.
871 return c.sharedLibrary()
872}
873
Paul Duffina2ae7e02020-09-11 11:55:00 +0100874func (c *commonToSdkLibraryAndImport) generateCommonBuildActions(ctx android.ModuleContext) {
875 c.doctagPaths = android.PathsForModuleSrc(ctx, c.commonSdkLibraryProperties.Doctag_files)
876}
877
Paul Duffineedc5d52020-06-12 17:46:39 +0100878// Module name of the runtime implementation library
879func (c *commonToSdkLibraryAndImport) implLibraryModuleName() string {
Paul Duffin71b33cc2021-06-23 11:39:47 +0100880 return c.module.BaseModuleName() + ".impl"
Paul Duffineedc5d52020-06-12 17:46:39 +0100881}
882
883// Module name of the XML file for the lib
884func (c *commonToSdkLibraryAndImport) xmlPermissionsModuleName() string {
Paul Duffin71b33cc2021-06-23 11:39:47 +0100885 return c.module.BaseModuleName() + sdkXmlFileSuffix
Paul Duffineedc5d52020-06-12 17:46:39 +0100886}
887
Paul Duffinc3091c82020-05-08 14:16:20 +0100888// Name of the java_library module that compiles the stubs source.
889func (c *commonToSdkLibraryAndImport) stubsLibraryModuleName(apiScope *apiScope) string {
Paul Duffinb97b1572021-04-29 21:50:40 +0100890 baseName := c.module.BaseModuleName()
Paul Duffin21787622022-11-25 12:48:20 +0000891 return c.namingScheme.stubsLibraryModuleName(apiScope, baseName)
Paul Duffinc3091c82020-05-08 14:16:20 +0100892}
893
894// Name of the droidstubs module that generates the stubs source and may also
895// generate/check the API.
896func (c *commonToSdkLibraryAndImport) stubsSourceModuleName(apiScope *apiScope) string {
Paul Duffinb97b1572021-04-29 21:50:40 +0100897 baseName := c.module.BaseModuleName()
Paul Duffin21787622022-11-25 12:48:20 +0000898 return c.namingScheme.stubsSourceModuleName(apiScope, baseName)
Paul Duffinc3091c82020-05-08 14:16:20 +0100899}
900
Jihoon Kang1c92c3e2023-03-23 17:44:51 +0000901// Name of the java_api_library module that generates the from-text stubs source
902// and compiles to a jar file.
903func (c *commonToSdkLibraryAndImport) apiLibraryModuleName(apiScope *apiScope) string {
904 baseName := c.module.BaseModuleName()
905 return c.namingScheme.apiLibraryModuleName(apiScope, baseName)
906}
907
Jihoon Kang1147b312023-06-08 23:25:57 +0000908// Name of the java_library module that compiles the stubs
909// generated from source Java files.
910func (c *commonToSdkLibraryAndImport) sourceStubLibraryModuleName(apiScope *apiScope) string {
911 baseName := c.module.BaseModuleName()
912 return c.namingScheme.sourceStubLibraryModuleName(apiScope, baseName)
913}
914
Paul Duffin46dc45a2020-05-14 15:39:10 +0100915// The component names for different outputs of the java_sdk_library.
916//
917// They are similar to the names used for the child modules it creates
918const (
919 stubsSourceComponentName = "stubs.source"
920
921 apiTxtComponentName = "api.txt"
922
923 removedApiTxtComponentName = "removed-api.txt"
Anton Hanssond78eb762021-09-21 15:25:12 +0100924
925 annotationsComponentName = "annotations.zip"
Paul Duffin46dc45a2020-05-14 15:39:10 +0100926)
927
928// A regular expression to match tags that reference a specific stubs component.
929//
930// It will only match if given a valid scope and a valid component. It is verfy strict
931// to ensure it does not accidentally match a similar looking tag that should be processed
932// by the embedded Library.
933var tagSplitter = func() *regexp.Regexp {
934 // Given a list of literal string items returns a regular expression that will
935 // match any one of the items.
936 choice := func(items ...string) string {
937 return `\Q` + strings.Join(items, `\E|\Q`) + `\E`
938 }
939
940 // Regular expression to match one of the scopes.
941 scopesRegexp := choice(allScopeNames...)
942
943 // Regular expression to match one of the components.
Anton Hanssond78eb762021-09-21 15:25:12 +0100944 componentsRegexp := choice(stubsSourceComponentName, apiTxtComponentName, removedApiTxtComponentName, annotationsComponentName)
Paul Duffin46dc45a2020-05-14 15:39:10 +0100945
946 // Regular expression to match any combination of one scope and one component.
947 return regexp.MustCompile(fmt.Sprintf(`^\.(%s)\.(%s)$`, scopesRegexp, componentsRegexp))
948}()
949
950// For OutputFileProducer interface
951//
Anton Hanssond78eb762021-09-21 15:25:12 +0100952// .<scope>.<component name>, for all ComponentNames (for example: .public.removed-api.txt)
Paul Duffin46dc45a2020-05-14 15:39:10 +0100953func (c *commonToSdkLibraryAndImport) commonOutputFiles(tag string) (android.Paths, error) {
954 if groups := tagSplitter.FindStringSubmatch(tag); groups != nil {
955 scopeName := groups[1]
956 component := groups[2]
957
958 if scope, ok := scopeByName[scopeName]; ok {
959 paths := c.findScopePaths(scope)
960 if paths == nil {
Paul Duffin71b33cc2021-06-23 11:39:47 +0100961 return nil, fmt.Errorf("%q does not provide api scope %s", c.module.BaseModuleName(), scopeName)
Paul Duffin46dc45a2020-05-14 15:39:10 +0100962 }
963
964 switch component {
965 case stubsSourceComponentName:
966 if paths.stubsSrcJar.Valid() {
967 return android.Paths{paths.stubsSrcJar.Path()}, nil
968 }
969
970 case apiTxtComponentName:
971 if paths.currentApiFilePath.Valid() {
972 return android.Paths{paths.currentApiFilePath.Path()}, nil
973 }
974
975 case removedApiTxtComponentName:
976 if paths.removedApiFilePath.Valid() {
977 return android.Paths{paths.removedApiFilePath.Path()}, nil
978 }
Anton Hanssond78eb762021-09-21 15:25:12 +0100979
980 case annotationsComponentName:
981 if paths.annotationsZip.Valid() {
982 return android.Paths{paths.annotationsZip.Path()}, nil
983 }
Paul Duffin46dc45a2020-05-14 15:39:10 +0100984 }
985
986 return nil, fmt.Errorf("%s not available for api scope %s", component, scopeName)
987 } else {
988 return nil, fmt.Errorf("unknown scope %s in %s", scope, tag)
989 }
990
991 } else {
Paul Duffina2ae7e02020-09-11 11:55:00 +0100992 switch tag {
993 case ".doctags":
994 if c.doctagPaths != nil {
995 return c.doctagPaths, nil
996 } else {
Paul Duffin71b33cc2021-06-23 11:39:47 +0100997 return nil, fmt.Errorf("no doctag_files specified on %s", c.module.BaseModuleName())
Paul Duffina2ae7e02020-09-11 11:55:00 +0100998 }
999 }
Paul Duffin46dc45a2020-05-14 15:39:10 +01001000 return nil, nil
1001 }
1002}
1003
Paul Duffin803a9562020-05-20 11:52:25 +01001004func (c *commonToSdkLibraryAndImport) getScopePathsCreateIfNeeded(scope *apiScope) *scopePaths {
Paul Duffin56d44902020-01-31 13:36:25 +00001005 if c.scopePaths == nil {
1006 c.scopePaths = make(map[*apiScope]*scopePaths)
1007 }
1008 paths := c.scopePaths[scope]
1009 if paths == nil {
1010 paths = &scopePaths{}
1011 c.scopePaths[scope] = paths
1012 }
1013
1014 return paths
1015}
1016
Paul Duffin803a9562020-05-20 11:52:25 +01001017func (c *commonToSdkLibraryAndImport) findScopePaths(scope *apiScope) *scopePaths {
1018 if c.scopePaths == nil {
1019 return nil
1020 }
1021
1022 return c.scopePaths[scope]
1023}
1024
1025// If this does not support the requested api scope then find the closest available
1026// scope it does support. Returns nil if no such scope is available.
1027func (c *commonToSdkLibraryAndImport) findClosestScopePath(scope *apiScope) *scopePaths {
Paul Duffind0b9fca2022-09-30 18:11:41 +01001028 for s := scope; s != nil; s = s.canAccess {
Paul Duffin803a9562020-05-20 11:52:25 +01001029 if paths := c.findScopePaths(s); paths != nil {
1030 return paths
1031 }
1032 }
1033
1034 // This should never happen outside tests as public should be the base scope for every
1035 // scope and is enabled by default.
1036 return nil
1037}
1038
Jiyong Parkf1691d22021-03-29 20:11:58 +09001039func (c *commonToSdkLibraryAndImport) selectHeaderJarsForSdkVersion(ctx android.BaseModuleContext, sdkVersion android.SdkSpec) android.Paths {
Paul Duffinb05d4292020-05-20 12:19:10 +01001040
1041 // If a specific numeric version has been requested then use prebuilt versions of the sdk.
Jiyong Park54105c42021-03-31 18:17:53 +09001042 if !sdkVersion.ApiLevel.IsPreview() {
Paul Duffin71b33cc2021-06-23 11:39:47 +01001043 return PrebuiltJars(ctx, c.module.BaseModuleName(), sdkVersion)
Paul Duffinb05d4292020-05-20 12:19:10 +01001044 }
1045
Paul Duffin1267d872021-04-16 17:21:36 +01001046 paths := c.selectScopePaths(ctx, sdkVersion.Kind)
1047 if paths == nil {
1048 return nil
1049 }
1050
1051 return paths.stubsHeaderPath
1052}
1053
1054// selectScopePaths returns the *scopePaths appropriate for the specific kind.
1055//
1056// If the module does not support the specific kind then it will return the *scopePaths for the
1057// closest kind which is a subset of the requested kind. e.g. if requesting android.SdkModule then
1058// it will return *scopePaths for android.SdkSystem if available or android.SdkPublic of not.
1059func (c *commonToSdkLibraryAndImport) selectScopePaths(ctx android.BaseModuleContext, kind android.SdkKind) *scopePaths {
Paul Duffin32cf58a2021-05-18 16:32:50 +01001060 apiScope := sdkKindToApiScope(kind)
Paul Duffinb05d4292020-05-20 12:19:10 +01001061
Paul Duffin803a9562020-05-20 11:52:25 +01001062 paths := c.findClosestScopePath(apiScope)
1063 if paths == nil {
1064 var scopes []string
1065 for _, s := range allApiScopes {
1066 if c.findScopePaths(s) != nil {
1067 scopes = append(scopes, s.name)
1068 }
1069 }
Paul Duffin71b33cc2021-06-23 11:39:47 +01001070 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 +01001071 return nil
1072 }
1073
Paul Duffin1267d872021-04-16 17:21:36 +01001074 return paths
1075}
1076
Paul Duffin32cf58a2021-05-18 16:32:50 +01001077// sdkKindToApiScope maps from android.SdkKind to apiScope.
1078func sdkKindToApiScope(kind android.SdkKind) *apiScope {
1079 var apiScope *apiScope
1080 switch kind {
1081 case android.SdkSystem:
1082 apiScope = apiScopeSystem
1083 case android.SdkModule:
1084 apiScope = apiScopeModuleLib
1085 case android.SdkTest:
1086 apiScope = apiScopeTest
1087 case android.SdkSystemServer:
1088 apiScope = apiScopeSystemServer
1089 default:
1090 apiScope = apiScopePublic
1091 }
1092 return apiScope
1093}
1094
Paul Duffin1267d872021-04-16 17:21:36 +01001095// to satisfy SdkLibraryDependency interface
Martin Stjernholm8be1e6d2021-09-15 03:34:04 +01001096func (c *commonToSdkLibraryAndImport) SdkApiStubDexJar(ctx android.BaseModuleContext, kind android.SdkKind) OptionalDexJarPath {
Paul Duffin1267d872021-04-16 17:21:36 +01001097 paths := c.selectScopePaths(ctx, kind)
1098 if paths == nil {
Martin Stjernholm8be1e6d2021-09-15 03:34:04 +01001099 return makeUnsetDexJarPath()
Paul Duffin1267d872021-04-16 17:21:36 +01001100 }
1101
1102 return paths.stubsDexJarPath
Paul Duffinb05d4292020-05-20 12:19:10 +01001103}
1104
Paul Duffin32cf58a2021-05-18 16:32:50 +01001105// to satisfy SdkLibraryDependency interface
1106func (c *commonToSdkLibraryAndImport) SdkRemovedTxtFile(ctx android.BaseModuleContext, kind android.SdkKind) android.OptionalPath {
1107 apiScope := sdkKindToApiScope(kind)
1108 paths := c.findScopePaths(apiScope)
1109 if paths == nil {
1110 return android.OptionalPath{}
1111 }
1112
1113 return paths.removedApiFilePath
1114}
1115
Paul Duffin859fe962020-05-15 10:20:31 +01001116func (c *commonToSdkLibraryAndImport) sdkComponentPropertiesForChildLibrary() interface{} {
1117 componentProps := &struct {
Paul Duffin3f0290e2021-06-30 18:25:36 +01001118 SdkLibraryName *string
Paul Duffin859fe962020-05-15 10:20:31 +01001119 SdkLibraryToImplicitlyTrack *string
Paul Duffindfa131e2020-05-15 20:37:11 +01001120 }{}
1121
Paul Duffin3f0290e2021-06-30 18:25:36 +01001122 namePtr := proptools.StringPtr(c.module.BaseModuleName())
1123 componentProps.SdkLibraryName = namePtr
1124
Paul Duffindfa131e2020-05-15 20:37:11 +01001125 if c.sharedLibrary() {
Paul Duffin859fe962020-05-15 10:20:31 +01001126 // Mark the stubs library as being components of this java_sdk_library so that
1127 // any app that includes code which depends (directly or indirectly) on the stubs
1128 // library will have the appropriate <uses-library> invocation inserted into its
1129 // manifest if necessary.
Paul Duffin3f0290e2021-06-30 18:25:36 +01001130 componentProps.SdkLibraryToImplicitlyTrack = namePtr
Paul Duffin859fe962020-05-15 10:20:31 +01001131 }
1132
1133 return componentProps
1134}
1135
Paul Duffindfa131e2020-05-15 20:37:11 +01001136func (c *commonToSdkLibraryAndImport) sharedLibrary() bool {
1137 return proptools.BoolDefault(c.commonSdkLibraryProperties.Shared_library, true)
1138}
1139
Paul Duffinf4600f62021-05-13 22:34:45 +01001140// Check if the stub libraries should be compiled for dex
1141func (c *commonToSdkLibraryAndImport) stubLibrariesCompiledForDex() bool {
1142 // Always compile the dex file files for the stub libraries if they will be used on the
1143 // bootclasspath.
1144 return !c.sharedLibrary()
1145}
1146
Paul Duffin859fe962020-05-15 10:20:31 +01001147// Properties related to the use of a module as an component of a java_sdk_library.
1148type SdkLibraryComponentProperties struct {
Paul Duffin3f0290e2021-06-30 18:25:36 +01001149 // The name of the java_sdk_library/_import module.
1150 SdkLibraryName *string `blueprint:"mutated"`
Paul Duffin859fe962020-05-15 10:20:31 +01001151
1152 // The name of the java_sdk_library/_import to add to a <uses-library> entry
1153 // in the AndroidManifest.xml of any Android app that includes code that references
1154 // this module. If not set then no java_sdk_library/_import is tracked.
1155 SdkLibraryToImplicitlyTrack *string `blueprint:"mutated"`
1156}
1157
1158// Structure to be embedded in a module struct that needs to support the
1159// SdkLibraryComponentDependency interface.
1160type EmbeddableSdkLibraryComponent struct {
1161 sdkLibraryComponentProperties SdkLibraryComponentProperties
1162}
1163
Paul Duffin71b33cc2021-06-23 11:39:47 +01001164func (e *EmbeddableSdkLibraryComponent) initSdkLibraryComponent(module android.Module) {
1165 module.AddProperties(&e.sdkLibraryComponentProperties)
Paul Duffin859fe962020-05-15 10:20:31 +01001166}
1167
1168// to satisfy SdkLibraryComponentDependency
Paul Duffin3f0290e2021-06-30 18:25:36 +01001169func (e *EmbeddableSdkLibraryComponent) SdkLibraryName() *string {
1170 return e.sdkLibraryComponentProperties.SdkLibraryName
1171}
1172
1173// to satisfy SdkLibraryComponentDependency
Ulya Trafimovich39b437b2020-09-23 16:42:35 +01001174func (e *EmbeddableSdkLibraryComponent) OptionalSdkLibraryImplementation() *string {
Ulya Trafimovich78645fb2021-07-16 15:29:25 +01001175 // For shared libraries, this is the same as the SDK library name. If a Java library or app
1176 // depends on a component library (e.g. a stub library) it still needs to know the name of the
1177 // run-time library and the corresponding module that provides the implementation. This name is
1178 // passed to manifest_fixer (to be added to AndroidManifest.xml) and added to CLC (to be used
1179 // in dexpreopt).
1180 //
1181 // For non-shared SDK (component or not) libraries this returns `nil`, as they are not
1182 // <uses-library> and should not be added to the manifest or to CLC.
Ulya Trafimovich39b437b2020-09-23 16:42:35 +01001183 return e.sdkLibraryComponentProperties.SdkLibraryToImplicitlyTrack
1184}
1185
Paul Duffin859fe962020-05-15 10:20:31 +01001186// Implemented by modules that are (or possibly could be) a component of a java_sdk_library
1187// (including the java_sdk_library) itself.
1188type SdkLibraryComponentDependency interface {
Ulya Trafimovich31e444e2020-08-14 17:32:16 +01001189 UsesLibraryDependency
1190
Paul Duffin3f0290e2021-06-30 18:25:36 +01001191 // SdkLibraryName returns the name of the java_sdk_library/_import module.
1192 SdkLibraryName() *string
1193
Ulya Trafimovich39b437b2020-09-23 16:42:35 +01001194 // The name of the implementation library for the optional SDK library or nil, if there isn't one.
1195 OptionalSdkLibraryImplementation() *string
Paul Duffin859fe962020-05-15 10:20:31 +01001196}
1197
1198// Make sure that all the module types that are components of java_sdk_library/_import
1199// and which can be referenced (directly or indirectly) from an android app implement
1200// the SdkLibraryComponentDependency interface.
1201var _ SdkLibraryComponentDependency = (*Library)(nil)
1202var _ SdkLibraryComponentDependency = (*Import)(nil)
1203var _ SdkLibraryComponentDependency = (*SdkLibrary)(nil)
Paul Duffineedc5d52020-06-12 17:46:39 +01001204var _ SdkLibraryComponentDependency = (*SdkLibraryImport)(nil)
Paul Duffin859fe962020-05-15 10:20:31 +01001205
Paul Duffin32cf58a2021-05-18 16:32:50 +01001206// Provides access to sdk_version related files, e.g. header and implementation jars.
Paul Duffin859fe962020-05-15 10:20:31 +01001207type SdkLibraryDependency interface {
1208 SdkLibraryComponentDependency
1209
1210 // Get the header jars appropriate for the supplied sdk_version.
1211 //
1212 // These are turbine generated jars so they only change if the externals of the
1213 // class changes but it does not contain and implementation or JavaDoc.
Jiyong Parkf1691d22021-03-29 20:11:58 +09001214 SdkHeaderJars(ctx android.BaseModuleContext, sdkVersion android.SdkSpec) android.Paths
Paul Duffin859fe962020-05-15 10:20:31 +01001215
1216 // Get the implementation jars appropriate for the supplied sdk version.
1217 //
1218 // These are either the implementation jar for the whole sdk library or the implementation
1219 // jars for the stubs. The latter should only be needed when generating JavaDoc as otherwise
1220 // they are identical to the corresponding header jars.
Jiyong Parkf1691d22021-03-29 20:11:58 +09001221 SdkImplementationJars(ctx android.BaseModuleContext, sdkVersion android.SdkSpec) android.Paths
Paul Duffin1267d872021-04-16 17:21:36 +01001222
1223 // SdkApiStubDexJar returns the dex jar for the stubs. It is needed by the hiddenapi processing
1224 // tool which processes dex files.
Martin Stjernholm8be1e6d2021-09-15 03:34:04 +01001225 SdkApiStubDexJar(ctx android.BaseModuleContext, kind android.SdkKind) OptionalDexJarPath
Paul Duffinf4600f62021-05-13 22:34:45 +01001226
Paul Duffin32cf58a2021-05-18 16:32:50 +01001227 // SdkRemovedTxtFile returns the optional path to the removed.txt file for the specified sdk kind.
1228 SdkRemovedTxtFile(ctx android.BaseModuleContext, kind android.SdkKind) android.OptionalPath
1229
Paul Duffinf4600f62021-05-13 22:34:45 +01001230 // sharedLibrary returns true if this can be used as a shared library.
1231 sharedLibrary() bool
Paul Duffin859fe962020-05-15 10:20:31 +01001232}
1233
Inseob Kimc0907f12019-02-08 21:00:45 +09001234type SdkLibrary struct {
Sundong Ahn054b19a2018-10-19 13:46:09 +09001235 Library
Jiyong Parkc678ad32018-04-10 13:07:10 +09001236
Sundong Ahn054b19a2018-10-19 13:46:09 +09001237 sdkLibraryProperties sdkLibraryProperties
Jiyong Parkc678ad32018-04-10 13:07:10 +09001238
Paul Duffin3375e352020-04-28 10:44:03 +01001239 // Map from api scope to the scope specific property structure.
1240 scopeToProperties map[*apiScope]*ApiScopeProperties
1241
Paul Duffin56d44902020-01-31 13:36:25 +00001242 commonToSdkLibraryAndImport
Jiyong Parkc678ad32018-04-10 13:07:10 +09001243}
1244
Inseob Kimc0907f12019-02-08 21:00:45 +09001245var _ SdkLibraryDependency = (*SdkLibrary)(nil)
Colin Cross897d2ed2019-02-11 14:03:51 -08001246
Paul Duffin3375e352020-04-28 10:44:03 +01001247func (module *SdkLibrary) generateTestAndSystemScopesByDefault() bool {
1248 return module.sdkLibraryProperties.Generate_system_and_test_apis
1249}
1250
1251func (module *SdkLibrary) getGeneratedApiScopes(ctx android.EarlyModuleContext) apiScopes {
1252 // Check to see if any scopes have been explicitly enabled. If any have then all
1253 // must be.
1254 anyScopesExplicitlyEnabled := false
1255 for _, scope := range allApiScopes {
1256 scopeProperties := module.scopeToProperties[scope]
1257 if scopeProperties.Enabled != nil {
1258 anyScopesExplicitlyEnabled = true
1259 break
1260 }
Paul Duffind1b3a922020-01-22 11:57:20 +00001261 }
Paul Duffin3375e352020-04-28 10:44:03 +01001262
1263 var generatedScopes apiScopes
1264 enabledScopes := make(map[*apiScope]struct{})
1265 for _, scope := range allApiScopes {
1266 scopeProperties := module.scopeToProperties[scope]
1267 // If any scopes are explicitly enabled then ignore the legacy enabled status.
1268 // This is to ensure that any new usages of this module type do not rely on legacy
1269 // behaviour.
1270 defaultEnabledStatus := false
1271 if anyScopesExplicitlyEnabled {
1272 defaultEnabledStatus = scope.defaultEnabledStatus
1273 } else {
1274 defaultEnabledStatus = scope.legacyEnabledStatus(module)
1275 }
1276 enabled := proptools.BoolDefault(scopeProperties.Enabled, defaultEnabledStatus)
1277 if enabled {
1278 enabledScopes[scope] = struct{}{}
1279 generatedScopes = append(generatedScopes, scope)
1280 }
1281 }
1282
1283 // Now check to make sure that any scope that is extended by an enabled scope is also
1284 // enabled.
1285 for _, scope := range allApiScopes {
1286 if _, ok := enabledScopes[scope]; ok {
1287 extends := scope.extends
1288 if extends != nil {
1289 if _, ok := enabledScopes[extends]; !ok {
1290 ctx.ModuleErrorf("enabled api scope %q depends on disabled scope %q", scope, extends)
1291 }
1292 }
1293 }
1294 }
1295
1296 return generatedScopes
Paul Duffind1b3a922020-01-22 11:57:20 +00001297}
1298
satayev758968a2021-12-06 11:42:40 +00001299var _ android.ModuleWithMinSdkVersionCheck = (*SdkLibrary)(nil)
1300
satayev8f088b02021-12-06 11:40:46 +00001301func (module *SdkLibrary) CheckMinSdkVersion(ctx android.ModuleContext) {
Spandan Das8c9ae7e2023-03-03 21:20:36 +00001302 android.CheckMinSdkVersion(ctx, module.MinSdkVersion(ctx), func(c android.ModuleContext, do android.PayloadDepsCallback) {
satayev8f088b02021-12-06 11:40:46 +00001303 ctx.WalkDeps(func(child android.Module, parent android.Module) bool {
1304 isExternal := !module.depIsInSameApex(ctx, child)
1305 if am, ok := child.(android.ApexModule); ok {
1306 if !do(ctx, parent, am, isExternal) {
1307 return false
1308 }
1309 }
1310 return !isExternal
1311 })
1312 })
1313}
1314
Paul Duffineedc5d52020-06-12 17:46:39 +01001315type sdkLibraryComponentTag struct {
1316 blueprint.BaseDependencyTag
1317 name string
1318}
1319
1320// Mark this tag so dependencies that use it are excluded from visibility enforcement.
1321func (t sdkLibraryComponentTag) ExcludeFromVisibilityEnforcement() {}
1322
1323var xmlPermissionsFileTag = sdkLibraryComponentTag{name: "xml-permissions-file"}
Paul Duffine74ac732020-02-06 13:51:46 +00001324
Jiyong Parke3833882020-02-17 17:28:10 +09001325func IsXmlPermissionsFileDepTag(depTag blueprint.DependencyTag) bool {
Paul Duffineedc5d52020-06-12 17:46:39 +01001326 if dt, ok := depTag.(sdkLibraryComponentTag); ok {
Jiyong Parke3833882020-02-17 17:28:10 +09001327 return dt == xmlPermissionsFileTag
1328 }
1329 return false
1330}
1331
Paul Duffineedc5d52020-06-12 17:46:39 +01001332var implLibraryTag = sdkLibraryComponentTag{name: "impl-library"}
Paul Duffin5df79302020-05-16 15:52:12 +01001333
Paul Duffin44f1d842020-06-26 20:17:02 +01001334// Add the dependencies on the child modules in the component deps mutator.
1335func (module *SdkLibrary) ComponentDepsMutator(ctx android.BottomUpMutatorContext) {
Paul Duffin3375e352020-04-28 10:44:03 +01001336 for _, apiScope := range module.getGeneratedApiScopes(ctx) {
Paul Duffind1b3a922020-01-22 11:57:20 +00001337 // Add dependencies to the stubs library
Spandan Das877f39d2023-03-29 16:19:51 +00001338 stubModuleName := module.stubsLibraryModuleName(apiScope)
Jihoon Kang1147b312023-06-08 23:25:57 +00001339
Spandan Das877f39d2023-03-29 16:19:51 +00001340 ctx.AddVariationDependencies(nil, apiScope.stubsTag, stubModuleName)
Paul Duffind1b3a922020-01-22 11:57:20 +00001341
Paul Duffin15f34ef2020-07-20 18:04:44 +01001342 // Add a dependency on the stubs source in order to access both stubs source and api information.
1343 ctx.AddVariationDependencies(nil, apiScope.stubsSourceAndApiTag, module.stubsSourceModuleName(apiScope))
Paul Duffin958806b2022-05-16 13:10:47 +00001344
1345 if module.compareAgainstLatestApi(apiScope) {
1346 // Add dependencies on the latest finalized version of the API .txt file.
1347 latestApiModuleName := module.latestApiModuleName(apiScope)
1348 ctx.AddDependency(module, apiScope.latestApiModuleTag, latestApiModuleName)
1349
1350 // Add dependencies on the latest finalized version of the remove API .txt file.
1351 latestRemovedApiModuleName := module.latestRemovedApiModuleName(apiScope)
1352 ctx.AddDependency(module, apiScope.latestRemovedApiModuleTag, latestRemovedApiModuleName)
1353 }
Sundong Ahn054b19a2018-10-19 13:46:09 +09001354 }
1355
Paul Duffindfa131e2020-05-15 20:37:11 +01001356 if module.requiresRuntimeImplementationLibrary() {
Paul Duffin5df79302020-05-16 15:52:12 +01001357 // Add dependency to the rule for generating the implementation library.
1358 ctx.AddDependency(module, implLibraryTag, module.implLibraryModuleName())
1359
Paul Duffindfa131e2020-05-15 20:37:11 +01001360 if module.sharedLibrary() {
1361 // Add dependency to the rule for generating the xml permissions file
Paul Duffineedc5d52020-06-12 17:46:39 +01001362 ctx.AddDependency(module, xmlPermissionsFileTag, module.xmlPermissionsModuleName())
Paul Duffindfa131e2020-05-15 20:37:11 +01001363 }
Paul Duffin44f1d842020-06-26 20:17:02 +01001364 }
1365}
Paul Duffine74ac732020-02-06 13:51:46 +00001366
Paul Duffin44f1d842020-06-26 20:17:02 +01001367// Add other dependencies as normal.
1368func (module *SdkLibrary) DepsMutator(ctx android.BottomUpMutatorContext) {
Anton Hanssone77fccc2021-01-20 16:52:41 +00001369 var missingApiModules []string
1370 for _, apiScope := range module.getGeneratedApiScopes(ctx) {
1371 if apiScope.unstable {
1372 continue
1373 }
Paul Duffin958806b2022-05-16 13:10:47 +00001374 if m := module.latestApiModuleName(apiScope); !ctx.OtherModuleExists(m) {
Anton Hanssone77fccc2021-01-20 16:52:41 +00001375 missingApiModules = append(missingApiModules, m)
1376 }
Paul Duffin958806b2022-05-16 13:10:47 +00001377 if m := module.latestRemovedApiModuleName(apiScope); !ctx.OtherModuleExists(m) {
Anton Hanssone77fccc2021-01-20 16:52:41 +00001378 missingApiModules = append(missingApiModules, m)
1379 }
Paul Duffin958806b2022-05-16 13:10:47 +00001380 if m := module.latestIncompatibilitiesModuleName(apiScope); !ctx.OtherModuleExists(m) {
Jaewoong Jung1a97ee02021-03-09 13:25:02 -08001381 missingApiModules = append(missingApiModules, m)
1382 }
Anton Hanssone77fccc2021-01-20 16:52:41 +00001383 }
1384 if len(missingApiModules) != 0 && !module.sdkLibraryProperties.Unsafe_ignore_missing_latest_api {
1385 m := module.Name() + " is missing tracking files for previously released library versions.\n"
1386 m += "You need to do one of the following:\n"
1387 m += "- Add `unsafe_ignore_missing_latest_api: true` to your blueprint (to disable compat tracking)\n"
1388 m += "- Add a set of prebuilt txt files representing the last released version of this library for compat checking.\n"
1389 m += " (the current set of API files can be used as a seed for this compatibility tracking\n"
1390 m += "\n"
1391 m += "The following filegroup modules are missing:\n "
1392 m += strings.Join(missingApiModules, "\n ") + "\n"
1393 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."
1394 ctx.ModuleErrorf(m)
1395 }
Paul Duffin44f1d842020-06-26 20:17:02 +01001396 if module.requiresRuntimeImplementationLibrary() {
Paul Duffindfa131e2020-05-15 20:37:11 +01001397 // Only add the deps for the library if it is actually going to be built.
1398 module.Library.deps(ctx)
1399 }
Jiyong Parkc678ad32018-04-10 13:07:10 +09001400}
1401
Paul Duffin46dc45a2020-05-14 15:39:10 +01001402func (module *SdkLibrary) OutputFiles(tag string) (android.Paths, error) {
1403 paths, err := module.commonOutputFiles(tag)
Colin Cross4acaea92021-12-10 23:05:02 +00001404 if paths != nil || err != nil {
Paul Duffin46dc45a2020-05-14 15:39:10 +01001405 return paths, err
1406 }
Colin Cross4acaea92021-12-10 23:05:02 +00001407 if module.requiresRuntimeImplementationLibrary() {
1408 return module.Library.OutputFiles(tag)
1409 }
1410 if tag == "" {
1411 return nil, nil
1412 }
1413 return nil, fmt.Errorf("unsupported module reference tag %q", tag)
Paul Duffin46dc45a2020-05-14 15:39:10 +01001414}
1415
Inseob Kimc0907f12019-02-08 21:00:45 +09001416func (module *SdkLibrary) GenerateAndroidBuildActions(ctx android.ModuleContext) {
satayev8f088b02021-12-06 11:40:46 +00001417 if proptools.String(module.deviceProperties.Min_sdk_version) != "" {
1418 module.CheckMinSdkVersion(ctx)
1419 }
1420
Paul Duffina2ae7e02020-09-11 11:55:00 +01001421 module.generateCommonBuildActions(ctx)
1422
Paul Duffindfa131e2020-05-15 20:37:11 +01001423 // Only build an implementation library if required.
1424 if module.requiresRuntimeImplementationLibrary() {
Paul Duffin43db9be2019-12-30 17:35:49 +00001425 module.Library.GenerateAndroidBuildActions(ctx)
1426 }
Sundong Ahn054b19a2018-10-19 13:46:09 +09001427
Paul Duffinb97b1572021-04-29 21:50:40 +01001428 // Collate the components exported by this module. All scope specific modules are exported but
1429 // the impl and xml component modules are not.
1430 exportedComponents := map[string]struct{}{}
1431
Sundong Ahn57368eb2018-07-06 11:20:23 +09001432 // Record the paths to the header jars of the library (stubs and impl).
Paul Duffind1b3a922020-01-22 11:57:20 +00001433 // When this java_sdk_library is depended upon from others via "libs" property,
Jiyong Parkc678ad32018-04-10 13:07:10 +09001434 // the recorded paths will be returned depending on the link type of the caller.
1435 ctx.VisitDirectDeps(func(to android.Module) {
Jiyong Parkc678ad32018-04-10 13:07:10 +09001436 tag := ctx.OtherModuleDependencyTag(to)
1437
Paul Duffinc8782502020-04-29 20:45:27 +01001438 // Extract information from any of the scope specific dependencies.
1439 if scopeTag, ok := tag.(scopeDependencyTag); ok {
1440 apiScope := scopeTag.apiScope
Paul Duffin803a9562020-05-20 11:52:25 +01001441 scopePaths := module.getScopePathsCreateIfNeeded(apiScope)
Paul Duffinc8782502020-04-29 20:45:27 +01001442
1443 // Extract information from the dependency. The exact information extracted
1444 // is determined by the nature of the dependency which is determined by the tag.
1445 scopeTag.extractDepInfo(ctx, to, scopePaths)
Paul Duffinb97b1572021-04-29 21:50:40 +01001446
1447 exportedComponents[ctx.OtherModuleName(to)] = struct{}{}
Sundong Ahn20e998b2018-07-24 11:19:26 +09001448 }
Jiyong Parkc678ad32018-04-10 13:07:10 +09001449 })
Paul Duffinb97b1572021-04-29 21:50:40 +01001450
1451 // Make the set of components exported by this module available for use elsewhere.
Cole Faust18994c72023-02-28 16:02:16 -08001452 exportedComponentInfo := android.ExportedComponentsInfo{Components: android.SortedKeys(exportedComponents)}
Colin Cross40213022023-12-13 15:19:49 -08001453 android.SetProvider(ctx, android.ExportedComponentsInfoProvider, exportedComponentInfo)
Paul Duffin958806b2022-05-16 13:10:47 +00001454
1455 // Provide additional information for inclusion in an sdk's generated .info file.
1456 additionalSdkInfo := map[string]interface{}{}
1457 additionalSdkInfo["dist_stem"] = module.distStem()
Paul Duffine8409952022-09-22 16:24:46 +01001458 baseModuleName := module.distStem()
Paul Duffin958806b2022-05-16 13:10:47 +00001459 scopes := map[string]interface{}{}
1460 additionalSdkInfo["scopes"] = scopes
1461 for scope, scopePaths := range module.scopePaths {
1462 scopeInfo := map[string]interface{}{}
1463 scopes[scope.name] = scopeInfo
1464 scopeInfo["current_api"] = scope.snapshotRelativeCurrentApiTxtPath(baseModuleName)
1465 scopeInfo["removed_api"] = scope.snapshotRelativeRemovedApiTxtPath(baseModuleName)
1466 if p := scopePaths.latestApiPath; p.Valid() {
1467 scopeInfo["latest_api"] = p.Path().String()
1468 }
1469 if p := scopePaths.latestRemovedApiPath; p.Valid() {
1470 scopeInfo["latest_removed_api"] = p.Path().String()
1471 }
1472 }
Colin Cross40213022023-12-13 15:19:49 -08001473 android.SetProvider(ctx, android.AdditionalSdkInfoProvider, android.AdditionalSdkInfo{additionalSdkInfo})
Jiyong Parkc678ad32018-04-10 13:07:10 +09001474}
1475
Jiyong Park0b0e1b92019-12-03 13:24:29 +09001476func (module *SdkLibrary) AndroidMkEntries() []android.AndroidMkEntries {
Paul Duffindfa131e2020-05-15 20:37:11 +01001477 if !module.requiresRuntimeImplementationLibrary() {
Paul Duffin43db9be2019-12-30 17:35:49 +00001478 return nil
1479 }
Jiyong Park0b0e1b92019-12-03 13:24:29 +09001480 entriesList := module.Library.AndroidMkEntries()
Yo Chiang07d75072020-06-05 17:43:19 +08001481 if module.sharedLibrary() {
1482 entries := &entriesList[0]
1483 entries.Required = append(entries.Required, module.xmlPermissionsModuleName())
1484 }
Jiyong Park0b0e1b92019-12-03 13:24:29 +09001485 return entriesList
Jiyong Park82484c02018-04-23 21:41:26 +09001486}
1487
Anton Hansson5fd5d242020-03-27 19:43:19 +00001488// The dist path of the stub artifacts
1489func (module *SdkLibrary) apiDistPath(apiScope *apiScope) string {
Colin Crossf0eace92021-06-02 13:02:23 -07001490 return path.Join("apistubs", module.distGroup(), apiScope.name)
Anton Hansson5fd5d242020-03-27 19:43:19 +00001491}
1492
Paul Duffin12ceb462019-12-24 20:31:31 +00001493// Get the sdk version for use when compiling the stubs library.
Paul Duffin780c5f42020-05-12 15:52:55 +01001494func (module *SdkLibrary) sdkVersionForStubsLibrary(mctx android.EarlyModuleContext, apiScope *apiScope) string {
Paul Duffin87a05a32020-05-12 11:50:28 +01001495 scopeProperties := module.scopeToProperties[apiScope]
1496 if scopeProperties.Sdk_version != nil {
1497 return proptools.String(scopeProperties.Sdk_version)
1498 }
1499
Jiyong Parkf1691d22021-03-29 20:11:58 +09001500 sdkDep := decodeSdkDep(mctx, android.SdkContext(&module.Library))
Paul Duffin12ceb462019-12-24 20:31:31 +00001501 if sdkDep.hasStandardLibs() {
1502 // If building against a standard sdk then use the sdk version appropriate for the scope.
Paul Duffind1b3a922020-01-22 11:57:20 +00001503 return apiScope.sdkVersion
Paul Duffin12ceb462019-12-24 20:31:31 +00001504 } else {
1505 // Otherwise, use no system module.
1506 return "none"
1507 }
1508}
1509
Paul Duffin31310252020-11-20 21:26:20 +00001510func (module *SdkLibrary) distStem() string {
1511 return proptools.StringDefault(module.sdkLibraryProperties.Dist_stem, module.BaseModuleName())
1512}
1513
Colin Cross986b69a2021-06-01 13:13:40 -07001514// distGroup returns the subdirectory of the dist path of the stub artifacts.
1515func (module *SdkLibrary) distGroup() string {
Colin Cross59b92bf2021-06-01 14:07:56 -07001516 return proptools.StringDefault(module.sdkLibraryProperties.Dist_group, "unknown")
Colin Cross986b69a2021-06-01 13:13:40 -07001517}
1518
Paul Duffin958806b2022-05-16 13:10:47 +00001519func latestPrebuiltApiModuleName(name string, apiScope *apiScope) string {
1520 return PrebuiltApiModuleName(name, apiScope.name, "latest")
1521}
1522
Paul Duffind1b3a922020-01-22 11:57:20 +00001523func (module *SdkLibrary) latestApiFilegroupName(apiScope *apiScope) string {
Paul Duffin958806b2022-05-16 13:10:47 +00001524 return ":" + module.latestApiModuleName(apiScope)
1525}
1526
1527func (module *SdkLibrary) latestApiModuleName(apiScope *apiScope) string {
1528 return latestPrebuiltApiModuleName(module.distStem(), apiScope)
Jiyong Park58c518b2018-05-12 22:29:12 +09001529}
Jiyong Parkc678ad32018-04-10 13:07:10 +09001530
Paul Duffind1b3a922020-01-22 11:57:20 +00001531func (module *SdkLibrary) latestRemovedApiFilegroupName(apiScope *apiScope) string {
Paul Duffin958806b2022-05-16 13:10:47 +00001532 return ":" + module.latestRemovedApiModuleName(apiScope)
1533}
1534
1535func (module *SdkLibrary) latestRemovedApiModuleName(apiScope *apiScope) string {
1536 return latestPrebuiltApiModuleName(module.distStem()+"-removed", apiScope)
Jiyong Parkc678ad32018-04-10 13:07:10 +09001537}
1538
Jaewoong Jung1a97ee02021-03-09 13:25:02 -08001539func (module *SdkLibrary) latestIncompatibilitiesFilegroupName(apiScope *apiScope) string {
Paul Duffin958806b2022-05-16 13:10:47 +00001540 return ":" + module.latestIncompatibilitiesModuleName(apiScope)
1541}
1542
1543func (module *SdkLibrary) latestIncompatibilitiesModuleName(apiScope *apiScope) string {
1544 return latestPrebuiltApiModuleName(module.distStem()+"-incompatibilities", apiScope)
Jaewoong Jung1a97ee02021-03-09 13:25:02 -08001545}
1546
Jihoon Kang1c92c3e2023-03-23 17:44:51 +00001547func (module *SdkLibrary) contributesToApiSurface(c android.Config) bool {
1548 _, exists := c.GetApiLibraries()[module.Name()]
1549 return exists
1550}
1551
Jihoon Kang0c705a42023-08-02 06:44:57 +00001552// The listed modules are the special java_sdk_libraries where apiScope.kind do not match the
1553// api surface that the module contribute to. For example, the public droidstubs and java_library
1554// do not contribute to the public api surface, but contributes to the core platform api surface.
1555// This method returns the full api surface stub lib that
1556// the generated java_api_library should depend on.
1557func (module *SdkLibrary) alternativeFullApiSurfaceStubLib() string {
1558 if val, ok := apiLibraryAdditionalProperties[module.Name()]; ok {
1559 return val.FullApiSurfaceStubLib
1560 }
1561 return ""
1562}
1563
1564// The listed modules' stubs contents do not match the corresponding txt files,
1565// but require additional api contributions to generate the full stubs.
1566// This method returns the name of the additional api contribution module
1567// for corresponding sdk_library modules.
1568func (module *SdkLibrary) apiLibraryAdditionalApiContribution() string {
1569 if val, ok := apiLibraryAdditionalProperties[module.Name()]; ok {
1570 return val.AdditionalApiContribution
1571 }
1572 return ""
1573}
1574
Anton Hansson944e77d2020-08-19 11:40:22 +01001575func childModuleVisibility(childVisibility []string) []string {
1576 if childVisibility == nil {
1577 // No child visibility set. The child will use the visibility of the sdk_library.
1578 return nil
1579 }
1580
1581 // Prepend an override to ignore the sdk_library's visibility, and rely on the child visibility.
1582 var visibility []string
1583 visibility = append(visibility, "//visibility:override")
1584 visibility = append(visibility, childVisibility...)
1585 return visibility
1586}
1587
Paul Duffin5df79302020-05-16 15:52:12 +01001588// Creates the implementation java library
1589func (module *SdkLibrary) createImplLibrary(mctx android.DefaultableHookContext) {
Anton Hansson944e77d2020-08-19 11:40:22 +01001590 visibility := childModuleVisibility(module.sdkLibraryProperties.Impl_library_visibility)
1591
Paul Duffin5df79302020-05-16 15:52:12 +01001592 props := struct {
Paul Duffin77590a82022-04-28 14:13:30 +00001593 Name *string
1594 Visibility []string
1595 Instrument bool
1596 Libs []string
1597 Static_libs []string
1598 Apex_available []string
Paul Duffin5df79302020-05-16 15:52:12 +01001599 }{
1600 Name: proptools.StringPtr(module.implLibraryModuleName()),
Anton Hansson944e77d2020-08-19 11:40:22 +01001601 Visibility: visibility,
Paul Duffin296cf332020-06-18 21:09:55 +01001602 // Set the instrument property to ensure it is instrumented when instrumentation is required.
1603 Instrument: true,
Anton Hansson7f66efa2020-10-08 14:47:23 +01001604 // Set the impl_only libs. Note that the module's "Libs" get appended as well, via the
1605 // addition of &module.properties below.
1606 Libs: module.sdkLibraryProperties.Impl_only_libs,
Paul Duffin77590a82022-04-28 14:13:30 +00001607 // Set the impl_only static libs. Note that the module's "static_libs" get appended as well, via the
1608 // addition of &module.properties below.
1609 Static_libs: module.sdkLibraryProperties.Impl_only_static_libs,
1610 // Pass the apex_available settings down so that the impl library can be statically
1611 // embedded within a library that is added to an APEX. Needed for updatable-media.
1612 Apex_available: module.ApexAvailable(),
Paul Duffin5df79302020-05-16 15:52:12 +01001613 }
1614
1615 properties := []interface{}{
1616 &module.properties,
1617 &module.protoProperties,
1618 &module.deviceProperties,
Liz Kammera7a64f32020-07-09 15:16:41 -07001619 &module.dexProperties,
Paul Duffin5df79302020-05-16 15:52:12 +01001620 &module.dexpreoptProperties,
Colin Cross014489c2020-06-02 20:09:13 -07001621 &module.linter.properties,
Paul Duffin5df79302020-05-16 15:52:12 +01001622 &props,
1623 module.sdkComponentPropertiesForChildLibrary(),
1624 }
1625 mctx.CreateModule(LibraryFactory, properties...)
1626}
1627
Jiyong Parkc678ad32018-04-10 13:07:10 +09001628// Creates a static java library that has API stubs
Paul Duffinf0229202020-04-29 16:47:28 +01001629func (module *SdkLibrary) createStubsLibrary(mctx android.DefaultableHookContext, apiScope *apiScope) {
Jiyong Parkc678ad32018-04-10 13:07:10 +09001630 props := struct {
Dan Willemsen9f435972020-05-28 15:28:00 -07001631 Name *string
1632 Visibility []string
1633 Srcs []string
1634 Installable *bool
1635 Sdk_version *string
1636 System_modules *string
1637 Patch_module *string
1638 Libs []string
Anton Hanssondae54cd2021-04-21 16:30:10 +01001639 Static_libs []string
Dan Willemsen9f435972020-05-28 15:28:00 -07001640 Compile_dex *bool
1641 Java_version *string
1642 Openjdk9 struct {
Sundong Ahn054b19a2018-10-19 13:46:09 +09001643 Srcs []string
1644 Javacflags []string
1645 }
Anton Hansson5fd5d242020-03-27 19:43:19 +00001646 Dist struct {
1647 Targets []string
1648 Dest *string
1649 Dir *string
1650 Tag *string
1651 }
Jiyong Parkc678ad32018-04-10 13:07:10 +09001652 }{}
1653
Jihoon Kang1147b312023-06-08 23:25:57 +00001654 props.Name = proptools.StringPtr(module.sourceStubLibraryModuleName(apiScope))
Jihoon Kang786df932023-09-07 01:18:31 +00001655 props.Visibility = []string{"//visibility:override", "//visibility:private"}
Jiyong Parkc678ad32018-04-10 13:07:10 +09001656 // sources are generated from the droiddoc
Paul Duffinc3091c82020-05-08 14:16:20 +01001657 props.Srcs = []string{":" + module.stubsSourceModuleName(apiScope)}
Paul Duffin12ceb462019-12-24 20:31:31 +00001658 sdkVersion := module.sdkVersionForStubsLibrary(mctx, apiScope)
Paul Duffin52d398a2019-06-11 12:31:14 +01001659 props.Sdk_version = proptools.StringPtr(sdkVersion)
Paul Duffina18abc22020-05-16 18:54:24 +01001660 props.System_modules = module.deviceProperties.System_modules
1661 props.Patch_module = module.properties.Patch_module
Paul Duffin367ab912019-12-23 19:40:36 +00001662 props.Installable = proptools.BoolPtr(false)
Sundong Ahn054b19a2018-10-19 13:46:09 +09001663 props.Libs = module.sdkLibraryProperties.Stub_only_libs
Mark White9421c4c2023-08-10 00:07:03 +00001664 props.Libs = append(props.Libs, module.scopeToProperties[apiScope].Libs...)
Anton Hanssondae54cd2021-04-21 16:30:10 +01001665 props.Static_libs = module.sdkLibraryProperties.Stub_only_static_libs
Paul Duffine22c2ab2020-05-20 19:35:27 +01001666 // The stub-annotations library contains special versions of the annotations
1667 // with CLASS retention policy, so that they're kept.
1668 if proptools.Bool(module.sdkLibraryProperties.Annotations_enabled) {
1669 props.Libs = append(props.Libs, "stub-annotations")
1670 }
Paul Duffina18abc22020-05-16 18:54:24 +01001671 props.Openjdk9.Srcs = module.properties.Openjdk9.Srcs
1672 props.Openjdk9.Javacflags = module.properties.Openjdk9.Javacflags
Anton Hansson83509b52020-05-21 09:21:57 +01001673 // We compile the stubs for 1.8 in line with the main android.jar stubs, and potential
1674 // interop with older developer tools that don't support 1.9.
1675 props.Java_version = proptools.StringPtr("1.8")
Paul Duffinf4600f62021-05-13 22:34:45 +01001676
Paul Duffin859fe962020-05-15 10:20:31 +01001677 mctx.CreateModule(LibraryFactory, &props, module.sdkComponentPropertiesForChildLibrary())
Jiyong Parkc678ad32018-04-10 13:07:10 +09001678}
1679
Paul Duffin6d0886e2020-04-07 18:49:53 +01001680// Creates a droidstubs module that creates stubs source files from the given full source
Paul Duffinc8782502020-04-29 20:45:27 +01001681// files and also updates and checks the API specification files.
Paul Duffin15f34ef2020-07-20 18:04:44 +01001682func (module *SdkLibrary) createStubsSourcesAndApi(mctx android.DefaultableHookContext, apiScope *apiScope, name string, scopeSpecificDroidstubsArgs []string) {
Jiyong Parkc678ad32018-04-10 13:07:10 +09001683 props := struct {
Sundong Ahn054b19a2018-10-19 13:46:09 +09001684 Name *string
Paul Duffin4911a892020-04-29 23:35:13 +01001685 Visibility []string
Sundong Ahn054b19a2018-10-19 13:46:09 +09001686 Srcs []string
1687 Installable *bool
Paul Duffin52d398a2019-06-11 12:31:14 +01001688 Sdk_version *string
Jihoon Kang3198f3c2023-01-26 08:08:52 +00001689 Api_surface *string
Paul Duffin12ceb462019-12-24 20:31:31 +00001690 System_modules *string
Sundong Ahn054b19a2018-10-19 13:46:09 +09001691 Libs []string
Paul Duffin6877e6d2020-09-25 19:59:14 +01001692 Output_javadoc_comments *bool
Paul Duffin11512472019-02-11 15:55:17 +00001693 Arg_files []string
Sundong Ahn054b19a2018-10-19 13:46:09 +09001694 Args *string
Sundong Ahn054b19a2018-10-19 13:46:09 +09001695 Java_version *string
Paul Duffine22c2ab2020-05-20 19:35:27 +01001696 Annotations_enabled *bool
Sundong Ahn054b19a2018-10-19 13:46:09 +09001697 Merge_annotations_dirs []string
1698 Merge_inclusion_annotations_dirs []string
Paul Duffin0ff08bd2020-04-29 13:30:54 +01001699 Generate_stubs *bool
Anton Hanssone87b03d2020-12-21 15:29:34 +00001700 Previous_api *string
Sundong Ahn054b19a2018-10-19 13:46:09 +09001701 Check_api struct {
Anton Hanssone6056152020-12-31 10:37:27 +00001702 Current ApiToCheck
1703 Last_released ApiToCheck
Paul Duffin160fe412020-05-10 19:32:20 +01001704
1705 Api_lint struct {
1706 Enabled *bool
1707 New_since *string
1708 Baseline_file *string
1709 }
Jiyong Park58c518b2018-05-12 22:29:12 +09001710 }
Sundong Ahn1b92c822018-05-29 11:35:17 +09001711 Aidl struct {
1712 Include_dirs []string
1713 Local_include_dirs []string
1714 }
Paul Duffin040e9062020-11-23 17:41:36 +00001715 Dists []android.Dist
Jiyong Parkc678ad32018-04-10 13:07:10 +09001716 }{}
1717
Paul Duffin7b78b4d2020-04-28 14:08:32 +01001718 // The stubs source processing uses the same compile time classpath when extracting the
1719 // API from the implementation library as it does when compiling it. i.e. the same
1720 // * sdk version
1721 // * system_modules
1722 // * libs (static_libs/libs)
Paul Duffin250e6192019-06-07 10:44:37 +01001723
Paul Duffin0ff08bd2020-04-29 13:30:54 +01001724 props.Name = proptools.StringPtr(name)
Anton Hansson944e77d2020-08-19 11:40:22 +01001725 props.Visibility = childModuleVisibility(module.sdkLibraryProperties.Stubs_source_visibility)
Paul Duffina18abc22020-05-16 18:54:24 +01001726 props.Srcs = append(props.Srcs, module.properties.Srcs...)
Anton Hanssonf8ea3722021-09-16 14:24:13 +01001727 props.Srcs = append(props.Srcs, module.sdkLibraryProperties.Api_srcs...)
Paul Duffina18abc22020-05-16 18:54:24 +01001728 props.Sdk_version = module.deviceProperties.Sdk_version
Jihoon Kang3198f3c2023-01-26 08:08:52 +00001729 props.Api_surface = &apiScope.name
Paul Duffina18abc22020-05-16 18:54:24 +01001730 props.System_modules = module.deviceProperties.System_modules
Jiyong Parkc678ad32018-04-10 13:07:10 +09001731 props.Installable = proptools.BoolPtr(false)
Sundong Ahne6f0b052018-06-05 16:46:14 +09001732 // A droiddoc module has only one Libs property and doesn't distinguish between
1733 // shared libs and static libs. So we need to add both of these libs to Libs property.
Paul Duffina18abc22020-05-16 18:54:24 +01001734 props.Libs = module.properties.Libs
1735 props.Libs = append(props.Libs, module.properties.Static_libs...)
Nikita Ioffed732da72022-11-21 12:38:25 +00001736 props.Libs = append(props.Libs, module.sdkLibraryProperties.Stub_only_libs...)
Mark White9421c4c2023-08-10 00:07:03 +00001737 props.Libs = append(props.Libs, module.scopeToProperties[apiScope].Libs...)
Paul Duffina18abc22020-05-16 18:54:24 +01001738 props.Aidl.Include_dirs = module.deviceProperties.Aidl.Include_dirs
1739 props.Aidl.Local_include_dirs = module.deviceProperties.Aidl.Local_include_dirs
1740 props.Java_version = module.properties.Java_version
Jiyong Parkc678ad32018-04-10 13:07:10 +09001741
Paul Duffine22c2ab2020-05-20 19:35:27 +01001742 props.Annotations_enabled = module.sdkLibraryProperties.Annotations_enabled
Sundong Ahn054b19a2018-10-19 13:46:09 +09001743 props.Merge_annotations_dirs = module.sdkLibraryProperties.Merge_annotations_dirs
1744 props.Merge_inclusion_annotations_dirs = module.sdkLibraryProperties.Merge_inclusion_annotations_dirs
1745
Paul Duffin6d0886e2020-04-07 18:49:53 +01001746 droidstubsArgs := []string{}
Paul Duffin235ffff2019-12-24 10:41:30 +00001747 if len(module.sdkLibraryProperties.Api_packages) != 0 {
Paul Duffin6d0886e2020-04-07 18:49:53 +01001748 droidstubsArgs = append(droidstubsArgs, "--stub-packages "+strings.Join(module.sdkLibraryProperties.Api_packages, ":"))
Paul Duffin235ffff2019-12-24 10:41:30 +00001749 }
1750 if len(module.sdkLibraryProperties.Hidden_api_packages) != 0 {
Paul Duffin6d0886e2020-04-07 18:49:53 +01001751 droidstubsArgs = append(droidstubsArgs,
Paul Duffin235ffff2019-12-24 10:41:30 +00001752 android.JoinWithPrefix(module.sdkLibraryProperties.Hidden_api_packages, " --hide-package "))
1753 }
Paul Duffin6d0886e2020-04-07 18:49:53 +01001754 droidstubsArgs = append(droidstubsArgs, module.sdkLibraryProperties.Droiddoc_options...)
Anton Hanssonfd1c0d22023-11-02 15:18:09 +00001755 disabledWarnings := []string{"HiddenSuperclass"}
1756 if proptools.BoolDefault(module.sdkLibraryProperties.Api_lint.Legacy_errors_allowed, true) {
1757 disabledWarnings = append(disabledWarnings,
1758 "BroadcastBehavior",
1759 "DeprecationMismatch",
1760 "MissingPermission",
1761 "SdkConstant",
1762 "Todo",
1763 )
Paul Duffin235ffff2019-12-24 10:41:30 +00001764 }
Paul Duffin6d0886e2020-04-07 18:49:53 +01001765 droidstubsArgs = append(droidstubsArgs, android.JoinWithPrefix(disabledWarnings, "--hide "))
Sundong Ahnfb2721f2018-09-17 13:23:09 +09001766
Paul Duffin6877e6d2020-09-25 19:59:14 +01001767 // Output Javadoc comments for public scope.
1768 if apiScope == apiScopePublic {
1769 props.Output_javadoc_comments = proptools.BoolPtr(true)
1770 }
1771
Paul Duffin1fb487d2020-04-07 18:50:10 +01001772 // Add in scope specific arguments.
Paul Duffin0ff08bd2020-04-29 13:30:54 +01001773 droidstubsArgs = append(droidstubsArgs, scopeSpecificDroidstubsArgs...)
Paul Duffin11512472019-02-11 15:55:17 +00001774 props.Arg_files = module.sdkLibraryProperties.Droiddoc_option_files
Paul Duffin6d0886e2020-04-07 18:49:53 +01001775 props.Args = proptools.StringPtr(strings.Join(droidstubsArgs, " "))
Jiyong Parkc678ad32018-04-10 13:07:10 +09001776
Paul Duffin15f34ef2020-07-20 18:04:44 +01001777 // List of APIs identified from the provided source files are created. They are later
1778 // compared against to the not-yet-released (a.k.a current) list of APIs and to the
1779 // last-released (a.k.a numbered) list of API.
1780 currentApiFileName := apiScope.apiFilePrefix + "current.txt"
1781 removedApiFileName := apiScope.apiFilePrefix + "removed.txt"
1782 apiDir := module.getApiDir()
1783 currentApiFileName = path.Join(apiDir, currentApiFileName)
1784 removedApiFileName = path.Join(apiDir, removedApiFileName)
Jiyong Parkc678ad32018-04-10 13:07:10 +09001785
Paul Duffin15f34ef2020-07-20 18:04:44 +01001786 // check against the not-yet-release API
1787 props.Check_api.Current.Api_file = proptools.StringPtr(currentApiFileName)
1788 props.Check_api.Current.Removed_api_file = proptools.StringPtr(removedApiFileName)
Jiyong Park58c518b2018-05-12 22:29:12 +09001789
Paul Duffin958806b2022-05-16 13:10:47 +00001790 if module.compareAgainstLatestApi(apiScope) {
Paul Duffin15f34ef2020-07-20 18:04:44 +01001791 // check against the latest released API
1792 latestApiFilegroupName := proptools.StringPtr(module.latestApiFilegroupName(apiScope))
Anton Hanssone87b03d2020-12-21 15:29:34 +00001793 props.Previous_api = latestApiFilegroupName
Paul Duffin15f34ef2020-07-20 18:04:44 +01001794 props.Check_api.Last_released.Api_file = latestApiFilegroupName
1795 props.Check_api.Last_released.Removed_api_file = proptools.StringPtr(
1796 module.latestRemovedApiFilegroupName(apiScope))
Jaewoong Jung1a97ee02021-03-09 13:25:02 -08001797 props.Check_api.Last_released.Baseline_file = proptools.StringPtr(
1798 module.latestIncompatibilitiesFilegroupName(apiScope))
Paul Duffin160fe412020-05-10 19:32:20 +01001799
Paul Duffin15f34ef2020-07-20 18:04:44 +01001800 if proptools.Bool(module.sdkLibraryProperties.Api_lint.Enabled) {
1801 // Enable api lint.
1802 props.Check_api.Api_lint.Enabled = proptools.BoolPtr(true)
1803 props.Check_api.Api_lint.New_since = latestApiFilegroupName
Paul Duffin160fe412020-05-10 19:32:20 +01001804
Paul Duffin15f34ef2020-07-20 18:04:44 +01001805 // If it exists then pass a lint-baseline.txt through to droidstubs.
1806 baselinePath := path.Join(apiDir, apiScope.apiFilePrefix+"lint-baseline.txt")
1807 baselinePathRelativeToRoot := path.Join(mctx.ModuleDir(), baselinePath)
1808 paths, err := mctx.GlobWithDeps(baselinePathRelativeToRoot, nil)
1809 if err != nil {
1810 mctx.ModuleErrorf("error checking for presence of %s: %s", baselinePathRelativeToRoot, err)
1811 }
1812 if len(paths) == 1 {
1813 props.Check_api.Api_lint.Baseline_file = proptools.StringPtr(baselinePath)
1814 } else if len(paths) != 0 {
1815 mctx.ModuleErrorf("error checking for presence of %s: expected one path, found: %v", baselinePathRelativeToRoot, paths)
Paul Duffin160fe412020-05-10 19:32:20 +01001816 }
1817 }
Paul Duffin15f34ef2020-07-20 18:04:44 +01001818 }
Jiyong Park58c518b2018-05-12 22:29:12 +09001819
Paul Duffin15f34ef2020-07-20 18:04:44 +01001820 if !Bool(module.sdkLibraryProperties.No_dist) {
Paul Duffin040e9062020-11-23 17:41:36 +00001821 // Dist the api txt and removed api txt artifacts for sdk builds.
1822 distDir := proptools.StringPtr(path.Join(module.apiDistPath(apiScope), "api"))
1823 for _, p := range []struct {
1824 tag string
1825 pattern string
1826 }{
1827 {tag: ".api.txt", pattern: "%s.txt"},
1828 {tag: ".removed-api.txt", pattern: "%s-removed.txt"},
1829 } {
1830 props.Dists = append(props.Dists, android.Dist{
1831 Targets: []string{"sdk", "win_sdk"},
1832 Dir: distDir,
1833 Dest: proptools.StringPtr(fmt.Sprintf(p.pattern, module.distStem())),
1834 Tag: proptools.StringPtr(p.tag),
1835 })
1836 }
Anton Hansson5fd5d242020-03-27 19:43:19 +00001837 }
1838
Spandan Das2cc80ba2023-10-27 17:21:52 +00001839 mctx.CreateModule(DroidstubsFactory, &props, module.sdkComponentPropertiesForChildLibrary()).(*Droidstubs).CallHookIfAvailable(mctx)
Jiyong Parkc678ad32018-04-10 13:07:10 +09001840}
1841
Jihoon Kang0c705a42023-08-02 06:44:57 +00001842func (module *SdkLibrary) createApiLibrary(mctx android.DefaultableHookContext, apiScope *apiScope, alternativeFullApiSurfaceStub string) {
Jihoon Kang1c92c3e2023-03-23 17:44:51 +00001843 props := struct {
Jihoon Kangca198c22023-06-22 23:13:51 +00001844 Name *string
1845 Visibility []string
1846 Api_contributions []string
1847 Libs []string
1848 Static_libs []string
1849 Full_api_surface_stub *string
Jihoon Kang4ec24872023-10-05 17:26:09 +00001850 System_modules *string
Jihoon Kang063ec002023-06-28 01:16:23 +00001851 Enable_validation *bool
Jihoon Kang1c92c3e2023-03-23 17:44:51 +00001852 }{}
1853
1854 props.Name = proptools.StringPtr(module.apiLibraryModuleName(apiScope))
Jihoon Kang786df932023-09-07 01:18:31 +00001855 props.Visibility = []string{"//visibility:override", "//visibility:private"}
Jihoon Kang1c92c3e2023-03-23 17:44:51 +00001856
1857 apiContributions := []string{}
1858
1859 // Api surfaces are not independent of each other, but have subset relationships,
1860 // and so does the api files. To generate from-text stubs for api surfaces other than public,
1861 // all subset api domains' api_contriubtions must be added as well.
1862 scope := apiScope
1863 for scope != nil {
1864 apiContributions = append(apiContributions, module.stubsSourceModuleName(scope)+".api.contribution")
1865 scope = scope.extends
1866 }
Jihoon Kang0c705a42023-08-02 06:44:57 +00001867 if apiScope == apiScopePublic {
1868 additionalApiContribution := module.apiLibraryAdditionalApiContribution()
1869 if additionalApiContribution != "" {
1870 apiContributions = append(apiContributions, additionalApiContribution)
1871 }
1872 }
Jihoon Kang1c92c3e2023-03-23 17:44:51 +00001873
1874 props.Api_contributions = apiContributions
1875 props.Libs = module.properties.Libs
1876 props.Libs = append(props.Libs, module.sdkLibraryProperties.Stub_only_libs...)
Mark White9421c4c2023-08-10 00:07:03 +00001877 props.Libs = append(props.Libs, module.scopeToProperties[apiScope].Libs...)
Jihoon Kang1c92c3e2023-03-23 17:44:51 +00001878 props.Libs = append(props.Libs, "stub-annotations")
1879 props.Static_libs = module.sdkLibraryProperties.Stub_only_static_libs
Jihoon Kange7ee2562023-07-25 05:51:46 +00001880 props.Full_api_surface_stub = proptools.StringPtr(apiScope.kind.DefaultJavaLibraryName())
Jihoon Kang0c705a42023-08-02 06:44:57 +00001881 if alternativeFullApiSurfaceStub != "" {
1882 props.Full_api_surface_stub = proptools.StringPtr(alternativeFullApiSurfaceStub)
1883 }
Jihoon Kang1c92c3e2023-03-23 17:44:51 +00001884
1885 // android_module_lib_stubs_current.from-text only comprises api contributions from art, conscrypt and i18n.
1886 // Thus, replace with android_module_lib_stubs_current_full.from-text, which comprises every api domains.
1887 if apiScope.kind == android.SdkModule {
Jihoon Kangca198c22023-06-22 23:13:51 +00001888 props.Full_api_surface_stub = proptools.StringPtr(apiScope.kind.DefaultJavaLibraryName() + "_full.from-text")
Jihoon Kang1c92c3e2023-03-23 17:44:51 +00001889 }
1890
Jihoon Kangd30ac8a2023-10-09 18:00:17 +00001891 // java_sdk_library modules that set sdk_version as none does not depend on other api
1892 // domains. Therefore, java_api_library created from such modules should not depend on
1893 // full_api_surface_stubs but create and compile stubs by the java_api_library module
1894 // itself.
1895 if module.SdkVersion(mctx).Kind == android.SdkNone {
1896 props.Full_api_surface_stub = nil
1897 }
1898
Jihoon Kang4ec24872023-10-05 17:26:09 +00001899 props.System_modules = module.deviceProperties.System_modules
Jihoon Kang063ec002023-06-28 01:16:23 +00001900 props.Enable_validation = proptools.BoolPtr(true)
Jihoon Kang4ec24872023-10-05 17:26:09 +00001901
Spandan Das2cc80ba2023-10-27 17:21:52 +00001902 mctx.CreateModule(ApiLibraryFactory, &props, module.sdkComponentPropertiesForChildLibrary())
Jihoon Kang1c92c3e2023-03-23 17:44:51 +00001903}
1904
Jihoon Kang1147b312023-06-08 23:25:57 +00001905func (module *SdkLibrary) createTopLevelStubsLibrary(
1906 mctx android.DefaultableHookContext, apiScope *apiScope, contributesToApiSurface bool) {
1907 props := struct {
1908 Name *string
1909 Visibility []string
1910 Sdk_version *string
1911 Static_libs []string
1912 System_modules *string
1913 Dist struct {
1914 Targets []string
1915 Dest *string
1916 Dir *string
1917 Tag *string
1918 }
1919 Compile_dex *bool
1920 }{}
1921 props.Name = proptools.StringPtr(module.stubsLibraryModuleName(apiScope))
1922 props.Visibility = childModuleVisibility(module.sdkLibraryProperties.Stubs_library_visibility)
1923 sdkVersion := module.sdkVersionForStubsLibrary(mctx, apiScope)
1924 props.Sdk_version = proptools.StringPtr(sdkVersion)
1925
1926 // Add the stub compiling java_library/java_api_library as static lib based on build config
1927 staticLib := module.sourceStubLibraryModuleName(apiScope)
1928 if mctx.Config().BuildFromTextStub() && contributesToApiSurface {
1929 staticLib = module.apiLibraryModuleName(apiScope)
1930 }
1931 props.Static_libs = append(props.Static_libs, staticLib)
1932 props.System_modules = module.deviceProperties.System_modules
1933
1934 // Dist the class jar artifact for sdk builds.
1935 if !Bool(module.sdkLibraryProperties.No_dist) {
1936 props.Dist.Targets = []string{"sdk", "win_sdk"}
1937 props.Dist.Dest = proptools.StringPtr(fmt.Sprintf("%v.jar", module.distStem()))
1938 props.Dist.Dir = proptools.StringPtr(module.apiDistPath(apiScope))
1939 props.Dist.Tag = proptools.StringPtr(".jar")
1940 }
1941
1942 // The imports need to be compiled to dex if the java_sdk_library requests it.
1943 compileDex := module.dexProperties.Compile_dex
1944 if module.stubLibrariesCompiledForDex() {
1945 compileDex = proptools.BoolPtr(true)
1946 }
1947 props.Compile_dex = compileDex
1948
1949 mctx.CreateModule(LibraryFactory, &props, module.sdkComponentPropertiesForChildLibrary())
1950}
1951
Paul Duffin958806b2022-05-16 13:10:47 +00001952func (module *SdkLibrary) compareAgainstLatestApi(apiScope *apiScope) bool {
1953 return !(apiScope.unstable || module.sdkLibraryProperties.Unsafe_ignore_missing_latest_api)
1954}
1955
Paul Duffinea8f8082021-06-24 13:25:57 +01001956// Implements android.ApexModule
Jooyung Han5e9013b2020-03-10 06:23:13 +09001957func (module *SdkLibrary) DepIsInSameApex(mctx android.BaseModuleContext, dep android.Module) bool {
1958 depTag := mctx.OtherModuleDependencyTag(dep)
1959 if depTag == xmlPermissionsFileTag {
1960 return true
1961 }
1962 return module.Library.DepIsInSameApex(mctx, dep)
1963}
1964
Paul Duffinea8f8082021-06-24 13:25:57 +01001965// Implements android.ApexModule
1966func (module *SdkLibrary) UniqueApexVariations() bool {
1967 return module.uniqueApexVariations()
1968}
1969
Jihoon Kang80456fd2023-11-15 19:22:14 +00001970func (module *SdkLibrary) ContributeToApi() bool {
1971 return proptools.BoolDefault(module.sdkLibraryProperties.Contribute_to_android_api, false)
1972}
1973
Jiyong Parkc678ad32018-04-10 13:07:10 +09001974// Creates the xml file that publicizes the runtime library
Paul Duffinf0229202020-04-29 16:47:28 +01001975func (module *SdkLibrary) createXmlFile(mctx android.DefaultableHookContext) {
Spandan Das8c9ae7e2023-03-03 21:20:36 +00001976 moduleMinApiLevel := module.Library.MinSdkVersion(mctx)
Pedro Loureiroc3621422021-09-28 15:40:23 +00001977 var moduleMinApiLevelStr = moduleMinApiLevel.String()
1978 if moduleMinApiLevel == android.NoneApiLevel {
1979 moduleMinApiLevelStr = "current"
1980 }
Jiyong Parke3833882020-02-17 17:28:10 +09001981 props := struct {
Pedro Loureiroc3621422021-09-28 15:40:23 +00001982 Name *string
1983 Lib_name *string
1984 Apex_available []string
1985 On_bootclasspath_since *string
1986 On_bootclasspath_before *string
1987 Min_device_sdk *string
1988 Max_device_sdk *string
1989 Sdk_library_min_api_level *string
Jamie Garsidee570ace2023-11-27 12:07:36 +00001990 Uses_libs_dependencies []string
Jiyong Parke3833882020-02-17 17:28:10 +09001991 }{
Pedro Loureiroc3621422021-09-28 15:40:23 +00001992 Name: proptools.StringPtr(module.xmlPermissionsModuleName()),
1993 Lib_name: proptools.StringPtr(module.BaseModuleName()),
1994 Apex_available: module.ApexProperties.Apex_available,
1995 On_bootclasspath_since: module.commonSdkLibraryProperties.On_bootclasspath_since,
1996 On_bootclasspath_before: module.commonSdkLibraryProperties.On_bootclasspath_before,
1997 Min_device_sdk: module.commonSdkLibraryProperties.Min_device_sdk,
1998 Max_device_sdk: module.commonSdkLibraryProperties.Max_device_sdk,
1999 Sdk_library_min_api_level: &moduleMinApiLevelStr,
Jamie Garsidee570ace2023-11-27 12:07:36 +00002000 Uses_libs_dependencies: module.usesLibraryProperties.Uses_libs,
Jiyong Parkc678ad32018-04-10 13:07:10 +09002001 }
Jiyong Parke3833882020-02-17 17:28:10 +09002002
Jiyong Parke3833882020-02-17 17:28:10 +09002003 mctx.CreateModule(sdkLibraryXmlFactory, &props)
Jiyong Parkc678ad32018-04-10 13:07:10 +09002004}
2005
Jiyong Parkf1691d22021-03-29 20:11:58 +09002006func PrebuiltJars(ctx android.BaseModuleContext, baseName string, s android.SdkSpec) android.Paths {
Jiyong Park54105c42021-03-31 18:17:53 +09002007 var ver android.ApiLevel
Jiyong Parkf1691d22021-03-29 20:11:58 +09002008 var kind android.SdkKind
2009 if s.UsePrebuilt(ctx) {
Jiyong Park54105c42021-03-31 18:17:53 +09002010 ver = s.ApiLevel
Jiyong Parkf1691d22021-03-29 20:11:58 +09002011 kind = s.Kind
Jiyong Parkc678ad32018-04-10 13:07:10 +09002012 } else {
Jiyong Park6a927c42020-01-21 02:03:43 +09002013 // We don't have prebuilt SDK for the specific sdkVersion.
2014 // Instead of breaking the build, fallback to use "system_current"
Jiyong Park54105c42021-03-31 18:17:53 +09002015 ver = android.FutureApiLevel
Jiyong Parkf1691d22021-03-29 20:11:58 +09002016 kind = android.SdkSystem
Sundong Ahn054b19a2018-10-19 13:46:09 +09002017 }
Jiyong Park6a927c42020-01-21 02:03:43 +09002018
2019 dir := filepath.Join("prebuilts", "sdk", ver.String(), kind.String())
Paul Duffin50061512020-01-21 16:31:05 +00002020 jar := filepath.Join(dir, baseName+".jar")
Sundong Ahn054b19a2018-10-19 13:46:09 +09002021 jarPath := android.ExistentPathForSource(ctx, jar)
Sundong Ahnae418ac2019-02-28 15:01:28 +09002022 if !jarPath.Valid() {
Colin Cross07c88562020-01-07 09:34:44 -08002023 if ctx.Config().AllowMissingDependencies() {
2024 return android.Paths{android.PathForSource(ctx, jar)}
2025 } else {
Jiyong Parkf1691d22021-03-29 20:11:58 +09002026 ctx.PropertyErrorf("sdk_library", "invalid sdk version %q, %q does not exist", s.Raw, jar)
Colin Cross07c88562020-01-07 09:34:44 -08002027 }
Sundong Ahnae418ac2019-02-28 15:01:28 +09002028 return nil
2029 }
Sundong Ahn054b19a2018-10-19 13:46:09 +09002030 return android.Paths{jarPath.Path()}
2031}
2032
Colin Crossaede88c2020-08-11 12:17:01 -07002033// 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 +01002034//
2035// If either this or the other module are on the platform then this will return
2036// false.
Colin Cross56a83212020-09-15 18:30:11 -07002037func withinSameApexesAs(ctx android.BaseModuleContext, other android.Module) bool {
Colin Crossff694a82023-12-13 15:54:49 -08002038 apexInfo, _ := android.ModuleProvider(ctx, android.ApexInfoProvider)
Colin Cross313aa542023-12-13 13:47:44 -08002039 otherApexInfo, _ := android.OtherModuleProvider(ctx, other, android.ApexInfoProvider)
Jiyong Parkab50b072021-05-12 17:13:56 +09002040 return len(otherApexInfo.InApexVariants) > 0 && reflect.DeepEqual(apexInfo.InApexVariants, otherApexInfo.InApexVariants)
Paul Duffin9b879592020-05-26 13:21:35 +01002041}
2042
Jiyong Parkf1691d22021-03-29 20:11:58 +09002043func (module *SdkLibrary) sdkJars(ctx android.BaseModuleContext, sdkVersion android.SdkSpec, headerJars bool) android.Paths {
Jiyong Park932cdfe2020-05-28 00:19:53 +09002044 // If the client doesn't set sdk_version, but if this library prefers stubs over
2045 // the impl library, let's provide the widest API surface possible. To do so,
2046 // force override sdk_version to module_current so that the closest possible API
2047 // surface could be found in selectHeaderJarsForSdkVersion
Jiyong Parkf1691d22021-03-29 20:11:58 +09002048 if module.defaultsToStubs() && !sdkVersion.Specified() {
Jiyong Park92315372021-04-02 08:45:46 +09002049 sdkVersion = android.SdkSpecFrom(ctx, "module_current")
Jiyong Park932cdfe2020-05-28 00:19:53 +09002050 }
Paul Duffind1b3a922020-01-22 11:57:20 +00002051
Paul Duffindaaa3322020-05-26 18:13:57 +01002052 // Only provide access to the implementation library if it is actually built.
2053 if module.requiresRuntimeImplementationLibrary() {
2054 // Check any special cases for java_sdk_library.
2055 //
2056 // Only allow access to the implementation library in the following condition:
2057 // * No sdk_version specified on the referencing module.
Paul Duffin9b879592020-05-26 13:21:35 +01002058 // * The referencing module is in the same apex as this.
Jiyong Parkf1691d22021-03-29 20:11:58 +09002059 if sdkVersion.Kind == android.SdkPrivate || withinSameApexesAs(ctx, module) {
Paul Duffindaaa3322020-05-26 18:13:57 +01002060 if headerJars {
2061 return module.HeaderJars()
2062 } else {
2063 return module.ImplementationJars()
2064 }
Sundong Ahn054b19a2018-10-19 13:46:09 +09002065 }
Jiyong Parkc678ad32018-04-10 13:07:10 +09002066 }
Paul Duffinb05d4292020-05-20 12:19:10 +01002067
Paul Duffin23970f42020-05-20 14:20:02 +01002068 return module.selectHeaderJarsForSdkVersion(ctx, sdkVersion)
Jiyong Parkc678ad32018-04-10 13:07:10 +09002069}
2070
Sundong Ahn241cd372018-07-13 16:16:44 +09002071// to satisfy SdkLibraryDependency interface
Jiyong Parkf1691d22021-03-29 20:11:58 +09002072func (module *SdkLibrary) SdkHeaderJars(ctx android.BaseModuleContext, sdkVersion android.SdkSpec) android.Paths {
Paul Duffind1b3a922020-01-22 11:57:20 +00002073 return module.sdkJars(ctx, sdkVersion, true /*headerJars*/)
2074}
2075
2076// to satisfy SdkLibraryDependency interface
Jiyong Parkf1691d22021-03-29 20:11:58 +09002077func (module *SdkLibrary) SdkImplementationJars(ctx android.BaseModuleContext, sdkVersion android.SdkSpec) android.Paths {
Paul Duffind1b3a922020-01-22 11:57:20 +00002078 return module.sdkJars(ctx, sdkVersion, false /*headerJars*/)
Sundong Ahn241cd372018-07-13 16:16:44 +09002079}
2080
Colin Cross571cccf2019-02-04 11:22:08 -08002081var javaSdkLibrariesKey = android.NewOnceKey("javaSdkLibraries")
2082
Jiyong Park82484c02018-04-23 21:41:26 +09002083func javaSdkLibraries(config android.Config) *[]string {
Colin Cross571cccf2019-02-04 11:22:08 -08002084 return config.Once(javaSdkLibrariesKey, func() interface{} {
Jiyong Park82484c02018-04-23 21:41:26 +09002085 return &[]string{}
2086 }).(*[]string)
2087}
2088
Paul Duffin749f98f2019-12-30 17:23:46 +00002089func (module *SdkLibrary) getApiDir() string {
2090 return proptools.StringDefault(module.sdkLibraryProperties.Api_dir, "api")
2091}
2092
Jiyong Parkc678ad32018-04-10 13:07:10 +09002093// For a java_sdk_library module, create internal modules for stubs, docs,
2094// runtime libs and xml file. If requested, the stubs and docs are created twice
2095// once for public API level and once for system API level
Paul Duffinf0229202020-04-29 16:47:28 +01002096func (module *SdkLibrary) CreateInternalModules(mctx android.DefaultableHookContext) {
2097 // If the module has been disabled then don't create any child modules.
2098 if !module.Enabled() {
2099 return
2100 }
2101
Paul Duffina18abc22020-05-16 18:54:24 +01002102 if len(module.properties.Srcs) == 0 {
Inseob Kimc0907f12019-02-08 21:00:45 +09002103 mctx.PropertyErrorf("srcs", "java_sdk_library must specify srcs")
Jooyung Han58f26ab2019-12-18 15:34:32 +09002104 return
Inseob Kimc0907f12019-02-08 21:00:45 +09002105 }
2106
Paul Duffin37e0b772019-12-30 17:20:10 +00002107 // If this builds against standard libraries (i.e. is not part of the core libraries)
Paul Duffin4f5c1ef2020-11-19 14:53:43 +00002108 // then assume it provides both system and test apis.
Jiyong Parkf1691d22021-03-29 20:11:58 +09002109 sdkDep := decodeSdkDep(mctx, android.SdkContext(&module.Library))
Paul Duffin37e0b772019-12-30 17:20:10 +00002110 hasSystemAndTestApis := sdkDep.hasStandardLibs()
Paul Duffin3375e352020-04-28 10:44:03 +01002111 module.sdkLibraryProperties.Generate_system_and_test_apis = hasSystemAndTestApis
Paul Duffin4f5c1ef2020-11-19 14:53:43 +00002112
Jaewoong Jung18aefc12020-12-21 09:11:10 -08002113 missingCurrentApi := false
Inseob Kim8098faa2019-03-18 10:19:51 +09002114
Paul Duffin3375e352020-04-28 10:44:03 +01002115 generatedScopes := module.getGeneratedApiScopes(mctx)
Paul Duffind1b3a922020-01-22 11:57:20 +00002116
Paul Duffin749f98f2019-12-30 17:23:46 +00002117 apiDir := module.getApiDir()
Paul Duffin3375e352020-04-28 10:44:03 +01002118 for _, scope := range generatedScopes {
Inseob Kim8098faa2019-03-18 10:19:51 +09002119 for _, api := range []string{"current.txt", "removed.txt"} {
Paul Duffind1b3a922020-01-22 11:57:20 +00002120 path := path.Join(mctx.ModuleDir(), apiDir, scope.apiFilePrefix+api)
Inseob Kim8098faa2019-03-18 10:19:51 +09002121 p := android.ExistentPathForSource(mctx, path)
2122 if !p.Valid() {
Colin Cross18f840c2021-05-20 17:56:54 -07002123 if mctx.Config().AllowMissingDependencies() {
2124 mctx.AddMissingDependencies([]string{path})
2125 } else {
2126 mctx.ModuleErrorf("Current api file %#v doesn't exist", path)
2127 missingCurrentApi = true
2128 }
Inseob Kim8098faa2019-03-18 10:19:51 +09002129 }
2130 }
2131 }
2132
Jaewoong Jung18aefc12020-12-21 09:11:10 -08002133 if missingCurrentApi {
Inseob Kim8098faa2019-03-18 10:19:51 +09002134 script := "build/soong/scripts/gen-java-current-api-files.sh"
2135 p := android.ExistentPathForSource(mctx, script)
2136
2137 if !p.Valid() {
2138 panic(fmt.Sprintf("script file %s doesn't exist", script))
2139 }
2140
2141 mctx.ModuleErrorf("One or more current api files are missing. "+
2142 "You can update them by:\n"+
Paul Duffin37e0b772019-12-30 17:20:10 +00002143 "%s %q %s && m update-api",
Paul Duffind1b3a922020-01-22 11:57:20 +00002144 script, filepath.Join(mctx.ModuleDir(), apiDir),
Paul Duffin3375e352020-04-28 10:44:03 +01002145 strings.Join(generatedScopes.Strings(func(s *apiScope) string { return s.apiFilePrefix }), " "))
Inseob Kim8098faa2019-03-18 10:19:51 +09002146 return
2147 }
2148
Paul Duffin3375e352020-04-28 10:44:03 +01002149 for _, scope := range generatedScopes {
Paul Duffin15f34ef2020-07-20 18:04:44 +01002150 // Use the stubs source name for legacy reasons.
2151 module.createStubsSourcesAndApi(mctx, scope, module.stubsSourceModuleName(scope), scope.droidstubsArgs)
Paul Duffin0ff08bd2020-04-29 13:30:54 +01002152
Paul Duffind1b3a922020-01-22 11:57:20 +00002153 module.createStubsLibrary(mctx, scope)
Jihoon Kang1c92c3e2023-03-23 17:44:51 +00002154
Jihoon Kang0c705a42023-08-02 06:44:57 +00002155 alternativeFullApiSurfaceStubLib := ""
2156 if scope == apiScopePublic {
2157 alternativeFullApiSurfaceStubLib = module.alternativeFullApiSurfaceStubLib()
2158 }
2159 contributesToApiSurface := module.contributesToApiSurface(mctx.Config()) || alternativeFullApiSurfaceStubLib != ""
Jihoon Kang1147b312023-06-08 23:25:57 +00002160 if contributesToApiSurface {
Jihoon Kang0c705a42023-08-02 06:44:57 +00002161 module.createApiLibrary(mctx, scope, alternativeFullApiSurfaceStubLib)
Jihoon Kang1c92c3e2023-03-23 17:44:51 +00002162 }
Jihoon Kang1147b312023-06-08 23:25:57 +00002163
2164 module.createTopLevelStubsLibrary(mctx, scope, contributesToApiSurface)
Inseob Kimc0907f12019-02-08 21:00:45 +09002165 }
2166
Paul Duffindfa131e2020-05-15 20:37:11 +01002167 if module.requiresRuntimeImplementationLibrary() {
Paul Duffin5df79302020-05-16 15:52:12 +01002168 // Create child module to create an implementation library.
2169 //
2170 // This temporarily creates a second implementation library that can be explicitly
2171 // referenced.
2172 //
2173 // TODO(b/156618935) - update comment once only one implementation library is created.
2174 module.createImplLibrary(mctx)
2175
Paul Duffindfa131e2020-05-15 20:37:11 +01002176 // Only create an XML permissions file that declares the library as being usable
2177 // as a shared library if required.
2178 if module.sharedLibrary() {
2179 module.createXmlFile(mctx)
2180 }
Paul Duffin43db9be2019-12-30 17:35:49 +00002181
2182 // record java_sdk_library modules so that they are exported to make
2183 javaSdkLibraries := javaSdkLibraries(mctx.Config())
2184 javaSdkLibrariesLock.Lock()
2185 defer javaSdkLibrariesLock.Unlock()
2186 *javaSdkLibraries = append(*javaSdkLibraries, module.BaseModuleName())
2187 }
Anton Hansson7f66efa2020-10-08 14:47:23 +01002188
Paul Duffin77590a82022-04-28 14:13:30 +00002189 // 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 +01002190 module.properties.Libs = append(module.properties.Libs, module.sdkLibraryProperties.Impl_only_libs...)
Paul Duffin77590a82022-04-28 14:13:30 +00002191 module.properties.Static_libs = append(module.properties.Static_libs, module.sdkLibraryProperties.Impl_only_static_libs...)
Inseob Kimc0907f12019-02-08 21:00:45 +09002192}
2193
2194func (module *SdkLibrary) InitSdkLibraryProperties() {
Colin Crossce6734e2020-06-15 16:09:53 -07002195 module.addHostAndDeviceProperties()
2196 module.AddProperties(&module.sdkLibraryProperties)
Sundong Ahn054b19a2018-10-19 13:46:09 +09002197
Paul Duffin71b33cc2021-06-23 11:39:47 +01002198 module.initSdkLibraryComponent(module)
Paul Duffin859fe962020-05-15 10:20:31 +01002199
Paul Duffina18abc22020-05-16 18:54:24 +01002200 module.properties.Installable = proptools.BoolPtr(true)
2201 module.deviceProperties.IsSDKLibrary = true
Inseob Kimc0907f12019-02-08 21:00:45 +09002202}
Sundong Ahn054b19a2018-10-19 13:46:09 +09002203
Paul Duffindfa131e2020-05-15 20:37:11 +01002204func (module *SdkLibrary) requiresRuntimeImplementationLibrary() bool {
2205 return !proptools.Bool(module.sdkLibraryProperties.Api_only)
2206}
2207
Jiyong Park932cdfe2020-05-28 00:19:53 +09002208func (module *SdkLibrary) defaultsToStubs() bool {
2209 return proptools.Bool(module.sdkLibraryProperties.Default_to_stubs)
2210}
2211
Paul Duffin1b1e8062020-05-08 13:44:43 +01002212// Defines how to name the individual component modules the sdk library creates.
2213type sdkLibraryComponentNamingScheme interface {
2214 stubsLibraryModuleName(scope *apiScope, baseName string) string
2215
2216 stubsSourceModuleName(scope *apiScope, baseName string) string
Jihoon Kang1c92c3e2023-03-23 17:44:51 +00002217
2218 apiLibraryModuleName(scope *apiScope, baseName string) string
Jihoon Kang1147b312023-06-08 23:25:57 +00002219
2220 sourceStubLibraryModuleName(scope *apiScope, baseName string) string
Paul Duffin1b1e8062020-05-08 13:44:43 +01002221}
2222
2223type defaultNamingScheme struct {
2224}
2225
2226func (s *defaultNamingScheme) stubsLibraryModuleName(scope *apiScope, baseName string) string {
2227 return scope.stubsLibraryModuleName(baseName)
2228}
2229
2230func (s *defaultNamingScheme) stubsSourceModuleName(scope *apiScope, baseName string) string {
2231 return scope.stubsSourceModuleName(baseName)
2232}
2233
Jihoon Kang1c92c3e2023-03-23 17:44:51 +00002234func (s *defaultNamingScheme) apiLibraryModuleName(scope *apiScope, baseName string) string {
2235 return scope.apiLibraryModuleName(baseName)
2236}
2237
Jihoon Kang1147b312023-06-08 23:25:57 +00002238func (s *defaultNamingScheme) sourceStubLibraryModuleName(scope *apiScope, baseName string) string {
2239 return scope.sourceStubLibraryModuleName(baseName)
2240}
2241
Paul Duffin1b1e8062020-05-08 13:44:43 +01002242var _ sdkLibraryComponentNamingScheme = (*defaultNamingScheme)(nil)
2243
Jaewoong Jungbc15e3a2021-03-10 17:02:43 -08002244func moduleStubLinkType(name string) (stub bool, ret sdkLinkType) {
Jihoon Kang1147b312023-06-08 23:25:57 +00002245 name = strings.TrimSuffix(name, ".from-source")
2246
Anton Hansson2d0c1942020-05-25 12:20:51 +01002247 // This suffix-based approach is fragile and could potentially mis-trigger.
2248 // TODO(b/155164730): Clean this up when modules no longer reference sdk_lib stubs directly.
Anton Hansson08f476b2021-04-07 15:32:19 +01002249 if strings.HasSuffix(name, apiScopePublic.stubsLibraryModuleNameSuffix()) {
2250 if name == "hwbinder.stubs" || name == "libcore_private.stubs" {
2251 // Due to a previous bug, these modules were not considered stubs, so we retain that.
2252 return false, javaPlatform
2253 }
Anton Hansson2d0c1942020-05-25 12:20:51 +01002254 return true, javaSdk
2255 }
Anton Hansson08f476b2021-04-07 15:32:19 +01002256 if strings.HasSuffix(name, apiScopeSystem.stubsLibraryModuleNameSuffix()) {
Anton Hansson2d0c1942020-05-25 12:20:51 +01002257 return true, javaSystem
2258 }
Anton Hansson08f476b2021-04-07 15:32:19 +01002259 if strings.HasSuffix(name, apiScopeModuleLib.stubsLibraryModuleNameSuffix()) {
Anton Hansson2d0c1942020-05-25 12:20:51 +01002260 return true, javaModule
2261 }
Anton Hansson08f476b2021-04-07 15:32:19 +01002262 if strings.HasSuffix(name, apiScopeTest.stubsLibraryModuleNameSuffix()) {
Anton Hansson2d0c1942020-05-25 12:20:51 +01002263 return true, javaSystem
2264 }
Jihoon Kang1147b312023-06-08 23:25:57 +00002265 if strings.HasSuffix(name, apiScopeSystemServer.stubsLibraryModuleNameSuffix()) {
2266 return true, javaSystemServer
2267 }
Anton Hansson2d0c1942020-05-25 12:20:51 +01002268 return false, javaPlatform
2269}
2270
Jaewoong Jung4f158ee2019-07-11 10:05:35 -07002271// java_sdk_library is a special Java library that provides optional platform APIs to apps.
2272// In practice, it can be viewed as a combination of several modules: 1) stubs library that clients
2273// are linked against to, 2) droiddoc module that internally generates API stubs source files,
2274// 3) the real runtime shared library that implements the APIs, and 4) XML file for adding
2275// the runtime lib to the classpath at runtime if requested via <uses-library>.
Inseob Kimc0907f12019-02-08 21:00:45 +09002276func SdkLibraryFactory() android.Module {
2277 module := &SdkLibrary{}
Paul Duffinc3091c82020-05-08 14:16:20 +01002278
2279 // Initialize information common between source and prebuilt.
Paul Duffin71b33cc2021-06-23 11:39:47 +01002280 module.initCommon(module)
Paul Duffinc3091c82020-05-08 14:16:20 +01002281
Inseob Kimc0907f12019-02-08 21:00:45 +09002282 module.InitSdkLibraryProperties()
Jooyung Han58f26ab2019-12-18 15:34:32 +09002283 android.InitApexModule(module)
Sundong Ahn054b19a2018-10-19 13:46:09 +09002284 InitJavaModule(module, android.HostAndDeviceSupported)
Paul Duffin3375e352020-04-28 10:44:03 +01002285
2286 // Initialize the map from scope to scope specific properties.
2287 scopeToProperties := make(map[*apiScope]*ApiScopeProperties)
2288 for _, scope := range allApiScopes {
2289 scopeToProperties[scope] = scope.scopeSpecificProperties(module)
2290 }
2291 module.scopeToProperties = scopeToProperties
2292
Paul Duffin4911a892020-04-29 23:35:13 +01002293 // Add the properties containing visibility rules so that they are checked.
Paul Duffin5df79302020-05-16 15:52:12 +01002294 android.AddVisibilityProperty(module, "impl_library_visibility", &module.sdkLibraryProperties.Impl_library_visibility)
Paul Duffin4911a892020-04-29 23:35:13 +01002295 android.AddVisibilityProperty(module, "stubs_library_visibility", &module.sdkLibraryProperties.Stubs_library_visibility)
2296 android.AddVisibilityProperty(module, "stubs_source_visibility", &module.sdkLibraryProperties.Stubs_source_visibility)
2297
Paul Duffin1b1e8062020-05-08 13:44:43 +01002298 module.SetDefaultableHook(func(ctx android.DefaultableHookContext) {
Paul Duffindfa131e2020-05-15 20:37:11 +01002299 // If no implementation is required then it cannot be used as a shared library
2300 // either.
2301 if !module.requiresRuntimeImplementationLibrary() {
2302 // If shared_library has been explicitly set to true then it is incompatible
2303 // with api_only: true.
2304 if proptools.Bool(module.commonSdkLibraryProperties.Shared_library) {
2305 ctx.PropertyErrorf("api_only/shared_library", "inconsistent settings, shared_library and api_only cannot both be true")
2306 }
2307 // Set shared_library: false.
2308 module.commonSdkLibraryProperties.Shared_library = proptools.BoolPtr(false)
2309 }
2310
Paul Duffin1b1e8062020-05-08 13:44:43 +01002311 if module.initCommonAfterDefaultsApplied(ctx) {
2312 module.CreateInternalModules(ctx)
2313 }
2314 })
Jiyong Parkc678ad32018-04-10 13:07:10 +09002315 return module
2316}
Colin Cross79c7c262019-04-17 11:11:46 -07002317
2318//
2319// SDK library prebuilts
2320//
2321
Paul Duffin56d44902020-01-31 13:36:25 +00002322// Properties associated with each api scope.
2323type sdkLibraryScopeProperties struct {
Colin Cross79c7c262019-04-17 11:11:46 -07002324 Jars []string `android:"path"`
2325
2326 Sdk_version *string
2327
Colin Cross79c7c262019-04-17 11:11:46 -07002328 // List of shared java libs that this module has dependencies to
2329 Libs []string
Paul Duffin3d1248c2020-04-09 00:10:17 +01002330
Paul Duffinc8782502020-04-29 20:45:27 +01002331 // The stubs source.
Paul Duffin3d1248c2020-04-09 00:10:17 +01002332 Stub_srcs []string `android:"path"`
Paul Duffin1fd005d2020-04-09 01:08:11 +01002333
2334 // The current.txt
Paul Duffin0f8faff2020-05-20 16:18:00 +01002335 Current_api *string `android:"path"`
Paul Duffin1fd005d2020-04-09 01:08:11 +01002336
2337 // The removed.txt
Paul Duffin0f8faff2020-05-20 16:18:00 +01002338 Removed_api *string `android:"path"`
Anton Hanssond78eb762021-09-21 15:25:12 +01002339
2340 // Annotation zip
2341 Annotations *string `android:"path"`
Colin Cross79c7c262019-04-17 11:11:46 -07002342}
2343
Paul Duffin56d44902020-01-31 13:36:25 +00002344type sdkLibraryImportProperties struct {
Paul Duffinfcfd7912020-01-31 17:54:30 +00002345 // List of shared java libs, common to all scopes, that this module has
2346 // dependencies to
2347 Libs []string
Paul Duffin1267d872021-04-16 17:21:36 +01002348
2349 // If set to true, compile dex files for the stubs. Defaults to false.
2350 Compile_dex *bool
Paul Duffin869de142021-07-15 14:14:41 +01002351
2352 // If not empty, classes are restricted to the specified packages and their sub-packages.
Paul Duffin869de142021-07-15 14:14:41 +01002353 Permitted_packages []string
Paul Duffin56d44902020-01-31 13:36:25 +00002354}
2355
Paul Duffineedc5d52020-06-12 17:46:39 +01002356type SdkLibraryImport struct {
Colin Cross79c7c262019-04-17 11:11:46 -07002357 android.ModuleBase
2358 android.DefaultableModuleBase
2359 prebuilt android.Prebuilt
Paul Duffindd46f712020-02-10 13:37:10 +00002360 android.ApexModuleBase
Colin Cross79c7c262019-04-17 11:11:46 -07002361
Paul Duffin37856732021-02-26 14:24:15 +00002362 hiddenAPI
Jiakai Zhang204356f2021-09-09 08:12:46 +00002363 dexpreopter
Paul Duffin37856732021-02-26 14:24:15 +00002364
Colin Cross79c7c262019-04-17 11:11:46 -07002365 properties sdkLibraryImportProperties
2366
Paul Duffin46a26a82020-04-07 19:27:04 +01002367 // Map from api scope to the scope specific property structure.
2368 scopeProperties map[*apiScope]*sdkLibraryScopeProperties
2369
Paul Duffin56d44902020-01-31 13:36:25 +00002370 commonToSdkLibraryAndImport
Paul Duffineedc5d52020-06-12 17:46:39 +01002371
2372 // The reference to the implementation library created by the source module.
2373 // Is nil if the source module does not exist.
2374 implLibraryModule *Library
2375
2376 // The reference to the xml permissions module created by the source module.
2377 // Is nil if the source module does not exist.
2378 xmlPermissionsFileModule *sdkLibraryXml
Paul Duffin39853512021-02-26 11:09:39 +00002379
Jeongik Chad5fe8782021-07-08 01:13:11 +09002380 // Build path to the dex implementation jar obtained from the prebuilt_apex, if any.
Spandan Dasfae468e2023-12-12 23:23:53 +00002381 dexJarFile OptionalDexJarPath
2382 dexJarFileErr error
Jeongik Chad5fe8782021-07-08 01:13:11 +09002383
2384 // Expected install file path of the source module(sdk_library)
2385 // or dex implementation jar obtained from the prebuilt_apex, if any.
2386 installFile android.Path
Colin Cross79c7c262019-04-17 11:11:46 -07002387}
2388
Paul Duffineedc5d52020-06-12 17:46:39 +01002389var _ SdkLibraryDependency = (*SdkLibraryImport)(nil)
Colin Cross79c7c262019-04-17 11:11:46 -07002390
Paul Duffin46a26a82020-04-07 19:27:04 +01002391// The type of a structure that contains a field of type sdkLibraryScopeProperties
2392// for each apiscope in allApiScopes, e.g. something like:
Colin Crossd079e0b2022-08-16 10:27:33 -07002393//
2394// struct {
2395// Public sdkLibraryScopeProperties
2396// System sdkLibraryScopeProperties
2397// ...
2398// }
Paul Duffin46a26a82020-04-07 19:27:04 +01002399var allScopeStructType = createAllScopePropertiesStructType()
2400
2401// Dynamically create a structure type for each apiscope in allApiScopes.
2402func createAllScopePropertiesStructType() reflect.Type {
2403 var fields []reflect.StructField
2404 for _, apiScope := range allApiScopes {
2405 field := reflect.StructField{
2406 Name: apiScope.fieldName,
2407 Type: reflect.TypeOf(sdkLibraryScopeProperties{}),
2408 }
2409 fields = append(fields, field)
2410 }
2411
2412 return reflect.StructOf(fields)
2413}
2414
2415// Create an instance of the scope specific structure type and return a map
2416// from apiscope to a pointer to each scope specific field.
2417func createPropertiesInstance() (interface{}, map[*apiScope]*sdkLibraryScopeProperties) {
2418 allScopePropertiesPtr := reflect.New(allScopeStructType)
2419 allScopePropertiesStruct := allScopePropertiesPtr.Elem()
2420 scopeProperties := make(map[*apiScope]*sdkLibraryScopeProperties)
2421
2422 for _, apiScope := range allApiScopes {
2423 field := allScopePropertiesStruct.FieldByName(apiScope.fieldName)
2424 scopeProperties[apiScope] = field.Addr().Interface().(*sdkLibraryScopeProperties)
2425 }
2426
2427 return allScopePropertiesPtr.Interface(), scopeProperties
2428}
2429
Jaewoong Jung4f158ee2019-07-11 10:05:35 -07002430// java_sdk_library_import imports a prebuilt java_sdk_library.
Colin Cross79c7c262019-04-17 11:11:46 -07002431func sdkLibraryImportFactory() android.Module {
Paul Duffineedc5d52020-06-12 17:46:39 +01002432 module := &SdkLibraryImport{}
Colin Cross79c7c262019-04-17 11:11:46 -07002433
Paul Duffin46a26a82020-04-07 19:27:04 +01002434 allScopeProperties, scopeToProperties := createPropertiesInstance()
2435 module.scopeProperties = scopeToProperties
Jiakai Zhang9c4dc192023-02-09 00:09:24 +08002436 module.AddProperties(&module.properties, allScopeProperties, &module.importDexpreoptProperties)
Colin Cross79c7c262019-04-17 11:11:46 -07002437
Paul Duffinc3091c82020-05-08 14:16:20 +01002438 // Initialize information common between source and prebuilt.
Paul Duffin71b33cc2021-06-23 11:39:47 +01002439 module.initCommon(module)
Paul Duffinc3091c82020-05-08 14:16:20 +01002440
Paul Duffin0bdcb272020-02-06 15:24:57 +00002441 android.InitPrebuiltModule(module, &[]string{""})
Paul Duffindd46f712020-02-10 13:37:10 +00002442 android.InitApexModule(module)
Colin Cross79c7c262019-04-17 11:11:46 -07002443 InitJavaModule(module, android.HostAndDeviceSupported)
2444
Paul Duffin1b1e8062020-05-08 13:44:43 +01002445 module.SetDefaultableHook(func(mctx android.DefaultableHookContext) {
2446 if module.initCommonAfterDefaultsApplied(mctx) {
2447 module.createInternalModules(mctx)
2448 }
2449 })
Colin Cross79c7c262019-04-17 11:11:46 -07002450 return module
2451}
2452
Paul Duffin630b11e2021-07-15 13:35:26 +01002453var _ PermittedPackagesForUpdatableBootJars = (*SdkLibraryImport)(nil)
2454
2455func (module *SdkLibraryImport) PermittedPackagesForUpdatableBootJars() []string {
2456 return module.properties.Permitted_packages
2457}
2458
Paul Duffineedc5d52020-06-12 17:46:39 +01002459func (module *SdkLibraryImport) Prebuilt() *android.Prebuilt {
Colin Cross79c7c262019-04-17 11:11:46 -07002460 return &module.prebuilt
2461}
2462
Paul Duffineedc5d52020-06-12 17:46:39 +01002463func (module *SdkLibraryImport) Name() string {
Colin Cross79c7c262019-04-17 11:11:46 -07002464 return module.prebuilt.Name(module.ModuleBase.Name())
2465}
2466
Paul Duffineedc5d52020-06-12 17:46:39 +01002467func (module *SdkLibraryImport) createInternalModules(mctx android.DefaultableHookContext) {
Colin Cross79c7c262019-04-17 11:11:46 -07002468
Paul Duffin50061512020-01-21 16:31:05 +00002469 // If the build is configured to use prebuilts then force this to be preferred.
Jeongik Cha816a23a2020-07-08 01:09:23 +09002470 if mctx.Config().AlwaysUsePrebuiltSdks() {
Paul Duffin50061512020-01-21 16:31:05 +00002471 module.prebuilt.ForcePrefer()
2472 }
2473
Paul Duffin46a26a82020-04-07 19:27:04 +01002474 for apiScope, scopeProperties := range module.scopeProperties {
Paul Duffin56d44902020-01-31 13:36:25 +00002475 if len(scopeProperties.Jars) == 0 {
2476 continue
2477 }
2478
Paul Duffinbbb546b2020-04-09 00:07:11 +01002479 module.createJavaImportForStubs(mctx, apiScope, scopeProperties)
Paul Duffin3d1248c2020-04-09 00:10:17 +01002480
Paul Duffin0f8faff2020-05-20 16:18:00 +01002481 if len(scopeProperties.Stub_srcs) > 0 {
2482 module.createPrebuiltStubsSources(mctx, apiScope, scopeProperties)
2483 }
Jihoon Kang71c86832023-09-13 01:01:53 +00002484
2485 if scopeProperties.Current_api != nil {
2486 module.createPrebuiltApiContribution(mctx, apiScope, scopeProperties)
2487 }
Paul Duffin56d44902020-01-31 13:36:25 +00002488 }
Colin Cross79c7c262019-04-17 11:11:46 -07002489
2490 javaSdkLibraries := javaSdkLibraries(mctx.Config())
2491 javaSdkLibrariesLock.Lock()
2492 defer javaSdkLibrariesLock.Unlock()
2493 *javaSdkLibraries = append(*javaSdkLibraries, module.BaseModuleName())
2494}
2495
Paul Duffineedc5d52020-06-12 17:46:39 +01002496func (module *SdkLibraryImport) createJavaImportForStubs(mctx android.DefaultableHookContext, apiScope *apiScope, scopeProperties *sdkLibraryScopeProperties) {
Paul Duffinbbb546b2020-04-09 00:07:11 +01002497 // Creates a java import for the jar with ".stubs" suffix
2498 props := struct {
Paul Duffin1dbe3ca2020-05-16 09:57:59 +01002499 Name *string
2500 Sdk_version *string
2501 Libs []string
2502 Jars []string
Paul Duffin1267d872021-04-16 17:21:36 +01002503 Compile_dex *bool
Paul Duffinbf4de042022-09-27 12:41:52 +01002504
2505 android.UserSuppliedPrebuiltProperties
Paul Duffinbbb546b2020-04-09 00:07:11 +01002506 }{}
Paul Duffinc3091c82020-05-08 14:16:20 +01002507 props.Name = proptools.StringPtr(module.stubsLibraryModuleName(apiScope))
Paul Duffinbbb546b2020-04-09 00:07:11 +01002508 props.Sdk_version = scopeProperties.Sdk_version
2509 // Prepend any of the libs from the legacy public properties to the libs for each of the
2510 // scopes to avoid having to duplicate them in each scope.
2511 props.Libs = append(module.properties.Libs, scopeProperties.Libs...)
2512 props.Jars = scopeProperties.Jars
Paul Duffin1dbe3ca2020-05-16 09:57:59 +01002513
Paul Duffin38b57852020-05-13 16:08:09 +01002514 // The imports are preferred if the java_sdk_library_import is preferred.
Paul Duffinbf4de042022-09-27 12:41:52 +01002515 props.CopyUserSuppliedPropertiesFromPrebuilt(&module.prebuilt)
Paul Duffin859fe962020-05-15 10:20:31 +01002516
Paul Duffin1267d872021-04-16 17:21:36 +01002517 // The imports need to be compiled to dex if the java_sdk_library_import requests it.
Paul Duffinf4600f62021-05-13 22:34:45 +01002518 compileDex := module.properties.Compile_dex
2519 if module.stubLibrariesCompiledForDex() {
2520 compileDex = proptools.BoolPtr(true)
2521 }
2522 props.Compile_dex = compileDex
Paul Duffin1267d872021-04-16 17:21:36 +01002523
Paul Duffin859fe962020-05-15 10:20:31 +01002524 mctx.CreateModule(ImportFactory, &props, module.sdkComponentPropertiesForChildLibrary())
Paul Duffinbbb546b2020-04-09 00:07:11 +01002525}
2526
Paul Duffineedc5d52020-06-12 17:46:39 +01002527func (module *SdkLibraryImport) createPrebuiltStubsSources(mctx android.DefaultableHookContext, apiScope *apiScope, scopeProperties *sdkLibraryScopeProperties) {
Paul Duffin3d1248c2020-04-09 00:10:17 +01002528 props := struct {
Paul Duffinbf4de042022-09-27 12:41:52 +01002529 Name *string
2530 Srcs []string
2531
2532 android.UserSuppliedPrebuiltProperties
Paul Duffin3d1248c2020-04-09 00:10:17 +01002533 }{}
Paul Duffinc3091c82020-05-08 14:16:20 +01002534 props.Name = proptools.StringPtr(module.stubsSourceModuleName(apiScope))
Paul Duffin3d1248c2020-04-09 00:10:17 +01002535 props.Srcs = scopeProperties.Stub_srcs
Paul Duffin38b57852020-05-13 16:08:09 +01002536
2537 // The stubs source is preferred if the java_sdk_library_import is preferred.
Paul Duffinbf4de042022-09-27 12:41:52 +01002538 props.CopyUserSuppliedPropertiesFromPrebuilt(&module.prebuilt)
2539
Spandan Das2cc80ba2023-10-27 17:21:52 +00002540 mctx.CreateModule(PrebuiltStubsSourcesFactory, &props, module.sdkComponentPropertiesForChildLibrary())
Paul Duffin3d1248c2020-04-09 00:10:17 +01002541}
2542
Jihoon Kang71c86832023-09-13 01:01:53 +00002543func (module *SdkLibraryImport) createPrebuiltApiContribution(mctx android.DefaultableHookContext, apiScope *apiScope, scopeProperties *sdkLibraryScopeProperties) {
2544 api_file := scopeProperties.Current_api
2545 api_surface := &apiScope.name
2546
2547 props := struct {
2548 Name *string
2549 Api_surface *string
2550 Api_file *string
2551 Visibility []string
2552 }{}
2553
2554 props.Name = proptools.StringPtr(module.stubsSourceModuleName(apiScope) + ".api.contribution")
2555 props.Api_surface = api_surface
2556 props.Api_file = api_file
2557 props.Visibility = []string{"//visibility:override", "//visibility:public"}
2558
Spandan Das2cc80ba2023-10-27 17:21:52 +00002559 mctx.CreateModule(ApiContributionImportFactory, &props, module.sdkComponentPropertiesForChildLibrary())
Jihoon Kang71c86832023-09-13 01:01:53 +00002560}
2561
Paul Duffin44f1d842020-06-26 20:17:02 +01002562// Add the dependencies on the child module in the component deps mutator so that it
2563// creates references to the prebuilt and not the source modules.
2564func (module *SdkLibraryImport) ComponentDepsMutator(ctx android.BottomUpMutatorContext) {
Paul Duffin46a26a82020-04-07 19:27:04 +01002565 for apiScope, scopeProperties := range module.scopeProperties {
Paul Duffin56d44902020-01-31 13:36:25 +00002566 if len(scopeProperties.Jars) == 0 {
2567 continue
2568 }
2569
2570 // Add dependencies to the prebuilt stubs library
Paul Duffin864116c2021-04-02 10:24:13 +01002571 ctx.AddVariationDependencies(nil, apiScope.stubsTag, android.PrebuiltNameFromSource(module.stubsLibraryModuleName(apiScope)))
Paul Duffin0f8faff2020-05-20 16:18:00 +01002572
2573 if len(scopeProperties.Stub_srcs) > 0 {
2574 // Add dependencies to the prebuilt stubs source library
Paul Duffin864116c2021-04-02 10:24:13 +01002575 ctx.AddVariationDependencies(nil, apiScope.stubsSourceTag, android.PrebuiltNameFromSource(module.stubsSourceModuleName(apiScope)))
Paul Duffin0f8faff2020-05-20 16:18:00 +01002576 }
Paul Duffin56d44902020-01-31 13:36:25 +00002577 }
Paul Duffin44f1d842020-06-26 20:17:02 +01002578}
2579
2580// Add other dependencies as normal.
2581func (module *SdkLibraryImport) DepsMutator(ctx android.BottomUpMutatorContext) {
Paul Duffineedc5d52020-06-12 17:46:39 +01002582
2583 implName := module.implLibraryModuleName()
2584 if ctx.OtherModuleExists(implName) {
2585 ctx.AddVariationDependencies(nil, implLibraryTag, implName)
2586
2587 xmlPermissionsModuleName := module.xmlPermissionsModuleName()
2588 if module.sharedLibrary() && ctx.OtherModuleExists(xmlPermissionsModuleName) {
2589 // Add dependency to the rule for generating the xml permissions file
2590 ctx.AddDependency(module, xmlPermissionsFileTag, xmlPermissionsModuleName)
2591 }
2592 }
Colin Cross79c7c262019-04-17 11:11:46 -07002593}
2594
Jiyong Park45bf82e2020-12-15 22:29:02 +09002595var _ android.ApexModule = (*SdkLibraryImport)(nil)
2596
2597// Implements android.ApexModule
Paul Duffineedc5d52020-06-12 17:46:39 +01002598func (module *SdkLibraryImport) DepIsInSameApex(mctx android.BaseModuleContext, dep android.Module) bool {
2599 depTag := mctx.OtherModuleDependencyTag(dep)
2600 if depTag == xmlPermissionsFileTag {
2601 return true
2602 }
2603
2604 // None of the other dependencies of the java_sdk_library_import are in the same apex
2605 // as the one that references this module.
2606 return false
2607}
2608
Jiyong Park45bf82e2020-12-15 22:29:02 +09002609// Implements android.ApexModule
Dan Albertc8060532020-07-22 22:32:17 -07002610func (module *SdkLibraryImport) ShouldSupportSdkVersion(ctx android.BaseModuleContext,
2611 sdkVersion android.ApiLevel) error {
Jooyung Han749dc692020-04-15 11:03:39 +09002612 // we don't check prebuilt modules for sdk_version
2613 return nil
2614}
2615
Paul Duffinea8f8082021-06-24 13:25:57 +01002616// Implements android.ApexModule
2617func (module *SdkLibraryImport) UniqueApexVariations() bool {
2618 return module.uniqueApexVariations()
2619}
2620
Paul Duffin09817d62022-04-28 17:45:11 +01002621// MinSdkVersion - Implements hiddenAPIModule
Spandan Das8c9ae7e2023-03-03 21:20:36 +00002622func (module *SdkLibraryImport) MinSdkVersion(ctx android.EarlyModuleContext) android.ApiLevel {
2623 return android.NoneApiLevel
Paul Duffin09817d62022-04-28 17:45:11 +01002624}
2625
2626var _ hiddenAPIModule = (*SdkLibraryImport)(nil)
2627
Paul Duffineedc5d52020-06-12 17:46:39 +01002628func (module *SdkLibraryImport) OutputFiles(tag string) (android.Paths, error) {
Paul Duffin1e940d52022-04-29 14:21:25 +01002629 paths, err := module.commonOutputFiles(tag)
2630 if paths != nil || err != nil {
2631 return paths, err
2632 }
2633 if module.implLibraryModule != nil {
2634 return module.implLibraryModule.OutputFiles(tag)
2635 } else {
2636 return nil, nil
2637 }
Paul Duffin46dc45a2020-05-14 15:39:10 +01002638}
2639
Paul Duffineedc5d52020-06-12 17:46:39 +01002640func (module *SdkLibraryImport) GenerateAndroidBuildActions(ctx android.ModuleContext) {
Paul Duffina2ae7e02020-09-11 11:55:00 +01002641 module.generateCommonBuildActions(ctx)
2642
Jeongik Chad5fe8782021-07-08 01:13:11 +09002643 // Assume that source module(sdk_library) is installed in /<sdk_library partition>/framework
2644 module.installFile = android.PathForModuleInstall(ctx, "framework", module.Stem()+".jar")
2645
Paul Duffin0f8faff2020-05-20 16:18:00 +01002646 // Record the paths to the prebuilt stubs library and stubs source.
Colin Cross79c7c262019-04-17 11:11:46 -07002647 ctx.VisitDirectDeps(func(to android.Module) {
2648 tag := ctx.OtherModuleDependencyTag(to)
2649
Paul Duffin0f8faff2020-05-20 16:18:00 +01002650 // Extract information from any of the scope specific dependencies.
2651 if scopeTag, ok := tag.(scopeDependencyTag); ok {
2652 apiScope := scopeTag.apiScope
2653 scopePaths := module.getScopePathsCreateIfNeeded(apiScope)
2654
2655 // Extract information from the dependency. The exact information extracted
2656 // is determined by the nature of the dependency which is determined by the tag.
2657 scopeTag.extractDepInfo(ctx, to, scopePaths)
Paul Duffineedc5d52020-06-12 17:46:39 +01002658 } else if tag == implLibraryTag {
2659 if implLibrary, ok := to.(*Library); ok {
2660 module.implLibraryModule = implLibrary
2661 } else {
2662 ctx.ModuleErrorf("implementation library must be of type *java.Library but was %T", to)
2663 }
2664 } else if tag == xmlPermissionsFileTag {
2665 if xmlPermissionsFileModule, ok := to.(*sdkLibraryXml); ok {
2666 module.xmlPermissionsFileModule = xmlPermissionsFileModule
2667 } else {
2668 ctx.ModuleErrorf("xml permissions file module must be of type *sdkLibraryXml but was %T", to)
2669 }
Colin Cross79c7c262019-04-17 11:11:46 -07002670 }
2671 })
Paul Duffin0f8faff2020-05-20 16:18:00 +01002672
2673 // Populate the scope paths with information from the properties.
2674 for apiScope, scopeProperties := range module.scopeProperties {
2675 if len(scopeProperties.Jars) == 0 {
2676 continue
2677 }
2678
2679 paths := module.getScopePathsCreateIfNeeded(apiScope)
Anton Hanssond78eb762021-09-21 15:25:12 +01002680 paths.annotationsZip = android.OptionalPathForModuleSrc(ctx, scopeProperties.Annotations)
Paul Duffin0f8faff2020-05-20 16:18:00 +01002681 paths.currentApiFilePath = android.OptionalPathForModuleSrc(ctx, scopeProperties.Current_api)
2682 paths.removedApiFilePath = android.OptionalPathForModuleSrc(ctx, scopeProperties.Removed_api)
2683 }
Paul Duffin39853512021-02-26 11:09:39 +00002684
2685 if ctx.Device() {
2686 // If this is a variant created for a prebuilt_apex then use the dex implementation jar
2687 // obtained from the associated deapexer module.
Colin Crossff694a82023-12-13 15:54:49 -08002688 ai, _ := android.ModuleProvider(ctx, android.ApexInfoProvider)
Paul Duffin39853512021-02-26 11:09:39 +00002689 if ai.ForPrebuiltApex {
Paul Duffin39853512021-02-26 11:09:39 +00002690 // Get the path of the dex implementation jar from the `deapexer` module.
Spandan Dasfae468e2023-12-12 23:23:53 +00002691 di, err := android.FindDeapexerProviderForModule(ctx)
2692 if err != nil {
2693 // An error was found, possibly due to multiple apexes in the tree that export this library
2694 // Defer the error till a client tries to call DexJarBuildPath
2695 module.dexJarFileErr = err
2696 return
Martin Stjernholm44825602021-09-17 01:44:12 +01002697 }
Jiakai Zhang81e46812023-02-08 21:56:07 +08002698 dexJarFileApexRootRelative := apexRootRelativePathToJavaLib(module.BaseModuleName())
2699 if dexOutputPath := di.PrebuiltExportPath(dexJarFileApexRootRelative); dexOutputPath != nil {
Martin Stjernholm8be1e6d2021-09-15 03:34:04 +01002700 dexJarFile := makeDexJarPathFromPath(dexOutputPath)
2701 module.dexJarFile = dexJarFile
Jiakai Zhang204356f2021-09-09 08:12:46 +00002702 installPath := android.PathForModuleInPartitionInstall(
Jiakai Zhang81e46812023-02-08 21:56:07 +08002703 ctx, "apex", ai.ApexVariationName, dexJarFileApexRootRelative)
Jiakai Zhang204356f2021-09-09 08:12:46 +00002704 module.installFile = installPath
Martin Stjernholm8be1e6d2021-09-15 03:34:04 +01002705 module.initHiddenAPI(ctx, dexJarFile, module.findScopePaths(apiScopePublic).stubsImplPath[0], nil)
Jiakai Zhang204356f2021-09-09 08:12:46 +00002706
Jiakai Zhang204356f2021-09-09 08:12:46 +00002707 module.dexpreopter.installPath = module.dexpreopter.getInstallPath(ctx, installPath)
2708 module.dexpreopter.isSDKLibrary = true
2709 module.dexpreopter.uncompressedDex = shouldUncompressDex(ctx, &module.dexpreopter)
Jiakai Zhang81e46812023-02-08 21:56:07 +08002710
2711 if profilePath := di.PrebuiltExportPath(dexJarFileApexRootRelative + ".prof"); profilePath != nil {
2712 module.dexpreopter.inputProfilePathOnHost = profilePath
2713 }
2714
2715 // Dexpreopting.
Jiakai Zhang204356f2021-09-09 08:12:46 +00002716 module.dexpreopt(ctx, dexOutputPath)
Paul Duffin39853512021-02-26 11:09:39 +00002717 } else {
2718 // This should never happen as a variant for a prebuilt_apex is only created if the
2719 // prebuilt_apex has been configured to export the java library dex file.
Martin Stjernholm44825602021-09-17 01:44:12 +01002720 ctx.ModuleErrorf("internal error: no dex implementation jar available from prebuilt APEX %s", di.ApexModuleName())
Paul Duffin39853512021-02-26 11:09:39 +00002721 }
2722 }
2723 }
Colin Cross79c7c262019-04-17 11:11:46 -07002724}
2725
Jiyong Parkf1691d22021-03-29 20:11:58 +09002726func (module *SdkLibraryImport) sdkJars(ctx android.BaseModuleContext, sdkVersion android.SdkSpec, headerJars bool) android.Paths {
Paul Duffineedc5d52020-06-12 17:46:39 +01002727
2728 // For consistency with SdkLibrary make the implementation jar available to libraries that
2729 // are within the same APEX.
2730 implLibraryModule := module.implLibraryModule
Colin Cross56a83212020-09-15 18:30:11 -07002731 if implLibraryModule != nil && withinSameApexesAs(ctx, module) {
Paul Duffineedc5d52020-06-12 17:46:39 +01002732 if headerJars {
2733 return implLibraryModule.HeaderJars()
2734 } else {
2735 return implLibraryModule.ImplementationJars()
2736 }
2737 }
2738
Paul Duffin23970f42020-05-20 14:20:02 +01002739 return module.selectHeaderJarsForSdkVersion(ctx, sdkVersion)
Paul Duffin56d44902020-01-31 13:36:25 +00002740}
2741
Colin Cross79c7c262019-04-17 11:11:46 -07002742// to satisfy SdkLibraryDependency interface
Jiyong Parkf1691d22021-03-29 20:11:58 +09002743func (module *SdkLibraryImport) SdkHeaderJars(ctx android.BaseModuleContext, sdkVersion android.SdkSpec) android.Paths {
Colin Cross79c7c262019-04-17 11:11:46 -07002744 // This module is just a wrapper for the prebuilt stubs.
Paul Duffineedc5d52020-06-12 17:46:39 +01002745 return module.sdkJars(ctx, sdkVersion, true)
Colin Cross79c7c262019-04-17 11:11:46 -07002746}
2747
2748// to satisfy SdkLibraryDependency interface
Jiyong Parkf1691d22021-03-29 20:11:58 +09002749func (module *SdkLibraryImport) SdkImplementationJars(ctx android.BaseModuleContext, sdkVersion android.SdkSpec) android.Paths {
Colin Cross79c7c262019-04-17 11:11:46 -07002750 // This module is just a wrapper for the stubs.
Paul Duffineedc5d52020-06-12 17:46:39 +01002751 return module.sdkJars(ctx, sdkVersion, false)
2752}
2753
Ulya Trafimovichdbf31662020-12-17 12:07:54 +00002754// to satisfy UsesLibraryDependency interface
Martin Stjernholm8be1e6d2021-09-15 03:34:04 +01002755func (module *SdkLibraryImport) DexJarBuildPath() OptionalDexJarPath {
Paul Duffin39853512021-02-26 11:09:39 +00002756 // The dex implementation jar extracted from the .apex file should be used in preference to the
2757 // source.
Spandan Dasfae468e2023-12-12 23:23:53 +00002758 if module.dexJarFileErr != nil {
2759 panic(module.dexJarFileErr.Error())
2760 }
Martin Stjernholm8be1e6d2021-09-15 03:34:04 +01002761 if module.dexJarFile.IsSet() {
Paul Duffin39853512021-02-26 11:09:39 +00002762 return module.dexJarFile
2763 }
Paul Duffineedc5d52020-06-12 17:46:39 +01002764 if module.implLibraryModule == nil {
Martin Stjernholm8be1e6d2021-09-15 03:34:04 +01002765 return makeUnsetDexJarPath()
Paul Duffineedc5d52020-06-12 17:46:39 +01002766 } else {
2767 return module.implLibraryModule.DexJarBuildPath()
2768 }
2769}
2770
Ulya Trafimovichdbf31662020-12-17 12:07:54 +00002771// to satisfy UsesLibraryDependency interface
Ulya Trafimovich31e444e2020-08-14 17:32:16 +01002772func (module *SdkLibraryImport) DexJarInstallPath() android.Path {
Jeongik Chad5fe8782021-07-08 01:13:11 +09002773 return module.installFile
Ulya Trafimovich31e444e2020-08-14 17:32:16 +01002774}
2775
Ulya Trafimovichdbf31662020-12-17 12:07:54 +00002776// to satisfy UsesLibraryDependency interface
2777func (module *SdkLibraryImport) ClassLoaderContexts() dexpreopt.ClassLoaderContextMap {
2778 return nil
2779}
2780
Paul Duffineedc5d52020-06-12 17:46:39 +01002781// to satisfy apex.javaDependency interface
2782func (module *SdkLibraryImport) JacocoReportClassesFile() android.Path {
2783 if module.implLibraryModule == nil {
2784 return nil
2785 } else {
2786 return module.implLibraryModule.JacocoReportClassesFile()
2787 }
2788}
2789
2790// to satisfy apex.javaDependency interface
Colin Cross08dca382020-07-21 20:31:17 -07002791func (module *SdkLibraryImport) LintDepSets() LintDepSets {
2792 if module.implLibraryModule == nil {
2793 return LintDepSets{}
2794 } else {
2795 return module.implLibraryModule.LintDepSets()
2796 }
2797}
2798
Spandan Das17854f52022-01-14 21:19:14 +00002799func (module *SdkLibraryImport) GetStrictUpdatabilityLinting() bool {
Jaewoong Jung476b9d62021-05-10 15:30:00 -07002800 if module.implLibraryModule == nil {
2801 return false
2802 } else {
Spandan Das17854f52022-01-14 21:19:14 +00002803 return module.implLibraryModule.GetStrictUpdatabilityLinting()
Jaewoong Jung476b9d62021-05-10 15:30:00 -07002804 }
2805}
2806
Spandan Das17854f52022-01-14 21:19:14 +00002807func (module *SdkLibraryImport) SetStrictUpdatabilityLinting(strictLinting bool) {
Jaewoong Jung476b9d62021-05-10 15:30:00 -07002808 if module.implLibraryModule != nil {
Spandan Das17854f52022-01-14 21:19:14 +00002809 module.implLibraryModule.SetStrictUpdatabilityLinting(strictLinting)
Jaewoong Jung476b9d62021-05-10 15:30:00 -07002810 }
2811}
2812
Colin Cross08dca382020-07-21 20:31:17 -07002813// to satisfy apex.javaDependency interface
Paul Duffineedc5d52020-06-12 17:46:39 +01002814func (module *SdkLibraryImport) Stem() string {
2815 return module.BaseModuleName()
Colin Cross79c7c262019-04-17 11:11:46 -07002816}
Jiyong Parke3833882020-02-17 17:28:10 +09002817
Paul Duffin44b481b2020-06-17 16:59:43 +01002818var _ ApexDependency = (*SdkLibraryImport)(nil)
2819
2820// to satisfy java.ApexDependency interface
2821func (module *SdkLibraryImport) HeaderJars() android.Paths {
2822 if module.implLibraryModule == nil {
2823 return nil
2824 } else {
2825 return module.implLibraryModule.HeaderJars()
2826 }
2827}
2828
2829// to satisfy java.ApexDependency interface
2830func (module *SdkLibraryImport) ImplementationAndResourcesJars() android.Paths {
2831 if module.implLibraryModule == nil {
2832 return nil
2833 } else {
2834 return module.implLibraryModule.ImplementationAndResourcesJars()
2835 }
2836}
2837
Jiakai Zhang204356f2021-09-09 08:12:46 +00002838// to satisfy java.DexpreopterInterface interface
2839func (module *SdkLibraryImport) IsInstallable() bool {
2840 return true
2841}
2842
Paul Duffinfef55002021-06-17 14:56:05 +01002843var _ android.RequiredFilesFromPrebuiltApex = (*SdkLibraryImport)(nil)
2844
Paul Duffinb4bbf2c2021-06-17 15:59:07 +01002845func (module *SdkLibraryImport) RequiredFilesFromPrebuiltApex(ctx android.BaseModuleContext) []string {
Paul Duffinfef55002021-06-17 14:56:05 +01002846 name := module.BaseModuleName()
Jiakai Zhang81e46812023-02-08 21:56:07 +08002847 return requiredFilesFromPrebuiltApexForImport(name, &module.dexpreopter)
Paul Duffinfef55002021-06-17 14:56:05 +01002848}
2849
Jiyong Parke3833882020-02-17 17:28:10 +09002850// java_sdk_library_xml
Jiyong Parke3833882020-02-17 17:28:10 +09002851type sdkLibraryXml struct {
2852 android.ModuleBase
2853 android.DefaultableModuleBase
2854 android.ApexModuleBase
2855
2856 properties sdkLibraryXmlProperties
2857
2858 outputFilePath android.OutputPath
2859 installDirPath android.InstallPath
Colin Cross56a83212020-09-15 18:30:11 -07002860
2861 hideApexVariantFromMake bool
Jiyong Parke3833882020-02-17 17:28:10 +09002862}
2863
2864type sdkLibraryXmlProperties struct {
2865 // canonical name of the lib
2866 Lib_name *string
Pedro Loureiro9956e5e2021-09-07 17:21:59 +00002867
2868 // Signals that this shared library is part of the bootclasspath starting
2869 // on the version indicated in this attribute.
2870 //
2871 // This will make platforms at this level and above to ignore
2872 // <uses-library> tags with this library name because the library is already
2873 // available
2874 On_bootclasspath_since *string
2875
2876 // Signals that this shared library was part of the bootclasspath before
2877 // (but not including) the version indicated in this attribute.
2878 //
2879 // The system will automatically add a <uses-library> tag with this library to
2880 // apps that target any SDK less than the version indicated in this attribute.
2881 On_bootclasspath_before *string
2882
2883 // Indicates that PackageManager should ignore this shared library if the
2884 // platform is below the version indicated in this attribute.
2885 //
2886 // This means that the device won't recognise this library as installed.
2887 Min_device_sdk *string
2888
2889 // Indicates that PackageManager should ignore this shared library if the
2890 // platform is above the version indicated in this attribute.
2891 //
2892 // This means that the device won't recognise this library as installed.
2893 Max_device_sdk *string
Pedro Loureiroc3621422021-09-28 15:40:23 +00002894
2895 // The SdkLibrary's min api level as a string
2896 //
2897 // This value comes from the ApiLevel of the MinSdkVersion property.
2898 Sdk_library_min_api_level *string
Jamie Garsidee570ace2023-11-27 12:07:36 +00002899
2900 // Uses-libs dependencies that the shared library requires to work correctly.
2901 //
2902 // This will add dependency="foo:bar" to the <library> section.
2903 Uses_libs_dependencies []string
Jiyong Parke3833882020-02-17 17:28:10 +09002904}
2905
2906// java_sdk_library_xml builds the permission xml file for a java_sdk_library.
2907// Not to be used directly by users. java_sdk_library internally uses this.
2908func sdkLibraryXmlFactory() android.Module {
2909 module := &sdkLibraryXml{}
2910
2911 module.AddProperties(&module.properties)
2912
2913 android.InitApexModule(module)
2914 android.InitAndroidArchModule(module, android.DeviceSupported, android.MultilibCommon)
2915
2916 return module
2917}
2918
Colin Crossaede88c2020-08-11 12:17:01 -07002919func (module *sdkLibraryXml) UniqueApexVariations() bool {
2920 // sdkLibraryXml needs a unique variation per APEX because the generated XML file contains the path to the
2921 // mounted APEX, which contains the name of the APEX.
2922 return true
2923}
2924
Jiyong Parke3833882020-02-17 17:28:10 +09002925// from android.PrebuiltEtcModule
Jooyung Han0703fd82020-08-26 22:11:53 +09002926func (module *sdkLibraryXml) BaseDir() string {
2927 return "etc"
2928}
2929
2930// from android.PrebuiltEtcModule
Jiyong Parke3833882020-02-17 17:28:10 +09002931func (module *sdkLibraryXml) SubDir() string {
2932 return "permissions"
2933}
2934
2935// from android.PrebuiltEtcModule
2936func (module *sdkLibraryXml) OutputFile() android.OutputPath {
2937 return module.outputFilePath
2938}
2939
2940// from android.ApexModule
2941func (module *sdkLibraryXml) AvailableFor(what string) bool {
2942 return true
2943}
2944
2945func (module *sdkLibraryXml) DepsMutator(ctx android.BottomUpMutatorContext) {
2946 // do nothing
2947}
2948
Jiyong Park45bf82e2020-12-15 22:29:02 +09002949var _ android.ApexModule = (*sdkLibraryXml)(nil)
2950
2951// Implements android.ApexModule
Dan Albertc8060532020-07-22 22:32:17 -07002952func (module *sdkLibraryXml) ShouldSupportSdkVersion(ctx android.BaseModuleContext,
2953 sdkVersion android.ApiLevel) error {
Jooyung Han749dc692020-04-15 11:03:39 +09002954 // sdkLibraryXml doesn't need to be checked separately because java_sdk_library is checked
2955 return nil
2956}
2957
Jiyong Parke3833882020-02-17 17:28:10 +09002958// File path to the runtime implementation library
Colin Cross56a83212020-09-15 18:30:11 -07002959func (module *sdkLibraryXml) implPath(ctx android.ModuleContext) string {
Jiyong Parke3833882020-02-17 17:28:10 +09002960 implName := proptools.String(module.properties.Lib_name)
Colin Crossff694a82023-12-13 15:54:49 -08002961 if apexInfo, _ := android.ModuleProvider(ctx, android.ApexInfoProvider); !apexInfo.IsForPlatform() {
Colin Crosse07f2312020-08-13 11:24:56 -07002962 // TODO(b/146468504): ApexVariationName() is only a soong module name, not apex name.
Jiyong Parke3833882020-02-17 17:28:10 +09002963 // In most cases, this works fine. But when apex_name is set or override_apex is used
2964 // this can be wrong.
Colin Cross56a83212020-09-15 18:30:11 -07002965 return fmt.Sprintf("/apex/%s/javalib/%s.jar", apexInfo.ApexVariationName, implName)
Jiyong Parke3833882020-02-17 17:28:10 +09002966 }
2967 partition := "system"
2968 if module.SocSpecific() {
2969 partition = "vendor"
2970 } else if module.DeviceSpecific() {
2971 partition = "odm"
2972 } else if module.ProductSpecific() {
2973 partition = "product"
2974 } else if module.SystemExtSpecific() {
2975 partition = "system_ext"
2976 }
2977 return "/" + partition + "/framework/" + implName + ".jar"
2978}
2979
Pedro Loureiro9956e5e2021-09-07 17:21:59 +00002980func formattedOptionalSdkLevelAttribute(ctx android.ModuleContext, attrName string, value *string) string {
2981 if value == nil {
2982 return ""
2983 }
2984 apiLevel, err := android.ApiLevelFromUser(ctx, *value)
2985 if err != nil {
Pedro Loureiroba6682f2021-10-29 09:32:32 +00002986 // attributes in bp files have underscores but in the xml have dashes.
2987 ctx.PropertyErrorf(strings.ReplaceAll(attrName, "-", "_"), err.Error())
Pedro Loureiro9956e5e2021-09-07 17:21:59 +00002988 return ""
2989 }
Pedro Loureirob638c622021-12-22 15:28:05 +00002990 if apiLevel.IsCurrent() {
2991 // passing "current" would always mean a future release, never the current (or the current in
2992 // progress) which means some conditions would never be triggered.
2993 ctx.PropertyErrorf(strings.ReplaceAll(attrName, "-", "_"),
2994 `"current" is not an allowed value for this attribute`)
2995 return ""
2996 }
Pedro Loureiro48991222022-06-17 20:01:21 +00002997 // "safeValue" is safe because it translates finalized codenames to a string
2998 // with their SDK int.
2999 safeValue := apiLevel.String()
3000 return formattedOptionalAttribute(attrName, &safeValue)
Pedro Loureiro9956e5e2021-09-07 17:21:59 +00003001}
3002
3003// formats an attribute for the xml permissions file if the value is not null
3004// returns empty string otherwise
3005func formattedOptionalAttribute(attrName string, value *string) string {
3006 if value == nil {
3007 return ""
3008 }
3009 return fmt.Sprintf(` %s=\"%s\"\n`, attrName, *value)
3010}
3011
Jamie Garsidee570ace2023-11-27 12:07:36 +00003012func formattedDependenciesAttribute(dependencies []string) string {
3013 if dependencies == nil {
3014 return ""
3015 }
3016 return fmt.Sprintf(` dependency=\"%s\"\n`, strings.Join(dependencies, ":"))
3017}
3018
Pedro Loureiro9956e5e2021-09-07 17:21:59 +00003019func (module *sdkLibraryXml) permissionsContents(ctx android.ModuleContext) string {
3020 libName := proptools.String(module.properties.Lib_name)
3021 libNameAttr := formattedOptionalAttribute("name", &libName)
3022 filePath := module.implPath(ctx)
3023 filePathAttr := formattedOptionalAttribute("file", &filePath)
Pedro Loureiroba6682f2021-10-29 09:32:32 +00003024 implicitFromAttr := formattedOptionalSdkLevelAttribute(ctx, "on-bootclasspath-since", module.properties.On_bootclasspath_since)
3025 implicitUntilAttr := formattedOptionalSdkLevelAttribute(ctx, "on-bootclasspath-before", module.properties.On_bootclasspath_before)
3026 minSdkAttr := formattedOptionalSdkLevelAttribute(ctx, "min-device-sdk", module.properties.Min_device_sdk)
3027 maxSdkAttr := formattedOptionalSdkLevelAttribute(ctx, "max-device-sdk", module.properties.Max_device_sdk)
Jamie Garsidee570ace2023-11-27 12:07:36 +00003028 dependenciesAttr := formattedDependenciesAttribute(module.properties.Uses_libs_dependencies)
Pedro Loureiro196d3e62021-12-22 19:53:01 +00003029 // <library> is understood in all android versions whereas <apex-library> is only understood from API T (and ignored before that).
3030 // 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 +00003031 var libraryTag string
3032 if module.properties.Min_device_sdk != nil {
Pedro Loureiro196d3e62021-12-22 19:53:01 +00003033 libraryTag = ` <apex-library\n`
Pedro Loureiroc3621422021-09-28 15:40:23 +00003034 } else {
3035 libraryTag = ` <library\n`
3036 }
Pedro Loureiro9956e5e2021-09-07 17:21:59 +00003037
3038 return strings.Join([]string{
3039 `<?xml version=\"1.0\" encoding=\"utf-8\"?>\n`,
3040 `<!-- Copyright (C) 2018 The Android Open Source Project\n`,
3041 `\n`,
3042 ` Licensed under the Apache License, Version 2.0 (the \"License\");\n`,
3043 ` you may not use this file except in compliance with the License.\n`,
3044 ` You may obtain a copy of the License at\n`,
3045 `\n`,
3046 ` http://www.apache.org/licenses/LICENSE-2.0\n`,
3047 `\n`,
3048 ` Unless required by applicable law or agreed to in writing, software\n`,
3049 ` distributed under the License is distributed on an \"AS IS\" BASIS,\n`,
3050 ` WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\n`,
3051 ` See the License for the specific language governing permissions and\n`,
3052 ` limitations under the License.\n`,
3053 `-->\n`,
3054 `<permissions>\n`,
Pedro Loureiroc3621422021-09-28 15:40:23 +00003055 libraryTag,
Pedro Loureiro9956e5e2021-09-07 17:21:59 +00003056 libNameAttr,
3057 filePathAttr,
3058 implicitFromAttr,
3059 implicitUntilAttr,
3060 minSdkAttr,
3061 maxSdkAttr,
Jamie Garsidee570ace2023-11-27 12:07:36 +00003062 dependenciesAttr,
Pedro Loureiro9956e5e2021-09-07 17:21:59 +00003063 ` />\n`,
3064 `</permissions>\n`}, "")
3065}
3066
Jiyong Parke3833882020-02-17 17:28:10 +09003067func (module *sdkLibraryXml) GenerateAndroidBuildActions(ctx android.ModuleContext) {
Colin Crossff694a82023-12-13 15:54:49 -08003068 apexInfo, _ := android.ModuleProvider(ctx, android.ApexInfoProvider)
3069 module.hideApexVariantFromMake = !apexInfo.IsForPlatform()
Colin Cross56a83212020-09-15 18:30:11 -07003070
Jiyong Parke3833882020-02-17 17:28:10 +09003071 libName := proptools.String(module.properties.Lib_name)
Pedro Loureiroc3621422021-09-28 15:40:23 +00003072 module.selfValidate(ctx)
Pedro Loureiro9956e5e2021-09-07 17:21:59 +00003073 xmlContent := module.permissionsContents(ctx)
Jiyong Parke3833882020-02-17 17:28:10 +09003074
3075 module.outputFilePath = android.PathForModuleOut(ctx, libName+".xml").OutputPath
Colin Crossf1a035e2020-11-16 17:32:30 -08003076 rule := android.NewRuleBuilder(pctx, ctx)
Jiyong Parke3833882020-02-17 17:28:10 +09003077 rule.Command().
3078 Text("/bin/bash -c \"echo -e '" + xmlContent + "'\" > ").
3079 Output(module.outputFilePath)
3080
Colin Crossf1a035e2020-11-16 17:32:30 -08003081 rule.Build("java_sdk_xml", "Permission XML")
Jiyong Parke3833882020-02-17 17:28:10 +09003082
3083 module.installDirPath = android.PathForModuleInstall(ctx, "etc", module.SubDir())
3084}
3085
3086func (module *sdkLibraryXml) AndroidMkEntries() []android.AndroidMkEntries {
Colin Cross56a83212020-09-15 18:30:11 -07003087 if module.hideApexVariantFromMake {
satayev8f088b02021-12-06 11:40:46 +00003088 return []android.AndroidMkEntries{{
Jiyong Parke3833882020-02-17 17:28:10 +09003089 Disabled: true,
3090 }}
3091 }
3092
satayev8f088b02021-12-06 11:40:46 +00003093 return []android.AndroidMkEntries{{
Jiyong Parke3833882020-02-17 17:28:10 +09003094 Class: "ETC",
3095 OutputFile: android.OptionalPathForPath(module.outputFilePath),
3096 ExtraEntries: []android.AndroidMkExtraEntriesFunc{
Colin Crossaa255532020-07-03 13:18:24 -07003097 func(ctx android.AndroidMkExtraEntriesContext, entries *android.AndroidMkEntries) {
Jiyong Parke3833882020-02-17 17:28:10 +09003098 entries.SetString("LOCAL_MODULE_TAGS", "optional")
Colin Crossc68db4b2021-11-11 18:59:15 -08003099 entries.SetString("LOCAL_MODULE_PATH", module.installDirPath.String())
Jiyong Parke3833882020-02-17 17:28:10 +09003100 entries.SetString("LOCAL_INSTALLED_MODULE_STEM", module.outputFilePath.Base())
3101 },
3102 },
3103 }}
3104}
Paul Duffindd46f712020-02-10 13:37:10 +00003105
Pedro Loureiroc3621422021-09-28 15:40:23 +00003106func (module *sdkLibraryXml) selfValidate(ctx android.ModuleContext) {
3107 module.validateAtLeastTAttributes(ctx)
3108 module.validateMinAndMaxDeviceSdk(ctx)
3109 module.validateMinMaxDeviceSdkAndModuleMinSdk(ctx)
3110 module.validateOnBootclasspathBeforeRequirements(ctx)
3111}
3112
3113func (module *sdkLibraryXml) validateAtLeastTAttributes(ctx android.ModuleContext) {
3114 t := android.ApiLevelOrPanic(ctx, "Tiramisu")
3115 module.attrAtLeastT(ctx, t, module.properties.Min_device_sdk, "min_device_sdk")
3116 module.attrAtLeastT(ctx, t, module.properties.Max_device_sdk, "max_device_sdk")
3117 module.attrAtLeastT(ctx, t, module.properties.On_bootclasspath_before, "on_bootclasspath_before")
3118 module.attrAtLeastT(ctx, t, module.properties.On_bootclasspath_since, "on_bootclasspath_since")
3119}
3120
3121func (module *sdkLibraryXml) attrAtLeastT(ctx android.ModuleContext, t android.ApiLevel, attr *string, attrName string) {
3122 if attr != nil {
3123 if level, err := android.ApiLevelFromUser(ctx, *attr); err == nil {
3124 // we will inform the user of invalid inputs when we try to write the
3125 // permissions xml file so we don't need to do it here
3126 if t.GreaterThan(level) {
3127 ctx.PropertyErrorf(attrName, "Attribute value needs to be at least T")
3128 }
3129 }
3130 }
3131}
3132
3133func (module *sdkLibraryXml) validateMinAndMaxDeviceSdk(ctx android.ModuleContext) {
3134 if module.properties.Min_device_sdk != nil && module.properties.Max_device_sdk != nil {
3135 min, minErr := android.ApiLevelFromUser(ctx, *module.properties.Min_device_sdk)
3136 max, maxErr := android.ApiLevelFromUser(ctx, *module.properties.Max_device_sdk)
3137 if minErr == nil && maxErr == nil {
3138 // we will inform the user of invalid inputs when we try to write the
3139 // permissions xml file so we don't need to do it here
3140 if min.GreaterThan(max) {
3141 ctx.ModuleErrorf("min_device_sdk can't be greater than max_device_sdk")
3142 }
3143 }
3144 }
3145}
3146
3147func (module *sdkLibraryXml) validateMinMaxDeviceSdkAndModuleMinSdk(ctx android.ModuleContext) {
3148 moduleMinApi := android.ApiLevelOrPanic(ctx, *module.properties.Sdk_library_min_api_level)
3149 if module.properties.Min_device_sdk != nil {
3150 api, err := android.ApiLevelFromUser(ctx, *module.properties.Min_device_sdk)
3151 if err == nil {
3152 if moduleMinApi.GreaterThan(api) {
3153 ctx.PropertyErrorf("min_device_sdk", "Can't be less than module's min sdk (%s)", moduleMinApi)
3154 }
3155 }
3156 }
3157 if module.properties.Max_device_sdk != nil {
3158 api, err := android.ApiLevelFromUser(ctx, *module.properties.Max_device_sdk)
3159 if err == nil {
3160 if moduleMinApi.GreaterThan(api) {
3161 ctx.PropertyErrorf("max_device_sdk", "Can't be less than module's min sdk (%s)", moduleMinApi)
3162 }
3163 }
3164 }
3165}
3166
3167func (module *sdkLibraryXml) validateOnBootclasspathBeforeRequirements(ctx android.ModuleContext) {
3168 moduleMinApi := android.ApiLevelOrPanic(ctx, *module.properties.Sdk_library_min_api_level)
3169 if module.properties.On_bootclasspath_before != nil {
3170 t := android.ApiLevelOrPanic(ctx, "Tiramisu")
3171 // if we use the attribute, then we need to do this validation
3172 if moduleMinApi.LessThan(t) {
3173 // if minAPi is < T, then we need to have min_device_sdk (which only accepts T+)
3174 if module.properties.Min_device_sdk == nil {
3175 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")
3176 }
3177 }
3178 }
3179}
3180
Paul Duffindd46f712020-02-10 13:37:10 +00003181type sdkLibrarySdkMemberType struct {
3182 android.SdkMemberTypeBase
3183}
3184
Paul Duffin296701e2021-07-14 10:29:36 +01003185func (s *sdkLibrarySdkMemberType) AddDependencies(ctx android.SdkDependencyContext, dependencyTag blueprint.DependencyTag, names []string) {
3186 ctx.AddVariationDependencies(nil, dependencyTag, names...)
Paul Duffindd46f712020-02-10 13:37:10 +00003187}
3188
3189func (s *sdkLibrarySdkMemberType) IsInstance(module android.Module) bool {
3190 _, ok := module.(*SdkLibrary)
3191 return ok
3192}
3193
3194func (s *sdkLibrarySdkMemberType) AddPrebuiltModule(ctx android.SdkMemberContext, member android.SdkMember) android.BpModule {
3195 return ctx.SnapshotBuilder().AddPrebuiltModule(member, "java_sdk_library_import")
3196}
3197
3198func (s *sdkLibrarySdkMemberType) CreateVariantPropertiesStruct() android.SdkMemberProperties {
3199 return &sdkLibrarySdkMemberProperties{}
3200}
3201
Paul Duffin976b0e52021-04-27 23:20:26 +01003202var javaSdkLibrarySdkMemberType = &sdkLibrarySdkMemberType{
3203 android.SdkMemberTypeBase{
3204 PropertyName: "java_sdk_libs",
3205 SupportsSdk: true,
3206 },
3207}
3208
Paul Duffindd46f712020-02-10 13:37:10 +00003209type sdkLibrarySdkMemberProperties struct {
3210 android.SdkMemberPropertiesBase
3211
Paul Duffine8409952022-09-22 16:24:46 +01003212 // Stem name for files in the sdk snapshot.
3213 //
3214 // This is used to construct the path names of various sdk library files in the sdk snapshot to
3215 // make sure that they match the finalized versions of those files in prebuilts/sdk.
3216 //
3217 // This property is marked as keep so that it will be kept in all instances of this struct, will
3218 // not be cleared but will be copied to common structs. That is needed because this field is used
3219 // to construct many file names for other parts of this struct and so it needs to be present in
3220 // all structs. If it was not marked as keep then it would be cleared in some structs and so would
3221 // be unavailable for generating file names if there were other properties that were still set.
3222 Stem string `sdk:"keep"`
3223
Paul Duffindd46f712020-02-10 13:37:10 +00003224 // Scope to per scope properties.
Paul Duffin106a3a42022-01-27 16:39:06 +00003225 Scopes map[*apiScope]*scopeProperties
Paul Duffindd46f712020-02-10 13:37:10 +00003226
Paul Duffin3d1248c2020-04-09 00:10:17 +01003227 // The Java stubs source files.
3228 Stub_srcs []string
Paul Duffinf7a64332020-05-13 16:54:55 +01003229
3230 // The naming scheme.
3231 Naming_scheme *string
Paul Duffind7eb1c22020-05-26 20:57:10 +01003232
3233 // True if the java_sdk_library_import is for a shared library, false
3234 // otherwise.
3235 Shared_library *bool
Paul Duffina2ae7e02020-09-11 11:55:00 +01003236
Paul Duffin1267d872021-04-16 17:21:36 +01003237 // True if the stub imports should produce dex jars.
3238 Compile_dex *bool
3239
Paul Duffina2ae7e02020-09-11 11:55:00 +01003240 // The paths to the doctag files to add to the prebuilt.
3241 Doctag_paths android.Paths
Paul Duffin869de142021-07-15 14:14:41 +01003242
3243 Permitted_packages []string
Pedro Loureiro9956e5e2021-09-07 17:21:59 +00003244
3245 // Signals that this shared library is part of the bootclasspath starting
3246 // on the version indicated in this attribute.
3247 //
3248 // This will make platforms at this level and above to ignore
3249 // <uses-library> tags with this library name because the library is already
3250 // available
3251 On_bootclasspath_since *string
3252
3253 // Signals that this shared library was part of the bootclasspath before
3254 // (but not including) the version indicated in this attribute.
3255 //
3256 // The system will automatically add a <uses-library> tag with this library to
3257 // apps that target any SDK less than the version indicated in this attribute.
3258 On_bootclasspath_before *string
3259
3260 // Indicates that PackageManager should ignore this shared library if the
3261 // platform is below the version indicated in this attribute.
3262 //
3263 // This means that the device won't recognise this library as installed.
3264 Min_device_sdk *string
3265
3266 // Indicates that PackageManager should ignore this shared library if the
3267 // platform is above the version indicated in this attribute.
3268 //
3269 // This means that the device won't recognise this library as installed.
3270 Max_device_sdk *string
Jiakai Zhang9c4dc192023-02-09 00:09:24 +08003271
3272 DexPreoptProfileGuided *bool `supported_build_releases:"UpsideDownCake+"`
Paul Duffindd46f712020-02-10 13:37:10 +00003273}
3274
3275type scopeProperties struct {
Paul Duffin1fd005d2020-04-09 01:08:11 +01003276 Jars android.Paths
3277 StubsSrcJar android.Path
3278 CurrentApiFile android.Path
3279 RemovedApiFile android.Path
Paul Duffine7babdb2022-02-10 13:06:54 +00003280 AnnotationsZip android.Path `supported_build_releases:"Tiramisu+"`
Paul Duffin1fd005d2020-04-09 01:08:11 +01003281 SdkVersion string
Paul Duffindd46f712020-02-10 13:37:10 +00003282}
3283
3284func (s *sdkLibrarySdkMemberProperties) PopulateFromVariant(ctx android.SdkMemberContext, variant android.Module) {
3285 sdk := variant.(*SdkLibrary)
3286
Paul Duffine8409952022-09-22 16:24:46 +01003287 // Copy the stem name for files in the sdk snapshot.
3288 s.Stem = sdk.distStem()
3289
Paul Duffin106a3a42022-01-27 16:39:06 +00003290 s.Scopes = make(map[*apiScope]*scopeProperties)
Paul Duffindd46f712020-02-10 13:37:10 +00003291 for _, apiScope := range allApiScopes {
Paul Duffin803a9562020-05-20 11:52:25 +01003292 paths := sdk.findScopePaths(apiScope)
3293 if paths == nil {
3294 continue
3295 }
3296
Paul Duffindd46f712020-02-10 13:37:10 +00003297 jars := paths.stubsImplPath
3298 if len(jars) > 0 {
3299 properties := scopeProperties{}
3300 properties.Jars = jars
Paul Duffin780c5f42020-05-12 15:52:55 +01003301 properties.SdkVersion = sdk.sdkVersionForStubsLibrary(ctx.SdkModuleContext(), apiScope)
Paul Duffin0f8faff2020-05-20 16:18:00 +01003302 properties.StubsSrcJar = paths.stubsSrcJar.Path()
Paul Duffin10269f12020-06-19 18:39:55 +01003303 if paths.currentApiFilePath.Valid() {
3304 properties.CurrentApiFile = paths.currentApiFilePath.Path()
3305 }
3306 if paths.removedApiFilePath.Valid() {
3307 properties.RemovedApiFile = paths.removedApiFilePath.Path()
3308 }
Anton Hanssond78eb762021-09-21 15:25:12 +01003309 // The annotations zip is only available for modules that set annotations_enabled: true.
3310 if paths.annotationsZip.Valid() {
3311 properties.AnnotationsZip = paths.annotationsZip.Path()
3312 }
Paul Duffin106a3a42022-01-27 16:39:06 +00003313 s.Scopes[apiScope] = &properties
Paul Duffindd46f712020-02-10 13:37:10 +00003314 }
3315 }
3316
Paul Duffindfa131e2020-05-15 20:37:11 +01003317 s.Naming_scheme = sdk.commonSdkLibraryProperties.Naming_scheme
Paul Duffind7eb1c22020-05-26 20:57:10 +01003318 s.Shared_library = proptools.BoolPtr(sdk.sharedLibrary())
Paul Duffin1267d872021-04-16 17:21:36 +01003319 s.Compile_dex = sdk.dexProperties.Compile_dex
Paul Duffina2ae7e02020-09-11 11:55:00 +01003320 s.Doctag_paths = sdk.doctagPaths
Paul Duffin869de142021-07-15 14:14:41 +01003321 s.Permitted_packages = sdk.PermittedPackagesForUpdatableBootJars()
Pedro Loureiro9956e5e2021-09-07 17:21:59 +00003322 s.On_bootclasspath_since = sdk.commonSdkLibraryProperties.On_bootclasspath_since
3323 s.On_bootclasspath_before = sdk.commonSdkLibraryProperties.On_bootclasspath_before
3324 s.Min_device_sdk = sdk.commonSdkLibraryProperties.Min_device_sdk
3325 s.Max_device_sdk = sdk.commonSdkLibraryProperties.Max_device_sdk
Jiakai Zhang9c4dc192023-02-09 00:09:24 +08003326
3327 if sdk.dexpreopter.dexpreoptProperties.Dex_preopt_result.Profile_guided {
3328 s.DexPreoptProfileGuided = proptools.BoolPtr(true)
3329 }
Paul Duffindd46f712020-02-10 13:37:10 +00003330}
3331
3332func (s *sdkLibrarySdkMemberProperties) AddToPropertySet(ctx android.SdkMemberContext, propertySet android.BpPropertySet) {
Paul Duffinf7a64332020-05-13 16:54:55 +01003333 if s.Naming_scheme != nil {
3334 propertySet.AddProperty("naming_scheme", proptools.String(s.Naming_scheme))
3335 }
Paul Duffind7eb1c22020-05-26 20:57:10 +01003336 if s.Shared_library != nil {
3337 propertySet.AddProperty("shared_library", *s.Shared_library)
3338 }
Paul Duffin1267d872021-04-16 17:21:36 +01003339 if s.Compile_dex != nil {
3340 propertySet.AddProperty("compile_dex", *s.Compile_dex)
3341 }
Paul Duffin869de142021-07-15 14:14:41 +01003342 if len(s.Permitted_packages) > 0 {
3343 propertySet.AddProperty("permitted_packages", s.Permitted_packages)
3344 }
Jiakai Zhang9c4dc192023-02-09 00:09:24 +08003345 dexPreoptSet := propertySet.AddPropertySet("dex_preopt")
3346 if s.DexPreoptProfileGuided != nil {
3347 dexPreoptSet.AddProperty("profile_guided", proptools.Bool(s.DexPreoptProfileGuided))
3348 }
Paul Duffinf7a64332020-05-13 16:54:55 +01003349
Paul Duffine8409952022-09-22 16:24:46 +01003350 stem := s.Stem
3351
Paul Duffindd46f712020-02-10 13:37:10 +00003352 for _, apiScope := range allApiScopes {
3353 if properties, ok := s.Scopes[apiScope]; ok {
Paul Duffin6b836ba2020-05-13 19:19:49 +01003354 scopeSet := propertySet.AddPropertySet(apiScope.propertyName)
Paul Duffindd46f712020-02-10 13:37:10 +00003355
Paul Duffin958806b2022-05-16 13:10:47 +00003356 scopeDir := apiScope.snapshotRelativeDir()
Paul Duffin3d1248c2020-04-09 00:10:17 +01003357
Paul Duffindd46f712020-02-10 13:37:10 +00003358 var jars []string
3359 for _, p := range properties.Jars {
Paul Duffine8409952022-09-22 16:24:46 +01003360 dest := filepath.Join(scopeDir, stem+"-stubs.jar")
Paul Duffindd46f712020-02-10 13:37:10 +00003361 ctx.SnapshotBuilder().CopyToSnapshot(p, dest)
3362 jars = append(jars, dest)
3363 }
3364 scopeSet.AddProperty("jars", jars)
3365
Paul Duffin22628d52021-05-12 23:13:22 +01003366 if ctx.SdkModuleContext().Config().IsEnvTrue("SOONG_SDK_SNAPSHOT_USE_SRCJAR") {
3367 // Copy the stubs source jar into the snapshot zip as is.
Paul Duffine8409952022-09-22 16:24:46 +01003368 srcJarSnapshotPath := filepath.Join(scopeDir, stem+".srcjar")
Paul Duffin22628d52021-05-12 23:13:22 +01003369 ctx.SnapshotBuilder().CopyToSnapshot(properties.StubsSrcJar, srcJarSnapshotPath)
3370 scopeSet.AddProperty("stub_srcs", []string{srcJarSnapshotPath})
3371 } else {
3372 // Merge the stubs source jar into the snapshot zip so that when it is unpacked
3373 // the source files are also unpacked.
Paul Duffine8409952022-09-22 16:24:46 +01003374 snapshotRelativeDir := filepath.Join(scopeDir, stem+"_stub_sources")
Paul Duffin22628d52021-05-12 23:13:22 +01003375 ctx.SnapshotBuilder().UnzipToSnapshot(properties.StubsSrcJar, snapshotRelativeDir)
3376 scopeSet.AddProperty("stub_srcs", []string{snapshotRelativeDir})
3377 }
Paul Duffin3d1248c2020-04-09 00:10:17 +01003378
Paul Duffin1fd005d2020-04-09 01:08:11 +01003379 if properties.CurrentApiFile != nil {
Paul Duffine8409952022-09-22 16:24:46 +01003380 currentApiSnapshotPath := apiScope.snapshotRelativeCurrentApiTxtPath(stem)
Paul Duffin1fd005d2020-04-09 01:08:11 +01003381 ctx.SnapshotBuilder().CopyToSnapshot(properties.CurrentApiFile, currentApiSnapshotPath)
3382 scopeSet.AddProperty("current_api", currentApiSnapshotPath)
3383 }
3384
3385 if properties.RemovedApiFile != nil {
Paul Duffine8409952022-09-22 16:24:46 +01003386 removedApiSnapshotPath := apiScope.snapshotRelativeRemovedApiTxtPath(stem)
Paul Duffin3dbf9fd2020-06-02 13:00:02 +01003387 ctx.SnapshotBuilder().CopyToSnapshot(properties.RemovedApiFile, removedApiSnapshotPath)
Paul Duffin1fd005d2020-04-09 01:08:11 +01003388 scopeSet.AddProperty("removed_api", removedApiSnapshotPath)
3389 }
3390
Anton Hanssond78eb762021-09-21 15:25:12 +01003391 if properties.AnnotationsZip != nil {
Paul Duffine8409952022-09-22 16:24:46 +01003392 annotationsSnapshotPath := filepath.Join(scopeDir, stem+"_annotations.zip")
Anton Hanssond78eb762021-09-21 15:25:12 +01003393 ctx.SnapshotBuilder().CopyToSnapshot(properties.AnnotationsZip, annotationsSnapshotPath)
3394 scopeSet.AddProperty("annotations", annotationsSnapshotPath)
3395 }
3396
Paul Duffindd46f712020-02-10 13:37:10 +00003397 if properties.SdkVersion != "" {
3398 scopeSet.AddProperty("sdk_version", properties.SdkVersion)
3399 }
3400 }
3401 }
3402
Paul Duffina2ae7e02020-09-11 11:55:00 +01003403 if len(s.Doctag_paths) > 0 {
3404 dests := []string{}
3405 for _, p := range s.Doctag_paths {
3406 dest := filepath.Join("doctags", p.Rel())
3407 ctx.SnapshotBuilder().CopyToSnapshot(p, dest)
3408 dests = append(dests, dest)
3409 }
3410 propertySet.AddProperty("doctag_files", dests)
3411 }
Paul Duffindd46f712020-02-10 13:37:10 +00003412}