blob: 1eb7ab8348f1cf335ec5b8fad1586594fe6ed902 [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
Paul Duffin749f98f2019-12-30 17:23:46 +0000541 // the relative path to the directory containing the api specification files.
542 // Defaults to "api".
543 Api_dir *string
544
Paul Duffindfa131e2020-05-15 20:37:11 +0100545 // Determines whether a runtime implementation library is built; defaults to false.
546 //
547 // If true then it also prevents the module from being used as a shared module, i.e.
MƄrten Kongstad81d90952022-05-25 16:27:11 +0200548 // it is as if shared_library: false, was set.
Paul Duffin43db9be2019-12-30 17:35:49 +0000549 Api_only *bool
550
Paul Duffin11512472019-02-11 15:55:17 +0000551 // local files that are used within user customized droiddoc options.
552 Droiddoc_option_files []string
553
Spandan Das93e95992021-07-29 18:26:39 +0000554 // additional droiddoc options.
Paul Duffin11512472019-02-11 15:55:17 +0000555 // Available variables for substitution:
556 //
557 // $(location <label>): the path to the droiddoc_option_files with name <label>
Sundong Ahndd567f92018-07-31 17:19:11 +0900558 Droiddoc_options []string
559
Paul Duffine22c2ab2020-05-20 19:35:27 +0100560 // is set to true, Metalava will allow framework SDK to contain annotations.
561 Annotations_enabled *bool
562
Sundong Ahn054b19a2018-10-19 13:46:09 +0900563 // a list of top-level directories containing files to merge qualifier annotations
564 // (i.e. those intended to be included in the stubs written) from.
565 Merge_annotations_dirs []string
566
567 // a list of top-level directories containing Java stub files to merge show/hide annotations from.
568 Merge_inclusion_annotations_dirs []string
569
Paul Duffin4f5c1ef2020-11-19 14:53:43 +0000570 // If set to true then don't create dist rules.
571 No_dist *bool
Sundong Ahn80a87b32019-05-13 15:02:50 +0900572
Paul Duffin31310252020-11-20 21:26:20 +0000573 // The stem for the artifacts that are copied to the dist, if not specified
574 // then defaults to the base module name.
575 //
576 // For each scope the following artifacts are copied to the apistubs/<scope>
577 // directory in the dist.
578 // * stubs impl jar -> <dist-stem>.jar
579 // * API specification file -> api/<dist-stem>.txt
580 // * Removed API specification file -> api/<dist-stem>-removed.txt
581 //
582 // Also used to construct the name of the filegroup (created by prebuilt_apis)
583 // that references the latest released API and remove API specification files.
584 // * API specification filegroup -> <dist-stem>.api.<scope>.latest
585 // * Removed API specification filegroup -> <dist-stem>-removed.api.<scope>.latest
Jaewoong Jung1a97ee02021-03-09 13:25:02 -0800586 // * API incompatibilities baseline filegroup -> <dist-stem>-incompatibilities.api.<scope>.latest
Paul Duffin31310252020-11-20 21:26:20 +0000587 Dist_stem *string
588
Colin Cross986b69a2021-06-01 13:13:40 -0700589 // The subdirectory for the artifacts that are copied to the dist directory. If not specified
Colin Cross3dd66252021-06-01 14:05:09 -0700590 // then defaults to "unknown". Should be set to "android" for anything that should be published
Colin Cross986b69a2021-06-01 13:13:40 -0700591 // in the public Android SDK.
592 Dist_group *string
593
Anton Hanssondff2c782020-12-21 17:10:01 +0000594 // A compatibility mode that allows historical API-tracking files to not exist.
595 // Do not use.
596 Unsafe_ignore_missing_latest_api bool
597
Paul Duffin3375e352020-04-28 10:44:03 +0100598 // indicates whether system and test apis should be generated.
599 Generate_system_and_test_apis bool `blueprint:"mutated"`
600
601 // The properties specific to the public api scope
602 //
603 // Unless explicitly specified by using public.enabled the public api scope is
604 // enabled by default in both legacy and non-legacy mode.
605 Public ApiScopeProperties
606
607 // The properties specific to the system api scope
608 //
609 // In legacy mode the system api scope is enabled by default when sdk_version
610 // is set to something other than "none".
611 //
612 // In non-legacy mode the system api scope is disabled by default.
613 System ApiScopeProperties
614
615 // The properties specific to the test api scope
616 //
617 // In legacy mode the test api scope is enabled by default when sdk_version
618 // is set to something other than "none".
619 //
620 // In non-legacy mode the test api scope is disabled by default.
621 Test ApiScopeProperties
Paul Duffin37e0b772019-12-30 17:20:10 +0000622
Paul Duffin0c5bae52020-06-02 13:00:08 +0100623 // The properties specific to the module-lib api scope
Paul Duffin8f265b92020-04-28 14:13:56 +0100624 //
Zi Wangb2179e32023-01-31 15:53:30 -0800625 // Unless explicitly specified by using module_lib.enabled the module_lib api
626 // scope is disabled by default.
Paul Duffin8f265b92020-04-28 14:13:56 +0100627 Module_lib ApiScopeProperties
628
Paul Duffin0c5bae52020-06-02 13:00:08 +0100629 // The properties specific to the system-server api scope
630 //
Zi Wangb2179e32023-01-31 15:53:30 -0800631 // Unless explicitly specified by using system_server.enabled the
632 // system_server api scope is disabled by default.
Paul Duffin0c5bae52020-06-02 13:00:08 +0100633 System_server ApiScopeProperties
634
Jiyong Park932cdfe2020-05-28 00:19:53 +0900635 // Determines if the stubs are preferred over the implementation library
636 // for linking, even when the client doesn't specify sdk_version. When this
637 // is set to true, such clients are provided with the widest API surface that
638 // this lib provides. Note however that this option doesn't affect the clients
639 // that are in the same APEX as this library. In that case, the clients are
640 // always linked with the implementation library. Default is false.
641 Default_to_stubs *bool
642
Paul Duffin160fe412020-05-10 19:32:20 +0100643 // Properties related to api linting.
644 Api_lint struct {
645 // Enable api linting.
646 Enabled *bool
Anton Hanssonfd1c0d22023-11-02 15:18:09 +0000647
648 // If API lint is enabled, this flag controls whether a set of legitimate lint errors
649 // are turned off. The default is true.
650 Legacy_errors_allowed *bool
Paul Duffin160fe412020-05-10 19:32:20 +0100651 }
652
Jihoon Kang80456fd2023-11-15 19:22:14 +0000653 // Determines if the module contributes to any api surfaces.
654 // This property should be set to true only if the module is listed under
655 // frameworks-base-api.bootclasspath in frameworks/base/api/Android.bp.
656 // Otherwise, this property should be set to false.
657 // Defaults to false.
658 Contribute_to_android_api *bool
659
Jihoon Kang6592e872023-12-19 01:13:16 +0000660 // a list of aconfig_declarations module names that the stubs generated in this module
661 // depend on.
662 Aconfig_declarations []string
663
Jiyong Parkc678ad32018-04-10 13:07:10 +0900664 // TODO: determines whether to create HTML doc or not
Paul Duffine8409952022-09-22 16:24:46 +0100665 // Html_doc *bool
Jiyong Parkc678ad32018-04-10 13:07:10 +0900666}
667
Paul Duffin0f8faff2020-05-20 16:18:00 +0100668// Paths to outputs from java_sdk_library and java_sdk_library_import.
669//
670// Fields that are android.Paths are always set (during GenerateAndroidBuildActions).
671// OptionalPaths are always set by java_sdk_library but may not be set by
672// java_sdk_library_import as not all instances provide that information.
Paul Duffind1b3a922020-01-22 11:57:20 +0000673type scopePaths struct {
Paul Duffin0f8faff2020-05-20 16:18:00 +0100674 // The path (represented as Paths for convenience when returning) to the stubs header jar.
675 //
676 // That is the jar that is created by turbine.
677 stubsHeaderPath android.Paths
678
679 // The path (represented as Paths for convenience when returning) to the stubs implementation jar.
680 //
681 // This is not the implementation jar, it still only contains stubs.
682 stubsImplPath android.Paths
683
Paul Duffin1267d872021-04-16 17:21:36 +0100684 // The dex jar for the stubs.
685 //
686 // This is not the implementation jar, it still only contains stubs.
Martin Stjernholm8be1e6d2021-09-15 03:34:04 +0100687 stubsDexJarPath OptionalDexJarPath
Paul Duffin1267d872021-04-16 17:21:36 +0100688
Jihoon Kangbd093452023-12-26 19:08:01 +0000689 // The exportable dex jar for the stubs.
690 // This is not the implementation jar, it still only contains stubs.
691 // Includes unflagged apis and flagged apis enabled by release configurations.
692 exportableStubsDexJarPath OptionalDexJarPath
693
Paul Duffin0f8faff2020-05-20 16:18:00 +0100694 // The API specification file, e.g. system_current.txt.
695 currentApiFilePath android.OptionalPath
696
697 // The specification of API elements removed since the last release.
698 removedApiFilePath android.OptionalPath
699
700 // The stubs source jar.
701 stubsSrcJar android.OptionalPath
Anton Hanssond78eb762021-09-21 15:25:12 +0100702
703 // Extracted annotations.
704 annotationsZip android.OptionalPath
Paul Duffin958806b2022-05-16 13:10:47 +0000705
706 // The path to the latest API file.
Jihoon Kang5623e542024-01-31 23:27:26 +0000707 latestApiPaths android.Paths
Paul Duffin958806b2022-05-16 13:10:47 +0000708
709 // The path to the latest removed API file.
Jihoon Kang5623e542024-01-31 23:27:26 +0000710 latestRemovedApiPaths android.Paths
Paul Duffind1b3a922020-01-22 11:57:20 +0000711}
712
Colin Crossdcf71b22021-02-01 13:59:03 -0800713func (paths *scopePaths) extractStubsLibraryInfoFromDependency(ctx android.ModuleContext, dep android.Module) error {
Colin Cross313aa542023-12-13 13:47:44 -0800714 if lib, ok := android.OtherModuleProvider(ctx, dep, JavaInfoProvider); ok {
Colin Crossdcf71b22021-02-01 13:59:03 -0800715 paths.stubsHeaderPath = lib.HeaderJars
716 paths.stubsImplPath = lib.ImplementationJars
Paul Duffin1267d872021-04-16 17:21:36 +0100717
718 libDep := dep.(UsesLibraryDependency)
Spandan Das59a4a2b2024-01-09 21:35:56 +0000719 paths.stubsDexJarPath = libDep.DexJarBuildPath(ctx)
Jihoon Kangbd093452023-12-26 19:08:01 +0000720 paths.exportableStubsDexJarPath = libDep.DexJarBuildPath(ctx)
721 return nil
722 } else {
723 return fmt.Errorf("expected module that has JavaInfoProvider, e.g. java_library")
724 }
725}
726
727func (paths *scopePaths) extractEverythingStubsLibraryInfoFromDependency(ctx android.ModuleContext, dep android.Module) error {
728 if lib, ok := android.OtherModuleProvider(ctx, dep, JavaInfoProvider); ok {
729 paths.stubsHeaderPath = lib.HeaderJars
Jihoon Kangf55a5f72024-01-08 08:56:20 +0000730 if !ctx.Config().ReleaseHiddenApiExportableStubs() {
731 paths.stubsImplPath = lib.ImplementationJars
732 }
Jihoon Kangbd093452023-12-26 19:08:01 +0000733
734 libDep := dep.(UsesLibraryDependency)
735 paths.stubsDexJarPath = libDep.DexJarBuildPath(ctx)
736 return nil
737 } else {
738 return fmt.Errorf("expected module that has JavaInfoProvider, e.g. java_library")
739 }
740}
741
742func (paths *scopePaths) extractExportableStubsLibraryInfoFromDependency(ctx android.ModuleContext, dep android.Module) error {
Jihoon Kangf55a5f72024-01-08 08:56:20 +0000743 if lib, ok := android.OtherModuleProvider(ctx, dep, JavaInfoProvider); ok {
744 if ctx.Config().ReleaseHiddenApiExportableStubs() {
745 paths.stubsImplPath = lib.ImplementationJars
746 }
747
Jihoon Kangbd093452023-12-26 19:08:01 +0000748 libDep := dep.(UsesLibraryDependency)
749 paths.exportableStubsDexJarPath = libDep.DexJarBuildPath(ctx)
Paul Duffinc8782502020-04-29 20:45:27 +0100750 return nil
751 } else {
Colin Crossdcf71b22021-02-01 13:59:03 -0800752 return fmt.Errorf("expected module that has JavaInfoProvider, e.g. java_library")
Paul Duffinc8782502020-04-29 20:45:27 +0100753 }
754}
755
Jihoon Kangee113282024-01-23 00:16:41 +0000756func (paths *scopePaths) treatDepAsApiStubsProvider(dep android.Module, action func(provider ApiStubsProvider) error) error {
Paul Duffin0ff08bd2020-04-29 13:30:54 +0100757 if apiStubsProvider, ok := dep.(ApiStubsProvider); ok {
Jihoon Kangee113282024-01-23 00:16:41 +0000758 err := action(apiStubsProvider)
759 if err != nil {
760 return err
761 }
Jihoon Kangf55a5f72024-01-08 08:56:20 +0000762 return nil
763 } else {
764 return fmt.Errorf("expected module that implements ExportableApiStubsSrcProvider, e.g. droidstubs")
765 }
766}
767
Jihoon Kangee113282024-01-23 00:16:41 +0000768func (paths *scopePaths) treatDepAsApiStubsSrcProvider(dep android.Module, action func(provider ApiStubsSrcProvider) error) error {
Paul Duffin0f8faff2020-05-20 16:18:00 +0100769 if apiStubsProvider, ok := dep.(ApiStubsSrcProvider); ok {
Jihoon Kangee113282024-01-23 00:16:41 +0000770 err := action(apiStubsProvider)
771 if err != nil {
772 return err
773 }
Paul Duffin0f8faff2020-05-20 16:18:00 +0100774 return nil
775 } else {
776 return fmt.Errorf("expected module that implements ApiStubsSrcProvider, e.g. droidstubs")
777 }
778}
779
Jihoon Kangee113282024-01-23 00:16:41 +0000780func (paths *scopePaths) extractApiInfoFromApiStubsProvider(provider ApiStubsProvider, stubsType StubsType) error {
781 var annotationsZip, currentApiFilePath, removedApiFilePath android.Path
782 annotationsZip, annotationsZipErr := provider.AnnotationsZip(stubsType)
783 currentApiFilePath, currentApiFilePathErr := provider.ApiFilePath(stubsType)
784 removedApiFilePath, removedApiFilePathErr := provider.RemovedApiFilePath(stubsType)
Paul Duffin0ff08bd2020-04-29 13:30:54 +0100785
Jihoon Kangee113282024-01-23 00:16:41 +0000786 combinedError := errors.Join(annotationsZipErr, currentApiFilePathErr, removedApiFilePathErr)
787
788 if combinedError == nil {
789 paths.annotationsZip = android.OptionalPathForPath(annotationsZip)
790 paths.currentApiFilePath = android.OptionalPathForPath(currentApiFilePath)
791 paths.removedApiFilePath = android.OptionalPathForPath(removedApiFilePath)
792 }
793 return combinedError
Jihoon Kangf55a5f72024-01-08 08:56:20 +0000794}
795
Jihoon Kangee113282024-01-23 00:16:41 +0000796func (paths *scopePaths) extractStubsSourceInfoFromApiStubsProviders(provider ApiStubsSrcProvider, stubsType StubsType) error {
797 stubsSrcJar, err := provider.StubsSrcJar(stubsType)
798 if err == nil {
799 paths.stubsSrcJar = android.OptionalPathForPath(stubsSrcJar)
800 }
801 return err
Jihoon Kangf55a5f72024-01-08 08:56:20 +0000802}
803
Colin Crossdcf71b22021-02-01 13:59:03 -0800804func (paths *scopePaths) extractStubsSourceInfoFromDep(ctx android.ModuleContext, dep android.Module) error {
Jihoon Kang2a26b132024-06-24 07:39:40 +0000805 stubsType := Everything
806 if ctx.Config().ReleaseHiddenApiExportableStubs() {
807 stubsType = Exportable
808 }
Jihoon Kangee113282024-01-23 00:16:41 +0000809 return paths.treatDepAsApiStubsSrcProvider(dep, func(provider ApiStubsSrcProvider) error {
Jihoon Kang2a26b132024-06-24 07:39:40 +0000810 return paths.extractStubsSourceInfoFromApiStubsProviders(provider, stubsType)
Paul Duffin0ff08bd2020-04-29 13:30:54 +0100811 })
812}
813
Colin Crossdcf71b22021-02-01 13:59:03 -0800814func (paths *scopePaths) extractStubsSourceAndApiInfoFromApiStubsProvider(ctx android.ModuleContext, dep android.Module) error {
Jihoon Kang2a26b132024-06-24 07:39:40 +0000815 stubsType := Everything
Jihoon Kangf55a5f72024-01-08 08:56:20 +0000816 if ctx.Config().ReleaseHiddenApiExportableStubs() {
Jihoon Kang2a26b132024-06-24 07:39:40 +0000817 stubsType = Exportable
Jihoon Kangf55a5f72024-01-08 08:56:20 +0000818 }
Jihoon Kangee113282024-01-23 00:16:41 +0000819 return paths.treatDepAsApiStubsProvider(dep, func(provider ApiStubsProvider) error {
Jihoon Kang2a26b132024-06-24 07:39:40 +0000820 extractApiInfoErr := paths.extractApiInfoFromApiStubsProvider(provider, stubsType)
821 extractStubsSourceInfoErr := paths.extractStubsSourceInfoFromApiStubsProviders(provider, stubsType)
Jihoon Kangee113282024-01-23 00:16:41 +0000822 return errors.Join(extractApiInfoErr, extractStubsSourceInfoErr)
Paul Duffin0ff08bd2020-04-29 13:30:54 +0100823 })
824}
825
Jihoon Kang5623e542024-01-31 23:27:26 +0000826func extractOutputPaths(dep android.Module) (android.Paths, error) {
Paul Duffin958806b2022-05-16 13:10:47 +0000827 var paths android.Paths
828 if sourceFileProducer, ok := dep.(android.SourceFileProducer); ok {
829 paths = sourceFileProducer.Srcs()
Jihoon Kang5623e542024-01-31 23:27:26 +0000830 return paths, nil
Paul Duffin958806b2022-05-16 13:10:47 +0000831 } else {
Jihoon Kang5623e542024-01-31 23:27:26 +0000832 return nil, fmt.Errorf("module %q does not produce source files", dep)
Paul Duffin958806b2022-05-16 13:10:47 +0000833 }
Paul Duffin958806b2022-05-16 13:10:47 +0000834}
835
836func (paths *scopePaths) extractLatestApiPath(ctx android.ModuleContext, dep android.Module) error {
Jihoon Kang5623e542024-01-31 23:27:26 +0000837 outputPaths, err := extractOutputPaths(dep)
838 paths.latestApiPaths = outputPaths
Paul Duffin958806b2022-05-16 13:10:47 +0000839 return err
840}
841
842func (paths *scopePaths) extractLatestRemovedApiPath(ctx android.ModuleContext, dep android.Module) error {
Jihoon Kang5623e542024-01-31 23:27:26 +0000843 outputPaths, err := extractOutputPaths(dep)
844 paths.latestRemovedApiPaths = outputPaths
Paul Duffin958806b2022-05-16 13:10:47 +0000845 return err
846}
847
Paul Duffin0ff08bd2020-04-29 13:30:54 +0100848type commonToSdkLibraryAndImportProperties struct {
Paul Duffin1b1e8062020-05-08 13:44:43 +0100849 // The naming scheme to use for the components that this module creates.
850 //
Paul Duffinee9ad5d2020-09-11 13:04:05 +0100851 // If not specified then it defaults to "default".
Paul Duffin1b1e8062020-05-08 13:44:43 +0100852 //
853 // This is a temporary mechanism to simplify conversion from separate modules for each
854 // component that follow a different naming pattern to the default one.
855 //
856 // TODO(b/155480189) - Remove once naming inconsistencies have been resolved.
Paul Duffin0ff08bd2020-04-29 13:30:54 +0100857 Naming_scheme *string
Paul Duffindfa131e2020-05-15 20:37:11 +0100858
859 // Specifies whether this module can be used as an Android shared library; defaults
860 // to true.
861 //
862 // An Android shared library is one that can be referenced in a <uses-library> element
863 // in an AndroidManifest.xml.
864 Shared_library *bool
Paul Duffina2ae7e02020-09-11 11:55:00 +0100865
866 // Files containing information about supported java doc tags.
867 Doctag_files []string `android:"path"`
Pedro Loureiro9956e5e2021-09-07 17:21:59 +0000868
869 // Signals that this shared library is part of the bootclasspath starting
870 // on the version indicated in this attribute.
871 //
872 // This will make platforms at this level and above to ignore
873 // <uses-library> tags with this library name because the library is already
874 // available
875 On_bootclasspath_since *string
876
877 // Signals that this shared library was part of the bootclasspath before
878 // (but not including) the version indicated in this attribute.
879 //
880 // The system will automatically add a <uses-library> tag with this library to
881 // apps that target any SDK less than the version indicated in this attribute.
882 On_bootclasspath_before *string
883
884 // Indicates that PackageManager should ignore this shared library if the
885 // platform is below the version indicated in this attribute.
886 //
887 // This means that the device won't recognise this library as installed.
888 Min_device_sdk *string
889
890 // Indicates that PackageManager should ignore this shared library if the
891 // platform is above the version indicated in this attribute.
892 //
893 // This means that the device won't recognise this library as installed.
894 Max_device_sdk *string
Paul Duffin0ff08bd2020-04-29 13:30:54 +0100895}
896
Paul Duffin71b33cc2021-06-23 11:39:47 +0100897// commonSdkLibraryAndImportModule defines the interface that must be provided by a module that
898// embeds the commonToSdkLibraryAndImport struct.
899type commonSdkLibraryAndImportModule interface {
Paul Duffind796f6f2022-11-23 23:06:05 +0000900 android.Module
Paul Duffin71b33cc2021-06-23 11:39:47 +0100901
Spandan Das23956d12024-01-19 00:22:22 +0000902 // Returns the name of the root java_sdk_library that creates the child stub libraries
903 // This is the `name` as it appears in Android.bp, and not the name in Soong's build graph
904 // (with the prebuilt_ prefix)
905 //
906 // e.g. in the following java_sdk_library_import
907 // java_sdk_library_import {
908 // name: "framework-foo.v1",
909 // source_module_name: "framework-foo",
910 // }
911 // the values returned by
912 // 1. Name(): prebuilt_framework-foo.v1 # unique
913 // 2. BaseModuleName(): framework-foo # the source
914 // 3. RootLibraryName: framework-foo.v1 # the undecordated `name` from Android.bp
915 RootLibraryName() string
916}
917
918func (m *SdkLibrary) RootLibraryName() string {
919 return m.BaseModuleName()
920}
921
922func (m *SdkLibraryImport) RootLibraryName() string {
923 // m.BaseModuleName refers to the source of the import
924 // use moduleBase.Name to get the name of the module as it appears in the .bp file
925 return m.ModuleBase.Name()
Paul Duffin71b33cc2021-06-23 11:39:47 +0100926}
927
Paul Duffin56d44902020-01-31 13:36:25 +0000928// Common code between sdk library and sdk library import
929type commonToSdkLibraryAndImport struct {
Paul Duffin71b33cc2021-06-23 11:39:47 +0100930 module commonSdkLibraryAndImportModule
Paul Duffinc3091c82020-05-08 14:16:20 +0100931
Paul Duffin56d44902020-01-31 13:36:25 +0000932 scopePaths map[*apiScope]*scopePaths
Paul Duffin1b1e8062020-05-08 13:44:43 +0100933
934 namingScheme sdkLibraryComponentNamingScheme
935
Paul Duffindfa131e2020-05-15 20:37:11 +0100936 commonSdkLibraryProperties commonToSdkLibraryAndImportProperties
Paul Duffin859fe962020-05-15 10:20:31 +0100937
Paul Duffina2ae7e02020-09-11 11:55:00 +0100938 // Paths to commonSdkLibraryProperties.Doctag_files
939 doctagPaths android.Paths
940
Paul Duffin859fe962020-05-15 10:20:31 +0100941 // Functionality related to this being used as a component of a java_sdk_library.
942 EmbeddableSdkLibraryComponent
Jihoon Kang8479dea2024-04-04 01:19:05 +0000943
944 // Path to the header jars of the implementation library
945 // This is non-empty only when api_only is false.
946 implLibraryHeaderJars android.Paths
Jihoon Kanga3a05462024-04-05 00:36:44 +0000947
948 // The reference to the implementation library created by the source module.
949 // Is nil if the source module does not exist.
950 implLibraryModule *Library
Paul Duffin56d44902020-01-31 13:36:25 +0000951}
952
Paul Duffin71b33cc2021-06-23 11:39:47 +0100953func (c *commonToSdkLibraryAndImport) initCommon(module commonSdkLibraryAndImportModule) {
954 c.module = module
Paul Duffin1b1e8062020-05-08 13:44:43 +0100955
Paul Duffin71b33cc2021-06-23 11:39:47 +0100956 module.AddProperties(&c.commonSdkLibraryProperties)
Paul Duffin859fe962020-05-15 10:20:31 +0100957
958 // Initialize this as an sdk library component.
Paul Duffin71b33cc2021-06-23 11:39:47 +0100959 c.initSdkLibraryComponent(module)
Paul Duffin1b1e8062020-05-08 13:44:43 +0100960}
961
962func (c *commonToSdkLibraryAndImport) initCommonAfterDefaultsApplied(ctx android.DefaultableHookContext) bool {
Paul Duffindfa131e2020-05-15 20:37:11 +0100963 schemeProperty := proptools.StringDefault(c.commonSdkLibraryProperties.Naming_scheme, "default")
Paul Duffin1b1e8062020-05-08 13:44:43 +0100964 switch schemeProperty {
965 case "default":
966 c.namingScheme = &defaultNamingScheme{}
967 default:
968 ctx.PropertyErrorf("naming_scheme", "expected 'default' but was %q", schemeProperty)
969 return false
970 }
971
Spandan Das23956d12024-01-19 00:22:22 +0000972 namePtr := proptools.StringPtr(c.module.RootLibraryName())
Paul Duffin3f0290e2021-06-30 18:25:36 +0100973 c.sdkLibraryComponentProperties.SdkLibraryName = namePtr
974
Paul Duffindfa131e2020-05-15 20:37:11 +0100975 // Only track this sdk library if this can be used as a shared library.
976 if c.sharedLibrary() {
977 // Use the name specified in the module definition as the owner.
Paul Duffin3f0290e2021-06-30 18:25:36 +0100978 c.sdkLibraryComponentProperties.SdkLibraryToImplicitlyTrack = namePtr
Paul Duffindfa131e2020-05-15 20:37:11 +0100979 }
Paul Duffin859fe962020-05-15 10:20:31 +0100980
Paul Duffin1b1e8062020-05-08 13:44:43 +0100981 return true
Paul Duffinc3091c82020-05-08 14:16:20 +0100982}
983
Paul Duffinea8f8082021-06-24 13:25:57 +0100984// uniqueApexVariations provides common implementation of the ApexModule.UniqueApexVariations
985// method.
986func (c *commonToSdkLibraryAndImport) uniqueApexVariations() bool {
987 // A java_sdk_library that is a shared library produces an XML file that makes the shared library
988 // usable from an AndroidManifest.xml's <uses-library> entry. That XML file contains the name of
989 // the APEX and so it needs a unique variation per APEX.
990 return c.sharedLibrary()
991}
992
Paul Duffina2ae7e02020-09-11 11:55:00 +0100993func (c *commonToSdkLibraryAndImport) generateCommonBuildActions(ctx android.ModuleContext) {
994 c.doctagPaths = android.PathsForModuleSrc(ctx, c.commonSdkLibraryProperties.Doctag_files)
995}
996
Jihoon Kanga3a05462024-04-05 00:36:44 +0000997func (c *commonToSdkLibraryAndImport) getImplLibraryModule() *Library {
998 return c.implLibraryModule
999}
1000
Paul Duffineedc5d52020-06-12 17:46:39 +01001001// Module name of the runtime implementation library
1002func (c *commonToSdkLibraryAndImport) implLibraryModuleName() string {
Spandan Das23956d12024-01-19 00:22:22 +00001003 return c.module.RootLibraryName() + ".impl"
Paul Duffineedc5d52020-06-12 17:46:39 +01001004}
1005
1006// Module name of the XML file for the lib
1007func (c *commonToSdkLibraryAndImport) xmlPermissionsModuleName() string {
Spandan Das23956d12024-01-19 00:22:22 +00001008 return c.module.RootLibraryName() + sdkXmlFileSuffix
Paul Duffineedc5d52020-06-12 17:46:39 +01001009}
1010
Paul Duffinc3091c82020-05-08 14:16:20 +01001011// Name of the java_library module that compiles the stubs source.
1012func (c *commonToSdkLibraryAndImport) stubsLibraryModuleName(apiScope *apiScope) string {
Spandan Das23956d12024-01-19 00:22:22 +00001013 baseName := c.module.RootLibraryName()
Paul Duffin21787622022-11-25 12:48:20 +00001014 return c.namingScheme.stubsLibraryModuleName(apiScope, baseName)
Paul Duffinc3091c82020-05-08 14:16:20 +01001015}
1016
Jihoon Kangfa4a90d2023-12-20 02:53:38 +00001017// Name of the java_library module that compiles the exportable stubs source.
1018func (c *commonToSdkLibraryAndImport) exportableStubsLibraryModuleName(apiScope *apiScope) string {
Spandan Das23956d12024-01-19 00:22:22 +00001019 baseName := c.module.RootLibraryName()
Jihoon Kangfa4a90d2023-12-20 02:53:38 +00001020 return c.namingScheme.exportableStubsLibraryModuleName(apiScope, baseName)
1021}
1022
Paul Duffinc3091c82020-05-08 14:16:20 +01001023// Name of the droidstubs module that generates the stubs source and may also
1024// generate/check the API.
1025func (c *commonToSdkLibraryAndImport) stubsSourceModuleName(apiScope *apiScope) string {
Spandan Das23956d12024-01-19 00:22:22 +00001026 baseName := c.module.RootLibraryName()
Paul Duffin21787622022-11-25 12:48:20 +00001027 return c.namingScheme.stubsSourceModuleName(apiScope, baseName)
Paul Duffinc3091c82020-05-08 14:16:20 +01001028}
1029
Jihoon Kang1c92c3e2023-03-23 17:44:51 +00001030// Name of the java_api_library module that generates the from-text stubs source
1031// and compiles to a jar file.
1032func (c *commonToSdkLibraryAndImport) apiLibraryModuleName(apiScope *apiScope) string {
Spandan Das23956d12024-01-19 00:22:22 +00001033 baseName := c.module.RootLibraryName()
Jihoon Kang1c92c3e2023-03-23 17:44:51 +00001034 return c.namingScheme.apiLibraryModuleName(apiScope, baseName)
1035}
1036
Jihoon Kang1147b312023-06-08 23:25:57 +00001037// Name of the java_library module that compiles the stubs
1038// generated from source Java files.
Jihoon Kangfa4a90d2023-12-20 02:53:38 +00001039func (c *commonToSdkLibraryAndImport) sourceStubsLibraryModuleName(apiScope *apiScope) string {
Spandan Das23956d12024-01-19 00:22:22 +00001040 baseName := c.module.RootLibraryName()
Jihoon Kangfa4a90d2023-12-20 02:53:38 +00001041 return c.namingScheme.sourceStubsLibraryModuleName(apiScope, baseName)
1042}
1043
1044// Name of the java_library module that compiles the exportable stubs
1045// generated from source Java files.
1046func (c *commonToSdkLibraryAndImport) exportableSourceStubsLibraryModuleName(apiScope *apiScope) string {
Spandan Das23956d12024-01-19 00:22:22 +00001047 baseName := c.module.RootLibraryName()
Jihoon Kangfa4a90d2023-12-20 02:53:38 +00001048 return c.namingScheme.exportableSourceStubsLibraryModuleName(apiScope, baseName)
Jihoon Kang1147b312023-06-08 23:25:57 +00001049}
1050
Paul Duffin46dc45a2020-05-14 15:39:10 +01001051// The component names for different outputs of the java_sdk_library.
1052//
1053// They are similar to the names used for the child modules it creates
1054const (
1055 stubsSourceComponentName = "stubs.source"
1056
1057 apiTxtComponentName = "api.txt"
1058
1059 removedApiTxtComponentName = "removed-api.txt"
Anton Hanssond78eb762021-09-21 15:25:12 +01001060
1061 annotationsComponentName = "annotations.zip"
Paul Duffin46dc45a2020-05-14 15:39:10 +01001062)
1063
1064// A regular expression to match tags that reference a specific stubs component.
1065//
1066// It will only match if given a valid scope and a valid component. It is verfy strict
1067// to ensure it does not accidentally match a similar looking tag that should be processed
1068// by the embedded Library.
1069var tagSplitter = func() *regexp.Regexp {
1070 // Given a list of literal string items returns a regular expression that will
1071 // match any one of the items.
1072 choice := func(items ...string) string {
1073 return `\Q` + strings.Join(items, `\E|\Q`) + `\E`
1074 }
1075
1076 // Regular expression to match one of the scopes.
1077 scopesRegexp := choice(allScopeNames...)
1078
1079 // Regular expression to match one of the components.
Anton Hanssond78eb762021-09-21 15:25:12 +01001080 componentsRegexp := choice(stubsSourceComponentName, apiTxtComponentName, removedApiTxtComponentName, annotationsComponentName)
Paul Duffin46dc45a2020-05-14 15:39:10 +01001081
1082 // Regular expression to match any combination of one scope and one component.
1083 return regexp.MustCompile(fmt.Sprintf(`^\.(%s)\.(%s)$`, scopesRegexp, componentsRegexp))
1084}()
1085
mrziwang9f7b9f42024-07-10 12:18:06 -07001086func (module *commonToSdkLibraryAndImport) setOutputFiles(ctx android.ModuleContext) {
1087 if module.doctagPaths != nil {
1088 ctx.SetOutputFiles(module.doctagPaths, ".doctags")
1089 }
1090 for _, scopeName := range android.SortedKeys(scopeByName) {
1091 paths := module.findScopePaths(scopeByName[scopeName])
1092 if paths == nil {
1093 continue
Paul Duffin46dc45a2020-05-14 15:39:10 +01001094 }
mrziwang9f7b9f42024-07-10 12:18:06 -07001095 componentToOutput := map[string]android.OptionalPath{
1096 stubsSourceComponentName: paths.stubsSrcJar,
1097 apiTxtComponentName: paths.currentApiFilePath,
1098 removedApiTxtComponentName: paths.removedApiFilePath,
1099 annotationsComponentName: paths.annotationsZip,
1100 }
1101 for _, component := range android.SortedKeys(componentToOutput) {
1102 if componentToOutput[component].Valid() {
1103 ctx.SetOutputFiles(android.Paths{componentToOutput[component].Path()}, "."+scopeName+"."+component)
Paul Duffina2ae7e02020-09-11 11:55:00 +01001104 }
1105 }
Paul Duffin46dc45a2020-05-14 15:39:10 +01001106 }
1107}
1108
Paul Duffin803a9562020-05-20 11:52:25 +01001109func (c *commonToSdkLibraryAndImport) getScopePathsCreateIfNeeded(scope *apiScope) *scopePaths {
Paul Duffin56d44902020-01-31 13:36:25 +00001110 if c.scopePaths == nil {
1111 c.scopePaths = make(map[*apiScope]*scopePaths)
1112 }
1113 paths := c.scopePaths[scope]
1114 if paths == nil {
1115 paths = &scopePaths{}
1116 c.scopePaths[scope] = paths
1117 }
1118
1119 return paths
1120}
1121
Paul Duffin803a9562020-05-20 11:52:25 +01001122func (c *commonToSdkLibraryAndImport) findScopePaths(scope *apiScope) *scopePaths {
1123 if c.scopePaths == nil {
1124 return nil
1125 }
1126
1127 return c.scopePaths[scope]
1128}
1129
1130// If this does not support the requested api scope then find the closest available
1131// scope it does support. Returns nil if no such scope is available.
1132func (c *commonToSdkLibraryAndImport) findClosestScopePath(scope *apiScope) *scopePaths {
Paul Duffind0b9fca2022-09-30 18:11:41 +01001133 for s := scope; s != nil; s = s.canAccess {
Paul Duffin803a9562020-05-20 11:52:25 +01001134 if paths := c.findScopePaths(s); paths != nil {
1135 return paths
1136 }
1137 }
1138
1139 // This should never happen outside tests as public should be the base scope for every
1140 // scope and is enabled by default.
1141 return nil
1142}
1143
Jiyong Parkf1691d22021-03-29 20:11:58 +09001144func (c *commonToSdkLibraryAndImport) selectHeaderJarsForSdkVersion(ctx android.BaseModuleContext, sdkVersion android.SdkSpec) android.Paths {
Paul Duffinb05d4292020-05-20 12:19:10 +01001145
1146 // If a specific numeric version has been requested then use prebuilt versions of the sdk.
Jiyong Park54105c42021-03-31 18:17:53 +09001147 if !sdkVersion.ApiLevel.IsPreview() {
Spandan Das23956d12024-01-19 00:22:22 +00001148 return PrebuiltJars(ctx, c.module.RootLibraryName(), sdkVersion)
Paul Duffinb05d4292020-05-20 12:19:10 +01001149 }
1150
Paul Duffin1267d872021-04-16 17:21:36 +01001151 paths := c.selectScopePaths(ctx, sdkVersion.Kind)
1152 if paths == nil {
1153 return nil
1154 }
1155
1156 return paths.stubsHeaderPath
1157}
1158
1159// selectScopePaths returns the *scopePaths appropriate for the specific kind.
1160//
1161// If the module does not support the specific kind then it will return the *scopePaths for the
1162// closest kind which is a subset of the requested kind. e.g. if requesting android.SdkModule then
1163// it will return *scopePaths for android.SdkSystem if available or android.SdkPublic of not.
1164func (c *commonToSdkLibraryAndImport) selectScopePaths(ctx android.BaseModuleContext, kind android.SdkKind) *scopePaths {
Paul Duffin32cf58a2021-05-18 16:32:50 +01001165 apiScope := sdkKindToApiScope(kind)
Paul Duffinb05d4292020-05-20 12:19:10 +01001166
Paul Duffin803a9562020-05-20 11:52:25 +01001167 paths := c.findClosestScopePath(apiScope)
1168 if paths == nil {
1169 var scopes []string
Jihoon Kang98aa8fa2024-06-07 11:06:57 +00001170 for _, s := range AllApiScopes {
Paul Duffin803a9562020-05-20 11:52:25 +01001171 if c.findScopePaths(s) != nil {
1172 scopes = append(scopes, s.name)
1173 }
1174 }
Spandan Das23956d12024-01-19 00:22:22 +00001175 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 +01001176 return nil
1177 }
1178
Paul Duffin1267d872021-04-16 17:21:36 +01001179 return paths
1180}
1181
Paul Duffin32cf58a2021-05-18 16:32:50 +01001182// sdkKindToApiScope maps from android.SdkKind to apiScope.
1183func sdkKindToApiScope(kind android.SdkKind) *apiScope {
1184 var apiScope *apiScope
1185 switch kind {
1186 case android.SdkSystem:
1187 apiScope = apiScopeSystem
1188 case android.SdkModule:
1189 apiScope = apiScopeModuleLib
1190 case android.SdkTest:
1191 apiScope = apiScopeTest
1192 case android.SdkSystemServer:
1193 apiScope = apiScopeSystemServer
1194 default:
1195 apiScope = apiScopePublic
1196 }
1197 return apiScope
1198}
1199
Paul Duffin1267d872021-04-16 17:21:36 +01001200// to satisfy SdkLibraryDependency interface
Martin Stjernholm8be1e6d2021-09-15 03:34:04 +01001201func (c *commonToSdkLibraryAndImport) SdkApiStubDexJar(ctx android.BaseModuleContext, kind android.SdkKind) OptionalDexJarPath {
Paul Duffin1267d872021-04-16 17:21:36 +01001202 paths := c.selectScopePaths(ctx, kind)
1203 if paths == nil {
Martin Stjernholm8be1e6d2021-09-15 03:34:04 +01001204 return makeUnsetDexJarPath()
Paul Duffin1267d872021-04-16 17:21:36 +01001205 }
1206
1207 return paths.stubsDexJarPath
Paul Duffinb05d4292020-05-20 12:19:10 +01001208}
1209
Paul Duffin32cf58a2021-05-18 16:32:50 +01001210// to satisfy SdkLibraryDependency interface
Jihoon Kangbd093452023-12-26 19:08:01 +00001211func (c *commonToSdkLibraryAndImport) SdkApiExportableStubDexJar(ctx android.BaseModuleContext, kind android.SdkKind) OptionalDexJarPath {
1212 paths := c.selectScopePaths(ctx, kind)
1213 if paths == nil {
1214 return makeUnsetDexJarPath()
1215 }
1216
1217 return paths.exportableStubsDexJarPath
1218}
1219
1220// to satisfy SdkLibraryDependency interface
Paul Duffin32cf58a2021-05-18 16:32:50 +01001221func (c *commonToSdkLibraryAndImport) SdkRemovedTxtFile(ctx android.BaseModuleContext, kind android.SdkKind) android.OptionalPath {
1222 apiScope := sdkKindToApiScope(kind)
1223 paths := c.findScopePaths(apiScope)
1224 if paths == nil {
1225 return android.OptionalPath{}
1226 }
1227
1228 return paths.removedApiFilePath
1229}
1230
Paul Duffin859fe962020-05-15 10:20:31 +01001231func (c *commonToSdkLibraryAndImport) sdkComponentPropertiesForChildLibrary() interface{} {
1232 componentProps := &struct {
Paul Duffin3f0290e2021-06-30 18:25:36 +01001233 SdkLibraryName *string
Paul Duffin859fe962020-05-15 10:20:31 +01001234 SdkLibraryToImplicitlyTrack *string
Paul Duffindfa131e2020-05-15 20:37:11 +01001235 }{}
1236
Spandan Das23956d12024-01-19 00:22:22 +00001237 namePtr := proptools.StringPtr(c.module.RootLibraryName())
Paul Duffin3f0290e2021-06-30 18:25:36 +01001238 componentProps.SdkLibraryName = namePtr
1239
Paul Duffindfa131e2020-05-15 20:37:11 +01001240 if c.sharedLibrary() {
Paul Duffin859fe962020-05-15 10:20:31 +01001241 // Mark the stubs library as being components of this java_sdk_library so that
1242 // any app that includes code which depends (directly or indirectly) on the stubs
1243 // library will have the appropriate <uses-library> invocation inserted into its
1244 // manifest if necessary.
Paul Duffin3f0290e2021-06-30 18:25:36 +01001245 componentProps.SdkLibraryToImplicitlyTrack = namePtr
Paul Duffin859fe962020-05-15 10:20:31 +01001246 }
1247
1248 return componentProps
1249}
1250
Paul Duffindfa131e2020-05-15 20:37:11 +01001251func (c *commonToSdkLibraryAndImport) sharedLibrary() bool {
1252 return proptools.BoolDefault(c.commonSdkLibraryProperties.Shared_library, true)
1253}
1254
Paul Duffinf4600f62021-05-13 22:34:45 +01001255// Check if the stub libraries should be compiled for dex
1256func (c *commonToSdkLibraryAndImport) stubLibrariesCompiledForDex() bool {
1257 // Always compile the dex file files for the stub libraries if they will be used on the
1258 // bootclasspath.
1259 return !c.sharedLibrary()
1260}
1261
Paul Duffin859fe962020-05-15 10:20:31 +01001262// Properties related to the use of a module as an component of a java_sdk_library.
1263type SdkLibraryComponentProperties struct {
Paul Duffin3f0290e2021-06-30 18:25:36 +01001264 // The name of the java_sdk_library/_import module.
1265 SdkLibraryName *string `blueprint:"mutated"`
Paul Duffin859fe962020-05-15 10:20:31 +01001266
1267 // The name of the java_sdk_library/_import to add to a <uses-library> entry
1268 // in the AndroidManifest.xml of any Android app that includes code that references
1269 // this module. If not set then no java_sdk_library/_import is tracked.
1270 SdkLibraryToImplicitlyTrack *string `blueprint:"mutated"`
1271}
1272
1273// Structure to be embedded in a module struct that needs to support the
1274// SdkLibraryComponentDependency interface.
1275type EmbeddableSdkLibraryComponent struct {
1276 sdkLibraryComponentProperties SdkLibraryComponentProperties
1277}
1278
Paul Duffin71b33cc2021-06-23 11:39:47 +01001279func (e *EmbeddableSdkLibraryComponent) initSdkLibraryComponent(module android.Module) {
1280 module.AddProperties(&e.sdkLibraryComponentProperties)
Paul Duffin859fe962020-05-15 10:20:31 +01001281}
1282
1283// to satisfy SdkLibraryComponentDependency
Paul Duffin3f0290e2021-06-30 18:25:36 +01001284func (e *EmbeddableSdkLibraryComponent) SdkLibraryName() *string {
1285 return e.sdkLibraryComponentProperties.SdkLibraryName
1286}
1287
1288// to satisfy SdkLibraryComponentDependency
Ulya Trafimovich39b437b2020-09-23 16:42:35 +01001289func (e *EmbeddableSdkLibraryComponent) OptionalSdkLibraryImplementation() *string {
Ulya Trafimovich78645fb2021-07-16 15:29:25 +01001290 // For shared libraries, this is the same as the SDK library name. If a Java library or app
1291 // depends on a component library (e.g. a stub library) it still needs to know the name of the
1292 // run-time library and the corresponding module that provides the implementation. This name is
1293 // passed to manifest_fixer (to be added to AndroidManifest.xml) and added to CLC (to be used
1294 // in dexpreopt).
1295 //
1296 // For non-shared SDK (component or not) libraries this returns `nil`, as they are not
1297 // <uses-library> and should not be added to the manifest or to CLC.
Ulya Trafimovich39b437b2020-09-23 16:42:35 +01001298 return e.sdkLibraryComponentProperties.SdkLibraryToImplicitlyTrack
1299}
1300
Paul Duffin859fe962020-05-15 10:20:31 +01001301// Implemented by modules that are (or possibly could be) a component of a java_sdk_library
1302// (including the java_sdk_library) itself.
1303type SdkLibraryComponentDependency interface {
Ulya Trafimovich31e444e2020-08-14 17:32:16 +01001304 UsesLibraryDependency
1305
Paul Duffin3f0290e2021-06-30 18:25:36 +01001306 // SdkLibraryName returns the name of the java_sdk_library/_import module.
1307 SdkLibraryName() *string
1308
Ulya Trafimovich39b437b2020-09-23 16:42:35 +01001309 // The name of the implementation library for the optional SDK library or nil, if there isn't one.
1310 OptionalSdkLibraryImplementation() *string
Paul Duffin859fe962020-05-15 10:20:31 +01001311}
1312
1313// Make sure that all the module types that are components of java_sdk_library/_import
1314// and which can be referenced (directly or indirectly) from an android app implement
1315// the SdkLibraryComponentDependency interface.
1316var _ SdkLibraryComponentDependency = (*Library)(nil)
1317var _ SdkLibraryComponentDependency = (*Import)(nil)
1318var _ SdkLibraryComponentDependency = (*SdkLibrary)(nil)
Paul Duffineedc5d52020-06-12 17:46:39 +01001319var _ SdkLibraryComponentDependency = (*SdkLibraryImport)(nil)
Paul Duffin859fe962020-05-15 10:20:31 +01001320
Paul Duffin32cf58a2021-05-18 16:32:50 +01001321// Provides access to sdk_version related files, e.g. header and implementation jars.
Paul Duffin859fe962020-05-15 10:20:31 +01001322type SdkLibraryDependency interface {
1323 SdkLibraryComponentDependency
1324
1325 // Get the header jars appropriate for the supplied sdk_version.
1326 //
1327 // These are turbine generated jars so they only change if the externals of the
1328 // class changes but it does not contain and implementation or JavaDoc.
Jiyong Parkf1691d22021-03-29 20:11:58 +09001329 SdkHeaderJars(ctx android.BaseModuleContext, sdkVersion android.SdkSpec) android.Paths
Paul Duffin859fe962020-05-15 10:20:31 +01001330
Jihoon Kangbd093452023-12-26 19:08:01 +00001331 // SdkApiStubDexJar returns the dex jar for the stubs for the prebuilt
1332 // java_sdk_library_import module. It is needed by the hiddenapi processing tool which
1333 // processes dex files.
Martin Stjernholm8be1e6d2021-09-15 03:34:04 +01001334 SdkApiStubDexJar(ctx android.BaseModuleContext, kind android.SdkKind) OptionalDexJarPath
Paul Duffinf4600f62021-05-13 22:34:45 +01001335
Jihoon Kangbd093452023-12-26 19:08:01 +00001336 // SdkApiExportableStubDexJar returns the exportable dex jar for the stubs for
1337 // java_sdk_library module. It is needed by the hiddenapi processing tool which processes
1338 // dex files.
1339 SdkApiExportableStubDexJar(ctx android.BaseModuleContext, kind android.SdkKind) OptionalDexJarPath
1340
Paul Duffin32cf58a2021-05-18 16:32:50 +01001341 // SdkRemovedTxtFile returns the optional path to the removed.txt file for the specified sdk kind.
1342 SdkRemovedTxtFile(ctx android.BaseModuleContext, kind android.SdkKind) android.OptionalPath
1343
Paul Duffinf4600f62021-05-13 22:34:45 +01001344 // sharedLibrary returns true if this can be used as a shared library.
1345 sharedLibrary() bool
Jihoon Kanga3a05462024-04-05 00:36:44 +00001346
1347 getImplLibraryModule() *Library
Paul Duffin859fe962020-05-15 10:20:31 +01001348}
1349
Inseob Kimc0907f12019-02-08 21:00:45 +09001350type SdkLibrary struct {
Sundong Ahn054b19a2018-10-19 13:46:09 +09001351 Library
Jiyong Parkc678ad32018-04-10 13:07:10 +09001352
Sundong Ahn054b19a2018-10-19 13:46:09 +09001353 sdkLibraryProperties sdkLibraryProperties
Jiyong Parkc678ad32018-04-10 13:07:10 +09001354
Paul Duffin3375e352020-04-28 10:44:03 +01001355 // Map from api scope to the scope specific property structure.
1356 scopeToProperties map[*apiScope]*ApiScopeProperties
1357
Paul Duffin56d44902020-01-31 13:36:25 +00001358 commonToSdkLibraryAndImport
Jihoon Kanga3a05462024-04-05 00:36:44 +00001359
1360 builtInstalledForApex []dexpreopterInstall
Jiyong Parkc678ad32018-04-10 13:07:10 +09001361}
1362
Inseob Kimc0907f12019-02-08 21:00:45 +09001363var _ SdkLibraryDependency = (*SdkLibrary)(nil)
Colin Cross897d2ed2019-02-11 14:03:51 -08001364
Paul Duffin3375e352020-04-28 10:44:03 +01001365func (module *SdkLibrary) generateTestAndSystemScopesByDefault() bool {
1366 return module.sdkLibraryProperties.Generate_system_and_test_apis
1367}
1368
Jihoon Kanga3a05462024-04-05 00:36:44 +00001369func (module *SdkLibrary) DexJarBuildPath(ctx android.ModuleErrorfContext) OptionalDexJarPath {
1370 if module.implLibraryModule != nil {
1371 return module.implLibraryModule.DexJarBuildPath(ctx)
1372 }
1373 return makeUnsetDexJarPath()
1374}
1375
1376func (module *SdkLibrary) DexJarInstallPath() android.Path {
1377 if module.implLibraryModule != nil {
1378 return module.implLibraryModule.DexJarInstallPath()
1379 }
1380 return nil
1381}
1382
Paul Duffin3375e352020-04-28 10:44:03 +01001383func (module *SdkLibrary) getGeneratedApiScopes(ctx android.EarlyModuleContext) apiScopes {
1384 // Check to see if any scopes have been explicitly enabled. If any have then all
1385 // must be.
1386 anyScopesExplicitlyEnabled := false
Jihoon Kang98aa8fa2024-06-07 11:06:57 +00001387 for _, scope := range AllApiScopes {
Paul Duffin3375e352020-04-28 10:44:03 +01001388 scopeProperties := module.scopeToProperties[scope]
1389 if scopeProperties.Enabled != nil {
1390 anyScopesExplicitlyEnabled = true
1391 break
1392 }
Paul Duffind1b3a922020-01-22 11:57:20 +00001393 }
Paul Duffin3375e352020-04-28 10:44:03 +01001394
1395 var generatedScopes apiScopes
1396 enabledScopes := make(map[*apiScope]struct{})
Jihoon Kang98aa8fa2024-06-07 11:06:57 +00001397 for _, scope := range AllApiScopes {
Paul Duffin3375e352020-04-28 10:44:03 +01001398 scopeProperties := module.scopeToProperties[scope]
1399 // If any scopes are explicitly enabled then ignore the legacy enabled status.
1400 // This is to ensure that any new usages of this module type do not rely on legacy
1401 // behaviour.
1402 defaultEnabledStatus := false
1403 if anyScopesExplicitlyEnabled {
1404 defaultEnabledStatus = scope.defaultEnabledStatus
1405 } else {
1406 defaultEnabledStatus = scope.legacyEnabledStatus(module)
1407 }
1408 enabled := proptools.BoolDefault(scopeProperties.Enabled, defaultEnabledStatus)
1409 if enabled {
1410 enabledScopes[scope] = struct{}{}
1411 generatedScopes = append(generatedScopes, scope)
1412 }
1413 }
1414
1415 // Now check to make sure that any scope that is extended by an enabled scope is also
1416 // enabled.
Jihoon Kang98aa8fa2024-06-07 11:06:57 +00001417 for _, scope := range AllApiScopes {
Paul Duffin3375e352020-04-28 10:44:03 +01001418 if _, ok := enabledScopes[scope]; ok {
1419 extends := scope.extends
1420 if extends != nil {
1421 if _, ok := enabledScopes[extends]; !ok {
1422 ctx.ModuleErrorf("enabled api scope %q depends on disabled scope %q", scope, extends)
1423 }
1424 }
1425 }
1426 }
1427
1428 return generatedScopes
Paul Duffind1b3a922020-01-22 11:57:20 +00001429}
1430
satayev758968a2021-12-06 11:42:40 +00001431var _ android.ModuleWithMinSdkVersionCheck = (*SdkLibrary)(nil)
1432
satayev8f088b02021-12-06 11:40:46 +00001433func (module *SdkLibrary) CheckMinSdkVersion(ctx android.ModuleContext) {
Jihoon Kanga3a05462024-04-05 00:36:44 +00001434 CheckMinSdkVersion(ctx, &module.Library)
1435}
1436
1437func CheckMinSdkVersion(ctx android.ModuleContext, module *Library) {
Spandan Das8c9ae7e2023-03-03 21:20:36 +00001438 android.CheckMinSdkVersion(ctx, module.MinSdkVersion(ctx), func(c android.ModuleContext, do android.PayloadDepsCallback) {
satayev8f088b02021-12-06 11:40:46 +00001439 ctx.WalkDeps(func(child android.Module, parent android.Module) bool {
1440 isExternal := !module.depIsInSameApex(ctx, child)
1441 if am, ok := child.(android.ApexModule); ok {
1442 if !do(ctx, parent, am, isExternal) {
1443 return false
1444 }
1445 }
1446 return !isExternal
1447 })
1448 })
1449}
1450
Paul Duffineedc5d52020-06-12 17:46:39 +01001451type sdkLibraryComponentTag struct {
1452 blueprint.BaseDependencyTag
1453 name string
1454}
1455
1456// Mark this tag so dependencies that use it are excluded from visibility enforcement.
1457func (t sdkLibraryComponentTag) ExcludeFromVisibilityEnforcement() {}
1458
1459var xmlPermissionsFileTag = sdkLibraryComponentTag{name: "xml-permissions-file"}
Paul Duffine74ac732020-02-06 13:51:46 +00001460
Jiyong Parke3833882020-02-17 17:28:10 +09001461func IsXmlPermissionsFileDepTag(depTag blueprint.DependencyTag) bool {
Paul Duffineedc5d52020-06-12 17:46:39 +01001462 if dt, ok := depTag.(sdkLibraryComponentTag); ok {
Jiyong Parke3833882020-02-17 17:28:10 +09001463 return dt == xmlPermissionsFileTag
1464 }
1465 return false
1466}
1467
Paul Duffineedc5d52020-06-12 17:46:39 +01001468var implLibraryTag = sdkLibraryComponentTag{name: "impl-library"}
Paul Duffin5df79302020-05-16 15:52:12 +01001469
Jeongik Chaaaa6dcd2024-05-22 00:41:28 +09001470var _ android.InstallNeededDependencyTag = sdkLibraryComponentTag{}
1471
Jihoon Kang46d66de2024-05-22 22:42:39 +00001472// To satisfy the CopyDirectlyInAnyApexTag interface. Implementation library of the sdk library
1473// in an apex is considered to be directly in the apex, as if it was listed in java_libs.
1474func (t sdkLibraryComponentTag) CopyDirectlyInAnyApex() {}
1475
1476var _ android.CopyDirectlyInAnyApexTag = implLibraryTag
1477
Jeongik Chaaaa6dcd2024-05-22 00:41:28 +09001478func (t sdkLibraryComponentTag) InstallDepNeeded() bool {
1479 return t.name == "xml-permissions-file" || t.name == "impl-library"
1480}
1481
Paul Duffin44f1d842020-06-26 20:17:02 +01001482// Add the dependencies on the child modules in the component deps mutator.
1483func (module *SdkLibrary) ComponentDepsMutator(ctx android.BottomUpMutatorContext) {
Paul Duffin3375e352020-04-28 10:44:03 +01001484 for _, apiScope := range module.getGeneratedApiScopes(ctx) {
Paul Duffind1b3a922020-01-22 11:57:20 +00001485 // Add dependencies to the stubs library
Spandan Das877f39d2023-03-29 16:19:51 +00001486 stubModuleName := module.stubsLibraryModuleName(apiScope)
Jihoon Kangbd093452023-12-26 19:08:01 +00001487 ctx.AddVariationDependencies(nil, apiScope.everythingStubsTag, stubModuleName)
Jihoon Kang1147b312023-06-08 23:25:57 +00001488
Jihoon Kangbd093452023-12-26 19:08:01 +00001489 exportableStubModuleName := module.exportableStubsLibraryModuleName(apiScope)
1490 ctx.AddVariationDependencies(nil, apiScope.exportableStubsTag, exportableStubModuleName)
Paul Duffind1b3a922020-01-22 11:57:20 +00001491
Paul Duffin15f34ef2020-07-20 18:04:44 +01001492 // Add a dependency on the stubs source in order to access both stubs source and api information.
1493 ctx.AddVariationDependencies(nil, apiScope.stubsSourceAndApiTag, module.stubsSourceModuleName(apiScope))
Paul Duffin958806b2022-05-16 13:10:47 +00001494
1495 if module.compareAgainstLatestApi(apiScope) {
1496 // Add dependencies on the latest finalized version of the API .txt file.
1497 latestApiModuleName := module.latestApiModuleName(apiScope)
1498 ctx.AddDependency(module, apiScope.latestApiModuleTag, latestApiModuleName)
1499
1500 // Add dependencies on the latest finalized version of the remove API .txt file.
1501 latestRemovedApiModuleName := module.latestRemovedApiModuleName(apiScope)
1502 ctx.AddDependency(module, apiScope.latestRemovedApiModuleTag, latestRemovedApiModuleName)
1503 }
Sundong Ahn054b19a2018-10-19 13:46:09 +09001504 }
1505
Paul Duffindfa131e2020-05-15 20:37:11 +01001506 if module.requiresRuntimeImplementationLibrary() {
Paul Duffin5df79302020-05-16 15:52:12 +01001507 // Add dependency to the rule for generating the implementation library.
1508 ctx.AddDependency(module, implLibraryTag, module.implLibraryModuleName())
1509
Paul Duffindfa131e2020-05-15 20:37:11 +01001510 if module.sharedLibrary() {
1511 // Add dependency to the rule for generating the xml permissions file
Paul Duffineedc5d52020-06-12 17:46:39 +01001512 ctx.AddDependency(module, xmlPermissionsFileTag, module.xmlPermissionsModuleName())
Paul Duffindfa131e2020-05-15 20:37:11 +01001513 }
Paul Duffin44f1d842020-06-26 20:17:02 +01001514 }
1515}
Paul Duffine74ac732020-02-06 13:51:46 +00001516
Paul Duffin44f1d842020-06-26 20:17:02 +01001517// Add other dependencies as normal.
1518func (module *SdkLibrary) DepsMutator(ctx android.BottomUpMutatorContext) {
Anton Hanssone77fccc2021-01-20 16:52:41 +00001519 var missingApiModules []string
1520 for _, apiScope := range module.getGeneratedApiScopes(ctx) {
1521 if apiScope.unstable {
1522 continue
1523 }
Paul Duffin958806b2022-05-16 13:10:47 +00001524 if m := module.latestApiModuleName(apiScope); !ctx.OtherModuleExists(m) {
Anton Hanssone77fccc2021-01-20 16:52:41 +00001525 missingApiModules = append(missingApiModules, m)
1526 }
Paul Duffin958806b2022-05-16 13:10:47 +00001527 if m := module.latestRemovedApiModuleName(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.latestIncompatibilitiesModuleName(apiScope); !ctx.OtherModuleExists(m) {
Jaewoong Jung1a97ee02021-03-09 13:25:02 -08001531 missingApiModules = append(missingApiModules, m)
1532 }
Anton Hanssone77fccc2021-01-20 16:52:41 +00001533 }
1534 if len(missingApiModules) != 0 && !module.sdkLibraryProperties.Unsafe_ignore_missing_latest_api {
1535 m := module.Name() + " is missing tracking files for previously released library versions.\n"
1536 m += "You need to do one of the following:\n"
1537 m += "- Add `unsafe_ignore_missing_latest_api: true` to your blueprint (to disable compat tracking)\n"
1538 m += "- Add a set of prebuilt txt files representing the last released version of this library for compat checking.\n"
1539 m += " (the current set of API files can be used as a seed for this compatibility tracking\n"
1540 m += "\n"
1541 m += "The following filegroup modules are missing:\n "
1542 m += strings.Join(missingApiModules, "\n ") + "\n"
1543 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."
1544 ctx.ModuleErrorf(m)
1545 }
Jiyong Parkc678ad32018-04-10 13:07:10 +09001546}
1547
Inseob Kimc0907f12019-02-08 21:00:45 +09001548func (module *SdkLibrary) GenerateAndroidBuildActions(ctx android.ModuleContext) {
Spandan Das5ae65ee2024-04-16 22:03:26 +00001549 if disableSourceApexVariant(ctx) {
1550 // Prebuilts are active, do not create the installation rules for the source javalib.
1551 // Even though the source javalib is not used, we need to hide it to prevent duplicate installation rules.
1552 // TODO (b/331665856): Implement a principled solution for this.
1553 module.HideFromMake()
1554 }
satayev8f088b02021-12-06 11:40:46 +00001555
Paul Duffina2ae7e02020-09-11 11:55:00 +01001556 module.generateCommonBuildActions(ctx)
1557
Jihoon Kanga3a05462024-04-05 00:36:44 +00001558 module.stem = proptools.StringDefault(module.overridableProperties.Stem, ctx.ModuleName())
1559
1560 module.provideHiddenAPIPropertyInfo(ctx)
Sundong Ahn054b19a2018-10-19 13:46:09 +09001561
Paul Duffinb97b1572021-04-29 21:50:40 +01001562 // Collate the components exported by this module. All scope specific modules are exported but
1563 // the impl and xml component modules are not.
1564 exportedComponents := map[string]struct{}{}
1565
Sundong Ahn57368eb2018-07-06 11:20:23 +09001566 // Record the paths to the header jars of the library (stubs and impl).
Paul Duffind1b3a922020-01-22 11:57:20 +00001567 // When this java_sdk_library is depended upon from others via "libs" property,
Jiyong Parkc678ad32018-04-10 13:07:10 +09001568 // the recorded paths will be returned depending on the link type of the caller.
1569 ctx.VisitDirectDeps(func(to android.Module) {
Jiyong Parkc678ad32018-04-10 13:07:10 +09001570 tag := ctx.OtherModuleDependencyTag(to)
1571
Paul Duffinc8782502020-04-29 20:45:27 +01001572 // Extract information from any of the scope specific dependencies.
1573 if scopeTag, ok := tag.(scopeDependencyTag); ok {
1574 apiScope := scopeTag.apiScope
Paul Duffin803a9562020-05-20 11:52:25 +01001575 scopePaths := module.getScopePathsCreateIfNeeded(apiScope)
Paul Duffinc8782502020-04-29 20:45:27 +01001576
1577 // Extract information from the dependency. The exact information extracted
1578 // is determined by the nature of the dependency which is determined by the tag.
1579 scopeTag.extractDepInfo(ctx, to, scopePaths)
Paul Duffinb97b1572021-04-29 21:50:40 +01001580
1581 exportedComponents[ctx.OtherModuleName(to)] = struct{}{}
Sundong Ahn20e998b2018-07-24 11:19:26 +09001582 }
Jihoon Kang8479dea2024-04-04 01:19:05 +00001583
1584 if tag == implLibraryTag {
1585 if dep, ok := android.OtherModuleProvider(ctx, to, JavaInfoProvider); ok {
1586 module.implLibraryHeaderJars = append(module.implLibraryHeaderJars, dep.HeaderJars...)
Jihoon Kanga3a05462024-04-05 00:36:44 +00001587 module.implLibraryModule = to.(*Library)
1588 android.SetProvider(ctx, JavaInfoProvider, dep)
Jihoon Kang8479dea2024-04-04 01:19:05 +00001589 }
1590 }
Jiyong Parkc678ad32018-04-10 13:07:10 +09001591 })
Paul Duffinb97b1572021-04-29 21:50:40 +01001592
Jihoon Kanga3a05462024-04-05 00:36:44 +00001593 apexInfo, _ := android.ModuleProvider(ctx, android.ApexInfoProvider)
1594 if !apexInfo.IsForPlatform() {
1595 module.hideApexVariantFromMake = true
1596 }
1597
1598 if module.implLibraryModule != nil {
1599 if ctx.Device() {
1600 module.classesJarPaths = android.Paths{module.implLibraryModule.implementationJarFile}
1601 module.bootDexJarPath = module.implLibraryModule.bootDexJarPath
1602 module.uncompressDexState = module.implLibraryModule.uncompressDexState
1603 module.active = module.implLibraryModule.active
1604 }
1605
1606 module.outputFile = module.implLibraryModule.outputFile
1607 module.dexJarFile = makeDexJarPathFromPath(module.implLibraryModule.dexJarFile.Path())
1608 module.headerJarFile = module.implLibraryModule.headerJarFile
1609 module.implementationAndResourcesJar = module.implLibraryModule.implementationAndResourcesJar
1610 module.builtInstalledForApex = module.implLibraryModule.builtInstalledForApex
1611 module.dexpreopter.configPath = module.implLibraryModule.dexpreopter.configPath
1612 module.dexpreopter.outputProfilePathOnHost = module.implLibraryModule.dexpreopter.outputProfilePathOnHost
1613
Jihoon Kang34155e32024-05-20 19:08:49 +00001614 // Properties required for Library.AndroidMkEntries
1615 module.logtagsSrcs = module.implLibraryModule.logtagsSrcs
1616 module.dexpreopter.builtInstalled = module.implLibraryModule.dexpreopter.builtInstalled
1617 module.jacocoReportClassesFile = module.implLibraryModule.jacocoReportClassesFile
1618 module.dexer.proguardDictionary = module.implLibraryModule.dexer.proguardDictionary
1619 module.dexer.proguardUsageZip = module.implLibraryModule.dexer.proguardUsageZip
1620 module.linter.reports = module.implLibraryModule.linter.reports
Jihoon Kang629e2a32024-06-25 20:47:49 +00001621 module.linter.outputs.depSets = module.implLibraryModule.LintDepSets()
Jihoon Kang34155e32024-05-20 19:08:49 +00001622
Jihoon Kanga3a05462024-04-05 00:36:44 +00001623 if !module.Host() {
1624 module.hostdexInstallFile = module.implLibraryModule.hostdexInstallFile
1625 }
1626
1627 android.SetProvider(ctx, blueprint.SrcsFileProviderKey, blueprint.SrcsFileProviderData{SrcPaths: module.implLibraryModule.uniqueSrcFiles.Strings()})
1628 }
1629
Paul Duffinb97b1572021-04-29 21:50:40 +01001630 // Make the set of components exported by this module available for use elsewhere.
Cole Faust18994c72023-02-28 16:02:16 -08001631 exportedComponentInfo := android.ExportedComponentsInfo{Components: android.SortedKeys(exportedComponents)}
Colin Cross40213022023-12-13 15:19:49 -08001632 android.SetProvider(ctx, android.ExportedComponentsInfoProvider, exportedComponentInfo)
Paul Duffin958806b2022-05-16 13:10:47 +00001633
1634 // Provide additional information for inclusion in an sdk's generated .info file.
1635 additionalSdkInfo := map[string]interface{}{}
1636 additionalSdkInfo["dist_stem"] = module.distStem()
Paul Duffine8409952022-09-22 16:24:46 +01001637 baseModuleName := module.distStem()
Paul Duffin958806b2022-05-16 13:10:47 +00001638 scopes := map[string]interface{}{}
1639 additionalSdkInfo["scopes"] = scopes
1640 for scope, scopePaths := range module.scopePaths {
1641 scopeInfo := map[string]interface{}{}
1642 scopes[scope.name] = scopeInfo
1643 scopeInfo["current_api"] = scope.snapshotRelativeCurrentApiTxtPath(baseModuleName)
1644 scopeInfo["removed_api"] = scope.snapshotRelativeRemovedApiTxtPath(baseModuleName)
Jihoon Kang5623e542024-01-31 23:27:26 +00001645 if p := scopePaths.latestApiPaths; len(p) > 0 {
1646 // The last path in the list is the one that applies to this scope, the
1647 // preceding ones, if any, are for the scope(s) that it extends.
1648 scopeInfo["latest_api"] = p[len(p)-1].String()
Paul Duffin958806b2022-05-16 13:10:47 +00001649 }
Jihoon Kang5623e542024-01-31 23:27:26 +00001650 if p := scopePaths.latestRemovedApiPaths; len(p) > 0 {
1651 // The last path in the list is the one that applies to this scope, the
1652 // preceding ones, if any, are for the scope(s) that it extends.
1653 scopeInfo["latest_removed_api"] = p[len(p)-1].String()
Paul Duffin958806b2022-05-16 13:10:47 +00001654 }
1655 }
Colin Cross40213022023-12-13 15:19:49 -08001656 android.SetProvider(ctx, android.AdditionalSdkInfoProvider, android.AdditionalSdkInfo{additionalSdkInfo})
mrziwang9f7b9f42024-07-10 12:18:06 -07001657 module.setOutputFiles(ctx)
1658 if module.requiresRuntimeImplementationLibrary() && module.implLibraryModule != nil {
1659 setOutputFiles(ctx, module.implLibraryModule.Module)
1660 }
Jiyong Parkc678ad32018-04-10 13:07:10 +09001661}
1662
Jihoon Kanga3a05462024-04-05 00:36:44 +00001663func (module *SdkLibrary) BuiltInstalledForApex() []dexpreopterInstall {
1664 return module.builtInstalledForApex
1665}
1666
Jiyong Park0b0e1b92019-12-03 13:24:29 +09001667func (module *SdkLibrary) AndroidMkEntries() []android.AndroidMkEntries {
Paul Duffindfa131e2020-05-15 20:37:11 +01001668 if !module.requiresRuntimeImplementationLibrary() {
Paul Duffin43db9be2019-12-30 17:35:49 +00001669 return nil
1670 }
Jiyong Park0b0e1b92019-12-03 13:24:29 +09001671 entriesList := module.Library.AndroidMkEntries()
Jihoon Kanga3a05462024-04-05 00:36:44 +00001672 entries := &entriesList[0]
1673 entries.Required = append(entries.Required, module.implLibraryModuleName())
Yo Chiang07d75072020-06-05 17:43:19 +08001674 if module.sharedLibrary() {
Yo Chiang07d75072020-06-05 17:43:19 +08001675 entries.Required = append(entries.Required, module.xmlPermissionsModuleName())
1676 }
Jiyong Park0b0e1b92019-12-03 13:24:29 +09001677 return entriesList
Jiyong Park82484c02018-04-23 21:41:26 +09001678}
1679
Anton Hansson5fd5d242020-03-27 19:43:19 +00001680// The dist path of the stub artifacts
1681func (module *SdkLibrary) apiDistPath(apiScope *apiScope) string {
Colin Crossf0eace92021-06-02 13:02:23 -07001682 return path.Join("apistubs", module.distGroup(), apiScope.name)
Anton Hansson5fd5d242020-03-27 19:43:19 +00001683}
1684
Paul Duffin12ceb462019-12-24 20:31:31 +00001685// Get the sdk version for use when compiling the stubs library.
Paul Duffin780c5f42020-05-12 15:52:55 +01001686func (module *SdkLibrary) sdkVersionForStubsLibrary(mctx android.EarlyModuleContext, apiScope *apiScope) string {
Paul Duffin87a05a32020-05-12 11:50:28 +01001687 scopeProperties := module.scopeToProperties[apiScope]
1688 if scopeProperties.Sdk_version != nil {
1689 return proptools.String(scopeProperties.Sdk_version)
1690 }
1691
Jiyong Parkf1691d22021-03-29 20:11:58 +09001692 sdkDep := decodeSdkDep(mctx, android.SdkContext(&module.Library))
Paul Duffin12ceb462019-12-24 20:31:31 +00001693 if sdkDep.hasStandardLibs() {
1694 // If building against a standard sdk then use the sdk version appropriate for the scope.
Paul Duffind1b3a922020-01-22 11:57:20 +00001695 return apiScope.sdkVersion
Paul Duffin12ceb462019-12-24 20:31:31 +00001696 } else {
1697 // Otherwise, use no system module.
1698 return "none"
1699 }
1700}
1701
Paul Duffin31310252020-11-20 21:26:20 +00001702func (module *SdkLibrary) distStem() string {
1703 return proptools.StringDefault(module.sdkLibraryProperties.Dist_stem, module.BaseModuleName())
1704}
1705
Colin Cross986b69a2021-06-01 13:13:40 -07001706// distGroup returns the subdirectory of the dist path of the stub artifacts.
1707func (module *SdkLibrary) distGroup() string {
Colin Cross59b92bf2021-06-01 14:07:56 -07001708 return proptools.StringDefault(module.sdkLibraryProperties.Dist_group, "unknown")
Colin Cross986b69a2021-06-01 13:13:40 -07001709}
1710
Paul Duffin958806b2022-05-16 13:10:47 +00001711func latestPrebuiltApiModuleName(name string, apiScope *apiScope) string {
1712 return PrebuiltApiModuleName(name, apiScope.name, "latest")
1713}
1714
Jihoon Kang748a24d2024-03-20 21:29:39 +00001715func latestPrebuiltApiCombinedModuleName(name string, apiScope *apiScope) string {
1716 return PrebuiltApiCombinedModuleName(name, apiScope.name, "latest")
1717}
1718
Paul Duffind1b3a922020-01-22 11:57:20 +00001719func (module *SdkLibrary) latestApiFilegroupName(apiScope *apiScope) string {
Paul Duffin958806b2022-05-16 13:10:47 +00001720 return ":" + module.latestApiModuleName(apiScope)
1721}
1722
1723func (module *SdkLibrary) latestApiModuleName(apiScope *apiScope) string {
Jihoon Kang748a24d2024-03-20 21:29:39 +00001724 return latestPrebuiltApiCombinedModuleName(module.distStem(), apiScope)
Jiyong Park58c518b2018-05-12 22:29:12 +09001725}
Jiyong Parkc678ad32018-04-10 13:07:10 +09001726
Paul Duffind1b3a922020-01-22 11:57:20 +00001727func (module *SdkLibrary) latestRemovedApiFilegroupName(apiScope *apiScope) string {
Paul Duffin958806b2022-05-16 13:10:47 +00001728 return ":" + module.latestRemovedApiModuleName(apiScope)
1729}
1730
1731func (module *SdkLibrary) latestRemovedApiModuleName(apiScope *apiScope) string {
Jihoon Kang748a24d2024-03-20 21:29:39 +00001732 return latestPrebuiltApiCombinedModuleName(module.distStem()+"-removed", apiScope)
Jiyong Parkc678ad32018-04-10 13:07:10 +09001733}
1734
Jaewoong Jung1a97ee02021-03-09 13:25:02 -08001735func (module *SdkLibrary) latestIncompatibilitiesFilegroupName(apiScope *apiScope) string {
Paul Duffin958806b2022-05-16 13:10:47 +00001736 return ":" + module.latestIncompatibilitiesModuleName(apiScope)
1737}
1738
1739func (module *SdkLibrary) latestIncompatibilitiesModuleName(apiScope *apiScope) string {
1740 return latestPrebuiltApiModuleName(module.distStem()+"-incompatibilities", apiScope)
Jaewoong Jung1a97ee02021-03-09 13:25:02 -08001741}
1742
Jihoon Kang1c92c3e2023-03-23 17:44:51 +00001743func (module *SdkLibrary) contributesToApiSurface(c android.Config) bool {
1744 _, exists := c.GetApiLibraries()[module.Name()]
1745 return exists
1746}
1747
Jihoon Kang0c705a42023-08-02 06:44:57 +00001748// The listed modules are the special java_sdk_libraries where apiScope.kind do not match the
1749// api surface that the module contribute to. For example, the public droidstubs and java_library
1750// do not contribute to the public api surface, but contributes to the core platform api surface.
1751// This method returns the full api surface stub lib that
1752// the generated java_api_library should depend on.
1753func (module *SdkLibrary) alternativeFullApiSurfaceStubLib() string {
1754 if val, ok := apiLibraryAdditionalProperties[module.Name()]; ok {
1755 return val.FullApiSurfaceStubLib
1756 }
1757 return ""
1758}
1759
1760// The listed modules' stubs contents do not match the corresponding txt files,
1761// but require additional api contributions to generate the full stubs.
1762// This method returns the name of the additional api contribution module
1763// for corresponding sdk_library modules.
1764func (module *SdkLibrary) apiLibraryAdditionalApiContribution() string {
1765 if val, ok := apiLibraryAdditionalProperties[module.Name()]; ok {
1766 return val.AdditionalApiContribution
1767 }
1768 return ""
1769}
1770
Anton Hansson944e77d2020-08-19 11:40:22 +01001771func childModuleVisibility(childVisibility []string) []string {
1772 if childVisibility == nil {
1773 // No child visibility set. The child will use the visibility of the sdk_library.
1774 return nil
1775 }
1776
1777 // Prepend an override to ignore the sdk_library's visibility, and rely on the child visibility.
1778 var visibility []string
1779 visibility = append(visibility, "//visibility:override")
1780 visibility = append(visibility, childVisibility...)
1781 return visibility
1782}
1783
Paul Duffin5df79302020-05-16 15:52:12 +01001784// Creates the implementation java library
1785func (module *SdkLibrary) createImplLibrary(mctx android.DefaultableHookContext) {
Anton Hansson944e77d2020-08-19 11:40:22 +01001786 visibility := childModuleVisibility(module.sdkLibraryProperties.Impl_library_visibility)
1787
Paul Duffin5df79302020-05-16 15:52:12 +01001788 props := struct {
Paul Duffin77590a82022-04-28 14:13:30 +00001789 Name *string
1790 Visibility []string
Paul Duffin77590a82022-04-28 14:13:30 +00001791 Libs []string
1792 Static_libs []string
1793 Apex_available []string
Jihoon Kanga3a05462024-04-05 00:36:44 +00001794 Stem *string
Paul Duffin5df79302020-05-16 15:52:12 +01001795 }{
1796 Name: proptools.StringPtr(module.implLibraryModuleName()),
Anton Hansson944e77d2020-08-19 11:40:22 +01001797 Visibility: visibility,
Jihoon Kanga3a05462024-04-05 00:36:44 +00001798
1799 Libs: append(module.properties.Libs, module.sdkLibraryProperties.Impl_only_libs...),
1800
1801 Static_libs: append(module.properties.Static_libs, module.sdkLibraryProperties.Impl_only_static_libs...),
Paul Duffin77590a82022-04-28 14:13:30 +00001802 // Pass the apex_available settings down so that the impl library can be statically
1803 // embedded within a library that is added to an APEX. Needed for updatable-media.
1804 Apex_available: module.ApexAvailable(),
Jihoon Kanga3a05462024-04-05 00:36:44 +00001805
1806 Stem: proptools.StringPtr(module.Name()),
Paul Duffin5df79302020-05-16 15:52:12 +01001807 }
1808
1809 properties := []interface{}{
1810 &module.properties,
1811 &module.protoProperties,
1812 &module.deviceProperties,
Liz Kammera7a64f32020-07-09 15:16:41 -07001813 &module.dexProperties,
Paul Duffin5df79302020-05-16 15:52:12 +01001814 &module.dexpreoptProperties,
Colin Cross014489c2020-06-02 20:09:13 -07001815 &module.linter.properties,
Spandan Dasb9c58352024-05-13 18:29:45 +00001816 &module.overridableProperties,
Paul Duffin5df79302020-05-16 15:52:12 +01001817 &props,
1818 module.sdkComponentPropertiesForChildLibrary(),
1819 }
1820 mctx.CreateModule(LibraryFactory, properties...)
1821}
1822
Jihoon Kangfa4a90d2023-12-20 02:53:38 +00001823type libraryProperties struct {
1824 Name *string
1825 Visibility []string
1826 Srcs []string
1827 Installable *bool
1828 Sdk_version *string
1829 System_modules *string
1830 Patch_module *string
1831 Libs []string
1832 Static_libs []string
1833 Compile_dex *bool
1834 Java_version *string
1835 Openjdk9 struct {
1836 Srcs []string
1837 Javacflags []string
1838 }
1839 Dist struct {
1840 Targets []string
1841 Dest *string
1842 Dir *string
1843 Tag *string
1844 }
Jihoon Kangfe914ed2024-02-12 22:49:21 +00001845 Is_stubs_module *bool
Jihoon Kangfa4a90d2023-12-20 02:53:38 +00001846}
Jiyong Parkc678ad32018-04-10 13:07:10 +09001847
Jihoon Kangfa4a90d2023-12-20 02:53:38 +00001848func (module *SdkLibrary) stubsLibraryProps(mctx android.DefaultableHookContext, apiScope *apiScope) libraryProperties {
1849 props := libraryProperties{}
Jihoon Kang786df932023-09-07 01:18:31 +00001850 props.Visibility = []string{"//visibility:override", "//visibility:private"}
Jiyong Parkc678ad32018-04-10 13:07:10 +09001851 // sources are generated from the droiddoc
Paul Duffin12ceb462019-12-24 20:31:31 +00001852 sdkVersion := module.sdkVersionForStubsLibrary(mctx, apiScope)
Paul Duffin52d398a2019-06-11 12:31:14 +01001853 props.Sdk_version = proptools.StringPtr(sdkVersion)
Paul Duffina18abc22020-05-16 18:54:24 +01001854 props.System_modules = module.deviceProperties.System_modules
1855 props.Patch_module = module.properties.Patch_module
Paul Duffin367ab912019-12-23 19:40:36 +00001856 props.Installable = proptools.BoolPtr(false)
Sundong Ahn054b19a2018-10-19 13:46:09 +09001857 props.Libs = module.sdkLibraryProperties.Stub_only_libs
Mark White9421c4c2023-08-10 00:07:03 +00001858 props.Libs = append(props.Libs, module.scopeToProperties[apiScope].Libs...)
Anton Hanssondae54cd2021-04-21 16:30:10 +01001859 props.Static_libs = module.sdkLibraryProperties.Stub_only_static_libs
Paul Duffine22c2ab2020-05-20 19:35:27 +01001860 // The stub-annotations library contains special versions of the annotations
1861 // with CLASS retention policy, so that they're kept.
1862 if proptools.Bool(module.sdkLibraryProperties.Annotations_enabled) {
1863 props.Libs = append(props.Libs, "stub-annotations")
1864 }
Paul Duffina18abc22020-05-16 18:54:24 +01001865 props.Openjdk9.Srcs = module.properties.Openjdk9.Srcs
1866 props.Openjdk9.Javacflags = module.properties.Openjdk9.Javacflags
Anton Hansson83509b52020-05-21 09:21:57 +01001867 // We compile the stubs for 1.8 in line with the main android.jar stubs, and potential
1868 // interop with older developer tools that don't support 1.9.
1869 props.Java_version = proptools.StringPtr("1.8")
Jihoon Kangfe914ed2024-02-12 22:49:21 +00001870 props.Is_stubs_module = proptools.BoolPtr(true)
Paul Duffinf4600f62021-05-13 22:34:45 +01001871
Jihoon Kangfa4a90d2023-12-20 02:53:38 +00001872 return props
1873}
1874
1875// Creates a static java library that has API stubs
1876func (module *SdkLibrary) createStubsLibrary(mctx android.DefaultableHookContext, apiScope *apiScope) {
1877
1878 props := module.stubsLibraryProps(mctx, apiScope)
1879 props.Name = proptools.StringPtr(module.sourceStubsLibraryModuleName(apiScope))
1880 props.Srcs = []string{":" + module.stubsSourceModuleName(apiScope)}
1881
1882 mctx.CreateModule(LibraryFactory, &props, module.sdkComponentPropertiesForChildLibrary())
1883}
1884
1885// Create a static java library that compiles the "exportable" stubs
1886func (module *SdkLibrary) createExportableStubsLibrary(mctx android.DefaultableHookContext, apiScope *apiScope) {
1887 props := module.stubsLibraryProps(mctx, apiScope)
1888 props.Name = proptools.StringPtr(module.exportableSourceStubsLibraryModuleName(apiScope))
1889 props.Srcs = []string{":" + module.stubsSourceModuleName(apiScope) + "{.exportable}"}
1890
Paul Duffin859fe962020-05-15 10:20:31 +01001891 mctx.CreateModule(LibraryFactory, &props, module.sdkComponentPropertiesForChildLibrary())
Jiyong Parkc678ad32018-04-10 13:07:10 +09001892}
1893
Paul Duffin6d0886e2020-04-07 18:49:53 +01001894// Creates a droidstubs module that creates stubs source files from the given full source
Paul Duffinc8782502020-04-29 20:45:27 +01001895// files and also updates and checks the API specification files.
Paul Duffin15f34ef2020-07-20 18:04:44 +01001896func (module *SdkLibrary) createStubsSourcesAndApi(mctx android.DefaultableHookContext, apiScope *apiScope, name string, scopeSpecificDroidstubsArgs []string) {
Jiyong Parkc678ad32018-04-10 13:07:10 +09001897 props := struct {
Sundong Ahn054b19a2018-10-19 13:46:09 +09001898 Name *string
Paul Duffin4911a892020-04-29 23:35:13 +01001899 Visibility []string
Sundong Ahn054b19a2018-10-19 13:46:09 +09001900 Srcs []string
1901 Installable *bool
Paul Duffin52d398a2019-06-11 12:31:14 +01001902 Sdk_version *string
Jihoon Kang3198f3c2023-01-26 08:08:52 +00001903 Api_surface *string
Paul Duffin12ceb462019-12-24 20:31:31 +00001904 System_modules *string
Sundong Ahn054b19a2018-10-19 13:46:09 +09001905 Libs []string
Paul Duffin6877e6d2020-09-25 19:59:14 +01001906 Output_javadoc_comments *bool
Paul Duffin11512472019-02-11 15:55:17 +00001907 Arg_files []string
Sundong Ahn054b19a2018-10-19 13:46:09 +09001908 Args *string
Sundong Ahn054b19a2018-10-19 13:46:09 +09001909 Java_version *string
Paul Duffine22c2ab2020-05-20 19:35:27 +01001910 Annotations_enabled *bool
Sundong Ahn054b19a2018-10-19 13:46:09 +09001911 Merge_annotations_dirs []string
1912 Merge_inclusion_annotations_dirs []string
Paul Duffin0ff08bd2020-04-29 13:30:54 +01001913 Generate_stubs *bool
Anton Hanssone87b03d2020-12-21 15:29:34 +00001914 Previous_api *string
Jihoon Kang6592e872023-12-19 01:13:16 +00001915 Aconfig_declarations []string
Sundong Ahn054b19a2018-10-19 13:46:09 +09001916 Check_api struct {
Anton Hanssone6056152020-12-31 10:37:27 +00001917 Current ApiToCheck
1918 Last_released ApiToCheck
Paul Duffin160fe412020-05-10 19:32:20 +01001919
1920 Api_lint struct {
1921 Enabled *bool
1922 New_since *string
1923 Baseline_file *string
1924 }
Jiyong Park58c518b2018-05-12 22:29:12 +09001925 }
Sundong Ahn1b92c822018-05-29 11:35:17 +09001926 Aidl struct {
1927 Include_dirs []string
1928 Local_include_dirs []string
1929 }
Paul Duffin040e9062020-11-23 17:41:36 +00001930 Dists []android.Dist
Jiyong Parkc678ad32018-04-10 13:07:10 +09001931 }{}
1932
Paul Duffin7b78b4d2020-04-28 14:08:32 +01001933 // The stubs source processing uses the same compile time classpath when extracting the
1934 // API from the implementation library as it does when compiling it. i.e. the same
1935 // * sdk version
1936 // * system_modules
1937 // * libs (static_libs/libs)
Paul Duffin250e6192019-06-07 10:44:37 +01001938
Paul Duffin0ff08bd2020-04-29 13:30:54 +01001939 props.Name = proptools.StringPtr(name)
Anton Hansson944e77d2020-08-19 11:40:22 +01001940 props.Visibility = childModuleVisibility(module.sdkLibraryProperties.Stubs_source_visibility)
Paul Duffina18abc22020-05-16 18:54:24 +01001941 props.Srcs = append(props.Srcs, module.properties.Srcs...)
Anton Hanssonf8ea3722021-09-16 14:24:13 +01001942 props.Srcs = append(props.Srcs, module.sdkLibraryProperties.Api_srcs...)
Paul Duffina18abc22020-05-16 18:54:24 +01001943 props.Sdk_version = module.deviceProperties.Sdk_version
Jihoon Kang3198f3c2023-01-26 08:08:52 +00001944 props.Api_surface = &apiScope.name
Paul Duffina18abc22020-05-16 18:54:24 +01001945 props.System_modules = module.deviceProperties.System_modules
Jiyong Parkc678ad32018-04-10 13:07:10 +09001946 props.Installable = proptools.BoolPtr(false)
Sundong Ahne6f0b052018-06-05 16:46:14 +09001947 // A droiddoc module has only one Libs property and doesn't distinguish between
1948 // shared libs and static libs. So we need to add both of these libs to Libs property.
Paul Duffina18abc22020-05-16 18:54:24 +01001949 props.Libs = module.properties.Libs
1950 props.Libs = append(props.Libs, module.properties.Static_libs...)
Nikita Ioffed732da72022-11-21 12:38:25 +00001951 props.Libs = append(props.Libs, module.sdkLibraryProperties.Stub_only_libs...)
Mark White9421c4c2023-08-10 00:07:03 +00001952 props.Libs = append(props.Libs, module.scopeToProperties[apiScope].Libs...)
Paul Duffina18abc22020-05-16 18:54:24 +01001953 props.Aidl.Include_dirs = module.deviceProperties.Aidl.Include_dirs
1954 props.Aidl.Local_include_dirs = module.deviceProperties.Aidl.Local_include_dirs
1955 props.Java_version = module.properties.Java_version
Jiyong Parkc678ad32018-04-10 13:07:10 +09001956
Paul Duffine22c2ab2020-05-20 19:35:27 +01001957 props.Annotations_enabled = module.sdkLibraryProperties.Annotations_enabled
Sundong Ahn054b19a2018-10-19 13:46:09 +09001958 props.Merge_annotations_dirs = module.sdkLibraryProperties.Merge_annotations_dirs
1959 props.Merge_inclusion_annotations_dirs = module.sdkLibraryProperties.Merge_inclusion_annotations_dirs
Jihoon Kang6592e872023-12-19 01:13:16 +00001960 props.Aconfig_declarations = module.sdkLibraryProperties.Aconfig_declarations
Sundong Ahn054b19a2018-10-19 13:46:09 +09001961
Paul Duffin6d0886e2020-04-07 18:49:53 +01001962 droidstubsArgs := []string{}
Paul Duffin235ffff2019-12-24 10:41:30 +00001963 if len(module.sdkLibraryProperties.Api_packages) != 0 {
Paul Duffin6d0886e2020-04-07 18:49:53 +01001964 droidstubsArgs = append(droidstubsArgs, "--stub-packages "+strings.Join(module.sdkLibraryProperties.Api_packages, ":"))
Paul Duffin235ffff2019-12-24 10:41:30 +00001965 }
Paul Duffin6d0886e2020-04-07 18:49:53 +01001966 droidstubsArgs = append(droidstubsArgs, module.sdkLibraryProperties.Droiddoc_options...)
Anton Hanssonfd1c0d22023-11-02 15:18:09 +00001967 disabledWarnings := []string{"HiddenSuperclass"}
1968 if proptools.BoolDefault(module.sdkLibraryProperties.Api_lint.Legacy_errors_allowed, true) {
1969 disabledWarnings = append(disabledWarnings,
1970 "BroadcastBehavior",
1971 "DeprecationMismatch",
1972 "MissingPermission",
1973 "SdkConstant",
1974 "Todo",
1975 )
Paul Duffin235ffff2019-12-24 10:41:30 +00001976 }
Paul Duffin6d0886e2020-04-07 18:49:53 +01001977 droidstubsArgs = append(droidstubsArgs, android.JoinWithPrefix(disabledWarnings, "--hide "))
Sundong Ahnfb2721f2018-09-17 13:23:09 +09001978
Paul Duffin6877e6d2020-09-25 19:59:14 +01001979 // Output Javadoc comments for public scope.
1980 if apiScope == apiScopePublic {
1981 props.Output_javadoc_comments = proptools.BoolPtr(true)
1982 }
1983
Paul Duffin1fb487d2020-04-07 18:50:10 +01001984 // Add in scope specific arguments.
Paul Duffin0ff08bd2020-04-29 13:30:54 +01001985 droidstubsArgs = append(droidstubsArgs, scopeSpecificDroidstubsArgs...)
Paul Duffin11512472019-02-11 15:55:17 +00001986 props.Arg_files = module.sdkLibraryProperties.Droiddoc_option_files
Paul Duffin6d0886e2020-04-07 18:49:53 +01001987 props.Args = proptools.StringPtr(strings.Join(droidstubsArgs, " "))
Jiyong Parkc678ad32018-04-10 13:07:10 +09001988
Paul Duffin15f34ef2020-07-20 18:04:44 +01001989 // List of APIs identified from the provided source files are created. They are later
1990 // compared against to the not-yet-released (a.k.a current) list of APIs and to the
1991 // last-released (a.k.a numbered) list of API.
1992 currentApiFileName := apiScope.apiFilePrefix + "current.txt"
1993 removedApiFileName := apiScope.apiFilePrefix + "removed.txt"
1994 apiDir := module.getApiDir()
1995 currentApiFileName = path.Join(apiDir, currentApiFileName)
1996 removedApiFileName = path.Join(apiDir, removedApiFileName)
Jiyong Parkc678ad32018-04-10 13:07:10 +09001997
Paul Duffin15f34ef2020-07-20 18:04:44 +01001998 // check against the not-yet-release API
1999 props.Check_api.Current.Api_file = proptools.StringPtr(currentApiFileName)
2000 props.Check_api.Current.Removed_api_file = proptools.StringPtr(removedApiFileName)
Jiyong Park58c518b2018-05-12 22:29:12 +09002001
Paul Duffin958806b2022-05-16 13:10:47 +00002002 if module.compareAgainstLatestApi(apiScope) {
Paul Duffin15f34ef2020-07-20 18:04:44 +01002003 // check against the latest released API
2004 latestApiFilegroupName := proptools.StringPtr(module.latestApiFilegroupName(apiScope))
Anton Hanssone87b03d2020-12-21 15:29:34 +00002005 props.Previous_api = latestApiFilegroupName
Paul Duffin15f34ef2020-07-20 18:04:44 +01002006 props.Check_api.Last_released.Api_file = latestApiFilegroupName
2007 props.Check_api.Last_released.Removed_api_file = proptools.StringPtr(
2008 module.latestRemovedApiFilegroupName(apiScope))
Jaewoong Jung1a97ee02021-03-09 13:25:02 -08002009 props.Check_api.Last_released.Baseline_file = proptools.StringPtr(
2010 module.latestIncompatibilitiesFilegroupName(apiScope))
Paul Duffin160fe412020-05-10 19:32:20 +01002011
Paul Duffin15f34ef2020-07-20 18:04:44 +01002012 if proptools.Bool(module.sdkLibraryProperties.Api_lint.Enabled) {
2013 // Enable api lint.
2014 props.Check_api.Api_lint.Enabled = proptools.BoolPtr(true)
2015 props.Check_api.Api_lint.New_since = latestApiFilegroupName
Paul Duffin160fe412020-05-10 19:32:20 +01002016
Paul Duffin15f34ef2020-07-20 18:04:44 +01002017 // If it exists then pass a lint-baseline.txt through to droidstubs.
2018 baselinePath := path.Join(apiDir, apiScope.apiFilePrefix+"lint-baseline.txt")
2019 baselinePathRelativeToRoot := path.Join(mctx.ModuleDir(), baselinePath)
2020 paths, err := mctx.GlobWithDeps(baselinePathRelativeToRoot, nil)
2021 if err != nil {
2022 mctx.ModuleErrorf("error checking for presence of %s: %s", baselinePathRelativeToRoot, err)
2023 }
2024 if len(paths) == 1 {
2025 props.Check_api.Api_lint.Baseline_file = proptools.StringPtr(baselinePath)
2026 } else if len(paths) != 0 {
2027 mctx.ModuleErrorf("error checking for presence of %s: expected one path, found: %v", baselinePathRelativeToRoot, paths)
Paul Duffin160fe412020-05-10 19:32:20 +01002028 }
2029 }
Paul Duffin15f34ef2020-07-20 18:04:44 +01002030 }
Jiyong Park58c518b2018-05-12 22:29:12 +09002031
Paul Duffin15f34ef2020-07-20 18:04:44 +01002032 if !Bool(module.sdkLibraryProperties.No_dist) {
Paul Duffin040e9062020-11-23 17:41:36 +00002033 // Dist the api txt and removed api txt artifacts for sdk builds.
2034 distDir := proptools.StringPtr(path.Join(module.apiDistPath(apiScope), "api"))
Jihoon Kang02168052024-03-20 00:44:54 +00002035 stubsTypeTagPrefix := ""
2036 if mctx.Config().ReleaseHiddenApiExportableStubs() {
2037 stubsTypeTagPrefix = ".exportable"
2038 }
Paul Duffin040e9062020-11-23 17:41:36 +00002039 for _, p := range []struct {
2040 tag string
2041 pattern string
2042 }{
Jihoon Kangd1799f62024-02-20 23:01:38 +00002043 // "exportable" api files are copied to the dist directory instead of the
Jihoon Kang02168052024-03-20 00:44:54 +00002044 // "everything" api files when "RELEASE_HIDDEN_API_EXPORTABLE_STUBS" build flag
2045 // is set. Otherwise, the "everything" api files are copied to the dist directory.
2046 {tag: "%s.api.txt", pattern: "%s.txt"},
2047 {tag: "%s.removed-api.txt", pattern: "%s-removed.txt"},
Paul Duffin040e9062020-11-23 17:41:36 +00002048 } {
2049 props.Dists = append(props.Dists, android.Dist{
2050 Targets: []string{"sdk", "win_sdk"},
2051 Dir: distDir,
2052 Dest: proptools.StringPtr(fmt.Sprintf(p.pattern, module.distStem())),
Jihoon Kang02168052024-03-20 00:44:54 +00002053 Tag: proptools.StringPtr(fmt.Sprintf(p.tag, stubsTypeTagPrefix)),
Paul Duffin040e9062020-11-23 17:41:36 +00002054 })
2055 }
Anton Hansson5fd5d242020-03-27 19:43:19 +00002056 }
2057
Spandan Das2cc80ba2023-10-27 17:21:52 +00002058 mctx.CreateModule(DroidstubsFactory, &props, module.sdkComponentPropertiesForChildLibrary()).(*Droidstubs).CallHookIfAvailable(mctx)
Jiyong Parkc678ad32018-04-10 13:07:10 +09002059}
2060
Jihoon Kang0c705a42023-08-02 06:44:57 +00002061func (module *SdkLibrary) createApiLibrary(mctx android.DefaultableHookContext, apiScope *apiScope, alternativeFullApiSurfaceStub string) {
Jihoon Kang1c92c3e2023-03-23 17:44:51 +00002062 props := struct {
Jihoon Kangca198c22023-06-22 23:13:51 +00002063 Name *string
2064 Visibility []string
2065 Api_contributions []string
2066 Libs []string
2067 Static_libs []string
2068 Full_api_surface_stub *string
Jihoon Kang4ec24872023-10-05 17:26:09 +00002069 System_modules *string
Jihoon Kang063ec002023-06-28 01:16:23 +00002070 Enable_validation *bool
Jihoon Kang5d701272024-02-15 21:53:49 +00002071 Stubs_type *string
Jihoon Kang1c92c3e2023-03-23 17:44:51 +00002072 }{}
2073
2074 props.Name = proptools.StringPtr(module.apiLibraryModuleName(apiScope))
Jihoon Kang786df932023-09-07 01:18:31 +00002075 props.Visibility = []string{"//visibility:override", "//visibility:private"}
Jihoon Kang1c92c3e2023-03-23 17:44:51 +00002076
2077 apiContributions := []string{}
2078
2079 // Api surfaces are not independent of each other, but have subset relationships,
2080 // and so does the api files. To generate from-text stubs for api surfaces other than public,
2081 // all subset api domains' api_contriubtions must be added as well.
2082 scope := apiScope
2083 for scope != nil {
2084 apiContributions = append(apiContributions, module.stubsSourceModuleName(scope)+".api.contribution")
2085 scope = scope.extends
2086 }
Jihoon Kang0c705a42023-08-02 06:44:57 +00002087 if apiScope == apiScopePublic {
2088 additionalApiContribution := module.apiLibraryAdditionalApiContribution()
2089 if additionalApiContribution != "" {
2090 apiContributions = append(apiContributions, additionalApiContribution)
2091 }
2092 }
Jihoon Kang1c92c3e2023-03-23 17:44:51 +00002093
2094 props.Api_contributions = apiContributions
2095 props.Libs = module.properties.Libs
2096 props.Libs = append(props.Libs, module.sdkLibraryProperties.Stub_only_libs...)
Mark White9421c4c2023-08-10 00:07:03 +00002097 props.Libs = append(props.Libs, module.scopeToProperties[apiScope].Libs...)
Jihoon Kang1c92c3e2023-03-23 17:44:51 +00002098 props.Libs = append(props.Libs, "stub-annotations")
2099 props.Static_libs = module.sdkLibraryProperties.Stub_only_static_libs
Jihoon Kange7ee2562023-07-25 05:51:46 +00002100 props.Full_api_surface_stub = proptools.StringPtr(apiScope.kind.DefaultJavaLibraryName())
Jihoon Kang0c705a42023-08-02 06:44:57 +00002101 if alternativeFullApiSurfaceStub != "" {
2102 props.Full_api_surface_stub = proptools.StringPtr(alternativeFullApiSurfaceStub)
2103 }
Jihoon Kang1c92c3e2023-03-23 17:44:51 +00002104
2105 // android_module_lib_stubs_current.from-text only comprises api contributions from art, conscrypt and i18n.
2106 // Thus, replace with android_module_lib_stubs_current_full.from-text, which comprises every api domains.
2107 if apiScope.kind == android.SdkModule {
Jihoon Kangca198c22023-06-22 23:13:51 +00002108 props.Full_api_surface_stub = proptools.StringPtr(apiScope.kind.DefaultJavaLibraryName() + "_full.from-text")
Jihoon Kang1c92c3e2023-03-23 17:44:51 +00002109 }
2110
Jihoon Kangd30ac8a2023-10-09 18:00:17 +00002111 // java_sdk_library modules that set sdk_version as none does not depend on other api
2112 // domains. Therefore, java_api_library created from such modules should not depend on
2113 // full_api_surface_stubs but create and compile stubs by the java_api_library module
2114 // itself.
2115 if module.SdkVersion(mctx).Kind == android.SdkNone {
2116 props.Full_api_surface_stub = nil
2117 }
2118
Jihoon Kang4ec24872023-10-05 17:26:09 +00002119 props.System_modules = module.deviceProperties.System_modules
Jihoon Kang063ec002023-06-28 01:16:23 +00002120 props.Enable_validation = proptools.BoolPtr(true)
Jihoon Kang5d701272024-02-15 21:53:49 +00002121 props.Stubs_type = proptools.StringPtr("everything")
Jihoon Kang4ec24872023-10-05 17:26:09 +00002122
Spandan Das2cc80ba2023-10-27 17:21:52 +00002123 mctx.CreateModule(ApiLibraryFactory, &props, module.sdkComponentPropertiesForChildLibrary())
Jihoon Kang1c92c3e2023-03-23 17:44:51 +00002124}
2125
Jihoon Kang02168052024-03-20 00:44:54 +00002126func (module *SdkLibrary) topLevelStubsLibraryProps(mctx android.DefaultableHookContext, apiScope *apiScope, doDist bool) libraryProperties {
Jihoon Kangfa4a90d2023-12-20 02:53:38 +00002127 props := libraryProperties{}
2128
Jihoon Kang1147b312023-06-08 23:25:57 +00002129 props.Visibility = childModuleVisibility(module.sdkLibraryProperties.Stubs_library_visibility)
2130 sdkVersion := module.sdkVersionForStubsLibrary(mctx, apiScope)
2131 props.Sdk_version = proptools.StringPtr(sdkVersion)
2132
Jihoon Kang1147b312023-06-08 23:25:57 +00002133 props.System_modules = module.deviceProperties.System_modules
2134
Jihoon Kang1147b312023-06-08 23:25:57 +00002135 // The imports need to be compiled to dex if the java_sdk_library requests it.
2136 compileDex := module.dexProperties.Compile_dex
2137 if module.stubLibrariesCompiledForDex() {
2138 compileDex = proptools.BoolPtr(true)
2139 }
2140 props.Compile_dex = compileDex
2141
Jihoon Kang02168052024-03-20 00:44:54 +00002142 if !Bool(module.sdkLibraryProperties.No_dist) && doDist {
2143 props.Dist.Targets = []string{"sdk", "win_sdk"}
2144 props.Dist.Dest = proptools.StringPtr(fmt.Sprintf("%v.jar", module.distStem()))
2145 props.Dist.Dir = proptools.StringPtr(module.apiDistPath(apiScope))
2146 props.Dist.Tag = proptools.StringPtr(".jar")
2147 }
2148
Jihoon Kangfa4a90d2023-12-20 02:53:38 +00002149 return props
2150}
2151
2152func (module *SdkLibrary) createTopLevelStubsLibrary(
2153 mctx android.DefaultableHookContext, apiScope *apiScope, contributesToApiSurface bool) {
2154
Jihoon Kang02168052024-03-20 00:44:54 +00002155 // Dist the "everything" stubs when the RELEASE_HIDDEN_API_EXPORTABLE_STUBS build flag is false
2156 doDist := !mctx.Config().ReleaseHiddenApiExportableStubs()
2157 props := module.topLevelStubsLibraryProps(mctx, apiScope, doDist)
Jihoon Kangfa4a90d2023-12-20 02:53:38 +00002158 props.Name = proptools.StringPtr(module.stubsLibraryModuleName(apiScope))
2159
2160 // Add the stub compiling java_library/java_api_library as static lib based on build config
2161 staticLib := module.sourceStubsLibraryModuleName(apiScope)
2162 if mctx.Config().BuildFromTextStub() && contributesToApiSurface {
2163 staticLib = module.apiLibraryModuleName(apiScope)
2164 }
2165 props.Static_libs = append(props.Static_libs, staticLib)
2166
2167 mctx.CreateModule(LibraryFactory, &props, module.sdkComponentPropertiesForChildLibrary())
2168}
2169
2170func (module *SdkLibrary) createTopLevelExportableStubsLibrary(
2171 mctx android.DefaultableHookContext, apiScope *apiScope) {
2172
Jihoon Kang02168052024-03-20 00:44:54 +00002173 // Dist the "exportable" stubs when the RELEASE_HIDDEN_API_EXPORTABLE_STUBS build flag is true
2174 doDist := mctx.Config().ReleaseHiddenApiExportableStubs()
2175 props := module.topLevelStubsLibraryProps(mctx, apiScope, doDist)
Jihoon Kangfa4a90d2023-12-20 02:53:38 +00002176 props.Name = proptools.StringPtr(module.exportableStubsLibraryModuleName(apiScope))
2177
Jihoon Kangfa4a90d2023-12-20 02:53:38 +00002178 staticLib := module.exportableSourceStubsLibraryModuleName(apiScope)
2179 props.Static_libs = append(props.Static_libs, staticLib)
2180
Jihoon Kang1147b312023-06-08 23:25:57 +00002181 mctx.CreateModule(LibraryFactory, &props, module.sdkComponentPropertiesForChildLibrary())
2182}
2183
Paul Duffin958806b2022-05-16 13:10:47 +00002184func (module *SdkLibrary) compareAgainstLatestApi(apiScope *apiScope) bool {
2185 return !(apiScope.unstable || module.sdkLibraryProperties.Unsafe_ignore_missing_latest_api)
2186}
2187
Paul Duffinea8f8082021-06-24 13:25:57 +01002188// Implements android.ApexModule
Jooyung Han5e9013b2020-03-10 06:23:13 +09002189func (module *SdkLibrary) DepIsInSameApex(mctx android.BaseModuleContext, dep android.Module) bool {
2190 depTag := mctx.OtherModuleDependencyTag(dep)
2191 if depTag == xmlPermissionsFileTag {
2192 return true
2193 }
Jihoon Kanga3a05462024-04-05 00:36:44 +00002194 if dep.Name() == module.implLibraryModuleName() {
2195 return true
2196 }
Jooyung Han5e9013b2020-03-10 06:23:13 +09002197 return module.Library.DepIsInSameApex(mctx, dep)
2198}
2199
Paul Duffinea8f8082021-06-24 13:25:57 +01002200// Implements android.ApexModule
2201func (module *SdkLibrary) UniqueApexVariations() bool {
2202 return module.uniqueApexVariations()
2203}
2204
Jihoon Kang80456fd2023-11-15 19:22:14 +00002205func (module *SdkLibrary) ContributeToApi() bool {
2206 return proptools.BoolDefault(module.sdkLibraryProperties.Contribute_to_android_api, false)
2207}
2208
Jiyong Parkc678ad32018-04-10 13:07:10 +09002209// Creates the xml file that publicizes the runtime library
Paul Duffinf0229202020-04-29 16:47:28 +01002210func (module *SdkLibrary) createXmlFile(mctx android.DefaultableHookContext) {
Spandan Das8c9ae7e2023-03-03 21:20:36 +00002211 moduleMinApiLevel := module.Library.MinSdkVersion(mctx)
Pedro Loureiroc3621422021-09-28 15:40:23 +00002212 var moduleMinApiLevelStr = moduleMinApiLevel.String()
2213 if moduleMinApiLevel == android.NoneApiLevel {
2214 moduleMinApiLevelStr = "current"
2215 }
Jiyong Parke3833882020-02-17 17:28:10 +09002216 props := struct {
Pedro Loureiroc3621422021-09-28 15:40:23 +00002217 Name *string
2218 Lib_name *string
2219 Apex_available []string
2220 On_bootclasspath_since *string
2221 On_bootclasspath_before *string
2222 Min_device_sdk *string
2223 Max_device_sdk *string
2224 Sdk_library_min_api_level *string
Jamie Garsidee570ace2023-11-27 12:07:36 +00002225 Uses_libs_dependencies []string
Jiyong Parke3833882020-02-17 17:28:10 +09002226 }{
Pedro Loureiroc3621422021-09-28 15:40:23 +00002227 Name: proptools.StringPtr(module.xmlPermissionsModuleName()),
2228 Lib_name: proptools.StringPtr(module.BaseModuleName()),
2229 Apex_available: module.ApexProperties.Apex_available,
2230 On_bootclasspath_since: module.commonSdkLibraryProperties.On_bootclasspath_since,
2231 On_bootclasspath_before: module.commonSdkLibraryProperties.On_bootclasspath_before,
2232 Min_device_sdk: module.commonSdkLibraryProperties.Min_device_sdk,
2233 Max_device_sdk: module.commonSdkLibraryProperties.Max_device_sdk,
2234 Sdk_library_min_api_level: &moduleMinApiLevelStr,
Jamie Garsidee570ace2023-11-27 12:07:36 +00002235 Uses_libs_dependencies: module.usesLibraryProperties.Uses_libs,
Jiyong Parkc678ad32018-04-10 13:07:10 +09002236 }
Jiyong Parke3833882020-02-17 17:28:10 +09002237
Jiyong Parke3833882020-02-17 17:28:10 +09002238 mctx.CreateModule(sdkLibraryXmlFactory, &props)
Jiyong Parkc678ad32018-04-10 13:07:10 +09002239}
2240
Jiyong Parkf1691d22021-03-29 20:11:58 +09002241func PrebuiltJars(ctx android.BaseModuleContext, baseName string, s android.SdkSpec) android.Paths {
Jiyong Park54105c42021-03-31 18:17:53 +09002242 var ver android.ApiLevel
Jiyong Parkf1691d22021-03-29 20:11:58 +09002243 var kind android.SdkKind
2244 if s.UsePrebuilt(ctx) {
Jiyong Park54105c42021-03-31 18:17:53 +09002245 ver = s.ApiLevel
Jiyong Parkf1691d22021-03-29 20:11:58 +09002246 kind = s.Kind
Jiyong Parkc678ad32018-04-10 13:07:10 +09002247 } else {
Jiyong Park6a927c42020-01-21 02:03:43 +09002248 // We don't have prebuilt SDK for the specific sdkVersion.
2249 // Instead of breaking the build, fallback to use "system_current"
Jiyong Park54105c42021-03-31 18:17:53 +09002250 ver = android.FutureApiLevel
Jiyong Parkf1691d22021-03-29 20:11:58 +09002251 kind = android.SdkSystem
Sundong Ahn054b19a2018-10-19 13:46:09 +09002252 }
Jiyong Park6a927c42020-01-21 02:03:43 +09002253
2254 dir := filepath.Join("prebuilts", "sdk", ver.String(), kind.String())
Paul Duffin50061512020-01-21 16:31:05 +00002255 jar := filepath.Join(dir, baseName+".jar")
Sundong Ahn054b19a2018-10-19 13:46:09 +09002256 jarPath := android.ExistentPathForSource(ctx, jar)
Sundong Ahnae418ac2019-02-28 15:01:28 +09002257 if !jarPath.Valid() {
Colin Cross07c88562020-01-07 09:34:44 -08002258 if ctx.Config().AllowMissingDependencies() {
2259 return android.Paths{android.PathForSource(ctx, jar)}
2260 } else {
Jiyong Parkf1691d22021-03-29 20:11:58 +09002261 ctx.PropertyErrorf("sdk_library", "invalid sdk version %q, %q does not exist", s.Raw, jar)
Colin Cross07c88562020-01-07 09:34:44 -08002262 }
Sundong Ahnae418ac2019-02-28 15:01:28 +09002263 return nil
2264 }
Sundong Ahn054b19a2018-10-19 13:46:09 +09002265 return android.Paths{jarPath.Path()}
2266}
2267
Colin Crossaede88c2020-08-11 12:17:01 -07002268// 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 +01002269//
2270// If either this or the other module are on the platform then this will return
2271// false.
Colin Cross56a83212020-09-15 18:30:11 -07002272func withinSameApexesAs(ctx android.BaseModuleContext, other android.Module) bool {
Colin Crossff694a82023-12-13 15:54:49 -08002273 apexInfo, _ := android.ModuleProvider(ctx, android.ApexInfoProvider)
Colin Cross313aa542023-12-13 13:47:44 -08002274 otherApexInfo, _ := android.OtherModuleProvider(ctx, other, android.ApexInfoProvider)
Jiyong Parkab50b072021-05-12 17:13:56 +09002275 return len(otherApexInfo.InApexVariants) > 0 && reflect.DeepEqual(apexInfo.InApexVariants, otherApexInfo.InApexVariants)
Paul Duffin9b879592020-05-26 13:21:35 +01002276}
2277
Jihoon Kang8479dea2024-04-04 01:19:05 +00002278func (module *SdkLibrary) sdkJars(ctx android.BaseModuleContext, sdkVersion android.SdkSpec) android.Paths {
Jiyong Park932cdfe2020-05-28 00:19:53 +09002279 // If the client doesn't set sdk_version, but if this library prefers stubs over
2280 // the impl library, let's provide the widest API surface possible. To do so,
2281 // force override sdk_version to module_current so that the closest possible API
2282 // surface could be found in selectHeaderJarsForSdkVersion
Jiyong Parkf1691d22021-03-29 20:11:58 +09002283 if module.defaultsToStubs() && !sdkVersion.Specified() {
Jiyong Park92315372021-04-02 08:45:46 +09002284 sdkVersion = android.SdkSpecFrom(ctx, "module_current")
Jiyong Park932cdfe2020-05-28 00:19:53 +09002285 }
Paul Duffind1b3a922020-01-22 11:57:20 +00002286
Paul Duffindaaa3322020-05-26 18:13:57 +01002287 // Only provide access to the implementation library if it is actually built.
2288 if module.requiresRuntimeImplementationLibrary() {
2289 // Check any special cases for java_sdk_library.
2290 //
2291 // Only allow access to the implementation library in the following condition:
2292 // * No sdk_version specified on the referencing module.
Paul Duffin9b879592020-05-26 13:21:35 +01002293 // * The referencing module is in the same apex as this.
Jiyong Parkf1691d22021-03-29 20:11:58 +09002294 if sdkVersion.Kind == android.SdkPrivate || withinSameApexesAs(ctx, module) {
Jihoon Kang8479dea2024-04-04 01:19:05 +00002295 return module.implLibraryHeaderJars
Sundong Ahn054b19a2018-10-19 13:46:09 +09002296 }
Jiyong Parkc678ad32018-04-10 13:07:10 +09002297 }
Paul Duffinb05d4292020-05-20 12:19:10 +01002298
Paul Duffin23970f42020-05-20 14:20:02 +01002299 return module.selectHeaderJarsForSdkVersion(ctx, sdkVersion)
Jiyong Parkc678ad32018-04-10 13:07:10 +09002300}
2301
Sundong Ahn241cd372018-07-13 16:16:44 +09002302// to satisfy SdkLibraryDependency interface
Jiyong Parkf1691d22021-03-29 20:11:58 +09002303func (module *SdkLibrary) SdkHeaderJars(ctx android.BaseModuleContext, sdkVersion android.SdkSpec) android.Paths {
Jihoon Kang8479dea2024-04-04 01:19:05 +00002304 return module.sdkJars(ctx, sdkVersion)
Sundong Ahn241cd372018-07-13 16:16:44 +09002305}
2306
Colin Cross571cccf2019-02-04 11:22:08 -08002307var javaSdkLibrariesKey = android.NewOnceKey("javaSdkLibraries")
2308
Jiyong Park82484c02018-04-23 21:41:26 +09002309func javaSdkLibraries(config android.Config) *[]string {
Colin Cross571cccf2019-02-04 11:22:08 -08002310 return config.Once(javaSdkLibrariesKey, func() interface{} {
Jiyong Park82484c02018-04-23 21:41:26 +09002311 return &[]string{}
2312 }).(*[]string)
2313}
2314
Paul Duffin749f98f2019-12-30 17:23:46 +00002315func (module *SdkLibrary) getApiDir() string {
2316 return proptools.StringDefault(module.sdkLibraryProperties.Api_dir, "api")
2317}
2318
Jiyong Parkc678ad32018-04-10 13:07:10 +09002319// For a java_sdk_library module, create internal modules for stubs, docs,
2320// runtime libs and xml file. If requested, the stubs and docs are created twice
2321// once for public API level and once for system API level
Paul Duffinf0229202020-04-29 16:47:28 +01002322func (module *SdkLibrary) CreateInternalModules(mctx android.DefaultableHookContext) {
2323 // If the module has been disabled then don't create any child modules.
Cole Fausta963b942024-04-11 17:43:00 -07002324 if !module.Enabled(mctx) {
Paul Duffinf0229202020-04-29 16:47:28 +01002325 return
2326 }
2327
Paul Duffina18abc22020-05-16 18:54:24 +01002328 if len(module.properties.Srcs) == 0 {
Inseob Kimc0907f12019-02-08 21:00:45 +09002329 mctx.PropertyErrorf("srcs", "java_sdk_library must specify srcs")
Jooyung Han58f26ab2019-12-18 15:34:32 +09002330 return
Inseob Kimc0907f12019-02-08 21:00:45 +09002331 }
2332
Paul Duffin37e0b772019-12-30 17:20:10 +00002333 // If this builds against standard libraries (i.e. is not part of the core libraries)
Paul Duffin4f5c1ef2020-11-19 14:53:43 +00002334 // then assume it provides both system and test apis.
Jiyong Parkf1691d22021-03-29 20:11:58 +09002335 sdkDep := decodeSdkDep(mctx, android.SdkContext(&module.Library))
Paul Duffin37e0b772019-12-30 17:20:10 +00002336 hasSystemAndTestApis := sdkDep.hasStandardLibs()
Paul Duffin3375e352020-04-28 10:44:03 +01002337 module.sdkLibraryProperties.Generate_system_and_test_apis = hasSystemAndTestApis
Paul Duffin4f5c1ef2020-11-19 14:53:43 +00002338
Jaewoong Jung18aefc12020-12-21 09:11:10 -08002339 missingCurrentApi := false
Inseob Kim8098faa2019-03-18 10:19:51 +09002340
Paul Duffin3375e352020-04-28 10:44:03 +01002341 generatedScopes := module.getGeneratedApiScopes(mctx)
Paul Duffind1b3a922020-01-22 11:57:20 +00002342
Paul Duffin749f98f2019-12-30 17:23:46 +00002343 apiDir := module.getApiDir()
Paul Duffin3375e352020-04-28 10:44:03 +01002344 for _, scope := range generatedScopes {
Inseob Kim8098faa2019-03-18 10:19:51 +09002345 for _, api := range []string{"current.txt", "removed.txt"} {
Paul Duffind1b3a922020-01-22 11:57:20 +00002346 path := path.Join(mctx.ModuleDir(), apiDir, scope.apiFilePrefix+api)
Inseob Kim8098faa2019-03-18 10:19:51 +09002347 p := android.ExistentPathForSource(mctx, path)
2348 if !p.Valid() {
Colin Cross18f840c2021-05-20 17:56:54 -07002349 if mctx.Config().AllowMissingDependencies() {
2350 mctx.AddMissingDependencies([]string{path})
2351 } else {
2352 mctx.ModuleErrorf("Current api file %#v doesn't exist", path)
2353 missingCurrentApi = true
2354 }
Inseob Kim8098faa2019-03-18 10:19:51 +09002355 }
2356 }
2357 }
2358
Jaewoong Jung18aefc12020-12-21 09:11:10 -08002359 if missingCurrentApi {
Inseob Kim8098faa2019-03-18 10:19:51 +09002360 script := "build/soong/scripts/gen-java-current-api-files.sh"
2361 p := android.ExistentPathForSource(mctx, script)
2362
2363 if !p.Valid() {
2364 panic(fmt.Sprintf("script file %s doesn't exist", script))
2365 }
2366
2367 mctx.ModuleErrorf("One or more current api files are missing. "+
2368 "You can update them by:\n"+
Paul Duffin37e0b772019-12-30 17:20:10 +00002369 "%s %q %s && m update-api",
Paul Duffind1b3a922020-01-22 11:57:20 +00002370 script, filepath.Join(mctx.ModuleDir(), apiDir),
Paul Duffin3375e352020-04-28 10:44:03 +01002371 strings.Join(generatedScopes.Strings(func(s *apiScope) string { return s.apiFilePrefix }), " "))
Inseob Kim8098faa2019-03-18 10:19:51 +09002372 return
2373 }
2374
Paul Duffin3375e352020-04-28 10:44:03 +01002375 for _, scope := range generatedScopes {
Paul Duffin15f34ef2020-07-20 18:04:44 +01002376 // Use the stubs source name for legacy reasons.
2377 module.createStubsSourcesAndApi(mctx, scope, module.stubsSourceModuleName(scope), scope.droidstubsArgs)
Paul Duffin0ff08bd2020-04-29 13:30:54 +01002378
Paul Duffind1b3a922020-01-22 11:57:20 +00002379 module.createStubsLibrary(mctx, scope)
Jihoon Kangfa4a90d2023-12-20 02:53:38 +00002380 module.createExportableStubsLibrary(mctx, scope)
Jihoon Kang1c92c3e2023-03-23 17:44:51 +00002381
Jihoon Kang0c705a42023-08-02 06:44:57 +00002382 alternativeFullApiSurfaceStubLib := ""
2383 if scope == apiScopePublic {
2384 alternativeFullApiSurfaceStubLib = module.alternativeFullApiSurfaceStubLib()
2385 }
2386 contributesToApiSurface := module.contributesToApiSurface(mctx.Config()) || alternativeFullApiSurfaceStubLib != ""
Jihoon Kang1147b312023-06-08 23:25:57 +00002387 if contributesToApiSurface {
Jihoon Kang0c705a42023-08-02 06:44:57 +00002388 module.createApiLibrary(mctx, scope, alternativeFullApiSurfaceStubLib)
Jihoon Kang1c92c3e2023-03-23 17:44:51 +00002389 }
Jihoon Kang1147b312023-06-08 23:25:57 +00002390
2391 module.createTopLevelStubsLibrary(mctx, scope, contributesToApiSurface)
Jihoon Kangfa4a90d2023-12-20 02:53:38 +00002392 module.createTopLevelExportableStubsLibrary(mctx, scope)
Inseob Kimc0907f12019-02-08 21:00:45 +09002393 }
2394
Paul Duffindfa131e2020-05-15 20:37:11 +01002395 if module.requiresRuntimeImplementationLibrary() {
Paul Duffin5df79302020-05-16 15:52:12 +01002396 // Create child module to create an implementation library.
2397 //
2398 // This temporarily creates a second implementation library that can be explicitly
2399 // referenced.
2400 //
2401 // TODO(b/156618935) - update comment once only one implementation library is created.
2402 module.createImplLibrary(mctx)
2403
Paul Duffindfa131e2020-05-15 20:37:11 +01002404 // Only create an XML permissions file that declares the library as being usable
2405 // as a shared library if required.
2406 if module.sharedLibrary() {
2407 module.createXmlFile(mctx)
2408 }
Paul Duffin43db9be2019-12-30 17:35:49 +00002409
2410 // record java_sdk_library modules so that they are exported to make
2411 javaSdkLibraries := javaSdkLibraries(mctx.Config())
2412 javaSdkLibrariesLock.Lock()
2413 defer javaSdkLibrariesLock.Unlock()
2414 *javaSdkLibraries = append(*javaSdkLibraries, module.BaseModuleName())
2415 }
Anton Hansson7f66efa2020-10-08 14:47:23 +01002416
Paul Duffin77590a82022-04-28 14:13:30 +00002417 // 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 +01002418 module.properties.Libs = append(module.properties.Libs, module.sdkLibraryProperties.Impl_only_libs...)
Paul Duffin77590a82022-04-28 14:13:30 +00002419 module.properties.Static_libs = append(module.properties.Static_libs, module.sdkLibraryProperties.Impl_only_static_libs...)
Inseob Kimc0907f12019-02-08 21:00:45 +09002420}
2421
2422func (module *SdkLibrary) InitSdkLibraryProperties() {
Colin Crossce6734e2020-06-15 16:09:53 -07002423 module.addHostAndDeviceProperties()
2424 module.AddProperties(&module.sdkLibraryProperties)
Sundong Ahn054b19a2018-10-19 13:46:09 +09002425
Paul Duffin71b33cc2021-06-23 11:39:47 +01002426 module.initSdkLibraryComponent(module)
Paul Duffin859fe962020-05-15 10:20:31 +01002427
Paul Duffina18abc22020-05-16 18:54:24 +01002428 module.properties.Installable = proptools.BoolPtr(true)
2429 module.deviceProperties.IsSDKLibrary = true
Inseob Kimc0907f12019-02-08 21:00:45 +09002430}
Sundong Ahn054b19a2018-10-19 13:46:09 +09002431
Paul Duffindfa131e2020-05-15 20:37:11 +01002432func (module *SdkLibrary) requiresRuntimeImplementationLibrary() bool {
2433 return !proptools.Bool(module.sdkLibraryProperties.Api_only)
2434}
2435
Jiyong Park932cdfe2020-05-28 00:19:53 +09002436func (module *SdkLibrary) defaultsToStubs() bool {
2437 return proptools.Bool(module.sdkLibraryProperties.Default_to_stubs)
2438}
2439
Paul Duffin1b1e8062020-05-08 13:44:43 +01002440// Defines how to name the individual component modules the sdk library creates.
2441type sdkLibraryComponentNamingScheme interface {
2442 stubsLibraryModuleName(scope *apiScope, baseName string) string
2443
2444 stubsSourceModuleName(scope *apiScope, baseName string) string
Jihoon Kang1c92c3e2023-03-23 17:44:51 +00002445
2446 apiLibraryModuleName(scope *apiScope, baseName string) string
Jihoon Kang1147b312023-06-08 23:25:57 +00002447
Jihoon Kangfa4a90d2023-12-20 02:53:38 +00002448 sourceStubsLibraryModuleName(scope *apiScope, baseName string) string
2449
2450 exportableStubsLibraryModuleName(scope *apiScope, baseName string) string
2451
2452 exportableSourceStubsLibraryModuleName(scope *apiScope, baseName string) string
Paul Duffin1b1e8062020-05-08 13:44:43 +01002453}
2454
2455type defaultNamingScheme struct {
2456}
2457
2458func (s *defaultNamingScheme) stubsLibraryModuleName(scope *apiScope, baseName string) string {
2459 return scope.stubsLibraryModuleName(baseName)
2460}
2461
2462func (s *defaultNamingScheme) stubsSourceModuleName(scope *apiScope, baseName string) string {
2463 return scope.stubsSourceModuleName(baseName)
2464}
2465
Jihoon Kang1c92c3e2023-03-23 17:44:51 +00002466func (s *defaultNamingScheme) apiLibraryModuleName(scope *apiScope, baseName string) string {
2467 return scope.apiLibraryModuleName(baseName)
2468}
2469
Jihoon Kangfa4a90d2023-12-20 02:53:38 +00002470func (s *defaultNamingScheme) sourceStubsLibraryModuleName(scope *apiScope, baseName string) string {
Jihoon Kang1147b312023-06-08 23:25:57 +00002471 return scope.sourceStubLibraryModuleName(baseName)
2472}
2473
Jihoon Kangfa4a90d2023-12-20 02:53:38 +00002474func (s *defaultNamingScheme) exportableStubsLibraryModuleName(scope *apiScope, baseName string) string {
2475 return scope.exportableStubsLibraryModuleName(baseName)
2476}
2477
2478func (s *defaultNamingScheme) exportableSourceStubsLibraryModuleName(scope *apiScope, baseName string) string {
2479 return scope.exportableSourceStubsLibraryModuleName(baseName)
2480}
2481
Paul Duffin1b1e8062020-05-08 13:44:43 +01002482var _ sdkLibraryComponentNamingScheme = (*defaultNamingScheme)(nil)
2483
Jihoon Kangfa4a90d2023-12-20 02:53:38 +00002484func hasStubsLibrarySuffix(name string, apiScope *apiScope) bool {
2485 return strings.HasSuffix(name, apiScope.stubsLibraryModuleNameSuffix()) ||
2486 strings.HasSuffix(name, apiScope.exportableStubsLibraryModuleNameSuffix())
2487}
2488
Jaewoong Jungbc15e3a2021-03-10 17:02:43 -08002489func moduleStubLinkType(name string) (stub bool, ret sdkLinkType) {
Jihoon Kang1147b312023-06-08 23:25:57 +00002490 name = strings.TrimSuffix(name, ".from-source")
2491
Anton Hansson2d0c1942020-05-25 12:20:51 +01002492 // This suffix-based approach is fragile and could potentially mis-trigger.
2493 // TODO(b/155164730): Clean this up when modules no longer reference sdk_lib stubs directly.
Jihoon Kangfa4a90d2023-12-20 02:53:38 +00002494 if hasStubsLibrarySuffix(name, apiScopePublic) {
Anton Hansson08f476b2021-04-07 15:32:19 +01002495 if name == "hwbinder.stubs" || name == "libcore_private.stubs" {
2496 // Due to a previous bug, these modules were not considered stubs, so we retain that.
2497 return false, javaPlatform
2498 }
Anton Hansson2d0c1942020-05-25 12:20:51 +01002499 return true, javaSdk
2500 }
Jihoon Kangfa4a90d2023-12-20 02:53:38 +00002501 if hasStubsLibrarySuffix(name, apiScopeSystem) {
Anton Hansson2d0c1942020-05-25 12:20:51 +01002502 return true, javaSystem
2503 }
Jihoon Kangfa4a90d2023-12-20 02:53:38 +00002504 if hasStubsLibrarySuffix(name, apiScopeModuleLib) {
Anton Hansson2d0c1942020-05-25 12:20:51 +01002505 return true, javaModule
2506 }
Jihoon Kangfa4a90d2023-12-20 02:53:38 +00002507 if hasStubsLibrarySuffix(name, apiScopeTest) {
Anton Hansson2d0c1942020-05-25 12:20:51 +01002508 return true, javaSystem
2509 }
Jihoon Kangfa4a90d2023-12-20 02:53:38 +00002510 if hasStubsLibrarySuffix(name, apiScopeSystemServer) {
Jihoon Kang1147b312023-06-08 23:25:57 +00002511 return true, javaSystemServer
2512 }
Anton Hansson2d0c1942020-05-25 12:20:51 +01002513 return false, javaPlatform
2514}
2515
Jaewoong Jung4f158ee2019-07-11 10:05:35 -07002516// java_sdk_library is a special Java library that provides optional platform APIs to apps.
2517// In practice, it can be viewed as a combination of several modules: 1) stubs library that clients
2518// are linked against to, 2) droiddoc module that internally generates API stubs source files,
2519// 3) the real runtime shared library that implements the APIs, and 4) XML file for adding
2520// the runtime lib to the classpath at runtime if requested via <uses-library>.
Inseob Kimc0907f12019-02-08 21:00:45 +09002521func SdkLibraryFactory() android.Module {
2522 module := &SdkLibrary{}
Paul Duffinc3091c82020-05-08 14:16:20 +01002523
2524 // Initialize information common between source and prebuilt.
Paul Duffin71b33cc2021-06-23 11:39:47 +01002525 module.initCommon(module)
Paul Duffinc3091c82020-05-08 14:16:20 +01002526
Inseob Kimc0907f12019-02-08 21:00:45 +09002527 module.InitSdkLibraryProperties()
Jooyung Han58f26ab2019-12-18 15:34:32 +09002528 android.InitApexModule(module)
Sundong Ahn054b19a2018-10-19 13:46:09 +09002529 InitJavaModule(module, android.HostAndDeviceSupported)
Paul Duffin3375e352020-04-28 10:44:03 +01002530
2531 // Initialize the map from scope to scope specific properties.
2532 scopeToProperties := make(map[*apiScope]*ApiScopeProperties)
Jihoon Kang98aa8fa2024-06-07 11:06:57 +00002533 for _, scope := range AllApiScopes {
Paul Duffin3375e352020-04-28 10:44:03 +01002534 scopeToProperties[scope] = scope.scopeSpecificProperties(module)
2535 }
2536 module.scopeToProperties = scopeToProperties
2537
Paul Duffin4911a892020-04-29 23:35:13 +01002538 // Add the properties containing visibility rules so that they are checked.
Paul Duffin5df79302020-05-16 15:52:12 +01002539 android.AddVisibilityProperty(module, "impl_library_visibility", &module.sdkLibraryProperties.Impl_library_visibility)
Paul Duffin4911a892020-04-29 23:35:13 +01002540 android.AddVisibilityProperty(module, "stubs_library_visibility", &module.sdkLibraryProperties.Stubs_library_visibility)
2541 android.AddVisibilityProperty(module, "stubs_source_visibility", &module.sdkLibraryProperties.Stubs_source_visibility)
2542
Paul Duffin1b1e8062020-05-08 13:44:43 +01002543 module.SetDefaultableHook(func(ctx android.DefaultableHookContext) {
Paul Duffindfa131e2020-05-15 20:37:11 +01002544 // If no implementation is required then it cannot be used as a shared library
2545 // either.
2546 if !module.requiresRuntimeImplementationLibrary() {
2547 // If shared_library has been explicitly set to true then it is incompatible
2548 // with api_only: true.
2549 if proptools.Bool(module.commonSdkLibraryProperties.Shared_library) {
2550 ctx.PropertyErrorf("api_only/shared_library", "inconsistent settings, shared_library and api_only cannot both be true")
2551 }
2552 // Set shared_library: false.
2553 module.commonSdkLibraryProperties.Shared_library = proptools.BoolPtr(false)
2554 }
2555
Paul Duffin1b1e8062020-05-08 13:44:43 +01002556 if module.initCommonAfterDefaultsApplied(ctx) {
2557 module.CreateInternalModules(ctx)
2558 }
2559 })
Jiyong Parkc678ad32018-04-10 13:07:10 +09002560 return module
2561}
Colin Cross79c7c262019-04-17 11:11:46 -07002562
2563//
2564// SDK library prebuilts
2565//
2566
Paul Duffin56d44902020-01-31 13:36:25 +00002567// Properties associated with each api scope.
2568type sdkLibraryScopeProperties struct {
Colin Cross79c7c262019-04-17 11:11:46 -07002569 Jars []string `android:"path"`
2570
2571 Sdk_version *string
2572
Colin Cross79c7c262019-04-17 11:11:46 -07002573 // List of shared java libs that this module has dependencies to
2574 Libs []string
Paul Duffin3d1248c2020-04-09 00:10:17 +01002575
Paul Duffinc8782502020-04-29 20:45:27 +01002576 // The stubs source.
Paul Duffin3d1248c2020-04-09 00:10:17 +01002577 Stub_srcs []string `android:"path"`
Paul Duffin1fd005d2020-04-09 01:08:11 +01002578
2579 // The current.txt
Paul Duffin0f8faff2020-05-20 16:18:00 +01002580 Current_api *string `android:"path"`
Paul Duffin1fd005d2020-04-09 01:08:11 +01002581
2582 // The removed.txt
Paul Duffin0f8faff2020-05-20 16:18:00 +01002583 Removed_api *string `android:"path"`
Anton Hanssond78eb762021-09-21 15:25:12 +01002584
2585 // Annotation zip
2586 Annotations *string `android:"path"`
Colin Cross79c7c262019-04-17 11:11:46 -07002587}
2588
Paul Duffin56d44902020-01-31 13:36:25 +00002589type sdkLibraryImportProperties struct {
Paul Duffinfcfd7912020-01-31 17:54:30 +00002590 // List of shared java libs, common to all scopes, that this module has
2591 // dependencies to
2592 Libs []string
Paul Duffin1267d872021-04-16 17:21:36 +01002593
2594 // If set to true, compile dex files for the stubs. Defaults to false.
2595 Compile_dex *bool
Paul Duffin869de142021-07-15 14:14:41 +01002596
2597 // If not empty, classes are restricted to the specified packages and their sub-packages.
Paul Duffin869de142021-07-15 14:14:41 +01002598 Permitted_packages []string
Spandan Das23956d12024-01-19 00:22:22 +00002599
2600 // Name of the source soong module that gets shadowed by this prebuilt
2601 // If unspecified, follows the naming convention that the source module of
2602 // the prebuilt is Name() without "prebuilt_" prefix
2603 Source_module_name *string
Paul Duffin56d44902020-01-31 13:36:25 +00002604}
2605
Paul Duffineedc5d52020-06-12 17:46:39 +01002606type SdkLibraryImport struct {
Colin Cross79c7c262019-04-17 11:11:46 -07002607 android.ModuleBase
2608 android.DefaultableModuleBase
2609 prebuilt android.Prebuilt
Paul Duffindd46f712020-02-10 13:37:10 +00002610 android.ApexModuleBase
Colin Cross79c7c262019-04-17 11:11:46 -07002611
Paul Duffin37856732021-02-26 14:24:15 +00002612 hiddenAPI
Jiakai Zhang204356f2021-09-09 08:12:46 +00002613 dexpreopter
Paul Duffin37856732021-02-26 14:24:15 +00002614
Colin Cross79c7c262019-04-17 11:11:46 -07002615 properties sdkLibraryImportProperties
2616
Paul Duffin46a26a82020-04-07 19:27:04 +01002617 // Map from api scope to the scope specific property structure.
2618 scopeProperties map[*apiScope]*sdkLibraryScopeProperties
2619
Paul Duffin56d44902020-01-31 13:36:25 +00002620 commonToSdkLibraryAndImport
Paul Duffineedc5d52020-06-12 17:46:39 +01002621
Paul Duffineedc5d52020-06-12 17:46:39 +01002622 // The reference to the xml permissions module created by the source module.
2623 // Is nil if the source module does not exist.
2624 xmlPermissionsFileModule *sdkLibraryXml
Paul Duffin39853512021-02-26 11:09:39 +00002625
Jeongik Chad5fe8782021-07-08 01:13:11 +09002626 // Build path to the dex implementation jar obtained from the prebuilt_apex, if any.
Spandan Dasfae468e2023-12-12 23:23:53 +00002627 dexJarFile OptionalDexJarPath
2628 dexJarFileErr error
Jeongik Chad5fe8782021-07-08 01:13:11 +09002629
2630 // Expected install file path of the source module(sdk_library)
2631 // or dex implementation jar obtained from the prebuilt_apex, if any.
2632 installFile android.Path
Colin Cross79c7c262019-04-17 11:11:46 -07002633}
2634
Paul Duffineedc5d52020-06-12 17:46:39 +01002635var _ SdkLibraryDependency = (*SdkLibraryImport)(nil)
Colin Cross79c7c262019-04-17 11:11:46 -07002636
Paul Duffin46a26a82020-04-07 19:27:04 +01002637// The type of a structure that contains a field of type sdkLibraryScopeProperties
2638// for each apiscope in allApiScopes, e.g. something like:
Colin Crossd079e0b2022-08-16 10:27:33 -07002639//
2640// struct {
2641// Public sdkLibraryScopeProperties
2642// System sdkLibraryScopeProperties
2643// ...
2644// }
Paul Duffin46a26a82020-04-07 19:27:04 +01002645var allScopeStructType = createAllScopePropertiesStructType()
2646
2647// Dynamically create a structure type for each apiscope in allApiScopes.
2648func createAllScopePropertiesStructType() reflect.Type {
2649 var fields []reflect.StructField
Jihoon Kang98aa8fa2024-06-07 11:06:57 +00002650 for _, apiScope := range AllApiScopes {
Paul Duffin46a26a82020-04-07 19:27:04 +01002651 field := reflect.StructField{
2652 Name: apiScope.fieldName,
2653 Type: reflect.TypeOf(sdkLibraryScopeProperties{}),
2654 }
2655 fields = append(fields, field)
2656 }
2657
2658 return reflect.StructOf(fields)
2659}
2660
2661// Create an instance of the scope specific structure type and return a map
2662// from apiscope to a pointer to each scope specific field.
2663func createPropertiesInstance() (interface{}, map[*apiScope]*sdkLibraryScopeProperties) {
2664 allScopePropertiesPtr := reflect.New(allScopeStructType)
2665 allScopePropertiesStruct := allScopePropertiesPtr.Elem()
2666 scopeProperties := make(map[*apiScope]*sdkLibraryScopeProperties)
2667
Jihoon Kang98aa8fa2024-06-07 11:06:57 +00002668 for _, apiScope := range AllApiScopes {
Paul Duffin46a26a82020-04-07 19:27:04 +01002669 field := allScopePropertiesStruct.FieldByName(apiScope.fieldName)
2670 scopeProperties[apiScope] = field.Addr().Interface().(*sdkLibraryScopeProperties)
2671 }
2672
2673 return allScopePropertiesPtr.Interface(), scopeProperties
2674}
2675
Jaewoong Jung4f158ee2019-07-11 10:05:35 -07002676// java_sdk_library_import imports a prebuilt java_sdk_library.
Colin Cross79c7c262019-04-17 11:11:46 -07002677func sdkLibraryImportFactory() android.Module {
Paul Duffineedc5d52020-06-12 17:46:39 +01002678 module := &SdkLibraryImport{}
Colin Cross79c7c262019-04-17 11:11:46 -07002679
Paul Duffin46a26a82020-04-07 19:27:04 +01002680 allScopeProperties, scopeToProperties := createPropertiesInstance()
2681 module.scopeProperties = scopeToProperties
Jiakai Zhang9c4dc192023-02-09 00:09:24 +08002682 module.AddProperties(&module.properties, allScopeProperties, &module.importDexpreoptProperties)
Colin Cross79c7c262019-04-17 11:11:46 -07002683
Paul Duffinc3091c82020-05-08 14:16:20 +01002684 // Initialize information common between source and prebuilt.
Paul Duffin71b33cc2021-06-23 11:39:47 +01002685 module.initCommon(module)
Paul Duffinc3091c82020-05-08 14:16:20 +01002686
Paul Duffin0bdcb272020-02-06 15:24:57 +00002687 android.InitPrebuiltModule(module, &[]string{""})
Paul Duffindd46f712020-02-10 13:37:10 +00002688 android.InitApexModule(module)
Colin Cross79c7c262019-04-17 11:11:46 -07002689 InitJavaModule(module, android.HostAndDeviceSupported)
2690
Paul Duffin1b1e8062020-05-08 13:44:43 +01002691 module.SetDefaultableHook(func(mctx android.DefaultableHookContext) {
2692 if module.initCommonAfterDefaultsApplied(mctx) {
2693 module.createInternalModules(mctx)
2694 }
2695 })
Colin Cross79c7c262019-04-17 11:11:46 -07002696 return module
2697}
2698
Paul Duffin630b11e2021-07-15 13:35:26 +01002699var _ PermittedPackagesForUpdatableBootJars = (*SdkLibraryImport)(nil)
2700
2701func (module *SdkLibraryImport) PermittedPackagesForUpdatableBootJars() []string {
2702 return module.properties.Permitted_packages
2703}
2704
Paul Duffineedc5d52020-06-12 17:46:39 +01002705func (module *SdkLibraryImport) Prebuilt() *android.Prebuilt {
Colin Cross79c7c262019-04-17 11:11:46 -07002706 return &module.prebuilt
2707}
2708
Paul Duffineedc5d52020-06-12 17:46:39 +01002709func (module *SdkLibraryImport) Name() string {
Colin Cross79c7c262019-04-17 11:11:46 -07002710 return module.prebuilt.Name(module.ModuleBase.Name())
2711}
2712
Spandan Das23956d12024-01-19 00:22:22 +00002713func (module *SdkLibraryImport) BaseModuleName() string {
2714 return proptools.StringDefault(module.properties.Source_module_name, module.ModuleBase.Name())
2715}
2716
Paul Duffineedc5d52020-06-12 17:46:39 +01002717func (module *SdkLibraryImport) createInternalModules(mctx android.DefaultableHookContext) {
Colin Cross79c7c262019-04-17 11:11:46 -07002718
Paul Duffin50061512020-01-21 16:31:05 +00002719 // If the build is configured to use prebuilts then force this to be preferred.
Jeongik Cha816a23a2020-07-08 01:09:23 +09002720 if mctx.Config().AlwaysUsePrebuiltSdks() {
Paul Duffin50061512020-01-21 16:31:05 +00002721 module.prebuilt.ForcePrefer()
2722 }
2723
Paul Duffin46a26a82020-04-07 19:27:04 +01002724 for apiScope, scopeProperties := range module.scopeProperties {
Paul Duffin56d44902020-01-31 13:36:25 +00002725 if len(scopeProperties.Jars) == 0 {
2726 continue
2727 }
2728
Paul Duffinbbb546b2020-04-09 00:07:11 +01002729 module.createJavaImportForStubs(mctx, apiScope, scopeProperties)
Paul Duffin3d1248c2020-04-09 00:10:17 +01002730
Paul Duffin0f8faff2020-05-20 16:18:00 +01002731 if len(scopeProperties.Stub_srcs) > 0 {
2732 module.createPrebuiltStubsSources(mctx, apiScope, scopeProperties)
2733 }
Jihoon Kang71c86832023-09-13 01:01:53 +00002734
2735 if scopeProperties.Current_api != nil {
2736 module.createPrebuiltApiContribution(mctx, apiScope, scopeProperties)
2737 }
Paul Duffin56d44902020-01-31 13:36:25 +00002738 }
Colin Cross79c7c262019-04-17 11:11:46 -07002739
2740 javaSdkLibraries := javaSdkLibraries(mctx.Config())
2741 javaSdkLibrariesLock.Lock()
2742 defer javaSdkLibrariesLock.Unlock()
2743 *javaSdkLibraries = append(*javaSdkLibraries, module.BaseModuleName())
2744}
2745
Paul Duffineedc5d52020-06-12 17:46:39 +01002746func (module *SdkLibraryImport) createJavaImportForStubs(mctx android.DefaultableHookContext, apiScope *apiScope, scopeProperties *sdkLibraryScopeProperties) {
Paul Duffinbbb546b2020-04-09 00:07:11 +01002747 // Creates a java import for the jar with ".stubs" suffix
2748 props := struct {
Spandan Das23956d12024-01-19 00:22:22 +00002749 Name *string
2750 Source_module_name *string
2751 Created_by_java_sdk_library_name *string
2752 Sdk_version *string
2753 Libs []string
2754 Jars []string
2755 Compile_dex *bool
Jihoon Kangfe914ed2024-02-12 22:49:21 +00002756 Is_stubs_module *bool
Paul Duffinbf4de042022-09-27 12:41:52 +01002757
2758 android.UserSuppliedPrebuiltProperties
Paul Duffinbbb546b2020-04-09 00:07:11 +01002759 }{}
Paul Duffinc3091c82020-05-08 14:16:20 +01002760 props.Name = proptools.StringPtr(module.stubsLibraryModuleName(apiScope))
Spandan Das23956d12024-01-19 00:22:22 +00002761 props.Source_module_name = proptools.StringPtr(apiScope.stubsLibraryModuleName(module.BaseModuleName()))
2762 props.Created_by_java_sdk_library_name = proptools.StringPtr(module.RootLibraryName())
Paul Duffinbbb546b2020-04-09 00:07:11 +01002763 props.Sdk_version = scopeProperties.Sdk_version
2764 // Prepend any of the libs from the legacy public properties to the libs for each of the
2765 // scopes to avoid having to duplicate them in each scope.
2766 props.Libs = append(module.properties.Libs, scopeProperties.Libs...)
2767 props.Jars = scopeProperties.Jars
Paul Duffin1dbe3ca2020-05-16 09:57:59 +01002768
Paul Duffin38b57852020-05-13 16:08:09 +01002769 // The imports are preferred if the java_sdk_library_import is preferred.
Paul Duffinbf4de042022-09-27 12:41:52 +01002770 props.CopyUserSuppliedPropertiesFromPrebuilt(&module.prebuilt)
Paul Duffin859fe962020-05-15 10:20:31 +01002771
Paul Duffin1267d872021-04-16 17:21:36 +01002772 // The imports need to be compiled to dex if the java_sdk_library_import requests it.
Paul Duffinf4600f62021-05-13 22:34:45 +01002773 compileDex := module.properties.Compile_dex
2774 if module.stubLibrariesCompiledForDex() {
2775 compileDex = proptools.BoolPtr(true)
2776 }
2777 props.Compile_dex = compileDex
Jihoon Kangfe914ed2024-02-12 22:49:21 +00002778 props.Is_stubs_module = proptools.BoolPtr(true)
Paul Duffin1267d872021-04-16 17:21:36 +01002779
Paul Duffin859fe962020-05-15 10:20:31 +01002780 mctx.CreateModule(ImportFactory, &props, module.sdkComponentPropertiesForChildLibrary())
Paul Duffinbbb546b2020-04-09 00:07:11 +01002781}
2782
Paul Duffineedc5d52020-06-12 17:46:39 +01002783func (module *SdkLibraryImport) createPrebuiltStubsSources(mctx android.DefaultableHookContext, apiScope *apiScope, scopeProperties *sdkLibraryScopeProperties) {
Paul Duffin3d1248c2020-04-09 00:10:17 +01002784 props := struct {
Spandan Das23956d12024-01-19 00:22:22 +00002785 Name *string
2786 Source_module_name *string
2787 Created_by_java_sdk_library_name *string
2788 Srcs []string
Paul Duffinbf4de042022-09-27 12:41:52 +01002789
2790 android.UserSuppliedPrebuiltProperties
Paul Duffin3d1248c2020-04-09 00:10:17 +01002791 }{}
Paul Duffinc3091c82020-05-08 14:16:20 +01002792 props.Name = proptools.StringPtr(module.stubsSourceModuleName(apiScope))
Spandan Das23956d12024-01-19 00:22:22 +00002793 props.Source_module_name = proptools.StringPtr(apiScope.stubsSourceModuleName(module.BaseModuleName()))
2794 props.Created_by_java_sdk_library_name = proptools.StringPtr(module.RootLibraryName())
Paul Duffin3d1248c2020-04-09 00:10:17 +01002795 props.Srcs = scopeProperties.Stub_srcs
Paul Duffin38b57852020-05-13 16:08:09 +01002796
2797 // The stubs source is preferred if the java_sdk_library_import is preferred.
Paul Duffinbf4de042022-09-27 12:41:52 +01002798 props.CopyUserSuppliedPropertiesFromPrebuilt(&module.prebuilt)
2799
Spandan Das2cc80ba2023-10-27 17:21:52 +00002800 mctx.CreateModule(PrebuiltStubsSourcesFactory, &props, module.sdkComponentPropertiesForChildLibrary())
Paul Duffin3d1248c2020-04-09 00:10:17 +01002801}
2802
Jihoon Kang71c86832023-09-13 01:01:53 +00002803func (module *SdkLibraryImport) createPrebuiltApiContribution(mctx android.DefaultableHookContext, apiScope *apiScope, scopeProperties *sdkLibraryScopeProperties) {
2804 api_file := scopeProperties.Current_api
2805 api_surface := &apiScope.name
2806
2807 props := struct {
Spandan Das23956d12024-01-19 00:22:22 +00002808 Name *string
2809 Source_module_name *string
2810 Created_by_java_sdk_library_name *string
2811 Api_surface *string
2812 Api_file *string
2813 Visibility []string
Jihoon Kang71c86832023-09-13 01:01:53 +00002814 }{}
2815
2816 props.Name = proptools.StringPtr(module.stubsSourceModuleName(apiScope) + ".api.contribution")
Spandan Das23956d12024-01-19 00:22:22 +00002817 props.Source_module_name = proptools.StringPtr(apiScope.stubsSourceModuleName(module.BaseModuleName()) + ".api.contribution")
2818 props.Created_by_java_sdk_library_name = proptools.StringPtr(module.RootLibraryName())
Jihoon Kang71c86832023-09-13 01:01:53 +00002819 props.Api_surface = api_surface
2820 props.Api_file = api_file
2821 props.Visibility = []string{"//visibility:override", "//visibility:public"}
2822
Spandan Das2cc80ba2023-10-27 17:21:52 +00002823 mctx.CreateModule(ApiContributionImportFactory, &props, module.sdkComponentPropertiesForChildLibrary())
Jihoon Kang71c86832023-09-13 01:01:53 +00002824}
2825
Paul Duffin44f1d842020-06-26 20:17:02 +01002826// Add the dependencies on the child module in the component deps mutator so that it
2827// creates references to the prebuilt and not the source modules.
2828func (module *SdkLibraryImport) ComponentDepsMutator(ctx android.BottomUpMutatorContext) {
Paul Duffin46a26a82020-04-07 19:27:04 +01002829 for apiScope, scopeProperties := range module.scopeProperties {
Paul Duffin56d44902020-01-31 13:36:25 +00002830 if len(scopeProperties.Jars) == 0 {
2831 continue
2832 }
2833
2834 // Add dependencies to the prebuilt stubs library
Jihoon Kangb7431552024-01-22 19:40:08 +00002835 ctx.AddVariationDependencies(nil, apiScope.prebuiltStubsTag, android.PrebuiltNameFromSource(module.stubsLibraryModuleName(apiScope)))
Paul Duffin0f8faff2020-05-20 16:18:00 +01002836
2837 if len(scopeProperties.Stub_srcs) > 0 {
2838 // Add dependencies to the prebuilt stubs source library
Paul Duffin864116c2021-04-02 10:24:13 +01002839 ctx.AddVariationDependencies(nil, apiScope.stubsSourceTag, android.PrebuiltNameFromSource(module.stubsSourceModuleName(apiScope)))
Paul Duffin0f8faff2020-05-20 16:18:00 +01002840 }
Paul Duffin56d44902020-01-31 13:36:25 +00002841 }
Paul Duffin44f1d842020-06-26 20:17:02 +01002842}
2843
2844// Add other dependencies as normal.
2845func (module *SdkLibraryImport) DepsMutator(ctx android.BottomUpMutatorContext) {
Paul Duffineedc5d52020-06-12 17:46:39 +01002846
2847 implName := module.implLibraryModuleName()
2848 if ctx.OtherModuleExists(implName) {
2849 ctx.AddVariationDependencies(nil, implLibraryTag, implName)
2850
2851 xmlPermissionsModuleName := module.xmlPermissionsModuleName()
2852 if module.sharedLibrary() && ctx.OtherModuleExists(xmlPermissionsModuleName) {
2853 // Add dependency to the rule for generating the xml permissions file
2854 ctx.AddDependency(module, xmlPermissionsFileTag, xmlPermissionsModuleName)
2855 }
2856 }
Colin Cross79c7c262019-04-17 11:11:46 -07002857}
2858
Jiyong Park45bf82e2020-12-15 22:29:02 +09002859var _ android.ApexModule = (*SdkLibraryImport)(nil)
2860
2861// Implements android.ApexModule
Paul Duffineedc5d52020-06-12 17:46:39 +01002862func (module *SdkLibraryImport) DepIsInSameApex(mctx android.BaseModuleContext, dep android.Module) bool {
2863 depTag := mctx.OtherModuleDependencyTag(dep)
2864 if depTag == xmlPermissionsFileTag {
2865 return true
2866 }
2867
2868 // None of the other dependencies of the java_sdk_library_import are in the same apex
2869 // as the one that references this module.
2870 return false
2871}
2872
Jiyong Park45bf82e2020-12-15 22:29:02 +09002873// Implements android.ApexModule
Dan Albertc8060532020-07-22 22:32:17 -07002874func (module *SdkLibraryImport) ShouldSupportSdkVersion(ctx android.BaseModuleContext,
2875 sdkVersion android.ApiLevel) error {
Jooyung Han749dc692020-04-15 11:03:39 +09002876 // we don't check prebuilt modules for sdk_version
2877 return nil
2878}
2879
Paul Duffinea8f8082021-06-24 13:25:57 +01002880// Implements android.ApexModule
2881func (module *SdkLibraryImport) UniqueApexVariations() bool {
2882 return module.uniqueApexVariations()
2883}
2884
Paul Duffin09817d62022-04-28 17:45:11 +01002885// MinSdkVersion - Implements hiddenAPIModule
Spandan Das8c9ae7e2023-03-03 21:20:36 +00002886func (module *SdkLibraryImport) MinSdkVersion(ctx android.EarlyModuleContext) android.ApiLevel {
2887 return android.NoneApiLevel
Paul Duffin09817d62022-04-28 17:45:11 +01002888}
2889
2890var _ hiddenAPIModule = (*SdkLibraryImport)(nil)
2891
Paul Duffineedc5d52020-06-12 17:46:39 +01002892func (module *SdkLibraryImport) GenerateAndroidBuildActions(ctx android.ModuleContext) {
Paul Duffina2ae7e02020-09-11 11:55:00 +01002893 module.generateCommonBuildActions(ctx)
2894
Jeongik Chad5fe8782021-07-08 01:13:11 +09002895 // Assume that source module(sdk_library) is installed in /<sdk_library partition>/framework
2896 module.installFile = android.PathForModuleInstall(ctx, "framework", module.Stem()+".jar")
2897
Paul Duffin0f8faff2020-05-20 16:18:00 +01002898 // Record the paths to the prebuilt stubs library and stubs source.
Colin Cross79c7c262019-04-17 11:11:46 -07002899 ctx.VisitDirectDeps(func(to android.Module) {
2900 tag := ctx.OtherModuleDependencyTag(to)
2901
Paul Duffin0f8faff2020-05-20 16:18:00 +01002902 // Extract information from any of the scope specific dependencies.
2903 if scopeTag, ok := tag.(scopeDependencyTag); ok {
2904 apiScope := scopeTag.apiScope
2905 scopePaths := module.getScopePathsCreateIfNeeded(apiScope)
2906
2907 // Extract information from the dependency. The exact information extracted
2908 // is determined by the nature of the dependency which is determined by the tag.
2909 scopeTag.extractDepInfo(ctx, to, scopePaths)
Paul Duffineedc5d52020-06-12 17:46:39 +01002910 } else if tag == implLibraryTag {
2911 if implLibrary, ok := to.(*Library); ok {
2912 module.implLibraryModule = implLibrary
2913 } else {
2914 ctx.ModuleErrorf("implementation library must be of type *java.Library but was %T", to)
2915 }
2916 } else if tag == xmlPermissionsFileTag {
2917 if xmlPermissionsFileModule, ok := to.(*sdkLibraryXml); ok {
2918 module.xmlPermissionsFileModule = xmlPermissionsFileModule
2919 } else {
2920 ctx.ModuleErrorf("xml permissions file module must be of type *sdkLibraryXml but was %T", to)
2921 }
Colin Cross79c7c262019-04-17 11:11:46 -07002922 }
2923 })
Paul Duffin0f8faff2020-05-20 16:18:00 +01002924
2925 // Populate the scope paths with information from the properties.
2926 for apiScope, scopeProperties := range module.scopeProperties {
2927 if len(scopeProperties.Jars) == 0 {
2928 continue
2929 }
2930
2931 paths := module.getScopePathsCreateIfNeeded(apiScope)
Anton Hanssond78eb762021-09-21 15:25:12 +01002932 paths.annotationsZip = android.OptionalPathForModuleSrc(ctx, scopeProperties.Annotations)
Paul Duffin0f8faff2020-05-20 16:18:00 +01002933 paths.currentApiFilePath = android.OptionalPathForModuleSrc(ctx, scopeProperties.Current_api)
2934 paths.removedApiFilePath = android.OptionalPathForModuleSrc(ctx, scopeProperties.Removed_api)
2935 }
Paul Duffin39853512021-02-26 11:09:39 +00002936
2937 if ctx.Device() {
2938 // If this is a variant created for a prebuilt_apex then use the dex implementation jar
2939 // obtained from the associated deapexer module.
Colin Crossff694a82023-12-13 15:54:49 -08002940 ai, _ := android.ModuleProvider(ctx, android.ApexInfoProvider)
Paul Duffin39853512021-02-26 11:09:39 +00002941 if ai.ForPrebuiltApex {
Paul Duffin39853512021-02-26 11:09:39 +00002942 // Get the path of the dex implementation jar from the `deapexer` module.
Spandan Dasfae468e2023-12-12 23:23:53 +00002943 di, err := android.FindDeapexerProviderForModule(ctx)
2944 if err != nil {
2945 // An error was found, possibly due to multiple apexes in the tree that export this library
2946 // Defer the error till a client tries to call DexJarBuildPath
2947 module.dexJarFileErr = err
Spandan Das3a392012024-01-17 18:26:27 +00002948 module.initHiddenAPIError(err)
Spandan Dasfae468e2023-12-12 23:23:53 +00002949 return
Martin Stjernholm44825602021-09-17 01:44:12 +01002950 }
Spandan Das5be63332023-12-13 00:06:32 +00002951 dexJarFileApexRootRelative := ApexRootRelativePathToJavaLib(module.BaseModuleName())
Jiakai Zhang81e46812023-02-08 21:56:07 +08002952 if dexOutputPath := di.PrebuiltExportPath(dexJarFileApexRootRelative); dexOutputPath != nil {
Martin Stjernholm8be1e6d2021-09-15 03:34:04 +01002953 dexJarFile := makeDexJarPathFromPath(dexOutputPath)
2954 module.dexJarFile = dexJarFile
Jiakai Zhang204356f2021-09-09 08:12:46 +00002955 installPath := android.PathForModuleInPartitionInstall(
Jiakai Zhang81e46812023-02-08 21:56:07 +08002956 ctx, "apex", ai.ApexVariationName, dexJarFileApexRootRelative)
Jiakai Zhang204356f2021-09-09 08:12:46 +00002957 module.installFile = installPath
Martin Stjernholm8be1e6d2021-09-15 03:34:04 +01002958 module.initHiddenAPI(ctx, dexJarFile, module.findScopePaths(apiScopePublic).stubsImplPath[0], nil)
Jiakai Zhang204356f2021-09-09 08:12:46 +00002959
Spandan Dase21a8d42024-01-23 23:56:29 +00002960 module.dexpreopter.installPath = module.dexpreopter.getInstallPath(ctx, android.RemoveOptionalPrebuiltPrefix(ctx.ModuleName()), installPath)
Jiakai Zhang204356f2021-09-09 08:12:46 +00002961 module.dexpreopter.isSDKLibrary = true
Spandan Dase21a8d42024-01-23 23:56:29 +00002962 module.dexpreopter.uncompressedDex = shouldUncompressDex(ctx, android.RemoveOptionalPrebuiltPrefix(ctx.ModuleName()), &module.dexpreopter)
Jiakai Zhang81e46812023-02-08 21:56:07 +08002963
2964 if profilePath := di.PrebuiltExportPath(dexJarFileApexRootRelative + ".prof"); profilePath != nil {
2965 module.dexpreopter.inputProfilePathOnHost = profilePath
2966 }
Paul Duffin39853512021-02-26 11:09:39 +00002967 } else {
2968 // This should never happen as a variant for a prebuilt_apex is only created if the
2969 // prebuilt_apex has been configured to export the java library dex file.
Martin Stjernholm44825602021-09-17 01:44:12 +01002970 ctx.ModuleErrorf("internal error: no dex implementation jar available from prebuilt APEX %s", di.ApexModuleName())
Paul Duffin39853512021-02-26 11:09:39 +00002971 }
2972 }
2973 }
mrziwang9f7b9f42024-07-10 12:18:06 -07002974
2975 module.setOutputFiles(ctx)
2976 if module.implLibraryModule != nil {
2977 setOutputFiles(ctx, module.implLibraryModule.Module)
2978 }
Colin Cross79c7c262019-04-17 11:11:46 -07002979}
2980
Jiyong Parkf1691d22021-03-29 20:11:58 +09002981func (module *SdkLibraryImport) sdkJars(ctx android.BaseModuleContext, sdkVersion android.SdkSpec, headerJars bool) android.Paths {
Paul Duffineedc5d52020-06-12 17:46:39 +01002982
2983 // For consistency with SdkLibrary make the implementation jar available to libraries that
2984 // are within the same APEX.
2985 implLibraryModule := module.implLibraryModule
Colin Cross56a83212020-09-15 18:30:11 -07002986 if implLibraryModule != nil && withinSameApexesAs(ctx, module) {
Paul Duffineedc5d52020-06-12 17:46:39 +01002987 if headerJars {
2988 return implLibraryModule.HeaderJars()
2989 } else {
2990 return implLibraryModule.ImplementationJars()
2991 }
2992 }
2993
Paul Duffin23970f42020-05-20 14:20:02 +01002994 return module.selectHeaderJarsForSdkVersion(ctx, sdkVersion)
Paul Duffin56d44902020-01-31 13:36:25 +00002995}
2996
Colin Cross79c7c262019-04-17 11:11:46 -07002997// to satisfy SdkLibraryDependency interface
Jiyong Parkf1691d22021-03-29 20:11:58 +09002998func (module *SdkLibraryImport) SdkHeaderJars(ctx android.BaseModuleContext, sdkVersion android.SdkSpec) android.Paths {
Colin Cross79c7c262019-04-17 11:11:46 -07002999 // This module is just a wrapper for the prebuilt stubs.
Paul Duffineedc5d52020-06-12 17:46:39 +01003000 return module.sdkJars(ctx, sdkVersion, true)
Colin Cross79c7c262019-04-17 11:11:46 -07003001}
3002
Ulya Trafimovichdbf31662020-12-17 12:07:54 +00003003// to satisfy UsesLibraryDependency interface
Spandan Das59a4a2b2024-01-09 21:35:56 +00003004func (module *SdkLibraryImport) DexJarBuildPath(ctx android.ModuleErrorfContext) OptionalDexJarPath {
Paul Duffin39853512021-02-26 11:09:39 +00003005 // The dex implementation jar extracted from the .apex file should be used in preference to the
3006 // source.
Spandan Dasfae468e2023-12-12 23:23:53 +00003007 if module.dexJarFileErr != nil {
Spandan Das59a4a2b2024-01-09 21:35:56 +00003008 ctx.ModuleErrorf(module.dexJarFileErr.Error())
Spandan Dasfae468e2023-12-12 23:23:53 +00003009 }
Martin Stjernholm8be1e6d2021-09-15 03:34:04 +01003010 if module.dexJarFile.IsSet() {
Paul Duffin39853512021-02-26 11:09:39 +00003011 return module.dexJarFile
3012 }
Paul Duffineedc5d52020-06-12 17:46:39 +01003013 if module.implLibraryModule == nil {
Martin Stjernholm8be1e6d2021-09-15 03:34:04 +01003014 return makeUnsetDexJarPath()
Paul Duffineedc5d52020-06-12 17:46:39 +01003015 } else {
Spandan Das59a4a2b2024-01-09 21:35:56 +00003016 return module.implLibraryModule.DexJarBuildPath(ctx)
Paul Duffineedc5d52020-06-12 17:46:39 +01003017 }
3018}
3019
Ulya Trafimovichdbf31662020-12-17 12:07:54 +00003020// to satisfy UsesLibraryDependency interface
Ulya Trafimovich31e444e2020-08-14 17:32:16 +01003021func (module *SdkLibraryImport) DexJarInstallPath() android.Path {
Jeongik Chad5fe8782021-07-08 01:13:11 +09003022 return module.installFile
Ulya Trafimovich31e444e2020-08-14 17:32:16 +01003023}
3024
Ulya Trafimovichdbf31662020-12-17 12:07:54 +00003025// to satisfy UsesLibraryDependency interface
3026func (module *SdkLibraryImport) ClassLoaderContexts() dexpreopt.ClassLoaderContextMap {
3027 return nil
3028}
3029
Paul Duffineedc5d52020-06-12 17:46:39 +01003030// to satisfy apex.javaDependency interface
3031func (module *SdkLibraryImport) JacocoReportClassesFile() android.Path {
3032 if module.implLibraryModule == nil {
3033 return nil
3034 } else {
3035 return module.implLibraryModule.JacocoReportClassesFile()
3036 }
3037}
3038
3039// to satisfy apex.javaDependency interface
Colin Cross08dca382020-07-21 20:31:17 -07003040func (module *SdkLibraryImport) LintDepSets() LintDepSets {
3041 if module.implLibraryModule == nil {
3042 return LintDepSets{}
3043 } else {
3044 return module.implLibraryModule.LintDepSets()
3045 }
3046}
3047
Spandan Das17854f52022-01-14 21:19:14 +00003048func (module *SdkLibraryImport) GetStrictUpdatabilityLinting() bool {
Jaewoong Jung476b9d62021-05-10 15:30:00 -07003049 if module.implLibraryModule == nil {
3050 return false
3051 } else {
Spandan Das17854f52022-01-14 21:19:14 +00003052 return module.implLibraryModule.GetStrictUpdatabilityLinting()
Jaewoong Jung476b9d62021-05-10 15:30:00 -07003053 }
3054}
3055
Spandan Das17854f52022-01-14 21:19:14 +00003056func (module *SdkLibraryImport) SetStrictUpdatabilityLinting(strictLinting bool) {
Jaewoong Jung476b9d62021-05-10 15:30:00 -07003057 if module.implLibraryModule != nil {
Spandan Das17854f52022-01-14 21:19:14 +00003058 module.implLibraryModule.SetStrictUpdatabilityLinting(strictLinting)
Jaewoong Jung476b9d62021-05-10 15:30:00 -07003059 }
3060}
3061
Colin Cross08dca382020-07-21 20:31:17 -07003062// to satisfy apex.javaDependency interface
Paul Duffineedc5d52020-06-12 17:46:39 +01003063func (module *SdkLibraryImport) Stem() string {
3064 return module.BaseModuleName()
Colin Cross79c7c262019-04-17 11:11:46 -07003065}
Jiyong Parke3833882020-02-17 17:28:10 +09003066
Paul Duffin44b481b2020-06-17 16:59:43 +01003067var _ ApexDependency = (*SdkLibraryImport)(nil)
3068
3069// to satisfy java.ApexDependency interface
3070func (module *SdkLibraryImport) HeaderJars() android.Paths {
3071 if module.implLibraryModule == nil {
3072 return nil
3073 } else {
3074 return module.implLibraryModule.HeaderJars()
3075 }
3076}
3077
3078// to satisfy java.ApexDependency interface
3079func (module *SdkLibraryImport) ImplementationAndResourcesJars() android.Paths {
3080 if module.implLibraryModule == nil {
3081 return nil
3082 } else {
3083 return module.implLibraryModule.ImplementationAndResourcesJars()
3084 }
3085}
3086
Jiakai Zhang204356f2021-09-09 08:12:46 +00003087// to satisfy java.DexpreopterInterface interface
3088func (module *SdkLibraryImport) IsInstallable() bool {
3089 return true
3090}
3091
Paul Duffinfef55002021-06-17 14:56:05 +01003092var _ android.RequiredFilesFromPrebuiltApex = (*SdkLibraryImport)(nil)
3093
Paul Duffinb4bbf2c2021-06-17 15:59:07 +01003094func (module *SdkLibraryImport) RequiredFilesFromPrebuiltApex(ctx android.BaseModuleContext) []string {
Paul Duffinfef55002021-06-17 14:56:05 +01003095 name := module.BaseModuleName()
Jiakai Zhang81e46812023-02-08 21:56:07 +08003096 return requiredFilesFromPrebuiltApexForImport(name, &module.dexpreopter)
Paul Duffinfef55002021-06-17 14:56:05 +01003097}
3098
Spandan Das2ea84dd2024-01-25 22:12:50 +00003099func (j *SdkLibraryImport) UseProfileGuidedDexpreopt() bool {
3100 return proptools.Bool(j.importDexpreoptProperties.Dex_preopt.Profile_guided)
3101}
3102
Jiyong Parke3833882020-02-17 17:28:10 +09003103// java_sdk_library_xml
Jiyong Parke3833882020-02-17 17:28:10 +09003104type sdkLibraryXml struct {
3105 android.ModuleBase
3106 android.DefaultableModuleBase
3107 android.ApexModuleBase
3108
3109 properties sdkLibraryXmlProperties
3110
3111 outputFilePath android.OutputPath
3112 installDirPath android.InstallPath
Colin Cross56a83212020-09-15 18:30:11 -07003113
3114 hideApexVariantFromMake bool
Jiyong Parke3833882020-02-17 17:28:10 +09003115}
3116
3117type sdkLibraryXmlProperties struct {
3118 // canonical name of the lib
3119 Lib_name *string
Pedro Loureiro9956e5e2021-09-07 17:21:59 +00003120
3121 // Signals that this shared library is part of the bootclasspath starting
3122 // on the version indicated in this attribute.
3123 //
3124 // This will make platforms at this level and above to ignore
3125 // <uses-library> tags with this library name because the library is already
3126 // available
3127 On_bootclasspath_since *string
3128
3129 // Signals that this shared library was part of the bootclasspath before
3130 // (but not including) the version indicated in this attribute.
3131 //
3132 // The system will automatically add a <uses-library> tag with this library to
3133 // apps that target any SDK less than the version indicated in this attribute.
3134 On_bootclasspath_before *string
3135
3136 // Indicates that PackageManager should ignore this shared library if the
3137 // platform is below the version indicated in this attribute.
3138 //
3139 // This means that the device won't recognise this library as installed.
3140 Min_device_sdk *string
3141
3142 // Indicates that PackageManager should ignore this shared library if the
3143 // platform is above the version indicated in this attribute.
3144 //
3145 // This means that the device won't recognise this library as installed.
3146 Max_device_sdk *string
Pedro Loureiroc3621422021-09-28 15:40:23 +00003147
3148 // The SdkLibrary's min api level as a string
3149 //
3150 // This value comes from the ApiLevel of the MinSdkVersion property.
3151 Sdk_library_min_api_level *string
Jamie Garsidee570ace2023-11-27 12:07:36 +00003152
3153 // Uses-libs dependencies that the shared library requires to work correctly.
3154 //
3155 // This will add dependency="foo:bar" to the <library> section.
3156 Uses_libs_dependencies []string
Jiyong Parke3833882020-02-17 17:28:10 +09003157}
3158
3159// java_sdk_library_xml builds the permission xml file for a java_sdk_library.
3160// Not to be used directly by users. java_sdk_library internally uses this.
3161func sdkLibraryXmlFactory() android.Module {
3162 module := &sdkLibraryXml{}
3163
3164 module.AddProperties(&module.properties)
3165
3166 android.InitApexModule(module)
3167 android.InitAndroidArchModule(module, android.DeviceSupported, android.MultilibCommon)
3168
3169 return module
3170}
3171
Colin Crossaede88c2020-08-11 12:17:01 -07003172func (module *sdkLibraryXml) UniqueApexVariations() bool {
3173 // sdkLibraryXml needs a unique variation per APEX because the generated XML file contains the path to the
3174 // mounted APEX, which contains the name of the APEX.
3175 return true
3176}
3177
Jiyong Parke3833882020-02-17 17:28:10 +09003178// from android.PrebuiltEtcModule
Jooyung Han0703fd82020-08-26 22:11:53 +09003179func (module *sdkLibraryXml) BaseDir() string {
3180 return "etc"
3181}
3182
3183// from android.PrebuiltEtcModule
Jiyong Parke3833882020-02-17 17:28:10 +09003184func (module *sdkLibraryXml) SubDir() string {
3185 return "permissions"
3186}
3187
ThiƩbaud Weksteen00e8b312024-03-18 14:06:00 +11003188var _ etc.PrebuiltEtcModule = (*sdkLibraryXml)(nil)
3189
Jiyong Parke3833882020-02-17 17:28:10 +09003190// from android.ApexModule
3191func (module *sdkLibraryXml) AvailableFor(what string) bool {
3192 return true
3193}
3194
3195func (module *sdkLibraryXml) DepsMutator(ctx android.BottomUpMutatorContext) {
3196 // do nothing
3197}
3198
Jiyong Park45bf82e2020-12-15 22:29:02 +09003199var _ android.ApexModule = (*sdkLibraryXml)(nil)
3200
3201// Implements android.ApexModule
Dan Albertc8060532020-07-22 22:32:17 -07003202func (module *sdkLibraryXml) ShouldSupportSdkVersion(ctx android.BaseModuleContext,
3203 sdkVersion android.ApiLevel) error {
Jooyung Han749dc692020-04-15 11:03:39 +09003204 // sdkLibraryXml doesn't need to be checked separately because java_sdk_library is checked
3205 return nil
3206}
3207
Jiyong Parke3833882020-02-17 17:28:10 +09003208// File path to the runtime implementation library
Colin Cross56a83212020-09-15 18:30:11 -07003209func (module *sdkLibraryXml) implPath(ctx android.ModuleContext) string {
Jiyong Parke3833882020-02-17 17:28:10 +09003210 implName := proptools.String(module.properties.Lib_name)
Colin Crossff694a82023-12-13 15:54:49 -08003211 if apexInfo, _ := android.ModuleProvider(ctx, android.ApexInfoProvider); !apexInfo.IsForPlatform() {
Colin Crosse07f2312020-08-13 11:24:56 -07003212 // TODO(b/146468504): ApexVariationName() is only a soong module name, not apex name.
Jiyong Parke3833882020-02-17 17:28:10 +09003213 // In most cases, this works fine. But when apex_name is set or override_apex is used
3214 // this can be wrong.
Spandan Das33bbeb22024-06-18 23:28:25 +00003215 return fmt.Sprintf("/apex/%s/javalib/%s.jar", apexInfo.BaseApexName, implName)
Jiyong Parke3833882020-02-17 17:28:10 +09003216 }
3217 partition := "system"
3218 if module.SocSpecific() {
3219 partition = "vendor"
3220 } else if module.DeviceSpecific() {
3221 partition = "odm"
3222 } else if module.ProductSpecific() {
3223 partition = "product"
3224 } else if module.SystemExtSpecific() {
3225 partition = "system_ext"
3226 }
3227 return "/" + partition + "/framework/" + implName + ".jar"
3228}
3229
Pedro Loureiro9956e5e2021-09-07 17:21:59 +00003230func formattedOptionalSdkLevelAttribute(ctx android.ModuleContext, attrName string, value *string) string {
3231 if value == nil {
3232 return ""
3233 }
3234 apiLevel, err := android.ApiLevelFromUser(ctx, *value)
3235 if err != nil {
Pedro Loureiroba6682f2021-10-29 09:32:32 +00003236 // attributes in bp files have underscores but in the xml have dashes.
3237 ctx.PropertyErrorf(strings.ReplaceAll(attrName, "-", "_"), err.Error())
Pedro Loureiro9956e5e2021-09-07 17:21:59 +00003238 return ""
3239 }
Pedro Loureirob638c622021-12-22 15:28:05 +00003240 if apiLevel.IsCurrent() {
3241 // passing "current" would always mean a future release, never the current (or the current in
3242 // progress) which means some conditions would never be triggered.
3243 ctx.PropertyErrorf(strings.ReplaceAll(attrName, "-", "_"),
3244 `"current" is not an allowed value for this attribute`)
3245 return ""
3246 }
Pedro Loureiro48991222022-06-17 20:01:21 +00003247 // "safeValue" is safe because it translates finalized codenames to a string
3248 // with their SDK int.
3249 safeValue := apiLevel.String()
3250 return formattedOptionalAttribute(attrName, &safeValue)
Pedro Loureiro9956e5e2021-09-07 17:21:59 +00003251}
3252
3253// formats an attribute for the xml permissions file if the value is not null
3254// returns empty string otherwise
3255func formattedOptionalAttribute(attrName string, value *string) string {
3256 if value == nil {
3257 return ""
3258 }
Paul Duffin1816cde2024-04-10 10:58:21 +01003259 return fmt.Sprintf(" %s=\"%s\"\n", attrName, *value)
Pedro Loureiro9956e5e2021-09-07 17:21:59 +00003260}
3261
Jamie Garsidee570ace2023-11-27 12:07:36 +00003262func formattedDependenciesAttribute(dependencies []string) string {
3263 if dependencies == nil {
3264 return ""
3265 }
Paul Duffin1816cde2024-04-10 10:58:21 +01003266 return fmt.Sprintf(" dependency=\"%s\"\n", strings.Join(dependencies, ":"))
Jamie Garsidee570ace2023-11-27 12:07:36 +00003267}
3268
Pedro Loureiro9956e5e2021-09-07 17:21:59 +00003269func (module *sdkLibraryXml) permissionsContents(ctx android.ModuleContext) string {
3270 libName := proptools.String(module.properties.Lib_name)
3271 libNameAttr := formattedOptionalAttribute("name", &libName)
3272 filePath := module.implPath(ctx)
3273 filePathAttr := formattedOptionalAttribute("file", &filePath)
Pedro Loureiroba6682f2021-10-29 09:32:32 +00003274 implicitFromAttr := formattedOptionalSdkLevelAttribute(ctx, "on-bootclasspath-since", module.properties.On_bootclasspath_since)
3275 implicitUntilAttr := formattedOptionalSdkLevelAttribute(ctx, "on-bootclasspath-before", module.properties.On_bootclasspath_before)
3276 minSdkAttr := formattedOptionalSdkLevelAttribute(ctx, "min-device-sdk", module.properties.Min_device_sdk)
3277 maxSdkAttr := formattedOptionalSdkLevelAttribute(ctx, "max-device-sdk", module.properties.Max_device_sdk)
Jamie Garsidee570ace2023-11-27 12:07:36 +00003278 dependenciesAttr := formattedDependenciesAttribute(module.properties.Uses_libs_dependencies)
Pedro Loureiro196d3e62021-12-22 19:53:01 +00003279 // <library> is understood in all android versions whereas <apex-library> is only understood from API T (and ignored before that).
3280 // 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 +00003281 var libraryTag string
3282 if module.properties.Min_device_sdk != nil {
Paul Duffin1816cde2024-04-10 10:58:21 +01003283 libraryTag = " <apex-library\n"
Pedro Loureiroc3621422021-09-28 15:40:23 +00003284 } else {
Paul Duffin1816cde2024-04-10 10:58:21 +01003285 libraryTag = " <library\n"
Pedro Loureiroc3621422021-09-28 15:40:23 +00003286 }
Pedro Loureiro9956e5e2021-09-07 17:21:59 +00003287
3288 return strings.Join([]string{
Paul Duffin1816cde2024-04-10 10:58:21 +01003289 "<?xml version=\"1.0\" encoding=\"utf-8\"?>\n",
3290 "<!-- Copyright (C) 2018 The Android Open Source Project\n",
3291 "\n",
3292 " Licensed under the Apache License, Version 2.0 (the \"License\");\n",
3293 " you may not use this file except in compliance with the License.\n",
3294 " You may obtain a copy of the License at\n",
3295 "\n",
3296 " http://www.apache.org/licenses/LICENSE-2.0\n",
3297 "\n",
3298 " Unless required by applicable law or agreed to in writing, software\n",
3299 " distributed under the License is distributed on an \"AS IS\" BASIS,\n",
3300 " WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\n",
3301 " See the License for the specific language governing permissions and\n",
3302 " limitations under the License.\n",
3303 "-->\n",
3304 "<permissions>\n",
Pedro Loureiroc3621422021-09-28 15:40:23 +00003305 libraryTag,
Pedro Loureiro9956e5e2021-09-07 17:21:59 +00003306 libNameAttr,
3307 filePathAttr,
3308 implicitFromAttr,
3309 implicitUntilAttr,
3310 minSdkAttr,
3311 maxSdkAttr,
Jamie Garsidee570ace2023-11-27 12:07:36 +00003312 dependenciesAttr,
Paul Duffin1816cde2024-04-10 10:58:21 +01003313 " />\n",
3314 "</permissions>\n",
3315 }, "")
Pedro Loureiro9956e5e2021-09-07 17:21:59 +00003316}
3317
Jiyong Parke3833882020-02-17 17:28:10 +09003318func (module *sdkLibraryXml) GenerateAndroidBuildActions(ctx android.ModuleContext) {
Colin Crossff694a82023-12-13 15:54:49 -08003319 apexInfo, _ := android.ModuleProvider(ctx, android.ApexInfoProvider)
3320 module.hideApexVariantFromMake = !apexInfo.IsForPlatform()
Colin Cross56a83212020-09-15 18:30:11 -07003321
Jiyong Parke3833882020-02-17 17:28:10 +09003322 libName := proptools.String(module.properties.Lib_name)
Pedro Loureiroc3621422021-09-28 15:40:23 +00003323 module.selfValidate(ctx)
Pedro Loureiro9956e5e2021-09-07 17:21:59 +00003324 xmlContent := module.permissionsContents(ctx)
Jiyong Parke3833882020-02-17 17:28:10 +09003325
3326 module.outputFilePath = android.PathForModuleOut(ctx, libName+".xml").OutputPath
Paul Duffin1816cde2024-04-10 10:58:21 +01003327 android.WriteFileRuleVerbatim(ctx, module.outputFilePath, xmlContent)
Jiyong Parke3833882020-02-17 17:28:10 +09003328
3329 module.installDirPath = android.PathForModuleInstall(ctx, "etc", module.SubDir())
Jeongik Cha00e09912024-04-23 05:07:13 +09003330 ctx.PackageFile(module.installDirPath, libName+".xml", module.outputFilePath)
mrziwange2346b82024-06-10 15:09:45 -07003331
3332 ctx.SetOutputFiles(android.OutputPaths{module.outputFilePath}.Paths(), "")
Jiyong Parke3833882020-02-17 17:28:10 +09003333}
3334
3335func (module *sdkLibraryXml) AndroidMkEntries() []android.AndroidMkEntries {
Colin Cross56a83212020-09-15 18:30:11 -07003336 if module.hideApexVariantFromMake {
satayev8f088b02021-12-06 11:40:46 +00003337 return []android.AndroidMkEntries{{
Jiyong Parke3833882020-02-17 17:28:10 +09003338 Disabled: true,
3339 }}
3340 }
3341
satayev8f088b02021-12-06 11:40:46 +00003342 return []android.AndroidMkEntries{{
Jiyong Parke3833882020-02-17 17:28:10 +09003343 Class: "ETC",
3344 OutputFile: android.OptionalPathForPath(module.outputFilePath),
3345 ExtraEntries: []android.AndroidMkExtraEntriesFunc{
Colin Crossaa255532020-07-03 13:18:24 -07003346 func(ctx android.AndroidMkExtraEntriesContext, entries *android.AndroidMkEntries) {
Jiyong Parke3833882020-02-17 17:28:10 +09003347 entries.SetString("LOCAL_MODULE_TAGS", "optional")
Colin Crossc68db4b2021-11-11 18:59:15 -08003348 entries.SetString("LOCAL_MODULE_PATH", module.installDirPath.String())
Jiyong Parke3833882020-02-17 17:28:10 +09003349 entries.SetString("LOCAL_INSTALLED_MODULE_STEM", module.outputFilePath.Base())
3350 },
3351 },
3352 }}
3353}
Paul Duffindd46f712020-02-10 13:37:10 +00003354
Pedro Loureiroc3621422021-09-28 15:40:23 +00003355func (module *sdkLibraryXml) selfValidate(ctx android.ModuleContext) {
3356 module.validateAtLeastTAttributes(ctx)
3357 module.validateMinAndMaxDeviceSdk(ctx)
3358 module.validateMinMaxDeviceSdkAndModuleMinSdk(ctx)
3359 module.validateOnBootclasspathBeforeRequirements(ctx)
3360}
3361
3362func (module *sdkLibraryXml) validateAtLeastTAttributes(ctx android.ModuleContext) {
3363 t := android.ApiLevelOrPanic(ctx, "Tiramisu")
3364 module.attrAtLeastT(ctx, t, module.properties.Min_device_sdk, "min_device_sdk")
3365 module.attrAtLeastT(ctx, t, module.properties.Max_device_sdk, "max_device_sdk")
3366 module.attrAtLeastT(ctx, t, module.properties.On_bootclasspath_before, "on_bootclasspath_before")
3367 module.attrAtLeastT(ctx, t, module.properties.On_bootclasspath_since, "on_bootclasspath_since")
3368}
3369
3370func (module *sdkLibraryXml) attrAtLeastT(ctx android.ModuleContext, t android.ApiLevel, attr *string, attrName string) {
3371 if attr != nil {
3372 if level, err := android.ApiLevelFromUser(ctx, *attr); err == nil {
3373 // we will inform the user of invalid inputs when we try to write the
3374 // permissions xml file so we don't need to do it here
3375 if t.GreaterThan(level) {
3376 ctx.PropertyErrorf(attrName, "Attribute value needs to be at least T")
3377 }
3378 }
3379 }
3380}
3381
3382func (module *sdkLibraryXml) validateMinAndMaxDeviceSdk(ctx android.ModuleContext) {
3383 if module.properties.Min_device_sdk != nil && module.properties.Max_device_sdk != nil {
3384 min, minErr := android.ApiLevelFromUser(ctx, *module.properties.Min_device_sdk)
3385 max, maxErr := android.ApiLevelFromUser(ctx, *module.properties.Max_device_sdk)
3386 if minErr == nil && maxErr == nil {
3387 // we will inform the user of invalid inputs when we try to write the
3388 // permissions xml file so we don't need to do it here
3389 if min.GreaterThan(max) {
3390 ctx.ModuleErrorf("min_device_sdk can't be greater than max_device_sdk")
3391 }
3392 }
3393 }
3394}
3395
3396func (module *sdkLibraryXml) validateMinMaxDeviceSdkAndModuleMinSdk(ctx android.ModuleContext) {
3397 moduleMinApi := android.ApiLevelOrPanic(ctx, *module.properties.Sdk_library_min_api_level)
3398 if module.properties.Min_device_sdk != nil {
3399 api, err := android.ApiLevelFromUser(ctx, *module.properties.Min_device_sdk)
3400 if err == nil {
3401 if moduleMinApi.GreaterThan(api) {
3402 ctx.PropertyErrorf("min_device_sdk", "Can't be less than module's min sdk (%s)", moduleMinApi)
3403 }
3404 }
3405 }
3406 if module.properties.Max_device_sdk != nil {
3407 api, err := android.ApiLevelFromUser(ctx, *module.properties.Max_device_sdk)
3408 if err == nil {
3409 if moduleMinApi.GreaterThan(api) {
3410 ctx.PropertyErrorf("max_device_sdk", "Can't be less than module's min sdk (%s)", moduleMinApi)
3411 }
3412 }
3413 }
3414}
3415
3416func (module *sdkLibraryXml) validateOnBootclasspathBeforeRequirements(ctx android.ModuleContext) {
3417 moduleMinApi := android.ApiLevelOrPanic(ctx, *module.properties.Sdk_library_min_api_level)
3418 if module.properties.On_bootclasspath_before != nil {
3419 t := android.ApiLevelOrPanic(ctx, "Tiramisu")
3420 // if we use the attribute, then we need to do this validation
3421 if moduleMinApi.LessThan(t) {
3422 // if minAPi is < T, then we need to have min_device_sdk (which only accepts T+)
3423 if module.properties.Min_device_sdk == nil {
3424 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")
3425 }
3426 }
3427 }
3428}
3429
Paul Duffindd46f712020-02-10 13:37:10 +00003430type sdkLibrarySdkMemberType struct {
3431 android.SdkMemberTypeBase
3432}
3433
Paul Duffin296701e2021-07-14 10:29:36 +01003434func (s *sdkLibrarySdkMemberType) AddDependencies(ctx android.SdkDependencyContext, dependencyTag blueprint.DependencyTag, names []string) {
3435 ctx.AddVariationDependencies(nil, dependencyTag, names...)
Paul Duffindd46f712020-02-10 13:37:10 +00003436}
3437
3438func (s *sdkLibrarySdkMemberType) IsInstance(module android.Module) bool {
3439 _, ok := module.(*SdkLibrary)
3440 return ok
3441}
3442
3443func (s *sdkLibrarySdkMemberType) AddPrebuiltModule(ctx android.SdkMemberContext, member android.SdkMember) android.BpModule {
3444 return ctx.SnapshotBuilder().AddPrebuiltModule(member, "java_sdk_library_import")
3445}
3446
3447func (s *sdkLibrarySdkMemberType) CreateVariantPropertiesStruct() android.SdkMemberProperties {
3448 return &sdkLibrarySdkMemberProperties{}
3449}
3450
Paul Duffin976b0e52021-04-27 23:20:26 +01003451var javaSdkLibrarySdkMemberType = &sdkLibrarySdkMemberType{
3452 android.SdkMemberTypeBase{
3453 PropertyName: "java_sdk_libs",
3454 SupportsSdk: true,
3455 },
3456}
3457
Paul Duffindd46f712020-02-10 13:37:10 +00003458type sdkLibrarySdkMemberProperties struct {
3459 android.SdkMemberPropertiesBase
3460
Paul Duffine8409952022-09-22 16:24:46 +01003461 // Stem name for files in the sdk snapshot.
3462 //
3463 // This is used to construct the path names of various sdk library files in the sdk snapshot to
3464 // make sure that they match the finalized versions of those files in prebuilts/sdk.
3465 //
3466 // This property is marked as keep so that it will be kept in all instances of this struct, will
3467 // not be cleared but will be copied to common structs. That is needed because this field is used
3468 // to construct many file names for other parts of this struct and so it needs to be present in
3469 // all structs. If it was not marked as keep then it would be cleared in some structs and so would
3470 // be unavailable for generating file names if there were other properties that were still set.
3471 Stem string `sdk:"keep"`
3472
Paul Duffindd46f712020-02-10 13:37:10 +00003473 // Scope to per scope properties.
Paul Duffin106a3a42022-01-27 16:39:06 +00003474 Scopes map[*apiScope]*scopeProperties
Paul Duffindd46f712020-02-10 13:37:10 +00003475
Paul Duffin3d1248c2020-04-09 00:10:17 +01003476 // The Java stubs source files.
3477 Stub_srcs []string
Paul Duffinf7a64332020-05-13 16:54:55 +01003478
3479 // The naming scheme.
3480 Naming_scheme *string
Paul Duffind7eb1c22020-05-26 20:57:10 +01003481
3482 // True if the java_sdk_library_import is for a shared library, false
3483 // otherwise.
3484 Shared_library *bool
Paul Duffina2ae7e02020-09-11 11:55:00 +01003485
Paul Duffin1267d872021-04-16 17:21:36 +01003486 // True if the stub imports should produce dex jars.
3487 Compile_dex *bool
3488
Paul Duffina2ae7e02020-09-11 11:55:00 +01003489 // The paths to the doctag files to add to the prebuilt.
3490 Doctag_paths android.Paths
Paul Duffin869de142021-07-15 14:14:41 +01003491
3492 Permitted_packages []string
Pedro Loureiro9956e5e2021-09-07 17:21:59 +00003493
3494 // Signals that this shared library is part of the bootclasspath starting
3495 // on the version indicated in this attribute.
3496 //
3497 // This will make platforms at this level and above to ignore
3498 // <uses-library> tags with this library name because the library is already
3499 // available
3500 On_bootclasspath_since *string
3501
3502 // Signals that this shared library was part of the bootclasspath before
3503 // (but not including) the version indicated in this attribute.
3504 //
3505 // The system will automatically add a <uses-library> tag with this library to
3506 // apps that target any SDK less than the version indicated in this attribute.
3507 On_bootclasspath_before *string
3508
3509 // Indicates that PackageManager should ignore this shared library if the
3510 // platform is below the version indicated in this attribute.
3511 //
3512 // This means that the device won't recognise this library as installed.
3513 Min_device_sdk *string
3514
3515 // Indicates that PackageManager should ignore this shared library if the
3516 // platform is above the version indicated in this attribute.
3517 //
3518 // This means that the device won't recognise this library as installed.
3519 Max_device_sdk *string
Jiakai Zhang9c4dc192023-02-09 00:09:24 +08003520
3521 DexPreoptProfileGuided *bool `supported_build_releases:"UpsideDownCake+"`
Paul Duffindd46f712020-02-10 13:37:10 +00003522}
3523
3524type scopeProperties struct {
Paul Duffin1fd005d2020-04-09 01:08:11 +01003525 Jars android.Paths
3526 StubsSrcJar android.Path
3527 CurrentApiFile android.Path
3528 RemovedApiFile android.Path
Paul Duffine7babdb2022-02-10 13:06:54 +00003529 AnnotationsZip android.Path `supported_build_releases:"Tiramisu+"`
Paul Duffin1fd005d2020-04-09 01:08:11 +01003530 SdkVersion string
Paul Duffindd46f712020-02-10 13:37:10 +00003531}
3532
3533func (s *sdkLibrarySdkMemberProperties) PopulateFromVariant(ctx android.SdkMemberContext, variant android.Module) {
3534 sdk := variant.(*SdkLibrary)
3535
Paul Duffine8409952022-09-22 16:24:46 +01003536 // Copy the stem name for files in the sdk snapshot.
3537 s.Stem = sdk.distStem()
3538
Paul Duffin106a3a42022-01-27 16:39:06 +00003539 s.Scopes = make(map[*apiScope]*scopeProperties)
Jihoon Kang98aa8fa2024-06-07 11:06:57 +00003540 for _, apiScope := range AllApiScopes {
Paul Duffin803a9562020-05-20 11:52:25 +01003541 paths := sdk.findScopePaths(apiScope)
3542 if paths == nil {
3543 continue
3544 }
3545
Paul Duffindd46f712020-02-10 13:37:10 +00003546 jars := paths.stubsImplPath
3547 if len(jars) > 0 {
3548 properties := scopeProperties{}
3549 properties.Jars = jars
Paul Duffin780c5f42020-05-12 15:52:55 +01003550 properties.SdkVersion = sdk.sdkVersionForStubsLibrary(ctx.SdkModuleContext(), apiScope)
Paul Duffin0f8faff2020-05-20 16:18:00 +01003551 properties.StubsSrcJar = paths.stubsSrcJar.Path()
Paul Duffin10269f12020-06-19 18:39:55 +01003552 if paths.currentApiFilePath.Valid() {
3553 properties.CurrentApiFile = paths.currentApiFilePath.Path()
3554 }
3555 if paths.removedApiFilePath.Valid() {
3556 properties.RemovedApiFile = paths.removedApiFilePath.Path()
3557 }
Anton Hanssond78eb762021-09-21 15:25:12 +01003558 // The annotations zip is only available for modules that set annotations_enabled: true.
3559 if paths.annotationsZip.Valid() {
3560 properties.AnnotationsZip = paths.annotationsZip.Path()
3561 }
Paul Duffin106a3a42022-01-27 16:39:06 +00003562 s.Scopes[apiScope] = &properties
Paul Duffindd46f712020-02-10 13:37:10 +00003563 }
3564 }
3565
Paul Duffindfa131e2020-05-15 20:37:11 +01003566 s.Naming_scheme = sdk.commonSdkLibraryProperties.Naming_scheme
Paul Duffind7eb1c22020-05-26 20:57:10 +01003567 s.Shared_library = proptools.BoolPtr(sdk.sharedLibrary())
Paul Duffin1267d872021-04-16 17:21:36 +01003568 s.Compile_dex = sdk.dexProperties.Compile_dex
Paul Duffina2ae7e02020-09-11 11:55:00 +01003569 s.Doctag_paths = sdk.doctagPaths
Paul Duffin869de142021-07-15 14:14:41 +01003570 s.Permitted_packages = sdk.PermittedPackagesForUpdatableBootJars()
Pedro Loureiro9956e5e2021-09-07 17:21:59 +00003571 s.On_bootclasspath_since = sdk.commonSdkLibraryProperties.On_bootclasspath_since
3572 s.On_bootclasspath_before = sdk.commonSdkLibraryProperties.On_bootclasspath_before
3573 s.Min_device_sdk = sdk.commonSdkLibraryProperties.Min_device_sdk
3574 s.Max_device_sdk = sdk.commonSdkLibraryProperties.Max_device_sdk
Jiakai Zhang9c4dc192023-02-09 00:09:24 +08003575
Jihoon Kanga3a05462024-04-05 00:36:44 +00003576 implLibrary := sdk.getImplLibraryModule()
3577 if implLibrary != nil && implLibrary.dexpreopter.dexpreoptProperties.Dex_preopt_result.Profile_guided {
Jiakai Zhang9c4dc192023-02-09 00:09:24 +08003578 s.DexPreoptProfileGuided = proptools.BoolPtr(true)
3579 }
Paul Duffindd46f712020-02-10 13:37:10 +00003580}
3581
3582func (s *sdkLibrarySdkMemberProperties) AddToPropertySet(ctx android.SdkMemberContext, propertySet android.BpPropertySet) {
Paul Duffinf7a64332020-05-13 16:54:55 +01003583 if s.Naming_scheme != nil {
3584 propertySet.AddProperty("naming_scheme", proptools.String(s.Naming_scheme))
3585 }
Paul Duffind7eb1c22020-05-26 20:57:10 +01003586 if s.Shared_library != nil {
3587 propertySet.AddProperty("shared_library", *s.Shared_library)
3588 }
Paul Duffin1267d872021-04-16 17:21:36 +01003589 if s.Compile_dex != nil {
3590 propertySet.AddProperty("compile_dex", *s.Compile_dex)
3591 }
Paul Duffin869de142021-07-15 14:14:41 +01003592 if len(s.Permitted_packages) > 0 {
3593 propertySet.AddProperty("permitted_packages", s.Permitted_packages)
3594 }
Jiakai Zhang9c4dc192023-02-09 00:09:24 +08003595 dexPreoptSet := propertySet.AddPropertySet("dex_preopt")
3596 if s.DexPreoptProfileGuided != nil {
3597 dexPreoptSet.AddProperty("profile_guided", proptools.Bool(s.DexPreoptProfileGuided))
3598 }
Paul Duffinf7a64332020-05-13 16:54:55 +01003599
Paul Duffine8409952022-09-22 16:24:46 +01003600 stem := s.Stem
3601
Jihoon Kang98aa8fa2024-06-07 11:06:57 +00003602 for _, apiScope := range AllApiScopes {
Paul Duffindd46f712020-02-10 13:37:10 +00003603 if properties, ok := s.Scopes[apiScope]; ok {
Paul Duffin6b836ba2020-05-13 19:19:49 +01003604 scopeSet := propertySet.AddPropertySet(apiScope.propertyName)
Paul Duffindd46f712020-02-10 13:37:10 +00003605
Paul Duffin958806b2022-05-16 13:10:47 +00003606 scopeDir := apiScope.snapshotRelativeDir()
Paul Duffin3d1248c2020-04-09 00:10:17 +01003607
Paul Duffindd46f712020-02-10 13:37:10 +00003608 var jars []string
3609 for _, p := range properties.Jars {
Paul Duffine8409952022-09-22 16:24:46 +01003610 dest := filepath.Join(scopeDir, stem+"-stubs.jar")
Paul Duffindd46f712020-02-10 13:37:10 +00003611 ctx.SnapshotBuilder().CopyToSnapshot(p, dest)
3612 jars = append(jars, dest)
3613 }
3614 scopeSet.AddProperty("jars", jars)
3615
Paul Duffin22628d52021-05-12 23:13:22 +01003616 if ctx.SdkModuleContext().Config().IsEnvTrue("SOONG_SDK_SNAPSHOT_USE_SRCJAR") {
3617 // Copy the stubs source jar into the snapshot zip as is.
Paul Duffine8409952022-09-22 16:24:46 +01003618 srcJarSnapshotPath := filepath.Join(scopeDir, stem+".srcjar")
Paul Duffin22628d52021-05-12 23:13:22 +01003619 ctx.SnapshotBuilder().CopyToSnapshot(properties.StubsSrcJar, srcJarSnapshotPath)
3620 scopeSet.AddProperty("stub_srcs", []string{srcJarSnapshotPath})
3621 } else {
3622 // Merge the stubs source jar into the snapshot zip so that when it is unpacked
3623 // the source files are also unpacked.
Paul Duffine8409952022-09-22 16:24:46 +01003624 snapshotRelativeDir := filepath.Join(scopeDir, stem+"_stub_sources")
Paul Duffin22628d52021-05-12 23:13:22 +01003625 ctx.SnapshotBuilder().UnzipToSnapshot(properties.StubsSrcJar, snapshotRelativeDir)
3626 scopeSet.AddProperty("stub_srcs", []string{snapshotRelativeDir})
3627 }
Paul Duffin3d1248c2020-04-09 00:10:17 +01003628
Paul Duffin1fd005d2020-04-09 01:08:11 +01003629 if properties.CurrentApiFile != nil {
Paul Duffine8409952022-09-22 16:24:46 +01003630 currentApiSnapshotPath := apiScope.snapshotRelativeCurrentApiTxtPath(stem)
Paul Duffin1fd005d2020-04-09 01:08:11 +01003631 ctx.SnapshotBuilder().CopyToSnapshot(properties.CurrentApiFile, currentApiSnapshotPath)
3632 scopeSet.AddProperty("current_api", currentApiSnapshotPath)
3633 }
3634
3635 if properties.RemovedApiFile != nil {
Paul Duffine8409952022-09-22 16:24:46 +01003636 removedApiSnapshotPath := apiScope.snapshotRelativeRemovedApiTxtPath(stem)
Paul Duffin3dbf9fd2020-06-02 13:00:02 +01003637 ctx.SnapshotBuilder().CopyToSnapshot(properties.RemovedApiFile, removedApiSnapshotPath)
Paul Duffin1fd005d2020-04-09 01:08:11 +01003638 scopeSet.AddProperty("removed_api", removedApiSnapshotPath)
3639 }
3640
Anton Hanssond78eb762021-09-21 15:25:12 +01003641 if properties.AnnotationsZip != nil {
Paul Duffine8409952022-09-22 16:24:46 +01003642 annotationsSnapshotPath := filepath.Join(scopeDir, stem+"_annotations.zip")
Anton Hanssond78eb762021-09-21 15:25:12 +01003643 ctx.SnapshotBuilder().CopyToSnapshot(properties.AnnotationsZip, annotationsSnapshotPath)
3644 scopeSet.AddProperty("annotations", annotationsSnapshotPath)
3645 }
3646
Paul Duffindd46f712020-02-10 13:37:10 +00003647 if properties.SdkVersion != "" {
3648 scopeSet.AddProperty("sdk_version", properties.SdkVersion)
3649 }
3650 }
3651 }
3652
Paul Duffina2ae7e02020-09-11 11:55:00 +01003653 if len(s.Doctag_paths) > 0 {
3654 dests := []string{}
3655 for _, p := range s.Doctag_paths {
3656 dest := filepath.Join("doctags", p.Rel())
3657 ctx.SnapshotBuilder().CopyToSnapshot(p, dest)
3658 dests = append(dests, dest)
3659 }
3660 propertySet.AddProperty("doctag_files", dests)
3661 }
Paul Duffindd46f712020-02-10 13:37:10 +00003662}