blob: c5f7a1ce02a652d3e2b171e4fb9702eec078aadd [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 }
Liana Kazanovaa574cd22024-08-05 19:45:03 +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 },
Jihoon Kang0c705a42023-08-02 06:44:57 +0000446 }
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
Paul Duffin749f98f2019-12-30 17:23:46 +0000541 // the relative path to the directory containing the api specification files.
542 // Defaults to "api".
543 Api_dir *string
544
Paul Duffindfa131e2020-05-15 20:37:11 +0100545 // Determines whether a runtime implementation library is built; defaults to false.
546 //
547 // 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 +0200548 // it is as if shared_library: false, was set.
Paul Duffin43db9be2019-12-30 17:35:49 +0000549 Api_only *bool
550
Paul Duffin11512472019-02-11 15:55:17 +0000551 // local files that are used within user customized droiddoc options.
552 Droiddoc_option_files []string
553
Spandan Das93e95992021-07-29 18:26:39 +0000554 // additional droiddoc options.
Paul Duffin11512472019-02-11 15:55:17 +0000555 // Available variables for substitution:
556 //
557 // $(location <label>): the path to the droiddoc_option_files with name <label>
Sundong Ahndd567f92018-07-31 17:19:11 +0900558 Droiddoc_options []string
559
Paul Duffine22c2ab2020-05-20 19:35:27 +0100560 // is set to true, Metalava will allow framework SDK to contain annotations.
561 Annotations_enabled *bool
562
Sundong Ahn054b19a2018-10-19 13:46:09 +0900563 // a list of top-level directories containing files to merge qualifier annotations
564 // (i.e. those intended to be included in the stubs written) from.
565 Merge_annotations_dirs []string
566
567 // a list of top-level directories containing Java stub files to merge show/hide annotations from.
568 Merge_inclusion_annotations_dirs []string
569
Paul Duffin4f5c1ef2020-11-19 14:53:43 +0000570 // If set to true then don't create dist rules.
571 No_dist *bool
Sundong Ahn80a87b32019-05-13 15:02:50 +0900572
Paul Duffin31310252020-11-20 21:26:20 +0000573 // The stem for the artifacts that are copied to the dist, if not specified
574 // then defaults to the base module name.
575 //
576 // For each scope the following artifacts are copied to the apistubs/<scope>
577 // directory in the dist.
578 // * stubs impl jar -> <dist-stem>.jar
579 // * API specification file -> api/<dist-stem>.txt
580 // * Removed API specification file -> api/<dist-stem>-removed.txt
581 //
582 // Also used to construct the name of the filegroup (created by prebuilt_apis)
583 // that references the latest released API and remove API specification files.
584 // * API specification filegroup -> <dist-stem>.api.<scope>.latest
585 // * Removed API specification filegroup -> <dist-stem>-removed.api.<scope>.latest
Jaewoong Jung1a97ee02021-03-09 13:25:02 -0800586 // * API incompatibilities baseline filegroup -> <dist-stem>-incompatibilities.api.<scope>.latest
Paul Duffin31310252020-11-20 21:26:20 +0000587 Dist_stem *string
588
Colin Cross986b69a2021-06-01 13:13:40 -0700589 // The subdirectory for the artifacts that are copied to the dist directory. If not specified
Colin Cross3dd66252021-06-01 14:05:09 -0700590 // then defaults to "unknown". Should be set to "android" for anything that should be published
Colin Cross986b69a2021-06-01 13:13:40 -0700591 // in the public Android SDK.
592 Dist_group *string
593
Anton Hanssondff2c782020-12-21 17:10:01 +0000594 // A compatibility mode that allows historical API-tracking files to not exist.
595 // Do not use.
596 Unsafe_ignore_missing_latest_api bool
597
Paul Duffin3375e352020-04-28 10:44:03 +0100598 // indicates whether system and test apis should be generated.
599 Generate_system_and_test_apis bool `blueprint:"mutated"`
600
601 // The properties specific to the public api scope
602 //
603 // Unless explicitly specified by using public.enabled the public api scope is
604 // enabled by default in both legacy and non-legacy mode.
605 Public ApiScopeProperties
606
607 // The properties specific to the system api scope
608 //
609 // In legacy mode the system api scope is enabled by default when sdk_version
610 // is set to something other than "none".
611 //
612 // In non-legacy mode the system api scope is disabled by default.
613 System ApiScopeProperties
614
615 // The properties specific to the test api scope
616 //
617 // In legacy mode the test api scope is enabled by default when sdk_version
618 // is set to something other than "none".
619 //
620 // In non-legacy mode the test api scope is disabled by default.
621 Test ApiScopeProperties
Paul Duffin37e0b772019-12-30 17:20:10 +0000622
Paul Duffin0c5bae52020-06-02 13:00:08 +0100623 // The properties specific to the module-lib api scope
Paul Duffin8f265b92020-04-28 14:13:56 +0100624 //
Zi Wangb2179e32023-01-31 15:53:30 -0800625 // Unless explicitly specified by using module_lib.enabled the module_lib api
626 // scope is disabled by default.
Paul Duffin8f265b92020-04-28 14:13:56 +0100627 Module_lib ApiScopeProperties
628
Paul Duffin0c5bae52020-06-02 13:00:08 +0100629 // The properties specific to the system-server api scope
630 //
Zi Wangb2179e32023-01-31 15:53:30 -0800631 // Unless explicitly specified by using system_server.enabled the
632 // system_server api scope is disabled by default.
Paul Duffin0c5bae52020-06-02 13:00:08 +0100633 System_server ApiScopeProperties
634
Jiyong Park932cdfe2020-05-28 00:19:53 +0900635 // Determines if the stubs are preferred over the implementation library
636 // for linking, even when the client doesn't specify sdk_version. When this
637 // is set to true, such clients are provided with the widest API surface that
638 // this lib provides. Note however that this option doesn't affect the clients
639 // that are in the same APEX as this library. In that case, the clients are
640 // always linked with the implementation library. Default is false.
641 Default_to_stubs *bool
642
Paul Duffin160fe412020-05-10 19:32:20 +0100643 // Properties related to api linting.
644 Api_lint struct {
645 // Enable api linting.
646 Enabled *bool
Anton Hanssonfd1c0d22023-11-02 15:18:09 +0000647
648 // If API lint is enabled, this flag controls whether a set of legitimate lint errors
649 // are turned off. The default is true.
650 Legacy_errors_allowed *bool
Paul Duffin160fe412020-05-10 19:32:20 +0100651 }
652
Liana Kazanovaa574cd22024-08-05 19:45:03 +0000653 // Determines if the module contributes to any api surfaces.
654 // This property should be set to true only if the module is listed under
655 // frameworks-base-api.bootclasspath in frameworks/base/api/Android.bp.
656 // Otherwise, this property should be set to false.
657 // Defaults to false.
658 Contribute_to_android_api *bool
659
Jihoon Kang6592e872023-12-19 01:13:16 +0000660 // a list of aconfig_declarations module names that the stubs generated in this module
661 // depend on.
662 Aconfig_declarations []string
663
Jihoon Kang48e2ac92024-07-29 21:18:46 +0000664 // Determines if the module generates the stubs from the api signature files
665 // instead of the source Java files. Defaults to true.
666 Build_from_text_stub *bool
667
Jiyong Parkc678ad32018-04-10 13:07:10 +0900668 // TODO: determines whether to create HTML doc or not
Paul Duffine8409952022-09-22 16:24:46 +0100669 // Html_doc *bool
Jiyong Parkc678ad32018-04-10 13:07:10 +0900670}
671
Paul Duffin0f8faff2020-05-20 16:18:00 +0100672// Paths to outputs from java_sdk_library and java_sdk_library_import.
673//
674// Fields that are android.Paths are always set (during GenerateAndroidBuildActions).
675// OptionalPaths are always set by java_sdk_library but may not be set by
676// java_sdk_library_import as not all instances provide that information.
Paul Duffind1b3a922020-01-22 11:57:20 +0000677type scopePaths struct {
Paul Duffin0f8faff2020-05-20 16:18:00 +0100678 // The path (represented as Paths for convenience when returning) to the stubs header jar.
679 //
680 // That is the jar that is created by turbine.
681 stubsHeaderPath android.Paths
682
683 // The path (represented as Paths for convenience when returning) to the stubs implementation jar.
684 //
685 // This is not the implementation jar, it still only contains stubs.
686 stubsImplPath android.Paths
687
Paul Duffin1267d872021-04-16 17:21:36 +0100688 // The dex jar for the stubs.
689 //
690 // This is not the implementation jar, it still only contains stubs.
Martin Stjernholm8be1e6d2021-09-15 03:34:04 +0100691 stubsDexJarPath OptionalDexJarPath
Paul Duffin1267d872021-04-16 17:21:36 +0100692
Jihoon Kangbd093452023-12-26 19:08:01 +0000693 // The exportable dex jar for the stubs.
694 // This is not the implementation jar, it still only contains stubs.
695 // Includes unflagged apis and flagged apis enabled by release configurations.
696 exportableStubsDexJarPath OptionalDexJarPath
697
Paul Duffin0f8faff2020-05-20 16:18:00 +0100698 // The API specification file, e.g. system_current.txt.
699 currentApiFilePath android.OptionalPath
700
701 // The specification of API elements removed since the last release.
702 removedApiFilePath android.OptionalPath
703
704 // The stubs source jar.
705 stubsSrcJar android.OptionalPath
Anton Hanssond78eb762021-09-21 15:25:12 +0100706
707 // Extracted annotations.
708 annotationsZip android.OptionalPath
Paul Duffin958806b2022-05-16 13:10:47 +0000709
710 // The path to the latest API file.
Jihoon Kang5623e542024-01-31 23:27:26 +0000711 latestApiPaths android.Paths
Paul Duffin958806b2022-05-16 13:10:47 +0000712
713 // The path to the latest removed API file.
Jihoon Kang5623e542024-01-31 23:27:26 +0000714 latestRemovedApiPaths android.Paths
Paul Duffind1b3a922020-01-22 11:57:20 +0000715}
716
Colin Crossdcf71b22021-02-01 13:59:03 -0800717func (paths *scopePaths) extractStubsLibraryInfoFromDependency(ctx android.ModuleContext, dep android.Module) error {
Colin Cross313aa542023-12-13 13:47:44 -0800718 if lib, ok := android.OtherModuleProvider(ctx, dep, JavaInfoProvider); ok {
Colin Crossdcf71b22021-02-01 13:59:03 -0800719 paths.stubsHeaderPath = lib.HeaderJars
720 paths.stubsImplPath = lib.ImplementationJars
Paul Duffin1267d872021-04-16 17:21:36 +0100721
722 libDep := dep.(UsesLibraryDependency)
Spandan Das59a4a2b2024-01-09 21:35:56 +0000723 paths.stubsDexJarPath = libDep.DexJarBuildPath(ctx)
Jihoon Kangbd093452023-12-26 19:08:01 +0000724 paths.exportableStubsDexJarPath = libDep.DexJarBuildPath(ctx)
725 return nil
726 } else {
727 return fmt.Errorf("expected module that has JavaInfoProvider, e.g. java_library")
728 }
729}
730
731func (paths *scopePaths) extractEverythingStubsLibraryInfoFromDependency(ctx android.ModuleContext, dep android.Module) error {
732 if lib, ok := android.OtherModuleProvider(ctx, dep, JavaInfoProvider); ok {
733 paths.stubsHeaderPath = lib.HeaderJars
Jihoon Kangf55a5f72024-01-08 08:56:20 +0000734 if !ctx.Config().ReleaseHiddenApiExportableStubs() {
735 paths.stubsImplPath = lib.ImplementationJars
736 }
Jihoon Kangbd093452023-12-26 19:08:01 +0000737
738 libDep := dep.(UsesLibraryDependency)
739 paths.stubsDexJarPath = libDep.DexJarBuildPath(ctx)
740 return nil
741 } else {
742 return fmt.Errorf("expected module that has JavaInfoProvider, e.g. java_library")
743 }
744}
745
746func (paths *scopePaths) extractExportableStubsLibraryInfoFromDependency(ctx android.ModuleContext, dep android.Module) error {
Jihoon Kangf55a5f72024-01-08 08:56:20 +0000747 if lib, ok := android.OtherModuleProvider(ctx, dep, JavaInfoProvider); ok {
748 if ctx.Config().ReleaseHiddenApiExportableStubs() {
749 paths.stubsImplPath = lib.ImplementationJars
750 }
751
Jihoon Kangbd093452023-12-26 19:08:01 +0000752 libDep := dep.(UsesLibraryDependency)
753 paths.exportableStubsDexJarPath = libDep.DexJarBuildPath(ctx)
Paul Duffinc8782502020-04-29 20:45:27 +0100754 return nil
755 } else {
Colin Crossdcf71b22021-02-01 13:59:03 -0800756 return fmt.Errorf("expected module that has JavaInfoProvider, e.g. java_library")
Paul Duffinc8782502020-04-29 20:45:27 +0100757 }
758}
759
Jihoon Kangee113282024-01-23 00:16:41 +0000760func (paths *scopePaths) treatDepAsApiStubsProvider(dep android.Module, action func(provider ApiStubsProvider) error) error {
Paul Duffin0ff08bd2020-04-29 13:30:54 +0100761 if apiStubsProvider, ok := dep.(ApiStubsProvider); ok {
Jihoon Kangee113282024-01-23 00:16:41 +0000762 err := action(apiStubsProvider)
763 if err != nil {
764 return err
765 }
Jihoon Kangf55a5f72024-01-08 08:56:20 +0000766 return nil
767 } else {
768 return fmt.Errorf("expected module that implements ExportableApiStubsSrcProvider, e.g. droidstubs")
769 }
770}
771
Jihoon Kangee113282024-01-23 00:16:41 +0000772func (paths *scopePaths) treatDepAsApiStubsSrcProvider(dep android.Module, action func(provider ApiStubsSrcProvider) error) error {
Paul Duffin0f8faff2020-05-20 16:18:00 +0100773 if apiStubsProvider, ok := dep.(ApiStubsSrcProvider); ok {
Jihoon Kangee113282024-01-23 00:16:41 +0000774 err := action(apiStubsProvider)
775 if err != nil {
776 return err
777 }
Paul Duffin0f8faff2020-05-20 16:18:00 +0100778 return nil
779 } else {
780 return fmt.Errorf("expected module that implements ApiStubsSrcProvider, e.g. droidstubs")
781 }
782}
783
Jihoon Kangee113282024-01-23 00:16:41 +0000784func (paths *scopePaths) extractApiInfoFromApiStubsProvider(provider ApiStubsProvider, stubsType StubsType) error {
785 var annotationsZip, currentApiFilePath, removedApiFilePath android.Path
786 annotationsZip, annotationsZipErr := provider.AnnotationsZip(stubsType)
787 currentApiFilePath, currentApiFilePathErr := provider.ApiFilePath(stubsType)
788 removedApiFilePath, removedApiFilePathErr := provider.RemovedApiFilePath(stubsType)
Paul Duffin0ff08bd2020-04-29 13:30:54 +0100789
Jihoon Kangee113282024-01-23 00:16:41 +0000790 combinedError := errors.Join(annotationsZipErr, currentApiFilePathErr, removedApiFilePathErr)
791
792 if combinedError == nil {
793 paths.annotationsZip = android.OptionalPathForPath(annotationsZip)
794 paths.currentApiFilePath = android.OptionalPathForPath(currentApiFilePath)
795 paths.removedApiFilePath = android.OptionalPathForPath(removedApiFilePath)
796 }
797 return combinedError
Jihoon Kangf55a5f72024-01-08 08:56:20 +0000798}
799
Jihoon Kangee113282024-01-23 00:16:41 +0000800func (paths *scopePaths) extractStubsSourceInfoFromApiStubsProviders(provider ApiStubsSrcProvider, stubsType StubsType) error {
801 stubsSrcJar, err := provider.StubsSrcJar(stubsType)
802 if err == nil {
803 paths.stubsSrcJar = android.OptionalPathForPath(stubsSrcJar)
804 }
805 return err
Jihoon Kangf55a5f72024-01-08 08:56:20 +0000806}
807
Colin Crossdcf71b22021-02-01 13:59:03 -0800808func (paths *scopePaths) extractStubsSourceInfoFromDep(ctx android.ModuleContext, dep android.Module) error {
Jihoon Kang2a26b132024-06-24 07:39:40 +0000809 stubsType := Everything
810 if ctx.Config().ReleaseHiddenApiExportableStubs() {
811 stubsType = Exportable
812 }
Jihoon Kangee113282024-01-23 00:16:41 +0000813 return paths.treatDepAsApiStubsSrcProvider(dep, func(provider ApiStubsSrcProvider) error {
Jihoon Kang2a26b132024-06-24 07:39:40 +0000814 return paths.extractStubsSourceInfoFromApiStubsProviders(provider, stubsType)
Paul Duffin0ff08bd2020-04-29 13:30:54 +0100815 })
816}
817
Colin Crossdcf71b22021-02-01 13:59:03 -0800818func (paths *scopePaths) extractStubsSourceAndApiInfoFromApiStubsProvider(ctx android.ModuleContext, dep android.Module) error {
Jihoon Kang2a26b132024-06-24 07:39:40 +0000819 stubsType := Everything
Jihoon Kangf55a5f72024-01-08 08:56:20 +0000820 if ctx.Config().ReleaseHiddenApiExportableStubs() {
Jihoon Kang2a26b132024-06-24 07:39:40 +0000821 stubsType = Exportable
Jihoon Kangf55a5f72024-01-08 08:56:20 +0000822 }
Jihoon Kangee113282024-01-23 00:16:41 +0000823 return paths.treatDepAsApiStubsProvider(dep, func(provider ApiStubsProvider) error {
Jihoon Kang2a26b132024-06-24 07:39:40 +0000824 extractApiInfoErr := paths.extractApiInfoFromApiStubsProvider(provider, stubsType)
825 extractStubsSourceInfoErr := paths.extractStubsSourceInfoFromApiStubsProviders(provider, stubsType)
Jihoon Kangee113282024-01-23 00:16:41 +0000826 return errors.Join(extractApiInfoErr, extractStubsSourceInfoErr)
Paul Duffin0ff08bd2020-04-29 13:30:54 +0100827 })
828}
829
Jihoon Kang5623e542024-01-31 23:27:26 +0000830func extractOutputPaths(dep android.Module) (android.Paths, error) {
Paul Duffin958806b2022-05-16 13:10:47 +0000831 var paths android.Paths
832 if sourceFileProducer, ok := dep.(android.SourceFileProducer); ok {
833 paths = sourceFileProducer.Srcs()
Jihoon Kang5623e542024-01-31 23:27:26 +0000834 return paths, nil
Paul Duffin958806b2022-05-16 13:10:47 +0000835 } else {
Jihoon Kang5623e542024-01-31 23:27:26 +0000836 return nil, fmt.Errorf("module %q does not produce source files", dep)
Paul Duffin958806b2022-05-16 13:10:47 +0000837 }
Paul Duffin958806b2022-05-16 13:10:47 +0000838}
839
840func (paths *scopePaths) extractLatestApiPath(ctx android.ModuleContext, dep android.Module) error {
Jihoon Kang5623e542024-01-31 23:27:26 +0000841 outputPaths, err := extractOutputPaths(dep)
842 paths.latestApiPaths = outputPaths
Paul Duffin958806b2022-05-16 13:10:47 +0000843 return err
844}
845
846func (paths *scopePaths) extractLatestRemovedApiPath(ctx android.ModuleContext, dep android.Module) error {
Jihoon Kang5623e542024-01-31 23:27:26 +0000847 outputPaths, err := extractOutputPaths(dep)
848 paths.latestRemovedApiPaths = outputPaths
Paul Duffin958806b2022-05-16 13:10:47 +0000849 return err
850}
851
Paul Duffin0ff08bd2020-04-29 13:30:54 +0100852type commonToSdkLibraryAndImportProperties struct {
Paul Duffin1b1e8062020-05-08 13:44:43 +0100853 // The naming scheme to use for the components that this module creates.
854 //
Paul Duffinee9ad5d2020-09-11 13:04:05 +0100855 // If not specified then it defaults to "default".
Paul Duffin1b1e8062020-05-08 13:44:43 +0100856 //
857 // This is a temporary mechanism to simplify conversion from separate modules for each
858 // component that follow a different naming pattern to the default one.
859 //
860 // TODO(b/155480189) - Remove once naming inconsistencies have been resolved.
Paul Duffin0ff08bd2020-04-29 13:30:54 +0100861 Naming_scheme *string
Paul Duffindfa131e2020-05-15 20:37:11 +0100862
863 // Specifies whether this module can be used as an Android shared library; defaults
864 // to true.
865 //
866 // An Android shared library is one that can be referenced in a <uses-library> element
867 // in an AndroidManifest.xml.
868 Shared_library *bool
Paul Duffina2ae7e02020-09-11 11:55:00 +0100869
870 // Files containing information about supported java doc tags.
871 Doctag_files []string `android:"path"`
Pedro Loureiro9956e5e2021-09-07 17:21:59 +0000872
873 // Signals that this shared library is part of the bootclasspath starting
874 // on the version indicated in this attribute.
875 //
876 // This will make platforms at this level and above to ignore
877 // <uses-library> tags with this library name because the library is already
878 // available
879 On_bootclasspath_since *string
880
881 // Signals that this shared library was part of the bootclasspath before
882 // (but not including) the version indicated in this attribute.
883 //
884 // The system will automatically add a <uses-library> tag with this library to
885 // apps that target any SDK less than the version indicated in this attribute.
886 On_bootclasspath_before *string
887
888 // Indicates that PackageManager should ignore this shared library if the
889 // platform is below the version indicated in this attribute.
890 //
891 // This means that the device won't recognise this library as installed.
892 Min_device_sdk *string
893
894 // Indicates that PackageManager should ignore this shared library if the
895 // platform is above the version indicated in this attribute.
896 //
897 // This means that the device won't recognise this library as installed.
898 Max_device_sdk *string
Paul Duffin0ff08bd2020-04-29 13:30:54 +0100899}
900
Paul Duffin71b33cc2021-06-23 11:39:47 +0100901// commonSdkLibraryAndImportModule defines the interface that must be provided by a module that
902// embeds the commonToSdkLibraryAndImport struct.
903type commonSdkLibraryAndImportModule interface {
Paul Duffind796f6f2022-11-23 23:06:05 +0000904 android.Module
Paul Duffin71b33cc2021-06-23 11:39:47 +0100905
Spandan Das23956d12024-01-19 00:22:22 +0000906 // Returns the name of the root java_sdk_library that creates the child stub libraries
907 // This is the `name` as it appears in Android.bp, and not the name in Soong's build graph
908 // (with the prebuilt_ prefix)
909 //
910 // e.g. in the following java_sdk_library_import
911 // java_sdk_library_import {
912 // name: "framework-foo.v1",
913 // source_module_name: "framework-foo",
914 // }
915 // the values returned by
916 // 1. Name(): prebuilt_framework-foo.v1 # unique
917 // 2. BaseModuleName(): framework-foo # the source
918 // 3. RootLibraryName: framework-foo.v1 # the undecordated `name` from Android.bp
919 RootLibraryName() string
920}
921
922func (m *SdkLibrary) RootLibraryName() string {
923 return m.BaseModuleName()
924}
925
926func (m *SdkLibraryImport) RootLibraryName() string {
927 // m.BaseModuleName refers to the source of the import
928 // use moduleBase.Name to get the name of the module as it appears in the .bp file
929 return m.ModuleBase.Name()
Paul Duffin71b33cc2021-06-23 11:39:47 +0100930}
931
Paul Duffin56d44902020-01-31 13:36:25 +0000932// Common code between sdk library and sdk library import
933type commonToSdkLibraryAndImport struct {
Paul Duffin71b33cc2021-06-23 11:39:47 +0100934 module commonSdkLibraryAndImportModule
Paul Duffinc3091c82020-05-08 14:16:20 +0100935
Paul Duffin56d44902020-01-31 13:36:25 +0000936 scopePaths map[*apiScope]*scopePaths
Paul Duffin1b1e8062020-05-08 13:44:43 +0100937
938 namingScheme sdkLibraryComponentNamingScheme
939
Paul Duffindfa131e2020-05-15 20:37:11 +0100940 commonSdkLibraryProperties commonToSdkLibraryAndImportProperties
Paul Duffin859fe962020-05-15 10:20:31 +0100941
Paul Duffina2ae7e02020-09-11 11:55:00 +0100942 // Paths to commonSdkLibraryProperties.Doctag_files
943 doctagPaths android.Paths
944
Paul Duffin859fe962020-05-15 10:20:31 +0100945 // Functionality related to this being used as a component of a java_sdk_library.
946 EmbeddableSdkLibraryComponent
Jihoon Kang8479dea2024-04-04 01:19:05 +0000947
948 // Path to the header jars of the implementation library
949 // This is non-empty only when api_only is false.
950 implLibraryHeaderJars android.Paths
Jihoon Kanga3a05462024-04-05 00:36:44 +0000951
952 // The reference to the implementation library created by the source module.
953 // Is nil if the source module does not exist.
954 implLibraryModule *Library
Paul Duffin56d44902020-01-31 13:36:25 +0000955}
956
Paul Duffin71b33cc2021-06-23 11:39:47 +0100957func (c *commonToSdkLibraryAndImport) initCommon(module commonSdkLibraryAndImportModule) {
958 c.module = module
Paul Duffin1b1e8062020-05-08 13:44:43 +0100959
Paul Duffin71b33cc2021-06-23 11:39:47 +0100960 module.AddProperties(&c.commonSdkLibraryProperties)
Paul Duffin859fe962020-05-15 10:20:31 +0100961
962 // Initialize this as an sdk library component.
Paul Duffin71b33cc2021-06-23 11:39:47 +0100963 c.initSdkLibraryComponent(module)
Paul Duffin1b1e8062020-05-08 13:44:43 +0100964}
965
966func (c *commonToSdkLibraryAndImport) initCommonAfterDefaultsApplied(ctx android.DefaultableHookContext) bool {
Paul Duffindfa131e2020-05-15 20:37:11 +0100967 schemeProperty := proptools.StringDefault(c.commonSdkLibraryProperties.Naming_scheme, "default")
Paul Duffin1b1e8062020-05-08 13:44:43 +0100968 switch schemeProperty {
969 case "default":
970 c.namingScheme = &defaultNamingScheme{}
971 default:
972 ctx.PropertyErrorf("naming_scheme", "expected 'default' but was %q", schemeProperty)
973 return false
974 }
975
Spandan Das23956d12024-01-19 00:22:22 +0000976 namePtr := proptools.StringPtr(c.module.RootLibraryName())
Paul Duffin3f0290e2021-06-30 18:25:36 +0100977 c.sdkLibraryComponentProperties.SdkLibraryName = namePtr
978
Paul Duffindfa131e2020-05-15 20:37:11 +0100979 // Only track this sdk library if this can be used as a shared library.
980 if c.sharedLibrary() {
981 // Use the name specified in the module definition as the owner.
Paul Duffin3f0290e2021-06-30 18:25:36 +0100982 c.sdkLibraryComponentProperties.SdkLibraryToImplicitlyTrack = namePtr
Paul Duffindfa131e2020-05-15 20:37:11 +0100983 }
Paul Duffin859fe962020-05-15 10:20:31 +0100984
Paul Duffin1b1e8062020-05-08 13:44:43 +0100985 return true
Paul Duffinc3091c82020-05-08 14:16:20 +0100986}
987
Paul Duffinea8f8082021-06-24 13:25:57 +0100988// uniqueApexVariations provides common implementation of the ApexModule.UniqueApexVariations
989// method.
990func (c *commonToSdkLibraryAndImport) uniqueApexVariations() bool {
991 // A java_sdk_library that is a shared library produces an XML file that makes the shared library
992 // usable from an AndroidManifest.xml's <uses-library> entry. That XML file contains the name of
993 // the APEX and so it needs a unique variation per APEX.
994 return c.sharedLibrary()
995}
996
Paul Duffina2ae7e02020-09-11 11:55:00 +0100997func (c *commonToSdkLibraryAndImport) generateCommonBuildActions(ctx android.ModuleContext) {
998 c.doctagPaths = android.PathsForModuleSrc(ctx, c.commonSdkLibraryProperties.Doctag_files)
999}
1000
Jihoon Kanga3a05462024-04-05 00:36:44 +00001001func (c *commonToSdkLibraryAndImport) getImplLibraryModule() *Library {
1002 return c.implLibraryModule
1003}
1004
Paul Duffineedc5d52020-06-12 17:46:39 +01001005// Module name of the runtime implementation library
1006func (c *commonToSdkLibraryAndImport) implLibraryModuleName() string {
Spandan Das23956d12024-01-19 00:22:22 +00001007 return c.module.RootLibraryName() + ".impl"
Paul Duffineedc5d52020-06-12 17:46:39 +01001008}
1009
1010// Module name of the XML file for the lib
1011func (c *commonToSdkLibraryAndImport) xmlPermissionsModuleName() string {
Spandan Das23956d12024-01-19 00:22:22 +00001012 return c.module.RootLibraryName() + sdkXmlFileSuffix
Paul Duffineedc5d52020-06-12 17:46:39 +01001013}
1014
Paul Duffinc3091c82020-05-08 14:16:20 +01001015// Name of the java_library module that compiles the stubs source.
1016func (c *commonToSdkLibraryAndImport) stubsLibraryModuleName(apiScope *apiScope) string {
Spandan Das23956d12024-01-19 00:22:22 +00001017 baseName := c.module.RootLibraryName()
Paul Duffin21787622022-11-25 12:48:20 +00001018 return c.namingScheme.stubsLibraryModuleName(apiScope, baseName)
Paul Duffinc3091c82020-05-08 14:16:20 +01001019}
1020
Jihoon Kangfa4a90d2023-12-20 02:53:38 +00001021// Name of the java_library module that compiles the exportable stubs source.
1022func (c *commonToSdkLibraryAndImport) exportableStubsLibraryModuleName(apiScope *apiScope) string {
Spandan Das23956d12024-01-19 00:22:22 +00001023 baseName := c.module.RootLibraryName()
Jihoon Kangfa4a90d2023-12-20 02:53:38 +00001024 return c.namingScheme.exportableStubsLibraryModuleName(apiScope, baseName)
1025}
1026
Paul Duffinc3091c82020-05-08 14:16:20 +01001027// Name of the droidstubs module that generates the stubs source and may also
1028// generate/check the API.
1029func (c *commonToSdkLibraryAndImport) stubsSourceModuleName(apiScope *apiScope) string {
Spandan Das23956d12024-01-19 00:22:22 +00001030 baseName := c.module.RootLibraryName()
Paul Duffin21787622022-11-25 12:48:20 +00001031 return c.namingScheme.stubsSourceModuleName(apiScope, baseName)
Paul Duffinc3091c82020-05-08 14:16:20 +01001032}
1033
Jihoon Kang1c92c3e2023-03-23 17:44:51 +00001034// Name of the java_api_library module that generates the from-text stubs source
1035// and compiles to a jar file.
1036func (c *commonToSdkLibraryAndImport) apiLibraryModuleName(apiScope *apiScope) string {
Spandan Das23956d12024-01-19 00:22:22 +00001037 baseName := c.module.RootLibraryName()
Jihoon Kang1c92c3e2023-03-23 17:44:51 +00001038 return c.namingScheme.apiLibraryModuleName(apiScope, baseName)
1039}
1040
Jihoon Kang1147b312023-06-08 23:25:57 +00001041// Name of the java_library module that compiles the stubs
1042// generated from source Java files.
Jihoon Kangfa4a90d2023-12-20 02:53:38 +00001043func (c *commonToSdkLibraryAndImport) sourceStubsLibraryModuleName(apiScope *apiScope) string {
Spandan Das23956d12024-01-19 00:22:22 +00001044 baseName := c.module.RootLibraryName()
Jihoon Kangfa4a90d2023-12-20 02:53:38 +00001045 return c.namingScheme.sourceStubsLibraryModuleName(apiScope, baseName)
1046}
1047
1048// Name of the java_library module that compiles the exportable stubs
1049// generated from source Java files.
1050func (c *commonToSdkLibraryAndImport) exportableSourceStubsLibraryModuleName(apiScope *apiScope) string {
Spandan Das23956d12024-01-19 00:22:22 +00001051 baseName := c.module.RootLibraryName()
Jihoon Kangfa4a90d2023-12-20 02:53:38 +00001052 return c.namingScheme.exportableSourceStubsLibraryModuleName(apiScope, baseName)
Jihoon Kang1147b312023-06-08 23:25:57 +00001053}
1054
Paul Duffin46dc45a2020-05-14 15:39:10 +01001055// The component names for different outputs of the java_sdk_library.
1056//
1057// They are similar to the names used for the child modules it creates
1058const (
1059 stubsSourceComponentName = "stubs.source"
1060
1061 apiTxtComponentName = "api.txt"
1062
1063 removedApiTxtComponentName = "removed-api.txt"
Anton Hanssond78eb762021-09-21 15:25:12 +01001064
1065 annotationsComponentName = "annotations.zip"
Paul Duffin46dc45a2020-05-14 15:39:10 +01001066)
1067
1068// A regular expression to match tags that reference a specific stubs component.
1069//
1070// It will only match if given a valid scope and a valid component. It is verfy strict
1071// to ensure it does not accidentally match a similar looking tag that should be processed
1072// by the embedded Library.
1073var tagSplitter = func() *regexp.Regexp {
1074 // Given a list of literal string items returns a regular expression that will
1075 // match any one of the items.
1076 choice := func(items ...string) string {
1077 return `\Q` + strings.Join(items, `\E|\Q`) + `\E`
1078 }
1079
1080 // Regular expression to match one of the scopes.
1081 scopesRegexp := choice(allScopeNames...)
1082
1083 // Regular expression to match one of the components.
Anton Hanssond78eb762021-09-21 15:25:12 +01001084 componentsRegexp := choice(stubsSourceComponentName, apiTxtComponentName, removedApiTxtComponentName, annotationsComponentName)
Paul Duffin46dc45a2020-05-14 15:39:10 +01001085
1086 // Regular expression to match any combination of one scope and one component.
1087 return regexp.MustCompile(fmt.Sprintf(`^\.(%s)\.(%s)$`, scopesRegexp, componentsRegexp))
1088}()
1089
mrziwang9f7b9f42024-07-10 12:18:06 -07001090func (module *commonToSdkLibraryAndImport) setOutputFiles(ctx android.ModuleContext) {
1091 if module.doctagPaths != nil {
1092 ctx.SetOutputFiles(module.doctagPaths, ".doctags")
1093 }
1094 for _, scopeName := range android.SortedKeys(scopeByName) {
1095 paths := module.findScopePaths(scopeByName[scopeName])
1096 if paths == nil {
1097 continue
Paul Duffin46dc45a2020-05-14 15:39:10 +01001098 }
mrziwang9f7b9f42024-07-10 12:18:06 -07001099 componentToOutput := map[string]android.OptionalPath{
1100 stubsSourceComponentName: paths.stubsSrcJar,
1101 apiTxtComponentName: paths.currentApiFilePath,
1102 removedApiTxtComponentName: paths.removedApiFilePath,
1103 annotationsComponentName: paths.annotationsZip,
1104 }
1105 for _, component := range android.SortedKeys(componentToOutput) {
1106 if componentToOutput[component].Valid() {
1107 ctx.SetOutputFiles(android.Paths{componentToOutput[component].Path()}, "."+scopeName+"."+component)
Paul Duffina2ae7e02020-09-11 11:55:00 +01001108 }
1109 }
Paul Duffin46dc45a2020-05-14 15:39:10 +01001110 }
1111}
1112
Paul Duffin803a9562020-05-20 11:52:25 +01001113func (c *commonToSdkLibraryAndImport) getScopePathsCreateIfNeeded(scope *apiScope) *scopePaths {
Paul Duffin56d44902020-01-31 13:36:25 +00001114 if c.scopePaths == nil {
1115 c.scopePaths = make(map[*apiScope]*scopePaths)
1116 }
1117 paths := c.scopePaths[scope]
1118 if paths == nil {
1119 paths = &scopePaths{}
1120 c.scopePaths[scope] = paths
1121 }
1122
1123 return paths
1124}
1125
Paul Duffin803a9562020-05-20 11:52:25 +01001126func (c *commonToSdkLibraryAndImport) findScopePaths(scope *apiScope) *scopePaths {
1127 if c.scopePaths == nil {
1128 return nil
1129 }
1130
1131 return c.scopePaths[scope]
1132}
1133
1134// If this does not support the requested api scope then find the closest available
1135// scope it does support. Returns nil if no such scope is available.
1136func (c *commonToSdkLibraryAndImport) findClosestScopePath(scope *apiScope) *scopePaths {
Paul Duffind0b9fca2022-09-30 18:11:41 +01001137 for s := scope; s != nil; s = s.canAccess {
Paul Duffin803a9562020-05-20 11:52:25 +01001138 if paths := c.findScopePaths(s); paths != nil {
1139 return paths
1140 }
1141 }
1142
1143 // This should never happen outside tests as public should be the base scope for every
1144 // scope and is enabled by default.
1145 return nil
1146}
1147
Jiyong Parkf1691d22021-03-29 20:11:58 +09001148func (c *commonToSdkLibraryAndImport) selectHeaderJarsForSdkVersion(ctx android.BaseModuleContext, sdkVersion android.SdkSpec) android.Paths {
Paul Duffinb05d4292020-05-20 12:19:10 +01001149
1150 // If a specific numeric version has been requested then use prebuilt versions of the sdk.
Jiyong Park54105c42021-03-31 18:17:53 +09001151 if !sdkVersion.ApiLevel.IsPreview() {
Spandan Das23956d12024-01-19 00:22:22 +00001152 return PrebuiltJars(ctx, c.module.RootLibraryName(), sdkVersion)
Paul Duffinb05d4292020-05-20 12:19:10 +01001153 }
1154
Paul Duffin1267d872021-04-16 17:21:36 +01001155 paths := c.selectScopePaths(ctx, sdkVersion.Kind)
1156 if paths == nil {
1157 return nil
1158 }
1159
1160 return paths.stubsHeaderPath
1161}
1162
1163// selectScopePaths returns the *scopePaths appropriate for the specific kind.
1164//
1165// If the module does not support the specific kind then it will return the *scopePaths for the
1166// closest kind which is a subset of the requested kind. e.g. if requesting android.SdkModule then
1167// it will return *scopePaths for android.SdkSystem if available or android.SdkPublic of not.
1168func (c *commonToSdkLibraryAndImport) selectScopePaths(ctx android.BaseModuleContext, kind android.SdkKind) *scopePaths {
Paul Duffin32cf58a2021-05-18 16:32:50 +01001169 apiScope := sdkKindToApiScope(kind)
Paul Duffinb05d4292020-05-20 12:19:10 +01001170
Paul Duffin803a9562020-05-20 11:52:25 +01001171 paths := c.findClosestScopePath(apiScope)
1172 if paths == nil {
1173 var scopes []string
Jihoon Kang98aa8fa2024-06-07 11:06:57 +00001174 for _, s := range AllApiScopes {
Paul Duffin803a9562020-05-20 11:52:25 +01001175 if c.findScopePaths(s) != nil {
1176 scopes = append(scopes, s.name)
1177 }
1178 }
Spandan Das23956d12024-01-19 00:22:22 +00001179 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 +01001180 return nil
1181 }
1182
Paul Duffin1267d872021-04-16 17:21:36 +01001183 return paths
1184}
1185
Paul Duffin32cf58a2021-05-18 16:32:50 +01001186// sdkKindToApiScope maps from android.SdkKind to apiScope.
1187func sdkKindToApiScope(kind android.SdkKind) *apiScope {
1188 var apiScope *apiScope
1189 switch kind {
1190 case android.SdkSystem:
1191 apiScope = apiScopeSystem
1192 case android.SdkModule:
1193 apiScope = apiScopeModuleLib
1194 case android.SdkTest:
1195 apiScope = apiScopeTest
1196 case android.SdkSystemServer:
1197 apiScope = apiScopeSystemServer
1198 default:
1199 apiScope = apiScopePublic
1200 }
1201 return apiScope
1202}
1203
Paul Duffin1267d872021-04-16 17:21:36 +01001204// to satisfy SdkLibraryDependency interface
Martin Stjernholm8be1e6d2021-09-15 03:34:04 +01001205func (c *commonToSdkLibraryAndImport) SdkApiStubDexJar(ctx android.BaseModuleContext, kind android.SdkKind) OptionalDexJarPath {
Paul Duffin1267d872021-04-16 17:21:36 +01001206 paths := c.selectScopePaths(ctx, kind)
1207 if paths == nil {
Martin Stjernholm8be1e6d2021-09-15 03:34:04 +01001208 return makeUnsetDexJarPath()
Paul Duffin1267d872021-04-16 17:21:36 +01001209 }
1210
1211 return paths.stubsDexJarPath
Paul Duffinb05d4292020-05-20 12:19:10 +01001212}
1213
Paul Duffin32cf58a2021-05-18 16:32:50 +01001214// to satisfy SdkLibraryDependency interface
Jihoon Kangbd093452023-12-26 19:08:01 +00001215func (c *commonToSdkLibraryAndImport) SdkApiExportableStubDexJar(ctx android.BaseModuleContext, kind android.SdkKind) OptionalDexJarPath {
1216 paths := c.selectScopePaths(ctx, kind)
1217 if paths == nil {
1218 return makeUnsetDexJarPath()
1219 }
1220
1221 return paths.exportableStubsDexJarPath
1222}
1223
1224// to satisfy SdkLibraryDependency interface
Paul Duffin32cf58a2021-05-18 16:32:50 +01001225func (c *commonToSdkLibraryAndImport) SdkRemovedTxtFile(ctx android.BaseModuleContext, kind android.SdkKind) android.OptionalPath {
1226 apiScope := sdkKindToApiScope(kind)
1227 paths := c.findScopePaths(apiScope)
1228 if paths == nil {
1229 return android.OptionalPath{}
1230 }
1231
1232 return paths.removedApiFilePath
1233}
1234
Paul Duffin859fe962020-05-15 10:20:31 +01001235func (c *commonToSdkLibraryAndImport) sdkComponentPropertiesForChildLibrary() interface{} {
1236 componentProps := &struct {
Paul Duffin3f0290e2021-06-30 18:25:36 +01001237 SdkLibraryName *string
Paul Duffin859fe962020-05-15 10:20:31 +01001238 SdkLibraryToImplicitlyTrack *string
Paul Duffindfa131e2020-05-15 20:37:11 +01001239 }{}
1240
Spandan Das23956d12024-01-19 00:22:22 +00001241 namePtr := proptools.StringPtr(c.module.RootLibraryName())
Paul Duffin3f0290e2021-06-30 18:25:36 +01001242 componentProps.SdkLibraryName = namePtr
1243
Paul Duffindfa131e2020-05-15 20:37:11 +01001244 if c.sharedLibrary() {
Paul Duffin859fe962020-05-15 10:20:31 +01001245 // Mark the stubs library as being components of this java_sdk_library so that
1246 // any app that includes code which depends (directly or indirectly) on the stubs
1247 // library will have the appropriate <uses-library> invocation inserted into its
1248 // manifest if necessary.
Paul Duffin3f0290e2021-06-30 18:25:36 +01001249 componentProps.SdkLibraryToImplicitlyTrack = namePtr
Paul Duffin859fe962020-05-15 10:20:31 +01001250 }
1251
1252 return componentProps
1253}
1254
Paul Duffindfa131e2020-05-15 20:37:11 +01001255func (c *commonToSdkLibraryAndImport) sharedLibrary() bool {
1256 return proptools.BoolDefault(c.commonSdkLibraryProperties.Shared_library, true)
1257}
1258
Paul Duffinf4600f62021-05-13 22:34:45 +01001259// Check if the stub libraries should be compiled for dex
1260func (c *commonToSdkLibraryAndImport) stubLibrariesCompiledForDex() bool {
1261 // Always compile the dex file files for the stub libraries if they will be used on the
1262 // bootclasspath.
1263 return !c.sharedLibrary()
1264}
1265
Paul Duffin859fe962020-05-15 10:20:31 +01001266// Properties related to the use of a module as an component of a java_sdk_library.
1267type SdkLibraryComponentProperties struct {
Paul Duffin3f0290e2021-06-30 18:25:36 +01001268 // The name of the java_sdk_library/_import module.
1269 SdkLibraryName *string `blueprint:"mutated"`
Paul Duffin859fe962020-05-15 10:20:31 +01001270
1271 // The name of the java_sdk_library/_import to add to a <uses-library> entry
1272 // in the AndroidManifest.xml of any Android app that includes code that references
1273 // this module. If not set then no java_sdk_library/_import is tracked.
1274 SdkLibraryToImplicitlyTrack *string `blueprint:"mutated"`
1275}
1276
1277// Structure to be embedded in a module struct that needs to support the
1278// SdkLibraryComponentDependency interface.
1279type EmbeddableSdkLibraryComponent struct {
1280 sdkLibraryComponentProperties SdkLibraryComponentProperties
1281}
1282
Paul Duffin71b33cc2021-06-23 11:39:47 +01001283func (e *EmbeddableSdkLibraryComponent) initSdkLibraryComponent(module android.Module) {
1284 module.AddProperties(&e.sdkLibraryComponentProperties)
Paul Duffin859fe962020-05-15 10:20:31 +01001285}
1286
1287// to satisfy SdkLibraryComponentDependency
Paul Duffin3f0290e2021-06-30 18:25:36 +01001288func (e *EmbeddableSdkLibraryComponent) SdkLibraryName() *string {
1289 return e.sdkLibraryComponentProperties.SdkLibraryName
1290}
1291
1292// to satisfy SdkLibraryComponentDependency
Ulya Trafimovich39b437b2020-09-23 16:42:35 +01001293func (e *EmbeddableSdkLibraryComponent) OptionalSdkLibraryImplementation() *string {
Ulya Trafimovich78645fb2021-07-16 15:29:25 +01001294 // For shared libraries, this is the same as the SDK library name. If a Java library or app
1295 // depends on a component library (e.g. a stub library) it still needs to know the name of the
1296 // run-time library and the corresponding module that provides the implementation. This name is
1297 // passed to manifest_fixer (to be added to AndroidManifest.xml) and added to CLC (to be used
1298 // in dexpreopt).
1299 //
1300 // For non-shared SDK (component or not) libraries this returns `nil`, as they are not
1301 // <uses-library> and should not be added to the manifest or to CLC.
Ulya Trafimovich39b437b2020-09-23 16:42:35 +01001302 return e.sdkLibraryComponentProperties.SdkLibraryToImplicitlyTrack
1303}
1304
Paul Duffin859fe962020-05-15 10:20:31 +01001305// Implemented by modules that are (or possibly could be) a component of a java_sdk_library
1306// (including the java_sdk_library) itself.
1307type SdkLibraryComponentDependency interface {
Ulya Trafimovich31e444e2020-08-14 17:32:16 +01001308 UsesLibraryDependency
1309
Paul Duffin3f0290e2021-06-30 18:25:36 +01001310 // SdkLibraryName returns the name of the java_sdk_library/_import module.
1311 SdkLibraryName() *string
1312
Ulya Trafimovich39b437b2020-09-23 16:42:35 +01001313 // The name of the implementation library for the optional SDK library or nil, if there isn't one.
1314 OptionalSdkLibraryImplementation() *string
Paul Duffin859fe962020-05-15 10:20:31 +01001315}
1316
1317// Make sure that all the module types that are components of java_sdk_library/_import
1318// and which can be referenced (directly or indirectly) from an android app implement
1319// the SdkLibraryComponentDependency interface.
1320var _ SdkLibraryComponentDependency = (*Library)(nil)
1321var _ SdkLibraryComponentDependency = (*Import)(nil)
1322var _ SdkLibraryComponentDependency = (*SdkLibrary)(nil)
Paul Duffineedc5d52020-06-12 17:46:39 +01001323var _ SdkLibraryComponentDependency = (*SdkLibraryImport)(nil)
Paul Duffin859fe962020-05-15 10:20:31 +01001324
Paul Duffin32cf58a2021-05-18 16:32:50 +01001325// Provides access to sdk_version related files, e.g. header and implementation jars.
Paul Duffin859fe962020-05-15 10:20:31 +01001326type SdkLibraryDependency interface {
1327 SdkLibraryComponentDependency
1328
1329 // Get the header jars appropriate for the supplied sdk_version.
1330 //
1331 // These are turbine generated jars so they only change if the externals of the
1332 // class changes but it does not contain and implementation or JavaDoc.
Jiyong Parkf1691d22021-03-29 20:11:58 +09001333 SdkHeaderJars(ctx android.BaseModuleContext, sdkVersion android.SdkSpec) android.Paths
Paul Duffin859fe962020-05-15 10:20:31 +01001334
Jihoon Kangbd093452023-12-26 19:08:01 +00001335 // SdkApiStubDexJar returns the dex jar for the stubs for the prebuilt
1336 // java_sdk_library_import module. It is needed by the hiddenapi processing tool which
1337 // processes dex files.
Martin Stjernholm8be1e6d2021-09-15 03:34:04 +01001338 SdkApiStubDexJar(ctx android.BaseModuleContext, kind android.SdkKind) OptionalDexJarPath
Paul Duffinf4600f62021-05-13 22:34:45 +01001339
Jihoon Kangbd093452023-12-26 19:08:01 +00001340 // SdkApiExportableStubDexJar returns the exportable dex jar for the stubs for
1341 // java_sdk_library module. It is needed by the hiddenapi processing tool which processes
1342 // dex files.
1343 SdkApiExportableStubDexJar(ctx android.BaseModuleContext, kind android.SdkKind) OptionalDexJarPath
1344
Paul Duffin32cf58a2021-05-18 16:32:50 +01001345 // SdkRemovedTxtFile returns the optional path to the removed.txt file for the specified sdk kind.
1346 SdkRemovedTxtFile(ctx android.BaseModuleContext, kind android.SdkKind) android.OptionalPath
1347
Paul Duffinf4600f62021-05-13 22:34:45 +01001348 // sharedLibrary returns true if this can be used as a shared library.
1349 sharedLibrary() bool
Jihoon Kanga3a05462024-04-05 00:36:44 +00001350
1351 getImplLibraryModule() *Library
Paul Duffin859fe962020-05-15 10:20:31 +01001352}
1353
Inseob Kimc0907f12019-02-08 21:00:45 +09001354type SdkLibrary struct {
Sundong Ahn054b19a2018-10-19 13:46:09 +09001355 Library
Jiyong Parkc678ad32018-04-10 13:07:10 +09001356
Sundong Ahn054b19a2018-10-19 13:46:09 +09001357 sdkLibraryProperties sdkLibraryProperties
Jiyong Parkc678ad32018-04-10 13:07:10 +09001358
Paul Duffin3375e352020-04-28 10:44:03 +01001359 // Map from api scope to the scope specific property structure.
1360 scopeToProperties map[*apiScope]*ApiScopeProperties
1361
Paul Duffin56d44902020-01-31 13:36:25 +00001362 commonToSdkLibraryAndImport
Jihoon Kanga3a05462024-04-05 00:36:44 +00001363
1364 builtInstalledForApex []dexpreopterInstall
Jiyong Parkc678ad32018-04-10 13:07:10 +09001365}
1366
Inseob Kimc0907f12019-02-08 21:00:45 +09001367var _ SdkLibraryDependency = (*SdkLibrary)(nil)
Colin Cross897d2ed2019-02-11 14:03:51 -08001368
Paul Duffin3375e352020-04-28 10:44:03 +01001369func (module *SdkLibrary) generateTestAndSystemScopesByDefault() bool {
1370 return module.sdkLibraryProperties.Generate_system_and_test_apis
1371}
1372
Jihoon Kanga3a05462024-04-05 00:36:44 +00001373func (module *SdkLibrary) DexJarBuildPath(ctx android.ModuleErrorfContext) OptionalDexJarPath {
1374 if module.implLibraryModule != nil {
1375 return module.implLibraryModule.DexJarBuildPath(ctx)
1376 }
1377 return makeUnsetDexJarPath()
1378}
1379
1380func (module *SdkLibrary) DexJarInstallPath() android.Path {
1381 if module.implLibraryModule != nil {
1382 return module.implLibraryModule.DexJarInstallPath()
1383 }
1384 return nil
1385}
1386
Paul Duffin3375e352020-04-28 10:44:03 +01001387func (module *SdkLibrary) getGeneratedApiScopes(ctx android.EarlyModuleContext) apiScopes {
1388 // Check to see if any scopes have been explicitly enabled. If any have then all
1389 // must be.
1390 anyScopesExplicitlyEnabled := false
Jihoon Kang98aa8fa2024-06-07 11:06:57 +00001391 for _, scope := range AllApiScopes {
Paul Duffin3375e352020-04-28 10:44:03 +01001392 scopeProperties := module.scopeToProperties[scope]
1393 if scopeProperties.Enabled != nil {
1394 anyScopesExplicitlyEnabled = true
1395 break
1396 }
Paul Duffind1b3a922020-01-22 11:57:20 +00001397 }
Paul Duffin3375e352020-04-28 10:44:03 +01001398
1399 var generatedScopes apiScopes
1400 enabledScopes := make(map[*apiScope]struct{})
Jihoon Kang98aa8fa2024-06-07 11:06:57 +00001401 for _, scope := range AllApiScopes {
Paul Duffin3375e352020-04-28 10:44:03 +01001402 scopeProperties := module.scopeToProperties[scope]
1403 // If any scopes are explicitly enabled then ignore the legacy enabled status.
1404 // This is to ensure that any new usages of this module type do not rely on legacy
1405 // behaviour.
1406 defaultEnabledStatus := false
1407 if anyScopesExplicitlyEnabled {
1408 defaultEnabledStatus = scope.defaultEnabledStatus
1409 } else {
1410 defaultEnabledStatus = scope.legacyEnabledStatus(module)
1411 }
1412 enabled := proptools.BoolDefault(scopeProperties.Enabled, defaultEnabledStatus)
1413 if enabled {
1414 enabledScopes[scope] = struct{}{}
1415 generatedScopes = append(generatedScopes, scope)
1416 }
1417 }
1418
1419 // Now check to make sure that any scope that is extended by an enabled scope is also
1420 // enabled.
Jihoon Kang98aa8fa2024-06-07 11:06:57 +00001421 for _, scope := range AllApiScopes {
Paul Duffin3375e352020-04-28 10:44:03 +01001422 if _, ok := enabledScopes[scope]; ok {
1423 extends := scope.extends
1424 if extends != nil {
1425 if _, ok := enabledScopes[extends]; !ok {
1426 ctx.ModuleErrorf("enabled api scope %q depends on disabled scope %q", scope, extends)
1427 }
1428 }
1429 }
1430 }
1431
1432 return generatedScopes
Paul Duffind1b3a922020-01-22 11:57:20 +00001433}
1434
satayev758968a2021-12-06 11:42:40 +00001435var _ android.ModuleWithMinSdkVersionCheck = (*SdkLibrary)(nil)
1436
satayev8f088b02021-12-06 11:40:46 +00001437func (module *SdkLibrary) CheckMinSdkVersion(ctx android.ModuleContext) {
Jihoon Kanga3a05462024-04-05 00:36:44 +00001438 CheckMinSdkVersion(ctx, &module.Library)
1439}
1440
1441func CheckMinSdkVersion(ctx android.ModuleContext, module *Library) {
Spandan Das8c9ae7e2023-03-03 21:20:36 +00001442 android.CheckMinSdkVersion(ctx, module.MinSdkVersion(ctx), func(c android.ModuleContext, do android.PayloadDepsCallback) {
satayev8f088b02021-12-06 11:40:46 +00001443 ctx.WalkDeps(func(child android.Module, parent android.Module) bool {
1444 isExternal := !module.depIsInSameApex(ctx, child)
1445 if am, ok := child.(android.ApexModule); ok {
1446 if !do(ctx, parent, am, isExternal) {
1447 return false
1448 }
1449 }
1450 return !isExternal
1451 })
1452 })
1453}
1454
Paul Duffineedc5d52020-06-12 17:46:39 +01001455type sdkLibraryComponentTag struct {
1456 blueprint.BaseDependencyTag
1457 name string
1458}
1459
1460// Mark this tag so dependencies that use it are excluded from visibility enforcement.
1461func (t sdkLibraryComponentTag) ExcludeFromVisibilityEnforcement() {}
1462
1463var xmlPermissionsFileTag = sdkLibraryComponentTag{name: "xml-permissions-file"}
Paul Duffine74ac732020-02-06 13:51:46 +00001464
Jiyong Parke3833882020-02-17 17:28:10 +09001465func IsXmlPermissionsFileDepTag(depTag blueprint.DependencyTag) bool {
Paul Duffineedc5d52020-06-12 17:46:39 +01001466 if dt, ok := depTag.(sdkLibraryComponentTag); ok {
Jiyong Parke3833882020-02-17 17:28:10 +09001467 return dt == xmlPermissionsFileTag
1468 }
1469 return false
1470}
1471
Paul Duffineedc5d52020-06-12 17:46:39 +01001472var implLibraryTag = sdkLibraryComponentTag{name: "impl-library"}
Paul Duffin5df79302020-05-16 15:52:12 +01001473
Jeongik Chaaaa6dcd2024-05-22 00:41:28 +09001474var _ android.InstallNeededDependencyTag = sdkLibraryComponentTag{}
1475
Jihoon Kang46d66de2024-05-22 22:42:39 +00001476// To satisfy the CopyDirectlyInAnyApexTag interface. Implementation library of the sdk library
1477// in an apex is considered to be directly in the apex, as if it was listed in java_libs.
1478func (t sdkLibraryComponentTag) CopyDirectlyInAnyApex() {}
1479
1480var _ android.CopyDirectlyInAnyApexTag = implLibraryTag
1481
Jeongik Chaaaa6dcd2024-05-22 00:41:28 +09001482func (t sdkLibraryComponentTag) InstallDepNeeded() bool {
1483 return t.name == "xml-permissions-file" || t.name == "impl-library"
1484}
1485
Paul Duffin44f1d842020-06-26 20:17:02 +01001486// Add the dependencies on the child modules in the component deps mutator.
1487func (module *SdkLibrary) ComponentDepsMutator(ctx android.BottomUpMutatorContext) {
Paul Duffin3375e352020-04-28 10:44:03 +01001488 for _, apiScope := range module.getGeneratedApiScopes(ctx) {
Paul Duffind1b3a922020-01-22 11:57:20 +00001489 // Add dependencies to the stubs library
Spandan Das877f39d2023-03-29 16:19:51 +00001490 stubModuleName := module.stubsLibraryModuleName(apiScope)
Jihoon Kangbd093452023-12-26 19:08:01 +00001491 ctx.AddVariationDependencies(nil, apiScope.everythingStubsTag, stubModuleName)
Jihoon Kang1147b312023-06-08 23:25:57 +00001492
Jihoon Kangbd093452023-12-26 19:08:01 +00001493 exportableStubModuleName := module.exportableStubsLibraryModuleName(apiScope)
1494 ctx.AddVariationDependencies(nil, apiScope.exportableStubsTag, exportableStubModuleName)
Paul Duffind1b3a922020-01-22 11:57:20 +00001495
Paul Duffin15f34ef2020-07-20 18:04:44 +01001496 // Add a dependency on the stubs source in order to access both stubs source and api information.
1497 ctx.AddVariationDependencies(nil, apiScope.stubsSourceAndApiTag, module.stubsSourceModuleName(apiScope))
Paul Duffin958806b2022-05-16 13:10:47 +00001498
1499 if module.compareAgainstLatestApi(apiScope) {
1500 // Add dependencies on the latest finalized version of the API .txt file.
1501 latestApiModuleName := module.latestApiModuleName(apiScope)
1502 ctx.AddDependency(module, apiScope.latestApiModuleTag, latestApiModuleName)
1503
1504 // Add dependencies on the latest finalized version of the remove API .txt file.
1505 latestRemovedApiModuleName := module.latestRemovedApiModuleName(apiScope)
1506 ctx.AddDependency(module, apiScope.latestRemovedApiModuleTag, latestRemovedApiModuleName)
1507 }
Sundong Ahn054b19a2018-10-19 13:46:09 +09001508 }
1509
Paul Duffindfa131e2020-05-15 20:37:11 +01001510 if module.requiresRuntimeImplementationLibrary() {
Paul Duffin5df79302020-05-16 15:52:12 +01001511 // Add dependency to the rule for generating the implementation library.
1512 ctx.AddDependency(module, implLibraryTag, module.implLibraryModuleName())
1513
Paul Duffindfa131e2020-05-15 20:37:11 +01001514 if module.sharedLibrary() {
1515 // Add dependency to the rule for generating the xml permissions file
Paul Duffineedc5d52020-06-12 17:46:39 +01001516 ctx.AddDependency(module, xmlPermissionsFileTag, module.xmlPermissionsModuleName())
Paul Duffindfa131e2020-05-15 20:37:11 +01001517 }
Paul Duffin44f1d842020-06-26 20:17:02 +01001518 }
1519}
Paul Duffine74ac732020-02-06 13:51:46 +00001520
Paul Duffin44f1d842020-06-26 20:17:02 +01001521// Add other dependencies as normal.
1522func (module *SdkLibrary) DepsMutator(ctx android.BottomUpMutatorContext) {
Jihoon Kange4a90172024-07-18 22:49:08 +00001523 // If the module does not create an implementation library or defaults to stubs,
1524 // mark the top level sdk library as stubs module as the module will provide stubs via
1525 // "magic" when listed as a dependency in the Android.bp files.
1526 notCreateImplLib := proptools.Bool(module.sdkLibraryProperties.Api_only)
1527 preferStubs := proptools.Bool(module.sdkLibraryProperties.Default_to_stubs)
1528 module.properties.Is_stubs_module = proptools.BoolPtr(notCreateImplLib || preferStubs)
1529
Anton Hanssone77fccc2021-01-20 16:52:41 +00001530 var missingApiModules []string
1531 for _, apiScope := range module.getGeneratedApiScopes(ctx) {
1532 if apiScope.unstable {
1533 continue
1534 }
Paul Duffin958806b2022-05-16 13:10:47 +00001535 if m := module.latestApiModuleName(apiScope); !ctx.OtherModuleExists(m) {
Anton Hanssone77fccc2021-01-20 16:52:41 +00001536 missingApiModules = append(missingApiModules, m)
1537 }
Paul Duffin958806b2022-05-16 13:10:47 +00001538 if m := module.latestRemovedApiModuleName(apiScope); !ctx.OtherModuleExists(m) {
Anton Hanssone77fccc2021-01-20 16:52:41 +00001539 missingApiModules = append(missingApiModules, m)
1540 }
Paul Duffin958806b2022-05-16 13:10:47 +00001541 if m := module.latestIncompatibilitiesModuleName(apiScope); !ctx.OtherModuleExists(m) {
Jaewoong Jung1a97ee02021-03-09 13:25:02 -08001542 missingApiModules = append(missingApiModules, m)
1543 }
Anton Hanssone77fccc2021-01-20 16:52:41 +00001544 }
1545 if len(missingApiModules) != 0 && !module.sdkLibraryProperties.Unsafe_ignore_missing_latest_api {
1546 m := module.Name() + " is missing tracking files for previously released library versions.\n"
1547 m += "You need to do one of the following:\n"
1548 m += "- Add `unsafe_ignore_missing_latest_api: true` to your blueprint (to disable compat tracking)\n"
1549 m += "- Add a set of prebuilt txt files representing the last released version of this library for compat checking.\n"
1550 m += " (the current set of API files can be used as a seed for this compatibility tracking\n"
1551 m += "\n"
1552 m += "The following filegroup modules are missing:\n "
1553 m += strings.Join(missingApiModules, "\n ") + "\n"
1554 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."
1555 ctx.ModuleErrorf(m)
1556 }
Jiyong Parkc678ad32018-04-10 13:07:10 +09001557}
1558
Inseob Kimc0907f12019-02-08 21:00:45 +09001559func (module *SdkLibrary) GenerateAndroidBuildActions(ctx android.ModuleContext) {
Spandan Das5ae65ee2024-04-16 22:03:26 +00001560 if disableSourceApexVariant(ctx) {
1561 // Prebuilts are active, do not create the installation rules for the source javalib.
1562 // Even though the source javalib is not used, we need to hide it to prevent duplicate installation rules.
1563 // TODO (b/331665856): Implement a principled solution for this.
1564 module.HideFromMake()
1565 }
satayev8f088b02021-12-06 11:40:46 +00001566
Paul Duffina2ae7e02020-09-11 11:55:00 +01001567 module.generateCommonBuildActions(ctx)
1568
Jihoon Kanga3a05462024-04-05 00:36:44 +00001569 module.stem = proptools.StringDefault(module.overridableProperties.Stem, ctx.ModuleName())
1570
1571 module.provideHiddenAPIPropertyInfo(ctx)
Sundong Ahn054b19a2018-10-19 13:46:09 +09001572
Paul Duffinb97b1572021-04-29 21:50:40 +01001573 // Collate the components exported by this module. All scope specific modules are exported but
1574 // the impl and xml component modules are not.
1575 exportedComponents := map[string]struct{}{}
1576
Sundong Ahn57368eb2018-07-06 11:20:23 +09001577 // Record the paths to the header jars of the library (stubs and impl).
Paul Duffind1b3a922020-01-22 11:57:20 +00001578 // When this java_sdk_library is depended upon from others via "libs" property,
Jiyong Parkc678ad32018-04-10 13:07:10 +09001579 // the recorded paths will be returned depending on the link type of the caller.
1580 ctx.VisitDirectDeps(func(to android.Module) {
Jiyong Parkc678ad32018-04-10 13:07:10 +09001581 tag := ctx.OtherModuleDependencyTag(to)
1582
Paul Duffinc8782502020-04-29 20:45:27 +01001583 // Extract information from any of the scope specific dependencies.
1584 if scopeTag, ok := tag.(scopeDependencyTag); ok {
1585 apiScope := scopeTag.apiScope
Paul Duffin803a9562020-05-20 11:52:25 +01001586 scopePaths := module.getScopePathsCreateIfNeeded(apiScope)
Paul Duffinc8782502020-04-29 20:45:27 +01001587
1588 // Extract information from the dependency. The exact information extracted
1589 // is determined by the nature of the dependency which is determined by the tag.
1590 scopeTag.extractDepInfo(ctx, to, scopePaths)
Paul Duffinb97b1572021-04-29 21:50:40 +01001591
1592 exportedComponents[ctx.OtherModuleName(to)] = struct{}{}
Sundong Ahn20e998b2018-07-24 11:19:26 +09001593 }
Jihoon Kang8479dea2024-04-04 01:19:05 +00001594
1595 if tag == implLibraryTag {
1596 if dep, ok := android.OtherModuleProvider(ctx, to, JavaInfoProvider); ok {
1597 module.implLibraryHeaderJars = append(module.implLibraryHeaderJars, dep.HeaderJars...)
Jihoon Kanga3a05462024-04-05 00:36:44 +00001598 module.implLibraryModule = to.(*Library)
1599 android.SetProvider(ctx, JavaInfoProvider, dep)
Jihoon Kang8479dea2024-04-04 01:19:05 +00001600 }
1601 }
Jiyong Parkc678ad32018-04-10 13:07:10 +09001602 })
Paul Duffinb97b1572021-04-29 21:50:40 +01001603
Jihoon Kanga3a05462024-04-05 00:36:44 +00001604 apexInfo, _ := android.ModuleProvider(ctx, android.ApexInfoProvider)
1605 if !apexInfo.IsForPlatform() {
1606 module.hideApexVariantFromMake = true
1607 }
1608
1609 if module.implLibraryModule != nil {
1610 if ctx.Device() {
1611 module.classesJarPaths = android.Paths{module.implLibraryModule.implementationJarFile}
1612 module.bootDexJarPath = module.implLibraryModule.bootDexJarPath
1613 module.uncompressDexState = module.implLibraryModule.uncompressDexState
1614 module.active = module.implLibraryModule.active
1615 }
1616
1617 module.outputFile = module.implLibraryModule.outputFile
1618 module.dexJarFile = makeDexJarPathFromPath(module.implLibraryModule.dexJarFile.Path())
1619 module.headerJarFile = module.implLibraryModule.headerJarFile
1620 module.implementationAndResourcesJar = module.implLibraryModule.implementationAndResourcesJar
1621 module.builtInstalledForApex = module.implLibraryModule.builtInstalledForApex
1622 module.dexpreopter.configPath = module.implLibraryModule.dexpreopter.configPath
1623 module.dexpreopter.outputProfilePathOnHost = module.implLibraryModule.dexpreopter.outputProfilePathOnHost
1624
Jihoon Kang34155e32024-05-20 19:08:49 +00001625 // Properties required for Library.AndroidMkEntries
1626 module.logtagsSrcs = module.implLibraryModule.logtagsSrcs
1627 module.dexpreopter.builtInstalled = module.implLibraryModule.dexpreopter.builtInstalled
1628 module.jacocoReportClassesFile = module.implLibraryModule.jacocoReportClassesFile
1629 module.dexer.proguardDictionary = module.implLibraryModule.dexer.proguardDictionary
1630 module.dexer.proguardUsageZip = module.implLibraryModule.dexer.proguardUsageZip
1631 module.linter.reports = module.implLibraryModule.linter.reports
Jihoon Kang629e2a32024-06-25 20:47:49 +00001632 module.linter.outputs.depSets = module.implLibraryModule.LintDepSets()
Jihoon Kang34155e32024-05-20 19:08:49 +00001633
Jihoon Kanga3a05462024-04-05 00:36:44 +00001634 if !module.Host() {
1635 module.hostdexInstallFile = module.implLibraryModule.hostdexInstallFile
1636 }
1637
1638 android.SetProvider(ctx, blueprint.SrcsFileProviderKey, blueprint.SrcsFileProviderData{SrcPaths: module.implLibraryModule.uniqueSrcFiles.Strings()})
1639 }
1640
Paul Duffinb97b1572021-04-29 21:50:40 +01001641 // Make the set of components exported by this module available for use elsewhere.
Cole Faust18994c72023-02-28 16:02:16 -08001642 exportedComponentInfo := android.ExportedComponentsInfo{Components: android.SortedKeys(exportedComponents)}
Colin Cross40213022023-12-13 15:19:49 -08001643 android.SetProvider(ctx, android.ExportedComponentsInfoProvider, exportedComponentInfo)
Paul Duffin958806b2022-05-16 13:10:47 +00001644
1645 // Provide additional information for inclusion in an sdk's generated .info file.
1646 additionalSdkInfo := map[string]interface{}{}
1647 additionalSdkInfo["dist_stem"] = module.distStem()
Paul Duffine8409952022-09-22 16:24:46 +01001648 baseModuleName := module.distStem()
Paul Duffin958806b2022-05-16 13:10:47 +00001649 scopes := map[string]interface{}{}
1650 additionalSdkInfo["scopes"] = scopes
1651 for scope, scopePaths := range module.scopePaths {
1652 scopeInfo := map[string]interface{}{}
1653 scopes[scope.name] = scopeInfo
1654 scopeInfo["current_api"] = scope.snapshotRelativeCurrentApiTxtPath(baseModuleName)
1655 scopeInfo["removed_api"] = scope.snapshotRelativeRemovedApiTxtPath(baseModuleName)
Jihoon Kang5623e542024-01-31 23:27:26 +00001656 if p := scopePaths.latestApiPaths; len(p) > 0 {
1657 // The last path in the list is the one that applies to this scope, the
1658 // preceding ones, if any, are for the scope(s) that it extends.
1659 scopeInfo["latest_api"] = p[len(p)-1].String()
Paul Duffin958806b2022-05-16 13:10:47 +00001660 }
Jihoon Kang5623e542024-01-31 23:27:26 +00001661 if p := scopePaths.latestRemovedApiPaths; len(p) > 0 {
1662 // The last path in the list is the one that applies to this scope, the
1663 // preceding ones, if any, are for the scope(s) that it extends.
1664 scopeInfo["latest_removed_api"] = p[len(p)-1].String()
Paul Duffin958806b2022-05-16 13:10:47 +00001665 }
1666 }
Colin Cross40213022023-12-13 15:19:49 -08001667 android.SetProvider(ctx, android.AdditionalSdkInfoProvider, android.AdditionalSdkInfo{additionalSdkInfo})
mrziwang9f7b9f42024-07-10 12:18:06 -07001668 module.setOutputFiles(ctx)
1669 if module.requiresRuntimeImplementationLibrary() && module.implLibraryModule != nil {
1670 setOutputFiles(ctx, module.implLibraryModule.Module)
1671 }
Jiyong Parkc678ad32018-04-10 13:07:10 +09001672}
1673
Jihoon Kanga3a05462024-04-05 00:36:44 +00001674func (module *SdkLibrary) BuiltInstalledForApex() []dexpreopterInstall {
1675 return module.builtInstalledForApex
1676}
1677
Jiyong Park0b0e1b92019-12-03 13:24:29 +09001678func (module *SdkLibrary) AndroidMkEntries() []android.AndroidMkEntries {
Paul Duffindfa131e2020-05-15 20:37:11 +01001679 if !module.requiresRuntimeImplementationLibrary() {
Paul Duffin43db9be2019-12-30 17:35:49 +00001680 return nil
1681 }
Jiyong Park0b0e1b92019-12-03 13:24:29 +09001682 entriesList := module.Library.AndroidMkEntries()
Jihoon Kanga3a05462024-04-05 00:36:44 +00001683 entries := &entriesList[0]
1684 entries.Required = append(entries.Required, module.implLibraryModuleName())
Yo Chiang07d75072020-06-05 17:43:19 +08001685 if module.sharedLibrary() {
Yo Chiang07d75072020-06-05 17:43:19 +08001686 entries.Required = append(entries.Required, module.xmlPermissionsModuleName())
1687 }
Jiyong Park0b0e1b92019-12-03 13:24:29 +09001688 return entriesList
Jiyong Park82484c02018-04-23 21:41:26 +09001689}
1690
Anton Hansson5fd5d242020-03-27 19:43:19 +00001691// The dist path of the stub artifacts
1692func (module *SdkLibrary) apiDistPath(apiScope *apiScope) string {
Colin Crossf0eace92021-06-02 13:02:23 -07001693 return path.Join("apistubs", module.distGroup(), apiScope.name)
Anton Hansson5fd5d242020-03-27 19:43:19 +00001694}
1695
Paul Duffin12ceb462019-12-24 20:31:31 +00001696// Get the sdk version for use when compiling the stubs library.
Paul Duffin780c5f42020-05-12 15:52:55 +01001697func (module *SdkLibrary) sdkVersionForStubsLibrary(mctx android.EarlyModuleContext, apiScope *apiScope) string {
Paul Duffin87a05a32020-05-12 11:50:28 +01001698 scopeProperties := module.scopeToProperties[apiScope]
1699 if scopeProperties.Sdk_version != nil {
1700 return proptools.String(scopeProperties.Sdk_version)
1701 }
1702
Jiyong Parkf1691d22021-03-29 20:11:58 +09001703 sdkDep := decodeSdkDep(mctx, android.SdkContext(&module.Library))
Paul Duffin12ceb462019-12-24 20:31:31 +00001704 if sdkDep.hasStandardLibs() {
1705 // If building against a standard sdk then use the sdk version appropriate for the scope.
Paul Duffind1b3a922020-01-22 11:57:20 +00001706 return apiScope.sdkVersion
Paul Duffin12ceb462019-12-24 20:31:31 +00001707 } else {
1708 // Otherwise, use no system module.
1709 return "none"
1710 }
1711}
1712
Paul Duffin31310252020-11-20 21:26:20 +00001713func (module *SdkLibrary) distStem() string {
1714 return proptools.StringDefault(module.sdkLibraryProperties.Dist_stem, module.BaseModuleName())
1715}
1716
Colin Cross986b69a2021-06-01 13:13:40 -07001717// distGroup returns the subdirectory of the dist path of the stub artifacts.
1718func (module *SdkLibrary) distGroup() string {
Colin Cross59b92bf2021-06-01 14:07:56 -07001719 return proptools.StringDefault(module.sdkLibraryProperties.Dist_group, "unknown")
Colin Cross986b69a2021-06-01 13:13:40 -07001720}
1721
Paul Duffin958806b2022-05-16 13:10:47 +00001722func latestPrebuiltApiModuleName(name string, apiScope *apiScope) string {
1723 return PrebuiltApiModuleName(name, apiScope.name, "latest")
1724}
1725
Jihoon Kang748a24d2024-03-20 21:29:39 +00001726func latestPrebuiltApiCombinedModuleName(name string, apiScope *apiScope) string {
1727 return PrebuiltApiCombinedModuleName(name, apiScope.name, "latest")
1728}
1729
Paul Duffind1b3a922020-01-22 11:57:20 +00001730func (module *SdkLibrary) latestApiFilegroupName(apiScope *apiScope) string {
Paul Duffin958806b2022-05-16 13:10:47 +00001731 return ":" + module.latestApiModuleName(apiScope)
1732}
1733
1734func (module *SdkLibrary) latestApiModuleName(apiScope *apiScope) string {
Jihoon Kang748a24d2024-03-20 21:29:39 +00001735 return latestPrebuiltApiCombinedModuleName(module.distStem(), apiScope)
Jiyong Park58c518b2018-05-12 22:29:12 +09001736}
Jiyong Parkc678ad32018-04-10 13:07:10 +09001737
Paul Duffind1b3a922020-01-22 11:57:20 +00001738func (module *SdkLibrary) latestRemovedApiFilegroupName(apiScope *apiScope) string {
Paul Duffin958806b2022-05-16 13:10:47 +00001739 return ":" + module.latestRemovedApiModuleName(apiScope)
1740}
1741
1742func (module *SdkLibrary) latestRemovedApiModuleName(apiScope *apiScope) string {
Jihoon Kang748a24d2024-03-20 21:29:39 +00001743 return latestPrebuiltApiCombinedModuleName(module.distStem()+"-removed", apiScope)
Jiyong Parkc678ad32018-04-10 13:07:10 +09001744}
1745
Jaewoong Jung1a97ee02021-03-09 13:25:02 -08001746func (module *SdkLibrary) latestIncompatibilitiesFilegroupName(apiScope *apiScope) string {
Paul Duffin958806b2022-05-16 13:10:47 +00001747 return ":" + module.latestIncompatibilitiesModuleName(apiScope)
1748}
1749
1750func (module *SdkLibrary) latestIncompatibilitiesModuleName(apiScope *apiScope) string {
1751 return latestPrebuiltApiModuleName(module.distStem()+"-incompatibilities", apiScope)
Jaewoong Jung1a97ee02021-03-09 13:25:02 -08001752}
1753
Liana Kazanovaa574cd22024-08-05 19:45:03 +00001754func (module *SdkLibrary) contributesToApiSurface(c android.Config) bool {
1755 _, exists := c.GetApiLibraries()[module.Name()]
1756 return exists
1757}
1758
1759// The listed modules are the special java_sdk_libraries where apiScope.kind do not match the
1760// api surface that the module contribute to. For example, the public droidstubs and java_library
1761// do not contribute to the public api surface, but contributes to the core platform api surface.
1762// This method returns the full api surface stub lib that
1763// the generated java_api_library should depend on.
1764func (module *SdkLibrary) alternativeFullApiSurfaceStubLib() string {
1765 if val, ok := apiLibraryAdditionalProperties[module.Name()]; ok {
1766 return val.FullApiSurfaceStubLib
1767 }
1768 return ""
1769}
1770
Jihoon Kang0c705a42023-08-02 06:44:57 +00001771// The listed modules' stubs contents do not match the corresponding txt files,
1772// but require additional api contributions to generate the full stubs.
1773// This method returns the name of the additional api contribution module
1774// for corresponding sdk_library modules.
1775func (module *SdkLibrary) apiLibraryAdditionalApiContribution() string {
1776 if val, ok := apiLibraryAdditionalProperties[module.Name()]; ok {
Liana Kazanovaa574cd22024-08-05 19:45:03 +00001777 return val.AdditionalApiContribution
Jihoon Kang0c705a42023-08-02 06:44:57 +00001778 }
1779 return ""
1780}
1781
Anton Hansson944e77d2020-08-19 11:40:22 +01001782func childModuleVisibility(childVisibility []string) []string {
1783 if childVisibility == nil {
1784 // No child visibility set. The child will use the visibility of the sdk_library.
1785 return nil
1786 }
1787
1788 // Prepend an override to ignore the sdk_library's visibility, and rely on the child visibility.
1789 var visibility []string
1790 visibility = append(visibility, "//visibility:override")
1791 visibility = append(visibility, childVisibility...)
1792 return visibility
1793}
1794
Paul Duffin5df79302020-05-16 15:52:12 +01001795// Creates the implementation java library
1796func (module *SdkLibrary) createImplLibrary(mctx android.DefaultableHookContext) {
Anton Hansson944e77d2020-08-19 11:40:22 +01001797 visibility := childModuleVisibility(module.sdkLibraryProperties.Impl_library_visibility)
1798
Paul Duffin5df79302020-05-16 15:52:12 +01001799 props := struct {
Paul Duffin77590a82022-04-28 14:13:30 +00001800 Name *string
1801 Visibility []string
Paul Duffin77590a82022-04-28 14:13:30 +00001802 Libs []string
1803 Static_libs []string
1804 Apex_available []string
Jihoon Kanga3a05462024-04-05 00:36:44 +00001805 Stem *string
Paul Duffin5df79302020-05-16 15:52:12 +01001806 }{
1807 Name: proptools.StringPtr(module.implLibraryModuleName()),
Anton Hansson944e77d2020-08-19 11:40:22 +01001808 Visibility: visibility,
Jihoon Kanga3a05462024-04-05 00:36:44 +00001809
1810 Libs: append(module.properties.Libs, module.sdkLibraryProperties.Impl_only_libs...),
1811
1812 Static_libs: append(module.properties.Static_libs, module.sdkLibraryProperties.Impl_only_static_libs...),
Paul Duffin77590a82022-04-28 14:13:30 +00001813 // Pass the apex_available settings down so that the impl library can be statically
1814 // embedded within a library that is added to an APEX. Needed for updatable-media.
1815 Apex_available: module.ApexAvailable(),
Jihoon Kanga3a05462024-04-05 00:36:44 +00001816
1817 Stem: proptools.StringPtr(module.Name()),
Paul Duffin5df79302020-05-16 15:52:12 +01001818 }
1819
1820 properties := []interface{}{
1821 &module.properties,
1822 &module.protoProperties,
1823 &module.deviceProperties,
Liz Kammera7a64f32020-07-09 15:16:41 -07001824 &module.dexProperties,
Paul Duffin5df79302020-05-16 15:52:12 +01001825 &module.dexpreoptProperties,
Colin Cross014489c2020-06-02 20:09:13 -07001826 &module.linter.properties,
Spandan Dasb9c58352024-05-13 18:29:45 +00001827 &module.overridableProperties,
Paul Duffin5df79302020-05-16 15:52:12 +01001828 &props,
1829 module.sdkComponentPropertiesForChildLibrary(),
1830 }
1831 mctx.CreateModule(LibraryFactory, properties...)
1832}
1833
Jihoon Kangfa4a90d2023-12-20 02:53:38 +00001834type libraryProperties struct {
1835 Name *string
1836 Visibility []string
1837 Srcs []string
1838 Installable *bool
1839 Sdk_version *string
1840 System_modules *string
1841 Patch_module *string
1842 Libs []string
1843 Static_libs []string
1844 Compile_dex *bool
1845 Java_version *string
1846 Openjdk9 struct {
1847 Srcs []string
1848 Javacflags []string
1849 }
1850 Dist struct {
1851 Targets []string
1852 Dest *string
1853 Dir *string
1854 Tag *string
1855 }
Jihoon Kangfe914ed2024-02-12 22:49:21 +00001856 Is_stubs_module *bool
Jihoon Kangfa4a90d2023-12-20 02:53:38 +00001857}
Jiyong Parkc678ad32018-04-10 13:07:10 +09001858
Jihoon Kangfa4a90d2023-12-20 02:53:38 +00001859func (module *SdkLibrary) stubsLibraryProps(mctx android.DefaultableHookContext, apiScope *apiScope) libraryProperties {
1860 props := libraryProperties{}
Jihoon Kang786df932023-09-07 01:18:31 +00001861 props.Visibility = []string{"//visibility:override", "//visibility:private"}
Jiyong Parkc678ad32018-04-10 13:07:10 +09001862 // sources are generated from the droiddoc
Paul Duffin12ceb462019-12-24 20:31:31 +00001863 sdkVersion := module.sdkVersionForStubsLibrary(mctx, apiScope)
Paul Duffin52d398a2019-06-11 12:31:14 +01001864 props.Sdk_version = proptools.StringPtr(sdkVersion)
Paul Duffina18abc22020-05-16 18:54:24 +01001865 props.System_modules = module.deviceProperties.System_modules
1866 props.Patch_module = module.properties.Patch_module
Paul Duffin367ab912019-12-23 19:40:36 +00001867 props.Installable = proptools.BoolPtr(false)
Sundong Ahn054b19a2018-10-19 13:46:09 +09001868 props.Libs = module.sdkLibraryProperties.Stub_only_libs
Mark White9421c4c2023-08-10 00:07:03 +00001869 props.Libs = append(props.Libs, module.scopeToProperties[apiScope].Libs...)
Anton Hanssondae54cd2021-04-21 16:30:10 +01001870 props.Static_libs = module.sdkLibraryProperties.Stub_only_static_libs
Paul Duffine22c2ab2020-05-20 19:35:27 +01001871 // The stub-annotations library contains special versions of the annotations
1872 // with CLASS retention policy, so that they're kept.
1873 if proptools.Bool(module.sdkLibraryProperties.Annotations_enabled) {
1874 props.Libs = append(props.Libs, "stub-annotations")
1875 }
Paul Duffina18abc22020-05-16 18:54:24 +01001876 props.Openjdk9.Srcs = module.properties.Openjdk9.Srcs
1877 props.Openjdk9.Javacflags = module.properties.Openjdk9.Javacflags
Anton Hansson83509b52020-05-21 09:21:57 +01001878 // We compile the stubs for 1.8 in line with the main android.jar stubs, and potential
1879 // interop with older developer tools that don't support 1.9.
1880 props.Java_version = proptools.StringPtr("1.8")
Jihoon Kangfe914ed2024-02-12 22:49:21 +00001881 props.Is_stubs_module = proptools.BoolPtr(true)
Paul Duffinf4600f62021-05-13 22:34:45 +01001882
Jihoon Kangfa4a90d2023-12-20 02:53:38 +00001883 return props
1884}
1885
1886// Creates a static java library that has API stubs
1887func (module *SdkLibrary) createStubsLibrary(mctx android.DefaultableHookContext, apiScope *apiScope) {
1888
1889 props := module.stubsLibraryProps(mctx, apiScope)
1890 props.Name = proptools.StringPtr(module.sourceStubsLibraryModuleName(apiScope))
1891 props.Srcs = []string{":" + module.stubsSourceModuleName(apiScope)}
1892
1893 mctx.CreateModule(LibraryFactory, &props, module.sdkComponentPropertiesForChildLibrary())
1894}
1895
1896// Create a static java library that compiles the "exportable" stubs
1897func (module *SdkLibrary) createExportableStubsLibrary(mctx android.DefaultableHookContext, apiScope *apiScope) {
1898 props := module.stubsLibraryProps(mctx, apiScope)
1899 props.Name = proptools.StringPtr(module.exportableSourceStubsLibraryModuleName(apiScope))
1900 props.Srcs = []string{":" + module.stubsSourceModuleName(apiScope) + "{.exportable}"}
1901
Paul Duffin859fe962020-05-15 10:20:31 +01001902 mctx.CreateModule(LibraryFactory, &props, module.sdkComponentPropertiesForChildLibrary())
Jiyong Parkc678ad32018-04-10 13:07:10 +09001903}
1904
Paul Duffin6d0886e2020-04-07 18:49:53 +01001905// Creates a droidstubs module that creates stubs source files from the given full source
Paul Duffinc8782502020-04-29 20:45:27 +01001906// files and also updates and checks the API specification files.
Paul Duffin15f34ef2020-07-20 18:04:44 +01001907func (module *SdkLibrary) createStubsSourcesAndApi(mctx android.DefaultableHookContext, apiScope *apiScope, name string, scopeSpecificDroidstubsArgs []string) {
Jiyong Parkc678ad32018-04-10 13:07:10 +09001908 props := struct {
Sundong Ahn054b19a2018-10-19 13:46:09 +09001909 Name *string
Paul Duffin4911a892020-04-29 23:35:13 +01001910 Visibility []string
Sundong Ahn054b19a2018-10-19 13:46:09 +09001911 Srcs []string
1912 Installable *bool
Paul Duffin52d398a2019-06-11 12:31:14 +01001913 Sdk_version *string
Jihoon Kang3198f3c2023-01-26 08:08:52 +00001914 Api_surface *string
Paul Duffin12ceb462019-12-24 20:31:31 +00001915 System_modules *string
Sundong Ahn054b19a2018-10-19 13:46:09 +09001916 Libs []string
Paul Duffin6877e6d2020-09-25 19:59:14 +01001917 Output_javadoc_comments *bool
Paul Duffin11512472019-02-11 15:55:17 +00001918 Arg_files []string
Sundong Ahn054b19a2018-10-19 13:46:09 +09001919 Args *string
Sundong Ahn054b19a2018-10-19 13:46:09 +09001920 Java_version *string
Paul Duffine22c2ab2020-05-20 19:35:27 +01001921 Annotations_enabled *bool
Sundong Ahn054b19a2018-10-19 13:46:09 +09001922 Merge_annotations_dirs []string
1923 Merge_inclusion_annotations_dirs []string
Paul Duffin0ff08bd2020-04-29 13:30:54 +01001924 Generate_stubs *bool
Anton Hanssone87b03d2020-12-21 15:29:34 +00001925 Previous_api *string
Jihoon Kang6592e872023-12-19 01:13:16 +00001926 Aconfig_declarations []string
Sundong Ahn054b19a2018-10-19 13:46:09 +09001927 Check_api struct {
Anton Hanssone6056152020-12-31 10:37:27 +00001928 Current ApiToCheck
1929 Last_released ApiToCheck
Paul Duffin160fe412020-05-10 19:32:20 +01001930
1931 Api_lint struct {
1932 Enabled *bool
1933 New_since *string
1934 Baseline_file *string
1935 }
Jiyong Park58c518b2018-05-12 22:29:12 +09001936 }
Sundong Ahn1b92c822018-05-29 11:35:17 +09001937 Aidl struct {
1938 Include_dirs []string
1939 Local_include_dirs []string
1940 }
Paul Duffin040e9062020-11-23 17:41:36 +00001941 Dists []android.Dist
Jiyong Parkc678ad32018-04-10 13:07:10 +09001942 }{}
1943
Paul Duffin7b78b4d2020-04-28 14:08:32 +01001944 // The stubs source processing uses the same compile time classpath when extracting the
1945 // API from the implementation library as it does when compiling it. i.e. the same
1946 // * sdk version
1947 // * system_modules
1948 // * libs (static_libs/libs)
Paul Duffin250e6192019-06-07 10:44:37 +01001949
Paul Duffin0ff08bd2020-04-29 13:30:54 +01001950 props.Name = proptools.StringPtr(name)
Anton Hansson944e77d2020-08-19 11:40:22 +01001951 props.Visibility = childModuleVisibility(module.sdkLibraryProperties.Stubs_source_visibility)
Paul Duffina18abc22020-05-16 18:54:24 +01001952 props.Srcs = append(props.Srcs, module.properties.Srcs...)
Anton Hanssonf8ea3722021-09-16 14:24:13 +01001953 props.Srcs = append(props.Srcs, module.sdkLibraryProperties.Api_srcs...)
Paul Duffina18abc22020-05-16 18:54:24 +01001954 props.Sdk_version = module.deviceProperties.Sdk_version
Jihoon Kang3198f3c2023-01-26 08:08:52 +00001955 props.Api_surface = &apiScope.name
Paul Duffina18abc22020-05-16 18:54:24 +01001956 props.System_modules = module.deviceProperties.System_modules
Jiyong Parkc678ad32018-04-10 13:07:10 +09001957 props.Installable = proptools.BoolPtr(false)
Sundong Ahne6f0b052018-06-05 16:46:14 +09001958 // A droiddoc module has only one Libs property and doesn't distinguish between
1959 // shared libs and static libs. So we need to add both of these libs to Libs property.
Paul Duffina18abc22020-05-16 18:54:24 +01001960 props.Libs = module.properties.Libs
1961 props.Libs = append(props.Libs, module.properties.Static_libs...)
Nikita Ioffed732da72022-11-21 12:38:25 +00001962 props.Libs = append(props.Libs, module.sdkLibraryProperties.Stub_only_libs...)
Mark White9421c4c2023-08-10 00:07:03 +00001963 props.Libs = append(props.Libs, module.scopeToProperties[apiScope].Libs...)
Paul Duffina18abc22020-05-16 18:54:24 +01001964 props.Aidl.Include_dirs = module.deviceProperties.Aidl.Include_dirs
1965 props.Aidl.Local_include_dirs = module.deviceProperties.Aidl.Local_include_dirs
1966 props.Java_version = module.properties.Java_version
Jiyong Parkc678ad32018-04-10 13:07:10 +09001967
Paul Duffine22c2ab2020-05-20 19:35:27 +01001968 props.Annotations_enabled = module.sdkLibraryProperties.Annotations_enabled
Sundong Ahn054b19a2018-10-19 13:46:09 +09001969 props.Merge_annotations_dirs = module.sdkLibraryProperties.Merge_annotations_dirs
1970 props.Merge_inclusion_annotations_dirs = module.sdkLibraryProperties.Merge_inclusion_annotations_dirs
Jihoon Kang6592e872023-12-19 01:13:16 +00001971 props.Aconfig_declarations = module.sdkLibraryProperties.Aconfig_declarations
Sundong Ahn054b19a2018-10-19 13:46:09 +09001972
Paul Duffin6d0886e2020-04-07 18:49:53 +01001973 droidstubsArgs := []string{}
Paul Duffin235ffff2019-12-24 10:41:30 +00001974 if len(module.sdkLibraryProperties.Api_packages) != 0 {
Paul Duffin6d0886e2020-04-07 18:49:53 +01001975 droidstubsArgs = append(droidstubsArgs, "--stub-packages "+strings.Join(module.sdkLibraryProperties.Api_packages, ":"))
Paul Duffin235ffff2019-12-24 10:41:30 +00001976 }
Paul Duffin6d0886e2020-04-07 18:49:53 +01001977 droidstubsArgs = append(droidstubsArgs, module.sdkLibraryProperties.Droiddoc_options...)
Anton Hanssonfd1c0d22023-11-02 15:18:09 +00001978 disabledWarnings := []string{"HiddenSuperclass"}
1979 if proptools.BoolDefault(module.sdkLibraryProperties.Api_lint.Legacy_errors_allowed, true) {
1980 disabledWarnings = append(disabledWarnings,
1981 "BroadcastBehavior",
1982 "DeprecationMismatch",
1983 "MissingPermission",
1984 "SdkConstant",
1985 "Todo",
1986 )
Paul Duffin235ffff2019-12-24 10:41:30 +00001987 }
Paul Duffin6d0886e2020-04-07 18:49:53 +01001988 droidstubsArgs = append(droidstubsArgs, android.JoinWithPrefix(disabledWarnings, "--hide "))
Sundong Ahnfb2721f2018-09-17 13:23:09 +09001989
Paul Duffin6877e6d2020-09-25 19:59:14 +01001990 // Output Javadoc comments for public scope.
1991 if apiScope == apiScopePublic {
1992 props.Output_javadoc_comments = proptools.BoolPtr(true)
1993 }
1994
Paul Duffin1fb487d2020-04-07 18:50:10 +01001995 // Add in scope specific arguments.
Paul Duffin0ff08bd2020-04-29 13:30:54 +01001996 droidstubsArgs = append(droidstubsArgs, scopeSpecificDroidstubsArgs...)
Paul Duffin11512472019-02-11 15:55:17 +00001997 props.Arg_files = module.sdkLibraryProperties.Droiddoc_option_files
Paul Duffin6d0886e2020-04-07 18:49:53 +01001998 props.Args = proptools.StringPtr(strings.Join(droidstubsArgs, " "))
Jiyong Parkc678ad32018-04-10 13:07:10 +09001999
Paul Duffin15f34ef2020-07-20 18:04:44 +01002000 // List of APIs identified from the provided source files are created. They are later
2001 // compared against to the not-yet-released (a.k.a current) list of APIs and to the
2002 // last-released (a.k.a numbered) list of API.
2003 currentApiFileName := apiScope.apiFilePrefix + "current.txt"
2004 removedApiFileName := apiScope.apiFilePrefix + "removed.txt"
2005 apiDir := module.getApiDir()
2006 currentApiFileName = path.Join(apiDir, currentApiFileName)
2007 removedApiFileName = path.Join(apiDir, removedApiFileName)
Jiyong Parkc678ad32018-04-10 13:07:10 +09002008
Paul Duffin15f34ef2020-07-20 18:04:44 +01002009 // check against the not-yet-release API
2010 props.Check_api.Current.Api_file = proptools.StringPtr(currentApiFileName)
2011 props.Check_api.Current.Removed_api_file = proptools.StringPtr(removedApiFileName)
Jiyong Park58c518b2018-05-12 22:29:12 +09002012
Paul Duffin958806b2022-05-16 13:10:47 +00002013 if module.compareAgainstLatestApi(apiScope) {
Paul Duffin15f34ef2020-07-20 18:04:44 +01002014 // check against the latest released API
2015 latestApiFilegroupName := proptools.StringPtr(module.latestApiFilegroupName(apiScope))
Anton Hanssone87b03d2020-12-21 15:29:34 +00002016 props.Previous_api = latestApiFilegroupName
Paul Duffin15f34ef2020-07-20 18:04:44 +01002017 props.Check_api.Last_released.Api_file = latestApiFilegroupName
2018 props.Check_api.Last_released.Removed_api_file = proptools.StringPtr(
2019 module.latestRemovedApiFilegroupName(apiScope))
Jaewoong Jung1a97ee02021-03-09 13:25:02 -08002020 props.Check_api.Last_released.Baseline_file = proptools.StringPtr(
2021 module.latestIncompatibilitiesFilegroupName(apiScope))
Paul Duffin160fe412020-05-10 19:32:20 +01002022
Paul Duffin15f34ef2020-07-20 18:04:44 +01002023 if proptools.Bool(module.sdkLibraryProperties.Api_lint.Enabled) {
2024 // Enable api lint.
2025 props.Check_api.Api_lint.Enabled = proptools.BoolPtr(true)
2026 props.Check_api.Api_lint.New_since = latestApiFilegroupName
Paul Duffin160fe412020-05-10 19:32:20 +01002027
Paul Duffin15f34ef2020-07-20 18:04:44 +01002028 // If it exists then pass a lint-baseline.txt through to droidstubs.
2029 baselinePath := path.Join(apiDir, apiScope.apiFilePrefix+"lint-baseline.txt")
2030 baselinePathRelativeToRoot := path.Join(mctx.ModuleDir(), baselinePath)
2031 paths, err := mctx.GlobWithDeps(baselinePathRelativeToRoot, nil)
2032 if err != nil {
2033 mctx.ModuleErrorf("error checking for presence of %s: %s", baselinePathRelativeToRoot, err)
2034 }
2035 if len(paths) == 1 {
2036 props.Check_api.Api_lint.Baseline_file = proptools.StringPtr(baselinePath)
2037 } else if len(paths) != 0 {
2038 mctx.ModuleErrorf("error checking for presence of %s: expected one path, found: %v", baselinePathRelativeToRoot, paths)
Paul Duffin160fe412020-05-10 19:32:20 +01002039 }
2040 }
Paul Duffin15f34ef2020-07-20 18:04:44 +01002041 }
Jiyong Park58c518b2018-05-12 22:29:12 +09002042
Paul Duffin15f34ef2020-07-20 18:04:44 +01002043 if !Bool(module.sdkLibraryProperties.No_dist) {
Paul Duffin040e9062020-11-23 17:41:36 +00002044 // Dist the api txt and removed api txt artifacts for sdk builds.
2045 distDir := proptools.StringPtr(path.Join(module.apiDistPath(apiScope), "api"))
Jihoon Kang02168052024-03-20 00:44:54 +00002046 stubsTypeTagPrefix := ""
2047 if mctx.Config().ReleaseHiddenApiExportableStubs() {
2048 stubsTypeTagPrefix = ".exportable"
2049 }
Paul Duffin040e9062020-11-23 17:41:36 +00002050 for _, p := range []struct {
2051 tag string
2052 pattern string
2053 }{
Jihoon Kangd1799f62024-02-20 23:01:38 +00002054 // "exportable" api files are copied to the dist directory instead of the
Jihoon Kang02168052024-03-20 00:44:54 +00002055 // "everything" api files when "RELEASE_HIDDEN_API_EXPORTABLE_STUBS" build flag
2056 // is set. Otherwise, the "everything" api files are copied to the dist directory.
2057 {tag: "%s.api.txt", pattern: "%s.txt"},
2058 {tag: "%s.removed-api.txt", pattern: "%s-removed.txt"},
Paul Duffin040e9062020-11-23 17:41:36 +00002059 } {
2060 props.Dists = append(props.Dists, android.Dist{
2061 Targets: []string{"sdk", "win_sdk"},
2062 Dir: distDir,
2063 Dest: proptools.StringPtr(fmt.Sprintf(p.pattern, module.distStem())),
Jihoon Kang02168052024-03-20 00:44:54 +00002064 Tag: proptools.StringPtr(fmt.Sprintf(p.tag, stubsTypeTagPrefix)),
Paul Duffin040e9062020-11-23 17:41:36 +00002065 })
2066 }
Anton Hansson5fd5d242020-03-27 19:43:19 +00002067 }
2068
Spandan Das2cc80ba2023-10-27 17:21:52 +00002069 mctx.CreateModule(DroidstubsFactory, &props, module.sdkComponentPropertiesForChildLibrary()).(*Droidstubs).CallHookIfAvailable(mctx)
Jiyong Parkc678ad32018-04-10 13:07:10 +09002070}
2071
Liana Kazanovaa574cd22024-08-05 19:45:03 +00002072func (module *SdkLibrary) createApiLibrary(mctx android.DefaultableHookContext, apiScope *apiScope, alternativeFullApiSurfaceStub string) {
Jihoon Kang1c92c3e2023-03-23 17:44:51 +00002073 props := struct {
Liana Kazanovaa574cd22024-08-05 19:45:03 +00002074 Name *string
2075 Visibility []string
2076 Api_contributions []string
2077 Libs []string
2078 Static_libs []string
2079 Full_api_surface_stub *string
2080 System_modules *string
2081 Enable_validation *bool
2082 Stubs_type *string
Jihoon Kang1c92c3e2023-03-23 17:44:51 +00002083 }{}
2084
2085 props.Name = proptools.StringPtr(module.apiLibraryModuleName(apiScope))
Jihoon Kang786df932023-09-07 01:18:31 +00002086 props.Visibility = []string{"//visibility:override", "//visibility:private"}
Jihoon Kang1c92c3e2023-03-23 17:44:51 +00002087
2088 apiContributions := []string{}
2089
2090 // Api surfaces are not independent of each other, but have subset relationships,
2091 // and so does the api files. To generate from-text stubs for api surfaces other than public,
2092 // all subset api domains' api_contriubtions must be added as well.
2093 scope := apiScope
2094 for scope != nil {
2095 apiContributions = append(apiContributions, module.stubsSourceModuleName(scope)+".api.contribution")
2096 scope = scope.extends
2097 }
Jihoon Kang0c705a42023-08-02 06:44:57 +00002098 if apiScope == apiScopePublic {
2099 additionalApiContribution := module.apiLibraryAdditionalApiContribution()
2100 if additionalApiContribution != "" {
2101 apiContributions = append(apiContributions, additionalApiContribution)
2102 }
2103 }
Jihoon Kang1c92c3e2023-03-23 17:44:51 +00002104
2105 props.Api_contributions = apiContributions
Liana Kazanovaa574cd22024-08-05 19:45:03 +00002106 props.Libs = module.properties.Libs
Jihoon Kang1c92c3e2023-03-23 17:44:51 +00002107 props.Libs = append(props.Libs, module.sdkLibraryProperties.Stub_only_libs...)
Mark White9421c4c2023-08-10 00:07:03 +00002108 props.Libs = append(props.Libs, module.scopeToProperties[apiScope].Libs...)
Liana Kazanovaa574cd22024-08-05 19:45:03 +00002109 props.Libs = append(props.Libs, "stub-annotations")
Jihoon Kang1c92c3e2023-03-23 17:44:51 +00002110 props.Static_libs = module.sdkLibraryProperties.Stub_only_static_libs
Liana Kazanovaa574cd22024-08-05 19:45:03 +00002111 props.Full_api_surface_stub = proptools.StringPtr(apiScope.kind.DefaultJavaLibraryName())
2112 if alternativeFullApiSurfaceStub != "" {
2113 props.Full_api_surface_stub = proptools.StringPtr(alternativeFullApiSurfaceStub)
2114 }
2115
2116 // android_module_lib_stubs_current.from-text only comprises api contributions from art, conscrypt and i18n.
2117 // Thus, replace with android_module_lib_stubs_current_full.from-text, which comprises every api domains.
2118 if apiScope.kind == android.SdkModule {
2119 props.Full_api_surface_stub = proptools.StringPtr(apiScope.kind.DefaultJavaLibraryName() + "_full.from-text")
2120 }
2121
2122 // java_sdk_library modules that set sdk_version as none does not depend on other api
2123 // domains. Therefore, java_api_library created from such modules should not depend on
2124 // full_api_surface_stubs but create and compile stubs by the java_api_library module
2125 // itself.
2126 if module.SdkVersion(mctx).Kind == android.SdkNone {
2127 props.Full_api_surface_stub = nil
2128 }
Jihoon Kangd30ac8a2023-10-09 18:00:17 +00002129
Jihoon Kang4ec24872023-10-05 17:26:09 +00002130 props.System_modules = module.deviceProperties.System_modules
Jihoon Kang063ec002023-06-28 01:16:23 +00002131 props.Enable_validation = proptools.BoolPtr(true)
Jihoon Kang5d701272024-02-15 21:53:49 +00002132 props.Stubs_type = proptools.StringPtr("everything")
Jihoon Kang4ec24872023-10-05 17:26:09 +00002133
Spandan Das2cc80ba2023-10-27 17:21:52 +00002134 mctx.CreateModule(ApiLibraryFactory, &props, module.sdkComponentPropertiesForChildLibrary())
Jihoon Kang1c92c3e2023-03-23 17:44:51 +00002135}
2136
Jihoon Kang02168052024-03-20 00:44:54 +00002137func (module *SdkLibrary) topLevelStubsLibraryProps(mctx android.DefaultableHookContext, apiScope *apiScope, doDist bool) libraryProperties {
Jihoon Kangfa4a90d2023-12-20 02:53:38 +00002138 props := libraryProperties{}
2139
Jihoon Kang1147b312023-06-08 23:25:57 +00002140 props.Visibility = childModuleVisibility(module.sdkLibraryProperties.Stubs_library_visibility)
2141 sdkVersion := module.sdkVersionForStubsLibrary(mctx, apiScope)
2142 props.Sdk_version = proptools.StringPtr(sdkVersion)
2143
Jihoon Kang1147b312023-06-08 23:25:57 +00002144 props.System_modules = module.deviceProperties.System_modules
2145
Jihoon Kang1147b312023-06-08 23:25:57 +00002146 // The imports need to be compiled to dex if the java_sdk_library requests it.
2147 compileDex := module.dexProperties.Compile_dex
2148 if module.stubLibrariesCompiledForDex() {
2149 compileDex = proptools.BoolPtr(true)
2150 }
2151 props.Compile_dex = compileDex
2152
Jihoon Kang02168052024-03-20 00:44:54 +00002153 if !Bool(module.sdkLibraryProperties.No_dist) && doDist {
2154 props.Dist.Targets = []string{"sdk", "win_sdk"}
2155 props.Dist.Dest = proptools.StringPtr(fmt.Sprintf("%v.jar", module.distStem()))
2156 props.Dist.Dir = proptools.StringPtr(module.apiDistPath(apiScope))
2157 props.Dist.Tag = proptools.StringPtr(".jar")
2158 }
2159
Jihoon Kangfa4a90d2023-12-20 02:53:38 +00002160 return props
2161}
2162
2163func (module *SdkLibrary) createTopLevelStubsLibrary(
Liana Kazanovaa574cd22024-08-05 19:45:03 +00002164 mctx android.DefaultableHookContext, apiScope *apiScope, contributesToApiSurface bool) {
Jihoon Kangfa4a90d2023-12-20 02:53:38 +00002165
Jihoon Kang02168052024-03-20 00:44:54 +00002166 // Dist the "everything" stubs when the RELEASE_HIDDEN_API_EXPORTABLE_STUBS build flag is false
2167 doDist := !mctx.Config().ReleaseHiddenApiExportableStubs()
2168 props := module.topLevelStubsLibraryProps(mctx, apiScope, doDist)
Jihoon Kangfa4a90d2023-12-20 02:53:38 +00002169 props.Name = proptools.StringPtr(module.stubsLibraryModuleName(apiScope))
2170
2171 // Add the stub compiling java_library/java_api_library as static lib based on build config
2172 staticLib := module.sourceStubsLibraryModuleName(apiScope)
Liana Kazanovaa574cd22024-08-05 19:45:03 +00002173 if mctx.Config().BuildFromTextStub() && contributesToApiSurface {
Jihoon Kangfa4a90d2023-12-20 02:53:38 +00002174 staticLib = module.apiLibraryModuleName(apiScope)
2175 }
2176 props.Static_libs = append(props.Static_libs, staticLib)
2177
2178 mctx.CreateModule(LibraryFactory, &props, module.sdkComponentPropertiesForChildLibrary())
2179}
2180
2181func (module *SdkLibrary) createTopLevelExportableStubsLibrary(
2182 mctx android.DefaultableHookContext, apiScope *apiScope) {
2183
Jihoon Kang02168052024-03-20 00:44:54 +00002184 // Dist the "exportable" stubs when the RELEASE_HIDDEN_API_EXPORTABLE_STUBS build flag is true
2185 doDist := mctx.Config().ReleaseHiddenApiExportableStubs()
2186 props := module.topLevelStubsLibraryProps(mctx, apiScope, doDist)
Jihoon Kangfa4a90d2023-12-20 02:53:38 +00002187 props.Name = proptools.StringPtr(module.exportableStubsLibraryModuleName(apiScope))
2188
Jihoon Kangfa4a90d2023-12-20 02:53:38 +00002189 staticLib := module.exportableSourceStubsLibraryModuleName(apiScope)
2190 props.Static_libs = append(props.Static_libs, staticLib)
2191
Jihoon Kang1147b312023-06-08 23:25:57 +00002192 mctx.CreateModule(LibraryFactory, &props, module.sdkComponentPropertiesForChildLibrary())
2193}
2194
Paul Duffin958806b2022-05-16 13:10:47 +00002195func (module *SdkLibrary) compareAgainstLatestApi(apiScope *apiScope) bool {
2196 return !(apiScope.unstable || module.sdkLibraryProperties.Unsafe_ignore_missing_latest_api)
2197}
2198
Paul Duffinea8f8082021-06-24 13:25:57 +01002199// Implements android.ApexModule
Jooyung Han5e9013b2020-03-10 06:23:13 +09002200func (module *SdkLibrary) DepIsInSameApex(mctx android.BaseModuleContext, dep android.Module) bool {
2201 depTag := mctx.OtherModuleDependencyTag(dep)
2202 if depTag == xmlPermissionsFileTag {
2203 return true
2204 }
Jihoon Kanga3a05462024-04-05 00:36:44 +00002205 if dep.Name() == module.implLibraryModuleName() {
2206 return true
2207 }
Jooyung Han5e9013b2020-03-10 06:23:13 +09002208 return module.Library.DepIsInSameApex(mctx, dep)
2209}
2210
Paul Duffinea8f8082021-06-24 13:25:57 +01002211// Implements android.ApexModule
2212func (module *SdkLibrary) UniqueApexVariations() bool {
2213 return module.uniqueApexVariations()
2214}
2215
Liana Kazanovaa574cd22024-08-05 19:45:03 +00002216func (module *SdkLibrary) ContributeToApi() bool {
2217 return proptools.BoolDefault(module.sdkLibraryProperties.Contribute_to_android_api, false)
Jihoon Kang80456fd2023-11-15 19:22:14 +00002218}
2219
Jiyong Parkc678ad32018-04-10 13:07:10 +09002220// Creates the xml file that publicizes the runtime library
Paul Duffinf0229202020-04-29 16:47:28 +01002221func (module *SdkLibrary) createXmlFile(mctx android.DefaultableHookContext) {
Spandan Das8c9ae7e2023-03-03 21:20:36 +00002222 moduleMinApiLevel := module.Library.MinSdkVersion(mctx)
Pedro Loureiroc3621422021-09-28 15:40:23 +00002223 var moduleMinApiLevelStr = moduleMinApiLevel.String()
2224 if moduleMinApiLevel == android.NoneApiLevel {
2225 moduleMinApiLevelStr = "current"
2226 }
Jiyong Parke3833882020-02-17 17:28:10 +09002227 props := struct {
Pedro Loureiroc3621422021-09-28 15:40:23 +00002228 Name *string
2229 Lib_name *string
2230 Apex_available []string
2231 On_bootclasspath_since *string
2232 On_bootclasspath_before *string
2233 Min_device_sdk *string
2234 Max_device_sdk *string
2235 Sdk_library_min_api_level *string
Jamie Garsidee570ace2023-11-27 12:07:36 +00002236 Uses_libs_dependencies []string
Jiyong Parke3833882020-02-17 17:28:10 +09002237 }{
Pedro Loureiroc3621422021-09-28 15:40:23 +00002238 Name: proptools.StringPtr(module.xmlPermissionsModuleName()),
2239 Lib_name: proptools.StringPtr(module.BaseModuleName()),
2240 Apex_available: module.ApexProperties.Apex_available,
2241 On_bootclasspath_since: module.commonSdkLibraryProperties.On_bootclasspath_since,
2242 On_bootclasspath_before: module.commonSdkLibraryProperties.On_bootclasspath_before,
2243 Min_device_sdk: module.commonSdkLibraryProperties.Min_device_sdk,
2244 Max_device_sdk: module.commonSdkLibraryProperties.Max_device_sdk,
2245 Sdk_library_min_api_level: &moduleMinApiLevelStr,
Jamie Garsidee570ace2023-11-27 12:07:36 +00002246 Uses_libs_dependencies: module.usesLibraryProperties.Uses_libs,
Jiyong Parkc678ad32018-04-10 13:07:10 +09002247 }
Jiyong Parke3833882020-02-17 17:28:10 +09002248
Jiyong Parke3833882020-02-17 17:28:10 +09002249 mctx.CreateModule(sdkLibraryXmlFactory, &props)
Jiyong Parkc678ad32018-04-10 13:07:10 +09002250}
2251
Jiyong Parkf1691d22021-03-29 20:11:58 +09002252func PrebuiltJars(ctx android.BaseModuleContext, baseName string, s android.SdkSpec) android.Paths {
Jiyong Park54105c42021-03-31 18:17:53 +09002253 var ver android.ApiLevel
Jiyong Parkf1691d22021-03-29 20:11:58 +09002254 var kind android.SdkKind
2255 if s.UsePrebuilt(ctx) {
Jiyong Park54105c42021-03-31 18:17:53 +09002256 ver = s.ApiLevel
Jiyong Parkf1691d22021-03-29 20:11:58 +09002257 kind = s.Kind
Jiyong Parkc678ad32018-04-10 13:07:10 +09002258 } else {
Jiyong Park6a927c42020-01-21 02:03:43 +09002259 // We don't have prebuilt SDK for the specific sdkVersion.
2260 // Instead of breaking the build, fallback to use "system_current"
Jiyong Park54105c42021-03-31 18:17:53 +09002261 ver = android.FutureApiLevel
Jiyong Parkf1691d22021-03-29 20:11:58 +09002262 kind = android.SdkSystem
Sundong Ahn054b19a2018-10-19 13:46:09 +09002263 }
Jiyong Park6a927c42020-01-21 02:03:43 +09002264
2265 dir := filepath.Join("prebuilts", "sdk", ver.String(), kind.String())
Paul Duffin50061512020-01-21 16:31:05 +00002266 jar := filepath.Join(dir, baseName+".jar")
Sundong Ahn054b19a2018-10-19 13:46:09 +09002267 jarPath := android.ExistentPathForSource(ctx, jar)
Sundong Ahnae418ac2019-02-28 15:01:28 +09002268 if !jarPath.Valid() {
Colin Cross07c88562020-01-07 09:34:44 -08002269 if ctx.Config().AllowMissingDependencies() {
2270 return android.Paths{android.PathForSource(ctx, jar)}
2271 } else {
Jiyong Parkf1691d22021-03-29 20:11:58 +09002272 ctx.PropertyErrorf("sdk_library", "invalid sdk version %q, %q does not exist", s.Raw, jar)
Colin Cross07c88562020-01-07 09:34:44 -08002273 }
Sundong Ahnae418ac2019-02-28 15:01:28 +09002274 return nil
2275 }
Sundong Ahn054b19a2018-10-19 13:46:09 +09002276 return android.Paths{jarPath.Path()}
2277}
2278
Colin Crossaede88c2020-08-11 12:17:01 -07002279// 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 +01002280//
2281// If either this or the other module are on the platform then this will return
2282// false.
Colin Cross56a83212020-09-15 18:30:11 -07002283func withinSameApexesAs(ctx android.BaseModuleContext, other android.Module) bool {
Colin Crossff694a82023-12-13 15:54:49 -08002284 apexInfo, _ := android.ModuleProvider(ctx, android.ApexInfoProvider)
Colin Cross313aa542023-12-13 13:47:44 -08002285 otherApexInfo, _ := android.OtherModuleProvider(ctx, other, android.ApexInfoProvider)
Jiyong Parkab50b072021-05-12 17:13:56 +09002286 return len(otherApexInfo.InApexVariants) > 0 && reflect.DeepEqual(apexInfo.InApexVariants, otherApexInfo.InApexVariants)
Paul Duffin9b879592020-05-26 13:21:35 +01002287}
2288
Jihoon Kang8479dea2024-04-04 01:19:05 +00002289func (module *SdkLibrary) sdkJars(ctx android.BaseModuleContext, sdkVersion android.SdkSpec) android.Paths {
Jiyong Park932cdfe2020-05-28 00:19:53 +09002290 // If the client doesn't set sdk_version, but if this library prefers stubs over
2291 // the impl library, let's provide the widest API surface possible. To do so,
2292 // force override sdk_version to module_current so that the closest possible API
2293 // surface could be found in selectHeaderJarsForSdkVersion
Jiyong Parkf1691d22021-03-29 20:11:58 +09002294 if module.defaultsToStubs() && !sdkVersion.Specified() {
Jiyong Park92315372021-04-02 08:45:46 +09002295 sdkVersion = android.SdkSpecFrom(ctx, "module_current")
Jiyong Park932cdfe2020-05-28 00:19:53 +09002296 }
Paul Duffind1b3a922020-01-22 11:57:20 +00002297
Paul Duffindaaa3322020-05-26 18:13:57 +01002298 // Only provide access to the implementation library if it is actually built.
2299 if module.requiresRuntimeImplementationLibrary() {
2300 // Check any special cases for java_sdk_library.
2301 //
2302 // Only allow access to the implementation library in the following condition:
2303 // * No sdk_version specified on the referencing module.
Paul Duffin9b879592020-05-26 13:21:35 +01002304 // * The referencing module is in the same apex as this.
Jiyong Parkf1691d22021-03-29 20:11:58 +09002305 if sdkVersion.Kind == android.SdkPrivate || withinSameApexesAs(ctx, module) {
Jihoon Kang8479dea2024-04-04 01:19:05 +00002306 return module.implLibraryHeaderJars
Sundong Ahn054b19a2018-10-19 13:46:09 +09002307 }
Jiyong Parkc678ad32018-04-10 13:07:10 +09002308 }
Paul Duffinb05d4292020-05-20 12:19:10 +01002309
Paul Duffin23970f42020-05-20 14:20:02 +01002310 return module.selectHeaderJarsForSdkVersion(ctx, sdkVersion)
Jiyong Parkc678ad32018-04-10 13:07:10 +09002311}
2312
Sundong Ahn241cd372018-07-13 16:16:44 +09002313// to satisfy SdkLibraryDependency interface
Jiyong Parkf1691d22021-03-29 20:11:58 +09002314func (module *SdkLibrary) SdkHeaderJars(ctx android.BaseModuleContext, sdkVersion android.SdkSpec) android.Paths {
Jihoon Kang8479dea2024-04-04 01:19:05 +00002315 return module.sdkJars(ctx, sdkVersion)
Sundong Ahn241cd372018-07-13 16:16:44 +09002316}
2317
Colin Cross571cccf2019-02-04 11:22:08 -08002318var javaSdkLibrariesKey = android.NewOnceKey("javaSdkLibraries")
2319
Jiyong Park82484c02018-04-23 21:41:26 +09002320func javaSdkLibraries(config android.Config) *[]string {
Colin Cross571cccf2019-02-04 11:22:08 -08002321 return config.Once(javaSdkLibrariesKey, func() interface{} {
Jiyong Park82484c02018-04-23 21:41:26 +09002322 return &[]string{}
2323 }).(*[]string)
2324}
2325
Paul Duffin749f98f2019-12-30 17:23:46 +00002326func (module *SdkLibrary) getApiDir() string {
2327 return proptools.StringDefault(module.sdkLibraryProperties.Api_dir, "api")
2328}
2329
Jiyong Parkc678ad32018-04-10 13:07:10 +09002330// For a java_sdk_library module, create internal modules for stubs, docs,
2331// runtime libs and xml file. If requested, the stubs and docs are created twice
2332// once for public API level and once for system API level
Paul Duffinf0229202020-04-29 16:47:28 +01002333func (module *SdkLibrary) CreateInternalModules(mctx android.DefaultableHookContext) {
2334 // If the module has been disabled then don't create any child modules.
Cole Fausta963b942024-04-11 17:43:00 -07002335 if !module.Enabled(mctx) {
Paul Duffinf0229202020-04-29 16:47:28 +01002336 return
2337 }
2338
Paul Duffina18abc22020-05-16 18:54:24 +01002339 if len(module.properties.Srcs) == 0 {
Inseob Kimc0907f12019-02-08 21:00:45 +09002340 mctx.PropertyErrorf("srcs", "java_sdk_library must specify srcs")
Jooyung Han58f26ab2019-12-18 15:34:32 +09002341 return
Inseob Kimc0907f12019-02-08 21:00:45 +09002342 }
2343
Paul Duffin37e0b772019-12-30 17:20:10 +00002344 // If this builds against standard libraries (i.e. is not part of the core libraries)
Paul Duffin4f5c1ef2020-11-19 14:53:43 +00002345 // then assume it provides both system and test apis.
Jiyong Parkf1691d22021-03-29 20:11:58 +09002346 sdkDep := decodeSdkDep(mctx, android.SdkContext(&module.Library))
Paul Duffin37e0b772019-12-30 17:20:10 +00002347 hasSystemAndTestApis := sdkDep.hasStandardLibs()
Paul Duffin3375e352020-04-28 10:44:03 +01002348 module.sdkLibraryProperties.Generate_system_and_test_apis = hasSystemAndTestApis
Paul Duffin4f5c1ef2020-11-19 14:53:43 +00002349
Jaewoong Jung18aefc12020-12-21 09:11:10 -08002350 missingCurrentApi := false
Inseob Kim8098faa2019-03-18 10:19:51 +09002351
Paul Duffin3375e352020-04-28 10:44:03 +01002352 generatedScopes := module.getGeneratedApiScopes(mctx)
Paul Duffind1b3a922020-01-22 11:57:20 +00002353
Paul Duffin749f98f2019-12-30 17:23:46 +00002354 apiDir := module.getApiDir()
Paul Duffin3375e352020-04-28 10:44:03 +01002355 for _, scope := range generatedScopes {
Inseob Kim8098faa2019-03-18 10:19:51 +09002356 for _, api := range []string{"current.txt", "removed.txt"} {
Paul Duffind1b3a922020-01-22 11:57:20 +00002357 path := path.Join(mctx.ModuleDir(), apiDir, scope.apiFilePrefix+api)
Inseob Kim8098faa2019-03-18 10:19:51 +09002358 p := android.ExistentPathForSource(mctx, path)
2359 if !p.Valid() {
Colin Cross18f840c2021-05-20 17:56:54 -07002360 if mctx.Config().AllowMissingDependencies() {
2361 mctx.AddMissingDependencies([]string{path})
2362 } else {
2363 mctx.ModuleErrorf("Current api file %#v doesn't exist", path)
2364 missingCurrentApi = true
2365 }
Inseob Kim8098faa2019-03-18 10:19:51 +09002366 }
2367 }
2368 }
2369
Jaewoong Jung18aefc12020-12-21 09:11:10 -08002370 if missingCurrentApi {
Inseob Kim8098faa2019-03-18 10:19:51 +09002371 script := "build/soong/scripts/gen-java-current-api-files.sh"
2372 p := android.ExistentPathForSource(mctx, script)
2373
2374 if !p.Valid() {
2375 panic(fmt.Sprintf("script file %s doesn't exist", script))
2376 }
2377
2378 mctx.ModuleErrorf("One or more current api files are missing. "+
2379 "You can update them by:\n"+
Paul Duffin37e0b772019-12-30 17:20:10 +00002380 "%s %q %s && m update-api",
Paul Duffind1b3a922020-01-22 11:57:20 +00002381 script, filepath.Join(mctx.ModuleDir(), apiDir),
Paul Duffin3375e352020-04-28 10:44:03 +01002382 strings.Join(generatedScopes.Strings(func(s *apiScope) string { return s.apiFilePrefix }), " "))
Inseob Kim8098faa2019-03-18 10:19:51 +09002383 return
2384 }
2385
Paul Duffin3375e352020-04-28 10:44:03 +01002386 for _, scope := range generatedScopes {
Paul Duffin15f34ef2020-07-20 18:04:44 +01002387 // Use the stubs source name for legacy reasons.
2388 module.createStubsSourcesAndApi(mctx, scope, module.stubsSourceModuleName(scope), scope.droidstubsArgs)
Paul Duffin0ff08bd2020-04-29 13:30:54 +01002389
Paul Duffind1b3a922020-01-22 11:57:20 +00002390 module.createStubsLibrary(mctx, scope)
Jihoon Kangfa4a90d2023-12-20 02:53:38 +00002391 module.createExportableStubsLibrary(mctx, scope)
Jihoon Kang1c92c3e2023-03-23 17:44:51 +00002392
Liana Kazanovaa574cd22024-08-05 19:45:03 +00002393 alternativeFullApiSurfaceStubLib := ""
2394 if scope == apiScopePublic {
2395 alternativeFullApiSurfaceStubLib = module.alternativeFullApiSurfaceStubLib()
Jihoon Kang0c705a42023-08-02 06:44:57 +00002396 }
Liana Kazanovaa574cd22024-08-05 19:45:03 +00002397 contributesToApiSurface := module.contributesToApiSurface(mctx.Config()) || alternativeFullApiSurfaceStubLib != ""
2398 if contributesToApiSurface {
2399 module.createApiLibrary(mctx, scope, alternativeFullApiSurfaceStubLib)
2400 }
2401
2402 module.createTopLevelStubsLibrary(mctx, scope, contributesToApiSurface)
Jihoon Kangfa4a90d2023-12-20 02:53:38 +00002403 module.createTopLevelExportableStubsLibrary(mctx, scope)
Inseob Kimc0907f12019-02-08 21:00:45 +09002404 }
2405
Paul Duffindfa131e2020-05-15 20:37:11 +01002406 if module.requiresRuntimeImplementationLibrary() {
Paul Duffin5df79302020-05-16 15:52:12 +01002407 // Create child module to create an implementation library.
2408 //
2409 // This temporarily creates a second implementation library that can be explicitly
2410 // referenced.
2411 //
2412 // TODO(b/156618935) - update comment once only one implementation library is created.
2413 module.createImplLibrary(mctx)
2414
Paul Duffindfa131e2020-05-15 20:37:11 +01002415 // Only create an XML permissions file that declares the library as being usable
2416 // as a shared library if required.
2417 if module.sharedLibrary() {
2418 module.createXmlFile(mctx)
2419 }
Paul Duffin43db9be2019-12-30 17:35:49 +00002420
2421 // record java_sdk_library modules so that they are exported to make
2422 javaSdkLibraries := javaSdkLibraries(mctx.Config())
2423 javaSdkLibrariesLock.Lock()
2424 defer javaSdkLibrariesLock.Unlock()
2425 *javaSdkLibraries = append(*javaSdkLibraries, module.BaseModuleName())
2426 }
Anton Hansson7f66efa2020-10-08 14:47:23 +01002427
Paul Duffin77590a82022-04-28 14:13:30 +00002428 // 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 +01002429 module.properties.Libs = append(module.properties.Libs, module.sdkLibraryProperties.Impl_only_libs...)
Paul Duffin77590a82022-04-28 14:13:30 +00002430 module.properties.Static_libs = append(module.properties.Static_libs, module.sdkLibraryProperties.Impl_only_static_libs...)
Inseob Kimc0907f12019-02-08 21:00:45 +09002431}
2432
2433func (module *SdkLibrary) InitSdkLibraryProperties() {
Colin Crossce6734e2020-06-15 16:09:53 -07002434 module.addHostAndDeviceProperties()
2435 module.AddProperties(&module.sdkLibraryProperties)
Sundong Ahn054b19a2018-10-19 13:46:09 +09002436
Paul Duffin71b33cc2021-06-23 11:39:47 +01002437 module.initSdkLibraryComponent(module)
Paul Duffin859fe962020-05-15 10:20:31 +01002438
Paul Duffina18abc22020-05-16 18:54:24 +01002439 module.properties.Installable = proptools.BoolPtr(true)
2440 module.deviceProperties.IsSDKLibrary = true
Inseob Kimc0907f12019-02-08 21:00:45 +09002441}
Sundong Ahn054b19a2018-10-19 13:46:09 +09002442
Paul Duffindfa131e2020-05-15 20:37:11 +01002443func (module *SdkLibrary) requiresRuntimeImplementationLibrary() bool {
2444 return !proptools.Bool(module.sdkLibraryProperties.Api_only)
2445}
2446
Jiyong Park932cdfe2020-05-28 00:19:53 +09002447func (module *SdkLibrary) defaultsToStubs() bool {
2448 return proptools.Bool(module.sdkLibraryProperties.Default_to_stubs)
2449}
2450
Paul Duffin1b1e8062020-05-08 13:44:43 +01002451// Defines how to name the individual component modules the sdk library creates.
2452type sdkLibraryComponentNamingScheme interface {
2453 stubsLibraryModuleName(scope *apiScope, baseName string) string
2454
2455 stubsSourceModuleName(scope *apiScope, baseName string) string
Jihoon Kang1c92c3e2023-03-23 17:44:51 +00002456
2457 apiLibraryModuleName(scope *apiScope, baseName string) string
Jihoon Kang1147b312023-06-08 23:25:57 +00002458
Jihoon Kangfa4a90d2023-12-20 02:53:38 +00002459 sourceStubsLibraryModuleName(scope *apiScope, baseName string) string
2460
2461 exportableStubsLibraryModuleName(scope *apiScope, baseName string) string
2462
2463 exportableSourceStubsLibraryModuleName(scope *apiScope, baseName string) string
Paul Duffin1b1e8062020-05-08 13:44:43 +01002464}
2465
2466type defaultNamingScheme struct {
2467}
2468
2469func (s *defaultNamingScheme) stubsLibraryModuleName(scope *apiScope, baseName string) string {
2470 return scope.stubsLibraryModuleName(baseName)
2471}
2472
2473func (s *defaultNamingScheme) stubsSourceModuleName(scope *apiScope, baseName string) string {
2474 return scope.stubsSourceModuleName(baseName)
2475}
2476
Jihoon Kang1c92c3e2023-03-23 17:44:51 +00002477func (s *defaultNamingScheme) apiLibraryModuleName(scope *apiScope, baseName string) string {
2478 return scope.apiLibraryModuleName(baseName)
2479}
2480
Jihoon Kangfa4a90d2023-12-20 02:53:38 +00002481func (s *defaultNamingScheme) sourceStubsLibraryModuleName(scope *apiScope, baseName string) string {
Jihoon Kang1147b312023-06-08 23:25:57 +00002482 return scope.sourceStubLibraryModuleName(baseName)
2483}
2484
Jihoon Kangfa4a90d2023-12-20 02:53:38 +00002485func (s *defaultNamingScheme) exportableStubsLibraryModuleName(scope *apiScope, baseName string) string {
2486 return scope.exportableStubsLibraryModuleName(baseName)
2487}
2488
2489func (s *defaultNamingScheme) exportableSourceStubsLibraryModuleName(scope *apiScope, baseName string) string {
2490 return scope.exportableSourceStubsLibraryModuleName(baseName)
2491}
2492
Paul Duffin1b1e8062020-05-08 13:44:43 +01002493var _ sdkLibraryComponentNamingScheme = (*defaultNamingScheme)(nil)
2494
Jihoon Kangfa4a90d2023-12-20 02:53:38 +00002495func hasStubsLibrarySuffix(name string, apiScope *apiScope) bool {
2496 return strings.HasSuffix(name, apiScope.stubsLibraryModuleNameSuffix()) ||
2497 strings.HasSuffix(name, apiScope.exportableStubsLibraryModuleNameSuffix())
2498}
2499
Jaewoong Jungbc15e3a2021-03-10 17:02:43 -08002500func moduleStubLinkType(name string) (stub bool, ret sdkLinkType) {
Jihoon Kang1147b312023-06-08 23:25:57 +00002501 name = strings.TrimSuffix(name, ".from-source")
2502
Anton Hansson2d0c1942020-05-25 12:20:51 +01002503 // This suffix-based approach is fragile and could potentially mis-trigger.
2504 // TODO(b/155164730): Clean this up when modules no longer reference sdk_lib stubs directly.
Jihoon Kangfa4a90d2023-12-20 02:53:38 +00002505 if hasStubsLibrarySuffix(name, apiScopePublic) {
Anton Hansson08f476b2021-04-07 15:32:19 +01002506 if name == "hwbinder.stubs" || name == "libcore_private.stubs" {
2507 // Due to a previous bug, these modules were not considered stubs, so we retain that.
2508 return false, javaPlatform
2509 }
Anton Hansson2d0c1942020-05-25 12:20:51 +01002510 return true, javaSdk
2511 }
Jihoon Kangfa4a90d2023-12-20 02:53:38 +00002512 if hasStubsLibrarySuffix(name, apiScopeSystem) {
Anton Hansson2d0c1942020-05-25 12:20:51 +01002513 return true, javaSystem
2514 }
Jihoon Kangfa4a90d2023-12-20 02:53:38 +00002515 if hasStubsLibrarySuffix(name, apiScopeModuleLib) {
Anton Hansson2d0c1942020-05-25 12:20:51 +01002516 return true, javaModule
2517 }
Jihoon Kangfa4a90d2023-12-20 02:53:38 +00002518 if hasStubsLibrarySuffix(name, apiScopeTest) {
Anton Hansson2d0c1942020-05-25 12:20:51 +01002519 return true, javaSystem
2520 }
Jihoon Kangfa4a90d2023-12-20 02:53:38 +00002521 if hasStubsLibrarySuffix(name, apiScopeSystemServer) {
Jihoon Kang1147b312023-06-08 23:25:57 +00002522 return true, javaSystemServer
2523 }
Anton Hansson2d0c1942020-05-25 12:20:51 +01002524 return false, javaPlatform
2525}
2526
Jaewoong Jung4f158ee2019-07-11 10:05:35 -07002527// java_sdk_library is a special Java library that provides optional platform APIs to apps.
2528// In practice, it can be viewed as a combination of several modules: 1) stubs library that clients
2529// are linked against to, 2) droiddoc module that internally generates API stubs source files,
2530// 3) the real runtime shared library that implements the APIs, and 4) XML file for adding
2531// the runtime lib to the classpath at runtime if requested via <uses-library>.
Inseob Kimc0907f12019-02-08 21:00:45 +09002532func SdkLibraryFactory() android.Module {
2533 module := &SdkLibrary{}
Paul Duffinc3091c82020-05-08 14:16:20 +01002534
2535 // Initialize information common between source and prebuilt.
Paul Duffin71b33cc2021-06-23 11:39:47 +01002536 module.initCommon(module)
Paul Duffinc3091c82020-05-08 14:16:20 +01002537
Inseob Kimc0907f12019-02-08 21:00:45 +09002538 module.InitSdkLibraryProperties()
Jooyung Han58f26ab2019-12-18 15:34:32 +09002539 android.InitApexModule(module)
Sundong Ahn054b19a2018-10-19 13:46:09 +09002540 InitJavaModule(module, android.HostAndDeviceSupported)
Paul Duffin3375e352020-04-28 10:44:03 +01002541
2542 // Initialize the map from scope to scope specific properties.
2543 scopeToProperties := make(map[*apiScope]*ApiScopeProperties)
Jihoon Kang98aa8fa2024-06-07 11:06:57 +00002544 for _, scope := range AllApiScopes {
Paul Duffin3375e352020-04-28 10:44:03 +01002545 scopeToProperties[scope] = scope.scopeSpecificProperties(module)
2546 }
2547 module.scopeToProperties = scopeToProperties
2548
Paul Duffin4911a892020-04-29 23:35:13 +01002549 // Add the properties containing visibility rules so that they are checked.
Paul Duffin5df79302020-05-16 15:52:12 +01002550 android.AddVisibilityProperty(module, "impl_library_visibility", &module.sdkLibraryProperties.Impl_library_visibility)
Paul Duffin4911a892020-04-29 23:35:13 +01002551 android.AddVisibilityProperty(module, "stubs_library_visibility", &module.sdkLibraryProperties.Stubs_library_visibility)
2552 android.AddVisibilityProperty(module, "stubs_source_visibility", &module.sdkLibraryProperties.Stubs_source_visibility)
2553
Paul Duffin1b1e8062020-05-08 13:44:43 +01002554 module.SetDefaultableHook(func(ctx android.DefaultableHookContext) {
Paul Duffindfa131e2020-05-15 20:37:11 +01002555 // If no implementation is required then it cannot be used as a shared library
2556 // either.
2557 if !module.requiresRuntimeImplementationLibrary() {
2558 // If shared_library has been explicitly set to true then it is incompatible
2559 // with api_only: true.
2560 if proptools.Bool(module.commonSdkLibraryProperties.Shared_library) {
2561 ctx.PropertyErrorf("api_only/shared_library", "inconsistent settings, shared_library and api_only cannot both be true")
2562 }
2563 // Set shared_library: false.
2564 module.commonSdkLibraryProperties.Shared_library = proptools.BoolPtr(false)
2565 }
2566
Paul Duffin1b1e8062020-05-08 13:44:43 +01002567 if module.initCommonAfterDefaultsApplied(ctx) {
2568 module.CreateInternalModules(ctx)
2569 }
2570 })
Jiyong Parkc678ad32018-04-10 13:07:10 +09002571 return module
2572}
Colin Cross79c7c262019-04-17 11:11:46 -07002573
2574//
2575// SDK library prebuilts
2576//
2577
Paul Duffin56d44902020-01-31 13:36:25 +00002578// Properties associated with each api scope.
2579type sdkLibraryScopeProperties struct {
Colin Cross79c7c262019-04-17 11:11:46 -07002580 Jars []string `android:"path"`
2581
2582 Sdk_version *string
2583
Colin Cross79c7c262019-04-17 11:11:46 -07002584 // List of shared java libs that this module has dependencies to
2585 Libs []string
Paul Duffin3d1248c2020-04-09 00:10:17 +01002586
Paul Duffinc8782502020-04-29 20:45:27 +01002587 // The stubs source.
Paul Duffin3d1248c2020-04-09 00:10:17 +01002588 Stub_srcs []string `android:"path"`
Paul Duffin1fd005d2020-04-09 01:08:11 +01002589
2590 // The current.txt
Paul Duffin0f8faff2020-05-20 16:18:00 +01002591 Current_api *string `android:"path"`
Paul Duffin1fd005d2020-04-09 01:08:11 +01002592
2593 // The removed.txt
Paul Duffin0f8faff2020-05-20 16:18:00 +01002594 Removed_api *string `android:"path"`
Anton Hanssond78eb762021-09-21 15:25:12 +01002595
2596 // Annotation zip
2597 Annotations *string `android:"path"`
Colin Cross79c7c262019-04-17 11:11:46 -07002598}
2599
Paul Duffin56d44902020-01-31 13:36:25 +00002600type sdkLibraryImportProperties struct {
Paul Duffinfcfd7912020-01-31 17:54:30 +00002601 // List of shared java libs, common to all scopes, that this module has
2602 // dependencies to
2603 Libs []string
Paul Duffin1267d872021-04-16 17:21:36 +01002604
2605 // If set to true, compile dex files for the stubs. Defaults to false.
2606 Compile_dex *bool
Paul Duffin869de142021-07-15 14:14:41 +01002607
2608 // If not empty, classes are restricted to the specified packages and their sub-packages.
Paul Duffin869de142021-07-15 14:14:41 +01002609 Permitted_packages []string
Spandan Das23956d12024-01-19 00:22:22 +00002610
2611 // Name of the source soong module that gets shadowed by this prebuilt
2612 // If unspecified, follows the naming convention that the source module of
2613 // the prebuilt is Name() without "prebuilt_" prefix
2614 Source_module_name *string
Paul Duffin56d44902020-01-31 13:36:25 +00002615}
2616
Paul Duffineedc5d52020-06-12 17:46:39 +01002617type SdkLibraryImport struct {
Colin Cross79c7c262019-04-17 11:11:46 -07002618 android.ModuleBase
2619 android.DefaultableModuleBase
2620 prebuilt android.Prebuilt
Paul Duffindd46f712020-02-10 13:37:10 +00002621 android.ApexModuleBase
Colin Cross79c7c262019-04-17 11:11:46 -07002622
Paul Duffin37856732021-02-26 14:24:15 +00002623 hiddenAPI
Jiakai Zhang204356f2021-09-09 08:12:46 +00002624 dexpreopter
Paul Duffin37856732021-02-26 14:24:15 +00002625
Colin Cross79c7c262019-04-17 11:11:46 -07002626 properties sdkLibraryImportProperties
2627
Paul Duffin46a26a82020-04-07 19:27:04 +01002628 // Map from api scope to the scope specific property structure.
2629 scopeProperties map[*apiScope]*sdkLibraryScopeProperties
2630
Paul Duffin56d44902020-01-31 13:36:25 +00002631 commonToSdkLibraryAndImport
Paul Duffineedc5d52020-06-12 17:46:39 +01002632
Paul Duffineedc5d52020-06-12 17:46:39 +01002633 // The reference to the xml permissions module created by the source module.
2634 // Is nil if the source module does not exist.
2635 xmlPermissionsFileModule *sdkLibraryXml
Paul Duffin39853512021-02-26 11:09:39 +00002636
Jeongik Chad5fe8782021-07-08 01:13:11 +09002637 // Build path to the dex implementation jar obtained from the prebuilt_apex, if any.
Spandan Dasfae468e2023-12-12 23:23:53 +00002638 dexJarFile OptionalDexJarPath
2639 dexJarFileErr error
Jeongik Chad5fe8782021-07-08 01:13:11 +09002640
2641 // Expected install file path of the source module(sdk_library)
2642 // or dex implementation jar obtained from the prebuilt_apex, if any.
2643 installFile android.Path
Colin Cross79c7c262019-04-17 11:11:46 -07002644}
2645
Paul Duffineedc5d52020-06-12 17:46:39 +01002646var _ SdkLibraryDependency = (*SdkLibraryImport)(nil)
Colin Cross79c7c262019-04-17 11:11:46 -07002647
Paul Duffin46a26a82020-04-07 19:27:04 +01002648// The type of a structure that contains a field of type sdkLibraryScopeProperties
2649// for each apiscope in allApiScopes, e.g. something like:
Colin Crossd079e0b2022-08-16 10:27:33 -07002650//
2651// struct {
2652// Public sdkLibraryScopeProperties
2653// System sdkLibraryScopeProperties
2654// ...
2655// }
Paul Duffin46a26a82020-04-07 19:27:04 +01002656var allScopeStructType = createAllScopePropertiesStructType()
2657
2658// Dynamically create a structure type for each apiscope in allApiScopes.
2659func createAllScopePropertiesStructType() reflect.Type {
2660 var fields []reflect.StructField
Jihoon Kang98aa8fa2024-06-07 11:06:57 +00002661 for _, apiScope := range AllApiScopes {
Paul Duffin46a26a82020-04-07 19:27:04 +01002662 field := reflect.StructField{
2663 Name: apiScope.fieldName,
2664 Type: reflect.TypeOf(sdkLibraryScopeProperties{}),
2665 }
2666 fields = append(fields, field)
2667 }
2668
2669 return reflect.StructOf(fields)
2670}
2671
2672// Create an instance of the scope specific structure type and return a map
2673// from apiscope to a pointer to each scope specific field.
2674func createPropertiesInstance() (interface{}, map[*apiScope]*sdkLibraryScopeProperties) {
2675 allScopePropertiesPtr := reflect.New(allScopeStructType)
2676 allScopePropertiesStruct := allScopePropertiesPtr.Elem()
2677 scopeProperties := make(map[*apiScope]*sdkLibraryScopeProperties)
2678
Jihoon Kang98aa8fa2024-06-07 11:06:57 +00002679 for _, apiScope := range AllApiScopes {
Paul Duffin46a26a82020-04-07 19:27:04 +01002680 field := allScopePropertiesStruct.FieldByName(apiScope.fieldName)
2681 scopeProperties[apiScope] = field.Addr().Interface().(*sdkLibraryScopeProperties)
2682 }
2683
2684 return allScopePropertiesPtr.Interface(), scopeProperties
2685}
2686
Jaewoong Jung4f158ee2019-07-11 10:05:35 -07002687// java_sdk_library_import imports a prebuilt java_sdk_library.
Colin Cross79c7c262019-04-17 11:11:46 -07002688func sdkLibraryImportFactory() android.Module {
Paul Duffineedc5d52020-06-12 17:46:39 +01002689 module := &SdkLibraryImport{}
Colin Cross79c7c262019-04-17 11:11:46 -07002690
Paul Duffin46a26a82020-04-07 19:27:04 +01002691 allScopeProperties, scopeToProperties := createPropertiesInstance()
2692 module.scopeProperties = scopeToProperties
Jiakai Zhang9c4dc192023-02-09 00:09:24 +08002693 module.AddProperties(&module.properties, allScopeProperties, &module.importDexpreoptProperties)
Colin Cross79c7c262019-04-17 11:11:46 -07002694
Paul Duffinc3091c82020-05-08 14:16:20 +01002695 // Initialize information common between source and prebuilt.
Paul Duffin71b33cc2021-06-23 11:39:47 +01002696 module.initCommon(module)
Paul Duffinc3091c82020-05-08 14:16:20 +01002697
Paul Duffin0bdcb272020-02-06 15:24:57 +00002698 android.InitPrebuiltModule(module, &[]string{""})
Paul Duffindd46f712020-02-10 13:37:10 +00002699 android.InitApexModule(module)
Colin Cross79c7c262019-04-17 11:11:46 -07002700 InitJavaModule(module, android.HostAndDeviceSupported)
2701
Paul Duffin1b1e8062020-05-08 13:44:43 +01002702 module.SetDefaultableHook(func(mctx android.DefaultableHookContext) {
2703 if module.initCommonAfterDefaultsApplied(mctx) {
2704 module.createInternalModules(mctx)
2705 }
2706 })
Colin Cross79c7c262019-04-17 11:11:46 -07002707 return module
2708}
2709
Paul Duffin630b11e2021-07-15 13:35:26 +01002710var _ PermittedPackagesForUpdatableBootJars = (*SdkLibraryImport)(nil)
2711
2712func (module *SdkLibraryImport) PermittedPackagesForUpdatableBootJars() []string {
2713 return module.properties.Permitted_packages
2714}
2715
Paul Duffineedc5d52020-06-12 17:46:39 +01002716func (module *SdkLibraryImport) Prebuilt() *android.Prebuilt {
Colin Cross79c7c262019-04-17 11:11:46 -07002717 return &module.prebuilt
2718}
2719
Paul Duffineedc5d52020-06-12 17:46:39 +01002720func (module *SdkLibraryImport) Name() string {
Colin Cross79c7c262019-04-17 11:11:46 -07002721 return module.prebuilt.Name(module.ModuleBase.Name())
2722}
2723
Spandan Das23956d12024-01-19 00:22:22 +00002724func (module *SdkLibraryImport) BaseModuleName() string {
2725 return proptools.StringDefault(module.properties.Source_module_name, module.ModuleBase.Name())
2726}
2727
Paul Duffineedc5d52020-06-12 17:46:39 +01002728func (module *SdkLibraryImport) createInternalModules(mctx android.DefaultableHookContext) {
Colin Cross79c7c262019-04-17 11:11:46 -07002729
Paul Duffin50061512020-01-21 16:31:05 +00002730 // If the build is configured to use prebuilts then force this to be preferred.
Jeongik Cha816a23a2020-07-08 01:09:23 +09002731 if mctx.Config().AlwaysUsePrebuiltSdks() {
Paul Duffin50061512020-01-21 16:31:05 +00002732 module.prebuilt.ForcePrefer()
2733 }
2734
Paul Duffin46a26a82020-04-07 19:27:04 +01002735 for apiScope, scopeProperties := range module.scopeProperties {
Paul Duffin56d44902020-01-31 13:36:25 +00002736 if len(scopeProperties.Jars) == 0 {
2737 continue
2738 }
2739
Paul Duffinbbb546b2020-04-09 00:07:11 +01002740 module.createJavaImportForStubs(mctx, apiScope, scopeProperties)
Paul Duffin3d1248c2020-04-09 00:10:17 +01002741
Paul Duffin0f8faff2020-05-20 16:18:00 +01002742 if len(scopeProperties.Stub_srcs) > 0 {
2743 module.createPrebuiltStubsSources(mctx, apiScope, scopeProperties)
2744 }
Jihoon Kang71c86832023-09-13 01:01:53 +00002745
2746 if scopeProperties.Current_api != nil {
2747 module.createPrebuiltApiContribution(mctx, apiScope, scopeProperties)
2748 }
Paul Duffin56d44902020-01-31 13:36:25 +00002749 }
Colin Cross79c7c262019-04-17 11:11:46 -07002750
2751 javaSdkLibraries := javaSdkLibraries(mctx.Config())
2752 javaSdkLibrariesLock.Lock()
2753 defer javaSdkLibrariesLock.Unlock()
2754 *javaSdkLibraries = append(*javaSdkLibraries, module.BaseModuleName())
2755}
2756
Paul Duffineedc5d52020-06-12 17:46:39 +01002757func (module *SdkLibraryImport) createJavaImportForStubs(mctx android.DefaultableHookContext, apiScope *apiScope, scopeProperties *sdkLibraryScopeProperties) {
Paul Duffinbbb546b2020-04-09 00:07:11 +01002758 // Creates a java import for the jar with ".stubs" suffix
2759 props := struct {
Spandan Das23956d12024-01-19 00:22:22 +00002760 Name *string
2761 Source_module_name *string
2762 Created_by_java_sdk_library_name *string
2763 Sdk_version *string
2764 Libs []string
2765 Jars []string
2766 Compile_dex *bool
Jihoon Kangfe914ed2024-02-12 22:49:21 +00002767 Is_stubs_module *bool
Paul Duffinbf4de042022-09-27 12:41:52 +01002768
2769 android.UserSuppliedPrebuiltProperties
Paul Duffinbbb546b2020-04-09 00:07:11 +01002770 }{}
Paul Duffinc3091c82020-05-08 14:16:20 +01002771 props.Name = proptools.StringPtr(module.stubsLibraryModuleName(apiScope))
Spandan Das23956d12024-01-19 00:22:22 +00002772 props.Source_module_name = proptools.StringPtr(apiScope.stubsLibraryModuleName(module.BaseModuleName()))
2773 props.Created_by_java_sdk_library_name = proptools.StringPtr(module.RootLibraryName())
Paul Duffinbbb546b2020-04-09 00:07:11 +01002774 props.Sdk_version = scopeProperties.Sdk_version
2775 // Prepend any of the libs from the legacy public properties to the libs for each of the
2776 // scopes to avoid having to duplicate them in each scope.
2777 props.Libs = append(module.properties.Libs, scopeProperties.Libs...)
2778 props.Jars = scopeProperties.Jars
Paul Duffin1dbe3ca2020-05-16 09:57:59 +01002779
Paul Duffin38b57852020-05-13 16:08:09 +01002780 // The imports are preferred if the java_sdk_library_import is preferred.
Paul Duffinbf4de042022-09-27 12:41:52 +01002781 props.CopyUserSuppliedPropertiesFromPrebuilt(&module.prebuilt)
Paul Duffin859fe962020-05-15 10:20:31 +01002782
Paul Duffin1267d872021-04-16 17:21:36 +01002783 // The imports need to be compiled to dex if the java_sdk_library_import requests it.
Paul Duffinf4600f62021-05-13 22:34:45 +01002784 compileDex := module.properties.Compile_dex
2785 if module.stubLibrariesCompiledForDex() {
2786 compileDex = proptools.BoolPtr(true)
2787 }
2788 props.Compile_dex = compileDex
Jihoon Kangfe914ed2024-02-12 22:49:21 +00002789 props.Is_stubs_module = proptools.BoolPtr(true)
Paul Duffin1267d872021-04-16 17:21:36 +01002790
Paul Duffin859fe962020-05-15 10:20:31 +01002791 mctx.CreateModule(ImportFactory, &props, module.sdkComponentPropertiesForChildLibrary())
Paul Duffinbbb546b2020-04-09 00:07:11 +01002792}
2793
Paul Duffineedc5d52020-06-12 17:46:39 +01002794func (module *SdkLibraryImport) createPrebuiltStubsSources(mctx android.DefaultableHookContext, apiScope *apiScope, scopeProperties *sdkLibraryScopeProperties) {
Paul Duffin3d1248c2020-04-09 00:10:17 +01002795 props := struct {
Spandan Das23956d12024-01-19 00:22:22 +00002796 Name *string
2797 Source_module_name *string
2798 Created_by_java_sdk_library_name *string
2799 Srcs []string
Paul Duffinbf4de042022-09-27 12:41:52 +01002800
2801 android.UserSuppliedPrebuiltProperties
Paul Duffin3d1248c2020-04-09 00:10:17 +01002802 }{}
Paul Duffinc3091c82020-05-08 14:16:20 +01002803 props.Name = proptools.StringPtr(module.stubsSourceModuleName(apiScope))
Spandan Das23956d12024-01-19 00:22:22 +00002804 props.Source_module_name = proptools.StringPtr(apiScope.stubsSourceModuleName(module.BaseModuleName()))
2805 props.Created_by_java_sdk_library_name = proptools.StringPtr(module.RootLibraryName())
Paul Duffin3d1248c2020-04-09 00:10:17 +01002806 props.Srcs = scopeProperties.Stub_srcs
Paul Duffin38b57852020-05-13 16:08:09 +01002807
2808 // The stubs source is preferred if the java_sdk_library_import is preferred.
Paul Duffinbf4de042022-09-27 12:41:52 +01002809 props.CopyUserSuppliedPropertiesFromPrebuilt(&module.prebuilt)
2810
Spandan Das2cc80ba2023-10-27 17:21:52 +00002811 mctx.CreateModule(PrebuiltStubsSourcesFactory, &props, module.sdkComponentPropertiesForChildLibrary())
Paul Duffin3d1248c2020-04-09 00:10:17 +01002812}
2813
Jihoon Kang71c86832023-09-13 01:01:53 +00002814func (module *SdkLibraryImport) createPrebuiltApiContribution(mctx android.DefaultableHookContext, apiScope *apiScope, scopeProperties *sdkLibraryScopeProperties) {
2815 api_file := scopeProperties.Current_api
2816 api_surface := &apiScope.name
2817
2818 props := struct {
Spandan Das23956d12024-01-19 00:22:22 +00002819 Name *string
2820 Source_module_name *string
2821 Created_by_java_sdk_library_name *string
2822 Api_surface *string
2823 Api_file *string
2824 Visibility []string
Jihoon Kang71c86832023-09-13 01:01:53 +00002825 }{}
2826
2827 props.Name = proptools.StringPtr(module.stubsSourceModuleName(apiScope) + ".api.contribution")
Spandan Das23956d12024-01-19 00:22:22 +00002828 props.Source_module_name = proptools.StringPtr(apiScope.stubsSourceModuleName(module.BaseModuleName()) + ".api.contribution")
2829 props.Created_by_java_sdk_library_name = proptools.StringPtr(module.RootLibraryName())
Jihoon Kang71c86832023-09-13 01:01:53 +00002830 props.Api_surface = api_surface
2831 props.Api_file = api_file
2832 props.Visibility = []string{"//visibility:override", "//visibility:public"}
2833
Spandan Das2cc80ba2023-10-27 17:21:52 +00002834 mctx.CreateModule(ApiContributionImportFactory, &props, module.sdkComponentPropertiesForChildLibrary())
Jihoon Kang71c86832023-09-13 01:01:53 +00002835}
2836
Paul Duffin44f1d842020-06-26 20:17:02 +01002837// Add the dependencies on the child module in the component deps mutator so that it
2838// creates references to the prebuilt and not the source modules.
2839func (module *SdkLibraryImport) ComponentDepsMutator(ctx android.BottomUpMutatorContext) {
Paul Duffin46a26a82020-04-07 19:27:04 +01002840 for apiScope, scopeProperties := range module.scopeProperties {
Paul Duffin56d44902020-01-31 13:36:25 +00002841 if len(scopeProperties.Jars) == 0 {
2842 continue
2843 }
2844
2845 // Add dependencies to the prebuilt stubs library
Jihoon Kangb7431552024-01-22 19:40:08 +00002846 ctx.AddVariationDependencies(nil, apiScope.prebuiltStubsTag, android.PrebuiltNameFromSource(module.stubsLibraryModuleName(apiScope)))
Paul Duffin0f8faff2020-05-20 16:18:00 +01002847
2848 if len(scopeProperties.Stub_srcs) > 0 {
2849 // Add dependencies to the prebuilt stubs source library
Paul Duffin864116c2021-04-02 10:24:13 +01002850 ctx.AddVariationDependencies(nil, apiScope.stubsSourceTag, android.PrebuiltNameFromSource(module.stubsSourceModuleName(apiScope)))
Paul Duffin0f8faff2020-05-20 16:18:00 +01002851 }
Paul Duffin56d44902020-01-31 13:36:25 +00002852 }
Paul Duffin44f1d842020-06-26 20:17:02 +01002853}
2854
2855// Add other dependencies as normal.
2856func (module *SdkLibraryImport) DepsMutator(ctx android.BottomUpMutatorContext) {
Paul Duffineedc5d52020-06-12 17:46:39 +01002857
2858 implName := module.implLibraryModuleName()
2859 if ctx.OtherModuleExists(implName) {
2860 ctx.AddVariationDependencies(nil, implLibraryTag, implName)
2861
2862 xmlPermissionsModuleName := module.xmlPermissionsModuleName()
2863 if module.sharedLibrary() && ctx.OtherModuleExists(xmlPermissionsModuleName) {
2864 // Add dependency to the rule for generating the xml permissions file
2865 ctx.AddDependency(module, xmlPermissionsFileTag, xmlPermissionsModuleName)
2866 }
2867 }
Colin Cross79c7c262019-04-17 11:11:46 -07002868}
2869
Jiyong Park45bf82e2020-12-15 22:29:02 +09002870var _ android.ApexModule = (*SdkLibraryImport)(nil)
2871
2872// Implements android.ApexModule
Paul Duffineedc5d52020-06-12 17:46:39 +01002873func (module *SdkLibraryImport) DepIsInSameApex(mctx android.BaseModuleContext, dep android.Module) bool {
2874 depTag := mctx.OtherModuleDependencyTag(dep)
2875 if depTag == xmlPermissionsFileTag {
2876 return true
2877 }
2878
2879 // None of the other dependencies of the java_sdk_library_import are in the same apex
2880 // as the one that references this module.
2881 return false
2882}
2883
Jiyong Park45bf82e2020-12-15 22:29:02 +09002884// Implements android.ApexModule
Dan Albertc8060532020-07-22 22:32:17 -07002885func (module *SdkLibraryImport) ShouldSupportSdkVersion(ctx android.BaseModuleContext,
2886 sdkVersion android.ApiLevel) error {
Jooyung Han749dc692020-04-15 11:03:39 +09002887 // we don't check prebuilt modules for sdk_version
2888 return nil
2889}
2890
Paul Duffinea8f8082021-06-24 13:25:57 +01002891// Implements android.ApexModule
2892func (module *SdkLibraryImport) UniqueApexVariations() bool {
2893 return module.uniqueApexVariations()
2894}
2895
Paul Duffin09817d62022-04-28 17:45:11 +01002896// MinSdkVersion - Implements hiddenAPIModule
Spandan Das8c9ae7e2023-03-03 21:20:36 +00002897func (module *SdkLibraryImport) MinSdkVersion(ctx android.EarlyModuleContext) android.ApiLevel {
2898 return android.NoneApiLevel
Paul Duffin09817d62022-04-28 17:45:11 +01002899}
2900
2901var _ hiddenAPIModule = (*SdkLibraryImport)(nil)
2902
Paul Duffineedc5d52020-06-12 17:46:39 +01002903func (module *SdkLibraryImport) GenerateAndroidBuildActions(ctx android.ModuleContext) {
Paul Duffina2ae7e02020-09-11 11:55:00 +01002904 module.generateCommonBuildActions(ctx)
2905
Jeongik Chad5fe8782021-07-08 01:13:11 +09002906 // Assume that source module(sdk_library) is installed in /<sdk_library partition>/framework
2907 module.installFile = android.PathForModuleInstall(ctx, "framework", module.Stem()+".jar")
2908
Paul Duffin0f8faff2020-05-20 16:18:00 +01002909 // Record the paths to the prebuilt stubs library and stubs source.
Colin Cross79c7c262019-04-17 11:11:46 -07002910 ctx.VisitDirectDeps(func(to android.Module) {
2911 tag := ctx.OtherModuleDependencyTag(to)
2912
Paul Duffin0f8faff2020-05-20 16:18:00 +01002913 // Extract information from any of the scope specific dependencies.
2914 if scopeTag, ok := tag.(scopeDependencyTag); ok {
2915 apiScope := scopeTag.apiScope
2916 scopePaths := module.getScopePathsCreateIfNeeded(apiScope)
2917
2918 // Extract information from the dependency. The exact information extracted
2919 // is determined by the nature of the dependency which is determined by the tag.
2920 scopeTag.extractDepInfo(ctx, to, scopePaths)
Paul Duffineedc5d52020-06-12 17:46:39 +01002921 } else if tag == implLibraryTag {
2922 if implLibrary, ok := to.(*Library); ok {
2923 module.implLibraryModule = implLibrary
2924 } else {
2925 ctx.ModuleErrorf("implementation library must be of type *java.Library but was %T", to)
2926 }
2927 } else if tag == xmlPermissionsFileTag {
2928 if xmlPermissionsFileModule, ok := to.(*sdkLibraryXml); ok {
2929 module.xmlPermissionsFileModule = xmlPermissionsFileModule
2930 } else {
2931 ctx.ModuleErrorf("xml permissions file module must be of type *sdkLibraryXml but was %T", to)
2932 }
Colin Cross79c7c262019-04-17 11:11:46 -07002933 }
2934 })
Paul Duffin0f8faff2020-05-20 16:18:00 +01002935
2936 // Populate the scope paths with information from the properties.
2937 for apiScope, scopeProperties := range module.scopeProperties {
2938 if len(scopeProperties.Jars) == 0 {
2939 continue
2940 }
2941
2942 paths := module.getScopePathsCreateIfNeeded(apiScope)
Anton Hanssond78eb762021-09-21 15:25:12 +01002943 paths.annotationsZip = android.OptionalPathForModuleSrc(ctx, scopeProperties.Annotations)
Paul Duffin0f8faff2020-05-20 16:18:00 +01002944 paths.currentApiFilePath = android.OptionalPathForModuleSrc(ctx, scopeProperties.Current_api)
2945 paths.removedApiFilePath = android.OptionalPathForModuleSrc(ctx, scopeProperties.Removed_api)
2946 }
Paul Duffin39853512021-02-26 11:09:39 +00002947
2948 if ctx.Device() {
2949 // If this is a variant created for a prebuilt_apex then use the dex implementation jar
2950 // obtained from the associated deapexer module.
Colin Crossff694a82023-12-13 15:54:49 -08002951 ai, _ := android.ModuleProvider(ctx, android.ApexInfoProvider)
Paul Duffin39853512021-02-26 11:09:39 +00002952 if ai.ForPrebuiltApex {
Paul Duffin39853512021-02-26 11:09:39 +00002953 // Get the path of the dex implementation jar from the `deapexer` module.
Spandan Dasfae468e2023-12-12 23:23:53 +00002954 di, err := android.FindDeapexerProviderForModule(ctx)
2955 if err != nil {
2956 // An error was found, possibly due to multiple apexes in the tree that export this library
2957 // Defer the error till a client tries to call DexJarBuildPath
2958 module.dexJarFileErr = err
Spandan Das3a392012024-01-17 18:26:27 +00002959 module.initHiddenAPIError(err)
Spandan Dasfae468e2023-12-12 23:23:53 +00002960 return
Martin Stjernholm44825602021-09-17 01:44:12 +01002961 }
Spandan Das5be63332023-12-13 00:06:32 +00002962 dexJarFileApexRootRelative := ApexRootRelativePathToJavaLib(module.BaseModuleName())
Jiakai Zhang81e46812023-02-08 21:56:07 +08002963 if dexOutputPath := di.PrebuiltExportPath(dexJarFileApexRootRelative); dexOutputPath != nil {
Martin Stjernholm8be1e6d2021-09-15 03:34:04 +01002964 dexJarFile := makeDexJarPathFromPath(dexOutputPath)
2965 module.dexJarFile = dexJarFile
Jiakai Zhang204356f2021-09-09 08:12:46 +00002966 installPath := android.PathForModuleInPartitionInstall(
Jiakai Zhang81e46812023-02-08 21:56:07 +08002967 ctx, "apex", ai.ApexVariationName, dexJarFileApexRootRelative)
Jiakai Zhang204356f2021-09-09 08:12:46 +00002968 module.installFile = installPath
Martin Stjernholm8be1e6d2021-09-15 03:34:04 +01002969 module.initHiddenAPI(ctx, dexJarFile, module.findScopePaths(apiScopePublic).stubsImplPath[0], nil)
Jiakai Zhang204356f2021-09-09 08:12:46 +00002970
Spandan Dase21a8d42024-01-23 23:56:29 +00002971 module.dexpreopter.installPath = module.dexpreopter.getInstallPath(ctx, android.RemoveOptionalPrebuiltPrefix(ctx.ModuleName()), installPath)
Jiakai Zhang204356f2021-09-09 08:12:46 +00002972 module.dexpreopter.isSDKLibrary = true
Spandan Dase21a8d42024-01-23 23:56:29 +00002973 module.dexpreopter.uncompressedDex = shouldUncompressDex(ctx, android.RemoveOptionalPrebuiltPrefix(ctx.ModuleName()), &module.dexpreopter)
Jiakai Zhang81e46812023-02-08 21:56:07 +08002974
2975 if profilePath := di.PrebuiltExportPath(dexJarFileApexRootRelative + ".prof"); profilePath != nil {
2976 module.dexpreopter.inputProfilePathOnHost = profilePath
2977 }
Paul Duffin39853512021-02-26 11:09:39 +00002978 } else {
2979 // This should never happen as a variant for a prebuilt_apex is only created if the
2980 // prebuilt_apex has been configured to export the java library dex file.
Martin Stjernholm44825602021-09-17 01:44:12 +01002981 ctx.ModuleErrorf("internal error: no dex implementation jar available from prebuilt APEX %s", di.ApexModuleName())
Paul Duffin39853512021-02-26 11:09:39 +00002982 }
2983 }
2984 }
mrziwang9f7b9f42024-07-10 12:18:06 -07002985
2986 module.setOutputFiles(ctx)
2987 if module.implLibraryModule != nil {
2988 setOutputFiles(ctx, module.implLibraryModule.Module)
2989 }
Colin Cross79c7c262019-04-17 11:11:46 -07002990}
2991
Jiyong Parkf1691d22021-03-29 20:11:58 +09002992func (module *SdkLibraryImport) sdkJars(ctx android.BaseModuleContext, sdkVersion android.SdkSpec, headerJars bool) android.Paths {
Paul Duffineedc5d52020-06-12 17:46:39 +01002993
2994 // For consistency with SdkLibrary make the implementation jar available to libraries that
2995 // are within the same APEX.
2996 implLibraryModule := module.implLibraryModule
Colin Cross56a83212020-09-15 18:30:11 -07002997 if implLibraryModule != nil && withinSameApexesAs(ctx, module) {
Paul Duffineedc5d52020-06-12 17:46:39 +01002998 if headerJars {
2999 return implLibraryModule.HeaderJars()
3000 } else {
3001 return implLibraryModule.ImplementationJars()
3002 }
3003 }
3004
Paul Duffin23970f42020-05-20 14:20:02 +01003005 return module.selectHeaderJarsForSdkVersion(ctx, sdkVersion)
Paul Duffin56d44902020-01-31 13:36:25 +00003006}
3007
Colin Cross79c7c262019-04-17 11:11:46 -07003008// to satisfy SdkLibraryDependency interface
Jiyong Parkf1691d22021-03-29 20:11:58 +09003009func (module *SdkLibraryImport) SdkHeaderJars(ctx android.BaseModuleContext, sdkVersion android.SdkSpec) android.Paths {
Colin Cross79c7c262019-04-17 11:11:46 -07003010 // This module is just a wrapper for the prebuilt stubs.
Paul Duffineedc5d52020-06-12 17:46:39 +01003011 return module.sdkJars(ctx, sdkVersion, true)
Colin Cross79c7c262019-04-17 11:11:46 -07003012}
3013
Ulya Trafimovichdbf31662020-12-17 12:07:54 +00003014// to satisfy UsesLibraryDependency interface
Spandan Das59a4a2b2024-01-09 21:35:56 +00003015func (module *SdkLibraryImport) DexJarBuildPath(ctx android.ModuleErrorfContext) OptionalDexJarPath {
Paul Duffin39853512021-02-26 11:09:39 +00003016 // The dex implementation jar extracted from the .apex file should be used in preference to the
3017 // source.
Spandan Dasfae468e2023-12-12 23:23:53 +00003018 if module.dexJarFileErr != nil {
Spandan Das59a4a2b2024-01-09 21:35:56 +00003019 ctx.ModuleErrorf(module.dexJarFileErr.Error())
Spandan Dasfae468e2023-12-12 23:23:53 +00003020 }
Martin Stjernholm8be1e6d2021-09-15 03:34:04 +01003021 if module.dexJarFile.IsSet() {
Paul Duffin39853512021-02-26 11:09:39 +00003022 return module.dexJarFile
3023 }
Paul Duffineedc5d52020-06-12 17:46:39 +01003024 if module.implLibraryModule == nil {
Martin Stjernholm8be1e6d2021-09-15 03:34:04 +01003025 return makeUnsetDexJarPath()
Paul Duffineedc5d52020-06-12 17:46:39 +01003026 } else {
Spandan Das59a4a2b2024-01-09 21:35:56 +00003027 return module.implLibraryModule.DexJarBuildPath(ctx)
Paul Duffineedc5d52020-06-12 17:46:39 +01003028 }
3029}
3030
Ulya Trafimovichdbf31662020-12-17 12:07:54 +00003031// to satisfy UsesLibraryDependency interface
Ulya Trafimovich31e444e2020-08-14 17:32:16 +01003032func (module *SdkLibraryImport) DexJarInstallPath() android.Path {
Jeongik Chad5fe8782021-07-08 01:13:11 +09003033 return module.installFile
Ulya Trafimovich31e444e2020-08-14 17:32:16 +01003034}
3035
Ulya Trafimovichdbf31662020-12-17 12:07:54 +00003036// to satisfy UsesLibraryDependency interface
3037func (module *SdkLibraryImport) ClassLoaderContexts() dexpreopt.ClassLoaderContextMap {
3038 return nil
3039}
3040
Paul Duffineedc5d52020-06-12 17:46:39 +01003041// to satisfy apex.javaDependency interface
3042func (module *SdkLibraryImport) JacocoReportClassesFile() android.Path {
3043 if module.implLibraryModule == nil {
3044 return nil
3045 } else {
3046 return module.implLibraryModule.JacocoReportClassesFile()
3047 }
3048}
3049
3050// to satisfy apex.javaDependency interface
Colin Cross08dca382020-07-21 20:31:17 -07003051func (module *SdkLibraryImport) LintDepSets() LintDepSets {
3052 if module.implLibraryModule == nil {
3053 return LintDepSets{}
3054 } else {
3055 return module.implLibraryModule.LintDepSets()
3056 }
3057}
3058
Spandan Das17854f52022-01-14 21:19:14 +00003059func (module *SdkLibraryImport) GetStrictUpdatabilityLinting() bool {
Jaewoong Jung476b9d62021-05-10 15:30:00 -07003060 if module.implLibraryModule == nil {
3061 return false
3062 } else {
Spandan Das17854f52022-01-14 21:19:14 +00003063 return module.implLibraryModule.GetStrictUpdatabilityLinting()
Jaewoong Jung476b9d62021-05-10 15:30:00 -07003064 }
3065}
3066
Spandan Das17854f52022-01-14 21:19:14 +00003067func (module *SdkLibraryImport) SetStrictUpdatabilityLinting(strictLinting bool) {
Jaewoong Jung476b9d62021-05-10 15:30:00 -07003068 if module.implLibraryModule != nil {
Spandan Das17854f52022-01-14 21:19:14 +00003069 module.implLibraryModule.SetStrictUpdatabilityLinting(strictLinting)
Jaewoong Jung476b9d62021-05-10 15:30:00 -07003070 }
3071}
3072
Colin Cross08dca382020-07-21 20:31:17 -07003073// to satisfy apex.javaDependency interface
Paul Duffineedc5d52020-06-12 17:46:39 +01003074func (module *SdkLibraryImport) Stem() string {
3075 return module.BaseModuleName()
Colin Cross79c7c262019-04-17 11:11:46 -07003076}
Jiyong Parke3833882020-02-17 17:28:10 +09003077
Paul Duffin44b481b2020-06-17 16:59:43 +01003078var _ ApexDependency = (*SdkLibraryImport)(nil)
3079
3080// to satisfy java.ApexDependency interface
3081func (module *SdkLibraryImport) HeaderJars() android.Paths {
3082 if module.implLibraryModule == nil {
3083 return nil
3084 } else {
3085 return module.implLibraryModule.HeaderJars()
3086 }
3087}
3088
3089// to satisfy java.ApexDependency interface
3090func (module *SdkLibraryImport) ImplementationAndResourcesJars() android.Paths {
3091 if module.implLibraryModule == nil {
3092 return nil
3093 } else {
3094 return module.implLibraryModule.ImplementationAndResourcesJars()
3095 }
3096}
3097
Jiakai Zhang204356f2021-09-09 08:12:46 +00003098// to satisfy java.DexpreopterInterface interface
3099func (module *SdkLibraryImport) IsInstallable() bool {
3100 return true
3101}
3102
Paul Duffinfef55002021-06-17 14:56:05 +01003103var _ android.RequiredFilesFromPrebuiltApex = (*SdkLibraryImport)(nil)
3104
Paul Duffinb4bbf2c2021-06-17 15:59:07 +01003105func (module *SdkLibraryImport) RequiredFilesFromPrebuiltApex(ctx android.BaseModuleContext) []string {
Paul Duffinfef55002021-06-17 14:56:05 +01003106 name := module.BaseModuleName()
Jiakai Zhang81e46812023-02-08 21:56:07 +08003107 return requiredFilesFromPrebuiltApexForImport(name, &module.dexpreopter)
Paul Duffinfef55002021-06-17 14:56:05 +01003108}
3109
Spandan Das2ea84dd2024-01-25 22:12:50 +00003110func (j *SdkLibraryImport) UseProfileGuidedDexpreopt() bool {
3111 return proptools.Bool(j.importDexpreoptProperties.Dex_preopt.Profile_guided)
3112}
3113
Jiyong Parke3833882020-02-17 17:28:10 +09003114// java_sdk_library_xml
Jiyong Parke3833882020-02-17 17:28:10 +09003115type sdkLibraryXml struct {
3116 android.ModuleBase
3117 android.DefaultableModuleBase
3118 android.ApexModuleBase
3119
3120 properties sdkLibraryXmlProperties
3121
3122 outputFilePath android.OutputPath
3123 installDirPath android.InstallPath
Colin Cross56a83212020-09-15 18:30:11 -07003124
3125 hideApexVariantFromMake bool
Jiyong Parke3833882020-02-17 17:28:10 +09003126}
3127
3128type sdkLibraryXmlProperties struct {
3129 // canonical name of the lib
3130 Lib_name *string
Pedro Loureiro9956e5e2021-09-07 17:21:59 +00003131
3132 // Signals that this shared library is part of the bootclasspath starting
3133 // on the version indicated in this attribute.
3134 //
3135 // This will make platforms at this level and above to ignore
3136 // <uses-library> tags with this library name because the library is already
3137 // available
3138 On_bootclasspath_since *string
3139
3140 // Signals that this shared library was part of the bootclasspath before
3141 // (but not including) the version indicated in this attribute.
3142 //
3143 // The system will automatically add a <uses-library> tag with this library to
3144 // apps that target any SDK less than the version indicated in this attribute.
3145 On_bootclasspath_before *string
3146
3147 // Indicates that PackageManager should ignore this shared library if the
3148 // platform is below the version indicated in this attribute.
3149 //
3150 // This means that the device won't recognise this library as installed.
3151 Min_device_sdk *string
3152
3153 // Indicates that PackageManager should ignore this shared library if the
3154 // platform is above the version indicated in this attribute.
3155 //
3156 // This means that the device won't recognise this library as installed.
3157 Max_device_sdk *string
Pedro Loureiroc3621422021-09-28 15:40:23 +00003158
3159 // The SdkLibrary's min api level as a string
3160 //
3161 // This value comes from the ApiLevel of the MinSdkVersion property.
3162 Sdk_library_min_api_level *string
Jamie Garsidee570ace2023-11-27 12:07:36 +00003163
3164 // Uses-libs dependencies that the shared library requires to work correctly.
3165 //
3166 // This will add dependency="foo:bar" to the <library> section.
3167 Uses_libs_dependencies []string
Jiyong Parke3833882020-02-17 17:28:10 +09003168}
3169
3170// java_sdk_library_xml builds the permission xml file for a java_sdk_library.
3171// Not to be used directly by users. java_sdk_library internally uses this.
3172func sdkLibraryXmlFactory() android.Module {
3173 module := &sdkLibraryXml{}
3174
3175 module.AddProperties(&module.properties)
3176
3177 android.InitApexModule(module)
3178 android.InitAndroidArchModule(module, android.DeviceSupported, android.MultilibCommon)
3179
3180 return module
3181}
3182
Colin Crossaede88c2020-08-11 12:17:01 -07003183func (module *sdkLibraryXml) UniqueApexVariations() bool {
3184 // sdkLibraryXml needs a unique variation per APEX because the generated XML file contains the path to the
3185 // mounted APEX, which contains the name of the APEX.
3186 return true
3187}
3188
Jiyong Parke3833882020-02-17 17:28:10 +09003189// from android.PrebuiltEtcModule
Jooyung Han0703fd82020-08-26 22:11:53 +09003190func (module *sdkLibraryXml) BaseDir() string {
3191 return "etc"
3192}
3193
3194// from android.PrebuiltEtcModule
Jiyong Parke3833882020-02-17 17:28:10 +09003195func (module *sdkLibraryXml) SubDir() string {
3196 return "permissions"
3197}
3198
ThiƩbaud Weksteen00e8b312024-03-18 14:06:00 +11003199var _ etc.PrebuiltEtcModule = (*sdkLibraryXml)(nil)
3200
Jiyong Parke3833882020-02-17 17:28:10 +09003201// from android.ApexModule
3202func (module *sdkLibraryXml) AvailableFor(what string) bool {
3203 return true
3204}
3205
3206func (module *sdkLibraryXml) DepsMutator(ctx android.BottomUpMutatorContext) {
3207 // do nothing
3208}
3209
Jiyong Park45bf82e2020-12-15 22:29:02 +09003210var _ android.ApexModule = (*sdkLibraryXml)(nil)
3211
3212// Implements android.ApexModule
Dan Albertc8060532020-07-22 22:32:17 -07003213func (module *sdkLibraryXml) ShouldSupportSdkVersion(ctx android.BaseModuleContext,
3214 sdkVersion android.ApiLevel) error {
Jooyung Han749dc692020-04-15 11:03:39 +09003215 // sdkLibraryXml doesn't need to be checked separately because java_sdk_library is checked
3216 return nil
3217}
3218
Jiyong Parke3833882020-02-17 17:28:10 +09003219// File path to the runtime implementation library
Colin Cross56a83212020-09-15 18:30:11 -07003220func (module *sdkLibraryXml) implPath(ctx android.ModuleContext) string {
Jiyong Parke3833882020-02-17 17:28:10 +09003221 implName := proptools.String(module.properties.Lib_name)
Colin Crossff694a82023-12-13 15:54:49 -08003222 if apexInfo, _ := android.ModuleProvider(ctx, android.ApexInfoProvider); !apexInfo.IsForPlatform() {
Colin Crosse07f2312020-08-13 11:24:56 -07003223 // TODO(b/146468504): ApexVariationName() is only a soong module name, not apex name.
Jiyong Parke3833882020-02-17 17:28:10 +09003224 // In most cases, this works fine. But when apex_name is set or override_apex is used
3225 // this can be wrong.
Spandan Das33bbeb22024-06-18 23:28:25 +00003226 return fmt.Sprintf("/apex/%s/javalib/%s.jar", apexInfo.BaseApexName, implName)
Jiyong Parke3833882020-02-17 17:28:10 +09003227 }
3228 partition := "system"
3229 if module.SocSpecific() {
3230 partition = "vendor"
3231 } else if module.DeviceSpecific() {
3232 partition = "odm"
3233 } else if module.ProductSpecific() {
3234 partition = "product"
3235 } else if module.SystemExtSpecific() {
3236 partition = "system_ext"
3237 }
3238 return "/" + partition + "/framework/" + implName + ".jar"
3239}
3240
Pedro Loureiro9956e5e2021-09-07 17:21:59 +00003241func formattedOptionalSdkLevelAttribute(ctx android.ModuleContext, attrName string, value *string) string {
3242 if value == nil {
3243 return ""
3244 }
3245 apiLevel, err := android.ApiLevelFromUser(ctx, *value)
3246 if err != nil {
Pedro Loureiroba6682f2021-10-29 09:32:32 +00003247 // attributes in bp files have underscores but in the xml have dashes.
3248 ctx.PropertyErrorf(strings.ReplaceAll(attrName, "-", "_"), err.Error())
Pedro Loureiro9956e5e2021-09-07 17:21:59 +00003249 return ""
3250 }
Pedro Loureirob638c622021-12-22 15:28:05 +00003251 if apiLevel.IsCurrent() {
3252 // passing "current" would always mean a future release, never the current (or the current in
3253 // progress) which means some conditions would never be triggered.
3254 ctx.PropertyErrorf(strings.ReplaceAll(attrName, "-", "_"),
3255 `"current" is not an allowed value for this attribute`)
3256 return ""
3257 }
Pedro Loureiro48991222022-06-17 20:01:21 +00003258 // "safeValue" is safe because it translates finalized codenames to a string
3259 // with their SDK int.
3260 safeValue := apiLevel.String()
3261 return formattedOptionalAttribute(attrName, &safeValue)
Pedro Loureiro9956e5e2021-09-07 17:21:59 +00003262}
3263
3264// formats an attribute for the xml permissions file if the value is not null
3265// returns empty string otherwise
3266func formattedOptionalAttribute(attrName string, value *string) string {
3267 if value == nil {
3268 return ""
3269 }
Paul Duffin1816cde2024-04-10 10:58:21 +01003270 return fmt.Sprintf(" %s=\"%s\"\n", attrName, *value)
Pedro Loureiro9956e5e2021-09-07 17:21:59 +00003271}
3272
Jamie Garsidee570ace2023-11-27 12:07:36 +00003273func formattedDependenciesAttribute(dependencies []string) string {
3274 if dependencies == nil {
3275 return ""
3276 }
Paul Duffin1816cde2024-04-10 10:58:21 +01003277 return fmt.Sprintf(" dependency=\"%s\"\n", strings.Join(dependencies, ":"))
Jamie Garsidee570ace2023-11-27 12:07:36 +00003278}
3279
Pedro Loureiro9956e5e2021-09-07 17:21:59 +00003280func (module *sdkLibraryXml) permissionsContents(ctx android.ModuleContext) string {
3281 libName := proptools.String(module.properties.Lib_name)
3282 libNameAttr := formattedOptionalAttribute("name", &libName)
3283 filePath := module.implPath(ctx)
3284 filePathAttr := formattedOptionalAttribute("file", &filePath)
Pedro Loureiroba6682f2021-10-29 09:32:32 +00003285 implicitFromAttr := formattedOptionalSdkLevelAttribute(ctx, "on-bootclasspath-since", module.properties.On_bootclasspath_since)
3286 implicitUntilAttr := formattedOptionalSdkLevelAttribute(ctx, "on-bootclasspath-before", module.properties.On_bootclasspath_before)
3287 minSdkAttr := formattedOptionalSdkLevelAttribute(ctx, "min-device-sdk", module.properties.Min_device_sdk)
3288 maxSdkAttr := formattedOptionalSdkLevelAttribute(ctx, "max-device-sdk", module.properties.Max_device_sdk)
Jamie Garsidee570ace2023-11-27 12:07:36 +00003289 dependenciesAttr := formattedDependenciesAttribute(module.properties.Uses_libs_dependencies)
Pedro Loureiro196d3e62021-12-22 19:53:01 +00003290 // <library> is understood in all android versions whereas <apex-library> is only understood from API T (and ignored before that).
3291 // 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 +00003292 var libraryTag string
3293 if module.properties.Min_device_sdk != nil {
Paul Duffin1816cde2024-04-10 10:58:21 +01003294 libraryTag = " <apex-library\n"
Pedro Loureiroc3621422021-09-28 15:40:23 +00003295 } else {
Paul Duffin1816cde2024-04-10 10:58:21 +01003296 libraryTag = " <library\n"
Pedro Loureiroc3621422021-09-28 15:40:23 +00003297 }
Pedro Loureiro9956e5e2021-09-07 17:21:59 +00003298
3299 return strings.Join([]string{
Paul Duffin1816cde2024-04-10 10:58:21 +01003300 "<?xml version=\"1.0\" encoding=\"utf-8\"?>\n",
3301 "<!-- Copyright (C) 2018 The Android Open Source Project\n",
3302 "\n",
3303 " Licensed under the Apache License, Version 2.0 (the \"License\");\n",
3304 " you may not use this file except in compliance with the License.\n",
3305 " You may obtain a copy of the License at\n",
3306 "\n",
3307 " http://www.apache.org/licenses/LICENSE-2.0\n",
3308 "\n",
3309 " Unless required by applicable law or agreed to in writing, software\n",
3310 " distributed under the License is distributed on an \"AS IS\" BASIS,\n",
3311 " WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\n",
3312 " See the License for the specific language governing permissions and\n",
3313 " limitations under the License.\n",
3314 "-->\n",
3315 "<permissions>\n",
Pedro Loureiroc3621422021-09-28 15:40:23 +00003316 libraryTag,
Pedro Loureiro9956e5e2021-09-07 17:21:59 +00003317 libNameAttr,
3318 filePathAttr,
3319 implicitFromAttr,
3320 implicitUntilAttr,
3321 minSdkAttr,
3322 maxSdkAttr,
Jamie Garsidee570ace2023-11-27 12:07:36 +00003323 dependenciesAttr,
Paul Duffin1816cde2024-04-10 10:58:21 +01003324 " />\n",
3325 "</permissions>\n",
3326 }, "")
Pedro Loureiro9956e5e2021-09-07 17:21:59 +00003327}
3328
Jiyong Parke3833882020-02-17 17:28:10 +09003329func (module *sdkLibraryXml) GenerateAndroidBuildActions(ctx android.ModuleContext) {
Colin Crossff694a82023-12-13 15:54:49 -08003330 apexInfo, _ := android.ModuleProvider(ctx, android.ApexInfoProvider)
3331 module.hideApexVariantFromMake = !apexInfo.IsForPlatform()
Colin Cross56a83212020-09-15 18:30:11 -07003332
Jiyong Parke3833882020-02-17 17:28:10 +09003333 libName := proptools.String(module.properties.Lib_name)
Pedro Loureiroc3621422021-09-28 15:40:23 +00003334 module.selfValidate(ctx)
Pedro Loureiro9956e5e2021-09-07 17:21:59 +00003335 xmlContent := module.permissionsContents(ctx)
Jiyong Parke3833882020-02-17 17:28:10 +09003336
3337 module.outputFilePath = android.PathForModuleOut(ctx, libName+".xml").OutputPath
Paul Duffin1816cde2024-04-10 10:58:21 +01003338 android.WriteFileRuleVerbatim(ctx, module.outputFilePath, xmlContent)
Jiyong Parke3833882020-02-17 17:28:10 +09003339
3340 module.installDirPath = android.PathForModuleInstall(ctx, "etc", module.SubDir())
Jeongik Cha00e09912024-04-23 05:07:13 +09003341 ctx.PackageFile(module.installDirPath, libName+".xml", module.outputFilePath)
mrziwange2346b82024-06-10 15:09:45 -07003342
3343 ctx.SetOutputFiles(android.OutputPaths{module.outputFilePath}.Paths(), "")
Jiyong Parke3833882020-02-17 17:28:10 +09003344}
3345
3346func (module *sdkLibraryXml) AndroidMkEntries() []android.AndroidMkEntries {
Colin Cross56a83212020-09-15 18:30:11 -07003347 if module.hideApexVariantFromMake {
satayev8f088b02021-12-06 11:40:46 +00003348 return []android.AndroidMkEntries{{
Jiyong Parke3833882020-02-17 17:28:10 +09003349 Disabled: true,
3350 }}
3351 }
3352
satayev8f088b02021-12-06 11:40:46 +00003353 return []android.AndroidMkEntries{{
Jiyong Parke3833882020-02-17 17:28:10 +09003354 Class: "ETC",
3355 OutputFile: android.OptionalPathForPath(module.outputFilePath),
3356 ExtraEntries: []android.AndroidMkExtraEntriesFunc{
Colin Crossaa255532020-07-03 13:18:24 -07003357 func(ctx android.AndroidMkExtraEntriesContext, entries *android.AndroidMkEntries) {
Jiyong Parke3833882020-02-17 17:28:10 +09003358 entries.SetString("LOCAL_MODULE_TAGS", "optional")
Colin Crossc68db4b2021-11-11 18:59:15 -08003359 entries.SetString("LOCAL_MODULE_PATH", module.installDirPath.String())
Jiyong Parke3833882020-02-17 17:28:10 +09003360 entries.SetString("LOCAL_INSTALLED_MODULE_STEM", module.outputFilePath.Base())
3361 },
3362 },
3363 }}
3364}
Paul Duffindd46f712020-02-10 13:37:10 +00003365
Pedro Loureiroc3621422021-09-28 15:40:23 +00003366func (module *sdkLibraryXml) selfValidate(ctx android.ModuleContext) {
3367 module.validateAtLeastTAttributes(ctx)
3368 module.validateMinAndMaxDeviceSdk(ctx)
3369 module.validateMinMaxDeviceSdkAndModuleMinSdk(ctx)
3370 module.validateOnBootclasspathBeforeRequirements(ctx)
3371}
3372
3373func (module *sdkLibraryXml) validateAtLeastTAttributes(ctx android.ModuleContext) {
3374 t := android.ApiLevelOrPanic(ctx, "Tiramisu")
3375 module.attrAtLeastT(ctx, t, module.properties.Min_device_sdk, "min_device_sdk")
3376 module.attrAtLeastT(ctx, t, module.properties.Max_device_sdk, "max_device_sdk")
3377 module.attrAtLeastT(ctx, t, module.properties.On_bootclasspath_before, "on_bootclasspath_before")
3378 module.attrAtLeastT(ctx, t, module.properties.On_bootclasspath_since, "on_bootclasspath_since")
3379}
3380
3381func (module *sdkLibraryXml) attrAtLeastT(ctx android.ModuleContext, t android.ApiLevel, attr *string, attrName string) {
3382 if attr != nil {
3383 if level, err := android.ApiLevelFromUser(ctx, *attr); err == nil {
3384 // we will inform the user of invalid inputs when we try to write the
3385 // permissions xml file so we don't need to do it here
3386 if t.GreaterThan(level) {
3387 ctx.PropertyErrorf(attrName, "Attribute value needs to be at least T")
3388 }
3389 }
3390 }
3391}
3392
3393func (module *sdkLibraryXml) validateMinAndMaxDeviceSdk(ctx android.ModuleContext) {
3394 if module.properties.Min_device_sdk != nil && module.properties.Max_device_sdk != nil {
3395 min, minErr := android.ApiLevelFromUser(ctx, *module.properties.Min_device_sdk)
3396 max, maxErr := android.ApiLevelFromUser(ctx, *module.properties.Max_device_sdk)
3397 if minErr == nil && maxErr == nil {
3398 // we will inform the user of invalid inputs when we try to write the
3399 // permissions xml file so we don't need to do it here
3400 if min.GreaterThan(max) {
3401 ctx.ModuleErrorf("min_device_sdk can't be greater than max_device_sdk")
3402 }
3403 }
3404 }
3405}
3406
3407func (module *sdkLibraryXml) validateMinMaxDeviceSdkAndModuleMinSdk(ctx android.ModuleContext) {
3408 moduleMinApi := android.ApiLevelOrPanic(ctx, *module.properties.Sdk_library_min_api_level)
3409 if module.properties.Min_device_sdk != nil {
3410 api, err := android.ApiLevelFromUser(ctx, *module.properties.Min_device_sdk)
3411 if err == nil {
3412 if moduleMinApi.GreaterThan(api) {
3413 ctx.PropertyErrorf("min_device_sdk", "Can't be less than module's min sdk (%s)", moduleMinApi)
3414 }
3415 }
3416 }
3417 if module.properties.Max_device_sdk != nil {
3418 api, err := android.ApiLevelFromUser(ctx, *module.properties.Max_device_sdk)
3419 if err == nil {
3420 if moduleMinApi.GreaterThan(api) {
3421 ctx.PropertyErrorf("max_device_sdk", "Can't be less than module's min sdk (%s)", moduleMinApi)
3422 }
3423 }
3424 }
3425}
3426
3427func (module *sdkLibraryXml) validateOnBootclasspathBeforeRequirements(ctx android.ModuleContext) {
3428 moduleMinApi := android.ApiLevelOrPanic(ctx, *module.properties.Sdk_library_min_api_level)
3429 if module.properties.On_bootclasspath_before != nil {
3430 t := android.ApiLevelOrPanic(ctx, "Tiramisu")
3431 // if we use the attribute, then we need to do this validation
3432 if moduleMinApi.LessThan(t) {
3433 // if minAPi is < T, then we need to have min_device_sdk (which only accepts T+)
3434 if module.properties.Min_device_sdk == nil {
3435 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")
3436 }
3437 }
3438 }
3439}
3440
Paul Duffindd46f712020-02-10 13:37:10 +00003441type sdkLibrarySdkMemberType struct {
3442 android.SdkMemberTypeBase
3443}
3444
Paul Duffin296701e2021-07-14 10:29:36 +01003445func (s *sdkLibrarySdkMemberType) AddDependencies(ctx android.SdkDependencyContext, dependencyTag blueprint.DependencyTag, names []string) {
3446 ctx.AddVariationDependencies(nil, dependencyTag, names...)
Paul Duffindd46f712020-02-10 13:37:10 +00003447}
3448
3449func (s *sdkLibrarySdkMemberType) IsInstance(module android.Module) bool {
3450 _, ok := module.(*SdkLibrary)
3451 return ok
3452}
3453
3454func (s *sdkLibrarySdkMemberType) AddPrebuiltModule(ctx android.SdkMemberContext, member android.SdkMember) android.BpModule {
3455 return ctx.SnapshotBuilder().AddPrebuiltModule(member, "java_sdk_library_import")
3456}
3457
3458func (s *sdkLibrarySdkMemberType) CreateVariantPropertiesStruct() android.SdkMemberProperties {
3459 return &sdkLibrarySdkMemberProperties{}
3460}
3461
Paul Duffin976b0e52021-04-27 23:20:26 +01003462var javaSdkLibrarySdkMemberType = &sdkLibrarySdkMemberType{
3463 android.SdkMemberTypeBase{
3464 PropertyName: "java_sdk_libs",
3465 SupportsSdk: true,
3466 },
3467}
3468
Paul Duffindd46f712020-02-10 13:37:10 +00003469type sdkLibrarySdkMemberProperties struct {
3470 android.SdkMemberPropertiesBase
3471
Paul Duffine8409952022-09-22 16:24:46 +01003472 // Stem name for files in the sdk snapshot.
3473 //
3474 // This is used to construct the path names of various sdk library files in the sdk snapshot to
3475 // make sure that they match the finalized versions of those files in prebuilts/sdk.
3476 //
3477 // This property is marked as keep so that it will be kept in all instances of this struct, will
3478 // not be cleared but will be copied to common structs. That is needed because this field is used
3479 // to construct many file names for other parts of this struct and so it needs to be present in
3480 // all structs. If it was not marked as keep then it would be cleared in some structs and so would
3481 // be unavailable for generating file names if there were other properties that were still set.
3482 Stem string `sdk:"keep"`
3483
Paul Duffindd46f712020-02-10 13:37:10 +00003484 // Scope to per scope properties.
Paul Duffin106a3a42022-01-27 16:39:06 +00003485 Scopes map[*apiScope]*scopeProperties
Paul Duffindd46f712020-02-10 13:37:10 +00003486
Paul Duffin3d1248c2020-04-09 00:10:17 +01003487 // The Java stubs source files.
3488 Stub_srcs []string
Paul Duffinf7a64332020-05-13 16:54:55 +01003489
3490 // The naming scheme.
3491 Naming_scheme *string
Paul Duffind7eb1c22020-05-26 20:57:10 +01003492
3493 // True if the java_sdk_library_import is for a shared library, false
3494 // otherwise.
3495 Shared_library *bool
Paul Duffina2ae7e02020-09-11 11:55:00 +01003496
Paul Duffin1267d872021-04-16 17:21:36 +01003497 // True if the stub imports should produce dex jars.
3498 Compile_dex *bool
3499
Paul Duffina2ae7e02020-09-11 11:55:00 +01003500 // The paths to the doctag files to add to the prebuilt.
3501 Doctag_paths android.Paths
Paul Duffin869de142021-07-15 14:14:41 +01003502
3503 Permitted_packages []string
Pedro Loureiro9956e5e2021-09-07 17:21:59 +00003504
3505 // Signals that this shared library is part of the bootclasspath starting
3506 // on the version indicated in this attribute.
3507 //
3508 // This will make platforms at this level and above to ignore
3509 // <uses-library> tags with this library name because the library is already
3510 // available
3511 On_bootclasspath_since *string
3512
3513 // Signals that this shared library was part of the bootclasspath before
3514 // (but not including) the version indicated in this attribute.
3515 //
3516 // The system will automatically add a <uses-library> tag with this library to
3517 // apps that target any SDK less than the version indicated in this attribute.
3518 On_bootclasspath_before *string
3519
3520 // Indicates that PackageManager should ignore this shared library if the
3521 // platform is below the version indicated in this attribute.
3522 //
3523 // This means that the device won't recognise this library as installed.
3524 Min_device_sdk *string
3525
3526 // Indicates that PackageManager should ignore this shared library if the
3527 // platform is above the version indicated in this attribute.
3528 //
3529 // This means that the device won't recognise this library as installed.
3530 Max_device_sdk *string
Jiakai Zhang9c4dc192023-02-09 00:09:24 +08003531
3532 DexPreoptProfileGuided *bool `supported_build_releases:"UpsideDownCake+"`
Paul Duffindd46f712020-02-10 13:37:10 +00003533}
3534
3535type scopeProperties struct {
Paul Duffin1fd005d2020-04-09 01:08:11 +01003536 Jars android.Paths
3537 StubsSrcJar android.Path
3538 CurrentApiFile android.Path
3539 RemovedApiFile android.Path
Paul Duffine7babdb2022-02-10 13:06:54 +00003540 AnnotationsZip android.Path `supported_build_releases:"Tiramisu+"`
Paul Duffin1fd005d2020-04-09 01:08:11 +01003541 SdkVersion string
Paul Duffindd46f712020-02-10 13:37:10 +00003542}
3543
3544func (s *sdkLibrarySdkMemberProperties) PopulateFromVariant(ctx android.SdkMemberContext, variant android.Module) {
3545 sdk := variant.(*SdkLibrary)
3546
Paul Duffine8409952022-09-22 16:24:46 +01003547 // Copy the stem name for files in the sdk snapshot.
3548 s.Stem = sdk.distStem()
3549
Paul Duffin106a3a42022-01-27 16:39:06 +00003550 s.Scopes = make(map[*apiScope]*scopeProperties)
Jihoon Kang98aa8fa2024-06-07 11:06:57 +00003551 for _, apiScope := range AllApiScopes {
Paul Duffin803a9562020-05-20 11:52:25 +01003552 paths := sdk.findScopePaths(apiScope)
3553 if paths == nil {
3554 continue
3555 }
3556
Paul Duffindd46f712020-02-10 13:37:10 +00003557 jars := paths.stubsImplPath
3558 if len(jars) > 0 {
3559 properties := scopeProperties{}
3560 properties.Jars = jars
Paul Duffin780c5f42020-05-12 15:52:55 +01003561 properties.SdkVersion = sdk.sdkVersionForStubsLibrary(ctx.SdkModuleContext(), apiScope)
Paul Duffin0f8faff2020-05-20 16:18:00 +01003562 properties.StubsSrcJar = paths.stubsSrcJar.Path()
Paul Duffin10269f12020-06-19 18:39:55 +01003563 if paths.currentApiFilePath.Valid() {
3564 properties.CurrentApiFile = paths.currentApiFilePath.Path()
3565 }
3566 if paths.removedApiFilePath.Valid() {
3567 properties.RemovedApiFile = paths.removedApiFilePath.Path()
3568 }
Anton Hanssond78eb762021-09-21 15:25:12 +01003569 // The annotations zip is only available for modules that set annotations_enabled: true.
3570 if paths.annotationsZip.Valid() {
3571 properties.AnnotationsZip = paths.annotationsZip.Path()
3572 }
Paul Duffin106a3a42022-01-27 16:39:06 +00003573 s.Scopes[apiScope] = &properties
Paul Duffindd46f712020-02-10 13:37:10 +00003574 }
3575 }
3576
Paul Duffindfa131e2020-05-15 20:37:11 +01003577 s.Naming_scheme = sdk.commonSdkLibraryProperties.Naming_scheme
Paul Duffind7eb1c22020-05-26 20:57:10 +01003578 s.Shared_library = proptools.BoolPtr(sdk.sharedLibrary())
Paul Duffin1267d872021-04-16 17:21:36 +01003579 s.Compile_dex = sdk.dexProperties.Compile_dex
Paul Duffina2ae7e02020-09-11 11:55:00 +01003580 s.Doctag_paths = sdk.doctagPaths
Paul Duffin869de142021-07-15 14:14:41 +01003581 s.Permitted_packages = sdk.PermittedPackagesForUpdatableBootJars()
Pedro Loureiro9956e5e2021-09-07 17:21:59 +00003582 s.On_bootclasspath_since = sdk.commonSdkLibraryProperties.On_bootclasspath_since
3583 s.On_bootclasspath_before = sdk.commonSdkLibraryProperties.On_bootclasspath_before
3584 s.Min_device_sdk = sdk.commonSdkLibraryProperties.Min_device_sdk
3585 s.Max_device_sdk = sdk.commonSdkLibraryProperties.Max_device_sdk
Jiakai Zhang9c4dc192023-02-09 00:09:24 +08003586
Jihoon Kanga3a05462024-04-05 00:36:44 +00003587 implLibrary := sdk.getImplLibraryModule()
3588 if implLibrary != nil && implLibrary.dexpreopter.dexpreoptProperties.Dex_preopt_result.Profile_guided {
Jiakai Zhang9c4dc192023-02-09 00:09:24 +08003589 s.DexPreoptProfileGuided = proptools.BoolPtr(true)
3590 }
Paul Duffindd46f712020-02-10 13:37:10 +00003591}
3592
3593func (s *sdkLibrarySdkMemberProperties) AddToPropertySet(ctx android.SdkMemberContext, propertySet android.BpPropertySet) {
Paul Duffinf7a64332020-05-13 16:54:55 +01003594 if s.Naming_scheme != nil {
3595 propertySet.AddProperty("naming_scheme", proptools.String(s.Naming_scheme))
3596 }
Paul Duffind7eb1c22020-05-26 20:57:10 +01003597 if s.Shared_library != nil {
3598 propertySet.AddProperty("shared_library", *s.Shared_library)
3599 }
Paul Duffin1267d872021-04-16 17:21:36 +01003600 if s.Compile_dex != nil {
3601 propertySet.AddProperty("compile_dex", *s.Compile_dex)
3602 }
Paul Duffin869de142021-07-15 14:14:41 +01003603 if len(s.Permitted_packages) > 0 {
3604 propertySet.AddProperty("permitted_packages", s.Permitted_packages)
3605 }
Jiakai Zhang9c4dc192023-02-09 00:09:24 +08003606 dexPreoptSet := propertySet.AddPropertySet("dex_preopt")
3607 if s.DexPreoptProfileGuided != nil {
3608 dexPreoptSet.AddProperty("profile_guided", proptools.Bool(s.DexPreoptProfileGuided))
3609 }
Paul Duffinf7a64332020-05-13 16:54:55 +01003610
Paul Duffine8409952022-09-22 16:24:46 +01003611 stem := s.Stem
3612
Jihoon Kang98aa8fa2024-06-07 11:06:57 +00003613 for _, apiScope := range AllApiScopes {
Paul Duffindd46f712020-02-10 13:37:10 +00003614 if properties, ok := s.Scopes[apiScope]; ok {
Paul Duffin6b836ba2020-05-13 19:19:49 +01003615 scopeSet := propertySet.AddPropertySet(apiScope.propertyName)
Paul Duffindd46f712020-02-10 13:37:10 +00003616
Paul Duffin958806b2022-05-16 13:10:47 +00003617 scopeDir := apiScope.snapshotRelativeDir()
Paul Duffin3d1248c2020-04-09 00:10:17 +01003618
Paul Duffindd46f712020-02-10 13:37:10 +00003619 var jars []string
3620 for _, p := range properties.Jars {
Paul Duffine8409952022-09-22 16:24:46 +01003621 dest := filepath.Join(scopeDir, stem+"-stubs.jar")
Paul Duffindd46f712020-02-10 13:37:10 +00003622 ctx.SnapshotBuilder().CopyToSnapshot(p, dest)
3623 jars = append(jars, dest)
3624 }
3625 scopeSet.AddProperty("jars", jars)
3626
Paul Duffin22628d52021-05-12 23:13:22 +01003627 if ctx.SdkModuleContext().Config().IsEnvTrue("SOONG_SDK_SNAPSHOT_USE_SRCJAR") {
3628 // Copy the stubs source jar into the snapshot zip as is.
Paul Duffine8409952022-09-22 16:24:46 +01003629 srcJarSnapshotPath := filepath.Join(scopeDir, stem+".srcjar")
Paul Duffin22628d52021-05-12 23:13:22 +01003630 ctx.SnapshotBuilder().CopyToSnapshot(properties.StubsSrcJar, srcJarSnapshotPath)
3631 scopeSet.AddProperty("stub_srcs", []string{srcJarSnapshotPath})
3632 } else {
3633 // Merge the stubs source jar into the snapshot zip so that when it is unpacked
3634 // the source files are also unpacked.
Paul Duffine8409952022-09-22 16:24:46 +01003635 snapshotRelativeDir := filepath.Join(scopeDir, stem+"_stub_sources")
Paul Duffin22628d52021-05-12 23:13:22 +01003636 ctx.SnapshotBuilder().UnzipToSnapshot(properties.StubsSrcJar, snapshotRelativeDir)
3637 scopeSet.AddProperty("stub_srcs", []string{snapshotRelativeDir})
3638 }
Paul Duffin3d1248c2020-04-09 00:10:17 +01003639
Paul Duffin1fd005d2020-04-09 01:08:11 +01003640 if properties.CurrentApiFile != nil {
Paul Duffine8409952022-09-22 16:24:46 +01003641 currentApiSnapshotPath := apiScope.snapshotRelativeCurrentApiTxtPath(stem)
Paul Duffin1fd005d2020-04-09 01:08:11 +01003642 ctx.SnapshotBuilder().CopyToSnapshot(properties.CurrentApiFile, currentApiSnapshotPath)
3643 scopeSet.AddProperty("current_api", currentApiSnapshotPath)
3644 }
3645
3646 if properties.RemovedApiFile != nil {
Paul Duffine8409952022-09-22 16:24:46 +01003647 removedApiSnapshotPath := apiScope.snapshotRelativeRemovedApiTxtPath(stem)
Paul Duffin3dbf9fd2020-06-02 13:00:02 +01003648 ctx.SnapshotBuilder().CopyToSnapshot(properties.RemovedApiFile, removedApiSnapshotPath)
Paul Duffin1fd005d2020-04-09 01:08:11 +01003649 scopeSet.AddProperty("removed_api", removedApiSnapshotPath)
3650 }
3651
Anton Hanssond78eb762021-09-21 15:25:12 +01003652 if properties.AnnotationsZip != nil {
Paul Duffine8409952022-09-22 16:24:46 +01003653 annotationsSnapshotPath := filepath.Join(scopeDir, stem+"_annotations.zip")
Anton Hanssond78eb762021-09-21 15:25:12 +01003654 ctx.SnapshotBuilder().CopyToSnapshot(properties.AnnotationsZip, annotationsSnapshotPath)
3655 scopeSet.AddProperty("annotations", annotationsSnapshotPath)
3656 }
3657
Paul Duffindd46f712020-02-10 13:37:10 +00003658 if properties.SdkVersion != "" {
3659 scopeSet.AddProperty("sdk_version", properties.SdkVersion)
3660 }
3661 }
3662 }
3663
Paul Duffina2ae7e02020-09-11 11:55:00 +01003664 if len(s.Doctag_paths) > 0 {
3665 dests := []string{}
3666 for _, p := range s.Doctag_paths {
3667 dest := filepath.Join("doctags", p.Rel())
3668 ctx.SnapshotBuilder().CopyToSnapshot(p, dest)
3669 dests = append(dests, dest)
3670 }
3671 propertySet.AddProperty("doctag_files", dests)
3672 }
Paul Duffindd46f712020-02-10 13:37:10 +00003673}