blob: 6d8e2ebfb4414f193a6793994234e3207457c50c [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 Duffind1b3a922020-01-22 11:57:20 +000028 "github.com/google/blueprint"
Jiyong Parkc678ad32018-04-10 13:07:10 +090029 "github.com/google/blueprint/proptools"
Paul Duffin46a26a82020-04-07 19:27:04 +010030
31 "android/soong/android"
Zi Wangb2179e32023-01-31 15:53:30 -080032 "android/soong/bazel"
Ulya Trafimovichdbf31662020-12-17 12:07:54 +000033 "android/soong/dexpreopt"
Jiyong Parkc678ad32018-04-10 13:07:10 +090034)
35
Jooyung Han58f26ab2019-12-18 15:34:32 +090036const (
Pedro Loureiro9956e5e2021-09-07 17:21:59 +000037 sdkXmlFileSuffix = ".xml"
Jiyong Parkc678ad32018-04-10 13:07:10 +090038)
39
Paul Duffind1b3a922020-01-22 11:57:20 +000040// A tag to associated a dependency with a specific api scope.
41type scopeDependencyTag struct {
42 blueprint.BaseDependencyTag
43 name string
44 apiScope *apiScope
Paul Duffinc8782502020-04-29 20:45:27 +010045
46 // Function for extracting appropriate path information from the dependency.
Colin Crossdcf71b22021-02-01 13:59:03 -080047 depInfoExtractor func(paths *scopePaths, ctx android.ModuleContext, dep android.Module) error
Paul Duffinc8782502020-04-29 20:45:27 +010048}
49
50// Extract tag specific information from the dependency.
51func (tag scopeDependencyTag) extractDepInfo(ctx android.ModuleContext, dep android.Module, paths *scopePaths) {
Colin Crossdcf71b22021-02-01 13:59:03 -080052 err := tag.depInfoExtractor(paths, ctx, dep)
Paul Duffinc8782502020-04-29 20:45:27 +010053 if err != nil {
54 ctx.ModuleErrorf("has an invalid {scopeDependencyTag: %s} dependency on module %s: %s", tag.name, ctx.OtherModuleName(dep), err.Error())
55 }
Paul Duffind1b3a922020-01-22 11:57:20 +000056}
57
Paul Duffin80342d72020-06-26 22:08:43 +010058var _ android.ReplaceSourceWithPrebuilt = (*scopeDependencyTag)(nil)
59
60func (tag scopeDependencyTag) ReplaceSourceWithPrebuilt() bool {
61 return false
62}
63
Paul Duffind1b3a922020-01-22 11:57:20 +000064// Provides information about an api scope, e.g. public, system, test.
65type apiScope struct {
66 // The name of the api scope, e.g. public, system, test
67 name string
68
Paul Duffin97b53b82020-05-05 14:40:52 +010069 // The api scope that this scope extends.
Paul Duffind0b9fca2022-09-30 18:11:41 +010070 //
71 // This organizes the scopes into an extension hierarchy.
72 //
73 // If set this means that the API provided by this scope includes the API provided by the scope
74 // set in this field.
Paul Duffin97b53b82020-05-05 14:40:52 +010075 extends *apiScope
76
Paul Duffind0b9fca2022-09-30 18:11:41 +010077 // The next api scope that a library that uses this scope can access.
78 //
79 // This organizes the scopes into an access hierarchy.
80 //
81 // If set this means that a library that can access this API can also access the API provided by
82 // the scope set in this field.
83 //
84 // A module that sets sdk_version: "<scope>_current" should have access to the <scope> API of
85 // every java_sdk_library that it depends on. If the library does not provide an API for <scope>
86 // then it will traverse up this access hierarchy to find an API that it does provide.
87 //
88 // If this is not set then it defaults to the scope set in extends.
89 canAccess *apiScope
90
Paul Duffin3375e352020-04-28 10:44:03 +010091 // The legacy enabled status for a specific scope can be dependent on other
92 // properties that have been specified on the library so it is provided by
93 // a function that can determine the status by examining those properties.
94 legacyEnabledStatus func(module *SdkLibrary) bool
95
96 // The default enabled status for non-legacy behavior, which is triggered by
97 // explicitly enabling at least one api scope.
98 defaultEnabledStatus bool
99
100 // Gets a pointer to the scope specific properties.
101 scopeSpecificProperties func(module *SdkLibrary) *ApiScopeProperties
102
Paul Duffin46a26a82020-04-07 19:27:04 +0100103 // The name of the field in the dynamically created structure.
104 fieldName string
105
Paul Duffin6b836ba2020-05-13 19:19:49 +0100106 // The name of the property in the java_sdk_library_import
107 propertyName string
108
Paul Duffind1b3a922020-01-22 11:57:20 +0000109 // The tag to use to depend on the stubs library module.
110 stubsTag scopeDependencyTag
111
Paul Duffin0ff08bd2020-04-29 13:30:54 +0100112 // The tag to use to depend on the stubs source module (if separate from the API module).
113 stubsSourceTag scopeDependencyTag
114
115 // The tag to use to depend on the API file generating module (if separate from the stubs source module).
116 apiFileTag scopeDependencyTag
117
Paul Duffinc8782502020-04-29 20:45:27 +0100118 // The tag to use to depend on the stubs source and API module.
119 stubsSourceAndApiTag scopeDependencyTag
Paul Duffind1b3a922020-01-22 11:57:20 +0000120
Paul Duffin958806b2022-05-16 13:10:47 +0000121 // The tag to use to depend on the module that provides the latest version of the API .txt file.
122 latestApiModuleTag scopeDependencyTag
123
124 // The tag to use to depend on the module that provides the latest version of the API removed.txt
125 // file.
126 latestRemovedApiModuleTag scopeDependencyTag
127
Paul Duffind1b3a922020-01-22 11:57:20 +0000128 // The scope specific prefix to add to the api file base of "current.txt" or "removed.txt".
129 apiFilePrefix string
130
Paul Duffind0b9fca2022-09-30 18:11:41 +0100131 // The scope specific suffix to add to the sdk library module name to construct a scope specific
Paul Duffind1b3a922020-01-22 11:57:20 +0000132 // module name.
133 moduleSuffix string
134
Paul Duffind1b3a922020-01-22 11:57:20 +0000135 // SDK version that the stubs library is built against. Note that this is always
136 // *current. Older stubs library built with a numbered SDK version is created from
137 // the prebuilt jar.
138 sdkVersion string
Paul Duffin1fb487d2020-04-07 18:50:10 +0100139
Paul Duffin15f34ef2020-07-20 18:04:44 +0100140 // The annotation that identifies this API level, empty for the public API scope.
141 annotation string
142
Paul Duffin1fb487d2020-04-07 18:50:10 +0100143 // Extra arguments to pass to droidstubs for this scope.
Paul Duffin0ff08bd2020-04-29 13:30:54 +0100144 //
Paul Duffin15f34ef2020-07-20 18:04:44 +0100145 // This is not used directly but is used to construct the droidstubsArgs.
146 extraArgs []string
Paul Duffin0ff08bd2020-04-29 13:30:54 +0100147
Paul Duffin15f34ef2020-07-20 18:04:44 +0100148 // The args that must be passed to droidstubs to generate the API and stubs source
149 // for this scope, constructed dynamically by initApiScope().
Paul Duffin0ff08bd2020-04-29 13:30:54 +0100150 //
151 // The API only includes the additional members that this scope adds over the scope
152 // that it extends.
Paul Duffin15f34ef2020-07-20 18:04:44 +0100153 //
154 // The stubs source must include the definitions of everything that is in this
155 // api scope and all the scopes that this one extends.
156 droidstubsArgs []string
Paul Duffin0ff08bd2020-04-29 13:30:54 +0100157
Anton Hansson6478ac12020-05-02 11:19:36 +0100158 // Whether the api scope can be treated as unstable, and should skip compat checks.
159 unstable bool
Jihoon Kang1c92c3e2023-03-23 17:44:51 +0000160
161 // Represents the SDK kind of this scope.
162 kind android.SdkKind
Paul Duffind1b3a922020-01-22 11:57:20 +0000163}
164
165// Initialize a scope, creating and adding appropriate dependency tags
166func initApiScope(scope *apiScope) *apiScope {
Paul Duffinc8782502020-04-29 20:45:27 +0100167 name := scope.name
Paul Duffin46dc45a2020-05-14 15:39:10 +0100168 scopeByName[name] = scope
169 allScopeNames = append(allScopeNames, name)
Paul Duffin6b836ba2020-05-13 19:19:49 +0100170 scope.propertyName = strings.ReplaceAll(name, "-", "_")
171 scope.fieldName = proptools.FieldNameForProperty(scope.propertyName)
Paul Duffind1b3a922020-01-22 11:57:20 +0000172 scope.stubsTag = scopeDependencyTag{
Paul Duffinc8782502020-04-29 20:45:27 +0100173 name: name + "-stubs",
174 apiScope: scope,
175 depInfoExtractor: (*scopePaths).extractStubsLibraryInfoFromDependency,
Paul Duffind1b3a922020-01-22 11:57:20 +0000176 }
Paul Duffin0ff08bd2020-04-29 13:30:54 +0100177 scope.stubsSourceTag = scopeDependencyTag{
178 name: name + "-stubs-source",
179 apiScope: scope,
180 depInfoExtractor: (*scopePaths).extractStubsSourceInfoFromDep,
181 }
182 scope.apiFileTag = scopeDependencyTag{
183 name: name + "-api",
184 apiScope: scope,
185 depInfoExtractor: (*scopePaths).extractApiInfoFromDep,
186 }
Paul Duffinc8782502020-04-29 20:45:27 +0100187 scope.stubsSourceAndApiTag = scopeDependencyTag{
188 name: name + "-stubs-source-and-api",
189 apiScope: scope,
190 depInfoExtractor: (*scopePaths).extractStubsSourceAndApiInfoFromApiStubsProvider,
Paul Duffind1b3a922020-01-22 11:57:20 +0000191 }
Paul Duffin958806b2022-05-16 13:10:47 +0000192 scope.latestApiModuleTag = scopeDependencyTag{
193 name: name + "-latest-api",
194 apiScope: scope,
195 depInfoExtractor: (*scopePaths).extractLatestApiPath,
196 }
197 scope.latestRemovedApiModuleTag = scopeDependencyTag{
198 name: name + "-latest-removed-api",
199 apiScope: scope,
200 depInfoExtractor: (*scopePaths).extractLatestRemovedApiPath,
201 }
Paul Duffin0ff08bd2020-04-29 13:30:54 +0100202
203 // To get the args needed to generate the stubs source append all the args from
204 // this scope and all the scopes it extends as each set of args adds additional
205 // members to the stubs.
Paul Duffin15f34ef2020-07-20 18:04:44 +0100206 var scopeSpecificArgs []string
207 if scope.annotation != "" {
208 scopeSpecificArgs = []string{"--show-annotation", scope.annotation}
Paul Duffin0ff08bd2020-04-29 13:30:54 +0100209 }
Paul Duffin15f34ef2020-07-20 18:04:44 +0100210 for s := scope; s != nil; s = s.extends {
211 scopeSpecificArgs = append(scopeSpecificArgs, s.extraArgs...)
Paul Duffin0ff08bd2020-04-29 13:30:54 +0100212
Paul Duffin15f34ef2020-07-20 18:04:44 +0100213 // Ensure that the generated stubs includes all the API elements from the API scope
214 // that this scope extends.
215 if s != scope && s.annotation != "" {
216 scopeSpecificArgs = append(scopeSpecificArgs, "--show-for-stub-purposes-annotation", s.annotation)
217 }
218 }
Paul Duffin0ff08bd2020-04-29 13:30:54 +0100219
Paul Duffind0b9fca2022-09-30 18:11:41 +0100220 // By default, a library that can access a scope can also access the scope it extends.
221 if scope.canAccess == nil {
222 scope.canAccess = scope.extends
223 }
224
Paul Duffin15f34ef2020-07-20 18:04:44 +0100225 // Escape any special characters in the arguments. This is needed because droidstubs
226 // passes these directly to the shell command.
227 scope.droidstubsArgs = proptools.ShellEscapeList(scopeSpecificArgs)
Paul Duffin0ff08bd2020-04-29 13:30:54 +0100228
Paul Duffind1b3a922020-01-22 11:57:20 +0000229 return scope
230}
231
Anton Hansson08f476b2021-04-07 15:32:19 +0100232func (scope *apiScope) stubsLibraryModuleNameSuffix() string {
233 return ".stubs" + scope.moduleSuffix
234}
235
Jihoon Kang1c92c3e2023-03-23 17:44:51 +0000236func (scope *apiScope) apiLibraryModuleName(baseName string) string {
237 return scope.stubsLibraryModuleName(baseName) + ".from-text"
238}
239
Paul Duffinc3091c82020-05-08 14:16:20 +0100240func (scope *apiScope) stubsLibraryModuleName(baseName string) string {
Anton Hansson08f476b2021-04-07 15:32:19 +0100241 return baseName + scope.stubsLibraryModuleNameSuffix()
Paul Duffind1b3a922020-01-22 11:57:20 +0000242}
243
Paul Duffinc8782502020-04-29 20:45:27 +0100244func (scope *apiScope) stubsSourceModuleName(baseName string) string {
Paul Duffindd9d0742020-05-08 15:52:37 +0100245 return baseName + ".stubs.source" + scope.moduleSuffix
Paul Duffind1b3a922020-01-22 11:57:20 +0000246}
247
Paul Duffin0ff08bd2020-04-29 13:30:54 +0100248func (scope *apiScope) apiModuleName(baseName string) string {
Paul Duffindd9d0742020-05-08 15:52:37 +0100249 return baseName + ".api" + scope.moduleSuffix
Paul Duffin0ff08bd2020-04-29 13:30:54 +0100250}
251
Paul Duffin3375e352020-04-28 10:44:03 +0100252func (scope *apiScope) String() string {
253 return scope.name
254}
255
Paul Duffin958806b2022-05-16 13:10:47 +0000256// snapshotRelativeDir returns the snapshot directory into which the files related to scopes will
257// be stored.
258func (scope *apiScope) snapshotRelativeDir() string {
259 return filepath.Join("sdk_library", scope.name)
260}
261
262// snapshotRelativeCurrentApiTxtPath returns the snapshot path to the API .txt file for the named
263// library.
264func (scope *apiScope) snapshotRelativeCurrentApiTxtPath(name string) string {
265 return filepath.Join(scope.snapshotRelativeDir(), name+".txt")
266}
267
268// snapshotRelativeRemovedApiTxtPath returns the snapshot path to the removed API .txt file for the
269// named library.
270func (scope *apiScope) snapshotRelativeRemovedApiTxtPath(name string) string {
271 return filepath.Join(scope.snapshotRelativeDir(), name+"-removed.txt")
272}
273
Paul Duffind1b3a922020-01-22 11:57:20 +0000274type apiScopes []*apiScope
275
276func (scopes apiScopes) Strings(accessor func(*apiScope) string) []string {
277 var list []string
278 for _, scope := range scopes {
279 list = append(list, accessor(scope))
280 }
281 return list
282}
283
Jiyong Parkc678ad32018-04-10 13:07:10 +0900284var (
Paul Duffin46dc45a2020-05-14 15:39:10 +0100285 scopeByName = make(map[string]*apiScope)
286 allScopeNames []string
Paul Duffind1b3a922020-01-22 11:57:20 +0000287 apiScopePublic = initApiScope(&apiScope{
Paul Duffin3375e352020-04-28 10:44:03 +0100288 name: "public",
289
290 // Public scope is enabled by default for both legacy and non-legacy modes.
291 legacyEnabledStatus: func(module *SdkLibrary) bool {
292 return true
293 },
294 defaultEnabledStatus: true,
295
296 scopeSpecificProperties: func(module *SdkLibrary) *ApiScopeProperties {
297 return &module.sdkLibraryProperties.Public
298 },
Paul Duffind1b3a922020-01-22 11:57:20 +0000299 sdkVersion: "current",
Jihoon Kang1c92c3e2023-03-23 17:44:51 +0000300 kind: android.SdkPublic,
Paul Duffind1b3a922020-01-22 11:57:20 +0000301 })
302 apiScopeSystem = initApiScope(&apiScope{
Paul Duffin3375e352020-04-28 10:44:03 +0100303 name: "system",
304 extends: apiScopePublic,
305 legacyEnabledStatus: (*SdkLibrary).generateTestAndSystemScopesByDefault,
306 scopeSpecificProperties: func(module *SdkLibrary) *ApiScopeProperties {
307 return &module.sdkLibraryProperties.System
308 },
Paul Duffin15f34ef2020-07-20 18:04:44 +0100309 apiFilePrefix: "system-",
310 moduleSuffix: ".system",
311 sdkVersion: "system_current",
312 annotation: "android.annotation.SystemApi(client=android.annotation.SystemApi.Client.PRIVILEGED_APPS)",
Jihoon Kang1c92c3e2023-03-23 17:44:51 +0000313 kind: android.SdkSystem,
Paul Duffind1b3a922020-01-22 11:57:20 +0000314 })
315 apiScopeTest = initApiScope(&apiScope{
Paul Duffin3375e352020-04-28 10:44:03 +0100316 name: "test",
Anton Hansson4fe970f2020-10-09 10:16:49 +0100317 extends: apiScopeSystem,
Paul Duffin3375e352020-04-28 10:44:03 +0100318 legacyEnabledStatus: (*SdkLibrary).generateTestAndSystemScopesByDefault,
319 scopeSpecificProperties: func(module *SdkLibrary) *ApiScopeProperties {
320 return &module.sdkLibraryProperties.Test
321 },
Paul Duffin15f34ef2020-07-20 18:04:44 +0100322 apiFilePrefix: "test-",
323 moduleSuffix: ".test",
324 sdkVersion: "test_current",
325 annotation: "android.annotation.TestApi",
326 unstable: true,
Jihoon Kang1c92c3e2023-03-23 17:44:51 +0000327 kind: android.SdkTest,
Paul Duffind1b3a922020-01-22 11:57:20 +0000328 })
Paul Duffin8f265b92020-04-28 14:13:56 +0100329 apiScopeModuleLib = initApiScope(&apiScope{
Paul Duffin6b836ba2020-05-13 19:19:49 +0100330 name: "module-lib",
Paul Duffin8f265b92020-04-28 14:13:56 +0100331 extends: apiScopeSystem,
Paul Duffin0c5bae52020-06-02 13:00:08 +0100332 // The module-lib scope is disabled by default in legacy mode.
Paul Duffin8f265b92020-04-28 14:13:56 +0100333 //
334 // Enabling this would break existing usages.
335 legacyEnabledStatus: func(module *SdkLibrary) bool {
336 return false
337 },
338 scopeSpecificProperties: func(module *SdkLibrary) *ApiScopeProperties {
339 return &module.sdkLibraryProperties.Module_lib
340 },
341 apiFilePrefix: "module-lib-",
342 moduleSuffix: ".module_lib",
343 sdkVersion: "module_current",
Paul Duffin15f34ef2020-07-20 18:04:44 +0100344 annotation: "android.annotation.SystemApi(client=android.annotation.SystemApi.Client.MODULE_LIBRARIES)",
Jihoon Kang1c92c3e2023-03-23 17:44:51 +0000345 kind: android.SdkModule,
Paul Duffin8f265b92020-04-28 14:13:56 +0100346 })
Paul Duffin0c5bae52020-06-02 13:00:08 +0100347 apiScopeSystemServer = initApiScope(&apiScope{
348 name: "system-server",
349 extends: apiScopePublic,
Paul Duffind0b9fca2022-09-30 18:11:41 +0100350
351 // The system-server scope can access the module-lib scope.
352 //
353 // A module that provides a system-server API is appended to the standard bootclasspath that is
354 // used by the system server. So, it should be able to access module-lib APIs provided by
355 // libraries on the bootclasspath.
356 canAccess: apiScopeModuleLib,
357
Paul Duffin0c5bae52020-06-02 13:00:08 +0100358 // The system-server scope is disabled by default in legacy mode.
359 //
360 // Enabling this would break existing usages.
361 legacyEnabledStatus: func(module *SdkLibrary) bool {
362 return false
363 },
364 scopeSpecificProperties: func(module *SdkLibrary) *ApiScopeProperties {
365 return &module.sdkLibraryProperties.System_server
366 },
367 apiFilePrefix: "system-server-",
368 moduleSuffix: ".system_server",
369 sdkVersion: "system_server_current",
Paul Duffin15f34ef2020-07-20 18:04:44 +0100370 annotation: "android.annotation.SystemApi(client=android.annotation.SystemApi.Client.SYSTEM_SERVER)",
371 extraArgs: []string{
372 "--hide-annotation", "android.annotation.Hide",
Paul Duffin0c5bae52020-06-02 13:00:08 +0100373 // com.android.* classes are okay in this interface"
Paul Duffin15f34ef2020-07-20 18:04:44 +0100374 "--hide", "InternalClasses",
Paul Duffin0c5bae52020-06-02 13:00:08 +0100375 },
Jihoon Kang1c92c3e2023-03-23 17:44:51 +0000376 kind: android.SdkSystemServer,
Paul Duffin0c5bae52020-06-02 13:00:08 +0100377 })
Paul Duffind1b3a922020-01-22 11:57:20 +0000378 allApiScopes = apiScopes{
379 apiScopePublic,
380 apiScopeSystem,
381 apiScopeTest,
Paul Duffin8f265b92020-04-28 14:13:56 +0100382 apiScopeModuleLib,
Paul Duffin0c5bae52020-06-02 13:00:08 +0100383 apiScopeSystemServer,
Paul Duffind1b3a922020-01-22 11:57:20 +0000384 }
Jiyong Parkc678ad32018-04-10 13:07:10 +0900385)
386
Jiyong Park82484c02018-04-23 21:41:26 +0900387var (
388 javaSdkLibrariesLock sync.Mutex
389)
390
Jiyong Parkc678ad32018-04-10 13:07:10 +0900391// TODO: these are big features that are currently missing
Jiyong Park1be96912018-05-28 18:02:19 +0900392// 1) disallowing linking to the runtime shared lib
393// 2) HTML generation
Jiyong Parkc678ad32018-04-10 13:07:10 +0900394
395func init() {
Paul Duffin43dc1cc2019-12-19 11:18:54 +0000396 RegisterSdkLibraryBuildComponents(android.InitRegistrationContext)
Jiyong Parkc678ad32018-04-10 13:07:10 +0900397
Jiyong Park82484c02018-04-23 21:41:26 +0900398 android.RegisterMakeVarsProvider(pctx, func(ctx android.MakeVarsContext) {
399 javaSdkLibraries := javaSdkLibraries(ctx.Config())
400 sort.Strings(*javaSdkLibraries)
401 ctx.Strict("JAVA_SDK_LIBRARIES", strings.Join(*javaSdkLibraries, " "))
402 })
Paul Duffindd46f712020-02-10 13:37:10 +0000403
404 // Register sdk member types.
Paul Duffin976b0e52021-04-27 23:20:26 +0100405 android.RegisterSdkMemberType(javaSdkLibrarySdkMemberType)
Jiyong Parkc678ad32018-04-10 13:07:10 +0900406}
407
Paul Duffin43dc1cc2019-12-19 11:18:54 +0000408func RegisterSdkLibraryBuildComponents(ctx android.RegistrationContext) {
409 ctx.RegisterModuleType("java_sdk_library", SdkLibraryFactory)
410 ctx.RegisterModuleType("java_sdk_library_import", sdkLibraryImportFactory)
411}
412
Paul Duffin3375e352020-04-28 10:44:03 +0100413// Properties associated with each api scope.
414type ApiScopeProperties struct {
415 // Indicates whether the api surface is generated.
416 //
417 // If this is set for any scope then all scopes must explicitly specify if they
418 // are enabled. This is to prevent new usages from depending on legacy behavior.
419 //
420 // Otherwise, if this is not set for any scope then the default behavior is
421 // scope specific so please refer to the scope specific property documentation.
422 Enabled *bool
Paul Duffin87a05a32020-05-12 11:50:28 +0100423
424 // The sdk_version to use for building the stubs.
425 //
426 // If not specified then it will use an sdk_version determined as follows:
Trevor Radcliffedf8aa1f2021-11-04 14:25:39 +0000427 //
Paul Duffin87a05a32020-05-12 11:50:28 +0100428 // 1) If the sdk_version specified on the java_sdk_library is none then this
Trevor Radcliffedf8aa1f2021-11-04 14:25:39 +0000429 // will be none. This is used for java_sdk_library instances that are used
430 // to create stubs that contribute to the core_current sdk version.
431 // 2) Otherwise, it is assumed that this library extends but does not
432 // contribute directly to a specific sdk_version and so this uses the
433 // sdk_version appropriate for the api scope. e.g. public will use
434 // sdk_version: current, system will use sdk_version: system_current, etc.
Paul Duffin87a05a32020-05-12 11:50:28 +0100435 //
436 // This does not affect the sdk_version used for either generating the stubs source
437 // or the API file. They both have to use the same sdk_version as is used for
438 // compiling the implementation library.
439 Sdk_version *string
Paul Duffin3375e352020-04-28 10:44:03 +0100440}
441
Jiyong Parkc678ad32018-04-10 13:07:10 +0900442type sdkLibraryProperties struct {
Anton Hanssonf8ea3722021-09-16 14:24:13 +0100443 // List of source files that are needed to compile the API, but are not part of runtime library.
444 Api_srcs []string `android:"arch_variant"`
445
Paul Duffin5df79302020-05-16 15:52:12 +0100446 // Visibility for impl library module. If not specified then defaults to the
447 // visibility property.
448 Impl_library_visibility []string
449
Paul Duffin4911a892020-04-29 23:35:13 +0100450 // Visibility for stubs library modules. If not specified then defaults to the
451 // visibility property.
452 Stubs_library_visibility []string
453
454 // Visibility for stubs source modules. If not specified then defaults to the
455 // visibility property.
456 Stubs_source_visibility []string
457
Anton Hansson7f66efa2020-10-08 14:47:23 +0100458 // List of Java libraries that will be in the classpath when building the implementation lib
459 Impl_only_libs []string `android:"arch_variant"`
460
Paul Duffin77590a82022-04-28 14:13:30 +0000461 // List of Java libraries that will included in the implementation lib.
462 Impl_only_static_libs []string `android:"arch_variant"`
463
Sundong Ahnf043cf62018-06-25 16:04:37 +0900464 // List of Java libraries that will be in the classpath when building stubs
465 Stub_only_libs []string `android:"arch_variant"`
466
Anton Hanssondae54cd2021-04-21 16:30:10 +0100467 // List of Java libraries that will included in stub libraries
468 Stub_only_static_libs []string `android:"arch_variant"`
469
Paul Duffin7a586d32019-12-30 17:09:34 +0000470 // list of package names that will be documented and publicized as API.
471 // This allows the API to be restricted to a subset of the source files provided.
472 // If this is unspecified then all the source files will be treated as being part
473 // of the API.
Jiyong Parkc678ad32018-04-10 13:07:10 +0900474 Api_packages []string
475
Jiyong Park5a2c9d72018-05-01 22:25:41 +0900476 // list of package names that must be hidden from the API
477 Hidden_api_packages []string
478
Paul Duffin749f98f2019-12-30 17:23:46 +0000479 // the relative path to the directory containing the api specification files.
480 // Defaults to "api".
481 Api_dir *string
482
Paul Duffindfa131e2020-05-15 20:37:11 +0100483 // Determines whether a runtime implementation library is built; defaults to false.
484 //
485 // 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 +0200486 // it is as if shared_library: false, was set.
Paul Duffin43db9be2019-12-30 17:35:49 +0000487 Api_only *bool
488
Paul Duffin11512472019-02-11 15:55:17 +0000489 // local files that are used within user customized droiddoc options.
490 Droiddoc_option_files []string
491
Spandan Das93e95992021-07-29 18:26:39 +0000492 // additional droiddoc options.
Paul Duffin11512472019-02-11 15:55:17 +0000493 // Available variables for substitution:
494 //
495 // $(location <label>): the path to the droiddoc_option_files with name <label>
Sundong Ahndd567f92018-07-31 17:19:11 +0900496 Droiddoc_options []string
497
Paul Duffine22c2ab2020-05-20 19:35:27 +0100498 // is set to true, Metalava will allow framework SDK to contain annotations.
499 Annotations_enabled *bool
500
Sundong Ahn054b19a2018-10-19 13:46:09 +0900501 // a list of top-level directories containing files to merge qualifier annotations
502 // (i.e. those intended to be included in the stubs written) from.
503 Merge_annotations_dirs []string
504
505 // a list of top-level directories containing Java stub files to merge show/hide annotations from.
506 Merge_inclusion_annotations_dirs []string
507
Paul Duffin4f5c1ef2020-11-19 14:53:43 +0000508 // If set to true then don't create dist rules.
509 No_dist *bool
Sundong Ahn80a87b32019-05-13 15:02:50 +0900510
Paul Duffin31310252020-11-20 21:26:20 +0000511 // The stem for the artifacts that are copied to the dist, if not specified
512 // then defaults to the base module name.
513 //
514 // For each scope the following artifacts are copied to the apistubs/<scope>
515 // directory in the dist.
516 // * stubs impl jar -> <dist-stem>.jar
517 // * API specification file -> api/<dist-stem>.txt
518 // * Removed API specification file -> api/<dist-stem>-removed.txt
519 //
520 // Also used to construct the name of the filegroup (created by prebuilt_apis)
521 // that references the latest released API and remove API specification files.
522 // * API specification filegroup -> <dist-stem>.api.<scope>.latest
523 // * Removed API specification filegroup -> <dist-stem>-removed.api.<scope>.latest
Jaewoong Jung1a97ee02021-03-09 13:25:02 -0800524 // * API incompatibilities baseline filegroup -> <dist-stem>-incompatibilities.api.<scope>.latest
Paul Duffin31310252020-11-20 21:26:20 +0000525 Dist_stem *string
526
Colin Cross986b69a2021-06-01 13:13:40 -0700527 // The subdirectory for the artifacts that are copied to the dist directory. If not specified
Colin Cross3dd66252021-06-01 14:05:09 -0700528 // then defaults to "unknown". Should be set to "android" for anything that should be published
Colin Cross986b69a2021-06-01 13:13:40 -0700529 // in the public Android SDK.
530 Dist_group *string
531
Anton Hanssondff2c782020-12-21 17:10:01 +0000532 // A compatibility mode that allows historical API-tracking files to not exist.
533 // Do not use.
534 Unsafe_ignore_missing_latest_api bool
535
Paul Duffin3375e352020-04-28 10:44:03 +0100536 // indicates whether system and test apis should be generated.
537 Generate_system_and_test_apis bool `blueprint:"mutated"`
538
539 // The properties specific to the public api scope
540 //
541 // Unless explicitly specified by using public.enabled the public api scope is
542 // enabled by default in both legacy and non-legacy mode.
543 Public ApiScopeProperties
544
545 // The properties specific to the system api scope
546 //
547 // In legacy mode the system api scope is enabled by default when sdk_version
548 // is set to something other than "none".
549 //
550 // In non-legacy mode the system api scope is disabled by default.
551 System ApiScopeProperties
552
553 // The properties specific to the test api scope
554 //
555 // In legacy mode the test api scope is enabled by default when sdk_version
556 // is set to something other than "none".
557 //
558 // In non-legacy mode the test api scope is disabled by default.
559 Test ApiScopeProperties
Paul Duffin37e0b772019-12-30 17:20:10 +0000560
Paul Duffin0c5bae52020-06-02 13:00:08 +0100561 // The properties specific to the module-lib api scope
Paul Duffin8f265b92020-04-28 14:13:56 +0100562 //
Zi Wangb2179e32023-01-31 15:53:30 -0800563 // Unless explicitly specified by using module_lib.enabled the module_lib api
564 // scope is disabled by default.
Paul Duffin8f265b92020-04-28 14:13:56 +0100565 Module_lib ApiScopeProperties
566
Paul Duffin0c5bae52020-06-02 13:00:08 +0100567 // The properties specific to the system-server api scope
568 //
Zi Wangb2179e32023-01-31 15:53:30 -0800569 // Unless explicitly specified by using system_server.enabled the
570 // system_server api scope is disabled by default.
Paul Duffin0c5bae52020-06-02 13:00:08 +0100571 System_server ApiScopeProperties
572
Jiyong Park932cdfe2020-05-28 00:19:53 +0900573 // Determines if the stubs are preferred over the implementation library
574 // for linking, even when the client doesn't specify sdk_version. When this
575 // is set to true, such clients are provided with the widest API surface that
576 // this lib provides. Note however that this option doesn't affect the clients
577 // that are in the same APEX as this library. In that case, the clients are
578 // always linked with the implementation library. Default is false.
579 Default_to_stubs *bool
580
Paul Duffin160fe412020-05-10 19:32:20 +0100581 // Properties related to api linting.
582 Api_lint struct {
583 // Enable api linting.
584 Enabled *bool
585 }
586
Jiyong Parkc678ad32018-04-10 13:07:10 +0900587 // TODO: determines whether to create HTML doc or not
Paul Duffine8409952022-09-22 16:24:46 +0100588 // Html_doc *bool
Jiyong Parkc678ad32018-04-10 13:07:10 +0900589}
590
Paul Duffin0f8faff2020-05-20 16:18:00 +0100591// Paths to outputs from java_sdk_library and java_sdk_library_import.
592//
593// Fields that are android.Paths are always set (during GenerateAndroidBuildActions).
594// OptionalPaths are always set by java_sdk_library but may not be set by
595// java_sdk_library_import as not all instances provide that information.
Paul Duffind1b3a922020-01-22 11:57:20 +0000596type scopePaths struct {
Paul Duffin0f8faff2020-05-20 16:18:00 +0100597 // The path (represented as Paths for convenience when returning) to the stubs header jar.
598 //
599 // That is the jar that is created by turbine.
600 stubsHeaderPath android.Paths
601
602 // The path (represented as Paths for convenience when returning) to the stubs implementation jar.
603 //
604 // This is not the implementation jar, it still only contains stubs.
605 stubsImplPath android.Paths
606
Paul Duffin1267d872021-04-16 17:21:36 +0100607 // The dex jar for the stubs.
608 //
609 // This is not the implementation jar, it still only contains stubs.
Martin Stjernholm8be1e6d2021-09-15 03:34:04 +0100610 stubsDexJarPath OptionalDexJarPath
Paul Duffin1267d872021-04-16 17:21:36 +0100611
Paul Duffin0f8faff2020-05-20 16:18:00 +0100612 // The API specification file, e.g. system_current.txt.
613 currentApiFilePath android.OptionalPath
614
615 // The specification of API elements removed since the last release.
616 removedApiFilePath android.OptionalPath
617
618 // The stubs source jar.
619 stubsSrcJar android.OptionalPath
Anton Hanssond78eb762021-09-21 15:25:12 +0100620
621 // Extracted annotations.
622 annotationsZip android.OptionalPath
Paul Duffin958806b2022-05-16 13:10:47 +0000623
624 // The path to the latest API file.
625 latestApiPath android.OptionalPath
626
627 // The path to the latest removed API file.
628 latestRemovedApiPath android.OptionalPath
Paul Duffind1b3a922020-01-22 11:57:20 +0000629}
630
Colin Crossdcf71b22021-02-01 13:59:03 -0800631func (paths *scopePaths) extractStubsLibraryInfoFromDependency(ctx android.ModuleContext, dep android.Module) error {
632 if ctx.OtherModuleHasProvider(dep, JavaInfoProvider) {
633 lib := ctx.OtherModuleProvider(dep, JavaInfoProvider).(JavaInfo)
634 paths.stubsHeaderPath = lib.HeaderJars
635 paths.stubsImplPath = lib.ImplementationJars
Paul Duffin1267d872021-04-16 17:21:36 +0100636
637 libDep := dep.(UsesLibraryDependency)
638 paths.stubsDexJarPath = libDep.DexJarBuildPath()
Paul Duffinc8782502020-04-29 20:45:27 +0100639 return nil
640 } else {
Colin Crossdcf71b22021-02-01 13:59:03 -0800641 return fmt.Errorf("expected module that has JavaInfoProvider, e.g. java_library")
Paul Duffinc8782502020-04-29 20:45:27 +0100642 }
643}
644
Paul Duffin0ff08bd2020-04-29 13:30:54 +0100645func (paths *scopePaths) treatDepAsApiStubsProvider(dep android.Module, action func(provider ApiStubsProvider)) error {
646 if apiStubsProvider, ok := dep.(ApiStubsProvider); ok {
647 action(apiStubsProvider)
Paul Duffinc8782502020-04-29 20:45:27 +0100648 return nil
649 } else {
650 return fmt.Errorf("expected module that implements ApiStubsProvider, e.g. droidstubs")
651 }
652}
653
Paul Duffin0f8faff2020-05-20 16:18:00 +0100654func (paths *scopePaths) treatDepAsApiStubsSrcProvider(dep android.Module, action func(provider ApiStubsSrcProvider)) error {
655 if apiStubsProvider, ok := dep.(ApiStubsSrcProvider); ok {
656 action(apiStubsProvider)
657 return nil
658 } else {
659 return fmt.Errorf("expected module that implements ApiStubsSrcProvider, e.g. droidstubs")
660 }
661}
662
Paul Duffin0ff08bd2020-04-29 13:30:54 +0100663func (paths *scopePaths) extractApiInfoFromApiStubsProvider(provider ApiStubsProvider) {
Anton Hanssond78eb762021-09-21 15:25:12 +0100664 paths.annotationsZip = android.OptionalPathForPath(provider.AnnotationsZip())
Paul Duffin0f8faff2020-05-20 16:18:00 +0100665 paths.currentApiFilePath = android.OptionalPathForPath(provider.ApiFilePath())
666 paths.removedApiFilePath = android.OptionalPathForPath(provider.RemovedApiFilePath())
Paul Duffin0ff08bd2020-04-29 13:30:54 +0100667}
668
Colin Crossdcf71b22021-02-01 13:59:03 -0800669func (paths *scopePaths) extractApiInfoFromDep(ctx android.ModuleContext, dep android.Module) error {
Paul Duffin0ff08bd2020-04-29 13:30:54 +0100670 return paths.treatDepAsApiStubsProvider(dep, func(provider ApiStubsProvider) {
671 paths.extractApiInfoFromApiStubsProvider(provider)
672 })
673}
674
Paul Duffin0f8faff2020-05-20 16:18:00 +0100675func (paths *scopePaths) extractStubsSourceInfoFromApiStubsProviders(provider ApiStubsSrcProvider) {
676 paths.stubsSrcJar = android.OptionalPathForPath(provider.StubsSrcJar())
Paul Duffin0ff08bd2020-04-29 13:30:54 +0100677}
678
Colin Crossdcf71b22021-02-01 13:59:03 -0800679func (paths *scopePaths) extractStubsSourceInfoFromDep(ctx android.ModuleContext, dep android.Module) error {
Paul Duffin0f8faff2020-05-20 16:18:00 +0100680 return paths.treatDepAsApiStubsSrcProvider(dep, func(provider ApiStubsSrcProvider) {
Paul Duffin0ff08bd2020-04-29 13:30:54 +0100681 paths.extractStubsSourceInfoFromApiStubsProviders(provider)
682 })
683}
684
Colin Crossdcf71b22021-02-01 13:59:03 -0800685func (paths *scopePaths) extractStubsSourceAndApiInfoFromApiStubsProvider(ctx android.ModuleContext, dep android.Module) error {
Paul Duffin0ff08bd2020-04-29 13:30:54 +0100686 return paths.treatDepAsApiStubsProvider(dep, func(provider ApiStubsProvider) {
687 paths.extractApiInfoFromApiStubsProvider(provider)
688 paths.extractStubsSourceInfoFromApiStubsProviders(provider)
689 })
690}
691
Paul Duffin958806b2022-05-16 13:10:47 +0000692func extractSingleOptionalOutputPath(dep android.Module) (android.OptionalPath, error) {
693 var paths android.Paths
694 if sourceFileProducer, ok := dep.(android.SourceFileProducer); ok {
695 paths = sourceFileProducer.Srcs()
696 } else {
697 return android.OptionalPath{}, fmt.Errorf("module %q does not produce source files", dep)
698 }
699 if len(paths) != 1 {
700 return android.OptionalPath{}, fmt.Errorf("expected one path from %q, got %q", dep, paths)
701 }
702 return android.OptionalPathForPath(paths[0]), nil
703}
704
705func (paths *scopePaths) extractLatestApiPath(ctx android.ModuleContext, dep android.Module) error {
706 outputPath, err := extractSingleOptionalOutputPath(dep)
707 paths.latestApiPath = outputPath
708 return err
709}
710
711func (paths *scopePaths) extractLatestRemovedApiPath(ctx android.ModuleContext, dep android.Module) error {
712 outputPath, err := extractSingleOptionalOutputPath(dep)
713 paths.latestRemovedApiPath = outputPath
714 return err
715}
716
Paul Duffin0ff08bd2020-04-29 13:30:54 +0100717type commonToSdkLibraryAndImportProperties struct {
Paul Duffin1b1e8062020-05-08 13:44:43 +0100718 // The naming scheme to use for the components that this module creates.
719 //
Paul Duffinee9ad5d2020-09-11 13:04:05 +0100720 // If not specified then it defaults to "default".
Paul Duffin1b1e8062020-05-08 13:44:43 +0100721 //
722 // This is a temporary mechanism to simplify conversion from separate modules for each
723 // component that follow a different naming pattern to the default one.
724 //
725 // TODO(b/155480189) - Remove once naming inconsistencies have been resolved.
Paul Duffin0ff08bd2020-04-29 13:30:54 +0100726 Naming_scheme *string
Paul Duffindfa131e2020-05-15 20:37:11 +0100727
728 // Specifies whether this module can be used as an Android shared library; defaults
729 // to true.
730 //
731 // An Android shared library is one that can be referenced in a <uses-library> element
732 // in an AndroidManifest.xml.
733 Shared_library *bool
Paul Duffina2ae7e02020-09-11 11:55:00 +0100734
735 // Files containing information about supported java doc tags.
736 Doctag_files []string `android:"path"`
Pedro Loureiro9956e5e2021-09-07 17:21:59 +0000737
738 // Signals that this shared library is part of the bootclasspath starting
739 // on the version indicated in this attribute.
740 //
741 // This will make platforms at this level and above to ignore
742 // <uses-library> tags with this library name because the library is already
743 // available
744 On_bootclasspath_since *string
745
746 // Signals that this shared library was part of the bootclasspath before
747 // (but not including) the version indicated in this attribute.
748 //
749 // The system will automatically add a <uses-library> tag with this library to
750 // apps that target any SDK less than the version indicated in this attribute.
751 On_bootclasspath_before *string
752
753 // Indicates that PackageManager should ignore this shared library if the
754 // platform is below the version indicated in this attribute.
755 //
756 // This means that the device won't recognise this library as installed.
757 Min_device_sdk *string
758
759 // Indicates that PackageManager should ignore this shared library if the
760 // platform is above the version indicated in this attribute.
761 //
762 // This means that the device won't recognise this library as installed.
763 Max_device_sdk *string
Paul Duffin0ff08bd2020-04-29 13:30:54 +0100764}
765
Paul Duffin71b33cc2021-06-23 11:39:47 +0100766// commonSdkLibraryAndImportModule defines the interface that must be provided by a module that
767// embeds the commonToSdkLibraryAndImport struct.
768type commonSdkLibraryAndImportModule interface {
Paul Duffind796f6f2022-11-23 23:06:05 +0000769 android.Module
Paul Duffin71b33cc2021-06-23 11:39:47 +0100770
771 BaseModuleName() string
772}
773
Paul Duffin56d44902020-01-31 13:36:25 +0000774// Common code between sdk library and sdk library import
775type commonToSdkLibraryAndImport struct {
Paul Duffin71b33cc2021-06-23 11:39:47 +0100776 module commonSdkLibraryAndImportModule
Paul Duffinc3091c82020-05-08 14:16:20 +0100777
Paul Duffin56d44902020-01-31 13:36:25 +0000778 scopePaths map[*apiScope]*scopePaths
Paul Duffin1b1e8062020-05-08 13:44:43 +0100779
780 namingScheme sdkLibraryComponentNamingScheme
781
Paul Duffindfa131e2020-05-15 20:37:11 +0100782 commonSdkLibraryProperties commonToSdkLibraryAndImportProperties
Paul Duffin859fe962020-05-15 10:20:31 +0100783
Paul Duffina2ae7e02020-09-11 11:55:00 +0100784 // Paths to commonSdkLibraryProperties.Doctag_files
785 doctagPaths android.Paths
786
Paul Duffin859fe962020-05-15 10:20:31 +0100787 // Functionality related to this being used as a component of a java_sdk_library.
788 EmbeddableSdkLibraryComponent
Paul Duffin56d44902020-01-31 13:36:25 +0000789}
790
Paul Duffin71b33cc2021-06-23 11:39:47 +0100791func (c *commonToSdkLibraryAndImport) initCommon(module commonSdkLibraryAndImportModule) {
792 c.module = module
Paul Duffin1b1e8062020-05-08 13:44:43 +0100793
Paul Duffin71b33cc2021-06-23 11:39:47 +0100794 module.AddProperties(&c.commonSdkLibraryProperties)
Paul Duffin859fe962020-05-15 10:20:31 +0100795
796 // Initialize this as an sdk library component.
Paul Duffin71b33cc2021-06-23 11:39:47 +0100797 c.initSdkLibraryComponent(module)
Paul Duffin1b1e8062020-05-08 13:44:43 +0100798}
799
800func (c *commonToSdkLibraryAndImport) initCommonAfterDefaultsApplied(ctx android.DefaultableHookContext) bool {
Paul Duffindfa131e2020-05-15 20:37:11 +0100801 schemeProperty := proptools.StringDefault(c.commonSdkLibraryProperties.Naming_scheme, "default")
Paul Duffin1b1e8062020-05-08 13:44:43 +0100802 switch schemeProperty {
803 case "default":
804 c.namingScheme = &defaultNamingScheme{}
805 default:
806 ctx.PropertyErrorf("naming_scheme", "expected 'default' but was %q", schemeProperty)
807 return false
808 }
809
Paul Duffin3f0290e2021-06-30 18:25:36 +0100810 namePtr := proptools.StringPtr(c.module.BaseModuleName())
811 c.sdkLibraryComponentProperties.SdkLibraryName = namePtr
812
Paul Duffindfa131e2020-05-15 20:37:11 +0100813 // Only track this sdk library if this can be used as a shared library.
814 if c.sharedLibrary() {
815 // Use the name specified in the module definition as the owner.
Paul Duffin3f0290e2021-06-30 18:25:36 +0100816 c.sdkLibraryComponentProperties.SdkLibraryToImplicitlyTrack = namePtr
Paul Duffindfa131e2020-05-15 20:37:11 +0100817 }
Paul Duffin859fe962020-05-15 10:20:31 +0100818
Paul Duffin1b1e8062020-05-08 13:44:43 +0100819 return true
Paul Duffinc3091c82020-05-08 14:16:20 +0100820}
821
Paul Duffinea8f8082021-06-24 13:25:57 +0100822// uniqueApexVariations provides common implementation of the ApexModule.UniqueApexVariations
823// method.
824func (c *commonToSdkLibraryAndImport) uniqueApexVariations() bool {
825 // A java_sdk_library that is a shared library produces an XML file that makes the shared library
826 // usable from an AndroidManifest.xml's <uses-library> entry. That XML file contains the name of
827 // the APEX and so it needs a unique variation per APEX.
828 return c.sharedLibrary()
829}
830
Paul Duffina2ae7e02020-09-11 11:55:00 +0100831func (c *commonToSdkLibraryAndImport) generateCommonBuildActions(ctx android.ModuleContext) {
832 c.doctagPaths = android.PathsForModuleSrc(ctx, c.commonSdkLibraryProperties.Doctag_files)
833}
834
Paul Duffineedc5d52020-06-12 17:46:39 +0100835// Module name of the runtime implementation library
836func (c *commonToSdkLibraryAndImport) implLibraryModuleName() string {
Paul Duffin71b33cc2021-06-23 11:39:47 +0100837 return c.module.BaseModuleName() + ".impl"
Paul Duffineedc5d52020-06-12 17:46:39 +0100838}
839
840// Module name of the XML file for the lib
841func (c *commonToSdkLibraryAndImport) xmlPermissionsModuleName() string {
Paul Duffin71b33cc2021-06-23 11:39:47 +0100842 return c.module.BaseModuleName() + sdkXmlFileSuffix
Paul Duffineedc5d52020-06-12 17:46:39 +0100843}
844
Paul Duffinc3091c82020-05-08 14:16:20 +0100845// Name of the java_library module that compiles the stubs source.
846func (c *commonToSdkLibraryAndImport) stubsLibraryModuleName(apiScope *apiScope) string {
Paul Duffinb97b1572021-04-29 21:50:40 +0100847 baseName := c.module.BaseModuleName()
Paul Duffin21787622022-11-25 12:48:20 +0000848 return c.namingScheme.stubsLibraryModuleName(apiScope, baseName)
Paul Duffinc3091c82020-05-08 14:16:20 +0100849}
850
851// Name of the droidstubs module that generates the stubs source and may also
852// generate/check the API.
853func (c *commonToSdkLibraryAndImport) stubsSourceModuleName(apiScope *apiScope) string {
Paul Duffinb97b1572021-04-29 21:50:40 +0100854 baseName := c.module.BaseModuleName()
Paul Duffin21787622022-11-25 12:48:20 +0000855 return c.namingScheme.stubsSourceModuleName(apiScope, baseName)
Paul Duffinc3091c82020-05-08 14:16:20 +0100856}
857
Jihoon Kang1c92c3e2023-03-23 17:44:51 +0000858// Name of the java_api_library module that generates the from-text stubs source
859// and compiles to a jar file.
860func (c *commonToSdkLibraryAndImport) apiLibraryModuleName(apiScope *apiScope) string {
861 baseName := c.module.BaseModuleName()
862 return c.namingScheme.apiLibraryModuleName(apiScope, baseName)
863}
864
Paul Duffin46dc45a2020-05-14 15:39:10 +0100865// The component names for different outputs of the java_sdk_library.
866//
867// They are similar to the names used for the child modules it creates
868const (
869 stubsSourceComponentName = "stubs.source"
870
871 apiTxtComponentName = "api.txt"
872
873 removedApiTxtComponentName = "removed-api.txt"
Anton Hanssond78eb762021-09-21 15:25:12 +0100874
875 annotationsComponentName = "annotations.zip"
Paul Duffin46dc45a2020-05-14 15:39:10 +0100876)
877
878// A regular expression to match tags that reference a specific stubs component.
879//
880// It will only match if given a valid scope and a valid component. It is verfy strict
881// to ensure it does not accidentally match a similar looking tag that should be processed
882// by the embedded Library.
883var tagSplitter = func() *regexp.Regexp {
884 // Given a list of literal string items returns a regular expression that will
885 // match any one of the items.
886 choice := func(items ...string) string {
887 return `\Q` + strings.Join(items, `\E|\Q`) + `\E`
888 }
889
890 // Regular expression to match one of the scopes.
891 scopesRegexp := choice(allScopeNames...)
892
893 // Regular expression to match one of the components.
Anton Hanssond78eb762021-09-21 15:25:12 +0100894 componentsRegexp := choice(stubsSourceComponentName, apiTxtComponentName, removedApiTxtComponentName, annotationsComponentName)
Paul Duffin46dc45a2020-05-14 15:39:10 +0100895
896 // Regular expression to match any combination of one scope and one component.
897 return regexp.MustCompile(fmt.Sprintf(`^\.(%s)\.(%s)$`, scopesRegexp, componentsRegexp))
898}()
899
900// For OutputFileProducer interface
901//
Anton Hanssond78eb762021-09-21 15:25:12 +0100902// .<scope>.<component name>, for all ComponentNames (for example: .public.removed-api.txt)
Paul Duffin46dc45a2020-05-14 15:39:10 +0100903func (c *commonToSdkLibraryAndImport) commonOutputFiles(tag string) (android.Paths, error) {
904 if groups := tagSplitter.FindStringSubmatch(tag); groups != nil {
905 scopeName := groups[1]
906 component := groups[2]
907
908 if scope, ok := scopeByName[scopeName]; ok {
909 paths := c.findScopePaths(scope)
910 if paths == nil {
Paul Duffin71b33cc2021-06-23 11:39:47 +0100911 return nil, fmt.Errorf("%q does not provide api scope %s", c.module.BaseModuleName(), scopeName)
Paul Duffin46dc45a2020-05-14 15:39:10 +0100912 }
913
914 switch component {
915 case stubsSourceComponentName:
916 if paths.stubsSrcJar.Valid() {
917 return android.Paths{paths.stubsSrcJar.Path()}, nil
918 }
919
920 case apiTxtComponentName:
921 if paths.currentApiFilePath.Valid() {
922 return android.Paths{paths.currentApiFilePath.Path()}, nil
923 }
924
925 case removedApiTxtComponentName:
926 if paths.removedApiFilePath.Valid() {
927 return android.Paths{paths.removedApiFilePath.Path()}, nil
928 }
Anton Hanssond78eb762021-09-21 15:25:12 +0100929
930 case annotationsComponentName:
931 if paths.annotationsZip.Valid() {
932 return android.Paths{paths.annotationsZip.Path()}, nil
933 }
Paul Duffin46dc45a2020-05-14 15:39:10 +0100934 }
935
936 return nil, fmt.Errorf("%s not available for api scope %s", component, scopeName)
937 } else {
938 return nil, fmt.Errorf("unknown scope %s in %s", scope, tag)
939 }
940
941 } else {
Paul Duffina2ae7e02020-09-11 11:55:00 +0100942 switch tag {
943 case ".doctags":
944 if c.doctagPaths != nil {
945 return c.doctagPaths, nil
946 } else {
Paul Duffin71b33cc2021-06-23 11:39:47 +0100947 return nil, fmt.Errorf("no doctag_files specified on %s", c.module.BaseModuleName())
Paul Duffina2ae7e02020-09-11 11:55:00 +0100948 }
949 }
Paul Duffin46dc45a2020-05-14 15:39:10 +0100950 return nil, nil
951 }
952}
953
Paul Duffin803a9562020-05-20 11:52:25 +0100954func (c *commonToSdkLibraryAndImport) getScopePathsCreateIfNeeded(scope *apiScope) *scopePaths {
Paul Duffin56d44902020-01-31 13:36:25 +0000955 if c.scopePaths == nil {
956 c.scopePaths = make(map[*apiScope]*scopePaths)
957 }
958 paths := c.scopePaths[scope]
959 if paths == nil {
960 paths = &scopePaths{}
961 c.scopePaths[scope] = paths
962 }
963
964 return paths
965}
966
Paul Duffin803a9562020-05-20 11:52:25 +0100967func (c *commonToSdkLibraryAndImport) findScopePaths(scope *apiScope) *scopePaths {
968 if c.scopePaths == nil {
969 return nil
970 }
971
972 return c.scopePaths[scope]
973}
974
975// If this does not support the requested api scope then find the closest available
976// scope it does support. Returns nil if no such scope is available.
977func (c *commonToSdkLibraryAndImport) findClosestScopePath(scope *apiScope) *scopePaths {
Paul Duffind0b9fca2022-09-30 18:11:41 +0100978 for s := scope; s != nil; s = s.canAccess {
Paul Duffin803a9562020-05-20 11:52:25 +0100979 if paths := c.findScopePaths(s); paths != nil {
980 return paths
981 }
982 }
983
984 // This should never happen outside tests as public should be the base scope for every
985 // scope and is enabled by default.
986 return nil
987}
988
Jiyong Parkf1691d22021-03-29 20:11:58 +0900989func (c *commonToSdkLibraryAndImport) selectHeaderJarsForSdkVersion(ctx android.BaseModuleContext, sdkVersion android.SdkSpec) android.Paths {
Paul Duffinb05d4292020-05-20 12:19:10 +0100990
991 // If a specific numeric version has been requested then use prebuilt versions of the sdk.
Jiyong Park54105c42021-03-31 18:17:53 +0900992 if !sdkVersion.ApiLevel.IsPreview() {
Paul Duffin71b33cc2021-06-23 11:39:47 +0100993 return PrebuiltJars(ctx, c.module.BaseModuleName(), sdkVersion)
Paul Duffinb05d4292020-05-20 12:19:10 +0100994 }
995
Paul Duffin1267d872021-04-16 17:21:36 +0100996 paths := c.selectScopePaths(ctx, sdkVersion.Kind)
997 if paths == nil {
998 return nil
999 }
1000
1001 return paths.stubsHeaderPath
1002}
1003
1004// selectScopePaths returns the *scopePaths appropriate for the specific kind.
1005//
1006// If the module does not support the specific kind then it will return the *scopePaths for the
1007// closest kind which is a subset of the requested kind. e.g. if requesting android.SdkModule then
1008// it will return *scopePaths for android.SdkSystem if available or android.SdkPublic of not.
1009func (c *commonToSdkLibraryAndImport) selectScopePaths(ctx android.BaseModuleContext, kind android.SdkKind) *scopePaths {
Paul Duffin32cf58a2021-05-18 16:32:50 +01001010 apiScope := sdkKindToApiScope(kind)
Paul Duffinb05d4292020-05-20 12:19:10 +01001011
Paul Duffin803a9562020-05-20 11:52:25 +01001012 paths := c.findClosestScopePath(apiScope)
1013 if paths == nil {
1014 var scopes []string
1015 for _, s := range allApiScopes {
1016 if c.findScopePaths(s) != nil {
1017 scopes = append(scopes, s.name)
1018 }
1019 }
Paul Duffin71b33cc2021-06-23 11:39:47 +01001020 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 +01001021 return nil
1022 }
1023
Paul Duffin1267d872021-04-16 17:21:36 +01001024 return paths
1025}
1026
Paul Duffin32cf58a2021-05-18 16:32:50 +01001027// sdkKindToApiScope maps from android.SdkKind to apiScope.
1028func sdkKindToApiScope(kind android.SdkKind) *apiScope {
1029 var apiScope *apiScope
1030 switch kind {
1031 case android.SdkSystem:
1032 apiScope = apiScopeSystem
1033 case android.SdkModule:
1034 apiScope = apiScopeModuleLib
1035 case android.SdkTest:
1036 apiScope = apiScopeTest
1037 case android.SdkSystemServer:
1038 apiScope = apiScopeSystemServer
1039 default:
1040 apiScope = apiScopePublic
1041 }
1042 return apiScope
1043}
1044
Paul Duffin1267d872021-04-16 17:21:36 +01001045// to satisfy SdkLibraryDependency interface
Martin Stjernholm8be1e6d2021-09-15 03:34:04 +01001046func (c *commonToSdkLibraryAndImport) SdkApiStubDexJar(ctx android.BaseModuleContext, kind android.SdkKind) OptionalDexJarPath {
Paul Duffin1267d872021-04-16 17:21:36 +01001047 paths := c.selectScopePaths(ctx, kind)
1048 if paths == nil {
Martin Stjernholm8be1e6d2021-09-15 03:34:04 +01001049 return makeUnsetDexJarPath()
Paul Duffin1267d872021-04-16 17:21:36 +01001050 }
1051
1052 return paths.stubsDexJarPath
Paul Duffinb05d4292020-05-20 12:19:10 +01001053}
1054
Paul Duffin32cf58a2021-05-18 16:32:50 +01001055// to satisfy SdkLibraryDependency interface
1056func (c *commonToSdkLibraryAndImport) SdkRemovedTxtFile(ctx android.BaseModuleContext, kind android.SdkKind) android.OptionalPath {
1057 apiScope := sdkKindToApiScope(kind)
1058 paths := c.findScopePaths(apiScope)
1059 if paths == nil {
1060 return android.OptionalPath{}
1061 }
1062
1063 return paths.removedApiFilePath
1064}
1065
Paul Duffin859fe962020-05-15 10:20:31 +01001066func (c *commonToSdkLibraryAndImport) sdkComponentPropertiesForChildLibrary() interface{} {
1067 componentProps := &struct {
Paul Duffin3f0290e2021-06-30 18:25:36 +01001068 SdkLibraryName *string
Paul Duffin859fe962020-05-15 10:20:31 +01001069 SdkLibraryToImplicitlyTrack *string
Paul Duffindfa131e2020-05-15 20:37:11 +01001070 }{}
1071
Paul Duffin3f0290e2021-06-30 18:25:36 +01001072 namePtr := proptools.StringPtr(c.module.BaseModuleName())
1073 componentProps.SdkLibraryName = namePtr
1074
Paul Duffindfa131e2020-05-15 20:37:11 +01001075 if c.sharedLibrary() {
Paul Duffin859fe962020-05-15 10:20:31 +01001076 // Mark the stubs library as being components of this java_sdk_library so that
1077 // any app that includes code which depends (directly or indirectly) on the stubs
1078 // library will have the appropriate <uses-library> invocation inserted into its
1079 // manifest if necessary.
Paul Duffin3f0290e2021-06-30 18:25:36 +01001080 componentProps.SdkLibraryToImplicitlyTrack = namePtr
Paul Duffin859fe962020-05-15 10:20:31 +01001081 }
1082
1083 return componentProps
1084}
1085
Paul Duffindfa131e2020-05-15 20:37:11 +01001086func (c *commonToSdkLibraryAndImport) sharedLibrary() bool {
1087 return proptools.BoolDefault(c.commonSdkLibraryProperties.Shared_library, true)
1088}
1089
Paul Duffinf4600f62021-05-13 22:34:45 +01001090// Check if the stub libraries should be compiled for dex
1091func (c *commonToSdkLibraryAndImport) stubLibrariesCompiledForDex() bool {
1092 // Always compile the dex file files for the stub libraries if they will be used on the
1093 // bootclasspath.
1094 return !c.sharedLibrary()
1095}
1096
Paul Duffin859fe962020-05-15 10:20:31 +01001097// Properties related to the use of a module as an component of a java_sdk_library.
1098type SdkLibraryComponentProperties struct {
Paul Duffin3f0290e2021-06-30 18:25:36 +01001099 // The name of the java_sdk_library/_import module.
1100 SdkLibraryName *string `blueprint:"mutated"`
Paul Duffin859fe962020-05-15 10:20:31 +01001101
1102 // The name of the java_sdk_library/_import to add to a <uses-library> entry
1103 // in the AndroidManifest.xml of any Android app that includes code that references
1104 // this module. If not set then no java_sdk_library/_import is tracked.
1105 SdkLibraryToImplicitlyTrack *string `blueprint:"mutated"`
1106}
1107
1108// Structure to be embedded in a module struct that needs to support the
1109// SdkLibraryComponentDependency interface.
1110type EmbeddableSdkLibraryComponent struct {
1111 sdkLibraryComponentProperties SdkLibraryComponentProperties
1112}
1113
Paul Duffin71b33cc2021-06-23 11:39:47 +01001114func (e *EmbeddableSdkLibraryComponent) initSdkLibraryComponent(module android.Module) {
1115 module.AddProperties(&e.sdkLibraryComponentProperties)
Paul Duffin859fe962020-05-15 10:20:31 +01001116}
1117
1118// to satisfy SdkLibraryComponentDependency
Paul Duffin3f0290e2021-06-30 18:25:36 +01001119func (e *EmbeddableSdkLibraryComponent) SdkLibraryName() *string {
1120 return e.sdkLibraryComponentProperties.SdkLibraryName
1121}
1122
1123// to satisfy SdkLibraryComponentDependency
Ulya Trafimovich39b437b2020-09-23 16:42:35 +01001124func (e *EmbeddableSdkLibraryComponent) OptionalSdkLibraryImplementation() *string {
Ulya Trafimovich78645fb2021-07-16 15:29:25 +01001125 // For shared libraries, this is the same as the SDK library name. If a Java library or app
1126 // depends on a component library (e.g. a stub library) it still needs to know the name of the
1127 // run-time library and the corresponding module that provides the implementation. This name is
1128 // passed to manifest_fixer (to be added to AndroidManifest.xml) and added to CLC (to be used
1129 // in dexpreopt).
1130 //
1131 // For non-shared SDK (component or not) libraries this returns `nil`, as they are not
1132 // <uses-library> and should not be added to the manifest or to CLC.
Ulya Trafimovich39b437b2020-09-23 16:42:35 +01001133 return e.sdkLibraryComponentProperties.SdkLibraryToImplicitlyTrack
1134}
1135
Paul Duffin859fe962020-05-15 10:20:31 +01001136// Implemented by modules that are (or possibly could be) a component of a java_sdk_library
1137// (including the java_sdk_library) itself.
1138type SdkLibraryComponentDependency interface {
Ulya Trafimovich31e444e2020-08-14 17:32:16 +01001139 UsesLibraryDependency
1140
Paul Duffin3f0290e2021-06-30 18:25:36 +01001141 // SdkLibraryName returns the name of the java_sdk_library/_import module.
1142 SdkLibraryName() *string
1143
Ulya Trafimovich39b437b2020-09-23 16:42:35 +01001144 // The name of the implementation library for the optional SDK library or nil, if there isn't one.
1145 OptionalSdkLibraryImplementation() *string
Paul Duffin859fe962020-05-15 10:20:31 +01001146}
1147
1148// Make sure that all the module types that are components of java_sdk_library/_import
1149// and which can be referenced (directly or indirectly) from an android app implement
1150// the SdkLibraryComponentDependency interface.
1151var _ SdkLibraryComponentDependency = (*Library)(nil)
1152var _ SdkLibraryComponentDependency = (*Import)(nil)
1153var _ SdkLibraryComponentDependency = (*SdkLibrary)(nil)
Paul Duffineedc5d52020-06-12 17:46:39 +01001154var _ SdkLibraryComponentDependency = (*SdkLibraryImport)(nil)
Paul Duffin859fe962020-05-15 10:20:31 +01001155
Paul Duffin32cf58a2021-05-18 16:32:50 +01001156// Provides access to sdk_version related files, e.g. header and implementation jars.
Paul Duffin859fe962020-05-15 10:20:31 +01001157type SdkLibraryDependency interface {
1158 SdkLibraryComponentDependency
1159
1160 // Get the header jars appropriate for the supplied sdk_version.
1161 //
1162 // These are turbine generated jars so they only change if the externals of the
1163 // class changes but it does not contain and implementation or JavaDoc.
Jiyong Parkf1691d22021-03-29 20:11:58 +09001164 SdkHeaderJars(ctx android.BaseModuleContext, sdkVersion android.SdkSpec) android.Paths
Paul Duffin859fe962020-05-15 10:20:31 +01001165
1166 // Get the implementation jars appropriate for the supplied sdk version.
1167 //
1168 // These are either the implementation jar for the whole sdk library or the implementation
1169 // jars for the stubs. The latter should only be needed when generating JavaDoc as otherwise
1170 // they are identical to the corresponding header jars.
Jiyong Parkf1691d22021-03-29 20:11:58 +09001171 SdkImplementationJars(ctx android.BaseModuleContext, sdkVersion android.SdkSpec) android.Paths
Paul Duffin1267d872021-04-16 17:21:36 +01001172
1173 // SdkApiStubDexJar returns the dex jar for the stubs. It is needed by the hiddenapi processing
1174 // tool which processes dex files.
Martin Stjernholm8be1e6d2021-09-15 03:34:04 +01001175 SdkApiStubDexJar(ctx android.BaseModuleContext, kind android.SdkKind) OptionalDexJarPath
Paul Duffinf4600f62021-05-13 22:34:45 +01001176
Paul Duffin32cf58a2021-05-18 16:32:50 +01001177 // SdkRemovedTxtFile returns the optional path to the removed.txt file for the specified sdk kind.
1178 SdkRemovedTxtFile(ctx android.BaseModuleContext, kind android.SdkKind) android.OptionalPath
1179
Paul Duffinf4600f62021-05-13 22:34:45 +01001180 // sharedLibrary returns true if this can be used as a shared library.
1181 sharedLibrary() bool
Paul Duffin859fe962020-05-15 10:20:31 +01001182}
1183
Inseob Kimc0907f12019-02-08 21:00:45 +09001184type SdkLibrary struct {
Sundong Ahn054b19a2018-10-19 13:46:09 +09001185 Library
Jiyong Parkc678ad32018-04-10 13:07:10 +09001186
Zi Wangb2179e32023-01-31 15:53:30 -08001187 android.BazelModuleBase
1188
Sundong Ahn054b19a2018-10-19 13:46:09 +09001189 sdkLibraryProperties sdkLibraryProperties
Jiyong Parkc678ad32018-04-10 13:07:10 +09001190
Paul Duffin3375e352020-04-28 10:44:03 +01001191 // Map from api scope to the scope specific property structure.
1192 scopeToProperties map[*apiScope]*ApiScopeProperties
1193
Paul Duffin56d44902020-01-31 13:36:25 +00001194 commonToSdkLibraryAndImport
Jiyong Parkc678ad32018-04-10 13:07:10 +09001195}
1196
Inseob Kimc0907f12019-02-08 21:00:45 +09001197var _ SdkLibraryDependency = (*SdkLibrary)(nil)
Colin Cross897d2ed2019-02-11 14:03:51 -08001198
Paul Duffin3375e352020-04-28 10:44:03 +01001199func (module *SdkLibrary) generateTestAndSystemScopesByDefault() bool {
1200 return module.sdkLibraryProperties.Generate_system_and_test_apis
1201}
1202
1203func (module *SdkLibrary) getGeneratedApiScopes(ctx android.EarlyModuleContext) apiScopes {
1204 // Check to see if any scopes have been explicitly enabled. If any have then all
1205 // must be.
1206 anyScopesExplicitlyEnabled := false
1207 for _, scope := range allApiScopes {
1208 scopeProperties := module.scopeToProperties[scope]
1209 if scopeProperties.Enabled != nil {
1210 anyScopesExplicitlyEnabled = true
1211 break
1212 }
Paul Duffind1b3a922020-01-22 11:57:20 +00001213 }
Paul Duffin3375e352020-04-28 10:44:03 +01001214
1215 var generatedScopes apiScopes
1216 enabledScopes := make(map[*apiScope]struct{})
1217 for _, scope := range allApiScopes {
1218 scopeProperties := module.scopeToProperties[scope]
1219 // If any scopes are explicitly enabled then ignore the legacy enabled status.
1220 // This is to ensure that any new usages of this module type do not rely on legacy
1221 // behaviour.
1222 defaultEnabledStatus := false
1223 if anyScopesExplicitlyEnabled {
1224 defaultEnabledStatus = scope.defaultEnabledStatus
1225 } else {
1226 defaultEnabledStatus = scope.legacyEnabledStatus(module)
1227 }
1228 enabled := proptools.BoolDefault(scopeProperties.Enabled, defaultEnabledStatus)
1229 if enabled {
1230 enabledScopes[scope] = struct{}{}
1231 generatedScopes = append(generatedScopes, scope)
1232 }
1233 }
1234
1235 // Now check to make sure that any scope that is extended by an enabled scope is also
1236 // enabled.
1237 for _, scope := range allApiScopes {
1238 if _, ok := enabledScopes[scope]; ok {
1239 extends := scope.extends
1240 if extends != nil {
1241 if _, ok := enabledScopes[extends]; !ok {
1242 ctx.ModuleErrorf("enabled api scope %q depends on disabled scope %q", scope, extends)
1243 }
1244 }
1245 }
1246 }
1247
1248 return generatedScopes
Paul Duffind1b3a922020-01-22 11:57:20 +00001249}
1250
satayev758968a2021-12-06 11:42:40 +00001251var _ android.ModuleWithMinSdkVersionCheck = (*SdkLibrary)(nil)
1252
satayev8f088b02021-12-06 11:40:46 +00001253func (module *SdkLibrary) CheckMinSdkVersion(ctx android.ModuleContext) {
Spandan Das8c9ae7e2023-03-03 21:20:36 +00001254 android.CheckMinSdkVersion(ctx, module.MinSdkVersion(ctx), func(c android.ModuleContext, do android.PayloadDepsCallback) {
satayev8f088b02021-12-06 11:40:46 +00001255 ctx.WalkDeps(func(child android.Module, parent android.Module) bool {
1256 isExternal := !module.depIsInSameApex(ctx, child)
1257 if am, ok := child.(android.ApexModule); ok {
1258 if !do(ctx, parent, am, isExternal) {
1259 return false
1260 }
1261 }
1262 return !isExternal
1263 })
1264 })
1265}
1266
Paul Duffineedc5d52020-06-12 17:46:39 +01001267type sdkLibraryComponentTag struct {
1268 blueprint.BaseDependencyTag
1269 name string
1270}
1271
1272// Mark this tag so dependencies that use it are excluded from visibility enforcement.
1273func (t sdkLibraryComponentTag) ExcludeFromVisibilityEnforcement() {}
1274
1275var xmlPermissionsFileTag = sdkLibraryComponentTag{name: "xml-permissions-file"}
Paul Duffine74ac732020-02-06 13:51:46 +00001276
Jiyong Parke3833882020-02-17 17:28:10 +09001277func IsXmlPermissionsFileDepTag(depTag blueprint.DependencyTag) bool {
Paul Duffineedc5d52020-06-12 17:46:39 +01001278 if dt, ok := depTag.(sdkLibraryComponentTag); ok {
Jiyong Parke3833882020-02-17 17:28:10 +09001279 return dt == xmlPermissionsFileTag
1280 }
1281 return false
1282}
1283
Paul Duffineedc5d52020-06-12 17:46:39 +01001284var implLibraryTag = sdkLibraryComponentTag{name: "impl-library"}
Paul Duffin5df79302020-05-16 15:52:12 +01001285
Paul Duffin44f1d842020-06-26 20:17:02 +01001286// Add the dependencies on the child modules in the component deps mutator.
1287func (module *SdkLibrary) ComponentDepsMutator(ctx android.BottomUpMutatorContext) {
Paul Duffin3375e352020-04-28 10:44:03 +01001288 for _, apiScope := range module.getGeneratedApiScopes(ctx) {
Paul Duffind1b3a922020-01-22 11:57:20 +00001289 // Add dependencies to the stubs library
Spandan Das877f39d2023-03-29 16:19:51 +00001290 stubModuleName := module.stubsLibraryModuleName(apiScope)
1291 // Use JavaApiLibraryName function to be redirected to stubs generated from .txt if applicable
Jihoon Kang1c92c3e2023-03-23 17:44:51 +00001292 if module.contributesToApiSurface(ctx.Config()) {
1293 stubModuleName = android.JavaApiLibraryName(ctx.Config(), stubModuleName)
1294 }
Spandan Das877f39d2023-03-29 16:19:51 +00001295 ctx.AddVariationDependencies(nil, apiScope.stubsTag, stubModuleName)
Paul Duffind1b3a922020-01-22 11:57:20 +00001296
Paul Duffin15f34ef2020-07-20 18:04:44 +01001297 // Add a dependency on the stubs source in order to access both stubs source and api information.
1298 ctx.AddVariationDependencies(nil, apiScope.stubsSourceAndApiTag, module.stubsSourceModuleName(apiScope))
Paul Duffin958806b2022-05-16 13:10:47 +00001299
1300 if module.compareAgainstLatestApi(apiScope) {
1301 // Add dependencies on the latest finalized version of the API .txt file.
1302 latestApiModuleName := module.latestApiModuleName(apiScope)
1303 ctx.AddDependency(module, apiScope.latestApiModuleTag, latestApiModuleName)
1304
1305 // Add dependencies on the latest finalized version of the remove API .txt file.
1306 latestRemovedApiModuleName := module.latestRemovedApiModuleName(apiScope)
1307 ctx.AddDependency(module, apiScope.latestRemovedApiModuleTag, latestRemovedApiModuleName)
1308 }
Sundong Ahn054b19a2018-10-19 13:46:09 +09001309 }
1310
Paul Duffindfa131e2020-05-15 20:37:11 +01001311 if module.requiresRuntimeImplementationLibrary() {
Paul Duffin5df79302020-05-16 15:52:12 +01001312 // Add dependency to the rule for generating the implementation library.
1313 ctx.AddDependency(module, implLibraryTag, module.implLibraryModuleName())
1314
Paul Duffindfa131e2020-05-15 20:37:11 +01001315 if module.sharedLibrary() {
1316 // Add dependency to the rule for generating the xml permissions file
Paul Duffineedc5d52020-06-12 17:46:39 +01001317 ctx.AddDependency(module, xmlPermissionsFileTag, module.xmlPermissionsModuleName())
Paul Duffindfa131e2020-05-15 20:37:11 +01001318 }
Paul Duffin44f1d842020-06-26 20:17:02 +01001319 }
1320}
Paul Duffine74ac732020-02-06 13:51:46 +00001321
Paul Duffin44f1d842020-06-26 20:17:02 +01001322// Add other dependencies as normal.
1323func (module *SdkLibrary) DepsMutator(ctx android.BottomUpMutatorContext) {
Anton Hanssone77fccc2021-01-20 16:52:41 +00001324 var missingApiModules []string
1325 for _, apiScope := range module.getGeneratedApiScopes(ctx) {
1326 if apiScope.unstable {
1327 continue
1328 }
Paul Duffin958806b2022-05-16 13:10:47 +00001329 if m := module.latestApiModuleName(apiScope); !ctx.OtherModuleExists(m) {
Anton Hanssone77fccc2021-01-20 16:52:41 +00001330 missingApiModules = append(missingApiModules, m)
1331 }
Paul Duffin958806b2022-05-16 13:10:47 +00001332 if m := module.latestRemovedApiModuleName(apiScope); !ctx.OtherModuleExists(m) {
Anton Hanssone77fccc2021-01-20 16:52:41 +00001333 missingApiModules = append(missingApiModules, m)
1334 }
Paul Duffin958806b2022-05-16 13:10:47 +00001335 if m := module.latestIncompatibilitiesModuleName(apiScope); !ctx.OtherModuleExists(m) {
Jaewoong Jung1a97ee02021-03-09 13:25:02 -08001336 missingApiModules = append(missingApiModules, m)
1337 }
Anton Hanssone77fccc2021-01-20 16:52:41 +00001338 }
1339 if len(missingApiModules) != 0 && !module.sdkLibraryProperties.Unsafe_ignore_missing_latest_api {
1340 m := module.Name() + " is missing tracking files for previously released library versions.\n"
1341 m += "You need to do one of the following:\n"
1342 m += "- Add `unsafe_ignore_missing_latest_api: true` to your blueprint (to disable compat tracking)\n"
1343 m += "- Add a set of prebuilt txt files representing the last released version of this library for compat checking.\n"
1344 m += " (the current set of API files can be used as a seed for this compatibility tracking\n"
1345 m += "\n"
1346 m += "The following filegroup modules are missing:\n "
1347 m += strings.Join(missingApiModules, "\n ") + "\n"
1348 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."
1349 ctx.ModuleErrorf(m)
1350 }
Paul Duffin44f1d842020-06-26 20:17:02 +01001351 if module.requiresRuntimeImplementationLibrary() {
Paul Duffindfa131e2020-05-15 20:37:11 +01001352 // Only add the deps for the library if it is actually going to be built.
1353 module.Library.deps(ctx)
1354 }
Jiyong Parkc678ad32018-04-10 13:07:10 +09001355}
1356
Paul Duffin46dc45a2020-05-14 15:39:10 +01001357func (module *SdkLibrary) OutputFiles(tag string) (android.Paths, error) {
1358 paths, err := module.commonOutputFiles(tag)
Colin Cross4acaea92021-12-10 23:05:02 +00001359 if paths != nil || err != nil {
Paul Duffin46dc45a2020-05-14 15:39:10 +01001360 return paths, err
1361 }
Colin Cross4acaea92021-12-10 23:05:02 +00001362 if module.requiresRuntimeImplementationLibrary() {
1363 return module.Library.OutputFiles(tag)
1364 }
1365 if tag == "" {
1366 return nil, nil
1367 }
1368 return nil, fmt.Errorf("unsupported module reference tag %q", tag)
Paul Duffin46dc45a2020-05-14 15:39:10 +01001369}
1370
Inseob Kimc0907f12019-02-08 21:00:45 +09001371func (module *SdkLibrary) GenerateAndroidBuildActions(ctx android.ModuleContext) {
satayev8f088b02021-12-06 11:40:46 +00001372 if proptools.String(module.deviceProperties.Min_sdk_version) != "" {
1373 module.CheckMinSdkVersion(ctx)
1374 }
1375
Paul Duffina2ae7e02020-09-11 11:55:00 +01001376 module.generateCommonBuildActions(ctx)
1377
Paul Duffindfa131e2020-05-15 20:37:11 +01001378 // Only build an implementation library if required.
1379 if module.requiresRuntimeImplementationLibrary() {
Paul Duffin43db9be2019-12-30 17:35:49 +00001380 module.Library.GenerateAndroidBuildActions(ctx)
1381 }
Sundong Ahn054b19a2018-10-19 13:46:09 +09001382
Paul Duffinb97b1572021-04-29 21:50:40 +01001383 // Collate the components exported by this module. All scope specific modules are exported but
1384 // the impl and xml component modules are not.
1385 exportedComponents := map[string]struct{}{}
1386
Sundong Ahn57368eb2018-07-06 11:20:23 +09001387 // Record the paths to the header jars of the library (stubs and impl).
Paul Duffind1b3a922020-01-22 11:57:20 +00001388 // When this java_sdk_library is depended upon from others via "libs" property,
Jiyong Parkc678ad32018-04-10 13:07:10 +09001389 // the recorded paths will be returned depending on the link type of the caller.
1390 ctx.VisitDirectDeps(func(to android.Module) {
Jiyong Parkc678ad32018-04-10 13:07:10 +09001391 tag := ctx.OtherModuleDependencyTag(to)
1392
Paul Duffinc8782502020-04-29 20:45:27 +01001393 // Extract information from any of the scope specific dependencies.
1394 if scopeTag, ok := tag.(scopeDependencyTag); ok {
1395 apiScope := scopeTag.apiScope
Paul Duffin803a9562020-05-20 11:52:25 +01001396 scopePaths := module.getScopePathsCreateIfNeeded(apiScope)
Paul Duffinc8782502020-04-29 20:45:27 +01001397
1398 // Extract information from the dependency. The exact information extracted
1399 // is determined by the nature of the dependency which is determined by the tag.
1400 scopeTag.extractDepInfo(ctx, to, scopePaths)
Paul Duffinb97b1572021-04-29 21:50:40 +01001401
1402 exportedComponents[ctx.OtherModuleName(to)] = struct{}{}
Sundong Ahn20e998b2018-07-24 11:19:26 +09001403 }
Jiyong Parkc678ad32018-04-10 13:07:10 +09001404 })
Paul Duffinb97b1572021-04-29 21:50:40 +01001405
1406 // Make the set of components exported by this module available for use elsewhere.
Cole Faust18994c72023-02-28 16:02:16 -08001407 exportedComponentInfo := android.ExportedComponentsInfo{Components: android.SortedKeys(exportedComponents)}
Paul Duffinb97b1572021-04-29 21:50:40 +01001408 ctx.SetProvider(android.ExportedComponentsInfoProvider, exportedComponentInfo)
Paul Duffin958806b2022-05-16 13:10:47 +00001409
1410 // Provide additional information for inclusion in an sdk's generated .info file.
1411 additionalSdkInfo := map[string]interface{}{}
1412 additionalSdkInfo["dist_stem"] = module.distStem()
Paul Duffine8409952022-09-22 16:24:46 +01001413 baseModuleName := module.distStem()
Paul Duffin958806b2022-05-16 13:10:47 +00001414 scopes := map[string]interface{}{}
1415 additionalSdkInfo["scopes"] = scopes
1416 for scope, scopePaths := range module.scopePaths {
1417 scopeInfo := map[string]interface{}{}
1418 scopes[scope.name] = scopeInfo
1419 scopeInfo["current_api"] = scope.snapshotRelativeCurrentApiTxtPath(baseModuleName)
1420 scopeInfo["removed_api"] = scope.snapshotRelativeRemovedApiTxtPath(baseModuleName)
1421 if p := scopePaths.latestApiPath; p.Valid() {
1422 scopeInfo["latest_api"] = p.Path().String()
1423 }
1424 if p := scopePaths.latestRemovedApiPath; p.Valid() {
1425 scopeInfo["latest_removed_api"] = p.Path().String()
1426 }
1427 }
1428 ctx.SetProvider(android.AdditionalSdkInfoProvider, android.AdditionalSdkInfo{additionalSdkInfo})
Jiyong Parkc678ad32018-04-10 13:07:10 +09001429}
1430
Jiyong Park0b0e1b92019-12-03 13:24:29 +09001431func (module *SdkLibrary) AndroidMkEntries() []android.AndroidMkEntries {
Paul Duffindfa131e2020-05-15 20:37:11 +01001432 if !module.requiresRuntimeImplementationLibrary() {
Paul Duffin43db9be2019-12-30 17:35:49 +00001433 return nil
1434 }
Jiyong Park0b0e1b92019-12-03 13:24:29 +09001435 entriesList := module.Library.AndroidMkEntries()
Yo Chiang07d75072020-06-05 17:43:19 +08001436 if module.sharedLibrary() {
1437 entries := &entriesList[0]
1438 entries.Required = append(entries.Required, module.xmlPermissionsModuleName())
1439 }
Jiyong Park0b0e1b92019-12-03 13:24:29 +09001440 return entriesList
Jiyong Park82484c02018-04-23 21:41:26 +09001441}
1442
Anton Hansson5fd5d242020-03-27 19:43:19 +00001443// The dist path of the stub artifacts
1444func (module *SdkLibrary) apiDistPath(apiScope *apiScope) string {
Colin Crossf0eace92021-06-02 13:02:23 -07001445 return path.Join("apistubs", module.distGroup(), apiScope.name)
Anton Hansson5fd5d242020-03-27 19:43:19 +00001446}
1447
Paul Duffin12ceb462019-12-24 20:31:31 +00001448// Get the sdk version for use when compiling the stubs library.
Paul Duffin780c5f42020-05-12 15:52:55 +01001449func (module *SdkLibrary) sdkVersionForStubsLibrary(mctx android.EarlyModuleContext, apiScope *apiScope) string {
Paul Duffin87a05a32020-05-12 11:50:28 +01001450 scopeProperties := module.scopeToProperties[apiScope]
1451 if scopeProperties.Sdk_version != nil {
1452 return proptools.String(scopeProperties.Sdk_version)
1453 }
1454
Jiyong Parkf1691d22021-03-29 20:11:58 +09001455 sdkDep := decodeSdkDep(mctx, android.SdkContext(&module.Library))
Paul Duffin12ceb462019-12-24 20:31:31 +00001456 if sdkDep.hasStandardLibs() {
1457 // If building against a standard sdk then use the sdk version appropriate for the scope.
Paul Duffind1b3a922020-01-22 11:57:20 +00001458 return apiScope.sdkVersion
Paul Duffin12ceb462019-12-24 20:31:31 +00001459 } else {
1460 // Otherwise, use no system module.
1461 return "none"
1462 }
1463}
1464
Paul Duffin31310252020-11-20 21:26:20 +00001465func (module *SdkLibrary) distStem() string {
1466 return proptools.StringDefault(module.sdkLibraryProperties.Dist_stem, module.BaseModuleName())
1467}
1468
Colin Cross986b69a2021-06-01 13:13:40 -07001469// distGroup returns the subdirectory of the dist path of the stub artifacts.
1470func (module *SdkLibrary) distGroup() string {
Colin Cross59b92bf2021-06-01 14:07:56 -07001471 return proptools.StringDefault(module.sdkLibraryProperties.Dist_group, "unknown")
Colin Cross986b69a2021-06-01 13:13:40 -07001472}
1473
Paul Duffin958806b2022-05-16 13:10:47 +00001474func latestPrebuiltApiModuleName(name string, apiScope *apiScope) string {
1475 return PrebuiltApiModuleName(name, apiScope.name, "latest")
1476}
1477
Paul Duffind1b3a922020-01-22 11:57:20 +00001478func (module *SdkLibrary) latestApiFilegroupName(apiScope *apiScope) string {
Paul Duffin958806b2022-05-16 13:10:47 +00001479 return ":" + module.latestApiModuleName(apiScope)
1480}
1481
1482func (module *SdkLibrary) latestApiModuleName(apiScope *apiScope) string {
1483 return latestPrebuiltApiModuleName(module.distStem(), apiScope)
Jiyong Park58c518b2018-05-12 22:29:12 +09001484}
Jiyong Parkc678ad32018-04-10 13:07:10 +09001485
Paul Duffind1b3a922020-01-22 11:57:20 +00001486func (module *SdkLibrary) latestRemovedApiFilegroupName(apiScope *apiScope) string {
Paul Duffin958806b2022-05-16 13:10:47 +00001487 return ":" + module.latestRemovedApiModuleName(apiScope)
1488}
1489
1490func (module *SdkLibrary) latestRemovedApiModuleName(apiScope *apiScope) string {
1491 return latestPrebuiltApiModuleName(module.distStem()+"-removed", apiScope)
Jiyong Parkc678ad32018-04-10 13:07:10 +09001492}
1493
Jaewoong Jung1a97ee02021-03-09 13:25:02 -08001494func (module *SdkLibrary) latestIncompatibilitiesFilegroupName(apiScope *apiScope) string {
Paul Duffin958806b2022-05-16 13:10:47 +00001495 return ":" + module.latestIncompatibilitiesModuleName(apiScope)
1496}
1497
1498func (module *SdkLibrary) latestIncompatibilitiesModuleName(apiScope *apiScope) string {
1499 return latestPrebuiltApiModuleName(module.distStem()+"-incompatibilities", apiScope)
Jaewoong Jung1a97ee02021-03-09 13:25:02 -08001500}
1501
Jihoon Kang1c92c3e2023-03-23 17:44:51 +00001502func (module *SdkLibrary) contributesToApiSurface(c android.Config) bool {
1503 _, exists := c.GetApiLibraries()[module.Name()]
1504 return exists
1505}
1506
Anton Hansson944e77d2020-08-19 11:40:22 +01001507func childModuleVisibility(childVisibility []string) []string {
1508 if childVisibility == nil {
1509 // No child visibility set. The child will use the visibility of the sdk_library.
1510 return nil
1511 }
1512
1513 // Prepend an override to ignore the sdk_library's visibility, and rely on the child visibility.
1514 var visibility []string
1515 visibility = append(visibility, "//visibility:override")
1516 visibility = append(visibility, childVisibility...)
1517 return visibility
1518}
1519
Paul Duffin5df79302020-05-16 15:52:12 +01001520// Creates the implementation java library
1521func (module *SdkLibrary) createImplLibrary(mctx android.DefaultableHookContext) {
Anton Hansson944e77d2020-08-19 11:40:22 +01001522 visibility := childModuleVisibility(module.sdkLibraryProperties.Impl_library_visibility)
1523
Paul Duffin5df79302020-05-16 15:52:12 +01001524 props := struct {
Paul Duffin77590a82022-04-28 14:13:30 +00001525 Name *string
1526 Visibility []string
1527 Instrument bool
1528 Libs []string
1529 Static_libs []string
1530 Apex_available []string
Paul Duffin5df79302020-05-16 15:52:12 +01001531 }{
1532 Name: proptools.StringPtr(module.implLibraryModuleName()),
Anton Hansson944e77d2020-08-19 11:40:22 +01001533 Visibility: visibility,
Paul Duffin296cf332020-06-18 21:09:55 +01001534 // Set the instrument property to ensure it is instrumented when instrumentation is required.
1535 Instrument: true,
Anton Hansson7f66efa2020-10-08 14:47:23 +01001536 // Set the impl_only libs. Note that the module's "Libs" get appended as well, via the
1537 // addition of &module.properties below.
1538 Libs: module.sdkLibraryProperties.Impl_only_libs,
Paul Duffin77590a82022-04-28 14:13:30 +00001539 // Set the impl_only static libs. Note that the module's "static_libs" get appended as well, via the
1540 // addition of &module.properties below.
1541 Static_libs: module.sdkLibraryProperties.Impl_only_static_libs,
1542 // Pass the apex_available settings down so that the impl library can be statically
1543 // embedded within a library that is added to an APEX. Needed for updatable-media.
1544 Apex_available: module.ApexAvailable(),
Paul Duffin5df79302020-05-16 15:52:12 +01001545 }
1546
1547 properties := []interface{}{
1548 &module.properties,
1549 &module.protoProperties,
1550 &module.deviceProperties,
Liz Kammera7a64f32020-07-09 15:16:41 -07001551 &module.dexProperties,
Paul Duffin5df79302020-05-16 15:52:12 +01001552 &module.dexpreoptProperties,
Colin Cross014489c2020-06-02 20:09:13 -07001553 &module.linter.properties,
Paul Duffin5df79302020-05-16 15:52:12 +01001554 &props,
1555 module.sdkComponentPropertiesForChildLibrary(),
1556 }
1557 mctx.CreateModule(LibraryFactory, properties...)
1558}
1559
Jiyong Parkc678ad32018-04-10 13:07:10 +09001560// Creates a static java library that has API stubs
Paul Duffinf0229202020-04-29 16:47:28 +01001561func (module *SdkLibrary) createStubsLibrary(mctx android.DefaultableHookContext, apiScope *apiScope) {
Jiyong Parkc678ad32018-04-10 13:07:10 +09001562 props := struct {
Dan Willemsen9f435972020-05-28 15:28:00 -07001563 Name *string
1564 Visibility []string
1565 Srcs []string
1566 Installable *bool
1567 Sdk_version *string
1568 System_modules *string
1569 Patch_module *string
1570 Libs []string
Anton Hanssondae54cd2021-04-21 16:30:10 +01001571 Static_libs []string
Dan Willemsen9f435972020-05-28 15:28:00 -07001572 Compile_dex *bool
1573 Java_version *string
1574 Openjdk9 struct {
Sundong Ahn054b19a2018-10-19 13:46:09 +09001575 Srcs []string
1576 Javacflags []string
1577 }
Anton Hansson5fd5d242020-03-27 19:43:19 +00001578 Dist struct {
1579 Targets []string
1580 Dest *string
1581 Dir *string
1582 Tag *string
1583 }
Jiyong Parkc678ad32018-04-10 13:07:10 +09001584 }{}
1585
Paul Duffinc3091c82020-05-08 14:16:20 +01001586 props.Name = proptools.StringPtr(module.stubsLibraryModuleName(apiScope))
Anton Hansson944e77d2020-08-19 11:40:22 +01001587 props.Visibility = childModuleVisibility(module.sdkLibraryProperties.Stubs_library_visibility)
Jiyong Parkc678ad32018-04-10 13:07:10 +09001588 // sources are generated from the droiddoc
Paul Duffinc3091c82020-05-08 14:16:20 +01001589 props.Srcs = []string{":" + module.stubsSourceModuleName(apiScope)}
Paul Duffin12ceb462019-12-24 20:31:31 +00001590 sdkVersion := module.sdkVersionForStubsLibrary(mctx, apiScope)
Paul Duffin52d398a2019-06-11 12:31:14 +01001591 props.Sdk_version = proptools.StringPtr(sdkVersion)
Paul Duffina18abc22020-05-16 18:54:24 +01001592 props.System_modules = module.deviceProperties.System_modules
1593 props.Patch_module = module.properties.Patch_module
Paul Duffin367ab912019-12-23 19:40:36 +00001594 props.Installable = proptools.BoolPtr(false)
Sundong Ahn054b19a2018-10-19 13:46:09 +09001595 props.Libs = module.sdkLibraryProperties.Stub_only_libs
Anton Hanssondae54cd2021-04-21 16:30:10 +01001596 props.Static_libs = module.sdkLibraryProperties.Stub_only_static_libs
Paul Duffine22c2ab2020-05-20 19:35:27 +01001597 // The stub-annotations library contains special versions of the annotations
1598 // with CLASS retention policy, so that they're kept.
1599 if proptools.Bool(module.sdkLibraryProperties.Annotations_enabled) {
1600 props.Libs = append(props.Libs, "stub-annotations")
1601 }
Paul Duffina18abc22020-05-16 18:54:24 +01001602 props.Openjdk9.Srcs = module.properties.Openjdk9.Srcs
1603 props.Openjdk9.Javacflags = module.properties.Openjdk9.Javacflags
Anton Hansson83509b52020-05-21 09:21:57 +01001604 // We compile the stubs for 1.8 in line with the main android.jar stubs, and potential
1605 // interop with older developer tools that don't support 1.9.
1606 props.Java_version = proptools.StringPtr("1.8")
Paul Duffinf4600f62021-05-13 22:34:45 +01001607
1608 // The imports need to be compiled to dex if the java_sdk_library requests it.
1609 compileDex := module.dexProperties.Compile_dex
1610 if module.stubLibrariesCompiledForDex() {
1611 compileDex = proptools.BoolPtr(true)
Sundong Ahndd567f92018-07-31 17:19:11 +09001612 }
Paul Duffinf4600f62021-05-13 22:34:45 +01001613 props.Compile_dex = compileDex
Jiyong Parkc678ad32018-04-10 13:07:10 +09001614
Anton Hansson5fd5d242020-03-27 19:43:19 +00001615 // Dist the class jar artifact for sdk builds.
1616 if !Bool(module.sdkLibraryProperties.No_dist) {
1617 props.Dist.Targets = []string{"sdk", "win_sdk"}
Paul Duffin31310252020-11-20 21:26:20 +00001618 props.Dist.Dest = proptools.StringPtr(fmt.Sprintf("%v.jar", module.distStem()))
Anton Hansson5fd5d242020-03-27 19:43:19 +00001619 props.Dist.Dir = proptools.StringPtr(module.apiDistPath(apiScope))
1620 props.Dist.Tag = proptools.StringPtr(".jar")
1621 }
Jiyong Parkc678ad32018-04-10 13:07:10 +09001622
Paul Duffin859fe962020-05-15 10:20:31 +01001623 mctx.CreateModule(LibraryFactory, &props, module.sdkComponentPropertiesForChildLibrary())
Jiyong Parkc678ad32018-04-10 13:07:10 +09001624}
1625
Paul Duffin6d0886e2020-04-07 18:49:53 +01001626// Creates a droidstubs module that creates stubs source files from the given full source
Paul Duffinc8782502020-04-29 20:45:27 +01001627// files and also updates and checks the API specification files.
Paul Duffin15f34ef2020-07-20 18:04:44 +01001628func (module *SdkLibrary) createStubsSourcesAndApi(mctx android.DefaultableHookContext, apiScope *apiScope, name string, scopeSpecificDroidstubsArgs []string) {
Jiyong Parkc678ad32018-04-10 13:07:10 +09001629 props := struct {
Sundong Ahn054b19a2018-10-19 13:46:09 +09001630 Name *string
Paul Duffin4911a892020-04-29 23:35:13 +01001631 Visibility []string
Sundong Ahn054b19a2018-10-19 13:46:09 +09001632 Srcs []string
1633 Installable *bool
Paul Duffin52d398a2019-06-11 12:31:14 +01001634 Sdk_version *string
Jihoon Kang3198f3c2023-01-26 08:08:52 +00001635 Api_surface *string
Paul Duffin12ceb462019-12-24 20:31:31 +00001636 System_modules *string
Sundong Ahn054b19a2018-10-19 13:46:09 +09001637 Libs []string
Paul Duffin6877e6d2020-09-25 19:59:14 +01001638 Output_javadoc_comments *bool
Paul Duffin11512472019-02-11 15:55:17 +00001639 Arg_files []string
Sundong Ahn054b19a2018-10-19 13:46:09 +09001640 Args *string
Sundong Ahn054b19a2018-10-19 13:46:09 +09001641 Java_version *string
Paul Duffine22c2ab2020-05-20 19:35:27 +01001642 Annotations_enabled *bool
Sundong Ahn054b19a2018-10-19 13:46:09 +09001643 Merge_annotations_dirs []string
1644 Merge_inclusion_annotations_dirs []string
Paul Duffin0ff08bd2020-04-29 13:30:54 +01001645 Generate_stubs *bool
Anton Hanssone87b03d2020-12-21 15:29:34 +00001646 Previous_api *string
Sundong Ahn054b19a2018-10-19 13:46:09 +09001647 Check_api struct {
Anton Hanssone6056152020-12-31 10:37:27 +00001648 Current ApiToCheck
1649 Last_released ApiToCheck
Paul Duffin160fe412020-05-10 19:32:20 +01001650
1651 Api_lint struct {
1652 Enabled *bool
1653 New_since *string
1654 Baseline_file *string
1655 }
Jiyong Park58c518b2018-05-12 22:29:12 +09001656 }
Sundong Ahn1b92c822018-05-29 11:35:17 +09001657 Aidl struct {
1658 Include_dirs []string
1659 Local_include_dirs []string
1660 }
Paul Duffin040e9062020-11-23 17:41:36 +00001661 Dists []android.Dist
Jiyong Parkc678ad32018-04-10 13:07:10 +09001662 }{}
1663
Paul Duffin7b78b4d2020-04-28 14:08:32 +01001664 // The stubs source processing uses the same compile time classpath when extracting the
1665 // API from the implementation library as it does when compiling it. i.e. the same
1666 // * sdk version
1667 // * system_modules
1668 // * libs (static_libs/libs)
Paul Duffin250e6192019-06-07 10:44:37 +01001669
Paul Duffin0ff08bd2020-04-29 13:30:54 +01001670 props.Name = proptools.StringPtr(name)
Anton Hansson944e77d2020-08-19 11:40:22 +01001671 props.Visibility = childModuleVisibility(module.sdkLibraryProperties.Stubs_source_visibility)
Paul Duffina18abc22020-05-16 18:54:24 +01001672 props.Srcs = append(props.Srcs, module.properties.Srcs...)
Anton Hanssonf8ea3722021-09-16 14:24:13 +01001673 props.Srcs = append(props.Srcs, module.sdkLibraryProperties.Api_srcs...)
Paul Duffina18abc22020-05-16 18:54:24 +01001674 props.Sdk_version = module.deviceProperties.Sdk_version
Jihoon Kang3198f3c2023-01-26 08:08:52 +00001675 props.Api_surface = &apiScope.name
Paul Duffina18abc22020-05-16 18:54:24 +01001676 props.System_modules = module.deviceProperties.System_modules
Jiyong Parkc678ad32018-04-10 13:07:10 +09001677 props.Installable = proptools.BoolPtr(false)
Sundong Ahne6f0b052018-06-05 16:46:14 +09001678 // A droiddoc module has only one Libs property and doesn't distinguish between
1679 // shared libs and static libs. So we need to add both of these libs to Libs property.
Paul Duffina18abc22020-05-16 18:54:24 +01001680 props.Libs = module.properties.Libs
1681 props.Libs = append(props.Libs, module.properties.Static_libs...)
Nikita Ioffed732da72022-11-21 12:38:25 +00001682 props.Libs = append(props.Libs, module.sdkLibraryProperties.Stub_only_libs...)
Paul Duffina18abc22020-05-16 18:54:24 +01001683 props.Aidl.Include_dirs = module.deviceProperties.Aidl.Include_dirs
1684 props.Aidl.Local_include_dirs = module.deviceProperties.Aidl.Local_include_dirs
1685 props.Java_version = module.properties.Java_version
Jiyong Parkc678ad32018-04-10 13:07:10 +09001686
Paul Duffine22c2ab2020-05-20 19:35:27 +01001687 props.Annotations_enabled = module.sdkLibraryProperties.Annotations_enabled
Sundong Ahn054b19a2018-10-19 13:46:09 +09001688 props.Merge_annotations_dirs = module.sdkLibraryProperties.Merge_annotations_dirs
1689 props.Merge_inclusion_annotations_dirs = module.sdkLibraryProperties.Merge_inclusion_annotations_dirs
1690
Paul Duffin6d0886e2020-04-07 18:49:53 +01001691 droidstubsArgs := []string{}
Paul Duffin235ffff2019-12-24 10:41:30 +00001692 if len(module.sdkLibraryProperties.Api_packages) != 0 {
Paul Duffin6d0886e2020-04-07 18:49:53 +01001693 droidstubsArgs = append(droidstubsArgs, "--stub-packages "+strings.Join(module.sdkLibraryProperties.Api_packages, ":"))
Paul Duffin235ffff2019-12-24 10:41:30 +00001694 }
1695 if len(module.sdkLibraryProperties.Hidden_api_packages) != 0 {
Paul Duffin6d0886e2020-04-07 18:49:53 +01001696 droidstubsArgs = append(droidstubsArgs,
Paul Duffin235ffff2019-12-24 10:41:30 +00001697 android.JoinWithPrefix(module.sdkLibraryProperties.Hidden_api_packages, " --hide-package "))
1698 }
Paul Duffin6d0886e2020-04-07 18:49:53 +01001699 droidstubsArgs = append(droidstubsArgs, module.sdkLibraryProperties.Droiddoc_options...)
Paul Duffin235ffff2019-12-24 10:41:30 +00001700 disabledWarnings := []string{
Paul Duffin235ffff2019-12-24 10:41:30 +00001701 "BroadcastBehavior",
Paul Duffin235ffff2019-12-24 10:41:30 +00001702 "DeprecationMismatch",
Anton Hansson3c0779a2022-02-18 19:24:30 +00001703 "HiddenSuperclass",
Paul Duffin235ffff2019-12-24 10:41:30 +00001704 "HiddenTypeParameter",
Anton Hansson3c0779a2022-02-18 19:24:30 +00001705 "MissingPermission",
1706 "SdkConstant",
Paul Duffin235ffff2019-12-24 10:41:30 +00001707 "Todo",
1708 "Typo",
Anton Hansson3c0779a2022-02-18 19:24:30 +00001709 "UnavailableSymbol",
Paul Duffin235ffff2019-12-24 10:41:30 +00001710 }
Paul Duffin6d0886e2020-04-07 18:49:53 +01001711 droidstubsArgs = append(droidstubsArgs, android.JoinWithPrefix(disabledWarnings, "--hide "))
Sundong Ahnfb2721f2018-09-17 13:23:09 +09001712
Paul Duffin6877e6d2020-09-25 19:59:14 +01001713 // Output Javadoc comments for public scope.
1714 if apiScope == apiScopePublic {
1715 props.Output_javadoc_comments = proptools.BoolPtr(true)
1716 }
1717
Paul Duffin1fb487d2020-04-07 18:50:10 +01001718 // Add in scope specific arguments.
Paul Duffin0ff08bd2020-04-29 13:30:54 +01001719 droidstubsArgs = append(droidstubsArgs, scopeSpecificDroidstubsArgs...)
Paul Duffin11512472019-02-11 15:55:17 +00001720 props.Arg_files = module.sdkLibraryProperties.Droiddoc_option_files
Paul Duffin6d0886e2020-04-07 18:49:53 +01001721 props.Args = proptools.StringPtr(strings.Join(droidstubsArgs, " "))
Jiyong Parkc678ad32018-04-10 13:07:10 +09001722
Paul Duffin15f34ef2020-07-20 18:04:44 +01001723 // List of APIs identified from the provided source files are created. They are later
1724 // compared against to the not-yet-released (a.k.a current) list of APIs and to the
1725 // last-released (a.k.a numbered) list of API.
1726 currentApiFileName := apiScope.apiFilePrefix + "current.txt"
1727 removedApiFileName := apiScope.apiFilePrefix + "removed.txt"
1728 apiDir := module.getApiDir()
1729 currentApiFileName = path.Join(apiDir, currentApiFileName)
1730 removedApiFileName = path.Join(apiDir, removedApiFileName)
Jiyong Parkc678ad32018-04-10 13:07:10 +09001731
Paul Duffin15f34ef2020-07-20 18:04:44 +01001732 // check against the not-yet-release API
1733 props.Check_api.Current.Api_file = proptools.StringPtr(currentApiFileName)
1734 props.Check_api.Current.Removed_api_file = proptools.StringPtr(removedApiFileName)
Jiyong Park58c518b2018-05-12 22:29:12 +09001735
Paul Duffin958806b2022-05-16 13:10:47 +00001736 if module.compareAgainstLatestApi(apiScope) {
Paul Duffin15f34ef2020-07-20 18:04:44 +01001737 // check against the latest released API
1738 latestApiFilegroupName := proptools.StringPtr(module.latestApiFilegroupName(apiScope))
Anton Hanssone87b03d2020-12-21 15:29:34 +00001739 props.Previous_api = latestApiFilegroupName
Paul Duffin15f34ef2020-07-20 18:04:44 +01001740 props.Check_api.Last_released.Api_file = latestApiFilegroupName
1741 props.Check_api.Last_released.Removed_api_file = proptools.StringPtr(
1742 module.latestRemovedApiFilegroupName(apiScope))
Jaewoong Jung1a97ee02021-03-09 13:25:02 -08001743 props.Check_api.Last_released.Baseline_file = proptools.StringPtr(
1744 module.latestIncompatibilitiesFilegroupName(apiScope))
Paul Duffin160fe412020-05-10 19:32:20 +01001745
Paul Duffin15f34ef2020-07-20 18:04:44 +01001746 if proptools.Bool(module.sdkLibraryProperties.Api_lint.Enabled) {
1747 // Enable api lint.
1748 props.Check_api.Api_lint.Enabled = proptools.BoolPtr(true)
1749 props.Check_api.Api_lint.New_since = latestApiFilegroupName
Paul Duffin160fe412020-05-10 19:32:20 +01001750
Paul Duffin15f34ef2020-07-20 18:04:44 +01001751 // If it exists then pass a lint-baseline.txt through to droidstubs.
1752 baselinePath := path.Join(apiDir, apiScope.apiFilePrefix+"lint-baseline.txt")
1753 baselinePathRelativeToRoot := path.Join(mctx.ModuleDir(), baselinePath)
1754 paths, err := mctx.GlobWithDeps(baselinePathRelativeToRoot, nil)
1755 if err != nil {
1756 mctx.ModuleErrorf("error checking for presence of %s: %s", baselinePathRelativeToRoot, err)
1757 }
1758 if len(paths) == 1 {
1759 props.Check_api.Api_lint.Baseline_file = proptools.StringPtr(baselinePath)
1760 } else if len(paths) != 0 {
1761 mctx.ModuleErrorf("error checking for presence of %s: expected one path, found: %v", baselinePathRelativeToRoot, paths)
Paul Duffin160fe412020-05-10 19:32:20 +01001762 }
1763 }
Paul Duffin15f34ef2020-07-20 18:04:44 +01001764 }
Jiyong Park58c518b2018-05-12 22:29:12 +09001765
Paul Duffin15f34ef2020-07-20 18:04:44 +01001766 if !Bool(module.sdkLibraryProperties.No_dist) {
Paul Duffin040e9062020-11-23 17:41:36 +00001767 // Dist the api txt and removed api txt artifacts for sdk builds.
1768 distDir := proptools.StringPtr(path.Join(module.apiDistPath(apiScope), "api"))
1769 for _, p := range []struct {
1770 tag string
1771 pattern string
1772 }{
1773 {tag: ".api.txt", pattern: "%s.txt"},
1774 {tag: ".removed-api.txt", pattern: "%s-removed.txt"},
1775 } {
1776 props.Dists = append(props.Dists, android.Dist{
1777 Targets: []string{"sdk", "win_sdk"},
1778 Dir: distDir,
1779 Dest: proptools.StringPtr(fmt.Sprintf(p.pattern, module.distStem())),
1780 Tag: proptools.StringPtr(p.tag),
1781 })
1782 }
Anton Hansson5fd5d242020-03-27 19:43:19 +00001783 }
1784
Jihoon Kangd48abd52023-02-02 22:32:31 +00001785 mctx.CreateModule(DroidstubsFactory, &props).(*Droidstubs).CallHookIfAvailable(mctx)
Jiyong Parkc678ad32018-04-10 13:07:10 +09001786}
1787
Jihoon Kang1c92c3e2023-03-23 17:44:51 +00001788func (module *SdkLibrary) createApiLibrary(mctx android.DefaultableHookContext, apiScope *apiScope) {
1789 props := struct {
1790 Name *string
1791 Visibility []string
1792 Api_contributions []string
1793 Libs []string
1794 Static_libs []string
1795 Dep_api_srcs *string
1796 }{}
1797
1798 props.Name = proptools.StringPtr(module.apiLibraryModuleName(apiScope))
1799 props.Visibility = childModuleVisibility(module.sdkLibraryProperties.Stubs_library_visibility)
1800
1801 apiContributions := []string{}
1802
1803 // Api surfaces are not independent of each other, but have subset relationships,
1804 // and so does the api files. To generate from-text stubs for api surfaces other than public,
1805 // all subset api domains' api_contriubtions must be added as well.
1806 scope := apiScope
1807 for scope != nil {
1808 apiContributions = append(apiContributions, module.stubsSourceModuleName(scope)+".api.contribution")
1809 scope = scope.extends
1810 }
1811
1812 props.Api_contributions = apiContributions
1813 props.Libs = module.properties.Libs
1814 props.Libs = append(props.Libs, module.sdkLibraryProperties.Stub_only_libs...)
1815 props.Libs = append(props.Libs, "stub-annotations")
1816 props.Static_libs = module.sdkLibraryProperties.Stub_only_static_libs
1817 props.Dep_api_srcs = proptools.StringPtr(apiScope.kind.DefaultJavaLibraryName() + ".from-text")
1818
1819 // android_module_lib_stubs_current.from-text only comprises api contributions from art, conscrypt and i18n.
1820 // Thus, replace with android_module_lib_stubs_current_full.from-text, which comprises every api domains.
1821 if apiScope.kind == android.SdkModule {
1822 props.Dep_api_srcs = proptools.StringPtr(apiScope.kind.DefaultJavaLibraryName() + "_full.from-text")
1823 }
1824
1825 mctx.CreateModule(ApiLibraryFactory, &props)
1826}
1827
Paul Duffin958806b2022-05-16 13:10:47 +00001828func (module *SdkLibrary) compareAgainstLatestApi(apiScope *apiScope) bool {
1829 return !(apiScope.unstable || module.sdkLibraryProperties.Unsafe_ignore_missing_latest_api)
1830}
1831
Paul Duffinea8f8082021-06-24 13:25:57 +01001832// Implements android.ApexModule
Jooyung Han5e9013b2020-03-10 06:23:13 +09001833func (module *SdkLibrary) DepIsInSameApex(mctx android.BaseModuleContext, dep android.Module) bool {
1834 depTag := mctx.OtherModuleDependencyTag(dep)
1835 if depTag == xmlPermissionsFileTag {
1836 return true
1837 }
1838 return module.Library.DepIsInSameApex(mctx, dep)
1839}
1840
Paul Duffinea8f8082021-06-24 13:25:57 +01001841// Implements android.ApexModule
1842func (module *SdkLibrary) UniqueApexVariations() bool {
1843 return module.uniqueApexVariations()
1844}
1845
Jiyong Parkc678ad32018-04-10 13:07:10 +09001846// Creates the xml file that publicizes the runtime library
Paul Duffinf0229202020-04-29 16:47:28 +01001847func (module *SdkLibrary) createXmlFile(mctx android.DefaultableHookContext) {
Spandan Das8c9ae7e2023-03-03 21:20:36 +00001848 moduleMinApiLevel := module.Library.MinSdkVersion(mctx)
Pedro Loureiroc3621422021-09-28 15:40:23 +00001849 var moduleMinApiLevelStr = moduleMinApiLevel.String()
1850 if moduleMinApiLevel == android.NoneApiLevel {
1851 moduleMinApiLevelStr = "current"
1852 }
Jiyong Parke3833882020-02-17 17:28:10 +09001853 props := struct {
Pedro Loureiroc3621422021-09-28 15:40:23 +00001854 Name *string
1855 Lib_name *string
1856 Apex_available []string
1857 On_bootclasspath_since *string
1858 On_bootclasspath_before *string
1859 Min_device_sdk *string
1860 Max_device_sdk *string
1861 Sdk_library_min_api_level *string
Jiyong Parke3833882020-02-17 17:28:10 +09001862 }{
Pedro Loureiroc3621422021-09-28 15:40:23 +00001863 Name: proptools.StringPtr(module.xmlPermissionsModuleName()),
1864 Lib_name: proptools.StringPtr(module.BaseModuleName()),
1865 Apex_available: module.ApexProperties.Apex_available,
1866 On_bootclasspath_since: module.commonSdkLibraryProperties.On_bootclasspath_since,
1867 On_bootclasspath_before: module.commonSdkLibraryProperties.On_bootclasspath_before,
1868 Min_device_sdk: module.commonSdkLibraryProperties.Min_device_sdk,
1869 Max_device_sdk: module.commonSdkLibraryProperties.Max_device_sdk,
1870 Sdk_library_min_api_level: &moduleMinApiLevelStr,
Jiyong Parkc678ad32018-04-10 13:07:10 +09001871 }
Jiyong Parke3833882020-02-17 17:28:10 +09001872
Jiyong Parke3833882020-02-17 17:28:10 +09001873 mctx.CreateModule(sdkLibraryXmlFactory, &props)
Jiyong Parkc678ad32018-04-10 13:07:10 +09001874}
1875
Jiyong Parkf1691d22021-03-29 20:11:58 +09001876func PrebuiltJars(ctx android.BaseModuleContext, baseName string, s android.SdkSpec) android.Paths {
Jiyong Park54105c42021-03-31 18:17:53 +09001877 var ver android.ApiLevel
Jiyong Parkf1691d22021-03-29 20:11:58 +09001878 var kind android.SdkKind
1879 if s.UsePrebuilt(ctx) {
Jiyong Park54105c42021-03-31 18:17:53 +09001880 ver = s.ApiLevel
Jiyong Parkf1691d22021-03-29 20:11:58 +09001881 kind = s.Kind
Jiyong Parkc678ad32018-04-10 13:07:10 +09001882 } else {
Jiyong Park6a927c42020-01-21 02:03:43 +09001883 // We don't have prebuilt SDK for the specific sdkVersion.
1884 // Instead of breaking the build, fallback to use "system_current"
Jiyong Park54105c42021-03-31 18:17:53 +09001885 ver = android.FutureApiLevel
Jiyong Parkf1691d22021-03-29 20:11:58 +09001886 kind = android.SdkSystem
Sundong Ahn054b19a2018-10-19 13:46:09 +09001887 }
Jiyong Park6a927c42020-01-21 02:03:43 +09001888
1889 dir := filepath.Join("prebuilts", "sdk", ver.String(), kind.String())
Paul Duffin50061512020-01-21 16:31:05 +00001890 jar := filepath.Join(dir, baseName+".jar")
Sundong Ahn054b19a2018-10-19 13:46:09 +09001891 jarPath := android.ExistentPathForSource(ctx, jar)
Sundong Ahnae418ac2019-02-28 15:01:28 +09001892 if !jarPath.Valid() {
Colin Cross07c88562020-01-07 09:34:44 -08001893 if ctx.Config().AllowMissingDependencies() {
1894 return android.Paths{android.PathForSource(ctx, jar)}
1895 } else {
Jiyong Parkf1691d22021-03-29 20:11:58 +09001896 ctx.PropertyErrorf("sdk_library", "invalid sdk version %q, %q does not exist", s.Raw, jar)
Colin Cross07c88562020-01-07 09:34:44 -08001897 }
Sundong Ahnae418ac2019-02-28 15:01:28 +09001898 return nil
1899 }
Sundong Ahn054b19a2018-10-19 13:46:09 +09001900 return android.Paths{jarPath.Path()}
1901}
1902
Colin Crossaede88c2020-08-11 12:17:01 -07001903// 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 +01001904//
1905// If either this or the other module are on the platform then this will return
1906// false.
Colin Cross56a83212020-09-15 18:30:11 -07001907func withinSameApexesAs(ctx android.BaseModuleContext, other android.Module) bool {
1908 apexInfo := ctx.Provider(android.ApexInfoProvider).(android.ApexInfo)
1909 otherApexInfo := ctx.OtherModuleProvider(other, android.ApexInfoProvider).(android.ApexInfo)
Jiyong Parkab50b072021-05-12 17:13:56 +09001910 return len(otherApexInfo.InApexVariants) > 0 && reflect.DeepEqual(apexInfo.InApexVariants, otherApexInfo.InApexVariants)
Paul Duffin9b879592020-05-26 13:21:35 +01001911}
1912
Jiyong Parkf1691d22021-03-29 20:11:58 +09001913func (module *SdkLibrary) sdkJars(ctx android.BaseModuleContext, sdkVersion android.SdkSpec, headerJars bool) android.Paths {
Jiyong Park932cdfe2020-05-28 00:19:53 +09001914 // If the client doesn't set sdk_version, but if this library prefers stubs over
1915 // the impl library, let's provide the widest API surface possible. To do so,
1916 // force override sdk_version to module_current so that the closest possible API
1917 // surface could be found in selectHeaderJarsForSdkVersion
Jiyong Parkf1691d22021-03-29 20:11:58 +09001918 if module.defaultsToStubs() && !sdkVersion.Specified() {
Jiyong Park92315372021-04-02 08:45:46 +09001919 sdkVersion = android.SdkSpecFrom(ctx, "module_current")
Jiyong Park932cdfe2020-05-28 00:19:53 +09001920 }
Paul Duffind1b3a922020-01-22 11:57:20 +00001921
Paul Duffindaaa3322020-05-26 18:13:57 +01001922 // Only provide access to the implementation library if it is actually built.
1923 if module.requiresRuntimeImplementationLibrary() {
1924 // Check any special cases for java_sdk_library.
1925 //
1926 // Only allow access to the implementation library in the following condition:
1927 // * No sdk_version specified on the referencing module.
Paul Duffin9b879592020-05-26 13:21:35 +01001928 // * The referencing module is in the same apex as this.
Jiyong Parkf1691d22021-03-29 20:11:58 +09001929 if sdkVersion.Kind == android.SdkPrivate || withinSameApexesAs(ctx, module) {
Paul Duffindaaa3322020-05-26 18:13:57 +01001930 if headerJars {
1931 return module.HeaderJars()
1932 } else {
1933 return module.ImplementationJars()
1934 }
Sundong Ahn054b19a2018-10-19 13:46:09 +09001935 }
Jiyong Parkc678ad32018-04-10 13:07:10 +09001936 }
Paul Duffinb05d4292020-05-20 12:19:10 +01001937
Paul Duffin23970f42020-05-20 14:20:02 +01001938 return module.selectHeaderJarsForSdkVersion(ctx, sdkVersion)
Jiyong Parkc678ad32018-04-10 13:07:10 +09001939}
1940
Sundong Ahn241cd372018-07-13 16:16:44 +09001941// to satisfy SdkLibraryDependency interface
Jiyong Parkf1691d22021-03-29 20:11:58 +09001942func (module *SdkLibrary) SdkHeaderJars(ctx android.BaseModuleContext, sdkVersion android.SdkSpec) android.Paths {
Paul Duffind1b3a922020-01-22 11:57:20 +00001943 return module.sdkJars(ctx, sdkVersion, true /*headerJars*/)
1944}
1945
1946// to satisfy SdkLibraryDependency interface
Jiyong Parkf1691d22021-03-29 20:11:58 +09001947func (module *SdkLibrary) SdkImplementationJars(ctx android.BaseModuleContext, sdkVersion android.SdkSpec) android.Paths {
Paul Duffind1b3a922020-01-22 11:57:20 +00001948 return module.sdkJars(ctx, sdkVersion, false /*headerJars*/)
Sundong Ahn241cd372018-07-13 16:16:44 +09001949}
1950
Colin Cross571cccf2019-02-04 11:22:08 -08001951var javaSdkLibrariesKey = android.NewOnceKey("javaSdkLibraries")
1952
Jiyong Park82484c02018-04-23 21:41:26 +09001953func javaSdkLibraries(config android.Config) *[]string {
Colin Cross571cccf2019-02-04 11:22:08 -08001954 return config.Once(javaSdkLibrariesKey, func() interface{} {
Jiyong Park82484c02018-04-23 21:41:26 +09001955 return &[]string{}
1956 }).(*[]string)
1957}
1958
Paul Duffin749f98f2019-12-30 17:23:46 +00001959func (module *SdkLibrary) getApiDir() string {
1960 return proptools.StringDefault(module.sdkLibraryProperties.Api_dir, "api")
1961}
1962
Jiyong Parkc678ad32018-04-10 13:07:10 +09001963// For a java_sdk_library module, create internal modules for stubs, docs,
1964// runtime libs and xml file. If requested, the stubs and docs are created twice
1965// once for public API level and once for system API level
Paul Duffinf0229202020-04-29 16:47:28 +01001966func (module *SdkLibrary) CreateInternalModules(mctx android.DefaultableHookContext) {
1967 // If the module has been disabled then don't create any child modules.
1968 if !module.Enabled() {
1969 return
1970 }
1971
Paul Duffina18abc22020-05-16 18:54:24 +01001972 if len(module.properties.Srcs) == 0 {
Inseob Kimc0907f12019-02-08 21:00:45 +09001973 mctx.PropertyErrorf("srcs", "java_sdk_library must specify srcs")
Jooyung Han58f26ab2019-12-18 15:34:32 +09001974 return
Inseob Kimc0907f12019-02-08 21:00:45 +09001975 }
1976
Paul Duffin37e0b772019-12-30 17:20:10 +00001977 // If this builds against standard libraries (i.e. is not part of the core libraries)
Paul Duffin4f5c1ef2020-11-19 14:53:43 +00001978 // then assume it provides both system and test apis.
Jiyong Parkf1691d22021-03-29 20:11:58 +09001979 sdkDep := decodeSdkDep(mctx, android.SdkContext(&module.Library))
Paul Duffin37e0b772019-12-30 17:20:10 +00001980 hasSystemAndTestApis := sdkDep.hasStandardLibs()
Paul Duffin3375e352020-04-28 10:44:03 +01001981 module.sdkLibraryProperties.Generate_system_and_test_apis = hasSystemAndTestApis
Paul Duffin4f5c1ef2020-11-19 14:53:43 +00001982
Jaewoong Jung18aefc12020-12-21 09:11:10 -08001983 missingCurrentApi := false
Inseob Kim8098faa2019-03-18 10:19:51 +09001984
Paul Duffin3375e352020-04-28 10:44:03 +01001985 generatedScopes := module.getGeneratedApiScopes(mctx)
Paul Duffind1b3a922020-01-22 11:57:20 +00001986
Paul Duffin749f98f2019-12-30 17:23:46 +00001987 apiDir := module.getApiDir()
Paul Duffin3375e352020-04-28 10:44:03 +01001988 for _, scope := range generatedScopes {
Inseob Kim8098faa2019-03-18 10:19:51 +09001989 for _, api := range []string{"current.txt", "removed.txt"} {
Paul Duffind1b3a922020-01-22 11:57:20 +00001990 path := path.Join(mctx.ModuleDir(), apiDir, scope.apiFilePrefix+api)
Inseob Kim8098faa2019-03-18 10:19:51 +09001991 p := android.ExistentPathForSource(mctx, path)
1992 if !p.Valid() {
Colin Cross18f840c2021-05-20 17:56:54 -07001993 if mctx.Config().AllowMissingDependencies() {
1994 mctx.AddMissingDependencies([]string{path})
1995 } else {
1996 mctx.ModuleErrorf("Current api file %#v doesn't exist", path)
1997 missingCurrentApi = true
1998 }
Inseob Kim8098faa2019-03-18 10:19:51 +09001999 }
2000 }
2001 }
2002
Jaewoong Jung18aefc12020-12-21 09:11:10 -08002003 if missingCurrentApi {
Inseob Kim8098faa2019-03-18 10:19:51 +09002004 script := "build/soong/scripts/gen-java-current-api-files.sh"
2005 p := android.ExistentPathForSource(mctx, script)
2006
2007 if !p.Valid() {
2008 panic(fmt.Sprintf("script file %s doesn't exist", script))
2009 }
2010
2011 mctx.ModuleErrorf("One or more current api files are missing. "+
2012 "You can update them by:\n"+
Paul Duffin37e0b772019-12-30 17:20:10 +00002013 "%s %q %s && m update-api",
Paul Duffind1b3a922020-01-22 11:57:20 +00002014 script, filepath.Join(mctx.ModuleDir(), apiDir),
Paul Duffin3375e352020-04-28 10:44:03 +01002015 strings.Join(generatedScopes.Strings(func(s *apiScope) string { return s.apiFilePrefix }), " "))
Inseob Kim8098faa2019-03-18 10:19:51 +09002016 return
2017 }
2018
Paul Duffin3375e352020-04-28 10:44:03 +01002019 for _, scope := range generatedScopes {
Paul Duffin15f34ef2020-07-20 18:04:44 +01002020 // Use the stubs source name for legacy reasons.
2021 module.createStubsSourcesAndApi(mctx, scope, module.stubsSourceModuleName(scope), scope.droidstubsArgs)
Paul Duffin0ff08bd2020-04-29 13:30:54 +01002022
Paul Duffind1b3a922020-01-22 11:57:20 +00002023 module.createStubsLibrary(mctx, scope)
Jihoon Kang1c92c3e2023-03-23 17:44:51 +00002024
2025 if module.contributesToApiSurface(mctx.Config()) {
2026 module.createApiLibrary(mctx, scope)
2027 }
Inseob Kimc0907f12019-02-08 21:00:45 +09002028 }
2029
Paul Duffindfa131e2020-05-15 20:37:11 +01002030 if module.requiresRuntimeImplementationLibrary() {
Paul Duffin5df79302020-05-16 15:52:12 +01002031 // Create child module to create an implementation library.
2032 //
2033 // This temporarily creates a second implementation library that can be explicitly
2034 // referenced.
2035 //
2036 // TODO(b/156618935) - update comment once only one implementation library is created.
2037 module.createImplLibrary(mctx)
2038
Paul Duffindfa131e2020-05-15 20:37:11 +01002039 // Only create an XML permissions file that declares the library as being usable
2040 // as a shared library if required.
2041 if module.sharedLibrary() {
2042 module.createXmlFile(mctx)
2043 }
Paul Duffin43db9be2019-12-30 17:35:49 +00002044
2045 // record java_sdk_library modules so that they are exported to make
2046 javaSdkLibraries := javaSdkLibraries(mctx.Config())
2047 javaSdkLibrariesLock.Lock()
2048 defer javaSdkLibrariesLock.Unlock()
2049 *javaSdkLibraries = append(*javaSdkLibraries, module.BaseModuleName())
2050 }
Anton Hansson7f66efa2020-10-08 14:47:23 +01002051
Paul Duffin77590a82022-04-28 14:13:30 +00002052 // 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 +01002053 module.properties.Libs = append(module.properties.Libs, module.sdkLibraryProperties.Impl_only_libs...)
Paul Duffin77590a82022-04-28 14:13:30 +00002054 module.properties.Static_libs = append(module.properties.Static_libs, module.sdkLibraryProperties.Impl_only_static_libs...)
Inseob Kimc0907f12019-02-08 21:00:45 +09002055}
2056
2057func (module *SdkLibrary) InitSdkLibraryProperties() {
Colin Crossce6734e2020-06-15 16:09:53 -07002058 module.addHostAndDeviceProperties()
2059 module.AddProperties(&module.sdkLibraryProperties)
Sundong Ahn054b19a2018-10-19 13:46:09 +09002060
Paul Duffin71b33cc2021-06-23 11:39:47 +01002061 module.initSdkLibraryComponent(module)
Paul Duffin859fe962020-05-15 10:20:31 +01002062
Paul Duffina18abc22020-05-16 18:54:24 +01002063 module.properties.Installable = proptools.BoolPtr(true)
2064 module.deviceProperties.IsSDKLibrary = true
Inseob Kimc0907f12019-02-08 21:00:45 +09002065}
Sundong Ahn054b19a2018-10-19 13:46:09 +09002066
Paul Duffindfa131e2020-05-15 20:37:11 +01002067func (module *SdkLibrary) requiresRuntimeImplementationLibrary() bool {
2068 return !proptools.Bool(module.sdkLibraryProperties.Api_only)
2069}
2070
Jiyong Park932cdfe2020-05-28 00:19:53 +09002071func (module *SdkLibrary) defaultsToStubs() bool {
2072 return proptools.Bool(module.sdkLibraryProperties.Default_to_stubs)
2073}
2074
Paul Duffin1b1e8062020-05-08 13:44:43 +01002075// Defines how to name the individual component modules the sdk library creates.
2076type sdkLibraryComponentNamingScheme interface {
2077 stubsLibraryModuleName(scope *apiScope, baseName string) string
2078
2079 stubsSourceModuleName(scope *apiScope, baseName string) string
Jihoon Kang1c92c3e2023-03-23 17:44:51 +00002080
2081 apiLibraryModuleName(scope *apiScope, baseName string) string
Paul Duffin1b1e8062020-05-08 13:44:43 +01002082}
2083
2084type defaultNamingScheme struct {
2085}
2086
2087func (s *defaultNamingScheme) stubsLibraryModuleName(scope *apiScope, baseName string) string {
2088 return scope.stubsLibraryModuleName(baseName)
2089}
2090
2091func (s *defaultNamingScheme) stubsSourceModuleName(scope *apiScope, baseName string) string {
2092 return scope.stubsSourceModuleName(baseName)
2093}
2094
Jihoon Kang1c92c3e2023-03-23 17:44:51 +00002095func (s *defaultNamingScheme) apiLibraryModuleName(scope *apiScope, baseName string) string {
2096 return scope.apiLibraryModuleName(baseName)
2097}
2098
Paul Duffin1b1e8062020-05-08 13:44:43 +01002099var _ sdkLibraryComponentNamingScheme = (*defaultNamingScheme)(nil)
2100
Jaewoong Jungbc15e3a2021-03-10 17:02:43 -08002101func moduleStubLinkType(name string) (stub bool, ret sdkLinkType) {
Anton Hansson2d0c1942020-05-25 12:20:51 +01002102 // This suffix-based approach is fragile and could potentially mis-trigger.
2103 // TODO(b/155164730): Clean this up when modules no longer reference sdk_lib stubs directly.
Anton Hansson08f476b2021-04-07 15:32:19 +01002104 if strings.HasSuffix(name, apiScopePublic.stubsLibraryModuleNameSuffix()) {
2105 if name == "hwbinder.stubs" || name == "libcore_private.stubs" {
2106 // Due to a previous bug, these modules were not considered stubs, so we retain that.
2107 return false, javaPlatform
2108 }
Anton Hansson2d0c1942020-05-25 12:20:51 +01002109 return true, javaSdk
2110 }
Anton Hansson08f476b2021-04-07 15:32:19 +01002111 if strings.HasSuffix(name, apiScopeSystem.stubsLibraryModuleNameSuffix()) {
Anton Hansson2d0c1942020-05-25 12:20:51 +01002112 return true, javaSystem
2113 }
Anton Hansson08f476b2021-04-07 15:32:19 +01002114 if strings.HasSuffix(name, apiScopeModuleLib.stubsLibraryModuleNameSuffix()) {
Anton Hansson2d0c1942020-05-25 12:20:51 +01002115 return true, javaModule
2116 }
Anton Hansson08f476b2021-04-07 15:32:19 +01002117 if strings.HasSuffix(name, apiScopeTest.stubsLibraryModuleNameSuffix()) {
Anton Hansson2d0c1942020-05-25 12:20:51 +01002118 return true, javaSystem
2119 }
2120 return false, javaPlatform
2121}
2122
Jaewoong Jung4f158ee2019-07-11 10:05:35 -07002123// java_sdk_library is a special Java library that provides optional platform APIs to apps.
2124// In practice, it can be viewed as a combination of several modules: 1) stubs library that clients
2125// are linked against to, 2) droiddoc module that internally generates API stubs source files,
2126// 3) the real runtime shared library that implements the APIs, and 4) XML file for adding
2127// the runtime lib to the classpath at runtime if requested via <uses-library>.
Inseob Kimc0907f12019-02-08 21:00:45 +09002128func SdkLibraryFactory() android.Module {
2129 module := &SdkLibrary{}
Paul Duffinc3091c82020-05-08 14:16:20 +01002130
2131 // Initialize information common between source and prebuilt.
Paul Duffin71b33cc2021-06-23 11:39:47 +01002132 module.initCommon(module)
Paul Duffinc3091c82020-05-08 14:16:20 +01002133
Inseob Kimc0907f12019-02-08 21:00:45 +09002134 module.InitSdkLibraryProperties()
Jooyung Han58f26ab2019-12-18 15:34:32 +09002135 android.InitApexModule(module)
Sundong Ahn054b19a2018-10-19 13:46:09 +09002136 InitJavaModule(module, android.HostAndDeviceSupported)
Paul Duffin3375e352020-04-28 10:44:03 +01002137
2138 // Initialize the map from scope to scope specific properties.
2139 scopeToProperties := make(map[*apiScope]*ApiScopeProperties)
2140 for _, scope := range allApiScopes {
2141 scopeToProperties[scope] = scope.scopeSpecificProperties(module)
2142 }
2143 module.scopeToProperties = scopeToProperties
2144
Paul Duffin4911a892020-04-29 23:35:13 +01002145 // Add the properties containing visibility rules so that they are checked.
Paul Duffin5df79302020-05-16 15:52:12 +01002146 android.AddVisibilityProperty(module, "impl_library_visibility", &module.sdkLibraryProperties.Impl_library_visibility)
Paul Duffin4911a892020-04-29 23:35:13 +01002147 android.AddVisibilityProperty(module, "stubs_library_visibility", &module.sdkLibraryProperties.Stubs_library_visibility)
2148 android.AddVisibilityProperty(module, "stubs_source_visibility", &module.sdkLibraryProperties.Stubs_source_visibility)
2149
Paul Duffin1b1e8062020-05-08 13:44:43 +01002150 module.SetDefaultableHook(func(ctx android.DefaultableHookContext) {
Paul Duffindfa131e2020-05-15 20:37:11 +01002151 // If no implementation is required then it cannot be used as a shared library
2152 // either.
2153 if !module.requiresRuntimeImplementationLibrary() {
2154 // If shared_library has been explicitly set to true then it is incompatible
2155 // with api_only: true.
2156 if proptools.Bool(module.commonSdkLibraryProperties.Shared_library) {
2157 ctx.PropertyErrorf("api_only/shared_library", "inconsistent settings, shared_library and api_only cannot both be true")
2158 }
2159 // Set shared_library: false.
2160 module.commonSdkLibraryProperties.Shared_library = proptools.BoolPtr(false)
2161 }
2162
Paul Duffin1b1e8062020-05-08 13:44:43 +01002163 if module.initCommonAfterDefaultsApplied(ctx) {
2164 module.CreateInternalModules(ctx)
2165 }
2166 })
Zi Wangb2179e32023-01-31 15:53:30 -08002167 android.InitBazelModule(module)
Jiyong Parkc678ad32018-04-10 13:07:10 +09002168 return module
2169}
Colin Cross79c7c262019-04-17 11:11:46 -07002170
Zi Wangb2179e32023-01-31 15:53:30 -08002171type bazelSdkLibraryAttributes struct {
2172 Public bazel.StringAttribute
2173 System bazel.StringAttribute
2174 Test bazel.StringAttribute
2175 Module_lib bazel.StringAttribute
2176 System_server bazel.StringAttribute
2177}
2178
2179// java_sdk_library bp2build converter
2180func (module *SdkLibrary) ConvertWithBp2build(ctx android.TopDownMutatorContext) {
2181 if ctx.ModuleType() != "java_sdk_library" {
Chris Parsons39a16972023-06-08 14:28:51 +00002182 ctx.MarkBp2buildUnconvertible(bp2build_metrics_proto.UnconvertedReasonType_TYPE_UNSUPPORTED, "")
Zi Wangb2179e32023-01-31 15:53:30 -08002183 return
2184 }
2185
2186 nameToAttr := make(map[string]bazel.StringAttribute)
2187
2188 for _, scope := range module.getGeneratedApiScopes(ctx) {
2189 apiSurfaceFile := path.Join(module.getApiDir(), scope.apiFilePrefix+"current.txt")
2190 var scopeStringAttribute bazel.StringAttribute
2191 scopeStringAttribute.SetValue(apiSurfaceFile)
2192 nameToAttr[scope.name] = scopeStringAttribute
2193 }
2194
2195 attrs := bazelSdkLibraryAttributes{
2196 Public: nameToAttr["public"],
2197 System: nameToAttr["system"],
2198 Test: nameToAttr["test"],
2199 Module_lib: nameToAttr["module-lib"],
2200 System_server: nameToAttr["system-server"],
2201 }
2202 props := bazel.BazelTargetModuleProperties{
2203 Rule_class: "java_sdk_library",
2204 Bzl_load_location: "//build/bazel/rules/java:sdk_library.bzl",
2205 }
2206
2207 ctx.CreateBazelTargetModule(props, android.CommonAttributes{Name: module.Name()}, &attrs)
2208}
2209
Colin Cross79c7c262019-04-17 11:11:46 -07002210//
2211// SDK library prebuilts
2212//
2213
Paul Duffin56d44902020-01-31 13:36:25 +00002214// Properties associated with each api scope.
2215type sdkLibraryScopeProperties struct {
Colin Cross79c7c262019-04-17 11:11:46 -07002216 Jars []string `android:"path"`
2217
2218 Sdk_version *string
2219
Colin Cross79c7c262019-04-17 11:11:46 -07002220 // List of shared java libs that this module has dependencies to
2221 Libs []string
Paul Duffin3d1248c2020-04-09 00:10:17 +01002222
Paul Duffinc8782502020-04-29 20:45:27 +01002223 // The stubs source.
Paul Duffin3d1248c2020-04-09 00:10:17 +01002224 Stub_srcs []string `android:"path"`
Paul Duffin1fd005d2020-04-09 01:08:11 +01002225
2226 // The current.txt
Paul Duffin0f8faff2020-05-20 16:18:00 +01002227 Current_api *string `android:"path"`
Paul Duffin1fd005d2020-04-09 01:08:11 +01002228
2229 // The removed.txt
Paul Duffin0f8faff2020-05-20 16:18:00 +01002230 Removed_api *string `android:"path"`
Anton Hanssond78eb762021-09-21 15:25:12 +01002231
2232 // Annotation zip
2233 Annotations *string `android:"path"`
Colin Cross79c7c262019-04-17 11:11:46 -07002234}
2235
Paul Duffin56d44902020-01-31 13:36:25 +00002236type sdkLibraryImportProperties struct {
Paul Duffinfcfd7912020-01-31 17:54:30 +00002237 // List of shared java libs, common to all scopes, that this module has
2238 // dependencies to
2239 Libs []string
Paul Duffin1267d872021-04-16 17:21:36 +01002240
2241 // If set to true, compile dex files for the stubs. Defaults to false.
2242 Compile_dex *bool
Paul Duffin869de142021-07-15 14:14:41 +01002243
2244 // If not empty, classes are restricted to the specified packages and their sub-packages.
Paul Duffin869de142021-07-15 14:14:41 +01002245 Permitted_packages []string
Paul Duffin56d44902020-01-31 13:36:25 +00002246}
2247
Paul Duffineedc5d52020-06-12 17:46:39 +01002248type SdkLibraryImport struct {
Colin Cross79c7c262019-04-17 11:11:46 -07002249 android.ModuleBase
2250 android.DefaultableModuleBase
2251 prebuilt android.Prebuilt
Paul Duffindd46f712020-02-10 13:37:10 +00002252 android.ApexModuleBase
Colin Cross79c7c262019-04-17 11:11:46 -07002253
Paul Duffin37856732021-02-26 14:24:15 +00002254 hiddenAPI
Jiakai Zhang204356f2021-09-09 08:12:46 +00002255 dexpreopter
Paul Duffin37856732021-02-26 14:24:15 +00002256
Colin Cross79c7c262019-04-17 11:11:46 -07002257 properties sdkLibraryImportProperties
2258
Paul Duffin46a26a82020-04-07 19:27:04 +01002259 // Map from api scope to the scope specific property structure.
2260 scopeProperties map[*apiScope]*sdkLibraryScopeProperties
2261
Paul Duffin56d44902020-01-31 13:36:25 +00002262 commonToSdkLibraryAndImport
Paul Duffineedc5d52020-06-12 17:46:39 +01002263
2264 // The reference to the implementation library created by the source module.
2265 // Is nil if the source module does not exist.
2266 implLibraryModule *Library
2267
2268 // The reference to the xml permissions module created by the source module.
2269 // Is nil if the source module does not exist.
2270 xmlPermissionsFileModule *sdkLibraryXml
Paul Duffin39853512021-02-26 11:09:39 +00002271
Jeongik Chad5fe8782021-07-08 01:13:11 +09002272 // Build path to the dex implementation jar obtained from the prebuilt_apex, if any.
Martin Stjernholm8be1e6d2021-09-15 03:34:04 +01002273 dexJarFile OptionalDexJarPath
Jeongik Chad5fe8782021-07-08 01:13:11 +09002274
2275 // Expected install file path of the source module(sdk_library)
2276 // or dex implementation jar obtained from the prebuilt_apex, if any.
2277 installFile android.Path
Colin Cross79c7c262019-04-17 11:11:46 -07002278}
2279
Paul Duffineedc5d52020-06-12 17:46:39 +01002280var _ SdkLibraryDependency = (*SdkLibraryImport)(nil)
Colin Cross79c7c262019-04-17 11:11:46 -07002281
Paul Duffin46a26a82020-04-07 19:27:04 +01002282// The type of a structure that contains a field of type sdkLibraryScopeProperties
2283// for each apiscope in allApiScopes, e.g. something like:
Colin Crossd079e0b2022-08-16 10:27:33 -07002284//
2285// struct {
2286// Public sdkLibraryScopeProperties
2287// System sdkLibraryScopeProperties
2288// ...
2289// }
Paul Duffin46a26a82020-04-07 19:27:04 +01002290var allScopeStructType = createAllScopePropertiesStructType()
2291
2292// Dynamically create a structure type for each apiscope in allApiScopes.
2293func createAllScopePropertiesStructType() reflect.Type {
2294 var fields []reflect.StructField
2295 for _, apiScope := range allApiScopes {
2296 field := reflect.StructField{
2297 Name: apiScope.fieldName,
2298 Type: reflect.TypeOf(sdkLibraryScopeProperties{}),
2299 }
2300 fields = append(fields, field)
2301 }
2302
2303 return reflect.StructOf(fields)
2304}
2305
2306// Create an instance of the scope specific structure type and return a map
2307// from apiscope to a pointer to each scope specific field.
2308func createPropertiesInstance() (interface{}, map[*apiScope]*sdkLibraryScopeProperties) {
2309 allScopePropertiesPtr := reflect.New(allScopeStructType)
2310 allScopePropertiesStruct := allScopePropertiesPtr.Elem()
2311 scopeProperties := make(map[*apiScope]*sdkLibraryScopeProperties)
2312
2313 for _, apiScope := range allApiScopes {
2314 field := allScopePropertiesStruct.FieldByName(apiScope.fieldName)
2315 scopeProperties[apiScope] = field.Addr().Interface().(*sdkLibraryScopeProperties)
2316 }
2317
2318 return allScopePropertiesPtr.Interface(), scopeProperties
2319}
2320
Jaewoong Jung4f158ee2019-07-11 10:05:35 -07002321// java_sdk_library_import imports a prebuilt java_sdk_library.
Colin Cross79c7c262019-04-17 11:11:46 -07002322func sdkLibraryImportFactory() android.Module {
Paul Duffineedc5d52020-06-12 17:46:39 +01002323 module := &SdkLibraryImport{}
Colin Cross79c7c262019-04-17 11:11:46 -07002324
Paul Duffin46a26a82020-04-07 19:27:04 +01002325 allScopeProperties, scopeToProperties := createPropertiesInstance()
2326 module.scopeProperties = scopeToProperties
Jiakai Zhang9c4dc192023-02-09 00:09:24 +08002327 module.AddProperties(&module.properties, allScopeProperties, &module.importDexpreoptProperties)
Colin Cross79c7c262019-04-17 11:11:46 -07002328
Paul Duffinc3091c82020-05-08 14:16:20 +01002329 // Initialize information common between source and prebuilt.
Paul Duffin71b33cc2021-06-23 11:39:47 +01002330 module.initCommon(module)
Paul Duffinc3091c82020-05-08 14:16:20 +01002331
Paul Duffin0bdcb272020-02-06 15:24:57 +00002332 android.InitPrebuiltModule(module, &[]string{""})
Paul Duffindd46f712020-02-10 13:37:10 +00002333 android.InitApexModule(module)
Colin Cross79c7c262019-04-17 11:11:46 -07002334 InitJavaModule(module, android.HostAndDeviceSupported)
2335
Paul Duffin1b1e8062020-05-08 13:44:43 +01002336 module.SetDefaultableHook(func(mctx android.DefaultableHookContext) {
2337 if module.initCommonAfterDefaultsApplied(mctx) {
2338 module.createInternalModules(mctx)
2339 }
2340 })
Colin Cross79c7c262019-04-17 11:11:46 -07002341 return module
2342}
2343
Paul Duffin630b11e2021-07-15 13:35:26 +01002344var _ PermittedPackagesForUpdatableBootJars = (*SdkLibraryImport)(nil)
2345
2346func (module *SdkLibraryImport) PermittedPackagesForUpdatableBootJars() []string {
2347 return module.properties.Permitted_packages
2348}
2349
Paul Duffineedc5d52020-06-12 17:46:39 +01002350func (module *SdkLibraryImport) Prebuilt() *android.Prebuilt {
Colin Cross79c7c262019-04-17 11:11:46 -07002351 return &module.prebuilt
2352}
2353
Paul Duffineedc5d52020-06-12 17:46:39 +01002354func (module *SdkLibraryImport) Name() string {
Colin Cross79c7c262019-04-17 11:11:46 -07002355 return module.prebuilt.Name(module.ModuleBase.Name())
2356}
2357
Paul Duffineedc5d52020-06-12 17:46:39 +01002358func (module *SdkLibraryImport) createInternalModules(mctx android.DefaultableHookContext) {
Colin Cross79c7c262019-04-17 11:11:46 -07002359
Paul Duffin50061512020-01-21 16:31:05 +00002360 // If the build is configured to use prebuilts then force this to be preferred.
Jeongik Cha816a23a2020-07-08 01:09:23 +09002361 if mctx.Config().AlwaysUsePrebuiltSdks() {
Paul Duffin50061512020-01-21 16:31:05 +00002362 module.prebuilt.ForcePrefer()
2363 }
2364
Paul Duffin46a26a82020-04-07 19:27:04 +01002365 for apiScope, scopeProperties := range module.scopeProperties {
Paul Duffin56d44902020-01-31 13:36:25 +00002366 if len(scopeProperties.Jars) == 0 {
2367 continue
2368 }
2369
Paul Duffinbbb546b2020-04-09 00:07:11 +01002370 module.createJavaImportForStubs(mctx, apiScope, scopeProperties)
Paul Duffin3d1248c2020-04-09 00:10:17 +01002371
Paul Duffin0f8faff2020-05-20 16:18:00 +01002372 if len(scopeProperties.Stub_srcs) > 0 {
2373 module.createPrebuiltStubsSources(mctx, apiScope, scopeProperties)
2374 }
Paul Duffin56d44902020-01-31 13:36:25 +00002375 }
Colin Cross79c7c262019-04-17 11:11:46 -07002376
2377 javaSdkLibraries := javaSdkLibraries(mctx.Config())
2378 javaSdkLibrariesLock.Lock()
2379 defer javaSdkLibrariesLock.Unlock()
2380 *javaSdkLibraries = append(*javaSdkLibraries, module.BaseModuleName())
2381}
2382
Paul Duffineedc5d52020-06-12 17:46:39 +01002383func (module *SdkLibraryImport) createJavaImportForStubs(mctx android.DefaultableHookContext, apiScope *apiScope, scopeProperties *sdkLibraryScopeProperties) {
Paul Duffinbbb546b2020-04-09 00:07:11 +01002384 // Creates a java import for the jar with ".stubs" suffix
2385 props := struct {
Paul Duffin1dbe3ca2020-05-16 09:57:59 +01002386 Name *string
2387 Sdk_version *string
2388 Libs []string
2389 Jars []string
Paul Duffin1267d872021-04-16 17:21:36 +01002390 Compile_dex *bool
Paul Duffinbf4de042022-09-27 12:41:52 +01002391
2392 android.UserSuppliedPrebuiltProperties
Paul Duffinbbb546b2020-04-09 00:07:11 +01002393 }{}
Paul Duffinc3091c82020-05-08 14:16:20 +01002394 props.Name = proptools.StringPtr(module.stubsLibraryModuleName(apiScope))
Paul Duffinbbb546b2020-04-09 00:07:11 +01002395 props.Sdk_version = scopeProperties.Sdk_version
2396 // Prepend any of the libs from the legacy public properties to the libs for each of the
2397 // scopes to avoid having to duplicate them in each scope.
2398 props.Libs = append(module.properties.Libs, scopeProperties.Libs...)
2399 props.Jars = scopeProperties.Jars
Paul Duffin1dbe3ca2020-05-16 09:57:59 +01002400
Paul Duffin38b57852020-05-13 16:08:09 +01002401 // The imports are preferred if the java_sdk_library_import is preferred.
Paul Duffinbf4de042022-09-27 12:41:52 +01002402 props.CopyUserSuppliedPropertiesFromPrebuilt(&module.prebuilt)
Paul Duffin859fe962020-05-15 10:20:31 +01002403
Paul Duffin1267d872021-04-16 17:21:36 +01002404 // The imports need to be compiled to dex if the java_sdk_library_import requests it.
Paul Duffinf4600f62021-05-13 22:34:45 +01002405 compileDex := module.properties.Compile_dex
2406 if module.stubLibrariesCompiledForDex() {
2407 compileDex = proptools.BoolPtr(true)
2408 }
2409 props.Compile_dex = compileDex
Paul Duffin1267d872021-04-16 17:21:36 +01002410
Paul Duffin859fe962020-05-15 10:20:31 +01002411 mctx.CreateModule(ImportFactory, &props, module.sdkComponentPropertiesForChildLibrary())
Paul Duffinbbb546b2020-04-09 00:07:11 +01002412}
2413
Paul Duffineedc5d52020-06-12 17:46:39 +01002414func (module *SdkLibraryImport) createPrebuiltStubsSources(mctx android.DefaultableHookContext, apiScope *apiScope, scopeProperties *sdkLibraryScopeProperties) {
Paul Duffin3d1248c2020-04-09 00:10:17 +01002415 props := struct {
Paul Duffinbf4de042022-09-27 12:41:52 +01002416 Name *string
2417 Srcs []string
2418
2419 android.UserSuppliedPrebuiltProperties
Paul Duffin3d1248c2020-04-09 00:10:17 +01002420 }{}
Paul Duffinc3091c82020-05-08 14:16:20 +01002421 props.Name = proptools.StringPtr(module.stubsSourceModuleName(apiScope))
Paul Duffin3d1248c2020-04-09 00:10:17 +01002422 props.Srcs = scopeProperties.Stub_srcs
Paul Duffin38b57852020-05-13 16:08:09 +01002423
2424 // The stubs source is preferred if the java_sdk_library_import is preferred.
Paul Duffinbf4de042022-09-27 12:41:52 +01002425 props.CopyUserSuppliedPropertiesFromPrebuilt(&module.prebuilt)
2426
2427 mctx.CreateModule(PrebuiltStubsSourcesFactory, &props)
Paul Duffin3d1248c2020-04-09 00:10:17 +01002428}
2429
Paul Duffin44f1d842020-06-26 20:17:02 +01002430// Add the dependencies on the child module in the component deps mutator so that it
2431// creates references to the prebuilt and not the source modules.
2432func (module *SdkLibraryImport) ComponentDepsMutator(ctx android.BottomUpMutatorContext) {
Paul Duffin46a26a82020-04-07 19:27:04 +01002433 for apiScope, scopeProperties := range module.scopeProperties {
Paul Duffin56d44902020-01-31 13:36:25 +00002434 if len(scopeProperties.Jars) == 0 {
2435 continue
2436 }
2437
2438 // Add dependencies to the prebuilt stubs library
Paul Duffin864116c2021-04-02 10:24:13 +01002439 ctx.AddVariationDependencies(nil, apiScope.stubsTag, android.PrebuiltNameFromSource(module.stubsLibraryModuleName(apiScope)))
Paul Duffin0f8faff2020-05-20 16:18:00 +01002440
2441 if len(scopeProperties.Stub_srcs) > 0 {
2442 // Add dependencies to the prebuilt stubs source library
Paul Duffin864116c2021-04-02 10:24:13 +01002443 ctx.AddVariationDependencies(nil, apiScope.stubsSourceTag, android.PrebuiltNameFromSource(module.stubsSourceModuleName(apiScope)))
Paul Duffin0f8faff2020-05-20 16:18:00 +01002444 }
Paul Duffin56d44902020-01-31 13:36:25 +00002445 }
Paul Duffin44f1d842020-06-26 20:17:02 +01002446}
2447
2448// Add other dependencies as normal.
2449func (module *SdkLibraryImport) DepsMutator(ctx android.BottomUpMutatorContext) {
Paul Duffineedc5d52020-06-12 17:46:39 +01002450
2451 implName := module.implLibraryModuleName()
2452 if ctx.OtherModuleExists(implName) {
2453 ctx.AddVariationDependencies(nil, implLibraryTag, implName)
2454
2455 xmlPermissionsModuleName := module.xmlPermissionsModuleName()
2456 if module.sharedLibrary() && ctx.OtherModuleExists(xmlPermissionsModuleName) {
2457 // Add dependency to the rule for generating the xml permissions file
2458 ctx.AddDependency(module, xmlPermissionsFileTag, xmlPermissionsModuleName)
2459 }
2460 }
Colin Cross79c7c262019-04-17 11:11:46 -07002461}
2462
Jiakai Zhang204356f2021-09-09 08:12:46 +00002463func (module *SdkLibraryImport) AndroidMkEntries() []android.AndroidMkEntries {
2464 // For an SDK library imported from a prebuilt APEX, we don't need a Make module for itself, as we
2465 // don't need to install it. However, we need to add its dexpreopt outputs as sub-modules, if it
2466 // is preopted.
2467 dexpreoptEntries := module.dexpreopter.AndroidMkEntriesForApex()
2468 return append(dexpreoptEntries, android.AndroidMkEntries{Disabled: true})
2469}
2470
Jiyong Park45bf82e2020-12-15 22:29:02 +09002471var _ android.ApexModule = (*SdkLibraryImport)(nil)
2472
2473// Implements android.ApexModule
Paul Duffineedc5d52020-06-12 17:46:39 +01002474func (module *SdkLibraryImport) DepIsInSameApex(mctx android.BaseModuleContext, dep android.Module) bool {
2475 depTag := mctx.OtherModuleDependencyTag(dep)
2476 if depTag == xmlPermissionsFileTag {
2477 return true
2478 }
2479
2480 // None of the other dependencies of the java_sdk_library_import are in the same apex
2481 // as the one that references this module.
2482 return false
2483}
2484
Jiyong Park45bf82e2020-12-15 22:29:02 +09002485// Implements android.ApexModule
Dan Albertc8060532020-07-22 22:32:17 -07002486func (module *SdkLibraryImport) ShouldSupportSdkVersion(ctx android.BaseModuleContext,
2487 sdkVersion android.ApiLevel) error {
Jooyung Han749dc692020-04-15 11:03:39 +09002488 // we don't check prebuilt modules for sdk_version
2489 return nil
2490}
2491
Paul Duffinea8f8082021-06-24 13:25:57 +01002492// Implements android.ApexModule
2493func (module *SdkLibraryImport) UniqueApexVariations() bool {
2494 return module.uniqueApexVariations()
2495}
2496
Paul Duffin09817d62022-04-28 17:45:11 +01002497// MinSdkVersion - Implements hiddenAPIModule
Spandan Das8c9ae7e2023-03-03 21:20:36 +00002498func (module *SdkLibraryImport) MinSdkVersion(ctx android.EarlyModuleContext) android.ApiLevel {
2499 return android.NoneApiLevel
Paul Duffin09817d62022-04-28 17:45:11 +01002500}
2501
2502var _ hiddenAPIModule = (*SdkLibraryImport)(nil)
2503
Paul Duffineedc5d52020-06-12 17:46:39 +01002504func (module *SdkLibraryImport) OutputFiles(tag string) (android.Paths, error) {
Paul Duffin1e940d52022-04-29 14:21:25 +01002505 paths, err := module.commonOutputFiles(tag)
2506 if paths != nil || err != nil {
2507 return paths, err
2508 }
2509 if module.implLibraryModule != nil {
2510 return module.implLibraryModule.OutputFiles(tag)
2511 } else {
2512 return nil, nil
2513 }
Paul Duffin46dc45a2020-05-14 15:39:10 +01002514}
2515
Paul Duffineedc5d52020-06-12 17:46:39 +01002516func (module *SdkLibraryImport) GenerateAndroidBuildActions(ctx android.ModuleContext) {
Paul Duffina2ae7e02020-09-11 11:55:00 +01002517 module.generateCommonBuildActions(ctx)
2518
Jeongik Chad5fe8782021-07-08 01:13:11 +09002519 // Assume that source module(sdk_library) is installed in /<sdk_library partition>/framework
2520 module.installFile = android.PathForModuleInstall(ctx, "framework", module.Stem()+".jar")
2521
Paul Duffin0f8faff2020-05-20 16:18:00 +01002522 // Record the paths to the prebuilt stubs library and stubs source.
Colin Cross79c7c262019-04-17 11:11:46 -07002523 ctx.VisitDirectDeps(func(to android.Module) {
2524 tag := ctx.OtherModuleDependencyTag(to)
2525
Paul Duffin0f8faff2020-05-20 16:18:00 +01002526 // Extract information from any of the scope specific dependencies.
2527 if scopeTag, ok := tag.(scopeDependencyTag); ok {
2528 apiScope := scopeTag.apiScope
2529 scopePaths := module.getScopePathsCreateIfNeeded(apiScope)
2530
2531 // Extract information from the dependency. The exact information extracted
2532 // is determined by the nature of the dependency which is determined by the tag.
2533 scopeTag.extractDepInfo(ctx, to, scopePaths)
Paul Duffineedc5d52020-06-12 17:46:39 +01002534 } else if tag == implLibraryTag {
2535 if implLibrary, ok := to.(*Library); ok {
2536 module.implLibraryModule = implLibrary
2537 } else {
2538 ctx.ModuleErrorf("implementation library must be of type *java.Library but was %T", to)
2539 }
2540 } else if tag == xmlPermissionsFileTag {
2541 if xmlPermissionsFileModule, ok := to.(*sdkLibraryXml); ok {
2542 module.xmlPermissionsFileModule = xmlPermissionsFileModule
2543 } else {
2544 ctx.ModuleErrorf("xml permissions file module must be of type *sdkLibraryXml but was %T", to)
2545 }
Colin Cross79c7c262019-04-17 11:11:46 -07002546 }
2547 })
Paul Duffin0f8faff2020-05-20 16:18:00 +01002548
2549 // Populate the scope paths with information from the properties.
2550 for apiScope, scopeProperties := range module.scopeProperties {
2551 if len(scopeProperties.Jars) == 0 {
2552 continue
2553 }
2554
2555 paths := module.getScopePathsCreateIfNeeded(apiScope)
Anton Hanssond78eb762021-09-21 15:25:12 +01002556 paths.annotationsZip = android.OptionalPathForModuleSrc(ctx, scopeProperties.Annotations)
Paul Duffin0f8faff2020-05-20 16:18:00 +01002557 paths.currentApiFilePath = android.OptionalPathForModuleSrc(ctx, scopeProperties.Current_api)
2558 paths.removedApiFilePath = android.OptionalPathForModuleSrc(ctx, scopeProperties.Removed_api)
2559 }
Paul Duffin39853512021-02-26 11:09:39 +00002560
2561 if ctx.Device() {
2562 // If this is a variant created for a prebuilt_apex then use the dex implementation jar
2563 // obtained from the associated deapexer module.
2564 ai := ctx.Provider(android.ApexInfoProvider).(android.ApexInfo)
2565 if ai.ForPrebuiltApex {
Paul Duffin39853512021-02-26 11:09:39 +00002566 // Get the path of the dex implementation jar from the `deapexer` module.
Martin Stjernholm44825602021-09-17 01:44:12 +01002567 di := android.FindDeapexerProviderForModule(ctx)
2568 if di == nil {
2569 return // An error has been reported by FindDeapexerProviderForModule.
2570 }
Jiakai Zhang81e46812023-02-08 21:56:07 +08002571 dexJarFileApexRootRelative := apexRootRelativePathToJavaLib(module.BaseModuleName())
2572 if dexOutputPath := di.PrebuiltExportPath(dexJarFileApexRootRelative); dexOutputPath != nil {
Martin Stjernholm8be1e6d2021-09-15 03:34:04 +01002573 dexJarFile := makeDexJarPathFromPath(dexOutputPath)
2574 module.dexJarFile = dexJarFile
Jiakai Zhang204356f2021-09-09 08:12:46 +00002575 installPath := android.PathForModuleInPartitionInstall(
Jiakai Zhang81e46812023-02-08 21:56:07 +08002576 ctx, "apex", ai.ApexVariationName, dexJarFileApexRootRelative)
Jiakai Zhang204356f2021-09-09 08:12:46 +00002577 module.installFile = installPath
Martin Stjernholm8be1e6d2021-09-15 03:34:04 +01002578 module.initHiddenAPI(ctx, dexJarFile, module.findScopePaths(apiScopePublic).stubsImplPath[0], nil)
Jiakai Zhang204356f2021-09-09 08:12:46 +00002579
Jiakai Zhang204356f2021-09-09 08:12:46 +00002580 module.dexpreopter.installPath = module.dexpreopter.getInstallPath(ctx, installPath)
2581 module.dexpreopter.isSDKLibrary = true
2582 module.dexpreopter.uncompressedDex = shouldUncompressDex(ctx, &module.dexpreopter)
Jiakai Zhang81e46812023-02-08 21:56:07 +08002583
2584 if profilePath := di.PrebuiltExportPath(dexJarFileApexRootRelative + ".prof"); profilePath != nil {
2585 module.dexpreopter.inputProfilePathOnHost = profilePath
2586 }
2587
2588 // Dexpreopting.
Jiakai Zhang204356f2021-09-09 08:12:46 +00002589 module.dexpreopt(ctx, dexOutputPath)
Paul Duffin39853512021-02-26 11:09:39 +00002590 } else {
2591 // This should never happen as a variant for a prebuilt_apex is only created if the
2592 // prebuilt_apex has been configured to export the java library dex file.
Martin Stjernholm44825602021-09-17 01:44:12 +01002593 ctx.ModuleErrorf("internal error: no dex implementation jar available from prebuilt APEX %s", di.ApexModuleName())
Paul Duffin39853512021-02-26 11:09:39 +00002594 }
2595 }
2596 }
Colin Cross79c7c262019-04-17 11:11:46 -07002597}
2598
Jiyong Parkf1691d22021-03-29 20:11:58 +09002599func (module *SdkLibraryImport) sdkJars(ctx android.BaseModuleContext, sdkVersion android.SdkSpec, headerJars bool) android.Paths {
Paul Duffineedc5d52020-06-12 17:46:39 +01002600
2601 // For consistency with SdkLibrary make the implementation jar available to libraries that
2602 // are within the same APEX.
2603 implLibraryModule := module.implLibraryModule
Colin Cross56a83212020-09-15 18:30:11 -07002604 if implLibraryModule != nil && withinSameApexesAs(ctx, module) {
Paul Duffineedc5d52020-06-12 17:46:39 +01002605 if headerJars {
2606 return implLibraryModule.HeaderJars()
2607 } else {
2608 return implLibraryModule.ImplementationJars()
2609 }
2610 }
2611
Paul Duffin23970f42020-05-20 14:20:02 +01002612 return module.selectHeaderJarsForSdkVersion(ctx, sdkVersion)
Paul Duffin56d44902020-01-31 13:36:25 +00002613}
2614
Colin Cross79c7c262019-04-17 11:11:46 -07002615// to satisfy SdkLibraryDependency interface
Jiyong Parkf1691d22021-03-29 20:11:58 +09002616func (module *SdkLibraryImport) SdkHeaderJars(ctx android.BaseModuleContext, sdkVersion android.SdkSpec) android.Paths {
Colin Cross79c7c262019-04-17 11:11:46 -07002617 // This module is just a wrapper for the prebuilt stubs.
Paul Duffineedc5d52020-06-12 17:46:39 +01002618 return module.sdkJars(ctx, sdkVersion, true)
Colin Cross79c7c262019-04-17 11:11:46 -07002619}
2620
2621// to satisfy SdkLibraryDependency interface
Jiyong Parkf1691d22021-03-29 20:11:58 +09002622func (module *SdkLibraryImport) SdkImplementationJars(ctx android.BaseModuleContext, sdkVersion android.SdkSpec) android.Paths {
Colin Cross79c7c262019-04-17 11:11:46 -07002623 // This module is just a wrapper for the stubs.
Paul Duffineedc5d52020-06-12 17:46:39 +01002624 return module.sdkJars(ctx, sdkVersion, false)
2625}
2626
Ulya Trafimovichdbf31662020-12-17 12:07:54 +00002627// to satisfy UsesLibraryDependency interface
Martin Stjernholm8be1e6d2021-09-15 03:34:04 +01002628func (module *SdkLibraryImport) DexJarBuildPath() OptionalDexJarPath {
Paul Duffin39853512021-02-26 11:09:39 +00002629 // The dex implementation jar extracted from the .apex file should be used in preference to the
2630 // source.
Martin Stjernholm8be1e6d2021-09-15 03:34:04 +01002631 if module.dexJarFile.IsSet() {
Paul Duffin39853512021-02-26 11:09:39 +00002632 return module.dexJarFile
2633 }
Paul Duffineedc5d52020-06-12 17:46:39 +01002634 if module.implLibraryModule == nil {
Martin Stjernholm8be1e6d2021-09-15 03:34:04 +01002635 return makeUnsetDexJarPath()
Paul Duffineedc5d52020-06-12 17:46:39 +01002636 } else {
2637 return module.implLibraryModule.DexJarBuildPath()
2638 }
2639}
2640
Ulya Trafimovichdbf31662020-12-17 12:07:54 +00002641// to satisfy UsesLibraryDependency interface
Ulya Trafimovich31e444e2020-08-14 17:32:16 +01002642func (module *SdkLibraryImport) DexJarInstallPath() android.Path {
Jeongik Chad5fe8782021-07-08 01:13:11 +09002643 return module.installFile
Ulya Trafimovich31e444e2020-08-14 17:32:16 +01002644}
2645
Ulya Trafimovichdbf31662020-12-17 12:07:54 +00002646// to satisfy UsesLibraryDependency interface
2647func (module *SdkLibraryImport) ClassLoaderContexts() dexpreopt.ClassLoaderContextMap {
2648 return nil
2649}
2650
Paul Duffineedc5d52020-06-12 17:46:39 +01002651// to satisfy apex.javaDependency interface
2652func (module *SdkLibraryImport) JacocoReportClassesFile() android.Path {
2653 if module.implLibraryModule == nil {
2654 return nil
2655 } else {
2656 return module.implLibraryModule.JacocoReportClassesFile()
2657 }
2658}
2659
2660// to satisfy apex.javaDependency interface
Colin Cross08dca382020-07-21 20:31:17 -07002661func (module *SdkLibraryImport) LintDepSets() LintDepSets {
2662 if module.implLibraryModule == nil {
2663 return LintDepSets{}
2664 } else {
2665 return module.implLibraryModule.LintDepSets()
2666 }
2667}
2668
Spandan Das17854f52022-01-14 21:19:14 +00002669func (module *SdkLibraryImport) GetStrictUpdatabilityLinting() bool {
Jaewoong Jung476b9d62021-05-10 15:30:00 -07002670 if module.implLibraryModule == nil {
2671 return false
2672 } else {
Spandan Das17854f52022-01-14 21:19:14 +00002673 return module.implLibraryModule.GetStrictUpdatabilityLinting()
Jaewoong Jung476b9d62021-05-10 15:30:00 -07002674 }
2675}
2676
Spandan Das17854f52022-01-14 21:19:14 +00002677func (module *SdkLibraryImport) SetStrictUpdatabilityLinting(strictLinting bool) {
Jaewoong Jung476b9d62021-05-10 15:30:00 -07002678 if module.implLibraryModule != nil {
Spandan Das17854f52022-01-14 21:19:14 +00002679 module.implLibraryModule.SetStrictUpdatabilityLinting(strictLinting)
Jaewoong Jung476b9d62021-05-10 15:30:00 -07002680 }
2681}
2682
Colin Cross08dca382020-07-21 20:31:17 -07002683// to satisfy apex.javaDependency interface
Paul Duffineedc5d52020-06-12 17:46:39 +01002684func (module *SdkLibraryImport) Stem() string {
2685 return module.BaseModuleName()
Colin Cross79c7c262019-04-17 11:11:46 -07002686}
Jiyong Parke3833882020-02-17 17:28:10 +09002687
Paul Duffin44b481b2020-06-17 16:59:43 +01002688var _ ApexDependency = (*SdkLibraryImport)(nil)
2689
2690// to satisfy java.ApexDependency interface
2691func (module *SdkLibraryImport) HeaderJars() android.Paths {
2692 if module.implLibraryModule == nil {
2693 return nil
2694 } else {
2695 return module.implLibraryModule.HeaderJars()
2696 }
2697}
2698
2699// to satisfy java.ApexDependency interface
2700func (module *SdkLibraryImport) ImplementationAndResourcesJars() android.Paths {
2701 if module.implLibraryModule == nil {
2702 return nil
2703 } else {
2704 return module.implLibraryModule.ImplementationAndResourcesJars()
2705 }
2706}
2707
Jiakai Zhang204356f2021-09-09 08:12:46 +00002708// to satisfy java.DexpreopterInterface interface
2709func (module *SdkLibraryImport) IsInstallable() bool {
2710 return true
2711}
2712
Paul Duffinfef55002021-06-17 14:56:05 +01002713var _ android.RequiredFilesFromPrebuiltApex = (*SdkLibraryImport)(nil)
2714
Paul Duffinb4bbf2c2021-06-17 15:59:07 +01002715func (module *SdkLibraryImport) RequiredFilesFromPrebuiltApex(ctx android.BaseModuleContext) []string {
Paul Duffinfef55002021-06-17 14:56:05 +01002716 name := module.BaseModuleName()
Jiakai Zhang81e46812023-02-08 21:56:07 +08002717 return requiredFilesFromPrebuiltApexForImport(name, &module.dexpreopter)
Paul Duffinfef55002021-06-17 14:56:05 +01002718}
2719
Jiyong Parke3833882020-02-17 17:28:10 +09002720// java_sdk_library_xml
Jiyong Parke3833882020-02-17 17:28:10 +09002721type sdkLibraryXml struct {
2722 android.ModuleBase
2723 android.DefaultableModuleBase
2724 android.ApexModuleBase
2725
2726 properties sdkLibraryXmlProperties
2727
2728 outputFilePath android.OutputPath
2729 installDirPath android.InstallPath
Colin Cross56a83212020-09-15 18:30:11 -07002730
2731 hideApexVariantFromMake bool
Jiyong Parke3833882020-02-17 17:28:10 +09002732}
2733
2734type sdkLibraryXmlProperties struct {
2735 // canonical name of the lib
2736 Lib_name *string
Pedro Loureiro9956e5e2021-09-07 17:21:59 +00002737
2738 // Signals that this shared library is part of the bootclasspath starting
2739 // on the version indicated in this attribute.
2740 //
2741 // This will make platforms at this level and above to ignore
2742 // <uses-library> tags with this library name because the library is already
2743 // available
2744 On_bootclasspath_since *string
2745
2746 // Signals that this shared library was part of the bootclasspath before
2747 // (but not including) the version indicated in this attribute.
2748 //
2749 // The system will automatically add a <uses-library> tag with this library to
2750 // apps that target any SDK less than the version indicated in this attribute.
2751 On_bootclasspath_before *string
2752
2753 // Indicates that PackageManager should ignore this shared library if the
2754 // platform is below the version indicated in this attribute.
2755 //
2756 // This means that the device won't recognise this library as installed.
2757 Min_device_sdk *string
2758
2759 // Indicates that PackageManager should ignore this shared library if the
2760 // platform is above the version indicated in this attribute.
2761 //
2762 // This means that the device won't recognise this library as installed.
2763 Max_device_sdk *string
Pedro Loureiroc3621422021-09-28 15:40:23 +00002764
2765 // The SdkLibrary's min api level as a string
2766 //
2767 // This value comes from the ApiLevel of the MinSdkVersion property.
2768 Sdk_library_min_api_level *string
Jiyong Parke3833882020-02-17 17:28:10 +09002769}
2770
2771// java_sdk_library_xml builds the permission xml file for a java_sdk_library.
2772// Not to be used directly by users. java_sdk_library internally uses this.
2773func sdkLibraryXmlFactory() android.Module {
2774 module := &sdkLibraryXml{}
2775
2776 module.AddProperties(&module.properties)
2777
2778 android.InitApexModule(module)
2779 android.InitAndroidArchModule(module, android.DeviceSupported, android.MultilibCommon)
2780
2781 return module
2782}
2783
Colin Crossaede88c2020-08-11 12:17:01 -07002784func (module *sdkLibraryXml) UniqueApexVariations() bool {
2785 // sdkLibraryXml needs a unique variation per APEX because the generated XML file contains the path to the
2786 // mounted APEX, which contains the name of the APEX.
2787 return true
2788}
2789
Jiyong Parke3833882020-02-17 17:28:10 +09002790// from android.PrebuiltEtcModule
Jooyung Han0703fd82020-08-26 22:11:53 +09002791func (module *sdkLibraryXml) BaseDir() string {
2792 return "etc"
2793}
2794
2795// from android.PrebuiltEtcModule
Jiyong Parke3833882020-02-17 17:28:10 +09002796func (module *sdkLibraryXml) SubDir() string {
2797 return "permissions"
2798}
2799
2800// from android.PrebuiltEtcModule
2801func (module *sdkLibraryXml) OutputFile() android.OutputPath {
2802 return module.outputFilePath
2803}
2804
2805// from android.ApexModule
2806func (module *sdkLibraryXml) AvailableFor(what string) bool {
2807 return true
2808}
2809
2810func (module *sdkLibraryXml) DepsMutator(ctx android.BottomUpMutatorContext) {
2811 // do nothing
2812}
2813
Jiyong Park45bf82e2020-12-15 22:29:02 +09002814var _ android.ApexModule = (*sdkLibraryXml)(nil)
2815
2816// Implements android.ApexModule
Dan Albertc8060532020-07-22 22:32:17 -07002817func (module *sdkLibraryXml) ShouldSupportSdkVersion(ctx android.BaseModuleContext,
2818 sdkVersion android.ApiLevel) error {
Jooyung Han749dc692020-04-15 11:03:39 +09002819 // sdkLibraryXml doesn't need to be checked separately because java_sdk_library is checked
2820 return nil
2821}
2822
Jiyong Parke3833882020-02-17 17:28:10 +09002823// File path to the runtime implementation library
Colin Cross56a83212020-09-15 18:30:11 -07002824func (module *sdkLibraryXml) implPath(ctx android.ModuleContext) string {
Jiyong Parke3833882020-02-17 17:28:10 +09002825 implName := proptools.String(module.properties.Lib_name)
Colin Cross56a83212020-09-15 18:30:11 -07002826 if apexInfo := ctx.Provider(android.ApexInfoProvider).(android.ApexInfo); !apexInfo.IsForPlatform() {
Colin Crosse07f2312020-08-13 11:24:56 -07002827 // TODO(b/146468504): ApexVariationName() is only a soong module name, not apex name.
Jiyong Parke3833882020-02-17 17:28:10 +09002828 // In most cases, this works fine. But when apex_name is set or override_apex is used
2829 // this can be wrong.
Colin Cross56a83212020-09-15 18:30:11 -07002830 return fmt.Sprintf("/apex/%s/javalib/%s.jar", apexInfo.ApexVariationName, implName)
Jiyong Parke3833882020-02-17 17:28:10 +09002831 }
2832 partition := "system"
2833 if module.SocSpecific() {
2834 partition = "vendor"
2835 } else if module.DeviceSpecific() {
2836 partition = "odm"
2837 } else if module.ProductSpecific() {
2838 partition = "product"
2839 } else if module.SystemExtSpecific() {
2840 partition = "system_ext"
2841 }
2842 return "/" + partition + "/framework/" + implName + ".jar"
2843}
2844
Pedro Loureiro9956e5e2021-09-07 17:21:59 +00002845func formattedOptionalSdkLevelAttribute(ctx android.ModuleContext, attrName string, value *string) string {
2846 if value == nil {
2847 return ""
2848 }
2849 apiLevel, err := android.ApiLevelFromUser(ctx, *value)
2850 if err != nil {
Pedro Loureiroba6682f2021-10-29 09:32:32 +00002851 // attributes in bp files have underscores but in the xml have dashes.
2852 ctx.PropertyErrorf(strings.ReplaceAll(attrName, "-", "_"), err.Error())
Pedro Loureiro9956e5e2021-09-07 17:21:59 +00002853 return ""
2854 }
Pedro Loureirob638c622021-12-22 15:28:05 +00002855 if apiLevel.IsCurrent() {
2856 // passing "current" would always mean a future release, never the current (or the current in
2857 // progress) which means some conditions would never be triggered.
2858 ctx.PropertyErrorf(strings.ReplaceAll(attrName, "-", "_"),
2859 `"current" is not an allowed value for this attribute`)
2860 return ""
2861 }
Pedro Loureiro48991222022-06-17 20:01:21 +00002862 // "safeValue" is safe because it translates finalized codenames to a string
2863 // with their SDK int.
2864 safeValue := apiLevel.String()
2865 return formattedOptionalAttribute(attrName, &safeValue)
Pedro Loureiro9956e5e2021-09-07 17:21:59 +00002866}
2867
2868// formats an attribute for the xml permissions file if the value is not null
2869// returns empty string otherwise
2870func formattedOptionalAttribute(attrName string, value *string) string {
2871 if value == nil {
2872 return ""
2873 }
2874 return fmt.Sprintf(` %s=\"%s\"\n`, attrName, *value)
2875}
2876
2877func (module *sdkLibraryXml) permissionsContents(ctx android.ModuleContext) string {
2878 libName := proptools.String(module.properties.Lib_name)
2879 libNameAttr := formattedOptionalAttribute("name", &libName)
2880 filePath := module.implPath(ctx)
2881 filePathAttr := formattedOptionalAttribute("file", &filePath)
Pedro Loureiroba6682f2021-10-29 09:32:32 +00002882 implicitFromAttr := formattedOptionalSdkLevelAttribute(ctx, "on-bootclasspath-since", module.properties.On_bootclasspath_since)
2883 implicitUntilAttr := formattedOptionalSdkLevelAttribute(ctx, "on-bootclasspath-before", module.properties.On_bootclasspath_before)
2884 minSdkAttr := formattedOptionalSdkLevelAttribute(ctx, "min-device-sdk", module.properties.Min_device_sdk)
2885 maxSdkAttr := formattedOptionalSdkLevelAttribute(ctx, "max-device-sdk", module.properties.Max_device_sdk)
Pedro Loureiro196d3e62021-12-22 19:53:01 +00002886 // <library> is understood in all android versions whereas <apex-library> is only understood from API T (and ignored before that).
2887 // 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 +00002888 var libraryTag string
2889 if module.properties.Min_device_sdk != nil {
Pedro Loureiro196d3e62021-12-22 19:53:01 +00002890 libraryTag = ` <apex-library\n`
Pedro Loureiroc3621422021-09-28 15:40:23 +00002891 } else {
2892 libraryTag = ` <library\n`
2893 }
Pedro Loureiro9956e5e2021-09-07 17:21:59 +00002894
2895 return strings.Join([]string{
2896 `<?xml version=\"1.0\" encoding=\"utf-8\"?>\n`,
2897 `<!-- Copyright (C) 2018 The Android Open Source Project\n`,
2898 `\n`,
2899 ` Licensed under the Apache License, Version 2.0 (the \"License\");\n`,
2900 ` you may not use this file except in compliance with the License.\n`,
2901 ` You may obtain a copy of the License at\n`,
2902 `\n`,
2903 ` http://www.apache.org/licenses/LICENSE-2.0\n`,
2904 `\n`,
2905 ` Unless required by applicable law or agreed to in writing, software\n`,
2906 ` distributed under the License is distributed on an \"AS IS\" BASIS,\n`,
2907 ` WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\n`,
2908 ` See the License for the specific language governing permissions and\n`,
2909 ` limitations under the License.\n`,
2910 `-->\n`,
2911 `<permissions>\n`,
Pedro Loureiroc3621422021-09-28 15:40:23 +00002912 libraryTag,
Pedro Loureiro9956e5e2021-09-07 17:21:59 +00002913 libNameAttr,
2914 filePathAttr,
2915 implicitFromAttr,
2916 implicitUntilAttr,
2917 minSdkAttr,
2918 maxSdkAttr,
2919 ` />\n`,
2920 `</permissions>\n`}, "")
2921}
2922
Jiyong Parke3833882020-02-17 17:28:10 +09002923func (module *sdkLibraryXml) GenerateAndroidBuildActions(ctx android.ModuleContext) {
Colin Cross56a83212020-09-15 18:30:11 -07002924 module.hideApexVariantFromMake = !ctx.Provider(android.ApexInfoProvider).(android.ApexInfo).IsForPlatform()
2925
Jiyong Parke3833882020-02-17 17:28:10 +09002926 libName := proptools.String(module.properties.Lib_name)
Pedro Loureiroc3621422021-09-28 15:40:23 +00002927 module.selfValidate(ctx)
Pedro Loureiro9956e5e2021-09-07 17:21:59 +00002928 xmlContent := module.permissionsContents(ctx)
Jiyong Parke3833882020-02-17 17:28:10 +09002929
2930 module.outputFilePath = android.PathForModuleOut(ctx, libName+".xml").OutputPath
Colin Crossf1a035e2020-11-16 17:32:30 -08002931 rule := android.NewRuleBuilder(pctx, ctx)
Jiyong Parke3833882020-02-17 17:28:10 +09002932 rule.Command().
2933 Text("/bin/bash -c \"echo -e '" + xmlContent + "'\" > ").
2934 Output(module.outputFilePath)
2935
Colin Crossf1a035e2020-11-16 17:32:30 -08002936 rule.Build("java_sdk_xml", "Permission XML")
Jiyong Parke3833882020-02-17 17:28:10 +09002937
2938 module.installDirPath = android.PathForModuleInstall(ctx, "etc", module.SubDir())
2939}
2940
2941func (module *sdkLibraryXml) AndroidMkEntries() []android.AndroidMkEntries {
Colin Cross56a83212020-09-15 18:30:11 -07002942 if module.hideApexVariantFromMake {
satayev8f088b02021-12-06 11:40:46 +00002943 return []android.AndroidMkEntries{{
Jiyong Parke3833882020-02-17 17:28:10 +09002944 Disabled: true,
2945 }}
2946 }
2947
satayev8f088b02021-12-06 11:40:46 +00002948 return []android.AndroidMkEntries{{
Jiyong Parke3833882020-02-17 17:28:10 +09002949 Class: "ETC",
2950 OutputFile: android.OptionalPathForPath(module.outputFilePath),
2951 ExtraEntries: []android.AndroidMkExtraEntriesFunc{
Colin Crossaa255532020-07-03 13:18:24 -07002952 func(ctx android.AndroidMkExtraEntriesContext, entries *android.AndroidMkEntries) {
Jiyong Parke3833882020-02-17 17:28:10 +09002953 entries.SetString("LOCAL_MODULE_TAGS", "optional")
Colin Crossc68db4b2021-11-11 18:59:15 -08002954 entries.SetString("LOCAL_MODULE_PATH", module.installDirPath.String())
Jiyong Parke3833882020-02-17 17:28:10 +09002955 entries.SetString("LOCAL_INSTALLED_MODULE_STEM", module.outputFilePath.Base())
2956 },
2957 },
2958 }}
2959}
Paul Duffindd46f712020-02-10 13:37:10 +00002960
Pedro Loureiroc3621422021-09-28 15:40:23 +00002961func (module *sdkLibraryXml) selfValidate(ctx android.ModuleContext) {
2962 module.validateAtLeastTAttributes(ctx)
2963 module.validateMinAndMaxDeviceSdk(ctx)
2964 module.validateMinMaxDeviceSdkAndModuleMinSdk(ctx)
2965 module.validateOnBootclasspathBeforeRequirements(ctx)
2966}
2967
2968func (module *sdkLibraryXml) validateAtLeastTAttributes(ctx android.ModuleContext) {
2969 t := android.ApiLevelOrPanic(ctx, "Tiramisu")
2970 module.attrAtLeastT(ctx, t, module.properties.Min_device_sdk, "min_device_sdk")
2971 module.attrAtLeastT(ctx, t, module.properties.Max_device_sdk, "max_device_sdk")
2972 module.attrAtLeastT(ctx, t, module.properties.On_bootclasspath_before, "on_bootclasspath_before")
2973 module.attrAtLeastT(ctx, t, module.properties.On_bootclasspath_since, "on_bootclasspath_since")
2974}
2975
2976func (module *sdkLibraryXml) attrAtLeastT(ctx android.ModuleContext, t android.ApiLevel, attr *string, attrName string) {
2977 if attr != nil {
2978 if level, err := android.ApiLevelFromUser(ctx, *attr); err == nil {
2979 // we will inform the user of invalid inputs when we try to write the
2980 // permissions xml file so we don't need to do it here
2981 if t.GreaterThan(level) {
2982 ctx.PropertyErrorf(attrName, "Attribute value needs to be at least T")
2983 }
2984 }
2985 }
2986}
2987
2988func (module *sdkLibraryXml) validateMinAndMaxDeviceSdk(ctx android.ModuleContext) {
2989 if module.properties.Min_device_sdk != nil && module.properties.Max_device_sdk != nil {
2990 min, minErr := android.ApiLevelFromUser(ctx, *module.properties.Min_device_sdk)
2991 max, maxErr := android.ApiLevelFromUser(ctx, *module.properties.Max_device_sdk)
2992 if minErr == nil && maxErr == nil {
2993 // we will inform the user of invalid inputs when we try to write the
2994 // permissions xml file so we don't need to do it here
2995 if min.GreaterThan(max) {
2996 ctx.ModuleErrorf("min_device_sdk can't be greater than max_device_sdk")
2997 }
2998 }
2999 }
3000}
3001
3002func (module *sdkLibraryXml) validateMinMaxDeviceSdkAndModuleMinSdk(ctx android.ModuleContext) {
3003 moduleMinApi := android.ApiLevelOrPanic(ctx, *module.properties.Sdk_library_min_api_level)
3004 if module.properties.Min_device_sdk != nil {
3005 api, err := android.ApiLevelFromUser(ctx, *module.properties.Min_device_sdk)
3006 if err == nil {
3007 if moduleMinApi.GreaterThan(api) {
3008 ctx.PropertyErrorf("min_device_sdk", "Can't be less than module's min sdk (%s)", moduleMinApi)
3009 }
3010 }
3011 }
3012 if module.properties.Max_device_sdk != nil {
3013 api, err := android.ApiLevelFromUser(ctx, *module.properties.Max_device_sdk)
3014 if err == nil {
3015 if moduleMinApi.GreaterThan(api) {
3016 ctx.PropertyErrorf("max_device_sdk", "Can't be less than module's min sdk (%s)", moduleMinApi)
3017 }
3018 }
3019 }
3020}
3021
3022func (module *sdkLibraryXml) validateOnBootclasspathBeforeRequirements(ctx android.ModuleContext) {
3023 moduleMinApi := android.ApiLevelOrPanic(ctx, *module.properties.Sdk_library_min_api_level)
3024 if module.properties.On_bootclasspath_before != nil {
3025 t := android.ApiLevelOrPanic(ctx, "Tiramisu")
3026 // if we use the attribute, then we need to do this validation
3027 if moduleMinApi.LessThan(t) {
3028 // if minAPi is < T, then we need to have min_device_sdk (which only accepts T+)
3029 if module.properties.Min_device_sdk == nil {
3030 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")
3031 }
3032 }
3033 }
3034}
3035
Paul Duffindd46f712020-02-10 13:37:10 +00003036type sdkLibrarySdkMemberType struct {
3037 android.SdkMemberTypeBase
3038}
3039
Paul Duffin296701e2021-07-14 10:29:36 +01003040func (s *sdkLibrarySdkMemberType) AddDependencies(ctx android.SdkDependencyContext, dependencyTag blueprint.DependencyTag, names []string) {
3041 ctx.AddVariationDependencies(nil, dependencyTag, names...)
Paul Duffindd46f712020-02-10 13:37:10 +00003042}
3043
3044func (s *sdkLibrarySdkMemberType) IsInstance(module android.Module) bool {
3045 _, ok := module.(*SdkLibrary)
3046 return ok
3047}
3048
3049func (s *sdkLibrarySdkMemberType) AddPrebuiltModule(ctx android.SdkMemberContext, member android.SdkMember) android.BpModule {
3050 return ctx.SnapshotBuilder().AddPrebuiltModule(member, "java_sdk_library_import")
3051}
3052
3053func (s *sdkLibrarySdkMemberType) CreateVariantPropertiesStruct() android.SdkMemberProperties {
3054 return &sdkLibrarySdkMemberProperties{}
3055}
3056
Paul Duffin976b0e52021-04-27 23:20:26 +01003057var javaSdkLibrarySdkMemberType = &sdkLibrarySdkMemberType{
3058 android.SdkMemberTypeBase{
3059 PropertyName: "java_sdk_libs",
3060 SupportsSdk: true,
3061 },
3062}
3063
Paul Duffindd46f712020-02-10 13:37:10 +00003064type sdkLibrarySdkMemberProperties struct {
3065 android.SdkMemberPropertiesBase
3066
Paul Duffine8409952022-09-22 16:24:46 +01003067 // Stem name for files in the sdk snapshot.
3068 //
3069 // This is used to construct the path names of various sdk library files in the sdk snapshot to
3070 // make sure that they match the finalized versions of those files in prebuilts/sdk.
3071 //
3072 // This property is marked as keep so that it will be kept in all instances of this struct, will
3073 // not be cleared but will be copied to common structs. That is needed because this field is used
3074 // to construct many file names for other parts of this struct and so it needs to be present in
3075 // all structs. If it was not marked as keep then it would be cleared in some structs and so would
3076 // be unavailable for generating file names if there were other properties that were still set.
3077 Stem string `sdk:"keep"`
3078
Paul Duffindd46f712020-02-10 13:37:10 +00003079 // Scope to per scope properties.
Paul Duffin106a3a42022-01-27 16:39:06 +00003080 Scopes map[*apiScope]*scopeProperties
Paul Duffindd46f712020-02-10 13:37:10 +00003081
Paul Duffin3d1248c2020-04-09 00:10:17 +01003082 // The Java stubs source files.
3083 Stub_srcs []string
Paul Duffinf7a64332020-05-13 16:54:55 +01003084
3085 // The naming scheme.
3086 Naming_scheme *string
Paul Duffind7eb1c22020-05-26 20:57:10 +01003087
3088 // True if the java_sdk_library_import is for a shared library, false
3089 // otherwise.
3090 Shared_library *bool
Paul Duffina2ae7e02020-09-11 11:55:00 +01003091
Paul Duffin1267d872021-04-16 17:21:36 +01003092 // True if the stub imports should produce dex jars.
3093 Compile_dex *bool
3094
Paul Duffina2ae7e02020-09-11 11:55:00 +01003095 // The paths to the doctag files to add to the prebuilt.
3096 Doctag_paths android.Paths
Paul Duffin869de142021-07-15 14:14:41 +01003097
3098 Permitted_packages []string
Pedro Loureiro9956e5e2021-09-07 17:21:59 +00003099
3100 // Signals that this shared library is part of the bootclasspath starting
3101 // on the version indicated in this attribute.
3102 //
3103 // This will make platforms at this level and above to ignore
3104 // <uses-library> tags with this library name because the library is already
3105 // available
3106 On_bootclasspath_since *string
3107
3108 // Signals that this shared library was part of the bootclasspath before
3109 // (but not including) the version indicated in this attribute.
3110 //
3111 // The system will automatically add a <uses-library> tag with this library to
3112 // apps that target any SDK less than the version indicated in this attribute.
3113 On_bootclasspath_before *string
3114
3115 // Indicates that PackageManager should ignore this shared library if the
3116 // platform is below the version indicated in this attribute.
3117 //
3118 // This means that the device won't recognise this library as installed.
3119 Min_device_sdk *string
3120
3121 // Indicates that PackageManager should ignore this shared library if the
3122 // platform is above the version indicated in this attribute.
3123 //
3124 // This means that the device won't recognise this library as installed.
3125 Max_device_sdk *string
Jiakai Zhang9c4dc192023-02-09 00:09:24 +08003126
3127 DexPreoptProfileGuided *bool `supported_build_releases:"UpsideDownCake+"`
Paul Duffindd46f712020-02-10 13:37:10 +00003128}
3129
3130type scopeProperties struct {
Paul Duffin1fd005d2020-04-09 01:08:11 +01003131 Jars android.Paths
3132 StubsSrcJar android.Path
3133 CurrentApiFile android.Path
3134 RemovedApiFile android.Path
Paul Duffine7babdb2022-02-10 13:06:54 +00003135 AnnotationsZip android.Path `supported_build_releases:"Tiramisu+"`
Paul Duffin1fd005d2020-04-09 01:08:11 +01003136 SdkVersion string
Paul Duffindd46f712020-02-10 13:37:10 +00003137}
3138
3139func (s *sdkLibrarySdkMemberProperties) PopulateFromVariant(ctx android.SdkMemberContext, variant android.Module) {
3140 sdk := variant.(*SdkLibrary)
3141
Paul Duffine8409952022-09-22 16:24:46 +01003142 // Copy the stem name for files in the sdk snapshot.
3143 s.Stem = sdk.distStem()
3144
Paul Duffin106a3a42022-01-27 16:39:06 +00003145 s.Scopes = make(map[*apiScope]*scopeProperties)
Paul Duffindd46f712020-02-10 13:37:10 +00003146 for _, apiScope := range allApiScopes {
Paul Duffin803a9562020-05-20 11:52:25 +01003147 paths := sdk.findScopePaths(apiScope)
3148 if paths == nil {
3149 continue
3150 }
3151
Paul Duffindd46f712020-02-10 13:37:10 +00003152 jars := paths.stubsImplPath
3153 if len(jars) > 0 {
3154 properties := scopeProperties{}
3155 properties.Jars = jars
Paul Duffin780c5f42020-05-12 15:52:55 +01003156 properties.SdkVersion = sdk.sdkVersionForStubsLibrary(ctx.SdkModuleContext(), apiScope)
Paul Duffin0f8faff2020-05-20 16:18:00 +01003157 properties.StubsSrcJar = paths.stubsSrcJar.Path()
Paul Duffin10269f12020-06-19 18:39:55 +01003158 if paths.currentApiFilePath.Valid() {
3159 properties.CurrentApiFile = paths.currentApiFilePath.Path()
3160 }
3161 if paths.removedApiFilePath.Valid() {
3162 properties.RemovedApiFile = paths.removedApiFilePath.Path()
3163 }
Anton Hanssond78eb762021-09-21 15:25:12 +01003164 // The annotations zip is only available for modules that set annotations_enabled: true.
3165 if paths.annotationsZip.Valid() {
3166 properties.AnnotationsZip = paths.annotationsZip.Path()
3167 }
Paul Duffin106a3a42022-01-27 16:39:06 +00003168 s.Scopes[apiScope] = &properties
Paul Duffindd46f712020-02-10 13:37:10 +00003169 }
3170 }
3171
Paul Duffindfa131e2020-05-15 20:37:11 +01003172 s.Naming_scheme = sdk.commonSdkLibraryProperties.Naming_scheme
Paul Duffind7eb1c22020-05-26 20:57:10 +01003173 s.Shared_library = proptools.BoolPtr(sdk.sharedLibrary())
Paul Duffin1267d872021-04-16 17:21:36 +01003174 s.Compile_dex = sdk.dexProperties.Compile_dex
Paul Duffina2ae7e02020-09-11 11:55:00 +01003175 s.Doctag_paths = sdk.doctagPaths
Paul Duffin869de142021-07-15 14:14:41 +01003176 s.Permitted_packages = sdk.PermittedPackagesForUpdatableBootJars()
Pedro Loureiro9956e5e2021-09-07 17:21:59 +00003177 s.On_bootclasspath_since = sdk.commonSdkLibraryProperties.On_bootclasspath_since
3178 s.On_bootclasspath_before = sdk.commonSdkLibraryProperties.On_bootclasspath_before
3179 s.Min_device_sdk = sdk.commonSdkLibraryProperties.Min_device_sdk
3180 s.Max_device_sdk = sdk.commonSdkLibraryProperties.Max_device_sdk
Jiakai Zhang9c4dc192023-02-09 00:09:24 +08003181
3182 if sdk.dexpreopter.dexpreoptProperties.Dex_preopt_result.Profile_guided {
3183 s.DexPreoptProfileGuided = proptools.BoolPtr(true)
3184 }
Paul Duffindd46f712020-02-10 13:37:10 +00003185}
3186
3187func (s *sdkLibrarySdkMemberProperties) AddToPropertySet(ctx android.SdkMemberContext, propertySet android.BpPropertySet) {
Paul Duffinf7a64332020-05-13 16:54:55 +01003188 if s.Naming_scheme != nil {
3189 propertySet.AddProperty("naming_scheme", proptools.String(s.Naming_scheme))
3190 }
Paul Duffind7eb1c22020-05-26 20:57:10 +01003191 if s.Shared_library != nil {
3192 propertySet.AddProperty("shared_library", *s.Shared_library)
3193 }
Paul Duffin1267d872021-04-16 17:21:36 +01003194 if s.Compile_dex != nil {
3195 propertySet.AddProperty("compile_dex", *s.Compile_dex)
3196 }
Paul Duffin869de142021-07-15 14:14:41 +01003197 if len(s.Permitted_packages) > 0 {
3198 propertySet.AddProperty("permitted_packages", s.Permitted_packages)
3199 }
Jiakai Zhang9c4dc192023-02-09 00:09:24 +08003200 dexPreoptSet := propertySet.AddPropertySet("dex_preopt")
3201 if s.DexPreoptProfileGuided != nil {
3202 dexPreoptSet.AddProperty("profile_guided", proptools.Bool(s.DexPreoptProfileGuided))
3203 }
Paul Duffinf7a64332020-05-13 16:54:55 +01003204
Paul Duffine8409952022-09-22 16:24:46 +01003205 stem := s.Stem
3206
Paul Duffindd46f712020-02-10 13:37:10 +00003207 for _, apiScope := range allApiScopes {
3208 if properties, ok := s.Scopes[apiScope]; ok {
Paul Duffin6b836ba2020-05-13 19:19:49 +01003209 scopeSet := propertySet.AddPropertySet(apiScope.propertyName)
Paul Duffindd46f712020-02-10 13:37:10 +00003210
Paul Duffin958806b2022-05-16 13:10:47 +00003211 scopeDir := apiScope.snapshotRelativeDir()
Paul Duffin3d1248c2020-04-09 00:10:17 +01003212
Paul Duffindd46f712020-02-10 13:37:10 +00003213 var jars []string
3214 for _, p := range properties.Jars {
Paul Duffine8409952022-09-22 16:24:46 +01003215 dest := filepath.Join(scopeDir, stem+"-stubs.jar")
Paul Duffindd46f712020-02-10 13:37:10 +00003216 ctx.SnapshotBuilder().CopyToSnapshot(p, dest)
3217 jars = append(jars, dest)
3218 }
3219 scopeSet.AddProperty("jars", jars)
3220
Paul Duffin22628d52021-05-12 23:13:22 +01003221 if ctx.SdkModuleContext().Config().IsEnvTrue("SOONG_SDK_SNAPSHOT_USE_SRCJAR") {
3222 // Copy the stubs source jar into the snapshot zip as is.
Paul Duffine8409952022-09-22 16:24:46 +01003223 srcJarSnapshotPath := filepath.Join(scopeDir, stem+".srcjar")
Paul Duffin22628d52021-05-12 23:13:22 +01003224 ctx.SnapshotBuilder().CopyToSnapshot(properties.StubsSrcJar, srcJarSnapshotPath)
3225 scopeSet.AddProperty("stub_srcs", []string{srcJarSnapshotPath})
3226 } else {
3227 // Merge the stubs source jar into the snapshot zip so that when it is unpacked
3228 // the source files are also unpacked.
Paul Duffine8409952022-09-22 16:24:46 +01003229 snapshotRelativeDir := filepath.Join(scopeDir, stem+"_stub_sources")
Paul Duffin22628d52021-05-12 23:13:22 +01003230 ctx.SnapshotBuilder().UnzipToSnapshot(properties.StubsSrcJar, snapshotRelativeDir)
3231 scopeSet.AddProperty("stub_srcs", []string{snapshotRelativeDir})
3232 }
Paul Duffin3d1248c2020-04-09 00:10:17 +01003233
Paul Duffin1fd005d2020-04-09 01:08:11 +01003234 if properties.CurrentApiFile != nil {
Paul Duffine8409952022-09-22 16:24:46 +01003235 currentApiSnapshotPath := apiScope.snapshotRelativeCurrentApiTxtPath(stem)
Paul Duffin1fd005d2020-04-09 01:08:11 +01003236 ctx.SnapshotBuilder().CopyToSnapshot(properties.CurrentApiFile, currentApiSnapshotPath)
3237 scopeSet.AddProperty("current_api", currentApiSnapshotPath)
3238 }
3239
3240 if properties.RemovedApiFile != nil {
Paul Duffine8409952022-09-22 16:24:46 +01003241 removedApiSnapshotPath := apiScope.snapshotRelativeRemovedApiTxtPath(stem)
Paul Duffin3dbf9fd2020-06-02 13:00:02 +01003242 ctx.SnapshotBuilder().CopyToSnapshot(properties.RemovedApiFile, removedApiSnapshotPath)
Paul Duffin1fd005d2020-04-09 01:08:11 +01003243 scopeSet.AddProperty("removed_api", removedApiSnapshotPath)
3244 }
3245
Anton Hanssond78eb762021-09-21 15:25:12 +01003246 if properties.AnnotationsZip != nil {
Paul Duffine8409952022-09-22 16:24:46 +01003247 annotationsSnapshotPath := filepath.Join(scopeDir, stem+"_annotations.zip")
Anton Hanssond78eb762021-09-21 15:25:12 +01003248 ctx.SnapshotBuilder().CopyToSnapshot(properties.AnnotationsZip, annotationsSnapshotPath)
3249 scopeSet.AddProperty("annotations", annotationsSnapshotPath)
3250 }
3251
Paul Duffindd46f712020-02-10 13:37:10 +00003252 if properties.SdkVersion != "" {
3253 scopeSet.AddProperty("sdk_version", properties.SdkVersion)
3254 }
3255 }
3256 }
3257
Paul Duffina2ae7e02020-09-11 11:55:00 +01003258 if len(s.Doctag_paths) > 0 {
3259 dests := []string{}
3260 for _, p := range s.Doctag_paths {
3261 dest := filepath.Join("doctags", p.Rel())
3262 ctx.SnapshotBuilder().CopyToSnapshot(p, dest)
3263 dests = append(dests, dest)
3264 }
3265 propertySet.AddProperty("doctag_files", dests)
3266 }
Paul Duffindd46f712020-02-10 13:37:10 +00003267}