blob: 1de89727f09218c1797133ad513270b97c2ae2ca [file] [log] [blame]
Jiyong Parkc678ad32018-04-10 13:07:10 +09001// Copyright 2018 Google Inc. All rights reserved.
2//
3// Licensed under the Apache License, Version 2.0 (the "License");
4// you may not use this file except in compliance with the License.
5// You may obtain a copy of the License at
6//
7// http://www.apache.org/licenses/LICENSE-2.0
8//
9// Unless required by applicable law or agreed to in writing, software
10// distributed under the License is distributed on an "AS IS" BASIS,
11// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12// See the License for the specific language governing permissions and
13// limitations under the License.
14
15package java
16
17import (
Jiyong Parkc678ad32018-04-10 13:07:10 +090018 "fmt"
19 "path"
Sundong Ahn054b19a2018-10-19 13:46:09 +090020 "path/filepath"
Paul Duffin46a26a82020-04-07 19:27:04 +010021 "reflect"
Paul Duffin46dc45a2020-05-14 15:39:10 +010022 "regexp"
Jiyong Park82484c02018-04-23 21:41:26 +090023 "sort"
Jiyong Parkc678ad32018-04-10 13:07:10 +090024 "strings"
Jiyong Park82484c02018-04-23 21:41:26 +090025 "sync"
Jiyong Parkc678ad32018-04-10 13:07:10 +090026
Chris Parsons39a16972023-06-08 14:28:51 +000027 "android/soong/ui/metrics/bp2build_metrics_proto"
Paul Duffinc0036492023-06-21 15:42:49 +010028
Paul Duffind1b3a922020-01-22 11:57:20 +000029 "github.com/google/blueprint"
Jiyong Parkc678ad32018-04-10 13:07:10 +090030 "github.com/google/blueprint/proptools"
Paul Duffin46a26a82020-04-07 19:27:04 +010031
32 "android/soong/android"
Zi Wangb2179e32023-01-31 15:53:30 -080033 "android/soong/bazel"
Ulya Trafimovichdbf31662020-12-17 12:07:54 +000034 "android/soong/dexpreopt"
Jiyong Parkc678ad32018-04-10 13:07:10 +090035)
36
Jooyung Han58f26ab2019-12-18 15:34:32 +090037const (
Pedro Loureiro9956e5e2021-09-07 17:21:59 +000038 sdkXmlFileSuffix = ".xml"
Jiyong Parkc678ad32018-04-10 13:07:10 +090039)
40
Paul Duffind1b3a922020-01-22 11:57:20 +000041// A tag to associated a dependency with a specific api scope.
42type scopeDependencyTag struct {
43 blueprint.BaseDependencyTag
44 name string
45 apiScope *apiScope
Paul Duffinc8782502020-04-29 20:45:27 +010046
47 // Function for extracting appropriate path information from the dependency.
Colin Crossdcf71b22021-02-01 13:59:03 -080048 depInfoExtractor func(paths *scopePaths, ctx android.ModuleContext, dep android.Module) error
Paul Duffinc8782502020-04-29 20:45:27 +010049}
50
51// Extract tag specific information from the dependency.
52func (tag scopeDependencyTag) extractDepInfo(ctx android.ModuleContext, dep android.Module, paths *scopePaths) {
Colin Crossdcf71b22021-02-01 13:59:03 -080053 err := tag.depInfoExtractor(paths, ctx, dep)
Paul Duffinc8782502020-04-29 20:45:27 +010054 if err != nil {
55 ctx.ModuleErrorf("has an invalid {scopeDependencyTag: %s} dependency on module %s: %s", tag.name, ctx.OtherModuleName(dep), err.Error())
56 }
Paul Duffind1b3a922020-01-22 11:57:20 +000057}
58
Paul Duffin80342d72020-06-26 22:08:43 +010059var _ android.ReplaceSourceWithPrebuilt = (*scopeDependencyTag)(nil)
60
61func (tag scopeDependencyTag) ReplaceSourceWithPrebuilt() bool {
62 return false
63}
64
Paul Duffind1b3a922020-01-22 11:57:20 +000065// Provides information about an api scope, e.g. public, system, test.
66type apiScope struct {
67 // The name of the api scope, e.g. public, system, test
68 name string
69
Paul Duffin97b53b82020-05-05 14:40:52 +010070 // The api scope that this scope extends.
Paul Duffind0b9fca2022-09-30 18:11:41 +010071 //
72 // This organizes the scopes into an extension hierarchy.
73 //
74 // If set this means that the API provided by this scope includes the API provided by the scope
75 // set in this field.
Paul Duffin97b53b82020-05-05 14:40:52 +010076 extends *apiScope
77
Paul Duffind0b9fca2022-09-30 18:11:41 +010078 // The next api scope that a library that uses this scope can access.
79 //
80 // This organizes the scopes into an access hierarchy.
81 //
82 // If set this means that a library that can access this API can also access the API provided by
83 // the scope set in this field.
84 //
85 // A module that sets sdk_version: "<scope>_current" should have access to the <scope> API of
86 // every java_sdk_library that it depends on. If the library does not provide an API for <scope>
87 // then it will traverse up this access hierarchy to find an API that it does provide.
88 //
89 // If this is not set then it defaults to the scope set in extends.
90 canAccess *apiScope
91
Paul Duffin3375e352020-04-28 10:44:03 +010092 // The legacy enabled status for a specific scope can be dependent on other
93 // properties that have been specified on the library so it is provided by
94 // a function that can determine the status by examining those properties.
95 legacyEnabledStatus func(module *SdkLibrary) bool
96
97 // The default enabled status for non-legacy behavior, which is triggered by
98 // explicitly enabling at least one api scope.
99 defaultEnabledStatus bool
100
101 // Gets a pointer to the scope specific properties.
102 scopeSpecificProperties func(module *SdkLibrary) *ApiScopeProperties
103
Paul Duffin46a26a82020-04-07 19:27:04 +0100104 // The name of the field in the dynamically created structure.
105 fieldName string
106
Paul Duffin6b836ba2020-05-13 19:19:49 +0100107 // The name of the property in the java_sdk_library_import
108 propertyName string
109
Paul Duffind1b3a922020-01-22 11:57:20 +0000110 // The tag to use to depend on the stubs library module.
111 stubsTag scopeDependencyTag
112
Paul Duffin0ff08bd2020-04-29 13:30:54 +0100113 // The tag to use to depend on the stubs source module (if separate from the API module).
114 stubsSourceTag scopeDependencyTag
115
116 // The tag to use to depend on the API file generating module (if separate from the stubs source module).
117 apiFileTag scopeDependencyTag
118
Paul Duffinc8782502020-04-29 20:45:27 +0100119 // The tag to use to depend on the stubs source and API module.
120 stubsSourceAndApiTag scopeDependencyTag
Paul Duffind1b3a922020-01-22 11:57:20 +0000121
Paul Duffin958806b2022-05-16 13:10:47 +0000122 // The tag to use to depend on the module that provides the latest version of the API .txt file.
123 latestApiModuleTag scopeDependencyTag
124
125 // The tag to use to depend on the module that provides the latest version of the API removed.txt
126 // file.
127 latestRemovedApiModuleTag scopeDependencyTag
128
Paul Duffind1b3a922020-01-22 11:57:20 +0000129 // The scope specific prefix to add to the api file base of "current.txt" or "removed.txt".
130 apiFilePrefix string
131
Paul Duffind0b9fca2022-09-30 18:11:41 +0100132 // The scope specific suffix to add to the sdk library module name to construct a scope specific
Paul Duffind1b3a922020-01-22 11:57:20 +0000133 // module name.
134 moduleSuffix string
135
Paul Duffind1b3a922020-01-22 11:57:20 +0000136 // SDK version that the stubs library is built against. Note that this is always
137 // *current. Older stubs library built with a numbered SDK version is created from
138 // the prebuilt jar.
139 sdkVersion string
Paul Duffin1fb487d2020-04-07 18:50:10 +0100140
Paul Duffin15f34ef2020-07-20 18:04:44 +0100141 // The annotation that identifies this API level, empty for the public API scope.
142 annotation string
143
Paul Duffin1fb487d2020-04-07 18:50:10 +0100144 // Extra arguments to pass to droidstubs for this scope.
Paul Duffin0ff08bd2020-04-29 13:30:54 +0100145 //
Paul Duffin15f34ef2020-07-20 18:04:44 +0100146 // This is not used directly but is used to construct the droidstubsArgs.
147 extraArgs []string
Paul Duffin0ff08bd2020-04-29 13:30:54 +0100148
Paul Duffin15f34ef2020-07-20 18:04:44 +0100149 // The args that must be passed to droidstubs to generate the API and stubs source
150 // for this scope, constructed dynamically by initApiScope().
Paul Duffin0ff08bd2020-04-29 13:30:54 +0100151 //
152 // The API only includes the additional members that this scope adds over the scope
153 // that it extends.
Paul Duffin15f34ef2020-07-20 18:04:44 +0100154 //
155 // The stubs source must include the definitions of everything that is in this
156 // api scope and all the scopes that this one extends.
157 droidstubsArgs []string
Paul Duffin0ff08bd2020-04-29 13:30:54 +0100158
Anton Hansson6478ac12020-05-02 11:19:36 +0100159 // Whether the api scope can be treated as unstable, and should skip compat checks.
160 unstable bool
Jihoon Kang1c92c3e2023-03-23 17:44:51 +0000161
162 // Represents the SDK kind of this scope.
163 kind android.SdkKind
Paul Duffind1b3a922020-01-22 11:57:20 +0000164}
165
166// Initialize a scope, creating and adding appropriate dependency tags
167func initApiScope(scope *apiScope) *apiScope {
Paul Duffinc8782502020-04-29 20:45:27 +0100168 name := scope.name
Paul Duffin46dc45a2020-05-14 15:39:10 +0100169 scopeByName[name] = scope
170 allScopeNames = append(allScopeNames, name)
Paul Duffin6b836ba2020-05-13 19:19:49 +0100171 scope.propertyName = strings.ReplaceAll(name, "-", "_")
172 scope.fieldName = proptools.FieldNameForProperty(scope.propertyName)
Paul Duffind1b3a922020-01-22 11:57:20 +0000173 scope.stubsTag = scopeDependencyTag{
Paul Duffinc8782502020-04-29 20:45:27 +0100174 name: name + "-stubs",
175 apiScope: scope,
176 depInfoExtractor: (*scopePaths).extractStubsLibraryInfoFromDependency,
Paul Duffind1b3a922020-01-22 11:57:20 +0000177 }
Paul Duffin0ff08bd2020-04-29 13:30:54 +0100178 scope.stubsSourceTag = scopeDependencyTag{
179 name: name + "-stubs-source",
180 apiScope: scope,
181 depInfoExtractor: (*scopePaths).extractStubsSourceInfoFromDep,
182 }
183 scope.apiFileTag = scopeDependencyTag{
184 name: name + "-api",
185 apiScope: scope,
186 depInfoExtractor: (*scopePaths).extractApiInfoFromDep,
187 }
Paul Duffinc8782502020-04-29 20:45:27 +0100188 scope.stubsSourceAndApiTag = scopeDependencyTag{
189 name: name + "-stubs-source-and-api",
190 apiScope: scope,
191 depInfoExtractor: (*scopePaths).extractStubsSourceAndApiInfoFromApiStubsProvider,
Paul Duffind1b3a922020-01-22 11:57:20 +0000192 }
Paul Duffin958806b2022-05-16 13:10:47 +0000193 scope.latestApiModuleTag = scopeDependencyTag{
194 name: name + "-latest-api",
195 apiScope: scope,
196 depInfoExtractor: (*scopePaths).extractLatestApiPath,
197 }
198 scope.latestRemovedApiModuleTag = scopeDependencyTag{
199 name: name + "-latest-removed-api",
200 apiScope: scope,
201 depInfoExtractor: (*scopePaths).extractLatestRemovedApiPath,
202 }
Paul Duffin0ff08bd2020-04-29 13:30:54 +0100203
204 // To get the args needed to generate the stubs source append all the args from
205 // this scope and all the scopes it extends as each set of args adds additional
206 // members to the stubs.
Paul Duffin15f34ef2020-07-20 18:04:44 +0100207 var scopeSpecificArgs []string
208 if scope.annotation != "" {
209 scopeSpecificArgs = []string{"--show-annotation", scope.annotation}
Paul Duffin0ff08bd2020-04-29 13:30:54 +0100210 }
Paul Duffin15f34ef2020-07-20 18:04:44 +0100211 for s := scope; s != nil; s = s.extends {
212 scopeSpecificArgs = append(scopeSpecificArgs, s.extraArgs...)
Paul Duffin0ff08bd2020-04-29 13:30:54 +0100213
Paul Duffin15f34ef2020-07-20 18:04:44 +0100214 // Ensure that the generated stubs includes all the API elements from the API scope
215 // that this scope extends.
216 if s != scope && s.annotation != "" {
217 scopeSpecificArgs = append(scopeSpecificArgs, "--show-for-stub-purposes-annotation", s.annotation)
218 }
219 }
Paul Duffin0ff08bd2020-04-29 13:30:54 +0100220
Paul Duffind0b9fca2022-09-30 18:11:41 +0100221 // By default, a library that can access a scope can also access the scope it extends.
222 if scope.canAccess == nil {
223 scope.canAccess = scope.extends
224 }
225
Paul Duffin15f34ef2020-07-20 18:04:44 +0100226 // Escape any special characters in the arguments. This is needed because droidstubs
227 // passes these directly to the shell command.
228 scope.droidstubsArgs = proptools.ShellEscapeList(scopeSpecificArgs)
Paul Duffin0ff08bd2020-04-29 13:30:54 +0100229
Paul Duffind1b3a922020-01-22 11:57:20 +0000230 return scope
231}
232
Anton Hansson08f476b2021-04-07 15:32:19 +0100233func (scope *apiScope) stubsLibraryModuleNameSuffix() string {
234 return ".stubs" + scope.moduleSuffix
235}
236
Jihoon Kang1c92c3e2023-03-23 17:44:51 +0000237func (scope *apiScope) apiLibraryModuleName(baseName string) string {
238 return scope.stubsLibraryModuleName(baseName) + ".from-text"
239}
240
Jihoon Kang1147b312023-06-08 23:25:57 +0000241func (scope *apiScope) sourceStubLibraryModuleName(baseName string) string {
242 return scope.stubsLibraryModuleName(baseName) + ".from-source"
243}
244
Paul Duffinc3091c82020-05-08 14:16:20 +0100245func (scope *apiScope) stubsLibraryModuleName(baseName string) string {
Anton Hansson08f476b2021-04-07 15:32:19 +0100246 return baseName + scope.stubsLibraryModuleNameSuffix()
Paul Duffind1b3a922020-01-22 11:57:20 +0000247}
248
Paul Duffinc8782502020-04-29 20:45:27 +0100249func (scope *apiScope) stubsSourceModuleName(baseName string) string {
Paul Duffindd9d0742020-05-08 15:52:37 +0100250 return baseName + ".stubs.source" + scope.moduleSuffix
Paul Duffind1b3a922020-01-22 11:57:20 +0000251}
252
Paul Duffin0ff08bd2020-04-29 13:30:54 +0100253func (scope *apiScope) apiModuleName(baseName string) string {
Paul Duffindd9d0742020-05-08 15:52:37 +0100254 return baseName + ".api" + scope.moduleSuffix
Paul Duffin0ff08bd2020-04-29 13:30:54 +0100255}
256
Paul Duffin3375e352020-04-28 10:44:03 +0100257func (scope *apiScope) String() string {
258 return scope.name
259}
260
Paul Duffin958806b2022-05-16 13:10:47 +0000261// snapshotRelativeDir returns the snapshot directory into which the files related to scopes will
262// be stored.
263func (scope *apiScope) snapshotRelativeDir() string {
264 return filepath.Join("sdk_library", scope.name)
265}
266
267// snapshotRelativeCurrentApiTxtPath returns the snapshot path to the API .txt file for the named
268// library.
269func (scope *apiScope) snapshotRelativeCurrentApiTxtPath(name string) string {
270 return filepath.Join(scope.snapshotRelativeDir(), name+".txt")
271}
272
273// snapshotRelativeRemovedApiTxtPath returns the snapshot path to the removed API .txt file for the
274// named library.
275func (scope *apiScope) snapshotRelativeRemovedApiTxtPath(name string) string {
276 return filepath.Join(scope.snapshotRelativeDir(), name+"-removed.txt")
277}
278
Paul Duffind1b3a922020-01-22 11:57:20 +0000279type apiScopes []*apiScope
280
281func (scopes apiScopes) Strings(accessor func(*apiScope) string) []string {
282 var list []string
283 for _, scope := range scopes {
284 list = append(list, accessor(scope))
285 }
286 return list
287}
288
Jihoon Kanga96a7b12023-09-20 23:43:32 +0000289// Method that maps the apiScopes properties to the index of each apiScopes elements.
290// apiScopes property to be used as the key can be specified with the input accessor.
291// Only a string property of apiScope can be used as the key of the map.
292func (scopes apiScopes) MapToIndex(accessor func(*apiScope) string) map[string]int {
293 ret := make(map[string]int)
294 for i, scope := range scopes {
295 ret[accessor(scope)] = i
296 }
297 return ret
298}
299
Jiyong Parkc678ad32018-04-10 13:07:10 +0900300var (
Paul Duffin46dc45a2020-05-14 15:39:10 +0100301 scopeByName = make(map[string]*apiScope)
302 allScopeNames []string
Paul Duffind1b3a922020-01-22 11:57:20 +0000303 apiScopePublic = initApiScope(&apiScope{
Paul Duffin3375e352020-04-28 10:44:03 +0100304 name: "public",
305
306 // Public scope is enabled by default for both legacy and non-legacy modes.
307 legacyEnabledStatus: func(module *SdkLibrary) bool {
308 return true
309 },
310 defaultEnabledStatus: true,
311
312 scopeSpecificProperties: func(module *SdkLibrary) *ApiScopeProperties {
313 return &module.sdkLibraryProperties.Public
314 },
Paul Duffind1b3a922020-01-22 11:57:20 +0000315 sdkVersion: "current",
Jihoon Kang1c92c3e2023-03-23 17:44:51 +0000316 kind: android.SdkPublic,
Paul Duffind1b3a922020-01-22 11:57:20 +0000317 })
318 apiScopeSystem = initApiScope(&apiScope{
Paul Duffin3375e352020-04-28 10:44:03 +0100319 name: "system",
320 extends: apiScopePublic,
321 legacyEnabledStatus: (*SdkLibrary).generateTestAndSystemScopesByDefault,
322 scopeSpecificProperties: func(module *SdkLibrary) *ApiScopeProperties {
323 return &module.sdkLibraryProperties.System
324 },
Paul Duffin15f34ef2020-07-20 18:04:44 +0100325 apiFilePrefix: "system-",
326 moduleSuffix: ".system",
327 sdkVersion: "system_current",
328 annotation: "android.annotation.SystemApi(client=android.annotation.SystemApi.Client.PRIVILEGED_APPS)",
Jihoon Kang1c92c3e2023-03-23 17:44:51 +0000329 kind: android.SdkSystem,
Paul Duffind1b3a922020-01-22 11:57:20 +0000330 })
331 apiScopeTest = initApiScope(&apiScope{
Paul Duffin3375e352020-04-28 10:44:03 +0100332 name: "test",
Anton Hansson4fe970f2020-10-09 10:16:49 +0100333 extends: apiScopeSystem,
Paul Duffin3375e352020-04-28 10:44:03 +0100334 legacyEnabledStatus: (*SdkLibrary).generateTestAndSystemScopesByDefault,
335 scopeSpecificProperties: func(module *SdkLibrary) *ApiScopeProperties {
336 return &module.sdkLibraryProperties.Test
337 },
Paul Duffin15f34ef2020-07-20 18:04:44 +0100338 apiFilePrefix: "test-",
339 moduleSuffix: ".test",
340 sdkVersion: "test_current",
341 annotation: "android.annotation.TestApi",
342 unstable: true,
Jihoon Kang1c92c3e2023-03-23 17:44:51 +0000343 kind: android.SdkTest,
Paul Duffind1b3a922020-01-22 11:57:20 +0000344 })
Paul Duffin8f265b92020-04-28 14:13:56 +0100345 apiScopeModuleLib = initApiScope(&apiScope{
Paul Duffin6b836ba2020-05-13 19:19:49 +0100346 name: "module-lib",
Paul Duffin8f265b92020-04-28 14:13:56 +0100347 extends: apiScopeSystem,
Paul Duffin0c5bae52020-06-02 13:00:08 +0100348 // The module-lib scope is disabled by default in legacy mode.
Paul Duffin8f265b92020-04-28 14:13:56 +0100349 //
350 // Enabling this would break existing usages.
351 legacyEnabledStatus: func(module *SdkLibrary) bool {
352 return false
353 },
354 scopeSpecificProperties: func(module *SdkLibrary) *ApiScopeProperties {
355 return &module.sdkLibraryProperties.Module_lib
356 },
357 apiFilePrefix: "module-lib-",
358 moduleSuffix: ".module_lib",
359 sdkVersion: "module_current",
Paul Duffin15f34ef2020-07-20 18:04:44 +0100360 annotation: "android.annotation.SystemApi(client=android.annotation.SystemApi.Client.MODULE_LIBRARIES)",
Jihoon Kang1c92c3e2023-03-23 17:44:51 +0000361 kind: android.SdkModule,
Paul Duffin8f265b92020-04-28 14:13:56 +0100362 })
Paul Duffin0c5bae52020-06-02 13:00:08 +0100363 apiScopeSystemServer = initApiScope(&apiScope{
364 name: "system-server",
365 extends: apiScopePublic,
Paul Duffind0b9fca2022-09-30 18:11:41 +0100366
367 // The system-server scope can access the module-lib scope.
368 //
369 // A module that provides a system-server API is appended to the standard bootclasspath that is
370 // used by the system server. So, it should be able to access module-lib APIs provided by
371 // libraries on the bootclasspath.
372 canAccess: apiScopeModuleLib,
373
Paul Duffin0c5bae52020-06-02 13:00:08 +0100374 // The system-server scope is disabled by default in legacy mode.
375 //
376 // Enabling this would break existing usages.
377 legacyEnabledStatus: func(module *SdkLibrary) bool {
378 return false
379 },
380 scopeSpecificProperties: func(module *SdkLibrary) *ApiScopeProperties {
381 return &module.sdkLibraryProperties.System_server
382 },
383 apiFilePrefix: "system-server-",
384 moduleSuffix: ".system_server",
385 sdkVersion: "system_server_current",
Paul Duffin15f34ef2020-07-20 18:04:44 +0100386 annotation: "android.annotation.SystemApi(client=android.annotation.SystemApi.Client.SYSTEM_SERVER)",
387 extraArgs: []string{
388 "--hide-annotation", "android.annotation.Hide",
Paul Duffin0c5bae52020-06-02 13:00:08 +0100389 // com.android.* classes are okay in this interface"
Paul Duffin15f34ef2020-07-20 18:04:44 +0100390 "--hide", "InternalClasses",
Paul Duffin0c5bae52020-06-02 13:00:08 +0100391 },
Jihoon Kang1c92c3e2023-03-23 17:44:51 +0000392 kind: android.SdkSystemServer,
Paul Duffin0c5bae52020-06-02 13:00:08 +0100393 })
Paul Duffind1b3a922020-01-22 11:57:20 +0000394 allApiScopes = apiScopes{
395 apiScopePublic,
396 apiScopeSystem,
397 apiScopeTest,
Paul Duffin8f265b92020-04-28 14:13:56 +0100398 apiScopeModuleLib,
Paul Duffin0c5bae52020-06-02 13:00:08 +0100399 apiScopeSystemServer,
Paul Duffind1b3a922020-01-22 11:57:20 +0000400 }
Jihoon Kang0c705a42023-08-02 06:44:57 +0000401 apiLibraryAdditionalProperties = map[string]struct {
402 FullApiSurfaceStubLib string
403 AdditionalApiContribution string
404 }{
405 "legacy.i18n.module.platform.api": {
406 FullApiSurfaceStubLib: "legacy.core.platform.api.stubs",
407 AdditionalApiContribution: "i18n.module.public.api.stubs.source.api.contribution",
408 },
409 "stable.i18n.module.platform.api": {
410 FullApiSurfaceStubLib: "stable.core.platform.api.stubs",
411 AdditionalApiContribution: "i18n.module.public.api.stubs.source.api.contribution",
412 },
413 "conscrypt.module.platform.api": {
414 FullApiSurfaceStubLib: "stable.core.platform.api.stubs",
415 AdditionalApiContribution: "conscrypt.module.public.api.stubs.source.api.contribution",
416 },
417 }
Jiyong Parkc678ad32018-04-10 13:07:10 +0900418)
419
Jiyong Park82484c02018-04-23 21:41:26 +0900420var (
421 javaSdkLibrariesLock sync.Mutex
422)
423
Jiyong Parkc678ad32018-04-10 13:07:10 +0900424// TODO: these are big features that are currently missing
Jiyong Park1be96912018-05-28 18:02:19 +0900425// 1) disallowing linking to the runtime shared lib
426// 2) HTML generation
Jiyong Parkc678ad32018-04-10 13:07:10 +0900427
428func init() {
Paul Duffin43dc1cc2019-12-19 11:18:54 +0000429 RegisterSdkLibraryBuildComponents(android.InitRegistrationContext)
Jiyong Parkc678ad32018-04-10 13:07:10 +0900430
Jiyong Park82484c02018-04-23 21:41:26 +0900431 android.RegisterMakeVarsProvider(pctx, func(ctx android.MakeVarsContext) {
432 javaSdkLibraries := javaSdkLibraries(ctx.Config())
433 sort.Strings(*javaSdkLibraries)
434 ctx.Strict("JAVA_SDK_LIBRARIES", strings.Join(*javaSdkLibraries, " "))
435 })
Paul Duffindd46f712020-02-10 13:37:10 +0000436
437 // Register sdk member types.
Paul Duffin976b0e52021-04-27 23:20:26 +0100438 android.RegisterSdkMemberType(javaSdkLibrarySdkMemberType)
Jiyong Parkc678ad32018-04-10 13:07:10 +0900439}
440
Paul Duffin43dc1cc2019-12-19 11:18:54 +0000441func RegisterSdkLibraryBuildComponents(ctx android.RegistrationContext) {
442 ctx.RegisterModuleType("java_sdk_library", SdkLibraryFactory)
443 ctx.RegisterModuleType("java_sdk_library_import", sdkLibraryImportFactory)
444}
445
Paul Duffin3375e352020-04-28 10:44:03 +0100446// Properties associated with each api scope.
447type ApiScopeProperties struct {
448 // Indicates whether the api surface is generated.
449 //
450 // If this is set for any scope then all scopes must explicitly specify if they
451 // are enabled. This is to prevent new usages from depending on legacy behavior.
452 //
453 // Otherwise, if this is not set for any scope then the default behavior is
454 // scope specific so please refer to the scope specific property documentation.
455 Enabled *bool
Paul Duffin87a05a32020-05-12 11:50:28 +0100456
457 // The sdk_version to use for building the stubs.
458 //
459 // If not specified then it will use an sdk_version determined as follows:
Trevor Radcliffedf8aa1f2021-11-04 14:25:39 +0000460 //
Paul Duffin87a05a32020-05-12 11:50:28 +0100461 // 1) If the sdk_version specified on the java_sdk_library is none then this
Trevor Radcliffedf8aa1f2021-11-04 14:25:39 +0000462 // will be none. This is used for java_sdk_library instances that are used
463 // to create stubs that contribute to the core_current sdk version.
464 // 2) Otherwise, it is assumed that this library extends but does not
465 // contribute directly to a specific sdk_version and so this uses the
466 // sdk_version appropriate for the api scope. e.g. public will use
467 // sdk_version: current, system will use sdk_version: system_current, etc.
Paul Duffin87a05a32020-05-12 11:50:28 +0100468 //
469 // This does not affect the sdk_version used for either generating the stubs source
470 // or the API file. They both have to use the same sdk_version as is used for
471 // compiling the implementation library.
472 Sdk_version *string
Mark White9421c4c2023-08-10 00:07:03 +0000473
474 // Extra libs used when compiling stubs for this scope.
475 Libs []string
Paul Duffin3375e352020-04-28 10:44:03 +0100476}
477
Jiyong Parkc678ad32018-04-10 13:07:10 +0900478type sdkLibraryProperties struct {
Anton Hanssonf8ea3722021-09-16 14:24:13 +0100479 // List of source files that are needed to compile the API, but are not part of runtime library.
480 Api_srcs []string `android:"arch_variant"`
481
Paul Duffin5df79302020-05-16 15:52:12 +0100482 // Visibility for impl library module. If not specified then defaults to the
483 // visibility property.
484 Impl_library_visibility []string
485
Paul Duffin4911a892020-04-29 23:35:13 +0100486 // Visibility for stubs library modules. If not specified then defaults to the
487 // visibility property.
488 Stubs_library_visibility []string
489
490 // Visibility for stubs source modules. If not specified then defaults to the
491 // visibility property.
492 Stubs_source_visibility []string
493
Anton Hansson7f66efa2020-10-08 14:47:23 +0100494 // List of Java libraries that will be in the classpath when building the implementation lib
495 Impl_only_libs []string `android:"arch_variant"`
496
Paul Duffin77590a82022-04-28 14:13:30 +0000497 // List of Java libraries that will included in the implementation lib.
498 Impl_only_static_libs []string `android:"arch_variant"`
499
Sundong Ahnf043cf62018-06-25 16:04:37 +0900500 // List of Java libraries that will be in the classpath when building stubs
501 Stub_only_libs []string `android:"arch_variant"`
502
Anton Hanssondae54cd2021-04-21 16:30:10 +0100503 // List of Java libraries that will included in stub libraries
504 Stub_only_static_libs []string `android:"arch_variant"`
505
Paul Duffin7a586d32019-12-30 17:09:34 +0000506 // list of package names that will be documented and publicized as API.
507 // This allows the API to be restricted to a subset of the source files provided.
508 // If this is unspecified then all the source files will be treated as being part
509 // of the API.
Jiyong Parkc678ad32018-04-10 13:07:10 +0900510 Api_packages []string
511
Jiyong Park5a2c9d72018-05-01 22:25:41 +0900512 // list of package names that must be hidden from the API
513 Hidden_api_packages []string
514
Paul Duffin749f98f2019-12-30 17:23:46 +0000515 // the relative path to the directory containing the api specification files.
516 // Defaults to "api".
517 Api_dir *string
518
Paul Duffindfa131e2020-05-15 20:37:11 +0100519 // Determines whether a runtime implementation library is built; defaults to false.
520 //
521 // 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 +0200522 // it is as if shared_library: false, was set.
Paul Duffin43db9be2019-12-30 17:35:49 +0000523 Api_only *bool
524
Paul Duffin11512472019-02-11 15:55:17 +0000525 // local files that are used within user customized droiddoc options.
526 Droiddoc_option_files []string
527
Spandan Das93e95992021-07-29 18:26:39 +0000528 // additional droiddoc options.
Paul Duffin11512472019-02-11 15:55:17 +0000529 // Available variables for substitution:
530 //
531 // $(location <label>): the path to the droiddoc_option_files with name <label>
Sundong Ahndd567f92018-07-31 17:19:11 +0900532 Droiddoc_options []string
533
Paul Duffine22c2ab2020-05-20 19:35:27 +0100534 // is set to true, Metalava will allow framework SDK to contain annotations.
535 Annotations_enabled *bool
536
Sundong Ahn054b19a2018-10-19 13:46:09 +0900537 // a list of top-level directories containing files to merge qualifier annotations
538 // (i.e. those intended to be included in the stubs written) from.
539 Merge_annotations_dirs []string
540
541 // a list of top-level directories containing Java stub files to merge show/hide annotations from.
542 Merge_inclusion_annotations_dirs []string
543
Paul Duffin4f5c1ef2020-11-19 14:53:43 +0000544 // If set to true then don't create dist rules.
545 No_dist *bool
Sundong Ahn80a87b32019-05-13 15:02:50 +0900546
Paul Duffin31310252020-11-20 21:26:20 +0000547 // The stem for the artifacts that are copied to the dist, if not specified
548 // then defaults to the base module name.
549 //
550 // For each scope the following artifacts are copied to the apistubs/<scope>
551 // directory in the dist.
552 // * stubs impl jar -> <dist-stem>.jar
553 // * API specification file -> api/<dist-stem>.txt
554 // * Removed API specification file -> api/<dist-stem>-removed.txt
555 //
556 // Also used to construct the name of the filegroup (created by prebuilt_apis)
557 // that references the latest released API and remove API specification files.
558 // * API specification filegroup -> <dist-stem>.api.<scope>.latest
559 // * Removed API specification filegroup -> <dist-stem>-removed.api.<scope>.latest
Jaewoong Jung1a97ee02021-03-09 13:25:02 -0800560 // * API incompatibilities baseline filegroup -> <dist-stem>-incompatibilities.api.<scope>.latest
Paul Duffin31310252020-11-20 21:26:20 +0000561 Dist_stem *string
562
Colin Cross986b69a2021-06-01 13:13:40 -0700563 // The subdirectory for the artifacts that are copied to the dist directory. If not specified
Colin Cross3dd66252021-06-01 14:05:09 -0700564 // then defaults to "unknown". Should be set to "android" for anything that should be published
Colin Cross986b69a2021-06-01 13:13:40 -0700565 // in the public Android SDK.
566 Dist_group *string
567
Anton Hanssondff2c782020-12-21 17:10:01 +0000568 // A compatibility mode that allows historical API-tracking files to not exist.
569 // Do not use.
570 Unsafe_ignore_missing_latest_api bool
571
Paul Duffin3375e352020-04-28 10:44:03 +0100572 // indicates whether system and test apis should be generated.
573 Generate_system_and_test_apis bool `blueprint:"mutated"`
574
575 // The properties specific to the public api scope
576 //
577 // Unless explicitly specified by using public.enabled the public api scope is
578 // enabled by default in both legacy and non-legacy mode.
579 Public ApiScopeProperties
580
581 // The properties specific to the system api scope
582 //
583 // In legacy mode the system api scope is enabled by default when sdk_version
584 // is set to something other than "none".
585 //
586 // In non-legacy mode the system api scope is disabled by default.
587 System ApiScopeProperties
588
589 // The properties specific to the test api scope
590 //
591 // In legacy mode the test api scope is enabled by default when sdk_version
592 // is set to something other than "none".
593 //
594 // In non-legacy mode the test api scope is disabled by default.
595 Test ApiScopeProperties
Paul Duffin37e0b772019-12-30 17:20:10 +0000596
Paul Duffin0c5bae52020-06-02 13:00:08 +0100597 // The properties specific to the module-lib api scope
Paul Duffin8f265b92020-04-28 14:13:56 +0100598 //
Zi Wangb2179e32023-01-31 15:53:30 -0800599 // Unless explicitly specified by using module_lib.enabled the module_lib api
600 // scope is disabled by default.
Paul Duffin8f265b92020-04-28 14:13:56 +0100601 Module_lib ApiScopeProperties
602
Paul Duffin0c5bae52020-06-02 13:00:08 +0100603 // The properties specific to the system-server api scope
604 //
Zi Wangb2179e32023-01-31 15:53:30 -0800605 // Unless explicitly specified by using system_server.enabled the
606 // system_server api scope is disabled by default.
Paul Duffin0c5bae52020-06-02 13:00:08 +0100607 System_server ApiScopeProperties
608
Jiyong Park932cdfe2020-05-28 00:19:53 +0900609 // Determines if the stubs are preferred over the implementation library
610 // for linking, even when the client doesn't specify sdk_version. When this
611 // is set to true, such clients are provided with the widest API surface that
612 // this lib provides. Note however that this option doesn't affect the clients
613 // that are in the same APEX as this library. In that case, the clients are
614 // always linked with the implementation library. Default is false.
615 Default_to_stubs *bool
616
Paul Duffin160fe412020-05-10 19:32:20 +0100617 // Properties related to api linting.
618 Api_lint struct {
619 // Enable api linting.
620 Enabled *bool
621 }
622
Jiyong Parkc678ad32018-04-10 13:07:10 +0900623 // TODO: determines whether to create HTML doc or not
Paul Duffine8409952022-09-22 16:24:46 +0100624 // Html_doc *bool
Jiyong Parkc678ad32018-04-10 13:07:10 +0900625}
626
Paul Duffin0f8faff2020-05-20 16:18:00 +0100627// Paths to outputs from java_sdk_library and java_sdk_library_import.
628//
629// Fields that are android.Paths are always set (during GenerateAndroidBuildActions).
630// OptionalPaths are always set by java_sdk_library but may not be set by
631// java_sdk_library_import as not all instances provide that information.
Paul Duffind1b3a922020-01-22 11:57:20 +0000632type scopePaths struct {
Paul Duffin0f8faff2020-05-20 16:18:00 +0100633 // The path (represented as Paths for convenience when returning) to the stubs header jar.
634 //
635 // That is the jar that is created by turbine.
636 stubsHeaderPath android.Paths
637
638 // The path (represented as Paths for convenience when returning) to the stubs implementation jar.
639 //
640 // This is not the implementation jar, it still only contains stubs.
641 stubsImplPath android.Paths
642
Paul Duffin1267d872021-04-16 17:21:36 +0100643 // The dex jar for the stubs.
644 //
645 // This is not the implementation jar, it still only contains stubs.
Martin Stjernholm8be1e6d2021-09-15 03:34:04 +0100646 stubsDexJarPath OptionalDexJarPath
Paul Duffin1267d872021-04-16 17:21:36 +0100647
Paul Duffin0f8faff2020-05-20 16:18:00 +0100648 // The API specification file, e.g. system_current.txt.
649 currentApiFilePath android.OptionalPath
650
651 // The specification of API elements removed since the last release.
652 removedApiFilePath android.OptionalPath
653
654 // The stubs source jar.
655 stubsSrcJar android.OptionalPath
Anton Hanssond78eb762021-09-21 15:25:12 +0100656
657 // Extracted annotations.
658 annotationsZip android.OptionalPath
Paul Duffin958806b2022-05-16 13:10:47 +0000659
660 // The path to the latest API file.
661 latestApiPath android.OptionalPath
662
663 // The path to the latest removed API file.
664 latestRemovedApiPath android.OptionalPath
Paul Duffind1b3a922020-01-22 11:57:20 +0000665}
666
Colin Crossdcf71b22021-02-01 13:59:03 -0800667func (paths *scopePaths) extractStubsLibraryInfoFromDependency(ctx android.ModuleContext, dep android.Module) error {
668 if ctx.OtherModuleHasProvider(dep, JavaInfoProvider) {
669 lib := ctx.OtherModuleProvider(dep, JavaInfoProvider).(JavaInfo)
670 paths.stubsHeaderPath = lib.HeaderJars
671 paths.stubsImplPath = lib.ImplementationJars
Paul Duffin1267d872021-04-16 17:21:36 +0100672
673 libDep := dep.(UsesLibraryDependency)
674 paths.stubsDexJarPath = libDep.DexJarBuildPath()
Paul Duffinc8782502020-04-29 20:45:27 +0100675 return nil
676 } else {
Colin Crossdcf71b22021-02-01 13:59:03 -0800677 return fmt.Errorf("expected module that has JavaInfoProvider, e.g. java_library")
Paul Duffinc8782502020-04-29 20:45:27 +0100678 }
679}
680
Paul Duffin0ff08bd2020-04-29 13:30:54 +0100681func (paths *scopePaths) treatDepAsApiStubsProvider(dep android.Module, action func(provider ApiStubsProvider)) error {
682 if apiStubsProvider, ok := dep.(ApiStubsProvider); ok {
683 action(apiStubsProvider)
Paul Duffinc8782502020-04-29 20:45:27 +0100684 return nil
685 } else {
686 return fmt.Errorf("expected module that implements ApiStubsProvider, e.g. droidstubs")
687 }
688}
689
Paul Duffin0f8faff2020-05-20 16:18:00 +0100690func (paths *scopePaths) treatDepAsApiStubsSrcProvider(dep android.Module, action func(provider ApiStubsSrcProvider)) error {
691 if apiStubsProvider, ok := dep.(ApiStubsSrcProvider); ok {
692 action(apiStubsProvider)
693 return nil
694 } else {
695 return fmt.Errorf("expected module that implements ApiStubsSrcProvider, e.g. droidstubs")
696 }
697}
698
Paul Duffin0ff08bd2020-04-29 13:30:54 +0100699func (paths *scopePaths) extractApiInfoFromApiStubsProvider(provider ApiStubsProvider) {
Anton Hanssond78eb762021-09-21 15:25:12 +0100700 paths.annotationsZip = android.OptionalPathForPath(provider.AnnotationsZip())
Paul Duffin0f8faff2020-05-20 16:18:00 +0100701 paths.currentApiFilePath = android.OptionalPathForPath(provider.ApiFilePath())
702 paths.removedApiFilePath = android.OptionalPathForPath(provider.RemovedApiFilePath())
Paul Duffin0ff08bd2020-04-29 13:30:54 +0100703}
704
Colin Crossdcf71b22021-02-01 13:59:03 -0800705func (paths *scopePaths) extractApiInfoFromDep(ctx android.ModuleContext, dep android.Module) error {
Paul Duffin0ff08bd2020-04-29 13:30:54 +0100706 return paths.treatDepAsApiStubsProvider(dep, func(provider ApiStubsProvider) {
707 paths.extractApiInfoFromApiStubsProvider(provider)
708 })
709}
710
Paul Duffin0f8faff2020-05-20 16:18:00 +0100711func (paths *scopePaths) extractStubsSourceInfoFromApiStubsProviders(provider ApiStubsSrcProvider) {
712 paths.stubsSrcJar = android.OptionalPathForPath(provider.StubsSrcJar())
Paul Duffin0ff08bd2020-04-29 13:30:54 +0100713}
714
Colin Crossdcf71b22021-02-01 13:59:03 -0800715func (paths *scopePaths) extractStubsSourceInfoFromDep(ctx android.ModuleContext, dep android.Module) error {
Paul Duffin0f8faff2020-05-20 16:18:00 +0100716 return paths.treatDepAsApiStubsSrcProvider(dep, func(provider ApiStubsSrcProvider) {
Paul Duffin0ff08bd2020-04-29 13:30:54 +0100717 paths.extractStubsSourceInfoFromApiStubsProviders(provider)
718 })
719}
720
Colin Crossdcf71b22021-02-01 13:59:03 -0800721func (paths *scopePaths) extractStubsSourceAndApiInfoFromApiStubsProvider(ctx android.ModuleContext, dep android.Module) error {
Paul Duffin0ff08bd2020-04-29 13:30:54 +0100722 return paths.treatDepAsApiStubsProvider(dep, func(provider ApiStubsProvider) {
723 paths.extractApiInfoFromApiStubsProvider(provider)
724 paths.extractStubsSourceInfoFromApiStubsProviders(provider)
725 })
726}
727
Paul Duffin958806b2022-05-16 13:10:47 +0000728func extractSingleOptionalOutputPath(dep android.Module) (android.OptionalPath, error) {
729 var paths android.Paths
730 if sourceFileProducer, ok := dep.(android.SourceFileProducer); ok {
731 paths = sourceFileProducer.Srcs()
732 } else {
733 return android.OptionalPath{}, fmt.Errorf("module %q does not produce source files", dep)
734 }
735 if len(paths) != 1 {
736 return android.OptionalPath{}, fmt.Errorf("expected one path from %q, got %q", dep, paths)
737 }
738 return android.OptionalPathForPath(paths[0]), nil
739}
740
741func (paths *scopePaths) extractLatestApiPath(ctx android.ModuleContext, dep android.Module) error {
742 outputPath, err := extractSingleOptionalOutputPath(dep)
743 paths.latestApiPath = outputPath
744 return err
745}
746
747func (paths *scopePaths) extractLatestRemovedApiPath(ctx android.ModuleContext, dep android.Module) error {
748 outputPath, err := extractSingleOptionalOutputPath(dep)
749 paths.latestRemovedApiPath = outputPath
750 return err
751}
752
Paul Duffin0ff08bd2020-04-29 13:30:54 +0100753type commonToSdkLibraryAndImportProperties struct {
Paul Duffin1b1e8062020-05-08 13:44:43 +0100754 // The naming scheme to use for the components that this module creates.
755 //
Paul Duffinee9ad5d2020-09-11 13:04:05 +0100756 // If not specified then it defaults to "default".
Paul Duffin1b1e8062020-05-08 13:44:43 +0100757 //
758 // This is a temporary mechanism to simplify conversion from separate modules for each
759 // component that follow a different naming pattern to the default one.
760 //
761 // TODO(b/155480189) - Remove once naming inconsistencies have been resolved.
Paul Duffin0ff08bd2020-04-29 13:30:54 +0100762 Naming_scheme *string
Paul Duffindfa131e2020-05-15 20:37:11 +0100763
764 // Specifies whether this module can be used as an Android shared library; defaults
765 // to true.
766 //
767 // An Android shared library is one that can be referenced in a <uses-library> element
768 // in an AndroidManifest.xml.
769 Shared_library *bool
Paul Duffina2ae7e02020-09-11 11:55:00 +0100770
771 // Files containing information about supported java doc tags.
772 Doctag_files []string `android:"path"`
Pedro Loureiro9956e5e2021-09-07 17:21:59 +0000773
774 // Signals that this shared library is part of the bootclasspath starting
775 // on the version indicated in this attribute.
776 //
777 // This will make platforms at this level and above to ignore
778 // <uses-library> tags with this library name because the library is already
779 // available
780 On_bootclasspath_since *string
781
782 // Signals that this shared library was part of the bootclasspath before
783 // (but not including) the version indicated in this attribute.
784 //
785 // The system will automatically add a <uses-library> tag with this library to
786 // apps that target any SDK less than the version indicated in this attribute.
787 On_bootclasspath_before *string
788
789 // Indicates that PackageManager should ignore this shared library if the
790 // platform is below the version indicated in this attribute.
791 //
792 // This means that the device won't recognise this library as installed.
793 Min_device_sdk *string
794
795 // Indicates that PackageManager should ignore this shared library if the
796 // platform is above the version indicated in this attribute.
797 //
798 // This means that the device won't recognise this library as installed.
799 Max_device_sdk *string
Paul Duffin0ff08bd2020-04-29 13:30:54 +0100800}
801
Paul Duffin71b33cc2021-06-23 11:39:47 +0100802// commonSdkLibraryAndImportModule defines the interface that must be provided by a module that
803// embeds the commonToSdkLibraryAndImport struct.
804type commonSdkLibraryAndImportModule interface {
Paul Duffind796f6f2022-11-23 23:06:05 +0000805 android.Module
Paul Duffin71b33cc2021-06-23 11:39:47 +0100806
807 BaseModuleName() string
808}
809
Paul Duffin56d44902020-01-31 13:36:25 +0000810// Common code between sdk library and sdk library import
811type commonToSdkLibraryAndImport struct {
Paul Duffin71b33cc2021-06-23 11:39:47 +0100812 module commonSdkLibraryAndImportModule
Paul Duffinc3091c82020-05-08 14:16:20 +0100813
Paul Duffin56d44902020-01-31 13:36:25 +0000814 scopePaths map[*apiScope]*scopePaths
Paul Duffin1b1e8062020-05-08 13:44:43 +0100815
816 namingScheme sdkLibraryComponentNamingScheme
817
Paul Duffindfa131e2020-05-15 20:37:11 +0100818 commonSdkLibraryProperties commonToSdkLibraryAndImportProperties
Paul Duffin859fe962020-05-15 10:20:31 +0100819
Paul Duffina2ae7e02020-09-11 11:55:00 +0100820 // Paths to commonSdkLibraryProperties.Doctag_files
821 doctagPaths android.Paths
822
Paul Duffin859fe962020-05-15 10:20:31 +0100823 // Functionality related to this being used as a component of a java_sdk_library.
824 EmbeddableSdkLibraryComponent
Paul Duffin56d44902020-01-31 13:36:25 +0000825}
826
Paul Duffin71b33cc2021-06-23 11:39:47 +0100827func (c *commonToSdkLibraryAndImport) initCommon(module commonSdkLibraryAndImportModule) {
828 c.module = module
Paul Duffin1b1e8062020-05-08 13:44:43 +0100829
Paul Duffin71b33cc2021-06-23 11:39:47 +0100830 module.AddProperties(&c.commonSdkLibraryProperties)
Paul Duffin859fe962020-05-15 10:20:31 +0100831
832 // Initialize this as an sdk library component.
Paul Duffin71b33cc2021-06-23 11:39:47 +0100833 c.initSdkLibraryComponent(module)
Paul Duffin1b1e8062020-05-08 13:44:43 +0100834}
835
836func (c *commonToSdkLibraryAndImport) initCommonAfterDefaultsApplied(ctx android.DefaultableHookContext) bool {
Paul Duffindfa131e2020-05-15 20:37:11 +0100837 schemeProperty := proptools.StringDefault(c.commonSdkLibraryProperties.Naming_scheme, "default")
Paul Duffin1b1e8062020-05-08 13:44:43 +0100838 switch schemeProperty {
839 case "default":
840 c.namingScheme = &defaultNamingScheme{}
841 default:
842 ctx.PropertyErrorf("naming_scheme", "expected 'default' but was %q", schemeProperty)
843 return false
844 }
845
Paul Duffin3f0290e2021-06-30 18:25:36 +0100846 namePtr := proptools.StringPtr(c.module.BaseModuleName())
847 c.sdkLibraryComponentProperties.SdkLibraryName = namePtr
848
Paul Duffindfa131e2020-05-15 20:37:11 +0100849 // Only track this sdk library if this can be used as a shared library.
850 if c.sharedLibrary() {
851 // Use the name specified in the module definition as the owner.
Paul Duffin3f0290e2021-06-30 18:25:36 +0100852 c.sdkLibraryComponentProperties.SdkLibraryToImplicitlyTrack = namePtr
Paul Duffindfa131e2020-05-15 20:37:11 +0100853 }
Paul Duffin859fe962020-05-15 10:20:31 +0100854
Paul Duffin1b1e8062020-05-08 13:44:43 +0100855 return true
Paul Duffinc3091c82020-05-08 14:16:20 +0100856}
857
Paul Duffinea8f8082021-06-24 13:25:57 +0100858// uniqueApexVariations provides common implementation of the ApexModule.UniqueApexVariations
859// method.
860func (c *commonToSdkLibraryAndImport) uniqueApexVariations() bool {
861 // A java_sdk_library that is a shared library produces an XML file that makes the shared library
862 // usable from an AndroidManifest.xml's <uses-library> entry. That XML file contains the name of
863 // the APEX and so it needs a unique variation per APEX.
864 return c.sharedLibrary()
865}
866
Paul Duffina2ae7e02020-09-11 11:55:00 +0100867func (c *commonToSdkLibraryAndImport) generateCommonBuildActions(ctx android.ModuleContext) {
868 c.doctagPaths = android.PathsForModuleSrc(ctx, c.commonSdkLibraryProperties.Doctag_files)
869}
870
Paul Duffineedc5d52020-06-12 17:46:39 +0100871// Module name of the runtime implementation library
872func (c *commonToSdkLibraryAndImport) implLibraryModuleName() string {
Paul Duffin71b33cc2021-06-23 11:39:47 +0100873 return c.module.BaseModuleName() + ".impl"
Paul Duffineedc5d52020-06-12 17:46:39 +0100874}
875
876// Module name of the XML file for the lib
877func (c *commonToSdkLibraryAndImport) xmlPermissionsModuleName() string {
Paul Duffin71b33cc2021-06-23 11:39:47 +0100878 return c.module.BaseModuleName() + sdkXmlFileSuffix
Paul Duffineedc5d52020-06-12 17:46:39 +0100879}
880
Paul Duffinc3091c82020-05-08 14:16:20 +0100881// Name of the java_library module that compiles the stubs source.
882func (c *commonToSdkLibraryAndImport) stubsLibraryModuleName(apiScope *apiScope) string {
Paul Duffinb97b1572021-04-29 21:50:40 +0100883 baseName := c.module.BaseModuleName()
Paul Duffin21787622022-11-25 12:48:20 +0000884 return c.namingScheme.stubsLibraryModuleName(apiScope, baseName)
Paul Duffinc3091c82020-05-08 14:16:20 +0100885}
886
887// Name of the droidstubs module that generates the stubs source and may also
888// generate/check the API.
889func (c *commonToSdkLibraryAndImport) stubsSourceModuleName(apiScope *apiScope) string {
Paul Duffinb97b1572021-04-29 21:50:40 +0100890 baseName := c.module.BaseModuleName()
Paul Duffin21787622022-11-25 12:48:20 +0000891 return c.namingScheme.stubsSourceModuleName(apiScope, baseName)
Paul Duffinc3091c82020-05-08 14:16:20 +0100892}
893
Jihoon Kang1c92c3e2023-03-23 17:44:51 +0000894// Name of the java_api_library module that generates the from-text stubs source
895// and compiles to a jar file.
896func (c *commonToSdkLibraryAndImport) apiLibraryModuleName(apiScope *apiScope) string {
897 baseName := c.module.BaseModuleName()
898 return c.namingScheme.apiLibraryModuleName(apiScope, baseName)
899}
900
Jihoon Kang1147b312023-06-08 23:25:57 +0000901// Name of the java_library module that compiles the stubs
902// generated from source Java files.
903func (c *commonToSdkLibraryAndImport) sourceStubLibraryModuleName(apiScope *apiScope) string {
904 baseName := c.module.BaseModuleName()
905 return c.namingScheme.sourceStubLibraryModuleName(apiScope, baseName)
906}
907
Paul Duffin46dc45a2020-05-14 15:39:10 +0100908// The component names for different outputs of the java_sdk_library.
909//
910// They are similar to the names used for the child modules it creates
911const (
912 stubsSourceComponentName = "stubs.source"
913
914 apiTxtComponentName = "api.txt"
915
916 removedApiTxtComponentName = "removed-api.txt"
Anton Hanssond78eb762021-09-21 15:25:12 +0100917
918 annotationsComponentName = "annotations.zip"
Paul Duffin46dc45a2020-05-14 15:39:10 +0100919)
920
921// A regular expression to match tags that reference a specific stubs component.
922//
923// It will only match if given a valid scope and a valid component. It is verfy strict
924// to ensure it does not accidentally match a similar looking tag that should be processed
925// by the embedded Library.
926var tagSplitter = func() *regexp.Regexp {
927 // Given a list of literal string items returns a regular expression that will
928 // match any one of the items.
929 choice := func(items ...string) string {
930 return `\Q` + strings.Join(items, `\E|\Q`) + `\E`
931 }
932
933 // Regular expression to match one of the scopes.
934 scopesRegexp := choice(allScopeNames...)
935
936 // Regular expression to match one of the components.
Anton Hanssond78eb762021-09-21 15:25:12 +0100937 componentsRegexp := choice(stubsSourceComponentName, apiTxtComponentName, removedApiTxtComponentName, annotationsComponentName)
Paul Duffin46dc45a2020-05-14 15:39:10 +0100938
939 // Regular expression to match any combination of one scope and one component.
940 return regexp.MustCompile(fmt.Sprintf(`^\.(%s)\.(%s)$`, scopesRegexp, componentsRegexp))
941}()
942
943// For OutputFileProducer interface
944//
Anton Hanssond78eb762021-09-21 15:25:12 +0100945// .<scope>.<component name>, for all ComponentNames (for example: .public.removed-api.txt)
Paul Duffin46dc45a2020-05-14 15:39:10 +0100946func (c *commonToSdkLibraryAndImport) commonOutputFiles(tag string) (android.Paths, error) {
947 if groups := tagSplitter.FindStringSubmatch(tag); groups != nil {
948 scopeName := groups[1]
949 component := groups[2]
950
951 if scope, ok := scopeByName[scopeName]; ok {
952 paths := c.findScopePaths(scope)
953 if paths == nil {
Paul Duffin71b33cc2021-06-23 11:39:47 +0100954 return nil, fmt.Errorf("%q does not provide api scope %s", c.module.BaseModuleName(), scopeName)
Paul Duffin46dc45a2020-05-14 15:39:10 +0100955 }
956
957 switch component {
958 case stubsSourceComponentName:
959 if paths.stubsSrcJar.Valid() {
960 return android.Paths{paths.stubsSrcJar.Path()}, nil
961 }
962
963 case apiTxtComponentName:
964 if paths.currentApiFilePath.Valid() {
965 return android.Paths{paths.currentApiFilePath.Path()}, nil
966 }
967
968 case removedApiTxtComponentName:
969 if paths.removedApiFilePath.Valid() {
970 return android.Paths{paths.removedApiFilePath.Path()}, nil
971 }
Anton Hanssond78eb762021-09-21 15:25:12 +0100972
973 case annotationsComponentName:
974 if paths.annotationsZip.Valid() {
975 return android.Paths{paths.annotationsZip.Path()}, nil
976 }
Paul Duffin46dc45a2020-05-14 15:39:10 +0100977 }
978
979 return nil, fmt.Errorf("%s not available for api scope %s", component, scopeName)
980 } else {
981 return nil, fmt.Errorf("unknown scope %s in %s", scope, tag)
982 }
983
984 } else {
Paul Duffina2ae7e02020-09-11 11:55:00 +0100985 switch tag {
986 case ".doctags":
987 if c.doctagPaths != nil {
988 return c.doctagPaths, nil
989 } else {
Paul Duffin71b33cc2021-06-23 11:39:47 +0100990 return nil, fmt.Errorf("no doctag_files specified on %s", c.module.BaseModuleName())
Paul Duffina2ae7e02020-09-11 11:55:00 +0100991 }
992 }
Paul Duffin46dc45a2020-05-14 15:39:10 +0100993 return nil, nil
994 }
995}
996
Paul Duffin803a9562020-05-20 11:52:25 +0100997func (c *commonToSdkLibraryAndImport) getScopePathsCreateIfNeeded(scope *apiScope) *scopePaths {
Paul Duffin56d44902020-01-31 13:36:25 +0000998 if c.scopePaths == nil {
999 c.scopePaths = make(map[*apiScope]*scopePaths)
1000 }
1001 paths := c.scopePaths[scope]
1002 if paths == nil {
1003 paths = &scopePaths{}
1004 c.scopePaths[scope] = paths
1005 }
1006
1007 return paths
1008}
1009
Paul Duffin803a9562020-05-20 11:52:25 +01001010func (c *commonToSdkLibraryAndImport) findScopePaths(scope *apiScope) *scopePaths {
1011 if c.scopePaths == nil {
1012 return nil
1013 }
1014
1015 return c.scopePaths[scope]
1016}
1017
1018// If this does not support the requested api scope then find the closest available
1019// scope it does support. Returns nil if no such scope is available.
1020func (c *commonToSdkLibraryAndImport) findClosestScopePath(scope *apiScope) *scopePaths {
Paul Duffind0b9fca2022-09-30 18:11:41 +01001021 for s := scope; s != nil; s = s.canAccess {
Paul Duffin803a9562020-05-20 11:52:25 +01001022 if paths := c.findScopePaths(s); paths != nil {
1023 return paths
1024 }
1025 }
1026
1027 // This should never happen outside tests as public should be the base scope for every
1028 // scope and is enabled by default.
1029 return nil
1030}
1031
Jiyong Parkf1691d22021-03-29 20:11:58 +09001032func (c *commonToSdkLibraryAndImport) selectHeaderJarsForSdkVersion(ctx android.BaseModuleContext, sdkVersion android.SdkSpec) android.Paths {
Paul Duffinb05d4292020-05-20 12:19:10 +01001033
1034 // If a specific numeric version has been requested then use prebuilt versions of the sdk.
Jiyong Park54105c42021-03-31 18:17:53 +09001035 if !sdkVersion.ApiLevel.IsPreview() {
Paul Duffin71b33cc2021-06-23 11:39:47 +01001036 return PrebuiltJars(ctx, c.module.BaseModuleName(), sdkVersion)
Paul Duffinb05d4292020-05-20 12:19:10 +01001037 }
1038
Paul Duffin1267d872021-04-16 17:21:36 +01001039 paths := c.selectScopePaths(ctx, sdkVersion.Kind)
1040 if paths == nil {
1041 return nil
1042 }
1043
1044 return paths.stubsHeaderPath
1045}
1046
1047// selectScopePaths returns the *scopePaths appropriate for the specific kind.
1048//
1049// If the module does not support the specific kind then it will return the *scopePaths for the
1050// closest kind which is a subset of the requested kind. e.g. if requesting android.SdkModule then
1051// it will return *scopePaths for android.SdkSystem if available or android.SdkPublic of not.
1052func (c *commonToSdkLibraryAndImport) selectScopePaths(ctx android.BaseModuleContext, kind android.SdkKind) *scopePaths {
Paul Duffin32cf58a2021-05-18 16:32:50 +01001053 apiScope := sdkKindToApiScope(kind)
Paul Duffinb05d4292020-05-20 12:19:10 +01001054
Paul Duffin803a9562020-05-20 11:52:25 +01001055 paths := c.findClosestScopePath(apiScope)
1056 if paths == nil {
1057 var scopes []string
1058 for _, s := range allApiScopes {
1059 if c.findScopePaths(s) != nil {
1060 scopes = append(scopes, s.name)
1061 }
1062 }
Paul Duffin71b33cc2021-06-23 11:39:47 +01001063 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 +01001064 return nil
1065 }
1066
Paul Duffin1267d872021-04-16 17:21:36 +01001067 return paths
1068}
1069
Paul Duffin32cf58a2021-05-18 16:32:50 +01001070// sdkKindToApiScope maps from android.SdkKind to apiScope.
1071func sdkKindToApiScope(kind android.SdkKind) *apiScope {
1072 var apiScope *apiScope
1073 switch kind {
1074 case android.SdkSystem:
1075 apiScope = apiScopeSystem
1076 case android.SdkModule:
1077 apiScope = apiScopeModuleLib
1078 case android.SdkTest:
1079 apiScope = apiScopeTest
1080 case android.SdkSystemServer:
1081 apiScope = apiScopeSystemServer
1082 default:
1083 apiScope = apiScopePublic
1084 }
1085 return apiScope
1086}
1087
Paul Duffin1267d872021-04-16 17:21:36 +01001088// to satisfy SdkLibraryDependency interface
Martin Stjernholm8be1e6d2021-09-15 03:34:04 +01001089func (c *commonToSdkLibraryAndImport) SdkApiStubDexJar(ctx android.BaseModuleContext, kind android.SdkKind) OptionalDexJarPath {
Paul Duffin1267d872021-04-16 17:21:36 +01001090 paths := c.selectScopePaths(ctx, kind)
1091 if paths == nil {
Martin Stjernholm8be1e6d2021-09-15 03:34:04 +01001092 return makeUnsetDexJarPath()
Paul Duffin1267d872021-04-16 17:21:36 +01001093 }
1094
1095 return paths.stubsDexJarPath
Paul Duffinb05d4292020-05-20 12:19:10 +01001096}
1097
Paul Duffin32cf58a2021-05-18 16:32:50 +01001098// to satisfy SdkLibraryDependency interface
1099func (c *commonToSdkLibraryAndImport) SdkRemovedTxtFile(ctx android.BaseModuleContext, kind android.SdkKind) android.OptionalPath {
1100 apiScope := sdkKindToApiScope(kind)
1101 paths := c.findScopePaths(apiScope)
1102 if paths == nil {
1103 return android.OptionalPath{}
1104 }
1105
1106 return paths.removedApiFilePath
1107}
1108
Paul Duffin859fe962020-05-15 10:20:31 +01001109func (c *commonToSdkLibraryAndImport) sdkComponentPropertiesForChildLibrary() interface{} {
1110 componentProps := &struct {
Paul Duffin3f0290e2021-06-30 18:25:36 +01001111 SdkLibraryName *string
Paul Duffin859fe962020-05-15 10:20:31 +01001112 SdkLibraryToImplicitlyTrack *string
Paul Duffindfa131e2020-05-15 20:37:11 +01001113 }{}
1114
Paul Duffin3f0290e2021-06-30 18:25:36 +01001115 namePtr := proptools.StringPtr(c.module.BaseModuleName())
1116 componentProps.SdkLibraryName = namePtr
1117
Paul Duffindfa131e2020-05-15 20:37:11 +01001118 if c.sharedLibrary() {
Paul Duffin859fe962020-05-15 10:20:31 +01001119 // Mark the stubs library as being components of this java_sdk_library so that
1120 // any app that includes code which depends (directly or indirectly) on the stubs
1121 // library will have the appropriate <uses-library> invocation inserted into its
1122 // manifest if necessary.
Paul Duffin3f0290e2021-06-30 18:25:36 +01001123 componentProps.SdkLibraryToImplicitlyTrack = namePtr
Paul Duffin859fe962020-05-15 10:20:31 +01001124 }
1125
1126 return componentProps
1127}
1128
Paul Duffindfa131e2020-05-15 20:37:11 +01001129func (c *commonToSdkLibraryAndImport) sharedLibrary() bool {
1130 return proptools.BoolDefault(c.commonSdkLibraryProperties.Shared_library, true)
1131}
1132
Paul Duffinf4600f62021-05-13 22:34:45 +01001133// Check if the stub libraries should be compiled for dex
1134func (c *commonToSdkLibraryAndImport) stubLibrariesCompiledForDex() bool {
1135 // Always compile the dex file files for the stub libraries if they will be used on the
1136 // bootclasspath.
1137 return !c.sharedLibrary()
1138}
1139
Paul Duffin859fe962020-05-15 10:20:31 +01001140// Properties related to the use of a module as an component of a java_sdk_library.
1141type SdkLibraryComponentProperties struct {
Paul Duffin3f0290e2021-06-30 18:25:36 +01001142 // The name of the java_sdk_library/_import module.
1143 SdkLibraryName *string `blueprint:"mutated"`
Paul Duffin859fe962020-05-15 10:20:31 +01001144
1145 // The name of the java_sdk_library/_import to add to a <uses-library> entry
1146 // in the AndroidManifest.xml of any Android app that includes code that references
1147 // this module. If not set then no java_sdk_library/_import is tracked.
1148 SdkLibraryToImplicitlyTrack *string `blueprint:"mutated"`
1149}
1150
1151// Structure to be embedded in a module struct that needs to support the
1152// SdkLibraryComponentDependency interface.
1153type EmbeddableSdkLibraryComponent struct {
1154 sdkLibraryComponentProperties SdkLibraryComponentProperties
1155}
1156
Paul Duffin71b33cc2021-06-23 11:39:47 +01001157func (e *EmbeddableSdkLibraryComponent) initSdkLibraryComponent(module android.Module) {
1158 module.AddProperties(&e.sdkLibraryComponentProperties)
Paul Duffin859fe962020-05-15 10:20:31 +01001159}
1160
1161// to satisfy SdkLibraryComponentDependency
Paul Duffin3f0290e2021-06-30 18:25:36 +01001162func (e *EmbeddableSdkLibraryComponent) SdkLibraryName() *string {
1163 return e.sdkLibraryComponentProperties.SdkLibraryName
1164}
1165
1166// to satisfy SdkLibraryComponentDependency
Ulya Trafimovich39b437b2020-09-23 16:42:35 +01001167func (e *EmbeddableSdkLibraryComponent) OptionalSdkLibraryImplementation() *string {
Ulya Trafimovich78645fb2021-07-16 15:29:25 +01001168 // For shared libraries, this is the same as the SDK library name. If a Java library or app
1169 // depends on a component library (e.g. a stub library) it still needs to know the name of the
1170 // run-time library and the corresponding module that provides the implementation. This name is
1171 // passed to manifest_fixer (to be added to AndroidManifest.xml) and added to CLC (to be used
1172 // in dexpreopt).
1173 //
1174 // For non-shared SDK (component or not) libraries this returns `nil`, as they are not
1175 // <uses-library> and should not be added to the manifest or to CLC.
Ulya Trafimovich39b437b2020-09-23 16:42:35 +01001176 return e.sdkLibraryComponentProperties.SdkLibraryToImplicitlyTrack
1177}
1178
Paul Duffin859fe962020-05-15 10:20:31 +01001179// Implemented by modules that are (or possibly could be) a component of a java_sdk_library
1180// (including the java_sdk_library) itself.
1181type SdkLibraryComponentDependency interface {
Ulya Trafimovich31e444e2020-08-14 17:32:16 +01001182 UsesLibraryDependency
1183
Paul Duffin3f0290e2021-06-30 18:25:36 +01001184 // SdkLibraryName returns the name of the java_sdk_library/_import module.
1185 SdkLibraryName() *string
1186
Ulya Trafimovich39b437b2020-09-23 16:42:35 +01001187 // The name of the implementation library for the optional SDK library or nil, if there isn't one.
1188 OptionalSdkLibraryImplementation() *string
Paul Duffin859fe962020-05-15 10:20:31 +01001189}
1190
1191// Make sure that all the module types that are components of java_sdk_library/_import
1192// and which can be referenced (directly or indirectly) from an android app implement
1193// the SdkLibraryComponentDependency interface.
1194var _ SdkLibraryComponentDependency = (*Library)(nil)
1195var _ SdkLibraryComponentDependency = (*Import)(nil)
1196var _ SdkLibraryComponentDependency = (*SdkLibrary)(nil)
Paul Duffineedc5d52020-06-12 17:46:39 +01001197var _ SdkLibraryComponentDependency = (*SdkLibraryImport)(nil)
Paul Duffin859fe962020-05-15 10:20:31 +01001198
Paul Duffin32cf58a2021-05-18 16:32:50 +01001199// Provides access to sdk_version related files, e.g. header and implementation jars.
Paul Duffin859fe962020-05-15 10:20:31 +01001200type SdkLibraryDependency interface {
1201 SdkLibraryComponentDependency
1202
1203 // Get the header jars appropriate for the supplied sdk_version.
1204 //
1205 // These are turbine generated jars so they only change if the externals of the
1206 // class changes but it does not contain and implementation or JavaDoc.
Jiyong Parkf1691d22021-03-29 20:11:58 +09001207 SdkHeaderJars(ctx android.BaseModuleContext, sdkVersion android.SdkSpec) android.Paths
Paul Duffin859fe962020-05-15 10:20:31 +01001208
1209 // Get the implementation jars appropriate for the supplied sdk version.
1210 //
1211 // These are either the implementation jar for the whole sdk library or the implementation
1212 // jars for the stubs. The latter should only be needed when generating JavaDoc as otherwise
1213 // they are identical to the corresponding header jars.
Jiyong Parkf1691d22021-03-29 20:11:58 +09001214 SdkImplementationJars(ctx android.BaseModuleContext, sdkVersion android.SdkSpec) android.Paths
Paul Duffin1267d872021-04-16 17:21:36 +01001215
1216 // SdkApiStubDexJar returns the dex jar for the stubs. It is needed by the hiddenapi processing
1217 // tool which processes dex files.
Martin Stjernholm8be1e6d2021-09-15 03:34:04 +01001218 SdkApiStubDexJar(ctx android.BaseModuleContext, kind android.SdkKind) OptionalDexJarPath
Paul Duffinf4600f62021-05-13 22:34:45 +01001219
Paul Duffin32cf58a2021-05-18 16:32:50 +01001220 // SdkRemovedTxtFile returns the optional path to the removed.txt file for the specified sdk kind.
1221 SdkRemovedTxtFile(ctx android.BaseModuleContext, kind android.SdkKind) android.OptionalPath
1222
Paul Duffinf4600f62021-05-13 22:34:45 +01001223 // sharedLibrary returns true if this can be used as a shared library.
1224 sharedLibrary() bool
Paul Duffin859fe962020-05-15 10:20:31 +01001225}
1226
Inseob Kimc0907f12019-02-08 21:00:45 +09001227type SdkLibrary struct {
Sundong Ahn054b19a2018-10-19 13:46:09 +09001228 Library
Jiyong Parkc678ad32018-04-10 13:07:10 +09001229
Zi Wangb2179e32023-01-31 15:53:30 -08001230 android.BazelModuleBase
1231
Sundong Ahn054b19a2018-10-19 13:46:09 +09001232 sdkLibraryProperties sdkLibraryProperties
Jiyong Parkc678ad32018-04-10 13:07:10 +09001233
Paul Duffin3375e352020-04-28 10:44:03 +01001234 // Map from api scope to the scope specific property structure.
1235 scopeToProperties map[*apiScope]*ApiScopeProperties
1236
Paul Duffin56d44902020-01-31 13:36:25 +00001237 commonToSdkLibraryAndImport
Jiyong Parkc678ad32018-04-10 13:07:10 +09001238}
1239
Inseob Kimc0907f12019-02-08 21:00:45 +09001240var _ SdkLibraryDependency = (*SdkLibrary)(nil)
Colin Cross897d2ed2019-02-11 14:03:51 -08001241
Paul Duffin3375e352020-04-28 10:44:03 +01001242func (module *SdkLibrary) generateTestAndSystemScopesByDefault() bool {
1243 return module.sdkLibraryProperties.Generate_system_and_test_apis
1244}
1245
1246func (module *SdkLibrary) getGeneratedApiScopes(ctx android.EarlyModuleContext) apiScopes {
1247 // Check to see if any scopes have been explicitly enabled. If any have then all
1248 // must be.
1249 anyScopesExplicitlyEnabled := false
1250 for _, scope := range allApiScopes {
1251 scopeProperties := module.scopeToProperties[scope]
1252 if scopeProperties.Enabled != nil {
1253 anyScopesExplicitlyEnabled = true
1254 break
1255 }
Paul Duffind1b3a922020-01-22 11:57:20 +00001256 }
Paul Duffin3375e352020-04-28 10:44:03 +01001257
1258 var generatedScopes apiScopes
1259 enabledScopes := make(map[*apiScope]struct{})
1260 for _, scope := range allApiScopes {
1261 scopeProperties := module.scopeToProperties[scope]
1262 // If any scopes are explicitly enabled then ignore the legacy enabled status.
1263 // This is to ensure that any new usages of this module type do not rely on legacy
1264 // behaviour.
1265 defaultEnabledStatus := false
1266 if anyScopesExplicitlyEnabled {
1267 defaultEnabledStatus = scope.defaultEnabledStatus
1268 } else {
1269 defaultEnabledStatus = scope.legacyEnabledStatus(module)
1270 }
1271 enabled := proptools.BoolDefault(scopeProperties.Enabled, defaultEnabledStatus)
1272 if enabled {
1273 enabledScopes[scope] = struct{}{}
1274 generatedScopes = append(generatedScopes, scope)
1275 }
1276 }
1277
1278 // Now check to make sure that any scope that is extended by an enabled scope is also
1279 // enabled.
1280 for _, scope := range allApiScopes {
1281 if _, ok := enabledScopes[scope]; ok {
1282 extends := scope.extends
1283 if extends != nil {
1284 if _, ok := enabledScopes[extends]; !ok {
1285 ctx.ModuleErrorf("enabled api scope %q depends on disabled scope %q", scope, extends)
1286 }
1287 }
1288 }
1289 }
1290
1291 return generatedScopes
Paul Duffind1b3a922020-01-22 11:57:20 +00001292}
1293
satayev758968a2021-12-06 11:42:40 +00001294var _ android.ModuleWithMinSdkVersionCheck = (*SdkLibrary)(nil)
1295
satayev8f088b02021-12-06 11:40:46 +00001296func (module *SdkLibrary) CheckMinSdkVersion(ctx android.ModuleContext) {
Spandan Das8c9ae7e2023-03-03 21:20:36 +00001297 android.CheckMinSdkVersion(ctx, module.MinSdkVersion(ctx), func(c android.ModuleContext, do android.PayloadDepsCallback) {
satayev8f088b02021-12-06 11:40:46 +00001298 ctx.WalkDeps(func(child android.Module, parent android.Module) bool {
1299 isExternal := !module.depIsInSameApex(ctx, child)
1300 if am, ok := child.(android.ApexModule); ok {
1301 if !do(ctx, parent, am, isExternal) {
1302 return false
1303 }
1304 }
1305 return !isExternal
1306 })
1307 })
1308}
1309
Paul Duffineedc5d52020-06-12 17:46:39 +01001310type sdkLibraryComponentTag struct {
1311 blueprint.BaseDependencyTag
1312 name string
1313}
1314
1315// Mark this tag so dependencies that use it are excluded from visibility enforcement.
1316func (t sdkLibraryComponentTag) ExcludeFromVisibilityEnforcement() {}
1317
1318var xmlPermissionsFileTag = sdkLibraryComponentTag{name: "xml-permissions-file"}
Paul Duffine74ac732020-02-06 13:51:46 +00001319
Jiyong Parke3833882020-02-17 17:28:10 +09001320func IsXmlPermissionsFileDepTag(depTag blueprint.DependencyTag) bool {
Paul Duffineedc5d52020-06-12 17:46:39 +01001321 if dt, ok := depTag.(sdkLibraryComponentTag); ok {
Jiyong Parke3833882020-02-17 17:28:10 +09001322 return dt == xmlPermissionsFileTag
1323 }
1324 return false
1325}
1326
Paul Duffineedc5d52020-06-12 17:46:39 +01001327var implLibraryTag = sdkLibraryComponentTag{name: "impl-library"}
Paul Duffin5df79302020-05-16 15:52:12 +01001328
Paul Duffin44f1d842020-06-26 20:17:02 +01001329// Add the dependencies on the child modules in the component deps mutator.
1330func (module *SdkLibrary) ComponentDepsMutator(ctx android.BottomUpMutatorContext) {
Paul Duffin3375e352020-04-28 10:44:03 +01001331 for _, apiScope := range module.getGeneratedApiScopes(ctx) {
Paul Duffind1b3a922020-01-22 11:57:20 +00001332 // Add dependencies to the stubs library
Spandan Das877f39d2023-03-29 16:19:51 +00001333 stubModuleName := module.stubsLibraryModuleName(apiScope)
Jihoon Kang1147b312023-06-08 23:25:57 +00001334
Spandan Das877f39d2023-03-29 16:19:51 +00001335 ctx.AddVariationDependencies(nil, apiScope.stubsTag, stubModuleName)
Paul Duffind1b3a922020-01-22 11:57:20 +00001336
Paul Duffin15f34ef2020-07-20 18:04:44 +01001337 // Add a dependency on the stubs source in order to access both stubs source and api information.
1338 ctx.AddVariationDependencies(nil, apiScope.stubsSourceAndApiTag, module.stubsSourceModuleName(apiScope))
Paul Duffin958806b2022-05-16 13:10:47 +00001339
1340 if module.compareAgainstLatestApi(apiScope) {
1341 // Add dependencies on the latest finalized version of the API .txt file.
1342 latestApiModuleName := module.latestApiModuleName(apiScope)
1343 ctx.AddDependency(module, apiScope.latestApiModuleTag, latestApiModuleName)
1344
1345 // Add dependencies on the latest finalized version of the remove API .txt file.
1346 latestRemovedApiModuleName := module.latestRemovedApiModuleName(apiScope)
1347 ctx.AddDependency(module, apiScope.latestRemovedApiModuleTag, latestRemovedApiModuleName)
1348 }
Sundong Ahn054b19a2018-10-19 13:46:09 +09001349 }
1350
Paul Duffindfa131e2020-05-15 20:37:11 +01001351 if module.requiresRuntimeImplementationLibrary() {
Paul Duffin5df79302020-05-16 15:52:12 +01001352 // Add dependency to the rule for generating the implementation library.
1353 ctx.AddDependency(module, implLibraryTag, module.implLibraryModuleName())
1354
Paul Duffindfa131e2020-05-15 20:37:11 +01001355 if module.sharedLibrary() {
1356 // Add dependency to the rule for generating the xml permissions file
Paul Duffineedc5d52020-06-12 17:46:39 +01001357 ctx.AddDependency(module, xmlPermissionsFileTag, module.xmlPermissionsModuleName())
Paul Duffindfa131e2020-05-15 20:37:11 +01001358 }
Paul Duffin44f1d842020-06-26 20:17:02 +01001359 }
1360}
Paul Duffine74ac732020-02-06 13:51:46 +00001361
Paul Duffin44f1d842020-06-26 20:17:02 +01001362// Add other dependencies as normal.
1363func (module *SdkLibrary) DepsMutator(ctx android.BottomUpMutatorContext) {
Anton Hanssone77fccc2021-01-20 16:52:41 +00001364 var missingApiModules []string
1365 for _, apiScope := range module.getGeneratedApiScopes(ctx) {
1366 if apiScope.unstable {
1367 continue
1368 }
Paul Duffin958806b2022-05-16 13:10:47 +00001369 if m := module.latestApiModuleName(apiScope); !ctx.OtherModuleExists(m) {
Anton Hanssone77fccc2021-01-20 16:52:41 +00001370 missingApiModules = append(missingApiModules, m)
1371 }
Paul Duffin958806b2022-05-16 13:10:47 +00001372 if m := module.latestRemovedApiModuleName(apiScope); !ctx.OtherModuleExists(m) {
Anton Hanssone77fccc2021-01-20 16:52:41 +00001373 missingApiModules = append(missingApiModules, m)
1374 }
Paul Duffin958806b2022-05-16 13:10:47 +00001375 if m := module.latestIncompatibilitiesModuleName(apiScope); !ctx.OtherModuleExists(m) {
Jaewoong Jung1a97ee02021-03-09 13:25:02 -08001376 missingApiModules = append(missingApiModules, m)
1377 }
Anton Hanssone77fccc2021-01-20 16:52:41 +00001378 }
1379 if len(missingApiModules) != 0 && !module.sdkLibraryProperties.Unsafe_ignore_missing_latest_api {
1380 m := module.Name() + " is missing tracking files for previously released library versions.\n"
1381 m += "You need to do one of the following:\n"
1382 m += "- Add `unsafe_ignore_missing_latest_api: true` to your blueprint (to disable compat tracking)\n"
1383 m += "- Add a set of prebuilt txt files representing the last released version of this library for compat checking.\n"
1384 m += " (the current set of API files can be used as a seed for this compatibility tracking\n"
1385 m += "\n"
1386 m += "The following filegroup modules are missing:\n "
1387 m += strings.Join(missingApiModules, "\n ") + "\n"
1388 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."
1389 ctx.ModuleErrorf(m)
1390 }
Paul Duffin44f1d842020-06-26 20:17:02 +01001391 if module.requiresRuntimeImplementationLibrary() {
Paul Duffindfa131e2020-05-15 20:37:11 +01001392 // Only add the deps for the library if it is actually going to be built.
1393 module.Library.deps(ctx)
1394 }
Jiyong Parkc678ad32018-04-10 13:07:10 +09001395}
1396
Paul Duffin46dc45a2020-05-14 15:39:10 +01001397func (module *SdkLibrary) OutputFiles(tag string) (android.Paths, error) {
1398 paths, err := module.commonOutputFiles(tag)
Colin Cross4acaea92021-12-10 23:05:02 +00001399 if paths != nil || err != nil {
Paul Duffin46dc45a2020-05-14 15:39:10 +01001400 return paths, err
1401 }
Colin Cross4acaea92021-12-10 23:05:02 +00001402 if module.requiresRuntimeImplementationLibrary() {
1403 return module.Library.OutputFiles(tag)
1404 }
1405 if tag == "" {
1406 return nil, nil
1407 }
1408 return nil, fmt.Errorf("unsupported module reference tag %q", tag)
Paul Duffin46dc45a2020-05-14 15:39:10 +01001409}
1410
Inseob Kimc0907f12019-02-08 21:00:45 +09001411func (module *SdkLibrary) GenerateAndroidBuildActions(ctx android.ModuleContext) {
satayev8f088b02021-12-06 11:40:46 +00001412 if proptools.String(module.deviceProperties.Min_sdk_version) != "" {
1413 module.CheckMinSdkVersion(ctx)
1414 }
1415
Paul Duffina2ae7e02020-09-11 11:55:00 +01001416 module.generateCommonBuildActions(ctx)
1417
Paul Duffindfa131e2020-05-15 20:37:11 +01001418 // Only build an implementation library if required.
1419 if module.requiresRuntimeImplementationLibrary() {
Paul Duffin43db9be2019-12-30 17:35:49 +00001420 module.Library.GenerateAndroidBuildActions(ctx)
1421 }
Sundong Ahn054b19a2018-10-19 13:46:09 +09001422
Paul Duffinb97b1572021-04-29 21:50:40 +01001423 // Collate the components exported by this module. All scope specific modules are exported but
1424 // the impl and xml component modules are not.
1425 exportedComponents := map[string]struct{}{}
1426
Sundong Ahn57368eb2018-07-06 11:20:23 +09001427 // Record the paths to the header jars of the library (stubs and impl).
Paul Duffind1b3a922020-01-22 11:57:20 +00001428 // When this java_sdk_library is depended upon from others via "libs" property,
Jiyong Parkc678ad32018-04-10 13:07:10 +09001429 // the recorded paths will be returned depending on the link type of the caller.
1430 ctx.VisitDirectDeps(func(to android.Module) {
Jiyong Parkc678ad32018-04-10 13:07:10 +09001431 tag := ctx.OtherModuleDependencyTag(to)
1432
Paul Duffinc8782502020-04-29 20:45:27 +01001433 // Extract information from any of the scope specific dependencies.
1434 if scopeTag, ok := tag.(scopeDependencyTag); ok {
1435 apiScope := scopeTag.apiScope
Paul Duffin803a9562020-05-20 11:52:25 +01001436 scopePaths := module.getScopePathsCreateIfNeeded(apiScope)
Paul Duffinc8782502020-04-29 20:45:27 +01001437
1438 // Extract information from the dependency. The exact information extracted
1439 // is determined by the nature of the dependency which is determined by the tag.
1440 scopeTag.extractDepInfo(ctx, to, scopePaths)
Paul Duffinb97b1572021-04-29 21:50:40 +01001441
1442 exportedComponents[ctx.OtherModuleName(to)] = struct{}{}
Sundong Ahn20e998b2018-07-24 11:19:26 +09001443 }
Jiyong Parkc678ad32018-04-10 13:07:10 +09001444 })
Paul Duffinb97b1572021-04-29 21:50:40 +01001445
1446 // Make the set of components exported by this module available for use elsewhere.
Cole Faust18994c72023-02-28 16:02:16 -08001447 exportedComponentInfo := android.ExportedComponentsInfo{Components: android.SortedKeys(exportedComponents)}
Paul Duffinb97b1572021-04-29 21:50:40 +01001448 ctx.SetProvider(android.ExportedComponentsInfoProvider, exportedComponentInfo)
Paul Duffin958806b2022-05-16 13:10:47 +00001449
1450 // Provide additional information for inclusion in an sdk's generated .info file.
1451 additionalSdkInfo := map[string]interface{}{}
1452 additionalSdkInfo["dist_stem"] = module.distStem()
Paul Duffine8409952022-09-22 16:24:46 +01001453 baseModuleName := module.distStem()
Paul Duffin958806b2022-05-16 13:10:47 +00001454 scopes := map[string]interface{}{}
1455 additionalSdkInfo["scopes"] = scopes
1456 for scope, scopePaths := range module.scopePaths {
1457 scopeInfo := map[string]interface{}{}
1458 scopes[scope.name] = scopeInfo
1459 scopeInfo["current_api"] = scope.snapshotRelativeCurrentApiTxtPath(baseModuleName)
1460 scopeInfo["removed_api"] = scope.snapshotRelativeRemovedApiTxtPath(baseModuleName)
1461 if p := scopePaths.latestApiPath; p.Valid() {
1462 scopeInfo["latest_api"] = p.Path().String()
1463 }
1464 if p := scopePaths.latestRemovedApiPath; p.Valid() {
1465 scopeInfo["latest_removed_api"] = p.Path().String()
1466 }
1467 }
1468 ctx.SetProvider(android.AdditionalSdkInfoProvider, android.AdditionalSdkInfo{additionalSdkInfo})
Jiyong Parkc678ad32018-04-10 13:07:10 +09001469}
1470
Jiyong Park0b0e1b92019-12-03 13:24:29 +09001471func (module *SdkLibrary) AndroidMkEntries() []android.AndroidMkEntries {
Paul Duffindfa131e2020-05-15 20:37:11 +01001472 if !module.requiresRuntimeImplementationLibrary() {
Paul Duffin43db9be2019-12-30 17:35:49 +00001473 return nil
1474 }
Jiyong Park0b0e1b92019-12-03 13:24:29 +09001475 entriesList := module.Library.AndroidMkEntries()
Yo Chiang07d75072020-06-05 17:43:19 +08001476 if module.sharedLibrary() {
1477 entries := &entriesList[0]
1478 entries.Required = append(entries.Required, module.xmlPermissionsModuleName())
1479 }
Jiyong Park0b0e1b92019-12-03 13:24:29 +09001480 return entriesList
Jiyong Park82484c02018-04-23 21:41:26 +09001481}
1482
Anton Hansson5fd5d242020-03-27 19:43:19 +00001483// The dist path of the stub artifacts
1484func (module *SdkLibrary) apiDistPath(apiScope *apiScope) string {
Colin Crossf0eace92021-06-02 13:02:23 -07001485 return path.Join("apistubs", module.distGroup(), apiScope.name)
Anton Hansson5fd5d242020-03-27 19:43:19 +00001486}
1487
Paul Duffin12ceb462019-12-24 20:31:31 +00001488// Get the sdk version for use when compiling the stubs library.
Paul Duffin780c5f42020-05-12 15:52:55 +01001489func (module *SdkLibrary) sdkVersionForStubsLibrary(mctx android.EarlyModuleContext, apiScope *apiScope) string {
Paul Duffin87a05a32020-05-12 11:50:28 +01001490 scopeProperties := module.scopeToProperties[apiScope]
1491 if scopeProperties.Sdk_version != nil {
1492 return proptools.String(scopeProperties.Sdk_version)
1493 }
1494
Jiyong Parkf1691d22021-03-29 20:11:58 +09001495 sdkDep := decodeSdkDep(mctx, android.SdkContext(&module.Library))
Paul Duffin12ceb462019-12-24 20:31:31 +00001496 if sdkDep.hasStandardLibs() {
1497 // If building against a standard sdk then use the sdk version appropriate for the scope.
Paul Duffind1b3a922020-01-22 11:57:20 +00001498 return apiScope.sdkVersion
Paul Duffin12ceb462019-12-24 20:31:31 +00001499 } else {
1500 // Otherwise, use no system module.
1501 return "none"
1502 }
1503}
1504
Paul Duffin31310252020-11-20 21:26:20 +00001505func (module *SdkLibrary) distStem() string {
1506 return proptools.StringDefault(module.sdkLibraryProperties.Dist_stem, module.BaseModuleName())
1507}
1508
Colin Cross986b69a2021-06-01 13:13:40 -07001509// distGroup returns the subdirectory of the dist path of the stub artifacts.
1510func (module *SdkLibrary) distGroup() string {
Colin Cross59b92bf2021-06-01 14:07:56 -07001511 return proptools.StringDefault(module.sdkLibraryProperties.Dist_group, "unknown")
Colin Cross986b69a2021-06-01 13:13:40 -07001512}
1513
Paul Duffin958806b2022-05-16 13:10:47 +00001514func latestPrebuiltApiModuleName(name string, apiScope *apiScope) string {
1515 return PrebuiltApiModuleName(name, apiScope.name, "latest")
1516}
1517
Paul Duffind1b3a922020-01-22 11:57:20 +00001518func (module *SdkLibrary) latestApiFilegroupName(apiScope *apiScope) string {
Paul Duffin958806b2022-05-16 13:10:47 +00001519 return ":" + module.latestApiModuleName(apiScope)
1520}
1521
1522func (module *SdkLibrary) latestApiModuleName(apiScope *apiScope) string {
1523 return latestPrebuiltApiModuleName(module.distStem(), apiScope)
Jiyong Park58c518b2018-05-12 22:29:12 +09001524}
Jiyong Parkc678ad32018-04-10 13:07:10 +09001525
Paul Duffind1b3a922020-01-22 11:57:20 +00001526func (module *SdkLibrary) latestRemovedApiFilegroupName(apiScope *apiScope) string {
Paul Duffin958806b2022-05-16 13:10:47 +00001527 return ":" + module.latestRemovedApiModuleName(apiScope)
1528}
1529
1530func (module *SdkLibrary) latestRemovedApiModuleName(apiScope *apiScope) string {
1531 return latestPrebuiltApiModuleName(module.distStem()+"-removed", apiScope)
Jiyong Parkc678ad32018-04-10 13:07:10 +09001532}
1533
Jaewoong Jung1a97ee02021-03-09 13:25:02 -08001534func (module *SdkLibrary) latestIncompatibilitiesFilegroupName(apiScope *apiScope) string {
Paul Duffin958806b2022-05-16 13:10:47 +00001535 return ":" + module.latestIncompatibilitiesModuleName(apiScope)
1536}
1537
1538func (module *SdkLibrary) latestIncompatibilitiesModuleName(apiScope *apiScope) string {
1539 return latestPrebuiltApiModuleName(module.distStem()+"-incompatibilities", apiScope)
Jaewoong Jung1a97ee02021-03-09 13:25:02 -08001540}
1541
Jihoon Kang1c92c3e2023-03-23 17:44:51 +00001542func (module *SdkLibrary) contributesToApiSurface(c android.Config) bool {
1543 _, exists := c.GetApiLibraries()[module.Name()]
1544 return exists
1545}
1546
Jihoon Kang0c705a42023-08-02 06:44:57 +00001547// The listed modules are the special java_sdk_libraries where apiScope.kind do not match the
1548// api surface that the module contribute to. For example, the public droidstubs and java_library
1549// do not contribute to the public api surface, but contributes to the core platform api surface.
1550// This method returns the full api surface stub lib that
1551// the generated java_api_library should depend on.
1552func (module *SdkLibrary) alternativeFullApiSurfaceStubLib() string {
1553 if val, ok := apiLibraryAdditionalProperties[module.Name()]; ok {
1554 return val.FullApiSurfaceStubLib
1555 }
1556 return ""
1557}
1558
1559// The listed modules' stubs contents do not match the corresponding txt files,
1560// but require additional api contributions to generate the full stubs.
1561// This method returns the name of the additional api contribution module
1562// for corresponding sdk_library modules.
1563func (module *SdkLibrary) apiLibraryAdditionalApiContribution() string {
1564 if val, ok := apiLibraryAdditionalProperties[module.Name()]; ok {
1565 return val.AdditionalApiContribution
1566 }
1567 return ""
1568}
1569
Anton Hansson944e77d2020-08-19 11:40:22 +01001570func childModuleVisibility(childVisibility []string) []string {
1571 if childVisibility == nil {
1572 // No child visibility set. The child will use the visibility of the sdk_library.
1573 return nil
1574 }
1575
1576 // Prepend an override to ignore the sdk_library's visibility, and rely on the child visibility.
1577 var visibility []string
1578 visibility = append(visibility, "//visibility:override")
1579 visibility = append(visibility, childVisibility...)
1580 return visibility
1581}
1582
Paul Duffin5df79302020-05-16 15:52:12 +01001583// Creates the implementation java library
1584func (module *SdkLibrary) createImplLibrary(mctx android.DefaultableHookContext) {
Anton Hansson944e77d2020-08-19 11:40:22 +01001585 visibility := childModuleVisibility(module.sdkLibraryProperties.Impl_library_visibility)
1586
Paul Duffin5df79302020-05-16 15:52:12 +01001587 props := struct {
Paul Duffin77590a82022-04-28 14:13:30 +00001588 Name *string
1589 Visibility []string
1590 Instrument bool
1591 Libs []string
1592 Static_libs []string
1593 Apex_available []string
Paul Duffin5df79302020-05-16 15:52:12 +01001594 }{
1595 Name: proptools.StringPtr(module.implLibraryModuleName()),
Anton Hansson944e77d2020-08-19 11:40:22 +01001596 Visibility: visibility,
Paul Duffin296cf332020-06-18 21:09:55 +01001597 // Set the instrument property to ensure it is instrumented when instrumentation is required.
1598 Instrument: true,
Anton Hansson7f66efa2020-10-08 14:47:23 +01001599 // Set the impl_only libs. Note that the module's "Libs" get appended as well, via the
1600 // addition of &module.properties below.
1601 Libs: module.sdkLibraryProperties.Impl_only_libs,
Paul Duffin77590a82022-04-28 14:13:30 +00001602 // Set the impl_only static libs. Note that the module's "static_libs" get appended as well, via the
1603 // addition of &module.properties below.
1604 Static_libs: module.sdkLibraryProperties.Impl_only_static_libs,
1605 // Pass the apex_available settings down so that the impl library can be statically
1606 // embedded within a library that is added to an APEX. Needed for updatable-media.
1607 Apex_available: module.ApexAvailable(),
Paul Duffin5df79302020-05-16 15:52:12 +01001608 }
1609
1610 properties := []interface{}{
1611 &module.properties,
1612 &module.protoProperties,
1613 &module.deviceProperties,
Liz Kammera7a64f32020-07-09 15:16:41 -07001614 &module.dexProperties,
Paul Duffin5df79302020-05-16 15:52:12 +01001615 &module.dexpreoptProperties,
Colin Cross014489c2020-06-02 20:09:13 -07001616 &module.linter.properties,
Paul Duffin5df79302020-05-16 15:52:12 +01001617 &props,
1618 module.sdkComponentPropertiesForChildLibrary(),
1619 }
1620 mctx.CreateModule(LibraryFactory, properties...)
1621}
1622
Jiyong Parkc678ad32018-04-10 13:07:10 +09001623// Creates a static java library that has API stubs
Paul Duffinf0229202020-04-29 16:47:28 +01001624func (module *SdkLibrary) createStubsLibrary(mctx android.DefaultableHookContext, apiScope *apiScope) {
Jiyong Parkc678ad32018-04-10 13:07:10 +09001625 props := struct {
Dan Willemsen9f435972020-05-28 15:28:00 -07001626 Name *string
1627 Visibility []string
1628 Srcs []string
1629 Installable *bool
1630 Sdk_version *string
1631 System_modules *string
1632 Patch_module *string
1633 Libs []string
Anton Hanssondae54cd2021-04-21 16:30:10 +01001634 Static_libs []string
Dan Willemsen9f435972020-05-28 15:28:00 -07001635 Compile_dex *bool
1636 Java_version *string
1637 Openjdk9 struct {
Sundong Ahn054b19a2018-10-19 13:46:09 +09001638 Srcs []string
1639 Javacflags []string
1640 }
Anton Hansson5fd5d242020-03-27 19:43:19 +00001641 Dist struct {
1642 Targets []string
1643 Dest *string
1644 Dir *string
1645 Tag *string
1646 }
Jiyong Parkc678ad32018-04-10 13:07:10 +09001647 }{}
1648
Jihoon Kang1147b312023-06-08 23:25:57 +00001649 props.Name = proptools.StringPtr(module.sourceStubLibraryModuleName(apiScope))
Jihoon Kang786df932023-09-07 01:18:31 +00001650 props.Visibility = []string{"//visibility:override", "//visibility:private"}
Jiyong Parkc678ad32018-04-10 13:07:10 +09001651 // sources are generated from the droiddoc
Paul Duffinc3091c82020-05-08 14:16:20 +01001652 props.Srcs = []string{":" + module.stubsSourceModuleName(apiScope)}
Paul Duffin12ceb462019-12-24 20:31:31 +00001653 sdkVersion := module.sdkVersionForStubsLibrary(mctx, apiScope)
Paul Duffin52d398a2019-06-11 12:31:14 +01001654 props.Sdk_version = proptools.StringPtr(sdkVersion)
Paul Duffina18abc22020-05-16 18:54:24 +01001655 props.System_modules = module.deviceProperties.System_modules
1656 props.Patch_module = module.properties.Patch_module
Paul Duffin367ab912019-12-23 19:40:36 +00001657 props.Installable = proptools.BoolPtr(false)
Sundong Ahn054b19a2018-10-19 13:46:09 +09001658 props.Libs = module.sdkLibraryProperties.Stub_only_libs
Mark White9421c4c2023-08-10 00:07:03 +00001659 props.Libs = append(props.Libs, module.scopeToProperties[apiScope].Libs...)
Anton Hanssondae54cd2021-04-21 16:30:10 +01001660 props.Static_libs = module.sdkLibraryProperties.Stub_only_static_libs
Paul Duffine22c2ab2020-05-20 19:35:27 +01001661 // The stub-annotations library contains special versions of the annotations
1662 // with CLASS retention policy, so that they're kept.
1663 if proptools.Bool(module.sdkLibraryProperties.Annotations_enabled) {
1664 props.Libs = append(props.Libs, "stub-annotations")
1665 }
Paul Duffina18abc22020-05-16 18:54:24 +01001666 props.Openjdk9.Srcs = module.properties.Openjdk9.Srcs
1667 props.Openjdk9.Javacflags = module.properties.Openjdk9.Javacflags
Anton Hansson83509b52020-05-21 09:21:57 +01001668 // We compile the stubs for 1.8 in line with the main android.jar stubs, and potential
1669 // interop with older developer tools that don't support 1.9.
1670 props.Java_version = proptools.StringPtr("1.8")
Paul Duffinf4600f62021-05-13 22:34:45 +01001671
Paul Duffin859fe962020-05-15 10:20:31 +01001672 mctx.CreateModule(LibraryFactory, &props, module.sdkComponentPropertiesForChildLibrary())
Jiyong Parkc678ad32018-04-10 13:07:10 +09001673}
1674
Paul Duffin6d0886e2020-04-07 18:49:53 +01001675// Creates a droidstubs module that creates stubs source files from the given full source
Paul Duffinc8782502020-04-29 20:45:27 +01001676// files and also updates and checks the API specification files.
Paul Duffin15f34ef2020-07-20 18:04:44 +01001677func (module *SdkLibrary) createStubsSourcesAndApi(mctx android.DefaultableHookContext, apiScope *apiScope, name string, scopeSpecificDroidstubsArgs []string) {
Jiyong Parkc678ad32018-04-10 13:07:10 +09001678 props := struct {
Sundong Ahn054b19a2018-10-19 13:46:09 +09001679 Name *string
Paul Duffin4911a892020-04-29 23:35:13 +01001680 Visibility []string
Sundong Ahn054b19a2018-10-19 13:46:09 +09001681 Srcs []string
1682 Installable *bool
Paul Duffin52d398a2019-06-11 12:31:14 +01001683 Sdk_version *string
Jihoon Kang3198f3c2023-01-26 08:08:52 +00001684 Api_surface *string
Paul Duffin12ceb462019-12-24 20:31:31 +00001685 System_modules *string
Sundong Ahn054b19a2018-10-19 13:46:09 +09001686 Libs []string
Paul Duffin6877e6d2020-09-25 19:59:14 +01001687 Output_javadoc_comments *bool
Paul Duffin11512472019-02-11 15:55:17 +00001688 Arg_files []string
Sundong Ahn054b19a2018-10-19 13:46:09 +09001689 Args *string
Sundong Ahn054b19a2018-10-19 13:46:09 +09001690 Java_version *string
Paul Duffine22c2ab2020-05-20 19:35:27 +01001691 Annotations_enabled *bool
Sundong Ahn054b19a2018-10-19 13:46:09 +09001692 Merge_annotations_dirs []string
1693 Merge_inclusion_annotations_dirs []string
Paul Duffin0ff08bd2020-04-29 13:30:54 +01001694 Generate_stubs *bool
Anton Hanssone87b03d2020-12-21 15:29:34 +00001695 Previous_api *string
Sundong Ahn054b19a2018-10-19 13:46:09 +09001696 Check_api struct {
Anton Hanssone6056152020-12-31 10:37:27 +00001697 Current ApiToCheck
1698 Last_released ApiToCheck
Paul Duffin160fe412020-05-10 19:32:20 +01001699
1700 Api_lint struct {
1701 Enabled *bool
1702 New_since *string
1703 Baseline_file *string
1704 }
Jiyong Park58c518b2018-05-12 22:29:12 +09001705 }
Sundong Ahn1b92c822018-05-29 11:35:17 +09001706 Aidl struct {
1707 Include_dirs []string
1708 Local_include_dirs []string
1709 }
Paul Duffin040e9062020-11-23 17:41:36 +00001710 Dists []android.Dist
Jiyong Parkc678ad32018-04-10 13:07:10 +09001711 }{}
1712
Paul Duffin7b78b4d2020-04-28 14:08:32 +01001713 // The stubs source processing uses the same compile time classpath when extracting the
1714 // API from the implementation library as it does when compiling it. i.e. the same
1715 // * sdk version
1716 // * system_modules
1717 // * libs (static_libs/libs)
Paul Duffin250e6192019-06-07 10:44:37 +01001718
Paul Duffin0ff08bd2020-04-29 13:30:54 +01001719 props.Name = proptools.StringPtr(name)
Anton Hansson944e77d2020-08-19 11:40:22 +01001720 props.Visibility = childModuleVisibility(module.sdkLibraryProperties.Stubs_source_visibility)
Paul Duffina18abc22020-05-16 18:54:24 +01001721 props.Srcs = append(props.Srcs, module.properties.Srcs...)
Anton Hanssonf8ea3722021-09-16 14:24:13 +01001722 props.Srcs = append(props.Srcs, module.sdkLibraryProperties.Api_srcs...)
Paul Duffina18abc22020-05-16 18:54:24 +01001723 props.Sdk_version = module.deviceProperties.Sdk_version
Jihoon Kang3198f3c2023-01-26 08:08:52 +00001724 props.Api_surface = &apiScope.name
Paul Duffina18abc22020-05-16 18:54:24 +01001725 props.System_modules = module.deviceProperties.System_modules
Jiyong Parkc678ad32018-04-10 13:07:10 +09001726 props.Installable = proptools.BoolPtr(false)
Sundong Ahne6f0b052018-06-05 16:46:14 +09001727 // A droiddoc module has only one Libs property and doesn't distinguish between
1728 // shared libs and static libs. So we need to add both of these libs to Libs property.
Paul Duffina18abc22020-05-16 18:54:24 +01001729 props.Libs = module.properties.Libs
1730 props.Libs = append(props.Libs, module.properties.Static_libs...)
Nikita Ioffed732da72022-11-21 12:38:25 +00001731 props.Libs = append(props.Libs, module.sdkLibraryProperties.Stub_only_libs...)
Mark White9421c4c2023-08-10 00:07:03 +00001732 props.Libs = append(props.Libs, module.scopeToProperties[apiScope].Libs...)
Paul Duffina18abc22020-05-16 18:54:24 +01001733 props.Aidl.Include_dirs = module.deviceProperties.Aidl.Include_dirs
1734 props.Aidl.Local_include_dirs = module.deviceProperties.Aidl.Local_include_dirs
1735 props.Java_version = module.properties.Java_version
Jiyong Parkc678ad32018-04-10 13:07:10 +09001736
Paul Duffine22c2ab2020-05-20 19:35:27 +01001737 props.Annotations_enabled = module.sdkLibraryProperties.Annotations_enabled
Sundong Ahn054b19a2018-10-19 13:46:09 +09001738 props.Merge_annotations_dirs = module.sdkLibraryProperties.Merge_annotations_dirs
1739 props.Merge_inclusion_annotations_dirs = module.sdkLibraryProperties.Merge_inclusion_annotations_dirs
1740
Paul Duffin6d0886e2020-04-07 18:49:53 +01001741 droidstubsArgs := []string{}
Paul Duffin235ffff2019-12-24 10:41:30 +00001742 if len(module.sdkLibraryProperties.Api_packages) != 0 {
Paul Duffin6d0886e2020-04-07 18:49:53 +01001743 droidstubsArgs = append(droidstubsArgs, "--stub-packages "+strings.Join(module.sdkLibraryProperties.Api_packages, ":"))
Paul Duffin235ffff2019-12-24 10:41:30 +00001744 }
1745 if len(module.sdkLibraryProperties.Hidden_api_packages) != 0 {
Paul Duffin6d0886e2020-04-07 18:49:53 +01001746 droidstubsArgs = append(droidstubsArgs,
Paul Duffin235ffff2019-12-24 10:41:30 +00001747 android.JoinWithPrefix(module.sdkLibraryProperties.Hidden_api_packages, " --hide-package "))
1748 }
Paul Duffin6d0886e2020-04-07 18:49:53 +01001749 droidstubsArgs = append(droidstubsArgs, module.sdkLibraryProperties.Droiddoc_options...)
Paul Duffin235ffff2019-12-24 10:41:30 +00001750 disabledWarnings := []string{
Paul Duffin235ffff2019-12-24 10:41:30 +00001751 "BroadcastBehavior",
Paul Duffin235ffff2019-12-24 10:41:30 +00001752 "DeprecationMismatch",
Anton Hansson3c0779a2022-02-18 19:24:30 +00001753 "HiddenSuperclass",
Anton Hansson3c0779a2022-02-18 19:24:30 +00001754 "MissingPermission",
1755 "SdkConstant",
Paul Duffin235ffff2019-12-24 10:41:30 +00001756 "Todo",
Paul Duffin235ffff2019-12-24 10:41:30 +00001757 }
Paul Duffin6d0886e2020-04-07 18:49:53 +01001758 droidstubsArgs = append(droidstubsArgs, android.JoinWithPrefix(disabledWarnings, "--hide "))
Sundong Ahnfb2721f2018-09-17 13:23:09 +09001759
Paul Duffin6877e6d2020-09-25 19:59:14 +01001760 // Output Javadoc comments for public scope.
1761 if apiScope == apiScopePublic {
1762 props.Output_javadoc_comments = proptools.BoolPtr(true)
1763 }
1764
Paul Duffin1fb487d2020-04-07 18:50:10 +01001765 // Add in scope specific arguments.
Paul Duffin0ff08bd2020-04-29 13:30:54 +01001766 droidstubsArgs = append(droidstubsArgs, scopeSpecificDroidstubsArgs...)
Paul Duffin11512472019-02-11 15:55:17 +00001767 props.Arg_files = module.sdkLibraryProperties.Droiddoc_option_files
Paul Duffin6d0886e2020-04-07 18:49:53 +01001768 props.Args = proptools.StringPtr(strings.Join(droidstubsArgs, " "))
Jiyong Parkc678ad32018-04-10 13:07:10 +09001769
Paul Duffin15f34ef2020-07-20 18:04:44 +01001770 // List of APIs identified from the provided source files are created. They are later
1771 // compared against to the not-yet-released (a.k.a current) list of APIs and to the
1772 // last-released (a.k.a numbered) list of API.
1773 currentApiFileName := apiScope.apiFilePrefix + "current.txt"
1774 removedApiFileName := apiScope.apiFilePrefix + "removed.txt"
1775 apiDir := module.getApiDir()
1776 currentApiFileName = path.Join(apiDir, currentApiFileName)
1777 removedApiFileName = path.Join(apiDir, removedApiFileName)
Jiyong Parkc678ad32018-04-10 13:07:10 +09001778
Paul Duffin15f34ef2020-07-20 18:04:44 +01001779 // check against the not-yet-release API
1780 props.Check_api.Current.Api_file = proptools.StringPtr(currentApiFileName)
1781 props.Check_api.Current.Removed_api_file = proptools.StringPtr(removedApiFileName)
Jiyong Park58c518b2018-05-12 22:29:12 +09001782
Paul Duffin958806b2022-05-16 13:10:47 +00001783 if module.compareAgainstLatestApi(apiScope) {
Paul Duffin15f34ef2020-07-20 18:04:44 +01001784 // check against the latest released API
1785 latestApiFilegroupName := proptools.StringPtr(module.latestApiFilegroupName(apiScope))
Anton Hanssone87b03d2020-12-21 15:29:34 +00001786 props.Previous_api = latestApiFilegroupName
Paul Duffin15f34ef2020-07-20 18:04:44 +01001787 props.Check_api.Last_released.Api_file = latestApiFilegroupName
1788 props.Check_api.Last_released.Removed_api_file = proptools.StringPtr(
1789 module.latestRemovedApiFilegroupName(apiScope))
Jaewoong Jung1a97ee02021-03-09 13:25:02 -08001790 props.Check_api.Last_released.Baseline_file = proptools.StringPtr(
1791 module.latestIncompatibilitiesFilegroupName(apiScope))
Paul Duffin160fe412020-05-10 19:32:20 +01001792
Paul Duffin15f34ef2020-07-20 18:04:44 +01001793 if proptools.Bool(module.sdkLibraryProperties.Api_lint.Enabled) {
1794 // Enable api lint.
1795 props.Check_api.Api_lint.Enabled = proptools.BoolPtr(true)
1796 props.Check_api.Api_lint.New_since = latestApiFilegroupName
Paul Duffin160fe412020-05-10 19:32:20 +01001797
Paul Duffin15f34ef2020-07-20 18:04:44 +01001798 // If it exists then pass a lint-baseline.txt through to droidstubs.
1799 baselinePath := path.Join(apiDir, apiScope.apiFilePrefix+"lint-baseline.txt")
1800 baselinePathRelativeToRoot := path.Join(mctx.ModuleDir(), baselinePath)
1801 paths, err := mctx.GlobWithDeps(baselinePathRelativeToRoot, nil)
1802 if err != nil {
1803 mctx.ModuleErrorf("error checking for presence of %s: %s", baselinePathRelativeToRoot, err)
1804 }
1805 if len(paths) == 1 {
1806 props.Check_api.Api_lint.Baseline_file = proptools.StringPtr(baselinePath)
1807 } else if len(paths) != 0 {
1808 mctx.ModuleErrorf("error checking for presence of %s: expected one path, found: %v", baselinePathRelativeToRoot, paths)
Paul Duffin160fe412020-05-10 19:32:20 +01001809 }
1810 }
Paul Duffin15f34ef2020-07-20 18:04:44 +01001811 }
Jiyong Park58c518b2018-05-12 22:29:12 +09001812
Paul Duffin15f34ef2020-07-20 18:04:44 +01001813 if !Bool(module.sdkLibraryProperties.No_dist) {
Paul Duffin040e9062020-11-23 17:41:36 +00001814 // Dist the api txt and removed api txt artifacts for sdk builds.
1815 distDir := proptools.StringPtr(path.Join(module.apiDistPath(apiScope), "api"))
1816 for _, p := range []struct {
1817 tag string
1818 pattern string
1819 }{
1820 {tag: ".api.txt", pattern: "%s.txt"},
1821 {tag: ".removed-api.txt", pattern: "%s-removed.txt"},
1822 } {
1823 props.Dists = append(props.Dists, android.Dist{
1824 Targets: []string{"sdk", "win_sdk"},
1825 Dir: distDir,
1826 Dest: proptools.StringPtr(fmt.Sprintf(p.pattern, module.distStem())),
1827 Tag: proptools.StringPtr(p.tag),
1828 })
1829 }
Anton Hansson5fd5d242020-03-27 19:43:19 +00001830 }
1831
Spandan Das2cc80ba2023-10-27 17:21:52 +00001832 mctx.CreateModule(DroidstubsFactory, &props, module.sdkComponentPropertiesForChildLibrary()).(*Droidstubs).CallHookIfAvailable(mctx)
Jiyong Parkc678ad32018-04-10 13:07:10 +09001833}
1834
Jihoon Kang0c705a42023-08-02 06:44:57 +00001835func (module *SdkLibrary) createApiLibrary(mctx android.DefaultableHookContext, apiScope *apiScope, alternativeFullApiSurfaceStub string) {
Jihoon Kang1c92c3e2023-03-23 17:44:51 +00001836 props := struct {
Jihoon Kangca198c22023-06-22 23:13:51 +00001837 Name *string
1838 Visibility []string
1839 Api_contributions []string
1840 Libs []string
1841 Static_libs []string
1842 Full_api_surface_stub *string
Jihoon Kang4ec24872023-10-05 17:26:09 +00001843 System_modules *string
Jihoon Kang063ec002023-06-28 01:16:23 +00001844 Enable_validation *bool
Jihoon Kang1c92c3e2023-03-23 17:44:51 +00001845 }{}
1846
1847 props.Name = proptools.StringPtr(module.apiLibraryModuleName(apiScope))
Jihoon Kang786df932023-09-07 01:18:31 +00001848 props.Visibility = []string{"//visibility:override", "//visibility:private"}
Jihoon Kang1c92c3e2023-03-23 17:44:51 +00001849
1850 apiContributions := []string{}
1851
1852 // Api surfaces are not independent of each other, but have subset relationships,
1853 // and so does the api files. To generate from-text stubs for api surfaces other than public,
1854 // all subset api domains' api_contriubtions must be added as well.
1855 scope := apiScope
1856 for scope != nil {
1857 apiContributions = append(apiContributions, module.stubsSourceModuleName(scope)+".api.contribution")
1858 scope = scope.extends
1859 }
Jihoon Kang0c705a42023-08-02 06:44:57 +00001860 if apiScope == apiScopePublic {
1861 additionalApiContribution := module.apiLibraryAdditionalApiContribution()
1862 if additionalApiContribution != "" {
1863 apiContributions = append(apiContributions, additionalApiContribution)
1864 }
1865 }
Jihoon Kang1c92c3e2023-03-23 17:44:51 +00001866
1867 props.Api_contributions = apiContributions
1868 props.Libs = module.properties.Libs
1869 props.Libs = append(props.Libs, module.sdkLibraryProperties.Stub_only_libs...)
Mark White9421c4c2023-08-10 00:07:03 +00001870 props.Libs = append(props.Libs, module.scopeToProperties[apiScope].Libs...)
Jihoon Kang1c92c3e2023-03-23 17:44:51 +00001871 props.Libs = append(props.Libs, "stub-annotations")
1872 props.Static_libs = module.sdkLibraryProperties.Stub_only_static_libs
Jihoon Kange7ee2562023-07-25 05:51:46 +00001873 props.Full_api_surface_stub = proptools.StringPtr(apiScope.kind.DefaultJavaLibraryName())
Jihoon Kang0c705a42023-08-02 06:44:57 +00001874 if alternativeFullApiSurfaceStub != "" {
1875 props.Full_api_surface_stub = proptools.StringPtr(alternativeFullApiSurfaceStub)
1876 }
Jihoon Kang1c92c3e2023-03-23 17:44:51 +00001877
1878 // android_module_lib_stubs_current.from-text only comprises api contributions from art, conscrypt and i18n.
1879 // Thus, replace with android_module_lib_stubs_current_full.from-text, which comprises every api domains.
1880 if apiScope.kind == android.SdkModule {
Jihoon Kangca198c22023-06-22 23:13:51 +00001881 props.Full_api_surface_stub = proptools.StringPtr(apiScope.kind.DefaultJavaLibraryName() + "_full.from-text")
Jihoon Kang1c92c3e2023-03-23 17:44:51 +00001882 }
1883
Jihoon Kangd30ac8a2023-10-09 18:00:17 +00001884 // java_sdk_library modules that set sdk_version as none does not depend on other api
1885 // domains. Therefore, java_api_library created from such modules should not depend on
1886 // full_api_surface_stubs but create and compile stubs by the java_api_library module
1887 // itself.
1888 if module.SdkVersion(mctx).Kind == android.SdkNone {
1889 props.Full_api_surface_stub = nil
1890 }
1891
Jihoon Kang4ec24872023-10-05 17:26:09 +00001892 props.System_modules = module.deviceProperties.System_modules
Jihoon Kang063ec002023-06-28 01:16:23 +00001893 props.Enable_validation = proptools.BoolPtr(true)
Jihoon Kang4ec24872023-10-05 17:26:09 +00001894
Spandan Das2cc80ba2023-10-27 17:21:52 +00001895 mctx.CreateModule(ApiLibraryFactory, &props, module.sdkComponentPropertiesForChildLibrary())
Jihoon Kang1c92c3e2023-03-23 17:44:51 +00001896}
1897
Jihoon Kang1147b312023-06-08 23:25:57 +00001898func (module *SdkLibrary) createTopLevelStubsLibrary(
1899 mctx android.DefaultableHookContext, apiScope *apiScope, contributesToApiSurface bool) {
1900 props := struct {
1901 Name *string
1902 Visibility []string
1903 Sdk_version *string
1904 Static_libs []string
1905 System_modules *string
1906 Dist struct {
1907 Targets []string
1908 Dest *string
1909 Dir *string
1910 Tag *string
1911 }
1912 Compile_dex *bool
1913 }{}
1914 props.Name = proptools.StringPtr(module.stubsLibraryModuleName(apiScope))
1915 props.Visibility = childModuleVisibility(module.sdkLibraryProperties.Stubs_library_visibility)
1916 sdkVersion := module.sdkVersionForStubsLibrary(mctx, apiScope)
1917 props.Sdk_version = proptools.StringPtr(sdkVersion)
1918
1919 // Add the stub compiling java_library/java_api_library as static lib based on build config
1920 staticLib := module.sourceStubLibraryModuleName(apiScope)
1921 if mctx.Config().BuildFromTextStub() && contributesToApiSurface {
1922 staticLib = module.apiLibraryModuleName(apiScope)
1923 }
1924 props.Static_libs = append(props.Static_libs, staticLib)
1925 props.System_modules = module.deviceProperties.System_modules
1926
1927 // Dist the class jar artifact for sdk builds.
1928 if !Bool(module.sdkLibraryProperties.No_dist) {
1929 props.Dist.Targets = []string{"sdk", "win_sdk"}
1930 props.Dist.Dest = proptools.StringPtr(fmt.Sprintf("%v.jar", module.distStem()))
1931 props.Dist.Dir = proptools.StringPtr(module.apiDistPath(apiScope))
1932 props.Dist.Tag = proptools.StringPtr(".jar")
1933 }
1934
1935 // The imports need to be compiled to dex if the java_sdk_library requests it.
1936 compileDex := module.dexProperties.Compile_dex
1937 if module.stubLibrariesCompiledForDex() {
1938 compileDex = proptools.BoolPtr(true)
1939 }
1940 props.Compile_dex = compileDex
1941
1942 mctx.CreateModule(LibraryFactory, &props, module.sdkComponentPropertiesForChildLibrary())
1943}
1944
Paul Duffin958806b2022-05-16 13:10:47 +00001945func (module *SdkLibrary) compareAgainstLatestApi(apiScope *apiScope) bool {
1946 return !(apiScope.unstable || module.sdkLibraryProperties.Unsafe_ignore_missing_latest_api)
1947}
1948
Paul Duffinea8f8082021-06-24 13:25:57 +01001949// Implements android.ApexModule
Jooyung Han5e9013b2020-03-10 06:23:13 +09001950func (module *SdkLibrary) DepIsInSameApex(mctx android.BaseModuleContext, dep android.Module) bool {
1951 depTag := mctx.OtherModuleDependencyTag(dep)
1952 if depTag == xmlPermissionsFileTag {
1953 return true
1954 }
1955 return module.Library.DepIsInSameApex(mctx, dep)
1956}
1957
Paul Duffinea8f8082021-06-24 13:25:57 +01001958// Implements android.ApexModule
1959func (module *SdkLibrary) UniqueApexVariations() bool {
1960 return module.uniqueApexVariations()
1961}
1962
Jiyong Parkc678ad32018-04-10 13:07:10 +09001963// Creates the xml file that publicizes the runtime library
Paul Duffinf0229202020-04-29 16:47:28 +01001964func (module *SdkLibrary) createXmlFile(mctx android.DefaultableHookContext) {
Spandan Das8c9ae7e2023-03-03 21:20:36 +00001965 moduleMinApiLevel := module.Library.MinSdkVersion(mctx)
Pedro Loureiroc3621422021-09-28 15:40:23 +00001966 var moduleMinApiLevelStr = moduleMinApiLevel.String()
1967 if moduleMinApiLevel == android.NoneApiLevel {
1968 moduleMinApiLevelStr = "current"
1969 }
Jiyong Parke3833882020-02-17 17:28:10 +09001970 props := struct {
Pedro Loureiroc3621422021-09-28 15:40:23 +00001971 Name *string
1972 Lib_name *string
1973 Apex_available []string
1974 On_bootclasspath_since *string
1975 On_bootclasspath_before *string
1976 Min_device_sdk *string
1977 Max_device_sdk *string
1978 Sdk_library_min_api_level *string
Jiyong Parke3833882020-02-17 17:28:10 +09001979 }{
Pedro Loureiroc3621422021-09-28 15:40:23 +00001980 Name: proptools.StringPtr(module.xmlPermissionsModuleName()),
1981 Lib_name: proptools.StringPtr(module.BaseModuleName()),
1982 Apex_available: module.ApexProperties.Apex_available,
1983 On_bootclasspath_since: module.commonSdkLibraryProperties.On_bootclasspath_since,
1984 On_bootclasspath_before: module.commonSdkLibraryProperties.On_bootclasspath_before,
1985 Min_device_sdk: module.commonSdkLibraryProperties.Min_device_sdk,
1986 Max_device_sdk: module.commonSdkLibraryProperties.Max_device_sdk,
1987 Sdk_library_min_api_level: &moduleMinApiLevelStr,
Jiyong Parkc678ad32018-04-10 13:07:10 +09001988 }
Jiyong Parke3833882020-02-17 17:28:10 +09001989
Jiyong Parke3833882020-02-17 17:28:10 +09001990 mctx.CreateModule(sdkLibraryXmlFactory, &props)
Jiyong Parkc678ad32018-04-10 13:07:10 +09001991}
1992
Jiyong Parkf1691d22021-03-29 20:11:58 +09001993func PrebuiltJars(ctx android.BaseModuleContext, baseName string, s android.SdkSpec) android.Paths {
Jiyong Park54105c42021-03-31 18:17:53 +09001994 var ver android.ApiLevel
Jiyong Parkf1691d22021-03-29 20:11:58 +09001995 var kind android.SdkKind
1996 if s.UsePrebuilt(ctx) {
Jiyong Park54105c42021-03-31 18:17:53 +09001997 ver = s.ApiLevel
Jiyong Parkf1691d22021-03-29 20:11:58 +09001998 kind = s.Kind
Jiyong Parkc678ad32018-04-10 13:07:10 +09001999 } else {
Jiyong Park6a927c42020-01-21 02:03:43 +09002000 // We don't have prebuilt SDK for the specific sdkVersion.
2001 // Instead of breaking the build, fallback to use "system_current"
Jiyong Park54105c42021-03-31 18:17:53 +09002002 ver = android.FutureApiLevel
Jiyong Parkf1691d22021-03-29 20:11:58 +09002003 kind = android.SdkSystem
Sundong Ahn054b19a2018-10-19 13:46:09 +09002004 }
Jiyong Park6a927c42020-01-21 02:03:43 +09002005
2006 dir := filepath.Join("prebuilts", "sdk", ver.String(), kind.String())
Paul Duffin50061512020-01-21 16:31:05 +00002007 jar := filepath.Join(dir, baseName+".jar")
Sundong Ahn054b19a2018-10-19 13:46:09 +09002008 jarPath := android.ExistentPathForSource(ctx, jar)
Sundong Ahnae418ac2019-02-28 15:01:28 +09002009 if !jarPath.Valid() {
Colin Cross07c88562020-01-07 09:34:44 -08002010 if ctx.Config().AllowMissingDependencies() {
2011 return android.Paths{android.PathForSource(ctx, jar)}
2012 } else {
Jiyong Parkf1691d22021-03-29 20:11:58 +09002013 ctx.PropertyErrorf("sdk_library", "invalid sdk version %q, %q does not exist", s.Raw, jar)
Colin Cross07c88562020-01-07 09:34:44 -08002014 }
Sundong Ahnae418ac2019-02-28 15:01:28 +09002015 return nil
2016 }
Sundong Ahn054b19a2018-10-19 13:46:09 +09002017 return android.Paths{jarPath.Path()}
2018}
2019
Colin Crossaede88c2020-08-11 12:17:01 -07002020// 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 +01002021//
2022// If either this or the other module are on the platform then this will return
2023// false.
Colin Cross56a83212020-09-15 18:30:11 -07002024func withinSameApexesAs(ctx android.BaseModuleContext, other android.Module) bool {
2025 apexInfo := ctx.Provider(android.ApexInfoProvider).(android.ApexInfo)
2026 otherApexInfo := ctx.OtherModuleProvider(other, android.ApexInfoProvider).(android.ApexInfo)
Jiyong Parkab50b072021-05-12 17:13:56 +09002027 return len(otherApexInfo.InApexVariants) > 0 && reflect.DeepEqual(apexInfo.InApexVariants, otherApexInfo.InApexVariants)
Paul Duffin9b879592020-05-26 13:21:35 +01002028}
2029
Jiyong Parkf1691d22021-03-29 20:11:58 +09002030func (module *SdkLibrary) sdkJars(ctx android.BaseModuleContext, sdkVersion android.SdkSpec, headerJars bool) android.Paths {
Jiyong Park932cdfe2020-05-28 00:19:53 +09002031 // If the client doesn't set sdk_version, but if this library prefers stubs over
2032 // the impl library, let's provide the widest API surface possible. To do so,
2033 // force override sdk_version to module_current so that the closest possible API
2034 // surface could be found in selectHeaderJarsForSdkVersion
Jiyong Parkf1691d22021-03-29 20:11:58 +09002035 if module.defaultsToStubs() && !sdkVersion.Specified() {
Jiyong Park92315372021-04-02 08:45:46 +09002036 sdkVersion = android.SdkSpecFrom(ctx, "module_current")
Jiyong Park932cdfe2020-05-28 00:19:53 +09002037 }
Paul Duffind1b3a922020-01-22 11:57:20 +00002038
Paul Duffindaaa3322020-05-26 18:13:57 +01002039 // Only provide access to the implementation library if it is actually built.
2040 if module.requiresRuntimeImplementationLibrary() {
2041 // Check any special cases for java_sdk_library.
2042 //
2043 // Only allow access to the implementation library in the following condition:
2044 // * No sdk_version specified on the referencing module.
Paul Duffin9b879592020-05-26 13:21:35 +01002045 // * The referencing module is in the same apex as this.
Jiyong Parkf1691d22021-03-29 20:11:58 +09002046 if sdkVersion.Kind == android.SdkPrivate || withinSameApexesAs(ctx, module) {
Paul Duffindaaa3322020-05-26 18:13:57 +01002047 if headerJars {
2048 return module.HeaderJars()
2049 } else {
2050 return module.ImplementationJars()
2051 }
Sundong Ahn054b19a2018-10-19 13:46:09 +09002052 }
Jiyong Parkc678ad32018-04-10 13:07:10 +09002053 }
Paul Duffinb05d4292020-05-20 12:19:10 +01002054
Paul Duffin23970f42020-05-20 14:20:02 +01002055 return module.selectHeaderJarsForSdkVersion(ctx, sdkVersion)
Jiyong Parkc678ad32018-04-10 13:07:10 +09002056}
2057
Sundong Ahn241cd372018-07-13 16:16:44 +09002058// to satisfy SdkLibraryDependency interface
Jiyong Parkf1691d22021-03-29 20:11:58 +09002059func (module *SdkLibrary) SdkHeaderJars(ctx android.BaseModuleContext, sdkVersion android.SdkSpec) android.Paths {
Paul Duffind1b3a922020-01-22 11:57:20 +00002060 return module.sdkJars(ctx, sdkVersion, true /*headerJars*/)
2061}
2062
2063// to satisfy SdkLibraryDependency interface
Jiyong Parkf1691d22021-03-29 20:11:58 +09002064func (module *SdkLibrary) SdkImplementationJars(ctx android.BaseModuleContext, sdkVersion android.SdkSpec) android.Paths {
Paul Duffind1b3a922020-01-22 11:57:20 +00002065 return module.sdkJars(ctx, sdkVersion, false /*headerJars*/)
Sundong Ahn241cd372018-07-13 16:16:44 +09002066}
2067
Colin Cross571cccf2019-02-04 11:22:08 -08002068var javaSdkLibrariesKey = android.NewOnceKey("javaSdkLibraries")
2069
Jiyong Park82484c02018-04-23 21:41:26 +09002070func javaSdkLibraries(config android.Config) *[]string {
Colin Cross571cccf2019-02-04 11:22:08 -08002071 return config.Once(javaSdkLibrariesKey, func() interface{} {
Jiyong Park82484c02018-04-23 21:41:26 +09002072 return &[]string{}
2073 }).(*[]string)
2074}
2075
Paul Duffin749f98f2019-12-30 17:23:46 +00002076func (module *SdkLibrary) getApiDir() string {
2077 return proptools.StringDefault(module.sdkLibraryProperties.Api_dir, "api")
2078}
2079
Jiyong Parkc678ad32018-04-10 13:07:10 +09002080// For a java_sdk_library module, create internal modules for stubs, docs,
2081// runtime libs and xml file. If requested, the stubs and docs are created twice
2082// once for public API level and once for system API level
Paul Duffinf0229202020-04-29 16:47:28 +01002083func (module *SdkLibrary) CreateInternalModules(mctx android.DefaultableHookContext) {
2084 // If the module has been disabled then don't create any child modules.
2085 if !module.Enabled() {
2086 return
2087 }
2088
Paul Duffina18abc22020-05-16 18:54:24 +01002089 if len(module.properties.Srcs) == 0 {
Inseob Kimc0907f12019-02-08 21:00:45 +09002090 mctx.PropertyErrorf("srcs", "java_sdk_library must specify srcs")
Jooyung Han58f26ab2019-12-18 15:34:32 +09002091 return
Inseob Kimc0907f12019-02-08 21:00:45 +09002092 }
2093
Paul Duffin37e0b772019-12-30 17:20:10 +00002094 // If this builds against standard libraries (i.e. is not part of the core libraries)
Paul Duffin4f5c1ef2020-11-19 14:53:43 +00002095 // then assume it provides both system and test apis.
Jiyong Parkf1691d22021-03-29 20:11:58 +09002096 sdkDep := decodeSdkDep(mctx, android.SdkContext(&module.Library))
Paul Duffin37e0b772019-12-30 17:20:10 +00002097 hasSystemAndTestApis := sdkDep.hasStandardLibs()
Paul Duffin3375e352020-04-28 10:44:03 +01002098 module.sdkLibraryProperties.Generate_system_and_test_apis = hasSystemAndTestApis
Paul Duffin4f5c1ef2020-11-19 14:53:43 +00002099
Jaewoong Jung18aefc12020-12-21 09:11:10 -08002100 missingCurrentApi := false
Inseob Kim8098faa2019-03-18 10:19:51 +09002101
Paul Duffin3375e352020-04-28 10:44:03 +01002102 generatedScopes := module.getGeneratedApiScopes(mctx)
Paul Duffind1b3a922020-01-22 11:57:20 +00002103
Paul Duffin749f98f2019-12-30 17:23:46 +00002104 apiDir := module.getApiDir()
Paul Duffin3375e352020-04-28 10:44:03 +01002105 for _, scope := range generatedScopes {
Inseob Kim8098faa2019-03-18 10:19:51 +09002106 for _, api := range []string{"current.txt", "removed.txt"} {
Paul Duffind1b3a922020-01-22 11:57:20 +00002107 path := path.Join(mctx.ModuleDir(), apiDir, scope.apiFilePrefix+api)
Inseob Kim8098faa2019-03-18 10:19:51 +09002108 p := android.ExistentPathForSource(mctx, path)
2109 if !p.Valid() {
Colin Cross18f840c2021-05-20 17:56:54 -07002110 if mctx.Config().AllowMissingDependencies() {
2111 mctx.AddMissingDependencies([]string{path})
2112 } else {
2113 mctx.ModuleErrorf("Current api file %#v doesn't exist", path)
2114 missingCurrentApi = true
2115 }
Inseob Kim8098faa2019-03-18 10:19:51 +09002116 }
2117 }
2118 }
2119
Jaewoong Jung18aefc12020-12-21 09:11:10 -08002120 if missingCurrentApi {
Inseob Kim8098faa2019-03-18 10:19:51 +09002121 script := "build/soong/scripts/gen-java-current-api-files.sh"
2122 p := android.ExistentPathForSource(mctx, script)
2123
2124 if !p.Valid() {
2125 panic(fmt.Sprintf("script file %s doesn't exist", script))
2126 }
2127
2128 mctx.ModuleErrorf("One or more current api files are missing. "+
2129 "You can update them by:\n"+
Paul Duffin37e0b772019-12-30 17:20:10 +00002130 "%s %q %s && m update-api",
Paul Duffind1b3a922020-01-22 11:57:20 +00002131 script, filepath.Join(mctx.ModuleDir(), apiDir),
Paul Duffin3375e352020-04-28 10:44:03 +01002132 strings.Join(generatedScopes.Strings(func(s *apiScope) string { return s.apiFilePrefix }), " "))
Inseob Kim8098faa2019-03-18 10:19:51 +09002133 return
2134 }
2135
Paul Duffin3375e352020-04-28 10:44:03 +01002136 for _, scope := range generatedScopes {
Paul Duffin15f34ef2020-07-20 18:04:44 +01002137 // Use the stubs source name for legacy reasons.
2138 module.createStubsSourcesAndApi(mctx, scope, module.stubsSourceModuleName(scope), scope.droidstubsArgs)
Paul Duffin0ff08bd2020-04-29 13:30:54 +01002139
Paul Duffind1b3a922020-01-22 11:57:20 +00002140 module.createStubsLibrary(mctx, scope)
Jihoon Kang1c92c3e2023-03-23 17:44:51 +00002141
Jihoon Kang0c705a42023-08-02 06:44:57 +00002142 alternativeFullApiSurfaceStubLib := ""
2143 if scope == apiScopePublic {
2144 alternativeFullApiSurfaceStubLib = module.alternativeFullApiSurfaceStubLib()
2145 }
2146 contributesToApiSurface := module.contributesToApiSurface(mctx.Config()) || alternativeFullApiSurfaceStubLib != ""
Jihoon Kang1147b312023-06-08 23:25:57 +00002147 if contributesToApiSurface {
Jihoon Kang0c705a42023-08-02 06:44:57 +00002148 module.createApiLibrary(mctx, scope, alternativeFullApiSurfaceStubLib)
Jihoon Kang1c92c3e2023-03-23 17:44:51 +00002149 }
Jihoon Kang1147b312023-06-08 23:25:57 +00002150
2151 module.createTopLevelStubsLibrary(mctx, scope, contributesToApiSurface)
Inseob Kimc0907f12019-02-08 21:00:45 +09002152 }
2153
Paul Duffindfa131e2020-05-15 20:37:11 +01002154 if module.requiresRuntimeImplementationLibrary() {
Paul Duffin5df79302020-05-16 15:52:12 +01002155 // Create child module to create an implementation library.
2156 //
2157 // This temporarily creates a second implementation library that can be explicitly
2158 // referenced.
2159 //
2160 // TODO(b/156618935) - update comment once only one implementation library is created.
2161 module.createImplLibrary(mctx)
2162
Paul Duffindfa131e2020-05-15 20:37:11 +01002163 // Only create an XML permissions file that declares the library as being usable
2164 // as a shared library if required.
2165 if module.sharedLibrary() {
2166 module.createXmlFile(mctx)
2167 }
Paul Duffin43db9be2019-12-30 17:35:49 +00002168
2169 // record java_sdk_library modules so that they are exported to make
2170 javaSdkLibraries := javaSdkLibraries(mctx.Config())
2171 javaSdkLibrariesLock.Lock()
2172 defer javaSdkLibrariesLock.Unlock()
2173 *javaSdkLibraries = append(*javaSdkLibraries, module.BaseModuleName())
2174 }
Anton Hansson7f66efa2020-10-08 14:47:23 +01002175
Paul Duffin77590a82022-04-28 14:13:30 +00002176 // 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 +01002177 module.properties.Libs = append(module.properties.Libs, module.sdkLibraryProperties.Impl_only_libs...)
Paul Duffin77590a82022-04-28 14:13:30 +00002178 module.properties.Static_libs = append(module.properties.Static_libs, module.sdkLibraryProperties.Impl_only_static_libs...)
Inseob Kimc0907f12019-02-08 21:00:45 +09002179}
2180
2181func (module *SdkLibrary) InitSdkLibraryProperties() {
Colin Crossce6734e2020-06-15 16:09:53 -07002182 module.addHostAndDeviceProperties()
2183 module.AddProperties(&module.sdkLibraryProperties)
Sundong Ahn054b19a2018-10-19 13:46:09 +09002184
Paul Duffin71b33cc2021-06-23 11:39:47 +01002185 module.initSdkLibraryComponent(module)
Paul Duffin859fe962020-05-15 10:20:31 +01002186
Paul Duffina18abc22020-05-16 18:54:24 +01002187 module.properties.Installable = proptools.BoolPtr(true)
2188 module.deviceProperties.IsSDKLibrary = true
Inseob Kimc0907f12019-02-08 21:00:45 +09002189}
Sundong Ahn054b19a2018-10-19 13:46:09 +09002190
Paul Duffindfa131e2020-05-15 20:37:11 +01002191func (module *SdkLibrary) requiresRuntimeImplementationLibrary() bool {
2192 return !proptools.Bool(module.sdkLibraryProperties.Api_only)
2193}
2194
Jiyong Park932cdfe2020-05-28 00:19:53 +09002195func (module *SdkLibrary) defaultsToStubs() bool {
2196 return proptools.Bool(module.sdkLibraryProperties.Default_to_stubs)
2197}
2198
Paul Duffin1b1e8062020-05-08 13:44:43 +01002199// Defines how to name the individual component modules the sdk library creates.
2200type sdkLibraryComponentNamingScheme interface {
2201 stubsLibraryModuleName(scope *apiScope, baseName string) string
2202
2203 stubsSourceModuleName(scope *apiScope, baseName string) string
Jihoon Kang1c92c3e2023-03-23 17:44:51 +00002204
2205 apiLibraryModuleName(scope *apiScope, baseName string) string
Jihoon Kang1147b312023-06-08 23:25:57 +00002206
2207 sourceStubLibraryModuleName(scope *apiScope, baseName string) string
Paul Duffin1b1e8062020-05-08 13:44:43 +01002208}
2209
2210type defaultNamingScheme struct {
2211}
2212
2213func (s *defaultNamingScheme) stubsLibraryModuleName(scope *apiScope, baseName string) string {
2214 return scope.stubsLibraryModuleName(baseName)
2215}
2216
2217func (s *defaultNamingScheme) stubsSourceModuleName(scope *apiScope, baseName string) string {
2218 return scope.stubsSourceModuleName(baseName)
2219}
2220
Jihoon Kang1c92c3e2023-03-23 17:44:51 +00002221func (s *defaultNamingScheme) apiLibraryModuleName(scope *apiScope, baseName string) string {
2222 return scope.apiLibraryModuleName(baseName)
2223}
2224
Jihoon Kang1147b312023-06-08 23:25:57 +00002225func (s *defaultNamingScheme) sourceStubLibraryModuleName(scope *apiScope, baseName string) string {
2226 return scope.sourceStubLibraryModuleName(baseName)
2227}
2228
Paul Duffin1b1e8062020-05-08 13:44:43 +01002229var _ sdkLibraryComponentNamingScheme = (*defaultNamingScheme)(nil)
2230
Jaewoong Jungbc15e3a2021-03-10 17:02:43 -08002231func moduleStubLinkType(name string) (stub bool, ret sdkLinkType) {
Jihoon Kang1147b312023-06-08 23:25:57 +00002232 name = strings.TrimSuffix(name, ".from-source")
2233
Anton Hansson2d0c1942020-05-25 12:20:51 +01002234 // This suffix-based approach is fragile and could potentially mis-trigger.
2235 // TODO(b/155164730): Clean this up when modules no longer reference sdk_lib stubs directly.
Anton Hansson08f476b2021-04-07 15:32:19 +01002236 if strings.HasSuffix(name, apiScopePublic.stubsLibraryModuleNameSuffix()) {
2237 if name == "hwbinder.stubs" || name == "libcore_private.stubs" {
2238 // Due to a previous bug, these modules were not considered stubs, so we retain that.
2239 return false, javaPlatform
2240 }
Anton Hansson2d0c1942020-05-25 12:20:51 +01002241 return true, javaSdk
2242 }
Anton Hansson08f476b2021-04-07 15:32:19 +01002243 if strings.HasSuffix(name, apiScopeSystem.stubsLibraryModuleNameSuffix()) {
Anton Hansson2d0c1942020-05-25 12:20:51 +01002244 return true, javaSystem
2245 }
Anton Hansson08f476b2021-04-07 15:32:19 +01002246 if strings.HasSuffix(name, apiScopeModuleLib.stubsLibraryModuleNameSuffix()) {
Anton Hansson2d0c1942020-05-25 12:20:51 +01002247 return true, javaModule
2248 }
Anton Hansson08f476b2021-04-07 15:32:19 +01002249 if strings.HasSuffix(name, apiScopeTest.stubsLibraryModuleNameSuffix()) {
Anton Hansson2d0c1942020-05-25 12:20:51 +01002250 return true, javaSystem
2251 }
Jihoon Kang1147b312023-06-08 23:25:57 +00002252 if strings.HasSuffix(name, apiScopeSystemServer.stubsLibraryModuleNameSuffix()) {
2253 return true, javaSystemServer
2254 }
Anton Hansson2d0c1942020-05-25 12:20:51 +01002255 return false, javaPlatform
2256}
2257
Jaewoong Jung4f158ee2019-07-11 10:05:35 -07002258// java_sdk_library is a special Java library that provides optional platform APIs to apps.
2259// In practice, it can be viewed as a combination of several modules: 1) stubs library that clients
2260// are linked against to, 2) droiddoc module that internally generates API stubs source files,
2261// 3) the real runtime shared library that implements the APIs, and 4) XML file for adding
2262// the runtime lib to the classpath at runtime if requested via <uses-library>.
Inseob Kimc0907f12019-02-08 21:00:45 +09002263func SdkLibraryFactory() android.Module {
2264 module := &SdkLibrary{}
Paul Duffinc3091c82020-05-08 14:16:20 +01002265
2266 // Initialize information common between source and prebuilt.
Paul Duffin71b33cc2021-06-23 11:39:47 +01002267 module.initCommon(module)
Paul Duffinc3091c82020-05-08 14:16:20 +01002268
Inseob Kimc0907f12019-02-08 21:00:45 +09002269 module.InitSdkLibraryProperties()
Jooyung Han58f26ab2019-12-18 15:34:32 +09002270 android.InitApexModule(module)
Sundong Ahn054b19a2018-10-19 13:46:09 +09002271 InitJavaModule(module, android.HostAndDeviceSupported)
Paul Duffin3375e352020-04-28 10:44:03 +01002272
2273 // Initialize the map from scope to scope specific properties.
2274 scopeToProperties := make(map[*apiScope]*ApiScopeProperties)
2275 for _, scope := range allApiScopes {
2276 scopeToProperties[scope] = scope.scopeSpecificProperties(module)
2277 }
2278 module.scopeToProperties = scopeToProperties
2279
Paul Duffin4911a892020-04-29 23:35:13 +01002280 // Add the properties containing visibility rules so that they are checked.
Paul Duffin5df79302020-05-16 15:52:12 +01002281 android.AddVisibilityProperty(module, "impl_library_visibility", &module.sdkLibraryProperties.Impl_library_visibility)
Paul Duffin4911a892020-04-29 23:35:13 +01002282 android.AddVisibilityProperty(module, "stubs_library_visibility", &module.sdkLibraryProperties.Stubs_library_visibility)
2283 android.AddVisibilityProperty(module, "stubs_source_visibility", &module.sdkLibraryProperties.Stubs_source_visibility)
2284
Paul Duffin1b1e8062020-05-08 13:44:43 +01002285 module.SetDefaultableHook(func(ctx android.DefaultableHookContext) {
Paul Duffindfa131e2020-05-15 20:37:11 +01002286 // If no implementation is required then it cannot be used as a shared library
2287 // either.
2288 if !module.requiresRuntimeImplementationLibrary() {
2289 // If shared_library has been explicitly set to true then it is incompatible
2290 // with api_only: true.
2291 if proptools.Bool(module.commonSdkLibraryProperties.Shared_library) {
2292 ctx.PropertyErrorf("api_only/shared_library", "inconsistent settings, shared_library and api_only cannot both be true")
2293 }
2294 // Set shared_library: false.
2295 module.commonSdkLibraryProperties.Shared_library = proptools.BoolPtr(false)
2296 }
2297
Paul Duffin1b1e8062020-05-08 13:44:43 +01002298 if module.initCommonAfterDefaultsApplied(ctx) {
2299 module.CreateInternalModules(ctx)
2300 }
2301 })
Zi Wangb2179e32023-01-31 15:53:30 -08002302 android.InitBazelModule(module)
Jiyong Parkc678ad32018-04-10 13:07:10 +09002303 return module
2304}
Colin Cross79c7c262019-04-17 11:11:46 -07002305
Zi Wangb2179e32023-01-31 15:53:30 -08002306type bazelSdkLibraryAttributes struct {
Liz Kammer9a97a8f2023-09-18 13:36:41 -04002307 Public *bazel.Label
2308 System *bazel.Label
2309 Test *bazel.Label
2310 Module_lib *bazel.Label
2311 System_server *bazel.Label
Zi Wangb2179e32023-01-31 15:53:30 -08002312}
2313
2314// java_sdk_library bp2build converter
Chris Parsons637458d2023-09-19 20:09:00 +00002315func (module *SdkLibrary) ConvertWithBp2build(ctx android.Bp2buildMutatorContext) {
Zi Wangb2179e32023-01-31 15:53:30 -08002316 if ctx.ModuleType() != "java_sdk_library" {
Chris Parsons39a16972023-06-08 14:28:51 +00002317 ctx.MarkBp2buildUnconvertible(bp2build_metrics_proto.UnconvertedReasonType_TYPE_UNSUPPORTED, "")
Zi Wangb2179e32023-01-31 15:53:30 -08002318 return
2319 }
2320
Liz Kammer9a97a8f2023-09-18 13:36:41 -04002321 nameToAttr := make(map[string]*bazel.Label)
Zi Wangb2179e32023-01-31 15:53:30 -08002322
2323 for _, scope := range module.getGeneratedApiScopes(ctx) {
Liz Kammer9a97a8f2023-09-18 13:36:41 -04002324 apiSurfaceFile := android.BazelLabelForModuleSrcSingle(ctx, path.Join(module.getApiDir(), scope.apiFilePrefix+"current.txt"))
2325 nameToAttr[scope.name] = &apiSurfaceFile
Zi Wangb2179e32023-01-31 15:53:30 -08002326 }
2327
2328 attrs := bazelSdkLibraryAttributes{
2329 Public: nameToAttr["public"],
2330 System: nameToAttr["system"],
2331 Test: nameToAttr["test"],
2332 Module_lib: nameToAttr["module-lib"],
2333 System_server: nameToAttr["system-server"],
2334 }
2335 props := bazel.BazelTargetModuleProperties{
2336 Rule_class: "java_sdk_library",
2337 Bzl_load_location: "//build/bazel/rules/java:sdk_library.bzl",
2338 }
2339
2340 ctx.CreateBazelTargetModule(props, android.CommonAttributes{Name: module.Name()}, &attrs)
2341}
2342
Colin Cross79c7c262019-04-17 11:11:46 -07002343//
2344// SDK library prebuilts
2345//
2346
Paul Duffin56d44902020-01-31 13:36:25 +00002347// Properties associated with each api scope.
2348type sdkLibraryScopeProperties struct {
Colin Cross79c7c262019-04-17 11:11:46 -07002349 Jars []string `android:"path"`
2350
2351 Sdk_version *string
2352
Colin Cross79c7c262019-04-17 11:11:46 -07002353 // List of shared java libs that this module has dependencies to
2354 Libs []string
Paul Duffin3d1248c2020-04-09 00:10:17 +01002355
Paul Duffinc8782502020-04-29 20:45:27 +01002356 // The stubs source.
Paul Duffin3d1248c2020-04-09 00:10:17 +01002357 Stub_srcs []string `android:"path"`
Paul Duffin1fd005d2020-04-09 01:08:11 +01002358
2359 // The current.txt
Paul Duffin0f8faff2020-05-20 16:18:00 +01002360 Current_api *string `android:"path"`
Paul Duffin1fd005d2020-04-09 01:08:11 +01002361
2362 // The removed.txt
Paul Duffin0f8faff2020-05-20 16:18:00 +01002363 Removed_api *string `android:"path"`
Anton Hanssond78eb762021-09-21 15:25:12 +01002364
2365 // Annotation zip
2366 Annotations *string `android:"path"`
Colin Cross79c7c262019-04-17 11:11:46 -07002367}
2368
Paul Duffin56d44902020-01-31 13:36:25 +00002369type sdkLibraryImportProperties struct {
Paul Duffinfcfd7912020-01-31 17:54:30 +00002370 // List of shared java libs, common to all scopes, that this module has
2371 // dependencies to
2372 Libs []string
Paul Duffin1267d872021-04-16 17:21:36 +01002373
2374 // If set to true, compile dex files for the stubs. Defaults to false.
2375 Compile_dex *bool
Paul Duffin869de142021-07-15 14:14:41 +01002376
2377 // If not empty, classes are restricted to the specified packages and their sub-packages.
Paul Duffin869de142021-07-15 14:14:41 +01002378 Permitted_packages []string
Paul Duffin56d44902020-01-31 13:36:25 +00002379}
2380
Paul Duffineedc5d52020-06-12 17:46:39 +01002381type SdkLibraryImport struct {
Colin Cross79c7c262019-04-17 11:11:46 -07002382 android.ModuleBase
2383 android.DefaultableModuleBase
Liz Kammer9a97a8f2023-09-18 13:36:41 -04002384 android.BazelModuleBase
Colin Cross79c7c262019-04-17 11:11:46 -07002385 prebuilt android.Prebuilt
Paul Duffindd46f712020-02-10 13:37:10 +00002386 android.ApexModuleBase
Colin Cross79c7c262019-04-17 11:11:46 -07002387
Paul Duffin37856732021-02-26 14:24:15 +00002388 hiddenAPI
Jiakai Zhang204356f2021-09-09 08:12:46 +00002389 dexpreopter
Paul Duffin37856732021-02-26 14:24:15 +00002390
Colin Cross79c7c262019-04-17 11:11:46 -07002391 properties sdkLibraryImportProperties
2392
Paul Duffin46a26a82020-04-07 19:27:04 +01002393 // Map from api scope to the scope specific property structure.
2394 scopeProperties map[*apiScope]*sdkLibraryScopeProperties
2395
Paul Duffin56d44902020-01-31 13:36:25 +00002396 commonToSdkLibraryAndImport
Paul Duffineedc5d52020-06-12 17:46:39 +01002397
2398 // The reference to the implementation library created by the source module.
2399 // Is nil if the source module does not exist.
2400 implLibraryModule *Library
2401
2402 // The reference to the xml permissions module created by the source module.
2403 // Is nil if the source module does not exist.
2404 xmlPermissionsFileModule *sdkLibraryXml
Paul Duffin39853512021-02-26 11:09:39 +00002405
Jeongik Chad5fe8782021-07-08 01:13:11 +09002406 // Build path to the dex implementation jar obtained from the prebuilt_apex, if any.
Martin Stjernholm8be1e6d2021-09-15 03:34:04 +01002407 dexJarFile OptionalDexJarPath
Jeongik Chad5fe8782021-07-08 01:13:11 +09002408
2409 // Expected install file path of the source module(sdk_library)
2410 // or dex implementation jar obtained from the prebuilt_apex, if any.
2411 installFile android.Path
Colin Cross79c7c262019-04-17 11:11:46 -07002412}
2413
Paul Duffineedc5d52020-06-12 17:46:39 +01002414var _ SdkLibraryDependency = (*SdkLibraryImport)(nil)
Colin Cross79c7c262019-04-17 11:11:46 -07002415
Paul Duffin46a26a82020-04-07 19:27:04 +01002416// The type of a structure that contains a field of type sdkLibraryScopeProperties
2417// for each apiscope in allApiScopes, e.g. something like:
Colin Crossd079e0b2022-08-16 10:27:33 -07002418//
2419// struct {
2420// Public sdkLibraryScopeProperties
2421// System sdkLibraryScopeProperties
2422// ...
2423// }
Paul Duffin46a26a82020-04-07 19:27:04 +01002424var allScopeStructType = createAllScopePropertiesStructType()
2425
2426// Dynamically create a structure type for each apiscope in allApiScopes.
2427func createAllScopePropertiesStructType() reflect.Type {
2428 var fields []reflect.StructField
2429 for _, apiScope := range allApiScopes {
2430 field := reflect.StructField{
2431 Name: apiScope.fieldName,
2432 Type: reflect.TypeOf(sdkLibraryScopeProperties{}),
2433 }
2434 fields = append(fields, field)
2435 }
2436
2437 return reflect.StructOf(fields)
2438}
2439
2440// Create an instance of the scope specific structure type and return a map
2441// from apiscope to a pointer to each scope specific field.
2442func createPropertiesInstance() (interface{}, map[*apiScope]*sdkLibraryScopeProperties) {
2443 allScopePropertiesPtr := reflect.New(allScopeStructType)
2444 allScopePropertiesStruct := allScopePropertiesPtr.Elem()
2445 scopeProperties := make(map[*apiScope]*sdkLibraryScopeProperties)
2446
2447 for _, apiScope := range allApiScopes {
2448 field := allScopePropertiesStruct.FieldByName(apiScope.fieldName)
2449 scopeProperties[apiScope] = field.Addr().Interface().(*sdkLibraryScopeProperties)
2450 }
2451
2452 return allScopePropertiesPtr.Interface(), scopeProperties
2453}
2454
Jaewoong Jung4f158ee2019-07-11 10:05:35 -07002455// java_sdk_library_import imports a prebuilt java_sdk_library.
Colin Cross79c7c262019-04-17 11:11:46 -07002456func sdkLibraryImportFactory() android.Module {
Paul Duffineedc5d52020-06-12 17:46:39 +01002457 module := &SdkLibraryImport{}
Colin Cross79c7c262019-04-17 11:11:46 -07002458
Paul Duffin46a26a82020-04-07 19:27:04 +01002459 allScopeProperties, scopeToProperties := createPropertiesInstance()
2460 module.scopeProperties = scopeToProperties
Jiakai Zhang9c4dc192023-02-09 00:09:24 +08002461 module.AddProperties(&module.properties, allScopeProperties, &module.importDexpreoptProperties)
Colin Cross79c7c262019-04-17 11:11:46 -07002462
Paul Duffinc3091c82020-05-08 14:16:20 +01002463 // Initialize information common between source and prebuilt.
Paul Duffin71b33cc2021-06-23 11:39:47 +01002464 module.initCommon(module)
Paul Duffinc3091c82020-05-08 14:16:20 +01002465
Paul Duffin0bdcb272020-02-06 15:24:57 +00002466 android.InitPrebuiltModule(module, &[]string{""})
Paul Duffindd46f712020-02-10 13:37:10 +00002467 android.InitApexModule(module)
Liz Kammer9a97a8f2023-09-18 13:36:41 -04002468 android.InitBazelModule(module)
Colin Cross79c7c262019-04-17 11:11:46 -07002469 InitJavaModule(module, android.HostAndDeviceSupported)
2470
Paul Duffin1b1e8062020-05-08 13:44:43 +01002471 module.SetDefaultableHook(func(mctx android.DefaultableHookContext) {
2472 if module.initCommonAfterDefaultsApplied(mctx) {
2473 module.createInternalModules(mctx)
2474 }
2475 })
Colin Cross79c7c262019-04-17 11:11:46 -07002476 return module
2477}
2478
Liz Kammer9a97a8f2023-09-18 13:36:41 -04002479// java_sdk_library bp2build converter
Chris Parsons637458d2023-09-19 20:09:00 +00002480func (i *SdkLibraryImport) ConvertWithBp2build(ctx android.Bp2buildMutatorContext) {
Liz Kammer9a97a8f2023-09-18 13:36:41 -04002481 nameToAttr := make(map[string]*bazel.Label)
2482
2483 for scope, props := range i.scopeProperties {
2484 if api := proptools.String(props.Current_api); api != "" {
2485 apiSurfaceFile := android.BazelLabelForModuleSrcSingle(ctx, api)
2486 nameToAttr[scope.name] = &apiSurfaceFile
2487 }
2488 }
2489
2490 attrs := bazelSdkLibraryAttributes{
2491 Public: nameToAttr["public"],
2492 System: nameToAttr["system"],
2493 Test: nameToAttr["test"],
2494 Module_lib: nameToAttr["module-lib"],
2495 System_server: nameToAttr["system-server"],
2496 }
2497 props := bazel.BazelTargetModuleProperties{
2498 Rule_class: "java_sdk_library",
2499 Bzl_load_location: "//build/bazel/rules/java:sdk_library.bzl",
2500 }
2501
2502 name := android.RemoveOptionalPrebuiltPrefix(i.Name())
2503 ctx.CreateBazelTargetModule(props, android.CommonAttributes{Name: name}, &attrs)
2504}
2505
Paul Duffin630b11e2021-07-15 13:35:26 +01002506var _ PermittedPackagesForUpdatableBootJars = (*SdkLibraryImport)(nil)
2507
2508func (module *SdkLibraryImport) PermittedPackagesForUpdatableBootJars() []string {
2509 return module.properties.Permitted_packages
2510}
2511
Paul Duffineedc5d52020-06-12 17:46:39 +01002512func (module *SdkLibraryImport) Prebuilt() *android.Prebuilt {
Colin Cross79c7c262019-04-17 11:11:46 -07002513 return &module.prebuilt
2514}
2515
Paul Duffineedc5d52020-06-12 17:46:39 +01002516func (module *SdkLibraryImport) Name() string {
Colin Cross79c7c262019-04-17 11:11:46 -07002517 return module.prebuilt.Name(module.ModuleBase.Name())
2518}
2519
Paul Duffineedc5d52020-06-12 17:46:39 +01002520func (module *SdkLibraryImport) createInternalModules(mctx android.DefaultableHookContext) {
Colin Cross79c7c262019-04-17 11:11:46 -07002521
Paul Duffin50061512020-01-21 16:31:05 +00002522 // If the build is configured to use prebuilts then force this to be preferred.
Jeongik Cha816a23a2020-07-08 01:09:23 +09002523 if mctx.Config().AlwaysUsePrebuiltSdks() {
Paul Duffin50061512020-01-21 16:31:05 +00002524 module.prebuilt.ForcePrefer()
2525 }
2526
Paul Duffin46a26a82020-04-07 19:27:04 +01002527 for apiScope, scopeProperties := range module.scopeProperties {
Paul Duffin56d44902020-01-31 13:36:25 +00002528 if len(scopeProperties.Jars) == 0 {
2529 continue
2530 }
2531
Paul Duffinbbb546b2020-04-09 00:07:11 +01002532 module.createJavaImportForStubs(mctx, apiScope, scopeProperties)
Paul Duffin3d1248c2020-04-09 00:10:17 +01002533
Paul Duffin0f8faff2020-05-20 16:18:00 +01002534 if len(scopeProperties.Stub_srcs) > 0 {
2535 module.createPrebuiltStubsSources(mctx, apiScope, scopeProperties)
2536 }
Jihoon Kang71c86832023-09-13 01:01:53 +00002537
2538 if scopeProperties.Current_api != nil {
2539 module.createPrebuiltApiContribution(mctx, apiScope, scopeProperties)
2540 }
Paul Duffin56d44902020-01-31 13:36:25 +00002541 }
Colin Cross79c7c262019-04-17 11:11:46 -07002542
2543 javaSdkLibraries := javaSdkLibraries(mctx.Config())
2544 javaSdkLibrariesLock.Lock()
2545 defer javaSdkLibrariesLock.Unlock()
2546 *javaSdkLibraries = append(*javaSdkLibraries, module.BaseModuleName())
2547}
2548
Paul Duffineedc5d52020-06-12 17:46:39 +01002549func (module *SdkLibraryImport) createJavaImportForStubs(mctx android.DefaultableHookContext, apiScope *apiScope, scopeProperties *sdkLibraryScopeProperties) {
Paul Duffinbbb546b2020-04-09 00:07:11 +01002550 // Creates a java import for the jar with ".stubs" suffix
2551 props := struct {
Paul Duffin1dbe3ca2020-05-16 09:57:59 +01002552 Name *string
2553 Sdk_version *string
2554 Libs []string
2555 Jars []string
Paul Duffin1267d872021-04-16 17:21:36 +01002556 Compile_dex *bool
Paul Duffinbf4de042022-09-27 12:41:52 +01002557
2558 android.UserSuppliedPrebuiltProperties
Paul Duffinbbb546b2020-04-09 00:07:11 +01002559 }{}
Paul Duffinc3091c82020-05-08 14:16:20 +01002560 props.Name = proptools.StringPtr(module.stubsLibraryModuleName(apiScope))
Paul Duffinbbb546b2020-04-09 00:07:11 +01002561 props.Sdk_version = scopeProperties.Sdk_version
2562 // Prepend any of the libs from the legacy public properties to the libs for each of the
2563 // scopes to avoid having to duplicate them in each scope.
2564 props.Libs = append(module.properties.Libs, scopeProperties.Libs...)
2565 props.Jars = scopeProperties.Jars
Paul Duffin1dbe3ca2020-05-16 09:57:59 +01002566
Paul Duffin38b57852020-05-13 16:08:09 +01002567 // The imports are preferred if the java_sdk_library_import is preferred.
Paul Duffinbf4de042022-09-27 12:41:52 +01002568 props.CopyUserSuppliedPropertiesFromPrebuilt(&module.prebuilt)
Paul Duffin859fe962020-05-15 10:20:31 +01002569
Paul Duffin1267d872021-04-16 17:21:36 +01002570 // The imports need to be compiled to dex if the java_sdk_library_import requests it.
Paul Duffinf4600f62021-05-13 22:34:45 +01002571 compileDex := module.properties.Compile_dex
2572 if module.stubLibrariesCompiledForDex() {
2573 compileDex = proptools.BoolPtr(true)
2574 }
2575 props.Compile_dex = compileDex
Paul Duffin1267d872021-04-16 17:21:36 +01002576
Paul Duffin859fe962020-05-15 10:20:31 +01002577 mctx.CreateModule(ImportFactory, &props, module.sdkComponentPropertiesForChildLibrary())
Paul Duffinbbb546b2020-04-09 00:07:11 +01002578}
2579
Paul Duffineedc5d52020-06-12 17:46:39 +01002580func (module *SdkLibraryImport) createPrebuiltStubsSources(mctx android.DefaultableHookContext, apiScope *apiScope, scopeProperties *sdkLibraryScopeProperties) {
Paul Duffin3d1248c2020-04-09 00:10:17 +01002581 props := struct {
Paul Duffinbf4de042022-09-27 12:41:52 +01002582 Name *string
2583 Srcs []string
2584
2585 android.UserSuppliedPrebuiltProperties
Paul Duffin3d1248c2020-04-09 00:10:17 +01002586 }{}
Paul Duffinc3091c82020-05-08 14:16:20 +01002587 props.Name = proptools.StringPtr(module.stubsSourceModuleName(apiScope))
Paul Duffin3d1248c2020-04-09 00:10:17 +01002588 props.Srcs = scopeProperties.Stub_srcs
Paul Duffin38b57852020-05-13 16:08:09 +01002589
2590 // The stubs source is preferred if the java_sdk_library_import is preferred.
Paul Duffinbf4de042022-09-27 12:41:52 +01002591 props.CopyUserSuppliedPropertiesFromPrebuilt(&module.prebuilt)
2592
Spandan Das2cc80ba2023-10-27 17:21:52 +00002593 mctx.CreateModule(PrebuiltStubsSourcesFactory, &props, module.sdkComponentPropertiesForChildLibrary())
Paul Duffin3d1248c2020-04-09 00:10:17 +01002594}
2595
Jihoon Kang71c86832023-09-13 01:01:53 +00002596func (module *SdkLibraryImport) createPrebuiltApiContribution(mctx android.DefaultableHookContext, apiScope *apiScope, scopeProperties *sdkLibraryScopeProperties) {
2597 api_file := scopeProperties.Current_api
2598 api_surface := &apiScope.name
2599
2600 props := struct {
2601 Name *string
2602 Api_surface *string
2603 Api_file *string
2604 Visibility []string
2605 }{}
2606
2607 props.Name = proptools.StringPtr(module.stubsSourceModuleName(apiScope) + ".api.contribution")
2608 props.Api_surface = api_surface
2609 props.Api_file = api_file
2610 props.Visibility = []string{"//visibility:override", "//visibility:public"}
2611
Spandan Das2cc80ba2023-10-27 17:21:52 +00002612 mctx.CreateModule(ApiContributionImportFactory, &props, module.sdkComponentPropertiesForChildLibrary())
Jihoon Kang71c86832023-09-13 01:01:53 +00002613}
2614
Paul Duffin44f1d842020-06-26 20:17:02 +01002615// Add the dependencies on the child module in the component deps mutator so that it
2616// creates references to the prebuilt and not the source modules.
2617func (module *SdkLibraryImport) ComponentDepsMutator(ctx android.BottomUpMutatorContext) {
Paul Duffin46a26a82020-04-07 19:27:04 +01002618 for apiScope, scopeProperties := range module.scopeProperties {
Paul Duffin56d44902020-01-31 13:36:25 +00002619 if len(scopeProperties.Jars) == 0 {
2620 continue
2621 }
2622
2623 // Add dependencies to the prebuilt stubs library
Paul Duffin864116c2021-04-02 10:24:13 +01002624 ctx.AddVariationDependencies(nil, apiScope.stubsTag, android.PrebuiltNameFromSource(module.stubsLibraryModuleName(apiScope)))
Paul Duffin0f8faff2020-05-20 16:18:00 +01002625
2626 if len(scopeProperties.Stub_srcs) > 0 {
2627 // Add dependencies to the prebuilt stubs source library
Paul Duffin864116c2021-04-02 10:24:13 +01002628 ctx.AddVariationDependencies(nil, apiScope.stubsSourceTag, android.PrebuiltNameFromSource(module.stubsSourceModuleName(apiScope)))
Paul Duffin0f8faff2020-05-20 16:18:00 +01002629 }
Paul Duffin56d44902020-01-31 13:36:25 +00002630 }
Paul Duffin44f1d842020-06-26 20:17:02 +01002631}
2632
2633// Add other dependencies as normal.
2634func (module *SdkLibraryImport) DepsMutator(ctx android.BottomUpMutatorContext) {
Paul Duffineedc5d52020-06-12 17:46:39 +01002635
2636 implName := module.implLibraryModuleName()
2637 if ctx.OtherModuleExists(implName) {
2638 ctx.AddVariationDependencies(nil, implLibraryTag, implName)
2639
2640 xmlPermissionsModuleName := module.xmlPermissionsModuleName()
2641 if module.sharedLibrary() && ctx.OtherModuleExists(xmlPermissionsModuleName) {
2642 // Add dependency to the rule for generating the xml permissions file
2643 ctx.AddDependency(module, xmlPermissionsFileTag, xmlPermissionsModuleName)
2644 }
2645 }
Colin Cross79c7c262019-04-17 11:11:46 -07002646}
2647
Jiakai Zhang204356f2021-09-09 08:12:46 +00002648func (module *SdkLibraryImport) AndroidMkEntries() []android.AndroidMkEntries {
2649 // For an SDK library imported from a prebuilt APEX, we don't need a Make module for itself, as we
2650 // don't need to install it. However, we need to add its dexpreopt outputs as sub-modules, if it
2651 // is preopted.
2652 dexpreoptEntries := module.dexpreopter.AndroidMkEntriesForApex()
2653 return append(dexpreoptEntries, android.AndroidMkEntries{Disabled: true})
2654}
2655
Jiyong Park45bf82e2020-12-15 22:29:02 +09002656var _ android.ApexModule = (*SdkLibraryImport)(nil)
2657
2658// Implements android.ApexModule
Paul Duffineedc5d52020-06-12 17:46:39 +01002659func (module *SdkLibraryImport) DepIsInSameApex(mctx android.BaseModuleContext, dep android.Module) bool {
2660 depTag := mctx.OtherModuleDependencyTag(dep)
2661 if depTag == xmlPermissionsFileTag {
2662 return true
2663 }
2664
2665 // None of the other dependencies of the java_sdk_library_import are in the same apex
2666 // as the one that references this module.
2667 return false
2668}
2669
Jiyong Park45bf82e2020-12-15 22:29:02 +09002670// Implements android.ApexModule
Dan Albertc8060532020-07-22 22:32:17 -07002671func (module *SdkLibraryImport) ShouldSupportSdkVersion(ctx android.BaseModuleContext,
2672 sdkVersion android.ApiLevel) error {
Jooyung Han749dc692020-04-15 11:03:39 +09002673 // we don't check prebuilt modules for sdk_version
2674 return nil
2675}
2676
Paul Duffinea8f8082021-06-24 13:25:57 +01002677// Implements android.ApexModule
2678func (module *SdkLibraryImport) UniqueApexVariations() bool {
2679 return module.uniqueApexVariations()
2680}
2681
Paul Duffin09817d62022-04-28 17:45:11 +01002682// MinSdkVersion - Implements hiddenAPIModule
Spandan Das8c9ae7e2023-03-03 21:20:36 +00002683func (module *SdkLibraryImport) MinSdkVersion(ctx android.EarlyModuleContext) android.ApiLevel {
2684 return android.NoneApiLevel
Paul Duffin09817d62022-04-28 17:45:11 +01002685}
2686
2687var _ hiddenAPIModule = (*SdkLibraryImport)(nil)
2688
Paul Duffineedc5d52020-06-12 17:46:39 +01002689func (module *SdkLibraryImport) OutputFiles(tag string) (android.Paths, error) {
Paul Duffin1e940d52022-04-29 14:21:25 +01002690 paths, err := module.commonOutputFiles(tag)
2691 if paths != nil || err != nil {
2692 return paths, err
2693 }
2694 if module.implLibraryModule != nil {
2695 return module.implLibraryModule.OutputFiles(tag)
2696 } else {
2697 return nil, nil
2698 }
Paul Duffin46dc45a2020-05-14 15:39:10 +01002699}
2700
Paul Duffineedc5d52020-06-12 17:46:39 +01002701func (module *SdkLibraryImport) GenerateAndroidBuildActions(ctx android.ModuleContext) {
Paul Duffina2ae7e02020-09-11 11:55:00 +01002702 module.generateCommonBuildActions(ctx)
2703
Jeongik Chad5fe8782021-07-08 01:13:11 +09002704 // Assume that source module(sdk_library) is installed in /<sdk_library partition>/framework
2705 module.installFile = android.PathForModuleInstall(ctx, "framework", module.Stem()+".jar")
2706
Paul Duffin0f8faff2020-05-20 16:18:00 +01002707 // Record the paths to the prebuilt stubs library and stubs source.
Colin Cross79c7c262019-04-17 11:11:46 -07002708 ctx.VisitDirectDeps(func(to android.Module) {
2709 tag := ctx.OtherModuleDependencyTag(to)
2710
Paul Duffin0f8faff2020-05-20 16:18:00 +01002711 // Extract information from any of the scope specific dependencies.
2712 if scopeTag, ok := tag.(scopeDependencyTag); ok {
2713 apiScope := scopeTag.apiScope
2714 scopePaths := module.getScopePathsCreateIfNeeded(apiScope)
2715
2716 // Extract information from the dependency. The exact information extracted
2717 // is determined by the nature of the dependency which is determined by the tag.
2718 scopeTag.extractDepInfo(ctx, to, scopePaths)
Paul Duffineedc5d52020-06-12 17:46:39 +01002719 } else if tag == implLibraryTag {
2720 if implLibrary, ok := to.(*Library); ok {
2721 module.implLibraryModule = implLibrary
2722 } else {
2723 ctx.ModuleErrorf("implementation library must be of type *java.Library but was %T", to)
2724 }
2725 } else if tag == xmlPermissionsFileTag {
2726 if xmlPermissionsFileModule, ok := to.(*sdkLibraryXml); ok {
2727 module.xmlPermissionsFileModule = xmlPermissionsFileModule
2728 } else {
2729 ctx.ModuleErrorf("xml permissions file module must be of type *sdkLibraryXml but was %T", to)
2730 }
Colin Cross79c7c262019-04-17 11:11:46 -07002731 }
2732 })
Paul Duffin0f8faff2020-05-20 16:18:00 +01002733
2734 // Populate the scope paths with information from the properties.
2735 for apiScope, scopeProperties := range module.scopeProperties {
2736 if len(scopeProperties.Jars) == 0 {
2737 continue
2738 }
2739
2740 paths := module.getScopePathsCreateIfNeeded(apiScope)
Anton Hanssond78eb762021-09-21 15:25:12 +01002741 paths.annotationsZip = android.OptionalPathForModuleSrc(ctx, scopeProperties.Annotations)
Paul Duffin0f8faff2020-05-20 16:18:00 +01002742 paths.currentApiFilePath = android.OptionalPathForModuleSrc(ctx, scopeProperties.Current_api)
2743 paths.removedApiFilePath = android.OptionalPathForModuleSrc(ctx, scopeProperties.Removed_api)
2744 }
Paul Duffin39853512021-02-26 11:09:39 +00002745
2746 if ctx.Device() {
2747 // If this is a variant created for a prebuilt_apex then use the dex implementation jar
2748 // obtained from the associated deapexer module.
2749 ai := ctx.Provider(android.ApexInfoProvider).(android.ApexInfo)
2750 if ai.ForPrebuiltApex {
Paul Duffin39853512021-02-26 11:09:39 +00002751 // Get the path of the dex implementation jar from the `deapexer` module.
Martin Stjernholm44825602021-09-17 01:44:12 +01002752 di := android.FindDeapexerProviderForModule(ctx)
2753 if di == nil {
2754 return // An error has been reported by FindDeapexerProviderForModule.
2755 }
Jiakai Zhang81e46812023-02-08 21:56:07 +08002756 dexJarFileApexRootRelative := apexRootRelativePathToJavaLib(module.BaseModuleName())
2757 if dexOutputPath := di.PrebuiltExportPath(dexJarFileApexRootRelative); dexOutputPath != nil {
Martin Stjernholm8be1e6d2021-09-15 03:34:04 +01002758 dexJarFile := makeDexJarPathFromPath(dexOutputPath)
2759 module.dexJarFile = dexJarFile
Jiakai Zhang204356f2021-09-09 08:12:46 +00002760 installPath := android.PathForModuleInPartitionInstall(
Jiakai Zhang81e46812023-02-08 21:56:07 +08002761 ctx, "apex", ai.ApexVariationName, dexJarFileApexRootRelative)
Jiakai Zhang204356f2021-09-09 08:12:46 +00002762 module.installFile = installPath
Martin Stjernholm8be1e6d2021-09-15 03:34:04 +01002763 module.initHiddenAPI(ctx, dexJarFile, module.findScopePaths(apiScopePublic).stubsImplPath[0], nil)
Jiakai Zhang204356f2021-09-09 08:12:46 +00002764
Jiakai Zhang204356f2021-09-09 08:12:46 +00002765 module.dexpreopter.installPath = module.dexpreopter.getInstallPath(ctx, installPath)
2766 module.dexpreopter.isSDKLibrary = true
2767 module.dexpreopter.uncompressedDex = shouldUncompressDex(ctx, &module.dexpreopter)
Jiakai Zhang81e46812023-02-08 21:56:07 +08002768
2769 if profilePath := di.PrebuiltExportPath(dexJarFileApexRootRelative + ".prof"); profilePath != nil {
2770 module.dexpreopter.inputProfilePathOnHost = profilePath
2771 }
2772
2773 // Dexpreopting.
Jiakai Zhang204356f2021-09-09 08:12:46 +00002774 module.dexpreopt(ctx, dexOutputPath)
Paul Duffin39853512021-02-26 11:09:39 +00002775 } else {
2776 // This should never happen as a variant for a prebuilt_apex is only created if the
2777 // prebuilt_apex has been configured to export the java library dex file.
Martin Stjernholm44825602021-09-17 01:44:12 +01002778 ctx.ModuleErrorf("internal error: no dex implementation jar available from prebuilt APEX %s", di.ApexModuleName())
Paul Duffin39853512021-02-26 11:09:39 +00002779 }
2780 }
2781 }
Colin Cross79c7c262019-04-17 11:11:46 -07002782}
2783
Jiyong Parkf1691d22021-03-29 20:11:58 +09002784func (module *SdkLibraryImport) sdkJars(ctx android.BaseModuleContext, sdkVersion android.SdkSpec, headerJars bool) android.Paths {
Paul Duffineedc5d52020-06-12 17:46:39 +01002785
2786 // For consistency with SdkLibrary make the implementation jar available to libraries that
2787 // are within the same APEX.
2788 implLibraryModule := module.implLibraryModule
Colin Cross56a83212020-09-15 18:30:11 -07002789 if implLibraryModule != nil && withinSameApexesAs(ctx, module) {
Paul Duffineedc5d52020-06-12 17:46:39 +01002790 if headerJars {
2791 return implLibraryModule.HeaderJars()
2792 } else {
2793 return implLibraryModule.ImplementationJars()
2794 }
2795 }
2796
Paul Duffin23970f42020-05-20 14:20:02 +01002797 return module.selectHeaderJarsForSdkVersion(ctx, sdkVersion)
Paul Duffin56d44902020-01-31 13:36:25 +00002798}
2799
Colin Cross79c7c262019-04-17 11:11:46 -07002800// to satisfy SdkLibraryDependency interface
Jiyong Parkf1691d22021-03-29 20:11:58 +09002801func (module *SdkLibraryImport) SdkHeaderJars(ctx android.BaseModuleContext, sdkVersion android.SdkSpec) android.Paths {
Colin Cross79c7c262019-04-17 11:11:46 -07002802 // This module is just a wrapper for the prebuilt stubs.
Paul Duffineedc5d52020-06-12 17:46:39 +01002803 return module.sdkJars(ctx, sdkVersion, true)
Colin Cross79c7c262019-04-17 11:11:46 -07002804}
2805
2806// to satisfy SdkLibraryDependency interface
Jiyong Parkf1691d22021-03-29 20:11:58 +09002807func (module *SdkLibraryImport) SdkImplementationJars(ctx android.BaseModuleContext, sdkVersion android.SdkSpec) android.Paths {
Colin Cross79c7c262019-04-17 11:11:46 -07002808 // This module is just a wrapper for the stubs.
Paul Duffineedc5d52020-06-12 17:46:39 +01002809 return module.sdkJars(ctx, sdkVersion, false)
2810}
2811
Ulya Trafimovichdbf31662020-12-17 12:07:54 +00002812// to satisfy UsesLibraryDependency interface
Martin Stjernholm8be1e6d2021-09-15 03:34:04 +01002813func (module *SdkLibraryImport) DexJarBuildPath() OptionalDexJarPath {
Paul Duffin39853512021-02-26 11:09:39 +00002814 // The dex implementation jar extracted from the .apex file should be used in preference to the
2815 // source.
Martin Stjernholm8be1e6d2021-09-15 03:34:04 +01002816 if module.dexJarFile.IsSet() {
Paul Duffin39853512021-02-26 11:09:39 +00002817 return module.dexJarFile
2818 }
Paul Duffineedc5d52020-06-12 17:46:39 +01002819 if module.implLibraryModule == nil {
Martin Stjernholm8be1e6d2021-09-15 03:34:04 +01002820 return makeUnsetDexJarPath()
Paul Duffineedc5d52020-06-12 17:46:39 +01002821 } else {
2822 return module.implLibraryModule.DexJarBuildPath()
2823 }
2824}
2825
Ulya Trafimovichdbf31662020-12-17 12:07:54 +00002826// to satisfy UsesLibraryDependency interface
Ulya Trafimovich31e444e2020-08-14 17:32:16 +01002827func (module *SdkLibraryImport) DexJarInstallPath() android.Path {
Jeongik Chad5fe8782021-07-08 01:13:11 +09002828 return module.installFile
Ulya Trafimovich31e444e2020-08-14 17:32:16 +01002829}
2830
Ulya Trafimovichdbf31662020-12-17 12:07:54 +00002831// to satisfy UsesLibraryDependency interface
2832func (module *SdkLibraryImport) ClassLoaderContexts() dexpreopt.ClassLoaderContextMap {
2833 return nil
2834}
2835
Paul Duffineedc5d52020-06-12 17:46:39 +01002836// to satisfy apex.javaDependency interface
2837func (module *SdkLibraryImport) JacocoReportClassesFile() android.Path {
2838 if module.implLibraryModule == nil {
2839 return nil
2840 } else {
2841 return module.implLibraryModule.JacocoReportClassesFile()
2842 }
2843}
2844
2845// to satisfy apex.javaDependency interface
Colin Cross08dca382020-07-21 20:31:17 -07002846func (module *SdkLibraryImport) LintDepSets() LintDepSets {
2847 if module.implLibraryModule == nil {
2848 return LintDepSets{}
2849 } else {
2850 return module.implLibraryModule.LintDepSets()
2851 }
2852}
2853
Spandan Das17854f52022-01-14 21:19:14 +00002854func (module *SdkLibraryImport) GetStrictUpdatabilityLinting() bool {
Jaewoong Jung476b9d62021-05-10 15:30:00 -07002855 if module.implLibraryModule == nil {
2856 return false
2857 } else {
Spandan Das17854f52022-01-14 21:19:14 +00002858 return module.implLibraryModule.GetStrictUpdatabilityLinting()
Jaewoong Jung476b9d62021-05-10 15:30:00 -07002859 }
2860}
2861
Spandan Das17854f52022-01-14 21:19:14 +00002862func (module *SdkLibraryImport) SetStrictUpdatabilityLinting(strictLinting bool) {
Jaewoong Jung476b9d62021-05-10 15:30:00 -07002863 if module.implLibraryModule != nil {
Spandan Das17854f52022-01-14 21:19:14 +00002864 module.implLibraryModule.SetStrictUpdatabilityLinting(strictLinting)
Jaewoong Jung476b9d62021-05-10 15:30:00 -07002865 }
2866}
2867
Colin Cross08dca382020-07-21 20:31:17 -07002868// to satisfy apex.javaDependency interface
Paul Duffineedc5d52020-06-12 17:46:39 +01002869func (module *SdkLibraryImport) Stem() string {
2870 return module.BaseModuleName()
Colin Cross79c7c262019-04-17 11:11:46 -07002871}
Jiyong Parke3833882020-02-17 17:28:10 +09002872
Paul Duffin44b481b2020-06-17 16:59:43 +01002873var _ ApexDependency = (*SdkLibraryImport)(nil)
2874
2875// to satisfy java.ApexDependency interface
2876func (module *SdkLibraryImport) HeaderJars() android.Paths {
2877 if module.implLibraryModule == nil {
2878 return nil
2879 } else {
2880 return module.implLibraryModule.HeaderJars()
2881 }
2882}
2883
2884// to satisfy java.ApexDependency interface
2885func (module *SdkLibraryImport) ImplementationAndResourcesJars() android.Paths {
2886 if module.implLibraryModule == nil {
2887 return nil
2888 } else {
2889 return module.implLibraryModule.ImplementationAndResourcesJars()
2890 }
2891}
2892
Jiakai Zhang204356f2021-09-09 08:12:46 +00002893// to satisfy java.DexpreopterInterface interface
2894func (module *SdkLibraryImport) IsInstallable() bool {
2895 return true
2896}
2897
Paul Duffinfef55002021-06-17 14:56:05 +01002898var _ android.RequiredFilesFromPrebuiltApex = (*SdkLibraryImport)(nil)
2899
Paul Duffinb4bbf2c2021-06-17 15:59:07 +01002900func (module *SdkLibraryImport) RequiredFilesFromPrebuiltApex(ctx android.BaseModuleContext) []string {
Paul Duffinfef55002021-06-17 14:56:05 +01002901 name := module.BaseModuleName()
Jiakai Zhang81e46812023-02-08 21:56:07 +08002902 return requiredFilesFromPrebuiltApexForImport(name, &module.dexpreopter)
Paul Duffinfef55002021-06-17 14:56:05 +01002903}
2904
Jiyong Parke3833882020-02-17 17:28:10 +09002905// java_sdk_library_xml
Jiyong Parke3833882020-02-17 17:28:10 +09002906type sdkLibraryXml struct {
2907 android.ModuleBase
2908 android.DefaultableModuleBase
2909 android.ApexModuleBase
2910
2911 properties sdkLibraryXmlProperties
2912
2913 outputFilePath android.OutputPath
2914 installDirPath android.InstallPath
Colin Cross56a83212020-09-15 18:30:11 -07002915
2916 hideApexVariantFromMake bool
Jiyong Parke3833882020-02-17 17:28:10 +09002917}
2918
2919type sdkLibraryXmlProperties struct {
2920 // canonical name of the lib
2921 Lib_name *string
Pedro Loureiro9956e5e2021-09-07 17:21:59 +00002922
2923 // Signals that this shared library is part of the bootclasspath starting
2924 // on the version indicated in this attribute.
2925 //
2926 // This will make platforms at this level and above to ignore
2927 // <uses-library> tags with this library name because the library is already
2928 // available
2929 On_bootclasspath_since *string
2930
2931 // Signals that this shared library was part of the bootclasspath before
2932 // (but not including) the version indicated in this attribute.
2933 //
2934 // The system will automatically add a <uses-library> tag with this library to
2935 // apps that target any SDK less than the version indicated in this attribute.
2936 On_bootclasspath_before *string
2937
2938 // Indicates that PackageManager should ignore this shared library if the
2939 // platform is below the version indicated in this attribute.
2940 //
2941 // This means that the device won't recognise this library as installed.
2942 Min_device_sdk *string
2943
2944 // Indicates that PackageManager should ignore this shared library if the
2945 // platform is above the version indicated in this attribute.
2946 //
2947 // This means that the device won't recognise this library as installed.
2948 Max_device_sdk *string
Pedro Loureiroc3621422021-09-28 15:40:23 +00002949
2950 // The SdkLibrary's min api level as a string
2951 //
2952 // This value comes from the ApiLevel of the MinSdkVersion property.
2953 Sdk_library_min_api_level *string
Jiyong Parke3833882020-02-17 17:28:10 +09002954}
2955
2956// java_sdk_library_xml builds the permission xml file for a java_sdk_library.
2957// Not to be used directly by users. java_sdk_library internally uses this.
2958func sdkLibraryXmlFactory() android.Module {
2959 module := &sdkLibraryXml{}
2960
2961 module.AddProperties(&module.properties)
2962
2963 android.InitApexModule(module)
2964 android.InitAndroidArchModule(module, android.DeviceSupported, android.MultilibCommon)
2965
2966 return module
2967}
2968
Colin Crossaede88c2020-08-11 12:17:01 -07002969func (module *sdkLibraryXml) UniqueApexVariations() bool {
2970 // sdkLibraryXml needs a unique variation per APEX because the generated XML file contains the path to the
2971 // mounted APEX, which contains the name of the APEX.
2972 return true
2973}
2974
Jiyong Parke3833882020-02-17 17:28:10 +09002975// from android.PrebuiltEtcModule
Jooyung Han0703fd82020-08-26 22:11:53 +09002976func (module *sdkLibraryXml) BaseDir() string {
2977 return "etc"
2978}
2979
2980// from android.PrebuiltEtcModule
Jiyong Parke3833882020-02-17 17:28:10 +09002981func (module *sdkLibraryXml) SubDir() string {
2982 return "permissions"
2983}
2984
2985// from android.PrebuiltEtcModule
2986func (module *sdkLibraryXml) OutputFile() android.OutputPath {
2987 return module.outputFilePath
2988}
2989
2990// from android.ApexModule
2991func (module *sdkLibraryXml) AvailableFor(what string) bool {
2992 return true
2993}
2994
2995func (module *sdkLibraryXml) DepsMutator(ctx android.BottomUpMutatorContext) {
2996 // do nothing
2997}
2998
Jiyong Park45bf82e2020-12-15 22:29:02 +09002999var _ android.ApexModule = (*sdkLibraryXml)(nil)
3000
3001// Implements android.ApexModule
Dan Albertc8060532020-07-22 22:32:17 -07003002func (module *sdkLibraryXml) ShouldSupportSdkVersion(ctx android.BaseModuleContext,
3003 sdkVersion android.ApiLevel) error {
Jooyung Han749dc692020-04-15 11:03:39 +09003004 // sdkLibraryXml doesn't need to be checked separately because java_sdk_library is checked
3005 return nil
3006}
3007
Jiyong Parke3833882020-02-17 17:28:10 +09003008// File path to the runtime implementation library
Colin Cross56a83212020-09-15 18:30:11 -07003009func (module *sdkLibraryXml) implPath(ctx android.ModuleContext) string {
Jiyong Parke3833882020-02-17 17:28:10 +09003010 implName := proptools.String(module.properties.Lib_name)
Colin Cross56a83212020-09-15 18:30:11 -07003011 if apexInfo := ctx.Provider(android.ApexInfoProvider).(android.ApexInfo); !apexInfo.IsForPlatform() {
Colin Crosse07f2312020-08-13 11:24:56 -07003012 // TODO(b/146468504): ApexVariationName() is only a soong module name, not apex name.
Jiyong Parke3833882020-02-17 17:28:10 +09003013 // In most cases, this works fine. But when apex_name is set or override_apex is used
3014 // this can be wrong.
Colin Cross56a83212020-09-15 18:30:11 -07003015 return fmt.Sprintf("/apex/%s/javalib/%s.jar", apexInfo.ApexVariationName, implName)
Jiyong Parke3833882020-02-17 17:28:10 +09003016 }
3017 partition := "system"
3018 if module.SocSpecific() {
3019 partition = "vendor"
3020 } else if module.DeviceSpecific() {
3021 partition = "odm"
3022 } else if module.ProductSpecific() {
3023 partition = "product"
3024 } else if module.SystemExtSpecific() {
3025 partition = "system_ext"
3026 }
3027 return "/" + partition + "/framework/" + implName + ".jar"
3028}
3029
Pedro Loureiro9956e5e2021-09-07 17:21:59 +00003030func formattedOptionalSdkLevelAttribute(ctx android.ModuleContext, attrName string, value *string) string {
3031 if value == nil {
3032 return ""
3033 }
3034 apiLevel, err := android.ApiLevelFromUser(ctx, *value)
3035 if err != nil {
Pedro Loureiroba6682f2021-10-29 09:32:32 +00003036 // attributes in bp files have underscores but in the xml have dashes.
3037 ctx.PropertyErrorf(strings.ReplaceAll(attrName, "-", "_"), err.Error())
Pedro Loureiro9956e5e2021-09-07 17:21:59 +00003038 return ""
3039 }
Pedro Loureirob638c622021-12-22 15:28:05 +00003040 if apiLevel.IsCurrent() {
3041 // passing "current" would always mean a future release, never the current (or the current in
3042 // progress) which means some conditions would never be triggered.
3043 ctx.PropertyErrorf(strings.ReplaceAll(attrName, "-", "_"),
3044 `"current" is not an allowed value for this attribute`)
3045 return ""
3046 }
Pedro Loureiro48991222022-06-17 20:01:21 +00003047 // "safeValue" is safe because it translates finalized codenames to a string
3048 // with their SDK int.
3049 safeValue := apiLevel.String()
3050 return formattedOptionalAttribute(attrName, &safeValue)
Pedro Loureiro9956e5e2021-09-07 17:21:59 +00003051}
3052
3053// formats an attribute for the xml permissions file if the value is not null
3054// returns empty string otherwise
3055func formattedOptionalAttribute(attrName string, value *string) string {
3056 if value == nil {
3057 return ""
3058 }
3059 return fmt.Sprintf(` %s=\"%s\"\n`, attrName, *value)
3060}
3061
3062func (module *sdkLibraryXml) permissionsContents(ctx android.ModuleContext) string {
3063 libName := proptools.String(module.properties.Lib_name)
3064 libNameAttr := formattedOptionalAttribute("name", &libName)
3065 filePath := module.implPath(ctx)
3066 filePathAttr := formattedOptionalAttribute("file", &filePath)
Pedro Loureiroba6682f2021-10-29 09:32:32 +00003067 implicitFromAttr := formattedOptionalSdkLevelAttribute(ctx, "on-bootclasspath-since", module.properties.On_bootclasspath_since)
3068 implicitUntilAttr := formattedOptionalSdkLevelAttribute(ctx, "on-bootclasspath-before", module.properties.On_bootclasspath_before)
3069 minSdkAttr := formattedOptionalSdkLevelAttribute(ctx, "min-device-sdk", module.properties.Min_device_sdk)
3070 maxSdkAttr := formattedOptionalSdkLevelAttribute(ctx, "max-device-sdk", module.properties.Max_device_sdk)
Pedro Loureiro196d3e62021-12-22 19:53:01 +00003071 // <library> is understood in all android versions whereas <apex-library> is only understood from API T (and ignored before that).
3072 // 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 +00003073 var libraryTag string
3074 if module.properties.Min_device_sdk != nil {
Pedro Loureiro196d3e62021-12-22 19:53:01 +00003075 libraryTag = ` <apex-library\n`
Pedro Loureiroc3621422021-09-28 15:40:23 +00003076 } else {
3077 libraryTag = ` <library\n`
3078 }
Pedro Loureiro9956e5e2021-09-07 17:21:59 +00003079
3080 return strings.Join([]string{
3081 `<?xml version=\"1.0\" encoding=\"utf-8\"?>\n`,
3082 `<!-- Copyright (C) 2018 The Android Open Source Project\n`,
3083 `\n`,
3084 ` Licensed under the Apache License, Version 2.0 (the \"License\");\n`,
3085 ` you may not use this file except in compliance with the License.\n`,
3086 ` You may obtain a copy of the License at\n`,
3087 `\n`,
3088 ` http://www.apache.org/licenses/LICENSE-2.0\n`,
3089 `\n`,
3090 ` Unless required by applicable law or agreed to in writing, software\n`,
3091 ` distributed under the License is distributed on an \"AS IS\" BASIS,\n`,
3092 ` WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\n`,
3093 ` See the License for the specific language governing permissions and\n`,
3094 ` limitations under the License.\n`,
3095 `-->\n`,
3096 `<permissions>\n`,
Pedro Loureiroc3621422021-09-28 15:40:23 +00003097 libraryTag,
Pedro Loureiro9956e5e2021-09-07 17:21:59 +00003098 libNameAttr,
3099 filePathAttr,
3100 implicitFromAttr,
3101 implicitUntilAttr,
3102 minSdkAttr,
3103 maxSdkAttr,
3104 ` />\n`,
3105 `</permissions>\n`}, "")
3106}
3107
Jiyong Parke3833882020-02-17 17:28:10 +09003108func (module *sdkLibraryXml) GenerateAndroidBuildActions(ctx android.ModuleContext) {
Colin Cross56a83212020-09-15 18:30:11 -07003109 module.hideApexVariantFromMake = !ctx.Provider(android.ApexInfoProvider).(android.ApexInfo).IsForPlatform()
3110
Jiyong Parke3833882020-02-17 17:28:10 +09003111 libName := proptools.String(module.properties.Lib_name)
Pedro Loureiroc3621422021-09-28 15:40:23 +00003112 module.selfValidate(ctx)
Pedro Loureiro9956e5e2021-09-07 17:21:59 +00003113 xmlContent := module.permissionsContents(ctx)
Jiyong Parke3833882020-02-17 17:28:10 +09003114
3115 module.outputFilePath = android.PathForModuleOut(ctx, libName+".xml").OutputPath
Colin Crossf1a035e2020-11-16 17:32:30 -08003116 rule := android.NewRuleBuilder(pctx, ctx)
Jiyong Parke3833882020-02-17 17:28:10 +09003117 rule.Command().
3118 Text("/bin/bash -c \"echo -e '" + xmlContent + "'\" > ").
3119 Output(module.outputFilePath)
3120
Colin Crossf1a035e2020-11-16 17:32:30 -08003121 rule.Build("java_sdk_xml", "Permission XML")
Jiyong Parke3833882020-02-17 17:28:10 +09003122
3123 module.installDirPath = android.PathForModuleInstall(ctx, "etc", module.SubDir())
3124}
3125
3126func (module *sdkLibraryXml) AndroidMkEntries() []android.AndroidMkEntries {
Colin Cross56a83212020-09-15 18:30:11 -07003127 if module.hideApexVariantFromMake {
satayev8f088b02021-12-06 11:40:46 +00003128 return []android.AndroidMkEntries{{
Jiyong Parke3833882020-02-17 17:28:10 +09003129 Disabled: true,
3130 }}
3131 }
3132
satayev8f088b02021-12-06 11:40:46 +00003133 return []android.AndroidMkEntries{{
Jiyong Parke3833882020-02-17 17:28:10 +09003134 Class: "ETC",
3135 OutputFile: android.OptionalPathForPath(module.outputFilePath),
3136 ExtraEntries: []android.AndroidMkExtraEntriesFunc{
Colin Crossaa255532020-07-03 13:18:24 -07003137 func(ctx android.AndroidMkExtraEntriesContext, entries *android.AndroidMkEntries) {
Jiyong Parke3833882020-02-17 17:28:10 +09003138 entries.SetString("LOCAL_MODULE_TAGS", "optional")
Colin Crossc68db4b2021-11-11 18:59:15 -08003139 entries.SetString("LOCAL_MODULE_PATH", module.installDirPath.String())
Jiyong Parke3833882020-02-17 17:28:10 +09003140 entries.SetString("LOCAL_INSTALLED_MODULE_STEM", module.outputFilePath.Base())
3141 },
3142 },
3143 }}
3144}
Paul Duffindd46f712020-02-10 13:37:10 +00003145
Pedro Loureiroc3621422021-09-28 15:40:23 +00003146func (module *sdkLibraryXml) selfValidate(ctx android.ModuleContext) {
3147 module.validateAtLeastTAttributes(ctx)
3148 module.validateMinAndMaxDeviceSdk(ctx)
3149 module.validateMinMaxDeviceSdkAndModuleMinSdk(ctx)
3150 module.validateOnBootclasspathBeforeRequirements(ctx)
3151}
3152
3153func (module *sdkLibraryXml) validateAtLeastTAttributes(ctx android.ModuleContext) {
3154 t := android.ApiLevelOrPanic(ctx, "Tiramisu")
3155 module.attrAtLeastT(ctx, t, module.properties.Min_device_sdk, "min_device_sdk")
3156 module.attrAtLeastT(ctx, t, module.properties.Max_device_sdk, "max_device_sdk")
3157 module.attrAtLeastT(ctx, t, module.properties.On_bootclasspath_before, "on_bootclasspath_before")
3158 module.attrAtLeastT(ctx, t, module.properties.On_bootclasspath_since, "on_bootclasspath_since")
3159}
3160
3161func (module *sdkLibraryXml) attrAtLeastT(ctx android.ModuleContext, t android.ApiLevel, attr *string, attrName string) {
3162 if attr != nil {
3163 if level, err := android.ApiLevelFromUser(ctx, *attr); err == nil {
3164 // we will inform the user of invalid inputs when we try to write the
3165 // permissions xml file so we don't need to do it here
3166 if t.GreaterThan(level) {
3167 ctx.PropertyErrorf(attrName, "Attribute value needs to be at least T")
3168 }
3169 }
3170 }
3171}
3172
3173func (module *sdkLibraryXml) validateMinAndMaxDeviceSdk(ctx android.ModuleContext) {
3174 if module.properties.Min_device_sdk != nil && module.properties.Max_device_sdk != nil {
3175 min, minErr := android.ApiLevelFromUser(ctx, *module.properties.Min_device_sdk)
3176 max, maxErr := android.ApiLevelFromUser(ctx, *module.properties.Max_device_sdk)
3177 if minErr == nil && maxErr == nil {
3178 // we will inform the user of invalid inputs when we try to write the
3179 // permissions xml file so we don't need to do it here
3180 if min.GreaterThan(max) {
3181 ctx.ModuleErrorf("min_device_sdk can't be greater than max_device_sdk")
3182 }
3183 }
3184 }
3185}
3186
3187func (module *sdkLibraryXml) validateMinMaxDeviceSdkAndModuleMinSdk(ctx android.ModuleContext) {
3188 moduleMinApi := android.ApiLevelOrPanic(ctx, *module.properties.Sdk_library_min_api_level)
3189 if module.properties.Min_device_sdk != nil {
3190 api, err := android.ApiLevelFromUser(ctx, *module.properties.Min_device_sdk)
3191 if err == nil {
3192 if moduleMinApi.GreaterThan(api) {
3193 ctx.PropertyErrorf("min_device_sdk", "Can't be less than module's min sdk (%s)", moduleMinApi)
3194 }
3195 }
3196 }
3197 if module.properties.Max_device_sdk != nil {
3198 api, err := android.ApiLevelFromUser(ctx, *module.properties.Max_device_sdk)
3199 if err == nil {
3200 if moduleMinApi.GreaterThan(api) {
3201 ctx.PropertyErrorf("max_device_sdk", "Can't be less than module's min sdk (%s)", moduleMinApi)
3202 }
3203 }
3204 }
3205}
3206
3207func (module *sdkLibraryXml) validateOnBootclasspathBeforeRequirements(ctx android.ModuleContext) {
3208 moduleMinApi := android.ApiLevelOrPanic(ctx, *module.properties.Sdk_library_min_api_level)
3209 if module.properties.On_bootclasspath_before != nil {
3210 t := android.ApiLevelOrPanic(ctx, "Tiramisu")
3211 // if we use the attribute, then we need to do this validation
3212 if moduleMinApi.LessThan(t) {
3213 // if minAPi is < T, then we need to have min_device_sdk (which only accepts T+)
3214 if module.properties.Min_device_sdk == nil {
3215 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")
3216 }
3217 }
3218 }
3219}
3220
Paul Duffindd46f712020-02-10 13:37:10 +00003221type sdkLibrarySdkMemberType struct {
3222 android.SdkMemberTypeBase
3223}
3224
Paul Duffin296701e2021-07-14 10:29:36 +01003225func (s *sdkLibrarySdkMemberType) AddDependencies(ctx android.SdkDependencyContext, dependencyTag blueprint.DependencyTag, names []string) {
3226 ctx.AddVariationDependencies(nil, dependencyTag, names...)
Paul Duffindd46f712020-02-10 13:37:10 +00003227}
3228
3229func (s *sdkLibrarySdkMemberType) IsInstance(module android.Module) bool {
3230 _, ok := module.(*SdkLibrary)
3231 return ok
3232}
3233
3234func (s *sdkLibrarySdkMemberType) AddPrebuiltModule(ctx android.SdkMemberContext, member android.SdkMember) android.BpModule {
3235 return ctx.SnapshotBuilder().AddPrebuiltModule(member, "java_sdk_library_import")
3236}
3237
3238func (s *sdkLibrarySdkMemberType) CreateVariantPropertiesStruct() android.SdkMemberProperties {
3239 return &sdkLibrarySdkMemberProperties{}
3240}
3241
Paul Duffin976b0e52021-04-27 23:20:26 +01003242var javaSdkLibrarySdkMemberType = &sdkLibrarySdkMemberType{
3243 android.SdkMemberTypeBase{
3244 PropertyName: "java_sdk_libs",
3245 SupportsSdk: true,
3246 },
3247}
3248
Paul Duffindd46f712020-02-10 13:37:10 +00003249type sdkLibrarySdkMemberProperties struct {
3250 android.SdkMemberPropertiesBase
3251
Paul Duffine8409952022-09-22 16:24:46 +01003252 // Stem name for files in the sdk snapshot.
3253 //
3254 // This is used to construct the path names of various sdk library files in the sdk snapshot to
3255 // make sure that they match the finalized versions of those files in prebuilts/sdk.
3256 //
3257 // This property is marked as keep so that it will be kept in all instances of this struct, will
3258 // not be cleared but will be copied to common structs. That is needed because this field is used
3259 // to construct many file names for other parts of this struct and so it needs to be present in
3260 // all structs. If it was not marked as keep then it would be cleared in some structs and so would
3261 // be unavailable for generating file names if there were other properties that were still set.
3262 Stem string `sdk:"keep"`
3263
Paul Duffindd46f712020-02-10 13:37:10 +00003264 // Scope to per scope properties.
Paul Duffin106a3a42022-01-27 16:39:06 +00003265 Scopes map[*apiScope]*scopeProperties
Paul Duffindd46f712020-02-10 13:37:10 +00003266
Paul Duffin3d1248c2020-04-09 00:10:17 +01003267 // The Java stubs source files.
3268 Stub_srcs []string
Paul Duffinf7a64332020-05-13 16:54:55 +01003269
3270 // The naming scheme.
3271 Naming_scheme *string
Paul Duffind7eb1c22020-05-26 20:57:10 +01003272
3273 // True if the java_sdk_library_import is for a shared library, false
3274 // otherwise.
3275 Shared_library *bool
Paul Duffina2ae7e02020-09-11 11:55:00 +01003276
Paul Duffin1267d872021-04-16 17:21:36 +01003277 // True if the stub imports should produce dex jars.
3278 Compile_dex *bool
3279
Paul Duffina2ae7e02020-09-11 11:55:00 +01003280 // The paths to the doctag files to add to the prebuilt.
3281 Doctag_paths android.Paths
Paul Duffin869de142021-07-15 14:14:41 +01003282
3283 Permitted_packages []string
Pedro Loureiro9956e5e2021-09-07 17:21:59 +00003284
3285 // Signals that this shared library is part of the bootclasspath starting
3286 // on the version indicated in this attribute.
3287 //
3288 // This will make platforms at this level and above to ignore
3289 // <uses-library> tags with this library name because the library is already
3290 // available
3291 On_bootclasspath_since *string
3292
3293 // Signals that this shared library was part of the bootclasspath before
3294 // (but not including) the version indicated in this attribute.
3295 //
3296 // The system will automatically add a <uses-library> tag with this library to
3297 // apps that target any SDK less than the version indicated in this attribute.
3298 On_bootclasspath_before *string
3299
3300 // Indicates that PackageManager should ignore this shared library if the
3301 // platform is below the version indicated in this attribute.
3302 //
3303 // This means that the device won't recognise this library as installed.
3304 Min_device_sdk *string
3305
3306 // Indicates that PackageManager should ignore this shared library if the
3307 // platform is above the version indicated in this attribute.
3308 //
3309 // This means that the device won't recognise this library as installed.
3310 Max_device_sdk *string
Jiakai Zhang9c4dc192023-02-09 00:09:24 +08003311
3312 DexPreoptProfileGuided *bool `supported_build_releases:"UpsideDownCake+"`
Paul Duffindd46f712020-02-10 13:37:10 +00003313}
3314
3315type scopeProperties struct {
Paul Duffin1fd005d2020-04-09 01:08:11 +01003316 Jars android.Paths
3317 StubsSrcJar android.Path
3318 CurrentApiFile android.Path
3319 RemovedApiFile android.Path
Paul Duffine7babdb2022-02-10 13:06:54 +00003320 AnnotationsZip android.Path `supported_build_releases:"Tiramisu+"`
Paul Duffin1fd005d2020-04-09 01:08:11 +01003321 SdkVersion string
Paul Duffindd46f712020-02-10 13:37:10 +00003322}
3323
3324func (s *sdkLibrarySdkMemberProperties) PopulateFromVariant(ctx android.SdkMemberContext, variant android.Module) {
3325 sdk := variant.(*SdkLibrary)
3326
Paul Duffine8409952022-09-22 16:24:46 +01003327 // Copy the stem name for files in the sdk snapshot.
3328 s.Stem = sdk.distStem()
3329
Paul Duffin106a3a42022-01-27 16:39:06 +00003330 s.Scopes = make(map[*apiScope]*scopeProperties)
Paul Duffindd46f712020-02-10 13:37:10 +00003331 for _, apiScope := range allApiScopes {
Paul Duffin803a9562020-05-20 11:52:25 +01003332 paths := sdk.findScopePaths(apiScope)
3333 if paths == nil {
3334 continue
3335 }
3336
Paul Duffindd46f712020-02-10 13:37:10 +00003337 jars := paths.stubsImplPath
3338 if len(jars) > 0 {
3339 properties := scopeProperties{}
3340 properties.Jars = jars
Paul Duffin780c5f42020-05-12 15:52:55 +01003341 properties.SdkVersion = sdk.sdkVersionForStubsLibrary(ctx.SdkModuleContext(), apiScope)
Paul Duffin0f8faff2020-05-20 16:18:00 +01003342 properties.StubsSrcJar = paths.stubsSrcJar.Path()
Paul Duffin10269f12020-06-19 18:39:55 +01003343 if paths.currentApiFilePath.Valid() {
3344 properties.CurrentApiFile = paths.currentApiFilePath.Path()
3345 }
3346 if paths.removedApiFilePath.Valid() {
3347 properties.RemovedApiFile = paths.removedApiFilePath.Path()
3348 }
Anton Hanssond78eb762021-09-21 15:25:12 +01003349 // The annotations zip is only available for modules that set annotations_enabled: true.
3350 if paths.annotationsZip.Valid() {
3351 properties.AnnotationsZip = paths.annotationsZip.Path()
3352 }
Paul Duffin106a3a42022-01-27 16:39:06 +00003353 s.Scopes[apiScope] = &properties
Paul Duffindd46f712020-02-10 13:37:10 +00003354 }
3355 }
3356
Paul Duffindfa131e2020-05-15 20:37:11 +01003357 s.Naming_scheme = sdk.commonSdkLibraryProperties.Naming_scheme
Paul Duffind7eb1c22020-05-26 20:57:10 +01003358 s.Shared_library = proptools.BoolPtr(sdk.sharedLibrary())
Paul Duffin1267d872021-04-16 17:21:36 +01003359 s.Compile_dex = sdk.dexProperties.Compile_dex
Paul Duffina2ae7e02020-09-11 11:55:00 +01003360 s.Doctag_paths = sdk.doctagPaths
Paul Duffin869de142021-07-15 14:14:41 +01003361 s.Permitted_packages = sdk.PermittedPackagesForUpdatableBootJars()
Pedro Loureiro9956e5e2021-09-07 17:21:59 +00003362 s.On_bootclasspath_since = sdk.commonSdkLibraryProperties.On_bootclasspath_since
3363 s.On_bootclasspath_before = sdk.commonSdkLibraryProperties.On_bootclasspath_before
3364 s.Min_device_sdk = sdk.commonSdkLibraryProperties.Min_device_sdk
3365 s.Max_device_sdk = sdk.commonSdkLibraryProperties.Max_device_sdk
Jiakai Zhang9c4dc192023-02-09 00:09:24 +08003366
3367 if sdk.dexpreopter.dexpreoptProperties.Dex_preopt_result.Profile_guided {
3368 s.DexPreoptProfileGuided = proptools.BoolPtr(true)
3369 }
Paul Duffindd46f712020-02-10 13:37:10 +00003370}
3371
3372func (s *sdkLibrarySdkMemberProperties) AddToPropertySet(ctx android.SdkMemberContext, propertySet android.BpPropertySet) {
Paul Duffinf7a64332020-05-13 16:54:55 +01003373 if s.Naming_scheme != nil {
3374 propertySet.AddProperty("naming_scheme", proptools.String(s.Naming_scheme))
3375 }
Paul Duffind7eb1c22020-05-26 20:57:10 +01003376 if s.Shared_library != nil {
3377 propertySet.AddProperty("shared_library", *s.Shared_library)
3378 }
Paul Duffin1267d872021-04-16 17:21:36 +01003379 if s.Compile_dex != nil {
3380 propertySet.AddProperty("compile_dex", *s.Compile_dex)
3381 }
Paul Duffin869de142021-07-15 14:14:41 +01003382 if len(s.Permitted_packages) > 0 {
3383 propertySet.AddProperty("permitted_packages", s.Permitted_packages)
3384 }
Jiakai Zhang9c4dc192023-02-09 00:09:24 +08003385 dexPreoptSet := propertySet.AddPropertySet("dex_preopt")
3386 if s.DexPreoptProfileGuided != nil {
3387 dexPreoptSet.AddProperty("profile_guided", proptools.Bool(s.DexPreoptProfileGuided))
3388 }
Paul Duffinf7a64332020-05-13 16:54:55 +01003389
Paul Duffine8409952022-09-22 16:24:46 +01003390 stem := s.Stem
3391
Paul Duffindd46f712020-02-10 13:37:10 +00003392 for _, apiScope := range allApiScopes {
3393 if properties, ok := s.Scopes[apiScope]; ok {
Paul Duffin6b836ba2020-05-13 19:19:49 +01003394 scopeSet := propertySet.AddPropertySet(apiScope.propertyName)
Paul Duffindd46f712020-02-10 13:37:10 +00003395
Paul Duffin958806b2022-05-16 13:10:47 +00003396 scopeDir := apiScope.snapshotRelativeDir()
Paul Duffin3d1248c2020-04-09 00:10:17 +01003397
Paul Duffindd46f712020-02-10 13:37:10 +00003398 var jars []string
3399 for _, p := range properties.Jars {
Paul Duffine8409952022-09-22 16:24:46 +01003400 dest := filepath.Join(scopeDir, stem+"-stubs.jar")
Paul Duffindd46f712020-02-10 13:37:10 +00003401 ctx.SnapshotBuilder().CopyToSnapshot(p, dest)
3402 jars = append(jars, dest)
3403 }
3404 scopeSet.AddProperty("jars", jars)
3405
Paul Duffin22628d52021-05-12 23:13:22 +01003406 if ctx.SdkModuleContext().Config().IsEnvTrue("SOONG_SDK_SNAPSHOT_USE_SRCJAR") {
3407 // Copy the stubs source jar into the snapshot zip as is.
Paul Duffine8409952022-09-22 16:24:46 +01003408 srcJarSnapshotPath := filepath.Join(scopeDir, stem+".srcjar")
Paul Duffin22628d52021-05-12 23:13:22 +01003409 ctx.SnapshotBuilder().CopyToSnapshot(properties.StubsSrcJar, srcJarSnapshotPath)
3410 scopeSet.AddProperty("stub_srcs", []string{srcJarSnapshotPath})
3411 } else {
3412 // Merge the stubs source jar into the snapshot zip so that when it is unpacked
3413 // the source files are also unpacked.
Paul Duffine8409952022-09-22 16:24:46 +01003414 snapshotRelativeDir := filepath.Join(scopeDir, stem+"_stub_sources")
Paul Duffin22628d52021-05-12 23:13:22 +01003415 ctx.SnapshotBuilder().UnzipToSnapshot(properties.StubsSrcJar, snapshotRelativeDir)
3416 scopeSet.AddProperty("stub_srcs", []string{snapshotRelativeDir})
3417 }
Paul Duffin3d1248c2020-04-09 00:10:17 +01003418
Paul Duffin1fd005d2020-04-09 01:08:11 +01003419 if properties.CurrentApiFile != nil {
Paul Duffine8409952022-09-22 16:24:46 +01003420 currentApiSnapshotPath := apiScope.snapshotRelativeCurrentApiTxtPath(stem)
Paul Duffin1fd005d2020-04-09 01:08:11 +01003421 ctx.SnapshotBuilder().CopyToSnapshot(properties.CurrentApiFile, currentApiSnapshotPath)
3422 scopeSet.AddProperty("current_api", currentApiSnapshotPath)
3423 }
3424
3425 if properties.RemovedApiFile != nil {
Paul Duffine8409952022-09-22 16:24:46 +01003426 removedApiSnapshotPath := apiScope.snapshotRelativeRemovedApiTxtPath(stem)
Paul Duffin3dbf9fd2020-06-02 13:00:02 +01003427 ctx.SnapshotBuilder().CopyToSnapshot(properties.RemovedApiFile, removedApiSnapshotPath)
Paul Duffin1fd005d2020-04-09 01:08:11 +01003428 scopeSet.AddProperty("removed_api", removedApiSnapshotPath)
3429 }
3430
Anton Hanssond78eb762021-09-21 15:25:12 +01003431 if properties.AnnotationsZip != nil {
Paul Duffine8409952022-09-22 16:24:46 +01003432 annotationsSnapshotPath := filepath.Join(scopeDir, stem+"_annotations.zip")
Anton Hanssond78eb762021-09-21 15:25:12 +01003433 ctx.SnapshotBuilder().CopyToSnapshot(properties.AnnotationsZip, annotationsSnapshotPath)
3434 scopeSet.AddProperty("annotations", annotationsSnapshotPath)
3435 }
3436
Paul Duffindd46f712020-02-10 13:37:10 +00003437 if properties.SdkVersion != "" {
3438 scopeSet.AddProperty("sdk_version", properties.SdkVersion)
3439 }
3440 }
3441 }
3442
Paul Duffina2ae7e02020-09-11 11:55:00 +01003443 if len(s.Doctag_paths) > 0 {
3444 dests := []string{}
3445 for _, p := range s.Doctag_paths {
3446 dest := filepath.Join("doctags", p.Rel())
3447 ctx.SnapshotBuilder().CopyToSnapshot(p, dest)
3448 dests = append(dests, dest)
3449 }
3450 propertySet.AddProperty("doctag_files", dests)
3451 }
Paul Duffindd46f712020-02-10 13:37:10 +00003452}