blob: e6cb6c49be0321b865b5f029bb64b2e8d8570f4b [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
Paul Duffinc8782502020-04-29 20:45:27 +0100121 // The tag to use to depend on the stubs source and API module.
122 stubsSourceAndApiTag scopeDependencyTag
Paul Duffind1b3a922020-01-22 11:57:20 +0000123
Paul Duffin958806b2022-05-16 13:10:47 +0000124 // The tag to use to depend on the module that provides the latest version of the API .txt file.
125 latestApiModuleTag scopeDependencyTag
126
127 // The tag to use to depend on the module that provides the latest version of the API removed.txt
128 // file.
129 latestRemovedApiModuleTag scopeDependencyTag
130
Paul Duffind1b3a922020-01-22 11:57:20 +0000131 // The scope specific prefix to add to the api file base of "current.txt" or "removed.txt".
132 apiFilePrefix string
133
Paul Duffind0b9fca2022-09-30 18:11:41 +0100134 // The scope specific suffix to add to the sdk library module name to construct a scope specific
Paul Duffind1b3a922020-01-22 11:57:20 +0000135 // module name.
136 moduleSuffix string
137
Paul Duffind1b3a922020-01-22 11:57:20 +0000138 // SDK version that the stubs library is built against. Note that this is always
139 // *current. Older stubs library built with a numbered SDK version is created from
140 // the prebuilt jar.
141 sdkVersion string
Paul Duffin1fb487d2020-04-07 18:50:10 +0100142
Paul Duffin15f34ef2020-07-20 18:04:44 +0100143 // The annotation that identifies this API level, empty for the public API scope.
144 annotation string
145
Paul Duffin1fb487d2020-04-07 18:50:10 +0100146 // Extra arguments to pass to droidstubs for this scope.
Paul Duffin0ff08bd2020-04-29 13:30:54 +0100147 //
Paul Duffin15f34ef2020-07-20 18:04:44 +0100148 // This is not used directly but is used to construct the droidstubsArgs.
149 extraArgs []string
Paul Duffin0ff08bd2020-04-29 13:30:54 +0100150
Paul Duffin15f34ef2020-07-20 18:04:44 +0100151 // The args that must be passed to droidstubs to generate the API and stubs source
152 // for this scope, constructed dynamically by initApiScope().
Paul Duffin0ff08bd2020-04-29 13:30:54 +0100153 //
154 // The API only includes the additional members that this scope adds over the scope
155 // that it extends.
Paul Duffin15f34ef2020-07-20 18:04:44 +0100156 //
157 // The stubs source must include the definitions of everything that is in this
158 // api scope and all the scopes that this one extends.
159 droidstubsArgs []string
Paul Duffin0ff08bd2020-04-29 13:30:54 +0100160
Anton Hansson6478ac12020-05-02 11:19:36 +0100161 // Whether the api scope can be treated as unstable, and should skip compat checks.
162 unstable bool
Jihoon Kang1c92c3e2023-03-23 17:44:51 +0000163
164 // Represents the SDK kind of this scope.
165 kind android.SdkKind
Paul Duffind1b3a922020-01-22 11:57:20 +0000166}
167
168// Initialize a scope, creating and adding appropriate dependency tags
169func initApiScope(scope *apiScope) *apiScope {
Paul Duffinc8782502020-04-29 20:45:27 +0100170 name := scope.name
Paul Duffin46dc45a2020-05-14 15:39:10 +0100171 scopeByName[name] = scope
172 allScopeNames = append(allScopeNames, name)
Paul Duffin6b836ba2020-05-13 19:19:49 +0100173 scope.propertyName = strings.ReplaceAll(name, "-", "_")
174 scope.fieldName = proptools.FieldNameForProperty(scope.propertyName)
Jihoon Kangb7431552024-01-22 19:40:08 +0000175 scope.prebuiltStubsTag = scopeDependencyTag{
Paul Duffinc8782502020-04-29 20:45:27 +0100176 name: name + "-stubs",
177 apiScope: scope,
178 depInfoExtractor: (*scopePaths).extractStubsLibraryInfoFromDependency,
Paul Duffind1b3a922020-01-22 11:57:20 +0000179 }
Jihoon Kangbd093452023-12-26 19:08:01 +0000180 scope.everythingStubsTag = scopeDependencyTag{
181 name: name + "-stubs-everything",
182 apiScope: scope,
183 depInfoExtractor: (*scopePaths).extractEverythingStubsLibraryInfoFromDependency,
184 }
185 scope.exportableStubsTag = scopeDependencyTag{
186 name: name + "-stubs-exportable",
187 apiScope: scope,
188 depInfoExtractor: (*scopePaths).extractExportableStubsLibraryInfoFromDependency,
189 }
Paul Duffin0ff08bd2020-04-29 13:30:54 +0100190 scope.stubsSourceTag = scopeDependencyTag{
191 name: name + "-stubs-source",
192 apiScope: scope,
193 depInfoExtractor: (*scopePaths).extractStubsSourceInfoFromDep,
194 }
Paul Duffinc8782502020-04-29 20:45:27 +0100195 scope.stubsSourceAndApiTag = scopeDependencyTag{
196 name: name + "-stubs-source-and-api",
197 apiScope: scope,
198 depInfoExtractor: (*scopePaths).extractStubsSourceAndApiInfoFromApiStubsProvider,
Paul Duffind1b3a922020-01-22 11:57:20 +0000199 }
Paul Duffin958806b2022-05-16 13:10:47 +0000200 scope.latestApiModuleTag = scopeDependencyTag{
201 name: name + "-latest-api",
202 apiScope: scope,
203 depInfoExtractor: (*scopePaths).extractLatestApiPath,
204 }
205 scope.latestRemovedApiModuleTag = scopeDependencyTag{
206 name: name + "-latest-removed-api",
207 apiScope: scope,
208 depInfoExtractor: (*scopePaths).extractLatestRemovedApiPath,
209 }
Paul Duffin0ff08bd2020-04-29 13:30:54 +0100210
211 // To get the args needed to generate the stubs source append all the args from
212 // this scope and all the scopes it extends as each set of args adds additional
213 // members to the stubs.
Paul Duffin15f34ef2020-07-20 18:04:44 +0100214 var scopeSpecificArgs []string
215 if scope.annotation != "" {
216 scopeSpecificArgs = []string{"--show-annotation", scope.annotation}
Paul Duffin0ff08bd2020-04-29 13:30:54 +0100217 }
Paul Duffin15f34ef2020-07-20 18:04:44 +0100218 for s := scope; s != nil; s = s.extends {
219 scopeSpecificArgs = append(scopeSpecificArgs, s.extraArgs...)
Paul Duffin0ff08bd2020-04-29 13:30:54 +0100220
Paul Duffin15f34ef2020-07-20 18:04:44 +0100221 // Ensure that the generated stubs includes all the API elements from the API scope
222 // that this scope extends.
223 if s != scope && s.annotation != "" {
224 scopeSpecificArgs = append(scopeSpecificArgs, "--show-for-stub-purposes-annotation", s.annotation)
225 }
226 }
Paul Duffin0ff08bd2020-04-29 13:30:54 +0100227
Paul Duffind0b9fca2022-09-30 18:11:41 +0100228 // By default, a library that can access a scope can also access the scope it extends.
229 if scope.canAccess == nil {
230 scope.canAccess = scope.extends
231 }
232
Paul Duffin15f34ef2020-07-20 18:04:44 +0100233 // Escape any special characters in the arguments. This is needed because droidstubs
234 // passes these directly to the shell command.
235 scope.droidstubsArgs = proptools.ShellEscapeList(scopeSpecificArgs)
Paul Duffin0ff08bd2020-04-29 13:30:54 +0100236
Paul Duffind1b3a922020-01-22 11:57:20 +0000237 return scope
238}
239
Anton Hansson08f476b2021-04-07 15:32:19 +0100240func (scope *apiScope) stubsLibraryModuleNameSuffix() string {
241 return ".stubs" + scope.moduleSuffix
242}
243
Jihoon Kangfa4a90d2023-12-20 02:53:38 +0000244func (scope *apiScope) exportableStubsLibraryModuleNameSuffix() string {
245 return ".stubs.exportable" + scope.moduleSuffix
246}
247
Jihoon Kang1c92c3e2023-03-23 17:44:51 +0000248func (scope *apiScope) apiLibraryModuleName(baseName string) string {
249 return scope.stubsLibraryModuleName(baseName) + ".from-text"
250}
251
Jihoon Kang1147b312023-06-08 23:25:57 +0000252func (scope *apiScope) sourceStubLibraryModuleName(baseName string) string {
253 return scope.stubsLibraryModuleName(baseName) + ".from-source"
254}
255
Jihoon Kangfa4a90d2023-12-20 02:53:38 +0000256func (scope *apiScope) exportableSourceStubsLibraryModuleName(baseName string) string {
257 return scope.exportableStubsLibraryModuleName(baseName) + ".from-source"
258}
259
Paul Duffinc3091c82020-05-08 14:16:20 +0100260func (scope *apiScope) stubsLibraryModuleName(baseName string) string {
Anton Hansson08f476b2021-04-07 15:32:19 +0100261 return baseName + scope.stubsLibraryModuleNameSuffix()
Paul Duffind1b3a922020-01-22 11:57:20 +0000262}
263
Jihoon Kangfa4a90d2023-12-20 02:53:38 +0000264func (scope *apiScope) exportableStubsLibraryModuleName(baseName string) string {
265 return baseName + scope.exportableStubsLibraryModuleNameSuffix()
266}
267
Paul Duffinc8782502020-04-29 20:45:27 +0100268func (scope *apiScope) stubsSourceModuleName(baseName string) string {
Paul Duffindd9d0742020-05-08 15:52:37 +0100269 return baseName + ".stubs.source" + scope.moduleSuffix
Paul Duffind1b3a922020-01-22 11:57:20 +0000270}
271
Paul Duffin0ff08bd2020-04-29 13:30:54 +0100272func (scope *apiScope) apiModuleName(baseName string) string {
Paul Duffindd9d0742020-05-08 15:52:37 +0100273 return baseName + ".api" + scope.moduleSuffix
Paul Duffin0ff08bd2020-04-29 13:30:54 +0100274}
275
Paul Duffin3375e352020-04-28 10:44:03 +0100276func (scope *apiScope) String() string {
277 return scope.name
278}
279
Paul Duffin958806b2022-05-16 13:10:47 +0000280// snapshotRelativeDir returns the snapshot directory into which the files related to scopes will
281// be stored.
282func (scope *apiScope) snapshotRelativeDir() string {
283 return filepath.Join("sdk_library", scope.name)
284}
285
286// snapshotRelativeCurrentApiTxtPath returns the snapshot path to the API .txt file for the named
287// library.
288func (scope *apiScope) snapshotRelativeCurrentApiTxtPath(name string) string {
289 return filepath.Join(scope.snapshotRelativeDir(), name+".txt")
290}
291
292// snapshotRelativeRemovedApiTxtPath returns the snapshot path to the removed API .txt file for the
293// named library.
294func (scope *apiScope) snapshotRelativeRemovedApiTxtPath(name string) string {
295 return filepath.Join(scope.snapshotRelativeDir(), name+"-removed.txt")
296}
297
Paul Duffind1b3a922020-01-22 11:57:20 +0000298type apiScopes []*apiScope
299
300func (scopes apiScopes) Strings(accessor func(*apiScope) string) []string {
301 var list []string
302 for _, scope := range scopes {
303 list = append(list, accessor(scope))
304 }
305 return list
306}
307
Jihoon Kanga96a7b12023-09-20 23:43:32 +0000308// Method that maps the apiScopes properties to the index of each apiScopes elements.
309// apiScopes property to be used as the key can be specified with the input accessor.
310// Only a string property of apiScope can be used as the key of the map.
311func (scopes apiScopes) MapToIndex(accessor func(*apiScope) string) map[string]int {
312 ret := make(map[string]int)
313 for i, scope := range scopes {
314 ret[accessor(scope)] = i
315 }
316 return ret
317}
318
Jihoon Kang98aa8fa2024-06-07 11:06:57 +0000319func (scopes apiScopes) ConvertStubsLibraryExportableToEverything(name string) string {
320 for _, scope := range scopes {
321 if strings.HasSuffix(name, scope.exportableStubsLibraryModuleNameSuffix()) {
322 return strings.TrimSuffix(name, scope.exportableStubsLibraryModuleNameSuffix()) +
323 scope.stubsLibraryModuleNameSuffix()
324 }
325 }
326 return name
327}
328
Jiyong Parkc678ad32018-04-10 13:07:10 +0900329var (
Paul Duffin46dc45a2020-05-14 15:39:10 +0100330 scopeByName = make(map[string]*apiScope)
331 allScopeNames []string
Paul Duffind1b3a922020-01-22 11:57:20 +0000332 apiScopePublic = initApiScope(&apiScope{
Paul Duffin3375e352020-04-28 10:44:03 +0100333 name: "public",
334
335 // Public scope is enabled by default for both legacy and non-legacy modes.
336 legacyEnabledStatus: func(module *SdkLibrary) bool {
337 return true
338 },
339 defaultEnabledStatus: true,
340
341 scopeSpecificProperties: func(module *SdkLibrary) *ApiScopeProperties {
342 return &module.sdkLibraryProperties.Public
343 },
Paul Duffind1b3a922020-01-22 11:57:20 +0000344 sdkVersion: "current",
Jihoon Kang1c92c3e2023-03-23 17:44:51 +0000345 kind: android.SdkPublic,
Paul Duffind1b3a922020-01-22 11:57:20 +0000346 })
347 apiScopeSystem = initApiScope(&apiScope{
Paul Duffin3375e352020-04-28 10:44:03 +0100348 name: "system",
349 extends: apiScopePublic,
350 legacyEnabledStatus: (*SdkLibrary).generateTestAndSystemScopesByDefault,
351 scopeSpecificProperties: func(module *SdkLibrary) *ApiScopeProperties {
352 return &module.sdkLibraryProperties.System
353 },
Paul Duffin15f34ef2020-07-20 18:04:44 +0100354 apiFilePrefix: "system-",
355 moduleSuffix: ".system",
356 sdkVersion: "system_current",
357 annotation: "android.annotation.SystemApi(client=android.annotation.SystemApi.Client.PRIVILEGED_APPS)",
Jihoon Kang1c92c3e2023-03-23 17:44:51 +0000358 kind: android.SdkSystem,
Paul Duffind1b3a922020-01-22 11:57:20 +0000359 })
360 apiScopeTest = initApiScope(&apiScope{
Paul Duffin3375e352020-04-28 10:44:03 +0100361 name: "test",
Anton Hansson4fe970f2020-10-09 10:16:49 +0100362 extends: apiScopeSystem,
Paul Duffin3375e352020-04-28 10:44:03 +0100363 legacyEnabledStatus: (*SdkLibrary).generateTestAndSystemScopesByDefault,
364 scopeSpecificProperties: func(module *SdkLibrary) *ApiScopeProperties {
365 return &module.sdkLibraryProperties.Test
366 },
Paul Duffin15f34ef2020-07-20 18:04:44 +0100367 apiFilePrefix: "test-",
368 moduleSuffix: ".test",
369 sdkVersion: "test_current",
370 annotation: "android.annotation.TestApi",
371 unstable: true,
Jihoon Kang1c92c3e2023-03-23 17:44:51 +0000372 kind: android.SdkTest,
Paul Duffind1b3a922020-01-22 11:57:20 +0000373 })
Paul Duffin8f265b92020-04-28 14:13:56 +0100374 apiScopeModuleLib = initApiScope(&apiScope{
Paul Duffin6b836ba2020-05-13 19:19:49 +0100375 name: "module-lib",
Paul Duffin8f265b92020-04-28 14:13:56 +0100376 extends: apiScopeSystem,
Paul Duffin0c5bae52020-06-02 13:00:08 +0100377 // The module-lib scope is disabled by default in legacy mode.
Paul Duffin8f265b92020-04-28 14:13:56 +0100378 //
379 // Enabling this would break existing usages.
380 legacyEnabledStatus: func(module *SdkLibrary) bool {
381 return false
382 },
383 scopeSpecificProperties: func(module *SdkLibrary) *ApiScopeProperties {
384 return &module.sdkLibraryProperties.Module_lib
385 },
386 apiFilePrefix: "module-lib-",
387 moduleSuffix: ".module_lib",
388 sdkVersion: "module_current",
Paul Duffin15f34ef2020-07-20 18:04:44 +0100389 annotation: "android.annotation.SystemApi(client=android.annotation.SystemApi.Client.MODULE_LIBRARIES)",
Jihoon Kang1c92c3e2023-03-23 17:44:51 +0000390 kind: android.SdkModule,
Paul Duffin8f265b92020-04-28 14:13:56 +0100391 })
Paul Duffin0c5bae52020-06-02 13:00:08 +0100392 apiScopeSystemServer = initApiScope(&apiScope{
393 name: "system-server",
394 extends: apiScopePublic,
Paul Duffind0b9fca2022-09-30 18:11:41 +0100395
396 // The system-server scope can access the module-lib scope.
397 //
398 // A module that provides a system-server API is appended to the standard bootclasspath that is
399 // used by the system server. So, it should be able to access module-lib APIs provided by
400 // libraries on the bootclasspath.
401 canAccess: apiScopeModuleLib,
402
Paul Duffin0c5bae52020-06-02 13:00:08 +0100403 // The system-server scope is disabled by default in legacy mode.
404 //
405 // Enabling this would break existing usages.
406 legacyEnabledStatus: func(module *SdkLibrary) bool {
407 return false
408 },
409 scopeSpecificProperties: func(module *SdkLibrary) *ApiScopeProperties {
410 return &module.sdkLibraryProperties.System_server
411 },
412 apiFilePrefix: "system-server-",
413 moduleSuffix: ".system_server",
414 sdkVersion: "system_server_current",
Paul Duffin15f34ef2020-07-20 18:04:44 +0100415 annotation: "android.annotation.SystemApi(client=android.annotation.SystemApi.Client.SYSTEM_SERVER)",
416 extraArgs: []string{
417 "--hide-annotation", "android.annotation.Hide",
Paul Duffin0c5bae52020-06-02 13:00:08 +0100418 // com.android.* classes are okay in this interface"
Paul Duffin15f34ef2020-07-20 18:04:44 +0100419 "--hide", "InternalClasses",
Paul Duffin0c5bae52020-06-02 13:00:08 +0100420 },
Jihoon Kang1c92c3e2023-03-23 17:44:51 +0000421 kind: android.SdkSystemServer,
Paul Duffin0c5bae52020-06-02 13:00:08 +0100422 })
Jihoon Kang98aa8fa2024-06-07 11:06:57 +0000423 AllApiScopes = apiScopes{
Paul Duffind1b3a922020-01-22 11:57:20 +0000424 apiScopePublic,
425 apiScopeSystem,
426 apiScopeTest,
Paul Duffin8f265b92020-04-28 14:13:56 +0100427 apiScopeModuleLib,
Paul Duffin0c5bae52020-06-02 13:00:08 +0100428 apiScopeSystemServer,
Paul Duffind1b3a922020-01-22 11:57:20 +0000429 }
Jihoon Kang0c705a42023-08-02 06:44:57 +0000430 apiLibraryAdditionalProperties = map[string]struct {
431 FullApiSurfaceStubLib string
432 AdditionalApiContribution string
433 }{
434 "legacy.i18n.module.platform.api": {
435 FullApiSurfaceStubLib: "legacy.core.platform.api.stubs",
436 AdditionalApiContribution: "i18n.module.public.api.stubs.source.api.contribution",
437 },
438 "stable.i18n.module.platform.api": {
439 FullApiSurfaceStubLib: "stable.core.platform.api.stubs",
440 AdditionalApiContribution: "i18n.module.public.api.stubs.source.api.contribution",
441 },
442 "conscrypt.module.platform.api": {
443 FullApiSurfaceStubLib: "stable.core.platform.api.stubs",
444 AdditionalApiContribution: "conscrypt.module.public.api.stubs.source.api.contribution",
445 },
446 }
Jiyong Parkc678ad32018-04-10 13:07:10 +0900447)
448
Jiyong Park82484c02018-04-23 21:41:26 +0900449var (
450 javaSdkLibrariesLock sync.Mutex
451)
452
Jiyong Parkc678ad32018-04-10 13:07:10 +0900453// TODO: these are big features that are currently missing
Jiyong Park1be96912018-05-28 18:02:19 +0900454// 1) disallowing linking to the runtime shared lib
455// 2) HTML generation
Jiyong Parkc678ad32018-04-10 13:07:10 +0900456
457func init() {
Paul Duffin43dc1cc2019-12-19 11:18:54 +0000458 RegisterSdkLibraryBuildComponents(android.InitRegistrationContext)
Jiyong Parkc678ad32018-04-10 13:07:10 +0900459
Jiyong Park82484c02018-04-23 21:41:26 +0900460 android.RegisterMakeVarsProvider(pctx, func(ctx android.MakeVarsContext) {
461 javaSdkLibraries := javaSdkLibraries(ctx.Config())
462 sort.Strings(*javaSdkLibraries)
463 ctx.Strict("JAVA_SDK_LIBRARIES", strings.Join(*javaSdkLibraries, " "))
464 })
Paul Duffindd46f712020-02-10 13:37:10 +0000465
466 // Register sdk member types.
Paul Duffin976b0e52021-04-27 23:20:26 +0100467 android.RegisterSdkMemberType(javaSdkLibrarySdkMemberType)
Jiyong Parkc678ad32018-04-10 13:07:10 +0900468}
469
Paul Duffin43dc1cc2019-12-19 11:18:54 +0000470func RegisterSdkLibraryBuildComponents(ctx android.RegistrationContext) {
471 ctx.RegisterModuleType("java_sdk_library", SdkLibraryFactory)
472 ctx.RegisterModuleType("java_sdk_library_import", sdkLibraryImportFactory)
473}
474
Paul Duffin3375e352020-04-28 10:44:03 +0100475// Properties associated with each api scope.
476type ApiScopeProperties struct {
477 // Indicates whether the api surface is generated.
478 //
479 // If this is set for any scope then all scopes must explicitly specify if they
480 // are enabled. This is to prevent new usages from depending on legacy behavior.
481 //
482 // Otherwise, if this is not set for any scope then the default behavior is
483 // scope specific so please refer to the scope specific property documentation.
484 Enabled *bool
Paul Duffin87a05a32020-05-12 11:50:28 +0100485
486 // The sdk_version to use for building the stubs.
487 //
488 // If not specified then it will use an sdk_version determined as follows:
Trevor Radcliffedf8aa1f2021-11-04 14:25:39 +0000489 //
Paul Duffin87a05a32020-05-12 11:50:28 +0100490 // 1) If the sdk_version specified on the java_sdk_library is none then this
Trevor Radcliffedf8aa1f2021-11-04 14:25:39 +0000491 // will be none. This is used for java_sdk_library instances that are used
492 // to create stubs that contribute to the core_current sdk version.
493 // 2) Otherwise, it is assumed that this library extends but does not
494 // contribute directly to a specific sdk_version and so this uses the
495 // sdk_version appropriate for the api scope. e.g. public will use
496 // sdk_version: current, system will use sdk_version: system_current, etc.
Paul Duffin87a05a32020-05-12 11:50:28 +0100497 //
498 // This does not affect the sdk_version used for either generating the stubs source
499 // or the API file. They both have to use the same sdk_version as is used for
500 // compiling the implementation library.
501 Sdk_version *string
Mark White9421c4c2023-08-10 00:07:03 +0000502
503 // Extra libs used when compiling stubs for this scope.
504 Libs []string
Paul Duffin3375e352020-04-28 10:44:03 +0100505}
506
Jiyong Parkc678ad32018-04-10 13:07:10 +0900507type sdkLibraryProperties struct {
Anton Hanssonf8ea3722021-09-16 14:24:13 +0100508 // List of source files that are needed to compile the API, but are not part of runtime library.
509 Api_srcs []string `android:"arch_variant"`
510
Paul Duffin5df79302020-05-16 15:52:12 +0100511 // Visibility for impl library module. If not specified then defaults to the
512 // visibility property.
513 Impl_library_visibility []string
514
Paul Duffin4911a892020-04-29 23:35:13 +0100515 // Visibility for stubs library modules. If not specified then defaults to the
516 // visibility property.
517 Stubs_library_visibility []string
518
519 // Visibility for stubs source modules. If not specified then defaults to the
520 // visibility property.
521 Stubs_source_visibility []string
522
Anton Hansson7f66efa2020-10-08 14:47:23 +0100523 // List of Java libraries that will be in the classpath when building the implementation lib
524 Impl_only_libs []string `android:"arch_variant"`
525
Paul Duffin77590a82022-04-28 14:13:30 +0000526 // List of Java libraries that will included in the implementation lib.
527 Impl_only_static_libs []string `android:"arch_variant"`
528
Sundong Ahnf043cf62018-06-25 16:04:37 +0900529 // List of Java libraries that will be in the classpath when building stubs
530 Stub_only_libs []string `android:"arch_variant"`
531
Anton Hanssondae54cd2021-04-21 16:30:10 +0100532 // List of Java libraries that will included in stub libraries
533 Stub_only_static_libs []string `android:"arch_variant"`
534
Paul Duffin7a586d32019-12-30 17:09:34 +0000535 // list of package names that will be documented and publicized as API.
536 // This allows the API to be restricted to a subset of the source files provided.
537 // If this is unspecified then all the source files will be treated as being part
538 // of the API.
Jiyong Parkc678ad32018-04-10 13:07:10 +0900539 Api_packages []string
540
Jiyong Park5a2c9d72018-05-01 22:25:41 +0900541 // list of package names that must be hidden from the API
542 Hidden_api_packages []string
543
Paul Duffin749f98f2019-12-30 17:23:46 +0000544 // the relative path to the directory containing the api specification files.
545 // Defaults to "api".
546 Api_dir *string
547
Paul Duffindfa131e2020-05-15 20:37:11 +0100548 // Determines whether a runtime implementation library is built; defaults to false.
549 //
550 // 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 +0200551 // it is as if shared_library: false, was set.
Paul Duffin43db9be2019-12-30 17:35:49 +0000552 Api_only *bool
553
Paul Duffin11512472019-02-11 15:55:17 +0000554 // local files that are used within user customized droiddoc options.
555 Droiddoc_option_files []string
556
Spandan Das93e95992021-07-29 18:26:39 +0000557 // additional droiddoc options.
Paul Duffin11512472019-02-11 15:55:17 +0000558 // Available variables for substitution:
559 //
560 // $(location <label>): the path to the droiddoc_option_files with name <label>
Sundong Ahndd567f92018-07-31 17:19:11 +0900561 Droiddoc_options []string
562
Paul Duffine22c2ab2020-05-20 19:35:27 +0100563 // is set to true, Metalava will allow framework SDK to contain annotations.
564 Annotations_enabled *bool
565
Sundong Ahn054b19a2018-10-19 13:46:09 +0900566 // a list of top-level directories containing files to merge qualifier annotations
567 // (i.e. those intended to be included in the stubs written) from.
568 Merge_annotations_dirs []string
569
570 // a list of top-level directories containing Java stub files to merge show/hide annotations from.
571 Merge_inclusion_annotations_dirs []string
572
Paul Duffin4f5c1ef2020-11-19 14:53:43 +0000573 // If set to true then don't create dist rules.
574 No_dist *bool
Sundong Ahn80a87b32019-05-13 15:02:50 +0900575
Paul Duffin31310252020-11-20 21:26:20 +0000576 // The stem for the artifacts that are copied to the dist, if not specified
577 // then defaults to the base module name.
578 //
579 // For each scope the following artifacts are copied to the apistubs/<scope>
580 // directory in the dist.
581 // * stubs impl jar -> <dist-stem>.jar
582 // * API specification file -> api/<dist-stem>.txt
583 // * Removed API specification file -> api/<dist-stem>-removed.txt
584 //
585 // Also used to construct the name of the filegroup (created by prebuilt_apis)
586 // that references the latest released API and remove API specification files.
587 // * API specification filegroup -> <dist-stem>.api.<scope>.latest
588 // * Removed API specification filegroup -> <dist-stem>-removed.api.<scope>.latest
Jaewoong Jung1a97ee02021-03-09 13:25:02 -0800589 // * API incompatibilities baseline filegroup -> <dist-stem>-incompatibilities.api.<scope>.latest
Paul Duffin31310252020-11-20 21:26:20 +0000590 Dist_stem *string
591
Colin Cross986b69a2021-06-01 13:13:40 -0700592 // The subdirectory for the artifacts that are copied to the dist directory. If not specified
Colin Cross3dd66252021-06-01 14:05:09 -0700593 // then defaults to "unknown". Should be set to "android" for anything that should be published
Colin Cross986b69a2021-06-01 13:13:40 -0700594 // in the public Android SDK.
595 Dist_group *string
596
Anton Hanssondff2c782020-12-21 17:10:01 +0000597 // A compatibility mode that allows historical API-tracking files to not exist.
598 // Do not use.
599 Unsafe_ignore_missing_latest_api bool
600
Paul Duffin3375e352020-04-28 10:44:03 +0100601 // indicates whether system and test apis should be generated.
602 Generate_system_and_test_apis bool `blueprint:"mutated"`
603
604 // The properties specific to the public api scope
605 //
606 // Unless explicitly specified by using public.enabled the public api scope is
607 // enabled by default in both legacy and non-legacy mode.
608 Public ApiScopeProperties
609
610 // The properties specific to the system api scope
611 //
612 // In legacy mode the system api scope is enabled by default when sdk_version
613 // is set to something other than "none".
614 //
615 // In non-legacy mode the system api scope is disabled by default.
616 System ApiScopeProperties
617
618 // The properties specific to the test api scope
619 //
620 // In legacy mode the test api scope is enabled by default when sdk_version
621 // is set to something other than "none".
622 //
623 // In non-legacy mode the test api scope is disabled by default.
624 Test ApiScopeProperties
Paul Duffin37e0b772019-12-30 17:20:10 +0000625
Paul Duffin0c5bae52020-06-02 13:00:08 +0100626 // The properties specific to the module-lib api scope
Paul Duffin8f265b92020-04-28 14:13:56 +0100627 //
Zi Wangb2179e32023-01-31 15:53:30 -0800628 // Unless explicitly specified by using module_lib.enabled the module_lib api
629 // scope is disabled by default.
Paul Duffin8f265b92020-04-28 14:13:56 +0100630 Module_lib ApiScopeProperties
631
Paul Duffin0c5bae52020-06-02 13:00:08 +0100632 // The properties specific to the system-server api scope
633 //
Zi Wangb2179e32023-01-31 15:53:30 -0800634 // Unless explicitly specified by using system_server.enabled the
635 // system_server api scope is disabled by default.
Paul Duffin0c5bae52020-06-02 13:00:08 +0100636 System_server ApiScopeProperties
637
Jiyong Park932cdfe2020-05-28 00:19:53 +0900638 // Determines if the stubs are preferred over the implementation library
639 // for linking, even when the client doesn't specify sdk_version. When this
640 // is set to true, such clients are provided with the widest API surface that
641 // this lib provides. Note however that this option doesn't affect the clients
642 // that are in the same APEX as this library. In that case, the clients are
643 // always linked with the implementation library. Default is false.
644 Default_to_stubs *bool
645
Paul Duffin160fe412020-05-10 19:32:20 +0100646 // Properties related to api linting.
647 Api_lint struct {
648 // Enable api linting.
649 Enabled *bool
Anton Hanssonfd1c0d22023-11-02 15:18:09 +0000650
651 // If API lint is enabled, this flag controls whether a set of legitimate lint errors
652 // are turned off. The default is true.
653 Legacy_errors_allowed *bool
Paul Duffin160fe412020-05-10 19:32:20 +0100654 }
655
Jihoon Kang80456fd2023-11-15 19:22:14 +0000656 // Determines if the module contributes to any api surfaces.
657 // This property should be set to true only if the module is listed under
658 // frameworks-base-api.bootclasspath in frameworks/base/api/Android.bp.
659 // Otherwise, this property should be set to false.
660 // Defaults to false.
661 Contribute_to_android_api *bool
662
Jihoon Kang6592e872023-12-19 01:13:16 +0000663 // a list of aconfig_declarations module names that the stubs generated in this module
664 // depend on.
665 Aconfig_declarations []string
666
Jiyong Parkc678ad32018-04-10 13:07:10 +0900667 // TODO: determines whether to create HTML doc or not
Paul Duffine8409952022-09-22 16:24:46 +0100668 // Html_doc *bool
Jiyong Parkc678ad32018-04-10 13:07:10 +0900669}
670
Paul Duffin0f8faff2020-05-20 16:18:00 +0100671// Paths to outputs from java_sdk_library and java_sdk_library_import.
672//
673// Fields that are android.Paths are always set (during GenerateAndroidBuildActions).
674// OptionalPaths are always set by java_sdk_library but may not be set by
675// java_sdk_library_import as not all instances provide that information.
Paul Duffind1b3a922020-01-22 11:57:20 +0000676type scopePaths struct {
Paul Duffin0f8faff2020-05-20 16:18:00 +0100677 // The path (represented as Paths for convenience when returning) to the stubs header jar.
678 //
679 // That is the jar that is created by turbine.
680 stubsHeaderPath android.Paths
681
682 // The path (represented as Paths for convenience when returning) to the stubs implementation jar.
683 //
684 // This is not the implementation jar, it still only contains stubs.
685 stubsImplPath android.Paths
686
Paul Duffin1267d872021-04-16 17:21:36 +0100687 // The dex jar for the stubs.
688 //
689 // This is not the implementation jar, it still only contains stubs.
Martin Stjernholm8be1e6d2021-09-15 03:34:04 +0100690 stubsDexJarPath OptionalDexJarPath
Paul Duffin1267d872021-04-16 17:21:36 +0100691
Jihoon Kangbd093452023-12-26 19:08:01 +0000692 // The exportable dex jar for the stubs.
693 // This is not the implementation jar, it still only contains stubs.
694 // Includes unflagged apis and flagged apis enabled by release configurations.
695 exportableStubsDexJarPath OptionalDexJarPath
696
Paul Duffin0f8faff2020-05-20 16:18:00 +0100697 // The API specification file, e.g. system_current.txt.
698 currentApiFilePath android.OptionalPath
699
700 // The specification of API elements removed since the last release.
701 removedApiFilePath android.OptionalPath
702
703 // The stubs source jar.
704 stubsSrcJar android.OptionalPath
Anton Hanssond78eb762021-09-21 15:25:12 +0100705
706 // Extracted annotations.
707 annotationsZip android.OptionalPath
Paul Duffin958806b2022-05-16 13:10:47 +0000708
709 // The path to the latest API file.
Jihoon Kang5623e542024-01-31 23:27:26 +0000710 latestApiPaths android.Paths
Paul Duffin958806b2022-05-16 13:10:47 +0000711
712 // The path to the latest removed API file.
Jihoon Kang5623e542024-01-31 23:27:26 +0000713 latestRemovedApiPaths android.Paths
Paul Duffind1b3a922020-01-22 11:57:20 +0000714}
715
Colin Crossdcf71b22021-02-01 13:59:03 -0800716func (paths *scopePaths) extractStubsLibraryInfoFromDependency(ctx android.ModuleContext, dep android.Module) error {
Colin Cross313aa542023-12-13 13:47:44 -0800717 if lib, ok := android.OtherModuleProvider(ctx, dep, JavaInfoProvider); ok {
Colin Crossdcf71b22021-02-01 13:59:03 -0800718 paths.stubsHeaderPath = lib.HeaderJars
719 paths.stubsImplPath = lib.ImplementationJars
Paul Duffin1267d872021-04-16 17:21:36 +0100720
721 libDep := dep.(UsesLibraryDependency)
Spandan Das59a4a2b2024-01-09 21:35:56 +0000722 paths.stubsDexJarPath = libDep.DexJarBuildPath(ctx)
Jihoon Kangbd093452023-12-26 19:08:01 +0000723 paths.exportableStubsDexJarPath = libDep.DexJarBuildPath(ctx)
724 return nil
725 } else {
726 return fmt.Errorf("expected module that has JavaInfoProvider, e.g. java_library")
727 }
728}
729
730func (paths *scopePaths) extractEverythingStubsLibraryInfoFromDependency(ctx android.ModuleContext, dep android.Module) error {
731 if lib, ok := android.OtherModuleProvider(ctx, dep, JavaInfoProvider); ok {
732 paths.stubsHeaderPath = lib.HeaderJars
Jihoon Kangf55a5f72024-01-08 08:56:20 +0000733 if !ctx.Config().ReleaseHiddenApiExportableStubs() {
734 paths.stubsImplPath = lib.ImplementationJars
735 }
Jihoon Kangbd093452023-12-26 19:08:01 +0000736
737 libDep := dep.(UsesLibraryDependency)
738 paths.stubsDexJarPath = libDep.DexJarBuildPath(ctx)
739 return nil
740 } else {
741 return fmt.Errorf("expected module that has JavaInfoProvider, e.g. java_library")
742 }
743}
744
745func (paths *scopePaths) extractExportableStubsLibraryInfoFromDependency(ctx android.ModuleContext, dep android.Module) error {
Jihoon Kangf55a5f72024-01-08 08:56:20 +0000746 if lib, ok := android.OtherModuleProvider(ctx, dep, JavaInfoProvider); ok {
747 if ctx.Config().ReleaseHiddenApiExportableStubs() {
748 paths.stubsImplPath = lib.ImplementationJars
749 }
750
Jihoon Kangbd093452023-12-26 19:08:01 +0000751 libDep := dep.(UsesLibraryDependency)
752 paths.exportableStubsDexJarPath = libDep.DexJarBuildPath(ctx)
Paul Duffinc8782502020-04-29 20:45:27 +0100753 return nil
754 } else {
Colin Crossdcf71b22021-02-01 13:59:03 -0800755 return fmt.Errorf("expected module that has JavaInfoProvider, e.g. java_library")
Paul Duffinc8782502020-04-29 20:45:27 +0100756 }
757}
758
Jihoon Kangee113282024-01-23 00:16:41 +0000759func (paths *scopePaths) treatDepAsApiStubsProvider(dep android.Module, action func(provider ApiStubsProvider) error) error {
Paul Duffin0ff08bd2020-04-29 13:30:54 +0100760 if apiStubsProvider, ok := dep.(ApiStubsProvider); ok {
Jihoon Kangee113282024-01-23 00:16:41 +0000761 err := action(apiStubsProvider)
762 if err != nil {
763 return err
764 }
Jihoon Kangf55a5f72024-01-08 08:56:20 +0000765 return nil
766 } else {
767 return fmt.Errorf("expected module that implements ExportableApiStubsSrcProvider, e.g. droidstubs")
768 }
769}
770
Jihoon Kangee113282024-01-23 00:16:41 +0000771func (paths *scopePaths) treatDepAsApiStubsSrcProvider(dep android.Module, action func(provider ApiStubsSrcProvider) error) error {
Paul Duffin0f8faff2020-05-20 16:18:00 +0100772 if apiStubsProvider, ok := dep.(ApiStubsSrcProvider); ok {
Jihoon Kangee113282024-01-23 00:16:41 +0000773 err := action(apiStubsProvider)
774 if err != nil {
775 return err
776 }
Paul Duffin0f8faff2020-05-20 16:18:00 +0100777 return nil
778 } else {
779 return fmt.Errorf("expected module that implements ApiStubsSrcProvider, e.g. droidstubs")
780 }
781}
782
Jihoon Kangee113282024-01-23 00:16:41 +0000783func (paths *scopePaths) extractApiInfoFromApiStubsProvider(provider ApiStubsProvider, stubsType StubsType) error {
784 var annotationsZip, currentApiFilePath, removedApiFilePath android.Path
785 annotationsZip, annotationsZipErr := provider.AnnotationsZip(stubsType)
786 currentApiFilePath, currentApiFilePathErr := provider.ApiFilePath(stubsType)
787 removedApiFilePath, removedApiFilePathErr := provider.RemovedApiFilePath(stubsType)
Paul Duffin0ff08bd2020-04-29 13:30:54 +0100788
Jihoon Kangee113282024-01-23 00:16:41 +0000789 combinedError := errors.Join(annotationsZipErr, currentApiFilePathErr, removedApiFilePathErr)
790
791 if combinedError == nil {
792 paths.annotationsZip = android.OptionalPathForPath(annotationsZip)
793 paths.currentApiFilePath = android.OptionalPathForPath(currentApiFilePath)
794 paths.removedApiFilePath = android.OptionalPathForPath(removedApiFilePath)
795 }
796 return combinedError
Jihoon Kangf55a5f72024-01-08 08:56:20 +0000797}
798
Jihoon Kangee113282024-01-23 00:16:41 +0000799func (paths *scopePaths) extractStubsSourceInfoFromApiStubsProviders(provider ApiStubsSrcProvider, stubsType StubsType) error {
800 stubsSrcJar, err := provider.StubsSrcJar(stubsType)
801 if err == nil {
802 paths.stubsSrcJar = android.OptionalPathForPath(stubsSrcJar)
803 }
804 return err
Jihoon Kangf55a5f72024-01-08 08:56:20 +0000805}
806
Colin Crossdcf71b22021-02-01 13:59:03 -0800807func (paths *scopePaths) extractStubsSourceInfoFromDep(ctx android.ModuleContext, dep android.Module) error {
Jihoon Kang2a26b132024-06-24 07:39:40 +0000808 stubsType := Everything
809 if ctx.Config().ReleaseHiddenApiExportableStubs() {
810 stubsType = Exportable
811 }
Jihoon Kangee113282024-01-23 00:16:41 +0000812 return paths.treatDepAsApiStubsSrcProvider(dep, func(provider ApiStubsSrcProvider) error {
Jihoon Kang2a26b132024-06-24 07:39:40 +0000813 return paths.extractStubsSourceInfoFromApiStubsProviders(provider, stubsType)
Paul Duffin0ff08bd2020-04-29 13:30:54 +0100814 })
815}
816
Colin Crossdcf71b22021-02-01 13:59:03 -0800817func (paths *scopePaths) extractStubsSourceAndApiInfoFromApiStubsProvider(ctx android.ModuleContext, dep android.Module) error {
Jihoon Kang2a26b132024-06-24 07:39:40 +0000818 stubsType := Everything
Jihoon Kangf55a5f72024-01-08 08:56:20 +0000819 if ctx.Config().ReleaseHiddenApiExportableStubs() {
Jihoon Kang2a26b132024-06-24 07:39:40 +0000820 stubsType = Exportable
Jihoon Kangf55a5f72024-01-08 08:56:20 +0000821 }
Jihoon Kangee113282024-01-23 00:16:41 +0000822 return paths.treatDepAsApiStubsProvider(dep, func(provider ApiStubsProvider) error {
Jihoon Kang2a26b132024-06-24 07:39:40 +0000823 extractApiInfoErr := paths.extractApiInfoFromApiStubsProvider(provider, stubsType)
824 extractStubsSourceInfoErr := paths.extractStubsSourceInfoFromApiStubsProviders(provider, stubsType)
Jihoon Kangee113282024-01-23 00:16:41 +0000825 return errors.Join(extractApiInfoErr, extractStubsSourceInfoErr)
Paul Duffin0ff08bd2020-04-29 13:30:54 +0100826 })
827}
828
Jihoon Kang5623e542024-01-31 23:27:26 +0000829func extractOutputPaths(dep android.Module) (android.Paths, error) {
Paul Duffin958806b2022-05-16 13:10:47 +0000830 var paths android.Paths
831 if sourceFileProducer, ok := dep.(android.SourceFileProducer); ok {
832 paths = sourceFileProducer.Srcs()
Jihoon Kang5623e542024-01-31 23:27:26 +0000833 return paths, nil
Paul Duffin958806b2022-05-16 13:10:47 +0000834 } else {
Jihoon Kang5623e542024-01-31 23:27:26 +0000835 return nil, fmt.Errorf("module %q does not produce source files", dep)
Paul Duffin958806b2022-05-16 13:10:47 +0000836 }
Paul Duffin958806b2022-05-16 13:10:47 +0000837}
838
839func (paths *scopePaths) extractLatestApiPath(ctx android.ModuleContext, dep android.Module) error {
Jihoon Kang5623e542024-01-31 23:27:26 +0000840 outputPaths, err := extractOutputPaths(dep)
841 paths.latestApiPaths = outputPaths
Paul Duffin958806b2022-05-16 13:10:47 +0000842 return err
843}
844
845func (paths *scopePaths) extractLatestRemovedApiPath(ctx android.ModuleContext, dep android.Module) error {
Jihoon Kang5623e542024-01-31 23:27:26 +0000846 outputPaths, err := extractOutputPaths(dep)
847 paths.latestRemovedApiPaths = outputPaths
Paul Duffin958806b2022-05-16 13:10:47 +0000848 return err
849}
850
Paul Duffin0ff08bd2020-04-29 13:30:54 +0100851type commonToSdkLibraryAndImportProperties struct {
Paul Duffin1b1e8062020-05-08 13:44:43 +0100852 // The naming scheme to use for the components that this module creates.
853 //
Paul Duffinee9ad5d2020-09-11 13:04:05 +0100854 // If not specified then it defaults to "default".
Paul Duffin1b1e8062020-05-08 13:44:43 +0100855 //
856 // This is a temporary mechanism to simplify conversion from separate modules for each
857 // component that follow a different naming pattern to the default one.
858 //
859 // TODO(b/155480189) - Remove once naming inconsistencies have been resolved.
Paul Duffin0ff08bd2020-04-29 13:30:54 +0100860 Naming_scheme *string
Paul Duffindfa131e2020-05-15 20:37:11 +0100861
862 // Specifies whether this module can be used as an Android shared library; defaults
863 // to true.
864 //
865 // An Android shared library is one that can be referenced in a <uses-library> element
866 // in an AndroidManifest.xml.
867 Shared_library *bool
Paul Duffina2ae7e02020-09-11 11:55:00 +0100868
869 // Files containing information about supported java doc tags.
870 Doctag_files []string `android:"path"`
Pedro Loureiro9956e5e2021-09-07 17:21:59 +0000871
872 // Signals that this shared library is part of the bootclasspath starting
873 // on the version indicated in this attribute.
874 //
875 // This will make platforms at this level and above to ignore
876 // <uses-library> tags with this library name because the library is already
877 // available
878 On_bootclasspath_since *string
879
880 // Signals that this shared library was part of the bootclasspath before
881 // (but not including) the version indicated in this attribute.
882 //
883 // The system will automatically add a <uses-library> tag with this library to
884 // apps that target any SDK less than the version indicated in this attribute.
885 On_bootclasspath_before *string
886
887 // Indicates that PackageManager should ignore this shared library if the
888 // platform is below the version indicated in this attribute.
889 //
890 // This means that the device won't recognise this library as installed.
891 Min_device_sdk *string
892
893 // Indicates that PackageManager should ignore this shared library if the
894 // platform is above the version indicated in this attribute.
895 //
896 // This means that the device won't recognise this library as installed.
897 Max_device_sdk *string
Paul Duffin0ff08bd2020-04-29 13:30:54 +0100898}
899
Paul Duffin71b33cc2021-06-23 11:39:47 +0100900// commonSdkLibraryAndImportModule defines the interface that must be provided by a module that
901// embeds the commonToSdkLibraryAndImport struct.
902type commonSdkLibraryAndImportModule interface {
Paul Duffind796f6f2022-11-23 23:06:05 +0000903 android.Module
Paul Duffin71b33cc2021-06-23 11:39:47 +0100904
Spandan Das23956d12024-01-19 00:22:22 +0000905 // Returns the name of the root java_sdk_library that creates the child stub libraries
906 // This is the `name` as it appears in Android.bp, and not the name in Soong's build graph
907 // (with the prebuilt_ prefix)
908 //
909 // e.g. in the following java_sdk_library_import
910 // java_sdk_library_import {
911 // name: "framework-foo.v1",
912 // source_module_name: "framework-foo",
913 // }
914 // the values returned by
915 // 1. Name(): prebuilt_framework-foo.v1 # unique
916 // 2. BaseModuleName(): framework-foo # the source
917 // 3. RootLibraryName: framework-foo.v1 # the undecordated `name` from Android.bp
918 RootLibraryName() string
919}
920
921func (m *SdkLibrary) RootLibraryName() string {
922 return m.BaseModuleName()
923}
924
925func (m *SdkLibraryImport) RootLibraryName() string {
926 // m.BaseModuleName refers to the source of the import
927 // use moduleBase.Name to get the name of the module as it appears in the .bp file
928 return m.ModuleBase.Name()
Paul Duffin71b33cc2021-06-23 11:39:47 +0100929}
930
Paul Duffin56d44902020-01-31 13:36:25 +0000931// Common code between sdk library and sdk library import
932type commonToSdkLibraryAndImport struct {
Paul Duffin71b33cc2021-06-23 11:39:47 +0100933 module commonSdkLibraryAndImportModule
Paul Duffinc3091c82020-05-08 14:16:20 +0100934
Paul Duffin56d44902020-01-31 13:36:25 +0000935 scopePaths map[*apiScope]*scopePaths
Paul Duffin1b1e8062020-05-08 13:44:43 +0100936
937 namingScheme sdkLibraryComponentNamingScheme
938
Paul Duffindfa131e2020-05-15 20:37:11 +0100939 commonSdkLibraryProperties commonToSdkLibraryAndImportProperties
Paul Duffin859fe962020-05-15 10:20:31 +0100940
Paul Duffina2ae7e02020-09-11 11:55:00 +0100941 // Paths to commonSdkLibraryProperties.Doctag_files
942 doctagPaths android.Paths
943
Paul Duffin859fe962020-05-15 10:20:31 +0100944 // Functionality related to this being used as a component of a java_sdk_library.
945 EmbeddableSdkLibraryComponent
Jihoon Kang8479dea2024-04-04 01:19:05 +0000946
947 // Path to the header jars of the implementation library
948 // This is non-empty only when api_only is false.
949 implLibraryHeaderJars android.Paths
Jihoon Kanga3a05462024-04-05 00:36:44 +0000950
951 // The reference to the implementation library created by the source module.
952 // Is nil if the source module does not exist.
953 implLibraryModule *Library
Paul Duffin56d44902020-01-31 13:36:25 +0000954}
955
Paul Duffin71b33cc2021-06-23 11:39:47 +0100956func (c *commonToSdkLibraryAndImport) initCommon(module commonSdkLibraryAndImportModule) {
957 c.module = module
Paul Duffin1b1e8062020-05-08 13:44:43 +0100958
Paul Duffin71b33cc2021-06-23 11:39:47 +0100959 module.AddProperties(&c.commonSdkLibraryProperties)
Paul Duffin859fe962020-05-15 10:20:31 +0100960
961 // Initialize this as an sdk library component.
Paul Duffin71b33cc2021-06-23 11:39:47 +0100962 c.initSdkLibraryComponent(module)
Paul Duffin1b1e8062020-05-08 13:44:43 +0100963}
964
965func (c *commonToSdkLibraryAndImport) initCommonAfterDefaultsApplied(ctx android.DefaultableHookContext) bool {
Paul Duffindfa131e2020-05-15 20:37:11 +0100966 schemeProperty := proptools.StringDefault(c.commonSdkLibraryProperties.Naming_scheme, "default")
Paul Duffin1b1e8062020-05-08 13:44:43 +0100967 switch schemeProperty {
968 case "default":
969 c.namingScheme = &defaultNamingScheme{}
970 default:
971 ctx.PropertyErrorf("naming_scheme", "expected 'default' but was %q", schemeProperty)
972 return false
973 }
974
Spandan Das23956d12024-01-19 00:22:22 +0000975 namePtr := proptools.StringPtr(c.module.RootLibraryName())
Paul Duffin3f0290e2021-06-30 18:25:36 +0100976 c.sdkLibraryComponentProperties.SdkLibraryName = namePtr
977
Paul Duffindfa131e2020-05-15 20:37:11 +0100978 // Only track this sdk library if this can be used as a shared library.
979 if c.sharedLibrary() {
980 // Use the name specified in the module definition as the owner.
Paul Duffin3f0290e2021-06-30 18:25:36 +0100981 c.sdkLibraryComponentProperties.SdkLibraryToImplicitlyTrack = namePtr
Paul Duffindfa131e2020-05-15 20:37:11 +0100982 }
Paul Duffin859fe962020-05-15 10:20:31 +0100983
Paul Duffin1b1e8062020-05-08 13:44:43 +0100984 return true
Paul Duffinc3091c82020-05-08 14:16:20 +0100985}
986
Paul Duffinea8f8082021-06-24 13:25:57 +0100987// uniqueApexVariations provides common implementation of the ApexModule.UniqueApexVariations
988// method.
989func (c *commonToSdkLibraryAndImport) uniqueApexVariations() bool {
990 // A java_sdk_library that is a shared library produces an XML file that makes the shared library
991 // usable from an AndroidManifest.xml's <uses-library> entry. That XML file contains the name of
992 // the APEX and so it needs a unique variation per APEX.
993 return c.sharedLibrary()
994}
995
Paul Duffina2ae7e02020-09-11 11:55:00 +0100996func (c *commonToSdkLibraryAndImport) generateCommonBuildActions(ctx android.ModuleContext) {
997 c.doctagPaths = android.PathsForModuleSrc(ctx, c.commonSdkLibraryProperties.Doctag_files)
998}
999
Jihoon Kanga3a05462024-04-05 00:36:44 +00001000func (c *commonToSdkLibraryAndImport) getImplLibraryModule() *Library {
1001 return c.implLibraryModule
1002}
1003
Paul Duffineedc5d52020-06-12 17:46:39 +01001004// Module name of the runtime implementation library
1005func (c *commonToSdkLibraryAndImport) implLibraryModuleName() string {
Spandan Das23956d12024-01-19 00:22:22 +00001006 return c.module.RootLibraryName() + ".impl"
Paul Duffineedc5d52020-06-12 17:46:39 +01001007}
1008
1009// Module name of the XML file for the lib
1010func (c *commonToSdkLibraryAndImport) xmlPermissionsModuleName() string {
Spandan Das23956d12024-01-19 00:22:22 +00001011 return c.module.RootLibraryName() + sdkXmlFileSuffix
Paul Duffineedc5d52020-06-12 17:46:39 +01001012}
1013
Paul Duffinc3091c82020-05-08 14:16:20 +01001014// Name of the java_library module that compiles the stubs source.
1015func (c *commonToSdkLibraryAndImport) stubsLibraryModuleName(apiScope *apiScope) string {
Spandan Das23956d12024-01-19 00:22:22 +00001016 baseName := c.module.RootLibraryName()
Paul Duffin21787622022-11-25 12:48:20 +00001017 return c.namingScheme.stubsLibraryModuleName(apiScope, baseName)
Paul Duffinc3091c82020-05-08 14:16:20 +01001018}
1019
Jihoon Kangfa4a90d2023-12-20 02:53:38 +00001020// Name of the java_library module that compiles the exportable stubs source.
1021func (c *commonToSdkLibraryAndImport) exportableStubsLibraryModuleName(apiScope *apiScope) string {
Spandan Das23956d12024-01-19 00:22:22 +00001022 baseName := c.module.RootLibraryName()
Jihoon Kangfa4a90d2023-12-20 02:53:38 +00001023 return c.namingScheme.exportableStubsLibraryModuleName(apiScope, baseName)
1024}
1025
Paul Duffinc3091c82020-05-08 14:16:20 +01001026// Name of the droidstubs module that generates the stubs source and may also
1027// generate/check the API.
1028func (c *commonToSdkLibraryAndImport) stubsSourceModuleName(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.stubsSourceModuleName(apiScope, baseName)
Paul Duffinc3091c82020-05-08 14:16:20 +01001031}
1032
Jihoon Kang1c92c3e2023-03-23 17:44:51 +00001033// Name of the java_api_library module that generates the from-text stubs source
1034// and compiles to a jar file.
1035func (c *commonToSdkLibraryAndImport) apiLibraryModuleName(apiScope *apiScope) string {
Spandan Das23956d12024-01-19 00:22:22 +00001036 baseName := c.module.RootLibraryName()
Jihoon Kang1c92c3e2023-03-23 17:44:51 +00001037 return c.namingScheme.apiLibraryModuleName(apiScope, baseName)
1038}
1039
Jihoon Kang1147b312023-06-08 23:25:57 +00001040// Name of the java_library module that compiles the stubs
1041// generated from source Java files.
Jihoon Kangfa4a90d2023-12-20 02:53:38 +00001042func (c *commonToSdkLibraryAndImport) sourceStubsLibraryModuleName(apiScope *apiScope) string {
Spandan Das23956d12024-01-19 00:22:22 +00001043 baseName := c.module.RootLibraryName()
Jihoon Kangfa4a90d2023-12-20 02:53:38 +00001044 return c.namingScheme.sourceStubsLibraryModuleName(apiScope, baseName)
1045}
1046
1047// Name of the java_library module that compiles the exportable stubs
1048// generated from source Java files.
1049func (c *commonToSdkLibraryAndImport) exportableSourceStubsLibraryModuleName(apiScope *apiScope) string {
Spandan Das23956d12024-01-19 00:22:22 +00001050 baseName := c.module.RootLibraryName()
Jihoon Kangfa4a90d2023-12-20 02:53:38 +00001051 return c.namingScheme.exportableSourceStubsLibraryModuleName(apiScope, baseName)
Jihoon Kang1147b312023-06-08 23:25:57 +00001052}
1053
Paul Duffin46dc45a2020-05-14 15:39:10 +01001054// The component names for different outputs of the java_sdk_library.
1055//
1056// They are similar to the names used for the child modules it creates
1057const (
1058 stubsSourceComponentName = "stubs.source"
1059
1060 apiTxtComponentName = "api.txt"
1061
1062 removedApiTxtComponentName = "removed-api.txt"
Anton Hanssond78eb762021-09-21 15:25:12 +01001063
1064 annotationsComponentName = "annotations.zip"
Paul Duffin46dc45a2020-05-14 15:39:10 +01001065)
1066
1067// A regular expression to match tags that reference a specific stubs component.
1068//
1069// It will only match if given a valid scope and a valid component. It is verfy strict
1070// to ensure it does not accidentally match a similar looking tag that should be processed
1071// by the embedded Library.
1072var tagSplitter = func() *regexp.Regexp {
1073 // Given a list of literal string items returns a regular expression that will
1074 // match any one of the items.
1075 choice := func(items ...string) string {
1076 return `\Q` + strings.Join(items, `\E|\Q`) + `\E`
1077 }
1078
1079 // Regular expression to match one of the scopes.
1080 scopesRegexp := choice(allScopeNames...)
1081
1082 // Regular expression to match one of the components.
Anton Hanssond78eb762021-09-21 15:25:12 +01001083 componentsRegexp := choice(stubsSourceComponentName, apiTxtComponentName, removedApiTxtComponentName, annotationsComponentName)
Paul Duffin46dc45a2020-05-14 15:39:10 +01001084
1085 // Regular expression to match any combination of one scope and one component.
1086 return regexp.MustCompile(fmt.Sprintf(`^\.(%s)\.(%s)$`, scopesRegexp, componentsRegexp))
1087}()
1088
1089// For OutputFileProducer interface
1090//
Anton Hanssond78eb762021-09-21 15:25:12 +01001091// .<scope>.<component name>, for all ComponentNames (for example: .public.removed-api.txt)
Paul Duffin46dc45a2020-05-14 15:39:10 +01001092func (c *commonToSdkLibraryAndImport) commonOutputFiles(tag string) (android.Paths, error) {
1093 if groups := tagSplitter.FindStringSubmatch(tag); groups != nil {
1094 scopeName := groups[1]
1095 component := groups[2]
1096
1097 if scope, ok := scopeByName[scopeName]; ok {
1098 paths := c.findScopePaths(scope)
1099 if paths == nil {
Spandan Das23956d12024-01-19 00:22:22 +00001100 return nil, fmt.Errorf("%q does not provide api scope %s", c.module.RootLibraryName(), scopeName)
Paul Duffin46dc45a2020-05-14 15:39:10 +01001101 }
1102
1103 switch component {
1104 case stubsSourceComponentName:
1105 if paths.stubsSrcJar.Valid() {
1106 return android.Paths{paths.stubsSrcJar.Path()}, nil
1107 }
1108
1109 case apiTxtComponentName:
1110 if paths.currentApiFilePath.Valid() {
1111 return android.Paths{paths.currentApiFilePath.Path()}, nil
1112 }
1113
1114 case removedApiTxtComponentName:
1115 if paths.removedApiFilePath.Valid() {
1116 return android.Paths{paths.removedApiFilePath.Path()}, nil
1117 }
Anton Hanssond78eb762021-09-21 15:25:12 +01001118
1119 case annotationsComponentName:
1120 if paths.annotationsZip.Valid() {
1121 return android.Paths{paths.annotationsZip.Path()}, nil
1122 }
Paul Duffin46dc45a2020-05-14 15:39:10 +01001123 }
1124
1125 return nil, fmt.Errorf("%s not available for api scope %s", component, scopeName)
1126 } else {
1127 return nil, fmt.Errorf("unknown scope %s in %s", scope, tag)
1128 }
1129
1130 } else {
Paul Duffina2ae7e02020-09-11 11:55:00 +01001131 switch tag {
1132 case ".doctags":
1133 if c.doctagPaths != nil {
1134 return c.doctagPaths, nil
1135 } else {
Spandan Das23956d12024-01-19 00:22:22 +00001136 return nil, fmt.Errorf("no doctag_files specified on %s", c.module.RootLibraryName())
Paul Duffina2ae7e02020-09-11 11:55:00 +01001137 }
1138 }
Paul Duffin46dc45a2020-05-14 15:39:10 +01001139 return nil, nil
1140 }
1141}
1142
Paul Duffin803a9562020-05-20 11:52:25 +01001143func (c *commonToSdkLibraryAndImport) getScopePathsCreateIfNeeded(scope *apiScope) *scopePaths {
Paul Duffin56d44902020-01-31 13:36:25 +00001144 if c.scopePaths == nil {
1145 c.scopePaths = make(map[*apiScope]*scopePaths)
1146 }
1147 paths := c.scopePaths[scope]
1148 if paths == nil {
1149 paths = &scopePaths{}
1150 c.scopePaths[scope] = paths
1151 }
1152
1153 return paths
1154}
1155
Paul Duffin803a9562020-05-20 11:52:25 +01001156func (c *commonToSdkLibraryAndImport) findScopePaths(scope *apiScope) *scopePaths {
1157 if c.scopePaths == nil {
1158 return nil
1159 }
1160
1161 return c.scopePaths[scope]
1162}
1163
1164// If this does not support the requested api scope then find the closest available
1165// scope it does support. Returns nil if no such scope is available.
1166func (c *commonToSdkLibraryAndImport) findClosestScopePath(scope *apiScope) *scopePaths {
Paul Duffind0b9fca2022-09-30 18:11:41 +01001167 for s := scope; s != nil; s = s.canAccess {
Paul Duffin803a9562020-05-20 11:52:25 +01001168 if paths := c.findScopePaths(s); paths != nil {
1169 return paths
1170 }
1171 }
1172
1173 // This should never happen outside tests as public should be the base scope for every
1174 // scope and is enabled by default.
1175 return nil
1176}
1177
Jiyong Parkf1691d22021-03-29 20:11:58 +09001178func (c *commonToSdkLibraryAndImport) selectHeaderJarsForSdkVersion(ctx android.BaseModuleContext, sdkVersion android.SdkSpec) android.Paths {
Paul Duffinb05d4292020-05-20 12:19:10 +01001179
1180 // If a specific numeric version has been requested then use prebuilt versions of the sdk.
Jiyong Park54105c42021-03-31 18:17:53 +09001181 if !sdkVersion.ApiLevel.IsPreview() {
Spandan Das23956d12024-01-19 00:22:22 +00001182 return PrebuiltJars(ctx, c.module.RootLibraryName(), sdkVersion)
Paul Duffinb05d4292020-05-20 12:19:10 +01001183 }
1184
Paul Duffin1267d872021-04-16 17:21:36 +01001185 paths := c.selectScopePaths(ctx, sdkVersion.Kind)
1186 if paths == nil {
1187 return nil
1188 }
1189
1190 return paths.stubsHeaderPath
1191}
1192
1193// selectScopePaths returns the *scopePaths appropriate for the specific kind.
1194//
1195// If the module does not support the specific kind then it will return the *scopePaths for the
1196// closest kind which is a subset of the requested kind. e.g. if requesting android.SdkModule then
1197// it will return *scopePaths for android.SdkSystem if available or android.SdkPublic of not.
1198func (c *commonToSdkLibraryAndImport) selectScopePaths(ctx android.BaseModuleContext, kind android.SdkKind) *scopePaths {
Paul Duffin32cf58a2021-05-18 16:32:50 +01001199 apiScope := sdkKindToApiScope(kind)
Paul Duffinb05d4292020-05-20 12:19:10 +01001200
Paul Duffin803a9562020-05-20 11:52:25 +01001201 paths := c.findClosestScopePath(apiScope)
1202 if paths == nil {
1203 var scopes []string
Jihoon Kang98aa8fa2024-06-07 11:06:57 +00001204 for _, s := range AllApiScopes {
Paul Duffin803a9562020-05-20 11:52:25 +01001205 if c.findScopePaths(s) != nil {
1206 scopes = append(scopes, s.name)
1207 }
1208 }
Spandan Das23956d12024-01-19 00:22:22 +00001209 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 +01001210 return nil
1211 }
1212
Paul Duffin1267d872021-04-16 17:21:36 +01001213 return paths
1214}
1215
Paul Duffin32cf58a2021-05-18 16:32:50 +01001216// sdkKindToApiScope maps from android.SdkKind to apiScope.
1217func sdkKindToApiScope(kind android.SdkKind) *apiScope {
1218 var apiScope *apiScope
1219 switch kind {
1220 case android.SdkSystem:
1221 apiScope = apiScopeSystem
1222 case android.SdkModule:
1223 apiScope = apiScopeModuleLib
1224 case android.SdkTest:
1225 apiScope = apiScopeTest
1226 case android.SdkSystemServer:
1227 apiScope = apiScopeSystemServer
1228 default:
1229 apiScope = apiScopePublic
1230 }
1231 return apiScope
1232}
1233
Paul Duffin1267d872021-04-16 17:21:36 +01001234// to satisfy SdkLibraryDependency interface
Martin Stjernholm8be1e6d2021-09-15 03:34:04 +01001235func (c *commonToSdkLibraryAndImport) SdkApiStubDexJar(ctx android.BaseModuleContext, kind android.SdkKind) OptionalDexJarPath {
Paul Duffin1267d872021-04-16 17:21:36 +01001236 paths := c.selectScopePaths(ctx, kind)
1237 if paths == nil {
Martin Stjernholm8be1e6d2021-09-15 03:34:04 +01001238 return makeUnsetDexJarPath()
Paul Duffin1267d872021-04-16 17:21:36 +01001239 }
1240
1241 return paths.stubsDexJarPath
Paul Duffinb05d4292020-05-20 12:19:10 +01001242}
1243
Paul Duffin32cf58a2021-05-18 16:32:50 +01001244// to satisfy SdkLibraryDependency interface
Jihoon Kangbd093452023-12-26 19:08:01 +00001245func (c *commonToSdkLibraryAndImport) SdkApiExportableStubDexJar(ctx android.BaseModuleContext, kind android.SdkKind) OptionalDexJarPath {
1246 paths := c.selectScopePaths(ctx, kind)
1247 if paths == nil {
1248 return makeUnsetDexJarPath()
1249 }
1250
1251 return paths.exportableStubsDexJarPath
1252}
1253
1254// to satisfy SdkLibraryDependency interface
Paul Duffin32cf58a2021-05-18 16:32:50 +01001255func (c *commonToSdkLibraryAndImport) SdkRemovedTxtFile(ctx android.BaseModuleContext, kind android.SdkKind) android.OptionalPath {
1256 apiScope := sdkKindToApiScope(kind)
1257 paths := c.findScopePaths(apiScope)
1258 if paths == nil {
1259 return android.OptionalPath{}
1260 }
1261
1262 return paths.removedApiFilePath
1263}
1264
Paul Duffin859fe962020-05-15 10:20:31 +01001265func (c *commonToSdkLibraryAndImport) sdkComponentPropertiesForChildLibrary() interface{} {
1266 componentProps := &struct {
Paul Duffin3f0290e2021-06-30 18:25:36 +01001267 SdkLibraryName *string
Paul Duffin859fe962020-05-15 10:20:31 +01001268 SdkLibraryToImplicitlyTrack *string
Paul Duffindfa131e2020-05-15 20:37:11 +01001269 }{}
1270
Spandan Das23956d12024-01-19 00:22:22 +00001271 namePtr := proptools.StringPtr(c.module.RootLibraryName())
Paul Duffin3f0290e2021-06-30 18:25:36 +01001272 componentProps.SdkLibraryName = namePtr
1273
Paul Duffindfa131e2020-05-15 20:37:11 +01001274 if c.sharedLibrary() {
Paul Duffin859fe962020-05-15 10:20:31 +01001275 // Mark the stubs library as being components of this java_sdk_library so that
1276 // any app that includes code which depends (directly or indirectly) on the stubs
1277 // library will have the appropriate <uses-library> invocation inserted into its
1278 // manifest if necessary.
Paul Duffin3f0290e2021-06-30 18:25:36 +01001279 componentProps.SdkLibraryToImplicitlyTrack = namePtr
Paul Duffin859fe962020-05-15 10:20:31 +01001280 }
1281
1282 return componentProps
1283}
1284
Paul Duffindfa131e2020-05-15 20:37:11 +01001285func (c *commonToSdkLibraryAndImport) sharedLibrary() bool {
1286 return proptools.BoolDefault(c.commonSdkLibraryProperties.Shared_library, true)
1287}
1288
Paul Duffinf4600f62021-05-13 22:34:45 +01001289// Check if the stub libraries should be compiled for dex
1290func (c *commonToSdkLibraryAndImport) stubLibrariesCompiledForDex() bool {
1291 // Always compile the dex file files for the stub libraries if they will be used on the
1292 // bootclasspath.
1293 return !c.sharedLibrary()
1294}
1295
Paul Duffin859fe962020-05-15 10:20:31 +01001296// Properties related to the use of a module as an component of a java_sdk_library.
1297type SdkLibraryComponentProperties struct {
Paul Duffin3f0290e2021-06-30 18:25:36 +01001298 // The name of the java_sdk_library/_import module.
1299 SdkLibraryName *string `blueprint:"mutated"`
Paul Duffin859fe962020-05-15 10:20:31 +01001300
1301 // The name of the java_sdk_library/_import to add to a <uses-library> entry
1302 // in the AndroidManifest.xml of any Android app that includes code that references
1303 // this module. If not set then no java_sdk_library/_import is tracked.
1304 SdkLibraryToImplicitlyTrack *string `blueprint:"mutated"`
1305}
1306
1307// Structure to be embedded in a module struct that needs to support the
1308// SdkLibraryComponentDependency interface.
1309type EmbeddableSdkLibraryComponent struct {
1310 sdkLibraryComponentProperties SdkLibraryComponentProperties
1311}
1312
Paul Duffin71b33cc2021-06-23 11:39:47 +01001313func (e *EmbeddableSdkLibraryComponent) initSdkLibraryComponent(module android.Module) {
1314 module.AddProperties(&e.sdkLibraryComponentProperties)
Paul Duffin859fe962020-05-15 10:20:31 +01001315}
1316
1317// to satisfy SdkLibraryComponentDependency
Paul Duffin3f0290e2021-06-30 18:25:36 +01001318func (e *EmbeddableSdkLibraryComponent) SdkLibraryName() *string {
1319 return e.sdkLibraryComponentProperties.SdkLibraryName
1320}
1321
1322// to satisfy SdkLibraryComponentDependency
Ulya Trafimovich39b437b2020-09-23 16:42:35 +01001323func (e *EmbeddableSdkLibraryComponent) OptionalSdkLibraryImplementation() *string {
Ulya Trafimovich78645fb2021-07-16 15:29:25 +01001324 // For shared libraries, this is the same as the SDK library name. If a Java library or app
1325 // depends on a component library (e.g. a stub library) it still needs to know the name of the
1326 // run-time library and the corresponding module that provides the implementation. This name is
1327 // passed to manifest_fixer (to be added to AndroidManifest.xml) and added to CLC (to be used
1328 // in dexpreopt).
1329 //
1330 // For non-shared SDK (component or not) libraries this returns `nil`, as they are not
1331 // <uses-library> and should not be added to the manifest or to CLC.
Ulya Trafimovich39b437b2020-09-23 16:42:35 +01001332 return e.sdkLibraryComponentProperties.SdkLibraryToImplicitlyTrack
1333}
1334
Paul Duffin859fe962020-05-15 10:20:31 +01001335// Implemented by modules that are (or possibly could be) a component of a java_sdk_library
1336// (including the java_sdk_library) itself.
1337type SdkLibraryComponentDependency interface {
Ulya Trafimovich31e444e2020-08-14 17:32:16 +01001338 UsesLibraryDependency
1339
Paul Duffin3f0290e2021-06-30 18:25:36 +01001340 // SdkLibraryName returns the name of the java_sdk_library/_import module.
1341 SdkLibraryName() *string
1342
Ulya Trafimovich39b437b2020-09-23 16:42:35 +01001343 // The name of the implementation library for the optional SDK library or nil, if there isn't one.
1344 OptionalSdkLibraryImplementation() *string
Paul Duffin859fe962020-05-15 10:20:31 +01001345}
1346
1347// Make sure that all the module types that are components of java_sdk_library/_import
1348// and which can be referenced (directly or indirectly) from an android app implement
1349// the SdkLibraryComponentDependency interface.
1350var _ SdkLibraryComponentDependency = (*Library)(nil)
1351var _ SdkLibraryComponentDependency = (*Import)(nil)
1352var _ SdkLibraryComponentDependency = (*SdkLibrary)(nil)
Paul Duffineedc5d52020-06-12 17:46:39 +01001353var _ SdkLibraryComponentDependency = (*SdkLibraryImport)(nil)
Paul Duffin859fe962020-05-15 10:20:31 +01001354
Paul Duffin32cf58a2021-05-18 16:32:50 +01001355// Provides access to sdk_version related files, e.g. header and implementation jars.
Paul Duffin859fe962020-05-15 10:20:31 +01001356type SdkLibraryDependency interface {
1357 SdkLibraryComponentDependency
1358
1359 // Get the header jars appropriate for the supplied sdk_version.
1360 //
1361 // These are turbine generated jars so they only change if the externals of the
1362 // class changes but it does not contain and implementation or JavaDoc.
Jiyong Parkf1691d22021-03-29 20:11:58 +09001363 SdkHeaderJars(ctx android.BaseModuleContext, sdkVersion android.SdkSpec) android.Paths
Paul Duffin859fe962020-05-15 10:20:31 +01001364
Jihoon Kangbd093452023-12-26 19:08:01 +00001365 // SdkApiStubDexJar returns the dex jar for the stubs for the prebuilt
1366 // java_sdk_library_import module. It is needed by the hiddenapi processing tool which
1367 // processes dex files.
Martin Stjernholm8be1e6d2021-09-15 03:34:04 +01001368 SdkApiStubDexJar(ctx android.BaseModuleContext, kind android.SdkKind) OptionalDexJarPath
Paul Duffinf4600f62021-05-13 22:34:45 +01001369
Jihoon Kangbd093452023-12-26 19:08:01 +00001370 // SdkApiExportableStubDexJar returns the exportable dex jar for the stubs for
1371 // java_sdk_library module. It is needed by the hiddenapi processing tool which processes
1372 // dex files.
1373 SdkApiExportableStubDexJar(ctx android.BaseModuleContext, kind android.SdkKind) OptionalDexJarPath
1374
Paul Duffin32cf58a2021-05-18 16:32:50 +01001375 // SdkRemovedTxtFile returns the optional path to the removed.txt file for the specified sdk kind.
1376 SdkRemovedTxtFile(ctx android.BaseModuleContext, kind android.SdkKind) android.OptionalPath
1377
Paul Duffinf4600f62021-05-13 22:34:45 +01001378 // sharedLibrary returns true if this can be used as a shared library.
1379 sharedLibrary() bool
Jihoon Kanga3a05462024-04-05 00:36:44 +00001380
1381 getImplLibraryModule() *Library
Paul Duffin859fe962020-05-15 10:20:31 +01001382}
1383
Inseob Kimc0907f12019-02-08 21:00:45 +09001384type SdkLibrary struct {
Sundong Ahn054b19a2018-10-19 13:46:09 +09001385 Library
Jiyong Parkc678ad32018-04-10 13:07:10 +09001386
Sundong Ahn054b19a2018-10-19 13:46:09 +09001387 sdkLibraryProperties sdkLibraryProperties
Jiyong Parkc678ad32018-04-10 13:07:10 +09001388
Paul Duffin3375e352020-04-28 10:44:03 +01001389 // Map from api scope to the scope specific property structure.
1390 scopeToProperties map[*apiScope]*ApiScopeProperties
1391
Paul Duffin56d44902020-01-31 13:36:25 +00001392 commonToSdkLibraryAndImport
Jihoon Kanga3a05462024-04-05 00:36:44 +00001393
1394 builtInstalledForApex []dexpreopterInstall
Jiyong Parkc678ad32018-04-10 13:07:10 +09001395}
1396
Inseob Kimc0907f12019-02-08 21:00:45 +09001397var _ SdkLibraryDependency = (*SdkLibrary)(nil)
Colin Cross897d2ed2019-02-11 14:03:51 -08001398
Paul Duffin3375e352020-04-28 10:44:03 +01001399func (module *SdkLibrary) generateTestAndSystemScopesByDefault() bool {
1400 return module.sdkLibraryProperties.Generate_system_and_test_apis
1401}
1402
Jihoon Kanga3a05462024-04-05 00:36:44 +00001403func (module *SdkLibrary) DexJarBuildPath(ctx android.ModuleErrorfContext) OptionalDexJarPath {
1404 if module.implLibraryModule != nil {
1405 return module.implLibraryModule.DexJarBuildPath(ctx)
1406 }
1407 return makeUnsetDexJarPath()
1408}
1409
1410func (module *SdkLibrary) DexJarInstallPath() android.Path {
1411 if module.implLibraryModule != nil {
1412 return module.implLibraryModule.DexJarInstallPath()
1413 }
1414 return nil
1415}
1416
Paul Duffin3375e352020-04-28 10:44:03 +01001417func (module *SdkLibrary) getGeneratedApiScopes(ctx android.EarlyModuleContext) apiScopes {
1418 // Check to see if any scopes have been explicitly enabled. If any have then all
1419 // must be.
1420 anyScopesExplicitlyEnabled := false
Jihoon Kang98aa8fa2024-06-07 11:06:57 +00001421 for _, scope := range AllApiScopes {
Paul Duffin3375e352020-04-28 10:44:03 +01001422 scopeProperties := module.scopeToProperties[scope]
1423 if scopeProperties.Enabled != nil {
1424 anyScopesExplicitlyEnabled = true
1425 break
1426 }
Paul Duffind1b3a922020-01-22 11:57:20 +00001427 }
Paul Duffin3375e352020-04-28 10:44:03 +01001428
1429 var generatedScopes apiScopes
1430 enabledScopes := make(map[*apiScope]struct{})
Jihoon Kang98aa8fa2024-06-07 11:06:57 +00001431 for _, scope := range AllApiScopes {
Paul Duffin3375e352020-04-28 10:44:03 +01001432 scopeProperties := module.scopeToProperties[scope]
1433 // If any scopes are explicitly enabled then ignore the legacy enabled status.
1434 // This is to ensure that any new usages of this module type do not rely on legacy
1435 // behaviour.
1436 defaultEnabledStatus := false
1437 if anyScopesExplicitlyEnabled {
1438 defaultEnabledStatus = scope.defaultEnabledStatus
1439 } else {
1440 defaultEnabledStatus = scope.legacyEnabledStatus(module)
1441 }
1442 enabled := proptools.BoolDefault(scopeProperties.Enabled, defaultEnabledStatus)
1443 if enabled {
1444 enabledScopes[scope] = struct{}{}
1445 generatedScopes = append(generatedScopes, scope)
1446 }
1447 }
1448
1449 // Now check to make sure that any scope that is extended by an enabled scope is also
1450 // enabled.
Jihoon Kang98aa8fa2024-06-07 11:06:57 +00001451 for _, scope := range AllApiScopes {
Paul Duffin3375e352020-04-28 10:44:03 +01001452 if _, ok := enabledScopes[scope]; ok {
1453 extends := scope.extends
1454 if extends != nil {
1455 if _, ok := enabledScopes[extends]; !ok {
1456 ctx.ModuleErrorf("enabled api scope %q depends on disabled scope %q", scope, extends)
1457 }
1458 }
1459 }
1460 }
1461
1462 return generatedScopes
Paul Duffind1b3a922020-01-22 11:57:20 +00001463}
1464
satayev758968a2021-12-06 11:42:40 +00001465var _ android.ModuleWithMinSdkVersionCheck = (*SdkLibrary)(nil)
1466
satayev8f088b02021-12-06 11:40:46 +00001467func (module *SdkLibrary) CheckMinSdkVersion(ctx android.ModuleContext) {
Jihoon Kanga3a05462024-04-05 00:36:44 +00001468 CheckMinSdkVersion(ctx, &module.Library)
1469}
1470
1471func CheckMinSdkVersion(ctx android.ModuleContext, module *Library) {
Spandan Das8c9ae7e2023-03-03 21:20:36 +00001472 android.CheckMinSdkVersion(ctx, module.MinSdkVersion(ctx), func(c android.ModuleContext, do android.PayloadDepsCallback) {
satayev8f088b02021-12-06 11:40:46 +00001473 ctx.WalkDeps(func(child android.Module, parent android.Module) bool {
1474 isExternal := !module.depIsInSameApex(ctx, child)
1475 if am, ok := child.(android.ApexModule); ok {
1476 if !do(ctx, parent, am, isExternal) {
1477 return false
1478 }
1479 }
1480 return !isExternal
1481 })
1482 })
1483}
1484
Paul Duffineedc5d52020-06-12 17:46:39 +01001485type sdkLibraryComponentTag struct {
1486 blueprint.BaseDependencyTag
1487 name string
1488}
1489
1490// Mark this tag so dependencies that use it are excluded from visibility enforcement.
1491func (t sdkLibraryComponentTag) ExcludeFromVisibilityEnforcement() {}
1492
1493var xmlPermissionsFileTag = sdkLibraryComponentTag{name: "xml-permissions-file"}
Paul Duffine74ac732020-02-06 13:51:46 +00001494
Jiyong Parke3833882020-02-17 17:28:10 +09001495func IsXmlPermissionsFileDepTag(depTag blueprint.DependencyTag) bool {
Paul Duffineedc5d52020-06-12 17:46:39 +01001496 if dt, ok := depTag.(sdkLibraryComponentTag); ok {
Jiyong Parke3833882020-02-17 17:28:10 +09001497 return dt == xmlPermissionsFileTag
1498 }
1499 return false
1500}
1501
Paul Duffineedc5d52020-06-12 17:46:39 +01001502var implLibraryTag = sdkLibraryComponentTag{name: "impl-library"}
Paul Duffin5df79302020-05-16 15:52:12 +01001503
Jeongik Chaaaa6dcd2024-05-22 00:41:28 +09001504var _ android.InstallNeededDependencyTag = sdkLibraryComponentTag{}
1505
Jihoon Kang46d66de2024-05-22 22:42:39 +00001506// To satisfy the CopyDirectlyInAnyApexTag interface. Implementation library of the sdk library
1507// in an apex is considered to be directly in the apex, as if it was listed in java_libs.
1508func (t sdkLibraryComponentTag) CopyDirectlyInAnyApex() {}
1509
1510var _ android.CopyDirectlyInAnyApexTag = implLibraryTag
1511
Jeongik Chaaaa6dcd2024-05-22 00:41:28 +09001512func (t sdkLibraryComponentTag) InstallDepNeeded() bool {
1513 return t.name == "xml-permissions-file" || t.name == "impl-library"
1514}
1515
Paul Duffin44f1d842020-06-26 20:17:02 +01001516// Add the dependencies on the child modules in the component deps mutator.
1517func (module *SdkLibrary) ComponentDepsMutator(ctx android.BottomUpMutatorContext) {
Paul Duffin3375e352020-04-28 10:44:03 +01001518 for _, apiScope := range module.getGeneratedApiScopes(ctx) {
Paul Duffind1b3a922020-01-22 11:57:20 +00001519 // Add dependencies to the stubs library
Spandan Das877f39d2023-03-29 16:19:51 +00001520 stubModuleName := module.stubsLibraryModuleName(apiScope)
Jihoon Kangbd093452023-12-26 19:08:01 +00001521 ctx.AddVariationDependencies(nil, apiScope.everythingStubsTag, stubModuleName)
Jihoon Kang1147b312023-06-08 23:25:57 +00001522
Jihoon Kangbd093452023-12-26 19:08:01 +00001523 exportableStubModuleName := module.exportableStubsLibraryModuleName(apiScope)
1524 ctx.AddVariationDependencies(nil, apiScope.exportableStubsTag, exportableStubModuleName)
Paul Duffind1b3a922020-01-22 11:57:20 +00001525
Paul Duffin15f34ef2020-07-20 18:04:44 +01001526 // Add a dependency on the stubs source in order to access both stubs source and api information.
1527 ctx.AddVariationDependencies(nil, apiScope.stubsSourceAndApiTag, module.stubsSourceModuleName(apiScope))
Paul Duffin958806b2022-05-16 13:10:47 +00001528
1529 if module.compareAgainstLatestApi(apiScope) {
1530 // Add dependencies on the latest finalized version of the API .txt file.
1531 latestApiModuleName := module.latestApiModuleName(apiScope)
1532 ctx.AddDependency(module, apiScope.latestApiModuleTag, latestApiModuleName)
1533
1534 // Add dependencies on the latest finalized version of the remove API .txt file.
1535 latestRemovedApiModuleName := module.latestRemovedApiModuleName(apiScope)
1536 ctx.AddDependency(module, apiScope.latestRemovedApiModuleTag, latestRemovedApiModuleName)
1537 }
Sundong Ahn054b19a2018-10-19 13:46:09 +09001538 }
1539
Paul Duffindfa131e2020-05-15 20:37:11 +01001540 if module.requiresRuntimeImplementationLibrary() {
Paul Duffin5df79302020-05-16 15:52:12 +01001541 // Add dependency to the rule for generating the implementation library.
1542 ctx.AddDependency(module, implLibraryTag, module.implLibraryModuleName())
1543
Paul Duffindfa131e2020-05-15 20:37:11 +01001544 if module.sharedLibrary() {
1545 // Add dependency to the rule for generating the xml permissions file
Paul Duffineedc5d52020-06-12 17:46:39 +01001546 ctx.AddDependency(module, xmlPermissionsFileTag, module.xmlPermissionsModuleName())
Paul Duffindfa131e2020-05-15 20:37:11 +01001547 }
Paul Duffin44f1d842020-06-26 20:17:02 +01001548 }
1549}
Paul Duffine74ac732020-02-06 13:51:46 +00001550
Paul Duffin44f1d842020-06-26 20:17:02 +01001551// Add other dependencies as normal.
1552func (module *SdkLibrary) DepsMutator(ctx android.BottomUpMutatorContext) {
Anton Hanssone77fccc2021-01-20 16:52:41 +00001553 var missingApiModules []string
1554 for _, apiScope := range module.getGeneratedApiScopes(ctx) {
1555 if apiScope.unstable {
1556 continue
1557 }
Paul Duffin958806b2022-05-16 13:10:47 +00001558 if m := module.latestApiModuleName(apiScope); !ctx.OtherModuleExists(m) {
Anton Hanssone77fccc2021-01-20 16:52:41 +00001559 missingApiModules = append(missingApiModules, m)
1560 }
Paul Duffin958806b2022-05-16 13:10:47 +00001561 if m := module.latestRemovedApiModuleName(apiScope); !ctx.OtherModuleExists(m) {
Anton Hanssone77fccc2021-01-20 16:52:41 +00001562 missingApiModules = append(missingApiModules, m)
1563 }
Paul Duffin958806b2022-05-16 13:10:47 +00001564 if m := module.latestIncompatibilitiesModuleName(apiScope); !ctx.OtherModuleExists(m) {
Jaewoong Jung1a97ee02021-03-09 13:25:02 -08001565 missingApiModules = append(missingApiModules, m)
1566 }
Anton Hanssone77fccc2021-01-20 16:52:41 +00001567 }
1568 if len(missingApiModules) != 0 && !module.sdkLibraryProperties.Unsafe_ignore_missing_latest_api {
1569 m := module.Name() + " is missing tracking files for previously released library versions.\n"
1570 m += "You need to do one of the following:\n"
1571 m += "- Add `unsafe_ignore_missing_latest_api: true` to your blueprint (to disable compat tracking)\n"
1572 m += "- Add a set of prebuilt txt files representing the last released version of this library for compat checking.\n"
1573 m += " (the current set of API files can be used as a seed for this compatibility tracking\n"
1574 m += "\n"
1575 m += "The following filegroup modules are missing:\n "
1576 m += strings.Join(missingApiModules, "\n ") + "\n"
1577 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."
1578 ctx.ModuleErrorf(m)
1579 }
Jiyong Parkc678ad32018-04-10 13:07:10 +09001580}
1581
Paul Duffin46dc45a2020-05-14 15:39:10 +01001582func (module *SdkLibrary) OutputFiles(tag string) (android.Paths, error) {
1583 paths, err := module.commonOutputFiles(tag)
Colin Cross4acaea92021-12-10 23:05:02 +00001584 if paths != nil || err != nil {
Paul Duffin46dc45a2020-05-14 15:39:10 +01001585 return paths, err
1586 }
Colin Cross4acaea92021-12-10 23:05:02 +00001587 if module.requiresRuntimeImplementationLibrary() {
Jihoon Kanga3a05462024-04-05 00:36:44 +00001588 return module.implLibraryModule.OutputFiles(tag)
Colin Cross4acaea92021-12-10 23:05:02 +00001589 }
1590 if tag == "" {
1591 return nil, nil
1592 }
1593 return nil, fmt.Errorf("unsupported module reference tag %q", tag)
Paul Duffin46dc45a2020-05-14 15:39:10 +01001594}
1595
Inseob Kimc0907f12019-02-08 21:00:45 +09001596func (module *SdkLibrary) GenerateAndroidBuildActions(ctx android.ModuleContext) {
Spandan Das5ae65ee2024-04-16 22:03:26 +00001597 if disableSourceApexVariant(ctx) {
1598 // Prebuilts are active, do not create the installation rules for the source javalib.
1599 // Even though the source javalib is not used, we need to hide it to prevent duplicate installation rules.
1600 // TODO (b/331665856): Implement a principled solution for this.
1601 module.HideFromMake()
1602 }
satayev8f088b02021-12-06 11:40:46 +00001603
Paul Duffina2ae7e02020-09-11 11:55:00 +01001604 module.generateCommonBuildActions(ctx)
1605
Jihoon Kanga3a05462024-04-05 00:36:44 +00001606 module.stem = proptools.StringDefault(module.overridableProperties.Stem, ctx.ModuleName())
1607
1608 module.provideHiddenAPIPropertyInfo(ctx)
Sundong Ahn054b19a2018-10-19 13:46:09 +09001609
Paul Duffinb97b1572021-04-29 21:50:40 +01001610 // Collate the components exported by this module. All scope specific modules are exported but
1611 // the impl and xml component modules are not.
1612 exportedComponents := map[string]struct{}{}
1613
Sundong Ahn57368eb2018-07-06 11:20:23 +09001614 // Record the paths to the header jars of the library (stubs and impl).
Paul Duffind1b3a922020-01-22 11:57:20 +00001615 // When this java_sdk_library is depended upon from others via "libs" property,
Jiyong Parkc678ad32018-04-10 13:07:10 +09001616 // the recorded paths will be returned depending on the link type of the caller.
1617 ctx.VisitDirectDeps(func(to android.Module) {
Jiyong Parkc678ad32018-04-10 13:07:10 +09001618 tag := ctx.OtherModuleDependencyTag(to)
1619
Paul Duffinc8782502020-04-29 20:45:27 +01001620 // Extract information from any of the scope specific dependencies.
1621 if scopeTag, ok := tag.(scopeDependencyTag); ok {
1622 apiScope := scopeTag.apiScope
Paul Duffin803a9562020-05-20 11:52:25 +01001623 scopePaths := module.getScopePathsCreateIfNeeded(apiScope)
Paul Duffinc8782502020-04-29 20:45:27 +01001624
1625 // Extract information from the dependency. The exact information extracted
1626 // is determined by the nature of the dependency which is determined by the tag.
1627 scopeTag.extractDepInfo(ctx, to, scopePaths)
Paul Duffinb97b1572021-04-29 21:50:40 +01001628
1629 exportedComponents[ctx.OtherModuleName(to)] = struct{}{}
Sundong Ahn20e998b2018-07-24 11:19:26 +09001630 }
Jihoon Kang8479dea2024-04-04 01:19:05 +00001631
1632 if tag == implLibraryTag {
1633 if dep, ok := android.OtherModuleProvider(ctx, to, JavaInfoProvider); ok {
1634 module.implLibraryHeaderJars = append(module.implLibraryHeaderJars, dep.HeaderJars...)
Jihoon Kanga3a05462024-04-05 00:36:44 +00001635 module.implLibraryModule = to.(*Library)
1636 android.SetProvider(ctx, JavaInfoProvider, dep)
Jihoon Kang8479dea2024-04-04 01:19:05 +00001637 }
1638 }
Jiyong Parkc678ad32018-04-10 13:07:10 +09001639 })
Paul Duffinb97b1572021-04-29 21:50:40 +01001640
Jihoon Kanga3a05462024-04-05 00:36:44 +00001641 apexInfo, _ := android.ModuleProvider(ctx, android.ApexInfoProvider)
1642 if !apexInfo.IsForPlatform() {
1643 module.hideApexVariantFromMake = true
1644 }
1645
1646 if module.implLibraryModule != nil {
1647 if ctx.Device() {
1648 module.classesJarPaths = android.Paths{module.implLibraryModule.implementationJarFile}
1649 module.bootDexJarPath = module.implLibraryModule.bootDexJarPath
1650 module.uncompressDexState = module.implLibraryModule.uncompressDexState
1651 module.active = module.implLibraryModule.active
1652 }
1653
1654 module.outputFile = module.implLibraryModule.outputFile
1655 module.dexJarFile = makeDexJarPathFromPath(module.implLibraryModule.dexJarFile.Path())
1656 module.headerJarFile = module.implLibraryModule.headerJarFile
1657 module.implementationAndResourcesJar = module.implLibraryModule.implementationAndResourcesJar
1658 module.builtInstalledForApex = module.implLibraryModule.builtInstalledForApex
1659 module.dexpreopter.configPath = module.implLibraryModule.dexpreopter.configPath
1660 module.dexpreopter.outputProfilePathOnHost = module.implLibraryModule.dexpreopter.outputProfilePathOnHost
1661
Jihoon Kang34155e32024-05-20 19:08:49 +00001662 // Properties required for Library.AndroidMkEntries
1663 module.logtagsSrcs = module.implLibraryModule.logtagsSrcs
1664 module.dexpreopter.builtInstalled = module.implLibraryModule.dexpreopter.builtInstalled
1665 module.jacocoReportClassesFile = module.implLibraryModule.jacocoReportClassesFile
1666 module.dexer.proguardDictionary = module.implLibraryModule.dexer.proguardDictionary
1667 module.dexer.proguardUsageZip = module.implLibraryModule.dexer.proguardUsageZip
1668 module.linter.reports = module.implLibraryModule.linter.reports
Jihoon Kang629e2a32024-06-25 20:47:49 +00001669 module.linter.outputs.depSets = module.implLibraryModule.LintDepSets()
Jihoon Kang34155e32024-05-20 19:08:49 +00001670
Jihoon Kanga3a05462024-04-05 00:36:44 +00001671 if !module.Host() {
1672 module.hostdexInstallFile = module.implLibraryModule.hostdexInstallFile
1673 }
1674
1675 android.SetProvider(ctx, blueprint.SrcsFileProviderKey, blueprint.SrcsFileProviderData{SrcPaths: module.implLibraryModule.uniqueSrcFiles.Strings()})
1676 }
1677
Paul Duffinb97b1572021-04-29 21:50:40 +01001678 // Make the set of components exported by this module available for use elsewhere.
Cole Faust18994c72023-02-28 16:02:16 -08001679 exportedComponentInfo := android.ExportedComponentsInfo{Components: android.SortedKeys(exportedComponents)}
Colin Cross40213022023-12-13 15:19:49 -08001680 android.SetProvider(ctx, android.ExportedComponentsInfoProvider, exportedComponentInfo)
Paul Duffin958806b2022-05-16 13:10:47 +00001681
1682 // Provide additional information for inclusion in an sdk's generated .info file.
1683 additionalSdkInfo := map[string]interface{}{}
1684 additionalSdkInfo["dist_stem"] = module.distStem()
Paul Duffine8409952022-09-22 16:24:46 +01001685 baseModuleName := module.distStem()
Paul Duffin958806b2022-05-16 13:10:47 +00001686 scopes := map[string]interface{}{}
1687 additionalSdkInfo["scopes"] = scopes
1688 for scope, scopePaths := range module.scopePaths {
1689 scopeInfo := map[string]interface{}{}
1690 scopes[scope.name] = scopeInfo
1691 scopeInfo["current_api"] = scope.snapshotRelativeCurrentApiTxtPath(baseModuleName)
1692 scopeInfo["removed_api"] = scope.snapshotRelativeRemovedApiTxtPath(baseModuleName)
Jihoon Kang5623e542024-01-31 23:27:26 +00001693 if p := scopePaths.latestApiPaths; len(p) > 0 {
1694 // The last path in the list is the one that applies to this scope, the
1695 // preceding ones, if any, are for the scope(s) that it extends.
1696 scopeInfo["latest_api"] = p[len(p)-1].String()
Paul Duffin958806b2022-05-16 13:10:47 +00001697 }
Jihoon Kang5623e542024-01-31 23:27:26 +00001698 if p := scopePaths.latestRemovedApiPaths; len(p) > 0 {
1699 // The last path in the list is the one that applies to this scope, the
1700 // preceding ones, if any, are for the scope(s) that it extends.
1701 scopeInfo["latest_removed_api"] = p[len(p)-1].String()
Paul Duffin958806b2022-05-16 13:10:47 +00001702 }
1703 }
Colin Cross40213022023-12-13 15:19:49 -08001704 android.SetProvider(ctx, android.AdditionalSdkInfoProvider, android.AdditionalSdkInfo{additionalSdkInfo})
Jiyong Parkc678ad32018-04-10 13:07:10 +09001705}
1706
Jihoon Kanga3a05462024-04-05 00:36:44 +00001707func (module *SdkLibrary) BuiltInstalledForApex() []dexpreopterInstall {
1708 return module.builtInstalledForApex
1709}
1710
Jiyong Park0b0e1b92019-12-03 13:24:29 +09001711func (module *SdkLibrary) AndroidMkEntries() []android.AndroidMkEntries {
Paul Duffindfa131e2020-05-15 20:37:11 +01001712 if !module.requiresRuntimeImplementationLibrary() {
Paul Duffin43db9be2019-12-30 17:35:49 +00001713 return nil
1714 }
Jiyong Park0b0e1b92019-12-03 13:24:29 +09001715 entriesList := module.Library.AndroidMkEntries()
Jihoon Kanga3a05462024-04-05 00:36:44 +00001716 entries := &entriesList[0]
1717 entries.Required = append(entries.Required, module.implLibraryModuleName())
Yo Chiang07d75072020-06-05 17:43:19 +08001718 if module.sharedLibrary() {
Yo Chiang07d75072020-06-05 17:43:19 +08001719 entries.Required = append(entries.Required, module.xmlPermissionsModuleName())
1720 }
Jiyong Park0b0e1b92019-12-03 13:24:29 +09001721 return entriesList
Jiyong Park82484c02018-04-23 21:41:26 +09001722}
1723
Anton Hansson5fd5d242020-03-27 19:43:19 +00001724// The dist path of the stub artifacts
1725func (module *SdkLibrary) apiDistPath(apiScope *apiScope) string {
Colin Crossf0eace92021-06-02 13:02:23 -07001726 return path.Join("apistubs", module.distGroup(), apiScope.name)
Anton Hansson5fd5d242020-03-27 19:43:19 +00001727}
1728
Paul Duffin12ceb462019-12-24 20:31:31 +00001729// Get the sdk version for use when compiling the stubs library.
Paul Duffin780c5f42020-05-12 15:52:55 +01001730func (module *SdkLibrary) sdkVersionForStubsLibrary(mctx android.EarlyModuleContext, apiScope *apiScope) string {
Paul Duffin87a05a32020-05-12 11:50:28 +01001731 scopeProperties := module.scopeToProperties[apiScope]
1732 if scopeProperties.Sdk_version != nil {
1733 return proptools.String(scopeProperties.Sdk_version)
1734 }
1735
Jiyong Parkf1691d22021-03-29 20:11:58 +09001736 sdkDep := decodeSdkDep(mctx, android.SdkContext(&module.Library))
Paul Duffin12ceb462019-12-24 20:31:31 +00001737 if sdkDep.hasStandardLibs() {
1738 // If building against a standard sdk then use the sdk version appropriate for the scope.
Paul Duffind1b3a922020-01-22 11:57:20 +00001739 return apiScope.sdkVersion
Paul Duffin12ceb462019-12-24 20:31:31 +00001740 } else {
1741 // Otherwise, use no system module.
1742 return "none"
1743 }
1744}
1745
Paul Duffin31310252020-11-20 21:26:20 +00001746func (module *SdkLibrary) distStem() string {
1747 return proptools.StringDefault(module.sdkLibraryProperties.Dist_stem, module.BaseModuleName())
1748}
1749
Colin Cross986b69a2021-06-01 13:13:40 -07001750// distGroup returns the subdirectory of the dist path of the stub artifacts.
1751func (module *SdkLibrary) distGroup() string {
Colin Cross59b92bf2021-06-01 14:07:56 -07001752 return proptools.StringDefault(module.sdkLibraryProperties.Dist_group, "unknown")
Colin Cross986b69a2021-06-01 13:13:40 -07001753}
1754
Paul Duffin958806b2022-05-16 13:10:47 +00001755func latestPrebuiltApiModuleName(name string, apiScope *apiScope) string {
1756 return PrebuiltApiModuleName(name, apiScope.name, "latest")
1757}
1758
Jihoon Kang748a24d2024-03-20 21:29:39 +00001759func latestPrebuiltApiCombinedModuleName(name string, apiScope *apiScope) string {
1760 return PrebuiltApiCombinedModuleName(name, apiScope.name, "latest")
1761}
1762
Paul Duffind1b3a922020-01-22 11:57:20 +00001763func (module *SdkLibrary) latestApiFilegroupName(apiScope *apiScope) string {
Paul Duffin958806b2022-05-16 13:10:47 +00001764 return ":" + module.latestApiModuleName(apiScope)
1765}
1766
1767func (module *SdkLibrary) latestApiModuleName(apiScope *apiScope) string {
Jihoon Kang748a24d2024-03-20 21:29:39 +00001768 return latestPrebuiltApiCombinedModuleName(module.distStem(), apiScope)
Jiyong Park58c518b2018-05-12 22:29:12 +09001769}
Jiyong Parkc678ad32018-04-10 13:07:10 +09001770
Paul Duffind1b3a922020-01-22 11:57:20 +00001771func (module *SdkLibrary) latestRemovedApiFilegroupName(apiScope *apiScope) string {
Paul Duffin958806b2022-05-16 13:10:47 +00001772 return ":" + module.latestRemovedApiModuleName(apiScope)
1773}
1774
1775func (module *SdkLibrary) latestRemovedApiModuleName(apiScope *apiScope) string {
Jihoon Kang748a24d2024-03-20 21:29:39 +00001776 return latestPrebuiltApiCombinedModuleName(module.distStem()+"-removed", apiScope)
Jiyong Parkc678ad32018-04-10 13:07:10 +09001777}
1778
Jaewoong Jung1a97ee02021-03-09 13:25:02 -08001779func (module *SdkLibrary) latestIncompatibilitiesFilegroupName(apiScope *apiScope) string {
Paul Duffin958806b2022-05-16 13:10:47 +00001780 return ":" + module.latestIncompatibilitiesModuleName(apiScope)
1781}
1782
1783func (module *SdkLibrary) latestIncompatibilitiesModuleName(apiScope *apiScope) string {
1784 return latestPrebuiltApiModuleName(module.distStem()+"-incompatibilities", apiScope)
Jaewoong Jung1a97ee02021-03-09 13:25:02 -08001785}
1786
Jihoon Kang1c92c3e2023-03-23 17:44:51 +00001787func (module *SdkLibrary) contributesToApiSurface(c android.Config) bool {
1788 _, exists := c.GetApiLibraries()[module.Name()]
1789 return exists
1790}
1791
Jihoon Kang0c705a42023-08-02 06:44:57 +00001792// The listed modules are the special java_sdk_libraries where apiScope.kind do not match the
1793// api surface that the module contribute to. For example, the public droidstubs and java_library
1794// do not contribute to the public api surface, but contributes to the core platform api surface.
1795// This method returns the full api surface stub lib that
1796// the generated java_api_library should depend on.
1797func (module *SdkLibrary) alternativeFullApiSurfaceStubLib() string {
1798 if val, ok := apiLibraryAdditionalProperties[module.Name()]; ok {
1799 return val.FullApiSurfaceStubLib
1800 }
1801 return ""
1802}
1803
1804// The listed modules' stubs contents do not match the corresponding txt files,
1805// but require additional api contributions to generate the full stubs.
1806// This method returns the name of the additional api contribution module
1807// for corresponding sdk_library modules.
1808func (module *SdkLibrary) apiLibraryAdditionalApiContribution() string {
1809 if val, ok := apiLibraryAdditionalProperties[module.Name()]; ok {
1810 return val.AdditionalApiContribution
1811 }
1812 return ""
1813}
1814
Anton Hansson944e77d2020-08-19 11:40:22 +01001815func childModuleVisibility(childVisibility []string) []string {
1816 if childVisibility == nil {
1817 // No child visibility set. The child will use the visibility of the sdk_library.
1818 return nil
1819 }
1820
1821 // Prepend an override to ignore the sdk_library's visibility, and rely on the child visibility.
1822 var visibility []string
1823 visibility = append(visibility, "//visibility:override")
1824 visibility = append(visibility, childVisibility...)
1825 return visibility
1826}
1827
Paul Duffin5df79302020-05-16 15:52:12 +01001828// Creates the implementation java library
1829func (module *SdkLibrary) createImplLibrary(mctx android.DefaultableHookContext) {
Anton Hansson944e77d2020-08-19 11:40:22 +01001830 visibility := childModuleVisibility(module.sdkLibraryProperties.Impl_library_visibility)
1831
Paul Duffin5df79302020-05-16 15:52:12 +01001832 props := struct {
Paul Duffin77590a82022-04-28 14:13:30 +00001833 Name *string
1834 Visibility []string
Paul Duffin77590a82022-04-28 14:13:30 +00001835 Libs []string
1836 Static_libs []string
1837 Apex_available []string
Jihoon Kanga3a05462024-04-05 00:36:44 +00001838 Stem *string
Paul Duffin5df79302020-05-16 15:52:12 +01001839 }{
1840 Name: proptools.StringPtr(module.implLibraryModuleName()),
Anton Hansson944e77d2020-08-19 11:40:22 +01001841 Visibility: visibility,
Jihoon Kanga3a05462024-04-05 00:36:44 +00001842
1843 Libs: append(module.properties.Libs, module.sdkLibraryProperties.Impl_only_libs...),
1844
1845 Static_libs: append(module.properties.Static_libs, module.sdkLibraryProperties.Impl_only_static_libs...),
Paul Duffin77590a82022-04-28 14:13:30 +00001846 // Pass the apex_available settings down so that the impl library can be statically
1847 // embedded within a library that is added to an APEX. Needed for updatable-media.
1848 Apex_available: module.ApexAvailable(),
Jihoon Kanga3a05462024-04-05 00:36:44 +00001849
1850 Stem: proptools.StringPtr(module.Name()),
Paul Duffin5df79302020-05-16 15:52:12 +01001851 }
1852
1853 properties := []interface{}{
1854 &module.properties,
1855 &module.protoProperties,
1856 &module.deviceProperties,
Liz Kammera7a64f32020-07-09 15:16:41 -07001857 &module.dexProperties,
Paul Duffin5df79302020-05-16 15:52:12 +01001858 &module.dexpreoptProperties,
Colin Cross014489c2020-06-02 20:09:13 -07001859 &module.linter.properties,
Spandan Dasb9c58352024-05-13 18:29:45 +00001860 &module.overridableProperties,
Paul Duffin5df79302020-05-16 15:52:12 +01001861 &props,
1862 module.sdkComponentPropertiesForChildLibrary(),
1863 }
1864 mctx.CreateModule(LibraryFactory, properties...)
1865}
1866
Jihoon Kangfa4a90d2023-12-20 02:53:38 +00001867type libraryProperties struct {
1868 Name *string
1869 Visibility []string
1870 Srcs []string
1871 Installable *bool
1872 Sdk_version *string
1873 System_modules *string
1874 Patch_module *string
1875 Libs []string
1876 Static_libs []string
1877 Compile_dex *bool
1878 Java_version *string
1879 Openjdk9 struct {
1880 Srcs []string
1881 Javacflags []string
1882 }
1883 Dist struct {
1884 Targets []string
1885 Dest *string
1886 Dir *string
1887 Tag *string
1888 }
Jihoon Kangfe914ed2024-02-12 22:49:21 +00001889 Is_stubs_module *bool
Jihoon Kangfa4a90d2023-12-20 02:53:38 +00001890}
Jiyong Parkc678ad32018-04-10 13:07:10 +09001891
Jihoon Kangfa4a90d2023-12-20 02:53:38 +00001892func (module *SdkLibrary) stubsLibraryProps(mctx android.DefaultableHookContext, apiScope *apiScope) libraryProperties {
1893 props := libraryProperties{}
Jihoon Kang786df932023-09-07 01:18:31 +00001894 props.Visibility = []string{"//visibility:override", "//visibility:private"}
Jiyong Parkc678ad32018-04-10 13:07:10 +09001895 // sources are generated from the droiddoc
Paul Duffin12ceb462019-12-24 20:31:31 +00001896 sdkVersion := module.sdkVersionForStubsLibrary(mctx, apiScope)
Paul Duffin52d398a2019-06-11 12:31:14 +01001897 props.Sdk_version = proptools.StringPtr(sdkVersion)
Paul Duffina18abc22020-05-16 18:54:24 +01001898 props.System_modules = module.deviceProperties.System_modules
1899 props.Patch_module = module.properties.Patch_module
Paul Duffin367ab912019-12-23 19:40:36 +00001900 props.Installable = proptools.BoolPtr(false)
Sundong Ahn054b19a2018-10-19 13:46:09 +09001901 props.Libs = module.sdkLibraryProperties.Stub_only_libs
Mark White9421c4c2023-08-10 00:07:03 +00001902 props.Libs = append(props.Libs, module.scopeToProperties[apiScope].Libs...)
Anton Hanssondae54cd2021-04-21 16:30:10 +01001903 props.Static_libs = module.sdkLibraryProperties.Stub_only_static_libs
Paul Duffine22c2ab2020-05-20 19:35:27 +01001904 // The stub-annotations library contains special versions of the annotations
1905 // with CLASS retention policy, so that they're kept.
1906 if proptools.Bool(module.sdkLibraryProperties.Annotations_enabled) {
1907 props.Libs = append(props.Libs, "stub-annotations")
1908 }
Paul Duffina18abc22020-05-16 18:54:24 +01001909 props.Openjdk9.Srcs = module.properties.Openjdk9.Srcs
1910 props.Openjdk9.Javacflags = module.properties.Openjdk9.Javacflags
Anton Hansson83509b52020-05-21 09:21:57 +01001911 // We compile the stubs for 1.8 in line with the main android.jar stubs, and potential
1912 // interop with older developer tools that don't support 1.9.
1913 props.Java_version = proptools.StringPtr("1.8")
Jihoon Kangfe914ed2024-02-12 22:49:21 +00001914 props.Is_stubs_module = proptools.BoolPtr(true)
Paul Duffinf4600f62021-05-13 22:34:45 +01001915
Jihoon Kangfa4a90d2023-12-20 02:53:38 +00001916 return props
1917}
1918
1919// Creates a static java library that has API stubs
1920func (module *SdkLibrary) createStubsLibrary(mctx android.DefaultableHookContext, apiScope *apiScope) {
1921
1922 props := module.stubsLibraryProps(mctx, apiScope)
1923 props.Name = proptools.StringPtr(module.sourceStubsLibraryModuleName(apiScope))
1924 props.Srcs = []string{":" + module.stubsSourceModuleName(apiScope)}
1925
1926 mctx.CreateModule(LibraryFactory, &props, module.sdkComponentPropertiesForChildLibrary())
1927}
1928
1929// Create a static java library that compiles the "exportable" stubs
1930func (module *SdkLibrary) createExportableStubsLibrary(mctx android.DefaultableHookContext, apiScope *apiScope) {
1931 props := module.stubsLibraryProps(mctx, apiScope)
1932 props.Name = proptools.StringPtr(module.exportableSourceStubsLibraryModuleName(apiScope))
1933 props.Srcs = []string{":" + module.stubsSourceModuleName(apiScope) + "{.exportable}"}
1934
Paul Duffin859fe962020-05-15 10:20:31 +01001935 mctx.CreateModule(LibraryFactory, &props, module.sdkComponentPropertiesForChildLibrary())
Jiyong Parkc678ad32018-04-10 13:07:10 +09001936}
1937
Paul Duffin6d0886e2020-04-07 18:49:53 +01001938// Creates a droidstubs module that creates stubs source files from the given full source
Paul Duffinc8782502020-04-29 20:45:27 +01001939// files and also updates and checks the API specification files.
Paul Duffin15f34ef2020-07-20 18:04:44 +01001940func (module *SdkLibrary) createStubsSourcesAndApi(mctx android.DefaultableHookContext, apiScope *apiScope, name string, scopeSpecificDroidstubsArgs []string) {
Jiyong Parkc678ad32018-04-10 13:07:10 +09001941 props := struct {
Sundong Ahn054b19a2018-10-19 13:46:09 +09001942 Name *string
Paul Duffin4911a892020-04-29 23:35:13 +01001943 Visibility []string
Sundong Ahn054b19a2018-10-19 13:46:09 +09001944 Srcs []string
1945 Installable *bool
Paul Duffin52d398a2019-06-11 12:31:14 +01001946 Sdk_version *string
Jihoon Kang3198f3c2023-01-26 08:08:52 +00001947 Api_surface *string
Paul Duffin12ceb462019-12-24 20:31:31 +00001948 System_modules *string
Sundong Ahn054b19a2018-10-19 13:46:09 +09001949 Libs []string
Paul Duffin6877e6d2020-09-25 19:59:14 +01001950 Output_javadoc_comments *bool
Paul Duffin11512472019-02-11 15:55:17 +00001951 Arg_files []string
Sundong Ahn054b19a2018-10-19 13:46:09 +09001952 Args *string
Sundong Ahn054b19a2018-10-19 13:46:09 +09001953 Java_version *string
Paul Duffine22c2ab2020-05-20 19:35:27 +01001954 Annotations_enabled *bool
Sundong Ahn054b19a2018-10-19 13:46:09 +09001955 Merge_annotations_dirs []string
1956 Merge_inclusion_annotations_dirs []string
Paul Duffin0ff08bd2020-04-29 13:30:54 +01001957 Generate_stubs *bool
Anton Hanssone87b03d2020-12-21 15:29:34 +00001958 Previous_api *string
Jihoon Kang6592e872023-12-19 01:13:16 +00001959 Aconfig_declarations []string
Sundong Ahn054b19a2018-10-19 13:46:09 +09001960 Check_api struct {
Anton Hanssone6056152020-12-31 10:37:27 +00001961 Current ApiToCheck
1962 Last_released ApiToCheck
Paul Duffin160fe412020-05-10 19:32:20 +01001963
1964 Api_lint struct {
1965 Enabled *bool
1966 New_since *string
1967 Baseline_file *string
1968 }
Jiyong Park58c518b2018-05-12 22:29:12 +09001969 }
Sundong Ahn1b92c822018-05-29 11:35:17 +09001970 Aidl struct {
1971 Include_dirs []string
1972 Local_include_dirs []string
1973 }
Paul Duffin040e9062020-11-23 17:41:36 +00001974 Dists []android.Dist
Jiyong Parkc678ad32018-04-10 13:07:10 +09001975 }{}
1976
Paul Duffin7b78b4d2020-04-28 14:08:32 +01001977 // The stubs source processing uses the same compile time classpath when extracting the
1978 // API from the implementation library as it does when compiling it. i.e. the same
1979 // * sdk version
1980 // * system_modules
1981 // * libs (static_libs/libs)
Paul Duffin250e6192019-06-07 10:44:37 +01001982
Paul Duffin0ff08bd2020-04-29 13:30:54 +01001983 props.Name = proptools.StringPtr(name)
Anton Hansson944e77d2020-08-19 11:40:22 +01001984 props.Visibility = childModuleVisibility(module.sdkLibraryProperties.Stubs_source_visibility)
Paul Duffina18abc22020-05-16 18:54:24 +01001985 props.Srcs = append(props.Srcs, module.properties.Srcs...)
Anton Hanssonf8ea3722021-09-16 14:24:13 +01001986 props.Srcs = append(props.Srcs, module.sdkLibraryProperties.Api_srcs...)
Paul Duffina18abc22020-05-16 18:54:24 +01001987 props.Sdk_version = module.deviceProperties.Sdk_version
Jihoon Kang3198f3c2023-01-26 08:08:52 +00001988 props.Api_surface = &apiScope.name
Paul Duffina18abc22020-05-16 18:54:24 +01001989 props.System_modules = module.deviceProperties.System_modules
Jiyong Parkc678ad32018-04-10 13:07:10 +09001990 props.Installable = proptools.BoolPtr(false)
Sundong Ahne6f0b052018-06-05 16:46:14 +09001991 // A droiddoc module has only one Libs property and doesn't distinguish between
1992 // shared libs and static libs. So we need to add both of these libs to Libs property.
Paul Duffina18abc22020-05-16 18:54:24 +01001993 props.Libs = module.properties.Libs
1994 props.Libs = append(props.Libs, module.properties.Static_libs...)
Nikita Ioffed732da72022-11-21 12:38:25 +00001995 props.Libs = append(props.Libs, module.sdkLibraryProperties.Stub_only_libs...)
Mark White9421c4c2023-08-10 00:07:03 +00001996 props.Libs = append(props.Libs, module.scopeToProperties[apiScope].Libs...)
Paul Duffina18abc22020-05-16 18:54:24 +01001997 props.Aidl.Include_dirs = module.deviceProperties.Aidl.Include_dirs
1998 props.Aidl.Local_include_dirs = module.deviceProperties.Aidl.Local_include_dirs
1999 props.Java_version = module.properties.Java_version
Jiyong Parkc678ad32018-04-10 13:07:10 +09002000
Paul Duffine22c2ab2020-05-20 19:35:27 +01002001 props.Annotations_enabled = module.sdkLibraryProperties.Annotations_enabled
Sundong Ahn054b19a2018-10-19 13:46:09 +09002002 props.Merge_annotations_dirs = module.sdkLibraryProperties.Merge_annotations_dirs
2003 props.Merge_inclusion_annotations_dirs = module.sdkLibraryProperties.Merge_inclusion_annotations_dirs
Jihoon Kang6592e872023-12-19 01:13:16 +00002004 props.Aconfig_declarations = module.sdkLibraryProperties.Aconfig_declarations
Sundong Ahn054b19a2018-10-19 13:46:09 +09002005
Paul Duffin6d0886e2020-04-07 18:49:53 +01002006 droidstubsArgs := []string{}
Paul Duffin235ffff2019-12-24 10:41:30 +00002007 if len(module.sdkLibraryProperties.Api_packages) != 0 {
Paul Duffin6d0886e2020-04-07 18:49:53 +01002008 droidstubsArgs = append(droidstubsArgs, "--stub-packages "+strings.Join(module.sdkLibraryProperties.Api_packages, ":"))
Paul Duffin235ffff2019-12-24 10:41:30 +00002009 }
2010 if len(module.sdkLibraryProperties.Hidden_api_packages) != 0 {
Paul Duffin6d0886e2020-04-07 18:49:53 +01002011 droidstubsArgs = append(droidstubsArgs,
Paul Duffin235ffff2019-12-24 10:41:30 +00002012 android.JoinWithPrefix(module.sdkLibraryProperties.Hidden_api_packages, " --hide-package "))
2013 }
Paul Duffin6d0886e2020-04-07 18:49:53 +01002014 droidstubsArgs = append(droidstubsArgs, module.sdkLibraryProperties.Droiddoc_options...)
Anton Hanssonfd1c0d22023-11-02 15:18:09 +00002015 disabledWarnings := []string{"HiddenSuperclass"}
2016 if proptools.BoolDefault(module.sdkLibraryProperties.Api_lint.Legacy_errors_allowed, true) {
2017 disabledWarnings = append(disabledWarnings,
2018 "BroadcastBehavior",
2019 "DeprecationMismatch",
2020 "MissingPermission",
2021 "SdkConstant",
2022 "Todo",
2023 )
Paul Duffin235ffff2019-12-24 10:41:30 +00002024 }
Paul Duffin6d0886e2020-04-07 18:49:53 +01002025 droidstubsArgs = append(droidstubsArgs, android.JoinWithPrefix(disabledWarnings, "--hide "))
Sundong Ahnfb2721f2018-09-17 13:23:09 +09002026
Paul Duffin6877e6d2020-09-25 19:59:14 +01002027 // Output Javadoc comments for public scope.
2028 if apiScope == apiScopePublic {
2029 props.Output_javadoc_comments = proptools.BoolPtr(true)
2030 }
2031
Paul Duffin1fb487d2020-04-07 18:50:10 +01002032 // Add in scope specific arguments.
Paul Duffin0ff08bd2020-04-29 13:30:54 +01002033 droidstubsArgs = append(droidstubsArgs, scopeSpecificDroidstubsArgs...)
Paul Duffin11512472019-02-11 15:55:17 +00002034 props.Arg_files = module.sdkLibraryProperties.Droiddoc_option_files
Paul Duffin6d0886e2020-04-07 18:49:53 +01002035 props.Args = proptools.StringPtr(strings.Join(droidstubsArgs, " "))
Jiyong Parkc678ad32018-04-10 13:07:10 +09002036
Paul Duffin15f34ef2020-07-20 18:04:44 +01002037 // List of APIs identified from the provided source files are created. They are later
2038 // compared against to the not-yet-released (a.k.a current) list of APIs and to the
2039 // last-released (a.k.a numbered) list of API.
2040 currentApiFileName := apiScope.apiFilePrefix + "current.txt"
2041 removedApiFileName := apiScope.apiFilePrefix + "removed.txt"
2042 apiDir := module.getApiDir()
2043 currentApiFileName = path.Join(apiDir, currentApiFileName)
2044 removedApiFileName = path.Join(apiDir, removedApiFileName)
Jiyong Parkc678ad32018-04-10 13:07:10 +09002045
Paul Duffin15f34ef2020-07-20 18:04:44 +01002046 // check against the not-yet-release API
2047 props.Check_api.Current.Api_file = proptools.StringPtr(currentApiFileName)
2048 props.Check_api.Current.Removed_api_file = proptools.StringPtr(removedApiFileName)
Jiyong Park58c518b2018-05-12 22:29:12 +09002049
Paul Duffin958806b2022-05-16 13:10:47 +00002050 if module.compareAgainstLatestApi(apiScope) {
Paul Duffin15f34ef2020-07-20 18:04:44 +01002051 // check against the latest released API
2052 latestApiFilegroupName := proptools.StringPtr(module.latestApiFilegroupName(apiScope))
Anton Hanssone87b03d2020-12-21 15:29:34 +00002053 props.Previous_api = latestApiFilegroupName
Paul Duffin15f34ef2020-07-20 18:04:44 +01002054 props.Check_api.Last_released.Api_file = latestApiFilegroupName
2055 props.Check_api.Last_released.Removed_api_file = proptools.StringPtr(
2056 module.latestRemovedApiFilegroupName(apiScope))
Jaewoong Jung1a97ee02021-03-09 13:25:02 -08002057 props.Check_api.Last_released.Baseline_file = proptools.StringPtr(
2058 module.latestIncompatibilitiesFilegroupName(apiScope))
Paul Duffin160fe412020-05-10 19:32:20 +01002059
Paul Duffin15f34ef2020-07-20 18:04:44 +01002060 if proptools.Bool(module.sdkLibraryProperties.Api_lint.Enabled) {
2061 // Enable api lint.
2062 props.Check_api.Api_lint.Enabled = proptools.BoolPtr(true)
2063 props.Check_api.Api_lint.New_since = latestApiFilegroupName
Paul Duffin160fe412020-05-10 19:32:20 +01002064
Paul Duffin15f34ef2020-07-20 18:04:44 +01002065 // If it exists then pass a lint-baseline.txt through to droidstubs.
2066 baselinePath := path.Join(apiDir, apiScope.apiFilePrefix+"lint-baseline.txt")
2067 baselinePathRelativeToRoot := path.Join(mctx.ModuleDir(), baselinePath)
2068 paths, err := mctx.GlobWithDeps(baselinePathRelativeToRoot, nil)
2069 if err != nil {
2070 mctx.ModuleErrorf("error checking for presence of %s: %s", baselinePathRelativeToRoot, err)
2071 }
2072 if len(paths) == 1 {
2073 props.Check_api.Api_lint.Baseline_file = proptools.StringPtr(baselinePath)
2074 } else if len(paths) != 0 {
2075 mctx.ModuleErrorf("error checking for presence of %s: expected one path, found: %v", baselinePathRelativeToRoot, paths)
Paul Duffin160fe412020-05-10 19:32:20 +01002076 }
2077 }
Paul Duffin15f34ef2020-07-20 18:04:44 +01002078 }
Jiyong Park58c518b2018-05-12 22:29:12 +09002079
Paul Duffin15f34ef2020-07-20 18:04:44 +01002080 if !Bool(module.sdkLibraryProperties.No_dist) {
Paul Duffin040e9062020-11-23 17:41:36 +00002081 // Dist the api txt and removed api txt artifacts for sdk builds.
2082 distDir := proptools.StringPtr(path.Join(module.apiDistPath(apiScope), "api"))
Jihoon Kang02168052024-03-20 00:44:54 +00002083 stubsTypeTagPrefix := ""
2084 if mctx.Config().ReleaseHiddenApiExportableStubs() {
2085 stubsTypeTagPrefix = ".exportable"
2086 }
Paul Duffin040e9062020-11-23 17:41:36 +00002087 for _, p := range []struct {
2088 tag string
2089 pattern string
2090 }{
Jihoon Kangd1799f62024-02-20 23:01:38 +00002091 // "exportable" api files are copied to the dist directory instead of the
Jihoon Kang02168052024-03-20 00:44:54 +00002092 // "everything" api files when "RELEASE_HIDDEN_API_EXPORTABLE_STUBS" build flag
2093 // is set. Otherwise, the "everything" api files are copied to the dist directory.
2094 {tag: "%s.api.txt", pattern: "%s.txt"},
2095 {tag: "%s.removed-api.txt", pattern: "%s-removed.txt"},
Paul Duffin040e9062020-11-23 17:41:36 +00002096 } {
2097 props.Dists = append(props.Dists, android.Dist{
2098 Targets: []string{"sdk", "win_sdk"},
2099 Dir: distDir,
2100 Dest: proptools.StringPtr(fmt.Sprintf(p.pattern, module.distStem())),
Jihoon Kang02168052024-03-20 00:44:54 +00002101 Tag: proptools.StringPtr(fmt.Sprintf(p.tag, stubsTypeTagPrefix)),
Paul Duffin040e9062020-11-23 17:41:36 +00002102 })
2103 }
Anton Hansson5fd5d242020-03-27 19:43:19 +00002104 }
2105
Spandan Das2cc80ba2023-10-27 17:21:52 +00002106 mctx.CreateModule(DroidstubsFactory, &props, module.sdkComponentPropertiesForChildLibrary()).(*Droidstubs).CallHookIfAvailable(mctx)
Jiyong Parkc678ad32018-04-10 13:07:10 +09002107}
2108
Jihoon Kang0c705a42023-08-02 06:44:57 +00002109func (module *SdkLibrary) createApiLibrary(mctx android.DefaultableHookContext, apiScope *apiScope, alternativeFullApiSurfaceStub string) {
Jihoon Kang1c92c3e2023-03-23 17:44:51 +00002110 props := struct {
Jihoon Kangca198c22023-06-22 23:13:51 +00002111 Name *string
2112 Visibility []string
2113 Api_contributions []string
2114 Libs []string
2115 Static_libs []string
2116 Full_api_surface_stub *string
Jihoon Kang4ec24872023-10-05 17:26:09 +00002117 System_modules *string
Jihoon Kang063ec002023-06-28 01:16:23 +00002118 Enable_validation *bool
Jihoon Kang5d701272024-02-15 21:53:49 +00002119 Stubs_type *string
Jihoon Kang1c92c3e2023-03-23 17:44:51 +00002120 }{}
2121
2122 props.Name = proptools.StringPtr(module.apiLibraryModuleName(apiScope))
Jihoon Kang786df932023-09-07 01:18:31 +00002123 props.Visibility = []string{"//visibility:override", "//visibility:private"}
Jihoon Kang1c92c3e2023-03-23 17:44:51 +00002124
2125 apiContributions := []string{}
2126
2127 // Api surfaces are not independent of each other, but have subset relationships,
2128 // and so does the api files. To generate from-text stubs for api surfaces other than public,
2129 // all subset api domains' api_contriubtions must be added as well.
2130 scope := apiScope
2131 for scope != nil {
2132 apiContributions = append(apiContributions, module.stubsSourceModuleName(scope)+".api.contribution")
2133 scope = scope.extends
2134 }
Jihoon Kang0c705a42023-08-02 06:44:57 +00002135 if apiScope == apiScopePublic {
2136 additionalApiContribution := module.apiLibraryAdditionalApiContribution()
2137 if additionalApiContribution != "" {
2138 apiContributions = append(apiContributions, additionalApiContribution)
2139 }
2140 }
Jihoon Kang1c92c3e2023-03-23 17:44:51 +00002141
2142 props.Api_contributions = apiContributions
2143 props.Libs = module.properties.Libs
2144 props.Libs = append(props.Libs, module.sdkLibraryProperties.Stub_only_libs...)
Mark White9421c4c2023-08-10 00:07:03 +00002145 props.Libs = append(props.Libs, module.scopeToProperties[apiScope].Libs...)
Jihoon Kang1c92c3e2023-03-23 17:44:51 +00002146 props.Libs = append(props.Libs, "stub-annotations")
2147 props.Static_libs = module.sdkLibraryProperties.Stub_only_static_libs
Jihoon Kange7ee2562023-07-25 05:51:46 +00002148 props.Full_api_surface_stub = proptools.StringPtr(apiScope.kind.DefaultJavaLibraryName())
Jihoon Kang0c705a42023-08-02 06:44:57 +00002149 if alternativeFullApiSurfaceStub != "" {
2150 props.Full_api_surface_stub = proptools.StringPtr(alternativeFullApiSurfaceStub)
2151 }
Jihoon Kang1c92c3e2023-03-23 17:44:51 +00002152
2153 // android_module_lib_stubs_current.from-text only comprises api contributions from art, conscrypt and i18n.
2154 // Thus, replace with android_module_lib_stubs_current_full.from-text, which comprises every api domains.
2155 if apiScope.kind == android.SdkModule {
Jihoon Kangca198c22023-06-22 23:13:51 +00002156 props.Full_api_surface_stub = proptools.StringPtr(apiScope.kind.DefaultJavaLibraryName() + "_full.from-text")
Jihoon Kang1c92c3e2023-03-23 17:44:51 +00002157 }
2158
Jihoon Kangd30ac8a2023-10-09 18:00:17 +00002159 // java_sdk_library modules that set sdk_version as none does not depend on other api
2160 // domains. Therefore, java_api_library created from such modules should not depend on
2161 // full_api_surface_stubs but create and compile stubs by the java_api_library module
2162 // itself.
2163 if module.SdkVersion(mctx).Kind == android.SdkNone {
2164 props.Full_api_surface_stub = nil
2165 }
2166
Jihoon Kang4ec24872023-10-05 17:26:09 +00002167 props.System_modules = module.deviceProperties.System_modules
Jihoon Kang063ec002023-06-28 01:16:23 +00002168 props.Enable_validation = proptools.BoolPtr(true)
Jihoon Kang5d701272024-02-15 21:53:49 +00002169 props.Stubs_type = proptools.StringPtr("everything")
Jihoon Kang4ec24872023-10-05 17:26:09 +00002170
Spandan Das2cc80ba2023-10-27 17:21:52 +00002171 mctx.CreateModule(ApiLibraryFactory, &props, module.sdkComponentPropertiesForChildLibrary())
Jihoon Kang1c92c3e2023-03-23 17:44:51 +00002172}
2173
Jihoon Kang02168052024-03-20 00:44:54 +00002174func (module *SdkLibrary) topLevelStubsLibraryProps(mctx android.DefaultableHookContext, apiScope *apiScope, doDist bool) libraryProperties {
Jihoon Kangfa4a90d2023-12-20 02:53:38 +00002175 props := libraryProperties{}
2176
Jihoon Kang1147b312023-06-08 23:25:57 +00002177 props.Visibility = childModuleVisibility(module.sdkLibraryProperties.Stubs_library_visibility)
2178 sdkVersion := module.sdkVersionForStubsLibrary(mctx, apiScope)
2179 props.Sdk_version = proptools.StringPtr(sdkVersion)
2180
Jihoon Kang1147b312023-06-08 23:25:57 +00002181 props.System_modules = module.deviceProperties.System_modules
2182
Jihoon Kang1147b312023-06-08 23:25:57 +00002183 // The imports need to be compiled to dex if the java_sdk_library requests it.
2184 compileDex := module.dexProperties.Compile_dex
2185 if module.stubLibrariesCompiledForDex() {
2186 compileDex = proptools.BoolPtr(true)
2187 }
2188 props.Compile_dex = compileDex
2189
Jihoon Kang02168052024-03-20 00:44:54 +00002190 if !Bool(module.sdkLibraryProperties.No_dist) && doDist {
2191 props.Dist.Targets = []string{"sdk", "win_sdk"}
2192 props.Dist.Dest = proptools.StringPtr(fmt.Sprintf("%v.jar", module.distStem()))
2193 props.Dist.Dir = proptools.StringPtr(module.apiDistPath(apiScope))
2194 props.Dist.Tag = proptools.StringPtr(".jar")
2195 }
2196
Jihoon Kangfa4a90d2023-12-20 02:53:38 +00002197 return props
2198}
2199
2200func (module *SdkLibrary) createTopLevelStubsLibrary(
2201 mctx android.DefaultableHookContext, apiScope *apiScope, contributesToApiSurface bool) {
2202
Jihoon Kang02168052024-03-20 00:44:54 +00002203 // Dist the "everything" stubs when the RELEASE_HIDDEN_API_EXPORTABLE_STUBS build flag is false
2204 doDist := !mctx.Config().ReleaseHiddenApiExportableStubs()
2205 props := module.topLevelStubsLibraryProps(mctx, apiScope, doDist)
Jihoon Kangfa4a90d2023-12-20 02:53:38 +00002206 props.Name = proptools.StringPtr(module.stubsLibraryModuleName(apiScope))
2207
2208 // Add the stub compiling java_library/java_api_library as static lib based on build config
2209 staticLib := module.sourceStubsLibraryModuleName(apiScope)
2210 if mctx.Config().BuildFromTextStub() && contributesToApiSurface {
2211 staticLib = module.apiLibraryModuleName(apiScope)
2212 }
2213 props.Static_libs = append(props.Static_libs, staticLib)
2214
2215 mctx.CreateModule(LibraryFactory, &props, module.sdkComponentPropertiesForChildLibrary())
2216}
2217
2218func (module *SdkLibrary) createTopLevelExportableStubsLibrary(
2219 mctx android.DefaultableHookContext, apiScope *apiScope) {
2220
Jihoon Kang02168052024-03-20 00:44:54 +00002221 // Dist the "exportable" stubs when the RELEASE_HIDDEN_API_EXPORTABLE_STUBS build flag is true
2222 doDist := mctx.Config().ReleaseHiddenApiExportableStubs()
2223 props := module.topLevelStubsLibraryProps(mctx, apiScope, doDist)
Jihoon Kangfa4a90d2023-12-20 02:53:38 +00002224 props.Name = proptools.StringPtr(module.exportableStubsLibraryModuleName(apiScope))
2225
Jihoon Kangfa4a90d2023-12-20 02:53:38 +00002226 staticLib := module.exportableSourceStubsLibraryModuleName(apiScope)
2227 props.Static_libs = append(props.Static_libs, staticLib)
2228
Jihoon Kang1147b312023-06-08 23:25:57 +00002229 mctx.CreateModule(LibraryFactory, &props, module.sdkComponentPropertiesForChildLibrary())
2230}
2231
Paul Duffin958806b2022-05-16 13:10:47 +00002232func (module *SdkLibrary) compareAgainstLatestApi(apiScope *apiScope) bool {
2233 return !(apiScope.unstable || module.sdkLibraryProperties.Unsafe_ignore_missing_latest_api)
2234}
2235
Paul Duffinea8f8082021-06-24 13:25:57 +01002236// Implements android.ApexModule
Jooyung Han5e9013b2020-03-10 06:23:13 +09002237func (module *SdkLibrary) DepIsInSameApex(mctx android.BaseModuleContext, dep android.Module) bool {
2238 depTag := mctx.OtherModuleDependencyTag(dep)
2239 if depTag == xmlPermissionsFileTag {
2240 return true
2241 }
Jihoon Kanga3a05462024-04-05 00:36:44 +00002242 if dep.Name() == module.implLibraryModuleName() {
2243 return true
2244 }
Jooyung Han5e9013b2020-03-10 06:23:13 +09002245 return module.Library.DepIsInSameApex(mctx, dep)
2246}
2247
Paul Duffinea8f8082021-06-24 13:25:57 +01002248// Implements android.ApexModule
2249func (module *SdkLibrary) UniqueApexVariations() bool {
2250 return module.uniqueApexVariations()
2251}
2252
Jihoon Kang80456fd2023-11-15 19:22:14 +00002253func (module *SdkLibrary) ContributeToApi() bool {
2254 return proptools.BoolDefault(module.sdkLibraryProperties.Contribute_to_android_api, false)
2255}
2256
Jiyong Parkc678ad32018-04-10 13:07:10 +09002257// Creates the xml file that publicizes the runtime library
Paul Duffinf0229202020-04-29 16:47:28 +01002258func (module *SdkLibrary) createXmlFile(mctx android.DefaultableHookContext) {
Spandan Das8c9ae7e2023-03-03 21:20:36 +00002259 moduleMinApiLevel := module.Library.MinSdkVersion(mctx)
Pedro Loureiroc3621422021-09-28 15:40:23 +00002260 var moduleMinApiLevelStr = moduleMinApiLevel.String()
2261 if moduleMinApiLevel == android.NoneApiLevel {
2262 moduleMinApiLevelStr = "current"
2263 }
Jiyong Parke3833882020-02-17 17:28:10 +09002264 props := struct {
Pedro Loureiroc3621422021-09-28 15:40:23 +00002265 Name *string
2266 Lib_name *string
2267 Apex_available []string
2268 On_bootclasspath_since *string
2269 On_bootclasspath_before *string
2270 Min_device_sdk *string
2271 Max_device_sdk *string
2272 Sdk_library_min_api_level *string
Jamie Garsidee570ace2023-11-27 12:07:36 +00002273 Uses_libs_dependencies []string
Jiyong Parke3833882020-02-17 17:28:10 +09002274 }{
Pedro Loureiroc3621422021-09-28 15:40:23 +00002275 Name: proptools.StringPtr(module.xmlPermissionsModuleName()),
2276 Lib_name: proptools.StringPtr(module.BaseModuleName()),
2277 Apex_available: module.ApexProperties.Apex_available,
2278 On_bootclasspath_since: module.commonSdkLibraryProperties.On_bootclasspath_since,
2279 On_bootclasspath_before: module.commonSdkLibraryProperties.On_bootclasspath_before,
2280 Min_device_sdk: module.commonSdkLibraryProperties.Min_device_sdk,
2281 Max_device_sdk: module.commonSdkLibraryProperties.Max_device_sdk,
2282 Sdk_library_min_api_level: &moduleMinApiLevelStr,
Jamie Garsidee570ace2023-11-27 12:07:36 +00002283 Uses_libs_dependencies: module.usesLibraryProperties.Uses_libs,
Jiyong Parkc678ad32018-04-10 13:07:10 +09002284 }
Jiyong Parke3833882020-02-17 17:28:10 +09002285
Jiyong Parke3833882020-02-17 17:28:10 +09002286 mctx.CreateModule(sdkLibraryXmlFactory, &props)
Jiyong Parkc678ad32018-04-10 13:07:10 +09002287}
2288
Jiyong Parkf1691d22021-03-29 20:11:58 +09002289func PrebuiltJars(ctx android.BaseModuleContext, baseName string, s android.SdkSpec) android.Paths {
Jiyong Park54105c42021-03-31 18:17:53 +09002290 var ver android.ApiLevel
Jiyong Parkf1691d22021-03-29 20:11:58 +09002291 var kind android.SdkKind
2292 if s.UsePrebuilt(ctx) {
Jiyong Park54105c42021-03-31 18:17:53 +09002293 ver = s.ApiLevel
Jiyong Parkf1691d22021-03-29 20:11:58 +09002294 kind = s.Kind
Jiyong Parkc678ad32018-04-10 13:07:10 +09002295 } else {
Jiyong Park6a927c42020-01-21 02:03:43 +09002296 // We don't have prebuilt SDK for the specific sdkVersion.
2297 // Instead of breaking the build, fallback to use "system_current"
Jiyong Park54105c42021-03-31 18:17:53 +09002298 ver = android.FutureApiLevel
Jiyong Parkf1691d22021-03-29 20:11:58 +09002299 kind = android.SdkSystem
Sundong Ahn054b19a2018-10-19 13:46:09 +09002300 }
Jiyong Park6a927c42020-01-21 02:03:43 +09002301
2302 dir := filepath.Join("prebuilts", "sdk", ver.String(), kind.String())
Paul Duffin50061512020-01-21 16:31:05 +00002303 jar := filepath.Join(dir, baseName+".jar")
Sundong Ahn054b19a2018-10-19 13:46:09 +09002304 jarPath := android.ExistentPathForSource(ctx, jar)
Sundong Ahnae418ac2019-02-28 15:01:28 +09002305 if !jarPath.Valid() {
Colin Cross07c88562020-01-07 09:34:44 -08002306 if ctx.Config().AllowMissingDependencies() {
2307 return android.Paths{android.PathForSource(ctx, jar)}
2308 } else {
Jiyong Parkf1691d22021-03-29 20:11:58 +09002309 ctx.PropertyErrorf("sdk_library", "invalid sdk version %q, %q does not exist", s.Raw, jar)
Colin Cross07c88562020-01-07 09:34:44 -08002310 }
Sundong Ahnae418ac2019-02-28 15:01:28 +09002311 return nil
2312 }
Sundong Ahn054b19a2018-10-19 13:46:09 +09002313 return android.Paths{jarPath.Path()}
2314}
2315
Colin Crossaede88c2020-08-11 12:17:01 -07002316// 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 +01002317//
2318// If either this or the other module are on the platform then this will return
2319// false.
Colin Cross56a83212020-09-15 18:30:11 -07002320func withinSameApexesAs(ctx android.BaseModuleContext, other android.Module) bool {
Colin Crossff694a82023-12-13 15:54:49 -08002321 apexInfo, _ := android.ModuleProvider(ctx, android.ApexInfoProvider)
Colin Cross313aa542023-12-13 13:47:44 -08002322 otherApexInfo, _ := android.OtherModuleProvider(ctx, other, android.ApexInfoProvider)
Jiyong Parkab50b072021-05-12 17:13:56 +09002323 return len(otherApexInfo.InApexVariants) > 0 && reflect.DeepEqual(apexInfo.InApexVariants, otherApexInfo.InApexVariants)
Paul Duffin9b879592020-05-26 13:21:35 +01002324}
2325
Jihoon Kang8479dea2024-04-04 01:19:05 +00002326func (module *SdkLibrary) sdkJars(ctx android.BaseModuleContext, sdkVersion android.SdkSpec) android.Paths {
Jiyong Park932cdfe2020-05-28 00:19:53 +09002327 // If the client doesn't set sdk_version, but if this library prefers stubs over
2328 // the impl library, let's provide the widest API surface possible. To do so,
2329 // force override sdk_version to module_current so that the closest possible API
2330 // surface could be found in selectHeaderJarsForSdkVersion
Jiyong Parkf1691d22021-03-29 20:11:58 +09002331 if module.defaultsToStubs() && !sdkVersion.Specified() {
Jiyong Park92315372021-04-02 08:45:46 +09002332 sdkVersion = android.SdkSpecFrom(ctx, "module_current")
Jiyong Park932cdfe2020-05-28 00:19:53 +09002333 }
Paul Duffind1b3a922020-01-22 11:57:20 +00002334
Paul Duffindaaa3322020-05-26 18:13:57 +01002335 // Only provide access to the implementation library if it is actually built.
2336 if module.requiresRuntimeImplementationLibrary() {
2337 // Check any special cases for java_sdk_library.
2338 //
2339 // Only allow access to the implementation library in the following condition:
2340 // * No sdk_version specified on the referencing module.
Paul Duffin9b879592020-05-26 13:21:35 +01002341 // * The referencing module is in the same apex as this.
Jiyong Parkf1691d22021-03-29 20:11:58 +09002342 if sdkVersion.Kind == android.SdkPrivate || withinSameApexesAs(ctx, module) {
Jihoon Kang8479dea2024-04-04 01:19:05 +00002343 return module.implLibraryHeaderJars
Sundong Ahn054b19a2018-10-19 13:46:09 +09002344 }
Jiyong Parkc678ad32018-04-10 13:07:10 +09002345 }
Paul Duffinb05d4292020-05-20 12:19:10 +01002346
Paul Duffin23970f42020-05-20 14:20:02 +01002347 return module.selectHeaderJarsForSdkVersion(ctx, sdkVersion)
Jiyong Parkc678ad32018-04-10 13:07:10 +09002348}
2349
Sundong Ahn241cd372018-07-13 16:16:44 +09002350// to satisfy SdkLibraryDependency interface
Jiyong Parkf1691d22021-03-29 20:11:58 +09002351func (module *SdkLibrary) SdkHeaderJars(ctx android.BaseModuleContext, sdkVersion android.SdkSpec) android.Paths {
Jihoon Kang8479dea2024-04-04 01:19:05 +00002352 return module.sdkJars(ctx, sdkVersion)
Sundong Ahn241cd372018-07-13 16:16:44 +09002353}
2354
Colin Cross571cccf2019-02-04 11:22:08 -08002355var javaSdkLibrariesKey = android.NewOnceKey("javaSdkLibraries")
2356
Jiyong Park82484c02018-04-23 21:41:26 +09002357func javaSdkLibraries(config android.Config) *[]string {
Colin Cross571cccf2019-02-04 11:22:08 -08002358 return config.Once(javaSdkLibrariesKey, func() interface{} {
Jiyong Park82484c02018-04-23 21:41:26 +09002359 return &[]string{}
2360 }).(*[]string)
2361}
2362
Paul Duffin749f98f2019-12-30 17:23:46 +00002363func (module *SdkLibrary) getApiDir() string {
2364 return proptools.StringDefault(module.sdkLibraryProperties.Api_dir, "api")
2365}
2366
Jiyong Parkc678ad32018-04-10 13:07:10 +09002367// For a java_sdk_library module, create internal modules for stubs, docs,
2368// runtime libs and xml file. If requested, the stubs and docs are created twice
2369// once for public API level and once for system API level
Paul Duffinf0229202020-04-29 16:47:28 +01002370func (module *SdkLibrary) CreateInternalModules(mctx android.DefaultableHookContext) {
2371 // If the module has been disabled then don't create any child modules.
Cole Fausta963b942024-04-11 17:43:00 -07002372 if !module.Enabled(mctx) {
Paul Duffinf0229202020-04-29 16:47:28 +01002373 return
2374 }
2375
Paul Duffina18abc22020-05-16 18:54:24 +01002376 if len(module.properties.Srcs) == 0 {
Inseob Kimc0907f12019-02-08 21:00:45 +09002377 mctx.PropertyErrorf("srcs", "java_sdk_library must specify srcs")
Jooyung Han58f26ab2019-12-18 15:34:32 +09002378 return
Inseob Kimc0907f12019-02-08 21:00:45 +09002379 }
2380
Paul Duffin37e0b772019-12-30 17:20:10 +00002381 // If this builds against standard libraries (i.e. is not part of the core libraries)
Paul Duffin4f5c1ef2020-11-19 14:53:43 +00002382 // then assume it provides both system and test apis.
Jiyong Parkf1691d22021-03-29 20:11:58 +09002383 sdkDep := decodeSdkDep(mctx, android.SdkContext(&module.Library))
Paul Duffin37e0b772019-12-30 17:20:10 +00002384 hasSystemAndTestApis := sdkDep.hasStandardLibs()
Paul Duffin3375e352020-04-28 10:44:03 +01002385 module.sdkLibraryProperties.Generate_system_and_test_apis = hasSystemAndTestApis
Paul Duffin4f5c1ef2020-11-19 14:53:43 +00002386
Jaewoong Jung18aefc12020-12-21 09:11:10 -08002387 missingCurrentApi := false
Inseob Kim8098faa2019-03-18 10:19:51 +09002388
Paul Duffin3375e352020-04-28 10:44:03 +01002389 generatedScopes := module.getGeneratedApiScopes(mctx)
Paul Duffind1b3a922020-01-22 11:57:20 +00002390
Paul Duffin749f98f2019-12-30 17:23:46 +00002391 apiDir := module.getApiDir()
Paul Duffin3375e352020-04-28 10:44:03 +01002392 for _, scope := range generatedScopes {
Inseob Kim8098faa2019-03-18 10:19:51 +09002393 for _, api := range []string{"current.txt", "removed.txt"} {
Paul Duffind1b3a922020-01-22 11:57:20 +00002394 path := path.Join(mctx.ModuleDir(), apiDir, scope.apiFilePrefix+api)
Inseob Kim8098faa2019-03-18 10:19:51 +09002395 p := android.ExistentPathForSource(mctx, path)
2396 if !p.Valid() {
Colin Cross18f840c2021-05-20 17:56:54 -07002397 if mctx.Config().AllowMissingDependencies() {
2398 mctx.AddMissingDependencies([]string{path})
2399 } else {
2400 mctx.ModuleErrorf("Current api file %#v doesn't exist", path)
2401 missingCurrentApi = true
2402 }
Inseob Kim8098faa2019-03-18 10:19:51 +09002403 }
2404 }
2405 }
2406
Jaewoong Jung18aefc12020-12-21 09:11:10 -08002407 if missingCurrentApi {
Inseob Kim8098faa2019-03-18 10:19:51 +09002408 script := "build/soong/scripts/gen-java-current-api-files.sh"
2409 p := android.ExistentPathForSource(mctx, script)
2410
2411 if !p.Valid() {
2412 panic(fmt.Sprintf("script file %s doesn't exist", script))
2413 }
2414
2415 mctx.ModuleErrorf("One or more current api files are missing. "+
2416 "You can update them by:\n"+
Paul Duffin37e0b772019-12-30 17:20:10 +00002417 "%s %q %s && m update-api",
Paul Duffind1b3a922020-01-22 11:57:20 +00002418 script, filepath.Join(mctx.ModuleDir(), apiDir),
Paul Duffin3375e352020-04-28 10:44:03 +01002419 strings.Join(generatedScopes.Strings(func(s *apiScope) string { return s.apiFilePrefix }), " "))
Inseob Kim8098faa2019-03-18 10:19:51 +09002420 return
2421 }
2422
Paul Duffin3375e352020-04-28 10:44:03 +01002423 for _, scope := range generatedScopes {
Paul Duffin15f34ef2020-07-20 18:04:44 +01002424 // Use the stubs source name for legacy reasons.
2425 module.createStubsSourcesAndApi(mctx, scope, module.stubsSourceModuleName(scope), scope.droidstubsArgs)
Paul Duffin0ff08bd2020-04-29 13:30:54 +01002426
Paul Duffind1b3a922020-01-22 11:57:20 +00002427 module.createStubsLibrary(mctx, scope)
Jihoon Kangfa4a90d2023-12-20 02:53:38 +00002428 module.createExportableStubsLibrary(mctx, scope)
Jihoon Kang1c92c3e2023-03-23 17:44:51 +00002429
Jihoon Kang0c705a42023-08-02 06:44:57 +00002430 alternativeFullApiSurfaceStubLib := ""
2431 if scope == apiScopePublic {
2432 alternativeFullApiSurfaceStubLib = module.alternativeFullApiSurfaceStubLib()
2433 }
2434 contributesToApiSurface := module.contributesToApiSurface(mctx.Config()) || alternativeFullApiSurfaceStubLib != ""
Jihoon Kang1147b312023-06-08 23:25:57 +00002435 if contributesToApiSurface {
Jihoon Kang0c705a42023-08-02 06:44:57 +00002436 module.createApiLibrary(mctx, scope, alternativeFullApiSurfaceStubLib)
Jihoon Kang1c92c3e2023-03-23 17:44:51 +00002437 }
Jihoon Kang1147b312023-06-08 23:25:57 +00002438
2439 module.createTopLevelStubsLibrary(mctx, scope, contributesToApiSurface)
Jihoon Kangfa4a90d2023-12-20 02:53:38 +00002440 module.createTopLevelExportableStubsLibrary(mctx, scope)
Inseob Kimc0907f12019-02-08 21:00:45 +09002441 }
2442
Paul Duffindfa131e2020-05-15 20:37:11 +01002443 if module.requiresRuntimeImplementationLibrary() {
Paul Duffin5df79302020-05-16 15:52:12 +01002444 // Create child module to create an implementation library.
2445 //
2446 // This temporarily creates a second implementation library that can be explicitly
2447 // referenced.
2448 //
2449 // TODO(b/156618935) - update comment once only one implementation library is created.
2450 module.createImplLibrary(mctx)
2451
Paul Duffindfa131e2020-05-15 20:37:11 +01002452 // Only create an XML permissions file that declares the library as being usable
2453 // as a shared library if required.
2454 if module.sharedLibrary() {
2455 module.createXmlFile(mctx)
2456 }
Paul Duffin43db9be2019-12-30 17:35:49 +00002457
2458 // record java_sdk_library modules so that they are exported to make
2459 javaSdkLibraries := javaSdkLibraries(mctx.Config())
2460 javaSdkLibrariesLock.Lock()
2461 defer javaSdkLibrariesLock.Unlock()
2462 *javaSdkLibraries = append(*javaSdkLibraries, module.BaseModuleName())
2463 }
Anton Hansson7f66efa2020-10-08 14:47:23 +01002464
Paul Duffin77590a82022-04-28 14:13:30 +00002465 // 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 +01002466 module.properties.Libs = append(module.properties.Libs, module.sdkLibraryProperties.Impl_only_libs...)
Paul Duffin77590a82022-04-28 14:13:30 +00002467 module.properties.Static_libs = append(module.properties.Static_libs, module.sdkLibraryProperties.Impl_only_static_libs...)
Inseob Kimc0907f12019-02-08 21:00:45 +09002468}
2469
2470func (module *SdkLibrary) InitSdkLibraryProperties() {
Colin Crossce6734e2020-06-15 16:09:53 -07002471 module.addHostAndDeviceProperties()
2472 module.AddProperties(&module.sdkLibraryProperties)
Sundong Ahn054b19a2018-10-19 13:46:09 +09002473
Paul Duffin71b33cc2021-06-23 11:39:47 +01002474 module.initSdkLibraryComponent(module)
Paul Duffin859fe962020-05-15 10:20:31 +01002475
Paul Duffina18abc22020-05-16 18:54:24 +01002476 module.properties.Installable = proptools.BoolPtr(true)
2477 module.deviceProperties.IsSDKLibrary = true
Inseob Kimc0907f12019-02-08 21:00:45 +09002478}
Sundong Ahn054b19a2018-10-19 13:46:09 +09002479
Paul Duffindfa131e2020-05-15 20:37:11 +01002480func (module *SdkLibrary) requiresRuntimeImplementationLibrary() bool {
2481 return !proptools.Bool(module.sdkLibraryProperties.Api_only)
2482}
2483
Jiyong Park932cdfe2020-05-28 00:19:53 +09002484func (module *SdkLibrary) defaultsToStubs() bool {
2485 return proptools.Bool(module.sdkLibraryProperties.Default_to_stubs)
2486}
2487
Paul Duffin1b1e8062020-05-08 13:44:43 +01002488// Defines how to name the individual component modules the sdk library creates.
2489type sdkLibraryComponentNamingScheme interface {
2490 stubsLibraryModuleName(scope *apiScope, baseName string) string
2491
2492 stubsSourceModuleName(scope *apiScope, baseName string) string
Jihoon Kang1c92c3e2023-03-23 17:44:51 +00002493
2494 apiLibraryModuleName(scope *apiScope, baseName string) string
Jihoon Kang1147b312023-06-08 23:25:57 +00002495
Jihoon Kangfa4a90d2023-12-20 02:53:38 +00002496 sourceStubsLibraryModuleName(scope *apiScope, baseName string) string
2497
2498 exportableStubsLibraryModuleName(scope *apiScope, baseName string) string
2499
2500 exportableSourceStubsLibraryModuleName(scope *apiScope, baseName string) string
Paul Duffin1b1e8062020-05-08 13:44:43 +01002501}
2502
2503type defaultNamingScheme struct {
2504}
2505
2506func (s *defaultNamingScheme) stubsLibraryModuleName(scope *apiScope, baseName string) string {
2507 return scope.stubsLibraryModuleName(baseName)
2508}
2509
2510func (s *defaultNamingScheme) stubsSourceModuleName(scope *apiScope, baseName string) string {
2511 return scope.stubsSourceModuleName(baseName)
2512}
2513
Jihoon Kang1c92c3e2023-03-23 17:44:51 +00002514func (s *defaultNamingScheme) apiLibraryModuleName(scope *apiScope, baseName string) string {
2515 return scope.apiLibraryModuleName(baseName)
2516}
2517
Jihoon Kangfa4a90d2023-12-20 02:53:38 +00002518func (s *defaultNamingScheme) sourceStubsLibraryModuleName(scope *apiScope, baseName string) string {
Jihoon Kang1147b312023-06-08 23:25:57 +00002519 return scope.sourceStubLibraryModuleName(baseName)
2520}
2521
Jihoon Kangfa4a90d2023-12-20 02:53:38 +00002522func (s *defaultNamingScheme) exportableStubsLibraryModuleName(scope *apiScope, baseName string) string {
2523 return scope.exportableStubsLibraryModuleName(baseName)
2524}
2525
2526func (s *defaultNamingScheme) exportableSourceStubsLibraryModuleName(scope *apiScope, baseName string) string {
2527 return scope.exportableSourceStubsLibraryModuleName(baseName)
2528}
2529
Paul Duffin1b1e8062020-05-08 13:44:43 +01002530var _ sdkLibraryComponentNamingScheme = (*defaultNamingScheme)(nil)
2531
Jihoon Kangfa4a90d2023-12-20 02:53:38 +00002532func hasStubsLibrarySuffix(name string, apiScope *apiScope) bool {
2533 return strings.HasSuffix(name, apiScope.stubsLibraryModuleNameSuffix()) ||
2534 strings.HasSuffix(name, apiScope.exportableStubsLibraryModuleNameSuffix())
2535}
2536
Jaewoong Jungbc15e3a2021-03-10 17:02:43 -08002537func moduleStubLinkType(name string) (stub bool, ret sdkLinkType) {
Jihoon Kang1147b312023-06-08 23:25:57 +00002538 name = strings.TrimSuffix(name, ".from-source")
2539
Anton Hansson2d0c1942020-05-25 12:20:51 +01002540 // This suffix-based approach is fragile and could potentially mis-trigger.
2541 // TODO(b/155164730): Clean this up when modules no longer reference sdk_lib stubs directly.
Jihoon Kangfa4a90d2023-12-20 02:53:38 +00002542 if hasStubsLibrarySuffix(name, apiScopePublic) {
Anton Hansson08f476b2021-04-07 15:32:19 +01002543 if name == "hwbinder.stubs" || name == "libcore_private.stubs" {
2544 // Due to a previous bug, these modules were not considered stubs, so we retain that.
2545 return false, javaPlatform
2546 }
Anton Hansson2d0c1942020-05-25 12:20:51 +01002547 return true, javaSdk
2548 }
Jihoon Kangfa4a90d2023-12-20 02:53:38 +00002549 if hasStubsLibrarySuffix(name, apiScopeSystem) {
Anton Hansson2d0c1942020-05-25 12:20:51 +01002550 return true, javaSystem
2551 }
Jihoon Kangfa4a90d2023-12-20 02:53:38 +00002552 if hasStubsLibrarySuffix(name, apiScopeModuleLib) {
Anton Hansson2d0c1942020-05-25 12:20:51 +01002553 return true, javaModule
2554 }
Jihoon Kangfa4a90d2023-12-20 02:53:38 +00002555 if hasStubsLibrarySuffix(name, apiScopeTest) {
Anton Hansson2d0c1942020-05-25 12:20:51 +01002556 return true, javaSystem
2557 }
Jihoon Kangfa4a90d2023-12-20 02:53:38 +00002558 if hasStubsLibrarySuffix(name, apiScopeSystemServer) {
Jihoon Kang1147b312023-06-08 23:25:57 +00002559 return true, javaSystemServer
2560 }
Anton Hansson2d0c1942020-05-25 12:20:51 +01002561 return false, javaPlatform
2562}
2563
Jaewoong Jung4f158ee2019-07-11 10:05:35 -07002564// java_sdk_library is a special Java library that provides optional platform APIs to apps.
2565// In practice, it can be viewed as a combination of several modules: 1) stubs library that clients
2566// are linked against to, 2) droiddoc module that internally generates API stubs source files,
2567// 3) the real runtime shared library that implements the APIs, and 4) XML file for adding
2568// the runtime lib to the classpath at runtime if requested via <uses-library>.
Inseob Kimc0907f12019-02-08 21:00:45 +09002569func SdkLibraryFactory() android.Module {
2570 module := &SdkLibrary{}
Paul Duffinc3091c82020-05-08 14:16:20 +01002571
2572 // Initialize information common between source and prebuilt.
Paul Duffin71b33cc2021-06-23 11:39:47 +01002573 module.initCommon(module)
Paul Duffinc3091c82020-05-08 14:16:20 +01002574
Inseob Kimc0907f12019-02-08 21:00:45 +09002575 module.InitSdkLibraryProperties()
Jooyung Han58f26ab2019-12-18 15:34:32 +09002576 android.InitApexModule(module)
Sundong Ahn054b19a2018-10-19 13:46:09 +09002577 InitJavaModule(module, android.HostAndDeviceSupported)
Paul Duffin3375e352020-04-28 10:44:03 +01002578
2579 // Initialize the map from scope to scope specific properties.
2580 scopeToProperties := make(map[*apiScope]*ApiScopeProperties)
Jihoon Kang98aa8fa2024-06-07 11:06:57 +00002581 for _, scope := range AllApiScopes {
Paul Duffin3375e352020-04-28 10:44:03 +01002582 scopeToProperties[scope] = scope.scopeSpecificProperties(module)
2583 }
2584 module.scopeToProperties = scopeToProperties
2585
Paul Duffin4911a892020-04-29 23:35:13 +01002586 // Add the properties containing visibility rules so that they are checked.
Paul Duffin5df79302020-05-16 15:52:12 +01002587 android.AddVisibilityProperty(module, "impl_library_visibility", &module.sdkLibraryProperties.Impl_library_visibility)
Paul Duffin4911a892020-04-29 23:35:13 +01002588 android.AddVisibilityProperty(module, "stubs_library_visibility", &module.sdkLibraryProperties.Stubs_library_visibility)
2589 android.AddVisibilityProperty(module, "stubs_source_visibility", &module.sdkLibraryProperties.Stubs_source_visibility)
2590
Paul Duffin1b1e8062020-05-08 13:44:43 +01002591 module.SetDefaultableHook(func(ctx android.DefaultableHookContext) {
Paul Duffindfa131e2020-05-15 20:37:11 +01002592 // If no implementation is required then it cannot be used as a shared library
2593 // either.
2594 if !module.requiresRuntimeImplementationLibrary() {
2595 // If shared_library has been explicitly set to true then it is incompatible
2596 // with api_only: true.
2597 if proptools.Bool(module.commonSdkLibraryProperties.Shared_library) {
2598 ctx.PropertyErrorf("api_only/shared_library", "inconsistent settings, shared_library and api_only cannot both be true")
2599 }
2600 // Set shared_library: false.
2601 module.commonSdkLibraryProperties.Shared_library = proptools.BoolPtr(false)
2602 }
2603
Paul Duffin1b1e8062020-05-08 13:44:43 +01002604 if module.initCommonAfterDefaultsApplied(ctx) {
2605 module.CreateInternalModules(ctx)
2606 }
2607 })
Jiyong Parkc678ad32018-04-10 13:07:10 +09002608 return module
2609}
Colin Cross79c7c262019-04-17 11:11:46 -07002610
2611//
2612// SDK library prebuilts
2613//
2614
Paul Duffin56d44902020-01-31 13:36:25 +00002615// Properties associated with each api scope.
2616type sdkLibraryScopeProperties struct {
Colin Cross79c7c262019-04-17 11:11:46 -07002617 Jars []string `android:"path"`
2618
2619 Sdk_version *string
2620
Colin Cross79c7c262019-04-17 11:11:46 -07002621 // List of shared java libs that this module has dependencies to
2622 Libs []string
Paul Duffin3d1248c2020-04-09 00:10:17 +01002623
Paul Duffinc8782502020-04-29 20:45:27 +01002624 // The stubs source.
Paul Duffin3d1248c2020-04-09 00:10:17 +01002625 Stub_srcs []string `android:"path"`
Paul Duffin1fd005d2020-04-09 01:08:11 +01002626
2627 // The current.txt
Paul Duffin0f8faff2020-05-20 16:18:00 +01002628 Current_api *string `android:"path"`
Paul Duffin1fd005d2020-04-09 01:08:11 +01002629
2630 // The removed.txt
Paul Duffin0f8faff2020-05-20 16:18:00 +01002631 Removed_api *string `android:"path"`
Anton Hanssond78eb762021-09-21 15:25:12 +01002632
2633 // Annotation zip
2634 Annotations *string `android:"path"`
Colin Cross79c7c262019-04-17 11:11:46 -07002635}
2636
Paul Duffin56d44902020-01-31 13:36:25 +00002637type sdkLibraryImportProperties struct {
Paul Duffinfcfd7912020-01-31 17:54:30 +00002638 // List of shared java libs, common to all scopes, that this module has
2639 // dependencies to
2640 Libs []string
Paul Duffin1267d872021-04-16 17:21:36 +01002641
2642 // If set to true, compile dex files for the stubs. Defaults to false.
2643 Compile_dex *bool
Paul Duffin869de142021-07-15 14:14:41 +01002644
2645 // If not empty, classes are restricted to the specified packages and their sub-packages.
Paul Duffin869de142021-07-15 14:14:41 +01002646 Permitted_packages []string
Spandan Das23956d12024-01-19 00:22:22 +00002647
2648 // Name of the source soong module that gets shadowed by this prebuilt
2649 // If unspecified, follows the naming convention that the source module of
2650 // the prebuilt is Name() without "prebuilt_" prefix
2651 Source_module_name *string
Paul Duffin56d44902020-01-31 13:36:25 +00002652}
2653
Paul Duffineedc5d52020-06-12 17:46:39 +01002654type SdkLibraryImport struct {
Colin Cross79c7c262019-04-17 11:11:46 -07002655 android.ModuleBase
2656 android.DefaultableModuleBase
2657 prebuilt android.Prebuilt
Paul Duffindd46f712020-02-10 13:37:10 +00002658 android.ApexModuleBase
Colin Cross79c7c262019-04-17 11:11:46 -07002659
Paul Duffin37856732021-02-26 14:24:15 +00002660 hiddenAPI
Jiakai Zhang204356f2021-09-09 08:12:46 +00002661 dexpreopter
Paul Duffin37856732021-02-26 14:24:15 +00002662
Colin Cross79c7c262019-04-17 11:11:46 -07002663 properties sdkLibraryImportProperties
2664
Paul Duffin46a26a82020-04-07 19:27:04 +01002665 // Map from api scope to the scope specific property structure.
2666 scopeProperties map[*apiScope]*sdkLibraryScopeProperties
2667
Paul Duffin56d44902020-01-31 13:36:25 +00002668 commonToSdkLibraryAndImport
Paul Duffineedc5d52020-06-12 17:46:39 +01002669
Paul Duffineedc5d52020-06-12 17:46:39 +01002670 // The reference to the xml permissions module created by the source module.
2671 // Is nil if the source module does not exist.
2672 xmlPermissionsFileModule *sdkLibraryXml
Paul Duffin39853512021-02-26 11:09:39 +00002673
Jeongik Chad5fe8782021-07-08 01:13:11 +09002674 // Build path to the dex implementation jar obtained from the prebuilt_apex, if any.
Spandan Dasfae468e2023-12-12 23:23:53 +00002675 dexJarFile OptionalDexJarPath
2676 dexJarFileErr error
Jeongik Chad5fe8782021-07-08 01:13:11 +09002677
2678 // Expected install file path of the source module(sdk_library)
2679 // or dex implementation jar obtained from the prebuilt_apex, if any.
2680 installFile android.Path
Colin Cross79c7c262019-04-17 11:11:46 -07002681}
2682
Paul Duffineedc5d52020-06-12 17:46:39 +01002683var _ SdkLibraryDependency = (*SdkLibraryImport)(nil)
Colin Cross79c7c262019-04-17 11:11:46 -07002684
Paul Duffin46a26a82020-04-07 19:27:04 +01002685// The type of a structure that contains a field of type sdkLibraryScopeProperties
2686// for each apiscope in allApiScopes, e.g. something like:
Colin Crossd079e0b2022-08-16 10:27:33 -07002687//
2688// struct {
2689// Public sdkLibraryScopeProperties
2690// System sdkLibraryScopeProperties
2691// ...
2692// }
Paul Duffin46a26a82020-04-07 19:27:04 +01002693var allScopeStructType = createAllScopePropertiesStructType()
2694
2695// Dynamically create a structure type for each apiscope in allApiScopes.
2696func createAllScopePropertiesStructType() reflect.Type {
2697 var fields []reflect.StructField
Jihoon Kang98aa8fa2024-06-07 11:06:57 +00002698 for _, apiScope := range AllApiScopes {
Paul Duffin46a26a82020-04-07 19:27:04 +01002699 field := reflect.StructField{
2700 Name: apiScope.fieldName,
2701 Type: reflect.TypeOf(sdkLibraryScopeProperties{}),
2702 }
2703 fields = append(fields, field)
2704 }
2705
2706 return reflect.StructOf(fields)
2707}
2708
2709// Create an instance of the scope specific structure type and return a map
2710// from apiscope to a pointer to each scope specific field.
2711func createPropertiesInstance() (interface{}, map[*apiScope]*sdkLibraryScopeProperties) {
2712 allScopePropertiesPtr := reflect.New(allScopeStructType)
2713 allScopePropertiesStruct := allScopePropertiesPtr.Elem()
2714 scopeProperties := make(map[*apiScope]*sdkLibraryScopeProperties)
2715
Jihoon Kang98aa8fa2024-06-07 11:06:57 +00002716 for _, apiScope := range AllApiScopes {
Paul Duffin46a26a82020-04-07 19:27:04 +01002717 field := allScopePropertiesStruct.FieldByName(apiScope.fieldName)
2718 scopeProperties[apiScope] = field.Addr().Interface().(*sdkLibraryScopeProperties)
2719 }
2720
2721 return allScopePropertiesPtr.Interface(), scopeProperties
2722}
2723
Jaewoong Jung4f158ee2019-07-11 10:05:35 -07002724// java_sdk_library_import imports a prebuilt java_sdk_library.
Colin Cross79c7c262019-04-17 11:11:46 -07002725func sdkLibraryImportFactory() android.Module {
Paul Duffineedc5d52020-06-12 17:46:39 +01002726 module := &SdkLibraryImport{}
Colin Cross79c7c262019-04-17 11:11:46 -07002727
Paul Duffin46a26a82020-04-07 19:27:04 +01002728 allScopeProperties, scopeToProperties := createPropertiesInstance()
2729 module.scopeProperties = scopeToProperties
Jiakai Zhang9c4dc192023-02-09 00:09:24 +08002730 module.AddProperties(&module.properties, allScopeProperties, &module.importDexpreoptProperties)
Colin Cross79c7c262019-04-17 11:11:46 -07002731
Paul Duffinc3091c82020-05-08 14:16:20 +01002732 // Initialize information common between source and prebuilt.
Paul Duffin71b33cc2021-06-23 11:39:47 +01002733 module.initCommon(module)
Paul Duffinc3091c82020-05-08 14:16:20 +01002734
Paul Duffin0bdcb272020-02-06 15:24:57 +00002735 android.InitPrebuiltModule(module, &[]string{""})
Paul Duffindd46f712020-02-10 13:37:10 +00002736 android.InitApexModule(module)
Colin Cross79c7c262019-04-17 11:11:46 -07002737 InitJavaModule(module, android.HostAndDeviceSupported)
2738
Paul Duffin1b1e8062020-05-08 13:44:43 +01002739 module.SetDefaultableHook(func(mctx android.DefaultableHookContext) {
2740 if module.initCommonAfterDefaultsApplied(mctx) {
2741 module.createInternalModules(mctx)
2742 }
2743 })
Colin Cross79c7c262019-04-17 11:11:46 -07002744 return module
2745}
2746
Paul Duffin630b11e2021-07-15 13:35:26 +01002747var _ PermittedPackagesForUpdatableBootJars = (*SdkLibraryImport)(nil)
2748
2749func (module *SdkLibraryImport) PermittedPackagesForUpdatableBootJars() []string {
2750 return module.properties.Permitted_packages
2751}
2752
Paul Duffineedc5d52020-06-12 17:46:39 +01002753func (module *SdkLibraryImport) Prebuilt() *android.Prebuilt {
Colin Cross79c7c262019-04-17 11:11:46 -07002754 return &module.prebuilt
2755}
2756
Paul Duffineedc5d52020-06-12 17:46:39 +01002757func (module *SdkLibraryImport) Name() string {
Colin Cross79c7c262019-04-17 11:11:46 -07002758 return module.prebuilt.Name(module.ModuleBase.Name())
2759}
2760
Spandan Das23956d12024-01-19 00:22:22 +00002761func (module *SdkLibraryImport) BaseModuleName() string {
2762 return proptools.StringDefault(module.properties.Source_module_name, module.ModuleBase.Name())
2763}
2764
Paul Duffineedc5d52020-06-12 17:46:39 +01002765func (module *SdkLibraryImport) createInternalModules(mctx android.DefaultableHookContext) {
Colin Cross79c7c262019-04-17 11:11:46 -07002766
Paul Duffin50061512020-01-21 16:31:05 +00002767 // If the build is configured to use prebuilts then force this to be preferred.
Jeongik Cha816a23a2020-07-08 01:09:23 +09002768 if mctx.Config().AlwaysUsePrebuiltSdks() {
Paul Duffin50061512020-01-21 16:31:05 +00002769 module.prebuilt.ForcePrefer()
2770 }
2771
Paul Duffin46a26a82020-04-07 19:27:04 +01002772 for apiScope, scopeProperties := range module.scopeProperties {
Paul Duffin56d44902020-01-31 13:36:25 +00002773 if len(scopeProperties.Jars) == 0 {
2774 continue
2775 }
2776
Paul Duffinbbb546b2020-04-09 00:07:11 +01002777 module.createJavaImportForStubs(mctx, apiScope, scopeProperties)
Paul Duffin3d1248c2020-04-09 00:10:17 +01002778
Paul Duffin0f8faff2020-05-20 16:18:00 +01002779 if len(scopeProperties.Stub_srcs) > 0 {
2780 module.createPrebuiltStubsSources(mctx, apiScope, scopeProperties)
2781 }
Jihoon Kang71c86832023-09-13 01:01:53 +00002782
2783 if scopeProperties.Current_api != nil {
2784 module.createPrebuiltApiContribution(mctx, apiScope, scopeProperties)
2785 }
Paul Duffin56d44902020-01-31 13:36:25 +00002786 }
Colin Cross79c7c262019-04-17 11:11:46 -07002787
2788 javaSdkLibraries := javaSdkLibraries(mctx.Config())
2789 javaSdkLibrariesLock.Lock()
2790 defer javaSdkLibrariesLock.Unlock()
2791 *javaSdkLibraries = append(*javaSdkLibraries, module.BaseModuleName())
2792}
2793
Paul Duffineedc5d52020-06-12 17:46:39 +01002794func (module *SdkLibraryImport) createJavaImportForStubs(mctx android.DefaultableHookContext, apiScope *apiScope, scopeProperties *sdkLibraryScopeProperties) {
Paul Duffinbbb546b2020-04-09 00:07:11 +01002795 // Creates a java import for the jar with ".stubs" suffix
2796 props := struct {
Spandan Das23956d12024-01-19 00:22:22 +00002797 Name *string
2798 Source_module_name *string
2799 Created_by_java_sdk_library_name *string
2800 Sdk_version *string
2801 Libs []string
2802 Jars []string
2803 Compile_dex *bool
Jihoon Kangfe914ed2024-02-12 22:49:21 +00002804 Is_stubs_module *bool
Paul Duffinbf4de042022-09-27 12:41:52 +01002805
2806 android.UserSuppliedPrebuiltProperties
Paul Duffinbbb546b2020-04-09 00:07:11 +01002807 }{}
Paul Duffinc3091c82020-05-08 14:16:20 +01002808 props.Name = proptools.StringPtr(module.stubsLibraryModuleName(apiScope))
Spandan Das23956d12024-01-19 00:22:22 +00002809 props.Source_module_name = proptools.StringPtr(apiScope.stubsLibraryModuleName(module.BaseModuleName()))
2810 props.Created_by_java_sdk_library_name = proptools.StringPtr(module.RootLibraryName())
Paul Duffinbbb546b2020-04-09 00:07:11 +01002811 props.Sdk_version = scopeProperties.Sdk_version
2812 // Prepend any of the libs from the legacy public properties to the libs for each of the
2813 // scopes to avoid having to duplicate them in each scope.
2814 props.Libs = append(module.properties.Libs, scopeProperties.Libs...)
2815 props.Jars = scopeProperties.Jars
Paul Duffin1dbe3ca2020-05-16 09:57:59 +01002816
Paul Duffin38b57852020-05-13 16:08:09 +01002817 // The imports are preferred if the java_sdk_library_import is preferred.
Paul Duffinbf4de042022-09-27 12:41:52 +01002818 props.CopyUserSuppliedPropertiesFromPrebuilt(&module.prebuilt)
Paul Duffin859fe962020-05-15 10:20:31 +01002819
Paul Duffin1267d872021-04-16 17:21:36 +01002820 // The imports need to be compiled to dex if the java_sdk_library_import requests it.
Paul Duffinf4600f62021-05-13 22:34:45 +01002821 compileDex := module.properties.Compile_dex
2822 if module.stubLibrariesCompiledForDex() {
2823 compileDex = proptools.BoolPtr(true)
2824 }
2825 props.Compile_dex = compileDex
Jihoon Kangfe914ed2024-02-12 22:49:21 +00002826 props.Is_stubs_module = proptools.BoolPtr(true)
Paul Duffin1267d872021-04-16 17:21:36 +01002827
Paul Duffin859fe962020-05-15 10:20:31 +01002828 mctx.CreateModule(ImportFactory, &props, module.sdkComponentPropertiesForChildLibrary())
Paul Duffinbbb546b2020-04-09 00:07:11 +01002829}
2830
Paul Duffineedc5d52020-06-12 17:46:39 +01002831func (module *SdkLibraryImport) createPrebuiltStubsSources(mctx android.DefaultableHookContext, apiScope *apiScope, scopeProperties *sdkLibraryScopeProperties) {
Paul Duffin3d1248c2020-04-09 00:10:17 +01002832 props := struct {
Spandan Das23956d12024-01-19 00:22:22 +00002833 Name *string
2834 Source_module_name *string
2835 Created_by_java_sdk_library_name *string
2836 Srcs []string
Paul Duffinbf4de042022-09-27 12:41:52 +01002837
2838 android.UserSuppliedPrebuiltProperties
Paul Duffin3d1248c2020-04-09 00:10:17 +01002839 }{}
Paul Duffinc3091c82020-05-08 14:16:20 +01002840 props.Name = proptools.StringPtr(module.stubsSourceModuleName(apiScope))
Spandan Das23956d12024-01-19 00:22:22 +00002841 props.Source_module_name = proptools.StringPtr(apiScope.stubsSourceModuleName(module.BaseModuleName()))
2842 props.Created_by_java_sdk_library_name = proptools.StringPtr(module.RootLibraryName())
Paul Duffin3d1248c2020-04-09 00:10:17 +01002843 props.Srcs = scopeProperties.Stub_srcs
Paul Duffin38b57852020-05-13 16:08:09 +01002844
2845 // The stubs source is preferred if the java_sdk_library_import is preferred.
Paul Duffinbf4de042022-09-27 12:41:52 +01002846 props.CopyUserSuppliedPropertiesFromPrebuilt(&module.prebuilt)
2847
Spandan Das2cc80ba2023-10-27 17:21:52 +00002848 mctx.CreateModule(PrebuiltStubsSourcesFactory, &props, module.sdkComponentPropertiesForChildLibrary())
Paul Duffin3d1248c2020-04-09 00:10:17 +01002849}
2850
Jihoon Kang71c86832023-09-13 01:01:53 +00002851func (module *SdkLibraryImport) createPrebuiltApiContribution(mctx android.DefaultableHookContext, apiScope *apiScope, scopeProperties *sdkLibraryScopeProperties) {
2852 api_file := scopeProperties.Current_api
2853 api_surface := &apiScope.name
2854
2855 props := struct {
Spandan Das23956d12024-01-19 00:22:22 +00002856 Name *string
2857 Source_module_name *string
2858 Created_by_java_sdk_library_name *string
2859 Api_surface *string
2860 Api_file *string
2861 Visibility []string
Jihoon Kang71c86832023-09-13 01:01:53 +00002862 }{}
2863
2864 props.Name = proptools.StringPtr(module.stubsSourceModuleName(apiScope) + ".api.contribution")
Spandan Das23956d12024-01-19 00:22:22 +00002865 props.Source_module_name = proptools.StringPtr(apiScope.stubsSourceModuleName(module.BaseModuleName()) + ".api.contribution")
2866 props.Created_by_java_sdk_library_name = proptools.StringPtr(module.RootLibraryName())
Jihoon Kang71c86832023-09-13 01:01:53 +00002867 props.Api_surface = api_surface
2868 props.Api_file = api_file
2869 props.Visibility = []string{"//visibility:override", "//visibility:public"}
2870
Spandan Das2cc80ba2023-10-27 17:21:52 +00002871 mctx.CreateModule(ApiContributionImportFactory, &props, module.sdkComponentPropertiesForChildLibrary())
Jihoon Kang71c86832023-09-13 01:01:53 +00002872}
2873
Paul Duffin44f1d842020-06-26 20:17:02 +01002874// Add the dependencies on the child module in the component deps mutator so that it
2875// creates references to the prebuilt and not the source modules.
2876func (module *SdkLibraryImport) ComponentDepsMutator(ctx android.BottomUpMutatorContext) {
Paul Duffin46a26a82020-04-07 19:27:04 +01002877 for apiScope, scopeProperties := range module.scopeProperties {
Paul Duffin56d44902020-01-31 13:36:25 +00002878 if len(scopeProperties.Jars) == 0 {
2879 continue
2880 }
2881
2882 // Add dependencies to the prebuilt stubs library
Jihoon Kangb7431552024-01-22 19:40:08 +00002883 ctx.AddVariationDependencies(nil, apiScope.prebuiltStubsTag, android.PrebuiltNameFromSource(module.stubsLibraryModuleName(apiScope)))
Paul Duffin0f8faff2020-05-20 16:18:00 +01002884
2885 if len(scopeProperties.Stub_srcs) > 0 {
2886 // Add dependencies to the prebuilt stubs source library
Paul Duffin864116c2021-04-02 10:24:13 +01002887 ctx.AddVariationDependencies(nil, apiScope.stubsSourceTag, android.PrebuiltNameFromSource(module.stubsSourceModuleName(apiScope)))
Paul Duffin0f8faff2020-05-20 16:18:00 +01002888 }
Paul Duffin56d44902020-01-31 13:36:25 +00002889 }
Paul Duffin44f1d842020-06-26 20:17:02 +01002890}
2891
2892// Add other dependencies as normal.
2893func (module *SdkLibraryImport) DepsMutator(ctx android.BottomUpMutatorContext) {
Paul Duffineedc5d52020-06-12 17:46:39 +01002894
2895 implName := module.implLibraryModuleName()
2896 if ctx.OtherModuleExists(implName) {
2897 ctx.AddVariationDependencies(nil, implLibraryTag, implName)
2898
2899 xmlPermissionsModuleName := module.xmlPermissionsModuleName()
2900 if module.sharedLibrary() && ctx.OtherModuleExists(xmlPermissionsModuleName) {
2901 // Add dependency to the rule for generating the xml permissions file
2902 ctx.AddDependency(module, xmlPermissionsFileTag, xmlPermissionsModuleName)
2903 }
2904 }
Colin Cross79c7c262019-04-17 11:11:46 -07002905}
2906
Jiyong Park45bf82e2020-12-15 22:29:02 +09002907var _ android.ApexModule = (*SdkLibraryImport)(nil)
2908
2909// Implements android.ApexModule
Paul Duffineedc5d52020-06-12 17:46:39 +01002910func (module *SdkLibraryImport) DepIsInSameApex(mctx android.BaseModuleContext, dep android.Module) bool {
2911 depTag := mctx.OtherModuleDependencyTag(dep)
2912 if depTag == xmlPermissionsFileTag {
2913 return true
2914 }
2915
2916 // None of the other dependencies of the java_sdk_library_import are in the same apex
2917 // as the one that references this module.
2918 return false
2919}
2920
Jiyong Park45bf82e2020-12-15 22:29:02 +09002921// Implements android.ApexModule
Dan Albertc8060532020-07-22 22:32:17 -07002922func (module *SdkLibraryImport) ShouldSupportSdkVersion(ctx android.BaseModuleContext,
2923 sdkVersion android.ApiLevel) error {
Jooyung Han749dc692020-04-15 11:03:39 +09002924 // we don't check prebuilt modules for sdk_version
2925 return nil
2926}
2927
Paul Duffinea8f8082021-06-24 13:25:57 +01002928// Implements android.ApexModule
2929func (module *SdkLibraryImport) UniqueApexVariations() bool {
2930 return module.uniqueApexVariations()
2931}
2932
Paul Duffin09817d62022-04-28 17:45:11 +01002933// MinSdkVersion - Implements hiddenAPIModule
Spandan Das8c9ae7e2023-03-03 21:20:36 +00002934func (module *SdkLibraryImport) MinSdkVersion(ctx android.EarlyModuleContext) android.ApiLevel {
2935 return android.NoneApiLevel
Paul Duffin09817d62022-04-28 17:45:11 +01002936}
2937
2938var _ hiddenAPIModule = (*SdkLibraryImport)(nil)
2939
Paul Duffineedc5d52020-06-12 17:46:39 +01002940func (module *SdkLibraryImport) OutputFiles(tag string) (android.Paths, error) {
Paul Duffin1e940d52022-04-29 14:21:25 +01002941 paths, err := module.commonOutputFiles(tag)
2942 if paths != nil || err != nil {
2943 return paths, err
2944 }
2945 if module.implLibraryModule != nil {
2946 return module.implLibraryModule.OutputFiles(tag)
2947 } else {
2948 return nil, nil
2949 }
Paul Duffin46dc45a2020-05-14 15:39:10 +01002950}
2951
Paul Duffineedc5d52020-06-12 17:46:39 +01002952func (module *SdkLibraryImport) GenerateAndroidBuildActions(ctx android.ModuleContext) {
Paul Duffina2ae7e02020-09-11 11:55:00 +01002953 module.generateCommonBuildActions(ctx)
2954
Jeongik Chad5fe8782021-07-08 01:13:11 +09002955 // Assume that source module(sdk_library) is installed in /<sdk_library partition>/framework
2956 module.installFile = android.PathForModuleInstall(ctx, "framework", module.Stem()+".jar")
2957
Paul Duffin0f8faff2020-05-20 16:18:00 +01002958 // Record the paths to the prebuilt stubs library and stubs source.
Colin Cross79c7c262019-04-17 11:11:46 -07002959 ctx.VisitDirectDeps(func(to android.Module) {
2960 tag := ctx.OtherModuleDependencyTag(to)
2961
Paul Duffin0f8faff2020-05-20 16:18:00 +01002962 // Extract information from any of the scope specific dependencies.
2963 if scopeTag, ok := tag.(scopeDependencyTag); ok {
2964 apiScope := scopeTag.apiScope
2965 scopePaths := module.getScopePathsCreateIfNeeded(apiScope)
2966
2967 // Extract information from the dependency. The exact information extracted
2968 // is determined by the nature of the dependency which is determined by the tag.
2969 scopeTag.extractDepInfo(ctx, to, scopePaths)
Paul Duffineedc5d52020-06-12 17:46:39 +01002970 } else if tag == implLibraryTag {
2971 if implLibrary, ok := to.(*Library); ok {
2972 module.implLibraryModule = implLibrary
2973 } else {
2974 ctx.ModuleErrorf("implementation library must be of type *java.Library but was %T", to)
2975 }
2976 } else if tag == xmlPermissionsFileTag {
2977 if xmlPermissionsFileModule, ok := to.(*sdkLibraryXml); ok {
2978 module.xmlPermissionsFileModule = xmlPermissionsFileModule
2979 } else {
2980 ctx.ModuleErrorf("xml permissions file module must be of type *sdkLibraryXml but was %T", to)
2981 }
Colin Cross79c7c262019-04-17 11:11:46 -07002982 }
2983 })
Paul Duffin0f8faff2020-05-20 16:18:00 +01002984
2985 // Populate the scope paths with information from the properties.
2986 for apiScope, scopeProperties := range module.scopeProperties {
2987 if len(scopeProperties.Jars) == 0 {
2988 continue
2989 }
2990
2991 paths := module.getScopePathsCreateIfNeeded(apiScope)
Anton Hanssond78eb762021-09-21 15:25:12 +01002992 paths.annotationsZip = android.OptionalPathForModuleSrc(ctx, scopeProperties.Annotations)
Paul Duffin0f8faff2020-05-20 16:18:00 +01002993 paths.currentApiFilePath = android.OptionalPathForModuleSrc(ctx, scopeProperties.Current_api)
2994 paths.removedApiFilePath = android.OptionalPathForModuleSrc(ctx, scopeProperties.Removed_api)
2995 }
Paul Duffin39853512021-02-26 11:09:39 +00002996
2997 if ctx.Device() {
2998 // If this is a variant created for a prebuilt_apex then use the dex implementation jar
2999 // obtained from the associated deapexer module.
Colin Crossff694a82023-12-13 15:54:49 -08003000 ai, _ := android.ModuleProvider(ctx, android.ApexInfoProvider)
Paul Duffin39853512021-02-26 11:09:39 +00003001 if ai.ForPrebuiltApex {
Paul Duffin39853512021-02-26 11:09:39 +00003002 // Get the path of the dex implementation jar from the `deapexer` module.
Spandan Dasfae468e2023-12-12 23:23:53 +00003003 di, err := android.FindDeapexerProviderForModule(ctx)
3004 if err != nil {
3005 // An error was found, possibly due to multiple apexes in the tree that export this library
3006 // Defer the error till a client tries to call DexJarBuildPath
3007 module.dexJarFileErr = err
Spandan Das3a392012024-01-17 18:26:27 +00003008 module.initHiddenAPIError(err)
Spandan Dasfae468e2023-12-12 23:23:53 +00003009 return
Martin Stjernholm44825602021-09-17 01:44:12 +01003010 }
Spandan Das5be63332023-12-13 00:06:32 +00003011 dexJarFileApexRootRelative := ApexRootRelativePathToJavaLib(module.BaseModuleName())
Jiakai Zhang81e46812023-02-08 21:56:07 +08003012 if dexOutputPath := di.PrebuiltExportPath(dexJarFileApexRootRelative); dexOutputPath != nil {
Martin Stjernholm8be1e6d2021-09-15 03:34:04 +01003013 dexJarFile := makeDexJarPathFromPath(dexOutputPath)
3014 module.dexJarFile = dexJarFile
Jiakai Zhang204356f2021-09-09 08:12:46 +00003015 installPath := android.PathForModuleInPartitionInstall(
Jiakai Zhang81e46812023-02-08 21:56:07 +08003016 ctx, "apex", ai.ApexVariationName, dexJarFileApexRootRelative)
Jiakai Zhang204356f2021-09-09 08:12:46 +00003017 module.installFile = installPath
Martin Stjernholm8be1e6d2021-09-15 03:34:04 +01003018 module.initHiddenAPI(ctx, dexJarFile, module.findScopePaths(apiScopePublic).stubsImplPath[0], nil)
Jiakai Zhang204356f2021-09-09 08:12:46 +00003019
Spandan Dase21a8d42024-01-23 23:56:29 +00003020 module.dexpreopter.installPath = module.dexpreopter.getInstallPath(ctx, android.RemoveOptionalPrebuiltPrefix(ctx.ModuleName()), installPath)
Jiakai Zhang204356f2021-09-09 08:12:46 +00003021 module.dexpreopter.isSDKLibrary = true
Spandan Dase21a8d42024-01-23 23:56:29 +00003022 module.dexpreopter.uncompressedDex = shouldUncompressDex(ctx, android.RemoveOptionalPrebuiltPrefix(ctx.ModuleName()), &module.dexpreopter)
Jiakai Zhang81e46812023-02-08 21:56:07 +08003023
3024 if profilePath := di.PrebuiltExportPath(dexJarFileApexRootRelative + ".prof"); profilePath != nil {
3025 module.dexpreopter.inputProfilePathOnHost = profilePath
3026 }
Paul Duffin39853512021-02-26 11:09:39 +00003027 } else {
3028 // This should never happen as a variant for a prebuilt_apex is only created if the
3029 // prebuilt_apex has been configured to export the java library dex file.
Martin Stjernholm44825602021-09-17 01:44:12 +01003030 ctx.ModuleErrorf("internal error: no dex implementation jar available from prebuilt APEX %s", di.ApexModuleName())
Paul Duffin39853512021-02-26 11:09:39 +00003031 }
3032 }
3033 }
Colin Cross79c7c262019-04-17 11:11:46 -07003034}
3035
Jiyong Parkf1691d22021-03-29 20:11:58 +09003036func (module *SdkLibraryImport) sdkJars(ctx android.BaseModuleContext, sdkVersion android.SdkSpec, headerJars bool) android.Paths {
Paul Duffineedc5d52020-06-12 17:46:39 +01003037
3038 // For consistency with SdkLibrary make the implementation jar available to libraries that
3039 // are within the same APEX.
3040 implLibraryModule := module.implLibraryModule
Colin Cross56a83212020-09-15 18:30:11 -07003041 if implLibraryModule != nil && withinSameApexesAs(ctx, module) {
Paul Duffineedc5d52020-06-12 17:46:39 +01003042 if headerJars {
3043 return implLibraryModule.HeaderJars()
3044 } else {
3045 return implLibraryModule.ImplementationJars()
3046 }
3047 }
3048
Paul Duffin23970f42020-05-20 14:20:02 +01003049 return module.selectHeaderJarsForSdkVersion(ctx, sdkVersion)
Paul Duffin56d44902020-01-31 13:36:25 +00003050}
3051
Colin Cross79c7c262019-04-17 11:11:46 -07003052// to satisfy SdkLibraryDependency interface
Jiyong Parkf1691d22021-03-29 20:11:58 +09003053func (module *SdkLibraryImport) SdkHeaderJars(ctx android.BaseModuleContext, sdkVersion android.SdkSpec) android.Paths {
Colin Cross79c7c262019-04-17 11:11:46 -07003054 // This module is just a wrapper for the prebuilt stubs.
Paul Duffineedc5d52020-06-12 17:46:39 +01003055 return module.sdkJars(ctx, sdkVersion, true)
Colin Cross79c7c262019-04-17 11:11:46 -07003056}
3057
Ulya Trafimovichdbf31662020-12-17 12:07:54 +00003058// to satisfy UsesLibraryDependency interface
Spandan Das59a4a2b2024-01-09 21:35:56 +00003059func (module *SdkLibraryImport) DexJarBuildPath(ctx android.ModuleErrorfContext) OptionalDexJarPath {
Paul Duffin39853512021-02-26 11:09:39 +00003060 // The dex implementation jar extracted from the .apex file should be used in preference to the
3061 // source.
Spandan Dasfae468e2023-12-12 23:23:53 +00003062 if module.dexJarFileErr != nil {
Spandan Das59a4a2b2024-01-09 21:35:56 +00003063 ctx.ModuleErrorf(module.dexJarFileErr.Error())
Spandan Dasfae468e2023-12-12 23:23:53 +00003064 }
Martin Stjernholm8be1e6d2021-09-15 03:34:04 +01003065 if module.dexJarFile.IsSet() {
Paul Duffin39853512021-02-26 11:09:39 +00003066 return module.dexJarFile
3067 }
Paul Duffineedc5d52020-06-12 17:46:39 +01003068 if module.implLibraryModule == nil {
Martin Stjernholm8be1e6d2021-09-15 03:34:04 +01003069 return makeUnsetDexJarPath()
Paul Duffineedc5d52020-06-12 17:46:39 +01003070 } else {
Spandan Das59a4a2b2024-01-09 21:35:56 +00003071 return module.implLibraryModule.DexJarBuildPath(ctx)
Paul Duffineedc5d52020-06-12 17:46:39 +01003072 }
3073}
3074
Ulya Trafimovichdbf31662020-12-17 12:07:54 +00003075// to satisfy UsesLibraryDependency interface
Ulya Trafimovich31e444e2020-08-14 17:32:16 +01003076func (module *SdkLibraryImport) DexJarInstallPath() android.Path {
Jeongik Chad5fe8782021-07-08 01:13:11 +09003077 return module.installFile
Ulya Trafimovich31e444e2020-08-14 17:32:16 +01003078}
3079
Ulya Trafimovichdbf31662020-12-17 12:07:54 +00003080// to satisfy UsesLibraryDependency interface
3081func (module *SdkLibraryImport) ClassLoaderContexts() dexpreopt.ClassLoaderContextMap {
3082 return nil
3083}
3084
Paul Duffineedc5d52020-06-12 17:46:39 +01003085// to satisfy apex.javaDependency interface
3086func (module *SdkLibraryImport) JacocoReportClassesFile() android.Path {
3087 if module.implLibraryModule == nil {
3088 return nil
3089 } else {
3090 return module.implLibraryModule.JacocoReportClassesFile()
3091 }
3092}
3093
3094// to satisfy apex.javaDependency interface
Colin Cross08dca382020-07-21 20:31:17 -07003095func (module *SdkLibraryImport) LintDepSets() LintDepSets {
3096 if module.implLibraryModule == nil {
3097 return LintDepSets{}
3098 } else {
3099 return module.implLibraryModule.LintDepSets()
3100 }
3101}
3102
Spandan Das17854f52022-01-14 21:19:14 +00003103func (module *SdkLibraryImport) GetStrictUpdatabilityLinting() bool {
Jaewoong Jung476b9d62021-05-10 15:30:00 -07003104 if module.implLibraryModule == nil {
3105 return false
3106 } else {
Spandan Das17854f52022-01-14 21:19:14 +00003107 return module.implLibraryModule.GetStrictUpdatabilityLinting()
Jaewoong Jung476b9d62021-05-10 15:30:00 -07003108 }
3109}
3110
Spandan Das17854f52022-01-14 21:19:14 +00003111func (module *SdkLibraryImport) SetStrictUpdatabilityLinting(strictLinting bool) {
Jaewoong Jung476b9d62021-05-10 15:30:00 -07003112 if module.implLibraryModule != nil {
Spandan Das17854f52022-01-14 21:19:14 +00003113 module.implLibraryModule.SetStrictUpdatabilityLinting(strictLinting)
Jaewoong Jung476b9d62021-05-10 15:30:00 -07003114 }
3115}
3116
Colin Cross08dca382020-07-21 20:31:17 -07003117// to satisfy apex.javaDependency interface
Paul Duffineedc5d52020-06-12 17:46:39 +01003118func (module *SdkLibraryImport) Stem() string {
3119 return module.BaseModuleName()
Colin Cross79c7c262019-04-17 11:11:46 -07003120}
Jiyong Parke3833882020-02-17 17:28:10 +09003121
Paul Duffin44b481b2020-06-17 16:59:43 +01003122var _ ApexDependency = (*SdkLibraryImport)(nil)
3123
3124// to satisfy java.ApexDependency interface
3125func (module *SdkLibraryImport) HeaderJars() android.Paths {
3126 if module.implLibraryModule == nil {
3127 return nil
3128 } else {
3129 return module.implLibraryModule.HeaderJars()
3130 }
3131}
3132
3133// to satisfy java.ApexDependency interface
3134func (module *SdkLibraryImport) ImplementationAndResourcesJars() android.Paths {
3135 if module.implLibraryModule == nil {
3136 return nil
3137 } else {
3138 return module.implLibraryModule.ImplementationAndResourcesJars()
3139 }
3140}
3141
Jiakai Zhang204356f2021-09-09 08:12:46 +00003142// to satisfy java.DexpreopterInterface interface
3143func (module *SdkLibraryImport) IsInstallable() bool {
3144 return true
3145}
3146
Paul Duffinfef55002021-06-17 14:56:05 +01003147var _ android.RequiredFilesFromPrebuiltApex = (*SdkLibraryImport)(nil)
3148
Paul Duffinb4bbf2c2021-06-17 15:59:07 +01003149func (module *SdkLibraryImport) RequiredFilesFromPrebuiltApex(ctx android.BaseModuleContext) []string {
Paul Duffinfef55002021-06-17 14:56:05 +01003150 name := module.BaseModuleName()
Jiakai Zhang81e46812023-02-08 21:56:07 +08003151 return requiredFilesFromPrebuiltApexForImport(name, &module.dexpreopter)
Paul Duffinfef55002021-06-17 14:56:05 +01003152}
3153
Spandan Das2ea84dd2024-01-25 22:12:50 +00003154func (j *SdkLibraryImport) UseProfileGuidedDexpreopt() bool {
3155 return proptools.Bool(j.importDexpreoptProperties.Dex_preopt.Profile_guided)
3156}
3157
Jiyong Parke3833882020-02-17 17:28:10 +09003158// java_sdk_library_xml
Jiyong Parke3833882020-02-17 17:28:10 +09003159type sdkLibraryXml struct {
3160 android.ModuleBase
3161 android.DefaultableModuleBase
3162 android.ApexModuleBase
3163
3164 properties sdkLibraryXmlProperties
3165
3166 outputFilePath android.OutputPath
3167 installDirPath android.InstallPath
Colin Cross56a83212020-09-15 18:30:11 -07003168
3169 hideApexVariantFromMake bool
Jiyong Parke3833882020-02-17 17:28:10 +09003170}
3171
3172type sdkLibraryXmlProperties struct {
3173 // canonical name of the lib
3174 Lib_name *string
Pedro Loureiro9956e5e2021-09-07 17:21:59 +00003175
3176 // Signals that this shared library is part of the bootclasspath starting
3177 // on the version indicated in this attribute.
3178 //
3179 // This will make platforms at this level and above to ignore
3180 // <uses-library> tags with this library name because the library is already
3181 // available
3182 On_bootclasspath_since *string
3183
3184 // Signals that this shared library was part of the bootclasspath before
3185 // (but not including) the version indicated in this attribute.
3186 //
3187 // The system will automatically add a <uses-library> tag with this library to
3188 // apps that target any SDK less than the version indicated in this attribute.
3189 On_bootclasspath_before *string
3190
3191 // Indicates that PackageManager should ignore this shared library if the
3192 // platform is below the version indicated in this attribute.
3193 //
3194 // This means that the device won't recognise this library as installed.
3195 Min_device_sdk *string
3196
3197 // Indicates that PackageManager should ignore this shared library if the
3198 // platform is above the version indicated in this attribute.
3199 //
3200 // This means that the device won't recognise this library as installed.
3201 Max_device_sdk *string
Pedro Loureiroc3621422021-09-28 15:40:23 +00003202
3203 // The SdkLibrary's min api level as a string
3204 //
3205 // This value comes from the ApiLevel of the MinSdkVersion property.
3206 Sdk_library_min_api_level *string
Jamie Garsidee570ace2023-11-27 12:07:36 +00003207
3208 // Uses-libs dependencies that the shared library requires to work correctly.
3209 //
3210 // This will add dependency="foo:bar" to the <library> section.
3211 Uses_libs_dependencies []string
Jiyong Parke3833882020-02-17 17:28:10 +09003212}
3213
3214// java_sdk_library_xml builds the permission xml file for a java_sdk_library.
3215// Not to be used directly by users. java_sdk_library internally uses this.
3216func sdkLibraryXmlFactory() android.Module {
3217 module := &sdkLibraryXml{}
3218
3219 module.AddProperties(&module.properties)
3220
3221 android.InitApexModule(module)
3222 android.InitAndroidArchModule(module, android.DeviceSupported, android.MultilibCommon)
3223
3224 return module
3225}
3226
Colin Crossaede88c2020-08-11 12:17:01 -07003227func (module *sdkLibraryXml) UniqueApexVariations() bool {
3228 // sdkLibraryXml needs a unique variation per APEX because the generated XML file contains the path to the
3229 // mounted APEX, which contains the name of the APEX.
3230 return true
3231}
3232
Jiyong Parke3833882020-02-17 17:28:10 +09003233// from android.PrebuiltEtcModule
Jooyung Han0703fd82020-08-26 22:11:53 +09003234func (module *sdkLibraryXml) BaseDir() string {
3235 return "etc"
3236}
3237
3238// from android.PrebuiltEtcModule
Jiyong Parke3833882020-02-17 17:28:10 +09003239func (module *sdkLibraryXml) SubDir() string {
3240 return "permissions"
3241}
3242
ThiƩbaud Weksteen00e8b312024-03-18 14:06:00 +11003243var _ etc.PrebuiltEtcModule = (*sdkLibraryXml)(nil)
3244
Jiyong Parke3833882020-02-17 17:28:10 +09003245// from android.ApexModule
3246func (module *sdkLibraryXml) AvailableFor(what string) bool {
3247 return true
3248}
3249
3250func (module *sdkLibraryXml) DepsMutator(ctx android.BottomUpMutatorContext) {
3251 // do nothing
3252}
3253
Jiyong Park45bf82e2020-12-15 22:29:02 +09003254var _ android.ApexModule = (*sdkLibraryXml)(nil)
3255
3256// Implements android.ApexModule
Dan Albertc8060532020-07-22 22:32:17 -07003257func (module *sdkLibraryXml) ShouldSupportSdkVersion(ctx android.BaseModuleContext,
3258 sdkVersion android.ApiLevel) error {
Jooyung Han749dc692020-04-15 11:03:39 +09003259 // sdkLibraryXml doesn't need to be checked separately because java_sdk_library is checked
3260 return nil
3261}
3262
Jiyong Parke3833882020-02-17 17:28:10 +09003263// File path to the runtime implementation library
Colin Cross56a83212020-09-15 18:30:11 -07003264func (module *sdkLibraryXml) implPath(ctx android.ModuleContext) string {
Jiyong Parke3833882020-02-17 17:28:10 +09003265 implName := proptools.String(module.properties.Lib_name)
Colin Crossff694a82023-12-13 15:54:49 -08003266 if apexInfo, _ := android.ModuleProvider(ctx, android.ApexInfoProvider); !apexInfo.IsForPlatform() {
Colin Crosse07f2312020-08-13 11:24:56 -07003267 // TODO(b/146468504): ApexVariationName() is only a soong module name, not apex name.
Jiyong Parke3833882020-02-17 17:28:10 +09003268 // In most cases, this works fine. But when apex_name is set or override_apex is used
3269 // this can be wrong.
Spandan Das33bbeb22024-06-18 23:28:25 +00003270 return fmt.Sprintf("/apex/%s/javalib/%s.jar", apexInfo.BaseApexName, implName)
Jiyong Parke3833882020-02-17 17:28:10 +09003271 }
3272 partition := "system"
3273 if module.SocSpecific() {
3274 partition = "vendor"
3275 } else if module.DeviceSpecific() {
3276 partition = "odm"
3277 } else if module.ProductSpecific() {
3278 partition = "product"
3279 } else if module.SystemExtSpecific() {
3280 partition = "system_ext"
3281 }
3282 return "/" + partition + "/framework/" + implName + ".jar"
3283}
3284
Pedro Loureiro9956e5e2021-09-07 17:21:59 +00003285func formattedOptionalSdkLevelAttribute(ctx android.ModuleContext, attrName string, value *string) string {
3286 if value == nil {
3287 return ""
3288 }
3289 apiLevel, err := android.ApiLevelFromUser(ctx, *value)
3290 if err != nil {
Pedro Loureiroba6682f2021-10-29 09:32:32 +00003291 // attributes in bp files have underscores but in the xml have dashes.
3292 ctx.PropertyErrorf(strings.ReplaceAll(attrName, "-", "_"), err.Error())
Pedro Loureiro9956e5e2021-09-07 17:21:59 +00003293 return ""
3294 }
Pedro Loureirob638c622021-12-22 15:28:05 +00003295 if apiLevel.IsCurrent() {
3296 // passing "current" would always mean a future release, never the current (or the current in
3297 // progress) which means some conditions would never be triggered.
3298 ctx.PropertyErrorf(strings.ReplaceAll(attrName, "-", "_"),
3299 `"current" is not an allowed value for this attribute`)
3300 return ""
3301 }
Pedro Loureiro48991222022-06-17 20:01:21 +00003302 // "safeValue" is safe because it translates finalized codenames to a string
3303 // with their SDK int.
3304 safeValue := apiLevel.String()
3305 return formattedOptionalAttribute(attrName, &safeValue)
Pedro Loureiro9956e5e2021-09-07 17:21:59 +00003306}
3307
3308// formats an attribute for the xml permissions file if the value is not null
3309// returns empty string otherwise
3310func formattedOptionalAttribute(attrName string, value *string) string {
3311 if value == nil {
3312 return ""
3313 }
Paul Duffin1816cde2024-04-10 10:58:21 +01003314 return fmt.Sprintf(" %s=\"%s\"\n", attrName, *value)
Pedro Loureiro9956e5e2021-09-07 17:21:59 +00003315}
3316
Jamie Garsidee570ace2023-11-27 12:07:36 +00003317func formattedDependenciesAttribute(dependencies []string) string {
3318 if dependencies == nil {
3319 return ""
3320 }
Paul Duffin1816cde2024-04-10 10:58:21 +01003321 return fmt.Sprintf(" dependency=\"%s\"\n", strings.Join(dependencies, ":"))
Jamie Garsidee570ace2023-11-27 12:07:36 +00003322}
3323
Pedro Loureiro9956e5e2021-09-07 17:21:59 +00003324func (module *sdkLibraryXml) permissionsContents(ctx android.ModuleContext) string {
3325 libName := proptools.String(module.properties.Lib_name)
3326 libNameAttr := formattedOptionalAttribute("name", &libName)
3327 filePath := module.implPath(ctx)
3328 filePathAttr := formattedOptionalAttribute("file", &filePath)
Pedro Loureiroba6682f2021-10-29 09:32:32 +00003329 implicitFromAttr := formattedOptionalSdkLevelAttribute(ctx, "on-bootclasspath-since", module.properties.On_bootclasspath_since)
3330 implicitUntilAttr := formattedOptionalSdkLevelAttribute(ctx, "on-bootclasspath-before", module.properties.On_bootclasspath_before)
3331 minSdkAttr := formattedOptionalSdkLevelAttribute(ctx, "min-device-sdk", module.properties.Min_device_sdk)
3332 maxSdkAttr := formattedOptionalSdkLevelAttribute(ctx, "max-device-sdk", module.properties.Max_device_sdk)
Jamie Garsidee570ace2023-11-27 12:07:36 +00003333 dependenciesAttr := formattedDependenciesAttribute(module.properties.Uses_libs_dependencies)
Pedro Loureiro196d3e62021-12-22 19:53:01 +00003334 // <library> is understood in all android versions whereas <apex-library> is only understood from API T (and ignored before that).
3335 // 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 +00003336 var libraryTag string
3337 if module.properties.Min_device_sdk != nil {
Paul Duffin1816cde2024-04-10 10:58:21 +01003338 libraryTag = " <apex-library\n"
Pedro Loureiroc3621422021-09-28 15:40:23 +00003339 } else {
Paul Duffin1816cde2024-04-10 10:58:21 +01003340 libraryTag = " <library\n"
Pedro Loureiroc3621422021-09-28 15:40:23 +00003341 }
Pedro Loureiro9956e5e2021-09-07 17:21:59 +00003342
3343 return strings.Join([]string{
Paul Duffin1816cde2024-04-10 10:58:21 +01003344 "<?xml version=\"1.0\" encoding=\"utf-8\"?>\n",
3345 "<!-- Copyright (C) 2018 The Android Open Source Project\n",
3346 "\n",
3347 " Licensed under the Apache License, Version 2.0 (the \"License\");\n",
3348 " you may not use this file except in compliance with the License.\n",
3349 " You may obtain a copy of the License at\n",
3350 "\n",
3351 " http://www.apache.org/licenses/LICENSE-2.0\n",
3352 "\n",
3353 " Unless required by applicable law or agreed to in writing, software\n",
3354 " distributed under the License is distributed on an \"AS IS\" BASIS,\n",
3355 " WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\n",
3356 " See the License for the specific language governing permissions and\n",
3357 " limitations under the License.\n",
3358 "-->\n",
3359 "<permissions>\n",
Pedro Loureiroc3621422021-09-28 15:40:23 +00003360 libraryTag,
Pedro Loureiro9956e5e2021-09-07 17:21:59 +00003361 libNameAttr,
3362 filePathAttr,
3363 implicitFromAttr,
3364 implicitUntilAttr,
3365 minSdkAttr,
3366 maxSdkAttr,
Jamie Garsidee570ace2023-11-27 12:07:36 +00003367 dependenciesAttr,
Paul Duffin1816cde2024-04-10 10:58:21 +01003368 " />\n",
3369 "</permissions>\n",
3370 }, "")
Pedro Loureiro9956e5e2021-09-07 17:21:59 +00003371}
3372
Jiyong Parke3833882020-02-17 17:28:10 +09003373func (module *sdkLibraryXml) GenerateAndroidBuildActions(ctx android.ModuleContext) {
Colin Crossff694a82023-12-13 15:54:49 -08003374 apexInfo, _ := android.ModuleProvider(ctx, android.ApexInfoProvider)
3375 module.hideApexVariantFromMake = !apexInfo.IsForPlatform()
Colin Cross56a83212020-09-15 18:30:11 -07003376
Jiyong Parke3833882020-02-17 17:28:10 +09003377 libName := proptools.String(module.properties.Lib_name)
Pedro Loureiroc3621422021-09-28 15:40:23 +00003378 module.selfValidate(ctx)
Pedro Loureiro9956e5e2021-09-07 17:21:59 +00003379 xmlContent := module.permissionsContents(ctx)
Jiyong Parke3833882020-02-17 17:28:10 +09003380
3381 module.outputFilePath = android.PathForModuleOut(ctx, libName+".xml").OutputPath
Paul Duffin1816cde2024-04-10 10:58:21 +01003382 android.WriteFileRuleVerbatim(ctx, module.outputFilePath, xmlContent)
Jiyong Parke3833882020-02-17 17:28:10 +09003383
3384 module.installDirPath = android.PathForModuleInstall(ctx, "etc", module.SubDir())
Jeongik Cha00e09912024-04-23 05:07:13 +09003385 ctx.PackageFile(module.installDirPath, libName+".xml", module.outputFilePath)
mrziwange2346b82024-06-10 15:09:45 -07003386
3387 ctx.SetOutputFiles(android.OutputPaths{module.outputFilePath}.Paths(), "")
Jiyong Parke3833882020-02-17 17:28:10 +09003388}
3389
3390func (module *sdkLibraryXml) AndroidMkEntries() []android.AndroidMkEntries {
Colin Cross56a83212020-09-15 18:30:11 -07003391 if module.hideApexVariantFromMake {
satayev8f088b02021-12-06 11:40:46 +00003392 return []android.AndroidMkEntries{{
Jiyong Parke3833882020-02-17 17:28:10 +09003393 Disabled: true,
3394 }}
3395 }
3396
satayev8f088b02021-12-06 11:40:46 +00003397 return []android.AndroidMkEntries{{
Jiyong Parke3833882020-02-17 17:28:10 +09003398 Class: "ETC",
3399 OutputFile: android.OptionalPathForPath(module.outputFilePath),
3400 ExtraEntries: []android.AndroidMkExtraEntriesFunc{
Colin Crossaa255532020-07-03 13:18:24 -07003401 func(ctx android.AndroidMkExtraEntriesContext, entries *android.AndroidMkEntries) {
Jiyong Parke3833882020-02-17 17:28:10 +09003402 entries.SetString("LOCAL_MODULE_TAGS", "optional")
Colin Crossc68db4b2021-11-11 18:59:15 -08003403 entries.SetString("LOCAL_MODULE_PATH", module.installDirPath.String())
Jiyong Parke3833882020-02-17 17:28:10 +09003404 entries.SetString("LOCAL_INSTALLED_MODULE_STEM", module.outputFilePath.Base())
3405 },
3406 },
3407 }}
3408}
Paul Duffindd46f712020-02-10 13:37:10 +00003409
Pedro Loureiroc3621422021-09-28 15:40:23 +00003410func (module *sdkLibraryXml) selfValidate(ctx android.ModuleContext) {
3411 module.validateAtLeastTAttributes(ctx)
3412 module.validateMinAndMaxDeviceSdk(ctx)
3413 module.validateMinMaxDeviceSdkAndModuleMinSdk(ctx)
3414 module.validateOnBootclasspathBeforeRequirements(ctx)
3415}
3416
3417func (module *sdkLibraryXml) validateAtLeastTAttributes(ctx android.ModuleContext) {
3418 t := android.ApiLevelOrPanic(ctx, "Tiramisu")
3419 module.attrAtLeastT(ctx, t, module.properties.Min_device_sdk, "min_device_sdk")
3420 module.attrAtLeastT(ctx, t, module.properties.Max_device_sdk, "max_device_sdk")
3421 module.attrAtLeastT(ctx, t, module.properties.On_bootclasspath_before, "on_bootclasspath_before")
3422 module.attrAtLeastT(ctx, t, module.properties.On_bootclasspath_since, "on_bootclasspath_since")
3423}
3424
3425func (module *sdkLibraryXml) attrAtLeastT(ctx android.ModuleContext, t android.ApiLevel, attr *string, attrName string) {
3426 if attr != nil {
3427 if level, err := android.ApiLevelFromUser(ctx, *attr); err == nil {
3428 // we will inform the user of invalid inputs when we try to write the
3429 // permissions xml file so we don't need to do it here
3430 if t.GreaterThan(level) {
3431 ctx.PropertyErrorf(attrName, "Attribute value needs to be at least T")
3432 }
3433 }
3434 }
3435}
3436
3437func (module *sdkLibraryXml) validateMinAndMaxDeviceSdk(ctx android.ModuleContext) {
3438 if module.properties.Min_device_sdk != nil && module.properties.Max_device_sdk != nil {
3439 min, minErr := android.ApiLevelFromUser(ctx, *module.properties.Min_device_sdk)
3440 max, maxErr := android.ApiLevelFromUser(ctx, *module.properties.Max_device_sdk)
3441 if minErr == nil && maxErr == nil {
3442 // we will inform the user of invalid inputs when we try to write the
3443 // permissions xml file so we don't need to do it here
3444 if min.GreaterThan(max) {
3445 ctx.ModuleErrorf("min_device_sdk can't be greater than max_device_sdk")
3446 }
3447 }
3448 }
3449}
3450
3451func (module *sdkLibraryXml) validateMinMaxDeviceSdkAndModuleMinSdk(ctx android.ModuleContext) {
3452 moduleMinApi := android.ApiLevelOrPanic(ctx, *module.properties.Sdk_library_min_api_level)
3453 if module.properties.Min_device_sdk != nil {
3454 api, err := android.ApiLevelFromUser(ctx, *module.properties.Min_device_sdk)
3455 if err == nil {
3456 if moduleMinApi.GreaterThan(api) {
3457 ctx.PropertyErrorf("min_device_sdk", "Can't be less than module's min sdk (%s)", moduleMinApi)
3458 }
3459 }
3460 }
3461 if module.properties.Max_device_sdk != nil {
3462 api, err := android.ApiLevelFromUser(ctx, *module.properties.Max_device_sdk)
3463 if err == nil {
3464 if moduleMinApi.GreaterThan(api) {
3465 ctx.PropertyErrorf("max_device_sdk", "Can't be less than module's min sdk (%s)", moduleMinApi)
3466 }
3467 }
3468 }
3469}
3470
3471func (module *sdkLibraryXml) validateOnBootclasspathBeforeRequirements(ctx android.ModuleContext) {
3472 moduleMinApi := android.ApiLevelOrPanic(ctx, *module.properties.Sdk_library_min_api_level)
3473 if module.properties.On_bootclasspath_before != nil {
3474 t := android.ApiLevelOrPanic(ctx, "Tiramisu")
3475 // if we use the attribute, then we need to do this validation
3476 if moduleMinApi.LessThan(t) {
3477 // if minAPi is < T, then we need to have min_device_sdk (which only accepts T+)
3478 if module.properties.Min_device_sdk == nil {
3479 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")
3480 }
3481 }
3482 }
3483}
3484
Paul Duffindd46f712020-02-10 13:37:10 +00003485type sdkLibrarySdkMemberType struct {
3486 android.SdkMemberTypeBase
3487}
3488
Paul Duffin296701e2021-07-14 10:29:36 +01003489func (s *sdkLibrarySdkMemberType) AddDependencies(ctx android.SdkDependencyContext, dependencyTag blueprint.DependencyTag, names []string) {
3490 ctx.AddVariationDependencies(nil, dependencyTag, names...)
Paul Duffindd46f712020-02-10 13:37:10 +00003491}
3492
3493func (s *sdkLibrarySdkMemberType) IsInstance(module android.Module) bool {
3494 _, ok := module.(*SdkLibrary)
3495 return ok
3496}
3497
3498func (s *sdkLibrarySdkMemberType) AddPrebuiltModule(ctx android.SdkMemberContext, member android.SdkMember) android.BpModule {
3499 return ctx.SnapshotBuilder().AddPrebuiltModule(member, "java_sdk_library_import")
3500}
3501
3502func (s *sdkLibrarySdkMemberType) CreateVariantPropertiesStruct() android.SdkMemberProperties {
3503 return &sdkLibrarySdkMemberProperties{}
3504}
3505
Paul Duffin976b0e52021-04-27 23:20:26 +01003506var javaSdkLibrarySdkMemberType = &sdkLibrarySdkMemberType{
3507 android.SdkMemberTypeBase{
3508 PropertyName: "java_sdk_libs",
3509 SupportsSdk: true,
3510 },
3511}
3512
Paul Duffindd46f712020-02-10 13:37:10 +00003513type sdkLibrarySdkMemberProperties struct {
3514 android.SdkMemberPropertiesBase
3515
Paul Duffine8409952022-09-22 16:24:46 +01003516 // Stem name for files in the sdk snapshot.
3517 //
3518 // This is used to construct the path names of various sdk library files in the sdk snapshot to
3519 // make sure that they match the finalized versions of those files in prebuilts/sdk.
3520 //
3521 // This property is marked as keep so that it will be kept in all instances of this struct, will
3522 // not be cleared but will be copied to common structs. That is needed because this field is used
3523 // to construct many file names for other parts of this struct and so it needs to be present in
3524 // all structs. If it was not marked as keep then it would be cleared in some structs and so would
3525 // be unavailable for generating file names if there were other properties that were still set.
3526 Stem string `sdk:"keep"`
3527
Paul Duffindd46f712020-02-10 13:37:10 +00003528 // Scope to per scope properties.
Paul Duffin106a3a42022-01-27 16:39:06 +00003529 Scopes map[*apiScope]*scopeProperties
Paul Duffindd46f712020-02-10 13:37:10 +00003530
Paul Duffin3d1248c2020-04-09 00:10:17 +01003531 // The Java stubs source files.
3532 Stub_srcs []string
Paul Duffinf7a64332020-05-13 16:54:55 +01003533
3534 // The naming scheme.
3535 Naming_scheme *string
Paul Duffind7eb1c22020-05-26 20:57:10 +01003536
3537 // True if the java_sdk_library_import is for a shared library, false
3538 // otherwise.
3539 Shared_library *bool
Paul Duffina2ae7e02020-09-11 11:55:00 +01003540
Paul Duffin1267d872021-04-16 17:21:36 +01003541 // True if the stub imports should produce dex jars.
3542 Compile_dex *bool
3543
Paul Duffina2ae7e02020-09-11 11:55:00 +01003544 // The paths to the doctag files to add to the prebuilt.
3545 Doctag_paths android.Paths
Paul Duffin869de142021-07-15 14:14:41 +01003546
3547 Permitted_packages []string
Pedro Loureiro9956e5e2021-09-07 17:21:59 +00003548
3549 // Signals that this shared library is part of the bootclasspath starting
3550 // on the version indicated in this attribute.
3551 //
3552 // This will make platforms at this level and above to ignore
3553 // <uses-library> tags with this library name because the library is already
3554 // available
3555 On_bootclasspath_since *string
3556
3557 // Signals that this shared library was part of the bootclasspath before
3558 // (but not including) the version indicated in this attribute.
3559 //
3560 // The system will automatically add a <uses-library> tag with this library to
3561 // apps that target any SDK less than the version indicated in this attribute.
3562 On_bootclasspath_before *string
3563
3564 // Indicates that PackageManager should ignore this shared library if the
3565 // platform is below the version indicated in this attribute.
3566 //
3567 // This means that the device won't recognise this library as installed.
3568 Min_device_sdk *string
3569
3570 // Indicates that PackageManager should ignore this shared library if the
3571 // platform is above the version indicated in this attribute.
3572 //
3573 // This means that the device won't recognise this library as installed.
3574 Max_device_sdk *string
Jiakai Zhang9c4dc192023-02-09 00:09:24 +08003575
3576 DexPreoptProfileGuided *bool `supported_build_releases:"UpsideDownCake+"`
Paul Duffindd46f712020-02-10 13:37:10 +00003577}
3578
3579type scopeProperties struct {
Paul Duffin1fd005d2020-04-09 01:08:11 +01003580 Jars android.Paths
3581 StubsSrcJar android.Path
3582 CurrentApiFile android.Path
3583 RemovedApiFile android.Path
Paul Duffine7babdb2022-02-10 13:06:54 +00003584 AnnotationsZip android.Path `supported_build_releases:"Tiramisu+"`
Paul Duffin1fd005d2020-04-09 01:08:11 +01003585 SdkVersion string
Paul Duffindd46f712020-02-10 13:37:10 +00003586}
3587
3588func (s *sdkLibrarySdkMemberProperties) PopulateFromVariant(ctx android.SdkMemberContext, variant android.Module) {
3589 sdk := variant.(*SdkLibrary)
3590
Paul Duffine8409952022-09-22 16:24:46 +01003591 // Copy the stem name for files in the sdk snapshot.
3592 s.Stem = sdk.distStem()
3593
Paul Duffin106a3a42022-01-27 16:39:06 +00003594 s.Scopes = make(map[*apiScope]*scopeProperties)
Jihoon Kang98aa8fa2024-06-07 11:06:57 +00003595 for _, apiScope := range AllApiScopes {
Paul Duffin803a9562020-05-20 11:52:25 +01003596 paths := sdk.findScopePaths(apiScope)
3597 if paths == nil {
3598 continue
3599 }
3600
Paul Duffindd46f712020-02-10 13:37:10 +00003601 jars := paths.stubsImplPath
3602 if len(jars) > 0 {
3603 properties := scopeProperties{}
3604 properties.Jars = jars
Paul Duffin780c5f42020-05-12 15:52:55 +01003605 properties.SdkVersion = sdk.sdkVersionForStubsLibrary(ctx.SdkModuleContext(), apiScope)
Paul Duffin0f8faff2020-05-20 16:18:00 +01003606 properties.StubsSrcJar = paths.stubsSrcJar.Path()
Paul Duffin10269f12020-06-19 18:39:55 +01003607 if paths.currentApiFilePath.Valid() {
3608 properties.CurrentApiFile = paths.currentApiFilePath.Path()
3609 }
3610 if paths.removedApiFilePath.Valid() {
3611 properties.RemovedApiFile = paths.removedApiFilePath.Path()
3612 }
Anton Hanssond78eb762021-09-21 15:25:12 +01003613 // The annotations zip is only available for modules that set annotations_enabled: true.
3614 if paths.annotationsZip.Valid() {
3615 properties.AnnotationsZip = paths.annotationsZip.Path()
3616 }
Paul Duffin106a3a42022-01-27 16:39:06 +00003617 s.Scopes[apiScope] = &properties
Paul Duffindd46f712020-02-10 13:37:10 +00003618 }
3619 }
3620
Paul Duffindfa131e2020-05-15 20:37:11 +01003621 s.Naming_scheme = sdk.commonSdkLibraryProperties.Naming_scheme
Paul Duffind7eb1c22020-05-26 20:57:10 +01003622 s.Shared_library = proptools.BoolPtr(sdk.sharedLibrary())
Paul Duffin1267d872021-04-16 17:21:36 +01003623 s.Compile_dex = sdk.dexProperties.Compile_dex
Paul Duffina2ae7e02020-09-11 11:55:00 +01003624 s.Doctag_paths = sdk.doctagPaths
Paul Duffin869de142021-07-15 14:14:41 +01003625 s.Permitted_packages = sdk.PermittedPackagesForUpdatableBootJars()
Pedro Loureiro9956e5e2021-09-07 17:21:59 +00003626 s.On_bootclasspath_since = sdk.commonSdkLibraryProperties.On_bootclasspath_since
3627 s.On_bootclasspath_before = sdk.commonSdkLibraryProperties.On_bootclasspath_before
3628 s.Min_device_sdk = sdk.commonSdkLibraryProperties.Min_device_sdk
3629 s.Max_device_sdk = sdk.commonSdkLibraryProperties.Max_device_sdk
Jiakai Zhang9c4dc192023-02-09 00:09:24 +08003630
Jihoon Kanga3a05462024-04-05 00:36:44 +00003631 implLibrary := sdk.getImplLibraryModule()
3632 if implLibrary != nil && implLibrary.dexpreopter.dexpreoptProperties.Dex_preopt_result.Profile_guided {
Jiakai Zhang9c4dc192023-02-09 00:09:24 +08003633 s.DexPreoptProfileGuided = proptools.BoolPtr(true)
3634 }
Paul Duffindd46f712020-02-10 13:37:10 +00003635}
3636
3637func (s *sdkLibrarySdkMemberProperties) AddToPropertySet(ctx android.SdkMemberContext, propertySet android.BpPropertySet) {
Paul Duffinf7a64332020-05-13 16:54:55 +01003638 if s.Naming_scheme != nil {
3639 propertySet.AddProperty("naming_scheme", proptools.String(s.Naming_scheme))
3640 }
Paul Duffind7eb1c22020-05-26 20:57:10 +01003641 if s.Shared_library != nil {
3642 propertySet.AddProperty("shared_library", *s.Shared_library)
3643 }
Paul Duffin1267d872021-04-16 17:21:36 +01003644 if s.Compile_dex != nil {
3645 propertySet.AddProperty("compile_dex", *s.Compile_dex)
3646 }
Paul Duffin869de142021-07-15 14:14:41 +01003647 if len(s.Permitted_packages) > 0 {
3648 propertySet.AddProperty("permitted_packages", s.Permitted_packages)
3649 }
Jiakai Zhang9c4dc192023-02-09 00:09:24 +08003650 dexPreoptSet := propertySet.AddPropertySet("dex_preopt")
3651 if s.DexPreoptProfileGuided != nil {
3652 dexPreoptSet.AddProperty("profile_guided", proptools.Bool(s.DexPreoptProfileGuided))
3653 }
Paul Duffinf7a64332020-05-13 16:54:55 +01003654
Paul Duffine8409952022-09-22 16:24:46 +01003655 stem := s.Stem
3656
Jihoon Kang98aa8fa2024-06-07 11:06:57 +00003657 for _, apiScope := range AllApiScopes {
Paul Duffindd46f712020-02-10 13:37:10 +00003658 if properties, ok := s.Scopes[apiScope]; ok {
Paul Duffin6b836ba2020-05-13 19:19:49 +01003659 scopeSet := propertySet.AddPropertySet(apiScope.propertyName)
Paul Duffindd46f712020-02-10 13:37:10 +00003660
Paul Duffin958806b2022-05-16 13:10:47 +00003661 scopeDir := apiScope.snapshotRelativeDir()
Paul Duffin3d1248c2020-04-09 00:10:17 +01003662
Paul Duffindd46f712020-02-10 13:37:10 +00003663 var jars []string
3664 for _, p := range properties.Jars {
Paul Duffine8409952022-09-22 16:24:46 +01003665 dest := filepath.Join(scopeDir, stem+"-stubs.jar")
Paul Duffindd46f712020-02-10 13:37:10 +00003666 ctx.SnapshotBuilder().CopyToSnapshot(p, dest)
3667 jars = append(jars, dest)
3668 }
3669 scopeSet.AddProperty("jars", jars)
3670
Paul Duffin22628d52021-05-12 23:13:22 +01003671 if ctx.SdkModuleContext().Config().IsEnvTrue("SOONG_SDK_SNAPSHOT_USE_SRCJAR") {
3672 // Copy the stubs source jar into the snapshot zip as is.
Paul Duffine8409952022-09-22 16:24:46 +01003673 srcJarSnapshotPath := filepath.Join(scopeDir, stem+".srcjar")
Paul Duffin22628d52021-05-12 23:13:22 +01003674 ctx.SnapshotBuilder().CopyToSnapshot(properties.StubsSrcJar, srcJarSnapshotPath)
3675 scopeSet.AddProperty("stub_srcs", []string{srcJarSnapshotPath})
3676 } else {
3677 // Merge the stubs source jar into the snapshot zip so that when it is unpacked
3678 // the source files are also unpacked.
Paul Duffine8409952022-09-22 16:24:46 +01003679 snapshotRelativeDir := filepath.Join(scopeDir, stem+"_stub_sources")
Paul Duffin22628d52021-05-12 23:13:22 +01003680 ctx.SnapshotBuilder().UnzipToSnapshot(properties.StubsSrcJar, snapshotRelativeDir)
3681 scopeSet.AddProperty("stub_srcs", []string{snapshotRelativeDir})
3682 }
Paul Duffin3d1248c2020-04-09 00:10:17 +01003683
Paul Duffin1fd005d2020-04-09 01:08:11 +01003684 if properties.CurrentApiFile != nil {
Paul Duffine8409952022-09-22 16:24:46 +01003685 currentApiSnapshotPath := apiScope.snapshotRelativeCurrentApiTxtPath(stem)
Paul Duffin1fd005d2020-04-09 01:08:11 +01003686 ctx.SnapshotBuilder().CopyToSnapshot(properties.CurrentApiFile, currentApiSnapshotPath)
3687 scopeSet.AddProperty("current_api", currentApiSnapshotPath)
3688 }
3689
3690 if properties.RemovedApiFile != nil {
Paul Duffine8409952022-09-22 16:24:46 +01003691 removedApiSnapshotPath := apiScope.snapshotRelativeRemovedApiTxtPath(stem)
Paul Duffin3dbf9fd2020-06-02 13:00:02 +01003692 ctx.SnapshotBuilder().CopyToSnapshot(properties.RemovedApiFile, removedApiSnapshotPath)
Paul Duffin1fd005d2020-04-09 01:08:11 +01003693 scopeSet.AddProperty("removed_api", removedApiSnapshotPath)
3694 }
3695
Anton Hanssond78eb762021-09-21 15:25:12 +01003696 if properties.AnnotationsZip != nil {
Paul Duffine8409952022-09-22 16:24:46 +01003697 annotationsSnapshotPath := filepath.Join(scopeDir, stem+"_annotations.zip")
Anton Hanssond78eb762021-09-21 15:25:12 +01003698 ctx.SnapshotBuilder().CopyToSnapshot(properties.AnnotationsZip, annotationsSnapshotPath)
3699 scopeSet.AddProperty("annotations", annotationsSnapshotPath)
3700 }
3701
Paul Duffindd46f712020-02-10 13:37:10 +00003702 if properties.SdkVersion != "" {
3703 scopeSet.AddProperty("sdk_version", properties.SdkVersion)
3704 }
3705 }
3706 }
3707
Paul Duffina2ae7e02020-09-11 11:55:00 +01003708 if len(s.Doctag_paths) > 0 {
3709 dests := []string{}
3710 for _, p := range s.Doctag_paths {
3711 dest := filepath.Join("doctags", p.Rel())
3712 ctx.SnapshotBuilder().CopyToSnapshot(p, dest)
3713 dests = append(dests, dest)
3714 }
3715 propertySet.AddProperty("doctag_files", dests)
3716 }
Paul Duffindd46f712020-02-10 13:37:10 +00003717}