blob: 6998bd2c9f6a4aa4d82d17c76820b68f04f47039 [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
Jihoon Kang6592e872023-12-19 01:13:16 +0000631 // a list of aconfig_declarations module names that the stubs generated in this module
632 // depend on.
633 Aconfig_declarations []string
634
Jiyong Parkc678ad32018-04-10 13:07:10 +0900635 // TODO: determines whether to create HTML doc or not
Paul Duffine8409952022-09-22 16:24:46 +0100636 // Html_doc *bool
Jiyong Parkc678ad32018-04-10 13:07:10 +0900637}
638
Paul Duffin0f8faff2020-05-20 16:18:00 +0100639// Paths to outputs from java_sdk_library and java_sdk_library_import.
640//
641// Fields that are android.Paths are always set (during GenerateAndroidBuildActions).
642// OptionalPaths are always set by java_sdk_library but may not be set by
643// java_sdk_library_import as not all instances provide that information.
Paul Duffind1b3a922020-01-22 11:57:20 +0000644type scopePaths struct {
Paul Duffin0f8faff2020-05-20 16:18:00 +0100645 // The path (represented as Paths for convenience when returning) to the stubs header jar.
646 //
647 // That is the jar that is created by turbine.
648 stubsHeaderPath android.Paths
649
650 // The path (represented as Paths for convenience when returning) to the stubs implementation jar.
651 //
652 // This is not the implementation jar, it still only contains stubs.
653 stubsImplPath android.Paths
654
Paul Duffin1267d872021-04-16 17:21:36 +0100655 // The dex jar for the stubs.
656 //
657 // This is not the implementation jar, it still only contains stubs.
Martin Stjernholm8be1e6d2021-09-15 03:34:04 +0100658 stubsDexJarPath OptionalDexJarPath
Paul Duffin1267d872021-04-16 17:21:36 +0100659
Paul Duffin0f8faff2020-05-20 16:18:00 +0100660 // The API specification file, e.g. system_current.txt.
661 currentApiFilePath android.OptionalPath
662
663 // The specification of API elements removed since the last release.
664 removedApiFilePath android.OptionalPath
665
666 // The stubs source jar.
667 stubsSrcJar android.OptionalPath
Anton Hanssond78eb762021-09-21 15:25:12 +0100668
669 // Extracted annotations.
670 annotationsZip android.OptionalPath
Paul Duffin958806b2022-05-16 13:10:47 +0000671
672 // The path to the latest API file.
673 latestApiPath android.OptionalPath
674
675 // The path to the latest removed API file.
676 latestRemovedApiPath android.OptionalPath
Paul Duffind1b3a922020-01-22 11:57:20 +0000677}
678
Colin Crossdcf71b22021-02-01 13:59:03 -0800679func (paths *scopePaths) extractStubsLibraryInfoFromDependency(ctx android.ModuleContext, dep android.Module) error {
Colin Cross313aa542023-12-13 13:47:44 -0800680 if lib, ok := android.OtherModuleProvider(ctx, dep, JavaInfoProvider); ok {
Colin Crossdcf71b22021-02-01 13:59:03 -0800681 paths.stubsHeaderPath = lib.HeaderJars
682 paths.stubsImplPath = lib.ImplementationJars
Paul Duffin1267d872021-04-16 17:21:36 +0100683
684 libDep := dep.(UsesLibraryDependency)
685 paths.stubsDexJarPath = libDep.DexJarBuildPath()
Paul Duffinc8782502020-04-29 20:45:27 +0100686 return nil
687 } else {
Colin Crossdcf71b22021-02-01 13:59:03 -0800688 return fmt.Errorf("expected module that has JavaInfoProvider, e.g. java_library")
Paul Duffinc8782502020-04-29 20:45:27 +0100689 }
690}
691
Paul Duffin0ff08bd2020-04-29 13:30:54 +0100692func (paths *scopePaths) treatDepAsApiStubsProvider(dep android.Module, action func(provider ApiStubsProvider)) error {
693 if apiStubsProvider, ok := dep.(ApiStubsProvider); ok {
694 action(apiStubsProvider)
Paul Duffinc8782502020-04-29 20:45:27 +0100695 return nil
696 } else {
697 return fmt.Errorf("expected module that implements ApiStubsProvider, e.g. droidstubs")
698 }
699}
700
Paul Duffin0f8faff2020-05-20 16:18:00 +0100701func (paths *scopePaths) treatDepAsApiStubsSrcProvider(dep android.Module, action func(provider ApiStubsSrcProvider)) error {
702 if apiStubsProvider, ok := dep.(ApiStubsSrcProvider); ok {
703 action(apiStubsProvider)
704 return nil
705 } else {
706 return fmt.Errorf("expected module that implements ApiStubsSrcProvider, e.g. droidstubs")
707 }
708}
709
Paul Duffin0ff08bd2020-04-29 13:30:54 +0100710func (paths *scopePaths) extractApiInfoFromApiStubsProvider(provider ApiStubsProvider) {
Anton Hanssond78eb762021-09-21 15:25:12 +0100711 paths.annotationsZip = android.OptionalPathForPath(provider.AnnotationsZip())
Paul Duffin0f8faff2020-05-20 16:18:00 +0100712 paths.currentApiFilePath = android.OptionalPathForPath(provider.ApiFilePath())
713 paths.removedApiFilePath = android.OptionalPathForPath(provider.RemovedApiFilePath())
Paul Duffin0ff08bd2020-04-29 13:30:54 +0100714}
715
Colin Crossdcf71b22021-02-01 13:59:03 -0800716func (paths *scopePaths) extractApiInfoFromDep(ctx android.ModuleContext, dep android.Module) error {
Paul Duffin0ff08bd2020-04-29 13:30:54 +0100717 return paths.treatDepAsApiStubsProvider(dep, func(provider ApiStubsProvider) {
718 paths.extractApiInfoFromApiStubsProvider(provider)
719 })
720}
721
Paul Duffin0f8faff2020-05-20 16:18:00 +0100722func (paths *scopePaths) extractStubsSourceInfoFromApiStubsProviders(provider ApiStubsSrcProvider) {
723 paths.stubsSrcJar = android.OptionalPathForPath(provider.StubsSrcJar())
Paul Duffin0ff08bd2020-04-29 13:30:54 +0100724}
725
Colin Crossdcf71b22021-02-01 13:59:03 -0800726func (paths *scopePaths) extractStubsSourceInfoFromDep(ctx android.ModuleContext, dep android.Module) error {
Paul Duffin0f8faff2020-05-20 16:18:00 +0100727 return paths.treatDepAsApiStubsSrcProvider(dep, func(provider ApiStubsSrcProvider) {
Paul Duffin0ff08bd2020-04-29 13:30:54 +0100728 paths.extractStubsSourceInfoFromApiStubsProviders(provider)
729 })
730}
731
Colin Crossdcf71b22021-02-01 13:59:03 -0800732func (paths *scopePaths) extractStubsSourceAndApiInfoFromApiStubsProvider(ctx android.ModuleContext, dep android.Module) error {
Paul Duffin0ff08bd2020-04-29 13:30:54 +0100733 return paths.treatDepAsApiStubsProvider(dep, func(provider ApiStubsProvider) {
734 paths.extractApiInfoFromApiStubsProvider(provider)
735 paths.extractStubsSourceInfoFromApiStubsProviders(provider)
736 })
737}
738
Paul Duffin958806b2022-05-16 13:10:47 +0000739func extractSingleOptionalOutputPath(dep android.Module) (android.OptionalPath, error) {
740 var paths android.Paths
741 if sourceFileProducer, ok := dep.(android.SourceFileProducer); ok {
742 paths = sourceFileProducer.Srcs()
743 } else {
744 return android.OptionalPath{}, fmt.Errorf("module %q does not produce source files", dep)
745 }
746 if len(paths) != 1 {
747 return android.OptionalPath{}, fmt.Errorf("expected one path from %q, got %q", dep, paths)
748 }
749 return android.OptionalPathForPath(paths[0]), nil
750}
751
752func (paths *scopePaths) extractLatestApiPath(ctx android.ModuleContext, dep android.Module) error {
753 outputPath, err := extractSingleOptionalOutputPath(dep)
754 paths.latestApiPath = outputPath
755 return err
756}
757
758func (paths *scopePaths) extractLatestRemovedApiPath(ctx android.ModuleContext, dep android.Module) error {
759 outputPath, err := extractSingleOptionalOutputPath(dep)
760 paths.latestRemovedApiPath = outputPath
761 return err
762}
763
Paul Duffin0ff08bd2020-04-29 13:30:54 +0100764type commonToSdkLibraryAndImportProperties struct {
Paul Duffin1b1e8062020-05-08 13:44:43 +0100765 // The naming scheme to use for the components that this module creates.
766 //
Paul Duffinee9ad5d2020-09-11 13:04:05 +0100767 // If not specified then it defaults to "default".
Paul Duffin1b1e8062020-05-08 13:44:43 +0100768 //
769 // This is a temporary mechanism to simplify conversion from separate modules for each
770 // component that follow a different naming pattern to the default one.
771 //
772 // TODO(b/155480189) - Remove once naming inconsistencies have been resolved.
Paul Duffin0ff08bd2020-04-29 13:30:54 +0100773 Naming_scheme *string
Paul Duffindfa131e2020-05-15 20:37:11 +0100774
775 // Specifies whether this module can be used as an Android shared library; defaults
776 // to true.
777 //
778 // An Android shared library is one that can be referenced in a <uses-library> element
779 // in an AndroidManifest.xml.
780 Shared_library *bool
Paul Duffina2ae7e02020-09-11 11:55:00 +0100781
782 // Files containing information about supported java doc tags.
783 Doctag_files []string `android:"path"`
Pedro Loureiro9956e5e2021-09-07 17:21:59 +0000784
785 // Signals that this shared library is part of the bootclasspath starting
786 // on the version indicated in this attribute.
787 //
788 // This will make platforms at this level and above to ignore
789 // <uses-library> tags with this library name because the library is already
790 // available
791 On_bootclasspath_since *string
792
793 // Signals that this shared library was part of the bootclasspath before
794 // (but not including) the version indicated in this attribute.
795 //
796 // The system will automatically add a <uses-library> tag with this library to
797 // apps that target any SDK less than the version indicated in this attribute.
798 On_bootclasspath_before *string
799
800 // Indicates that PackageManager should ignore this shared library if the
801 // platform is below the version indicated in this attribute.
802 //
803 // This means that the device won't recognise this library as installed.
804 Min_device_sdk *string
805
806 // Indicates that PackageManager should ignore this shared library if the
807 // platform is above the version indicated in this attribute.
808 //
809 // This means that the device won't recognise this library as installed.
810 Max_device_sdk *string
Paul Duffin0ff08bd2020-04-29 13:30:54 +0100811}
812
Paul Duffin71b33cc2021-06-23 11:39:47 +0100813// commonSdkLibraryAndImportModule defines the interface that must be provided by a module that
814// embeds the commonToSdkLibraryAndImport struct.
815type commonSdkLibraryAndImportModule interface {
Paul Duffind796f6f2022-11-23 23:06:05 +0000816 android.Module
Paul Duffin71b33cc2021-06-23 11:39:47 +0100817
818 BaseModuleName() string
819}
820
Paul Duffin56d44902020-01-31 13:36:25 +0000821// Common code between sdk library and sdk library import
822type commonToSdkLibraryAndImport struct {
Paul Duffin71b33cc2021-06-23 11:39:47 +0100823 module commonSdkLibraryAndImportModule
Paul Duffinc3091c82020-05-08 14:16:20 +0100824
Paul Duffin56d44902020-01-31 13:36:25 +0000825 scopePaths map[*apiScope]*scopePaths
Paul Duffin1b1e8062020-05-08 13:44:43 +0100826
827 namingScheme sdkLibraryComponentNamingScheme
828
Paul Duffindfa131e2020-05-15 20:37:11 +0100829 commonSdkLibraryProperties commonToSdkLibraryAndImportProperties
Paul Duffin859fe962020-05-15 10:20:31 +0100830
Paul Duffina2ae7e02020-09-11 11:55:00 +0100831 // Paths to commonSdkLibraryProperties.Doctag_files
832 doctagPaths android.Paths
833
Paul Duffin859fe962020-05-15 10:20:31 +0100834 // Functionality related to this being used as a component of a java_sdk_library.
835 EmbeddableSdkLibraryComponent
Paul Duffin56d44902020-01-31 13:36:25 +0000836}
837
Paul Duffin71b33cc2021-06-23 11:39:47 +0100838func (c *commonToSdkLibraryAndImport) initCommon(module commonSdkLibraryAndImportModule) {
839 c.module = module
Paul Duffin1b1e8062020-05-08 13:44:43 +0100840
Paul Duffin71b33cc2021-06-23 11:39:47 +0100841 module.AddProperties(&c.commonSdkLibraryProperties)
Paul Duffin859fe962020-05-15 10:20:31 +0100842
843 // Initialize this as an sdk library component.
Paul Duffin71b33cc2021-06-23 11:39:47 +0100844 c.initSdkLibraryComponent(module)
Paul Duffin1b1e8062020-05-08 13:44:43 +0100845}
846
847func (c *commonToSdkLibraryAndImport) initCommonAfterDefaultsApplied(ctx android.DefaultableHookContext) bool {
Paul Duffindfa131e2020-05-15 20:37:11 +0100848 schemeProperty := proptools.StringDefault(c.commonSdkLibraryProperties.Naming_scheme, "default")
Paul Duffin1b1e8062020-05-08 13:44:43 +0100849 switch schemeProperty {
850 case "default":
851 c.namingScheme = &defaultNamingScheme{}
852 default:
853 ctx.PropertyErrorf("naming_scheme", "expected 'default' but was %q", schemeProperty)
854 return false
855 }
856
Paul Duffin3f0290e2021-06-30 18:25:36 +0100857 namePtr := proptools.StringPtr(c.module.BaseModuleName())
858 c.sdkLibraryComponentProperties.SdkLibraryName = namePtr
859
Paul Duffindfa131e2020-05-15 20:37:11 +0100860 // Only track this sdk library if this can be used as a shared library.
861 if c.sharedLibrary() {
862 // Use the name specified in the module definition as the owner.
Paul Duffin3f0290e2021-06-30 18:25:36 +0100863 c.sdkLibraryComponentProperties.SdkLibraryToImplicitlyTrack = namePtr
Paul Duffindfa131e2020-05-15 20:37:11 +0100864 }
Paul Duffin859fe962020-05-15 10:20:31 +0100865
Paul Duffin1b1e8062020-05-08 13:44:43 +0100866 return true
Paul Duffinc3091c82020-05-08 14:16:20 +0100867}
868
Paul Duffinea8f8082021-06-24 13:25:57 +0100869// uniqueApexVariations provides common implementation of the ApexModule.UniqueApexVariations
870// method.
871func (c *commonToSdkLibraryAndImport) uniqueApexVariations() bool {
872 // A java_sdk_library that is a shared library produces an XML file that makes the shared library
873 // usable from an AndroidManifest.xml's <uses-library> entry. That XML file contains the name of
874 // the APEX and so it needs a unique variation per APEX.
875 return c.sharedLibrary()
876}
877
Paul Duffina2ae7e02020-09-11 11:55:00 +0100878func (c *commonToSdkLibraryAndImport) generateCommonBuildActions(ctx android.ModuleContext) {
879 c.doctagPaths = android.PathsForModuleSrc(ctx, c.commonSdkLibraryProperties.Doctag_files)
880}
881
Paul Duffineedc5d52020-06-12 17:46:39 +0100882// Module name of the runtime implementation library
883func (c *commonToSdkLibraryAndImport) implLibraryModuleName() string {
Paul Duffin71b33cc2021-06-23 11:39:47 +0100884 return c.module.BaseModuleName() + ".impl"
Paul Duffineedc5d52020-06-12 17:46:39 +0100885}
886
887// Module name of the XML file for the lib
888func (c *commonToSdkLibraryAndImport) xmlPermissionsModuleName() string {
Paul Duffin71b33cc2021-06-23 11:39:47 +0100889 return c.module.BaseModuleName() + sdkXmlFileSuffix
Paul Duffineedc5d52020-06-12 17:46:39 +0100890}
891
Paul Duffinc3091c82020-05-08 14:16:20 +0100892// Name of the java_library module that compiles the stubs source.
893func (c *commonToSdkLibraryAndImport) stubsLibraryModuleName(apiScope *apiScope) string {
Paul Duffinb97b1572021-04-29 21:50:40 +0100894 baseName := c.module.BaseModuleName()
Paul Duffin21787622022-11-25 12:48:20 +0000895 return c.namingScheme.stubsLibraryModuleName(apiScope, baseName)
Paul Duffinc3091c82020-05-08 14:16:20 +0100896}
897
898// Name of the droidstubs module that generates the stubs source and may also
899// generate/check the API.
900func (c *commonToSdkLibraryAndImport) stubsSourceModuleName(apiScope *apiScope) string {
Paul Duffinb97b1572021-04-29 21:50:40 +0100901 baseName := c.module.BaseModuleName()
Paul Duffin21787622022-11-25 12:48:20 +0000902 return c.namingScheme.stubsSourceModuleName(apiScope, baseName)
Paul Duffinc3091c82020-05-08 14:16:20 +0100903}
904
Jihoon Kang1c92c3e2023-03-23 17:44:51 +0000905// Name of the java_api_library module that generates the from-text stubs source
906// and compiles to a jar file.
907func (c *commonToSdkLibraryAndImport) apiLibraryModuleName(apiScope *apiScope) string {
908 baseName := c.module.BaseModuleName()
909 return c.namingScheme.apiLibraryModuleName(apiScope, baseName)
910}
911
Jihoon Kang1147b312023-06-08 23:25:57 +0000912// Name of the java_library module that compiles the stubs
913// generated from source Java files.
914func (c *commonToSdkLibraryAndImport) sourceStubLibraryModuleName(apiScope *apiScope) string {
915 baseName := c.module.BaseModuleName()
916 return c.namingScheme.sourceStubLibraryModuleName(apiScope, baseName)
917}
918
Paul Duffin46dc45a2020-05-14 15:39:10 +0100919// The component names for different outputs of the java_sdk_library.
920//
921// They are similar to the names used for the child modules it creates
922const (
923 stubsSourceComponentName = "stubs.source"
924
925 apiTxtComponentName = "api.txt"
926
927 removedApiTxtComponentName = "removed-api.txt"
Anton Hanssond78eb762021-09-21 15:25:12 +0100928
929 annotationsComponentName = "annotations.zip"
Paul Duffin46dc45a2020-05-14 15:39:10 +0100930)
931
932// A regular expression to match tags that reference a specific stubs component.
933//
934// It will only match if given a valid scope and a valid component. It is verfy strict
935// to ensure it does not accidentally match a similar looking tag that should be processed
936// by the embedded Library.
937var tagSplitter = func() *regexp.Regexp {
938 // Given a list of literal string items returns a regular expression that will
939 // match any one of the items.
940 choice := func(items ...string) string {
941 return `\Q` + strings.Join(items, `\E|\Q`) + `\E`
942 }
943
944 // Regular expression to match one of the scopes.
945 scopesRegexp := choice(allScopeNames...)
946
947 // Regular expression to match one of the components.
Anton Hanssond78eb762021-09-21 15:25:12 +0100948 componentsRegexp := choice(stubsSourceComponentName, apiTxtComponentName, removedApiTxtComponentName, annotationsComponentName)
Paul Duffin46dc45a2020-05-14 15:39:10 +0100949
950 // Regular expression to match any combination of one scope and one component.
951 return regexp.MustCompile(fmt.Sprintf(`^\.(%s)\.(%s)$`, scopesRegexp, componentsRegexp))
952}()
953
954// For OutputFileProducer interface
955//
Anton Hanssond78eb762021-09-21 15:25:12 +0100956// .<scope>.<component name>, for all ComponentNames (for example: .public.removed-api.txt)
Paul Duffin46dc45a2020-05-14 15:39:10 +0100957func (c *commonToSdkLibraryAndImport) commonOutputFiles(tag string) (android.Paths, error) {
958 if groups := tagSplitter.FindStringSubmatch(tag); groups != nil {
959 scopeName := groups[1]
960 component := groups[2]
961
962 if scope, ok := scopeByName[scopeName]; ok {
963 paths := c.findScopePaths(scope)
964 if paths == nil {
Paul Duffin71b33cc2021-06-23 11:39:47 +0100965 return nil, fmt.Errorf("%q does not provide api scope %s", c.module.BaseModuleName(), scopeName)
Paul Duffin46dc45a2020-05-14 15:39:10 +0100966 }
967
968 switch component {
969 case stubsSourceComponentName:
970 if paths.stubsSrcJar.Valid() {
971 return android.Paths{paths.stubsSrcJar.Path()}, nil
972 }
973
974 case apiTxtComponentName:
975 if paths.currentApiFilePath.Valid() {
976 return android.Paths{paths.currentApiFilePath.Path()}, nil
977 }
978
979 case removedApiTxtComponentName:
980 if paths.removedApiFilePath.Valid() {
981 return android.Paths{paths.removedApiFilePath.Path()}, nil
982 }
Anton Hanssond78eb762021-09-21 15:25:12 +0100983
984 case annotationsComponentName:
985 if paths.annotationsZip.Valid() {
986 return android.Paths{paths.annotationsZip.Path()}, nil
987 }
Paul Duffin46dc45a2020-05-14 15:39:10 +0100988 }
989
990 return nil, fmt.Errorf("%s not available for api scope %s", component, scopeName)
991 } else {
992 return nil, fmt.Errorf("unknown scope %s in %s", scope, tag)
993 }
994
995 } else {
Paul Duffina2ae7e02020-09-11 11:55:00 +0100996 switch tag {
997 case ".doctags":
998 if c.doctagPaths != nil {
999 return c.doctagPaths, nil
1000 } else {
Paul Duffin71b33cc2021-06-23 11:39:47 +01001001 return nil, fmt.Errorf("no doctag_files specified on %s", c.module.BaseModuleName())
Paul Duffina2ae7e02020-09-11 11:55:00 +01001002 }
1003 }
Paul Duffin46dc45a2020-05-14 15:39:10 +01001004 return nil, nil
1005 }
1006}
1007
Paul Duffin803a9562020-05-20 11:52:25 +01001008func (c *commonToSdkLibraryAndImport) getScopePathsCreateIfNeeded(scope *apiScope) *scopePaths {
Paul Duffin56d44902020-01-31 13:36:25 +00001009 if c.scopePaths == nil {
1010 c.scopePaths = make(map[*apiScope]*scopePaths)
1011 }
1012 paths := c.scopePaths[scope]
1013 if paths == nil {
1014 paths = &scopePaths{}
1015 c.scopePaths[scope] = paths
1016 }
1017
1018 return paths
1019}
1020
Paul Duffin803a9562020-05-20 11:52:25 +01001021func (c *commonToSdkLibraryAndImport) findScopePaths(scope *apiScope) *scopePaths {
1022 if c.scopePaths == nil {
1023 return nil
1024 }
1025
1026 return c.scopePaths[scope]
1027}
1028
1029// If this does not support the requested api scope then find the closest available
1030// scope it does support. Returns nil if no such scope is available.
1031func (c *commonToSdkLibraryAndImport) findClosestScopePath(scope *apiScope) *scopePaths {
Paul Duffind0b9fca2022-09-30 18:11:41 +01001032 for s := scope; s != nil; s = s.canAccess {
Paul Duffin803a9562020-05-20 11:52:25 +01001033 if paths := c.findScopePaths(s); paths != nil {
1034 return paths
1035 }
1036 }
1037
1038 // This should never happen outside tests as public should be the base scope for every
1039 // scope and is enabled by default.
1040 return nil
1041}
1042
Jiyong Parkf1691d22021-03-29 20:11:58 +09001043func (c *commonToSdkLibraryAndImport) selectHeaderJarsForSdkVersion(ctx android.BaseModuleContext, sdkVersion android.SdkSpec) android.Paths {
Paul Duffinb05d4292020-05-20 12:19:10 +01001044
1045 // If a specific numeric version has been requested then use prebuilt versions of the sdk.
Jiyong Park54105c42021-03-31 18:17:53 +09001046 if !sdkVersion.ApiLevel.IsPreview() {
Paul Duffin71b33cc2021-06-23 11:39:47 +01001047 return PrebuiltJars(ctx, c.module.BaseModuleName(), sdkVersion)
Paul Duffinb05d4292020-05-20 12:19:10 +01001048 }
1049
Paul Duffin1267d872021-04-16 17:21:36 +01001050 paths := c.selectScopePaths(ctx, sdkVersion.Kind)
1051 if paths == nil {
1052 return nil
1053 }
1054
1055 return paths.stubsHeaderPath
1056}
1057
1058// selectScopePaths returns the *scopePaths appropriate for the specific kind.
1059//
1060// If the module does not support the specific kind then it will return the *scopePaths for the
1061// closest kind which is a subset of the requested kind. e.g. if requesting android.SdkModule then
1062// it will return *scopePaths for android.SdkSystem if available or android.SdkPublic of not.
1063func (c *commonToSdkLibraryAndImport) selectScopePaths(ctx android.BaseModuleContext, kind android.SdkKind) *scopePaths {
Paul Duffin32cf58a2021-05-18 16:32:50 +01001064 apiScope := sdkKindToApiScope(kind)
Paul Duffinb05d4292020-05-20 12:19:10 +01001065
Paul Duffin803a9562020-05-20 11:52:25 +01001066 paths := c.findClosestScopePath(apiScope)
1067 if paths == nil {
1068 var scopes []string
1069 for _, s := range allApiScopes {
1070 if c.findScopePaths(s) != nil {
1071 scopes = append(scopes, s.name)
1072 }
1073 }
Paul Duffin71b33cc2021-06-23 11:39:47 +01001074 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 +01001075 return nil
1076 }
1077
Paul Duffin1267d872021-04-16 17:21:36 +01001078 return paths
1079}
1080
Paul Duffin32cf58a2021-05-18 16:32:50 +01001081// sdkKindToApiScope maps from android.SdkKind to apiScope.
1082func sdkKindToApiScope(kind android.SdkKind) *apiScope {
1083 var apiScope *apiScope
1084 switch kind {
1085 case android.SdkSystem:
1086 apiScope = apiScopeSystem
1087 case android.SdkModule:
1088 apiScope = apiScopeModuleLib
1089 case android.SdkTest:
1090 apiScope = apiScopeTest
1091 case android.SdkSystemServer:
1092 apiScope = apiScopeSystemServer
1093 default:
1094 apiScope = apiScopePublic
1095 }
1096 return apiScope
1097}
1098
Paul Duffin1267d872021-04-16 17:21:36 +01001099// to satisfy SdkLibraryDependency interface
Martin Stjernholm8be1e6d2021-09-15 03:34:04 +01001100func (c *commonToSdkLibraryAndImport) SdkApiStubDexJar(ctx android.BaseModuleContext, kind android.SdkKind) OptionalDexJarPath {
Paul Duffin1267d872021-04-16 17:21:36 +01001101 paths := c.selectScopePaths(ctx, kind)
1102 if paths == nil {
Martin Stjernholm8be1e6d2021-09-15 03:34:04 +01001103 return makeUnsetDexJarPath()
Paul Duffin1267d872021-04-16 17:21:36 +01001104 }
1105
1106 return paths.stubsDexJarPath
Paul Duffinb05d4292020-05-20 12:19:10 +01001107}
1108
Paul Duffin32cf58a2021-05-18 16:32:50 +01001109// to satisfy SdkLibraryDependency interface
1110func (c *commonToSdkLibraryAndImport) SdkRemovedTxtFile(ctx android.BaseModuleContext, kind android.SdkKind) android.OptionalPath {
1111 apiScope := sdkKindToApiScope(kind)
1112 paths := c.findScopePaths(apiScope)
1113 if paths == nil {
1114 return android.OptionalPath{}
1115 }
1116
1117 return paths.removedApiFilePath
1118}
1119
Paul Duffin859fe962020-05-15 10:20:31 +01001120func (c *commonToSdkLibraryAndImport) sdkComponentPropertiesForChildLibrary() interface{} {
1121 componentProps := &struct {
Paul Duffin3f0290e2021-06-30 18:25:36 +01001122 SdkLibraryName *string
Paul Duffin859fe962020-05-15 10:20:31 +01001123 SdkLibraryToImplicitlyTrack *string
Paul Duffindfa131e2020-05-15 20:37:11 +01001124 }{}
1125
Paul Duffin3f0290e2021-06-30 18:25:36 +01001126 namePtr := proptools.StringPtr(c.module.BaseModuleName())
1127 componentProps.SdkLibraryName = namePtr
1128
Paul Duffindfa131e2020-05-15 20:37:11 +01001129 if c.sharedLibrary() {
Paul Duffin859fe962020-05-15 10:20:31 +01001130 // Mark the stubs library as being components of this java_sdk_library so that
1131 // any app that includes code which depends (directly or indirectly) on the stubs
1132 // library will have the appropriate <uses-library> invocation inserted into its
1133 // manifest if necessary.
Paul Duffin3f0290e2021-06-30 18:25:36 +01001134 componentProps.SdkLibraryToImplicitlyTrack = namePtr
Paul Duffin859fe962020-05-15 10:20:31 +01001135 }
1136
1137 return componentProps
1138}
1139
Paul Duffindfa131e2020-05-15 20:37:11 +01001140func (c *commonToSdkLibraryAndImport) sharedLibrary() bool {
1141 return proptools.BoolDefault(c.commonSdkLibraryProperties.Shared_library, true)
1142}
1143
Paul Duffinf4600f62021-05-13 22:34:45 +01001144// Check if the stub libraries should be compiled for dex
1145func (c *commonToSdkLibraryAndImport) stubLibrariesCompiledForDex() bool {
1146 // Always compile the dex file files for the stub libraries if they will be used on the
1147 // bootclasspath.
1148 return !c.sharedLibrary()
1149}
1150
Paul Duffin859fe962020-05-15 10:20:31 +01001151// Properties related to the use of a module as an component of a java_sdk_library.
1152type SdkLibraryComponentProperties struct {
Paul Duffin3f0290e2021-06-30 18:25:36 +01001153 // The name of the java_sdk_library/_import module.
1154 SdkLibraryName *string `blueprint:"mutated"`
Paul Duffin859fe962020-05-15 10:20:31 +01001155
1156 // The name of the java_sdk_library/_import to add to a <uses-library> entry
1157 // in the AndroidManifest.xml of any Android app that includes code that references
1158 // this module. If not set then no java_sdk_library/_import is tracked.
1159 SdkLibraryToImplicitlyTrack *string `blueprint:"mutated"`
1160}
1161
1162// Structure to be embedded in a module struct that needs to support the
1163// SdkLibraryComponentDependency interface.
1164type EmbeddableSdkLibraryComponent struct {
1165 sdkLibraryComponentProperties SdkLibraryComponentProperties
1166}
1167
Paul Duffin71b33cc2021-06-23 11:39:47 +01001168func (e *EmbeddableSdkLibraryComponent) initSdkLibraryComponent(module android.Module) {
1169 module.AddProperties(&e.sdkLibraryComponentProperties)
Paul Duffin859fe962020-05-15 10:20:31 +01001170}
1171
1172// to satisfy SdkLibraryComponentDependency
Paul Duffin3f0290e2021-06-30 18:25:36 +01001173func (e *EmbeddableSdkLibraryComponent) SdkLibraryName() *string {
1174 return e.sdkLibraryComponentProperties.SdkLibraryName
1175}
1176
1177// to satisfy SdkLibraryComponentDependency
Ulya Trafimovich39b437b2020-09-23 16:42:35 +01001178func (e *EmbeddableSdkLibraryComponent) OptionalSdkLibraryImplementation() *string {
Ulya Trafimovich78645fb2021-07-16 15:29:25 +01001179 // For shared libraries, this is the same as the SDK library name. If a Java library or app
1180 // depends on a component library (e.g. a stub library) it still needs to know the name of the
1181 // run-time library and the corresponding module that provides the implementation. This name is
1182 // passed to manifest_fixer (to be added to AndroidManifest.xml) and added to CLC (to be used
1183 // in dexpreopt).
1184 //
1185 // For non-shared SDK (component or not) libraries this returns `nil`, as they are not
1186 // <uses-library> and should not be added to the manifest or to CLC.
Ulya Trafimovich39b437b2020-09-23 16:42:35 +01001187 return e.sdkLibraryComponentProperties.SdkLibraryToImplicitlyTrack
1188}
1189
Paul Duffin859fe962020-05-15 10:20:31 +01001190// Implemented by modules that are (or possibly could be) a component of a java_sdk_library
1191// (including the java_sdk_library) itself.
1192type SdkLibraryComponentDependency interface {
Ulya Trafimovich31e444e2020-08-14 17:32:16 +01001193 UsesLibraryDependency
1194
Paul Duffin3f0290e2021-06-30 18:25:36 +01001195 // SdkLibraryName returns the name of the java_sdk_library/_import module.
1196 SdkLibraryName() *string
1197
Ulya Trafimovich39b437b2020-09-23 16:42:35 +01001198 // The name of the implementation library for the optional SDK library or nil, if there isn't one.
1199 OptionalSdkLibraryImplementation() *string
Paul Duffin859fe962020-05-15 10:20:31 +01001200}
1201
1202// Make sure that all the module types that are components of java_sdk_library/_import
1203// and which can be referenced (directly or indirectly) from an android app implement
1204// the SdkLibraryComponentDependency interface.
1205var _ SdkLibraryComponentDependency = (*Library)(nil)
1206var _ SdkLibraryComponentDependency = (*Import)(nil)
1207var _ SdkLibraryComponentDependency = (*SdkLibrary)(nil)
Paul Duffineedc5d52020-06-12 17:46:39 +01001208var _ SdkLibraryComponentDependency = (*SdkLibraryImport)(nil)
Paul Duffin859fe962020-05-15 10:20:31 +01001209
Paul Duffin32cf58a2021-05-18 16:32:50 +01001210// Provides access to sdk_version related files, e.g. header and implementation jars.
Paul Duffin859fe962020-05-15 10:20:31 +01001211type SdkLibraryDependency interface {
1212 SdkLibraryComponentDependency
1213
1214 // Get the header jars appropriate for the supplied sdk_version.
1215 //
1216 // These are turbine generated jars so they only change if the externals of the
1217 // class changes but it does not contain and implementation or JavaDoc.
Jiyong Parkf1691d22021-03-29 20:11:58 +09001218 SdkHeaderJars(ctx android.BaseModuleContext, sdkVersion android.SdkSpec) android.Paths
Paul Duffin859fe962020-05-15 10:20:31 +01001219
1220 // Get the implementation jars appropriate for the supplied sdk version.
1221 //
1222 // These are either the implementation jar for the whole sdk library or the implementation
1223 // jars for the stubs. The latter should only be needed when generating JavaDoc as otherwise
1224 // they are identical to the corresponding header jars.
Jiyong Parkf1691d22021-03-29 20:11:58 +09001225 SdkImplementationJars(ctx android.BaseModuleContext, sdkVersion android.SdkSpec) android.Paths
Paul Duffin1267d872021-04-16 17:21:36 +01001226
1227 // SdkApiStubDexJar returns the dex jar for the stubs. It is needed by the hiddenapi processing
1228 // tool which processes dex files.
Martin Stjernholm8be1e6d2021-09-15 03:34:04 +01001229 SdkApiStubDexJar(ctx android.BaseModuleContext, kind android.SdkKind) OptionalDexJarPath
Paul Duffinf4600f62021-05-13 22:34:45 +01001230
Paul Duffin32cf58a2021-05-18 16:32:50 +01001231 // SdkRemovedTxtFile returns the optional path to the removed.txt file for the specified sdk kind.
1232 SdkRemovedTxtFile(ctx android.BaseModuleContext, kind android.SdkKind) android.OptionalPath
1233
Paul Duffinf4600f62021-05-13 22:34:45 +01001234 // sharedLibrary returns true if this can be used as a shared library.
1235 sharedLibrary() bool
Paul Duffin859fe962020-05-15 10:20:31 +01001236}
1237
Inseob Kimc0907f12019-02-08 21:00:45 +09001238type SdkLibrary struct {
Sundong Ahn054b19a2018-10-19 13:46:09 +09001239 Library
Jiyong Parkc678ad32018-04-10 13:07:10 +09001240
Sundong Ahn054b19a2018-10-19 13:46:09 +09001241 sdkLibraryProperties sdkLibraryProperties
Jiyong Parkc678ad32018-04-10 13:07:10 +09001242
Paul Duffin3375e352020-04-28 10:44:03 +01001243 // Map from api scope to the scope specific property structure.
1244 scopeToProperties map[*apiScope]*ApiScopeProperties
1245
Paul Duffin56d44902020-01-31 13:36:25 +00001246 commonToSdkLibraryAndImport
Jiyong Parkc678ad32018-04-10 13:07:10 +09001247}
1248
Inseob Kimc0907f12019-02-08 21:00:45 +09001249var _ SdkLibraryDependency = (*SdkLibrary)(nil)
Colin Cross897d2ed2019-02-11 14:03:51 -08001250
Paul Duffin3375e352020-04-28 10:44:03 +01001251func (module *SdkLibrary) generateTestAndSystemScopesByDefault() bool {
1252 return module.sdkLibraryProperties.Generate_system_and_test_apis
1253}
1254
1255func (module *SdkLibrary) getGeneratedApiScopes(ctx android.EarlyModuleContext) apiScopes {
1256 // Check to see if any scopes have been explicitly enabled. If any have then all
1257 // must be.
1258 anyScopesExplicitlyEnabled := false
1259 for _, scope := range allApiScopes {
1260 scopeProperties := module.scopeToProperties[scope]
1261 if scopeProperties.Enabled != nil {
1262 anyScopesExplicitlyEnabled = true
1263 break
1264 }
Paul Duffind1b3a922020-01-22 11:57:20 +00001265 }
Paul Duffin3375e352020-04-28 10:44:03 +01001266
1267 var generatedScopes apiScopes
1268 enabledScopes := make(map[*apiScope]struct{})
1269 for _, scope := range allApiScopes {
1270 scopeProperties := module.scopeToProperties[scope]
1271 // If any scopes are explicitly enabled then ignore the legacy enabled status.
1272 // This is to ensure that any new usages of this module type do not rely on legacy
1273 // behaviour.
1274 defaultEnabledStatus := false
1275 if anyScopesExplicitlyEnabled {
1276 defaultEnabledStatus = scope.defaultEnabledStatus
1277 } else {
1278 defaultEnabledStatus = scope.legacyEnabledStatus(module)
1279 }
1280 enabled := proptools.BoolDefault(scopeProperties.Enabled, defaultEnabledStatus)
1281 if enabled {
1282 enabledScopes[scope] = struct{}{}
1283 generatedScopes = append(generatedScopes, scope)
1284 }
1285 }
1286
1287 // Now check to make sure that any scope that is extended by an enabled scope is also
1288 // enabled.
1289 for _, scope := range allApiScopes {
1290 if _, ok := enabledScopes[scope]; ok {
1291 extends := scope.extends
1292 if extends != nil {
1293 if _, ok := enabledScopes[extends]; !ok {
1294 ctx.ModuleErrorf("enabled api scope %q depends on disabled scope %q", scope, extends)
1295 }
1296 }
1297 }
1298 }
1299
1300 return generatedScopes
Paul Duffind1b3a922020-01-22 11:57:20 +00001301}
1302
satayev758968a2021-12-06 11:42:40 +00001303var _ android.ModuleWithMinSdkVersionCheck = (*SdkLibrary)(nil)
1304
satayev8f088b02021-12-06 11:40:46 +00001305func (module *SdkLibrary) CheckMinSdkVersion(ctx android.ModuleContext) {
Spandan Das8c9ae7e2023-03-03 21:20:36 +00001306 android.CheckMinSdkVersion(ctx, module.MinSdkVersion(ctx), func(c android.ModuleContext, do android.PayloadDepsCallback) {
satayev8f088b02021-12-06 11:40:46 +00001307 ctx.WalkDeps(func(child android.Module, parent android.Module) bool {
1308 isExternal := !module.depIsInSameApex(ctx, child)
1309 if am, ok := child.(android.ApexModule); ok {
1310 if !do(ctx, parent, am, isExternal) {
1311 return false
1312 }
1313 }
1314 return !isExternal
1315 })
1316 })
1317}
1318
Paul Duffineedc5d52020-06-12 17:46:39 +01001319type sdkLibraryComponentTag struct {
1320 blueprint.BaseDependencyTag
1321 name string
1322}
1323
1324// Mark this tag so dependencies that use it are excluded from visibility enforcement.
1325func (t sdkLibraryComponentTag) ExcludeFromVisibilityEnforcement() {}
1326
1327var xmlPermissionsFileTag = sdkLibraryComponentTag{name: "xml-permissions-file"}
Paul Duffine74ac732020-02-06 13:51:46 +00001328
Jiyong Parke3833882020-02-17 17:28:10 +09001329func IsXmlPermissionsFileDepTag(depTag blueprint.DependencyTag) bool {
Paul Duffineedc5d52020-06-12 17:46:39 +01001330 if dt, ok := depTag.(sdkLibraryComponentTag); ok {
Jiyong Parke3833882020-02-17 17:28:10 +09001331 return dt == xmlPermissionsFileTag
1332 }
1333 return false
1334}
1335
Paul Duffineedc5d52020-06-12 17:46:39 +01001336var implLibraryTag = sdkLibraryComponentTag{name: "impl-library"}
Paul Duffin5df79302020-05-16 15:52:12 +01001337
Paul Duffin44f1d842020-06-26 20:17:02 +01001338// Add the dependencies on the child modules in the component deps mutator.
1339func (module *SdkLibrary) ComponentDepsMutator(ctx android.BottomUpMutatorContext) {
Paul Duffin3375e352020-04-28 10:44:03 +01001340 for _, apiScope := range module.getGeneratedApiScopes(ctx) {
Paul Duffind1b3a922020-01-22 11:57:20 +00001341 // Add dependencies to the stubs library
Spandan Das877f39d2023-03-29 16:19:51 +00001342 stubModuleName := module.stubsLibraryModuleName(apiScope)
Jihoon Kang1147b312023-06-08 23:25:57 +00001343
Spandan Das877f39d2023-03-29 16:19:51 +00001344 ctx.AddVariationDependencies(nil, apiScope.stubsTag, stubModuleName)
Paul Duffind1b3a922020-01-22 11:57:20 +00001345
Paul Duffin15f34ef2020-07-20 18:04:44 +01001346 // Add a dependency on the stubs source in order to access both stubs source and api information.
1347 ctx.AddVariationDependencies(nil, apiScope.stubsSourceAndApiTag, module.stubsSourceModuleName(apiScope))
Paul Duffin958806b2022-05-16 13:10:47 +00001348
1349 if module.compareAgainstLatestApi(apiScope) {
1350 // Add dependencies on the latest finalized version of the API .txt file.
1351 latestApiModuleName := module.latestApiModuleName(apiScope)
1352 ctx.AddDependency(module, apiScope.latestApiModuleTag, latestApiModuleName)
1353
1354 // Add dependencies on the latest finalized version of the remove API .txt file.
1355 latestRemovedApiModuleName := module.latestRemovedApiModuleName(apiScope)
1356 ctx.AddDependency(module, apiScope.latestRemovedApiModuleTag, latestRemovedApiModuleName)
1357 }
Sundong Ahn054b19a2018-10-19 13:46:09 +09001358 }
1359
Paul Duffindfa131e2020-05-15 20:37:11 +01001360 if module.requiresRuntimeImplementationLibrary() {
Paul Duffin5df79302020-05-16 15:52:12 +01001361 // Add dependency to the rule for generating the implementation library.
1362 ctx.AddDependency(module, implLibraryTag, module.implLibraryModuleName())
1363
Paul Duffindfa131e2020-05-15 20:37:11 +01001364 if module.sharedLibrary() {
1365 // Add dependency to the rule for generating the xml permissions file
Paul Duffineedc5d52020-06-12 17:46:39 +01001366 ctx.AddDependency(module, xmlPermissionsFileTag, module.xmlPermissionsModuleName())
Paul Duffindfa131e2020-05-15 20:37:11 +01001367 }
Paul Duffin44f1d842020-06-26 20:17:02 +01001368 }
1369}
Paul Duffine74ac732020-02-06 13:51:46 +00001370
Paul Duffin44f1d842020-06-26 20:17:02 +01001371// Add other dependencies as normal.
1372func (module *SdkLibrary) DepsMutator(ctx android.BottomUpMutatorContext) {
Anton Hanssone77fccc2021-01-20 16:52:41 +00001373 var missingApiModules []string
1374 for _, apiScope := range module.getGeneratedApiScopes(ctx) {
1375 if apiScope.unstable {
1376 continue
1377 }
Paul Duffin958806b2022-05-16 13:10:47 +00001378 if m := module.latestApiModuleName(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.latestRemovedApiModuleName(apiScope); !ctx.OtherModuleExists(m) {
Anton Hanssone77fccc2021-01-20 16:52:41 +00001382 missingApiModules = append(missingApiModules, m)
1383 }
Paul Duffin958806b2022-05-16 13:10:47 +00001384 if m := module.latestIncompatibilitiesModuleName(apiScope); !ctx.OtherModuleExists(m) {
Jaewoong Jung1a97ee02021-03-09 13:25:02 -08001385 missingApiModules = append(missingApiModules, m)
1386 }
Anton Hanssone77fccc2021-01-20 16:52:41 +00001387 }
1388 if len(missingApiModules) != 0 && !module.sdkLibraryProperties.Unsafe_ignore_missing_latest_api {
1389 m := module.Name() + " is missing tracking files for previously released library versions.\n"
1390 m += "You need to do one of the following:\n"
1391 m += "- Add `unsafe_ignore_missing_latest_api: true` to your blueprint (to disable compat tracking)\n"
1392 m += "- Add a set of prebuilt txt files representing the last released version of this library for compat checking.\n"
1393 m += " (the current set of API files can be used as a seed for this compatibility tracking\n"
1394 m += "\n"
1395 m += "The following filegroup modules are missing:\n "
1396 m += strings.Join(missingApiModules, "\n ") + "\n"
1397 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."
1398 ctx.ModuleErrorf(m)
1399 }
Paul Duffin44f1d842020-06-26 20:17:02 +01001400 if module.requiresRuntimeImplementationLibrary() {
Paul Duffindfa131e2020-05-15 20:37:11 +01001401 // Only add the deps for the library if it is actually going to be built.
1402 module.Library.deps(ctx)
1403 }
Jiyong Parkc678ad32018-04-10 13:07:10 +09001404}
1405
Paul Duffin46dc45a2020-05-14 15:39:10 +01001406func (module *SdkLibrary) OutputFiles(tag string) (android.Paths, error) {
1407 paths, err := module.commonOutputFiles(tag)
Colin Cross4acaea92021-12-10 23:05:02 +00001408 if paths != nil || err != nil {
Paul Duffin46dc45a2020-05-14 15:39:10 +01001409 return paths, err
1410 }
Colin Cross4acaea92021-12-10 23:05:02 +00001411 if module.requiresRuntimeImplementationLibrary() {
1412 return module.Library.OutputFiles(tag)
1413 }
1414 if tag == "" {
1415 return nil, nil
1416 }
1417 return nil, fmt.Errorf("unsupported module reference tag %q", tag)
Paul Duffin46dc45a2020-05-14 15:39:10 +01001418}
1419
Inseob Kimc0907f12019-02-08 21:00:45 +09001420func (module *SdkLibrary) GenerateAndroidBuildActions(ctx android.ModuleContext) {
satayev8f088b02021-12-06 11:40:46 +00001421 if proptools.String(module.deviceProperties.Min_sdk_version) != "" {
1422 module.CheckMinSdkVersion(ctx)
1423 }
1424
Paul Duffina2ae7e02020-09-11 11:55:00 +01001425 module.generateCommonBuildActions(ctx)
1426
Paul Duffindfa131e2020-05-15 20:37:11 +01001427 // Only build an implementation library if required.
1428 if module.requiresRuntimeImplementationLibrary() {
Paul Duffin43db9be2019-12-30 17:35:49 +00001429 module.Library.GenerateAndroidBuildActions(ctx)
1430 }
Sundong Ahn054b19a2018-10-19 13:46:09 +09001431
Paul Duffinb97b1572021-04-29 21:50:40 +01001432 // Collate the components exported by this module. All scope specific modules are exported but
1433 // the impl and xml component modules are not.
1434 exportedComponents := map[string]struct{}{}
1435
Sundong Ahn57368eb2018-07-06 11:20:23 +09001436 // Record the paths to the header jars of the library (stubs and impl).
Paul Duffind1b3a922020-01-22 11:57:20 +00001437 // When this java_sdk_library is depended upon from others via "libs" property,
Jiyong Parkc678ad32018-04-10 13:07:10 +09001438 // the recorded paths will be returned depending on the link type of the caller.
1439 ctx.VisitDirectDeps(func(to android.Module) {
Jiyong Parkc678ad32018-04-10 13:07:10 +09001440 tag := ctx.OtherModuleDependencyTag(to)
1441
Paul Duffinc8782502020-04-29 20:45:27 +01001442 // Extract information from any of the scope specific dependencies.
1443 if scopeTag, ok := tag.(scopeDependencyTag); ok {
1444 apiScope := scopeTag.apiScope
Paul Duffin803a9562020-05-20 11:52:25 +01001445 scopePaths := module.getScopePathsCreateIfNeeded(apiScope)
Paul Duffinc8782502020-04-29 20:45:27 +01001446
1447 // Extract information from the dependency. The exact information extracted
1448 // is determined by the nature of the dependency which is determined by the tag.
1449 scopeTag.extractDepInfo(ctx, to, scopePaths)
Paul Duffinb97b1572021-04-29 21:50:40 +01001450
1451 exportedComponents[ctx.OtherModuleName(to)] = struct{}{}
Sundong Ahn20e998b2018-07-24 11:19:26 +09001452 }
Jiyong Parkc678ad32018-04-10 13:07:10 +09001453 })
Paul Duffinb97b1572021-04-29 21:50:40 +01001454
1455 // Make the set of components exported by this module available for use elsewhere.
Cole Faust18994c72023-02-28 16:02:16 -08001456 exportedComponentInfo := android.ExportedComponentsInfo{Components: android.SortedKeys(exportedComponents)}
Colin Cross40213022023-12-13 15:19:49 -08001457 android.SetProvider(ctx, android.ExportedComponentsInfoProvider, exportedComponentInfo)
Paul Duffin958806b2022-05-16 13:10:47 +00001458
1459 // Provide additional information for inclusion in an sdk's generated .info file.
1460 additionalSdkInfo := map[string]interface{}{}
1461 additionalSdkInfo["dist_stem"] = module.distStem()
Paul Duffine8409952022-09-22 16:24:46 +01001462 baseModuleName := module.distStem()
Paul Duffin958806b2022-05-16 13:10:47 +00001463 scopes := map[string]interface{}{}
1464 additionalSdkInfo["scopes"] = scopes
1465 for scope, scopePaths := range module.scopePaths {
1466 scopeInfo := map[string]interface{}{}
1467 scopes[scope.name] = scopeInfo
1468 scopeInfo["current_api"] = scope.snapshotRelativeCurrentApiTxtPath(baseModuleName)
1469 scopeInfo["removed_api"] = scope.snapshotRelativeRemovedApiTxtPath(baseModuleName)
1470 if p := scopePaths.latestApiPath; p.Valid() {
1471 scopeInfo["latest_api"] = p.Path().String()
1472 }
1473 if p := scopePaths.latestRemovedApiPath; p.Valid() {
1474 scopeInfo["latest_removed_api"] = p.Path().String()
1475 }
1476 }
Colin Cross40213022023-12-13 15:19:49 -08001477 android.SetProvider(ctx, android.AdditionalSdkInfoProvider, android.AdditionalSdkInfo{additionalSdkInfo})
Jiyong Parkc678ad32018-04-10 13:07:10 +09001478}
1479
Jiyong Park0b0e1b92019-12-03 13:24:29 +09001480func (module *SdkLibrary) AndroidMkEntries() []android.AndroidMkEntries {
Paul Duffindfa131e2020-05-15 20:37:11 +01001481 if !module.requiresRuntimeImplementationLibrary() {
Paul Duffin43db9be2019-12-30 17:35:49 +00001482 return nil
1483 }
Jiyong Park0b0e1b92019-12-03 13:24:29 +09001484 entriesList := module.Library.AndroidMkEntries()
Yo Chiang07d75072020-06-05 17:43:19 +08001485 if module.sharedLibrary() {
1486 entries := &entriesList[0]
1487 entries.Required = append(entries.Required, module.xmlPermissionsModuleName())
1488 }
Jiyong Park0b0e1b92019-12-03 13:24:29 +09001489 return entriesList
Jiyong Park82484c02018-04-23 21:41:26 +09001490}
1491
Anton Hansson5fd5d242020-03-27 19:43:19 +00001492// The dist path of the stub artifacts
1493func (module *SdkLibrary) apiDistPath(apiScope *apiScope) string {
Colin Crossf0eace92021-06-02 13:02:23 -07001494 return path.Join("apistubs", module.distGroup(), apiScope.name)
Anton Hansson5fd5d242020-03-27 19:43:19 +00001495}
1496
Paul Duffin12ceb462019-12-24 20:31:31 +00001497// Get the sdk version for use when compiling the stubs library.
Paul Duffin780c5f42020-05-12 15:52:55 +01001498func (module *SdkLibrary) sdkVersionForStubsLibrary(mctx android.EarlyModuleContext, apiScope *apiScope) string {
Paul Duffin87a05a32020-05-12 11:50:28 +01001499 scopeProperties := module.scopeToProperties[apiScope]
1500 if scopeProperties.Sdk_version != nil {
1501 return proptools.String(scopeProperties.Sdk_version)
1502 }
1503
Jiyong Parkf1691d22021-03-29 20:11:58 +09001504 sdkDep := decodeSdkDep(mctx, android.SdkContext(&module.Library))
Paul Duffin12ceb462019-12-24 20:31:31 +00001505 if sdkDep.hasStandardLibs() {
1506 // If building against a standard sdk then use the sdk version appropriate for the scope.
Paul Duffind1b3a922020-01-22 11:57:20 +00001507 return apiScope.sdkVersion
Paul Duffin12ceb462019-12-24 20:31:31 +00001508 } else {
1509 // Otherwise, use no system module.
1510 return "none"
1511 }
1512}
1513
Paul Duffin31310252020-11-20 21:26:20 +00001514func (module *SdkLibrary) distStem() string {
1515 return proptools.StringDefault(module.sdkLibraryProperties.Dist_stem, module.BaseModuleName())
1516}
1517
Colin Cross986b69a2021-06-01 13:13:40 -07001518// distGroup returns the subdirectory of the dist path of the stub artifacts.
1519func (module *SdkLibrary) distGroup() string {
Colin Cross59b92bf2021-06-01 14:07:56 -07001520 return proptools.StringDefault(module.sdkLibraryProperties.Dist_group, "unknown")
Colin Cross986b69a2021-06-01 13:13:40 -07001521}
1522
Paul Duffin958806b2022-05-16 13:10:47 +00001523func latestPrebuiltApiModuleName(name string, apiScope *apiScope) string {
1524 return PrebuiltApiModuleName(name, apiScope.name, "latest")
1525}
1526
Paul Duffind1b3a922020-01-22 11:57:20 +00001527func (module *SdkLibrary) latestApiFilegroupName(apiScope *apiScope) string {
Paul Duffin958806b2022-05-16 13:10:47 +00001528 return ":" + module.latestApiModuleName(apiScope)
1529}
1530
1531func (module *SdkLibrary) latestApiModuleName(apiScope *apiScope) string {
1532 return latestPrebuiltApiModuleName(module.distStem(), apiScope)
Jiyong Park58c518b2018-05-12 22:29:12 +09001533}
Jiyong Parkc678ad32018-04-10 13:07:10 +09001534
Paul Duffind1b3a922020-01-22 11:57:20 +00001535func (module *SdkLibrary) latestRemovedApiFilegroupName(apiScope *apiScope) string {
Paul Duffin958806b2022-05-16 13:10:47 +00001536 return ":" + module.latestRemovedApiModuleName(apiScope)
1537}
1538
1539func (module *SdkLibrary) latestRemovedApiModuleName(apiScope *apiScope) string {
1540 return latestPrebuiltApiModuleName(module.distStem()+"-removed", apiScope)
Jiyong Parkc678ad32018-04-10 13:07:10 +09001541}
1542
Jaewoong Jung1a97ee02021-03-09 13:25:02 -08001543func (module *SdkLibrary) latestIncompatibilitiesFilegroupName(apiScope *apiScope) string {
Paul Duffin958806b2022-05-16 13:10:47 +00001544 return ":" + module.latestIncompatibilitiesModuleName(apiScope)
1545}
1546
1547func (module *SdkLibrary) latestIncompatibilitiesModuleName(apiScope *apiScope) string {
1548 return latestPrebuiltApiModuleName(module.distStem()+"-incompatibilities", apiScope)
Jaewoong Jung1a97ee02021-03-09 13:25:02 -08001549}
1550
Jihoon Kang1c92c3e2023-03-23 17:44:51 +00001551func (module *SdkLibrary) contributesToApiSurface(c android.Config) bool {
1552 _, exists := c.GetApiLibraries()[module.Name()]
1553 return exists
1554}
1555
Jihoon Kang0c705a42023-08-02 06:44:57 +00001556// The listed modules are the special java_sdk_libraries where apiScope.kind do not match the
1557// api surface that the module contribute to. For example, the public droidstubs and java_library
1558// do not contribute to the public api surface, but contributes to the core platform api surface.
1559// This method returns the full api surface stub lib that
1560// the generated java_api_library should depend on.
1561func (module *SdkLibrary) alternativeFullApiSurfaceStubLib() string {
1562 if val, ok := apiLibraryAdditionalProperties[module.Name()]; ok {
1563 return val.FullApiSurfaceStubLib
1564 }
1565 return ""
1566}
1567
1568// The listed modules' stubs contents do not match the corresponding txt files,
1569// but require additional api contributions to generate the full stubs.
1570// This method returns the name of the additional api contribution module
1571// for corresponding sdk_library modules.
1572func (module *SdkLibrary) apiLibraryAdditionalApiContribution() string {
1573 if val, ok := apiLibraryAdditionalProperties[module.Name()]; ok {
1574 return val.AdditionalApiContribution
1575 }
1576 return ""
1577}
1578
Anton Hansson944e77d2020-08-19 11:40:22 +01001579func childModuleVisibility(childVisibility []string) []string {
1580 if childVisibility == nil {
1581 // No child visibility set. The child will use the visibility of the sdk_library.
1582 return nil
1583 }
1584
1585 // Prepend an override to ignore the sdk_library's visibility, and rely on the child visibility.
1586 var visibility []string
1587 visibility = append(visibility, "//visibility:override")
1588 visibility = append(visibility, childVisibility...)
1589 return visibility
1590}
1591
Paul Duffin5df79302020-05-16 15:52:12 +01001592// Creates the implementation java library
1593func (module *SdkLibrary) createImplLibrary(mctx android.DefaultableHookContext) {
Anton Hansson944e77d2020-08-19 11:40:22 +01001594 visibility := childModuleVisibility(module.sdkLibraryProperties.Impl_library_visibility)
1595
Paul Duffin5df79302020-05-16 15:52:12 +01001596 props := struct {
Paul Duffin77590a82022-04-28 14:13:30 +00001597 Name *string
1598 Visibility []string
1599 Instrument bool
1600 Libs []string
1601 Static_libs []string
1602 Apex_available []string
Paul Duffin5df79302020-05-16 15:52:12 +01001603 }{
1604 Name: proptools.StringPtr(module.implLibraryModuleName()),
Anton Hansson944e77d2020-08-19 11:40:22 +01001605 Visibility: visibility,
Paul Duffin296cf332020-06-18 21:09:55 +01001606 // Set the instrument property to ensure it is instrumented when instrumentation is required.
1607 Instrument: true,
Anton Hansson7f66efa2020-10-08 14:47:23 +01001608 // Set the impl_only libs. Note that the module's "Libs" get appended as well, via the
1609 // addition of &module.properties below.
1610 Libs: module.sdkLibraryProperties.Impl_only_libs,
Paul Duffin77590a82022-04-28 14:13:30 +00001611 // Set the impl_only static libs. Note that the module's "static_libs" get appended as well, via the
1612 // addition of &module.properties below.
1613 Static_libs: module.sdkLibraryProperties.Impl_only_static_libs,
1614 // Pass the apex_available settings down so that the impl library can be statically
1615 // embedded within a library that is added to an APEX. Needed for updatable-media.
1616 Apex_available: module.ApexAvailable(),
Paul Duffin5df79302020-05-16 15:52:12 +01001617 }
1618
1619 properties := []interface{}{
1620 &module.properties,
1621 &module.protoProperties,
1622 &module.deviceProperties,
Liz Kammera7a64f32020-07-09 15:16:41 -07001623 &module.dexProperties,
Paul Duffin5df79302020-05-16 15:52:12 +01001624 &module.dexpreoptProperties,
Colin Cross014489c2020-06-02 20:09:13 -07001625 &module.linter.properties,
Paul Duffin5df79302020-05-16 15:52:12 +01001626 &props,
1627 module.sdkComponentPropertiesForChildLibrary(),
1628 }
1629 mctx.CreateModule(LibraryFactory, properties...)
1630}
1631
Jiyong Parkc678ad32018-04-10 13:07:10 +09001632// Creates a static java library that has API stubs
Paul Duffinf0229202020-04-29 16:47:28 +01001633func (module *SdkLibrary) createStubsLibrary(mctx android.DefaultableHookContext, apiScope *apiScope) {
Jiyong Parkc678ad32018-04-10 13:07:10 +09001634 props := struct {
Dan Willemsen9f435972020-05-28 15:28:00 -07001635 Name *string
1636 Visibility []string
1637 Srcs []string
1638 Installable *bool
1639 Sdk_version *string
1640 System_modules *string
1641 Patch_module *string
1642 Libs []string
Anton Hanssondae54cd2021-04-21 16:30:10 +01001643 Static_libs []string
Dan Willemsen9f435972020-05-28 15:28:00 -07001644 Compile_dex *bool
1645 Java_version *string
1646 Openjdk9 struct {
Sundong Ahn054b19a2018-10-19 13:46:09 +09001647 Srcs []string
1648 Javacflags []string
1649 }
Anton Hansson5fd5d242020-03-27 19:43:19 +00001650 Dist struct {
1651 Targets []string
1652 Dest *string
1653 Dir *string
1654 Tag *string
1655 }
Jiyong Parkc678ad32018-04-10 13:07:10 +09001656 }{}
1657
Jihoon Kang1147b312023-06-08 23:25:57 +00001658 props.Name = proptools.StringPtr(module.sourceStubLibraryModuleName(apiScope))
Jihoon Kang786df932023-09-07 01:18:31 +00001659 props.Visibility = []string{"//visibility:override", "//visibility:private"}
Jiyong Parkc678ad32018-04-10 13:07:10 +09001660 // sources are generated from the droiddoc
Paul Duffinc3091c82020-05-08 14:16:20 +01001661 props.Srcs = []string{":" + module.stubsSourceModuleName(apiScope)}
Paul Duffin12ceb462019-12-24 20:31:31 +00001662 sdkVersion := module.sdkVersionForStubsLibrary(mctx, apiScope)
Paul Duffin52d398a2019-06-11 12:31:14 +01001663 props.Sdk_version = proptools.StringPtr(sdkVersion)
Paul Duffina18abc22020-05-16 18:54:24 +01001664 props.System_modules = module.deviceProperties.System_modules
1665 props.Patch_module = module.properties.Patch_module
Paul Duffin367ab912019-12-23 19:40:36 +00001666 props.Installable = proptools.BoolPtr(false)
Sundong Ahn054b19a2018-10-19 13:46:09 +09001667 props.Libs = module.sdkLibraryProperties.Stub_only_libs
Mark White9421c4c2023-08-10 00:07:03 +00001668 props.Libs = append(props.Libs, module.scopeToProperties[apiScope].Libs...)
Anton Hanssondae54cd2021-04-21 16:30:10 +01001669 props.Static_libs = module.sdkLibraryProperties.Stub_only_static_libs
Paul Duffine22c2ab2020-05-20 19:35:27 +01001670 // The stub-annotations library contains special versions of the annotations
1671 // with CLASS retention policy, so that they're kept.
1672 if proptools.Bool(module.sdkLibraryProperties.Annotations_enabled) {
1673 props.Libs = append(props.Libs, "stub-annotations")
1674 }
Paul Duffina18abc22020-05-16 18:54:24 +01001675 props.Openjdk9.Srcs = module.properties.Openjdk9.Srcs
1676 props.Openjdk9.Javacflags = module.properties.Openjdk9.Javacflags
Anton Hansson83509b52020-05-21 09:21:57 +01001677 // We compile the stubs for 1.8 in line with the main android.jar stubs, and potential
1678 // interop with older developer tools that don't support 1.9.
1679 props.Java_version = proptools.StringPtr("1.8")
Paul Duffinf4600f62021-05-13 22:34:45 +01001680
Paul Duffin859fe962020-05-15 10:20:31 +01001681 mctx.CreateModule(LibraryFactory, &props, module.sdkComponentPropertiesForChildLibrary())
Jiyong Parkc678ad32018-04-10 13:07:10 +09001682}
1683
Paul Duffin6d0886e2020-04-07 18:49:53 +01001684// Creates a droidstubs module that creates stubs source files from the given full source
Paul Duffinc8782502020-04-29 20:45:27 +01001685// files and also updates and checks the API specification files.
Paul Duffin15f34ef2020-07-20 18:04:44 +01001686func (module *SdkLibrary) createStubsSourcesAndApi(mctx android.DefaultableHookContext, apiScope *apiScope, name string, scopeSpecificDroidstubsArgs []string) {
Jiyong Parkc678ad32018-04-10 13:07:10 +09001687 props := struct {
Sundong Ahn054b19a2018-10-19 13:46:09 +09001688 Name *string
Paul Duffin4911a892020-04-29 23:35:13 +01001689 Visibility []string
Sundong Ahn054b19a2018-10-19 13:46:09 +09001690 Srcs []string
1691 Installable *bool
Paul Duffin52d398a2019-06-11 12:31:14 +01001692 Sdk_version *string
Jihoon Kang3198f3c2023-01-26 08:08:52 +00001693 Api_surface *string
Paul Duffin12ceb462019-12-24 20:31:31 +00001694 System_modules *string
Sundong Ahn054b19a2018-10-19 13:46:09 +09001695 Libs []string
Paul Duffin6877e6d2020-09-25 19:59:14 +01001696 Output_javadoc_comments *bool
Paul Duffin11512472019-02-11 15:55:17 +00001697 Arg_files []string
Sundong Ahn054b19a2018-10-19 13:46:09 +09001698 Args *string
Sundong Ahn054b19a2018-10-19 13:46:09 +09001699 Java_version *string
Paul Duffine22c2ab2020-05-20 19:35:27 +01001700 Annotations_enabled *bool
Sundong Ahn054b19a2018-10-19 13:46:09 +09001701 Merge_annotations_dirs []string
1702 Merge_inclusion_annotations_dirs []string
Paul Duffin0ff08bd2020-04-29 13:30:54 +01001703 Generate_stubs *bool
Anton Hanssone87b03d2020-12-21 15:29:34 +00001704 Previous_api *string
Jihoon Kang6592e872023-12-19 01:13:16 +00001705 Aconfig_declarations []string
Sundong Ahn054b19a2018-10-19 13:46:09 +09001706 Check_api struct {
Anton Hanssone6056152020-12-31 10:37:27 +00001707 Current ApiToCheck
1708 Last_released ApiToCheck
Paul Duffin160fe412020-05-10 19:32:20 +01001709
1710 Api_lint struct {
1711 Enabled *bool
1712 New_since *string
1713 Baseline_file *string
1714 }
Jiyong Park58c518b2018-05-12 22:29:12 +09001715 }
Sundong Ahn1b92c822018-05-29 11:35:17 +09001716 Aidl struct {
1717 Include_dirs []string
1718 Local_include_dirs []string
1719 }
Paul Duffin040e9062020-11-23 17:41:36 +00001720 Dists []android.Dist
Jiyong Parkc678ad32018-04-10 13:07:10 +09001721 }{}
1722
Paul Duffin7b78b4d2020-04-28 14:08:32 +01001723 // The stubs source processing uses the same compile time classpath when extracting the
1724 // API from the implementation library as it does when compiling it. i.e. the same
1725 // * sdk version
1726 // * system_modules
1727 // * libs (static_libs/libs)
Paul Duffin250e6192019-06-07 10:44:37 +01001728
Paul Duffin0ff08bd2020-04-29 13:30:54 +01001729 props.Name = proptools.StringPtr(name)
Anton Hansson944e77d2020-08-19 11:40:22 +01001730 props.Visibility = childModuleVisibility(module.sdkLibraryProperties.Stubs_source_visibility)
Paul Duffina18abc22020-05-16 18:54:24 +01001731 props.Srcs = append(props.Srcs, module.properties.Srcs...)
Anton Hanssonf8ea3722021-09-16 14:24:13 +01001732 props.Srcs = append(props.Srcs, module.sdkLibraryProperties.Api_srcs...)
Paul Duffina18abc22020-05-16 18:54:24 +01001733 props.Sdk_version = module.deviceProperties.Sdk_version
Jihoon Kang3198f3c2023-01-26 08:08:52 +00001734 props.Api_surface = &apiScope.name
Paul Duffina18abc22020-05-16 18:54:24 +01001735 props.System_modules = module.deviceProperties.System_modules
Jiyong Parkc678ad32018-04-10 13:07:10 +09001736 props.Installable = proptools.BoolPtr(false)
Sundong Ahne6f0b052018-06-05 16:46:14 +09001737 // A droiddoc module has only one Libs property and doesn't distinguish between
1738 // shared libs and static libs. So we need to add both of these libs to Libs property.
Paul Duffina18abc22020-05-16 18:54:24 +01001739 props.Libs = module.properties.Libs
1740 props.Libs = append(props.Libs, module.properties.Static_libs...)
Nikita Ioffed732da72022-11-21 12:38:25 +00001741 props.Libs = append(props.Libs, module.sdkLibraryProperties.Stub_only_libs...)
Mark White9421c4c2023-08-10 00:07:03 +00001742 props.Libs = append(props.Libs, module.scopeToProperties[apiScope].Libs...)
Paul Duffina18abc22020-05-16 18:54:24 +01001743 props.Aidl.Include_dirs = module.deviceProperties.Aidl.Include_dirs
1744 props.Aidl.Local_include_dirs = module.deviceProperties.Aidl.Local_include_dirs
1745 props.Java_version = module.properties.Java_version
Jiyong Parkc678ad32018-04-10 13:07:10 +09001746
Paul Duffine22c2ab2020-05-20 19:35:27 +01001747 props.Annotations_enabled = module.sdkLibraryProperties.Annotations_enabled
Sundong Ahn054b19a2018-10-19 13:46:09 +09001748 props.Merge_annotations_dirs = module.sdkLibraryProperties.Merge_annotations_dirs
1749 props.Merge_inclusion_annotations_dirs = module.sdkLibraryProperties.Merge_inclusion_annotations_dirs
Jihoon Kang6592e872023-12-19 01:13:16 +00001750 props.Aconfig_declarations = module.sdkLibraryProperties.Aconfig_declarations
Sundong Ahn054b19a2018-10-19 13:46:09 +09001751
Paul Duffin6d0886e2020-04-07 18:49:53 +01001752 droidstubsArgs := []string{}
Paul Duffin235ffff2019-12-24 10:41:30 +00001753 if len(module.sdkLibraryProperties.Api_packages) != 0 {
Paul Duffin6d0886e2020-04-07 18:49:53 +01001754 droidstubsArgs = append(droidstubsArgs, "--stub-packages "+strings.Join(module.sdkLibraryProperties.Api_packages, ":"))
Paul Duffin235ffff2019-12-24 10:41:30 +00001755 }
1756 if len(module.sdkLibraryProperties.Hidden_api_packages) != 0 {
Paul Duffin6d0886e2020-04-07 18:49:53 +01001757 droidstubsArgs = append(droidstubsArgs,
Paul Duffin235ffff2019-12-24 10:41:30 +00001758 android.JoinWithPrefix(module.sdkLibraryProperties.Hidden_api_packages, " --hide-package "))
1759 }
Paul Duffin6d0886e2020-04-07 18:49:53 +01001760 droidstubsArgs = append(droidstubsArgs, module.sdkLibraryProperties.Droiddoc_options...)
Anton Hanssonfd1c0d22023-11-02 15:18:09 +00001761 disabledWarnings := []string{"HiddenSuperclass"}
1762 if proptools.BoolDefault(module.sdkLibraryProperties.Api_lint.Legacy_errors_allowed, true) {
1763 disabledWarnings = append(disabledWarnings,
1764 "BroadcastBehavior",
1765 "DeprecationMismatch",
1766 "MissingPermission",
1767 "SdkConstant",
1768 "Todo",
1769 )
Paul Duffin235ffff2019-12-24 10:41:30 +00001770 }
Paul Duffin6d0886e2020-04-07 18:49:53 +01001771 droidstubsArgs = append(droidstubsArgs, android.JoinWithPrefix(disabledWarnings, "--hide "))
Sundong Ahnfb2721f2018-09-17 13:23:09 +09001772
Paul Duffin6877e6d2020-09-25 19:59:14 +01001773 // Output Javadoc comments for public scope.
1774 if apiScope == apiScopePublic {
1775 props.Output_javadoc_comments = proptools.BoolPtr(true)
1776 }
1777
Paul Duffin1fb487d2020-04-07 18:50:10 +01001778 // Add in scope specific arguments.
Paul Duffin0ff08bd2020-04-29 13:30:54 +01001779 droidstubsArgs = append(droidstubsArgs, scopeSpecificDroidstubsArgs...)
Paul Duffin11512472019-02-11 15:55:17 +00001780 props.Arg_files = module.sdkLibraryProperties.Droiddoc_option_files
Paul Duffin6d0886e2020-04-07 18:49:53 +01001781 props.Args = proptools.StringPtr(strings.Join(droidstubsArgs, " "))
Jiyong Parkc678ad32018-04-10 13:07:10 +09001782
Paul Duffin15f34ef2020-07-20 18:04:44 +01001783 // List of APIs identified from the provided source files are created. They are later
1784 // compared against to the not-yet-released (a.k.a current) list of APIs and to the
1785 // last-released (a.k.a numbered) list of API.
1786 currentApiFileName := apiScope.apiFilePrefix + "current.txt"
1787 removedApiFileName := apiScope.apiFilePrefix + "removed.txt"
1788 apiDir := module.getApiDir()
1789 currentApiFileName = path.Join(apiDir, currentApiFileName)
1790 removedApiFileName = path.Join(apiDir, removedApiFileName)
Jiyong Parkc678ad32018-04-10 13:07:10 +09001791
Paul Duffin15f34ef2020-07-20 18:04:44 +01001792 // check against the not-yet-release API
1793 props.Check_api.Current.Api_file = proptools.StringPtr(currentApiFileName)
1794 props.Check_api.Current.Removed_api_file = proptools.StringPtr(removedApiFileName)
Jiyong Park58c518b2018-05-12 22:29:12 +09001795
Paul Duffin958806b2022-05-16 13:10:47 +00001796 if module.compareAgainstLatestApi(apiScope) {
Paul Duffin15f34ef2020-07-20 18:04:44 +01001797 // check against the latest released API
1798 latestApiFilegroupName := proptools.StringPtr(module.latestApiFilegroupName(apiScope))
Anton Hanssone87b03d2020-12-21 15:29:34 +00001799 props.Previous_api = latestApiFilegroupName
Paul Duffin15f34ef2020-07-20 18:04:44 +01001800 props.Check_api.Last_released.Api_file = latestApiFilegroupName
1801 props.Check_api.Last_released.Removed_api_file = proptools.StringPtr(
1802 module.latestRemovedApiFilegroupName(apiScope))
Jaewoong Jung1a97ee02021-03-09 13:25:02 -08001803 props.Check_api.Last_released.Baseline_file = proptools.StringPtr(
1804 module.latestIncompatibilitiesFilegroupName(apiScope))
Paul Duffin160fe412020-05-10 19:32:20 +01001805
Paul Duffin15f34ef2020-07-20 18:04:44 +01001806 if proptools.Bool(module.sdkLibraryProperties.Api_lint.Enabled) {
1807 // Enable api lint.
1808 props.Check_api.Api_lint.Enabled = proptools.BoolPtr(true)
1809 props.Check_api.Api_lint.New_since = latestApiFilegroupName
Paul Duffin160fe412020-05-10 19:32:20 +01001810
Paul Duffin15f34ef2020-07-20 18:04:44 +01001811 // If it exists then pass a lint-baseline.txt through to droidstubs.
1812 baselinePath := path.Join(apiDir, apiScope.apiFilePrefix+"lint-baseline.txt")
1813 baselinePathRelativeToRoot := path.Join(mctx.ModuleDir(), baselinePath)
1814 paths, err := mctx.GlobWithDeps(baselinePathRelativeToRoot, nil)
1815 if err != nil {
1816 mctx.ModuleErrorf("error checking for presence of %s: %s", baselinePathRelativeToRoot, err)
1817 }
1818 if len(paths) == 1 {
1819 props.Check_api.Api_lint.Baseline_file = proptools.StringPtr(baselinePath)
1820 } else if len(paths) != 0 {
1821 mctx.ModuleErrorf("error checking for presence of %s: expected one path, found: %v", baselinePathRelativeToRoot, paths)
Paul Duffin160fe412020-05-10 19:32:20 +01001822 }
1823 }
Paul Duffin15f34ef2020-07-20 18:04:44 +01001824 }
Jiyong Park58c518b2018-05-12 22:29:12 +09001825
Paul Duffin15f34ef2020-07-20 18:04:44 +01001826 if !Bool(module.sdkLibraryProperties.No_dist) {
Paul Duffin040e9062020-11-23 17:41:36 +00001827 // Dist the api txt and removed api txt artifacts for sdk builds.
1828 distDir := proptools.StringPtr(path.Join(module.apiDistPath(apiScope), "api"))
1829 for _, p := range []struct {
1830 tag string
1831 pattern string
1832 }{
1833 {tag: ".api.txt", pattern: "%s.txt"},
1834 {tag: ".removed-api.txt", pattern: "%s-removed.txt"},
1835 } {
1836 props.Dists = append(props.Dists, android.Dist{
1837 Targets: []string{"sdk", "win_sdk"},
1838 Dir: distDir,
1839 Dest: proptools.StringPtr(fmt.Sprintf(p.pattern, module.distStem())),
1840 Tag: proptools.StringPtr(p.tag),
1841 })
1842 }
Anton Hansson5fd5d242020-03-27 19:43:19 +00001843 }
1844
Spandan Das2cc80ba2023-10-27 17:21:52 +00001845 mctx.CreateModule(DroidstubsFactory, &props, module.sdkComponentPropertiesForChildLibrary()).(*Droidstubs).CallHookIfAvailable(mctx)
Jiyong Parkc678ad32018-04-10 13:07:10 +09001846}
1847
Jihoon Kang0c705a42023-08-02 06:44:57 +00001848func (module *SdkLibrary) createApiLibrary(mctx android.DefaultableHookContext, apiScope *apiScope, alternativeFullApiSurfaceStub string) {
Jihoon Kang1c92c3e2023-03-23 17:44:51 +00001849 props := struct {
Jihoon Kangca198c22023-06-22 23:13:51 +00001850 Name *string
1851 Visibility []string
1852 Api_contributions []string
1853 Libs []string
1854 Static_libs []string
1855 Full_api_surface_stub *string
Jihoon Kang4ec24872023-10-05 17:26:09 +00001856 System_modules *string
Jihoon Kang063ec002023-06-28 01:16:23 +00001857 Enable_validation *bool
Jihoon Kang1c92c3e2023-03-23 17:44:51 +00001858 }{}
1859
1860 props.Name = proptools.StringPtr(module.apiLibraryModuleName(apiScope))
Jihoon Kang786df932023-09-07 01:18:31 +00001861 props.Visibility = []string{"//visibility:override", "//visibility:private"}
Jihoon Kang1c92c3e2023-03-23 17:44:51 +00001862
1863 apiContributions := []string{}
1864
1865 // Api surfaces are not independent of each other, but have subset relationships,
1866 // and so does the api files. To generate from-text stubs for api surfaces other than public,
1867 // all subset api domains' api_contriubtions must be added as well.
1868 scope := apiScope
1869 for scope != nil {
1870 apiContributions = append(apiContributions, module.stubsSourceModuleName(scope)+".api.contribution")
1871 scope = scope.extends
1872 }
Jihoon Kang0c705a42023-08-02 06:44:57 +00001873 if apiScope == apiScopePublic {
1874 additionalApiContribution := module.apiLibraryAdditionalApiContribution()
1875 if additionalApiContribution != "" {
1876 apiContributions = append(apiContributions, additionalApiContribution)
1877 }
1878 }
Jihoon Kang1c92c3e2023-03-23 17:44:51 +00001879
1880 props.Api_contributions = apiContributions
1881 props.Libs = module.properties.Libs
1882 props.Libs = append(props.Libs, module.sdkLibraryProperties.Stub_only_libs...)
Mark White9421c4c2023-08-10 00:07:03 +00001883 props.Libs = append(props.Libs, module.scopeToProperties[apiScope].Libs...)
Jihoon Kang1c92c3e2023-03-23 17:44:51 +00001884 props.Libs = append(props.Libs, "stub-annotations")
1885 props.Static_libs = module.sdkLibraryProperties.Stub_only_static_libs
Jihoon Kange7ee2562023-07-25 05:51:46 +00001886 props.Full_api_surface_stub = proptools.StringPtr(apiScope.kind.DefaultJavaLibraryName())
Jihoon Kang0c705a42023-08-02 06:44:57 +00001887 if alternativeFullApiSurfaceStub != "" {
1888 props.Full_api_surface_stub = proptools.StringPtr(alternativeFullApiSurfaceStub)
1889 }
Jihoon Kang1c92c3e2023-03-23 17:44:51 +00001890
1891 // android_module_lib_stubs_current.from-text only comprises api contributions from art, conscrypt and i18n.
1892 // Thus, replace with android_module_lib_stubs_current_full.from-text, which comprises every api domains.
1893 if apiScope.kind == android.SdkModule {
Jihoon Kangca198c22023-06-22 23:13:51 +00001894 props.Full_api_surface_stub = proptools.StringPtr(apiScope.kind.DefaultJavaLibraryName() + "_full.from-text")
Jihoon Kang1c92c3e2023-03-23 17:44:51 +00001895 }
1896
Jihoon Kangd30ac8a2023-10-09 18:00:17 +00001897 // java_sdk_library modules that set sdk_version as none does not depend on other api
1898 // domains. Therefore, java_api_library created from such modules should not depend on
1899 // full_api_surface_stubs but create and compile stubs by the java_api_library module
1900 // itself.
1901 if module.SdkVersion(mctx).Kind == android.SdkNone {
1902 props.Full_api_surface_stub = nil
1903 }
1904
Jihoon Kang4ec24872023-10-05 17:26:09 +00001905 props.System_modules = module.deviceProperties.System_modules
Jihoon Kang063ec002023-06-28 01:16:23 +00001906 props.Enable_validation = proptools.BoolPtr(true)
Jihoon Kang4ec24872023-10-05 17:26:09 +00001907
Spandan Das2cc80ba2023-10-27 17:21:52 +00001908 mctx.CreateModule(ApiLibraryFactory, &props, module.sdkComponentPropertiesForChildLibrary())
Jihoon Kang1c92c3e2023-03-23 17:44:51 +00001909}
1910
Jihoon Kang1147b312023-06-08 23:25:57 +00001911func (module *SdkLibrary) createTopLevelStubsLibrary(
1912 mctx android.DefaultableHookContext, apiScope *apiScope, contributesToApiSurface bool) {
1913 props := struct {
1914 Name *string
1915 Visibility []string
1916 Sdk_version *string
1917 Static_libs []string
1918 System_modules *string
1919 Dist struct {
1920 Targets []string
1921 Dest *string
1922 Dir *string
1923 Tag *string
1924 }
1925 Compile_dex *bool
1926 }{}
1927 props.Name = proptools.StringPtr(module.stubsLibraryModuleName(apiScope))
1928 props.Visibility = childModuleVisibility(module.sdkLibraryProperties.Stubs_library_visibility)
1929 sdkVersion := module.sdkVersionForStubsLibrary(mctx, apiScope)
1930 props.Sdk_version = proptools.StringPtr(sdkVersion)
1931
1932 // Add the stub compiling java_library/java_api_library as static lib based on build config
1933 staticLib := module.sourceStubLibraryModuleName(apiScope)
1934 if mctx.Config().BuildFromTextStub() && contributesToApiSurface {
1935 staticLib = module.apiLibraryModuleName(apiScope)
1936 }
1937 props.Static_libs = append(props.Static_libs, staticLib)
1938 props.System_modules = module.deviceProperties.System_modules
1939
1940 // Dist the class jar artifact for sdk builds.
1941 if !Bool(module.sdkLibraryProperties.No_dist) {
1942 props.Dist.Targets = []string{"sdk", "win_sdk"}
1943 props.Dist.Dest = proptools.StringPtr(fmt.Sprintf("%v.jar", module.distStem()))
1944 props.Dist.Dir = proptools.StringPtr(module.apiDistPath(apiScope))
1945 props.Dist.Tag = proptools.StringPtr(".jar")
1946 }
1947
1948 // The imports need to be compiled to dex if the java_sdk_library requests it.
1949 compileDex := module.dexProperties.Compile_dex
1950 if module.stubLibrariesCompiledForDex() {
1951 compileDex = proptools.BoolPtr(true)
1952 }
1953 props.Compile_dex = compileDex
1954
1955 mctx.CreateModule(LibraryFactory, &props, module.sdkComponentPropertiesForChildLibrary())
1956}
1957
Paul Duffin958806b2022-05-16 13:10:47 +00001958func (module *SdkLibrary) compareAgainstLatestApi(apiScope *apiScope) bool {
1959 return !(apiScope.unstable || module.sdkLibraryProperties.Unsafe_ignore_missing_latest_api)
1960}
1961
Paul Duffinea8f8082021-06-24 13:25:57 +01001962// Implements android.ApexModule
Jooyung Han5e9013b2020-03-10 06:23:13 +09001963func (module *SdkLibrary) DepIsInSameApex(mctx android.BaseModuleContext, dep android.Module) bool {
1964 depTag := mctx.OtherModuleDependencyTag(dep)
1965 if depTag == xmlPermissionsFileTag {
1966 return true
1967 }
1968 return module.Library.DepIsInSameApex(mctx, dep)
1969}
1970
Paul Duffinea8f8082021-06-24 13:25:57 +01001971// Implements android.ApexModule
1972func (module *SdkLibrary) UniqueApexVariations() bool {
1973 return module.uniqueApexVariations()
1974}
1975
Jihoon Kang80456fd2023-11-15 19:22:14 +00001976func (module *SdkLibrary) ContributeToApi() bool {
1977 return proptools.BoolDefault(module.sdkLibraryProperties.Contribute_to_android_api, false)
1978}
1979
Jiyong Parkc678ad32018-04-10 13:07:10 +09001980// Creates the xml file that publicizes the runtime library
Paul Duffinf0229202020-04-29 16:47:28 +01001981func (module *SdkLibrary) createXmlFile(mctx android.DefaultableHookContext) {
Spandan Das8c9ae7e2023-03-03 21:20:36 +00001982 moduleMinApiLevel := module.Library.MinSdkVersion(mctx)
Pedro Loureiroc3621422021-09-28 15:40:23 +00001983 var moduleMinApiLevelStr = moduleMinApiLevel.String()
1984 if moduleMinApiLevel == android.NoneApiLevel {
1985 moduleMinApiLevelStr = "current"
1986 }
Jiyong Parke3833882020-02-17 17:28:10 +09001987 props := struct {
Pedro Loureiroc3621422021-09-28 15:40:23 +00001988 Name *string
1989 Lib_name *string
1990 Apex_available []string
1991 On_bootclasspath_since *string
1992 On_bootclasspath_before *string
1993 Min_device_sdk *string
1994 Max_device_sdk *string
1995 Sdk_library_min_api_level *string
Jamie Garsidee570ace2023-11-27 12:07:36 +00001996 Uses_libs_dependencies []string
Jiyong Parke3833882020-02-17 17:28:10 +09001997 }{
Pedro Loureiroc3621422021-09-28 15:40:23 +00001998 Name: proptools.StringPtr(module.xmlPermissionsModuleName()),
1999 Lib_name: proptools.StringPtr(module.BaseModuleName()),
2000 Apex_available: module.ApexProperties.Apex_available,
2001 On_bootclasspath_since: module.commonSdkLibraryProperties.On_bootclasspath_since,
2002 On_bootclasspath_before: module.commonSdkLibraryProperties.On_bootclasspath_before,
2003 Min_device_sdk: module.commonSdkLibraryProperties.Min_device_sdk,
2004 Max_device_sdk: module.commonSdkLibraryProperties.Max_device_sdk,
2005 Sdk_library_min_api_level: &moduleMinApiLevelStr,
Jamie Garsidee570ace2023-11-27 12:07:36 +00002006 Uses_libs_dependencies: module.usesLibraryProperties.Uses_libs,
Jiyong Parkc678ad32018-04-10 13:07:10 +09002007 }
Jiyong Parke3833882020-02-17 17:28:10 +09002008
Jiyong Parke3833882020-02-17 17:28:10 +09002009 mctx.CreateModule(sdkLibraryXmlFactory, &props)
Jiyong Parkc678ad32018-04-10 13:07:10 +09002010}
2011
Jiyong Parkf1691d22021-03-29 20:11:58 +09002012func PrebuiltJars(ctx android.BaseModuleContext, baseName string, s android.SdkSpec) android.Paths {
Jiyong Park54105c42021-03-31 18:17:53 +09002013 var ver android.ApiLevel
Jiyong Parkf1691d22021-03-29 20:11:58 +09002014 var kind android.SdkKind
2015 if s.UsePrebuilt(ctx) {
Jiyong Park54105c42021-03-31 18:17:53 +09002016 ver = s.ApiLevel
Jiyong Parkf1691d22021-03-29 20:11:58 +09002017 kind = s.Kind
Jiyong Parkc678ad32018-04-10 13:07:10 +09002018 } else {
Jiyong Park6a927c42020-01-21 02:03:43 +09002019 // We don't have prebuilt SDK for the specific sdkVersion.
2020 // Instead of breaking the build, fallback to use "system_current"
Jiyong Park54105c42021-03-31 18:17:53 +09002021 ver = android.FutureApiLevel
Jiyong Parkf1691d22021-03-29 20:11:58 +09002022 kind = android.SdkSystem
Sundong Ahn054b19a2018-10-19 13:46:09 +09002023 }
Jiyong Park6a927c42020-01-21 02:03:43 +09002024
2025 dir := filepath.Join("prebuilts", "sdk", ver.String(), kind.String())
Paul Duffin50061512020-01-21 16:31:05 +00002026 jar := filepath.Join(dir, baseName+".jar")
Sundong Ahn054b19a2018-10-19 13:46:09 +09002027 jarPath := android.ExistentPathForSource(ctx, jar)
Sundong Ahnae418ac2019-02-28 15:01:28 +09002028 if !jarPath.Valid() {
Colin Cross07c88562020-01-07 09:34:44 -08002029 if ctx.Config().AllowMissingDependencies() {
2030 return android.Paths{android.PathForSource(ctx, jar)}
2031 } else {
Jiyong Parkf1691d22021-03-29 20:11:58 +09002032 ctx.PropertyErrorf("sdk_library", "invalid sdk version %q, %q does not exist", s.Raw, jar)
Colin Cross07c88562020-01-07 09:34:44 -08002033 }
Sundong Ahnae418ac2019-02-28 15:01:28 +09002034 return nil
2035 }
Sundong Ahn054b19a2018-10-19 13:46:09 +09002036 return android.Paths{jarPath.Path()}
2037}
2038
Colin Crossaede88c2020-08-11 12:17:01 -07002039// 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 +01002040//
2041// If either this or the other module are on the platform then this will return
2042// false.
Colin Cross56a83212020-09-15 18:30:11 -07002043func withinSameApexesAs(ctx android.BaseModuleContext, other android.Module) bool {
Colin Crossff694a82023-12-13 15:54:49 -08002044 apexInfo, _ := android.ModuleProvider(ctx, android.ApexInfoProvider)
Colin Cross313aa542023-12-13 13:47:44 -08002045 otherApexInfo, _ := android.OtherModuleProvider(ctx, other, android.ApexInfoProvider)
Jiyong Parkab50b072021-05-12 17:13:56 +09002046 return len(otherApexInfo.InApexVariants) > 0 && reflect.DeepEqual(apexInfo.InApexVariants, otherApexInfo.InApexVariants)
Paul Duffin9b879592020-05-26 13:21:35 +01002047}
2048
Jiyong Parkf1691d22021-03-29 20:11:58 +09002049func (module *SdkLibrary) sdkJars(ctx android.BaseModuleContext, sdkVersion android.SdkSpec, headerJars bool) android.Paths {
Jiyong Park932cdfe2020-05-28 00:19:53 +09002050 // If the client doesn't set sdk_version, but if this library prefers stubs over
2051 // the impl library, let's provide the widest API surface possible. To do so,
2052 // force override sdk_version to module_current so that the closest possible API
2053 // surface could be found in selectHeaderJarsForSdkVersion
Jiyong Parkf1691d22021-03-29 20:11:58 +09002054 if module.defaultsToStubs() && !sdkVersion.Specified() {
Jiyong Park92315372021-04-02 08:45:46 +09002055 sdkVersion = android.SdkSpecFrom(ctx, "module_current")
Jiyong Park932cdfe2020-05-28 00:19:53 +09002056 }
Paul Duffind1b3a922020-01-22 11:57:20 +00002057
Paul Duffindaaa3322020-05-26 18:13:57 +01002058 // Only provide access to the implementation library if it is actually built.
2059 if module.requiresRuntimeImplementationLibrary() {
2060 // Check any special cases for java_sdk_library.
2061 //
2062 // Only allow access to the implementation library in the following condition:
2063 // * No sdk_version specified on the referencing module.
Paul Duffin9b879592020-05-26 13:21:35 +01002064 // * The referencing module is in the same apex as this.
Jiyong Parkf1691d22021-03-29 20:11:58 +09002065 if sdkVersion.Kind == android.SdkPrivate || withinSameApexesAs(ctx, module) {
Paul Duffindaaa3322020-05-26 18:13:57 +01002066 if headerJars {
2067 return module.HeaderJars()
2068 } else {
2069 return module.ImplementationJars()
2070 }
Sundong Ahn054b19a2018-10-19 13:46:09 +09002071 }
Jiyong Parkc678ad32018-04-10 13:07:10 +09002072 }
Paul Duffinb05d4292020-05-20 12:19:10 +01002073
Paul Duffin23970f42020-05-20 14:20:02 +01002074 return module.selectHeaderJarsForSdkVersion(ctx, sdkVersion)
Jiyong Parkc678ad32018-04-10 13:07:10 +09002075}
2076
Sundong Ahn241cd372018-07-13 16:16:44 +09002077// to satisfy SdkLibraryDependency interface
Jiyong Parkf1691d22021-03-29 20:11:58 +09002078func (module *SdkLibrary) SdkHeaderJars(ctx android.BaseModuleContext, sdkVersion android.SdkSpec) android.Paths {
Paul Duffind1b3a922020-01-22 11:57:20 +00002079 return module.sdkJars(ctx, sdkVersion, true /*headerJars*/)
2080}
2081
2082// to satisfy SdkLibraryDependency interface
Jiyong Parkf1691d22021-03-29 20:11:58 +09002083func (module *SdkLibrary) SdkImplementationJars(ctx android.BaseModuleContext, sdkVersion android.SdkSpec) android.Paths {
Paul Duffind1b3a922020-01-22 11:57:20 +00002084 return module.sdkJars(ctx, sdkVersion, false /*headerJars*/)
Sundong Ahn241cd372018-07-13 16:16:44 +09002085}
2086
Colin Cross571cccf2019-02-04 11:22:08 -08002087var javaSdkLibrariesKey = android.NewOnceKey("javaSdkLibraries")
2088
Jiyong Park82484c02018-04-23 21:41:26 +09002089func javaSdkLibraries(config android.Config) *[]string {
Colin Cross571cccf2019-02-04 11:22:08 -08002090 return config.Once(javaSdkLibrariesKey, func() interface{} {
Jiyong Park82484c02018-04-23 21:41:26 +09002091 return &[]string{}
2092 }).(*[]string)
2093}
2094
Paul Duffin749f98f2019-12-30 17:23:46 +00002095func (module *SdkLibrary) getApiDir() string {
2096 return proptools.StringDefault(module.sdkLibraryProperties.Api_dir, "api")
2097}
2098
Jiyong Parkc678ad32018-04-10 13:07:10 +09002099// For a java_sdk_library module, create internal modules for stubs, docs,
2100// runtime libs and xml file. If requested, the stubs and docs are created twice
2101// once for public API level and once for system API level
Paul Duffinf0229202020-04-29 16:47:28 +01002102func (module *SdkLibrary) CreateInternalModules(mctx android.DefaultableHookContext) {
2103 // If the module has been disabled then don't create any child modules.
2104 if !module.Enabled() {
2105 return
2106 }
2107
Paul Duffina18abc22020-05-16 18:54:24 +01002108 if len(module.properties.Srcs) == 0 {
Inseob Kimc0907f12019-02-08 21:00:45 +09002109 mctx.PropertyErrorf("srcs", "java_sdk_library must specify srcs")
Jooyung Han58f26ab2019-12-18 15:34:32 +09002110 return
Inseob Kimc0907f12019-02-08 21:00:45 +09002111 }
2112
Paul Duffin37e0b772019-12-30 17:20:10 +00002113 // If this builds against standard libraries (i.e. is not part of the core libraries)
Paul Duffin4f5c1ef2020-11-19 14:53:43 +00002114 // then assume it provides both system and test apis.
Jiyong Parkf1691d22021-03-29 20:11:58 +09002115 sdkDep := decodeSdkDep(mctx, android.SdkContext(&module.Library))
Paul Duffin37e0b772019-12-30 17:20:10 +00002116 hasSystemAndTestApis := sdkDep.hasStandardLibs()
Paul Duffin3375e352020-04-28 10:44:03 +01002117 module.sdkLibraryProperties.Generate_system_and_test_apis = hasSystemAndTestApis
Paul Duffin4f5c1ef2020-11-19 14:53:43 +00002118
Jaewoong Jung18aefc12020-12-21 09:11:10 -08002119 missingCurrentApi := false
Inseob Kim8098faa2019-03-18 10:19:51 +09002120
Paul Duffin3375e352020-04-28 10:44:03 +01002121 generatedScopes := module.getGeneratedApiScopes(mctx)
Paul Duffind1b3a922020-01-22 11:57:20 +00002122
Paul Duffin749f98f2019-12-30 17:23:46 +00002123 apiDir := module.getApiDir()
Paul Duffin3375e352020-04-28 10:44:03 +01002124 for _, scope := range generatedScopes {
Inseob Kim8098faa2019-03-18 10:19:51 +09002125 for _, api := range []string{"current.txt", "removed.txt"} {
Paul Duffind1b3a922020-01-22 11:57:20 +00002126 path := path.Join(mctx.ModuleDir(), apiDir, scope.apiFilePrefix+api)
Inseob Kim8098faa2019-03-18 10:19:51 +09002127 p := android.ExistentPathForSource(mctx, path)
2128 if !p.Valid() {
Colin Cross18f840c2021-05-20 17:56:54 -07002129 if mctx.Config().AllowMissingDependencies() {
2130 mctx.AddMissingDependencies([]string{path})
2131 } else {
2132 mctx.ModuleErrorf("Current api file %#v doesn't exist", path)
2133 missingCurrentApi = true
2134 }
Inseob Kim8098faa2019-03-18 10:19:51 +09002135 }
2136 }
2137 }
2138
Jaewoong Jung18aefc12020-12-21 09:11:10 -08002139 if missingCurrentApi {
Inseob Kim8098faa2019-03-18 10:19:51 +09002140 script := "build/soong/scripts/gen-java-current-api-files.sh"
2141 p := android.ExistentPathForSource(mctx, script)
2142
2143 if !p.Valid() {
2144 panic(fmt.Sprintf("script file %s doesn't exist", script))
2145 }
2146
2147 mctx.ModuleErrorf("One or more current api files are missing. "+
2148 "You can update them by:\n"+
Paul Duffin37e0b772019-12-30 17:20:10 +00002149 "%s %q %s && m update-api",
Paul Duffind1b3a922020-01-22 11:57:20 +00002150 script, filepath.Join(mctx.ModuleDir(), apiDir),
Paul Duffin3375e352020-04-28 10:44:03 +01002151 strings.Join(generatedScopes.Strings(func(s *apiScope) string { return s.apiFilePrefix }), " "))
Inseob Kim8098faa2019-03-18 10:19:51 +09002152 return
2153 }
2154
Paul Duffin3375e352020-04-28 10:44:03 +01002155 for _, scope := range generatedScopes {
Paul Duffin15f34ef2020-07-20 18:04:44 +01002156 // Use the stubs source name for legacy reasons.
2157 module.createStubsSourcesAndApi(mctx, scope, module.stubsSourceModuleName(scope), scope.droidstubsArgs)
Paul Duffin0ff08bd2020-04-29 13:30:54 +01002158
Paul Duffind1b3a922020-01-22 11:57:20 +00002159 module.createStubsLibrary(mctx, scope)
Jihoon Kang1c92c3e2023-03-23 17:44:51 +00002160
Jihoon Kang0c705a42023-08-02 06:44:57 +00002161 alternativeFullApiSurfaceStubLib := ""
2162 if scope == apiScopePublic {
2163 alternativeFullApiSurfaceStubLib = module.alternativeFullApiSurfaceStubLib()
2164 }
2165 contributesToApiSurface := module.contributesToApiSurface(mctx.Config()) || alternativeFullApiSurfaceStubLib != ""
Jihoon Kang1147b312023-06-08 23:25:57 +00002166 if contributesToApiSurface {
Jihoon Kang0c705a42023-08-02 06:44:57 +00002167 module.createApiLibrary(mctx, scope, alternativeFullApiSurfaceStubLib)
Jihoon Kang1c92c3e2023-03-23 17:44:51 +00002168 }
Jihoon Kang1147b312023-06-08 23:25:57 +00002169
2170 module.createTopLevelStubsLibrary(mctx, scope, contributesToApiSurface)
Inseob Kimc0907f12019-02-08 21:00:45 +09002171 }
2172
Paul Duffindfa131e2020-05-15 20:37:11 +01002173 if module.requiresRuntimeImplementationLibrary() {
Paul Duffin5df79302020-05-16 15:52:12 +01002174 // Create child module to create an implementation library.
2175 //
2176 // This temporarily creates a second implementation library that can be explicitly
2177 // referenced.
2178 //
2179 // TODO(b/156618935) - update comment once only one implementation library is created.
2180 module.createImplLibrary(mctx)
2181
Paul Duffindfa131e2020-05-15 20:37:11 +01002182 // Only create an XML permissions file that declares the library as being usable
2183 // as a shared library if required.
2184 if module.sharedLibrary() {
2185 module.createXmlFile(mctx)
2186 }
Paul Duffin43db9be2019-12-30 17:35:49 +00002187
2188 // record java_sdk_library modules so that they are exported to make
2189 javaSdkLibraries := javaSdkLibraries(mctx.Config())
2190 javaSdkLibrariesLock.Lock()
2191 defer javaSdkLibrariesLock.Unlock()
2192 *javaSdkLibraries = append(*javaSdkLibraries, module.BaseModuleName())
2193 }
Anton Hansson7f66efa2020-10-08 14:47:23 +01002194
Paul Duffin77590a82022-04-28 14:13:30 +00002195 // 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 +01002196 module.properties.Libs = append(module.properties.Libs, module.sdkLibraryProperties.Impl_only_libs...)
Paul Duffin77590a82022-04-28 14:13:30 +00002197 module.properties.Static_libs = append(module.properties.Static_libs, module.sdkLibraryProperties.Impl_only_static_libs...)
Inseob Kimc0907f12019-02-08 21:00:45 +09002198}
2199
2200func (module *SdkLibrary) InitSdkLibraryProperties() {
Colin Crossce6734e2020-06-15 16:09:53 -07002201 module.addHostAndDeviceProperties()
2202 module.AddProperties(&module.sdkLibraryProperties)
Sundong Ahn054b19a2018-10-19 13:46:09 +09002203
Paul Duffin71b33cc2021-06-23 11:39:47 +01002204 module.initSdkLibraryComponent(module)
Paul Duffin859fe962020-05-15 10:20:31 +01002205
Paul Duffina18abc22020-05-16 18:54:24 +01002206 module.properties.Installable = proptools.BoolPtr(true)
2207 module.deviceProperties.IsSDKLibrary = true
Inseob Kimc0907f12019-02-08 21:00:45 +09002208}
Sundong Ahn054b19a2018-10-19 13:46:09 +09002209
Paul Duffindfa131e2020-05-15 20:37:11 +01002210func (module *SdkLibrary) requiresRuntimeImplementationLibrary() bool {
2211 return !proptools.Bool(module.sdkLibraryProperties.Api_only)
2212}
2213
Jiyong Park932cdfe2020-05-28 00:19:53 +09002214func (module *SdkLibrary) defaultsToStubs() bool {
2215 return proptools.Bool(module.sdkLibraryProperties.Default_to_stubs)
2216}
2217
Paul Duffin1b1e8062020-05-08 13:44:43 +01002218// Defines how to name the individual component modules the sdk library creates.
2219type sdkLibraryComponentNamingScheme interface {
2220 stubsLibraryModuleName(scope *apiScope, baseName string) string
2221
2222 stubsSourceModuleName(scope *apiScope, baseName string) string
Jihoon Kang1c92c3e2023-03-23 17:44:51 +00002223
2224 apiLibraryModuleName(scope *apiScope, baseName string) string
Jihoon Kang1147b312023-06-08 23:25:57 +00002225
2226 sourceStubLibraryModuleName(scope *apiScope, baseName string) string
Paul Duffin1b1e8062020-05-08 13:44:43 +01002227}
2228
2229type defaultNamingScheme struct {
2230}
2231
2232func (s *defaultNamingScheme) stubsLibraryModuleName(scope *apiScope, baseName string) string {
2233 return scope.stubsLibraryModuleName(baseName)
2234}
2235
2236func (s *defaultNamingScheme) stubsSourceModuleName(scope *apiScope, baseName string) string {
2237 return scope.stubsSourceModuleName(baseName)
2238}
2239
Jihoon Kang1c92c3e2023-03-23 17:44:51 +00002240func (s *defaultNamingScheme) apiLibraryModuleName(scope *apiScope, baseName string) string {
2241 return scope.apiLibraryModuleName(baseName)
2242}
2243
Jihoon Kang1147b312023-06-08 23:25:57 +00002244func (s *defaultNamingScheme) sourceStubLibraryModuleName(scope *apiScope, baseName string) string {
2245 return scope.sourceStubLibraryModuleName(baseName)
2246}
2247
Paul Duffin1b1e8062020-05-08 13:44:43 +01002248var _ sdkLibraryComponentNamingScheme = (*defaultNamingScheme)(nil)
2249
Jaewoong Jungbc15e3a2021-03-10 17:02:43 -08002250func moduleStubLinkType(name string) (stub bool, ret sdkLinkType) {
Jihoon Kang1147b312023-06-08 23:25:57 +00002251 name = strings.TrimSuffix(name, ".from-source")
2252
Anton Hansson2d0c1942020-05-25 12:20:51 +01002253 // This suffix-based approach is fragile and could potentially mis-trigger.
2254 // TODO(b/155164730): Clean this up when modules no longer reference sdk_lib stubs directly.
Anton Hansson08f476b2021-04-07 15:32:19 +01002255 if strings.HasSuffix(name, apiScopePublic.stubsLibraryModuleNameSuffix()) {
2256 if name == "hwbinder.stubs" || name == "libcore_private.stubs" {
2257 // Due to a previous bug, these modules were not considered stubs, so we retain that.
2258 return false, javaPlatform
2259 }
Anton Hansson2d0c1942020-05-25 12:20:51 +01002260 return true, javaSdk
2261 }
Anton Hansson08f476b2021-04-07 15:32:19 +01002262 if strings.HasSuffix(name, apiScopeSystem.stubsLibraryModuleNameSuffix()) {
Anton Hansson2d0c1942020-05-25 12:20:51 +01002263 return true, javaSystem
2264 }
Anton Hansson08f476b2021-04-07 15:32:19 +01002265 if strings.HasSuffix(name, apiScopeModuleLib.stubsLibraryModuleNameSuffix()) {
Anton Hansson2d0c1942020-05-25 12:20:51 +01002266 return true, javaModule
2267 }
Anton Hansson08f476b2021-04-07 15:32:19 +01002268 if strings.HasSuffix(name, apiScopeTest.stubsLibraryModuleNameSuffix()) {
Anton Hansson2d0c1942020-05-25 12:20:51 +01002269 return true, javaSystem
2270 }
Jihoon Kang1147b312023-06-08 23:25:57 +00002271 if strings.HasSuffix(name, apiScopeSystemServer.stubsLibraryModuleNameSuffix()) {
2272 return true, javaSystemServer
2273 }
Anton Hansson2d0c1942020-05-25 12:20:51 +01002274 return false, javaPlatform
2275}
2276
Jaewoong Jung4f158ee2019-07-11 10:05:35 -07002277// java_sdk_library is a special Java library that provides optional platform APIs to apps.
2278// In practice, it can be viewed as a combination of several modules: 1) stubs library that clients
2279// are linked against to, 2) droiddoc module that internally generates API stubs source files,
2280// 3) the real runtime shared library that implements the APIs, and 4) XML file for adding
2281// the runtime lib to the classpath at runtime if requested via <uses-library>.
Inseob Kimc0907f12019-02-08 21:00:45 +09002282func SdkLibraryFactory() android.Module {
2283 module := &SdkLibrary{}
Paul Duffinc3091c82020-05-08 14:16:20 +01002284
2285 // Initialize information common between source and prebuilt.
Paul Duffin71b33cc2021-06-23 11:39:47 +01002286 module.initCommon(module)
Paul Duffinc3091c82020-05-08 14:16:20 +01002287
Inseob Kimc0907f12019-02-08 21:00:45 +09002288 module.InitSdkLibraryProperties()
Jooyung Han58f26ab2019-12-18 15:34:32 +09002289 android.InitApexModule(module)
Sundong Ahn054b19a2018-10-19 13:46:09 +09002290 InitJavaModule(module, android.HostAndDeviceSupported)
Paul Duffin3375e352020-04-28 10:44:03 +01002291
2292 // Initialize the map from scope to scope specific properties.
2293 scopeToProperties := make(map[*apiScope]*ApiScopeProperties)
2294 for _, scope := range allApiScopes {
2295 scopeToProperties[scope] = scope.scopeSpecificProperties(module)
2296 }
2297 module.scopeToProperties = scopeToProperties
2298
Paul Duffin4911a892020-04-29 23:35:13 +01002299 // Add the properties containing visibility rules so that they are checked.
Paul Duffin5df79302020-05-16 15:52:12 +01002300 android.AddVisibilityProperty(module, "impl_library_visibility", &module.sdkLibraryProperties.Impl_library_visibility)
Paul Duffin4911a892020-04-29 23:35:13 +01002301 android.AddVisibilityProperty(module, "stubs_library_visibility", &module.sdkLibraryProperties.Stubs_library_visibility)
2302 android.AddVisibilityProperty(module, "stubs_source_visibility", &module.sdkLibraryProperties.Stubs_source_visibility)
2303
Paul Duffin1b1e8062020-05-08 13:44:43 +01002304 module.SetDefaultableHook(func(ctx android.DefaultableHookContext) {
Paul Duffindfa131e2020-05-15 20:37:11 +01002305 // If no implementation is required then it cannot be used as a shared library
2306 // either.
2307 if !module.requiresRuntimeImplementationLibrary() {
2308 // If shared_library has been explicitly set to true then it is incompatible
2309 // with api_only: true.
2310 if proptools.Bool(module.commonSdkLibraryProperties.Shared_library) {
2311 ctx.PropertyErrorf("api_only/shared_library", "inconsistent settings, shared_library and api_only cannot both be true")
2312 }
2313 // Set shared_library: false.
2314 module.commonSdkLibraryProperties.Shared_library = proptools.BoolPtr(false)
2315 }
2316
Paul Duffin1b1e8062020-05-08 13:44:43 +01002317 if module.initCommonAfterDefaultsApplied(ctx) {
2318 module.CreateInternalModules(ctx)
2319 }
2320 })
Jiyong Parkc678ad32018-04-10 13:07:10 +09002321 return module
2322}
Colin Cross79c7c262019-04-17 11:11:46 -07002323
2324//
2325// SDK library prebuilts
2326//
2327
Paul Duffin56d44902020-01-31 13:36:25 +00002328// Properties associated with each api scope.
2329type sdkLibraryScopeProperties struct {
Colin Cross79c7c262019-04-17 11:11:46 -07002330 Jars []string `android:"path"`
2331
2332 Sdk_version *string
2333
Colin Cross79c7c262019-04-17 11:11:46 -07002334 // List of shared java libs that this module has dependencies to
2335 Libs []string
Paul Duffin3d1248c2020-04-09 00:10:17 +01002336
Paul Duffinc8782502020-04-29 20:45:27 +01002337 // The stubs source.
Paul Duffin3d1248c2020-04-09 00:10:17 +01002338 Stub_srcs []string `android:"path"`
Paul Duffin1fd005d2020-04-09 01:08:11 +01002339
2340 // The current.txt
Paul Duffin0f8faff2020-05-20 16:18:00 +01002341 Current_api *string `android:"path"`
Paul Duffin1fd005d2020-04-09 01:08:11 +01002342
2343 // The removed.txt
Paul Duffin0f8faff2020-05-20 16:18:00 +01002344 Removed_api *string `android:"path"`
Anton Hanssond78eb762021-09-21 15:25:12 +01002345
2346 // Annotation zip
2347 Annotations *string `android:"path"`
Colin Cross79c7c262019-04-17 11:11:46 -07002348}
2349
Paul Duffin56d44902020-01-31 13:36:25 +00002350type sdkLibraryImportProperties struct {
Paul Duffinfcfd7912020-01-31 17:54:30 +00002351 // List of shared java libs, common to all scopes, that this module has
2352 // dependencies to
2353 Libs []string
Paul Duffin1267d872021-04-16 17:21:36 +01002354
2355 // If set to true, compile dex files for the stubs. Defaults to false.
2356 Compile_dex *bool
Paul Duffin869de142021-07-15 14:14:41 +01002357
2358 // If not empty, classes are restricted to the specified packages and their sub-packages.
Paul Duffin869de142021-07-15 14:14:41 +01002359 Permitted_packages []string
Paul Duffin56d44902020-01-31 13:36:25 +00002360}
2361
Paul Duffineedc5d52020-06-12 17:46:39 +01002362type SdkLibraryImport struct {
Colin Cross79c7c262019-04-17 11:11:46 -07002363 android.ModuleBase
2364 android.DefaultableModuleBase
2365 prebuilt android.Prebuilt
Paul Duffindd46f712020-02-10 13:37:10 +00002366 android.ApexModuleBase
Colin Cross79c7c262019-04-17 11:11:46 -07002367
Paul Duffin37856732021-02-26 14:24:15 +00002368 hiddenAPI
Jiakai Zhang204356f2021-09-09 08:12:46 +00002369 dexpreopter
Paul Duffin37856732021-02-26 14:24:15 +00002370
Colin Cross79c7c262019-04-17 11:11:46 -07002371 properties sdkLibraryImportProperties
2372
Paul Duffin46a26a82020-04-07 19:27:04 +01002373 // Map from api scope to the scope specific property structure.
2374 scopeProperties map[*apiScope]*sdkLibraryScopeProperties
2375
Paul Duffin56d44902020-01-31 13:36:25 +00002376 commonToSdkLibraryAndImport
Paul Duffineedc5d52020-06-12 17:46:39 +01002377
2378 // The reference to the implementation library created by the source module.
2379 // Is nil if the source module does not exist.
2380 implLibraryModule *Library
2381
2382 // The reference to the xml permissions module created by the source module.
2383 // Is nil if the source module does not exist.
2384 xmlPermissionsFileModule *sdkLibraryXml
Paul Duffin39853512021-02-26 11:09:39 +00002385
Jeongik Chad5fe8782021-07-08 01:13:11 +09002386 // Build path to the dex implementation jar obtained from the prebuilt_apex, if any.
Spandan Dasfae468e2023-12-12 23:23:53 +00002387 dexJarFile OptionalDexJarPath
2388 dexJarFileErr error
Jeongik Chad5fe8782021-07-08 01:13:11 +09002389
2390 // Expected install file path of the source module(sdk_library)
2391 // or dex implementation jar obtained from the prebuilt_apex, if any.
2392 installFile android.Path
Colin Cross79c7c262019-04-17 11:11:46 -07002393}
2394
Paul Duffineedc5d52020-06-12 17:46:39 +01002395var _ SdkLibraryDependency = (*SdkLibraryImport)(nil)
Colin Cross79c7c262019-04-17 11:11:46 -07002396
Paul Duffin46a26a82020-04-07 19:27:04 +01002397// The type of a structure that contains a field of type sdkLibraryScopeProperties
2398// for each apiscope in allApiScopes, e.g. something like:
Colin Crossd079e0b2022-08-16 10:27:33 -07002399//
2400// struct {
2401// Public sdkLibraryScopeProperties
2402// System sdkLibraryScopeProperties
2403// ...
2404// }
Paul Duffin46a26a82020-04-07 19:27:04 +01002405var allScopeStructType = createAllScopePropertiesStructType()
2406
2407// Dynamically create a structure type for each apiscope in allApiScopes.
2408func createAllScopePropertiesStructType() reflect.Type {
2409 var fields []reflect.StructField
2410 for _, apiScope := range allApiScopes {
2411 field := reflect.StructField{
2412 Name: apiScope.fieldName,
2413 Type: reflect.TypeOf(sdkLibraryScopeProperties{}),
2414 }
2415 fields = append(fields, field)
2416 }
2417
2418 return reflect.StructOf(fields)
2419}
2420
2421// Create an instance of the scope specific structure type and return a map
2422// from apiscope to a pointer to each scope specific field.
2423func createPropertiesInstance() (interface{}, map[*apiScope]*sdkLibraryScopeProperties) {
2424 allScopePropertiesPtr := reflect.New(allScopeStructType)
2425 allScopePropertiesStruct := allScopePropertiesPtr.Elem()
2426 scopeProperties := make(map[*apiScope]*sdkLibraryScopeProperties)
2427
2428 for _, apiScope := range allApiScopes {
2429 field := allScopePropertiesStruct.FieldByName(apiScope.fieldName)
2430 scopeProperties[apiScope] = field.Addr().Interface().(*sdkLibraryScopeProperties)
2431 }
2432
2433 return allScopePropertiesPtr.Interface(), scopeProperties
2434}
2435
Jaewoong Jung4f158ee2019-07-11 10:05:35 -07002436// java_sdk_library_import imports a prebuilt java_sdk_library.
Colin Cross79c7c262019-04-17 11:11:46 -07002437func sdkLibraryImportFactory() android.Module {
Paul Duffineedc5d52020-06-12 17:46:39 +01002438 module := &SdkLibraryImport{}
Colin Cross79c7c262019-04-17 11:11:46 -07002439
Paul Duffin46a26a82020-04-07 19:27:04 +01002440 allScopeProperties, scopeToProperties := createPropertiesInstance()
2441 module.scopeProperties = scopeToProperties
Jiakai Zhang9c4dc192023-02-09 00:09:24 +08002442 module.AddProperties(&module.properties, allScopeProperties, &module.importDexpreoptProperties)
Colin Cross79c7c262019-04-17 11:11:46 -07002443
Paul Duffinc3091c82020-05-08 14:16:20 +01002444 // Initialize information common between source and prebuilt.
Paul Duffin71b33cc2021-06-23 11:39:47 +01002445 module.initCommon(module)
Paul Duffinc3091c82020-05-08 14:16:20 +01002446
Paul Duffin0bdcb272020-02-06 15:24:57 +00002447 android.InitPrebuiltModule(module, &[]string{""})
Paul Duffindd46f712020-02-10 13:37:10 +00002448 android.InitApexModule(module)
Colin Cross79c7c262019-04-17 11:11:46 -07002449 InitJavaModule(module, android.HostAndDeviceSupported)
2450
Paul Duffin1b1e8062020-05-08 13:44:43 +01002451 module.SetDefaultableHook(func(mctx android.DefaultableHookContext) {
2452 if module.initCommonAfterDefaultsApplied(mctx) {
2453 module.createInternalModules(mctx)
2454 }
2455 })
Colin Cross79c7c262019-04-17 11:11:46 -07002456 return module
2457}
2458
Paul Duffin630b11e2021-07-15 13:35:26 +01002459var _ PermittedPackagesForUpdatableBootJars = (*SdkLibraryImport)(nil)
2460
2461func (module *SdkLibraryImport) PermittedPackagesForUpdatableBootJars() []string {
2462 return module.properties.Permitted_packages
2463}
2464
Paul Duffineedc5d52020-06-12 17:46:39 +01002465func (module *SdkLibraryImport) Prebuilt() *android.Prebuilt {
Colin Cross79c7c262019-04-17 11:11:46 -07002466 return &module.prebuilt
2467}
2468
Paul Duffineedc5d52020-06-12 17:46:39 +01002469func (module *SdkLibraryImport) Name() string {
Colin Cross79c7c262019-04-17 11:11:46 -07002470 return module.prebuilt.Name(module.ModuleBase.Name())
2471}
2472
Paul Duffineedc5d52020-06-12 17:46:39 +01002473func (module *SdkLibraryImport) createInternalModules(mctx android.DefaultableHookContext) {
Colin Cross79c7c262019-04-17 11:11:46 -07002474
Paul Duffin50061512020-01-21 16:31:05 +00002475 // If the build is configured to use prebuilts then force this to be preferred.
Jeongik Cha816a23a2020-07-08 01:09:23 +09002476 if mctx.Config().AlwaysUsePrebuiltSdks() {
Paul Duffin50061512020-01-21 16:31:05 +00002477 module.prebuilt.ForcePrefer()
2478 }
2479
Paul Duffin46a26a82020-04-07 19:27:04 +01002480 for apiScope, scopeProperties := range module.scopeProperties {
Paul Duffin56d44902020-01-31 13:36:25 +00002481 if len(scopeProperties.Jars) == 0 {
2482 continue
2483 }
2484
Paul Duffinbbb546b2020-04-09 00:07:11 +01002485 module.createJavaImportForStubs(mctx, apiScope, scopeProperties)
Paul Duffin3d1248c2020-04-09 00:10:17 +01002486
Paul Duffin0f8faff2020-05-20 16:18:00 +01002487 if len(scopeProperties.Stub_srcs) > 0 {
2488 module.createPrebuiltStubsSources(mctx, apiScope, scopeProperties)
2489 }
Jihoon Kang71c86832023-09-13 01:01:53 +00002490
2491 if scopeProperties.Current_api != nil {
2492 module.createPrebuiltApiContribution(mctx, apiScope, scopeProperties)
2493 }
Paul Duffin56d44902020-01-31 13:36:25 +00002494 }
Colin Cross79c7c262019-04-17 11:11:46 -07002495
2496 javaSdkLibraries := javaSdkLibraries(mctx.Config())
2497 javaSdkLibrariesLock.Lock()
2498 defer javaSdkLibrariesLock.Unlock()
2499 *javaSdkLibraries = append(*javaSdkLibraries, module.BaseModuleName())
2500}
2501
Paul Duffineedc5d52020-06-12 17:46:39 +01002502func (module *SdkLibraryImport) createJavaImportForStubs(mctx android.DefaultableHookContext, apiScope *apiScope, scopeProperties *sdkLibraryScopeProperties) {
Paul Duffinbbb546b2020-04-09 00:07:11 +01002503 // Creates a java import for the jar with ".stubs" suffix
2504 props := struct {
Paul Duffin1dbe3ca2020-05-16 09:57:59 +01002505 Name *string
2506 Sdk_version *string
2507 Libs []string
2508 Jars []string
Paul Duffin1267d872021-04-16 17:21:36 +01002509 Compile_dex *bool
Paul Duffinbf4de042022-09-27 12:41:52 +01002510
2511 android.UserSuppliedPrebuiltProperties
Paul Duffinbbb546b2020-04-09 00:07:11 +01002512 }{}
Paul Duffinc3091c82020-05-08 14:16:20 +01002513 props.Name = proptools.StringPtr(module.stubsLibraryModuleName(apiScope))
Paul Duffinbbb546b2020-04-09 00:07:11 +01002514 props.Sdk_version = scopeProperties.Sdk_version
2515 // Prepend any of the libs from the legacy public properties to the libs for each of the
2516 // scopes to avoid having to duplicate them in each scope.
2517 props.Libs = append(module.properties.Libs, scopeProperties.Libs...)
2518 props.Jars = scopeProperties.Jars
Paul Duffin1dbe3ca2020-05-16 09:57:59 +01002519
Paul Duffin38b57852020-05-13 16:08:09 +01002520 // The imports are preferred if the java_sdk_library_import is preferred.
Paul Duffinbf4de042022-09-27 12:41:52 +01002521 props.CopyUserSuppliedPropertiesFromPrebuilt(&module.prebuilt)
Paul Duffin859fe962020-05-15 10:20:31 +01002522
Paul Duffin1267d872021-04-16 17:21:36 +01002523 // The imports need to be compiled to dex if the java_sdk_library_import requests it.
Paul Duffinf4600f62021-05-13 22:34:45 +01002524 compileDex := module.properties.Compile_dex
2525 if module.stubLibrariesCompiledForDex() {
2526 compileDex = proptools.BoolPtr(true)
2527 }
2528 props.Compile_dex = compileDex
Paul Duffin1267d872021-04-16 17:21:36 +01002529
Paul Duffin859fe962020-05-15 10:20:31 +01002530 mctx.CreateModule(ImportFactory, &props, module.sdkComponentPropertiesForChildLibrary())
Paul Duffinbbb546b2020-04-09 00:07:11 +01002531}
2532
Paul Duffineedc5d52020-06-12 17:46:39 +01002533func (module *SdkLibraryImport) createPrebuiltStubsSources(mctx android.DefaultableHookContext, apiScope *apiScope, scopeProperties *sdkLibraryScopeProperties) {
Paul Duffin3d1248c2020-04-09 00:10:17 +01002534 props := struct {
Paul Duffinbf4de042022-09-27 12:41:52 +01002535 Name *string
2536 Srcs []string
2537
2538 android.UserSuppliedPrebuiltProperties
Paul Duffin3d1248c2020-04-09 00:10:17 +01002539 }{}
Paul Duffinc3091c82020-05-08 14:16:20 +01002540 props.Name = proptools.StringPtr(module.stubsSourceModuleName(apiScope))
Paul Duffin3d1248c2020-04-09 00:10:17 +01002541 props.Srcs = scopeProperties.Stub_srcs
Paul Duffin38b57852020-05-13 16:08:09 +01002542
2543 // The stubs source is preferred if the java_sdk_library_import is preferred.
Paul Duffinbf4de042022-09-27 12:41:52 +01002544 props.CopyUserSuppliedPropertiesFromPrebuilt(&module.prebuilt)
2545
Spandan Das2cc80ba2023-10-27 17:21:52 +00002546 mctx.CreateModule(PrebuiltStubsSourcesFactory, &props, module.sdkComponentPropertiesForChildLibrary())
Paul Duffin3d1248c2020-04-09 00:10:17 +01002547}
2548
Jihoon Kang71c86832023-09-13 01:01:53 +00002549func (module *SdkLibraryImport) createPrebuiltApiContribution(mctx android.DefaultableHookContext, apiScope *apiScope, scopeProperties *sdkLibraryScopeProperties) {
2550 api_file := scopeProperties.Current_api
2551 api_surface := &apiScope.name
2552
2553 props := struct {
2554 Name *string
2555 Api_surface *string
2556 Api_file *string
2557 Visibility []string
2558 }{}
2559
2560 props.Name = proptools.StringPtr(module.stubsSourceModuleName(apiScope) + ".api.contribution")
2561 props.Api_surface = api_surface
2562 props.Api_file = api_file
2563 props.Visibility = []string{"//visibility:override", "//visibility:public"}
2564
Spandan Das2cc80ba2023-10-27 17:21:52 +00002565 mctx.CreateModule(ApiContributionImportFactory, &props, module.sdkComponentPropertiesForChildLibrary())
Jihoon Kang71c86832023-09-13 01:01:53 +00002566}
2567
Paul Duffin44f1d842020-06-26 20:17:02 +01002568// Add the dependencies on the child module in the component deps mutator so that it
2569// creates references to the prebuilt and not the source modules.
2570func (module *SdkLibraryImport) ComponentDepsMutator(ctx android.BottomUpMutatorContext) {
Paul Duffin46a26a82020-04-07 19:27:04 +01002571 for apiScope, scopeProperties := range module.scopeProperties {
Paul Duffin56d44902020-01-31 13:36:25 +00002572 if len(scopeProperties.Jars) == 0 {
2573 continue
2574 }
2575
2576 // Add dependencies to the prebuilt stubs library
Paul Duffin864116c2021-04-02 10:24:13 +01002577 ctx.AddVariationDependencies(nil, apiScope.stubsTag, android.PrebuiltNameFromSource(module.stubsLibraryModuleName(apiScope)))
Paul Duffin0f8faff2020-05-20 16:18:00 +01002578
2579 if len(scopeProperties.Stub_srcs) > 0 {
2580 // Add dependencies to the prebuilt stubs source library
Paul Duffin864116c2021-04-02 10:24:13 +01002581 ctx.AddVariationDependencies(nil, apiScope.stubsSourceTag, android.PrebuiltNameFromSource(module.stubsSourceModuleName(apiScope)))
Paul Duffin0f8faff2020-05-20 16:18:00 +01002582 }
Paul Duffin56d44902020-01-31 13:36:25 +00002583 }
Paul Duffin44f1d842020-06-26 20:17:02 +01002584}
2585
2586// Add other dependencies as normal.
2587func (module *SdkLibraryImport) DepsMutator(ctx android.BottomUpMutatorContext) {
Paul Duffineedc5d52020-06-12 17:46:39 +01002588
2589 implName := module.implLibraryModuleName()
2590 if ctx.OtherModuleExists(implName) {
2591 ctx.AddVariationDependencies(nil, implLibraryTag, implName)
2592
2593 xmlPermissionsModuleName := module.xmlPermissionsModuleName()
2594 if module.sharedLibrary() && ctx.OtherModuleExists(xmlPermissionsModuleName) {
2595 // Add dependency to the rule for generating the xml permissions file
2596 ctx.AddDependency(module, xmlPermissionsFileTag, xmlPermissionsModuleName)
2597 }
2598 }
Colin Cross79c7c262019-04-17 11:11:46 -07002599}
2600
Jiyong Park45bf82e2020-12-15 22:29:02 +09002601var _ android.ApexModule = (*SdkLibraryImport)(nil)
2602
2603// Implements android.ApexModule
Paul Duffineedc5d52020-06-12 17:46:39 +01002604func (module *SdkLibraryImport) DepIsInSameApex(mctx android.BaseModuleContext, dep android.Module) bool {
2605 depTag := mctx.OtherModuleDependencyTag(dep)
2606 if depTag == xmlPermissionsFileTag {
2607 return true
2608 }
2609
2610 // None of the other dependencies of the java_sdk_library_import are in the same apex
2611 // as the one that references this module.
2612 return false
2613}
2614
Jiyong Park45bf82e2020-12-15 22:29:02 +09002615// Implements android.ApexModule
Dan Albertc8060532020-07-22 22:32:17 -07002616func (module *SdkLibraryImport) ShouldSupportSdkVersion(ctx android.BaseModuleContext,
2617 sdkVersion android.ApiLevel) error {
Jooyung Han749dc692020-04-15 11:03:39 +09002618 // we don't check prebuilt modules for sdk_version
2619 return nil
2620}
2621
Paul Duffinea8f8082021-06-24 13:25:57 +01002622// Implements android.ApexModule
2623func (module *SdkLibraryImport) UniqueApexVariations() bool {
2624 return module.uniqueApexVariations()
2625}
2626
Paul Duffin09817d62022-04-28 17:45:11 +01002627// MinSdkVersion - Implements hiddenAPIModule
Spandan Das8c9ae7e2023-03-03 21:20:36 +00002628func (module *SdkLibraryImport) MinSdkVersion(ctx android.EarlyModuleContext) android.ApiLevel {
2629 return android.NoneApiLevel
Paul Duffin09817d62022-04-28 17:45:11 +01002630}
2631
2632var _ hiddenAPIModule = (*SdkLibraryImport)(nil)
2633
Paul Duffineedc5d52020-06-12 17:46:39 +01002634func (module *SdkLibraryImport) OutputFiles(tag string) (android.Paths, error) {
Paul Duffin1e940d52022-04-29 14:21:25 +01002635 paths, err := module.commonOutputFiles(tag)
2636 if paths != nil || err != nil {
2637 return paths, err
2638 }
2639 if module.implLibraryModule != nil {
2640 return module.implLibraryModule.OutputFiles(tag)
2641 } else {
2642 return nil, nil
2643 }
Paul Duffin46dc45a2020-05-14 15:39:10 +01002644}
2645
Paul Duffineedc5d52020-06-12 17:46:39 +01002646func (module *SdkLibraryImport) GenerateAndroidBuildActions(ctx android.ModuleContext) {
Paul Duffina2ae7e02020-09-11 11:55:00 +01002647 module.generateCommonBuildActions(ctx)
2648
Jeongik Chad5fe8782021-07-08 01:13:11 +09002649 // Assume that source module(sdk_library) is installed in /<sdk_library partition>/framework
2650 module.installFile = android.PathForModuleInstall(ctx, "framework", module.Stem()+".jar")
2651
Paul Duffin0f8faff2020-05-20 16:18:00 +01002652 // Record the paths to the prebuilt stubs library and stubs source.
Colin Cross79c7c262019-04-17 11:11:46 -07002653 ctx.VisitDirectDeps(func(to android.Module) {
2654 tag := ctx.OtherModuleDependencyTag(to)
2655
Paul Duffin0f8faff2020-05-20 16:18:00 +01002656 // Extract information from any of the scope specific dependencies.
2657 if scopeTag, ok := tag.(scopeDependencyTag); ok {
2658 apiScope := scopeTag.apiScope
2659 scopePaths := module.getScopePathsCreateIfNeeded(apiScope)
2660
2661 // Extract information from the dependency. The exact information extracted
2662 // is determined by the nature of the dependency which is determined by the tag.
2663 scopeTag.extractDepInfo(ctx, to, scopePaths)
Paul Duffineedc5d52020-06-12 17:46:39 +01002664 } else if tag == implLibraryTag {
2665 if implLibrary, ok := to.(*Library); ok {
2666 module.implLibraryModule = implLibrary
2667 } else {
2668 ctx.ModuleErrorf("implementation library must be of type *java.Library but was %T", to)
2669 }
2670 } else if tag == xmlPermissionsFileTag {
2671 if xmlPermissionsFileModule, ok := to.(*sdkLibraryXml); ok {
2672 module.xmlPermissionsFileModule = xmlPermissionsFileModule
2673 } else {
2674 ctx.ModuleErrorf("xml permissions file module must be of type *sdkLibraryXml but was %T", to)
2675 }
Colin Cross79c7c262019-04-17 11:11:46 -07002676 }
2677 })
Paul Duffin0f8faff2020-05-20 16:18:00 +01002678
2679 // Populate the scope paths with information from the properties.
2680 for apiScope, scopeProperties := range module.scopeProperties {
2681 if len(scopeProperties.Jars) == 0 {
2682 continue
2683 }
2684
2685 paths := module.getScopePathsCreateIfNeeded(apiScope)
Anton Hanssond78eb762021-09-21 15:25:12 +01002686 paths.annotationsZip = android.OptionalPathForModuleSrc(ctx, scopeProperties.Annotations)
Paul Duffin0f8faff2020-05-20 16:18:00 +01002687 paths.currentApiFilePath = android.OptionalPathForModuleSrc(ctx, scopeProperties.Current_api)
2688 paths.removedApiFilePath = android.OptionalPathForModuleSrc(ctx, scopeProperties.Removed_api)
2689 }
Paul Duffin39853512021-02-26 11:09:39 +00002690
2691 if ctx.Device() {
2692 // If this is a variant created for a prebuilt_apex then use the dex implementation jar
2693 // obtained from the associated deapexer module.
Colin Crossff694a82023-12-13 15:54:49 -08002694 ai, _ := android.ModuleProvider(ctx, android.ApexInfoProvider)
Paul Duffin39853512021-02-26 11:09:39 +00002695 if ai.ForPrebuiltApex {
Paul Duffin39853512021-02-26 11:09:39 +00002696 // Get the path of the dex implementation jar from the `deapexer` module.
Spandan Dasfae468e2023-12-12 23:23:53 +00002697 di, err := android.FindDeapexerProviderForModule(ctx)
2698 if err != nil {
2699 // An error was found, possibly due to multiple apexes in the tree that export this library
2700 // Defer the error till a client tries to call DexJarBuildPath
2701 module.dexJarFileErr = err
2702 return
Martin Stjernholm44825602021-09-17 01:44:12 +01002703 }
Spandan Das5be63332023-12-13 00:06:32 +00002704 dexJarFileApexRootRelative := ApexRootRelativePathToJavaLib(module.BaseModuleName())
Jiakai Zhang81e46812023-02-08 21:56:07 +08002705 if dexOutputPath := di.PrebuiltExportPath(dexJarFileApexRootRelative); dexOutputPath != nil {
Martin Stjernholm8be1e6d2021-09-15 03:34:04 +01002706 dexJarFile := makeDexJarPathFromPath(dexOutputPath)
2707 module.dexJarFile = dexJarFile
Jiakai Zhang204356f2021-09-09 08:12:46 +00002708 installPath := android.PathForModuleInPartitionInstall(
Jiakai Zhang81e46812023-02-08 21:56:07 +08002709 ctx, "apex", ai.ApexVariationName, dexJarFileApexRootRelative)
Jiakai Zhang204356f2021-09-09 08:12:46 +00002710 module.installFile = installPath
Martin Stjernholm8be1e6d2021-09-15 03:34:04 +01002711 module.initHiddenAPI(ctx, dexJarFile, module.findScopePaths(apiScopePublic).stubsImplPath[0], nil)
Jiakai Zhang204356f2021-09-09 08:12:46 +00002712
Jiakai Zhang204356f2021-09-09 08:12:46 +00002713 module.dexpreopter.installPath = module.dexpreopter.getInstallPath(ctx, installPath)
2714 module.dexpreopter.isSDKLibrary = true
2715 module.dexpreopter.uncompressedDex = shouldUncompressDex(ctx, &module.dexpreopter)
Jiakai Zhang81e46812023-02-08 21:56:07 +08002716
2717 if profilePath := di.PrebuiltExportPath(dexJarFileApexRootRelative + ".prof"); profilePath != nil {
2718 module.dexpreopter.inputProfilePathOnHost = profilePath
2719 }
2720
2721 // Dexpreopting.
Jiakai Zhang204356f2021-09-09 08:12:46 +00002722 module.dexpreopt(ctx, dexOutputPath)
Paul Duffin39853512021-02-26 11:09:39 +00002723 } else {
2724 // This should never happen as a variant for a prebuilt_apex is only created if the
2725 // prebuilt_apex has been configured to export the java library dex file.
Martin Stjernholm44825602021-09-17 01:44:12 +01002726 ctx.ModuleErrorf("internal error: no dex implementation jar available from prebuilt APEX %s", di.ApexModuleName())
Paul Duffin39853512021-02-26 11:09:39 +00002727 }
2728 }
2729 }
Colin Cross79c7c262019-04-17 11:11:46 -07002730}
2731
Jiyong Parkf1691d22021-03-29 20:11:58 +09002732func (module *SdkLibraryImport) sdkJars(ctx android.BaseModuleContext, sdkVersion android.SdkSpec, headerJars bool) android.Paths {
Paul Duffineedc5d52020-06-12 17:46:39 +01002733
2734 // For consistency with SdkLibrary make the implementation jar available to libraries that
2735 // are within the same APEX.
2736 implLibraryModule := module.implLibraryModule
Colin Cross56a83212020-09-15 18:30:11 -07002737 if implLibraryModule != nil && withinSameApexesAs(ctx, module) {
Paul Duffineedc5d52020-06-12 17:46:39 +01002738 if headerJars {
2739 return implLibraryModule.HeaderJars()
2740 } else {
2741 return implLibraryModule.ImplementationJars()
2742 }
2743 }
2744
Paul Duffin23970f42020-05-20 14:20:02 +01002745 return module.selectHeaderJarsForSdkVersion(ctx, sdkVersion)
Paul Duffin56d44902020-01-31 13:36:25 +00002746}
2747
Colin Cross79c7c262019-04-17 11:11:46 -07002748// to satisfy SdkLibraryDependency interface
Jiyong Parkf1691d22021-03-29 20:11:58 +09002749func (module *SdkLibraryImport) SdkHeaderJars(ctx android.BaseModuleContext, sdkVersion android.SdkSpec) android.Paths {
Colin Cross79c7c262019-04-17 11:11:46 -07002750 // This module is just a wrapper for the prebuilt stubs.
Paul Duffineedc5d52020-06-12 17:46:39 +01002751 return module.sdkJars(ctx, sdkVersion, true)
Colin Cross79c7c262019-04-17 11:11:46 -07002752}
2753
2754// to satisfy SdkLibraryDependency interface
Jiyong Parkf1691d22021-03-29 20:11:58 +09002755func (module *SdkLibraryImport) SdkImplementationJars(ctx android.BaseModuleContext, sdkVersion android.SdkSpec) android.Paths {
Colin Cross79c7c262019-04-17 11:11:46 -07002756 // This module is just a wrapper for the stubs.
Paul Duffineedc5d52020-06-12 17:46:39 +01002757 return module.sdkJars(ctx, sdkVersion, false)
2758}
2759
Ulya Trafimovichdbf31662020-12-17 12:07:54 +00002760// to satisfy UsesLibraryDependency interface
Martin Stjernholm8be1e6d2021-09-15 03:34:04 +01002761func (module *SdkLibraryImport) DexJarBuildPath() OptionalDexJarPath {
Paul Duffin39853512021-02-26 11:09:39 +00002762 // The dex implementation jar extracted from the .apex file should be used in preference to the
2763 // source.
Spandan Dasfae468e2023-12-12 23:23:53 +00002764 if module.dexJarFileErr != nil {
2765 panic(module.dexJarFileErr.Error())
2766 }
Martin Stjernholm8be1e6d2021-09-15 03:34:04 +01002767 if module.dexJarFile.IsSet() {
Paul Duffin39853512021-02-26 11:09:39 +00002768 return module.dexJarFile
2769 }
Paul Duffineedc5d52020-06-12 17:46:39 +01002770 if module.implLibraryModule == nil {
Martin Stjernholm8be1e6d2021-09-15 03:34:04 +01002771 return makeUnsetDexJarPath()
Paul Duffineedc5d52020-06-12 17:46:39 +01002772 } else {
2773 return module.implLibraryModule.DexJarBuildPath()
2774 }
2775}
2776
Ulya Trafimovichdbf31662020-12-17 12:07:54 +00002777// to satisfy UsesLibraryDependency interface
Ulya Trafimovich31e444e2020-08-14 17:32:16 +01002778func (module *SdkLibraryImport) DexJarInstallPath() android.Path {
Jeongik Chad5fe8782021-07-08 01:13:11 +09002779 return module.installFile
Ulya Trafimovich31e444e2020-08-14 17:32:16 +01002780}
2781
Ulya Trafimovichdbf31662020-12-17 12:07:54 +00002782// to satisfy UsesLibraryDependency interface
2783func (module *SdkLibraryImport) ClassLoaderContexts() dexpreopt.ClassLoaderContextMap {
2784 return nil
2785}
2786
Paul Duffineedc5d52020-06-12 17:46:39 +01002787// to satisfy apex.javaDependency interface
2788func (module *SdkLibraryImport) JacocoReportClassesFile() android.Path {
2789 if module.implLibraryModule == nil {
2790 return nil
2791 } else {
2792 return module.implLibraryModule.JacocoReportClassesFile()
2793 }
2794}
2795
2796// to satisfy apex.javaDependency interface
Colin Cross08dca382020-07-21 20:31:17 -07002797func (module *SdkLibraryImport) LintDepSets() LintDepSets {
2798 if module.implLibraryModule == nil {
2799 return LintDepSets{}
2800 } else {
2801 return module.implLibraryModule.LintDepSets()
2802 }
2803}
2804
Spandan Das17854f52022-01-14 21:19:14 +00002805func (module *SdkLibraryImport) GetStrictUpdatabilityLinting() bool {
Jaewoong Jung476b9d62021-05-10 15:30:00 -07002806 if module.implLibraryModule == nil {
2807 return false
2808 } else {
Spandan Das17854f52022-01-14 21:19:14 +00002809 return module.implLibraryModule.GetStrictUpdatabilityLinting()
Jaewoong Jung476b9d62021-05-10 15:30:00 -07002810 }
2811}
2812
Spandan Das17854f52022-01-14 21:19:14 +00002813func (module *SdkLibraryImport) SetStrictUpdatabilityLinting(strictLinting bool) {
Jaewoong Jung476b9d62021-05-10 15:30:00 -07002814 if module.implLibraryModule != nil {
Spandan Das17854f52022-01-14 21:19:14 +00002815 module.implLibraryModule.SetStrictUpdatabilityLinting(strictLinting)
Jaewoong Jung476b9d62021-05-10 15:30:00 -07002816 }
2817}
2818
Colin Cross08dca382020-07-21 20:31:17 -07002819// to satisfy apex.javaDependency interface
Paul Duffineedc5d52020-06-12 17:46:39 +01002820func (module *SdkLibraryImport) Stem() string {
2821 return module.BaseModuleName()
Colin Cross79c7c262019-04-17 11:11:46 -07002822}
Jiyong Parke3833882020-02-17 17:28:10 +09002823
Paul Duffin44b481b2020-06-17 16:59:43 +01002824var _ ApexDependency = (*SdkLibraryImport)(nil)
2825
2826// to satisfy java.ApexDependency interface
2827func (module *SdkLibraryImport) HeaderJars() android.Paths {
2828 if module.implLibraryModule == nil {
2829 return nil
2830 } else {
2831 return module.implLibraryModule.HeaderJars()
2832 }
2833}
2834
2835// to satisfy java.ApexDependency interface
2836func (module *SdkLibraryImport) ImplementationAndResourcesJars() android.Paths {
2837 if module.implLibraryModule == nil {
2838 return nil
2839 } else {
2840 return module.implLibraryModule.ImplementationAndResourcesJars()
2841 }
2842}
2843
Jiakai Zhang204356f2021-09-09 08:12:46 +00002844// to satisfy java.DexpreopterInterface interface
2845func (module *SdkLibraryImport) IsInstallable() bool {
2846 return true
2847}
2848
Paul Duffinfef55002021-06-17 14:56:05 +01002849var _ android.RequiredFilesFromPrebuiltApex = (*SdkLibraryImport)(nil)
2850
Paul Duffinb4bbf2c2021-06-17 15:59:07 +01002851func (module *SdkLibraryImport) RequiredFilesFromPrebuiltApex(ctx android.BaseModuleContext) []string {
Paul Duffinfef55002021-06-17 14:56:05 +01002852 name := module.BaseModuleName()
Jiakai Zhang81e46812023-02-08 21:56:07 +08002853 return requiredFilesFromPrebuiltApexForImport(name, &module.dexpreopter)
Paul Duffinfef55002021-06-17 14:56:05 +01002854}
2855
Jiyong Parke3833882020-02-17 17:28:10 +09002856// java_sdk_library_xml
Jiyong Parke3833882020-02-17 17:28:10 +09002857type sdkLibraryXml struct {
2858 android.ModuleBase
2859 android.DefaultableModuleBase
2860 android.ApexModuleBase
2861
2862 properties sdkLibraryXmlProperties
2863
2864 outputFilePath android.OutputPath
2865 installDirPath android.InstallPath
Colin Cross56a83212020-09-15 18:30:11 -07002866
2867 hideApexVariantFromMake bool
Jiyong Parke3833882020-02-17 17:28:10 +09002868}
2869
2870type sdkLibraryXmlProperties struct {
2871 // canonical name of the lib
2872 Lib_name *string
Pedro Loureiro9956e5e2021-09-07 17:21:59 +00002873
2874 // Signals that this shared library is part of the bootclasspath starting
2875 // on the version indicated in this attribute.
2876 //
2877 // This will make platforms at this level and above to ignore
2878 // <uses-library> tags with this library name because the library is already
2879 // available
2880 On_bootclasspath_since *string
2881
2882 // Signals that this shared library was part of the bootclasspath before
2883 // (but not including) the version indicated in this attribute.
2884 //
2885 // The system will automatically add a <uses-library> tag with this library to
2886 // apps that target any SDK less than the version indicated in this attribute.
2887 On_bootclasspath_before *string
2888
2889 // Indicates that PackageManager should ignore this shared library if the
2890 // platform is below the version indicated in this attribute.
2891 //
2892 // This means that the device won't recognise this library as installed.
2893 Min_device_sdk *string
2894
2895 // Indicates that PackageManager should ignore this shared library if the
2896 // platform is above the version indicated in this attribute.
2897 //
2898 // This means that the device won't recognise this library as installed.
2899 Max_device_sdk *string
Pedro Loureiroc3621422021-09-28 15:40:23 +00002900
2901 // The SdkLibrary's min api level as a string
2902 //
2903 // This value comes from the ApiLevel of the MinSdkVersion property.
2904 Sdk_library_min_api_level *string
Jamie Garsidee570ace2023-11-27 12:07:36 +00002905
2906 // Uses-libs dependencies that the shared library requires to work correctly.
2907 //
2908 // This will add dependency="foo:bar" to the <library> section.
2909 Uses_libs_dependencies []string
Jiyong Parke3833882020-02-17 17:28:10 +09002910}
2911
2912// java_sdk_library_xml builds the permission xml file for a java_sdk_library.
2913// Not to be used directly by users. java_sdk_library internally uses this.
2914func sdkLibraryXmlFactory() android.Module {
2915 module := &sdkLibraryXml{}
2916
2917 module.AddProperties(&module.properties)
2918
2919 android.InitApexModule(module)
2920 android.InitAndroidArchModule(module, android.DeviceSupported, android.MultilibCommon)
2921
2922 return module
2923}
2924
Colin Crossaede88c2020-08-11 12:17:01 -07002925func (module *sdkLibraryXml) UniqueApexVariations() bool {
2926 // sdkLibraryXml needs a unique variation per APEX because the generated XML file contains the path to the
2927 // mounted APEX, which contains the name of the APEX.
2928 return true
2929}
2930
Jiyong Parke3833882020-02-17 17:28:10 +09002931// from android.PrebuiltEtcModule
Jooyung Han0703fd82020-08-26 22:11:53 +09002932func (module *sdkLibraryXml) BaseDir() string {
2933 return "etc"
2934}
2935
2936// from android.PrebuiltEtcModule
Jiyong Parke3833882020-02-17 17:28:10 +09002937func (module *sdkLibraryXml) SubDir() string {
2938 return "permissions"
2939}
2940
2941// from android.PrebuiltEtcModule
2942func (module *sdkLibraryXml) OutputFile() android.OutputPath {
2943 return module.outputFilePath
2944}
2945
2946// from android.ApexModule
2947func (module *sdkLibraryXml) AvailableFor(what string) bool {
2948 return true
2949}
2950
2951func (module *sdkLibraryXml) DepsMutator(ctx android.BottomUpMutatorContext) {
2952 // do nothing
2953}
2954
Jiyong Park45bf82e2020-12-15 22:29:02 +09002955var _ android.ApexModule = (*sdkLibraryXml)(nil)
2956
2957// Implements android.ApexModule
Dan Albertc8060532020-07-22 22:32:17 -07002958func (module *sdkLibraryXml) ShouldSupportSdkVersion(ctx android.BaseModuleContext,
2959 sdkVersion android.ApiLevel) error {
Jooyung Han749dc692020-04-15 11:03:39 +09002960 // sdkLibraryXml doesn't need to be checked separately because java_sdk_library is checked
2961 return nil
2962}
2963
Jiyong Parke3833882020-02-17 17:28:10 +09002964// File path to the runtime implementation library
Colin Cross56a83212020-09-15 18:30:11 -07002965func (module *sdkLibraryXml) implPath(ctx android.ModuleContext) string {
Jiyong Parke3833882020-02-17 17:28:10 +09002966 implName := proptools.String(module.properties.Lib_name)
Colin Crossff694a82023-12-13 15:54:49 -08002967 if apexInfo, _ := android.ModuleProvider(ctx, android.ApexInfoProvider); !apexInfo.IsForPlatform() {
Colin Crosse07f2312020-08-13 11:24:56 -07002968 // TODO(b/146468504): ApexVariationName() is only a soong module name, not apex name.
Jiyong Parke3833882020-02-17 17:28:10 +09002969 // In most cases, this works fine. But when apex_name is set or override_apex is used
2970 // this can be wrong.
Colin Cross56a83212020-09-15 18:30:11 -07002971 return fmt.Sprintf("/apex/%s/javalib/%s.jar", apexInfo.ApexVariationName, implName)
Jiyong Parke3833882020-02-17 17:28:10 +09002972 }
2973 partition := "system"
2974 if module.SocSpecific() {
2975 partition = "vendor"
2976 } else if module.DeviceSpecific() {
2977 partition = "odm"
2978 } else if module.ProductSpecific() {
2979 partition = "product"
2980 } else if module.SystemExtSpecific() {
2981 partition = "system_ext"
2982 }
2983 return "/" + partition + "/framework/" + implName + ".jar"
2984}
2985
Pedro Loureiro9956e5e2021-09-07 17:21:59 +00002986func formattedOptionalSdkLevelAttribute(ctx android.ModuleContext, attrName string, value *string) string {
2987 if value == nil {
2988 return ""
2989 }
2990 apiLevel, err := android.ApiLevelFromUser(ctx, *value)
2991 if err != nil {
Pedro Loureiroba6682f2021-10-29 09:32:32 +00002992 // attributes in bp files have underscores but in the xml have dashes.
2993 ctx.PropertyErrorf(strings.ReplaceAll(attrName, "-", "_"), err.Error())
Pedro Loureiro9956e5e2021-09-07 17:21:59 +00002994 return ""
2995 }
Pedro Loureirob638c622021-12-22 15:28:05 +00002996 if apiLevel.IsCurrent() {
2997 // passing "current" would always mean a future release, never the current (or the current in
2998 // progress) which means some conditions would never be triggered.
2999 ctx.PropertyErrorf(strings.ReplaceAll(attrName, "-", "_"),
3000 `"current" is not an allowed value for this attribute`)
3001 return ""
3002 }
Pedro Loureiro48991222022-06-17 20:01:21 +00003003 // "safeValue" is safe because it translates finalized codenames to a string
3004 // with their SDK int.
3005 safeValue := apiLevel.String()
3006 return formattedOptionalAttribute(attrName, &safeValue)
Pedro Loureiro9956e5e2021-09-07 17:21:59 +00003007}
3008
3009// formats an attribute for the xml permissions file if the value is not null
3010// returns empty string otherwise
3011func formattedOptionalAttribute(attrName string, value *string) string {
3012 if value == nil {
3013 return ""
3014 }
3015 return fmt.Sprintf(` %s=\"%s\"\n`, attrName, *value)
3016}
3017
Jamie Garsidee570ace2023-11-27 12:07:36 +00003018func formattedDependenciesAttribute(dependencies []string) string {
3019 if dependencies == nil {
3020 return ""
3021 }
3022 return fmt.Sprintf(` dependency=\"%s\"\n`, strings.Join(dependencies, ":"))
3023}
3024
Pedro Loureiro9956e5e2021-09-07 17:21:59 +00003025func (module *sdkLibraryXml) permissionsContents(ctx android.ModuleContext) string {
3026 libName := proptools.String(module.properties.Lib_name)
3027 libNameAttr := formattedOptionalAttribute("name", &libName)
3028 filePath := module.implPath(ctx)
3029 filePathAttr := formattedOptionalAttribute("file", &filePath)
Pedro Loureiroba6682f2021-10-29 09:32:32 +00003030 implicitFromAttr := formattedOptionalSdkLevelAttribute(ctx, "on-bootclasspath-since", module.properties.On_bootclasspath_since)
3031 implicitUntilAttr := formattedOptionalSdkLevelAttribute(ctx, "on-bootclasspath-before", module.properties.On_bootclasspath_before)
3032 minSdkAttr := formattedOptionalSdkLevelAttribute(ctx, "min-device-sdk", module.properties.Min_device_sdk)
3033 maxSdkAttr := formattedOptionalSdkLevelAttribute(ctx, "max-device-sdk", module.properties.Max_device_sdk)
Jamie Garsidee570ace2023-11-27 12:07:36 +00003034 dependenciesAttr := formattedDependenciesAttribute(module.properties.Uses_libs_dependencies)
Pedro Loureiro196d3e62021-12-22 19:53:01 +00003035 // <library> is understood in all android versions whereas <apex-library> is only understood from API T (and ignored before that).
3036 // 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 +00003037 var libraryTag string
3038 if module.properties.Min_device_sdk != nil {
Pedro Loureiro196d3e62021-12-22 19:53:01 +00003039 libraryTag = ` <apex-library\n`
Pedro Loureiroc3621422021-09-28 15:40:23 +00003040 } else {
3041 libraryTag = ` <library\n`
3042 }
Pedro Loureiro9956e5e2021-09-07 17:21:59 +00003043
3044 return strings.Join([]string{
3045 `<?xml version=\"1.0\" encoding=\"utf-8\"?>\n`,
3046 `<!-- Copyright (C) 2018 The Android Open Source Project\n`,
3047 `\n`,
3048 ` Licensed under the Apache License, Version 2.0 (the \"License\");\n`,
3049 ` you may not use this file except in compliance with the License.\n`,
3050 ` You may obtain a copy of the License at\n`,
3051 `\n`,
3052 ` http://www.apache.org/licenses/LICENSE-2.0\n`,
3053 `\n`,
3054 ` Unless required by applicable law or agreed to in writing, software\n`,
3055 ` distributed under the License is distributed on an \"AS IS\" BASIS,\n`,
3056 ` WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\n`,
3057 ` See the License for the specific language governing permissions and\n`,
3058 ` limitations under the License.\n`,
3059 `-->\n`,
3060 `<permissions>\n`,
Pedro Loureiroc3621422021-09-28 15:40:23 +00003061 libraryTag,
Pedro Loureiro9956e5e2021-09-07 17:21:59 +00003062 libNameAttr,
3063 filePathAttr,
3064 implicitFromAttr,
3065 implicitUntilAttr,
3066 minSdkAttr,
3067 maxSdkAttr,
Jamie Garsidee570ace2023-11-27 12:07:36 +00003068 dependenciesAttr,
Pedro Loureiro9956e5e2021-09-07 17:21:59 +00003069 ` />\n`,
3070 `</permissions>\n`}, "")
3071}
3072
Jiyong Parke3833882020-02-17 17:28:10 +09003073func (module *sdkLibraryXml) GenerateAndroidBuildActions(ctx android.ModuleContext) {
Colin Crossff694a82023-12-13 15:54:49 -08003074 apexInfo, _ := android.ModuleProvider(ctx, android.ApexInfoProvider)
3075 module.hideApexVariantFromMake = !apexInfo.IsForPlatform()
Colin Cross56a83212020-09-15 18:30:11 -07003076
Jiyong Parke3833882020-02-17 17:28:10 +09003077 libName := proptools.String(module.properties.Lib_name)
Pedro Loureiroc3621422021-09-28 15:40:23 +00003078 module.selfValidate(ctx)
Pedro Loureiro9956e5e2021-09-07 17:21:59 +00003079 xmlContent := module.permissionsContents(ctx)
Jiyong Parke3833882020-02-17 17:28:10 +09003080
3081 module.outputFilePath = android.PathForModuleOut(ctx, libName+".xml").OutputPath
Colin Crossf1a035e2020-11-16 17:32:30 -08003082 rule := android.NewRuleBuilder(pctx, ctx)
Jiyong Parke3833882020-02-17 17:28:10 +09003083 rule.Command().
3084 Text("/bin/bash -c \"echo -e '" + xmlContent + "'\" > ").
3085 Output(module.outputFilePath)
3086
Colin Crossf1a035e2020-11-16 17:32:30 -08003087 rule.Build("java_sdk_xml", "Permission XML")
Jiyong Parke3833882020-02-17 17:28:10 +09003088
3089 module.installDirPath = android.PathForModuleInstall(ctx, "etc", module.SubDir())
3090}
3091
3092func (module *sdkLibraryXml) AndroidMkEntries() []android.AndroidMkEntries {
Colin Cross56a83212020-09-15 18:30:11 -07003093 if module.hideApexVariantFromMake {
satayev8f088b02021-12-06 11:40:46 +00003094 return []android.AndroidMkEntries{{
Jiyong Parke3833882020-02-17 17:28:10 +09003095 Disabled: true,
3096 }}
3097 }
3098
satayev8f088b02021-12-06 11:40:46 +00003099 return []android.AndroidMkEntries{{
Jiyong Parke3833882020-02-17 17:28:10 +09003100 Class: "ETC",
3101 OutputFile: android.OptionalPathForPath(module.outputFilePath),
3102 ExtraEntries: []android.AndroidMkExtraEntriesFunc{
Colin Crossaa255532020-07-03 13:18:24 -07003103 func(ctx android.AndroidMkExtraEntriesContext, entries *android.AndroidMkEntries) {
Jiyong Parke3833882020-02-17 17:28:10 +09003104 entries.SetString("LOCAL_MODULE_TAGS", "optional")
Colin Crossc68db4b2021-11-11 18:59:15 -08003105 entries.SetString("LOCAL_MODULE_PATH", module.installDirPath.String())
Jiyong Parke3833882020-02-17 17:28:10 +09003106 entries.SetString("LOCAL_INSTALLED_MODULE_STEM", module.outputFilePath.Base())
3107 },
3108 },
3109 }}
3110}
Paul Duffindd46f712020-02-10 13:37:10 +00003111
Pedro Loureiroc3621422021-09-28 15:40:23 +00003112func (module *sdkLibraryXml) selfValidate(ctx android.ModuleContext) {
3113 module.validateAtLeastTAttributes(ctx)
3114 module.validateMinAndMaxDeviceSdk(ctx)
3115 module.validateMinMaxDeviceSdkAndModuleMinSdk(ctx)
3116 module.validateOnBootclasspathBeforeRequirements(ctx)
3117}
3118
3119func (module *sdkLibraryXml) validateAtLeastTAttributes(ctx android.ModuleContext) {
3120 t := android.ApiLevelOrPanic(ctx, "Tiramisu")
3121 module.attrAtLeastT(ctx, t, module.properties.Min_device_sdk, "min_device_sdk")
3122 module.attrAtLeastT(ctx, t, module.properties.Max_device_sdk, "max_device_sdk")
3123 module.attrAtLeastT(ctx, t, module.properties.On_bootclasspath_before, "on_bootclasspath_before")
3124 module.attrAtLeastT(ctx, t, module.properties.On_bootclasspath_since, "on_bootclasspath_since")
3125}
3126
3127func (module *sdkLibraryXml) attrAtLeastT(ctx android.ModuleContext, t android.ApiLevel, attr *string, attrName string) {
3128 if attr != nil {
3129 if level, err := android.ApiLevelFromUser(ctx, *attr); err == nil {
3130 // we will inform the user of invalid inputs when we try to write the
3131 // permissions xml file so we don't need to do it here
3132 if t.GreaterThan(level) {
3133 ctx.PropertyErrorf(attrName, "Attribute value needs to be at least T")
3134 }
3135 }
3136 }
3137}
3138
3139func (module *sdkLibraryXml) validateMinAndMaxDeviceSdk(ctx android.ModuleContext) {
3140 if module.properties.Min_device_sdk != nil && module.properties.Max_device_sdk != nil {
3141 min, minErr := android.ApiLevelFromUser(ctx, *module.properties.Min_device_sdk)
3142 max, maxErr := android.ApiLevelFromUser(ctx, *module.properties.Max_device_sdk)
3143 if minErr == nil && maxErr == nil {
3144 // we will inform the user of invalid inputs when we try to write the
3145 // permissions xml file so we don't need to do it here
3146 if min.GreaterThan(max) {
3147 ctx.ModuleErrorf("min_device_sdk can't be greater than max_device_sdk")
3148 }
3149 }
3150 }
3151}
3152
3153func (module *sdkLibraryXml) validateMinMaxDeviceSdkAndModuleMinSdk(ctx android.ModuleContext) {
3154 moduleMinApi := android.ApiLevelOrPanic(ctx, *module.properties.Sdk_library_min_api_level)
3155 if module.properties.Min_device_sdk != nil {
3156 api, err := android.ApiLevelFromUser(ctx, *module.properties.Min_device_sdk)
3157 if err == nil {
3158 if moduleMinApi.GreaterThan(api) {
3159 ctx.PropertyErrorf("min_device_sdk", "Can't be less than module's min sdk (%s)", moduleMinApi)
3160 }
3161 }
3162 }
3163 if module.properties.Max_device_sdk != nil {
3164 api, err := android.ApiLevelFromUser(ctx, *module.properties.Max_device_sdk)
3165 if err == nil {
3166 if moduleMinApi.GreaterThan(api) {
3167 ctx.PropertyErrorf("max_device_sdk", "Can't be less than module's min sdk (%s)", moduleMinApi)
3168 }
3169 }
3170 }
3171}
3172
3173func (module *sdkLibraryXml) validateOnBootclasspathBeforeRequirements(ctx android.ModuleContext) {
3174 moduleMinApi := android.ApiLevelOrPanic(ctx, *module.properties.Sdk_library_min_api_level)
3175 if module.properties.On_bootclasspath_before != nil {
3176 t := android.ApiLevelOrPanic(ctx, "Tiramisu")
3177 // if we use the attribute, then we need to do this validation
3178 if moduleMinApi.LessThan(t) {
3179 // if minAPi is < T, then we need to have min_device_sdk (which only accepts T+)
3180 if module.properties.Min_device_sdk == nil {
3181 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")
3182 }
3183 }
3184 }
3185}
3186
Paul Duffindd46f712020-02-10 13:37:10 +00003187type sdkLibrarySdkMemberType struct {
3188 android.SdkMemberTypeBase
3189}
3190
Paul Duffin296701e2021-07-14 10:29:36 +01003191func (s *sdkLibrarySdkMemberType) AddDependencies(ctx android.SdkDependencyContext, dependencyTag blueprint.DependencyTag, names []string) {
3192 ctx.AddVariationDependencies(nil, dependencyTag, names...)
Paul Duffindd46f712020-02-10 13:37:10 +00003193}
3194
3195func (s *sdkLibrarySdkMemberType) IsInstance(module android.Module) bool {
3196 _, ok := module.(*SdkLibrary)
3197 return ok
3198}
3199
3200func (s *sdkLibrarySdkMemberType) AddPrebuiltModule(ctx android.SdkMemberContext, member android.SdkMember) android.BpModule {
3201 return ctx.SnapshotBuilder().AddPrebuiltModule(member, "java_sdk_library_import")
3202}
3203
3204func (s *sdkLibrarySdkMemberType) CreateVariantPropertiesStruct() android.SdkMemberProperties {
3205 return &sdkLibrarySdkMemberProperties{}
3206}
3207
Paul Duffin976b0e52021-04-27 23:20:26 +01003208var javaSdkLibrarySdkMemberType = &sdkLibrarySdkMemberType{
3209 android.SdkMemberTypeBase{
3210 PropertyName: "java_sdk_libs",
3211 SupportsSdk: true,
3212 },
3213}
3214
Paul Duffindd46f712020-02-10 13:37:10 +00003215type sdkLibrarySdkMemberProperties struct {
3216 android.SdkMemberPropertiesBase
3217
Paul Duffine8409952022-09-22 16:24:46 +01003218 // Stem name for files in the sdk snapshot.
3219 //
3220 // This is used to construct the path names of various sdk library files in the sdk snapshot to
3221 // make sure that they match the finalized versions of those files in prebuilts/sdk.
3222 //
3223 // This property is marked as keep so that it will be kept in all instances of this struct, will
3224 // not be cleared but will be copied to common structs. That is needed because this field is used
3225 // to construct many file names for other parts of this struct and so it needs to be present in
3226 // all structs. If it was not marked as keep then it would be cleared in some structs and so would
3227 // be unavailable for generating file names if there were other properties that were still set.
3228 Stem string `sdk:"keep"`
3229
Paul Duffindd46f712020-02-10 13:37:10 +00003230 // Scope to per scope properties.
Paul Duffin106a3a42022-01-27 16:39:06 +00003231 Scopes map[*apiScope]*scopeProperties
Paul Duffindd46f712020-02-10 13:37:10 +00003232
Paul Duffin3d1248c2020-04-09 00:10:17 +01003233 // The Java stubs source files.
3234 Stub_srcs []string
Paul Duffinf7a64332020-05-13 16:54:55 +01003235
3236 // The naming scheme.
3237 Naming_scheme *string
Paul Duffind7eb1c22020-05-26 20:57:10 +01003238
3239 // True if the java_sdk_library_import is for a shared library, false
3240 // otherwise.
3241 Shared_library *bool
Paul Duffina2ae7e02020-09-11 11:55:00 +01003242
Paul Duffin1267d872021-04-16 17:21:36 +01003243 // True if the stub imports should produce dex jars.
3244 Compile_dex *bool
3245
Paul Duffina2ae7e02020-09-11 11:55:00 +01003246 // The paths to the doctag files to add to the prebuilt.
3247 Doctag_paths android.Paths
Paul Duffin869de142021-07-15 14:14:41 +01003248
3249 Permitted_packages []string
Pedro Loureiro9956e5e2021-09-07 17:21:59 +00003250
3251 // Signals that this shared library is part of the bootclasspath starting
3252 // on the version indicated in this attribute.
3253 //
3254 // This will make platforms at this level and above to ignore
3255 // <uses-library> tags with this library name because the library is already
3256 // available
3257 On_bootclasspath_since *string
3258
3259 // Signals that this shared library was part of the bootclasspath before
3260 // (but not including) the version indicated in this attribute.
3261 //
3262 // The system will automatically add a <uses-library> tag with this library to
3263 // apps that target any SDK less than the version indicated in this attribute.
3264 On_bootclasspath_before *string
3265
3266 // Indicates that PackageManager should ignore this shared library if the
3267 // platform is below the version indicated in this attribute.
3268 //
3269 // This means that the device won't recognise this library as installed.
3270 Min_device_sdk *string
3271
3272 // Indicates that PackageManager should ignore this shared library if the
3273 // platform is above the version indicated in this attribute.
3274 //
3275 // This means that the device won't recognise this library as installed.
3276 Max_device_sdk *string
Jiakai Zhang9c4dc192023-02-09 00:09:24 +08003277
3278 DexPreoptProfileGuided *bool `supported_build_releases:"UpsideDownCake+"`
Paul Duffindd46f712020-02-10 13:37:10 +00003279}
3280
3281type scopeProperties struct {
Paul Duffin1fd005d2020-04-09 01:08:11 +01003282 Jars android.Paths
3283 StubsSrcJar android.Path
3284 CurrentApiFile android.Path
3285 RemovedApiFile android.Path
Paul Duffine7babdb2022-02-10 13:06:54 +00003286 AnnotationsZip android.Path `supported_build_releases:"Tiramisu+"`
Paul Duffin1fd005d2020-04-09 01:08:11 +01003287 SdkVersion string
Paul Duffindd46f712020-02-10 13:37:10 +00003288}
3289
3290func (s *sdkLibrarySdkMemberProperties) PopulateFromVariant(ctx android.SdkMemberContext, variant android.Module) {
3291 sdk := variant.(*SdkLibrary)
3292
Paul Duffine8409952022-09-22 16:24:46 +01003293 // Copy the stem name for files in the sdk snapshot.
3294 s.Stem = sdk.distStem()
3295
Paul Duffin106a3a42022-01-27 16:39:06 +00003296 s.Scopes = make(map[*apiScope]*scopeProperties)
Paul Duffindd46f712020-02-10 13:37:10 +00003297 for _, apiScope := range allApiScopes {
Paul Duffin803a9562020-05-20 11:52:25 +01003298 paths := sdk.findScopePaths(apiScope)
3299 if paths == nil {
3300 continue
3301 }
3302
Paul Duffindd46f712020-02-10 13:37:10 +00003303 jars := paths.stubsImplPath
3304 if len(jars) > 0 {
3305 properties := scopeProperties{}
3306 properties.Jars = jars
Paul Duffin780c5f42020-05-12 15:52:55 +01003307 properties.SdkVersion = sdk.sdkVersionForStubsLibrary(ctx.SdkModuleContext(), apiScope)
Paul Duffin0f8faff2020-05-20 16:18:00 +01003308 properties.StubsSrcJar = paths.stubsSrcJar.Path()
Paul Duffin10269f12020-06-19 18:39:55 +01003309 if paths.currentApiFilePath.Valid() {
3310 properties.CurrentApiFile = paths.currentApiFilePath.Path()
3311 }
3312 if paths.removedApiFilePath.Valid() {
3313 properties.RemovedApiFile = paths.removedApiFilePath.Path()
3314 }
Anton Hanssond78eb762021-09-21 15:25:12 +01003315 // The annotations zip is only available for modules that set annotations_enabled: true.
3316 if paths.annotationsZip.Valid() {
3317 properties.AnnotationsZip = paths.annotationsZip.Path()
3318 }
Paul Duffin106a3a42022-01-27 16:39:06 +00003319 s.Scopes[apiScope] = &properties
Paul Duffindd46f712020-02-10 13:37:10 +00003320 }
3321 }
3322
Paul Duffindfa131e2020-05-15 20:37:11 +01003323 s.Naming_scheme = sdk.commonSdkLibraryProperties.Naming_scheme
Paul Duffind7eb1c22020-05-26 20:57:10 +01003324 s.Shared_library = proptools.BoolPtr(sdk.sharedLibrary())
Paul Duffin1267d872021-04-16 17:21:36 +01003325 s.Compile_dex = sdk.dexProperties.Compile_dex
Paul Duffina2ae7e02020-09-11 11:55:00 +01003326 s.Doctag_paths = sdk.doctagPaths
Paul Duffin869de142021-07-15 14:14:41 +01003327 s.Permitted_packages = sdk.PermittedPackagesForUpdatableBootJars()
Pedro Loureiro9956e5e2021-09-07 17:21:59 +00003328 s.On_bootclasspath_since = sdk.commonSdkLibraryProperties.On_bootclasspath_since
3329 s.On_bootclasspath_before = sdk.commonSdkLibraryProperties.On_bootclasspath_before
3330 s.Min_device_sdk = sdk.commonSdkLibraryProperties.Min_device_sdk
3331 s.Max_device_sdk = sdk.commonSdkLibraryProperties.Max_device_sdk
Jiakai Zhang9c4dc192023-02-09 00:09:24 +08003332
3333 if sdk.dexpreopter.dexpreoptProperties.Dex_preopt_result.Profile_guided {
3334 s.DexPreoptProfileGuided = proptools.BoolPtr(true)
3335 }
Paul Duffindd46f712020-02-10 13:37:10 +00003336}
3337
3338func (s *sdkLibrarySdkMemberProperties) AddToPropertySet(ctx android.SdkMemberContext, propertySet android.BpPropertySet) {
Paul Duffinf7a64332020-05-13 16:54:55 +01003339 if s.Naming_scheme != nil {
3340 propertySet.AddProperty("naming_scheme", proptools.String(s.Naming_scheme))
3341 }
Paul Duffind7eb1c22020-05-26 20:57:10 +01003342 if s.Shared_library != nil {
3343 propertySet.AddProperty("shared_library", *s.Shared_library)
3344 }
Paul Duffin1267d872021-04-16 17:21:36 +01003345 if s.Compile_dex != nil {
3346 propertySet.AddProperty("compile_dex", *s.Compile_dex)
3347 }
Paul Duffin869de142021-07-15 14:14:41 +01003348 if len(s.Permitted_packages) > 0 {
3349 propertySet.AddProperty("permitted_packages", s.Permitted_packages)
3350 }
Jiakai Zhang9c4dc192023-02-09 00:09:24 +08003351 dexPreoptSet := propertySet.AddPropertySet("dex_preopt")
3352 if s.DexPreoptProfileGuided != nil {
3353 dexPreoptSet.AddProperty("profile_guided", proptools.Bool(s.DexPreoptProfileGuided))
3354 }
Paul Duffinf7a64332020-05-13 16:54:55 +01003355
Paul Duffine8409952022-09-22 16:24:46 +01003356 stem := s.Stem
3357
Paul Duffindd46f712020-02-10 13:37:10 +00003358 for _, apiScope := range allApiScopes {
3359 if properties, ok := s.Scopes[apiScope]; ok {
Paul Duffin6b836ba2020-05-13 19:19:49 +01003360 scopeSet := propertySet.AddPropertySet(apiScope.propertyName)
Paul Duffindd46f712020-02-10 13:37:10 +00003361
Paul Duffin958806b2022-05-16 13:10:47 +00003362 scopeDir := apiScope.snapshotRelativeDir()
Paul Duffin3d1248c2020-04-09 00:10:17 +01003363
Paul Duffindd46f712020-02-10 13:37:10 +00003364 var jars []string
3365 for _, p := range properties.Jars {
Paul Duffine8409952022-09-22 16:24:46 +01003366 dest := filepath.Join(scopeDir, stem+"-stubs.jar")
Paul Duffindd46f712020-02-10 13:37:10 +00003367 ctx.SnapshotBuilder().CopyToSnapshot(p, dest)
3368 jars = append(jars, dest)
3369 }
3370 scopeSet.AddProperty("jars", jars)
3371
Paul Duffin22628d52021-05-12 23:13:22 +01003372 if ctx.SdkModuleContext().Config().IsEnvTrue("SOONG_SDK_SNAPSHOT_USE_SRCJAR") {
3373 // Copy the stubs source jar into the snapshot zip as is.
Paul Duffine8409952022-09-22 16:24:46 +01003374 srcJarSnapshotPath := filepath.Join(scopeDir, stem+".srcjar")
Paul Duffin22628d52021-05-12 23:13:22 +01003375 ctx.SnapshotBuilder().CopyToSnapshot(properties.StubsSrcJar, srcJarSnapshotPath)
3376 scopeSet.AddProperty("stub_srcs", []string{srcJarSnapshotPath})
3377 } else {
3378 // Merge the stubs source jar into the snapshot zip so that when it is unpacked
3379 // the source files are also unpacked.
Paul Duffine8409952022-09-22 16:24:46 +01003380 snapshotRelativeDir := filepath.Join(scopeDir, stem+"_stub_sources")
Paul Duffin22628d52021-05-12 23:13:22 +01003381 ctx.SnapshotBuilder().UnzipToSnapshot(properties.StubsSrcJar, snapshotRelativeDir)
3382 scopeSet.AddProperty("stub_srcs", []string{snapshotRelativeDir})
3383 }
Paul Duffin3d1248c2020-04-09 00:10:17 +01003384
Paul Duffin1fd005d2020-04-09 01:08:11 +01003385 if properties.CurrentApiFile != nil {
Paul Duffine8409952022-09-22 16:24:46 +01003386 currentApiSnapshotPath := apiScope.snapshotRelativeCurrentApiTxtPath(stem)
Paul Duffin1fd005d2020-04-09 01:08:11 +01003387 ctx.SnapshotBuilder().CopyToSnapshot(properties.CurrentApiFile, currentApiSnapshotPath)
3388 scopeSet.AddProperty("current_api", currentApiSnapshotPath)
3389 }
3390
3391 if properties.RemovedApiFile != nil {
Paul Duffine8409952022-09-22 16:24:46 +01003392 removedApiSnapshotPath := apiScope.snapshotRelativeRemovedApiTxtPath(stem)
Paul Duffin3dbf9fd2020-06-02 13:00:02 +01003393 ctx.SnapshotBuilder().CopyToSnapshot(properties.RemovedApiFile, removedApiSnapshotPath)
Paul Duffin1fd005d2020-04-09 01:08:11 +01003394 scopeSet.AddProperty("removed_api", removedApiSnapshotPath)
3395 }
3396
Anton Hanssond78eb762021-09-21 15:25:12 +01003397 if properties.AnnotationsZip != nil {
Paul Duffine8409952022-09-22 16:24:46 +01003398 annotationsSnapshotPath := filepath.Join(scopeDir, stem+"_annotations.zip")
Anton Hanssond78eb762021-09-21 15:25:12 +01003399 ctx.SnapshotBuilder().CopyToSnapshot(properties.AnnotationsZip, annotationsSnapshotPath)
3400 scopeSet.AddProperty("annotations", annotationsSnapshotPath)
3401 }
3402
Paul Duffindd46f712020-02-10 13:37:10 +00003403 if properties.SdkVersion != "" {
3404 scopeSet.AddProperty("sdk_version", properties.SdkVersion)
3405 }
3406 }
3407 }
3408
Paul Duffina2ae7e02020-09-11 11:55:00 +01003409 if len(s.Doctag_paths) > 0 {
3410 dests := []string{}
3411 for _, p := range s.Doctag_paths {
3412 dest := filepath.Join("doctags", p.Rel())
3413 ctx.SnapshotBuilder().CopyToSnapshot(p, dest)
3414 dests = append(dests, dest)
3415 }
3416 propertySet.AddProperty("doctag_files", dests)
3417 }
Paul Duffindd46f712020-02-10 13:37:10 +00003418}