blob: e7c84f6ffacda5e9c68f328f58a6bfeac6f5c345 [file] [log] [blame]
Jiyong Parkc678ad32018-04-10 13:07:10 +09001// Copyright 2018 Google Inc. All rights reserved.
2//
3// Licensed under the Apache License, Version 2.0 (the "License");
4// you may not use this file except in compliance with the License.
5// You may obtain a copy of the License at
6//
7// http://www.apache.org/licenses/LICENSE-2.0
8//
9// Unless required by applicable law or agreed to in writing, software
10// distributed under the License is distributed on an "AS IS" BASIS,
11// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12// See the License for the specific language governing permissions and
13// limitations under the License.
14
15package java
16
17import (
Jiyong Parkc678ad32018-04-10 13:07:10 +090018 "fmt"
19 "path"
Sundong Ahn054b19a2018-10-19 13:46:09 +090020 "path/filepath"
Paul Duffin46a26a82020-04-07 19:27:04 +010021 "reflect"
Paul Duffin46dc45a2020-05-14 15:39:10 +010022 "regexp"
Jiyong Park82484c02018-04-23 21:41:26 +090023 "sort"
Jiyong Parkc678ad32018-04-10 13:07:10 +090024 "strings"
Jiyong Park82484c02018-04-23 21:41:26 +090025 "sync"
Jiyong Parkc678ad32018-04-10 13:07:10 +090026
Chris Parsons39a16972023-06-08 14:28:51 +000027 "android/soong/ui/metrics/bp2build_metrics_proto"
Paul Duffinc0036492023-06-21 15:42:49 +010028
Paul Duffind1b3a922020-01-22 11:57:20 +000029 "github.com/google/blueprint"
Jiyong Parkc678ad32018-04-10 13:07:10 +090030 "github.com/google/blueprint/proptools"
Paul Duffin46a26a82020-04-07 19:27:04 +010031
32 "android/soong/android"
Zi Wangb2179e32023-01-31 15:53:30 -080033 "android/soong/bazel"
Ulya Trafimovichdbf31662020-12-17 12:07:54 +000034 "android/soong/dexpreopt"
Jiyong Parkc678ad32018-04-10 13:07:10 +090035)
36
Jooyung Han58f26ab2019-12-18 15:34:32 +090037const (
Pedro Loureiro9956e5e2021-09-07 17:21:59 +000038 sdkXmlFileSuffix = ".xml"
Jiyong Parkc678ad32018-04-10 13:07:10 +090039)
40
Paul Duffind1b3a922020-01-22 11:57:20 +000041// A tag to associated a dependency with a specific api scope.
42type scopeDependencyTag struct {
43 blueprint.BaseDependencyTag
44 name string
45 apiScope *apiScope
Paul Duffinc8782502020-04-29 20:45:27 +010046
47 // Function for extracting appropriate path information from the dependency.
Colin Crossdcf71b22021-02-01 13:59:03 -080048 depInfoExtractor func(paths *scopePaths, ctx android.ModuleContext, dep android.Module) error
Paul Duffinc8782502020-04-29 20:45:27 +010049}
50
51// Extract tag specific information from the dependency.
52func (tag scopeDependencyTag) extractDepInfo(ctx android.ModuleContext, dep android.Module, paths *scopePaths) {
Colin Crossdcf71b22021-02-01 13:59:03 -080053 err := tag.depInfoExtractor(paths, ctx, dep)
Paul Duffinc8782502020-04-29 20:45:27 +010054 if err != nil {
55 ctx.ModuleErrorf("has an invalid {scopeDependencyTag: %s} dependency on module %s: %s", tag.name, ctx.OtherModuleName(dep), err.Error())
56 }
Paul Duffind1b3a922020-01-22 11:57:20 +000057}
58
Paul Duffin80342d72020-06-26 22:08:43 +010059var _ android.ReplaceSourceWithPrebuilt = (*scopeDependencyTag)(nil)
60
61func (tag scopeDependencyTag) ReplaceSourceWithPrebuilt() bool {
62 return false
63}
64
Paul Duffind1b3a922020-01-22 11:57:20 +000065// Provides information about an api scope, e.g. public, system, test.
66type apiScope struct {
67 // The name of the api scope, e.g. public, system, test
68 name string
69
Paul Duffin97b53b82020-05-05 14:40:52 +010070 // The api scope that this scope extends.
Paul Duffind0b9fca2022-09-30 18:11:41 +010071 //
72 // This organizes the scopes into an extension hierarchy.
73 //
74 // If set this means that the API provided by this scope includes the API provided by the scope
75 // set in this field.
Paul Duffin97b53b82020-05-05 14:40:52 +010076 extends *apiScope
77
Paul Duffind0b9fca2022-09-30 18:11:41 +010078 // The next api scope that a library that uses this scope can access.
79 //
80 // This organizes the scopes into an access hierarchy.
81 //
82 // If set this means that a library that can access this API can also access the API provided by
83 // the scope set in this field.
84 //
85 // A module that sets sdk_version: "<scope>_current" should have access to the <scope> API of
86 // every java_sdk_library that it depends on. If the library does not provide an API for <scope>
87 // then it will traverse up this access hierarchy to find an API that it does provide.
88 //
89 // If this is not set then it defaults to the scope set in extends.
90 canAccess *apiScope
91
Paul Duffin3375e352020-04-28 10:44:03 +010092 // The legacy enabled status for a specific scope can be dependent on other
93 // properties that have been specified on the library so it is provided by
94 // a function that can determine the status by examining those properties.
95 legacyEnabledStatus func(module *SdkLibrary) bool
96
97 // The default enabled status for non-legacy behavior, which is triggered by
98 // explicitly enabling at least one api scope.
99 defaultEnabledStatus bool
100
101 // Gets a pointer to the scope specific properties.
102 scopeSpecificProperties func(module *SdkLibrary) *ApiScopeProperties
103
Paul Duffin46a26a82020-04-07 19:27:04 +0100104 // The name of the field in the dynamically created structure.
105 fieldName string
106
Paul Duffin6b836ba2020-05-13 19:19:49 +0100107 // The name of the property in the java_sdk_library_import
108 propertyName string
109
Paul Duffind1b3a922020-01-22 11:57:20 +0000110 // The tag to use to depend on the stubs library module.
111 stubsTag scopeDependencyTag
112
Paul Duffin0ff08bd2020-04-29 13:30:54 +0100113 // The tag to use to depend on the stubs source module (if separate from the API module).
114 stubsSourceTag scopeDependencyTag
115
116 // The tag to use to depend on the API file generating module (if separate from the stubs source module).
117 apiFileTag scopeDependencyTag
118
Paul Duffinc8782502020-04-29 20:45:27 +0100119 // The tag to use to depend on the stubs source and API module.
120 stubsSourceAndApiTag scopeDependencyTag
Paul Duffind1b3a922020-01-22 11:57:20 +0000121
Paul Duffin958806b2022-05-16 13:10:47 +0000122 // The tag to use to depend on the module that provides the latest version of the API .txt file.
123 latestApiModuleTag scopeDependencyTag
124
125 // The tag to use to depend on the module that provides the latest version of the API removed.txt
126 // file.
127 latestRemovedApiModuleTag scopeDependencyTag
128
Paul Duffind1b3a922020-01-22 11:57:20 +0000129 // The scope specific prefix to add to the api file base of "current.txt" or "removed.txt".
130 apiFilePrefix string
131
Paul Duffind0b9fca2022-09-30 18:11:41 +0100132 // The scope specific suffix to add to the sdk library module name to construct a scope specific
Paul Duffind1b3a922020-01-22 11:57:20 +0000133 // module name.
134 moduleSuffix string
135
Paul Duffind1b3a922020-01-22 11:57:20 +0000136 // SDK version that the stubs library is built against. Note that this is always
137 // *current. Older stubs library built with a numbered SDK version is created from
138 // the prebuilt jar.
139 sdkVersion string
Paul Duffin1fb487d2020-04-07 18:50:10 +0100140
Paul Duffin15f34ef2020-07-20 18:04:44 +0100141 // The annotation that identifies this API level, empty for the public API scope.
142 annotation string
143
Paul Duffin1fb487d2020-04-07 18:50:10 +0100144 // Extra arguments to pass to droidstubs for this scope.
Paul Duffin0ff08bd2020-04-29 13:30:54 +0100145 //
Paul Duffin15f34ef2020-07-20 18:04:44 +0100146 // This is not used directly but is used to construct the droidstubsArgs.
147 extraArgs []string
Paul Duffin0ff08bd2020-04-29 13:30:54 +0100148
Paul Duffin15f34ef2020-07-20 18:04:44 +0100149 // The args that must be passed to droidstubs to generate the API and stubs source
150 // for this scope, constructed dynamically by initApiScope().
Paul Duffin0ff08bd2020-04-29 13:30:54 +0100151 //
152 // The API only includes the additional members that this scope adds over the scope
153 // that it extends.
Paul Duffin15f34ef2020-07-20 18:04:44 +0100154 //
155 // The stubs source must include the definitions of everything that is in this
156 // api scope and all the scopes that this one extends.
157 droidstubsArgs []string
Paul Duffin0ff08bd2020-04-29 13:30:54 +0100158
Anton Hansson6478ac12020-05-02 11:19:36 +0100159 // Whether the api scope can be treated as unstable, and should skip compat checks.
160 unstable bool
Jihoon Kang1c92c3e2023-03-23 17:44:51 +0000161
162 // Represents the SDK kind of this scope.
163 kind android.SdkKind
Paul Duffind1b3a922020-01-22 11:57:20 +0000164}
165
166// Initialize a scope, creating and adding appropriate dependency tags
167func initApiScope(scope *apiScope) *apiScope {
Paul Duffinc8782502020-04-29 20:45:27 +0100168 name := scope.name
Paul Duffin46dc45a2020-05-14 15:39:10 +0100169 scopeByName[name] = scope
170 allScopeNames = append(allScopeNames, name)
Paul Duffin6b836ba2020-05-13 19:19:49 +0100171 scope.propertyName = strings.ReplaceAll(name, "-", "_")
172 scope.fieldName = proptools.FieldNameForProperty(scope.propertyName)
Paul Duffind1b3a922020-01-22 11:57:20 +0000173 scope.stubsTag = scopeDependencyTag{
Paul Duffinc8782502020-04-29 20:45:27 +0100174 name: name + "-stubs",
175 apiScope: scope,
176 depInfoExtractor: (*scopePaths).extractStubsLibraryInfoFromDependency,
Paul Duffind1b3a922020-01-22 11:57:20 +0000177 }
Paul Duffin0ff08bd2020-04-29 13:30:54 +0100178 scope.stubsSourceTag = scopeDependencyTag{
179 name: name + "-stubs-source",
180 apiScope: scope,
181 depInfoExtractor: (*scopePaths).extractStubsSourceInfoFromDep,
182 }
183 scope.apiFileTag = scopeDependencyTag{
184 name: name + "-api",
185 apiScope: scope,
186 depInfoExtractor: (*scopePaths).extractApiInfoFromDep,
187 }
Paul Duffinc8782502020-04-29 20:45:27 +0100188 scope.stubsSourceAndApiTag = scopeDependencyTag{
189 name: name + "-stubs-source-and-api",
190 apiScope: scope,
191 depInfoExtractor: (*scopePaths).extractStubsSourceAndApiInfoFromApiStubsProvider,
Paul Duffind1b3a922020-01-22 11:57:20 +0000192 }
Paul Duffin958806b2022-05-16 13:10:47 +0000193 scope.latestApiModuleTag = scopeDependencyTag{
194 name: name + "-latest-api",
195 apiScope: scope,
196 depInfoExtractor: (*scopePaths).extractLatestApiPath,
197 }
198 scope.latestRemovedApiModuleTag = scopeDependencyTag{
199 name: name + "-latest-removed-api",
200 apiScope: scope,
201 depInfoExtractor: (*scopePaths).extractLatestRemovedApiPath,
202 }
Paul Duffin0ff08bd2020-04-29 13:30:54 +0100203
204 // To get the args needed to generate the stubs source append all the args from
205 // this scope and all the scopes it extends as each set of args adds additional
206 // members to the stubs.
Paul Duffin15f34ef2020-07-20 18:04:44 +0100207 var scopeSpecificArgs []string
208 if scope.annotation != "" {
209 scopeSpecificArgs = []string{"--show-annotation", scope.annotation}
Paul Duffin0ff08bd2020-04-29 13:30:54 +0100210 }
Paul Duffin15f34ef2020-07-20 18:04:44 +0100211 for s := scope; s != nil; s = s.extends {
212 scopeSpecificArgs = append(scopeSpecificArgs, s.extraArgs...)
Paul Duffin0ff08bd2020-04-29 13:30:54 +0100213
Paul Duffin15f34ef2020-07-20 18:04:44 +0100214 // Ensure that the generated stubs includes all the API elements from the API scope
215 // that this scope extends.
216 if s != scope && s.annotation != "" {
217 scopeSpecificArgs = append(scopeSpecificArgs, "--show-for-stub-purposes-annotation", s.annotation)
218 }
219 }
Paul Duffin0ff08bd2020-04-29 13:30:54 +0100220
Paul Duffind0b9fca2022-09-30 18:11:41 +0100221 // By default, a library that can access a scope can also access the scope it extends.
222 if scope.canAccess == nil {
223 scope.canAccess = scope.extends
224 }
225
Paul Duffin15f34ef2020-07-20 18:04:44 +0100226 // Escape any special characters in the arguments. This is needed because droidstubs
227 // passes these directly to the shell command.
228 scope.droidstubsArgs = proptools.ShellEscapeList(scopeSpecificArgs)
Paul Duffin0ff08bd2020-04-29 13:30:54 +0100229
Paul Duffind1b3a922020-01-22 11:57:20 +0000230 return scope
231}
232
Anton Hansson08f476b2021-04-07 15:32:19 +0100233func (scope *apiScope) stubsLibraryModuleNameSuffix() string {
234 return ".stubs" + scope.moduleSuffix
235}
236
Jihoon Kang1c92c3e2023-03-23 17:44:51 +0000237func (scope *apiScope) apiLibraryModuleName(baseName string) string {
238 return scope.stubsLibraryModuleName(baseName) + ".from-text"
239}
240
Jihoon Kang1147b312023-06-08 23:25:57 +0000241func (scope *apiScope) sourceStubLibraryModuleName(baseName string) string {
242 return scope.stubsLibraryModuleName(baseName) + ".from-source"
243}
244
Paul Duffinc3091c82020-05-08 14:16:20 +0100245func (scope *apiScope) stubsLibraryModuleName(baseName string) string {
Anton Hansson08f476b2021-04-07 15:32:19 +0100246 return baseName + scope.stubsLibraryModuleNameSuffix()
Paul Duffind1b3a922020-01-22 11:57:20 +0000247}
248
Paul Duffinc8782502020-04-29 20:45:27 +0100249func (scope *apiScope) stubsSourceModuleName(baseName string) string {
Paul Duffindd9d0742020-05-08 15:52:37 +0100250 return baseName + ".stubs.source" + scope.moduleSuffix
Paul Duffind1b3a922020-01-22 11:57:20 +0000251}
252
Paul Duffin0ff08bd2020-04-29 13:30:54 +0100253func (scope *apiScope) apiModuleName(baseName string) string {
Paul Duffindd9d0742020-05-08 15:52:37 +0100254 return baseName + ".api" + scope.moduleSuffix
Paul Duffin0ff08bd2020-04-29 13:30:54 +0100255}
256
Paul Duffin3375e352020-04-28 10:44:03 +0100257func (scope *apiScope) String() string {
258 return scope.name
259}
260
Paul Duffin958806b2022-05-16 13:10:47 +0000261// snapshotRelativeDir returns the snapshot directory into which the files related to scopes will
262// be stored.
263func (scope *apiScope) snapshotRelativeDir() string {
264 return filepath.Join("sdk_library", scope.name)
265}
266
267// snapshotRelativeCurrentApiTxtPath returns the snapshot path to the API .txt file for the named
268// library.
269func (scope *apiScope) snapshotRelativeCurrentApiTxtPath(name string) string {
270 return filepath.Join(scope.snapshotRelativeDir(), name+".txt")
271}
272
273// snapshotRelativeRemovedApiTxtPath returns the snapshot path to the removed API .txt file for the
274// named library.
275func (scope *apiScope) snapshotRelativeRemovedApiTxtPath(name string) string {
276 return filepath.Join(scope.snapshotRelativeDir(), name+"-removed.txt")
277}
278
Paul Duffind1b3a922020-01-22 11:57:20 +0000279type apiScopes []*apiScope
280
281func (scopes apiScopes) Strings(accessor func(*apiScope) string) []string {
282 var list []string
283 for _, scope := range scopes {
284 list = append(list, accessor(scope))
285 }
286 return list
287}
288
Jiyong Parkc678ad32018-04-10 13:07:10 +0900289var (
Paul Duffin46dc45a2020-05-14 15:39:10 +0100290 scopeByName = make(map[string]*apiScope)
291 allScopeNames []string
Paul Duffind1b3a922020-01-22 11:57:20 +0000292 apiScopePublic = initApiScope(&apiScope{
Paul Duffin3375e352020-04-28 10:44:03 +0100293 name: "public",
294
295 // Public scope is enabled by default for both legacy and non-legacy modes.
296 legacyEnabledStatus: func(module *SdkLibrary) bool {
297 return true
298 },
299 defaultEnabledStatus: true,
300
301 scopeSpecificProperties: func(module *SdkLibrary) *ApiScopeProperties {
302 return &module.sdkLibraryProperties.Public
303 },
Paul Duffind1b3a922020-01-22 11:57:20 +0000304 sdkVersion: "current",
Jihoon Kang1c92c3e2023-03-23 17:44:51 +0000305 kind: android.SdkPublic,
Paul Duffind1b3a922020-01-22 11:57:20 +0000306 })
307 apiScopeSystem = initApiScope(&apiScope{
Paul Duffin3375e352020-04-28 10:44:03 +0100308 name: "system",
309 extends: apiScopePublic,
310 legacyEnabledStatus: (*SdkLibrary).generateTestAndSystemScopesByDefault,
311 scopeSpecificProperties: func(module *SdkLibrary) *ApiScopeProperties {
312 return &module.sdkLibraryProperties.System
313 },
Paul Duffin15f34ef2020-07-20 18:04:44 +0100314 apiFilePrefix: "system-",
315 moduleSuffix: ".system",
316 sdkVersion: "system_current",
317 annotation: "android.annotation.SystemApi(client=android.annotation.SystemApi.Client.PRIVILEGED_APPS)",
Jihoon Kang1c92c3e2023-03-23 17:44:51 +0000318 kind: android.SdkSystem,
Paul Duffind1b3a922020-01-22 11:57:20 +0000319 })
320 apiScopeTest = initApiScope(&apiScope{
Paul Duffin3375e352020-04-28 10:44:03 +0100321 name: "test",
Anton Hansson4fe970f2020-10-09 10:16:49 +0100322 extends: apiScopeSystem,
Paul Duffin3375e352020-04-28 10:44:03 +0100323 legacyEnabledStatus: (*SdkLibrary).generateTestAndSystemScopesByDefault,
324 scopeSpecificProperties: func(module *SdkLibrary) *ApiScopeProperties {
325 return &module.sdkLibraryProperties.Test
326 },
Paul Duffin15f34ef2020-07-20 18:04:44 +0100327 apiFilePrefix: "test-",
328 moduleSuffix: ".test",
329 sdkVersion: "test_current",
330 annotation: "android.annotation.TestApi",
331 unstable: true,
Jihoon Kang1c92c3e2023-03-23 17:44:51 +0000332 kind: android.SdkTest,
Paul Duffind1b3a922020-01-22 11:57:20 +0000333 })
Paul Duffin8f265b92020-04-28 14:13:56 +0100334 apiScopeModuleLib = initApiScope(&apiScope{
Paul Duffin6b836ba2020-05-13 19:19:49 +0100335 name: "module-lib",
Paul Duffin8f265b92020-04-28 14:13:56 +0100336 extends: apiScopeSystem,
Paul Duffin0c5bae52020-06-02 13:00:08 +0100337 // The module-lib scope is disabled by default in legacy mode.
Paul Duffin8f265b92020-04-28 14:13:56 +0100338 //
339 // Enabling this would break existing usages.
340 legacyEnabledStatus: func(module *SdkLibrary) bool {
341 return false
342 },
343 scopeSpecificProperties: func(module *SdkLibrary) *ApiScopeProperties {
344 return &module.sdkLibraryProperties.Module_lib
345 },
346 apiFilePrefix: "module-lib-",
347 moduleSuffix: ".module_lib",
348 sdkVersion: "module_current",
Paul Duffin15f34ef2020-07-20 18:04:44 +0100349 annotation: "android.annotation.SystemApi(client=android.annotation.SystemApi.Client.MODULE_LIBRARIES)",
Jihoon Kang1c92c3e2023-03-23 17:44:51 +0000350 kind: android.SdkModule,
Paul Duffin8f265b92020-04-28 14:13:56 +0100351 })
Paul Duffin0c5bae52020-06-02 13:00:08 +0100352 apiScopeSystemServer = initApiScope(&apiScope{
353 name: "system-server",
354 extends: apiScopePublic,
Paul Duffind0b9fca2022-09-30 18:11:41 +0100355
356 // The system-server scope can access the module-lib scope.
357 //
358 // A module that provides a system-server API is appended to the standard bootclasspath that is
359 // used by the system server. So, it should be able to access module-lib APIs provided by
360 // libraries on the bootclasspath.
361 canAccess: apiScopeModuleLib,
362
Paul Duffin0c5bae52020-06-02 13:00:08 +0100363 // The system-server scope is disabled by default in legacy mode.
364 //
365 // Enabling this would break existing usages.
366 legacyEnabledStatus: func(module *SdkLibrary) bool {
367 return false
368 },
369 scopeSpecificProperties: func(module *SdkLibrary) *ApiScopeProperties {
370 return &module.sdkLibraryProperties.System_server
371 },
372 apiFilePrefix: "system-server-",
373 moduleSuffix: ".system_server",
374 sdkVersion: "system_server_current",
Paul Duffin15f34ef2020-07-20 18:04:44 +0100375 annotation: "android.annotation.SystemApi(client=android.annotation.SystemApi.Client.SYSTEM_SERVER)",
376 extraArgs: []string{
377 "--hide-annotation", "android.annotation.Hide",
Paul Duffin0c5bae52020-06-02 13:00:08 +0100378 // com.android.* classes are okay in this interface"
Paul Duffin15f34ef2020-07-20 18:04:44 +0100379 "--hide", "InternalClasses",
Paul Duffin0c5bae52020-06-02 13:00:08 +0100380 },
Jihoon Kang1c92c3e2023-03-23 17:44:51 +0000381 kind: android.SdkSystemServer,
Paul Duffin0c5bae52020-06-02 13:00:08 +0100382 })
Paul Duffind1b3a922020-01-22 11:57:20 +0000383 allApiScopes = apiScopes{
384 apiScopePublic,
385 apiScopeSystem,
386 apiScopeTest,
Paul Duffin8f265b92020-04-28 14:13:56 +0100387 apiScopeModuleLib,
Paul Duffin0c5bae52020-06-02 13:00:08 +0100388 apiScopeSystemServer,
Paul Duffind1b3a922020-01-22 11:57:20 +0000389 }
Jiyong Parkc678ad32018-04-10 13:07:10 +0900390)
391
Jiyong Park82484c02018-04-23 21:41:26 +0900392var (
393 javaSdkLibrariesLock sync.Mutex
394)
395
Jiyong Parkc678ad32018-04-10 13:07:10 +0900396// TODO: these are big features that are currently missing
Jiyong Park1be96912018-05-28 18:02:19 +0900397// 1) disallowing linking to the runtime shared lib
398// 2) HTML generation
Jiyong Parkc678ad32018-04-10 13:07:10 +0900399
400func init() {
Paul Duffin43dc1cc2019-12-19 11:18:54 +0000401 RegisterSdkLibraryBuildComponents(android.InitRegistrationContext)
Jiyong Parkc678ad32018-04-10 13:07:10 +0900402
Jiyong Park82484c02018-04-23 21:41:26 +0900403 android.RegisterMakeVarsProvider(pctx, func(ctx android.MakeVarsContext) {
404 javaSdkLibraries := javaSdkLibraries(ctx.Config())
405 sort.Strings(*javaSdkLibraries)
406 ctx.Strict("JAVA_SDK_LIBRARIES", strings.Join(*javaSdkLibraries, " "))
407 })
Paul Duffindd46f712020-02-10 13:37:10 +0000408
409 // Register sdk member types.
Paul Duffin976b0e52021-04-27 23:20:26 +0100410 android.RegisterSdkMemberType(javaSdkLibrarySdkMemberType)
Jiyong Parkc678ad32018-04-10 13:07:10 +0900411}
412
Paul Duffin43dc1cc2019-12-19 11:18:54 +0000413func RegisterSdkLibraryBuildComponents(ctx android.RegistrationContext) {
414 ctx.RegisterModuleType("java_sdk_library", SdkLibraryFactory)
415 ctx.RegisterModuleType("java_sdk_library_import", sdkLibraryImportFactory)
416}
417
Paul Duffin3375e352020-04-28 10:44:03 +0100418// Properties associated with each api scope.
419type ApiScopeProperties struct {
420 // Indicates whether the api surface is generated.
421 //
422 // If this is set for any scope then all scopes must explicitly specify if they
423 // are enabled. This is to prevent new usages from depending on legacy behavior.
424 //
425 // Otherwise, if this is not set for any scope then the default behavior is
426 // scope specific so please refer to the scope specific property documentation.
427 Enabled *bool
Paul Duffin87a05a32020-05-12 11:50:28 +0100428
429 // The sdk_version to use for building the stubs.
430 //
431 // If not specified then it will use an sdk_version determined as follows:
Trevor Radcliffedf8aa1f2021-11-04 14:25:39 +0000432 //
Paul Duffin87a05a32020-05-12 11:50:28 +0100433 // 1) If the sdk_version specified on the java_sdk_library is none then this
Trevor Radcliffedf8aa1f2021-11-04 14:25:39 +0000434 // will be none. This is used for java_sdk_library instances that are used
435 // to create stubs that contribute to the core_current sdk version.
436 // 2) Otherwise, it is assumed that this library extends but does not
437 // contribute directly to a specific sdk_version and so this uses the
438 // sdk_version appropriate for the api scope. e.g. public will use
439 // sdk_version: current, system will use sdk_version: system_current, etc.
Paul Duffin87a05a32020-05-12 11:50:28 +0100440 //
441 // This does not affect the sdk_version used for either generating the stubs source
442 // or the API file. They both have to use the same sdk_version as is used for
443 // compiling the implementation library.
444 Sdk_version *string
Mark Whitebeb6e9f2023-08-10 00:07:03 +0000445
446 // Extra libs used when compiling stubs for this scope.
447 Libs []string
Paul Duffin3375e352020-04-28 10:44:03 +0100448}
449
Jiyong Parkc678ad32018-04-10 13:07:10 +0900450type sdkLibraryProperties struct {
Anton Hanssonf8ea3722021-09-16 14:24:13 +0100451 // List of source files that are needed to compile the API, but are not part of runtime library.
452 Api_srcs []string `android:"arch_variant"`
453
Paul Duffin5df79302020-05-16 15:52:12 +0100454 // Visibility for impl library module. If not specified then defaults to the
455 // visibility property.
456 Impl_library_visibility []string
457
Paul Duffin4911a892020-04-29 23:35:13 +0100458 // Visibility for stubs library modules. If not specified then defaults to the
459 // visibility property.
460 Stubs_library_visibility []string
461
462 // Visibility for stubs source modules. If not specified then defaults to the
463 // visibility property.
464 Stubs_source_visibility []string
465
Anton Hansson7f66efa2020-10-08 14:47:23 +0100466 // List of Java libraries that will be in the classpath when building the implementation lib
467 Impl_only_libs []string `android:"arch_variant"`
468
Paul Duffin77590a82022-04-28 14:13:30 +0000469 // List of Java libraries that will included in the implementation lib.
470 Impl_only_static_libs []string `android:"arch_variant"`
471
Sundong Ahnf043cf62018-06-25 16:04:37 +0900472 // List of Java libraries that will be in the classpath when building stubs
473 Stub_only_libs []string `android:"arch_variant"`
474
Anton Hanssondae54cd2021-04-21 16:30:10 +0100475 // List of Java libraries that will included in stub libraries
476 Stub_only_static_libs []string `android:"arch_variant"`
477
Paul Duffin7a586d32019-12-30 17:09:34 +0000478 // list of package names that will be documented and publicized as API.
479 // This allows the API to be restricted to a subset of the source files provided.
480 // If this is unspecified then all the source files will be treated as being part
481 // of the API.
Jiyong Parkc678ad32018-04-10 13:07:10 +0900482 Api_packages []string
483
Jiyong Park5a2c9d72018-05-01 22:25:41 +0900484 // list of package names that must be hidden from the API
485 Hidden_api_packages []string
486
Paul Duffin749f98f2019-12-30 17:23:46 +0000487 // the relative path to the directory containing the api specification files.
488 // Defaults to "api".
489 Api_dir *string
490
Paul Duffindfa131e2020-05-15 20:37:11 +0100491 // Determines whether a runtime implementation library is built; defaults to false.
492 //
493 // 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 +0200494 // it is as if shared_library: false, was set.
Paul Duffin43db9be2019-12-30 17:35:49 +0000495 Api_only *bool
496
Paul Duffin11512472019-02-11 15:55:17 +0000497 // local files that are used within user customized droiddoc options.
498 Droiddoc_option_files []string
499
Spandan Das93e95992021-07-29 18:26:39 +0000500 // additional droiddoc options.
Paul Duffin11512472019-02-11 15:55:17 +0000501 // Available variables for substitution:
502 //
503 // $(location <label>): the path to the droiddoc_option_files with name <label>
Sundong Ahndd567f92018-07-31 17:19:11 +0900504 Droiddoc_options []string
505
Paul Duffine22c2ab2020-05-20 19:35:27 +0100506 // is set to true, Metalava will allow framework SDK to contain annotations.
507 Annotations_enabled *bool
508
Sundong Ahn054b19a2018-10-19 13:46:09 +0900509 // a list of top-level directories containing files to merge qualifier annotations
510 // (i.e. those intended to be included in the stubs written) from.
511 Merge_annotations_dirs []string
512
513 // a list of top-level directories containing Java stub files to merge show/hide annotations from.
514 Merge_inclusion_annotations_dirs []string
515
Paul Duffin4f5c1ef2020-11-19 14:53:43 +0000516 // If set to true then don't create dist rules.
517 No_dist *bool
Sundong Ahn80a87b32019-05-13 15:02:50 +0900518
Paul Duffin31310252020-11-20 21:26:20 +0000519 // The stem for the artifacts that are copied to the dist, if not specified
520 // then defaults to the base module name.
521 //
522 // For each scope the following artifacts are copied to the apistubs/<scope>
523 // directory in the dist.
524 // * stubs impl jar -> <dist-stem>.jar
525 // * API specification file -> api/<dist-stem>.txt
526 // * Removed API specification file -> api/<dist-stem>-removed.txt
527 //
528 // Also used to construct the name of the filegroup (created by prebuilt_apis)
529 // that references the latest released API and remove API specification files.
530 // * API specification filegroup -> <dist-stem>.api.<scope>.latest
531 // * Removed API specification filegroup -> <dist-stem>-removed.api.<scope>.latest
Jaewoong Jung1a97ee02021-03-09 13:25:02 -0800532 // * API incompatibilities baseline filegroup -> <dist-stem>-incompatibilities.api.<scope>.latest
Paul Duffin31310252020-11-20 21:26:20 +0000533 Dist_stem *string
534
Colin Cross986b69a2021-06-01 13:13:40 -0700535 // The subdirectory for the artifacts that are copied to the dist directory. If not specified
Colin Cross3dd66252021-06-01 14:05:09 -0700536 // then defaults to "unknown". Should be set to "android" for anything that should be published
Colin Cross986b69a2021-06-01 13:13:40 -0700537 // in the public Android SDK.
538 Dist_group *string
539
Anton Hanssondff2c782020-12-21 17:10:01 +0000540 // A compatibility mode that allows historical API-tracking files to not exist.
541 // Do not use.
542 Unsafe_ignore_missing_latest_api bool
543
Paul Duffin3375e352020-04-28 10:44:03 +0100544 // indicates whether system and test apis should be generated.
545 Generate_system_and_test_apis bool `blueprint:"mutated"`
546
547 // The properties specific to the public api scope
548 //
549 // Unless explicitly specified by using public.enabled the public api scope is
550 // enabled by default in both legacy and non-legacy mode.
551 Public ApiScopeProperties
552
553 // The properties specific to the system api scope
554 //
555 // In legacy mode the system api scope is enabled by default when sdk_version
556 // is set to something other than "none".
557 //
558 // In non-legacy mode the system api scope is disabled by default.
559 System ApiScopeProperties
560
561 // The properties specific to the test api scope
562 //
563 // In legacy mode the test api scope is enabled by default when sdk_version
564 // is set to something other than "none".
565 //
566 // In non-legacy mode the test api scope is disabled by default.
567 Test ApiScopeProperties
Paul Duffin37e0b772019-12-30 17:20:10 +0000568
Paul Duffin0c5bae52020-06-02 13:00:08 +0100569 // The properties specific to the module-lib api scope
Paul Duffin8f265b92020-04-28 14:13:56 +0100570 //
Zi Wangb2179e32023-01-31 15:53:30 -0800571 // Unless explicitly specified by using module_lib.enabled the module_lib api
572 // scope is disabled by default.
Paul Duffin8f265b92020-04-28 14:13:56 +0100573 Module_lib ApiScopeProperties
574
Paul Duffin0c5bae52020-06-02 13:00:08 +0100575 // The properties specific to the system-server api scope
576 //
Zi Wangb2179e32023-01-31 15:53:30 -0800577 // Unless explicitly specified by using system_server.enabled the
578 // system_server api scope is disabled by default.
Paul Duffin0c5bae52020-06-02 13:00:08 +0100579 System_server ApiScopeProperties
580
Jiyong Park932cdfe2020-05-28 00:19:53 +0900581 // Determines if the stubs are preferred over the implementation library
582 // for linking, even when the client doesn't specify sdk_version. When this
583 // is set to true, such clients are provided with the widest API surface that
584 // this lib provides. Note however that this option doesn't affect the clients
585 // that are in the same APEX as this library. In that case, the clients are
586 // always linked with the implementation library. Default is false.
587 Default_to_stubs *bool
588
Paul Duffin160fe412020-05-10 19:32:20 +0100589 // Properties related to api linting.
590 Api_lint struct {
591 // Enable api linting.
592 Enabled *bool
593 }
594
Jiyong Parkc678ad32018-04-10 13:07:10 +0900595 // TODO: determines whether to create HTML doc or not
Paul Duffine8409952022-09-22 16:24:46 +0100596 // Html_doc *bool
Jiyong Parkc678ad32018-04-10 13:07:10 +0900597}
598
Paul Duffin0f8faff2020-05-20 16:18:00 +0100599// Paths to outputs from java_sdk_library and java_sdk_library_import.
600//
601// Fields that are android.Paths are always set (during GenerateAndroidBuildActions).
602// OptionalPaths are always set by java_sdk_library but may not be set by
603// java_sdk_library_import as not all instances provide that information.
Paul Duffind1b3a922020-01-22 11:57:20 +0000604type scopePaths struct {
Paul Duffin0f8faff2020-05-20 16:18:00 +0100605 // The path (represented as Paths for convenience when returning) to the stubs header jar.
606 //
607 // That is the jar that is created by turbine.
608 stubsHeaderPath android.Paths
609
610 // The path (represented as Paths for convenience when returning) to the stubs implementation jar.
611 //
612 // This is not the implementation jar, it still only contains stubs.
613 stubsImplPath android.Paths
614
Paul Duffin1267d872021-04-16 17:21:36 +0100615 // The dex jar for the stubs.
616 //
617 // This is not the implementation jar, it still only contains stubs.
Martin Stjernholm8be1e6d2021-09-15 03:34:04 +0100618 stubsDexJarPath OptionalDexJarPath
Paul Duffin1267d872021-04-16 17:21:36 +0100619
Paul Duffin0f8faff2020-05-20 16:18:00 +0100620 // The API specification file, e.g. system_current.txt.
621 currentApiFilePath android.OptionalPath
622
623 // The specification of API elements removed since the last release.
624 removedApiFilePath android.OptionalPath
625
626 // The stubs source jar.
627 stubsSrcJar android.OptionalPath
Anton Hanssond78eb762021-09-21 15:25:12 +0100628
629 // Extracted annotations.
630 annotationsZip android.OptionalPath
Paul Duffin958806b2022-05-16 13:10:47 +0000631
632 // The path to the latest API file.
633 latestApiPath android.OptionalPath
634
635 // The path to the latest removed API file.
636 latestRemovedApiPath android.OptionalPath
Paul Duffind1b3a922020-01-22 11:57:20 +0000637}
638
Colin Crossdcf71b22021-02-01 13:59:03 -0800639func (paths *scopePaths) extractStubsLibraryInfoFromDependency(ctx android.ModuleContext, dep android.Module) error {
640 if ctx.OtherModuleHasProvider(dep, JavaInfoProvider) {
641 lib := ctx.OtherModuleProvider(dep, JavaInfoProvider).(JavaInfo)
642 paths.stubsHeaderPath = lib.HeaderJars
643 paths.stubsImplPath = lib.ImplementationJars
Paul Duffin1267d872021-04-16 17:21:36 +0100644
645 libDep := dep.(UsesLibraryDependency)
646 paths.stubsDexJarPath = libDep.DexJarBuildPath()
Paul Duffinc8782502020-04-29 20:45:27 +0100647 return nil
648 } else {
Colin Crossdcf71b22021-02-01 13:59:03 -0800649 return fmt.Errorf("expected module that has JavaInfoProvider, e.g. java_library")
Paul Duffinc8782502020-04-29 20:45:27 +0100650 }
651}
652
Paul Duffin0ff08bd2020-04-29 13:30:54 +0100653func (paths *scopePaths) treatDepAsApiStubsProvider(dep android.Module, action func(provider ApiStubsProvider)) error {
654 if apiStubsProvider, ok := dep.(ApiStubsProvider); ok {
655 action(apiStubsProvider)
Paul Duffinc8782502020-04-29 20:45:27 +0100656 return nil
657 } else {
658 return fmt.Errorf("expected module that implements ApiStubsProvider, e.g. droidstubs")
659 }
660}
661
Paul Duffin0f8faff2020-05-20 16:18:00 +0100662func (paths *scopePaths) treatDepAsApiStubsSrcProvider(dep android.Module, action func(provider ApiStubsSrcProvider)) error {
663 if apiStubsProvider, ok := dep.(ApiStubsSrcProvider); ok {
664 action(apiStubsProvider)
665 return nil
666 } else {
667 return fmt.Errorf("expected module that implements ApiStubsSrcProvider, e.g. droidstubs")
668 }
669}
670
Paul Duffin0ff08bd2020-04-29 13:30:54 +0100671func (paths *scopePaths) extractApiInfoFromApiStubsProvider(provider ApiStubsProvider) {
Anton Hanssond78eb762021-09-21 15:25:12 +0100672 paths.annotationsZip = android.OptionalPathForPath(provider.AnnotationsZip())
Paul Duffin0f8faff2020-05-20 16:18:00 +0100673 paths.currentApiFilePath = android.OptionalPathForPath(provider.ApiFilePath())
674 paths.removedApiFilePath = android.OptionalPathForPath(provider.RemovedApiFilePath())
Paul Duffin0ff08bd2020-04-29 13:30:54 +0100675}
676
Colin Crossdcf71b22021-02-01 13:59:03 -0800677func (paths *scopePaths) extractApiInfoFromDep(ctx android.ModuleContext, dep android.Module) error {
Paul Duffin0ff08bd2020-04-29 13:30:54 +0100678 return paths.treatDepAsApiStubsProvider(dep, func(provider ApiStubsProvider) {
679 paths.extractApiInfoFromApiStubsProvider(provider)
680 })
681}
682
Paul Duffin0f8faff2020-05-20 16:18:00 +0100683func (paths *scopePaths) extractStubsSourceInfoFromApiStubsProviders(provider ApiStubsSrcProvider) {
684 paths.stubsSrcJar = android.OptionalPathForPath(provider.StubsSrcJar())
Paul Duffin0ff08bd2020-04-29 13:30:54 +0100685}
686
Colin Crossdcf71b22021-02-01 13:59:03 -0800687func (paths *scopePaths) extractStubsSourceInfoFromDep(ctx android.ModuleContext, dep android.Module) error {
Paul Duffin0f8faff2020-05-20 16:18:00 +0100688 return paths.treatDepAsApiStubsSrcProvider(dep, func(provider ApiStubsSrcProvider) {
Paul Duffin0ff08bd2020-04-29 13:30:54 +0100689 paths.extractStubsSourceInfoFromApiStubsProviders(provider)
690 })
691}
692
Colin Crossdcf71b22021-02-01 13:59:03 -0800693func (paths *scopePaths) extractStubsSourceAndApiInfoFromApiStubsProvider(ctx android.ModuleContext, dep android.Module) error {
Paul Duffin0ff08bd2020-04-29 13:30:54 +0100694 return paths.treatDepAsApiStubsProvider(dep, func(provider ApiStubsProvider) {
695 paths.extractApiInfoFromApiStubsProvider(provider)
696 paths.extractStubsSourceInfoFromApiStubsProviders(provider)
697 })
698}
699
Paul Duffin958806b2022-05-16 13:10:47 +0000700func extractSingleOptionalOutputPath(dep android.Module) (android.OptionalPath, error) {
701 var paths android.Paths
702 if sourceFileProducer, ok := dep.(android.SourceFileProducer); ok {
703 paths = sourceFileProducer.Srcs()
704 } else {
705 return android.OptionalPath{}, fmt.Errorf("module %q does not produce source files", dep)
706 }
707 if len(paths) != 1 {
708 return android.OptionalPath{}, fmt.Errorf("expected one path from %q, got %q", dep, paths)
709 }
710 return android.OptionalPathForPath(paths[0]), nil
711}
712
713func (paths *scopePaths) extractLatestApiPath(ctx android.ModuleContext, dep android.Module) error {
714 outputPath, err := extractSingleOptionalOutputPath(dep)
715 paths.latestApiPath = outputPath
716 return err
717}
718
719func (paths *scopePaths) extractLatestRemovedApiPath(ctx android.ModuleContext, dep android.Module) error {
720 outputPath, err := extractSingleOptionalOutputPath(dep)
721 paths.latestRemovedApiPath = outputPath
722 return err
723}
724
Paul Duffin0ff08bd2020-04-29 13:30:54 +0100725type commonToSdkLibraryAndImportProperties struct {
Paul Duffin1b1e8062020-05-08 13:44:43 +0100726 // The naming scheme to use for the components that this module creates.
727 //
Paul Duffinee9ad5d2020-09-11 13:04:05 +0100728 // If not specified then it defaults to "default".
Paul Duffin1b1e8062020-05-08 13:44:43 +0100729 //
730 // This is a temporary mechanism to simplify conversion from separate modules for each
731 // component that follow a different naming pattern to the default one.
732 //
733 // TODO(b/155480189) - Remove once naming inconsistencies have been resolved.
Paul Duffin0ff08bd2020-04-29 13:30:54 +0100734 Naming_scheme *string
Paul Duffindfa131e2020-05-15 20:37:11 +0100735
736 // Specifies whether this module can be used as an Android shared library; defaults
737 // to true.
738 //
739 // An Android shared library is one that can be referenced in a <uses-library> element
740 // in an AndroidManifest.xml.
741 Shared_library *bool
Paul Duffina2ae7e02020-09-11 11:55:00 +0100742
743 // Files containing information about supported java doc tags.
744 Doctag_files []string `android:"path"`
Pedro Loureiro9956e5e2021-09-07 17:21:59 +0000745
746 // Signals that this shared library is part of the bootclasspath starting
747 // on the version indicated in this attribute.
748 //
749 // This will make platforms at this level and above to ignore
750 // <uses-library> tags with this library name because the library is already
751 // available
752 On_bootclasspath_since *string
753
754 // Signals that this shared library was part of the bootclasspath before
755 // (but not including) the version indicated in this attribute.
756 //
757 // The system will automatically add a <uses-library> tag with this library to
758 // apps that target any SDK less than the version indicated in this attribute.
759 On_bootclasspath_before *string
760
761 // Indicates that PackageManager should ignore this shared library if the
762 // platform is below the version indicated in this attribute.
763 //
764 // This means that the device won't recognise this library as installed.
765 Min_device_sdk *string
766
767 // Indicates that PackageManager should ignore this shared library if the
768 // platform is above the version indicated in this attribute.
769 //
770 // This means that the device won't recognise this library as installed.
771 Max_device_sdk *string
Paul Duffin0ff08bd2020-04-29 13:30:54 +0100772}
773
Paul Duffin71b33cc2021-06-23 11:39:47 +0100774// commonSdkLibraryAndImportModule defines the interface that must be provided by a module that
775// embeds the commonToSdkLibraryAndImport struct.
776type commonSdkLibraryAndImportModule interface {
Paul Duffind796f6f2022-11-23 23:06:05 +0000777 android.Module
Paul Duffin71b33cc2021-06-23 11:39:47 +0100778
779 BaseModuleName() string
780}
781
Paul Duffin56d44902020-01-31 13:36:25 +0000782// Common code between sdk library and sdk library import
783type commonToSdkLibraryAndImport struct {
Paul Duffin71b33cc2021-06-23 11:39:47 +0100784 module commonSdkLibraryAndImportModule
Paul Duffinc3091c82020-05-08 14:16:20 +0100785
Paul Duffin56d44902020-01-31 13:36:25 +0000786 scopePaths map[*apiScope]*scopePaths
Paul Duffin1b1e8062020-05-08 13:44:43 +0100787
788 namingScheme sdkLibraryComponentNamingScheme
789
Paul Duffindfa131e2020-05-15 20:37:11 +0100790 commonSdkLibraryProperties commonToSdkLibraryAndImportProperties
Paul Duffin859fe962020-05-15 10:20:31 +0100791
Paul Duffina2ae7e02020-09-11 11:55:00 +0100792 // Paths to commonSdkLibraryProperties.Doctag_files
793 doctagPaths android.Paths
794
Paul Duffin859fe962020-05-15 10:20:31 +0100795 // Functionality related to this being used as a component of a java_sdk_library.
796 EmbeddableSdkLibraryComponent
Paul Duffin56d44902020-01-31 13:36:25 +0000797}
798
Paul Duffin71b33cc2021-06-23 11:39:47 +0100799func (c *commonToSdkLibraryAndImport) initCommon(module commonSdkLibraryAndImportModule) {
800 c.module = module
Paul Duffin1b1e8062020-05-08 13:44:43 +0100801
Paul Duffin71b33cc2021-06-23 11:39:47 +0100802 module.AddProperties(&c.commonSdkLibraryProperties)
Paul Duffin859fe962020-05-15 10:20:31 +0100803
804 // Initialize this as an sdk library component.
Paul Duffin71b33cc2021-06-23 11:39:47 +0100805 c.initSdkLibraryComponent(module)
Paul Duffin1b1e8062020-05-08 13:44:43 +0100806}
807
808func (c *commonToSdkLibraryAndImport) initCommonAfterDefaultsApplied(ctx android.DefaultableHookContext) bool {
Paul Duffindfa131e2020-05-15 20:37:11 +0100809 schemeProperty := proptools.StringDefault(c.commonSdkLibraryProperties.Naming_scheme, "default")
Paul Duffin1b1e8062020-05-08 13:44:43 +0100810 switch schemeProperty {
811 case "default":
812 c.namingScheme = &defaultNamingScheme{}
813 default:
814 ctx.PropertyErrorf("naming_scheme", "expected 'default' but was %q", schemeProperty)
815 return false
816 }
817
Paul Duffin3f0290e2021-06-30 18:25:36 +0100818 namePtr := proptools.StringPtr(c.module.BaseModuleName())
819 c.sdkLibraryComponentProperties.SdkLibraryName = namePtr
820
Paul Duffindfa131e2020-05-15 20:37:11 +0100821 // Only track this sdk library if this can be used as a shared library.
822 if c.sharedLibrary() {
823 // Use the name specified in the module definition as the owner.
Paul Duffin3f0290e2021-06-30 18:25:36 +0100824 c.sdkLibraryComponentProperties.SdkLibraryToImplicitlyTrack = namePtr
Paul Duffindfa131e2020-05-15 20:37:11 +0100825 }
Paul Duffin859fe962020-05-15 10:20:31 +0100826
Paul Duffin1b1e8062020-05-08 13:44:43 +0100827 return true
Paul Duffinc3091c82020-05-08 14:16:20 +0100828}
829
Paul Duffinea8f8082021-06-24 13:25:57 +0100830// uniqueApexVariations provides common implementation of the ApexModule.UniqueApexVariations
831// method.
832func (c *commonToSdkLibraryAndImport) uniqueApexVariations() bool {
833 // A java_sdk_library that is a shared library produces an XML file that makes the shared library
834 // usable from an AndroidManifest.xml's <uses-library> entry. That XML file contains the name of
835 // the APEX and so it needs a unique variation per APEX.
836 return c.sharedLibrary()
837}
838
Paul Duffina2ae7e02020-09-11 11:55:00 +0100839func (c *commonToSdkLibraryAndImport) generateCommonBuildActions(ctx android.ModuleContext) {
840 c.doctagPaths = android.PathsForModuleSrc(ctx, c.commonSdkLibraryProperties.Doctag_files)
841}
842
Paul Duffineedc5d52020-06-12 17:46:39 +0100843// Module name of the runtime implementation library
844func (c *commonToSdkLibraryAndImport) implLibraryModuleName() string {
Paul Duffin71b33cc2021-06-23 11:39:47 +0100845 return c.module.BaseModuleName() + ".impl"
Paul Duffineedc5d52020-06-12 17:46:39 +0100846}
847
848// Module name of the XML file for the lib
849func (c *commonToSdkLibraryAndImport) xmlPermissionsModuleName() string {
Paul Duffin71b33cc2021-06-23 11:39:47 +0100850 return c.module.BaseModuleName() + sdkXmlFileSuffix
Paul Duffineedc5d52020-06-12 17:46:39 +0100851}
852
Paul Duffinc3091c82020-05-08 14:16:20 +0100853// Name of the java_library module that compiles the stubs source.
854func (c *commonToSdkLibraryAndImport) stubsLibraryModuleName(apiScope *apiScope) string {
Paul Duffinb97b1572021-04-29 21:50:40 +0100855 baseName := c.module.BaseModuleName()
Paul Duffin21787622022-11-25 12:48:20 +0000856 return c.namingScheme.stubsLibraryModuleName(apiScope, baseName)
Paul Duffinc3091c82020-05-08 14:16:20 +0100857}
858
859// Name of the droidstubs module that generates the stubs source and may also
860// generate/check the API.
861func (c *commonToSdkLibraryAndImport) stubsSourceModuleName(apiScope *apiScope) string {
Paul Duffinb97b1572021-04-29 21:50:40 +0100862 baseName := c.module.BaseModuleName()
Paul Duffin21787622022-11-25 12:48:20 +0000863 return c.namingScheme.stubsSourceModuleName(apiScope, baseName)
Paul Duffinc3091c82020-05-08 14:16:20 +0100864}
865
Jihoon Kang1c92c3e2023-03-23 17:44:51 +0000866// Name of the java_api_library module that generates the from-text stubs source
867// and compiles to a jar file.
868func (c *commonToSdkLibraryAndImport) apiLibraryModuleName(apiScope *apiScope) string {
869 baseName := c.module.BaseModuleName()
870 return c.namingScheme.apiLibraryModuleName(apiScope, baseName)
871}
872
Jihoon Kang1147b312023-06-08 23:25:57 +0000873// Name of the java_library module that compiles the stubs
874// generated from source Java files.
875func (c *commonToSdkLibraryAndImport) sourceStubLibraryModuleName(apiScope *apiScope) string {
876 baseName := c.module.BaseModuleName()
877 return c.namingScheme.sourceStubLibraryModuleName(apiScope, baseName)
878}
879
Paul Duffin46dc45a2020-05-14 15:39:10 +0100880// The component names for different outputs of the java_sdk_library.
881//
882// They are similar to the names used for the child modules it creates
883const (
884 stubsSourceComponentName = "stubs.source"
885
886 apiTxtComponentName = "api.txt"
887
888 removedApiTxtComponentName = "removed-api.txt"
Anton Hanssond78eb762021-09-21 15:25:12 +0100889
890 annotationsComponentName = "annotations.zip"
Paul Duffin46dc45a2020-05-14 15:39:10 +0100891)
892
893// A regular expression to match tags that reference a specific stubs component.
894//
895// It will only match if given a valid scope and a valid component. It is verfy strict
896// to ensure it does not accidentally match a similar looking tag that should be processed
897// by the embedded Library.
898var tagSplitter = func() *regexp.Regexp {
899 // Given a list of literal string items returns a regular expression that will
900 // match any one of the items.
901 choice := func(items ...string) string {
902 return `\Q` + strings.Join(items, `\E|\Q`) + `\E`
903 }
904
905 // Regular expression to match one of the scopes.
906 scopesRegexp := choice(allScopeNames...)
907
908 // Regular expression to match one of the components.
Anton Hanssond78eb762021-09-21 15:25:12 +0100909 componentsRegexp := choice(stubsSourceComponentName, apiTxtComponentName, removedApiTxtComponentName, annotationsComponentName)
Paul Duffin46dc45a2020-05-14 15:39:10 +0100910
911 // Regular expression to match any combination of one scope and one component.
912 return regexp.MustCompile(fmt.Sprintf(`^\.(%s)\.(%s)$`, scopesRegexp, componentsRegexp))
913}()
914
915// For OutputFileProducer interface
916//
Anton Hanssond78eb762021-09-21 15:25:12 +0100917// .<scope>.<component name>, for all ComponentNames (for example: .public.removed-api.txt)
Paul Duffin46dc45a2020-05-14 15:39:10 +0100918func (c *commonToSdkLibraryAndImport) commonOutputFiles(tag string) (android.Paths, error) {
919 if groups := tagSplitter.FindStringSubmatch(tag); groups != nil {
920 scopeName := groups[1]
921 component := groups[2]
922
923 if scope, ok := scopeByName[scopeName]; ok {
924 paths := c.findScopePaths(scope)
925 if paths == nil {
Paul Duffin71b33cc2021-06-23 11:39:47 +0100926 return nil, fmt.Errorf("%q does not provide api scope %s", c.module.BaseModuleName(), scopeName)
Paul Duffin46dc45a2020-05-14 15:39:10 +0100927 }
928
929 switch component {
930 case stubsSourceComponentName:
931 if paths.stubsSrcJar.Valid() {
932 return android.Paths{paths.stubsSrcJar.Path()}, nil
933 }
934
935 case apiTxtComponentName:
936 if paths.currentApiFilePath.Valid() {
937 return android.Paths{paths.currentApiFilePath.Path()}, nil
938 }
939
940 case removedApiTxtComponentName:
941 if paths.removedApiFilePath.Valid() {
942 return android.Paths{paths.removedApiFilePath.Path()}, nil
943 }
Anton Hanssond78eb762021-09-21 15:25:12 +0100944
945 case annotationsComponentName:
946 if paths.annotationsZip.Valid() {
947 return android.Paths{paths.annotationsZip.Path()}, nil
948 }
Paul Duffin46dc45a2020-05-14 15:39:10 +0100949 }
950
951 return nil, fmt.Errorf("%s not available for api scope %s", component, scopeName)
952 } else {
953 return nil, fmt.Errorf("unknown scope %s in %s", scope, tag)
954 }
955
956 } else {
Paul Duffina2ae7e02020-09-11 11:55:00 +0100957 switch tag {
958 case ".doctags":
959 if c.doctagPaths != nil {
960 return c.doctagPaths, nil
961 } else {
Paul Duffin71b33cc2021-06-23 11:39:47 +0100962 return nil, fmt.Errorf("no doctag_files specified on %s", c.module.BaseModuleName())
Paul Duffina2ae7e02020-09-11 11:55:00 +0100963 }
964 }
Paul Duffin46dc45a2020-05-14 15:39:10 +0100965 return nil, nil
966 }
967}
968
Paul Duffin803a9562020-05-20 11:52:25 +0100969func (c *commonToSdkLibraryAndImport) getScopePathsCreateIfNeeded(scope *apiScope) *scopePaths {
Paul Duffin56d44902020-01-31 13:36:25 +0000970 if c.scopePaths == nil {
971 c.scopePaths = make(map[*apiScope]*scopePaths)
972 }
973 paths := c.scopePaths[scope]
974 if paths == nil {
975 paths = &scopePaths{}
976 c.scopePaths[scope] = paths
977 }
978
979 return paths
980}
981
Paul Duffin803a9562020-05-20 11:52:25 +0100982func (c *commonToSdkLibraryAndImport) findScopePaths(scope *apiScope) *scopePaths {
983 if c.scopePaths == nil {
984 return nil
985 }
986
987 return c.scopePaths[scope]
988}
989
990// If this does not support the requested api scope then find the closest available
991// scope it does support. Returns nil if no such scope is available.
992func (c *commonToSdkLibraryAndImport) findClosestScopePath(scope *apiScope) *scopePaths {
Paul Duffind0b9fca2022-09-30 18:11:41 +0100993 for s := scope; s != nil; s = s.canAccess {
Paul Duffin803a9562020-05-20 11:52:25 +0100994 if paths := c.findScopePaths(s); paths != nil {
995 return paths
996 }
997 }
998
999 // This should never happen outside tests as public should be the base scope for every
1000 // scope and is enabled by default.
1001 return nil
1002}
1003
Jiyong Parkf1691d22021-03-29 20:11:58 +09001004func (c *commonToSdkLibraryAndImport) selectHeaderJarsForSdkVersion(ctx android.BaseModuleContext, sdkVersion android.SdkSpec) android.Paths {
Paul Duffinb05d4292020-05-20 12:19:10 +01001005
1006 // If a specific numeric version has been requested then use prebuilt versions of the sdk.
Jiyong Park54105c42021-03-31 18:17:53 +09001007 if !sdkVersion.ApiLevel.IsPreview() {
Paul Duffin71b33cc2021-06-23 11:39:47 +01001008 return PrebuiltJars(ctx, c.module.BaseModuleName(), sdkVersion)
Paul Duffinb05d4292020-05-20 12:19:10 +01001009 }
1010
Paul Duffin1267d872021-04-16 17:21:36 +01001011 paths := c.selectScopePaths(ctx, sdkVersion.Kind)
1012 if paths == nil {
1013 return nil
1014 }
1015
1016 return paths.stubsHeaderPath
1017}
1018
1019// selectScopePaths returns the *scopePaths appropriate for the specific kind.
1020//
1021// If the module does not support the specific kind then it will return the *scopePaths for the
1022// closest kind which is a subset of the requested kind. e.g. if requesting android.SdkModule then
1023// it will return *scopePaths for android.SdkSystem if available or android.SdkPublic of not.
1024func (c *commonToSdkLibraryAndImport) selectScopePaths(ctx android.BaseModuleContext, kind android.SdkKind) *scopePaths {
Paul Duffin32cf58a2021-05-18 16:32:50 +01001025 apiScope := sdkKindToApiScope(kind)
Paul Duffinb05d4292020-05-20 12:19:10 +01001026
Paul Duffin803a9562020-05-20 11:52:25 +01001027 paths := c.findClosestScopePath(apiScope)
1028 if paths == nil {
1029 var scopes []string
1030 for _, s := range allApiScopes {
1031 if c.findScopePaths(s) != nil {
1032 scopes = append(scopes, s.name)
1033 }
1034 }
Paul Duffin71b33cc2021-06-23 11:39:47 +01001035 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 +01001036 return nil
1037 }
1038
Paul Duffin1267d872021-04-16 17:21:36 +01001039 return paths
1040}
1041
Paul Duffin32cf58a2021-05-18 16:32:50 +01001042// sdkKindToApiScope maps from android.SdkKind to apiScope.
1043func sdkKindToApiScope(kind android.SdkKind) *apiScope {
1044 var apiScope *apiScope
1045 switch kind {
1046 case android.SdkSystem:
1047 apiScope = apiScopeSystem
1048 case android.SdkModule:
1049 apiScope = apiScopeModuleLib
1050 case android.SdkTest:
1051 apiScope = apiScopeTest
1052 case android.SdkSystemServer:
1053 apiScope = apiScopeSystemServer
1054 default:
1055 apiScope = apiScopePublic
1056 }
1057 return apiScope
1058}
1059
Paul Duffin1267d872021-04-16 17:21:36 +01001060// to satisfy SdkLibraryDependency interface
Martin Stjernholm8be1e6d2021-09-15 03:34:04 +01001061func (c *commonToSdkLibraryAndImport) SdkApiStubDexJar(ctx android.BaseModuleContext, kind android.SdkKind) OptionalDexJarPath {
Paul Duffin1267d872021-04-16 17:21:36 +01001062 paths := c.selectScopePaths(ctx, kind)
1063 if paths == nil {
Martin Stjernholm8be1e6d2021-09-15 03:34:04 +01001064 return makeUnsetDexJarPath()
Paul Duffin1267d872021-04-16 17:21:36 +01001065 }
1066
1067 return paths.stubsDexJarPath
Paul Duffinb05d4292020-05-20 12:19:10 +01001068}
1069
Paul Duffin32cf58a2021-05-18 16:32:50 +01001070// to satisfy SdkLibraryDependency interface
1071func (c *commonToSdkLibraryAndImport) SdkRemovedTxtFile(ctx android.BaseModuleContext, kind android.SdkKind) android.OptionalPath {
1072 apiScope := sdkKindToApiScope(kind)
1073 paths := c.findScopePaths(apiScope)
1074 if paths == nil {
1075 return android.OptionalPath{}
1076 }
1077
1078 return paths.removedApiFilePath
1079}
1080
Paul Duffin859fe962020-05-15 10:20:31 +01001081func (c *commonToSdkLibraryAndImport) sdkComponentPropertiesForChildLibrary() interface{} {
1082 componentProps := &struct {
Paul Duffin3f0290e2021-06-30 18:25:36 +01001083 SdkLibraryName *string
Paul Duffin859fe962020-05-15 10:20:31 +01001084 SdkLibraryToImplicitlyTrack *string
Paul Duffindfa131e2020-05-15 20:37:11 +01001085 }{}
1086
Paul Duffin3f0290e2021-06-30 18:25:36 +01001087 namePtr := proptools.StringPtr(c.module.BaseModuleName())
1088 componentProps.SdkLibraryName = namePtr
1089
Paul Duffindfa131e2020-05-15 20:37:11 +01001090 if c.sharedLibrary() {
Paul Duffin859fe962020-05-15 10:20:31 +01001091 // Mark the stubs library as being components of this java_sdk_library so that
1092 // any app that includes code which depends (directly or indirectly) on the stubs
1093 // library will have the appropriate <uses-library> invocation inserted into its
1094 // manifest if necessary.
Paul Duffin3f0290e2021-06-30 18:25:36 +01001095 componentProps.SdkLibraryToImplicitlyTrack = namePtr
Paul Duffin859fe962020-05-15 10:20:31 +01001096 }
1097
1098 return componentProps
1099}
1100
Paul Duffindfa131e2020-05-15 20:37:11 +01001101func (c *commonToSdkLibraryAndImport) sharedLibrary() bool {
1102 return proptools.BoolDefault(c.commonSdkLibraryProperties.Shared_library, true)
1103}
1104
Paul Duffinf4600f62021-05-13 22:34:45 +01001105// Check if the stub libraries should be compiled for dex
1106func (c *commonToSdkLibraryAndImport) stubLibrariesCompiledForDex() bool {
1107 // Always compile the dex file files for the stub libraries if they will be used on the
1108 // bootclasspath.
1109 return !c.sharedLibrary()
1110}
1111
Paul Duffin859fe962020-05-15 10:20:31 +01001112// Properties related to the use of a module as an component of a java_sdk_library.
1113type SdkLibraryComponentProperties struct {
Paul Duffin3f0290e2021-06-30 18:25:36 +01001114 // The name of the java_sdk_library/_import module.
1115 SdkLibraryName *string `blueprint:"mutated"`
Paul Duffin859fe962020-05-15 10:20:31 +01001116
1117 // The name of the java_sdk_library/_import to add to a <uses-library> entry
1118 // in the AndroidManifest.xml of any Android app that includes code that references
1119 // this module. If not set then no java_sdk_library/_import is tracked.
1120 SdkLibraryToImplicitlyTrack *string `blueprint:"mutated"`
1121}
1122
1123// Structure to be embedded in a module struct that needs to support the
1124// SdkLibraryComponentDependency interface.
1125type EmbeddableSdkLibraryComponent struct {
1126 sdkLibraryComponentProperties SdkLibraryComponentProperties
1127}
1128
Paul Duffin71b33cc2021-06-23 11:39:47 +01001129func (e *EmbeddableSdkLibraryComponent) initSdkLibraryComponent(module android.Module) {
1130 module.AddProperties(&e.sdkLibraryComponentProperties)
Paul Duffin859fe962020-05-15 10:20:31 +01001131}
1132
1133// to satisfy SdkLibraryComponentDependency
Paul Duffin3f0290e2021-06-30 18:25:36 +01001134func (e *EmbeddableSdkLibraryComponent) SdkLibraryName() *string {
1135 return e.sdkLibraryComponentProperties.SdkLibraryName
1136}
1137
1138// to satisfy SdkLibraryComponentDependency
Ulya Trafimovich39b437b2020-09-23 16:42:35 +01001139func (e *EmbeddableSdkLibraryComponent) OptionalSdkLibraryImplementation() *string {
Ulya Trafimovich78645fb2021-07-16 15:29:25 +01001140 // For shared libraries, this is the same as the SDK library name. If a Java library or app
1141 // depends on a component library (e.g. a stub library) it still needs to know the name of the
1142 // run-time library and the corresponding module that provides the implementation. This name is
1143 // passed to manifest_fixer (to be added to AndroidManifest.xml) and added to CLC (to be used
1144 // in dexpreopt).
1145 //
1146 // For non-shared SDK (component or not) libraries this returns `nil`, as they are not
1147 // <uses-library> and should not be added to the manifest or to CLC.
Ulya Trafimovich39b437b2020-09-23 16:42:35 +01001148 return e.sdkLibraryComponentProperties.SdkLibraryToImplicitlyTrack
1149}
1150
Paul Duffin859fe962020-05-15 10:20:31 +01001151// Implemented by modules that are (or possibly could be) a component of a java_sdk_library
1152// (including the java_sdk_library) itself.
1153type SdkLibraryComponentDependency interface {
Ulya Trafimovich31e444e2020-08-14 17:32:16 +01001154 UsesLibraryDependency
1155
Paul Duffin3f0290e2021-06-30 18:25:36 +01001156 // SdkLibraryName returns the name of the java_sdk_library/_import module.
1157 SdkLibraryName() *string
1158
Ulya Trafimovich39b437b2020-09-23 16:42:35 +01001159 // The name of the implementation library for the optional SDK library or nil, if there isn't one.
1160 OptionalSdkLibraryImplementation() *string
Paul Duffin859fe962020-05-15 10:20:31 +01001161}
1162
1163// Make sure that all the module types that are components of java_sdk_library/_import
1164// and which can be referenced (directly or indirectly) from an android app implement
1165// the SdkLibraryComponentDependency interface.
1166var _ SdkLibraryComponentDependency = (*Library)(nil)
1167var _ SdkLibraryComponentDependency = (*Import)(nil)
1168var _ SdkLibraryComponentDependency = (*SdkLibrary)(nil)
Paul Duffineedc5d52020-06-12 17:46:39 +01001169var _ SdkLibraryComponentDependency = (*SdkLibraryImport)(nil)
Paul Duffin859fe962020-05-15 10:20:31 +01001170
Paul Duffin32cf58a2021-05-18 16:32:50 +01001171// Provides access to sdk_version related files, e.g. header and implementation jars.
Paul Duffin859fe962020-05-15 10:20:31 +01001172type SdkLibraryDependency interface {
1173 SdkLibraryComponentDependency
1174
1175 // Get the header jars appropriate for the supplied sdk_version.
1176 //
1177 // These are turbine generated jars so they only change if the externals of the
1178 // class changes but it does not contain and implementation or JavaDoc.
Jiyong Parkf1691d22021-03-29 20:11:58 +09001179 SdkHeaderJars(ctx android.BaseModuleContext, sdkVersion android.SdkSpec) android.Paths
Paul Duffin859fe962020-05-15 10:20:31 +01001180
1181 // Get the implementation jars appropriate for the supplied sdk version.
1182 //
1183 // These are either the implementation jar for the whole sdk library or the implementation
1184 // jars for the stubs. The latter should only be needed when generating JavaDoc as otherwise
1185 // they are identical to the corresponding header jars.
Jiyong Parkf1691d22021-03-29 20:11:58 +09001186 SdkImplementationJars(ctx android.BaseModuleContext, sdkVersion android.SdkSpec) android.Paths
Paul Duffin1267d872021-04-16 17:21:36 +01001187
1188 // SdkApiStubDexJar returns the dex jar for the stubs. It is needed by the hiddenapi processing
1189 // tool which processes dex files.
Martin Stjernholm8be1e6d2021-09-15 03:34:04 +01001190 SdkApiStubDexJar(ctx android.BaseModuleContext, kind android.SdkKind) OptionalDexJarPath
Paul Duffinf4600f62021-05-13 22:34:45 +01001191
Paul Duffin32cf58a2021-05-18 16:32:50 +01001192 // SdkRemovedTxtFile returns the optional path to the removed.txt file for the specified sdk kind.
1193 SdkRemovedTxtFile(ctx android.BaseModuleContext, kind android.SdkKind) android.OptionalPath
1194
Paul Duffinf4600f62021-05-13 22:34:45 +01001195 // sharedLibrary returns true if this can be used as a shared library.
1196 sharedLibrary() bool
Paul Duffin859fe962020-05-15 10:20:31 +01001197}
1198
Inseob Kimc0907f12019-02-08 21:00:45 +09001199type SdkLibrary struct {
Sundong Ahn054b19a2018-10-19 13:46:09 +09001200 Library
Jiyong Parkc678ad32018-04-10 13:07:10 +09001201
Zi Wangb2179e32023-01-31 15:53:30 -08001202 android.BazelModuleBase
1203
Sundong Ahn054b19a2018-10-19 13:46:09 +09001204 sdkLibraryProperties sdkLibraryProperties
Jiyong Parkc678ad32018-04-10 13:07:10 +09001205
Paul Duffin3375e352020-04-28 10:44:03 +01001206 // Map from api scope to the scope specific property structure.
1207 scopeToProperties map[*apiScope]*ApiScopeProperties
1208
Paul Duffin56d44902020-01-31 13:36:25 +00001209 commonToSdkLibraryAndImport
Jiyong Parkc678ad32018-04-10 13:07:10 +09001210}
1211
Inseob Kimc0907f12019-02-08 21:00:45 +09001212var _ SdkLibraryDependency = (*SdkLibrary)(nil)
Colin Cross897d2ed2019-02-11 14:03:51 -08001213
Paul Duffin3375e352020-04-28 10:44:03 +01001214func (module *SdkLibrary) generateTestAndSystemScopesByDefault() bool {
1215 return module.sdkLibraryProperties.Generate_system_and_test_apis
1216}
1217
1218func (module *SdkLibrary) getGeneratedApiScopes(ctx android.EarlyModuleContext) apiScopes {
1219 // Check to see if any scopes have been explicitly enabled. If any have then all
1220 // must be.
1221 anyScopesExplicitlyEnabled := false
1222 for _, scope := range allApiScopes {
1223 scopeProperties := module.scopeToProperties[scope]
1224 if scopeProperties.Enabled != nil {
1225 anyScopesExplicitlyEnabled = true
1226 break
1227 }
Paul Duffind1b3a922020-01-22 11:57:20 +00001228 }
Paul Duffin3375e352020-04-28 10:44:03 +01001229
1230 var generatedScopes apiScopes
1231 enabledScopes := make(map[*apiScope]struct{})
1232 for _, scope := range allApiScopes {
1233 scopeProperties := module.scopeToProperties[scope]
1234 // If any scopes are explicitly enabled then ignore the legacy enabled status.
1235 // This is to ensure that any new usages of this module type do not rely on legacy
1236 // behaviour.
1237 defaultEnabledStatus := false
1238 if anyScopesExplicitlyEnabled {
1239 defaultEnabledStatus = scope.defaultEnabledStatus
1240 } else {
1241 defaultEnabledStatus = scope.legacyEnabledStatus(module)
1242 }
1243 enabled := proptools.BoolDefault(scopeProperties.Enabled, defaultEnabledStatus)
1244 if enabled {
1245 enabledScopes[scope] = struct{}{}
1246 generatedScopes = append(generatedScopes, scope)
1247 }
1248 }
1249
1250 // Now check to make sure that any scope that is extended by an enabled scope is also
1251 // enabled.
1252 for _, scope := range allApiScopes {
1253 if _, ok := enabledScopes[scope]; ok {
1254 extends := scope.extends
1255 if extends != nil {
1256 if _, ok := enabledScopes[extends]; !ok {
1257 ctx.ModuleErrorf("enabled api scope %q depends on disabled scope %q", scope, extends)
1258 }
1259 }
1260 }
1261 }
1262
1263 return generatedScopes
Paul Duffind1b3a922020-01-22 11:57:20 +00001264}
1265
satayev758968a2021-12-06 11:42:40 +00001266var _ android.ModuleWithMinSdkVersionCheck = (*SdkLibrary)(nil)
1267
satayev8f088b02021-12-06 11:40:46 +00001268func (module *SdkLibrary) CheckMinSdkVersion(ctx android.ModuleContext) {
Spandan Das8c9ae7e2023-03-03 21:20:36 +00001269 android.CheckMinSdkVersion(ctx, module.MinSdkVersion(ctx), func(c android.ModuleContext, do android.PayloadDepsCallback) {
satayev8f088b02021-12-06 11:40:46 +00001270 ctx.WalkDeps(func(child android.Module, parent android.Module) bool {
1271 isExternal := !module.depIsInSameApex(ctx, child)
1272 if am, ok := child.(android.ApexModule); ok {
1273 if !do(ctx, parent, am, isExternal) {
1274 return false
1275 }
1276 }
1277 return !isExternal
1278 })
1279 })
1280}
1281
Paul Duffineedc5d52020-06-12 17:46:39 +01001282type sdkLibraryComponentTag struct {
1283 blueprint.BaseDependencyTag
1284 name string
1285}
1286
1287// Mark this tag so dependencies that use it are excluded from visibility enforcement.
1288func (t sdkLibraryComponentTag) ExcludeFromVisibilityEnforcement() {}
1289
1290var xmlPermissionsFileTag = sdkLibraryComponentTag{name: "xml-permissions-file"}
Paul Duffine74ac732020-02-06 13:51:46 +00001291
Jiyong Parke3833882020-02-17 17:28:10 +09001292func IsXmlPermissionsFileDepTag(depTag blueprint.DependencyTag) bool {
Paul Duffineedc5d52020-06-12 17:46:39 +01001293 if dt, ok := depTag.(sdkLibraryComponentTag); ok {
Jiyong Parke3833882020-02-17 17:28:10 +09001294 return dt == xmlPermissionsFileTag
1295 }
1296 return false
1297}
1298
Paul Duffineedc5d52020-06-12 17:46:39 +01001299var implLibraryTag = sdkLibraryComponentTag{name: "impl-library"}
Paul Duffin5df79302020-05-16 15:52:12 +01001300
Paul Duffin44f1d842020-06-26 20:17:02 +01001301// Add the dependencies on the child modules in the component deps mutator.
1302func (module *SdkLibrary) ComponentDepsMutator(ctx android.BottomUpMutatorContext) {
Paul Duffin3375e352020-04-28 10:44:03 +01001303 for _, apiScope := range module.getGeneratedApiScopes(ctx) {
Paul Duffind1b3a922020-01-22 11:57:20 +00001304 // Add dependencies to the stubs library
Spandan Das877f39d2023-03-29 16:19:51 +00001305 stubModuleName := module.stubsLibraryModuleName(apiScope)
Jihoon Kang1147b312023-06-08 23:25:57 +00001306
Spandan Das877f39d2023-03-29 16:19:51 +00001307 ctx.AddVariationDependencies(nil, apiScope.stubsTag, stubModuleName)
Paul Duffind1b3a922020-01-22 11:57:20 +00001308
Paul Duffin15f34ef2020-07-20 18:04:44 +01001309 // Add a dependency on the stubs source in order to access both stubs source and api information.
1310 ctx.AddVariationDependencies(nil, apiScope.stubsSourceAndApiTag, module.stubsSourceModuleName(apiScope))
Paul Duffin958806b2022-05-16 13:10:47 +00001311
1312 if module.compareAgainstLatestApi(apiScope) {
1313 // Add dependencies on the latest finalized version of the API .txt file.
1314 latestApiModuleName := module.latestApiModuleName(apiScope)
1315 ctx.AddDependency(module, apiScope.latestApiModuleTag, latestApiModuleName)
1316
1317 // Add dependencies on the latest finalized version of the remove API .txt file.
1318 latestRemovedApiModuleName := module.latestRemovedApiModuleName(apiScope)
1319 ctx.AddDependency(module, apiScope.latestRemovedApiModuleTag, latestRemovedApiModuleName)
1320 }
Sundong Ahn054b19a2018-10-19 13:46:09 +09001321 }
1322
Paul Duffindfa131e2020-05-15 20:37:11 +01001323 if module.requiresRuntimeImplementationLibrary() {
Paul Duffin5df79302020-05-16 15:52:12 +01001324 // Add dependency to the rule for generating the implementation library.
1325 ctx.AddDependency(module, implLibraryTag, module.implLibraryModuleName())
1326
Paul Duffindfa131e2020-05-15 20:37:11 +01001327 if module.sharedLibrary() {
1328 // Add dependency to the rule for generating the xml permissions file
Paul Duffineedc5d52020-06-12 17:46:39 +01001329 ctx.AddDependency(module, xmlPermissionsFileTag, module.xmlPermissionsModuleName())
Paul Duffindfa131e2020-05-15 20:37:11 +01001330 }
Paul Duffin44f1d842020-06-26 20:17:02 +01001331 }
1332}
Paul Duffine74ac732020-02-06 13:51:46 +00001333
Paul Duffin44f1d842020-06-26 20:17:02 +01001334// Add other dependencies as normal.
1335func (module *SdkLibrary) DepsMutator(ctx android.BottomUpMutatorContext) {
Anton Hanssone77fccc2021-01-20 16:52:41 +00001336 var missingApiModules []string
1337 for _, apiScope := range module.getGeneratedApiScopes(ctx) {
1338 if apiScope.unstable {
1339 continue
1340 }
Paul Duffin958806b2022-05-16 13:10:47 +00001341 if m := module.latestApiModuleName(apiScope); !ctx.OtherModuleExists(m) {
Anton Hanssone77fccc2021-01-20 16:52:41 +00001342 missingApiModules = append(missingApiModules, m)
1343 }
Paul Duffin958806b2022-05-16 13:10:47 +00001344 if m := module.latestRemovedApiModuleName(apiScope); !ctx.OtherModuleExists(m) {
Anton Hanssone77fccc2021-01-20 16:52:41 +00001345 missingApiModules = append(missingApiModules, m)
1346 }
Paul Duffin958806b2022-05-16 13:10:47 +00001347 if m := module.latestIncompatibilitiesModuleName(apiScope); !ctx.OtherModuleExists(m) {
Jaewoong Jung1a97ee02021-03-09 13:25:02 -08001348 missingApiModules = append(missingApiModules, m)
1349 }
Anton Hanssone77fccc2021-01-20 16:52:41 +00001350 }
1351 if len(missingApiModules) != 0 && !module.sdkLibraryProperties.Unsafe_ignore_missing_latest_api {
1352 m := module.Name() + " is missing tracking files for previously released library versions.\n"
1353 m += "You need to do one of the following:\n"
1354 m += "- Add `unsafe_ignore_missing_latest_api: true` to your blueprint (to disable compat tracking)\n"
1355 m += "- Add a set of prebuilt txt files representing the last released version of this library for compat checking.\n"
1356 m += " (the current set of API files can be used as a seed for this compatibility tracking\n"
1357 m += "\n"
1358 m += "The following filegroup modules are missing:\n "
1359 m += strings.Join(missingApiModules, "\n ") + "\n"
1360 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."
1361 ctx.ModuleErrorf(m)
1362 }
Paul Duffin44f1d842020-06-26 20:17:02 +01001363 if module.requiresRuntimeImplementationLibrary() {
Paul Duffindfa131e2020-05-15 20:37:11 +01001364 // Only add the deps for the library if it is actually going to be built.
1365 module.Library.deps(ctx)
1366 }
Jiyong Parkc678ad32018-04-10 13:07:10 +09001367}
1368
Paul Duffin46dc45a2020-05-14 15:39:10 +01001369func (module *SdkLibrary) OutputFiles(tag string) (android.Paths, error) {
1370 paths, err := module.commonOutputFiles(tag)
Colin Cross4acaea92021-12-10 23:05:02 +00001371 if paths != nil || err != nil {
Paul Duffin46dc45a2020-05-14 15:39:10 +01001372 return paths, err
1373 }
Colin Cross4acaea92021-12-10 23:05:02 +00001374 if module.requiresRuntimeImplementationLibrary() {
1375 return module.Library.OutputFiles(tag)
1376 }
1377 if tag == "" {
1378 return nil, nil
1379 }
1380 return nil, fmt.Errorf("unsupported module reference tag %q", tag)
Paul Duffin46dc45a2020-05-14 15:39:10 +01001381}
1382
Inseob Kimc0907f12019-02-08 21:00:45 +09001383func (module *SdkLibrary) GenerateAndroidBuildActions(ctx android.ModuleContext) {
satayev8f088b02021-12-06 11:40:46 +00001384 if proptools.String(module.deviceProperties.Min_sdk_version) != "" {
1385 module.CheckMinSdkVersion(ctx)
1386 }
1387
Paul Duffina2ae7e02020-09-11 11:55:00 +01001388 module.generateCommonBuildActions(ctx)
1389
Paul Duffindfa131e2020-05-15 20:37:11 +01001390 // Only build an implementation library if required.
1391 if module.requiresRuntimeImplementationLibrary() {
Paul Duffin43db9be2019-12-30 17:35:49 +00001392 module.Library.GenerateAndroidBuildActions(ctx)
1393 }
Sundong Ahn054b19a2018-10-19 13:46:09 +09001394
Paul Duffinb97b1572021-04-29 21:50:40 +01001395 // Collate the components exported by this module. All scope specific modules are exported but
1396 // the impl and xml component modules are not.
1397 exportedComponents := map[string]struct{}{}
1398
Sundong Ahn57368eb2018-07-06 11:20:23 +09001399 // Record the paths to the header jars of the library (stubs and impl).
Paul Duffind1b3a922020-01-22 11:57:20 +00001400 // When this java_sdk_library is depended upon from others via "libs" property,
Jiyong Parkc678ad32018-04-10 13:07:10 +09001401 // the recorded paths will be returned depending on the link type of the caller.
1402 ctx.VisitDirectDeps(func(to android.Module) {
Jiyong Parkc678ad32018-04-10 13:07:10 +09001403 tag := ctx.OtherModuleDependencyTag(to)
1404
Paul Duffinc8782502020-04-29 20:45:27 +01001405 // Extract information from any of the scope specific dependencies.
1406 if scopeTag, ok := tag.(scopeDependencyTag); ok {
1407 apiScope := scopeTag.apiScope
Paul Duffin803a9562020-05-20 11:52:25 +01001408 scopePaths := module.getScopePathsCreateIfNeeded(apiScope)
Paul Duffinc8782502020-04-29 20:45:27 +01001409
1410 // Extract information from the dependency. The exact information extracted
1411 // is determined by the nature of the dependency which is determined by the tag.
1412 scopeTag.extractDepInfo(ctx, to, scopePaths)
Paul Duffinb97b1572021-04-29 21:50:40 +01001413
1414 exportedComponents[ctx.OtherModuleName(to)] = struct{}{}
Sundong Ahn20e998b2018-07-24 11:19:26 +09001415 }
Jiyong Parkc678ad32018-04-10 13:07:10 +09001416 })
Paul Duffinb97b1572021-04-29 21:50:40 +01001417
1418 // Make the set of components exported by this module available for use elsewhere.
Cole Faust18994c72023-02-28 16:02:16 -08001419 exportedComponentInfo := android.ExportedComponentsInfo{Components: android.SortedKeys(exportedComponents)}
Paul Duffinb97b1572021-04-29 21:50:40 +01001420 ctx.SetProvider(android.ExportedComponentsInfoProvider, exportedComponentInfo)
Paul Duffin958806b2022-05-16 13:10:47 +00001421
1422 // Provide additional information for inclusion in an sdk's generated .info file.
1423 additionalSdkInfo := map[string]interface{}{}
1424 additionalSdkInfo["dist_stem"] = module.distStem()
Paul Duffine8409952022-09-22 16:24:46 +01001425 baseModuleName := module.distStem()
Paul Duffin958806b2022-05-16 13:10:47 +00001426 scopes := map[string]interface{}{}
1427 additionalSdkInfo["scopes"] = scopes
1428 for scope, scopePaths := range module.scopePaths {
1429 scopeInfo := map[string]interface{}{}
1430 scopes[scope.name] = scopeInfo
1431 scopeInfo["current_api"] = scope.snapshotRelativeCurrentApiTxtPath(baseModuleName)
1432 scopeInfo["removed_api"] = scope.snapshotRelativeRemovedApiTxtPath(baseModuleName)
1433 if p := scopePaths.latestApiPath; p.Valid() {
1434 scopeInfo["latest_api"] = p.Path().String()
1435 }
1436 if p := scopePaths.latestRemovedApiPath; p.Valid() {
1437 scopeInfo["latest_removed_api"] = p.Path().String()
1438 }
1439 }
1440 ctx.SetProvider(android.AdditionalSdkInfoProvider, android.AdditionalSdkInfo{additionalSdkInfo})
Jiyong Parkc678ad32018-04-10 13:07:10 +09001441}
1442
Jiyong Park0b0e1b92019-12-03 13:24:29 +09001443func (module *SdkLibrary) AndroidMkEntries() []android.AndroidMkEntries {
Paul Duffindfa131e2020-05-15 20:37:11 +01001444 if !module.requiresRuntimeImplementationLibrary() {
Paul Duffin43db9be2019-12-30 17:35:49 +00001445 return nil
1446 }
Jiyong Park0b0e1b92019-12-03 13:24:29 +09001447 entriesList := module.Library.AndroidMkEntries()
Yo Chiang07d75072020-06-05 17:43:19 +08001448 if module.sharedLibrary() {
1449 entries := &entriesList[0]
1450 entries.Required = append(entries.Required, module.xmlPermissionsModuleName())
1451 }
Jiyong Park0b0e1b92019-12-03 13:24:29 +09001452 return entriesList
Jiyong Park82484c02018-04-23 21:41:26 +09001453}
1454
Anton Hansson5fd5d242020-03-27 19:43:19 +00001455// The dist path of the stub artifacts
1456func (module *SdkLibrary) apiDistPath(apiScope *apiScope) string {
Colin Crossf0eace92021-06-02 13:02:23 -07001457 return path.Join("apistubs", module.distGroup(), apiScope.name)
Anton Hansson5fd5d242020-03-27 19:43:19 +00001458}
1459
Paul Duffin12ceb462019-12-24 20:31:31 +00001460// Get the sdk version for use when compiling the stubs library.
Paul Duffin780c5f42020-05-12 15:52:55 +01001461func (module *SdkLibrary) sdkVersionForStubsLibrary(mctx android.EarlyModuleContext, apiScope *apiScope) string {
Paul Duffin87a05a32020-05-12 11:50:28 +01001462 scopeProperties := module.scopeToProperties[apiScope]
1463 if scopeProperties.Sdk_version != nil {
1464 return proptools.String(scopeProperties.Sdk_version)
1465 }
1466
Jiyong Parkf1691d22021-03-29 20:11:58 +09001467 sdkDep := decodeSdkDep(mctx, android.SdkContext(&module.Library))
Paul Duffin12ceb462019-12-24 20:31:31 +00001468 if sdkDep.hasStandardLibs() {
1469 // If building against a standard sdk then use the sdk version appropriate for the scope.
Paul Duffind1b3a922020-01-22 11:57:20 +00001470 return apiScope.sdkVersion
Paul Duffin12ceb462019-12-24 20:31:31 +00001471 } else {
1472 // Otherwise, use no system module.
1473 return "none"
1474 }
1475}
1476
Paul Duffin31310252020-11-20 21:26:20 +00001477func (module *SdkLibrary) distStem() string {
1478 return proptools.StringDefault(module.sdkLibraryProperties.Dist_stem, module.BaseModuleName())
1479}
1480
Colin Cross986b69a2021-06-01 13:13:40 -07001481// distGroup returns the subdirectory of the dist path of the stub artifacts.
1482func (module *SdkLibrary) distGroup() string {
Colin Cross59b92bf2021-06-01 14:07:56 -07001483 return proptools.StringDefault(module.sdkLibraryProperties.Dist_group, "unknown")
Colin Cross986b69a2021-06-01 13:13:40 -07001484}
1485
Paul Duffin958806b2022-05-16 13:10:47 +00001486func latestPrebuiltApiModuleName(name string, apiScope *apiScope) string {
1487 return PrebuiltApiModuleName(name, apiScope.name, "latest")
1488}
1489
Paul Duffind1b3a922020-01-22 11:57:20 +00001490func (module *SdkLibrary) latestApiFilegroupName(apiScope *apiScope) string {
Paul Duffin958806b2022-05-16 13:10:47 +00001491 return ":" + module.latestApiModuleName(apiScope)
1492}
1493
1494func (module *SdkLibrary) latestApiModuleName(apiScope *apiScope) string {
1495 return latestPrebuiltApiModuleName(module.distStem(), apiScope)
Jiyong Park58c518b2018-05-12 22:29:12 +09001496}
Jiyong Parkc678ad32018-04-10 13:07:10 +09001497
Paul Duffind1b3a922020-01-22 11:57:20 +00001498func (module *SdkLibrary) latestRemovedApiFilegroupName(apiScope *apiScope) string {
Paul Duffin958806b2022-05-16 13:10:47 +00001499 return ":" + module.latestRemovedApiModuleName(apiScope)
1500}
1501
1502func (module *SdkLibrary) latestRemovedApiModuleName(apiScope *apiScope) string {
1503 return latestPrebuiltApiModuleName(module.distStem()+"-removed", apiScope)
Jiyong Parkc678ad32018-04-10 13:07:10 +09001504}
1505
Jaewoong Jung1a97ee02021-03-09 13:25:02 -08001506func (module *SdkLibrary) latestIncompatibilitiesFilegroupName(apiScope *apiScope) string {
Paul Duffin958806b2022-05-16 13:10:47 +00001507 return ":" + module.latestIncompatibilitiesModuleName(apiScope)
1508}
1509
1510func (module *SdkLibrary) latestIncompatibilitiesModuleName(apiScope *apiScope) string {
1511 return latestPrebuiltApiModuleName(module.distStem()+"-incompatibilities", apiScope)
Jaewoong Jung1a97ee02021-03-09 13:25:02 -08001512}
1513
Jihoon Kang1c92c3e2023-03-23 17:44:51 +00001514func (module *SdkLibrary) contributesToApiSurface(c android.Config) bool {
1515 _, exists := c.GetApiLibraries()[module.Name()]
1516 return exists
1517}
1518
Anton Hansson944e77d2020-08-19 11:40:22 +01001519func childModuleVisibility(childVisibility []string) []string {
1520 if childVisibility == nil {
1521 // No child visibility set. The child will use the visibility of the sdk_library.
1522 return nil
1523 }
1524
1525 // Prepend an override to ignore the sdk_library's visibility, and rely on the child visibility.
1526 var visibility []string
1527 visibility = append(visibility, "//visibility:override")
1528 visibility = append(visibility, childVisibility...)
1529 return visibility
1530}
1531
Paul Duffin5df79302020-05-16 15:52:12 +01001532// Creates the implementation java library
1533func (module *SdkLibrary) createImplLibrary(mctx android.DefaultableHookContext) {
Anton Hansson944e77d2020-08-19 11:40:22 +01001534 visibility := childModuleVisibility(module.sdkLibraryProperties.Impl_library_visibility)
1535
Paul Duffin5df79302020-05-16 15:52:12 +01001536 props := struct {
Paul Duffin77590a82022-04-28 14:13:30 +00001537 Name *string
1538 Visibility []string
1539 Instrument bool
1540 Libs []string
1541 Static_libs []string
1542 Apex_available []string
Paul Duffin5df79302020-05-16 15:52:12 +01001543 }{
1544 Name: proptools.StringPtr(module.implLibraryModuleName()),
Anton Hansson944e77d2020-08-19 11:40:22 +01001545 Visibility: visibility,
Paul Duffin296cf332020-06-18 21:09:55 +01001546 // Set the instrument property to ensure it is instrumented when instrumentation is required.
1547 Instrument: true,
Anton Hansson7f66efa2020-10-08 14:47:23 +01001548 // Set the impl_only libs. Note that the module's "Libs" get appended as well, via the
1549 // addition of &module.properties below.
1550 Libs: module.sdkLibraryProperties.Impl_only_libs,
Paul Duffin77590a82022-04-28 14:13:30 +00001551 // Set the impl_only static libs. Note that the module's "static_libs" get appended as well, via the
1552 // addition of &module.properties below.
1553 Static_libs: module.sdkLibraryProperties.Impl_only_static_libs,
1554 // Pass the apex_available settings down so that the impl library can be statically
1555 // embedded within a library that is added to an APEX. Needed for updatable-media.
1556 Apex_available: module.ApexAvailable(),
Paul Duffin5df79302020-05-16 15:52:12 +01001557 }
1558
1559 properties := []interface{}{
1560 &module.properties,
1561 &module.protoProperties,
1562 &module.deviceProperties,
Liz Kammera7a64f32020-07-09 15:16:41 -07001563 &module.dexProperties,
Paul Duffin5df79302020-05-16 15:52:12 +01001564 &module.dexpreoptProperties,
Colin Cross014489c2020-06-02 20:09:13 -07001565 &module.linter.properties,
Paul Duffin5df79302020-05-16 15:52:12 +01001566 &props,
1567 module.sdkComponentPropertiesForChildLibrary(),
1568 }
1569 mctx.CreateModule(LibraryFactory, properties...)
1570}
1571
Jiyong Parkc678ad32018-04-10 13:07:10 +09001572// Creates a static java library that has API stubs
Paul Duffinf0229202020-04-29 16:47:28 +01001573func (module *SdkLibrary) createStubsLibrary(mctx android.DefaultableHookContext, apiScope *apiScope) {
Jiyong Parkc678ad32018-04-10 13:07:10 +09001574 props := struct {
Dan Willemsen9f435972020-05-28 15:28:00 -07001575 Name *string
1576 Visibility []string
1577 Srcs []string
1578 Installable *bool
1579 Sdk_version *string
1580 System_modules *string
1581 Patch_module *string
1582 Libs []string
Anton Hanssondae54cd2021-04-21 16:30:10 +01001583 Static_libs []string
Dan Willemsen9f435972020-05-28 15:28:00 -07001584 Compile_dex *bool
1585 Java_version *string
1586 Openjdk9 struct {
Sundong Ahn054b19a2018-10-19 13:46:09 +09001587 Srcs []string
1588 Javacflags []string
1589 }
Anton Hansson5fd5d242020-03-27 19:43:19 +00001590 Dist struct {
1591 Targets []string
1592 Dest *string
1593 Dir *string
1594 Tag *string
1595 }
Jiyong Parkc678ad32018-04-10 13:07:10 +09001596 }{}
1597
Jihoon Kang1147b312023-06-08 23:25:57 +00001598 props.Name = proptools.StringPtr(module.sourceStubLibraryModuleName(apiScope))
Anton Hansson944e77d2020-08-19 11:40:22 +01001599 props.Visibility = childModuleVisibility(module.sdkLibraryProperties.Stubs_library_visibility)
Jiyong Parkc678ad32018-04-10 13:07:10 +09001600 // sources are generated from the droiddoc
Paul Duffinc3091c82020-05-08 14:16:20 +01001601 props.Srcs = []string{":" + module.stubsSourceModuleName(apiScope)}
Paul Duffin12ceb462019-12-24 20:31:31 +00001602 sdkVersion := module.sdkVersionForStubsLibrary(mctx, apiScope)
Paul Duffin52d398a2019-06-11 12:31:14 +01001603 props.Sdk_version = proptools.StringPtr(sdkVersion)
Paul Duffina18abc22020-05-16 18:54:24 +01001604 props.System_modules = module.deviceProperties.System_modules
1605 props.Patch_module = module.properties.Patch_module
Paul Duffin367ab912019-12-23 19:40:36 +00001606 props.Installable = proptools.BoolPtr(false)
Sundong Ahn054b19a2018-10-19 13:46:09 +09001607 props.Libs = module.sdkLibraryProperties.Stub_only_libs
Mark Whitebeb6e9f2023-08-10 00:07:03 +00001608 props.Libs = append(props.Libs, module.scopeToProperties[apiScope].Libs...)
Anton Hanssondae54cd2021-04-21 16:30:10 +01001609 props.Static_libs = module.sdkLibraryProperties.Stub_only_static_libs
Paul Duffine22c2ab2020-05-20 19:35:27 +01001610 // The stub-annotations library contains special versions of the annotations
1611 // with CLASS retention policy, so that they're kept.
1612 if proptools.Bool(module.sdkLibraryProperties.Annotations_enabled) {
1613 props.Libs = append(props.Libs, "stub-annotations")
1614 }
Paul Duffina18abc22020-05-16 18:54:24 +01001615 props.Openjdk9.Srcs = module.properties.Openjdk9.Srcs
1616 props.Openjdk9.Javacflags = module.properties.Openjdk9.Javacflags
Anton Hansson83509b52020-05-21 09:21:57 +01001617 // We compile the stubs for 1.8 in line with the main android.jar stubs, and potential
1618 // interop with older developer tools that don't support 1.9.
1619 props.Java_version = proptools.StringPtr("1.8")
Paul Duffinf4600f62021-05-13 22:34:45 +01001620
Paul Duffin859fe962020-05-15 10:20:31 +01001621 mctx.CreateModule(LibraryFactory, &props, module.sdkComponentPropertiesForChildLibrary())
Jiyong Parkc678ad32018-04-10 13:07:10 +09001622}
1623
Paul Duffin6d0886e2020-04-07 18:49:53 +01001624// Creates a droidstubs module that creates stubs source files from the given full source
Paul Duffinc8782502020-04-29 20:45:27 +01001625// files and also updates and checks the API specification files.
Paul Duffin15f34ef2020-07-20 18:04:44 +01001626func (module *SdkLibrary) createStubsSourcesAndApi(mctx android.DefaultableHookContext, apiScope *apiScope, name string, scopeSpecificDroidstubsArgs []string) {
Jiyong Parkc678ad32018-04-10 13:07:10 +09001627 props := struct {
Sundong Ahn054b19a2018-10-19 13:46:09 +09001628 Name *string
Paul Duffin4911a892020-04-29 23:35:13 +01001629 Visibility []string
Sundong Ahn054b19a2018-10-19 13:46:09 +09001630 Srcs []string
1631 Installable *bool
Paul Duffin52d398a2019-06-11 12:31:14 +01001632 Sdk_version *string
Jihoon Kang3198f3c2023-01-26 08:08:52 +00001633 Api_surface *string
Paul Duffin12ceb462019-12-24 20:31:31 +00001634 System_modules *string
Sundong Ahn054b19a2018-10-19 13:46:09 +09001635 Libs []string
Paul Duffin6877e6d2020-09-25 19:59:14 +01001636 Output_javadoc_comments *bool
Paul Duffin11512472019-02-11 15:55:17 +00001637 Arg_files []string
Sundong Ahn054b19a2018-10-19 13:46:09 +09001638 Args *string
Sundong Ahn054b19a2018-10-19 13:46:09 +09001639 Java_version *string
Paul Duffine22c2ab2020-05-20 19:35:27 +01001640 Annotations_enabled *bool
Sundong Ahn054b19a2018-10-19 13:46:09 +09001641 Merge_annotations_dirs []string
1642 Merge_inclusion_annotations_dirs []string
Paul Duffin0ff08bd2020-04-29 13:30:54 +01001643 Generate_stubs *bool
Anton Hanssone87b03d2020-12-21 15:29:34 +00001644 Previous_api *string
Sundong Ahn054b19a2018-10-19 13:46:09 +09001645 Check_api struct {
Anton Hanssone6056152020-12-31 10:37:27 +00001646 Current ApiToCheck
1647 Last_released ApiToCheck
Paul Duffin160fe412020-05-10 19:32:20 +01001648
1649 Api_lint struct {
1650 Enabled *bool
1651 New_since *string
1652 Baseline_file *string
1653 }
Jiyong Park58c518b2018-05-12 22:29:12 +09001654 }
Sundong Ahn1b92c822018-05-29 11:35:17 +09001655 Aidl struct {
1656 Include_dirs []string
1657 Local_include_dirs []string
1658 }
Paul Duffin040e9062020-11-23 17:41:36 +00001659 Dists []android.Dist
Jiyong Parkc678ad32018-04-10 13:07:10 +09001660 }{}
1661
Paul Duffin7b78b4d2020-04-28 14:08:32 +01001662 // The stubs source processing uses the same compile time classpath when extracting the
1663 // API from the implementation library as it does when compiling it. i.e. the same
1664 // * sdk version
1665 // * system_modules
1666 // * libs (static_libs/libs)
Paul Duffin250e6192019-06-07 10:44:37 +01001667
Paul Duffin0ff08bd2020-04-29 13:30:54 +01001668 props.Name = proptools.StringPtr(name)
Anton Hansson944e77d2020-08-19 11:40:22 +01001669 props.Visibility = childModuleVisibility(module.sdkLibraryProperties.Stubs_source_visibility)
Paul Duffina18abc22020-05-16 18:54:24 +01001670 props.Srcs = append(props.Srcs, module.properties.Srcs...)
Anton Hanssonf8ea3722021-09-16 14:24:13 +01001671 props.Srcs = append(props.Srcs, module.sdkLibraryProperties.Api_srcs...)
Paul Duffina18abc22020-05-16 18:54:24 +01001672 props.Sdk_version = module.deviceProperties.Sdk_version
Jihoon Kang3198f3c2023-01-26 08:08:52 +00001673 props.Api_surface = &apiScope.name
Paul Duffina18abc22020-05-16 18:54:24 +01001674 props.System_modules = module.deviceProperties.System_modules
Jiyong Parkc678ad32018-04-10 13:07:10 +09001675 props.Installable = proptools.BoolPtr(false)
Sundong Ahne6f0b052018-06-05 16:46:14 +09001676 // A droiddoc module has only one Libs property and doesn't distinguish between
1677 // shared libs and static libs. So we need to add both of these libs to Libs property.
Paul Duffina18abc22020-05-16 18:54:24 +01001678 props.Libs = module.properties.Libs
1679 props.Libs = append(props.Libs, module.properties.Static_libs...)
Nikita Ioffed732da72022-11-21 12:38:25 +00001680 props.Libs = append(props.Libs, module.sdkLibraryProperties.Stub_only_libs...)
Mark Whitebeb6e9f2023-08-10 00:07:03 +00001681 props.Libs = append(props.Libs, module.scopeToProperties[apiScope].Libs...)
Paul Duffina18abc22020-05-16 18:54:24 +01001682 props.Aidl.Include_dirs = module.deviceProperties.Aidl.Include_dirs
1683 props.Aidl.Local_include_dirs = module.deviceProperties.Aidl.Local_include_dirs
1684 props.Java_version = module.properties.Java_version
Jiyong Parkc678ad32018-04-10 13:07:10 +09001685
Paul Duffine22c2ab2020-05-20 19:35:27 +01001686 props.Annotations_enabled = module.sdkLibraryProperties.Annotations_enabled
Sundong Ahn054b19a2018-10-19 13:46:09 +09001687 props.Merge_annotations_dirs = module.sdkLibraryProperties.Merge_annotations_dirs
1688 props.Merge_inclusion_annotations_dirs = module.sdkLibraryProperties.Merge_inclusion_annotations_dirs
1689
Paul Duffin6d0886e2020-04-07 18:49:53 +01001690 droidstubsArgs := []string{}
Paul Duffin235ffff2019-12-24 10:41:30 +00001691 if len(module.sdkLibraryProperties.Api_packages) != 0 {
Paul Duffin6d0886e2020-04-07 18:49:53 +01001692 droidstubsArgs = append(droidstubsArgs, "--stub-packages "+strings.Join(module.sdkLibraryProperties.Api_packages, ":"))
Paul Duffin235ffff2019-12-24 10:41:30 +00001693 }
1694 if len(module.sdkLibraryProperties.Hidden_api_packages) != 0 {
Paul Duffin6d0886e2020-04-07 18:49:53 +01001695 droidstubsArgs = append(droidstubsArgs,
Paul Duffin235ffff2019-12-24 10:41:30 +00001696 android.JoinWithPrefix(module.sdkLibraryProperties.Hidden_api_packages, " --hide-package "))
1697 }
Paul Duffin6d0886e2020-04-07 18:49:53 +01001698 droidstubsArgs = append(droidstubsArgs, module.sdkLibraryProperties.Droiddoc_options...)
Paul Duffin235ffff2019-12-24 10:41:30 +00001699 disabledWarnings := []string{
Paul Duffin235ffff2019-12-24 10:41:30 +00001700 "BroadcastBehavior",
Paul Duffin235ffff2019-12-24 10:41:30 +00001701 "DeprecationMismatch",
Anton Hansson3c0779a2022-02-18 19:24:30 +00001702 "HiddenSuperclass",
Paul Duffin235ffff2019-12-24 10:41:30 +00001703 "HiddenTypeParameter",
Anton Hansson3c0779a2022-02-18 19:24:30 +00001704 "MissingPermission",
1705 "SdkConstant",
Paul Duffin235ffff2019-12-24 10:41:30 +00001706 "Todo",
Anton Hansson3c0779a2022-02-18 19:24:30 +00001707 "UnavailableSymbol",
Paul Duffin235ffff2019-12-24 10:41:30 +00001708 }
Paul Duffin6d0886e2020-04-07 18:49:53 +01001709 droidstubsArgs = append(droidstubsArgs, android.JoinWithPrefix(disabledWarnings, "--hide "))
Sundong Ahnfb2721f2018-09-17 13:23:09 +09001710
Paul Duffin6877e6d2020-09-25 19:59:14 +01001711 // Output Javadoc comments for public scope.
1712 if apiScope == apiScopePublic {
1713 props.Output_javadoc_comments = proptools.BoolPtr(true)
1714 }
1715
Paul Duffin1fb487d2020-04-07 18:50:10 +01001716 // Add in scope specific arguments.
Paul Duffin0ff08bd2020-04-29 13:30:54 +01001717 droidstubsArgs = append(droidstubsArgs, scopeSpecificDroidstubsArgs...)
Paul Duffin11512472019-02-11 15:55:17 +00001718 props.Arg_files = module.sdkLibraryProperties.Droiddoc_option_files
Paul Duffin6d0886e2020-04-07 18:49:53 +01001719 props.Args = proptools.StringPtr(strings.Join(droidstubsArgs, " "))
Jiyong Parkc678ad32018-04-10 13:07:10 +09001720
Paul Duffin15f34ef2020-07-20 18:04:44 +01001721 // List of APIs identified from the provided source files are created. They are later
1722 // compared against to the not-yet-released (a.k.a current) list of APIs and to the
1723 // last-released (a.k.a numbered) list of API.
1724 currentApiFileName := apiScope.apiFilePrefix + "current.txt"
1725 removedApiFileName := apiScope.apiFilePrefix + "removed.txt"
1726 apiDir := module.getApiDir()
1727 currentApiFileName = path.Join(apiDir, currentApiFileName)
1728 removedApiFileName = path.Join(apiDir, removedApiFileName)
Jiyong Parkc678ad32018-04-10 13:07:10 +09001729
Paul Duffin15f34ef2020-07-20 18:04:44 +01001730 // check against the not-yet-release API
1731 props.Check_api.Current.Api_file = proptools.StringPtr(currentApiFileName)
1732 props.Check_api.Current.Removed_api_file = proptools.StringPtr(removedApiFileName)
Jiyong Park58c518b2018-05-12 22:29:12 +09001733
Paul Duffin958806b2022-05-16 13:10:47 +00001734 if module.compareAgainstLatestApi(apiScope) {
Paul Duffin15f34ef2020-07-20 18:04:44 +01001735 // check against the latest released API
1736 latestApiFilegroupName := proptools.StringPtr(module.latestApiFilegroupName(apiScope))
Anton Hanssone87b03d2020-12-21 15:29:34 +00001737 props.Previous_api = latestApiFilegroupName
Paul Duffin15f34ef2020-07-20 18:04:44 +01001738 props.Check_api.Last_released.Api_file = latestApiFilegroupName
1739 props.Check_api.Last_released.Removed_api_file = proptools.StringPtr(
1740 module.latestRemovedApiFilegroupName(apiScope))
Jaewoong Jung1a97ee02021-03-09 13:25:02 -08001741 props.Check_api.Last_released.Baseline_file = proptools.StringPtr(
1742 module.latestIncompatibilitiesFilegroupName(apiScope))
Paul Duffin160fe412020-05-10 19:32:20 +01001743
Paul Duffin15f34ef2020-07-20 18:04:44 +01001744 if proptools.Bool(module.sdkLibraryProperties.Api_lint.Enabled) {
1745 // Enable api lint.
1746 props.Check_api.Api_lint.Enabled = proptools.BoolPtr(true)
1747 props.Check_api.Api_lint.New_since = latestApiFilegroupName
Paul Duffin160fe412020-05-10 19:32:20 +01001748
Paul Duffin15f34ef2020-07-20 18:04:44 +01001749 // If it exists then pass a lint-baseline.txt through to droidstubs.
1750 baselinePath := path.Join(apiDir, apiScope.apiFilePrefix+"lint-baseline.txt")
1751 baselinePathRelativeToRoot := path.Join(mctx.ModuleDir(), baselinePath)
1752 paths, err := mctx.GlobWithDeps(baselinePathRelativeToRoot, nil)
1753 if err != nil {
1754 mctx.ModuleErrorf("error checking for presence of %s: %s", baselinePathRelativeToRoot, err)
1755 }
1756 if len(paths) == 1 {
1757 props.Check_api.Api_lint.Baseline_file = proptools.StringPtr(baselinePath)
1758 } else if len(paths) != 0 {
1759 mctx.ModuleErrorf("error checking for presence of %s: expected one path, found: %v", baselinePathRelativeToRoot, paths)
Paul Duffin160fe412020-05-10 19:32:20 +01001760 }
1761 }
Paul Duffin15f34ef2020-07-20 18:04:44 +01001762 }
Jiyong Park58c518b2018-05-12 22:29:12 +09001763
Paul Duffin15f34ef2020-07-20 18:04:44 +01001764 if !Bool(module.sdkLibraryProperties.No_dist) {
Paul Duffin040e9062020-11-23 17:41:36 +00001765 // Dist the api txt and removed api txt artifacts for sdk builds.
1766 distDir := proptools.StringPtr(path.Join(module.apiDistPath(apiScope), "api"))
1767 for _, p := range []struct {
1768 tag string
1769 pattern string
1770 }{
1771 {tag: ".api.txt", pattern: "%s.txt"},
1772 {tag: ".removed-api.txt", pattern: "%s-removed.txt"},
1773 } {
1774 props.Dists = append(props.Dists, android.Dist{
1775 Targets: []string{"sdk", "win_sdk"},
1776 Dir: distDir,
1777 Dest: proptools.StringPtr(fmt.Sprintf(p.pattern, module.distStem())),
1778 Tag: proptools.StringPtr(p.tag),
1779 })
1780 }
Anton Hansson5fd5d242020-03-27 19:43:19 +00001781 }
1782
Jihoon Kangd48abd52023-02-02 22:32:31 +00001783 mctx.CreateModule(DroidstubsFactory, &props).(*Droidstubs).CallHookIfAvailable(mctx)
Jiyong Parkc678ad32018-04-10 13:07:10 +09001784}
1785
Jihoon Kang1c92c3e2023-03-23 17:44:51 +00001786func (module *SdkLibrary) createApiLibrary(mctx android.DefaultableHookContext, apiScope *apiScope) {
1787 props := struct {
Jihoon Kangca198c22023-06-22 23:13:51 +00001788 Name *string
1789 Visibility []string
1790 Api_contributions []string
1791 Libs []string
1792 Static_libs []string
1793 Full_api_surface_stub *string
Jihoon Kang1c92c3e2023-03-23 17:44:51 +00001794 }{}
1795
1796 props.Name = proptools.StringPtr(module.apiLibraryModuleName(apiScope))
1797 props.Visibility = childModuleVisibility(module.sdkLibraryProperties.Stubs_library_visibility)
1798
1799 apiContributions := []string{}
1800
1801 // Api surfaces are not independent of each other, but have subset relationships,
1802 // and so does the api files. To generate from-text stubs for api surfaces other than public,
1803 // all subset api domains' api_contriubtions must be added as well.
1804 scope := apiScope
1805 for scope != nil {
1806 apiContributions = append(apiContributions, module.stubsSourceModuleName(scope)+".api.contribution")
1807 scope = scope.extends
1808 }
1809
1810 props.Api_contributions = apiContributions
1811 props.Libs = module.properties.Libs
1812 props.Libs = append(props.Libs, module.sdkLibraryProperties.Stub_only_libs...)
Mark Whitebeb6e9f2023-08-10 00:07:03 +00001813 props.Libs = append(props.Libs, module.scopeToProperties[apiScope].Libs...)
Jihoon Kang1c92c3e2023-03-23 17:44:51 +00001814 props.Libs = append(props.Libs, "stub-annotations")
1815 props.Static_libs = module.sdkLibraryProperties.Stub_only_static_libs
Jihoon Kangca198c22023-06-22 23:13:51 +00001816 props.Full_api_surface_stub = proptools.StringPtr(apiScope.kind.DefaultJavaLibraryName() + ".from-text")
Jihoon Kang1c92c3e2023-03-23 17:44:51 +00001817
1818 // android_module_lib_stubs_current.from-text only comprises api contributions from art, conscrypt and i18n.
1819 // Thus, replace with android_module_lib_stubs_current_full.from-text, which comprises every api domains.
1820 if apiScope.kind == android.SdkModule {
Jihoon Kangca198c22023-06-22 23:13:51 +00001821 props.Full_api_surface_stub = proptools.StringPtr(apiScope.kind.DefaultJavaLibraryName() + "_full.from-text")
Jihoon Kang1c92c3e2023-03-23 17:44:51 +00001822 }
1823
1824 mctx.CreateModule(ApiLibraryFactory, &props)
1825}
1826
Jihoon Kang1147b312023-06-08 23:25:57 +00001827func (module *SdkLibrary) createTopLevelStubsLibrary(
1828 mctx android.DefaultableHookContext, apiScope *apiScope, contributesToApiSurface bool) {
1829 props := struct {
1830 Name *string
1831 Visibility []string
1832 Sdk_version *string
1833 Static_libs []string
1834 System_modules *string
1835 Dist struct {
1836 Targets []string
1837 Dest *string
1838 Dir *string
1839 Tag *string
1840 }
1841 Compile_dex *bool
1842 }{}
1843 props.Name = proptools.StringPtr(module.stubsLibraryModuleName(apiScope))
1844 props.Visibility = childModuleVisibility(module.sdkLibraryProperties.Stubs_library_visibility)
1845 sdkVersion := module.sdkVersionForStubsLibrary(mctx, apiScope)
1846 props.Sdk_version = proptools.StringPtr(sdkVersion)
1847
1848 // Add the stub compiling java_library/java_api_library as static lib based on build config
1849 staticLib := module.sourceStubLibraryModuleName(apiScope)
1850 if mctx.Config().BuildFromTextStub() && contributesToApiSurface {
1851 staticLib = module.apiLibraryModuleName(apiScope)
1852 }
1853 props.Static_libs = append(props.Static_libs, staticLib)
1854 props.System_modules = module.deviceProperties.System_modules
1855
1856 // Dist the class jar artifact for sdk builds.
1857 if !Bool(module.sdkLibraryProperties.No_dist) {
1858 props.Dist.Targets = []string{"sdk", "win_sdk"}
1859 props.Dist.Dest = proptools.StringPtr(fmt.Sprintf("%v.jar", module.distStem()))
1860 props.Dist.Dir = proptools.StringPtr(module.apiDistPath(apiScope))
1861 props.Dist.Tag = proptools.StringPtr(".jar")
1862 }
1863
1864 // The imports need to be compiled to dex if the java_sdk_library requests it.
1865 compileDex := module.dexProperties.Compile_dex
1866 if module.stubLibrariesCompiledForDex() {
1867 compileDex = proptools.BoolPtr(true)
1868 }
1869 props.Compile_dex = compileDex
1870
1871 mctx.CreateModule(LibraryFactory, &props, module.sdkComponentPropertiesForChildLibrary())
1872}
1873
Paul Duffin958806b2022-05-16 13:10:47 +00001874func (module *SdkLibrary) compareAgainstLatestApi(apiScope *apiScope) bool {
1875 return !(apiScope.unstable || module.sdkLibraryProperties.Unsafe_ignore_missing_latest_api)
1876}
1877
Paul Duffinea8f8082021-06-24 13:25:57 +01001878// Implements android.ApexModule
Jooyung Han5e9013b2020-03-10 06:23:13 +09001879func (module *SdkLibrary) DepIsInSameApex(mctx android.BaseModuleContext, dep android.Module) bool {
1880 depTag := mctx.OtherModuleDependencyTag(dep)
1881 if depTag == xmlPermissionsFileTag {
1882 return true
1883 }
1884 return module.Library.DepIsInSameApex(mctx, dep)
1885}
1886
Paul Duffinea8f8082021-06-24 13:25:57 +01001887// Implements android.ApexModule
1888func (module *SdkLibrary) UniqueApexVariations() bool {
1889 return module.uniqueApexVariations()
1890}
1891
Jiyong Parkc678ad32018-04-10 13:07:10 +09001892// Creates the xml file that publicizes the runtime library
Paul Duffinf0229202020-04-29 16:47:28 +01001893func (module *SdkLibrary) createXmlFile(mctx android.DefaultableHookContext) {
Spandan Das8c9ae7e2023-03-03 21:20:36 +00001894 moduleMinApiLevel := module.Library.MinSdkVersion(mctx)
Pedro Loureiroc3621422021-09-28 15:40:23 +00001895 var moduleMinApiLevelStr = moduleMinApiLevel.String()
1896 if moduleMinApiLevel == android.NoneApiLevel {
1897 moduleMinApiLevelStr = "current"
1898 }
Jiyong Parke3833882020-02-17 17:28:10 +09001899 props := struct {
Pedro Loureiroc3621422021-09-28 15:40:23 +00001900 Name *string
1901 Lib_name *string
1902 Apex_available []string
1903 On_bootclasspath_since *string
1904 On_bootclasspath_before *string
1905 Min_device_sdk *string
1906 Max_device_sdk *string
1907 Sdk_library_min_api_level *string
Jiyong Parke3833882020-02-17 17:28:10 +09001908 }{
Pedro Loureiroc3621422021-09-28 15:40:23 +00001909 Name: proptools.StringPtr(module.xmlPermissionsModuleName()),
1910 Lib_name: proptools.StringPtr(module.BaseModuleName()),
1911 Apex_available: module.ApexProperties.Apex_available,
1912 On_bootclasspath_since: module.commonSdkLibraryProperties.On_bootclasspath_since,
1913 On_bootclasspath_before: module.commonSdkLibraryProperties.On_bootclasspath_before,
1914 Min_device_sdk: module.commonSdkLibraryProperties.Min_device_sdk,
1915 Max_device_sdk: module.commonSdkLibraryProperties.Max_device_sdk,
1916 Sdk_library_min_api_level: &moduleMinApiLevelStr,
Jiyong Parkc678ad32018-04-10 13:07:10 +09001917 }
Jiyong Parke3833882020-02-17 17:28:10 +09001918
Jiyong Parke3833882020-02-17 17:28:10 +09001919 mctx.CreateModule(sdkLibraryXmlFactory, &props)
Jiyong Parkc678ad32018-04-10 13:07:10 +09001920}
1921
Jiyong Parkf1691d22021-03-29 20:11:58 +09001922func PrebuiltJars(ctx android.BaseModuleContext, baseName string, s android.SdkSpec) android.Paths {
Jiyong Park54105c42021-03-31 18:17:53 +09001923 var ver android.ApiLevel
Jiyong Parkf1691d22021-03-29 20:11:58 +09001924 var kind android.SdkKind
1925 if s.UsePrebuilt(ctx) {
Jiyong Park54105c42021-03-31 18:17:53 +09001926 ver = s.ApiLevel
Jiyong Parkf1691d22021-03-29 20:11:58 +09001927 kind = s.Kind
Jiyong Parkc678ad32018-04-10 13:07:10 +09001928 } else {
Jiyong Park6a927c42020-01-21 02:03:43 +09001929 // We don't have prebuilt SDK for the specific sdkVersion.
1930 // Instead of breaking the build, fallback to use "system_current"
Jiyong Park54105c42021-03-31 18:17:53 +09001931 ver = android.FutureApiLevel
Jiyong Parkf1691d22021-03-29 20:11:58 +09001932 kind = android.SdkSystem
Sundong Ahn054b19a2018-10-19 13:46:09 +09001933 }
Jiyong Park6a927c42020-01-21 02:03:43 +09001934
1935 dir := filepath.Join("prebuilts", "sdk", ver.String(), kind.String())
Paul Duffin50061512020-01-21 16:31:05 +00001936 jar := filepath.Join(dir, baseName+".jar")
Sundong Ahn054b19a2018-10-19 13:46:09 +09001937 jarPath := android.ExistentPathForSource(ctx, jar)
Sundong Ahnae418ac2019-02-28 15:01:28 +09001938 if !jarPath.Valid() {
Colin Cross07c88562020-01-07 09:34:44 -08001939 if ctx.Config().AllowMissingDependencies() {
1940 return android.Paths{android.PathForSource(ctx, jar)}
1941 } else {
Jiyong Parkf1691d22021-03-29 20:11:58 +09001942 ctx.PropertyErrorf("sdk_library", "invalid sdk version %q, %q does not exist", s.Raw, jar)
Colin Cross07c88562020-01-07 09:34:44 -08001943 }
Sundong Ahnae418ac2019-02-28 15:01:28 +09001944 return nil
1945 }
Sundong Ahn054b19a2018-10-19 13:46:09 +09001946 return android.Paths{jarPath.Path()}
1947}
1948
Colin Crossaede88c2020-08-11 12:17:01 -07001949// 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 +01001950//
1951// If either this or the other module are on the platform then this will return
1952// false.
Colin Cross56a83212020-09-15 18:30:11 -07001953func withinSameApexesAs(ctx android.BaseModuleContext, other android.Module) bool {
1954 apexInfo := ctx.Provider(android.ApexInfoProvider).(android.ApexInfo)
1955 otherApexInfo := ctx.OtherModuleProvider(other, android.ApexInfoProvider).(android.ApexInfo)
Jiyong Parkab50b072021-05-12 17:13:56 +09001956 return len(otherApexInfo.InApexVariants) > 0 && reflect.DeepEqual(apexInfo.InApexVariants, otherApexInfo.InApexVariants)
Paul Duffin9b879592020-05-26 13:21:35 +01001957}
1958
Jiyong Parkf1691d22021-03-29 20:11:58 +09001959func (module *SdkLibrary) sdkJars(ctx android.BaseModuleContext, sdkVersion android.SdkSpec, headerJars bool) android.Paths {
Jiyong Park932cdfe2020-05-28 00:19:53 +09001960 // If the client doesn't set sdk_version, but if this library prefers stubs over
1961 // the impl library, let's provide the widest API surface possible. To do so,
1962 // force override sdk_version to module_current so that the closest possible API
1963 // surface could be found in selectHeaderJarsForSdkVersion
Jiyong Parkf1691d22021-03-29 20:11:58 +09001964 if module.defaultsToStubs() && !sdkVersion.Specified() {
Jiyong Park92315372021-04-02 08:45:46 +09001965 sdkVersion = android.SdkSpecFrom(ctx, "module_current")
Jiyong Park932cdfe2020-05-28 00:19:53 +09001966 }
Paul Duffind1b3a922020-01-22 11:57:20 +00001967
Paul Duffindaaa3322020-05-26 18:13:57 +01001968 // Only provide access to the implementation library if it is actually built.
1969 if module.requiresRuntimeImplementationLibrary() {
1970 // Check any special cases for java_sdk_library.
1971 //
1972 // Only allow access to the implementation library in the following condition:
1973 // * No sdk_version specified on the referencing module.
Paul Duffin9b879592020-05-26 13:21:35 +01001974 // * The referencing module is in the same apex as this.
Jiyong Parkf1691d22021-03-29 20:11:58 +09001975 if sdkVersion.Kind == android.SdkPrivate || withinSameApexesAs(ctx, module) {
Paul Duffindaaa3322020-05-26 18:13:57 +01001976 if headerJars {
1977 return module.HeaderJars()
1978 } else {
1979 return module.ImplementationJars()
1980 }
Sundong Ahn054b19a2018-10-19 13:46:09 +09001981 }
Jiyong Parkc678ad32018-04-10 13:07:10 +09001982 }
Paul Duffinb05d4292020-05-20 12:19:10 +01001983
Paul Duffin23970f42020-05-20 14:20:02 +01001984 return module.selectHeaderJarsForSdkVersion(ctx, sdkVersion)
Jiyong Parkc678ad32018-04-10 13:07:10 +09001985}
1986
Sundong Ahn241cd372018-07-13 16:16:44 +09001987// to satisfy SdkLibraryDependency interface
Jiyong Parkf1691d22021-03-29 20:11:58 +09001988func (module *SdkLibrary) SdkHeaderJars(ctx android.BaseModuleContext, sdkVersion android.SdkSpec) android.Paths {
Paul Duffind1b3a922020-01-22 11:57:20 +00001989 return module.sdkJars(ctx, sdkVersion, true /*headerJars*/)
1990}
1991
1992// to satisfy SdkLibraryDependency interface
Jiyong Parkf1691d22021-03-29 20:11:58 +09001993func (module *SdkLibrary) SdkImplementationJars(ctx android.BaseModuleContext, sdkVersion android.SdkSpec) android.Paths {
Paul Duffind1b3a922020-01-22 11:57:20 +00001994 return module.sdkJars(ctx, sdkVersion, false /*headerJars*/)
Sundong Ahn241cd372018-07-13 16:16:44 +09001995}
1996
Colin Cross571cccf2019-02-04 11:22:08 -08001997var javaSdkLibrariesKey = android.NewOnceKey("javaSdkLibraries")
1998
Jiyong Park82484c02018-04-23 21:41:26 +09001999func javaSdkLibraries(config android.Config) *[]string {
Colin Cross571cccf2019-02-04 11:22:08 -08002000 return config.Once(javaSdkLibrariesKey, func() interface{} {
Jiyong Park82484c02018-04-23 21:41:26 +09002001 return &[]string{}
2002 }).(*[]string)
2003}
2004
Paul Duffin749f98f2019-12-30 17:23:46 +00002005func (module *SdkLibrary) getApiDir() string {
2006 return proptools.StringDefault(module.sdkLibraryProperties.Api_dir, "api")
2007}
2008
Jiyong Parkc678ad32018-04-10 13:07:10 +09002009// For a java_sdk_library module, create internal modules for stubs, docs,
2010// runtime libs and xml file. If requested, the stubs and docs are created twice
2011// once for public API level and once for system API level
Paul Duffinf0229202020-04-29 16:47:28 +01002012func (module *SdkLibrary) CreateInternalModules(mctx android.DefaultableHookContext) {
2013 // If the module has been disabled then don't create any child modules.
2014 if !module.Enabled() {
2015 return
2016 }
2017
Paul Duffina18abc22020-05-16 18:54:24 +01002018 if len(module.properties.Srcs) == 0 {
Inseob Kimc0907f12019-02-08 21:00:45 +09002019 mctx.PropertyErrorf("srcs", "java_sdk_library must specify srcs")
Jooyung Han58f26ab2019-12-18 15:34:32 +09002020 return
Inseob Kimc0907f12019-02-08 21:00:45 +09002021 }
2022
Paul Duffin37e0b772019-12-30 17:20:10 +00002023 // If this builds against standard libraries (i.e. is not part of the core libraries)
Paul Duffin4f5c1ef2020-11-19 14:53:43 +00002024 // then assume it provides both system and test apis.
Jiyong Parkf1691d22021-03-29 20:11:58 +09002025 sdkDep := decodeSdkDep(mctx, android.SdkContext(&module.Library))
Paul Duffin37e0b772019-12-30 17:20:10 +00002026 hasSystemAndTestApis := sdkDep.hasStandardLibs()
Paul Duffin3375e352020-04-28 10:44:03 +01002027 module.sdkLibraryProperties.Generate_system_and_test_apis = hasSystemAndTestApis
Paul Duffin4f5c1ef2020-11-19 14:53:43 +00002028
Jaewoong Jung18aefc12020-12-21 09:11:10 -08002029 missingCurrentApi := false
Inseob Kim8098faa2019-03-18 10:19:51 +09002030
Paul Duffin3375e352020-04-28 10:44:03 +01002031 generatedScopes := module.getGeneratedApiScopes(mctx)
Paul Duffind1b3a922020-01-22 11:57:20 +00002032
Paul Duffin749f98f2019-12-30 17:23:46 +00002033 apiDir := module.getApiDir()
Paul Duffin3375e352020-04-28 10:44:03 +01002034 for _, scope := range generatedScopes {
Inseob Kim8098faa2019-03-18 10:19:51 +09002035 for _, api := range []string{"current.txt", "removed.txt"} {
Paul Duffind1b3a922020-01-22 11:57:20 +00002036 path := path.Join(mctx.ModuleDir(), apiDir, scope.apiFilePrefix+api)
Inseob Kim8098faa2019-03-18 10:19:51 +09002037 p := android.ExistentPathForSource(mctx, path)
2038 if !p.Valid() {
Colin Cross18f840c2021-05-20 17:56:54 -07002039 if mctx.Config().AllowMissingDependencies() {
2040 mctx.AddMissingDependencies([]string{path})
2041 } else {
2042 mctx.ModuleErrorf("Current api file %#v doesn't exist", path)
2043 missingCurrentApi = true
2044 }
Inseob Kim8098faa2019-03-18 10:19:51 +09002045 }
2046 }
2047 }
2048
Jaewoong Jung18aefc12020-12-21 09:11:10 -08002049 if missingCurrentApi {
Inseob Kim8098faa2019-03-18 10:19:51 +09002050 script := "build/soong/scripts/gen-java-current-api-files.sh"
2051 p := android.ExistentPathForSource(mctx, script)
2052
2053 if !p.Valid() {
2054 panic(fmt.Sprintf("script file %s doesn't exist", script))
2055 }
2056
2057 mctx.ModuleErrorf("One or more current api files are missing. "+
2058 "You can update them by:\n"+
Paul Duffin37e0b772019-12-30 17:20:10 +00002059 "%s %q %s && m update-api",
Paul Duffind1b3a922020-01-22 11:57:20 +00002060 script, filepath.Join(mctx.ModuleDir(), apiDir),
Paul Duffin3375e352020-04-28 10:44:03 +01002061 strings.Join(generatedScopes.Strings(func(s *apiScope) string { return s.apiFilePrefix }), " "))
Inseob Kim8098faa2019-03-18 10:19:51 +09002062 return
2063 }
2064
Paul Duffin3375e352020-04-28 10:44:03 +01002065 for _, scope := range generatedScopes {
Paul Duffin15f34ef2020-07-20 18:04:44 +01002066 // Use the stubs source name for legacy reasons.
2067 module.createStubsSourcesAndApi(mctx, scope, module.stubsSourceModuleName(scope), scope.droidstubsArgs)
Paul Duffin0ff08bd2020-04-29 13:30:54 +01002068
Paul Duffind1b3a922020-01-22 11:57:20 +00002069 module.createStubsLibrary(mctx, scope)
Jihoon Kang1c92c3e2023-03-23 17:44:51 +00002070
Jihoon Kang1147b312023-06-08 23:25:57 +00002071 contributesToApiSurface := module.contributesToApiSurface(mctx.Config())
2072 if contributesToApiSurface {
Jihoon Kang1c92c3e2023-03-23 17:44:51 +00002073 module.createApiLibrary(mctx, scope)
2074 }
Jihoon Kang1147b312023-06-08 23:25:57 +00002075
2076 module.createTopLevelStubsLibrary(mctx, scope, contributesToApiSurface)
Inseob Kimc0907f12019-02-08 21:00:45 +09002077 }
2078
Paul Duffindfa131e2020-05-15 20:37:11 +01002079 if module.requiresRuntimeImplementationLibrary() {
Paul Duffin5df79302020-05-16 15:52:12 +01002080 // Create child module to create an implementation library.
2081 //
2082 // This temporarily creates a second implementation library that can be explicitly
2083 // referenced.
2084 //
2085 // TODO(b/156618935) - update comment once only one implementation library is created.
2086 module.createImplLibrary(mctx)
2087
Paul Duffindfa131e2020-05-15 20:37:11 +01002088 // Only create an XML permissions file that declares the library as being usable
2089 // as a shared library if required.
2090 if module.sharedLibrary() {
2091 module.createXmlFile(mctx)
2092 }
Paul Duffin43db9be2019-12-30 17:35:49 +00002093
2094 // record java_sdk_library modules so that they are exported to make
2095 javaSdkLibraries := javaSdkLibraries(mctx.Config())
2096 javaSdkLibrariesLock.Lock()
2097 defer javaSdkLibrariesLock.Unlock()
2098 *javaSdkLibraries = append(*javaSdkLibraries, module.BaseModuleName())
2099 }
Anton Hansson7f66efa2020-10-08 14:47:23 +01002100
Paul Duffin77590a82022-04-28 14:13:30 +00002101 // 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 +01002102 module.properties.Libs = append(module.properties.Libs, module.sdkLibraryProperties.Impl_only_libs...)
Paul Duffin77590a82022-04-28 14:13:30 +00002103 module.properties.Static_libs = append(module.properties.Static_libs, module.sdkLibraryProperties.Impl_only_static_libs...)
Inseob Kimc0907f12019-02-08 21:00:45 +09002104}
2105
2106func (module *SdkLibrary) InitSdkLibraryProperties() {
Colin Crossce6734e2020-06-15 16:09:53 -07002107 module.addHostAndDeviceProperties()
2108 module.AddProperties(&module.sdkLibraryProperties)
Sundong Ahn054b19a2018-10-19 13:46:09 +09002109
Paul Duffin71b33cc2021-06-23 11:39:47 +01002110 module.initSdkLibraryComponent(module)
Paul Duffin859fe962020-05-15 10:20:31 +01002111
Paul Duffina18abc22020-05-16 18:54:24 +01002112 module.properties.Installable = proptools.BoolPtr(true)
2113 module.deviceProperties.IsSDKLibrary = true
Inseob Kimc0907f12019-02-08 21:00:45 +09002114}
Sundong Ahn054b19a2018-10-19 13:46:09 +09002115
Paul Duffindfa131e2020-05-15 20:37:11 +01002116func (module *SdkLibrary) requiresRuntimeImplementationLibrary() bool {
2117 return !proptools.Bool(module.sdkLibraryProperties.Api_only)
2118}
2119
Jiyong Park932cdfe2020-05-28 00:19:53 +09002120func (module *SdkLibrary) defaultsToStubs() bool {
2121 return proptools.Bool(module.sdkLibraryProperties.Default_to_stubs)
2122}
2123
Paul Duffin1b1e8062020-05-08 13:44:43 +01002124// Defines how to name the individual component modules the sdk library creates.
2125type sdkLibraryComponentNamingScheme interface {
2126 stubsLibraryModuleName(scope *apiScope, baseName string) string
2127
2128 stubsSourceModuleName(scope *apiScope, baseName string) string
Jihoon Kang1c92c3e2023-03-23 17:44:51 +00002129
2130 apiLibraryModuleName(scope *apiScope, baseName string) string
Jihoon Kang1147b312023-06-08 23:25:57 +00002131
2132 sourceStubLibraryModuleName(scope *apiScope, baseName string) string
Paul Duffin1b1e8062020-05-08 13:44:43 +01002133}
2134
2135type defaultNamingScheme struct {
2136}
2137
2138func (s *defaultNamingScheme) stubsLibraryModuleName(scope *apiScope, baseName string) string {
2139 return scope.stubsLibraryModuleName(baseName)
2140}
2141
2142func (s *defaultNamingScheme) stubsSourceModuleName(scope *apiScope, baseName string) string {
2143 return scope.stubsSourceModuleName(baseName)
2144}
2145
Jihoon Kang1c92c3e2023-03-23 17:44:51 +00002146func (s *defaultNamingScheme) apiLibraryModuleName(scope *apiScope, baseName string) string {
2147 return scope.apiLibraryModuleName(baseName)
2148}
2149
Jihoon Kang1147b312023-06-08 23:25:57 +00002150func (s *defaultNamingScheme) sourceStubLibraryModuleName(scope *apiScope, baseName string) string {
2151 return scope.sourceStubLibraryModuleName(baseName)
2152}
2153
Paul Duffin1b1e8062020-05-08 13:44:43 +01002154var _ sdkLibraryComponentNamingScheme = (*defaultNamingScheme)(nil)
2155
Jaewoong Jungbc15e3a2021-03-10 17:02:43 -08002156func moduleStubLinkType(name string) (stub bool, ret sdkLinkType) {
Jihoon Kang1147b312023-06-08 23:25:57 +00002157 name = strings.TrimSuffix(name, ".from-source")
2158
Anton Hansson2d0c1942020-05-25 12:20:51 +01002159 // This suffix-based approach is fragile and could potentially mis-trigger.
2160 // TODO(b/155164730): Clean this up when modules no longer reference sdk_lib stubs directly.
Anton Hansson08f476b2021-04-07 15:32:19 +01002161 if strings.HasSuffix(name, apiScopePublic.stubsLibraryModuleNameSuffix()) {
2162 if name == "hwbinder.stubs" || name == "libcore_private.stubs" {
2163 // Due to a previous bug, these modules were not considered stubs, so we retain that.
2164 return false, javaPlatform
2165 }
Anton Hansson2d0c1942020-05-25 12:20:51 +01002166 return true, javaSdk
2167 }
Anton Hansson08f476b2021-04-07 15:32:19 +01002168 if strings.HasSuffix(name, apiScopeSystem.stubsLibraryModuleNameSuffix()) {
Anton Hansson2d0c1942020-05-25 12:20:51 +01002169 return true, javaSystem
2170 }
Anton Hansson08f476b2021-04-07 15:32:19 +01002171 if strings.HasSuffix(name, apiScopeModuleLib.stubsLibraryModuleNameSuffix()) {
Anton Hansson2d0c1942020-05-25 12:20:51 +01002172 return true, javaModule
2173 }
Anton Hansson08f476b2021-04-07 15:32:19 +01002174 if strings.HasSuffix(name, apiScopeTest.stubsLibraryModuleNameSuffix()) {
Anton Hansson2d0c1942020-05-25 12:20:51 +01002175 return true, javaSystem
2176 }
Jihoon Kang1147b312023-06-08 23:25:57 +00002177 if strings.HasSuffix(name, apiScopeSystemServer.stubsLibraryModuleNameSuffix()) {
2178 return true, javaSystemServer
2179 }
Anton Hansson2d0c1942020-05-25 12:20:51 +01002180 return false, javaPlatform
2181}
2182
Jaewoong Jung4f158ee2019-07-11 10:05:35 -07002183// java_sdk_library is a special Java library that provides optional platform APIs to apps.
2184// In practice, it can be viewed as a combination of several modules: 1) stubs library that clients
2185// are linked against to, 2) droiddoc module that internally generates API stubs source files,
2186// 3) the real runtime shared library that implements the APIs, and 4) XML file for adding
2187// the runtime lib to the classpath at runtime if requested via <uses-library>.
Inseob Kimc0907f12019-02-08 21:00:45 +09002188func SdkLibraryFactory() android.Module {
2189 module := &SdkLibrary{}
Paul Duffinc3091c82020-05-08 14:16:20 +01002190
2191 // Initialize information common between source and prebuilt.
Paul Duffin71b33cc2021-06-23 11:39:47 +01002192 module.initCommon(module)
Paul Duffinc3091c82020-05-08 14:16:20 +01002193
Inseob Kimc0907f12019-02-08 21:00:45 +09002194 module.InitSdkLibraryProperties()
Jooyung Han58f26ab2019-12-18 15:34:32 +09002195 android.InitApexModule(module)
Sundong Ahn054b19a2018-10-19 13:46:09 +09002196 InitJavaModule(module, android.HostAndDeviceSupported)
Paul Duffin3375e352020-04-28 10:44:03 +01002197
2198 // Initialize the map from scope to scope specific properties.
2199 scopeToProperties := make(map[*apiScope]*ApiScopeProperties)
2200 for _, scope := range allApiScopes {
2201 scopeToProperties[scope] = scope.scopeSpecificProperties(module)
2202 }
2203 module.scopeToProperties = scopeToProperties
2204
Paul Duffin4911a892020-04-29 23:35:13 +01002205 // Add the properties containing visibility rules so that they are checked.
Paul Duffin5df79302020-05-16 15:52:12 +01002206 android.AddVisibilityProperty(module, "impl_library_visibility", &module.sdkLibraryProperties.Impl_library_visibility)
Paul Duffin4911a892020-04-29 23:35:13 +01002207 android.AddVisibilityProperty(module, "stubs_library_visibility", &module.sdkLibraryProperties.Stubs_library_visibility)
2208 android.AddVisibilityProperty(module, "stubs_source_visibility", &module.sdkLibraryProperties.Stubs_source_visibility)
2209
Paul Duffin1b1e8062020-05-08 13:44:43 +01002210 module.SetDefaultableHook(func(ctx android.DefaultableHookContext) {
Paul Duffindfa131e2020-05-15 20:37:11 +01002211 // If no implementation is required then it cannot be used as a shared library
2212 // either.
2213 if !module.requiresRuntimeImplementationLibrary() {
2214 // If shared_library has been explicitly set to true then it is incompatible
2215 // with api_only: true.
2216 if proptools.Bool(module.commonSdkLibraryProperties.Shared_library) {
2217 ctx.PropertyErrorf("api_only/shared_library", "inconsistent settings, shared_library and api_only cannot both be true")
2218 }
2219 // Set shared_library: false.
2220 module.commonSdkLibraryProperties.Shared_library = proptools.BoolPtr(false)
2221 }
2222
Paul Duffin1b1e8062020-05-08 13:44:43 +01002223 if module.initCommonAfterDefaultsApplied(ctx) {
2224 module.CreateInternalModules(ctx)
2225 }
2226 })
Zi Wangb2179e32023-01-31 15:53:30 -08002227 android.InitBazelModule(module)
Jiyong Parkc678ad32018-04-10 13:07:10 +09002228 return module
2229}
Colin Cross79c7c262019-04-17 11:11:46 -07002230
Zi Wangb2179e32023-01-31 15:53:30 -08002231type bazelSdkLibraryAttributes struct {
2232 Public bazel.StringAttribute
2233 System bazel.StringAttribute
2234 Test bazel.StringAttribute
2235 Module_lib bazel.StringAttribute
2236 System_server bazel.StringAttribute
2237}
2238
2239// java_sdk_library bp2build converter
2240func (module *SdkLibrary) ConvertWithBp2build(ctx android.TopDownMutatorContext) {
2241 if ctx.ModuleType() != "java_sdk_library" {
Chris Parsons39a16972023-06-08 14:28:51 +00002242 ctx.MarkBp2buildUnconvertible(bp2build_metrics_proto.UnconvertedReasonType_TYPE_UNSUPPORTED, "")
Zi Wangb2179e32023-01-31 15:53:30 -08002243 return
2244 }
2245
2246 nameToAttr := make(map[string]bazel.StringAttribute)
2247
2248 for _, scope := range module.getGeneratedApiScopes(ctx) {
2249 apiSurfaceFile := path.Join(module.getApiDir(), scope.apiFilePrefix+"current.txt")
2250 var scopeStringAttribute bazel.StringAttribute
2251 scopeStringAttribute.SetValue(apiSurfaceFile)
2252 nameToAttr[scope.name] = scopeStringAttribute
2253 }
2254
2255 attrs := bazelSdkLibraryAttributes{
2256 Public: nameToAttr["public"],
2257 System: nameToAttr["system"],
2258 Test: nameToAttr["test"],
2259 Module_lib: nameToAttr["module-lib"],
2260 System_server: nameToAttr["system-server"],
2261 }
2262 props := bazel.BazelTargetModuleProperties{
2263 Rule_class: "java_sdk_library",
2264 Bzl_load_location: "//build/bazel/rules/java:sdk_library.bzl",
2265 }
2266
2267 ctx.CreateBazelTargetModule(props, android.CommonAttributes{Name: module.Name()}, &attrs)
2268}
2269
Colin Cross79c7c262019-04-17 11:11:46 -07002270//
2271// SDK library prebuilts
2272//
2273
Paul Duffin56d44902020-01-31 13:36:25 +00002274// Properties associated with each api scope.
2275type sdkLibraryScopeProperties struct {
Colin Cross79c7c262019-04-17 11:11:46 -07002276 Jars []string `android:"path"`
2277
2278 Sdk_version *string
2279
Colin Cross79c7c262019-04-17 11:11:46 -07002280 // List of shared java libs that this module has dependencies to
2281 Libs []string
Paul Duffin3d1248c2020-04-09 00:10:17 +01002282
Paul Duffinc8782502020-04-29 20:45:27 +01002283 // The stubs source.
Paul Duffin3d1248c2020-04-09 00:10:17 +01002284 Stub_srcs []string `android:"path"`
Paul Duffin1fd005d2020-04-09 01:08:11 +01002285
2286 // The current.txt
Paul Duffin0f8faff2020-05-20 16:18:00 +01002287 Current_api *string `android:"path"`
Paul Duffin1fd005d2020-04-09 01:08:11 +01002288
2289 // The removed.txt
Paul Duffin0f8faff2020-05-20 16:18:00 +01002290 Removed_api *string `android:"path"`
Anton Hanssond78eb762021-09-21 15:25:12 +01002291
2292 // Annotation zip
2293 Annotations *string `android:"path"`
Colin Cross79c7c262019-04-17 11:11:46 -07002294}
2295
Paul Duffin56d44902020-01-31 13:36:25 +00002296type sdkLibraryImportProperties struct {
Paul Duffinfcfd7912020-01-31 17:54:30 +00002297 // List of shared java libs, common to all scopes, that this module has
2298 // dependencies to
2299 Libs []string
Paul Duffin1267d872021-04-16 17:21:36 +01002300
2301 // If set to true, compile dex files for the stubs. Defaults to false.
2302 Compile_dex *bool
Paul Duffin869de142021-07-15 14:14:41 +01002303
2304 // If not empty, classes are restricted to the specified packages and their sub-packages.
Paul Duffin869de142021-07-15 14:14:41 +01002305 Permitted_packages []string
Paul Duffin56d44902020-01-31 13:36:25 +00002306}
2307
Paul Duffineedc5d52020-06-12 17:46:39 +01002308type SdkLibraryImport struct {
Colin Cross79c7c262019-04-17 11:11:46 -07002309 android.ModuleBase
2310 android.DefaultableModuleBase
2311 prebuilt android.Prebuilt
Paul Duffindd46f712020-02-10 13:37:10 +00002312 android.ApexModuleBase
Colin Cross79c7c262019-04-17 11:11:46 -07002313
Paul Duffin37856732021-02-26 14:24:15 +00002314 hiddenAPI
Jiakai Zhang204356f2021-09-09 08:12:46 +00002315 dexpreopter
Paul Duffin37856732021-02-26 14:24:15 +00002316
Colin Cross79c7c262019-04-17 11:11:46 -07002317 properties sdkLibraryImportProperties
2318
Paul Duffin46a26a82020-04-07 19:27:04 +01002319 // Map from api scope to the scope specific property structure.
2320 scopeProperties map[*apiScope]*sdkLibraryScopeProperties
2321
Paul Duffin56d44902020-01-31 13:36:25 +00002322 commonToSdkLibraryAndImport
Paul Duffineedc5d52020-06-12 17:46:39 +01002323
2324 // The reference to the implementation library created by the source module.
2325 // Is nil if the source module does not exist.
2326 implLibraryModule *Library
2327
2328 // The reference to the xml permissions module created by the source module.
2329 // Is nil if the source module does not exist.
2330 xmlPermissionsFileModule *sdkLibraryXml
Paul Duffin39853512021-02-26 11:09:39 +00002331
Jeongik Chad5fe8782021-07-08 01:13:11 +09002332 // Build path to the dex implementation jar obtained from the prebuilt_apex, if any.
Martin Stjernholm8be1e6d2021-09-15 03:34:04 +01002333 dexJarFile OptionalDexJarPath
Jeongik Chad5fe8782021-07-08 01:13:11 +09002334
2335 // Expected install file path of the source module(sdk_library)
2336 // or dex implementation jar obtained from the prebuilt_apex, if any.
2337 installFile android.Path
Colin Cross79c7c262019-04-17 11:11:46 -07002338}
2339
Paul Duffineedc5d52020-06-12 17:46:39 +01002340var _ SdkLibraryDependency = (*SdkLibraryImport)(nil)
Colin Cross79c7c262019-04-17 11:11:46 -07002341
Paul Duffin46a26a82020-04-07 19:27:04 +01002342// The type of a structure that contains a field of type sdkLibraryScopeProperties
2343// for each apiscope in allApiScopes, e.g. something like:
Colin Crossd079e0b2022-08-16 10:27:33 -07002344//
2345// struct {
2346// Public sdkLibraryScopeProperties
2347// System sdkLibraryScopeProperties
2348// ...
2349// }
Paul Duffin46a26a82020-04-07 19:27:04 +01002350var allScopeStructType = createAllScopePropertiesStructType()
2351
2352// Dynamically create a structure type for each apiscope in allApiScopes.
2353func createAllScopePropertiesStructType() reflect.Type {
2354 var fields []reflect.StructField
2355 for _, apiScope := range allApiScopes {
2356 field := reflect.StructField{
2357 Name: apiScope.fieldName,
2358 Type: reflect.TypeOf(sdkLibraryScopeProperties{}),
2359 }
2360 fields = append(fields, field)
2361 }
2362
2363 return reflect.StructOf(fields)
2364}
2365
2366// Create an instance of the scope specific structure type and return a map
2367// from apiscope to a pointer to each scope specific field.
2368func createPropertiesInstance() (interface{}, map[*apiScope]*sdkLibraryScopeProperties) {
2369 allScopePropertiesPtr := reflect.New(allScopeStructType)
2370 allScopePropertiesStruct := allScopePropertiesPtr.Elem()
2371 scopeProperties := make(map[*apiScope]*sdkLibraryScopeProperties)
2372
2373 for _, apiScope := range allApiScopes {
2374 field := allScopePropertiesStruct.FieldByName(apiScope.fieldName)
2375 scopeProperties[apiScope] = field.Addr().Interface().(*sdkLibraryScopeProperties)
2376 }
2377
2378 return allScopePropertiesPtr.Interface(), scopeProperties
2379}
2380
Jaewoong Jung4f158ee2019-07-11 10:05:35 -07002381// java_sdk_library_import imports a prebuilt java_sdk_library.
Colin Cross79c7c262019-04-17 11:11:46 -07002382func sdkLibraryImportFactory() android.Module {
Paul Duffineedc5d52020-06-12 17:46:39 +01002383 module := &SdkLibraryImport{}
Colin Cross79c7c262019-04-17 11:11:46 -07002384
Paul Duffin46a26a82020-04-07 19:27:04 +01002385 allScopeProperties, scopeToProperties := createPropertiesInstance()
2386 module.scopeProperties = scopeToProperties
Jiakai Zhang9c4dc192023-02-09 00:09:24 +08002387 module.AddProperties(&module.properties, allScopeProperties, &module.importDexpreoptProperties)
Colin Cross79c7c262019-04-17 11:11:46 -07002388
Paul Duffinc3091c82020-05-08 14:16:20 +01002389 // Initialize information common between source and prebuilt.
Paul Duffin71b33cc2021-06-23 11:39:47 +01002390 module.initCommon(module)
Paul Duffinc3091c82020-05-08 14:16:20 +01002391
Paul Duffin0bdcb272020-02-06 15:24:57 +00002392 android.InitPrebuiltModule(module, &[]string{""})
Paul Duffindd46f712020-02-10 13:37:10 +00002393 android.InitApexModule(module)
Colin Cross79c7c262019-04-17 11:11:46 -07002394 InitJavaModule(module, android.HostAndDeviceSupported)
2395
Paul Duffin1b1e8062020-05-08 13:44:43 +01002396 module.SetDefaultableHook(func(mctx android.DefaultableHookContext) {
2397 if module.initCommonAfterDefaultsApplied(mctx) {
2398 module.createInternalModules(mctx)
2399 }
2400 })
Colin Cross79c7c262019-04-17 11:11:46 -07002401 return module
2402}
2403
Paul Duffin630b11e2021-07-15 13:35:26 +01002404var _ PermittedPackagesForUpdatableBootJars = (*SdkLibraryImport)(nil)
2405
2406func (module *SdkLibraryImport) PermittedPackagesForUpdatableBootJars() []string {
2407 return module.properties.Permitted_packages
2408}
2409
Paul Duffineedc5d52020-06-12 17:46:39 +01002410func (module *SdkLibraryImport) Prebuilt() *android.Prebuilt {
Colin Cross79c7c262019-04-17 11:11:46 -07002411 return &module.prebuilt
2412}
2413
Paul Duffineedc5d52020-06-12 17:46:39 +01002414func (module *SdkLibraryImport) Name() string {
Colin Cross79c7c262019-04-17 11:11:46 -07002415 return module.prebuilt.Name(module.ModuleBase.Name())
2416}
2417
Paul Duffineedc5d52020-06-12 17:46:39 +01002418func (module *SdkLibraryImport) createInternalModules(mctx android.DefaultableHookContext) {
Colin Cross79c7c262019-04-17 11:11:46 -07002419
Paul Duffin50061512020-01-21 16:31:05 +00002420 // If the build is configured to use prebuilts then force this to be preferred.
Jeongik Cha816a23a2020-07-08 01:09:23 +09002421 if mctx.Config().AlwaysUsePrebuiltSdks() {
Paul Duffin50061512020-01-21 16:31:05 +00002422 module.prebuilt.ForcePrefer()
2423 }
2424
Paul Duffin46a26a82020-04-07 19:27:04 +01002425 for apiScope, scopeProperties := range module.scopeProperties {
Paul Duffin56d44902020-01-31 13:36:25 +00002426 if len(scopeProperties.Jars) == 0 {
2427 continue
2428 }
2429
Paul Duffinbbb546b2020-04-09 00:07:11 +01002430 module.createJavaImportForStubs(mctx, apiScope, scopeProperties)
Paul Duffin3d1248c2020-04-09 00:10:17 +01002431
Paul Duffin0f8faff2020-05-20 16:18:00 +01002432 if len(scopeProperties.Stub_srcs) > 0 {
2433 module.createPrebuiltStubsSources(mctx, apiScope, scopeProperties)
2434 }
Paul Duffin56d44902020-01-31 13:36:25 +00002435 }
Colin Cross79c7c262019-04-17 11:11:46 -07002436
2437 javaSdkLibraries := javaSdkLibraries(mctx.Config())
2438 javaSdkLibrariesLock.Lock()
2439 defer javaSdkLibrariesLock.Unlock()
2440 *javaSdkLibraries = append(*javaSdkLibraries, module.BaseModuleName())
2441}
2442
Paul Duffineedc5d52020-06-12 17:46:39 +01002443func (module *SdkLibraryImport) createJavaImportForStubs(mctx android.DefaultableHookContext, apiScope *apiScope, scopeProperties *sdkLibraryScopeProperties) {
Paul Duffinbbb546b2020-04-09 00:07:11 +01002444 // Creates a java import for the jar with ".stubs" suffix
2445 props := struct {
Paul Duffin1dbe3ca2020-05-16 09:57:59 +01002446 Name *string
2447 Sdk_version *string
2448 Libs []string
2449 Jars []string
Paul Duffin1267d872021-04-16 17:21:36 +01002450 Compile_dex *bool
Paul Duffinbf4de042022-09-27 12:41:52 +01002451
2452 android.UserSuppliedPrebuiltProperties
Paul Duffinbbb546b2020-04-09 00:07:11 +01002453 }{}
Paul Duffinc3091c82020-05-08 14:16:20 +01002454 props.Name = proptools.StringPtr(module.stubsLibraryModuleName(apiScope))
Paul Duffinbbb546b2020-04-09 00:07:11 +01002455 props.Sdk_version = scopeProperties.Sdk_version
2456 // Prepend any of the libs from the legacy public properties to the libs for each of the
2457 // scopes to avoid having to duplicate them in each scope.
2458 props.Libs = append(module.properties.Libs, scopeProperties.Libs...)
2459 props.Jars = scopeProperties.Jars
Paul Duffin1dbe3ca2020-05-16 09:57:59 +01002460
Paul Duffin38b57852020-05-13 16:08:09 +01002461 // The imports are preferred if the java_sdk_library_import is preferred.
Paul Duffinbf4de042022-09-27 12:41:52 +01002462 props.CopyUserSuppliedPropertiesFromPrebuilt(&module.prebuilt)
Paul Duffin859fe962020-05-15 10:20:31 +01002463
Paul Duffin1267d872021-04-16 17:21:36 +01002464 // The imports need to be compiled to dex if the java_sdk_library_import requests it.
Paul Duffinf4600f62021-05-13 22:34:45 +01002465 compileDex := module.properties.Compile_dex
2466 if module.stubLibrariesCompiledForDex() {
2467 compileDex = proptools.BoolPtr(true)
2468 }
2469 props.Compile_dex = compileDex
Paul Duffin1267d872021-04-16 17:21:36 +01002470
Paul Duffin859fe962020-05-15 10:20:31 +01002471 mctx.CreateModule(ImportFactory, &props, module.sdkComponentPropertiesForChildLibrary())
Paul Duffinbbb546b2020-04-09 00:07:11 +01002472}
2473
Paul Duffineedc5d52020-06-12 17:46:39 +01002474func (module *SdkLibraryImport) createPrebuiltStubsSources(mctx android.DefaultableHookContext, apiScope *apiScope, scopeProperties *sdkLibraryScopeProperties) {
Paul Duffin3d1248c2020-04-09 00:10:17 +01002475 props := struct {
Paul Duffinbf4de042022-09-27 12:41:52 +01002476 Name *string
2477 Srcs []string
2478
2479 android.UserSuppliedPrebuiltProperties
Paul Duffin3d1248c2020-04-09 00:10:17 +01002480 }{}
Paul Duffinc3091c82020-05-08 14:16:20 +01002481 props.Name = proptools.StringPtr(module.stubsSourceModuleName(apiScope))
Paul Duffin3d1248c2020-04-09 00:10:17 +01002482 props.Srcs = scopeProperties.Stub_srcs
Paul Duffin38b57852020-05-13 16:08:09 +01002483
2484 // The stubs source is preferred if the java_sdk_library_import is preferred.
Paul Duffinbf4de042022-09-27 12:41:52 +01002485 props.CopyUserSuppliedPropertiesFromPrebuilt(&module.prebuilt)
2486
2487 mctx.CreateModule(PrebuiltStubsSourcesFactory, &props)
Paul Duffin3d1248c2020-04-09 00:10:17 +01002488}
2489
Paul Duffin44f1d842020-06-26 20:17:02 +01002490// Add the dependencies on the child module in the component deps mutator so that it
2491// creates references to the prebuilt and not the source modules.
2492func (module *SdkLibraryImport) ComponentDepsMutator(ctx android.BottomUpMutatorContext) {
Paul Duffin46a26a82020-04-07 19:27:04 +01002493 for apiScope, scopeProperties := range module.scopeProperties {
Paul Duffin56d44902020-01-31 13:36:25 +00002494 if len(scopeProperties.Jars) == 0 {
2495 continue
2496 }
2497
2498 // Add dependencies to the prebuilt stubs library
Paul Duffin864116c2021-04-02 10:24:13 +01002499 ctx.AddVariationDependencies(nil, apiScope.stubsTag, android.PrebuiltNameFromSource(module.stubsLibraryModuleName(apiScope)))
Paul Duffin0f8faff2020-05-20 16:18:00 +01002500
2501 if len(scopeProperties.Stub_srcs) > 0 {
2502 // Add dependencies to the prebuilt stubs source library
Paul Duffin864116c2021-04-02 10:24:13 +01002503 ctx.AddVariationDependencies(nil, apiScope.stubsSourceTag, android.PrebuiltNameFromSource(module.stubsSourceModuleName(apiScope)))
Paul Duffin0f8faff2020-05-20 16:18:00 +01002504 }
Paul Duffin56d44902020-01-31 13:36:25 +00002505 }
Paul Duffin44f1d842020-06-26 20:17:02 +01002506}
2507
2508// Add other dependencies as normal.
2509func (module *SdkLibraryImport) DepsMutator(ctx android.BottomUpMutatorContext) {
Paul Duffineedc5d52020-06-12 17:46:39 +01002510
2511 implName := module.implLibraryModuleName()
2512 if ctx.OtherModuleExists(implName) {
2513 ctx.AddVariationDependencies(nil, implLibraryTag, implName)
2514
2515 xmlPermissionsModuleName := module.xmlPermissionsModuleName()
2516 if module.sharedLibrary() && ctx.OtherModuleExists(xmlPermissionsModuleName) {
2517 // Add dependency to the rule for generating the xml permissions file
2518 ctx.AddDependency(module, xmlPermissionsFileTag, xmlPermissionsModuleName)
2519 }
2520 }
Colin Cross79c7c262019-04-17 11:11:46 -07002521}
2522
Jiakai Zhang204356f2021-09-09 08:12:46 +00002523func (module *SdkLibraryImport) AndroidMkEntries() []android.AndroidMkEntries {
2524 // For an SDK library imported from a prebuilt APEX, we don't need a Make module for itself, as we
2525 // don't need to install it. However, we need to add its dexpreopt outputs as sub-modules, if it
2526 // is preopted.
2527 dexpreoptEntries := module.dexpreopter.AndroidMkEntriesForApex()
2528 return append(dexpreoptEntries, android.AndroidMkEntries{Disabled: true})
2529}
2530
Jiyong Park45bf82e2020-12-15 22:29:02 +09002531var _ android.ApexModule = (*SdkLibraryImport)(nil)
2532
2533// Implements android.ApexModule
Paul Duffineedc5d52020-06-12 17:46:39 +01002534func (module *SdkLibraryImport) DepIsInSameApex(mctx android.BaseModuleContext, dep android.Module) bool {
2535 depTag := mctx.OtherModuleDependencyTag(dep)
2536 if depTag == xmlPermissionsFileTag {
2537 return true
2538 }
2539
2540 // None of the other dependencies of the java_sdk_library_import are in the same apex
2541 // as the one that references this module.
2542 return false
2543}
2544
Jiyong Park45bf82e2020-12-15 22:29:02 +09002545// Implements android.ApexModule
Dan Albertc8060532020-07-22 22:32:17 -07002546func (module *SdkLibraryImport) ShouldSupportSdkVersion(ctx android.BaseModuleContext,
2547 sdkVersion android.ApiLevel) error {
Jooyung Han749dc692020-04-15 11:03:39 +09002548 // we don't check prebuilt modules for sdk_version
2549 return nil
2550}
2551
Paul Duffinea8f8082021-06-24 13:25:57 +01002552// Implements android.ApexModule
2553func (module *SdkLibraryImport) UniqueApexVariations() bool {
2554 return module.uniqueApexVariations()
2555}
2556
Paul Duffin09817d62022-04-28 17:45:11 +01002557// MinSdkVersion - Implements hiddenAPIModule
Spandan Das8c9ae7e2023-03-03 21:20:36 +00002558func (module *SdkLibraryImport) MinSdkVersion(ctx android.EarlyModuleContext) android.ApiLevel {
2559 return android.NoneApiLevel
Paul Duffin09817d62022-04-28 17:45:11 +01002560}
2561
2562var _ hiddenAPIModule = (*SdkLibraryImport)(nil)
2563
Paul Duffineedc5d52020-06-12 17:46:39 +01002564func (module *SdkLibraryImport) OutputFiles(tag string) (android.Paths, error) {
Paul Duffin1e940d52022-04-29 14:21:25 +01002565 paths, err := module.commonOutputFiles(tag)
2566 if paths != nil || err != nil {
2567 return paths, err
2568 }
2569 if module.implLibraryModule != nil {
2570 return module.implLibraryModule.OutputFiles(tag)
2571 } else {
2572 return nil, nil
2573 }
Paul Duffin46dc45a2020-05-14 15:39:10 +01002574}
2575
Paul Duffineedc5d52020-06-12 17:46:39 +01002576func (module *SdkLibraryImport) GenerateAndroidBuildActions(ctx android.ModuleContext) {
Paul Duffina2ae7e02020-09-11 11:55:00 +01002577 module.generateCommonBuildActions(ctx)
2578
Jeongik Chad5fe8782021-07-08 01:13:11 +09002579 // Assume that source module(sdk_library) is installed in /<sdk_library partition>/framework
2580 module.installFile = android.PathForModuleInstall(ctx, "framework", module.Stem()+".jar")
2581
Paul Duffin0f8faff2020-05-20 16:18:00 +01002582 // Record the paths to the prebuilt stubs library and stubs source.
Colin Cross79c7c262019-04-17 11:11:46 -07002583 ctx.VisitDirectDeps(func(to android.Module) {
2584 tag := ctx.OtherModuleDependencyTag(to)
2585
Paul Duffin0f8faff2020-05-20 16:18:00 +01002586 // Extract information from any of the scope specific dependencies.
2587 if scopeTag, ok := tag.(scopeDependencyTag); ok {
2588 apiScope := scopeTag.apiScope
2589 scopePaths := module.getScopePathsCreateIfNeeded(apiScope)
2590
2591 // Extract information from the dependency. The exact information extracted
2592 // is determined by the nature of the dependency which is determined by the tag.
2593 scopeTag.extractDepInfo(ctx, to, scopePaths)
Paul Duffineedc5d52020-06-12 17:46:39 +01002594 } else if tag == implLibraryTag {
2595 if implLibrary, ok := to.(*Library); ok {
2596 module.implLibraryModule = implLibrary
2597 } else {
2598 ctx.ModuleErrorf("implementation library must be of type *java.Library but was %T", to)
2599 }
2600 } else if tag == xmlPermissionsFileTag {
2601 if xmlPermissionsFileModule, ok := to.(*sdkLibraryXml); ok {
2602 module.xmlPermissionsFileModule = xmlPermissionsFileModule
2603 } else {
2604 ctx.ModuleErrorf("xml permissions file module must be of type *sdkLibraryXml but was %T", to)
2605 }
Colin Cross79c7c262019-04-17 11:11:46 -07002606 }
2607 })
Paul Duffin0f8faff2020-05-20 16:18:00 +01002608
2609 // Populate the scope paths with information from the properties.
2610 for apiScope, scopeProperties := range module.scopeProperties {
2611 if len(scopeProperties.Jars) == 0 {
2612 continue
2613 }
2614
2615 paths := module.getScopePathsCreateIfNeeded(apiScope)
Anton Hanssond78eb762021-09-21 15:25:12 +01002616 paths.annotationsZip = android.OptionalPathForModuleSrc(ctx, scopeProperties.Annotations)
Paul Duffin0f8faff2020-05-20 16:18:00 +01002617 paths.currentApiFilePath = android.OptionalPathForModuleSrc(ctx, scopeProperties.Current_api)
2618 paths.removedApiFilePath = android.OptionalPathForModuleSrc(ctx, scopeProperties.Removed_api)
2619 }
Paul Duffin39853512021-02-26 11:09:39 +00002620
2621 if ctx.Device() {
2622 // If this is a variant created for a prebuilt_apex then use the dex implementation jar
2623 // obtained from the associated deapexer module.
2624 ai := ctx.Provider(android.ApexInfoProvider).(android.ApexInfo)
2625 if ai.ForPrebuiltApex {
Paul Duffin39853512021-02-26 11:09:39 +00002626 // Get the path of the dex implementation jar from the `deapexer` module.
Martin Stjernholm44825602021-09-17 01:44:12 +01002627 di := android.FindDeapexerProviderForModule(ctx)
2628 if di == nil {
2629 return // An error has been reported by FindDeapexerProviderForModule.
2630 }
Jiakai Zhang81e46812023-02-08 21:56:07 +08002631 dexJarFileApexRootRelative := apexRootRelativePathToJavaLib(module.BaseModuleName())
2632 if dexOutputPath := di.PrebuiltExportPath(dexJarFileApexRootRelative); dexOutputPath != nil {
Martin Stjernholm8be1e6d2021-09-15 03:34:04 +01002633 dexJarFile := makeDexJarPathFromPath(dexOutputPath)
2634 module.dexJarFile = dexJarFile
Jiakai Zhang204356f2021-09-09 08:12:46 +00002635 installPath := android.PathForModuleInPartitionInstall(
Jiakai Zhang81e46812023-02-08 21:56:07 +08002636 ctx, "apex", ai.ApexVariationName, dexJarFileApexRootRelative)
Jiakai Zhang204356f2021-09-09 08:12:46 +00002637 module.installFile = installPath
Martin Stjernholm8be1e6d2021-09-15 03:34:04 +01002638 module.initHiddenAPI(ctx, dexJarFile, module.findScopePaths(apiScopePublic).stubsImplPath[0], nil)
Jiakai Zhang204356f2021-09-09 08:12:46 +00002639
Jiakai Zhang204356f2021-09-09 08:12:46 +00002640 module.dexpreopter.installPath = module.dexpreopter.getInstallPath(ctx, installPath)
2641 module.dexpreopter.isSDKLibrary = true
2642 module.dexpreopter.uncompressedDex = shouldUncompressDex(ctx, &module.dexpreopter)
Jiakai Zhang81e46812023-02-08 21:56:07 +08002643
2644 if profilePath := di.PrebuiltExportPath(dexJarFileApexRootRelative + ".prof"); profilePath != nil {
2645 module.dexpreopter.inputProfilePathOnHost = profilePath
2646 }
2647
2648 // Dexpreopting.
Jiakai Zhang204356f2021-09-09 08:12:46 +00002649 module.dexpreopt(ctx, dexOutputPath)
Paul Duffin39853512021-02-26 11:09:39 +00002650 } else {
2651 // This should never happen as a variant for a prebuilt_apex is only created if the
2652 // prebuilt_apex has been configured to export the java library dex file.
Martin Stjernholm44825602021-09-17 01:44:12 +01002653 ctx.ModuleErrorf("internal error: no dex implementation jar available from prebuilt APEX %s", di.ApexModuleName())
Paul Duffin39853512021-02-26 11:09:39 +00002654 }
2655 }
2656 }
Colin Cross79c7c262019-04-17 11:11:46 -07002657}
2658
Jiyong Parkf1691d22021-03-29 20:11:58 +09002659func (module *SdkLibraryImport) sdkJars(ctx android.BaseModuleContext, sdkVersion android.SdkSpec, headerJars bool) android.Paths {
Paul Duffineedc5d52020-06-12 17:46:39 +01002660
2661 // For consistency with SdkLibrary make the implementation jar available to libraries that
2662 // are within the same APEX.
2663 implLibraryModule := module.implLibraryModule
Colin Cross56a83212020-09-15 18:30:11 -07002664 if implLibraryModule != nil && withinSameApexesAs(ctx, module) {
Paul Duffineedc5d52020-06-12 17:46:39 +01002665 if headerJars {
2666 return implLibraryModule.HeaderJars()
2667 } else {
2668 return implLibraryModule.ImplementationJars()
2669 }
2670 }
2671
Paul Duffin23970f42020-05-20 14:20:02 +01002672 return module.selectHeaderJarsForSdkVersion(ctx, sdkVersion)
Paul Duffin56d44902020-01-31 13:36:25 +00002673}
2674
Colin Cross79c7c262019-04-17 11:11:46 -07002675// to satisfy SdkLibraryDependency interface
Jiyong Parkf1691d22021-03-29 20:11:58 +09002676func (module *SdkLibraryImport) SdkHeaderJars(ctx android.BaseModuleContext, sdkVersion android.SdkSpec) android.Paths {
Colin Cross79c7c262019-04-17 11:11:46 -07002677 // This module is just a wrapper for the prebuilt stubs.
Paul Duffineedc5d52020-06-12 17:46:39 +01002678 return module.sdkJars(ctx, sdkVersion, true)
Colin Cross79c7c262019-04-17 11:11:46 -07002679}
2680
2681// to satisfy SdkLibraryDependency interface
Jiyong Parkf1691d22021-03-29 20:11:58 +09002682func (module *SdkLibraryImport) SdkImplementationJars(ctx android.BaseModuleContext, sdkVersion android.SdkSpec) android.Paths {
Colin Cross79c7c262019-04-17 11:11:46 -07002683 // This module is just a wrapper for the stubs.
Paul Duffineedc5d52020-06-12 17:46:39 +01002684 return module.sdkJars(ctx, sdkVersion, false)
2685}
2686
Ulya Trafimovichdbf31662020-12-17 12:07:54 +00002687// to satisfy UsesLibraryDependency interface
Martin Stjernholm8be1e6d2021-09-15 03:34:04 +01002688func (module *SdkLibraryImport) DexJarBuildPath() OptionalDexJarPath {
Paul Duffin39853512021-02-26 11:09:39 +00002689 // The dex implementation jar extracted from the .apex file should be used in preference to the
2690 // source.
Martin Stjernholm8be1e6d2021-09-15 03:34:04 +01002691 if module.dexJarFile.IsSet() {
Paul Duffin39853512021-02-26 11:09:39 +00002692 return module.dexJarFile
2693 }
Paul Duffineedc5d52020-06-12 17:46:39 +01002694 if module.implLibraryModule == nil {
Martin Stjernholm8be1e6d2021-09-15 03:34:04 +01002695 return makeUnsetDexJarPath()
Paul Duffineedc5d52020-06-12 17:46:39 +01002696 } else {
2697 return module.implLibraryModule.DexJarBuildPath()
2698 }
2699}
2700
Ulya Trafimovichdbf31662020-12-17 12:07:54 +00002701// to satisfy UsesLibraryDependency interface
Ulya Trafimovich31e444e2020-08-14 17:32:16 +01002702func (module *SdkLibraryImport) DexJarInstallPath() android.Path {
Jeongik Chad5fe8782021-07-08 01:13:11 +09002703 return module.installFile
Ulya Trafimovich31e444e2020-08-14 17:32:16 +01002704}
2705
Ulya Trafimovichdbf31662020-12-17 12:07:54 +00002706// to satisfy UsesLibraryDependency interface
2707func (module *SdkLibraryImport) ClassLoaderContexts() dexpreopt.ClassLoaderContextMap {
2708 return nil
2709}
2710
Paul Duffineedc5d52020-06-12 17:46:39 +01002711// to satisfy apex.javaDependency interface
2712func (module *SdkLibraryImport) JacocoReportClassesFile() android.Path {
2713 if module.implLibraryModule == nil {
2714 return nil
2715 } else {
2716 return module.implLibraryModule.JacocoReportClassesFile()
2717 }
2718}
2719
2720// to satisfy apex.javaDependency interface
Colin Cross08dca382020-07-21 20:31:17 -07002721func (module *SdkLibraryImport) LintDepSets() LintDepSets {
2722 if module.implLibraryModule == nil {
2723 return LintDepSets{}
2724 } else {
2725 return module.implLibraryModule.LintDepSets()
2726 }
2727}
2728
Spandan Das17854f52022-01-14 21:19:14 +00002729func (module *SdkLibraryImport) GetStrictUpdatabilityLinting() bool {
Jaewoong Jung476b9d62021-05-10 15:30:00 -07002730 if module.implLibraryModule == nil {
2731 return false
2732 } else {
Spandan Das17854f52022-01-14 21:19:14 +00002733 return module.implLibraryModule.GetStrictUpdatabilityLinting()
Jaewoong Jung476b9d62021-05-10 15:30:00 -07002734 }
2735}
2736
Spandan Das17854f52022-01-14 21:19:14 +00002737func (module *SdkLibraryImport) SetStrictUpdatabilityLinting(strictLinting bool) {
Jaewoong Jung476b9d62021-05-10 15:30:00 -07002738 if module.implLibraryModule != nil {
Spandan Das17854f52022-01-14 21:19:14 +00002739 module.implLibraryModule.SetStrictUpdatabilityLinting(strictLinting)
Jaewoong Jung476b9d62021-05-10 15:30:00 -07002740 }
2741}
2742
Colin Cross08dca382020-07-21 20:31:17 -07002743// to satisfy apex.javaDependency interface
Paul Duffineedc5d52020-06-12 17:46:39 +01002744func (module *SdkLibraryImport) Stem() string {
2745 return module.BaseModuleName()
Colin Cross79c7c262019-04-17 11:11:46 -07002746}
Jiyong Parke3833882020-02-17 17:28:10 +09002747
Paul Duffin44b481b2020-06-17 16:59:43 +01002748var _ ApexDependency = (*SdkLibraryImport)(nil)
2749
2750// to satisfy java.ApexDependency interface
2751func (module *SdkLibraryImport) HeaderJars() android.Paths {
2752 if module.implLibraryModule == nil {
2753 return nil
2754 } else {
2755 return module.implLibraryModule.HeaderJars()
2756 }
2757}
2758
2759// to satisfy java.ApexDependency interface
2760func (module *SdkLibraryImport) ImplementationAndResourcesJars() android.Paths {
2761 if module.implLibraryModule == nil {
2762 return nil
2763 } else {
2764 return module.implLibraryModule.ImplementationAndResourcesJars()
2765 }
2766}
2767
Jiakai Zhang204356f2021-09-09 08:12:46 +00002768// to satisfy java.DexpreopterInterface interface
2769func (module *SdkLibraryImport) IsInstallable() bool {
2770 return true
2771}
2772
Paul Duffinfef55002021-06-17 14:56:05 +01002773var _ android.RequiredFilesFromPrebuiltApex = (*SdkLibraryImport)(nil)
2774
Paul Duffinb4bbf2c2021-06-17 15:59:07 +01002775func (module *SdkLibraryImport) RequiredFilesFromPrebuiltApex(ctx android.BaseModuleContext) []string {
Paul Duffinfef55002021-06-17 14:56:05 +01002776 name := module.BaseModuleName()
Jiakai Zhang81e46812023-02-08 21:56:07 +08002777 return requiredFilesFromPrebuiltApexForImport(name, &module.dexpreopter)
Paul Duffinfef55002021-06-17 14:56:05 +01002778}
2779
Jiyong Parke3833882020-02-17 17:28:10 +09002780// java_sdk_library_xml
Jiyong Parke3833882020-02-17 17:28:10 +09002781type sdkLibraryXml struct {
2782 android.ModuleBase
2783 android.DefaultableModuleBase
2784 android.ApexModuleBase
2785
2786 properties sdkLibraryXmlProperties
2787
2788 outputFilePath android.OutputPath
2789 installDirPath android.InstallPath
Colin Cross56a83212020-09-15 18:30:11 -07002790
2791 hideApexVariantFromMake bool
Jiyong Parke3833882020-02-17 17:28:10 +09002792}
2793
2794type sdkLibraryXmlProperties struct {
2795 // canonical name of the lib
2796 Lib_name *string
Pedro Loureiro9956e5e2021-09-07 17:21:59 +00002797
2798 // Signals that this shared library is part of the bootclasspath starting
2799 // on the version indicated in this attribute.
2800 //
2801 // This will make platforms at this level and above to ignore
2802 // <uses-library> tags with this library name because the library is already
2803 // available
2804 On_bootclasspath_since *string
2805
2806 // Signals that this shared library was part of the bootclasspath before
2807 // (but not including) the version indicated in this attribute.
2808 //
2809 // The system will automatically add a <uses-library> tag with this library to
2810 // apps that target any SDK less than the version indicated in this attribute.
2811 On_bootclasspath_before *string
2812
2813 // Indicates that PackageManager should ignore this shared library if the
2814 // platform is below the version indicated in this attribute.
2815 //
2816 // This means that the device won't recognise this library as installed.
2817 Min_device_sdk *string
2818
2819 // Indicates that PackageManager should ignore this shared library if the
2820 // platform is above the version indicated in this attribute.
2821 //
2822 // This means that the device won't recognise this library as installed.
2823 Max_device_sdk *string
Pedro Loureiroc3621422021-09-28 15:40:23 +00002824
2825 // The SdkLibrary's min api level as a string
2826 //
2827 // This value comes from the ApiLevel of the MinSdkVersion property.
2828 Sdk_library_min_api_level *string
Jiyong Parke3833882020-02-17 17:28:10 +09002829}
2830
2831// java_sdk_library_xml builds the permission xml file for a java_sdk_library.
2832// Not to be used directly by users. java_sdk_library internally uses this.
2833func sdkLibraryXmlFactory() android.Module {
2834 module := &sdkLibraryXml{}
2835
2836 module.AddProperties(&module.properties)
2837
2838 android.InitApexModule(module)
2839 android.InitAndroidArchModule(module, android.DeviceSupported, android.MultilibCommon)
2840
2841 return module
2842}
2843
Colin Crossaede88c2020-08-11 12:17:01 -07002844func (module *sdkLibraryXml) UniqueApexVariations() bool {
2845 // sdkLibraryXml needs a unique variation per APEX because the generated XML file contains the path to the
2846 // mounted APEX, which contains the name of the APEX.
2847 return true
2848}
2849
Jiyong Parke3833882020-02-17 17:28:10 +09002850// from android.PrebuiltEtcModule
Jooyung Han0703fd82020-08-26 22:11:53 +09002851func (module *sdkLibraryXml) BaseDir() string {
2852 return "etc"
2853}
2854
2855// from android.PrebuiltEtcModule
Jiyong Parke3833882020-02-17 17:28:10 +09002856func (module *sdkLibraryXml) SubDir() string {
2857 return "permissions"
2858}
2859
2860// from android.PrebuiltEtcModule
2861func (module *sdkLibraryXml) OutputFile() android.OutputPath {
2862 return module.outputFilePath
2863}
2864
2865// from android.ApexModule
2866func (module *sdkLibraryXml) AvailableFor(what string) bool {
2867 return true
2868}
2869
2870func (module *sdkLibraryXml) DepsMutator(ctx android.BottomUpMutatorContext) {
2871 // do nothing
2872}
2873
Jiyong Park45bf82e2020-12-15 22:29:02 +09002874var _ android.ApexModule = (*sdkLibraryXml)(nil)
2875
2876// Implements android.ApexModule
Dan Albertc8060532020-07-22 22:32:17 -07002877func (module *sdkLibraryXml) ShouldSupportSdkVersion(ctx android.BaseModuleContext,
2878 sdkVersion android.ApiLevel) error {
Jooyung Han749dc692020-04-15 11:03:39 +09002879 // sdkLibraryXml doesn't need to be checked separately because java_sdk_library is checked
2880 return nil
2881}
2882
Jiyong Parke3833882020-02-17 17:28:10 +09002883// File path to the runtime implementation library
Colin Cross56a83212020-09-15 18:30:11 -07002884func (module *sdkLibraryXml) implPath(ctx android.ModuleContext) string {
Jiyong Parke3833882020-02-17 17:28:10 +09002885 implName := proptools.String(module.properties.Lib_name)
Colin Cross56a83212020-09-15 18:30:11 -07002886 if apexInfo := ctx.Provider(android.ApexInfoProvider).(android.ApexInfo); !apexInfo.IsForPlatform() {
Colin Crosse07f2312020-08-13 11:24:56 -07002887 // TODO(b/146468504): ApexVariationName() is only a soong module name, not apex name.
Jiyong Parke3833882020-02-17 17:28:10 +09002888 // In most cases, this works fine. But when apex_name is set or override_apex is used
2889 // this can be wrong.
Colin Cross56a83212020-09-15 18:30:11 -07002890 return fmt.Sprintf("/apex/%s/javalib/%s.jar", apexInfo.ApexVariationName, implName)
Jiyong Parke3833882020-02-17 17:28:10 +09002891 }
2892 partition := "system"
2893 if module.SocSpecific() {
2894 partition = "vendor"
2895 } else if module.DeviceSpecific() {
2896 partition = "odm"
2897 } else if module.ProductSpecific() {
2898 partition = "product"
2899 } else if module.SystemExtSpecific() {
2900 partition = "system_ext"
2901 }
2902 return "/" + partition + "/framework/" + implName + ".jar"
2903}
2904
Pedro Loureiro9956e5e2021-09-07 17:21:59 +00002905func formattedOptionalSdkLevelAttribute(ctx android.ModuleContext, attrName string, value *string) string {
2906 if value == nil {
2907 return ""
2908 }
2909 apiLevel, err := android.ApiLevelFromUser(ctx, *value)
2910 if err != nil {
Pedro Loureiroba6682f2021-10-29 09:32:32 +00002911 // attributes in bp files have underscores but in the xml have dashes.
2912 ctx.PropertyErrorf(strings.ReplaceAll(attrName, "-", "_"), err.Error())
Pedro Loureiro9956e5e2021-09-07 17:21:59 +00002913 return ""
2914 }
Pedro Loureirob638c622021-12-22 15:28:05 +00002915 if apiLevel.IsCurrent() {
2916 // passing "current" would always mean a future release, never the current (or the current in
2917 // progress) which means some conditions would never be triggered.
2918 ctx.PropertyErrorf(strings.ReplaceAll(attrName, "-", "_"),
2919 `"current" is not an allowed value for this attribute`)
2920 return ""
2921 }
Pedro Loureiro48991222022-06-17 20:01:21 +00002922 // "safeValue" is safe because it translates finalized codenames to a string
2923 // with their SDK int.
2924 safeValue := apiLevel.String()
2925 return formattedOptionalAttribute(attrName, &safeValue)
Pedro Loureiro9956e5e2021-09-07 17:21:59 +00002926}
2927
2928// formats an attribute for the xml permissions file if the value is not null
2929// returns empty string otherwise
2930func formattedOptionalAttribute(attrName string, value *string) string {
2931 if value == nil {
2932 return ""
2933 }
2934 return fmt.Sprintf(` %s=\"%s\"\n`, attrName, *value)
2935}
2936
2937func (module *sdkLibraryXml) permissionsContents(ctx android.ModuleContext) string {
2938 libName := proptools.String(module.properties.Lib_name)
2939 libNameAttr := formattedOptionalAttribute("name", &libName)
2940 filePath := module.implPath(ctx)
2941 filePathAttr := formattedOptionalAttribute("file", &filePath)
Pedro Loureiroba6682f2021-10-29 09:32:32 +00002942 implicitFromAttr := formattedOptionalSdkLevelAttribute(ctx, "on-bootclasspath-since", module.properties.On_bootclasspath_since)
2943 implicitUntilAttr := formattedOptionalSdkLevelAttribute(ctx, "on-bootclasspath-before", module.properties.On_bootclasspath_before)
2944 minSdkAttr := formattedOptionalSdkLevelAttribute(ctx, "min-device-sdk", module.properties.Min_device_sdk)
2945 maxSdkAttr := formattedOptionalSdkLevelAttribute(ctx, "max-device-sdk", module.properties.Max_device_sdk)
Pedro Loureiro196d3e62021-12-22 19:53:01 +00002946 // <library> is understood in all android versions whereas <apex-library> is only understood from API T (and ignored before that).
2947 // 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 +00002948 var libraryTag string
2949 if module.properties.Min_device_sdk != nil {
Pedro Loureiro196d3e62021-12-22 19:53:01 +00002950 libraryTag = ` <apex-library\n`
Pedro Loureiroc3621422021-09-28 15:40:23 +00002951 } else {
2952 libraryTag = ` <library\n`
2953 }
Pedro Loureiro9956e5e2021-09-07 17:21:59 +00002954
2955 return strings.Join([]string{
2956 `<?xml version=\"1.0\" encoding=\"utf-8\"?>\n`,
2957 `<!-- Copyright (C) 2018 The Android Open Source Project\n`,
2958 `\n`,
2959 ` Licensed under the Apache License, Version 2.0 (the \"License\");\n`,
2960 ` you may not use this file except in compliance with the License.\n`,
2961 ` You may obtain a copy of the License at\n`,
2962 `\n`,
2963 ` http://www.apache.org/licenses/LICENSE-2.0\n`,
2964 `\n`,
2965 ` Unless required by applicable law or agreed to in writing, software\n`,
2966 ` distributed under the License is distributed on an \"AS IS\" BASIS,\n`,
2967 ` WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\n`,
2968 ` See the License for the specific language governing permissions and\n`,
2969 ` limitations under the License.\n`,
2970 `-->\n`,
2971 `<permissions>\n`,
Pedro Loureiroc3621422021-09-28 15:40:23 +00002972 libraryTag,
Pedro Loureiro9956e5e2021-09-07 17:21:59 +00002973 libNameAttr,
2974 filePathAttr,
2975 implicitFromAttr,
2976 implicitUntilAttr,
2977 minSdkAttr,
2978 maxSdkAttr,
2979 ` />\n`,
2980 `</permissions>\n`}, "")
2981}
2982
Jiyong Parke3833882020-02-17 17:28:10 +09002983func (module *sdkLibraryXml) GenerateAndroidBuildActions(ctx android.ModuleContext) {
Colin Cross56a83212020-09-15 18:30:11 -07002984 module.hideApexVariantFromMake = !ctx.Provider(android.ApexInfoProvider).(android.ApexInfo).IsForPlatform()
2985
Jiyong Parke3833882020-02-17 17:28:10 +09002986 libName := proptools.String(module.properties.Lib_name)
Pedro Loureiroc3621422021-09-28 15:40:23 +00002987 module.selfValidate(ctx)
Pedro Loureiro9956e5e2021-09-07 17:21:59 +00002988 xmlContent := module.permissionsContents(ctx)
Jiyong Parke3833882020-02-17 17:28:10 +09002989
2990 module.outputFilePath = android.PathForModuleOut(ctx, libName+".xml").OutputPath
Colin Crossf1a035e2020-11-16 17:32:30 -08002991 rule := android.NewRuleBuilder(pctx, ctx)
Jiyong Parke3833882020-02-17 17:28:10 +09002992 rule.Command().
2993 Text("/bin/bash -c \"echo -e '" + xmlContent + "'\" > ").
2994 Output(module.outputFilePath)
2995
Colin Crossf1a035e2020-11-16 17:32:30 -08002996 rule.Build("java_sdk_xml", "Permission XML")
Jiyong Parke3833882020-02-17 17:28:10 +09002997
2998 module.installDirPath = android.PathForModuleInstall(ctx, "etc", module.SubDir())
2999}
3000
3001func (module *sdkLibraryXml) AndroidMkEntries() []android.AndroidMkEntries {
Colin Cross56a83212020-09-15 18:30:11 -07003002 if module.hideApexVariantFromMake {
satayev8f088b02021-12-06 11:40:46 +00003003 return []android.AndroidMkEntries{{
Jiyong Parke3833882020-02-17 17:28:10 +09003004 Disabled: true,
3005 }}
3006 }
3007
satayev8f088b02021-12-06 11:40:46 +00003008 return []android.AndroidMkEntries{{
Jiyong Parke3833882020-02-17 17:28:10 +09003009 Class: "ETC",
3010 OutputFile: android.OptionalPathForPath(module.outputFilePath),
3011 ExtraEntries: []android.AndroidMkExtraEntriesFunc{
Colin Crossaa255532020-07-03 13:18:24 -07003012 func(ctx android.AndroidMkExtraEntriesContext, entries *android.AndroidMkEntries) {
Jiyong Parke3833882020-02-17 17:28:10 +09003013 entries.SetString("LOCAL_MODULE_TAGS", "optional")
Colin Crossc68db4b2021-11-11 18:59:15 -08003014 entries.SetString("LOCAL_MODULE_PATH", module.installDirPath.String())
Jiyong Parke3833882020-02-17 17:28:10 +09003015 entries.SetString("LOCAL_INSTALLED_MODULE_STEM", module.outputFilePath.Base())
3016 },
3017 },
3018 }}
3019}
Paul Duffindd46f712020-02-10 13:37:10 +00003020
Pedro Loureiroc3621422021-09-28 15:40:23 +00003021func (module *sdkLibraryXml) selfValidate(ctx android.ModuleContext) {
3022 module.validateAtLeastTAttributes(ctx)
3023 module.validateMinAndMaxDeviceSdk(ctx)
3024 module.validateMinMaxDeviceSdkAndModuleMinSdk(ctx)
3025 module.validateOnBootclasspathBeforeRequirements(ctx)
3026}
3027
3028func (module *sdkLibraryXml) validateAtLeastTAttributes(ctx android.ModuleContext) {
3029 t := android.ApiLevelOrPanic(ctx, "Tiramisu")
3030 module.attrAtLeastT(ctx, t, module.properties.Min_device_sdk, "min_device_sdk")
3031 module.attrAtLeastT(ctx, t, module.properties.Max_device_sdk, "max_device_sdk")
3032 module.attrAtLeastT(ctx, t, module.properties.On_bootclasspath_before, "on_bootclasspath_before")
3033 module.attrAtLeastT(ctx, t, module.properties.On_bootclasspath_since, "on_bootclasspath_since")
3034}
3035
3036func (module *sdkLibraryXml) attrAtLeastT(ctx android.ModuleContext, t android.ApiLevel, attr *string, attrName string) {
3037 if attr != nil {
3038 if level, err := android.ApiLevelFromUser(ctx, *attr); err == nil {
3039 // we will inform the user of invalid inputs when we try to write the
3040 // permissions xml file so we don't need to do it here
3041 if t.GreaterThan(level) {
3042 ctx.PropertyErrorf(attrName, "Attribute value needs to be at least T")
3043 }
3044 }
3045 }
3046}
3047
3048func (module *sdkLibraryXml) validateMinAndMaxDeviceSdk(ctx android.ModuleContext) {
3049 if module.properties.Min_device_sdk != nil && module.properties.Max_device_sdk != nil {
3050 min, minErr := android.ApiLevelFromUser(ctx, *module.properties.Min_device_sdk)
3051 max, maxErr := android.ApiLevelFromUser(ctx, *module.properties.Max_device_sdk)
3052 if minErr == nil && maxErr == nil {
3053 // we will inform the user of invalid inputs when we try to write the
3054 // permissions xml file so we don't need to do it here
3055 if min.GreaterThan(max) {
3056 ctx.ModuleErrorf("min_device_sdk can't be greater than max_device_sdk")
3057 }
3058 }
3059 }
3060}
3061
3062func (module *sdkLibraryXml) validateMinMaxDeviceSdkAndModuleMinSdk(ctx android.ModuleContext) {
3063 moduleMinApi := android.ApiLevelOrPanic(ctx, *module.properties.Sdk_library_min_api_level)
3064 if module.properties.Min_device_sdk != nil {
3065 api, err := android.ApiLevelFromUser(ctx, *module.properties.Min_device_sdk)
3066 if err == nil {
3067 if moduleMinApi.GreaterThan(api) {
3068 ctx.PropertyErrorf("min_device_sdk", "Can't be less than module's min sdk (%s)", moduleMinApi)
3069 }
3070 }
3071 }
3072 if module.properties.Max_device_sdk != nil {
3073 api, err := android.ApiLevelFromUser(ctx, *module.properties.Max_device_sdk)
3074 if err == nil {
3075 if moduleMinApi.GreaterThan(api) {
3076 ctx.PropertyErrorf("max_device_sdk", "Can't be less than module's min sdk (%s)", moduleMinApi)
3077 }
3078 }
3079 }
3080}
3081
3082func (module *sdkLibraryXml) validateOnBootclasspathBeforeRequirements(ctx android.ModuleContext) {
3083 moduleMinApi := android.ApiLevelOrPanic(ctx, *module.properties.Sdk_library_min_api_level)
3084 if module.properties.On_bootclasspath_before != nil {
3085 t := android.ApiLevelOrPanic(ctx, "Tiramisu")
3086 // if we use the attribute, then we need to do this validation
3087 if moduleMinApi.LessThan(t) {
3088 // if minAPi is < T, then we need to have min_device_sdk (which only accepts T+)
3089 if module.properties.Min_device_sdk == nil {
3090 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")
3091 }
3092 }
3093 }
3094}
3095
Paul Duffindd46f712020-02-10 13:37:10 +00003096type sdkLibrarySdkMemberType struct {
3097 android.SdkMemberTypeBase
3098}
3099
Paul Duffin296701e2021-07-14 10:29:36 +01003100func (s *sdkLibrarySdkMemberType) AddDependencies(ctx android.SdkDependencyContext, dependencyTag blueprint.DependencyTag, names []string) {
3101 ctx.AddVariationDependencies(nil, dependencyTag, names...)
Paul Duffindd46f712020-02-10 13:37:10 +00003102}
3103
3104func (s *sdkLibrarySdkMemberType) IsInstance(module android.Module) bool {
3105 _, ok := module.(*SdkLibrary)
3106 return ok
3107}
3108
3109func (s *sdkLibrarySdkMemberType) AddPrebuiltModule(ctx android.SdkMemberContext, member android.SdkMember) android.BpModule {
3110 return ctx.SnapshotBuilder().AddPrebuiltModule(member, "java_sdk_library_import")
3111}
3112
3113func (s *sdkLibrarySdkMemberType) CreateVariantPropertiesStruct() android.SdkMemberProperties {
3114 return &sdkLibrarySdkMemberProperties{}
3115}
3116
Paul Duffin976b0e52021-04-27 23:20:26 +01003117var javaSdkLibrarySdkMemberType = &sdkLibrarySdkMemberType{
3118 android.SdkMemberTypeBase{
3119 PropertyName: "java_sdk_libs",
3120 SupportsSdk: true,
3121 },
3122}
3123
Paul Duffindd46f712020-02-10 13:37:10 +00003124type sdkLibrarySdkMemberProperties struct {
3125 android.SdkMemberPropertiesBase
3126
Paul Duffine8409952022-09-22 16:24:46 +01003127 // Stem name for files in the sdk snapshot.
3128 //
3129 // This is used to construct the path names of various sdk library files in the sdk snapshot to
3130 // make sure that they match the finalized versions of those files in prebuilts/sdk.
3131 //
3132 // This property is marked as keep so that it will be kept in all instances of this struct, will
3133 // not be cleared but will be copied to common structs. That is needed because this field is used
3134 // to construct many file names for other parts of this struct and so it needs to be present in
3135 // all structs. If it was not marked as keep then it would be cleared in some structs and so would
3136 // be unavailable for generating file names if there were other properties that were still set.
3137 Stem string `sdk:"keep"`
3138
Paul Duffindd46f712020-02-10 13:37:10 +00003139 // Scope to per scope properties.
Paul Duffin106a3a42022-01-27 16:39:06 +00003140 Scopes map[*apiScope]*scopeProperties
Paul Duffindd46f712020-02-10 13:37:10 +00003141
Paul Duffin3d1248c2020-04-09 00:10:17 +01003142 // The Java stubs source files.
3143 Stub_srcs []string
Paul Duffinf7a64332020-05-13 16:54:55 +01003144
3145 // The naming scheme.
3146 Naming_scheme *string
Paul Duffind7eb1c22020-05-26 20:57:10 +01003147
3148 // True if the java_sdk_library_import is for a shared library, false
3149 // otherwise.
3150 Shared_library *bool
Paul Duffina2ae7e02020-09-11 11:55:00 +01003151
Paul Duffin1267d872021-04-16 17:21:36 +01003152 // True if the stub imports should produce dex jars.
3153 Compile_dex *bool
3154
Paul Duffina2ae7e02020-09-11 11:55:00 +01003155 // The paths to the doctag files to add to the prebuilt.
3156 Doctag_paths android.Paths
Paul Duffin869de142021-07-15 14:14:41 +01003157
3158 Permitted_packages []string
Pedro Loureiro9956e5e2021-09-07 17:21:59 +00003159
3160 // Signals that this shared library is part of the bootclasspath starting
3161 // on the version indicated in this attribute.
3162 //
3163 // This will make platforms at this level and above to ignore
3164 // <uses-library> tags with this library name because the library is already
3165 // available
3166 On_bootclasspath_since *string
3167
3168 // Signals that this shared library was part of the bootclasspath before
3169 // (but not including) the version indicated in this attribute.
3170 //
3171 // The system will automatically add a <uses-library> tag with this library to
3172 // apps that target any SDK less than the version indicated in this attribute.
3173 On_bootclasspath_before *string
3174
3175 // Indicates that PackageManager should ignore this shared library if the
3176 // platform is below the version indicated in this attribute.
3177 //
3178 // This means that the device won't recognise this library as installed.
3179 Min_device_sdk *string
3180
3181 // Indicates that PackageManager should ignore this shared library if the
3182 // platform is above the version indicated in this attribute.
3183 //
3184 // This means that the device won't recognise this library as installed.
3185 Max_device_sdk *string
Jiakai Zhang9c4dc192023-02-09 00:09:24 +08003186
3187 DexPreoptProfileGuided *bool `supported_build_releases:"UpsideDownCake+"`
Paul Duffindd46f712020-02-10 13:37:10 +00003188}
3189
3190type scopeProperties struct {
Paul Duffin1fd005d2020-04-09 01:08:11 +01003191 Jars android.Paths
3192 StubsSrcJar android.Path
3193 CurrentApiFile android.Path
3194 RemovedApiFile android.Path
Paul Duffine7babdb2022-02-10 13:06:54 +00003195 AnnotationsZip android.Path `supported_build_releases:"Tiramisu+"`
Paul Duffin1fd005d2020-04-09 01:08:11 +01003196 SdkVersion string
Paul Duffindd46f712020-02-10 13:37:10 +00003197}
3198
3199func (s *sdkLibrarySdkMemberProperties) PopulateFromVariant(ctx android.SdkMemberContext, variant android.Module) {
3200 sdk := variant.(*SdkLibrary)
3201
Paul Duffine8409952022-09-22 16:24:46 +01003202 // Copy the stem name for files in the sdk snapshot.
3203 s.Stem = sdk.distStem()
3204
Paul Duffin106a3a42022-01-27 16:39:06 +00003205 s.Scopes = make(map[*apiScope]*scopeProperties)
Paul Duffindd46f712020-02-10 13:37:10 +00003206 for _, apiScope := range allApiScopes {
Paul Duffin803a9562020-05-20 11:52:25 +01003207 paths := sdk.findScopePaths(apiScope)
3208 if paths == nil {
3209 continue
3210 }
3211
Paul Duffindd46f712020-02-10 13:37:10 +00003212 jars := paths.stubsImplPath
3213 if len(jars) > 0 {
3214 properties := scopeProperties{}
3215 properties.Jars = jars
Paul Duffin780c5f42020-05-12 15:52:55 +01003216 properties.SdkVersion = sdk.sdkVersionForStubsLibrary(ctx.SdkModuleContext(), apiScope)
Paul Duffin0f8faff2020-05-20 16:18:00 +01003217 properties.StubsSrcJar = paths.stubsSrcJar.Path()
Paul Duffin10269f12020-06-19 18:39:55 +01003218 if paths.currentApiFilePath.Valid() {
3219 properties.CurrentApiFile = paths.currentApiFilePath.Path()
3220 }
3221 if paths.removedApiFilePath.Valid() {
3222 properties.RemovedApiFile = paths.removedApiFilePath.Path()
3223 }
Anton Hanssond78eb762021-09-21 15:25:12 +01003224 // The annotations zip is only available for modules that set annotations_enabled: true.
3225 if paths.annotationsZip.Valid() {
3226 properties.AnnotationsZip = paths.annotationsZip.Path()
3227 }
Paul Duffin106a3a42022-01-27 16:39:06 +00003228 s.Scopes[apiScope] = &properties
Paul Duffindd46f712020-02-10 13:37:10 +00003229 }
3230 }
3231
Paul Duffindfa131e2020-05-15 20:37:11 +01003232 s.Naming_scheme = sdk.commonSdkLibraryProperties.Naming_scheme
Paul Duffind7eb1c22020-05-26 20:57:10 +01003233 s.Shared_library = proptools.BoolPtr(sdk.sharedLibrary())
Paul Duffin1267d872021-04-16 17:21:36 +01003234 s.Compile_dex = sdk.dexProperties.Compile_dex
Paul Duffina2ae7e02020-09-11 11:55:00 +01003235 s.Doctag_paths = sdk.doctagPaths
Paul Duffin869de142021-07-15 14:14:41 +01003236 s.Permitted_packages = sdk.PermittedPackagesForUpdatableBootJars()
Pedro Loureiro9956e5e2021-09-07 17:21:59 +00003237 s.On_bootclasspath_since = sdk.commonSdkLibraryProperties.On_bootclasspath_since
3238 s.On_bootclasspath_before = sdk.commonSdkLibraryProperties.On_bootclasspath_before
3239 s.Min_device_sdk = sdk.commonSdkLibraryProperties.Min_device_sdk
3240 s.Max_device_sdk = sdk.commonSdkLibraryProperties.Max_device_sdk
Jiakai Zhang9c4dc192023-02-09 00:09:24 +08003241
3242 if sdk.dexpreopter.dexpreoptProperties.Dex_preopt_result.Profile_guided {
3243 s.DexPreoptProfileGuided = proptools.BoolPtr(true)
3244 }
Paul Duffindd46f712020-02-10 13:37:10 +00003245}
3246
3247func (s *sdkLibrarySdkMemberProperties) AddToPropertySet(ctx android.SdkMemberContext, propertySet android.BpPropertySet) {
Paul Duffinf7a64332020-05-13 16:54:55 +01003248 if s.Naming_scheme != nil {
3249 propertySet.AddProperty("naming_scheme", proptools.String(s.Naming_scheme))
3250 }
Paul Duffind7eb1c22020-05-26 20:57:10 +01003251 if s.Shared_library != nil {
3252 propertySet.AddProperty("shared_library", *s.Shared_library)
3253 }
Paul Duffin1267d872021-04-16 17:21:36 +01003254 if s.Compile_dex != nil {
3255 propertySet.AddProperty("compile_dex", *s.Compile_dex)
3256 }
Paul Duffin869de142021-07-15 14:14:41 +01003257 if len(s.Permitted_packages) > 0 {
3258 propertySet.AddProperty("permitted_packages", s.Permitted_packages)
3259 }
Jiakai Zhang9c4dc192023-02-09 00:09:24 +08003260 dexPreoptSet := propertySet.AddPropertySet("dex_preopt")
3261 if s.DexPreoptProfileGuided != nil {
3262 dexPreoptSet.AddProperty("profile_guided", proptools.Bool(s.DexPreoptProfileGuided))
3263 }
Paul Duffinf7a64332020-05-13 16:54:55 +01003264
Paul Duffine8409952022-09-22 16:24:46 +01003265 stem := s.Stem
3266
Paul Duffindd46f712020-02-10 13:37:10 +00003267 for _, apiScope := range allApiScopes {
3268 if properties, ok := s.Scopes[apiScope]; ok {
Paul Duffin6b836ba2020-05-13 19:19:49 +01003269 scopeSet := propertySet.AddPropertySet(apiScope.propertyName)
Paul Duffindd46f712020-02-10 13:37:10 +00003270
Paul Duffin958806b2022-05-16 13:10:47 +00003271 scopeDir := apiScope.snapshotRelativeDir()
Paul Duffin3d1248c2020-04-09 00:10:17 +01003272
Paul Duffindd46f712020-02-10 13:37:10 +00003273 var jars []string
3274 for _, p := range properties.Jars {
Paul Duffine8409952022-09-22 16:24:46 +01003275 dest := filepath.Join(scopeDir, stem+"-stubs.jar")
Paul Duffindd46f712020-02-10 13:37:10 +00003276 ctx.SnapshotBuilder().CopyToSnapshot(p, dest)
3277 jars = append(jars, dest)
3278 }
3279 scopeSet.AddProperty("jars", jars)
3280
Paul Duffin22628d52021-05-12 23:13:22 +01003281 if ctx.SdkModuleContext().Config().IsEnvTrue("SOONG_SDK_SNAPSHOT_USE_SRCJAR") {
3282 // Copy the stubs source jar into the snapshot zip as is.
Paul Duffine8409952022-09-22 16:24:46 +01003283 srcJarSnapshotPath := filepath.Join(scopeDir, stem+".srcjar")
Paul Duffin22628d52021-05-12 23:13:22 +01003284 ctx.SnapshotBuilder().CopyToSnapshot(properties.StubsSrcJar, srcJarSnapshotPath)
3285 scopeSet.AddProperty("stub_srcs", []string{srcJarSnapshotPath})
3286 } else {
3287 // Merge the stubs source jar into the snapshot zip so that when it is unpacked
3288 // the source files are also unpacked.
Paul Duffine8409952022-09-22 16:24:46 +01003289 snapshotRelativeDir := filepath.Join(scopeDir, stem+"_stub_sources")
Paul Duffin22628d52021-05-12 23:13:22 +01003290 ctx.SnapshotBuilder().UnzipToSnapshot(properties.StubsSrcJar, snapshotRelativeDir)
3291 scopeSet.AddProperty("stub_srcs", []string{snapshotRelativeDir})
3292 }
Paul Duffin3d1248c2020-04-09 00:10:17 +01003293
Paul Duffin1fd005d2020-04-09 01:08:11 +01003294 if properties.CurrentApiFile != nil {
Paul Duffine8409952022-09-22 16:24:46 +01003295 currentApiSnapshotPath := apiScope.snapshotRelativeCurrentApiTxtPath(stem)
Paul Duffin1fd005d2020-04-09 01:08:11 +01003296 ctx.SnapshotBuilder().CopyToSnapshot(properties.CurrentApiFile, currentApiSnapshotPath)
3297 scopeSet.AddProperty("current_api", currentApiSnapshotPath)
3298 }
3299
3300 if properties.RemovedApiFile != nil {
Paul Duffine8409952022-09-22 16:24:46 +01003301 removedApiSnapshotPath := apiScope.snapshotRelativeRemovedApiTxtPath(stem)
Paul Duffin3dbf9fd2020-06-02 13:00:02 +01003302 ctx.SnapshotBuilder().CopyToSnapshot(properties.RemovedApiFile, removedApiSnapshotPath)
Paul Duffin1fd005d2020-04-09 01:08:11 +01003303 scopeSet.AddProperty("removed_api", removedApiSnapshotPath)
3304 }
3305
Anton Hanssond78eb762021-09-21 15:25:12 +01003306 if properties.AnnotationsZip != nil {
Paul Duffine8409952022-09-22 16:24:46 +01003307 annotationsSnapshotPath := filepath.Join(scopeDir, stem+"_annotations.zip")
Anton Hanssond78eb762021-09-21 15:25:12 +01003308 ctx.SnapshotBuilder().CopyToSnapshot(properties.AnnotationsZip, annotationsSnapshotPath)
3309 scopeSet.AddProperty("annotations", annotationsSnapshotPath)
3310 }
3311
Paul Duffindd46f712020-02-10 13:37:10 +00003312 if properties.SdkVersion != "" {
3313 scopeSet.AddProperty("sdk_version", properties.SdkVersion)
3314 }
3315 }
3316 }
3317
Paul Duffina2ae7e02020-09-11 11:55:00 +01003318 if len(s.Doctag_paths) > 0 {
3319 dests := []string{}
3320 for _, p := range s.Doctag_paths {
3321 dest := filepath.Join("doctags", p.Rel())
3322 ctx.SnapshotBuilder().CopyToSnapshot(p, dest)
3323 dests = append(dests, dest)
3324 }
3325 propertySet.AddProperty("doctag_files", dests)
3326 }
Paul Duffindd46f712020-02-10 13:37:10 +00003327}