blob: ea451743bc183a166cf175f2c3b9a7848e87922e [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
Anton Hanssonfd1c0d22023-11-02 15:18:09 +0000621
622 // If API lint is enabled, this flag controls whether a set of legitimate lint errors
623 // are turned off. The default is true.
624 Legacy_errors_allowed *bool
Paul Duffin160fe412020-05-10 19:32:20 +0100625 }
626
Jiyong Parkc678ad32018-04-10 13:07:10 +0900627 // TODO: determines whether to create HTML doc or not
Paul Duffine8409952022-09-22 16:24:46 +0100628 // Html_doc *bool
Jiyong Parkc678ad32018-04-10 13:07:10 +0900629}
630
Paul Duffin0f8faff2020-05-20 16:18:00 +0100631// Paths to outputs from java_sdk_library and java_sdk_library_import.
632//
633// Fields that are android.Paths are always set (during GenerateAndroidBuildActions).
634// OptionalPaths are always set by java_sdk_library but may not be set by
635// java_sdk_library_import as not all instances provide that information.
Paul Duffind1b3a922020-01-22 11:57:20 +0000636type scopePaths struct {
Paul Duffin0f8faff2020-05-20 16:18:00 +0100637 // The path (represented as Paths for convenience when returning) to the stubs header jar.
638 //
639 // That is the jar that is created by turbine.
640 stubsHeaderPath android.Paths
641
642 // The path (represented as Paths for convenience when returning) to the stubs implementation jar.
643 //
644 // This is not the implementation jar, it still only contains stubs.
645 stubsImplPath android.Paths
646
Paul Duffin1267d872021-04-16 17:21:36 +0100647 // The dex jar for the stubs.
648 //
649 // This is not the implementation jar, it still only contains stubs.
Martin Stjernholm8be1e6d2021-09-15 03:34:04 +0100650 stubsDexJarPath OptionalDexJarPath
Paul Duffin1267d872021-04-16 17:21:36 +0100651
Paul Duffin0f8faff2020-05-20 16:18:00 +0100652 // The API specification file, e.g. system_current.txt.
653 currentApiFilePath android.OptionalPath
654
655 // The specification of API elements removed since the last release.
656 removedApiFilePath android.OptionalPath
657
658 // The stubs source jar.
659 stubsSrcJar android.OptionalPath
Anton Hanssond78eb762021-09-21 15:25:12 +0100660
661 // Extracted annotations.
662 annotationsZip android.OptionalPath
Paul Duffin958806b2022-05-16 13:10:47 +0000663
664 // The path to the latest API file.
665 latestApiPath android.OptionalPath
666
667 // The path to the latest removed API file.
668 latestRemovedApiPath android.OptionalPath
Paul Duffind1b3a922020-01-22 11:57:20 +0000669}
670
Colin Crossdcf71b22021-02-01 13:59:03 -0800671func (paths *scopePaths) extractStubsLibraryInfoFromDependency(ctx android.ModuleContext, dep android.Module) error {
672 if ctx.OtherModuleHasProvider(dep, JavaInfoProvider) {
673 lib := ctx.OtherModuleProvider(dep, JavaInfoProvider).(JavaInfo)
674 paths.stubsHeaderPath = lib.HeaderJars
675 paths.stubsImplPath = lib.ImplementationJars
Paul Duffin1267d872021-04-16 17:21:36 +0100676
677 libDep := dep.(UsesLibraryDependency)
678 paths.stubsDexJarPath = libDep.DexJarBuildPath()
Paul Duffinc8782502020-04-29 20:45:27 +0100679 return nil
680 } else {
Colin Crossdcf71b22021-02-01 13:59:03 -0800681 return fmt.Errorf("expected module that has JavaInfoProvider, e.g. java_library")
Paul Duffinc8782502020-04-29 20:45:27 +0100682 }
683}
684
Paul Duffin0ff08bd2020-04-29 13:30:54 +0100685func (paths *scopePaths) treatDepAsApiStubsProvider(dep android.Module, action func(provider ApiStubsProvider)) error {
686 if apiStubsProvider, ok := dep.(ApiStubsProvider); ok {
687 action(apiStubsProvider)
Paul Duffinc8782502020-04-29 20:45:27 +0100688 return nil
689 } else {
690 return fmt.Errorf("expected module that implements ApiStubsProvider, e.g. droidstubs")
691 }
692}
693
Paul Duffin0f8faff2020-05-20 16:18:00 +0100694func (paths *scopePaths) treatDepAsApiStubsSrcProvider(dep android.Module, action func(provider ApiStubsSrcProvider)) error {
695 if apiStubsProvider, ok := dep.(ApiStubsSrcProvider); ok {
696 action(apiStubsProvider)
697 return nil
698 } else {
699 return fmt.Errorf("expected module that implements ApiStubsSrcProvider, e.g. droidstubs")
700 }
701}
702
Paul Duffin0ff08bd2020-04-29 13:30:54 +0100703func (paths *scopePaths) extractApiInfoFromApiStubsProvider(provider ApiStubsProvider) {
Anton Hanssond78eb762021-09-21 15:25:12 +0100704 paths.annotationsZip = android.OptionalPathForPath(provider.AnnotationsZip())
Paul Duffin0f8faff2020-05-20 16:18:00 +0100705 paths.currentApiFilePath = android.OptionalPathForPath(provider.ApiFilePath())
706 paths.removedApiFilePath = android.OptionalPathForPath(provider.RemovedApiFilePath())
Paul Duffin0ff08bd2020-04-29 13:30:54 +0100707}
708
Colin Crossdcf71b22021-02-01 13:59:03 -0800709func (paths *scopePaths) extractApiInfoFromDep(ctx android.ModuleContext, dep android.Module) error {
Paul Duffin0ff08bd2020-04-29 13:30:54 +0100710 return paths.treatDepAsApiStubsProvider(dep, func(provider ApiStubsProvider) {
711 paths.extractApiInfoFromApiStubsProvider(provider)
712 })
713}
714
Paul Duffin0f8faff2020-05-20 16:18:00 +0100715func (paths *scopePaths) extractStubsSourceInfoFromApiStubsProviders(provider ApiStubsSrcProvider) {
716 paths.stubsSrcJar = android.OptionalPathForPath(provider.StubsSrcJar())
Paul Duffin0ff08bd2020-04-29 13:30:54 +0100717}
718
Colin Crossdcf71b22021-02-01 13:59:03 -0800719func (paths *scopePaths) extractStubsSourceInfoFromDep(ctx android.ModuleContext, dep android.Module) error {
Paul Duffin0f8faff2020-05-20 16:18:00 +0100720 return paths.treatDepAsApiStubsSrcProvider(dep, func(provider ApiStubsSrcProvider) {
Paul Duffin0ff08bd2020-04-29 13:30:54 +0100721 paths.extractStubsSourceInfoFromApiStubsProviders(provider)
722 })
723}
724
Colin Crossdcf71b22021-02-01 13:59:03 -0800725func (paths *scopePaths) extractStubsSourceAndApiInfoFromApiStubsProvider(ctx android.ModuleContext, dep android.Module) error {
Paul Duffin0ff08bd2020-04-29 13:30:54 +0100726 return paths.treatDepAsApiStubsProvider(dep, func(provider ApiStubsProvider) {
727 paths.extractApiInfoFromApiStubsProvider(provider)
728 paths.extractStubsSourceInfoFromApiStubsProviders(provider)
729 })
730}
731
Paul Duffin958806b2022-05-16 13:10:47 +0000732func extractSingleOptionalOutputPath(dep android.Module) (android.OptionalPath, error) {
733 var paths android.Paths
734 if sourceFileProducer, ok := dep.(android.SourceFileProducer); ok {
735 paths = sourceFileProducer.Srcs()
736 } else {
737 return android.OptionalPath{}, fmt.Errorf("module %q does not produce source files", dep)
738 }
739 if len(paths) != 1 {
740 return android.OptionalPath{}, fmt.Errorf("expected one path from %q, got %q", dep, paths)
741 }
742 return android.OptionalPathForPath(paths[0]), nil
743}
744
745func (paths *scopePaths) extractLatestApiPath(ctx android.ModuleContext, dep android.Module) error {
746 outputPath, err := extractSingleOptionalOutputPath(dep)
747 paths.latestApiPath = outputPath
748 return err
749}
750
751func (paths *scopePaths) extractLatestRemovedApiPath(ctx android.ModuleContext, dep android.Module) error {
752 outputPath, err := extractSingleOptionalOutputPath(dep)
753 paths.latestRemovedApiPath = outputPath
754 return err
755}
756
Paul Duffin0ff08bd2020-04-29 13:30:54 +0100757type commonToSdkLibraryAndImportProperties struct {
Paul Duffin1b1e8062020-05-08 13:44:43 +0100758 // The naming scheme to use for the components that this module creates.
759 //
Paul Duffinee9ad5d2020-09-11 13:04:05 +0100760 // If not specified then it defaults to "default".
Paul Duffin1b1e8062020-05-08 13:44:43 +0100761 //
762 // This is a temporary mechanism to simplify conversion from separate modules for each
763 // component that follow a different naming pattern to the default one.
764 //
765 // TODO(b/155480189) - Remove once naming inconsistencies have been resolved.
Paul Duffin0ff08bd2020-04-29 13:30:54 +0100766 Naming_scheme *string
Paul Duffindfa131e2020-05-15 20:37:11 +0100767
768 // Specifies whether this module can be used as an Android shared library; defaults
769 // to true.
770 //
771 // An Android shared library is one that can be referenced in a <uses-library> element
772 // in an AndroidManifest.xml.
773 Shared_library *bool
Paul Duffina2ae7e02020-09-11 11:55:00 +0100774
775 // Files containing information about supported java doc tags.
776 Doctag_files []string `android:"path"`
Pedro Loureiro9956e5e2021-09-07 17:21:59 +0000777
778 // Signals that this shared library is part of the bootclasspath starting
779 // on the version indicated in this attribute.
780 //
781 // This will make platforms at this level and above to ignore
782 // <uses-library> tags with this library name because the library is already
783 // available
784 On_bootclasspath_since *string
785
786 // Signals that this shared library was part of the bootclasspath before
787 // (but not including) the version indicated in this attribute.
788 //
789 // The system will automatically add a <uses-library> tag with this library to
790 // apps that target any SDK less than the version indicated in this attribute.
791 On_bootclasspath_before *string
792
793 // Indicates that PackageManager should ignore this shared library if the
794 // platform is below the version indicated in this attribute.
795 //
796 // This means that the device won't recognise this library as installed.
797 Min_device_sdk *string
798
799 // Indicates that PackageManager should ignore this shared library if the
800 // platform is above the version indicated in this attribute.
801 //
802 // This means that the device won't recognise this library as installed.
803 Max_device_sdk *string
Paul Duffin0ff08bd2020-04-29 13:30:54 +0100804}
805
Paul Duffin71b33cc2021-06-23 11:39:47 +0100806// commonSdkLibraryAndImportModule defines the interface that must be provided by a module that
807// embeds the commonToSdkLibraryAndImport struct.
808type commonSdkLibraryAndImportModule interface {
Paul Duffind796f6f2022-11-23 23:06:05 +0000809 android.Module
Paul Duffin71b33cc2021-06-23 11:39:47 +0100810
811 BaseModuleName() string
812}
813
Paul Duffin56d44902020-01-31 13:36:25 +0000814// Common code between sdk library and sdk library import
815type commonToSdkLibraryAndImport struct {
Paul Duffin71b33cc2021-06-23 11:39:47 +0100816 module commonSdkLibraryAndImportModule
Paul Duffinc3091c82020-05-08 14:16:20 +0100817
Paul Duffin56d44902020-01-31 13:36:25 +0000818 scopePaths map[*apiScope]*scopePaths
Paul Duffin1b1e8062020-05-08 13:44:43 +0100819
820 namingScheme sdkLibraryComponentNamingScheme
821
Paul Duffindfa131e2020-05-15 20:37:11 +0100822 commonSdkLibraryProperties commonToSdkLibraryAndImportProperties
Paul Duffin859fe962020-05-15 10:20:31 +0100823
Paul Duffina2ae7e02020-09-11 11:55:00 +0100824 // Paths to commonSdkLibraryProperties.Doctag_files
825 doctagPaths android.Paths
826
Paul Duffin859fe962020-05-15 10:20:31 +0100827 // Functionality related to this being used as a component of a java_sdk_library.
828 EmbeddableSdkLibraryComponent
Paul Duffin56d44902020-01-31 13:36:25 +0000829}
830
Paul Duffin71b33cc2021-06-23 11:39:47 +0100831func (c *commonToSdkLibraryAndImport) initCommon(module commonSdkLibraryAndImportModule) {
832 c.module = module
Paul Duffin1b1e8062020-05-08 13:44:43 +0100833
Paul Duffin71b33cc2021-06-23 11:39:47 +0100834 module.AddProperties(&c.commonSdkLibraryProperties)
Paul Duffin859fe962020-05-15 10:20:31 +0100835
836 // Initialize this as an sdk library component.
Paul Duffin71b33cc2021-06-23 11:39:47 +0100837 c.initSdkLibraryComponent(module)
Paul Duffin1b1e8062020-05-08 13:44:43 +0100838}
839
840func (c *commonToSdkLibraryAndImport) initCommonAfterDefaultsApplied(ctx android.DefaultableHookContext) bool {
Paul Duffindfa131e2020-05-15 20:37:11 +0100841 schemeProperty := proptools.StringDefault(c.commonSdkLibraryProperties.Naming_scheme, "default")
Paul Duffin1b1e8062020-05-08 13:44:43 +0100842 switch schemeProperty {
843 case "default":
844 c.namingScheme = &defaultNamingScheme{}
845 default:
846 ctx.PropertyErrorf("naming_scheme", "expected 'default' but was %q", schemeProperty)
847 return false
848 }
849
Paul Duffin3f0290e2021-06-30 18:25:36 +0100850 namePtr := proptools.StringPtr(c.module.BaseModuleName())
851 c.sdkLibraryComponentProperties.SdkLibraryName = namePtr
852
Paul Duffindfa131e2020-05-15 20:37:11 +0100853 // Only track this sdk library if this can be used as a shared library.
854 if c.sharedLibrary() {
855 // Use the name specified in the module definition as the owner.
Paul Duffin3f0290e2021-06-30 18:25:36 +0100856 c.sdkLibraryComponentProperties.SdkLibraryToImplicitlyTrack = namePtr
Paul Duffindfa131e2020-05-15 20:37:11 +0100857 }
Paul Duffin859fe962020-05-15 10:20:31 +0100858
Paul Duffin1b1e8062020-05-08 13:44:43 +0100859 return true
Paul Duffinc3091c82020-05-08 14:16:20 +0100860}
861
Paul Duffinea8f8082021-06-24 13:25:57 +0100862// uniqueApexVariations provides common implementation of the ApexModule.UniqueApexVariations
863// method.
864func (c *commonToSdkLibraryAndImport) uniqueApexVariations() bool {
865 // A java_sdk_library that is a shared library produces an XML file that makes the shared library
866 // usable from an AndroidManifest.xml's <uses-library> entry. That XML file contains the name of
867 // the APEX and so it needs a unique variation per APEX.
868 return c.sharedLibrary()
869}
870
Paul Duffina2ae7e02020-09-11 11:55:00 +0100871func (c *commonToSdkLibraryAndImport) generateCommonBuildActions(ctx android.ModuleContext) {
872 c.doctagPaths = android.PathsForModuleSrc(ctx, c.commonSdkLibraryProperties.Doctag_files)
873}
874
Paul Duffineedc5d52020-06-12 17:46:39 +0100875// Module name of the runtime implementation library
876func (c *commonToSdkLibraryAndImport) implLibraryModuleName() string {
Paul Duffin71b33cc2021-06-23 11:39:47 +0100877 return c.module.BaseModuleName() + ".impl"
Paul Duffineedc5d52020-06-12 17:46:39 +0100878}
879
880// Module name of the XML file for the lib
881func (c *commonToSdkLibraryAndImport) xmlPermissionsModuleName() string {
Paul Duffin71b33cc2021-06-23 11:39:47 +0100882 return c.module.BaseModuleName() + sdkXmlFileSuffix
Paul Duffineedc5d52020-06-12 17:46:39 +0100883}
884
Paul Duffinc3091c82020-05-08 14:16:20 +0100885// Name of the java_library module that compiles the stubs source.
886func (c *commonToSdkLibraryAndImport) stubsLibraryModuleName(apiScope *apiScope) string {
Paul Duffinb97b1572021-04-29 21:50:40 +0100887 baseName := c.module.BaseModuleName()
Paul Duffin21787622022-11-25 12:48:20 +0000888 return c.namingScheme.stubsLibraryModuleName(apiScope, baseName)
Paul Duffinc3091c82020-05-08 14:16:20 +0100889}
890
891// Name of the droidstubs module that generates the stubs source and may also
892// generate/check the API.
893func (c *commonToSdkLibraryAndImport) stubsSourceModuleName(apiScope *apiScope) string {
Paul Duffinb97b1572021-04-29 21:50:40 +0100894 baseName := c.module.BaseModuleName()
Paul Duffin21787622022-11-25 12:48:20 +0000895 return c.namingScheme.stubsSourceModuleName(apiScope, baseName)
Paul Duffinc3091c82020-05-08 14:16:20 +0100896}
897
Jihoon Kang1c92c3e2023-03-23 17:44:51 +0000898// Name of the java_api_library module that generates the from-text stubs source
899// and compiles to a jar file.
900func (c *commonToSdkLibraryAndImport) apiLibraryModuleName(apiScope *apiScope) string {
901 baseName := c.module.BaseModuleName()
902 return c.namingScheme.apiLibraryModuleName(apiScope, baseName)
903}
904
Jihoon Kang1147b312023-06-08 23:25:57 +0000905// Name of the java_library module that compiles the stubs
906// generated from source Java files.
907func (c *commonToSdkLibraryAndImport) sourceStubLibraryModuleName(apiScope *apiScope) string {
908 baseName := c.module.BaseModuleName()
909 return c.namingScheme.sourceStubLibraryModuleName(apiScope, baseName)
910}
911
Paul Duffin46dc45a2020-05-14 15:39:10 +0100912// The component names for different outputs of the java_sdk_library.
913//
914// They are similar to the names used for the child modules it creates
915const (
916 stubsSourceComponentName = "stubs.source"
917
918 apiTxtComponentName = "api.txt"
919
920 removedApiTxtComponentName = "removed-api.txt"
Anton Hanssond78eb762021-09-21 15:25:12 +0100921
922 annotationsComponentName = "annotations.zip"
Paul Duffin46dc45a2020-05-14 15:39:10 +0100923)
924
925// A regular expression to match tags that reference a specific stubs component.
926//
927// It will only match if given a valid scope and a valid component. It is verfy strict
928// to ensure it does not accidentally match a similar looking tag that should be processed
929// by the embedded Library.
930var tagSplitter = func() *regexp.Regexp {
931 // Given a list of literal string items returns a regular expression that will
932 // match any one of the items.
933 choice := func(items ...string) string {
934 return `\Q` + strings.Join(items, `\E|\Q`) + `\E`
935 }
936
937 // Regular expression to match one of the scopes.
938 scopesRegexp := choice(allScopeNames...)
939
940 // Regular expression to match one of the components.
Anton Hanssond78eb762021-09-21 15:25:12 +0100941 componentsRegexp := choice(stubsSourceComponentName, apiTxtComponentName, removedApiTxtComponentName, annotationsComponentName)
Paul Duffin46dc45a2020-05-14 15:39:10 +0100942
943 // Regular expression to match any combination of one scope and one component.
944 return regexp.MustCompile(fmt.Sprintf(`^\.(%s)\.(%s)$`, scopesRegexp, componentsRegexp))
945}()
946
947// For OutputFileProducer interface
948//
Anton Hanssond78eb762021-09-21 15:25:12 +0100949// .<scope>.<component name>, for all ComponentNames (for example: .public.removed-api.txt)
Paul Duffin46dc45a2020-05-14 15:39:10 +0100950func (c *commonToSdkLibraryAndImport) commonOutputFiles(tag string) (android.Paths, error) {
951 if groups := tagSplitter.FindStringSubmatch(tag); groups != nil {
952 scopeName := groups[1]
953 component := groups[2]
954
955 if scope, ok := scopeByName[scopeName]; ok {
956 paths := c.findScopePaths(scope)
957 if paths == nil {
Paul Duffin71b33cc2021-06-23 11:39:47 +0100958 return nil, fmt.Errorf("%q does not provide api scope %s", c.module.BaseModuleName(), scopeName)
Paul Duffin46dc45a2020-05-14 15:39:10 +0100959 }
960
961 switch component {
962 case stubsSourceComponentName:
963 if paths.stubsSrcJar.Valid() {
964 return android.Paths{paths.stubsSrcJar.Path()}, nil
965 }
966
967 case apiTxtComponentName:
968 if paths.currentApiFilePath.Valid() {
969 return android.Paths{paths.currentApiFilePath.Path()}, nil
970 }
971
972 case removedApiTxtComponentName:
973 if paths.removedApiFilePath.Valid() {
974 return android.Paths{paths.removedApiFilePath.Path()}, nil
975 }
Anton Hanssond78eb762021-09-21 15:25:12 +0100976
977 case annotationsComponentName:
978 if paths.annotationsZip.Valid() {
979 return android.Paths{paths.annotationsZip.Path()}, nil
980 }
Paul Duffin46dc45a2020-05-14 15:39:10 +0100981 }
982
983 return nil, fmt.Errorf("%s not available for api scope %s", component, scopeName)
984 } else {
985 return nil, fmt.Errorf("unknown scope %s in %s", scope, tag)
986 }
987
988 } else {
Paul Duffina2ae7e02020-09-11 11:55:00 +0100989 switch tag {
990 case ".doctags":
991 if c.doctagPaths != nil {
992 return c.doctagPaths, nil
993 } else {
Paul Duffin71b33cc2021-06-23 11:39:47 +0100994 return nil, fmt.Errorf("no doctag_files specified on %s", c.module.BaseModuleName())
Paul Duffina2ae7e02020-09-11 11:55:00 +0100995 }
996 }
Paul Duffin46dc45a2020-05-14 15:39:10 +0100997 return nil, nil
998 }
999}
1000
Paul Duffin803a9562020-05-20 11:52:25 +01001001func (c *commonToSdkLibraryAndImport) getScopePathsCreateIfNeeded(scope *apiScope) *scopePaths {
Paul Duffin56d44902020-01-31 13:36:25 +00001002 if c.scopePaths == nil {
1003 c.scopePaths = make(map[*apiScope]*scopePaths)
1004 }
1005 paths := c.scopePaths[scope]
1006 if paths == nil {
1007 paths = &scopePaths{}
1008 c.scopePaths[scope] = paths
1009 }
1010
1011 return paths
1012}
1013
Paul Duffin803a9562020-05-20 11:52:25 +01001014func (c *commonToSdkLibraryAndImport) findScopePaths(scope *apiScope) *scopePaths {
1015 if c.scopePaths == nil {
1016 return nil
1017 }
1018
1019 return c.scopePaths[scope]
1020}
1021
1022// If this does not support the requested api scope then find the closest available
1023// scope it does support. Returns nil if no such scope is available.
1024func (c *commonToSdkLibraryAndImport) findClosestScopePath(scope *apiScope) *scopePaths {
Paul Duffind0b9fca2022-09-30 18:11:41 +01001025 for s := scope; s != nil; s = s.canAccess {
Paul Duffin803a9562020-05-20 11:52:25 +01001026 if paths := c.findScopePaths(s); paths != nil {
1027 return paths
1028 }
1029 }
1030
1031 // This should never happen outside tests as public should be the base scope for every
1032 // scope and is enabled by default.
1033 return nil
1034}
1035
Jiyong Parkf1691d22021-03-29 20:11:58 +09001036func (c *commonToSdkLibraryAndImport) selectHeaderJarsForSdkVersion(ctx android.BaseModuleContext, sdkVersion android.SdkSpec) android.Paths {
Paul Duffinb05d4292020-05-20 12:19:10 +01001037
1038 // If a specific numeric version has been requested then use prebuilt versions of the sdk.
Jiyong Park54105c42021-03-31 18:17:53 +09001039 if !sdkVersion.ApiLevel.IsPreview() {
Paul Duffin71b33cc2021-06-23 11:39:47 +01001040 return PrebuiltJars(ctx, c.module.BaseModuleName(), sdkVersion)
Paul Duffinb05d4292020-05-20 12:19:10 +01001041 }
1042
Paul Duffin1267d872021-04-16 17:21:36 +01001043 paths := c.selectScopePaths(ctx, sdkVersion.Kind)
1044 if paths == nil {
1045 return nil
1046 }
1047
1048 return paths.stubsHeaderPath
1049}
1050
1051// selectScopePaths returns the *scopePaths appropriate for the specific kind.
1052//
1053// If the module does not support the specific kind then it will return the *scopePaths for the
1054// closest kind which is a subset of the requested kind. e.g. if requesting android.SdkModule then
1055// it will return *scopePaths for android.SdkSystem if available or android.SdkPublic of not.
1056func (c *commonToSdkLibraryAndImport) selectScopePaths(ctx android.BaseModuleContext, kind android.SdkKind) *scopePaths {
Paul Duffin32cf58a2021-05-18 16:32:50 +01001057 apiScope := sdkKindToApiScope(kind)
Paul Duffinb05d4292020-05-20 12:19:10 +01001058
Paul Duffin803a9562020-05-20 11:52:25 +01001059 paths := c.findClosestScopePath(apiScope)
1060 if paths == nil {
1061 var scopes []string
1062 for _, s := range allApiScopes {
1063 if c.findScopePaths(s) != nil {
1064 scopes = append(scopes, s.name)
1065 }
1066 }
Paul Duffin71b33cc2021-06-23 11:39:47 +01001067 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 +01001068 return nil
1069 }
1070
Paul Duffin1267d872021-04-16 17:21:36 +01001071 return paths
1072}
1073
Paul Duffin32cf58a2021-05-18 16:32:50 +01001074// sdkKindToApiScope maps from android.SdkKind to apiScope.
1075func sdkKindToApiScope(kind android.SdkKind) *apiScope {
1076 var apiScope *apiScope
1077 switch kind {
1078 case android.SdkSystem:
1079 apiScope = apiScopeSystem
1080 case android.SdkModule:
1081 apiScope = apiScopeModuleLib
1082 case android.SdkTest:
1083 apiScope = apiScopeTest
1084 case android.SdkSystemServer:
1085 apiScope = apiScopeSystemServer
1086 default:
1087 apiScope = apiScopePublic
1088 }
1089 return apiScope
1090}
1091
Paul Duffin1267d872021-04-16 17:21:36 +01001092// to satisfy SdkLibraryDependency interface
Martin Stjernholm8be1e6d2021-09-15 03:34:04 +01001093func (c *commonToSdkLibraryAndImport) SdkApiStubDexJar(ctx android.BaseModuleContext, kind android.SdkKind) OptionalDexJarPath {
Paul Duffin1267d872021-04-16 17:21:36 +01001094 paths := c.selectScopePaths(ctx, kind)
1095 if paths == nil {
Martin Stjernholm8be1e6d2021-09-15 03:34:04 +01001096 return makeUnsetDexJarPath()
Paul Duffin1267d872021-04-16 17:21:36 +01001097 }
1098
1099 return paths.stubsDexJarPath
Paul Duffinb05d4292020-05-20 12:19:10 +01001100}
1101
Paul Duffin32cf58a2021-05-18 16:32:50 +01001102// to satisfy SdkLibraryDependency interface
1103func (c *commonToSdkLibraryAndImport) SdkRemovedTxtFile(ctx android.BaseModuleContext, kind android.SdkKind) android.OptionalPath {
1104 apiScope := sdkKindToApiScope(kind)
1105 paths := c.findScopePaths(apiScope)
1106 if paths == nil {
1107 return android.OptionalPath{}
1108 }
1109
1110 return paths.removedApiFilePath
1111}
1112
Paul Duffin859fe962020-05-15 10:20:31 +01001113func (c *commonToSdkLibraryAndImport) sdkComponentPropertiesForChildLibrary() interface{} {
1114 componentProps := &struct {
Paul Duffin3f0290e2021-06-30 18:25:36 +01001115 SdkLibraryName *string
Paul Duffin859fe962020-05-15 10:20:31 +01001116 SdkLibraryToImplicitlyTrack *string
Paul Duffindfa131e2020-05-15 20:37:11 +01001117 }{}
1118
Paul Duffin3f0290e2021-06-30 18:25:36 +01001119 namePtr := proptools.StringPtr(c.module.BaseModuleName())
1120 componentProps.SdkLibraryName = namePtr
1121
Paul Duffindfa131e2020-05-15 20:37:11 +01001122 if c.sharedLibrary() {
Paul Duffin859fe962020-05-15 10:20:31 +01001123 // Mark the stubs library as being components of this java_sdk_library so that
1124 // any app that includes code which depends (directly or indirectly) on the stubs
1125 // library will have the appropriate <uses-library> invocation inserted into its
1126 // manifest if necessary.
Paul Duffin3f0290e2021-06-30 18:25:36 +01001127 componentProps.SdkLibraryToImplicitlyTrack = namePtr
Paul Duffin859fe962020-05-15 10:20:31 +01001128 }
1129
1130 return componentProps
1131}
1132
Paul Duffindfa131e2020-05-15 20:37:11 +01001133func (c *commonToSdkLibraryAndImport) sharedLibrary() bool {
1134 return proptools.BoolDefault(c.commonSdkLibraryProperties.Shared_library, true)
1135}
1136
Paul Duffinf4600f62021-05-13 22:34:45 +01001137// Check if the stub libraries should be compiled for dex
1138func (c *commonToSdkLibraryAndImport) stubLibrariesCompiledForDex() bool {
1139 // Always compile the dex file files for the stub libraries if they will be used on the
1140 // bootclasspath.
1141 return !c.sharedLibrary()
1142}
1143
Paul Duffin859fe962020-05-15 10:20:31 +01001144// Properties related to the use of a module as an component of a java_sdk_library.
1145type SdkLibraryComponentProperties struct {
Paul Duffin3f0290e2021-06-30 18:25:36 +01001146 // The name of the java_sdk_library/_import module.
1147 SdkLibraryName *string `blueprint:"mutated"`
Paul Duffin859fe962020-05-15 10:20:31 +01001148
1149 // The name of the java_sdk_library/_import to add to a <uses-library> entry
1150 // in the AndroidManifest.xml of any Android app that includes code that references
1151 // this module. If not set then no java_sdk_library/_import is tracked.
1152 SdkLibraryToImplicitlyTrack *string `blueprint:"mutated"`
1153}
1154
1155// Structure to be embedded in a module struct that needs to support the
1156// SdkLibraryComponentDependency interface.
1157type EmbeddableSdkLibraryComponent struct {
1158 sdkLibraryComponentProperties SdkLibraryComponentProperties
1159}
1160
Paul Duffin71b33cc2021-06-23 11:39:47 +01001161func (e *EmbeddableSdkLibraryComponent) initSdkLibraryComponent(module android.Module) {
1162 module.AddProperties(&e.sdkLibraryComponentProperties)
Paul Duffin859fe962020-05-15 10:20:31 +01001163}
1164
1165// to satisfy SdkLibraryComponentDependency
Paul Duffin3f0290e2021-06-30 18:25:36 +01001166func (e *EmbeddableSdkLibraryComponent) SdkLibraryName() *string {
1167 return e.sdkLibraryComponentProperties.SdkLibraryName
1168}
1169
1170// to satisfy SdkLibraryComponentDependency
Ulya Trafimovich39b437b2020-09-23 16:42:35 +01001171func (e *EmbeddableSdkLibraryComponent) OptionalSdkLibraryImplementation() *string {
Ulya Trafimovich78645fb2021-07-16 15:29:25 +01001172 // For shared libraries, this is the same as the SDK library name. If a Java library or app
1173 // depends on a component library (e.g. a stub library) it still needs to know the name of the
1174 // run-time library and the corresponding module that provides the implementation. This name is
1175 // passed to manifest_fixer (to be added to AndroidManifest.xml) and added to CLC (to be used
1176 // in dexpreopt).
1177 //
1178 // For non-shared SDK (component or not) libraries this returns `nil`, as they are not
1179 // <uses-library> and should not be added to the manifest or to CLC.
Ulya Trafimovich39b437b2020-09-23 16:42:35 +01001180 return e.sdkLibraryComponentProperties.SdkLibraryToImplicitlyTrack
1181}
1182
Paul Duffin859fe962020-05-15 10:20:31 +01001183// Implemented by modules that are (or possibly could be) a component of a java_sdk_library
1184// (including the java_sdk_library) itself.
1185type SdkLibraryComponentDependency interface {
Ulya Trafimovich31e444e2020-08-14 17:32:16 +01001186 UsesLibraryDependency
1187
Paul Duffin3f0290e2021-06-30 18:25:36 +01001188 // SdkLibraryName returns the name of the java_sdk_library/_import module.
1189 SdkLibraryName() *string
1190
Ulya Trafimovich39b437b2020-09-23 16:42:35 +01001191 // The name of the implementation library for the optional SDK library or nil, if there isn't one.
1192 OptionalSdkLibraryImplementation() *string
Paul Duffin859fe962020-05-15 10:20:31 +01001193}
1194
1195// Make sure that all the module types that are components of java_sdk_library/_import
1196// and which can be referenced (directly or indirectly) from an android app implement
1197// the SdkLibraryComponentDependency interface.
1198var _ SdkLibraryComponentDependency = (*Library)(nil)
1199var _ SdkLibraryComponentDependency = (*Import)(nil)
1200var _ SdkLibraryComponentDependency = (*SdkLibrary)(nil)
Paul Duffineedc5d52020-06-12 17:46:39 +01001201var _ SdkLibraryComponentDependency = (*SdkLibraryImport)(nil)
Paul Duffin859fe962020-05-15 10:20:31 +01001202
Paul Duffin32cf58a2021-05-18 16:32:50 +01001203// Provides access to sdk_version related files, e.g. header and implementation jars.
Paul Duffin859fe962020-05-15 10:20:31 +01001204type SdkLibraryDependency interface {
1205 SdkLibraryComponentDependency
1206
1207 // Get the header jars appropriate for the supplied sdk_version.
1208 //
1209 // These are turbine generated jars so they only change if the externals of the
1210 // class changes but it does not contain and implementation or JavaDoc.
Jiyong Parkf1691d22021-03-29 20:11:58 +09001211 SdkHeaderJars(ctx android.BaseModuleContext, sdkVersion android.SdkSpec) android.Paths
Paul Duffin859fe962020-05-15 10:20:31 +01001212
1213 // Get the implementation jars appropriate for the supplied sdk version.
1214 //
1215 // These are either the implementation jar for the whole sdk library or the implementation
1216 // jars for the stubs. The latter should only be needed when generating JavaDoc as otherwise
1217 // they are identical to the corresponding header jars.
Jiyong Parkf1691d22021-03-29 20:11:58 +09001218 SdkImplementationJars(ctx android.BaseModuleContext, sdkVersion android.SdkSpec) android.Paths
Paul Duffin1267d872021-04-16 17:21:36 +01001219
1220 // SdkApiStubDexJar returns the dex jar for the stubs. It is needed by the hiddenapi processing
1221 // tool which processes dex files.
Martin Stjernholm8be1e6d2021-09-15 03:34:04 +01001222 SdkApiStubDexJar(ctx android.BaseModuleContext, kind android.SdkKind) OptionalDexJarPath
Paul Duffinf4600f62021-05-13 22:34:45 +01001223
Paul Duffin32cf58a2021-05-18 16:32:50 +01001224 // SdkRemovedTxtFile returns the optional path to the removed.txt file for the specified sdk kind.
1225 SdkRemovedTxtFile(ctx android.BaseModuleContext, kind android.SdkKind) android.OptionalPath
1226
Paul Duffinf4600f62021-05-13 22:34:45 +01001227 // sharedLibrary returns true if this can be used as a shared library.
1228 sharedLibrary() bool
Paul Duffin859fe962020-05-15 10:20:31 +01001229}
1230
Inseob Kimc0907f12019-02-08 21:00:45 +09001231type SdkLibrary struct {
Sundong Ahn054b19a2018-10-19 13:46:09 +09001232 Library
Jiyong Parkc678ad32018-04-10 13:07:10 +09001233
Zi Wangb2179e32023-01-31 15:53:30 -08001234 android.BazelModuleBase
1235
Sundong Ahn054b19a2018-10-19 13:46:09 +09001236 sdkLibraryProperties sdkLibraryProperties
Jiyong Parkc678ad32018-04-10 13:07:10 +09001237
Paul Duffin3375e352020-04-28 10:44:03 +01001238 // Map from api scope to the scope specific property structure.
1239 scopeToProperties map[*apiScope]*ApiScopeProperties
1240
Paul Duffin56d44902020-01-31 13:36:25 +00001241 commonToSdkLibraryAndImport
Jiyong Parkc678ad32018-04-10 13:07:10 +09001242}
1243
Inseob Kimc0907f12019-02-08 21:00:45 +09001244var _ SdkLibraryDependency = (*SdkLibrary)(nil)
Colin Cross897d2ed2019-02-11 14:03:51 -08001245
Paul Duffin3375e352020-04-28 10:44:03 +01001246func (module *SdkLibrary) generateTestAndSystemScopesByDefault() bool {
1247 return module.sdkLibraryProperties.Generate_system_and_test_apis
1248}
1249
1250func (module *SdkLibrary) getGeneratedApiScopes(ctx android.EarlyModuleContext) apiScopes {
1251 // Check to see if any scopes have been explicitly enabled. If any have then all
1252 // must be.
1253 anyScopesExplicitlyEnabled := false
1254 for _, scope := range allApiScopes {
1255 scopeProperties := module.scopeToProperties[scope]
1256 if scopeProperties.Enabled != nil {
1257 anyScopesExplicitlyEnabled = true
1258 break
1259 }
Paul Duffind1b3a922020-01-22 11:57:20 +00001260 }
Paul Duffin3375e352020-04-28 10:44:03 +01001261
1262 var generatedScopes apiScopes
1263 enabledScopes := make(map[*apiScope]struct{})
1264 for _, scope := range allApiScopes {
1265 scopeProperties := module.scopeToProperties[scope]
1266 // If any scopes are explicitly enabled then ignore the legacy enabled status.
1267 // This is to ensure that any new usages of this module type do not rely on legacy
1268 // behaviour.
1269 defaultEnabledStatus := false
1270 if anyScopesExplicitlyEnabled {
1271 defaultEnabledStatus = scope.defaultEnabledStatus
1272 } else {
1273 defaultEnabledStatus = scope.legacyEnabledStatus(module)
1274 }
1275 enabled := proptools.BoolDefault(scopeProperties.Enabled, defaultEnabledStatus)
1276 if enabled {
1277 enabledScopes[scope] = struct{}{}
1278 generatedScopes = append(generatedScopes, scope)
1279 }
1280 }
1281
1282 // Now check to make sure that any scope that is extended by an enabled scope is also
1283 // enabled.
1284 for _, scope := range allApiScopes {
1285 if _, ok := enabledScopes[scope]; ok {
1286 extends := scope.extends
1287 if extends != nil {
1288 if _, ok := enabledScopes[extends]; !ok {
1289 ctx.ModuleErrorf("enabled api scope %q depends on disabled scope %q", scope, extends)
1290 }
1291 }
1292 }
1293 }
1294
1295 return generatedScopes
Paul Duffind1b3a922020-01-22 11:57:20 +00001296}
1297
satayev758968a2021-12-06 11:42:40 +00001298var _ android.ModuleWithMinSdkVersionCheck = (*SdkLibrary)(nil)
1299
satayev8f088b02021-12-06 11:40:46 +00001300func (module *SdkLibrary) CheckMinSdkVersion(ctx android.ModuleContext) {
Spandan Das8c9ae7e2023-03-03 21:20:36 +00001301 android.CheckMinSdkVersion(ctx, module.MinSdkVersion(ctx), func(c android.ModuleContext, do android.PayloadDepsCallback) {
satayev8f088b02021-12-06 11:40:46 +00001302 ctx.WalkDeps(func(child android.Module, parent android.Module) bool {
1303 isExternal := !module.depIsInSameApex(ctx, child)
1304 if am, ok := child.(android.ApexModule); ok {
1305 if !do(ctx, parent, am, isExternal) {
1306 return false
1307 }
1308 }
1309 return !isExternal
1310 })
1311 })
1312}
1313
Paul Duffineedc5d52020-06-12 17:46:39 +01001314type sdkLibraryComponentTag struct {
1315 blueprint.BaseDependencyTag
1316 name string
1317}
1318
1319// Mark this tag so dependencies that use it are excluded from visibility enforcement.
1320func (t sdkLibraryComponentTag) ExcludeFromVisibilityEnforcement() {}
1321
1322var xmlPermissionsFileTag = sdkLibraryComponentTag{name: "xml-permissions-file"}
Paul Duffine74ac732020-02-06 13:51:46 +00001323
Jiyong Parke3833882020-02-17 17:28:10 +09001324func IsXmlPermissionsFileDepTag(depTag blueprint.DependencyTag) bool {
Paul Duffineedc5d52020-06-12 17:46:39 +01001325 if dt, ok := depTag.(sdkLibraryComponentTag); ok {
Jiyong Parke3833882020-02-17 17:28:10 +09001326 return dt == xmlPermissionsFileTag
1327 }
1328 return false
1329}
1330
Paul Duffineedc5d52020-06-12 17:46:39 +01001331var implLibraryTag = sdkLibraryComponentTag{name: "impl-library"}
Paul Duffin5df79302020-05-16 15:52:12 +01001332
Paul Duffin44f1d842020-06-26 20:17:02 +01001333// Add the dependencies on the child modules in the component deps mutator.
1334func (module *SdkLibrary) ComponentDepsMutator(ctx android.BottomUpMutatorContext) {
Paul Duffin3375e352020-04-28 10:44:03 +01001335 for _, apiScope := range module.getGeneratedApiScopes(ctx) {
Paul Duffind1b3a922020-01-22 11:57:20 +00001336 // Add dependencies to the stubs library
Spandan Das877f39d2023-03-29 16:19:51 +00001337 stubModuleName := module.stubsLibraryModuleName(apiScope)
Jihoon Kang1147b312023-06-08 23:25:57 +00001338
Spandan Das877f39d2023-03-29 16:19:51 +00001339 ctx.AddVariationDependencies(nil, apiScope.stubsTag, stubModuleName)
Paul Duffind1b3a922020-01-22 11:57:20 +00001340
Paul Duffin15f34ef2020-07-20 18:04:44 +01001341 // Add a dependency on the stubs source in order to access both stubs source and api information.
1342 ctx.AddVariationDependencies(nil, apiScope.stubsSourceAndApiTag, module.stubsSourceModuleName(apiScope))
Paul Duffin958806b2022-05-16 13:10:47 +00001343
1344 if module.compareAgainstLatestApi(apiScope) {
1345 // Add dependencies on the latest finalized version of the API .txt file.
1346 latestApiModuleName := module.latestApiModuleName(apiScope)
1347 ctx.AddDependency(module, apiScope.latestApiModuleTag, latestApiModuleName)
1348
1349 // Add dependencies on the latest finalized version of the remove API .txt file.
1350 latestRemovedApiModuleName := module.latestRemovedApiModuleName(apiScope)
1351 ctx.AddDependency(module, apiScope.latestRemovedApiModuleTag, latestRemovedApiModuleName)
1352 }
Sundong Ahn054b19a2018-10-19 13:46:09 +09001353 }
1354
Paul Duffindfa131e2020-05-15 20:37:11 +01001355 if module.requiresRuntimeImplementationLibrary() {
Paul Duffin5df79302020-05-16 15:52:12 +01001356 // Add dependency to the rule for generating the implementation library.
1357 ctx.AddDependency(module, implLibraryTag, module.implLibraryModuleName())
1358
Paul Duffindfa131e2020-05-15 20:37:11 +01001359 if module.sharedLibrary() {
1360 // Add dependency to the rule for generating the xml permissions file
Paul Duffineedc5d52020-06-12 17:46:39 +01001361 ctx.AddDependency(module, xmlPermissionsFileTag, module.xmlPermissionsModuleName())
Paul Duffindfa131e2020-05-15 20:37:11 +01001362 }
Paul Duffin44f1d842020-06-26 20:17:02 +01001363 }
1364}
Paul Duffine74ac732020-02-06 13:51:46 +00001365
Paul Duffin44f1d842020-06-26 20:17:02 +01001366// Add other dependencies as normal.
1367func (module *SdkLibrary) DepsMutator(ctx android.BottomUpMutatorContext) {
Anton Hanssone77fccc2021-01-20 16:52:41 +00001368 var missingApiModules []string
1369 for _, apiScope := range module.getGeneratedApiScopes(ctx) {
1370 if apiScope.unstable {
1371 continue
1372 }
Paul Duffin958806b2022-05-16 13:10:47 +00001373 if m := module.latestApiModuleName(apiScope); !ctx.OtherModuleExists(m) {
Anton Hanssone77fccc2021-01-20 16:52:41 +00001374 missingApiModules = append(missingApiModules, m)
1375 }
Paul Duffin958806b2022-05-16 13:10:47 +00001376 if m := module.latestRemovedApiModuleName(apiScope); !ctx.OtherModuleExists(m) {
Anton Hanssone77fccc2021-01-20 16:52:41 +00001377 missingApiModules = append(missingApiModules, m)
1378 }
Paul Duffin958806b2022-05-16 13:10:47 +00001379 if m := module.latestIncompatibilitiesModuleName(apiScope); !ctx.OtherModuleExists(m) {
Jaewoong Jung1a97ee02021-03-09 13:25:02 -08001380 missingApiModules = append(missingApiModules, m)
1381 }
Anton Hanssone77fccc2021-01-20 16:52:41 +00001382 }
1383 if len(missingApiModules) != 0 && !module.sdkLibraryProperties.Unsafe_ignore_missing_latest_api {
1384 m := module.Name() + " is missing tracking files for previously released library versions.\n"
1385 m += "You need to do one of the following:\n"
1386 m += "- Add `unsafe_ignore_missing_latest_api: true` to your blueprint (to disable compat tracking)\n"
1387 m += "- Add a set of prebuilt txt files representing the last released version of this library for compat checking.\n"
1388 m += " (the current set of API files can be used as a seed for this compatibility tracking\n"
1389 m += "\n"
1390 m += "The following filegroup modules are missing:\n "
1391 m += strings.Join(missingApiModules, "\n ") + "\n"
1392 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."
1393 ctx.ModuleErrorf(m)
1394 }
Paul Duffin44f1d842020-06-26 20:17:02 +01001395 if module.requiresRuntimeImplementationLibrary() {
Paul Duffindfa131e2020-05-15 20:37:11 +01001396 // Only add the deps for the library if it is actually going to be built.
1397 module.Library.deps(ctx)
1398 }
Jiyong Parkc678ad32018-04-10 13:07:10 +09001399}
1400
Paul Duffin46dc45a2020-05-14 15:39:10 +01001401func (module *SdkLibrary) OutputFiles(tag string) (android.Paths, error) {
1402 paths, err := module.commonOutputFiles(tag)
Colin Cross4acaea92021-12-10 23:05:02 +00001403 if paths != nil || err != nil {
Paul Duffin46dc45a2020-05-14 15:39:10 +01001404 return paths, err
1405 }
Colin Cross4acaea92021-12-10 23:05:02 +00001406 if module.requiresRuntimeImplementationLibrary() {
1407 return module.Library.OutputFiles(tag)
1408 }
1409 if tag == "" {
1410 return nil, nil
1411 }
1412 return nil, fmt.Errorf("unsupported module reference tag %q", tag)
Paul Duffin46dc45a2020-05-14 15:39:10 +01001413}
1414
Inseob Kimc0907f12019-02-08 21:00:45 +09001415func (module *SdkLibrary) GenerateAndroidBuildActions(ctx android.ModuleContext) {
satayev8f088b02021-12-06 11:40:46 +00001416 if proptools.String(module.deviceProperties.Min_sdk_version) != "" {
1417 module.CheckMinSdkVersion(ctx)
1418 }
1419
Paul Duffina2ae7e02020-09-11 11:55:00 +01001420 module.generateCommonBuildActions(ctx)
1421
Paul Duffindfa131e2020-05-15 20:37:11 +01001422 // Only build an implementation library if required.
1423 if module.requiresRuntimeImplementationLibrary() {
Paul Duffin43db9be2019-12-30 17:35:49 +00001424 module.Library.GenerateAndroidBuildActions(ctx)
1425 }
Sundong Ahn054b19a2018-10-19 13:46:09 +09001426
Paul Duffinb97b1572021-04-29 21:50:40 +01001427 // Collate the components exported by this module. All scope specific modules are exported but
1428 // the impl and xml component modules are not.
1429 exportedComponents := map[string]struct{}{}
1430
Sundong Ahn57368eb2018-07-06 11:20:23 +09001431 // Record the paths to the header jars of the library (stubs and impl).
Paul Duffind1b3a922020-01-22 11:57:20 +00001432 // When this java_sdk_library is depended upon from others via "libs" property,
Jiyong Parkc678ad32018-04-10 13:07:10 +09001433 // the recorded paths will be returned depending on the link type of the caller.
1434 ctx.VisitDirectDeps(func(to android.Module) {
Jiyong Parkc678ad32018-04-10 13:07:10 +09001435 tag := ctx.OtherModuleDependencyTag(to)
1436
Paul Duffinc8782502020-04-29 20:45:27 +01001437 // Extract information from any of the scope specific dependencies.
1438 if scopeTag, ok := tag.(scopeDependencyTag); ok {
1439 apiScope := scopeTag.apiScope
Paul Duffin803a9562020-05-20 11:52:25 +01001440 scopePaths := module.getScopePathsCreateIfNeeded(apiScope)
Paul Duffinc8782502020-04-29 20:45:27 +01001441
1442 // Extract information from the dependency. The exact information extracted
1443 // is determined by the nature of the dependency which is determined by the tag.
1444 scopeTag.extractDepInfo(ctx, to, scopePaths)
Paul Duffinb97b1572021-04-29 21:50:40 +01001445
1446 exportedComponents[ctx.OtherModuleName(to)] = struct{}{}
Sundong Ahn20e998b2018-07-24 11:19:26 +09001447 }
Jiyong Parkc678ad32018-04-10 13:07:10 +09001448 })
Paul Duffinb97b1572021-04-29 21:50:40 +01001449
1450 // Make the set of components exported by this module available for use elsewhere.
Cole Faust18994c72023-02-28 16:02:16 -08001451 exportedComponentInfo := android.ExportedComponentsInfo{Components: android.SortedKeys(exportedComponents)}
Paul Duffinb97b1572021-04-29 21:50:40 +01001452 ctx.SetProvider(android.ExportedComponentsInfoProvider, exportedComponentInfo)
Paul Duffin958806b2022-05-16 13:10:47 +00001453
1454 // Provide additional information for inclusion in an sdk's generated .info file.
1455 additionalSdkInfo := map[string]interface{}{}
1456 additionalSdkInfo["dist_stem"] = module.distStem()
Paul Duffine8409952022-09-22 16:24:46 +01001457 baseModuleName := module.distStem()
Paul Duffin958806b2022-05-16 13:10:47 +00001458 scopes := map[string]interface{}{}
1459 additionalSdkInfo["scopes"] = scopes
1460 for scope, scopePaths := range module.scopePaths {
1461 scopeInfo := map[string]interface{}{}
1462 scopes[scope.name] = scopeInfo
1463 scopeInfo["current_api"] = scope.snapshotRelativeCurrentApiTxtPath(baseModuleName)
1464 scopeInfo["removed_api"] = scope.snapshotRelativeRemovedApiTxtPath(baseModuleName)
1465 if p := scopePaths.latestApiPath; p.Valid() {
1466 scopeInfo["latest_api"] = p.Path().String()
1467 }
1468 if p := scopePaths.latestRemovedApiPath; p.Valid() {
1469 scopeInfo["latest_removed_api"] = p.Path().String()
1470 }
1471 }
1472 ctx.SetProvider(android.AdditionalSdkInfoProvider, android.AdditionalSdkInfo{additionalSdkInfo})
Jiyong Parkc678ad32018-04-10 13:07:10 +09001473}
1474
Jiyong Park0b0e1b92019-12-03 13:24:29 +09001475func (module *SdkLibrary) AndroidMkEntries() []android.AndroidMkEntries {
Paul Duffindfa131e2020-05-15 20:37:11 +01001476 if !module.requiresRuntimeImplementationLibrary() {
Paul Duffin43db9be2019-12-30 17:35:49 +00001477 return nil
1478 }
Jiyong Park0b0e1b92019-12-03 13:24:29 +09001479 entriesList := module.Library.AndroidMkEntries()
Yo Chiang07d75072020-06-05 17:43:19 +08001480 if module.sharedLibrary() {
1481 entries := &entriesList[0]
1482 entries.Required = append(entries.Required, module.xmlPermissionsModuleName())
1483 }
Jiyong Park0b0e1b92019-12-03 13:24:29 +09001484 return entriesList
Jiyong Park82484c02018-04-23 21:41:26 +09001485}
1486
Anton Hansson5fd5d242020-03-27 19:43:19 +00001487// The dist path of the stub artifacts
1488func (module *SdkLibrary) apiDistPath(apiScope *apiScope) string {
Colin Crossf0eace92021-06-02 13:02:23 -07001489 return path.Join("apistubs", module.distGroup(), apiScope.name)
Anton Hansson5fd5d242020-03-27 19:43:19 +00001490}
1491
Paul Duffin12ceb462019-12-24 20:31:31 +00001492// Get the sdk version for use when compiling the stubs library.
Paul Duffin780c5f42020-05-12 15:52:55 +01001493func (module *SdkLibrary) sdkVersionForStubsLibrary(mctx android.EarlyModuleContext, apiScope *apiScope) string {
Paul Duffin87a05a32020-05-12 11:50:28 +01001494 scopeProperties := module.scopeToProperties[apiScope]
1495 if scopeProperties.Sdk_version != nil {
1496 return proptools.String(scopeProperties.Sdk_version)
1497 }
1498
Jiyong Parkf1691d22021-03-29 20:11:58 +09001499 sdkDep := decodeSdkDep(mctx, android.SdkContext(&module.Library))
Paul Duffin12ceb462019-12-24 20:31:31 +00001500 if sdkDep.hasStandardLibs() {
1501 // If building against a standard sdk then use the sdk version appropriate for the scope.
Paul Duffind1b3a922020-01-22 11:57:20 +00001502 return apiScope.sdkVersion
Paul Duffin12ceb462019-12-24 20:31:31 +00001503 } else {
1504 // Otherwise, use no system module.
1505 return "none"
1506 }
1507}
1508
Paul Duffin31310252020-11-20 21:26:20 +00001509func (module *SdkLibrary) distStem() string {
1510 return proptools.StringDefault(module.sdkLibraryProperties.Dist_stem, module.BaseModuleName())
1511}
1512
Colin Cross986b69a2021-06-01 13:13:40 -07001513// distGroup returns the subdirectory of the dist path of the stub artifacts.
1514func (module *SdkLibrary) distGroup() string {
Colin Cross59b92bf2021-06-01 14:07:56 -07001515 return proptools.StringDefault(module.sdkLibraryProperties.Dist_group, "unknown")
Colin Cross986b69a2021-06-01 13:13:40 -07001516}
1517
Paul Duffin958806b2022-05-16 13:10:47 +00001518func latestPrebuiltApiModuleName(name string, apiScope *apiScope) string {
1519 return PrebuiltApiModuleName(name, apiScope.name, "latest")
1520}
1521
Paul Duffind1b3a922020-01-22 11:57:20 +00001522func (module *SdkLibrary) latestApiFilegroupName(apiScope *apiScope) string {
Paul Duffin958806b2022-05-16 13:10:47 +00001523 return ":" + module.latestApiModuleName(apiScope)
1524}
1525
1526func (module *SdkLibrary) latestApiModuleName(apiScope *apiScope) string {
1527 return latestPrebuiltApiModuleName(module.distStem(), apiScope)
Jiyong Park58c518b2018-05-12 22:29:12 +09001528}
Jiyong Parkc678ad32018-04-10 13:07:10 +09001529
Paul Duffind1b3a922020-01-22 11:57:20 +00001530func (module *SdkLibrary) latestRemovedApiFilegroupName(apiScope *apiScope) string {
Paul Duffin958806b2022-05-16 13:10:47 +00001531 return ":" + module.latestRemovedApiModuleName(apiScope)
1532}
1533
1534func (module *SdkLibrary) latestRemovedApiModuleName(apiScope *apiScope) string {
1535 return latestPrebuiltApiModuleName(module.distStem()+"-removed", apiScope)
Jiyong Parkc678ad32018-04-10 13:07:10 +09001536}
1537
Jaewoong Jung1a97ee02021-03-09 13:25:02 -08001538func (module *SdkLibrary) latestIncompatibilitiesFilegroupName(apiScope *apiScope) string {
Paul Duffin958806b2022-05-16 13:10:47 +00001539 return ":" + module.latestIncompatibilitiesModuleName(apiScope)
1540}
1541
1542func (module *SdkLibrary) latestIncompatibilitiesModuleName(apiScope *apiScope) string {
1543 return latestPrebuiltApiModuleName(module.distStem()+"-incompatibilities", apiScope)
Jaewoong Jung1a97ee02021-03-09 13:25:02 -08001544}
1545
Jihoon Kang1c92c3e2023-03-23 17:44:51 +00001546func (module *SdkLibrary) contributesToApiSurface(c android.Config) bool {
1547 _, exists := c.GetApiLibraries()[module.Name()]
1548 return exists
1549}
1550
Jihoon Kang0c705a42023-08-02 06:44:57 +00001551// The listed modules are the special java_sdk_libraries where apiScope.kind do not match the
1552// api surface that the module contribute to. For example, the public droidstubs and java_library
1553// do not contribute to the public api surface, but contributes to the core platform api surface.
1554// This method returns the full api surface stub lib that
1555// the generated java_api_library should depend on.
1556func (module *SdkLibrary) alternativeFullApiSurfaceStubLib() string {
1557 if val, ok := apiLibraryAdditionalProperties[module.Name()]; ok {
1558 return val.FullApiSurfaceStubLib
1559 }
1560 return ""
1561}
1562
1563// The listed modules' stubs contents do not match the corresponding txt files,
1564// but require additional api contributions to generate the full stubs.
1565// This method returns the name of the additional api contribution module
1566// for corresponding sdk_library modules.
1567func (module *SdkLibrary) apiLibraryAdditionalApiContribution() string {
1568 if val, ok := apiLibraryAdditionalProperties[module.Name()]; ok {
1569 return val.AdditionalApiContribution
1570 }
1571 return ""
1572}
1573
Anton Hansson944e77d2020-08-19 11:40:22 +01001574func childModuleVisibility(childVisibility []string) []string {
1575 if childVisibility == nil {
1576 // No child visibility set. The child will use the visibility of the sdk_library.
1577 return nil
1578 }
1579
1580 // Prepend an override to ignore the sdk_library's visibility, and rely on the child visibility.
1581 var visibility []string
1582 visibility = append(visibility, "//visibility:override")
1583 visibility = append(visibility, childVisibility...)
1584 return visibility
1585}
1586
Paul Duffin5df79302020-05-16 15:52:12 +01001587// Creates the implementation java library
1588func (module *SdkLibrary) createImplLibrary(mctx android.DefaultableHookContext) {
Anton Hansson944e77d2020-08-19 11:40:22 +01001589 visibility := childModuleVisibility(module.sdkLibraryProperties.Impl_library_visibility)
1590
Paul Duffin5df79302020-05-16 15:52:12 +01001591 props := struct {
Paul Duffin77590a82022-04-28 14:13:30 +00001592 Name *string
1593 Visibility []string
1594 Instrument bool
1595 Libs []string
1596 Static_libs []string
1597 Apex_available []string
Paul Duffin5df79302020-05-16 15:52:12 +01001598 }{
1599 Name: proptools.StringPtr(module.implLibraryModuleName()),
Anton Hansson944e77d2020-08-19 11:40:22 +01001600 Visibility: visibility,
Paul Duffin296cf332020-06-18 21:09:55 +01001601 // Set the instrument property to ensure it is instrumented when instrumentation is required.
1602 Instrument: true,
Anton Hansson7f66efa2020-10-08 14:47:23 +01001603 // Set the impl_only libs. Note that the module's "Libs" get appended as well, via the
1604 // addition of &module.properties below.
1605 Libs: module.sdkLibraryProperties.Impl_only_libs,
Paul Duffin77590a82022-04-28 14:13:30 +00001606 // Set the impl_only static libs. Note that the module's "static_libs" get appended as well, via the
1607 // addition of &module.properties below.
1608 Static_libs: module.sdkLibraryProperties.Impl_only_static_libs,
1609 // Pass the apex_available settings down so that the impl library can be statically
1610 // embedded within a library that is added to an APEX. Needed for updatable-media.
1611 Apex_available: module.ApexAvailable(),
Paul Duffin5df79302020-05-16 15:52:12 +01001612 }
1613
1614 properties := []interface{}{
1615 &module.properties,
1616 &module.protoProperties,
1617 &module.deviceProperties,
Liz Kammera7a64f32020-07-09 15:16:41 -07001618 &module.dexProperties,
Paul Duffin5df79302020-05-16 15:52:12 +01001619 &module.dexpreoptProperties,
Colin Cross014489c2020-06-02 20:09:13 -07001620 &module.linter.properties,
Paul Duffin5df79302020-05-16 15:52:12 +01001621 &props,
1622 module.sdkComponentPropertiesForChildLibrary(),
1623 }
1624 mctx.CreateModule(LibraryFactory, properties...)
1625}
1626
Jiyong Parkc678ad32018-04-10 13:07:10 +09001627// Creates a static java library that has API stubs
Paul Duffinf0229202020-04-29 16:47:28 +01001628func (module *SdkLibrary) createStubsLibrary(mctx android.DefaultableHookContext, apiScope *apiScope) {
Jiyong Parkc678ad32018-04-10 13:07:10 +09001629 props := struct {
Dan Willemsen9f435972020-05-28 15:28:00 -07001630 Name *string
1631 Visibility []string
1632 Srcs []string
1633 Installable *bool
1634 Sdk_version *string
1635 System_modules *string
1636 Patch_module *string
1637 Libs []string
Anton Hanssondae54cd2021-04-21 16:30:10 +01001638 Static_libs []string
Dan Willemsen9f435972020-05-28 15:28:00 -07001639 Compile_dex *bool
1640 Java_version *string
1641 Openjdk9 struct {
Sundong Ahn054b19a2018-10-19 13:46:09 +09001642 Srcs []string
1643 Javacflags []string
1644 }
Anton Hansson5fd5d242020-03-27 19:43:19 +00001645 Dist struct {
1646 Targets []string
1647 Dest *string
1648 Dir *string
1649 Tag *string
1650 }
Jiyong Parkc678ad32018-04-10 13:07:10 +09001651 }{}
1652
Jihoon Kang1147b312023-06-08 23:25:57 +00001653 props.Name = proptools.StringPtr(module.sourceStubLibraryModuleName(apiScope))
Jihoon Kang786df932023-09-07 01:18:31 +00001654 props.Visibility = []string{"//visibility:override", "//visibility:private"}
Jiyong Parkc678ad32018-04-10 13:07:10 +09001655 // sources are generated from the droiddoc
Paul Duffinc3091c82020-05-08 14:16:20 +01001656 props.Srcs = []string{":" + module.stubsSourceModuleName(apiScope)}
Paul Duffin12ceb462019-12-24 20:31:31 +00001657 sdkVersion := module.sdkVersionForStubsLibrary(mctx, apiScope)
Paul Duffin52d398a2019-06-11 12:31:14 +01001658 props.Sdk_version = proptools.StringPtr(sdkVersion)
Paul Duffina18abc22020-05-16 18:54:24 +01001659 props.System_modules = module.deviceProperties.System_modules
1660 props.Patch_module = module.properties.Patch_module
Paul Duffin367ab912019-12-23 19:40:36 +00001661 props.Installable = proptools.BoolPtr(false)
Sundong Ahn054b19a2018-10-19 13:46:09 +09001662 props.Libs = module.sdkLibraryProperties.Stub_only_libs
Mark White9421c4c2023-08-10 00:07:03 +00001663 props.Libs = append(props.Libs, module.scopeToProperties[apiScope].Libs...)
Anton Hanssondae54cd2021-04-21 16:30:10 +01001664 props.Static_libs = module.sdkLibraryProperties.Stub_only_static_libs
Paul Duffine22c2ab2020-05-20 19:35:27 +01001665 // The stub-annotations library contains special versions of the annotations
1666 // with CLASS retention policy, so that they're kept.
1667 if proptools.Bool(module.sdkLibraryProperties.Annotations_enabled) {
1668 props.Libs = append(props.Libs, "stub-annotations")
1669 }
Paul Duffina18abc22020-05-16 18:54:24 +01001670 props.Openjdk9.Srcs = module.properties.Openjdk9.Srcs
1671 props.Openjdk9.Javacflags = module.properties.Openjdk9.Javacflags
Anton Hansson83509b52020-05-21 09:21:57 +01001672 // We compile the stubs for 1.8 in line with the main android.jar stubs, and potential
1673 // interop with older developer tools that don't support 1.9.
1674 props.Java_version = proptools.StringPtr("1.8")
Paul Duffinf4600f62021-05-13 22:34:45 +01001675
Paul Duffin859fe962020-05-15 10:20:31 +01001676 mctx.CreateModule(LibraryFactory, &props, module.sdkComponentPropertiesForChildLibrary())
Jiyong Parkc678ad32018-04-10 13:07:10 +09001677}
1678
Paul Duffin6d0886e2020-04-07 18:49:53 +01001679// Creates a droidstubs module that creates stubs source files from the given full source
Paul Duffinc8782502020-04-29 20:45:27 +01001680// files and also updates and checks the API specification files.
Paul Duffin15f34ef2020-07-20 18:04:44 +01001681func (module *SdkLibrary) createStubsSourcesAndApi(mctx android.DefaultableHookContext, apiScope *apiScope, name string, scopeSpecificDroidstubsArgs []string) {
Jiyong Parkc678ad32018-04-10 13:07:10 +09001682 props := struct {
Sundong Ahn054b19a2018-10-19 13:46:09 +09001683 Name *string
Paul Duffin4911a892020-04-29 23:35:13 +01001684 Visibility []string
Sundong Ahn054b19a2018-10-19 13:46:09 +09001685 Srcs []string
1686 Installable *bool
Paul Duffin52d398a2019-06-11 12:31:14 +01001687 Sdk_version *string
Jihoon Kang3198f3c2023-01-26 08:08:52 +00001688 Api_surface *string
Paul Duffin12ceb462019-12-24 20:31:31 +00001689 System_modules *string
Sundong Ahn054b19a2018-10-19 13:46:09 +09001690 Libs []string
Paul Duffin6877e6d2020-09-25 19:59:14 +01001691 Output_javadoc_comments *bool
Paul Duffin11512472019-02-11 15:55:17 +00001692 Arg_files []string
Sundong Ahn054b19a2018-10-19 13:46:09 +09001693 Args *string
Sundong Ahn054b19a2018-10-19 13:46:09 +09001694 Java_version *string
Paul Duffine22c2ab2020-05-20 19:35:27 +01001695 Annotations_enabled *bool
Sundong Ahn054b19a2018-10-19 13:46:09 +09001696 Merge_annotations_dirs []string
1697 Merge_inclusion_annotations_dirs []string
Paul Duffin0ff08bd2020-04-29 13:30:54 +01001698 Generate_stubs *bool
Anton Hanssone87b03d2020-12-21 15:29:34 +00001699 Previous_api *string
Sundong Ahn054b19a2018-10-19 13:46:09 +09001700 Check_api struct {
Anton Hanssone6056152020-12-31 10:37:27 +00001701 Current ApiToCheck
1702 Last_released ApiToCheck
Paul Duffin160fe412020-05-10 19:32:20 +01001703
1704 Api_lint struct {
1705 Enabled *bool
1706 New_since *string
1707 Baseline_file *string
1708 }
Jiyong Park58c518b2018-05-12 22:29:12 +09001709 }
Sundong Ahn1b92c822018-05-29 11:35:17 +09001710 Aidl struct {
1711 Include_dirs []string
1712 Local_include_dirs []string
1713 }
Paul Duffin040e9062020-11-23 17:41:36 +00001714 Dists []android.Dist
Jiyong Parkc678ad32018-04-10 13:07:10 +09001715 }{}
1716
Paul Duffin7b78b4d2020-04-28 14:08:32 +01001717 // The stubs source processing uses the same compile time classpath when extracting the
1718 // API from the implementation library as it does when compiling it. i.e. the same
1719 // * sdk version
1720 // * system_modules
1721 // * libs (static_libs/libs)
Paul Duffin250e6192019-06-07 10:44:37 +01001722
Paul Duffin0ff08bd2020-04-29 13:30:54 +01001723 props.Name = proptools.StringPtr(name)
Anton Hansson944e77d2020-08-19 11:40:22 +01001724 props.Visibility = childModuleVisibility(module.sdkLibraryProperties.Stubs_source_visibility)
Paul Duffina18abc22020-05-16 18:54:24 +01001725 props.Srcs = append(props.Srcs, module.properties.Srcs...)
Anton Hanssonf8ea3722021-09-16 14:24:13 +01001726 props.Srcs = append(props.Srcs, module.sdkLibraryProperties.Api_srcs...)
Paul Duffina18abc22020-05-16 18:54:24 +01001727 props.Sdk_version = module.deviceProperties.Sdk_version
Jihoon Kang3198f3c2023-01-26 08:08:52 +00001728 props.Api_surface = &apiScope.name
Paul Duffina18abc22020-05-16 18:54:24 +01001729 props.System_modules = module.deviceProperties.System_modules
Jiyong Parkc678ad32018-04-10 13:07:10 +09001730 props.Installable = proptools.BoolPtr(false)
Sundong Ahne6f0b052018-06-05 16:46:14 +09001731 // A droiddoc module has only one Libs property and doesn't distinguish between
1732 // shared libs and static libs. So we need to add both of these libs to Libs property.
Paul Duffina18abc22020-05-16 18:54:24 +01001733 props.Libs = module.properties.Libs
1734 props.Libs = append(props.Libs, module.properties.Static_libs...)
Nikita Ioffed732da72022-11-21 12:38:25 +00001735 props.Libs = append(props.Libs, module.sdkLibraryProperties.Stub_only_libs...)
Mark White9421c4c2023-08-10 00:07:03 +00001736 props.Libs = append(props.Libs, module.scopeToProperties[apiScope].Libs...)
Paul Duffina18abc22020-05-16 18:54:24 +01001737 props.Aidl.Include_dirs = module.deviceProperties.Aidl.Include_dirs
1738 props.Aidl.Local_include_dirs = module.deviceProperties.Aidl.Local_include_dirs
1739 props.Java_version = module.properties.Java_version
Jiyong Parkc678ad32018-04-10 13:07:10 +09001740
Paul Duffine22c2ab2020-05-20 19:35:27 +01001741 props.Annotations_enabled = module.sdkLibraryProperties.Annotations_enabled
Sundong Ahn054b19a2018-10-19 13:46:09 +09001742 props.Merge_annotations_dirs = module.sdkLibraryProperties.Merge_annotations_dirs
1743 props.Merge_inclusion_annotations_dirs = module.sdkLibraryProperties.Merge_inclusion_annotations_dirs
1744
Paul Duffin6d0886e2020-04-07 18:49:53 +01001745 droidstubsArgs := []string{}
Paul Duffin235ffff2019-12-24 10:41:30 +00001746 if len(module.sdkLibraryProperties.Api_packages) != 0 {
Paul Duffin6d0886e2020-04-07 18:49:53 +01001747 droidstubsArgs = append(droidstubsArgs, "--stub-packages "+strings.Join(module.sdkLibraryProperties.Api_packages, ":"))
Paul Duffin235ffff2019-12-24 10:41:30 +00001748 }
1749 if len(module.sdkLibraryProperties.Hidden_api_packages) != 0 {
Paul Duffin6d0886e2020-04-07 18:49:53 +01001750 droidstubsArgs = append(droidstubsArgs,
Paul Duffin235ffff2019-12-24 10:41:30 +00001751 android.JoinWithPrefix(module.sdkLibraryProperties.Hidden_api_packages, " --hide-package "))
1752 }
Paul Duffin6d0886e2020-04-07 18:49:53 +01001753 droidstubsArgs = append(droidstubsArgs, module.sdkLibraryProperties.Droiddoc_options...)
Anton Hanssonfd1c0d22023-11-02 15:18:09 +00001754 disabledWarnings := []string{"HiddenSuperclass"}
1755 if proptools.BoolDefault(module.sdkLibraryProperties.Api_lint.Legacy_errors_allowed, true) {
1756 disabledWarnings = append(disabledWarnings,
1757 "BroadcastBehavior",
1758 "DeprecationMismatch",
1759 "MissingPermission",
1760 "SdkConstant",
1761 "Todo",
1762 )
Paul Duffin235ffff2019-12-24 10:41:30 +00001763 }
Paul Duffin6d0886e2020-04-07 18:49:53 +01001764 droidstubsArgs = append(droidstubsArgs, android.JoinWithPrefix(disabledWarnings, "--hide "))
Sundong Ahnfb2721f2018-09-17 13:23:09 +09001765
Paul Duffin6877e6d2020-09-25 19:59:14 +01001766 // Output Javadoc comments for public scope.
1767 if apiScope == apiScopePublic {
1768 props.Output_javadoc_comments = proptools.BoolPtr(true)
1769 }
1770
Paul Duffin1fb487d2020-04-07 18:50:10 +01001771 // Add in scope specific arguments.
Paul Duffin0ff08bd2020-04-29 13:30:54 +01001772 droidstubsArgs = append(droidstubsArgs, scopeSpecificDroidstubsArgs...)
Paul Duffin11512472019-02-11 15:55:17 +00001773 props.Arg_files = module.sdkLibraryProperties.Droiddoc_option_files
Paul Duffin6d0886e2020-04-07 18:49:53 +01001774 props.Args = proptools.StringPtr(strings.Join(droidstubsArgs, " "))
Jiyong Parkc678ad32018-04-10 13:07:10 +09001775
Paul Duffin15f34ef2020-07-20 18:04:44 +01001776 // List of APIs identified from the provided source files are created. They are later
1777 // compared against to the not-yet-released (a.k.a current) list of APIs and to the
1778 // last-released (a.k.a numbered) list of API.
1779 currentApiFileName := apiScope.apiFilePrefix + "current.txt"
1780 removedApiFileName := apiScope.apiFilePrefix + "removed.txt"
1781 apiDir := module.getApiDir()
1782 currentApiFileName = path.Join(apiDir, currentApiFileName)
1783 removedApiFileName = path.Join(apiDir, removedApiFileName)
Jiyong Parkc678ad32018-04-10 13:07:10 +09001784
Paul Duffin15f34ef2020-07-20 18:04:44 +01001785 // check against the not-yet-release API
1786 props.Check_api.Current.Api_file = proptools.StringPtr(currentApiFileName)
1787 props.Check_api.Current.Removed_api_file = proptools.StringPtr(removedApiFileName)
Jiyong Park58c518b2018-05-12 22:29:12 +09001788
Paul Duffin958806b2022-05-16 13:10:47 +00001789 if module.compareAgainstLatestApi(apiScope) {
Paul Duffin15f34ef2020-07-20 18:04:44 +01001790 // check against the latest released API
1791 latestApiFilegroupName := proptools.StringPtr(module.latestApiFilegroupName(apiScope))
Anton Hanssone87b03d2020-12-21 15:29:34 +00001792 props.Previous_api = latestApiFilegroupName
Paul Duffin15f34ef2020-07-20 18:04:44 +01001793 props.Check_api.Last_released.Api_file = latestApiFilegroupName
1794 props.Check_api.Last_released.Removed_api_file = proptools.StringPtr(
1795 module.latestRemovedApiFilegroupName(apiScope))
Jaewoong Jung1a97ee02021-03-09 13:25:02 -08001796 props.Check_api.Last_released.Baseline_file = proptools.StringPtr(
1797 module.latestIncompatibilitiesFilegroupName(apiScope))
Paul Duffin160fe412020-05-10 19:32:20 +01001798
Paul Duffin15f34ef2020-07-20 18:04:44 +01001799 if proptools.Bool(module.sdkLibraryProperties.Api_lint.Enabled) {
1800 // Enable api lint.
1801 props.Check_api.Api_lint.Enabled = proptools.BoolPtr(true)
1802 props.Check_api.Api_lint.New_since = latestApiFilegroupName
Paul Duffin160fe412020-05-10 19:32:20 +01001803
Paul Duffin15f34ef2020-07-20 18:04:44 +01001804 // If it exists then pass a lint-baseline.txt through to droidstubs.
1805 baselinePath := path.Join(apiDir, apiScope.apiFilePrefix+"lint-baseline.txt")
1806 baselinePathRelativeToRoot := path.Join(mctx.ModuleDir(), baselinePath)
1807 paths, err := mctx.GlobWithDeps(baselinePathRelativeToRoot, nil)
1808 if err != nil {
1809 mctx.ModuleErrorf("error checking for presence of %s: %s", baselinePathRelativeToRoot, err)
1810 }
1811 if len(paths) == 1 {
1812 props.Check_api.Api_lint.Baseline_file = proptools.StringPtr(baselinePath)
1813 } else if len(paths) != 0 {
1814 mctx.ModuleErrorf("error checking for presence of %s: expected one path, found: %v", baselinePathRelativeToRoot, paths)
Paul Duffin160fe412020-05-10 19:32:20 +01001815 }
1816 }
Paul Duffin15f34ef2020-07-20 18:04:44 +01001817 }
Jiyong Park58c518b2018-05-12 22:29:12 +09001818
Paul Duffin15f34ef2020-07-20 18:04:44 +01001819 if !Bool(module.sdkLibraryProperties.No_dist) {
Paul Duffin040e9062020-11-23 17:41:36 +00001820 // Dist the api txt and removed api txt artifacts for sdk builds.
1821 distDir := proptools.StringPtr(path.Join(module.apiDistPath(apiScope), "api"))
1822 for _, p := range []struct {
1823 tag string
1824 pattern string
1825 }{
1826 {tag: ".api.txt", pattern: "%s.txt"},
1827 {tag: ".removed-api.txt", pattern: "%s-removed.txt"},
1828 } {
1829 props.Dists = append(props.Dists, android.Dist{
1830 Targets: []string{"sdk", "win_sdk"},
1831 Dir: distDir,
1832 Dest: proptools.StringPtr(fmt.Sprintf(p.pattern, module.distStem())),
1833 Tag: proptools.StringPtr(p.tag),
1834 })
1835 }
Anton Hansson5fd5d242020-03-27 19:43:19 +00001836 }
1837
Spandan Das2cc80ba2023-10-27 17:21:52 +00001838 mctx.CreateModule(DroidstubsFactory, &props, module.sdkComponentPropertiesForChildLibrary()).(*Droidstubs).CallHookIfAvailable(mctx)
Jiyong Parkc678ad32018-04-10 13:07:10 +09001839}
1840
Jihoon Kang0c705a42023-08-02 06:44:57 +00001841func (module *SdkLibrary) createApiLibrary(mctx android.DefaultableHookContext, apiScope *apiScope, alternativeFullApiSurfaceStub string) {
Jihoon Kang1c92c3e2023-03-23 17:44:51 +00001842 props := struct {
Jihoon Kangca198c22023-06-22 23:13:51 +00001843 Name *string
1844 Visibility []string
1845 Api_contributions []string
1846 Libs []string
1847 Static_libs []string
1848 Full_api_surface_stub *string
Jihoon Kang4ec24872023-10-05 17:26:09 +00001849 System_modules *string
Jihoon Kang063ec002023-06-28 01:16:23 +00001850 Enable_validation *bool
Jihoon Kang1c92c3e2023-03-23 17:44:51 +00001851 }{}
1852
1853 props.Name = proptools.StringPtr(module.apiLibraryModuleName(apiScope))
Jihoon Kang786df932023-09-07 01:18:31 +00001854 props.Visibility = []string{"//visibility:override", "//visibility:private"}
Jihoon Kang1c92c3e2023-03-23 17:44:51 +00001855
1856 apiContributions := []string{}
1857
1858 // Api surfaces are not independent of each other, but have subset relationships,
1859 // and so does the api files. To generate from-text stubs for api surfaces other than public,
1860 // all subset api domains' api_contriubtions must be added as well.
1861 scope := apiScope
1862 for scope != nil {
1863 apiContributions = append(apiContributions, module.stubsSourceModuleName(scope)+".api.contribution")
1864 scope = scope.extends
1865 }
Jihoon Kang0c705a42023-08-02 06:44:57 +00001866 if apiScope == apiScopePublic {
1867 additionalApiContribution := module.apiLibraryAdditionalApiContribution()
1868 if additionalApiContribution != "" {
1869 apiContributions = append(apiContributions, additionalApiContribution)
1870 }
1871 }
Jihoon Kang1c92c3e2023-03-23 17:44:51 +00001872
1873 props.Api_contributions = apiContributions
1874 props.Libs = module.properties.Libs
1875 props.Libs = append(props.Libs, module.sdkLibraryProperties.Stub_only_libs...)
Mark White9421c4c2023-08-10 00:07:03 +00001876 props.Libs = append(props.Libs, module.scopeToProperties[apiScope].Libs...)
Jihoon Kang1c92c3e2023-03-23 17:44:51 +00001877 props.Libs = append(props.Libs, "stub-annotations")
1878 props.Static_libs = module.sdkLibraryProperties.Stub_only_static_libs
Jihoon Kange7ee2562023-07-25 05:51:46 +00001879 props.Full_api_surface_stub = proptools.StringPtr(apiScope.kind.DefaultJavaLibraryName())
Jihoon Kang0c705a42023-08-02 06:44:57 +00001880 if alternativeFullApiSurfaceStub != "" {
1881 props.Full_api_surface_stub = proptools.StringPtr(alternativeFullApiSurfaceStub)
1882 }
Jihoon Kang1c92c3e2023-03-23 17:44:51 +00001883
1884 // android_module_lib_stubs_current.from-text only comprises api contributions from art, conscrypt and i18n.
1885 // Thus, replace with android_module_lib_stubs_current_full.from-text, which comprises every api domains.
1886 if apiScope.kind == android.SdkModule {
Jihoon Kangca198c22023-06-22 23:13:51 +00001887 props.Full_api_surface_stub = proptools.StringPtr(apiScope.kind.DefaultJavaLibraryName() + "_full.from-text")
Jihoon Kang1c92c3e2023-03-23 17:44:51 +00001888 }
1889
Jihoon Kangd30ac8a2023-10-09 18:00:17 +00001890 // java_sdk_library modules that set sdk_version as none does not depend on other api
1891 // domains. Therefore, java_api_library created from such modules should not depend on
1892 // full_api_surface_stubs but create and compile stubs by the java_api_library module
1893 // itself.
1894 if module.SdkVersion(mctx).Kind == android.SdkNone {
1895 props.Full_api_surface_stub = nil
1896 }
1897
Jihoon Kang4ec24872023-10-05 17:26:09 +00001898 props.System_modules = module.deviceProperties.System_modules
Jihoon Kang063ec002023-06-28 01:16:23 +00001899 props.Enable_validation = proptools.BoolPtr(true)
Jihoon Kang4ec24872023-10-05 17:26:09 +00001900
Spandan Das2cc80ba2023-10-27 17:21:52 +00001901 mctx.CreateModule(ApiLibraryFactory, &props, module.sdkComponentPropertiesForChildLibrary())
Jihoon Kang1c92c3e2023-03-23 17:44:51 +00001902}
1903
Jihoon Kang1147b312023-06-08 23:25:57 +00001904func (module *SdkLibrary) createTopLevelStubsLibrary(
1905 mctx android.DefaultableHookContext, apiScope *apiScope, contributesToApiSurface bool) {
1906 props := struct {
1907 Name *string
1908 Visibility []string
1909 Sdk_version *string
1910 Static_libs []string
1911 System_modules *string
1912 Dist struct {
1913 Targets []string
1914 Dest *string
1915 Dir *string
1916 Tag *string
1917 }
1918 Compile_dex *bool
1919 }{}
1920 props.Name = proptools.StringPtr(module.stubsLibraryModuleName(apiScope))
1921 props.Visibility = childModuleVisibility(module.sdkLibraryProperties.Stubs_library_visibility)
1922 sdkVersion := module.sdkVersionForStubsLibrary(mctx, apiScope)
1923 props.Sdk_version = proptools.StringPtr(sdkVersion)
1924
1925 // Add the stub compiling java_library/java_api_library as static lib based on build config
1926 staticLib := module.sourceStubLibraryModuleName(apiScope)
1927 if mctx.Config().BuildFromTextStub() && contributesToApiSurface {
1928 staticLib = module.apiLibraryModuleName(apiScope)
1929 }
1930 props.Static_libs = append(props.Static_libs, staticLib)
1931 props.System_modules = module.deviceProperties.System_modules
1932
1933 // Dist the class jar artifact for sdk builds.
1934 if !Bool(module.sdkLibraryProperties.No_dist) {
1935 props.Dist.Targets = []string{"sdk", "win_sdk"}
1936 props.Dist.Dest = proptools.StringPtr(fmt.Sprintf("%v.jar", module.distStem()))
1937 props.Dist.Dir = proptools.StringPtr(module.apiDistPath(apiScope))
1938 props.Dist.Tag = proptools.StringPtr(".jar")
1939 }
1940
1941 // The imports need to be compiled to dex if the java_sdk_library requests it.
1942 compileDex := module.dexProperties.Compile_dex
1943 if module.stubLibrariesCompiledForDex() {
1944 compileDex = proptools.BoolPtr(true)
1945 }
1946 props.Compile_dex = compileDex
1947
1948 mctx.CreateModule(LibraryFactory, &props, module.sdkComponentPropertiesForChildLibrary())
1949}
1950
Paul Duffin958806b2022-05-16 13:10:47 +00001951func (module *SdkLibrary) compareAgainstLatestApi(apiScope *apiScope) bool {
1952 return !(apiScope.unstable || module.sdkLibraryProperties.Unsafe_ignore_missing_latest_api)
1953}
1954
Paul Duffinea8f8082021-06-24 13:25:57 +01001955// Implements android.ApexModule
Jooyung Han5e9013b2020-03-10 06:23:13 +09001956func (module *SdkLibrary) DepIsInSameApex(mctx android.BaseModuleContext, dep android.Module) bool {
1957 depTag := mctx.OtherModuleDependencyTag(dep)
1958 if depTag == xmlPermissionsFileTag {
1959 return true
1960 }
1961 return module.Library.DepIsInSameApex(mctx, dep)
1962}
1963
Paul Duffinea8f8082021-06-24 13:25:57 +01001964// Implements android.ApexModule
1965func (module *SdkLibrary) UniqueApexVariations() bool {
1966 return module.uniqueApexVariations()
1967}
1968
Jiyong Parkc678ad32018-04-10 13:07:10 +09001969// Creates the xml file that publicizes the runtime library
Paul Duffinf0229202020-04-29 16:47:28 +01001970func (module *SdkLibrary) createXmlFile(mctx android.DefaultableHookContext) {
Spandan Das8c9ae7e2023-03-03 21:20:36 +00001971 moduleMinApiLevel := module.Library.MinSdkVersion(mctx)
Pedro Loureiroc3621422021-09-28 15:40:23 +00001972 var moduleMinApiLevelStr = moduleMinApiLevel.String()
1973 if moduleMinApiLevel == android.NoneApiLevel {
1974 moduleMinApiLevelStr = "current"
1975 }
Jiyong Parke3833882020-02-17 17:28:10 +09001976 props := struct {
Pedro Loureiroc3621422021-09-28 15:40:23 +00001977 Name *string
1978 Lib_name *string
1979 Apex_available []string
1980 On_bootclasspath_since *string
1981 On_bootclasspath_before *string
1982 Min_device_sdk *string
1983 Max_device_sdk *string
1984 Sdk_library_min_api_level *string
Jiyong Parke3833882020-02-17 17:28:10 +09001985 }{
Pedro Loureiroc3621422021-09-28 15:40:23 +00001986 Name: proptools.StringPtr(module.xmlPermissionsModuleName()),
1987 Lib_name: proptools.StringPtr(module.BaseModuleName()),
1988 Apex_available: module.ApexProperties.Apex_available,
1989 On_bootclasspath_since: module.commonSdkLibraryProperties.On_bootclasspath_since,
1990 On_bootclasspath_before: module.commonSdkLibraryProperties.On_bootclasspath_before,
1991 Min_device_sdk: module.commonSdkLibraryProperties.Min_device_sdk,
1992 Max_device_sdk: module.commonSdkLibraryProperties.Max_device_sdk,
1993 Sdk_library_min_api_level: &moduleMinApiLevelStr,
Jiyong Parkc678ad32018-04-10 13:07:10 +09001994 }
Jiyong Parke3833882020-02-17 17:28:10 +09001995
Jiyong Parke3833882020-02-17 17:28:10 +09001996 mctx.CreateModule(sdkLibraryXmlFactory, &props)
Jiyong Parkc678ad32018-04-10 13:07:10 +09001997}
1998
Jiyong Parkf1691d22021-03-29 20:11:58 +09001999func PrebuiltJars(ctx android.BaseModuleContext, baseName string, s android.SdkSpec) android.Paths {
Jiyong Park54105c42021-03-31 18:17:53 +09002000 var ver android.ApiLevel
Jiyong Parkf1691d22021-03-29 20:11:58 +09002001 var kind android.SdkKind
2002 if s.UsePrebuilt(ctx) {
Jiyong Park54105c42021-03-31 18:17:53 +09002003 ver = s.ApiLevel
Jiyong Parkf1691d22021-03-29 20:11:58 +09002004 kind = s.Kind
Jiyong Parkc678ad32018-04-10 13:07:10 +09002005 } else {
Jiyong Park6a927c42020-01-21 02:03:43 +09002006 // We don't have prebuilt SDK for the specific sdkVersion.
2007 // Instead of breaking the build, fallback to use "system_current"
Jiyong Park54105c42021-03-31 18:17:53 +09002008 ver = android.FutureApiLevel
Jiyong Parkf1691d22021-03-29 20:11:58 +09002009 kind = android.SdkSystem
Sundong Ahn054b19a2018-10-19 13:46:09 +09002010 }
Jiyong Park6a927c42020-01-21 02:03:43 +09002011
2012 dir := filepath.Join("prebuilts", "sdk", ver.String(), kind.String())
Paul Duffin50061512020-01-21 16:31:05 +00002013 jar := filepath.Join(dir, baseName+".jar")
Sundong Ahn054b19a2018-10-19 13:46:09 +09002014 jarPath := android.ExistentPathForSource(ctx, jar)
Sundong Ahnae418ac2019-02-28 15:01:28 +09002015 if !jarPath.Valid() {
Colin Cross07c88562020-01-07 09:34:44 -08002016 if ctx.Config().AllowMissingDependencies() {
2017 return android.Paths{android.PathForSource(ctx, jar)}
2018 } else {
Jiyong Parkf1691d22021-03-29 20:11:58 +09002019 ctx.PropertyErrorf("sdk_library", "invalid sdk version %q, %q does not exist", s.Raw, jar)
Colin Cross07c88562020-01-07 09:34:44 -08002020 }
Sundong Ahnae418ac2019-02-28 15:01:28 +09002021 return nil
2022 }
Sundong Ahn054b19a2018-10-19 13:46:09 +09002023 return android.Paths{jarPath.Path()}
2024}
2025
Colin Crossaede88c2020-08-11 12:17:01 -07002026// 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 +01002027//
2028// If either this or the other module are on the platform then this will return
2029// false.
Colin Cross56a83212020-09-15 18:30:11 -07002030func withinSameApexesAs(ctx android.BaseModuleContext, other android.Module) bool {
2031 apexInfo := ctx.Provider(android.ApexInfoProvider).(android.ApexInfo)
2032 otherApexInfo := ctx.OtherModuleProvider(other, android.ApexInfoProvider).(android.ApexInfo)
Jiyong Parkab50b072021-05-12 17:13:56 +09002033 return len(otherApexInfo.InApexVariants) > 0 && reflect.DeepEqual(apexInfo.InApexVariants, otherApexInfo.InApexVariants)
Paul Duffin9b879592020-05-26 13:21:35 +01002034}
2035
Jiyong Parkf1691d22021-03-29 20:11:58 +09002036func (module *SdkLibrary) sdkJars(ctx android.BaseModuleContext, sdkVersion android.SdkSpec, headerJars bool) android.Paths {
Jiyong Park932cdfe2020-05-28 00:19:53 +09002037 // If the client doesn't set sdk_version, but if this library prefers stubs over
2038 // the impl library, let's provide the widest API surface possible. To do so,
2039 // force override sdk_version to module_current so that the closest possible API
2040 // surface could be found in selectHeaderJarsForSdkVersion
Jiyong Parkf1691d22021-03-29 20:11:58 +09002041 if module.defaultsToStubs() && !sdkVersion.Specified() {
Jiyong Park92315372021-04-02 08:45:46 +09002042 sdkVersion = android.SdkSpecFrom(ctx, "module_current")
Jiyong Park932cdfe2020-05-28 00:19:53 +09002043 }
Paul Duffind1b3a922020-01-22 11:57:20 +00002044
Paul Duffindaaa3322020-05-26 18:13:57 +01002045 // Only provide access to the implementation library if it is actually built.
2046 if module.requiresRuntimeImplementationLibrary() {
2047 // Check any special cases for java_sdk_library.
2048 //
2049 // Only allow access to the implementation library in the following condition:
2050 // * No sdk_version specified on the referencing module.
Paul Duffin9b879592020-05-26 13:21:35 +01002051 // * The referencing module is in the same apex as this.
Jiyong Parkf1691d22021-03-29 20:11:58 +09002052 if sdkVersion.Kind == android.SdkPrivate || withinSameApexesAs(ctx, module) {
Paul Duffindaaa3322020-05-26 18:13:57 +01002053 if headerJars {
2054 return module.HeaderJars()
2055 } else {
2056 return module.ImplementationJars()
2057 }
Sundong Ahn054b19a2018-10-19 13:46:09 +09002058 }
Jiyong Parkc678ad32018-04-10 13:07:10 +09002059 }
Paul Duffinb05d4292020-05-20 12:19:10 +01002060
Paul Duffin23970f42020-05-20 14:20:02 +01002061 return module.selectHeaderJarsForSdkVersion(ctx, sdkVersion)
Jiyong Parkc678ad32018-04-10 13:07:10 +09002062}
2063
Sundong Ahn241cd372018-07-13 16:16:44 +09002064// to satisfy SdkLibraryDependency interface
Jiyong Parkf1691d22021-03-29 20:11:58 +09002065func (module *SdkLibrary) SdkHeaderJars(ctx android.BaseModuleContext, sdkVersion android.SdkSpec) android.Paths {
Paul Duffind1b3a922020-01-22 11:57:20 +00002066 return module.sdkJars(ctx, sdkVersion, true /*headerJars*/)
2067}
2068
2069// to satisfy SdkLibraryDependency interface
Jiyong Parkf1691d22021-03-29 20:11:58 +09002070func (module *SdkLibrary) SdkImplementationJars(ctx android.BaseModuleContext, sdkVersion android.SdkSpec) android.Paths {
Paul Duffind1b3a922020-01-22 11:57:20 +00002071 return module.sdkJars(ctx, sdkVersion, false /*headerJars*/)
Sundong Ahn241cd372018-07-13 16:16:44 +09002072}
2073
Colin Cross571cccf2019-02-04 11:22:08 -08002074var javaSdkLibrariesKey = android.NewOnceKey("javaSdkLibraries")
2075
Jiyong Park82484c02018-04-23 21:41:26 +09002076func javaSdkLibraries(config android.Config) *[]string {
Colin Cross571cccf2019-02-04 11:22:08 -08002077 return config.Once(javaSdkLibrariesKey, func() interface{} {
Jiyong Park82484c02018-04-23 21:41:26 +09002078 return &[]string{}
2079 }).(*[]string)
2080}
2081
Paul Duffin749f98f2019-12-30 17:23:46 +00002082func (module *SdkLibrary) getApiDir() string {
2083 return proptools.StringDefault(module.sdkLibraryProperties.Api_dir, "api")
2084}
2085
Jiyong Parkc678ad32018-04-10 13:07:10 +09002086// For a java_sdk_library module, create internal modules for stubs, docs,
2087// runtime libs and xml file. If requested, the stubs and docs are created twice
2088// once for public API level and once for system API level
Paul Duffinf0229202020-04-29 16:47:28 +01002089func (module *SdkLibrary) CreateInternalModules(mctx android.DefaultableHookContext) {
2090 // If the module has been disabled then don't create any child modules.
2091 if !module.Enabled() {
2092 return
2093 }
2094
Paul Duffina18abc22020-05-16 18:54:24 +01002095 if len(module.properties.Srcs) == 0 {
Inseob Kimc0907f12019-02-08 21:00:45 +09002096 mctx.PropertyErrorf("srcs", "java_sdk_library must specify srcs")
Jooyung Han58f26ab2019-12-18 15:34:32 +09002097 return
Inseob Kimc0907f12019-02-08 21:00:45 +09002098 }
2099
Paul Duffin37e0b772019-12-30 17:20:10 +00002100 // If this builds against standard libraries (i.e. is not part of the core libraries)
Paul Duffin4f5c1ef2020-11-19 14:53:43 +00002101 // then assume it provides both system and test apis.
Jiyong Parkf1691d22021-03-29 20:11:58 +09002102 sdkDep := decodeSdkDep(mctx, android.SdkContext(&module.Library))
Paul Duffin37e0b772019-12-30 17:20:10 +00002103 hasSystemAndTestApis := sdkDep.hasStandardLibs()
Paul Duffin3375e352020-04-28 10:44:03 +01002104 module.sdkLibraryProperties.Generate_system_and_test_apis = hasSystemAndTestApis
Paul Duffin4f5c1ef2020-11-19 14:53:43 +00002105
Jaewoong Jung18aefc12020-12-21 09:11:10 -08002106 missingCurrentApi := false
Inseob Kim8098faa2019-03-18 10:19:51 +09002107
Paul Duffin3375e352020-04-28 10:44:03 +01002108 generatedScopes := module.getGeneratedApiScopes(mctx)
Paul Duffind1b3a922020-01-22 11:57:20 +00002109
Paul Duffin749f98f2019-12-30 17:23:46 +00002110 apiDir := module.getApiDir()
Paul Duffin3375e352020-04-28 10:44:03 +01002111 for _, scope := range generatedScopes {
Inseob Kim8098faa2019-03-18 10:19:51 +09002112 for _, api := range []string{"current.txt", "removed.txt"} {
Paul Duffind1b3a922020-01-22 11:57:20 +00002113 path := path.Join(mctx.ModuleDir(), apiDir, scope.apiFilePrefix+api)
Inseob Kim8098faa2019-03-18 10:19:51 +09002114 p := android.ExistentPathForSource(mctx, path)
2115 if !p.Valid() {
Colin Cross18f840c2021-05-20 17:56:54 -07002116 if mctx.Config().AllowMissingDependencies() {
2117 mctx.AddMissingDependencies([]string{path})
2118 } else {
2119 mctx.ModuleErrorf("Current api file %#v doesn't exist", path)
2120 missingCurrentApi = true
2121 }
Inseob Kim8098faa2019-03-18 10:19:51 +09002122 }
2123 }
2124 }
2125
Jaewoong Jung18aefc12020-12-21 09:11:10 -08002126 if missingCurrentApi {
Inseob Kim8098faa2019-03-18 10:19:51 +09002127 script := "build/soong/scripts/gen-java-current-api-files.sh"
2128 p := android.ExistentPathForSource(mctx, script)
2129
2130 if !p.Valid() {
2131 panic(fmt.Sprintf("script file %s doesn't exist", script))
2132 }
2133
2134 mctx.ModuleErrorf("One or more current api files are missing. "+
2135 "You can update them by:\n"+
Paul Duffin37e0b772019-12-30 17:20:10 +00002136 "%s %q %s && m update-api",
Paul Duffind1b3a922020-01-22 11:57:20 +00002137 script, filepath.Join(mctx.ModuleDir(), apiDir),
Paul Duffin3375e352020-04-28 10:44:03 +01002138 strings.Join(generatedScopes.Strings(func(s *apiScope) string { return s.apiFilePrefix }), " "))
Inseob Kim8098faa2019-03-18 10:19:51 +09002139 return
2140 }
2141
Paul Duffin3375e352020-04-28 10:44:03 +01002142 for _, scope := range generatedScopes {
Paul Duffin15f34ef2020-07-20 18:04:44 +01002143 // Use the stubs source name for legacy reasons.
2144 module.createStubsSourcesAndApi(mctx, scope, module.stubsSourceModuleName(scope), scope.droidstubsArgs)
Paul Duffin0ff08bd2020-04-29 13:30:54 +01002145
Paul Duffind1b3a922020-01-22 11:57:20 +00002146 module.createStubsLibrary(mctx, scope)
Jihoon Kang1c92c3e2023-03-23 17:44:51 +00002147
Jihoon Kang0c705a42023-08-02 06:44:57 +00002148 alternativeFullApiSurfaceStubLib := ""
2149 if scope == apiScopePublic {
2150 alternativeFullApiSurfaceStubLib = module.alternativeFullApiSurfaceStubLib()
2151 }
2152 contributesToApiSurface := module.contributesToApiSurface(mctx.Config()) || alternativeFullApiSurfaceStubLib != ""
Jihoon Kang1147b312023-06-08 23:25:57 +00002153 if contributesToApiSurface {
Jihoon Kang0c705a42023-08-02 06:44:57 +00002154 module.createApiLibrary(mctx, scope, alternativeFullApiSurfaceStubLib)
Jihoon Kang1c92c3e2023-03-23 17:44:51 +00002155 }
Jihoon Kang1147b312023-06-08 23:25:57 +00002156
2157 module.createTopLevelStubsLibrary(mctx, scope, contributesToApiSurface)
Inseob Kimc0907f12019-02-08 21:00:45 +09002158 }
2159
Paul Duffindfa131e2020-05-15 20:37:11 +01002160 if module.requiresRuntimeImplementationLibrary() {
Paul Duffin5df79302020-05-16 15:52:12 +01002161 // Create child module to create an implementation library.
2162 //
2163 // This temporarily creates a second implementation library that can be explicitly
2164 // referenced.
2165 //
2166 // TODO(b/156618935) - update comment once only one implementation library is created.
2167 module.createImplLibrary(mctx)
2168
Paul Duffindfa131e2020-05-15 20:37:11 +01002169 // Only create an XML permissions file that declares the library as being usable
2170 // as a shared library if required.
2171 if module.sharedLibrary() {
2172 module.createXmlFile(mctx)
2173 }
Paul Duffin43db9be2019-12-30 17:35:49 +00002174
2175 // record java_sdk_library modules so that they are exported to make
2176 javaSdkLibraries := javaSdkLibraries(mctx.Config())
2177 javaSdkLibrariesLock.Lock()
2178 defer javaSdkLibrariesLock.Unlock()
2179 *javaSdkLibraries = append(*javaSdkLibraries, module.BaseModuleName())
2180 }
Anton Hansson7f66efa2020-10-08 14:47:23 +01002181
Paul Duffin77590a82022-04-28 14:13:30 +00002182 // 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 +01002183 module.properties.Libs = append(module.properties.Libs, module.sdkLibraryProperties.Impl_only_libs...)
Paul Duffin77590a82022-04-28 14:13:30 +00002184 module.properties.Static_libs = append(module.properties.Static_libs, module.sdkLibraryProperties.Impl_only_static_libs...)
Inseob Kimc0907f12019-02-08 21:00:45 +09002185}
2186
2187func (module *SdkLibrary) InitSdkLibraryProperties() {
Colin Crossce6734e2020-06-15 16:09:53 -07002188 module.addHostAndDeviceProperties()
2189 module.AddProperties(&module.sdkLibraryProperties)
Sundong Ahn054b19a2018-10-19 13:46:09 +09002190
Paul Duffin71b33cc2021-06-23 11:39:47 +01002191 module.initSdkLibraryComponent(module)
Paul Duffin859fe962020-05-15 10:20:31 +01002192
Paul Duffina18abc22020-05-16 18:54:24 +01002193 module.properties.Installable = proptools.BoolPtr(true)
2194 module.deviceProperties.IsSDKLibrary = true
Inseob Kimc0907f12019-02-08 21:00:45 +09002195}
Sundong Ahn054b19a2018-10-19 13:46:09 +09002196
Paul Duffindfa131e2020-05-15 20:37:11 +01002197func (module *SdkLibrary) requiresRuntimeImplementationLibrary() bool {
2198 return !proptools.Bool(module.sdkLibraryProperties.Api_only)
2199}
2200
Jiyong Park932cdfe2020-05-28 00:19:53 +09002201func (module *SdkLibrary) defaultsToStubs() bool {
2202 return proptools.Bool(module.sdkLibraryProperties.Default_to_stubs)
2203}
2204
Paul Duffin1b1e8062020-05-08 13:44:43 +01002205// Defines how to name the individual component modules the sdk library creates.
2206type sdkLibraryComponentNamingScheme interface {
2207 stubsLibraryModuleName(scope *apiScope, baseName string) string
2208
2209 stubsSourceModuleName(scope *apiScope, baseName string) string
Jihoon Kang1c92c3e2023-03-23 17:44:51 +00002210
2211 apiLibraryModuleName(scope *apiScope, baseName string) string
Jihoon Kang1147b312023-06-08 23:25:57 +00002212
2213 sourceStubLibraryModuleName(scope *apiScope, baseName string) string
Paul Duffin1b1e8062020-05-08 13:44:43 +01002214}
2215
2216type defaultNamingScheme struct {
2217}
2218
2219func (s *defaultNamingScheme) stubsLibraryModuleName(scope *apiScope, baseName string) string {
2220 return scope.stubsLibraryModuleName(baseName)
2221}
2222
2223func (s *defaultNamingScheme) stubsSourceModuleName(scope *apiScope, baseName string) string {
2224 return scope.stubsSourceModuleName(baseName)
2225}
2226
Jihoon Kang1c92c3e2023-03-23 17:44:51 +00002227func (s *defaultNamingScheme) apiLibraryModuleName(scope *apiScope, baseName string) string {
2228 return scope.apiLibraryModuleName(baseName)
2229}
2230
Jihoon Kang1147b312023-06-08 23:25:57 +00002231func (s *defaultNamingScheme) sourceStubLibraryModuleName(scope *apiScope, baseName string) string {
2232 return scope.sourceStubLibraryModuleName(baseName)
2233}
2234
Paul Duffin1b1e8062020-05-08 13:44:43 +01002235var _ sdkLibraryComponentNamingScheme = (*defaultNamingScheme)(nil)
2236
Jaewoong Jungbc15e3a2021-03-10 17:02:43 -08002237func moduleStubLinkType(name string) (stub bool, ret sdkLinkType) {
Jihoon Kang1147b312023-06-08 23:25:57 +00002238 name = strings.TrimSuffix(name, ".from-source")
2239
Anton Hansson2d0c1942020-05-25 12:20:51 +01002240 // This suffix-based approach is fragile and could potentially mis-trigger.
2241 // TODO(b/155164730): Clean this up when modules no longer reference sdk_lib stubs directly.
Anton Hansson08f476b2021-04-07 15:32:19 +01002242 if strings.HasSuffix(name, apiScopePublic.stubsLibraryModuleNameSuffix()) {
2243 if name == "hwbinder.stubs" || name == "libcore_private.stubs" {
2244 // Due to a previous bug, these modules were not considered stubs, so we retain that.
2245 return false, javaPlatform
2246 }
Anton Hansson2d0c1942020-05-25 12:20:51 +01002247 return true, javaSdk
2248 }
Anton Hansson08f476b2021-04-07 15:32:19 +01002249 if strings.HasSuffix(name, apiScopeSystem.stubsLibraryModuleNameSuffix()) {
Anton Hansson2d0c1942020-05-25 12:20:51 +01002250 return true, javaSystem
2251 }
Anton Hansson08f476b2021-04-07 15:32:19 +01002252 if strings.HasSuffix(name, apiScopeModuleLib.stubsLibraryModuleNameSuffix()) {
Anton Hansson2d0c1942020-05-25 12:20:51 +01002253 return true, javaModule
2254 }
Anton Hansson08f476b2021-04-07 15:32:19 +01002255 if strings.HasSuffix(name, apiScopeTest.stubsLibraryModuleNameSuffix()) {
Anton Hansson2d0c1942020-05-25 12:20:51 +01002256 return true, javaSystem
2257 }
Jihoon Kang1147b312023-06-08 23:25:57 +00002258 if strings.HasSuffix(name, apiScopeSystemServer.stubsLibraryModuleNameSuffix()) {
2259 return true, javaSystemServer
2260 }
Anton Hansson2d0c1942020-05-25 12:20:51 +01002261 return false, javaPlatform
2262}
2263
Jaewoong Jung4f158ee2019-07-11 10:05:35 -07002264// java_sdk_library is a special Java library that provides optional platform APIs to apps.
2265// In practice, it can be viewed as a combination of several modules: 1) stubs library that clients
2266// are linked against to, 2) droiddoc module that internally generates API stubs source files,
2267// 3) the real runtime shared library that implements the APIs, and 4) XML file for adding
2268// the runtime lib to the classpath at runtime if requested via <uses-library>.
Inseob Kimc0907f12019-02-08 21:00:45 +09002269func SdkLibraryFactory() android.Module {
2270 module := &SdkLibrary{}
Paul Duffinc3091c82020-05-08 14:16:20 +01002271
2272 // Initialize information common between source and prebuilt.
Paul Duffin71b33cc2021-06-23 11:39:47 +01002273 module.initCommon(module)
Paul Duffinc3091c82020-05-08 14:16:20 +01002274
Inseob Kimc0907f12019-02-08 21:00:45 +09002275 module.InitSdkLibraryProperties()
Jooyung Han58f26ab2019-12-18 15:34:32 +09002276 android.InitApexModule(module)
Sundong Ahn054b19a2018-10-19 13:46:09 +09002277 InitJavaModule(module, android.HostAndDeviceSupported)
Paul Duffin3375e352020-04-28 10:44:03 +01002278
2279 // Initialize the map from scope to scope specific properties.
2280 scopeToProperties := make(map[*apiScope]*ApiScopeProperties)
2281 for _, scope := range allApiScopes {
2282 scopeToProperties[scope] = scope.scopeSpecificProperties(module)
2283 }
2284 module.scopeToProperties = scopeToProperties
2285
Paul Duffin4911a892020-04-29 23:35:13 +01002286 // Add the properties containing visibility rules so that they are checked.
Paul Duffin5df79302020-05-16 15:52:12 +01002287 android.AddVisibilityProperty(module, "impl_library_visibility", &module.sdkLibraryProperties.Impl_library_visibility)
Paul Duffin4911a892020-04-29 23:35:13 +01002288 android.AddVisibilityProperty(module, "stubs_library_visibility", &module.sdkLibraryProperties.Stubs_library_visibility)
2289 android.AddVisibilityProperty(module, "stubs_source_visibility", &module.sdkLibraryProperties.Stubs_source_visibility)
2290
Paul Duffin1b1e8062020-05-08 13:44:43 +01002291 module.SetDefaultableHook(func(ctx android.DefaultableHookContext) {
Paul Duffindfa131e2020-05-15 20:37:11 +01002292 // If no implementation is required then it cannot be used as a shared library
2293 // either.
2294 if !module.requiresRuntimeImplementationLibrary() {
2295 // If shared_library has been explicitly set to true then it is incompatible
2296 // with api_only: true.
2297 if proptools.Bool(module.commonSdkLibraryProperties.Shared_library) {
2298 ctx.PropertyErrorf("api_only/shared_library", "inconsistent settings, shared_library and api_only cannot both be true")
2299 }
2300 // Set shared_library: false.
2301 module.commonSdkLibraryProperties.Shared_library = proptools.BoolPtr(false)
2302 }
2303
Paul Duffin1b1e8062020-05-08 13:44:43 +01002304 if module.initCommonAfterDefaultsApplied(ctx) {
2305 module.CreateInternalModules(ctx)
2306 }
2307 })
Zi Wangb2179e32023-01-31 15:53:30 -08002308 android.InitBazelModule(module)
Jiyong Parkc678ad32018-04-10 13:07:10 +09002309 return module
2310}
Colin Cross79c7c262019-04-17 11:11:46 -07002311
Zi Wangb2179e32023-01-31 15:53:30 -08002312type bazelSdkLibraryAttributes struct {
Liz Kammer9a97a8f2023-09-18 13:36:41 -04002313 Public *bazel.Label
2314 System *bazel.Label
2315 Test *bazel.Label
2316 Module_lib *bazel.Label
2317 System_server *bazel.Label
Zi Wangb2179e32023-01-31 15:53:30 -08002318}
2319
2320// java_sdk_library bp2build converter
Chris Parsons637458d2023-09-19 20:09:00 +00002321func (module *SdkLibrary) ConvertWithBp2build(ctx android.Bp2buildMutatorContext) {
Zi Wangb2179e32023-01-31 15:53:30 -08002322 if ctx.ModuleType() != "java_sdk_library" {
Chris Parsons39a16972023-06-08 14:28:51 +00002323 ctx.MarkBp2buildUnconvertible(bp2build_metrics_proto.UnconvertedReasonType_TYPE_UNSUPPORTED, "")
Zi Wangb2179e32023-01-31 15:53:30 -08002324 return
2325 }
2326
Liz Kammer9a97a8f2023-09-18 13:36:41 -04002327 nameToAttr := make(map[string]*bazel.Label)
Zi Wangb2179e32023-01-31 15:53:30 -08002328
2329 for _, scope := range module.getGeneratedApiScopes(ctx) {
Liz Kammer9a97a8f2023-09-18 13:36:41 -04002330 apiSurfaceFile := android.BazelLabelForModuleSrcSingle(ctx, path.Join(module.getApiDir(), scope.apiFilePrefix+"current.txt"))
2331 nameToAttr[scope.name] = &apiSurfaceFile
Zi Wangb2179e32023-01-31 15:53:30 -08002332 }
2333
2334 attrs := bazelSdkLibraryAttributes{
2335 Public: nameToAttr["public"],
2336 System: nameToAttr["system"],
2337 Test: nameToAttr["test"],
2338 Module_lib: nameToAttr["module-lib"],
2339 System_server: nameToAttr["system-server"],
2340 }
2341 props := bazel.BazelTargetModuleProperties{
2342 Rule_class: "java_sdk_library",
2343 Bzl_load_location: "//build/bazel/rules/java:sdk_library.bzl",
2344 }
2345
2346 ctx.CreateBazelTargetModule(props, android.CommonAttributes{Name: module.Name()}, &attrs)
2347}
2348
Colin Cross79c7c262019-04-17 11:11:46 -07002349//
2350// SDK library prebuilts
2351//
2352
Paul Duffin56d44902020-01-31 13:36:25 +00002353// Properties associated with each api scope.
2354type sdkLibraryScopeProperties struct {
Colin Cross79c7c262019-04-17 11:11:46 -07002355 Jars []string `android:"path"`
2356
2357 Sdk_version *string
2358
Colin Cross79c7c262019-04-17 11:11:46 -07002359 // List of shared java libs that this module has dependencies to
2360 Libs []string
Paul Duffin3d1248c2020-04-09 00:10:17 +01002361
Paul Duffinc8782502020-04-29 20:45:27 +01002362 // The stubs source.
Paul Duffin3d1248c2020-04-09 00:10:17 +01002363 Stub_srcs []string `android:"path"`
Paul Duffin1fd005d2020-04-09 01:08:11 +01002364
2365 // The current.txt
Paul Duffin0f8faff2020-05-20 16:18:00 +01002366 Current_api *string `android:"path"`
Paul Duffin1fd005d2020-04-09 01:08:11 +01002367
2368 // The removed.txt
Paul Duffin0f8faff2020-05-20 16:18:00 +01002369 Removed_api *string `android:"path"`
Anton Hanssond78eb762021-09-21 15:25:12 +01002370
2371 // Annotation zip
2372 Annotations *string `android:"path"`
Colin Cross79c7c262019-04-17 11:11:46 -07002373}
2374
Paul Duffin56d44902020-01-31 13:36:25 +00002375type sdkLibraryImportProperties struct {
Paul Duffinfcfd7912020-01-31 17:54:30 +00002376 // List of shared java libs, common to all scopes, that this module has
2377 // dependencies to
2378 Libs []string
Paul Duffin1267d872021-04-16 17:21:36 +01002379
2380 // If set to true, compile dex files for the stubs. Defaults to false.
2381 Compile_dex *bool
Paul Duffin869de142021-07-15 14:14:41 +01002382
2383 // If not empty, classes are restricted to the specified packages and their sub-packages.
Paul Duffin869de142021-07-15 14:14:41 +01002384 Permitted_packages []string
Paul Duffin56d44902020-01-31 13:36:25 +00002385}
2386
Paul Duffineedc5d52020-06-12 17:46:39 +01002387type SdkLibraryImport struct {
Colin Cross79c7c262019-04-17 11:11:46 -07002388 android.ModuleBase
2389 android.DefaultableModuleBase
Liz Kammer9a97a8f2023-09-18 13:36:41 -04002390 android.BazelModuleBase
Colin Cross79c7c262019-04-17 11:11:46 -07002391 prebuilt android.Prebuilt
Paul Duffindd46f712020-02-10 13:37:10 +00002392 android.ApexModuleBase
Colin Cross79c7c262019-04-17 11:11:46 -07002393
Paul Duffin37856732021-02-26 14:24:15 +00002394 hiddenAPI
Jiakai Zhang204356f2021-09-09 08:12:46 +00002395 dexpreopter
Paul Duffin37856732021-02-26 14:24:15 +00002396
Colin Cross79c7c262019-04-17 11:11:46 -07002397 properties sdkLibraryImportProperties
2398
Paul Duffin46a26a82020-04-07 19:27:04 +01002399 // Map from api scope to the scope specific property structure.
2400 scopeProperties map[*apiScope]*sdkLibraryScopeProperties
2401
Paul Duffin56d44902020-01-31 13:36:25 +00002402 commonToSdkLibraryAndImport
Paul Duffineedc5d52020-06-12 17:46:39 +01002403
2404 // The reference to the implementation library created by the source module.
2405 // Is nil if the source module does not exist.
2406 implLibraryModule *Library
2407
2408 // The reference to the xml permissions module created by the source module.
2409 // Is nil if the source module does not exist.
2410 xmlPermissionsFileModule *sdkLibraryXml
Paul Duffin39853512021-02-26 11:09:39 +00002411
Jeongik Chad5fe8782021-07-08 01:13:11 +09002412 // Build path to the dex implementation jar obtained from the prebuilt_apex, if any.
Martin Stjernholm8be1e6d2021-09-15 03:34:04 +01002413 dexJarFile OptionalDexJarPath
Jeongik Chad5fe8782021-07-08 01:13:11 +09002414
2415 // Expected install file path of the source module(sdk_library)
2416 // or dex implementation jar obtained from the prebuilt_apex, if any.
2417 installFile android.Path
Colin Cross79c7c262019-04-17 11:11:46 -07002418}
2419
Paul Duffineedc5d52020-06-12 17:46:39 +01002420var _ SdkLibraryDependency = (*SdkLibraryImport)(nil)
Colin Cross79c7c262019-04-17 11:11:46 -07002421
Paul Duffin46a26a82020-04-07 19:27:04 +01002422// The type of a structure that contains a field of type sdkLibraryScopeProperties
2423// for each apiscope in allApiScopes, e.g. something like:
Colin Crossd079e0b2022-08-16 10:27:33 -07002424//
2425// struct {
2426// Public sdkLibraryScopeProperties
2427// System sdkLibraryScopeProperties
2428// ...
2429// }
Paul Duffin46a26a82020-04-07 19:27:04 +01002430var allScopeStructType = createAllScopePropertiesStructType()
2431
2432// Dynamically create a structure type for each apiscope in allApiScopes.
2433func createAllScopePropertiesStructType() reflect.Type {
2434 var fields []reflect.StructField
2435 for _, apiScope := range allApiScopes {
2436 field := reflect.StructField{
2437 Name: apiScope.fieldName,
2438 Type: reflect.TypeOf(sdkLibraryScopeProperties{}),
2439 }
2440 fields = append(fields, field)
2441 }
2442
2443 return reflect.StructOf(fields)
2444}
2445
2446// Create an instance of the scope specific structure type and return a map
2447// from apiscope to a pointer to each scope specific field.
2448func createPropertiesInstance() (interface{}, map[*apiScope]*sdkLibraryScopeProperties) {
2449 allScopePropertiesPtr := reflect.New(allScopeStructType)
2450 allScopePropertiesStruct := allScopePropertiesPtr.Elem()
2451 scopeProperties := make(map[*apiScope]*sdkLibraryScopeProperties)
2452
2453 for _, apiScope := range allApiScopes {
2454 field := allScopePropertiesStruct.FieldByName(apiScope.fieldName)
2455 scopeProperties[apiScope] = field.Addr().Interface().(*sdkLibraryScopeProperties)
2456 }
2457
2458 return allScopePropertiesPtr.Interface(), scopeProperties
2459}
2460
Jaewoong Jung4f158ee2019-07-11 10:05:35 -07002461// java_sdk_library_import imports a prebuilt java_sdk_library.
Colin Cross79c7c262019-04-17 11:11:46 -07002462func sdkLibraryImportFactory() android.Module {
Paul Duffineedc5d52020-06-12 17:46:39 +01002463 module := &SdkLibraryImport{}
Colin Cross79c7c262019-04-17 11:11:46 -07002464
Paul Duffin46a26a82020-04-07 19:27:04 +01002465 allScopeProperties, scopeToProperties := createPropertiesInstance()
2466 module.scopeProperties = scopeToProperties
Jiakai Zhang9c4dc192023-02-09 00:09:24 +08002467 module.AddProperties(&module.properties, allScopeProperties, &module.importDexpreoptProperties)
Colin Cross79c7c262019-04-17 11:11:46 -07002468
Paul Duffinc3091c82020-05-08 14:16:20 +01002469 // Initialize information common between source and prebuilt.
Paul Duffin71b33cc2021-06-23 11:39:47 +01002470 module.initCommon(module)
Paul Duffinc3091c82020-05-08 14:16:20 +01002471
Paul Duffin0bdcb272020-02-06 15:24:57 +00002472 android.InitPrebuiltModule(module, &[]string{""})
Paul Duffindd46f712020-02-10 13:37:10 +00002473 android.InitApexModule(module)
Liz Kammer9a97a8f2023-09-18 13:36:41 -04002474 android.InitBazelModule(module)
Colin Cross79c7c262019-04-17 11:11:46 -07002475 InitJavaModule(module, android.HostAndDeviceSupported)
2476
Paul Duffin1b1e8062020-05-08 13:44:43 +01002477 module.SetDefaultableHook(func(mctx android.DefaultableHookContext) {
2478 if module.initCommonAfterDefaultsApplied(mctx) {
2479 module.createInternalModules(mctx)
2480 }
2481 })
Colin Cross79c7c262019-04-17 11:11:46 -07002482 return module
2483}
2484
Liz Kammer9a97a8f2023-09-18 13:36:41 -04002485// java_sdk_library bp2build converter
Chris Parsons637458d2023-09-19 20:09:00 +00002486func (i *SdkLibraryImport) ConvertWithBp2build(ctx android.Bp2buildMutatorContext) {
Liz Kammer9a97a8f2023-09-18 13:36:41 -04002487 nameToAttr := make(map[string]*bazel.Label)
2488
2489 for scope, props := range i.scopeProperties {
2490 if api := proptools.String(props.Current_api); api != "" {
2491 apiSurfaceFile := android.BazelLabelForModuleSrcSingle(ctx, api)
2492 nameToAttr[scope.name] = &apiSurfaceFile
2493 }
2494 }
2495
2496 attrs := bazelSdkLibraryAttributes{
2497 Public: nameToAttr["public"],
2498 System: nameToAttr["system"],
2499 Test: nameToAttr["test"],
2500 Module_lib: nameToAttr["module-lib"],
2501 System_server: nameToAttr["system-server"],
2502 }
2503 props := bazel.BazelTargetModuleProperties{
2504 Rule_class: "java_sdk_library",
2505 Bzl_load_location: "//build/bazel/rules/java:sdk_library.bzl",
2506 }
2507
2508 name := android.RemoveOptionalPrebuiltPrefix(i.Name())
2509 ctx.CreateBazelTargetModule(props, android.CommonAttributes{Name: name}, &attrs)
2510}
2511
Paul Duffin630b11e2021-07-15 13:35:26 +01002512var _ PermittedPackagesForUpdatableBootJars = (*SdkLibraryImport)(nil)
2513
2514func (module *SdkLibraryImport) PermittedPackagesForUpdatableBootJars() []string {
2515 return module.properties.Permitted_packages
2516}
2517
Paul Duffineedc5d52020-06-12 17:46:39 +01002518func (module *SdkLibraryImport) Prebuilt() *android.Prebuilt {
Colin Cross79c7c262019-04-17 11:11:46 -07002519 return &module.prebuilt
2520}
2521
Paul Duffineedc5d52020-06-12 17:46:39 +01002522func (module *SdkLibraryImport) Name() string {
Colin Cross79c7c262019-04-17 11:11:46 -07002523 return module.prebuilt.Name(module.ModuleBase.Name())
2524}
2525
Paul Duffineedc5d52020-06-12 17:46:39 +01002526func (module *SdkLibraryImport) createInternalModules(mctx android.DefaultableHookContext) {
Colin Cross79c7c262019-04-17 11:11:46 -07002527
Paul Duffin50061512020-01-21 16:31:05 +00002528 // If the build is configured to use prebuilts then force this to be preferred.
Jeongik Cha816a23a2020-07-08 01:09:23 +09002529 if mctx.Config().AlwaysUsePrebuiltSdks() {
Paul Duffin50061512020-01-21 16:31:05 +00002530 module.prebuilt.ForcePrefer()
2531 }
2532
Paul Duffin46a26a82020-04-07 19:27:04 +01002533 for apiScope, scopeProperties := range module.scopeProperties {
Paul Duffin56d44902020-01-31 13:36:25 +00002534 if len(scopeProperties.Jars) == 0 {
2535 continue
2536 }
2537
Paul Duffinbbb546b2020-04-09 00:07:11 +01002538 module.createJavaImportForStubs(mctx, apiScope, scopeProperties)
Paul Duffin3d1248c2020-04-09 00:10:17 +01002539
Paul Duffin0f8faff2020-05-20 16:18:00 +01002540 if len(scopeProperties.Stub_srcs) > 0 {
2541 module.createPrebuiltStubsSources(mctx, apiScope, scopeProperties)
2542 }
Jihoon Kang71c86832023-09-13 01:01:53 +00002543
2544 if scopeProperties.Current_api != nil {
2545 module.createPrebuiltApiContribution(mctx, apiScope, scopeProperties)
2546 }
Paul Duffin56d44902020-01-31 13:36:25 +00002547 }
Colin Cross79c7c262019-04-17 11:11:46 -07002548
2549 javaSdkLibraries := javaSdkLibraries(mctx.Config())
2550 javaSdkLibrariesLock.Lock()
2551 defer javaSdkLibrariesLock.Unlock()
2552 *javaSdkLibraries = append(*javaSdkLibraries, module.BaseModuleName())
2553}
2554
Paul Duffineedc5d52020-06-12 17:46:39 +01002555func (module *SdkLibraryImport) createJavaImportForStubs(mctx android.DefaultableHookContext, apiScope *apiScope, scopeProperties *sdkLibraryScopeProperties) {
Paul Duffinbbb546b2020-04-09 00:07:11 +01002556 // Creates a java import for the jar with ".stubs" suffix
2557 props := struct {
Paul Duffin1dbe3ca2020-05-16 09:57:59 +01002558 Name *string
2559 Sdk_version *string
2560 Libs []string
2561 Jars []string
Paul Duffin1267d872021-04-16 17:21:36 +01002562 Compile_dex *bool
Paul Duffinbf4de042022-09-27 12:41:52 +01002563
2564 android.UserSuppliedPrebuiltProperties
Paul Duffinbbb546b2020-04-09 00:07:11 +01002565 }{}
Paul Duffinc3091c82020-05-08 14:16:20 +01002566 props.Name = proptools.StringPtr(module.stubsLibraryModuleName(apiScope))
Paul Duffinbbb546b2020-04-09 00:07:11 +01002567 props.Sdk_version = scopeProperties.Sdk_version
2568 // Prepend any of the libs from the legacy public properties to the libs for each of the
2569 // scopes to avoid having to duplicate them in each scope.
2570 props.Libs = append(module.properties.Libs, scopeProperties.Libs...)
2571 props.Jars = scopeProperties.Jars
Paul Duffin1dbe3ca2020-05-16 09:57:59 +01002572
Paul Duffin38b57852020-05-13 16:08:09 +01002573 // The imports are preferred if the java_sdk_library_import is preferred.
Paul Duffinbf4de042022-09-27 12:41:52 +01002574 props.CopyUserSuppliedPropertiesFromPrebuilt(&module.prebuilt)
Paul Duffin859fe962020-05-15 10:20:31 +01002575
Paul Duffin1267d872021-04-16 17:21:36 +01002576 // The imports need to be compiled to dex if the java_sdk_library_import requests it.
Paul Duffinf4600f62021-05-13 22:34:45 +01002577 compileDex := module.properties.Compile_dex
2578 if module.stubLibrariesCompiledForDex() {
2579 compileDex = proptools.BoolPtr(true)
2580 }
2581 props.Compile_dex = compileDex
Paul Duffin1267d872021-04-16 17:21:36 +01002582
Paul Duffin859fe962020-05-15 10:20:31 +01002583 mctx.CreateModule(ImportFactory, &props, module.sdkComponentPropertiesForChildLibrary())
Paul Duffinbbb546b2020-04-09 00:07:11 +01002584}
2585
Paul Duffineedc5d52020-06-12 17:46:39 +01002586func (module *SdkLibraryImport) createPrebuiltStubsSources(mctx android.DefaultableHookContext, apiScope *apiScope, scopeProperties *sdkLibraryScopeProperties) {
Paul Duffin3d1248c2020-04-09 00:10:17 +01002587 props := struct {
Paul Duffinbf4de042022-09-27 12:41:52 +01002588 Name *string
2589 Srcs []string
2590
2591 android.UserSuppliedPrebuiltProperties
Paul Duffin3d1248c2020-04-09 00:10:17 +01002592 }{}
Paul Duffinc3091c82020-05-08 14:16:20 +01002593 props.Name = proptools.StringPtr(module.stubsSourceModuleName(apiScope))
Paul Duffin3d1248c2020-04-09 00:10:17 +01002594 props.Srcs = scopeProperties.Stub_srcs
Paul Duffin38b57852020-05-13 16:08:09 +01002595
2596 // The stubs source is preferred if the java_sdk_library_import is preferred.
Paul Duffinbf4de042022-09-27 12:41:52 +01002597 props.CopyUserSuppliedPropertiesFromPrebuilt(&module.prebuilt)
2598
Spandan Das2cc80ba2023-10-27 17:21:52 +00002599 mctx.CreateModule(PrebuiltStubsSourcesFactory, &props, module.sdkComponentPropertiesForChildLibrary())
Paul Duffin3d1248c2020-04-09 00:10:17 +01002600}
2601
Jihoon Kang71c86832023-09-13 01:01:53 +00002602func (module *SdkLibraryImport) createPrebuiltApiContribution(mctx android.DefaultableHookContext, apiScope *apiScope, scopeProperties *sdkLibraryScopeProperties) {
2603 api_file := scopeProperties.Current_api
2604 api_surface := &apiScope.name
2605
2606 props := struct {
2607 Name *string
2608 Api_surface *string
2609 Api_file *string
2610 Visibility []string
2611 }{}
2612
2613 props.Name = proptools.StringPtr(module.stubsSourceModuleName(apiScope) + ".api.contribution")
2614 props.Api_surface = api_surface
2615 props.Api_file = api_file
2616 props.Visibility = []string{"//visibility:override", "//visibility:public"}
2617
Spandan Das2cc80ba2023-10-27 17:21:52 +00002618 mctx.CreateModule(ApiContributionImportFactory, &props, module.sdkComponentPropertiesForChildLibrary())
Jihoon Kang71c86832023-09-13 01:01:53 +00002619}
2620
Paul Duffin44f1d842020-06-26 20:17:02 +01002621// Add the dependencies on the child module in the component deps mutator so that it
2622// creates references to the prebuilt and not the source modules.
2623func (module *SdkLibraryImport) ComponentDepsMutator(ctx android.BottomUpMutatorContext) {
Paul Duffin46a26a82020-04-07 19:27:04 +01002624 for apiScope, scopeProperties := range module.scopeProperties {
Paul Duffin56d44902020-01-31 13:36:25 +00002625 if len(scopeProperties.Jars) == 0 {
2626 continue
2627 }
2628
2629 // Add dependencies to the prebuilt stubs library
Paul Duffin864116c2021-04-02 10:24:13 +01002630 ctx.AddVariationDependencies(nil, apiScope.stubsTag, android.PrebuiltNameFromSource(module.stubsLibraryModuleName(apiScope)))
Paul Duffin0f8faff2020-05-20 16:18:00 +01002631
2632 if len(scopeProperties.Stub_srcs) > 0 {
2633 // Add dependencies to the prebuilt stubs source library
Paul Duffin864116c2021-04-02 10:24:13 +01002634 ctx.AddVariationDependencies(nil, apiScope.stubsSourceTag, android.PrebuiltNameFromSource(module.stubsSourceModuleName(apiScope)))
Paul Duffin0f8faff2020-05-20 16:18:00 +01002635 }
Paul Duffin56d44902020-01-31 13:36:25 +00002636 }
Paul Duffin44f1d842020-06-26 20:17:02 +01002637}
2638
2639// Add other dependencies as normal.
2640func (module *SdkLibraryImport) DepsMutator(ctx android.BottomUpMutatorContext) {
Paul Duffineedc5d52020-06-12 17:46:39 +01002641
2642 implName := module.implLibraryModuleName()
2643 if ctx.OtherModuleExists(implName) {
2644 ctx.AddVariationDependencies(nil, implLibraryTag, implName)
2645
2646 xmlPermissionsModuleName := module.xmlPermissionsModuleName()
2647 if module.sharedLibrary() && ctx.OtherModuleExists(xmlPermissionsModuleName) {
2648 // Add dependency to the rule for generating the xml permissions file
2649 ctx.AddDependency(module, xmlPermissionsFileTag, xmlPermissionsModuleName)
2650 }
2651 }
Colin Cross79c7c262019-04-17 11:11:46 -07002652}
2653
Jiakai Zhang204356f2021-09-09 08:12:46 +00002654func (module *SdkLibraryImport) AndroidMkEntries() []android.AndroidMkEntries {
2655 // For an SDK library imported from a prebuilt APEX, we don't need a Make module for itself, as we
2656 // don't need to install it. However, we need to add its dexpreopt outputs as sub-modules, if it
2657 // is preopted.
2658 dexpreoptEntries := module.dexpreopter.AndroidMkEntriesForApex()
2659 return append(dexpreoptEntries, android.AndroidMkEntries{Disabled: true})
2660}
2661
Jiyong Park45bf82e2020-12-15 22:29:02 +09002662var _ android.ApexModule = (*SdkLibraryImport)(nil)
2663
2664// Implements android.ApexModule
Paul Duffineedc5d52020-06-12 17:46:39 +01002665func (module *SdkLibraryImport) DepIsInSameApex(mctx android.BaseModuleContext, dep android.Module) bool {
2666 depTag := mctx.OtherModuleDependencyTag(dep)
2667 if depTag == xmlPermissionsFileTag {
2668 return true
2669 }
2670
2671 // None of the other dependencies of the java_sdk_library_import are in the same apex
2672 // as the one that references this module.
2673 return false
2674}
2675
Jiyong Park45bf82e2020-12-15 22:29:02 +09002676// Implements android.ApexModule
Dan Albertc8060532020-07-22 22:32:17 -07002677func (module *SdkLibraryImport) ShouldSupportSdkVersion(ctx android.BaseModuleContext,
2678 sdkVersion android.ApiLevel) error {
Jooyung Han749dc692020-04-15 11:03:39 +09002679 // we don't check prebuilt modules for sdk_version
2680 return nil
2681}
2682
Paul Duffinea8f8082021-06-24 13:25:57 +01002683// Implements android.ApexModule
2684func (module *SdkLibraryImport) UniqueApexVariations() bool {
2685 return module.uniqueApexVariations()
2686}
2687
Paul Duffin09817d62022-04-28 17:45:11 +01002688// MinSdkVersion - Implements hiddenAPIModule
Spandan Das8c9ae7e2023-03-03 21:20:36 +00002689func (module *SdkLibraryImport) MinSdkVersion(ctx android.EarlyModuleContext) android.ApiLevel {
2690 return android.NoneApiLevel
Paul Duffin09817d62022-04-28 17:45:11 +01002691}
2692
2693var _ hiddenAPIModule = (*SdkLibraryImport)(nil)
2694
Paul Duffineedc5d52020-06-12 17:46:39 +01002695func (module *SdkLibraryImport) OutputFiles(tag string) (android.Paths, error) {
Paul Duffin1e940d52022-04-29 14:21:25 +01002696 paths, err := module.commonOutputFiles(tag)
2697 if paths != nil || err != nil {
2698 return paths, err
2699 }
2700 if module.implLibraryModule != nil {
2701 return module.implLibraryModule.OutputFiles(tag)
2702 } else {
2703 return nil, nil
2704 }
Paul Duffin46dc45a2020-05-14 15:39:10 +01002705}
2706
Paul Duffineedc5d52020-06-12 17:46:39 +01002707func (module *SdkLibraryImport) GenerateAndroidBuildActions(ctx android.ModuleContext) {
Paul Duffina2ae7e02020-09-11 11:55:00 +01002708 module.generateCommonBuildActions(ctx)
2709
Jeongik Chad5fe8782021-07-08 01:13:11 +09002710 // Assume that source module(sdk_library) is installed in /<sdk_library partition>/framework
2711 module.installFile = android.PathForModuleInstall(ctx, "framework", module.Stem()+".jar")
2712
Paul Duffin0f8faff2020-05-20 16:18:00 +01002713 // Record the paths to the prebuilt stubs library and stubs source.
Colin Cross79c7c262019-04-17 11:11:46 -07002714 ctx.VisitDirectDeps(func(to android.Module) {
2715 tag := ctx.OtherModuleDependencyTag(to)
2716
Paul Duffin0f8faff2020-05-20 16:18:00 +01002717 // Extract information from any of the scope specific dependencies.
2718 if scopeTag, ok := tag.(scopeDependencyTag); ok {
2719 apiScope := scopeTag.apiScope
2720 scopePaths := module.getScopePathsCreateIfNeeded(apiScope)
2721
2722 // Extract information from the dependency. The exact information extracted
2723 // is determined by the nature of the dependency which is determined by the tag.
2724 scopeTag.extractDepInfo(ctx, to, scopePaths)
Paul Duffineedc5d52020-06-12 17:46:39 +01002725 } else if tag == implLibraryTag {
2726 if implLibrary, ok := to.(*Library); ok {
2727 module.implLibraryModule = implLibrary
2728 } else {
2729 ctx.ModuleErrorf("implementation library must be of type *java.Library but was %T", to)
2730 }
2731 } else if tag == xmlPermissionsFileTag {
2732 if xmlPermissionsFileModule, ok := to.(*sdkLibraryXml); ok {
2733 module.xmlPermissionsFileModule = xmlPermissionsFileModule
2734 } else {
2735 ctx.ModuleErrorf("xml permissions file module must be of type *sdkLibraryXml but was %T", to)
2736 }
Colin Cross79c7c262019-04-17 11:11:46 -07002737 }
2738 })
Paul Duffin0f8faff2020-05-20 16:18:00 +01002739
2740 // Populate the scope paths with information from the properties.
2741 for apiScope, scopeProperties := range module.scopeProperties {
2742 if len(scopeProperties.Jars) == 0 {
2743 continue
2744 }
2745
2746 paths := module.getScopePathsCreateIfNeeded(apiScope)
Anton Hanssond78eb762021-09-21 15:25:12 +01002747 paths.annotationsZip = android.OptionalPathForModuleSrc(ctx, scopeProperties.Annotations)
Paul Duffin0f8faff2020-05-20 16:18:00 +01002748 paths.currentApiFilePath = android.OptionalPathForModuleSrc(ctx, scopeProperties.Current_api)
2749 paths.removedApiFilePath = android.OptionalPathForModuleSrc(ctx, scopeProperties.Removed_api)
2750 }
Paul Duffin39853512021-02-26 11:09:39 +00002751
2752 if ctx.Device() {
2753 // If this is a variant created for a prebuilt_apex then use the dex implementation jar
2754 // obtained from the associated deapexer module.
2755 ai := ctx.Provider(android.ApexInfoProvider).(android.ApexInfo)
2756 if ai.ForPrebuiltApex {
Paul Duffin39853512021-02-26 11:09:39 +00002757 // Get the path of the dex implementation jar from the `deapexer` module.
Martin Stjernholm44825602021-09-17 01:44:12 +01002758 di := android.FindDeapexerProviderForModule(ctx)
2759 if di == nil {
2760 return // An error has been reported by FindDeapexerProviderForModule.
2761 }
Jiakai Zhang81e46812023-02-08 21:56:07 +08002762 dexJarFileApexRootRelative := apexRootRelativePathToJavaLib(module.BaseModuleName())
2763 if dexOutputPath := di.PrebuiltExportPath(dexJarFileApexRootRelative); dexOutputPath != nil {
Martin Stjernholm8be1e6d2021-09-15 03:34:04 +01002764 dexJarFile := makeDexJarPathFromPath(dexOutputPath)
2765 module.dexJarFile = dexJarFile
Jiakai Zhang204356f2021-09-09 08:12:46 +00002766 installPath := android.PathForModuleInPartitionInstall(
Jiakai Zhang81e46812023-02-08 21:56:07 +08002767 ctx, "apex", ai.ApexVariationName, dexJarFileApexRootRelative)
Jiakai Zhang204356f2021-09-09 08:12:46 +00002768 module.installFile = installPath
Martin Stjernholm8be1e6d2021-09-15 03:34:04 +01002769 module.initHiddenAPI(ctx, dexJarFile, module.findScopePaths(apiScopePublic).stubsImplPath[0], nil)
Jiakai Zhang204356f2021-09-09 08:12:46 +00002770
Jiakai Zhang204356f2021-09-09 08:12:46 +00002771 module.dexpreopter.installPath = module.dexpreopter.getInstallPath(ctx, installPath)
2772 module.dexpreopter.isSDKLibrary = true
2773 module.dexpreopter.uncompressedDex = shouldUncompressDex(ctx, &module.dexpreopter)
Jiakai Zhang81e46812023-02-08 21:56:07 +08002774
2775 if profilePath := di.PrebuiltExportPath(dexJarFileApexRootRelative + ".prof"); profilePath != nil {
2776 module.dexpreopter.inputProfilePathOnHost = profilePath
2777 }
2778
2779 // Dexpreopting.
Jiakai Zhang204356f2021-09-09 08:12:46 +00002780 module.dexpreopt(ctx, dexOutputPath)
Paul Duffin39853512021-02-26 11:09:39 +00002781 } else {
2782 // This should never happen as a variant for a prebuilt_apex is only created if the
2783 // prebuilt_apex has been configured to export the java library dex file.
Martin Stjernholm44825602021-09-17 01:44:12 +01002784 ctx.ModuleErrorf("internal error: no dex implementation jar available from prebuilt APEX %s", di.ApexModuleName())
Paul Duffin39853512021-02-26 11:09:39 +00002785 }
2786 }
2787 }
Colin Cross79c7c262019-04-17 11:11:46 -07002788}
2789
Jiyong Parkf1691d22021-03-29 20:11:58 +09002790func (module *SdkLibraryImport) sdkJars(ctx android.BaseModuleContext, sdkVersion android.SdkSpec, headerJars bool) android.Paths {
Paul Duffineedc5d52020-06-12 17:46:39 +01002791
2792 // For consistency with SdkLibrary make the implementation jar available to libraries that
2793 // are within the same APEX.
2794 implLibraryModule := module.implLibraryModule
Colin Cross56a83212020-09-15 18:30:11 -07002795 if implLibraryModule != nil && withinSameApexesAs(ctx, module) {
Paul Duffineedc5d52020-06-12 17:46:39 +01002796 if headerJars {
2797 return implLibraryModule.HeaderJars()
2798 } else {
2799 return implLibraryModule.ImplementationJars()
2800 }
2801 }
2802
Paul Duffin23970f42020-05-20 14:20:02 +01002803 return module.selectHeaderJarsForSdkVersion(ctx, sdkVersion)
Paul Duffin56d44902020-01-31 13:36:25 +00002804}
2805
Colin Cross79c7c262019-04-17 11:11:46 -07002806// to satisfy SdkLibraryDependency interface
Jiyong Parkf1691d22021-03-29 20:11:58 +09002807func (module *SdkLibraryImport) SdkHeaderJars(ctx android.BaseModuleContext, sdkVersion android.SdkSpec) android.Paths {
Colin Cross79c7c262019-04-17 11:11:46 -07002808 // This module is just a wrapper for the prebuilt stubs.
Paul Duffineedc5d52020-06-12 17:46:39 +01002809 return module.sdkJars(ctx, sdkVersion, true)
Colin Cross79c7c262019-04-17 11:11:46 -07002810}
2811
2812// to satisfy SdkLibraryDependency interface
Jiyong Parkf1691d22021-03-29 20:11:58 +09002813func (module *SdkLibraryImport) SdkImplementationJars(ctx android.BaseModuleContext, sdkVersion android.SdkSpec) android.Paths {
Colin Cross79c7c262019-04-17 11:11:46 -07002814 // This module is just a wrapper for the stubs.
Paul Duffineedc5d52020-06-12 17:46:39 +01002815 return module.sdkJars(ctx, sdkVersion, false)
2816}
2817
Ulya Trafimovichdbf31662020-12-17 12:07:54 +00002818// to satisfy UsesLibraryDependency interface
Martin Stjernholm8be1e6d2021-09-15 03:34:04 +01002819func (module *SdkLibraryImport) DexJarBuildPath() OptionalDexJarPath {
Paul Duffin39853512021-02-26 11:09:39 +00002820 // The dex implementation jar extracted from the .apex file should be used in preference to the
2821 // source.
Martin Stjernholm8be1e6d2021-09-15 03:34:04 +01002822 if module.dexJarFile.IsSet() {
Paul Duffin39853512021-02-26 11:09:39 +00002823 return module.dexJarFile
2824 }
Paul Duffineedc5d52020-06-12 17:46:39 +01002825 if module.implLibraryModule == nil {
Martin Stjernholm8be1e6d2021-09-15 03:34:04 +01002826 return makeUnsetDexJarPath()
Paul Duffineedc5d52020-06-12 17:46:39 +01002827 } else {
2828 return module.implLibraryModule.DexJarBuildPath()
2829 }
2830}
2831
Ulya Trafimovichdbf31662020-12-17 12:07:54 +00002832// to satisfy UsesLibraryDependency interface
Ulya Trafimovich31e444e2020-08-14 17:32:16 +01002833func (module *SdkLibraryImport) DexJarInstallPath() android.Path {
Jeongik Chad5fe8782021-07-08 01:13:11 +09002834 return module.installFile
Ulya Trafimovich31e444e2020-08-14 17:32:16 +01002835}
2836
Ulya Trafimovichdbf31662020-12-17 12:07:54 +00002837// to satisfy UsesLibraryDependency interface
2838func (module *SdkLibraryImport) ClassLoaderContexts() dexpreopt.ClassLoaderContextMap {
2839 return nil
2840}
2841
Paul Duffineedc5d52020-06-12 17:46:39 +01002842// to satisfy apex.javaDependency interface
2843func (module *SdkLibraryImport) JacocoReportClassesFile() android.Path {
2844 if module.implLibraryModule == nil {
2845 return nil
2846 } else {
2847 return module.implLibraryModule.JacocoReportClassesFile()
2848 }
2849}
2850
2851// to satisfy apex.javaDependency interface
Colin Cross08dca382020-07-21 20:31:17 -07002852func (module *SdkLibraryImport) LintDepSets() LintDepSets {
2853 if module.implLibraryModule == nil {
2854 return LintDepSets{}
2855 } else {
2856 return module.implLibraryModule.LintDepSets()
2857 }
2858}
2859
Spandan Das17854f52022-01-14 21:19:14 +00002860func (module *SdkLibraryImport) GetStrictUpdatabilityLinting() bool {
Jaewoong Jung476b9d62021-05-10 15:30:00 -07002861 if module.implLibraryModule == nil {
2862 return false
2863 } else {
Spandan Das17854f52022-01-14 21:19:14 +00002864 return module.implLibraryModule.GetStrictUpdatabilityLinting()
Jaewoong Jung476b9d62021-05-10 15:30:00 -07002865 }
2866}
2867
Spandan Das17854f52022-01-14 21:19:14 +00002868func (module *SdkLibraryImport) SetStrictUpdatabilityLinting(strictLinting bool) {
Jaewoong Jung476b9d62021-05-10 15:30:00 -07002869 if module.implLibraryModule != nil {
Spandan Das17854f52022-01-14 21:19:14 +00002870 module.implLibraryModule.SetStrictUpdatabilityLinting(strictLinting)
Jaewoong Jung476b9d62021-05-10 15:30:00 -07002871 }
2872}
2873
Colin Cross08dca382020-07-21 20:31:17 -07002874// to satisfy apex.javaDependency interface
Paul Duffineedc5d52020-06-12 17:46:39 +01002875func (module *SdkLibraryImport) Stem() string {
2876 return module.BaseModuleName()
Colin Cross79c7c262019-04-17 11:11:46 -07002877}
Jiyong Parke3833882020-02-17 17:28:10 +09002878
Paul Duffin44b481b2020-06-17 16:59:43 +01002879var _ ApexDependency = (*SdkLibraryImport)(nil)
2880
2881// to satisfy java.ApexDependency interface
2882func (module *SdkLibraryImport) HeaderJars() android.Paths {
2883 if module.implLibraryModule == nil {
2884 return nil
2885 } else {
2886 return module.implLibraryModule.HeaderJars()
2887 }
2888}
2889
2890// to satisfy java.ApexDependency interface
2891func (module *SdkLibraryImport) ImplementationAndResourcesJars() android.Paths {
2892 if module.implLibraryModule == nil {
2893 return nil
2894 } else {
2895 return module.implLibraryModule.ImplementationAndResourcesJars()
2896 }
2897}
2898
Jiakai Zhang204356f2021-09-09 08:12:46 +00002899// to satisfy java.DexpreopterInterface interface
2900func (module *SdkLibraryImport) IsInstallable() bool {
2901 return true
2902}
2903
Paul Duffinfef55002021-06-17 14:56:05 +01002904var _ android.RequiredFilesFromPrebuiltApex = (*SdkLibraryImport)(nil)
2905
Paul Duffinb4bbf2c2021-06-17 15:59:07 +01002906func (module *SdkLibraryImport) RequiredFilesFromPrebuiltApex(ctx android.BaseModuleContext) []string {
Paul Duffinfef55002021-06-17 14:56:05 +01002907 name := module.BaseModuleName()
Jiakai Zhang81e46812023-02-08 21:56:07 +08002908 return requiredFilesFromPrebuiltApexForImport(name, &module.dexpreopter)
Paul Duffinfef55002021-06-17 14:56:05 +01002909}
2910
Jiyong Parke3833882020-02-17 17:28:10 +09002911// java_sdk_library_xml
Jiyong Parke3833882020-02-17 17:28:10 +09002912type sdkLibraryXml struct {
2913 android.ModuleBase
2914 android.DefaultableModuleBase
2915 android.ApexModuleBase
2916
2917 properties sdkLibraryXmlProperties
2918
2919 outputFilePath android.OutputPath
2920 installDirPath android.InstallPath
Colin Cross56a83212020-09-15 18:30:11 -07002921
2922 hideApexVariantFromMake bool
Jiyong Parke3833882020-02-17 17:28:10 +09002923}
2924
2925type sdkLibraryXmlProperties struct {
2926 // canonical name of the lib
2927 Lib_name *string
Pedro Loureiro9956e5e2021-09-07 17:21:59 +00002928
2929 // Signals that this shared library is part of the bootclasspath starting
2930 // on the version indicated in this attribute.
2931 //
2932 // This will make platforms at this level and above to ignore
2933 // <uses-library> tags with this library name because the library is already
2934 // available
2935 On_bootclasspath_since *string
2936
2937 // Signals that this shared library was part of the bootclasspath before
2938 // (but not including) the version indicated in this attribute.
2939 //
2940 // The system will automatically add a <uses-library> tag with this library to
2941 // apps that target any SDK less than the version indicated in this attribute.
2942 On_bootclasspath_before *string
2943
2944 // Indicates that PackageManager should ignore this shared library if the
2945 // platform is below the version indicated in this attribute.
2946 //
2947 // This means that the device won't recognise this library as installed.
2948 Min_device_sdk *string
2949
2950 // Indicates that PackageManager should ignore this shared library if the
2951 // platform is above the version indicated in this attribute.
2952 //
2953 // This means that the device won't recognise this library as installed.
2954 Max_device_sdk *string
Pedro Loureiroc3621422021-09-28 15:40:23 +00002955
2956 // The SdkLibrary's min api level as a string
2957 //
2958 // This value comes from the ApiLevel of the MinSdkVersion property.
2959 Sdk_library_min_api_level *string
Jiyong Parke3833882020-02-17 17:28:10 +09002960}
2961
2962// java_sdk_library_xml builds the permission xml file for a java_sdk_library.
2963// Not to be used directly by users. java_sdk_library internally uses this.
2964func sdkLibraryXmlFactory() android.Module {
2965 module := &sdkLibraryXml{}
2966
2967 module.AddProperties(&module.properties)
2968
2969 android.InitApexModule(module)
2970 android.InitAndroidArchModule(module, android.DeviceSupported, android.MultilibCommon)
2971
2972 return module
2973}
2974
Colin Crossaede88c2020-08-11 12:17:01 -07002975func (module *sdkLibraryXml) UniqueApexVariations() bool {
2976 // sdkLibraryXml needs a unique variation per APEX because the generated XML file contains the path to the
2977 // mounted APEX, which contains the name of the APEX.
2978 return true
2979}
2980
Jiyong Parke3833882020-02-17 17:28:10 +09002981// from android.PrebuiltEtcModule
Jooyung Han0703fd82020-08-26 22:11:53 +09002982func (module *sdkLibraryXml) BaseDir() string {
2983 return "etc"
2984}
2985
2986// from android.PrebuiltEtcModule
Jiyong Parke3833882020-02-17 17:28:10 +09002987func (module *sdkLibraryXml) SubDir() string {
2988 return "permissions"
2989}
2990
2991// from android.PrebuiltEtcModule
2992func (module *sdkLibraryXml) OutputFile() android.OutputPath {
2993 return module.outputFilePath
2994}
2995
2996// from android.ApexModule
2997func (module *sdkLibraryXml) AvailableFor(what string) bool {
2998 return true
2999}
3000
3001func (module *sdkLibraryXml) DepsMutator(ctx android.BottomUpMutatorContext) {
3002 // do nothing
3003}
3004
Jiyong Park45bf82e2020-12-15 22:29:02 +09003005var _ android.ApexModule = (*sdkLibraryXml)(nil)
3006
3007// Implements android.ApexModule
Dan Albertc8060532020-07-22 22:32:17 -07003008func (module *sdkLibraryXml) ShouldSupportSdkVersion(ctx android.BaseModuleContext,
3009 sdkVersion android.ApiLevel) error {
Jooyung Han749dc692020-04-15 11:03:39 +09003010 // sdkLibraryXml doesn't need to be checked separately because java_sdk_library is checked
3011 return nil
3012}
3013
Jiyong Parke3833882020-02-17 17:28:10 +09003014// File path to the runtime implementation library
Colin Cross56a83212020-09-15 18:30:11 -07003015func (module *sdkLibraryXml) implPath(ctx android.ModuleContext) string {
Jiyong Parke3833882020-02-17 17:28:10 +09003016 implName := proptools.String(module.properties.Lib_name)
Colin Cross56a83212020-09-15 18:30:11 -07003017 if apexInfo := ctx.Provider(android.ApexInfoProvider).(android.ApexInfo); !apexInfo.IsForPlatform() {
Colin Crosse07f2312020-08-13 11:24:56 -07003018 // TODO(b/146468504): ApexVariationName() is only a soong module name, not apex name.
Jiyong Parke3833882020-02-17 17:28:10 +09003019 // In most cases, this works fine. But when apex_name is set or override_apex is used
3020 // this can be wrong.
Colin Cross56a83212020-09-15 18:30:11 -07003021 return fmt.Sprintf("/apex/%s/javalib/%s.jar", apexInfo.ApexVariationName, implName)
Jiyong Parke3833882020-02-17 17:28:10 +09003022 }
3023 partition := "system"
3024 if module.SocSpecific() {
3025 partition = "vendor"
3026 } else if module.DeviceSpecific() {
3027 partition = "odm"
3028 } else if module.ProductSpecific() {
3029 partition = "product"
3030 } else if module.SystemExtSpecific() {
3031 partition = "system_ext"
3032 }
3033 return "/" + partition + "/framework/" + implName + ".jar"
3034}
3035
Pedro Loureiro9956e5e2021-09-07 17:21:59 +00003036func formattedOptionalSdkLevelAttribute(ctx android.ModuleContext, attrName string, value *string) string {
3037 if value == nil {
3038 return ""
3039 }
3040 apiLevel, err := android.ApiLevelFromUser(ctx, *value)
3041 if err != nil {
Pedro Loureiroba6682f2021-10-29 09:32:32 +00003042 // attributes in bp files have underscores but in the xml have dashes.
3043 ctx.PropertyErrorf(strings.ReplaceAll(attrName, "-", "_"), err.Error())
Pedro Loureiro9956e5e2021-09-07 17:21:59 +00003044 return ""
3045 }
Pedro Loureirob638c622021-12-22 15:28:05 +00003046 if apiLevel.IsCurrent() {
3047 // passing "current" would always mean a future release, never the current (or the current in
3048 // progress) which means some conditions would never be triggered.
3049 ctx.PropertyErrorf(strings.ReplaceAll(attrName, "-", "_"),
3050 `"current" is not an allowed value for this attribute`)
3051 return ""
3052 }
Pedro Loureiro48991222022-06-17 20:01:21 +00003053 // "safeValue" is safe because it translates finalized codenames to a string
3054 // with their SDK int.
3055 safeValue := apiLevel.String()
3056 return formattedOptionalAttribute(attrName, &safeValue)
Pedro Loureiro9956e5e2021-09-07 17:21:59 +00003057}
3058
3059// formats an attribute for the xml permissions file if the value is not null
3060// returns empty string otherwise
3061func formattedOptionalAttribute(attrName string, value *string) string {
3062 if value == nil {
3063 return ""
3064 }
3065 return fmt.Sprintf(` %s=\"%s\"\n`, attrName, *value)
3066}
3067
3068func (module *sdkLibraryXml) permissionsContents(ctx android.ModuleContext) string {
3069 libName := proptools.String(module.properties.Lib_name)
3070 libNameAttr := formattedOptionalAttribute("name", &libName)
3071 filePath := module.implPath(ctx)
3072 filePathAttr := formattedOptionalAttribute("file", &filePath)
Pedro Loureiroba6682f2021-10-29 09:32:32 +00003073 implicitFromAttr := formattedOptionalSdkLevelAttribute(ctx, "on-bootclasspath-since", module.properties.On_bootclasspath_since)
3074 implicitUntilAttr := formattedOptionalSdkLevelAttribute(ctx, "on-bootclasspath-before", module.properties.On_bootclasspath_before)
3075 minSdkAttr := formattedOptionalSdkLevelAttribute(ctx, "min-device-sdk", module.properties.Min_device_sdk)
3076 maxSdkAttr := formattedOptionalSdkLevelAttribute(ctx, "max-device-sdk", module.properties.Max_device_sdk)
Pedro Loureiro196d3e62021-12-22 19:53:01 +00003077 // <library> is understood in all android versions whereas <apex-library> is only understood from API T (and ignored before that).
3078 // 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 +00003079 var libraryTag string
3080 if module.properties.Min_device_sdk != nil {
Pedro Loureiro196d3e62021-12-22 19:53:01 +00003081 libraryTag = ` <apex-library\n`
Pedro Loureiroc3621422021-09-28 15:40:23 +00003082 } else {
3083 libraryTag = ` <library\n`
3084 }
Pedro Loureiro9956e5e2021-09-07 17:21:59 +00003085
3086 return strings.Join([]string{
3087 `<?xml version=\"1.0\" encoding=\"utf-8\"?>\n`,
3088 `<!-- Copyright (C) 2018 The Android Open Source Project\n`,
3089 `\n`,
3090 ` Licensed under the Apache License, Version 2.0 (the \"License\");\n`,
3091 ` you may not use this file except in compliance with the License.\n`,
3092 ` You may obtain a copy of the License at\n`,
3093 `\n`,
3094 ` http://www.apache.org/licenses/LICENSE-2.0\n`,
3095 `\n`,
3096 ` Unless required by applicable law or agreed to in writing, software\n`,
3097 ` distributed under the License is distributed on an \"AS IS\" BASIS,\n`,
3098 ` WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\n`,
3099 ` See the License for the specific language governing permissions and\n`,
3100 ` limitations under the License.\n`,
3101 `-->\n`,
3102 `<permissions>\n`,
Pedro Loureiroc3621422021-09-28 15:40:23 +00003103 libraryTag,
Pedro Loureiro9956e5e2021-09-07 17:21:59 +00003104 libNameAttr,
3105 filePathAttr,
3106 implicitFromAttr,
3107 implicitUntilAttr,
3108 minSdkAttr,
3109 maxSdkAttr,
3110 ` />\n`,
3111 `</permissions>\n`}, "")
3112}
3113
Jiyong Parke3833882020-02-17 17:28:10 +09003114func (module *sdkLibraryXml) GenerateAndroidBuildActions(ctx android.ModuleContext) {
Colin Cross56a83212020-09-15 18:30:11 -07003115 module.hideApexVariantFromMake = !ctx.Provider(android.ApexInfoProvider).(android.ApexInfo).IsForPlatform()
3116
Jiyong Parke3833882020-02-17 17:28:10 +09003117 libName := proptools.String(module.properties.Lib_name)
Pedro Loureiroc3621422021-09-28 15:40:23 +00003118 module.selfValidate(ctx)
Pedro Loureiro9956e5e2021-09-07 17:21:59 +00003119 xmlContent := module.permissionsContents(ctx)
Jiyong Parke3833882020-02-17 17:28:10 +09003120
3121 module.outputFilePath = android.PathForModuleOut(ctx, libName+".xml").OutputPath
Colin Crossf1a035e2020-11-16 17:32:30 -08003122 rule := android.NewRuleBuilder(pctx, ctx)
Jiyong Parke3833882020-02-17 17:28:10 +09003123 rule.Command().
3124 Text("/bin/bash -c \"echo -e '" + xmlContent + "'\" > ").
3125 Output(module.outputFilePath)
3126
Colin Crossf1a035e2020-11-16 17:32:30 -08003127 rule.Build("java_sdk_xml", "Permission XML")
Jiyong Parke3833882020-02-17 17:28:10 +09003128
3129 module.installDirPath = android.PathForModuleInstall(ctx, "etc", module.SubDir())
3130}
3131
3132func (module *sdkLibraryXml) AndroidMkEntries() []android.AndroidMkEntries {
Colin Cross56a83212020-09-15 18:30:11 -07003133 if module.hideApexVariantFromMake {
satayev8f088b02021-12-06 11:40:46 +00003134 return []android.AndroidMkEntries{{
Jiyong Parke3833882020-02-17 17:28:10 +09003135 Disabled: true,
3136 }}
3137 }
3138
satayev8f088b02021-12-06 11:40:46 +00003139 return []android.AndroidMkEntries{{
Jiyong Parke3833882020-02-17 17:28:10 +09003140 Class: "ETC",
3141 OutputFile: android.OptionalPathForPath(module.outputFilePath),
3142 ExtraEntries: []android.AndroidMkExtraEntriesFunc{
Colin Crossaa255532020-07-03 13:18:24 -07003143 func(ctx android.AndroidMkExtraEntriesContext, entries *android.AndroidMkEntries) {
Jiyong Parke3833882020-02-17 17:28:10 +09003144 entries.SetString("LOCAL_MODULE_TAGS", "optional")
Colin Crossc68db4b2021-11-11 18:59:15 -08003145 entries.SetString("LOCAL_MODULE_PATH", module.installDirPath.String())
Jiyong Parke3833882020-02-17 17:28:10 +09003146 entries.SetString("LOCAL_INSTALLED_MODULE_STEM", module.outputFilePath.Base())
3147 },
3148 },
3149 }}
3150}
Paul Duffindd46f712020-02-10 13:37:10 +00003151
Pedro Loureiroc3621422021-09-28 15:40:23 +00003152func (module *sdkLibraryXml) selfValidate(ctx android.ModuleContext) {
3153 module.validateAtLeastTAttributes(ctx)
3154 module.validateMinAndMaxDeviceSdk(ctx)
3155 module.validateMinMaxDeviceSdkAndModuleMinSdk(ctx)
3156 module.validateOnBootclasspathBeforeRequirements(ctx)
3157}
3158
3159func (module *sdkLibraryXml) validateAtLeastTAttributes(ctx android.ModuleContext) {
3160 t := android.ApiLevelOrPanic(ctx, "Tiramisu")
3161 module.attrAtLeastT(ctx, t, module.properties.Min_device_sdk, "min_device_sdk")
3162 module.attrAtLeastT(ctx, t, module.properties.Max_device_sdk, "max_device_sdk")
3163 module.attrAtLeastT(ctx, t, module.properties.On_bootclasspath_before, "on_bootclasspath_before")
3164 module.attrAtLeastT(ctx, t, module.properties.On_bootclasspath_since, "on_bootclasspath_since")
3165}
3166
3167func (module *sdkLibraryXml) attrAtLeastT(ctx android.ModuleContext, t android.ApiLevel, attr *string, attrName string) {
3168 if attr != nil {
3169 if level, err := android.ApiLevelFromUser(ctx, *attr); err == nil {
3170 // we will inform the user of invalid inputs when we try to write the
3171 // permissions xml file so we don't need to do it here
3172 if t.GreaterThan(level) {
3173 ctx.PropertyErrorf(attrName, "Attribute value needs to be at least T")
3174 }
3175 }
3176 }
3177}
3178
3179func (module *sdkLibraryXml) validateMinAndMaxDeviceSdk(ctx android.ModuleContext) {
3180 if module.properties.Min_device_sdk != nil && module.properties.Max_device_sdk != nil {
3181 min, minErr := android.ApiLevelFromUser(ctx, *module.properties.Min_device_sdk)
3182 max, maxErr := android.ApiLevelFromUser(ctx, *module.properties.Max_device_sdk)
3183 if minErr == nil && maxErr == nil {
3184 // we will inform the user of invalid inputs when we try to write the
3185 // permissions xml file so we don't need to do it here
3186 if min.GreaterThan(max) {
3187 ctx.ModuleErrorf("min_device_sdk can't be greater than max_device_sdk")
3188 }
3189 }
3190 }
3191}
3192
3193func (module *sdkLibraryXml) validateMinMaxDeviceSdkAndModuleMinSdk(ctx android.ModuleContext) {
3194 moduleMinApi := android.ApiLevelOrPanic(ctx, *module.properties.Sdk_library_min_api_level)
3195 if module.properties.Min_device_sdk != nil {
3196 api, err := android.ApiLevelFromUser(ctx, *module.properties.Min_device_sdk)
3197 if err == nil {
3198 if moduleMinApi.GreaterThan(api) {
3199 ctx.PropertyErrorf("min_device_sdk", "Can't be less than module's min sdk (%s)", moduleMinApi)
3200 }
3201 }
3202 }
3203 if module.properties.Max_device_sdk != nil {
3204 api, err := android.ApiLevelFromUser(ctx, *module.properties.Max_device_sdk)
3205 if err == nil {
3206 if moduleMinApi.GreaterThan(api) {
3207 ctx.PropertyErrorf("max_device_sdk", "Can't be less than module's min sdk (%s)", moduleMinApi)
3208 }
3209 }
3210 }
3211}
3212
3213func (module *sdkLibraryXml) validateOnBootclasspathBeforeRequirements(ctx android.ModuleContext) {
3214 moduleMinApi := android.ApiLevelOrPanic(ctx, *module.properties.Sdk_library_min_api_level)
3215 if module.properties.On_bootclasspath_before != nil {
3216 t := android.ApiLevelOrPanic(ctx, "Tiramisu")
3217 // if we use the attribute, then we need to do this validation
3218 if moduleMinApi.LessThan(t) {
3219 // if minAPi is < T, then we need to have min_device_sdk (which only accepts T+)
3220 if module.properties.Min_device_sdk == nil {
3221 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")
3222 }
3223 }
3224 }
3225}
3226
Paul Duffindd46f712020-02-10 13:37:10 +00003227type sdkLibrarySdkMemberType struct {
3228 android.SdkMemberTypeBase
3229}
3230
Paul Duffin296701e2021-07-14 10:29:36 +01003231func (s *sdkLibrarySdkMemberType) AddDependencies(ctx android.SdkDependencyContext, dependencyTag blueprint.DependencyTag, names []string) {
3232 ctx.AddVariationDependencies(nil, dependencyTag, names...)
Paul Duffindd46f712020-02-10 13:37:10 +00003233}
3234
3235func (s *sdkLibrarySdkMemberType) IsInstance(module android.Module) bool {
3236 _, ok := module.(*SdkLibrary)
3237 return ok
3238}
3239
3240func (s *sdkLibrarySdkMemberType) AddPrebuiltModule(ctx android.SdkMemberContext, member android.SdkMember) android.BpModule {
3241 return ctx.SnapshotBuilder().AddPrebuiltModule(member, "java_sdk_library_import")
3242}
3243
3244func (s *sdkLibrarySdkMemberType) CreateVariantPropertiesStruct() android.SdkMemberProperties {
3245 return &sdkLibrarySdkMemberProperties{}
3246}
3247
Paul Duffin976b0e52021-04-27 23:20:26 +01003248var javaSdkLibrarySdkMemberType = &sdkLibrarySdkMemberType{
3249 android.SdkMemberTypeBase{
3250 PropertyName: "java_sdk_libs",
3251 SupportsSdk: true,
3252 },
3253}
3254
Paul Duffindd46f712020-02-10 13:37:10 +00003255type sdkLibrarySdkMemberProperties struct {
3256 android.SdkMemberPropertiesBase
3257
Paul Duffine8409952022-09-22 16:24:46 +01003258 // Stem name for files in the sdk snapshot.
3259 //
3260 // This is used to construct the path names of various sdk library files in the sdk snapshot to
3261 // make sure that they match the finalized versions of those files in prebuilts/sdk.
3262 //
3263 // This property is marked as keep so that it will be kept in all instances of this struct, will
3264 // not be cleared but will be copied to common structs. That is needed because this field is used
3265 // to construct many file names for other parts of this struct and so it needs to be present in
3266 // all structs. If it was not marked as keep then it would be cleared in some structs and so would
3267 // be unavailable for generating file names if there were other properties that were still set.
3268 Stem string `sdk:"keep"`
3269
Paul Duffindd46f712020-02-10 13:37:10 +00003270 // Scope to per scope properties.
Paul Duffin106a3a42022-01-27 16:39:06 +00003271 Scopes map[*apiScope]*scopeProperties
Paul Duffindd46f712020-02-10 13:37:10 +00003272
Paul Duffin3d1248c2020-04-09 00:10:17 +01003273 // The Java stubs source files.
3274 Stub_srcs []string
Paul Duffinf7a64332020-05-13 16:54:55 +01003275
3276 // The naming scheme.
3277 Naming_scheme *string
Paul Duffind7eb1c22020-05-26 20:57:10 +01003278
3279 // True if the java_sdk_library_import is for a shared library, false
3280 // otherwise.
3281 Shared_library *bool
Paul Duffina2ae7e02020-09-11 11:55:00 +01003282
Paul Duffin1267d872021-04-16 17:21:36 +01003283 // True if the stub imports should produce dex jars.
3284 Compile_dex *bool
3285
Paul Duffina2ae7e02020-09-11 11:55:00 +01003286 // The paths to the doctag files to add to the prebuilt.
3287 Doctag_paths android.Paths
Paul Duffin869de142021-07-15 14:14:41 +01003288
3289 Permitted_packages []string
Pedro Loureiro9956e5e2021-09-07 17:21:59 +00003290
3291 // Signals that this shared library is part of the bootclasspath starting
3292 // on the version indicated in this attribute.
3293 //
3294 // This will make platforms at this level and above to ignore
3295 // <uses-library> tags with this library name because the library is already
3296 // available
3297 On_bootclasspath_since *string
3298
3299 // Signals that this shared library was part of the bootclasspath before
3300 // (but not including) the version indicated in this attribute.
3301 //
3302 // The system will automatically add a <uses-library> tag with this library to
3303 // apps that target any SDK less than the version indicated in this attribute.
3304 On_bootclasspath_before *string
3305
3306 // Indicates that PackageManager should ignore this shared library if the
3307 // platform is below the version indicated in this attribute.
3308 //
3309 // This means that the device won't recognise this library as installed.
3310 Min_device_sdk *string
3311
3312 // Indicates that PackageManager should ignore this shared library if the
3313 // platform is above the version indicated in this attribute.
3314 //
3315 // This means that the device won't recognise this library as installed.
3316 Max_device_sdk *string
Jiakai Zhang9c4dc192023-02-09 00:09:24 +08003317
3318 DexPreoptProfileGuided *bool `supported_build_releases:"UpsideDownCake+"`
Paul Duffindd46f712020-02-10 13:37:10 +00003319}
3320
3321type scopeProperties struct {
Paul Duffin1fd005d2020-04-09 01:08:11 +01003322 Jars android.Paths
3323 StubsSrcJar android.Path
3324 CurrentApiFile android.Path
3325 RemovedApiFile android.Path
Paul Duffine7babdb2022-02-10 13:06:54 +00003326 AnnotationsZip android.Path `supported_build_releases:"Tiramisu+"`
Paul Duffin1fd005d2020-04-09 01:08:11 +01003327 SdkVersion string
Paul Duffindd46f712020-02-10 13:37:10 +00003328}
3329
3330func (s *sdkLibrarySdkMemberProperties) PopulateFromVariant(ctx android.SdkMemberContext, variant android.Module) {
3331 sdk := variant.(*SdkLibrary)
3332
Paul Duffine8409952022-09-22 16:24:46 +01003333 // Copy the stem name for files in the sdk snapshot.
3334 s.Stem = sdk.distStem()
3335
Paul Duffin106a3a42022-01-27 16:39:06 +00003336 s.Scopes = make(map[*apiScope]*scopeProperties)
Paul Duffindd46f712020-02-10 13:37:10 +00003337 for _, apiScope := range allApiScopes {
Paul Duffin803a9562020-05-20 11:52:25 +01003338 paths := sdk.findScopePaths(apiScope)
3339 if paths == nil {
3340 continue
3341 }
3342
Paul Duffindd46f712020-02-10 13:37:10 +00003343 jars := paths.stubsImplPath
3344 if len(jars) > 0 {
3345 properties := scopeProperties{}
3346 properties.Jars = jars
Paul Duffin780c5f42020-05-12 15:52:55 +01003347 properties.SdkVersion = sdk.sdkVersionForStubsLibrary(ctx.SdkModuleContext(), apiScope)
Paul Duffin0f8faff2020-05-20 16:18:00 +01003348 properties.StubsSrcJar = paths.stubsSrcJar.Path()
Paul Duffin10269f12020-06-19 18:39:55 +01003349 if paths.currentApiFilePath.Valid() {
3350 properties.CurrentApiFile = paths.currentApiFilePath.Path()
3351 }
3352 if paths.removedApiFilePath.Valid() {
3353 properties.RemovedApiFile = paths.removedApiFilePath.Path()
3354 }
Anton Hanssond78eb762021-09-21 15:25:12 +01003355 // The annotations zip is only available for modules that set annotations_enabled: true.
3356 if paths.annotationsZip.Valid() {
3357 properties.AnnotationsZip = paths.annotationsZip.Path()
3358 }
Paul Duffin106a3a42022-01-27 16:39:06 +00003359 s.Scopes[apiScope] = &properties
Paul Duffindd46f712020-02-10 13:37:10 +00003360 }
3361 }
3362
Paul Duffindfa131e2020-05-15 20:37:11 +01003363 s.Naming_scheme = sdk.commonSdkLibraryProperties.Naming_scheme
Paul Duffind7eb1c22020-05-26 20:57:10 +01003364 s.Shared_library = proptools.BoolPtr(sdk.sharedLibrary())
Paul Duffin1267d872021-04-16 17:21:36 +01003365 s.Compile_dex = sdk.dexProperties.Compile_dex
Paul Duffina2ae7e02020-09-11 11:55:00 +01003366 s.Doctag_paths = sdk.doctagPaths
Paul Duffin869de142021-07-15 14:14:41 +01003367 s.Permitted_packages = sdk.PermittedPackagesForUpdatableBootJars()
Pedro Loureiro9956e5e2021-09-07 17:21:59 +00003368 s.On_bootclasspath_since = sdk.commonSdkLibraryProperties.On_bootclasspath_since
3369 s.On_bootclasspath_before = sdk.commonSdkLibraryProperties.On_bootclasspath_before
3370 s.Min_device_sdk = sdk.commonSdkLibraryProperties.Min_device_sdk
3371 s.Max_device_sdk = sdk.commonSdkLibraryProperties.Max_device_sdk
Jiakai Zhang9c4dc192023-02-09 00:09:24 +08003372
3373 if sdk.dexpreopter.dexpreoptProperties.Dex_preopt_result.Profile_guided {
3374 s.DexPreoptProfileGuided = proptools.BoolPtr(true)
3375 }
Paul Duffindd46f712020-02-10 13:37:10 +00003376}
3377
3378func (s *sdkLibrarySdkMemberProperties) AddToPropertySet(ctx android.SdkMemberContext, propertySet android.BpPropertySet) {
Paul Duffinf7a64332020-05-13 16:54:55 +01003379 if s.Naming_scheme != nil {
3380 propertySet.AddProperty("naming_scheme", proptools.String(s.Naming_scheme))
3381 }
Paul Duffind7eb1c22020-05-26 20:57:10 +01003382 if s.Shared_library != nil {
3383 propertySet.AddProperty("shared_library", *s.Shared_library)
3384 }
Paul Duffin1267d872021-04-16 17:21:36 +01003385 if s.Compile_dex != nil {
3386 propertySet.AddProperty("compile_dex", *s.Compile_dex)
3387 }
Paul Duffin869de142021-07-15 14:14:41 +01003388 if len(s.Permitted_packages) > 0 {
3389 propertySet.AddProperty("permitted_packages", s.Permitted_packages)
3390 }
Jiakai Zhang9c4dc192023-02-09 00:09:24 +08003391 dexPreoptSet := propertySet.AddPropertySet("dex_preopt")
3392 if s.DexPreoptProfileGuided != nil {
3393 dexPreoptSet.AddProperty("profile_guided", proptools.Bool(s.DexPreoptProfileGuided))
3394 }
Paul Duffinf7a64332020-05-13 16:54:55 +01003395
Paul Duffine8409952022-09-22 16:24:46 +01003396 stem := s.Stem
3397
Paul Duffindd46f712020-02-10 13:37:10 +00003398 for _, apiScope := range allApiScopes {
3399 if properties, ok := s.Scopes[apiScope]; ok {
Paul Duffin6b836ba2020-05-13 19:19:49 +01003400 scopeSet := propertySet.AddPropertySet(apiScope.propertyName)
Paul Duffindd46f712020-02-10 13:37:10 +00003401
Paul Duffin958806b2022-05-16 13:10:47 +00003402 scopeDir := apiScope.snapshotRelativeDir()
Paul Duffin3d1248c2020-04-09 00:10:17 +01003403
Paul Duffindd46f712020-02-10 13:37:10 +00003404 var jars []string
3405 for _, p := range properties.Jars {
Paul Duffine8409952022-09-22 16:24:46 +01003406 dest := filepath.Join(scopeDir, stem+"-stubs.jar")
Paul Duffindd46f712020-02-10 13:37:10 +00003407 ctx.SnapshotBuilder().CopyToSnapshot(p, dest)
3408 jars = append(jars, dest)
3409 }
3410 scopeSet.AddProperty("jars", jars)
3411
Paul Duffin22628d52021-05-12 23:13:22 +01003412 if ctx.SdkModuleContext().Config().IsEnvTrue("SOONG_SDK_SNAPSHOT_USE_SRCJAR") {
3413 // Copy the stubs source jar into the snapshot zip as is.
Paul Duffine8409952022-09-22 16:24:46 +01003414 srcJarSnapshotPath := filepath.Join(scopeDir, stem+".srcjar")
Paul Duffin22628d52021-05-12 23:13:22 +01003415 ctx.SnapshotBuilder().CopyToSnapshot(properties.StubsSrcJar, srcJarSnapshotPath)
3416 scopeSet.AddProperty("stub_srcs", []string{srcJarSnapshotPath})
3417 } else {
3418 // Merge the stubs source jar into the snapshot zip so that when it is unpacked
3419 // the source files are also unpacked.
Paul Duffine8409952022-09-22 16:24:46 +01003420 snapshotRelativeDir := filepath.Join(scopeDir, stem+"_stub_sources")
Paul Duffin22628d52021-05-12 23:13:22 +01003421 ctx.SnapshotBuilder().UnzipToSnapshot(properties.StubsSrcJar, snapshotRelativeDir)
3422 scopeSet.AddProperty("stub_srcs", []string{snapshotRelativeDir})
3423 }
Paul Duffin3d1248c2020-04-09 00:10:17 +01003424
Paul Duffin1fd005d2020-04-09 01:08:11 +01003425 if properties.CurrentApiFile != nil {
Paul Duffine8409952022-09-22 16:24:46 +01003426 currentApiSnapshotPath := apiScope.snapshotRelativeCurrentApiTxtPath(stem)
Paul Duffin1fd005d2020-04-09 01:08:11 +01003427 ctx.SnapshotBuilder().CopyToSnapshot(properties.CurrentApiFile, currentApiSnapshotPath)
3428 scopeSet.AddProperty("current_api", currentApiSnapshotPath)
3429 }
3430
3431 if properties.RemovedApiFile != nil {
Paul Duffine8409952022-09-22 16:24:46 +01003432 removedApiSnapshotPath := apiScope.snapshotRelativeRemovedApiTxtPath(stem)
Paul Duffin3dbf9fd2020-06-02 13:00:02 +01003433 ctx.SnapshotBuilder().CopyToSnapshot(properties.RemovedApiFile, removedApiSnapshotPath)
Paul Duffin1fd005d2020-04-09 01:08:11 +01003434 scopeSet.AddProperty("removed_api", removedApiSnapshotPath)
3435 }
3436
Anton Hanssond78eb762021-09-21 15:25:12 +01003437 if properties.AnnotationsZip != nil {
Paul Duffine8409952022-09-22 16:24:46 +01003438 annotationsSnapshotPath := filepath.Join(scopeDir, stem+"_annotations.zip")
Anton Hanssond78eb762021-09-21 15:25:12 +01003439 ctx.SnapshotBuilder().CopyToSnapshot(properties.AnnotationsZip, annotationsSnapshotPath)
3440 scopeSet.AddProperty("annotations", annotationsSnapshotPath)
3441 }
3442
Paul Duffindd46f712020-02-10 13:37:10 +00003443 if properties.SdkVersion != "" {
3444 scopeSet.AddProperty("sdk_version", properties.SdkVersion)
3445 }
3446 }
3447 }
3448
Paul Duffina2ae7e02020-09-11 11:55:00 +01003449 if len(s.Doctag_paths) > 0 {
3450 dests := []string{}
3451 for _, p := range s.Doctag_paths {
3452 dest := filepath.Join("doctags", p.Rel())
3453 ctx.SnapshotBuilder().CopyToSnapshot(p, dest)
3454 dests = append(dests, dest)
3455 }
3456 propertySet.AddProperty("doctag_files", dests)
3457 }
Paul Duffindd46f712020-02-10 13:37:10 +00003458}