blob: d1620af9f669916f17a68a5a49f41cd62bc4a099 [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
Chris Parsons39a16972023-06-08 14:28:51 +000027 "android/soong/ui/metrics/bp2build_metrics_proto"
Paul Duffinc0036492023-06-21 15:42:49 +010028
Paul Duffind1b3a922020-01-22 11:57:20 +000029 "github.com/google/blueprint"
Jiyong Parkc678ad32018-04-10 13:07:10 +090030 "github.com/google/blueprint/proptools"
Paul Duffin46a26a82020-04-07 19:27:04 +010031
32 "android/soong/android"
Zi Wangb2179e32023-01-31 15:53:30 -080033 "android/soong/bazel"
Ulya Trafimovichdbf31662020-12-17 12:07:54 +000034 "android/soong/dexpreopt"
Jiyong Parkc678ad32018-04-10 13:07:10 +090035)
36
Jooyung Han58f26ab2019-12-18 15:34:32 +090037const (
Pedro Loureiro9956e5e2021-09-07 17:21:59 +000038 sdkXmlFileSuffix = ".xml"
Jiyong Parkc678ad32018-04-10 13:07:10 +090039)
40
Paul Duffind1b3a922020-01-22 11:57:20 +000041// A tag to associated a dependency with a specific api scope.
42type scopeDependencyTag struct {
43 blueprint.BaseDependencyTag
44 name string
45 apiScope *apiScope
Paul Duffinc8782502020-04-29 20:45:27 +010046
47 // Function for extracting appropriate path information from the dependency.
Colin Crossdcf71b22021-02-01 13:59:03 -080048 depInfoExtractor func(paths *scopePaths, ctx android.ModuleContext, dep android.Module) error
Paul Duffinc8782502020-04-29 20:45:27 +010049}
50
51// Extract tag specific information from the dependency.
52func (tag scopeDependencyTag) extractDepInfo(ctx android.ModuleContext, dep android.Module, paths *scopePaths) {
Colin Crossdcf71b22021-02-01 13:59:03 -080053 err := tag.depInfoExtractor(paths, ctx, dep)
Paul Duffinc8782502020-04-29 20:45:27 +010054 if err != nil {
55 ctx.ModuleErrorf("has an invalid {scopeDependencyTag: %s} dependency on module %s: %s", tag.name, ctx.OtherModuleName(dep), err.Error())
56 }
Paul Duffind1b3a922020-01-22 11:57:20 +000057}
58
Paul Duffin80342d72020-06-26 22:08:43 +010059var _ android.ReplaceSourceWithPrebuilt = (*scopeDependencyTag)(nil)
60
61func (tag scopeDependencyTag) ReplaceSourceWithPrebuilt() bool {
62 return false
63}
64
Paul Duffind1b3a922020-01-22 11:57:20 +000065// Provides information about an api scope, e.g. public, system, test.
66type apiScope struct {
67 // The name of the api scope, e.g. public, system, test
68 name string
69
Paul Duffin97b53b82020-05-05 14:40:52 +010070 // The api scope that this scope extends.
Paul Duffind0b9fca2022-09-30 18:11:41 +010071 //
72 // This organizes the scopes into an extension hierarchy.
73 //
74 // If set this means that the API provided by this scope includes the API provided by the scope
75 // set in this field.
Paul Duffin97b53b82020-05-05 14:40:52 +010076 extends *apiScope
77
Paul Duffind0b9fca2022-09-30 18:11:41 +010078 // The next api scope that a library that uses this scope can access.
79 //
80 // This organizes the scopes into an access hierarchy.
81 //
82 // If set this means that a library that can access this API can also access the API provided by
83 // the scope set in this field.
84 //
85 // A module that sets sdk_version: "<scope>_current" should have access to the <scope> API of
86 // every java_sdk_library that it depends on. If the library does not provide an API for <scope>
87 // then it will traverse up this access hierarchy to find an API that it does provide.
88 //
89 // If this is not set then it defaults to the scope set in extends.
90 canAccess *apiScope
91
Paul Duffin3375e352020-04-28 10:44:03 +010092 // The legacy enabled status for a specific scope can be dependent on other
93 // properties that have been specified on the library so it is provided by
94 // a function that can determine the status by examining those properties.
95 legacyEnabledStatus func(module *SdkLibrary) bool
96
97 // The default enabled status for non-legacy behavior, which is triggered by
98 // explicitly enabling at least one api scope.
99 defaultEnabledStatus bool
100
101 // Gets a pointer to the scope specific properties.
102 scopeSpecificProperties func(module *SdkLibrary) *ApiScopeProperties
103
Paul Duffin46a26a82020-04-07 19:27:04 +0100104 // The name of the field in the dynamically created structure.
105 fieldName string
106
Paul Duffin6b836ba2020-05-13 19:19:49 +0100107 // The name of the property in the java_sdk_library_import
108 propertyName string
109
Paul Duffind1b3a922020-01-22 11:57:20 +0000110 // The tag to use to depend on the stubs library module.
111 stubsTag scopeDependencyTag
112
Paul Duffin0ff08bd2020-04-29 13:30:54 +0100113 // The tag to use to depend on the stubs source module (if separate from the API module).
114 stubsSourceTag scopeDependencyTag
115
116 // The tag to use to depend on the API file generating module (if separate from the stubs source module).
117 apiFileTag scopeDependencyTag
118
Paul Duffinc8782502020-04-29 20:45:27 +0100119 // The tag to use to depend on the stubs source and API module.
120 stubsSourceAndApiTag scopeDependencyTag
Paul Duffind1b3a922020-01-22 11:57:20 +0000121
Paul Duffin958806b2022-05-16 13:10:47 +0000122 // The tag to use to depend on the module that provides the latest version of the API .txt file.
123 latestApiModuleTag scopeDependencyTag
124
125 // The tag to use to depend on the module that provides the latest version of the API removed.txt
126 // file.
127 latestRemovedApiModuleTag scopeDependencyTag
128
Paul Duffind1b3a922020-01-22 11:57:20 +0000129 // The scope specific prefix to add to the api file base of "current.txt" or "removed.txt".
130 apiFilePrefix string
131
Paul Duffind0b9fca2022-09-30 18:11:41 +0100132 // The scope specific suffix to add to the sdk library module name to construct a scope specific
Paul Duffind1b3a922020-01-22 11:57:20 +0000133 // module name.
134 moduleSuffix string
135
Paul Duffind1b3a922020-01-22 11:57:20 +0000136 // SDK version that the stubs library is built against. Note that this is always
137 // *current. Older stubs library built with a numbered SDK version is created from
138 // the prebuilt jar.
139 sdkVersion string
Paul Duffin1fb487d2020-04-07 18:50:10 +0100140
Paul Duffin15f34ef2020-07-20 18:04:44 +0100141 // The annotation that identifies this API level, empty for the public API scope.
142 annotation string
143
Paul Duffin1fb487d2020-04-07 18:50:10 +0100144 // Extra arguments to pass to droidstubs for this scope.
Paul Duffin0ff08bd2020-04-29 13:30:54 +0100145 //
Paul Duffin15f34ef2020-07-20 18:04:44 +0100146 // This is not used directly but is used to construct the droidstubsArgs.
147 extraArgs []string
Paul Duffin0ff08bd2020-04-29 13:30:54 +0100148
Paul Duffin15f34ef2020-07-20 18:04:44 +0100149 // The args that must be passed to droidstubs to generate the API and stubs source
150 // for this scope, constructed dynamically by initApiScope().
Paul Duffin0ff08bd2020-04-29 13:30:54 +0100151 //
152 // The API only includes the additional members that this scope adds over the scope
153 // that it extends.
Paul Duffin15f34ef2020-07-20 18:04:44 +0100154 //
155 // The stubs source must include the definitions of everything that is in this
156 // api scope and all the scopes that this one extends.
157 droidstubsArgs []string
Paul Duffin0ff08bd2020-04-29 13:30:54 +0100158
Anton Hansson6478ac12020-05-02 11:19:36 +0100159 // Whether the api scope can be treated as unstable, and should skip compat checks.
160 unstable bool
Jihoon Kang1c92c3e2023-03-23 17:44:51 +0000161
162 // Represents the SDK kind of this scope.
163 kind android.SdkKind
Paul Duffind1b3a922020-01-22 11:57:20 +0000164}
165
166// Initialize a scope, creating and adding appropriate dependency tags
167func initApiScope(scope *apiScope) *apiScope {
Paul Duffinc8782502020-04-29 20:45:27 +0100168 name := scope.name
Paul Duffin46dc45a2020-05-14 15:39:10 +0100169 scopeByName[name] = scope
170 allScopeNames = append(allScopeNames, name)
Paul Duffin6b836ba2020-05-13 19:19:49 +0100171 scope.propertyName = strings.ReplaceAll(name, "-", "_")
172 scope.fieldName = proptools.FieldNameForProperty(scope.propertyName)
Paul Duffind1b3a922020-01-22 11:57:20 +0000173 scope.stubsTag = scopeDependencyTag{
Paul Duffinc8782502020-04-29 20:45:27 +0100174 name: name + "-stubs",
175 apiScope: scope,
176 depInfoExtractor: (*scopePaths).extractStubsLibraryInfoFromDependency,
Paul Duffind1b3a922020-01-22 11:57:20 +0000177 }
Paul Duffin0ff08bd2020-04-29 13:30:54 +0100178 scope.stubsSourceTag = scopeDependencyTag{
179 name: name + "-stubs-source",
180 apiScope: scope,
181 depInfoExtractor: (*scopePaths).extractStubsSourceInfoFromDep,
182 }
183 scope.apiFileTag = scopeDependencyTag{
184 name: name + "-api",
185 apiScope: scope,
186 depInfoExtractor: (*scopePaths).extractApiInfoFromDep,
187 }
Paul Duffinc8782502020-04-29 20:45:27 +0100188 scope.stubsSourceAndApiTag = scopeDependencyTag{
189 name: name + "-stubs-source-and-api",
190 apiScope: scope,
191 depInfoExtractor: (*scopePaths).extractStubsSourceAndApiInfoFromApiStubsProvider,
Paul Duffind1b3a922020-01-22 11:57:20 +0000192 }
Paul Duffin958806b2022-05-16 13:10:47 +0000193 scope.latestApiModuleTag = scopeDependencyTag{
194 name: name + "-latest-api",
195 apiScope: scope,
196 depInfoExtractor: (*scopePaths).extractLatestApiPath,
197 }
198 scope.latestRemovedApiModuleTag = scopeDependencyTag{
199 name: name + "-latest-removed-api",
200 apiScope: scope,
201 depInfoExtractor: (*scopePaths).extractLatestRemovedApiPath,
202 }
Paul Duffin0ff08bd2020-04-29 13:30:54 +0100203
204 // To get the args needed to generate the stubs source append all the args from
205 // this scope and all the scopes it extends as each set of args adds additional
206 // members to the stubs.
Paul Duffin15f34ef2020-07-20 18:04:44 +0100207 var scopeSpecificArgs []string
208 if scope.annotation != "" {
209 scopeSpecificArgs = []string{"--show-annotation", scope.annotation}
Paul Duffin0ff08bd2020-04-29 13:30:54 +0100210 }
Paul Duffin15f34ef2020-07-20 18:04:44 +0100211 for s := scope; s != nil; s = s.extends {
212 scopeSpecificArgs = append(scopeSpecificArgs, s.extraArgs...)
Paul Duffin0ff08bd2020-04-29 13:30:54 +0100213
Paul Duffin15f34ef2020-07-20 18:04:44 +0100214 // Ensure that the generated stubs includes all the API elements from the API scope
215 // that this scope extends.
216 if s != scope && s.annotation != "" {
217 scopeSpecificArgs = append(scopeSpecificArgs, "--show-for-stub-purposes-annotation", s.annotation)
218 }
219 }
Paul Duffin0ff08bd2020-04-29 13:30:54 +0100220
Paul Duffind0b9fca2022-09-30 18:11:41 +0100221 // By default, a library that can access a scope can also access the scope it extends.
222 if scope.canAccess == nil {
223 scope.canAccess = scope.extends
224 }
225
Paul Duffin15f34ef2020-07-20 18:04:44 +0100226 // Escape any special characters in the arguments. This is needed because droidstubs
227 // passes these directly to the shell command.
228 scope.droidstubsArgs = proptools.ShellEscapeList(scopeSpecificArgs)
Paul Duffin0ff08bd2020-04-29 13:30:54 +0100229
Paul Duffind1b3a922020-01-22 11:57:20 +0000230 return scope
231}
232
Anton Hansson08f476b2021-04-07 15:32:19 +0100233func (scope *apiScope) stubsLibraryModuleNameSuffix() string {
234 return ".stubs" + scope.moduleSuffix
235}
236
Jihoon Kang1c92c3e2023-03-23 17:44:51 +0000237func (scope *apiScope) apiLibraryModuleName(baseName string) string {
238 return scope.stubsLibraryModuleName(baseName) + ".from-text"
239}
240
Jihoon Kang1147b312023-06-08 23:25:57 +0000241func (scope *apiScope) sourceStubLibraryModuleName(baseName string) string {
242 return scope.stubsLibraryModuleName(baseName) + ".from-source"
243}
244
Paul Duffinc3091c82020-05-08 14:16:20 +0100245func (scope *apiScope) stubsLibraryModuleName(baseName string) string {
Anton Hansson08f476b2021-04-07 15:32:19 +0100246 return baseName + scope.stubsLibraryModuleNameSuffix()
Paul Duffind1b3a922020-01-22 11:57:20 +0000247}
248
Paul Duffinc8782502020-04-29 20:45:27 +0100249func (scope *apiScope) stubsSourceModuleName(baseName string) string {
Paul Duffindd9d0742020-05-08 15:52:37 +0100250 return baseName + ".stubs.source" + scope.moduleSuffix
Paul Duffind1b3a922020-01-22 11:57:20 +0000251}
252
Paul Duffin0ff08bd2020-04-29 13:30:54 +0100253func (scope *apiScope) apiModuleName(baseName string) string {
Paul Duffindd9d0742020-05-08 15:52:37 +0100254 return baseName + ".api" + scope.moduleSuffix
Paul Duffin0ff08bd2020-04-29 13:30:54 +0100255}
256
Paul Duffin3375e352020-04-28 10:44:03 +0100257func (scope *apiScope) String() string {
258 return scope.name
259}
260
Paul Duffin958806b2022-05-16 13:10:47 +0000261// snapshotRelativeDir returns the snapshot directory into which the files related to scopes will
262// be stored.
263func (scope *apiScope) snapshotRelativeDir() string {
264 return filepath.Join("sdk_library", scope.name)
265}
266
267// snapshotRelativeCurrentApiTxtPath returns the snapshot path to the API .txt file for the named
268// library.
269func (scope *apiScope) snapshotRelativeCurrentApiTxtPath(name string) string {
270 return filepath.Join(scope.snapshotRelativeDir(), name+".txt")
271}
272
273// snapshotRelativeRemovedApiTxtPath returns the snapshot path to the removed API .txt file for the
274// named library.
275func (scope *apiScope) snapshotRelativeRemovedApiTxtPath(name string) string {
276 return filepath.Join(scope.snapshotRelativeDir(), name+"-removed.txt")
277}
278
Paul Duffind1b3a922020-01-22 11:57:20 +0000279type apiScopes []*apiScope
280
281func (scopes apiScopes) Strings(accessor func(*apiScope) string) []string {
282 var list []string
283 for _, scope := range scopes {
284 list = append(list, accessor(scope))
285 }
286 return list
287}
288
Jiyong Parkc678ad32018-04-10 13:07:10 +0900289var (
Paul Duffin46dc45a2020-05-14 15:39:10 +0100290 scopeByName = make(map[string]*apiScope)
291 allScopeNames []string
Paul Duffind1b3a922020-01-22 11:57:20 +0000292 apiScopePublic = initApiScope(&apiScope{
Paul Duffin3375e352020-04-28 10:44:03 +0100293 name: "public",
294
295 // Public scope is enabled by default for both legacy and non-legacy modes.
296 legacyEnabledStatus: func(module *SdkLibrary) bool {
297 return true
298 },
299 defaultEnabledStatus: true,
300
301 scopeSpecificProperties: func(module *SdkLibrary) *ApiScopeProperties {
302 return &module.sdkLibraryProperties.Public
303 },
Paul Duffind1b3a922020-01-22 11:57:20 +0000304 sdkVersion: "current",
Jihoon Kang1c92c3e2023-03-23 17:44:51 +0000305 kind: android.SdkPublic,
Paul Duffind1b3a922020-01-22 11:57:20 +0000306 })
307 apiScopeSystem = initApiScope(&apiScope{
Paul Duffin3375e352020-04-28 10:44:03 +0100308 name: "system",
309 extends: apiScopePublic,
310 legacyEnabledStatus: (*SdkLibrary).generateTestAndSystemScopesByDefault,
311 scopeSpecificProperties: func(module *SdkLibrary) *ApiScopeProperties {
312 return &module.sdkLibraryProperties.System
313 },
Paul Duffin15f34ef2020-07-20 18:04:44 +0100314 apiFilePrefix: "system-",
315 moduleSuffix: ".system",
316 sdkVersion: "system_current",
317 annotation: "android.annotation.SystemApi(client=android.annotation.SystemApi.Client.PRIVILEGED_APPS)",
Jihoon Kang1c92c3e2023-03-23 17:44:51 +0000318 kind: android.SdkSystem,
Paul Duffind1b3a922020-01-22 11:57:20 +0000319 })
320 apiScopeTest = initApiScope(&apiScope{
Paul Duffin3375e352020-04-28 10:44:03 +0100321 name: "test",
Anton Hansson4fe970f2020-10-09 10:16:49 +0100322 extends: apiScopeSystem,
Paul Duffin3375e352020-04-28 10:44:03 +0100323 legacyEnabledStatus: (*SdkLibrary).generateTestAndSystemScopesByDefault,
324 scopeSpecificProperties: func(module *SdkLibrary) *ApiScopeProperties {
325 return &module.sdkLibraryProperties.Test
326 },
Paul Duffin15f34ef2020-07-20 18:04:44 +0100327 apiFilePrefix: "test-",
328 moduleSuffix: ".test",
329 sdkVersion: "test_current",
330 annotation: "android.annotation.TestApi",
331 unstable: true,
Jihoon Kang1c92c3e2023-03-23 17:44:51 +0000332 kind: android.SdkTest,
Paul Duffind1b3a922020-01-22 11:57:20 +0000333 })
Paul Duffin8f265b92020-04-28 14:13:56 +0100334 apiScopeModuleLib = initApiScope(&apiScope{
Paul Duffin6b836ba2020-05-13 19:19:49 +0100335 name: "module-lib",
Paul Duffin8f265b92020-04-28 14:13:56 +0100336 extends: apiScopeSystem,
Paul Duffin0c5bae52020-06-02 13:00:08 +0100337 // The module-lib scope is disabled by default in legacy mode.
Paul Duffin8f265b92020-04-28 14:13:56 +0100338 //
339 // Enabling this would break existing usages.
340 legacyEnabledStatus: func(module *SdkLibrary) bool {
341 return false
342 },
343 scopeSpecificProperties: func(module *SdkLibrary) *ApiScopeProperties {
344 return &module.sdkLibraryProperties.Module_lib
345 },
346 apiFilePrefix: "module-lib-",
347 moduleSuffix: ".module_lib",
348 sdkVersion: "module_current",
Paul Duffin15f34ef2020-07-20 18:04:44 +0100349 annotation: "android.annotation.SystemApi(client=android.annotation.SystemApi.Client.MODULE_LIBRARIES)",
Jihoon Kang1c92c3e2023-03-23 17:44:51 +0000350 kind: android.SdkModule,
Paul Duffin8f265b92020-04-28 14:13:56 +0100351 })
Paul Duffin0c5bae52020-06-02 13:00:08 +0100352 apiScopeSystemServer = initApiScope(&apiScope{
353 name: "system-server",
354 extends: apiScopePublic,
Paul Duffind0b9fca2022-09-30 18:11:41 +0100355
356 // The system-server scope can access the module-lib scope.
357 //
358 // A module that provides a system-server API is appended to the standard bootclasspath that is
359 // used by the system server. So, it should be able to access module-lib APIs provided by
360 // libraries on the bootclasspath.
361 canAccess: apiScopeModuleLib,
362
Paul Duffin0c5bae52020-06-02 13:00:08 +0100363 // The system-server scope is disabled by default in legacy mode.
364 //
365 // Enabling this would break existing usages.
366 legacyEnabledStatus: func(module *SdkLibrary) bool {
367 return false
368 },
369 scopeSpecificProperties: func(module *SdkLibrary) *ApiScopeProperties {
370 return &module.sdkLibraryProperties.System_server
371 },
372 apiFilePrefix: "system-server-",
373 moduleSuffix: ".system_server",
374 sdkVersion: "system_server_current",
Paul Duffin15f34ef2020-07-20 18:04:44 +0100375 annotation: "android.annotation.SystemApi(client=android.annotation.SystemApi.Client.SYSTEM_SERVER)",
376 extraArgs: []string{
377 "--hide-annotation", "android.annotation.Hide",
Paul Duffin0c5bae52020-06-02 13:00:08 +0100378 // com.android.* classes are okay in this interface"
Paul Duffin15f34ef2020-07-20 18:04:44 +0100379 "--hide", "InternalClasses",
Paul Duffin0c5bae52020-06-02 13:00:08 +0100380 },
Jihoon Kang1c92c3e2023-03-23 17:44:51 +0000381 kind: android.SdkSystemServer,
Paul Duffin0c5bae52020-06-02 13:00:08 +0100382 })
Paul Duffind1b3a922020-01-22 11:57:20 +0000383 allApiScopes = apiScopes{
384 apiScopePublic,
385 apiScopeSystem,
386 apiScopeTest,
Paul Duffin8f265b92020-04-28 14:13:56 +0100387 apiScopeModuleLib,
Paul Duffin0c5bae52020-06-02 13:00:08 +0100388 apiScopeSystemServer,
Paul Duffind1b3a922020-01-22 11:57:20 +0000389 }
Jihoon Kang0c705a42023-08-02 06:44:57 +0000390 apiLibraryAdditionalProperties = map[string]struct {
391 FullApiSurfaceStubLib string
392 AdditionalApiContribution string
393 }{
394 "legacy.i18n.module.platform.api": {
395 FullApiSurfaceStubLib: "legacy.core.platform.api.stubs",
396 AdditionalApiContribution: "i18n.module.public.api.stubs.source.api.contribution",
397 },
398 "stable.i18n.module.platform.api": {
399 FullApiSurfaceStubLib: "stable.core.platform.api.stubs",
400 AdditionalApiContribution: "i18n.module.public.api.stubs.source.api.contribution",
401 },
402 "conscrypt.module.platform.api": {
403 FullApiSurfaceStubLib: "stable.core.platform.api.stubs",
404 AdditionalApiContribution: "conscrypt.module.public.api.stubs.source.api.contribution",
405 },
406 }
Jiyong Parkc678ad32018-04-10 13:07:10 +0900407)
408
Jiyong Park82484c02018-04-23 21:41:26 +0900409var (
410 javaSdkLibrariesLock sync.Mutex
411)
412
Jiyong Parkc678ad32018-04-10 13:07:10 +0900413// TODO: these are big features that are currently missing
Jiyong Park1be96912018-05-28 18:02:19 +0900414// 1) disallowing linking to the runtime shared lib
415// 2) HTML generation
Jiyong Parkc678ad32018-04-10 13:07:10 +0900416
417func init() {
Paul Duffin43dc1cc2019-12-19 11:18:54 +0000418 RegisterSdkLibraryBuildComponents(android.InitRegistrationContext)
Jiyong Parkc678ad32018-04-10 13:07:10 +0900419
Jiyong Park82484c02018-04-23 21:41:26 +0900420 android.RegisterMakeVarsProvider(pctx, func(ctx android.MakeVarsContext) {
421 javaSdkLibraries := javaSdkLibraries(ctx.Config())
422 sort.Strings(*javaSdkLibraries)
423 ctx.Strict("JAVA_SDK_LIBRARIES", strings.Join(*javaSdkLibraries, " "))
424 })
Paul Duffindd46f712020-02-10 13:37:10 +0000425
426 // Register sdk member types.
Paul Duffin976b0e52021-04-27 23:20:26 +0100427 android.RegisterSdkMemberType(javaSdkLibrarySdkMemberType)
Jiyong Parkc678ad32018-04-10 13:07:10 +0900428}
429
Paul Duffin43dc1cc2019-12-19 11:18:54 +0000430func RegisterSdkLibraryBuildComponents(ctx android.RegistrationContext) {
431 ctx.RegisterModuleType("java_sdk_library", SdkLibraryFactory)
432 ctx.RegisterModuleType("java_sdk_library_import", sdkLibraryImportFactory)
433}
434
Paul Duffin3375e352020-04-28 10:44:03 +0100435// Properties associated with each api scope.
436type ApiScopeProperties struct {
437 // Indicates whether the api surface is generated.
438 //
439 // If this is set for any scope then all scopes must explicitly specify if they
440 // are enabled. This is to prevent new usages from depending on legacy behavior.
441 //
442 // Otherwise, if this is not set for any scope then the default behavior is
443 // scope specific so please refer to the scope specific property documentation.
444 Enabled *bool
Paul Duffin87a05a32020-05-12 11:50:28 +0100445
446 // The sdk_version to use for building the stubs.
447 //
448 // If not specified then it will use an sdk_version determined as follows:
Trevor Radcliffedf8aa1f2021-11-04 14:25:39 +0000449 //
Paul Duffin87a05a32020-05-12 11:50:28 +0100450 // 1) If the sdk_version specified on the java_sdk_library is none then this
Trevor Radcliffedf8aa1f2021-11-04 14:25:39 +0000451 // will be none. This is used for java_sdk_library instances that are used
452 // to create stubs that contribute to the core_current sdk version.
453 // 2) Otherwise, it is assumed that this library extends but does not
454 // contribute directly to a specific sdk_version and so this uses the
455 // sdk_version appropriate for the api scope. e.g. public will use
456 // sdk_version: current, system will use sdk_version: system_current, etc.
Paul Duffin87a05a32020-05-12 11:50:28 +0100457 //
458 // This does not affect the sdk_version used for either generating the stubs source
459 // or the API file. They both have to use the same sdk_version as is used for
460 // compiling the implementation library.
461 Sdk_version *string
Paul Duffin3375e352020-04-28 10:44:03 +0100462}
463
Jiyong Parkc678ad32018-04-10 13:07:10 +0900464type sdkLibraryProperties struct {
Anton Hanssonf8ea3722021-09-16 14:24:13 +0100465 // List of source files that are needed to compile the API, but are not part of runtime library.
466 Api_srcs []string `android:"arch_variant"`
467
Paul Duffin5df79302020-05-16 15:52:12 +0100468 // Visibility for impl library module. If not specified then defaults to the
469 // visibility property.
470 Impl_library_visibility []string
471
Paul Duffin4911a892020-04-29 23:35:13 +0100472 // Visibility for stubs library modules. If not specified then defaults to the
473 // visibility property.
474 Stubs_library_visibility []string
475
476 // Visibility for stubs source modules. If not specified then defaults to the
477 // visibility property.
478 Stubs_source_visibility []string
479
Anton Hansson7f66efa2020-10-08 14:47:23 +0100480 // List of Java libraries that will be in the classpath when building the implementation lib
481 Impl_only_libs []string `android:"arch_variant"`
482
Paul Duffin77590a82022-04-28 14:13:30 +0000483 // List of Java libraries that will included in the implementation lib.
484 Impl_only_static_libs []string `android:"arch_variant"`
485
Sundong Ahnf043cf62018-06-25 16:04:37 +0900486 // List of Java libraries that will be in the classpath when building stubs
487 Stub_only_libs []string `android:"arch_variant"`
488
Anton Hanssondae54cd2021-04-21 16:30:10 +0100489 // List of Java libraries that will included in stub libraries
490 Stub_only_static_libs []string `android:"arch_variant"`
491
Paul Duffin7a586d32019-12-30 17:09:34 +0000492 // list of package names that will be documented and publicized as API.
493 // This allows the API to be restricted to a subset of the source files provided.
494 // If this is unspecified then all the source files will be treated as being part
495 // of the API.
Jiyong Parkc678ad32018-04-10 13:07:10 +0900496 Api_packages []string
497
Jiyong Park5a2c9d72018-05-01 22:25:41 +0900498 // list of package names that must be hidden from the API
499 Hidden_api_packages []string
500
Paul Duffin749f98f2019-12-30 17:23:46 +0000501 // the relative path to the directory containing the api specification files.
502 // Defaults to "api".
503 Api_dir *string
504
Paul Duffindfa131e2020-05-15 20:37:11 +0100505 // Determines whether a runtime implementation library is built; defaults to false.
506 //
507 // 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 +0200508 // it is as if shared_library: false, was set.
Paul Duffin43db9be2019-12-30 17:35:49 +0000509 Api_only *bool
510
Paul Duffin11512472019-02-11 15:55:17 +0000511 // local files that are used within user customized droiddoc options.
512 Droiddoc_option_files []string
513
Spandan Das93e95992021-07-29 18:26:39 +0000514 // additional droiddoc options.
Paul Duffin11512472019-02-11 15:55:17 +0000515 // Available variables for substitution:
516 //
517 // $(location <label>): the path to the droiddoc_option_files with name <label>
Sundong Ahndd567f92018-07-31 17:19:11 +0900518 Droiddoc_options []string
519
Paul Duffine22c2ab2020-05-20 19:35:27 +0100520 // is set to true, Metalava will allow framework SDK to contain annotations.
521 Annotations_enabled *bool
522
Sundong Ahn054b19a2018-10-19 13:46:09 +0900523 // a list of top-level directories containing files to merge qualifier annotations
524 // (i.e. those intended to be included in the stubs written) from.
525 Merge_annotations_dirs []string
526
527 // a list of top-level directories containing Java stub files to merge show/hide annotations from.
528 Merge_inclusion_annotations_dirs []string
529
Paul Duffin4f5c1ef2020-11-19 14:53:43 +0000530 // If set to true then don't create dist rules.
531 No_dist *bool
Sundong Ahn80a87b32019-05-13 15:02:50 +0900532
Paul Duffin31310252020-11-20 21:26:20 +0000533 // The stem for the artifacts that are copied to the dist, if not specified
534 // then defaults to the base module name.
535 //
536 // For each scope the following artifacts are copied to the apistubs/<scope>
537 // directory in the dist.
538 // * stubs impl jar -> <dist-stem>.jar
539 // * API specification file -> api/<dist-stem>.txt
540 // * Removed API specification file -> api/<dist-stem>-removed.txt
541 //
542 // Also used to construct the name of the filegroup (created by prebuilt_apis)
543 // that references the latest released API and remove API specification files.
544 // * API specification filegroup -> <dist-stem>.api.<scope>.latest
545 // * Removed API specification filegroup -> <dist-stem>-removed.api.<scope>.latest
Jaewoong Jung1a97ee02021-03-09 13:25:02 -0800546 // * API incompatibilities baseline filegroup -> <dist-stem>-incompatibilities.api.<scope>.latest
Paul Duffin31310252020-11-20 21:26:20 +0000547 Dist_stem *string
548
Colin Cross986b69a2021-06-01 13:13:40 -0700549 // The subdirectory for the artifacts that are copied to the dist directory. If not specified
Colin Cross3dd66252021-06-01 14:05:09 -0700550 // then defaults to "unknown". Should be set to "android" for anything that should be published
Colin Cross986b69a2021-06-01 13:13:40 -0700551 // in the public Android SDK.
552 Dist_group *string
553
Anton Hanssondff2c782020-12-21 17:10:01 +0000554 // A compatibility mode that allows historical API-tracking files to not exist.
555 // Do not use.
556 Unsafe_ignore_missing_latest_api bool
557
Paul Duffin3375e352020-04-28 10:44:03 +0100558 // indicates whether system and test apis should be generated.
559 Generate_system_and_test_apis bool `blueprint:"mutated"`
560
561 // The properties specific to the public api scope
562 //
563 // Unless explicitly specified by using public.enabled the public api scope is
564 // enabled by default in both legacy and non-legacy mode.
565 Public ApiScopeProperties
566
567 // The properties specific to the system api scope
568 //
569 // In legacy mode the system api scope is enabled by default when sdk_version
570 // is set to something other than "none".
571 //
572 // In non-legacy mode the system api scope is disabled by default.
573 System ApiScopeProperties
574
575 // The properties specific to the test api scope
576 //
577 // In legacy mode the test api scope is enabled by default when sdk_version
578 // is set to something other than "none".
579 //
580 // In non-legacy mode the test api scope is disabled by default.
581 Test ApiScopeProperties
Paul Duffin37e0b772019-12-30 17:20:10 +0000582
Paul Duffin0c5bae52020-06-02 13:00:08 +0100583 // The properties specific to the module-lib api scope
Paul Duffin8f265b92020-04-28 14:13:56 +0100584 //
Zi Wangb2179e32023-01-31 15:53:30 -0800585 // Unless explicitly specified by using module_lib.enabled the module_lib api
586 // scope is disabled by default.
Paul Duffin8f265b92020-04-28 14:13:56 +0100587 Module_lib ApiScopeProperties
588
Paul Duffin0c5bae52020-06-02 13:00:08 +0100589 // The properties specific to the system-server api scope
590 //
Zi Wangb2179e32023-01-31 15:53:30 -0800591 // Unless explicitly specified by using system_server.enabled the
592 // system_server api scope is disabled by default.
Paul Duffin0c5bae52020-06-02 13:00:08 +0100593 System_server ApiScopeProperties
594
Jiyong Park932cdfe2020-05-28 00:19:53 +0900595 // Determines if the stubs are preferred over the implementation library
596 // for linking, even when the client doesn't specify sdk_version. When this
597 // is set to true, such clients are provided with the widest API surface that
598 // this lib provides. Note however that this option doesn't affect the clients
599 // that are in the same APEX as this library. In that case, the clients are
600 // always linked with the implementation library. Default is false.
601 Default_to_stubs *bool
602
Paul Duffin160fe412020-05-10 19:32:20 +0100603 // Properties related to api linting.
604 Api_lint struct {
605 // Enable api linting.
606 Enabled *bool
607 }
608
Jiyong Parkc678ad32018-04-10 13:07:10 +0900609 // TODO: determines whether to create HTML doc or not
Paul Duffine8409952022-09-22 16:24:46 +0100610 // Html_doc *bool
Jiyong Parkc678ad32018-04-10 13:07:10 +0900611}
612
Paul Duffin0f8faff2020-05-20 16:18:00 +0100613// Paths to outputs from java_sdk_library and java_sdk_library_import.
614//
615// Fields that are android.Paths are always set (during GenerateAndroidBuildActions).
616// OptionalPaths are always set by java_sdk_library but may not be set by
617// java_sdk_library_import as not all instances provide that information.
Paul Duffind1b3a922020-01-22 11:57:20 +0000618type scopePaths struct {
Paul Duffin0f8faff2020-05-20 16:18:00 +0100619 // The path (represented as Paths for convenience when returning) to the stubs header jar.
620 //
621 // That is the jar that is created by turbine.
622 stubsHeaderPath android.Paths
623
624 // The path (represented as Paths for convenience when returning) to the stubs implementation jar.
625 //
626 // This is not the implementation jar, it still only contains stubs.
627 stubsImplPath android.Paths
628
Paul Duffin1267d872021-04-16 17:21:36 +0100629 // The dex jar for the stubs.
630 //
631 // This is not the implementation jar, it still only contains stubs.
Martin Stjernholm8be1e6d2021-09-15 03:34:04 +0100632 stubsDexJarPath OptionalDexJarPath
Paul Duffin1267d872021-04-16 17:21:36 +0100633
Paul Duffin0f8faff2020-05-20 16:18:00 +0100634 // The API specification file, e.g. system_current.txt.
635 currentApiFilePath android.OptionalPath
636
637 // The specification of API elements removed since the last release.
638 removedApiFilePath android.OptionalPath
639
640 // The stubs source jar.
641 stubsSrcJar android.OptionalPath
Anton Hanssond78eb762021-09-21 15:25:12 +0100642
643 // Extracted annotations.
644 annotationsZip android.OptionalPath
Paul Duffin958806b2022-05-16 13:10:47 +0000645
646 // The path to the latest API file.
647 latestApiPath android.OptionalPath
648
649 // The path to the latest removed API file.
650 latestRemovedApiPath android.OptionalPath
Paul Duffind1b3a922020-01-22 11:57:20 +0000651}
652
Colin Crossdcf71b22021-02-01 13:59:03 -0800653func (paths *scopePaths) extractStubsLibraryInfoFromDependency(ctx android.ModuleContext, dep android.Module) error {
654 if ctx.OtherModuleHasProvider(dep, JavaInfoProvider) {
655 lib := ctx.OtherModuleProvider(dep, JavaInfoProvider).(JavaInfo)
656 paths.stubsHeaderPath = lib.HeaderJars
657 paths.stubsImplPath = lib.ImplementationJars
Paul Duffin1267d872021-04-16 17:21:36 +0100658
659 libDep := dep.(UsesLibraryDependency)
660 paths.stubsDexJarPath = libDep.DexJarBuildPath()
Paul Duffinc8782502020-04-29 20:45:27 +0100661 return nil
662 } else {
Colin Crossdcf71b22021-02-01 13:59:03 -0800663 return fmt.Errorf("expected module that has JavaInfoProvider, e.g. java_library")
Paul Duffinc8782502020-04-29 20:45:27 +0100664 }
665}
666
Paul Duffin0ff08bd2020-04-29 13:30:54 +0100667func (paths *scopePaths) treatDepAsApiStubsProvider(dep android.Module, action func(provider ApiStubsProvider)) error {
668 if apiStubsProvider, ok := dep.(ApiStubsProvider); ok {
669 action(apiStubsProvider)
Paul Duffinc8782502020-04-29 20:45:27 +0100670 return nil
671 } else {
672 return fmt.Errorf("expected module that implements ApiStubsProvider, e.g. droidstubs")
673 }
674}
675
Paul Duffin0f8faff2020-05-20 16:18:00 +0100676func (paths *scopePaths) treatDepAsApiStubsSrcProvider(dep android.Module, action func(provider ApiStubsSrcProvider)) error {
677 if apiStubsProvider, ok := dep.(ApiStubsSrcProvider); ok {
678 action(apiStubsProvider)
679 return nil
680 } else {
681 return fmt.Errorf("expected module that implements ApiStubsSrcProvider, e.g. droidstubs")
682 }
683}
684
Paul Duffin0ff08bd2020-04-29 13:30:54 +0100685func (paths *scopePaths) extractApiInfoFromApiStubsProvider(provider ApiStubsProvider) {
Anton Hanssond78eb762021-09-21 15:25:12 +0100686 paths.annotationsZip = android.OptionalPathForPath(provider.AnnotationsZip())
Paul Duffin0f8faff2020-05-20 16:18:00 +0100687 paths.currentApiFilePath = android.OptionalPathForPath(provider.ApiFilePath())
688 paths.removedApiFilePath = android.OptionalPathForPath(provider.RemovedApiFilePath())
Paul Duffin0ff08bd2020-04-29 13:30:54 +0100689}
690
Colin Crossdcf71b22021-02-01 13:59:03 -0800691func (paths *scopePaths) extractApiInfoFromDep(ctx android.ModuleContext, dep android.Module) error {
Paul Duffin0ff08bd2020-04-29 13:30:54 +0100692 return paths.treatDepAsApiStubsProvider(dep, func(provider ApiStubsProvider) {
693 paths.extractApiInfoFromApiStubsProvider(provider)
694 })
695}
696
Paul Duffin0f8faff2020-05-20 16:18:00 +0100697func (paths *scopePaths) extractStubsSourceInfoFromApiStubsProviders(provider ApiStubsSrcProvider) {
698 paths.stubsSrcJar = android.OptionalPathForPath(provider.StubsSrcJar())
Paul Duffin0ff08bd2020-04-29 13:30:54 +0100699}
700
Colin Crossdcf71b22021-02-01 13:59:03 -0800701func (paths *scopePaths) extractStubsSourceInfoFromDep(ctx android.ModuleContext, dep android.Module) error {
Paul Duffin0f8faff2020-05-20 16:18:00 +0100702 return paths.treatDepAsApiStubsSrcProvider(dep, func(provider ApiStubsSrcProvider) {
Paul Duffin0ff08bd2020-04-29 13:30:54 +0100703 paths.extractStubsSourceInfoFromApiStubsProviders(provider)
704 })
705}
706
Colin Crossdcf71b22021-02-01 13:59:03 -0800707func (paths *scopePaths) extractStubsSourceAndApiInfoFromApiStubsProvider(ctx android.ModuleContext, dep android.Module) error {
Paul Duffin0ff08bd2020-04-29 13:30:54 +0100708 return paths.treatDepAsApiStubsProvider(dep, func(provider ApiStubsProvider) {
709 paths.extractApiInfoFromApiStubsProvider(provider)
710 paths.extractStubsSourceInfoFromApiStubsProviders(provider)
711 })
712}
713
Paul Duffin958806b2022-05-16 13:10:47 +0000714func extractSingleOptionalOutputPath(dep android.Module) (android.OptionalPath, error) {
715 var paths android.Paths
716 if sourceFileProducer, ok := dep.(android.SourceFileProducer); ok {
717 paths = sourceFileProducer.Srcs()
718 } else {
719 return android.OptionalPath{}, fmt.Errorf("module %q does not produce source files", dep)
720 }
721 if len(paths) != 1 {
722 return android.OptionalPath{}, fmt.Errorf("expected one path from %q, got %q", dep, paths)
723 }
724 return android.OptionalPathForPath(paths[0]), nil
725}
726
727func (paths *scopePaths) extractLatestApiPath(ctx android.ModuleContext, dep android.Module) error {
728 outputPath, err := extractSingleOptionalOutputPath(dep)
729 paths.latestApiPath = outputPath
730 return err
731}
732
733func (paths *scopePaths) extractLatestRemovedApiPath(ctx android.ModuleContext, dep android.Module) error {
734 outputPath, err := extractSingleOptionalOutputPath(dep)
735 paths.latestRemovedApiPath = outputPath
736 return err
737}
738
Paul Duffin0ff08bd2020-04-29 13:30:54 +0100739type commonToSdkLibraryAndImportProperties struct {
Paul Duffin1b1e8062020-05-08 13:44:43 +0100740 // The naming scheme to use for the components that this module creates.
741 //
Paul Duffinee9ad5d2020-09-11 13:04:05 +0100742 // If not specified then it defaults to "default".
Paul Duffin1b1e8062020-05-08 13:44:43 +0100743 //
744 // This is a temporary mechanism to simplify conversion from separate modules for each
745 // component that follow a different naming pattern to the default one.
746 //
747 // TODO(b/155480189) - Remove once naming inconsistencies have been resolved.
Paul Duffin0ff08bd2020-04-29 13:30:54 +0100748 Naming_scheme *string
Paul Duffindfa131e2020-05-15 20:37:11 +0100749
750 // Specifies whether this module can be used as an Android shared library; defaults
751 // to true.
752 //
753 // An Android shared library is one that can be referenced in a <uses-library> element
754 // in an AndroidManifest.xml.
755 Shared_library *bool
Paul Duffina2ae7e02020-09-11 11:55:00 +0100756
757 // Files containing information about supported java doc tags.
758 Doctag_files []string `android:"path"`
Pedro Loureiro9956e5e2021-09-07 17:21:59 +0000759
760 // Signals that this shared library is part of the bootclasspath starting
761 // on the version indicated in this attribute.
762 //
763 // This will make platforms at this level and above to ignore
764 // <uses-library> tags with this library name because the library is already
765 // available
766 On_bootclasspath_since *string
767
768 // Signals that this shared library was part of the bootclasspath before
769 // (but not including) the version indicated in this attribute.
770 //
771 // The system will automatically add a <uses-library> tag with this library to
772 // apps that target any SDK less than the version indicated in this attribute.
773 On_bootclasspath_before *string
774
775 // Indicates that PackageManager should ignore this shared library if the
776 // platform is below the version indicated in this attribute.
777 //
778 // This means that the device won't recognise this library as installed.
779 Min_device_sdk *string
780
781 // Indicates that PackageManager should ignore this shared library if the
782 // platform is above the version indicated in this attribute.
783 //
784 // This means that the device won't recognise this library as installed.
785 Max_device_sdk *string
Paul Duffin0ff08bd2020-04-29 13:30:54 +0100786}
787
Paul Duffin71b33cc2021-06-23 11:39:47 +0100788// commonSdkLibraryAndImportModule defines the interface that must be provided by a module that
789// embeds the commonToSdkLibraryAndImport struct.
790type commonSdkLibraryAndImportModule interface {
Paul Duffind796f6f2022-11-23 23:06:05 +0000791 android.Module
Paul Duffin71b33cc2021-06-23 11:39:47 +0100792
793 BaseModuleName() string
794}
795
Paul Duffin56d44902020-01-31 13:36:25 +0000796// Common code between sdk library and sdk library import
797type commonToSdkLibraryAndImport struct {
Paul Duffin71b33cc2021-06-23 11:39:47 +0100798 module commonSdkLibraryAndImportModule
Paul Duffinc3091c82020-05-08 14:16:20 +0100799
Paul Duffin56d44902020-01-31 13:36:25 +0000800 scopePaths map[*apiScope]*scopePaths
Paul Duffin1b1e8062020-05-08 13:44:43 +0100801
802 namingScheme sdkLibraryComponentNamingScheme
803
Paul Duffindfa131e2020-05-15 20:37:11 +0100804 commonSdkLibraryProperties commonToSdkLibraryAndImportProperties
Paul Duffin859fe962020-05-15 10:20:31 +0100805
Paul Duffina2ae7e02020-09-11 11:55:00 +0100806 // Paths to commonSdkLibraryProperties.Doctag_files
807 doctagPaths android.Paths
808
Paul Duffin859fe962020-05-15 10:20:31 +0100809 // Functionality related to this being used as a component of a java_sdk_library.
810 EmbeddableSdkLibraryComponent
Paul Duffin56d44902020-01-31 13:36:25 +0000811}
812
Paul Duffin71b33cc2021-06-23 11:39:47 +0100813func (c *commonToSdkLibraryAndImport) initCommon(module commonSdkLibraryAndImportModule) {
814 c.module = module
Paul Duffin1b1e8062020-05-08 13:44:43 +0100815
Paul Duffin71b33cc2021-06-23 11:39:47 +0100816 module.AddProperties(&c.commonSdkLibraryProperties)
Paul Duffin859fe962020-05-15 10:20:31 +0100817
818 // Initialize this as an sdk library component.
Paul Duffin71b33cc2021-06-23 11:39:47 +0100819 c.initSdkLibraryComponent(module)
Paul Duffin1b1e8062020-05-08 13:44:43 +0100820}
821
822func (c *commonToSdkLibraryAndImport) initCommonAfterDefaultsApplied(ctx android.DefaultableHookContext) bool {
Paul Duffindfa131e2020-05-15 20:37:11 +0100823 schemeProperty := proptools.StringDefault(c.commonSdkLibraryProperties.Naming_scheme, "default")
Paul Duffin1b1e8062020-05-08 13:44:43 +0100824 switch schemeProperty {
825 case "default":
826 c.namingScheme = &defaultNamingScheme{}
827 default:
828 ctx.PropertyErrorf("naming_scheme", "expected 'default' but was %q", schemeProperty)
829 return false
830 }
831
Paul Duffin3f0290e2021-06-30 18:25:36 +0100832 namePtr := proptools.StringPtr(c.module.BaseModuleName())
833 c.sdkLibraryComponentProperties.SdkLibraryName = namePtr
834
Paul Duffindfa131e2020-05-15 20:37:11 +0100835 // Only track this sdk library if this can be used as a shared library.
836 if c.sharedLibrary() {
837 // Use the name specified in the module definition as the owner.
Paul Duffin3f0290e2021-06-30 18:25:36 +0100838 c.sdkLibraryComponentProperties.SdkLibraryToImplicitlyTrack = namePtr
Paul Duffindfa131e2020-05-15 20:37:11 +0100839 }
Paul Duffin859fe962020-05-15 10:20:31 +0100840
Paul Duffin1b1e8062020-05-08 13:44:43 +0100841 return true
Paul Duffinc3091c82020-05-08 14:16:20 +0100842}
843
Paul Duffinea8f8082021-06-24 13:25:57 +0100844// uniqueApexVariations provides common implementation of the ApexModule.UniqueApexVariations
845// method.
846func (c *commonToSdkLibraryAndImport) uniqueApexVariations() bool {
847 // A java_sdk_library that is a shared library produces an XML file that makes the shared library
848 // usable from an AndroidManifest.xml's <uses-library> entry. That XML file contains the name of
849 // the APEX and so it needs a unique variation per APEX.
850 return c.sharedLibrary()
851}
852
Paul Duffina2ae7e02020-09-11 11:55:00 +0100853func (c *commonToSdkLibraryAndImport) generateCommonBuildActions(ctx android.ModuleContext) {
854 c.doctagPaths = android.PathsForModuleSrc(ctx, c.commonSdkLibraryProperties.Doctag_files)
855}
856
Paul Duffineedc5d52020-06-12 17:46:39 +0100857// Module name of the runtime implementation library
858func (c *commonToSdkLibraryAndImport) implLibraryModuleName() string {
Paul Duffin71b33cc2021-06-23 11:39:47 +0100859 return c.module.BaseModuleName() + ".impl"
Paul Duffineedc5d52020-06-12 17:46:39 +0100860}
861
862// Module name of the XML file for the lib
863func (c *commonToSdkLibraryAndImport) xmlPermissionsModuleName() string {
Paul Duffin71b33cc2021-06-23 11:39:47 +0100864 return c.module.BaseModuleName() + sdkXmlFileSuffix
Paul Duffineedc5d52020-06-12 17:46:39 +0100865}
866
Paul Duffinc3091c82020-05-08 14:16:20 +0100867// Name of the java_library module that compiles the stubs source.
868func (c *commonToSdkLibraryAndImport) stubsLibraryModuleName(apiScope *apiScope) string {
Paul Duffinb97b1572021-04-29 21:50:40 +0100869 baseName := c.module.BaseModuleName()
Paul Duffin21787622022-11-25 12:48:20 +0000870 return c.namingScheme.stubsLibraryModuleName(apiScope, baseName)
Paul Duffinc3091c82020-05-08 14:16:20 +0100871}
872
873// Name of the droidstubs module that generates the stubs source and may also
874// generate/check the API.
875func (c *commonToSdkLibraryAndImport) stubsSourceModuleName(apiScope *apiScope) string {
Paul Duffinb97b1572021-04-29 21:50:40 +0100876 baseName := c.module.BaseModuleName()
Paul Duffin21787622022-11-25 12:48:20 +0000877 return c.namingScheme.stubsSourceModuleName(apiScope, baseName)
Paul Duffinc3091c82020-05-08 14:16:20 +0100878}
879
Jihoon Kang1c92c3e2023-03-23 17:44:51 +0000880// Name of the java_api_library module that generates the from-text stubs source
881// and compiles to a jar file.
882func (c *commonToSdkLibraryAndImport) apiLibraryModuleName(apiScope *apiScope) string {
883 baseName := c.module.BaseModuleName()
884 return c.namingScheme.apiLibraryModuleName(apiScope, baseName)
885}
886
Jihoon Kang1147b312023-06-08 23:25:57 +0000887// Name of the java_library module that compiles the stubs
888// generated from source Java files.
889func (c *commonToSdkLibraryAndImport) sourceStubLibraryModuleName(apiScope *apiScope) string {
890 baseName := c.module.BaseModuleName()
891 return c.namingScheme.sourceStubLibraryModuleName(apiScope, baseName)
892}
893
Paul Duffin46dc45a2020-05-14 15:39:10 +0100894// The component names for different outputs of the java_sdk_library.
895//
896// They are similar to the names used for the child modules it creates
897const (
898 stubsSourceComponentName = "stubs.source"
899
900 apiTxtComponentName = "api.txt"
901
902 removedApiTxtComponentName = "removed-api.txt"
Anton Hanssond78eb762021-09-21 15:25:12 +0100903
904 annotationsComponentName = "annotations.zip"
Paul Duffin46dc45a2020-05-14 15:39:10 +0100905)
906
907// A regular expression to match tags that reference a specific stubs component.
908//
909// It will only match if given a valid scope and a valid component. It is verfy strict
910// to ensure it does not accidentally match a similar looking tag that should be processed
911// by the embedded Library.
912var tagSplitter = func() *regexp.Regexp {
913 // Given a list of literal string items returns a regular expression that will
914 // match any one of the items.
915 choice := func(items ...string) string {
916 return `\Q` + strings.Join(items, `\E|\Q`) + `\E`
917 }
918
919 // Regular expression to match one of the scopes.
920 scopesRegexp := choice(allScopeNames...)
921
922 // Regular expression to match one of the components.
Anton Hanssond78eb762021-09-21 15:25:12 +0100923 componentsRegexp := choice(stubsSourceComponentName, apiTxtComponentName, removedApiTxtComponentName, annotationsComponentName)
Paul Duffin46dc45a2020-05-14 15:39:10 +0100924
925 // Regular expression to match any combination of one scope and one component.
926 return regexp.MustCompile(fmt.Sprintf(`^\.(%s)\.(%s)$`, scopesRegexp, componentsRegexp))
927}()
928
929// For OutputFileProducer interface
930//
Anton Hanssond78eb762021-09-21 15:25:12 +0100931// .<scope>.<component name>, for all ComponentNames (for example: .public.removed-api.txt)
Paul Duffin46dc45a2020-05-14 15:39:10 +0100932func (c *commonToSdkLibraryAndImport) commonOutputFiles(tag string) (android.Paths, error) {
933 if groups := tagSplitter.FindStringSubmatch(tag); groups != nil {
934 scopeName := groups[1]
935 component := groups[2]
936
937 if scope, ok := scopeByName[scopeName]; ok {
938 paths := c.findScopePaths(scope)
939 if paths == nil {
Paul Duffin71b33cc2021-06-23 11:39:47 +0100940 return nil, fmt.Errorf("%q does not provide api scope %s", c.module.BaseModuleName(), scopeName)
Paul Duffin46dc45a2020-05-14 15:39:10 +0100941 }
942
943 switch component {
944 case stubsSourceComponentName:
945 if paths.stubsSrcJar.Valid() {
946 return android.Paths{paths.stubsSrcJar.Path()}, nil
947 }
948
949 case apiTxtComponentName:
950 if paths.currentApiFilePath.Valid() {
951 return android.Paths{paths.currentApiFilePath.Path()}, nil
952 }
953
954 case removedApiTxtComponentName:
955 if paths.removedApiFilePath.Valid() {
956 return android.Paths{paths.removedApiFilePath.Path()}, nil
957 }
Anton Hanssond78eb762021-09-21 15:25:12 +0100958
959 case annotationsComponentName:
960 if paths.annotationsZip.Valid() {
961 return android.Paths{paths.annotationsZip.Path()}, nil
962 }
Paul Duffin46dc45a2020-05-14 15:39:10 +0100963 }
964
965 return nil, fmt.Errorf("%s not available for api scope %s", component, scopeName)
966 } else {
967 return nil, fmt.Errorf("unknown scope %s in %s", scope, tag)
968 }
969
970 } else {
Paul Duffina2ae7e02020-09-11 11:55:00 +0100971 switch tag {
972 case ".doctags":
973 if c.doctagPaths != nil {
974 return c.doctagPaths, nil
975 } else {
Paul Duffin71b33cc2021-06-23 11:39:47 +0100976 return nil, fmt.Errorf("no doctag_files specified on %s", c.module.BaseModuleName())
Paul Duffina2ae7e02020-09-11 11:55:00 +0100977 }
978 }
Paul Duffin46dc45a2020-05-14 15:39:10 +0100979 return nil, nil
980 }
981}
982
Paul Duffin803a9562020-05-20 11:52:25 +0100983func (c *commonToSdkLibraryAndImport) getScopePathsCreateIfNeeded(scope *apiScope) *scopePaths {
Paul Duffin56d44902020-01-31 13:36:25 +0000984 if c.scopePaths == nil {
985 c.scopePaths = make(map[*apiScope]*scopePaths)
986 }
987 paths := c.scopePaths[scope]
988 if paths == nil {
989 paths = &scopePaths{}
990 c.scopePaths[scope] = paths
991 }
992
993 return paths
994}
995
Paul Duffin803a9562020-05-20 11:52:25 +0100996func (c *commonToSdkLibraryAndImport) findScopePaths(scope *apiScope) *scopePaths {
997 if c.scopePaths == nil {
998 return nil
999 }
1000
1001 return c.scopePaths[scope]
1002}
1003
1004// If this does not support the requested api scope then find the closest available
1005// scope it does support. Returns nil if no such scope is available.
1006func (c *commonToSdkLibraryAndImport) findClosestScopePath(scope *apiScope) *scopePaths {
Paul Duffind0b9fca2022-09-30 18:11:41 +01001007 for s := scope; s != nil; s = s.canAccess {
Paul Duffin803a9562020-05-20 11:52:25 +01001008 if paths := c.findScopePaths(s); paths != nil {
1009 return paths
1010 }
1011 }
1012
1013 // This should never happen outside tests as public should be the base scope for every
1014 // scope and is enabled by default.
1015 return nil
1016}
1017
Jiyong Parkf1691d22021-03-29 20:11:58 +09001018func (c *commonToSdkLibraryAndImport) selectHeaderJarsForSdkVersion(ctx android.BaseModuleContext, sdkVersion android.SdkSpec) android.Paths {
Paul Duffinb05d4292020-05-20 12:19:10 +01001019
1020 // If a specific numeric version has been requested then use prebuilt versions of the sdk.
Jiyong Park54105c42021-03-31 18:17:53 +09001021 if !sdkVersion.ApiLevel.IsPreview() {
Paul Duffin71b33cc2021-06-23 11:39:47 +01001022 return PrebuiltJars(ctx, c.module.BaseModuleName(), sdkVersion)
Paul Duffinb05d4292020-05-20 12:19:10 +01001023 }
1024
Paul Duffin1267d872021-04-16 17:21:36 +01001025 paths := c.selectScopePaths(ctx, sdkVersion.Kind)
1026 if paths == nil {
1027 return nil
1028 }
1029
1030 return paths.stubsHeaderPath
1031}
1032
1033// selectScopePaths returns the *scopePaths appropriate for the specific kind.
1034//
1035// If the module does not support the specific kind then it will return the *scopePaths for the
1036// closest kind which is a subset of the requested kind. e.g. if requesting android.SdkModule then
1037// it will return *scopePaths for android.SdkSystem if available or android.SdkPublic of not.
1038func (c *commonToSdkLibraryAndImport) selectScopePaths(ctx android.BaseModuleContext, kind android.SdkKind) *scopePaths {
Paul Duffin32cf58a2021-05-18 16:32:50 +01001039 apiScope := sdkKindToApiScope(kind)
Paul Duffinb05d4292020-05-20 12:19:10 +01001040
Paul Duffin803a9562020-05-20 11:52:25 +01001041 paths := c.findClosestScopePath(apiScope)
1042 if paths == nil {
1043 var scopes []string
1044 for _, s := range allApiScopes {
1045 if c.findScopePaths(s) != nil {
1046 scopes = append(scopes, s.name)
1047 }
1048 }
Paul Duffin71b33cc2021-06-23 11:39:47 +01001049 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 +01001050 return nil
1051 }
1052
Paul Duffin1267d872021-04-16 17:21:36 +01001053 return paths
1054}
1055
Paul Duffin32cf58a2021-05-18 16:32:50 +01001056// sdkKindToApiScope maps from android.SdkKind to apiScope.
1057func sdkKindToApiScope(kind android.SdkKind) *apiScope {
1058 var apiScope *apiScope
1059 switch kind {
1060 case android.SdkSystem:
1061 apiScope = apiScopeSystem
1062 case android.SdkModule:
1063 apiScope = apiScopeModuleLib
1064 case android.SdkTest:
1065 apiScope = apiScopeTest
1066 case android.SdkSystemServer:
1067 apiScope = apiScopeSystemServer
1068 default:
1069 apiScope = apiScopePublic
1070 }
1071 return apiScope
1072}
1073
Paul Duffin1267d872021-04-16 17:21:36 +01001074// to satisfy SdkLibraryDependency interface
Martin Stjernholm8be1e6d2021-09-15 03:34:04 +01001075func (c *commonToSdkLibraryAndImport) SdkApiStubDexJar(ctx android.BaseModuleContext, kind android.SdkKind) OptionalDexJarPath {
Paul Duffin1267d872021-04-16 17:21:36 +01001076 paths := c.selectScopePaths(ctx, kind)
1077 if paths == nil {
Martin Stjernholm8be1e6d2021-09-15 03:34:04 +01001078 return makeUnsetDexJarPath()
Paul Duffin1267d872021-04-16 17:21:36 +01001079 }
1080
1081 return paths.stubsDexJarPath
Paul Duffinb05d4292020-05-20 12:19:10 +01001082}
1083
Paul Duffin32cf58a2021-05-18 16:32:50 +01001084// to satisfy SdkLibraryDependency interface
1085func (c *commonToSdkLibraryAndImport) SdkRemovedTxtFile(ctx android.BaseModuleContext, kind android.SdkKind) android.OptionalPath {
1086 apiScope := sdkKindToApiScope(kind)
1087 paths := c.findScopePaths(apiScope)
1088 if paths == nil {
1089 return android.OptionalPath{}
1090 }
1091
1092 return paths.removedApiFilePath
1093}
1094
Paul Duffin859fe962020-05-15 10:20:31 +01001095func (c *commonToSdkLibraryAndImport) sdkComponentPropertiesForChildLibrary() interface{} {
1096 componentProps := &struct {
Paul Duffin3f0290e2021-06-30 18:25:36 +01001097 SdkLibraryName *string
Paul Duffin859fe962020-05-15 10:20:31 +01001098 SdkLibraryToImplicitlyTrack *string
Paul Duffindfa131e2020-05-15 20:37:11 +01001099 }{}
1100
Paul Duffin3f0290e2021-06-30 18:25:36 +01001101 namePtr := proptools.StringPtr(c.module.BaseModuleName())
1102 componentProps.SdkLibraryName = namePtr
1103
Paul Duffindfa131e2020-05-15 20:37:11 +01001104 if c.sharedLibrary() {
Paul Duffin859fe962020-05-15 10:20:31 +01001105 // Mark the stubs library as being components of this java_sdk_library so that
1106 // any app that includes code which depends (directly or indirectly) on the stubs
1107 // library will have the appropriate <uses-library> invocation inserted into its
1108 // manifest if necessary.
Paul Duffin3f0290e2021-06-30 18:25:36 +01001109 componentProps.SdkLibraryToImplicitlyTrack = namePtr
Paul Duffin859fe962020-05-15 10:20:31 +01001110 }
1111
1112 return componentProps
1113}
1114
Paul Duffindfa131e2020-05-15 20:37:11 +01001115func (c *commonToSdkLibraryAndImport) sharedLibrary() bool {
1116 return proptools.BoolDefault(c.commonSdkLibraryProperties.Shared_library, true)
1117}
1118
Paul Duffinf4600f62021-05-13 22:34:45 +01001119// Check if the stub libraries should be compiled for dex
1120func (c *commonToSdkLibraryAndImport) stubLibrariesCompiledForDex() bool {
1121 // Always compile the dex file files for the stub libraries if they will be used on the
1122 // bootclasspath.
1123 return !c.sharedLibrary()
1124}
1125
Paul Duffin859fe962020-05-15 10:20:31 +01001126// Properties related to the use of a module as an component of a java_sdk_library.
1127type SdkLibraryComponentProperties struct {
Paul Duffin3f0290e2021-06-30 18:25:36 +01001128 // The name of the java_sdk_library/_import module.
1129 SdkLibraryName *string `blueprint:"mutated"`
Paul Duffin859fe962020-05-15 10:20:31 +01001130
1131 // The name of the java_sdk_library/_import to add to a <uses-library> entry
1132 // in the AndroidManifest.xml of any Android app that includes code that references
1133 // this module. If not set then no java_sdk_library/_import is tracked.
1134 SdkLibraryToImplicitlyTrack *string `blueprint:"mutated"`
1135}
1136
1137// Structure to be embedded in a module struct that needs to support the
1138// SdkLibraryComponentDependency interface.
1139type EmbeddableSdkLibraryComponent struct {
1140 sdkLibraryComponentProperties SdkLibraryComponentProperties
1141}
1142
Paul Duffin71b33cc2021-06-23 11:39:47 +01001143func (e *EmbeddableSdkLibraryComponent) initSdkLibraryComponent(module android.Module) {
1144 module.AddProperties(&e.sdkLibraryComponentProperties)
Paul Duffin859fe962020-05-15 10:20:31 +01001145}
1146
1147// to satisfy SdkLibraryComponentDependency
Paul Duffin3f0290e2021-06-30 18:25:36 +01001148func (e *EmbeddableSdkLibraryComponent) SdkLibraryName() *string {
1149 return e.sdkLibraryComponentProperties.SdkLibraryName
1150}
1151
1152// to satisfy SdkLibraryComponentDependency
Ulya Trafimovich39b437b2020-09-23 16:42:35 +01001153func (e *EmbeddableSdkLibraryComponent) OptionalSdkLibraryImplementation() *string {
Ulya Trafimovich78645fb2021-07-16 15:29:25 +01001154 // For shared libraries, this is the same as the SDK library name. If a Java library or app
1155 // depends on a component library (e.g. a stub library) it still needs to know the name of the
1156 // run-time library and the corresponding module that provides the implementation. This name is
1157 // passed to manifest_fixer (to be added to AndroidManifest.xml) and added to CLC (to be used
1158 // in dexpreopt).
1159 //
1160 // For non-shared SDK (component or not) libraries this returns `nil`, as they are not
1161 // <uses-library> and should not be added to the manifest or to CLC.
Ulya Trafimovich39b437b2020-09-23 16:42:35 +01001162 return e.sdkLibraryComponentProperties.SdkLibraryToImplicitlyTrack
1163}
1164
Paul Duffin859fe962020-05-15 10:20:31 +01001165// Implemented by modules that are (or possibly could be) a component of a java_sdk_library
1166// (including the java_sdk_library) itself.
1167type SdkLibraryComponentDependency interface {
Ulya Trafimovich31e444e2020-08-14 17:32:16 +01001168 UsesLibraryDependency
1169
Paul Duffin3f0290e2021-06-30 18:25:36 +01001170 // SdkLibraryName returns the name of the java_sdk_library/_import module.
1171 SdkLibraryName() *string
1172
Ulya Trafimovich39b437b2020-09-23 16:42:35 +01001173 // The name of the implementation library for the optional SDK library or nil, if there isn't one.
1174 OptionalSdkLibraryImplementation() *string
Paul Duffin859fe962020-05-15 10:20:31 +01001175}
1176
1177// Make sure that all the module types that are components of java_sdk_library/_import
1178// and which can be referenced (directly or indirectly) from an android app implement
1179// the SdkLibraryComponentDependency interface.
1180var _ SdkLibraryComponentDependency = (*Library)(nil)
1181var _ SdkLibraryComponentDependency = (*Import)(nil)
1182var _ SdkLibraryComponentDependency = (*SdkLibrary)(nil)
Paul Duffineedc5d52020-06-12 17:46:39 +01001183var _ SdkLibraryComponentDependency = (*SdkLibraryImport)(nil)
Paul Duffin859fe962020-05-15 10:20:31 +01001184
Paul Duffin32cf58a2021-05-18 16:32:50 +01001185// Provides access to sdk_version related files, e.g. header and implementation jars.
Paul Duffin859fe962020-05-15 10:20:31 +01001186type SdkLibraryDependency interface {
1187 SdkLibraryComponentDependency
1188
1189 // Get the header jars appropriate for the supplied sdk_version.
1190 //
1191 // These are turbine generated jars so they only change if the externals of the
1192 // class changes but it does not contain and implementation or JavaDoc.
Jiyong Parkf1691d22021-03-29 20:11:58 +09001193 SdkHeaderJars(ctx android.BaseModuleContext, sdkVersion android.SdkSpec) android.Paths
Paul Duffin859fe962020-05-15 10:20:31 +01001194
1195 // Get the implementation jars appropriate for the supplied sdk version.
1196 //
1197 // These are either the implementation jar for the whole sdk library or the implementation
1198 // jars for the stubs. The latter should only be needed when generating JavaDoc as otherwise
1199 // they are identical to the corresponding header jars.
Jiyong Parkf1691d22021-03-29 20:11:58 +09001200 SdkImplementationJars(ctx android.BaseModuleContext, sdkVersion android.SdkSpec) android.Paths
Paul Duffin1267d872021-04-16 17:21:36 +01001201
1202 // SdkApiStubDexJar returns the dex jar for the stubs. It is needed by the hiddenapi processing
1203 // tool which processes dex files.
Martin Stjernholm8be1e6d2021-09-15 03:34:04 +01001204 SdkApiStubDexJar(ctx android.BaseModuleContext, kind android.SdkKind) OptionalDexJarPath
Paul Duffinf4600f62021-05-13 22:34:45 +01001205
Paul Duffin32cf58a2021-05-18 16:32:50 +01001206 // SdkRemovedTxtFile returns the optional path to the removed.txt file for the specified sdk kind.
1207 SdkRemovedTxtFile(ctx android.BaseModuleContext, kind android.SdkKind) android.OptionalPath
1208
Paul Duffinf4600f62021-05-13 22:34:45 +01001209 // sharedLibrary returns true if this can be used as a shared library.
1210 sharedLibrary() bool
Paul Duffin859fe962020-05-15 10:20:31 +01001211}
1212
Inseob Kimc0907f12019-02-08 21:00:45 +09001213type SdkLibrary struct {
Sundong Ahn054b19a2018-10-19 13:46:09 +09001214 Library
Jiyong Parkc678ad32018-04-10 13:07:10 +09001215
Zi Wangb2179e32023-01-31 15:53:30 -08001216 android.BazelModuleBase
1217
Sundong Ahn054b19a2018-10-19 13:46:09 +09001218 sdkLibraryProperties sdkLibraryProperties
Jiyong Parkc678ad32018-04-10 13:07:10 +09001219
Paul Duffin3375e352020-04-28 10:44:03 +01001220 // Map from api scope to the scope specific property structure.
1221 scopeToProperties map[*apiScope]*ApiScopeProperties
1222
Paul Duffin56d44902020-01-31 13:36:25 +00001223 commonToSdkLibraryAndImport
Jiyong Parkc678ad32018-04-10 13:07:10 +09001224}
1225
Inseob Kimc0907f12019-02-08 21:00:45 +09001226var _ SdkLibraryDependency = (*SdkLibrary)(nil)
Colin Cross897d2ed2019-02-11 14:03:51 -08001227
Paul Duffin3375e352020-04-28 10:44:03 +01001228func (module *SdkLibrary) generateTestAndSystemScopesByDefault() bool {
1229 return module.sdkLibraryProperties.Generate_system_and_test_apis
1230}
1231
1232func (module *SdkLibrary) getGeneratedApiScopes(ctx android.EarlyModuleContext) apiScopes {
1233 // Check to see if any scopes have been explicitly enabled. If any have then all
1234 // must be.
1235 anyScopesExplicitlyEnabled := false
1236 for _, scope := range allApiScopes {
1237 scopeProperties := module.scopeToProperties[scope]
1238 if scopeProperties.Enabled != nil {
1239 anyScopesExplicitlyEnabled = true
1240 break
1241 }
Paul Duffind1b3a922020-01-22 11:57:20 +00001242 }
Paul Duffin3375e352020-04-28 10:44:03 +01001243
1244 var generatedScopes apiScopes
1245 enabledScopes := make(map[*apiScope]struct{})
1246 for _, scope := range allApiScopes {
1247 scopeProperties := module.scopeToProperties[scope]
1248 // If any scopes are explicitly enabled then ignore the legacy enabled status.
1249 // This is to ensure that any new usages of this module type do not rely on legacy
1250 // behaviour.
1251 defaultEnabledStatus := false
1252 if anyScopesExplicitlyEnabled {
1253 defaultEnabledStatus = scope.defaultEnabledStatus
1254 } else {
1255 defaultEnabledStatus = scope.legacyEnabledStatus(module)
1256 }
1257 enabled := proptools.BoolDefault(scopeProperties.Enabled, defaultEnabledStatus)
1258 if enabled {
1259 enabledScopes[scope] = struct{}{}
1260 generatedScopes = append(generatedScopes, scope)
1261 }
1262 }
1263
1264 // Now check to make sure that any scope that is extended by an enabled scope is also
1265 // enabled.
1266 for _, scope := range allApiScopes {
1267 if _, ok := enabledScopes[scope]; ok {
1268 extends := scope.extends
1269 if extends != nil {
1270 if _, ok := enabledScopes[extends]; !ok {
1271 ctx.ModuleErrorf("enabled api scope %q depends on disabled scope %q", scope, extends)
1272 }
1273 }
1274 }
1275 }
1276
1277 return generatedScopes
Paul Duffind1b3a922020-01-22 11:57:20 +00001278}
1279
satayev758968a2021-12-06 11:42:40 +00001280var _ android.ModuleWithMinSdkVersionCheck = (*SdkLibrary)(nil)
1281
satayev8f088b02021-12-06 11:40:46 +00001282func (module *SdkLibrary) CheckMinSdkVersion(ctx android.ModuleContext) {
Spandan Das8c9ae7e2023-03-03 21:20:36 +00001283 android.CheckMinSdkVersion(ctx, module.MinSdkVersion(ctx), func(c android.ModuleContext, do android.PayloadDepsCallback) {
satayev8f088b02021-12-06 11:40:46 +00001284 ctx.WalkDeps(func(child android.Module, parent android.Module) bool {
1285 isExternal := !module.depIsInSameApex(ctx, child)
1286 if am, ok := child.(android.ApexModule); ok {
1287 if !do(ctx, parent, am, isExternal) {
1288 return false
1289 }
1290 }
1291 return !isExternal
1292 })
1293 })
1294}
1295
Paul Duffineedc5d52020-06-12 17:46:39 +01001296type sdkLibraryComponentTag struct {
1297 blueprint.BaseDependencyTag
1298 name string
1299}
1300
1301// Mark this tag so dependencies that use it are excluded from visibility enforcement.
1302func (t sdkLibraryComponentTag) ExcludeFromVisibilityEnforcement() {}
1303
1304var xmlPermissionsFileTag = sdkLibraryComponentTag{name: "xml-permissions-file"}
Paul Duffine74ac732020-02-06 13:51:46 +00001305
Jiyong Parke3833882020-02-17 17:28:10 +09001306func IsXmlPermissionsFileDepTag(depTag blueprint.DependencyTag) bool {
Paul Duffineedc5d52020-06-12 17:46:39 +01001307 if dt, ok := depTag.(sdkLibraryComponentTag); ok {
Jiyong Parke3833882020-02-17 17:28:10 +09001308 return dt == xmlPermissionsFileTag
1309 }
1310 return false
1311}
1312
Paul Duffineedc5d52020-06-12 17:46:39 +01001313var implLibraryTag = sdkLibraryComponentTag{name: "impl-library"}
Paul Duffin5df79302020-05-16 15:52:12 +01001314
Paul Duffin44f1d842020-06-26 20:17:02 +01001315// Add the dependencies on the child modules in the component deps mutator.
1316func (module *SdkLibrary) ComponentDepsMutator(ctx android.BottomUpMutatorContext) {
Paul Duffin3375e352020-04-28 10:44:03 +01001317 for _, apiScope := range module.getGeneratedApiScopes(ctx) {
Paul Duffind1b3a922020-01-22 11:57:20 +00001318 // Add dependencies to the stubs library
Spandan Das877f39d2023-03-29 16:19:51 +00001319 stubModuleName := module.stubsLibraryModuleName(apiScope)
Jihoon Kang1147b312023-06-08 23:25:57 +00001320
Spandan Das877f39d2023-03-29 16:19:51 +00001321 ctx.AddVariationDependencies(nil, apiScope.stubsTag, stubModuleName)
Paul Duffind1b3a922020-01-22 11:57:20 +00001322
Paul Duffin15f34ef2020-07-20 18:04:44 +01001323 // Add a dependency on the stubs source in order to access both stubs source and api information.
1324 ctx.AddVariationDependencies(nil, apiScope.stubsSourceAndApiTag, module.stubsSourceModuleName(apiScope))
Paul Duffin958806b2022-05-16 13:10:47 +00001325
1326 if module.compareAgainstLatestApi(apiScope) {
1327 // Add dependencies on the latest finalized version of the API .txt file.
1328 latestApiModuleName := module.latestApiModuleName(apiScope)
1329 ctx.AddDependency(module, apiScope.latestApiModuleTag, latestApiModuleName)
1330
1331 // Add dependencies on the latest finalized version of the remove API .txt file.
1332 latestRemovedApiModuleName := module.latestRemovedApiModuleName(apiScope)
1333 ctx.AddDependency(module, apiScope.latestRemovedApiModuleTag, latestRemovedApiModuleName)
1334 }
Sundong Ahn054b19a2018-10-19 13:46:09 +09001335 }
1336
Paul Duffindfa131e2020-05-15 20:37:11 +01001337 if module.requiresRuntimeImplementationLibrary() {
Paul Duffin5df79302020-05-16 15:52:12 +01001338 // Add dependency to the rule for generating the implementation library.
1339 ctx.AddDependency(module, implLibraryTag, module.implLibraryModuleName())
1340
Paul Duffindfa131e2020-05-15 20:37:11 +01001341 if module.sharedLibrary() {
1342 // Add dependency to the rule for generating the xml permissions file
Paul Duffineedc5d52020-06-12 17:46:39 +01001343 ctx.AddDependency(module, xmlPermissionsFileTag, module.xmlPermissionsModuleName())
Paul Duffindfa131e2020-05-15 20:37:11 +01001344 }
Paul Duffin44f1d842020-06-26 20:17:02 +01001345 }
1346}
Paul Duffine74ac732020-02-06 13:51:46 +00001347
Paul Duffin44f1d842020-06-26 20:17:02 +01001348// Add other dependencies as normal.
1349func (module *SdkLibrary) DepsMutator(ctx android.BottomUpMutatorContext) {
Anton Hanssone77fccc2021-01-20 16:52:41 +00001350 var missingApiModules []string
1351 for _, apiScope := range module.getGeneratedApiScopes(ctx) {
1352 if apiScope.unstable {
1353 continue
1354 }
Paul Duffin958806b2022-05-16 13:10:47 +00001355 if m := module.latestApiModuleName(apiScope); !ctx.OtherModuleExists(m) {
Anton Hanssone77fccc2021-01-20 16:52:41 +00001356 missingApiModules = append(missingApiModules, m)
1357 }
Paul Duffin958806b2022-05-16 13:10:47 +00001358 if m := module.latestRemovedApiModuleName(apiScope); !ctx.OtherModuleExists(m) {
Anton Hanssone77fccc2021-01-20 16:52:41 +00001359 missingApiModules = append(missingApiModules, m)
1360 }
Paul Duffin958806b2022-05-16 13:10:47 +00001361 if m := module.latestIncompatibilitiesModuleName(apiScope); !ctx.OtherModuleExists(m) {
Jaewoong Jung1a97ee02021-03-09 13:25:02 -08001362 missingApiModules = append(missingApiModules, m)
1363 }
Anton Hanssone77fccc2021-01-20 16:52:41 +00001364 }
1365 if len(missingApiModules) != 0 && !module.sdkLibraryProperties.Unsafe_ignore_missing_latest_api {
1366 m := module.Name() + " is missing tracking files for previously released library versions.\n"
1367 m += "You need to do one of the following:\n"
1368 m += "- Add `unsafe_ignore_missing_latest_api: true` to your blueprint (to disable compat tracking)\n"
1369 m += "- Add a set of prebuilt txt files representing the last released version of this library for compat checking.\n"
1370 m += " (the current set of API files can be used as a seed for this compatibility tracking\n"
1371 m += "\n"
1372 m += "The following filegroup modules are missing:\n "
1373 m += strings.Join(missingApiModules, "\n ") + "\n"
1374 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."
1375 ctx.ModuleErrorf(m)
1376 }
Paul Duffin44f1d842020-06-26 20:17:02 +01001377 if module.requiresRuntimeImplementationLibrary() {
Paul Duffindfa131e2020-05-15 20:37:11 +01001378 // Only add the deps for the library if it is actually going to be built.
1379 module.Library.deps(ctx)
1380 }
Jiyong Parkc678ad32018-04-10 13:07:10 +09001381}
1382
Paul Duffin46dc45a2020-05-14 15:39:10 +01001383func (module *SdkLibrary) OutputFiles(tag string) (android.Paths, error) {
1384 paths, err := module.commonOutputFiles(tag)
Colin Cross4acaea92021-12-10 23:05:02 +00001385 if paths != nil || err != nil {
Paul Duffin46dc45a2020-05-14 15:39:10 +01001386 return paths, err
1387 }
Colin Cross4acaea92021-12-10 23:05:02 +00001388 if module.requiresRuntimeImplementationLibrary() {
1389 return module.Library.OutputFiles(tag)
1390 }
1391 if tag == "" {
1392 return nil, nil
1393 }
1394 return nil, fmt.Errorf("unsupported module reference tag %q", tag)
Paul Duffin46dc45a2020-05-14 15:39:10 +01001395}
1396
Inseob Kimc0907f12019-02-08 21:00:45 +09001397func (module *SdkLibrary) GenerateAndroidBuildActions(ctx android.ModuleContext) {
satayev8f088b02021-12-06 11:40:46 +00001398 if proptools.String(module.deviceProperties.Min_sdk_version) != "" {
1399 module.CheckMinSdkVersion(ctx)
1400 }
1401
Paul Duffina2ae7e02020-09-11 11:55:00 +01001402 module.generateCommonBuildActions(ctx)
1403
Paul Duffindfa131e2020-05-15 20:37:11 +01001404 // Only build an implementation library if required.
1405 if module.requiresRuntimeImplementationLibrary() {
Paul Duffin43db9be2019-12-30 17:35:49 +00001406 module.Library.GenerateAndroidBuildActions(ctx)
1407 }
Sundong Ahn054b19a2018-10-19 13:46:09 +09001408
Paul Duffinb97b1572021-04-29 21:50:40 +01001409 // Collate the components exported by this module. All scope specific modules are exported but
1410 // the impl and xml component modules are not.
1411 exportedComponents := map[string]struct{}{}
1412
Sundong Ahn57368eb2018-07-06 11:20:23 +09001413 // Record the paths to the header jars of the library (stubs and impl).
Paul Duffind1b3a922020-01-22 11:57:20 +00001414 // When this java_sdk_library is depended upon from others via "libs" property,
Jiyong Parkc678ad32018-04-10 13:07:10 +09001415 // the recorded paths will be returned depending on the link type of the caller.
1416 ctx.VisitDirectDeps(func(to android.Module) {
Jiyong Parkc678ad32018-04-10 13:07:10 +09001417 tag := ctx.OtherModuleDependencyTag(to)
1418
Paul Duffinc8782502020-04-29 20:45:27 +01001419 // Extract information from any of the scope specific dependencies.
1420 if scopeTag, ok := tag.(scopeDependencyTag); ok {
1421 apiScope := scopeTag.apiScope
Paul Duffin803a9562020-05-20 11:52:25 +01001422 scopePaths := module.getScopePathsCreateIfNeeded(apiScope)
Paul Duffinc8782502020-04-29 20:45:27 +01001423
1424 // Extract information from the dependency. The exact information extracted
1425 // is determined by the nature of the dependency which is determined by the tag.
1426 scopeTag.extractDepInfo(ctx, to, scopePaths)
Paul Duffinb97b1572021-04-29 21:50:40 +01001427
1428 exportedComponents[ctx.OtherModuleName(to)] = struct{}{}
Sundong Ahn20e998b2018-07-24 11:19:26 +09001429 }
Jiyong Parkc678ad32018-04-10 13:07:10 +09001430 })
Paul Duffinb97b1572021-04-29 21:50:40 +01001431
1432 // Make the set of components exported by this module available for use elsewhere.
Cole Faust18994c72023-02-28 16:02:16 -08001433 exportedComponentInfo := android.ExportedComponentsInfo{Components: android.SortedKeys(exportedComponents)}
Paul Duffinb97b1572021-04-29 21:50:40 +01001434 ctx.SetProvider(android.ExportedComponentsInfoProvider, exportedComponentInfo)
Paul Duffin958806b2022-05-16 13:10:47 +00001435
1436 // Provide additional information for inclusion in an sdk's generated .info file.
1437 additionalSdkInfo := map[string]interface{}{}
1438 additionalSdkInfo["dist_stem"] = module.distStem()
Paul Duffine8409952022-09-22 16:24:46 +01001439 baseModuleName := module.distStem()
Paul Duffin958806b2022-05-16 13:10:47 +00001440 scopes := map[string]interface{}{}
1441 additionalSdkInfo["scopes"] = scopes
1442 for scope, scopePaths := range module.scopePaths {
1443 scopeInfo := map[string]interface{}{}
1444 scopes[scope.name] = scopeInfo
1445 scopeInfo["current_api"] = scope.snapshotRelativeCurrentApiTxtPath(baseModuleName)
1446 scopeInfo["removed_api"] = scope.snapshotRelativeRemovedApiTxtPath(baseModuleName)
1447 if p := scopePaths.latestApiPath; p.Valid() {
1448 scopeInfo["latest_api"] = p.Path().String()
1449 }
1450 if p := scopePaths.latestRemovedApiPath; p.Valid() {
1451 scopeInfo["latest_removed_api"] = p.Path().String()
1452 }
1453 }
1454 ctx.SetProvider(android.AdditionalSdkInfoProvider, android.AdditionalSdkInfo{additionalSdkInfo})
Jiyong Parkc678ad32018-04-10 13:07:10 +09001455}
1456
Jiyong Park0b0e1b92019-12-03 13:24:29 +09001457func (module *SdkLibrary) AndroidMkEntries() []android.AndroidMkEntries {
Paul Duffindfa131e2020-05-15 20:37:11 +01001458 if !module.requiresRuntimeImplementationLibrary() {
Paul Duffin43db9be2019-12-30 17:35:49 +00001459 return nil
1460 }
Jiyong Park0b0e1b92019-12-03 13:24:29 +09001461 entriesList := module.Library.AndroidMkEntries()
Yo Chiang07d75072020-06-05 17:43:19 +08001462 if module.sharedLibrary() {
1463 entries := &entriesList[0]
1464 entries.Required = append(entries.Required, module.xmlPermissionsModuleName())
1465 }
Jiyong Park0b0e1b92019-12-03 13:24:29 +09001466 return entriesList
Jiyong Park82484c02018-04-23 21:41:26 +09001467}
1468
Anton Hansson5fd5d242020-03-27 19:43:19 +00001469// The dist path of the stub artifacts
1470func (module *SdkLibrary) apiDistPath(apiScope *apiScope) string {
Colin Crossf0eace92021-06-02 13:02:23 -07001471 return path.Join("apistubs", module.distGroup(), apiScope.name)
Anton Hansson5fd5d242020-03-27 19:43:19 +00001472}
1473
Paul Duffin12ceb462019-12-24 20:31:31 +00001474// Get the sdk version for use when compiling the stubs library.
Paul Duffin780c5f42020-05-12 15:52:55 +01001475func (module *SdkLibrary) sdkVersionForStubsLibrary(mctx android.EarlyModuleContext, apiScope *apiScope) string {
Paul Duffin87a05a32020-05-12 11:50:28 +01001476 scopeProperties := module.scopeToProperties[apiScope]
1477 if scopeProperties.Sdk_version != nil {
1478 return proptools.String(scopeProperties.Sdk_version)
1479 }
1480
Jiyong Parkf1691d22021-03-29 20:11:58 +09001481 sdkDep := decodeSdkDep(mctx, android.SdkContext(&module.Library))
Paul Duffin12ceb462019-12-24 20:31:31 +00001482 if sdkDep.hasStandardLibs() {
1483 // If building against a standard sdk then use the sdk version appropriate for the scope.
Paul Duffind1b3a922020-01-22 11:57:20 +00001484 return apiScope.sdkVersion
Paul Duffin12ceb462019-12-24 20:31:31 +00001485 } else {
1486 // Otherwise, use no system module.
1487 return "none"
1488 }
1489}
1490
Paul Duffin31310252020-11-20 21:26:20 +00001491func (module *SdkLibrary) distStem() string {
1492 return proptools.StringDefault(module.sdkLibraryProperties.Dist_stem, module.BaseModuleName())
1493}
1494
Colin Cross986b69a2021-06-01 13:13:40 -07001495// distGroup returns the subdirectory of the dist path of the stub artifacts.
1496func (module *SdkLibrary) distGroup() string {
Colin Cross59b92bf2021-06-01 14:07:56 -07001497 return proptools.StringDefault(module.sdkLibraryProperties.Dist_group, "unknown")
Colin Cross986b69a2021-06-01 13:13:40 -07001498}
1499
Paul Duffin958806b2022-05-16 13:10:47 +00001500func latestPrebuiltApiModuleName(name string, apiScope *apiScope) string {
1501 return PrebuiltApiModuleName(name, apiScope.name, "latest")
1502}
1503
Paul Duffind1b3a922020-01-22 11:57:20 +00001504func (module *SdkLibrary) latestApiFilegroupName(apiScope *apiScope) string {
Paul Duffin958806b2022-05-16 13:10:47 +00001505 return ":" + module.latestApiModuleName(apiScope)
1506}
1507
1508func (module *SdkLibrary) latestApiModuleName(apiScope *apiScope) string {
1509 return latestPrebuiltApiModuleName(module.distStem(), apiScope)
Jiyong Park58c518b2018-05-12 22:29:12 +09001510}
Jiyong Parkc678ad32018-04-10 13:07:10 +09001511
Paul Duffind1b3a922020-01-22 11:57:20 +00001512func (module *SdkLibrary) latestRemovedApiFilegroupName(apiScope *apiScope) string {
Paul Duffin958806b2022-05-16 13:10:47 +00001513 return ":" + module.latestRemovedApiModuleName(apiScope)
1514}
1515
1516func (module *SdkLibrary) latestRemovedApiModuleName(apiScope *apiScope) string {
1517 return latestPrebuiltApiModuleName(module.distStem()+"-removed", apiScope)
Jiyong Parkc678ad32018-04-10 13:07:10 +09001518}
1519
Jaewoong Jung1a97ee02021-03-09 13:25:02 -08001520func (module *SdkLibrary) latestIncompatibilitiesFilegroupName(apiScope *apiScope) string {
Paul Duffin958806b2022-05-16 13:10:47 +00001521 return ":" + module.latestIncompatibilitiesModuleName(apiScope)
1522}
1523
1524func (module *SdkLibrary) latestIncompatibilitiesModuleName(apiScope *apiScope) string {
1525 return latestPrebuiltApiModuleName(module.distStem()+"-incompatibilities", apiScope)
Jaewoong Jung1a97ee02021-03-09 13:25:02 -08001526}
1527
Jihoon Kang1c92c3e2023-03-23 17:44:51 +00001528func (module *SdkLibrary) contributesToApiSurface(c android.Config) bool {
1529 _, exists := c.GetApiLibraries()[module.Name()]
1530 return exists
1531}
1532
Jihoon Kang0c705a42023-08-02 06:44:57 +00001533// The listed modules are the special java_sdk_libraries where apiScope.kind do not match the
1534// api surface that the module contribute to. For example, the public droidstubs and java_library
1535// do not contribute to the public api surface, but contributes to the core platform api surface.
1536// This method returns the full api surface stub lib that
1537// the generated java_api_library should depend on.
1538func (module *SdkLibrary) alternativeFullApiSurfaceStubLib() string {
1539 if val, ok := apiLibraryAdditionalProperties[module.Name()]; ok {
1540 return val.FullApiSurfaceStubLib
1541 }
1542 return ""
1543}
1544
1545// The listed modules' stubs contents do not match the corresponding txt files,
1546// but require additional api contributions to generate the full stubs.
1547// This method returns the name of the additional api contribution module
1548// for corresponding sdk_library modules.
1549func (module *SdkLibrary) apiLibraryAdditionalApiContribution() string {
1550 if val, ok := apiLibraryAdditionalProperties[module.Name()]; ok {
1551 return val.AdditionalApiContribution
1552 }
1553 return ""
1554}
1555
Anton Hansson944e77d2020-08-19 11:40:22 +01001556func childModuleVisibility(childVisibility []string) []string {
1557 if childVisibility == nil {
1558 // No child visibility set. The child will use the visibility of the sdk_library.
1559 return nil
1560 }
1561
1562 // Prepend an override to ignore the sdk_library's visibility, and rely on the child visibility.
1563 var visibility []string
1564 visibility = append(visibility, "//visibility:override")
1565 visibility = append(visibility, childVisibility...)
1566 return visibility
1567}
1568
Paul Duffin5df79302020-05-16 15:52:12 +01001569// Creates the implementation java library
1570func (module *SdkLibrary) createImplLibrary(mctx android.DefaultableHookContext) {
Anton Hansson944e77d2020-08-19 11:40:22 +01001571 visibility := childModuleVisibility(module.sdkLibraryProperties.Impl_library_visibility)
1572
Paul Duffin5df79302020-05-16 15:52:12 +01001573 props := struct {
Paul Duffin77590a82022-04-28 14:13:30 +00001574 Name *string
1575 Visibility []string
1576 Instrument bool
1577 Libs []string
1578 Static_libs []string
1579 Apex_available []string
Paul Duffin5df79302020-05-16 15:52:12 +01001580 }{
1581 Name: proptools.StringPtr(module.implLibraryModuleName()),
Anton Hansson944e77d2020-08-19 11:40:22 +01001582 Visibility: visibility,
Paul Duffin296cf332020-06-18 21:09:55 +01001583 // Set the instrument property to ensure it is instrumented when instrumentation is required.
1584 Instrument: true,
Anton Hansson7f66efa2020-10-08 14:47:23 +01001585 // Set the impl_only libs. Note that the module's "Libs" get appended as well, via the
1586 // addition of &module.properties below.
1587 Libs: module.sdkLibraryProperties.Impl_only_libs,
Paul Duffin77590a82022-04-28 14:13:30 +00001588 // Set the impl_only static libs. Note that the module's "static_libs" get appended as well, via the
1589 // addition of &module.properties below.
1590 Static_libs: module.sdkLibraryProperties.Impl_only_static_libs,
1591 // Pass the apex_available settings down so that the impl library can be statically
1592 // embedded within a library that is added to an APEX. Needed for updatable-media.
1593 Apex_available: module.ApexAvailable(),
Paul Duffin5df79302020-05-16 15:52:12 +01001594 }
1595
1596 properties := []interface{}{
1597 &module.properties,
1598 &module.protoProperties,
1599 &module.deviceProperties,
Liz Kammera7a64f32020-07-09 15:16:41 -07001600 &module.dexProperties,
Paul Duffin5df79302020-05-16 15:52:12 +01001601 &module.dexpreoptProperties,
Colin Cross014489c2020-06-02 20:09:13 -07001602 &module.linter.properties,
Paul Duffin5df79302020-05-16 15:52:12 +01001603 &props,
1604 module.sdkComponentPropertiesForChildLibrary(),
1605 }
1606 mctx.CreateModule(LibraryFactory, properties...)
1607}
1608
Jiyong Parkc678ad32018-04-10 13:07:10 +09001609// Creates a static java library that has API stubs
Paul Duffinf0229202020-04-29 16:47:28 +01001610func (module *SdkLibrary) createStubsLibrary(mctx android.DefaultableHookContext, apiScope *apiScope) {
Jiyong Parkc678ad32018-04-10 13:07:10 +09001611 props := struct {
Dan Willemsen9f435972020-05-28 15:28:00 -07001612 Name *string
1613 Visibility []string
1614 Srcs []string
1615 Installable *bool
1616 Sdk_version *string
1617 System_modules *string
1618 Patch_module *string
1619 Libs []string
Anton Hanssondae54cd2021-04-21 16:30:10 +01001620 Static_libs []string
Dan Willemsen9f435972020-05-28 15:28:00 -07001621 Compile_dex *bool
1622 Java_version *string
1623 Openjdk9 struct {
Sundong Ahn054b19a2018-10-19 13:46:09 +09001624 Srcs []string
1625 Javacflags []string
1626 }
Anton Hansson5fd5d242020-03-27 19:43:19 +00001627 Dist struct {
1628 Targets []string
1629 Dest *string
1630 Dir *string
1631 Tag *string
1632 }
Jiyong Parkc678ad32018-04-10 13:07:10 +09001633 }{}
1634
Jihoon Kang1147b312023-06-08 23:25:57 +00001635 props.Name = proptools.StringPtr(module.sourceStubLibraryModuleName(apiScope))
Jihoon Kang786df932023-09-07 01:18:31 +00001636 props.Visibility = []string{"//visibility:override", "//visibility:private"}
Jiyong Parkc678ad32018-04-10 13:07:10 +09001637 // sources are generated from the droiddoc
Paul Duffinc3091c82020-05-08 14:16:20 +01001638 props.Srcs = []string{":" + module.stubsSourceModuleName(apiScope)}
Paul Duffin12ceb462019-12-24 20:31:31 +00001639 sdkVersion := module.sdkVersionForStubsLibrary(mctx, apiScope)
Paul Duffin52d398a2019-06-11 12:31:14 +01001640 props.Sdk_version = proptools.StringPtr(sdkVersion)
Paul Duffina18abc22020-05-16 18:54:24 +01001641 props.System_modules = module.deviceProperties.System_modules
1642 props.Patch_module = module.properties.Patch_module
Paul Duffin367ab912019-12-23 19:40:36 +00001643 props.Installable = proptools.BoolPtr(false)
Sundong Ahn054b19a2018-10-19 13:46:09 +09001644 props.Libs = module.sdkLibraryProperties.Stub_only_libs
Anton Hanssondae54cd2021-04-21 16:30:10 +01001645 props.Static_libs = module.sdkLibraryProperties.Stub_only_static_libs
Paul Duffine22c2ab2020-05-20 19:35:27 +01001646 // The stub-annotations library contains special versions of the annotations
1647 // with CLASS retention policy, so that they're kept.
1648 if proptools.Bool(module.sdkLibraryProperties.Annotations_enabled) {
1649 props.Libs = append(props.Libs, "stub-annotations")
1650 }
Paul Duffina18abc22020-05-16 18:54:24 +01001651 props.Openjdk9.Srcs = module.properties.Openjdk9.Srcs
1652 props.Openjdk9.Javacflags = module.properties.Openjdk9.Javacflags
Anton Hansson83509b52020-05-21 09:21:57 +01001653 // We compile the stubs for 1.8 in line with the main android.jar stubs, and potential
1654 // interop with older developer tools that don't support 1.9.
1655 props.Java_version = proptools.StringPtr("1.8")
Paul Duffinf4600f62021-05-13 22:34:45 +01001656
Paul Duffin859fe962020-05-15 10:20:31 +01001657 mctx.CreateModule(LibraryFactory, &props, module.sdkComponentPropertiesForChildLibrary())
Jiyong Parkc678ad32018-04-10 13:07:10 +09001658}
1659
Paul Duffin6d0886e2020-04-07 18:49:53 +01001660// Creates a droidstubs module that creates stubs source files from the given full source
Paul Duffinc8782502020-04-29 20:45:27 +01001661// files and also updates and checks the API specification files.
Paul Duffin15f34ef2020-07-20 18:04:44 +01001662func (module *SdkLibrary) createStubsSourcesAndApi(mctx android.DefaultableHookContext, apiScope *apiScope, name string, scopeSpecificDroidstubsArgs []string) {
Jiyong Parkc678ad32018-04-10 13:07:10 +09001663 props := struct {
Sundong Ahn054b19a2018-10-19 13:46:09 +09001664 Name *string
Paul Duffin4911a892020-04-29 23:35:13 +01001665 Visibility []string
Sundong Ahn054b19a2018-10-19 13:46:09 +09001666 Srcs []string
1667 Installable *bool
Paul Duffin52d398a2019-06-11 12:31:14 +01001668 Sdk_version *string
Jihoon Kang3198f3c2023-01-26 08:08:52 +00001669 Api_surface *string
Paul Duffin12ceb462019-12-24 20:31:31 +00001670 System_modules *string
Sundong Ahn054b19a2018-10-19 13:46:09 +09001671 Libs []string
Paul Duffin6877e6d2020-09-25 19:59:14 +01001672 Output_javadoc_comments *bool
Paul Duffin11512472019-02-11 15:55:17 +00001673 Arg_files []string
Sundong Ahn054b19a2018-10-19 13:46:09 +09001674 Args *string
Sundong Ahn054b19a2018-10-19 13:46:09 +09001675 Java_version *string
Paul Duffine22c2ab2020-05-20 19:35:27 +01001676 Annotations_enabled *bool
Sundong Ahn054b19a2018-10-19 13:46:09 +09001677 Merge_annotations_dirs []string
1678 Merge_inclusion_annotations_dirs []string
Paul Duffin0ff08bd2020-04-29 13:30:54 +01001679 Generate_stubs *bool
Anton Hanssone87b03d2020-12-21 15:29:34 +00001680 Previous_api *string
Sundong Ahn054b19a2018-10-19 13:46:09 +09001681 Check_api struct {
Anton Hanssone6056152020-12-31 10:37:27 +00001682 Current ApiToCheck
1683 Last_released ApiToCheck
Paul Duffin160fe412020-05-10 19:32:20 +01001684
1685 Api_lint struct {
1686 Enabled *bool
1687 New_since *string
1688 Baseline_file *string
1689 }
Jiyong Park58c518b2018-05-12 22:29:12 +09001690 }
Sundong Ahn1b92c822018-05-29 11:35:17 +09001691 Aidl struct {
1692 Include_dirs []string
1693 Local_include_dirs []string
1694 }
Paul Duffin040e9062020-11-23 17:41:36 +00001695 Dists []android.Dist
Jiyong Parkc678ad32018-04-10 13:07:10 +09001696 }{}
1697
Paul Duffin7b78b4d2020-04-28 14:08:32 +01001698 // The stubs source processing uses the same compile time classpath when extracting the
1699 // API from the implementation library as it does when compiling it. i.e. the same
1700 // * sdk version
1701 // * system_modules
1702 // * libs (static_libs/libs)
Paul Duffin250e6192019-06-07 10:44:37 +01001703
Paul Duffin0ff08bd2020-04-29 13:30:54 +01001704 props.Name = proptools.StringPtr(name)
Anton Hansson944e77d2020-08-19 11:40:22 +01001705 props.Visibility = childModuleVisibility(module.sdkLibraryProperties.Stubs_source_visibility)
Paul Duffina18abc22020-05-16 18:54:24 +01001706 props.Srcs = append(props.Srcs, module.properties.Srcs...)
Anton Hanssonf8ea3722021-09-16 14:24:13 +01001707 props.Srcs = append(props.Srcs, module.sdkLibraryProperties.Api_srcs...)
Paul Duffina18abc22020-05-16 18:54:24 +01001708 props.Sdk_version = module.deviceProperties.Sdk_version
Jihoon Kang3198f3c2023-01-26 08:08:52 +00001709 props.Api_surface = &apiScope.name
Paul Duffina18abc22020-05-16 18:54:24 +01001710 props.System_modules = module.deviceProperties.System_modules
Jiyong Parkc678ad32018-04-10 13:07:10 +09001711 props.Installable = proptools.BoolPtr(false)
Sundong Ahne6f0b052018-06-05 16:46:14 +09001712 // A droiddoc module has only one Libs property and doesn't distinguish between
1713 // shared libs and static libs. So we need to add both of these libs to Libs property.
Paul Duffina18abc22020-05-16 18:54:24 +01001714 props.Libs = module.properties.Libs
1715 props.Libs = append(props.Libs, module.properties.Static_libs...)
Nikita Ioffed732da72022-11-21 12:38:25 +00001716 props.Libs = append(props.Libs, module.sdkLibraryProperties.Stub_only_libs...)
Paul Duffina18abc22020-05-16 18:54:24 +01001717 props.Aidl.Include_dirs = module.deviceProperties.Aidl.Include_dirs
1718 props.Aidl.Local_include_dirs = module.deviceProperties.Aidl.Local_include_dirs
1719 props.Java_version = module.properties.Java_version
Jiyong Parkc678ad32018-04-10 13:07:10 +09001720
Paul Duffine22c2ab2020-05-20 19:35:27 +01001721 props.Annotations_enabled = module.sdkLibraryProperties.Annotations_enabled
Sundong Ahn054b19a2018-10-19 13:46:09 +09001722 props.Merge_annotations_dirs = module.sdkLibraryProperties.Merge_annotations_dirs
1723 props.Merge_inclusion_annotations_dirs = module.sdkLibraryProperties.Merge_inclusion_annotations_dirs
1724
Paul Duffin6d0886e2020-04-07 18:49:53 +01001725 droidstubsArgs := []string{}
Paul Duffin235ffff2019-12-24 10:41:30 +00001726 if len(module.sdkLibraryProperties.Api_packages) != 0 {
Paul Duffin6d0886e2020-04-07 18:49:53 +01001727 droidstubsArgs = append(droidstubsArgs, "--stub-packages "+strings.Join(module.sdkLibraryProperties.Api_packages, ":"))
Paul Duffin235ffff2019-12-24 10:41:30 +00001728 }
1729 if len(module.sdkLibraryProperties.Hidden_api_packages) != 0 {
Paul Duffin6d0886e2020-04-07 18:49:53 +01001730 droidstubsArgs = append(droidstubsArgs,
Paul Duffin235ffff2019-12-24 10:41:30 +00001731 android.JoinWithPrefix(module.sdkLibraryProperties.Hidden_api_packages, " --hide-package "))
1732 }
Paul Duffin6d0886e2020-04-07 18:49:53 +01001733 droidstubsArgs = append(droidstubsArgs, module.sdkLibraryProperties.Droiddoc_options...)
Paul Duffin235ffff2019-12-24 10:41:30 +00001734 disabledWarnings := []string{
Paul Duffin235ffff2019-12-24 10:41:30 +00001735 "BroadcastBehavior",
Paul Duffin235ffff2019-12-24 10:41:30 +00001736 "DeprecationMismatch",
Anton Hansson3c0779a2022-02-18 19:24:30 +00001737 "HiddenSuperclass",
Paul Duffin235ffff2019-12-24 10:41:30 +00001738 "HiddenTypeParameter",
Anton Hansson3c0779a2022-02-18 19:24:30 +00001739 "MissingPermission",
1740 "SdkConstant",
Paul Duffin235ffff2019-12-24 10:41:30 +00001741 "Todo",
Anton Hansson3c0779a2022-02-18 19:24:30 +00001742 "UnavailableSymbol",
Paul Duffin235ffff2019-12-24 10:41:30 +00001743 }
Paul Duffin6d0886e2020-04-07 18:49:53 +01001744 droidstubsArgs = append(droidstubsArgs, android.JoinWithPrefix(disabledWarnings, "--hide "))
Sundong Ahnfb2721f2018-09-17 13:23:09 +09001745
Paul Duffin6877e6d2020-09-25 19:59:14 +01001746 // Output Javadoc comments for public scope.
1747 if apiScope == apiScopePublic {
1748 props.Output_javadoc_comments = proptools.BoolPtr(true)
1749 }
1750
Paul Duffin1fb487d2020-04-07 18:50:10 +01001751 // Add in scope specific arguments.
Paul Duffin0ff08bd2020-04-29 13:30:54 +01001752 droidstubsArgs = append(droidstubsArgs, scopeSpecificDroidstubsArgs...)
Paul Duffin11512472019-02-11 15:55:17 +00001753 props.Arg_files = module.sdkLibraryProperties.Droiddoc_option_files
Paul Duffin6d0886e2020-04-07 18:49:53 +01001754 props.Args = proptools.StringPtr(strings.Join(droidstubsArgs, " "))
Jiyong Parkc678ad32018-04-10 13:07:10 +09001755
Paul Duffin15f34ef2020-07-20 18:04:44 +01001756 // List of APIs identified from the provided source files are created. They are later
1757 // compared against to the not-yet-released (a.k.a current) list of APIs and to the
1758 // last-released (a.k.a numbered) list of API.
1759 currentApiFileName := apiScope.apiFilePrefix + "current.txt"
1760 removedApiFileName := apiScope.apiFilePrefix + "removed.txt"
1761 apiDir := module.getApiDir()
1762 currentApiFileName = path.Join(apiDir, currentApiFileName)
1763 removedApiFileName = path.Join(apiDir, removedApiFileName)
Jiyong Parkc678ad32018-04-10 13:07:10 +09001764
Paul Duffin15f34ef2020-07-20 18:04:44 +01001765 // check against the not-yet-release API
1766 props.Check_api.Current.Api_file = proptools.StringPtr(currentApiFileName)
1767 props.Check_api.Current.Removed_api_file = proptools.StringPtr(removedApiFileName)
Jiyong Park58c518b2018-05-12 22:29:12 +09001768
Paul Duffin958806b2022-05-16 13:10:47 +00001769 if module.compareAgainstLatestApi(apiScope) {
Paul Duffin15f34ef2020-07-20 18:04:44 +01001770 // check against the latest released API
1771 latestApiFilegroupName := proptools.StringPtr(module.latestApiFilegroupName(apiScope))
Anton Hanssone87b03d2020-12-21 15:29:34 +00001772 props.Previous_api = latestApiFilegroupName
Paul Duffin15f34ef2020-07-20 18:04:44 +01001773 props.Check_api.Last_released.Api_file = latestApiFilegroupName
1774 props.Check_api.Last_released.Removed_api_file = proptools.StringPtr(
1775 module.latestRemovedApiFilegroupName(apiScope))
Jaewoong Jung1a97ee02021-03-09 13:25:02 -08001776 props.Check_api.Last_released.Baseline_file = proptools.StringPtr(
1777 module.latestIncompatibilitiesFilegroupName(apiScope))
Paul Duffin160fe412020-05-10 19:32:20 +01001778
Paul Duffin15f34ef2020-07-20 18:04:44 +01001779 if proptools.Bool(module.sdkLibraryProperties.Api_lint.Enabled) {
1780 // Enable api lint.
1781 props.Check_api.Api_lint.Enabled = proptools.BoolPtr(true)
1782 props.Check_api.Api_lint.New_since = latestApiFilegroupName
Paul Duffin160fe412020-05-10 19:32:20 +01001783
Paul Duffin15f34ef2020-07-20 18:04:44 +01001784 // If it exists then pass a lint-baseline.txt through to droidstubs.
1785 baselinePath := path.Join(apiDir, apiScope.apiFilePrefix+"lint-baseline.txt")
1786 baselinePathRelativeToRoot := path.Join(mctx.ModuleDir(), baselinePath)
1787 paths, err := mctx.GlobWithDeps(baselinePathRelativeToRoot, nil)
1788 if err != nil {
1789 mctx.ModuleErrorf("error checking for presence of %s: %s", baselinePathRelativeToRoot, err)
1790 }
1791 if len(paths) == 1 {
1792 props.Check_api.Api_lint.Baseline_file = proptools.StringPtr(baselinePath)
1793 } else if len(paths) != 0 {
1794 mctx.ModuleErrorf("error checking for presence of %s: expected one path, found: %v", baselinePathRelativeToRoot, paths)
Paul Duffin160fe412020-05-10 19:32:20 +01001795 }
1796 }
Paul Duffin15f34ef2020-07-20 18:04:44 +01001797 }
Jiyong Park58c518b2018-05-12 22:29:12 +09001798
Paul Duffin15f34ef2020-07-20 18:04:44 +01001799 if !Bool(module.sdkLibraryProperties.No_dist) {
Paul Duffin040e9062020-11-23 17:41:36 +00001800 // Dist the api txt and removed api txt artifacts for sdk builds.
1801 distDir := proptools.StringPtr(path.Join(module.apiDistPath(apiScope), "api"))
1802 for _, p := range []struct {
1803 tag string
1804 pattern string
1805 }{
1806 {tag: ".api.txt", pattern: "%s.txt"},
1807 {tag: ".removed-api.txt", pattern: "%s-removed.txt"},
1808 } {
1809 props.Dists = append(props.Dists, android.Dist{
1810 Targets: []string{"sdk", "win_sdk"},
1811 Dir: distDir,
1812 Dest: proptools.StringPtr(fmt.Sprintf(p.pattern, module.distStem())),
1813 Tag: proptools.StringPtr(p.tag),
1814 })
1815 }
Anton Hansson5fd5d242020-03-27 19:43:19 +00001816 }
1817
Jihoon Kangd48abd52023-02-02 22:32:31 +00001818 mctx.CreateModule(DroidstubsFactory, &props).(*Droidstubs).CallHookIfAvailable(mctx)
Jiyong Parkc678ad32018-04-10 13:07:10 +09001819}
1820
Jihoon Kang0c705a42023-08-02 06:44:57 +00001821func (module *SdkLibrary) createApiLibrary(mctx android.DefaultableHookContext, apiScope *apiScope, alternativeFullApiSurfaceStub string) {
Jihoon Kang1c92c3e2023-03-23 17:44:51 +00001822 props := struct {
Jihoon Kangca198c22023-06-22 23:13:51 +00001823 Name *string
1824 Visibility []string
1825 Api_contributions []string
1826 Libs []string
1827 Static_libs []string
1828 Full_api_surface_stub *string
Jihoon Kang1c92c3e2023-03-23 17:44:51 +00001829 }{}
1830
1831 props.Name = proptools.StringPtr(module.apiLibraryModuleName(apiScope))
Jihoon Kang786df932023-09-07 01:18:31 +00001832 props.Visibility = []string{"//visibility:override", "//visibility:private"}
Jihoon Kang1c92c3e2023-03-23 17:44:51 +00001833
1834 apiContributions := []string{}
1835
1836 // Api surfaces are not independent of each other, but have subset relationships,
1837 // and so does the api files. To generate from-text stubs for api surfaces other than public,
1838 // all subset api domains' api_contriubtions must be added as well.
1839 scope := apiScope
1840 for scope != nil {
1841 apiContributions = append(apiContributions, module.stubsSourceModuleName(scope)+".api.contribution")
1842 scope = scope.extends
1843 }
Jihoon Kang0c705a42023-08-02 06:44:57 +00001844 if apiScope == apiScopePublic {
1845 additionalApiContribution := module.apiLibraryAdditionalApiContribution()
1846 if additionalApiContribution != "" {
1847 apiContributions = append(apiContributions, additionalApiContribution)
1848 }
1849 }
Jihoon Kang1c92c3e2023-03-23 17:44:51 +00001850
1851 props.Api_contributions = apiContributions
1852 props.Libs = module.properties.Libs
1853 props.Libs = append(props.Libs, module.sdkLibraryProperties.Stub_only_libs...)
1854 props.Libs = append(props.Libs, "stub-annotations")
1855 props.Static_libs = module.sdkLibraryProperties.Stub_only_static_libs
Jihoon Kange7ee2562023-07-25 05:51:46 +00001856 props.Full_api_surface_stub = proptools.StringPtr(apiScope.kind.DefaultJavaLibraryName())
Jihoon Kang0c705a42023-08-02 06:44:57 +00001857 if alternativeFullApiSurfaceStub != "" {
1858 props.Full_api_surface_stub = proptools.StringPtr(alternativeFullApiSurfaceStub)
1859 }
Jihoon Kang1c92c3e2023-03-23 17:44:51 +00001860
1861 // android_module_lib_stubs_current.from-text only comprises api contributions from art, conscrypt and i18n.
1862 // Thus, replace with android_module_lib_stubs_current_full.from-text, which comprises every api domains.
1863 if apiScope.kind == android.SdkModule {
Jihoon Kangca198c22023-06-22 23:13:51 +00001864 props.Full_api_surface_stub = proptools.StringPtr(apiScope.kind.DefaultJavaLibraryName() + "_full.from-text")
Jihoon Kang1c92c3e2023-03-23 17:44:51 +00001865 }
1866
1867 mctx.CreateModule(ApiLibraryFactory, &props)
1868}
1869
Jihoon Kang1147b312023-06-08 23:25:57 +00001870func (module *SdkLibrary) createTopLevelStubsLibrary(
1871 mctx android.DefaultableHookContext, apiScope *apiScope, contributesToApiSurface bool) {
1872 props := struct {
1873 Name *string
1874 Visibility []string
1875 Sdk_version *string
1876 Static_libs []string
1877 System_modules *string
1878 Dist struct {
1879 Targets []string
1880 Dest *string
1881 Dir *string
1882 Tag *string
1883 }
1884 Compile_dex *bool
1885 }{}
1886 props.Name = proptools.StringPtr(module.stubsLibraryModuleName(apiScope))
1887 props.Visibility = childModuleVisibility(module.sdkLibraryProperties.Stubs_library_visibility)
1888 sdkVersion := module.sdkVersionForStubsLibrary(mctx, apiScope)
1889 props.Sdk_version = proptools.StringPtr(sdkVersion)
1890
1891 // Add the stub compiling java_library/java_api_library as static lib based on build config
1892 staticLib := module.sourceStubLibraryModuleName(apiScope)
1893 if mctx.Config().BuildFromTextStub() && contributesToApiSurface {
1894 staticLib = module.apiLibraryModuleName(apiScope)
1895 }
1896 props.Static_libs = append(props.Static_libs, staticLib)
1897 props.System_modules = module.deviceProperties.System_modules
1898
1899 // Dist the class jar artifact for sdk builds.
1900 if !Bool(module.sdkLibraryProperties.No_dist) {
1901 props.Dist.Targets = []string{"sdk", "win_sdk"}
1902 props.Dist.Dest = proptools.StringPtr(fmt.Sprintf("%v.jar", module.distStem()))
1903 props.Dist.Dir = proptools.StringPtr(module.apiDistPath(apiScope))
1904 props.Dist.Tag = proptools.StringPtr(".jar")
1905 }
1906
1907 // The imports need to be compiled to dex if the java_sdk_library requests it.
1908 compileDex := module.dexProperties.Compile_dex
1909 if module.stubLibrariesCompiledForDex() {
1910 compileDex = proptools.BoolPtr(true)
1911 }
1912 props.Compile_dex = compileDex
1913
1914 mctx.CreateModule(LibraryFactory, &props, module.sdkComponentPropertiesForChildLibrary())
1915}
1916
Paul Duffin958806b2022-05-16 13:10:47 +00001917func (module *SdkLibrary) compareAgainstLatestApi(apiScope *apiScope) bool {
1918 return !(apiScope.unstable || module.sdkLibraryProperties.Unsafe_ignore_missing_latest_api)
1919}
1920
Paul Duffinea8f8082021-06-24 13:25:57 +01001921// Implements android.ApexModule
Jooyung Han5e9013b2020-03-10 06:23:13 +09001922func (module *SdkLibrary) DepIsInSameApex(mctx android.BaseModuleContext, dep android.Module) bool {
1923 depTag := mctx.OtherModuleDependencyTag(dep)
1924 if depTag == xmlPermissionsFileTag {
1925 return true
1926 }
1927 return module.Library.DepIsInSameApex(mctx, dep)
1928}
1929
Paul Duffinea8f8082021-06-24 13:25:57 +01001930// Implements android.ApexModule
1931func (module *SdkLibrary) UniqueApexVariations() bool {
1932 return module.uniqueApexVariations()
1933}
1934
Jiyong Parkc678ad32018-04-10 13:07:10 +09001935// Creates the xml file that publicizes the runtime library
Paul Duffinf0229202020-04-29 16:47:28 +01001936func (module *SdkLibrary) createXmlFile(mctx android.DefaultableHookContext) {
Spandan Das8c9ae7e2023-03-03 21:20:36 +00001937 moduleMinApiLevel := module.Library.MinSdkVersion(mctx)
Pedro Loureiroc3621422021-09-28 15:40:23 +00001938 var moduleMinApiLevelStr = moduleMinApiLevel.String()
1939 if moduleMinApiLevel == android.NoneApiLevel {
1940 moduleMinApiLevelStr = "current"
1941 }
Jiyong Parke3833882020-02-17 17:28:10 +09001942 props := struct {
Pedro Loureiroc3621422021-09-28 15:40:23 +00001943 Name *string
1944 Lib_name *string
1945 Apex_available []string
1946 On_bootclasspath_since *string
1947 On_bootclasspath_before *string
1948 Min_device_sdk *string
1949 Max_device_sdk *string
1950 Sdk_library_min_api_level *string
Jiyong Parke3833882020-02-17 17:28:10 +09001951 }{
Pedro Loureiroc3621422021-09-28 15:40:23 +00001952 Name: proptools.StringPtr(module.xmlPermissionsModuleName()),
1953 Lib_name: proptools.StringPtr(module.BaseModuleName()),
1954 Apex_available: module.ApexProperties.Apex_available,
1955 On_bootclasspath_since: module.commonSdkLibraryProperties.On_bootclasspath_since,
1956 On_bootclasspath_before: module.commonSdkLibraryProperties.On_bootclasspath_before,
1957 Min_device_sdk: module.commonSdkLibraryProperties.Min_device_sdk,
1958 Max_device_sdk: module.commonSdkLibraryProperties.Max_device_sdk,
1959 Sdk_library_min_api_level: &moduleMinApiLevelStr,
Jiyong Parkc678ad32018-04-10 13:07:10 +09001960 }
Jiyong Parke3833882020-02-17 17:28:10 +09001961
Jiyong Parke3833882020-02-17 17:28:10 +09001962 mctx.CreateModule(sdkLibraryXmlFactory, &props)
Jiyong Parkc678ad32018-04-10 13:07:10 +09001963}
1964
Jiyong Parkf1691d22021-03-29 20:11:58 +09001965func PrebuiltJars(ctx android.BaseModuleContext, baseName string, s android.SdkSpec) android.Paths {
Jiyong Park54105c42021-03-31 18:17:53 +09001966 var ver android.ApiLevel
Jiyong Parkf1691d22021-03-29 20:11:58 +09001967 var kind android.SdkKind
1968 if s.UsePrebuilt(ctx) {
Jiyong Park54105c42021-03-31 18:17:53 +09001969 ver = s.ApiLevel
Jiyong Parkf1691d22021-03-29 20:11:58 +09001970 kind = s.Kind
Jiyong Parkc678ad32018-04-10 13:07:10 +09001971 } else {
Jiyong Park6a927c42020-01-21 02:03:43 +09001972 // We don't have prebuilt SDK for the specific sdkVersion.
1973 // Instead of breaking the build, fallback to use "system_current"
Jiyong Park54105c42021-03-31 18:17:53 +09001974 ver = android.FutureApiLevel
Jiyong Parkf1691d22021-03-29 20:11:58 +09001975 kind = android.SdkSystem
Sundong Ahn054b19a2018-10-19 13:46:09 +09001976 }
Jiyong Park6a927c42020-01-21 02:03:43 +09001977
1978 dir := filepath.Join("prebuilts", "sdk", ver.String(), kind.String())
Paul Duffin50061512020-01-21 16:31:05 +00001979 jar := filepath.Join(dir, baseName+".jar")
Sundong Ahn054b19a2018-10-19 13:46:09 +09001980 jarPath := android.ExistentPathForSource(ctx, jar)
Sundong Ahnae418ac2019-02-28 15:01:28 +09001981 if !jarPath.Valid() {
Colin Cross07c88562020-01-07 09:34:44 -08001982 if ctx.Config().AllowMissingDependencies() {
1983 return android.Paths{android.PathForSource(ctx, jar)}
1984 } else {
Jiyong Parkf1691d22021-03-29 20:11:58 +09001985 ctx.PropertyErrorf("sdk_library", "invalid sdk version %q, %q does not exist", s.Raw, jar)
Colin Cross07c88562020-01-07 09:34:44 -08001986 }
Sundong Ahnae418ac2019-02-28 15:01:28 +09001987 return nil
1988 }
Sundong Ahn054b19a2018-10-19 13:46:09 +09001989 return android.Paths{jarPath.Path()}
1990}
1991
Colin Crossaede88c2020-08-11 12:17:01 -07001992// 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 +01001993//
1994// If either this or the other module are on the platform then this will return
1995// false.
Colin Cross56a83212020-09-15 18:30:11 -07001996func withinSameApexesAs(ctx android.BaseModuleContext, other android.Module) bool {
1997 apexInfo := ctx.Provider(android.ApexInfoProvider).(android.ApexInfo)
1998 otherApexInfo := ctx.OtherModuleProvider(other, android.ApexInfoProvider).(android.ApexInfo)
Jiyong Parkab50b072021-05-12 17:13:56 +09001999 return len(otherApexInfo.InApexVariants) > 0 && reflect.DeepEqual(apexInfo.InApexVariants, otherApexInfo.InApexVariants)
Paul Duffin9b879592020-05-26 13:21:35 +01002000}
2001
Jiyong Parkf1691d22021-03-29 20:11:58 +09002002func (module *SdkLibrary) sdkJars(ctx android.BaseModuleContext, sdkVersion android.SdkSpec, headerJars bool) android.Paths {
Jiyong Park932cdfe2020-05-28 00:19:53 +09002003 // If the client doesn't set sdk_version, but if this library prefers stubs over
2004 // the impl library, let's provide the widest API surface possible. To do so,
2005 // force override sdk_version to module_current so that the closest possible API
2006 // surface could be found in selectHeaderJarsForSdkVersion
Jiyong Parkf1691d22021-03-29 20:11:58 +09002007 if module.defaultsToStubs() && !sdkVersion.Specified() {
Jiyong Park92315372021-04-02 08:45:46 +09002008 sdkVersion = android.SdkSpecFrom(ctx, "module_current")
Jiyong Park932cdfe2020-05-28 00:19:53 +09002009 }
Paul Duffind1b3a922020-01-22 11:57:20 +00002010
Paul Duffindaaa3322020-05-26 18:13:57 +01002011 // Only provide access to the implementation library if it is actually built.
2012 if module.requiresRuntimeImplementationLibrary() {
2013 // Check any special cases for java_sdk_library.
2014 //
2015 // Only allow access to the implementation library in the following condition:
2016 // * No sdk_version specified on the referencing module.
Paul Duffin9b879592020-05-26 13:21:35 +01002017 // * The referencing module is in the same apex as this.
Jiyong Parkf1691d22021-03-29 20:11:58 +09002018 if sdkVersion.Kind == android.SdkPrivate || withinSameApexesAs(ctx, module) {
Paul Duffindaaa3322020-05-26 18:13:57 +01002019 if headerJars {
2020 return module.HeaderJars()
2021 } else {
2022 return module.ImplementationJars()
2023 }
Sundong Ahn054b19a2018-10-19 13:46:09 +09002024 }
Jiyong Parkc678ad32018-04-10 13:07:10 +09002025 }
Paul Duffinb05d4292020-05-20 12:19:10 +01002026
Paul Duffin23970f42020-05-20 14:20:02 +01002027 return module.selectHeaderJarsForSdkVersion(ctx, sdkVersion)
Jiyong Parkc678ad32018-04-10 13:07:10 +09002028}
2029
Sundong Ahn241cd372018-07-13 16:16:44 +09002030// to satisfy SdkLibraryDependency interface
Jiyong Parkf1691d22021-03-29 20:11:58 +09002031func (module *SdkLibrary) SdkHeaderJars(ctx android.BaseModuleContext, sdkVersion android.SdkSpec) android.Paths {
Paul Duffind1b3a922020-01-22 11:57:20 +00002032 return module.sdkJars(ctx, sdkVersion, true /*headerJars*/)
2033}
2034
2035// to satisfy SdkLibraryDependency interface
Jiyong Parkf1691d22021-03-29 20:11:58 +09002036func (module *SdkLibrary) SdkImplementationJars(ctx android.BaseModuleContext, sdkVersion android.SdkSpec) android.Paths {
Paul Duffind1b3a922020-01-22 11:57:20 +00002037 return module.sdkJars(ctx, sdkVersion, false /*headerJars*/)
Sundong Ahn241cd372018-07-13 16:16:44 +09002038}
2039
Colin Cross571cccf2019-02-04 11:22:08 -08002040var javaSdkLibrariesKey = android.NewOnceKey("javaSdkLibraries")
2041
Jiyong Park82484c02018-04-23 21:41:26 +09002042func javaSdkLibraries(config android.Config) *[]string {
Colin Cross571cccf2019-02-04 11:22:08 -08002043 return config.Once(javaSdkLibrariesKey, func() interface{} {
Jiyong Park82484c02018-04-23 21:41:26 +09002044 return &[]string{}
2045 }).(*[]string)
2046}
2047
Paul Duffin749f98f2019-12-30 17:23:46 +00002048func (module *SdkLibrary) getApiDir() string {
2049 return proptools.StringDefault(module.sdkLibraryProperties.Api_dir, "api")
2050}
2051
Jiyong Parkc678ad32018-04-10 13:07:10 +09002052// For a java_sdk_library module, create internal modules for stubs, docs,
2053// runtime libs and xml file. If requested, the stubs and docs are created twice
2054// once for public API level and once for system API level
Paul Duffinf0229202020-04-29 16:47:28 +01002055func (module *SdkLibrary) CreateInternalModules(mctx android.DefaultableHookContext) {
2056 // If the module has been disabled then don't create any child modules.
2057 if !module.Enabled() {
2058 return
2059 }
2060
Paul Duffina18abc22020-05-16 18:54:24 +01002061 if len(module.properties.Srcs) == 0 {
Inseob Kimc0907f12019-02-08 21:00:45 +09002062 mctx.PropertyErrorf("srcs", "java_sdk_library must specify srcs")
Jooyung Han58f26ab2019-12-18 15:34:32 +09002063 return
Inseob Kimc0907f12019-02-08 21:00:45 +09002064 }
2065
Paul Duffin37e0b772019-12-30 17:20:10 +00002066 // If this builds against standard libraries (i.e. is not part of the core libraries)
Paul Duffin4f5c1ef2020-11-19 14:53:43 +00002067 // then assume it provides both system and test apis.
Jiyong Parkf1691d22021-03-29 20:11:58 +09002068 sdkDep := decodeSdkDep(mctx, android.SdkContext(&module.Library))
Paul Duffin37e0b772019-12-30 17:20:10 +00002069 hasSystemAndTestApis := sdkDep.hasStandardLibs()
Paul Duffin3375e352020-04-28 10:44:03 +01002070 module.sdkLibraryProperties.Generate_system_and_test_apis = hasSystemAndTestApis
Paul Duffin4f5c1ef2020-11-19 14:53:43 +00002071
Jaewoong Jung18aefc12020-12-21 09:11:10 -08002072 missingCurrentApi := false
Inseob Kim8098faa2019-03-18 10:19:51 +09002073
Paul Duffin3375e352020-04-28 10:44:03 +01002074 generatedScopes := module.getGeneratedApiScopes(mctx)
Paul Duffind1b3a922020-01-22 11:57:20 +00002075
Paul Duffin749f98f2019-12-30 17:23:46 +00002076 apiDir := module.getApiDir()
Paul Duffin3375e352020-04-28 10:44:03 +01002077 for _, scope := range generatedScopes {
Inseob Kim8098faa2019-03-18 10:19:51 +09002078 for _, api := range []string{"current.txt", "removed.txt"} {
Paul Duffind1b3a922020-01-22 11:57:20 +00002079 path := path.Join(mctx.ModuleDir(), apiDir, scope.apiFilePrefix+api)
Inseob Kim8098faa2019-03-18 10:19:51 +09002080 p := android.ExistentPathForSource(mctx, path)
2081 if !p.Valid() {
Colin Cross18f840c2021-05-20 17:56:54 -07002082 if mctx.Config().AllowMissingDependencies() {
2083 mctx.AddMissingDependencies([]string{path})
2084 } else {
2085 mctx.ModuleErrorf("Current api file %#v doesn't exist", path)
2086 missingCurrentApi = true
2087 }
Inseob Kim8098faa2019-03-18 10:19:51 +09002088 }
2089 }
2090 }
2091
Jaewoong Jung18aefc12020-12-21 09:11:10 -08002092 if missingCurrentApi {
Inseob Kim8098faa2019-03-18 10:19:51 +09002093 script := "build/soong/scripts/gen-java-current-api-files.sh"
2094 p := android.ExistentPathForSource(mctx, script)
2095
2096 if !p.Valid() {
2097 panic(fmt.Sprintf("script file %s doesn't exist", script))
2098 }
2099
2100 mctx.ModuleErrorf("One or more current api files are missing. "+
2101 "You can update them by:\n"+
Paul Duffin37e0b772019-12-30 17:20:10 +00002102 "%s %q %s && m update-api",
Paul Duffind1b3a922020-01-22 11:57:20 +00002103 script, filepath.Join(mctx.ModuleDir(), apiDir),
Paul Duffin3375e352020-04-28 10:44:03 +01002104 strings.Join(generatedScopes.Strings(func(s *apiScope) string { return s.apiFilePrefix }), " "))
Inseob Kim8098faa2019-03-18 10:19:51 +09002105 return
2106 }
2107
Paul Duffin3375e352020-04-28 10:44:03 +01002108 for _, scope := range generatedScopes {
Paul Duffin15f34ef2020-07-20 18:04:44 +01002109 // Use the stubs source name for legacy reasons.
2110 module.createStubsSourcesAndApi(mctx, scope, module.stubsSourceModuleName(scope), scope.droidstubsArgs)
Paul Duffin0ff08bd2020-04-29 13:30:54 +01002111
Paul Duffind1b3a922020-01-22 11:57:20 +00002112 module.createStubsLibrary(mctx, scope)
Jihoon Kang1c92c3e2023-03-23 17:44:51 +00002113
Jihoon Kang0c705a42023-08-02 06:44:57 +00002114 alternativeFullApiSurfaceStubLib := ""
2115 if scope == apiScopePublic {
2116 alternativeFullApiSurfaceStubLib = module.alternativeFullApiSurfaceStubLib()
2117 }
2118 contributesToApiSurface := module.contributesToApiSurface(mctx.Config()) || alternativeFullApiSurfaceStubLib != ""
Jihoon Kang1147b312023-06-08 23:25:57 +00002119 if contributesToApiSurface {
Jihoon Kang0c705a42023-08-02 06:44:57 +00002120 module.createApiLibrary(mctx, scope, alternativeFullApiSurfaceStubLib)
Jihoon Kang1c92c3e2023-03-23 17:44:51 +00002121 }
Jihoon Kang1147b312023-06-08 23:25:57 +00002122
2123 module.createTopLevelStubsLibrary(mctx, scope, contributesToApiSurface)
Inseob Kimc0907f12019-02-08 21:00:45 +09002124 }
2125
Paul Duffindfa131e2020-05-15 20:37:11 +01002126 if module.requiresRuntimeImplementationLibrary() {
Paul Duffin5df79302020-05-16 15:52:12 +01002127 // Create child module to create an implementation library.
2128 //
2129 // This temporarily creates a second implementation library that can be explicitly
2130 // referenced.
2131 //
2132 // TODO(b/156618935) - update comment once only one implementation library is created.
2133 module.createImplLibrary(mctx)
2134
Paul Duffindfa131e2020-05-15 20:37:11 +01002135 // Only create an XML permissions file that declares the library as being usable
2136 // as a shared library if required.
2137 if module.sharedLibrary() {
2138 module.createXmlFile(mctx)
2139 }
Paul Duffin43db9be2019-12-30 17:35:49 +00002140
2141 // record java_sdk_library modules so that they are exported to make
2142 javaSdkLibraries := javaSdkLibraries(mctx.Config())
2143 javaSdkLibrariesLock.Lock()
2144 defer javaSdkLibrariesLock.Unlock()
2145 *javaSdkLibraries = append(*javaSdkLibraries, module.BaseModuleName())
2146 }
Anton Hansson7f66efa2020-10-08 14:47:23 +01002147
Paul Duffin77590a82022-04-28 14:13:30 +00002148 // 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 +01002149 module.properties.Libs = append(module.properties.Libs, module.sdkLibraryProperties.Impl_only_libs...)
Paul Duffin77590a82022-04-28 14:13:30 +00002150 module.properties.Static_libs = append(module.properties.Static_libs, module.sdkLibraryProperties.Impl_only_static_libs...)
Inseob Kimc0907f12019-02-08 21:00:45 +09002151}
2152
2153func (module *SdkLibrary) InitSdkLibraryProperties() {
Colin Crossce6734e2020-06-15 16:09:53 -07002154 module.addHostAndDeviceProperties()
2155 module.AddProperties(&module.sdkLibraryProperties)
Sundong Ahn054b19a2018-10-19 13:46:09 +09002156
Paul Duffin71b33cc2021-06-23 11:39:47 +01002157 module.initSdkLibraryComponent(module)
Paul Duffin859fe962020-05-15 10:20:31 +01002158
Paul Duffina18abc22020-05-16 18:54:24 +01002159 module.properties.Installable = proptools.BoolPtr(true)
2160 module.deviceProperties.IsSDKLibrary = true
Inseob Kimc0907f12019-02-08 21:00:45 +09002161}
Sundong Ahn054b19a2018-10-19 13:46:09 +09002162
Paul Duffindfa131e2020-05-15 20:37:11 +01002163func (module *SdkLibrary) requiresRuntimeImplementationLibrary() bool {
2164 return !proptools.Bool(module.sdkLibraryProperties.Api_only)
2165}
2166
Jiyong Park932cdfe2020-05-28 00:19:53 +09002167func (module *SdkLibrary) defaultsToStubs() bool {
2168 return proptools.Bool(module.sdkLibraryProperties.Default_to_stubs)
2169}
2170
Paul Duffin1b1e8062020-05-08 13:44:43 +01002171// Defines how to name the individual component modules the sdk library creates.
2172type sdkLibraryComponentNamingScheme interface {
2173 stubsLibraryModuleName(scope *apiScope, baseName string) string
2174
2175 stubsSourceModuleName(scope *apiScope, baseName string) string
Jihoon Kang1c92c3e2023-03-23 17:44:51 +00002176
2177 apiLibraryModuleName(scope *apiScope, baseName string) string
Jihoon Kang1147b312023-06-08 23:25:57 +00002178
2179 sourceStubLibraryModuleName(scope *apiScope, baseName string) string
Paul Duffin1b1e8062020-05-08 13:44:43 +01002180}
2181
2182type defaultNamingScheme struct {
2183}
2184
2185func (s *defaultNamingScheme) stubsLibraryModuleName(scope *apiScope, baseName string) string {
2186 return scope.stubsLibraryModuleName(baseName)
2187}
2188
2189func (s *defaultNamingScheme) stubsSourceModuleName(scope *apiScope, baseName string) string {
2190 return scope.stubsSourceModuleName(baseName)
2191}
2192
Jihoon Kang1c92c3e2023-03-23 17:44:51 +00002193func (s *defaultNamingScheme) apiLibraryModuleName(scope *apiScope, baseName string) string {
2194 return scope.apiLibraryModuleName(baseName)
2195}
2196
Jihoon Kang1147b312023-06-08 23:25:57 +00002197func (s *defaultNamingScheme) sourceStubLibraryModuleName(scope *apiScope, baseName string) string {
2198 return scope.sourceStubLibraryModuleName(baseName)
2199}
2200
Paul Duffin1b1e8062020-05-08 13:44:43 +01002201var _ sdkLibraryComponentNamingScheme = (*defaultNamingScheme)(nil)
2202
Jaewoong Jungbc15e3a2021-03-10 17:02:43 -08002203func moduleStubLinkType(name string) (stub bool, ret sdkLinkType) {
Jihoon Kang1147b312023-06-08 23:25:57 +00002204 name = strings.TrimSuffix(name, ".from-source")
2205
Anton Hansson2d0c1942020-05-25 12:20:51 +01002206 // This suffix-based approach is fragile and could potentially mis-trigger.
2207 // TODO(b/155164730): Clean this up when modules no longer reference sdk_lib stubs directly.
Anton Hansson08f476b2021-04-07 15:32:19 +01002208 if strings.HasSuffix(name, apiScopePublic.stubsLibraryModuleNameSuffix()) {
2209 if name == "hwbinder.stubs" || name == "libcore_private.stubs" {
2210 // Due to a previous bug, these modules were not considered stubs, so we retain that.
2211 return false, javaPlatform
2212 }
Anton Hansson2d0c1942020-05-25 12:20:51 +01002213 return true, javaSdk
2214 }
Anton Hansson08f476b2021-04-07 15:32:19 +01002215 if strings.HasSuffix(name, apiScopeSystem.stubsLibraryModuleNameSuffix()) {
Anton Hansson2d0c1942020-05-25 12:20:51 +01002216 return true, javaSystem
2217 }
Anton Hansson08f476b2021-04-07 15:32:19 +01002218 if strings.HasSuffix(name, apiScopeModuleLib.stubsLibraryModuleNameSuffix()) {
Anton Hansson2d0c1942020-05-25 12:20:51 +01002219 return true, javaModule
2220 }
Anton Hansson08f476b2021-04-07 15:32:19 +01002221 if strings.HasSuffix(name, apiScopeTest.stubsLibraryModuleNameSuffix()) {
Anton Hansson2d0c1942020-05-25 12:20:51 +01002222 return true, javaSystem
2223 }
Jihoon Kang1147b312023-06-08 23:25:57 +00002224 if strings.HasSuffix(name, apiScopeSystemServer.stubsLibraryModuleNameSuffix()) {
2225 return true, javaSystemServer
2226 }
Anton Hansson2d0c1942020-05-25 12:20:51 +01002227 return false, javaPlatform
2228}
2229
Jaewoong Jung4f158ee2019-07-11 10:05:35 -07002230// java_sdk_library is a special Java library that provides optional platform APIs to apps.
2231// In practice, it can be viewed as a combination of several modules: 1) stubs library that clients
2232// are linked against to, 2) droiddoc module that internally generates API stubs source files,
2233// 3) the real runtime shared library that implements the APIs, and 4) XML file for adding
2234// the runtime lib to the classpath at runtime if requested via <uses-library>.
Inseob Kimc0907f12019-02-08 21:00:45 +09002235func SdkLibraryFactory() android.Module {
2236 module := &SdkLibrary{}
Paul Duffinc3091c82020-05-08 14:16:20 +01002237
2238 // Initialize information common between source and prebuilt.
Paul Duffin71b33cc2021-06-23 11:39:47 +01002239 module.initCommon(module)
Paul Duffinc3091c82020-05-08 14:16:20 +01002240
Inseob Kimc0907f12019-02-08 21:00:45 +09002241 module.InitSdkLibraryProperties()
Jooyung Han58f26ab2019-12-18 15:34:32 +09002242 android.InitApexModule(module)
Sundong Ahn054b19a2018-10-19 13:46:09 +09002243 InitJavaModule(module, android.HostAndDeviceSupported)
Paul Duffin3375e352020-04-28 10:44:03 +01002244
2245 // Initialize the map from scope to scope specific properties.
2246 scopeToProperties := make(map[*apiScope]*ApiScopeProperties)
2247 for _, scope := range allApiScopes {
2248 scopeToProperties[scope] = scope.scopeSpecificProperties(module)
2249 }
2250 module.scopeToProperties = scopeToProperties
2251
Paul Duffin4911a892020-04-29 23:35:13 +01002252 // Add the properties containing visibility rules so that they are checked.
Paul Duffin5df79302020-05-16 15:52:12 +01002253 android.AddVisibilityProperty(module, "impl_library_visibility", &module.sdkLibraryProperties.Impl_library_visibility)
Paul Duffin4911a892020-04-29 23:35:13 +01002254 android.AddVisibilityProperty(module, "stubs_library_visibility", &module.sdkLibraryProperties.Stubs_library_visibility)
2255 android.AddVisibilityProperty(module, "stubs_source_visibility", &module.sdkLibraryProperties.Stubs_source_visibility)
2256
Paul Duffin1b1e8062020-05-08 13:44:43 +01002257 module.SetDefaultableHook(func(ctx android.DefaultableHookContext) {
Paul Duffindfa131e2020-05-15 20:37:11 +01002258 // If no implementation is required then it cannot be used as a shared library
2259 // either.
2260 if !module.requiresRuntimeImplementationLibrary() {
2261 // If shared_library has been explicitly set to true then it is incompatible
2262 // with api_only: true.
2263 if proptools.Bool(module.commonSdkLibraryProperties.Shared_library) {
2264 ctx.PropertyErrorf("api_only/shared_library", "inconsistent settings, shared_library and api_only cannot both be true")
2265 }
2266 // Set shared_library: false.
2267 module.commonSdkLibraryProperties.Shared_library = proptools.BoolPtr(false)
2268 }
2269
Paul Duffin1b1e8062020-05-08 13:44:43 +01002270 if module.initCommonAfterDefaultsApplied(ctx) {
2271 module.CreateInternalModules(ctx)
2272 }
2273 })
Zi Wangb2179e32023-01-31 15:53:30 -08002274 android.InitBazelModule(module)
Jiyong Parkc678ad32018-04-10 13:07:10 +09002275 return module
2276}
Colin Cross79c7c262019-04-17 11:11:46 -07002277
Zi Wangb2179e32023-01-31 15:53:30 -08002278type bazelSdkLibraryAttributes struct {
2279 Public bazel.StringAttribute
2280 System bazel.StringAttribute
2281 Test bazel.StringAttribute
2282 Module_lib bazel.StringAttribute
2283 System_server bazel.StringAttribute
2284}
2285
2286// java_sdk_library bp2build converter
2287func (module *SdkLibrary) ConvertWithBp2build(ctx android.TopDownMutatorContext) {
2288 if ctx.ModuleType() != "java_sdk_library" {
Chris Parsons39a16972023-06-08 14:28:51 +00002289 ctx.MarkBp2buildUnconvertible(bp2build_metrics_proto.UnconvertedReasonType_TYPE_UNSUPPORTED, "")
Zi Wangb2179e32023-01-31 15:53:30 -08002290 return
2291 }
2292
2293 nameToAttr := make(map[string]bazel.StringAttribute)
2294
2295 for _, scope := range module.getGeneratedApiScopes(ctx) {
2296 apiSurfaceFile := path.Join(module.getApiDir(), scope.apiFilePrefix+"current.txt")
2297 var scopeStringAttribute bazel.StringAttribute
2298 scopeStringAttribute.SetValue(apiSurfaceFile)
2299 nameToAttr[scope.name] = scopeStringAttribute
2300 }
2301
2302 attrs := bazelSdkLibraryAttributes{
2303 Public: nameToAttr["public"],
2304 System: nameToAttr["system"],
2305 Test: nameToAttr["test"],
2306 Module_lib: nameToAttr["module-lib"],
2307 System_server: nameToAttr["system-server"],
2308 }
2309 props := bazel.BazelTargetModuleProperties{
2310 Rule_class: "java_sdk_library",
2311 Bzl_load_location: "//build/bazel/rules/java:sdk_library.bzl",
2312 }
2313
2314 ctx.CreateBazelTargetModule(props, android.CommonAttributes{Name: module.Name()}, &attrs)
2315}
2316
Colin Cross79c7c262019-04-17 11:11:46 -07002317//
2318// SDK library prebuilts
2319//
2320
Paul Duffin56d44902020-01-31 13:36:25 +00002321// Properties associated with each api scope.
2322type sdkLibraryScopeProperties struct {
Colin Cross79c7c262019-04-17 11:11:46 -07002323 Jars []string `android:"path"`
2324
2325 Sdk_version *string
2326
Colin Cross79c7c262019-04-17 11:11:46 -07002327 // List of shared java libs that this module has dependencies to
2328 Libs []string
Paul Duffin3d1248c2020-04-09 00:10:17 +01002329
Paul Duffinc8782502020-04-29 20:45:27 +01002330 // The stubs source.
Paul Duffin3d1248c2020-04-09 00:10:17 +01002331 Stub_srcs []string `android:"path"`
Paul Duffin1fd005d2020-04-09 01:08:11 +01002332
2333 // The current.txt
Paul Duffin0f8faff2020-05-20 16:18:00 +01002334 Current_api *string `android:"path"`
Paul Duffin1fd005d2020-04-09 01:08:11 +01002335
2336 // The removed.txt
Paul Duffin0f8faff2020-05-20 16:18:00 +01002337 Removed_api *string `android:"path"`
Anton Hanssond78eb762021-09-21 15:25:12 +01002338
2339 // Annotation zip
2340 Annotations *string `android:"path"`
Colin Cross79c7c262019-04-17 11:11:46 -07002341}
2342
Paul Duffin56d44902020-01-31 13:36:25 +00002343type sdkLibraryImportProperties struct {
Paul Duffinfcfd7912020-01-31 17:54:30 +00002344 // List of shared java libs, common to all scopes, that this module has
2345 // dependencies to
2346 Libs []string
Paul Duffin1267d872021-04-16 17:21:36 +01002347
2348 // If set to true, compile dex files for the stubs. Defaults to false.
2349 Compile_dex *bool
Paul Duffin869de142021-07-15 14:14:41 +01002350
2351 // If not empty, classes are restricted to the specified packages and their sub-packages.
Paul Duffin869de142021-07-15 14:14:41 +01002352 Permitted_packages []string
Paul Duffin56d44902020-01-31 13:36:25 +00002353}
2354
Paul Duffineedc5d52020-06-12 17:46:39 +01002355type SdkLibraryImport struct {
Colin Cross79c7c262019-04-17 11:11:46 -07002356 android.ModuleBase
2357 android.DefaultableModuleBase
2358 prebuilt android.Prebuilt
Paul Duffindd46f712020-02-10 13:37:10 +00002359 android.ApexModuleBase
Colin Cross79c7c262019-04-17 11:11:46 -07002360
Paul Duffin37856732021-02-26 14:24:15 +00002361 hiddenAPI
Jiakai Zhang204356f2021-09-09 08:12:46 +00002362 dexpreopter
Paul Duffin37856732021-02-26 14:24:15 +00002363
Colin Cross79c7c262019-04-17 11:11:46 -07002364 properties sdkLibraryImportProperties
2365
Paul Duffin46a26a82020-04-07 19:27:04 +01002366 // Map from api scope to the scope specific property structure.
2367 scopeProperties map[*apiScope]*sdkLibraryScopeProperties
2368
Paul Duffin56d44902020-01-31 13:36:25 +00002369 commonToSdkLibraryAndImport
Paul Duffineedc5d52020-06-12 17:46:39 +01002370
2371 // The reference to the implementation library created by the source module.
2372 // Is nil if the source module does not exist.
2373 implLibraryModule *Library
2374
2375 // The reference to the xml permissions module created by the source module.
2376 // Is nil if the source module does not exist.
2377 xmlPermissionsFileModule *sdkLibraryXml
Paul Duffin39853512021-02-26 11:09:39 +00002378
Jeongik Chad5fe8782021-07-08 01:13:11 +09002379 // Build path to the dex implementation jar obtained from the prebuilt_apex, if any.
Martin Stjernholm8be1e6d2021-09-15 03:34:04 +01002380 dexJarFile OptionalDexJarPath
Jeongik Chad5fe8782021-07-08 01:13:11 +09002381
2382 // Expected install file path of the source module(sdk_library)
2383 // or dex implementation jar obtained from the prebuilt_apex, if any.
2384 installFile android.Path
Colin Cross79c7c262019-04-17 11:11:46 -07002385}
2386
Paul Duffineedc5d52020-06-12 17:46:39 +01002387var _ SdkLibraryDependency = (*SdkLibraryImport)(nil)
Colin Cross79c7c262019-04-17 11:11:46 -07002388
Paul Duffin46a26a82020-04-07 19:27:04 +01002389// The type of a structure that contains a field of type sdkLibraryScopeProperties
2390// for each apiscope in allApiScopes, e.g. something like:
Colin Crossd079e0b2022-08-16 10:27:33 -07002391//
2392// struct {
2393// Public sdkLibraryScopeProperties
2394// System sdkLibraryScopeProperties
2395// ...
2396// }
Paul Duffin46a26a82020-04-07 19:27:04 +01002397var allScopeStructType = createAllScopePropertiesStructType()
2398
2399// Dynamically create a structure type for each apiscope in allApiScopes.
2400func createAllScopePropertiesStructType() reflect.Type {
2401 var fields []reflect.StructField
2402 for _, apiScope := range allApiScopes {
2403 field := reflect.StructField{
2404 Name: apiScope.fieldName,
2405 Type: reflect.TypeOf(sdkLibraryScopeProperties{}),
2406 }
2407 fields = append(fields, field)
2408 }
2409
2410 return reflect.StructOf(fields)
2411}
2412
2413// Create an instance of the scope specific structure type and return a map
2414// from apiscope to a pointer to each scope specific field.
2415func createPropertiesInstance() (interface{}, map[*apiScope]*sdkLibraryScopeProperties) {
2416 allScopePropertiesPtr := reflect.New(allScopeStructType)
2417 allScopePropertiesStruct := allScopePropertiesPtr.Elem()
2418 scopeProperties := make(map[*apiScope]*sdkLibraryScopeProperties)
2419
2420 for _, apiScope := range allApiScopes {
2421 field := allScopePropertiesStruct.FieldByName(apiScope.fieldName)
2422 scopeProperties[apiScope] = field.Addr().Interface().(*sdkLibraryScopeProperties)
2423 }
2424
2425 return allScopePropertiesPtr.Interface(), scopeProperties
2426}
2427
Jaewoong Jung4f158ee2019-07-11 10:05:35 -07002428// java_sdk_library_import imports a prebuilt java_sdk_library.
Colin Cross79c7c262019-04-17 11:11:46 -07002429func sdkLibraryImportFactory() android.Module {
Paul Duffineedc5d52020-06-12 17:46:39 +01002430 module := &SdkLibraryImport{}
Colin Cross79c7c262019-04-17 11:11:46 -07002431
Paul Duffin46a26a82020-04-07 19:27:04 +01002432 allScopeProperties, scopeToProperties := createPropertiesInstance()
2433 module.scopeProperties = scopeToProperties
Jiakai Zhang9c4dc192023-02-09 00:09:24 +08002434 module.AddProperties(&module.properties, allScopeProperties, &module.importDexpreoptProperties)
Colin Cross79c7c262019-04-17 11:11:46 -07002435
Paul Duffinc3091c82020-05-08 14:16:20 +01002436 // Initialize information common between source and prebuilt.
Paul Duffin71b33cc2021-06-23 11:39:47 +01002437 module.initCommon(module)
Paul Duffinc3091c82020-05-08 14:16:20 +01002438
Paul Duffin0bdcb272020-02-06 15:24:57 +00002439 android.InitPrebuiltModule(module, &[]string{""})
Paul Duffindd46f712020-02-10 13:37:10 +00002440 android.InitApexModule(module)
Colin Cross79c7c262019-04-17 11:11:46 -07002441 InitJavaModule(module, android.HostAndDeviceSupported)
2442
Paul Duffin1b1e8062020-05-08 13:44:43 +01002443 module.SetDefaultableHook(func(mctx android.DefaultableHookContext) {
2444 if module.initCommonAfterDefaultsApplied(mctx) {
2445 module.createInternalModules(mctx)
2446 }
2447 })
Colin Cross79c7c262019-04-17 11:11:46 -07002448 return module
2449}
2450
Paul Duffin630b11e2021-07-15 13:35:26 +01002451var _ PermittedPackagesForUpdatableBootJars = (*SdkLibraryImport)(nil)
2452
2453func (module *SdkLibraryImport) PermittedPackagesForUpdatableBootJars() []string {
2454 return module.properties.Permitted_packages
2455}
2456
Paul Duffineedc5d52020-06-12 17:46:39 +01002457func (module *SdkLibraryImport) Prebuilt() *android.Prebuilt {
Colin Cross79c7c262019-04-17 11:11:46 -07002458 return &module.prebuilt
2459}
2460
Paul Duffineedc5d52020-06-12 17:46:39 +01002461func (module *SdkLibraryImport) Name() string {
Colin Cross79c7c262019-04-17 11:11:46 -07002462 return module.prebuilt.Name(module.ModuleBase.Name())
2463}
2464
Paul Duffineedc5d52020-06-12 17:46:39 +01002465func (module *SdkLibraryImport) createInternalModules(mctx android.DefaultableHookContext) {
Colin Cross79c7c262019-04-17 11:11:46 -07002466
Paul Duffin50061512020-01-21 16:31:05 +00002467 // If the build is configured to use prebuilts then force this to be preferred.
Jeongik Cha816a23a2020-07-08 01:09:23 +09002468 if mctx.Config().AlwaysUsePrebuiltSdks() {
Paul Duffin50061512020-01-21 16:31:05 +00002469 module.prebuilt.ForcePrefer()
2470 }
2471
Paul Duffin46a26a82020-04-07 19:27:04 +01002472 for apiScope, scopeProperties := range module.scopeProperties {
Paul Duffin56d44902020-01-31 13:36:25 +00002473 if len(scopeProperties.Jars) == 0 {
2474 continue
2475 }
2476
Paul Duffinbbb546b2020-04-09 00:07:11 +01002477 module.createJavaImportForStubs(mctx, apiScope, scopeProperties)
Paul Duffin3d1248c2020-04-09 00:10:17 +01002478
Paul Duffin0f8faff2020-05-20 16:18:00 +01002479 if len(scopeProperties.Stub_srcs) > 0 {
2480 module.createPrebuiltStubsSources(mctx, apiScope, scopeProperties)
2481 }
Paul Duffin56d44902020-01-31 13:36:25 +00002482 }
Colin Cross79c7c262019-04-17 11:11:46 -07002483
2484 javaSdkLibraries := javaSdkLibraries(mctx.Config())
2485 javaSdkLibrariesLock.Lock()
2486 defer javaSdkLibrariesLock.Unlock()
2487 *javaSdkLibraries = append(*javaSdkLibraries, module.BaseModuleName())
2488}
2489
Paul Duffineedc5d52020-06-12 17:46:39 +01002490func (module *SdkLibraryImport) createJavaImportForStubs(mctx android.DefaultableHookContext, apiScope *apiScope, scopeProperties *sdkLibraryScopeProperties) {
Paul Duffinbbb546b2020-04-09 00:07:11 +01002491 // Creates a java import for the jar with ".stubs" suffix
2492 props := struct {
Paul Duffin1dbe3ca2020-05-16 09:57:59 +01002493 Name *string
2494 Sdk_version *string
2495 Libs []string
2496 Jars []string
Paul Duffin1267d872021-04-16 17:21:36 +01002497 Compile_dex *bool
Paul Duffinbf4de042022-09-27 12:41:52 +01002498
2499 android.UserSuppliedPrebuiltProperties
Paul Duffinbbb546b2020-04-09 00:07:11 +01002500 }{}
Paul Duffinc3091c82020-05-08 14:16:20 +01002501 props.Name = proptools.StringPtr(module.stubsLibraryModuleName(apiScope))
Paul Duffinbbb546b2020-04-09 00:07:11 +01002502 props.Sdk_version = scopeProperties.Sdk_version
2503 // Prepend any of the libs from the legacy public properties to the libs for each of the
2504 // scopes to avoid having to duplicate them in each scope.
2505 props.Libs = append(module.properties.Libs, scopeProperties.Libs...)
2506 props.Jars = scopeProperties.Jars
Paul Duffin1dbe3ca2020-05-16 09:57:59 +01002507
Paul Duffin38b57852020-05-13 16:08:09 +01002508 // The imports are preferred if the java_sdk_library_import is preferred.
Paul Duffinbf4de042022-09-27 12:41:52 +01002509 props.CopyUserSuppliedPropertiesFromPrebuilt(&module.prebuilt)
Paul Duffin859fe962020-05-15 10:20:31 +01002510
Paul Duffin1267d872021-04-16 17:21:36 +01002511 // The imports need to be compiled to dex if the java_sdk_library_import requests it.
Paul Duffinf4600f62021-05-13 22:34:45 +01002512 compileDex := module.properties.Compile_dex
2513 if module.stubLibrariesCompiledForDex() {
2514 compileDex = proptools.BoolPtr(true)
2515 }
2516 props.Compile_dex = compileDex
Paul Duffin1267d872021-04-16 17:21:36 +01002517
Paul Duffin859fe962020-05-15 10:20:31 +01002518 mctx.CreateModule(ImportFactory, &props, module.sdkComponentPropertiesForChildLibrary())
Paul Duffinbbb546b2020-04-09 00:07:11 +01002519}
2520
Paul Duffineedc5d52020-06-12 17:46:39 +01002521func (module *SdkLibraryImport) createPrebuiltStubsSources(mctx android.DefaultableHookContext, apiScope *apiScope, scopeProperties *sdkLibraryScopeProperties) {
Paul Duffin3d1248c2020-04-09 00:10:17 +01002522 props := struct {
Paul Duffinbf4de042022-09-27 12:41:52 +01002523 Name *string
2524 Srcs []string
2525
2526 android.UserSuppliedPrebuiltProperties
Paul Duffin3d1248c2020-04-09 00:10:17 +01002527 }{}
Paul Duffinc3091c82020-05-08 14:16:20 +01002528 props.Name = proptools.StringPtr(module.stubsSourceModuleName(apiScope))
Paul Duffin3d1248c2020-04-09 00:10:17 +01002529 props.Srcs = scopeProperties.Stub_srcs
Paul Duffin38b57852020-05-13 16:08:09 +01002530
2531 // The stubs source is preferred if the java_sdk_library_import is preferred.
Paul Duffinbf4de042022-09-27 12:41:52 +01002532 props.CopyUserSuppliedPropertiesFromPrebuilt(&module.prebuilt)
2533
2534 mctx.CreateModule(PrebuiltStubsSourcesFactory, &props)
Paul Duffin3d1248c2020-04-09 00:10:17 +01002535}
2536
Paul Duffin44f1d842020-06-26 20:17:02 +01002537// Add the dependencies on the child module in the component deps mutator so that it
2538// creates references to the prebuilt and not the source modules.
2539func (module *SdkLibraryImport) ComponentDepsMutator(ctx android.BottomUpMutatorContext) {
Paul Duffin46a26a82020-04-07 19:27:04 +01002540 for apiScope, scopeProperties := range module.scopeProperties {
Paul Duffin56d44902020-01-31 13:36:25 +00002541 if len(scopeProperties.Jars) == 0 {
2542 continue
2543 }
2544
2545 // Add dependencies to the prebuilt stubs library
Paul Duffin864116c2021-04-02 10:24:13 +01002546 ctx.AddVariationDependencies(nil, apiScope.stubsTag, android.PrebuiltNameFromSource(module.stubsLibraryModuleName(apiScope)))
Paul Duffin0f8faff2020-05-20 16:18:00 +01002547
2548 if len(scopeProperties.Stub_srcs) > 0 {
2549 // Add dependencies to the prebuilt stubs source library
Paul Duffin864116c2021-04-02 10:24:13 +01002550 ctx.AddVariationDependencies(nil, apiScope.stubsSourceTag, android.PrebuiltNameFromSource(module.stubsSourceModuleName(apiScope)))
Paul Duffin0f8faff2020-05-20 16:18:00 +01002551 }
Paul Duffin56d44902020-01-31 13:36:25 +00002552 }
Paul Duffin44f1d842020-06-26 20:17:02 +01002553}
2554
2555// Add other dependencies as normal.
2556func (module *SdkLibraryImport) DepsMutator(ctx android.BottomUpMutatorContext) {
Paul Duffineedc5d52020-06-12 17:46:39 +01002557
2558 implName := module.implLibraryModuleName()
2559 if ctx.OtherModuleExists(implName) {
2560 ctx.AddVariationDependencies(nil, implLibraryTag, implName)
2561
2562 xmlPermissionsModuleName := module.xmlPermissionsModuleName()
2563 if module.sharedLibrary() && ctx.OtherModuleExists(xmlPermissionsModuleName) {
2564 // Add dependency to the rule for generating the xml permissions file
2565 ctx.AddDependency(module, xmlPermissionsFileTag, xmlPermissionsModuleName)
2566 }
2567 }
Colin Cross79c7c262019-04-17 11:11:46 -07002568}
2569
Jiakai Zhang204356f2021-09-09 08:12:46 +00002570func (module *SdkLibraryImport) AndroidMkEntries() []android.AndroidMkEntries {
2571 // For an SDK library imported from a prebuilt APEX, we don't need a Make module for itself, as we
2572 // don't need to install it. However, we need to add its dexpreopt outputs as sub-modules, if it
2573 // is preopted.
2574 dexpreoptEntries := module.dexpreopter.AndroidMkEntriesForApex()
2575 return append(dexpreoptEntries, android.AndroidMkEntries{Disabled: true})
2576}
2577
Jiyong Park45bf82e2020-12-15 22:29:02 +09002578var _ android.ApexModule = (*SdkLibraryImport)(nil)
2579
2580// Implements android.ApexModule
Paul Duffineedc5d52020-06-12 17:46:39 +01002581func (module *SdkLibraryImport) DepIsInSameApex(mctx android.BaseModuleContext, dep android.Module) bool {
2582 depTag := mctx.OtherModuleDependencyTag(dep)
2583 if depTag == xmlPermissionsFileTag {
2584 return true
2585 }
2586
2587 // None of the other dependencies of the java_sdk_library_import are in the same apex
2588 // as the one that references this module.
2589 return false
2590}
2591
Jiyong Park45bf82e2020-12-15 22:29:02 +09002592// Implements android.ApexModule
Dan Albertc8060532020-07-22 22:32:17 -07002593func (module *SdkLibraryImport) ShouldSupportSdkVersion(ctx android.BaseModuleContext,
2594 sdkVersion android.ApiLevel) error {
Jooyung Han749dc692020-04-15 11:03:39 +09002595 // we don't check prebuilt modules for sdk_version
2596 return nil
2597}
2598
Paul Duffinea8f8082021-06-24 13:25:57 +01002599// Implements android.ApexModule
2600func (module *SdkLibraryImport) UniqueApexVariations() bool {
2601 return module.uniqueApexVariations()
2602}
2603
Paul Duffin09817d62022-04-28 17:45:11 +01002604// MinSdkVersion - Implements hiddenAPIModule
Spandan Das8c9ae7e2023-03-03 21:20:36 +00002605func (module *SdkLibraryImport) MinSdkVersion(ctx android.EarlyModuleContext) android.ApiLevel {
2606 return android.NoneApiLevel
Paul Duffin09817d62022-04-28 17:45:11 +01002607}
2608
2609var _ hiddenAPIModule = (*SdkLibraryImport)(nil)
2610
Paul Duffineedc5d52020-06-12 17:46:39 +01002611func (module *SdkLibraryImport) OutputFiles(tag string) (android.Paths, error) {
Paul Duffin1e940d52022-04-29 14:21:25 +01002612 paths, err := module.commonOutputFiles(tag)
2613 if paths != nil || err != nil {
2614 return paths, err
2615 }
2616 if module.implLibraryModule != nil {
2617 return module.implLibraryModule.OutputFiles(tag)
2618 } else {
2619 return nil, nil
2620 }
Paul Duffin46dc45a2020-05-14 15:39:10 +01002621}
2622
Paul Duffineedc5d52020-06-12 17:46:39 +01002623func (module *SdkLibraryImport) GenerateAndroidBuildActions(ctx android.ModuleContext) {
Paul Duffina2ae7e02020-09-11 11:55:00 +01002624 module.generateCommonBuildActions(ctx)
2625
Jeongik Chad5fe8782021-07-08 01:13:11 +09002626 // Assume that source module(sdk_library) is installed in /<sdk_library partition>/framework
2627 module.installFile = android.PathForModuleInstall(ctx, "framework", module.Stem()+".jar")
2628
Paul Duffin0f8faff2020-05-20 16:18:00 +01002629 // Record the paths to the prebuilt stubs library and stubs source.
Colin Cross79c7c262019-04-17 11:11:46 -07002630 ctx.VisitDirectDeps(func(to android.Module) {
2631 tag := ctx.OtherModuleDependencyTag(to)
2632
Paul Duffin0f8faff2020-05-20 16:18:00 +01002633 // Extract information from any of the scope specific dependencies.
2634 if scopeTag, ok := tag.(scopeDependencyTag); ok {
2635 apiScope := scopeTag.apiScope
2636 scopePaths := module.getScopePathsCreateIfNeeded(apiScope)
2637
2638 // Extract information from the dependency. The exact information extracted
2639 // is determined by the nature of the dependency which is determined by the tag.
2640 scopeTag.extractDepInfo(ctx, to, scopePaths)
Paul Duffineedc5d52020-06-12 17:46:39 +01002641 } else if tag == implLibraryTag {
2642 if implLibrary, ok := to.(*Library); ok {
2643 module.implLibraryModule = implLibrary
2644 } else {
2645 ctx.ModuleErrorf("implementation library must be of type *java.Library but was %T", to)
2646 }
2647 } else if tag == xmlPermissionsFileTag {
2648 if xmlPermissionsFileModule, ok := to.(*sdkLibraryXml); ok {
2649 module.xmlPermissionsFileModule = xmlPermissionsFileModule
2650 } else {
2651 ctx.ModuleErrorf("xml permissions file module must be of type *sdkLibraryXml but was %T", to)
2652 }
Colin Cross79c7c262019-04-17 11:11:46 -07002653 }
2654 })
Paul Duffin0f8faff2020-05-20 16:18:00 +01002655
2656 // Populate the scope paths with information from the properties.
2657 for apiScope, scopeProperties := range module.scopeProperties {
2658 if len(scopeProperties.Jars) == 0 {
2659 continue
2660 }
2661
2662 paths := module.getScopePathsCreateIfNeeded(apiScope)
Anton Hanssond78eb762021-09-21 15:25:12 +01002663 paths.annotationsZip = android.OptionalPathForModuleSrc(ctx, scopeProperties.Annotations)
Paul Duffin0f8faff2020-05-20 16:18:00 +01002664 paths.currentApiFilePath = android.OptionalPathForModuleSrc(ctx, scopeProperties.Current_api)
2665 paths.removedApiFilePath = android.OptionalPathForModuleSrc(ctx, scopeProperties.Removed_api)
2666 }
Paul Duffin39853512021-02-26 11:09:39 +00002667
2668 if ctx.Device() {
2669 // If this is a variant created for a prebuilt_apex then use the dex implementation jar
2670 // obtained from the associated deapexer module.
2671 ai := ctx.Provider(android.ApexInfoProvider).(android.ApexInfo)
2672 if ai.ForPrebuiltApex {
Paul Duffin39853512021-02-26 11:09:39 +00002673 // Get the path of the dex implementation jar from the `deapexer` module.
Martin Stjernholm44825602021-09-17 01:44:12 +01002674 di := android.FindDeapexerProviderForModule(ctx)
2675 if di == nil {
2676 return // An error has been reported by FindDeapexerProviderForModule.
2677 }
Jiakai Zhang81e46812023-02-08 21:56:07 +08002678 dexJarFileApexRootRelative := apexRootRelativePathToJavaLib(module.BaseModuleName())
2679 if dexOutputPath := di.PrebuiltExportPath(dexJarFileApexRootRelative); dexOutputPath != nil {
Martin Stjernholm8be1e6d2021-09-15 03:34:04 +01002680 dexJarFile := makeDexJarPathFromPath(dexOutputPath)
2681 module.dexJarFile = dexJarFile
Jiakai Zhang204356f2021-09-09 08:12:46 +00002682 installPath := android.PathForModuleInPartitionInstall(
Jiakai Zhang81e46812023-02-08 21:56:07 +08002683 ctx, "apex", ai.ApexVariationName, dexJarFileApexRootRelative)
Jiakai Zhang204356f2021-09-09 08:12:46 +00002684 module.installFile = installPath
Martin Stjernholm8be1e6d2021-09-15 03:34:04 +01002685 module.initHiddenAPI(ctx, dexJarFile, module.findScopePaths(apiScopePublic).stubsImplPath[0], nil)
Jiakai Zhang204356f2021-09-09 08:12:46 +00002686
Jiakai Zhang204356f2021-09-09 08:12:46 +00002687 module.dexpreopter.installPath = module.dexpreopter.getInstallPath(ctx, installPath)
2688 module.dexpreopter.isSDKLibrary = true
2689 module.dexpreopter.uncompressedDex = shouldUncompressDex(ctx, &module.dexpreopter)
Jiakai Zhang81e46812023-02-08 21:56:07 +08002690
2691 if profilePath := di.PrebuiltExportPath(dexJarFileApexRootRelative + ".prof"); profilePath != nil {
2692 module.dexpreopter.inputProfilePathOnHost = profilePath
2693 }
2694
2695 // Dexpreopting.
Jiakai Zhang204356f2021-09-09 08:12:46 +00002696 module.dexpreopt(ctx, dexOutputPath)
Paul Duffin39853512021-02-26 11:09:39 +00002697 } else {
2698 // This should never happen as a variant for a prebuilt_apex is only created if the
2699 // prebuilt_apex has been configured to export the java library dex file.
Martin Stjernholm44825602021-09-17 01:44:12 +01002700 ctx.ModuleErrorf("internal error: no dex implementation jar available from prebuilt APEX %s", di.ApexModuleName())
Paul Duffin39853512021-02-26 11:09:39 +00002701 }
2702 }
2703 }
Colin Cross79c7c262019-04-17 11:11:46 -07002704}
2705
Jiyong Parkf1691d22021-03-29 20:11:58 +09002706func (module *SdkLibraryImport) sdkJars(ctx android.BaseModuleContext, sdkVersion android.SdkSpec, headerJars bool) android.Paths {
Paul Duffineedc5d52020-06-12 17:46:39 +01002707
2708 // For consistency with SdkLibrary make the implementation jar available to libraries that
2709 // are within the same APEX.
2710 implLibraryModule := module.implLibraryModule
Colin Cross56a83212020-09-15 18:30:11 -07002711 if implLibraryModule != nil && withinSameApexesAs(ctx, module) {
Paul Duffineedc5d52020-06-12 17:46:39 +01002712 if headerJars {
2713 return implLibraryModule.HeaderJars()
2714 } else {
2715 return implLibraryModule.ImplementationJars()
2716 }
2717 }
2718
Paul Duffin23970f42020-05-20 14:20:02 +01002719 return module.selectHeaderJarsForSdkVersion(ctx, sdkVersion)
Paul Duffin56d44902020-01-31 13:36:25 +00002720}
2721
Colin Cross79c7c262019-04-17 11:11:46 -07002722// to satisfy SdkLibraryDependency interface
Jiyong Parkf1691d22021-03-29 20:11:58 +09002723func (module *SdkLibraryImport) SdkHeaderJars(ctx android.BaseModuleContext, sdkVersion android.SdkSpec) android.Paths {
Colin Cross79c7c262019-04-17 11:11:46 -07002724 // This module is just a wrapper for the prebuilt stubs.
Paul Duffineedc5d52020-06-12 17:46:39 +01002725 return module.sdkJars(ctx, sdkVersion, true)
Colin Cross79c7c262019-04-17 11:11:46 -07002726}
2727
2728// to satisfy SdkLibraryDependency interface
Jiyong Parkf1691d22021-03-29 20:11:58 +09002729func (module *SdkLibraryImport) SdkImplementationJars(ctx android.BaseModuleContext, sdkVersion android.SdkSpec) android.Paths {
Colin Cross79c7c262019-04-17 11:11:46 -07002730 // This module is just a wrapper for the stubs.
Paul Duffineedc5d52020-06-12 17:46:39 +01002731 return module.sdkJars(ctx, sdkVersion, false)
2732}
2733
Ulya Trafimovichdbf31662020-12-17 12:07:54 +00002734// to satisfy UsesLibraryDependency interface
Martin Stjernholm8be1e6d2021-09-15 03:34:04 +01002735func (module *SdkLibraryImport) DexJarBuildPath() OptionalDexJarPath {
Paul Duffin39853512021-02-26 11:09:39 +00002736 // The dex implementation jar extracted from the .apex file should be used in preference to the
2737 // source.
Martin Stjernholm8be1e6d2021-09-15 03:34:04 +01002738 if module.dexJarFile.IsSet() {
Paul Duffin39853512021-02-26 11:09:39 +00002739 return module.dexJarFile
2740 }
Paul Duffineedc5d52020-06-12 17:46:39 +01002741 if module.implLibraryModule == nil {
Martin Stjernholm8be1e6d2021-09-15 03:34:04 +01002742 return makeUnsetDexJarPath()
Paul Duffineedc5d52020-06-12 17:46:39 +01002743 } else {
2744 return module.implLibraryModule.DexJarBuildPath()
2745 }
2746}
2747
Ulya Trafimovichdbf31662020-12-17 12:07:54 +00002748// to satisfy UsesLibraryDependency interface
Ulya Trafimovich31e444e2020-08-14 17:32:16 +01002749func (module *SdkLibraryImport) DexJarInstallPath() android.Path {
Jeongik Chad5fe8782021-07-08 01:13:11 +09002750 return module.installFile
Ulya Trafimovich31e444e2020-08-14 17:32:16 +01002751}
2752
Ulya Trafimovichdbf31662020-12-17 12:07:54 +00002753// to satisfy UsesLibraryDependency interface
2754func (module *SdkLibraryImport) ClassLoaderContexts() dexpreopt.ClassLoaderContextMap {
2755 return nil
2756}
2757
Paul Duffineedc5d52020-06-12 17:46:39 +01002758// to satisfy apex.javaDependency interface
2759func (module *SdkLibraryImport) JacocoReportClassesFile() android.Path {
2760 if module.implLibraryModule == nil {
2761 return nil
2762 } else {
2763 return module.implLibraryModule.JacocoReportClassesFile()
2764 }
2765}
2766
2767// to satisfy apex.javaDependency interface
Colin Cross08dca382020-07-21 20:31:17 -07002768func (module *SdkLibraryImport) LintDepSets() LintDepSets {
2769 if module.implLibraryModule == nil {
2770 return LintDepSets{}
2771 } else {
2772 return module.implLibraryModule.LintDepSets()
2773 }
2774}
2775
Spandan Das17854f52022-01-14 21:19:14 +00002776func (module *SdkLibraryImport) GetStrictUpdatabilityLinting() bool {
Jaewoong Jung476b9d62021-05-10 15:30:00 -07002777 if module.implLibraryModule == nil {
2778 return false
2779 } else {
Spandan Das17854f52022-01-14 21:19:14 +00002780 return module.implLibraryModule.GetStrictUpdatabilityLinting()
Jaewoong Jung476b9d62021-05-10 15:30:00 -07002781 }
2782}
2783
Spandan Das17854f52022-01-14 21:19:14 +00002784func (module *SdkLibraryImport) SetStrictUpdatabilityLinting(strictLinting bool) {
Jaewoong Jung476b9d62021-05-10 15:30:00 -07002785 if module.implLibraryModule != nil {
Spandan Das17854f52022-01-14 21:19:14 +00002786 module.implLibraryModule.SetStrictUpdatabilityLinting(strictLinting)
Jaewoong Jung476b9d62021-05-10 15:30:00 -07002787 }
2788}
2789
Colin Cross08dca382020-07-21 20:31:17 -07002790// to satisfy apex.javaDependency interface
Paul Duffineedc5d52020-06-12 17:46:39 +01002791func (module *SdkLibraryImport) Stem() string {
2792 return module.BaseModuleName()
Colin Cross79c7c262019-04-17 11:11:46 -07002793}
Jiyong Parke3833882020-02-17 17:28:10 +09002794
Paul Duffin44b481b2020-06-17 16:59:43 +01002795var _ ApexDependency = (*SdkLibraryImport)(nil)
2796
2797// to satisfy java.ApexDependency interface
2798func (module *SdkLibraryImport) HeaderJars() android.Paths {
2799 if module.implLibraryModule == nil {
2800 return nil
2801 } else {
2802 return module.implLibraryModule.HeaderJars()
2803 }
2804}
2805
2806// to satisfy java.ApexDependency interface
2807func (module *SdkLibraryImport) ImplementationAndResourcesJars() android.Paths {
2808 if module.implLibraryModule == nil {
2809 return nil
2810 } else {
2811 return module.implLibraryModule.ImplementationAndResourcesJars()
2812 }
2813}
2814
Jiakai Zhang204356f2021-09-09 08:12:46 +00002815// to satisfy java.DexpreopterInterface interface
2816func (module *SdkLibraryImport) IsInstallable() bool {
2817 return true
2818}
2819
Paul Duffinfef55002021-06-17 14:56:05 +01002820var _ android.RequiredFilesFromPrebuiltApex = (*SdkLibraryImport)(nil)
2821
Paul Duffinb4bbf2c2021-06-17 15:59:07 +01002822func (module *SdkLibraryImport) RequiredFilesFromPrebuiltApex(ctx android.BaseModuleContext) []string {
Paul Duffinfef55002021-06-17 14:56:05 +01002823 name := module.BaseModuleName()
Jiakai Zhang81e46812023-02-08 21:56:07 +08002824 return requiredFilesFromPrebuiltApexForImport(name, &module.dexpreopter)
Paul Duffinfef55002021-06-17 14:56:05 +01002825}
2826
Jiyong Parke3833882020-02-17 17:28:10 +09002827// java_sdk_library_xml
Jiyong Parke3833882020-02-17 17:28:10 +09002828type sdkLibraryXml struct {
2829 android.ModuleBase
2830 android.DefaultableModuleBase
2831 android.ApexModuleBase
2832
2833 properties sdkLibraryXmlProperties
2834
2835 outputFilePath android.OutputPath
2836 installDirPath android.InstallPath
Colin Cross56a83212020-09-15 18:30:11 -07002837
2838 hideApexVariantFromMake bool
Jiyong Parke3833882020-02-17 17:28:10 +09002839}
2840
2841type sdkLibraryXmlProperties struct {
2842 // canonical name of the lib
2843 Lib_name *string
Pedro Loureiro9956e5e2021-09-07 17:21:59 +00002844
2845 // Signals that this shared library is part of the bootclasspath starting
2846 // on the version indicated in this attribute.
2847 //
2848 // This will make platforms at this level and above to ignore
2849 // <uses-library> tags with this library name because the library is already
2850 // available
2851 On_bootclasspath_since *string
2852
2853 // Signals that this shared library was part of the bootclasspath before
2854 // (but not including) the version indicated in this attribute.
2855 //
2856 // The system will automatically add a <uses-library> tag with this library to
2857 // apps that target any SDK less than the version indicated in this attribute.
2858 On_bootclasspath_before *string
2859
2860 // Indicates that PackageManager should ignore this shared library if the
2861 // platform is below the version indicated in this attribute.
2862 //
2863 // This means that the device won't recognise this library as installed.
2864 Min_device_sdk *string
2865
2866 // Indicates that PackageManager should ignore this shared library if the
2867 // platform is above the version indicated in this attribute.
2868 //
2869 // This means that the device won't recognise this library as installed.
2870 Max_device_sdk *string
Pedro Loureiroc3621422021-09-28 15:40:23 +00002871
2872 // The SdkLibrary's min api level as a string
2873 //
2874 // This value comes from the ApiLevel of the MinSdkVersion property.
2875 Sdk_library_min_api_level *string
Jiyong Parke3833882020-02-17 17:28:10 +09002876}
2877
2878// java_sdk_library_xml builds the permission xml file for a java_sdk_library.
2879// Not to be used directly by users. java_sdk_library internally uses this.
2880func sdkLibraryXmlFactory() android.Module {
2881 module := &sdkLibraryXml{}
2882
2883 module.AddProperties(&module.properties)
2884
2885 android.InitApexModule(module)
2886 android.InitAndroidArchModule(module, android.DeviceSupported, android.MultilibCommon)
2887
2888 return module
2889}
2890
Colin Crossaede88c2020-08-11 12:17:01 -07002891func (module *sdkLibraryXml) UniqueApexVariations() bool {
2892 // sdkLibraryXml needs a unique variation per APEX because the generated XML file contains the path to the
2893 // mounted APEX, which contains the name of the APEX.
2894 return true
2895}
2896
Jiyong Parke3833882020-02-17 17:28:10 +09002897// from android.PrebuiltEtcModule
Jooyung Han0703fd82020-08-26 22:11:53 +09002898func (module *sdkLibraryXml) BaseDir() string {
2899 return "etc"
2900}
2901
2902// from android.PrebuiltEtcModule
Jiyong Parke3833882020-02-17 17:28:10 +09002903func (module *sdkLibraryXml) SubDir() string {
2904 return "permissions"
2905}
2906
2907// from android.PrebuiltEtcModule
2908func (module *sdkLibraryXml) OutputFile() android.OutputPath {
2909 return module.outputFilePath
2910}
2911
2912// from android.ApexModule
2913func (module *sdkLibraryXml) AvailableFor(what string) bool {
2914 return true
2915}
2916
2917func (module *sdkLibraryXml) DepsMutator(ctx android.BottomUpMutatorContext) {
2918 // do nothing
2919}
2920
Jiyong Park45bf82e2020-12-15 22:29:02 +09002921var _ android.ApexModule = (*sdkLibraryXml)(nil)
2922
2923// Implements android.ApexModule
Dan Albertc8060532020-07-22 22:32:17 -07002924func (module *sdkLibraryXml) ShouldSupportSdkVersion(ctx android.BaseModuleContext,
2925 sdkVersion android.ApiLevel) error {
Jooyung Han749dc692020-04-15 11:03:39 +09002926 // sdkLibraryXml doesn't need to be checked separately because java_sdk_library is checked
2927 return nil
2928}
2929
Jiyong Parke3833882020-02-17 17:28:10 +09002930// File path to the runtime implementation library
Colin Cross56a83212020-09-15 18:30:11 -07002931func (module *sdkLibraryXml) implPath(ctx android.ModuleContext) string {
Jiyong Parke3833882020-02-17 17:28:10 +09002932 implName := proptools.String(module.properties.Lib_name)
Colin Cross56a83212020-09-15 18:30:11 -07002933 if apexInfo := ctx.Provider(android.ApexInfoProvider).(android.ApexInfo); !apexInfo.IsForPlatform() {
Colin Crosse07f2312020-08-13 11:24:56 -07002934 // TODO(b/146468504): ApexVariationName() is only a soong module name, not apex name.
Jiyong Parke3833882020-02-17 17:28:10 +09002935 // In most cases, this works fine. But when apex_name is set or override_apex is used
2936 // this can be wrong.
Colin Cross56a83212020-09-15 18:30:11 -07002937 return fmt.Sprintf("/apex/%s/javalib/%s.jar", apexInfo.ApexVariationName, implName)
Jiyong Parke3833882020-02-17 17:28:10 +09002938 }
2939 partition := "system"
2940 if module.SocSpecific() {
2941 partition = "vendor"
2942 } else if module.DeviceSpecific() {
2943 partition = "odm"
2944 } else if module.ProductSpecific() {
2945 partition = "product"
2946 } else if module.SystemExtSpecific() {
2947 partition = "system_ext"
2948 }
2949 return "/" + partition + "/framework/" + implName + ".jar"
2950}
2951
Pedro Loureiro9956e5e2021-09-07 17:21:59 +00002952func formattedOptionalSdkLevelAttribute(ctx android.ModuleContext, attrName string, value *string) string {
2953 if value == nil {
2954 return ""
2955 }
2956 apiLevel, err := android.ApiLevelFromUser(ctx, *value)
2957 if err != nil {
Pedro Loureiroba6682f2021-10-29 09:32:32 +00002958 // attributes in bp files have underscores but in the xml have dashes.
2959 ctx.PropertyErrorf(strings.ReplaceAll(attrName, "-", "_"), err.Error())
Pedro Loureiro9956e5e2021-09-07 17:21:59 +00002960 return ""
2961 }
Pedro Loureirob638c622021-12-22 15:28:05 +00002962 if apiLevel.IsCurrent() {
2963 // passing "current" would always mean a future release, never the current (or the current in
2964 // progress) which means some conditions would never be triggered.
2965 ctx.PropertyErrorf(strings.ReplaceAll(attrName, "-", "_"),
2966 `"current" is not an allowed value for this attribute`)
2967 return ""
2968 }
Pedro Loureiro48991222022-06-17 20:01:21 +00002969 // "safeValue" is safe because it translates finalized codenames to a string
2970 // with their SDK int.
2971 safeValue := apiLevel.String()
2972 return formattedOptionalAttribute(attrName, &safeValue)
Pedro Loureiro9956e5e2021-09-07 17:21:59 +00002973}
2974
2975// formats an attribute for the xml permissions file if the value is not null
2976// returns empty string otherwise
2977func formattedOptionalAttribute(attrName string, value *string) string {
2978 if value == nil {
2979 return ""
2980 }
2981 return fmt.Sprintf(` %s=\"%s\"\n`, attrName, *value)
2982}
2983
2984func (module *sdkLibraryXml) permissionsContents(ctx android.ModuleContext) string {
2985 libName := proptools.String(module.properties.Lib_name)
2986 libNameAttr := formattedOptionalAttribute("name", &libName)
2987 filePath := module.implPath(ctx)
2988 filePathAttr := formattedOptionalAttribute("file", &filePath)
Pedro Loureiroba6682f2021-10-29 09:32:32 +00002989 implicitFromAttr := formattedOptionalSdkLevelAttribute(ctx, "on-bootclasspath-since", module.properties.On_bootclasspath_since)
2990 implicitUntilAttr := formattedOptionalSdkLevelAttribute(ctx, "on-bootclasspath-before", module.properties.On_bootclasspath_before)
2991 minSdkAttr := formattedOptionalSdkLevelAttribute(ctx, "min-device-sdk", module.properties.Min_device_sdk)
2992 maxSdkAttr := formattedOptionalSdkLevelAttribute(ctx, "max-device-sdk", module.properties.Max_device_sdk)
Pedro Loureiro196d3e62021-12-22 19:53:01 +00002993 // <library> is understood in all android versions whereas <apex-library> is only understood from API T (and ignored before that).
2994 // 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 +00002995 var libraryTag string
2996 if module.properties.Min_device_sdk != nil {
Pedro Loureiro196d3e62021-12-22 19:53:01 +00002997 libraryTag = ` <apex-library\n`
Pedro Loureiroc3621422021-09-28 15:40:23 +00002998 } else {
2999 libraryTag = ` <library\n`
3000 }
Pedro Loureiro9956e5e2021-09-07 17:21:59 +00003001
3002 return strings.Join([]string{
3003 `<?xml version=\"1.0\" encoding=\"utf-8\"?>\n`,
3004 `<!-- Copyright (C) 2018 The Android Open Source Project\n`,
3005 `\n`,
3006 ` Licensed under the Apache License, Version 2.0 (the \"License\");\n`,
3007 ` you may not use this file except in compliance with the License.\n`,
3008 ` You may obtain a copy of the License at\n`,
3009 `\n`,
3010 ` http://www.apache.org/licenses/LICENSE-2.0\n`,
3011 `\n`,
3012 ` Unless required by applicable law or agreed to in writing, software\n`,
3013 ` distributed under the License is distributed on an \"AS IS\" BASIS,\n`,
3014 ` WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\n`,
3015 ` See the License for the specific language governing permissions and\n`,
3016 ` limitations under the License.\n`,
3017 `-->\n`,
3018 `<permissions>\n`,
Pedro Loureiroc3621422021-09-28 15:40:23 +00003019 libraryTag,
Pedro Loureiro9956e5e2021-09-07 17:21:59 +00003020 libNameAttr,
3021 filePathAttr,
3022 implicitFromAttr,
3023 implicitUntilAttr,
3024 minSdkAttr,
3025 maxSdkAttr,
3026 ` />\n`,
3027 `</permissions>\n`}, "")
3028}
3029
Jiyong Parke3833882020-02-17 17:28:10 +09003030func (module *sdkLibraryXml) GenerateAndroidBuildActions(ctx android.ModuleContext) {
Colin Cross56a83212020-09-15 18:30:11 -07003031 module.hideApexVariantFromMake = !ctx.Provider(android.ApexInfoProvider).(android.ApexInfo).IsForPlatform()
3032
Jiyong Parke3833882020-02-17 17:28:10 +09003033 libName := proptools.String(module.properties.Lib_name)
Pedro Loureiroc3621422021-09-28 15:40:23 +00003034 module.selfValidate(ctx)
Pedro Loureiro9956e5e2021-09-07 17:21:59 +00003035 xmlContent := module.permissionsContents(ctx)
Jiyong Parke3833882020-02-17 17:28:10 +09003036
3037 module.outputFilePath = android.PathForModuleOut(ctx, libName+".xml").OutputPath
Colin Crossf1a035e2020-11-16 17:32:30 -08003038 rule := android.NewRuleBuilder(pctx, ctx)
Jiyong Parke3833882020-02-17 17:28:10 +09003039 rule.Command().
3040 Text("/bin/bash -c \"echo -e '" + xmlContent + "'\" > ").
3041 Output(module.outputFilePath)
3042
Colin Crossf1a035e2020-11-16 17:32:30 -08003043 rule.Build("java_sdk_xml", "Permission XML")
Jiyong Parke3833882020-02-17 17:28:10 +09003044
3045 module.installDirPath = android.PathForModuleInstall(ctx, "etc", module.SubDir())
3046}
3047
3048func (module *sdkLibraryXml) AndroidMkEntries() []android.AndroidMkEntries {
Colin Cross56a83212020-09-15 18:30:11 -07003049 if module.hideApexVariantFromMake {
satayev8f088b02021-12-06 11:40:46 +00003050 return []android.AndroidMkEntries{{
Jiyong Parke3833882020-02-17 17:28:10 +09003051 Disabled: true,
3052 }}
3053 }
3054
satayev8f088b02021-12-06 11:40:46 +00003055 return []android.AndroidMkEntries{{
Jiyong Parke3833882020-02-17 17:28:10 +09003056 Class: "ETC",
3057 OutputFile: android.OptionalPathForPath(module.outputFilePath),
3058 ExtraEntries: []android.AndroidMkExtraEntriesFunc{
Colin Crossaa255532020-07-03 13:18:24 -07003059 func(ctx android.AndroidMkExtraEntriesContext, entries *android.AndroidMkEntries) {
Jiyong Parke3833882020-02-17 17:28:10 +09003060 entries.SetString("LOCAL_MODULE_TAGS", "optional")
Colin Crossc68db4b2021-11-11 18:59:15 -08003061 entries.SetString("LOCAL_MODULE_PATH", module.installDirPath.String())
Jiyong Parke3833882020-02-17 17:28:10 +09003062 entries.SetString("LOCAL_INSTALLED_MODULE_STEM", module.outputFilePath.Base())
3063 },
3064 },
3065 }}
3066}
Paul Duffindd46f712020-02-10 13:37:10 +00003067
Pedro Loureiroc3621422021-09-28 15:40:23 +00003068func (module *sdkLibraryXml) selfValidate(ctx android.ModuleContext) {
3069 module.validateAtLeastTAttributes(ctx)
3070 module.validateMinAndMaxDeviceSdk(ctx)
3071 module.validateMinMaxDeviceSdkAndModuleMinSdk(ctx)
3072 module.validateOnBootclasspathBeforeRequirements(ctx)
3073}
3074
3075func (module *sdkLibraryXml) validateAtLeastTAttributes(ctx android.ModuleContext) {
3076 t := android.ApiLevelOrPanic(ctx, "Tiramisu")
3077 module.attrAtLeastT(ctx, t, module.properties.Min_device_sdk, "min_device_sdk")
3078 module.attrAtLeastT(ctx, t, module.properties.Max_device_sdk, "max_device_sdk")
3079 module.attrAtLeastT(ctx, t, module.properties.On_bootclasspath_before, "on_bootclasspath_before")
3080 module.attrAtLeastT(ctx, t, module.properties.On_bootclasspath_since, "on_bootclasspath_since")
3081}
3082
3083func (module *sdkLibraryXml) attrAtLeastT(ctx android.ModuleContext, t android.ApiLevel, attr *string, attrName string) {
3084 if attr != nil {
3085 if level, err := android.ApiLevelFromUser(ctx, *attr); err == nil {
3086 // we will inform the user of invalid inputs when we try to write the
3087 // permissions xml file so we don't need to do it here
3088 if t.GreaterThan(level) {
3089 ctx.PropertyErrorf(attrName, "Attribute value needs to be at least T")
3090 }
3091 }
3092 }
3093}
3094
3095func (module *sdkLibraryXml) validateMinAndMaxDeviceSdk(ctx android.ModuleContext) {
3096 if module.properties.Min_device_sdk != nil && module.properties.Max_device_sdk != nil {
3097 min, minErr := android.ApiLevelFromUser(ctx, *module.properties.Min_device_sdk)
3098 max, maxErr := android.ApiLevelFromUser(ctx, *module.properties.Max_device_sdk)
3099 if minErr == nil && maxErr == nil {
3100 // we will inform the user of invalid inputs when we try to write the
3101 // permissions xml file so we don't need to do it here
3102 if min.GreaterThan(max) {
3103 ctx.ModuleErrorf("min_device_sdk can't be greater than max_device_sdk")
3104 }
3105 }
3106 }
3107}
3108
3109func (module *sdkLibraryXml) validateMinMaxDeviceSdkAndModuleMinSdk(ctx android.ModuleContext) {
3110 moduleMinApi := android.ApiLevelOrPanic(ctx, *module.properties.Sdk_library_min_api_level)
3111 if module.properties.Min_device_sdk != nil {
3112 api, err := android.ApiLevelFromUser(ctx, *module.properties.Min_device_sdk)
3113 if err == nil {
3114 if moduleMinApi.GreaterThan(api) {
3115 ctx.PropertyErrorf("min_device_sdk", "Can't be less than module's min sdk (%s)", moduleMinApi)
3116 }
3117 }
3118 }
3119 if module.properties.Max_device_sdk != nil {
3120 api, err := android.ApiLevelFromUser(ctx, *module.properties.Max_device_sdk)
3121 if err == nil {
3122 if moduleMinApi.GreaterThan(api) {
3123 ctx.PropertyErrorf("max_device_sdk", "Can't be less than module's min sdk (%s)", moduleMinApi)
3124 }
3125 }
3126 }
3127}
3128
3129func (module *sdkLibraryXml) validateOnBootclasspathBeforeRequirements(ctx android.ModuleContext) {
3130 moduleMinApi := android.ApiLevelOrPanic(ctx, *module.properties.Sdk_library_min_api_level)
3131 if module.properties.On_bootclasspath_before != nil {
3132 t := android.ApiLevelOrPanic(ctx, "Tiramisu")
3133 // if we use the attribute, then we need to do this validation
3134 if moduleMinApi.LessThan(t) {
3135 // if minAPi is < T, then we need to have min_device_sdk (which only accepts T+)
3136 if module.properties.Min_device_sdk == nil {
3137 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")
3138 }
3139 }
3140 }
3141}
3142
Paul Duffindd46f712020-02-10 13:37:10 +00003143type sdkLibrarySdkMemberType struct {
3144 android.SdkMemberTypeBase
3145}
3146
Paul Duffin296701e2021-07-14 10:29:36 +01003147func (s *sdkLibrarySdkMemberType) AddDependencies(ctx android.SdkDependencyContext, dependencyTag blueprint.DependencyTag, names []string) {
3148 ctx.AddVariationDependencies(nil, dependencyTag, names...)
Paul Duffindd46f712020-02-10 13:37:10 +00003149}
3150
3151func (s *sdkLibrarySdkMemberType) IsInstance(module android.Module) bool {
3152 _, ok := module.(*SdkLibrary)
3153 return ok
3154}
3155
3156func (s *sdkLibrarySdkMemberType) AddPrebuiltModule(ctx android.SdkMemberContext, member android.SdkMember) android.BpModule {
3157 return ctx.SnapshotBuilder().AddPrebuiltModule(member, "java_sdk_library_import")
3158}
3159
3160func (s *sdkLibrarySdkMemberType) CreateVariantPropertiesStruct() android.SdkMemberProperties {
3161 return &sdkLibrarySdkMemberProperties{}
3162}
3163
Paul Duffin976b0e52021-04-27 23:20:26 +01003164var javaSdkLibrarySdkMemberType = &sdkLibrarySdkMemberType{
3165 android.SdkMemberTypeBase{
3166 PropertyName: "java_sdk_libs",
3167 SupportsSdk: true,
3168 },
3169}
3170
Paul Duffindd46f712020-02-10 13:37:10 +00003171type sdkLibrarySdkMemberProperties struct {
3172 android.SdkMemberPropertiesBase
3173
Paul Duffine8409952022-09-22 16:24:46 +01003174 // Stem name for files in the sdk snapshot.
3175 //
3176 // This is used to construct the path names of various sdk library files in the sdk snapshot to
3177 // make sure that they match the finalized versions of those files in prebuilts/sdk.
3178 //
3179 // This property is marked as keep so that it will be kept in all instances of this struct, will
3180 // not be cleared but will be copied to common structs. That is needed because this field is used
3181 // to construct many file names for other parts of this struct and so it needs to be present in
3182 // all structs. If it was not marked as keep then it would be cleared in some structs and so would
3183 // be unavailable for generating file names if there were other properties that were still set.
3184 Stem string `sdk:"keep"`
3185
Paul Duffindd46f712020-02-10 13:37:10 +00003186 // Scope to per scope properties.
Paul Duffin106a3a42022-01-27 16:39:06 +00003187 Scopes map[*apiScope]*scopeProperties
Paul Duffindd46f712020-02-10 13:37:10 +00003188
Paul Duffin3d1248c2020-04-09 00:10:17 +01003189 // The Java stubs source files.
3190 Stub_srcs []string
Paul Duffinf7a64332020-05-13 16:54:55 +01003191
3192 // The naming scheme.
3193 Naming_scheme *string
Paul Duffind7eb1c22020-05-26 20:57:10 +01003194
3195 // True if the java_sdk_library_import is for a shared library, false
3196 // otherwise.
3197 Shared_library *bool
Paul Duffina2ae7e02020-09-11 11:55:00 +01003198
Paul Duffin1267d872021-04-16 17:21:36 +01003199 // True if the stub imports should produce dex jars.
3200 Compile_dex *bool
3201
Paul Duffina2ae7e02020-09-11 11:55:00 +01003202 // The paths to the doctag files to add to the prebuilt.
3203 Doctag_paths android.Paths
Paul Duffin869de142021-07-15 14:14:41 +01003204
3205 Permitted_packages []string
Pedro Loureiro9956e5e2021-09-07 17:21:59 +00003206
3207 // Signals that this shared library is part of the bootclasspath starting
3208 // on the version indicated in this attribute.
3209 //
3210 // This will make platforms at this level and above to ignore
3211 // <uses-library> tags with this library name because the library is already
3212 // available
3213 On_bootclasspath_since *string
3214
3215 // Signals that this shared library was part of the bootclasspath before
3216 // (but not including) the version indicated in this attribute.
3217 //
3218 // The system will automatically add a <uses-library> tag with this library to
3219 // apps that target any SDK less than the version indicated in this attribute.
3220 On_bootclasspath_before *string
3221
3222 // Indicates that PackageManager should ignore this shared library if the
3223 // platform is below the version indicated in this attribute.
3224 //
3225 // This means that the device won't recognise this library as installed.
3226 Min_device_sdk *string
3227
3228 // Indicates that PackageManager should ignore this shared library if the
3229 // platform is above the version indicated in this attribute.
3230 //
3231 // This means that the device won't recognise this library as installed.
3232 Max_device_sdk *string
Jiakai Zhang9c4dc192023-02-09 00:09:24 +08003233
3234 DexPreoptProfileGuided *bool `supported_build_releases:"UpsideDownCake+"`
Paul Duffindd46f712020-02-10 13:37:10 +00003235}
3236
3237type scopeProperties struct {
Paul Duffin1fd005d2020-04-09 01:08:11 +01003238 Jars android.Paths
3239 StubsSrcJar android.Path
3240 CurrentApiFile android.Path
3241 RemovedApiFile android.Path
Paul Duffine7babdb2022-02-10 13:06:54 +00003242 AnnotationsZip android.Path `supported_build_releases:"Tiramisu+"`
Paul Duffin1fd005d2020-04-09 01:08:11 +01003243 SdkVersion string
Paul Duffindd46f712020-02-10 13:37:10 +00003244}
3245
3246func (s *sdkLibrarySdkMemberProperties) PopulateFromVariant(ctx android.SdkMemberContext, variant android.Module) {
3247 sdk := variant.(*SdkLibrary)
3248
Paul Duffine8409952022-09-22 16:24:46 +01003249 // Copy the stem name for files in the sdk snapshot.
3250 s.Stem = sdk.distStem()
3251
Paul Duffin106a3a42022-01-27 16:39:06 +00003252 s.Scopes = make(map[*apiScope]*scopeProperties)
Paul Duffindd46f712020-02-10 13:37:10 +00003253 for _, apiScope := range allApiScopes {
Paul Duffin803a9562020-05-20 11:52:25 +01003254 paths := sdk.findScopePaths(apiScope)
3255 if paths == nil {
3256 continue
3257 }
3258
Paul Duffindd46f712020-02-10 13:37:10 +00003259 jars := paths.stubsImplPath
3260 if len(jars) > 0 {
3261 properties := scopeProperties{}
3262 properties.Jars = jars
Paul Duffin780c5f42020-05-12 15:52:55 +01003263 properties.SdkVersion = sdk.sdkVersionForStubsLibrary(ctx.SdkModuleContext(), apiScope)
Paul Duffin0f8faff2020-05-20 16:18:00 +01003264 properties.StubsSrcJar = paths.stubsSrcJar.Path()
Paul Duffin10269f12020-06-19 18:39:55 +01003265 if paths.currentApiFilePath.Valid() {
3266 properties.CurrentApiFile = paths.currentApiFilePath.Path()
3267 }
3268 if paths.removedApiFilePath.Valid() {
3269 properties.RemovedApiFile = paths.removedApiFilePath.Path()
3270 }
Anton Hanssond78eb762021-09-21 15:25:12 +01003271 // The annotations zip is only available for modules that set annotations_enabled: true.
3272 if paths.annotationsZip.Valid() {
3273 properties.AnnotationsZip = paths.annotationsZip.Path()
3274 }
Paul Duffin106a3a42022-01-27 16:39:06 +00003275 s.Scopes[apiScope] = &properties
Paul Duffindd46f712020-02-10 13:37:10 +00003276 }
3277 }
3278
Paul Duffindfa131e2020-05-15 20:37:11 +01003279 s.Naming_scheme = sdk.commonSdkLibraryProperties.Naming_scheme
Paul Duffind7eb1c22020-05-26 20:57:10 +01003280 s.Shared_library = proptools.BoolPtr(sdk.sharedLibrary())
Paul Duffin1267d872021-04-16 17:21:36 +01003281 s.Compile_dex = sdk.dexProperties.Compile_dex
Paul Duffina2ae7e02020-09-11 11:55:00 +01003282 s.Doctag_paths = sdk.doctagPaths
Paul Duffin869de142021-07-15 14:14:41 +01003283 s.Permitted_packages = sdk.PermittedPackagesForUpdatableBootJars()
Pedro Loureiro9956e5e2021-09-07 17:21:59 +00003284 s.On_bootclasspath_since = sdk.commonSdkLibraryProperties.On_bootclasspath_since
3285 s.On_bootclasspath_before = sdk.commonSdkLibraryProperties.On_bootclasspath_before
3286 s.Min_device_sdk = sdk.commonSdkLibraryProperties.Min_device_sdk
3287 s.Max_device_sdk = sdk.commonSdkLibraryProperties.Max_device_sdk
Jiakai Zhang9c4dc192023-02-09 00:09:24 +08003288
3289 if sdk.dexpreopter.dexpreoptProperties.Dex_preopt_result.Profile_guided {
3290 s.DexPreoptProfileGuided = proptools.BoolPtr(true)
3291 }
Paul Duffindd46f712020-02-10 13:37:10 +00003292}
3293
3294func (s *sdkLibrarySdkMemberProperties) AddToPropertySet(ctx android.SdkMemberContext, propertySet android.BpPropertySet) {
Paul Duffinf7a64332020-05-13 16:54:55 +01003295 if s.Naming_scheme != nil {
3296 propertySet.AddProperty("naming_scheme", proptools.String(s.Naming_scheme))
3297 }
Paul Duffind7eb1c22020-05-26 20:57:10 +01003298 if s.Shared_library != nil {
3299 propertySet.AddProperty("shared_library", *s.Shared_library)
3300 }
Paul Duffin1267d872021-04-16 17:21:36 +01003301 if s.Compile_dex != nil {
3302 propertySet.AddProperty("compile_dex", *s.Compile_dex)
3303 }
Paul Duffin869de142021-07-15 14:14:41 +01003304 if len(s.Permitted_packages) > 0 {
3305 propertySet.AddProperty("permitted_packages", s.Permitted_packages)
3306 }
Jiakai Zhang9c4dc192023-02-09 00:09:24 +08003307 dexPreoptSet := propertySet.AddPropertySet("dex_preopt")
3308 if s.DexPreoptProfileGuided != nil {
3309 dexPreoptSet.AddProperty("profile_guided", proptools.Bool(s.DexPreoptProfileGuided))
3310 }
Paul Duffinf7a64332020-05-13 16:54:55 +01003311
Paul Duffine8409952022-09-22 16:24:46 +01003312 stem := s.Stem
3313
Paul Duffindd46f712020-02-10 13:37:10 +00003314 for _, apiScope := range allApiScopes {
3315 if properties, ok := s.Scopes[apiScope]; ok {
Paul Duffin6b836ba2020-05-13 19:19:49 +01003316 scopeSet := propertySet.AddPropertySet(apiScope.propertyName)
Paul Duffindd46f712020-02-10 13:37:10 +00003317
Paul Duffin958806b2022-05-16 13:10:47 +00003318 scopeDir := apiScope.snapshotRelativeDir()
Paul Duffin3d1248c2020-04-09 00:10:17 +01003319
Paul Duffindd46f712020-02-10 13:37:10 +00003320 var jars []string
3321 for _, p := range properties.Jars {
Paul Duffine8409952022-09-22 16:24:46 +01003322 dest := filepath.Join(scopeDir, stem+"-stubs.jar")
Paul Duffindd46f712020-02-10 13:37:10 +00003323 ctx.SnapshotBuilder().CopyToSnapshot(p, dest)
3324 jars = append(jars, dest)
3325 }
3326 scopeSet.AddProperty("jars", jars)
3327
Paul Duffin22628d52021-05-12 23:13:22 +01003328 if ctx.SdkModuleContext().Config().IsEnvTrue("SOONG_SDK_SNAPSHOT_USE_SRCJAR") {
3329 // Copy the stubs source jar into the snapshot zip as is.
Paul Duffine8409952022-09-22 16:24:46 +01003330 srcJarSnapshotPath := filepath.Join(scopeDir, stem+".srcjar")
Paul Duffin22628d52021-05-12 23:13:22 +01003331 ctx.SnapshotBuilder().CopyToSnapshot(properties.StubsSrcJar, srcJarSnapshotPath)
3332 scopeSet.AddProperty("stub_srcs", []string{srcJarSnapshotPath})
3333 } else {
3334 // Merge the stubs source jar into the snapshot zip so that when it is unpacked
3335 // the source files are also unpacked.
Paul Duffine8409952022-09-22 16:24:46 +01003336 snapshotRelativeDir := filepath.Join(scopeDir, stem+"_stub_sources")
Paul Duffin22628d52021-05-12 23:13:22 +01003337 ctx.SnapshotBuilder().UnzipToSnapshot(properties.StubsSrcJar, snapshotRelativeDir)
3338 scopeSet.AddProperty("stub_srcs", []string{snapshotRelativeDir})
3339 }
Paul Duffin3d1248c2020-04-09 00:10:17 +01003340
Paul Duffin1fd005d2020-04-09 01:08:11 +01003341 if properties.CurrentApiFile != nil {
Paul Duffine8409952022-09-22 16:24:46 +01003342 currentApiSnapshotPath := apiScope.snapshotRelativeCurrentApiTxtPath(stem)
Paul Duffin1fd005d2020-04-09 01:08:11 +01003343 ctx.SnapshotBuilder().CopyToSnapshot(properties.CurrentApiFile, currentApiSnapshotPath)
3344 scopeSet.AddProperty("current_api", currentApiSnapshotPath)
3345 }
3346
3347 if properties.RemovedApiFile != nil {
Paul Duffine8409952022-09-22 16:24:46 +01003348 removedApiSnapshotPath := apiScope.snapshotRelativeRemovedApiTxtPath(stem)
Paul Duffin3dbf9fd2020-06-02 13:00:02 +01003349 ctx.SnapshotBuilder().CopyToSnapshot(properties.RemovedApiFile, removedApiSnapshotPath)
Paul Duffin1fd005d2020-04-09 01:08:11 +01003350 scopeSet.AddProperty("removed_api", removedApiSnapshotPath)
3351 }
3352
Anton Hanssond78eb762021-09-21 15:25:12 +01003353 if properties.AnnotationsZip != nil {
Paul Duffine8409952022-09-22 16:24:46 +01003354 annotationsSnapshotPath := filepath.Join(scopeDir, stem+"_annotations.zip")
Anton Hanssond78eb762021-09-21 15:25:12 +01003355 ctx.SnapshotBuilder().CopyToSnapshot(properties.AnnotationsZip, annotationsSnapshotPath)
3356 scopeSet.AddProperty("annotations", annotationsSnapshotPath)
3357 }
3358
Paul Duffindd46f712020-02-10 13:37:10 +00003359 if properties.SdkVersion != "" {
3360 scopeSet.AddProperty("sdk_version", properties.SdkVersion)
3361 }
3362 }
3363 }
3364
Paul Duffina2ae7e02020-09-11 11:55:00 +01003365 if len(s.Doctag_paths) > 0 {
3366 dests := []string{}
3367 for _, p := range s.Doctag_paths {
3368 dest := filepath.Join("doctags", p.Rel())
3369 ctx.SnapshotBuilder().CopyToSnapshot(p, dest)
3370 dests = append(dests, dest)
3371 }
3372 propertySet.AddProperty("doctag_files", dests)
3373 }
Paul Duffindd46f712020-02-10 13:37:10 +00003374}