blob: 282379cc4a8362615fd5828941e814789bc1c819 [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
Jihoon Kanga96a7b12023-09-20 23:43:32 +0000289// Method that maps the apiScopes properties to the index of each apiScopes elements.
290// apiScopes property to be used as the key can be specified with the input accessor.
291// Only a string property of apiScope can be used as the key of the map.
292func (scopes apiScopes) MapToIndex(accessor func(*apiScope) string) map[string]int {
293 ret := make(map[string]int)
294 for i, scope := range scopes {
295 ret[accessor(scope)] = i
296 }
297 return ret
298}
299
Jiyong Parkc678ad32018-04-10 13:07:10 +0900300var (
Paul Duffin46dc45a2020-05-14 15:39:10 +0100301 scopeByName = make(map[string]*apiScope)
302 allScopeNames []string
Paul Duffind1b3a922020-01-22 11:57:20 +0000303 apiScopePublic = initApiScope(&apiScope{
Paul Duffin3375e352020-04-28 10:44:03 +0100304 name: "public",
305
306 // Public scope is enabled by default for both legacy and non-legacy modes.
307 legacyEnabledStatus: func(module *SdkLibrary) bool {
308 return true
309 },
310 defaultEnabledStatus: true,
311
312 scopeSpecificProperties: func(module *SdkLibrary) *ApiScopeProperties {
313 return &module.sdkLibraryProperties.Public
314 },
Paul Duffind1b3a922020-01-22 11:57:20 +0000315 sdkVersion: "current",
Jihoon Kang1c92c3e2023-03-23 17:44:51 +0000316 kind: android.SdkPublic,
Paul Duffind1b3a922020-01-22 11:57:20 +0000317 })
318 apiScopeSystem = initApiScope(&apiScope{
Paul Duffin3375e352020-04-28 10:44:03 +0100319 name: "system",
320 extends: apiScopePublic,
321 legacyEnabledStatus: (*SdkLibrary).generateTestAndSystemScopesByDefault,
322 scopeSpecificProperties: func(module *SdkLibrary) *ApiScopeProperties {
323 return &module.sdkLibraryProperties.System
324 },
Paul Duffin15f34ef2020-07-20 18:04:44 +0100325 apiFilePrefix: "system-",
326 moduleSuffix: ".system",
327 sdkVersion: "system_current",
328 annotation: "android.annotation.SystemApi(client=android.annotation.SystemApi.Client.PRIVILEGED_APPS)",
Jihoon Kang1c92c3e2023-03-23 17:44:51 +0000329 kind: android.SdkSystem,
Paul Duffind1b3a922020-01-22 11:57:20 +0000330 })
331 apiScopeTest = initApiScope(&apiScope{
Paul Duffin3375e352020-04-28 10:44:03 +0100332 name: "test",
Anton Hansson4fe970f2020-10-09 10:16:49 +0100333 extends: apiScopeSystem,
Paul Duffin3375e352020-04-28 10:44:03 +0100334 legacyEnabledStatus: (*SdkLibrary).generateTestAndSystemScopesByDefault,
335 scopeSpecificProperties: func(module *SdkLibrary) *ApiScopeProperties {
336 return &module.sdkLibraryProperties.Test
337 },
Paul Duffin15f34ef2020-07-20 18:04:44 +0100338 apiFilePrefix: "test-",
339 moduleSuffix: ".test",
340 sdkVersion: "test_current",
341 annotation: "android.annotation.TestApi",
342 unstable: true,
Jihoon Kang1c92c3e2023-03-23 17:44:51 +0000343 kind: android.SdkTest,
Paul Duffind1b3a922020-01-22 11:57:20 +0000344 })
Paul Duffin8f265b92020-04-28 14:13:56 +0100345 apiScopeModuleLib = initApiScope(&apiScope{
Paul Duffin6b836ba2020-05-13 19:19:49 +0100346 name: "module-lib",
Paul Duffin8f265b92020-04-28 14:13:56 +0100347 extends: apiScopeSystem,
Paul Duffin0c5bae52020-06-02 13:00:08 +0100348 // The module-lib scope is disabled by default in legacy mode.
Paul Duffin8f265b92020-04-28 14:13:56 +0100349 //
350 // Enabling this would break existing usages.
351 legacyEnabledStatus: func(module *SdkLibrary) bool {
352 return false
353 },
354 scopeSpecificProperties: func(module *SdkLibrary) *ApiScopeProperties {
355 return &module.sdkLibraryProperties.Module_lib
356 },
357 apiFilePrefix: "module-lib-",
358 moduleSuffix: ".module_lib",
359 sdkVersion: "module_current",
Paul Duffin15f34ef2020-07-20 18:04:44 +0100360 annotation: "android.annotation.SystemApi(client=android.annotation.SystemApi.Client.MODULE_LIBRARIES)",
Jihoon Kang1c92c3e2023-03-23 17:44:51 +0000361 kind: android.SdkModule,
Paul Duffin8f265b92020-04-28 14:13:56 +0100362 })
Paul Duffin0c5bae52020-06-02 13:00:08 +0100363 apiScopeSystemServer = initApiScope(&apiScope{
364 name: "system-server",
365 extends: apiScopePublic,
Paul Duffind0b9fca2022-09-30 18:11:41 +0100366
367 // The system-server scope can access the module-lib scope.
368 //
369 // A module that provides a system-server API is appended to the standard bootclasspath that is
370 // used by the system server. So, it should be able to access module-lib APIs provided by
371 // libraries on the bootclasspath.
372 canAccess: apiScopeModuleLib,
373
Paul Duffin0c5bae52020-06-02 13:00:08 +0100374 // The system-server scope is disabled by default in legacy mode.
375 //
376 // Enabling this would break existing usages.
377 legacyEnabledStatus: func(module *SdkLibrary) bool {
378 return false
379 },
380 scopeSpecificProperties: func(module *SdkLibrary) *ApiScopeProperties {
381 return &module.sdkLibraryProperties.System_server
382 },
383 apiFilePrefix: "system-server-",
384 moduleSuffix: ".system_server",
385 sdkVersion: "system_server_current",
Paul Duffin15f34ef2020-07-20 18:04:44 +0100386 annotation: "android.annotation.SystemApi(client=android.annotation.SystemApi.Client.SYSTEM_SERVER)",
387 extraArgs: []string{
388 "--hide-annotation", "android.annotation.Hide",
Paul Duffin0c5bae52020-06-02 13:00:08 +0100389 // com.android.* classes are okay in this interface"
Paul Duffin15f34ef2020-07-20 18:04:44 +0100390 "--hide", "InternalClasses",
Paul Duffin0c5bae52020-06-02 13:00:08 +0100391 },
Jihoon Kang1c92c3e2023-03-23 17:44:51 +0000392 kind: android.SdkSystemServer,
Paul Duffin0c5bae52020-06-02 13:00:08 +0100393 })
Paul Duffind1b3a922020-01-22 11:57:20 +0000394 allApiScopes = apiScopes{
395 apiScopePublic,
396 apiScopeSystem,
397 apiScopeTest,
Paul Duffin8f265b92020-04-28 14:13:56 +0100398 apiScopeModuleLib,
Paul Duffin0c5bae52020-06-02 13:00:08 +0100399 apiScopeSystemServer,
Paul Duffind1b3a922020-01-22 11:57:20 +0000400 }
Jihoon Kang0c705a42023-08-02 06:44:57 +0000401 apiLibraryAdditionalProperties = map[string]struct {
402 FullApiSurfaceStubLib string
403 AdditionalApiContribution string
404 }{
405 "legacy.i18n.module.platform.api": {
406 FullApiSurfaceStubLib: "legacy.core.platform.api.stubs",
407 AdditionalApiContribution: "i18n.module.public.api.stubs.source.api.contribution",
408 },
409 "stable.i18n.module.platform.api": {
410 FullApiSurfaceStubLib: "stable.core.platform.api.stubs",
411 AdditionalApiContribution: "i18n.module.public.api.stubs.source.api.contribution",
412 },
413 "conscrypt.module.platform.api": {
414 FullApiSurfaceStubLib: "stable.core.platform.api.stubs",
415 AdditionalApiContribution: "conscrypt.module.public.api.stubs.source.api.contribution",
416 },
417 }
Jiyong Parkc678ad32018-04-10 13:07:10 +0900418)
419
Jiyong Park82484c02018-04-23 21:41:26 +0900420var (
421 javaSdkLibrariesLock sync.Mutex
422)
423
Jiyong Parkc678ad32018-04-10 13:07:10 +0900424// TODO: these are big features that are currently missing
Jiyong Park1be96912018-05-28 18:02:19 +0900425// 1) disallowing linking to the runtime shared lib
426// 2) HTML generation
Jiyong Parkc678ad32018-04-10 13:07:10 +0900427
428func init() {
Paul Duffin43dc1cc2019-12-19 11:18:54 +0000429 RegisterSdkLibraryBuildComponents(android.InitRegistrationContext)
Jiyong Parkc678ad32018-04-10 13:07:10 +0900430
Jiyong Park82484c02018-04-23 21:41:26 +0900431 android.RegisterMakeVarsProvider(pctx, func(ctx android.MakeVarsContext) {
432 javaSdkLibraries := javaSdkLibraries(ctx.Config())
433 sort.Strings(*javaSdkLibraries)
434 ctx.Strict("JAVA_SDK_LIBRARIES", strings.Join(*javaSdkLibraries, " "))
435 })
Paul Duffindd46f712020-02-10 13:37:10 +0000436
437 // Register sdk member types.
Paul Duffin976b0e52021-04-27 23:20:26 +0100438 android.RegisterSdkMemberType(javaSdkLibrarySdkMemberType)
Jiyong Parkc678ad32018-04-10 13:07:10 +0900439}
440
Paul Duffin43dc1cc2019-12-19 11:18:54 +0000441func RegisterSdkLibraryBuildComponents(ctx android.RegistrationContext) {
442 ctx.RegisterModuleType("java_sdk_library", SdkLibraryFactory)
443 ctx.RegisterModuleType("java_sdk_library_import", sdkLibraryImportFactory)
444}
445
Paul Duffin3375e352020-04-28 10:44:03 +0100446// Properties associated with each api scope.
447type ApiScopeProperties struct {
448 // Indicates whether the api surface is generated.
449 //
450 // If this is set for any scope then all scopes must explicitly specify if they
451 // are enabled. This is to prevent new usages from depending on legacy behavior.
452 //
453 // Otherwise, if this is not set for any scope then the default behavior is
454 // scope specific so please refer to the scope specific property documentation.
455 Enabled *bool
Paul Duffin87a05a32020-05-12 11:50:28 +0100456
457 // The sdk_version to use for building the stubs.
458 //
459 // If not specified then it will use an sdk_version determined as follows:
Trevor Radcliffedf8aa1f2021-11-04 14:25:39 +0000460 //
Paul Duffin87a05a32020-05-12 11:50:28 +0100461 // 1) If the sdk_version specified on the java_sdk_library is none then this
Trevor Radcliffedf8aa1f2021-11-04 14:25:39 +0000462 // will be none. This is used for java_sdk_library instances that are used
463 // to create stubs that contribute to the core_current sdk version.
464 // 2) Otherwise, it is assumed that this library extends but does not
465 // contribute directly to a specific sdk_version and so this uses the
466 // sdk_version appropriate for the api scope. e.g. public will use
467 // sdk_version: current, system will use sdk_version: system_current, etc.
Paul Duffin87a05a32020-05-12 11:50:28 +0100468 //
469 // This does not affect the sdk_version used for either generating the stubs source
470 // or the API file. They both have to use the same sdk_version as is used for
471 // compiling the implementation library.
472 Sdk_version *string
Paul Duffin3375e352020-04-28 10:44:03 +0100473}
474
Jiyong Parkc678ad32018-04-10 13:07:10 +0900475type sdkLibraryProperties struct {
Anton Hanssonf8ea3722021-09-16 14:24:13 +0100476 // List of source files that are needed to compile the API, but are not part of runtime library.
477 Api_srcs []string `android:"arch_variant"`
478
Paul Duffin5df79302020-05-16 15:52:12 +0100479 // Visibility for impl library module. If not specified then defaults to the
480 // visibility property.
481 Impl_library_visibility []string
482
Paul Duffin4911a892020-04-29 23:35:13 +0100483 // Visibility for stubs library modules. If not specified then defaults to the
484 // visibility property.
485 Stubs_library_visibility []string
486
487 // Visibility for stubs source modules. If not specified then defaults to the
488 // visibility property.
489 Stubs_source_visibility []string
490
Anton Hansson7f66efa2020-10-08 14:47:23 +0100491 // List of Java libraries that will be in the classpath when building the implementation lib
492 Impl_only_libs []string `android:"arch_variant"`
493
Paul Duffin77590a82022-04-28 14:13:30 +0000494 // List of Java libraries that will included in the implementation lib.
495 Impl_only_static_libs []string `android:"arch_variant"`
496
Sundong Ahnf043cf62018-06-25 16:04:37 +0900497 // List of Java libraries that will be in the classpath when building stubs
498 Stub_only_libs []string `android:"arch_variant"`
499
Anton Hanssondae54cd2021-04-21 16:30:10 +0100500 // List of Java libraries that will included in stub libraries
501 Stub_only_static_libs []string `android:"arch_variant"`
502
Paul Duffin7a586d32019-12-30 17:09:34 +0000503 // list of package names that will be documented and publicized as API.
504 // This allows the API to be restricted to a subset of the source files provided.
505 // If this is unspecified then all the source files will be treated as being part
506 // of the API.
Jiyong Parkc678ad32018-04-10 13:07:10 +0900507 Api_packages []string
508
Jiyong Park5a2c9d72018-05-01 22:25:41 +0900509 // list of package names that must be hidden from the API
510 Hidden_api_packages []string
511
Paul Duffin749f98f2019-12-30 17:23:46 +0000512 // the relative path to the directory containing the api specification files.
513 // Defaults to "api".
514 Api_dir *string
515
Paul Duffindfa131e2020-05-15 20:37:11 +0100516 // Determines whether a runtime implementation library is built; defaults to false.
517 //
518 // If true then it also prevents the module from being used as a shared module, i.e.
MÃ¥rten Kongstad81d90952022-05-25 16:27:11 +0200519 // it is as if shared_library: false, was set.
Paul Duffin43db9be2019-12-30 17:35:49 +0000520 Api_only *bool
521
Paul Duffin11512472019-02-11 15:55:17 +0000522 // local files that are used within user customized droiddoc options.
523 Droiddoc_option_files []string
524
Spandan Das93e95992021-07-29 18:26:39 +0000525 // additional droiddoc options.
Paul Duffin11512472019-02-11 15:55:17 +0000526 // Available variables for substitution:
527 //
528 // $(location <label>): the path to the droiddoc_option_files with name <label>
Sundong Ahndd567f92018-07-31 17:19:11 +0900529 Droiddoc_options []string
530
Paul Duffine22c2ab2020-05-20 19:35:27 +0100531 // is set to true, Metalava will allow framework SDK to contain annotations.
532 Annotations_enabled *bool
533
Sundong Ahn054b19a2018-10-19 13:46:09 +0900534 // a list of top-level directories containing files to merge qualifier annotations
535 // (i.e. those intended to be included in the stubs written) from.
536 Merge_annotations_dirs []string
537
538 // a list of top-level directories containing Java stub files to merge show/hide annotations from.
539 Merge_inclusion_annotations_dirs []string
540
Paul Duffin4f5c1ef2020-11-19 14:53:43 +0000541 // If set to true then don't create dist rules.
542 No_dist *bool
Sundong Ahn80a87b32019-05-13 15:02:50 +0900543
Paul Duffin31310252020-11-20 21:26:20 +0000544 // The stem for the artifacts that are copied to the dist, if not specified
545 // then defaults to the base module name.
546 //
547 // For each scope the following artifacts are copied to the apistubs/<scope>
548 // directory in the dist.
549 // * stubs impl jar -> <dist-stem>.jar
550 // * API specification file -> api/<dist-stem>.txt
551 // * Removed API specification file -> api/<dist-stem>-removed.txt
552 //
553 // Also used to construct the name of the filegroup (created by prebuilt_apis)
554 // that references the latest released API and remove API specification files.
555 // * API specification filegroup -> <dist-stem>.api.<scope>.latest
556 // * Removed API specification filegroup -> <dist-stem>-removed.api.<scope>.latest
Jaewoong Jung1a97ee02021-03-09 13:25:02 -0800557 // * API incompatibilities baseline filegroup -> <dist-stem>-incompatibilities.api.<scope>.latest
Paul Duffin31310252020-11-20 21:26:20 +0000558 Dist_stem *string
559
Colin Cross986b69a2021-06-01 13:13:40 -0700560 // The subdirectory for the artifacts that are copied to the dist directory. If not specified
Colin Cross3dd66252021-06-01 14:05:09 -0700561 // then defaults to "unknown". Should be set to "android" for anything that should be published
Colin Cross986b69a2021-06-01 13:13:40 -0700562 // in the public Android SDK.
563 Dist_group *string
564
Anton Hanssondff2c782020-12-21 17:10:01 +0000565 // A compatibility mode that allows historical API-tracking files to not exist.
566 // Do not use.
567 Unsafe_ignore_missing_latest_api bool
568
Paul Duffin3375e352020-04-28 10:44:03 +0100569 // indicates whether system and test apis should be generated.
570 Generate_system_and_test_apis bool `blueprint:"mutated"`
571
572 // The properties specific to the public api scope
573 //
574 // Unless explicitly specified by using public.enabled the public api scope is
575 // enabled by default in both legacy and non-legacy mode.
576 Public ApiScopeProperties
577
578 // The properties specific to the system api scope
579 //
580 // In legacy mode the system api scope is enabled by default when sdk_version
581 // is set to something other than "none".
582 //
583 // In non-legacy mode the system api scope is disabled by default.
584 System ApiScopeProperties
585
586 // The properties specific to the test api scope
587 //
588 // In legacy mode the test api scope is enabled by default when sdk_version
589 // is set to something other than "none".
590 //
591 // In non-legacy mode the test api scope is disabled by default.
592 Test ApiScopeProperties
Paul Duffin37e0b772019-12-30 17:20:10 +0000593
Paul Duffin0c5bae52020-06-02 13:00:08 +0100594 // The properties specific to the module-lib api scope
Paul Duffin8f265b92020-04-28 14:13:56 +0100595 //
Zi Wangb2179e32023-01-31 15:53:30 -0800596 // Unless explicitly specified by using module_lib.enabled the module_lib api
597 // scope is disabled by default.
Paul Duffin8f265b92020-04-28 14:13:56 +0100598 Module_lib ApiScopeProperties
599
Paul Duffin0c5bae52020-06-02 13:00:08 +0100600 // The properties specific to the system-server api scope
601 //
Zi Wangb2179e32023-01-31 15:53:30 -0800602 // Unless explicitly specified by using system_server.enabled the
603 // system_server api scope is disabled by default.
Paul Duffin0c5bae52020-06-02 13:00:08 +0100604 System_server ApiScopeProperties
605
Jiyong Park932cdfe2020-05-28 00:19:53 +0900606 // Determines if the stubs are preferred over the implementation library
607 // for linking, even when the client doesn't specify sdk_version. When this
608 // is set to true, such clients are provided with the widest API surface that
609 // this lib provides. Note however that this option doesn't affect the clients
610 // that are in the same APEX as this library. In that case, the clients are
611 // always linked with the implementation library. Default is false.
612 Default_to_stubs *bool
613
Paul Duffin160fe412020-05-10 19:32:20 +0100614 // Properties related to api linting.
615 Api_lint struct {
616 // Enable api linting.
617 Enabled *bool
618 }
619
Jiyong Parkc678ad32018-04-10 13:07:10 +0900620 // TODO: determines whether to create HTML doc or not
Paul Duffine8409952022-09-22 16:24:46 +0100621 // Html_doc *bool
Jiyong Parkc678ad32018-04-10 13:07:10 +0900622}
623
Paul Duffin0f8faff2020-05-20 16:18:00 +0100624// Paths to outputs from java_sdk_library and java_sdk_library_import.
625//
626// Fields that are android.Paths are always set (during GenerateAndroidBuildActions).
627// OptionalPaths are always set by java_sdk_library but may not be set by
628// java_sdk_library_import as not all instances provide that information.
Paul Duffind1b3a922020-01-22 11:57:20 +0000629type scopePaths struct {
Paul Duffin0f8faff2020-05-20 16:18:00 +0100630 // The path (represented as Paths for convenience when returning) to the stubs header jar.
631 //
632 // That is the jar that is created by turbine.
633 stubsHeaderPath android.Paths
634
635 // The path (represented as Paths for convenience when returning) to the stubs implementation jar.
636 //
637 // This is not the implementation jar, it still only contains stubs.
638 stubsImplPath android.Paths
639
Paul Duffin1267d872021-04-16 17:21:36 +0100640 // The dex jar for the stubs.
641 //
642 // This is not the implementation jar, it still only contains stubs.
Martin Stjernholm8be1e6d2021-09-15 03:34:04 +0100643 stubsDexJarPath OptionalDexJarPath
Paul Duffin1267d872021-04-16 17:21:36 +0100644
Paul Duffin0f8faff2020-05-20 16:18:00 +0100645 // The API specification file, e.g. system_current.txt.
646 currentApiFilePath android.OptionalPath
647
648 // The specification of API elements removed since the last release.
649 removedApiFilePath android.OptionalPath
650
651 // The stubs source jar.
652 stubsSrcJar android.OptionalPath
Anton Hanssond78eb762021-09-21 15:25:12 +0100653
654 // Extracted annotations.
655 annotationsZip android.OptionalPath
Paul Duffin958806b2022-05-16 13:10:47 +0000656
657 // The path to the latest API file.
658 latestApiPath android.OptionalPath
659
660 // The path to the latest removed API file.
661 latestRemovedApiPath android.OptionalPath
Paul Duffind1b3a922020-01-22 11:57:20 +0000662}
663
Colin Crossdcf71b22021-02-01 13:59:03 -0800664func (paths *scopePaths) extractStubsLibraryInfoFromDependency(ctx android.ModuleContext, dep android.Module) error {
665 if ctx.OtherModuleHasProvider(dep, JavaInfoProvider) {
666 lib := ctx.OtherModuleProvider(dep, JavaInfoProvider).(JavaInfo)
667 paths.stubsHeaderPath = lib.HeaderJars
668 paths.stubsImplPath = lib.ImplementationJars
Paul Duffin1267d872021-04-16 17:21:36 +0100669
670 libDep := dep.(UsesLibraryDependency)
671 paths.stubsDexJarPath = libDep.DexJarBuildPath()
Paul Duffinc8782502020-04-29 20:45:27 +0100672 return nil
673 } else {
Colin Crossdcf71b22021-02-01 13:59:03 -0800674 return fmt.Errorf("expected module that has JavaInfoProvider, e.g. java_library")
Paul Duffinc8782502020-04-29 20:45:27 +0100675 }
676}
677
Paul Duffin0ff08bd2020-04-29 13:30:54 +0100678func (paths *scopePaths) treatDepAsApiStubsProvider(dep android.Module, action func(provider ApiStubsProvider)) error {
679 if apiStubsProvider, ok := dep.(ApiStubsProvider); ok {
680 action(apiStubsProvider)
Paul Duffinc8782502020-04-29 20:45:27 +0100681 return nil
682 } else {
683 return fmt.Errorf("expected module that implements ApiStubsProvider, e.g. droidstubs")
684 }
685}
686
Paul Duffin0f8faff2020-05-20 16:18:00 +0100687func (paths *scopePaths) treatDepAsApiStubsSrcProvider(dep android.Module, action func(provider ApiStubsSrcProvider)) error {
688 if apiStubsProvider, ok := dep.(ApiStubsSrcProvider); ok {
689 action(apiStubsProvider)
690 return nil
691 } else {
692 return fmt.Errorf("expected module that implements ApiStubsSrcProvider, e.g. droidstubs")
693 }
694}
695
Paul Duffin0ff08bd2020-04-29 13:30:54 +0100696func (paths *scopePaths) extractApiInfoFromApiStubsProvider(provider ApiStubsProvider) {
Anton Hanssond78eb762021-09-21 15:25:12 +0100697 paths.annotationsZip = android.OptionalPathForPath(provider.AnnotationsZip())
Paul Duffin0f8faff2020-05-20 16:18:00 +0100698 paths.currentApiFilePath = android.OptionalPathForPath(provider.ApiFilePath())
699 paths.removedApiFilePath = android.OptionalPathForPath(provider.RemovedApiFilePath())
Paul Duffin0ff08bd2020-04-29 13:30:54 +0100700}
701
Colin Crossdcf71b22021-02-01 13:59:03 -0800702func (paths *scopePaths) extractApiInfoFromDep(ctx android.ModuleContext, dep android.Module) error {
Paul Duffin0ff08bd2020-04-29 13:30:54 +0100703 return paths.treatDepAsApiStubsProvider(dep, func(provider ApiStubsProvider) {
704 paths.extractApiInfoFromApiStubsProvider(provider)
705 })
706}
707
Paul Duffin0f8faff2020-05-20 16:18:00 +0100708func (paths *scopePaths) extractStubsSourceInfoFromApiStubsProviders(provider ApiStubsSrcProvider) {
709 paths.stubsSrcJar = android.OptionalPathForPath(provider.StubsSrcJar())
Paul Duffin0ff08bd2020-04-29 13:30:54 +0100710}
711
Colin Crossdcf71b22021-02-01 13:59:03 -0800712func (paths *scopePaths) extractStubsSourceInfoFromDep(ctx android.ModuleContext, dep android.Module) error {
Paul Duffin0f8faff2020-05-20 16:18:00 +0100713 return paths.treatDepAsApiStubsSrcProvider(dep, func(provider ApiStubsSrcProvider) {
Paul Duffin0ff08bd2020-04-29 13:30:54 +0100714 paths.extractStubsSourceInfoFromApiStubsProviders(provider)
715 })
716}
717
Colin Crossdcf71b22021-02-01 13:59:03 -0800718func (paths *scopePaths) extractStubsSourceAndApiInfoFromApiStubsProvider(ctx android.ModuleContext, dep android.Module) error {
Paul Duffin0ff08bd2020-04-29 13:30:54 +0100719 return paths.treatDepAsApiStubsProvider(dep, func(provider ApiStubsProvider) {
720 paths.extractApiInfoFromApiStubsProvider(provider)
721 paths.extractStubsSourceInfoFromApiStubsProviders(provider)
722 })
723}
724
Paul Duffin958806b2022-05-16 13:10:47 +0000725func extractSingleOptionalOutputPath(dep android.Module) (android.OptionalPath, error) {
726 var paths android.Paths
727 if sourceFileProducer, ok := dep.(android.SourceFileProducer); ok {
728 paths = sourceFileProducer.Srcs()
729 } else {
730 return android.OptionalPath{}, fmt.Errorf("module %q does not produce source files", dep)
731 }
732 if len(paths) != 1 {
733 return android.OptionalPath{}, fmt.Errorf("expected one path from %q, got %q", dep, paths)
734 }
735 return android.OptionalPathForPath(paths[0]), nil
736}
737
738func (paths *scopePaths) extractLatestApiPath(ctx android.ModuleContext, dep android.Module) error {
739 outputPath, err := extractSingleOptionalOutputPath(dep)
740 paths.latestApiPath = outputPath
741 return err
742}
743
744func (paths *scopePaths) extractLatestRemovedApiPath(ctx android.ModuleContext, dep android.Module) error {
745 outputPath, err := extractSingleOptionalOutputPath(dep)
746 paths.latestRemovedApiPath = outputPath
747 return err
748}
749
Paul Duffin0ff08bd2020-04-29 13:30:54 +0100750type commonToSdkLibraryAndImportProperties struct {
Paul Duffin1b1e8062020-05-08 13:44:43 +0100751 // The naming scheme to use for the components that this module creates.
752 //
Paul Duffinee9ad5d2020-09-11 13:04:05 +0100753 // If not specified then it defaults to "default".
Paul Duffin1b1e8062020-05-08 13:44:43 +0100754 //
755 // This is a temporary mechanism to simplify conversion from separate modules for each
756 // component that follow a different naming pattern to the default one.
757 //
758 // TODO(b/155480189) - Remove once naming inconsistencies have been resolved.
Paul Duffin0ff08bd2020-04-29 13:30:54 +0100759 Naming_scheme *string
Paul Duffindfa131e2020-05-15 20:37:11 +0100760
761 // Specifies whether this module can be used as an Android shared library; defaults
762 // to true.
763 //
764 // An Android shared library is one that can be referenced in a <uses-library> element
765 // in an AndroidManifest.xml.
766 Shared_library *bool
Paul Duffina2ae7e02020-09-11 11:55:00 +0100767
768 // Files containing information about supported java doc tags.
769 Doctag_files []string `android:"path"`
Pedro Loureiro9956e5e2021-09-07 17:21:59 +0000770
771 // Signals that this shared library is part of the bootclasspath starting
772 // on the version indicated in this attribute.
773 //
774 // This will make platforms at this level and above to ignore
775 // <uses-library> tags with this library name because the library is already
776 // available
777 On_bootclasspath_since *string
778
779 // Signals that this shared library was part of the bootclasspath before
780 // (but not including) the version indicated in this attribute.
781 //
782 // The system will automatically add a <uses-library> tag with this library to
783 // apps that target any SDK less than the version indicated in this attribute.
784 On_bootclasspath_before *string
785
786 // Indicates that PackageManager should ignore this shared library if the
787 // platform is below the version indicated in this attribute.
788 //
789 // This means that the device won't recognise this library as installed.
790 Min_device_sdk *string
791
792 // Indicates that PackageManager should ignore this shared library if the
793 // platform is above the version indicated in this attribute.
794 //
795 // This means that the device won't recognise this library as installed.
796 Max_device_sdk *string
Paul Duffin0ff08bd2020-04-29 13:30:54 +0100797}
798
Paul Duffin71b33cc2021-06-23 11:39:47 +0100799// commonSdkLibraryAndImportModule defines the interface that must be provided by a module that
800// embeds the commonToSdkLibraryAndImport struct.
801type commonSdkLibraryAndImportModule interface {
Paul Duffind796f6f2022-11-23 23:06:05 +0000802 android.Module
Paul Duffin71b33cc2021-06-23 11:39:47 +0100803
804 BaseModuleName() string
805}
806
Paul Duffin56d44902020-01-31 13:36:25 +0000807// Common code between sdk library and sdk library import
808type commonToSdkLibraryAndImport struct {
Paul Duffin71b33cc2021-06-23 11:39:47 +0100809 module commonSdkLibraryAndImportModule
Paul Duffinc3091c82020-05-08 14:16:20 +0100810
Paul Duffin56d44902020-01-31 13:36:25 +0000811 scopePaths map[*apiScope]*scopePaths
Paul Duffin1b1e8062020-05-08 13:44:43 +0100812
813 namingScheme sdkLibraryComponentNamingScheme
814
Paul Duffindfa131e2020-05-15 20:37:11 +0100815 commonSdkLibraryProperties commonToSdkLibraryAndImportProperties
Paul Duffin859fe962020-05-15 10:20:31 +0100816
Paul Duffina2ae7e02020-09-11 11:55:00 +0100817 // Paths to commonSdkLibraryProperties.Doctag_files
818 doctagPaths android.Paths
819
Paul Duffin859fe962020-05-15 10:20:31 +0100820 // Functionality related to this being used as a component of a java_sdk_library.
821 EmbeddableSdkLibraryComponent
Paul Duffin56d44902020-01-31 13:36:25 +0000822}
823
Paul Duffin71b33cc2021-06-23 11:39:47 +0100824func (c *commonToSdkLibraryAndImport) initCommon(module commonSdkLibraryAndImportModule) {
825 c.module = module
Paul Duffin1b1e8062020-05-08 13:44:43 +0100826
Paul Duffin71b33cc2021-06-23 11:39:47 +0100827 module.AddProperties(&c.commonSdkLibraryProperties)
Paul Duffin859fe962020-05-15 10:20:31 +0100828
829 // Initialize this as an sdk library component.
Paul Duffin71b33cc2021-06-23 11:39:47 +0100830 c.initSdkLibraryComponent(module)
Paul Duffin1b1e8062020-05-08 13:44:43 +0100831}
832
833func (c *commonToSdkLibraryAndImport) initCommonAfterDefaultsApplied(ctx android.DefaultableHookContext) bool {
Paul Duffindfa131e2020-05-15 20:37:11 +0100834 schemeProperty := proptools.StringDefault(c.commonSdkLibraryProperties.Naming_scheme, "default")
Paul Duffin1b1e8062020-05-08 13:44:43 +0100835 switch schemeProperty {
836 case "default":
837 c.namingScheme = &defaultNamingScheme{}
838 default:
839 ctx.PropertyErrorf("naming_scheme", "expected 'default' but was %q", schemeProperty)
840 return false
841 }
842
Paul Duffin3f0290e2021-06-30 18:25:36 +0100843 namePtr := proptools.StringPtr(c.module.BaseModuleName())
844 c.sdkLibraryComponentProperties.SdkLibraryName = namePtr
845
Paul Duffindfa131e2020-05-15 20:37:11 +0100846 // Only track this sdk library if this can be used as a shared library.
847 if c.sharedLibrary() {
848 // Use the name specified in the module definition as the owner.
Paul Duffin3f0290e2021-06-30 18:25:36 +0100849 c.sdkLibraryComponentProperties.SdkLibraryToImplicitlyTrack = namePtr
Paul Duffindfa131e2020-05-15 20:37:11 +0100850 }
Paul Duffin859fe962020-05-15 10:20:31 +0100851
Paul Duffin1b1e8062020-05-08 13:44:43 +0100852 return true
Paul Duffinc3091c82020-05-08 14:16:20 +0100853}
854
Paul Duffinea8f8082021-06-24 13:25:57 +0100855// uniqueApexVariations provides common implementation of the ApexModule.UniqueApexVariations
856// method.
857func (c *commonToSdkLibraryAndImport) uniqueApexVariations() bool {
858 // A java_sdk_library that is a shared library produces an XML file that makes the shared library
859 // usable from an AndroidManifest.xml's <uses-library> entry. That XML file contains the name of
860 // the APEX and so it needs a unique variation per APEX.
861 return c.sharedLibrary()
862}
863
Paul Duffina2ae7e02020-09-11 11:55:00 +0100864func (c *commonToSdkLibraryAndImport) generateCommonBuildActions(ctx android.ModuleContext) {
865 c.doctagPaths = android.PathsForModuleSrc(ctx, c.commonSdkLibraryProperties.Doctag_files)
866}
867
Paul Duffineedc5d52020-06-12 17:46:39 +0100868// Module name of the runtime implementation library
869func (c *commonToSdkLibraryAndImport) implLibraryModuleName() string {
Paul Duffin71b33cc2021-06-23 11:39:47 +0100870 return c.module.BaseModuleName() + ".impl"
Paul Duffineedc5d52020-06-12 17:46:39 +0100871}
872
873// Module name of the XML file for the lib
874func (c *commonToSdkLibraryAndImport) xmlPermissionsModuleName() string {
Paul Duffin71b33cc2021-06-23 11:39:47 +0100875 return c.module.BaseModuleName() + sdkXmlFileSuffix
Paul Duffineedc5d52020-06-12 17:46:39 +0100876}
877
Paul Duffinc3091c82020-05-08 14:16:20 +0100878// Name of the java_library module that compiles the stubs source.
879func (c *commonToSdkLibraryAndImport) stubsLibraryModuleName(apiScope *apiScope) string {
Paul Duffinb97b1572021-04-29 21:50:40 +0100880 baseName := c.module.BaseModuleName()
Paul Duffin21787622022-11-25 12:48:20 +0000881 return c.namingScheme.stubsLibraryModuleName(apiScope, baseName)
Paul Duffinc3091c82020-05-08 14:16:20 +0100882}
883
884// Name of the droidstubs module that generates the stubs source and may also
885// generate/check the API.
886func (c *commonToSdkLibraryAndImport) stubsSourceModuleName(apiScope *apiScope) string {
Paul Duffinb97b1572021-04-29 21:50:40 +0100887 baseName := c.module.BaseModuleName()
Paul Duffin21787622022-11-25 12:48:20 +0000888 return c.namingScheme.stubsSourceModuleName(apiScope, baseName)
Paul Duffinc3091c82020-05-08 14:16:20 +0100889}
890
Jihoon Kang1c92c3e2023-03-23 17:44:51 +0000891// Name of the java_api_library module that generates the from-text stubs source
892// and compiles to a jar file.
893func (c *commonToSdkLibraryAndImport) apiLibraryModuleName(apiScope *apiScope) string {
894 baseName := c.module.BaseModuleName()
895 return c.namingScheme.apiLibraryModuleName(apiScope, baseName)
896}
897
Jihoon Kang1147b312023-06-08 23:25:57 +0000898// Name of the java_library module that compiles the stubs
899// generated from source Java files.
900func (c *commonToSdkLibraryAndImport) sourceStubLibraryModuleName(apiScope *apiScope) string {
901 baseName := c.module.BaseModuleName()
902 return c.namingScheme.sourceStubLibraryModuleName(apiScope, baseName)
903}
904
Paul Duffin46dc45a2020-05-14 15:39:10 +0100905// The component names for different outputs of the java_sdk_library.
906//
907// They are similar to the names used for the child modules it creates
908const (
909 stubsSourceComponentName = "stubs.source"
910
911 apiTxtComponentName = "api.txt"
912
913 removedApiTxtComponentName = "removed-api.txt"
Anton Hanssond78eb762021-09-21 15:25:12 +0100914
915 annotationsComponentName = "annotations.zip"
Paul Duffin46dc45a2020-05-14 15:39:10 +0100916)
917
918// A regular expression to match tags that reference a specific stubs component.
919//
920// It will only match if given a valid scope and a valid component. It is verfy strict
921// to ensure it does not accidentally match a similar looking tag that should be processed
922// by the embedded Library.
923var tagSplitter = func() *regexp.Regexp {
924 // Given a list of literal string items returns a regular expression that will
925 // match any one of the items.
926 choice := func(items ...string) string {
927 return `\Q` + strings.Join(items, `\E|\Q`) + `\E`
928 }
929
930 // Regular expression to match one of the scopes.
931 scopesRegexp := choice(allScopeNames...)
932
933 // Regular expression to match one of the components.
Anton Hanssond78eb762021-09-21 15:25:12 +0100934 componentsRegexp := choice(stubsSourceComponentName, apiTxtComponentName, removedApiTxtComponentName, annotationsComponentName)
Paul Duffin46dc45a2020-05-14 15:39:10 +0100935
936 // Regular expression to match any combination of one scope and one component.
937 return regexp.MustCompile(fmt.Sprintf(`^\.(%s)\.(%s)$`, scopesRegexp, componentsRegexp))
938}()
939
940// For OutputFileProducer interface
941//
Anton Hanssond78eb762021-09-21 15:25:12 +0100942// .<scope>.<component name>, for all ComponentNames (for example: .public.removed-api.txt)
Paul Duffin46dc45a2020-05-14 15:39:10 +0100943func (c *commonToSdkLibraryAndImport) commonOutputFiles(tag string) (android.Paths, error) {
944 if groups := tagSplitter.FindStringSubmatch(tag); groups != nil {
945 scopeName := groups[1]
946 component := groups[2]
947
948 if scope, ok := scopeByName[scopeName]; ok {
949 paths := c.findScopePaths(scope)
950 if paths == nil {
Paul Duffin71b33cc2021-06-23 11:39:47 +0100951 return nil, fmt.Errorf("%q does not provide api scope %s", c.module.BaseModuleName(), scopeName)
Paul Duffin46dc45a2020-05-14 15:39:10 +0100952 }
953
954 switch component {
955 case stubsSourceComponentName:
956 if paths.stubsSrcJar.Valid() {
957 return android.Paths{paths.stubsSrcJar.Path()}, nil
958 }
959
960 case apiTxtComponentName:
961 if paths.currentApiFilePath.Valid() {
962 return android.Paths{paths.currentApiFilePath.Path()}, nil
963 }
964
965 case removedApiTxtComponentName:
966 if paths.removedApiFilePath.Valid() {
967 return android.Paths{paths.removedApiFilePath.Path()}, nil
968 }
Anton Hanssond78eb762021-09-21 15:25:12 +0100969
970 case annotationsComponentName:
971 if paths.annotationsZip.Valid() {
972 return android.Paths{paths.annotationsZip.Path()}, nil
973 }
Paul Duffin46dc45a2020-05-14 15:39:10 +0100974 }
975
976 return nil, fmt.Errorf("%s not available for api scope %s", component, scopeName)
977 } else {
978 return nil, fmt.Errorf("unknown scope %s in %s", scope, tag)
979 }
980
981 } else {
Paul Duffina2ae7e02020-09-11 11:55:00 +0100982 switch tag {
983 case ".doctags":
984 if c.doctagPaths != nil {
985 return c.doctagPaths, nil
986 } else {
Paul Duffin71b33cc2021-06-23 11:39:47 +0100987 return nil, fmt.Errorf("no doctag_files specified on %s", c.module.BaseModuleName())
Paul Duffina2ae7e02020-09-11 11:55:00 +0100988 }
989 }
Paul Duffin46dc45a2020-05-14 15:39:10 +0100990 return nil, nil
991 }
992}
993
Paul Duffin803a9562020-05-20 11:52:25 +0100994func (c *commonToSdkLibraryAndImport) getScopePathsCreateIfNeeded(scope *apiScope) *scopePaths {
Paul Duffin56d44902020-01-31 13:36:25 +0000995 if c.scopePaths == nil {
996 c.scopePaths = make(map[*apiScope]*scopePaths)
997 }
998 paths := c.scopePaths[scope]
999 if paths == nil {
1000 paths = &scopePaths{}
1001 c.scopePaths[scope] = paths
1002 }
1003
1004 return paths
1005}
1006
Paul Duffin803a9562020-05-20 11:52:25 +01001007func (c *commonToSdkLibraryAndImport) findScopePaths(scope *apiScope) *scopePaths {
1008 if c.scopePaths == nil {
1009 return nil
1010 }
1011
1012 return c.scopePaths[scope]
1013}
1014
1015// If this does not support the requested api scope then find the closest available
1016// scope it does support. Returns nil if no such scope is available.
1017func (c *commonToSdkLibraryAndImport) findClosestScopePath(scope *apiScope) *scopePaths {
Paul Duffind0b9fca2022-09-30 18:11:41 +01001018 for s := scope; s != nil; s = s.canAccess {
Paul Duffin803a9562020-05-20 11:52:25 +01001019 if paths := c.findScopePaths(s); paths != nil {
1020 return paths
1021 }
1022 }
1023
1024 // This should never happen outside tests as public should be the base scope for every
1025 // scope and is enabled by default.
1026 return nil
1027}
1028
Jiyong Parkf1691d22021-03-29 20:11:58 +09001029func (c *commonToSdkLibraryAndImport) selectHeaderJarsForSdkVersion(ctx android.BaseModuleContext, sdkVersion android.SdkSpec) android.Paths {
Paul Duffinb05d4292020-05-20 12:19:10 +01001030
1031 // If a specific numeric version has been requested then use prebuilt versions of the sdk.
Jiyong Park54105c42021-03-31 18:17:53 +09001032 if !sdkVersion.ApiLevel.IsPreview() {
Paul Duffin71b33cc2021-06-23 11:39:47 +01001033 return PrebuiltJars(ctx, c.module.BaseModuleName(), sdkVersion)
Paul Duffinb05d4292020-05-20 12:19:10 +01001034 }
1035
Paul Duffin1267d872021-04-16 17:21:36 +01001036 paths := c.selectScopePaths(ctx, sdkVersion.Kind)
1037 if paths == nil {
1038 return nil
1039 }
1040
1041 return paths.stubsHeaderPath
1042}
1043
1044// selectScopePaths returns the *scopePaths appropriate for the specific kind.
1045//
1046// If the module does not support the specific kind then it will return the *scopePaths for the
1047// closest kind which is a subset of the requested kind. e.g. if requesting android.SdkModule then
1048// it will return *scopePaths for android.SdkSystem if available or android.SdkPublic of not.
1049func (c *commonToSdkLibraryAndImport) selectScopePaths(ctx android.BaseModuleContext, kind android.SdkKind) *scopePaths {
Paul Duffin32cf58a2021-05-18 16:32:50 +01001050 apiScope := sdkKindToApiScope(kind)
Paul Duffinb05d4292020-05-20 12:19:10 +01001051
Paul Duffin803a9562020-05-20 11:52:25 +01001052 paths := c.findClosestScopePath(apiScope)
1053 if paths == nil {
1054 var scopes []string
1055 for _, s := range allApiScopes {
1056 if c.findScopePaths(s) != nil {
1057 scopes = append(scopes, s.name)
1058 }
1059 }
Paul Duffin71b33cc2021-06-23 11:39:47 +01001060 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 +01001061 return nil
1062 }
1063
Paul Duffin1267d872021-04-16 17:21:36 +01001064 return paths
1065}
1066
Paul Duffin32cf58a2021-05-18 16:32:50 +01001067// sdkKindToApiScope maps from android.SdkKind to apiScope.
1068func sdkKindToApiScope(kind android.SdkKind) *apiScope {
1069 var apiScope *apiScope
1070 switch kind {
1071 case android.SdkSystem:
1072 apiScope = apiScopeSystem
1073 case android.SdkModule:
1074 apiScope = apiScopeModuleLib
1075 case android.SdkTest:
1076 apiScope = apiScopeTest
1077 case android.SdkSystemServer:
1078 apiScope = apiScopeSystemServer
1079 default:
1080 apiScope = apiScopePublic
1081 }
1082 return apiScope
1083}
1084
Paul Duffin1267d872021-04-16 17:21:36 +01001085// to satisfy SdkLibraryDependency interface
Martin Stjernholm8be1e6d2021-09-15 03:34:04 +01001086func (c *commonToSdkLibraryAndImport) SdkApiStubDexJar(ctx android.BaseModuleContext, kind android.SdkKind) OptionalDexJarPath {
Paul Duffin1267d872021-04-16 17:21:36 +01001087 paths := c.selectScopePaths(ctx, kind)
1088 if paths == nil {
Martin Stjernholm8be1e6d2021-09-15 03:34:04 +01001089 return makeUnsetDexJarPath()
Paul Duffin1267d872021-04-16 17:21:36 +01001090 }
1091
1092 return paths.stubsDexJarPath
Paul Duffinb05d4292020-05-20 12:19:10 +01001093}
1094
Paul Duffin32cf58a2021-05-18 16:32:50 +01001095// to satisfy SdkLibraryDependency interface
1096func (c *commonToSdkLibraryAndImport) SdkRemovedTxtFile(ctx android.BaseModuleContext, kind android.SdkKind) android.OptionalPath {
1097 apiScope := sdkKindToApiScope(kind)
1098 paths := c.findScopePaths(apiScope)
1099 if paths == nil {
1100 return android.OptionalPath{}
1101 }
1102
1103 return paths.removedApiFilePath
1104}
1105
Paul Duffin859fe962020-05-15 10:20:31 +01001106func (c *commonToSdkLibraryAndImport) sdkComponentPropertiesForChildLibrary() interface{} {
1107 componentProps := &struct {
Paul Duffin3f0290e2021-06-30 18:25:36 +01001108 SdkLibraryName *string
Paul Duffin859fe962020-05-15 10:20:31 +01001109 SdkLibraryToImplicitlyTrack *string
Paul Duffindfa131e2020-05-15 20:37:11 +01001110 }{}
1111
Paul Duffin3f0290e2021-06-30 18:25:36 +01001112 namePtr := proptools.StringPtr(c.module.BaseModuleName())
1113 componentProps.SdkLibraryName = namePtr
1114
Paul Duffindfa131e2020-05-15 20:37:11 +01001115 if c.sharedLibrary() {
Paul Duffin859fe962020-05-15 10:20:31 +01001116 // Mark the stubs library as being components of this java_sdk_library so that
1117 // any app that includes code which depends (directly or indirectly) on the stubs
1118 // library will have the appropriate <uses-library> invocation inserted into its
1119 // manifest if necessary.
Paul Duffin3f0290e2021-06-30 18:25:36 +01001120 componentProps.SdkLibraryToImplicitlyTrack = namePtr
Paul Duffin859fe962020-05-15 10:20:31 +01001121 }
1122
1123 return componentProps
1124}
1125
Paul Duffindfa131e2020-05-15 20:37:11 +01001126func (c *commonToSdkLibraryAndImport) sharedLibrary() bool {
1127 return proptools.BoolDefault(c.commonSdkLibraryProperties.Shared_library, true)
1128}
1129
Paul Duffinf4600f62021-05-13 22:34:45 +01001130// Check if the stub libraries should be compiled for dex
1131func (c *commonToSdkLibraryAndImport) stubLibrariesCompiledForDex() bool {
1132 // Always compile the dex file files for the stub libraries if they will be used on the
1133 // bootclasspath.
1134 return !c.sharedLibrary()
1135}
1136
Paul Duffin859fe962020-05-15 10:20:31 +01001137// Properties related to the use of a module as an component of a java_sdk_library.
1138type SdkLibraryComponentProperties struct {
Paul Duffin3f0290e2021-06-30 18:25:36 +01001139 // The name of the java_sdk_library/_import module.
1140 SdkLibraryName *string `blueprint:"mutated"`
Paul Duffin859fe962020-05-15 10:20:31 +01001141
1142 // The name of the java_sdk_library/_import to add to a <uses-library> entry
1143 // in the AndroidManifest.xml of any Android app that includes code that references
1144 // this module. If not set then no java_sdk_library/_import is tracked.
1145 SdkLibraryToImplicitlyTrack *string `blueprint:"mutated"`
1146}
1147
1148// Structure to be embedded in a module struct that needs to support the
1149// SdkLibraryComponentDependency interface.
1150type EmbeddableSdkLibraryComponent struct {
1151 sdkLibraryComponentProperties SdkLibraryComponentProperties
1152}
1153
Paul Duffin71b33cc2021-06-23 11:39:47 +01001154func (e *EmbeddableSdkLibraryComponent) initSdkLibraryComponent(module android.Module) {
1155 module.AddProperties(&e.sdkLibraryComponentProperties)
Paul Duffin859fe962020-05-15 10:20:31 +01001156}
1157
1158// to satisfy SdkLibraryComponentDependency
Paul Duffin3f0290e2021-06-30 18:25:36 +01001159func (e *EmbeddableSdkLibraryComponent) SdkLibraryName() *string {
1160 return e.sdkLibraryComponentProperties.SdkLibraryName
1161}
1162
1163// to satisfy SdkLibraryComponentDependency
Ulya Trafimovich39b437b2020-09-23 16:42:35 +01001164func (e *EmbeddableSdkLibraryComponent) OptionalSdkLibraryImplementation() *string {
Ulya Trafimovich78645fb2021-07-16 15:29:25 +01001165 // For shared libraries, this is the same as the SDK library name. If a Java library or app
1166 // depends on a component library (e.g. a stub library) it still needs to know the name of the
1167 // run-time library and the corresponding module that provides the implementation. This name is
1168 // passed to manifest_fixer (to be added to AndroidManifest.xml) and added to CLC (to be used
1169 // in dexpreopt).
1170 //
1171 // For non-shared SDK (component or not) libraries this returns `nil`, as they are not
1172 // <uses-library> and should not be added to the manifest or to CLC.
Ulya Trafimovich39b437b2020-09-23 16:42:35 +01001173 return e.sdkLibraryComponentProperties.SdkLibraryToImplicitlyTrack
1174}
1175
Paul Duffin859fe962020-05-15 10:20:31 +01001176// Implemented by modules that are (or possibly could be) a component of a java_sdk_library
1177// (including the java_sdk_library) itself.
1178type SdkLibraryComponentDependency interface {
Ulya Trafimovich31e444e2020-08-14 17:32:16 +01001179 UsesLibraryDependency
1180
Paul Duffin3f0290e2021-06-30 18:25:36 +01001181 // SdkLibraryName returns the name of the java_sdk_library/_import module.
1182 SdkLibraryName() *string
1183
Ulya Trafimovich39b437b2020-09-23 16:42:35 +01001184 // The name of the implementation library for the optional SDK library or nil, if there isn't one.
1185 OptionalSdkLibraryImplementation() *string
Paul Duffin859fe962020-05-15 10:20:31 +01001186}
1187
1188// Make sure that all the module types that are components of java_sdk_library/_import
1189// and which can be referenced (directly or indirectly) from an android app implement
1190// the SdkLibraryComponentDependency interface.
1191var _ SdkLibraryComponentDependency = (*Library)(nil)
1192var _ SdkLibraryComponentDependency = (*Import)(nil)
1193var _ SdkLibraryComponentDependency = (*SdkLibrary)(nil)
Paul Duffineedc5d52020-06-12 17:46:39 +01001194var _ SdkLibraryComponentDependency = (*SdkLibraryImport)(nil)
Paul Duffin859fe962020-05-15 10:20:31 +01001195
Paul Duffin32cf58a2021-05-18 16:32:50 +01001196// Provides access to sdk_version related files, e.g. header and implementation jars.
Paul Duffin859fe962020-05-15 10:20:31 +01001197type SdkLibraryDependency interface {
1198 SdkLibraryComponentDependency
1199
1200 // Get the header jars appropriate for the supplied sdk_version.
1201 //
1202 // These are turbine generated jars so they only change if the externals of the
1203 // class changes but it does not contain and implementation or JavaDoc.
Jiyong Parkf1691d22021-03-29 20:11:58 +09001204 SdkHeaderJars(ctx android.BaseModuleContext, sdkVersion android.SdkSpec) android.Paths
Paul Duffin859fe962020-05-15 10:20:31 +01001205
1206 // Get the implementation jars appropriate for the supplied sdk version.
1207 //
1208 // These are either the implementation jar for the whole sdk library or the implementation
1209 // jars for the stubs. The latter should only be needed when generating JavaDoc as otherwise
1210 // they are identical to the corresponding header jars.
Jiyong Parkf1691d22021-03-29 20:11:58 +09001211 SdkImplementationJars(ctx android.BaseModuleContext, sdkVersion android.SdkSpec) android.Paths
Paul Duffin1267d872021-04-16 17:21:36 +01001212
1213 // SdkApiStubDexJar returns the dex jar for the stubs. It is needed by the hiddenapi processing
1214 // tool which processes dex files.
Martin Stjernholm8be1e6d2021-09-15 03:34:04 +01001215 SdkApiStubDexJar(ctx android.BaseModuleContext, kind android.SdkKind) OptionalDexJarPath
Paul Duffinf4600f62021-05-13 22:34:45 +01001216
Paul Duffin32cf58a2021-05-18 16:32:50 +01001217 // SdkRemovedTxtFile returns the optional path to the removed.txt file for the specified sdk kind.
1218 SdkRemovedTxtFile(ctx android.BaseModuleContext, kind android.SdkKind) android.OptionalPath
1219
Paul Duffinf4600f62021-05-13 22:34:45 +01001220 // sharedLibrary returns true if this can be used as a shared library.
1221 sharedLibrary() bool
Paul Duffin859fe962020-05-15 10:20:31 +01001222}
1223
Inseob Kimc0907f12019-02-08 21:00:45 +09001224type SdkLibrary struct {
Sundong Ahn054b19a2018-10-19 13:46:09 +09001225 Library
Jiyong Parkc678ad32018-04-10 13:07:10 +09001226
Zi Wangb2179e32023-01-31 15:53:30 -08001227 android.BazelModuleBase
1228
Sundong Ahn054b19a2018-10-19 13:46:09 +09001229 sdkLibraryProperties sdkLibraryProperties
Jiyong Parkc678ad32018-04-10 13:07:10 +09001230
Paul Duffin3375e352020-04-28 10:44:03 +01001231 // Map from api scope to the scope specific property structure.
1232 scopeToProperties map[*apiScope]*ApiScopeProperties
1233
Paul Duffin56d44902020-01-31 13:36:25 +00001234 commonToSdkLibraryAndImport
Jiyong Parkc678ad32018-04-10 13:07:10 +09001235}
1236
Inseob Kimc0907f12019-02-08 21:00:45 +09001237var _ SdkLibraryDependency = (*SdkLibrary)(nil)
Colin Cross897d2ed2019-02-11 14:03:51 -08001238
Paul Duffin3375e352020-04-28 10:44:03 +01001239func (module *SdkLibrary) generateTestAndSystemScopesByDefault() bool {
1240 return module.sdkLibraryProperties.Generate_system_and_test_apis
1241}
1242
1243func (module *SdkLibrary) getGeneratedApiScopes(ctx android.EarlyModuleContext) apiScopes {
1244 // Check to see if any scopes have been explicitly enabled. If any have then all
1245 // must be.
1246 anyScopesExplicitlyEnabled := false
1247 for _, scope := range allApiScopes {
1248 scopeProperties := module.scopeToProperties[scope]
1249 if scopeProperties.Enabled != nil {
1250 anyScopesExplicitlyEnabled = true
1251 break
1252 }
Paul Duffind1b3a922020-01-22 11:57:20 +00001253 }
Paul Duffin3375e352020-04-28 10:44:03 +01001254
1255 var generatedScopes apiScopes
1256 enabledScopes := make(map[*apiScope]struct{})
1257 for _, scope := range allApiScopes {
1258 scopeProperties := module.scopeToProperties[scope]
1259 // If any scopes are explicitly enabled then ignore the legacy enabled status.
1260 // This is to ensure that any new usages of this module type do not rely on legacy
1261 // behaviour.
1262 defaultEnabledStatus := false
1263 if anyScopesExplicitlyEnabled {
1264 defaultEnabledStatus = scope.defaultEnabledStatus
1265 } else {
1266 defaultEnabledStatus = scope.legacyEnabledStatus(module)
1267 }
1268 enabled := proptools.BoolDefault(scopeProperties.Enabled, defaultEnabledStatus)
1269 if enabled {
1270 enabledScopes[scope] = struct{}{}
1271 generatedScopes = append(generatedScopes, scope)
1272 }
1273 }
1274
1275 // Now check to make sure that any scope that is extended by an enabled scope is also
1276 // enabled.
1277 for _, scope := range allApiScopes {
1278 if _, ok := enabledScopes[scope]; ok {
1279 extends := scope.extends
1280 if extends != nil {
1281 if _, ok := enabledScopes[extends]; !ok {
1282 ctx.ModuleErrorf("enabled api scope %q depends on disabled scope %q", scope, extends)
1283 }
1284 }
1285 }
1286 }
1287
1288 return generatedScopes
Paul Duffind1b3a922020-01-22 11:57:20 +00001289}
1290
satayev758968a2021-12-06 11:42:40 +00001291var _ android.ModuleWithMinSdkVersionCheck = (*SdkLibrary)(nil)
1292
satayev8f088b02021-12-06 11:40:46 +00001293func (module *SdkLibrary) CheckMinSdkVersion(ctx android.ModuleContext) {
Spandan Das8c9ae7e2023-03-03 21:20:36 +00001294 android.CheckMinSdkVersion(ctx, module.MinSdkVersion(ctx), func(c android.ModuleContext, do android.PayloadDepsCallback) {
satayev8f088b02021-12-06 11:40:46 +00001295 ctx.WalkDeps(func(child android.Module, parent android.Module) bool {
1296 isExternal := !module.depIsInSameApex(ctx, child)
1297 if am, ok := child.(android.ApexModule); ok {
1298 if !do(ctx, parent, am, isExternal) {
1299 return false
1300 }
1301 }
1302 return !isExternal
1303 })
1304 })
1305}
1306
Paul Duffineedc5d52020-06-12 17:46:39 +01001307type sdkLibraryComponentTag struct {
1308 blueprint.BaseDependencyTag
1309 name string
1310}
1311
1312// Mark this tag so dependencies that use it are excluded from visibility enforcement.
1313func (t sdkLibraryComponentTag) ExcludeFromVisibilityEnforcement() {}
1314
1315var xmlPermissionsFileTag = sdkLibraryComponentTag{name: "xml-permissions-file"}
Paul Duffine74ac732020-02-06 13:51:46 +00001316
Jiyong Parke3833882020-02-17 17:28:10 +09001317func IsXmlPermissionsFileDepTag(depTag blueprint.DependencyTag) bool {
Paul Duffineedc5d52020-06-12 17:46:39 +01001318 if dt, ok := depTag.(sdkLibraryComponentTag); ok {
Jiyong Parke3833882020-02-17 17:28:10 +09001319 return dt == xmlPermissionsFileTag
1320 }
1321 return false
1322}
1323
Paul Duffineedc5d52020-06-12 17:46:39 +01001324var implLibraryTag = sdkLibraryComponentTag{name: "impl-library"}
Paul Duffin5df79302020-05-16 15:52:12 +01001325
Paul Duffin44f1d842020-06-26 20:17:02 +01001326// Add the dependencies on the child modules in the component deps mutator.
1327func (module *SdkLibrary) ComponentDepsMutator(ctx android.BottomUpMutatorContext) {
Paul Duffin3375e352020-04-28 10:44:03 +01001328 for _, apiScope := range module.getGeneratedApiScopes(ctx) {
Paul Duffind1b3a922020-01-22 11:57:20 +00001329 // Add dependencies to the stubs library
Spandan Das877f39d2023-03-29 16:19:51 +00001330 stubModuleName := module.stubsLibraryModuleName(apiScope)
Jihoon Kang1147b312023-06-08 23:25:57 +00001331
Spandan Das877f39d2023-03-29 16:19:51 +00001332 ctx.AddVariationDependencies(nil, apiScope.stubsTag, stubModuleName)
Paul Duffind1b3a922020-01-22 11:57:20 +00001333
Paul Duffin15f34ef2020-07-20 18:04:44 +01001334 // Add a dependency on the stubs source in order to access both stubs source and api information.
1335 ctx.AddVariationDependencies(nil, apiScope.stubsSourceAndApiTag, module.stubsSourceModuleName(apiScope))
Paul Duffin958806b2022-05-16 13:10:47 +00001336
1337 if module.compareAgainstLatestApi(apiScope) {
1338 // Add dependencies on the latest finalized version of the API .txt file.
1339 latestApiModuleName := module.latestApiModuleName(apiScope)
1340 ctx.AddDependency(module, apiScope.latestApiModuleTag, latestApiModuleName)
1341
1342 // Add dependencies on the latest finalized version of the remove API .txt file.
1343 latestRemovedApiModuleName := module.latestRemovedApiModuleName(apiScope)
1344 ctx.AddDependency(module, apiScope.latestRemovedApiModuleTag, latestRemovedApiModuleName)
1345 }
Sundong Ahn054b19a2018-10-19 13:46:09 +09001346 }
1347
Paul Duffindfa131e2020-05-15 20:37:11 +01001348 if module.requiresRuntimeImplementationLibrary() {
Paul Duffin5df79302020-05-16 15:52:12 +01001349 // Add dependency to the rule for generating the implementation library.
1350 ctx.AddDependency(module, implLibraryTag, module.implLibraryModuleName())
1351
Paul Duffindfa131e2020-05-15 20:37:11 +01001352 if module.sharedLibrary() {
1353 // Add dependency to the rule for generating the xml permissions file
Paul Duffineedc5d52020-06-12 17:46:39 +01001354 ctx.AddDependency(module, xmlPermissionsFileTag, module.xmlPermissionsModuleName())
Paul Duffindfa131e2020-05-15 20:37:11 +01001355 }
Paul Duffin44f1d842020-06-26 20:17:02 +01001356 }
1357}
Paul Duffine74ac732020-02-06 13:51:46 +00001358
Paul Duffin44f1d842020-06-26 20:17:02 +01001359// Add other dependencies as normal.
1360func (module *SdkLibrary) DepsMutator(ctx android.BottomUpMutatorContext) {
Anton Hanssone77fccc2021-01-20 16:52:41 +00001361 var missingApiModules []string
1362 for _, apiScope := range module.getGeneratedApiScopes(ctx) {
1363 if apiScope.unstable {
1364 continue
1365 }
Paul Duffin958806b2022-05-16 13:10:47 +00001366 if m := module.latestApiModuleName(apiScope); !ctx.OtherModuleExists(m) {
Anton Hanssone77fccc2021-01-20 16:52:41 +00001367 missingApiModules = append(missingApiModules, m)
1368 }
Paul Duffin958806b2022-05-16 13:10:47 +00001369 if m := module.latestRemovedApiModuleName(apiScope); !ctx.OtherModuleExists(m) {
Anton Hanssone77fccc2021-01-20 16:52:41 +00001370 missingApiModules = append(missingApiModules, m)
1371 }
Paul Duffin958806b2022-05-16 13:10:47 +00001372 if m := module.latestIncompatibilitiesModuleName(apiScope); !ctx.OtherModuleExists(m) {
Jaewoong Jung1a97ee02021-03-09 13:25:02 -08001373 missingApiModules = append(missingApiModules, m)
1374 }
Anton Hanssone77fccc2021-01-20 16:52:41 +00001375 }
1376 if len(missingApiModules) != 0 && !module.sdkLibraryProperties.Unsafe_ignore_missing_latest_api {
1377 m := module.Name() + " is missing tracking files for previously released library versions.\n"
1378 m += "You need to do one of the following:\n"
1379 m += "- Add `unsafe_ignore_missing_latest_api: true` to your blueprint (to disable compat tracking)\n"
1380 m += "- Add a set of prebuilt txt files representing the last released version of this library for compat checking.\n"
1381 m += " (the current set of API files can be used as a seed for this compatibility tracking\n"
1382 m += "\n"
1383 m += "The following filegroup modules are missing:\n "
1384 m += strings.Join(missingApiModules, "\n ") + "\n"
1385 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."
1386 ctx.ModuleErrorf(m)
1387 }
Paul Duffin44f1d842020-06-26 20:17:02 +01001388 if module.requiresRuntimeImplementationLibrary() {
Paul Duffindfa131e2020-05-15 20:37:11 +01001389 // Only add the deps for the library if it is actually going to be built.
1390 module.Library.deps(ctx)
1391 }
Jiyong Parkc678ad32018-04-10 13:07:10 +09001392}
1393
Paul Duffin46dc45a2020-05-14 15:39:10 +01001394func (module *SdkLibrary) OutputFiles(tag string) (android.Paths, error) {
1395 paths, err := module.commonOutputFiles(tag)
Colin Cross4acaea92021-12-10 23:05:02 +00001396 if paths != nil || err != nil {
Paul Duffin46dc45a2020-05-14 15:39:10 +01001397 return paths, err
1398 }
Colin Cross4acaea92021-12-10 23:05:02 +00001399 if module.requiresRuntimeImplementationLibrary() {
1400 return module.Library.OutputFiles(tag)
1401 }
1402 if tag == "" {
1403 return nil, nil
1404 }
1405 return nil, fmt.Errorf("unsupported module reference tag %q", tag)
Paul Duffin46dc45a2020-05-14 15:39:10 +01001406}
1407
Inseob Kimc0907f12019-02-08 21:00:45 +09001408func (module *SdkLibrary) GenerateAndroidBuildActions(ctx android.ModuleContext) {
satayev8f088b02021-12-06 11:40:46 +00001409 if proptools.String(module.deviceProperties.Min_sdk_version) != "" {
1410 module.CheckMinSdkVersion(ctx)
1411 }
1412
Paul Duffina2ae7e02020-09-11 11:55:00 +01001413 module.generateCommonBuildActions(ctx)
1414
Paul Duffindfa131e2020-05-15 20:37:11 +01001415 // Only build an implementation library if required.
1416 if module.requiresRuntimeImplementationLibrary() {
Paul Duffin43db9be2019-12-30 17:35:49 +00001417 module.Library.GenerateAndroidBuildActions(ctx)
1418 }
Sundong Ahn054b19a2018-10-19 13:46:09 +09001419
Paul Duffinb97b1572021-04-29 21:50:40 +01001420 // Collate the components exported by this module. All scope specific modules are exported but
1421 // the impl and xml component modules are not.
1422 exportedComponents := map[string]struct{}{}
1423
Sundong Ahn57368eb2018-07-06 11:20:23 +09001424 // Record the paths to the header jars of the library (stubs and impl).
Paul Duffind1b3a922020-01-22 11:57:20 +00001425 // When this java_sdk_library is depended upon from others via "libs" property,
Jiyong Parkc678ad32018-04-10 13:07:10 +09001426 // the recorded paths will be returned depending on the link type of the caller.
1427 ctx.VisitDirectDeps(func(to android.Module) {
Jiyong Parkc678ad32018-04-10 13:07:10 +09001428 tag := ctx.OtherModuleDependencyTag(to)
1429
Paul Duffinc8782502020-04-29 20:45:27 +01001430 // Extract information from any of the scope specific dependencies.
1431 if scopeTag, ok := tag.(scopeDependencyTag); ok {
1432 apiScope := scopeTag.apiScope
Paul Duffin803a9562020-05-20 11:52:25 +01001433 scopePaths := module.getScopePathsCreateIfNeeded(apiScope)
Paul Duffinc8782502020-04-29 20:45:27 +01001434
1435 // Extract information from the dependency. The exact information extracted
1436 // is determined by the nature of the dependency which is determined by the tag.
1437 scopeTag.extractDepInfo(ctx, to, scopePaths)
Paul Duffinb97b1572021-04-29 21:50:40 +01001438
1439 exportedComponents[ctx.OtherModuleName(to)] = struct{}{}
Sundong Ahn20e998b2018-07-24 11:19:26 +09001440 }
Jiyong Parkc678ad32018-04-10 13:07:10 +09001441 })
Paul Duffinb97b1572021-04-29 21:50:40 +01001442
1443 // Make the set of components exported by this module available for use elsewhere.
Cole Faust18994c72023-02-28 16:02:16 -08001444 exportedComponentInfo := android.ExportedComponentsInfo{Components: android.SortedKeys(exportedComponents)}
Paul Duffinb97b1572021-04-29 21:50:40 +01001445 ctx.SetProvider(android.ExportedComponentsInfoProvider, exportedComponentInfo)
Paul Duffin958806b2022-05-16 13:10:47 +00001446
1447 // Provide additional information for inclusion in an sdk's generated .info file.
1448 additionalSdkInfo := map[string]interface{}{}
1449 additionalSdkInfo["dist_stem"] = module.distStem()
Paul Duffine8409952022-09-22 16:24:46 +01001450 baseModuleName := module.distStem()
Paul Duffin958806b2022-05-16 13:10:47 +00001451 scopes := map[string]interface{}{}
1452 additionalSdkInfo["scopes"] = scopes
1453 for scope, scopePaths := range module.scopePaths {
1454 scopeInfo := map[string]interface{}{}
1455 scopes[scope.name] = scopeInfo
1456 scopeInfo["current_api"] = scope.snapshotRelativeCurrentApiTxtPath(baseModuleName)
1457 scopeInfo["removed_api"] = scope.snapshotRelativeRemovedApiTxtPath(baseModuleName)
1458 if p := scopePaths.latestApiPath; p.Valid() {
1459 scopeInfo["latest_api"] = p.Path().String()
1460 }
1461 if p := scopePaths.latestRemovedApiPath; p.Valid() {
1462 scopeInfo["latest_removed_api"] = p.Path().String()
1463 }
1464 }
1465 ctx.SetProvider(android.AdditionalSdkInfoProvider, android.AdditionalSdkInfo{additionalSdkInfo})
Jiyong Parkc678ad32018-04-10 13:07:10 +09001466}
1467
Jiyong Park0b0e1b92019-12-03 13:24:29 +09001468func (module *SdkLibrary) AndroidMkEntries() []android.AndroidMkEntries {
Paul Duffindfa131e2020-05-15 20:37:11 +01001469 if !module.requiresRuntimeImplementationLibrary() {
Paul Duffin43db9be2019-12-30 17:35:49 +00001470 return nil
1471 }
Jiyong Park0b0e1b92019-12-03 13:24:29 +09001472 entriesList := module.Library.AndroidMkEntries()
Yo Chiang07d75072020-06-05 17:43:19 +08001473 if module.sharedLibrary() {
1474 entries := &entriesList[0]
1475 entries.Required = append(entries.Required, module.xmlPermissionsModuleName())
1476 }
Jiyong Park0b0e1b92019-12-03 13:24:29 +09001477 return entriesList
Jiyong Park82484c02018-04-23 21:41:26 +09001478}
1479
Anton Hansson5fd5d242020-03-27 19:43:19 +00001480// The dist path of the stub artifacts
1481func (module *SdkLibrary) apiDistPath(apiScope *apiScope) string {
Colin Crossf0eace92021-06-02 13:02:23 -07001482 return path.Join("apistubs", module.distGroup(), apiScope.name)
Anton Hansson5fd5d242020-03-27 19:43:19 +00001483}
1484
Paul Duffin12ceb462019-12-24 20:31:31 +00001485// Get the sdk version for use when compiling the stubs library.
Paul Duffin780c5f42020-05-12 15:52:55 +01001486func (module *SdkLibrary) sdkVersionForStubsLibrary(mctx android.EarlyModuleContext, apiScope *apiScope) string {
Paul Duffin87a05a32020-05-12 11:50:28 +01001487 scopeProperties := module.scopeToProperties[apiScope]
1488 if scopeProperties.Sdk_version != nil {
1489 return proptools.String(scopeProperties.Sdk_version)
1490 }
1491
Jiyong Parkf1691d22021-03-29 20:11:58 +09001492 sdkDep := decodeSdkDep(mctx, android.SdkContext(&module.Library))
Paul Duffin12ceb462019-12-24 20:31:31 +00001493 if sdkDep.hasStandardLibs() {
1494 // If building against a standard sdk then use the sdk version appropriate for the scope.
Paul Duffind1b3a922020-01-22 11:57:20 +00001495 return apiScope.sdkVersion
Paul Duffin12ceb462019-12-24 20:31:31 +00001496 } else {
1497 // Otherwise, use no system module.
1498 return "none"
1499 }
1500}
1501
Paul Duffin31310252020-11-20 21:26:20 +00001502func (module *SdkLibrary) distStem() string {
1503 return proptools.StringDefault(module.sdkLibraryProperties.Dist_stem, module.BaseModuleName())
1504}
1505
Colin Cross986b69a2021-06-01 13:13:40 -07001506// distGroup returns the subdirectory of the dist path of the stub artifacts.
1507func (module *SdkLibrary) distGroup() string {
Colin Cross59b92bf2021-06-01 14:07:56 -07001508 return proptools.StringDefault(module.sdkLibraryProperties.Dist_group, "unknown")
Colin Cross986b69a2021-06-01 13:13:40 -07001509}
1510
Paul Duffin958806b2022-05-16 13:10:47 +00001511func latestPrebuiltApiModuleName(name string, apiScope *apiScope) string {
1512 return PrebuiltApiModuleName(name, apiScope.name, "latest")
1513}
1514
Paul Duffind1b3a922020-01-22 11:57:20 +00001515func (module *SdkLibrary) latestApiFilegroupName(apiScope *apiScope) string {
Paul Duffin958806b2022-05-16 13:10:47 +00001516 return ":" + module.latestApiModuleName(apiScope)
1517}
1518
1519func (module *SdkLibrary) latestApiModuleName(apiScope *apiScope) string {
1520 return latestPrebuiltApiModuleName(module.distStem(), apiScope)
Jiyong Park58c518b2018-05-12 22:29:12 +09001521}
Jiyong Parkc678ad32018-04-10 13:07:10 +09001522
Paul Duffind1b3a922020-01-22 11:57:20 +00001523func (module *SdkLibrary) latestRemovedApiFilegroupName(apiScope *apiScope) string {
Paul Duffin958806b2022-05-16 13:10:47 +00001524 return ":" + module.latestRemovedApiModuleName(apiScope)
1525}
1526
1527func (module *SdkLibrary) latestRemovedApiModuleName(apiScope *apiScope) string {
1528 return latestPrebuiltApiModuleName(module.distStem()+"-removed", apiScope)
Jiyong Parkc678ad32018-04-10 13:07:10 +09001529}
1530
Jaewoong Jung1a97ee02021-03-09 13:25:02 -08001531func (module *SdkLibrary) latestIncompatibilitiesFilegroupName(apiScope *apiScope) string {
Paul Duffin958806b2022-05-16 13:10:47 +00001532 return ":" + module.latestIncompatibilitiesModuleName(apiScope)
1533}
1534
1535func (module *SdkLibrary) latestIncompatibilitiesModuleName(apiScope *apiScope) string {
1536 return latestPrebuiltApiModuleName(module.distStem()+"-incompatibilities", apiScope)
Jaewoong Jung1a97ee02021-03-09 13:25:02 -08001537}
1538
Jihoon Kang1c92c3e2023-03-23 17:44:51 +00001539func (module *SdkLibrary) contributesToApiSurface(c android.Config) bool {
1540 _, exists := c.GetApiLibraries()[module.Name()]
1541 return exists
1542}
1543
Jihoon Kang0c705a42023-08-02 06:44:57 +00001544// The listed modules are the special java_sdk_libraries where apiScope.kind do not match the
1545// api surface that the module contribute to. For example, the public droidstubs and java_library
1546// do not contribute to the public api surface, but contributes to the core platform api surface.
1547// This method returns the full api surface stub lib that
1548// the generated java_api_library should depend on.
1549func (module *SdkLibrary) alternativeFullApiSurfaceStubLib() string {
1550 if val, ok := apiLibraryAdditionalProperties[module.Name()]; ok {
1551 return val.FullApiSurfaceStubLib
1552 }
1553 return ""
1554}
1555
1556// The listed modules' stubs contents do not match the corresponding txt files,
1557// but require additional api contributions to generate the full stubs.
1558// This method returns the name of the additional api contribution module
1559// for corresponding sdk_library modules.
1560func (module *SdkLibrary) apiLibraryAdditionalApiContribution() string {
1561 if val, ok := apiLibraryAdditionalProperties[module.Name()]; ok {
1562 return val.AdditionalApiContribution
1563 }
1564 return ""
1565}
1566
Anton Hansson944e77d2020-08-19 11:40:22 +01001567func childModuleVisibility(childVisibility []string) []string {
1568 if childVisibility == nil {
1569 // No child visibility set. The child will use the visibility of the sdk_library.
1570 return nil
1571 }
1572
1573 // Prepend an override to ignore the sdk_library's visibility, and rely on the child visibility.
1574 var visibility []string
1575 visibility = append(visibility, "//visibility:override")
1576 visibility = append(visibility, childVisibility...)
1577 return visibility
1578}
1579
Paul Duffin5df79302020-05-16 15:52:12 +01001580// Creates the implementation java library
1581func (module *SdkLibrary) createImplLibrary(mctx android.DefaultableHookContext) {
Anton Hansson944e77d2020-08-19 11:40:22 +01001582 visibility := childModuleVisibility(module.sdkLibraryProperties.Impl_library_visibility)
1583
Paul Duffin5df79302020-05-16 15:52:12 +01001584 props := struct {
Paul Duffin77590a82022-04-28 14:13:30 +00001585 Name *string
1586 Visibility []string
1587 Instrument bool
1588 Libs []string
1589 Static_libs []string
1590 Apex_available []string
Paul Duffin5df79302020-05-16 15:52:12 +01001591 }{
1592 Name: proptools.StringPtr(module.implLibraryModuleName()),
Anton Hansson944e77d2020-08-19 11:40:22 +01001593 Visibility: visibility,
Paul Duffin296cf332020-06-18 21:09:55 +01001594 // Set the instrument property to ensure it is instrumented when instrumentation is required.
1595 Instrument: true,
Anton Hansson7f66efa2020-10-08 14:47:23 +01001596 // Set the impl_only libs. Note that the module's "Libs" get appended as well, via the
1597 // addition of &module.properties below.
1598 Libs: module.sdkLibraryProperties.Impl_only_libs,
Paul Duffin77590a82022-04-28 14:13:30 +00001599 // Set the impl_only static libs. Note that the module's "static_libs" get appended as well, via the
1600 // addition of &module.properties below.
1601 Static_libs: module.sdkLibraryProperties.Impl_only_static_libs,
1602 // Pass the apex_available settings down so that the impl library can be statically
1603 // embedded within a library that is added to an APEX. Needed for updatable-media.
1604 Apex_available: module.ApexAvailable(),
Paul Duffin5df79302020-05-16 15:52:12 +01001605 }
1606
1607 properties := []interface{}{
1608 &module.properties,
1609 &module.protoProperties,
1610 &module.deviceProperties,
Liz Kammera7a64f32020-07-09 15:16:41 -07001611 &module.dexProperties,
Paul Duffin5df79302020-05-16 15:52:12 +01001612 &module.dexpreoptProperties,
Colin Cross014489c2020-06-02 20:09:13 -07001613 &module.linter.properties,
Paul Duffin5df79302020-05-16 15:52:12 +01001614 &props,
1615 module.sdkComponentPropertiesForChildLibrary(),
1616 }
1617 mctx.CreateModule(LibraryFactory, properties...)
1618}
1619
Jiyong Parkc678ad32018-04-10 13:07:10 +09001620// Creates a static java library that has API stubs
Paul Duffinf0229202020-04-29 16:47:28 +01001621func (module *SdkLibrary) createStubsLibrary(mctx android.DefaultableHookContext, apiScope *apiScope) {
Jiyong Parkc678ad32018-04-10 13:07:10 +09001622 props := struct {
Dan Willemsen9f435972020-05-28 15:28:00 -07001623 Name *string
1624 Visibility []string
1625 Srcs []string
1626 Installable *bool
1627 Sdk_version *string
1628 System_modules *string
1629 Patch_module *string
1630 Libs []string
Anton Hanssondae54cd2021-04-21 16:30:10 +01001631 Static_libs []string
Dan Willemsen9f435972020-05-28 15:28:00 -07001632 Compile_dex *bool
1633 Java_version *string
1634 Openjdk9 struct {
Sundong Ahn054b19a2018-10-19 13:46:09 +09001635 Srcs []string
1636 Javacflags []string
1637 }
Anton Hansson5fd5d242020-03-27 19:43:19 +00001638 Dist struct {
1639 Targets []string
1640 Dest *string
1641 Dir *string
1642 Tag *string
1643 }
Jiyong Parkc678ad32018-04-10 13:07:10 +09001644 }{}
1645
Jihoon Kang1147b312023-06-08 23:25:57 +00001646 props.Name = proptools.StringPtr(module.sourceStubLibraryModuleName(apiScope))
Jihoon Kang786df932023-09-07 01:18:31 +00001647 props.Visibility = []string{"//visibility:override", "//visibility:private"}
Jiyong Parkc678ad32018-04-10 13:07:10 +09001648 // sources are generated from the droiddoc
Paul Duffinc3091c82020-05-08 14:16:20 +01001649 props.Srcs = []string{":" + module.stubsSourceModuleName(apiScope)}
Paul Duffin12ceb462019-12-24 20:31:31 +00001650 sdkVersion := module.sdkVersionForStubsLibrary(mctx, apiScope)
Paul Duffin52d398a2019-06-11 12:31:14 +01001651 props.Sdk_version = proptools.StringPtr(sdkVersion)
Paul Duffina18abc22020-05-16 18:54:24 +01001652 props.System_modules = module.deviceProperties.System_modules
1653 props.Patch_module = module.properties.Patch_module
Paul Duffin367ab912019-12-23 19:40:36 +00001654 props.Installable = proptools.BoolPtr(false)
Sundong Ahn054b19a2018-10-19 13:46:09 +09001655 props.Libs = module.sdkLibraryProperties.Stub_only_libs
Anton Hanssondae54cd2021-04-21 16:30:10 +01001656 props.Static_libs = module.sdkLibraryProperties.Stub_only_static_libs
Paul Duffine22c2ab2020-05-20 19:35:27 +01001657 // The stub-annotations library contains special versions of the annotations
1658 // with CLASS retention policy, so that they're kept.
1659 if proptools.Bool(module.sdkLibraryProperties.Annotations_enabled) {
1660 props.Libs = append(props.Libs, "stub-annotations")
1661 }
Paul Duffina18abc22020-05-16 18:54:24 +01001662 props.Openjdk9.Srcs = module.properties.Openjdk9.Srcs
1663 props.Openjdk9.Javacflags = module.properties.Openjdk9.Javacflags
Anton Hansson83509b52020-05-21 09:21:57 +01001664 // We compile the stubs for 1.8 in line with the main android.jar stubs, and potential
1665 // interop with older developer tools that don't support 1.9.
1666 props.Java_version = proptools.StringPtr("1.8")
Paul Duffinf4600f62021-05-13 22:34:45 +01001667
Paul Duffin859fe962020-05-15 10:20:31 +01001668 mctx.CreateModule(LibraryFactory, &props, module.sdkComponentPropertiesForChildLibrary())
Jiyong Parkc678ad32018-04-10 13:07:10 +09001669}
1670
Paul Duffin6d0886e2020-04-07 18:49:53 +01001671// Creates a droidstubs module that creates stubs source files from the given full source
Paul Duffinc8782502020-04-29 20:45:27 +01001672// files and also updates and checks the API specification files.
Paul Duffin15f34ef2020-07-20 18:04:44 +01001673func (module *SdkLibrary) createStubsSourcesAndApi(mctx android.DefaultableHookContext, apiScope *apiScope, name string, scopeSpecificDroidstubsArgs []string) {
Jiyong Parkc678ad32018-04-10 13:07:10 +09001674 props := struct {
Sundong Ahn054b19a2018-10-19 13:46:09 +09001675 Name *string
Paul Duffin4911a892020-04-29 23:35:13 +01001676 Visibility []string
Sundong Ahn054b19a2018-10-19 13:46:09 +09001677 Srcs []string
1678 Installable *bool
Paul Duffin52d398a2019-06-11 12:31:14 +01001679 Sdk_version *string
Jihoon Kang3198f3c2023-01-26 08:08:52 +00001680 Api_surface *string
Paul Duffin12ceb462019-12-24 20:31:31 +00001681 System_modules *string
Sundong Ahn054b19a2018-10-19 13:46:09 +09001682 Libs []string
Paul Duffin6877e6d2020-09-25 19:59:14 +01001683 Output_javadoc_comments *bool
Paul Duffin11512472019-02-11 15:55:17 +00001684 Arg_files []string
Sundong Ahn054b19a2018-10-19 13:46:09 +09001685 Args *string
Sundong Ahn054b19a2018-10-19 13:46:09 +09001686 Java_version *string
Paul Duffine22c2ab2020-05-20 19:35:27 +01001687 Annotations_enabled *bool
Sundong Ahn054b19a2018-10-19 13:46:09 +09001688 Merge_annotations_dirs []string
1689 Merge_inclusion_annotations_dirs []string
Paul Duffin0ff08bd2020-04-29 13:30:54 +01001690 Generate_stubs *bool
Anton Hanssone87b03d2020-12-21 15:29:34 +00001691 Previous_api *string
Sundong Ahn054b19a2018-10-19 13:46:09 +09001692 Check_api struct {
Anton Hanssone6056152020-12-31 10:37:27 +00001693 Current ApiToCheck
1694 Last_released ApiToCheck
Paul Duffin160fe412020-05-10 19:32:20 +01001695
1696 Api_lint struct {
1697 Enabled *bool
1698 New_since *string
1699 Baseline_file *string
1700 }
Jiyong Park58c518b2018-05-12 22:29:12 +09001701 }
Sundong Ahn1b92c822018-05-29 11:35:17 +09001702 Aidl struct {
1703 Include_dirs []string
1704 Local_include_dirs []string
1705 }
Paul Duffin040e9062020-11-23 17:41:36 +00001706 Dists []android.Dist
Jiyong Parkc678ad32018-04-10 13:07:10 +09001707 }{}
1708
Paul Duffin7b78b4d2020-04-28 14:08:32 +01001709 // The stubs source processing uses the same compile time classpath when extracting the
1710 // API from the implementation library as it does when compiling it. i.e. the same
1711 // * sdk version
1712 // * system_modules
1713 // * libs (static_libs/libs)
Paul Duffin250e6192019-06-07 10:44:37 +01001714
Paul Duffin0ff08bd2020-04-29 13:30:54 +01001715 props.Name = proptools.StringPtr(name)
Anton Hansson944e77d2020-08-19 11:40:22 +01001716 props.Visibility = childModuleVisibility(module.sdkLibraryProperties.Stubs_source_visibility)
Paul Duffina18abc22020-05-16 18:54:24 +01001717 props.Srcs = append(props.Srcs, module.properties.Srcs...)
Anton Hanssonf8ea3722021-09-16 14:24:13 +01001718 props.Srcs = append(props.Srcs, module.sdkLibraryProperties.Api_srcs...)
Paul Duffina18abc22020-05-16 18:54:24 +01001719 props.Sdk_version = module.deviceProperties.Sdk_version
Jihoon Kang3198f3c2023-01-26 08:08:52 +00001720 props.Api_surface = &apiScope.name
Paul Duffina18abc22020-05-16 18:54:24 +01001721 props.System_modules = module.deviceProperties.System_modules
Jiyong Parkc678ad32018-04-10 13:07:10 +09001722 props.Installable = proptools.BoolPtr(false)
Sundong Ahne6f0b052018-06-05 16:46:14 +09001723 // A droiddoc module has only one Libs property and doesn't distinguish between
1724 // shared libs and static libs. So we need to add both of these libs to Libs property.
Paul Duffina18abc22020-05-16 18:54:24 +01001725 props.Libs = module.properties.Libs
1726 props.Libs = append(props.Libs, module.properties.Static_libs...)
Nikita Ioffed732da72022-11-21 12:38:25 +00001727 props.Libs = append(props.Libs, module.sdkLibraryProperties.Stub_only_libs...)
Paul Duffina18abc22020-05-16 18:54:24 +01001728 props.Aidl.Include_dirs = module.deviceProperties.Aidl.Include_dirs
1729 props.Aidl.Local_include_dirs = module.deviceProperties.Aidl.Local_include_dirs
1730 props.Java_version = module.properties.Java_version
Jiyong Parkc678ad32018-04-10 13:07:10 +09001731
Paul Duffine22c2ab2020-05-20 19:35:27 +01001732 props.Annotations_enabled = module.sdkLibraryProperties.Annotations_enabled
Sundong Ahn054b19a2018-10-19 13:46:09 +09001733 props.Merge_annotations_dirs = module.sdkLibraryProperties.Merge_annotations_dirs
1734 props.Merge_inclusion_annotations_dirs = module.sdkLibraryProperties.Merge_inclusion_annotations_dirs
1735
Paul Duffin6d0886e2020-04-07 18:49:53 +01001736 droidstubsArgs := []string{}
Paul Duffin235ffff2019-12-24 10:41:30 +00001737 if len(module.sdkLibraryProperties.Api_packages) != 0 {
Paul Duffin6d0886e2020-04-07 18:49:53 +01001738 droidstubsArgs = append(droidstubsArgs, "--stub-packages "+strings.Join(module.sdkLibraryProperties.Api_packages, ":"))
Paul Duffin235ffff2019-12-24 10:41:30 +00001739 }
1740 if len(module.sdkLibraryProperties.Hidden_api_packages) != 0 {
Paul Duffin6d0886e2020-04-07 18:49:53 +01001741 droidstubsArgs = append(droidstubsArgs,
Paul Duffin235ffff2019-12-24 10:41:30 +00001742 android.JoinWithPrefix(module.sdkLibraryProperties.Hidden_api_packages, " --hide-package "))
1743 }
Paul Duffin6d0886e2020-04-07 18:49:53 +01001744 droidstubsArgs = append(droidstubsArgs, module.sdkLibraryProperties.Droiddoc_options...)
Paul Duffin235ffff2019-12-24 10:41:30 +00001745 disabledWarnings := []string{
Paul Duffin235ffff2019-12-24 10:41:30 +00001746 "BroadcastBehavior",
Paul Duffin235ffff2019-12-24 10:41:30 +00001747 "DeprecationMismatch",
Anton Hansson3c0779a2022-02-18 19:24:30 +00001748 "HiddenSuperclass",
Paul Duffin235ffff2019-12-24 10:41:30 +00001749 "HiddenTypeParameter",
Anton Hansson3c0779a2022-02-18 19:24:30 +00001750 "MissingPermission",
1751 "SdkConstant",
Paul Duffin235ffff2019-12-24 10:41:30 +00001752 "Todo",
Anton Hansson3c0779a2022-02-18 19:24:30 +00001753 "UnavailableSymbol",
Paul Duffin235ffff2019-12-24 10:41:30 +00001754 }
Paul Duffin6d0886e2020-04-07 18:49:53 +01001755 droidstubsArgs = append(droidstubsArgs, android.JoinWithPrefix(disabledWarnings, "--hide "))
Sundong Ahnfb2721f2018-09-17 13:23:09 +09001756
Paul Duffin6877e6d2020-09-25 19:59:14 +01001757 // Output Javadoc comments for public scope.
1758 if apiScope == apiScopePublic {
1759 props.Output_javadoc_comments = proptools.BoolPtr(true)
1760 }
1761
Paul Duffin1fb487d2020-04-07 18:50:10 +01001762 // Add in scope specific arguments.
Paul Duffin0ff08bd2020-04-29 13:30:54 +01001763 droidstubsArgs = append(droidstubsArgs, scopeSpecificDroidstubsArgs...)
Paul Duffin11512472019-02-11 15:55:17 +00001764 props.Arg_files = module.sdkLibraryProperties.Droiddoc_option_files
Paul Duffin6d0886e2020-04-07 18:49:53 +01001765 props.Args = proptools.StringPtr(strings.Join(droidstubsArgs, " "))
Jiyong Parkc678ad32018-04-10 13:07:10 +09001766
Paul Duffin15f34ef2020-07-20 18:04:44 +01001767 // List of APIs identified from the provided source files are created. They are later
1768 // compared against to the not-yet-released (a.k.a current) list of APIs and to the
1769 // last-released (a.k.a numbered) list of API.
1770 currentApiFileName := apiScope.apiFilePrefix + "current.txt"
1771 removedApiFileName := apiScope.apiFilePrefix + "removed.txt"
1772 apiDir := module.getApiDir()
1773 currentApiFileName = path.Join(apiDir, currentApiFileName)
1774 removedApiFileName = path.Join(apiDir, removedApiFileName)
Jiyong Parkc678ad32018-04-10 13:07:10 +09001775
Paul Duffin15f34ef2020-07-20 18:04:44 +01001776 // check against the not-yet-release API
1777 props.Check_api.Current.Api_file = proptools.StringPtr(currentApiFileName)
1778 props.Check_api.Current.Removed_api_file = proptools.StringPtr(removedApiFileName)
Jiyong Park58c518b2018-05-12 22:29:12 +09001779
Paul Duffin958806b2022-05-16 13:10:47 +00001780 if module.compareAgainstLatestApi(apiScope) {
Paul Duffin15f34ef2020-07-20 18:04:44 +01001781 // check against the latest released API
1782 latestApiFilegroupName := proptools.StringPtr(module.latestApiFilegroupName(apiScope))
Anton Hanssone87b03d2020-12-21 15:29:34 +00001783 props.Previous_api = latestApiFilegroupName
Paul Duffin15f34ef2020-07-20 18:04:44 +01001784 props.Check_api.Last_released.Api_file = latestApiFilegroupName
1785 props.Check_api.Last_released.Removed_api_file = proptools.StringPtr(
1786 module.latestRemovedApiFilegroupName(apiScope))
Jaewoong Jung1a97ee02021-03-09 13:25:02 -08001787 props.Check_api.Last_released.Baseline_file = proptools.StringPtr(
1788 module.latestIncompatibilitiesFilegroupName(apiScope))
Paul Duffin160fe412020-05-10 19:32:20 +01001789
Paul Duffin15f34ef2020-07-20 18:04:44 +01001790 if proptools.Bool(module.sdkLibraryProperties.Api_lint.Enabled) {
1791 // Enable api lint.
1792 props.Check_api.Api_lint.Enabled = proptools.BoolPtr(true)
1793 props.Check_api.Api_lint.New_since = latestApiFilegroupName
Paul Duffin160fe412020-05-10 19:32:20 +01001794
Paul Duffin15f34ef2020-07-20 18:04:44 +01001795 // If it exists then pass a lint-baseline.txt through to droidstubs.
1796 baselinePath := path.Join(apiDir, apiScope.apiFilePrefix+"lint-baseline.txt")
1797 baselinePathRelativeToRoot := path.Join(mctx.ModuleDir(), baselinePath)
1798 paths, err := mctx.GlobWithDeps(baselinePathRelativeToRoot, nil)
1799 if err != nil {
1800 mctx.ModuleErrorf("error checking for presence of %s: %s", baselinePathRelativeToRoot, err)
1801 }
1802 if len(paths) == 1 {
1803 props.Check_api.Api_lint.Baseline_file = proptools.StringPtr(baselinePath)
1804 } else if len(paths) != 0 {
1805 mctx.ModuleErrorf("error checking for presence of %s: expected one path, found: %v", baselinePathRelativeToRoot, paths)
Paul Duffin160fe412020-05-10 19:32:20 +01001806 }
1807 }
Paul Duffin15f34ef2020-07-20 18:04:44 +01001808 }
Jiyong Park58c518b2018-05-12 22:29:12 +09001809
Paul Duffin15f34ef2020-07-20 18:04:44 +01001810 if !Bool(module.sdkLibraryProperties.No_dist) {
Paul Duffin040e9062020-11-23 17:41:36 +00001811 // Dist the api txt and removed api txt artifacts for sdk builds.
1812 distDir := proptools.StringPtr(path.Join(module.apiDistPath(apiScope), "api"))
1813 for _, p := range []struct {
1814 tag string
1815 pattern string
1816 }{
1817 {tag: ".api.txt", pattern: "%s.txt"},
1818 {tag: ".removed-api.txt", pattern: "%s-removed.txt"},
1819 } {
1820 props.Dists = append(props.Dists, android.Dist{
1821 Targets: []string{"sdk", "win_sdk"},
1822 Dir: distDir,
1823 Dest: proptools.StringPtr(fmt.Sprintf(p.pattern, module.distStem())),
1824 Tag: proptools.StringPtr(p.tag),
1825 })
1826 }
Anton Hansson5fd5d242020-03-27 19:43:19 +00001827 }
1828
Jihoon Kangd48abd52023-02-02 22:32:31 +00001829 mctx.CreateModule(DroidstubsFactory, &props).(*Droidstubs).CallHookIfAvailable(mctx)
Jiyong Parkc678ad32018-04-10 13:07:10 +09001830}
1831
Jihoon Kang0c705a42023-08-02 06:44:57 +00001832func (module *SdkLibrary) createApiLibrary(mctx android.DefaultableHookContext, apiScope *apiScope, alternativeFullApiSurfaceStub string) {
Jihoon Kang1c92c3e2023-03-23 17:44:51 +00001833 props := struct {
Jihoon Kangca198c22023-06-22 23:13:51 +00001834 Name *string
1835 Visibility []string
1836 Api_contributions []string
1837 Libs []string
1838 Static_libs []string
1839 Full_api_surface_stub *string
Jihoon Kang1c92c3e2023-03-23 17:44:51 +00001840 }{}
1841
1842 props.Name = proptools.StringPtr(module.apiLibraryModuleName(apiScope))
Jihoon Kang786df932023-09-07 01:18:31 +00001843 props.Visibility = []string{"//visibility:override", "//visibility:private"}
Jihoon Kang1c92c3e2023-03-23 17:44:51 +00001844
1845 apiContributions := []string{}
1846
1847 // Api surfaces are not independent of each other, but have subset relationships,
1848 // and so does the api files. To generate from-text stubs for api surfaces other than public,
1849 // all subset api domains' api_contriubtions must be added as well.
1850 scope := apiScope
1851 for scope != nil {
1852 apiContributions = append(apiContributions, module.stubsSourceModuleName(scope)+".api.contribution")
1853 scope = scope.extends
1854 }
Jihoon Kang0c705a42023-08-02 06:44:57 +00001855 if apiScope == apiScopePublic {
1856 additionalApiContribution := module.apiLibraryAdditionalApiContribution()
1857 if additionalApiContribution != "" {
1858 apiContributions = append(apiContributions, additionalApiContribution)
1859 }
1860 }
Jihoon Kang1c92c3e2023-03-23 17:44:51 +00001861
1862 props.Api_contributions = apiContributions
1863 props.Libs = module.properties.Libs
1864 props.Libs = append(props.Libs, module.sdkLibraryProperties.Stub_only_libs...)
1865 props.Libs = append(props.Libs, "stub-annotations")
1866 props.Static_libs = module.sdkLibraryProperties.Stub_only_static_libs
Jihoon Kange7ee2562023-07-25 05:51:46 +00001867 props.Full_api_surface_stub = proptools.StringPtr(apiScope.kind.DefaultJavaLibraryName())
Jihoon Kang0c705a42023-08-02 06:44:57 +00001868 if alternativeFullApiSurfaceStub != "" {
1869 props.Full_api_surface_stub = proptools.StringPtr(alternativeFullApiSurfaceStub)
1870 }
Jihoon Kang1c92c3e2023-03-23 17:44:51 +00001871
1872 // android_module_lib_stubs_current.from-text only comprises api contributions from art, conscrypt and i18n.
1873 // Thus, replace with android_module_lib_stubs_current_full.from-text, which comprises every api domains.
1874 if apiScope.kind == android.SdkModule {
Jihoon Kangca198c22023-06-22 23:13:51 +00001875 props.Full_api_surface_stub = proptools.StringPtr(apiScope.kind.DefaultJavaLibraryName() + "_full.from-text")
Jihoon Kang1c92c3e2023-03-23 17:44:51 +00001876 }
1877
1878 mctx.CreateModule(ApiLibraryFactory, &props)
1879}
1880
Jihoon Kang1147b312023-06-08 23:25:57 +00001881func (module *SdkLibrary) createTopLevelStubsLibrary(
1882 mctx android.DefaultableHookContext, apiScope *apiScope, contributesToApiSurface bool) {
1883 props := struct {
1884 Name *string
1885 Visibility []string
1886 Sdk_version *string
1887 Static_libs []string
1888 System_modules *string
1889 Dist struct {
1890 Targets []string
1891 Dest *string
1892 Dir *string
1893 Tag *string
1894 }
1895 Compile_dex *bool
1896 }{}
1897 props.Name = proptools.StringPtr(module.stubsLibraryModuleName(apiScope))
1898 props.Visibility = childModuleVisibility(module.sdkLibraryProperties.Stubs_library_visibility)
1899 sdkVersion := module.sdkVersionForStubsLibrary(mctx, apiScope)
1900 props.Sdk_version = proptools.StringPtr(sdkVersion)
1901
1902 // Add the stub compiling java_library/java_api_library as static lib based on build config
1903 staticLib := module.sourceStubLibraryModuleName(apiScope)
1904 if mctx.Config().BuildFromTextStub() && contributesToApiSurface {
1905 staticLib = module.apiLibraryModuleName(apiScope)
1906 }
1907 props.Static_libs = append(props.Static_libs, staticLib)
1908 props.System_modules = module.deviceProperties.System_modules
1909
1910 // Dist the class jar artifact for sdk builds.
1911 if !Bool(module.sdkLibraryProperties.No_dist) {
1912 props.Dist.Targets = []string{"sdk", "win_sdk"}
1913 props.Dist.Dest = proptools.StringPtr(fmt.Sprintf("%v.jar", module.distStem()))
1914 props.Dist.Dir = proptools.StringPtr(module.apiDistPath(apiScope))
1915 props.Dist.Tag = proptools.StringPtr(".jar")
1916 }
1917
1918 // The imports need to be compiled to dex if the java_sdk_library requests it.
1919 compileDex := module.dexProperties.Compile_dex
1920 if module.stubLibrariesCompiledForDex() {
1921 compileDex = proptools.BoolPtr(true)
1922 }
1923 props.Compile_dex = compileDex
1924
1925 mctx.CreateModule(LibraryFactory, &props, module.sdkComponentPropertiesForChildLibrary())
1926}
1927
Paul Duffin958806b2022-05-16 13:10:47 +00001928func (module *SdkLibrary) compareAgainstLatestApi(apiScope *apiScope) bool {
1929 return !(apiScope.unstable || module.sdkLibraryProperties.Unsafe_ignore_missing_latest_api)
1930}
1931
Paul Duffinea8f8082021-06-24 13:25:57 +01001932// Implements android.ApexModule
Jooyung Han5e9013b2020-03-10 06:23:13 +09001933func (module *SdkLibrary) DepIsInSameApex(mctx android.BaseModuleContext, dep android.Module) bool {
1934 depTag := mctx.OtherModuleDependencyTag(dep)
1935 if depTag == xmlPermissionsFileTag {
1936 return true
1937 }
1938 return module.Library.DepIsInSameApex(mctx, dep)
1939}
1940
Paul Duffinea8f8082021-06-24 13:25:57 +01001941// Implements android.ApexModule
1942func (module *SdkLibrary) UniqueApexVariations() bool {
1943 return module.uniqueApexVariations()
1944}
1945
Jiyong Parkc678ad32018-04-10 13:07:10 +09001946// Creates the xml file that publicizes the runtime library
Paul Duffinf0229202020-04-29 16:47:28 +01001947func (module *SdkLibrary) createXmlFile(mctx android.DefaultableHookContext) {
Spandan Das8c9ae7e2023-03-03 21:20:36 +00001948 moduleMinApiLevel := module.Library.MinSdkVersion(mctx)
Pedro Loureiroc3621422021-09-28 15:40:23 +00001949 var moduleMinApiLevelStr = moduleMinApiLevel.String()
1950 if moduleMinApiLevel == android.NoneApiLevel {
1951 moduleMinApiLevelStr = "current"
1952 }
Jiyong Parke3833882020-02-17 17:28:10 +09001953 props := struct {
Pedro Loureiroc3621422021-09-28 15:40:23 +00001954 Name *string
1955 Lib_name *string
1956 Apex_available []string
1957 On_bootclasspath_since *string
1958 On_bootclasspath_before *string
1959 Min_device_sdk *string
1960 Max_device_sdk *string
1961 Sdk_library_min_api_level *string
Jiyong Parke3833882020-02-17 17:28:10 +09001962 }{
Pedro Loureiroc3621422021-09-28 15:40:23 +00001963 Name: proptools.StringPtr(module.xmlPermissionsModuleName()),
1964 Lib_name: proptools.StringPtr(module.BaseModuleName()),
1965 Apex_available: module.ApexProperties.Apex_available,
1966 On_bootclasspath_since: module.commonSdkLibraryProperties.On_bootclasspath_since,
1967 On_bootclasspath_before: module.commonSdkLibraryProperties.On_bootclasspath_before,
1968 Min_device_sdk: module.commonSdkLibraryProperties.Min_device_sdk,
1969 Max_device_sdk: module.commonSdkLibraryProperties.Max_device_sdk,
1970 Sdk_library_min_api_level: &moduleMinApiLevelStr,
Jiyong Parkc678ad32018-04-10 13:07:10 +09001971 }
Jiyong Parke3833882020-02-17 17:28:10 +09001972
Jiyong Parke3833882020-02-17 17:28:10 +09001973 mctx.CreateModule(sdkLibraryXmlFactory, &props)
Jiyong Parkc678ad32018-04-10 13:07:10 +09001974}
1975
Jiyong Parkf1691d22021-03-29 20:11:58 +09001976func PrebuiltJars(ctx android.BaseModuleContext, baseName string, s android.SdkSpec) android.Paths {
Jiyong Park54105c42021-03-31 18:17:53 +09001977 var ver android.ApiLevel
Jiyong Parkf1691d22021-03-29 20:11:58 +09001978 var kind android.SdkKind
1979 if s.UsePrebuilt(ctx) {
Jiyong Park54105c42021-03-31 18:17:53 +09001980 ver = s.ApiLevel
Jiyong Parkf1691d22021-03-29 20:11:58 +09001981 kind = s.Kind
Jiyong Parkc678ad32018-04-10 13:07:10 +09001982 } else {
Jiyong Park6a927c42020-01-21 02:03:43 +09001983 // We don't have prebuilt SDK for the specific sdkVersion.
1984 // Instead of breaking the build, fallback to use "system_current"
Jiyong Park54105c42021-03-31 18:17:53 +09001985 ver = android.FutureApiLevel
Jiyong Parkf1691d22021-03-29 20:11:58 +09001986 kind = android.SdkSystem
Sundong Ahn054b19a2018-10-19 13:46:09 +09001987 }
Jiyong Park6a927c42020-01-21 02:03:43 +09001988
1989 dir := filepath.Join("prebuilts", "sdk", ver.String(), kind.String())
Paul Duffin50061512020-01-21 16:31:05 +00001990 jar := filepath.Join(dir, baseName+".jar")
Sundong Ahn054b19a2018-10-19 13:46:09 +09001991 jarPath := android.ExistentPathForSource(ctx, jar)
Sundong Ahnae418ac2019-02-28 15:01:28 +09001992 if !jarPath.Valid() {
Colin Cross07c88562020-01-07 09:34:44 -08001993 if ctx.Config().AllowMissingDependencies() {
1994 return android.Paths{android.PathForSource(ctx, jar)}
1995 } else {
Jiyong Parkf1691d22021-03-29 20:11:58 +09001996 ctx.PropertyErrorf("sdk_library", "invalid sdk version %q, %q does not exist", s.Raw, jar)
Colin Cross07c88562020-01-07 09:34:44 -08001997 }
Sundong Ahnae418ac2019-02-28 15:01:28 +09001998 return nil
1999 }
Sundong Ahn054b19a2018-10-19 13:46:09 +09002000 return android.Paths{jarPath.Path()}
2001}
2002
Colin Crossaede88c2020-08-11 12:17:01 -07002003// 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 +01002004//
2005// If either this or the other module are on the platform then this will return
2006// false.
Colin Cross56a83212020-09-15 18:30:11 -07002007func withinSameApexesAs(ctx android.BaseModuleContext, other android.Module) bool {
2008 apexInfo := ctx.Provider(android.ApexInfoProvider).(android.ApexInfo)
2009 otherApexInfo := ctx.OtherModuleProvider(other, android.ApexInfoProvider).(android.ApexInfo)
Jiyong Parkab50b072021-05-12 17:13:56 +09002010 return len(otherApexInfo.InApexVariants) > 0 && reflect.DeepEqual(apexInfo.InApexVariants, otherApexInfo.InApexVariants)
Paul Duffin9b879592020-05-26 13:21:35 +01002011}
2012
Jiyong Parkf1691d22021-03-29 20:11:58 +09002013func (module *SdkLibrary) sdkJars(ctx android.BaseModuleContext, sdkVersion android.SdkSpec, headerJars bool) android.Paths {
Jiyong Park932cdfe2020-05-28 00:19:53 +09002014 // If the client doesn't set sdk_version, but if this library prefers stubs over
2015 // the impl library, let's provide the widest API surface possible. To do so,
2016 // force override sdk_version to module_current so that the closest possible API
2017 // surface could be found in selectHeaderJarsForSdkVersion
Jiyong Parkf1691d22021-03-29 20:11:58 +09002018 if module.defaultsToStubs() && !sdkVersion.Specified() {
Jiyong Park92315372021-04-02 08:45:46 +09002019 sdkVersion = android.SdkSpecFrom(ctx, "module_current")
Jiyong Park932cdfe2020-05-28 00:19:53 +09002020 }
Paul Duffind1b3a922020-01-22 11:57:20 +00002021
Paul Duffindaaa3322020-05-26 18:13:57 +01002022 // Only provide access to the implementation library if it is actually built.
2023 if module.requiresRuntimeImplementationLibrary() {
2024 // Check any special cases for java_sdk_library.
2025 //
2026 // Only allow access to the implementation library in the following condition:
2027 // * No sdk_version specified on the referencing module.
Paul Duffin9b879592020-05-26 13:21:35 +01002028 // * The referencing module is in the same apex as this.
Jiyong Parkf1691d22021-03-29 20:11:58 +09002029 if sdkVersion.Kind == android.SdkPrivate || withinSameApexesAs(ctx, module) {
Paul Duffindaaa3322020-05-26 18:13:57 +01002030 if headerJars {
2031 return module.HeaderJars()
2032 } else {
2033 return module.ImplementationJars()
2034 }
Sundong Ahn054b19a2018-10-19 13:46:09 +09002035 }
Jiyong Parkc678ad32018-04-10 13:07:10 +09002036 }
Paul Duffinb05d4292020-05-20 12:19:10 +01002037
Paul Duffin23970f42020-05-20 14:20:02 +01002038 return module.selectHeaderJarsForSdkVersion(ctx, sdkVersion)
Jiyong Parkc678ad32018-04-10 13:07:10 +09002039}
2040
Sundong Ahn241cd372018-07-13 16:16:44 +09002041// to satisfy SdkLibraryDependency interface
Jiyong Parkf1691d22021-03-29 20:11:58 +09002042func (module *SdkLibrary) SdkHeaderJars(ctx android.BaseModuleContext, sdkVersion android.SdkSpec) android.Paths {
Paul Duffind1b3a922020-01-22 11:57:20 +00002043 return module.sdkJars(ctx, sdkVersion, true /*headerJars*/)
2044}
2045
2046// to satisfy SdkLibraryDependency interface
Jiyong Parkf1691d22021-03-29 20:11:58 +09002047func (module *SdkLibrary) SdkImplementationJars(ctx android.BaseModuleContext, sdkVersion android.SdkSpec) android.Paths {
Paul Duffind1b3a922020-01-22 11:57:20 +00002048 return module.sdkJars(ctx, sdkVersion, false /*headerJars*/)
Sundong Ahn241cd372018-07-13 16:16:44 +09002049}
2050
Colin Cross571cccf2019-02-04 11:22:08 -08002051var javaSdkLibrariesKey = android.NewOnceKey("javaSdkLibraries")
2052
Jiyong Park82484c02018-04-23 21:41:26 +09002053func javaSdkLibraries(config android.Config) *[]string {
Colin Cross571cccf2019-02-04 11:22:08 -08002054 return config.Once(javaSdkLibrariesKey, func() interface{} {
Jiyong Park82484c02018-04-23 21:41:26 +09002055 return &[]string{}
2056 }).(*[]string)
2057}
2058
Paul Duffin749f98f2019-12-30 17:23:46 +00002059func (module *SdkLibrary) getApiDir() string {
2060 return proptools.StringDefault(module.sdkLibraryProperties.Api_dir, "api")
2061}
2062
Jiyong Parkc678ad32018-04-10 13:07:10 +09002063// For a java_sdk_library module, create internal modules for stubs, docs,
2064// runtime libs and xml file. If requested, the stubs and docs are created twice
2065// once for public API level and once for system API level
Paul Duffinf0229202020-04-29 16:47:28 +01002066func (module *SdkLibrary) CreateInternalModules(mctx android.DefaultableHookContext) {
2067 // If the module has been disabled then don't create any child modules.
2068 if !module.Enabled() {
2069 return
2070 }
2071
Paul Duffina18abc22020-05-16 18:54:24 +01002072 if len(module.properties.Srcs) == 0 {
Inseob Kimc0907f12019-02-08 21:00:45 +09002073 mctx.PropertyErrorf("srcs", "java_sdk_library must specify srcs")
Jooyung Han58f26ab2019-12-18 15:34:32 +09002074 return
Inseob Kimc0907f12019-02-08 21:00:45 +09002075 }
2076
Paul Duffin37e0b772019-12-30 17:20:10 +00002077 // If this builds against standard libraries (i.e. is not part of the core libraries)
Paul Duffin4f5c1ef2020-11-19 14:53:43 +00002078 // then assume it provides both system and test apis.
Jiyong Parkf1691d22021-03-29 20:11:58 +09002079 sdkDep := decodeSdkDep(mctx, android.SdkContext(&module.Library))
Paul Duffin37e0b772019-12-30 17:20:10 +00002080 hasSystemAndTestApis := sdkDep.hasStandardLibs()
Paul Duffin3375e352020-04-28 10:44:03 +01002081 module.sdkLibraryProperties.Generate_system_and_test_apis = hasSystemAndTestApis
Paul Duffin4f5c1ef2020-11-19 14:53:43 +00002082
Jaewoong Jung18aefc12020-12-21 09:11:10 -08002083 missingCurrentApi := false
Inseob Kim8098faa2019-03-18 10:19:51 +09002084
Paul Duffin3375e352020-04-28 10:44:03 +01002085 generatedScopes := module.getGeneratedApiScopes(mctx)
Paul Duffind1b3a922020-01-22 11:57:20 +00002086
Paul Duffin749f98f2019-12-30 17:23:46 +00002087 apiDir := module.getApiDir()
Paul Duffin3375e352020-04-28 10:44:03 +01002088 for _, scope := range generatedScopes {
Inseob Kim8098faa2019-03-18 10:19:51 +09002089 for _, api := range []string{"current.txt", "removed.txt"} {
Paul Duffind1b3a922020-01-22 11:57:20 +00002090 path := path.Join(mctx.ModuleDir(), apiDir, scope.apiFilePrefix+api)
Inseob Kim8098faa2019-03-18 10:19:51 +09002091 p := android.ExistentPathForSource(mctx, path)
2092 if !p.Valid() {
Colin Cross18f840c2021-05-20 17:56:54 -07002093 if mctx.Config().AllowMissingDependencies() {
2094 mctx.AddMissingDependencies([]string{path})
2095 } else {
2096 mctx.ModuleErrorf("Current api file %#v doesn't exist", path)
2097 missingCurrentApi = true
2098 }
Inseob Kim8098faa2019-03-18 10:19:51 +09002099 }
2100 }
2101 }
2102
Jaewoong Jung18aefc12020-12-21 09:11:10 -08002103 if missingCurrentApi {
Inseob Kim8098faa2019-03-18 10:19:51 +09002104 script := "build/soong/scripts/gen-java-current-api-files.sh"
2105 p := android.ExistentPathForSource(mctx, script)
2106
2107 if !p.Valid() {
2108 panic(fmt.Sprintf("script file %s doesn't exist", script))
2109 }
2110
2111 mctx.ModuleErrorf("One or more current api files are missing. "+
2112 "You can update them by:\n"+
Paul Duffin37e0b772019-12-30 17:20:10 +00002113 "%s %q %s && m update-api",
Paul Duffind1b3a922020-01-22 11:57:20 +00002114 script, filepath.Join(mctx.ModuleDir(), apiDir),
Paul Duffin3375e352020-04-28 10:44:03 +01002115 strings.Join(generatedScopes.Strings(func(s *apiScope) string { return s.apiFilePrefix }), " "))
Inseob Kim8098faa2019-03-18 10:19:51 +09002116 return
2117 }
2118
Paul Duffin3375e352020-04-28 10:44:03 +01002119 for _, scope := range generatedScopes {
Paul Duffin15f34ef2020-07-20 18:04:44 +01002120 // Use the stubs source name for legacy reasons.
2121 module.createStubsSourcesAndApi(mctx, scope, module.stubsSourceModuleName(scope), scope.droidstubsArgs)
Paul Duffin0ff08bd2020-04-29 13:30:54 +01002122
Paul Duffind1b3a922020-01-22 11:57:20 +00002123 module.createStubsLibrary(mctx, scope)
Jihoon Kang1c92c3e2023-03-23 17:44:51 +00002124
Jihoon Kang0c705a42023-08-02 06:44:57 +00002125 alternativeFullApiSurfaceStubLib := ""
2126 if scope == apiScopePublic {
2127 alternativeFullApiSurfaceStubLib = module.alternativeFullApiSurfaceStubLib()
2128 }
2129 contributesToApiSurface := module.contributesToApiSurface(mctx.Config()) || alternativeFullApiSurfaceStubLib != ""
Jihoon Kang1147b312023-06-08 23:25:57 +00002130 if contributesToApiSurface {
Jihoon Kang0c705a42023-08-02 06:44:57 +00002131 module.createApiLibrary(mctx, scope, alternativeFullApiSurfaceStubLib)
Jihoon Kang1c92c3e2023-03-23 17:44:51 +00002132 }
Jihoon Kang1147b312023-06-08 23:25:57 +00002133
2134 module.createTopLevelStubsLibrary(mctx, scope, contributesToApiSurface)
Inseob Kimc0907f12019-02-08 21:00:45 +09002135 }
2136
Paul Duffindfa131e2020-05-15 20:37:11 +01002137 if module.requiresRuntimeImplementationLibrary() {
Paul Duffin5df79302020-05-16 15:52:12 +01002138 // Create child module to create an implementation library.
2139 //
2140 // This temporarily creates a second implementation library that can be explicitly
2141 // referenced.
2142 //
2143 // TODO(b/156618935) - update comment once only one implementation library is created.
2144 module.createImplLibrary(mctx)
2145
Paul Duffindfa131e2020-05-15 20:37:11 +01002146 // Only create an XML permissions file that declares the library as being usable
2147 // as a shared library if required.
2148 if module.sharedLibrary() {
2149 module.createXmlFile(mctx)
2150 }
Paul Duffin43db9be2019-12-30 17:35:49 +00002151
2152 // record java_sdk_library modules so that they are exported to make
2153 javaSdkLibraries := javaSdkLibraries(mctx.Config())
2154 javaSdkLibrariesLock.Lock()
2155 defer javaSdkLibrariesLock.Unlock()
2156 *javaSdkLibraries = append(*javaSdkLibraries, module.BaseModuleName())
2157 }
Anton Hansson7f66efa2020-10-08 14:47:23 +01002158
Paul Duffin77590a82022-04-28 14:13:30 +00002159 // 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 +01002160 module.properties.Libs = append(module.properties.Libs, module.sdkLibraryProperties.Impl_only_libs...)
Paul Duffin77590a82022-04-28 14:13:30 +00002161 module.properties.Static_libs = append(module.properties.Static_libs, module.sdkLibraryProperties.Impl_only_static_libs...)
Inseob Kimc0907f12019-02-08 21:00:45 +09002162}
2163
2164func (module *SdkLibrary) InitSdkLibraryProperties() {
Colin Crossce6734e2020-06-15 16:09:53 -07002165 module.addHostAndDeviceProperties()
2166 module.AddProperties(&module.sdkLibraryProperties)
Sundong Ahn054b19a2018-10-19 13:46:09 +09002167
Paul Duffin71b33cc2021-06-23 11:39:47 +01002168 module.initSdkLibraryComponent(module)
Paul Duffin859fe962020-05-15 10:20:31 +01002169
Paul Duffina18abc22020-05-16 18:54:24 +01002170 module.properties.Installable = proptools.BoolPtr(true)
2171 module.deviceProperties.IsSDKLibrary = true
Inseob Kimc0907f12019-02-08 21:00:45 +09002172}
Sundong Ahn054b19a2018-10-19 13:46:09 +09002173
Paul Duffindfa131e2020-05-15 20:37:11 +01002174func (module *SdkLibrary) requiresRuntimeImplementationLibrary() bool {
2175 return !proptools.Bool(module.sdkLibraryProperties.Api_only)
2176}
2177
Jiyong Park932cdfe2020-05-28 00:19:53 +09002178func (module *SdkLibrary) defaultsToStubs() bool {
2179 return proptools.Bool(module.sdkLibraryProperties.Default_to_stubs)
2180}
2181
Paul Duffin1b1e8062020-05-08 13:44:43 +01002182// Defines how to name the individual component modules the sdk library creates.
2183type sdkLibraryComponentNamingScheme interface {
2184 stubsLibraryModuleName(scope *apiScope, baseName string) string
2185
2186 stubsSourceModuleName(scope *apiScope, baseName string) string
Jihoon Kang1c92c3e2023-03-23 17:44:51 +00002187
2188 apiLibraryModuleName(scope *apiScope, baseName string) string
Jihoon Kang1147b312023-06-08 23:25:57 +00002189
2190 sourceStubLibraryModuleName(scope *apiScope, baseName string) string
Paul Duffin1b1e8062020-05-08 13:44:43 +01002191}
2192
2193type defaultNamingScheme struct {
2194}
2195
2196func (s *defaultNamingScheme) stubsLibraryModuleName(scope *apiScope, baseName string) string {
2197 return scope.stubsLibraryModuleName(baseName)
2198}
2199
2200func (s *defaultNamingScheme) stubsSourceModuleName(scope *apiScope, baseName string) string {
2201 return scope.stubsSourceModuleName(baseName)
2202}
2203
Jihoon Kang1c92c3e2023-03-23 17:44:51 +00002204func (s *defaultNamingScheme) apiLibraryModuleName(scope *apiScope, baseName string) string {
2205 return scope.apiLibraryModuleName(baseName)
2206}
2207
Jihoon Kang1147b312023-06-08 23:25:57 +00002208func (s *defaultNamingScheme) sourceStubLibraryModuleName(scope *apiScope, baseName string) string {
2209 return scope.sourceStubLibraryModuleName(baseName)
2210}
2211
Paul Duffin1b1e8062020-05-08 13:44:43 +01002212var _ sdkLibraryComponentNamingScheme = (*defaultNamingScheme)(nil)
2213
Jaewoong Jungbc15e3a2021-03-10 17:02:43 -08002214func moduleStubLinkType(name string) (stub bool, ret sdkLinkType) {
Jihoon Kang1147b312023-06-08 23:25:57 +00002215 name = strings.TrimSuffix(name, ".from-source")
2216
Anton Hansson2d0c1942020-05-25 12:20:51 +01002217 // This suffix-based approach is fragile and could potentially mis-trigger.
2218 // TODO(b/155164730): Clean this up when modules no longer reference sdk_lib stubs directly.
Anton Hansson08f476b2021-04-07 15:32:19 +01002219 if strings.HasSuffix(name, apiScopePublic.stubsLibraryModuleNameSuffix()) {
2220 if name == "hwbinder.stubs" || name == "libcore_private.stubs" {
2221 // Due to a previous bug, these modules were not considered stubs, so we retain that.
2222 return false, javaPlatform
2223 }
Anton Hansson2d0c1942020-05-25 12:20:51 +01002224 return true, javaSdk
2225 }
Anton Hansson08f476b2021-04-07 15:32:19 +01002226 if strings.HasSuffix(name, apiScopeSystem.stubsLibraryModuleNameSuffix()) {
Anton Hansson2d0c1942020-05-25 12:20:51 +01002227 return true, javaSystem
2228 }
Anton Hansson08f476b2021-04-07 15:32:19 +01002229 if strings.HasSuffix(name, apiScopeModuleLib.stubsLibraryModuleNameSuffix()) {
Anton Hansson2d0c1942020-05-25 12:20:51 +01002230 return true, javaModule
2231 }
Anton Hansson08f476b2021-04-07 15:32:19 +01002232 if strings.HasSuffix(name, apiScopeTest.stubsLibraryModuleNameSuffix()) {
Anton Hansson2d0c1942020-05-25 12:20:51 +01002233 return true, javaSystem
2234 }
Jihoon Kang1147b312023-06-08 23:25:57 +00002235 if strings.HasSuffix(name, apiScopeSystemServer.stubsLibraryModuleNameSuffix()) {
2236 return true, javaSystemServer
2237 }
Anton Hansson2d0c1942020-05-25 12:20:51 +01002238 return false, javaPlatform
2239}
2240
Jaewoong Jung4f158ee2019-07-11 10:05:35 -07002241// java_sdk_library is a special Java library that provides optional platform APIs to apps.
2242// In practice, it can be viewed as a combination of several modules: 1) stubs library that clients
2243// are linked against to, 2) droiddoc module that internally generates API stubs source files,
2244// 3) the real runtime shared library that implements the APIs, and 4) XML file for adding
2245// the runtime lib to the classpath at runtime if requested via <uses-library>.
Inseob Kimc0907f12019-02-08 21:00:45 +09002246func SdkLibraryFactory() android.Module {
2247 module := &SdkLibrary{}
Paul Duffinc3091c82020-05-08 14:16:20 +01002248
2249 // Initialize information common between source and prebuilt.
Paul Duffin71b33cc2021-06-23 11:39:47 +01002250 module.initCommon(module)
Paul Duffinc3091c82020-05-08 14:16:20 +01002251
Inseob Kimc0907f12019-02-08 21:00:45 +09002252 module.InitSdkLibraryProperties()
Jooyung Han58f26ab2019-12-18 15:34:32 +09002253 android.InitApexModule(module)
Sundong Ahn054b19a2018-10-19 13:46:09 +09002254 InitJavaModule(module, android.HostAndDeviceSupported)
Paul Duffin3375e352020-04-28 10:44:03 +01002255
2256 // Initialize the map from scope to scope specific properties.
2257 scopeToProperties := make(map[*apiScope]*ApiScopeProperties)
2258 for _, scope := range allApiScopes {
2259 scopeToProperties[scope] = scope.scopeSpecificProperties(module)
2260 }
2261 module.scopeToProperties = scopeToProperties
2262
Paul Duffin4911a892020-04-29 23:35:13 +01002263 // Add the properties containing visibility rules so that they are checked.
Paul Duffin5df79302020-05-16 15:52:12 +01002264 android.AddVisibilityProperty(module, "impl_library_visibility", &module.sdkLibraryProperties.Impl_library_visibility)
Paul Duffin4911a892020-04-29 23:35:13 +01002265 android.AddVisibilityProperty(module, "stubs_library_visibility", &module.sdkLibraryProperties.Stubs_library_visibility)
2266 android.AddVisibilityProperty(module, "stubs_source_visibility", &module.sdkLibraryProperties.Stubs_source_visibility)
2267
Paul Duffin1b1e8062020-05-08 13:44:43 +01002268 module.SetDefaultableHook(func(ctx android.DefaultableHookContext) {
Paul Duffindfa131e2020-05-15 20:37:11 +01002269 // If no implementation is required then it cannot be used as a shared library
2270 // either.
2271 if !module.requiresRuntimeImplementationLibrary() {
2272 // If shared_library has been explicitly set to true then it is incompatible
2273 // with api_only: true.
2274 if proptools.Bool(module.commonSdkLibraryProperties.Shared_library) {
2275 ctx.PropertyErrorf("api_only/shared_library", "inconsistent settings, shared_library and api_only cannot both be true")
2276 }
2277 // Set shared_library: false.
2278 module.commonSdkLibraryProperties.Shared_library = proptools.BoolPtr(false)
2279 }
2280
Paul Duffin1b1e8062020-05-08 13:44:43 +01002281 if module.initCommonAfterDefaultsApplied(ctx) {
2282 module.CreateInternalModules(ctx)
2283 }
2284 })
Zi Wangb2179e32023-01-31 15:53:30 -08002285 android.InitBazelModule(module)
Jiyong Parkc678ad32018-04-10 13:07:10 +09002286 return module
2287}
Colin Cross79c7c262019-04-17 11:11:46 -07002288
Zi Wangb2179e32023-01-31 15:53:30 -08002289type bazelSdkLibraryAttributes struct {
2290 Public bazel.StringAttribute
2291 System bazel.StringAttribute
2292 Test bazel.StringAttribute
2293 Module_lib bazel.StringAttribute
2294 System_server bazel.StringAttribute
2295}
2296
2297// java_sdk_library bp2build converter
2298func (module *SdkLibrary) ConvertWithBp2build(ctx android.TopDownMutatorContext) {
2299 if ctx.ModuleType() != "java_sdk_library" {
Chris Parsons39a16972023-06-08 14:28:51 +00002300 ctx.MarkBp2buildUnconvertible(bp2build_metrics_proto.UnconvertedReasonType_TYPE_UNSUPPORTED, "")
Zi Wangb2179e32023-01-31 15:53:30 -08002301 return
2302 }
2303
2304 nameToAttr := make(map[string]bazel.StringAttribute)
2305
2306 for _, scope := range module.getGeneratedApiScopes(ctx) {
2307 apiSurfaceFile := path.Join(module.getApiDir(), scope.apiFilePrefix+"current.txt")
2308 var scopeStringAttribute bazel.StringAttribute
2309 scopeStringAttribute.SetValue(apiSurfaceFile)
2310 nameToAttr[scope.name] = scopeStringAttribute
2311 }
2312
2313 attrs := bazelSdkLibraryAttributes{
2314 Public: nameToAttr["public"],
2315 System: nameToAttr["system"],
2316 Test: nameToAttr["test"],
2317 Module_lib: nameToAttr["module-lib"],
2318 System_server: nameToAttr["system-server"],
2319 }
2320 props := bazel.BazelTargetModuleProperties{
2321 Rule_class: "java_sdk_library",
2322 Bzl_load_location: "//build/bazel/rules/java:sdk_library.bzl",
2323 }
2324
2325 ctx.CreateBazelTargetModule(props, android.CommonAttributes{Name: module.Name()}, &attrs)
2326}
2327
Colin Cross79c7c262019-04-17 11:11:46 -07002328//
2329// SDK library prebuilts
2330//
2331
Paul Duffin56d44902020-01-31 13:36:25 +00002332// Properties associated with each api scope.
2333type sdkLibraryScopeProperties struct {
Colin Cross79c7c262019-04-17 11:11:46 -07002334 Jars []string `android:"path"`
2335
2336 Sdk_version *string
2337
Colin Cross79c7c262019-04-17 11:11:46 -07002338 // List of shared java libs that this module has dependencies to
2339 Libs []string
Paul Duffin3d1248c2020-04-09 00:10:17 +01002340
Paul Duffinc8782502020-04-29 20:45:27 +01002341 // The stubs source.
Paul Duffin3d1248c2020-04-09 00:10:17 +01002342 Stub_srcs []string `android:"path"`
Paul Duffin1fd005d2020-04-09 01:08:11 +01002343
2344 // The current.txt
Paul Duffin0f8faff2020-05-20 16:18:00 +01002345 Current_api *string `android:"path"`
Paul Duffin1fd005d2020-04-09 01:08:11 +01002346
2347 // The removed.txt
Paul Duffin0f8faff2020-05-20 16:18:00 +01002348 Removed_api *string `android:"path"`
Anton Hanssond78eb762021-09-21 15:25:12 +01002349
2350 // Annotation zip
2351 Annotations *string `android:"path"`
Colin Cross79c7c262019-04-17 11:11:46 -07002352}
2353
Paul Duffin56d44902020-01-31 13:36:25 +00002354type sdkLibraryImportProperties struct {
Paul Duffinfcfd7912020-01-31 17:54:30 +00002355 // List of shared java libs, common to all scopes, that this module has
2356 // dependencies to
2357 Libs []string
Paul Duffin1267d872021-04-16 17:21:36 +01002358
2359 // If set to true, compile dex files for the stubs. Defaults to false.
2360 Compile_dex *bool
Paul Duffin869de142021-07-15 14:14:41 +01002361
2362 // If not empty, classes are restricted to the specified packages and their sub-packages.
Paul Duffin869de142021-07-15 14:14:41 +01002363 Permitted_packages []string
Paul Duffin56d44902020-01-31 13:36:25 +00002364}
2365
Paul Duffineedc5d52020-06-12 17:46:39 +01002366type SdkLibraryImport struct {
Colin Cross79c7c262019-04-17 11:11:46 -07002367 android.ModuleBase
2368 android.DefaultableModuleBase
2369 prebuilt android.Prebuilt
Paul Duffindd46f712020-02-10 13:37:10 +00002370 android.ApexModuleBase
Colin Cross79c7c262019-04-17 11:11:46 -07002371
Paul Duffin37856732021-02-26 14:24:15 +00002372 hiddenAPI
Jiakai Zhang204356f2021-09-09 08:12:46 +00002373 dexpreopter
Paul Duffin37856732021-02-26 14:24:15 +00002374
Colin Cross79c7c262019-04-17 11:11:46 -07002375 properties sdkLibraryImportProperties
2376
Paul Duffin46a26a82020-04-07 19:27:04 +01002377 // Map from api scope to the scope specific property structure.
2378 scopeProperties map[*apiScope]*sdkLibraryScopeProperties
2379
Paul Duffin56d44902020-01-31 13:36:25 +00002380 commonToSdkLibraryAndImport
Paul Duffineedc5d52020-06-12 17:46:39 +01002381
2382 // The reference to the implementation library created by the source module.
2383 // Is nil if the source module does not exist.
2384 implLibraryModule *Library
2385
2386 // The reference to the xml permissions module created by the source module.
2387 // Is nil if the source module does not exist.
2388 xmlPermissionsFileModule *sdkLibraryXml
Paul Duffin39853512021-02-26 11:09:39 +00002389
Jeongik Chad5fe8782021-07-08 01:13:11 +09002390 // Build path to the dex implementation jar obtained from the prebuilt_apex, if any.
Martin Stjernholm8be1e6d2021-09-15 03:34:04 +01002391 dexJarFile OptionalDexJarPath
Jeongik Chad5fe8782021-07-08 01:13:11 +09002392
2393 // Expected install file path of the source module(sdk_library)
2394 // or dex implementation jar obtained from the prebuilt_apex, if any.
2395 installFile android.Path
Colin Cross79c7c262019-04-17 11:11:46 -07002396}
2397
Paul Duffineedc5d52020-06-12 17:46:39 +01002398var _ SdkLibraryDependency = (*SdkLibraryImport)(nil)
Colin Cross79c7c262019-04-17 11:11:46 -07002399
Paul Duffin46a26a82020-04-07 19:27:04 +01002400// The type of a structure that contains a field of type sdkLibraryScopeProperties
2401// for each apiscope in allApiScopes, e.g. something like:
Colin Crossd079e0b2022-08-16 10:27:33 -07002402//
2403// struct {
2404// Public sdkLibraryScopeProperties
2405// System sdkLibraryScopeProperties
2406// ...
2407// }
Paul Duffin46a26a82020-04-07 19:27:04 +01002408var allScopeStructType = createAllScopePropertiesStructType()
2409
2410// Dynamically create a structure type for each apiscope in allApiScopes.
2411func createAllScopePropertiesStructType() reflect.Type {
2412 var fields []reflect.StructField
2413 for _, apiScope := range allApiScopes {
2414 field := reflect.StructField{
2415 Name: apiScope.fieldName,
2416 Type: reflect.TypeOf(sdkLibraryScopeProperties{}),
2417 }
2418 fields = append(fields, field)
2419 }
2420
2421 return reflect.StructOf(fields)
2422}
2423
2424// Create an instance of the scope specific structure type and return a map
2425// from apiscope to a pointer to each scope specific field.
2426func createPropertiesInstance() (interface{}, map[*apiScope]*sdkLibraryScopeProperties) {
2427 allScopePropertiesPtr := reflect.New(allScopeStructType)
2428 allScopePropertiesStruct := allScopePropertiesPtr.Elem()
2429 scopeProperties := make(map[*apiScope]*sdkLibraryScopeProperties)
2430
2431 for _, apiScope := range allApiScopes {
2432 field := allScopePropertiesStruct.FieldByName(apiScope.fieldName)
2433 scopeProperties[apiScope] = field.Addr().Interface().(*sdkLibraryScopeProperties)
2434 }
2435
2436 return allScopePropertiesPtr.Interface(), scopeProperties
2437}
2438
Jaewoong Jung4f158ee2019-07-11 10:05:35 -07002439// java_sdk_library_import imports a prebuilt java_sdk_library.
Colin Cross79c7c262019-04-17 11:11:46 -07002440func sdkLibraryImportFactory() android.Module {
Paul Duffineedc5d52020-06-12 17:46:39 +01002441 module := &SdkLibraryImport{}
Colin Cross79c7c262019-04-17 11:11:46 -07002442
Paul Duffin46a26a82020-04-07 19:27:04 +01002443 allScopeProperties, scopeToProperties := createPropertiesInstance()
2444 module.scopeProperties = scopeToProperties
Jiakai Zhang9c4dc192023-02-09 00:09:24 +08002445 module.AddProperties(&module.properties, allScopeProperties, &module.importDexpreoptProperties)
Colin Cross79c7c262019-04-17 11:11:46 -07002446
Paul Duffinc3091c82020-05-08 14:16:20 +01002447 // Initialize information common between source and prebuilt.
Paul Duffin71b33cc2021-06-23 11:39:47 +01002448 module.initCommon(module)
Paul Duffinc3091c82020-05-08 14:16:20 +01002449
Paul Duffin0bdcb272020-02-06 15:24:57 +00002450 android.InitPrebuiltModule(module, &[]string{""})
Paul Duffindd46f712020-02-10 13:37:10 +00002451 android.InitApexModule(module)
Colin Cross79c7c262019-04-17 11:11:46 -07002452 InitJavaModule(module, android.HostAndDeviceSupported)
2453
Paul Duffin1b1e8062020-05-08 13:44:43 +01002454 module.SetDefaultableHook(func(mctx android.DefaultableHookContext) {
2455 if module.initCommonAfterDefaultsApplied(mctx) {
2456 module.createInternalModules(mctx)
2457 }
2458 })
Colin Cross79c7c262019-04-17 11:11:46 -07002459 return module
2460}
2461
Paul Duffin630b11e2021-07-15 13:35:26 +01002462var _ PermittedPackagesForUpdatableBootJars = (*SdkLibraryImport)(nil)
2463
2464func (module *SdkLibraryImport) PermittedPackagesForUpdatableBootJars() []string {
2465 return module.properties.Permitted_packages
2466}
2467
Paul Duffineedc5d52020-06-12 17:46:39 +01002468func (module *SdkLibraryImport) Prebuilt() *android.Prebuilt {
Colin Cross79c7c262019-04-17 11:11:46 -07002469 return &module.prebuilt
2470}
2471
Paul Duffineedc5d52020-06-12 17:46:39 +01002472func (module *SdkLibraryImport) Name() string {
Colin Cross79c7c262019-04-17 11:11:46 -07002473 return module.prebuilt.Name(module.ModuleBase.Name())
2474}
2475
Paul Duffineedc5d52020-06-12 17:46:39 +01002476func (module *SdkLibraryImport) createInternalModules(mctx android.DefaultableHookContext) {
Colin Cross79c7c262019-04-17 11:11:46 -07002477
Paul Duffin50061512020-01-21 16:31:05 +00002478 // If the build is configured to use prebuilts then force this to be preferred.
Jeongik Cha816a23a2020-07-08 01:09:23 +09002479 if mctx.Config().AlwaysUsePrebuiltSdks() {
Paul Duffin50061512020-01-21 16:31:05 +00002480 module.prebuilt.ForcePrefer()
2481 }
2482
Paul Duffin46a26a82020-04-07 19:27:04 +01002483 for apiScope, scopeProperties := range module.scopeProperties {
Paul Duffin56d44902020-01-31 13:36:25 +00002484 if len(scopeProperties.Jars) == 0 {
2485 continue
2486 }
2487
Paul Duffinbbb546b2020-04-09 00:07:11 +01002488 module.createJavaImportForStubs(mctx, apiScope, scopeProperties)
Paul Duffin3d1248c2020-04-09 00:10:17 +01002489
Paul Duffin0f8faff2020-05-20 16:18:00 +01002490 if len(scopeProperties.Stub_srcs) > 0 {
2491 module.createPrebuiltStubsSources(mctx, apiScope, scopeProperties)
2492 }
Jihoon Kang71c86832023-09-13 01:01:53 +00002493
2494 if scopeProperties.Current_api != nil {
2495 module.createPrebuiltApiContribution(mctx, apiScope, scopeProperties)
2496 }
Paul Duffin56d44902020-01-31 13:36:25 +00002497 }
Colin Cross79c7c262019-04-17 11:11:46 -07002498
2499 javaSdkLibraries := javaSdkLibraries(mctx.Config())
2500 javaSdkLibrariesLock.Lock()
2501 defer javaSdkLibrariesLock.Unlock()
2502 *javaSdkLibraries = append(*javaSdkLibraries, module.BaseModuleName())
2503}
2504
Paul Duffineedc5d52020-06-12 17:46:39 +01002505func (module *SdkLibraryImport) createJavaImportForStubs(mctx android.DefaultableHookContext, apiScope *apiScope, scopeProperties *sdkLibraryScopeProperties) {
Paul Duffinbbb546b2020-04-09 00:07:11 +01002506 // Creates a java import for the jar with ".stubs" suffix
2507 props := struct {
Paul Duffin1dbe3ca2020-05-16 09:57:59 +01002508 Name *string
2509 Sdk_version *string
2510 Libs []string
2511 Jars []string
Paul Duffin1267d872021-04-16 17:21:36 +01002512 Compile_dex *bool
Paul Duffinbf4de042022-09-27 12:41:52 +01002513
2514 android.UserSuppliedPrebuiltProperties
Paul Duffinbbb546b2020-04-09 00:07:11 +01002515 }{}
Paul Duffinc3091c82020-05-08 14:16:20 +01002516 props.Name = proptools.StringPtr(module.stubsLibraryModuleName(apiScope))
Paul Duffinbbb546b2020-04-09 00:07:11 +01002517 props.Sdk_version = scopeProperties.Sdk_version
2518 // Prepend any of the libs from the legacy public properties to the libs for each of the
2519 // scopes to avoid having to duplicate them in each scope.
2520 props.Libs = append(module.properties.Libs, scopeProperties.Libs...)
2521 props.Jars = scopeProperties.Jars
Paul Duffin1dbe3ca2020-05-16 09:57:59 +01002522
Paul Duffin38b57852020-05-13 16:08:09 +01002523 // The imports are preferred if the java_sdk_library_import is preferred.
Paul Duffinbf4de042022-09-27 12:41:52 +01002524 props.CopyUserSuppliedPropertiesFromPrebuilt(&module.prebuilt)
Paul Duffin859fe962020-05-15 10:20:31 +01002525
Paul Duffin1267d872021-04-16 17:21:36 +01002526 // The imports need to be compiled to dex if the java_sdk_library_import requests it.
Paul Duffinf4600f62021-05-13 22:34:45 +01002527 compileDex := module.properties.Compile_dex
2528 if module.stubLibrariesCompiledForDex() {
2529 compileDex = proptools.BoolPtr(true)
2530 }
2531 props.Compile_dex = compileDex
Paul Duffin1267d872021-04-16 17:21:36 +01002532
Paul Duffin859fe962020-05-15 10:20:31 +01002533 mctx.CreateModule(ImportFactory, &props, module.sdkComponentPropertiesForChildLibrary())
Paul Duffinbbb546b2020-04-09 00:07:11 +01002534}
2535
Paul Duffineedc5d52020-06-12 17:46:39 +01002536func (module *SdkLibraryImport) createPrebuiltStubsSources(mctx android.DefaultableHookContext, apiScope *apiScope, scopeProperties *sdkLibraryScopeProperties) {
Paul Duffin3d1248c2020-04-09 00:10:17 +01002537 props := struct {
Paul Duffinbf4de042022-09-27 12:41:52 +01002538 Name *string
2539 Srcs []string
2540
2541 android.UserSuppliedPrebuiltProperties
Paul Duffin3d1248c2020-04-09 00:10:17 +01002542 }{}
Paul Duffinc3091c82020-05-08 14:16:20 +01002543 props.Name = proptools.StringPtr(module.stubsSourceModuleName(apiScope))
Paul Duffin3d1248c2020-04-09 00:10:17 +01002544 props.Srcs = scopeProperties.Stub_srcs
Paul Duffin38b57852020-05-13 16:08:09 +01002545
2546 // The stubs source is preferred if the java_sdk_library_import is preferred.
Paul Duffinbf4de042022-09-27 12:41:52 +01002547 props.CopyUserSuppliedPropertiesFromPrebuilt(&module.prebuilt)
2548
2549 mctx.CreateModule(PrebuiltStubsSourcesFactory, &props)
Paul Duffin3d1248c2020-04-09 00:10:17 +01002550}
2551
Jihoon Kang71c86832023-09-13 01:01:53 +00002552func (module *SdkLibraryImport) createPrebuiltApiContribution(mctx android.DefaultableHookContext, apiScope *apiScope, scopeProperties *sdkLibraryScopeProperties) {
2553 api_file := scopeProperties.Current_api
2554 api_surface := &apiScope.name
2555
2556 props := struct {
2557 Name *string
2558 Api_surface *string
2559 Api_file *string
2560 Visibility []string
2561 }{}
2562
2563 props.Name = proptools.StringPtr(module.stubsSourceModuleName(apiScope) + ".api.contribution")
2564 props.Api_surface = api_surface
2565 props.Api_file = api_file
2566 props.Visibility = []string{"//visibility:override", "//visibility:public"}
2567
2568 mctx.CreateModule(ApiContributionImportFactory, &props)
2569}
2570
Paul Duffin44f1d842020-06-26 20:17:02 +01002571// Add the dependencies on the child module in the component deps mutator so that it
2572// creates references to the prebuilt and not the source modules.
2573func (module *SdkLibraryImport) ComponentDepsMutator(ctx android.BottomUpMutatorContext) {
Paul Duffin46a26a82020-04-07 19:27:04 +01002574 for apiScope, scopeProperties := range module.scopeProperties {
Paul Duffin56d44902020-01-31 13:36:25 +00002575 if len(scopeProperties.Jars) == 0 {
2576 continue
2577 }
2578
2579 // Add dependencies to the prebuilt stubs library
Paul Duffin864116c2021-04-02 10:24:13 +01002580 ctx.AddVariationDependencies(nil, apiScope.stubsTag, android.PrebuiltNameFromSource(module.stubsLibraryModuleName(apiScope)))
Paul Duffin0f8faff2020-05-20 16:18:00 +01002581
2582 if len(scopeProperties.Stub_srcs) > 0 {
2583 // Add dependencies to the prebuilt stubs source library
Paul Duffin864116c2021-04-02 10:24:13 +01002584 ctx.AddVariationDependencies(nil, apiScope.stubsSourceTag, android.PrebuiltNameFromSource(module.stubsSourceModuleName(apiScope)))
Paul Duffin0f8faff2020-05-20 16:18:00 +01002585 }
Paul Duffin56d44902020-01-31 13:36:25 +00002586 }
Paul Duffin44f1d842020-06-26 20:17:02 +01002587}
2588
2589// Add other dependencies as normal.
2590func (module *SdkLibraryImport) DepsMutator(ctx android.BottomUpMutatorContext) {
Paul Duffineedc5d52020-06-12 17:46:39 +01002591
2592 implName := module.implLibraryModuleName()
2593 if ctx.OtherModuleExists(implName) {
2594 ctx.AddVariationDependencies(nil, implLibraryTag, implName)
2595
2596 xmlPermissionsModuleName := module.xmlPermissionsModuleName()
2597 if module.sharedLibrary() && ctx.OtherModuleExists(xmlPermissionsModuleName) {
2598 // Add dependency to the rule for generating the xml permissions file
2599 ctx.AddDependency(module, xmlPermissionsFileTag, xmlPermissionsModuleName)
2600 }
2601 }
Colin Cross79c7c262019-04-17 11:11:46 -07002602}
2603
Jiakai Zhang204356f2021-09-09 08:12:46 +00002604func (module *SdkLibraryImport) AndroidMkEntries() []android.AndroidMkEntries {
2605 // For an SDK library imported from a prebuilt APEX, we don't need a Make module for itself, as we
2606 // don't need to install it. However, we need to add its dexpreopt outputs as sub-modules, if it
2607 // is preopted.
2608 dexpreoptEntries := module.dexpreopter.AndroidMkEntriesForApex()
2609 return append(dexpreoptEntries, android.AndroidMkEntries{Disabled: true})
2610}
2611
Jiyong Park45bf82e2020-12-15 22:29:02 +09002612var _ android.ApexModule = (*SdkLibraryImport)(nil)
2613
2614// Implements android.ApexModule
Paul Duffineedc5d52020-06-12 17:46:39 +01002615func (module *SdkLibraryImport) DepIsInSameApex(mctx android.BaseModuleContext, dep android.Module) bool {
2616 depTag := mctx.OtherModuleDependencyTag(dep)
2617 if depTag == xmlPermissionsFileTag {
2618 return true
2619 }
2620
2621 // None of the other dependencies of the java_sdk_library_import are in the same apex
2622 // as the one that references this module.
2623 return false
2624}
2625
Jiyong Park45bf82e2020-12-15 22:29:02 +09002626// Implements android.ApexModule
Dan Albertc8060532020-07-22 22:32:17 -07002627func (module *SdkLibraryImport) ShouldSupportSdkVersion(ctx android.BaseModuleContext,
2628 sdkVersion android.ApiLevel) error {
Jooyung Han749dc692020-04-15 11:03:39 +09002629 // we don't check prebuilt modules for sdk_version
2630 return nil
2631}
2632
Paul Duffinea8f8082021-06-24 13:25:57 +01002633// Implements android.ApexModule
2634func (module *SdkLibraryImport) UniqueApexVariations() bool {
2635 return module.uniqueApexVariations()
2636}
2637
Paul Duffin09817d62022-04-28 17:45:11 +01002638// MinSdkVersion - Implements hiddenAPIModule
Spandan Das8c9ae7e2023-03-03 21:20:36 +00002639func (module *SdkLibraryImport) MinSdkVersion(ctx android.EarlyModuleContext) android.ApiLevel {
2640 return android.NoneApiLevel
Paul Duffin09817d62022-04-28 17:45:11 +01002641}
2642
2643var _ hiddenAPIModule = (*SdkLibraryImport)(nil)
2644
Paul Duffineedc5d52020-06-12 17:46:39 +01002645func (module *SdkLibraryImport) OutputFiles(tag string) (android.Paths, error) {
Paul Duffin1e940d52022-04-29 14:21:25 +01002646 paths, err := module.commonOutputFiles(tag)
2647 if paths != nil || err != nil {
2648 return paths, err
2649 }
2650 if module.implLibraryModule != nil {
2651 return module.implLibraryModule.OutputFiles(tag)
2652 } else {
2653 return nil, nil
2654 }
Paul Duffin46dc45a2020-05-14 15:39:10 +01002655}
2656
Paul Duffineedc5d52020-06-12 17:46:39 +01002657func (module *SdkLibraryImport) GenerateAndroidBuildActions(ctx android.ModuleContext) {
Paul Duffina2ae7e02020-09-11 11:55:00 +01002658 module.generateCommonBuildActions(ctx)
2659
Jeongik Chad5fe8782021-07-08 01:13:11 +09002660 // Assume that source module(sdk_library) is installed in /<sdk_library partition>/framework
2661 module.installFile = android.PathForModuleInstall(ctx, "framework", module.Stem()+".jar")
2662
Paul Duffin0f8faff2020-05-20 16:18:00 +01002663 // Record the paths to the prebuilt stubs library and stubs source.
Colin Cross79c7c262019-04-17 11:11:46 -07002664 ctx.VisitDirectDeps(func(to android.Module) {
2665 tag := ctx.OtherModuleDependencyTag(to)
2666
Paul Duffin0f8faff2020-05-20 16:18:00 +01002667 // Extract information from any of the scope specific dependencies.
2668 if scopeTag, ok := tag.(scopeDependencyTag); ok {
2669 apiScope := scopeTag.apiScope
2670 scopePaths := module.getScopePathsCreateIfNeeded(apiScope)
2671
2672 // Extract information from the dependency. The exact information extracted
2673 // is determined by the nature of the dependency which is determined by the tag.
2674 scopeTag.extractDepInfo(ctx, to, scopePaths)
Paul Duffineedc5d52020-06-12 17:46:39 +01002675 } else if tag == implLibraryTag {
2676 if implLibrary, ok := to.(*Library); ok {
2677 module.implLibraryModule = implLibrary
2678 } else {
2679 ctx.ModuleErrorf("implementation library must be of type *java.Library but was %T", to)
2680 }
2681 } else if tag == xmlPermissionsFileTag {
2682 if xmlPermissionsFileModule, ok := to.(*sdkLibraryXml); ok {
2683 module.xmlPermissionsFileModule = xmlPermissionsFileModule
2684 } else {
2685 ctx.ModuleErrorf("xml permissions file module must be of type *sdkLibraryXml but was %T", to)
2686 }
Colin Cross79c7c262019-04-17 11:11:46 -07002687 }
2688 })
Paul Duffin0f8faff2020-05-20 16:18:00 +01002689
2690 // Populate the scope paths with information from the properties.
2691 for apiScope, scopeProperties := range module.scopeProperties {
2692 if len(scopeProperties.Jars) == 0 {
2693 continue
2694 }
2695
2696 paths := module.getScopePathsCreateIfNeeded(apiScope)
Anton Hanssond78eb762021-09-21 15:25:12 +01002697 paths.annotationsZip = android.OptionalPathForModuleSrc(ctx, scopeProperties.Annotations)
Paul Duffin0f8faff2020-05-20 16:18:00 +01002698 paths.currentApiFilePath = android.OptionalPathForModuleSrc(ctx, scopeProperties.Current_api)
2699 paths.removedApiFilePath = android.OptionalPathForModuleSrc(ctx, scopeProperties.Removed_api)
2700 }
Paul Duffin39853512021-02-26 11:09:39 +00002701
2702 if ctx.Device() {
2703 // If this is a variant created for a prebuilt_apex then use the dex implementation jar
2704 // obtained from the associated deapexer module.
2705 ai := ctx.Provider(android.ApexInfoProvider).(android.ApexInfo)
2706 if ai.ForPrebuiltApex {
Paul Duffin39853512021-02-26 11:09:39 +00002707 // Get the path of the dex implementation jar from the `deapexer` module.
Martin Stjernholm44825602021-09-17 01:44:12 +01002708 di := android.FindDeapexerProviderForModule(ctx)
2709 if di == nil {
2710 return // An error has been reported by FindDeapexerProviderForModule.
2711 }
Jiakai Zhang81e46812023-02-08 21:56:07 +08002712 dexJarFileApexRootRelative := apexRootRelativePathToJavaLib(module.BaseModuleName())
2713 if dexOutputPath := di.PrebuiltExportPath(dexJarFileApexRootRelative); dexOutputPath != nil {
Martin Stjernholm8be1e6d2021-09-15 03:34:04 +01002714 dexJarFile := makeDexJarPathFromPath(dexOutputPath)
2715 module.dexJarFile = dexJarFile
Jiakai Zhang204356f2021-09-09 08:12:46 +00002716 installPath := android.PathForModuleInPartitionInstall(
Jiakai Zhang81e46812023-02-08 21:56:07 +08002717 ctx, "apex", ai.ApexVariationName, dexJarFileApexRootRelative)
Jiakai Zhang204356f2021-09-09 08:12:46 +00002718 module.installFile = installPath
Martin Stjernholm8be1e6d2021-09-15 03:34:04 +01002719 module.initHiddenAPI(ctx, dexJarFile, module.findScopePaths(apiScopePublic).stubsImplPath[0], nil)
Jiakai Zhang204356f2021-09-09 08:12:46 +00002720
Jiakai Zhang204356f2021-09-09 08:12:46 +00002721 module.dexpreopter.installPath = module.dexpreopter.getInstallPath(ctx, installPath)
2722 module.dexpreopter.isSDKLibrary = true
2723 module.dexpreopter.uncompressedDex = shouldUncompressDex(ctx, &module.dexpreopter)
Jiakai Zhang81e46812023-02-08 21:56:07 +08002724
2725 if profilePath := di.PrebuiltExportPath(dexJarFileApexRootRelative + ".prof"); profilePath != nil {
2726 module.dexpreopter.inputProfilePathOnHost = profilePath
2727 }
2728
2729 // Dexpreopting.
Jiakai Zhang204356f2021-09-09 08:12:46 +00002730 module.dexpreopt(ctx, dexOutputPath)
Paul Duffin39853512021-02-26 11:09:39 +00002731 } else {
2732 // This should never happen as a variant for a prebuilt_apex is only created if the
2733 // prebuilt_apex has been configured to export the java library dex file.
Martin Stjernholm44825602021-09-17 01:44:12 +01002734 ctx.ModuleErrorf("internal error: no dex implementation jar available from prebuilt APEX %s", di.ApexModuleName())
Paul Duffin39853512021-02-26 11:09:39 +00002735 }
2736 }
2737 }
Colin Cross79c7c262019-04-17 11:11:46 -07002738}
2739
Jiyong Parkf1691d22021-03-29 20:11:58 +09002740func (module *SdkLibraryImport) sdkJars(ctx android.BaseModuleContext, sdkVersion android.SdkSpec, headerJars bool) android.Paths {
Paul Duffineedc5d52020-06-12 17:46:39 +01002741
2742 // For consistency with SdkLibrary make the implementation jar available to libraries that
2743 // are within the same APEX.
2744 implLibraryModule := module.implLibraryModule
Colin Cross56a83212020-09-15 18:30:11 -07002745 if implLibraryModule != nil && withinSameApexesAs(ctx, module) {
Paul Duffineedc5d52020-06-12 17:46:39 +01002746 if headerJars {
2747 return implLibraryModule.HeaderJars()
2748 } else {
2749 return implLibraryModule.ImplementationJars()
2750 }
2751 }
2752
Paul Duffin23970f42020-05-20 14:20:02 +01002753 return module.selectHeaderJarsForSdkVersion(ctx, sdkVersion)
Paul Duffin56d44902020-01-31 13:36:25 +00002754}
2755
Colin Cross79c7c262019-04-17 11:11:46 -07002756// to satisfy SdkLibraryDependency interface
Jiyong Parkf1691d22021-03-29 20:11:58 +09002757func (module *SdkLibraryImport) SdkHeaderJars(ctx android.BaseModuleContext, sdkVersion android.SdkSpec) android.Paths {
Colin Cross79c7c262019-04-17 11:11:46 -07002758 // This module is just a wrapper for the prebuilt stubs.
Paul Duffineedc5d52020-06-12 17:46:39 +01002759 return module.sdkJars(ctx, sdkVersion, true)
Colin Cross79c7c262019-04-17 11:11:46 -07002760}
2761
2762// to satisfy SdkLibraryDependency interface
Jiyong Parkf1691d22021-03-29 20:11:58 +09002763func (module *SdkLibraryImport) SdkImplementationJars(ctx android.BaseModuleContext, sdkVersion android.SdkSpec) android.Paths {
Colin Cross79c7c262019-04-17 11:11:46 -07002764 // This module is just a wrapper for the stubs.
Paul Duffineedc5d52020-06-12 17:46:39 +01002765 return module.sdkJars(ctx, sdkVersion, false)
2766}
2767
Ulya Trafimovichdbf31662020-12-17 12:07:54 +00002768// to satisfy UsesLibraryDependency interface
Martin Stjernholm8be1e6d2021-09-15 03:34:04 +01002769func (module *SdkLibraryImport) DexJarBuildPath() OptionalDexJarPath {
Paul Duffin39853512021-02-26 11:09:39 +00002770 // The dex implementation jar extracted from the .apex file should be used in preference to the
2771 // source.
Martin Stjernholm8be1e6d2021-09-15 03:34:04 +01002772 if module.dexJarFile.IsSet() {
Paul Duffin39853512021-02-26 11:09:39 +00002773 return module.dexJarFile
2774 }
Paul Duffineedc5d52020-06-12 17:46:39 +01002775 if module.implLibraryModule == nil {
Martin Stjernholm8be1e6d2021-09-15 03:34:04 +01002776 return makeUnsetDexJarPath()
Paul Duffineedc5d52020-06-12 17:46:39 +01002777 } else {
2778 return module.implLibraryModule.DexJarBuildPath()
2779 }
2780}
2781
Ulya Trafimovichdbf31662020-12-17 12:07:54 +00002782// to satisfy UsesLibraryDependency interface
Ulya Trafimovich31e444e2020-08-14 17:32:16 +01002783func (module *SdkLibraryImport) DexJarInstallPath() android.Path {
Jeongik Chad5fe8782021-07-08 01:13:11 +09002784 return module.installFile
Ulya Trafimovich31e444e2020-08-14 17:32:16 +01002785}
2786
Ulya Trafimovichdbf31662020-12-17 12:07:54 +00002787// to satisfy UsesLibraryDependency interface
2788func (module *SdkLibraryImport) ClassLoaderContexts() dexpreopt.ClassLoaderContextMap {
2789 return nil
2790}
2791
Paul Duffineedc5d52020-06-12 17:46:39 +01002792// to satisfy apex.javaDependency interface
2793func (module *SdkLibraryImport) JacocoReportClassesFile() android.Path {
2794 if module.implLibraryModule == nil {
2795 return nil
2796 } else {
2797 return module.implLibraryModule.JacocoReportClassesFile()
2798 }
2799}
2800
2801// to satisfy apex.javaDependency interface
Colin Cross08dca382020-07-21 20:31:17 -07002802func (module *SdkLibraryImport) LintDepSets() LintDepSets {
2803 if module.implLibraryModule == nil {
2804 return LintDepSets{}
2805 } else {
2806 return module.implLibraryModule.LintDepSets()
2807 }
2808}
2809
Spandan Das17854f52022-01-14 21:19:14 +00002810func (module *SdkLibraryImport) GetStrictUpdatabilityLinting() bool {
Jaewoong Jung476b9d62021-05-10 15:30:00 -07002811 if module.implLibraryModule == nil {
2812 return false
2813 } else {
Spandan Das17854f52022-01-14 21:19:14 +00002814 return module.implLibraryModule.GetStrictUpdatabilityLinting()
Jaewoong Jung476b9d62021-05-10 15:30:00 -07002815 }
2816}
2817
Spandan Das17854f52022-01-14 21:19:14 +00002818func (module *SdkLibraryImport) SetStrictUpdatabilityLinting(strictLinting bool) {
Jaewoong Jung476b9d62021-05-10 15:30:00 -07002819 if module.implLibraryModule != nil {
Spandan Das17854f52022-01-14 21:19:14 +00002820 module.implLibraryModule.SetStrictUpdatabilityLinting(strictLinting)
Jaewoong Jung476b9d62021-05-10 15:30:00 -07002821 }
2822}
2823
Colin Cross08dca382020-07-21 20:31:17 -07002824// to satisfy apex.javaDependency interface
Paul Duffineedc5d52020-06-12 17:46:39 +01002825func (module *SdkLibraryImport) Stem() string {
2826 return module.BaseModuleName()
Colin Cross79c7c262019-04-17 11:11:46 -07002827}
Jiyong Parke3833882020-02-17 17:28:10 +09002828
Paul Duffin44b481b2020-06-17 16:59:43 +01002829var _ ApexDependency = (*SdkLibraryImport)(nil)
2830
2831// to satisfy java.ApexDependency interface
2832func (module *SdkLibraryImport) HeaderJars() android.Paths {
2833 if module.implLibraryModule == nil {
2834 return nil
2835 } else {
2836 return module.implLibraryModule.HeaderJars()
2837 }
2838}
2839
2840// to satisfy java.ApexDependency interface
2841func (module *SdkLibraryImport) ImplementationAndResourcesJars() android.Paths {
2842 if module.implLibraryModule == nil {
2843 return nil
2844 } else {
2845 return module.implLibraryModule.ImplementationAndResourcesJars()
2846 }
2847}
2848
Jiakai Zhang204356f2021-09-09 08:12:46 +00002849// to satisfy java.DexpreopterInterface interface
2850func (module *SdkLibraryImport) IsInstallable() bool {
2851 return true
2852}
2853
Paul Duffinfef55002021-06-17 14:56:05 +01002854var _ android.RequiredFilesFromPrebuiltApex = (*SdkLibraryImport)(nil)
2855
Paul Duffinb4bbf2c2021-06-17 15:59:07 +01002856func (module *SdkLibraryImport) RequiredFilesFromPrebuiltApex(ctx android.BaseModuleContext) []string {
Paul Duffinfef55002021-06-17 14:56:05 +01002857 name := module.BaseModuleName()
Jiakai Zhang81e46812023-02-08 21:56:07 +08002858 return requiredFilesFromPrebuiltApexForImport(name, &module.dexpreopter)
Paul Duffinfef55002021-06-17 14:56:05 +01002859}
2860
Jiyong Parke3833882020-02-17 17:28:10 +09002861// java_sdk_library_xml
Jiyong Parke3833882020-02-17 17:28:10 +09002862type sdkLibraryXml struct {
2863 android.ModuleBase
2864 android.DefaultableModuleBase
2865 android.ApexModuleBase
2866
2867 properties sdkLibraryXmlProperties
2868
2869 outputFilePath android.OutputPath
2870 installDirPath android.InstallPath
Colin Cross56a83212020-09-15 18:30:11 -07002871
2872 hideApexVariantFromMake bool
Jiyong Parke3833882020-02-17 17:28:10 +09002873}
2874
2875type sdkLibraryXmlProperties struct {
2876 // canonical name of the lib
2877 Lib_name *string
Pedro Loureiro9956e5e2021-09-07 17:21:59 +00002878
2879 // Signals that this shared library is part of the bootclasspath starting
2880 // on the version indicated in this attribute.
2881 //
2882 // This will make platforms at this level and above to ignore
2883 // <uses-library> tags with this library name because the library is already
2884 // available
2885 On_bootclasspath_since *string
2886
2887 // Signals that this shared library was part of the bootclasspath before
2888 // (but not including) the version indicated in this attribute.
2889 //
2890 // The system will automatically add a <uses-library> tag with this library to
2891 // apps that target any SDK less than the version indicated in this attribute.
2892 On_bootclasspath_before *string
2893
2894 // Indicates that PackageManager should ignore this shared library if the
2895 // platform is below the version indicated in this attribute.
2896 //
2897 // This means that the device won't recognise this library as installed.
2898 Min_device_sdk *string
2899
2900 // Indicates that PackageManager should ignore this shared library if the
2901 // platform is above the version indicated in this attribute.
2902 //
2903 // This means that the device won't recognise this library as installed.
2904 Max_device_sdk *string
Pedro Loureiroc3621422021-09-28 15:40:23 +00002905
2906 // The SdkLibrary's min api level as a string
2907 //
2908 // This value comes from the ApiLevel of the MinSdkVersion property.
2909 Sdk_library_min_api_level *string
Jiyong Parke3833882020-02-17 17:28:10 +09002910}
2911
2912// java_sdk_library_xml builds the permission xml file for a java_sdk_library.
2913// Not to be used directly by users. java_sdk_library internally uses this.
2914func sdkLibraryXmlFactory() android.Module {
2915 module := &sdkLibraryXml{}
2916
2917 module.AddProperties(&module.properties)
2918
2919 android.InitApexModule(module)
2920 android.InitAndroidArchModule(module, android.DeviceSupported, android.MultilibCommon)
2921
2922 return module
2923}
2924
Colin Crossaede88c2020-08-11 12:17:01 -07002925func (module *sdkLibraryXml) UniqueApexVariations() bool {
2926 // sdkLibraryXml needs a unique variation per APEX because the generated XML file contains the path to the
2927 // mounted APEX, which contains the name of the APEX.
2928 return true
2929}
2930
Jiyong Parke3833882020-02-17 17:28:10 +09002931// from android.PrebuiltEtcModule
Jooyung Han0703fd82020-08-26 22:11:53 +09002932func (module *sdkLibraryXml) BaseDir() string {
2933 return "etc"
2934}
2935
2936// from android.PrebuiltEtcModule
Jiyong Parke3833882020-02-17 17:28:10 +09002937func (module *sdkLibraryXml) SubDir() string {
2938 return "permissions"
2939}
2940
2941// from android.PrebuiltEtcModule
2942func (module *sdkLibraryXml) OutputFile() android.OutputPath {
2943 return module.outputFilePath
2944}
2945
2946// from android.ApexModule
2947func (module *sdkLibraryXml) AvailableFor(what string) bool {
2948 return true
2949}
2950
2951func (module *sdkLibraryXml) DepsMutator(ctx android.BottomUpMutatorContext) {
2952 // do nothing
2953}
2954
Jiyong Park45bf82e2020-12-15 22:29:02 +09002955var _ android.ApexModule = (*sdkLibraryXml)(nil)
2956
2957// Implements android.ApexModule
Dan Albertc8060532020-07-22 22:32:17 -07002958func (module *sdkLibraryXml) ShouldSupportSdkVersion(ctx android.BaseModuleContext,
2959 sdkVersion android.ApiLevel) error {
Jooyung Han749dc692020-04-15 11:03:39 +09002960 // sdkLibraryXml doesn't need to be checked separately because java_sdk_library is checked
2961 return nil
2962}
2963
Jiyong Parke3833882020-02-17 17:28:10 +09002964// File path to the runtime implementation library
Colin Cross56a83212020-09-15 18:30:11 -07002965func (module *sdkLibraryXml) implPath(ctx android.ModuleContext) string {
Jiyong Parke3833882020-02-17 17:28:10 +09002966 implName := proptools.String(module.properties.Lib_name)
Colin Cross56a83212020-09-15 18:30:11 -07002967 if apexInfo := ctx.Provider(android.ApexInfoProvider).(android.ApexInfo); !apexInfo.IsForPlatform() {
Colin Crosse07f2312020-08-13 11:24:56 -07002968 // TODO(b/146468504): ApexVariationName() is only a soong module name, not apex name.
Jiyong Parke3833882020-02-17 17:28:10 +09002969 // In most cases, this works fine. But when apex_name is set or override_apex is used
2970 // this can be wrong.
Colin Cross56a83212020-09-15 18:30:11 -07002971 return fmt.Sprintf("/apex/%s/javalib/%s.jar", apexInfo.ApexVariationName, implName)
Jiyong Parke3833882020-02-17 17:28:10 +09002972 }
2973 partition := "system"
2974 if module.SocSpecific() {
2975 partition = "vendor"
2976 } else if module.DeviceSpecific() {
2977 partition = "odm"
2978 } else if module.ProductSpecific() {
2979 partition = "product"
2980 } else if module.SystemExtSpecific() {
2981 partition = "system_ext"
2982 }
2983 return "/" + partition + "/framework/" + implName + ".jar"
2984}
2985
Pedro Loureiro9956e5e2021-09-07 17:21:59 +00002986func formattedOptionalSdkLevelAttribute(ctx android.ModuleContext, attrName string, value *string) string {
2987 if value == nil {
2988 return ""
2989 }
2990 apiLevel, err := android.ApiLevelFromUser(ctx, *value)
2991 if err != nil {
Pedro Loureiroba6682f2021-10-29 09:32:32 +00002992 // attributes in bp files have underscores but in the xml have dashes.
2993 ctx.PropertyErrorf(strings.ReplaceAll(attrName, "-", "_"), err.Error())
Pedro Loureiro9956e5e2021-09-07 17:21:59 +00002994 return ""
2995 }
Pedro Loureirob638c622021-12-22 15:28:05 +00002996 if apiLevel.IsCurrent() {
2997 // passing "current" would always mean a future release, never the current (or the current in
2998 // progress) which means some conditions would never be triggered.
2999 ctx.PropertyErrorf(strings.ReplaceAll(attrName, "-", "_"),
3000 `"current" is not an allowed value for this attribute`)
3001 return ""
3002 }
Pedro Loureiro48991222022-06-17 20:01:21 +00003003 // "safeValue" is safe because it translates finalized codenames to a string
3004 // with their SDK int.
3005 safeValue := apiLevel.String()
3006 return formattedOptionalAttribute(attrName, &safeValue)
Pedro Loureiro9956e5e2021-09-07 17:21:59 +00003007}
3008
3009// formats an attribute for the xml permissions file if the value is not null
3010// returns empty string otherwise
3011func formattedOptionalAttribute(attrName string, value *string) string {
3012 if value == nil {
3013 return ""
3014 }
3015 return fmt.Sprintf(` %s=\"%s\"\n`, attrName, *value)
3016}
3017
3018func (module *sdkLibraryXml) permissionsContents(ctx android.ModuleContext) string {
3019 libName := proptools.String(module.properties.Lib_name)
3020 libNameAttr := formattedOptionalAttribute("name", &libName)
3021 filePath := module.implPath(ctx)
3022 filePathAttr := formattedOptionalAttribute("file", &filePath)
Pedro Loureiroba6682f2021-10-29 09:32:32 +00003023 implicitFromAttr := formattedOptionalSdkLevelAttribute(ctx, "on-bootclasspath-since", module.properties.On_bootclasspath_since)
3024 implicitUntilAttr := formattedOptionalSdkLevelAttribute(ctx, "on-bootclasspath-before", module.properties.On_bootclasspath_before)
3025 minSdkAttr := formattedOptionalSdkLevelAttribute(ctx, "min-device-sdk", module.properties.Min_device_sdk)
3026 maxSdkAttr := formattedOptionalSdkLevelAttribute(ctx, "max-device-sdk", module.properties.Max_device_sdk)
Pedro Loureiro196d3e62021-12-22 19:53:01 +00003027 // <library> is understood in all android versions whereas <apex-library> is only understood from API T (and ignored before that).
3028 // 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 +00003029 var libraryTag string
3030 if module.properties.Min_device_sdk != nil {
Pedro Loureiro196d3e62021-12-22 19:53:01 +00003031 libraryTag = ` <apex-library\n`
Pedro Loureiroc3621422021-09-28 15:40:23 +00003032 } else {
3033 libraryTag = ` <library\n`
3034 }
Pedro Loureiro9956e5e2021-09-07 17:21:59 +00003035
3036 return strings.Join([]string{
3037 `<?xml version=\"1.0\" encoding=\"utf-8\"?>\n`,
3038 `<!-- Copyright (C) 2018 The Android Open Source Project\n`,
3039 `\n`,
3040 ` Licensed under the Apache License, Version 2.0 (the \"License\");\n`,
3041 ` you may not use this file except in compliance with the License.\n`,
3042 ` You may obtain a copy of the License at\n`,
3043 `\n`,
3044 ` http://www.apache.org/licenses/LICENSE-2.0\n`,
3045 `\n`,
3046 ` Unless required by applicable law or agreed to in writing, software\n`,
3047 ` distributed under the License is distributed on an \"AS IS\" BASIS,\n`,
3048 ` WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\n`,
3049 ` See the License for the specific language governing permissions and\n`,
3050 ` limitations under the License.\n`,
3051 `-->\n`,
3052 `<permissions>\n`,
Pedro Loureiroc3621422021-09-28 15:40:23 +00003053 libraryTag,
Pedro Loureiro9956e5e2021-09-07 17:21:59 +00003054 libNameAttr,
3055 filePathAttr,
3056 implicitFromAttr,
3057 implicitUntilAttr,
3058 minSdkAttr,
3059 maxSdkAttr,
3060 ` />\n`,
3061 `</permissions>\n`}, "")
3062}
3063
Jiyong Parke3833882020-02-17 17:28:10 +09003064func (module *sdkLibraryXml) GenerateAndroidBuildActions(ctx android.ModuleContext) {
Colin Cross56a83212020-09-15 18:30:11 -07003065 module.hideApexVariantFromMake = !ctx.Provider(android.ApexInfoProvider).(android.ApexInfo).IsForPlatform()
3066
Jiyong Parke3833882020-02-17 17:28:10 +09003067 libName := proptools.String(module.properties.Lib_name)
Pedro Loureiroc3621422021-09-28 15:40:23 +00003068 module.selfValidate(ctx)
Pedro Loureiro9956e5e2021-09-07 17:21:59 +00003069 xmlContent := module.permissionsContents(ctx)
Jiyong Parke3833882020-02-17 17:28:10 +09003070
3071 module.outputFilePath = android.PathForModuleOut(ctx, libName+".xml").OutputPath
Colin Crossf1a035e2020-11-16 17:32:30 -08003072 rule := android.NewRuleBuilder(pctx, ctx)
Jiyong Parke3833882020-02-17 17:28:10 +09003073 rule.Command().
3074 Text("/bin/bash -c \"echo -e '" + xmlContent + "'\" > ").
3075 Output(module.outputFilePath)
3076
Colin Crossf1a035e2020-11-16 17:32:30 -08003077 rule.Build("java_sdk_xml", "Permission XML")
Jiyong Parke3833882020-02-17 17:28:10 +09003078
3079 module.installDirPath = android.PathForModuleInstall(ctx, "etc", module.SubDir())
3080}
3081
3082func (module *sdkLibraryXml) AndroidMkEntries() []android.AndroidMkEntries {
Colin Cross56a83212020-09-15 18:30:11 -07003083 if module.hideApexVariantFromMake {
satayev8f088b02021-12-06 11:40:46 +00003084 return []android.AndroidMkEntries{{
Jiyong Parke3833882020-02-17 17:28:10 +09003085 Disabled: true,
3086 }}
3087 }
3088
satayev8f088b02021-12-06 11:40:46 +00003089 return []android.AndroidMkEntries{{
Jiyong Parke3833882020-02-17 17:28:10 +09003090 Class: "ETC",
3091 OutputFile: android.OptionalPathForPath(module.outputFilePath),
3092 ExtraEntries: []android.AndroidMkExtraEntriesFunc{
Colin Crossaa255532020-07-03 13:18:24 -07003093 func(ctx android.AndroidMkExtraEntriesContext, entries *android.AndroidMkEntries) {
Jiyong Parke3833882020-02-17 17:28:10 +09003094 entries.SetString("LOCAL_MODULE_TAGS", "optional")
Colin Crossc68db4b2021-11-11 18:59:15 -08003095 entries.SetString("LOCAL_MODULE_PATH", module.installDirPath.String())
Jiyong Parke3833882020-02-17 17:28:10 +09003096 entries.SetString("LOCAL_INSTALLED_MODULE_STEM", module.outputFilePath.Base())
3097 },
3098 },
3099 }}
3100}
Paul Duffindd46f712020-02-10 13:37:10 +00003101
Pedro Loureiroc3621422021-09-28 15:40:23 +00003102func (module *sdkLibraryXml) selfValidate(ctx android.ModuleContext) {
3103 module.validateAtLeastTAttributes(ctx)
3104 module.validateMinAndMaxDeviceSdk(ctx)
3105 module.validateMinMaxDeviceSdkAndModuleMinSdk(ctx)
3106 module.validateOnBootclasspathBeforeRequirements(ctx)
3107}
3108
3109func (module *sdkLibraryXml) validateAtLeastTAttributes(ctx android.ModuleContext) {
3110 t := android.ApiLevelOrPanic(ctx, "Tiramisu")
3111 module.attrAtLeastT(ctx, t, module.properties.Min_device_sdk, "min_device_sdk")
3112 module.attrAtLeastT(ctx, t, module.properties.Max_device_sdk, "max_device_sdk")
3113 module.attrAtLeastT(ctx, t, module.properties.On_bootclasspath_before, "on_bootclasspath_before")
3114 module.attrAtLeastT(ctx, t, module.properties.On_bootclasspath_since, "on_bootclasspath_since")
3115}
3116
3117func (module *sdkLibraryXml) attrAtLeastT(ctx android.ModuleContext, t android.ApiLevel, attr *string, attrName string) {
3118 if attr != nil {
3119 if level, err := android.ApiLevelFromUser(ctx, *attr); err == nil {
3120 // we will inform the user of invalid inputs when we try to write the
3121 // permissions xml file so we don't need to do it here
3122 if t.GreaterThan(level) {
3123 ctx.PropertyErrorf(attrName, "Attribute value needs to be at least T")
3124 }
3125 }
3126 }
3127}
3128
3129func (module *sdkLibraryXml) validateMinAndMaxDeviceSdk(ctx android.ModuleContext) {
3130 if module.properties.Min_device_sdk != nil && module.properties.Max_device_sdk != nil {
3131 min, minErr := android.ApiLevelFromUser(ctx, *module.properties.Min_device_sdk)
3132 max, maxErr := android.ApiLevelFromUser(ctx, *module.properties.Max_device_sdk)
3133 if minErr == nil && maxErr == nil {
3134 // we will inform the user of invalid inputs when we try to write the
3135 // permissions xml file so we don't need to do it here
3136 if min.GreaterThan(max) {
3137 ctx.ModuleErrorf("min_device_sdk can't be greater than max_device_sdk")
3138 }
3139 }
3140 }
3141}
3142
3143func (module *sdkLibraryXml) validateMinMaxDeviceSdkAndModuleMinSdk(ctx android.ModuleContext) {
3144 moduleMinApi := android.ApiLevelOrPanic(ctx, *module.properties.Sdk_library_min_api_level)
3145 if module.properties.Min_device_sdk != nil {
3146 api, err := android.ApiLevelFromUser(ctx, *module.properties.Min_device_sdk)
3147 if err == nil {
3148 if moduleMinApi.GreaterThan(api) {
3149 ctx.PropertyErrorf("min_device_sdk", "Can't be less than module's min sdk (%s)", moduleMinApi)
3150 }
3151 }
3152 }
3153 if module.properties.Max_device_sdk != nil {
3154 api, err := android.ApiLevelFromUser(ctx, *module.properties.Max_device_sdk)
3155 if err == nil {
3156 if moduleMinApi.GreaterThan(api) {
3157 ctx.PropertyErrorf("max_device_sdk", "Can't be less than module's min sdk (%s)", moduleMinApi)
3158 }
3159 }
3160 }
3161}
3162
3163func (module *sdkLibraryXml) validateOnBootclasspathBeforeRequirements(ctx android.ModuleContext) {
3164 moduleMinApi := android.ApiLevelOrPanic(ctx, *module.properties.Sdk_library_min_api_level)
3165 if module.properties.On_bootclasspath_before != nil {
3166 t := android.ApiLevelOrPanic(ctx, "Tiramisu")
3167 // if we use the attribute, then we need to do this validation
3168 if moduleMinApi.LessThan(t) {
3169 // if minAPi is < T, then we need to have min_device_sdk (which only accepts T+)
3170 if module.properties.Min_device_sdk == nil {
3171 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")
3172 }
3173 }
3174 }
3175}
3176
Paul Duffindd46f712020-02-10 13:37:10 +00003177type sdkLibrarySdkMemberType struct {
3178 android.SdkMemberTypeBase
3179}
3180
Paul Duffin296701e2021-07-14 10:29:36 +01003181func (s *sdkLibrarySdkMemberType) AddDependencies(ctx android.SdkDependencyContext, dependencyTag blueprint.DependencyTag, names []string) {
3182 ctx.AddVariationDependencies(nil, dependencyTag, names...)
Paul Duffindd46f712020-02-10 13:37:10 +00003183}
3184
3185func (s *sdkLibrarySdkMemberType) IsInstance(module android.Module) bool {
3186 _, ok := module.(*SdkLibrary)
3187 return ok
3188}
3189
3190func (s *sdkLibrarySdkMemberType) AddPrebuiltModule(ctx android.SdkMemberContext, member android.SdkMember) android.BpModule {
3191 return ctx.SnapshotBuilder().AddPrebuiltModule(member, "java_sdk_library_import")
3192}
3193
3194func (s *sdkLibrarySdkMemberType) CreateVariantPropertiesStruct() android.SdkMemberProperties {
3195 return &sdkLibrarySdkMemberProperties{}
3196}
3197
Paul Duffin976b0e52021-04-27 23:20:26 +01003198var javaSdkLibrarySdkMemberType = &sdkLibrarySdkMemberType{
3199 android.SdkMemberTypeBase{
3200 PropertyName: "java_sdk_libs",
3201 SupportsSdk: true,
3202 },
3203}
3204
Paul Duffindd46f712020-02-10 13:37:10 +00003205type sdkLibrarySdkMemberProperties struct {
3206 android.SdkMemberPropertiesBase
3207
Paul Duffine8409952022-09-22 16:24:46 +01003208 // Stem name for files in the sdk snapshot.
3209 //
3210 // This is used to construct the path names of various sdk library files in the sdk snapshot to
3211 // make sure that they match the finalized versions of those files in prebuilts/sdk.
3212 //
3213 // This property is marked as keep so that it will be kept in all instances of this struct, will
3214 // not be cleared but will be copied to common structs. That is needed because this field is used
3215 // to construct many file names for other parts of this struct and so it needs to be present in
3216 // all structs. If it was not marked as keep then it would be cleared in some structs and so would
3217 // be unavailable for generating file names if there were other properties that were still set.
3218 Stem string `sdk:"keep"`
3219
Paul Duffindd46f712020-02-10 13:37:10 +00003220 // Scope to per scope properties.
Paul Duffin106a3a42022-01-27 16:39:06 +00003221 Scopes map[*apiScope]*scopeProperties
Paul Duffindd46f712020-02-10 13:37:10 +00003222
Paul Duffin3d1248c2020-04-09 00:10:17 +01003223 // The Java stubs source files.
3224 Stub_srcs []string
Paul Duffinf7a64332020-05-13 16:54:55 +01003225
3226 // The naming scheme.
3227 Naming_scheme *string
Paul Duffind7eb1c22020-05-26 20:57:10 +01003228
3229 // True if the java_sdk_library_import is for a shared library, false
3230 // otherwise.
3231 Shared_library *bool
Paul Duffina2ae7e02020-09-11 11:55:00 +01003232
Paul Duffin1267d872021-04-16 17:21:36 +01003233 // True if the stub imports should produce dex jars.
3234 Compile_dex *bool
3235
Paul Duffina2ae7e02020-09-11 11:55:00 +01003236 // The paths to the doctag files to add to the prebuilt.
3237 Doctag_paths android.Paths
Paul Duffin869de142021-07-15 14:14:41 +01003238
3239 Permitted_packages []string
Pedro Loureiro9956e5e2021-09-07 17:21:59 +00003240
3241 // Signals that this shared library is part of the bootclasspath starting
3242 // on the version indicated in this attribute.
3243 //
3244 // This will make platforms at this level and above to ignore
3245 // <uses-library> tags with this library name because the library is already
3246 // available
3247 On_bootclasspath_since *string
3248
3249 // Signals that this shared library was part of the bootclasspath before
3250 // (but not including) the version indicated in this attribute.
3251 //
3252 // The system will automatically add a <uses-library> tag with this library to
3253 // apps that target any SDK less than the version indicated in this attribute.
3254 On_bootclasspath_before *string
3255
3256 // Indicates that PackageManager should ignore this shared library if the
3257 // platform is below the version indicated in this attribute.
3258 //
3259 // This means that the device won't recognise this library as installed.
3260 Min_device_sdk *string
3261
3262 // Indicates that PackageManager should ignore this shared library if the
3263 // platform is above the version indicated in this attribute.
3264 //
3265 // This means that the device won't recognise this library as installed.
3266 Max_device_sdk *string
Jiakai Zhang9c4dc192023-02-09 00:09:24 +08003267
3268 DexPreoptProfileGuided *bool `supported_build_releases:"UpsideDownCake+"`
Paul Duffindd46f712020-02-10 13:37:10 +00003269}
3270
3271type scopeProperties struct {
Paul Duffin1fd005d2020-04-09 01:08:11 +01003272 Jars android.Paths
3273 StubsSrcJar android.Path
3274 CurrentApiFile android.Path
3275 RemovedApiFile android.Path
Paul Duffine7babdb2022-02-10 13:06:54 +00003276 AnnotationsZip android.Path `supported_build_releases:"Tiramisu+"`
Paul Duffin1fd005d2020-04-09 01:08:11 +01003277 SdkVersion string
Paul Duffindd46f712020-02-10 13:37:10 +00003278}
3279
3280func (s *sdkLibrarySdkMemberProperties) PopulateFromVariant(ctx android.SdkMemberContext, variant android.Module) {
3281 sdk := variant.(*SdkLibrary)
3282
Paul Duffine8409952022-09-22 16:24:46 +01003283 // Copy the stem name for files in the sdk snapshot.
3284 s.Stem = sdk.distStem()
3285
Paul Duffin106a3a42022-01-27 16:39:06 +00003286 s.Scopes = make(map[*apiScope]*scopeProperties)
Paul Duffindd46f712020-02-10 13:37:10 +00003287 for _, apiScope := range allApiScopes {
Paul Duffin803a9562020-05-20 11:52:25 +01003288 paths := sdk.findScopePaths(apiScope)
3289 if paths == nil {
3290 continue
3291 }
3292
Paul Duffindd46f712020-02-10 13:37:10 +00003293 jars := paths.stubsImplPath
3294 if len(jars) > 0 {
3295 properties := scopeProperties{}
3296 properties.Jars = jars
Paul Duffin780c5f42020-05-12 15:52:55 +01003297 properties.SdkVersion = sdk.sdkVersionForStubsLibrary(ctx.SdkModuleContext(), apiScope)
Paul Duffin0f8faff2020-05-20 16:18:00 +01003298 properties.StubsSrcJar = paths.stubsSrcJar.Path()
Paul Duffin10269f12020-06-19 18:39:55 +01003299 if paths.currentApiFilePath.Valid() {
3300 properties.CurrentApiFile = paths.currentApiFilePath.Path()
3301 }
3302 if paths.removedApiFilePath.Valid() {
3303 properties.RemovedApiFile = paths.removedApiFilePath.Path()
3304 }
Anton Hanssond78eb762021-09-21 15:25:12 +01003305 // The annotations zip is only available for modules that set annotations_enabled: true.
3306 if paths.annotationsZip.Valid() {
3307 properties.AnnotationsZip = paths.annotationsZip.Path()
3308 }
Paul Duffin106a3a42022-01-27 16:39:06 +00003309 s.Scopes[apiScope] = &properties
Paul Duffindd46f712020-02-10 13:37:10 +00003310 }
3311 }
3312
Paul Duffindfa131e2020-05-15 20:37:11 +01003313 s.Naming_scheme = sdk.commonSdkLibraryProperties.Naming_scheme
Paul Duffind7eb1c22020-05-26 20:57:10 +01003314 s.Shared_library = proptools.BoolPtr(sdk.sharedLibrary())
Paul Duffin1267d872021-04-16 17:21:36 +01003315 s.Compile_dex = sdk.dexProperties.Compile_dex
Paul Duffina2ae7e02020-09-11 11:55:00 +01003316 s.Doctag_paths = sdk.doctagPaths
Paul Duffin869de142021-07-15 14:14:41 +01003317 s.Permitted_packages = sdk.PermittedPackagesForUpdatableBootJars()
Pedro Loureiro9956e5e2021-09-07 17:21:59 +00003318 s.On_bootclasspath_since = sdk.commonSdkLibraryProperties.On_bootclasspath_since
3319 s.On_bootclasspath_before = sdk.commonSdkLibraryProperties.On_bootclasspath_before
3320 s.Min_device_sdk = sdk.commonSdkLibraryProperties.Min_device_sdk
3321 s.Max_device_sdk = sdk.commonSdkLibraryProperties.Max_device_sdk
Jiakai Zhang9c4dc192023-02-09 00:09:24 +08003322
3323 if sdk.dexpreopter.dexpreoptProperties.Dex_preopt_result.Profile_guided {
3324 s.DexPreoptProfileGuided = proptools.BoolPtr(true)
3325 }
Paul Duffindd46f712020-02-10 13:37:10 +00003326}
3327
3328func (s *sdkLibrarySdkMemberProperties) AddToPropertySet(ctx android.SdkMemberContext, propertySet android.BpPropertySet) {
Paul Duffinf7a64332020-05-13 16:54:55 +01003329 if s.Naming_scheme != nil {
3330 propertySet.AddProperty("naming_scheme", proptools.String(s.Naming_scheme))
3331 }
Paul Duffind7eb1c22020-05-26 20:57:10 +01003332 if s.Shared_library != nil {
3333 propertySet.AddProperty("shared_library", *s.Shared_library)
3334 }
Paul Duffin1267d872021-04-16 17:21:36 +01003335 if s.Compile_dex != nil {
3336 propertySet.AddProperty("compile_dex", *s.Compile_dex)
3337 }
Paul Duffin869de142021-07-15 14:14:41 +01003338 if len(s.Permitted_packages) > 0 {
3339 propertySet.AddProperty("permitted_packages", s.Permitted_packages)
3340 }
Jiakai Zhang9c4dc192023-02-09 00:09:24 +08003341 dexPreoptSet := propertySet.AddPropertySet("dex_preopt")
3342 if s.DexPreoptProfileGuided != nil {
3343 dexPreoptSet.AddProperty("profile_guided", proptools.Bool(s.DexPreoptProfileGuided))
3344 }
Paul Duffinf7a64332020-05-13 16:54:55 +01003345
Paul Duffine8409952022-09-22 16:24:46 +01003346 stem := s.Stem
3347
Paul Duffindd46f712020-02-10 13:37:10 +00003348 for _, apiScope := range allApiScopes {
3349 if properties, ok := s.Scopes[apiScope]; ok {
Paul Duffin6b836ba2020-05-13 19:19:49 +01003350 scopeSet := propertySet.AddPropertySet(apiScope.propertyName)
Paul Duffindd46f712020-02-10 13:37:10 +00003351
Paul Duffin958806b2022-05-16 13:10:47 +00003352 scopeDir := apiScope.snapshotRelativeDir()
Paul Duffin3d1248c2020-04-09 00:10:17 +01003353
Paul Duffindd46f712020-02-10 13:37:10 +00003354 var jars []string
3355 for _, p := range properties.Jars {
Paul Duffine8409952022-09-22 16:24:46 +01003356 dest := filepath.Join(scopeDir, stem+"-stubs.jar")
Paul Duffindd46f712020-02-10 13:37:10 +00003357 ctx.SnapshotBuilder().CopyToSnapshot(p, dest)
3358 jars = append(jars, dest)
3359 }
3360 scopeSet.AddProperty("jars", jars)
3361
Paul Duffin22628d52021-05-12 23:13:22 +01003362 if ctx.SdkModuleContext().Config().IsEnvTrue("SOONG_SDK_SNAPSHOT_USE_SRCJAR") {
3363 // Copy the stubs source jar into the snapshot zip as is.
Paul Duffine8409952022-09-22 16:24:46 +01003364 srcJarSnapshotPath := filepath.Join(scopeDir, stem+".srcjar")
Paul Duffin22628d52021-05-12 23:13:22 +01003365 ctx.SnapshotBuilder().CopyToSnapshot(properties.StubsSrcJar, srcJarSnapshotPath)
3366 scopeSet.AddProperty("stub_srcs", []string{srcJarSnapshotPath})
3367 } else {
3368 // Merge the stubs source jar into the snapshot zip so that when it is unpacked
3369 // the source files are also unpacked.
Paul Duffine8409952022-09-22 16:24:46 +01003370 snapshotRelativeDir := filepath.Join(scopeDir, stem+"_stub_sources")
Paul Duffin22628d52021-05-12 23:13:22 +01003371 ctx.SnapshotBuilder().UnzipToSnapshot(properties.StubsSrcJar, snapshotRelativeDir)
3372 scopeSet.AddProperty("stub_srcs", []string{snapshotRelativeDir})
3373 }
Paul Duffin3d1248c2020-04-09 00:10:17 +01003374
Paul Duffin1fd005d2020-04-09 01:08:11 +01003375 if properties.CurrentApiFile != nil {
Paul Duffine8409952022-09-22 16:24:46 +01003376 currentApiSnapshotPath := apiScope.snapshotRelativeCurrentApiTxtPath(stem)
Paul Duffin1fd005d2020-04-09 01:08:11 +01003377 ctx.SnapshotBuilder().CopyToSnapshot(properties.CurrentApiFile, currentApiSnapshotPath)
3378 scopeSet.AddProperty("current_api", currentApiSnapshotPath)
3379 }
3380
3381 if properties.RemovedApiFile != nil {
Paul Duffine8409952022-09-22 16:24:46 +01003382 removedApiSnapshotPath := apiScope.snapshotRelativeRemovedApiTxtPath(stem)
Paul Duffin3dbf9fd2020-06-02 13:00:02 +01003383 ctx.SnapshotBuilder().CopyToSnapshot(properties.RemovedApiFile, removedApiSnapshotPath)
Paul Duffin1fd005d2020-04-09 01:08:11 +01003384 scopeSet.AddProperty("removed_api", removedApiSnapshotPath)
3385 }
3386
Anton Hanssond78eb762021-09-21 15:25:12 +01003387 if properties.AnnotationsZip != nil {
Paul Duffine8409952022-09-22 16:24:46 +01003388 annotationsSnapshotPath := filepath.Join(scopeDir, stem+"_annotations.zip")
Anton Hanssond78eb762021-09-21 15:25:12 +01003389 ctx.SnapshotBuilder().CopyToSnapshot(properties.AnnotationsZip, annotationsSnapshotPath)
3390 scopeSet.AddProperty("annotations", annotationsSnapshotPath)
3391 }
3392
Paul Duffindd46f712020-02-10 13:37:10 +00003393 if properties.SdkVersion != "" {
3394 scopeSet.AddProperty("sdk_version", properties.SdkVersion)
3395 }
3396 }
3397 }
3398
Paul Duffina2ae7e02020-09-11 11:55:00 +01003399 if len(s.Doctag_paths) > 0 {
3400 dests := []string{}
3401 for _, p := range s.Doctag_paths {
3402 dest := filepath.Join("doctags", p.Rel())
3403 ctx.SnapshotBuilder().CopyToSnapshot(p, dest)
3404 dests = append(dests, dest)
3405 }
3406 propertySet.AddProperty("doctag_files", dests)
3407 }
Paul Duffindd46f712020-02-10 13:37:10 +00003408}