blob: e9fa83ae707c54aff9ef0194b2cac73d81dc4e99 [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 (
Jihoon Kangee113282024-01-23 00:16:41 +000018 "errors"
Jiyong Parkc678ad32018-04-10 13:07:10 +090019 "fmt"
20 "path"
Sundong Ahn054b19a2018-10-19 13:46:09 +090021 "path/filepath"
Paul Duffin46a26a82020-04-07 19:27:04 +010022 "reflect"
Paul Duffin46dc45a2020-05-14 15:39:10 +010023 "regexp"
Jiyong Park82484c02018-04-23 21:41:26 +090024 "sort"
Jiyong Parkc678ad32018-04-10 13:07:10 +090025 "strings"
Jiyong Park82484c02018-04-23 21:41:26 +090026 "sync"
Jiyong Parkc678ad32018-04-10 13:07:10 +090027
Paul Duffind1b3a922020-01-22 11:57:20 +000028 "github.com/google/blueprint"
Jiyong Parkc678ad32018-04-10 13:07:10 +090029 "github.com/google/blueprint/proptools"
Paul Duffin46a26a82020-04-07 19:27:04 +010030
31 "android/soong/android"
Ulya Trafimovichdbf31662020-12-17 12:07:54 +000032 "android/soong/dexpreopt"
ThiƩbaud Weksteen00e8b312024-03-18 14:06:00 +110033 "android/soong/etc"
Jiyong Parkc678ad32018-04-10 13:07:10 +090034)
35
Jooyung Han58f26ab2019-12-18 15:34:32 +090036const (
Pedro Loureiro9956e5e2021-09-07 17:21:59 +000037 sdkXmlFileSuffix = ".xml"
Jiyong Parkc678ad32018-04-10 13:07:10 +090038)
39
Paul Duffind1b3a922020-01-22 11:57:20 +000040// A tag to associated a dependency with a specific api scope.
41type scopeDependencyTag struct {
42 blueprint.BaseDependencyTag
43 name string
44 apiScope *apiScope
Paul Duffinc8782502020-04-29 20:45:27 +010045
46 // Function for extracting appropriate path information from the dependency.
Colin Crossdcf71b22021-02-01 13:59:03 -080047 depInfoExtractor func(paths *scopePaths, ctx android.ModuleContext, dep android.Module) error
Paul Duffinc8782502020-04-29 20:45:27 +010048}
49
50// Extract tag specific information from the dependency.
51func (tag scopeDependencyTag) extractDepInfo(ctx android.ModuleContext, dep android.Module, paths *scopePaths) {
Colin Crossdcf71b22021-02-01 13:59:03 -080052 err := tag.depInfoExtractor(paths, ctx, dep)
Paul Duffinc8782502020-04-29 20:45:27 +010053 if err != nil {
54 ctx.ModuleErrorf("has an invalid {scopeDependencyTag: %s} dependency on module %s: %s", tag.name, ctx.OtherModuleName(dep), err.Error())
55 }
Paul Duffind1b3a922020-01-22 11:57:20 +000056}
57
Paul Duffin80342d72020-06-26 22:08:43 +010058var _ android.ReplaceSourceWithPrebuilt = (*scopeDependencyTag)(nil)
59
60func (tag scopeDependencyTag) ReplaceSourceWithPrebuilt() bool {
61 return false
62}
63
Paul Duffind1b3a922020-01-22 11:57:20 +000064// Provides information about an api scope, e.g. public, system, test.
65type apiScope struct {
66 // The name of the api scope, e.g. public, system, test
67 name string
68
Paul Duffin97b53b82020-05-05 14:40:52 +010069 // The api scope that this scope extends.
Paul Duffind0b9fca2022-09-30 18:11:41 +010070 //
71 // This organizes the scopes into an extension hierarchy.
72 //
73 // If set this means that the API provided by this scope includes the API provided by the scope
74 // set in this field.
Paul Duffin97b53b82020-05-05 14:40:52 +010075 extends *apiScope
76
Paul Duffind0b9fca2022-09-30 18:11:41 +010077 // The next api scope that a library that uses this scope can access.
78 //
79 // This organizes the scopes into an access hierarchy.
80 //
81 // If set this means that a library that can access this API can also access the API provided by
82 // the scope set in this field.
83 //
84 // A module that sets sdk_version: "<scope>_current" should have access to the <scope> API of
85 // every java_sdk_library that it depends on. If the library does not provide an API for <scope>
86 // then it will traverse up this access hierarchy to find an API that it does provide.
87 //
88 // If this is not set then it defaults to the scope set in extends.
89 canAccess *apiScope
90
Paul Duffin3375e352020-04-28 10:44:03 +010091 // The legacy enabled status for a specific scope can be dependent on other
92 // properties that have been specified on the library so it is provided by
93 // a function that can determine the status by examining those properties.
94 legacyEnabledStatus func(module *SdkLibrary) bool
95
96 // The default enabled status for non-legacy behavior, which is triggered by
97 // explicitly enabling at least one api scope.
98 defaultEnabledStatus bool
99
100 // Gets a pointer to the scope specific properties.
101 scopeSpecificProperties func(module *SdkLibrary) *ApiScopeProperties
102
Paul Duffin46a26a82020-04-07 19:27:04 +0100103 // The name of the field in the dynamically created structure.
104 fieldName string
105
Paul Duffin6b836ba2020-05-13 19:19:49 +0100106 // The name of the property in the java_sdk_library_import
107 propertyName string
108
Jihoon Kangb7431552024-01-22 19:40:08 +0000109 // The tag to use to depend on the prebuilt stubs library module
110 prebuiltStubsTag scopeDependencyTag
Paul Duffind1b3a922020-01-22 11:57:20 +0000111
Jihoon Kangbd093452023-12-26 19:08:01 +0000112 // The tag to use to depend on the everything stubs library module.
113 everythingStubsTag scopeDependencyTag
114
115 // The tag to use to depend on the exportable stubs library module.
116 exportableStubsTag scopeDependencyTag
117
Paul Duffin0ff08bd2020-04-29 13:30:54 +0100118 // The tag to use to depend on the stubs source module (if separate from the API module).
119 stubsSourceTag scopeDependencyTag
120
121 // The tag to use to depend on the API file generating module (if separate from the stubs source module).
122 apiFileTag scopeDependencyTag
123
Paul Duffinc8782502020-04-29 20:45:27 +0100124 // The tag to use to depend on the stubs source and API module.
125 stubsSourceAndApiTag scopeDependencyTag
Paul Duffind1b3a922020-01-22 11:57:20 +0000126
Paul Duffin958806b2022-05-16 13:10:47 +0000127 // The tag to use to depend on the module that provides the latest version of the API .txt file.
128 latestApiModuleTag scopeDependencyTag
129
130 // The tag to use to depend on the module that provides the latest version of the API removed.txt
131 // file.
132 latestRemovedApiModuleTag scopeDependencyTag
133
Paul Duffind1b3a922020-01-22 11:57:20 +0000134 // The scope specific prefix to add to the api file base of "current.txt" or "removed.txt".
135 apiFilePrefix string
136
Paul Duffind0b9fca2022-09-30 18:11:41 +0100137 // The scope specific suffix to add to the sdk library module name to construct a scope specific
Paul Duffind1b3a922020-01-22 11:57:20 +0000138 // module name.
139 moduleSuffix string
140
Paul Duffind1b3a922020-01-22 11:57:20 +0000141 // SDK version that the stubs library is built against. Note that this is always
142 // *current. Older stubs library built with a numbered SDK version is created from
143 // the prebuilt jar.
144 sdkVersion string
Paul Duffin1fb487d2020-04-07 18:50:10 +0100145
Paul Duffin15f34ef2020-07-20 18:04:44 +0100146 // The annotation that identifies this API level, empty for the public API scope.
147 annotation string
148
Paul Duffin1fb487d2020-04-07 18:50:10 +0100149 // Extra arguments to pass to droidstubs for this scope.
Paul Duffin0ff08bd2020-04-29 13:30:54 +0100150 //
Paul Duffin15f34ef2020-07-20 18:04:44 +0100151 // This is not used directly but is used to construct the droidstubsArgs.
152 extraArgs []string
Paul Duffin0ff08bd2020-04-29 13:30:54 +0100153
Paul Duffin15f34ef2020-07-20 18:04:44 +0100154 // The args that must be passed to droidstubs to generate the API and stubs source
155 // for this scope, constructed dynamically by initApiScope().
Paul Duffin0ff08bd2020-04-29 13:30:54 +0100156 //
157 // The API only includes the additional members that this scope adds over the scope
158 // that it extends.
Paul Duffin15f34ef2020-07-20 18:04:44 +0100159 //
160 // The stubs source must include the definitions of everything that is in this
161 // api scope and all the scopes that this one extends.
162 droidstubsArgs []string
Paul Duffin0ff08bd2020-04-29 13:30:54 +0100163
Anton Hansson6478ac12020-05-02 11:19:36 +0100164 // Whether the api scope can be treated as unstable, and should skip compat checks.
165 unstable bool
Jihoon Kang1c92c3e2023-03-23 17:44:51 +0000166
167 // Represents the SDK kind of this scope.
168 kind android.SdkKind
Paul Duffind1b3a922020-01-22 11:57:20 +0000169}
170
171// Initialize a scope, creating and adding appropriate dependency tags
172func initApiScope(scope *apiScope) *apiScope {
Paul Duffinc8782502020-04-29 20:45:27 +0100173 name := scope.name
Paul Duffin46dc45a2020-05-14 15:39:10 +0100174 scopeByName[name] = scope
175 allScopeNames = append(allScopeNames, name)
Paul Duffin6b836ba2020-05-13 19:19:49 +0100176 scope.propertyName = strings.ReplaceAll(name, "-", "_")
177 scope.fieldName = proptools.FieldNameForProperty(scope.propertyName)
Jihoon Kangb7431552024-01-22 19:40:08 +0000178 scope.prebuiltStubsTag = scopeDependencyTag{
Paul Duffinc8782502020-04-29 20:45:27 +0100179 name: name + "-stubs",
180 apiScope: scope,
181 depInfoExtractor: (*scopePaths).extractStubsLibraryInfoFromDependency,
Paul Duffind1b3a922020-01-22 11:57:20 +0000182 }
Jihoon Kangbd093452023-12-26 19:08:01 +0000183 scope.everythingStubsTag = scopeDependencyTag{
184 name: name + "-stubs-everything",
185 apiScope: scope,
186 depInfoExtractor: (*scopePaths).extractEverythingStubsLibraryInfoFromDependency,
187 }
188 scope.exportableStubsTag = scopeDependencyTag{
189 name: name + "-stubs-exportable",
190 apiScope: scope,
191 depInfoExtractor: (*scopePaths).extractExportableStubsLibraryInfoFromDependency,
192 }
Paul Duffin0ff08bd2020-04-29 13:30:54 +0100193 scope.stubsSourceTag = scopeDependencyTag{
194 name: name + "-stubs-source",
195 apiScope: scope,
196 depInfoExtractor: (*scopePaths).extractStubsSourceInfoFromDep,
197 }
198 scope.apiFileTag = scopeDependencyTag{
199 name: name + "-api",
200 apiScope: scope,
201 depInfoExtractor: (*scopePaths).extractApiInfoFromDep,
202 }
Paul Duffinc8782502020-04-29 20:45:27 +0100203 scope.stubsSourceAndApiTag = scopeDependencyTag{
204 name: name + "-stubs-source-and-api",
205 apiScope: scope,
206 depInfoExtractor: (*scopePaths).extractStubsSourceAndApiInfoFromApiStubsProvider,
Paul Duffind1b3a922020-01-22 11:57:20 +0000207 }
Paul Duffin958806b2022-05-16 13:10:47 +0000208 scope.latestApiModuleTag = scopeDependencyTag{
209 name: name + "-latest-api",
210 apiScope: scope,
211 depInfoExtractor: (*scopePaths).extractLatestApiPath,
212 }
213 scope.latestRemovedApiModuleTag = scopeDependencyTag{
214 name: name + "-latest-removed-api",
215 apiScope: scope,
216 depInfoExtractor: (*scopePaths).extractLatestRemovedApiPath,
217 }
Paul Duffin0ff08bd2020-04-29 13:30:54 +0100218
219 // To get the args needed to generate the stubs source append all the args from
220 // this scope and all the scopes it extends as each set of args adds additional
221 // members to the stubs.
Paul Duffin15f34ef2020-07-20 18:04:44 +0100222 var scopeSpecificArgs []string
223 if scope.annotation != "" {
224 scopeSpecificArgs = []string{"--show-annotation", scope.annotation}
Paul Duffin0ff08bd2020-04-29 13:30:54 +0100225 }
Paul Duffin15f34ef2020-07-20 18:04:44 +0100226 for s := scope; s != nil; s = s.extends {
227 scopeSpecificArgs = append(scopeSpecificArgs, s.extraArgs...)
Paul Duffin0ff08bd2020-04-29 13:30:54 +0100228
Paul Duffin15f34ef2020-07-20 18:04:44 +0100229 // Ensure that the generated stubs includes all the API elements from the API scope
230 // that this scope extends.
231 if s != scope && s.annotation != "" {
232 scopeSpecificArgs = append(scopeSpecificArgs, "--show-for-stub-purposes-annotation", s.annotation)
233 }
234 }
Paul Duffin0ff08bd2020-04-29 13:30:54 +0100235
Paul Duffind0b9fca2022-09-30 18:11:41 +0100236 // By default, a library that can access a scope can also access the scope it extends.
237 if scope.canAccess == nil {
238 scope.canAccess = scope.extends
239 }
240
Paul Duffin15f34ef2020-07-20 18:04:44 +0100241 // Escape any special characters in the arguments. This is needed because droidstubs
242 // passes these directly to the shell command.
243 scope.droidstubsArgs = proptools.ShellEscapeList(scopeSpecificArgs)
Paul Duffin0ff08bd2020-04-29 13:30:54 +0100244
Paul Duffind1b3a922020-01-22 11:57:20 +0000245 return scope
246}
247
Anton Hansson08f476b2021-04-07 15:32:19 +0100248func (scope *apiScope) stubsLibraryModuleNameSuffix() string {
249 return ".stubs" + scope.moduleSuffix
250}
251
Jihoon Kangfa4a90d2023-12-20 02:53:38 +0000252func (scope *apiScope) exportableStubsLibraryModuleNameSuffix() string {
253 return ".stubs.exportable" + scope.moduleSuffix
254}
255
Jihoon Kang1c92c3e2023-03-23 17:44:51 +0000256func (scope *apiScope) apiLibraryModuleName(baseName string) string {
257 return scope.stubsLibraryModuleName(baseName) + ".from-text"
258}
259
Jihoon Kang1147b312023-06-08 23:25:57 +0000260func (scope *apiScope) sourceStubLibraryModuleName(baseName string) string {
261 return scope.stubsLibraryModuleName(baseName) + ".from-source"
262}
263
Jihoon Kangfa4a90d2023-12-20 02:53:38 +0000264func (scope *apiScope) exportableSourceStubsLibraryModuleName(baseName string) string {
265 return scope.exportableStubsLibraryModuleName(baseName) + ".from-source"
266}
267
Paul Duffinc3091c82020-05-08 14:16:20 +0100268func (scope *apiScope) stubsLibraryModuleName(baseName string) string {
Anton Hansson08f476b2021-04-07 15:32:19 +0100269 return baseName + scope.stubsLibraryModuleNameSuffix()
Paul Duffind1b3a922020-01-22 11:57:20 +0000270}
271
Jihoon Kangfa4a90d2023-12-20 02:53:38 +0000272func (scope *apiScope) exportableStubsLibraryModuleName(baseName string) string {
273 return baseName + scope.exportableStubsLibraryModuleNameSuffix()
274}
275
Paul Duffinc8782502020-04-29 20:45:27 +0100276func (scope *apiScope) stubsSourceModuleName(baseName string) string {
Paul Duffindd9d0742020-05-08 15:52:37 +0100277 return baseName + ".stubs.source" + scope.moduleSuffix
Paul Duffind1b3a922020-01-22 11:57:20 +0000278}
279
Paul Duffin0ff08bd2020-04-29 13:30:54 +0100280func (scope *apiScope) apiModuleName(baseName string) string {
Paul Duffindd9d0742020-05-08 15:52:37 +0100281 return baseName + ".api" + scope.moduleSuffix
Paul Duffin0ff08bd2020-04-29 13:30:54 +0100282}
283
Paul Duffin3375e352020-04-28 10:44:03 +0100284func (scope *apiScope) String() string {
285 return scope.name
286}
287
Paul Duffin958806b2022-05-16 13:10:47 +0000288// snapshotRelativeDir returns the snapshot directory into which the files related to scopes will
289// be stored.
290func (scope *apiScope) snapshotRelativeDir() string {
291 return filepath.Join("sdk_library", scope.name)
292}
293
294// snapshotRelativeCurrentApiTxtPath returns the snapshot path to the API .txt file for the named
295// library.
296func (scope *apiScope) snapshotRelativeCurrentApiTxtPath(name string) string {
297 return filepath.Join(scope.snapshotRelativeDir(), name+".txt")
298}
299
300// snapshotRelativeRemovedApiTxtPath returns the snapshot path to the removed API .txt file for the
301// named library.
302func (scope *apiScope) snapshotRelativeRemovedApiTxtPath(name string) string {
303 return filepath.Join(scope.snapshotRelativeDir(), name+"-removed.txt")
304}
305
Paul Duffind1b3a922020-01-22 11:57:20 +0000306type apiScopes []*apiScope
307
308func (scopes apiScopes) Strings(accessor func(*apiScope) string) []string {
309 var list []string
310 for _, scope := range scopes {
311 list = append(list, accessor(scope))
312 }
313 return list
314}
315
Jihoon Kanga96a7b12023-09-20 23:43:32 +0000316// Method that maps the apiScopes properties to the index of each apiScopes elements.
317// apiScopes property to be used as the key can be specified with the input accessor.
318// Only a string property of apiScope can be used as the key of the map.
319func (scopes apiScopes) MapToIndex(accessor func(*apiScope) string) map[string]int {
320 ret := make(map[string]int)
321 for i, scope := range scopes {
322 ret[accessor(scope)] = i
323 }
324 return ret
325}
326
Jihoon Kang98aa8fa2024-06-07 11:06:57 +0000327func (scopes apiScopes) ConvertStubsLibraryExportableToEverything(name string) string {
328 for _, scope := range scopes {
329 if strings.HasSuffix(name, scope.exportableStubsLibraryModuleNameSuffix()) {
330 return strings.TrimSuffix(name, scope.exportableStubsLibraryModuleNameSuffix()) +
331 scope.stubsLibraryModuleNameSuffix()
332 }
333 }
334 return name
335}
336
Jiyong Parkc678ad32018-04-10 13:07:10 +0900337var (
Paul Duffin46dc45a2020-05-14 15:39:10 +0100338 scopeByName = make(map[string]*apiScope)
339 allScopeNames []string
Paul Duffind1b3a922020-01-22 11:57:20 +0000340 apiScopePublic = initApiScope(&apiScope{
Paul Duffin3375e352020-04-28 10:44:03 +0100341 name: "public",
342
343 // Public scope is enabled by default for both legacy and non-legacy modes.
344 legacyEnabledStatus: func(module *SdkLibrary) bool {
345 return true
346 },
347 defaultEnabledStatus: true,
348
349 scopeSpecificProperties: func(module *SdkLibrary) *ApiScopeProperties {
350 return &module.sdkLibraryProperties.Public
351 },
Paul Duffind1b3a922020-01-22 11:57:20 +0000352 sdkVersion: "current",
Jihoon Kang1c92c3e2023-03-23 17:44:51 +0000353 kind: android.SdkPublic,
Paul Duffind1b3a922020-01-22 11:57:20 +0000354 })
355 apiScopeSystem = initApiScope(&apiScope{
Paul Duffin3375e352020-04-28 10:44:03 +0100356 name: "system",
357 extends: apiScopePublic,
358 legacyEnabledStatus: (*SdkLibrary).generateTestAndSystemScopesByDefault,
359 scopeSpecificProperties: func(module *SdkLibrary) *ApiScopeProperties {
360 return &module.sdkLibraryProperties.System
361 },
Paul Duffin15f34ef2020-07-20 18:04:44 +0100362 apiFilePrefix: "system-",
363 moduleSuffix: ".system",
364 sdkVersion: "system_current",
365 annotation: "android.annotation.SystemApi(client=android.annotation.SystemApi.Client.PRIVILEGED_APPS)",
Jihoon Kang1c92c3e2023-03-23 17:44:51 +0000366 kind: android.SdkSystem,
Paul Duffind1b3a922020-01-22 11:57:20 +0000367 })
368 apiScopeTest = initApiScope(&apiScope{
Paul Duffin3375e352020-04-28 10:44:03 +0100369 name: "test",
Anton Hansson4fe970f2020-10-09 10:16:49 +0100370 extends: apiScopeSystem,
Paul Duffin3375e352020-04-28 10:44:03 +0100371 legacyEnabledStatus: (*SdkLibrary).generateTestAndSystemScopesByDefault,
372 scopeSpecificProperties: func(module *SdkLibrary) *ApiScopeProperties {
373 return &module.sdkLibraryProperties.Test
374 },
Paul Duffin15f34ef2020-07-20 18:04:44 +0100375 apiFilePrefix: "test-",
376 moduleSuffix: ".test",
377 sdkVersion: "test_current",
378 annotation: "android.annotation.TestApi",
379 unstable: true,
Jihoon Kang1c92c3e2023-03-23 17:44:51 +0000380 kind: android.SdkTest,
Paul Duffind1b3a922020-01-22 11:57:20 +0000381 })
Paul Duffin8f265b92020-04-28 14:13:56 +0100382 apiScopeModuleLib = initApiScope(&apiScope{
Paul Duffin6b836ba2020-05-13 19:19:49 +0100383 name: "module-lib",
Paul Duffin8f265b92020-04-28 14:13:56 +0100384 extends: apiScopeSystem,
Paul Duffin0c5bae52020-06-02 13:00:08 +0100385 // The module-lib scope is disabled by default in legacy mode.
Paul Duffin8f265b92020-04-28 14:13:56 +0100386 //
387 // Enabling this would break existing usages.
388 legacyEnabledStatus: func(module *SdkLibrary) bool {
389 return false
390 },
391 scopeSpecificProperties: func(module *SdkLibrary) *ApiScopeProperties {
392 return &module.sdkLibraryProperties.Module_lib
393 },
394 apiFilePrefix: "module-lib-",
395 moduleSuffix: ".module_lib",
396 sdkVersion: "module_current",
Paul Duffin15f34ef2020-07-20 18:04:44 +0100397 annotation: "android.annotation.SystemApi(client=android.annotation.SystemApi.Client.MODULE_LIBRARIES)",
Jihoon Kang1c92c3e2023-03-23 17:44:51 +0000398 kind: android.SdkModule,
Paul Duffin8f265b92020-04-28 14:13:56 +0100399 })
Paul Duffin0c5bae52020-06-02 13:00:08 +0100400 apiScopeSystemServer = initApiScope(&apiScope{
401 name: "system-server",
402 extends: apiScopePublic,
Paul Duffind0b9fca2022-09-30 18:11:41 +0100403
404 // The system-server scope can access the module-lib scope.
405 //
406 // A module that provides a system-server API is appended to the standard bootclasspath that is
407 // used by the system server. So, it should be able to access module-lib APIs provided by
408 // libraries on the bootclasspath.
409 canAccess: apiScopeModuleLib,
410
Paul Duffin0c5bae52020-06-02 13:00:08 +0100411 // The system-server scope is disabled by default in legacy mode.
412 //
413 // Enabling this would break existing usages.
414 legacyEnabledStatus: func(module *SdkLibrary) bool {
415 return false
416 },
417 scopeSpecificProperties: func(module *SdkLibrary) *ApiScopeProperties {
418 return &module.sdkLibraryProperties.System_server
419 },
420 apiFilePrefix: "system-server-",
421 moduleSuffix: ".system_server",
422 sdkVersion: "system_server_current",
Paul Duffin15f34ef2020-07-20 18:04:44 +0100423 annotation: "android.annotation.SystemApi(client=android.annotation.SystemApi.Client.SYSTEM_SERVER)",
424 extraArgs: []string{
425 "--hide-annotation", "android.annotation.Hide",
Paul Duffin0c5bae52020-06-02 13:00:08 +0100426 // com.android.* classes are okay in this interface"
Paul Duffin15f34ef2020-07-20 18:04:44 +0100427 "--hide", "InternalClasses",
Paul Duffin0c5bae52020-06-02 13:00:08 +0100428 },
Jihoon Kang1c92c3e2023-03-23 17:44:51 +0000429 kind: android.SdkSystemServer,
Paul Duffin0c5bae52020-06-02 13:00:08 +0100430 })
Jihoon Kang98aa8fa2024-06-07 11:06:57 +0000431 AllApiScopes = apiScopes{
Paul Duffind1b3a922020-01-22 11:57:20 +0000432 apiScopePublic,
433 apiScopeSystem,
434 apiScopeTest,
Paul Duffin8f265b92020-04-28 14:13:56 +0100435 apiScopeModuleLib,
Paul Duffin0c5bae52020-06-02 13:00:08 +0100436 apiScopeSystemServer,
Paul Duffind1b3a922020-01-22 11:57:20 +0000437 }
Jihoon Kang0c705a42023-08-02 06:44:57 +0000438 apiLibraryAdditionalProperties = map[string]struct {
439 FullApiSurfaceStubLib string
440 AdditionalApiContribution string
441 }{
442 "legacy.i18n.module.platform.api": {
443 FullApiSurfaceStubLib: "legacy.core.platform.api.stubs",
444 AdditionalApiContribution: "i18n.module.public.api.stubs.source.api.contribution",
445 },
446 "stable.i18n.module.platform.api": {
447 FullApiSurfaceStubLib: "stable.core.platform.api.stubs",
448 AdditionalApiContribution: "i18n.module.public.api.stubs.source.api.contribution",
449 },
450 "conscrypt.module.platform.api": {
451 FullApiSurfaceStubLib: "stable.core.platform.api.stubs",
452 AdditionalApiContribution: "conscrypt.module.public.api.stubs.source.api.contribution",
453 },
454 }
Jiyong Parkc678ad32018-04-10 13:07:10 +0900455)
456
Jiyong Park82484c02018-04-23 21:41:26 +0900457var (
458 javaSdkLibrariesLock sync.Mutex
459)
460
Jiyong Parkc678ad32018-04-10 13:07:10 +0900461// TODO: these are big features that are currently missing
Jiyong Park1be96912018-05-28 18:02:19 +0900462// 1) disallowing linking to the runtime shared lib
463// 2) HTML generation
Jiyong Parkc678ad32018-04-10 13:07:10 +0900464
465func init() {
Paul Duffin43dc1cc2019-12-19 11:18:54 +0000466 RegisterSdkLibraryBuildComponents(android.InitRegistrationContext)
Jiyong Parkc678ad32018-04-10 13:07:10 +0900467
Jiyong Park82484c02018-04-23 21:41:26 +0900468 android.RegisterMakeVarsProvider(pctx, func(ctx android.MakeVarsContext) {
469 javaSdkLibraries := javaSdkLibraries(ctx.Config())
470 sort.Strings(*javaSdkLibraries)
471 ctx.Strict("JAVA_SDK_LIBRARIES", strings.Join(*javaSdkLibraries, " "))
472 })
Paul Duffindd46f712020-02-10 13:37:10 +0000473
474 // Register sdk member types.
Paul Duffin976b0e52021-04-27 23:20:26 +0100475 android.RegisterSdkMemberType(javaSdkLibrarySdkMemberType)
Jiyong Parkc678ad32018-04-10 13:07:10 +0900476}
477
Paul Duffin43dc1cc2019-12-19 11:18:54 +0000478func RegisterSdkLibraryBuildComponents(ctx android.RegistrationContext) {
479 ctx.RegisterModuleType("java_sdk_library", SdkLibraryFactory)
480 ctx.RegisterModuleType("java_sdk_library_import", sdkLibraryImportFactory)
481}
482
Paul Duffin3375e352020-04-28 10:44:03 +0100483// Properties associated with each api scope.
484type ApiScopeProperties struct {
485 // Indicates whether the api surface is generated.
486 //
487 // If this is set for any scope then all scopes must explicitly specify if they
488 // are enabled. This is to prevent new usages from depending on legacy behavior.
489 //
490 // Otherwise, if this is not set for any scope then the default behavior is
491 // scope specific so please refer to the scope specific property documentation.
492 Enabled *bool
Paul Duffin87a05a32020-05-12 11:50:28 +0100493
494 // The sdk_version to use for building the stubs.
495 //
496 // If not specified then it will use an sdk_version determined as follows:
Trevor Radcliffedf8aa1f2021-11-04 14:25:39 +0000497 //
Paul Duffin87a05a32020-05-12 11:50:28 +0100498 // 1) If the sdk_version specified on the java_sdk_library is none then this
Trevor Radcliffedf8aa1f2021-11-04 14:25:39 +0000499 // will be none. This is used for java_sdk_library instances that are used
500 // to create stubs that contribute to the core_current sdk version.
501 // 2) Otherwise, it is assumed that this library extends but does not
502 // contribute directly to a specific sdk_version and so this uses the
503 // sdk_version appropriate for the api scope. e.g. public will use
504 // sdk_version: current, system will use sdk_version: system_current, etc.
Paul Duffin87a05a32020-05-12 11:50:28 +0100505 //
506 // This does not affect the sdk_version used for either generating the stubs source
507 // or the API file. They both have to use the same sdk_version as is used for
508 // compiling the implementation library.
509 Sdk_version *string
Mark White9421c4c2023-08-10 00:07:03 +0000510
511 // Extra libs used when compiling stubs for this scope.
512 Libs []string
Paul Duffin3375e352020-04-28 10:44:03 +0100513}
514
Jiyong Parkc678ad32018-04-10 13:07:10 +0900515type sdkLibraryProperties struct {
Anton Hanssonf8ea3722021-09-16 14:24:13 +0100516 // List of source files that are needed to compile the API, but are not part of runtime library.
517 Api_srcs []string `android:"arch_variant"`
518
Paul Duffin5df79302020-05-16 15:52:12 +0100519 // Visibility for impl library module. If not specified then defaults to the
520 // visibility property.
521 Impl_library_visibility []string
522
Paul Duffin4911a892020-04-29 23:35:13 +0100523 // Visibility for stubs library modules. If not specified then defaults to the
524 // visibility property.
525 Stubs_library_visibility []string
526
527 // Visibility for stubs source modules. If not specified then defaults to the
528 // visibility property.
529 Stubs_source_visibility []string
530
Anton Hansson7f66efa2020-10-08 14:47:23 +0100531 // List of Java libraries that will be in the classpath when building the implementation lib
532 Impl_only_libs []string `android:"arch_variant"`
533
Paul Duffin77590a82022-04-28 14:13:30 +0000534 // List of Java libraries that will included in the implementation lib.
535 Impl_only_static_libs []string `android:"arch_variant"`
536
Sundong Ahnf043cf62018-06-25 16:04:37 +0900537 // List of Java libraries that will be in the classpath when building stubs
538 Stub_only_libs []string `android:"arch_variant"`
539
Anton Hanssondae54cd2021-04-21 16:30:10 +0100540 // List of Java libraries that will included in stub libraries
541 Stub_only_static_libs []string `android:"arch_variant"`
542
Paul Duffin7a586d32019-12-30 17:09:34 +0000543 // list of package names that will be documented and publicized as API.
544 // This allows the API to be restricted to a subset of the source files provided.
545 // If this is unspecified then all the source files will be treated as being part
546 // of the API.
Jiyong Parkc678ad32018-04-10 13:07:10 +0900547 Api_packages []string
548
Jiyong Park5a2c9d72018-05-01 22:25:41 +0900549 // list of package names that must be hidden from the API
550 Hidden_api_packages []string
551
Paul Duffin749f98f2019-12-30 17:23:46 +0000552 // the relative path to the directory containing the api specification files.
553 // Defaults to "api".
554 Api_dir *string
555
Paul Duffindfa131e2020-05-15 20:37:11 +0100556 // Determines whether a runtime implementation library is built; defaults to false.
557 //
558 // 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 +0200559 // it is as if shared_library: false, was set.
Paul Duffin43db9be2019-12-30 17:35:49 +0000560 Api_only *bool
561
Paul Duffin11512472019-02-11 15:55:17 +0000562 // local files that are used within user customized droiddoc options.
563 Droiddoc_option_files []string
564
Spandan Das93e95992021-07-29 18:26:39 +0000565 // additional droiddoc options.
Paul Duffin11512472019-02-11 15:55:17 +0000566 // Available variables for substitution:
567 //
568 // $(location <label>): the path to the droiddoc_option_files with name <label>
Sundong Ahndd567f92018-07-31 17:19:11 +0900569 Droiddoc_options []string
570
Paul Duffine22c2ab2020-05-20 19:35:27 +0100571 // is set to true, Metalava will allow framework SDK to contain annotations.
572 Annotations_enabled *bool
573
Sundong Ahn054b19a2018-10-19 13:46:09 +0900574 // a list of top-level directories containing files to merge qualifier annotations
575 // (i.e. those intended to be included in the stubs written) from.
576 Merge_annotations_dirs []string
577
578 // a list of top-level directories containing Java stub files to merge show/hide annotations from.
579 Merge_inclusion_annotations_dirs []string
580
Paul Duffin4f5c1ef2020-11-19 14:53:43 +0000581 // If set to true then don't create dist rules.
582 No_dist *bool
Sundong Ahn80a87b32019-05-13 15:02:50 +0900583
Paul Duffin31310252020-11-20 21:26:20 +0000584 // The stem for the artifacts that are copied to the dist, if not specified
585 // then defaults to the base module name.
586 //
587 // For each scope the following artifacts are copied to the apistubs/<scope>
588 // directory in the dist.
589 // * stubs impl jar -> <dist-stem>.jar
590 // * API specification file -> api/<dist-stem>.txt
591 // * Removed API specification file -> api/<dist-stem>-removed.txt
592 //
593 // Also used to construct the name of the filegroup (created by prebuilt_apis)
594 // that references the latest released API and remove API specification files.
595 // * API specification filegroup -> <dist-stem>.api.<scope>.latest
596 // * Removed API specification filegroup -> <dist-stem>-removed.api.<scope>.latest
Jaewoong Jung1a97ee02021-03-09 13:25:02 -0800597 // * API incompatibilities baseline filegroup -> <dist-stem>-incompatibilities.api.<scope>.latest
Paul Duffin31310252020-11-20 21:26:20 +0000598 Dist_stem *string
599
Colin Cross986b69a2021-06-01 13:13:40 -0700600 // The subdirectory for the artifacts that are copied to the dist directory. If not specified
Colin Cross3dd66252021-06-01 14:05:09 -0700601 // then defaults to "unknown". Should be set to "android" for anything that should be published
Colin Cross986b69a2021-06-01 13:13:40 -0700602 // in the public Android SDK.
603 Dist_group *string
604
Anton Hanssondff2c782020-12-21 17:10:01 +0000605 // A compatibility mode that allows historical API-tracking files to not exist.
606 // Do not use.
607 Unsafe_ignore_missing_latest_api bool
608
Paul Duffin3375e352020-04-28 10:44:03 +0100609 // indicates whether system and test apis should be generated.
610 Generate_system_and_test_apis bool `blueprint:"mutated"`
611
612 // The properties specific to the public api scope
613 //
614 // Unless explicitly specified by using public.enabled the public api scope is
615 // enabled by default in both legacy and non-legacy mode.
616 Public ApiScopeProperties
617
618 // The properties specific to the system api scope
619 //
620 // In legacy mode the system api scope is enabled by default when sdk_version
621 // is set to something other than "none".
622 //
623 // In non-legacy mode the system api scope is disabled by default.
624 System ApiScopeProperties
625
626 // The properties specific to the test api scope
627 //
628 // In legacy mode the test api scope is enabled by default when sdk_version
629 // is set to something other than "none".
630 //
631 // In non-legacy mode the test api scope is disabled by default.
632 Test ApiScopeProperties
Paul Duffin37e0b772019-12-30 17:20:10 +0000633
Paul Duffin0c5bae52020-06-02 13:00:08 +0100634 // The properties specific to the module-lib api scope
Paul Duffin8f265b92020-04-28 14:13:56 +0100635 //
Zi Wangb2179e32023-01-31 15:53:30 -0800636 // Unless explicitly specified by using module_lib.enabled the module_lib api
637 // scope is disabled by default.
Paul Duffin8f265b92020-04-28 14:13:56 +0100638 Module_lib ApiScopeProperties
639
Paul Duffin0c5bae52020-06-02 13:00:08 +0100640 // The properties specific to the system-server api scope
641 //
Zi Wangb2179e32023-01-31 15:53:30 -0800642 // Unless explicitly specified by using system_server.enabled the
643 // system_server api scope is disabled by default.
Paul Duffin0c5bae52020-06-02 13:00:08 +0100644 System_server ApiScopeProperties
645
Jiyong Park932cdfe2020-05-28 00:19:53 +0900646 // Determines if the stubs are preferred over the implementation library
647 // for linking, even when the client doesn't specify sdk_version. When this
648 // is set to true, such clients are provided with the widest API surface that
649 // this lib provides. Note however that this option doesn't affect the clients
650 // that are in the same APEX as this library. In that case, the clients are
651 // always linked with the implementation library. Default is false.
652 Default_to_stubs *bool
653
Paul Duffin160fe412020-05-10 19:32:20 +0100654 // Properties related to api linting.
655 Api_lint struct {
656 // Enable api linting.
657 Enabled *bool
Anton Hanssonfd1c0d22023-11-02 15:18:09 +0000658
659 // If API lint is enabled, this flag controls whether a set of legitimate lint errors
660 // are turned off. The default is true.
661 Legacy_errors_allowed *bool
Paul Duffin160fe412020-05-10 19:32:20 +0100662 }
663
Jihoon Kang80456fd2023-11-15 19:22:14 +0000664 // Determines if the module contributes to any api surfaces.
665 // This property should be set to true only if the module is listed under
666 // frameworks-base-api.bootclasspath in frameworks/base/api/Android.bp.
667 // Otherwise, this property should be set to false.
668 // Defaults to false.
669 Contribute_to_android_api *bool
670
Jihoon Kang6592e872023-12-19 01:13:16 +0000671 // a list of aconfig_declarations module names that the stubs generated in this module
672 // depend on.
673 Aconfig_declarations []string
674
Jiyong Parkc678ad32018-04-10 13:07:10 +0900675 // TODO: determines whether to create HTML doc or not
Paul Duffine8409952022-09-22 16:24:46 +0100676 // Html_doc *bool
Jiyong Parkc678ad32018-04-10 13:07:10 +0900677}
678
Paul Duffin0f8faff2020-05-20 16:18:00 +0100679// Paths to outputs from java_sdk_library and java_sdk_library_import.
680//
681// Fields that are android.Paths are always set (during GenerateAndroidBuildActions).
682// OptionalPaths are always set by java_sdk_library but may not be set by
683// java_sdk_library_import as not all instances provide that information.
Paul Duffind1b3a922020-01-22 11:57:20 +0000684type scopePaths struct {
Paul Duffin0f8faff2020-05-20 16:18:00 +0100685 // The path (represented as Paths for convenience when returning) to the stubs header jar.
686 //
687 // That is the jar that is created by turbine.
688 stubsHeaderPath android.Paths
689
690 // The path (represented as Paths for convenience when returning) to the stubs implementation jar.
691 //
692 // This is not the implementation jar, it still only contains stubs.
693 stubsImplPath android.Paths
694
Paul Duffin1267d872021-04-16 17:21:36 +0100695 // The dex jar for the stubs.
696 //
697 // This is not the implementation jar, it still only contains stubs.
Martin Stjernholm8be1e6d2021-09-15 03:34:04 +0100698 stubsDexJarPath OptionalDexJarPath
Paul Duffin1267d872021-04-16 17:21:36 +0100699
Jihoon Kangbd093452023-12-26 19:08:01 +0000700 // The exportable dex jar for the stubs.
701 // This is not the implementation jar, it still only contains stubs.
702 // Includes unflagged apis and flagged apis enabled by release configurations.
703 exportableStubsDexJarPath OptionalDexJarPath
704
Paul Duffin0f8faff2020-05-20 16:18:00 +0100705 // The API specification file, e.g. system_current.txt.
706 currentApiFilePath android.OptionalPath
707
708 // The specification of API elements removed since the last release.
709 removedApiFilePath android.OptionalPath
710
711 // The stubs source jar.
712 stubsSrcJar android.OptionalPath
Anton Hanssond78eb762021-09-21 15:25:12 +0100713
714 // Extracted annotations.
715 annotationsZip android.OptionalPath
Paul Duffin958806b2022-05-16 13:10:47 +0000716
717 // The path to the latest API file.
Jihoon Kang5623e542024-01-31 23:27:26 +0000718 latestApiPaths android.Paths
Paul Duffin958806b2022-05-16 13:10:47 +0000719
720 // The path to the latest removed API file.
Jihoon Kang5623e542024-01-31 23:27:26 +0000721 latestRemovedApiPaths android.Paths
Paul Duffind1b3a922020-01-22 11:57:20 +0000722}
723
Colin Crossdcf71b22021-02-01 13:59:03 -0800724func (paths *scopePaths) extractStubsLibraryInfoFromDependency(ctx android.ModuleContext, dep android.Module) error {
Colin Cross313aa542023-12-13 13:47:44 -0800725 if lib, ok := android.OtherModuleProvider(ctx, dep, JavaInfoProvider); ok {
Colin Crossdcf71b22021-02-01 13:59:03 -0800726 paths.stubsHeaderPath = lib.HeaderJars
727 paths.stubsImplPath = lib.ImplementationJars
Paul Duffin1267d872021-04-16 17:21:36 +0100728
729 libDep := dep.(UsesLibraryDependency)
Spandan Das59a4a2b2024-01-09 21:35:56 +0000730 paths.stubsDexJarPath = libDep.DexJarBuildPath(ctx)
Jihoon Kangbd093452023-12-26 19:08:01 +0000731 paths.exportableStubsDexJarPath = libDep.DexJarBuildPath(ctx)
732 return nil
733 } else {
734 return fmt.Errorf("expected module that has JavaInfoProvider, e.g. java_library")
735 }
736}
737
738func (paths *scopePaths) extractEverythingStubsLibraryInfoFromDependency(ctx android.ModuleContext, dep android.Module) error {
739 if lib, ok := android.OtherModuleProvider(ctx, dep, JavaInfoProvider); ok {
740 paths.stubsHeaderPath = lib.HeaderJars
Jihoon Kangf55a5f72024-01-08 08:56:20 +0000741 if !ctx.Config().ReleaseHiddenApiExportableStubs() {
742 paths.stubsImplPath = lib.ImplementationJars
743 }
Jihoon Kangbd093452023-12-26 19:08:01 +0000744
745 libDep := dep.(UsesLibraryDependency)
746 paths.stubsDexJarPath = libDep.DexJarBuildPath(ctx)
747 return nil
748 } else {
749 return fmt.Errorf("expected module that has JavaInfoProvider, e.g. java_library")
750 }
751}
752
753func (paths *scopePaths) extractExportableStubsLibraryInfoFromDependency(ctx android.ModuleContext, dep android.Module) error {
Jihoon Kangf55a5f72024-01-08 08:56:20 +0000754 if lib, ok := android.OtherModuleProvider(ctx, dep, JavaInfoProvider); ok {
755 if ctx.Config().ReleaseHiddenApiExportableStubs() {
756 paths.stubsImplPath = lib.ImplementationJars
757 }
758
Jihoon Kangbd093452023-12-26 19:08:01 +0000759 libDep := dep.(UsesLibraryDependency)
760 paths.exportableStubsDexJarPath = libDep.DexJarBuildPath(ctx)
Paul Duffinc8782502020-04-29 20:45:27 +0100761 return nil
762 } else {
Colin Crossdcf71b22021-02-01 13:59:03 -0800763 return fmt.Errorf("expected module that has JavaInfoProvider, e.g. java_library")
Paul Duffinc8782502020-04-29 20:45:27 +0100764 }
765}
766
Jihoon Kangee113282024-01-23 00:16:41 +0000767func (paths *scopePaths) treatDepAsApiStubsProvider(dep android.Module, action func(provider ApiStubsProvider) error) error {
Paul Duffin0ff08bd2020-04-29 13:30:54 +0100768 if apiStubsProvider, ok := dep.(ApiStubsProvider); ok {
Jihoon Kangee113282024-01-23 00:16:41 +0000769 err := action(apiStubsProvider)
770 if err != nil {
771 return err
772 }
Jihoon Kangf55a5f72024-01-08 08:56:20 +0000773 return nil
774 } else {
775 return fmt.Errorf("expected module that implements ExportableApiStubsSrcProvider, e.g. droidstubs")
776 }
777}
778
Jihoon Kangee113282024-01-23 00:16:41 +0000779func (paths *scopePaths) treatDepAsApiStubsSrcProvider(dep android.Module, action func(provider ApiStubsSrcProvider) error) error {
Paul Duffin0f8faff2020-05-20 16:18:00 +0100780 if apiStubsProvider, ok := dep.(ApiStubsSrcProvider); ok {
Jihoon Kangee113282024-01-23 00:16:41 +0000781 err := action(apiStubsProvider)
782 if err != nil {
783 return err
784 }
Paul Duffin0f8faff2020-05-20 16:18:00 +0100785 return nil
786 } else {
787 return fmt.Errorf("expected module that implements ApiStubsSrcProvider, e.g. droidstubs")
788 }
789}
790
Jihoon Kangee113282024-01-23 00:16:41 +0000791func (paths *scopePaths) extractApiInfoFromApiStubsProvider(provider ApiStubsProvider, stubsType StubsType) error {
792 var annotationsZip, currentApiFilePath, removedApiFilePath android.Path
793 annotationsZip, annotationsZipErr := provider.AnnotationsZip(stubsType)
794 currentApiFilePath, currentApiFilePathErr := provider.ApiFilePath(stubsType)
795 removedApiFilePath, removedApiFilePathErr := provider.RemovedApiFilePath(stubsType)
Paul Duffin0ff08bd2020-04-29 13:30:54 +0100796
Jihoon Kangee113282024-01-23 00:16:41 +0000797 combinedError := errors.Join(annotationsZipErr, currentApiFilePathErr, removedApiFilePathErr)
798
799 if combinedError == nil {
800 paths.annotationsZip = android.OptionalPathForPath(annotationsZip)
801 paths.currentApiFilePath = android.OptionalPathForPath(currentApiFilePath)
802 paths.removedApiFilePath = android.OptionalPathForPath(removedApiFilePath)
803 }
804 return combinedError
Jihoon Kangf55a5f72024-01-08 08:56:20 +0000805}
806
Colin Crossdcf71b22021-02-01 13:59:03 -0800807func (paths *scopePaths) extractApiInfoFromDep(ctx android.ModuleContext, dep android.Module) error {
Jihoon Kangee113282024-01-23 00:16:41 +0000808 return paths.treatDepAsApiStubsProvider(dep, func(provider ApiStubsProvider) error {
809 return paths.extractApiInfoFromApiStubsProvider(provider, Everything)
Paul Duffin0ff08bd2020-04-29 13:30:54 +0100810 })
811}
812
Jihoon Kangee113282024-01-23 00:16:41 +0000813func (paths *scopePaths) extractStubsSourceInfoFromApiStubsProviders(provider ApiStubsSrcProvider, stubsType StubsType) error {
814 stubsSrcJar, err := provider.StubsSrcJar(stubsType)
815 if err == nil {
816 paths.stubsSrcJar = android.OptionalPathForPath(stubsSrcJar)
817 }
818 return err
Jihoon Kangf55a5f72024-01-08 08:56:20 +0000819}
820
Colin Crossdcf71b22021-02-01 13:59:03 -0800821func (paths *scopePaths) extractStubsSourceInfoFromDep(ctx android.ModuleContext, dep android.Module) error {
Jihoon Kangee113282024-01-23 00:16:41 +0000822 return paths.treatDepAsApiStubsSrcProvider(dep, func(provider ApiStubsSrcProvider) error {
823 return paths.extractStubsSourceInfoFromApiStubsProviders(provider, Everything)
Paul Duffin0ff08bd2020-04-29 13:30:54 +0100824 })
825}
826
Colin Crossdcf71b22021-02-01 13:59:03 -0800827func (paths *scopePaths) extractStubsSourceAndApiInfoFromApiStubsProvider(ctx android.ModuleContext, dep android.Module) error {
Jihoon Kangf55a5f72024-01-08 08:56:20 +0000828 if ctx.Config().ReleaseHiddenApiExportableStubs() {
Jihoon Kangee113282024-01-23 00:16:41 +0000829 return paths.treatDepAsApiStubsProvider(dep, func(provider ApiStubsProvider) error {
830 extractApiInfoErr := paths.extractApiInfoFromApiStubsProvider(provider, Exportable)
831 extractStubsSourceInfoErr := paths.extractStubsSourceInfoFromApiStubsProviders(provider, Exportable)
832 return errors.Join(extractApiInfoErr, extractStubsSourceInfoErr)
Jihoon Kangf55a5f72024-01-08 08:56:20 +0000833 })
834 }
Jihoon Kangee113282024-01-23 00:16:41 +0000835 return paths.treatDepAsApiStubsProvider(dep, func(provider ApiStubsProvider) error {
836 extractApiInfoErr := paths.extractApiInfoFromApiStubsProvider(provider, Everything)
837 extractStubsSourceInfoErr := paths.extractStubsSourceInfoFromApiStubsProviders(provider, Everything)
838 return errors.Join(extractApiInfoErr, extractStubsSourceInfoErr)
Paul Duffin0ff08bd2020-04-29 13:30:54 +0100839 })
840}
841
Jihoon Kang5623e542024-01-31 23:27:26 +0000842func extractOutputPaths(dep android.Module) (android.Paths, error) {
Paul Duffin958806b2022-05-16 13:10:47 +0000843 var paths android.Paths
844 if sourceFileProducer, ok := dep.(android.SourceFileProducer); ok {
845 paths = sourceFileProducer.Srcs()
Jihoon Kang5623e542024-01-31 23:27:26 +0000846 return paths, nil
Paul Duffin958806b2022-05-16 13:10:47 +0000847 } else {
Jihoon Kang5623e542024-01-31 23:27:26 +0000848 return nil, fmt.Errorf("module %q does not produce source files", dep)
Paul Duffin958806b2022-05-16 13:10:47 +0000849 }
Paul Duffin958806b2022-05-16 13:10:47 +0000850}
851
852func (paths *scopePaths) extractLatestApiPath(ctx android.ModuleContext, dep android.Module) error {
Jihoon Kang5623e542024-01-31 23:27:26 +0000853 outputPaths, err := extractOutputPaths(dep)
854 paths.latestApiPaths = outputPaths
Paul Duffin958806b2022-05-16 13:10:47 +0000855 return err
856}
857
858func (paths *scopePaths) extractLatestRemovedApiPath(ctx android.ModuleContext, dep android.Module) error {
Jihoon Kang5623e542024-01-31 23:27:26 +0000859 outputPaths, err := extractOutputPaths(dep)
860 paths.latestRemovedApiPaths = outputPaths
Paul Duffin958806b2022-05-16 13:10:47 +0000861 return err
862}
863
Paul Duffin0ff08bd2020-04-29 13:30:54 +0100864type commonToSdkLibraryAndImportProperties struct {
Paul Duffin1b1e8062020-05-08 13:44:43 +0100865 // The naming scheme to use for the components that this module creates.
866 //
Paul Duffinee9ad5d2020-09-11 13:04:05 +0100867 // If not specified then it defaults to "default".
Paul Duffin1b1e8062020-05-08 13:44:43 +0100868 //
869 // This is a temporary mechanism to simplify conversion from separate modules for each
870 // component that follow a different naming pattern to the default one.
871 //
872 // TODO(b/155480189) - Remove once naming inconsistencies have been resolved.
Paul Duffin0ff08bd2020-04-29 13:30:54 +0100873 Naming_scheme *string
Paul Duffindfa131e2020-05-15 20:37:11 +0100874
875 // Specifies whether this module can be used as an Android shared library; defaults
876 // to true.
877 //
878 // An Android shared library is one that can be referenced in a <uses-library> element
879 // in an AndroidManifest.xml.
880 Shared_library *bool
Paul Duffina2ae7e02020-09-11 11:55:00 +0100881
882 // Files containing information about supported java doc tags.
883 Doctag_files []string `android:"path"`
Pedro Loureiro9956e5e2021-09-07 17:21:59 +0000884
885 // Signals that this shared library is part of the bootclasspath starting
886 // on the version indicated in this attribute.
887 //
888 // This will make platforms at this level and above to ignore
889 // <uses-library> tags with this library name because the library is already
890 // available
891 On_bootclasspath_since *string
892
893 // Signals that this shared library was part of the bootclasspath before
894 // (but not including) the version indicated in this attribute.
895 //
896 // The system will automatically add a <uses-library> tag with this library to
897 // apps that target any SDK less than the version indicated in this attribute.
898 On_bootclasspath_before *string
899
900 // Indicates that PackageManager should ignore this shared library if the
901 // platform is below the version indicated in this attribute.
902 //
903 // This means that the device won't recognise this library as installed.
904 Min_device_sdk *string
905
906 // Indicates that PackageManager should ignore this shared library if the
907 // platform is above the version indicated in this attribute.
908 //
909 // This means that the device won't recognise this library as installed.
910 Max_device_sdk *string
Paul Duffin0ff08bd2020-04-29 13:30:54 +0100911}
912
Paul Duffin71b33cc2021-06-23 11:39:47 +0100913// commonSdkLibraryAndImportModule defines the interface that must be provided by a module that
914// embeds the commonToSdkLibraryAndImport struct.
915type commonSdkLibraryAndImportModule interface {
Paul Duffind796f6f2022-11-23 23:06:05 +0000916 android.Module
Paul Duffin71b33cc2021-06-23 11:39:47 +0100917
Spandan Das23956d12024-01-19 00:22:22 +0000918 // Returns the name of the root java_sdk_library that creates the child stub libraries
919 // This is the `name` as it appears in Android.bp, and not the name in Soong's build graph
920 // (with the prebuilt_ prefix)
921 //
922 // e.g. in the following java_sdk_library_import
923 // java_sdk_library_import {
924 // name: "framework-foo.v1",
925 // source_module_name: "framework-foo",
926 // }
927 // the values returned by
928 // 1. Name(): prebuilt_framework-foo.v1 # unique
929 // 2. BaseModuleName(): framework-foo # the source
930 // 3. RootLibraryName: framework-foo.v1 # the undecordated `name` from Android.bp
931 RootLibraryName() string
932}
933
934func (m *SdkLibrary) RootLibraryName() string {
935 return m.BaseModuleName()
936}
937
938func (m *SdkLibraryImport) RootLibraryName() string {
939 // m.BaseModuleName refers to the source of the import
940 // use moduleBase.Name to get the name of the module as it appears in the .bp file
941 return m.ModuleBase.Name()
Paul Duffin71b33cc2021-06-23 11:39:47 +0100942}
943
Paul Duffin56d44902020-01-31 13:36:25 +0000944// Common code between sdk library and sdk library import
945type commonToSdkLibraryAndImport struct {
Paul Duffin71b33cc2021-06-23 11:39:47 +0100946 module commonSdkLibraryAndImportModule
Paul Duffinc3091c82020-05-08 14:16:20 +0100947
Paul Duffin56d44902020-01-31 13:36:25 +0000948 scopePaths map[*apiScope]*scopePaths
Paul Duffin1b1e8062020-05-08 13:44:43 +0100949
950 namingScheme sdkLibraryComponentNamingScheme
951
Paul Duffindfa131e2020-05-15 20:37:11 +0100952 commonSdkLibraryProperties commonToSdkLibraryAndImportProperties
Paul Duffin859fe962020-05-15 10:20:31 +0100953
Paul Duffina2ae7e02020-09-11 11:55:00 +0100954 // Paths to commonSdkLibraryProperties.Doctag_files
955 doctagPaths android.Paths
956
Paul Duffin859fe962020-05-15 10:20:31 +0100957 // Functionality related to this being used as a component of a java_sdk_library.
958 EmbeddableSdkLibraryComponent
Jihoon Kang8479dea2024-04-04 01:19:05 +0000959
960 // Path to the header jars of the implementation library
961 // This is non-empty only when api_only is false.
962 implLibraryHeaderJars android.Paths
Jihoon Kanga3a05462024-04-05 00:36:44 +0000963
964 // The reference to the implementation library created by the source module.
965 // Is nil if the source module does not exist.
966 implLibraryModule *Library
Paul Duffin56d44902020-01-31 13:36:25 +0000967}
968
Paul Duffin71b33cc2021-06-23 11:39:47 +0100969func (c *commonToSdkLibraryAndImport) initCommon(module commonSdkLibraryAndImportModule) {
970 c.module = module
Paul Duffin1b1e8062020-05-08 13:44:43 +0100971
Paul Duffin71b33cc2021-06-23 11:39:47 +0100972 module.AddProperties(&c.commonSdkLibraryProperties)
Paul Duffin859fe962020-05-15 10:20:31 +0100973
974 // Initialize this as an sdk library component.
Paul Duffin71b33cc2021-06-23 11:39:47 +0100975 c.initSdkLibraryComponent(module)
Paul Duffin1b1e8062020-05-08 13:44:43 +0100976}
977
978func (c *commonToSdkLibraryAndImport) initCommonAfterDefaultsApplied(ctx android.DefaultableHookContext) bool {
Paul Duffindfa131e2020-05-15 20:37:11 +0100979 schemeProperty := proptools.StringDefault(c.commonSdkLibraryProperties.Naming_scheme, "default")
Paul Duffin1b1e8062020-05-08 13:44:43 +0100980 switch schemeProperty {
981 case "default":
982 c.namingScheme = &defaultNamingScheme{}
983 default:
984 ctx.PropertyErrorf("naming_scheme", "expected 'default' but was %q", schemeProperty)
985 return false
986 }
987
Spandan Das23956d12024-01-19 00:22:22 +0000988 namePtr := proptools.StringPtr(c.module.RootLibraryName())
Paul Duffin3f0290e2021-06-30 18:25:36 +0100989 c.sdkLibraryComponentProperties.SdkLibraryName = namePtr
990
Paul Duffindfa131e2020-05-15 20:37:11 +0100991 // Only track this sdk library if this can be used as a shared library.
992 if c.sharedLibrary() {
993 // Use the name specified in the module definition as the owner.
Paul Duffin3f0290e2021-06-30 18:25:36 +0100994 c.sdkLibraryComponentProperties.SdkLibraryToImplicitlyTrack = namePtr
Paul Duffindfa131e2020-05-15 20:37:11 +0100995 }
Paul Duffin859fe962020-05-15 10:20:31 +0100996
Paul Duffin1b1e8062020-05-08 13:44:43 +0100997 return true
Paul Duffinc3091c82020-05-08 14:16:20 +0100998}
999
Paul Duffinea8f8082021-06-24 13:25:57 +01001000// uniqueApexVariations provides common implementation of the ApexModule.UniqueApexVariations
1001// method.
1002func (c *commonToSdkLibraryAndImport) uniqueApexVariations() bool {
1003 // A java_sdk_library that is a shared library produces an XML file that makes the shared library
1004 // usable from an AndroidManifest.xml's <uses-library> entry. That XML file contains the name of
1005 // the APEX and so it needs a unique variation per APEX.
1006 return c.sharedLibrary()
1007}
1008
Paul Duffina2ae7e02020-09-11 11:55:00 +01001009func (c *commonToSdkLibraryAndImport) generateCommonBuildActions(ctx android.ModuleContext) {
1010 c.doctagPaths = android.PathsForModuleSrc(ctx, c.commonSdkLibraryProperties.Doctag_files)
1011}
1012
Jihoon Kanga3a05462024-04-05 00:36:44 +00001013func (c *commonToSdkLibraryAndImport) getImplLibraryModule() *Library {
1014 return c.implLibraryModule
1015}
1016
Paul Duffineedc5d52020-06-12 17:46:39 +01001017// Module name of the runtime implementation library
1018func (c *commonToSdkLibraryAndImport) implLibraryModuleName() string {
Spandan Das23956d12024-01-19 00:22:22 +00001019 return c.module.RootLibraryName() + ".impl"
Paul Duffineedc5d52020-06-12 17:46:39 +01001020}
1021
1022// Module name of the XML file for the lib
1023func (c *commonToSdkLibraryAndImport) xmlPermissionsModuleName() string {
Spandan Das23956d12024-01-19 00:22:22 +00001024 return c.module.RootLibraryName() + sdkXmlFileSuffix
Paul Duffineedc5d52020-06-12 17:46:39 +01001025}
1026
Paul Duffinc3091c82020-05-08 14:16:20 +01001027// Name of the java_library module that compiles the stubs source.
1028func (c *commonToSdkLibraryAndImport) stubsLibraryModuleName(apiScope *apiScope) string {
Spandan Das23956d12024-01-19 00:22:22 +00001029 baseName := c.module.RootLibraryName()
Paul Duffin21787622022-11-25 12:48:20 +00001030 return c.namingScheme.stubsLibraryModuleName(apiScope, baseName)
Paul Duffinc3091c82020-05-08 14:16:20 +01001031}
1032
Jihoon Kangfa4a90d2023-12-20 02:53:38 +00001033// Name of the java_library module that compiles the exportable stubs source.
1034func (c *commonToSdkLibraryAndImport) exportableStubsLibraryModuleName(apiScope *apiScope) string {
Spandan Das23956d12024-01-19 00:22:22 +00001035 baseName := c.module.RootLibraryName()
Jihoon Kangfa4a90d2023-12-20 02:53:38 +00001036 return c.namingScheme.exportableStubsLibraryModuleName(apiScope, baseName)
1037}
1038
Paul Duffinc3091c82020-05-08 14:16:20 +01001039// Name of the droidstubs module that generates the stubs source and may also
1040// generate/check the API.
1041func (c *commonToSdkLibraryAndImport) stubsSourceModuleName(apiScope *apiScope) string {
Spandan Das23956d12024-01-19 00:22:22 +00001042 baseName := c.module.RootLibraryName()
Paul Duffin21787622022-11-25 12:48:20 +00001043 return c.namingScheme.stubsSourceModuleName(apiScope, baseName)
Paul Duffinc3091c82020-05-08 14:16:20 +01001044}
1045
Jihoon Kang1c92c3e2023-03-23 17:44:51 +00001046// Name of the java_api_library module that generates the from-text stubs source
1047// and compiles to a jar file.
1048func (c *commonToSdkLibraryAndImport) apiLibraryModuleName(apiScope *apiScope) string {
Spandan Das23956d12024-01-19 00:22:22 +00001049 baseName := c.module.RootLibraryName()
Jihoon Kang1c92c3e2023-03-23 17:44:51 +00001050 return c.namingScheme.apiLibraryModuleName(apiScope, baseName)
1051}
1052
Jihoon Kang1147b312023-06-08 23:25:57 +00001053// Name of the java_library module that compiles the stubs
1054// generated from source Java files.
Jihoon Kangfa4a90d2023-12-20 02:53:38 +00001055func (c *commonToSdkLibraryAndImport) sourceStubsLibraryModuleName(apiScope *apiScope) string {
Spandan Das23956d12024-01-19 00:22:22 +00001056 baseName := c.module.RootLibraryName()
Jihoon Kangfa4a90d2023-12-20 02:53:38 +00001057 return c.namingScheme.sourceStubsLibraryModuleName(apiScope, baseName)
1058}
1059
1060// Name of the java_library module that compiles the exportable stubs
1061// generated from source Java files.
1062func (c *commonToSdkLibraryAndImport) exportableSourceStubsLibraryModuleName(apiScope *apiScope) string {
Spandan Das23956d12024-01-19 00:22:22 +00001063 baseName := c.module.RootLibraryName()
Jihoon Kangfa4a90d2023-12-20 02:53:38 +00001064 return c.namingScheme.exportableSourceStubsLibraryModuleName(apiScope, baseName)
Jihoon Kang1147b312023-06-08 23:25:57 +00001065}
1066
Paul Duffin46dc45a2020-05-14 15:39:10 +01001067// The component names for different outputs of the java_sdk_library.
1068//
1069// They are similar to the names used for the child modules it creates
1070const (
1071 stubsSourceComponentName = "stubs.source"
1072
1073 apiTxtComponentName = "api.txt"
1074
1075 removedApiTxtComponentName = "removed-api.txt"
Anton Hanssond78eb762021-09-21 15:25:12 +01001076
1077 annotationsComponentName = "annotations.zip"
Paul Duffin46dc45a2020-05-14 15:39:10 +01001078)
1079
1080// A regular expression to match tags that reference a specific stubs component.
1081//
1082// It will only match if given a valid scope and a valid component. It is verfy strict
1083// to ensure it does not accidentally match a similar looking tag that should be processed
1084// by the embedded Library.
1085var tagSplitter = func() *regexp.Regexp {
1086 // Given a list of literal string items returns a regular expression that will
1087 // match any one of the items.
1088 choice := func(items ...string) string {
1089 return `\Q` + strings.Join(items, `\E|\Q`) + `\E`
1090 }
1091
1092 // Regular expression to match one of the scopes.
1093 scopesRegexp := choice(allScopeNames...)
1094
1095 // Regular expression to match one of the components.
Anton Hanssond78eb762021-09-21 15:25:12 +01001096 componentsRegexp := choice(stubsSourceComponentName, apiTxtComponentName, removedApiTxtComponentName, annotationsComponentName)
Paul Duffin46dc45a2020-05-14 15:39:10 +01001097
1098 // Regular expression to match any combination of one scope and one component.
1099 return regexp.MustCompile(fmt.Sprintf(`^\.(%s)\.(%s)$`, scopesRegexp, componentsRegexp))
1100}()
1101
1102// For OutputFileProducer interface
1103//
Anton Hanssond78eb762021-09-21 15:25:12 +01001104// .<scope>.<component name>, for all ComponentNames (for example: .public.removed-api.txt)
Paul Duffin46dc45a2020-05-14 15:39:10 +01001105func (c *commonToSdkLibraryAndImport) commonOutputFiles(tag string) (android.Paths, error) {
1106 if groups := tagSplitter.FindStringSubmatch(tag); groups != nil {
1107 scopeName := groups[1]
1108 component := groups[2]
1109
1110 if scope, ok := scopeByName[scopeName]; ok {
1111 paths := c.findScopePaths(scope)
1112 if paths == nil {
Spandan Das23956d12024-01-19 00:22:22 +00001113 return nil, fmt.Errorf("%q does not provide api scope %s", c.module.RootLibraryName(), scopeName)
Paul Duffin46dc45a2020-05-14 15:39:10 +01001114 }
1115
1116 switch component {
1117 case stubsSourceComponentName:
1118 if paths.stubsSrcJar.Valid() {
1119 return android.Paths{paths.stubsSrcJar.Path()}, nil
1120 }
1121
1122 case apiTxtComponentName:
1123 if paths.currentApiFilePath.Valid() {
1124 return android.Paths{paths.currentApiFilePath.Path()}, nil
1125 }
1126
1127 case removedApiTxtComponentName:
1128 if paths.removedApiFilePath.Valid() {
1129 return android.Paths{paths.removedApiFilePath.Path()}, nil
1130 }
Anton Hanssond78eb762021-09-21 15:25:12 +01001131
1132 case annotationsComponentName:
1133 if paths.annotationsZip.Valid() {
1134 return android.Paths{paths.annotationsZip.Path()}, nil
1135 }
Paul Duffin46dc45a2020-05-14 15:39:10 +01001136 }
1137
1138 return nil, fmt.Errorf("%s not available for api scope %s", component, scopeName)
1139 } else {
1140 return nil, fmt.Errorf("unknown scope %s in %s", scope, tag)
1141 }
1142
1143 } else {
Paul Duffina2ae7e02020-09-11 11:55:00 +01001144 switch tag {
1145 case ".doctags":
1146 if c.doctagPaths != nil {
1147 return c.doctagPaths, nil
1148 } else {
Spandan Das23956d12024-01-19 00:22:22 +00001149 return nil, fmt.Errorf("no doctag_files specified on %s", c.module.RootLibraryName())
Paul Duffina2ae7e02020-09-11 11:55:00 +01001150 }
1151 }
Paul Duffin46dc45a2020-05-14 15:39:10 +01001152 return nil, nil
1153 }
1154}
1155
Paul Duffin803a9562020-05-20 11:52:25 +01001156func (c *commonToSdkLibraryAndImport) getScopePathsCreateIfNeeded(scope *apiScope) *scopePaths {
Paul Duffin56d44902020-01-31 13:36:25 +00001157 if c.scopePaths == nil {
1158 c.scopePaths = make(map[*apiScope]*scopePaths)
1159 }
1160 paths := c.scopePaths[scope]
1161 if paths == nil {
1162 paths = &scopePaths{}
1163 c.scopePaths[scope] = paths
1164 }
1165
1166 return paths
1167}
1168
Paul Duffin803a9562020-05-20 11:52:25 +01001169func (c *commonToSdkLibraryAndImport) findScopePaths(scope *apiScope) *scopePaths {
1170 if c.scopePaths == nil {
1171 return nil
1172 }
1173
1174 return c.scopePaths[scope]
1175}
1176
1177// If this does not support the requested api scope then find the closest available
1178// scope it does support. Returns nil if no such scope is available.
1179func (c *commonToSdkLibraryAndImport) findClosestScopePath(scope *apiScope) *scopePaths {
Paul Duffind0b9fca2022-09-30 18:11:41 +01001180 for s := scope; s != nil; s = s.canAccess {
Paul Duffin803a9562020-05-20 11:52:25 +01001181 if paths := c.findScopePaths(s); paths != nil {
1182 return paths
1183 }
1184 }
1185
1186 // This should never happen outside tests as public should be the base scope for every
1187 // scope and is enabled by default.
1188 return nil
1189}
1190
Jiyong Parkf1691d22021-03-29 20:11:58 +09001191func (c *commonToSdkLibraryAndImport) selectHeaderJarsForSdkVersion(ctx android.BaseModuleContext, sdkVersion android.SdkSpec) android.Paths {
Paul Duffinb05d4292020-05-20 12:19:10 +01001192
1193 // If a specific numeric version has been requested then use prebuilt versions of the sdk.
Jiyong Park54105c42021-03-31 18:17:53 +09001194 if !sdkVersion.ApiLevel.IsPreview() {
Spandan Das23956d12024-01-19 00:22:22 +00001195 return PrebuiltJars(ctx, c.module.RootLibraryName(), sdkVersion)
Paul Duffinb05d4292020-05-20 12:19:10 +01001196 }
1197
Paul Duffin1267d872021-04-16 17:21:36 +01001198 paths := c.selectScopePaths(ctx, sdkVersion.Kind)
1199 if paths == nil {
1200 return nil
1201 }
1202
1203 return paths.stubsHeaderPath
1204}
1205
1206// selectScopePaths returns the *scopePaths appropriate for the specific kind.
1207//
1208// If the module does not support the specific kind then it will return the *scopePaths for the
1209// closest kind which is a subset of the requested kind. e.g. if requesting android.SdkModule then
1210// it will return *scopePaths for android.SdkSystem if available or android.SdkPublic of not.
1211func (c *commonToSdkLibraryAndImport) selectScopePaths(ctx android.BaseModuleContext, kind android.SdkKind) *scopePaths {
Paul Duffin32cf58a2021-05-18 16:32:50 +01001212 apiScope := sdkKindToApiScope(kind)
Paul Duffinb05d4292020-05-20 12:19:10 +01001213
Paul Duffin803a9562020-05-20 11:52:25 +01001214 paths := c.findClosestScopePath(apiScope)
1215 if paths == nil {
1216 var scopes []string
Jihoon Kang98aa8fa2024-06-07 11:06:57 +00001217 for _, s := range AllApiScopes {
Paul Duffin803a9562020-05-20 11:52:25 +01001218 if c.findScopePaths(s) != nil {
1219 scopes = append(scopes, s.name)
1220 }
1221 }
Spandan Das23956d12024-01-19 00:22:22 +00001222 ctx.ModuleErrorf("requires api scope %s from %s but it only has %q available", apiScope.name, c.module.RootLibraryName(), scopes)
Paul Duffin803a9562020-05-20 11:52:25 +01001223 return nil
1224 }
1225
Paul Duffin1267d872021-04-16 17:21:36 +01001226 return paths
1227}
1228
Paul Duffin32cf58a2021-05-18 16:32:50 +01001229// sdkKindToApiScope maps from android.SdkKind to apiScope.
1230func sdkKindToApiScope(kind android.SdkKind) *apiScope {
1231 var apiScope *apiScope
1232 switch kind {
1233 case android.SdkSystem:
1234 apiScope = apiScopeSystem
1235 case android.SdkModule:
1236 apiScope = apiScopeModuleLib
1237 case android.SdkTest:
1238 apiScope = apiScopeTest
1239 case android.SdkSystemServer:
1240 apiScope = apiScopeSystemServer
1241 default:
1242 apiScope = apiScopePublic
1243 }
1244 return apiScope
1245}
1246
Paul Duffin1267d872021-04-16 17:21:36 +01001247// to satisfy SdkLibraryDependency interface
Martin Stjernholm8be1e6d2021-09-15 03:34:04 +01001248func (c *commonToSdkLibraryAndImport) SdkApiStubDexJar(ctx android.BaseModuleContext, kind android.SdkKind) OptionalDexJarPath {
Paul Duffin1267d872021-04-16 17:21:36 +01001249 paths := c.selectScopePaths(ctx, kind)
1250 if paths == nil {
Martin Stjernholm8be1e6d2021-09-15 03:34:04 +01001251 return makeUnsetDexJarPath()
Paul Duffin1267d872021-04-16 17:21:36 +01001252 }
1253
1254 return paths.stubsDexJarPath
Paul Duffinb05d4292020-05-20 12:19:10 +01001255}
1256
Paul Duffin32cf58a2021-05-18 16:32:50 +01001257// to satisfy SdkLibraryDependency interface
Jihoon Kangbd093452023-12-26 19:08:01 +00001258func (c *commonToSdkLibraryAndImport) SdkApiExportableStubDexJar(ctx android.BaseModuleContext, kind android.SdkKind) OptionalDexJarPath {
1259 paths := c.selectScopePaths(ctx, kind)
1260 if paths == nil {
1261 return makeUnsetDexJarPath()
1262 }
1263
1264 return paths.exportableStubsDexJarPath
1265}
1266
1267// to satisfy SdkLibraryDependency interface
Paul Duffin32cf58a2021-05-18 16:32:50 +01001268func (c *commonToSdkLibraryAndImport) SdkRemovedTxtFile(ctx android.BaseModuleContext, kind android.SdkKind) android.OptionalPath {
1269 apiScope := sdkKindToApiScope(kind)
1270 paths := c.findScopePaths(apiScope)
1271 if paths == nil {
1272 return android.OptionalPath{}
1273 }
1274
1275 return paths.removedApiFilePath
1276}
1277
Paul Duffin859fe962020-05-15 10:20:31 +01001278func (c *commonToSdkLibraryAndImport) sdkComponentPropertiesForChildLibrary() interface{} {
1279 componentProps := &struct {
Paul Duffin3f0290e2021-06-30 18:25:36 +01001280 SdkLibraryName *string
Paul Duffin859fe962020-05-15 10:20:31 +01001281 SdkLibraryToImplicitlyTrack *string
Paul Duffindfa131e2020-05-15 20:37:11 +01001282 }{}
1283
Spandan Das23956d12024-01-19 00:22:22 +00001284 namePtr := proptools.StringPtr(c.module.RootLibraryName())
Paul Duffin3f0290e2021-06-30 18:25:36 +01001285 componentProps.SdkLibraryName = namePtr
1286
Paul Duffindfa131e2020-05-15 20:37:11 +01001287 if c.sharedLibrary() {
Paul Duffin859fe962020-05-15 10:20:31 +01001288 // Mark the stubs library as being components of this java_sdk_library so that
1289 // any app that includes code which depends (directly or indirectly) on the stubs
1290 // library will have the appropriate <uses-library> invocation inserted into its
1291 // manifest if necessary.
Paul Duffin3f0290e2021-06-30 18:25:36 +01001292 componentProps.SdkLibraryToImplicitlyTrack = namePtr
Paul Duffin859fe962020-05-15 10:20:31 +01001293 }
1294
1295 return componentProps
1296}
1297
Paul Duffindfa131e2020-05-15 20:37:11 +01001298func (c *commonToSdkLibraryAndImport) sharedLibrary() bool {
1299 return proptools.BoolDefault(c.commonSdkLibraryProperties.Shared_library, true)
1300}
1301
Paul Duffinf4600f62021-05-13 22:34:45 +01001302// Check if the stub libraries should be compiled for dex
1303func (c *commonToSdkLibraryAndImport) stubLibrariesCompiledForDex() bool {
1304 // Always compile the dex file files for the stub libraries if they will be used on the
1305 // bootclasspath.
1306 return !c.sharedLibrary()
1307}
1308
Paul Duffin859fe962020-05-15 10:20:31 +01001309// Properties related to the use of a module as an component of a java_sdk_library.
1310type SdkLibraryComponentProperties struct {
Paul Duffin3f0290e2021-06-30 18:25:36 +01001311 // The name of the java_sdk_library/_import module.
1312 SdkLibraryName *string `blueprint:"mutated"`
Paul Duffin859fe962020-05-15 10:20:31 +01001313
1314 // The name of the java_sdk_library/_import to add to a <uses-library> entry
1315 // in the AndroidManifest.xml of any Android app that includes code that references
1316 // this module. If not set then no java_sdk_library/_import is tracked.
1317 SdkLibraryToImplicitlyTrack *string `blueprint:"mutated"`
1318}
1319
1320// Structure to be embedded in a module struct that needs to support the
1321// SdkLibraryComponentDependency interface.
1322type EmbeddableSdkLibraryComponent struct {
1323 sdkLibraryComponentProperties SdkLibraryComponentProperties
1324}
1325
Paul Duffin71b33cc2021-06-23 11:39:47 +01001326func (e *EmbeddableSdkLibraryComponent) initSdkLibraryComponent(module android.Module) {
1327 module.AddProperties(&e.sdkLibraryComponentProperties)
Paul Duffin859fe962020-05-15 10:20:31 +01001328}
1329
1330// to satisfy SdkLibraryComponentDependency
Paul Duffin3f0290e2021-06-30 18:25:36 +01001331func (e *EmbeddableSdkLibraryComponent) SdkLibraryName() *string {
1332 return e.sdkLibraryComponentProperties.SdkLibraryName
1333}
1334
1335// to satisfy SdkLibraryComponentDependency
Ulya Trafimovich39b437b2020-09-23 16:42:35 +01001336func (e *EmbeddableSdkLibraryComponent) OptionalSdkLibraryImplementation() *string {
Ulya Trafimovich78645fb2021-07-16 15:29:25 +01001337 // For shared libraries, this is the same as the SDK library name. If a Java library or app
1338 // depends on a component library (e.g. a stub library) it still needs to know the name of the
1339 // run-time library and the corresponding module that provides the implementation. This name is
1340 // passed to manifest_fixer (to be added to AndroidManifest.xml) and added to CLC (to be used
1341 // in dexpreopt).
1342 //
1343 // For non-shared SDK (component or not) libraries this returns `nil`, as they are not
1344 // <uses-library> and should not be added to the manifest or to CLC.
Ulya Trafimovich39b437b2020-09-23 16:42:35 +01001345 return e.sdkLibraryComponentProperties.SdkLibraryToImplicitlyTrack
1346}
1347
Paul Duffin859fe962020-05-15 10:20:31 +01001348// Implemented by modules that are (or possibly could be) a component of a java_sdk_library
1349// (including the java_sdk_library) itself.
1350type SdkLibraryComponentDependency interface {
Ulya Trafimovich31e444e2020-08-14 17:32:16 +01001351 UsesLibraryDependency
1352
Paul Duffin3f0290e2021-06-30 18:25:36 +01001353 // SdkLibraryName returns the name of the java_sdk_library/_import module.
1354 SdkLibraryName() *string
1355
Ulya Trafimovich39b437b2020-09-23 16:42:35 +01001356 // The name of the implementation library for the optional SDK library or nil, if there isn't one.
1357 OptionalSdkLibraryImplementation() *string
Paul Duffin859fe962020-05-15 10:20:31 +01001358}
1359
1360// Make sure that all the module types that are components of java_sdk_library/_import
1361// and which can be referenced (directly or indirectly) from an android app implement
1362// the SdkLibraryComponentDependency interface.
1363var _ SdkLibraryComponentDependency = (*Library)(nil)
1364var _ SdkLibraryComponentDependency = (*Import)(nil)
1365var _ SdkLibraryComponentDependency = (*SdkLibrary)(nil)
Paul Duffineedc5d52020-06-12 17:46:39 +01001366var _ SdkLibraryComponentDependency = (*SdkLibraryImport)(nil)
Paul Duffin859fe962020-05-15 10:20:31 +01001367
Paul Duffin32cf58a2021-05-18 16:32:50 +01001368// Provides access to sdk_version related files, e.g. header and implementation jars.
Paul Duffin859fe962020-05-15 10:20:31 +01001369type SdkLibraryDependency interface {
1370 SdkLibraryComponentDependency
1371
1372 // Get the header jars appropriate for the supplied sdk_version.
1373 //
1374 // These are turbine generated jars so they only change if the externals of the
1375 // class changes but it does not contain and implementation or JavaDoc.
Jiyong Parkf1691d22021-03-29 20:11:58 +09001376 SdkHeaderJars(ctx android.BaseModuleContext, sdkVersion android.SdkSpec) android.Paths
Paul Duffin859fe962020-05-15 10:20:31 +01001377
Jihoon Kangbd093452023-12-26 19:08:01 +00001378 // SdkApiStubDexJar returns the dex jar for the stubs for the prebuilt
1379 // java_sdk_library_import module. It is needed by the hiddenapi processing tool which
1380 // processes dex files.
Martin Stjernholm8be1e6d2021-09-15 03:34:04 +01001381 SdkApiStubDexJar(ctx android.BaseModuleContext, kind android.SdkKind) OptionalDexJarPath
Paul Duffinf4600f62021-05-13 22:34:45 +01001382
Jihoon Kangbd093452023-12-26 19:08:01 +00001383 // SdkApiExportableStubDexJar returns the exportable dex jar for the stubs for
1384 // java_sdk_library module. It is needed by the hiddenapi processing tool which processes
1385 // dex files.
1386 SdkApiExportableStubDexJar(ctx android.BaseModuleContext, kind android.SdkKind) OptionalDexJarPath
1387
Paul Duffin32cf58a2021-05-18 16:32:50 +01001388 // SdkRemovedTxtFile returns the optional path to the removed.txt file for the specified sdk kind.
1389 SdkRemovedTxtFile(ctx android.BaseModuleContext, kind android.SdkKind) android.OptionalPath
1390
Paul Duffinf4600f62021-05-13 22:34:45 +01001391 // sharedLibrary returns true if this can be used as a shared library.
1392 sharedLibrary() bool
Jihoon Kanga3a05462024-04-05 00:36:44 +00001393
1394 getImplLibraryModule() *Library
Paul Duffin859fe962020-05-15 10:20:31 +01001395}
1396
Inseob Kimc0907f12019-02-08 21:00:45 +09001397type SdkLibrary struct {
Sundong Ahn054b19a2018-10-19 13:46:09 +09001398 Library
Jiyong Parkc678ad32018-04-10 13:07:10 +09001399
Sundong Ahn054b19a2018-10-19 13:46:09 +09001400 sdkLibraryProperties sdkLibraryProperties
Jiyong Parkc678ad32018-04-10 13:07:10 +09001401
Paul Duffin3375e352020-04-28 10:44:03 +01001402 // Map from api scope to the scope specific property structure.
1403 scopeToProperties map[*apiScope]*ApiScopeProperties
1404
Paul Duffin56d44902020-01-31 13:36:25 +00001405 commonToSdkLibraryAndImport
Jihoon Kanga3a05462024-04-05 00:36:44 +00001406
1407 builtInstalledForApex []dexpreopterInstall
Jiyong Parkc678ad32018-04-10 13:07:10 +09001408}
1409
Inseob Kimc0907f12019-02-08 21:00:45 +09001410var _ SdkLibraryDependency = (*SdkLibrary)(nil)
Colin Cross897d2ed2019-02-11 14:03:51 -08001411
Paul Duffin3375e352020-04-28 10:44:03 +01001412func (module *SdkLibrary) generateTestAndSystemScopesByDefault() bool {
1413 return module.sdkLibraryProperties.Generate_system_and_test_apis
1414}
1415
Jihoon Kanga3a05462024-04-05 00:36:44 +00001416func (module *SdkLibrary) DexJarBuildPath(ctx android.ModuleErrorfContext) OptionalDexJarPath {
1417 if module.implLibraryModule != nil {
1418 return module.implLibraryModule.DexJarBuildPath(ctx)
1419 }
1420 return makeUnsetDexJarPath()
1421}
1422
1423func (module *SdkLibrary) DexJarInstallPath() android.Path {
1424 if module.implLibraryModule != nil {
1425 return module.implLibraryModule.DexJarInstallPath()
1426 }
1427 return nil
1428}
1429
Paul Duffin3375e352020-04-28 10:44:03 +01001430func (module *SdkLibrary) getGeneratedApiScopes(ctx android.EarlyModuleContext) apiScopes {
1431 // Check to see if any scopes have been explicitly enabled. If any have then all
1432 // must be.
1433 anyScopesExplicitlyEnabled := false
Jihoon Kang98aa8fa2024-06-07 11:06:57 +00001434 for _, scope := range AllApiScopes {
Paul Duffin3375e352020-04-28 10:44:03 +01001435 scopeProperties := module.scopeToProperties[scope]
1436 if scopeProperties.Enabled != nil {
1437 anyScopesExplicitlyEnabled = true
1438 break
1439 }
Paul Duffind1b3a922020-01-22 11:57:20 +00001440 }
Paul Duffin3375e352020-04-28 10:44:03 +01001441
1442 var generatedScopes apiScopes
1443 enabledScopes := make(map[*apiScope]struct{})
Jihoon Kang98aa8fa2024-06-07 11:06:57 +00001444 for _, scope := range AllApiScopes {
Paul Duffin3375e352020-04-28 10:44:03 +01001445 scopeProperties := module.scopeToProperties[scope]
1446 // If any scopes are explicitly enabled then ignore the legacy enabled status.
1447 // This is to ensure that any new usages of this module type do not rely on legacy
1448 // behaviour.
1449 defaultEnabledStatus := false
1450 if anyScopesExplicitlyEnabled {
1451 defaultEnabledStatus = scope.defaultEnabledStatus
1452 } else {
1453 defaultEnabledStatus = scope.legacyEnabledStatus(module)
1454 }
1455 enabled := proptools.BoolDefault(scopeProperties.Enabled, defaultEnabledStatus)
1456 if enabled {
1457 enabledScopes[scope] = struct{}{}
1458 generatedScopes = append(generatedScopes, scope)
1459 }
1460 }
1461
1462 // Now check to make sure that any scope that is extended by an enabled scope is also
1463 // enabled.
Jihoon Kang98aa8fa2024-06-07 11:06:57 +00001464 for _, scope := range AllApiScopes {
Paul Duffin3375e352020-04-28 10:44:03 +01001465 if _, ok := enabledScopes[scope]; ok {
1466 extends := scope.extends
1467 if extends != nil {
1468 if _, ok := enabledScopes[extends]; !ok {
1469 ctx.ModuleErrorf("enabled api scope %q depends on disabled scope %q", scope, extends)
1470 }
1471 }
1472 }
1473 }
1474
1475 return generatedScopes
Paul Duffind1b3a922020-01-22 11:57:20 +00001476}
1477
satayev758968a2021-12-06 11:42:40 +00001478var _ android.ModuleWithMinSdkVersionCheck = (*SdkLibrary)(nil)
1479
satayev8f088b02021-12-06 11:40:46 +00001480func (module *SdkLibrary) CheckMinSdkVersion(ctx android.ModuleContext) {
Jihoon Kanga3a05462024-04-05 00:36:44 +00001481 CheckMinSdkVersion(ctx, &module.Library)
1482}
1483
1484func CheckMinSdkVersion(ctx android.ModuleContext, module *Library) {
Spandan Das8c9ae7e2023-03-03 21:20:36 +00001485 android.CheckMinSdkVersion(ctx, module.MinSdkVersion(ctx), func(c android.ModuleContext, do android.PayloadDepsCallback) {
satayev8f088b02021-12-06 11:40:46 +00001486 ctx.WalkDeps(func(child android.Module, parent android.Module) bool {
1487 isExternal := !module.depIsInSameApex(ctx, child)
1488 if am, ok := child.(android.ApexModule); ok {
1489 if !do(ctx, parent, am, isExternal) {
1490 return false
1491 }
1492 }
1493 return !isExternal
1494 })
1495 })
1496}
1497
Paul Duffineedc5d52020-06-12 17:46:39 +01001498type sdkLibraryComponentTag struct {
1499 blueprint.BaseDependencyTag
1500 name string
1501}
1502
1503// Mark this tag so dependencies that use it are excluded from visibility enforcement.
1504func (t sdkLibraryComponentTag) ExcludeFromVisibilityEnforcement() {}
1505
1506var xmlPermissionsFileTag = sdkLibraryComponentTag{name: "xml-permissions-file"}
Paul Duffine74ac732020-02-06 13:51:46 +00001507
Jiyong Parke3833882020-02-17 17:28:10 +09001508func IsXmlPermissionsFileDepTag(depTag blueprint.DependencyTag) bool {
Paul Duffineedc5d52020-06-12 17:46:39 +01001509 if dt, ok := depTag.(sdkLibraryComponentTag); ok {
Jiyong Parke3833882020-02-17 17:28:10 +09001510 return dt == xmlPermissionsFileTag
1511 }
1512 return false
1513}
1514
Paul Duffineedc5d52020-06-12 17:46:39 +01001515var implLibraryTag = sdkLibraryComponentTag{name: "impl-library"}
Paul Duffin5df79302020-05-16 15:52:12 +01001516
Jeongik Chaaaa6dcd2024-05-22 00:41:28 +09001517var _ android.InstallNeededDependencyTag = sdkLibraryComponentTag{}
1518
Jihoon Kang46d66de2024-05-22 22:42:39 +00001519// To satisfy the CopyDirectlyInAnyApexTag interface. Implementation library of the sdk library
1520// in an apex is considered to be directly in the apex, as if it was listed in java_libs.
1521func (t sdkLibraryComponentTag) CopyDirectlyInAnyApex() {}
1522
1523var _ android.CopyDirectlyInAnyApexTag = implLibraryTag
1524
Jeongik Chaaaa6dcd2024-05-22 00:41:28 +09001525func (t sdkLibraryComponentTag) InstallDepNeeded() bool {
1526 return t.name == "xml-permissions-file" || t.name == "impl-library"
1527}
1528
Paul Duffin44f1d842020-06-26 20:17:02 +01001529// Add the dependencies on the child modules in the component deps mutator.
1530func (module *SdkLibrary) ComponentDepsMutator(ctx android.BottomUpMutatorContext) {
Paul Duffin3375e352020-04-28 10:44:03 +01001531 for _, apiScope := range module.getGeneratedApiScopes(ctx) {
Paul Duffind1b3a922020-01-22 11:57:20 +00001532 // Add dependencies to the stubs library
Spandan Das877f39d2023-03-29 16:19:51 +00001533 stubModuleName := module.stubsLibraryModuleName(apiScope)
Jihoon Kangbd093452023-12-26 19:08:01 +00001534 ctx.AddVariationDependencies(nil, apiScope.everythingStubsTag, stubModuleName)
Jihoon Kang1147b312023-06-08 23:25:57 +00001535
Jihoon Kangbd093452023-12-26 19:08:01 +00001536 exportableStubModuleName := module.exportableStubsLibraryModuleName(apiScope)
1537 ctx.AddVariationDependencies(nil, apiScope.exportableStubsTag, exportableStubModuleName)
Paul Duffind1b3a922020-01-22 11:57:20 +00001538
Paul Duffin15f34ef2020-07-20 18:04:44 +01001539 // Add a dependency on the stubs source in order to access both stubs source and api information.
1540 ctx.AddVariationDependencies(nil, apiScope.stubsSourceAndApiTag, module.stubsSourceModuleName(apiScope))
Paul Duffin958806b2022-05-16 13:10:47 +00001541
1542 if module.compareAgainstLatestApi(apiScope) {
1543 // Add dependencies on the latest finalized version of the API .txt file.
1544 latestApiModuleName := module.latestApiModuleName(apiScope)
1545 ctx.AddDependency(module, apiScope.latestApiModuleTag, latestApiModuleName)
1546
1547 // Add dependencies on the latest finalized version of the remove API .txt file.
1548 latestRemovedApiModuleName := module.latestRemovedApiModuleName(apiScope)
1549 ctx.AddDependency(module, apiScope.latestRemovedApiModuleTag, latestRemovedApiModuleName)
1550 }
Sundong Ahn054b19a2018-10-19 13:46:09 +09001551 }
1552
Paul Duffindfa131e2020-05-15 20:37:11 +01001553 if module.requiresRuntimeImplementationLibrary() {
Paul Duffin5df79302020-05-16 15:52:12 +01001554 // Add dependency to the rule for generating the implementation library.
1555 ctx.AddDependency(module, implLibraryTag, module.implLibraryModuleName())
1556
Paul Duffindfa131e2020-05-15 20:37:11 +01001557 if module.sharedLibrary() {
1558 // Add dependency to the rule for generating the xml permissions file
Paul Duffineedc5d52020-06-12 17:46:39 +01001559 ctx.AddDependency(module, xmlPermissionsFileTag, module.xmlPermissionsModuleName())
Paul Duffindfa131e2020-05-15 20:37:11 +01001560 }
Paul Duffin44f1d842020-06-26 20:17:02 +01001561 }
1562}
Paul Duffine74ac732020-02-06 13:51:46 +00001563
Paul Duffin44f1d842020-06-26 20:17:02 +01001564// Add other dependencies as normal.
1565func (module *SdkLibrary) DepsMutator(ctx android.BottomUpMutatorContext) {
Anton Hanssone77fccc2021-01-20 16:52:41 +00001566 var missingApiModules []string
1567 for _, apiScope := range module.getGeneratedApiScopes(ctx) {
1568 if apiScope.unstable {
1569 continue
1570 }
Paul Duffin958806b2022-05-16 13:10:47 +00001571 if m := module.latestApiModuleName(apiScope); !ctx.OtherModuleExists(m) {
Anton Hanssone77fccc2021-01-20 16:52:41 +00001572 missingApiModules = append(missingApiModules, m)
1573 }
Paul Duffin958806b2022-05-16 13:10:47 +00001574 if m := module.latestRemovedApiModuleName(apiScope); !ctx.OtherModuleExists(m) {
Anton Hanssone77fccc2021-01-20 16:52:41 +00001575 missingApiModules = append(missingApiModules, m)
1576 }
Paul Duffin958806b2022-05-16 13:10:47 +00001577 if m := module.latestIncompatibilitiesModuleName(apiScope); !ctx.OtherModuleExists(m) {
Jaewoong Jung1a97ee02021-03-09 13:25:02 -08001578 missingApiModules = append(missingApiModules, m)
1579 }
Anton Hanssone77fccc2021-01-20 16:52:41 +00001580 }
1581 if len(missingApiModules) != 0 && !module.sdkLibraryProperties.Unsafe_ignore_missing_latest_api {
1582 m := module.Name() + " is missing tracking files for previously released library versions.\n"
1583 m += "You need to do one of the following:\n"
1584 m += "- Add `unsafe_ignore_missing_latest_api: true` to your blueprint (to disable compat tracking)\n"
1585 m += "- Add a set of prebuilt txt files representing the last released version of this library for compat checking.\n"
1586 m += " (the current set of API files can be used as a seed for this compatibility tracking\n"
1587 m += "\n"
1588 m += "The following filegroup modules are missing:\n "
1589 m += strings.Join(missingApiModules, "\n ") + "\n"
1590 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."
1591 ctx.ModuleErrorf(m)
1592 }
Jiyong Parkc678ad32018-04-10 13:07:10 +09001593}
1594
Paul Duffin46dc45a2020-05-14 15:39:10 +01001595func (module *SdkLibrary) OutputFiles(tag string) (android.Paths, error) {
1596 paths, err := module.commonOutputFiles(tag)
Colin Cross4acaea92021-12-10 23:05:02 +00001597 if paths != nil || err != nil {
Paul Duffin46dc45a2020-05-14 15:39:10 +01001598 return paths, err
1599 }
Colin Cross4acaea92021-12-10 23:05:02 +00001600 if module.requiresRuntimeImplementationLibrary() {
Jihoon Kanga3a05462024-04-05 00:36:44 +00001601 return module.implLibraryModule.OutputFiles(tag)
Colin Cross4acaea92021-12-10 23:05:02 +00001602 }
1603 if tag == "" {
1604 return nil, nil
1605 }
1606 return nil, fmt.Errorf("unsupported module reference tag %q", tag)
Paul Duffin46dc45a2020-05-14 15:39:10 +01001607}
1608
Inseob Kimc0907f12019-02-08 21:00:45 +09001609func (module *SdkLibrary) GenerateAndroidBuildActions(ctx android.ModuleContext) {
Spandan Das5ae65ee2024-04-16 22:03:26 +00001610 if disableSourceApexVariant(ctx) {
1611 // Prebuilts are active, do not create the installation rules for the source javalib.
1612 // Even though the source javalib is not used, we need to hide it to prevent duplicate installation rules.
1613 // TODO (b/331665856): Implement a principled solution for this.
1614 module.HideFromMake()
1615 }
satayev8f088b02021-12-06 11:40:46 +00001616
Paul Duffina2ae7e02020-09-11 11:55:00 +01001617 module.generateCommonBuildActions(ctx)
1618
Jihoon Kanga3a05462024-04-05 00:36:44 +00001619 module.stem = proptools.StringDefault(module.overridableProperties.Stem, ctx.ModuleName())
1620
1621 module.provideHiddenAPIPropertyInfo(ctx)
Sundong Ahn054b19a2018-10-19 13:46:09 +09001622
Paul Duffinb97b1572021-04-29 21:50:40 +01001623 // Collate the components exported by this module. All scope specific modules are exported but
1624 // the impl and xml component modules are not.
1625 exportedComponents := map[string]struct{}{}
1626
Sundong Ahn57368eb2018-07-06 11:20:23 +09001627 // Record the paths to the header jars of the library (stubs and impl).
Paul Duffind1b3a922020-01-22 11:57:20 +00001628 // When this java_sdk_library is depended upon from others via "libs" property,
Jiyong Parkc678ad32018-04-10 13:07:10 +09001629 // the recorded paths will be returned depending on the link type of the caller.
1630 ctx.VisitDirectDeps(func(to android.Module) {
Jiyong Parkc678ad32018-04-10 13:07:10 +09001631 tag := ctx.OtherModuleDependencyTag(to)
1632
Paul Duffinc8782502020-04-29 20:45:27 +01001633 // Extract information from any of the scope specific dependencies.
1634 if scopeTag, ok := tag.(scopeDependencyTag); ok {
1635 apiScope := scopeTag.apiScope
Paul Duffin803a9562020-05-20 11:52:25 +01001636 scopePaths := module.getScopePathsCreateIfNeeded(apiScope)
Paul Duffinc8782502020-04-29 20:45:27 +01001637
1638 // Extract information from the dependency. The exact information extracted
1639 // is determined by the nature of the dependency which is determined by the tag.
1640 scopeTag.extractDepInfo(ctx, to, scopePaths)
Paul Duffinb97b1572021-04-29 21:50:40 +01001641
1642 exportedComponents[ctx.OtherModuleName(to)] = struct{}{}
Sundong Ahn20e998b2018-07-24 11:19:26 +09001643 }
Jihoon Kang8479dea2024-04-04 01:19:05 +00001644
1645 if tag == implLibraryTag {
1646 if dep, ok := android.OtherModuleProvider(ctx, to, JavaInfoProvider); ok {
1647 module.implLibraryHeaderJars = append(module.implLibraryHeaderJars, dep.HeaderJars...)
Jihoon Kanga3a05462024-04-05 00:36:44 +00001648 module.implLibraryModule = to.(*Library)
1649 android.SetProvider(ctx, JavaInfoProvider, dep)
Jihoon Kang8479dea2024-04-04 01:19:05 +00001650 }
1651 }
Jiyong Parkc678ad32018-04-10 13:07:10 +09001652 })
Paul Duffinb97b1572021-04-29 21:50:40 +01001653
Jihoon Kanga3a05462024-04-05 00:36:44 +00001654 apexInfo, _ := android.ModuleProvider(ctx, android.ApexInfoProvider)
1655 if !apexInfo.IsForPlatform() {
1656 module.hideApexVariantFromMake = true
1657 }
1658
1659 if module.implLibraryModule != nil {
1660 if ctx.Device() {
1661 module.classesJarPaths = android.Paths{module.implLibraryModule.implementationJarFile}
1662 module.bootDexJarPath = module.implLibraryModule.bootDexJarPath
1663 module.uncompressDexState = module.implLibraryModule.uncompressDexState
1664 module.active = module.implLibraryModule.active
1665 }
1666
1667 module.outputFile = module.implLibraryModule.outputFile
1668 module.dexJarFile = makeDexJarPathFromPath(module.implLibraryModule.dexJarFile.Path())
1669 module.headerJarFile = module.implLibraryModule.headerJarFile
1670 module.implementationAndResourcesJar = module.implLibraryModule.implementationAndResourcesJar
1671 module.builtInstalledForApex = module.implLibraryModule.builtInstalledForApex
1672 module.dexpreopter.configPath = module.implLibraryModule.dexpreopter.configPath
1673 module.dexpreopter.outputProfilePathOnHost = module.implLibraryModule.dexpreopter.outputProfilePathOnHost
1674
Jihoon Kang34155e32024-05-20 19:08:49 +00001675 // Properties required for Library.AndroidMkEntries
1676 module.logtagsSrcs = module.implLibraryModule.logtagsSrcs
1677 module.dexpreopter.builtInstalled = module.implLibraryModule.dexpreopter.builtInstalled
1678 module.jacocoReportClassesFile = module.implLibraryModule.jacocoReportClassesFile
1679 module.dexer.proguardDictionary = module.implLibraryModule.dexer.proguardDictionary
1680 module.dexer.proguardUsageZip = module.implLibraryModule.dexer.proguardUsageZip
1681 module.linter.reports = module.implLibraryModule.linter.reports
1682
Jihoon Kanga3a05462024-04-05 00:36:44 +00001683 if !module.Host() {
1684 module.hostdexInstallFile = module.implLibraryModule.hostdexInstallFile
1685 }
1686
1687 android.SetProvider(ctx, blueprint.SrcsFileProviderKey, blueprint.SrcsFileProviderData{SrcPaths: module.implLibraryModule.uniqueSrcFiles.Strings()})
1688 }
1689
Paul Duffinb97b1572021-04-29 21:50:40 +01001690 // Make the set of components exported by this module available for use elsewhere.
Cole Faust18994c72023-02-28 16:02:16 -08001691 exportedComponentInfo := android.ExportedComponentsInfo{Components: android.SortedKeys(exportedComponents)}
Colin Cross40213022023-12-13 15:19:49 -08001692 android.SetProvider(ctx, android.ExportedComponentsInfoProvider, exportedComponentInfo)
Paul Duffin958806b2022-05-16 13:10:47 +00001693
1694 // Provide additional information for inclusion in an sdk's generated .info file.
1695 additionalSdkInfo := map[string]interface{}{}
1696 additionalSdkInfo["dist_stem"] = module.distStem()
Paul Duffine8409952022-09-22 16:24:46 +01001697 baseModuleName := module.distStem()
Paul Duffin958806b2022-05-16 13:10:47 +00001698 scopes := map[string]interface{}{}
1699 additionalSdkInfo["scopes"] = scopes
1700 for scope, scopePaths := range module.scopePaths {
1701 scopeInfo := map[string]interface{}{}
1702 scopes[scope.name] = scopeInfo
1703 scopeInfo["current_api"] = scope.snapshotRelativeCurrentApiTxtPath(baseModuleName)
1704 scopeInfo["removed_api"] = scope.snapshotRelativeRemovedApiTxtPath(baseModuleName)
Jihoon Kang5623e542024-01-31 23:27:26 +00001705 if p := scopePaths.latestApiPaths; len(p) > 0 {
1706 // The last path in the list is the one that applies to this scope, the
1707 // preceding ones, if any, are for the scope(s) that it extends.
1708 scopeInfo["latest_api"] = p[len(p)-1].String()
Paul Duffin958806b2022-05-16 13:10:47 +00001709 }
Jihoon Kang5623e542024-01-31 23:27:26 +00001710 if p := scopePaths.latestRemovedApiPaths; len(p) > 0 {
1711 // The last path in the list is the one that applies to this scope, the
1712 // preceding ones, if any, are for the scope(s) that it extends.
1713 scopeInfo["latest_removed_api"] = p[len(p)-1].String()
Paul Duffin958806b2022-05-16 13:10:47 +00001714 }
1715 }
Colin Cross40213022023-12-13 15:19:49 -08001716 android.SetProvider(ctx, android.AdditionalSdkInfoProvider, android.AdditionalSdkInfo{additionalSdkInfo})
Jiyong Parkc678ad32018-04-10 13:07:10 +09001717}
1718
Jihoon Kanga3a05462024-04-05 00:36:44 +00001719func (module *SdkLibrary) BuiltInstalledForApex() []dexpreopterInstall {
1720 return module.builtInstalledForApex
1721}
1722
Jiyong Park0b0e1b92019-12-03 13:24:29 +09001723func (module *SdkLibrary) AndroidMkEntries() []android.AndroidMkEntries {
Paul Duffindfa131e2020-05-15 20:37:11 +01001724 if !module.requiresRuntimeImplementationLibrary() {
Paul Duffin43db9be2019-12-30 17:35:49 +00001725 return nil
1726 }
Jiyong Park0b0e1b92019-12-03 13:24:29 +09001727 entriesList := module.Library.AndroidMkEntries()
Jihoon Kanga3a05462024-04-05 00:36:44 +00001728 entries := &entriesList[0]
1729 entries.Required = append(entries.Required, module.implLibraryModuleName())
Yo Chiang07d75072020-06-05 17:43:19 +08001730 if module.sharedLibrary() {
Yo Chiang07d75072020-06-05 17:43:19 +08001731 entries.Required = append(entries.Required, module.xmlPermissionsModuleName())
1732 }
Jiyong Park0b0e1b92019-12-03 13:24:29 +09001733 return entriesList
Jiyong Park82484c02018-04-23 21:41:26 +09001734}
1735
Anton Hansson5fd5d242020-03-27 19:43:19 +00001736// The dist path of the stub artifacts
1737func (module *SdkLibrary) apiDistPath(apiScope *apiScope) string {
Colin Crossf0eace92021-06-02 13:02:23 -07001738 return path.Join("apistubs", module.distGroup(), apiScope.name)
Anton Hansson5fd5d242020-03-27 19:43:19 +00001739}
1740
Paul Duffin12ceb462019-12-24 20:31:31 +00001741// Get the sdk version for use when compiling the stubs library.
Paul Duffin780c5f42020-05-12 15:52:55 +01001742func (module *SdkLibrary) sdkVersionForStubsLibrary(mctx android.EarlyModuleContext, apiScope *apiScope) string {
Paul Duffin87a05a32020-05-12 11:50:28 +01001743 scopeProperties := module.scopeToProperties[apiScope]
1744 if scopeProperties.Sdk_version != nil {
1745 return proptools.String(scopeProperties.Sdk_version)
1746 }
1747
Jiyong Parkf1691d22021-03-29 20:11:58 +09001748 sdkDep := decodeSdkDep(mctx, android.SdkContext(&module.Library))
Paul Duffin12ceb462019-12-24 20:31:31 +00001749 if sdkDep.hasStandardLibs() {
1750 // If building against a standard sdk then use the sdk version appropriate for the scope.
Paul Duffind1b3a922020-01-22 11:57:20 +00001751 return apiScope.sdkVersion
Paul Duffin12ceb462019-12-24 20:31:31 +00001752 } else {
1753 // Otherwise, use no system module.
1754 return "none"
1755 }
1756}
1757
Paul Duffin31310252020-11-20 21:26:20 +00001758func (module *SdkLibrary) distStem() string {
1759 return proptools.StringDefault(module.sdkLibraryProperties.Dist_stem, module.BaseModuleName())
1760}
1761
Colin Cross986b69a2021-06-01 13:13:40 -07001762// distGroup returns the subdirectory of the dist path of the stub artifacts.
1763func (module *SdkLibrary) distGroup() string {
Colin Cross59b92bf2021-06-01 14:07:56 -07001764 return proptools.StringDefault(module.sdkLibraryProperties.Dist_group, "unknown")
Colin Cross986b69a2021-06-01 13:13:40 -07001765}
1766
Paul Duffin958806b2022-05-16 13:10:47 +00001767func latestPrebuiltApiModuleName(name string, apiScope *apiScope) string {
1768 return PrebuiltApiModuleName(name, apiScope.name, "latest")
1769}
1770
Jihoon Kang748a24d2024-03-20 21:29:39 +00001771func latestPrebuiltApiCombinedModuleName(name string, apiScope *apiScope) string {
1772 return PrebuiltApiCombinedModuleName(name, apiScope.name, "latest")
1773}
1774
Paul Duffind1b3a922020-01-22 11:57:20 +00001775func (module *SdkLibrary) latestApiFilegroupName(apiScope *apiScope) string {
Paul Duffin958806b2022-05-16 13:10:47 +00001776 return ":" + module.latestApiModuleName(apiScope)
1777}
1778
1779func (module *SdkLibrary) latestApiModuleName(apiScope *apiScope) string {
Jihoon Kang748a24d2024-03-20 21:29:39 +00001780 return latestPrebuiltApiCombinedModuleName(module.distStem(), apiScope)
Jiyong Park58c518b2018-05-12 22:29:12 +09001781}
Jiyong Parkc678ad32018-04-10 13:07:10 +09001782
Paul Duffind1b3a922020-01-22 11:57:20 +00001783func (module *SdkLibrary) latestRemovedApiFilegroupName(apiScope *apiScope) string {
Paul Duffin958806b2022-05-16 13:10:47 +00001784 return ":" + module.latestRemovedApiModuleName(apiScope)
1785}
1786
1787func (module *SdkLibrary) latestRemovedApiModuleName(apiScope *apiScope) string {
Jihoon Kang748a24d2024-03-20 21:29:39 +00001788 return latestPrebuiltApiCombinedModuleName(module.distStem()+"-removed", apiScope)
Jiyong Parkc678ad32018-04-10 13:07:10 +09001789}
1790
Jaewoong Jung1a97ee02021-03-09 13:25:02 -08001791func (module *SdkLibrary) latestIncompatibilitiesFilegroupName(apiScope *apiScope) string {
Paul Duffin958806b2022-05-16 13:10:47 +00001792 return ":" + module.latestIncompatibilitiesModuleName(apiScope)
1793}
1794
1795func (module *SdkLibrary) latestIncompatibilitiesModuleName(apiScope *apiScope) string {
1796 return latestPrebuiltApiModuleName(module.distStem()+"-incompatibilities", apiScope)
Jaewoong Jung1a97ee02021-03-09 13:25:02 -08001797}
1798
Jihoon Kang1c92c3e2023-03-23 17:44:51 +00001799func (module *SdkLibrary) contributesToApiSurface(c android.Config) bool {
1800 _, exists := c.GetApiLibraries()[module.Name()]
1801 return exists
1802}
1803
Jihoon Kang0c705a42023-08-02 06:44:57 +00001804// The listed modules are the special java_sdk_libraries where apiScope.kind do not match the
1805// api surface that the module contribute to. For example, the public droidstubs and java_library
1806// do not contribute to the public api surface, but contributes to the core platform api surface.
1807// This method returns the full api surface stub lib that
1808// the generated java_api_library should depend on.
1809func (module *SdkLibrary) alternativeFullApiSurfaceStubLib() string {
1810 if val, ok := apiLibraryAdditionalProperties[module.Name()]; ok {
1811 return val.FullApiSurfaceStubLib
1812 }
1813 return ""
1814}
1815
1816// The listed modules' stubs contents do not match the corresponding txt files,
1817// but require additional api contributions to generate the full stubs.
1818// This method returns the name of the additional api contribution module
1819// for corresponding sdk_library modules.
1820func (module *SdkLibrary) apiLibraryAdditionalApiContribution() string {
1821 if val, ok := apiLibraryAdditionalProperties[module.Name()]; ok {
1822 return val.AdditionalApiContribution
1823 }
1824 return ""
1825}
1826
Anton Hansson944e77d2020-08-19 11:40:22 +01001827func childModuleVisibility(childVisibility []string) []string {
1828 if childVisibility == nil {
1829 // No child visibility set. The child will use the visibility of the sdk_library.
1830 return nil
1831 }
1832
1833 // Prepend an override to ignore the sdk_library's visibility, and rely on the child visibility.
1834 var visibility []string
1835 visibility = append(visibility, "//visibility:override")
1836 visibility = append(visibility, childVisibility...)
1837 return visibility
1838}
1839
Paul Duffin5df79302020-05-16 15:52:12 +01001840// Creates the implementation java library
1841func (module *SdkLibrary) createImplLibrary(mctx android.DefaultableHookContext) {
Anton Hansson944e77d2020-08-19 11:40:22 +01001842 visibility := childModuleVisibility(module.sdkLibraryProperties.Impl_library_visibility)
1843
Paul Duffin5df79302020-05-16 15:52:12 +01001844 props := struct {
Paul Duffin77590a82022-04-28 14:13:30 +00001845 Name *string
1846 Visibility []string
Paul Duffin77590a82022-04-28 14:13:30 +00001847 Libs []string
1848 Static_libs []string
1849 Apex_available []string
Jihoon Kanga3a05462024-04-05 00:36:44 +00001850 Stem *string
Paul Duffin5df79302020-05-16 15:52:12 +01001851 }{
1852 Name: proptools.StringPtr(module.implLibraryModuleName()),
Anton Hansson944e77d2020-08-19 11:40:22 +01001853 Visibility: visibility,
Jihoon Kanga3a05462024-04-05 00:36:44 +00001854
1855 Libs: append(module.properties.Libs, module.sdkLibraryProperties.Impl_only_libs...),
1856
1857 Static_libs: append(module.properties.Static_libs, module.sdkLibraryProperties.Impl_only_static_libs...),
Paul Duffin77590a82022-04-28 14:13:30 +00001858 // Pass the apex_available settings down so that the impl library can be statically
1859 // embedded within a library that is added to an APEX. Needed for updatable-media.
1860 Apex_available: module.ApexAvailable(),
Jihoon Kanga3a05462024-04-05 00:36:44 +00001861
1862 Stem: proptools.StringPtr(module.Name()),
Paul Duffin5df79302020-05-16 15:52:12 +01001863 }
1864
1865 properties := []interface{}{
1866 &module.properties,
1867 &module.protoProperties,
1868 &module.deviceProperties,
Liz Kammera7a64f32020-07-09 15:16:41 -07001869 &module.dexProperties,
Paul Duffin5df79302020-05-16 15:52:12 +01001870 &module.dexpreoptProperties,
Colin Cross014489c2020-06-02 20:09:13 -07001871 &module.linter.properties,
Spandan Dasb9c58352024-05-13 18:29:45 +00001872 &module.overridableProperties,
Paul Duffin5df79302020-05-16 15:52:12 +01001873 &props,
1874 module.sdkComponentPropertiesForChildLibrary(),
1875 }
1876 mctx.CreateModule(LibraryFactory, properties...)
1877}
1878
Jihoon Kangfa4a90d2023-12-20 02:53:38 +00001879type libraryProperties struct {
1880 Name *string
1881 Visibility []string
1882 Srcs []string
1883 Installable *bool
1884 Sdk_version *string
1885 System_modules *string
1886 Patch_module *string
1887 Libs []string
1888 Static_libs []string
1889 Compile_dex *bool
1890 Java_version *string
1891 Openjdk9 struct {
1892 Srcs []string
1893 Javacflags []string
1894 }
1895 Dist struct {
1896 Targets []string
1897 Dest *string
1898 Dir *string
1899 Tag *string
1900 }
Jihoon Kangfe914ed2024-02-12 22:49:21 +00001901 Is_stubs_module *bool
Jihoon Kangfa4a90d2023-12-20 02:53:38 +00001902}
Jiyong Parkc678ad32018-04-10 13:07:10 +09001903
Jihoon Kangfa4a90d2023-12-20 02:53:38 +00001904func (module *SdkLibrary) stubsLibraryProps(mctx android.DefaultableHookContext, apiScope *apiScope) libraryProperties {
1905 props := libraryProperties{}
Jihoon Kang786df932023-09-07 01:18:31 +00001906 props.Visibility = []string{"//visibility:override", "//visibility:private"}
Jiyong Parkc678ad32018-04-10 13:07:10 +09001907 // sources are generated from the droiddoc
Paul Duffin12ceb462019-12-24 20:31:31 +00001908 sdkVersion := module.sdkVersionForStubsLibrary(mctx, apiScope)
Paul Duffin52d398a2019-06-11 12:31:14 +01001909 props.Sdk_version = proptools.StringPtr(sdkVersion)
Paul Duffina18abc22020-05-16 18:54:24 +01001910 props.System_modules = module.deviceProperties.System_modules
1911 props.Patch_module = module.properties.Patch_module
Paul Duffin367ab912019-12-23 19:40:36 +00001912 props.Installable = proptools.BoolPtr(false)
Sundong Ahn054b19a2018-10-19 13:46:09 +09001913 props.Libs = module.sdkLibraryProperties.Stub_only_libs
Mark White9421c4c2023-08-10 00:07:03 +00001914 props.Libs = append(props.Libs, module.scopeToProperties[apiScope].Libs...)
Anton Hanssondae54cd2021-04-21 16:30:10 +01001915 props.Static_libs = module.sdkLibraryProperties.Stub_only_static_libs
Paul Duffine22c2ab2020-05-20 19:35:27 +01001916 // The stub-annotations library contains special versions of the annotations
1917 // with CLASS retention policy, so that they're kept.
1918 if proptools.Bool(module.sdkLibraryProperties.Annotations_enabled) {
1919 props.Libs = append(props.Libs, "stub-annotations")
1920 }
Paul Duffina18abc22020-05-16 18:54:24 +01001921 props.Openjdk9.Srcs = module.properties.Openjdk9.Srcs
1922 props.Openjdk9.Javacflags = module.properties.Openjdk9.Javacflags
Anton Hansson83509b52020-05-21 09:21:57 +01001923 // We compile the stubs for 1.8 in line with the main android.jar stubs, and potential
1924 // interop with older developer tools that don't support 1.9.
1925 props.Java_version = proptools.StringPtr("1.8")
Jihoon Kangfe914ed2024-02-12 22:49:21 +00001926 props.Is_stubs_module = proptools.BoolPtr(true)
Paul Duffinf4600f62021-05-13 22:34:45 +01001927
Jihoon Kangfa4a90d2023-12-20 02:53:38 +00001928 return props
1929}
1930
1931// Creates a static java library that has API stubs
1932func (module *SdkLibrary) createStubsLibrary(mctx android.DefaultableHookContext, apiScope *apiScope) {
1933
1934 props := module.stubsLibraryProps(mctx, apiScope)
1935 props.Name = proptools.StringPtr(module.sourceStubsLibraryModuleName(apiScope))
1936 props.Srcs = []string{":" + module.stubsSourceModuleName(apiScope)}
1937
1938 mctx.CreateModule(LibraryFactory, &props, module.sdkComponentPropertiesForChildLibrary())
1939}
1940
1941// Create a static java library that compiles the "exportable" stubs
1942func (module *SdkLibrary) createExportableStubsLibrary(mctx android.DefaultableHookContext, apiScope *apiScope) {
1943 props := module.stubsLibraryProps(mctx, apiScope)
1944 props.Name = proptools.StringPtr(module.exportableSourceStubsLibraryModuleName(apiScope))
1945 props.Srcs = []string{":" + module.stubsSourceModuleName(apiScope) + "{.exportable}"}
1946
Paul Duffin859fe962020-05-15 10:20:31 +01001947 mctx.CreateModule(LibraryFactory, &props, module.sdkComponentPropertiesForChildLibrary())
Jiyong Parkc678ad32018-04-10 13:07:10 +09001948}
1949
Paul Duffin6d0886e2020-04-07 18:49:53 +01001950// Creates a droidstubs module that creates stubs source files from the given full source
Paul Duffinc8782502020-04-29 20:45:27 +01001951// files and also updates and checks the API specification files.
Paul Duffin15f34ef2020-07-20 18:04:44 +01001952func (module *SdkLibrary) createStubsSourcesAndApi(mctx android.DefaultableHookContext, apiScope *apiScope, name string, scopeSpecificDroidstubsArgs []string) {
Jiyong Parkc678ad32018-04-10 13:07:10 +09001953 props := struct {
Sundong Ahn054b19a2018-10-19 13:46:09 +09001954 Name *string
Paul Duffin4911a892020-04-29 23:35:13 +01001955 Visibility []string
Sundong Ahn054b19a2018-10-19 13:46:09 +09001956 Srcs []string
1957 Installable *bool
Paul Duffin52d398a2019-06-11 12:31:14 +01001958 Sdk_version *string
Jihoon Kang3198f3c2023-01-26 08:08:52 +00001959 Api_surface *string
Paul Duffin12ceb462019-12-24 20:31:31 +00001960 System_modules *string
Sundong Ahn054b19a2018-10-19 13:46:09 +09001961 Libs []string
Paul Duffin6877e6d2020-09-25 19:59:14 +01001962 Output_javadoc_comments *bool
Paul Duffin11512472019-02-11 15:55:17 +00001963 Arg_files []string
Sundong Ahn054b19a2018-10-19 13:46:09 +09001964 Args *string
Sundong Ahn054b19a2018-10-19 13:46:09 +09001965 Java_version *string
Paul Duffine22c2ab2020-05-20 19:35:27 +01001966 Annotations_enabled *bool
Sundong Ahn054b19a2018-10-19 13:46:09 +09001967 Merge_annotations_dirs []string
1968 Merge_inclusion_annotations_dirs []string
Paul Duffin0ff08bd2020-04-29 13:30:54 +01001969 Generate_stubs *bool
Anton Hanssone87b03d2020-12-21 15:29:34 +00001970 Previous_api *string
Jihoon Kang6592e872023-12-19 01:13:16 +00001971 Aconfig_declarations []string
Sundong Ahn054b19a2018-10-19 13:46:09 +09001972 Check_api struct {
Anton Hanssone6056152020-12-31 10:37:27 +00001973 Current ApiToCheck
1974 Last_released ApiToCheck
Paul Duffin160fe412020-05-10 19:32:20 +01001975
1976 Api_lint struct {
1977 Enabled *bool
1978 New_since *string
1979 Baseline_file *string
1980 }
Jiyong Park58c518b2018-05-12 22:29:12 +09001981 }
Sundong Ahn1b92c822018-05-29 11:35:17 +09001982 Aidl struct {
1983 Include_dirs []string
1984 Local_include_dirs []string
1985 }
Paul Duffin040e9062020-11-23 17:41:36 +00001986 Dists []android.Dist
Jiyong Parkc678ad32018-04-10 13:07:10 +09001987 }{}
1988
Paul Duffin7b78b4d2020-04-28 14:08:32 +01001989 // The stubs source processing uses the same compile time classpath when extracting the
1990 // API from the implementation library as it does when compiling it. i.e. the same
1991 // * sdk version
1992 // * system_modules
1993 // * libs (static_libs/libs)
Paul Duffin250e6192019-06-07 10:44:37 +01001994
Paul Duffin0ff08bd2020-04-29 13:30:54 +01001995 props.Name = proptools.StringPtr(name)
Anton Hansson944e77d2020-08-19 11:40:22 +01001996 props.Visibility = childModuleVisibility(module.sdkLibraryProperties.Stubs_source_visibility)
Paul Duffina18abc22020-05-16 18:54:24 +01001997 props.Srcs = append(props.Srcs, module.properties.Srcs...)
Anton Hanssonf8ea3722021-09-16 14:24:13 +01001998 props.Srcs = append(props.Srcs, module.sdkLibraryProperties.Api_srcs...)
Paul Duffina18abc22020-05-16 18:54:24 +01001999 props.Sdk_version = module.deviceProperties.Sdk_version
Jihoon Kang3198f3c2023-01-26 08:08:52 +00002000 props.Api_surface = &apiScope.name
Paul Duffina18abc22020-05-16 18:54:24 +01002001 props.System_modules = module.deviceProperties.System_modules
Jiyong Parkc678ad32018-04-10 13:07:10 +09002002 props.Installable = proptools.BoolPtr(false)
Sundong Ahne6f0b052018-06-05 16:46:14 +09002003 // A droiddoc module has only one Libs property and doesn't distinguish between
2004 // shared libs and static libs. So we need to add both of these libs to Libs property.
Paul Duffina18abc22020-05-16 18:54:24 +01002005 props.Libs = module.properties.Libs
2006 props.Libs = append(props.Libs, module.properties.Static_libs...)
Nikita Ioffed732da72022-11-21 12:38:25 +00002007 props.Libs = append(props.Libs, module.sdkLibraryProperties.Stub_only_libs...)
Mark White9421c4c2023-08-10 00:07:03 +00002008 props.Libs = append(props.Libs, module.scopeToProperties[apiScope].Libs...)
Paul Duffina18abc22020-05-16 18:54:24 +01002009 props.Aidl.Include_dirs = module.deviceProperties.Aidl.Include_dirs
2010 props.Aidl.Local_include_dirs = module.deviceProperties.Aidl.Local_include_dirs
2011 props.Java_version = module.properties.Java_version
Jiyong Parkc678ad32018-04-10 13:07:10 +09002012
Paul Duffine22c2ab2020-05-20 19:35:27 +01002013 props.Annotations_enabled = module.sdkLibraryProperties.Annotations_enabled
Sundong Ahn054b19a2018-10-19 13:46:09 +09002014 props.Merge_annotations_dirs = module.sdkLibraryProperties.Merge_annotations_dirs
2015 props.Merge_inclusion_annotations_dirs = module.sdkLibraryProperties.Merge_inclusion_annotations_dirs
Jihoon Kang6592e872023-12-19 01:13:16 +00002016 props.Aconfig_declarations = module.sdkLibraryProperties.Aconfig_declarations
Sundong Ahn054b19a2018-10-19 13:46:09 +09002017
Paul Duffin6d0886e2020-04-07 18:49:53 +01002018 droidstubsArgs := []string{}
Paul Duffin235ffff2019-12-24 10:41:30 +00002019 if len(module.sdkLibraryProperties.Api_packages) != 0 {
Paul Duffin6d0886e2020-04-07 18:49:53 +01002020 droidstubsArgs = append(droidstubsArgs, "--stub-packages "+strings.Join(module.sdkLibraryProperties.Api_packages, ":"))
Paul Duffin235ffff2019-12-24 10:41:30 +00002021 }
2022 if len(module.sdkLibraryProperties.Hidden_api_packages) != 0 {
Paul Duffin6d0886e2020-04-07 18:49:53 +01002023 droidstubsArgs = append(droidstubsArgs,
Paul Duffin235ffff2019-12-24 10:41:30 +00002024 android.JoinWithPrefix(module.sdkLibraryProperties.Hidden_api_packages, " --hide-package "))
2025 }
Paul Duffin6d0886e2020-04-07 18:49:53 +01002026 droidstubsArgs = append(droidstubsArgs, module.sdkLibraryProperties.Droiddoc_options...)
Anton Hanssonfd1c0d22023-11-02 15:18:09 +00002027 disabledWarnings := []string{"HiddenSuperclass"}
2028 if proptools.BoolDefault(module.sdkLibraryProperties.Api_lint.Legacy_errors_allowed, true) {
2029 disabledWarnings = append(disabledWarnings,
2030 "BroadcastBehavior",
2031 "DeprecationMismatch",
2032 "MissingPermission",
2033 "SdkConstant",
2034 "Todo",
2035 )
Paul Duffin235ffff2019-12-24 10:41:30 +00002036 }
Paul Duffin6d0886e2020-04-07 18:49:53 +01002037 droidstubsArgs = append(droidstubsArgs, android.JoinWithPrefix(disabledWarnings, "--hide "))
Sundong Ahnfb2721f2018-09-17 13:23:09 +09002038
Paul Duffin6877e6d2020-09-25 19:59:14 +01002039 // Output Javadoc comments for public scope.
2040 if apiScope == apiScopePublic {
2041 props.Output_javadoc_comments = proptools.BoolPtr(true)
2042 }
2043
Paul Duffin1fb487d2020-04-07 18:50:10 +01002044 // Add in scope specific arguments.
Paul Duffin0ff08bd2020-04-29 13:30:54 +01002045 droidstubsArgs = append(droidstubsArgs, scopeSpecificDroidstubsArgs...)
Paul Duffin11512472019-02-11 15:55:17 +00002046 props.Arg_files = module.sdkLibraryProperties.Droiddoc_option_files
Paul Duffin6d0886e2020-04-07 18:49:53 +01002047 props.Args = proptools.StringPtr(strings.Join(droidstubsArgs, " "))
Jiyong Parkc678ad32018-04-10 13:07:10 +09002048
Paul Duffin15f34ef2020-07-20 18:04:44 +01002049 // List of APIs identified from the provided source files are created. They are later
2050 // compared against to the not-yet-released (a.k.a current) list of APIs and to the
2051 // last-released (a.k.a numbered) list of API.
2052 currentApiFileName := apiScope.apiFilePrefix + "current.txt"
2053 removedApiFileName := apiScope.apiFilePrefix + "removed.txt"
2054 apiDir := module.getApiDir()
2055 currentApiFileName = path.Join(apiDir, currentApiFileName)
2056 removedApiFileName = path.Join(apiDir, removedApiFileName)
Jiyong Parkc678ad32018-04-10 13:07:10 +09002057
Paul Duffin15f34ef2020-07-20 18:04:44 +01002058 // check against the not-yet-release API
2059 props.Check_api.Current.Api_file = proptools.StringPtr(currentApiFileName)
2060 props.Check_api.Current.Removed_api_file = proptools.StringPtr(removedApiFileName)
Jiyong Park58c518b2018-05-12 22:29:12 +09002061
Paul Duffin958806b2022-05-16 13:10:47 +00002062 if module.compareAgainstLatestApi(apiScope) {
Paul Duffin15f34ef2020-07-20 18:04:44 +01002063 // check against the latest released API
2064 latestApiFilegroupName := proptools.StringPtr(module.latestApiFilegroupName(apiScope))
Anton Hanssone87b03d2020-12-21 15:29:34 +00002065 props.Previous_api = latestApiFilegroupName
Paul Duffin15f34ef2020-07-20 18:04:44 +01002066 props.Check_api.Last_released.Api_file = latestApiFilegroupName
2067 props.Check_api.Last_released.Removed_api_file = proptools.StringPtr(
2068 module.latestRemovedApiFilegroupName(apiScope))
Jaewoong Jung1a97ee02021-03-09 13:25:02 -08002069 props.Check_api.Last_released.Baseline_file = proptools.StringPtr(
2070 module.latestIncompatibilitiesFilegroupName(apiScope))
Paul Duffin160fe412020-05-10 19:32:20 +01002071
Paul Duffin15f34ef2020-07-20 18:04:44 +01002072 if proptools.Bool(module.sdkLibraryProperties.Api_lint.Enabled) {
2073 // Enable api lint.
2074 props.Check_api.Api_lint.Enabled = proptools.BoolPtr(true)
2075 props.Check_api.Api_lint.New_since = latestApiFilegroupName
Paul Duffin160fe412020-05-10 19:32:20 +01002076
Paul Duffin15f34ef2020-07-20 18:04:44 +01002077 // If it exists then pass a lint-baseline.txt through to droidstubs.
2078 baselinePath := path.Join(apiDir, apiScope.apiFilePrefix+"lint-baseline.txt")
2079 baselinePathRelativeToRoot := path.Join(mctx.ModuleDir(), baselinePath)
2080 paths, err := mctx.GlobWithDeps(baselinePathRelativeToRoot, nil)
2081 if err != nil {
2082 mctx.ModuleErrorf("error checking for presence of %s: %s", baselinePathRelativeToRoot, err)
2083 }
2084 if len(paths) == 1 {
2085 props.Check_api.Api_lint.Baseline_file = proptools.StringPtr(baselinePath)
2086 } else if len(paths) != 0 {
2087 mctx.ModuleErrorf("error checking for presence of %s: expected one path, found: %v", baselinePathRelativeToRoot, paths)
Paul Duffin160fe412020-05-10 19:32:20 +01002088 }
2089 }
Paul Duffin15f34ef2020-07-20 18:04:44 +01002090 }
Jiyong Park58c518b2018-05-12 22:29:12 +09002091
Paul Duffin15f34ef2020-07-20 18:04:44 +01002092 if !Bool(module.sdkLibraryProperties.No_dist) {
Paul Duffin040e9062020-11-23 17:41:36 +00002093 // Dist the api txt and removed api txt artifacts for sdk builds.
2094 distDir := proptools.StringPtr(path.Join(module.apiDistPath(apiScope), "api"))
Jihoon Kang02168052024-03-20 00:44:54 +00002095 stubsTypeTagPrefix := ""
2096 if mctx.Config().ReleaseHiddenApiExportableStubs() {
2097 stubsTypeTagPrefix = ".exportable"
2098 }
Paul Duffin040e9062020-11-23 17:41:36 +00002099 for _, p := range []struct {
2100 tag string
2101 pattern string
2102 }{
Jihoon Kangd1799f62024-02-20 23:01:38 +00002103 // "exportable" api files are copied to the dist directory instead of the
Jihoon Kang02168052024-03-20 00:44:54 +00002104 // "everything" api files when "RELEASE_HIDDEN_API_EXPORTABLE_STUBS" build flag
2105 // is set. Otherwise, the "everything" api files are copied to the dist directory.
2106 {tag: "%s.api.txt", pattern: "%s.txt"},
2107 {tag: "%s.removed-api.txt", pattern: "%s-removed.txt"},
Paul Duffin040e9062020-11-23 17:41:36 +00002108 } {
2109 props.Dists = append(props.Dists, android.Dist{
2110 Targets: []string{"sdk", "win_sdk"},
2111 Dir: distDir,
2112 Dest: proptools.StringPtr(fmt.Sprintf(p.pattern, module.distStem())),
Jihoon Kang02168052024-03-20 00:44:54 +00002113 Tag: proptools.StringPtr(fmt.Sprintf(p.tag, stubsTypeTagPrefix)),
Paul Duffin040e9062020-11-23 17:41:36 +00002114 })
2115 }
Anton Hansson5fd5d242020-03-27 19:43:19 +00002116 }
2117
Spandan Das2cc80ba2023-10-27 17:21:52 +00002118 mctx.CreateModule(DroidstubsFactory, &props, module.sdkComponentPropertiesForChildLibrary()).(*Droidstubs).CallHookIfAvailable(mctx)
Jiyong Parkc678ad32018-04-10 13:07:10 +09002119}
2120
Jihoon Kang0c705a42023-08-02 06:44:57 +00002121func (module *SdkLibrary) createApiLibrary(mctx android.DefaultableHookContext, apiScope *apiScope, alternativeFullApiSurfaceStub string) {
Jihoon Kang1c92c3e2023-03-23 17:44:51 +00002122 props := struct {
Jihoon Kangca198c22023-06-22 23:13:51 +00002123 Name *string
2124 Visibility []string
2125 Api_contributions []string
2126 Libs []string
2127 Static_libs []string
2128 Full_api_surface_stub *string
Jihoon Kang4ec24872023-10-05 17:26:09 +00002129 System_modules *string
Jihoon Kang063ec002023-06-28 01:16:23 +00002130 Enable_validation *bool
Jihoon Kang5d701272024-02-15 21:53:49 +00002131 Stubs_type *string
Jihoon Kang1c92c3e2023-03-23 17:44:51 +00002132 }{}
2133
2134 props.Name = proptools.StringPtr(module.apiLibraryModuleName(apiScope))
Jihoon Kang786df932023-09-07 01:18:31 +00002135 props.Visibility = []string{"//visibility:override", "//visibility:private"}
Jihoon Kang1c92c3e2023-03-23 17:44:51 +00002136
2137 apiContributions := []string{}
2138
2139 // Api surfaces are not independent of each other, but have subset relationships,
2140 // and so does the api files. To generate from-text stubs for api surfaces other than public,
2141 // all subset api domains' api_contriubtions must be added as well.
2142 scope := apiScope
2143 for scope != nil {
2144 apiContributions = append(apiContributions, module.stubsSourceModuleName(scope)+".api.contribution")
2145 scope = scope.extends
2146 }
Jihoon Kang0c705a42023-08-02 06:44:57 +00002147 if apiScope == apiScopePublic {
2148 additionalApiContribution := module.apiLibraryAdditionalApiContribution()
2149 if additionalApiContribution != "" {
2150 apiContributions = append(apiContributions, additionalApiContribution)
2151 }
2152 }
Jihoon Kang1c92c3e2023-03-23 17:44:51 +00002153
2154 props.Api_contributions = apiContributions
2155 props.Libs = module.properties.Libs
2156 props.Libs = append(props.Libs, module.sdkLibraryProperties.Stub_only_libs...)
Mark White9421c4c2023-08-10 00:07:03 +00002157 props.Libs = append(props.Libs, module.scopeToProperties[apiScope].Libs...)
Jihoon Kang1c92c3e2023-03-23 17:44:51 +00002158 props.Libs = append(props.Libs, "stub-annotations")
2159 props.Static_libs = module.sdkLibraryProperties.Stub_only_static_libs
Jihoon Kange7ee2562023-07-25 05:51:46 +00002160 props.Full_api_surface_stub = proptools.StringPtr(apiScope.kind.DefaultJavaLibraryName())
Jihoon Kang0c705a42023-08-02 06:44:57 +00002161 if alternativeFullApiSurfaceStub != "" {
2162 props.Full_api_surface_stub = proptools.StringPtr(alternativeFullApiSurfaceStub)
2163 }
Jihoon Kang1c92c3e2023-03-23 17:44:51 +00002164
2165 // android_module_lib_stubs_current.from-text only comprises api contributions from art, conscrypt and i18n.
2166 // Thus, replace with android_module_lib_stubs_current_full.from-text, which comprises every api domains.
2167 if apiScope.kind == android.SdkModule {
Jihoon Kangca198c22023-06-22 23:13:51 +00002168 props.Full_api_surface_stub = proptools.StringPtr(apiScope.kind.DefaultJavaLibraryName() + "_full.from-text")
Jihoon Kang1c92c3e2023-03-23 17:44:51 +00002169 }
2170
Jihoon Kangd30ac8a2023-10-09 18:00:17 +00002171 // java_sdk_library modules that set sdk_version as none does not depend on other api
2172 // domains. Therefore, java_api_library created from such modules should not depend on
2173 // full_api_surface_stubs but create and compile stubs by the java_api_library module
2174 // itself.
2175 if module.SdkVersion(mctx).Kind == android.SdkNone {
2176 props.Full_api_surface_stub = nil
2177 }
2178
Jihoon Kang4ec24872023-10-05 17:26:09 +00002179 props.System_modules = module.deviceProperties.System_modules
Jihoon Kang063ec002023-06-28 01:16:23 +00002180 props.Enable_validation = proptools.BoolPtr(true)
Jihoon Kang5d701272024-02-15 21:53:49 +00002181 props.Stubs_type = proptools.StringPtr("everything")
Jihoon Kang4ec24872023-10-05 17:26:09 +00002182
Spandan Das2cc80ba2023-10-27 17:21:52 +00002183 mctx.CreateModule(ApiLibraryFactory, &props, module.sdkComponentPropertiesForChildLibrary())
Jihoon Kang1c92c3e2023-03-23 17:44:51 +00002184}
2185
Jihoon Kang02168052024-03-20 00:44:54 +00002186func (module *SdkLibrary) topLevelStubsLibraryProps(mctx android.DefaultableHookContext, apiScope *apiScope, doDist bool) libraryProperties {
Jihoon Kangfa4a90d2023-12-20 02:53:38 +00002187 props := libraryProperties{}
2188
Jihoon Kang1147b312023-06-08 23:25:57 +00002189 props.Visibility = childModuleVisibility(module.sdkLibraryProperties.Stubs_library_visibility)
2190 sdkVersion := module.sdkVersionForStubsLibrary(mctx, apiScope)
2191 props.Sdk_version = proptools.StringPtr(sdkVersion)
2192
Jihoon Kang1147b312023-06-08 23:25:57 +00002193 props.System_modules = module.deviceProperties.System_modules
2194
Jihoon Kang1147b312023-06-08 23:25:57 +00002195 // The imports need to be compiled to dex if the java_sdk_library requests it.
2196 compileDex := module.dexProperties.Compile_dex
2197 if module.stubLibrariesCompiledForDex() {
2198 compileDex = proptools.BoolPtr(true)
2199 }
2200 props.Compile_dex = compileDex
2201
Jihoon Kang02168052024-03-20 00:44:54 +00002202 if !Bool(module.sdkLibraryProperties.No_dist) && doDist {
2203 props.Dist.Targets = []string{"sdk", "win_sdk"}
2204 props.Dist.Dest = proptools.StringPtr(fmt.Sprintf("%v.jar", module.distStem()))
2205 props.Dist.Dir = proptools.StringPtr(module.apiDistPath(apiScope))
2206 props.Dist.Tag = proptools.StringPtr(".jar")
2207 }
2208
Jihoon Kangfa4a90d2023-12-20 02:53:38 +00002209 return props
2210}
2211
2212func (module *SdkLibrary) createTopLevelStubsLibrary(
2213 mctx android.DefaultableHookContext, apiScope *apiScope, contributesToApiSurface bool) {
2214
Jihoon Kang02168052024-03-20 00:44:54 +00002215 // Dist the "everything" stubs when the RELEASE_HIDDEN_API_EXPORTABLE_STUBS build flag is false
2216 doDist := !mctx.Config().ReleaseHiddenApiExportableStubs()
2217 props := module.topLevelStubsLibraryProps(mctx, apiScope, doDist)
Jihoon Kangfa4a90d2023-12-20 02:53:38 +00002218 props.Name = proptools.StringPtr(module.stubsLibraryModuleName(apiScope))
2219
2220 // Add the stub compiling java_library/java_api_library as static lib based on build config
2221 staticLib := module.sourceStubsLibraryModuleName(apiScope)
2222 if mctx.Config().BuildFromTextStub() && contributesToApiSurface {
2223 staticLib = module.apiLibraryModuleName(apiScope)
2224 }
2225 props.Static_libs = append(props.Static_libs, staticLib)
2226
2227 mctx.CreateModule(LibraryFactory, &props, module.sdkComponentPropertiesForChildLibrary())
2228}
2229
2230func (module *SdkLibrary) createTopLevelExportableStubsLibrary(
2231 mctx android.DefaultableHookContext, apiScope *apiScope) {
2232
Jihoon Kang02168052024-03-20 00:44:54 +00002233 // Dist the "exportable" stubs when the RELEASE_HIDDEN_API_EXPORTABLE_STUBS build flag is true
2234 doDist := mctx.Config().ReleaseHiddenApiExportableStubs()
2235 props := module.topLevelStubsLibraryProps(mctx, apiScope, doDist)
Jihoon Kangfa4a90d2023-12-20 02:53:38 +00002236 props.Name = proptools.StringPtr(module.exportableStubsLibraryModuleName(apiScope))
2237
Jihoon Kangfa4a90d2023-12-20 02:53:38 +00002238 staticLib := module.exportableSourceStubsLibraryModuleName(apiScope)
2239 props.Static_libs = append(props.Static_libs, staticLib)
2240
Jihoon Kang1147b312023-06-08 23:25:57 +00002241 mctx.CreateModule(LibraryFactory, &props, module.sdkComponentPropertiesForChildLibrary())
2242}
2243
Paul Duffin958806b2022-05-16 13:10:47 +00002244func (module *SdkLibrary) compareAgainstLatestApi(apiScope *apiScope) bool {
2245 return !(apiScope.unstable || module.sdkLibraryProperties.Unsafe_ignore_missing_latest_api)
2246}
2247
Paul Duffinea8f8082021-06-24 13:25:57 +01002248// Implements android.ApexModule
Jooyung Han5e9013b2020-03-10 06:23:13 +09002249func (module *SdkLibrary) DepIsInSameApex(mctx android.BaseModuleContext, dep android.Module) bool {
2250 depTag := mctx.OtherModuleDependencyTag(dep)
2251 if depTag == xmlPermissionsFileTag {
2252 return true
2253 }
Jihoon Kanga3a05462024-04-05 00:36:44 +00002254 if dep.Name() == module.implLibraryModuleName() {
2255 return true
2256 }
Jooyung Han5e9013b2020-03-10 06:23:13 +09002257 return module.Library.DepIsInSameApex(mctx, dep)
2258}
2259
Paul Duffinea8f8082021-06-24 13:25:57 +01002260// Implements android.ApexModule
2261func (module *SdkLibrary) UniqueApexVariations() bool {
2262 return module.uniqueApexVariations()
2263}
2264
Jihoon Kang80456fd2023-11-15 19:22:14 +00002265func (module *SdkLibrary) ContributeToApi() bool {
2266 return proptools.BoolDefault(module.sdkLibraryProperties.Contribute_to_android_api, false)
2267}
2268
Jiyong Parkc678ad32018-04-10 13:07:10 +09002269// Creates the xml file that publicizes the runtime library
Paul Duffinf0229202020-04-29 16:47:28 +01002270func (module *SdkLibrary) createXmlFile(mctx android.DefaultableHookContext) {
Spandan Das8c9ae7e2023-03-03 21:20:36 +00002271 moduleMinApiLevel := module.Library.MinSdkVersion(mctx)
Pedro Loureiroc3621422021-09-28 15:40:23 +00002272 var moduleMinApiLevelStr = moduleMinApiLevel.String()
2273 if moduleMinApiLevel == android.NoneApiLevel {
2274 moduleMinApiLevelStr = "current"
2275 }
Jiyong Parke3833882020-02-17 17:28:10 +09002276 props := struct {
Pedro Loureiroc3621422021-09-28 15:40:23 +00002277 Name *string
2278 Lib_name *string
2279 Apex_available []string
2280 On_bootclasspath_since *string
2281 On_bootclasspath_before *string
2282 Min_device_sdk *string
2283 Max_device_sdk *string
2284 Sdk_library_min_api_level *string
Jamie Garsidee570ace2023-11-27 12:07:36 +00002285 Uses_libs_dependencies []string
Jiyong Parke3833882020-02-17 17:28:10 +09002286 }{
Pedro Loureiroc3621422021-09-28 15:40:23 +00002287 Name: proptools.StringPtr(module.xmlPermissionsModuleName()),
2288 Lib_name: proptools.StringPtr(module.BaseModuleName()),
2289 Apex_available: module.ApexProperties.Apex_available,
2290 On_bootclasspath_since: module.commonSdkLibraryProperties.On_bootclasspath_since,
2291 On_bootclasspath_before: module.commonSdkLibraryProperties.On_bootclasspath_before,
2292 Min_device_sdk: module.commonSdkLibraryProperties.Min_device_sdk,
2293 Max_device_sdk: module.commonSdkLibraryProperties.Max_device_sdk,
2294 Sdk_library_min_api_level: &moduleMinApiLevelStr,
Jamie Garsidee570ace2023-11-27 12:07:36 +00002295 Uses_libs_dependencies: module.usesLibraryProperties.Uses_libs,
Jiyong Parkc678ad32018-04-10 13:07:10 +09002296 }
Jiyong Parke3833882020-02-17 17:28:10 +09002297
Jiyong Parke3833882020-02-17 17:28:10 +09002298 mctx.CreateModule(sdkLibraryXmlFactory, &props)
Jiyong Parkc678ad32018-04-10 13:07:10 +09002299}
2300
Jiyong Parkf1691d22021-03-29 20:11:58 +09002301func PrebuiltJars(ctx android.BaseModuleContext, baseName string, s android.SdkSpec) android.Paths {
Jiyong Park54105c42021-03-31 18:17:53 +09002302 var ver android.ApiLevel
Jiyong Parkf1691d22021-03-29 20:11:58 +09002303 var kind android.SdkKind
2304 if s.UsePrebuilt(ctx) {
Jiyong Park54105c42021-03-31 18:17:53 +09002305 ver = s.ApiLevel
Jiyong Parkf1691d22021-03-29 20:11:58 +09002306 kind = s.Kind
Jiyong Parkc678ad32018-04-10 13:07:10 +09002307 } else {
Jiyong Park6a927c42020-01-21 02:03:43 +09002308 // We don't have prebuilt SDK for the specific sdkVersion.
2309 // Instead of breaking the build, fallback to use "system_current"
Jiyong Park54105c42021-03-31 18:17:53 +09002310 ver = android.FutureApiLevel
Jiyong Parkf1691d22021-03-29 20:11:58 +09002311 kind = android.SdkSystem
Sundong Ahn054b19a2018-10-19 13:46:09 +09002312 }
Jiyong Park6a927c42020-01-21 02:03:43 +09002313
2314 dir := filepath.Join("prebuilts", "sdk", ver.String(), kind.String())
Paul Duffin50061512020-01-21 16:31:05 +00002315 jar := filepath.Join(dir, baseName+".jar")
Sundong Ahn054b19a2018-10-19 13:46:09 +09002316 jarPath := android.ExistentPathForSource(ctx, jar)
Sundong Ahnae418ac2019-02-28 15:01:28 +09002317 if !jarPath.Valid() {
Colin Cross07c88562020-01-07 09:34:44 -08002318 if ctx.Config().AllowMissingDependencies() {
2319 return android.Paths{android.PathForSource(ctx, jar)}
2320 } else {
Jiyong Parkf1691d22021-03-29 20:11:58 +09002321 ctx.PropertyErrorf("sdk_library", "invalid sdk version %q, %q does not exist", s.Raw, jar)
Colin Cross07c88562020-01-07 09:34:44 -08002322 }
Sundong Ahnae418ac2019-02-28 15:01:28 +09002323 return nil
2324 }
Sundong Ahn054b19a2018-10-19 13:46:09 +09002325 return android.Paths{jarPath.Path()}
2326}
2327
Colin Crossaede88c2020-08-11 12:17:01 -07002328// 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 +01002329//
2330// If either this or the other module are on the platform then this will return
2331// false.
Colin Cross56a83212020-09-15 18:30:11 -07002332func withinSameApexesAs(ctx android.BaseModuleContext, other android.Module) bool {
Colin Crossff694a82023-12-13 15:54:49 -08002333 apexInfo, _ := android.ModuleProvider(ctx, android.ApexInfoProvider)
Colin Cross313aa542023-12-13 13:47:44 -08002334 otherApexInfo, _ := android.OtherModuleProvider(ctx, other, android.ApexInfoProvider)
Jiyong Parkab50b072021-05-12 17:13:56 +09002335 return len(otherApexInfo.InApexVariants) > 0 && reflect.DeepEqual(apexInfo.InApexVariants, otherApexInfo.InApexVariants)
Paul Duffin9b879592020-05-26 13:21:35 +01002336}
2337
Jihoon Kang8479dea2024-04-04 01:19:05 +00002338func (module *SdkLibrary) sdkJars(ctx android.BaseModuleContext, sdkVersion android.SdkSpec) android.Paths {
Jiyong Park932cdfe2020-05-28 00:19:53 +09002339 // If the client doesn't set sdk_version, but if this library prefers stubs over
2340 // the impl library, let's provide the widest API surface possible. To do so,
2341 // force override sdk_version to module_current so that the closest possible API
2342 // surface could be found in selectHeaderJarsForSdkVersion
Jiyong Parkf1691d22021-03-29 20:11:58 +09002343 if module.defaultsToStubs() && !sdkVersion.Specified() {
Jiyong Park92315372021-04-02 08:45:46 +09002344 sdkVersion = android.SdkSpecFrom(ctx, "module_current")
Jiyong Park932cdfe2020-05-28 00:19:53 +09002345 }
Paul Duffind1b3a922020-01-22 11:57:20 +00002346
Paul Duffindaaa3322020-05-26 18:13:57 +01002347 // Only provide access to the implementation library if it is actually built.
2348 if module.requiresRuntimeImplementationLibrary() {
2349 // Check any special cases for java_sdk_library.
2350 //
2351 // Only allow access to the implementation library in the following condition:
2352 // * No sdk_version specified on the referencing module.
Paul Duffin9b879592020-05-26 13:21:35 +01002353 // * The referencing module is in the same apex as this.
Jiyong Parkf1691d22021-03-29 20:11:58 +09002354 if sdkVersion.Kind == android.SdkPrivate || withinSameApexesAs(ctx, module) {
Jihoon Kang8479dea2024-04-04 01:19:05 +00002355 return module.implLibraryHeaderJars
Sundong Ahn054b19a2018-10-19 13:46:09 +09002356 }
Jiyong Parkc678ad32018-04-10 13:07:10 +09002357 }
Paul Duffinb05d4292020-05-20 12:19:10 +01002358
Paul Duffin23970f42020-05-20 14:20:02 +01002359 return module.selectHeaderJarsForSdkVersion(ctx, sdkVersion)
Jiyong Parkc678ad32018-04-10 13:07:10 +09002360}
2361
Sundong Ahn241cd372018-07-13 16:16:44 +09002362// to satisfy SdkLibraryDependency interface
Jiyong Parkf1691d22021-03-29 20:11:58 +09002363func (module *SdkLibrary) SdkHeaderJars(ctx android.BaseModuleContext, sdkVersion android.SdkSpec) android.Paths {
Jihoon Kang8479dea2024-04-04 01:19:05 +00002364 return module.sdkJars(ctx, sdkVersion)
Sundong Ahn241cd372018-07-13 16:16:44 +09002365}
2366
Colin Cross571cccf2019-02-04 11:22:08 -08002367var javaSdkLibrariesKey = android.NewOnceKey("javaSdkLibraries")
2368
Jiyong Park82484c02018-04-23 21:41:26 +09002369func javaSdkLibraries(config android.Config) *[]string {
Colin Cross571cccf2019-02-04 11:22:08 -08002370 return config.Once(javaSdkLibrariesKey, func() interface{} {
Jiyong Park82484c02018-04-23 21:41:26 +09002371 return &[]string{}
2372 }).(*[]string)
2373}
2374
Paul Duffin749f98f2019-12-30 17:23:46 +00002375func (module *SdkLibrary) getApiDir() string {
2376 return proptools.StringDefault(module.sdkLibraryProperties.Api_dir, "api")
2377}
2378
Jiyong Parkc678ad32018-04-10 13:07:10 +09002379// For a java_sdk_library module, create internal modules for stubs, docs,
2380// runtime libs and xml file. If requested, the stubs and docs are created twice
2381// once for public API level and once for system API level
Paul Duffinf0229202020-04-29 16:47:28 +01002382func (module *SdkLibrary) CreateInternalModules(mctx android.DefaultableHookContext) {
2383 // If the module has been disabled then don't create any child modules.
Cole Fausta963b942024-04-11 17:43:00 -07002384 if !module.Enabled(mctx) {
Paul Duffinf0229202020-04-29 16:47:28 +01002385 return
2386 }
2387
Paul Duffina18abc22020-05-16 18:54:24 +01002388 if len(module.properties.Srcs) == 0 {
Inseob Kimc0907f12019-02-08 21:00:45 +09002389 mctx.PropertyErrorf("srcs", "java_sdk_library must specify srcs")
Jooyung Han58f26ab2019-12-18 15:34:32 +09002390 return
Inseob Kimc0907f12019-02-08 21:00:45 +09002391 }
2392
Paul Duffin37e0b772019-12-30 17:20:10 +00002393 // If this builds against standard libraries (i.e. is not part of the core libraries)
Paul Duffin4f5c1ef2020-11-19 14:53:43 +00002394 // then assume it provides both system and test apis.
Jiyong Parkf1691d22021-03-29 20:11:58 +09002395 sdkDep := decodeSdkDep(mctx, android.SdkContext(&module.Library))
Paul Duffin37e0b772019-12-30 17:20:10 +00002396 hasSystemAndTestApis := sdkDep.hasStandardLibs()
Paul Duffin3375e352020-04-28 10:44:03 +01002397 module.sdkLibraryProperties.Generate_system_and_test_apis = hasSystemAndTestApis
Paul Duffin4f5c1ef2020-11-19 14:53:43 +00002398
Jaewoong Jung18aefc12020-12-21 09:11:10 -08002399 missingCurrentApi := false
Inseob Kim8098faa2019-03-18 10:19:51 +09002400
Paul Duffin3375e352020-04-28 10:44:03 +01002401 generatedScopes := module.getGeneratedApiScopes(mctx)
Paul Duffind1b3a922020-01-22 11:57:20 +00002402
Paul Duffin749f98f2019-12-30 17:23:46 +00002403 apiDir := module.getApiDir()
Paul Duffin3375e352020-04-28 10:44:03 +01002404 for _, scope := range generatedScopes {
Inseob Kim8098faa2019-03-18 10:19:51 +09002405 for _, api := range []string{"current.txt", "removed.txt"} {
Paul Duffind1b3a922020-01-22 11:57:20 +00002406 path := path.Join(mctx.ModuleDir(), apiDir, scope.apiFilePrefix+api)
Inseob Kim8098faa2019-03-18 10:19:51 +09002407 p := android.ExistentPathForSource(mctx, path)
2408 if !p.Valid() {
Colin Cross18f840c2021-05-20 17:56:54 -07002409 if mctx.Config().AllowMissingDependencies() {
2410 mctx.AddMissingDependencies([]string{path})
2411 } else {
2412 mctx.ModuleErrorf("Current api file %#v doesn't exist", path)
2413 missingCurrentApi = true
2414 }
Inseob Kim8098faa2019-03-18 10:19:51 +09002415 }
2416 }
2417 }
2418
Jaewoong Jung18aefc12020-12-21 09:11:10 -08002419 if missingCurrentApi {
Inseob Kim8098faa2019-03-18 10:19:51 +09002420 script := "build/soong/scripts/gen-java-current-api-files.sh"
2421 p := android.ExistentPathForSource(mctx, script)
2422
2423 if !p.Valid() {
2424 panic(fmt.Sprintf("script file %s doesn't exist", script))
2425 }
2426
2427 mctx.ModuleErrorf("One or more current api files are missing. "+
2428 "You can update them by:\n"+
Paul Duffin37e0b772019-12-30 17:20:10 +00002429 "%s %q %s && m update-api",
Paul Duffind1b3a922020-01-22 11:57:20 +00002430 script, filepath.Join(mctx.ModuleDir(), apiDir),
Paul Duffin3375e352020-04-28 10:44:03 +01002431 strings.Join(generatedScopes.Strings(func(s *apiScope) string { return s.apiFilePrefix }), " "))
Inseob Kim8098faa2019-03-18 10:19:51 +09002432 return
2433 }
2434
Paul Duffin3375e352020-04-28 10:44:03 +01002435 for _, scope := range generatedScopes {
Paul Duffin15f34ef2020-07-20 18:04:44 +01002436 // Use the stubs source name for legacy reasons.
2437 module.createStubsSourcesAndApi(mctx, scope, module.stubsSourceModuleName(scope), scope.droidstubsArgs)
Paul Duffin0ff08bd2020-04-29 13:30:54 +01002438
Paul Duffind1b3a922020-01-22 11:57:20 +00002439 module.createStubsLibrary(mctx, scope)
Jihoon Kangfa4a90d2023-12-20 02:53:38 +00002440 module.createExportableStubsLibrary(mctx, scope)
Jihoon Kang1c92c3e2023-03-23 17:44:51 +00002441
Jihoon Kang0c705a42023-08-02 06:44:57 +00002442 alternativeFullApiSurfaceStubLib := ""
2443 if scope == apiScopePublic {
2444 alternativeFullApiSurfaceStubLib = module.alternativeFullApiSurfaceStubLib()
2445 }
2446 contributesToApiSurface := module.contributesToApiSurface(mctx.Config()) || alternativeFullApiSurfaceStubLib != ""
Jihoon Kang1147b312023-06-08 23:25:57 +00002447 if contributesToApiSurface {
Jihoon Kang0c705a42023-08-02 06:44:57 +00002448 module.createApiLibrary(mctx, scope, alternativeFullApiSurfaceStubLib)
Jihoon Kang1c92c3e2023-03-23 17:44:51 +00002449 }
Jihoon Kang1147b312023-06-08 23:25:57 +00002450
2451 module.createTopLevelStubsLibrary(mctx, scope, contributesToApiSurface)
Jihoon Kangfa4a90d2023-12-20 02:53:38 +00002452 module.createTopLevelExportableStubsLibrary(mctx, scope)
Inseob Kimc0907f12019-02-08 21:00:45 +09002453 }
2454
Paul Duffindfa131e2020-05-15 20:37:11 +01002455 if module.requiresRuntimeImplementationLibrary() {
Paul Duffin5df79302020-05-16 15:52:12 +01002456 // Create child module to create an implementation library.
2457 //
2458 // This temporarily creates a second implementation library that can be explicitly
2459 // referenced.
2460 //
2461 // TODO(b/156618935) - update comment once only one implementation library is created.
2462 module.createImplLibrary(mctx)
2463
Paul Duffindfa131e2020-05-15 20:37:11 +01002464 // Only create an XML permissions file that declares the library as being usable
2465 // as a shared library if required.
2466 if module.sharedLibrary() {
2467 module.createXmlFile(mctx)
2468 }
Paul Duffin43db9be2019-12-30 17:35:49 +00002469
2470 // record java_sdk_library modules so that they are exported to make
2471 javaSdkLibraries := javaSdkLibraries(mctx.Config())
2472 javaSdkLibrariesLock.Lock()
2473 defer javaSdkLibrariesLock.Unlock()
2474 *javaSdkLibraries = append(*javaSdkLibraries, module.BaseModuleName())
2475 }
Anton Hansson7f66efa2020-10-08 14:47:23 +01002476
Paul Duffin77590a82022-04-28 14:13:30 +00002477 // 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 +01002478 module.properties.Libs = append(module.properties.Libs, module.sdkLibraryProperties.Impl_only_libs...)
Paul Duffin77590a82022-04-28 14:13:30 +00002479 module.properties.Static_libs = append(module.properties.Static_libs, module.sdkLibraryProperties.Impl_only_static_libs...)
Inseob Kimc0907f12019-02-08 21:00:45 +09002480}
2481
2482func (module *SdkLibrary) InitSdkLibraryProperties() {
Colin Crossce6734e2020-06-15 16:09:53 -07002483 module.addHostAndDeviceProperties()
2484 module.AddProperties(&module.sdkLibraryProperties)
Sundong Ahn054b19a2018-10-19 13:46:09 +09002485
Paul Duffin71b33cc2021-06-23 11:39:47 +01002486 module.initSdkLibraryComponent(module)
Paul Duffin859fe962020-05-15 10:20:31 +01002487
Paul Duffina18abc22020-05-16 18:54:24 +01002488 module.properties.Installable = proptools.BoolPtr(true)
2489 module.deviceProperties.IsSDKLibrary = true
Inseob Kimc0907f12019-02-08 21:00:45 +09002490}
Sundong Ahn054b19a2018-10-19 13:46:09 +09002491
Paul Duffindfa131e2020-05-15 20:37:11 +01002492func (module *SdkLibrary) requiresRuntimeImplementationLibrary() bool {
2493 return !proptools.Bool(module.sdkLibraryProperties.Api_only)
2494}
2495
Jiyong Park932cdfe2020-05-28 00:19:53 +09002496func (module *SdkLibrary) defaultsToStubs() bool {
2497 return proptools.Bool(module.sdkLibraryProperties.Default_to_stubs)
2498}
2499
Paul Duffin1b1e8062020-05-08 13:44:43 +01002500// Defines how to name the individual component modules the sdk library creates.
2501type sdkLibraryComponentNamingScheme interface {
2502 stubsLibraryModuleName(scope *apiScope, baseName string) string
2503
2504 stubsSourceModuleName(scope *apiScope, baseName string) string
Jihoon Kang1c92c3e2023-03-23 17:44:51 +00002505
2506 apiLibraryModuleName(scope *apiScope, baseName string) string
Jihoon Kang1147b312023-06-08 23:25:57 +00002507
Jihoon Kangfa4a90d2023-12-20 02:53:38 +00002508 sourceStubsLibraryModuleName(scope *apiScope, baseName string) string
2509
2510 exportableStubsLibraryModuleName(scope *apiScope, baseName string) string
2511
2512 exportableSourceStubsLibraryModuleName(scope *apiScope, baseName string) string
Paul Duffin1b1e8062020-05-08 13:44:43 +01002513}
2514
2515type defaultNamingScheme struct {
2516}
2517
2518func (s *defaultNamingScheme) stubsLibraryModuleName(scope *apiScope, baseName string) string {
2519 return scope.stubsLibraryModuleName(baseName)
2520}
2521
2522func (s *defaultNamingScheme) stubsSourceModuleName(scope *apiScope, baseName string) string {
2523 return scope.stubsSourceModuleName(baseName)
2524}
2525
Jihoon Kang1c92c3e2023-03-23 17:44:51 +00002526func (s *defaultNamingScheme) apiLibraryModuleName(scope *apiScope, baseName string) string {
2527 return scope.apiLibraryModuleName(baseName)
2528}
2529
Jihoon Kangfa4a90d2023-12-20 02:53:38 +00002530func (s *defaultNamingScheme) sourceStubsLibraryModuleName(scope *apiScope, baseName string) string {
Jihoon Kang1147b312023-06-08 23:25:57 +00002531 return scope.sourceStubLibraryModuleName(baseName)
2532}
2533
Jihoon Kangfa4a90d2023-12-20 02:53:38 +00002534func (s *defaultNamingScheme) exportableStubsLibraryModuleName(scope *apiScope, baseName string) string {
2535 return scope.exportableStubsLibraryModuleName(baseName)
2536}
2537
2538func (s *defaultNamingScheme) exportableSourceStubsLibraryModuleName(scope *apiScope, baseName string) string {
2539 return scope.exportableSourceStubsLibraryModuleName(baseName)
2540}
2541
Paul Duffin1b1e8062020-05-08 13:44:43 +01002542var _ sdkLibraryComponentNamingScheme = (*defaultNamingScheme)(nil)
2543
Jihoon Kangfa4a90d2023-12-20 02:53:38 +00002544func hasStubsLibrarySuffix(name string, apiScope *apiScope) bool {
2545 return strings.HasSuffix(name, apiScope.stubsLibraryModuleNameSuffix()) ||
2546 strings.HasSuffix(name, apiScope.exportableStubsLibraryModuleNameSuffix())
2547}
2548
Jaewoong Jungbc15e3a2021-03-10 17:02:43 -08002549func moduleStubLinkType(name string) (stub bool, ret sdkLinkType) {
Jihoon Kang1147b312023-06-08 23:25:57 +00002550 name = strings.TrimSuffix(name, ".from-source")
2551
Anton Hansson2d0c1942020-05-25 12:20:51 +01002552 // This suffix-based approach is fragile and could potentially mis-trigger.
2553 // TODO(b/155164730): Clean this up when modules no longer reference sdk_lib stubs directly.
Jihoon Kangfa4a90d2023-12-20 02:53:38 +00002554 if hasStubsLibrarySuffix(name, apiScopePublic) {
Anton Hansson08f476b2021-04-07 15:32:19 +01002555 if name == "hwbinder.stubs" || name == "libcore_private.stubs" {
2556 // Due to a previous bug, these modules were not considered stubs, so we retain that.
2557 return false, javaPlatform
2558 }
Anton Hansson2d0c1942020-05-25 12:20:51 +01002559 return true, javaSdk
2560 }
Jihoon Kangfa4a90d2023-12-20 02:53:38 +00002561 if hasStubsLibrarySuffix(name, apiScopeSystem) {
Anton Hansson2d0c1942020-05-25 12:20:51 +01002562 return true, javaSystem
2563 }
Jihoon Kangfa4a90d2023-12-20 02:53:38 +00002564 if hasStubsLibrarySuffix(name, apiScopeModuleLib) {
Anton Hansson2d0c1942020-05-25 12:20:51 +01002565 return true, javaModule
2566 }
Jihoon Kangfa4a90d2023-12-20 02:53:38 +00002567 if hasStubsLibrarySuffix(name, apiScopeTest) {
Anton Hansson2d0c1942020-05-25 12:20:51 +01002568 return true, javaSystem
2569 }
Jihoon Kangfa4a90d2023-12-20 02:53:38 +00002570 if hasStubsLibrarySuffix(name, apiScopeSystemServer) {
Jihoon Kang1147b312023-06-08 23:25:57 +00002571 return true, javaSystemServer
2572 }
Anton Hansson2d0c1942020-05-25 12:20:51 +01002573 return false, javaPlatform
2574}
2575
Jaewoong Jung4f158ee2019-07-11 10:05:35 -07002576// java_sdk_library is a special Java library that provides optional platform APIs to apps.
2577// In practice, it can be viewed as a combination of several modules: 1) stubs library that clients
2578// are linked against to, 2) droiddoc module that internally generates API stubs source files,
2579// 3) the real runtime shared library that implements the APIs, and 4) XML file for adding
2580// the runtime lib to the classpath at runtime if requested via <uses-library>.
Inseob Kimc0907f12019-02-08 21:00:45 +09002581func SdkLibraryFactory() android.Module {
2582 module := &SdkLibrary{}
Paul Duffinc3091c82020-05-08 14:16:20 +01002583
2584 // Initialize information common between source and prebuilt.
Paul Duffin71b33cc2021-06-23 11:39:47 +01002585 module.initCommon(module)
Paul Duffinc3091c82020-05-08 14:16:20 +01002586
Inseob Kimc0907f12019-02-08 21:00:45 +09002587 module.InitSdkLibraryProperties()
Jooyung Han58f26ab2019-12-18 15:34:32 +09002588 android.InitApexModule(module)
Sundong Ahn054b19a2018-10-19 13:46:09 +09002589 InitJavaModule(module, android.HostAndDeviceSupported)
Paul Duffin3375e352020-04-28 10:44:03 +01002590
2591 // Initialize the map from scope to scope specific properties.
2592 scopeToProperties := make(map[*apiScope]*ApiScopeProperties)
Jihoon Kang98aa8fa2024-06-07 11:06:57 +00002593 for _, scope := range AllApiScopes {
Paul Duffin3375e352020-04-28 10:44:03 +01002594 scopeToProperties[scope] = scope.scopeSpecificProperties(module)
2595 }
2596 module.scopeToProperties = scopeToProperties
2597
Paul Duffin4911a892020-04-29 23:35:13 +01002598 // Add the properties containing visibility rules so that they are checked.
Paul Duffin5df79302020-05-16 15:52:12 +01002599 android.AddVisibilityProperty(module, "impl_library_visibility", &module.sdkLibraryProperties.Impl_library_visibility)
Paul Duffin4911a892020-04-29 23:35:13 +01002600 android.AddVisibilityProperty(module, "stubs_library_visibility", &module.sdkLibraryProperties.Stubs_library_visibility)
2601 android.AddVisibilityProperty(module, "stubs_source_visibility", &module.sdkLibraryProperties.Stubs_source_visibility)
2602
Paul Duffin1b1e8062020-05-08 13:44:43 +01002603 module.SetDefaultableHook(func(ctx android.DefaultableHookContext) {
Paul Duffindfa131e2020-05-15 20:37:11 +01002604 // If no implementation is required then it cannot be used as a shared library
2605 // either.
2606 if !module.requiresRuntimeImplementationLibrary() {
2607 // If shared_library has been explicitly set to true then it is incompatible
2608 // with api_only: true.
2609 if proptools.Bool(module.commonSdkLibraryProperties.Shared_library) {
2610 ctx.PropertyErrorf("api_only/shared_library", "inconsistent settings, shared_library and api_only cannot both be true")
2611 }
2612 // Set shared_library: false.
2613 module.commonSdkLibraryProperties.Shared_library = proptools.BoolPtr(false)
2614 }
2615
Paul Duffin1b1e8062020-05-08 13:44:43 +01002616 if module.initCommonAfterDefaultsApplied(ctx) {
2617 module.CreateInternalModules(ctx)
2618 }
2619 })
Jiyong Parkc678ad32018-04-10 13:07:10 +09002620 return module
2621}
Colin Cross79c7c262019-04-17 11:11:46 -07002622
2623//
2624// SDK library prebuilts
2625//
2626
Paul Duffin56d44902020-01-31 13:36:25 +00002627// Properties associated with each api scope.
2628type sdkLibraryScopeProperties struct {
Colin Cross79c7c262019-04-17 11:11:46 -07002629 Jars []string `android:"path"`
2630
2631 Sdk_version *string
2632
Colin Cross79c7c262019-04-17 11:11:46 -07002633 // List of shared java libs that this module has dependencies to
2634 Libs []string
Paul Duffin3d1248c2020-04-09 00:10:17 +01002635
Paul Duffinc8782502020-04-29 20:45:27 +01002636 // The stubs source.
Paul Duffin3d1248c2020-04-09 00:10:17 +01002637 Stub_srcs []string `android:"path"`
Paul Duffin1fd005d2020-04-09 01:08:11 +01002638
2639 // The current.txt
Paul Duffin0f8faff2020-05-20 16:18:00 +01002640 Current_api *string `android:"path"`
Paul Duffin1fd005d2020-04-09 01:08:11 +01002641
2642 // The removed.txt
Paul Duffin0f8faff2020-05-20 16:18:00 +01002643 Removed_api *string `android:"path"`
Anton Hanssond78eb762021-09-21 15:25:12 +01002644
2645 // Annotation zip
2646 Annotations *string `android:"path"`
Colin Cross79c7c262019-04-17 11:11:46 -07002647}
2648
Paul Duffin56d44902020-01-31 13:36:25 +00002649type sdkLibraryImportProperties struct {
Paul Duffinfcfd7912020-01-31 17:54:30 +00002650 // List of shared java libs, common to all scopes, that this module has
2651 // dependencies to
2652 Libs []string
Paul Duffin1267d872021-04-16 17:21:36 +01002653
2654 // If set to true, compile dex files for the stubs. Defaults to false.
2655 Compile_dex *bool
Paul Duffin869de142021-07-15 14:14:41 +01002656
2657 // If not empty, classes are restricted to the specified packages and their sub-packages.
Paul Duffin869de142021-07-15 14:14:41 +01002658 Permitted_packages []string
Spandan Das23956d12024-01-19 00:22:22 +00002659
2660 // Name of the source soong module that gets shadowed by this prebuilt
2661 // If unspecified, follows the naming convention that the source module of
2662 // the prebuilt is Name() without "prebuilt_" prefix
2663 Source_module_name *string
Paul Duffin56d44902020-01-31 13:36:25 +00002664}
2665
Paul Duffineedc5d52020-06-12 17:46:39 +01002666type SdkLibraryImport struct {
Colin Cross79c7c262019-04-17 11:11:46 -07002667 android.ModuleBase
2668 android.DefaultableModuleBase
2669 prebuilt android.Prebuilt
Paul Duffindd46f712020-02-10 13:37:10 +00002670 android.ApexModuleBase
Colin Cross79c7c262019-04-17 11:11:46 -07002671
Paul Duffin37856732021-02-26 14:24:15 +00002672 hiddenAPI
Jiakai Zhang204356f2021-09-09 08:12:46 +00002673 dexpreopter
Paul Duffin37856732021-02-26 14:24:15 +00002674
Colin Cross79c7c262019-04-17 11:11:46 -07002675 properties sdkLibraryImportProperties
2676
Paul Duffin46a26a82020-04-07 19:27:04 +01002677 // Map from api scope to the scope specific property structure.
2678 scopeProperties map[*apiScope]*sdkLibraryScopeProperties
2679
Paul Duffin56d44902020-01-31 13:36:25 +00002680 commonToSdkLibraryAndImport
Paul Duffineedc5d52020-06-12 17:46:39 +01002681
Paul Duffineedc5d52020-06-12 17:46:39 +01002682 // The reference to the xml permissions module created by the source module.
2683 // Is nil if the source module does not exist.
2684 xmlPermissionsFileModule *sdkLibraryXml
Paul Duffin39853512021-02-26 11:09:39 +00002685
Jeongik Chad5fe8782021-07-08 01:13:11 +09002686 // Build path to the dex implementation jar obtained from the prebuilt_apex, if any.
Spandan Dasfae468e2023-12-12 23:23:53 +00002687 dexJarFile OptionalDexJarPath
2688 dexJarFileErr error
Jeongik Chad5fe8782021-07-08 01:13:11 +09002689
2690 // Expected install file path of the source module(sdk_library)
2691 // or dex implementation jar obtained from the prebuilt_apex, if any.
2692 installFile android.Path
Colin Cross79c7c262019-04-17 11:11:46 -07002693}
2694
Paul Duffineedc5d52020-06-12 17:46:39 +01002695var _ SdkLibraryDependency = (*SdkLibraryImport)(nil)
Colin Cross79c7c262019-04-17 11:11:46 -07002696
Paul Duffin46a26a82020-04-07 19:27:04 +01002697// The type of a structure that contains a field of type sdkLibraryScopeProperties
2698// for each apiscope in allApiScopes, e.g. something like:
Colin Crossd079e0b2022-08-16 10:27:33 -07002699//
2700// struct {
2701// Public sdkLibraryScopeProperties
2702// System sdkLibraryScopeProperties
2703// ...
2704// }
Paul Duffin46a26a82020-04-07 19:27:04 +01002705var allScopeStructType = createAllScopePropertiesStructType()
2706
2707// Dynamically create a structure type for each apiscope in allApiScopes.
2708func createAllScopePropertiesStructType() reflect.Type {
2709 var fields []reflect.StructField
Jihoon Kang98aa8fa2024-06-07 11:06:57 +00002710 for _, apiScope := range AllApiScopes {
Paul Duffin46a26a82020-04-07 19:27:04 +01002711 field := reflect.StructField{
2712 Name: apiScope.fieldName,
2713 Type: reflect.TypeOf(sdkLibraryScopeProperties{}),
2714 }
2715 fields = append(fields, field)
2716 }
2717
2718 return reflect.StructOf(fields)
2719}
2720
2721// Create an instance of the scope specific structure type and return a map
2722// from apiscope to a pointer to each scope specific field.
2723func createPropertiesInstance() (interface{}, map[*apiScope]*sdkLibraryScopeProperties) {
2724 allScopePropertiesPtr := reflect.New(allScopeStructType)
2725 allScopePropertiesStruct := allScopePropertiesPtr.Elem()
2726 scopeProperties := make(map[*apiScope]*sdkLibraryScopeProperties)
2727
Jihoon Kang98aa8fa2024-06-07 11:06:57 +00002728 for _, apiScope := range AllApiScopes {
Paul Duffin46a26a82020-04-07 19:27:04 +01002729 field := allScopePropertiesStruct.FieldByName(apiScope.fieldName)
2730 scopeProperties[apiScope] = field.Addr().Interface().(*sdkLibraryScopeProperties)
2731 }
2732
2733 return allScopePropertiesPtr.Interface(), scopeProperties
2734}
2735
Jaewoong Jung4f158ee2019-07-11 10:05:35 -07002736// java_sdk_library_import imports a prebuilt java_sdk_library.
Colin Cross79c7c262019-04-17 11:11:46 -07002737func sdkLibraryImportFactory() android.Module {
Paul Duffineedc5d52020-06-12 17:46:39 +01002738 module := &SdkLibraryImport{}
Colin Cross79c7c262019-04-17 11:11:46 -07002739
Paul Duffin46a26a82020-04-07 19:27:04 +01002740 allScopeProperties, scopeToProperties := createPropertiesInstance()
2741 module.scopeProperties = scopeToProperties
Jiakai Zhang9c4dc192023-02-09 00:09:24 +08002742 module.AddProperties(&module.properties, allScopeProperties, &module.importDexpreoptProperties)
Colin Cross79c7c262019-04-17 11:11:46 -07002743
Paul Duffinc3091c82020-05-08 14:16:20 +01002744 // Initialize information common between source and prebuilt.
Paul Duffin71b33cc2021-06-23 11:39:47 +01002745 module.initCommon(module)
Paul Duffinc3091c82020-05-08 14:16:20 +01002746
Paul Duffin0bdcb272020-02-06 15:24:57 +00002747 android.InitPrebuiltModule(module, &[]string{""})
Paul Duffindd46f712020-02-10 13:37:10 +00002748 android.InitApexModule(module)
Colin Cross79c7c262019-04-17 11:11:46 -07002749 InitJavaModule(module, android.HostAndDeviceSupported)
2750
Paul Duffin1b1e8062020-05-08 13:44:43 +01002751 module.SetDefaultableHook(func(mctx android.DefaultableHookContext) {
2752 if module.initCommonAfterDefaultsApplied(mctx) {
2753 module.createInternalModules(mctx)
2754 }
2755 })
Colin Cross79c7c262019-04-17 11:11:46 -07002756 return module
2757}
2758
Paul Duffin630b11e2021-07-15 13:35:26 +01002759var _ PermittedPackagesForUpdatableBootJars = (*SdkLibraryImport)(nil)
2760
2761func (module *SdkLibraryImport) PermittedPackagesForUpdatableBootJars() []string {
2762 return module.properties.Permitted_packages
2763}
2764
Paul Duffineedc5d52020-06-12 17:46:39 +01002765func (module *SdkLibraryImport) Prebuilt() *android.Prebuilt {
Colin Cross79c7c262019-04-17 11:11:46 -07002766 return &module.prebuilt
2767}
2768
Paul Duffineedc5d52020-06-12 17:46:39 +01002769func (module *SdkLibraryImport) Name() string {
Colin Cross79c7c262019-04-17 11:11:46 -07002770 return module.prebuilt.Name(module.ModuleBase.Name())
2771}
2772
Spandan Das23956d12024-01-19 00:22:22 +00002773func (module *SdkLibraryImport) BaseModuleName() string {
2774 return proptools.StringDefault(module.properties.Source_module_name, module.ModuleBase.Name())
2775}
2776
Paul Duffineedc5d52020-06-12 17:46:39 +01002777func (module *SdkLibraryImport) createInternalModules(mctx android.DefaultableHookContext) {
Colin Cross79c7c262019-04-17 11:11:46 -07002778
Paul Duffin50061512020-01-21 16:31:05 +00002779 // If the build is configured to use prebuilts then force this to be preferred.
Jeongik Cha816a23a2020-07-08 01:09:23 +09002780 if mctx.Config().AlwaysUsePrebuiltSdks() {
Paul Duffin50061512020-01-21 16:31:05 +00002781 module.prebuilt.ForcePrefer()
2782 }
2783
Paul Duffin46a26a82020-04-07 19:27:04 +01002784 for apiScope, scopeProperties := range module.scopeProperties {
Paul Duffin56d44902020-01-31 13:36:25 +00002785 if len(scopeProperties.Jars) == 0 {
2786 continue
2787 }
2788
Paul Duffinbbb546b2020-04-09 00:07:11 +01002789 module.createJavaImportForStubs(mctx, apiScope, scopeProperties)
Paul Duffin3d1248c2020-04-09 00:10:17 +01002790
Paul Duffin0f8faff2020-05-20 16:18:00 +01002791 if len(scopeProperties.Stub_srcs) > 0 {
2792 module.createPrebuiltStubsSources(mctx, apiScope, scopeProperties)
2793 }
Jihoon Kang71c86832023-09-13 01:01:53 +00002794
2795 if scopeProperties.Current_api != nil {
2796 module.createPrebuiltApiContribution(mctx, apiScope, scopeProperties)
2797 }
Paul Duffin56d44902020-01-31 13:36:25 +00002798 }
Colin Cross79c7c262019-04-17 11:11:46 -07002799
2800 javaSdkLibraries := javaSdkLibraries(mctx.Config())
2801 javaSdkLibrariesLock.Lock()
2802 defer javaSdkLibrariesLock.Unlock()
2803 *javaSdkLibraries = append(*javaSdkLibraries, module.BaseModuleName())
2804}
2805
Paul Duffineedc5d52020-06-12 17:46:39 +01002806func (module *SdkLibraryImport) createJavaImportForStubs(mctx android.DefaultableHookContext, apiScope *apiScope, scopeProperties *sdkLibraryScopeProperties) {
Paul Duffinbbb546b2020-04-09 00:07:11 +01002807 // Creates a java import for the jar with ".stubs" suffix
2808 props := struct {
Spandan Das23956d12024-01-19 00:22:22 +00002809 Name *string
2810 Source_module_name *string
2811 Created_by_java_sdk_library_name *string
2812 Sdk_version *string
2813 Libs []string
2814 Jars []string
2815 Compile_dex *bool
Jihoon Kangfe914ed2024-02-12 22:49:21 +00002816 Is_stubs_module *bool
Paul Duffinbf4de042022-09-27 12:41:52 +01002817
2818 android.UserSuppliedPrebuiltProperties
Paul Duffinbbb546b2020-04-09 00:07:11 +01002819 }{}
Paul Duffinc3091c82020-05-08 14:16:20 +01002820 props.Name = proptools.StringPtr(module.stubsLibraryModuleName(apiScope))
Spandan Das23956d12024-01-19 00:22:22 +00002821 props.Source_module_name = proptools.StringPtr(apiScope.stubsLibraryModuleName(module.BaseModuleName()))
2822 props.Created_by_java_sdk_library_name = proptools.StringPtr(module.RootLibraryName())
Paul Duffinbbb546b2020-04-09 00:07:11 +01002823 props.Sdk_version = scopeProperties.Sdk_version
2824 // Prepend any of the libs from the legacy public properties to the libs for each of the
2825 // scopes to avoid having to duplicate them in each scope.
2826 props.Libs = append(module.properties.Libs, scopeProperties.Libs...)
2827 props.Jars = scopeProperties.Jars
Paul Duffin1dbe3ca2020-05-16 09:57:59 +01002828
Paul Duffin38b57852020-05-13 16:08:09 +01002829 // The imports are preferred if the java_sdk_library_import is preferred.
Paul Duffinbf4de042022-09-27 12:41:52 +01002830 props.CopyUserSuppliedPropertiesFromPrebuilt(&module.prebuilt)
Paul Duffin859fe962020-05-15 10:20:31 +01002831
Paul Duffin1267d872021-04-16 17:21:36 +01002832 // The imports need to be compiled to dex if the java_sdk_library_import requests it.
Paul Duffinf4600f62021-05-13 22:34:45 +01002833 compileDex := module.properties.Compile_dex
2834 if module.stubLibrariesCompiledForDex() {
2835 compileDex = proptools.BoolPtr(true)
2836 }
2837 props.Compile_dex = compileDex
Jihoon Kangfe914ed2024-02-12 22:49:21 +00002838 props.Is_stubs_module = proptools.BoolPtr(true)
Paul Duffin1267d872021-04-16 17:21:36 +01002839
Paul Duffin859fe962020-05-15 10:20:31 +01002840 mctx.CreateModule(ImportFactory, &props, module.sdkComponentPropertiesForChildLibrary())
Paul Duffinbbb546b2020-04-09 00:07:11 +01002841}
2842
Paul Duffineedc5d52020-06-12 17:46:39 +01002843func (module *SdkLibraryImport) createPrebuiltStubsSources(mctx android.DefaultableHookContext, apiScope *apiScope, scopeProperties *sdkLibraryScopeProperties) {
Paul Duffin3d1248c2020-04-09 00:10:17 +01002844 props := struct {
Spandan Das23956d12024-01-19 00:22:22 +00002845 Name *string
2846 Source_module_name *string
2847 Created_by_java_sdk_library_name *string
2848 Srcs []string
Paul Duffinbf4de042022-09-27 12:41:52 +01002849
2850 android.UserSuppliedPrebuiltProperties
Paul Duffin3d1248c2020-04-09 00:10:17 +01002851 }{}
Paul Duffinc3091c82020-05-08 14:16:20 +01002852 props.Name = proptools.StringPtr(module.stubsSourceModuleName(apiScope))
Spandan Das23956d12024-01-19 00:22:22 +00002853 props.Source_module_name = proptools.StringPtr(apiScope.stubsSourceModuleName(module.BaseModuleName()))
2854 props.Created_by_java_sdk_library_name = proptools.StringPtr(module.RootLibraryName())
Paul Duffin3d1248c2020-04-09 00:10:17 +01002855 props.Srcs = scopeProperties.Stub_srcs
Paul Duffin38b57852020-05-13 16:08:09 +01002856
2857 // The stubs source is preferred if the java_sdk_library_import is preferred.
Paul Duffinbf4de042022-09-27 12:41:52 +01002858 props.CopyUserSuppliedPropertiesFromPrebuilt(&module.prebuilt)
2859
Spandan Das2cc80ba2023-10-27 17:21:52 +00002860 mctx.CreateModule(PrebuiltStubsSourcesFactory, &props, module.sdkComponentPropertiesForChildLibrary())
Paul Duffin3d1248c2020-04-09 00:10:17 +01002861}
2862
Jihoon Kang71c86832023-09-13 01:01:53 +00002863func (module *SdkLibraryImport) createPrebuiltApiContribution(mctx android.DefaultableHookContext, apiScope *apiScope, scopeProperties *sdkLibraryScopeProperties) {
2864 api_file := scopeProperties.Current_api
2865 api_surface := &apiScope.name
2866
2867 props := struct {
Spandan Das23956d12024-01-19 00:22:22 +00002868 Name *string
2869 Source_module_name *string
2870 Created_by_java_sdk_library_name *string
2871 Api_surface *string
2872 Api_file *string
2873 Visibility []string
Jihoon Kang71c86832023-09-13 01:01:53 +00002874 }{}
2875
2876 props.Name = proptools.StringPtr(module.stubsSourceModuleName(apiScope) + ".api.contribution")
Spandan Das23956d12024-01-19 00:22:22 +00002877 props.Source_module_name = proptools.StringPtr(apiScope.stubsSourceModuleName(module.BaseModuleName()) + ".api.contribution")
2878 props.Created_by_java_sdk_library_name = proptools.StringPtr(module.RootLibraryName())
Jihoon Kang71c86832023-09-13 01:01:53 +00002879 props.Api_surface = api_surface
2880 props.Api_file = api_file
2881 props.Visibility = []string{"//visibility:override", "//visibility:public"}
2882
Spandan Das2cc80ba2023-10-27 17:21:52 +00002883 mctx.CreateModule(ApiContributionImportFactory, &props, module.sdkComponentPropertiesForChildLibrary())
Jihoon Kang71c86832023-09-13 01:01:53 +00002884}
2885
Paul Duffin44f1d842020-06-26 20:17:02 +01002886// Add the dependencies on the child module in the component deps mutator so that it
2887// creates references to the prebuilt and not the source modules.
2888func (module *SdkLibraryImport) ComponentDepsMutator(ctx android.BottomUpMutatorContext) {
Paul Duffin46a26a82020-04-07 19:27:04 +01002889 for apiScope, scopeProperties := range module.scopeProperties {
Paul Duffin56d44902020-01-31 13:36:25 +00002890 if len(scopeProperties.Jars) == 0 {
2891 continue
2892 }
2893
2894 // Add dependencies to the prebuilt stubs library
Jihoon Kangb7431552024-01-22 19:40:08 +00002895 ctx.AddVariationDependencies(nil, apiScope.prebuiltStubsTag, android.PrebuiltNameFromSource(module.stubsLibraryModuleName(apiScope)))
Paul Duffin0f8faff2020-05-20 16:18:00 +01002896
2897 if len(scopeProperties.Stub_srcs) > 0 {
2898 // Add dependencies to the prebuilt stubs source library
Paul Duffin864116c2021-04-02 10:24:13 +01002899 ctx.AddVariationDependencies(nil, apiScope.stubsSourceTag, android.PrebuiltNameFromSource(module.stubsSourceModuleName(apiScope)))
Paul Duffin0f8faff2020-05-20 16:18:00 +01002900 }
Paul Duffin56d44902020-01-31 13:36:25 +00002901 }
Paul Duffin44f1d842020-06-26 20:17:02 +01002902}
2903
2904// Add other dependencies as normal.
2905func (module *SdkLibraryImport) DepsMutator(ctx android.BottomUpMutatorContext) {
Paul Duffineedc5d52020-06-12 17:46:39 +01002906
2907 implName := module.implLibraryModuleName()
2908 if ctx.OtherModuleExists(implName) {
2909 ctx.AddVariationDependencies(nil, implLibraryTag, implName)
2910
2911 xmlPermissionsModuleName := module.xmlPermissionsModuleName()
2912 if module.sharedLibrary() && ctx.OtherModuleExists(xmlPermissionsModuleName) {
2913 // Add dependency to the rule for generating the xml permissions file
2914 ctx.AddDependency(module, xmlPermissionsFileTag, xmlPermissionsModuleName)
2915 }
2916 }
Colin Cross79c7c262019-04-17 11:11:46 -07002917}
2918
Jiyong Park45bf82e2020-12-15 22:29:02 +09002919var _ android.ApexModule = (*SdkLibraryImport)(nil)
2920
2921// Implements android.ApexModule
Paul Duffineedc5d52020-06-12 17:46:39 +01002922func (module *SdkLibraryImport) DepIsInSameApex(mctx android.BaseModuleContext, dep android.Module) bool {
2923 depTag := mctx.OtherModuleDependencyTag(dep)
2924 if depTag == xmlPermissionsFileTag {
2925 return true
2926 }
2927
2928 // None of the other dependencies of the java_sdk_library_import are in the same apex
2929 // as the one that references this module.
2930 return false
2931}
2932
Jiyong Park45bf82e2020-12-15 22:29:02 +09002933// Implements android.ApexModule
Dan Albertc8060532020-07-22 22:32:17 -07002934func (module *SdkLibraryImport) ShouldSupportSdkVersion(ctx android.BaseModuleContext,
2935 sdkVersion android.ApiLevel) error {
Jooyung Han749dc692020-04-15 11:03:39 +09002936 // we don't check prebuilt modules for sdk_version
2937 return nil
2938}
2939
Paul Duffinea8f8082021-06-24 13:25:57 +01002940// Implements android.ApexModule
2941func (module *SdkLibraryImport) UniqueApexVariations() bool {
2942 return module.uniqueApexVariations()
2943}
2944
Paul Duffin09817d62022-04-28 17:45:11 +01002945// MinSdkVersion - Implements hiddenAPIModule
Spandan Das8c9ae7e2023-03-03 21:20:36 +00002946func (module *SdkLibraryImport) MinSdkVersion(ctx android.EarlyModuleContext) android.ApiLevel {
2947 return android.NoneApiLevel
Paul Duffin09817d62022-04-28 17:45:11 +01002948}
2949
2950var _ hiddenAPIModule = (*SdkLibraryImport)(nil)
2951
Paul Duffineedc5d52020-06-12 17:46:39 +01002952func (module *SdkLibraryImport) OutputFiles(tag string) (android.Paths, error) {
Paul Duffin1e940d52022-04-29 14:21:25 +01002953 paths, err := module.commonOutputFiles(tag)
2954 if paths != nil || err != nil {
2955 return paths, err
2956 }
2957 if module.implLibraryModule != nil {
2958 return module.implLibraryModule.OutputFiles(tag)
2959 } else {
2960 return nil, nil
2961 }
Paul Duffin46dc45a2020-05-14 15:39:10 +01002962}
2963
Paul Duffineedc5d52020-06-12 17:46:39 +01002964func (module *SdkLibraryImport) GenerateAndroidBuildActions(ctx android.ModuleContext) {
Paul Duffina2ae7e02020-09-11 11:55:00 +01002965 module.generateCommonBuildActions(ctx)
2966
Jeongik Chad5fe8782021-07-08 01:13:11 +09002967 // Assume that source module(sdk_library) is installed in /<sdk_library partition>/framework
2968 module.installFile = android.PathForModuleInstall(ctx, "framework", module.Stem()+".jar")
2969
Paul Duffin0f8faff2020-05-20 16:18:00 +01002970 // Record the paths to the prebuilt stubs library and stubs source.
Colin Cross79c7c262019-04-17 11:11:46 -07002971 ctx.VisitDirectDeps(func(to android.Module) {
2972 tag := ctx.OtherModuleDependencyTag(to)
2973
Paul Duffin0f8faff2020-05-20 16:18:00 +01002974 // Extract information from any of the scope specific dependencies.
2975 if scopeTag, ok := tag.(scopeDependencyTag); ok {
2976 apiScope := scopeTag.apiScope
2977 scopePaths := module.getScopePathsCreateIfNeeded(apiScope)
2978
2979 // Extract information from the dependency. The exact information extracted
2980 // is determined by the nature of the dependency which is determined by the tag.
2981 scopeTag.extractDepInfo(ctx, to, scopePaths)
Paul Duffineedc5d52020-06-12 17:46:39 +01002982 } else if tag == implLibraryTag {
2983 if implLibrary, ok := to.(*Library); ok {
2984 module.implLibraryModule = implLibrary
2985 } else {
2986 ctx.ModuleErrorf("implementation library must be of type *java.Library but was %T", to)
2987 }
2988 } else if tag == xmlPermissionsFileTag {
2989 if xmlPermissionsFileModule, ok := to.(*sdkLibraryXml); ok {
2990 module.xmlPermissionsFileModule = xmlPermissionsFileModule
2991 } else {
2992 ctx.ModuleErrorf("xml permissions file module must be of type *sdkLibraryXml but was %T", to)
2993 }
Colin Cross79c7c262019-04-17 11:11:46 -07002994 }
2995 })
Paul Duffin0f8faff2020-05-20 16:18:00 +01002996
2997 // Populate the scope paths with information from the properties.
2998 for apiScope, scopeProperties := range module.scopeProperties {
2999 if len(scopeProperties.Jars) == 0 {
3000 continue
3001 }
3002
3003 paths := module.getScopePathsCreateIfNeeded(apiScope)
Anton Hanssond78eb762021-09-21 15:25:12 +01003004 paths.annotationsZip = android.OptionalPathForModuleSrc(ctx, scopeProperties.Annotations)
Paul Duffin0f8faff2020-05-20 16:18:00 +01003005 paths.currentApiFilePath = android.OptionalPathForModuleSrc(ctx, scopeProperties.Current_api)
3006 paths.removedApiFilePath = android.OptionalPathForModuleSrc(ctx, scopeProperties.Removed_api)
3007 }
Paul Duffin39853512021-02-26 11:09:39 +00003008
3009 if ctx.Device() {
3010 // If this is a variant created for a prebuilt_apex then use the dex implementation jar
3011 // obtained from the associated deapexer module.
Colin Crossff694a82023-12-13 15:54:49 -08003012 ai, _ := android.ModuleProvider(ctx, android.ApexInfoProvider)
Paul Duffin39853512021-02-26 11:09:39 +00003013 if ai.ForPrebuiltApex {
Paul Duffin39853512021-02-26 11:09:39 +00003014 // Get the path of the dex implementation jar from the `deapexer` module.
Spandan Dasfae468e2023-12-12 23:23:53 +00003015 di, err := android.FindDeapexerProviderForModule(ctx)
3016 if err != nil {
3017 // An error was found, possibly due to multiple apexes in the tree that export this library
3018 // Defer the error till a client tries to call DexJarBuildPath
3019 module.dexJarFileErr = err
Spandan Das3a392012024-01-17 18:26:27 +00003020 module.initHiddenAPIError(err)
Spandan Dasfae468e2023-12-12 23:23:53 +00003021 return
Martin Stjernholm44825602021-09-17 01:44:12 +01003022 }
Spandan Das5be63332023-12-13 00:06:32 +00003023 dexJarFileApexRootRelative := ApexRootRelativePathToJavaLib(module.BaseModuleName())
Jiakai Zhang81e46812023-02-08 21:56:07 +08003024 if dexOutputPath := di.PrebuiltExportPath(dexJarFileApexRootRelative); dexOutputPath != nil {
Martin Stjernholm8be1e6d2021-09-15 03:34:04 +01003025 dexJarFile := makeDexJarPathFromPath(dexOutputPath)
3026 module.dexJarFile = dexJarFile
Jiakai Zhang204356f2021-09-09 08:12:46 +00003027 installPath := android.PathForModuleInPartitionInstall(
Jiakai Zhang81e46812023-02-08 21:56:07 +08003028 ctx, "apex", ai.ApexVariationName, dexJarFileApexRootRelative)
Jiakai Zhang204356f2021-09-09 08:12:46 +00003029 module.installFile = installPath
Martin Stjernholm8be1e6d2021-09-15 03:34:04 +01003030 module.initHiddenAPI(ctx, dexJarFile, module.findScopePaths(apiScopePublic).stubsImplPath[0], nil)
Jiakai Zhang204356f2021-09-09 08:12:46 +00003031
Spandan Dase21a8d42024-01-23 23:56:29 +00003032 module.dexpreopter.installPath = module.dexpreopter.getInstallPath(ctx, android.RemoveOptionalPrebuiltPrefix(ctx.ModuleName()), installPath)
Jiakai Zhang204356f2021-09-09 08:12:46 +00003033 module.dexpreopter.isSDKLibrary = true
Spandan Dase21a8d42024-01-23 23:56:29 +00003034 module.dexpreopter.uncompressedDex = shouldUncompressDex(ctx, android.RemoveOptionalPrebuiltPrefix(ctx.ModuleName()), &module.dexpreopter)
Jiakai Zhang81e46812023-02-08 21:56:07 +08003035
3036 if profilePath := di.PrebuiltExportPath(dexJarFileApexRootRelative + ".prof"); profilePath != nil {
3037 module.dexpreopter.inputProfilePathOnHost = profilePath
3038 }
Paul Duffin39853512021-02-26 11:09:39 +00003039 } else {
3040 // This should never happen as a variant for a prebuilt_apex is only created if the
3041 // prebuilt_apex has been configured to export the java library dex file.
Martin Stjernholm44825602021-09-17 01:44:12 +01003042 ctx.ModuleErrorf("internal error: no dex implementation jar available from prebuilt APEX %s", di.ApexModuleName())
Paul Duffin39853512021-02-26 11:09:39 +00003043 }
3044 }
3045 }
Colin Cross79c7c262019-04-17 11:11:46 -07003046}
3047
Jiyong Parkf1691d22021-03-29 20:11:58 +09003048func (module *SdkLibraryImport) sdkJars(ctx android.BaseModuleContext, sdkVersion android.SdkSpec, headerJars bool) android.Paths {
Paul Duffineedc5d52020-06-12 17:46:39 +01003049
3050 // For consistency with SdkLibrary make the implementation jar available to libraries that
3051 // are within the same APEX.
3052 implLibraryModule := module.implLibraryModule
Colin Cross56a83212020-09-15 18:30:11 -07003053 if implLibraryModule != nil && withinSameApexesAs(ctx, module) {
Paul Duffineedc5d52020-06-12 17:46:39 +01003054 if headerJars {
3055 return implLibraryModule.HeaderJars()
3056 } else {
3057 return implLibraryModule.ImplementationJars()
3058 }
3059 }
3060
Paul Duffin23970f42020-05-20 14:20:02 +01003061 return module.selectHeaderJarsForSdkVersion(ctx, sdkVersion)
Paul Duffin56d44902020-01-31 13:36:25 +00003062}
3063
Colin Cross79c7c262019-04-17 11:11:46 -07003064// to satisfy SdkLibraryDependency interface
Jiyong Parkf1691d22021-03-29 20:11:58 +09003065func (module *SdkLibraryImport) SdkHeaderJars(ctx android.BaseModuleContext, sdkVersion android.SdkSpec) android.Paths {
Colin Cross79c7c262019-04-17 11:11:46 -07003066 // This module is just a wrapper for the prebuilt stubs.
Paul Duffineedc5d52020-06-12 17:46:39 +01003067 return module.sdkJars(ctx, sdkVersion, true)
Colin Cross79c7c262019-04-17 11:11:46 -07003068}
3069
Ulya Trafimovichdbf31662020-12-17 12:07:54 +00003070// to satisfy UsesLibraryDependency interface
Spandan Das59a4a2b2024-01-09 21:35:56 +00003071func (module *SdkLibraryImport) DexJarBuildPath(ctx android.ModuleErrorfContext) OptionalDexJarPath {
Paul Duffin39853512021-02-26 11:09:39 +00003072 // The dex implementation jar extracted from the .apex file should be used in preference to the
3073 // source.
Spandan Dasfae468e2023-12-12 23:23:53 +00003074 if module.dexJarFileErr != nil {
Spandan Das59a4a2b2024-01-09 21:35:56 +00003075 ctx.ModuleErrorf(module.dexJarFileErr.Error())
Spandan Dasfae468e2023-12-12 23:23:53 +00003076 }
Martin Stjernholm8be1e6d2021-09-15 03:34:04 +01003077 if module.dexJarFile.IsSet() {
Paul Duffin39853512021-02-26 11:09:39 +00003078 return module.dexJarFile
3079 }
Paul Duffineedc5d52020-06-12 17:46:39 +01003080 if module.implLibraryModule == nil {
Martin Stjernholm8be1e6d2021-09-15 03:34:04 +01003081 return makeUnsetDexJarPath()
Paul Duffineedc5d52020-06-12 17:46:39 +01003082 } else {
Spandan Das59a4a2b2024-01-09 21:35:56 +00003083 return module.implLibraryModule.DexJarBuildPath(ctx)
Paul Duffineedc5d52020-06-12 17:46:39 +01003084 }
3085}
3086
Ulya Trafimovichdbf31662020-12-17 12:07:54 +00003087// to satisfy UsesLibraryDependency interface
Ulya Trafimovich31e444e2020-08-14 17:32:16 +01003088func (module *SdkLibraryImport) DexJarInstallPath() android.Path {
Jeongik Chad5fe8782021-07-08 01:13:11 +09003089 return module.installFile
Ulya Trafimovich31e444e2020-08-14 17:32:16 +01003090}
3091
Ulya Trafimovichdbf31662020-12-17 12:07:54 +00003092// to satisfy UsesLibraryDependency interface
3093func (module *SdkLibraryImport) ClassLoaderContexts() dexpreopt.ClassLoaderContextMap {
3094 return nil
3095}
3096
Paul Duffineedc5d52020-06-12 17:46:39 +01003097// to satisfy apex.javaDependency interface
3098func (module *SdkLibraryImport) JacocoReportClassesFile() android.Path {
3099 if module.implLibraryModule == nil {
3100 return nil
3101 } else {
3102 return module.implLibraryModule.JacocoReportClassesFile()
3103 }
3104}
3105
3106// to satisfy apex.javaDependency interface
Colin Cross08dca382020-07-21 20:31:17 -07003107func (module *SdkLibraryImport) LintDepSets() LintDepSets {
3108 if module.implLibraryModule == nil {
3109 return LintDepSets{}
3110 } else {
3111 return module.implLibraryModule.LintDepSets()
3112 }
3113}
3114
Spandan Das17854f52022-01-14 21:19:14 +00003115func (module *SdkLibraryImport) GetStrictUpdatabilityLinting() bool {
Jaewoong Jung476b9d62021-05-10 15:30:00 -07003116 if module.implLibraryModule == nil {
3117 return false
3118 } else {
Spandan Das17854f52022-01-14 21:19:14 +00003119 return module.implLibraryModule.GetStrictUpdatabilityLinting()
Jaewoong Jung476b9d62021-05-10 15:30:00 -07003120 }
3121}
3122
Spandan Das17854f52022-01-14 21:19:14 +00003123func (module *SdkLibraryImport) SetStrictUpdatabilityLinting(strictLinting bool) {
Jaewoong Jung476b9d62021-05-10 15:30:00 -07003124 if module.implLibraryModule != nil {
Spandan Das17854f52022-01-14 21:19:14 +00003125 module.implLibraryModule.SetStrictUpdatabilityLinting(strictLinting)
Jaewoong Jung476b9d62021-05-10 15:30:00 -07003126 }
3127}
3128
Colin Cross08dca382020-07-21 20:31:17 -07003129// to satisfy apex.javaDependency interface
Paul Duffineedc5d52020-06-12 17:46:39 +01003130func (module *SdkLibraryImport) Stem() string {
3131 return module.BaseModuleName()
Colin Cross79c7c262019-04-17 11:11:46 -07003132}
Jiyong Parke3833882020-02-17 17:28:10 +09003133
Paul Duffin44b481b2020-06-17 16:59:43 +01003134var _ ApexDependency = (*SdkLibraryImport)(nil)
3135
3136// to satisfy java.ApexDependency interface
3137func (module *SdkLibraryImport) HeaderJars() android.Paths {
3138 if module.implLibraryModule == nil {
3139 return nil
3140 } else {
3141 return module.implLibraryModule.HeaderJars()
3142 }
3143}
3144
3145// to satisfy java.ApexDependency interface
3146func (module *SdkLibraryImport) ImplementationAndResourcesJars() android.Paths {
3147 if module.implLibraryModule == nil {
3148 return nil
3149 } else {
3150 return module.implLibraryModule.ImplementationAndResourcesJars()
3151 }
3152}
3153
Jiakai Zhang204356f2021-09-09 08:12:46 +00003154// to satisfy java.DexpreopterInterface interface
3155func (module *SdkLibraryImport) IsInstallable() bool {
3156 return true
3157}
3158
Paul Duffinfef55002021-06-17 14:56:05 +01003159var _ android.RequiredFilesFromPrebuiltApex = (*SdkLibraryImport)(nil)
3160
Paul Duffinb4bbf2c2021-06-17 15:59:07 +01003161func (module *SdkLibraryImport) RequiredFilesFromPrebuiltApex(ctx android.BaseModuleContext) []string {
Paul Duffinfef55002021-06-17 14:56:05 +01003162 name := module.BaseModuleName()
Jiakai Zhang81e46812023-02-08 21:56:07 +08003163 return requiredFilesFromPrebuiltApexForImport(name, &module.dexpreopter)
Paul Duffinfef55002021-06-17 14:56:05 +01003164}
3165
Spandan Das2ea84dd2024-01-25 22:12:50 +00003166func (j *SdkLibraryImport) UseProfileGuidedDexpreopt() bool {
3167 return proptools.Bool(j.importDexpreoptProperties.Dex_preopt.Profile_guided)
3168}
3169
Jiyong Parke3833882020-02-17 17:28:10 +09003170// java_sdk_library_xml
Jiyong Parke3833882020-02-17 17:28:10 +09003171type sdkLibraryXml struct {
3172 android.ModuleBase
3173 android.DefaultableModuleBase
3174 android.ApexModuleBase
3175
3176 properties sdkLibraryXmlProperties
3177
3178 outputFilePath android.OutputPath
3179 installDirPath android.InstallPath
Colin Cross56a83212020-09-15 18:30:11 -07003180
3181 hideApexVariantFromMake bool
Jiyong Parke3833882020-02-17 17:28:10 +09003182}
3183
3184type sdkLibraryXmlProperties struct {
3185 // canonical name of the lib
3186 Lib_name *string
Pedro Loureiro9956e5e2021-09-07 17:21:59 +00003187
3188 // Signals that this shared library is part of the bootclasspath starting
3189 // on the version indicated in this attribute.
3190 //
3191 // This will make platforms at this level and above to ignore
3192 // <uses-library> tags with this library name because the library is already
3193 // available
3194 On_bootclasspath_since *string
3195
3196 // Signals that this shared library was part of the bootclasspath before
3197 // (but not including) the version indicated in this attribute.
3198 //
3199 // The system will automatically add a <uses-library> tag with this library to
3200 // apps that target any SDK less than the version indicated in this attribute.
3201 On_bootclasspath_before *string
3202
3203 // Indicates that PackageManager should ignore this shared library if the
3204 // platform is below the version indicated in this attribute.
3205 //
3206 // This means that the device won't recognise this library as installed.
3207 Min_device_sdk *string
3208
3209 // Indicates that PackageManager should ignore this shared library if the
3210 // platform is above the version indicated in this attribute.
3211 //
3212 // This means that the device won't recognise this library as installed.
3213 Max_device_sdk *string
Pedro Loureiroc3621422021-09-28 15:40:23 +00003214
3215 // The SdkLibrary's min api level as a string
3216 //
3217 // This value comes from the ApiLevel of the MinSdkVersion property.
3218 Sdk_library_min_api_level *string
Jamie Garsidee570ace2023-11-27 12:07:36 +00003219
3220 // Uses-libs dependencies that the shared library requires to work correctly.
3221 //
3222 // This will add dependency="foo:bar" to the <library> section.
3223 Uses_libs_dependencies []string
Jiyong Parke3833882020-02-17 17:28:10 +09003224}
3225
3226// java_sdk_library_xml builds the permission xml file for a java_sdk_library.
3227// Not to be used directly by users. java_sdk_library internally uses this.
3228func sdkLibraryXmlFactory() android.Module {
3229 module := &sdkLibraryXml{}
3230
3231 module.AddProperties(&module.properties)
3232
3233 android.InitApexModule(module)
3234 android.InitAndroidArchModule(module, android.DeviceSupported, android.MultilibCommon)
3235
3236 return module
3237}
3238
Colin Crossaede88c2020-08-11 12:17:01 -07003239func (module *sdkLibraryXml) UniqueApexVariations() bool {
3240 // sdkLibraryXml needs a unique variation per APEX because the generated XML file contains the path to the
3241 // mounted APEX, which contains the name of the APEX.
3242 return true
3243}
3244
Jiyong Parke3833882020-02-17 17:28:10 +09003245// from android.PrebuiltEtcModule
Jooyung Han0703fd82020-08-26 22:11:53 +09003246func (module *sdkLibraryXml) BaseDir() string {
3247 return "etc"
3248}
3249
3250// from android.PrebuiltEtcModule
Jiyong Parke3833882020-02-17 17:28:10 +09003251func (module *sdkLibraryXml) SubDir() string {
3252 return "permissions"
3253}
3254
ThiƩbaud Weksteen00e8b312024-03-18 14:06:00 +11003255var _ etc.PrebuiltEtcModule = (*sdkLibraryXml)(nil)
3256
Jiyong Parke3833882020-02-17 17:28:10 +09003257// from android.ApexModule
3258func (module *sdkLibraryXml) AvailableFor(what string) bool {
3259 return true
3260}
3261
3262func (module *sdkLibraryXml) DepsMutator(ctx android.BottomUpMutatorContext) {
3263 // do nothing
3264}
3265
Jiyong Park45bf82e2020-12-15 22:29:02 +09003266var _ android.ApexModule = (*sdkLibraryXml)(nil)
3267
3268// Implements android.ApexModule
Dan Albertc8060532020-07-22 22:32:17 -07003269func (module *sdkLibraryXml) ShouldSupportSdkVersion(ctx android.BaseModuleContext,
3270 sdkVersion android.ApiLevel) error {
Jooyung Han749dc692020-04-15 11:03:39 +09003271 // sdkLibraryXml doesn't need to be checked separately because java_sdk_library is checked
3272 return nil
3273}
3274
Jiyong Parke3833882020-02-17 17:28:10 +09003275// File path to the runtime implementation library
Colin Cross56a83212020-09-15 18:30:11 -07003276func (module *sdkLibraryXml) implPath(ctx android.ModuleContext) string {
Jiyong Parke3833882020-02-17 17:28:10 +09003277 implName := proptools.String(module.properties.Lib_name)
Colin Crossff694a82023-12-13 15:54:49 -08003278 if apexInfo, _ := android.ModuleProvider(ctx, android.ApexInfoProvider); !apexInfo.IsForPlatform() {
Colin Crosse07f2312020-08-13 11:24:56 -07003279 // TODO(b/146468504): ApexVariationName() is only a soong module name, not apex name.
Jiyong Parke3833882020-02-17 17:28:10 +09003280 // In most cases, this works fine. But when apex_name is set or override_apex is used
3281 // this can be wrong.
Colin Cross56a83212020-09-15 18:30:11 -07003282 return fmt.Sprintf("/apex/%s/javalib/%s.jar", apexInfo.ApexVariationName, implName)
Jiyong Parke3833882020-02-17 17:28:10 +09003283 }
3284 partition := "system"
3285 if module.SocSpecific() {
3286 partition = "vendor"
3287 } else if module.DeviceSpecific() {
3288 partition = "odm"
3289 } else if module.ProductSpecific() {
3290 partition = "product"
3291 } else if module.SystemExtSpecific() {
3292 partition = "system_ext"
3293 }
3294 return "/" + partition + "/framework/" + implName + ".jar"
3295}
3296
Pedro Loureiro9956e5e2021-09-07 17:21:59 +00003297func formattedOptionalSdkLevelAttribute(ctx android.ModuleContext, attrName string, value *string) string {
3298 if value == nil {
3299 return ""
3300 }
3301 apiLevel, err := android.ApiLevelFromUser(ctx, *value)
3302 if err != nil {
Pedro Loureiroba6682f2021-10-29 09:32:32 +00003303 // attributes in bp files have underscores but in the xml have dashes.
3304 ctx.PropertyErrorf(strings.ReplaceAll(attrName, "-", "_"), err.Error())
Pedro Loureiro9956e5e2021-09-07 17:21:59 +00003305 return ""
3306 }
Pedro Loureirob638c622021-12-22 15:28:05 +00003307 if apiLevel.IsCurrent() {
3308 // passing "current" would always mean a future release, never the current (or the current in
3309 // progress) which means some conditions would never be triggered.
3310 ctx.PropertyErrorf(strings.ReplaceAll(attrName, "-", "_"),
3311 `"current" is not an allowed value for this attribute`)
3312 return ""
3313 }
Pedro Loureiro48991222022-06-17 20:01:21 +00003314 // "safeValue" is safe because it translates finalized codenames to a string
3315 // with their SDK int.
3316 safeValue := apiLevel.String()
3317 return formattedOptionalAttribute(attrName, &safeValue)
Pedro Loureiro9956e5e2021-09-07 17:21:59 +00003318}
3319
3320// formats an attribute for the xml permissions file if the value is not null
3321// returns empty string otherwise
3322func formattedOptionalAttribute(attrName string, value *string) string {
3323 if value == nil {
3324 return ""
3325 }
Paul Duffin1816cde2024-04-10 10:58:21 +01003326 return fmt.Sprintf(" %s=\"%s\"\n", attrName, *value)
Pedro Loureiro9956e5e2021-09-07 17:21:59 +00003327}
3328
Jamie Garsidee570ace2023-11-27 12:07:36 +00003329func formattedDependenciesAttribute(dependencies []string) string {
3330 if dependencies == nil {
3331 return ""
3332 }
Paul Duffin1816cde2024-04-10 10:58:21 +01003333 return fmt.Sprintf(" dependency=\"%s\"\n", strings.Join(dependencies, ":"))
Jamie Garsidee570ace2023-11-27 12:07:36 +00003334}
3335
Pedro Loureiro9956e5e2021-09-07 17:21:59 +00003336func (module *sdkLibraryXml) permissionsContents(ctx android.ModuleContext) string {
3337 libName := proptools.String(module.properties.Lib_name)
3338 libNameAttr := formattedOptionalAttribute("name", &libName)
3339 filePath := module.implPath(ctx)
3340 filePathAttr := formattedOptionalAttribute("file", &filePath)
Pedro Loureiroba6682f2021-10-29 09:32:32 +00003341 implicitFromAttr := formattedOptionalSdkLevelAttribute(ctx, "on-bootclasspath-since", module.properties.On_bootclasspath_since)
3342 implicitUntilAttr := formattedOptionalSdkLevelAttribute(ctx, "on-bootclasspath-before", module.properties.On_bootclasspath_before)
3343 minSdkAttr := formattedOptionalSdkLevelAttribute(ctx, "min-device-sdk", module.properties.Min_device_sdk)
3344 maxSdkAttr := formattedOptionalSdkLevelAttribute(ctx, "max-device-sdk", module.properties.Max_device_sdk)
Jamie Garsidee570ace2023-11-27 12:07:36 +00003345 dependenciesAttr := formattedDependenciesAttribute(module.properties.Uses_libs_dependencies)
Pedro Loureiro196d3e62021-12-22 19:53:01 +00003346 // <library> is understood in all android versions whereas <apex-library> is only understood from API T (and ignored before that).
3347 // 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 +00003348 var libraryTag string
3349 if module.properties.Min_device_sdk != nil {
Paul Duffin1816cde2024-04-10 10:58:21 +01003350 libraryTag = " <apex-library\n"
Pedro Loureiroc3621422021-09-28 15:40:23 +00003351 } else {
Paul Duffin1816cde2024-04-10 10:58:21 +01003352 libraryTag = " <library\n"
Pedro Loureiroc3621422021-09-28 15:40:23 +00003353 }
Pedro Loureiro9956e5e2021-09-07 17:21:59 +00003354
3355 return strings.Join([]string{
Paul Duffin1816cde2024-04-10 10:58:21 +01003356 "<?xml version=\"1.0\" encoding=\"utf-8\"?>\n",
3357 "<!-- Copyright (C) 2018 The Android Open Source Project\n",
3358 "\n",
3359 " Licensed under the Apache License, Version 2.0 (the \"License\");\n",
3360 " you may not use this file except in compliance with the License.\n",
3361 " You may obtain a copy of the License at\n",
3362 "\n",
3363 " http://www.apache.org/licenses/LICENSE-2.0\n",
3364 "\n",
3365 " Unless required by applicable law or agreed to in writing, software\n",
3366 " distributed under the License is distributed on an \"AS IS\" BASIS,\n",
3367 " WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\n",
3368 " See the License for the specific language governing permissions and\n",
3369 " limitations under the License.\n",
3370 "-->\n",
3371 "<permissions>\n",
Pedro Loureiroc3621422021-09-28 15:40:23 +00003372 libraryTag,
Pedro Loureiro9956e5e2021-09-07 17:21:59 +00003373 libNameAttr,
3374 filePathAttr,
3375 implicitFromAttr,
3376 implicitUntilAttr,
3377 minSdkAttr,
3378 maxSdkAttr,
Jamie Garsidee570ace2023-11-27 12:07:36 +00003379 dependenciesAttr,
Paul Duffin1816cde2024-04-10 10:58:21 +01003380 " />\n",
3381 "</permissions>\n",
3382 }, "")
Pedro Loureiro9956e5e2021-09-07 17:21:59 +00003383}
3384
Jiyong Parke3833882020-02-17 17:28:10 +09003385func (module *sdkLibraryXml) GenerateAndroidBuildActions(ctx android.ModuleContext) {
Colin Crossff694a82023-12-13 15:54:49 -08003386 apexInfo, _ := android.ModuleProvider(ctx, android.ApexInfoProvider)
3387 module.hideApexVariantFromMake = !apexInfo.IsForPlatform()
Colin Cross56a83212020-09-15 18:30:11 -07003388
Jiyong Parke3833882020-02-17 17:28:10 +09003389 libName := proptools.String(module.properties.Lib_name)
Pedro Loureiroc3621422021-09-28 15:40:23 +00003390 module.selfValidate(ctx)
Pedro Loureiro9956e5e2021-09-07 17:21:59 +00003391 xmlContent := module.permissionsContents(ctx)
Jiyong Parke3833882020-02-17 17:28:10 +09003392
3393 module.outputFilePath = android.PathForModuleOut(ctx, libName+".xml").OutputPath
Paul Duffin1816cde2024-04-10 10:58:21 +01003394 android.WriteFileRuleVerbatim(ctx, module.outputFilePath, xmlContent)
Jiyong Parke3833882020-02-17 17:28:10 +09003395
3396 module.installDirPath = android.PathForModuleInstall(ctx, "etc", module.SubDir())
Jeongik Cha00e09912024-04-23 05:07:13 +09003397 ctx.PackageFile(module.installDirPath, libName+".xml", module.outputFilePath)
mrziwange2346b82024-06-10 15:09:45 -07003398
3399 ctx.SetOutputFiles(android.OutputPaths{module.outputFilePath}.Paths(), "")
Jiyong Parke3833882020-02-17 17:28:10 +09003400}
3401
3402func (module *sdkLibraryXml) AndroidMkEntries() []android.AndroidMkEntries {
Colin Cross56a83212020-09-15 18:30:11 -07003403 if module.hideApexVariantFromMake {
satayev8f088b02021-12-06 11:40:46 +00003404 return []android.AndroidMkEntries{{
Jiyong Parke3833882020-02-17 17:28:10 +09003405 Disabled: true,
3406 }}
3407 }
3408
satayev8f088b02021-12-06 11:40:46 +00003409 return []android.AndroidMkEntries{{
Jiyong Parke3833882020-02-17 17:28:10 +09003410 Class: "ETC",
3411 OutputFile: android.OptionalPathForPath(module.outputFilePath),
3412 ExtraEntries: []android.AndroidMkExtraEntriesFunc{
Colin Crossaa255532020-07-03 13:18:24 -07003413 func(ctx android.AndroidMkExtraEntriesContext, entries *android.AndroidMkEntries) {
Jiyong Parke3833882020-02-17 17:28:10 +09003414 entries.SetString("LOCAL_MODULE_TAGS", "optional")
Colin Crossc68db4b2021-11-11 18:59:15 -08003415 entries.SetString("LOCAL_MODULE_PATH", module.installDirPath.String())
Jiyong Parke3833882020-02-17 17:28:10 +09003416 entries.SetString("LOCAL_INSTALLED_MODULE_STEM", module.outputFilePath.Base())
3417 },
3418 },
3419 }}
3420}
Paul Duffindd46f712020-02-10 13:37:10 +00003421
Pedro Loureiroc3621422021-09-28 15:40:23 +00003422func (module *sdkLibraryXml) selfValidate(ctx android.ModuleContext) {
3423 module.validateAtLeastTAttributes(ctx)
3424 module.validateMinAndMaxDeviceSdk(ctx)
3425 module.validateMinMaxDeviceSdkAndModuleMinSdk(ctx)
3426 module.validateOnBootclasspathBeforeRequirements(ctx)
3427}
3428
3429func (module *sdkLibraryXml) validateAtLeastTAttributes(ctx android.ModuleContext) {
3430 t := android.ApiLevelOrPanic(ctx, "Tiramisu")
3431 module.attrAtLeastT(ctx, t, module.properties.Min_device_sdk, "min_device_sdk")
3432 module.attrAtLeastT(ctx, t, module.properties.Max_device_sdk, "max_device_sdk")
3433 module.attrAtLeastT(ctx, t, module.properties.On_bootclasspath_before, "on_bootclasspath_before")
3434 module.attrAtLeastT(ctx, t, module.properties.On_bootclasspath_since, "on_bootclasspath_since")
3435}
3436
3437func (module *sdkLibraryXml) attrAtLeastT(ctx android.ModuleContext, t android.ApiLevel, attr *string, attrName string) {
3438 if attr != nil {
3439 if level, err := android.ApiLevelFromUser(ctx, *attr); err == nil {
3440 // we will inform the user of invalid inputs when we try to write the
3441 // permissions xml file so we don't need to do it here
3442 if t.GreaterThan(level) {
3443 ctx.PropertyErrorf(attrName, "Attribute value needs to be at least T")
3444 }
3445 }
3446 }
3447}
3448
3449func (module *sdkLibraryXml) validateMinAndMaxDeviceSdk(ctx android.ModuleContext) {
3450 if module.properties.Min_device_sdk != nil && module.properties.Max_device_sdk != nil {
3451 min, minErr := android.ApiLevelFromUser(ctx, *module.properties.Min_device_sdk)
3452 max, maxErr := android.ApiLevelFromUser(ctx, *module.properties.Max_device_sdk)
3453 if minErr == nil && maxErr == nil {
3454 // we will inform the user of invalid inputs when we try to write the
3455 // permissions xml file so we don't need to do it here
3456 if min.GreaterThan(max) {
3457 ctx.ModuleErrorf("min_device_sdk can't be greater than max_device_sdk")
3458 }
3459 }
3460 }
3461}
3462
3463func (module *sdkLibraryXml) validateMinMaxDeviceSdkAndModuleMinSdk(ctx android.ModuleContext) {
3464 moduleMinApi := android.ApiLevelOrPanic(ctx, *module.properties.Sdk_library_min_api_level)
3465 if module.properties.Min_device_sdk != nil {
3466 api, err := android.ApiLevelFromUser(ctx, *module.properties.Min_device_sdk)
3467 if err == nil {
3468 if moduleMinApi.GreaterThan(api) {
3469 ctx.PropertyErrorf("min_device_sdk", "Can't be less than module's min sdk (%s)", moduleMinApi)
3470 }
3471 }
3472 }
3473 if module.properties.Max_device_sdk != nil {
3474 api, err := android.ApiLevelFromUser(ctx, *module.properties.Max_device_sdk)
3475 if err == nil {
3476 if moduleMinApi.GreaterThan(api) {
3477 ctx.PropertyErrorf("max_device_sdk", "Can't be less than module's min sdk (%s)", moduleMinApi)
3478 }
3479 }
3480 }
3481}
3482
3483func (module *sdkLibraryXml) validateOnBootclasspathBeforeRequirements(ctx android.ModuleContext) {
3484 moduleMinApi := android.ApiLevelOrPanic(ctx, *module.properties.Sdk_library_min_api_level)
3485 if module.properties.On_bootclasspath_before != nil {
3486 t := android.ApiLevelOrPanic(ctx, "Tiramisu")
3487 // if we use the attribute, then we need to do this validation
3488 if moduleMinApi.LessThan(t) {
3489 // if minAPi is < T, then we need to have min_device_sdk (which only accepts T+)
3490 if module.properties.Min_device_sdk == nil {
3491 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")
3492 }
3493 }
3494 }
3495}
3496
Paul Duffindd46f712020-02-10 13:37:10 +00003497type sdkLibrarySdkMemberType struct {
3498 android.SdkMemberTypeBase
3499}
3500
Paul Duffin296701e2021-07-14 10:29:36 +01003501func (s *sdkLibrarySdkMemberType) AddDependencies(ctx android.SdkDependencyContext, dependencyTag blueprint.DependencyTag, names []string) {
3502 ctx.AddVariationDependencies(nil, dependencyTag, names...)
Paul Duffindd46f712020-02-10 13:37:10 +00003503}
3504
3505func (s *sdkLibrarySdkMemberType) IsInstance(module android.Module) bool {
3506 _, ok := module.(*SdkLibrary)
3507 return ok
3508}
3509
3510func (s *sdkLibrarySdkMemberType) AddPrebuiltModule(ctx android.SdkMemberContext, member android.SdkMember) android.BpModule {
3511 return ctx.SnapshotBuilder().AddPrebuiltModule(member, "java_sdk_library_import")
3512}
3513
3514func (s *sdkLibrarySdkMemberType) CreateVariantPropertiesStruct() android.SdkMemberProperties {
3515 return &sdkLibrarySdkMemberProperties{}
3516}
3517
Paul Duffin976b0e52021-04-27 23:20:26 +01003518var javaSdkLibrarySdkMemberType = &sdkLibrarySdkMemberType{
3519 android.SdkMemberTypeBase{
3520 PropertyName: "java_sdk_libs",
3521 SupportsSdk: true,
3522 },
3523}
3524
Paul Duffindd46f712020-02-10 13:37:10 +00003525type sdkLibrarySdkMemberProperties struct {
3526 android.SdkMemberPropertiesBase
3527
Paul Duffine8409952022-09-22 16:24:46 +01003528 // Stem name for files in the sdk snapshot.
3529 //
3530 // This is used to construct the path names of various sdk library files in the sdk snapshot to
3531 // make sure that they match the finalized versions of those files in prebuilts/sdk.
3532 //
3533 // This property is marked as keep so that it will be kept in all instances of this struct, will
3534 // not be cleared but will be copied to common structs. That is needed because this field is used
3535 // to construct many file names for other parts of this struct and so it needs to be present in
3536 // all structs. If it was not marked as keep then it would be cleared in some structs and so would
3537 // be unavailable for generating file names if there were other properties that were still set.
3538 Stem string `sdk:"keep"`
3539
Paul Duffindd46f712020-02-10 13:37:10 +00003540 // Scope to per scope properties.
Paul Duffin106a3a42022-01-27 16:39:06 +00003541 Scopes map[*apiScope]*scopeProperties
Paul Duffindd46f712020-02-10 13:37:10 +00003542
Paul Duffin3d1248c2020-04-09 00:10:17 +01003543 // The Java stubs source files.
3544 Stub_srcs []string
Paul Duffinf7a64332020-05-13 16:54:55 +01003545
3546 // The naming scheme.
3547 Naming_scheme *string
Paul Duffind7eb1c22020-05-26 20:57:10 +01003548
3549 // True if the java_sdk_library_import is for a shared library, false
3550 // otherwise.
3551 Shared_library *bool
Paul Duffina2ae7e02020-09-11 11:55:00 +01003552
Paul Duffin1267d872021-04-16 17:21:36 +01003553 // True if the stub imports should produce dex jars.
3554 Compile_dex *bool
3555
Paul Duffina2ae7e02020-09-11 11:55:00 +01003556 // The paths to the doctag files to add to the prebuilt.
3557 Doctag_paths android.Paths
Paul Duffin869de142021-07-15 14:14:41 +01003558
3559 Permitted_packages []string
Pedro Loureiro9956e5e2021-09-07 17:21:59 +00003560
3561 // Signals that this shared library is part of the bootclasspath starting
3562 // on the version indicated in this attribute.
3563 //
3564 // This will make platforms at this level and above to ignore
3565 // <uses-library> tags with this library name because the library is already
3566 // available
3567 On_bootclasspath_since *string
3568
3569 // Signals that this shared library was part of the bootclasspath before
3570 // (but not including) the version indicated in this attribute.
3571 //
3572 // The system will automatically add a <uses-library> tag with this library to
3573 // apps that target any SDK less than the version indicated in this attribute.
3574 On_bootclasspath_before *string
3575
3576 // Indicates that PackageManager should ignore this shared library if the
3577 // platform is below the version indicated in this attribute.
3578 //
3579 // This means that the device won't recognise this library as installed.
3580 Min_device_sdk *string
3581
3582 // Indicates that PackageManager should ignore this shared library if the
3583 // platform is above the version indicated in this attribute.
3584 //
3585 // This means that the device won't recognise this library as installed.
3586 Max_device_sdk *string
Jiakai Zhang9c4dc192023-02-09 00:09:24 +08003587
3588 DexPreoptProfileGuided *bool `supported_build_releases:"UpsideDownCake+"`
Paul Duffindd46f712020-02-10 13:37:10 +00003589}
3590
3591type scopeProperties struct {
Paul Duffin1fd005d2020-04-09 01:08:11 +01003592 Jars android.Paths
3593 StubsSrcJar android.Path
3594 CurrentApiFile android.Path
3595 RemovedApiFile android.Path
Paul Duffine7babdb2022-02-10 13:06:54 +00003596 AnnotationsZip android.Path `supported_build_releases:"Tiramisu+"`
Paul Duffin1fd005d2020-04-09 01:08:11 +01003597 SdkVersion string
Paul Duffindd46f712020-02-10 13:37:10 +00003598}
3599
3600func (s *sdkLibrarySdkMemberProperties) PopulateFromVariant(ctx android.SdkMemberContext, variant android.Module) {
3601 sdk := variant.(*SdkLibrary)
3602
Paul Duffine8409952022-09-22 16:24:46 +01003603 // Copy the stem name for files in the sdk snapshot.
3604 s.Stem = sdk.distStem()
3605
Paul Duffin106a3a42022-01-27 16:39:06 +00003606 s.Scopes = make(map[*apiScope]*scopeProperties)
Jihoon Kang98aa8fa2024-06-07 11:06:57 +00003607 for _, apiScope := range AllApiScopes {
Paul Duffin803a9562020-05-20 11:52:25 +01003608 paths := sdk.findScopePaths(apiScope)
3609 if paths == nil {
3610 continue
3611 }
3612
Paul Duffindd46f712020-02-10 13:37:10 +00003613 jars := paths.stubsImplPath
3614 if len(jars) > 0 {
3615 properties := scopeProperties{}
3616 properties.Jars = jars
Paul Duffin780c5f42020-05-12 15:52:55 +01003617 properties.SdkVersion = sdk.sdkVersionForStubsLibrary(ctx.SdkModuleContext(), apiScope)
Paul Duffin0f8faff2020-05-20 16:18:00 +01003618 properties.StubsSrcJar = paths.stubsSrcJar.Path()
Paul Duffin10269f12020-06-19 18:39:55 +01003619 if paths.currentApiFilePath.Valid() {
3620 properties.CurrentApiFile = paths.currentApiFilePath.Path()
3621 }
3622 if paths.removedApiFilePath.Valid() {
3623 properties.RemovedApiFile = paths.removedApiFilePath.Path()
3624 }
Anton Hanssond78eb762021-09-21 15:25:12 +01003625 // The annotations zip is only available for modules that set annotations_enabled: true.
3626 if paths.annotationsZip.Valid() {
3627 properties.AnnotationsZip = paths.annotationsZip.Path()
3628 }
Paul Duffin106a3a42022-01-27 16:39:06 +00003629 s.Scopes[apiScope] = &properties
Paul Duffindd46f712020-02-10 13:37:10 +00003630 }
3631 }
3632
Paul Duffindfa131e2020-05-15 20:37:11 +01003633 s.Naming_scheme = sdk.commonSdkLibraryProperties.Naming_scheme
Paul Duffind7eb1c22020-05-26 20:57:10 +01003634 s.Shared_library = proptools.BoolPtr(sdk.sharedLibrary())
Paul Duffin1267d872021-04-16 17:21:36 +01003635 s.Compile_dex = sdk.dexProperties.Compile_dex
Paul Duffina2ae7e02020-09-11 11:55:00 +01003636 s.Doctag_paths = sdk.doctagPaths
Paul Duffin869de142021-07-15 14:14:41 +01003637 s.Permitted_packages = sdk.PermittedPackagesForUpdatableBootJars()
Pedro Loureiro9956e5e2021-09-07 17:21:59 +00003638 s.On_bootclasspath_since = sdk.commonSdkLibraryProperties.On_bootclasspath_since
3639 s.On_bootclasspath_before = sdk.commonSdkLibraryProperties.On_bootclasspath_before
3640 s.Min_device_sdk = sdk.commonSdkLibraryProperties.Min_device_sdk
3641 s.Max_device_sdk = sdk.commonSdkLibraryProperties.Max_device_sdk
Jiakai Zhang9c4dc192023-02-09 00:09:24 +08003642
Jihoon Kanga3a05462024-04-05 00:36:44 +00003643 implLibrary := sdk.getImplLibraryModule()
3644 if implLibrary != nil && implLibrary.dexpreopter.dexpreoptProperties.Dex_preopt_result.Profile_guided {
Jiakai Zhang9c4dc192023-02-09 00:09:24 +08003645 s.DexPreoptProfileGuided = proptools.BoolPtr(true)
3646 }
Paul Duffindd46f712020-02-10 13:37:10 +00003647}
3648
3649func (s *sdkLibrarySdkMemberProperties) AddToPropertySet(ctx android.SdkMemberContext, propertySet android.BpPropertySet) {
Paul Duffinf7a64332020-05-13 16:54:55 +01003650 if s.Naming_scheme != nil {
3651 propertySet.AddProperty("naming_scheme", proptools.String(s.Naming_scheme))
3652 }
Paul Duffind7eb1c22020-05-26 20:57:10 +01003653 if s.Shared_library != nil {
3654 propertySet.AddProperty("shared_library", *s.Shared_library)
3655 }
Paul Duffin1267d872021-04-16 17:21:36 +01003656 if s.Compile_dex != nil {
3657 propertySet.AddProperty("compile_dex", *s.Compile_dex)
3658 }
Paul Duffin869de142021-07-15 14:14:41 +01003659 if len(s.Permitted_packages) > 0 {
3660 propertySet.AddProperty("permitted_packages", s.Permitted_packages)
3661 }
Jiakai Zhang9c4dc192023-02-09 00:09:24 +08003662 dexPreoptSet := propertySet.AddPropertySet("dex_preopt")
3663 if s.DexPreoptProfileGuided != nil {
3664 dexPreoptSet.AddProperty("profile_guided", proptools.Bool(s.DexPreoptProfileGuided))
3665 }
Paul Duffinf7a64332020-05-13 16:54:55 +01003666
Paul Duffine8409952022-09-22 16:24:46 +01003667 stem := s.Stem
3668
Jihoon Kang98aa8fa2024-06-07 11:06:57 +00003669 for _, apiScope := range AllApiScopes {
Paul Duffindd46f712020-02-10 13:37:10 +00003670 if properties, ok := s.Scopes[apiScope]; ok {
Paul Duffin6b836ba2020-05-13 19:19:49 +01003671 scopeSet := propertySet.AddPropertySet(apiScope.propertyName)
Paul Duffindd46f712020-02-10 13:37:10 +00003672
Paul Duffin958806b2022-05-16 13:10:47 +00003673 scopeDir := apiScope.snapshotRelativeDir()
Paul Duffin3d1248c2020-04-09 00:10:17 +01003674
Paul Duffindd46f712020-02-10 13:37:10 +00003675 var jars []string
3676 for _, p := range properties.Jars {
Paul Duffine8409952022-09-22 16:24:46 +01003677 dest := filepath.Join(scopeDir, stem+"-stubs.jar")
Paul Duffindd46f712020-02-10 13:37:10 +00003678 ctx.SnapshotBuilder().CopyToSnapshot(p, dest)
3679 jars = append(jars, dest)
3680 }
3681 scopeSet.AddProperty("jars", jars)
3682
Paul Duffin22628d52021-05-12 23:13:22 +01003683 if ctx.SdkModuleContext().Config().IsEnvTrue("SOONG_SDK_SNAPSHOT_USE_SRCJAR") {
3684 // Copy the stubs source jar into the snapshot zip as is.
Paul Duffine8409952022-09-22 16:24:46 +01003685 srcJarSnapshotPath := filepath.Join(scopeDir, stem+".srcjar")
Paul Duffin22628d52021-05-12 23:13:22 +01003686 ctx.SnapshotBuilder().CopyToSnapshot(properties.StubsSrcJar, srcJarSnapshotPath)
3687 scopeSet.AddProperty("stub_srcs", []string{srcJarSnapshotPath})
3688 } else {
3689 // Merge the stubs source jar into the snapshot zip so that when it is unpacked
3690 // the source files are also unpacked.
Paul Duffine8409952022-09-22 16:24:46 +01003691 snapshotRelativeDir := filepath.Join(scopeDir, stem+"_stub_sources")
Paul Duffin22628d52021-05-12 23:13:22 +01003692 ctx.SnapshotBuilder().UnzipToSnapshot(properties.StubsSrcJar, snapshotRelativeDir)
3693 scopeSet.AddProperty("stub_srcs", []string{snapshotRelativeDir})
3694 }
Paul Duffin3d1248c2020-04-09 00:10:17 +01003695
Paul Duffin1fd005d2020-04-09 01:08:11 +01003696 if properties.CurrentApiFile != nil {
Paul Duffine8409952022-09-22 16:24:46 +01003697 currentApiSnapshotPath := apiScope.snapshotRelativeCurrentApiTxtPath(stem)
Paul Duffin1fd005d2020-04-09 01:08:11 +01003698 ctx.SnapshotBuilder().CopyToSnapshot(properties.CurrentApiFile, currentApiSnapshotPath)
3699 scopeSet.AddProperty("current_api", currentApiSnapshotPath)
3700 }
3701
3702 if properties.RemovedApiFile != nil {
Paul Duffine8409952022-09-22 16:24:46 +01003703 removedApiSnapshotPath := apiScope.snapshotRelativeRemovedApiTxtPath(stem)
Paul Duffin3dbf9fd2020-06-02 13:00:02 +01003704 ctx.SnapshotBuilder().CopyToSnapshot(properties.RemovedApiFile, removedApiSnapshotPath)
Paul Duffin1fd005d2020-04-09 01:08:11 +01003705 scopeSet.AddProperty("removed_api", removedApiSnapshotPath)
3706 }
3707
Anton Hanssond78eb762021-09-21 15:25:12 +01003708 if properties.AnnotationsZip != nil {
Paul Duffine8409952022-09-22 16:24:46 +01003709 annotationsSnapshotPath := filepath.Join(scopeDir, stem+"_annotations.zip")
Anton Hanssond78eb762021-09-21 15:25:12 +01003710 ctx.SnapshotBuilder().CopyToSnapshot(properties.AnnotationsZip, annotationsSnapshotPath)
3711 scopeSet.AddProperty("annotations", annotationsSnapshotPath)
3712 }
3713
Paul Duffindd46f712020-02-10 13:37:10 +00003714 if properties.SdkVersion != "" {
3715 scopeSet.AddProperty("sdk_version", properties.SdkVersion)
3716 }
3717 }
3718 }
3719
Paul Duffina2ae7e02020-09-11 11:55:00 +01003720 if len(s.Doctag_paths) > 0 {
3721 dests := []string{}
3722 for _, p := range s.Doctag_paths {
3723 dest := filepath.Join("doctags", p.Rel())
3724 ctx.SnapshotBuilder().CopyToSnapshot(p, dest)
3725 dests = append(dests, dest)
3726 }
3727 propertySet.AddProperty("doctag_files", dests)
3728 }
Paul Duffindd46f712020-02-10 13:37:10 +00003729}