blob: 031bde3c8609f7f2069e3a2883ab59833febf0e7 [file] [log] [blame]
Jiyong Parkc678ad32018-04-10 13:07:10 +09001// Copyright 2018 Google Inc. All rights reserved.
2//
3// Licensed under the Apache License, Version 2.0 (the "License");
4// you may not use this file except in compliance with the License.
5// You may obtain a copy of the License at
6//
7// http://www.apache.org/licenses/LICENSE-2.0
8//
9// Unless required by applicable law or agreed to in writing, software
10// distributed under the License is distributed on an "AS IS" BASIS,
11// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12// See the License for the specific language governing permissions and
13// limitations under the License.
14
15package java
16
17import (
Jihoon Kangee113282024-01-23 00:16:41 +000018 "errors"
Jiyong Parkc678ad32018-04-10 13:07:10 +090019 "fmt"
20 "path"
Sundong Ahn054b19a2018-10-19 13:46:09 +090021 "path/filepath"
Paul Duffin46a26a82020-04-07 19:27:04 +010022 "reflect"
Paul Duffin46dc45a2020-05-14 15:39:10 +010023 "regexp"
Jiyong Park82484c02018-04-23 21:41:26 +090024 "sort"
Jiyong Parkc678ad32018-04-10 13:07:10 +090025 "strings"
Jiyong Park82484c02018-04-23 21:41:26 +090026 "sync"
Jiyong Parkc678ad32018-04-10 13:07:10 +090027
Paul Duffind1b3a922020-01-22 11:57:20 +000028 "github.com/google/blueprint"
Jiyong Parkc678ad32018-04-10 13:07:10 +090029 "github.com/google/blueprint/proptools"
Paul Duffin46a26a82020-04-07 19:27:04 +010030
31 "android/soong/android"
Ulya Trafimovichdbf31662020-12-17 12:07:54 +000032 "android/soong/dexpreopt"
ThiƩbaud Weksteen00e8b312024-03-18 14:06:00 +110033 "android/soong/etc"
Jiyong Parkc678ad32018-04-10 13:07:10 +090034)
35
Jooyung Han58f26ab2019-12-18 15:34:32 +090036const (
Pedro Loureiro9956e5e2021-09-07 17:21:59 +000037 sdkXmlFileSuffix = ".xml"
Jiyong Parkc678ad32018-04-10 13:07:10 +090038)
39
Paul Duffind1b3a922020-01-22 11:57:20 +000040// A tag to associated a dependency with a specific api scope.
41type scopeDependencyTag struct {
42 blueprint.BaseDependencyTag
43 name string
44 apiScope *apiScope
Paul Duffinc8782502020-04-29 20:45:27 +010045
46 // Function for extracting appropriate path information from the dependency.
Colin Crossdcf71b22021-02-01 13:59:03 -080047 depInfoExtractor func(paths *scopePaths, ctx android.ModuleContext, dep android.Module) error
Paul Duffinc8782502020-04-29 20:45:27 +010048}
49
50// Extract tag specific information from the dependency.
51func (tag scopeDependencyTag) extractDepInfo(ctx android.ModuleContext, dep android.Module, paths *scopePaths) {
Colin Crossdcf71b22021-02-01 13:59:03 -080052 err := tag.depInfoExtractor(paths, ctx, dep)
Paul Duffinc8782502020-04-29 20:45:27 +010053 if err != nil {
54 ctx.ModuleErrorf("has an invalid {scopeDependencyTag: %s} dependency on module %s: %s", tag.name, ctx.OtherModuleName(dep), err.Error())
55 }
Paul Duffind1b3a922020-01-22 11:57:20 +000056}
57
Paul Duffin80342d72020-06-26 22:08:43 +010058var _ android.ReplaceSourceWithPrebuilt = (*scopeDependencyTag)(nil)
59
60func (tag scopeDependencyTag) ReplaceSourceWithPrebuilt() bool {
61 return false
62}
63
Paul Duffind1b3a922020-01-22 11:57:20 +000064// Provides information about an api scope, e.g. public, system, test.
65type apiScope struct {
66 // The name of the api scope, e.g. public, system, test
67 name string
68
Paul Duffin97b53b82020-05-05 14:40:52 +010069 // The api scope that this scope extends.
Paul Duffind0b9fca2022-09-30 18:11:41 +010070 //
71 // This organizes the scopes into an extension hierarchy.
72 //
73 // If set this means that the API provided by this scope includes the API provided by the scope
74 // set in this field.
Paul Duffin97b53b82020-05-05 14:40:52 +010075 extends *apiScope
76
Paul Duffind0b9fca2022-09-30 18:11:41 +010077 // The next api scope that a library that uses this scope can access.
78 //
79 // This organizes the scopes into an access hierarchy.
80 //
81 // If set this means that a library that can access this API can also access the API provided by
82 // the scope set in this field.
83 //
84 // A module that sets sdk_version: "<scope>_current" should have access to the <scope> API of
85 // every java_sdk_library that it depends on. If the library does not provide an API for <scope>
86 // then it will traverse up this access hierarchy to find an API that it does provide.
87 //
88 // If this is not set then it defaults to the scope set in extends.
89 canAccess *apiScope
90
Paul Duffin3375e352020-04-28 10:44:03 +010091 // The legacy enabled status for a specific scope can be dependent on other
92 // properties that have been specified on the library so it is provided by
93 // a function that can determine the status by examining those properties.
94 legacyEnabledStatus func(module *SdkLibrary) bool
95
96 // The default enabled status for non-legacy behavior, which is triggered by
97 // explicitly enabling at least one api scope.
98 defaultEnabledStatus bool
99
100 // Gets a pointer to the scope specific properties.
101 scopeSpecificProperties func(module *SdkLibrary) *ApiScopeProperties
102
Paul Duffin46a26a82020-04-07 19:27:04 +0100103 // The name of the field in the dynamically created structure.
104 fieldName string
105
Paul Duffin6b836ba2020-05-13 19:19:49 +0100106 // The name of the property in the java_sdk_library_import
107 propertyName string
108
Jihoon Kangb7431552024-01-22 19:40:08 +0000109 // The tag to use to depend on the prebuilt stubs library module
110 prebuiltStubsTag scopeDependencyTag
Paul Duffind1b3a922020-01-22 11:57:20 +0000111
Jihoon Kangbd093452023-12-26 19:08:01 +0000112 // The tag to use to depend on the everything stubs library module.
113 everythingStubsTag scopeDependencyTag
114
115 // The tag to use to depend on the exportable stubs library module.
116 exportableStubsTag scopeDependencyTag
117
Paul Duffin0ff08bd2020-04-29 13:30:54 +0100118 // The tag to use to depend on the stubs source module (if separate from the API module).
119 stubsSourceTag scopeDependencyTag
120
Paul Duffinc8782502020-04-29 20:45:27 +0100121 // The tag to use to depend on the stubs source and API module.
122 stubsSourceAndApiTag scopeDependencyTag
Paul Duffind1b3a922020-01-22 11:57:20 +0000123
Paul Duffin958806b2022-05-16 13:10:47 +0000124 // The tag to use to depend on the module that provides the latest version of the API .txt file.
125 latestApiModuleTag scopeDependencyTag
126
127 // The tag to use to depend on the module that provides the latest version of the API removed.txt
128 // file.
129 latestRemovedApiModuleTag scopeDependencyTag
130
Paul Duffind1b3a922020-01-22 11:57:20 +0000131 // The scope specific prefix to add to the api file base of "current.txt" or "removed.txt".
132 apiFilePrefix string
133
Paul Duffind0b9fca2022-09-30 18:11:41 +0100134 // The scope specific suffix to add to the sdk library module name to construct a scope specific
Paul Duffind1b3a922020-01-22 11:57:20 +0000135 // module name.
136 moduleSuffix string
137
Paul Duffind1b3a922020-01-22 11:57:20 +0000138 // SDK version that the stubs library is built against. Note that this is always
139 // *current. Older stubs library built with a numbered SDK version is created from
140 // the prebuilt jar.
141 sdkVersion string
Paul Duffin1fb487d2020-04-07 18:50:10 +0100142
Paul Duffin15f34ef2020-07-20 18:04:44 +0100143 // The annotation that identifies this API level, empty for the public API scope.
144 annotation string
145
Paul Duffin1fb487d2020-04-07 18:50:10 +0100146 // Extra arguments to pass to droidstubs for this scope.
Paul Duffin0ff08bd2020-04-29 13:30:54 +0100147 //
Paul Duffin15f34ef2020-07-20 18:04:44 +0100148 // This is not used directly but is used to construct the droidstubsArgs.
149 extraArgs []string
Paul Duffin0ff08bd2020-04-29 13:30:54 +0100150
Paul Duffin15f34ef2020-07-20 18:04:44 +0100151 // The args that must be passed to droidstubs to generate the API and stubs source
152 // for this scope, constructed dynamically by initApiScope().
Paul Duffin0ff08bd2020-04-29 13:30:54 +0100153 //
154 // The API only includes the additional members that this scope adds over the scope
155 // that it extends.
Paul Duffin15f34ef2020-07-20 18:04:44 +0100156 //
157 // The stubs source must include the definitions of everything that is in this
158 // api scope and all the scopes that this one extends.
159 droidstubsArgs []string
Paul Duffin0ff08bd2020-04-29 13:30:54 +0100160
Anton Hansson6478ac12020-05-02 11:19:36 +0100161 // Whether the api scope can be treated as unstable, and should skip compat checks.
162 unstable bool
Jihoon Kang1c92c3e2023-03-23 17:44:51 +0000163
164 // Represents the SDK kind of this scope.
165 kind android.SdkKind
Paul Duffind1b3a922020-01-22 11:57:20 +0000166}
167
168// Initialize a scope, creating and adding appropriate dependency tags
169func initApiScope(scope *apiScope) *apiScope {
Paul Duffinc8782502020-04-29 20:45:27 +0100170 name := scope.name
Paul Duffin46dc45a2020-05-14 15:39:10 +0100171 scopeByName[name] = scope
172 allScopeNames = append(allScopeNames, name)
Paul Duffin6b836ba2020-05-13 19:19:49 +0100173 scope.propertyName = strings.ReplaceAll(name, "-", "_")
174 scope.fieldName = proptools.FieldNameForProperty(scope.propertyName)
Jihoon Kangb7431552024-01-22 19:40:08 +0000175 scope.prebuiltStubsTag = scopeDependencyTag{
Paul Duffinc8782502020-04-29 20:45:27 +0100176 name: name + "-stubs",
177 apiScope: scope,
178 depInfoExtractor: (*scopePaths).extractStubsLibraryInfoFromDependency,
Paul Duffind1b3a922020-01-22 11:57:20 +0000179 }
Jihoon Kangbd093452023-12-26 19:08:01 +0000180 scope.everythingStubsTag = scopeDependencyTag{
181 name: name + "-stubs-everything",
182 apiScope: scope,
183 depInfoExtractor: (*scopePaths).extractEverythingStubsLibraryInfoFromDependency,
184 }
185 scope.exportableStubsTag = scopeDependencyTag{
186 name: name + "-stubs-exportable",
187 apiScope: scope,
188 depInfoExtractor: (*scopePaths).extractExportableStubsLibraryInfoFromDependency,
189 }
Paul Duffin0ff08bd2020-04-29 13:30:54 +0100190 scope.stubsSourceTag = scopeDependencyTag{
191 name: name + "-stubs-source",
192 apiScope: scope,
193 depInfoExtractor: (*scopePaths).extractStubsSourceInfoFromDep,
194 }
Paul Duffinc8782502020-04-29 20:45:27 +0100195 scope.stubsSourceAndApiTag = scopeDependencyTag{
196 name: name + "-stubs-source-and-api",
197 apiScope: scope,
198 depInfoExtractor: (*scopePaths).extractStubsSourceAndApiInfoFromApiStubsProvider,
Paul Duffind1b3a922020-01-22 11:57:20 +0000199 }
Paul Duffin958806b2022-05-16 13:10:47 +0000200 scope.latestApiModuleTag = scopeDependencyTag{
201 name: name + "-latest-api",
202 apiScope: scope,
203 depInfoExtractor: (*scopePaths).extractLatestApiPath,
204 }
205 scope.latestRemovedApiModuleTag = scopeDependencyTag{
206 name: name + "-latest-removed-api",
207 apiScope: scope,
208 depInfoExtractor: (*scopePaths).extractLatestRemovedApiPath,
209 }
Paul Duffin0ff08bd2020-04-29 13:30:54 +0100210
211 // To get the args needed to generate the stubs source append all the args from
212 // this scope and all the scopes it extends as each set of args adds additional
213 // members to the stubs.
Paul Duffin15f34ef2020-07-20 18:04:44 +0100214 var scopeSpecificArgs []string
215 if scope.annotation != "" {
216 scopeSpecificArgs = []string{"--show-annotation", scope.annotation}
Paul Duffin0ff08bd2020-04-29 13:30:54 +0100217 }
Paul Duffin15f34ef2020-07-20 18:04:44 +0100218 for s := scope; s != nil; s = s.extends {
219 scopeSpecificArgs = append(scopeSpecificArgs, s.extraArgs...)
Paul Duffin0ff08bd2020-04-29 13:30:54 +0100220
Paul Duffin15f34ef2020-07-20 18:04:44 +0100221 // Ensure that the generated stubs includes all the API elements from the API scope
222 // that this scope extends.
223 if s != scope && s.annotation != "" {
224 scopeSpecificArgs = append(scopeSpecificArgs, "--show-for-stub-purposes-annotation", s.annotation)
225 }
226 }
Paul Duffin0ff08bd2020-04-29 13:30:54 +0100227
Paul Duffind0b9fca2022-09-30 18:11:41 +0100228 // By default, a library that can access a scope can also access the scope it extends.
229 if scope.canAccess == nil {
230 scope.canAccess = scope.extends
231 }
232
Paul Duffin15f34ef2020-07-20 18:04:44 +0100233 // Escape any special characters in the arguments. This is needed because droidstubs
234 // passes these directly to the shell command.
235 scope.droidstubsArgs = proptools.ShellEscapeList(scopeSpecificArgs)
Paul Duffin0ff08bd2020-04-29 13:30:54 +0100236
Paul Duffind1b3a922020-01-22 11:57:20 +0000237 return scope
238}
239
Anton Hansson08f476b2021-04-07 15:32:19 +0100240func (scope *apiScope) stubsLibraryModuleNameSuffix() string {
241 return ".stubs" + scope.moduleSuffix
242}
243
Jihoon Kangfa4a90d2023-12-20 02:53:38 +0000244func (scope *apiScope) exportableStubsLibraryModuleNameSuffix() string {
245 return ".stubs.exportable" + scope.moduleSuffix
246}
247
Jihoon Kang1c92c3e2023-03-23 17:44:51 +0000248func (scope *apiScope) apiLibraryModuleName(baseName string) string {
249 return scope.stubsLibraryModuleName(baseName) + ".from-text"
250}
251
Jihoon Kang1147b312023-06-08 23:25:57 +0000252func (scope *apiScope) sourceStubLibraryModuleName(baseName string) string {
253 return scope.stubsLibraryModuleName(baseName) + ".from-source"
254}
255
Jihoon Kangfa4a90d2023-12-20 02:53:38 +0000256func (scope *apiScope) exportableSourceStubsLibraryModuleName(baseName string) string {
257 return scope.exportableStubsLibraryModuleName(baseName) + ".from-source"
258}
259
Paul Duffinc3091c82020-05-08 14:16:20 +0100260func (scope *apiScope) stubsLibraryModuleName(baseName string) string {
Anton Hansson08f476b2021-04-07 15:32:19 +0100261 return baseName + scope.stubsLibraryModuleNameSuffix()
Paul Duffind1b3a922020-01-22 11:57:20 +0000262}
263
Jihoon Kangfa4a90d2023-12-20 02:53:38 +0000264func (scope *apiScope) exportableStubsLibraryModuleName(baseName string) string {
265 return baseName + scope.exportableStubsLibraryModuleNameSuffix()
266}
267
Paul Duffinc8782502020-04-29 20:45:27 +0100268func (scope *apiScope) stubsSourceModuleName(baseName string) string {
Paul Duffindd9d0742020-05-08 15:52:37 +0100269 return baseName + ".stubs.source" + scope.moduleSuffix
Paul Duffind1b3a922020-01-22 11:57:20 +0000270}
271
Paul Duffin0ff08bd2020-04-29 13:30:54 +0100272func (scope *apiScope) apiModuleName(baseName string) string {
Paul Duffindd9d0742020-05-08 15:52:37 +0100273 return baseName + ".api" + scope.moduleSuffix
Paul Duffin0ff08bd2020-04-29 13:30:54 +0100274}
275
Paul Duffin3375e352020-04-28 10:44:03 +0100276func (scope *apiScope) String() string {
277 return scope.name
278}
279
Paul Duffin958806b2022-05-16 13:10:47 +0000280// snapshotRelativeDir returns the snapshot directory into which the files related to scopes will
281// be stored.
282func (scope *apiScope) snapshotRelativeDir() string {
283 return filepath.Join("sdk_library", scope.name)
284}
285
286// snapshotRelativeCurrentApiTxtPath returns the snapshot path to the API .txt file for the named
287// library.
288func (scope *apiScope) snapshotRelativeCurrentApiTxtPath(name string) string {
289 return filepath.Join(scope.snapshotRelativeDir(), name+".txt")
290}
291
292// snapshotRelativeRemovedApiTxtPath returns the snapshot path to the removed API .txt file for the
293// named library.
294func (scope *apiScope) snapshotRelativeRemovedApiTxtPath(name string) string {
295 return filepath.Join(scope.snapshotRelativeDir(), name+"-removed.txt")
296}
297
Paul Duffind1b3a922020-01-22 11:57:20 +0000298type apiScopes []*apiScope
299
300func (scopes apiScopes) Strings(accessor func(*apiScope) string) []string {
301 var list []string
302 for _, scope := range scopes {
303 list = append(list, accessor(scope))
304 }
305 return list
306}
307
Jihoon Kanga96a7b12023-09-20 23:43:32 +0000308// Method that maps the apiScopes properties to the index of each apiScopes elements.
309// apiScopes property to be used as the key can be specified with the input accessor.
310// Only a string property of apiScope can be used as the key of the map.
311func (scopes apiScopes) MapToIndex(accessor func(*apiScope) string) map[string]int {
312 ret := make(map[string]int)
313 for i, scope := range scopes {
314 ret[accessor(scope)] = i
315 }
316 return ret
317}
318
Jihoon Kang98aa8fa2024-06-07 11:06:57 +0000319func (scopes apiScopes) ConvertStubsLibraryExportableToEverything(name string) string {
320 for _, scope := range scopes {
321 if strings.HasSuffix(name, scope.exportableStubsLibraryModuleNameSuffix()) {
322 return strings.TrimSuffix(name, scope.exportableStubsLibraryModuleNameSuffix()) +
323 scope.stubsLibraryModuleNameSuffix()
324 }
325 }
326 return name
327}
328
Jiyong Parkc678ad32018-04-10 13:07:10 +0900329var (
Paul Duffin46dc45a2020-05-14 15:39:10 +0100330 scopeByName = make(map[string]*apiScope)
331 allScopeNames []string
Paul Duffind1b3a922020-01-22 11:57:20 +0000332 apiScopePublic = initApiScope(&apiScope{
Paul Duffin3375e352020-04-28 10:44:03 +0100333 name: "public",
334
335 // Public scope is enabled by default for both legacy and non-legacy modes.
336 legacyEnabledStatus: func(module *SdkLibrary) bool {
337 return true
338 },
339 defaultEnabledStatus: true,
340
341 scopeSpecificProperties: func(module *SdkLibrary) *ApiScopeProperties {
342 return &module.sdkLibraryProperties.Public
343 },
Paul Duffind1b3a922020-01-22 11:57:20 +0000344 sdkVersion: "current",
Jihoon Kang1c92c3e2023-03-23 17:44:51 +0000345 kind: android.SdkPublic,
Paul Duffind1b3a922020-01-22 11:57:20 +0000346 })
347 apiScopeSystem = initApiScope(&apiScope{
Paul Duffin3375e352020-04-28 10:44:03 +0100348 name: "system",
349 extends: apiScopePublic,
350 legacyEnabledStatus: (*SdkLibrary).generateTestAndSystemScopesByDefault,
351 scopeSpecificProperties: func(module *SdkLibrary) *ApiScopeProperties {
352 return &module.sdkLibraryProperties.System
353 },
Paul Duffin15f34ef2020-07-20 18:04:44 +0100354 apiFilePrefix: "system-",
355 moduleSuffix: ".system",
356 sdkVersion: "system_current",
357 annotation: "android.annotation.SystemApi(client=android.annotation.SystemApi.Client.PRIVILEGED_APPS)",
Jihoon Kang1c92c3e2023-03-23 17:44:51 +0000358 kind: android.SdkSystem,
Paul Duffind1b3a922020-01-22 11:57:20 +0000359 })
360 apiScopeTest = initApiScope(&apiScope{
Paul Duffin3375e352020-04-28 10:44:03 +0100361 name: "test",
Anton Hansson4fe970f2020-10-09 10:16:49 +0100362 extends: apiScopeSystem,
Paul Duffin3375e352020-04-28 10:44:03 +0100363 legacyEnabledStatus: (*SdkLibrary).generateTestAndSystemScopesByDefault,
364 scopeSpecificProperties: func(module *SdkLibrary) *ApiScopeProperties {
365 return &module.sdkLibraryProperties.Test
366 },
Paul Duffin15f34ef2020-07-20 18:04:44 +0100367 apiFilePrefix: "test-",
368 moduleSuffix: ".test",
369 sdkVersion: "test_current",
370 annotation: "android.annotation.TestApi",
371 unstable: true,
Jihoon Kang1c92c3e2023-03-23 17:44:51 +0000372 kind: android.SdkTest,
Paul Duffind1b3a922020-01-22 11:57:20 +0000373 })
Paul Duffin8f265b92020-04-28 14:13:56 +0100374 apiScopeModuleLib = initApiScope(&apiScope{
Paul Duffin6b836ba2020-05-13 19:19:49 +0100375 name: "module-lib",
Paul Duffin8f265b92020-04-28 14:13:56 +0100376 extends: apiScopeSystem,
Paul Duffin0c5bae52020-06-02 13:00:08 +0100377 // The module-lib scope is disabled by default in legacy mode.
Paul Duffin8f265b92020-04-28 14:13:56 +0100378 //
379 // Enabling this would break existing usages.
380 legacyEnabledStatus: func(module *SdkLibrary) bool {
381 return false
382 },
383 scopeSpecificProperties: func(module *SdkLibrary) *ApiScopeProperties {
384 return &module.sdkLibraryProperties.Module_lib
385 },
386 apiFilePrefix: "module-lib-",
387 moduleSuffix: ".module_lib",
388 sdkVersion: "module_current",
Paul Duffin15f34ef2020-07-20 18:04:44 +0100389 annotation: "android.annotation.SystemApi(client=android.annotation.SystemApi.Client.MODULE_LIBRARIES)",
Jihoon Kang1c92c3e2023-03-23 17:44:51 +0000390 kind: android.SdkModule,
Paul Duffin8f265b92020-04-28 14:13:56 +0100391 })
Paul Duffin0c5bae52020-06-02 13:00:08 +0100392 apiScopeSystemServer = initApiScope(&apiScope{
393 name: "system-server",
394 extends: apiScopePublic,
Paul Duffind0b9fca2022-09-30 18:11:41 +0100395
396 // The system-server scope can access the module-lib scope.
397 //
398 // A module that provides a system-server API is appended to the standard bootclasspath that is
399 // used by the system server. So, it should be able to access module-lib APIs provided by
400 // libraries on the bootclasspath.
401 canAccess: apiScopeModuleLib,
402
Paul Duffin0c5bae52020-06-02 13:00:08 +0100403 // The system-server scope is disabled by default in legacy mode.
404 //
405 // Enabling this would break existing usages.
406 legacyEnabledStatus: func(module *SdkLibrary) bool {
407 return false
408 },
409 scopeSpecificProperties: func(module *SdkLibrary) *ApiScopeProperties {
410 return &module.sdkLibraryProperties.System_server
411 },
412 apiFilePrefix: "system-server-",
413 moduleSuffix: ".system_server",
414 sdkVersion: "system_server_current",
Paul Duffin15f34ef2020-07-20 18:04:44 +0100415 annotation: "android.annotation.SystemApi(client=android.annotation.SystemApi.Client.SYSTEM_SERVER)",
416 extraArgs: []string{
417 "--hide-annotation", "android.annotation.Hide",
Paul Duffin0c5bae52020-06-02 13:00:08 +0100418 // com.android.* classes are okay in this interface"
Paul Duffin15f34ef2020-07-20 18:04:44 +0100419 "--hide", "InternalClasses",
Paul Duffin0c5bae52020-06-02 13:00:08 +0100420 },
Jihoon Kang1c92c3e2023-03-23 17:44:51 +0000421 kind: android.SdkSystemServer,
Paul Duffin0c5bae52020-06-02 13:00:08 +0100422 })
Jihoon Kang98aa8fa2024-06-07 11:06:57 +0000423 AllApiScopes = apiScopes{
Paul Duffind1b3a922020-01-22 11:57:20 +0000424 apiScopePublic,
425 apiScopeSystem,
426 apiScopeTest,
Paul Duffin8f265b92020-04-28 14:13:56 +0100427 apiScopeModuleLib,
Paul Duffin0c5bae52020-06-02 13:00:08 +0100428 apiScopeSystemServer,
Paul Duffind1b3a922020-01-22 11:57:20 +0000429 }
Jihoon Kang0c705a42023-08-02 06:44:57 +0000430 apiLibraryAdditionalProperties = map[string]struct {
431 FullApiSurfaceStubLib string
432 AdditionalApiContribution string
433 }{
434 "legacy.i18n.module.platform.api": {
435 FullApiSurfaceStubLib: "legacy.core.platform.api.stubs",
436 AdditionalApiContribution: "i18n.module.public.api.stubs.source.api.contribution",
437 },
438 "stable.i18n.module.platform.api": {
439 FullApiSurfaceStubLib: "stable.core.platform.api.stubs",
440 AdditionalApiContribution: "i18n.module.public.api.stubs.source.api.contribution",
441 },
442 "conscrypt.module.platform.api": {
443 FullApiSurfaceStubLib: "stable.core.platform.api.stubs",
444 AdditionalApiContribution: "conscrypt.module.public.api.stubs.source.api.contribution",
445 },
446 }
Jiyong Parkc678ad32018-04-10 13:07:10 +0900447)
448
Jiyong Park82484c02018-04-23 21:41:26 +0900449var (
450 javaSdkLibrariesLock sync.Mutex
451)
452
Jiyong Parkc678ad32018-04-10 13:07:10 +0900453// TODO: these are big features that are currently missing
Jiyong Park1be96912018-05-28 18:02:19 +0900454// 1) disallowing linking to the runtime shared lib
455// 2) HTML generation
Jiyong Parkc678ad32018-04-10 13:07:10 +0900456
457func init() {
Paul Duffin43dc1cc2019-12-19 11:18:54 +0000458 RegisterSdkLibraryBuildComponents(android.InitRegistrationContext)
Jiyong Parkc678ad32018-04-10 13:07:10 +0900459
Jiyong Park82484c02018-04-23 21:41:26 +0900460 android.RegisterMakeVarsProvider(pctx, func(ctx android.MakeVarsContext) {
461 javaSdkLibraries := javaSdkLibraries(ctx.Config())
462 sort.Strings(*javaSdkLibraries)
463 ctx.Strict("JAVA_SDK_LIBRARIES", strings.Join(*javaSdkLibraries, " "))
464 })
Paul Duffindd46f712020-02-10 13:37:10 +0000465
466 // Register sdk member types.
Paul Duffin976b0e52021-04-27 23:20:26 +0100467 android.RegisterSdkMemberType(javaSdkLibrarySdkMemberType)
Jiyong Parkc678ad32018-04-10 13:07:10 +0900468}
469
Paul Duffin43dc1cc2019-12-19 11:18:54 +0000470func RegisterSdkLibraryBuildComponents(ctx android.RegistrationContext) {
471 ctx.RegisterModuleType("java_sdk_library", SdkLibraryFactory)
472 ctx.RegisterModuleType("java_sdk_library_import", sdkLibraryImportFactory)
473}
474
Paul Duffin3375e352020-04-28 10:44:03 +0100475// Properties associated with each api scope.
476type ApiScopeProperties struct {
477 // Indicates whether the api surface is generated.
478 //
479 // If this is set for any scope then all scopes must explicitly specify if they
480 // are enabled. This is to prevent new usages from depending on legacy behavior.
481 //
482 // Otherwise, if this is not set for any scope then the default behavior is
483 // scope specific so please refer to the scope specific property documentation.
484 Enabled *bool
Paul Duffin87a05a32020-05-12 11:50:28 +0100485
486 // The sdk_version to use for building the stubs.
487 //
488 // If not specified then it will use an sdk_version determined as follows:
Trevor Radcliffedf8aa1f2021-11-04 14:25:39 +0000489 //
Paul Duffin87a05a32020-05-12 11:50:28 +0100490 // 1) If the sdk_version specified on the java_sdk_library is none then this
Trevor Radcliffedf8aa1f2021-11-04 14:25:39 +0000491 // will be none. This is used for java_sdk_library instances that are used
492 // to create stubs that contribute to the core_current sdk version.
493 // 2) Otherwise, it is assumed that this library extends but does not
494 // contribute directly to a specific sdk_version and so this uses the
495 // sdk_version appropriate for the api scope. e.g. public will use
496 // sdk_version: current, system will use sdk_version: system_current, etc.
Paul Duffin87a05a32020-05-12 11:50:28 +0100497 //
498 // This does not affect the sdk_version used for either generating the stubs source
499 // or the API file. They both have to use the same sdk_version as is used for
500 // compiling the implementation library.
501 Sdk_version *string
Mark White9421c4c2023-08-10 00:07:03 +0000502
503 // Extra libs used when compiling stubs for this scope.
504 Libs []string
Paul Duffin3375e352020-04-28 10:44:03 +0100505}
506
Jiyong Parkc678ad32018-04-10 13:07:10 +0900507type sdkLibraryProperties struct {
Anton Hanssonf8ea3722021-09-16 14:24:13 +0100508 // List of source files that are needed to compile the API, but are not part of runtime library.
509 Api_srcs []string `android:"arch_variant"`
510
Paul Duffin5df79302020-05-16 15:52:12 +0100511 // Visibility for impl library module. If not specified then defaults to the
512 // visibility property.
513 Impl_library_visibility []string
514
Paul Duffin4911a892020-04-29 23:35:13 +0100515 // Visibility for stubs library modules. If not specified then defaults to the
516 // visibility property.
517 Stubs_library_visibility []string
518
519 // Visibility for stubs source modules. If not specified then defaults to the
520 // visibility property.
521 Stubs_source_visibility []string
522
Anton Hansson7f66efa2020-10-08 14:47:23 +0100523 // List of Java libraries that will be in the classpath when building the implementation lib
524 Impl_only_libs []string `android:"arch_variant"`
525
Paul Duffin77590a82022-04-28 14:13:30 +0000526 // List of Java libraries that will included in the implementation lib.
527 Impl_only_static_libs []string `android:"arch_variant"`
528
Sundong Ahnf043cf62018-06-25 16:04:37 +0900529 // List of Java libraries that will be in the classpath when building stubs
530 Stub_only_libs []string `android:"arch_variant"`
531
Anton Hanssondae54cd2021-04-21 16:30:10 +0100532 // List of Java libraries that will included in stub libraries
533 Stub_only_static_libs []string `android:"arch_variant"`
534
Paul Duffin7a586d32019-12-30 17:09:34 +0000535 // list of package names that will be documented and publicized as API.
536 // This allows the API to be restricted to a subset of the source files provided.
537 // If this is unspecified then all the source files will be treated as being part
538 // of the API.
Jiyong Parkc678ad32018-04-10 13:07:10 +0900539 Api_packages []string
540
Jiyong Park5a2c9d72018-05-01 22:25:41 +0900541 // list of package names that must be hidden from the API
542 Hidden_api_packages []string
543
Paul Duffin749f98f2019-12-30 17:23:46 +0000544 // the relative path to the directory containing the api specification files.
545 // Defaults to "api".
546 Api_dir *string
547
Paul Duffindfa131e2020-05-15 20:37:11 +0100548 // Determines whether a runtime implementation library is built; defaults to false.
549 //
550 // If true then it also prevents the module from being used as a shared module, i.e.
MƄrten Kongstad81d90952022-05-25 16:27:11 +0200551 // it is as if shared_library: false, was set.
Paul Duffin43db9be2019-12-30 17:35:49 +0000552 Api_only *bool
553
Paul Duffin11512472019-02-11 15:55:17 +0000554 // local files that are used within user customized droiddoc options.
555 Droiddoc_option_files []string
556
Spandan Das93e95992021-07-29 18:26:39 +0000557 // additional droiddoc options.
Paul Duffin11512472019-02-11 15:55:17 +0000558 // Available variables for substitution:
559 //
560 // $(location <label>): the path to the droiddoc_option_files with name <label>
Sundong Ahndd567f92018-07-31 17:19:11 +0900561 Droiddoc_options []string
562
Paul Duffine22c2ab2020-05-20 19:35:27 +0100563 // is set to true, Metalava will allow framework SDK to contain annotations.
564 Annotations_enabled *bool
565
Sundong Ahn054b19a2018-10-19 13:46:09 +0900566 // a list of top-level directories containing files to merge qualifier annotations
567 // (i.e. those intended to be included in the stubs written) from.
568 Merge_annotations_dirs []string
569
570 // a list of top-level directories containing Java stub files to merge show/hide annotations from.
571 Merge_inclusion_annotations_dirs []string
572
Paul Duffin4f5c1ef2020-11-19 14:53:43 +0000573 // If set to true then don't create dist rules.
574 No_dist *bool
Sundong Ahn80a87b32019-05-13 15:02:50 +0900575
Paul Duffin31310252020-11-20 21:26:20 +0000576 // The stem for the artifacts that are copied to the dist, if not specified
577 // then defaults to the base module name.
578 //
579 // For each scope the following artifacts are copied to the apistubs/<scope>
580 // directory in the dist.
581 // * stubs impl jar -> <dist-stem>.jar
582 // * API specification file -> api/<dist-stem>.txt
583 // * Removed API specification file -> api/<dist-stem>-removed.txt
584 //
585 // Also used to construct the name of the filegroup (created by prebuilt_apis)
586 // that references the latest released API and remove API specification files.
587 // * API specification filegroup -> <dist-stem>.api.<scope>.latest
588 // * Removed API specification filegroup -> <dist-stem>-removed.api.<scope>.latest
Jaewoong Jung1a97ee02021-03-09 13:25:02 -0800589 // * API incompatibilities baseline filegroup -> <dist-stem>-incompatibilities.api.<scope>.latest
Paul Duffin31310252020-11-20 21:26:20 +0000590 Dist_stem *string
591
Colin Cross986b69a2021-06-01 13:13:40 -0700592 // The subdirectory for the artifacts that are copied to the dist directory. If not specified
Colin Cross3dd66252021-06-01 14:05:09 -0700593 // then defaults to "unknown". Should be set to "android" for anything that should be published
Colin Cross986b69a2021-06-01 13:13:40 -0700594 // in the public Android SDK.
595 Dist_group *string
596
Anton Hanssondff2c782020-12-21 17:10:01 +0000597 // A compatibility mode that allows historical API-tracking files to not exist.
598 // Do not use.
599 Unsafe_ignore_missing_latest_api bool
600
Paul Duffin3375e352020-04-28 10:44:03 +0100601 // indicates whether system and test apis should be generated.
602 Generate_system_and_test_apis bool `blueprint:"mutated"`
603
604 // The properties specific to the public api scope
605 //
606 // Unless explicitly specified by using public.enabled the public api scope is
607 // enabled by default in both legacy and non-legacy mode.
608 Public ApiScopeProperties
609
610 // The properties specific to the system api scope
611 //
612 // In legacy mode the system api scope is enabled by default when sdk_version
613 // is set to something other than "none".
614 //
615 // In non-legacy mode the system api scope is disabled by default.
616 System ApiScopeProperties
617
618 // The properties specific to the test api scope
619 //
620 // In legacy mode the test api scope is enabled by default when sdk_version
621 // is set to something other than "none".
622 //
623 // In non-legacy mode the test api scope is disabled by default.
624 Test ApiScopeProperties
Paul Duffin37e0b772019-12-30 17:20:10 +0000625
Paul Duffin0c5bae52020-06-02 13:00:08 +0100626 // The properties specific to the module-lib api scope
Paul Duffin8f265b92020-04-28 14:13:56 +0100627 //
Zi Wangb2179e32023-01-31 15:53:30 -0800628 // Unless explicitly specified by using module_lib.enabled the module_lib api
629 // scope is disabled by default.
Paul Duffin8f265b92020-04-28 14:13:56 +0100630 Module_lib ApiScopeProperties
631
Paul Duffin0c5bae52020-06-02 13:00:08 +0100632 // The properties specific to the system-server api scope
633 //
Zi Wangb2179e32023-01-31 15:53:30 -0800634 // Unless explicitly specified by using system_server.enabled the
635 // system_server api scope is disabled by default.
Paul Duffin0c5bae52020-06-02 13:00:08 +0100636 System_server ApiScopeProperties
637
Jiyong Park932cdfe2020-05-28 00:19:53 +0900638 // Determines if the stubs are preferred over the implementation library
639 // for linking, even when the client doesn't specify sdk_version. When this
640 // is set to true, such clients are provided with the widest API surface that
641 // this lib provides. Note however that this option doesn't affect the clients
642 // that are in the same APEX as this library. In that case, the clients are
643 // always linked with the implementation library. Default is false.
644 Default_to_stubs *bool
645
Paul Duffin160fe412020-05-10 19:32:20 +0100646 // Properties related to api linting.
647 Api_lint struct {
648 // Enable api linting.
649 Enabled *bool
Anton Hanssonfd1c0d22023-11-02 15:18:09 +0000650
651 // If API lint is enabled, this flag controls whether a set of legitimate lint errors
652 // are turned off. The default is true.
653 Legacy_errors_allowed *bool
Paul Duffin160fe412020-05-10 19:32:20 +0100654 }
655
Jihoon Kang80456fd2023-11-15 19:22:14 +0000656 // Determines if the module contributes to any api surfaces.
657 // This property should be set to true only if the module is listed under
658 // frameworks-base-api.bootclasspath in frameworks/base/api/Android.bp.
659 // Otherwise, this property should be set to false.
660 // Defaults to false.
661 Contribute_to_android_api *bool
662
Jihoon Kang6592e872023-12-19 01:13:16 +0000663 // a list of aconfig_declarations module names that the stubs generated in this module
664 // depend on.
665 Aconfig_declarations []string
666
Jiyong Parkc678ad32018-04-10 13:07:10 +0900667 // TODO: determines whether to create HTML doc or not
Paul Duffine8409952022-09-22 16:24:46 +0100668 // Html_doc *bool
Jiyong Parkc678ad32018-04-10 13:07:10 +0900669}
670
Paul Duffin0f8faff2020-05-20 16:18:00 +0100671// Paths to outputs from java_sdk_library and java_sdk_library_import.
672//
673// Fields that are android.Paths are always set (during GenerateAndroidBuildActions).
674// OptionalPaths are always set by java_sdk_library but may not be set by
675// java_sdk_library_import as not all instances provide that information.
Paul Duffind1b3a922020-01-22 11:57:20 +0000676type scopePaths struct {
Paul Duffin0f8faff2020-05-20 16:18:00 +0100677 // The path (represented as Paths for convenience when returning) to the stubs header jar.
678 //
679 // That is the jar that is created by turbine.
680 stubsHeaderPath android.Paths
681
682 // The path (represented as Paths for convenience when returning) to the stubs implementation jar.
683 //
684 // This is not the implementation jar, it still only contains stubs.
685 stubsImplPath android.Paths
686
Paul Duffin1267d872021-04-16 17:21:36 +0100687 // The dex jar for the stubs.
688 //
689 // This is not the implementation jar, it still only contains stubs.
Martin Stjernholm8be1e6d2021-09-15 03:34:04 +0100690 stubsDexJarPath OptionalDexJarPath
Paul Duffin1267d872021-04-16 17:21:36 +0100691
Jihoon Kangbd093452023-12-26 19:08:01 +0000692 // The exportable dex jar for the stubs.
693 // This is not the implementation jar, it still only contains stubs.
694 // Includes unflagged apis and flagged apis enabled by release configurations.
695 exportableStubsDexJarPath OptionalDexJarPath
696
Paul Duffin0f8faff2020-05-20 16:18:00 +0100697 // The API specification file, e.g. system_current.txt.
698 currentApiFilePath android.OptionalPath
699
700 // The specification of API elements removed since the last release.
701 removedApiFilePath android.OptionalPath
702
703 // The stubs source jar.
704 stubsSrcJar android.OptionalPath
Anton Hanssond78eb762021-09-21 15:25:12 +0100705
706 // Extracted annotations.
707 annotationsZip android.OptionalPath
Paul Duffin958806b2022-05-16 13:10:47 +0000708
709 // The path to the latest API file.
Jihoon Kang5623e542024-01-31 23:27:26 +0000710 latestApiPaths android.Paths
Paul Duffin958806b2022-05-16 13:10:47 +0000711
712 // The path to the latest removed API file.
Jihoon Kang5623e542024-01-31 23:27:26 +0000713 latestRemovedApiPaths android.Paths
Paul Duffind1b3a922020-01-22 11:57:20 +0000714}
715
Colin Crossdcf71b22021-02-01 13:59:03 -0800716func (paths *scopePaths) extractStubsLibraryInfoFromDependency(ctx android.ModuleContext, dep android.Module) error {
Colin Cross313aa542023-12-13 13:47:44 -0800717 if lib, ok := android.OtherModuleProvider(ctx, dep, JavaInfoProvider); ok {
Colin Crossdcf71b22021-02-01 13:59:03 -0800718 paths.stubsHeaderPath = lib.HeaderJars
719 paths.stubsImplPath = lib.ImplementationJars
Paul Duffin1267d872021-04-16 17:21:36 +0100720
721 libDep := dep.(UsesLibraryDependency)
Spandan Das59a4a2b2024-01-09 21:35:56 +0000722 paths.stubsDexJarPath = libDep.DexJarBuildPath(ctx)
Jihoon Kangbd093452023-12-26 19:08:01 +0000723 paths.exportableStubsDexJarPath = libDep.DexJarBuildPath(ctx)
724 return nil
725 } else {
726 return fmt.Errorf("expected module that has JavaInfoProvider, e.g. java_library")
727 }
728}
729
730func (paths *scopePaths) extractEverythingStubsLibraryInfoFromDependency(ctx android.ModuleContext, dep android.Module) error {
731 if lib, ok := android.OtherModuleProvider(ctx, dep, JavaInfoProvider); ok {
732 paths.stubsHeaderPath = lib.HeaderJars
Jihoon Kangf55a5f72024-01-08 08:56:20 +0000733 if !ctx.Config().ReleaseHiddenApiExportableStubs() {
734 paths.stubsImplPath = lib.ImplementationJars
735 }
Jihoon Kangbd093452023-12-26 19:08:01 +0000736
737 libDep := dep.(UsesLibraryDependency)
738 paths.stubsDexJarPath = libDep.DexJarBuildPath(ctx)
739 return nil
740 } else {
741 return fmt.Errorf("expected module that has JavaInfoProvider, e.g. java_library")
742 }
743}
744
745func (paths *scopePaths) extractExportableStubsLibraryInfoFromDependency(ctx android.ModuleContext, dep android.Module) error {
Jihoon Kangf55a5f72024-01-08 08:56:20 +0000746 if lib, ok := android.OtherModuleProvider(ctx, dep, JavaInfoProvider); ok {
747 if ctx.Config().ReleaseHiddenApiExportableStubs() {
748 paths.stubsImplPath = lib.ImplementationJars
749 }
750
Jihoon Kangbd093452023-12-26 19:08:01 +0000751 libDep := dep.(UsesLibraryDependency)
752 paths.exportableStubsDexJarPath = libDep.DexJarBuildPath(ctx)
Paul Duffinc8782502020-04-29 20:45:27 +0100753 return nil
754 } else {
Colin Crossdcf71b22021-02-01 13:59:03 -0800755 return fmt.Errorf("expected module that has JavaInfoProvider, e.g. java_library")
Paul Duffinc8782502020-04-29 20:45:27 +0100756 }
757}
758
Jihoon Kangee113282024-01-23 00:16:41 +0000759func (paths *scopePaths) treatDepAsApiStubsProvider(dep android.Module, action func(provider ApiStubsProvider) error) error {
Paul Duffin0ff08bd2020-04-29 13:30:54 +0100760 if apiStubsProvider, ok := dep.(ApiStubsProvider); ok {
Jihoon Kangee113282024-01-23 00:16:41 +0000761 err := action(apiStubsProvider)
762 if err != nil {
763 return err
764 }
Jihoon Kangf55a5f72024-01-08 08:56:20 +0000765 return nil
766 } else {
767 return fmt.Errorf("expected module that implements ExportableApiStubsSrcProvider, e.g. droidstubs")
768 }
769}
770
Jihoon Kangee113282024-01-23 00:16:41 +0000771func (paths *scopePaths) treatDepAsApiStubsSrcProvider(dep android.Module, action func(provider ApiStubsSrcProvider) error) error {
Paul Duffin0f8faff2020-05-20 16:18:00 +0100772 if apiStubsProvider, ok := dep.(ApiStubsSrcProvider); ok {
Jihoon Kangee113282024-01-23 00:16:41 +0000773 err := action(apiStubsProvider)
774 if err != nil {
775 return err
776 }
Paul Duffin0f8faff2020-05-20 16:18:00 +0100777 return nil
778 } else {
779 return fmt.Errorf("expected module that implements ApiStubsSrcProvider, e.g. droidstubs")
780 }
781}
782
Jihoon Kangee113282024-01-23 00:16:41 +0000783func (paths *scopePaths) extractApiInfoFromApiStubsProvider(provider ApiStubsProvider, stubsType StubsType) error {
784 var annotationsZip, currentApiFilePath, removedApiFilePath android.Path
785 annotationsZip, annotationsZipErr := provider.AnnotationsZip(stubsType)
786 currentApiFilePath, currentApiFilePathErr := provider.ApiFilePath(stubsType)
787 removedApiFilePath, removedApiFilePathErr := provider.RemovedApiFilePath(stubsType)
Paul Duffin0ff08bd2020-04-29 13:30:54 +0100788
Jihoon Kangee113282024-01-23 00:16:41 +0000789 combinedError := errors.Join(annotationsZipErr, currentApiFilePathErr, removedApiFilePathErr)
790
791 if combinedError == nil {
792 paths.annotationsZip = android.OptionalPathForPath(annotationsZip)
793 paths.currentApiFilePath = android.OptionalPathForPath(currentApiFilePath)
794 paths.removedApiFilePath = android.OptionalPathForPath(removedApiFilePath)
795 }
796 return combinedError
Jihoon Kangf55a5f72024-01-08 08:56:20 +0000797}
798
Jihoon Kangee113282024-01-23 00:16:41 +0000799func (paths *scopePaths) extractStubsSourceInfoFromApiStubsProviders(provider ApiStubsSrcProvider, stubsType StubsType) error {
800 stubsSrcJar, err := provider.StubsSrcJar(stubsType)
801 if err == nil {
802 paths.stubsSrcJar = android.OptionalPathForPath(stubsSrcJar)
803 }
804 return err
Jihoon Kangf55a5f72024-01-08 08:56:20 +0000805}
806
Colin Crossdcf71b22021-02-01 13:59:03 -0800807func (paths *scopePaths) extractStubsSourceInfoFromDep(ctx android.ModuleContext, dep android.Module) error {
Jihoon Kang2a26b132024-06-24 07:39:40 +0000808 stubsType := Everything
809 if ctx.Config().ReleaseHiddenApiExportableStubs() {
810 stubsType = Exportable
811 }
Jihoon Kangee113282024-01-23 00:16:41 +0000812 return paths.treatDepAsApiStubsSrcProvider(dep, func(provider ApiStubsSrcProvider) error {
Jihoon Kang2a26b132024-06-24 07:39:40 +0000813 return paths.extractStubsSourceInfoFromApiStubsProviders(provider, stubsType)
Paul Duffin0ff08bd2020-04-29 13:30:54 +0100814 })
815}
816
Colin Crossdcf71b22021-02-01 13:59:03 -0800817func (paths *scopePaths) extractStubsSourceAndApiInfoFromApiStubsProvider(ctx android.ModuleContext, dep android.Module) error {
Jihoon Kang2a26b132024-06-24 07:39:40 +0000818 stubsType := Everything
Jihoon Kangf55a5f72024-01-08 08:56:20 +0000819 if ctx.Config().ReleaseHiddenApiExportableStubs() {
Jihoon Kang2a26b132024-06-24 07:39:40 +0000820 stubsType = Exportable
Jihoon Kangf55a5f72024-01-08 08:56:20 +0000821 }
Jihoon Kangee113282024-01-23 00:16:41 +0000822 return paths.treatDepAsApiStubsProvider(dep, func(provider ApiStubsProvider) error {
Jihoon Kang2a26b132024-06-24 07:39:40 +0000823 extractApiInfoErr := paths.extractApiInfoFromApiStubsProvider(provider, stubsType)
824 extractStubsSourceInfoErr := paths.extractStubsSourceInfoFromApiStubsProviders(provider, stubsType)
Jihoon Kangee113282024-01-23 00:16:41 +0000825 return errors.Join(extractApiInfoErr, extractStubsSourceInfoErr)
Paul Duffin0ff08bd2020-04-29 13:30:54 +0100826 })
827}
828
Jihoon Kang5623e542024-01-31 23:27:26 +0000829func extractOutputPaths(dep android.Module) (android.Paths, error) {
Paul Duffin958806b2022-05-16 13:10:47 +0000830 var paths android.Paths
831 if sourceFileProducer, ok := dep.(android.SourceFileProducer); ok {
832 paths = sourceFileProducer.Srcs()
Jihoon Kang5623e542024-01-31 23:27:26 +0000833 return paths, nil
Paul Duffin958806b2022-05-16 13:10:47 +0000834 } else {
Jihoon Kang5623e542024-01-31 23:27:26 +0000835 return nil, fmt.Errorf("module %q does not produce source files", dep)
Paul Duffin958806b2022-05-16 13:10:47 +0000836 }
Paul Duffin958806b2022-05-16 13:10:47 +0000837}
838
839func (paths *scopePaths) extractLatestApiPath(ctx android.ModuleContext, dep android.Module) error {
Jihoon Kang5623e542024-01-31 23:27:26 +0000840 outputPaths, err := extractOutputPaths(dep)
841 paths.latestApiPaths = outputPaths
Paul Duffin958806b2022-05-16 13:10:47 +0000842 return err
843}
844
845func (paths *scopePaths) extractLatestRemovedApiPath(ctx android.ModuleContext, dep android.Module) error {
Jihoon Kang5623e542024-01-31 23:27:26 +0000846 outputPaths, err := extractOutputPaths(dep)
847 paths.latestRemovedApiPaths = outputPaths
Paul Duffin958806b2022-05-16 13:10:47 +0000848 return err
849}
850
Paul Duffin0ff08bd2020-04-29 13:30:54 +0100851type commonToSdkLibraryAndImportProperties struct {
Paul Duffin1b1e8062020-05-08 13:44:43 +0100852 // The naming scheme to use for the components that this module creates.
853 //
Paul Duffinee9ad5d2020-09-11 13:04:05 +0100854 // If not specified then it defaults to "default".
Paul Duffin1b1e8062020-05-08 13:44:43 +0100855 //
856 // This is a temporary mechanism to simplify conversion from separate modules for each
857 // component that follow a different naming pattern to the default one.
858 //
859 // TODO(b/155480189) - Remove once naming inconsistencies have been resolved.
Paul Duffin0ff08bd2020-04-29 13:30:54 +0100860 Naming_scheme *string
Paul Duffindfa131e2020-05-15 20:37:11 +0100861
862 // Specifies whether this module can be used as an Android shared library; defaults
863 // to true.
864 //
865 // An Android shared library is one that can be referenced in a <uses-library> element
866 // in an AndroidManifest.xml.
867 Shared_library *bool
Paul Duffina2ae7e02020-09-11 11:55:00 +0100868
869 // Files containing information about supported java doc tags.
870 Doctag_files []string `android:"path"`
Pedro Loureiro9956e5e2021-09-07 17:21:59 +0000871
872 // Signals that this shared library is part of the bootclasspath starting
873 // on the version indicated in this attribute.
874 //
875 // This will make platforms at this level and above to ignore
876 // <uses-library> tags with this library name because the library is already
877 // available
878 On_bootclasspath_since *string
879
880 // Signals that this shared library was part of the bootclasspath before
881 // (but not including) the version indicated in this attribute.
882 //
883 // The system will automatically add a <uses-library> tag with this library to
884 // apps that target any SDK less than the version indicated in this attribute.
885 On_bootclasspath_before *string
886
887 // Indicates that PackageManager should ignore this shared library if the
888 // platform is below the version indicated in this attribute.
889 //
890 // This means that the device won't recognise this library as installed.
891 Min_device_sdk *string
892
893 // Indicates that PackageManager should ignore this shared library if the
894 // platform is above the version indicated in this attribute.
895 //
896 // This means that the device won't recognise this library as installed.
897 Max_device_sdk *string
Paul Duffin0ff08bd2020-04-29 13:30:54 +0100898}
899
Paul Duffin71b33cc2021-06-23 11:39:47 +0100900// commonSdkLibraryAndImportModule defines the interface that must be provided by a module that
901// embeds the commonToSdkLibraryAndImport struct.
902type commonSdkLibraryAndImportModule interface {
Paul Duffind796f6f2022-11-23 23:06:05 +0000903 android.Module
Paul Duffin71b33cc2021-06-23 11:39:47 +0100904
Spandan Das23956d12024-01-19 00:22:22 +0000905 // Returns the name of the root java_sdk_library that creates the child stub libraries
906 // This is the `name` as it appears in Android.bp, and not the name in Soong's build graph
907 // (with the prebuilt_ prefix)
908 //
909 // e.g. in the following java_sdk_library_import
910 // java_sdk_library_import {
911 // name: "framework-foo.v1",
912 // source_module_name: "framework-foo",
913 // }
914 // the values returned by
915 // 1. Name(): prebuilt_framework-foo.v1 # unique
916 // 2. BaseModuleName(): framework-foo # the source
917 // 3. RootLibraryName: framework-foo.v1 # the undecordated `name` from Android.bp
918 RootLibraryName() string
919}
920
921func (m *SdkLibrary) RootLibraryName() string {
922 return m.BaseModuleName()
923}
924
925func (m *SdkLibraryImport) RootLibraryName() string {
926 // m.BaseModuleName refers to the source of the import
927 // use moduleBase.Name to get the name of the module as it appears in the .bp file
928 return m.ModuleBase.Name()
Paul Duffin71b33cc2021-06-23 11:39:47 +0100929}
930
Paul Duffin56d44902020-01-31 13:36:25 +0000931// Common code between sdk library and sdk library import
932type commonToSdkLibraryAndImport struct {
Paul Duffin71b33cc2021-06-23 11:39:47 +0100933 module commonSdkLibraryAndImportModule
Paul Duffinc3091c82020-05-08 14:16:20 +0100934
Paul Duffin56d44902020-01-31 13:36:25 +0000935 scopePaths map[*apiScope]*scopePaths
Paul Duffin1b1e8062020-05-08 13:44:43 +0100936
937 namingScheme sdkLibraryComponentNamingScheme
938
Paul Duffindfa131e2020-05-15 20:37:11 +0100939 commonSdkLibraryProperties commonToSdkLibraryAndImportProperties
Paul Duffin859fe962020-05-15 10:20:31 +0100940
Paul Duffina2ae7e02020-09-11 11:55:00 +0100941 // Paths to commonSdkLibraryProperties.Doctag_files
942 doctagPaths android.Paths
943
Paul Duffin859fe962020-05-15 10:20:31 +0100944 // Functionality related to this being used as a component of a java_sdk_library.
945 EmbeddableSdkLibraryComponent
Jihoon Kang8479dea2024-04-04 01:19:05 +0000946
947 // Path to the header jars of the implementation library
948 // This is non-empty only when api_only is false.
949 implLibraryHeaderJars android.Paths
Jihoon Kanga3a05462024-04-05 00:36:44 +0000950
951 // The reference to the implementation library created by the source module.
952 // Is nil if the source module does not exist.
953 implLibraryModule *Library
Paul Duffin56d44902020-01-31 13:36:25 +0000954}
955
Paul Duffin71b33cc2021-06-23 11:39:47 +0100956func (c *commonToSdkLibraryAndImport) initCommon(module commonSdkLibraryAndImportModule) {
957 c.module = module
Paul Duffin1b1e8062020-05-08 13:44:43 +0100958
Paul Duffin71b33cc2021-06-23 11:39:47 +0100959 module.AddProperties(&c.commonSdkLibraryProperties)
Paul Duffin859fe962020-05-15 10:20:31 +0100960
961 // Initialize this as an sdk library component.
Paul Duffin71b33cc2021-06-23 11:39:47 +0100962 c.initSdkLibraryComponent(module)
Paul Duffin1b1e8062020-05-08 13:44:43 +0100963}
964
965func (c *commonToSdkLibraryAndImport) initCommonAfterDefaultsApplied(ctx android.DefaultableHookContext) bool {
Paul Duffindfa131e2020-05-15 20:37:11 +0100966 schemeProperty := proptools.StringDefault(c.commonSdkLibraryProperties.Naming_scheme, "default")
Paul Duffin1b1e8062020-05-08 13:44:43 +0100967 switch schemeProperty {
968 case "default":
969 c.namingScheme = &defaultNamingScheme{}
970 default:
971 ctx.PropertyErrorf("naming_scheme", "expected 'default' but was %q", schemeProperty)
972 return false
973 }
974
Spandan Das23956d12024-01-19 00:22:22 +0000975 namePtr := proptools.StringPtr(c.module.RootLibraryName())
Paul Duffin3f0290e2021-06-30 18:25:36 +0100976 c.sdkLibraryComponentProperties.SdkLibraryName = namePtr
977
Paul Duffindfa131e2020-05-15 20:37:11 +0100978 // Only track this sdk library if this can be used as a shared library.
979 if c.sharedLibrary() {
980 // Use the name specified in the module definition as the owner.
Paul Duffin3f0290e2021-06-30 18:25:36 +0100981 c.sdkLibraryComponentProperties.SdkLibraryToImplicitlyTrack = namePtr
Paul Duffindfa131e2020-05-15 20:37:11 +0100982 }
Paul Duffin859fe962020-05-15 10:20:31 +0100983
Paul Duffin1b1e8062020-05-08 13:44:43 +0100984 return true
Paul Duffinc3091c82020-05-08 14:16:20 +0100985}
986
Paul Duffinea8f8082021-06-24 13:25:57 +0100987// uniqueApexVariations provides common implementation of the ApexModule.UniqueApexVariations
988// method.
989func (c *commonToSdkLibraryAndImport) uniqueApexVariations() bool {
990 // A java_sdk_library that is a shared library produces an XML file that makes the shared library
991 // usable from an AndroidManifest.xml's <uses-library> entry. That XML file contains the name of
992 // the APEX and so it needs a unique variation per APEX.
993 return c.sharedLibrary()
994}
995
Paul Duffina2ae7e02020-09-11 11:55:00 +0100996func (c *commonToSdkLibraryAndImport) generateCommonBuildActions(ctx android.ModuleContext) {
997 c.doctagPaths = android.PathsForModuleSrc(ctx, c.commonSdkLibraryProperties.Doctag_files)
998}
999
Jihoon Kanga3a05462024-04-05 00:36:44 +00001000func (c *commonToSdkLibraryAndImport) getImplLibraryModule() *Library {
1001 return c.implLibraryModule
1002}
1003
Paul Duffineedc5d52020-06-12 17:46:39 +01001004// Module name of the runtime implementation library
1005func (c *commonToSdkLibraryAndImport) implLibraryModuleName() string {
Spandan Das23956d12024-01-19 00:22:22 +00001006 return c.module.RootLibraryName() + ".impl"
Paul Duffineedc5d52020-06-12 17:46:39 +01001007}
1008
1009// Module name of the XML file for the lib
1010func (c *commonToSdkLibraryAndImport) xmlPermissionsModuleName() string {
Spandan Das23956d12024-01-19 00:22:22 +00001011 return c.module.RootLibraryName() + sdkXmlFileSuffix
Paul Duffineedc5d52020-06-12 17:46:39 +01001012}
1013
Paul Duffinc3091c82020-05-08 14:16:20 +01001014// Name of the java_library module that compiles the stubs source.
1015func (c *commonToSdkLibraryAndImport) stubsLibraryModuleName(apiScope *apiScope) string {
Spandan Das23956d12024-01-19 00:22:22 +00001016 baseName := c.module.RootLibraryName()
Paul Duffin21787622022-11-25 12:48:20 +00001017 return c.namingScheme.stubsLibraryModuleName(apiScope, baseName)
Paul Duffinc3091c82020-05-08 14:16:20 +01001018}
1019
Jihoon Kangfa4a90d2023-12-20 02:53:38 +00001020// Name of the java_library module that compiles the exportable stubs source.
1021func (c *commonToSdkLibraryAndImport) exportableStubsLibraryModuleName(apiScope *apiScope) string {
Spandan Das23956d12024-01-19 00:22:22 +00001022 baseName := c.module.RootLibraryName()
Jihoon Kangfa4a90d2023-12-20 02:53:38 +00001023 return c.namingScheme.exportableStubsLibraryModuleName(apiScope, baseName)
1024}
1025
Paul Duffinc3091c82020-05-08 14:16:20 +01001026// Name of the droidstubs module that generates the stubs source and may also
1027// generate/check the API.
1028func (c *commonToSdkLibraryAndImport) stubsSourceModuleName(apiScope *apiScope) string {
Spandan Das23956d12024-01-19 00:22:22 +00001029 baseName := c.module.RootLibraryName()
Paul Duffin21787622022-11-25 12:48:20 +00001030 return c.namingScheme.stubsSourceModuleName(apiScope, baseName)
Paul Duffinc3091c82020-05-08 14:16:20 +01001031}
1032
Jihoon Kang1c92c3e2023-03-23 17:44:51 +00001033// Name of the java_api_library module that generates the from-text stubs source
1034// and compiles to a jar file.
1035func (c *commonToSdkLibraryAndImport) apiLibraryModuleName(apiScope *apiScope) string {
Spandan Das23956d12024-01-19 00:22:22 +00001036 baseName := c.module.RootLibraryName()
Jihoon Kang1c92c3e2023-03-23 17:44:51 +00001037 return c.namingScheme.apiLibraryModuleName(apiScope, baseName)
1038}
1039
Jihoon Kang1147b312023-06-08 23:25:57 +00001040// Name of the java_library module that compiles the stubs
1041// generated from source Java files.
Jihoon Kangfa4a90d2023-12-20 02:53:38 +00001042func (c *commonToSdkLibraryAndImport) sourceStubsLibraryModuleName(apiScope *apiScope) string {
Spandan Das23956d12024-01-19 00:22:22 +00001043 baseName := c.module.RootLibraryName()
Jihoon Kangfa4a90d2023-12-20 02:53:38 +00001044 return c.namingScheme.sourceStubsLibraryModuleName(apiScope, baseName)
1045}
1046
1047// Name of the java_library module that compiles the exportable stubs
1048// generated from source Java files.
1049func (c *commonToSdkLibraryAndImport) exportableSourceStubsLibraryModuleName(apiScope *apiScope) string {
Spandan Das23956d12024-01-19 00:22:22 +00001050 baseName := c.module.RootLibraryName()
Jihoon Kangfa4a90d2023-12-20 02:53:38 +00001051 return c.namingScheme.exportableSourceStubsLibraryModuleName(apiScope, baseName)
Jihoon Kang1147b312023-06-08 23:25:57 +00001052}
1053
Paul Duffin46dc45a2020-05-14 15:39:10 +01001054// The component names for different outputs of the java_sdk_library.
1055//
1056// They are similar to the names used for the child modules it creates
1057const (
1058 stubsSourceComponentName = "stubs.source"
1059
1060 apiTxtComponentName = "api.txt"
1061
1062 removedApiTxtComponentName = "removed-api.txt"
Anton Hanssond78eb762021-09-21 15:25:12 +01001063
1064 annotationsComponentName = "annotations.zip"
Paul Duffin46dc45a2020-05-14 15:39:10 +01001065)
1066
1067// A regular expression to match tags that reference a specific stubs component.
1068//
1069// It will only match if given a valid scope and a valid component. It is verfy strict
1070// to ensure it does not accidentally match a similar looking tag that should be processed
1071// by the embedded Library.
1072var tagSplitter = func() *regexp.Regexp {
1073 // Given a list of literal string items returns a regular expression that will
1074 // match any one of the items.
1075 choice := func(items ...string) string {
1076 return `\Q` + strings.Join(items, `\E|\Q`) + `\E`
1077 }
1078
1079 // Regular expression to match one of the scopes.
1080 scopesRegexp := choice(allScopeNames...)
1081
1082 // Regular expression to match one of the components.
Anton Hanssond78eb762021-09-21 15:25:12 +01001083 componentsRegexp := choice(stubsSourceComponentName, apiTxtComponentName, removedApiTxtComponentName, annotationsComponentName)
Paul Duffin46dc45a2020-05-14 15:39:10 +01001084
1085 // Regular expression to match any combination of one scope and one component.
1086 return regexp.MustCompile(fmt.Sprintf(`^\.(%s)\.(%s)$`, scopesRegexp, componentsRegexp))
1087}()
1088
mrziwang9f7b9f42024-07-10 12:18:06 -07001089func (module *commonToSdkLibraryAndImport) setOutputFiles(ctx android.ModuleContext) {
1090 if module.doctagPaths != nil {
1091 ctx.SetOutputFiles(module.doctagPaths, ".doctags")
1092 }
1093 for _, scopeName := range android.SortedKeys(scopeByName) {
1094 paths := module.findScopePaths(scopeByName[scopeName])
1095 if paths == nil {
1096 continue
Paul Duffin46dc45a2020-05-14 15:39:10 +01001097 }
mrziwang9f7b9f42024-07-10 12:18:06 -07001098 componentToOutput := map[string]android.OptionalPath{
1099 stubsSourceComponentName: paths.stubsSrcJar,
1100 apiTxtComponentName: paths.currentApiFilePath,
1101 removedApiTxtComponentName: paths.removedApiFilePath,
1102 annotationsComponentName: paths.annotationsZip,
1103 }
1104 for _, component := range android.SortedKeys(componentToOutput) {
1105 if componentToOutput[component].Valid() {
1106 ctx.SetOutputFiles(android.Paths{componentToOutput[component].Path()}, "."+scopeName+"."+component)
Paul Duffina2ae7e02020-09-11 11:55:00 +01001107 }
1108 }
Paul Duffin46dc45a2020-05-14 15:39:10 +01001109 }
1110}
1111
Paul Duffin803a9562020-05-20 11:52:25 +01001112func (c *commonToSdkLibraryAndImport) getScopePathsCreateIfNeeded(scope *apiScope) *scopePaths {
Paul Duffin56d44902020-01-31 13:36:25 +00001113 if c.scopePaths == nil {
1114 c.scopePaths = make(map[*apiScope]*scopePaths)
1115 }
1116 paths := c.scopePaths[scope]
1117 if paths == nil {
1118 paths = &scopePaths{}
1119 c.scopePaths[scope] = paths
1120 }
1121
1122 return paths
1123}
1124
Paul Duffin803a9562020-05-20 11:52:25 +01001125func (c *commonToSdkLibraryAndImport) findScopePaths(scope *apiScope) *scopePaths {
1126 if c.scopePaths == nil {
1127 return nil
1128 }
1129
1130 return c.scopePaths[scope]
1131}
1132
1133// If this does not support the requested api scope then find the closest available
1134// scope it does support. Returns nil if no such scope is available.
1135func (c *commonToSdkLibraryAndImport) findClosestScopePath(scope *apiScope) *scopePaths {
Paul Duffind0b9fca2022-09-30 18:11:41 +01001136 for s := scope; s != nil; s = s.canAccess {
Paul Duffin803a9562020-05-20 11:52:25 +01001137 if paths := c.findScopePaths(s); paths != nil {
1138 return paths
1139 }
1140 }
1141
1142 // This should never happen outside tests as public should be the base scope for every
1143 // scope and is enabled by default.
1144 return nil
1145}
1146
Jiyong Parkf1691d22021-03-29 20:11:58 +09001147func (c *commonToSdkLibraryAndImport) selectHeaderJarsForSdkVersion(ctx android.BaseModuleContext, sdkVersion android.SdkSpec) android.Paths {
Paul Duffinb05d4292020-05-20 12:19:10 +01001148
1149 // If a specific numeric version has been requested then use prebuilt versions of the sdk.
Jiyong Park54105c42021-03-31 18:17:53 +09001150 if !sdkVersion.ApiLevel.IsPreview() {
Spandan Das23956d12024-01-19 00:22:22 +00001151 return PrebuiltJars(ctx, c.module.RootLibraryName(), sdkVersion)
Paul Duffinb05d4292020-05-20 12:19:10 +01001152 }
1153
Paul Duffin1267d872021-04-16 17:21:36 +01001154 paths := c.selectScopePaths(ctx, sdkVersion.Kind)
1155 if paths == nil {
1156 return nil
1157 }
1158
1159 return paths.stubsHeaderPath
1160}
1161
1162// selectScopePaths returns the *scopePaths appropriate for the specific kind.
1163//
1164// If the module does not support the specific kind then it will return the *scopePaths for the
1165// closest kind which is a subset of the requested kind. e.g. if requesting android.SdkModule then
1166// it will return *scopePaths for android.SdkSystem if available or android.SdkPublic of not.
1167func (c *commonToSdkLibraryAndImport) selectScopePaths(ctx android.BaseModuleContext, kind android.SdkKind) *scopePaths {
Paul Duffin32cf58a2021-05-18 16:32:50 +01001168 apiScope := sdkKindToApiScope(kind)
Paul Duffinb05d4292020-05-20 12:19:10 +01001169
Paul Duffin803a9562020-05-20 11:52:25 +01001170 paths := c.findClosestScopePath(apiScope)
1171 if paths == nil {
1172 var scopes []string
Jihoon Kang98aa8fa2024-06-07 11:06:57 +00001173 for _, s := range AllApiScopes {
Paul Duffin803a9562020-05-20 11:52:25 +01001174 if c.findScopePaths(s) != nil {
1175 scopes = append(scopes, s.name)
1176 }
1177 }
Spandan Das23956d12024-01-19 00:22:22 +00001178 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 +01001179 return nil
1180 }
1181
Paul Duffin1267d872021-04-16 17:21:36 +01001182 return paths
1183}
1184
Paul Duffin32cf58a2021-05-18 16:32:50 +01001185// sdkKindToApiScope maps from android.SdkKind to apiScope.
1186func sdkKindToApiScope(kind android.SdkKind) *apiScope {
1187 var apiScope *apiScope
1188 switch kind {
1189 case android.SdkSystem:
1190 apiScope = apiScopeSystem
1191 case android.SdkModule:
1192 apiScope = apiScopeModuleLib
1193 case android.SdkTest:
1194 apiScope = apiScopeTest
1195 case android.SdkSystemServer:
1196 apiScope = apiScopeSystemServer
1197 default:
1198 apiScope = apiScopePublic
1199 }
1200 return apiScope
1201}
1202
Paul Duffin1267d872021-04-16 17:21:36 +01001203// to satisfy SdkLibraryDependency interface
Martin Stjernholm8be1e6d2021-09-15 03:34:04 +01001204func (c *commonToSdkLibraryAndImport) SdkApiStubDexJar(ctx android.BaseModuleContext, kind android.SdkKind) OptionalDexJarPath {
Paul Duffin1267d872021-04-16 17:21:36 +01001205 paths := c.selectScopePaths(ctx, kind)
1206 if paths == nil {
Martin Stjernholm8be1e6d2021-09-15 03:34:04 +01001207 return makeUnsetDexJarPath()
Paul Duffin1267d872021-04-16 17:21:36 +01001208 }
1209
1210 return paths.stubsDexJarPath
Paul Duffinb05d4292020-05-20 12:19:10 +01001211}
1212
Paul Duffin32cf58a2021-05-18 16:32:50 +01001213// to satisfy SdkLibraryDependency interface
Jihoon Kangbd093452023-12-26 19:08:01 +00001214func (c *commonToSdkLibraryAndImport) SdkApiExportableStubDexJar(ctx android.BaseModuleContext, kind android.SdkKind) OptionalDexJarPath {
1215 paths := c.selectScopePaths(ctx, kind)
1216 if paths == nil {
1217 return makeUnsetDexJarPath()
1218 }
1219
1220 return paths.exportableStubsDexJarPath
1221}
1222
1223// to satisfy SdkLibraryDependency interface
Paul Duffin32cf58a2021-05-18 16:32:50 +01001224func (c *commonToSdkLibraryAndImport) SdkRemovedTxtFile(ctx android.BaseModuleContext, kind android.SdkKind) android.OptionalPath {
1225 apiScope := sdkKindToApiScope(kind)
1226 paths := c.findScopePaths(apiScope)
1227 if paths == nil {
1228 return android.OptionalPath{}
1229 }
1230
1231 return paths.removedApiFilePath
1232}
1233
Paul Duffin859fe962020-05-15 10:20:31 +01001234func (c *commonToSdkLibraryAndImport) sdkComponentPropertiesForChildLibrary() interface{} {
1235 componentProps := &struct {
Paul Duffin3f0290e2021-06-30 18:25:36 +01001236 SdkLibraryName *string
Paul Duffin859fe962020-05-15 10:20:31 +01001237 SdkLibraryToImplicitlyTrack *string
Paul Duffindfa131e2020-05-15 20:37:11 +01001238 }{}
1239
Spandan Das23956d12024-01-19 00:22:22 +00001240 namePtr := proptools.StringPtr(c.module.RootLibraryName())
Paul Duffin3f0290e2021-06-30 18:25:36 +01001241 componentProps.SdkLibraryName = namePtr
1242
Paul Duffindfa131e2020-05-15 20:37:11 +01001243 if c.sharedLibrary() {
Paul Duffin859fe962020-05-15 10:20:31 +01001244 // Mark the stubs library as being components of this java_sdk_library so that
1245 // any app that includes code which depends (directly or indirectly) on the stubs
1246 // library will have the appropriate <uses-library> invocation inserted into its
1247 // manifest if necessary.
Paul Duffin3f0290e2021-06-30 18:25:36 +01001248 componentProps.SdkLibraryToImplicitlyTrack = namePtr
Paul Duffin859fe962020-05-15 10:20:31 +01001249 }
1250
1251 return componentProps
1252}
1253
Paul Duffindfa131e2020-05-15 20:37:11 +01001254func (c *commonToSdkLibraryAndImport) sharedLibrary() bool {
1255 return proptools.BoolDefault(c.commonSdkLibraryProperties.Shared_library, true)
1256}
1257
Paul Duffinf4600f62021-05-13 22:34:45 +01001258// Check if the stub libraries should be compiled for dex
1259func (c *commonToSdkLibraryAndImport) stubLibrariesCompiledForDex() bool {
1260 // Always compile the dex file files for the stub libraries if they will be used on the
1261 // bootclasspath.
1262 return !c.sharedLibrary()
1263}
1264
Paul Duffin859fe962020-05-15 10:20:31 +01001265// Properties related to the use of a module as an component of a java_sdk_library.
1266type SdkLibraryComponentProperties struct {
Paul Duffin3f0290e2021-06-30 18:25:36 +01001267 // The name of the java_sdk_library/_import module.
1268 SdkLibraryName *string `blueprint:"mutated"`
Paul Duffin859fe962020-05-15 10:20:31 +01001269
1270 // The name of the java_sdk_library/_import to add to a <uses-library> entry
1271 // in the AndroidManifest.xml of any Android app that includes code that references
1272 // this module. If not set then no java_sdk_library/_import is tracked.
1273 SdkLibraryToImplicitlyTrack *string `blueprint:"mutated"`
1274}
1275
1276// Structure to be embedded in a module struct that needs to support the
1277// SdkLibraryComponentDependency interface.
1278type EmbeddableSdkLibraryComponent struct {
1279 sdkLibraryComponentProperties SdkLibraryComponentProperties
1280}
1281
Paul Duffin71b33cc2021-06-23 11:39:47 +01001282func (e *EmbeddableSdkLibraryComponent) initSdkLibraryComponent(module android.Module) {
1283 module.AddProperties(&e.sdkLibraryComponentProperties)
Paul Duffin859fe962020-05-15 10:20:31 +01001284}
1285
1286// to satisfy SdkLibraryComponentDependency
Paul Duffin3f0290e2021-06-30 18:25:36 +01001287func (e *EmbeddableSdkLibraryComponent) SdkLibraryName() *string {
1288 return e.sdkLibraryComponentProperties.SdkLibraryName
1289}
1290
1291// to satisfy SdkLibraryComponentDependency
Ulya Trafimovich39b437b2020-09-23 16:42:35 +01001292func (e *EmbeddableSdkLibraryComponent) OptionalSdkLibraryImplementation() *string {
Ulya Trafimovich78645fb2021-07-16 15:29:25 +01001293 // For shared libraries, this is the same as the SDK library name. If a Java library or app
1294 // depends on a component library (e.g. a stub library) it still needs to know the name of the
1295 // run-time library and the corresponding module that provides the implementation. This name is
1296 // passed to manifest_fixer (to be added to AndroidManifest.xml) and added to CLC (to be used
1297 // in dexpreopt).
1298 //
1299 // For non-shared SDK (component or not) libraries this returns `nil`, as they are not
1300 // <uses-library> and should not be added to the manifest or to CLC.
Ulya Trafimovich39b437b2020-09-23 16:42:35 +01001301 return e.sdkLibraryComponentProperties.SdkLibraryToImplicitlyTrack
1302}
1303
Paul Duffin859fe962020-05-15 10:20:31 +01001304// Implemented by modules that are (or possibly could be) a component of a java_sdk_library
1305// (including the java_sdk_library) itself.
1306type SdkLibraryComponentDependency interface {
Ulya Trafimovich31e444e2020-08-14 17:32:16 +01001307 UsesLibraryDependency
1308
Paul Duffin3f0290e2021-06-30 18:25:36 +01001309 // SdkLibraryName returns the name of the java_sdk_library/_import module.
1310 SdkLibraryName() *string
1311
Ulya Trafimovich39b437b2020-09-23 16:42:35 +01001312 // The name of the implementation library for the optional SDK library or nil, if there isn't one.
1313 OptionalSdkLibraryImplementation() *string
Paul Duffin859fe962020-05-15 10:20:31 +01001314}
1315
1316// Make sure that all the module types that are components of java_sdk_library/_import
1317// and which can be referenced (directly or indirectly) from an android app implement
1318// the SdkLibraryComponentDependency interface.
1319var _ SdkLibraryComponentDependency = (*Library)(nil)
1320var _ SdkLibraryComponentDependency = (*Import)(nil)
1321var _ SdkLibraryComponentDependency = (*SdkLibrary)(nil)
Paul Duffineedc5d52020-06-12 17:46:39 +01001322var _ SdkLibraryComponentDependency = (*SdkLibraryImport)(nil)
Paul Duffin859fe962020-05-15 10:20:31 +01001323
Paul Duffin32cf58a2021-05-18 16:32:50 +01001324// Provides access to sdk_version related files, e.g. header and implementation jars.
Paul Duffin859fe962020-05-15 10:20:31 +01001325type SdkLibraryDependency interface {
1326 SdkLibraryComponentDependency
1327
1328 // Get the header jars appropriate for the supplied sdk_version.
1329 //
1330 // These are turbine generated jars so they only change if the externals of the
1331 // class changes but it does not contain and implementation or JavaDoc.
Jiyong Parkf1691d22021-03-29 20:11:58 +09001332 SdkHeaderJars(ctx android.BaseModuleContext, sdkVersion android.SdkSpec) android.Paths
Paul Duffin859fe962020-05-15 10:20:31 +01001333
Jihoon Kangbd093452023-12-26 19:08:01 +00001334 // SdkApiStubDexJar returns the dex jar for the stubs for the prebuilt
1335 // java_sdk_library_import module. It is needed by the hiddenapi processing tool which
1336 // processes dex files.
Martin Stjernholm8be1e6d2021-09-15 03:34:04 +01001337 SdkApiStubDexJar(ctx android.BaseModuleContext, kind android.SdkKind) OptionalDexJarPath
Paul Duffinf4600f62021-05-13 22:34:45 +01001338
Jihoon Kangbd093452023-12-26 19:08:01 +00001339 // SdkApiExportableStubDexJar returns the exportable dex jar for the stubs for
1340 // java_sdk_library module. It is needed by the hiddenapi processing tool which processes
1341 // dex files.
1342 SdkApiExportableStubDexJar(ctx android.BaseModuleContext, kind android.SdkKind) OptionalDexJarPath
1343
Paul Duffin32cf58a2021-05-18 16:32:50 +01001344 // SdkRemovedTxtFile returns the optional path to the removed.txt file for the specified sdk kind.
1345 SdkRemovedTxtFile(ctx android.BaseModuleContext, kind android.SdkKind) android.OptionalPath
1346
Paul Duffinf4600f62021-05-13 22:34:45 +01001347 // sharedLibrary returns true if this can be used as a shared library.
1348 sharedLibrary() bool
Jihoon Kanga3a05462024-04-05 00:36:44 +00001349
1350 getImplLibraryModule() *Library
Paul Duffin859fe962020-05-15 10:20:31 +01001351}
1352
Inseob Kimc0907f12019-02-08 21:00:45 +09001353type SdkLibrary struct {
Sundong Ahn054b19a2018-10-19 13:46:09 +09001354 Library
Jiyong Parkc678ad32018-04-10 13:07:10 +09001355
Sundong Ahn054b19a2018-10-19 13:46:09 +09001356 sdkLibraryProperties sdkLibraryProperties
Jiyong Parkc678ad32018-04-10 13:07:10 +09001357
Paul Duffin3375e352020-04-28 10:44:03 +01001358 // Map from api scope to the scope specific property structure.
1359 scopeToProperties map[*apiScope]*ApiScopeProperties
1360
Paul Duffin56d44902020-01-31 13:36:25 +00001361 commonToSdkLibraryAndImport
Jihoon Kanga3a05462024-04-05 00:36:44 +00001362
1363 builtInstalledForApex []dexpreopterInstall
Jiyong Parkc678ad32018-04-10 13:07:10 +09001364}
1365
Inseob Kimc0907f12019-02-08 21:00:45 +09001366var _ SdkLibraryDependency = (*SdkLibrary)(nil)
Colin Cross897d2ed2019-02-11 14:03:51 -08001367
Paul Duffin3375e352020-04-28 10:44:03 +01001368func (module *SdkLibrary) generateTestAndSystemScopesByDefault() bool {
1369 return module.sdkLibraryProperties.Generate_system_and_test_apis
1370}
1371
Jihoon Kanga3a05462024-04-05 00:36:44 +00001372func (module *SdkLibrary) DexJarBuildPath(ctx android.ModuleErrorfContext) OptionalDexJarPath {
1373 if module.implLibraryModule != nil {
1374 return module.implLibraryModule.DexJarBuildPath(ctx)
1375 }
1376 return makeUnsetDexJarPath()
1377}
1378
1379func (module *SdkLibrary) DexJarInstallPath() android.Path {
1380 if module.implLibraryModule != nil {
1381 return module.implLibraryModule.DexJarInstallPath()
1382 }
1383 return nil
1384}
1385
Paul Duffin3375e352020-04-28 10:44:03 +01001386func (module *SdkLibrary) getGeneratedApiScopes(ctx android.EarlyModuleContext) apiScopes {
1387 // Check to see if any scopes have been explicitly enabled. If any have then all
1388 // must be.
1389 anyScopesExplicitlyEnabled := false
Jihoon Kang98aa8fa2024-06-07 11:06:57 +00001390 for _, scope := range AllApiScopes {
Paul Duffin3375e352020-04-28 10:44:03 +01001391 scopeProperties := module.scopeToProperties[scope]
1392 if scopeProperties.Enabled != nil {
1393 anyScopesExplicitlyEnabled = true
1394 break
1395 }
Paul Duffind1b3a922020-01-22 11:57:20 +00001396 }
Paul Duffin3375e352020-04-28 10:44:03 +01001397
1398 var generatedScopes apiScopes
1399 enabledScopes := make(map[*apiScope]struct{})
Jihoon Kang98aa8fa2024-06-07 11:06:57 +00001400 for _, scope := range AllApiScopes {
Paul Duffin3375e352020-04-28 10:44:03 +01001401 scopeProperties := module.scopeToProperties[scope]
1402 // If any scopes are explicitly enabled then ignore the legacy enabled status.
1403 // This is to ensure that any new usages of this module type do not rely on legacy
1404 // behaviour.
1405 defaultEnabledStatus := false
1406 if anyScopesExplicitlyEnabled {
1407 defaultEnabledStatus = scope.defaultEnabledStatus
1408 } else {
1409 defaultEnabledStatus = scope.legacyEnabledStatus(module)
1410 }
1411 enabled := proptools.BoolDefault(scopeProperties.Enabled, defaultEnabledStatus)
1412 if enabled {
1413 enabledScopes[scope] = struct{}{}
1414 generatedScopes = append(generatedScopes, scope)
1415 }
1416 }
1417
1418 // Now check to make sure that any scope that is extended by an enabled scope is also
1419 // enabled.
Jihoon Kang98aa8fa2024-06-07 11:06:57 +00001420 for _, scope := range AllApiScopes {
Paul Duffin3375e352020-04-28 10:44:03 +01001421 if _, ok := enabledScopes[scope]; ok {
1422 extends := scope.extends
1423 if extends != nil {
1424 if _, ok := enabledScopes[extends]; !ok {
1425 ctx.ModuleErrorf("enabled api scope %q depends on disabled scope %q", scope, extends)
1426 }
1427 }
1428 }
1429 }
1430
1431 return generatedScopes
Paul Duffind1b3a922020-01-22 11:57:20 +00001432}
1433
satayev758968a2021-12-06 11:42:40 +00001434var _ android.ModuleWithMinSdkVersionCheck = (*SdkLibrary)(nil)
1435
satayev8f088b02021-12-06 11:40:46 +00001436func (module *SdkLibrary) CheckMinSdkVersion(ctx android.ModuleContext) {
Jihoon Kanga3a05462024-04-05 00:36:44 +00001437 CheckMinSdkVersion(ctx, &module.Library)
1438}
1439
1440func CheckMinSdkVersion(ctx android.ModuleContext, module *Library) {
Spandan Das8c9ae7e2023-03-03 21:20:36 +00001441 android.CheckMinSdkVersion(ctx, module.MinSdkVersion(ctx), func(c android.ModuleContext, do android.PayloadDepsCallback) {
satayev8f088b02021-12-06 11:40:46 +00001442 ctx.WalkDeps(func(child android.Module, parent android.Module) bool {
1443 isExternal := !module.depIsInSameApex(ctx, child)
1444 if am, ok := child.(android.ApexModule); ok {
1445 if !do(ctx, parent, am, isExternal) {
1446 return false
1447 }
1448 }
1449 return !isExternal
1450 })
1451 })
1452}
1453
Paul Duffineedc5d52020-06-12 17:46:39 +01001454type sdkLibraryComponentTag struct {
1455 blueprint.BaseDependencyTag
1456 name string
1457}
1458
1459// Mark this tag so dependencies that use it are excluded from visibility enforcement.
1460func (t sdkLibraryComponentTag) ExcludeFromVisibilityEnforcement() {}
1461
1462var xmlPermissionsFileTag = sdkLibraryComponentTag{name: "xml-permissions-file"}
Paul Duffine74ac732020-02-06 13:51:46 +00001463
Jiyong Parke3833882020-02-17 17:28:10 +09001464func IsXmlPermissionsFileDepTag(depTag blueprint.DependencyTag) bool {
Paul Duffineedc5d52020-06-12 17:46:39 +01001465 if dt, ok := depTag.(sdkLibraryComponentTag); ok {
Jiyong Parke3833882020-02-17 17:28:10 +09001466 return dt == xmlPermissionsFileTag
1467 }
1468 return false
1469}
1470
Paul Duffineedc5d52020-06-12 17:46:39 +01001471var implLibraryTag = sdkLibraryComponentTag{name: "impl-library"}
Paul Duffin5df79302020-05-16 15:52:12 +01001472
Jeongik Chaaaa6dcd2024-05-22 00:41:28 +09001473var _ android.InstallNeededDependencyTag = sdkLibraryComponentTag{}
1474
Jihoon Kang46d66de2024-05-22 22:42:39 +00001475// To satisfy the CopyDirectlyInAnyApexTag interface. Implementation library of the sdk library
1476// in an apex is considered to be directly in the apex, as if it was listed in java_libs.
1477func (t sdkLibraryComponentTag) CopyDirectlyInAnyApex() {}
1478
1479var _ android.CopyDirectlyInAnyApexTag = implLibraryTag
1480
Jeongik Chaaaa6dcd2024-05-22 00:41:28 +09001481func (t sdkLibraryComponentTag) InstallDepNeeded() bool {
1482 return t.name == "xml-permissions-file" || t.name == "impl-library"
1483}
1484
Paul Duffin44f1d842020-06-26 20:17:02 +01001485// Add the dependencies on the child modules in the component deps mutator.
1486func (module *SdkLibrary) ComponentDepsMutator(ctx android.BottomUpMutatorContext) {
Paul Duffin3375e352020-04-28 10:44:03 +01001487 for _, apiScope := range module.getGeneratedApiScopes(ctx) {
Paul Duffind1b3a922020-01-22 11:57:20 +00001488 // Add dependencies to the stubs library
Spandan Das877f39d2023-03-29 16:19:51 +00001489 stubModuleName := module.stubsLibraryModuleName(apiScope)
Jihoon Kangbd093452023-12-26 19:08:01 +00001490 ctx.AddVariationDependencies(nil, apiScope.everythingStubsTag, stubModuleName)
Jihoon Kang1147b312023-06-08 23:25:57 +00001491
Jihoon Kangbd093452023-12-26 19:08:01 +00001492 exportableStubModuleName := module.exportableStubsLibraryModuleName(apiScope)
1493 ctx.AddVariationDependencies(nil, apiScope.exportableStubsTag, exportableStubModuleName)
Paul Duffind1b3a922020-01-22 11:57:20 +00001494
Paul Duffin15f34ef2020-07-20 18:04:44 +01001495 // Add a dependency on the stubs source in order to access both stubs source and api information.
1496 ctx.AddVariationDependencies(nil, apiScope.stubsSourceAndApiTag, module.stubsSourceModuleName(apiScope))
Paul Duffin958806b2022-05-16 13:10:47 +00001497
1498 if module.compareAgainstLatestApi(apiScope) {
1499 // Add dependencies on the latest finalized version of the API .txt file.
1500 latestApiModuleName := module.latestApiModuleName(apiScope)
1501 ctx.AddDependency(module, apiScope.latestApiModuleTag, latestApiModuleName)
1502
1503 // Add dependencies on the latest finalized version of the remove API .txt file.
1504 latestRemovedApiModuleName := module.latestRemovedApiModuleName(apiScope)
1505 ctx.AddDependency(module, apiScope.latestRemovedApiModuleTag, latestRemovedApiModuleName)
1506 }
Sundong Ahn054b19a2018-10-19 13:46:09 +09001507 }
1508
Paul Duffindfa131e2020-05-15 20:37:11 +01001509 if module.requiresRuntimeImplementationLibrary() {
Paul Duffin5df79302020-05-16 15:52:12 +01001510 // Add dependency to the rule for generating the implementation library.
1511 ctx.AddDependency(module, implLibraryTag, module.implLibraryModuleName())
1512
Paul Duffindfa131e2020-05-15 20:37:11 +01001513 if module.sharedLibrary() {
1514 // Add dependency to the rule for generating the xml permissions file
Paul Duffineedc5d52020-06-12 17:46:39 +01001515 ctx.AddDependency(module, xmlPermissionsFileTag, module.xmlPermissionsModuleName())
Paul Duffindfa131e2020-05-15 20:37:11 +01001516 }
Paul Duffin44f1d842020-06-26 20:17:02 +01001517 }
1518}
Paul Duffine74ac732020-02-06 13:51:46 +00001519
Paul Duffin44f1d842020-06-26 20:17:02 +01001520// Add other dependencies as normal.
1521func (module *SdkLibrary) DepsMutator(ctx android.BottomUpMutatorContext) {
Anton Hanssone77fccc2021-01-20 16:52:41 +00001522 var missingApiModules []string
1523 for _, apiScope := range module.getGeneratedApiScopes(ctx) {
1524 if apiScope.unstable {
1525 continue
1526 }
Paul Duffin958806b2022-05-16 13:10:47 +00001527 if m := module.latestApiModuleName(apiScope); !ctx.OtherModuleExists(m) {
Anton Hanssone77fccc2021-01-20 16:52:41 +00001528 missingApiModules = append(missingApiModules, m)
1529 }
Paul Duffin958806b2022-05-16 13:10:47 +00001530 if m := module.latestRemovedApiModuleName(apiScope); !ctx.OtherModuleExists(m) {
Anton Hanssone77fccc2021-01-20 16:52:41 +00001531 missingApiModules = append(missingApiModules, m)
1532 }
Paul Duffin958806b2022-05-16 13:10:47 +00001533 if m := module.latestIncompatibilitiesModuleName(apiScope); !ctx.OtherModuleExists(m) {
Jaewoong Jung1a97ee02021-03-09 13:25:02 -08001534 missingApiModules = append(missingApiModules, m)
1535 }
Anton Hanssone77fccc2021-01-20 16:52:41 +00001536 }
1537 if len(missingApiModules) != 0 && !module.sdkLibraryProperties.Unsafe_ignore_missing_latest_api {
1538 m := module.Name() + " is missing tracking files for previously released library versions.\n"
1539 m += "You need to do one of the following:\n"
1540 m += "- Add `unsafe_ignore_missing_latest_api: true` to your blueprint (to disable compat tracking)\n"
1541 m += "- Add a set of prebuilt txt files representing the last released version of this library for compat checking.\n"
1542 m += " (the current set of API files can be used as a seed for this compatibility tracking\n"
1543 m += "\n"
1544 m += "The following filegroup modules are missing:\n "
1545 m += strings.Join(missingApiModules, "\n ") + "\n"
1546 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."
1547 ctx.ModuleErrorf(m)
1548 }
Jiyong Parkc678ad32018-04-10 13:07:10 +09001549}
1550
Inseob Kimc0907f12019-02-08 21:00:45 +09001551func (module *SdkLibrary) GenerateAndroidBuildActions(ctx android.ModuleContext) {
Spandan Das5ae65ee2024-04-16 22:03:26 +00001552 if disableSourceApexVariant(ctx) {
1553 // Prebuilts are active, do not create the installation rules for the source javalib.
1554 // Even though the source javalib is not used, we need to hide it to prevent duplicate installation rules.
1555 // TODO (b/331665856): Implement a principled solution for this.
1556 module.HideFromMake()
1557 }
satayev8f088b02021-12-06 11:40:46 +00001558
Paul Duffina2ae7e02020-09-11 11:55:00 +01001559 module.generateCommonBuildActions(ctx)
1560
Jihoon Kanga3a05462024-04-05 00:36:44 +00001561 module.stem = proptools.StringDefault(module.overridableProperties.Stem, ctx.ModuleName())
1562
1563 module.provideHiddenAPIPropertyInfo(ctx)
Sundong Ahn054b19a2018-10-19 13:46:09 +09001564
Paul Duffinb97b1572021-04-29 21:50:40 +01001565 // Collate the components exported by this module. All scope specific modules are exported but
1566 // the impl and xml component modules are not.
1567 exportedComponents := map[string]struct{}{}
1568
Sundong Ahn57368eb2018-07-06 11:20:23 +09001569 // Record the paths to the header jars of the library (stubs and impl).
Paul Duffind1b3a922020-01-22 11:57:20 +00001570 // When this java_sdk_library is depended upon from others via "libs" property,
Jiyong Parkc678ad32018-04-10 13:07:10 +09001571 // the recorded paths will be returned depending on the link type of the caller.
1572 ctx.VisitDirectDeps(func(to android.Module) {
Jiyong Parkc678ad32018-04-10 13:07:10 +09001573 tag := ctx.OtherModuleDependencyTag(to)
1574
Paul Duffinc8782502020-04-29 20:45:27 +01001575 // Extract information from any of the scope specific dependencies.
1576 if scopeTag, ok := tag.(scopeDependencyTag); ok {
1577 apiScope := scopeTag.apiScope
Paul Duffin803a9562020-05-20 11:52:25 +01001578 scopePaths := module.getScopePathsCreateIfNeeded(apiScope)
Paul Duffinc8782502020-04-29 20:45:27 +01001579
1580 // Extract information from the dependency. The exact information extracted
1581 // is determined by the nature of the dependency which is determined by the tag.
1582 scopeTag.extractDepInfo(ctx, to, scopePaths)
Paul Duffinb97b1572021-04-29 21:50:40 +01001583
1584 exportedComponents[ctx.OtherModuleName(to)] = struct{}{}
Sundong Ahn20e998b2018-07-24 11:19:26 +09001585 }
Jihoon Kang8479dea2024-04-04 01:19:05 +00001586
1587 if tag == implLibraryTag {
1588 if dep, ok := android.OtherModuleProvider(ctx, to, JavaInfoProvider); ok {
1589 module.implLibraryHeaderJars = append(module.implLibraryHeaderJars, dep.HeaderJars...)
Jihoon Kanga3a05462024-04-05 00:36:44 +00001590 module.implLibraryModule = to.(*Library)
1591 android.SetProvider(ctx, JavaInfoProvider, dep)
Jihoon Kang8479dea2024-04-04 01:19:05 +00001592 }
1593 }
Jiyong Parkc678ad32018-04-10 13:07:10 +09001594 })
Paul Duffinb97b1572021-04-29 21:50:40 +01001595
Jihoon Kanga3a05462024-04-05 00:36:44 +00001596 apexInfo, _ := android.ModuleProvider(ctx, android.ApexInfoProvider)
1597 if !apexInfo.IsForPlatform() {
1598 module.hideApexVariantFromMake = true
1599 }
1600
1601 if module.implLibraryModule != nil {
1602 if ctx.Device() {
1603 module.classesJarPaths = android.Paths{module.implLibraryModule.implementationJarFile}
1604 module.bootDexJarPath = module.implLibraryModule.bootDexJarPath
1605 module.uncompressDexState = module.implLibraryModule.uncompressDexState
1606 module.active = module.implLibraryModule.active
1607 }
1608
1609 module.outputFile = module.implLibraryModule.outputFile
1610 module.dexJarFile = makeDexJarPathFromPath(module.implLibraryModule.dexJarFile.Path())
1611 module.headerJarFile = module.implLibraryModule.headerJarFile
1612 module.implementationAndResourcesJar = module.implLibraryModule.implementationAndResourcesJar
1613 module.builtInstalledForApex = module.implLibraryModule.builtInstalledForApex
1614 module.dexpreopter.configPath = module.implLibraryModule.dexpreopter.configPath
1615 module.dexpreopter.outputProfilePathOnHost = module.implLibraryModule.dexpreopter.outputProfilePathOnHost
1616
Jihoon Kang34155e32024-05-20 19:08:49 +00001617 // Properties required for Library.AndroidMkEntries
1618 module.logtagsSrcs = module.implLibraryModule.logtagsSrcs
1619 module.dexpreopter.builtInstalled = module.implLibraryModule.dexpreopter.builtInstalled
1620 module.jacocoReportClassesFile = module.implLibraryModule.jacocoReportClassesFile
1621 module.dexer.proguardDictionary = module.implLibraryModule.dexer.proguardDictionary
1622 module.dexer.proguardUsageZip = module.implLibraryModule.dexer.proguardUsageZip
1623 module.linter.reports = module.implLibraryModule.linter.reports
Jihoon Kang629e2a32024-06-25 20:47:49 +00001624 module.linter.outputs.depSets = module.implLibraryModule.LintDepSets()
Jihoon Kang34155e32024-05-20 19:08:49 +00001625
Jihoon Kanga3a05462024-04-05 00:36:44 +00001626 if !module.Host() {
1627 module.hostdexInstallFile = module.implLibraryModule.hostdexInstallFile
1628 }
1629
1630 android.SetProvider(ctx, blueprint.SrcsFileProviderKey, blueprint.SrcsFileProviderData{SrcPaths: module.implLibraryModule.uniqueSrcFiles.Strings()})
1631 }
1632
Paul Duffinb97b1572021-04-29 21:50:40 +01001633 // Make the set of components exported by this module available for use elsewhere.
Cole Faust18994c72023-02-28 16:02:16 -08001634 exportedComponentInfo := android.ExportedComponentsInfo{Components: android.SortedKeys(exportedComponents)}
Colin Cross40213022023-12-13 15:19:49 -08001635 android.SetProvider(ctx, android.ExportedComponentsInfoProvider, exportedComponentInfo)
Paul Duffin958806b2022-05-16 13:10:47 +00001636
1637 // Provide additional information for inclusion in an sdk's generated .info file.
1638 additionalSdkInfo := map[string]interface{}{}
1639 additionalSdkInfo["dist_stem"] = module.distStem()
Paul Duffine8409952022-09-22 16:24:46 +01001640 baseModuleName := module.distStem()
Paul Duffin958806b2022-05-16 13:10:47 +00001641 scopes := map[string]interface{}{}
1642 additionalSdkInfo["scopes"] = scopes
1643 for scope, scopePaths := range module.scopePaths {
1644 scopeInfo := map[string]interface{}{}
1645 scopes[scope.name] = scopeInfo
1646 scopeInfo["current_api"] = scope.snapshotRelativeCurrentApiTxtPath(baseModuleName)
1647 scopeInfo["removed_api"] = scope.snapshotRelativeRemovedApiTxtPath(baseModuleName)
Jihoon Kang5623e542024-01-31 23:27:26 +00001648 if p := scopePaths.latestApiPaths; len(p) > 0 {
1649 // The last path in the list is the one that applies to this scope, the
1650 // preceding ones, if any, are for the scope(s) that it extends.
1651 scopeInfo["latest_api"] = p[len(p)-1].String()
Paul Duffin958806b2022-05-16 13:10:47 +00001652 }
Jihoon Kang5623e542024-01-31 23:27:26 +00001653 if p := scopePaths.latestRemovedApiPaths; len(p) > 0 {
1654 // The last path in the list is the one that applies to this scope, the
1655 // preceding ones, if any, are for the scope(s) that it extends.
1656 scopeInfo["latest_removed_api"] = p[len(p)-1].String()
Paul Duffin958806b2022-05-16 13:10:47 +00001657 }
1658 }
Colin Cross40213022023-12-13 15:19:49 -08001659 android.SetProvider(ctx, android.AdditionalSdkInfoProvider, android.AdditionalSdkInfo{additionalSdkInfo})
mrziwang9f7b9f42024-07-10 12:18:06 -07001660 module.setOutputFiles(ctx)
1661 if module.requiresRuntimeImplementationLibrary() && module.implLibraryModule != nil {
1662 setOutputFiles(ctx, module.implLibraryModule.Module)
1663 }
Jiyong Parkc678ad32018-04-10 13:07:10 +09001664}
1665
Jihoon Kanga3a05462024-04-05 00:36:44 +00001666func (module *SdkLibrary) BuiltInstalledForApex() []dexpreopterInstall {
1667 return module.builtInstalledForApex
1668}
1669
Jiyong Park0b0e1b92019-12-03 13:24:29 +09001670func (module *SdkLibrary) AndroidMkEntries() []android.AndroidMkEntries {
Paul Duffindfa131e2020-05-15 20:37:11 +01001671 if !module.requiresRuntimeImplementationLibrary() {
Paul Duffin43db9be2019-12-30 17:35:49 +00001672 return nil
1673 }
Jiyong Park0b0e1b92019-12-03 13:24:29 +09001674 entriesList := module.Library.AndroidMkEntries()
Jihoon Kanga3a05462024-04-05 00:36:44 +00001675 entries := &entriesList[0]
1676 entries.Required = append(entries.Required, module.implLibraryModuleName())
Yo Chiang07d75072020-06-05 17:43:19 +08001677 if module.sharedLibrary() {
Yo Chiang07d75072020-06-05 17:43:19 +08001678 entries.Required = append(entries.Required, module.xmlPermissionsModuleName())
1679 }
Jiyong Park0b0e1b92019-12-03 13:24:29 +09001680 return entriesList
Jiyong Park82484c02018-04-23 21:41:26 +09001681}
1682
Anton Hansson5fd5d242020-03-27 19:43:19 +00001683// The dist path of the stub artifacts
1684func (module *SdkLibrary) apiDistPath(apiScope *apiScope) string {
Colin Crossf0eace92021-06-02 13:02:23 -07001685 return path.Join("apistubs", module.distGroup(), apiScope.name)
Anton Hansson5fd5d242020-03-27 19:43:19 +00001686}
1687
Paul Duffin12ceb462019-12-24 20:31:31 +00001688// Get the sdk version for use when compiling the stubs library.
Paul Duffin780c5f42020-05-12 15:52:55 +01001689func (module *SdkLibrary) sdkVersionForStubsLibrary(mctx android.EarlyModuleContext, apiScope *apiScope) string {
Paul Duffin87a05a32020-05-12 11:50:28 +01001690 scopeProperties := module.scopeToProperties[apiScope]
1691 if scopeProperties.Sdk_version != nil {
1692 return proptools.String(scopeProperties.Sdk_version)
1693 }
1694
Jiyong Parkf1691d22021-03-29 20:11:58 +09001695 sdkDep := decodeSdkDep(mctx, android.SdkContext(&module.Library))
Paul Duffin12ceb462019-12-24 20:31:31 +00001696 if sdkDep.hasStandardLibs() {
1697 // If building against a standard sdk then use the sdk version appropriate for the scope.
Paul Duffind1b3a922020-01-22 11:57:20 +00001698 return apiScope.sdkVersion
Paul Duffin12ceb462019-12-24 20:31:31 +00001699 } else {
1700 // Otherwise, use no system module.
1701 return "none"
1702 }
1703}
1704
Paul Duffin31310252020-11-20 21:26:20 +00001705func (module *SdkLibrary) distStem() string {
1706 return proptools.StringDefault(module.sdkLibraryProperties.Dist_stem, module.BaseModuleName())
1707}
1708
Colin Cross986b69a2021-06-01 13:13:40 -07001709// distGroup returns the subdirectory of the dist path of the stub artifacts.
1710func (module *SdkLibrary) distGroup() string {
Colin Cross59b92bf2021-06-01 14:07:56 -07001711 return proptools.StringDefault(module.sdkLibraryProperties.Dist_group, "unknown")
Colin Cross986b69a2021-06-01 13:13:40 -07001712}
1713
Paul Duffin958806b2022-05-16 13:10:47 +00001714func latestPrebuiltApiModuleName(name string, apiScope *apiScope) string {
1715 return PrebuiltApiModuleName(name, apiScope.name, "latest")
1716}
1717
Jihoon Kang748a24d2024-03-20 21:29:39 +00001718func latestPrebuiltApiCombinedModuleName(name string, apiScope *apiScope) string {
1719 return PrebuiltApiCombinedModuleName(name, apiScope.name, "latest")
1720}
1721
Paul Duffind1b3a922020-01-22 11:57:20 +00001722func (module *SdkLibrary) latestApiFilegroupName(apiScope *apiScope) string {
Paul Duffin958806b2022-05-16 13:10:47 +00001723 return ":" + module.latestApiModuleName(apiScope)
1724}
1725
1726func (module *SdkLibrary) latestApiModuleName(apiScope *apiScope) string {
Jihoon Kang748a24d2024-03-20 21:29:39 +00001727 return latestPrebuiltApiCombinedModuleName(module.distStem(), apiScope)
Jiyong Park58c518b2018-05-12 22:29:12 +09001728}
Jiyong Parkc678ad32018-04-10 13:07:10 +09001729
Paul Duffind1b3a922020-01-22 11:57:20 +00001730func (module *SdkLibrary) latestRemovedApiFilegroupName(apiScope *apiScope) string {
Paul Duffin958806b2022-05-16 13:10:47 +00001731 return ":" + module.latestRemovedApiModuleName(apiScope)
1732}
1733
1734func (module *SdkLibrary) latestRemovedApiModuleName(apiScope *apiScope) string {
Jihoon Kang748a24d2024-03-20 21:29:39 +00001735 return latestPrebuiltApiCombinedModuleName(module.distStem()+"-removed", apiScope)
Jiyong Parkc678ad32018-04-10 13:07:10 +09001736}
1737
Jaewoong Jung1a97ee02021-03-09 13:25:02 -08001738func (module *SdkLibrary) latestIncompatibilitiesFilegroupName(apiScope *apiScope) string {
Paul Duffin958806b2022-05-16 13:10:47 +00001739 return ":" + module.latestIncompatibilitiesModuleName(apiScope)
1740}
1741
1742func (module *SdkLibrary) latestIncompatibilitiesModuleName(apiScope *apiScope) string {
1743 return latestPrebuiltApiModuleName(module.distStem()+"-incompatibilities", apiScope)
Jaewoong Jung1a97ee02021-03-09 13:25:02 -08001744}
1745
Jihoon Kang1c92c3e2023-03-23 17:44:51 +00001746func (module *SdkLibrary) contributesToApiSurface(c android.Config) bool {
1747 _, exists := c.GetApiLibraries()[module.Name()]
1748 return exists
1749}
1750
Jihoon Kang0c705a42023-08-02 06:44:57 +00001751// The listed modules are the special java_sdk_libraries where apiScope.kind do not match the
1752// api surface that the module contribute to. For example, the public droidstubs and java_library
1753// do not contribute to the public api surface, but contributes to the core platform api surface.
1754// This method returns the full api surface stub lib that
1755// the generated java_api_library should depend on.
1756func (module *SdkLibrary) alternativeFullApiSurfaceStubLib() string {
1757 if val, ok := apiLibraryAdditionalProperties[module.Name()]; ok {
1758 return val.FullApiSurfaceStubLib
1759 }
1760 return ""
1761}
1762
1763// The listed modules' stubs contents do not match the corresponding txt files,
1764// but require additional api contributions to generate the full stubs.
1765// This method returns the name of the additional api contribution module
1766// for corresponding sdk_library modules.
1767func (module *SdkLibrary) apiLibraryAdditionalApiContribution() string {
1768 if val, ok := apiLibraryAdditionalProperties[module.Name()]; ok {
1769 return val.AdditionalApiContribution
1770 }
1771 return ""
1772}
1773
Anton Hansson944e77d2020-08-19 11:40:22 +01001774func childModuleVisibility(childVisibility []string) []string {
1775 if childVisibility == nil {
1776 // No child visibility set. The child will use the visibility of the sdk_library.
1777 return nil
1778 }
1779
1780 // Prepend an override to ignore the sdk_library's visibility, and rely on the child visibility.
1781 var visibility []string
1782 visibility = append(visibility, "//visibility:override")
1783 visibility = append(visibility, childVisibility...)
1784 return visibility
1785}
1786
Paul Duffin5df79302020-05-16 15:52:12 +01001787// Creates the implementation java library
1788func (module *SdkLibrary) createImplLibrary(mctx android.DefaultableHookContext) {
Anton Hansson944e77d2020-08-19 11:40:22 +01001789 visibility := childModuleVisibility(module.sdkLibraryProperties.Impl_library_visibility)
1790
Paul Duffin5df79302020-05-16 15:52:12 +01001791 props := struct {
Paul Duffin77590a82022-04-28 14:13:30 +00001792 Name *string
1793 Visibility []string
Paul Duffin77590a82022-04-28 14:13:30 +00001794 Libs []string
1795 Static_libs []string
1796 Apex_available []string
Jihoon Kanga3a05462024-04-05 00:36:44 +00001797 Stem *string
Paul Duffin5df79302020-05-16 15:52:12 +01001798 }{
1799 Name: proptools.StringPtr(module.implLibraryModuleName()),
Anton Hansson944e77d2020-08-19 11:40:22 +01001800 Visibility: visibility,
Jihoon Kanga3a05462024-04-05 00:36:44 +00001801
1802 Libs: append(module.properties.Libs, module.sdkLibraryProperties.Impl_only_libs...),
1803
1804 Static_libs: append(module.properties.Static_libs, module.sdkLibraryProperties.Impl_only_static_libs...),
Paul Duffin77590a82022-04-28 14:13:30 +00001805 // Pass the apex_available settings down so that the impl library can be statically
1806 // embedded within a library that is added to an APEX. Needed for updatable-media.
1807 Apex_available: module.ApexAvailable(),
Jihoon Kanga3a05462024-04-05 00:36:44 +00001808
1809 Stem: proptools.StringPtr(module.Name()),
Paul Duffin5df79302020-05-16 15:52:12 +01001810 }
1811
1812 properties := []interface{}{
1813 &module.properties,
1814 &module.protoProperties,
1815 &module.deviceProperties,
Liz Kammera7a64f32020-07-09 15:16:41 -07001816 &module.dexProperties,
Paul Duffin5df79302020-05-16 15:52:12 +01001817 &module.dexpreoptProperties,
Colin Cross014489c2020-06-02 20:09:13 -07001818 &module.linter.properties,
Spandan Dasb9c58352024-05-13 18:29:45 +00001819 &module.overridableProperties,
Paul Duffin5df79302020-05-16 15:52:12 +01001820 &props,
1821 module.sdkComponentPropertiesForChildLibrary(),
1822 }
1823 mctx.CreateModule(LibraryFactory, properties...)
1824}
1825
Jihoon Kangfa4a90d2023-12-20 02:53:38 +00001826type libraryProperties struct {
1827 Name *string
1828 Visibility []string
1829 Srcs []string
1830 Installable *bool
1831 Sdk_version *string
1832 System_modules *string
1833 Patch_module *string
1834 Libs []string
1835 Static_libs []string
1836 Compile_dex *bool
1837 Java_version *string
1838 Openjdk9 struct {
1839 Srcs []string
1840 Javacflags []string
1841 }
1842 Dist struct {
1843 Targets []string
1844 Dest *string
1845 Dir *string
1846 Tag *string
1847 }
Jihoon Kangfe914ed2024-02-12 22:49:21 +00001848 Is_stubs_module *bool
Jihoon Kangfa4a90d2023-12-20 02:53:38 +00001849}
Jiyong Parkc678ad32018-04-10 13:07:10 +09001850
Jihoon Kangfa4a90d2023-12-20 02:53:38 +00001851func (module *SdkLibrary) stubsLibraryProps(mctx android.DefaultableHookContext, apiScope *apiScope) libraryProperties {
1852 props := libraryProperties{}
Jihoon Kang786df932023-09-07 01:18:31 +00001853 props.Visibility = []string{"//visibility:override", "//visibility:private"}
Jiyong Parkc678ad32018-04-10 13:07:10 +09001854 // sources are generated from the droiddoc
Paul Duffin12ceb462019-12-24 20:31:31 +00001855 sdkVersion := module.sdkVersionForStubsLibrary(mctx, apiScope)
Paul Duffin52d398a2019-06-11 12:31:14 +01001856 props.Sdk_version = proptools.StringPtr(sdkVersion)
Paul Duffina18abc22020-05-16 18:54:24 +01001857 props.System_modules = module.deviceProperties.System_modules
1858 props.Patch_module = module.properties.Patch_module
Paul Duffin367ab912019-12-23 19:40:36 +00001859 props.Installable = proptools.BoolPtr(false)
Sundong Ahn054b19a2018-10-19 13:46:09 +09001860 props.Libs = module.sdkLibraryProperties.Stub_only_libs
Mark White9421c4c2023-08-10 00:07:03 +00001861 props.Libs = append(props.Libs, module.scopeToProperties[apiScope].Libs...)
Anton Hanssondae54cd2021-04-21 16:30:10 +01001862 props.Static_libs = module.sdkLibraryProperties.Stub_only_static_libs
Paul Duffine22c2ab2020-05-20 19:35:27 +01001863 // The stub-annotations library contains special versions of the annotations
1864 // with CLASS retention policy, so that they're kept.
1865 if proptools.Bool(module.sdkLibraryProperties.Annotations_enabled) {
1866 props.Libs = append(props.Libs, "stub-annotations")
1867 }
Paul Duffina18abc22020-05-16 18:54:24 +01001868 props.Openjdk9.Srcs = module.properties.Openjdk9.Srcs
1869 props.Openjdk9.Javacflags = module.properties.Openjdk9.Javacflags
Anton Hansson83509b52020-05-21 09:21:57 +01001870 // We compile the stubs for 1.8 in line with the main android.jar stubs, and potential
1871 // interop with older developer tools that don't support 1.9.
1872 props.Java_version = proptools.StringPtr("1.8")
Jihoon Kangfe914ed2024-02-12 22:49:21 +00001873 props.Is_stubs_module = proptools.BoolPtr(true)
Paul Duffinf4600f62021-05-13 22:34:45 +01001874
Jihoon Kangfa4a90d2023-12-20 02:53:38 +00001875 return props
1876}
1877
1878// Creates a static java library that has API stubs
1879func (module *SdkLibrary) createStubsLibrary(mctx android.DefaultableHookContext, apiScope *apiScope) {
1880
1881 props := module.stubsLibraryProps(mctx, apiScope)
1882 props.Name = proptools.StringPtr(module.sourceStubsLibraryModuleName(apiScope))
1883 props.Srcs = []string{":" + module.stubsSourceModuleName(apiScope)}
1884
1885 mctx.CreateModule(LibraryFactory, &props, module.sdkComponentPropertiesForChildLibrary())
1886}
1887
1888// Create a static java library that compiles the "exportable" stubs
1889func (module *SdkLibrary) createExportableStubsLibrary(mctx android.DefaultableHookContext, apiScope *apiScope) {
1890 props := module.stubsLibraryProps(mctx, apiScope)
1891 props.Name = proptools.StringPtr(module.exportableSourceStubsLibraryModuleName(apiScope))
1892 props.Srcs = []string{":" + module.stubsSourceModuleName(apiScope) + "{.exportable}"}
1893
Paul Duffin859fe962020-05-15 10:20:31 +01001894 mctx.CreateModule(LibraryFactory, &props, module.sdkComponentPropertiesForChildLibrary())
Jiyong Parkc678ad32018-04-10 13:07:10 +09001895}
1896
Paul Duffin6d0886e2020-04-07 18:49:53 +01001897// Creates a droidstubs module that creates stubs source files from the given full source
Paul Duffinc8782502020-04-29 20:45:27 +01001898// files and also updates and checks the API specification files.
Paul Duffin15f34ef2020-07-20 18:04:44 +01001899func (module *SdkLibrary) createStubsSourcesAndApi(mctx android.DefaultableHookContext, apiScope *apiScope, name string, scopeSpecificDroidstubsArgs []string) {
Jiyong Parkc678ad32018-04-10 13:07:10 +09001900 props := struct {
Sundong Ahn054b19a2018-10-19 13:46:09 +09001901 Name *string
Paul Duffin4911a892020-04-29 23:35:13 +01001902 Visibility []string
Sundong Ahn054b19a2018-10-19 13:46:09 +09001903 Srcs []string
1904 Installable *bool
Paul Duffin52d398a2019-06-11 12:31:14 +01001905 Sdk_version *string
Jihoon Kang3198f3c2023-01-26 08:08:52 +00001906 Api_surface *string
Paul Duffin12ceb462019-12-24 20:31:31 +00001907 System_modules *string
Sundong Ahn054b19a2018-10-19 13:46:09 +09001908 Libs []string
Paul Duffin6877e6d2020-09-25 19:59:14 +01001909 Output_javadoc_comments *bool
Paul Duffin11512472019-02-11 15:55:17 +00001910 Arg_files []string
Sundong Ahn054b19a2018-10-19 13:46:09 +09001911 Args *string
Sundong Ahn054b19a2018-10-19 13:46:09 +09001912 Java_version *string
Paul Duffine22c2ab2020-05-20 19:35:27 +01001913 Annotations_enabled *bool
Sundong Ahn054b19a2018-10-19 13:46:09 +09001914 Merge_annotations_dirs []string
1915 Merge_inclusion_annotations_dirs []string
Paul Duffin0ff08bd2020-04-29 13:30:54 +01001916 Generate_stubs *bool
Anton Hanssone87b03d2020-12-21 15:29:34 +00001917 Previous_api *string
Jihoon Kang6592e872023-12-19 01:13:16 +00001918 Aconfig_declarations []string
Sundong Ahn054b19a2018-10-19 13:46:09 +09001919 Check_api struct {
Anton Hanssone6056152020-12-31 10:37:27 +00001920 Current ApiToCheck
1921 Last_released ApiToCheck
Paul Duffin160fe412020-05-10 19:32:20 +01001922
1923 Api_lint struct {
1924 Enabled *bool
1925 New_since *string
1926 Baseline_file *string
1927 }
Jiyong Park58c518b2018-05-12 22:29:12 +09001928 }
Sundong Ahn1b92c822018-05-29 11:35:17 +09001929 Aidl struct {
1930 Include_dirs []string
1931 Local_include_dirs []string
1932 }
Paul Duffin040e9062020-11-23 17:41:36 +00001933 Dists []android.Dist
Jiyong Parkc678ad32018-04-10 13:07:10 +09001934 }{}
1935
Paul Duffin7b78b4d2020-04-28 14:08:32 +01001936 // The stubs source processing uses the same compile time classpath when extracting the
1937 // API from the implementation library as it does when compiling it. i.e. the same
1938 // * sdk version
1939 // * system_modules
1940 // * libs (static_libs/libs)
Paul Duffin250e6192019-06-07 10:44:37 +01001941
Paul Duffin0ff08bd2020-04-29 13:30:54 +01001942 props.Name = proptools.StringPtr(name)
Anton Hansson944e77d2020-08-19 11:40:22 +01001943 props.Visibility = childModuleVisibility(module.sdkLibraryProperties.Stubs_source_visibility)
Paul Duffina18abc22020-05-16 18:54:24 +01001944 props.Srcs = append(props.Srcs, module.properties.Srcs...)
Anton Hanssonf8ea3722021-09-16 14:24:13 +01001945 props.Srcs = append(props.Srcs, module.sdkLibraryProperties.Api_srcs...)
Paul Duffina18abc22020-05-16 18:54:24 +01001946 props.Sdk_version = module.deviceProperties.Sdk_version
Jihoon Kang3198f3c2023-01-26 08:08:52 +00001947 props.Api_surface = &apiScope.name
Paul Duffina18abc22020-05-16 18:54:24 +01001948 props.System_modules = module.deviceProperties.System_modules
Jiyong Parkc678ad32018-04-10 13:07:10 +09001949 props.Installable = proptools.BoolPtr(false)
Sundong Ahne6f0b052018-06-05 16:46:14 +09001950 // A droiddoc module has only one Libs property and doesn't distinguish between
1951 // shared libs and static libs. So we need to add both of these libs to Libs property.
Paul Duffina18abc22020-05-16 18:54:24 +01001952 props.Libs = module.properties.Libs
1953 props.Libs = append(props.Libs, module.properties.Static_libs...)
Nikita Ioffed732da72022-11-21 12:38:25 +00001954 props.Libs = append(props.Libs, module.sdkLibraryProperties.Stub_only_libs...)
Mark White9421c4c2023-08-10 00:07:03 +00001955 props.Libs = append(props.Libs, module.scopeToProperties[apiScope].Libs...)
Paul Duffina18abc22020-05-16 18:54:24 +01001956 props.Aidl.Include_dirs = module.deviceProperties.Aidl.Include_dirs
1957 props.Aidl.Local_include_dirs = module.deviceProperties.Aidl.Local_include_dirs
1958 props.Java_version = module.properties.Java_version
Jiyong Parkc678ad32018-04-10 13:07:10 +09001959
Paul Duffine22c2ab2020-05-20 19:35:27 +01001960 props.Annotations_enabled = module.sdkLibraryProperties.Annotations_enabled
Sundong Ahn054b19a2018-10-19 13:46:09 +09001961 props.Merge_annotations_dirs = module.sdkLibraryProperties.Merge_annotations_dirs
1962 props.Merge_inclusion_annotations_dirs = module.sdkLibraryProperties.Merge_inclusion_annotations_dirs
Jihoon Kang6592e872023-12-19 01:13:16 +00001963 props.Aconfig_declarations = module.sdkLibraryProperties.Aconfig_declarations
Sundong Ahn054b19a2018-10-19 13:46:09 +09001964
Paul Duffin6d0886e2020-04-07 18:49:53 +01001965 droidstubsArgs := []string{}
Paul Duffin235ffff2019-12-24 10:41:30 +00001966 if len(module.sdkLibraryProperties.Api_packages) != 0 {
Paul Duffin6d0886e2020-04-07 18:49:53 +01001967 droidstubsArgs = append(droidstubsArgs, "--stub-packages "+strings.Join(module.sdkLibraryProperties.Api_packages, ":"))
Paul Duffin235ffff2019-12-24 10:41:30 +00001968 }
1969 if len(module.sdkLibraryProperties.Hidden_api_packages) != 0 {
Paul Duffin6d0886e2020-04-07 18:49:53 +01001970 droidstubsArgs = append(droidstubsArgs,
Paul Duffin235ffff2019-12-24 10:41:30 +00001971 android.JoinWithPrefix(module.sdkLibraryProperties.Hidden_api_packages, " --hide-package "))
1972 }
Paul Duffin6d0886e2020-04-07 18:49:53 +01001973 droidstubsArgs = append(droidstubsArgs, module.sdkLibraryProperties.Droiddoc_options...)
Anton Hanssonfd1c0d22023-11-02 15:18:09 +00001974 disabledWarnings := []string{"HiddenSuperclass"}
1975 if proptools.BoolDefault(module.sdkLibraryProperties.Api_lint.Legacy_errors_allowed, true) {
1976 disabledWarnings = append(disabledWarnings,
1977 "BroadcastBehavior",
1978 "DeprecationMismatch",
1979 "MissingPermission",
1980 "SdkConstant",
1981 "Todo",
1982 )
Paul Duffin235ffff2019-12-24 10:41:30 +00001983 }
Paul Duffin6d0886e2020-04-07 18:49:53 +01001984 droidstubsArgs = append(droidstubsArgs, android.JoinWithPrefix(disabledWarnings, "--hide "))
Sundong Ahnfb2721f2018-09-17 13:23:09 +09001985
Paul Duffin6877e6d2020-09-25 19:59:14 +01001986 // Output Javadoc comments for public scope.
1987 if apiScope == apiScopePublic {
1988 props.Output_javadoc_comments = proptools.BoolPtr(true)
1989 }
1990
Paul Duffin1fb487d2020-04-07 18:50:10 +01001991 // Add in scope specific arguments.
Paul Duffin0ff08bd2020-04-29 13:30:54 +01001992 droidstubsArgs = append(droidstubsArgs, scopeSpecificDroidstubsArgs...)
Paul Duffin11512472019-02-11 15:55:17 +00001993 props.Arg_files = module.sdkLibraryProperties.Droiddoc_option_files
Paul Duffin6d0886e2020-04-07 18:49:53 +01001994 props.Args = proptools.StringPtr(strings.Join(droidstubsArgs, " "))
Jiyong Parkc678ad32018-04-10 13:07:10 +09001995
Paul Duffin15f34ef2020-07-20 18:04:44 +01001996 // List of APIs identified from the provided source files are created. They are later
1997 // compared against to the not-yet-released (a.k.a current) list of APIs and to the
1998 // last-released (a.k.a numbered) list of API.
1999 currentApiFileName := apiScope.apiFilePrefix + "current.txt"
2000 removedApiFileName := apiScope.apiFilePrefix + "removed.txt"
2001 apiDir := module.getApiDir()
2002 currentApiFileName = path.Join(apiDir, currentApiFileName)
2003 removedApiFileName = path.Join(apiDir, removedApiFileName)
Jiyong Parkc678ad32018-04-10 13:07:10 +09002004
Paul Duffin15f34ef2020-07-20 18:04:44 +01002005 // check against the not-yet-release API
2006 props.Check_api.Current.Api_file = proptools.StringPtr(currentApiFileName)
2007 props.Check_api.Current.Removed_api_file = proptools.StringPtr(removedApiFileName)
Jiyong Park58c518b2018-05-12 22:29:12 +09002008
Paul Duffin958806b2022-05-16 13:10:47 +00002009 if module.compareAgainstLatestApi(apiScope) {
Paul Duffin15f34ef2020-07-20 18:04:44 +01002010 // check against the latest released API
2011 latestApiFilegroupName := proptools.StringPtr(module.latestApiFilegroupName(apiScope))
Anton Hanssone87b03d2020-12-21 15:29:34 +00002012 props.Previous_api = latestApiFilegroupName
Paul Duffin15f34ef2020-07-20 18:04:44 +01002013 props.Check_api.Last_released.Api_file = latestApiFilegroupName
2014 props.Check_api.Last_released.Removed_api_file = proptools.StringPtr(
2015 module.latestRemovedApiFilegroupName(apiScope))
Jaewoong Jung1a97ee02021-03-09 13:25:02 -08002016 props.Check_api.Last_released.Baseline_file = proptools.StringPtr(
2017 module.latestIncompatibilitiesFilegroupName(apiScope))
Paul Duffin160fe412020-05-10 19:32:20 +01002018
Paul Duffin15f34ef2020-07-20 18:04:44 +01002019 if proptools.Bool(module.sdkLibraryProperties.Api_lint.Enabled) {
2020 // Enable api lint.
2021 props.Check_api.Api_lint.Enabled = proptools.BoolPtr(true)
2022 props.Check_api.Api_lint.New_since = latestApiFilegroupName
Paul Duffin160fe412020-05-10 19:32:20 +01002023
Paul Duffin15f34ef2020-07-20 18:04:44 +01002024 // If it exists then pass a lint-baseline.txt through to droidstubs.
2025 baselinePath := path.Join(apiDir, apiScope.apiFilePrefix+"lint-baseline.txt")
2026 baselinePathRelativeToRoot := path.Join(mctx.ModuleDir(), baselinePath)
2027 paths, err := mctx.GlobWithDeps(baselinePathRelativeToRoot, nil)
2028 if err != nil {
2029 mctx.ModuleErrorf("error checking for presence of %s: %s", baselinePathRelativeToRoot, err)
2030 }
2031 if len(paths) == 1 {
2032 props.Check_api.Api_lint.Baseline_file = proptools.StringPtr(baselinePath)
2033 } else if len(paths) != 0 {
2034 mctx.ModuleErrorf("error checking for presence of %s: expected one path, found: %v", baselinePathRelativeToRoot, paths)
Paul Duffin160fe412020-05-10 19:32:20 +01002035 }
2036 }
Paul Duffin15f34ef2020-07-20 18:04:44 +01002037 }
Jiyong Park58c518b2018-05-12 22:29:12 +09002038
Paul Duffin15f34ef2020-07-20 18:04:44 +01002039 if !Bool(module.sdkLibraryProperties.No_dist) {
Paul Duffin040e9062020-11-23 17:41:36 +00002040 // Dist the api txt and removed api txt artifacts for sdk builds.
2041 distDir := proptools.StringPtr(path.Join(module.apiDistPath(apiScope), "api"))
Jihoon Kang02168052024-03-20 00:44:54 +00002042 stubsTypeTagPrefix := ""
2043 if mctx.Config().ReleaseHiddenApiExportableStubs() {
2044 stubsTypeTagPrefix = ".exportable"
2045 }
Paul Duffin040e9062020-11-23 17:41:36 +00002046 for _, p := range []struct {
2047 tag string
2048 pattern string
2049 }{
Jihoon Kangd1799f62024-02-20 23:01:38 +00002050 // "exportable" api files are copied to the dist directory instead of the
Jihoon Kang02168052024-03-20 00:44:54 +00002051 // "everything" api files when "RELEASE_HIDDEN_API_EXPORTABLE_STUBS" build flag
2052 // is set. Otherwise, the "everything" api files are copied to the dist directory.
2053 {tag: "%s.api.txt", pattern: "%s.txt"},
2054 {tag: "%s.removed-api.txt", pattern: "%s-removed.txt"},
Paul Duffin040e9062020-11-23 17:41:36 +00002055 } {
2056 props.Dists = append(props.Dists, android.Dist{
2057 Targets: []string{"sdk", "win_sdk"},
2058 Dir: distDir,
2059 Dest: proptools.StringPtr(fmt.Sprintf(p.pattern, module.distStem())),
Jihoon Kang02168052024-03-20 00:44:54 +00002060 Tag: proptools.StringPtr(fmt.Sprintf(p.tag, stubsTypeTagPrefix)),
Paul Duffin040e9062020-11-23 17:41:36 +00002061 })
2062 }
Anton Hansson5fd5d242020-03-27 19:43:19 +00002063 }
2064
Spandan Das2cc80ba2023-10-27 17:21:52 +00002065 mctx.CreateModule(DroidstubsFactory, &props, module.sdkComponentPropertiesForChildLibrary()).(*Droidstubs).CallHookIfAvailable(mctx)
Jiyong Parkc678ad32018-04-10 13:07:10 +09002066}
2067
Jihoon Kang0c705a42023-08-02 06:44:57 +00002068func (module *SdkLibrary) createApiLibrary(mctx android.DefaultableHookContext, apiScope *apiScope, alternativeFullApiSurfaceStub string) {
Jihoon Kang1c92c3e2023-03-23 17:44:51 +00002069 props := struct {
Jihoon Kangca198c22023-06-22 23:13:51 +00002070 Name *string
2071 Visibility []string
2072 Api_contributions []string
2073 Libs []string
2074 Static_libs []string
2075 Full_api_surface_stub *string
Jihoon Kang4ec24872023-10-05 17:26:09 +00002076 System_modules *string
Jihoon Kang063ec002023-06-28 01:16:23 +00002077 Enable_validation *bool
Jihoon Kang5d701272024-02-15 21:53:49 +00002078 Stubs_type *string
Jihoon Kang1c92c3e2023-03-23 17:44:51 +00002079 }{}
2080
2081 props.Name = proptools.StringPtr(module.apiLibraryModuleName(apiScope))
Jihoon Kang786df932023-09-07 01:18:31 +00002082 props.Visibility = []string{"//visibility:override", "//visibility:private"}
Jihoon Kang1c92c3e2023-03-23 17:44:51 +00002083
2084 apiContributions := []string{}
2085
2086 // Api surfaces are not independent of each other, but have subset relationships,
2087 // and so does the api files. To generate from-text stubs for api surfaces other than public,
2088 // all subset api domains' api_contriubtions must be added as well.
2089 scope := apiScope
2090 for scope != nil {
2091 apiContributions = append(apiContributions, module.stubsSourceModuleName(scope)+".api.contribution")
2092 scope = scope.extends
2093 }
Jihoon Kang0c705a42023-08-02 06:44:57 +00002094 if apiScope == apiScopePublic {
2095 additionalApiContribution := module.apiLibraryAdditionalApiContribution()
2096 if additionalApiContribution != "" {
2097 apiContributions = append(apiContributions, additionalApiContribution)
2098 }
2099 }
Jihoon Kang1c92c3e2023-03-23 17:44:51 +00002100
2101 props.Api_contributions = apiContributions
2102 props.Libs = module.properties.Libs
2103 props.Libs = append(props.Libs, module.sdkLibraryProperties.Stub_only_libs...)
Mark White9421c4c2023-08-10 00:07:03 +00002104 props.Libs = append(props.Libs, module.scopeToProperties[apiScope].Libs...)
Jihoon Kang1c92c3e2023-03-23 17:44:51 +00002105 props.Libs = append(props.Libs, "stub-annotations")
2106 props.Static_libs = module.sdkLibraryProperties.Stub_only_static_libs
Jihoon Kange7ee2562023-07-25 05:51:46 +00002107 props.Full_api_surface_stub = proptools.StringPtr(apiScope.kind.DefaultJavaLibraryName())
Jihoon Kang0c705a42023-08-02 06:44:57 +00002108 if alternativeFullApiSurfaceStub != "" {
2109 props.Full_api_surface_stub = proptools.StringPtr(alternativeFullApiSurfaceStub)
2110 }
Jihoon Kang1c92c3e2023-03-23 17:44:51 +00002111
2112 // android_module_lib_stubs_current.from-text only comprises api contributions from art, conscrypt and i18n.
2113 // Thus, replace with android_module_lib_stubs_current_full.from-text, which comprises every api domains.
2114 if apiScope.kind == android.SdkModule {
Jihoon Kangca198c22023-06-22 23:13:51 +00002115 props.Full_api_surface_stub = proptools.StringPtr(apiScope.kind.DefaultJavaLibraryName() + "_full.from-text")
Jihoon Kang1c92c3e2023-03-23 17:44:51 +00002116 }
2117
Jihoon Kangd30ac8a2023-10-09 18:00:17 +00002118 // java_sdk_library modules that set sdk_version as none does not depend on other api
2119 // domains. Therefore, java_api_library created from such modules should not depend on
2120 // full_api_surface_stubs but create and compile stubs by the java_api_library module
2121 // itself.
2122 if module.SdkVersion(mctx).Kind == android.SdkNone {
2123 props.Full_api_surface_stub = nil
2124 }
2125
Jihoon Kang4ec24872023-10-05 17:26:09 +00002126 props.System_modules = module.deviceProperties.System_modules
Jihoon Kang063ec002023-06-28 01:16:23 +00002127 props.Enable_validation = proptools.BoolPtr(true)
Jihoon Kang5d701272024-02-15 21:53:49 +00002128 props.Stubs_type = proptools.StringPtr("everything")
Jihoon Kang4ec24872023-10-05 17:26:09 +00002129
Spandan Das2cc80ba2023-10-27 17:21:52 +00002130 mctx.CreateModule(ApiLibraryFactory, &props, module.sdkComponentPropertiesForChildLibrary())
Jihoon Kang1c92c3e2023-03-23 17:44:51 +00002131}
2132
Jihoon Kang02168052024-03-20 00:44:54 +00002133func (module *SdkLibrary) topLevelStubsLibraryProps(mctx android.DefaultableHookContext, apiScope *apiScope, doDist bool) libraryProperties {
Jihoon Kangfa4a90d2023-12-20 02:53:38 +00002134 props := libraryProperties{}
2135
Jihoon Kang1147b312023-06-08 23:25:57 +00002136 props.Visibility = childModuleVisibility(module.sdkLibraryProperties.Stubs_library_visibility)
2137 sdkVersion := module.sdkVersionForStubsLibrary(mctx, apiScope)
2138 props.Sdk_version = proptools.StringPtr(sdkVersion)
2139
Jihoon Kang1147b312023-06-08 23:25:57 +00002140 props.System_modules = module.deviceProperties.System_modules
2141
Jihoon Kang1147b312023-06-08 23:25:57 +00002142 // The imports need to be compiled to dex if the java_sdk_library requests it.
2143 compileDex := module.dexProperties.Compile_dex
2144 if module.stubLibrariesCompiledForDex() {
2145 compileDex = proptools.BoolPtr(true)
2146 }
2147 props.Compile_dex = compileDex
2148
Jihoon Kang02168052024-03-20 00:44:54 +00002149 if !Bool(module.sdkLibraryProperties.No_dist) && doDist {
2150 props.Dist.Targets = []string{"sdk", "win_sdk"}
2151 props.Dist.Dest = proptools.StringPtr(fmt.Sprintf("%v.jar", module.distStem()))
2152 props.Dist.Dir = proptools.StringPtr(module.apiDistPath(apiScope))
2153 props.Dist.Tag = proptools.StringPtr(".jar")
2154 }
2155
Jihoon Kangfa4a90d2023-12-20 02:53:38 +00002156 return props
2157}
2158
2159func (module *SdkLibrary) createTopLevelStubsLibrary(
2160 mctx android.DefaultableHookContext, apiScope *apiScope, contributesToApiSurface bool) {
2161
Jihoon Kang02168052024-03-20 00:44:54 +00002162 // Dist the "everything" stubs when the RELEASE_HIDDEN_API_EXPORTABLE_STUBS build flag is false
2163 doDist := !mctx.Config().ReleaseHiddenApiExportableStubs()
2164 props := module.topLevelStubsLibraryProps(mctx, apiScope, doDist)
Jihoon Kangfa4a90d2023-12-20 02:53:38 +00002165 props.Name = proptools.StringPtr(module.stubsLibraryModuleName(apiScope))
2166
2167 // Add the stub compiling java_library/java_api_library as static lib based on build config
2168 staticLib := module.sourceStubsLibraryModuleName(apiScope)
2169 if mctx.Config().BuildFromTextStub() && contributesToApiSurface {
2170 staticLib = module.apiLibraryModuleName(apiScope)
2171 }
2172 props.Static_libs = append(props.Static_libs, staticLib)
2173
2174 mctx.CreateModule(LibraryFactory, &props, module.sdkComponentPropertiesForChildLibrary())
2175}
2176
2177func (module *SdkLibrary) createTopLevelExportableStubsLibrary(
2178 mctx android.DefaultableHookContext, apiScope *apiScope) {
2179
Jihoon Kang02168052024-03-20 00:44:54 +00002180 // Dist the "exportable" stubs when the RELEASE_HIDDEN_API_EXPORTABLE_STUBS build flag is true
2181 doDist := mctx.Config().ReleaseHiddenApiExportableStubs()
2182 props := module.topLevelStubsLibraryProps(mctx, apiScope, doDist)
Jihoon Kangfa4a90d2023-12-20 02:53:38 +00002183 props.Name = proptools.StringPtr(module.exportableStubsLibraryModuleName(apiScope))
2184
Jihoon Kangfa4a90d2023-12-20 02:53:38 +00002185 staticLib := module.exportableSourceStubsLibraryModuleName(apiScope)
2186 props.Static_libs = append(props.Static_libs, staticLib)
2187
Jihoon Kang1147b312023-06-08 23:25:57 +00002188 mctx.CreateModule(LibraryFactory, &props, module.sdkComponentPropertiesForChildLibrary())
2189}
2190
Paul Duffin958806b2022-05-16 13:10:47 +00002191func (module *SdkLibrary) compareAgainstLatestApi(apiScope *apiScope) bool {
2192 return !(apiScope.unstable || module.sdkLibraryProperties.Unsafe_ignore_missing_latest_api)
2193}
2194
Paul Duffinea8f8082021-06-24 13:25:57 +01002195// Implements android.ApexModule
Jooyung Han5e9013b2020-03-10 06:23:13 +09002196func (module *SdkLibrary) DepIsInSameApex(mctx android.BaseModuleContext, dep android.Module) bool {
2197 depTag := mctx.OtherModuleDependencyTag(dep)
2198 if depTag == xmlPermissionsFileTag {
2199 return true
2200 }
Jihoon Kanga3a05462024-04-05 00:36:44 +00002201 if dep.Name() == module.implLibraryModuleName() {
2202 return true
2203 }
Jooyung Han5e9013b2020-03-10 06:23:13 +09002204 return module.Library.DepIsInSameApex(mctx, dep)
2205}
2206
Paul Duffinea8f8082021-06-24 13:25:57 +01002207// Implements android.ApexModule
2208func (module *SdkLibrary) UniqueApexVariations() bool {
2209 return module.uniqueApexVariations()
2210}
2211
Jihoon Kang80456fd2023-11-15 19:22:14 +00002212func (module *SdkLibrary) ContributeToApi() bool {
2213 return proptools.BoolDefault(module.sdkLibraryProperties.Contribute_to_android_api, false)
2214}
2215
Jiyong Parkc678ad32018-04-10 13:07:10 +09002216// Creates the xml file that publicizes the runtime library
Paul Duffinf0229202020-04-29 16:47:28 +01002217func (module *SdkLibrary) createXmlFile(mctx android.DefaultableHookContext) {
Spandan Das8c9ae7e2023-03-03 21:20:36 +00002218 moduleMinApiLevel := module.Library.MinSdkVersion(mctx)
Pedro Loureiroc3621422021-09-28 15:40:23 +00002219 var moduleMinApiLevelStr = moduleMinApiLevel.String()
2220 if moduleMinApiLevel == android.NoneApiLevel {
2221 moduleMinApiLevelStr = "current"
2222 }
Jiyong Parke3833882020-02-17 17:28:10 +09002223 props := struct {
Pedro Loureiroc3621422021-09-28 15:40:23 +00002224 Name *string
2225 Lib_name *string
2226 Apex_available []string
2227 On_bootclasspath_since *string
2228 On_bootclasspath_before *string
2229 Min_device_sdk *string
2230 Max_device_sdk *string
2231 Sdk_library_min_api_level *string
Jamie Garsidee570ace2023-11-27 12:07:36 +00002232 Uses_libs_dependencies []string
Jiyong Parke3833882020-02-17 17:28:10 +09002233 }{
Pedro Loureiroc3621422021-09-28 15:40:23 +00002234 Name: proptools.StringPtr(module.xmlPermissionsModuleName()),
2235 Lib_name: proptools.StringPtr(module.BaseModuleName()),
2236 Apex_available: module.ApexProperties.Apex_available,
2237 On_bootclasspath_since: module.commonSdkLibraryProperties.On_bootclasspath_since,
2238 On_bootclasspath_before: module.commonSdkLibraryProperties.On_bootclasspath_before,
2239 Min_device_sdk: module.commonSdkLibraryProperties.Min_device_sdk,
2240 Max_device_sdk: module.commonSdkLibraryProperties.Max_device_sdk,
2241 Sdk_library_min_api_level: &moduleMinApiLevelStr,
Jamie Garsidee570ace2023-11-27 12:07:36 +00002242 Uses_libs_dependencies: module.usesLibraryProperties.Uses_libs,
Jiyong Parkc678ad32018-04-10 13:07:10 +09002243 }
Jiyong Parke3833882020-02-17 17:28:10 +09002244
Jiyong Parke3833882020-02-17 17:28:10 +09002245 mctx.CreateModule(sdkLibraryXmlFactory, &props)
Jiyong Parkc678ad32018-04-10 13:07:10 +09002246}
2247
Jiyong Parkf1691d22021-03-29 20:11:58 +09002248func PrebuiltJars(ctx android.BaseModuleContext, baseName string, s android.SdkSpec) android.Paths {
Jiyong Park54105c42021-03-31 18:17:53 +09002249 var ver android.ApiLevel
Jiyong Parkf1691d22021-03-29 20:11:58 +09002250 var kind android.SdkKind
2251 if s.UsePrebuilt(ctx) {
Jiyong Park54105c42021-03-31 18:17:53 +09002252 ver = s.ApiLevel
Jiyong Parkf1691d22021-03-29 20:11:58 +09002253 kind = s.Kind
Jiyong Parkc678ad32018-04-10 13:07:10 +09002254 } else {
Jiyong Park6a927c42020-01-21 02:03:43 +09002255 // We don't have prebuilt SDK for the specific sdkVersion.
2256 // Instead of breaking the build, fallback to use "system_current"
Jiyong Park54105c42021-03-31 18:17:53 +09002257 ver = android.FutureApiLevel
Jiyong Parkf1691d22021-03-29 20:11:58 +09002258 kind = android.SdkSystem
Sundong Ahn054b19a2018-10-19 13:46:09 +09002259 }
Jiyong Park6a927c42020-01-21 02:03:43 +09002260
2261 dir := filepath.Join("prebuilts", "sdk", ver.String(), kind.String())
Paul Duffin50061512020-01-21 16:31:05 +00002262 jar := filepath.Join(dir, baseName+".jar")
Sundong Ahn054b19a2018-10-19 13:46:09 +09002263 jarPath := android.ExistentPathForSource(ctx, jar)
Sundong Ahnae418ac2019-02-28 15:01:28 +09002264 if !jarPath.Valid() {
Colin Cross07c88562020-01-07 09:34:44 -08002265 if ctx.Config().AllowMissingDependencies() {
2266 return android.Paths{android.PathForSource(ctx, jar)}
2267 } else {
Jiyong Parkf1691d22021-03-29 20:11:58 +09002268 ctx.PropertyErrorf("sdk_library", "invalid sdk version %q, %q does not exist", s.Raw, jar)
Colin Cross07c88562020-01-07 09:34:44 -08002269 }
Sundong Ahnae418ac2019-02-28 15:01:28 +09002270 return nil
2271 }
Sundong Ahn054b19a2018-10-19 13:46:09 +09002272 return android.Paths{jarPath.Path()}
2273}
2274
Colin Crossaede88c2020-08-11 12:17:01 -07002275// 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 +01002276//
2277// If either this or the other module are on the platform then this will return
2278// false.
Colin Cross56a83212020-09-15 18:30:11 -07002279func withinSameApexesAs(ctx android.BaseModuleContext, other android.Module) bool {
Colin Crossff694a82023-12-13 15:54:49 -08002280 apexInfo, _ := android.ModuleProvider(ctx, android.ApexInfoProvider)
Colin Cross313aa542023-12-13 13:47:44 -08002281 otherApexInfo, _ := android.OtherModuleProvider(ctx, other, android.ApexInfoProvider)
Jiyong Parkab50b072021-05-12 17:13:56 +09002282 return len(otherApexInfo.InApexVariants) > 0 && reflect.DeepEqual(apexInfo.InApexVariants, otherApexInfo.InApexVariants)
Paul Duffin9b879592020-05-26 13:21:35 +01002283}
2284
Jihoon Kang8479dea2024-04-04 01:19:05 +00002285func (module *SdkLibrary) sdkJars(ctx android.BaseModuleContext, sdkVersion android.SdkSpec) android.Paths {
Jiyong Park932cdfe2020-05-28 00:19:53 +09002286 // If the client doesn't set sdk_version, but if this library prefers stubs over
2287 // the impl library, let's provide the widest API surface possible. To do so,
2288 // force override sdk_version to module_current so that the closest possible API
2289 // surface could be found in selectHeaderJarsForSdkVersion
Jiyong Parkf1691d22021-03-29 20:11:58 +09002290 if module.defaultsToStubs() && !sdkVersion.Specified() {
Jiyong Park92315372021-04-02 08:45:46 +09002291 sdkVersion = android.SdkSpecFrom(ctx, "module_current")
Jiyong Park932cdfe2020-05-28 00:19:53 +09002292 }
Paul Duffind1b3a922020-01-22 11:57:20 +00002293
Paul Duffindaaa3322020-05-26 18:13:57 +01002294 // Only provide access to the implementation library if it is actually built.
2295 if module.requiresRuntimeImplementationLibrary() {
2296 // Check any special cases for java_sdk_library.
2297 //
2298 // Only allow access to the implementation library in the following condition:
2299 // * No sdk_version specified on the referencing module.
Paul Duffin9b879592020-05-26 13:21:35 +01002300 // * The referencing module is in the same apex as this.
Jiyong Parkf1691d22021-03-29 20:11:58 +09002301 if sdkVersion.Kind == android.SdkPrivate || withinSameApexesAs(ctx, module) {
Jihoon Kang8479dea2024-04-04 01:19:05 +00002302 return module.implLibraryHeaderJars
Sundong Ahn054b19a2018-10-19 13:46:09 +09002303 }
Jiyong Parkc678ad32018-04-10 13:07:10 +09002304 }
Paul Duffinb05d4292020-05-20 12:19:10 +01002305
Paul Duffin23970f42020-05-20 14:20:02 +01002306 return module.selectHeaderJarsForSdkVersion(ctx, sdkVersion)
Jiyong Parkc678ad32018-04-10 13:07:10 +09002307}
2308
Sundong Ahn241cd372018-07-13 16:16:44 +09002309// to satisfy SdkLibraryDependency interface
Jiyong Parkf1691d22021-03-29 20:11:58 +09002310func (module *SdkLibrary) SdkHeaderJars(ctx android.BaseModuleContext, sdkVersion android.SdkSpec) android.Paths {
Jihoon Kang8479dea2024-04-04 01:19:05 +00002311 return module.sdkJars(ctx, sdkVersion)
Sundong Ahn241cd372018-07-13 16:16:44 +09002312}
2313
Colin Cross571cccf2019-02-04 11:22:08 -08002314var javaSdkLibrariesKey = android.NewOnceKey("javaSdkLibraries")
2315
Jiyong Park82484c02018-04-23 21:41:26 +09002316func javaSdkLibraries(config android.Config) *[]string {
Colin Cross571cccf2019-02-04 11:22:08 -08002317 return config.Once(javaSdkLibrariesKey, func() interface{} {
Jiyong Park82484c02018-04-23 21:41:26 +09002318 return &[]string{}
2319 }).(*[]string)
2320}
2321
Paul Duffin749f98f2019-12-30 17:23:46 +00002322func (module *SdkLibrary) getApiDir() string {
2323 return proptools.StringDefault(module.sdkLibraryProperties.Api_dir, "api")
2324}
2325
Jiyong Parkc678ad32018-04-10 13:07:10 +09002326// For a java_sdk_library module, create internal modules for stubs, docs,
2327// runtime libs and xml file. If requested, the stubs and docs are created twice
2328// once for public API level and once for system API level
Paul Duffinf0229202020-04-29 16:47:28 +01002329func (module *SdkLibrary) CreateInternalModules(mctx android.DefaultableHookContext) {
2330 // If the module has been disabled then don't create any child modules.
Cole Fausta963b942024-04-11 17:43:00 -07002331 if !module.Enabled(mctx) {
Paul Duffinf0229202020-04-29 16:47:28 +01002332 return
2333 }
2334
Paul Duffina18abc22020-05-16 18:54:24 +01002335 if len(module.properties.Srcs) == 0 {
Inseob Kimc0907f12019-02-08 21:00:45 +09002336 mctx.PropertyErrorf("srcs", "java_sdk_library must specify srcs")
Jooyung Han58f26ab2019-12-18 15:34:32 +09002337 return
Inseob Kimc0907f12019-02-08 21:00:45 +09002338 }
2339
Paul Duffin37e0b772019-12-30 17:20:10 +00002340 // If this builds against standard libraries (i.e. is not part of the core libraries)
Paul Duffin4f5c1ef2020-11-19 14:53:43 +00002341 // then assume it provides both system and test apis.
Jiyong Parkf1691d22021-03-29 20:11:58 +09002342 sdkDep := decodeSdkDep(mctx, android.SdkContext(&module.Library))
Paul Duffin37e0b772019-12-30 17:20:10 +00002343 hasSystemAndTestApis := sdkDep.hasStandardLibs()
Paul Duffin3375e352020-04-28 10:44:03 +01002344 module.sdkLibraryProperties.Generate_system_and_test_apis = hasSystemAndTestApis
Paul Duffin4f5c1ef2020-11-19 14:53:43 +00002345
Jaewoong Jung18aefc12020-12-21 09:11:10 -08002346 missingCurrentApi := false
Inseob Kim8098faa2019-03-18 10:19:51 +09002347
Paul Duffin3375e352020-04-28 10:44:03 +01002348 generatedScopes := module.getGeneratedApiScopes(mctx)
Paul Duffind1b3a922020-01-22 11:57:20 +00002349
Paul Duffin749f98f2019-12-30 17:23:46 +00002350 apiDir := module.getApiDir()
Paul Duffin3375e352020-04-28 10:44:03 +01002351 for _, scope := range generatedScopes {
Inseob Kim8098faa2019-03-18 10:19:51 +09002352 for _, api := range []string{"current.txt", "removed.txt"} {
Paul Duffind1b3a922020-01-22 11:57:20 +00002353 path := path.Join(mctx.ModuleDir(), apiDir, scope.apiFilePrefix+api)
Inseob Kim8098faa2019-03-18 10:19:51 +09002354 p := android.ExistentPathForSource(mctx, path)
2355 if !p.Valid() {
Colin Cross18f840c2021-05-20 17:56:54 -07002356 if mctx.Config().AllowMissingDependencies() {
2357 mctx.AddMissingDependencies([]string{path})
2358 } else {
2359 mctx.ModuleErrorf("Current api file %#v doesn't exist", path)
2360 missingCurrentApi = true
2361 }
Inseob Kim8098faa2019-03-18 10:19:51 +09002362 }
2363 }
2364 }
2365
Jaewoong Jung18aefc12020-12-21 09:11:10 -08002366 if missingCurrentApi {
Inseob Kim8098faa2019-03-18 10:19:51 +09002367 script := "build/soong/scripts/gen-java-current-api-files.sh"
2368 p := android.ExistentPathForSource(mctx, script)
2369
2370 if !p.Valid() {
2371 panic(fmt.Sprintf("script file %s doesn't exist", script))
2372 }
2373
2374 mctx.ModuleErrorf("One or more current api files are missing. "+
2375 "You can update them by:\n"+
Paul Duffin37e0b772019-12-30 17:20:10 +00002376 "%s %q %s && m update-api",
Paul Duffind1b3a922020-01-22 11:57:20 +00002377 script, filepath.Join(mctx.ModuleDir(), apiDir),
Paul Duffin3375e352020-04-28 10:44:03 +01002378 strings.Join(generatedScopes.Strings(func(s *apiScope) string { return s.apiFilePrefix }), " "))
Inseob Kim8098faa2019-03-18 10:19:51 +09002379 return
2380 }
2381
Paul Duffin3375e352020-04-28 10:44:03 +01002382 for _, scope := range generatedScopes {
Paul Duffin15f34ef2020-07-20 18:04:44 +01002383 // Use the stubs source name for legacy reasons.
2384 module.createStubsSourcesAndApi(mctx, scope, module.stubsSourceModuleName(scope), scope.droidstubsArgs)
Paul Duffin0ff08bd2020-04-29 13:30:54 +01002385
Paul Duffind1b3a922020-01-22 11:57:20 +00002386 module.createStubsLibrary(mctx, scope)
Jihoon Kangfa4a90d2023-12-20 02:53:38 +00002387 module.createExportableStubsLibrary(mctx, scope)
Jihoon Kang1c92c3e2023-03-23 17:44:51 +00002388
Jihoon Kang0c705a42023-08-02 06:44:57 +00002389 alternativeFullApiSurfaceStubLib := ""
2390 if scope == apiScopePublic {
2391 alternativeFullApiSurfaceStubLib = module.alternativeFullApiSurfaceStubLib()
2392 }
2393 contributesToApiSurface := module.contributesToApiSurface(mctx.Config()) || alternativeFullApiSurfaceStubLib != ""
Jihoon Kang1147b312023-06-08 23:25:57 +00002394 if contributesToApiSurface {
Jihoon Kang0c705a42023-08-02 06:44:57 +00002395 module.createApiLibrary(mctx, scope, alternativeFullApiSurfaceStubLib)
Jihoon Kang1c92c3e2023-03-23 17:44:51 +00002396 }
Jihoon Kang1147b312023-06-08 23:25:57 +00002397
2398 module.createTopLevelStubsLibrary(mctx, scope, contributesToApiSurface)
Jihoon Kangfa4a90d2023-12-20 02:53:38 +00002399 module.createTopLevelExportableStubsLibrary(mctx, scope)
Inseob Kimc0907f12019-02-08 21:00:45 +09002400 }
2401
Paul Duffindfa131e2020-05-15 20:37:11 +01002402 if module.requiresRuntimeImplementationLibrary() {
Paul Duffin5df79302020-05-16 15:52:12 +01002403 // Create child module to create an implementation library.
2404 //
2405 // This temporarily creates a second implementation library that can be explicitly
2406 // referenced.
2407 //
2408 // TODO(b/156618935) - update comment once only one implementation library is created.
2409 module.createImplLibrary(mctx)
2410
Paul Duffindfa131e2020-05-15 20:37:11 +01002411 // Only create an XML permissions file that declares the library as being usable
2412 // as a shared library if required.
2413 if module.sharedLibrary() {
2414 module.createXmlFile(mctx)
2415 }
Paul Duffin43db9be2019-12-30 17:35:49 +00002416
2417 // record java_sdk_library modules so that they are exported to make
2418 javaSdkLibraries := javaSdkLibraries(mctx.Config())
2419 javaSdkLibrariesLock.Lock()
2420 defer javaSdkLibrariesLock.Unlock()
2421 *javaSdkLibraries = append(*javaSdkLibraries, module.BaseModuleName())
2422 }
Anton Hansson7f66efa2020-10-08 14:47:23 +01002423
Paul Duffin77590a82022-04-28 14:13:30 +00002424 // 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 +01002425 module.properties.Libs = append(module.properties.Libs, module.sdkLibraryProperties.Impl_only_libs...)
Paul Duffin77590a82022-04-28 14:13:30 +00002426 module.properties.Static_libs = append(module.properties.Static_libs, module.sdkLibraryProperties.Impl_only_static_libs...)
Inseob Kimc0907f12019-02-08 21:00:45 +09002427}
2428
2429func (module *SdkLibrary) InitSdkLibraryProperties() {
Colin Crossce6734e2020-06-15 16:09:53 -07002430 module.addHostAndDeviceProperties()
2431 module.AddProperties(&module.sdkLibraryProperties)
Sundong Ahn054b19a2018-10-19 13:46:09 +09002432
Paul Duffin71b33cc2021-06-23 11:39:47 +01002433 module.initSdkLibraryComponent(module)
Paul Duffin859fe962020-05-15 10:20:31 +01002434
Paul Duffina18abc22020-05-16 18:54:24 +01002435 module.properties.Installable = proptools.BoolPtr(true)
2436 module.deviceProperties.IsSDKLibrary = true
Inseob Kimc0907f12019-02-08 21:00:45 +09002437}
Sundong Ahn054b19a2018-10-19 13:46:09 +09002438
Paul Duffindfa131e2020-05-15 20:37:11 +01002439func (module *SdkLibrary) requiresRuntimeImplementationLibrary() bool {
2440 return !proptools.Bool(module.sdkLibraryProperties.Api_only)
2441}
2442
Jiyong Park932cdfe2020-05-28 00:19:53 +09002443func (module *SdkLibrary) defaultsToStubs() bool {
2444 return proptools.Bool(module.sdkLibraryProperties.Default_to_stubs)
2445}
2446
Paul Duffin1b1e8062020-05-08 13:44:43 +01002447// Defines how to name the individual component modules the sdk library creates.
2448type sdkLibraryComponentNamingScheme interface {
2449 stubsLibraryModuleName(scope *apiScope, baseName string) string
2450
2451 stubsSourceModuleName(scope *apiScope, baseName string) string
Jihoon Kang1c92c3e2023-03-23 17:44:51 +00002452
2453 apiLibraryModuleName(scope *apiScope, baseName string) string
Jihoon Kang1147b312023-06-08 23:25:57 +00002454
Jihoon Kangfa4a90d2023-12-20 02:53:38 +00002455 sourceStubsLibraryModuleName(scope *apiScope, baseName string) string
2456
2457 exportableStubsLibraryModuleName(scope *apiScope, baseName string) string
2458
2459 exportableSourceStubsLibraryModuleName(scope *apiScope, baseName string) string
Paul Duffin1b1e8062020-05-08 13:44:43 +01002460}
2461
2462type defaultNamingScheme struct {
2463}
2464
2465func (s *defaultNamingScheme) stubsLibraryModuleName(scope *apiScope, baseName string) string {
2466 return scope.stubsLibraryModuleName(baseName)
2467}
2468
2469func (s *defaultNamingScheme) stubsSourceModuleName(scope *apiScope, baseName string) string {
2470 return scope.stubsSourceModuleName(baseName)
2471}
2472
Jihoon Kang1c92c3e2023-03-23 17:44:51 +00002473func (s *defaultNamingScheme) apiLibraryModuleName(scope *apiScope, baseName string) string {
2474 return scope.apiLibraryModuleName(baseName)
2475}
2476
Jihoon Kangfa4a90d2023-12-20 02:53:38 +00002477func (s *defaultNamingScheme) sourceStubsLibraryModuleName(scope *apiScope, baseName string) string {
Jihoon Kang1147b312023-06-08 23:25:57 +00002478 return scope.sourceStubLibraryModuleName(baseName)
2479}
2480
Jihoon Kangfa4a90d2023-12-20 02:53:38 +00002481func (s *defaultNamingScheme) exportableStubsLibraryModuleName(scope *apiScope, baseName string) string {
2482 return scope.exportableStubsLibraryModuleName(baseName)
2483}
2484
2485func (s *defaultNamingScheme) exportableSourceStubsLibraryModuleName(scope *apiScope, baseName string) string {
2486 return scope.exportableSourceStubsLibraryModuleName(baseName)
2487}
2488
Paul Duffin1b1e8062020-05-08 13:44:43 +01002489var _ sdkLibraryComponentNamingScheme = (*defaultNamingScheme)(nil)
2490
Jihoon Kangfa4a90d2023-12-20 02:53:38 +00002491func hasStubsLibrarySuffix(name string, apiScope *apiScope) bool {
2492 return strings.HasSuffix(name, apiScope.stubsLibraryModuleNameSuffix()) ||
2493 strings.HasSuffix(name, apiScope.exportableStubsLibraryModuleNameSuffix())
2494}
2495
Jaewoong Jungbc15e3a2021-03-10 17:02:43 -08002496func moduleStubLinkType(name string) (stub bool, ret sdkLinkType) {
Jihoon Kang1147b312023-06-08 23:25:57 +00002497 name = strings.TrimSuffix(name, ".from-source")
2498
Anton Hansson2d0c1942020-05-25 12:20:51 +01002499 // This suffix-based approach is fragile and could potentially mis-trigger.
2500 // TODO(b/155164730): Clean this up when modules no longer reference sdk_lib stubs directly.
Jihoon Kangfa4a90d2023-12-20 02:53:38 +00002501 if hasStubsLibrarySuffix(name, apiScopePublic) {
Anton Hansson08f476b2021-04-07 15:32:19 +01002502 if name == "hwbinder.stubs" || name == "libcore_private.stubs" {
2503 // Due to a previous bug, these modules were not considered stubs, so we retain that.
2504 return false, javaPlatform
2505 }
Anton Hansson2d0c1942020-05-25 12:20:51 +01002506 return true, javaSdk
2507 }
Jihoon Kangfa4a90d2023-12-20 02:53:38 +00002508 if hasStubsLibrarySuffix(name, apiScopeSystem) {
Anton Hansson2d0c1942020-05-25 12:20:51 +01002509 return true, javaSystem
2510 }
Jihoon Kangfa4a90d2023-12-20 02:53:38 +00002511 if hasStubsLibrarySuffix(name, apiScopeModuleLib) {
Anton Hansson2d0c1942020-05-25 12:20:51 +01002512 return true, javaModule
2513 }
Jihoon Kangfa4a90d2023-12-20 02:53:38 +00002514 if hasStubsLibrarySuffix(name, apiScopeTest) {
Anton Hansson2d0c1942020-05-25 12:20:51 +01002515 return true, javaSystem
2516 }
Jihoon Kangfa4a90d2023-12-20 02:53:38 +00002517 if hasStubsLibrarySuffix(name, apiScopeSystemServer) {
Jihoon Kang1147b312023-06-08 23:25:57 +00002518 return true, javaSystemServer
2519 }
Anton Hansson2d0c1942020-05-25 12:20:51 +01002520 return false, javaPlatform
2521}
2522
Jaewoong Jung4f158ee2019-07-11 10:05:35 -07002523// java_sdk_library is a special Java library that provides optional platform APIs to apps.
2524// In practice, it can be viewed as a combination of several modules: 1) stubs library that clients
2525// are linked against to, 2) droiddoc module that internally generates API stubs source files,
2526// 3) the real runtime shared library that implements the APIs, and 4) XML file for adding
2527// the runtime lib to the classpath at runtime if requested via <uses-library>.
Inseob Kimc0907f12019-02-08 21:00:45 +09002528func SdkLibraryFactory() android.Module {
2529 module := &SdkLibrary{}
Paul Duffinc3091c82020-05-08 14:16:20 +01002530
2531 // Initialize information common between source and prebuilt.
Paul Duffin71b33cc2021-06-23 11:39:47 +01002532 module.initCommon(module)
Paul Duffinc3091c82020-05-08 14:16:20 +01002533
Inseob Kimc0907f12019-02-08 21:00:45 +09002534 module.InitSdkLibraryProperties()
Jooyung Han58f26ab2019-12-18 15:34:32 +09002535 android.InitApexModule(module)
Sundong Ahn054b19a2018-10-19 13:46:09 +09002536 InitJavaModule(module, android.HostAndDeviceSupported)
Paul Duffin3375e352020-04-28 10:44:03 +01002537
2538 // Initialize the map from scope to scope specific properties.
2539 scopeToProperties := make(map[*apiScope]*ApiScopeProperties)
Jihoon Kang98aa8fa2024-06-07 11:06:57 +00002540 for _, scope := range AllApiScopes {
Paul Duffin3375e352020-04-28 10:44:03 +01002541 scopeToProperties[scope] = scope.scopeSpecificProperties(module)
2542 }
2543 module.scopeToProperties = scopeToProperties
2544
Paul Duffin4911a892020-04-29 23:35:13 +01002545 // Add the properties containing visibility rules so that they are checked.
Paul Duffin5df79302020-05-16 15:52:12 +01002546 android.AddVisibilityProperty(module, "impl_library_visibility", &module.sdkLibraryProperties.Impl_library_visibility)
Paul Duffin4911a892020-04-29 23:35:13 +01002547 android.AddVisibilityProperty(module, "stubs_library_visibility", &module.sdkLibraryProperties.Stubs_library_visibility)
2548 android.AddVisibilityProperty(module, "stubs_source_visibility", &module.sdkLibraryProperties.Stubs_source_visibility)
2549
Paul Duffin1b1e8062020-05-08 13:44:43 +01002550 module.SetDefaultableHook(func(ctx android.DefaultableHookContext) {
Paul Duffindfa131e2020-05-15 20:37:11 +01002551 // If no implementation is required then it cannot be used as a shared library
2552 // either.
2553 if !module.requiresRuntimeImplementationLibrary() {
2554 // If shared_library has been explicitly set to true then it is incompatible
2555 // with api_only: true.
2556 if proptools.Bool(module.commonSdkLibraryProperties.Shared_library) {
2557 ctx.PropertyErrorf("api_only/shared_library", "inconsistent settings, shared_library and api_only cannot both be true")
2558 }
2559 // Set shared_library: false.
2560 module.commonSdkLibraryProperties.Shared_library = proptools.BoolPtr(false)
2561 }
2562
Paul Duffin1b1e8062020-05-08 13:44:43 +01002563 if module.initCommonAfterDefaultsApplied(ctx) {
2564 module.CreateInternalModules(ctx)
2565 }
2566 })
Jiyong Parkc678ad32018-04-10 13:07:10 +09002567 return module
2568}
Colin Cross79c7c262019-04-17 11:11:46 -07002569
2570//
2571// SDK library prebuilts
2572//
2573
Paul Duffin56d44902020-01-31 13:36:25 +00002574// Properties associated with each api scope.
2575type sdkLibraryScopeProperties struct {
Colin Cross79c7c262019-04-17 11:11:46 -07002576 Jars []string `android:"path"`
2577
2578 Sdk_version *string
2579
Colin Cross79c7c262019-04-17 11:11:46 -07002580 // List of shared java libs that this module has dependencies to
2581 Libs []string
Paul Duffin3d1248c2020-04-09 00:10:17 +01002582
Paul Duffinc8782502020-04-29 20:45:27 +01002583 // The stubs source.
Paul Duffin3d1248c2020-04-09 00:10:17 +01002584 Stub_srcs []string `android:"path"`
Paul Duffin1fd005d2020-04-09 01:08:11 +01002585
2586 // The current.txt
Paul Duffin0f8faff2020-05-20 16:18:00 +01002587 Current_api *string `android:"path"`
Paul Duffin1fd005d2020-04-09 01:08:11 +01002588
2589 // The removed.txt
Paul Duffin0f8faff2020-05-20 16:18:00 +01002590 Removed_api *string `android:"path"`
Anton Hanssond78eb762021-09-21 15:25:12 +01002591
2592 // Annotation zip
2593 Annotations *string `android:"path"`
Colin Cross79c7c262019-04-17 11:11:46 -07002594}
2595
Paul Duffin56d44902020-01-31 13:36:25 +00002596type sdkLibraryImportProperties struct {
Paul Duffinfcfd7912020-01-31 17:54:30 +00002597 // List of shared java libs, common to all scopes, that this module has
2598 // dependencies to
2599 Libs []string
Paul Duffin1267d872021-04-16 17:21:36 +01002600
2601 // If set to true, compile dex files for the stubs. Defaults to false.
2602 Compile_dex *bool
Paul Duffin869de142021-07-15 14:14:41 +01002603
2604 // If not empty, classes are restricted to the specified packages and their sub-packages.
Paul Duffin869de142021-07-15 14:14:41 +01002605 Permitted_packages []string
Spandan Das23956d12024-01-19 00:22:22 +00002606
2607 // Name of the source soong module that gets shadowed by this prebuilt
2608 // If unspecified, follows the naming convention that the source module of
2609 // the prebuilt is Name() without "prebuilt_" prefix
2610 Source_module_name *string
Paul Duffin56d44902020-01-31 13:36:25 +00002611}
2612
Paul Duffineedc5d52020-06-12 17:46:39 +01002613type SdkLibraryImport struct {
Colin Cross79c7c262019-04-17 11:11:46 -07002614 android.ModuleBase
2615 android.DefaultableModuleBase
2616 prebuilt android.Prebuilt
Paul Duffindd46f712020-02-10 13:37:10 +00002617 android.ApexModuleBase
Colin Cross79c7c262019-04-17 11:11:46 -07002618
Paul Duffin37856732021-02-26 14:24:15 +00002619 hiddenAPI
Jiakai Zhang204356f2021-09-09 08:12:46 +00002620 dexpreopter
Paul Duffin37856732021-02-26 14:24:15 +00002621
Colin Cross79c7c262019-04-17 11:11:46 -07002622 properties sdkLibraryImportProperties
2623
Paul Duffin46a26a82020-04-07 19:27:04 +01002624 // Map from api scope to the scope specific property structure.
2625 scopeProperties map[*apiScope]*sdkLibraryScopeProperties
2626
Paul Duffin56d44902020-01-31 13:36:25 +00002627 commonToSdkLibraryAndImport
Paul Duffineedc5d52020-06-12 17:46:39 +01002628
Paul Duffineedc5d52020-06-12 17:46:39 +01002629 // The reference to the xml permissions module created by the source module.
2630 // Is nil if the source module does not exist.
2631 xmlPermissionsFileModule *sdkLibraryXml
Paul Duffin39853512021-02-26 11:09:39 +00002632
Jeongik Chad5fe8782021-07-08 01:13:11 +09002633 // Build path to the dex implementation jar obtained from the prebuilt_apex, if any.
Spandan Dasfae468e2023-12-12 23:23:53 +00002634 dexJarFile OptionalDexJarPath
2635 dexJarFileErr error
Jeongik Chad5fe8782021-07-08 01:13:11 +09002636
2637 // Expected install file path of the source module(sdk_library)
2638 // or dex implementation jar obtained from the prebuilt_apex, if any.
2639 installFile android.Path
Colin Cross79c7c262019-04-17 11:11:46 -07002640}
2641
Paul Duffineedc5d52020-06-12 17:46:39 +01002642var _ SdkLibraryDependency = (*SdkLibraryImport)(nil)
Colin Cross79c7c262019-04-17 11:11:46 -07002643
Paul Duffin46a26a82020-04-07 19:27:04 +01002644// The type of a structure that contains a field of type sdkLibraryScopeProperties
2645// for each apiscope in allApiScopes, e.g. something like:
Colin Crossd079e0b2022-08-16 10:27:33 -07002646//
2647// struct {
2648// Public sdkLibraryScopeProperties
2649// System sdkLibraryScopeProperties
2650// ...
2651// }
Paul Duffin46a26a82020-04-07 19:27:04 +01002652var allScopeStructType = createAllScopePropertiesStructType()
2653
2654// Dynamically create a structure type for each apiscope in allApiScopes.
2655func createAllScopePropertiesStructType() reflect.Type {
2656 var fields []reflect.StructField
Jihoon Kang98aa8fa2024-06-07 11:06:57 +00002657 for _, apiScope := range AllApiScopes {
Paul Duffin46a26a82020-04-07 19:27:04 +01002658 field := reflect.StructField{
2659 Name: apiScope.fieldName,
2660 Type: reflect.TypeOf(sdkLibraryScopeProperties{}),
2661 }
2662 fields = append(fields, field)
2663 }
2664
2665 return reflect.StructOf(fields)
2666}
2667
2668// Create an instance of the scope specific structure type and return a map
2669// from apiscope to a pointer to each scope specific field.
2670func createPropertiesInstance() (interface{}, map[*apiScope]*sdkLibraryScopeProperties) {
2671 allScopePropertiesPtr := reflect.New(allScopeStructType)
2672 allScopePropertiesStruct := allScopePropertiesPtr.Elem()
2673 scopeProperties := make(map[*apiScope]*sdkLibraryScopeProperties)
2674
Jihoon Kang98aa8fa2024-06-07 11:06:57 +00002675 for _, apiScope := range AllApiScopes {
Paul Duffin46a26a82020-04-07 19:27:04 +01002676 field := allScopePropertiesStruct.FieldByName(apiScope.fieldName)
2677 scopeProperties[apiScope] = field.Addr().Interface().(*sdkLibraryScopeProperties)
2678 }
2679
2680 return allScopePropertiesPtr.Interface(), scopeProperties
2681}
2682
Jaewoong Jung4f158ee2019-07-11 10:05:35 -07002683// java_sdk_library_import imports a prebuilt java_sdk_library.
Colin Cross79c7c262019-04-17 11:11:46 -07002684func sdkLibraryImportFactory() android.Module {
Paul Duffineedc5d52020-06-12 17:46:39 +01002685 module := &SdkLibraryImport{}
Colin Cross79c7c262019-04-17 11:11:46 -07002686
Paul Duffin46a26a82020-04-07 19:27:04 +01002687 allScopeProperties, scopeToProperties := createPropertiesInstance()
2688 module.scopeProperties = scopeToProperties
Jiakai Zhang9c4dc192023-02-09 00:09:24 +08002689 module.AddProperties(&module.properties, allScopeProperties, &module.importDexpreoptProperties)
Colin Cross79c7c262019-04-17 11:11:46 -07002690
Paul Duffinc3091c82020-05-08 14:16:20 +01002691 // Initialize information common between source and prebuilt.
Paul Duffin71b33cc2021-06-23 11:39:47 +01002692 module.initCommon(module)
Paul Duffinc3091c82020-05-08 14:16:20 +01002693
Paul Duffin0bdcb272020-02-06 15:24:57 +00002694 android.InitPrebuiltModule(module, &[]string{""})
Paul Duffindd46f712020-02-10 13:37:10 +00002695 android.InitApexModule(module)
Colin Cross79c7c262019-04-17 11:11:46 -07002696 InitJavaModule(module, android.HostAndDeviceSupported)
2697
Paul Duffin1b1e8062020-05-08 13:44:43 +01002698 module.SetDefaultableHook(func(mctx android.DefaultableHookContext) {
2699 if module.initCommonAfterDefaultsApplied(mctx) {
2700 module.createInternalModules(mctx)
2701 }
2702 })
Colin Cross79c7c262019-04-17 11:11:46 -07002703 return module
2704}
2705
Paul Duffin630b11e2021-07-15 13:35:26 +01002706var _ PermittedPackagesForUpdatableBootJars = (*SdkLibraryImport)(nil)
2707
2708func (module *SdkLibraryImport) PermittedPackagesForUpdatableBootJars() []string {
2709 return module.properties.Permitted_packages
2710}
2711
Paul Duffineedc5d52020-06-12 17:46:39 +01002712func (module *SdkLibraryImport) Prebuilt() *android.Prebuilt {
Colin Cross79c7c262019-04-17 11:11:46 -07002713 return &module.prebuilt
2714}
2715
Paul Duffineedc5d52020-06-12 17:46:39 +01002716func (module *SdkLibraryImport) Name() string {
Colin Cross79c7c262019-04-17 11:11:46 -07002717 return module.prebuilt.Name(module.ModuleBase.Name())
2718}
2719
Spandan Das23956d12024-01-19 00:22:22 +00002720func (module *SdkLibraryImport) BaseModuleName() string {
2721 return proptools.StringDefault(module.properties.Source_module_name, module.ModuleBase.Name())
2722}
2723
Paul Duffineedc5d52020-06-12 17:46:39 +01002724func (module *SdkLibraryImport) createInternalModules(mctx android.DefaultableHookContext) {
Colin Cross79c7c262019-04-17 11:11:46 -07002725
Paul Duffin50061512020-01-21 16:31:05 +00002726 // If the build is configured to use prebuilts then force this to be preferred.
Jeongik Cha816a23a2020-07-08 01:09:23 +09002727 if mctx.Config().AlwaysUsePrebuiltSdks() {
Paul Duffin50061512020-01-21 16:31:05 +00002728 module.prebuilt.ForcePrefer()
2729 }
2730
Paul Duffin46a26a82020-04-07 19:27:04 +01002731 for apiScope, scopeProperties := range module.scopeProperties {
Paul Duffin56d44902020-01-31 13:36:25 +00002732 if len(scopeProperties.Jars) == 0 {
2733 continue
2734 }
2735
Paul Duffinbbb546b2020-04-09 00:07:11 +01002736 module.createJavaImportForStubs(mctx, apiScope, scopeProperties)
Paul Duffin3d1248c2020-04-09 00:10:17 +01002737
Paul Duffin0f8faff2020-05-20 16:18:00 +01002738 if len(scopeProperties.Stub_srcs) > 0 {
2739 module.createPrebuiltStubsSources(mctx, apiScope, scopeProperties)
2740 }
Jihoon Kang71c86832023-09-13 01:01:53 +00002741
2742 if scopeProperties.Current_api != nil {
2743 module.createPrebuiltApiContribution(mctx, apiScope, scopeProperties)
2744 }
Paul Duffin56d44902020-01-31 13:36:25 +00002745 }
Colin Cross79c7c262019-04-17 11:11:46 -07002746
2747 javaSdkLibraries := javaSdkLibraries(mctx.Config())
2748 javaSdkLibrariesLock.Lock()
2749 defer javaSdkLibrariesLock.Unlock()
2750 *javaSdkLibraries = append(*javaSdkLibraries, module.BaseModuleName())
2751}
2752
Paul Duffineedc5d52020-06-12 17:46:39 +01002753func (module *SdkLibraryImport) createJavaImportForStubs(mctx android.DefaultableHookContext, apiScope *apiScope, scopeProperties *sdkLibraryScopeProperties) {
Paul Duffinbbb546b2020-04-09 00:07:11 +01002754 // Creates a java import for the jar with ".stubs" suffix
2755 props := struct {
Spandan Das23956d12024-01-19 00:22:22 +00002756 Name *string
2757 Source_module_name *string
2758 Created_by_java_sdk_library_name *string
2759 Sdk_version *string
2760 Libs []string
2761 Jars []string
2762 Compile_dex *bool
Jihoon Kangfe914ed2024-02-12 22:49:21 +00002763 Is_stubs_module *bool
Paul Duffinbf4de042022-09-27 12:41:52 +01002764
2765 android.UserSuppliedPrebuiltProperties
Paul Duffinbbb546b2020-04-09 00:07:11 +01002766 }{}
Paul Duffinc3091c82020-05-08 14:16:20 +01002767 props.Name = proptools.StringPtr(module.stubsLibraryModuleName(apiScope))
Spandan Das23956d12024-01-19 00:22:22 +00002768 props.Source_module_name = proptools.StringPtr(apiScope.stubsLibraryModuleName(module.BaseModuleName()))
2769 props.Created_by_java_sdk_library_name = proptools.StringPtr(module.RootLibraryName())
Paul Duffinbbb546b2020-04-09 00:07:11 +01002770 props.Sdk_version = scopeProperties.Sdk_version
2771 // Prepend any of the libs from the legacy public properties to the libs for each of the
2772 // scopes to avoid having to duplicate them in each scope.
2773 props.Libs = append(module.properties.Libs, scopeProperties.Libs...)
2774 props.Jars = scopeProperties.Jars
Paul Duffin1dbe3ca2020-05-16 09:57:59 +01002775
Paul Duffin38b57852020-05-13 16:08:09 +01002776 // The imports are preferred if the java_sdk_library_import is preferred.
Paul Duffinbf4de042022-09-27 12:41:52 +01002777 props.CopyUserSuppliedPropertiesFromPrebuilt(&module.prebuilt)
Paul Duffin859fe962020-05-15 10:20:31 +01002778
Paul Duffin1267d872021-04-16 17:21:36 +01002779 // The imports need to be compiled to dex if the java_sdk_library_import requests it.
Paul Duffinf4600f62021-05-13 22:34:45 +01002780 compileDex := module.properties.Compile_dex
2781 if module.stubLibrariesCompiledForDex() {
2782 compileDex = proptools.BoolPtr(true)
2783 }
2784 props.Compile_dex = compileDex
Jihoon Kangfe914ed2024-02-12 22:49:21 +00002785 props.Is_stubs_module = proptools.BoolPtr(true)
Paul Duffin1267d872021-04-16 17:21:36 +01002786
Paul Duffin859fe962020-05-15 10:20:31 +01002787 mctx.CreateModule(ImportFactory, &props, module.sdkComponentPropertiesForChildLibrary())
Paul Duffinbbb546b2020-04-09 00:07:11 +01002788}
2789
Paul Duffineedc5d52020-06-12 17:46:39 +01002790func (module *SdkLibraryImport) createPrebuiltStubsSources(mctx android.DefaultableHookContext, apiScope *apiScope, scopeProperties *sdkLibraryScopeProperties) {
Paul Duffin3d1248c2020-04-09 00:10:17 +01002791 props := struct {
Spandan Das23956d12024-01-19 00:22:22 +00002792 Name *string
2793 Source_module_name *string
2794 Created_by_java_sdk_library_name *string
2795 Srcs []string
Paul Duffinbf4de042022-09-27 12:41:52 +01002796
2797 android.UserSuppliedPrebuiltProperties
Paul Duffin3d1248c2020-04-09 00:10:17 +01002798 }{}
Paul Duffinc3091c82020-05-08 14:16:20 +01002799 props.Name = proptools.StringPtr(module.stubsSourceModuleName(apiScope))
Spandan Das23956d12024-01-19 00:22:22 +00002800 props.Source_module_name = proptools.StringPtr(apiScope.stubsSourceModuleName(module.BaseModuleName()))
2801 props.Created_by_java_sdk_library_name = proptools.StringPtr(module.RootLibraryName())
Paul Duffin3d1248c2020-04-09 00:10:17 +01002802 props.Srcs = scopeProperties.Stub_srcs
Paul Duffin38b57852020-05-13 16:08:09 +01002803
2804 // The stubs source is preferred if the java_sdk_library_import is preferred.
Paul Duffinbf4de042022-09-27 12:41:52 +01002805 props.CopyUserSuppliedPropertiesFromPrebuilt(&module.prebuilt)
2806
Spandan Das2cc80ba2023-10-27 17:21:52 +00002807 mctx.CreateModule(PrebuiltStubsSourcesFactory, &props, module.sdkComponentPropertiesForChildLibrary())
Paul Duffin3d1248c2020-04-09 00:10:17 +01002808}
2809
Jihoon Kang71c86832023-09-13 01:01:53 +00002810func (module *SdkLibraryImport) createPrebuiltApiContribution(mctx android.DefaultableHookContext, apiScope *apiScope, scopeProperties *sdkLibraryScopeProperties) {
2811 api_file := scopeProperties.Current_api
2812 api_surface := &apiScope.name
2813
2814 props := struct {
Spandan Das23956d12024-01-19 00:22:22 +00002815 Name *string
2816 Source_module_name *string
2817 Created_by_java_sdk_library_name *string
2818 Api_surface *string
2819 Api_file *string
2820 Visibility []string
Jihoon Kang71c86832023-09-13 01:01:53 +00002821 }{}
2822
2823 props.Name = proptools.StringPtr(module.stubsSourceModuleName(apiScope) + ".api.contribution")
Spandan Das23956d12024-01-19 00:22:22 +00002824 props.Source_module_name = proptools.StringPtr(apiScope.stubsSourceModuleName(module.BaseModuleName()) + ".api.contribution")
2825 props.Created_by_java_sdk_library_name = proptools.StringPtr(module.RootLibraryName())
Jihoon Kang71c86832023-09-13 01:01:53 +00002826 props.Api_surface = api_surface
2827 props.Api_file = api_file
2828 props.Visibility = []string{"//visibility:override", "//visibility:public"}
2829
Spandan Das2cc80ba2023-10-27 17:21:52 +00002830 mctx.CreateModule(ApiContributionImportFactory, &props, module.sdkComponentPropertiesForChildLibrary())
Jihoon Kang71c86832023-09-13 01:01:53 +00002831}
2832
Paul Duffin44f1d842020-06-26 20:17:02 +01002833// Add the dependencies on the child module in the component deps mutator so that it
2834// creates references to the prebuilt and not the source modules.
2835func (module *SdkLibraryImport) ComponentDepsMutator(ctx android.BottomUpMutatorContext) {
Paul Duffin46a26a82020-04-07 19:27:04 +01002836 for apiScope, scopeProperties := range module.scopeProperties {
Paul Duffin56d44902020-01-31 13:36:25 +00002837 if len(scopeProperties.Jars) == 0 {
2838 continue
2839 }
2840
2841 // Add dependencies to the prebuilt stubs library
Jihoon Kangb7431552024-01-22 19:40:08 +00002842 ctx.AddVariationDependencies(nil, apiScope.prebuiltStubsTag, android.PrebuiltNameFromSource(module.stubsLibraryModuleName(apiScope)))
Paul Duffin0f8faff2020-05-20 16:18:00 +01002843
2844 if len(scopeProperties.Stub_srcs) > 0 {
2845 // Add dependencies to the prebuilt stubs source library
Paul Duffin864116c2021-04-02 10:24:13 +01002846 ctx.AddVariationDependencies(nil, apiScope.stubsSourceTag, android.PrebuiltNameFromSource(module.stubsSourceModuleName(apiScope)))
Paul Duffin0f8faff2020-05-20 16:18:00 +01002847 }
Paul Duffin56d44902020-01-31 13:36:25 +00002848 }
Paul Duffin44f1d842020-06-26 20:17:02 +01002849}
2850
2851// Add other dependencies as normal.
2852func (module *SdkLibraryImport) DepsMutator(ctx android.BottomUpMutatorContext) {
Paul Duffineedc5d52020-06-12 17:46:39 +01002853
2854 implName := module.implLibraryModuleName()
2855 if ctx.OtherModuleExists(implName) {
2856 ctx.AddVariationDependencies(nil, implLibraryTag, implName)
2857
2858 xmlPermissionsModuleName := module.xmlPermissionsModuleName()
2859 if module.sharedLibrary() && ctx.OtherModuleExists(xmlPermissionsModuleName) {
2860 // Add dependency to the rule for generating the xml permissions file
2861 ctx.AddDependency(module, xmlPermissionsFileTag, xmlPermissionsModuleName)
2862 }
2863 }
Colin Cross79c7c262019-04-17 11:11:46 -07002864}
2865
Jiyong Park45bf82e2020-12-15 22:29:02 +09002866var _ android.ApexModule = (*SdkLibraryImport)(nil)
2867
2868// Implements android.ApexModule
Paul Duffineedc5d52020-06-12 17:46:39 +01002869func (module *SdkLibraryImport) DepIsInSameApex(mctx android.BaseModuleContext, dep android.Module) bool {
2870 depTag := mctx.OtherModuleDependencyTag(dep)
2871 if depTag == xmlPermissionsFileTag {
2872 return true
2873 }
2874
2875 // None of the other dependencies of the java_sdk_library_import are in the same apex
2876 // as the one that references this module.
2877 return false
2878}
2879
Jiyong Park45bf82e2020-12-15 22:29:02 +09002880// Implements android.ApexModule
Dan Albertc8060532020-07-22 22:32:17 -07002881func (module *SdkLibraryImport) ShouldSupportSdkVersion(ctx android.BaseModuleContext,
2882 sdkVersion android.ApiLevel) error {
Jooyung Han749dc692020-04-15 11:03:39 +09002883 // we don't check prebuilt modules for sdk_version
2884 return nil
2885}
2886
Paul Duffinea8f8082021-06-24 13:25:57 +01002887// Implements android.ApexModule
2888func (module *SdkLibraryImport) UniqueApexVariations() bool {
2889 return module.uniqueApexVariations()
2890}
2891
Paul Duffin09817d62022-04-28 17:45:11 +01002892// MinSdkVersion - Implements hiddenAPIModule
Spandan Das8c9ae7e2023-03-03 21:20:36 +00002893func (module *SdkLibraryImport) MinSdkVersion(ctx android.EarlyModuleContext) android.ApiLevel {
2894 return android.NoneApiLevel
Paul Duffin09817d62022-04-28 17:45:11 +01002895}
2896
2897var _ hiddenAPIModule = (*SdkLibraryImport)(nil)
2898
Paul Duffineedc5d52020-06-12 17:46:39 +01002899func (module *SdkLibraryImport) GenerateAndroidBuildActions(ctx android.ModuleContext) {
Paul Duffina2ae7e02020-09-11 11:55:00 +01002900 module.generateCommonBuildActions(ctx)
2901
Jeongik Chad5fe8782021-07-08 01:13:11 +09002902 // Assume that source module(sdk_library) is installed in /<sdk_library partition>/framework
2903 module.installFile = android.PathForModuleInstall(ctx, "framework", module.Stem()+".jar")
2904
Paul Duffin0f8faff2020-05-20 16:18:00 +01002905 // Record the paths to the prebuilt stubs library and stubs source.
Colin Cross79c7c262019-04-17 11:11:46 -07002906 ctx.VisitDirectDeps(func(to android.Module) {
2907 tag := ctx.OtherModuleDependencyTag(to)
2908
Paul Duffin0f8faff2020-05-20 16:18:00 +01002909 // Extract information from any of the scope specific dependencies.
2910 if scopeTag, ok := tag.(scopeDependencyTag); ok {
2911 apiScope := scopeTag.apiScope
2912 scopePaths := module.getScopePathsCreateIfNeeded(apiScope)
2913
2914 // Extract information from the dependency. The exact information extracted
2915 // is determined by the nature of the dependency which is determined by the tag.
2916 scopeTag.extractDepInfo(ctx, to, scopePaths)
Paul Duffineedc5d52020-06-12 17:46:39 +01002917 } else if tag == implLibraryTag {
2918 if implLibrary, ok := to.(*Library); ok {
2919 module.implLibraryModule = implLibrary
2920 } else {
2921 ctx.ModuleErrorf("implementation library must be of type *java.Library but was %T", to)
2922 }
2923 } else if tag == xmlPermissionsFileTag {
2924 if xmlPermissionsFileModule, ok := to.(*sdkLibraryXml); ok {
2925 module.xmlPermissionsFileModule = xmlPermissionsFileModule
2926 } else {
2927 ctx.ModuleErrorf("xml permissions file module must be of type *sdkLibraryXml but was %T", to)
2928 }
Colin Cross79c7c262019-04-17 11:11:46 -07002929 }
2930 })
Paul Duffin0f8faff2020-05-20 16:18:00 +01002931
2932 // Populate the scope paths with information from the properties.
2933 for apiScope, scopeProperties := range module.scopeProperties {
2934 if len(scopeProperties.Jars) == 0 {
2935 continue
2936 }
2937
2938 paths := module.getScopePathsCreateIfNeeded(apiScope)
Anton Hanssond78eb762021-09-21 15:25:12 +01002939 paths.annotationsZip = android.OptionalPathForModuleSrc(ctx, scopeProperties.Annotations)
Paul Duffin0f8faff2020-05-20 16:18:00 +01002940 paths.currentApiFilePath = android.OptionalPathForModuleSrc(ctx, scopeProperties.Current_api)
2941 paths.removedApiFilePath = android.OptionalPathForModuleSrc(ctx, scopeProperties.Removed_api)
2942 }
Paul Duffin39853512021-02-26 11:09:39 +00002943
2944 if ctx.Device() {
2945 // If this is a variant created for a prebuilt_apex then use the dex implementation jar
2946 // obtained from the associated deapexer module.
Colin Crossff694a82023-12-13 15:54:49 -08002947 ai, _ := android.ModuleProvider(ctx, android.ApexInfoProvider)
Paul Duffin39853512021-02-26 11:09:39 +00002948 if ai.ForPrebuiltApex {
Paul Duffin39853512021-02-26 11:09:39 +00002949 // Get the path of the dex implementation jar from the `deapexer` module.
Spandan Dasfae468e2023-12-12 23:23:53 +00002950 di, err := android.FindDeapexerProviderForModule(ctx)
2951 if err != nil {
2952 // An error was found, possibly due to multiple apexes in the tree that export this library
2953 // Defer the error till a client tries to call DexJarBuildPath
2954 module.dexJarFileErr = err
Spandan Das3a392012024-01-17 18:26:27 +00002955 module.initHiddenAPIError(err)
Spandan Dasfae468e2023-12-12 23:23:53 +00002956 return
Martin Stjernholm44825602021-09-17 01:44:12 +01002957 }
Spandan Das5be63332023-12-13 00:06:32 +00002958 dexJarFileApexRootRelative := ApexRootRelativePathToJavaLib(module.BaseModuleName())
Jiakai Zhang81e46812023-02-08 21:56:07 +08002959 if dexOutputPath := di.PrebuiltExportPath(dexJarFileApexRootRelative); dexOutputPath != nil {
Martin Stjernholm8be1e6d2021-09-15 03:34:04 +01002960 dexJarFile := makeDexJarPathFromPath(dexOutputPath)
2961 module.dexJarFile = dexJarFile
Jiakai Zhang204356f2021-09-09 08:12:46 +00002962 installPath := android.PathForModuleInPartitionInstall(
Jiakai Zhang81e46812023-02-08 21:56:07 +08002963 ctx, "apex", ai.ApexVariationName, dexJarFileApexRootRelative)
Jiakai Zhang204356f2021-09-09 08:12:46 +00002964 module.installFile = installPath
Martin Stjernholm8be1e6d2021-09-15 03:34:04 +01002965 module.initHiddenAPI(ctx, dexJarFile, module.findScopePaths(apiScopePublic).stubsImplPath[0], nil)
Jiakai Zhang204356f2021-09-09 08:12:46 +00002966
Spandan Dase21a8d42024-01-23 23:56:29 +00002967 module.dexpreopter.installPath = module.dexpreopter.getInstallPath(ctx, android.RemoveOptionalPrebuiltPrefix(ctx.ModuleName()), installPath)
Jiakai Zhang204356f2021-09-09 08:12:46 +00002968 module.dexpreopter.isSDKLibrary = true
Spandan Dase21a8d42024-01-23 23:56:29 +00002969 module.dexpreopter.uncompressedDex = shouldUncompressDex(ctx, android.RemoveOptionalPrebuiltPrefix(ctx.ModuleName()), &module.dexpreopter)
Jiakai Zhang81e46812023-02-08 21:56:07 +08002970
2971 if profilePath := di.PrebuiltExportPath(dexJarFileApexRootRelative + ".prof"); profilePath != nil {
2972 module.dexpreopter.inputProfilePathOnHost = profilePath
2973 }
Paul Duffin39853512021-02-26 11:09:39 +00002974 } else {
2975 // This should never happen as a variant for a prebuilt_apex is only created if the
2976 // prebuilt_apex has been configured to export the java library dex file.
Martin Stjernholm44825602021-09-17 01:44:12 +01002977 ctx.ModuleErrorf("internal error: no dex implementation jar available from prebuilt APEX %s", di.ApexModuleName())
Paul Duffin39853512021-02-26 11:09:39 +00002978 }
2979 }
2980 }
mrziwang9f7b9f42024-07-10 12:18:06 -07002981
2982 module.setOutputFiles(ctx)
2983 if module.implLibraryModule != nil {
2984 setOutputFiles(ctx, module.implLibraryModule.Module)
2985 }
Colin Cross79c7c262019-04-17 11:11:46 -07002986}
2987
Jiyong Parkf1691d22021-03-29 20:11:58 +09002988func (module *SdkLibraryImport) sdkJars(ctx android.BaseModuleContext, sdkVersion android.SdkSpec, headerJars bool) android.Paths {
Paul Duffineedc5d52020-06-12 17:46:39 +01002989
2990 // For consistency with SdkLibrary make the implementation jar available to libraries that
2991 // are within the same APEX.
2992 implLibraryModule := module.implLibraryModule
Colin Cross56a83212020-09-15 18:30:11 -07002993 if implLibraryModule != nil && withinSameApexesAs(ctx, module) {
Paul Duffineedc5d52020-06-12 17:46:39 +01002994 if headerJars {
2995 return implLibraryModule.HeaderJars()
2996 } else {
2997 return implLibraryModule.ImplementationJars()
2998 }
2999 }
3000
Paul Duffin23970f42020-05-20 14:20:02 +01003001 return module.selectHeaderJarsForSdkVersion(ctx, sdkVersion)
Paul Duffin56d44902020-01-31 13:36:25 +00003002}
3003
Colin Cross79c7c262019-04-17 11:11:46 -07003004// to satisfy SdkLibraryDependency interface
Jiyong Parkf1691d22021-03-29 20:11:58 +09003005func (module *SdkLibraryImport) SdkHeaderJars(ctx android.BaseModuleContext, sdkVersion android.SdkSpec) android.Paths {
Colin Cross79c7c262019-04-17 11:11:46 -07003006 // This module is just a wrapper for the prebuilt stubs.
Paul Duffineedc5d52020-06-12 17:46:39 +01003007 return module.sdkJars(ctx, sdkVersion, true)
Colin Cross79c7c262019-04-17 11:11:46 -07003008}
3009
Ulya Trafimovichdbf31662020-12-17 12:07:54 +00003010// to satisfy UsesLibraryDependency interface
Spandan Das59a4a2b2024-01-09 21:35:56 +00003011func (module *SdkLibraryImport) DexJarBuildPath(ctx android.ModuleErrorfContext) OptionalDexJarPath {
Paul Duffin39853512021-02-26 11:09:39 +00003012 // The dex implementation jar extracted from the .apex file should be used in preference to the
3013 // source.
Spandan Dasfae468e2023-12-12 23:23:53 +00003014 if module.dexJarFileErr != nil {
Spandan Das59a4a2b2024-01-09 21:35:56 +00003015 ctx.ModuleErrorf(module.dexJarFileErr.Error())
Spandan Dasfae468e2023-12-12 23:23:53 +00003016 }
Martin Stjernholm8be1e6d2021-09-15 03:34:04 +01003017 if module.dexJarFile.IsSet() {
Paul Duffin39853512021-02-26 11:09:39 +00003018 return module.dexJarFile
3019 }
Paul Duffineedc5d52020-06-12 17:46:39 +01003020 if module.implLibraryModule == nil {
Martin Stjernholm8be1e6d2021-09-15 03:34:04 +01003021 return makeUnsetDexJarPath()
Paul Duffineedc5d52020-06-12 17:46:39 +01003022 } else {
Spandan Das59a4a2b2024-01-09 21:35:56 +00003023 return module.implLibraryModule.DexJarBuildPath(ctx)
Paul Duffineedc5d52020-06-12 17:46:39 +01003024 }
3025}
3026
Ulya Trafimovichdbf31662020-12-17 12:07:54 +00003027// to satisfy UsesLibraryDependency interface
Ulya Trafimovich31e444e2020-08-14 17:32:16 +01003028func (module *SdkLibraryImport) DexJarInstallPath() android.Path {
Jeongik Chad5fe8782021-07-08 01:13:11 +09003029 return module.installFile
Ulya Trafimovich31e444e2020-08-14 17:32:16 +01003030}
3031
Ulya Trafimovichdbf31662020-12-17 12:07:54 +00003032// to satisfy UsesLibraryDependency interface
3033func (module *SdkLibraryImport) ClassLoaderContexts() dexpreopt.ClassLoaderContextMap {
3034 return nil
3035}
3036
Paul Duffineedc5d52020-06-12 17:46:39 +01003037// to satisfy apex.javaDependency interface
3038func (module *SdkLibraryImport) JacocoReportClassesFile() android.Path {
3039 if module.implLibraryModule == nil {
3040 return nil
3041 } else {
3042 return module.implLibraryModule.JacocoReportClassesFile()
3043 }
3044}
3045
3046// to satisfy apex.javaDependency interface
Colin Cross08dca382020-07-21 20:31:17 -07003047func (module *SdkLibraryImport) LintDepSets() LintDepSets {
3048 if module.implLibraryModule == nil {
3049 return LintDepSets{}
3050 } else {
3051 return module.implLibraryModule.LintDepSets()
3052 }
3053}
3054
Spandan Das17854f52022-01-14 21:19:14 +00003055func (module *SdkLibraryImport) GetStrictUpdatabilityLinting() bool {
Jaewoong Jung476b9d62021-05-10 15:30:00 -07003056 if module.implLibraryModule == nil {
3057 return false
3058 } else {
Spandan Das17854f52022-01-14 21:19:14 +00003059 return module.implLibraryModule.GetStrictUpdatabilityLinting()
Jaewoong Jung476b9d62021-05-10 15:30:00 -07003060 }
3061}
3062
Spandan Das17854f52022-01-14 21:19:14 +00003063func (module *SdkLibraryImport) SetStrictUpdatabilityLinting(strictLinting bool) {
Jaewoong Jung476b9d62021-05-10 15:30:00 -07003064 if module.implLibraryModule != nil {
Spandan Das17854f52022-01-14 21:19:14 +00003065 module.implLibraryModule.SetStrictUpdatabilityLinting(strictLinting)
Jaewoong Jung476b9d62021-05-10 15:30:00 -07003066 }
3067}
3068
Colin Cross08dca382020-07-21 20:31:17 -07003069// to satisfy apex.javaDependency interface
Paul Duffineedc5d52020-06-12 17:46:39 +01003070func (module *SdkLibraryImport) Stem() string {
3071 return module.BaseModuleName()
Colin Cross79c7c262019-04-17 11:11:46 -07003072}
Jiyong Parke3833882020-02-17 17:28:10 +09003073
Paul Duffin44b481b2020-06-17 16:59:43 +01003074var _ ApexDependency = (*SdkLibraryImport)(nil)
3075
3076// to satisfy java.ApexDependency interface
3077func (module *SdkLibraryImport) HeaderJars() android.Paths {
3078 if module.implLibraryModule == nil {
3079 return nil
3080 } else {
3081 return module.implLibraryModule.HeaderJars()
3082 }
3083}
3084
3085// to satisfy java.ApexDependency interface
3086func (module *SdkLibraryImport) ImplementationAndResourcesJars() android.Paths {
3087 if module.implLibraryModule == nil {
3088 return nil
3089 } else {
3090 return module.implLibraryModule.ImplementationAndResourcesJars()
3091 }
3092}
3093
Jiakai Zhang204356f2021-09-09 08:12:46 +00003094// to satisfy java.DexpreopterInterface interface
3095func (module *SdkLibraryImport) IsInstallable() bool {
3096 return true
3097}
3098
Paul Duffinfef55002021-06-17 14:56:05 +01003099var _ android.RequiredFilesFromPrebuiltApex = (*SdkLibraryImport)(nil)
3100
Paul Duffinb4bbf2c2021-06-17 15:59:07 +01003101func (module *SdkLibraryImport) RequiredFilesFromPrebuiltApex(ctx android.BaseModuleContext) []string {
Paul Duffinfef55002021-06-17 14:56:05 +01003102 name := module.BaseModuleName()
Jiakai Zhang81e46812023-02-08 21:56:07 +08003103 return requiredFilesFromPrebuiltApexForImport(name, &module.dexpreopter)
Paul Duffinfef55002021-06-17 14:56:05 +01003104}
3105
Spandan Das2ea84dd2024-01-25 22:12:50 +00003106func (j *SdkLibraryImport) UseProfileGuidedDexpreopt() bool {
3107 return proptools.Bool(j.importDexpreoptProperties.Dex_preopt.Profile_guided)
3108}
3109
Jiyong Parke3833882020-02-17 17:28:10 +09003110// java_sdk_library_xml
Jiyong Parke3833882020-02-17 17:28:10 +09003111type sdkLibraryXml struct {
3112 android.ModuleBase
3113 android.DefaultableModuleBase
3114 android.ApexModuleBase
3115
3116 properties sdkLibraryXmlProperties
3117
3118 outputFilePath android.OutputPath
3119 installDirPath android.InstallPath
Colin Cross56a83212020-09-15 18:30:11 -07003120
3121 hideApexVariantFromMake bool
Jiyong Parke3833882020-02-17 17:28:10 +09003122}
3123
3124type sdkLibraryXmlProperties struct {
3125 // canonical name of the lib
3126 Lib_name *string
Pedro Loureiro9956e5e2021-09-07 17:21:59 +00003127
3128 // Signals that this shared library is part of the bootclasspath starting
3129 // on the version indicated in this attribute.
3130 //
3131 // This will make platforms at this level and above to ignore
3132 // <uses-library> tags with this library name because the library is already
3133 // available
3134 On_bootclasspath_since *string
3135
3136 // Signals that this shared library was part of the bootclasspath before
3137 // (but not including) the version indicated in this attribute.
3138 //
3139 // The system will automatically add a <uses-library> tag with this library to
3140 // apps that target any SDK less than the version indicated in this attribute.
3141 On_bootclasspath_before *string
3142
3143 // Indicates that PackageManager should ignore this shared library if the
3144 // platform is below the version indicated in this attribute.
3145 //
3146 // This means that the device won't recognise this library as installed.
3147 Min_device_sdk *string
3148
3149 // Indicates that PackageManager should ignore this shared library if the
3150 // platform is above the version indicated in this attribute.
3151 //
3152 // This means that the device won't recognise this library as installed.
3153 Max_device_sdk *string
Pedro Loureiroc3621422021-09-28 15:40:23 +00003154
3155 // The SdkLibrary's min api level as a string
3156 //
3157 // This value comes from the ApiLevel of the MinSdkVersion property.
3158 Sdk_library_min_api_level *string
Jamie Garsidee570ace2023-11-27 12:07:36 +00003159
3160 // Uses-libs dependencies that the shared library requires to work correctly.
3161 //
3162 // This will add dependency="foo:bar" to the <library> section.
3163 Uses_libs_dependencies []string
Jiyong Parke3833882020-02-17 17:28:10 +09003164}
3165
3166// java_sdk_library_xml builds the permission xml file for a java_sdk_library.
3167// Not to be used directly by users. java_sdk_library internally uses this.
3168func sdkLibraryXmlFactory() android.Module {
3169 module := &sdkLibraryXml{}
3170
3171 module.AddProperties(&module.properties)
3172
3173 android.InitApexModule(module)
3174 android.InitAndroidArchModule(module, android.DeviceSupported, android.MultilibCommon)
3175
3176 return module
3177}
3178
Colin Crossaede88c2020-08-11 12:17:01 -07003179func (module *sdkLibraryXml) UniqueApexVariations() bool {
3180 // sdkLibraryXml needs a unique variation per APEX because the generated XML file contains the path to the
3181 // mounted APEX, which contains the name of the APEX.
3182 return true
3183}
3184
Jiyong Parke3833882020-02-17 17:28:10 +09003185// from android.PrebuiltEtcModule
Jooyung Han0703fd82020-08-26 22:11:53 +09003186func (module *sdkLibraryXml) BaseDir() string {
3187 return "etc"
3188}
3189
3190// from android.PrebuiltEtcModule
Jiyong Parke3833882020-02-17 17:28:10 +09003191func (module *sdkLibraryXml) SubDir() string {
3192 return "permissions"
3193}
3194
ThiƩbaud Weksteen00e8b312024-03-18 14:06:00 +11003195var _ etc.PrebuiltEtcModule = (*sdkLibraryXml)(nil)
3196
Jiyong Parke3833882020-02-17 17:28:10 +09003197// from android.ApexModule
3198func (module *sdkLibraryXml) AvailableFor(what string) bool {
3199 return true
3200}
3201
3202func (module *sdkLibraryXml) DepsMutator(ctx android.BottomUpMutatorContext) {
3203 // do nothing
3204}
3205
Jiyong Park45bf82e2020-12-15 22:29:02 +09003206var _ android.ApexModule = (*sdkLibraryXml)(nil)
3207
3208// Implements android.ApexModule
Dan Albertc8060532020-07-22 22:32:17 -07003209func (module *sdkLibraryXml) ShouldSupportSdkVersion(ctx android.BaseModuleContext,
3210 sdkVersion android.ApiLevel) error {
Jooyung Han749dc692020-04-15 11:03:39 +09003211 // sdkLibraryXml doesn't need to be checked separately because java_sdk_library is checked
3212 return nil
3213}
3214
Jiyong Parke3833882020-02-17 17:28:10 +09003215// File path to the runtime implementation library
Colin Cross56a83212020-09-15 18:30:11 -07003216func (module *sdkLibraryXml) implPath(ctx android.ModuleContext) string {
Jiyong Parke3833882020-02-17 17:28:10 +09003217 implName := proptools.String(module.properties.Lib_name)
Colin Crossff694a82023-12-13 15:54:49 -08003218 if apexInfo, _ := android.ModuleProvider(ctx, android.ApexInfoProvider); !apexInfo.IsForPlatform() {
Colin Crosse07f2312020-08-13 11:24:56 -07003219 // TODO(b/146468504): ApexVariationName() is only a soong module name, not apex name.
Jiyong Parke3833882020-02-17 17:28:10 +09003220 // In most cases, this works fine. But when apex_name is set or override_apex is used
3221 // this can be wrong.
Spandan Das33bbeb22024-06-18 23:28:25 +00003222 return fmt.Sprintf("/apex/%s/javalib/%s.jar", apexInfo.BaseApexName, implName)
Jiyong Parke3833882020-02-17 17:28:10 +09003223 }
3224 partition := "system"
3225 if module.SocSpecific() {
3226 partition = "vendor"
3227 } else if module.DeviceSpecific() {
3228 partition = "odm"
3229 } else if module.ProductSpecific() {
3230 partition = "product"
3231 } else if module.SystemExtSpecific() {
3232 partition = "system_ext"
3233 }
3234 return "/" + partition + "/framework/" + implName + ".jar"
3235}
3236
Pedro Loureiro9956e5e2021-09-07 17:21:59 +00003237func formattedOptionalSdkLevelAttribute(ctx android.ModuleContext, attrName string, value *string) string {
3238 if value == nil {
3239 return ""
3240 }
3241 apiLevel, err := android.ApiLevelFromUser(ctx, *value)
3242 if err != nil {
Pedro Loureiroba6682f2021-10-29 09:32:32 +00003243 // attributes in bp files have underscores but in the xml have dashes.
3244 ctx.PropertyErrorf(strings.ReplaceAll(attrName, "-", "_"), err.Error())
Pedro Loureiro9956e5e2021-09-07 17:21:59 +00003245 return ""
3246 }
Pedro Loureirob638c622021-12-22 15:28:05 +00003247 if apiLevel.IsCurrent() {
3248 // passing "current" would always mean a future release, never the current (or the current in
3249 // progress) which means some conditions would never be triggered.
3250 ctx.PropertyErrorf(strings.ReplaceAll(attrName, "-", "_"),
3251 `"current" is not an allowed value for this attribute`)
3252 return ""
3253 }
Pedro Loureiro48991222022-06-17 20:01:21 +00003254 // "safeValue" is safe because it translates finalized codenames to a string
3255 // with their SDK int.
3256 safeValue := apiLevel.String()
3257 return formattedOptionalAttribute(attrName, &safeValue)
Pedro Loureiro9956e5e2021-09-07 17:21:59 +00003258}
3259
3260// formats an attribute for the xml permissions file if the value is not null
3261// returns empty string otherwise
3262func formattedOptionalAttribute(attrName string, value *string) string {
3263 if value == nil {
3264 return ""
3265 }
Paul Duffin1816cde2024-04-10 10:58:21 +01003266 return fmt.Sprintf(" %s=\"%s\"\n", attrName, *value)
Pedro Loureiro9956e5e2021-09-07 17:21:59 +00003267}
3268
Jamie Garsidee570ace2023-11-27 12:07:36 +00003269func formattedDependenciesAttribute(dependencies []string) string {
3270 if dependencies == nil {
3271 return ""
3272 }
Paul Duffin1816cde2024-04-10 10:58:21 +01003273 return fmt.Sprintf(" dependency=\"%s\"\n", strings.Join(dependencies, ":"))
Jamie Garsidee570ace2023-11-27 12:07:36 +00003274}
3275
Pedro Loureiro9956e5e2021-09-07 17:21:59 +00003276func (module *sdkLibraryXml) permissionsContents(ctx android.ModuleContext) string {
3277 libName := proptools.String(module.properties.Lib_name)
3278 libNameAttr := formattedOptionalAttribute("name", &libName)
3279 filePath := module.implPath(ctx)
3280 filePathAttr := formattedOptionalAttribute("file", &filePath)
Pedro Loureiroba6682f2021-10-29 09:32:32 +00003281 implicitFromAttr := formattedOptionalSdkLevelAttribute(ctx, "on-bootclasspath-since", module.properties.On_bootclasspath_since)
3282 implicitUntilAttr := formattedOptionalSdkLevelAttribute(ctx, "on-bootclasspath-before", module.properties.On_bootclasspath_before)
3283 minSdkAttr := formattedOptionalSdkLevelAttribute(ctx, "min-device-sdk", module.properties.Min_device_sdk)
3284 maxSdkAttr := formattedOptionalSdkLevelAttribute(ctx, "max-device-sdk", module.properties.Max_device_sdk)
Jamie Garsidee570ace2023-11-27 12:07:36 +00003285 dependenciesAttr := formattedDependenciesAttribute(module.properties.Uses_libs_dependencies)
Pedro Loureiro196d3e62021-12-22 19:53:01 +00003286 // <library> is understood in all android versions whereas <apex-library> is only understood from API T (and ignored before that).
3287 // 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 +00003288 var libraryTag string
3289 if module.properties.Min_device_sdk != nil {
Paul Duffin1816cde2024-04-10 10:58:21 +01003290 libraryTag = " <apex-library\n"
Pedro Loureiroc3621422021-09-28 15:40:23 +00003291 } else {
Paul Duffin1816cde2024-04-10 10:58:21 +01003292 libraryTag = " <library\n"
Pedro Loureiroc3621422021-09-28 15:40:23 +00003293 }
Pedro Loureiro9956e5e2021-09-07 17:21:59 +00003294
3295 return strings.Join([]string{
Paul Duffin1816cde2024-04-10 10:58:21 +01003296 "<?xml version=\"1.0\" encoding=\"utf-8\"?>\n",
3297 "<!-- Copyright (C) 2018 The Android Open Source Project\n",
3298 "\n",
3299 " Licensed under the Apache License, Version 2.0 (the \"License\");\n",
3300 " you may not use this file except in compliance with the License.\n",
3301 " You may obtain a copy of the License at\n",
3302 "\n",
3303 " http://www.apache.org/licenses/LICENSE-2.0\n",
3304 "\n",
3305 " Unless required by applicable law or agreed to in writing, software\n",
3306 " distributed under the License is distributed on an \"AS IS\" BASIS,\n",
3307 " WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\n",
3308 " See the License for the specific language governing permissions and\n",
3309 " limitations under the License.\n",
3310 "-->\n",
3311 "<permissions>\n",
Pedro Loureiroc3621422021-09-28 15:40:23 +00003312 libraryTag,
Pedro Loureiro9956e5e2021-09-07 17:21:59 +00003313 libNameAttr,
3314 filePathAttr,
3315 implicitFromAttr,
3316 implicitUntilAttr,
3317 minSdkAttr,
3318 maxSdkAttr,
Jamie Garsidee570ace2023-11-27 12:07:36 +00003319 dependenciesAttr,
Paul Duffin1816cde2024-04-10 10:58:21 +01003320 " />\n",
3321 "</permissions>\n",
3322 }, "")
Pedro Loureiro9956e5e2021-09-07 17:21:59 +00003323}
3324
Jiyong Parke3833882020-02-17 17:28:10 +09003325func (module *sdkLibraryXml) GenerateAndroidBuildActions(ctx android.ModuleContext) {
Colin Crossff694a82023-12-13 15:54:49 -08003326 apexInfo, _ := android.ModuleProvider(ctx, android.ApexInfoProvider)
3327 module.hideApexVariantFromMake = !apexInfo.IsForPlatform()
Colin Cross56a83212020-09-15 18:30:11 -07003328
Jiyong Parke3833882020-02-17 17:28:10 +09003329 libName := proptools.String(module.properties.Lib_name)
Pedro Loureiroc3621422021-09-28 15:40:23 +00003330 module.selfValidate(ctx)
Pedro Loureiro9956e5e2021-09-07 17:21:59 +00003331 xmlContent := module.permissionsContents(ctx)
Jiyong Parke3833882020-02-17 17:28:10 +09003332
3333 module.outputFilePath = android.PathForModuleOut(ctx, libName+".xml").OutputPath
Paul Duffin1816cde2024-04-10 10:58:21 +01003334 android.WriteFileRuleVerbatim(ctx, module.outputFilePath, xmlContent)
Jiyong Parke3833882020-02-17 17:28:10 +09003335
3336 module.installDirPath = android.PathForModuleInstall(ctx, "etc", module.SubDir())
Jeongik Cha00e09912024-04-23 05:07:13 +09003337 ctx.PackageFile(module.installDirPath, libName+".xml", module.outputFilePath)
mrziwange2346b82024-06-10 15:09:45 -07003338
3339 ctx.SetOutputFiles(android.OutputPaths{module.outputFilePath}.Paths(), "")
Jiyong Parke3833882020-02-17 17:28:10 +09003340}
3341
3342func (module *sdkLibraryXml) AndroidMkEntries() []android.AndroidMkEntries {
Colin Cross56a83212020-09-15 18:30:11 -07003343 if module.hideApexVariantFromMake {
satayev8f088b02021-12-06 11:40:46 +00003344 return []android.AndroidMkEntries{{
Jiyong Parke3833882020-02-17 17:28:10 +09003345 Disabled: true,
3346 }}
3347 }
3348
satayev8f088b02021-12-06 11:40:46 +00003349 return []android.AndroidMkEntries{{
Jiyong Parke3833882020-02-17 17:28:10 +09003350 Class: "ETC",
3351 OutputFile: android.OptionalPathForPath(module.outputFilePath),
3352 ExtraEntries: []android.AndroidMkExtraEntriesFunc{
Colin Crossaa255532020-07-03 13:18:24 -07003353 func(ctx android.AndroidMkExtraEntriesContext, entries *android.AndroidMkEntries) {
Jiyong Parke3833882020-02-17 17:28:10 +09003354 entries.SetString("LOCAL_MODULE_TAGS", "optional")
Colin Crossc68db4b2021-11-11 18:59:15 -08003355 entries.SetString("LOCAL_MODULE_PATH", module.installDirPath.String())
Jiyong Parke3833882020-02-17 17:28:10 +09003356 entries.SetString("LOCAL_INSTALLED_MODULE_STEM", module.outputFilePath.Base())
3357 },
3358 },
3359 }}
3360}
Paul Duffindd46f712020-02-10 13:37:10 +00003361
Pedro Loureiroc3621422021-09-28 15:40:23 +00003362func (module *sdkLibraryXml) selfValidate(ctx android.ModuleContext) {
3363 module.validateAtLeastTAttributes(ctx)
3364 module.validateMinAndMaxDeviceSdk(ctx)
3365 module.validateMinMaxDeviceSdkAndModuleMinSdk(ctx)
3366 module.validateOnBootclasspathBeforeRequirements(ctx)
3367}
3368
3369func (module *sdkLibraryXml) validateAtLeastTAttributes(ctx android.ModuleContext) {
3370 t := android.ApiLevelOrPanic(ctx, "Tiramisu")
3371 module.attrAtLeastT(ctx, t, module.properties.Min_device_sdk, "min_device_sdk")
3372 module.attrAtLeastT(ctx, t, module.properties.Max_device_sdk, "max_device_sdk")
3373 module.attrAtLeastT(ctx, t, module.properties.On_bootclasspath_before, "on_bootclasspath_before")
3374 module.attrAtLeastT(ctx, t, module.properties.On_bootclasspath_since, "on_bootclasspath_since")
3375}
3376
3377func (module *sdkLibraryXml) attrAtLeastT(ctx android.ModuleContext, t android.ApiLevel, attr *string, attrName string) {
3378 if attr != nil {
3379 if level, err := android.ApiLevelFromUser(ctx, *attr); err == nil {
3380 // we will inform the user of invalid inputs when we try to write the
3381 // permissions xml file so we don't need to do it here
3382 if t.GreaterThan(level) {
3383 ctx.PropertyErrorf(attrName, "Attribute value needs to be at least T")
3384 }
3385 }
3386 }
3387}
3388
3389func (module *sdkLibraryXml) validateMinAndMaxDeviceSdk(ctx android.ModuleContext) {
3390 if module.properties.Min_device_sdk != nil && module.properties.Max_device_sdk != nil {
3391 min, minErr := android.ApiLevelFromUser(ctx, *module.properties.Min_device_sdk)
3392 max, maxErr := android.ApiLevelFromUser(ctx, *module.properties.Max_device_sdk)
3393 if minErr == nil && maxErr == nil {
3394 // we will inform the user of invalid inputs when we try to write the
3395 // permissions xml file so we don't need to do it here
3396 if min.GreaterThan(max) {
3397 ctx.ModuleErrorf("min_device_sdk can't be greater than max_device_sdk")
3398 }
3399 }
3400 }
3401}
3402
3403func (module *sdkLibraryXml) validateMinMaxDeviceSdkAndModuleMinSdk(ctx android.ModuleContext) {
3404 moduleMinApi := android.ApiLevelOrPanic(ctx, *module.properties.Sdk_library_min_api_level)
3405 if module.properties.Min_device_sdk != nil {
3406 api, err := android.ApiLevelFromUser(ctx, *module.properties.Min_device_sdk)
3407 if err == nil {
3408 if moduleMinApi.GreaterThan(api) {
3409 ctx.PropertyErrorf("min_device_sdk", "Can't be less than module's min sdk (%s)", moduleMinApi)
3410 }
3411 }
3412 }
3413 if module.properties.Max_device_sdk != nil {
3414 api, err := android.ApiLevelFromUser(ctx, *module.properties.Max_device_sdk)
3415 if err == nil {
3416 if moduleMinApi.GreaterThan(api) {
3417 ctx.PropertyErrorf("max_device_sdk", "Can't be less than module's min sdk (%s)", moduleMinApi)
3418 }
3419 }
3420 }
3421}
3422
3423func (module *sdkLibraryXml) validateOnBootclasspathBeforeRequirements(ctx android.ModuleContext) {
3424 moduleMinApi := android.ApiLevelOrPanic(ctx, *module.properties.Sdk_library_min_api_level)
3425 if module.properties.On_bootclasspath_before != nil {
3426 t := android.ApiLevelOrPanic(ctx, "Tiramisu")
3427 // if we use the attribute, then we need to do this validation
3428 if moduleMinApi.LessThan(t) {
3429 // if minAPi is < T, then we need to have min_device_sdk (which only accepts T+)
3430 if module.properties.Min_device_sdk == nil {
3431 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")
3432 }
3433 }
3434 }
3435}
3436
Paul Duffindd46f712020-02-10 13:37:10 +00003437type sdkLibrarySdkMemberType struct {
3438 android.SdkMemberTypeBase
3439}
3440
Paul Duffin296701e2021-07-14 10:29:36 +01003441func (s *sdkLibrarySdkMemberType) AddDependencies(ctx android.SdkDependencyContext, dependencyTag blueprint.DependencyTag, names []string) {
3442 ctx.AddVariationDependencies(nil, dependencyTag, names...)
Paul Duffindd46f712020-02-10 13:37:10 +00003443}
3444
3445func (s *sdkLibrarySdkMemberType) IsInstance(module android.Module) bool {
3446 _, ok := module.(*SdkLibrary)
3447 return ok
3448}
3449
3450func (s *sdkLibrarySdkMemberType) AddPrebuiltModule(ctx android.SdkMemberContext, member android.SdkMember) android.BpModule {
3451 return ctx.SnapshotBuilder().AddPrebuiltModule(member, "java_sdk_library_import")
3452}
3453
3454func (s *sdkLibrarySdkMemberType) CreateVariantPropertiesStruct() android.SdkMemberProperties {
3455 return &sdkLibrarySdkMemberProperties{}
3456}
3457
Paul Duffin976b0e52021-04-27 23:20:26 +01003458var javaSdkLibrarySdkMemberType = &sdkLibrarySdkMemberType{
3459 android.SdkMemberTypeBase{
3460 PropertyName: "java_sdk_libs",
3461 SupportsSdk: true,
3462 },
3463}
3464
Paul Duffindd46f712020-02-10 13:37:10 +00003465type sdkLibrarySdkMemberProperties struct {
3466 android.SdkMemberPropertiesBase
3467
Paul Duffine8409952022-09-22 16:24:46 +01003468 // Stem name for files in the sdk snapshot.
3469 //
3470 // This is used to construct the path names of various sdk library files in the sdk snapshot to
3471 // make sure that they match the finalized versions of those files in prebuilts/sdk.
3472 //
3473 // This property is marked as keep so that it will be kept in all instances of this struct, will
3474 // not be cleared but will be copied to common structs. That is needed because this field is used
3475 // to construct many file names for other parts of this struct and so it needs to be present in
3476 // all structs. If it was not marked as keep then it would be cleared in some structs and so would
3477 // be unavailable for generating file names if there were other properties that were still set.
3478 Stem string `sdk:"keep"`
3479
Paul Duffindd46f712020-02-10 13:37:10 +00003480 // Scope to per scope properties.
Paul Duffin106a3a42022-01-27 16:39:06 +00003481 Scopes map[*apiScope]*scopeProperties
Paul Duffindd46f712020-02-10 13:37:10 +00003482
Paul Duffin3d1248c2020-04-09 00:10:17 +01003483 // The Java stubs source files.
3484 Stub_srcs []string
Paul Duffinf7a64332020-05-13 16:54:55 +01003485
3486 // The naming scheme.
3487 Naming_scheme *string
Paul Duffind7eb1c22020-05-26 20:57:10 +01003488
3489 // True if the java_sdk_library_import is for a shared library, false
3490 // otherwise.
3491 Shared_library *bool
Paul Duffina2ae7e02020-09-11 11:55:00 +01003492
Paul Duffin1267d872021-04-16 17:21:36 +01003493 // True if the stub imports should produce dex jars.
3494 Compile_dex *bool
3495
Paul Duffina2ae7e02020-09-11 11:55:00 +01003496 // The paths to the doctag files to add to the prebuilt.
3497 Doctag_paths android.Paths
Paul Duffin869de142021-07-15 14:14:41 +01003498
3499 Permitted_packages []string
Pedro Loureiro9956e5e2021-09-07 17:21:59 +00003500
3501 // Signals that this shared library is part of the bootclasspath starting
3502 // on the version indicated in this attribute.
3503 //
3504 // This will make platforms at this level and above to ignore
3505 // <uses-library> tags with this library name because the library is already
3506 // available
3507 On_bootclasspath_since *string
3508
3509 // Signals that this shared library was part of the bootclasspath before
3510 // (but not including) the version indicated in this attribute.
3511 //
3512 // The system will automatically add a <uses-library> tag with this library to
3513 // apps that target any SDK less than the version indicated in this attribute.
3514 On_bootclasspath_before *string
3515
3516 // Indicates that PackageManager should ignore this shared library if the
3517 // platform is below the version indicated in this attribute.
3518 //
3519 // This means that the device won't recognise this library as installed.
3520 Min_device_sdk *string
3521
3522 // Indicates that PackageManager should ignore this shared library if the
3523 // platform is above the version indicated in this attribute.
3524 //
3525 // This means that the device won't recognise this library as installed.
3526 Max_device_sdk *string
Jiakai Zhang9c4dc192023-02-09 00:09:24 +08003527
3528 DexPreoptProfileGuided *bool `supported_build_releases:"UpsideDownCake+"`
Paul Duffindd46f712020-02-10 13:37:10 +00003529}
3530
3531type scopeProperties struct {
Paul Duffin1fd005d2020-04-09 01:08:11 +01003532 Jars android.Paths
3533 StubsSrcJar android.Path
3534 CurrentApiFile android.Path
3535 RemovedApiFile android.Path
Paul Duffine7babdb2022-02-10 13:06:54 +00003536 AnnotationsZip android.Path `supported_build_releases:"Tiramisu+"`
Paul Duffin1fd005d2020-04-09 01:08:11 +01003537 SdkVersion string
Paul Duffindd46f712020-02-10 13:37:10 +00003538}
3539
3540func (s *sdkLibrarySdkMemberProperties) PopulateFromVariant(ctx android.SdkMemberContext, variant android.Module) {
3541 sdk := variant.(*SdkLibrary)
3542
Paul Duffine8409952022-09-22 16:24:46 +01003543 // Copy the stem name for files in the sdk snapshot.
3544 s.Stem = sdk.distStem()
3545
Paul Duffin106a3a42022-01-27 16:39:06 +00003546 s.Scopes = make(map[*apiScope]*scopeProperties)
Jihoon Kang98aa8fa2024-06-07 11:06:57 +00003547 for _, apiScope := range AllApiScopes {
Paul Duffin803a9562020-05-20 11:52:25 +01003548 paths := sdk.findScopePaths(apiScope)
3549 if paths == nil {
3550 continue
3551 }
3552
Paul Duffindd46f712020-02-10 13:37:10 +00003553 jars := paths.stubsImplPath
3554 if len(jars) > 0 {
3555 properties := scopeProperties{}
3556 properties.Jars = jars
Paul Duffin780c5f42020-05-12 15:52:55 +01003557 properties.SdkVersion = sdk.sdkVersionForStubsLibrary(ctx.SdkModuleContext(), apiScope)
Paul Duffin0f8faff2020-05-20 16:18:00 +01003558 properties.StubsSrcJar = paths.stubsSrcJar.Path()
Paul Duffin10269f12020-06-19 18:39:55 +01003559 if paths.currentApiFilePath.Valid() {
3560 properties.CurrentApiFile = paths.currentApiFilePath.Path()
3561 }
3562 if paths.removedApiFilePath.Valid() {
3563 properties.RemovedApiFile = paths.removedApiFilePath.Path()
3564 }
Anton Hanssond78eb762021-09-21 15:25:12 +01003565 // The annotations zip is only available for modules that set annotations_enabled: true.
3566 if paths.annotationsZip.Valid() {
3567 properties.AnnotationsZip = paths.annotationsZip.Path()
3568 }
Paul Duffin106a3a42022-01-27 16:39:06 +00003569 s.Scopes[apiScope] = &properties
Paul Duffindd46f712020-02-10 13:37:10 +00003570 }
3571 }
3572
Paul Duffindfa131e2020-05-15 20:37:11 +01003573 s.Naming_scheme = sdk.commonSdkLibraryProperties.Naming_scheme
Paul Duffind7eb1c22020-05-26 20:57:10 +01003574 s.Shared_library = proptools.BoolPtr(sdk.sharedLibrary())
Paul Duffin1267d872021-04-16 17:21:36 +01003575 s.Compile_dex = sdk.dexProperties.Compile_dex
Paul Duffina2ae7e02020-09-11 11:55:00 +01003576 s.Doctag_paths = sdk.doctagPaths
Paul Duffin869de142021-07-15 14:14:41 +01003577 s.Permitted_packages = sdk.PermittedPackagesForUpdatableBootJars()
Pedro Loureiro9956e5e2021-09-07 17:21:59 +00003578 s.On_bootclasspath_since = sdk.commonSdkLibraryProperties.On_bootclasspath_since
3579 s.On_bootclasspath_before = sdk.commonSdkLibraryProperties.On_bootclasspath_before
3580 s.Min_device_sdk = sdk.commonSdkLibraryProperties.Min_device_sdk
3581 s.Max_device_sdk = sdk.commonSdkLibraryProperties.Max_device_sdk
Jiakai Zhang9c4dc192023-02-09 00:09:24 +08003582
Jihoon Kanga3a05462024-04-05 00:36:44 +00003583 implLibrary := sdk.getImplLibraryModule()
3584 if implLibrary != nil && implLibrary.dexpreopter.dexpreoptProperties.Dex_preopt_result.Profile_guided {
Jiakai Zhang9c4dc192023-02-09 00:09:24 +08003585 s.DexPreoptProfileGuided = proptools.BoolPtr(true)
3586 }
Paul Duffindd46f712020-02-10 13:37:10 +00003587}
3588
3589func (s *sdkLibrarySdkMemberProperties) AddToPropertySet(ctx android.SdkMemberContext, propertySet android.BpPropertySet) {
Paul Duffinf7a64332020-05-13 16:54:55 +01003590 if s.Naming_scheme != nil {
3591 propertySet.AddProperty("naming_scheme", proptools.String(s.Naming_scheme))
3592 }
Paul Duffind7eb1c22020-05-26 20:57:10 +01003593 if s.Shared_library != nil {
3594 propertySet.AddProperty("shared_library", *s.Shared_library)
3595 }
Paul Duffin1267d872021-04-16 17:21:36 +01003596 if s.Compile_dex != nil {
3597 propertySet.AddProperty("compile_dex", *s.Compile_dex)
3598 }
Paul Duffin869de142021-07-15 14:14:41 +01003599 if len(s.Permitted_packages) > 0 {
3600 propertySet.AddProperty("permitted_packages", s.Permitted_packages)
3601 }
Jiakai Zhang9c4dc192023-02-09 00:09:24 +08003602 dexPreoptSet := propertySet.AddPropertySet("dex_preopt")
3603 if s.DexPreoptProfileGuided != nil {
3604 dexPreoptSet.AddProperty("profile_guided", proptools.Bool(s.DexPreoptProfileGuided))
3605 }
Paul Duffinf7a64332020-05-13 16:54:55 +01003606
Paul Duffine8409952022-09-22 16:24:46 +01003607 stem := s.Stem
3608
Jihoon Kang98aa8fa2024-06-07 11:06:57 +00003609 for _, apiScope := range AllApiScopes {
Paul Duffindd46f712020-02-10 13:37:10 +00003610 if properties, ok := s.Scopes[apiScope]; ok {
Paul Duffin6b836ba2020-05-13 19:19:49 +01003611 scopeSet := propertySet.AddPropertySet(apiScope.propertyName)
Paul Duffindd46f712020-02-10 13:37:10 +00003612
Paul Duffin958806b2022-05-16 13:10:47 +00003613 scopeDir := apiScope.snapshotRelativeDir()
Paul Duffin3d1248c2020-04-09 00:10:17 +01003614
Paul Duffindd46f712020-02-10 13:37:10 +00003615 var jars []string
3616 for _, p := range properties.Jars {
Paul Duffine8409952022-09-22 16:24:46 +01003617 dest := filepath.Join(scopeDir, stem+"-stubs.jar")
Paul Duffindd46f712020-02-10 13:37:10 +00003618 ctx.SnapshotBuilder().CopyToSnapshot(p, dest)
3619 jars = append(jars, dest)
3620 }
3621 scopeSet.AddProperty("jars", jars)
3622
Paul Duffin22628d52021-05-12 23:13:22 +01003623 if ctx.SdkModuleContext().Config().IsEnvTrue("SOONG_SDK_SNAPSHOT_USE_SRCJAR") {
3624 // Copy the stubs source jar into the snapshot zip as is.
Paul Duffine8409952022-09-22 16:24:46 +01003625 srcJarSnapshotPath := filepath.Join(scopeDir, stem+".srcjar")
Paul Duffin22628d52021-05-12 23:13:22 +01003626 ctx.SnapshotBuilder().CopyToSnapshot(properties.StubsSrcJar, srcJarSnapshotPath)
3627 scopeSet.AddProperty("stub_srcs", []string{srcJarSnapshotPath})
3628 } else {
3629 // Merge the stubs source jar into the snapshot zip so that when it is unpacked
3630 // the source files are also unpacked.
Paul Duffine8409952022-09-22 16:24:46 +01003631 snapshotRelativeDir := filepath.Join(scopeDir, stem+"_stub_sources")
Paul Duffin22628d52021-05-12 23:13:22 +01003632 ctx.SnapshotBuilder().UnzipToSnapshot(properties.StubsSrcJar, snapshotRelativeDir)
3633 scopeSet.AddProperty("stub_srcs", []string{snapshotRelativeDir})
3634 }
Paul Duffin3d1248c2020-04-09 00:10:17 +01003635
Paul Duffin1fd005d2020-04-09 01:08:11 +01003636 if properties.CurrentApiFile != nil {
Paul Duffine8409952022-09-22 16:24:46 +01003637 currentApiSnapshotPath := apiScope.snapshotRelativeCurrentApiTxtPath(stem)
Paul Duffin1fd005d2020-04-09 01:08:11 +01003638 ctx.SnapshotBuilder().CopyToSnapshot(properties.CurrentApiFile, currentApiSnapshotPath)
3639 scopeSet.AddProperty("current_api", currentApiSnapshotPath)
3640 }
3641
3642 if properties.RemovedApiFile != nil {
Paul Duffine8409952022-09-22 16:24:46 +01003643 removedApiSnapshotPath := apiScope.snapshotRelativeRemovedApiTxtPath(stem)
Paul Duffin3dbf9fd2020-06-02 13:00:02 +01003644 ctx.SnapshotBuilder().CopyToSnapshot(properties.RemovedApiFile, removedApiSnapshotPath)
Paul Duffin1fd005d2020-04-09 01:08:11 +01003645 scopeSet.AddProperty("removed_api", removedApiSnapshotPath)
3646 }
3647
Anton Hanssond78eb762021-09-21 15:25:12 +01003648 if properties.AnnotationsZip != nil {
Paul Duffine8409952022-09-22 16:24:46 +01003649 annotationsSnapshotPath := filepath.Join(scopeDir, stem+"_annotations.zip")
Anton Hanssond78eb762021-09-21 15:25:12 +01003650 ctx.SnapshotBuilder().CopyToSnapshot(properties.AnnotationsZip, annotationsSnapshotPath)
3651 scopeSet.AddProperty("annotations", annotationsSnapshotPath)
3652 }
3653
Paul Duffindd46f712020-02-10 13:37:10 +00003654 if properties.SdkVersion != "" {
3655 scopeSet.AddProperty("sdk_version", properties.SdkVersion)
3656 }
3657 }
3658 }
3659
Paul Duffina2ae7e02020-09-11 11:55:00 +01003660 if len(s.Doctag_paths) > 0 {
3661 dests := []string{}
3662 for _, p := range s.Doctag_paths {
3663 dest := filepath.Join("doctags", p.Rel())
3664 ctx.SnapshotBuilder().CopyToSnapshot(p, dest)
3665 dests = append(dests, dest)
3666 }
3667 propertySet.AddProperty("doctag_files", dests)
3668 }
Paul Duffindd46f712020-02-10 13:37:10 +00003669}