blob: 3de82388dc9eed99981557c3db4d0cf3c256d86d [file] [log] [blame]
Jiyong Parkc678ad32018-04-10 13:07:10 +09001// Copyright 2018 Google Inc. All rights reserved.
2//
3// Licensed under the Apache License, Version 2.0 (the "License");
4// you may not use this file except in compliance with the License.
5// You may obtain a copy of the License at
6//
7// http://www.apache.org/licenses/LICENSE-2.0
8//
9// Unless required by applicable law or agreed to in writing, software
10// distributed under the License is distributed on an "AS IS" BASIS,
11// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12// See the License for the specific language governing permissions and
13// limitations under the License.
14
15package java
16
17import (
Jiyong Parkc678ad32018-04-10 13:07:10 +090018 "fmt"
19 "path"
Sundong Ahn054b19a2018-10-19 13:46:09 +090020 "path/filepath"
Paul Duffin46a26a82020-04-07 19:27:04 +010021 "reflect"
Paul Duffin46dc45a2020-05-14 15:39:10 +010022 "regexp"
Jiyong Park82484c02018-04-23 21:41:26 +090023 "sort"
Jiyong Parkc678ad32018-04-10 13:07:10 +090024 "strings"
Jiyong Park82484c02018-04-23 21:41:26 +090025 "sync"
Jiyong Parkc678ad32018-04-10 13:07:10 +090026
Chris Parsons39a16972023-06-08 14:28:51 +000027 "android/soong/ui/metrics/bp2build_metrics_proto"
Paul Duffind1b3a922020-01-22 11:57:20 +000028 "github.com/google/blueprint"
Jiyong Parkc678ad32018-04-10 13:07:10 +090029 "github.com/google/blueprint/proptools"
Paul Duffin46a26a82020-04-07 19:27:04 +010030
31 "android/soong/android"
Zi Wangb2179e32023-01-31 15:53:30 -080032 "android/soong/bazel"
Ulya Trafimovichdbf31662020-12-17 12:07:54 +000033 "android/soong/dexpreopt"
Jiyong Parkc678ad32018-04-10 13:07:10 +090034)
35
Jooyung Han58f26ab2019-12-18 15:34:32 +090036const (
Pedro Loureiro9956e5e2021-09-07 17:21:59 +000037 sdkXmlFileSuffix = ".xml"
Jiyong Parkc678ad32018-04-10 13:07:10 +090038)
39
Paul Duffind1b3a922020-01-22 11:57:20 +000040// A tag to associated a dependency with a specific api scope.
41type scopeDependencyTag struct {
42 blueprint.BaseDependencyTag
43 name string
44 apiScope *apiScope
Paul Duffinc8782502020-04-29 20:45:27 +010045
46 // Function for extracting appropriate path information from the dependency.
Colin Crossdcf71b22021-02-01 13:59:03 -080047 depInfoExtractor func(paths *scopePaths, ctx android.ModuleContext, dep android.Module) error
Paul Duffinc8782502020-04-29 20:45:27 +010048}
49
50// Extract tag specific information from the dependency.
51func (tag scopeDependencyTag) extractDepInfo(ctx android.ModuleContext, dep android.Module, paths *scopePaths) {
Colin Crossdcf71b22021-02-01 13:59:03 -080052 err := tag.depInfoExtractor(paths, ctx, dep)
Paul Duffinc8782502020-04-29 20:45:27 +010053 if err != nil {
54 ctx.ModuleErrorf("has an invalid {scopeDependencyTag: %s} dependency on module %s: %s", tag.name, ctx.OtherModuleName(dep), err.Error())
55 }
Paul Duffind1b3a922020-01-22 11:57:20 +000056}
57
Paul Duffin80342d72020-06-26 22:08:43 +010058var _ android.ReplaceSourceWithPrebuilt = (*scopeDependencyTag)(nil)
59
60func (tag scopeDependencyTag) ReplaceSourceWithPrebuilt() bool {
61 return false
62}
63
Paul Duffind1b3a922020-01-22 11:57:20 +000064// Provides information about an api scope, e.g. public, system, test.
65type apiScope struct {
66 // The name of the api scope, e.g. public, system, test
67 name string
68
Paul Duffin97b53b82020-05-05 14:40:52 +010069 // The api scope that this scope extends.
Paul Duffind0b9fca2022-09-30 18:11:41 +010070 //
71 // This organizes the scopes into an extension hierarchy.
72 //
73 // If set this means that the API provided by this scope includes the API provided by the scope
74 // set in this field.
Paul Duffin97b53b82020-05-05 14:40:52 +010075 extends *apiScope
76
Paul Duffind0b9fca2022-09-30 18:11:41 +010077 // The next api scope that a library that uses this scope can access.
78 //
79 // This organizes the scopes into an access hierarchy.
80 //
81 // If set this means that a library that can access this API can also access the API provided by
82 // the scope set in this field.
83 //
84 // A module that sets sdk_version: "<scope>_current" should have access to the <scope> API of
85 // every java_sdk_library that it depends on. If the library does not provide an API for <scope>
86 // then it will traverse up this access hierarchy to find an API that it does provide.
87 //
88 // If this is not set then it defaults to the scope set in extends.
89 canAccess *apiScope
90
Paul Duffin3375e352020-04-28 10:44:03 +010091 // The legacy enabled status for a specific scope can be dependent on other
92 // properties that have been specified on the library so it is provided by
93 // a function that can determine the status by examining those properties.
94 legacyEnabledStatus func(module *SdkLibrary) bool
95
96 // The default enabled status for non-legacy behavior, which is triggered by
97 // explicitly enabling at least one api scope.
98 defaultEnabledStatus bool
99
100 // Gets a pointer to the scope specific properties.
101 scopeSpecificProperties func(module *SdkLibrary) *ApiScopeProperties
102
Paul Duffin46a26a82020-04-07 19:27:04 +0100103 // The name of the field in the dynamically created structure.
104 fieldName string
105
Paul Duffin6b836ba2020-05-13 19:19:49 +0100106 // The name of the property in the java_sdk_library_import
107 propertyName string
108
Paul Duffind1b3a922020-01-22 11:57:20 +0000109 // The tag to use to depend on the stubs library module.
110 stubsTag scopeDependencyTag
111
Paul Duffin0ff08bd2020-04-29 13:30:54 +0100112 // The tag to use to depend on the stubs source module (if separate from the API module).
113 stubsSourceTag scopeDependencyTag
114
115 // The tag to use to depend on the API file generating module (if separate from the stubs source module).
116 apiFileTag scopeDependencyTag
117
Paul Duffinc8782502020-04-29 20:45:27 +0100118 // The tag to use to depend on the stubs source and API module.
119 stubsSourceAndApiTag scopeDependencyTag
Paul Duffind1b3a922020-01-22 11:57:20 +0000120
Paul Duffin958806b2022-05-16 13:10:47 +0000121 // The tag to use to depend on the module that provides the latest version of the API .txt file.
122 latestApiModuleTag scopeDependencyTag
123
124 // The tag to use to depend on the module that provides the latest version of the API removed.txt
125 // file.
126 latestRemovedApiModuleTag scopeDependencyTag
127
Paul Duffind1b3a922020-01-22 11:57:20 +0000128 // The scope specific prefix to add to the api file base of "current.txt" or "removed.txt".
129 apiFilePrefix string
130
Paul Duffind0b9fca2022-09-30 18:11:41 +0100131 // The scope specific suffix to add to the sdk library module name to construct a scope specific
Paul Duffind1b3a922020-01-22 11:57:20 +0000132 // module name.
133 moduleSuffix string
134
Paul Duffind1b3a922020-01-22 11:57:20 +0000135 // SDK version that the stubs library is built against. Note that this is always
136 // *current. Older stubs library built with a numbered SDK version is created from
137 // the prebuilt jar.
138 sdkVersion string
Paul Duffin1fb487d2020-04-07 18:50:10 +0100139
Paul Duffin15f34ef2020-07-20 18:04:44 +0100140 // The annotation that identifies this API level, empty for the public API scope.
141 annotation string
142
Paul Duffin1fb487d2020-04-07 18:50:10 +0100143 // Extra arguments to pass to droidstubs for this scope.
Paul Duffin0ff08bd2020-04-29 13:30:54 +0100144 //
Paul Duffin15f34ef2020-07-20 18:04:44 +0100145 // This is not used directly but is used to construct the droidstubsArgs.
146 extraArgs []string
Paul Duffin0ff08bd2020-04-29 13:30:54 +0100147
Paul Duffin15f34ef2020-07-20 18:04:44 +0100148 // The args that must be passed to droidstubs to generate the API and stubs source
149 // for this scope, constructed dynamically by initApiScope().
Paul Duffin0ff08bd2020-04-29 13:30:54 +0100150 //
151 // The API only includes the additional members that this scope adds over the scope
152 // that it extends.
Paul Duffin15f34ef2020-07-20 18:04:44 +0100153 //
154 // The stubs source must include the definitions of everything that is in this
155 // api scope and all the scopes that this one extends.
156 droidstubsArgs []string
Paul Duffin0ff08bd2020-04-29 13:30:54 +0100157
Anton Hansson6478ac12020-05-02 11:19:36 +0100158 // Whether the api scope can be treated as unstable, and should skip compat checks.
159 unstable bool
Jihoon Kang1c92c3e2023-03-23 17:44:51 +0000160
161 // Represents the SDK kind of this scope.
162 kind android.SdkKind
Paul Duffind1b3a922020-01-22 11:57:20 +0000163}
164
165// Initialize a scope, creating and adding appropriate dependency tags
166func initApiScope(scope *apiScope) *apiScope {
Paul Duffinc8782502020-04-29 20:45:27 +0100167 name := scope.name
Paul Duffin46dc45a2020-05-14 15:39:10 +0100168 scopeByName[name] = scope
169 allScopeNames = append(allScopeNames, name)
Paul Duffin6b836ba2020-05-13 19:19:49 +0100170 scope.propertyName = strings.ReplaceAll(name, "-", "_")
171 scope.fieldName = proptools.FieldNameForProperty(scope.propertyName)
Paul Duffind1b3a922020-01-22 11:57:20 +0000172 scope.stubsTag = scopeDependencyTag{
Paul Duffinc8782502020-04-29 20:45:27 +0100173 name: name + "-stubs",
174 apiScope: scope,
175 depInfoExtractor: (*scopePaths).extractStubsLibraryInfoFromDependency,
Paul Duffind1b3a922020-01-22 11:57:20 +0000176 }
Paul Duffin0ff08bd2020-04-29 13:30:54 +0100177 scope.stubsSourceTag = scopeDependencyTag{
178 name: name + "-stubs-source",
179 apiScope: scope,
180 depInfoExtractor: (*scopePaths).extractStubsSourceInfoFromDep,
181 }
182 scope.apiFileTag = scopeDependencyTag{
183 name: name + "-api",
184 apiScope: scope,
185 depInfoExtractor: (*scopePaths).extractApiInfoFromDep,
186 }
Paul Duffinc8782502020-04-29 20:45:27 +0100187 scope.stubsSourceAndApiTag = scopeDependencyTag{
188 name: name + "-stubs-source-and-api",
189 apiScope: scope,
190 depInfoExtractor: (*scopePaths).extractStubsSourceAndApiInfoFromApiStubsProvider,
Paul Duffind1b3a922020-01-22 11:57:20 +0000191 }
Paul Duffin958806b2022-05-16 13:10:47 +0000192 scope.latestApiModuleTag = scopeDependencyTag{
193 name: name + "-latest-api",
194 apiScope: scope,
195 depInfoExtractor: (*scopePaths).extractLatestApiPath,
196 }
197 scope.latestRemovedApiModuleTag = scopeDependencyTag{
198 name: name + "-latest-removed-api",
199 apiScope: scope,
200 depInfoExtractor: (*scopePaths).extractLatestRemovedApiPath,
201 }
Paul Duffin0ff08bd2020-04-29 13:30:54 +0100202
203 // To get the args needed to generate the stubs source append all the args from
204 // this scope and all the scopes it extends as each set of args adds additional
205 // members to the stubs.
Paul Duffin15f34ef2020-07-20 18:04:44 +0100206 var scopeSpecificArgs []string
207 if scope.annotation != "" {
208 scopeSpecificArgs = []string{"--show-annotation", scope.annotation}
Paul Duffin0ff08bd2020-04-29 13:30:54 +0100209 }
Paul Duffin15f34ef2020-07-20 18:04:44 +0100210 for s := scope; s != nil; s = s.extends {
211 scopeSpecificArgs = append(scopeSpecificArgs, s.extraArgs...)
Paul Duffin0ff08bd2020-04-29 13:30:54 +0100212
Paul Duffin15f34ef2020-07-20 18:04:44 +0100213 // Ensure that the generated stubs includes all the API elements from the API scope
214 // that this scope extends.
215 if s != scope && s.annotation != "" {
216 scopeSpecificArgs = append(scopeSpecificArgs, "--show-for-stub-purposes-annotation", s.annotation)
217 }
218 }
Paul Duffin0ff08bd2020-04-29 13:30:54 +0100219
Paul Duffind0b9fca2022-09-30 18:11:41 +0100220 // By default, a library that can access a scope can also access the scope it extends.
221 if scope.canAccess == nil {
222 scope.canAccess = scope.extends
223 }
224
Paul Duffin15f34ef2020-07-20 18:04:44 +0100225 // Escape any special characters in the arguments. This is needed because droidstubs
226 // passes these directly to the shell command.
227 scope.droidstubsArgs = proptools.ShellEscapeList(scopeSpecificArgs)
Paul Duffin0ff08bd2020-04-29 13:30:54 +0100228
Paul Duffind1b3a922020-01-22 11:57:20 +0000229 return scope
230}
231
Anton Hansson08f476b2021-04-07 15:32:19 +0100232func (scope *apiScope) stubsLibraryModuleNameSuffix() string {
233 return ".stubs" + scope.moduleSuffix
234}
235
Jihoon Kang1c92c3e2023-03-23 17:44:51 +0000236func (scope *apiScope) apiLibraryModuleName(baseName string) string {
237 return scope.stubsLibraryModuleName(baseName) + ".from-text"
238}
239
Jihoon Kang1147b312023-06-08 23:25:57 +0000240func (scope *apiScope) sourceStubLibraryModuleName(baseName string) string {
241 return scope.stubsLibraryModuleName(baseName) + ".from-source"
242}
243
Paul Duffinc3091c82020-05-08 14:16:20 +0100244func (scope *apiScope) stubsLibraryModuleName(baseName string) string {
Anton Hansson08f476b2021-04-07 15:32:19 +0100245 return baseName + scope.stubsLibraryModuleNameSuffix()
Paul Duffind1b3a922020-01-22 11:57:20 +0000246}
247
Paul Duffinc8782502020-04-29 20:45:27 +0100248func (scope *apiScope) stubsSourceModuleName(baseName string) string {
Paul Duffindd9d0742020-05-08 15:52:37 +0100249 return baseName + ".stubs.source" + scope.moduleSuffix
Paul Duffind1b3a922020-01-22 11:57:20 +0000250}
251
Paul Duffin0ff08bd2020-04-29 13:30:54 +0100252func (scope *apiScope) apiModuleName(baseName string) string {
Paul Duffindd9d0742020-05-08 15:52:37 +0100253 return baseName + ".api" + scope.moduleSuffix
Paul Duffin0ff08bd2020-04-29 13:30:54 +0100254}
255
Paul Duffin3375e352020-04-28 10:44:03 +0100256func (scope *apiScope) String() string {
257 return scope.name
258}
259
Paul Duffin958806b2022-05-16 13:10:47 +0000260// snapshotRelativeDir returns the snapshot directory into which the files related to scopes will
261// be stored.
262func (scope *apiScope) snapshotRelativeDir() string {
263 return filepath.Join("sdk_library", scope.name)
264}
265
266// snapshotRelativeCurrentApiTxtPath returns the snapshot path to the API .txt file for the named
267// library.
268func (scope *apiScope) snapshotRelativeCurrentApiTxtPath(name string) string {
269 return filepath.Join(scope.snapshotRelativeDir(), name+".txt")
270}
271
272// snapshotRelativeRemovedApiTxtPath returns the snapshot path to the removed API .txt file for the
273// named library.
274func (scope *apiScope) snapshotRelativeRemovedApiTxtPath(name string) string {
275 return filepath.Join(scope.snapshotRelativeDir(), name+"-removed.txt")
276}
277
Paul Duffind1b3a922020-01-22 11:57:20 +0000278type apiScopes []*apiScope
279
280func (scopes apiScopes) Strings(accessor func(*apiScope) string) []string {
281 var list []string
282 for _, scope := range scopes {
283 list = append(list, accessor(scope))
284 }
285 return list
286}
287
Jiyong Parkc678ad32018-04-10 13:07:10 +0900288var (
Paul Duffin46dc45a2020-05-14 15:39:10 +0100289 scopeByName = make(map[string]*apiScope)
290 allScopeNames []string
Paul Duffind1b3a922020-01-22 11:57:20 +0000291 apiScopePublic = initApiScope(&apiScope{
Paul Duffin3375e352020-04-28 10:44:03 +0100292 name: "public",
293
294 // Public scope is enabled by default for both legacy and non-legacy modes.
295 legacyEnabledStatus: func(module *SdkLibrary) bool {
296 return true
297 },
298 defaultEnabledStatus: true,
299
300 scopeSpecificProperties: func(module *SdkLibrary) *ApiScopeProperties {
301 return &module.sdkLibraryProperties.Public
302 },
Paul Duffind1b3a922020-01-22 11:57:20 +0000303 sdkVersion: "current",
Jihoon Kang1c92c3e2023-03-23 17:44:51 +0000304 kind: android.SdkPublic,
Paul Duffind1b3a922020-01-22 11:57:20 +0000305 })
306 apiScopeSystem = initApiScope(&apiScope{
Paul Duffin3375e352020-04-28 10:44:03 +0100307 name: "system",
308 extends: apiScopePublic,
309 legacyEnabledStatus: (*SdkLibrary).generateTestAndSystemScopesByDefault,
310 scopeSpecificProperties: func(module *SdkLibrary) *ApiScopeProperties {
311 return &module.sdkLibraryProperties.System
312 },
Paul Duffin15f34ef2020-07-20 18:04:44 +0100313 apiFilePrefix: "system-",
314 moduleSuffix: ".system",
315 sdkVersion: "system_current",
316 annotation: "android.annotation.SystemApi(client=android.annotation.SystemApi.Client.PRIVILEGED_APPS)",
Jihoon Kang1c92c3e2023-03-23 17:44:51 +0000317 kind: android.SdkSystem,
Paul Duffind1b3a922020-01-22 11:57:20 +0000318 })
319 apiScopeTest = initApiScope(&apiScope{
Paul Duffin3375e352020-04-28 10:44:03 +0100320 name: "test",
Anton Hansson4fe970f2020-10-09 10:16:49 +0100321 extends: apiScopeSystem,
Paul Duffin3375e352020-04-28 10:44:03 +0100322 legacyEnabledStatus: (*SdkLibrary).generateTestAndSystemScopesByDefault,
323 scopeSpecificProperties: func(module *SdkLibrary) *ApiScopeProperties {
324 return &module.sdkLibraryProperties.Test
325 },
Paul Duffin15f34ef2020-07-20 18:04:44 +0100326 apiFilePrefix: "test-",
327 moduleSuffix: ".test",
328 sdkVersion: "test_current",
329 annotation: "android.annotation.TestApi",
330 unstable: true,
Jihoon Kang1c92c3e2023-03-23 17:44:51 +0000331 kind: android.SdkTest,
Paul Duffind1b3a922020-01-22 11:57:20 +0000332 })
Paul Duffin8f265b92020-04-28 14:13:56 +0100333 apiScopeModuleLib = initApiScope(&apiScope{
Paul Duffin6b836ba2020-05-13 19:19:49 +0100334 name: "module-lib",
Paul Duffin8f265b92020-04-28 14:13:56 +0100335 extends: apiScopeSystem,
Paul Duffin0c5bae52020-06-02 13:00:08 +0100336 // The module-lib scope is disabled by default in legacy mode.
Paul Duffin8f265b92020-04-28 14:13:56 +0100337 //
338 // Enabling this would break existing usages.
339 legacyEnabledStatus: func(module *SdkLibrary) bool {
340 return false
341 },
342 scopeSpecificProperties: func(module *SdkLibrary) *ApiScopeProperties {
343 return &module.sdkLibraryProperties.Module_lib
344 },
345 apiFilePrefix: "module-lib-",
346 moduleSuffix: ".module_lib",
347 sdkVersion: "module_current",
Paul Duffin15f34ef2020-07-20 18:04:44 +0100348 annotation: "android.annotation.SystemApi(client=android.annotation.SystemApi.Client.MODULE_LIBRARIES)",
Jihoon Kang1c92c3e2023-03-23 17:44:51 +0000349 kind: android.SdkModule,
Paul Duffin8f265b92020-04-28 14:13:56 +0100350 })
Paul Duffin0c5bae52020-06-02 13:00:08 +0100351 apiScopeSystemServer = initApiScope(&apiScope{
352 name: "system-server",
353 extends: apiScopePublic,
Paul Duffind0b9fca2022-09-30 18:11:41 +0100354
355 // The system-server scope can access the module-lib scope.
356 //
357 // A module that provides a system-server API is appended to the standard bootclasspath that is
358 // used by the system server. So, it should be able to access module-lib APIs provided by
359 // libraries on the bootclasspath.
360 canAccess: apiScopeModuleLib,
361
Paul Duffin0c5bae52020-06-02 13:00:08 +0100362 // The system-server scope is disabled by default in legacy mode.
363 //
364 // Enabling this would break existing usages.
365 legacyEnabledStatus: func(module *SdkLibrary) bool {
366 return false
367 },
368 scopeSpecificProperties: func(module *SdkLibrary) *ApiScopeProperties {
369 return &module.sdkLibraryProperties.System_server
370 },
371 apiFilePrefix: "system-server-",
372 moduleSuffix: ".system_server",
373 sdkVersion: "system_server_current",
Paul Duffin15f34ef2020-07-20 18:04:44 +0100374 annotation: "android.annotation.SystemApi(client=android.annotation.SystemApi.Client.SYSTEM_SERVER)",
375 extraArgs: []string{
376 "--hide-annotation", "android.annotation.Hide",
Paul Duffin0c5bae52020-06-02 13:00:08 +0100377 // com.android.* classes are okay in this interface"
Paul Duffin15f34ef2020-07-20 18:04:44 +0100378 "--hide", "InternalClasses",
Paul Duffin0c5bae52020-06-02 13:00:08 +0100379 },
Jihoon Kang1c92c3e2023-03-23 17:44:51 +0000380 kind: android.SdkSystemServer,
Paul Duffin0c5bae52020-06-02 13:00:08 +0100381 })
Paul Duffind1b3a922020-01-22 11:57:20 +0000382 allApiScopes = apiScopes{
383 apiScopePublic,
384 apiScopeSystem,
385 apiScopeTest,
Paul Duffin8f265b92020-04-28 14:13:56 +0100386 apiScopeModuleLib,
Paul Duffin0c5bae52020-06-02 13:00:08 +0100387 apiScopeSystemServer,
Paul Duffind1b3a922020-01-22 11:57:20 +0000388 }
Jiyong Parkc678ad32018-04-10 13:07:10 +0900389)
390
Jiyong Park82484c02018-04-23 21:41:26 +0900391var (
392 javaSdkLibrariesLock sync.Mutex
393)
394
Jiyong Parkc678ad32018-04-10 13:07:10 +0900395// TODO: these are big features that are currently missing
Jiyong Park1be96912018-05-28 18:02:19 +0900396// 1) disallowing linking to the runtime shared lib
397// 2) HTML generation
Jiyong Parkc678ad32018-04-10 13:07:10 +0900398
399func init() {
Paul Duffin43dc1cc2019-12-19 11:18:54 +0000400 RegisterSdkLibraryBuildComponents(android.InitRegistrationContext)
Jiyong Parkc678ad32018-04-10 13:07:10 +0900401
Jiyong Park82484c02018-04-23 21:41:26 +0900402 android.RegisterMakeVarsProvider(pctx, func(ctx android.MakeVarsContext) {
403 javaSdkLibraries := javaSdkLibraries(ctx.Config())
404 sort.Strings(*javaSdkLibraries)
405 ctx.Strict("JAVA_SDK_LIBRARIES", strings.Join(*javaSdkLibraries, " "))
406 })
Paul Duffindd46f712020-02-10 13:37:10 +0000407
408 // Register sdk member types.
Paul Duffin976b0e52021-04-27 23:20:26 +0100409 android.RegisterSdkMemberType(javaSdkLibrarySdkMemberType)
Jiyong Parkc678ad32018-04-10 13:07:10 +0900410}
411
Paul Duffin43dc1cc2019-12-19 11:18:54 +0000412func RegisterSdkLibraryBuildComponents(ctx android.RegistrationContext) {
413 ctx.RegisterModuleType("java_sdk_library", SdkLibraryFactory)
414 ctx.RegisterModuleType("java_sdk_library_import", sdkLibraryImportFactory)
415}
416
Paul Duffin3375e352020-04-28 10:44:03 +0100417// Properties associated with each api scope.
418type ApiScopeProperties struct {
419 // Indicates whether the api surface is generated.
420 //
421 // If this is set for any scope then all scopes must explicitly specify if they
422 // are enabled. This is to prevent new usages from depending on legacy behavior.
423 //
424 // Otherwise, if this is not set for any scope then the default behavior is
425 // scope specific so please refer to the scope specific property documentation.
426 Enabled *bool
Paul Duffin87a05a32020-05-12 11:50:28 +0100427
428 // The sdk_version to use for building the stubs.
429 //
430 // If not specified then it will use an sdk_version determined as follows:
Trevor Radcliffedf8aa1f2021-11-04 14:25:39 +0000431 //
Paul Duffin87a05a32020-05-12 11:50:28 +0100432 // 1) If the sdk_version specified on the java_sdk_library is none then this
Trevor Radcliffedf8aa1f2021-11-04 14:25:39 +0000433 // will be none. This is used for java_sdk_library instances that are used
434 // to create stubs that contribute to the core_current sdk version.
435 // 2) Otherwise, it is assumed that this library extends but does not
436 // contribute directly to a specific sdk_version and so this uses the
437 // sdk_version appropriate for the api scope. e.g. public will use
438 // sdk_version: current, system will use sdk_version: system_current, etc.
Paul Duffin87a05a32020-05-12 11:50:28 +0100439 //
440 // This does not affect the sdk_version used for either generating the stubs source
441 // or the API file. They both have to use the same sdk_version as is used for
442 // compiling the implementation library.
443 Sdk_version *string
Paul Duffin3375e352020-04-28 10:44:03 +0100444}
445
Jiyong Parkc678ad32018-04-10 13:07:10 +0900446type sdkLibraryProperties struct {
Anton Hanssonf8ea3722021-09-16 14:24:13 +0100447 // List of source files that are needed to compile the API, but are not part of runtime library.
448 Api_srcs []string `android:"arch_variant"`
449
Paul Duffin5df79302020-05-16 15:52:12 +0100450 // Visibility for impl library module. If not specified then defaults to the
451 // visibility property.
452 Impl_library_visibility []string
453
Paul Duffin4911a892020-04-29 23:35:13 +0100454 // Visibility for stubs library modules. If not specified then defaults to the
455 // visibility property.
456 Stubs_library_visibility []string
457
458 // Visibility for stubs source modules. If not specified then defaults to the
459 // visibility property.
460 Stubs_source_visibility []string
461
Anton Hansson7f66efa2020-10-08 14:47:23 +0100462 // List of Java libraries that will be in the classpath when building the implementation lib
463 Impl_only_libs []string `android:"arch_variant"`
464
Paul Duffin77590a82022-04-28 14:13:30 +0000465 // List of Java libraries that will included in the implementation lib.
466 Impl_only_static_libs []string `android:"arch_variant"`
467
Sundong Ahnf043cf62018-06-25 16:04:37 +0900468 // List of Java libraries that will be in the classpath when building stubs
469 Stub_only_libs []string `android:"arch_variant"`
470
Anton Hanssondae54cd2021-04-21 16:30:10 +0100471 // List of Java libraries that will included in stub libraries
472 Stub_only_static_libs []string `android:"arch_variant"`
473
Paul Duffin7a586d32019-12-30 17:09:34 +0000474 // list of package names that will be documented and publicized as API.
475 // This allows the API to be restricted to a subset of the source files provided.
476 // If this is unspecified then all the source files will be treated as being part
477 // of the API.
Jiyong Parkc678ad32018-04-10 13:07:10 +0900478 Api_packages []string
479
Jiyong Park5a2c9d72018-05-01 22:25:41 +0900480 // list of package names that must be hidden from the API
481 Hidden_api_packages []string
482
Paul Duffin749f98f2019-12-30 17:23:46 +0000483 // the relative path to the directory containing the api specification files.
484 // Defaults to "api".
485 Api_dir *string
486
Paul Duffindfa131e2020-05-15 20:37:11 +0100487 // Determines whether a runtime implementation library is built; defaults to false.
488 //
489 // 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 +0200490 // it is as if shared_library: false, was set.
Paul Duffin43db9be2019-12-30 17:35:49 +0000491 Api_only *bool
492
Paul Duffin11512472019-02-11 15:55:17 +0000493 // local files that are used within user customized droiddoc options.
494 Droiddoc_option_files []string
495
Spandan Das93e95992021-07-29 18:26:39 +0000496 // additional droiddoc options.
Paul Duffin11512472019-02-11 15:55:17 +0000497 // Available variables for substitution:
498 //
499 // $(location <label>): the path to the droiddoc_option_files with name <label>
Sundong Ahndd567f92018-07-31 17:19:11 +0900500 Droiddoc_options []string
501
Paul Duffine22c2ab2020-05-20 19:35:27 +0100502 // is set to true, Metalava will allow framework SDK to contain annotations.
503 Annotations_enabled *bool
504
Sundong Ahn054b19a2018-10-19 13:46:09 +0900505 // a list of top-level directories containing files to merge qualifier annotations
506 // (i.e. those intended to be included in the stubs written) from.
507 Merge_annotations_dirs []string
508
509 // a list of top-level directories containing Java stub files to merge show/hide annotations from.
510 Merge_inclusion_annotations_dirs []string
511
Paul Duffin4f5c1ef2020-11-19 14:53:43 +0000512 // If set to true then don't create dist rules.
513 No_dist *bool
Sundong Ahn80a87b32019-05-13 15:02:50 +0900514
Paul Duffin31310252020-11-20 21:26:20 +0000515 // The stem for the artifacts that are copied to the dist, if not specified
516 // then defaults to the base module name.
517 //
518 // For each scope the following artifacts are copied to the apistubs/<scope>
519 // directory in the dist.
520 // * stubs impl jar -> <dist-stem>.jar
521 // * API specification file -> api/<dist-stem>.txt
522 // * Removed API specification file -> api/<dist-stem>-removed.txt
523 //
524 // Also used to construct the name of the filegroup (created by prebuilt_apis)
525 // that references the latest released API and remove API specification files.
526 // * API specification filegroup -> <dist-stem>.api.<scope>.latest
527 // * Removed API specification filegroup -> <dist-stem>-removed.api.<scope>.latest
Jaewoong Jung1a97ee02021-03-09 13:25:02 -0800528 // * API incompatibilities baseline filegroup -> <dist-stem>-incompatibilities.api.<scope>.latest
Paul Duffin31310252020-11-20 21:26:20 +0000529 Dist_stem *string
530
Colin Cross986b69a2021-06-01 13:13:40 -0700531 // The subdirectory for the artifacts that are copied to the dist directory. If not specified
Colin Cross3dd66252021-06-01 14:05:09 -0700532 // then defaults to "unknown". Should be set to "android" for anything that should be published
Colin Cross986b69a2021-06-01 13:13:40 -0700533 // in the public Android SDK.
534 Dist_group *string
535
Anton Hanssondff2c782020-12-21 17:10:01 +0000536 // A compatibility mode that allows historical API-tracking files to not exist.
537 // Do not use.
538 Unsafe_ignore_missing_latest_api bool
539
Paul Duffin3375e352020-04-28 10:44:03 +0100540 // indicates whether system and test apis should be generated.
541 Generate_system_and_test_apis bool `blueprint:"mutated"`
542
543 // The properties specific to the public api scope
544 //
545 // Unless explicitly specified by using public.enabled the public api scope is
546 // enabled by default in both legacy and non-legacy mode.
547 Public ApiScopeProperties
548
549 // The properties specific to the system api scope
550 //
551 // In legacy mode the system api scope is enabled by default when sdk_version
552 // is set to something other than "none".
553 //
554 // In non-legacy mode the system api scope is disabled by default.
555 System ApiScopeProperties
556
557 // The properties specific to the test api scope
558 //
559 // In legacy mode the test api scope is enabled by default when sdk_version
560 // is set to something other than "none".
561 //
562 // In non-legacy mode the test api scope is disabled by default.
563 Test ApiScopeProperties
Paul Duffin37e0b772019-12-30 17:20:10 +0000564
Paul Duffin0c5bae52020-06-02 13:00:08 +0100565 // The properties specific to the module-lib api scope
Paul Duffin8f265b92020-04-28 14:13:56 +0100566 //
Zi Wangb2179e32023-01-31 15:53:30 -0800567 // Unless explicitly specified by using module_lib.enabled the module_lib api
568 // scope is disabled by default.
Paul Duffin8f265b92020-04-28 14:13:56 +0100569 Module_lib ApiScopeProperties
570
Paul Duffin0c5bae52020-06-02 13:00:08 +0100571 // The properties specific to the system-server api scope
572 //
Zi Wangb2179e32023-01-31 15:53:30 -0800573 // Unless explicitly specified by using system_server.enabled the
574 // system_server api scope is disabled by default.
Paul Duffin0c5bae52020-06-02 13:00:08 +0100575 System_server ApiScopeProperties
576
Jiyong Park932cdfe2020-05-28 00:19:53 +0900577 // Determines if the stubs are preferred over the implementation library
578 // for linking, even when the client doesn't specify sdk_version. When this
579 // is set to true, such clients are provided with the widest API surface that
580 // this lib provides. Note however that this option doesn't affect the clients
581 // that are in the same APEX as this library. In that case, the clients are
582 // always linked with the implementation library. Default is false.
583 Default_to_stubs *bool
584
Paul Duffin160fe412020-05-10 19:32:20 +0100585 // Properties related to api linting.
586 Api_lint struct {
587 // Enable api linting.
588 Enabled *bool
589 }
590
Jiyong Parkc678ad32018-04-10 13:07:10 +0900591 // TODO: determines whether to create HTML doc or not
Paul Duffine8409952022-09-22 16:24:46 +0100592 // Html_doc *bool
Jiyong Parkc678ad32018-04-10 13:07:10 +0900593}
594
Paul Duffin0f8faff2020-05-20 16:18:00 +0100595// Paths to outputs from java_sdk_library and java_sdk_library_import.
596//
597// Fields that are android.Paths are always set (during GenerateAndroidBuildActions).
598// OptionalPaths are always set by java_sdk_library but may not be set by
599// java_sdk_library_import as not all instances provide that information.
Paul Duffind1b3a922020-01-22 11:57:20 +0000600type scopePaths struct {
Paul Duffin0f8faff2020-05-20 16:18:00 +0100601 // The path (represented as Paths for convenience when returning) to the stubs header jar.
602 //
603 // That is the jar that is created by turbine.
604 stubsHeaderPath android.Paths
605
606 // The path (represented as Paths for convenience when returning) to the stubs implementation jar.
607 //
608 // This is not the implementation jar, it still only contains stubs.
609 stubsImplPath android.Paths
610
Paul Duffin1267d872021-04-16 17:21:36 +0100611 // The dex jar for the stubs.
612 //
613 // This is not the implementation jar, it still only contains stubs.
Martin Stjernholm8be1e6d2021-09-15 03:34:04 +0100614 stubsDexJarPath OptionalDexJarPath
Paul Duffin1267d872021-04-16 17:21:36 +0100615
Paul Duffin0f8faff2020-05-20 16:18:00 +0100616 // The API specification file, e.g. system_current.txt.
617 currentApiFilePath android.OptionalPath
618
619 // The specification of API elements removed since the last release.
620 removedApiFilePath android.OptionalPath
621
622 // The stubs source jar.
623 stubsSrcJar android.OptionalPath
Anton Hanssond78eb762021-09-21 15:25:12 +0100624
625 // Extracted annotations.
626 annotationsZip android.OptionalPath
Paul Duffin958806b2022-05-16 13:10:47 +0000627
628 // The path to the latest API file.
629 latestApiPath android.OptionalPath
630
631 // The path to the latest removed API file.
632 latestRemovedApiPath android.OptionalPath
Paul Duffind1b3a922020-01-22 11:57:20 +0000633}
634
Colin Crossdcf71b22021-02-01 13:59:03 -0800635func (paths *scopePaths) extractStubsLibraryInfoFromDependency(ctx android.ModuleContext, dep android.Module) error {
636 if ctx.OtherModuleHasProvider(dep, JavaInfoProvider) {
637 lib := ctx.OtherModuleProvider(dep, JavaInfoProvider).(JavaInfo)
638 paths.stubsHeaderPath = lib.HeaderJars
639 paths.stubsImplPath = lib.ImplementationJars
Paul Duffin1267d872021-04-16 17:21:36 +0100640
641 libDep := dep.(UsesLibraryDependency)
642 paths.stubsDexJarPath = libDep.DexJarBuildPath()
Paul Duffinc8782502020-04-29 20:45:27 +0100643 return nil
644 } else {
Colin Crossdcf71b22021-02-01 13:59:03 -0800645 return fmt.Errorf("expected module that has JavaInfoProvider, e.g. java_library")
Paul Duffinc8782502020-04-29 20:45:27 +0100646 }
647}
648
Paul Duffin0ff08bd2020-04-29 13:30:54 +0100649func (paths *scopePaths) treatDepAsApiStubsProvider(dep android.Module, action func(provider ApiStubsProvider)) error {
650 if apiStubsProvider, ok := dep.(ApiStubsProvider); ok {
651 action(apiStubsProvider)
Paul Duffinc8782502020-04-29 20:45:27 +0100652 return nil
653 } else {
654 return fmt.Errorf("expected module that implements ApiStubsProvider, e.g. droidstubs")
655 }
656}
657
Paul Duffin0f8faff2020-05-20 16:18:00 +0100658func (paths *scopePaths) treatDepAsApiStubsSrcProvider(dep android.Module, action func(provider ApiStubsSrcProvider)) error {
659 if apiStubsProvider, ok := dep.(ApiStubsSrcProvider); ok {
660 action(apiStubsProvider)
661 return nil
662 } else {
663 return fmt.Errorf("expected module that implements ApiStubsSrcProvider, e.g. droidstubs")
664 }
665}
666
Paul Duffin0ff08bd2020-04-29 13:30:54 +0100667func (paths *scopePaths) extractApiInfoFromApiStubsProvider(provider ApiStubsProvider) {
Anton Hanssond78eb762021-09-21 15:25:12 +0100668 paths.annotationsZip = android.OptionalPathForPath(provider.AnnotationsZip())
Paul Duffin0f8faff2020-05-20 16:18:00 +0100669 paths.currentApiFilePath = android.OptionalPathForPath(provider.ApiFilePath())
670 paths.removedApiFilePath = android.OptionalPathForPath(provider.RemovedApiFilePath())
Paul Duffin0ff08bd2020-04-29 13:30:54 +0100671}
672
Colin Crossdcf71b22021-02-01 13:59:03 -0800673func (paths *scopePaths) extractApiInfoFromDep(ctx android.ModuleContext, dep android.Module) error {
Paul Duffin0ff08bd2020-04-29 13:30:54 +0100674 return paths.treatDepAsApiStubsProvider(dep, func(provider ApiStubsProvider) {
675 paths.extractApiInfoFromApiStubsProvider(provider)
676 })
677}
678
Paul Duffin0f8faff2020-05-20 16:18:00 +0100679func (paths *scopePaths) extractStubsSourceInfoFromApiStubsProviders(provider ApiStubsSrcProvider) {
680 paths.stubsSrcJar = android.OptionalPathForPath(provider.StubsSrcJar())
Paul Duffin0ff08bd2020-04-29 13:30:54 +0100681}
682
Colin Crossdcf71b22021-02-01 13:59:03 -0800683func (paths *scopePaths) extractStubsSourceInfoFromDep(ctx android.ModuleContext, dep android.Module) error {
Paul Duffin0f8faff2020-05-20 16:18:00 +0100684 return paths.treatDepAsApiStubsSrcProvider(dep, func(provider ApiStubsSrcProvider) {
Paul Duffin0ff08bd2020-04-29 13:30:54 +0100685 paths.extractStubsSourceInfoFromApiStubsProviders(provider)
686 })
687}
688
Colin Crossdcf71b22021-02-01 13:59:03 -0800689func (paths *scopePaths) extractStubsSourceAndApiInfoFromApiStubsProvider(ctx android.ModuleContext, dep android.Module) error {
Paul Duffin0ff08bd2020-04-29 13:30:54 +0100690 return paths.treatDepAsApiStubsProvider(dep, func(provider ApiStubsProvider) {
691 paths.extractApiInfoFromApiStubsProvider(provider)
692 paths.extractStubsSourceInfoFromApiStubsProviders(provider)
693 })
694}
695
Paul Duffin958806b2022-05-16 13:10:47 +0000696func extractSingleOptionalOutputPath(dep android.Module) (android.OptionalPath, error) {
697 var paths android.Paths
698 if sourceFileProducer, ok := dep.(android.SourceFileProducer); ok {
699 paths = sourceFileProducer.Srcs()
700 } else {
701 return android.OptionalPath{}, fmt.Errorf("module %q does not produce source files", dep)
702 }
703 if len(paths) != 1 {
704 return android.OptionalPath{}, fmt.Errorf("expected one path from %q, got %q", dep, paths)
705 }
706 return android.OptionalPathForPath(paths[0]), nil
707}
708
709func (paths *scopePaths) extractLatestApiPath(ctx android.ModuleContext, dep android.Module) error {
710 outputPath, err := extractSingleOptionalOutputPath(dep)
711 paths.latestApiPath = outputPath
712 return err
713}
714
715func (paths *scopePaths) extractLatestRemovedApiPath(ctx android.ModuleContext, dep android.Module) error {
716 outputPath, err := extractSingleOptionalOutputPath(dep)
717 paths.latestRemovedApiPath = outputPath
718 return err
719}
720
Paul Duffin0ff08bd2020-04-29 13:30:54 +0100721type commonToSdkLibraryAndImportProperties struct {
Paul Duffin1b1e8062020-05-08 13:44:43 +0100722 // The naming scheme to use for the components that this module creates.
723 //
Paul Duffinee9ad5d2020-09-11 13:04:05 +0100724 // If not specified then it defaults to "default".
Paul Duffin1b1e8062020-05-08 13:44:43 +0100725 //
726 // This is a temporary mechanism to simplify conversion from separate modules for each
727 // component that follow a different naming pattern to the default one.
728 //
729 // TODO(b/155480189) - Remove once naming inconsistencies have been resolved.
Paul Duffin0ff08bd2020-04-29 13:30:54 +0100730 Naming_scheme *string
Paul Duffindfa131e2020-05-15 20:37:11 +0100731
732 // Specifies whether this module can be used as an Android shared library; defaults
733 // to true.
734 //
735 // An Android shared library is one that can be referenced in a <uses-library> element
736 // in an AndroidManifest.xml.
737 Shared_library *bool
Paul Duffina2ae7e02020-09-11 11:55:00 +0100738
739 // Files containing information about supported java doc tags.
740 Doctag_files []string `android:"path"`
Pedro Loureiro9956e5e2021-09-07 17:21:59 +0000741
742 // Signals that this shared library is part of the bootclasspath starting
743 // on the version indicated in this attribute.
744 //
745 // This will make platforms at this level and above to ignore
746 // <uses-library> tags with this library name because the library is already
747 // available
748 On_bootclasspath_since *string
749
750 // Signals that this shared library was part of the bootclasspath before
751 // (but not including) the version indicated in this attribute.
752 //
753 // The system will automatically add a <uses-library> tag with this library to
754 // apps that target any SDK less than the version indicated in this attribute.
755 On_bootclasspath_before *string
756
757 // Indicates that PackageManager should ignore this shared library if the
758 // platform is below the version indicated in this attribute.
759 //
760 // This means that the device won't recognise this library as installed.
761 Min_device_sdk *string
762
763 // Indicates that PackageManager should ignore this shared library if the
764 // platform is above the version indicated in this attribute.
765 //
766 // This means that the device won't recognise this library as installed.
767 Max_device_sdk *string
Paul Duffin0ff08bd2020-04-29 13:30:54 +0100768}
769
Paul Duffin71b33cc2021-06-23 11:39:47 +0100770// commonSdkLibraryAndImportModule defines the interface that must be provided by a module that
771// embeds the commonToSdkLibraryAndImport struct.
772type commonSdkLibraryAndImportModule interface {
Paul Duffind796f6f2022-11-23 23:06:05 +0000773 android.Module
Paul Duffin71b33cc2021-06-23 11:39:47 +0100774
775 BaseModuleName() string
776}
777
Paul Duffin56d44902020-01-31 13:36:25 +0000778// Common code between sdk library and sdk library import
779type commonToSdkLibraryAndImport struct {
Paul Duffin71b33cc2021-06-23 11:39:47 +0100780 module commonSdkLibraryAndImportModule
Paul Duffinc3091c82020-05-08 14:16:20 +0100781
Paul Duffin56d44902020-01-31 13:36:25 +0000782 scopePaths map[*apiScope]*scopePaths
Paul Duffin1b1e8062020-05-08 13:44:43 +0100783
784 namingScheme sdkLibraryComponentNamingScheme
785
Paul Duffindfa131e2020-05-15 20:37:11 +0100786 commonSdkLibraryProperties commonToSdkLibraryAndImportProperties
Paul Duffin859fe962020-05-15 10:20:31 +0100787
Paul Duffina2ae7e02020-09-11 11:55:00 +0100788 // Paths to commonSdkLibraryProperties.Doctag_files
789 doctagPaths android.Paths
790
Paul Duffin859fe962020-05-15 10:20:31 +0100791 // Functionality related to this being used as a component of a java_sdk_library.
792 EmbeddableSdkLibraryComponent
Paul Duffin56d44902020-01-31 13:36:25 +0000793}
794
Paul Duffin71b33cc2021-06-23 11:39:47 +0100795func (c *commonToSdkLibraryAndImport) initCommon(module commonSdkLibraryAndImportModule) {
796 c.module = module
Paul Duffin1b1e8062020-05-08 13:44:43 +0100797
Paul Duffin71b33cc2021-06-23 11:39:47 +0100798 module.AddProperties(&c.commonSdkLibraryProperties)
Paul Duffin859fe962020-05-15 10:20:31 +0100799
800 // Initialize this as an sdk library component.
Paul Duffin71b33cc2021-06-23 11:39:47 +0100801 c.initSdkLibraryComponent(module)
Paul Duffin1b1e8062020-05-08 13:44:43 +0100802}
803
804func (c *commonToSdkLibraryAndImport) initCommonAfterDefaultsApplied(ctx android.DefaultableHookContext) bool {
Paul Duffindfa131e2020-05-15 20:37:11 +0100805 schemeProperty := proptools.StringDefault(c.commonSdkLibraryProperties.Naming_scheme, "default")
Paul Duffin1b1e8062020-05-08 13:44:43 +0100806 switch schemeProperty {
807 case "default":
808 c.namingScheme = &defaultNamingScheme{}
809 default:
810 ctx.PropertyErrorf("naming_scheme", "expected 'default' but was %q", schemeProperty)
811 return false
812 }
813
Paul Duffin3f0290e2021-06-30 18:25:36 +0100814 namePtr := proptools.StringPtr(c.module.BaseModuleName())
815 c.sdkLibraryComponentProperties.SdkLibraryName = namePtr
816
Paul Duffindfa131e2020-05-15 20:37:11 +0100817 // Only track this sdk library if this can be used as a shared library.
818 if c.sharedLibrary() {
819 // Use the name specified in the module definition as the owner.
Paul Duffin3f0290e2021-06-30 18:25:36 +0100820 c.sdkLibraryComponentProperties.SdkLibraryToImplicitlyTrack = namePtr
Paul Duffindfa131e2020-05-15 20:37:11 +0100821 }
Paul Duffin859fe962020-05-15 10:20:31 +0100822
Paul Duffin1b1e8062020-05-08 13:44:43 +0100823 return true
Paul Duffinc3091c82020-05-08 14:16:20 +0100824}
825
Paul Duffinea8f8082021-06-24 13:25:57 +0100826// uniqueApexVariations provides common implementation of the ApexModule.UniqueApexVariations
827// method.
828func (c *commonToSdkLibraryAndImport) uniqueApexVariations() bool {
829 // A java_sdk_library that is a shared library produces an XML file that makes the shared library
830 // usable from an AndroidManifest.xml's <uses-library> entry. That XML file contains the name of
831 // the APEX and so it needs a unique variation per APEX.
832 return c.sharedLibrary()
833}
834
Paul Duffina2ae7e02020-09-11 11:55:00 +0100835func (c *commonToSdkLibraryAndImport) generateCommonBuildActions(ctx android.ModuleContext) {
836 c.doctagPaths = android.PathsForModuleSrc(ctx, c.commonSdkLibraryProperties.Doctag_files)
837}
838
Paul Duffineedc5d52020-06-12 17:46:39 +0100839// Module name of the runtime implementation library
840func (c *commonToSdkLibraryAndImport) implLibraryModuleName() string {
Paul Duffin71b33cc2021-06-23 11:39:47 +0100841 return c.module.BaseModuleName() + ".impl"
Paul Duffineedc5d52020-06-12 17:46:39 +0100842}
843
844// Module name of the XML file for the lib
845func (c *commonToSdkLibraryAndImport) xmlPermissionsModuleName() string {
Paul Duffin71b33cc2021-06-23 11:39:47 +0100846 return c.module.BaseModuleName() + sdkXmlFileSuffix
Paul Duffineedc5d52020-06-12 17:46:39 +0100847}
848
Paul Duffinc3091c82020-05-08 14:16:20 +0100849// Name of the java_library module that compiles the stubs source.
850func (c *commonToSdkLibraryAndImport) stubsLibraryModuleName(apiScope *apiScope) string {
Paul Duffinb97b1572021-04-29 21:50:40 +0100851 baseName := c.module.BaseModuleName()
Paul Duffin21787622022-11-25 12:48:20 +0000852 return c.namingScheme.stubsLibraryModuleName(apiScope, baseName)
Paul Duffinc3091c82020-05-08 14:16:20 +0100853}
854
855// Name of the droidstubs module that generates the stubs source and may also
856// generate/check the API.
857func (c *commonToSdkLibraryAndImport) stubsSourceModuleName(apiScope *apiScope) string {
Paul Duffinb97b1572021-04-29 21:50:40 +0100858 baseName := c.module.BaseModuleName()
Paul Duffin21787622022-11-25 12:48:20 +0000859 return c.namingScheme.stubsSourceModuleName(apiScope, baseName)
Paul Duffinc3091c82020-05-08 14:16:20 +0100860}
861
Jihoon Kang1c92c3e2023-03-23 17:44:51 +0000862// Name of the java_api_library module that generates the from-text stubs source
863// and compiles to a jar file.
864func (c *commonToSdkLibraryAndImport) apiLibraryModuleName(apiScope *apiScope) string {
865 baseName := c.module.BaseModuleName()
866 return c.namingScheme.apiLibraryModuleName(apiScope, baseName)
867}
868
Jihoon Kang1147b312023-06-08 23:25:57 +0000869// Name of the java_library module that compiles the stubs
870// generated from source Java files.
871func (c *commonToSdkLibraryAndImport) sourceStubLibraryModuleName(apiScope *apiScope) string {
872 baseName := c.module.BaseModuleName()
873 return c.namingScheme.sourceStubLibraryModuleName(apiScope, baseName)
874}
875
Paul Duffin46dc45a2020-05-14 15:39:10 +0100876// The component names for different outputs of the java_sdk_library.
877//
878// They are similar to the names used for the child modules it creates
879const (
880 stubsSourceComponentName = "stubs.source"
881
882 apiTxtComponentName = "api.txt"
883
884 removedApiTxtComponentName = "removed-api.txt"
Anton Hanssond78eb762021-09-21 15:25:12 +0100885
886 annotationsComponentName = "annotations.zip"
Paul Duffin46dc45a2020-05-14 15:39:10 +0100887)
888
889// A regular expression to match tags that reference a specific stubs component.
890//
891// It will only match if given a valid scope and a valid component. It is verfy strict
892// to ensure it does not accidentally match a similar looking tag that should be processed
893// by the embedded Library.
894var tagSplitter = func() *regexp.Regexp {
895 // Given a list of literal string items returns a regular expression that will
896 // match any one of the items.
897 choice := func(items ...string) string {
898 return `\Q` + strings.Join(items, `\E|\Q`) + `\E`
899 }
900
901 // Regular expression to match one of the scopes.
902 scopesRegexp := choice(allScopeNames...)
903
904 // Regular expression to match one of the components.
Anton Hanssond78eb762021-09-21 15:25:12 +0100905 componentsRegexp := choice(stubsSourceComponentName, apiTxtComponentName, removedApiTxtComponentName, annotationsComponentName)
Paul Duffin46dc45a2020-05-14 15:39:10 +0100906
907 // Regular expression to match any combination of one scope and one component.
908 return regexp.MustCompile(fmt.Sprintf(`^\.(%s)\.(%s)$`, scopesRegexp, componentsRegexp))
909}()
910
911// For OutputFileProducer interface
912//
Anton Hanssond78eb762021-09-21 15:25:12 +0100913// .<scope>.<component name>, for all ComponentNames (for example: .public.removed-api.txt)
Paul Duffin46dc45a2020-05-14 15:39:10 +0100914func (c *commonToSdkLibraryAndImport) commonOutputFiles(tag string) (android.Paths, error) {
915 if groups := tagSplitter.FindStringSubmatch(tag); groups != nil {
916 scopeName := groups[1]
917 component := groups[2]
918
919 if scope, ok := scopeByName[scopeName]; ok {
920 paths := c.findScopePaths(scope)
921 if paths == nil {
Paul Duffin71b33cc2021-06-23 11:39:47 +0100922 return nil, fmt.Errorf("%q does not provide api scope %s", c.module.BaseModuleName(), scopeName)
Paul Duffin46dc45a2020-05-14 15:39:10 +0100923 }
924
925 switch component {
926 case stubsSourceComponentName:
927 if paths.stubsSrcJar.Valid() {
928 return android.Paths{paths.stubsSrcJar.Path()}, nil
929 }
930
931 case apiTxtComponentName:
932 if paths.currentApiFilePath.Valid() {
933 return android.Paths{paths.currentApiFilePath.Path()}, nil
934 }
935
936 case removedApiTxtComponentName:
937 if paths.removedApiFilePath.Valid() {
938 return android.Paths{paths.removedApiFilePath.Path()}, nil
939 }
Anton Hanssond78eb762021-09-21 15:25:12 +0100940
941 case annotationsComponentName:
942 if paths.annotationsZip.Valid() {
943 return android.Paths{paths.annotationsZip.Path()}, nil
944 }
Paul Duffin46dc45a2020-05-14 15:39:10 +0100945 }
946
947 return nil, fmt.Errorf("%s not available for api scope %s", component, scopeName)
948 } else {
949 return nil, fmt.Errorf("unknown scope %s in %s", scope, tag)
950 }
951
952 } else {
Paul Duffina2ae7e02020-09-11 11:55:00 +0100953 switch tag {
954 case ".doctags":
955 if c.doctagPaths != nil {
956 return c.doctagPaths, nil
957 } else {
Paul Duffin71b33cc2021-06-23 11:39:47 +0100958 return nil, fmt.Errorf("no doctag_files specified on %s", c.module.BaseModuleName())
Paul Duffina2ae7e02020-09-11 11:55:00 +0100959 }
960 }
Paul Duffin46dc45a2020-05-14 15:39:10 +0100961 return nil, nil
962 }
963}
964
Paul Duffin803a9562020-05-20 11:52:25 +0100965func (c *commonToSdkLibraryAndImport) getScopePathsCreateIfNeeded(scope *apiScope) *scopePaths {
Paul Duffin56d44902020-01-31 13:36:25 +0000966 if c.scopePaths == nil {
967 c.scopePaths = make(map[*apiScope]*scopePaths)
968 }
969 paths := c.scopePaths[scope]
970 if paths == nil {
971 paths = &scopePaths{}
972 c.scopePaths[scope] = paths
973 }
974
975 return paths
976}
977
Paul Duffin803a9562020-05-20 11:52:25 +0100978func (c *commonToSdkLibraryAndImport) findScopePaths(scope *apiScope) *scopePaths {
979 if c.scopePaths == nil {
980 return nil
981 }
982
983 return c.scopePaths[scope]
984}
985
986// If this does not support the requested api scope then find the closest available
987// scope it does support. Returns nil if no such scope is available.
988func (c *commonToSdkLibraryAndImport) findClosestScopePath(scope *apiScope) *scopePaths {
Paul Duffind0b9fca2022-09-30 18:11:41 +0100989 for s := scope; s != nil; s = s.canAccess {
Paul Duffin803a9562020-05-20 11:52:25 +0100990 if paths := c.findScopePaths(s); paths != nil {
991 return paths
992 }
993 }
994
995 // This should never happen outside tests as public should be the base scope for every
996 // scope and is enabled by default.
997 return nil
998}
999
Jiyong Parkf1691d22021-03-29 20:11:58 +09001000func (c *commonToSdkLibraryAndImport) selectHeaderJarsForSdkVersion(ctx android.BaseModuleContext, sdkVersion android.SdkSpec) android.Paths {
Paul Duffinb05d4292020-05-20 12:19:10 +01001001
1002 // If a specific numeric version has been requested then use prebuilt versions of the sdk.
Jiyong Park54105c42021-03-31 18:17:53 +09001003 if !sdkVersion.ApiLevel.IsPreview() {
Paul Duffin71b33cc2021-06-23 11:39:47 +01001004 return PrebuiltJars(ctx, c.module.BaseModuleName(), sdkVersion)
Paul Duffinb05d4292020-05-20 12:19:10 +01001005 }
1006
Paul Duffin1267d872021-04-16 17:21:36 +01001007 paths := c.selectScopePaths(ctx, sdkVersion.Kind)
1008 if paths == nil {
1009 return nil
1010 }
1011
1012 return paths.stubsHeaderPath
1013}
1014
1015// selectScopePaths returns the *scopePaths appropriate for the specific kind.
1016//
1017// If the module does not support the specific kind then it will return the *scopePaths for the
1018// closest kind which is a subset of the requested kind. e.g. if requesting android.SdkModule then
1019// it will return *scopePaths for android.SdkSystem if available or android.SdkPublic of not.
1020func (c *commonToSdkLibraryAndImport) selectScopePaths(ctx android.BaseModuleContext, kind android.SdkKind) *scopePaths {
Paul Duffin32cf58a2021-05-18 16:32:50 +01001021 apiScope := sdkKindToApiScope(kind)
Paul Duffinb05d4292020-05-20 12:19:10 +01001022
Paul Duffin803a9562020-05-20 11:52:25 +01001023 paths := c.findClosestScopePath(apiScope)
1024 if paths == nil {
1025 var scopes []string
1026 for _, s := range allApiScopes {
1027 if c.findScopePaths(s) != nil {
1028 scopes = append(scopes, s.name)
1029 }
1030 }
Paul Duffin71b33cc2021-06-23 11:39:47 +01001031 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 +01001032 return nil
1033 }
1034
Paul Duffin1267d872021-04-16 17:21:36 +01001035 return paths
1036}
1037
Paul Duffin32cf58a2021-05-18 16:32:50 +01001038// sdkKindToApiScope maps from android.SdkKind to apiScope.
1039func sdkKindToApiScope(kind android.SdkKind) *apiScope {
1040 var apiScope *apiScope
1041 switch kind {
1042 case android.SdkSystem:
1043 apiScope = apiScopeSystem
1044 case android.SdkModule:
1045 apiScope = apiScopeModuleLib
1046 case android.SdkTest:
1047 apiScope = apiScopeTest
1048 case android.SdkSystemServer:
1049 apiScope = apiScopeSystemServer
1050 default:
1051 apiScope = apiScopePublic
1052 }
1053 return apiScope
1054}
1055
Paul Duffin1267d872021-04-16 17:21:36 +01001056// to satisfy SdkLibraryDependency interface
Martin Stjernholm8be1e6d2021-09-15 03:34:04 +01001057func (c *commonToSdkLibraryAndImport) SdkApiStubDexJar(ctx android.BaseModuleContext, kind android.SdkKind) OptionalDexJarPath {
Paul Duffin1267d872021-04-16 17:21:36 +01001058 paths := c.selectScopePaths(ctx, kind)
1059 if paths == nil {
Martin Stjernholm8be1e6d2021-09-15 03:34:04 +01001060 return makeUnsetDexJarPath()
Paul Duffin1267d872021-04-16 17:21:36 +01001061 }
1062
1063 return paths.stubsDexJarPath
Paul Duffinb05d4292020-05-20 12:19:10 +01001064}
1065
Paul Duffin32cf58a2021-05-18 16:32:50 +01001066// to satisfy SdkLibraryDependency interface
1067func (c *commonToSdkLibraryAndImport) SdkRemovedTxtFile(ctx android.BaseModuleContext, kind android.SdkKind) android.OptionalPath {
1068 apiScope := sdkKindToApiScope(kind)
1069 paths := c.findScopePaths(apiScope)
1070 if paths == nil {
1071 return android.OptionalPath{}
1072 }
1073
1074 return paths.removedApiFilePath
1075}
1076
Paul Duffin859fe962020-05-15 10:20:31 +01001077func (c *commonToSdkLibraryAndImport) sdkComponentPropertiesForChildLibrary() interface{} {
1078 componentProps := &struct {
Paul Duffin3f0290e2021-06-30 18:25:36 +01001079 SdkLibraryName *string
Paul Duffin859fe962020-05-15 10:20:31 +01001080 SdkLibraryToImplicitlyTrack *string
Paul Duffindfa131e2020-05-15 20:37:11 +01001081 }{}
1082
Paul Duffin3f0290e2021-06-30 18:25:36 +01001083 namePtr := proptools.StringPtr(c.module.BaseModuleName())
1084 componentProps.SdkLibraryName = namePtr
1085
Paul Duffindfa131e2020-05-15 20:37:11 +01001086 if c.sharedLibrary() {
Paul Duffin859fe962020-05-15 10:20:31 +01001087 // Mark the stubs library as being components of this java_sdk_library so that
1088 // any app that includes code which depends (directly or indirectly) on the stubs
1089 // library will have the appropriate <uses-library> invocation inserted into its
1090 // manifest if necessary.
Paul Duffin3f0290e2021-06-30 18:25:36 +01001091 componentProps.SdkLibraryToImplicitlyTrack = namePtr
Paul Duffin859fe962020-05-15 10:20:31 +01001092 }
1093
1094 return componentProps
1095}
1096
Paul Duffindfa131e2020-05-15 20:37:11 +01001097func (c *commonToSdkLibraryAndImport) sharedLibrary() bool {
1098 return proptools.BoolDefault(c.commonSdkLibraryProperties.Shared_library, true)
1099}
1100
Paul Duffinf4600f62021-05-13 22:34:45 +01001101// Check if the stub libraries should be compiled for dex
1102func (c *commonToSdkLibraryAndImport) stubLibrariesCompiledForDex() bool {
1103 // Always compile the dex file files for the stub libraries if they will be used on the
1104 // bootclasspath.
1105 return !c.sharedLibrary()
1106}
1107
Paul Duffin859fe962020-05-15 10:20:31 +01001108// Properties related to the use of a module as an component of a java_sdk_library.
1109type SdkLibraryComponentProperties struct {
Paul Duffin3f0290e2021-06-30 18:25:36 +01001110 // The name of the java_sdk_library/_import module.
1111 SdkLibraryName *string `blueprint:"mutated"`
Paul Duffin859fe962020-05-15 10:20:31 +01001112
1113 // The name of the java_sdk_library/_import to add to a <uses-library> entry
1114 // in the AndroidManifest.xml of any Android app that includes code that references
1115 // this module. If not set then no java_sdk_library/_import is tracked.
1116 SdkLibraryToImplicitlyTrack *string `blueprint:"mutated"`
1117}
1118
1119// Structure to be embedded in a module struct that needs to support the
1120// SdkLibraryComponentDependency interface.
1121type EmbeddableSdkLibraryComponent struct {
1122 sdkLibraryComponentProperties SdkLibraryComponentProperties
1123}
1124
Paul Duffin71b33cc2021-06-23 11:39:47 +01001125func (e *EmbeddableSdkLibraryComponent) initSdkLibraryComponent(module android.Module) {
1126 module.AddProperties(&e.sdkLibraryComponentProperties)
Paul Duffin859fe962020-05-15 10:20:31 +01001127}
1128
1129// to satisfy SdkLibraryComponentDependency
Paul Duffin3f0290e2021-06-30 18:25:36 +01001130func (e *EmbeddableSdkLibraryComponent) SdkLibraryName() *string {
1131 return e.sdkLibraryComponentProperties.SdkLibraryName
1132}
1133
1134// to satisfy SdkLibraryComponentDependency
Ulya Trafimovich39b437b2020-09-23 16:42:35 +01001135func (e *EmbeddableSdkLibraryComponent) OptionalSdkLibraryImplementation() *string {
Ulya Trafimovich78645fb2021-07-16 15:29:25 +01001136 // For shared libraries, this is the same as the SDK library name. If a Java library or app
1137 // depends on a component library (e.g. a stub library) it still needs to know the name of the
1138 // run-time library and the corresponding module that provides the implementation. This name is
1139 // passed to manifest_fixer (to be added to AndroidManifest.xml) and added to CLC (to be used
1140 // in dexpreopt).
1141 //
1142 // For non-shared SDK (component or not) libraries this returns `nil`, as they are not
1143 // <uses-library> and should not be added to the manifest or to CLC.
Ulya Trafimovich39b437b2020-09-23 16:42:35 +01001144 return e.sdkLibraryComponentProperties.SdkLibraryToImplicitlyTrack
1145}
1146
Paul Duffin859fe962020-05-15 10:20:31 +01001147// Implemented by modules that are (or possibly could be) a component of a java_sdk_library
1148// (including the java_sdk_library) itself.
1149type SdkLibraryComponentDependency interface {
Ulya Trafimovich31e444e2020-08-14 17:32:16 +01001150 UsesLibraryDependency
1151
Paul Duffin3f0290e2021-06-30 18:25:36 +01001152 // SdkLibraryName returns the name of the java_sdk_library/_import module.
1153 SdkLibraryName() *string
1154
Ulya Trafimovich39b437b2020-09-23 16:42:35 +01001155 // The name of the implementation library for the optional SDK library or nil, if there isn't one.
1156 OptionalSdkLibraryImplementation() *string
Paul Duffin859fe962020-05-15 10:20:31 +01001157}
1158
1159// Make sure that all the module types that are components of java_sdk_library/_import
1160// and which can be referenced (directly or indirectly) from an android app implement
1161// the SdkLibraryComponentDependency interface.
1162var _ SdkLibraryComponentDependency = (*Library)(nil)
1163var _ SdkLibraryComponentDependency = (*Import)(nil)
1164var _ SdkLibraryComponentDependency = (*SdkLibrary)(nil)
Paul Duffineedc5d52020-06-12 17:46:39 +01001165var _ SdkLibraryComponentDependency = (*SdkLibraryImport)(nil)
Paul Duffin859fe962020-05-15 10:20:31 +01001166
Paul Duffin32cf58a2021-05-18 16:32:50 +01001167// Provides access to sdk_version related files, e.g. header and implementation jars.
Paul Duffin859fe962020-05-15 10:20:31 +01001168type SdkLibraryDependency interface {
1169 SdkLibraryComponentDependency
1170
1171 // Get the header jars appropriate for the supplied sdk_version.
1172 //
1173 // These are turbine generated jars so they only change if the externals of the
1174 // class changes but it does not contain and implementation or JavaDoc.
Jiyong Parkf1691d22021-03-29 20:11:58 +09001175 SdkHeaderJars(ctx android.BaseModuleContext, sdkVersion android.SdkSpec) android.Paths
Paul Duffin859fe962020-05-15 10:20:31 +01001176
1177 // Get the implementation jars appropriate for the supplied sdk version.
1178 //
1179 // These are either the implementation jar for the whole sdk library or the implementation
1180 // jars for the stubs. The latter should only be needed when generating JavaDoc as otherwise
1181 // they are identical to the corresponding header jars.
Jiyong Parkf1691d22021-03-29 20:11:58 +09001182 SdkImplementationJars(ctx android.BaseModuleContext, sdkVersion android.SdkSpec) android.Paths
Paul Duffin1267d872021-04-16 17:21:36 +01001183
1184 // SdkApiStubDexJar returns the dex jar for the stubs. It is needed by the hiddenapi processing
1185 // tool which processes dex files.
Martin Stjernholm8be1e6d2021-09-15 03:34:04 +01001186 SdkApiStubDexJar(ctx android.BaseModuleContext, kind android.SdkKind) OptionalDexJarPath
Paul Duffinf4600f62021-05-13 22:34:45 +01001187
Paul Duffin32cf58a2021-05-18 16:32:50 +01001188 // SdkRemovedTxtFile returns the optional path to the removed.txt file for the specified sdk kind.
1189 SdkRemovedTxtFile(ctx android.BaseModuleContext, kind android.SdkKind) android.OptionalPath
1190
Paul Duffinf4600f62021-05-13 22:34:45 +01001191 // sharedLibrary returns true if this can be used as a shared library.
1192 sharedLibrary() bool
Paul Duffin859fe962020-05-15 10:20:31 +01001193}
1194
Inseob Kimc0907f12019-02-08 21:00:45 +09001195type SdkLibrary struct {
Sundong Ahn054b19a2018-10-19 13:46:09 +09001196 Library
Jiyong Parkc678ad32018-04-10 13:07:10 +09001197
Zi Wangb2179e32023-01-31 15:53:30 -08001198 android.BazelModuleBase
1199
Sundong Ahn054b19a2018-10-19 13:46:09 +09001200 sdkLibraryProperties sdkLibraryProperties
Jiyong Parkc678ad32018-04-10 13:07:10 +09001201
Paul Duffin3375e352020-04-28 10:44:03 +01001202 // Map from api scope to the scope specific property structure.
1203 scopeToProperties map[*apiScope]*ApiScopeProperties
1204
Paul Duffin56d44902020-01-31 13:36:25 +00001205 commonToSdkLibraryAndImport
Jiyong Parkc678ad32018-04-10 13:07:10 +09001206}
1207
Inseob Kimc0907f12019-02-08 21:00:45 +09001208var _ SdkLibraryDependency = (*SdkLibrary)(nil)
Colin Cross897d2ed2019-02-11 14:03:51 -08001209
Paul Duffin3375e352020-04-28 10:44:03 +01001210func (module *SdkLibrary) generateTestAndSystemScopesByDefault() bool {
1211 return module.sdkLibraryProperties.Generate_system_and_test_apis
1212}
1213
1214func (module *SdkLibrary) getGeneratedApiScopes(ctx android.EarlyModuleContext) apiScopes {
1215 // Check to see if any scopes have been explicitly enabled. If any have then all
1216 // must be.
1217 anyScopesExplicitlyEnabled := false
1218 for _, scope := range allApiScopes {
1219 scopeProperties := module.scopeToProperties[scope]
1220 if scopeProperties.Enabled != nil {
1221 anyScopesExplicitlyEnabled = true
1222 break
1223 }
Paul Duffind1b3a922020-01-22 11:57:20 +00001224 }
Paul Duffin3375e352020-04-28 10:44:03 +01001225
1226 var generatedScopes apiScopes
1227 enabledScopes := make(map[*apiScope]struct{})
1228 for _, scope := range allApiScopes {
1229 scopeProperties := module.scopeToProperties[scope]
1230 // If any scopes are explicitly enabled then ignore the legacy enabled status.
1231 // This is to ensure that any new usages of this module type do not rely on legacy
1232 // behaviour.
1233 defaultEnabledStatus := false
1234 if anyScopesExplicitlyEnabled {
1235 defaultEnabledStatus = scope.defaultEnabledStatus
1236 } else {
1237 defaultEnabledStatus = scope.legacyEnabledStatus(module)
1238 }
1239 enabled := proptools.BoolDefault(scopeProperties.Enabled, defaultEnabledStatus)
1240 if enabled {
1241 enabledScopes[scope] = struct{}{}
1242 generatedScopes = append(generatedScopes, scope)
1243 }
1244 }
1245
1246 // Now check to make sure that any scope that is extended by an enabled scope is also
1247 // enabled.
1248 for _, scope := range allApiScopes {
1249 if _, ok := enabledScopes[scope]; ok {
1250 extends := scope.extends
1251 if extends != nil {
1252 if _, ok := enabledScopes[extends]; !ok {
1253 ctx.ModuleErrorf("enabled api scope %q depends on disabled scope %q", scope, extends)
1254 }
1255 }
1256 }
1257 }
1258
1259 return generatedScopes
Paul Duffind1b3a922020-01-22 11:57:20 +00001260}
1261
satayev758968a2021-12-06 11:42:40 +00001262var _ android.ModuleWithMinSdkVersionCheck = (*SdkLibrary)(nil)
1263
satayev8f088b02021-12-06 11:40:46 +00001264func (module *SdkLibrary) CheckMinSdkVersion(ctx android.ModuleContext) {
Spandan Das8c9ae7e2023-03-03 21:20:36 +00001265 android.CheckMinSdkVersion(ctx, module.MinSdkVersion(ctx), func(c android.ModuleContext, do android.PayloadDepsCallback) {
satayev8f088b02021-12-06 11:40:46 +00001266 ctx.WalkDeps(func(child android.Module, parent android.Module) bool {
1267 isExternal := !module.depIsInSameApex(ctx, child)
1268 if am, ok := child.(android.ApexModule); ok {
1269 if !do(ctx, parent, am, isExternal) {
1270 return false
1271 }
1272 }
1273 return !isExternal
1274 })
1275 })
1276}
1277
Paul Duffineedc5d52020-06-12 17:46:39 +01001278type sdkLibraryComponentTag struct {
1279 blueprint.BaseDependencyTag
1280 name string
1281}
1282
1283// Mark this tag so dependencies that use it are excluded from visibility enforcement.
1284func (t sdkLibraryComponentTag) ExcludeFromVisibilityEnforcement() {}
1285
1286var xmlPermissionsFileTag = sdkLibraryComponentTag{name: "xml-permissions-file"}
Paul Duffine74ac732020-02-06 13:51:46 +00001287
Jiyong Parke3833882020-02-17 17:28:10 +09001288func IsXmlPermissionsFileDepTag(depTag blueprint.DependencyTag) bool {
Paul Duffineedc5d52020-06-12 17:46:39 +01001289 if dt, ok := depTag.(sdkLibraryComponentTag); ok {
Jiyong Parke3833882020-02-17 17:28:10 +09001290 return dt == xmlPermissionsFileTag
1291 }
1292 return false
1293}
1294
Paul Duffineedc5d52020-06-12 17:46:39 +01001295var implLibraryTag = sdkLibraryComponentTag{name: "impl-library"}
Paul Duffin5df79302020-05-16 15:52:12 +01001296
Paul Duffin44f1d842020-06-26 20:17:02 +01001297// Add the dependencies on the child modules in the component deps mutator.
1298func (module *SdkLibrary) ComponentDepsMutator(ctx android.BottomUpMutatorContext) {
Paul Duffin3375e352020-04-28 10:44:03 +01001299 for _, apiScope := range module.getGeneratedApiScopes(ctx) {
Paul Duffind1b3a922020-01-22 11:57:20 +00001300 // Add dependencies to the stubs library
Spandan Das877f39d2023-03-29 16:19:51 +00001301 stubModuleName := module.stubsLibraryModuleName(apiScope)
Jihoon Kang1147b312023-06-08 23:25:57 +00001302
Spandan Das877f39d2023-03-29 16:19:51 +00001303 ctx.AddVariationDependencies(nil, apiScope.stubsTag, stubModuleName)
Paul Duffind1b3a922020-01-22 11:57:20 +00001304
Paul Duffin15f34ef2020-07-20 18:04:44 +01001305 // Add a dependency on the stubs source in order to access both stubs source and api information.
1306 ctx.AddVariationDependencies(nil, apiScope.stubsSourceAndApiTag, module.stubsSourceModuleName(apiScope))
Paul Duffin958806b2022-05-16 13:10:47 +00001307
1308 if module.compareAgainstLatestApi(apiScope) {
1309 // Add dependencies on the latest finalized version of the API .txt file.
1310 latestApiModuleName := module.latestApiModuleName(apiScope)
1311 ctx.AddDependency(module, apiScope.latestApiModuleTag, latestApiModuleName)
1312
1313 // Add dependencies on the latest finalized version of the remove API .txt file.
1314 latestRemovedApiModuleName := module.latestRemovedApiModuleName(apiScope)
1315 ctx.AddDependency(module, apiScope.latestRemovedApiModuleTag, latestRemovedApiModuleName)
1316 }
Sundong Ahn054b19a2018-10-19 13:46:09 +09001317 }
1318
Paul Duffindfa131e2020-05-15 20:37:11 +01001319 if module.requiresRuntimeImplementationLibrary() {
Paul Duffin5df79302020-05-16 15:52:12 +01001320 // Add dependency to the rule for generating the implementation library.
1321 ctx.AddDependency(module, implLibraryTag, module.implLibraryModuleName())
1322
Paul Duffindfa131e2020-05-15 20:37:11 +01001323 if module.sharedLibrary() {
1324 // Add dependency to the rule for generating the xml permissions file
Paul Duffineedc5d52020-06-12 17:46:39 +01001325 ctx.AddDependency(module, xmlPermissionsFileTag, module.xmlPermissionsModuleName())
Paul Duffindfa131e2020-05-15 20:37:11 +01001326 }
Paul Duffin44f1d842020-06-26 20:17:02 +01001327 }
1328}
Paul Duffine74ac732020-02-06 13:51:46 +00001329
Paul Duffin44f1d842020-06-26 20:17:02 +01001330// Add other dependencies as normal.
1331func (module *SdkLibrary) DepsMutator(ctx android.BottomUpMutatorContext) {
Anton Hanssone77fccc2021-01-20 16:52:41 +00001332 var missingApiModules []string
1333 for _, apiScope := range module.getGeneratedApiScopes(ctx) {
1334 if apiScope.unstable {
1335 continue
1336 }
Paul Duffin958806b2022-05-16 13:10:47 +00001337 if m := module.latestApiModuleName(apiScope); !ctx.OtherModuleExists(m) {
Anton Hanssone77fccc2021-01-20 16:52:41 +00001338 missingApiModules = append(missingApiModules, m)
1339 }
Paul Duffin958806b2022-05-16 13:10:47 +00001340 if m := module.latestRemovedApiModuleName(apiScope); !ctx.OtherModuleExists(m) {
Anton Hanssone77fccc2021-01-20 16:52:41 +00001341 missingApiModules = append(missingApiModules, m)
1342 }
Paul Duffin958806b2022-05-16 13:10:47 +00001343 if m := module.latestIncompatibilitiesModuleName(apiScope); !ctx.OtherModuleExists(m) {
Jaewoong Jung1a97ee02021-03-09 13:25:02 -08001344 missingApiModules = append(missingApiModules, m)
1345 }
Anton Hanssone77fccc2021-01-20 16:52:41 +00001346 }
1347 if len(missingApiModules) != 0 && !module.sdkLibraryProperties.Unsafe_ignore_missing_latest_api {
1348 m := module.Name() + " is missing tracking files for previously released library versions.\n"
1349 m += "You need to do one of the following:\n"
1350 m += "- Add `unsafe_ignore_missing_latest_api: true` to your blueprint (to disable compat tracking)\n"
1351 m += "- Add a set of prebuilt txt files representing the last released version of this library for compat checking.\n"
1352 m += " (the current set of API files can be used as a seed for this compatibility tracking\n"
1353 m += "\n"
1354 m += "The following filegroup modules are missing:\n "
1355 m += strings.Join(missingApiModules, "\n ") + "\n"
1356 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."
1357 ctx.ModuleErrorf(m)
1358 }
Paul Duffin44f1d842020-06-26 20:17:02 +01001359 if module.requiresRuntimeImplementationLibrary() {
Paul Duffindfa131e2020-05-15 20:37:11 +01001360 // Only add the deps for the library if it is actually going to be built.
1361 module.Library.deps(ctx)
1362 }
Jiyong Parkc678ad32018-04-10 13:07:10 +09001363}
1364
Paul Duffin46dc45a2020-05-14 15:39:10 +01001365func (module *SdkLibrary) OutputFiles(tag string) (android.Paths, error) {
1366 paths, err := module.commonOutputFiles(tag)
Colin Cross4acaea92021-12-10 23:05:02 +00001367 if paths != nil || err != nil {
Paul Duffin46dc45a2020-05-14 15:39:10 +01001368 return paths, err
1369 }
Colin Cross4acaea92021-12-10 23:05:02 +00001370 if module.requiresRuntimeImplementationLibrary() {
1371 return module.Library.OutputFiles(tag)
1372 }
1373 if tag == "" {
1374 return nil, nil
1375 }
1376 return nil, fmt.Errorf("unsupported module reference tag %q", tag)
Paul Duffin46dc45a2020-05-14 15:39:10 +01001377}
1378
Inseob Kimc0907f12019-02-08 21:00:45 +09001379func (module *SdkLibrary) GenerateAndroidBuildActions(ctx android.ModuleContext) {
satayev8f088b02021-12-06 11:40:46 +00001380 if proptools.String(module.deviceProperties.Min_sdk_version) != "" {
1381 module.CheckMinSdkVersion(ctx)
1382 }
1383
Paul Duffina2ae7e02020-09-11 11:55:00 +01001384 module.generateCommonBuildActions(ctx)
1385
Paul Duffindfa131e2020-05-15 20:37:11 +01001386 // Only build an implementation library if required.
1387 if module.requiresRuntimeImplementationLibrary() {
Paul Duffin43db9be2019-12-30 17:35:49 +00001388 module.Library.GenerateAndroidBuildActions(ctx)
1389 }
Sundong Ahn054b19a2018-10-19 13:46:09 +09001390
Paul Duffinb97b1572021-04-29 21:50:40 +01001391 // Collate the components exported by this module. All scope specific modules are exported but
1392 // the impl and xml component modules are not.
1393 exportedComponents := map[string]struct{}{}
1394
Sundong Ahn57368eb2018-07-06 11:20:23 +09001395 // Record the paths to the header jars of the library (stubs and impl).
Paul Duffind1b3a922020-01-22 11:57:20 +00001396 // When this java_sdk_library is depended upon from others via "libs" property,
Jiyong Parkc678ad32018-04-10 13:07:10 +09001397 // the recorded paths will be returned depending on the link type of the caller.
1398 ctx.VisitDirectDeps(func(to android.Module) {
Jiyong Parkc678ad32018-04-10 13:07:10 +09001399 tag := ctx.OtherModuleDependencyTag(to)
1400
Paul Duffinc8782502020-04-29 20:45:27 +01001401 // Extract information from any of the scope specific dependencies.
1402 if scopeTag, ok := tag.(scopeDependencyTag); ok {
1403 apiScope := scopeTag.apiScope
Paul Duffin803a9562020-05-20 11:52:25 +01001404 scopePaths := module.getScopePathsCreateIfNeeded(apiScope)
Paul Duffinc8782502020-04-29 20:45:27 +01001405
1406 // Extract information from the dependency. The exact information extracted
1407 // is determined by the nature of the dependency which is determined by the tag.
1408 scopeTag.extractDepInfo(ctx, to, scopePaths)
Paul Duffinb97b1572021-04-29 21:50:40 +01001409
1410 exportedComponents[ctx.OtherModuleName(to)] = struct{}{}
Sundong Ahn20e998b2018-07-24 11:19:26 +09001411 }
Jiyong Parkc678ad32018-04-10 13:07:10 +09001412 })
Paul Duffinb97b1572021-04-29 21:50:40 +01001413
1414 // Make the set of components exported by this module available for use elsewhere.
Cole Faust18994c72023-02-28 16:02:16 -08001415 exportedComponentInfo := android.ExportedComponentsInfo{Components: android.SortedKeys(exportedComponents)}
Paul Duffinb97b1572021-04-29 21:50:40 +01001416 ctx.SetProvider(android.ExportedComponentsInfoProvider, exportedComponentInfo)
Paul Duffin958806b2022-05-16 13:10:47 +00001417
1418 // Provide additional information for inclusion in an sdk's generated .info file.
1419 additionalSdkInfo := map[string]interface{}{}
1420 additionalSdkInfo["dist_stem"] = module.distStem()
Paul Duffine8409952022-09-22 16:24:46 +01001421 baseModuleName := module.distStem()
Paul Duffin958806b2022-05-16 13:10:47 +00001422 scopes := map[string]interface{}{}
1423 additionalSdkInfo["scopes"] = scopes
1424 for scope, scopePaths := range module.scopePaths {
1425 scopeInfo := map[string]interface{}{}
1426 scopes[scope.name] = scopeInfo
1427 scopeInfo["current_api"] = scope.snapshotRelativeCurrentApiTxtPath(baseModuleName)
1428 scopeInfo["removed_api"] = scope.snapshotRelativeRemovedApiTxtPath(baseModuleName)
1429 if p := scopePaths.latestApiPath; p.Valid() {
1430 scopeInfo["latest_api"] = p.Path().String()
1431 }
1432 if p := scopePaths.latestRemovedApiPath; p.Valid() {
1433 scopeInfo["latest_removed_api"] = p.Path().String()
1434 }
1435 }
1436 ctx.SetProvider(android.AdditionalSdkInfoProvider, android.AdditionalSdkInfo{additionalSdkInfo})
Jiyong Parkc678ad32018-04-10 13:07:10 +09001437}
1438
Jiyong Park0b0e1b92019-12-03 13:24:29 +09001439func (module *SdkLibrary) AndroidMkEntries() []android.AndroidMkEntries {
Paul Duffindfa131e2020-05-15 20:37:11 +01001440 if !module.requiresRuntimeImplementationLibrary() {
Paul Duffin43db9be2019-12-30 17:35:49 +00001441 return nil
1442 }
Jiyong Park0b0e1b92019-12-03 13:24:29 +09001443 entriesList := module.Library.AndroidMkEntries()
Yo Chiang07d75072020-06-05 17:43:19 +08001444 if module.sharedLibrary() {
1445 entries := &entriesList[0]
1446 entries.Required = append(entries.Required, module.xmlPermissionsModuleName())
1447 }
Jiyong Park0b0e1b92019-12-03 13:24:29 +09001448 return entriesList
Jiyong Park82484c02018-04-23 21:41:26 +09001449}
1450
Anton Hansson5fd5d242020-03-27 19:43:19 +00001451// The dist path of the stub artifacts
1452func (module *SdkLibrary) apiDistPath(apiScope *apiScope) string {
Colin Crossf0eace92021-06-02 13:02:23 -07001453 return path.Join("apistubs", module.distGroup(), apiScope.name)
Anton Hansson5fd5d242020-03-27 19:43:19 +00001454}
1455
Paul Duffin12ceb462019-12-24 20:31:31 +00001456// Get the sdk version for use when compiling the stubs library.
Paul Duffin780c5f42020-05-12 15:52:55 +01001457func (module *SdkLibrary) sdkVersionForStubsLibrary(mctx android.EarlyModuleContext, apiScope *apiScope) string {
Paul Duffin87a05a32020-05-12 11:50:28 +01001458 scopeProperties := module.scopeToProperties[apiScope]
1459 if scopeProperties.Sdk_version != nil {
1460 return proptools.String(scopeProperties.Sdk_version)
1461 }
1462
Jiyong Parkf1691d22021-03-29 20:11:58 +09001463 sdkDep := decodeSdkDep(mctx, android.SdkContext(&module.Library))
Paul Duffin12ceb462019-12-24 20:31:31 +00001464 if sdkDep.hasStandardLibs() {
1465 // If building against a standard sdk then use the sdk version appropriate for the scope.
Paul Duffind1b3a922020-01-22 11:57:20 +00001466 return apiScope.sdkVersion
Paul Duffin12ceb462019-12-24 20:31:31 +00001467 } else {
1468 // Otherwise, use no system module.
1469 return "none"
1470 }
1471}
1472
Paul Duffin31310252020-11-20 21:26:20 +00001473func (module *SdkLibrary) distStem() string {
1474 return proptools.StringDefault(module.sdkLibraryProperties.Dist_stem, module.BaseModuleName())
1475}
1476
Colin Cross986b69a2021-06-01 13:13:40 -07001477// distGroup returns the subdirectory of the dist path of the stub artifacts.
1478func (module *SdkLibrary) distGroup() string {
Colin Cross59b92bf2021-06-01 14:07:56 -07001479 return proptools.StringDefault(module.sdkLibraryProperties.Dist_group, "unknown")
Colin Cross986b69a2021-06-01 13:13:40 -07001480}
1481
Paul Duffin958806b2022-05-16 13:10:47 +00001482func latestPrebuiltApiModuleName(name string, apiScope *apiScope) string {
1483 return PrebuiltApiModuleName(name, apiScope.name, "latest")
1484}
1485
Paul Duffind1b3a922020-01-22 11:57:20 +00001486func (module *SdkLibrary) latestApiFilegroupName(apiScope *apiScope) string {
Paul Duffin958806b2022-05-16 13:10:47 +00001487 return ":" + module.latestApiModuleName(apiScope)
1488}
1489
1490func (module *SdkLibrary) latestApiModuleName(apiScope *apiScope) string {
1491 return latestPrebuiltApiModuleName(module.distStem(), apiScope)
Jiyong Park58c518b2018-05-12 22:29:12 +09001492}
Jiyong Parkc678ad32018-04-10 13:07:10 +09001493
Paul Duffind1b3a922020-01-22 11:57:20 +00001494func (module *SdkLibrary) latestRemovedApiFilegroupName(apiScope *apiScope) string {
Paul Duffin958806b2022-05-16 13:10:47 +00001495 return ":" + module.latestRemovedApiModuleName(apiScope)
1496}
1497
1498func (module *SdkLibrary) latestRemovedApiModuleName(apiScope *apiScope) string {
1499 return latestPrebuiltApiModuleName(module.distStem()+"-removed", apiScope)
Jiyong Parkc678ad32018-04-10 13:07:10 +09001500}
1501
Jaewoong Jung1a97ee02021-03-09 13:25:02 -08001502func (module *SdkLibrary) latestIncompatibilitiesFilegroupName(apiScope *apiScope) string {
Paul Duffin958806b2022-05-16 13:10:47 +00001503 return ":" + module.latestIncompatibilitiesModuleName(apiScope)
1504}
1505
1506func (module *SdkLibrary) latestIncompatibilitiesModuleName(apiScope *apiScope) string {
1507 return latestPrebuiltApiModuleName(module.distStem()+"-incompatibilities", apiScope)
Jaewoong Jung1a97ee02021-03-09 13:25:02 -08001508}
1509
Jihoon Kang1c92c3e2023-03-23 17:44:51 +00001510func (module *SdkLibrary) contributesToApiSurface(c android.Config) bool {
1511 _, exists := c.GetApiLibraries()[module.Name()]
1512 return exists
1513}
1514
Anton Hansson944e77d2020-08-19 11:40:22 +01001515func childModuleVisibility(childVisibility []string) []string {
1516 if childVisibility == nil {
1517 // No child visibility set. The child will use the visibility of the sdk_library.
1518 return nil
1519 }
1520
1521 // Prepend an override to ignore the sdk_library's visibility, and rely on the child visibility.
1522 var visibility []string
1523 visibility = append(visibility, "//visibility:override")
1524 visibility = append(visibility, childVisibility...)
1525 return visibility
1526}
1527
Paul Duffin5df79302020-05-16 15:52:12 +01001528// Creates the implementation java library
1529func (module *SdkLibrary) createImplLibrary(mctx android.DefaultableHookContext) {
Anton Hansson944e77d2020-08-19 11:40:22 +01001530 visibility := childModuleVisibility(module.sdkLibraryProperties.Impl_library_visibility)
1531
Paul Duffin5df79302020-05-16 15:52:12 +01001532 props := struct {
Paul Duffin77590a82022-04-28 14:13:30 +00001533 Name *string
1534 Visibility []string
1535 Instrument bool
1536 Libs []string
1537 Static_libs []string
1538 Apex_available []string
Paul Duffin5df79302020-05-16 15:52:12 +01001539 }{
1540 Name: proptools.StringPtr(module.implLibraryModuleName()),
Anton Hansson944e77d2020-08-19 11:40:22 +01001541 Visibility: visibility,
Paul Duffin296cf332020-06-18 21:09:55 +01001542 // Set the instrument property to ensure it is instrumented when instrumentation is required.
1543 Instrument: true,
Anton Hansson7f66efa2020-10-08 14:47:23 +01001544 // Set the impl_only libs. Note that the module's "Libs" get appended as well, via the
1545 // addition of &module.properties below.
1546 Libs: module.sdkLibraryProperties.Impl_only_libs,
Paul Duffin77590a82022-04-28 14:13:30 +00001547 // Set the impl_only static libs. Note that the module's "static_libs" get appended as well, via the
1548 // addition of &module.properties below.
1549 Static_libs: module.sdkLibraryProperties.Impl_only_static_libs,
1550 // Pass the apex_available settings down so that the impl library can be statically
1551 // embedded within a library that is added to an APEX. Needed for updatable-media.
1552 Apex_available: module.ApexAvailable(),
Paul Duffin5df79302020-05-16 15:52:12 +01001553 }
1554
1555 properties := []interface{}{
1556 &module.properties,
1557 &module.protoProperties,
1558 &module.deviceProperties,
Liz Kammera7a64f32020-07-09 15:16:41 -07001559 &module.dexProperties,
Paul Duffin5df79302020-05-16 15:52:12 +01001560 &module.dexpreoptProperties,
Colin Cross014489c2020-06-02 20:09:13 -07001561 &module.linter.properties,
Paul Duffin5df79302020-05-16 15:52:12 +01001562 &props,
1563 module.sdkComponentPropertiesForChildLibrary(),
1564 }
1565 mctx.CreateModule(LibraryFactory, properties...)
1566}
1567
Jiyong Parkc678ad32018-04-10 13:07:10 +09001568// Creates a static java library that has API stubs
Paul Duffinf0229202020-04-29 16:47:28 +01001569func (module *SdkLibrary) createStubsLibrary(mctx android.DefaultableHookContext, apiScope *apiScope) {
Jiyong Parkc678ad32018-04-10 13:07:10 +09001570 props := struct {
Dan Willemsen9f435972020-05-28 15:28:00 -07001571 Name *string
1572 Visibility []string
1573 Srcs []string
1574 Installable *bool
1575 Sdk_version *string
1576 System_modules *string
1577 Patch_module *string
1578 Libs []string
Anton Hanssondae54cd2021-04-21 16:30:10 +01001579 Static_libs []string
Dan Willemsen9f435972020-05-28 15:28:00 -07001580 Compile_dex *bool
1581 Java_version *string
1582 Openjdk9 struct {
Sundong Ahn054b19a2018-10-19 13:46:09 +09001583 Srcs []string
1584 Javacflags []string
1585 }
Anton Hansson5fd5d242020-03-27 19:43:19 +00001586 Dist struct {
1587 Targets []string
1588 Dest *string
1589 Dir *string
1590 Tag *string
1591 }
Jiyong Parkc678ad32018-04-10 13:07:10 +09001592 }{}
1593
Jihoon Kang1147b312023-06-08 23:25:57 +00001594 props.Name = proptools.StringPtr(module.sourceStubLibraryModuleName(apiScope))
Anton Hansson944e77d2020-08-19 11:40:22 +01001595 props.Visibility = childModuleVisibility(module.sdkLibraryProperties.Stubs_library_visibility)
Jiyong Parkc678ad32018-04-10 13:07:10 +09001596 // sources are generated from the droiddoc
Paul Duffinc3091c82020-05-08 14:16:20 +01001597 props.Srcs = []string{":" + module.stubsSourceModuleName(apiScope)}
Paul Duffin12ceb462019-12-24 20:31:31 +00001598 sdkVersion := module.sdkVersionForStubsLibrary(mctx, apiScope)
Paul Duffin52d398a2019-06-11 12:31:14 +01001599 props.Sdk_version = proptools.StringPtr(sdkVersion)
Paul Duffina18abc22020-05-16 18:54:24 +01001600 props.System_modules = module.deviceProperties.System_modules
1601 props.Patch_module = module.properties.Patch_module
Paul Duffin367ab912019-12-23 19:40:36 +00001602 props.Installable = proptools.BoolPtr(false)
Sundong Ahn054b19a2018-10-19 13:46:09 +09001603 props.Libs = module.sdkLibraryProperties.Stub_only_libs
Anton Hanssondae54cd2021-04-21 16:30:10 +01001604 props.Static_libs = module.sdkLibraryProperties.Stub_only_static_libs
Paul Duffine22c2ab2020-05-20 19:35:27 +01001605 // The stub-annotations library contains special versions of the annotations
1606 // with CLASS retention policy, so that they're kept.
1607 if proptools.Bool(module.sdkLibraryProperties.Annotations_enabled) {
1608 props.Libs = append(props.Libs, "stub-annotations")
1609 }
Paul Duffina18abc22020-05-16 18:54:24 +01001610 props.Openjdk9.Srcs = module.properties.Openjdk9.Srcs
1611 props.Openjdk9.Javacflags = module.properties.Openjdk9.Javacflags
Anton Hansson83509b52020-05-21 09:21:57 +01001612 // We compile the stubs for 1.8 in line with the main android.jar stubs, and potential
1613 // interop with older developer tools that don't support 1.9.
1614 props.Java_version = proptools.StringPtr("1.8")
Paul Duffinf4600f62021-05-13 22:34:45 +01001615
Paul Duffin859fe962020-05-15 10:20:31 +01001616 mctx.CreateModule(LibraryFactory, &props, module.sdkComponentPropertiesForChildLibrary())
Jiyong Parkc678ad32018-04-10 13:07:10 +09001617}
1618
Paul Duffin6d0886e2020-04-07 18:49:53 +01001619// Creates a droidstubs module that creates stubs source files from the given full source
Paul Duffinc8782502020-04-29 20:45:27 +01001620// files and also updates and checks the API specification files.
Paul Duffin15f34ef2020-07-20 18:04:44 +01001621func (module *SdkLibrary) createStubsSourcesAndApi(mctx android.DefaultableHookContext, apiScope *apiScope, name string, scopeSpecificDroidstubsArgs []string) {
Jiyong Parkc678ad32018-04-10 13:07:10 +09001622 props := struct {
Sundong Ahn054b19a2018-10-19 13:46:09 +09001623 Name *string
Paul Duffin4911a892020-04-29 23:35:13 +01001624 Visibility []string
Sundong Ahn054b19a2018-10-19 13:46:09 +09001625 Srcs []string
1626 Installable *bool
Paul Duffin52d398a2019-06-11 12:31:14 +01001627 Sdk_version *string
Jihoon Kang3198f3c2023-01-26 08:08:52 +00001628 Api_surface *string
Paul Duffin12ceb462019-12-24 20:31:31 +00001629 System_modules *string
Sundong Ahn054b19a2018-10-19 13:46:09 +09001630 Libs []string
Paul Duffin6877e6d2020-09-25 19:59:14 +01001631 Output_javadoc_comments *bool
Paul Duffin11512472019-02-11 15:55:17 +00001632 Arg_files []string
Sundong Ahn054b19a2018-10-19 13:46:09 +09001633 Args *string
Sundong Ahn054b19a2018-10-19 13:46:09 +09001634 Java_version *string
Paul Duffine22c2ab2020-05-20 19:35:27 +01001635 Annotations_enabled *bool
Sundong Ahn054b19a2018-10-19 13:46:09 +09001636 Merge_annotations_dirs []string
1637 Merge_inclusion_annotations_dirs []string
Paul Duffin0ff08bd2020-04-29 13:30:54 +01001638 Generate_stubs *bool
Anton Hanssone87b03d2020-12-21 15:29:34 +00001639 Previous_api *string
Sundong Ahn054b19a2018-10-19 13:46:09 +09001640 Check_api struct {
Anton Hanssone6056152020-12-31 10:37:27 +00001641 Current ApiToCheck
1642 Last_released ApiToCheck
Paul Duffin160fe412020-05-10 19:32:20 +01001643
1644 Api_lint struct {
1645 Enabled *bool
1646 New_since *string
1647 Baseline_file *string
1648 }
Jiyong Park58c518b2018-05-12 22:29:12 +09001649 }
Sundong Ahn1b92c822018-05-29 11:35:17 +09001650 Aidl struct {
1651 Include_dirs []string
1652 Local_include_dirs []string
1653 }
Paul Duffin040e9062020-11-23 17:41:36 +00001654 Dists []android.Dist
Jiyong Parkc678ad32018-04-10 13:07:10 +09001655 }{}
1656
Paul Duffin7b78b4d2020-04-28 14:08:32 +01001657 // The stubs source processing uses the same compile time classpath when extracting the
1658 // API from the implementation library as it does when compiling it. i.e. the same
1659 // * sdk version
1660 // * system_modules
1661 // * libs (static_libs/libs)
Paul Duffin250e6192019-06-07 10:44:37 +01001662
Paul Duffin0ff08bd2020-04-29 13:30:54 +01001663 props.Name = proptools.StringPtr(name)
Anton Hansson944e77d2020-08-19 11:40:22 +01001664 props.Visibility = childModuleVisibility(module.sdkLibraryProperties.Stubs_source_visibility)
Paul Duffina18abc22020-05-16 18:54:24 +01001665 props.Srcs = append(props.Srcs, module.properties.Srcs...)
Anton Hanssonf8ea3722021-09-16 14:24:13 +01001666 props.Srcs = append(props.Srcs, module.sdkLibraryProperties.Api_srcs...)
Paul Duffina18abc22020-05-16 18:54:24 +01001667 props.Sdk_version = module.deviceProperties.Sdk_version
Jihoon Kang3198f3c2023-01-26 08:08:52 +00001668 props.Api_surface = &apiScope.name
Paul Duffina18abc22020-05-16 18:54:24 +01001669 props.System_modules = module.deviceProperties.System_modules
Jiyong Parkc678ad32018-04-10 13:07:10 +09001670 props.Installable = proptools.BoolPtr(false)
Sundong Ahne6f0b052018-06-05 16:46:14 +09001671 // A droiddoc module has only one Libs property and doesn't distinguish between
1672 // shared libs and static libs. So we need to add both of these libs to Libs property.
Paul Duffina18abc22020-05-16 18:54:24 +01001673 props.Libs = module.properties.Libs
1674 props.Libs = append(props.Libs, module.properties.Static_libs...)
Nikita Ioffed732da72022-11-21 12:38:25 +00001675 props.Libs = append(props.Libs, module.sdkLibraryProperties.Stub_only_libs...)
Paul Duffina18abc22020-05-16 18:54:24 +01001676 props.Aidl.Include_dirs = module.deviceProperties.Aidl.Include_dirs
1677 props.Aidl.Local_include_dirs = module.deviceProperties.Aidl.Local_include_dirs
1678 props.Java_version = module.properties.Java_version
Jiyong Parkc678ad32018-04-10 13:07:10 +09001679
Paul Duffine22c2ab2020-05-20 19:35:27 +01001680 props.Annotations_enabled = module.sdkLibraryProperties.Annotations_enabled
Sundong Ahn054b19a2018-10-19 13:46:09 +09001681 props.Merge_annotations_dirs = module.sdkLibraryProperties.Merge_annotations_dirs
1682 props.Merge_inclusion_annotations_dirs = module.sdkLibraryProperties.Merge_inclusion_annotations_dirs
1683
Paul Duffin6d0886e2020-04-07 18:49:53 +01001684 droidstubsArgs := []string{}
Paul Duffin235ffff2019-12-24 10:41:30 +00001685 if len(module.sdkLibraryProperties.Api_packages) != 0 {
Paul Duffin6d0886e2020-04-07 18:49:53 +01001686 droidstubsArgs = append(droidstubsArgs, "--stub-packages "+strings.Join(module.sdkLibraryProperties.Api_packages, ":"))
Paul Duffin235ffff2019-12-24 10:41:30 +00001687 }
1688 if len(module.sdkLibraryProperties.Hidden_api_packages) != 0 {
Paul Duffin6d0886e2020-04-07 18:49:53 +01001689 droidstubsArgs = append(droidstubsArgs,
Paul Duffin235ffff2019-12-24 10:41:30 +00001690 android.JoinWithPrefix(module.sdkLibraryProperties.Hidden_api_packages, " --hide-package "))
1691 }
Paul Duffin6d0886e2020-04-07 18:49:53 +01001692 droidstubsArgs = append(droidstubsArgs, module.sdkLibraryProperties.Droiddoc_options...)
Paul Duffin235ffff2019-12-24 10:41:30 +00001693 disabledWarnings := []string{
Paul Duffin235ffff2019-12-24 10:41:30 +00001694 "BroadcastBehavior",
Paul Duffin235ffff2019-12-24 10:41:30 +00001695 "DeprecationMismatch",
Anton Hansson3c0779a2022-02-18 19:24:30 +00001696 "HiddenSuperclass",
Paul Duffin235ffff2019-12-24 10:41:30 +00001697 "HiddenTypeParameter",
Anton Hansson3c0779a2022-02-18 19:24:30 +00001698 "MissingPermission",
1699 "SdkConstant",
Paul Duffin235ffff2019-12-24 10:41:30 +00001700 "Todo",
1701 "Typo",
Anton Hansson3c0779a2022-02-18 19:24:30 +00001702 "UnavailableSymbol",
Paul Duffin235ffff2019-12-24 10:41:30 +00001703 }
Paul Duffin6d0886e2020-04-07 18:49:53 +01001704 droidstubsArgs = append(droidstubsArgs, android.JoinWithPrefix(disabledWarnings, "--hide "))
Sundong Ahnfb2721f2018-09-17 13:23:09 +09001705
Paul Duffin6877e6d2020-09-25 19:59:14 +01001706 // Output Javadoc comments for public scope.
1707 if apiScope == apiScopePublic {
1708 props.Output_javadoc_comments = proptools.BoolPtr(true)
1709 }
1710
Paul Duffin1fb487d2020-04-07 18:50:10 +01001711 // Add in scope specific arguments.
Paul Duffin0ff08bd2020-04-29 13:30:54 +01001712 droidstubsArgs = append(droidstubsArgs, scopeSpecificDroidstubsArgs...)
Paul Duffin11512472019-02-11 15:55:17 +00001713 props.Arg_files = module.sdkLibraryProperties.Droiddoc_option_files
Paul Duffin6d0886e2020-04-07 18:49:53 +01001714 props.Args = proptools.StringPtr(strings.Join(droidstubsArgs, " "))
Jiyong Parkc678ad32018-04-10 13:07:10 +09001715
Paul Duffin15f34ef2020-07-20 18:04:44 +01001716 // List of APIs identified from the provided source files are created. They are later
1717 // compared against to the not-yet-released (a.k.a current) list of APIs and to the
1718 // last-released (a.k.a numbered) list of API.
1719 currentApiFileName := apiScope.apiFilePrefix + "current.txt"
1720 removedApiFileName := apiScope.apiFilePrefix + "removed.txt"
1721 apiDir := module.getApiDir()
1722 currentApiFileName = path.Join(apiDir, currentApiFileName)
1723 removedApiFileName = path.Join(apiDir, removedApiFileName)
Jiyong Parkc678ad32018-04-10 13:07:10 +09001724
Paul Duffin15f34ef2020-07-20 18:04:44 +01001725 // check against the not-yet-release API
1726 props.Check_api.Current.Api_file = proptools.StringPtr(currentApiFileName)
1727 props.Check_api.Current.Removed_api_file = proptools.StringPtr(removedApiFileName)
Jiyong Park58c518b2018-05-12 22:29:12 +09001728
Paul Duffin958806b2022-05-16 13:10:47 +00001729 if module.compareAgainstLatestApi(apiScope) {
Paul Duffin15f34ef2020-07-20 18:04:44 +01001730 // check against the latest released API
1731 latestApiFilegroupName := proptools.StringPtr(module.latestApiFilegroupName(apiScope))
Anton Hanssone87b03d2020-12-21 15:29:34 +00001732 props.Previous_api = latestApiFilegroupName
Paul Duffin15f34ef2020-07-20 18:04:44 +01001733 props.Check_api.Last_released.Api_file = latestApiFilegroupName
1734 props.Check_api.Last_released.Removed_api_file = proptools.StringPtr(
1735 module.latestRemovedApiFilegroupName(apiScope))
Jaewoong Jung1a97ee02021-03-09 13:25:02 -08001736 props.Check_api.Last_released.Baseline_file = proptools.StringPtr(
1737 module.latestIncompatibilitiesFilegroupName(apiScope))
Paul Duffin160fe412020-05-10 19:32:20 +01001738
Paul Duffin15f34ef2020-07-20 18:04:44 +01001739 if proptools.Bool(module.sdkLibraryProperties.Api_lint.Enabled) {
1740 // Enable api lint.
1741 props.Check_api.Api_lint.Enabled = proptools.BoolPtr(true)
1742 props.Check_api.Api_lint.New_since = latestApiFilegroupName
Paul Duffin160fe412020-05-10 19:32:20 +01001743
Paul Duffin15f34ef2020-07-20 18:04:44 +01001744 // If it exists then pass a lint-baseline.txt through to droidstubs.
1745 baselinePath := path.Join(apiDir, apiScope.apiFilePrefix+"lint-baseline.txt")
1746 baselinePathRelativeToRoot := path.Join(mctx.ModuleDir(), baselinePath)
1747 paths, err := mctx.GlobWithDeps(baselinePathRelativeToRoot, nil)
1748 if err != nil {
1749 mctx.ModuleErrorf("error checking for presence of %s: %s", baselinePathRelativeToRoot, err)
1750 }
1751 if len(paths) == 1 {
1752 props.Check_api.Api_lint.Baseline_file = proptools.StringPtr(baselinePath)
1753 } else if len(paths) != 0 {
1754 mctx.ModuleErrorf("error checking for presence of %s: expected one path, found: %v", baselinePathRelativeToRoot, paths)
Paul Duffin160fe412020-05-10 19:32:20 +01001755 }
1756 }
Paul Duffin15f34ef2020-07-20 18:04:44 +01001757 }
Jiyong Park58c518b2018-05-12 22:29:12 +09001758
Paul Duffin15f34ef2020-07-20 18:04:44 +01001759 if !Bool(module.sdkLibraryProperties.No_dist) {
Paul Duffin040e9062020-11-23 17:41:36 +00001760 // Dist the api txt and removed api txt artifacts for sdk builds.
1761 distDir := proptools.StringPtr(path.Join(module.apiDistPath(apiScope), "api"))
1762 for _, p := range []struct {
1763 tag string
1764 pattern string
1765 }{
1766 {tag: ".api.txt", pattern: "%s.txt"},
1767 {tag: ".removed-api.txt", pattern: "%s-removed.txt"},
1768 } {
1769 props.Dists = append(props.Dists, android.Dist{
1770 Targets: []string{"sdk", "win_sdk"},
1771 Dir: distDir,
1772 Dest: proptools.StringPtr(fmt.Sprintf(p.pattern, module.distStem())),
1773 Tag: proptools.StringPtr(p.tag),
1774 })
1775 }
Anton Hansson5fd5d242020-03-27 19:43:19 +00001776 }
1777
Jihoon Kangd48abd52023-02-02 22:32:31 +00001778 mctx.CreateModule(DroidstubsFactory, &props).(*Droidstubs).CallHookIfAvailable(mctx)
Jiyong Parkc678ad32018-04-10 13:07:10 +09001779}
1780
Jihoon Kang1c92c3e2023-03-23 17:44:51 +00001781func (module *SdkLibrary) createApiLibrary(mctx android.DefaultableHookContext, apiScope *apiScope) {
1782 props := struct {
1783 Name *string
1784 Visibility []string
1785 Api_contributions []string
1786 Libs []string
1787 Static_libs []string
1788 Dep_api_srcs *string
1789 }{}
1790
1791 props.Name = proptools.StringPtr(module.apiLibraryModuleName(apiScope))
1792 props.Visibility = childModuleVisibility(module.sdkLibraryProperties.Stubs_library_visibility)
1793
1794 apiContributions := []string{}
1795
1796 // Api surfaces are not independent of each other, but have subset relationships,
1797 // and so does the api files. To generate from-text stubs for api surfaces other than public,
1798 // all subset api domains' api_contriubtions must be added as well.
1799 scope := apiScope
1800 for scope != nil {
1801 apiContributions = append(apiContributions, module.stubsSourceModuleName(scope)+".api.contribution")
1802 scope = scope.extends
1803 }
1804
1805 props.Api_contributions = apiContributions
1806 props.Libs = module.properties.Libs
1807 props.Libs = append(props.Libs, module.sdkLibraryProperties.Stub_only_libs...)
1808 props.Libs = append(props.Libs, "stub-annotations")
1809 props.Static_libs = module.sdkLibraryProperties.Stub_only_static_libs
1810 props.Dep_api_srcs = proptools.StringPtr(apiScope.kind.DefaultJavaLibraryName() + ".from-text")
1811
1812 // android_module_lib_stubs_current.from-text only comprises api contributions from art, conscrypt and i18n.
1813 // Thus, replace with android_module_lib_stubs_current_full.from-text, which comprises every api domains.
1814 if apiScope.kind == android.SdkModule {
1815 props.Dep_api_srcs = proptools.StringPtr(apiScope.kind.DefaultJavaLibraryName() + "_full.from-text")
1816 }
1817
1818 mctx.CreateModule(ApiLibraryFactory, &props)
1819}
1820
Jihoon Kang1147b312023-06-08 23:25:57 +00001821func (module *SdkLibrary) createTopLevelStubsLibrary(
1822 mctx android.DefaultableHookContext, apiScope *apiScope, contributesToApiSurface bool) {
1823 props := struct {
1824 Name *string
1825 Visibility []string
1826 Sdk_version *string
1827 Static_libs []string
1828 System_modules *string
1829 Dist struct {
1830 Targets []string
1831 Dest *string
1832 Dir *string
1833 Tag *string
1834 }
1835 Compile_dex *bool
1836 }{}
1837 props.Name = proptools.StringPtr(module.stubsLibraryModuleName(apiScope))
1838 props.Visibility = childModuleVisibility(module.sdkLibraryProperties.Stubs_library_visibility)
1839 sdkVersion := module.sdkVersionForStubsLibrary(mctx, apiScope)
1840 props.Sdk_version = proptools.StringPtr(sdkVersion)
1841
1842 // Add the stub compiling java_library/java_api_library as static lib based on build config
1843 staticLib := module.sourceStubLibraryModuleName(apiScope)
1844 if mctx.Config().BuildFromTextStub() && contributesToApiSurface {
1845 staticLib = module.apiLibraryModuleName(apiScope)
1846 }
1847 props.Static_libs = append(props.Static_libs, staticLib)
1848 props.System_modules = module.deviceProperties.System_modules
1849
1850 // Dist the class jar artifact for sdk builds.
1851 if !Bool(module.sdkLibraryProperties.No_dist) {
1852 props.Dist.Targets = []string{"sdk", "win_sdk"}
1853 props.Dist.Dest = proptools.StringPtr(fmt.Sprintf("%v.jar", module.distStem()))
1854 props.Dist.Dir = proptools.StringPtr(module.apiDistPath(apiScope))
1855 props.Dist.Tag = proptools.StringPtr(".jar")
1856 }
1857
1858 // The imports need to be compiled to dex if the java_sdk_library requests it.
1859 compileDex := module.dexProperties.Compile_dex
1860 if module.stubLibrariesCompiledForDex() {
1861 compileDex = proptools.BoolPtr(true)
1862 }
1863 props.Compile_dex = compileDex
1864
1865 mctx.CreateModule(LibraryFactory, &props, module.sdkComponentPropertiesForChildLibrary())
1866}
1867
Paul Duffin958806b2022-05-16 13:10:47 +00001868func (module *SdkLibrary) compareAgainstLatestApi(apiScope *apiScope) bool {
1869 return !(apiScope.unstable || module.sdkLibraryProperties.Unsafe_ignore_missing_latest_api)
1870}
1871
Paul Duffinea8f8082021-06-24 13:25:57 +01001872// Implements android.ApexModule
Jooyung Han5e9013b2020-03-10 06:23:13 +09001873func (module *SdkLibrary) DepIsInSameApex(mctx android.BaseModuleContext, dep android.Module) bool {
1874 depTag := mctx.OtherModuleDependencyTag(dep)
1875 if depTag == xmlPermissionsFileTag {
1876 return true
1877 }
1878 return module.Library.DepIsInSameApex(mctx, dep)
1879}
1880
Paul Duffinea8f8082021-06-24 13:25:57 +01001881// Implements android.ApexModule
1882func (module *SdkLibrary) UniqueApexVariations() bool {
1883 return module.uniqueApexVariations()
1884}
1885
Jiyong Parkc678ad32018-04-10 13:07:10 +09001886// Creates the xml file that publicizes the runtime library
Paul Duffinf0229202020-04-29 16:47:28 +01001887func (module *SdkLibrary) createXmlFile(mctx android.DefaultableHookContext) {
Spandan Das8c9ae7e2023-03-03 21:20:36 +00001888 moduleMinApiLevel := module.Library.MinSdkVersion(mctx)
Pedro Loureiroc3621422021-09-28 15:40:23 +00001889 var moduleMinApiLevelStr = moduleMinApiLevel.String()
1890 if moduleMinApiLevel == android.NoneApiLevel {
1891 moduleMinApiLevelStr = "current"
1892 }
Jiyong Parke3833882020-02-17 17:28:10 +09001893 props := struct {
Pedro Loureiroc3621422021-09-28 15:40:23 +00001894 Name *string
1895 Lib_name *string
1896 Apex_available []string
1897 On_bootclasspath_since *string
1898 On_bootclasspath_before *string
1899 Min_device_sdk *string
1900 Max_device_sdk *string
1901 Sdk_library_min_api_level *string
Jiyong Parke3833882020-02-17 17:28:10 +09001902 }{
Pedro Loureiroc3621422021-09-28 15:40:23 +00001903 Name: proptools.StringPtr(module.xmlPermissionsModuleName()),
1904 Lib_name: proptools.StringPtr(module.BaseModuleName()),
1905 Apex_available: module.ApexProperties.Apex_available,
1906 On_bootclasspath_since: module.commonSdkLibraryProperties.On_bootclasspath_since,
1907 On_bootclasspath_before: module.commonSdkLibraryProperties.On_bootclasspath_before,
1908 Min_device_sdk: module.commonSdkLibraryProperties.Min_device_sdk,
1909 Max_device_sdk: module.commonSdkLibraryProperties.Max_device_sdk,
1910 Sdk_library_min_api_level: &moduleMinApiLevelStr,
Jiyong Parkc678ad32018-04-10 13:07:10 +09001911 }
Jiyong Parke3833882020-02-17 17:28:10 +09001912
Jiyong Parke3833882020-02-17 17:28:10 +09001913 mctx.CreateModule(sdkLibraryXmlFactory, &props)
Jiyong Parkc678ad32018-04-10 13:07:10 +09001914}
1915
Jiyong Parkf1691d22021-03-29 20:11:58 +09001916func PrebuiltJars(ctx android.BaseModuleContext, baseName string, s android.SdkSpec) android.Paths {
Jiyong Park54105c42021-03-31 18:17:53 +09001917 var ver android.ApiLevel
Jiyong Parkf1691d22021-03-29 20:11:58 +09001918 var kind android.SdkKind
1919 if s.UsePrebuilt(ctx) {
Jiyong Park54105c42021-03-31 18:17:53 +09001920 ver = s.ApiLevel
Jiyong Parkf1691d22021-03-29 20:11:58 +09001921 kind = s.Kind
Jiyong Parkc678ad32018-04-10 13:07:10 +09001922 } else {
Jiyong Park6a927c42020-01-21 02:03:43 +09001923 // We don't have prebuilt SDK for the specific sdkVersion.
1924 // Instead of breaking the build, fallback to use "system_current"
Jiyong Park54105c42021-03-31 18:17:53 +09001925 ver = android.FutureApiLevel
Jiyong Parkf1691d22021-03-29 20:11:58 +09001926 kind = android.SdkSystem
Sundong Ahn054b19a2018-10-19 13:46:09 +09001927 }
Jiyong Park6a927c42020-01-21 02:03:43 +09001928
1929 dir := filepath.Join("prebuilts", "sdk", ver.String(), kind.String())
Paul Duffin50061512020-01-21 16:31:05 +00001930 jar := filepath.Join(dir, baseName+".jar")
Sundong Ahn054b19a2018-10-19 13:46:09 +09001931 jarPath := android.ExistentPathForSource(ctx, jar)
Sundong Ahnae418ac2019-02-28 15:01:28 +09001932 if !jarPath.Valid() {
Colin Cross07c88562020-01-07 09:34:44 -08001933 if ctx.Config().AllowMissingDependencies() {
1934 return android.Paths{android.PathForSource(ctx, jar)}
1935 } else {
Jiyong Parkf1691d22021-03-29 20:11:58 +09001936 ctx.PropertyErrorf("sdk_library", "invalid sdk version %q, %q does not exist", s.Raw, jar)
Colin Cross07c88562020-01-07 09:34:44 -08001937 }
Sundong Ahnae418ac2019-02-28 15:01:28 +09001938 return nil
1939 }
Sundong Ahn054b19a2018-10-19 13:46:09 +09001940 return android.Paths{jarPath.Path()}
1941}
1942
Colin Crossaede88c2020-08-11 12:17:01 -07001943// 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 +01001944//
1945// If either this or the other module are on the platform then this will return
1946// false.
Colin Cross56a83212020-09-15 18:30:11 -07001947func withinSameApexesAs(ctx android.BaseModuleContext, other android.Module) bool {
1948 apexInfo := ctx.Provider(android.ApexInfoProvider).(android.ApexInfo)
1949 otherApexInfo := ctx.OtherModuleProvider(other, android.ApexInfoProvider).(android.ApexInfo)
Jiyong Parkab50b072021-05-12 17:13:56 +09001950 return len(otherApexInfo.InApexVariants) > 0 && reflect.DeepEqual(apexInfo.InApexVariants, otherApexInfo.InApexVariants)
Paul Duffin9b879592020-05-26 13:21:35 +01001951}
1952
Jiyong Parkf1691d22021-03-29 20:11:58 +09001953func (module *SdkLibrary) sdkJars(ctx android.BaseModuleContext, sdkVersion android.SdkSpec, headerJars bool) android.Paths {
Jiyong Park932cdfe2020-05-28 00:19:53 +09001954 // If the client doesn't set sdk_version, but if this library prefers stubs over
1955 // the impl library, let's provide the widest API surface possible. To do so,
1956 // force override sdk_version to module_current so that the closest possible API
1957 // surface could be found in selectHeaderJarsForSdkVersion
Jiyong Parkf1691d22021-03-29 20:11:58 +09001958 if module.defaultsToStubs() && !sdkVersion.Specified() {
Jiyong Park92315372021-04-02 08:45:46 +09001959 sdkVersion = android.SdkSpecFrom(ctx, "module_current")
Jiyong Park932cdfe2020-05-28 00:19:53 +09001960 }
Paul Duffind1b3a922020-01-22 11:57:20 +00001961
Paul Duffindaaa3322020-05-26 18:13:57 +01001962 // Only provide access to the implementation library if it is actually built.
1963 if module.requiresRuntimeImplementationLibrary() {
1964 // Check any special cases for java_sdk_library.
1965 //
1966 // Only allow access to the implementation library in the following condition:
1967 // * No sdk_version specified on the referencing module.
Paul Duffin9b879592020-05-26 13:21:35 +01001968 // * The referencing module is in the same apex as this.
Jiyong Parkf1691d22021-03-29 20:11:58 +09001969 if sdkVersion.Kind == android.SdkPrivate || withinSameApexesAs(ctx, module) {
Paul Duffindaaa3322020-05-26 18:13:57 +01001970 if headerJars {
1971 return module.HeaderJars()
1972 } else {
1973 return module.ImplementationJars()
1974 }
Sundong Ahn054b19a2018-10-19 13:46:09 +09001975 }
Jiyong Parkc678ad32018-04-10 13:07:10 +09001976 }
Paul Duffinb05d4292020-05-20 12:19:10 +01001977
Paul Duffin23970f42020-05-20 14:20:02 +01001978 return module.selectHeaderJarsForSdkVersion(ctx, sdkVersion)
Jiyong Parkc678ad32018-04-10 13:07:10 +09001979}
1980
Sundong Ahn241cd372018-07-13 16:16:44 +09001981// to satisfy SdkLibraryDependency interface
Jiyong Parkf1691d22021-03-29 20:11:58 +09001982func (module *SdkLibrary) SdkHeaderJars(ctx android.BaseModuleContext, sdkVersion android.SdkSpec) android.Paths {
Paul Duffind1b3a922020-01-22 11:57:20 +00001983 return module.sdkJars(ctx, sdkVersion, true /*headerJars*/)
1984}
1985
1986// to satisfy SdkLibraryDependency interface
Jiyong Parkf1691d22021-03-29 20:11:58 +09001987func (module *SdkLibrary) SdkImplementationJars(ctx android.BaseModuleContext, sdkVersion android.SdkSpec) android.Paths {
Paul Duffind1b3a922020-01-22 11:57:20 +00001988 return module.sdkJars(ctx, sdkVersion, false /*headerJars*/)
Sundong Ahn241cd372018-07-13 16:16:44 +09001989}
1990
Colin Cross571cccf2019-02-04 11:22:08 -08001991var javaSdkLibrariesKey = android.NewOnceKey("javaSdkLibraries")
1992
Jiyong Park82484c02018-04-23 21:41:26 +09001993func javaSdkLibraries(config android.Config) *[]string {
Colin Cross571cccf2019-02-04 11:22:08 -08001994 return config.Once(javaSdkLibrariesKey, func() interface{} {
Jiyong Park82484c02018-04-23 21:41:26 +09001995 return &[]string{}
1996 }).(*[]string)
1997}
1998
Paul Duffin749f98f2019-12-30 17:23:46 +00001999func (module *SdkLibrary) getApiDir() string {
2000 return proptools.StringDefault(module.sdkLibraryProperties.Api_dir, "api")
2001}
2002
Jiyong Parkc678ad32018-04-10 13:07:10 +09002003// For a java_sdk_library module, create internal modules for stubs, docs,
2004// runtime libs and xml file. If requested, the stubs and docs are created twice
2005// once for public API level and once for system API level
Paul Duffinf0229202020-04-29 16:47:28 +01002006func (module *SdkLibrary) CreateInternalModules(mctx android.DefaultableHookContext) {
2007 // If the module has been disabled then don't create any child modules.
2008 if !module.Enabled() {
2009 return
2010 }
2011
Paul Duffina18abc22020-05-16 18:54:24 +01002012 if len(module.properties.Srcs) == 0 {
Inseob Kimc0907f12019-02-08 21:00:45 +09002013 mctx.PropertyErrorf("srcs", "java_sdk_library must specify srcs")
Jooyung Han58f26ab2019-12-18 15:34:32 +09002014 return
Inseob Kimc0907f12019-02-08 21:00:45 +09002015 }
2016
Paul Duffin37e0b772019-12-30 17:20:10 +00002017 // If this builds against standard libraries (i.e. is not part of the core libraries)
Paul Duffin4f5c1ef2020-11-19 14:53:43 +00002018 // then assume it provides both system and test apis.
Jiyong Parkf1691d22021-03-29 20:11:58 +09002019 sdkDep := decodeSdkDep(mctx, android.SdkContext(&module.Library))
Paul Duffin37e0b772019-12-30 17:20:10 +00002020 hasSystemAndTestApis := sdkDep.hasStandardLibs()
Paul Duffin3375e352020-04-28 10:44:03 +01002021 module.sdkLibraryProperties.Generate_system_and_test_apis = hasSystemAndTestApis
Paul Duffin4f5c1ef2020-11-19 14:53:43 +00002022
Jaewoong Jung18aefc12020-12-21 09:11:10 -08002023 missingCurrentApi := false
Inseob Kim8098faa2019-03-18 10:19:51 +09002024
Paul Duffin3375e352020-04-28 10:44:03 +01002025 generatedScopes := module.getGeneratedApiScopes(mctx)
Paul Duffind1b3a922020-01-22 11:57:20 +00002026
Paul Duffin749f98f2019-12-30 17:23:46 +00002027 apiDir := module.getApiDir()
Paul Duffin3375e352020-04-28 10:44:03 +01002028 for _, scope := range generatedScopes {
Inseob Kim8098faa2019-03-18 10:19:51 +09002029 for _, api := range []string{"current.txt", "removed.txt"} {
Paul Duffind1b3a922020-01-22 11:57:20 +00002030 path := path.Join(mctx.ModuleDir(), apiDir, scope.apiFilePrefix+api)
Inseob Kim8098faa2019-03-18 10:19:51 +09002031 p := android.ExistentPathForSource(mctx, path)
2032 if !p.Valid() {
Colin Cross18f840c2021-05-20 17:56:54 -07002033 if mctx.Config().AllowMissingDependencies() {
2034 mctx.AddMissingDependencies([]string{path})
2035 } else {
2036 mctx.ModuleErrorf("Current api file %#v doesn't exist", path)
2037 missingCurrentApi = true
2038 }
Inseob Kim8098faa2019-03-18 10:19:51 +09002039 }
2040 }
2041 }
2042
Jaewoong Jung18aefc12020-12-21 09:11:10 -08002043 if missingCurrentApi {
Inseob Kim8098faa2019-03-18 10:19:51 +09002044 script := "build/soong/scripts/gen-java-current-api-files.sh"
2045 p := android.ExistentPathForSource(mctx, script)
2046
2047 if !p.Valid() {
2048 panic(fmt.Sprintf("script file %s doesn't exist", script))
2049 }
2050
2051 mctx.ModuleErrorf("One or more current api files are missing. "+
2052 "You can update them by:\n"+
Paul Duffin37e0b772019-12-30 17:20:10 +00002053 "%s %q %s && m update-api",
Paul Duffind1b3a922020-01-22 11:57:20 +00002054 script, filepath.Join(mctx.ModuleDir(), apiDir),
Paul Duffin3375e352020-04-28 10:44:03 +01002055 strings.Join(generatedScopes.Strings(func(s *apiScope) string { return s.apiFilePrefix }), " "))
Inseob Kim8098faa2019-03-18 10:19:51 +09002056 return
2057 }
2058
Paul Duffin3375e352020-04-28 10:44:03 +01002059 for _, scope := range generatedScopes {
Paul Duffin15f34ef2020-07-20 18:04:44 +01002060 // Use the stubs source name for legacy reasons.
2061 module.createStubsSourcesAndApi(mctx, scope, module.stubsSourceModuleName(scope), scope.droidstubsArgs)
Paul Duffin0ff08bd2020-04-29 13:30:54 +01002062
Paul Duffind1b3a922020-01-22 11:57:20 +00002063 module.createStubsLibrary(mctx, scope)
Jihoon Kang1c92c3e2023-03-23 17:44:51 +00002064
Jihoon Kang1147b312023-06-08 23:25:57 +00002065 contributesToApiSurface := module.contributesToApiSurface(mctx.Config())
2066 if contributesToApiSurface {
Jihoon Kang1c92c3e2023-03-23 17:44:51 +00002067 module.createApiLibrary(mctx, scope)
2068 }
Jihoon Kang1147b312023-06-08 23:25:57 +00002069
2070 module.createTopLevelStubsLibrary(mctx, scope, contributesToApiSurface)
Inseob Kimc0907f12019-02-08 21:00:45 +09002071 }
2072
Paul Duffindfa131e2020-05-15 20:37:11 +01002073 if module.requiresRuntimeImplementationLibrary() {
Paul Duffin5df79302020-05-16 15:52:12 +01002074 // Create child module to create an implementation library.
2075 //
2076 // This temporarily creates a second implementation library that can be explicitly
2077 // referenced.
2078 //
2079 // TODO(b/156618935) - update comment once only one implementation library is created.
2080 module.createImplLibrary(mctx)
2081
Paul Duffindfa131e2020-05-15 20:37:11 +01002082 // Only create an XML permissions file that declares the library as being usable
2083 // as a shared library if required.
2084 if module.sharedLibrary() {
2085 module.createXmlFile(mctx)
2086 }
Paul Duffin43db9be2019-12-30 17:35:49 +00002087
2088 // record java_sdk_library modules so that they are exported to make
2089 javaSdkLibraries := javaSdkLibraries(mctx.Config())
2090 javaSdkLibrariesLock.Lock()
2091 defer javaSdkLibrariesLock.Unlock()
2092 *javaSdkLibraries = append(*javaSdkLibraries, module.BaseModuleName())
2093 }
Anton Hansson7f66efa2020-10-08 14:47:23 +01002094
Paul Duffin77590a82022-04-28 14:13:30 +00002095 // 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 +01002096 module.properties.Libs = append(module.properties.Libs, module.sdkLibraryProperties.Impl_only_libs...)
Paul Duffin77590a82022-04-28 14:13:30 +00002097 module.properties.Static_libs = append(module.properties.Static_libs, module.sdkLibraryProperties.Impl_only_static_libs...)
Inseob Kimc0907f12019-02-08 21:00:45 +09002098}
2099
2100func (module *SdkLibrary) InitSdkLibraryProperties() {
Colin Crossce6734e2020-06-15 16:09:53 -07002101 module.addHostAndDeviceProperties()
2102 module.AddProperties(&module.sdkLibraryProperties)
Sundong Ahn054b19a2018-10-19 13:46:09 +09002103
Paul Duffin71b33cc2021-06-23 11:39:47 +01002104 module.initSdkLibraryComponent(module)
Paul Duffin859fe962020-05-15 10:20:31 +01002105
Paul Duffina18abc22020-05-16 18:54:24 +01002106 module.properties.Installable = proptools.BoolPtr(true)
2107 module.deviceProperties.IsSDKLibrary = true
Inseob Kimc0907f12019-02-08 21:00:45 +09002108}
Sundong Ahn054b19a2018-10-19 13:46:09 +09002109
Paul Duffindfa131e2020-05-15 20:37:11 +01002110func (module *SdkLibrary) requiresRuntimeImplementationLibrary() bool {
2111 return !proptools.Bool(module.sdkLibraryProperties.Api_only)
2112}
2113
Jiyong Park932cdfe2020-05-28 00:19:53 +09002114func (module *SdkLibrary) defaultsToStubs() bool {
2115 return proptools.Bool(module.sdkLibraryProperties.Default_to_stubs)
2116}
2117
Paul Duffin1b1e8062020-05-08 13:44:43 +01002118// Defines how to name the individual component modules the sdk library creates.
2119type sdkLibraryComponentNamingScheme interface {
2120 stubsLibraryModuleName(scope *apiScope, baseName string) string
2121
2122 stubsSourceModuleName(scope *apiScope, baseName string) string
Jihoon Kang1c92c3e2023-03-23 17:44:51 +00002123
2124 apiLibraryModuleName(scope *apiScope, baseName string) string
Jihoon Kang1147b312023-06-08 23:25:57 +00002125
2126 sourceStubLibraryModuleName(scope *apiScope, baseName string) string
Paul Duffin1b1e8062020-05-08 13:44:43 +01002127}
2128
2129type defaultNamingScheme struct {
2130}
2131
2132func (s *defaultNamingScheme) stubsLibraryModuleName(scope *apiScope, baseName string) string {
2133 return scope.stubsLibraryModuleName(baseName)
2134}
2135
2136func (s *defaultNamingScheme) stubsSourceModuleName(scope *apiScope, baseName string) string {
2137 return scope.stubsSourceModuleName(baseName)
2138}
2139
Jihoon Kang1c92c3e2023-03-23 17:44:51 +00002140func (s *defaultNamingScheme) apiLibraryModuleName(scope *apiScope, baseName string) string {
2141 return scope.apiLibraryModuleName(baseName)
2142}
2143
Jihoon Kang1147b312023-06-08 23:25:57 +00002144func (s *defaultNamingScheme) sourceStubLibraryModuleName(scope *apiScope, baseName string) string {
2145 return scope.sourceStubLibraryModuleName(baseName)
2146}
2147
Paul Duffin1b1e8062020-05-08 13:44:43 +01002148var _ sdkLibraryComponentNamingScheme = (*defaultNamingScheme)(nil)
2149
Jaewoong Jungbc15e3a2021-03-10 17:02:43 -08002150func moduleStubLinkType(name string) (stub bool, ret sdkLinkType) {
Jihoon Kang1147b312023-06-08 23:25:57 +00002151 name = strings.TrimSuffix(name, ".from-source")
2152
Anton Hansson2d0c1942020-05-25 12:20:51 +01002153 // This suffix-based approach is fragile and could potentially mis-trigger.
2154 // TODO(b/155164730): Clean this up when modules no longer reference sdk_lib stubs directly.
Anton Hansson08f476b2021-04-07 15:32:19 +01002155 if strings.HasSuffix(name, apiScopePublic.stubsLibraryModuleNameSuffix()) {
2156 if name == "hwbinder.stubs" || name == "libcore_private.stubs" {
2157 // Due to a previous bug, these modules were not considered stubs, so we retain that.
2158 return false, javaPlatform
2159 }
Anton Hansson2d0c1942020-05-25 12:20:51 +01002160 return true, javaSdk
2161 }
Anton Hansson08f476b2021-04-07 15:32:19 +01002162 if strings.HasSuffix(name, apiScopeSystem.stubsLibraryModuleNameSuffix()) {
Anton Hansson2d0c1942020-05-25 12:20:51 +01002163 return true, javaSystem
2164 }
Anton Hansson08f476b2021-04-07 15:32:19 +01002165 if strings.HasSuffix(name, apiScopeModuleLib.stubsLibraryModuleNameSuffix()) {
Anton Hansson2d0c1942020-05-25 12:20:51 +01002166 return true, javaModule
2167 }
Anton Hansson08f476b2021-04-07 15:32:19 +01002168 if strings.HasSuffix(name, apiScopeTest.stubsLibraryModuleNameSuffix()) {
Anton Hansson2d0c1942020-05-25 12:20:51 +01002169 return true, javaSystem
2170 }
Jihoon Kang1147b312023-06-08 23:25:57 +00002171 if strings.HasSuffix(name, apiScopeSystemServer.stubsLibraryModuleNameSuffix()) {
2172 return true, javaSystemServer
2173 }
Anton Hansson2d0c1942020-05-25 12:20:51 +01002174 return false, javaPlatform
2175}
2176
Jaewoong Jung4f158ee2019-07-11 10:05:35 -07002177// java_sdk_library is a special Java library that provides optional platform APIs to apps.
2178// In practice, it can be viewed as a combination of several modules: 1) stubs library that clients
2179// are linked against to, 2) droiddoc module that internally generates API stubs source files,
2180// 3) the real runtime shared library that implements the APIs, and 4) XML file for adding
2181// the runtime lib to the classpath at runtime if requested via <uses-library>.
Inseob Kimc0907f12019-02-08 21:00:45 +09002182func SdkLibraryFactory() android.Module {
2183 module := &SdkLibrary{}
Paul Duffinc3091c82020-05-08 14:16:20 +01002184
2185 // Initialize information common between source and prebuilt.
Paul Duffin71b33cc2021-06-23 11:39:47 +01002186 module.initCommon(module)
Paul Duffinc3091c82020-05-08 14:16:20 +01002187
Inseob Kimc0907f12019-02-08 21:00:45 +09002188 module.InitSdkLibraryProperties()
Jooyung Han58f26ab2019-12-18 15:34:32 +09002189 android.InitApexModule(module)
Sundong Ahn054b19a2018-10-19 13:46:09 +09002190 InitJavaModule(module, android.HostAndDeviceSupported)
Paul Duffin3375e352020-04-28 10:44:03 +01002191
2192 // Initialize the map from scope to scope specific properties.
2193 scopeToProperties := make(map[*apiScope]*ApiScopeProperties)
2194 for _, scope := range allApiScopes {
2195 scopeToProperties[scope] = scope.scopeSpecificProperties(module)
2196 }
2197 module.scopeToProperties = scopeToProperties
2198
Paul Duffin4911a892020-04-29 23:35:13 +01002199 // Add the properties containing visibility rules so that they are checked.
Paul Duffin5df79302020-05-16 15:52:12 +01002200 android.AddVisibilityProperty(module, "impl_library_visibility", &module.sdkLibraryProperties.Impl_library_visibility)
Paul Duffin4911a892020-04-29 23:35:13 +01002201 android.AddVisibilityProperty(module, "stubs_library_visibility", &module.sdkLibraryProperties.Stubs_library_visibility)
2202 android.AddVisibilityProperty(module, "stubs_source_visibility", &module.sdkLibraryProperties.Stubs_source_visibility)
2203
Paul Duffin1b1e8062020-05-08 13:44:43 +01002204 module.SetDefaultableHook(func(ctx android.DefaultableHookContext) {
Paul Duffindfa131e2020-05-15 20:37:11 +01002205 // If no implementation is required then it cannot be used as a shared library
2206 // either.
2207 if !module.requiresRuntimeImplementationLibrary() {
2208 // If shared_library has been explicitly set to true then it is incompatible
2209 // with api_only: true.
2210 if proptools.Bool(module.commonSdkLibraryProperties.Shared_library) {
2211 ctx.PropertyErrorf("api_only/shared_library", "inconsistent settings, shared_library and api_only cannot both be true")
2212 }
2213 // Set shared_library: false.
2214 module.commonSdkLibraryProperties.Shared_library = proptools.BoolPtr(false)
2215 }
2216
Paul Duffin1b1e8062020-05-08 13:44:43 +01002217 if module.initCommonAfterDefaultsApplied(ctx) {
2218 module.CreateInternalModules(ctx)
2219 }
2220 })
Zi Wangb2179e32023-01-31 15:53:30 -08002221 android.InitBazelModule(module)
Jiyong Parkc678ad32018-04-10 13:07:10 +09002222 return module
2223}
Colin Cross79c7c262019-04-17 11:11:46 -07002224
Zi Wangb2179e32023-01-31 15:53:30 -08002225type bazelSdkLibraryAttributes struct {
2226 Public bazel.StringAttribute
2227 System bazel.StringAttribute
2228 Test bazel.StringAttribute
2229 Module_lib bazel.StringAttribute
2230 System_server bazel.StringAttribute
2231}
2232
2233// java_sdk_library bp2build converter
2234func (module *SdkLibrary) ConvertWithBp2build(ctx android.TopDownMutatorContext) {
2235 if ctx.ModuleType() != "java_sdk_library" {
Chris Parsons39a16972023-06-08 14:28:51 +00002236 ctx.MarkBp2buildUnconvertible(bp2build_metrics_proto.UnconvertedReasonType_TYPE_UNSUPPORTED, "")
Zi Wangb2179e32023-01-31 15:53:30 -08002237 return
2238 }
2239
2240 nameToAttr := make(map[string]bazel.StringAttribute)
2241
2242 for _, scope := range module.getGeneratedApiScopes(ctx) {
2243 apiSurfaceFile := path.Join(module.getApiDir(), scope.apiFilePrefix+"current.txt")
2244 var scopeStringAttribute bazel.StringAttribute
2245 scopeStringAttribute.SetValue(apiSurfaceFile)
2246 nameToAttr[scope.name] = scopeStringAttribute
2247 }
2248
2249 attrs := bazelSdkLibraryAttributes{
2250 Public: nameToAttr["public"],
2251 System: nameToAttr["system"],
2252 Test: nameToAttr["test"],
2253 Module_lib: nameToAttr["module-lib"],
2254 System_server: nameToAttr["system-server"],
2255 }
2256 props := bazel.BazelTargetModuleProperties{
2257 Rule_class: "java_sdk_library",
2258 Bzl_load_location: "//build/bazel/rules/java:sdk_library.bzl",
2259 }
2260
2261 ctx.CreateBazelTargetModule(props, android.CommonAttributes{Name: module.Name()}, &attrs)
2262}
2263
Colin Cross79c7c262019-04-17 11:11:46 -07002264//
2265// SDK library prebuilts
2266//
2267
Paul Duffin56d44902020-01-31 13:36:25 +00002268// Properties associated with each api scope.
2269type sdkLibraryScopeProperties struct {
Colin Cross79c7c262019-04-17 11:11:46 -07002270 Jars []string `android:"path"`
2271
2272 Sdk_version *string
2273
Colin Cross79c7c262019-04-17 11:11:46 -07002274 // List of shared java libs that this module has dependencies to
2275 Libs []string
Paul Duffin3d1248c2020-04-09 00:10:17 +01002276
Paul Duffinc8782502020-04-29 20:45:27 +01002277 // The stubs source.
Paul Duffin3d1248c2020-04-09 00:10:17 +01002278 Stub_srcs []string `android:"path"`
Paul Duffin1fd005d2020-04-09 01:08:11 +01002279
2280 // The current.txt
Paul Duffin0f8faff2020-05-20 16:18:00 +01002281 Current_api *string `android:"path"`
Paul Duffin1fd005d2020-04-09 01:08:11 +01002282
2283 // The removed.txt
Paul Duffin0f8faff2020-05-20 16:18:00 +01002284 Removed_api *string `android:"path"`
Anton Hanssond78eb762021-09-21 15:25:12 +01002285
2286 // Annotation zip
2287 Annotations *string `android:"path"`
Colin Cross79c7c262019-04-17 11:11:46 -07002288}
2289
Paul Duffin56d44902020-01-31 13:36:25 +00002290type sdkLibraryImportProperties struct {
Paul Duffinfcfd7912020-01-31 17:54:30 +00002291 // List of shared java libs, common to all scopes, that this module has
2292 // dependencies to
2293 Libs []string
Paul Duffin1267d872021-04-16 17:21:36 +01002294
2295 // If set to true, compile dex files for the stubs. Defaults to false.
2296 Compile_dex *bool
Paul Duffin869de142021-07-15 14:14:41 +01002297
2298 // If not empty, classes are restricted to the specified packages and their sub-packages.
Paul Duffin869de142021-07-15 14:14:41 +01002299 Permitted_packages []string
Paul Duffin56d44902020-01-31 13:36:25 +00002300}
2301
Paul Duffineedc5d52020-06-12 17:46:39 +01002302type SdkLibraryImport struct {
Colin Cross79c7c262019-04-17 11:11:46 -07002303 android.ModuleBase
2304 android.DefaultableModuleBase
2305 prebuilt android.Prebuilt
Paul Duffindd46f712020-02-10 13:37:10 +00002306 android.ApexModuleBase
Colin Cross79c7c262019-04-17 11:11:46 -07002307
Paul Duffin37856732021-02-26 14:24:15 +00002308 hiddenAPI
Jiakai Zhang204356f2021-09-09 08:12:46 +00002309 dexpreopter
Paul Duffin37856732021-02-26 14:24:15 +00002310
Colin Cross79c7c262019-04-17 11:11:46 -07002311 properties sdkLibraryImportProperties
2312
Paul Duffin46a26a82020-04-07 19:27:04 +01002313 // Map from api scope to the scope specific property structure.
2314 scopeProperties map[*apiScope]*sdkLibraryScopeProperties
2315
Paul Duffin56d44902020-01-31 13:36:25 +00002316 commonToSdkLibraryAndImport
Paul Duffineedc5d52020-06-12 17:46:39 +01002317
2318 // The reference to the implementation library created by the source module.
2319 // Is nil if the source module does not exist.
2320 implLibraryModule *Library
2321
2322 // The reference to the xml permissions module created by the source module.
2323 // Is nil if the source module does not exist.
2324 xmlPermissionsFileModule *sdkLibraryXml
Paul Duffin39853512021-02-26 11:09:39 +00002325
Jeongik Chad5fe8782021-07-08 01:13:11 +09002326 // Build path to the dex implementation jar obtained from the prebuilt_apex, if any.
Martin Stjernholm8be1e6d2021-09-15 03:34:04 +01002327 dexJarFile OptionalDexJarPath
Jeongik Chad5fe8782021-07-08 01:13:11 +09002328
2329 // Expected install file path of the source module(sdk_library)
2330 // or dex implementation jar obtained from the prebuilt_apex, if any.
2331 installFile android.Path
Colin Cross79c7c262019-04-17 11:11:46 -07002332}
2333
Paul Duffineedc5d52020-06-12 17:46:39 +01002334var _ SdkLibraryDependency = (*SdkLibraryImport)(nil)
Colin Cross79c7c262019-04-17 11:11:46 -07002335
Paul Duffin46a26a82020-04-07 19:27:04 +01002336// The type of a structure that contains a field of type sdkLibraryScopeProperties
2337// for each apiscope in allApiScopes, e.g. something like:
Colin Crossd079e0b2022-08-16 10:27:33 -07002338//
2339// struct {
2340// Public sdkLibraryScopeProperties
2341// System sdkLibraryScopeProperties
2342// ...
2343// }
Paul Duffin46a26a82020-04-07 19:27:04 +01002344var allScopeStructType = createAllScopePropertiesStructType()
2345
2346// Dynamically create a structure type for each apiscope in allApiScopes.
2347func createAllScopePropertiesStructType() reflect.Type {
2348 var fields []reflect.StructField
2349 for _, apiScope := range allApiScopes {
2350 field := reflect.StructField{
2351 Name: apiScope.fieldName,
2352 Type: reflect.TypeOf(sdkLibraryScopeProperties{}),
2353 }
2354 fields = append(fields, field)
2355 }
2356
2357 return reflect.StructOf(fields)
2358}
2359
2360// Create an instance of the scope specific structure type and return a map
2361// from apiscope to a pointer to each scope specific field.
2362func createPropertiesInstance() (interface{}, map[*apiScope]*sdkLibraryScopeProperties) {
2363 allScopePropertiesPtr := reflect.New(allScopeStructType)
2364 allScopePropertiesStruct := allScopePropertiesPtr.Elem()
2365 scopeProperties := make(map[*apiScope]*sdkLibraryScopeProperties)
2366
2367 for _, apiScope := range allApiScopes {
2368 field := allScopePropertiesStruct.FieldByName(apiScope.fieldName)
2369 scopeProperties[apiScope] = field.Addr().Interface().(*sdkLibraryScopeProperties)
2370 }
2371
2372 return allScopePropertiesPtr.Interface(), scopeProperties
2373}
2374
Jaewoong Jung4f158ee2019-07-11 10:05:35 -07002375// java_sdk_library_import imports a prebuilt java_sdk_library.
Colin Cross79c7c262019-04-17 11:11:46 -07002376func sdkLibraryImportFactory() android.Module {
Paul Duffineedc5d52020-06-12 17:46:39 +01002377 module := &SdkLibraryImport{}
Colin Cross79c7c262019-04-17 11:11:46 -07002378
Paul Duffin46a26a82020-04-07 19:27:04 +01002379 allScopeProperties, scopeToProperties := createPropertiesInstance()
2380 module.scopeProperties = scopeToProperties
Jiakai Zhang9c4dc192023-02-09 00:09:24 +08002381 module.AddProperties(&module.properties, allScopeProperties, &module.importDexpreoptProperties)
Colin Cross79c7c262019-04-17 11:11:46 -07002382
Paul Duffinc3091c82020-05-08 14:16:20 +01002383 // Initialize information common between source and prebuilt.
Paul Duffin71b33cc2021-06-23 11:39:47 +01002384 module.initCommon(module)
Paul Duffinc3091c82020-05-08 14:16:20 +01002385
Paul Duffin0bdcb272020-02-06 15:24:57 +00002386 android.InitPrebuiltModule(module, &[]string{""})
Paul Duffindd46f712020-02-10 13:37:10 +00002387 android.InitApexModule(module)
Colin Cross79c7c262019-04-17 11:11:46 -07002388 InitJavaModule(module, android.HostAndDeviceSupported)
2389
Paul Duffin1b1e8062020-05-08 13:44:43 +01002390 module.SetDefaultableHook(func(mctx android.DefaultableHookContext) {
2391 if module.initCommonAfterDefaultsApplied(mctx) {
2392 module.createInternalModules(mctx)
2393 }
2394 })
Colin Cross79c7c262019-04-17 11:11:46 -07002395 return module
2396}
2397
Paul Duffin630b11e2021-07-15 13:35:26 +01002398var _ PermittedPackagesForUpdatableBootJars = (*SdkLibraryImport)(nil)
2399
2400func (module *SdkLibraryImport) PermittedPackagesForUpdatableBootJars() []string {
2401 return module.properties.Permitted_packages
2402}
2403
Paul Duffineedc5d52020-06-12 17:46:39 +01002404func (module *SdkLibraryImport) Prebuilt() *android.Prebuilt {
Colin Cross79c7c262019-04-17 11:11:46 -07002405 return &module.prebuilt
2406}
2407
Paul Duffineedc5d52020-06-12 17:46:39 +01002408func (module *SdkLibraryImport) Name() string {
Colin Cross79c7c262019-04-17 11:11:46 -07002409 return module.prebuilt.Name(module.ModuleBase.Name())
2410}
2411
Paul Duffineedc5d52020-06-12 17:46:39 +01002412func (module *SdkLibraryImport) createInternalModules(mctx android.DefaultableHookContext) {
Colin Cross79c7c262019-04-17 11:11:46 -07002413
Paul Duffin50061512020-01-21 16:31:05 +00002414 // If the build is configured to use prebuilts then force this to be preferred.
Jeongik Cha816a23a2020-07-08 01:09:23 +09002415 if mctx.Config().AlwaysUsePrebuiltSdks() {
Paul Duffin50061512020-01-21 16:31:05 +00002416 module.prebuilt.ForcePrefer()
2417 }
2418
Paul Duffin46a26a82020-04-07 19:27:04 +01002419 for apiScope, scopeProperties := range module.scopeProperties {
Paul Duffin56d44902020-01-31 13:36:25 +00002420 if len(scopeProperties.Jars) == 0 {
2421 continue
2422 }
2423
Paul Duffinbbb546b2020-04-09 00:07:11 +01002424 module.createJavaImportForStubs(mctx, apiScope, scopeProperties)
Paul Duffin3d1248c2020-04-09 00:10:17 +01002425
Paul Duffin0f8faff2020-05-20 16:18:00 +01002426 if len(scopeProperties.Stub_srcs) > 0 {
2427 module.createPrebuiltStubsSources(mctx, apiScope, scopeProperties)
2428 }
Paul Duffin56d44902020-01-31 13:36:25 +00002429 }
Colin Cross79c7c262019-04-17 11:11:46 -07002430
2431 javaSdkLibraries := javaSdkLibraries(mctx.Config())
2432 javaSdkLibrariesLock.Lock()
2433 defer javaSdkLibrariesLock.Unlock()
2434 *javaSdkLibraries = append(*javaSdkLibraries, module.BaseModuleName())
2435}
2436
Paul Duffineedc5d52020-06-12 17:46:39 +01002437func (module *SdkLibraryImport) createJavaImportForStubs(mctx android.DefaultableHookContext, apiScope *apiScope, scopeProperties *sdkLibraryScopeProperties) {
Paul Duffinbbb546b2020-04-09 00:07:11 +01002438 // Creates a java import for the jar with ".stubs" suffix
2439 props := struct {
Paul Duffin1dbe3ca2020-05-16 09:57:59 +01002440 Name *string
2441 Sdk_version *string
2442 Libs []string
2443 Jars []string
Paul Duffin1267d872021-04-16 17:21:36 +01002444 Compile_dex *bool
Paul Duffinbf4de042022-09-27 12:41:52 +01002445
2446 android.UserSuppliedPrebuiltProperties
Paul Duffinbbb546b2020-04-09 00:07:11 +01002447 }{}
Paul Duffinc3091c82020-05-08 14:16:20 +01002448 props.Name = proptools.StringPtr(module.stubsLibraryModuleName(apiScope))
Paul Duffinbbb546b2020-04-09 00:07:11 +01002449 props.Sdk_version = scopeProperties.Sdk_version
2450 // Prepend any of the libs from the legacy public properties to the libs for each of the
2451 // scopes to avoid having to duplicate them in each scope.
2452 props.Libs = append(module.properties.Libs, scopeProperties.Libs...)
2453 props.Jars = scopeProperties.Jars
Paul Duffin1dbe3ca2020-05-16 09:57:59 +01002454
Paul Duffin38b57852020-05-13 16:08:09 +01002455 // The imports are preferred if the java_sdk_library_import is preferred.
Paul Duffinbf4de042022-09-27 12:41:52 +01002456 props.CopyUserSuppliedPropertiesFromPrebuilt(&module.prebuilt)
Paul Duffin859fe962020-05-15 10:20:31 +01002457
Paul Duffin1267d872021-04-16 17:21:36 +01002458 // The imports need to be compiled to dex if the java_sdk_library_import requests it.
Paul Duffinf4600f62021-05-13 22:34:45 +01002459 compileDex := module.properties.Compile_dex
2460 if module.stubLibrariesCompiledForDex() {
2461 compileDex = proptools.BoolPtr(true)
2462 }
2463 props.Compile_dex = compileDex
Paul Duffin1267d872021-04-16 17:21:36 +01002464
Paul Duffin859fe962020-05-15 10:20:31 +01002465 mctx.CreateModule(ImportFactory, &props, module.sdkComponentPropertiesForChildLibrary())
Paul Duffinbbb546b2020-04-09 00:07:11 +01002466}
2467
Paul Duffineedc5d52020-06-12 17:46:39 +01002468func (module *SdkLibraryImport) createPrebuiltStubsSources(mctx android.DefaultableHookContext, apiScope *apiScope, scopeProperties *sdkLibraryScopeProperties) {
Paul Duffin3d1248c2020-04-09 00:10:17 +01002469 props := struct {
Paul Duffinbf4de042022-09-27 12:41:52 +01002470 Name *string
2471 Srcs []string
2472
2473 android.UserSuppliedPrebuiltProperties
Paul Duffin3d1248c2020-04-09 00:10:17 +01002474 }{}
Paul Duffinc3091c82020-05-08 14:16:20 +01002475 props.Name = proptools.StringPtr(module.stubsSourceModuleName(apiScope))
Paul Duffin3d1248c2020-04-09 00:10:17 +01002476 props.Srcs = scopeProperties.Stub_srcs
Paul Duffin38b57852020-05-13 16:08:09 +01002477
2478 // The stubs source is preferred if the java_sdk_library_import is preferred.
Paul Duffinbf4de042022-09-27 12:41:52 +01002479 props.CopyUserSuppliedPropertiesFromPrebuilt(&module.prebuilt)
2480
2481 mctx.CreateModule(PrebuiltStubsSourcesFactory, &props)
Paul Duffin3d1248c2020-04-09 00:10:17 +01002482}
2483
Paul Duffin44f1d842020-06-26 20:17:02 +01002484// Add the dependencies on the child module in the component deps mutator so that it
2485// creates references to the prebuilt and not the source modules.
2486func (module *SdkLibraryImport) ComponentDepsMutator(ctx android.BottomUpMutatorContext) {
Paul Duffin46a26a82020-04-07 19:27:04 +01002487 for apiScope, scopeProperties := range module.scopeProperties {
Paul Duffin56d44902020-01-31 13:36:25 +00002488 if len(scopeProperties.Jars) == 0 {
2489 continue
2490 }
2491
2492 // Add dependencies to the prebuilt stubs library
Paul Duffin864116c2021-04-02 10:24:13 +01002493 ctx.AddVariationDependencies(nil, apiScope.stubsTag, android.PrebuiltNameFromSource(module.stubsLibraryModuleName(apiScope)))
Paul Duffin0f8faff2020-05-20 16:18:00 +01002494
2495 if len(scopeProperties.Stub_srcs) > 0 {
2496 // Add dependencies to the prebuilt stubs source library
Paul Duffin864116c2021-04-02 10:24:13 +01002497 ctx.AddVariationDependencies(nil, apiScope.stubsSourceTag, android.PrebuiltNameFromSource(module.stubsSourceModuleName(apiScope)))
Paul Duffin0f8faff2020-05-20 16:18:00 +01002498 }
Paul Duffin56d44902020-01-31 13:36:25 +00002499 }
Paul Duffin44f1d842020-06-26 20:17:02 +01002500}
2501
2502// Add other dependencies as normal.
2503func (module *SdkLibraryImport) DepsMutator(ctx android.BottomUpMutatorContext) {
Paul Duffineedc5d52020-06-12 17:46:39 +01002504
2505 implName := module.implLibraryModuleName()
2506 if ctx.OtherModuleExists(implName) {
2507 ctx.AddVariationDependencies(nil, implLibraryTag, implName)
2508
2509 xmlPermissionsModuleName := module.xmlPermissionsModuleName()
2510 if module.sharedLibrary() && ctx.OtherModuleExists(xmlPermissionsModuleName) {
2511 // Add dependency to the rule for generating the xml permissions file
2512 ctx.AddDependency(module, xmlPermissionsFileTag, xmlPermissionsModuleName)
2513 }
2514 }
Colin Cross79c7c262019-04-17 11:11:46 -07002515}
2516
Jiakai Zhang204356f2021-09-09 08:12:46 +00002517func (module *SdkLibraryImport) AndroidMkEntries() []android.AndroidMkEntries {
2518 // For an SDK library imported from a prebuilt APEX, we don't need a Make module for itself, as we
2519 // don't need to install it. However, we need to add its dexpreopt outputs as sub-modules, if it
2520 // is preopted.
2521 dexpreoptEntries := module.dexpreopter.AndroidMkEntriesForApex()
2522 return append(dexpreoptEntries, android.AndroidMkEntries{Disabled: true})
2523}
2524
Jiyong Park45bf82e2020-12-15 22:29:02 +09002525var _ android.ApexModule = (*SdkLibraryImport)(nil)
2526
2527// Implements android.ApexModule
Paul Duffineedc5d52020-06-12 17:46:39 +01002528func (module *SdkLibraryImport) DepIsInSameApex(mctx android.BaseModuleContext, dep android.Module) bool {
2529 depTag := mctx.OtherModuleDependencyTag(dep)
2530 if depTag == xmlPermissionsFileTag {
2531 return true
2532 }
2533
2534 // None of the other dependencies of the java_sdk_library_import are in the same apex
2535 // as the one that references this module.
2536 return false
2537}
2538
Jiyong Park45bf82e2020-12-15 22:29:02 +09002539// Implements android.ApexModule
Dan Albertc8060532020-07-22 22:32:17 -07002540func (module *SdkLibraryImport) ShouldSupportSdkVersion(ctx android.BaseModuleContext,
2541 sdkVersion android.ApiLevel) error {
Jooyung Han749dc692020-04-15 11:03:39 +09002542 // we don't check prebuilt modules for sdk_version
2543 return nil
2544}
2545
Paul Duffinea8f8082021-06-24 13:25:57 +01002546// Implements android.ApexModule
2547func (module *SdkLibraryImport) UniqueApexVariations() bool {
2548 return module.uniqueApexVariations()
2549}
2550
Paul Duffin09817d62022-04-28 17:45:11 +01002551// MinSdkVersion - Implements hiddenAPIModule
Spandan Das8c9ae7e2023-03-03 21:20:36 +00002552func (module *SdkLibraryImport) MinSdkVersion(ctx android.EarlyModuleContext) android.ApiLevel {
2553 return android.NoneApiLevel
Paul Duffin09817d62022-04-28 17:45:11 +01002554}
2555
2556var _ hiddenAPIModule = (*SdkLibraryImport)(nil)
2557
Paul Duffineedc5d52020-06-12 17:46:39 +01002558func (module *SdkLibraryImport) OutputFiles(tag string) (android.Paths, error) {
Paul Duffin1e940d52022-04-29 14:21:25 +01002559 paths, err := module.commonOutputFiles(tag)
2560 if paths != nil || err != nil {
2561 return paths, err
2562 }
2563 if module.implLibraryModule != nil {
2564 return module.implLibraryModule.OutputFiles(tag)
2565 } else {
2566 return nil, nil
2567 }
Paul Duffin46dc45a2020-05-14 15:39:10 +01002568}
2569
Paul Duffineedc5d52020-06-12 17:46:39 +01002570func (module *SdkLibraryImport) GenerateAndroidBuildActions(ctx android.ModuleContext) {
Paul Duffina2ae7e02020-09-11 11:55:00 +01002571 module.generateCommonBuildActions(ctx)
2572
Jeongik Chad5fe8782021-07-08 01:13:11 +09002573 // Assume that source module(sdk_library) is installed in /<sdk_library partition>/framework
2574 module.installFile = android.PathForModuleInstall(ctx, "framework", module.Stem()+".jar")
2575
Paul Duffin0f8faff2020-05-20 16:18:00 +01002576 // Record the paths to the prebuilt stubs library and stubs source.
Colin Cross79c7c262019-04-17 11:11:46 -07002577 ctx.VisitDirectDeps(func(to android.Module) {
2578 tag := ctx.OtherModuleDependencyTag(to)
2579
Paul Duffin0f8faff2020-05-20 16:18:00 +01002580 // Extract information from any of the scope specific dependencies.
2581 if scopeTag, ok := tag.(scopeDependencyTag); ok {
2582 apiScope := scopeTag.apiScope
2583 scopePaths := module.getScopePathsCreateIfNeeded(apiScope)
2584
2585 // Extract information from the dependency. The exact information extracted
2586 // is determined by the nature of the dependency which is determined by the tag.
2587 scopeTag.extractDepInfo(ctx, to, scopePaths)
Paul Duffineedc5d52020-06-12 17:46:39 +01002588 } else if tag == implLibraryTag {
2589 if implLibrary, ok := to.(*Library); ok {
2590 module.implLibraryModule = implLibrary
2591 } else {
2592 ctx.ModuleErrorf("implementation library must be of type *java.Library but was %T", to)
2593 }
2594 } else if tag == xmlPermissionsFileTag {
2595 if xmlPermissionsFileModule, ok := to.(*sdkLibraryXml); ok {
2596 module.xmlPermissionsFileModule = xmlPermissionsFileModule
2597 } else {
2598 ctx.ModuleErrorf("xml permissions file module must be of type *sdkLibraryXml but was %T", to)
2599 }
Colin Cross79c7c262019-04-17 11:11:46 -07002600 }
2601 })
Paul Duffin0f8faff2020-05-20 16:18:00 +01002602
2603 // Populate the scope paths with information from the properties.
2604 for apiScope, scopeProperties := range module.scopeProperties {
2605 if len(scopeProperties.Jars) == 0 {
2606 continue
2607 }
2608
2609 paths := module.getScopePathsCreateIfNeeded(apiScope)
Anton Hanssond78eb762021-09-21 15:25:12 +01002610 paths.annotationsZip = android.OptionalPathForModuleSrc(ctx, scopeProperties.Annotations)
Paul Duffin0f8faff2020-05-20 16:18:00 +01002611 paths.currentApiFilePath = android.OptionalPathForModuleSrc(ctx, scopeProperties.Current_api)
2612 paths.removedApiFilePath = android.OptionalPathForModuleSrc(ctx, scopeProperties.Removed_api)
2613 }
Paul Duffin39853512021-02-26 11:09:39 +00002614
2615 if ctx.Device() {
2616 // If this is a variant created for a prebuilt_apex then use the dex implementation jar
2617 // obtained from the associated deapexer module.
2618 ai := ctx.Provider(android.ApexInfoProvider).(android.ApexInfo)
2619 if ai.ForPrebuiltApex {
Paul Duffin39853512021-02-26 11:09:39 +00002620 // Get the path of the dex implementation jar from the `deapexer` module.
Martin Stjernholm44825602021-09-17 01:44:12 +01002621 di := android.FindDeapexerProviderForModule(ctx)
2622 if di == nil {
2623 return // An error has been reported by FindDeapexerProviderForModule.
2624 }
Jiakai Zhang81e46812023-02-08 21:56:07 +08002625 dexJarFileApexRootRelative := apexRootRelativePathToJavaLib(module.BaseModuleName())
2626 if dexOutputPath := di.PrebuiltExportPath(dexJarFileApexRootRelative); dexOutputPath != nil {
Martin Stjernholm8be1e6d2021-09-15 03:34:04 +01002627 dexJarFile := makeDexJarPathFromPath(dexOutputPath)
2628 module.dexJarFile = dexJarFile
Jiakai Zhang204356f2021-09-09 08:12:46 +00002629 installPath := android.PathForModuleInPartitionInstall(
Jiakai Zhang81e46812023-02-08 21:56:07 +08002630 ctx, "apex", ai.ApexVariationName, dexJarFileApexRootRelative)
Jiakai Zhang204356f2021-09-09 08:12:46 +00002631 module.installFile = installPath
Martin Stjernholm8be1e6d2021-09-15 03:34:04 +01002632 module.initHiddenAPI(ctx, dexJarFile, module.findScopePaths(apiScopePublic).stubsImplPath[0], nil)
Jiakai Zhang204356f2021-09-09 08:12:46 +00002633
Jiakai Zhang204356f2021-09-09 08:12:46 +00002634 module.dexpreopter.installPath = module.dexpreopter.getInstallPath(ctx, installPath)
2635 module.dexpreopter.isSDKLibrary = true
2636 module.dexpreopter.uncompressedDex = shouldUncompressDex(ctx, &module.dexpreopter)
Jiakai Zhang81e46812023-02-08 21:56:07 +08002637
2638 if profilePath := di.PrebuiltExportPath(dexJarFileApexRootRelative + ".prof"); profilePath != nil {
2639 module.dexpreopter.inputProfilePathOnHost = profilePath
2640 }
2641
2642 // Dexpreopting.
Jiakai Zhang204356f2021-09-09 08:12:46 +00002643 module.dexpreopt(ctx, dexOutputPath)
Paul Duffin39853512021-02-26 11:09:39 +00002644 } else {
2645 // This should never happen as a variant for a prebuilt_apex is only created if the
2646 // prebuilt_apex has been configured to export the java library dex file.
Martin Stjernholm44825602021-09-17 01:44:12 +01002647 ctx.ModuleErrorf("internal error: no dex implementation jar available from prebuilt APEX %s", di.ApexModuleName())
Paul Duffin39853512021-02-26 11:09:39 +00002648 }
2649 }
2650 }
Colin Cross79c7c262019-04-17 11:11:46 -07002651}
2652
Jiyong Parkf1691d22021-03-29 20:11:58 +09002653func (module *SdkLibraryImport) sdkJars(ctx android.BaseModuleContext, sdkVersion android.SdkSpec, headerJars bool) android.Paths {
Paul Duffineedc5d52020-06-12 17:46:39 +01002654
2655 // For consistency with SdkLibrary make the implementation jar available to libraries that
2656 // are within the same APEX.
2657 implLibraryModule := module.implLibraryModule
Colin Cross56a83212020-09-15 18:30:11 -07002658 if implLibraryModule != nil && withinSameApexesAs(ctx, module) {
Paul Duffineedc5d52020-06-12 17:46:39 +01002659 if headerJars {
2660 return implLibraryModule.HeaderJars()
2661 } else {
2662 return implLibraryModule.ImplementationJars()
2663 }
2664 }
2665
Paul Duffin23970f42020-05-20 14:20:02 +01002666 return module.selectHeaderJarsForSdkVersion(ctx, sdkVersion)
Paul Duffin56d44902020-01-31 13:36:25 +00002667}
2668
Colin Cross79c7c262019-04-17 11:11:46 -07002669// to satisfy SdkLibraryDependency interface
Jiyong Parkf1691d22021-03-29 20:11:58 +09002670func (module *SdkLibraryImport) SdkHeaderJars(ctx android.BaseModuleContext, sdkVersion android.SdkSpec) android.Paths {
Colin Cross79c7c262019-04-17 11:11:46 -07002671 // This module is just a wrapper for the prebuilt stubs.
Paul Duffineedc5d52020-06-12 17:46:39 +01002672 return module.sdkJars(ctx, sdkVersion, true)
Colin Cross79c7c262019-04-17 11:11:46 -07002673}
2674
2675// to satisfy SdkLibraryDependency interface
Jiyong Parkf1691d22021-03-29 20:11:58 +09002676func (module *SdkLibraryImport) SdkImplementationJars(ctx android.BaseModuleContext, sdkVersion android.SdkSpec) android.Paths {
Colin Cross79c7c262019-04-17 11:11:46 -07002677 // This module is just a wrapper for the stubs.
Paul Duffineedc5d52020-06-12 17:46:39 +01002678 return module.sdkJars(ctx, sdkVersion, false)
2679}
2680
Ulya Trafimovichdbf31662020-12-17 12:07:54 +00002681// to satisfy UsesLibraryDependency interface
Martin Stjernholm8be1e6d2021-09-15 03:34:04 +01002682func (module *SdkLibraryImport) DexJarBuildPath() OptionalDexJarPath {
Paul Duffin39853512021-02-26 11:09:39 +00002683 // The dex implementation jar extracted from the .apex file should be used in preference to the
2684 // source.
Martin Stjernholm8be1e6d2021-09-15 03:34:04 +01002685 if module.dexJarFile.IsSet() {
Paul Duffin39853512021-02-26 11:09:39 +00002686 return module.dexJarFile
2687 }
Paul Duffineedc5d52020-06-12 17:46:39 +01002688 if module.implLibraryModule == nil {
Martin Stjernholm8be1e6d2021-09-15 03:34:04 +01002689 return makeUnsetDexJarPath()
Paul Duffineedc5d52020-06-12 17:46:39 +01002690 } else {
2691 return module.implLibraryModule.DexJarBuildPath()
2692 }
2693}
2694
Ulya Trafimovichdbf31662020-12-17 12:07:54 +00002695// to satisfy UsesLibraryDependency interface
Ulya Trafimovich31e444e2020-08-14 17:32:16 +01002696func (module *SdkLibraryImport) DexJarInstallPath() android.Path {
Jeongik Chad5fe8782021-07-08 01:13:11 +09002697 return module.installFile
Ulya Trafimovich31e444e2020-08-14 17:32:16 +01002698}
2699
Ulya Trafimovichdbf31662020-12-17 12:07:54 +00002700// to satisfy UsesLibraryDependency interface
2701func (module *SdkLibraryImport) ClassLoaderContexts() dexpreopt.ClassLoaderContextMap {
2702 return nil
2703}
2704
Paul Duffineedc5d52020-06-12 17:46:39 +01002705// to satisfy apex.javaDependency interface
2706func (module *SdkLibraryImport) JacocoReportClassesFile() android.Path {
2707 if module.implLibraryModule == nil {
2708 return nil
2709 } else {
2710 return module.implLibraryModule.JacocoReportClassesFile()
2711 }
2712}
2713
2714// to satisfy apex.javaDependency interface
Colin Cross08dca382020-07-21 20:31:17 -07002715func (module *SdkLibraryImport) LintDepSets() LintDepSets {
2716 if module.implLibraryModule == nil {
2717 return LintDepSets{}
2718 } else {
2719 return module.implLibraryModule.LintDepSets()
2720 }
2721}
2722
Spandan Das17854f52022-01-14 21:19:14 +00002723func (module *SdkLibraryImport) GetStrictUpdatabilityLinting() bool {
Jaewoong Jung476b9d62021-05-10 15:30:00 -07002724 if module.implLibraryModule == nil {
2725 return false
2726 } else {
Spandan Das17854f52022-01-14 21:19:14 +00002727 return module.implLibraryModule.GetStrictUpdatabilityLinting()
Jaewoong Jung476b9d62021-05-10 15:30:00 -07002728 }
2729}
2730
Spandan Das17854f52022-01-14 21:19:14 +00002731func (module *SdkLibraryImport) SetStrictUpdatabilityLinting(strictLinting bool) {
Jaewoong Jung476b9d62021-05-10 15:30:00 -07002732 if module.implLibraryModule != nil {
Spandan Das17854f52022-01-14 21:19:14 +00002733 module.implLibraryModule.SetStrictUpdatabilityLinting(strictLinting)
Jaewoong Jung476b9d62021-05-10 15:30:00 -07002734 }
2735}
2736
Colin Cross08dca382020-07-21 20:31:17 -07002737// to satisfy apex.javaDependency interface
Paul Duffineedc5d52020-06-12 17:46:39 +01002738func (module *SdkLibraryImport) Stem() string {
2739 return module.BaseModuleName()
Colin Cross79c7c262019-04-17 11:11:46 -07002740}
Jiyong Parke3833882020-02-17 17:28:10 +09002741
Paul Duffin44b481b2020-06-17 16:59:43 +01002742var _ ApexDependency = (*SdkLibraryImport)(nil)
2743
2744// to satisfy java.ApexDependency interface
2745func (module *SdkLibraryImport) HeaderJars() android.Paths {
2746 if module.implLibraryModule == nil {
2747 return nil
2748 } else {
2749 return module.implLibraryModule.HeaderJars()
2750 }
2751}
2752
2753// to satisfy java.ApexDependency interface
2754func (module *SdkLibraryImport) ImplementationAndResourcesJars() android.Paths {
2755 if module.implLibraryModule == nil {
2756 return nil
2757 } else {
2758 return module.implLibraryModule.ImplementationAndResourcesJars()
2759 }
2760}
2761
Jiakai Zhang204356f2021-09-09 08:12:46 +00002762// to satisfy java.DexpreopterInterface interface
2763func (module *SdkLibraryImport) IsInstallable() bool {
2764 return true
2765}
2766
Paul Duffinfef55002021-06-17 14:56:05 +01002767var _ android.RequiredFilesFromPrebuiltApex = (*SdkLibraryImport)(nil)
2768
Paul Duffinb4bbf2c2021-06-17 15:59:07 +01002769func (module *SdkLibraryImport) RequiredFilesFromPrebuiltApex(ctx android.BaseModuleContext) []string {
Paul Duffinfef55002021-06-17 14:56:05 +01002770 name := module.BaseModuleName()
Jiakai Zhang81e46812023-02-08 21:56:07 +08002771 return requiredFilesFromPrebuiltApexForImport(name, &module.dexpreopter)
Paul Duffinfef55002021-06-17 14:56:05 +01002772}
2773
Jiyong Parke3833882020-02-17 17:28:10 +09002774// java_sdk_library_xml
Jiyong Parke3833882020-02-17 17:28:10 +09002775type sdkLibraryXml struct {
2776 android.ModuleBase
2777 android.DefaultableModuleBase
2778 android.ApexModuleBase
2779
2780 properties sdkLibraryXmlProperties
2781
2782 outputFilePath android.OutputPath
2783 installDirPath android.InstallPath
Colin Cross56a83212020-09-15 18:30:11 -07002784
2785 hideApexVariantFromMake bool
Jiyong Parke3833882020-02-17 17:28:10 +09002786}
2787
2788type sdkLibraryXmlProperties struct {
2789 // canonical name of the lib
2790 Lib_name *string
Pedro Loureiro9956e5e2021-09-07 17:21:59 +00002791
2792 // Signals that this shared library is part of the bootclasspath starting
2793 // on the version indicated in this attribute.
2794 //
2795 // This will make platforms at this level and above to ignore
2796 // <uses-library> tags with this library name because the library is already
2797 // available
2798 On_bootclasspath_since *string
2799
2800 // Signals that this shared library was part of the bootclasspath before
2801 // (but not including) the version indicated in this attribute.
2802 //
2803 // The system will automatically add a <uses-library> tag with this library to
2804 // apps that target any SDK less than the version indicated in this attribute.
2805 On_bootclasspath_before *string
2806
2807 // Indicates that PackageManager should ignore this shared library if the
2808 // platform is below the version indicated in this attribute.
2809 //
2810 // This means that the device won't recognise this library as installed.
2811 Min_device_sdk *string
2812
2813 // Indicates that PackageManager should ignore this shared library if the
2814 // platform is above the version indicated in this attribute.
2815 //
2816 // This means that the device won't recognise this library as installed.
2817 Max_device_sdk *string
Pedro Loureiroc3621422021-09-28 15:40:23 +00002818
2819 // The SdkLibrary's min api level as a string
2820 //
2821 // This value comes from the ApiLevel of the MinSdkVersion property.
2822 Sdk_library_min_api_level *string
Jiyong Parke3833882020-02-17 17:28:10 +09002823}
2824
2825// java_sdk_library_xml builds the permission xml file for a java_sdk_library.
2826// Not to be used directly by users. java_sdk_library internally uses this.
2827func sdkLibraryXmlFactory() android.Module {
2828 module := &sdkLibraryXml{}
2829
2830 module.AddProperties(&module.properties)
2831
2832 android.InitApexModule(module)
2833 android.InitAndroidArchModule(module, android.DeviceSupported, android.MultilibCommon)
2834
2835 return module
2836}
2837
Colin Crossaede88c2020-08-11 12:17:01 -07002838func (module *sdkLibraryXml) UniqueApexVariations() bool {
2839 // sdkLibraryXml needs a unique variation per APEX because the generated XML file contains the path to the
2840 // mounted APEX, which contains the name of the APEX.
2841 return true
2842}
2843
Jiyong Parke3833882020-02-17 17:28:10 +09002844// from android.PrebuiltEtcModule
Jooyung Han0703fd82020-08-26 22:11:53 +09002845func (module *sdkLibraryXml) BaseDir() string {
2846 return "etc"
2847}
2848
2849// from android.PrebuiltEtcModule
Jiyong Parke3833882020-02-17 17:28:10 +09002850func (module *sdkLibraryXml) SubDir() string {
2851 return "permissions"
2852}
2853
2854// from android.PrebuiltEtcModule
2855func (module *sdkLibraryXml) OutputFile() android.OutputPath {
2856 return module.outputFilePath
2857}
2858
2859// from android.ApexModule
2860func (module *sdkLibraryXml) AvailableFor(what string) bool {
2861 return true
2862}
2863
2864func (module *sdkLibraryXml) DepsMutator(ctx android.BottomUpMutatorContext) {
2865 // do nothing
2866}
2867
Jiyong Park45bf82e2020-12-15 22:29:02 +09002868var _ android.ApexModule = (*sdkLibraryXml)(nil)
2869
2870// Implements android.ApexModule
Dan Albertc8060532020-07-22 22:32:17 -07002871func (module *sdkLibraryXml) ShouldSupportSdkVersion(ctx android.BaseModuleContext,
2872 sdkVersion android.ApiLevel) error {
Jooyung Han749dc692020-04-15 11:03:39 +09002873 // sdkLibraryXml doesn't need to be checked separately because java_sdk_library is checked
2874 return nil
2875}
2876
Jiyong Parke3833882020-02-17 17:28:10 +09002877// File path to the runtime implementation library
Colin Cross56a83212020-09-15 18:30:11 -07002878func (module *sdkLibraryXml) implPath(ctx android.ModuleContext) string {
Jiyong Parke3833882020-02-17 17:28:10 +09002879 implName := proptools.String(module.properties.Lib_name)
Colin Cross56a83212020-09-15 18:30:11 -07002880 if apexInfo := ctx.Provider(android.ApexInfoProvider).(android.ApexInfo); !apexInfo.IsForPlatform() {
Colin Crosse07f2312020-08-13 11:24:56 -07002881 // TODO(b/146468504): ApexVariationName() is only a soong module name, not apex name.
Jiyong Parke3833882020-02-17 17:28:10 +09002882 // In most cases, this works fine. But when apex_name is set or override_apex is used
2883 // this can be wrong.
Colin Cross56a83212020-09-15 18:30:11 -07002884 return fmt.Sprintf("/apex/%s/javalib/%s.jar", apexInfo.ApexVariationName, implName)
Jiyong Parke3833882020-02-17 17:28:10 +09002885 }
2886 partition := "system"
2887 if module.SocSpecific() {
2888 partition = "vendor"
2889 } else if module.DeviceSpecific() {
2890 partition = "odm"
2891 } else if module.ProductSpecific() {
2892 partition = "product"
2893 } else if module.SystemExtSpecific() {
2894 partition = "system_ext"
2895 }
2896 return "/" + partition + "/framework/" + implName + ".jar"
2897}
2898
Pedro Loureiro9956e5e2021-09-07 17:21:59 +00002899func formattedOptionalSdkLevelAttribute(ctx android.ModuleContext, attrName string, value *string) string {
2900 if value == nil {
2901 return ""
2902 }
2903 apiLevel, err := android.ApiLevelFromUser(ctx, *value)
2904 if err != nil {
Pedro Loureiroba6682f2021-10-29 09:32:32 +00002905 // attributes in bp files have underscores but in the xml have dashes.
2906 ctx.PropertyErrorf(strings.ReplaceAll(attrName, "-", "_"), err.Error())
Pedro Loureiro9956e5e2021-09-07 17:21:59 +00002907 return ""
2908 }
Pedro Loureirob638c622021-12-22 15:28:05 +00002909 if apiLevel.IsCurrent() {
2910 // passing "current" would always mean a future release, never the current (or the current in
2911 // progress) which means some conditions would never be triggered.
2912 ctx.PropertyErrorf(strings.ReplaceAll(attrName, "-", "_"),
2913 `"current" is not an allowed value for this attribute`)
2914 return ""
2915 }
Pedro Loureiro48991222022-06-17 20:01:21 +00002916 // "safeValue" is safe because it translates finalized codenames to a string
2917 // with their SDK int.
2918 safeValue := apiLevel.String()
2919 return formattedOptionalAttribute(attrName, &safeValue)
Pedro Loureiro9956e5e2021-09-07 17:21:59 +00002920}
2921
2922// formats an attribute for the xml permissions file if the value is not null
2923// returns empty string otherwise
2924func formattedOptionalAttribute(attrName string, value *string) string {
2925 if value == nil {
2926 return ""
2927 }
2928 return fmt.Sprintf(` %s=\"%s\"\n`, attrName, *value)
2929}
2930
2931func (module *sdkLibraryXml) permissionsContents(ctx android.ModuleContext) string {
2932 libName := proptools.String(module.properties.Lib_name)
2933 libNameAttr := formattedOptionalAttribute("name", &libName)
2934 filePath := module.implPath(ctx)
2935 filePathAttr := formattedOptionalAttribute("file", &filePath)
Pedro Loureiroba6682f2021-10-29 09:32:32 +00002936 implicitFromAttr := formattedOptionalSdkLevelAttribute(ctx, "on-bootclasspath-since", module.properties.On_bootclasspath_since)
2937 implicitUntilAttr := formattedOptionalSdkLevelAttribute(ctx, "on-bootclasspath-before", module.properties.On_bootclasspath_before)
2938 minSdkAttr := formattedOptionalSdkLevelAttribute(ctx, "min-device-sdk", module.properties.Min_device_sdk)
2939 maxSdkAttr := formattedOptionalSdkLevelAttribute(ctx, "max-device-sdk", module.properties.Max_device_sdk)
Pedro Loureiro196d3e62021-12-22 19:53:01 +00002940 // <library> is understood in all android versions whereas <apex-library> is only understood from API T (and ignored before that).
2941 // 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 +00002942 var libraryTag string
2943 if module.properties.Min_device_sdk != nil {
Pedro Loureiro196d3e62021-12-22 19:53:01 +00002944 libraryTag = ` <apex-library\n`
Pedro Loureiroc3621422021-09-28 15:40:23 +00002945 } else {
2946 libraryTag = ` <library\n`
2947 }
Pedro Loureiro9956e5e2021-09-07 17:21:59 +00002948
2949 return strings.Join([]string{
2950 `<?xml version=\"1.0\" encoding=\"utf-8\"?>\n`,
2951 `<!-- Copyright (C) 2018 The Android Open Source Project\n`,
2952 `\n`,
2953 ` Licensed under the Apache License, Version 2.0 (the \"License\");\n`,
2954 ` you may not use this file except in compliance with the License.\n`,
2955 ` You may obtain a copy of the License at\n`,
2956 `\n`,
2957 ` http://www.apache.org/licenses/LICENSE-2.0\n`,
2958 `\n`,
2959 ` Unless required by applicable law or agreed to in writing, software\n`,
2960 ` distributed under the License is distributed on an \"AS IS\" BASIS,\n`,
2961 ` WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\n`,
2962 ` See the License for the specific language governing permissions and\n`,
2963 ` limitations under the License.\n`,
2964 `-->\n`,
2965 `<permissions>\n`,
Pedro Loureiroc3621422021-09-28 15:40:23 +00002966 libraryTag,
Pedro Loureiro9956e5e2021-09-07 17:21:59 +00002967 libNameAttr,
2968 filePathAttr,
2969 implicitFromAttr,
2970 implicitUntilAttr,
2971 minSdkAttr,
2972 maxSdkAttr,
2973 ` />\n`,
2974 `</permissions>\n`}, "")
2975}
2976
Jiyong Parke3833882020-02-17 17:28:10 +09002977func (module *sdkLibraryXml) GenerateAndroidBuildActions(ctx android.ModuleContext) {
Colin Cross56a83212020-09-15 18:30:11 -07002978 module.hideApexVariantFromMake = !ctx.Provider(android.ApexInfoProvider).(android.ApexInfo).IsForPlatform()
2979
Jiyong Parke3833882020-02-17 17:28:10 +09002980 libName := proptools.String(module.properties.Lib_name)
Pedro Loureiroc3621422021-09-28 15:40:23 +00002981 module.selfValidate(ctx)
Pedro Loureiro9956e5e2021-09-07 17:21:59 +00002982 xmlContent := module.permissionsContents(ctx)
Jiyong Parke3833882020-02-17 17:28:10 +09002983
2984 module.outputFilePath = android.PathForModuleOut(ctx, libName+".xml").OutputPath
Colin Crossf1a035e2020-11-16 17:32:30 -08002985 rule := android.NewRuleBuilder(pctx, ctx)
Jiyong Parke3833882020-02-17 17:28:10 +09002986 rule.Command().
2987 Text("/bin/bash -c \"echo -e '" + xmlContent + "'\" > ").
2988 Output(module.outputFilePath)
2989
Colin Crossf1a035e2020-11-16 17:32:30 -08002990 rule.Build("java_sdk_xml", "Permission XML")
Jiyong Parke3833882020-02-17 17:28:10 +09002991
2992 module.installDirPath = android.PathForModuleInstall(ctx, "etc", module.SubDir())
2993}
2994
2995func (module *sdkLibraryXml) AndroidMkEntries() []android.AndroidMkEntries {
Colin Cross56a83212020-09-15 18:30:11 -07002996 if module.hideApexVariantFromMake {
satayev8f088b02021-12-06 11:40:46 +00002997 return []android.AndroidMkEntries{{
Jiyong Parke3833882020-02-17 17:28:10 +09002998 Disabled: true,
2999 }}
3000 }
3001
satayev8f088b02021-12-06 11:40:46 +00003002 return []android.AndroidMkEntries{{
Jiyong Parke3833882020-02-17 17:28:10 +09003003 Class: "ETC",
3004 OutputFile: android.OptionalPathForPath(module.outputFilePath),
3005 ExtraEntries: []android.AndroidMkExtraEntriesFunc{
Colin Crossaa255532020-07-03 13:18:24 -07003006 func(ctx android.AndroidMkExtraEntriesContext, entries *android.AndroidMkEntries) {
Jiyong Parke3833882020-02-17 17:28:10 +09003007 entries.SetString("LOCAL_MODULE_TAGS", "optional")
Colin Crossc68db4b2021-11-11 18:59:15 -08003008 entries.SetString("LOCAL_MODULE_PATH", module.installDirPath.String())
Jiyong Parke3833882020-02-17 17:28:10 +09003009 entries.SetString("LOCAL_INSTALLED_MODULE_STEM", module.outputFilePath.Base())
3010 },
3011 },
3012 }}
3013}
Paul Duffindd46f712020-02-10 13:37:10 +00003014
Pedro Loureiroc3621422021-09-28 15:40:23 +00003015func (module *sdkLibraryXml) selfValidate(ctx android.ModuleContext) {
3016 module.validateAtLeastTAttributes(ctx)
3017 module.validateMinAndMaxDeviceSdk(ctx)
3018 module.validateMinMaxDeviceSdkAndModuleMinSdk(ctx)
3019 module.validateOnBootclasspathBeforeRequirements(ctx)
3020}
3021
3022func (module *sdkLibraryXml) validateAtLeastTAttributes(ctx android.ModuleContext) {
3023 t := android.ApiLevelOrPanic(ctx, "Tiramisu")
3024 module.attrAtLeastT(ctx, t, module.properties.Min_device_sdk, "min_device_sdk")
3025 module.attrAtLeastT(ctx, t, module.properties.Max_device_sdk, "max_device_sdk")
3026 module.attrAtLeastT(ctx, t, module.properties.On_bootclasspath_before, "on_bootclasspath_before")
3027 module.attrAtLeastT(ctx, t, module.properties.On_bootclasspath_since, "on_bootclasspath_since")
3028}
3029
3030func (module *sdkLibraryXml) attrAtLeastT(ctx android.ModuleContext, t android.ApiLevel, attr *string, attrName string) {
3031 if attr != nil {
3032 if level, err := android.ApiLevelFromUser(ctx, *attr); err == nil {
3033 // we will inform the user of invalid inputs when we try to write the
3034 // permissions xml file so we don't need to do it here
3035 if t.GreaterThan(level) {
3036 ctx.PropertyErrorf(attrName, "Attribute value needs to be at least T")
3037 }
3038 }
3039 }
3040}
3041
3042func (module *sdkLibraryXml) validateMinAndMaxDeviceSdk(ctx android.ModuleContext) {
3043 if module.properties.Min_device_sdk != nil && module.properties.Max_device_sdk != nil {
3044 min, minErr := android.ApiLevelFromUser(ctx, *module.properties.Min_device_sdk)
3045 max, maxErr := android.ApiLevelFromUser(ctx, *module.properties.Max_device_sdk)
3046 if minErr == nil && maxErr == nil {
3047 // we will inform the user of invalid inputs when we try to write the
3048 // permissions xml file so we don't need to do it here
3049 if min.GreaterThan(max) {
3050 ctx.ModuleErrorf("min_device_sdk can't be greater than max_device_sdk")
3051 }
3052 }
3053 }
3054}
3055
3056func (module *sdkLibraryXml) validateMinMaxDeviceSdkAndModuleMinSdk(ctx android.ModuleContext) {
3057 moduleMinApi := android.ApiLevelOrPanic(ctx, *module.properties.Sdk_library_min_api_level)
3058 if module.properties.Min_device_sdk != nil {
3059 api, err := android.ApiLevelFromUser(ctx, *module.properties.Min_device_sdk)
3060 if err == nil {
3061 if moduleMinApi.GreaterThan(api) {
3062 ctx.PropertyErrorf("min_device_sdk", "Can't be less than module's min sdk (%s)", moduleMinApi)
3063 }
3064 }
3065 }
3066 if module.properties.Max_device_sdk != nil {
3067 api, err := android.ApiLevelFromUser(ctx, *module.properties.Max_device_sdk)
3068 if err == nil {
3069 if moduleMinApi.GreaterThan(api) {
3070 ctx.PropertyErrorf("max_device_sdk", "Can't be less than module's min sdk (%s)", moduleMinApi)
3071 }
3072 }
3073 }
3074}
3075
3076func (module *sdkLibraryXml) validateOnBootclasspathBeforeRequirements(ctx android.ModuleContext) {
3077 moduleMinApi := android.ApiLevelOrPanic(ctx, *module.properties.Sdk_library_min_api_level)
3078 if module.properties.On_bootclasspath_before != nil {
3079 t := android.ApiLevelOrPanic(ctx, "Tiramisu")
3080 // if we use the attribute, then we need to do this validation
3081 if moduleMinApi.LessThan(t) {
3082 // if minAPi is < T, then we need to have min_device_sdk (which only accepts T+)
3083 if module.properties.Min_device_sdk == nil {
3084 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")
3085 }
3086 }
3087 }
3088}
3089
Paul Duffindd46f712020-02-10 13:37:10 +00003090type sdkLibrarySdkMemberType struct {
3091 android.SdkMemberTypeBase
3092}
3093
Paul Duffin296701e2021-07-14 10:29:36 +01003094func (s *sdkLibrarySdkMemberType) AddDependencies(ctx android.SdkDependencyContext, dependencyTag blueprint.DependencyTag, names []string) {
3095 ctx.AddVariationDependencies(nil, dependencyTag, names...)
Paul Duffindd46f712020-02-10 13:37:10 +00003096}
3097
3098func (s *sdkLibrarySdkMemberType) IsInstance(module android.Module) bool {
3099 _, ok := module.(*SdkLibrary)
3100 return ok
3101}
3102
3103func (s *sdkLibrarySdkMemberType) AddPrebuiltModule(ctx android.SdkMemberContext, member android.SdkMember) android.BpModule {
3104 return ctx.SnapshotBuilder().AddPrebuiltModule(member, "java_sdk_library_import")
3105}
3106
3107func (s *sdkLibrarySdkMemberType) CreateVariantPropertiesStruct() android.SdkMemberProperties {
3108 return &sdkLibrarySdkMemberProperties{}
3109}
3110
Paul Duffin976b0e52021-04-27 23:20:26 +01003111var javaSdkLibrarySdkMemberType = &sdkLibrarySdkMemberType{
3112 android.SdkMemberTypeBase{
3113 PropertyName: "java_sdk_libs",
3114 SupportsSdk: true,
3115 },
3116}
3117
Paul Duffindd46f712020-02-10 13:37:10 +00003118type sdkLibrarySdkMemberProperties struct {
3119 android.SdkMemberPropertiesBase
3120
Paul Duffine8409952022-09-22 16:24:46 +01003121 // Stem name for files in the sdk snapshot.
3122 //
3123 // This is used to construct the path names of various sdk library files in the sdk snapshot to
3124 // make sure that they match the finalized versions of those files in prebuilts/sdk.
3125 //
3126 // This property is marked as keep so that it will be kept in all instances of this struct, will
3127 // not be cleared but will be copied to common structs. That is needed because this field is used
3128 // to construct many file names for other parts of this struct and so it needs to be present in
3129 // all structs. If it was not marked as keep then it would be cleared in some structs and so would
3130 // be unavailable for generating file names if there were other properties that were still set.
3131 Stem string `sdk:"keep"`
3132
Paul Duffindd46f712020-02-10 13:37:10 +00003133 // Scope to per scope properties.
Paul Duffin106a3a42022-01-27 16:39:06 +00003134 Scopes map[*apiScope]*scopeProperties
Paul Duffindd46f712020-02-10 13:37:10 +00003135
Paul Duffin3d1248c2020-04-09 00:10:17 +01003136 // The Java stubs source files.
3137 Stub_srcs []string
Paul Duffinf7a64332020-05-13 16:54:55 +01003138
3139 // The naming scheme.
3140 Naming_scheme *string
Paul Duffind7eb1c22020-05-26 20:57:10 +01003141
3142 // True if the java_sdk_library_import is for a shared library, false
3143 // otherwise.
3144 Shared_library *bool
Paul Duffina2ae7e02020-09-11 11:55:00 +01003145
Paul Duffin1267d872021-04-16 17:21:36 +01003146 // True if the stub imports should produce dex jars.
3147 Compile_dex *bool
3148
Paul Duffina2ae7e02020-09-11 11:55:00 +01003149 // The paths to the doctag files to add to the prebuilt.
3150 Doctag_paths android.Paths
Paul Duffin869de142021-07-15 14:14:41 +01003151
3152 Permitted_packages []string
Pedro Loureiro9956e5e2021-09-07 17:21:59 +00003153
3154 // Signals that this shared library is part of the bootclasspath starting
3155 // on the version indicated in this attribute.
3156 //
3157 // This will make platforms at this level and above to ignore
3158 // <uses-library> tags with this library name because the library is already
3159 // available
3160 On_bootclasspath_since *string
3161
3162 // Signals that this shared library was part of the bootclasspath before
3163 // (but not including) the version indicated in this attribute.
3164 //
3165 // The system will automatically add a <uses-library> tag with this library to
3166 // apps that target any SDK less than the version indicated in this attribute.
3167 On_bootclasspath_before *string
3168
3169 // Indicates that PackageManager should ignore this shared library if the
3170 // platform is below the version indicated in this attribute.
3171 //
3172 // This means that the device won't recognise this library as installed.
3173 Min_device_sdk *string
3174
3175 // Indicates that PackageManager should ignore this shared library if the
3176 // platform is above the version indicated in this attribute.
3177 //
3178 // This means that the device won't recognise this library as installed.
3179 Max_device_sdk *string
Jiakai Zhang9c4dc192023-02-09 00:09:24 +08003180
3181 DexPreoptProfileGuided *bool `supported_build_releases:"UpsideDownCake+"`
Paul Duffindd46f712020-02-10 13:37:10 +00003182}
3183
3184type scopeProperties struct {
Paul Duffin1fd005d2020-04-09 01:08:11 +01003185 Jars android.Paths
3186 StubsSrcJar android.Path
3187 CurrentApiFile android.Path
3188 RemovedApiFile android.Path
Paul Duffine7babdb2022-02-10 13:06:54 +00003189 AnnotationsZip android.Path `supported_build_releases:"Tiramisu+"`
Paul Duffin1fd005d2020-04-09 01:08:11 +01003190 SdkVersion string
Paul Duffindd46f712020-02-10 13:37:10 +00003191}
3192
3193func (s *sdkLibrarySdkMemberProperties) PopulateFromVariant(ctx android.SdkMemberContext, variant android.Module) {
3194 sdk := variant.(*SdkLibrary)
3195
Paul Duffine8409952022-09-22 16:24:46 +01003196 // Copy the stem name for files in the sdk snapshot.
3197 s.Stem = sdk.distStem()
3198
Paul Duffin106a3a42022-01-27 16:39:06 +00003199 s.Scopes = make(map[*apiScope]*scopeProperties)
Paul Duffindd46f712020-02-10 13:37:10 +00003200 for _, apiScope := range allApiScopes {
Paul Duffin803a9562020-05-20 11:52:25 +01003201 paths := sdk.findScopePaths(apiScope)
3202 if paths == nil {
3203 continue
3204 }
3205
Paul Duffindd46f712020-02-10 13:37:10 +00003206 jars := paths.stubsImplPath
3207 if len(jars) > 0 {
3208 properties := scopeProperties{}
3209 properties.Jars = jars
Paul Duffin780c5f42020-05-12 15:52:55 +01003210 properties.SdkVersion = sdk.sdkVersionForStubsLibrary(ctx.SdkModuleContext(), apiScope)
Paul Duffin0f8faff2020-05-20 16:18:00 +01003211 properties.StubsSrcJar = paths.stubsSrcJar.Path()
Paul Duffin10269f12020-06-19 18:39:55 +01003212 if paths.currentApiFilePath.Valid() {
3213 properties.CurrentApiFile = paths.currentApiFilePath.Path()
3214 }
3215 if paths.removedApiFilePath.Valid() {
3216 properties.RemovedApiFile = paths.removedApiFilePath.Path()
3217 }
Anton Hanssond78eb762021-09-21 15:25:12 +01003218 // The annotations zip is only available for modules that set annotations_enabled: true.
3219 if paths.annotationsZip.Valid() {
3220 properties.AnnotationsZip = paths.annotationsZip.Path()
3221 }
Paul Duffin106a3a42022-01-27 16:39:06 +00003222 s.Scopes[apiScope] = &properties
Paul Duffindd46f712020-02-10 13:37:10 +00003223 }
3224 }
3225
Paul Duffindfa131e2020-05-15 20:37:11 +01003226 s.Naming_scheme = sdk.commonSdkLibraryProperties.Naming_scheme
Paul Duffind7eb1c22020-05-26 20:57:10 +01003227 s.Shared_library = proptools.BoolPtr(sdk.sharedLibrary())
Paul Duffin1267d872021-04-16 17:21:36 +01003228 s.Compile_dex = sdk.dexProperties.Compile_dex
Paul Duffina2ae7e02020-09-11 11:55:00 +01003229 s.Doctag_paths = sdk.doctagPaths
Paul Duffin869de142021-07-15 14:14:41 +01003230 s.Permitted_packages = sdk.PermittedPackagesForUpdatableBootJars()
Pedro Loureiro9956e5e2021-09-07 17:21:59 +00003231 s.On_bootclasspath_since = sdk.commonSdkLibraryProperties.On_bootclasspath_since
3232 s.On_bootclasspath_before = sdk.commonSdkLibraryProperties.On_bootclasspath_before
3233 s.Min_device_sdk = sdk.commonSdkLibraryProperties.Min_device_sdk
3234 s.Max_device_sdk = sdk.commonSdkLibraryProperties.Max_device_sdk
Jiakai Zhang9c4dc192023-02-09 00:09:24 +08003235
3236 if sdk.dexpreopter.dexpreoptProperties.Dex_preopt_result.Profile_guided {
3237 s.DexPreoptProfileGuided = proptools.BoolPtr(true)
3238 }
Paul Duffindd46f712020-02-10 13:37:10 +00003239}
3240
3241func (s *sdkLibrarySdkMemberProperties) AddToPropertySet(ctx android.SdkMemberContext, propertySet android.BpPropertySet) {
Paul Duffinf7a64332020-05-13 16:54:55 +01003242 if s.Naming_scheme != nil {
3243 propertySet.AddProperty("naming_scheme", proptools.String(s.Naming_scheme))
3244 }
Paul Duffind7eb1c22020-05-26 20:57:10 +01003245 if s.Shared_library != nil {
3246 propertySet.AddProperty("shared_library", *s.Shared_library)
3247 }
Paul Duffin1267d872021-04-16 17:21:36 +01003248 if s.Compile_dex != nil {
3249 propertySet.AddProperty("compile_dex", *s.Compile_dex)
3250 }
Paul Duffin869de142021-07-15 14:14:41 +01003251 if len(s.Permitted_packages) > 0 {
3252 propertySet.AddProperty("permitted_packages", s.Permitted_packages)
3253 }
Jiakai Zhang9c4dc192023-02-09 00:09:24 +08003254 dexPreoptSet := propertySet.AddPropertySet("dex_preopt")
3255 if s.DexPreoptProfileGuided != nil {
3256 dexPreoptSet.AddProperty("profile_guided", proptools.Bool(s.DexPreoptProfileGuided))
3257 }
Paul Duffinf7a64332020-05-13 16:54:55 +01003258
Paul Duffine8409952022-09-22 16:24:46 +01003259 stem := s.Stem
3260
Paul Duffindd46f712020-02-10 13:37:10 +00003261 for _, apiScope := range allApiScopes {
3262 if properties, ok := s.Scopes[apiScope]; ok {
Paul Duffin6b836ba2020-05-13 19:19:49 +01003263 scopeSet := propertySet.AddPropertySet(apiScope.propertyName)
Paul Duffindd46f712020-02-10 13:37:10 +00003264
Paul Duffin958806b2022-05-16 13:10:47 +00003265 scopeDir := apiScope.snapshotRelativeDir()
Paul Duffin3d1248c2020-04-09 00:10:17 +01003266
Paul Duffindd46f712020-02-10 13:37:10 +00003267 var jars []string
3268 for _, p := range properties.Jars {
Paul Duffine8409952022-09-22 16:24:46 +01003269 dest := filepath.Join(scopeDir, stem+"-stubs.jar")
Paul Duffindd46f712020-02-10 13:37:10 +00003270 ctx.SnapshotBuilder().CopyToSnapshot(p, dest)
3271 jars = append(jars, dest)
3272 }
3273 scopeSet.AddProperty("jars", jars)
3274
Paul Duffin22628d52021-05-12 23:13:22 +01003275 if ctx.SdkModuleContext().Config().IsEnvTrue("SOONG_SDK_SNAPSHOT_USE_SRCJAR") {
3276 // Copy the stubs source jar into the snapshot zip as is.
Paul Duffine8409952022-09-22 16:24:46 +01003277 srcJarSnapshotPath := filepath.Join(scopeDir, stem+".srcjar")
Paul Duffin22628d52021-05-12 23:13:22 +01003278 ctx.SnapshotBuilder().CopyToSnapshot(properties.StubsSrcJar, srcJarSnapshotPath)
3279 scopeSet.AddProperty("stub_srcs", []string{srcJarSnapshotPath})
3280 } else {
3281 // Merge the stubs source jar into the snapshot zip so that when it is unpacked
3282 // the source files are also unpacked.
Paul Duffine8409952022-09-22 16:24:46 +01003283 snapshotRelativeDir := filepath.Join(scopeDir, stem+"_stub_sources")
Paul Duffin22628d52021-05-12 23:13:22 +01003284 ctx.SnapshotBuilder().UnzipToSnapshot(properties.StubsSrcJar, snapshotRelativeDir)
3285 scopeSet.AddProperty("stub_srcs", []string{snapshotRelativeDir})
3286 }
Paul Duffin3d1248c2020-04-09 00:10:17 +01003287
Paul Duffin1fd005d2020-04-09 01:08:11 +01003288 if properties.CurrentApiFile != nil {
Paul Duffine8409952022-09-22 16:24:46 +01003289 currentApiSnapshotPath := apiScope.snapshotRelativeCurrentApiTxtPath(stem)
Paul Duffin1fd005d2020-04-09 01:08:11 +01003290 ctx.SnapshotBuilder().CopyToSnapshot(properties.CurrentApiFile, currentApiSnapshotPath)
3291 scopeSet.AddProperty("current_api", currentApiSnapshotPath)
3292 }
3293
3294 if properties.RemovedApiFile != nil {
Paul Duffine8409952022-09-22 16:24:46 +01003295 removedApiSnapshotPath := apiScope.snapshotRelativeRemovedApiTxtPath(stem)
Paul Duffin3dbf9fd2020-06-02 13:00:02 +01003296 ctx.SnapshotBuilder().CopyToSnapshot(properties.RemovedApiFile, removedApiSnapshotPath)
Paul Duffin1fd005d2020-04-09 01:08:11 +01003297 scopeSet.AddProperty("removed_api", removedApiSnapshotPath)
3298 }
3299
Anton Hanssond78eb762021-09-21 15:25:12 +01003300 if properties.AnnotationsZip != nil {
Paul Duffine8409952022-09-22 16:24:46 +01003301 annotationsSnapshotPath := filepath.Join(scopeDir, stem+"_annotations.zip")
Anton Hanssond78eb762021-09-21 15:25:12 +01003302 ctx.SnapshotBuilder().CopyToSnapshot(properties.AnnotationsZip, annotationsSnapshotPath)
3303 scopeSet.AddProperty("annotations", annotationsSnapshotPath)
3304 }
3305
Paul Duffindd46f712020-02-10 13:37:10 +00003306 if properties.SdkVersion != "" {
3307 scopeSet.AddProperty("sdk_version", properties.SdkVersion)
3308 }
3309 }
3310 }
3311
Paul Duffina2ae7e02020-09-11 11:55:00 +01003312 if len(s.Doctag_paths) > 0 {
3313 dests := []string{}
3314 for _, p := range s.Doctag_paths {
3315 dest := filepath.Join("doctags", p.Rel())
3316 ctx.SnapshotBuilder().CopyToSnapshot(p, dest)
3317 dests = append(dests, dest)
3318 }
3319 propertySet.AddProperty("doctag_files", dests)
3320 }
Paul Duffindd46f712020-02-10 13:37:10 +00003321}