blob: 176bda0cec3e2e569911e06c35e0831e39055c4d [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 {
676 if ctx.OtherModuleHasProvider(dep, JavaInfoProvider) {
677 lib := ctx.OtherModuleProvider(dep, JavaInfoProvider).(JavaInfo)
678 paths.stubsHeaderPath = lib.HeaderJars
679 paths.stubsImplPath = lib.ImplementationJars
Paul Duffin1267d872021-04-16 17:21:36 +0100680
681 libDep := dep.(UsesLibraryDependency)
682 paths.stubsDexJarPath = libDep.DexJarBuildPath()
Paul Duffinc8782502020-04-29 20:45:27 +0100683 return nil
684 } else {
Colin Crossdcf71b22021-02-01 13:59:03 -0800685 return fmt.Errorf("expected module that has JavaInfoProvider, e.g. java_library")
Paul Duffinc8782502020-04-29 20:45:27 +0100686 }
687}
688
Paul Duffin0ff08bd2020-04-29 13:30:54 +0100689func (paths *scopePaths) treatDepAsApiStubsProvider(dep android.Module, action func(provider ApiStubsProvider)) error {
690 if apiStubsProvider, ok := dep.(ApiStubsProvider); ok {
691 action(apiStubsProvider)
Paul Duffinc8782502020-04-29 20:45:27 +0100692 return nil
693 } else {
694 return fmt.Errorf("expected module that implements ApiStubsProvider, e.g. droidstubs")
695 }
696}
697
Paul Duffin0f8faff2020-05-20 16:18:00 +0100698func (paths *scopePaths) treatDepAsApiStubsSrcProvider(dep android.Module, action func(provider ApiStubsSrcProvider)) error {
699 if apiStubsProvider, ok := dep.(ApiStubsSrcProvider); ok {
700 action(apiStubsProvider)
701 return nil
702 } else {
703 return fmt.Errorf("expected module that implements ApiStubsSrcProvider, e.g. droidstubs")
704 }
705}
706
Paul Duffin0ff08bd2020-04-29 13:30:54 +0100707func (paths *scopePaths) extractApiInfoFromApiStubsProvider(provider ApiStubsProvider) {
Anton Hanssond78eb762021-09-21 15:25:12 +0100708 paths.annotationsZip = android.OptionalPathForPath(provider.AnnotationsZip())
Paul Duffin0f8faff2020-05-20 16:18:00 +0100709 paths.currentApiFilePath = android.OptionalPathForPath(provider.ApiFilePath())
710 paths.removedApiFilePath = android.OptionalPathForPath(provider.RemovedApiFilePath())
Paul Duffin0ff08bd2020-04-29 13:30:54 +0100711}
712
Colin Crossdcf71b22021-02-01 13:59:03 -0800713func (paths *scopePaths) extractApiInfoFromDep(ctx android.ModuleContext, dep android.Module) error {
Paul Duffin0ff08bd2020-04-29 13:30:54 +0100714 return paths.treatDepAsApiStubsProvider(dep, func(provider ApiStubsProvider) {
715 paths.extractApiInfoFromApiStubsProvider(provider)
716 })
717}
718
Paul Duffin0f8faff2020-05-20 16:18:00 +0100719func (paths *scopePaths) extractStubsSourceInfoFromApiStubsProviders(provider ApiStubsSrcProvider) {
720 paths.stubsSrcJar = android.OptionalPathForPath(provider.StubsSrcJar())
Paul Duffin0ff08bd2020-04-29 13:30:54 +0100721}
722
Colin Crossdcf71b22021-02-01 13:59:03 -0800723func (paths *scopePaths) extractStubsSourceInfoFromDep(ctx android.ModuleContext, dep android.Module) error {
Paul Duffin0f8faff2020-05-20 16:18:00 +0100724 return paths.treatDepAsApiStubsSrcProvider(dep, func(provider ApiStubsSrcProvider) {
Paul Duffin0ff08bd2020-04-29 13:30:54 +0100725 paths.extractStubsSourceInfoFromApiStubsProviders(provider)
726 })
727}
728
Colin Crossdcf71b22021-02-01 13:59:03 -0800729func (paths *scopePaths) extractStubsSourceAndApiInfoFromApiStubsProvider(ctx android.ModuleContext, dep android.Module) error {
Paul Duffin0ff08bd2020-04-29 13:30:54 +0100730 return paths.treatDepAsApiStubsProvider(dep, func(provider ApiStubsProvider) {
731 paths.extractApiInfoFromApiStubsProvider(provider)
732 paths.extractStubsSourceInfoFromApiStubsProviders(provider)
733 })
734}
735
Paul Duffin958806b2022-05-16 13:10:47 +0000736func extractSingleOptionalOutputPath(dep android.Module) (android.OptionalPath, error) {
737 var paths android.Paths
738 if sourceFileProducer, ok := dep.(android.SourceFileProducer); ok {
739 paths = sourceFileProducer.Srcs()
740 } else {
741 return android.OptionalPath{}, fmt.Errorf("module %q does not produce source files", dep)
742 }
743 if len(paths) != 1 {
744 return android.OptionalPath{}, fmt.Errorf("expected one path from %q, got %q", dep, paths)
745 }
746 return android.OptionalPathForPath(paths[0]), nil
747}
748
749func (paths *scopePaths) extractLatestApiPath(ctx android.ModuleContext, dep android.Module) error {
750 outputPath, err := extractSingleOptionalOutputPath(dep)
751 paths.latestApiPath = outputPath
752 return err
753}
754
755func (paths *scopePaths) extractLatestRemovedApiPath(ctx android.ModuleContext, dep android.Module) error {
756 outputPath, err := extractSingleOptionalOutputPath(dep)
757 paths.latestRemovedApiPath = outputPath
758 return err
759}
760
Paul Duffin0ff08bd2020-04-29 13:30:54 +0100761type commonToSdkLibraryAndImportProperties struct {
Paul Duffin1b1e8062020-05-08 13:44:43 +0100762 // The naming scheme to use for the components that this module creates.
763 //
Paul Duffinee9ad5d2020-09-11 13:04:05 +0100764 // If not specified then it defaults to "default".
Paul Duffin1b1e8062020-05-08 13:44:43 +0100765 //
766 // This is a temporary mechanism to simplify conversion from separate modules for each
767 // component that follow a different naming pattern to the default one.
768 //
769 // TODO(b/155480189) - Remove once naming inconsistencies have been resolved.
Paul Duffin0ff08bd2020-04-29 13:30:54 +0100770 Naming_scheme *string
Paul Duffindfa131e2020-05-15 20:37:11 +0100771
772 // Specifies whether this module can be used as an Android shared library; defaults
773 // to true.
774 //
775 // An Android shared library is one that can be referenced in a <uses-library> element
776 // in an AndroidManifest.xml.
777 Shared_library *bool
Paul Duffina2ae7e02020-09-11 11:55:00 +0100778
779 // Files containing information about supported java doc tags.
780 Doctag_files []string `android:"path"`
Pedro Loureiro9956e5e2021-09-07 17:21:59 +0000781
782 // Signals that this shared library is part of the bootclasspath starting
783 // on the version indicated in this attribute.
784 //
785 // This will make platforms at this level and above to ignore
786 // <uses-library> tags with this library name because the library is already
787 // available
788 On_bootclasspath_since *string
789
790 // Signals that this shared library was part of the bootclasspath before
791 // (but not including) the version indicated in this attribute.
792 //
793 // The system will automatically add a <uses-library> tag with this library to
794 // apps that target any SDK less than the version indicated in this attribute.
795 On_bootclasspath_before *string
796
797 // Indicates that PackageManager should ignore this shared library if the
798 // platform is below the version indicated in this attribute.
799 //
800 // This means that the device won't recognise this library as installed.
801 Min_device_sdk *string
802
803 // Indicates that PackageManager should ignore this shared library if the
804 // platform is above the version indicated in this attribute.
805 //
806 // This means that the device won't recognise this library as installed.
807 Max_device_sdk *string
Paul Duffin0ff08bd2020-04-29 13:30:54 +0100808}
809
Paul Duffin71b33cc2021-06-23 11:39:47 +0100810// commonSdkLibraryAndImportModule defines the interface that must be provided by a module that
811// embeds the commonToSdkLibraryAndImport struct.
812type commonSdkLibraryAndImportModule interface {
Paul Duffind796f6f2022-11-23 23:06:05 +0000813 android.Module
Paul Duffin71b33cc2021-06-23 11:39:47 +0100814
815 BaseModuleName() string
816}
817
Paul Duffin56d44902020-01-31 13:36:25 +0000818// Common code between sdk library and sdk library import
819type commonToSdkLibraryAndImport struct {
Paul Duffin71b33cc2021-06-23 11:39:47 +0100820 module commonSdkLibraryAndImportModule
Paul Duffinc3091c82020-05-08 14:16:20 +0100821
Paul Duffin56d44902020-01-31 13:36:25 +0000822 scopePaths map[*apiScope]*scopePaths
Paul Duffin1b1e8062020-05-08 13:44:43 +0100823
824 namingScheme sdkLibraryComponentNamingScheme
825
Paul Duffindfa131e2020-05-15 20:37:11 +0100826 commonSdkLibraryProperties commonToSdkLibraryAndImportProperties
Paul Duffin859fe962020-05-15 10:20:31 +0100827
Paul Duffina2ae7e02020-09-11 11:55:00 +0100828 // Paths to commonSdkLibraryProperties.Doctag_files
829 doctagPaths android.Paths
830
Paul Duffin859fe962020-05-15 10:20:31 +0100831 // Functionality related to this being used as a component of a java_sdk_library.
832 EmbeddableSdkLibraryComponent
Paul Duffin56d44902020-01-31 13:36:25 +0000833}
834
Paul Duffin71b33cc2021-06-23 11:39:47 +0100835func (c *commonToSdkLibraryAndImport) initCommon(module commonSdkLibraryAndImportModule) {
836 c.module = module
Paul Duffin1b1e8062020-05-08 13:44:43 +0100837
Paul Duffin71b33cc2021-06-23 11:39:47 +0100838 module.AddProperties(&c.commonSdkLibraryProperties)
Paul Duffin859fe962020-05-15 10:20:31 +0100839
840 // Initialize this as an sdk library component.
Paul Duffin71b33cc2021-06-23 11:39:47 +0100841 c.initSdkLibraryComponent(module)
Paul Duffin1b1e8062020-05-08 13:44:43 +0100842}
843
844func (c *commonToSdkLibraryAndImport) initCommonAfterDefaultsApplied(ctx android.DefaultableHookContext) bool {
Paul Duffindfa131e2020-05-15 20:37:11 +0100845 schemeProperty := proptools.StringDefault(c.commonSdkLibraryProperties.Naming_scheme, "default")
Paul Duffin1b1e8062020-05-08 13:44:43 +0100846 switch schemeProperty {
847 case "default":
848 c.namingScheme = &defaultNamingScheme{}
849 default:
850 ctx.PropertyErrorf("naming_scheme", "expected 'default' but was %q", schemeProperty)
851 return false
852 }
853
Paul Duffin3f0290e2021-06-30 18:25:36 +0100854 namePtr := proptools.StringPtr(c.module.BaseModuleName())
855 c.sdkLibraryComponentProperties.SdkLibraryName = namePtr
856
Paul Duffindfa131e2020-05-15 20:37:11 +0100857 // Only track this sdk library if this can be used as a shared library.
858 if c.sharedLibrary() {
859 // Use the name specified in the module definition as the owner.
Paul Duffin3f0290e2021-06-30 18:25:36 +0100860 c.sdkLibraryComponentProperties.SdkLibraryToImplicitlyTrack = namePtr
Paul Duffindfa131e2020-05-15 20:37:11 +0100861 }
Paul Duffin859fe962020-05-15 10:20:31 +0100862
Paul Duffin1b1e8062020-05-08 13:44:43 +0100863 return true
Paul Duffinc3091c82020-05-08 14:16:20 +0100864}
865
Paul Duffinea8f8082021-06-24 13:25:57 +0100866// uniqueApexVariations provides common implementation of the ApexModule.UniqueApexVariations
867// method.
868func (c *commonToSdkLibraryAndImport) uniqueApexVariations() bool {
869 // A java_sdk_library that is a shared library produces an XML file that makes the shared library
870 // usable from an AndroidManifest.xml's <uses-library> entry. That XML file contains the name of
871 // the APEX and so it needs a unique variation per APEX.
872 return c.sharedLibrary()
873}
874
Paul Duffina2ae7e02020-09-11 11:55:00 +0100875func (c *commonToSdkLibraryAndImport) generateCommonBuildActions(ctx android.ModuleContext) {
876 c.doctagPaths = android.PathsForModuleSrc(ctx, c.commonSdkLibraryProperties.Doctag_files)
877}
878
Paul Duffineedc5d52020-06-12 17:46:39 +0100879// Module name of the runtime implementation library
880func (c *commonToSdkLibraryAndImport) implLibraryModuleName() string {
Paul Duffin71b33cc2021-06-23 11:39:47 +0100881 return c.module.BaseModuleName() + ".impl"
Paul Duffineedc5d52020-06-12 17:46:39 +0100882}
883
884// Module name of the XML file for the lib
885func (c *commonToSdkLibraryAndImport) xmlPermissionsModuleName() string {
Paul Duffin71b33cc2021-06-23 11:39:47 +0100886 return c.module.BaseModuleName() + sdkXmlFileSuffix
Paul Duffineedc5d52020-06-12 17:46:39 +0100887}
888
Paul Duffinc3091c82020-05-08 14:16:20 +0100889// Name of the java_library module that compiles the stubs source.
890func (c *commonToSdkLibraryAndImport) stubsLibraryModuleName(apiScope *apiScope) string {
Paul Duffinb97b1572021-04-29 21:50:40 +0100891 baseName := c.module.BaseModuleName()
Paul Duffin21787622022-11-25 12:48:20 +0000892 return c.namingScheme.stubsLibraryModuleName(apiScope, baseName)
Paul Duffinc3091c82020-05-08 14:16:20 +0100893}
894
895// Name of the droidstubs module that generates the stubs source and may also
896// generate/check the API.
897func (c *commonToSdkLibraryAndImport) stubsSourceModuleName(apiScope *apiScope) string {
Paul Duffinb97b1572021-04-29 21:50:40 +0100898 baseName := c.module.BaseModuleName()
Paul Duffin21787622022-11-25 12:48:20 +0000899 return c.namingScheme.stubsSourceModuleName(apiScope, baseName)
Paul Duffinc3091c82020-05-08 14:16:20 +0100900}
901
Jihoon Kang1c92c3e2023-03-23 17:44:51 +0000902// Name of the java_api_library module that generates the from-text stubs source
903// and compiles to a jar file.
904func (c *commonToSdkLibraryAndImport) apiLibraryModuleName(apiScope *apiScope) string {
905 baseName := c.module.BaseModuleName()
906 return c.namingScheme.apiLibraryModuleName(apiScope, baseName)
907}
908
Jihoon Kang1147b312023-06-08 23:25:57 +0000909// Name of the java_library module that compiles the stubs
910// generated from source Java files.
911func (c *commonToSdkLibraryAndImport) sourceStubLibraryModuleName(apiScope *apiScope) string {
912 baseName := c.module.BaseModuleName()
913 return c.namingScheme.sourceStubLibraryModuleName(apiScope, baseName)
914}
915
Paul Duffin46dc45a2020-05-14 15:39:10 +0100916// The component names for different outputs of the java_sdk_library.
917//
918// They are similar to the names used for the child modules it creates
919const (
920 stubsSourceComponentName = "stubs.source"
921
922 apiTxtComponentName = "api.txt"
923
924 removedApiTxtComponentName = "removed-api.txt"
Anton Hanssond78eb762021-09-21 15:25:12 +0100925
926 annotationsComponentName = "annotations.zip"
Paul Duffin46dc45a2020-05-14 15:39:10 +0100927)
928
929// A regular expression to match tags that reference a specific stubs component.
930//
931// It will only match if given a valid scope and a valid component. It is verfy strict
932// to ensure it does not accidentally match a similar looking tag that should be processed
933// by the embedded Library.
934var tagSplitter = func() *regexp.Regexp {
935 // Given a list of literal string items returns a regular expression that will
936 // match any one of the items.
937 choice := func(items ...string) string {
938 return `\Q` + strings.Join(items, `\E|\Q`) + `\E`
939 }
940
941 // Regular expression to match one of the scopes.
942 scopesRegexp := choice(allScopeNames...)
943
944 // Regular expression to match one of the components.
Anton Hanssond78eb762021-09-21 15:25:12 +0100945 componentsRegexp := choice(stubsSourceComponentName, apiTxtComponentName, removedApiTxtComponentName, annotationsComponentName)
Paul Duffin46dc45a2020-05-14 15:39:10 +0100946
947 // Regular expression to match any combination of one scope and one component.
948 return regexp.MustCompile(fmt.Sprintf(`^\.(%s)\.(%s)$`, scopesRegexp, componentsRegexp))
949}()
950
951// For OutputFileProducer interface
952//
Anton Hanssond78eb762021-09-21 15:25:12 +0100953// .<scope>.<component name>, for all ComponentNames (for example: .public.removed-api.txt)
Paul Duffin46dc45a2020-05-14 15:39:10 +0100954func (c *commonToSdkLibraryAndImport) commonOutputFiles(tag string) (android.Paths, error) {
955 if groups := tagSplitter.FindStringSubmatch(tag); groups != nil {
956 scopeName := groups[1]
957 component := groups[2]
958
959 if scope, ok := scopeByName[scopeName]; ok {
960 paths := c.findScopePaths(scope)
961 if paths == nil {
Paul Duffin71b33cc2021-06-23 11:39:47 +0100962 return nil, fmt.Errorf("%q does not provide api scope %s", c.module.BaseModuleName(), scopeName)
Paul Duffin46dc45a2020-05-14 15:39:10 +0100963 }
964
965 switch component {
966 case stubsSourceComponentName:
967 if paths.stubsSrcJar.Valid() {
968 return android.Paths{paths.stubsSrcJar.Path()}, nil
969 }
970
971 case apiTxtComponentName:
972 if paths.currentApiFilePath.Valid() {
973 return android.Paths{paths.currentApiFilePath.Path()}, nil
974 }
975
976 case removedApiTxtComponentName:
977 if paths.removedApiFilePath.Valid() {
978 return android.Paths{paths.removedApiFilePath.Path()}, nil
979 }
Anton Hanssond78eb762021-09-21 15:25:12 +0100980
981 case annotationsComponentName:
982 if paths.annotationsZip.Valid() {
983 return android.Paths{paths.annotationsZip.Path()}, nil
984 }
Paul Duffin46dc45a2020-05-14 15:39:10 +0100985 }
986
987 return nil, fmt.Errorf("%s not available for api scope %s", component, scopeName)
988 } else {
989 return nil, fmt.Errorf("unknown scope %s in %s", scope, tag)
990 }
991
992 } else {
Paul Duffina2ae7e02020-09-11 11:55:00 +0100993 switch tag {
994 case ".doctags":
995 if c.doctagPaths != nil {
996 return c.doctagPaths, nil
997 } else {
Paul Duffin71b33cc2021-06-23 11:39:47 +0100998 return nil, fmt.Errorf("no doctag_files specified on %s", c.module.BaseModuleName())
Paul Duffina2ae7e02020-09-11 11:55:00 +0100999 }
1000 }
Paul Duffin46dc45a2020-05-14 15:39:10 +01001001 return nil, nil
1002 }
1003}
1004
Paul Duffin803a9562020-05-20 11:52:25 +01001005func (c *commonToSdkLibraryAndImport) getScopePathsCreateIfNeeded(scope *apiScope) *scopePaths {
Paul Duffin56d44902020-01-31 13:36:25 +00001006 if c.scopePaths == nil {
1007 c.scopePaths = make(map[*apiScope]*scopePaths)
1008 }
1009 paths := c.scopePaths[scope]
1010 if paths == nil {
1011 paths = &scopePaths{}
1012 c.scopePaths[scope] = paths
1013 }
1014
1015 return paths
1016}
1017
Paul Duffin803a9562020-05-20 11:52:25 +01001018func (c *commonToSdkLibraryAndImport) findScopePaths(scope *apiScope) *scopePaths {
1019 if c.scopePaths == nil {
1020 return nil
1021 }
1022
1023 return c.scopePaths[scope]
1024}
1025
1026// If this does not support the requested api scope then find the closest available
1027// scope it does support. Returns nil if no such scope is available.
1028func (c *commonToSdkLibraryAndImport) findClosestScopePath(scope *apiScope) *scopePaths {
Paul Duffind0b9fca2022-09-30 18:11:41 +01001029 for s := scope; s != nil; s = s.canAccess {
Paul Duffin803a9562020-05-20 11:52:25 +01001030 if paths := c.findScopePaths(s); paths != nil {
1031 return paths
1032 }
1033 }
1034
1035 // This should never happen outside tests as public should be the base scope for every
1036 // scope and is enabled by default.
1037 return nil
1038}
1039
Jiyong Parkf1691d22021-03-29 20:11:58 +09001040func (c *commonToSdkLibraryAndImport) selectHeaderJarsForSdkVersion(ctx android.BaseModuleContext, sdkVersion android.SdkSpec) android.Paths {
Paul Duffinb05d4292020-05-20 12:19:10 +01001041
1042 // If a specific numeric version has been requested then use prebuilt versions of the sdk.
Jiyong Park54105c42021-03-31 18:17:53 +09001043 if !sdkVersion.ApiLevel.IsPreview() {
Paul Duffin71b33cc2021-06-23 11:39:47 +01001044 return PrebuiltJars(ctx, c.module.BaseModuleName(), sdkVersion)
Paul Duffinb05d4292020-05-20 12:19:10 +01001045 }
1046
Paul Duffin1267d872021-04-16 17:21:36 +01001047 paths := c.selectScopePaths(ctx, sdkVersion.Kind)
1048 if paths == nil {
1049 return nil
1050 }
1051
1052 return paths.stubsHeaderPath
1053}
1054
1055// selectScopePaths returns the *scopePaths appropriate for the specific kind.
1056//
1057// If the module does not support the specific kind then it will return the *scopePaths for the
1058// closest kind which is a subset of the requested kind. e.g. if requesting android.SdkModule then
1059// it will return *scopePaths for android.SdkSystem if available or android.SdkPublic of not.
1060func (c *commonToSdkLibraryAndImport) selectScopePaths(ctx android.BaseModuleContext, kind android.SdkKind) *scopePaths {
Paul Duffin32cf58a2021-05-18 16:32:50 +01001061 apiScope := sdkKindToApiScope(kind)
Paul Duffinb05d4292020-05-20 12:19:10 +01001062
Paul Duffin803a9562020-05-20 11:52:25 +01001063 paths := c.findClosestScopePath(apiScope)
1064 if paths == nil {
1065 var scopes []string
1066 for _, s := range allApiScopes {
1067 if c.findScopePaths(s) != nil {
1068 scopes = append(scopes, s.name)
1069 }
1070 }
Paul Duffin71b33cc2021-06-23 11:39:47 +01001071 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 +01001072 return nil
1073 }
1074
Paul Duffin1267d872021-04-16 17:21:36 +01001075 return paths
1076}
1077
Paul Duffin32cf58a2021-05-18 16:32:50 +01001078// sdkKindToApiScope maps from android.SdkKind to apiScope.
1079func sdkKindToApiScope(kind android.SdkKind) *apiScope {
1080 var apiScope *apiScope
1081 switch kind {
1082 case android.SdkSystem:
1083 apiScope = apiScopeSystem
1084 case android.SdkModule:
1085 apiScope = apiScopeModuleLib
1086 case android.SdkTest:
1087 apiScope = apiScopeTest
1088 case android.SdkSystemServer:
1089 apiScope = apiScopeSystemServer
1090 default:
1091 apiScope = apiScopePublic
1092 }
1093 return apiScope
1094}
1095
Paul Duffin1267d872021-04-16 17:21:36 +01001096// to satisfy SdkLibraryDependency interface
Martin Stjernholm8be1e6d2021-09-15 03:34:04 +01001097func (c *commonToSdkLibraryAndImport) SdkApiStubDexJar(ctx android.BaseModuleContext, kind android.SdkKind) OptionalDexJarPath {
Paul Duffin1267d872021-04-16 17:21:36 +01001098 paths := c.selectScopePaths(ctx, kind)
1099 if paths == nil {
Martin Stjernholm8be1e6d2021-09-15 03:34:04 +01001100 return makeUnsetDexJarPath()
Paul Duffin1267d872021-04-16 17:21:36 +01001101 }
1102
1103 return paths.stubsDexJarPath
Paul Duffinb05d4292020-05-20 12:19:10 +01001104}
1105
Paul Duffin32cf58a2021-05-18 16:32:50 +01001106// to satisfy SdkLibraryDependency interface
1107func (c *commonToSdkLibraryAndImport) SdkRemovedTxtFile(ctx android.BaseModuleContext, kind android.SdkKind) android.OptionalPath {
1108 apiScope := sdkKindToApiScope(kind)
1109 paths := c.findScopePaths(apiScope)
1110 if paths == nil {
1111 return android.OptionalPath{}
1112 }
1113
1114 return paths.removedApiFilePath
1115}
1116
Paul Duffin859fe962020-05-15 10:20:31 +01001117func (c *commonToSdkLibraryAndImport) sdkComponentPropertiesForChildLibrary() interface{} {
1118 componentProps := &struct {
Paul Duffin3f0290e2021-06-30 18:25:36 +01001119 SdkLibraryName *string
Paul Duffin859fe962020-05-15 10:20:31 +01001120 SdkLibraryToImplicitlyTrack *string
Paul Duffindfa131e2020-05-15 20:37:11 +01001121 }{}
1122
Paul Duffin3f0290e2021-06-30 18:25:36 +01001123 namePtr := proptools.StringPtr(c.module.BaseModuleName())
1124 componentProps.SdkLibraryName = namePtr
1125
Paul Duffindfa131e2020-05-15 20:37:11 +01001126 if c.sharedLibrary() {
Paul Duffin859fe962020-05-15 10:20:31 +01001127 // Mark the stubs library as being components of this java_sdk_library so that
1128 // any app that includes code which depends (directly or indirectly) on the stubs
1129 // library will have the appropriate <uses-library> invocation inserted into its
1130 // manifest if necessary.
Paul Duffin3f0290e2021-06-30 18:25:36 +01001131 componentProps.SdkLibraryToImplicitlyTrack = namePtr
Paul Duffin859fe962020-05-15 10:20:31 +01001132 }
1133
1134 return componentProps
1135}
1136
Paul Duffindfa131e2020-05-15 20:37:11 +01001137func (c *commonToSdkLibraryAndImport) sharedLibrary() bool {
1138 return proptools.BoolDefault(c.commonSdkLibraryProperties.Shared_library, true)
1139}
1140
Paul Duffinf4600f62021-05-13 22:34:45 +01001141// Check if the stub libraries should be compiled for dex
1142func (c *commonToSdkLibraryAndImport) stubLibrariesCompiledForDex() bool {
1143 // Always compile the dex file files for the stub libraries if they will be used on the
1144 // bootclasspath.
1145 return !c.sharedLibrary()
1146}
1147
Paul Duffin859fe962020-05-15 10:20:31 +01001148// Properties related to the use of a module as an component of a java_sdk_library.
1149type SdkLibraryComponentProperties struct {
Paul Duffin3f0290e2021-06-30 18:25:36 +01001150 // The name of the java_sdk_library/_import module.
1151 SdkLibraryName *string `blueprint:"mutated"`
Paul Duffin859fe962020-05-15 10:20:31 +01001152
1153 // The name of the java_sdk_library/_import to add to a <uses-library> entry
1154 // in the AndroidManifest.xml of any Android app that includes code that references
1155 // this module. If not set then no java_sdk_library/_import is tracked.
1156 SdkLibraryToImplicitlyTrack *string `blueprint:"mutated"`
1157}
1158
1159// Structure to be embedded in a module struct that needs to support the
1160// SdkLibraryComponentDependency interface.
1161type EmbeddableSdkLibraryComponent struct {
1162 sdkLibraryComponentProperties SdkLibraryComponentProperties
1163}
1164
Paul Duffin71b33cc2021-06-23 11:39:47 +01001165func (e *EmbeddableSdkLibraryComponent) initSdkLibraryComponent(module android.Module) {
1166 module.AddProperties(&e.sdkLibraryComponentProperties)
Paul Duffin859fe962020-05-15 10:20:31 +01001167}
1168
1169// to satisfy SdkLibraryComponentDependency
Paul Duffin3f0290e2021-06-30 18:25:36 +01001170func (e *EmbeddableSdkLibraryComponent) SdkLibraryName() *string {
1171 return e.sdkLibraryComponentProperties.SdkLibraryName
1172}
1173
1174// to satisfy SdkLibraryComponentDependency
Ulya Trafimovich39b437b2020-09-23 16:42:35 +01001175func (e *EmbeddableSdkLibraryComponent) OptionalSdkLibraryImplementation() *string {
Ulya Trafimovich78645fb2021-07-16 15:29:25 +01001176 // For shared libraries, this is the same as the SDK library name. If a Java library or app
1177 // depends on a component library (e.g. a stub library) it still needs to know the name of the
1178 // run-time library and the corresponding module that provides the implementation. This name is
1179 // passed to manifest_fixer (to be added to AndroidManifest.xml) and added to CLC (to be used
1180 // in dexpreopt).
1181 //
1182 // For non-shared SDK (component or not) libraries this returns `nil`, as they are not
1183 // <uses-library> and should not be added to the manifest or to CLC.
Ulya Trafimovich39b437b2020-09-23 16:42:35 +01001184 return e.sdkLibraryComponentProperties.SdkLibraryToImplicitlyTrack
1185}
1186
Paul Duffin859fe962020-05-15 10:20:31 +01001187// Implemented by modules that are (or possibly could be) a component of a java_sdk_library
1188// (including the java_sdk_library) itself.
1189type SdkLibraryComponentDependency interface {
Ulya Trafimovich31e444e2020-08-14 17:32:16 +01001190 UsesLibraryDependency
1191
Paul Duffin3f0290e2021-06-30 18:25:36 +01001192 // SdkLibraryName returns the name of the java_sdk_library/_import module.
1193 SdkLibraryName() *string
1194
Ulya Trafimovich39b437b2020-09-23 16:42:35 +01001195 // The name of the implementation library for the optional SDK library or nil, if there isn't one.
1196 OptionalSdkLibraryImplementation() *string
Paul Duffin859fe962020-05-15 10:20:31 +01001197}
1198
1199// Make sure that all the module types that are components of java_sdk_library/_import
1200// and which can be referenced (directly or indirectly) from an android app implement
1201// the SdkLibraryComponentDependency interface.
1202var _ SdkLibraryComponentDependency = (*Library)(nil)
1203var _ SdkLibraryComponentDependency = (*Import)(nil)
1204var _ SdkLibraryComponentDependency = (*SdkLibrary)(nil)
Paul Duffineedc5d52020-06-12 17:46:39 +01001205var _ SdkLibraryComponentDependency = (*SdkLibraryImport)(nil)
Paul Duffin859fe962020-05-15 10:20:31 +01001206
Paul Duffin32cf58a2021-05-18 16:32:50 +01001207// Provides access to sdk_version related files, e.g. header and implementation jars.
Paul Duffin859fe962020-05-15 10:20:31 +01001208type SdkLibraryDependency interface {
1209 SdkLibraryComponentDependency
1210
1211 // Get the header jars appropriate for the supplied sdk_version.
1212 //
1213 // These are turbine generated jars so they only change if the externals of the
1214 // class changes but it does not contain and implementation or JavaDoc.
Jiyong Parkf1691d22021-03-29 20:11:58 +09001215 SdkHeaderJars(ctx android.BaseModuleContext, sdkVersion android.SdkSpec) android.Paths
Paul Duffin859fe962020-05-15 10:20:31 +01001216
1217 // Get the implementation jars appropriate for the supplied sdk version.
1218 //
1219 // These are either the implementation jar for the whole sdk library or the implementation
1220 // jars for the stubs. The latter should only be needed when generating JavaDoc as otherwise
1221 // they are identical to the corresponding header jars.
Jiyong Parkf1691d22021-03-29 20:11:58 +09001222 SdkImplementationJars(ctx android.BaseModuleContext, sdkVersion android.SdkSpec) android.Paths
Paul Duffin1267d872021-04-16 17:21:36 +01001223
1224 // SdkApiStubDexJar returns the dex jar for the stubs. It is needed by the hiddenapi processing
1225 // tool which processes dex files.
Martin Stjernholm8be1e6d2021-09-15 03:34:04 +01001226 SdkApiStubDexJar(ctx android.BaseModuleContext, kind android.SdkKind) OptionalDexJarPath
Paul Duffinf4600f62021-05-13 22:34:45 +01001227
Paul Duffin32cf58a2021-05-18 16:32:50 +01001228 // SdkRemovedTxtFile returns the optional path to the removed.txt file for the specified sdk kind.
1229 SdkRemovedTxtFile(ctx android.BaseModuleContext, kind android.SdkKind) android.OptionalPath
1230
Paul Duffinf4600f62021-05-13 22:34:45 +01001231 // sharedLibrary returns true if this can be used as a shared library.
1232 sharedLibrary() bool
Paul Duffin859fe962020-05-15 10:20:31 +01001233}
1234
Inseob Kimc0907f12019-02-08 21:00:45 +09001235type SdkLibrary struct {
Sundong Ahn054b19a2018-10-19 13:46:09 +09001236 Library
Jiyong Parkc678ad32018-04-10 13:07:10 +09001237
Sundong Ahn054b19a2018-10-19 13:46:09 +09001238 sdkLibraryProperties sdkLibraryProperties
Jiyong Parkc678ad32018-04-10 13:07:10 +09001239
Paul Duffin3375e352020-04-28 10:44:03 +01001240 // Map from api scope to the scope specific property structure.
1241 scopeToProperties map[*apiScope]*ApiScopeProperties
1242
Paul Duffin56d44902020-01-31 13:36:25 +00001243 commonToSdkLibraryAndImport
Jiyong Parkc678ad32018-04-10 13:07:10 +09001244}
1245
Inseob Kimc0907f12019-02-08 21:00:45 +09001246var _ SdkLibraryDependency = (*SdkLibrary)(nil)
Colin Cross897d2ed2019-02-11 14:03:51 -08001247
Paul Duffin3375e352020-04-28 10:44:03 +01001248func (module *SdkLibrary) generateTestAndSystemScopesByDefault() bool {
1249 return module.sdkLibraryProperties.Generate_system_and_test_apis
1250}
1251
1252func (module *SdkLibrary) getGeneratedApiScopes(ctx android.EarlyModuleContext) apiScopes {
1253 // Check to see if any scopes have been explicitly enabled. If any have then all
1254 // must be.
1255 anyScopesExplicitlyEnabled := false
1256 for _, scope := range allApiScopes {
1257 scopeProperties := module.scopeToProperties[scope]
1258 if scopeProperties.Enabled != nil {
1259 anyScopesExplicitlyEnabled = true
1260 break
1261 }
Paul Duffind1b3a922020-01-22 11:57:20 +00001262 }
Paul Duffin3375e352020-04-28 10:44:03 +01001263
1264 var generatedScopes apiScopes
1265 enabledScopes := make(map[*apiScope]struct{})
1266 for _, scope := range allApiScopes {
1267 scopeProperties := module.scopeToProperties[scope]
1268 // If any scopes are explicitly enabled then ignore the legacy enabled status.
1269 // This is to ensure that any new usages of this module type do not rely on legacy
1270 // behaviour.
1271 defaultEnabledStatus := false
1272 if anyScopesExplicitlyEnabled {
1273 defaultEnabledStatus = scope.defaultEnabledStatus
1274 } else {
1275 defaultEnabledStatus = scope.legacyEnabledStatus(module)
1276 }
1277 enabled := proptools.BoolDefault(scopeProperties.Enabled, defaultEnabledStatus)
1278 if enabled {
1279 enabledScopes[scope] = struct{}{}
1280 generatedScopes = append(generatedScopes, scope)
1281 }
1282 }
1283
1284 // Now check to make sure that any scope that is extended by an enabled scope is also
1285 // enabled.
1286 for _, scope := range allApiScopes {
1287 if _, ok := enabledScopes[scope]; ok {
1288 extends := scope.extends
1289 if extends != nil {
1290 if _, ok := enabledScopes[extends]; !ok {
1291 ctx.ModuleErrorf("enabled api scope %q depends on disabled scope %q", scope, extends)
1292 }
1293 }
1294 }
1295 }
1296
1297 return generatedScopes
Paul Duffind1b3a922020-01-22 11:57:20 +00001298}
1299
satayev758968a2021-12-06 11:42:40 +00001300var _ android.ModuleWithMinSdkVersionCheck = (*SdkLibrary)(nil)
1301
satayev8f088b02021-12-06 11:40:46 +00001302func (module *SdkLibrary) CheckMinSdkVersion(ctx android.ModuleContext) {
Spandan Das8c9ae7e2023-03-03 21:20:36 +00001303 android.CheckMinSdkVersion(ctx, module.MinSdkVersion(ctx), func(c android.ModuleContext, do android.PayloadDepsCallback) {
satayev8f088b02021-12-06 11:40:46 +00001304 ctx.WalkDeps(func(child android.Module, parent android.Module) bool {
1305 isExternal := !module.depIsInSameApex(ctx, child)
1306 if am, ok := child.(android.ApexModule); ok {
1307 if !do(ctx, parent, am, isExternal) {
1308 return false
1309 }
1310 }
1311 return !isExternal
1312 })
1313 })
1314}
1315
Paul Duffineedc5d52020-06-12 17:46:39 +01001316type sdkLibraryComponentTag struct {
1317 blueprint.BaseDependencyTag
1318 name string
1319}
1320
1321// Mark this tag so dependencies that use it are excluded from visibility enforcement.
1322func (t sdkLibraryComponentTag) ExcludeFromVisibilityEnforcement() {}
1323
1324var xmlPermissionsFileTag = sdkLibraryComponentTag{name: "xml-permissions-file"}
Paul Duffine74ac732020-02-06 13:51:46 +00001325
Jiyong Parke3833882020-02-17 17:28:10 +09001326func IsXmlPermissionsFileDepTag(depTag blueprint.DependencyTag) bool {
Paul Duffineedc5d52020-06-12 17:46:39 +01001327 if dt, ok := depTag.(sdkLibraryComponentTag); ok {
Jiyong Parke3833882020-02-17 17:28:10 +09001328 return dt == xmlPermissionsFileTag
1329 }
1330 return false
1331}
1332
Paul Duffineedc5d52020-06-12 17:46:39 +01001333var implLibraryTag = sdkLibraryComponentTag{name: "impl-library"}
Paul Duffin5df79302020-05-16 15:52:12 +01001334
Paul Duffin44f1d842020-06-26 20:17:02 +01001335// Add the dependencies on the child modules in the component deps mutator.
1336func (module *SdkLibrary) ComponentDepsMutator(ctx android.BottomUpMutatorContext) {
Paul Duffin3375e352020-04-28 10:44:03 +01001337 for _, apiScope := range module.getGeneratedApiScopes(ctx) {
Paul Duffind1b3a922020-01-22 11:57:20 +00001338 // Add dependencies to the stubs library
Spandan Das877f39d2023-03-29 16:19:51 +00001339 stubModuleName := module.stubsLibraryModuleName(apiScope)
Jihoon Kang1147b312023-06-08 23:25:57 +00001340
Spandan Das877f39d2023-03-29 16:19:51 +00001341 ctx.AddVariationDependencies(nil, apiScope.stubsTag, stubModuleName)
Paul Duffind1b3a922020-01-22 11:57:20 +00001342
Paul Duffin15f34ef2020-07-20 18:04:44 +01001343 // Add a dependency on the stubs source in order to access both stubs source and api information.
1344 ctx.AddVariationDependencies(nil, apiScope.stubsSourceAndApiTag, module.stubsSourceModuleName(apiScope))
Paul Duffin958806b2022-05-16 13:10:47 +00001345
1346 if module.compareAgainstLatestApi(apiScope) {
1347 // Add dependencies on the latest finalized version of the API .txt file.
1348 latestApiModuleName := module.latestApiModuleName(apiScope)
1349 ctx.AddDependency(module, apiScope.latestApiModuleTag, latestApiModuleName)
1350
1351 // Add dependencies on the latest finalized version of the remove API .txt file.
1352 latestRemovedApiModuleName := module.latestRemovedApiModuleName(apiScope)
1353 ctx.AddDependency(module, apiScope.latestRemovedApiModuleTag, latestRemovedApiModuleName)
1354 }
Sundong Ahn054b19a2018-10-19 13:46:09 +09001355 }
1356
Paul Duffindfa131e2020-05-15 20:37:11 +01001357 if module.requiresRuntimeImplementationLibrary() {
Paul Duffin5df79302020-05-16 15:52:12 +01001358 // Add dependency to the rule for generating the implementation library.
1359 ctx.AddDependency(module, implLibraryTag, module.implLibraryModuleName())
1360
Paul Duffindfa131e2020-05-15 20:37:11 +01001361 if module.sharedLibrary() {
1362 // Add dependency to the rule for generating the xml permissions file
Paul Duffineedc5d52020-06-12 17:46:39 +01001363 ctx.AddDependency(module, xmlPermissionsFileTag, module.xmlPermissionsModuleName())
Paul Duffindfa131e2020-05-15 20:37:11 +01001364 }
Paul Duffin44f1d842020-06-26 20:17:02 +01001365 }
1366}
Paul Duffine74ac732020-02-06 13:51:46 +00001367
Paul Duffin44f1d842020-06-26 20:17:02 +01001368// Add other dependencies as normal.
1369func (module *SdkLibrary) DepsMutator(ctx android.BottomUpMutatorContext) {
Anton Hanssone77fccc2021-01-20 16:52:41 +00001370 var missingApiModules []string
1371 for _, apiScope := range module.getGeneratedApiScopes(ctx) {
1372 if apiScope.unstable {
1373 continue
1374 }
Paul Duffin958806b2022-05-16 13:10:47 +00001375 if m := module.latestApiModuleName(apiScope); !ctx.OtherModuleExists(m) {
Anton Hanssone77fccc2021-01-20 16:52:41 +00001376 missingApiModules = append(missingApiModules, m)
1377 }
Paul Duffin958806b2022-05-16 13:10:47 +00001378 if m := module.latestRemovedApiModuleName(apiScope); !ctx.OtherModuleExists(m) {
Anton Hanssone77fccc2021-01-20 16:52:41 +00001379 missingApiModules = append(missingApiModules, m)
1380 }
Paul Duffin958806b2022-05-16 13:10:47 +00001381 if m := module.latestIncompatibilitiesModuleName(apiScope); !ctx.OtherModuleExists(m) {
Jaewoong Jung1a97ee02021-03-09 13:25:02 -08001382 missingApiModules = append(missingApiModules, m)
1383 }
Anton Hanssone77fccc2021-01-20 16:52:41 +00001384 }
1385 if len(missingApiModules) != 0 && !module.sdkLibraryProperties.Unsafe_ignore_missing_latest_api {
1386 m := module.Name() + " is missing tracking files for previously released library versions.\n"
1387 m += "You need to do one of the following:\n"
1388 m += "- Add `unsafe_ignore_missing_latest_api: true` to your blueprint (to disable compat tracking)\n"
1389 m += "- Add a set of prebuilt txt files representing the last released version of this library for compat checking.\n"
1390 m += " (the current set of API files can be used as a seed for this compatibility tracking\n"
1391 m += "\n"
1392 m += "The following filegroup modules are missing:\n "
1393 m += strings.Join(missingApiModules, "\n ") + "\n"
1394 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."
1395 ctx.ModuleErrorf(m)
1396 }
Paul Duffin44f1d842020-06-26 20:17:02 +01001397 if module.requiresRuntimeImplementationLibrary() {
Paul Duffindfa131e2020-05-15 20:37:11 +01001398 // Only add the deps for the library if it is actually going to be built.
1399 module.Library.deps(ctx)
1400 }
Jiyong Parkc678ad32018-04-10 13:07:10 +09001401}
1402
Paul Duffin46dc45a2020-05-14 15:39:10 +01001403func (module *SdkLibrary) OutputFiles(tag string) (android.Paths, error) {
1404 paths, err := module.commonOutputFiles(tag)
Colin Cross4acaea92021-12-10 23:05:02 +00001405 if paths != nil || err != nil {
Paul Duffin46dc45a2020-05-14 15:39:10 +01001406 return paths, err
1407 }
Colin Cross4acaea92021-12-10 23:05:02 +00001408 if module.requiresRuntimeImplementationLibrary() {
1409 return module.Library.OutputFiles(tag)
1410 }
1411 if tag == "" {
1412 return nil, nil
1413 }
1414 return nil, fmt.Errorf("unsupported module reference tag %q", tag)
Paul Duffin46dc45a2020-05-14 15:39:10 +01001415}
1416
Inseob Kimc0907f12019-02-08 21:00:45 +09001417func (module *SdkLibrary) GenerateAndroidBuildActions(ctx android.ModuleContext) {
satayev8f088b02021-12-06 11:40:46 +00001418 if proptools.String(module.deviceProperties.Min_sdk_version) != "" {
1419 module.CheckMinSdkVersion(ctx)
1420 }
1421
Paul Duffina2ae7e02020-09-11 11:55:00 +01001422 module.generateCommonBuildActions(ctx)
1423
Paul Duffindfa131e2020-05-15 20:37:11 +01001424 // Only build an implementation library if required.
1425 if module.requiresRuntimeImplementationLibrary() {
Paul Duffin43db9be2019-12-30 17:35:49 +00001426 module.Library.GenerateAndroidBuildActions(ctx)
1427 }
Sundong Ahn054b19a2018-10-19 13:46:09 +09001428
Paul Duffinb97b1572021-04-29 21:50:40 +01001429 // Collate the components exported by this module. All scope specific modules are exported but
1430 // the impl and xml component modules are not.
1431 exportedComponents := map[string]struct{}{}
1432
Sundong Ahn57368eb2018-07-06 11:20:23 +09001433 // Record the paths to the header jars of the library (stubs and impl).
Paul Duffind1b3a922020-01-22 11:57:20 +00001434 // When this java_sdk_library is depended upon from others via "libs" property,
Jiyong Parkc678ad32018-04-10 13:07:10 +09001435 // the recorded paths will be returned depending on the link type of the caller.
1436 ctx.VisitDirectDeps(func(to android.Module) {
Jiyong Parkc678ad32018-04-10 13:07:10 +09001437 tag := ctx.OtherModuleDependencyTag(to)
1438
Paul Duffinc8782502020-04-29 20:45:27 +01001439 // Extract information from any of the scope specific dependencies.
1440 if scopeTag, ok := tag.(scopeDependencyTag); ok {
1441 apiScope := scopeTag.apiScope
Paul Duffin803a9562020-05-20 11:52:25 +01001442 scopePaths := module.getScopePathsCreateIfNeeded(apiScope)
Paul Duffinc8782502020-04-29 20:45:27 +01001443
1444 // Extract information from the dependency. The exact information extracted
1445 // is determined by the nature of the dependency which is determined by the tag.
1446 scopeTag.extractDepInfo(ctx, to, scopePaths)
Paul Duffinb97b1572021-04-29 21:50:40 +01001447
1448 exportedComponents[ctx.OtherModuleName(to)] = struct{}{}
Sundong Ahn20e998b2018-07-24 11:19:26 +09001449 }
Jiyong Parkc678ad32018-04-10 13:07:10 +09001450 })
Paul Duffinb97b1572021-04-29 21:50:40 +01001451
1452 // Make the set of components exported by this module available for use elsewhere.
Cole Faust18994c72023-02-28 16:02:16 -08001453 exportedComponentInfo := android.ExportedComponentsInfo{Components: android.SortedKeys(exportedComponents)}
Paul Duffinb97b1572021-04-29 21:50:40 +01001454 ctx.SetProvider(android.ExportedComponentsInfoProvider, exportedComponentInfo)
Paul Duffin958806b2022-05-16 13:10:47 +00001455
1456 // Provide additional information for inclusion in an sdk's generated .info file.
1457 additionalSdkInfo := map[string]interface{}{}
1458 additionalSdkInfo["dist_stem"] = module.distStem()
Paul Duffine8409952022-09-22 16:24:46 +01001459 baseModuleName := module.distStem()
Paul Duffin958806b2022-05-16 13:10:47 +00001460 scopes := map[string]interface{}{}
1461 additionalSdkInfo["scopes"] = scopes
1462 for scope, scopePaths := range module.scopePaths {
1463 scopeInfo := map[string]interface{}{}
1464 scopes[scope.name] = scopeInfo
1465 scopeInfo["current_api"] = scope.snapshotRelativeCurrentApiTxtPath(baseModuleName)
1466 scopeInfo["removed_api"] = scope.snapshotRelativeRemovedApiTxtPath(baseModuleName)
1467 if p := scopePaths.latestApiPath; p.Valid() {
1468 scopeInfo["latest_api"] = p.Path().String()
1469 }
1470 if p := scopePaths.latestRemovedApiPath; p.Valid() {
1471 scopeInfo["latest_removed_api"] = p.Path().String()
1472 }
1473 }
1474 ctx.SetProvider(android.AdditionalSdkInfoProvider, android.AdditionalSdkInfo{additionalSdkInfo})
Jiyong Parkc678ad32018-04-10 13:07:10 +09001475}
1476
Jiyong Park0b0e1b92019-12-03 13:24:29 +09001477func (module *SdkLibrary) AndroidMkEntries() []android.AndroidMkEntries {
Paul Duffindfa131e2020-05-15 20:37:11 +01001478 if !module.requiresRuntimeImplementationLibrary() {
Paul Duffin43db9be2019-12-30 17:35:49 +00001479 return nil
1480 }
Jiyong Park0b0e1b92019-12-03 13:24:29 +09001481 entriesList := module.Library.AndroidMkEntries()
Yo Chiang07d75072020-06-05 17:43:19 +08001482 if module.sharedLibrary() {
1483 entries := &entriesList[0]
1484 entries.Required = append(entries.Required, module.xmlPermissionsModuleName())
1485 }
Jiyong Park0b0e1b92019-12-03 13:24:29 +09001486 return entriesList
Jiyong Park82484c02018-04-23 21:41:26 +09001487}
1488
Anton Hansson5fd5d242020-03-27 19:43:19 +00001489// The dist path of the stub artifacts
1490func (module *SdkLibrary) apiDistPath(apiScope *apiScope) string {
Colin Crossf0eace92021-06-02 13:02:23 -07001491 return path.Join("apistubs", module.distGroup(), apiScope.name)
Anton Hansson5fd5d242020-03-27 19:43:19 +00001492}
1493
Paul Duffin12ceb462019-12-24 20:31:31 +00001494// Get the sdk version for use when compiling the stubs library.
Paul Duffin780c5f42020-05-12 15:52:55 +01001495func (module *SdkLibrary) sdkVersionForStubsLibrary(mctx android.EarlyModuleContext, apiScope *apiScope) string {
Paul Duffin87a05a32020-05-12 11:50:28 +01001496 scopeProperties := module.scopeToProperties[apiScope]
1497 if scopeProperties.Sdk_version != nil {
1498 return proptools.String(scopeProperties.Sdk_version)
1499 }
1500
Jiyong Parkf1691d22021-03-29 20:11:58 +09001501 sdkDep := decodeSdkDep(mctx, android.SdkContext(&module.Library))
Paul Duffin12ceb462019-12-24 20:31:31 +00001502 if sdkDep.hasStandardLibs() {
1503 // If building against a standard sdk then use the sdk version appropriate for the scope.
Paul Duffind1b3a922020-01-22 11:57:20 +00001504 return apiScope.sdkVersion
Paul Duffin12ceb462019-12-24 20:31:31 +00001505 } else {
1506 // Otherwise, use no system module.
1507 return "none"
1508 }
1509}
1510
Paul Duffin31310252020-11-20 21:26:20 +00001511func (module *SdkLibrary) distStem() string {
1512 return proptools.StringDefault(module.sdkLibraryProperties.Dist_stem, module.BaseModuleName())
1513}
1514
Colin Cross986b69a2021-06-01 13:13:40 -07001515// distGroup returns the subdirectory of the dist path of the stub artifacts.
1516func (module *SdkLibrary) distGroup() string {
Colin Cross59b92bf2021-06-01 14:07:56 -07001517 return proptools.StringDefault(module.sdkLibraryProperties.Dist_group, "unknown")
Colin Cross986b69a2021-06-01 13:13:40 -07001518}
1519
Paul Duffin958806b2022-05-16 13:10:47 +00001520func latestPrebuiltApiModuleName(name string, apiScope *apiScope) string {
1521 return PrebuiltApiModuleName(name, apiScope.name, "latest")
1522}
1523
Paul Duffind1b3a922020-01-22 11:57:20 +00001524func (module *SdkLibrary) latestApiFilegroupName(apiScope *apiScope) string {
Paul Duffin958806b2022-05-16 13:10:47 +00001525 return ":" + module.latestApiModuleName(apiScope)
1526}
1527
1528func (module *SdkLibrary) latestApiModuleName(apiScope *apiScope) string {
1529 return latestPrebuiltApiModuleName(module.distStem(), apiScope)
Jiyong Park58c518b2018-05-12 22:29:12 +09001530}
Jiyong Parkc678ad32018-04-10 13:07:10 +09001531
Paul Duffind1b3a922020-01-22 11:57:20 +00001532func (module *SdkLibrary) latestRemovedApiFilegroupName(apiScope *apiScope) string {
Paul Duffin958806b2022-05-16 13:10:47 +00001533 return ":" + module.latestRemovedApiModuleName(apiScope)
1534}
1535
1536func (module *SdkLibrary) latestRemovedApiModuleName(apiScope *apiScope) string {
1537 return latestPrebuiltApiModuleName(module.distStem()+"-removed", apiScope)
Jiyong Parkc678ad32018-04-10 13:07:10 +09001538}
1539
Jaewoong Jung1a97ee02021-03-09 13:25:02 -08001540func (module *SdkLibrary) latestIncompatibilitiesFilegroupName(apiScope *apiScope) string {
Paul Duffin958806b2022-05-16 13:10:47 +00001541 return ":" + module.latestIncompatibilitiesModuleName(apiScope)
1542}
1543
1544func (module *SdkLibrary) latestIncompatibilitiesModuleName(apiScope *apiScope) string {
1545 return latestPrebuiltApiModuleName(module.distStem()+"-incompatibilities", apiScope)
Jaewoong Jung1a97ee02021-03-09 13:25:02 -08001546}
1547
Jihoon Kang1c92c3e2023-03-23 17:44:51 +00001548func (module *SdkLibrary) contributesToApiSurface(c android.Config) bool {
1549 _, exists := c.GetApiLibraries()[module.Name()]
1550 return exists
1551}
1552
Jihoon Kang0c705a42023-08-02 06:44:57 +00001553// The listed modules are the special java_sdk_libraries where apiScope.kind do not match the
1554// api surface that the module contribute to. For example, the public droidstubs and java_library
1555// do not contribute to the public api surface, but contributes to the core platform api surface.
1556// This method returns the full api surface stub lib that
1557// the generated java_api_library should depend on.
1558func (module *SdkLibrary) alternativeFullApiSurfaceStubLib() string {
1559 if val, ok := apiLibraryAdditionalProperties[module.Name()]; ok {
1560 return val.FullApiSurfaceStubLib
1561 }
1562 return ""
1563}
1564
1565// The listed modules' stubs contents do not match the corresponding txt files,
1566// but require additional api contributions to generate the full stubs.
1567// This method returns the name of the additional api contribution module
1568// for corresponding sdk_library modules.
1569func (module *SdkLibrary) apiLibraryAdditionalApiContribution() string {
1570 if val, ok := apiLibraryAdditionalProperties[module.Name()]; ok {
1571 return val.AdditionalApiContribution
1572 }
1573 return ""
1574}
1575
Anton Hansson944e77d2020-08-19 11:40:22 +01001576func childModuleVisibility(childVisibility []string) []string {
1577 if childVisibility == nil {
1578 // No child visibility set. The child will use the visibility of the sdk_library.
1579 return nil
1580 }
1581
1582 // Prepend an override to ignore the sdk_library's visibility, and rely on the child visibility.
1583 var visibility []string
1584 visibility = append(visibility, "//visibility:override")
1585 visibility = append(visibility, childVisibility...)
1586 return visibility
1587}
1588
Paul Duffin5df79302020-05-16 15:52:12 +01001589// Creates the implementation java library
1590func (module *SdkLibrary) createImplLibrary(mctx android.DefaultableHookContext) {
Anton Hansson944e77d2020-08-19 11:40:22 +01001591 visibility := childModuleVisibility(module.sdkLibraryProperties.Impl_library_visibility)
1592
Paul Duffin5df79302020-05-16 15:52:12 +01001593 props := struct {
Paul Duffin77590a82022-04-28 14:13:30 +00001594 Name *string
1595 Visibility []string
1596 Instrument bool
1597 Libs []string
1598 Static_libs []string
1599 Apex_available []string
Paul Duffin5df79302020-05-16 15:52:12 +01001600 }{
1601 Name: proptools.StringPtr(module.implLibraryModuleName()),
Anton Hansson944e77d2020-08-19 11:40:22 +01001602 Visibility: visibility,
Paul Duffin296cf332020-06-18 21:09:55 +01001603 // Set the instrument property to ensure it is instrumented when instrumentation is required.
1604 Instrument: true,
Anton Hansson7f66efa2020-10-08 14:47:23 +01001605 // Set the impl_only libs. Note that the module's "Libs" get appended as well, via the
1606 // addition of &module.properties below.
1607 Libs: module.sdkLibraryProperties.Impl_only_libs,
Paul Duffin77590a82022-04-28 14:13:30 +00001608 // Set the impl_only static libs. Note that the module's "static_libs" get appended as well, via the
1609 // addition of &module.properties below.
1610 Static_libs: module.sdkLibraryProperties.Impl_only_static_libs,
1611 // Pass the apex_available settings down so that the impl library can be statically
1612 // embedded within a library that is added to an APEX. Needed for updatable-media.
1613 Apex_available: module.ApexAvailable(),
Paul Duffin5df79302020-05-16 15:52:12 +01001614 }
1615
1616 properties := []interface{}{
1617 &module.properties,
1618 &module.protoProperties,
1619 &module.deviceProperties,
Liz Kammera7a64f32020-07-09 15:16:41 -07001620 &module.dexProperties,
Paul Duffin5df79302020-05-16 15:52:12 +01001621 &module.dexpreoptProperties,
Colin Cross014489c2020-06-02 20:09:13 -07001622 &module.linter.properties,
Paul Duffin5df79302020-05-16 15:52:12 +01001623 &props,
1624 module.sdkComponentPropertiesForChildLibrary(),
1625 }
1626 mctx.CreateModule(LibraryFactory, properties...)
1627}
1628
Jiyong Parkc678ad32018-04-10 13:07:10 +09001629// Creates a static java library that has API stubs
Paul Duffinf0229202020-04-29 16:47:28 +01001630func (module *SdkLibrary) createStubsLibrary(mctx android.DefaultableHookContext, apiScope *apiScope) {
Jiyong Parkc678ad32018-04-10 13:07:10 +09001631 props := struct {
Dan Willemsen9f435972020-05-28 15:28:00 -07001632 Name *string
1633 Visibility []string
1634 Srcs []string
1635 Installable *bool
1636 Sdk_version *string
1637 System_modules *string
1638 Patch_module *string
1639 Libs []string
Anton Hanssondae54cd2021-04-21 16:30:10 +01001640 Static_libs []string
Dan Willemsen9f435972020-05-28 15:28:00 -07001641 Compile_dex *bool
1642 Java_version *string
1643 Openjdk9 struct {
Sundong Ahn054b19a2018-10-19 13:46:09 +09001644 Srcs []string
1645 Javacflags []string
1646 }
Anton Hansson5fd5d242020-03-27 19:43:19 +00001647 Dist struct {
1648 Targets []string
1649 Dest *string
1650 Dir *string
1651 Tag *string
1652 }
Jiyong Parkc678ad32018-04-10 13:07:10 +09001653 }{}
1654
Jihoon Kang1147b312023-06-08 23:25:57 +00001655 props.Name = proptools.StringPtr(module.sourceStubLibraryModuleName(apiScope))
Jihoon Kang786df932023-09-07 01:18:31 +00001656 props.Visibility = []string{"//visibility:override", "//visibility:private"}
Jiyong Parkc678ad32018-04-10 13:07:10 +09001657 // sources are generated from the droiddoc
Paul Duffinc3091c82020-05-08 14:16:20 +01001658 props.Srcs = []string{":" + module.stubsSourceModuleName(apiScope)}
Paul Duffin12ceb462019-12-24 20:31:31 +00001659 sdkVersion := module.sdkVersionForStubsLibrary(mctx, apiScope)
Paul Duffin52d398a2019-06-11 12:31:14 +01001660 props.Sdk_version = proptools.StringPtr(sdkVersion)
Paul Duffina18abc22020-05-16 18:54:24 +01001661 props.System_modules = module.deviceProperties.System_modules
1662 props.Patch_module = module.properties.Patch_module
Paul Duffin367ab912019-12-23 19:40:36 +00001663 props.Installable = proptools.BoolPtr(false)
Sundong Ahn054b19a2018-10-19 13:46:09 +09001664 props.Libs = module.sdkLibraryProperties.Stub_only_libs
Mark White9421c4c2023-08-10 00:07:03 +00001665 props.Libs = append(props.Libs, module.scopeToProperties[apiScope].Libs...)
Anton Hanssondae54cd2021-04-21 16:30:10 +01001666 props.Static_libs = module.sdkLibraryProperties.Stub_only_static_libs
Paul Duffine22c2ab2020-05-20 19:35:27 +01001667 // The stub-annotations library contains special versions of the annotations
1668 // with CLASS retention policy, so that they're kept.
1669 if proptools.Bool(module.sdkLibraryProperties.Annotations_enabled) {
1670 props.Libs = append(props.Libs, "stub-annotations")
1671 }
Paul Duffina18abc22020-05-16 18:54:24 +01001672 props.Openjdk9.Srcs = module.properties.Openjdk9.Srcs
1673 props.Openjdk9.Javacflags = module.properties.Openjdk9.Javacflags
Anton Hansson83509b52020-05-21 09:21:57 +01001674 // We compile the stubs for 1.8 in line with the main android.jar stubs, and potential
1675 // interop with older developer tools that don't support 1.9.
1676 props.Java_version = proptools.StringPtr("1.8")
Paul Duffinf4600f62021-05-13 22:34:45 +01001677
Paul Duffin859fe962020-05-15 10:20:31 +01001678 mctx.CreateModule(LibraryFactory, &props, module.sdkComponentPropertiesForChildLibrary())
Jiyong Parkc678ad32018-04-10 13:07:10 +09001679}
1680
Paul Duffin6d0886e2020-04-07 18:49:53 +01001681// Creates a droidstubs module that creates stubs source files from the given full source
Paul Duffinc8782502020-04-29 20:45:27 +01001682// files and also updates and checks the API specification files.
Paul Duffin15f34ef2020-07-20 18:04:44 +01001683func (module *SdkLibrary) createStubsSourcesAndApi(mctx android.DefaultableHookContext, apiScope *apiScope, name string, scopeSpecificDroidstubsArgs []string) {
Jiyong Parkc678ad32018-04-10 13:07:10 +09001684 props := struct {
Sundong Ahn054b19a2018-10-19 13:46:09 +09001685 Name *string
Paul Duffin4911a892020-04-29 23:35:13 +01001686 Visibility []string
Sundong Ahn054b19a2018-10-19 13:46:09 +09001687 Srcs []string
1688 Installable *bool
Paul Duffin52d398a2019-06-11 12:31:14 +01001689 Sdk_version *string
Jihoon Kang3198f3c2023-01-26 08:08:52 +00001690 Api_surface *string
Paul Duffin12ceb462019-12-24 20:31:31 +00001691 System_modules *string
Sundong Ahn054b19a2018-10-19 13:46:09 +09001692 Libs []string
Paul Duffin6877e6d2020-09-25 19:59:14 +01001693 Output_javadoc_comments *bool
Paul Duffin11512472019-02-11 15:55:17 +00001694 Arg_files []string
Sundong Ahn054b19a2018-10-19 13:46:09 +09001695 Args *string
Sundong Ahn054b19a2018-10-19 13:46:09 +09001696 Java_version *string
Paul Duffine22c2ab2020-05-20 19:35:27 +01001697 Annotations_enabled *bool
Sundong Ahn054b19a2018-10-19 13:46:09 +09001698 Merge_annotations_dirs []string
1699 Merge_inclusion_annotations_dirs []string
Paul Duffin0ff08bd2020-04-29 13:30:54 +01001700 Generate_stubs *bool
Anton Hanssone87b03d2020-12-21 15:29:34 +00001701 Previous_api *string
Sundong Ahn054b19a2018-10-19 13:46:09 +09001702 Check_api struct {
Anton Hanssone6056152020-12-31 10:37:27 +00001703 Current ApiToCheck
1704 Last_released ApiToCheck
Paul Duffin160fe412020-05-10 19:32:20 +01001705
1706 Api_lint struct {
1707 Enabled *bool
1708 New_since *string
1709 Baseline_file *string
1710 }
Jiyong Park58c518b2018-05-12 22:29:12 +09001711 }
Sundong Ahn1b92c822018-05-29 11:35:17 +09001712 Aidl struct {
1713 Include_dirs []string
1714 Local_include_dirs []string
1715 }
Paul Duffin040e9062020-11-23 17:41:36 +00001716 Dists []android.Dist
Jiyong Parkc678ad32018-04-10 13:07:10 +09001717 }{}
1718
Paul Duffin7b78b4d2020-04-28 14:08:32 +01001719 // The stubs source processing uses the same compile time classpath when extracting the
1720 // API from the implementation library as it does when compiling it. i.e. the same
1721 // * sdk version
1722 // * system_modules
1723 // * libs (static_libs/libs)
Paul Duffin250e6192019-06-07 10:44:37 +01001724
Paul Duffin0ff08bd2020-04-29 13:30:54 +01001725 props.Name = proptools.StringPtr(name)
Anton Hansson944e77d2020-08-19 11:40:22 +01001726 props.Visibility = childModuleVisibility(module.sdkLibraryProperties.Stubs_source_visibility)
Paul Duffina18abc22020-05-16 18:54:24 +01001727 props.Srcs = append(props.Srcs, module.properties.Srcs...)
Anton Hanssonf8ea3722021-09-16 14:24:13 +01001728 props.Srcs = append(props.Srcs, module.sdkLibraryProperties.Api_srcs...)
Paul Duffina18abc22020-05-16 18:54:24 +01001729 props.Sdk_version = module.deviceProperties.Sdk_version
Jihoon Kang3198f3c2023-01-26 08:08:52 +00001730 props.Api_surface = &apiScope.name
Paul Duffina18abc22020-05-16 18:54:24 +01001731 props.System_modules = module.deviceProperties.System_modules
Jiyong Parkc678ad32018-04-10 13:07:10 +09001732 props.Installable = proptools.BoolPtr(false)
Sundong Ahne6f0b052018-06-05 16:46:14 +09001733 // A droiddoc module has only one Libs property and doesn't distinguish between
1734 // shared libs and static libs. So we need to add both of these libs to Libs property.
Paul Duffina18abc22020-05-16 18:54:24 +01001735 props.Libs = module.properties.Libs
1736 props.Libs = append(props.Libs, module.properties.Static_libs...)
Nikita Ioffed732da72022-11-21 12:38:25 +00001737 props.Libs = append(props.Libs, module.sdkLibraryProperties.Stub_only_libs...)
Mark White9421c4c2023-08-10 00:07:03 +00001738 props.Libs = append(props.Libs, module.scopeToProperties[apiScope].Libs...)
Paul Duffina18abc22020-05-16 18:54:24 +01001739 props.Aidl.Include_dirs = module.deviceProperties.Aidl.Include_dirs
1740 props.Aidl.Local_include_dirs = module.deviceProperties.Aidl.Local_include_dirs
1741 props.Java_version = module.properties.Java_version
Jiyong Parkc678ad32018-04-10 13:07:10 +09001742
Paul Duffine22c2ab2020-05-20 19:35:27 +01001743 props.Annotations_enabled = module.sdkLibraryProperties.Annotations_enabled
Sundong Ahn054b19a2018-10-19 13:46:09 +09001744 props.Merge_annotations_dirs = module.sdkLibraryProperties.Merge_annotations_dirs
1745 props.Merge_inclusion_annotations_dirs = module.sdkLibraryProperties.Merge_inclusion_annotations_dirs
1746
Paul Duffin6d0886e2020-04-07 18:49:53 +01001747 droidstubsArgs := []string{}
Paul Duffin235ffff2019-12-24 10:41:30 +00001748 if len(module.sdkLibraryProperties.Api_packages) != 0 {
Paul Duffin6d0886e2020-04-07 18:49:53 +01001749 droidstubsArgs = append(droidstubsArgs, "--stub-packages "+strings.Join(module.sdkLibraryProperties.Api_packages, ":"))
Paul Duffin235ffff2019-12-24 10:41:30 +00001750 }
1751 if len(module.sdkLibraryProperties.Hidden_api_packages) != 0 {
Paul Duffin6d0886e2020-04-07 18:49:53 +01001752 droidstubsArgs = append(droidstubsArgs,
Paul Duffin235ffff2019-12-24 10:41:30 +00001753 android.JoinWithPrefix(module.sdkLibraryProperties.Hidden_api_packages, " --hide-package "))
1754 }
Paul Duffin6d0886e2020-04-07 18:49:53 +01001755 droidstubsArgs = append(droidstubsArgs, module.sdkLibraryProperties.Droiddoc_options...)
Anton Hanssonfd1c0d22023-11-02 15:18:09 +00001756 disabledWarnings := []string{"HiddenSuperclass"}
1757 if proptools.BoolDefault(module.sdkLibraryProperties.Api_lint.Legacy_errors_allowed, true) {
1758 disabledWarnings = append(disabledWarnings,
1759 "BroadcastBehavior",
1760 "DeprecationMismatch",
1761 "MissingPermission",
1762 "SdkConstant",
1763 "Todo",
1764 )
Paul Duffin235ffff2019-12-24 10:41:30 +00001765 }
Paul Duffin6d0886e2020-04-07 18:49:53 +01001766 droidstubsArgs = append(droidstubsArgs, android.JoinWithPrefix(disabledWarnings, "--hide "))
Sundong Ahnfb2721f2018-09-17 13:23:09 +09001767
Paul Duffin6877e6d2020-09-25 19:59:14 +01001768 // Output Javadoc comments for public scope.
1769 if apiScope == apiScopePublic {
1770 props.Output_javadoc_comments = proptools.BoolPtr(true)
1771 }
1772
Paul Duffin1fb487d2020-04-07 18:50:10 +01001773 // Add in scope specific arguments.
Paul Duffin0ff08bd2020-04-29 13:30:54 +01001774 droidstubsArgs = append(droidstubsArgs, scopeSpecificDroidstubsArgs...)
Paul Duffin11512472019-02-11 15:55:17 +00001775 props.Arg_files = module.sdkLibraryProperties.Droiddoc_option_files
Paul Duffin6d0886e2020-04-07 18:49:53 +01001776 props.Args = proptools.StringPtr(strings.Join(droidstubsArgs, " "))
Jiyong Parkc678ad32018-04-10 13:07:10 +09001777
Paul Duffin15f34ef2020-07-20 18:04:44 +01001778 // List of APIs identified from the provided source files are created. They are later
1779 // compared against to the not-yet-released (a.k.a current) list of APIs and to the
1780 // last-released (a.k.a numbered) list of API.
1781 currentApiFileName := apiScope.apiFilePrefix + "current.txt"
1782 removedApiFileName := apiScope.apiFilePrefix + "removed.txt"
1783 apiDir := module.getApiDir()
1784 currentApiFileName = path.Join(apiDir, currentApiFileName)
1785 removedApiFileName = path.Join(apiDir, removedApiFileName)
Jiyong Parkc678ad32018-04-10 13:07:10 +09001786
Paul Duffin15f34ef2020-07-20 18:04:44 +01001787 // check against the not-yet-release API
1788 props.Check_api.Current.Api_file = proptools.StringPtr(currentApiFileName)
1789 props.Check_api.Current.Removed_api_file = proptools.StringPtr(removedApiFileName)
Jiyong Park58c518b2018-05-12 22:29:12 +09001790
Paul Duffin958806b2022-05-16 13:10:47 +00001791 if module.compareAgainstLatestApi(apiScope) {
Paul Duffin15f34ef2020-07-20 18:04:44 +01001792 // check against the latest released API
1793 latestApiFilegroupName := proptools.StringPtr(module.latestApiFilegroupName(apiScope))
Anton Hanssone87b03d2020-12-21 15:29:34 +00001794 props.Previous_api = latestApiFilegroupName
Paul Duffin15f34ef2020-07-20 18:04:44 +01001795 props.Check_api.Last_released.Api_file = latestApiFilegroupName
1796 props.Check_api.Last_released.Removed_api_file = proptools.StringPtr(
1797 module.latestRemovedApiFilegroupName(apiScope))
Jaewoong Jung1a97ee02021-03-09 13:25:02 -08001798 props.Check_api.Last_released.Baseline_file = proptools.StringPtr(
1799 module.latestIncompatibilitiesFilegroupName(apiScope))
Paul Duffin160fe412020-05-10 19:32:20 +01001800
Paul Duffin15f34ef2020-07-20 18:04:44 +01001801 if proptools.Bool(module.sdkLibraryProperties.Api_lint.Enabled) {
1802 // Enable api lint.
1803 props.Check_api.Api_lint.Enabled = proptools.BoolPtr(true)
1804 props.Check_api.Api_lint.New_since = latestApiFilegroupName
Paul Duffin160fe412020-05-10 19:32:20 +01001805
Paul Duffin15f34ef2020-07-20 18:04:44 +01001806 // If it exists then pass a lint-baseline.txt through to droidstubs.
1807 baselinePath := path.Join(apiDir, apiScope.apiFilePrefix+"lint-baseline.txt")
1808 baselinePathRelativeToRoot := path.Join(mctx.ModuleDir(), baselinePath)
1809 paths, err := mctx.GlobWithDeps(baselinePathRelativeToRoot, nil)
1810 if err != nil {
1811 mctx.ModuleErrorf("error checking for presence of %s: %s", baselinePathRelativeToRoot, err)
1812 }
1813 if len(paths) == 1 {
1814 props.Check_api.Api_lint.Baseline_file = proptools.StringPtr(baselinePath)
1815 } else if len(paths) != 0 {
1816 mctx.ModuleErrorf("error checking for presence of %s: expected one path, found: %v", baselinePathRelativeToRoot, paths)
Paul Duffin160fe412020-05-10 19:32:20 +01001817 }
1818 }
Paul Duffin15f34ef2020-07-20 18:04:44 +01001819 }
Jiyong Park58c518b2018-05-12 22:29:12 +09001820
Paul Duffin15f34ef2020-07-20 18:04:44 +01001821 if !Bool(module.sdkLibraryProperties.No_dist) {
Paul Duffin040e9062020-11-23 17:41:36 +00001822 // Dist the api txt and removed api txt artifacts for sdk builds.
1823 distDir := proptools.StringPtr(path.Join(module.apiDistPath(apiScope), "api"))
1824 for _, p := range []struct {
1825 tag string
1826 pattern string
1827 }{
1828 {tag: ".api.txt", pattern: "%s.txt"},
1829 {tag: ".removed-api.txt", pattern: "%s-removed.txt"},
1830 } {
1831 props.Dists = append(props.Dists, android.Dist{
1832 Targets: []string{"sdk", "win_sdk"},
1833 Dir: distDir,
1834 Dest: proptools.StringPtr(fmt.Sprintf(p.pattern, module.distStem())),
1835 Tag: proptools.StringPtr(p.tag),
1836 })
1837 }
Anton Hansson5fd5d242020-03-27 19:43:19 +00001838 }
1839
Spandan Das2cc80ba2023-10-27 17:21:52 +00001840 mctx.CreateModule(DroidstubsFactory, &props, module.sdkComponentPropertiesForChildLibrary()).(*Droidstubs).CallHookIfAvailable(mctx)
Jiyong Parkc678ad32018-04-10 13:07:10 +09001841}
1842
Jihoon Kang0c705a42023-08-02 06:44:57 +00001843func (module *SdkLibrary) createApiLibrary(mctx android.DefaultableHookContext, apiScope *apiScope, alternativeFullApiSurfaceStub string) {
Jihoon Kang1c92c3e2023-03-23 17:44:51 +00001844 props := struct {
Jihoon Kangca198c22023-06-22 23:13:51 +00001845 Name *string
1846 Visibility []string
1847 Api_contributions []string
1848 Libs []string
1849 Static_libs []string
1850 Full_api_surface_stub *string
Jihoon Kang4ec24872023-10-05 17:26:09 +00001851 System_modules *string
Jihoon Kang063ec002023-06-28 01:16:23 +00001852 Enable_validation *bool
Jihoon Kang1c92c3e2023-03-23 17:44:51 +00001853 }{}
1854
1855 props.Name = proptools.StringPtr(module.apiLibraryModuleName(apiScope))
Jihoon Kang786df932023-09-07 01:18:31 +00001856 props.Visibility = []string{"//visibility:override", "//visibility:private"}
Jihoon Kang1c92c3e2023-03-23 17:44:51 +00001857
1858 apiContributions := []string{}
1859
1860 // Api surfaces are not independent of each other, but have subset relationships,
1861 // and so does the api files. To generate from-text stubs for api surfaces other than public,
1862 // all subset api domains' api_contriubtions must be added as well.
1863 scope := apiScope
1864 for scope != nil {
1865 apiContributions = append(apiContributions, module.stubsSourceModuleName(scope)+".api.contribution")
1866 scope = scope.extends
1867 }
Jihoon Kang0c705a42023-08-02 06:44:57 +00001868 if apiScope == apiScopePublic {
1869 additionalApiContribution := module.apiLibraryAdditionalApiContribution()
1870 if additionalApiContribution != "" {
1871 apiContributions = append(apiContributions, additionalApiContribution)
1872 }
1873 }
Jihoon Kang1c92c3e2023-03-23 17:44:51 +00001874
1875 props.Api_contributions = apiContributions
1876 props.Libs = module.properties.Libs
1877 props.Libs = append(props.Libs, module.sdkLibraryProperties.Stub_only_libs...)
Mark White9421c4c2023-08-10 00:07:03 +00001878 props.Libs = append(props.Libs, module.scopeToProperties[apiScope].Libs...)
Jihoon Kang1c92c3e2023-03-23 17:44:51 +00001879 props.Libs = append(props.Libs, "stub-annotations")
1880 props.Static_libs = module.sdkLibraryProperties.Stub_only_static_libs
Jihoon Kange7ee2562023-07-25 05:51:46 +00001881 props.Full_api_surface_stub = proptools.StringPtr(apiScope.kind.DefaultJavaLibraryName())
Jihoon Kang0c705a42023-08-02 06:44:57 +00001882 if alternativeFullApiSurfaceStub != "" {
1883 props.Full_api_surface_stub = proptools.StringPtr(alternativeFullApiSurfaceStub)
1884 }
Jihoon Kang1c92c3e2023-03-23 17:44:51 +00001885
1886 // android_module_lib_stubs_current.from-text only comprises api contributions from art, conscrypt and i18n.
1887 // Thus, replace with android_module_lib_stubs_current_full.from-text, which comprises every api domains.
1888 if apiScope.kind == android.SdkModule {
Jihoon Kangca198c22023-06-22 23:13:51 +00001889 props.Full_api_surface_stub = proptools.StringPtr(apiScope.kind.DefaultJavaLibraryName() + "_full.from-text")
Jihoon Kang1c92c3e2023-03-23 17:44:51 +00001890 }
1891
Jihoon Kangd30ac8a2023-10-09 18:00:17 +00001892 // java_sdk_library modules that set sdk_version as none does not depend on other api
1893 // domains. Therefore, java_api_library created from such modules should not depend on
1894 // full_api_surface_stubs but create and compile stubs by the java_api_library module
1895 // itself.
1896 if module.SdkVersion(mctx).Kind == android.SdkNone {
1897 props.Full_api_surface_stub = nil
1898 }
1899
Jihoon Kang4ec24872023-10-05 17:26:09 +00001900 props.System_modules = module.deviceProperties.System_modules
Jihoon Kang063ec002023-06-28 01:16:23 +00001901 props.Enable_validation = proptools.BoolPtr(true)
Jihoon Kang4ec24872023-10-05 17:26:09 +00001902
Spandan Das2cc80ba2023-10-27 17:21:52 +00001903 mctx.CreateModule(ApiLibraryFactory, &props, module.sdkComponentPropertiesForChildLibrary())
Jihoon Kang1c92c3e2023-03-23 17:44:51 +00001904}
1905
Jihoon Kang1147b312023-06-08 23:25:57 +00001906func (module *SdkLibrary) createTopLevelStubsLibrary(
1907 mctx android.DefaultableHookContext, apiScope *apiScope, contributesToApiSurface bool) {
1908 props := struct {
1909 Name *string
1910 Visibility []string
1911 Sdk_version *string
1912 Static_libs []string
1913 System_modules *string
1914 Dist struct {
1915 Targets []string
1916 Dest *string
1917 Dir *string
1918 Tag *string
1919 }
1920 Compile_dex *bool
1921 }{}
1922 props.Name = proptools.StringPtr(module.stubsLibraryModuleName(apiScope))
1923 props.Visibility = childModuleVisibility(module.sdkLibraryProperties.Stubs_library_visibility)
1924 sdkVersion := module.sdkVersionForStubsLibrary(mctx, apiScope)
1925 props.Sdk_version = proptools.StringPtr(sdkVersion)
1926
1927 // Add the stub compiling java_library/java_api_library as static lib based on build config
1928 staticLib := module.sourceStubLibraryModuleName(apiScope)
1929 if mctx.Config().BuildFromTextStub() && contributesToApiSurface {
1930 staticLib = module.apiLibraryModuleName(apiScope)
1931 }
1932 props.Static_libs = append(props.Static_libs, staticLib)
1933 props.System_modules = module.deviceProperties.System_modules
1934
1935 // Dist the class jar artifact for sdk builds.
1936 if !Bool(module.sdkLibraryProperties.No_dist) {
1937 props.Dist.Targets = []string{"sdk", "win_sdk"}
1938 props.Dist.Dest = proptools.StringPtr(fmt.Sprintf("%v.jar", module.distStem()))
1939 props.Dist.Dir = proptools.StringPtr(module.apiDistPath(apiScope))
1940 props.Dist.Tag = proptools.StringPtr(".jar")
1941 }
1942
1943 // The imports need to be compiled to dex if the java_sdk_library requests it.
1944 compileDex := module.dexProperties.Compile_dex
1945 if module.stubLibrariesCompiledForDex() {
1946 compileDex = proptools.BoolPtr(true)
1947 }
1948 props.Compile_dex = compileDex
1949
1950 mctx.CreateModule(LibraryFactory, &props, module.sdkComponentPropertiesForChildLibrary())
1951}
1952
Paul Duffin958806b2022-05-16 13:10:47 +00001953func (module *SdkLibrary) compareAgainstLatestApi(apiScope *apiScope) bool {
1954 return !(apiScope.unstable || module.sdkLibraryProperties.Unsafe_ignore_missing_latest_api)
1955}
1956
Paul Duffinea8f8082021-06-24 13:25:57 +01001957// Implements android.ApexModule
Jooyung Han5e9013b2020-03-10 06:23:13 +09001958func (module *SdkLibrary) DepIsInSameApex(mctx android.BaseModuleContext, dep android.Module) bool {
1959 depTag := mctx.OtherModuleDependencyTag(dep)
1960 if depTag == xmlPermissionsFileTag {
1961 return true
1962 }
1963 return module.Library.DepIsInSameApex(mctx, dep)
1964}
1965
Paul Duffinea8f8082021-06-24 13:25:57 +01001966// Implements android.ApexModule
1967func (module *SdkLibrary) UniqueApexVariations() bool {
1968 return module.uniqueApexVariations()
1969}
1970
Jihoon Kang80456fd2023-11-15 19:22:14 +00001971func (module *SdkLibrary) ContributeToApi() bool {
1972 return proptools.BoolDefault(module.sdkLibraryProperties.Contribute_to_android_api, false)
1973}
1974
Jiyong Parkc678ad32018-04-10 13:07:10 +09001975// Creates the xml file that publicizes the runtime library
Paul Duffinf0229202020-04-29 16:47:28 +01001976func (module *SdkLibrary) createXmlFile(mctx android.DefaultableHookContext) {
Spandan Das8c9ae7e2023-03-03 21:20:36 +00001977 moduleMinApiLevel := module.Library.MinSdkVersion(mctx)
Pedro Loureiroc3621422021-09-28 15:40:23 +00001978 var moduleMinApiLevelStr = moduleMinApiLevel.String()
1979 if moduleMinApiLevel == android.NoneApiLevel {
1980 moduleMinApiLevelStr = "current"
1981 }
Jiyong Parke3833882020-02-17 17:28:10 +09001982 props := struct {
Pedro Loureiroc3621422021-09-28 15:40:23 +00001983 Name *string
1984 Lib_name *string
1985 Apex_available []string
1986 On_bootclasspath_since *string
1987 On_bootclasspath_before *string
1988 Min_device_sdk *string
1989 Max_device_sdk *string
1990 Sdk_library_min_api_level *string
Jamie Garsidee570ace2023-11-27 12:07:36 +00001991 Uses_libs_dependencies []string
Jiyong Parke3833882020-02-17 17:28:10 +09001992 }{
Pedro Loureiroc3621422021-09-28 15:40:23 +00001993 Name: proptools.StringPtr(module.xmlPermissionsModuleName()),
1994 Lib_name: proptools.StringPtr(module.BaseModuleName()),
1995 Apex_available: module.ApexProperties.Apex_available,
1996 On_bootclasspath_since: module.commonSdkLibraryProperties.On_bootclasspath_since,
1997 On_bootclasspath_before: module.commonSdkLibraryProperties.On_bootclasspath_before,
1998 Min_device_sdk: module.commonSdkLibraryProperties.Min_device_sdk,
1999 Max_device_sdk: module.commonSdkLibraryProperties.Max_device_sdk,
2000 Sdk_library_min_api_level: &moduleMinApiLevelStr,
Jamie Garsidee570ace2023-11-27 12:07:36 +00002001 Uses_libs_dependencies: module.usesLibraryProperties.Uses_libs,
Jiyong Parkc678ad32018-04-10 13:07:10 +09002002 }
Jiyong Parke3833882020-02-17 17:28:10 +09002003
Jiyong Parke3833882020-02-17 17:28:10 +09002004 mctx.CreateModule(sdkLibraryXmlFactory, &props)
Jiyong Parkc678ad32018-04-10 13:07:10 +09002005}
2006
Jiyong Parkf1691d22021-03-29 20:11:58 +09002007func PrebuiltJars(ctx android.BaseModuleContext, baseName string, s android.SdkSpec) android.Paths {
Jiyong Park54105c42021-03-31 18:17:53 +09002008 var ver android.ApiLevel
Jiyong Parkf1691d22021-03-29 20:11:58 +09002009 var kind android.SdkKind
2010 if s.UsePrebuilt(ctx) {
Jiyong Park54105c42021-03-31 18:17:53 +09002011 ver = s.ApiLevel
Jiyong Parkf1691d22021-03-29 20:11:58 +09002012 kind = s.Kind
Jiyong Parkc678ad32018-04-10 13:07:10 +09002013 } else {
Jiyong Park6a927c42020-01-21 02:03:43 +09002014 // We don't have prebuilt SDK for the specific sdkVersion.
2015 // Instead of breaking the build, fallback to use "system_current"
Jiyong Park54105c42021-03-31 18:17:53 +09002016 ver = android.FutureApiLevel
Jiyong Parkf1691d22021-03-29 20:11:58 +09002017 kind = android.SdkSystem
Sundong Ahn054b19a2018-10-19 13:46:09 +09002018 }
Jiyong Park6a927c42020-01-21 02:03:43 +09002019
2020 dir := filepath.Join("prebuilts", "sdk", ver.String(), kind.String())
Paul Duffin50061512020-01-21 16:31:05 +00002021 jar := filepath.Join(dir, baseName+".jar")
Sundong Ahn054b19a2018-10-19 13:46:09 +09002022 jarPath := android.ExistentPathForSource(ctx, jar)
Sundong Ahnae418ac2019-02-28 15:01:28 +09002023 if !jarPath.Valid() {
Colin Cross07c88562020-01-07 09:34:44 -08002024 if ctx.Config().AllowMissingDependencies() {
2025 return android.Paths{android.PathForSource(ctx, jar)}
2026 } else {
Jiyong Parkf1691d22021-03-29 20:11:58 +09002027 ctx.PropertyErrorf("sdk_library", "invalid sdk version %q, %q does not exist", s.Raw, jar)
Colin Cross07c88562020-01-07 09:34:44 -08002028 }
Sundong Ahnae418ac2019-02-28 15:01:28 +09002029 return nil
2030 }
Sundong Ahn054b19a2018-10-19 13:46:09 +09002031 return android.Paths{jarPath.Path()}
2032}
2033
Colin Crossaede88c2020-08-11 12:17:01 -07002034// 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 +01002035//
2036// If either this or the other module are on the platform then this will return
2037// false.
Colin Cross56a83212020-09-15 18:30:11 -07002038func withinSameApexesAs(ctx android.BaseModuleContext, other android.Module) bool {
2039 apexInfo := ctx.Provider(android.ApexInfoProvider).(android.ApexInfo)
2040 otherApexInfo := ctx.OtherModuleProvider(other, android.ApexInfoProvider).(android.ApexInfo)
Jiyong Parkab50b072021-05-12 17:13:56 +09002041 return len(otherApexInfo.InApexVariants) > 0 && reflect.DeepEqual(apexInfo.InApexVariants, otherApexInfo.InApexVariants)
Paul Duffin9b879592020-05-26 13:21:35 +01002042}
2043
Jiyong Parkf1691d22021-03-29 20:11:58 +09002044func (module *SdkLibrary) sdkJars(ctx android.BaseModuleContext, sdkVersion android.SdkSpec, headerJars bool) android.Paths {
Jiyong Park932cdfe2020-05-28 00:19:53 +09002045 // If the client doesn't set sdk_version, but if this library prefers stubs over
2046 // the impl library, let's provide the widest API surface possible. To do so,
2047 // force override sdk_version to module_current so that the closest possible API
2048 // surface could be found in selectHeaderJarsForSdkVersion
Jiyong Parkf1691d22021-03-29 20:11:58 +09002049 if module.defaultsToStubs() && !sdkVersion.Specified() {
Jiyong Park92315372021-04-02 08:45:46 +09002050 sdkVersion = android.SdkSpecFrom(ctx, "module_current")
Jiyong Park932cdfe2020-05-28 00:19:53 +09002051 }
Paul Duffind1b3a922020-01-22 11:57:20 +00002052
Paul Duffindaaa3322020-05-26 18:13:57 +01002053 // Only provide access to the implementation library if it is actually built.
2054 if module.requiresRuntimeImplementationLibrary() {
2055 // Check any special cases for java_sdk_library.
2056 //
2057 // Only allow access to the implementation library in the following condition:
2058 // * No sdk_version specified on the referencing module.
Paul Duffin9b879592020-05-26 13:21:35 +01002059 // * The referencing module is in the same apex as this.
Jiyong Parkf1691d22021-03-29 20:11:58 +09002060 if sdkVersion.Kind == android.SdkPrivate || withinSameApexesAs(ctx, module) {
Paul Duffindaaa3322020-05-26 18:13:57 +01002061 if headerJars {
2062 return module.HeaderJars()
2063 } else {
2064 return module.ImplementationJars()
2065 }
Sundong Ahn054b19a2018-10-19 13:46:09 +09002066 }
Jiyong Parkc678ad32018-04-10 13:07:10 +09002067 }
Paul Duffinb05d4292020-05-20 12:19:10 +01002068
Paul Duffin23970f42020-05-20 14:20:02 +01002069 return module.selectHeaderJarsForSdkVersion(ctx, sdkVersion)
Jiyong Parkc678ad32018-04-10 13:07:10 +09002070}
2071
Sundong Ahn241cd372018-07-13 16:16:44 +09002072// to satisfy SdkLibraryDependency interface
Jiyong Parkf1691d22021-03-29 20:11:58 +09002073func (module *SdkLibrary) SdkHeaderJars(ctx android.BaseModuleContext, sdkVersion android.SdkSpec) android.Paths {
Paul Duffind1b3a922020-01-22 11:57:20 +00002074 return module.sdkJars(ctx, sdkVersion, true /*headerJars*/)
2075}
2076
2077// to satisfy SdkLibraryDependency interface
Jiyong Parkf1691d22021-03-29 20:11:58 +09002078func (module *SdkLibrary) SdkImplementationJars(ctx android.BaseModuleContext, sdkVersion android.SdkSpec) android.Paths {
Paul Duffind1b3a922020-01-22 11:57:20 +00002079 return module.sdkJars(ctx, sdkVersion, false /*headerJars*/)
Sundong Ahn241cd372018-07-13 16:16:44 +09002080}
2081
Colin Cross571cccf2019-02-04 11:22:08 -08002082var javaSdkLibrariesKey = android.NewOnceKey("javaSdkLibraries")
2083
Jiyong Park82484c02018-04-23 21:41:26 +09002084func javaSdkLibraries(config android.Config) *[]string {
Colin Cross571cccf2019-02-04 11:22:08 -08002085 return config.Once(javaSdkLibrariesKey, func() interface{} {
Jiyong Park82484c02018-04-23 21:41:26 +09002086 return &[]string{}
2087 }).(*[]string)
2088}
2089
Paul Duffin749f98f2019-12-30 17:23:46 +00002090func (module *SdkLibrary) getApiDir() string {
2091 return proptools.StringDefault(module.sdkLibraryProperties.Api_dir, "api")
2092}
2093
Jiyong Parkc678ad32018-04-10 13:07:10 +09002094// For a java_sdk_library module, create internal modules for stubs, docs,
2095// runtime libs and xml file. If requested, the stubs and docs are created twice
2096// once for public API level and once for system API level
Paul Duffinf0229202020-04-29 16:47:28 +01002097func (module *SdkLibrary) CreateInternalModules(mctx android.DefaultableHookContext) {
2098 // If the module has been disabled then don't create any child modules.
2099 if !module.Enabled() {
2100 return
2101 }
2102
Paul Duffina18abc22020-05-16 18:54:24 +01002103 if len(module.properties.Srcs) == 0 {
Inseob Kimc0907f12019-02-08 21:00:45 +09002104 mctx.PropertyErrorf("srcs", "java_sdk_library must specify srcs")
Jooyung Han58f26ab2019-12-18 15:34:32 +09002105 return
Inseob Kimc0907f12019-02-08 21:00:45 +09002106 }
2107
Paul Duffin37e0b772019-12-30 17:20:10 +00002108 // If this builds against standard libraries (i.e. is not part of the core libraries)
Paul Duffin4f5c1ef2020-11-19 14:53:43 +00002109 // then assume it provides both system and test apis.
Jiyong Parkf1691d22021-03-29 20:11:58 +09002110 sdkDep := decodeSdkDep(mctx, android.SdkContext(&module.Library))
Paul Duffin37e0b772019-12-30 17:20:10 +00002111 hasSystemAndTestApis := sdkDep.hasStandardLibs()
Paul Duffin3375e352020-04-28 10:44:03 +01002112 module.sdkLibraryProperties.Generate_system_and_test_apis = hasSystemAndTestApis
Paul Duffin4f5c1ef2020-11-19 14:53:43 +00002113
Jaewoong Jung18aefc12020-12-21 09:11:10 -08002114 missingCurrentApi := false
Inseob Kim8098faa2019-03-18 10:19:51 +09002115
Paul Duffin3375e352020-04-28 10:44:03 +01002116 generatedScopes := module.getGeneratedApiScopes(mctx)
Paul Duffind1b3a922020-01-22 11:57:20 +00002117
Paul Duffin749f98f2019-12-30 17:23:46 +00002118 apiDir := module.getApiDir()
Paul Duffin3375e352020-04-28 10:44:03 +01002119 for _, scope := range generatedScopes {
Inseob Kim8098faa2019-03-18 10:19:51 +09002120 for _, api := range []string{"current.txt", "removed.txt"} {
Paul Duffind1b3a922020-01-22 11:57:20 +00002121 path := path.Join(mctx.ModuleDir(), apiDir, scope.apiFilePrefix+api)
Inseob Kim8098faa2019-03-18 10:19:51 +09002122 p := android.ExistentPathForSource(mctx, path)
2123 if !p.Valid() {
Colin Cross18f840c2021-05-20 17:56:54 -07002124 if mctx.Config().AllowMissingDependencies() {
2125 mctx.AddMissingDependencies([]string{path})
2126 } else {
2127 mctx.ModuleErrorf("Current api file %#v doesn't exist", path)
2128 missingCurrentApi = true
2129 }
Inseob Kim8098faa2019-03-18 10:19:51 +09002130 }
2131 }
2132 }
2133
Jaewoong Jung18aefc12020-12-21 09:11:10 -08002134 if missingCurrentApi {
Inseob Kim8098faa2019-03-18 10:19:51 +09002135 script := "build/soong/scripts/gen-java-current-api-files.sh"
2136 p := android.ExistentPathForSource(mctx, script)
2137
2138 if !p.Valid() {
2139 panic(fmt.Sprintf("script file %s doesn't exist", script))
2140 }
2141
2142 mctx.ModuleErrorf("One or more current api files are missing. "+
2143 "You can update them by:\n"+
Paul Duffin37e0b772019-12-30 17:20:10 +00002144 "%s %q %s && m update-api",
Paul Duffind1b3a922020-01-22 11:57:20 +00002145 script, filepath.Join(mctx.ModuleDir(), apiDir),
Paul Duffin3375e352020-04-28 10:44:03 +01002146 strings.Join(generatedScopes.Strings(func(s *apiScope) string { return s.apiFilePrefix }), " "))
Inseob Kim8098faa2019-03-18 10:19:51 +09002147 return
2148 }
2149
Paul Duffin3375e352020-04-28 10:44:03 +01002150 for _, scope := range generatedScopes {
Paul Duffin15f34ef2020-07-20 18:04:44 +01002151 // Use the stubs source name for legacy reasons.
2152 module.createStubsSourcesAndApi(mctx, scope, module.stubsSourceModuleName(scope), scope.droidstubsArgs)
Paul Duffin0ff08bd2020-04-29 13:30:54 +01002153
Paul Duffind1b3a922020-01-22 11:57:20 +00002154 module.createStubsLibrary(mctx, scope)
Jihoon Kang1c92c3e2023-03-23 17:44:51 +00002155
Jihoon Kang0c705a42023-08-02 06:44:57 +00002156 alternativeFullApiSurfaceStubLib := ""
2157 if scope == apiScopePublic {
2158 alternativeFullApiSurfaceStubLib = module.alternativeFullApiSurfaceStubLib()
2159 }
2160 contributesToApiSurface := module.contributesToApiSurface(mctx.Config()) || alternativeFullApiSurfaceStubLib != ""
Jihoon Kang1147b312023-06-08 23:25:57 +00002161 if contributesToApiSurface {
Jihoon Kang0c705a42023-08-02 06:44:57 +00002162 module.createApiLibrary(mctx, scope, alternativeFullApiSurfaceStubLib)
Jihoon Kang1c92c3e2023-03-23 17:44:51 +00002163 }
Jihoon Kang1147b312023-06-08 23:25:57 +00002164
2165 module.createTopLevelStubsLibrary(mctx, scope, contributesToApiSurface)
Inseob Kimc0907f12019-02-08 21:00:45 +09002166 }
2167
Paul Duffindfa131e2020-05-15 20:37:11 +01002168 if module.requiresRuntimeImplementationLibrary() {
Paul Duffin5df79302020-05-16 15:52:12 +01002169 // Create child module to create an implementation library.
2170 //
2171 // This temporarily creates a second implementation library that can be explicitly
2172 // referenced.
2173 //
2174 // TODO(b/156618935) - update comment once only one implementation library is created.
2175 module.createImplLibrary(mctx)
2176
Paul Duffindfa131e2020-05-15 20:37:11 +01002177 // Only create an XML permissions file that declares the library as being usable
2178 // as a shared library if required.
2179 if module.sharedLibrary() {
2180 module.createXmlFile(mctx)
2181 }
Paul Duffin43db9be2019-12-30 17:35:49 +00002182
2183 // record java_sdk_library modules so that they are exported to make
2184 javaSdkLibraries := javaSdkLibraries(mctx.Config())
2185 javaSdkLibrariesLock.Lock()
2186 defer javaSdkLibrariesLock.Unlock()
2187 *javaSdkLibraries = append(*javaSdkLibraries, module.BaseModuleName())
2188 }
Anton Hansson7f66efa2020-10-08 14:47:23 +01002189
Paul Duffin77590a82022-04-28 14:13:30 +00002190 // 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 +01002191 module.properties.Libs = append(module.properties.Libs, module.sdkLibraryProperties.Impl_only_libs...)
Paul Duffin77590a82022-04-28 14:13:30 +00002192 module.properties.Static_libs = append(module.properties.Static_libs, module.sdkLibraryProperties.Impl_only_static_libs...)
Inseob Kimc0907f12019-02-08 21:00:45 +09002193}
2194
2195func (module *SdkLibrary) InitSdkLibraryProperties() {
Colin Crossce6734e2020-06-15 16:09:53 -07002196 module.addHostAndDeviceProperties()
2197 module.AddProperties(&module.sdkLibraryProperties)
Sundong Ahn054b19a2018-10-19 13:46:09 +09002198
Paul Duffin71b33cc2021-06-23 11:39:47 +01002199 module.initSdkLibraryComponent(module)
Paul Duffin859fe962020-05-15 10:20:31 +01002200
Paul Duffina18abc22020-05-16 18:54:24 +01002201 module.properties.Installable = proptools.BoolPtr(true)
2202 module.deviceProperties.IsSDKLibrary = true
Inseob Kimc0907f12019-02-08 21:00:45 +09002203}
Sundong Ahn054b19a2018-10-19 13:46:09 +09002204
Paul Duffindfa131e2020-05-15 20:37:11 +01002205func (module *SdkLibrary) requiresRuntimeImplementationLibrary() bool {
2206 return !proptools.Bool(module.sdkLibraryProperties.Api_only)
2207}
2208
Jiyong Park932cdfe2020-05-28 00:19:53 +09002209func (module *SdkLibrary) defaultsToStubs() bool {
2210 return proptools.Bool(module.sdkLibraryProperties.Default_to_stubs)
2211}
2212
Paul Duffin1b1e8062020-05-08 13:44:43 +01002213// Defines how to name the individual component modules the sdk library creates.
2214type sdkLibraryComponentNamingScheme interface {
2215 stubsLibraryModuleName(scope *apiScope, baseName string) string
2216
2217 stubsSourceModuleName(scope *apiScope, baseName string) string
Jihoon Kang1c92c3e2023-03-23 17:44:51 +00002218
2219 apiLibraryModuleName(scope *apiScope, baseName string) string
Jihoon Kang1147b312023-06-08 23:25:57 +00002220
2221 sourceStubLibraryModuleName(scope *apiScope, baseName string) string
Paul Duffin1b1e8062020-05-08 13:44:43 +01002222}
2223
2224type defaultNamingScheme struct {
2225}
2226
2227func (s *defaultNamingScheme) stubsLibraryModuleName(scope *apiScope, baseName string) string {
2228 return scope.stubsLibraryModuleName(baseName)
2229}
2230
2231func (s *defaultNamingScheme) stubsSourceModuleName(scope *apiScope, baseName string) string {
2232 return scope.stubsSourceModuleName(baseName)
2233}
2234
Jihoon Kang1c92c3e2023-03-23 17:44:51 +00002235func (s *defaultNamingScheme) apiLibraryModuleName(scope *apiScope, baseName string) string {
2236 return scope.apiLibraryModuleName(baseName)
2237}
2238
Jihoon Kang1147b312023-06-08 23:25:57 +00002239func (s *defaultNamingScheme) sourceStubLibraryModuleName(scope *apiScope, baseName string) string {
2240 return scope.sourceStubLibraryModuleName(baseName)
2241}
2242
Paul Duffin1b1e8062020-05-08 13:44:43 +01002243var _ sdkLibraryComponentNamingScheme = (*defaultNamingScheme)(nil)
2244
Jaewoong Jungbc15e3a2021-03-10 17:02:43 -08002245func moduleStubLinkType(name string) (stub bool, ret sdkLinkType) {
Jihoon Kang1147b312023-06-08 23:25:57 +00002246 name = strings.TrimSuffix(name, ".from-source")
2247
Anton Hansson2d0c1942020-05-25 12:20:51 +01002248 // This suffix-based approach is fragile and could potentially mis-trigger.
2249 // TODO(b/155164730): Clean this up when modules no longer reference sdk_lib stubs directly.
Anton Hansson08f476b2021-04-07 15:32:19 +01002250 if strings.HasSuffix(name, apiScopePublic.stubsLibraryModuleNameSuffix()) {
2251 if name == "hwbinder.stubs" || name == "libcore_private.stubs" {
2252 // Due to a previous bug, these modules were not considered stubs, so we retain that.
2253 return false, javaPlatform
2254 }
Anton Hansson2d0c1942020-05-25 12:20:51 +01002255 return true, javaSdk
2256 }
Anton Hansson08f476b2021-04-07 15:32:19 +01002257 if strings.HasSuffix(name, apiScopeSystem.stubsLibraryModuleNameSuffix()) {
Anton Hansson2d0c1942020-05-25 12:20:51 +01002258 return true, javaSystem
2259 }
Anton Hansson08f476b2021-04-07 15:32:19 +01002260 if strings.HasSuffix(name, apiScopeModuleLib.stubsLibraryModuleNameSuffix()) {
Anton Hansson2d0c1942020-05-25 12:20:51 +01002261 return true, javaModule
2262 }
Anton Hansson08f476b2021-04-07 15:32:19 +01002263 if strings.HasSuffix(name, apiScopeTest.stubsLibraryModuleNameSuffix()) {
Anton Hansson2d0c1942020-05-25 12:20:51 +01002264 return true, javaSystem
2265 }
Jihoon Kang1147b312023-06-08 23:25:57 +00002266 if strings.HasSuffix(name, apiScopeSystemServer.stubsLibraryModuleNameSuffix()) {
2267 return true, javaSystemServer
2268 }
Anton Hansson2d0c1942020-05-25 12:20:51 +01002269 return false, javaPlatform
2270}
2271
Jaewoong Jung4f158ee2019-07-11 10:05:35 -07002272// java_sdk_library is a special Java library that provides optional platform APIs to apps.
2273// In practice, it can be viewed as a combination of several modules: 1) stubs library that clients
2274// are linked against to, 2) droiddoc module that internally generates API stubs source files,
2275// 3) the real runtime shared library that implements the APIs, and 4) XML file for adding
2276// the runtime lib to the classpath at runtime if requested via <uses-library>.
Inseob Kimc0907f12019-02-08 21:00:45 +09002277func SdkLibraryFactory() android.Module {
2278 module := &SdkLibrary{}
Paul Duffinc3091c82020-05-08 14:16:20 +01002279
2280 // Initialize information common between source and prebuilt.
Paul Duffin71b33cc2021-06-23 11:39:47 +01002281 module.initCommon(module)
Paul Duffinc3091c82020-05-08 14:16:20 +01002282
Inseob Kimc0907f12019-02-08 21:00:45 +09002283 module.InitSdkLibraryProperties()
Jooyung Han58f26ab2019-12-18 15:34:32 +09002284 android.InitApexModule(module)
Sundong Ahn054b19a2018-10-19 13:46:09 +09002285 InitJavaModule(module, android.HostAndDeviceSupported)
Paul Duffin3375e352020-04-28 10:44:03 +01002286
2287 // Initialize the map from scope to scope specific properties.
2288 scopeToProperties := make(map[*apiScope]*ApiScopeProperties)
2289 for _, scope := range allApiScopes {
2290 scopeToProperties[scope] = scope.scopeSpecificProperties(module)
2291 }
2292 module.scopeToProperties = scopeToProperties
2293
Paul Duffin4911a892020-04-29 23:35:13 +01002294 // Add the properties containing visibility rules so that they are checked.
Paul Duffin5df79302020-05-16 15:52:12 +01002295 android.AddVisibilityProperty(module, "impl_library_visibility", &module.sdkLibraryProperties.Impl_library_visibility)
Paul Duffin4911a892020-04-29 23:35:13 +01002296 android.AddVisibilityProperty(module, "stubs_library_visibility", &module.sdkLibraryProperties.Stubs_library_visibility)
2297 android.AddVisibilityProperty(module, "stubs_source_visibility", &module.sdkLibraryProperties.Stubs_source_visibility)
2298
Paul Duffin1b1e8062020-05-08 13:44:43 +01002299 module.SetDefaultableHook(func(ctx android.DefaultableHookContext) {
Paul Duffindfa131e2020-05-15 20:37:11 +01002300 // If no implementation is required then it cannot be used as a shared library
2301 // either.
2302 if !module.requiresRuntimeImplementationLibrary() {
2303 // If shared_library has been explicitly set to true then it is incompatible
2304 // with api_only: true.
2305 if proptools.Bool(module.commonSdkLibraryProperties.Shared_library) {
2306 ctx.PropertyErrorf("api_only/shared_library", "inconsistent settings, shared_library and api_only cannot both be true")
2307 }
2308 // Set shared_library: false.
2309 module.commonSdkLibraryProperties.Shared_library = proptools.BoolPtr(false)
2310 }
2311
Paul Duffin1b1e8062020-05-08 13:44:43 +01002312 if module.initCommonAfterDefaultsApplied(ctx) {
2313 module.CreateInternalModules(ctx)
2314 }
2315 })
Jiyong Parkc678ad32018-04-10 13:07:10 +09002316 return module
2317}
Colin Cross79c7c262019-04-17 11:11:46 -07002318
2319//
2320// SDK library prebuilts
2321//
2322
Paul Duffin56d44902020-01-31 13:36:25 +00002323// Properties associated with each api scope.
2324type sdkLibraryScopeProperties struct {
Colin Cross79c7c262019-04-17 11:11:46 -07002325 Jars []string `android:"path"`
2326
2327 Sdk_version *string
2328
Colin Cross79c7c262019-04-17 11:11:46 -07002329 // List of shared java libs that this module has dependencies to
2330 Libs []string
Paul Duffin3d1248c2020-04-09 00:10:17 +01002331
Paul Duffinc8782502020-04-29 20:45:27 +01002332 // The stubs source.
Paul Duffin3d1248c2020-04-09 00:10:17 +01002333 Stub_srcs []string `android:"path"`
Paul Duffin1fd005d2020-04-09 01:08:11 +01002334
2335 // The current.txt
Paul Duffin0f8faff2020-05-20 16:18:00 +01002336 Current_api *string `android:"path"`
Paul Duffin1fd005d2020-04-09 01:08:11 +01002337
2338 // The removed.txt
Paul Duffin0f8faff2020-05-20 16:18:00 +01002339 Removed_api *string `android:"path"`
Anton Hanssond78eb762021-09-21 15:25:12 +01002340
2341 // Annotation zip
2342 Annotations *string `android:"path"`
Colin Cross79c7c262019-04-17 11:11:46 -07002343}
2344
Paul Duffin56d44902020-01-31 13:36:25 +00002345type sdkLibraryImportProperties struct {
Paul Duffinfcfd7912020-01-31 17:54:30 +00002346 // List of shared java libs, common to all scopes, that this module has
2347 // dependencies to
2348 Libs []string
Paul Duffin1267d872021-04-16 17:21:36 +01002349
2350 // If set to true, compile dex files for the stubs. Defaults to false.
2351 Compile_dex *bool
Paul Duffin869de142021-07-15 14:14:41 +01002352
2353 // If not empty, classes are restricted to the specified packages and their sub-packages.
Paul Duffin869de142021-07-15 14:14:41 +01002354 Permitted_packages []string
Paul Duffin56d44902020-01-31 13:36:25 +00002355}
2356
Paul Duffineedc5d52020-06-12 17:46:39 +01002357type SdkLibraryImport struct {
Colin Cross79c7c262019-04-17 11:11:46 -07002358 android.ModuleBase
2359 android.DefaultableModuleBase
2360 prebuilt android.Prebuilt
Paul Duffindd46f712020-02-10 13:37:10 +00002361 android.ApexModuleBase
Colin Cross79c7c262019-04-17 11:11:46 -07002362
Paul Duffin37856732021-02-26 14:24:15 +00002363 hiddenAPI
Jiakai Zhang204356f2021-09-09 08:12:46 +00002364 dexpreopter
Paul Duffin37856732021-02-26 14:24:15 +00002365
Colin Cross79c7c262019-04-17 11:11:46 -07002366 properties sdkLibraryImportProperties
2367
Paul Duffin46a26a82020-04-07 19:27:04 +01002368 // Map from api scope to the scope specific property structure.
2369 scopeProperties map[*apiScope]*sdkLibraryScopeProperties
2370
Paul Duffin56d44902020-01-31 13:36:25 +00002371 commonToSdkLibraryAndImport
Paul Duffineedc5d52020-06-12 17:46:39 +01002372
2373 // The reference to the implementation library created by the source module.
2374 // Is nil if the source module does not exist.
2375 implLibraryModule *Library
2376
2377 // The reference to the xml permissions module created by the source module.
2378 // Is nil if the source module does not exist.
2379 xmlPermissionsFileModule *sdkLibraryXml
Paul Duffin39853512021-02-26 11:09:39 +00002380
Jeongik Chad5fe8782021-07-08 01:13:11 +09002381 // Build path to the dex implementation jar obtained from the prebuilt_apex, if any.
Martin Stjernholm8be1e6d2021-09-15 03:34:04 +01002382 dexJarFile OptionalDexJarPath
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
Jiakai Zhang204356f2021-09-09 08:12:46 +00002595func (module *SdkLibraryImport) AndroidMkEntries() []android.AndroidMkEntries {
2596 // For an SDK library imported from a prebuilt APEX, we don't need a Make module for itself, as we
2597 // don't need to install it. However, we need to add its dexpreopt outputs as sub-modules, if it
2598 // is preopted.
2599 dexpreoptEntries := module.dexpreopter.AndroidMkEntriesForApex()
2600 return append(dexpreoptEntries, android.AndroidMkEntries{Disabled: true})
2601}
2602
Jiyong Park45bf82e2020-12-15 22:29:02 +09002603var _ android.ApexModule = (*SdkLibraryImport)(nil)
2604
2605// Implements android.ApexModule
Paul Duffineedc5d52020-06-12 17:46:39 +01002606func (module *SdkLibraryImport) DepIsInSameApex(mctx android.BaseModuleContext, dep android.Module) bool {
2607 depTag := mctx.OtherModuleDependencyTag(dep)
2608 if depTag == xmlPermissionsFileTag {
2609 return true
2610 }
2611
2612 // None of the other dependencies of the java_sdk_library_import are in the same apex
2613 // as the one that references this module.
2614 return false
2615}
2616
Jiyong Park45bf82e2020-12-15 22:29:02 +09002617// Implements android.ApexModule
Dan Albertc8060532020-07-22 22:32:17 -07002618func (module *SdkLibraryImport) ShouldSupportSdkVersion(ctx android.BaseModuleContext,
2619 sdkVersion android.ApiLevel) error {
Jooyung Han749dc692020-04-15 11:03:39 +09002620 // we don't check prebuilt modules for sdk_version
2621 return nil
2622}
2623
Paul Duffinea8f8082021-06-24 13:25:57 +01002624// Implements android.ApexModule
2625func (module *SdkLibraryImport) UniqueApexVariations() bool {
2626 return module.uniqueApexVariations()
2627}
2628
Paul Duffin09817d62022-04-28 17:45:11 +01002629// MinSdkVersion - Implements hiddenAPIModule
Spandan Das8c9ae7e2023-03-03 21:20:36 +00002630func (module *SdkLibraryImport) MinSdkVersion(ctx android.EarlyModuleContext) android.ApiLevel {
2631 return android.NoneApiLevel
Paul Duffin09817d62022-04-28 17:45:11 +01002632}
2633
2634var _ hiddenAPIModule = (*SdkLibraryImport)(nil)
2635
Paul Duffineedc5d52020-06-12 17:46:39 +01002636func (module *SdkLibraryImport) OutputFiles(tag string) (android.Paths, error) {
Paul Duffin1e940d52022-04-29 14:21:25 +01002637 paths, err := module.commonOutputFiles(tag)
2638 if paths != nil || err != nil {
2639 return paths, err
2640 }
2641 if module.implLibraryModule != nil {
2642 return module.implLibraryModule.OutputFiles(tag)
2643 } else {
2644 return nil, nil
2645 }
Paul Duffin46dc45a2020-05-14 15:39:10 +01002646}
2647
Paul Duffineedc5d52020-06-12 17:46:39 +01002648func (module *SdkLibraryImport) GenerateAndroidBuildActions(ctx android.ModuleContext) {
Paul Duffina2ae7e02020-09-11 11:55:00 +01002649 module.generateCommonBuildActions(ctx)
2650
Jeongik Chad5fe8782021-07-08 01:13:11 +09002651 // Assume that source module(sdk_library) is installed in /<sdk_library partition>/framework
2652 module.installFile = android.PathForModuleInstall(ctx, "framework", module.Stem()+".jar")
2653
Paul Duffin0f8faff2020-05-20 16:18:00 +01002654 // Record the paths to the prebuilt stubs library and stubs source.
Colin Cross79c7c262019-04-17 11:11:46 -07002655 ctx.VisitDirectDeps(func(to android.Module) {
2656 tag := ctx.OtherModuleDependencyTag(to)
2657
Paul Duffin0f8faff2020-05-20 16:18:00 +01002658 // Extract information from any of the scope specific dependencies.
2659 if scopeTag, ok := tag.(scopeDependencyTag); ok {
2660 apiScope := scopeTag.apiScope
2661 scopePaths := module.getScopePathsCreateIfNeeded(apiScope)
2662
2663 // Extract information from the dependency. The exact information extracted
2664 // is determined by the nature of the dependency which is determined by the tag.
2665 scopeTag.extractDepInfo(ctx, to, scopePaths)
Paul Duffineedc5d52020-06-12 17:46:39 +01002666 } else if tag == implLibraryTag {
2667 if implLibrary, ok := to.(*Library); ok {
2668 module.implLibraryModule = implLibrary
2669 } else {
2670 ctx.ModuleErrorf("implementation library must be of type *java.Library but was %T", to)
2671 }
2672 } else if tag == xmlPermissionsFileTag {
2673 if xmlPermissionsFileModule, ok := to.(*sdkLibraryXml); ok {
2674 module.xmlPermissionsFileModule = xmlPermissionsFileModule
2675 } else {
2676 ctx.ModuleErrorf("xml permissions file module must be of type *sdkLibraryXml but was %T", to)
2677 }
Colin Cross79c7c262019-04-17 11:11:46 -07002678 }
2679 })
Paul Duffin0f8faff2020-05-20 16:18:00 +01002680
2681 // Populate the scope paths with information from the properties.
2682 for apiScope, scopeProperties := range module.scopeProperties {
2683 if len(scopeProperties.Jars) == 0 {
2684 continue
2685 }
2686
2687 paths := module.getScopePathsCreateIfNeeded(apiScope)
Anton Hanssond78eb762021-09-21 15:25:12 +01002688 paths.annotationsZip = android.OptionalPathForModuleSrc(ctx, scopeProperties.Annotations)
Paul Duffin0f8faff2020-05-20 16:18:00 +01002689 paths.currentApiFilePath = android.OptionalPathForModuleSrc(ctx, scopeProperties.Current_api)
2690 paths.removedApiFilePath = android.OptionalPathForModuleSrc(ctx, scopeProperties.Removed_api)
2691 }
Paul Duffin39853512021-02-26 11:09:39 +00002692
2693 if ctx.Device() {
2694 // If this is a variant created for a prebuilt_apex then use the dex implementation jar
2695 // obtained from the associated deapexer module.
2696 ai := ctx.Provider(android.ApexInfoProvider).(android.ApexInfo)
2697 if ai.ForPrebuiltApex {
Paul Duffin39853512021-02-26 11:09:39 +00002698 // Get the path of the dex implementation jar from the `deapexer` module.
Martin Stjernholm44825602021-09-17 01:44:12 +01002699 di := android.FindDeapexerProviderForModule(ctx)
2700 if di == nil {
2701 return // An error has been reported by FindDeapexerProviderForModule.
2702 }
Jiakai Zhang81e46812023-02-08 21:56:07 +08002703 dexJarFileApexRootRelative := apexRootRelativePathToJavaLib(module.BaseModuleName())
2704 if dexOutputPath := di.PrebuiltExportPath(dexJarFileApexRootRelative); dexOutputPath != nil {
Martin Stjernholm8be1e6d2021-09-15 03:34:04 +01002705 dexJarFile := makeDexJarPathFromPath(dexOutputPath)
2706 module.dexJarFile = dexJarFile
Jiakai Zhang204356f2021-09-09 08:12:46 +00002707 installPath := android.PathForModuleInPartitionInstall(
Jiakai Zhang81e46812023-02-08 21:56:07 +08002708 ctx, "apex", ai.ApexVariationName, dexJarFileApexRootRelative)
Jiakai Zhang204356f2021-09-09 08:12:46 +00002709 module.installFile = installPath
Martin Stjernholm8be1e6d2021-09-15 03:34:04 +01002710 module.initHiddenAPI(ctx, dexJarFile, module.findScopePaths(apiScopePublic).stubsImplPath[0], nil)
Jiakai Zhang204356f2021-09-09 08:12:46 +00002711
Jiakai Zhang204356f2021-09-09 08:12:46 +00002712 module.dexpreopter.installPath = module.dexpreopter.getInstallPath(ctx, installPath)
2713 module.dexpreopter.isSDKLibrary = true
2714 module.dexpreopter.uncompressedDex = shouldUncompressDex(ctx, &module.dexpreopter)
Jiakai Zhang81e46812023-02-08 21:56:07 +08002715
2716 if profilePath := di.PrebuiltExportPath(dexJarFileApexRootRelative + ".prof"); profilePath != nil {
2717 module.dexpreopter.inputProfilePathOnHost = profilePath
2718 }
2719
2720 // Dexpreopting.
Jiakai Zhang204356f2021-09-09 08:12:46 +00002721 module.dexpreopt(ctx, dexOutputPath)
Paul Duffin39853512021-02-26 11:09:39 +00002722 } else {
2723 // This should never happen as a variant for a prebuilt_apex is only created if the
2724 // prebuilt_apex has been configured to export the java library dex file.
Martin Stjernholm44825602021-09-17 01:44:12 +01002725 ctx.ModuleErrorf("internal error: no dex implementation jar available from prebuilt APEX %s", di.ApexModuleName())
Paul Duffin39853512021-02-26 11:09:39 +00002726 }
2727 }
2728 }
Colin Cross79c7c262019-04-17 11:11:46 -07002729}
2730
Jiyong Parkf1691d22021-03-29 20:11:58 +09002731func (module *SdkLibraryImport) sdkJars(ctx android.BaseModuleContext, sdkVersion android.SdkSpec, headerJars bool) android.Paths {
Paul Duffineedc5d52020-06-12 17:46:39 +01002732
2733 // For consistency with SdkLibrary make the implementation jar available to libraries that
2734 // are within the same APEX.
2735 implLibraryModule := module.implLibraryModule
Colin Cross56a83212020-09-15 18:30:11 -07002736 if implLibraryModule != nil && withinSameApexesAs(ctx, module) {
Paul Duffineedc5d52020-06-12 17:46:39 +01002737 if headerJars {
2738 return implLibraryModule.HeaderJars()
2739 } else {
2740 return implLibraryModule.ImplementationJars()
2741 }
2742 }
2743
Paul Duffin23970f42020-05-20 14:20:02 +01002744 return module.selectHeaderJarsForSdkVersion(ctx, sdkVersion)
Paul Duffin56d44902020-01-31 13:36:25 +00002745}
2746
Colin Cross79c7c262019-04-17 11:11:46 -07002747// to satisfy SdkLibraryDependency interface
Jiyong Parkf1691d22021-03-29 20:11:58 +09002748func (module *SdkLibraryImport) SdkHeaderJars(ctx android.BaseModuleContext, sdkVersion android.SdkSpec) android.Paths {
Colin Cross79c7c262019-04-17 11:11:46 -07002749 // This module is just a wrapper for the prebuilt stubs.
Paul Duffineedc5d52020-06-12 17:46:39 +01002750 return module.sdkJars(ctx, sdkVersion, true)
Colin Cross79c7c262019-04-17 11:11:46 -07002751}
2752
2753// to satisfy SdkLibraryDependency interface
Jiyong Parkf1691d22021-03-29 20:11:58 +09002754func (module *SdkLibraryImport) SdkImplementationJars(ctx android.BaseModuleContext, sdkVersion android.SdkSpec) android.Paths {
Colin Cross79c7c262019-04-17 11:11:46 -07002755 // This module is just a wrapper for the stubs.
Paul Duffineedc5d52020-06-12 17:46:39 +01002756 return module.sdkJars(ctx, sdkVersion, false)
2757}
2758
Ulya Trafimovichdbf31662020-12-17 12:07:54 +00002759// to satisfy UsesLibraryDependency interface
Martin Stjernholm8be1e6d2021-09-15 03:34:04 +01002760func (module *SdkLibraryImport) DexJarBuildPath() OptionalDexJarPath {
Paul Duffin39853512021-02-26 11:09:39 +00002761 // The dex implementation jar extracted from the .apex file should be used in preference to the
2762 // source.
Martin Stjernholm8be1e6d2021-09-15 03:34:04 +01002763 if module.dexJarFile.IsSet() {
Paul Duffin39853512021-02-26 11:09:39 +00002764 return module.dexJarFile
2765 }
Paul Duffineedc5d52020-06-12 17:46:39 +01002766 if module.implLibraryModule == nil {
Martin Stjernholm8be1e6d2021-09-15 03:34:04 +01002767 return makeUnsetDexJarPath()
Paul Duffineedc5d52020-06-12 17:46:39 +01002768 } else {
2769 return module.implLibraryModule.DexJarBuildPath()
2770 }
2771}
2772
Ulya Trafimovichdbf31662020-12-17 12:07:54 +00002773// to satisfy UsesLibraryDependency interface
Ulya Trafimovich31e444e2020-08-14 17:32:16 +01002774func (module *SdkLibraryImport) DexJarInstallPath() android.Path {
Jeongik Chad5fe8782021-07-08 01:13:11 +09002775 return module.installFile
Ulya Trafimovich31e444e2020-08-14 17:32:16 +01002776}
2777
Ulya Trafimovichdbf31662020-12-17 12:07:54 +00002778// to satisfy UsesLibraryDependency interface
2779func (module *SdkLibraryImport) ClassLoaderContexts() dexpreopt.ClassLoaderContextMap {
2780 return nil
2781}
2782
Paul Duffineedc5d52020-06-12 17:46:39 +01002783// to satisfy apex.javaDependency interface
2784func (module *SdkLibraryImport) JacocoReportClassesFile() android.Path {
2785 if module.implLibraryModule == nil {
2786 return nil
2787 } else {
2788 return module.implLibraryModule.JacocoReportClassesFile()
2789 }
2790}
2791
2792// to satisfy apex.javaDependency interface
Colin Cross08dca382020-07-21 20:31:17 -07002793func (module *SdkLibraryImport) LintDepSets() LintDepSets {
2794 if module.implLibraryModule == nil {
2795 return LintDepSets{}
2796 } else {
2797 return module.implLibraryModule.LintDepSets()
2798 }
2799}
2800
Spandan Das17854f52022-01-14 21:19:14 +00002801func (module *SdkLibraryImport) GetStrictUpdatabilityLinting() bool {
Jaewoong Jung476b9d62021-05-10 15:30:00 -07002802 if module.implLibraryModule == nil {
2803 return false
2804 } else {
Spandan Das17854f52022-01-14 21:19:14 +00002805 return module.implLibraryModule.GetStrictUpdatabilityLinting()
Jaewoong Jung476b9d62021-05-10 15:30:00 -07002806 }
2807}
2808
Spandan Das17854f52022-01-14 21:19:14 +00002809func (module *SdkLibraryImport) SetStrictUpdatabilityLinting(strictLinting bool) {
Jaewoong Jung476b9d62021-05-10 15:30:00 -07002810 if module.implLibraryModule != nil {
Spandan Das17854f52022-01-14 21:19:14 +00002811 module.implLibraryModule.SetStrictUpdatabilityLinting(strictLinting)
Jaewoong Jung476b9d62021-05-10 15:30:00 -07002812 }
2813}
2814
Colin Cross08dca382020-07-21 20:31:17 -07002815// to satisfy apex.javaDependency interface
Paul Duffineedc5d52020-06-12 17:46:39 +01002816func (module *SdkLibraryImport) Stem() string {
2817 return module.BaseModuleName()
Colin Cross79c7c262019-04-17 11:11:46 -07002818}
Jiyong Parke3833882020-02-17 17:28:10 +09002819
Paul Duffin44b481b2020-06-17 16:59:43 +01002820var _ ApexDependency = (*SdkLibraryImport)(nil)
2821
2822// to satisfy java.ApexDependency interface
2823func (module *SdkLibraryImport) HeaderJars() android.Paths {
2824 if module.implLibraryModule == nil {
2825 return nil
2826 } else {
2827 return module.implLibraryModule.HeaderJars()
2828 }
2829}
2830
2831// to satisfy java.ApexDependency interface
2832func (module *SdkLibraryImport) ImplementationAndResourcesJars() android.Paths {
2833 if module.implLibraryModule == nil {
2834 return nil
2835 } else {
2836 return module.implLibraryModule.ImplementationAndResourcesJars()
2837 }
2838}
2839
Jiakai Zhang204356f2021-09-09 08:12:46 +00002840// to satisfy java.DexpreopterInterface interface
2841func (module *SdkLibraryImport) IsInstallable() bool {
2842 return true
2843}
2844
Paul Duffinfef55002021-06-17 14:56:05 +01002845var _ android.RequiredFilesFromPrebuiltApex = (*SdkLibraryImport)(nil)
2846
Paul Duffinb4bbf2c2021-06-17 15:59:07 +01002847func (module *SdkLibraryImport) RequiredFilesFromPrebuiltApex(ctx android.BaseModuleContext) []string {
Paul Duffinfef55002021-06-17 14:56:05 +01002848 name := module.BaseModuleName()
Jiakai Zhang81e46812023-02-08 21:56:07 +08002849 return requiredFilesFromPrebuiltApexForImport(name, &module.dexpreopter)
Paul Duffinfef55002021-06-17 14:56:05 +01002850}
2851
Jiyong Parke3833882020-02-17 17:28:10 +09002852// java_sdk_library_xml
Jiyong Parke3833882020-02-17 17:28:10 +09002853type sdkLibraryXml struct {
2854 android.ModuleBase
2855 android.DefaultableModuleBase
2856 android.ApexModuleBase
2857
2858 properties sdkLibraryXmlProperties
2859
2860 outputFilePath android.OutputPath
2861 installDirPath android.InstallPath
Colin Cross56a83212020-09-15 18:30:11 -07002862
2863 hideApexVariantFromMake bool
Jiyong Parke3833882020-02-17 17:28:10 +09002864}
2865
2866type sdkLibraryXmlProperties struct {
2867 // canonical name of the lib
2868 Lib_name *string
Pedro Loureiro9956e5e2021-09-07 17:21:59 +00002869
2870 // Signals that this shared library is part of the bootclasspath starting
2871 // on the version indicated in this attribute.
2872 //
2873 // This will make platforms at this level and above to ignore
2874 // <uses-library> tags with this library name because the library is already
2875 // available
2876 On_bootclasspath_since *string
2877
2878 // Signals that this shared library was part of the bootclasspath before
2879 // (but not including) the version indicated in this attribute.
2880 //
2881 // The system will automatically add a <uses-library> tag with this library to
2882 // apps that target any SDK less than the version indicated in this attribute.
2883 On_bootclasspath_before *string
2884
2885 // Indicates that PackageManager should ignore this shared library if the
2886 // platform is below the version indicated in this attribute.
2887 //
2888 // This means that the device won't recognise this library as installed.
2889 Min_device_sdk *string
2890
2891 // Indicates that PackageManager should ignore this shared library if the
2892 // platform is above the version indicated in this attribute.
2893 //
2894 // This means that the device won't recognise this library as installed.
2895 Max_device_sdk *string
Pedro Loureiroc3621422021-09-28 15:40:23 +00002896
2897 // The SdkLibrary's min api level as a string
2898 //
2899 // This value comes from the ApiLevel of the MinSdkVersion property.
2900 Sdk_library_min_api_level *string
Jamie Garsidee570ace2023-11-27 12:07:36 +00002901
2902 // Uses-libs dependencies that the shared library requires to work correctly.
2903 //
2904 // This will add dependency="foo:bar" to the <library> section.
2905 Uses_libs_dependencies []string
Jiyong Parke3833882020-02-17 17:28:10 +09002906}
2907
2908// java_sdk_library_xml builds the permission xml file for a java_sdk_library.
2909// Not to be used directly by users. java_sdk_library internally uses this.
2910func sdkLibraryXmlFactory() android.Module {
2911 module := &sdkLibraryXml{}
2912
2913 module.AddProperties(&module.properties)
2914
2915 android.InitApexModule(module)
2916 android.InitAndroidArchModule(module, android.DeviceSupported, android.MultilibCommon)
2917
2918 return module
2919}
2920
Colin Crossaede88c2020-08-11 12:17:01 -07002921func (module *sdkLibraryXml) UniqueApexVariations() bool {
2922 // sdkLibraryXml needs a unique variation per APEX because the generated XML file contains the path to the
2923 // mounted APEX, which contains the name of the APEX.
2924 return true
2925}
2926
Jiyong Parke3833882020-02-17 17:28:10 +09002927// from android.PrebuiltEtcModule
Jooyung Han0703fd82020-08-26 22:11:53 +09002928func (module *sdkLibraryXml) BaseDir() string {
2929 return "etc"
2930}
2931
2932// from android.PrebuiltEtcModule
Jiyong Parke3833882020-02-17 17:28:10 +09002933func (module *sdkLibraryXml) SubDir() string {
2934 return "permissions"
2935}
2936
2937// from android.PrebuiltEtcModule
2938func (module *sdkLibraryXml) OutputFile() android.OutputPath {
2939 return module.outputFilePath
2940}
2941
2942// from android.ApexModule
2943func (module *sdkLibraryXml) AvailableFor(what string) bool {
2944 return true
2945}
2946
2947func (module *sdkLibraryXml) DepsMutator(ctx android.BottomUpMutatorContext) {
2948 // do nothing
2949}
2950
Jiyong Park45bf82e2020-12-15 22:29:02 +09002951var _ android.ApexModule = (*sdkLibraryXml)(nil)
2952
2953// Implements android.ApexModule
Dan Albertc8060532020-07-22 22:32:17 -07002954func (module *sdkLibraryXml) ShouldSupportSdkVersion(ctx android.BaseModuleContext,
2955 sdkVersion android.ApiLevel) error {
Jooyung Han749dc692020-04-15 11:03:39 +09002956 // sdkLibraryXml doesn't need to be checked separately because java_sdk_library is checked
2957 return nil
2958}
2959
Jiyong Parke3833882020-02-17 17:28:10 +09002960// File path to the runtime implementation library
Colin Cross56a83212020-09-15 18:30:11 -07002961func (module *sdkLibraryXml) implPath(ctx android.ModuleContext) string {
Jiyong Parke3833882020-02-17 17:28:10 +09002962 implName := proptools.String(module.properties.Lib_name)
Colin Cross56a83212020-09-15 18:30:11 -07002963 if apexInfo := ctx.Provider(android.ApexInfoProvider).(android.ApexInfo); !apexInfo.IsForPlatform() {
Colin Crosse07f2312020-08-13 11:24:56 -07002964 // TODO(b/146468504): ApexVariationName() is only a soong module name, not apex name.
Jiyong Parke3833882020-02-17 17:28:10 +09002965 // In most cases, this works fine. But when apex_name is set or override_apex is used
2966 // this can be wrong.
Colin Cross56a83212020-09-15 18:30:11 -07002967 return fmt.Sprintf("/apex/%s/javalib/%s.jar", apexInfo.ApexVariationName, implName)
Jiyong Parke3833882020-02-17 17:28:10 +09002968 }
2969 partition := "system"
2970 if module.SocSpecific() {
2971 partition = "vendor"
2972 } else if module.DeviceSpecific() {
2973 partition = "odm"
2974 } else if module.ProductSpecific() {
2975 partition = "product"
2976 } else if module.SystemExtSpecific() {
2977 partition = "system_ext"
2978 }
2979 return "/" + partition + "/framework/" + implName + ".jar"
2980}
2981
Pedro Loureiro9956e5e2021-09-07 17:21:59 +00002982func formattedOptionalSdkLevelAttribute(ctx android.ModuleContext, attrName string, value *string) string {
2983 if value == nil {
2984 return ""
2985 }
2986 apiLevel, err := android.ApiLevelFromUser(ctx, *value)
2987 if err != nil {
Pedro Loureiroba6682f2021-10-29 09:32:32 +00002988 // attributes in bp files have underscores but in the xml have dashes.
2989 ctx.PropertyErrorf(strings.ReplaceAll(attrName, "-", "_"), err.Error())
Pedro Loureiro9956e5e2021-09-07 17:21:59 +00002990 return ""
2991 }
Pedro Loureirob638c622021-12-22 15:28:05 +00002992 if apiLevel.IsCurrent() {
2993 // passing "current" would always mean a future release, never the current (or the current in
2994 // progress) which means some conditions would never be triggered.
2995 ctx.PropertyErrorf(strings.ReplaceAll(attrName, "-", "_"),
2996 `"current" is not an allowed value for this attribute`)
2997 return ""
2998 }
Pedro Loureiro48991222022-06-17 20:01:21 +00002999 // "safeValue" is safe because it translates finalized codenames to a string
3000 // with their SDK int.
3001 safeValue := apiLevel.String()
3002 return formattedOptionalAttribute(attrName, &safeValue)
Pedro Loureiro9956e5e2021-09-07 17:21:59 +00003003}
3004
3005// formats an attribute for the xml permissions file if the value is not null
3006// returns empty string otherwise
3007func formattedOptionalAttribute(attrName string, value *string) string {
3008 if value == nil {
3009 return ""
3010 }
3011 return fmt.Sprintf(` %s=\"%s\"\n`, attrName, *value)
3012}
3013
Jamie Garsidee570ace2023-11-27 12:07:36 +00003014func formattedDependenciesAttribute(dependencies []string) string {
3015 if dependencies == nil {
3016 return ""
3017 }
3018 return fmt.Sprintf(` dependency=\"%s\"\n`, strings.Join(dependencies, ":"))
3019}
3020
Pedro Loureiro9956e5e2021-09-07 17:21:59 +00003021func (module *sdkLibraryXml) permissionsContents(ctx android.ModuleContext) string {
3022 libName := proptools.String(module.properties.Lib_name)
3023 libNameAttr := formattedOptionalAttribute("name", &libName)
3024 filePath := module.implPath(ctx)
3025 filePathAttr := formattedOptionalAttribute("file", &filePath)
Pedro Loureiroba6682f2021-10-29 09:32:32 +00003026 implicitFromAttr := formattedOptionalSdkLevelAttribute(ctx, "on-bootclasspath-since", module.properties.On_bootclasspath_since)
3027 implicitUntilAttr := formattedOptionalSdkLevelAttribute(ctx, "on-bootclasspath-before", module.properties.On_bootclasspath_before)
3028 minSdkAttr := formattedOptionalSdkLevelAttribute(ctx, "min-device-sdk", module.properties.Min_device_sdk)
3029 maxSdkAttr := formattedOptionalSdkLevelAttribute(ctx, "max-device-sdk", module.properties.Max_device_sdk)
Jamie Garsidee570ace2023-11-27 12:07:36 +00003030 dependenciesAttr := formattedDependenciesAttribute(module.properties.Uses_libs_dependencies)
Pedro Loureiro196d3e62021-12-22 19:53:01 +00003031 // <library> is understood in all android versions whereas <apex-library> is only understood from API T (and ignored before that).
3032 // 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 +00003033 var libraryTag string
3034 if module.properties.Min_device_sdk != nil {
Pedro Loureiro196d3e62021-12-22 19:53:01 +00003035 libraryTag = ` <apex-library\n`
Pedro Loureiroc3621422021-09-28 15:40:23 +00003036 } else {
3037 libraryTag = ` <library\n`
3038 }
Pedro Loureiro9956e5e2021-09-07 17:21:59 +00003039
3040 return strings.Join([]string{
3041 `<?xml version=\"1.0\" encoding=\"utf-8\"?>\n`,
3042 `<!-- Copyright (C) 2018 The Android Open Source Project\n`,
3043 `\n`,
3044 ` Licensed under the Apache License, Version 2.0 (the \"License\");\n`,
3045 ` you may not use this file except in compliance with the License.\n`,
3046 ` You may obtain a copy of the License at\n`,
3047 `\n`,
3048 ` http://www.apache.org/licenses/LICENSE-2.0\n`,
3049 `\n`,
3050 ` Unless required by applicable law or agreed to in writing, software\n`,
3051 ` distributed under the License is distributed on an \"AS IS\" BASIS,\n`,
3052 ` WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\n`,
3053 ` See the License for the specific language governing permissions and\n`,
3054 ` limitations under the License.\n`,
3055 `-->\n`,
3056 `<permissions>\n`,
Pedro Loureiroc3621422021-09-28 15:40:23 +00003057 libraryTag,
Pedro Loureiro9956e5e2021-09-07 17:21:59 +00003058 libNameAttr,
3059 filePathAttr,
3060 implicitFromAttr,
3061 implicitUntilAttr,
3062 minSdkAttr,
3063 maxSdkAttr,
Jamie Garsidee570ace2023-11-27 12:07:36 +00003064 dependenciesAttr,
Pedro Loureiro9956e5e2021-09-07 17:21:59 +00003065 ` />\n`,
3066 `</permissions>\n`}, "")
3067}
3068
Jiyong Parke3833882020-02-17 17:28:10 +09003069func (module *sdkLibraryXml) GenerateAndroidBuildActions(ctx android.ModuleContext) {
Colin Cross56a83212020-09-15 18:30:11 -07003070 module.hideApexVariantFromMake = !ctx.Provider(android.ApexInfoProvider).(android.ApexInfo).IsForPlatform()
3071
Jiyong Parke3833882020-02-17 17:28:10 +09003072 libName := proptools.String(module.properties.Lib_name)
Pedro Loureiroc3621422021-09-28 15:40:23 +00003073 module.selfValidate(ctx)
Pedro Loureiro9956e5e2021-09-07 17:21:59 +00003074 xmlContent := module.permissionsContents(ctx)
Jiyong Parke3833882020-02-17 17:28:10 +09003075
3076 module.outputFilePath = android.PathForModuleOut(ctx, libName+".xml").OutputPath
Colin Crossf1a035e2020-11-16 17:32:30 -08003077 rule := android.NewRuleBuilder(pctx, ctx)
Jiyong Parke3833882020-02-17 17:28:10 +09003078 rule.Command().
3079 Text("/bin/bash -c \"echo -e '" + xmlContent + "'\" > ").
3080 Output(module.outputFilePath)
3081
Colin Crossf1a035e2020-11-16 17:32:30 -08003082 rule.Build("java_sdk_xml", "Permission XML")
Jiyong Parke3833882020-02-17 17:28:10 +09003083
3084 module.installDirPath = android.PathForModuleInstall(ctx, "etc", module.SubDir())
3085}
3086
3087func (module *sdkLibraryXml) AndroidMkEntries() []android.AndroidMkEntries {
Colin Cross56a83212020-09-15 18:30:11 -07003088 if module.hideApexVariantFromMake {
satayev8f088b02021-12-06 11:40:46 +00003089 return []android.AndroidMkEntries{{
Jiyong Parke3833882020-02-17 17:28:10 +09003090 Disabled: true,
3091 }}
3092 }
3093
satayev8f088b02021-12-06 11:40:46 +00003094 return []android.AndroidMkEntries{{
Jiyong Parke3833882020-02-17 17:28:10 +09003095 Class: "ETC",
3096 OutputFile: android.OptionalPathForPath(module.outputFilePath),
3097 ExtraEntries: []android.AndroidMkExtraEntriesFunc{
Colin Crossaa255532020-07-03 13:18:24 -07003098 func(ctx android.AndroidMkExtraEntriesContext, entries *android.AndroidMkEntries) {
Jiyong Parke3833882020-02-17 17:28:10 +09003099 entries.SetString("LOCAL_MODULE_TAGS", "optional")
Colin Crossc68db4b2021-11-11 18:59:15 -08003100 entries.SetString("LOCAL_MODULE_PATH", module.installDirPath.String())
Jiyong Parke3833882020-02-17 17:28:10 +09003101 entries.SetString("LOCAL_INSTALLED_MODULE_STEM", module.outputFilePath.Base())
3102 },
3103 },
3104 }}
3105}
Paul Duffindd46f712020-02-10 13:37:10 +00003106
Pedro Loureiroc3621422021-09-28 15:40:23 +00003107func (module *sdkLibraryXml) selfValidate(ctx android.ModuleContext) {
3108 module.validateAtLeastTAttributes(ctx)
3109 module.validateMinAndMaxDeviceSdk(ctx)
3110 module.validateMinMaxDeviceSdkAndModuleMinSdk(ctx)
3111 module.validateOnBootclasspathBeforeRequirements(ctx)
3112}
3113
3114func (module *sdkLibraryXml) validateAtLeastTAttributes(ctx android.ModuleContext) {
3115 t := android.ApiLevelOrPanic(ctx, "Tiramisu")
3116 module.attrAtLeastT(ctx, t, module.properties.Min_device_sdk, "min_device_sdk")
3117 module.attrAtLeastT(ctx, t, module.properties.Max_device_sdk, "max_device_sdk")
3118 module.attrAtLeastT(ctx, t, module.properties.On_bootclasspath_before, "on_bootclasspath_before")
3119 module.attrAtLeastT(ctx, t, module.properties.On_bootclasspath_since, "on_bootclasspath_since")
3120}
3121
3122func (module *sdkLibraryXml) attrAtLeastT(ctx android.ModuleContext, t android.ApiLevel, attr *string, attrName string) {
3123 if attr != nil {
3124 if level, err := android.ApiLevelFromUser(ctx, *attr); err == nil {
3125 // we will inform the user of invalid inputs when we try to write the
3126 // permissions xml file so we don't need to do it here
3127 if t.GreaterThan(level) {
3128 ctx.PropertyErrorf(attrName, "Attribute value needs to be at least T")
3129 }
3130 }
3131 }
3132}
3133
3134func (module *sdkLibraryXml) validateMinAndMaxDeviceSdk(ctx android.ModuleContext) {
3135 if module.properties.Min_device_sdk != nil && module.properties.Max_device_sdk != nil {
3136 min, minErr := android.ApiLevelFromUser(ctx, *module.properties.Min_device_sdk)
3137 max, maxErr := android.ApiLevelFromUser(ctx, *module.properties.Max_device_sdk)
3138 if minErr == nil && maxErr == nil {
3139 // we will inform the user of invalid inputs when we try to write the
3140 // permissions xml file so we don't need to do it here
3141 if min.GreaterThan(max) {
3142 ctx.ModuleErrorf("min_device_sdk can't be greater than max_device_sdk")
3143 }
3144 }
3145 }
3146}
3147
3148func (module *sdkLibraryXml) validateMinMaxDeviceSdkAndModuleMinSdk(ctx android.ModuleContext) {
3149 moduleMinApi := android.ApiLevelOrPanic(ctx, *module.properties.Sdk_library_min_api_level)
3150 if module.properties.Min_device_sdk != nil {
3151 api, err := android.ApiLevelFromUser(ctx, *module.properties.Min_device_sdk)
3152 if err == nil {
3153 if moduleMinApi.GreaterThan(api) {
3154 ctx.PropertyErrorf("min_device_sdk", "Can't be less than module's min sdk (%s)", moduleMinApi)
3155 }
3156 }
3157 }
3158 if module.properties.Max_device_sdk != nil {
3159 api, err := android.ApiLevelFromUser(ctx, *module.properties.Max_device_sdk)
3160 if err == nil {
3161 if moduleMinApi.GreaterThan(api) {
3162 ctx.PropertyErrorf("max_device_sdk", "Can't be less than module's min sdk (%s)", moduleMinApi)
3163 }
3164 }
3165 }
3166}
3167
3168func (module *sdkLibraryXml) validateOnBootclasspathBeforeRequirements(ctx android.ModuleContext) {
3169 moduleMinApi := android.ApiLevelOrPanic(ctx, *module.properties.Sdk_library_min_api_level)
3170 if module.properties.On_bootclasspath_before != nil {
3171 t := android.ApiLevelOrPanic(ctx, "Tiramisu")
3172 // if we use the attribute, then we need to do this validation
3173 if moduleMinApi.LessThan(t) {
3174 // if minAPi is < T, then we need to have min_device_sdk (which only accepts T+)
3175 if module.properties.Min_device_sdk == nil {
3176 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")
3177 }
3178 }
3179 }
3180}
3181
Paul Duffindd46f712020-02-10 13:37:10 +00003182type sdkLibrarySdkMemberType struct {
3183 android.SdkMemberTypeBase
3184}
3185
Paul Duffin296701e2021-07-14 10:29:36 +01003186func (s *sdkLibrarySdkMemberType) AddDependencies(ctx android.SdkDependencyContext, dependencyTag blueprint.DependencyTag, names []string) {
3187 ctx.AddVariationDependencies(nil, dependencyTag, names...)
Paul Duffindd46f712020-02-10 13:37:10 +00003188}
3189
3190func (s *sdkLibrarySdkMemberType) IsInstance(module android.Module) bool {
3191 _, ok := module.(*SdkLibrary)
3192 return ok
3193}
3194
3195func (s *sdkLibrarySdkMemberType) AddPrebuiltModule(ctx android.SdkMemberContext, member android.SdkMember) android.BpModule {
3196 return ctx.SnapshotBuilder().AddPrebuiltModule(member, "java_sdk_library_import")
3197}
3198
3199func (s *sdkLibrarySdkMemberType) CreateVariantPropertiesStruct() android.SdkMemberProperties {
3200 return &sdkLibrarySdkMemberProperties{}
3201}
3202
Paul Duffin976b0e52021-04-27 23:20:26 +01003203var javaSdkLibrarySdkMemberType = &sdkLibrarySdkMemberType{
3204 android.SdkMemberTypeBase{
3205 PropertyName: "java_sdk_libs",
3206 SupportsSdk: true,
3207 },
3208}
3209
Paul Duffindd46f712020-02-10 13:37:10 +00003210type sdkLibrarySdkMemberProperties struct {
3211 android.SdkMemberPropertiesBase
3212
Paul Duffine8409952022-09-22 16:24:46 +01003213 // Stem name for files in the sdk snapshot.
3214 //
3215 // This is used to construct the path names of various sdk library files in the sdk snapshot to
3216 // make sure that they match the finalized versions of those files in prebuilts/sdk.
3217 //
3218 // This property is marked as keep so that it will be kept in all instances of this struct, will
3219 // not be cleared but will be copied to common structs. That is needed because this field is used
3220 // to construct many file names for other parts of this struct and so it needs to be present in
3221 // all structs. If it was not marked as keep then it would be cleared in some structs and so would
3222 // be unavailable for generating file names if there were other properties that were still set.
3223 Stem string `sdk:"keep"`
3224
Paul Duffindd46f712020-02-10 13:37:10 +00003225 // Scope to per scope properties.
Paul Duffin106a3a42022-01-27 16:39:06 +00003226 Scopes map[*apiScope]*scopeProperties
Paul Duffindd46f712020-02-10 13:37:10 +00003227
Paul Duffin3d1248c2020-04-09 00:10:17 +01003228 // The Java stubs source files.
3229 Stub_srcs []string
Paul Duffinf7a64332020-05-13 16:54:55 +01003230
3231 // The naming scheme.
3232 Naming_scheme *string
Paul Duffind7eb1c22020-05-26 20:57:10 +01003233
3234 // True if the java_sdk_library_import is for a shared library, false
3235 // otherwise.
3236 Shared_library *bool
Paul Duffina2ae7e02020-09-11 11:55:00 +01003237
Paul Duffin1267d872021-04-16 17:21:36 +01003238 // True if the stub imports should produce dex jars.
3239 Compile_dex *bool
3240
Paul Duffina2ae7e02020-09-11 11:55:00 +01003241 // The paths to the doctag files to add to the prebuilt.
3242 Doctag_paths android.Paths
Paul Duffin869de142021-07-15 14:14:41 +01003243
3244 Permitted_packages []string
Pedro Loureiro9956e5e2021-09-07 17:21:59 +00003245
3246 // Signals that this shared library is part of the bootclasspath starting
3247 // on the version indicated in this attribute.
3248 //
3249 // This will make platforms at this level and above to ignore
3250 // <uses-library> tags with this library name because the library is already
3251 // available
3252 On_bootclasspath_since *string
3253
3254 // Signals that this shared library was part of the bootclasspath before
3255 // (but not including) the version indicated in this attribute.
3256 //
3257 // The system will automatically add a <uses-library> tag with this library to
3258 // apps that target any SDK less than the version indicated in this attribute.
3259 On_bootclasspath_before *string
3260
3261 // Indicates that PackageManager should ignore this shared library if the
3262 // platform is below the version indicated in this attribute.
3263 //
3264 // This means that the device won't recognise this library as installed.
3265 Min_device_sdk *string
3266
3267 // Indicates that PackageManager should ignore this shared library if the
3268 // platform is above the version indicated in this attribute.
3269 //
3270 // This means that the device won't recognise this library as installed.
3271 Max_device_sdk *string
Jiakai Zhang9c4dc192023-02-09 00:09:24 +08003272
3273 DexPreoptProfileGuided *bool `supported_build_releases:"UpsideDownCake+"`
Paul Duffindd46f712020-02-10 13:37:10 +00003274}
3275
3276type scopeProperties struct {
Paul Duffin1fd005d2020-04-09 01:08:11 +01003277 Jars android.Paths
3278 StubsSrcJar android.Path
3279 CurrentApiFile android.Path
3280 RemovedApiFile android.Path
Paul Duffine7babdb2022-02-10 13:06:54 +00003281 AnnotationsZip android.Path `supported_build_releases:"Tiramisu+"`
Paul Duffin1fd005d2020-04-09 01:08:11 +01003282 SdkVersion string
Paul Duffindd46f712020-02-10 13:37:10 +00003283}
3284
3285func (s *sdkLibrarySdkMemberProperties) PopulateFromVariant(ctx android.SdkMemberContext, variant android.Module) {
3286 sdk := variant.(*SdkLibrary)
3287
Paul Duffine8409952022-09-22 16:24:46 +01003288 // Copy the stem name for files in the sdk snapshot.
3289 s.Stem = sdk.distStem()
3290
Paul Duffin106a3a42022-01-27 16:39:06 +00003291 s.Scopes = make(map[*apiScope]*scopeProperties)
Paul Duffindd46f712020-02-10 13:37:10 +00003292 for _, apiScope := range allApiScopes {
Paul Duffin803a9562020-05-20 11:52:25 +01003293 paths := sdk.findScopePaths(apiScope)
3294 if paths == nil {
3295 continue
3296 }
3297
Paul Duffindd46f712020-02-10 13:37:10 +00003298 jars := paths.stubsImplPath
3299 if len(jars) > 0 {
3300 properties := scopeProperties{}
3301 properties.Jars = jars
Paul Duffin780c5f42020-05-12 15:52:55 +01003302 properties.SdkVersion = sdk.sdkVersionForStubsLibrary(ctx.SdkModuleContext(), apiScope)
Paul Duffin0f8faff2020-05-20 16:18:00 +01003303 properties.StubsSrcJar = paths.stubsSrcJar.Path()
Paul Duffin10269f12020-06-19 18:39:55 +01003304 if paths.currentApiFilePath.Valid() {
3305 properties.CurrentApiFile = paths.currentApiFilePath.Path()
3306 }
3307 if paths.removedApiFilePath.Valid() {
3308 properties.RemovedApiFile = paths.removedApiFilePath.Path()
3309 }
Anton Hanssond78eb762021-09-21 15:25:12 +01003310 // The annotations zip is only available for modules that set annotations_enabled: true.
3311 if paths.annotationsZip.Valid() {
3312 properties.AnnotationsZip = paths.annotationsZip.Path()
3313 }
Paul Duffin106a3a42022-01-27 16:39:06 +00003314 s.Scopes[apiScope] = &properties
Paul Duffindd46f712020-02-10 13:37:10 +00003315 }
3316 }
3317
Paul Duffindfa131e2020-05-15 20:37:11 +01003318 s.Naming_scheme = sdk.commonSdkLibraryProperties.Naming_scheme
Paul Duffind7eb1c22020-05-26 20:57:10 +01003319 s.Shared_library = proptools.BoolPtr(sdk.sharedLibrary())
Paul Duffin1267d872021-04-16 17:21:36 +01003320 s.Compile_dex = sdk.dexProperties.Compile_dex
Paul Duffina2ae7e02020-09-11 11:55:00 +01003321 s.Doctag_paths = sdk.doctagPaths
Paul Duffin869de142021-07-15 14:14:41 +01003322 s.Permitted_packages = sdk.PermittedPackagesForUpdatableBootJars()
Pedro Loureiro9956e5e2021-09-07 17:21:59 +00003323 s.On_bootclasspath_since = sdk.commonSdkLibraryProperties.On_bootclasspath_since
3324 s.On_bootclasspath_before = sdk.commonSdkLibraryProperties.On_bootclasspath_before
3325 s.Min_device_sdk = sdk.commonSdkLibraryProperties.Min_device_sdk
3326 s.Max_device_sdk = sdk.commonSdkLibraryProperties.Max_device_sdk
Jiakai Zhang9c4dc192023-02-09 00:09:24 +08003327
3328 if sdk.dexpreopter.dexpreoptProperties.Dex_preopt_result.Profile_guided {
3329 s.DexPreoptProfileGuided = proptools.BoolPtr(true)
3330 }
Paul Duffindd46f712020-02-10 13:37:10 +00003331}
3332
3333func (s *sdkLibrarySdkMemberProperties) AddToPropertySet(ctx android.SdkMemberContext, propertySet android.BpPropertySet) {
Paul Duffinf7a64332020-05-13 16:54:55 +01003334 if s.Naming_scheme != nil {
3335 propertySet.AddProperty("naming_scheme", proptools.String(s.Naming_scheme))
3336 }
Paul Duffind7eb1c22020-05-26 20:57:10 +01003337 if s.Shared_library != nil {
3338 propertySet.AddProperty("shared_library", *s.Shared_library)
3339 }
Paul Duffin1267d872021-04-16 17:21:36 +01003340 if s.Compile_dex != nil {
3341 propertySet.AddProperty("compile_dex", *s.Compile_dex)
3342 }
Paul Duffin869de142021-07-15 14:14:41 +01003343 if len(s.Permitted_packages) > 0 {
3344 propertySet.AddProperty("permitted_packages", s.Permitted_packages)
3345 }
Jiakai Zhang9c4dc192023-02-09 00:09:24 +08003346 dexPreoptSet := propertySet.AddPropertySet("dex_preopt")
3347 if s.DexPreoptProfileGuided != nil {
3348 dexPreoptSet.AddProperty("profile_guided", proptools.Bool(s.DexPreoptProfileGuided))
3349 }
Paul Duffinf7a64332020-05-13 16:54:55 +01003350
Paul Duffine8409952022-09-22 16:24:46 +01003351 stem := s.Stem
3352
Paul Duffindd46f712020-02-10 13:37:10 +00003353 for _, apiScope := range allApiScopes {
3354 if properties, ok := s.Scopes[apiScope]; ok {
Paul Duffin6b836ba2020-05-13 19:19:49 +01003355 scopeSet := propertySet.AddPropertySet(apiScope.propertyName)
Paul Duffindd46f712020-02-10 13:37:10 +00003356
Paul Duffin958806b2022-05-16 13:10:47 +00003357 scopeDir := apiScope.snapshotRelativeDir()
Paul Duffin3d1248c2020-04-09 00:10:17 +01003358
Paul Duffindd46f712020-02-10 13:37:10 +00003359 var jars []string
3360 for _, p := range properties.Jars {
Paul Duffine8409952022-09-22 16:24:46 +01003361 dest := filepath.Join(scopeDir, stem+"-stubs.jar")
Paul Duffindd46f712020-02-10 13:37:10 +00003362 ctx.SnapshotBuilder().CopyToSnapshot(p, dest)
3363 jars = append(jars, dest)
3364 }
3365 scopeSet.AddProperty("jars", jars)
3366
Paul Duffin22628d52021-05-12 23:13:22 +01003367 if ctx.SdkModuleContext().Config().IsEnvTrue("SOONG_SDK_SNAPSHOT_USE_SRCJAR") {
3368 // Copy the stubs source jar into the snapshot zip as is.
Paul Duffine8409952022-09-22 16:24:46 +01003369 srcJarSnapshotPath := filepath.Join(scopeDir, stem+".srcjar")
Paul Duffin22628d52021-05-12 23:13:22 +01003370 ctx.SnapshotBuilder().CopyToSnapshot(properties.StubsSrcJar, srcJarSnapshotPath)
3371 scopeSet.AddProperty("stub_srcs", []string{srcJarSnapshotPath})
3372 } else {
3373 // Merge the stubs source jar into the snapshot zip so that when it is unpacked
3374 // the source files are also unpacked.
Paul Duffine8409952022-09-22 16:24:46 +01003375 snapshotRelativeDir := filepath.Join(scopeDir, stem+"_stub_sources")
Paul Duffin22628d52021-05-12 23:13:22 +01003376 ctx.SnapshotBuilder().UnzipToSnapshot(properties.StubsSrcJar, snapshotRelativeDir)
3377 scopeSet.AddProperty("stub_srcs", []string{snapshotRelativeDir})
3378 }
Paul Duffin3d1248c2020-04-09 00:10:17 +01003379
Paul Duffin1fd005d2020-04-09 01:08:11 +01003380 if properties.CurrentApiFile != nil {
Paul Duffine8409952022-09-22 16:24:46 +01003381 currentApiSnapshotPath := apiScope.snapshotRelativeCurrentApiTxtPath(stem)
Paul Duffin1fd005d2020-04-09 01:08:11 +01003382 ctx.SnapshotBuilder().CopyToSnapshot(properties.CurrentApiFile, currentApiSnapshotPath)
3383 scopeSet.AddProperty("current_api", currentApiSnapshotPath)
3384 }
3385
3386 if properties.RemovedApiFile != nil {
Paul Duffine8409952022-09-22 16:24:46 +01003387 removedApiSnapshotPath := apiScope.snapshotRelativeRemovedApiTxtPath(stem)
Paul Duffin3dbf9fd2020-06-02 13:00:02 +01003388 ctx.SnapshotBuilder().CopyToSnapshot(properties.RemovedApiFile, removedApiSnapshotPath)
Paul Duffin1fd005d2020-04-09 01:08:11 +01003389 scopeSet.AddProperty("removed_api", removedApiSnapshotPath)
3390 }
3391
Anton Hanssond78eb762021-09-21 15:25:12 +01003392 if properties.AnnotationsZip != nil {
Paul Duffine8409952022-09-22 16:24:46 +01003393 annotationsSnapshotPath := filepath.Join(scopeDir, stem+"_annotations.zip")
Anton Hanssond78eb762021-09-21 15:25:12 +01003394 ctx.SnapshotBuilder().CopyToSnapshot(properties.AnnotationsZip, annotationsSnapshotPath)
3395 scopeSet.AddProperty("annotations", annotationsSnapshotPath)
3396 }
3397
Paul Duffindd46f712020-02-10 13:37:10 +00003398 if properties.SdkVersion != "" {
3399 scopeSet.AddProperty("sdk_version", properties.SdkVersion)
3400 }
3401 }
3402 }
3403
Paul Duffina2ae7e02020-09-11 11:55:00 +01003404 if len(s.Doctag_paths) > 0 {
3405 dests := []string{}
3406 for _, p := range s.Doctag_paths {
3407 dest := filepath.Join("doctags", p.Rel())
3408 ctx.SnapshotBuilder().CopyToSnapshot(p, dest)
3409 dests = append(dests, dest)
3410 }
3411 propertySet.AddProperty("doctag_files", dests)
3412 }
Paul Duffindd46f712020-02-10 13:37:10 +00003413}