blob: b45341ab4b057642c3ab2b5c0369db6060d96854 [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 Kangb0f4c022024-08-06 00:15:25 +0000430 apiLibraryAdditionalProperties = map[string]string{
431 "legacy.i18n.module.platform.api": "i18n.module.public.api.stubs.source.api.contribution",
432 "stable.i18n.module.platform.api": "i18n.module.public.api.stubs.source.api.contribution",
433 "conscrypt.module.platform.api": "conscrypt.module.public.api.stubs.source.api.contribution",
Jihoon Kang0c705a42023-08-02 06:44:57 +0000434 }
Jiyong Parkc678ad32018-04-10 13:07:10 +0900435)
436
Jiyong Park82484c02018-04-23 21:41:26 +0900437var (
438 javaSdkLibrariesLock sync.Mutex
439)
440
Jiyong Parkc678ad32018-04-10 13:07:10 +0900441// TODO: these are big features that are currently missing
Jiyong Park1be96912018-05-28 18:02:19 +0900442// 1) disallowing linking to the runtime shared lib
443// 2) HTML generation
Jiyong Parkc678ad32018-04-10 13:07:10 +0900444
445func init() {
Paul Duffin43dc1cc2019-12-19 11:18:54 +0000446 RegisterSdkLibraryBuildComponents(android.InitRegistrationContext)
Jiyong Parkc678ad32018-04-10 13:07:10 +0900447
Jiyong Park82484c02018-04-23 21:41:26 +0900448 android.RegisterMakeVarsProvider(pctx, func(ctx android.MakeVarsContext) {
449 javaSdkLibraries := javaSdkLibraries(ctx.Config())
450 sort.Strings(*javaSdkLibraries)
451 ctx.Strict("JAVA_SDK_LIBRARIES", strings.Join(*javaSdkLibraries, " "))
452 })
Paul Duffindd46f712020-02-10 13:37:10 +0000453
454 // Register sdk member types.
Paul Duffin976b0e52021-04-27 23:20:26 +0100455 android.RegisterSdkMemberType(javaSdkLibrarySdkMemberType)
Jiyong Parkc678ad32018-04-10 13:07:10 +0900456}
457
Paul Duffin43dc1cc2019-12-19 11:18:54 +0000458func RegisterSdkLibraryBuildComponents(ctx android.RegistrationContext) {
459 ctx.RegisterModuleType("java_sdk_library", SdkLibraryFactory)
460 ctx.RegisterModuleType("java_sdk_library_import", sdkLibraryImportFactory)
461}
462
Paul Duffin3375e352020-04-28 10:44:03 +0100463// Properties associated with each api scope.
464type ApiScopeProperties struct {
465 // Indicates whether the api surface is generated.
466 //
467 // If this is set for any scope then all scopes must explicitly specify if they
468 // are enabled. This is to prevent new usages from depending on legacy behavior.
469 //
470 // Otherwise, if this is not set for any scope then the default behavior is
471 // scope specific so please refer to the scope specific property documentation.
472 Enabled *bool
Paul Duffin87a05a32020-05-12 11:50:28 +0100473
474 // The sdk_version to use for building the stubs.
475 //
476 // If not specified then it will use an sdk_version determined as follows:
Trevor Radcliffedf8aa1f2021-11-04 14:25:39 +0000477 //
Paul Duffin87a05a32020-05-12 11:50:28 +0100478 // 1) If the sdk_version specified on the java_sdk_library is none then this
Trevor Radcliffedf8aa1f2021-11-04 14:25:39 +0000479 // will be none. This is used for java_sdk_library instances that are used
480 // to create stubs that contribute to the core_current sdk version.
481 // 2) Otherwise, it is assumed that this library extends but does not
482 // contribute directly to a specific sdk_version and so this uses the
483 // sdk_version appropriate for the api scope. e.g. public will use
484 // sdk_version: current, system will use sdk_version: system_current, etc.
Paul Duffin87a05a32020-05-12 11:50:28 +0100485 //
486 // This does not affect the sdk_version used for either generating the stubs source
487 // or the API file. They both have to use the same sdk_version as is used for
488 // compiling the implementation library.
489 Sdk_version *string
Mark White9421c4c2023-08-10 00:07:03 +0000490
491 // Extra libs used when compiling stubs for this scope.
492 Libs []string
Paul Duffin3375e352020-04-28 10:44:03 +0100493}
494
Jiyong Parkc678ad32018-04-10 13:07:10 +0900495type sdkLibraryProperties struct {
Anton Hanssonf8ea3722021-09-16 14:24:13 +0100496 // List of source files that are needed to compile the API, but are not part of runtime library.
497 Api_srcs []string `android:"arch_variant"`
498
Paul Duffin5df79302020-05-16 15:52:12 +0100499 // Visibility for impl library module. If not specified then defaults to the
500 // visibility property.
501 Impl_library_visibility []string
502
Paul Duffin4911a892020-04-29 23:35:13 +0100503 // Visibility for stubs library modules. If not specified then defaults to the
504 // visibility property.
505 Stubs_library_visibility []string
506
507 // Visibility for stubs source modules. If not specified then defaults to the
508 // visibility property.
509 Stubs_source_visibility []string
510
Anton Hansson7f66efa2020-10-08 14:47:23 +0100511 // List of Java libraries that will be in the classpath when building the implementation lib
512 Impl_only_libs []string `android:"arch_variant"`
513
Paul Duffin77590a82022-04-28 14:13:30 +0000514 // List of Java libraries that will included in the implementation lib.
515 Impl_only_static_libs []string `android:"arch_variant"`
516
Sundong Ahnf043cf62018-06-25 16:04:37 +0900517 // List of Java libraries that will be in the classpath when building stubs
518 Stub_only_libs []string `android:"arch_variant"`
519
Anton Hanssondae54cd2021-04-21 16:30:10 +0100520 // List of Java libraries that will included in stub libraries
521 Stub_only_static_libs []string `android:"arch_variant"`
522
Paul Duffin7a586d32019-12-30 17:09:34 +0000523 // list of package names that will be documented and publicized as API.
524 // This allows the API to be restricted to a subset of the source files provided.
525 // If this is unspecified then all the source files will be treated as being part
526 // of the API.
Jiyong Parkc678ad32018-04-10 13:07:10 +0900527 Api_packages []string
528
Paul Duffin749f98f2019-12-30 17:23:46 +0000529 // the relative path to the directory containing the api specification files.
530 // Defaults to "api".
531 Api_dir *string
532
Paul Duffindfa131e2020-05-15 20:37:11 +0100533 // Determines whether a runtime implementation library is built; defaults to false.
534 //
535 // 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 +0200536 // it is as if shared_library: false, was set.
Paul Duffin43db9be2019-12-30 17:35:49 +0000537 Api_only *bool
538
Paul Duffin11512472019-02-11 15:55:17 +0000539 // local files that are used within user customized droiddoc options.
540 Droiddoc_option_files []string
541
Spandan Das93e95992021-07-29 18:26:39 +0000542 // additional droiddoc options.
Paul Duffin11512472019-02-11 15:55:17 +0000543 // Available variables for substitution:
544 //
545 // $(location <label>): the path to the droiddoc_option_files with name <label>
Sundong Ahndd567f92018-07-31 17:19:11 +0900546 Droiddoc_options []string
547
Paul Duffine22c2ab2020-05-20 19:35:27 +0100548 // is set to true, Metalava will allow framework SDK to contain annotations.
549 Annotations_enabled *bool
550
Sundong Ahn054b19a2018-10-19 13:46:09 +0900551 // a list of top-level directories containing files to merge qualifier annotations
552 // (i.e. those intended to be included in the stubs written) from.
553 Merge_annotations_dirs []string
554
555 // a list of top-level directories containing Java stub files to merge show/hide annotations from.
556 Merge_inclusion_annotations_dirs []string
557
Paul Duffin4f5c1ef2020-11-19 14:53:43 +0000558 // If set to true then don't create dist rules.
559 No_dist *bool
Sundong Ahn80a87b32019-05-13 15:02:50 +0900560
Paul Duffin31310252020-11-20 21:26:20 +0000561 // The stem for the artifacts that are copied to the dist, if not specified
562 // then defaults to the base module name.
563 //
564 // For each scope the following artifacts are copied to the apistubs/<scope>
565 // directory in the dist.
566 // * stubs impl jar -> <dist-stem>.jar
567 // * API specification file -> api/<dist-stem>.txt
568 // * Removed API specification file -> api/<dist-stem>-removed.txt
569 //
570 // Also used to construct the name of the filegroup (created by prebuilt_apis)
571 // that references the latest released API and remove API specification files.
572 // * API specification filegroup -> <dist-stem>.api.<scope>.latest
573 // * Removed API specification filegroup -> <dist-stem>-removed.api.<scope>.latest
Jaewoong Jung1a97ee02021-03-09 13:25:02 -0800574 // * API incompatibilities baseline filegroup -> <dist-stem>-incompatibilities.api.<scope>.latest
Paul Duffin31310252020-11-20 21:26:20 +0000575 Dist_stem *string
576
Colin Cross986b69a2021-06-01 13:13:40 -0700577 // The subdirectory for the artifacts that are copied to the dist directory. If not specified
Colin Cross3dd66252021-06-01 14:05:09 -0700578 // then defaults to "unknown". Should be set to "android" for anything that should be published
Colin Cross986b69a2021-06-01 13:13:40 -0700579 // in the public Android SDK.
580 Dist_group *string
581
Anton Hanssondff2c782020-12-21 17:10:01 +0000582 // A compatibility mode that allows historical API-tracking files to not exist.
583 // Do not use.
584 Unsafe_ignore_missing_latest_api bool
585
Paul Duffin3375e352020-04-28 10:44:03 +0100586 // indicates whether system and test apis should be generated.
587 Generate_system_and_test_apis bool `blueprint:"mutated"`
588
589 // The properties specific to the public api scope
590 //
591 // Unless explicitly specified by using public.enabled the public api scope is
592 // enabled by default in both legacy and non-legacy mode.
593 Public ApiScopeProperties
594
595 // The properties specific to the system api scope
596 //
597 // In legacy mode the system api scope is enabled by default when sdk_version
598 // is set to something other than "none".
599 //
600 // In non-legacy mode the system api scope is disabled by default.
601 System ApiScopeProperties
602
603 // The properties specific to the test api scope
604 //
605 // In legacy mode the test api scope is enabled by default when sdk_version
606 // is set to something other than "none".
607 //
608 // In non-legacy mode the test api scope is disabled by default.
609 Test ApiScopeProperties
Paul Duffin37e0b772019-12-30 17:20:10 +0000610
Paul Duffin0c5bae52020-06-02 13:00:08 +0100611 // The properties specific to the module-lib api scope
Paul Duffin8f265b92020-04-28 14:13:56 +0100612 //
Zi Wangb2179e32023-01-31 15:53:30 -0800613 // Unless explicitly specified by using module_lib.enabled the module_lib api
614 // scope is disabled by default.
Paul Duffin8f265b92020-04-28 14:13:56 +0100615 Module_lib ApiScopeProperties
616
Paul Duffin0c5bae52020-06-02 13:00:08 +0100617 // The properties specific to the system-server api scope
618 //
Zi Wangb2179e32023-01-31 15:53:30 -0800619 // Unless explicitly specified by using system_server.enabled the
620 // system_server api scope is disabled by default.
Paul Duffin0c5bae52020-06-02 13:00:08 +0100621 System_server ApiScopeProperties
622
Jiyong Park932cdfe2020-05-28 00:19:53 +0900623 // Determines if the stubs are preferred over the implementation library
624 // for linking, even when the client doesn't specify sdk_version. When this
625 // is set to true, such clients are provided with the widest API surface that
626 // this lib provides. Note however that this option doesn't affect the clients
627 // that are in the same APEX as this library. In that case, the clients are
628 // always linked with the implementation library. Default is false.
629 Default_to_stubs *bool
630
Paul Duffin160fe412020-05-10 19:32:20 +0100631 // Properties related to api linting.
632 Api_lint struct {
633 // Enable api linting.
634 Enabled *bool
Anton Hanssonfd1c0d22023-11-02 15:18:09 +0000635
636 // If API lint is enabled, this flag controls whether a set of legitimate lint errors
637 // are turned off. The default is true.
638 Legacy_errors_allowed *bool
Paul Duffin160fe412020-05-10 19:32:20 +0100639 }
640
Jihoon Kang6592e872023-12-19 01:13:16 +0000641 // a list of aconfig_declarations module names that the stubs generated in this module
642 // depend on.
643 Aconfig_declarations []string
644
Jihoon Kang48e2ac92024-07-29 21:18:46 +0000645 // Determines if the module generates the stubs from the api signature files
646 // instead of the source Java files. Defaults to true.
647 Build_from_text_stub *bool
648
Jiyong Parkc678ad32018-04-10 13:07:10 +0900649 // TODO: determines whether to create HTML doc or not
Paul Duffine8409952022-09-22 16:24:46 +0100650 // Html_doc *bool
Jiyong Parkc678ad32018-04-10 13:07:10 +0900651}
652
Paul Duffin0f8faff2020-05-20 16:18:00 +0100653// Paths to outputs from java_sdk_library and java_sdk_library_import.
654//
655// Fields that are android.Paths are always set (during GenerateAndroidBuildActions).
656// OptionalPaths are always set by java_sdk_library but may not be set by
657// java_sdk_library_import as not all instances provide that information.
Paul Duffind1b3a922020-01-22 11:57:20 +0000658type scopePaths struct {
Paul Duffin0f8faff2020-05-20 16:18:00 +0100659 // The path (represented as Paths for convenience when returning) to the stubs header jar.
660 //
661 // That is the jar that is created by turbine.
662 stubsHeaderPath android.Paths
663
664 // The path (represented as Paths for convenience when returning) to the stubs implementation jar.
665 //
666 // This is not the implementation jar, it still only contains stubs.
667 stubsImplPath android.Paths
668
Paul Duffin1267d872021-04-16 17:21:36 +0100669 // The dex jar for the stubs.
670 //
671 // This is not the implementation jar, it still only contains stubs.
Martin Stjernholm8be1e6d2021-09-15 03:34:04 +0100672 stubsDexJarPath OptionalDexJarPath
Paul Duffin1267d872021-04-16 17:21:36 +0100673
Jihoon Kangbd093452023-12-26 19:08:01 +0000674 // The exportable dex jar for the stubs.
675 // This is not the implementation jar, it still only contains stubs.
676 // Includes unflagged apis and flagged apis enabled by release configurations.
677 exportableStubsDexJarPath OptionalDexJarPath
678
Paul Duffin0f8faff2020-05-20 16:18:00 +0100679 // The API specification file, e.g. system_current.txt.
680 currentApiFilePath android.OptionalPath
681
682 // The specification of API elements removed since the last release.
683 removedApiFilePath android.OptionalPath
684
685 // The stubs source jar.
686 stubsSrcJar android.OptionalPath
Anton Hanssond78eb762021-09-21 15:25:12 +0100687
688 // Extracted annotations.
689 annotationsZip android.OptionalPath
Paul Duffin958806b2022-05-16 13:10:47 +0000690
691 // The path to the latest API file.
Jihoon Kang5623e542024-01-31 23:27:26 +0000692 latestApiPaths android.Paths
Paul Duffin958806b2022-05-16 13:10:47 +0000693
694 // The path to the latest removed API file.
Jihoon Kang5623e542024-01-31 23:27:26 +0000695 latestRemovedApiPaths android.Paths
Paul Duffind1b3a922020-01-22 11:57:20 +0000696}
697
Colin Crossdcf71b22021-02-01 13:59:03 -0800698func (paths *scopePaths) extractStubsLibraryInfoFromDependency(ctx android.ModuleContext, dep android.Module) error {
Colin Cross313aa542023-12-13 13:47:44 -0800699 if lib, ok := android.OtherModuleProvider(ctx, dep, JavaInfoProvider); ok {
Colin Crossdcf71b22021-02-01 13:59:03 -0800700 paths.stubsHeaderPath = lib.HeaderJars
701 paths.stubsImplPath = lib.ImplementationJars
Paul Duffin1267d872021-04-16 17:21:36 +0100702
703 libDep := dep.(UsesLibraryDependency)
Spandan Das59a4a2b2024-01-09 21:35:56 +0000704 paths.stubsDexJarPath = libDep.DexJarBuildPath(ctx)
Jihoon Kangbd093452023-12-26 19:08:01 +0000705 paths.exportableStubsDexJarPath = libDep.DexJarBuildPath(ctx)
706 return nil
707 } else {
708 return fmt.Errorf("expected module that has JavaInfoProvider, e.g. java_library")
709 }
710}
711
712func (paths *scopePaths) extractEverythingStubsLibraryInfoFromDependency(ctx android.ModuleContext, dep android.Module) error {
713 if lib, ok := android.OtherModuleProvider(ctx, dep, JavaInfoProvider); ok {
714 paths.stubsHeaderPath = lib.HeaderJars
Jihoon Kangf55a5f72024-01-08 08:56:20 +0000715 if !ctx.Config().ReleaseHiddenApiExportableStubs() {
716 paths.stubsImplPath = lib.ImplementationJars
717 }
Jihoon Kangbd093452023-12-26 19:08:01 +0000718
719 libDep := dep.(UsesLibraryDependency)
720 paths.stubsDexJarPath = 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) extractExportableStubsLibraryInfoFromDependency(ctx android.ModuleContext, dep android.Module) error {
Jihoon Kangf55a5f72024-01-08 08:56:20 +0000728 if lib, ok := android.OtherModuleProvider(ctx, dep, JavaInfoProvider); ok {
729 if ctx.Config().ReleaseHiddenApiExportableStubs() {
730 paths.stubsImplPath = lib.ImplementationJars
731 }
732
Jihoon Kangbd093452023-12-26 19:08:01 +0000733 libDep := dep.(UsesLibraryDependency)
734 paths.exportableStubsDexJarPath = libDep.DexJarBuildPath(ctx)
Paul Duffinc8782502020-04-29 20:45:27 +0100735 return nil
736 } else {
Colin Crossdcf71b22021-02-01 13:59:03 -0800737 return fmt.Errorf("expected module that has JavaInfoProvider, e.g. java_library")
Paul Duffinc8782502020-04-29 20:45:27 +0100738 }
739}
740
Jihoon Kangee113282024-01-23 00:16:41 +0000741func (paths *scopePaths) treatDepAsApiStubsProvider(dep android.Module, action func(provider ApiStubsProvider) error) error {
Paul Duffin0ff08bd2020-04-29 13:30:54 +0100742 if apiStubsProvider, ok := dep.(ApiStubsProvider); ok {
Jihoon Kangee113282024-01-23 00:16:41 +0000743 err := action(apiStubsProvider)
744 if err != nil {
745 return err
746 }
Jihoon Kangf55a5f72024-01-08 08:56:20 +0000747 return nil
748 } else {
749 return fmt.Errorf("expected module that implements ExportableApiStubsSrcProvider, e.g. droidstubs")
750 }
751}
752
Jihoon Kangee113282024-01-23 00:16:41 +0000753func (paths *scopePaths) treatDepAsApiStubsSrcProvider(dep android.Module, action func(provider ApiStubsSrcProvider) error) error {
Paul Duffin0f8faff2020-05-20 16:18:00 +0100754 if apiStubsProvider, ok := dep.(ApiStubsSrcProvider); ok {
Jihoon Kangee113282024-01-23 00:16:41 +0000755 err := action(apiStubsProvider)
756 if err != nil {
757 return err
758 }
Paul Duffin0f8faff2020-05-20 16:18:00 +0100759 return nil
760 } else {
761 return fmt.Errorf("expected module that implements ApiStubsSrcProvider, e.g. droidstubs")
762 }
763}
764
Jihoon Kangee113282024-01-23 00:16:41 +0000765func (paths *scopePaths) extractApiInfoFromApiStubsProvider(provider ApiStubsProvider, stubsType StubsType) error {
766 var annotationsZip, currentApiFilePath, removedApiFilePath android.Path
767 annotationsZip, annotationsZipErr := provider.AnnotationsZip(stubsType)
768 currentApiFilePath, currentApiFilePathErr := provider.ApiFilePath(stubsType)
769 removedApiFilePath, removedApiFilePathErr := provider.RemovedApiFilePath(stubsType)
Paul Duffin0ff08bd2020-04-29 13:30:54 +0100770
Jihoon Kangee113282024-01-23 00:16:41 +0000771 combinedError := errors.Join(annotationsZipErr, currentApiFilePathErr, removedApiFilePathErr)
772
773 if combinedError == nil {
774 paths.annotationsZip = android.OptionalPathForPath(annotationsZip)
775 paths.currentApiFilePath = android.OptionalPathForPath(currentApiFilePath)
776 paths.removedApiFilePath = android.OptionalPathForPath(removedApiFilePath)
777 }
778 return combinedError
Jihoon Kangf55a5f72024-01-08 08:56:20 +0000779}
780
Jihoon Kangee113282024-01-23 00:16:41 +0000781func (paths *scopePaths) extractStubsSourceInfoFromApiStubsProviders(provider ApiStubsSrcProvider, stubsType StubsType) error {
782 stubsSrcJar, err := provider.StubsSrcJar(stubsType)
783 if err == nil {
784 paths.stubsSrcJar = android.OptionalPathForPath(stubsSrcJar)
785 }
786 return err
Jihoon Kangf55a5f72024-01-08 08:56:20 +0000787}
788
Colin Crossdcf71b22021-02-01 13:59:03 -0800789func (paths *scopePaths) extractStubsSourceInfoFromDep(ctx android.ModuleContext, dep android.Module) error {
Jihoon Kang2a26b132024-06-24 07:39:40 +0000790 stubsType := Everything
791 if ctx.Config().ReleaseHiddenApiExportableStubs() {
792 stubsType = Exportable
793 }
Jihoon Kangee113282024-01-23 00:16:41 +0000794 return paths.treatDepAsApiStubsSrcProvider(dep, func(provider ApiStubsSrcProvider) error {
Jihoon Kang2a26b132024-06-24 07:39:40 +0000795 return paths.extractStubsSourceInfoFromApiStubsProviders(provider, stubsType)
Paul Duffin0ff08bd2020-04-29 13:30:54 +0100796 })
797}
798
Colin Crossdcf71b22021-02-01 13:59:03 -0800799func (paths *scopePaths) extractStubsSourceAndApiInfoFromApiStubsProvider(ctx android.ModuleContext, dep android.Module) error {
Jihoon Kang2a26b132024-06-24 07:39:40 +0000800 stubsType := Everything
Jihoon Kangf55a5f72024-01-08 08:56:20 +0000801 if ctx.Config().ReleaseHiddenApiExportableStubs() {
Jihoon Kang2a26b132024-06-24 07:39:40 +0000802 stubsType = Exportable
Jihoon Kangf55a5f72024-01-08 08:56:20 +0000803 }
Jihoon Kangee113282024-01-23 00:16:41 +0000804 return paths.treatDepAsApiStubsProvider(dep, func(provider ApiStubsProvider) error {
Jihoon Kang2a26b132024-06-24 07:39:40 +0000805 extractApiInfoErr := paths.extractApiInfoFromApiStubsProvider(provider, stubsType)
806 extractStubsSourceInfoErr := paths.extractStubsSourceInfoFromApiStubsProviders(provider, stubsType)
Jihoon Kangee113282024-01-23 00:16:41 +0000807 return errors.Join(extractApiInfoErr, extractStubsSourceInfoErr)
Paul Duffin0ff08bd2020-04-29 13:30:54 +0100808 })
809}
810
Jihoon Kang5623e542024-01-31 23:27:26 +0000811func extractOutputPaths(dep android.Module) (android.Paths, error) {
Paul Duffin958806b2022-05-16 13:10:47 +0000812 var paths android.Paths
813 if sourceFileProducer, ok := dep.(android.SourceFileProducer); ok {
814 paths = sourceFileProducer.Srcs()
Jihoon Kang5623e542024-01-31 23:27:26 +0000815 return paths, nil
Paul Duffin958806b2022-05-16 13:10:47 +0000816 } else {
Jihoon Kang5623e542024-01-31 23:27:26 +0000817 return nil, fmt.Errorf("module %q does not produce source files", dep)
Paul Duffin958806b2022-05-16 13:10:47 +0000818 }
Paul Duffin958806b2022-05-16 13:10:47 +0000819}
820
821func (paths *scopePaths) extractLatestApiPath(ctx android.ModuleContext, dep android.Module) error {
Jihoon Kang5623e542024-01-31 23:27:26 +0000822 outputPaths, err := extractOutputPaths(dep)
823 paths.latestApiPaths = outputPaths
Paul Duffin958806b2022-05-16 13:10:47 +0000824 return err
825}
826
827func (paths *scopePaths) extractLatestRemovedApiPath(ctx android.ModuleContext, dep android.Module) error {
Jihoon Kang5623e542024-01-31 23:27:26 +0000828 outputPaths, err := extractOutputPaths(dep)
829 paths.latestRemovedApiPaths = outputPaths
Paul Duffin958806b2022-05-16 13:10:47 +0000830 return err
831}
832
Paul Duffin0ff08bd2020-04-29 13:30:54 +0100833type commonToSdkLibraryAndImportProperties struct {
Paul Duffin1b1e8062020-05-08 13:44:43 +0100834 // The naming scheme to use for the components that this module creates.
835 //
Paul Duffinee9ad5d2020-09-11 13:04:05 +0100836 // If not specified then it defaults to "default".
Paul Duffin1b1e8062020-05-08 13:44:43 +0100837 //
838 // This is a temporary mechanism to simplify conversion from separate modules for each
839 // component that follow a different naming pattern to the default one.
840 //
841 // TODO(b/155480189) - Remove once naming inconsistencies have been resolved.
Paul Duffin0ff08bd2020-04-29 13:30:54 +0100842 Naming_scheme *string
Paul Duffindfa131e2020-05-15 20:37:11 +0100843
844 // Specifies whether this module can be used as an Android shared library; defaults
845 // to true.
846 //
847 // An Android shared library is one that can be referenced in a <uses-library> element
848 // in an AndroidManifest.xml.
849 Shared_library *bool
Paul Duffina2ae7e02020-09-11 11:55:00 +0100850
851 // Files containing information about supported java doc tags.
852 Doctag_files []string `android:"path"`
Pedro Loureiro9956e5e2021-09-07 17:21:59 +0000853
854 // Signals that this shared library is part of the bootclasspath starting
855 // on the version indicated in this attribute.
856 //
857 // This will make platforms at this level and above to ignore
858 // <uses-library> tags with this library name because the library is already
859 // available
860 On_bootclasspath_since *string
861
862 // Signals that this shared library was part of the bootclasspath before
863 // (but not including) the version indicated in this attribute.
864 //
865 // The system will automatically add a <uses-library> tag with this library to
866 // apps that target any SDK less than the version indicated in this attribute.
867 On_bootclasspath_before *string
868
869 // Indicates that PackageManager should ignore this shared library if the
870 // platform is below the version indicated in this attribute.
871 //
872 // This means that the device won't recognise this library as installed.
873 Min_device_sdk *string
874
875 // Indicates that PackageManager should ignore this shared library if the
876 // platform is above the version indicated in this attribute.
877 //
878 // This means that the device won't recognise this library as installed.
879 Max_device_sdk *string
Paul Duffin0ff08bd2020-04-29 13:30:54 +0100880}
881
Paul Duffin71b33cc2021-06-23 11:39:47 +0100882// commonSdkLibraryAndImportModule defines the interface that must be provided by a module that
883// embeds the commonToSdkLibraryAndImport struct.
884type commonSdkLibraryAndImportModule interface {
Paul Duffind796f6f2022-11-23 23:06:05 +0000885 android.Module
Paul Duffin71b33cc2021-06-23 11:39:47 +0100886
Spandan Das23956d12024-01-19 00:22:22 +0000887 // Returns the name of the root java_sdk_library that creates the child stub libraries
888 // This is the `name` as it appears in Android.bp, and not the name in Soong's build graph
889 // (with the prebuilt_ prefix)
890 //
891 // e.g. in the following java_sdk_library_import
892 // java_sdk_library_import {
893 // name: "framework-foo.v1",
894 // source_module_name: "framework-foo",
895 // }
896 // the values returned by
897 // 1. Name(): prebuilt_framework-foo.v1 # unique
898 // 2. BaseModuleName(): framework-foo # the source
899 // 3. RootLibraryName: framework-foo.v1 # the undecordated `name` from Android.bp
900 RootLibraryName() string
901}
902
903func (m *SdkLibrary) RootLibraryName() string {
904 return m.BaseModuleName()
905}
906
907func (m *SdkLibraryImport) RootLibraryName() string {
908 // m.BaseModuleName refers to the source of the import
909 // use moduleBase.Name to get the name of the module as it appears in the .bp file
910 return m.ModuleBase.Name()
Paul Duffin71b33cc2021-06-23 11:39:47 +0100911}
912
Paul Duffin56d44902020-01-31 13:36:25 +0000913// Common code between sdk library and sdk library import
914type commonToSdkLibraryAndImport struct {
Paul Duffin71b33cc2021-06-23 11:39:47 +0100915 module commonSdkLibraryAndImportModule
Paul Duffinc3091c82020-05-08 14:16:20 +0100916
Paul Duffin56d44902020-01-31 13:36:25 +0000917 scopePaths map[*apiScope]*scopePaths
Paul Duffin1b1e8062020-05-08 13:44:43 +0100918
919 namingScheme sdkLibraryComponentNamingScheme
920
Paul Duffindfa131e2020-05-15 20:37:11 +0100921 commonSdkLibraryProperties commonToSdkLibraryAndImportProperties
Paul Duffin859fe962020-05-15 10:20:31 +0100922
Paul Duffina2ae7e02020-09-11 11:55:00 +0100923 // Paths to commonSdkLibraryProperties.Doctag_files
924 doctagPaths android.Paths
925
Paul Duffin859fe962020-05-15 10:20:31 +0100926 // Functionality related to this being used as a component of a java_sdk_library.
927 EmbeddableSdkLibraryComponent
Jihoon Kang8479dea2024-04-04 01:19:05 +0000928
929 // Path to the header jars of the implementation library
930 // This is non-empty only when api_only is false.
931 implLibraryHeaderJars android.Paths
Jihoon Kanga3a05462024-04-05 00:36:44 +0000932
933 // The reference to the implementation library created by the source module.
934 // Is nil if the source module does not exist.
935 implLibraryModule *Library
Paul Duffin56d44902020-01-31 13:36:25 +0000936}
937
Paul Duffin71b33cc2021-06-23 11:39:47 +0100938func (c *commonToSdkLibraryAndImport) initCommon(module commonSdkLibraryAndImportModule) {
939 c.module = module
Paul Duffin1b1e8062020-05-08 13:44:43 +0100940
Paul Duffin71b33cc2021-06-23 11:39:47 +0100941 module.AddProperties(&c.commonSdkLibraryProperties)
Paul Duffin859fe962020-05-15 10:20:31 +0100942
943 // Initialize this as an sdk library component.
Paul Duffin71b33cc2021-06-23 11:39:47 +0100944 c.initSdkLibraryComponent(module)
Paul Duffin1b1e8062020-05-08 13:44:43 +0100945}
946
947func (c *commonToSdkLibraryAndImport) initCommonAfterDefaultsApplied(ctx android.DefaultableHookContext) bool {
Paul Duffindfa131e2020-05-15 20:37:11 +0100948 schemeProperty := proptools.StringDefault(c.commonSdkLibraryProperties.Naming_scheme, "default")
Paul Duffin1b1e8062020-05-08 13:44:43 +0100949 switch schemeProperty {
950 case "default":
951 c.namingScheme = &defaultNamingScheme{}
952 default:
953 ctx.PropertyErrorf("naming_scheme", "expected 'default' but was %q", schemeProperty)
954 return false
955 }
956
Spandan Das23956d12024-01-19 00:22:22 +0000957 namePtr := proptools.StringPtr(c.module.RootLibraryName())
Paul Duffin3f0290e2021-06-30 18:25:36 +0100958 c.sdkLibraryComponentProperties.SdkLibraryName = namePtr
959
Paul Duffindfa131e2020-05-15 20:37:11 +0100960 // Only track this sdk library if this can be used as a shared library.
961 if c.sharedLibrary() {
962 // Use the name specified in the module definition as the owner.
Paul Duffin3f0290e2021-06-30 18:25:36 +0100963 c.sdkLibraryComponentProperties.SdkLibraryToImplicitlyTrack = namePtr
Paul Duffindfa131e2020-05-15 20:37:11 +0100964 }
Paul Duffin859fe962020-05-15 10:20:31 +0100965
Paul Duffin1b1e8062020-05-08 13:44:43 +0100966 return true
Paul Duffinc3091c82020-05-08 14:16:20 +0100967}
968
Paul Duffinea8f8082021-06-24 13:25:57 +0100969// uniqueApexVariations provides common implementation of the ApexModule.UniqueApexVariations
970// method.
971func (c *commonToSdkLibraryAndImport) uniqueApexVariations() bool {
972 // A java_sdk_library that is a shared library produces an XML file that makes the shared library
973 // usable from an AndroidManifest.xml's <uses-library> entry. That XML file contains the name of
974 // the APEX and so it needs a unique variation per APEX.
975 return c.sharedLibrary()
976}
977
Paul Duffina2ae7e02020-09-11 11:55:00 +0100978func (c *commonToSdkLibraryAndImport) generateCommonBuildActions(ctx android.ModuleContext) {
979 c.doctagPaths = android.PathsForModuleSrc(ctx, c.commonSdkLibraryProperties.Doctag_files)
980}
981
Jihoon Kanga3a05462024-04-05 00:36:44 +0000982func (c *commonToSdkLibraryAndImport) getImplLibraryModule() *Library {
983 return c.implLibraryModule
984}
985
Paul Duffineedc5d52020-06-12 17:46:39 +0100986// Module name of the runtime implementation library
987func (c *commonToSdkLibraryAndImport) implLibraryModuleName() string {
Spandan Das23956d12024-01-19 00:22:22 +0000988 return c.module.RootLibraryName() + ".impl"
Paul Duffineedc5d52020-06-12 17:46:39 +0100989}
990
991// Module name of the XML file for the lib
992func (c *commonToSdkLibraryAndImport) xmlPermissionsModuleName() string {
Spandan Das23956d12024-01-19 00:22:22 +0000993 return c.module.RootLibraryName() + sdkXmlFileSuffix
Paul Duffineedc5d52020-06-12 17:46:39 +0100994}
995
Paul Duffinc3091c82020-05-08 14:16:20 +0100996// Name of the java_library module that compiles the stubs source.
997func (c *commonToSdkLibraryAndImport) stubsLibraryModuleName(apiScope *apiScope) string {
Spandan Das23956d12024-01-19 00:22:22 +0000998 baseName := c.module.RootLibraryName()
Paul Duffin21787622022-11-25 12:48:20 +0000999 return c.namingScheme.stubsLibraryModuleName(apiScope, baseName)
Paul Duffinc3091c82020-05-08 14:16:20 +01001000}
1001
Jihoon Kangfa4a90d2023-12-20 02:53:38 +00001002// Name of the java_library module that compiles the exportable stubs source.
1003func (c *commonToSdkLibraryAndImport) exportableStubsLibraryModuleName(apiScope *apiScope) string {
Spandan Das23956d12024-01-19 00:22:22 +00001004 baseName := c.module.RootLibraryName()
Jihoon Kangfa4a90d2023-12-20 02:53:38 +00001005 return c.namingScheme.exportableStubsLibraryModuleName(apiScope, baseName)
1006}
1007
Paul Duffinc3091c82020-05-08 14:16:20 +01001008// Name of the droidstubs module that generates the stubs source and may also
1009// generate/check the API.
1010func (c *commonToSdkLibraryAndImport) stubsSourceModuleName(apiScope *apiScope) string {
Spandan Das23956d12024-01-19 00:22:22 +00001011 baseName := c.module.RootLibraryName()
Paul Duffin21787622022-11-25 12:48:20 +00001012 return c.namingScheme.stubsSourceModuleName(apiScope, baseName)
Paul Duffinc3091c82020-05-08 14:16:20 +01001013}
1014
Jihoon Kang1c92c3e2023-03-23 17:44:51 +00001015// Name of the java_api_library module that generates the from-text stubs source
1016// and compiles to a jar file.
1017func (c *commonToSdkLibraryAndImport) apiLibraryModuleName(apiScope *apiScope) string {
Spandan Das23956d12024-01-19 00:22:22 +00001018 baseName := c.module.RootLibraryName()
Jihoon Kang1c92c3e2023-03-23 17:44:51 +00001019 return c.namingScheme.apiLibraryModuleName(apiScope, baseName)
1020}
1021
Jihoon Kang1147b312023-06-08 23:25:57 +00001022// Name of the java_library module that compiles the stubs
1023// generated from source Java files.
Jihoon Kangfa4a90d2023-12-20 02:53:38 +00001024func (c *commonToSdkLibraryAndImport) sourceStubsLibraryModuleName(apiScope *apiScope) string {
Spandan Das23956d12024-01-19 00:22:22 +00001025 baseName := c.module.RootLibraryName()
Jihoon Kangfa4a90d2023-12-20 02:53:38 +00001026 return c.namingScheme.sourceStubsLibraryModuleName(apiScope, baseName)
1027}
1028
1029// Name of the java_library module that compiles the exportable stubs
1030// generated from source Java files.
1031func (c *commonToSdkLibraryAndImport) exportableSourceStubsLibraryModuleName(apiScope *apiScope) string {
Spandan Das23956d12024-01-19 00:22:22 +00001032 baseName := c.module.RootLibraryName()
Jihoon Kangfa4a90d2023-12-20 02:53:38 +00001033 return c.namingScheme.exportableSourceStubsLibraryModuleName(apiScope, baseName)
Jihoon Kang1147b312023-06-08 23:25:57 +00001034}
1035
Paul Duffin46dc45a2020-05-14 15:39:10 +01001036// The component names for different outputs of the java_sdk_library.
1037//
1038// They are similar to the names used for the child modules it creates
1039const (
1040 stubsSourceComponentName = "stubs.source"
1041
1042 apiTxtComponentName = "api.txt"
1043
1044 removedApiTxtComponentName = "removed-api.txt"
Anton Hanssond78eb762021-09-21 15:25:12 +01001045
1046 annotationsComponentName = "annotations.zip"
Paul Duffin46dc45a2020-05-14 15:39:10 +01001047)
1048
1049// A regular expression to match tags that reference a specific stubs component.
1050//
1051// It will only match if given a valid scope and a valid component. It is verfy strict
1052// to ensure it does not accidentally match a similar looking tag that should be processed
1053// by the embedded Library.
1054var tagSplitter = func() *regexp.Regexp {
1055 // Given a list of literal string items returns a regular expression that will
1056 // match any one of the items.
1057 choice := func(items ...string) string {
1058 return `\Q` + strings.Join(items, `\E|\Q`) + `\E`
1059 }
1060
1061 // Regular expression to match one of the scopes.
1062 scopesRegexp := choice(allScopeNames...)
1063
1064 // Regular expression to match one of the components.
Anton Hanssond78eb762021-09-21 15:25:12 +01001065 componentsRegexp := choice(stubsSourceComponentName, apiTxtComponentName, removedApiTxtComponentName, annotationsComponentName)
Paul Duffin46dc45a2020-05-14 15:39:10 +01001066
1067 // Regular expression to match any combination of one scope and one component.
1068 return regexp.MustCompile(fmt.Sprintf(`^\.(%s)\.(%s)$`, scopesRegexp, componentsRegexp))
1069}()
1070
mrziwang9f7b9f42024-07-10 12:18:06 -07001071func (module *commonToSdkLibraryAndImport) setOutputFiles(ctx android.ModuleContext) {
1072 if module.doctagPaths != nil {
1073 ctx.SetOutputFiles(module.doctagPaths, ".doctags")
1074 }
1075 for _, scopeName := range android.SortedKeys(scopeByName) {
1076 paths := module.findScopePaths(scopeByName[scopeName])
1077 if paths == nil {
1078 continue
Paul Duffin46dc45a2020-05-14 15:39:10 +01001079 }
mrziwang9f7b9f42024-07-10 12:18:06 -07001080 componentToOutput := map[string]android.OptionalPath{
1081 stubsSourceComponentName: paths.stubsSrcJar,
1082 apiTxtComponentName: paths.currentApiFilePath,
1083 removedApiTxtComponentName: paths.removedApiFilePath,
1084 annotationsComponentName: paths.annotationsZip,
1085 }
1086 for _, component := range android.SortedKeys(componentToOutput) {
1087 if componentToOutput[component].Valid() {
1088 ctx.SetOutputFiles(android.Paths{componentToOutput[component].Path()}, "."+scopeName+"."+component)
Paul Duffina2ae7e02020-09-11 11:55:00 +01001089 }
1090 }
Paul Duffin46dc45a2020-05-14 15:39:10 +01001091 }
1092}
1093
Paul Duffin803a9562020-05-20 11:52:25 +01001094func (c *commonToSdkLibraryAndImport) getScopePathsCreateIfNeeded(scope *apiScope) *scopePaths {
Paul Duffin56d44902020-01-31 13:36:25 +00001095 if c.scopePaths == nil {
1096 c.scopePaths = make(map[*apiScope]*scopePaths)
1097 }
1098 paths := c.scopePaths[scope]
1099 if paths == nil {
1100 paths = &scopePaths{}
1101 c.scopePaths[scope] = paths
1102 }
1103
1104 return paths
1105}
1106
Paul Duffin803a9562020-05-20 11:52:25 +01001107func (c *commonToSdkLibraryAndImport) findScopePaths(scope *apiScope) *scopePaths {
1108 if c.scopePaths == nil {
1109 return nil
1110 }
1111
1112 return c.scopePaths[scope]
1113}
1114
1115// If this does not support the requested api scope then find the closest available
1116// scope it does support. Returns nil if no such scope is available.
1117func (c *commonToSdkLibraryAndImport) findClosestScopePath(scope *apiScope) *scopePaths {
Paul Duffind0b9fca2022-09-30 18:11:41 +01001118 for s := scope; s != nil; s = s.canAccess {
Paul Duffin803a9562020-05-20 11:52:25 +01001119 if paths := c.findScopePaths(s); paths != nil {
1120 return paths
1121 }
1122 }
1123
1124 // This should never happen outside tests as public should be the base scope for every
1125 // scope and is enabled by default.
1126 return nil
1127}
1128
Jiyong Parkf1691d22021-03-29 20:11:58 +09001129func (c *commonToSdkLibraryAndImport) selectHeaderJarsForSdkVersion(ctx android.BaseModuleContext, sdkVersion android.SdkSpec) android.Paths {
Paul Duffinb05d4292020-05-20 12:19:10 +01001130
1131 // If a specific numeric version has been requested then use prebuilt versions of the sdk.
Jiyong Park54105c42021-03-31 18:17:53 +09001132 if !sdkVersion.ApiLevel.IsPreview() {
Spandan Das23956d12024-01-19 00:22:22 +00001133 return PrebuiltJars(ctx, c.module.RootLibraryName(), sdkVersion)
Paul Duffinb05d4292020-05-20 12:19:10 +01001134 }
1135
Paul Duffin1267d872021-04-16 17:21:36 +01001136 paths := c.selectScopePaths(ctx, sdkVersion.Kind)
1137 if paths == nil {
1138 return nil
1139 }
1140
1141 return paths.stubsHeaderPath
1142}
1143
1144// selectScopePaths returns the *scopePaths appropriate for the specific kind.
1145//
1146// If the module does not support the specific kind then it will return the *scopePaths for the
1147// closest kind which is a subset of the requested kind. e.g. if requesting android.SdkModule then
1148// it will return *scopePaths for android.SdkSystem if available or android.SdkPublic of not.
1149func (c *commonToSdkLibraryAndImport) selectScopePaths(ctx android.BaseModuleContext, kind android.SdkKind) *scopePaths {
Paul Duffin32cf58a2021-05-18 16:32:50 +01001150 apiScope := sdkKindToApiScope(kind)
Paul Duffinb05d4292020-05-20 12:19:10 +01001151
Paul Duffin803a9562020-05-20 11:52:25 +01001152 paths := c.findClosestScopePath(apiScope)
1153 if paths == nil {
1154 var scopes []string
Jihoon Kang98aa8fa2024-06-07 11:06:57 +00001155 for _, s := range AllApiScopes {
Paul Duffin803a9562020-05-20 11:52:25 +01001156 if c.findScopePaths(s) != nil {
1157 scopes = append(scopes, s.name)
1158 }
1159 }
Spandan Das23956d12024-01-19 00:22:22 +00001160 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 +01001161 return nil
1162 }
1163
Paul Duffin1267d872021-04-16 17:21:36 +01001164 return paths
1165}
1166
Paul Duffin32cf58a2021-05-18 16:32:50 +01001167// sdkKindToApiScope maps from android.SdkKind to apiScope.
1168func sdkKindToApiScope(kind android.SdkKind) *apiScope {
1169 var apiScope *apiScope
1170 switch kind {
1171 case android.SdkSystem:
1172 apiScope = apiScopeSystem
1173 case android.SdkModule:
1174 apiScope = apiScopeModuleLib
1175 case android.SdkTest:
1176 apiScope = apiScopeTest
1177 case android.SdkSystemServer:
1178 apiScope = apiScopeSystemServer
1179 default:
1180 apiScope = apiScopePublic
1181 }
1182 return apiScope
1183}
1184
Paul Duffin1267d872021-04-16 17:21:36 +01001185// to satisfy SdkLibraryDependency interface
Martin Stjernholm8be1e6d2021-09-15 03:34:04 +01001186func (c *commonToSdkLibraryAndImport) SdkApiStubDexJar(ctx android.BaseModuleContext, kind android.SdkKind) OptionalDexJarPath {
Paul Duffin1267d872021-04-16 17:21:36 +01001187 paths := c.selectScopePaths(ctx, kind)
1188 if paths == nil {
Martin Stjernholm8be1e6d2021-09-15 03:34:04 +01001189 return makeUnsetDexJarPath()
Paul Duffin1267d872021-04-16 17:21:36 +01001190 }
1191
1192 return paths.stubsDexJarPath
Paul Duffinb05d4292020-05-20 12:19:10 +01001193}
1194
Paul Duffin32cf58a2021-05-18 16:32:50 +01001195// to satisfy SdkLibraryDependency interface
Jihoon Kangbd093452023-12-26 19:08:01 +00001196func (c *commonToSdkLibraryAndImport) SdkApiExportableStubDexJar(ctx android.BaseModuleContext, kind android.SdkKind) OptionalDexJarPath {
1197 paths := c.selectScopePaths(ctx, kind)
1198 if paths == nil {
1199 return makeUnsetDexJarPath()
1200 }
1201
1202 return paths.exportableStubsDexJarPath
1203}
1204
1205// to satisfy SdkLibraryDependency interface
Paul Duffin32cf58a2021-05-18 16:32:50 +01001206func (c *commonToSdkLibraryAndImport) SdkRemovedTxtFile(ctx android.BaseModuleContext, kind android.SdkKind) android.OptionalPath {
1207 apiScope := sdkKindToApiScope(kind)
1208 paths := c.findScopePaths(apiScope)
1209 if paths == nil {
1210 return android.OptionalPath{}
1211 }
1212
1213 return paths.removedApiFilePath
1214}
1215
Paul Duffin859fe962020-05-15 10:20:31 +01001216func (c *commonToSdkLibraryAndImport) sdkComponentPropertiesForChildLibrary() interface{} {
1217 componentProps := &struct {
Paul Duffin3f0290e2021-06-30 18:25:36 +01001218 SdkLibraryName *string
Paul Duffin859fe962020-05-15 10:20:31 +01001219 SdkLibraryToImplicitlyTrack *string
Paul Duffindfa131e2020-05-15 20:37:11 +01001220 }{}
1221
Spandan Das23956d12024-01-19 00:22:22 +00001222 namePtr := proptools.StringPtr(c.module.RootLibraryName())
Paul Duffin3f0290e2021-06-30 18:25:36 +01001223 componentProps.SdkLibraryName = namePtr
1224
Paul Duffindfa131e2020-05-15 20:37:11 +01001225 if c.sharedLibrary() {
Paul Duffin859fe962020-05-15 10:20:31 +01001226 // Mark the stubs library as being components of this java_sdk_library so that
1227 // any app that includes code which depends (directly or indirectly) on the stubs
1228 // library will have the appropriate <uses-library> invocation inserted into its
1229 // manifest if necessary.
Paul Duffin3f0290e2021-06-30 18:25:36 +01001230 componentProps.SdkLibraryToImplicitlyTrack = namePtr
Paul Duffin859fe962020-05-15 10:20:31 +01001231 }
1232
1233 return componentProps
1234}
1235
Paul Duffindfa131e2020-05-15 20:37:11 +01001236func (c *commonToSdkLibraryAndImport) sharedLibrary() bool {
1237 return proptools.BoolDefault(c.commonSdkLibraryProperties.Shared_library, true)
1238}
1239
Paul Duffinf4600f62021-05-13 22:34:45 +01001240// Check if the stub libraries should be compiled for dex
1241func (c *commonToSdkLibraryAndImport) stubLibrariesCompiledForDex() bool {
1242 // Always compile the dex file files for the stub libraries if they will be used on the
1243 // bootclasspath.
1244 return !c.sharedLibrary()
1245}
1246
Paul Duffin859fe962020-05-15 10:20:31 +01001247// Properties related to the use of a module as an component of a java_sdk_library.
1248type SdkLibraryComponentProperties struct {
Paul Duffin3f0290e2021-06-30 18:25:36 +01001249 // The name of the java_sdk_library/_import module.
1250 SdkLibraryName *string `blueprint:"mutated"`
Paul Duffin859fe962020-05-15 10:20:31 +01001251
1252 // The name of the java_sdk_library/_import to add to a <uses-library> entry
1253 // in the AndroidManifest.xml of any Android app that includes code that references
1254 // this module. If not set then no java_sdk_library/_import is tracked.
1255 SdkLibraryToImplicitlyTrack *string `blueprint:"mutated"`
1256}
1257
1258// Structure to be embedded in a module struct that needs to support the
1259// SdkLibraryComponentDependency interface.
1260type EmbeddableSdkLibraryComponent struct {
1261 sdkLibraryComponentProperties SdkLibraryComponentProperties
1262}
1263
Paul Duffin71b33cc2021-06-23 11:39:47 +01001264func (e *EmbeddableSdkLibraryComponent) initSdkLibraryComponent(module android.Module) {
1265 module.AddProperties(&e.sdkLibraryComponentProperties)
Paul Duffin859fe962020-05-15 10:20:31 +01001266}
1267
1268// to satisfy SdkLibraryComponentDependency
Paul Duffin3f0290e2021-06-30 18:25:36 +01001269func (e *EmbeddableSdkLibraryComponent) SdkLibraryName() *string {
1270 return e.sdkLibraryComponentProperties.SdkLibraryName
1271}
1272
1273// to satisfy SdkLibraryComponentDependency
Ulya Trafimovich39b437b2020-09-23 16:42:35 +01001274func (e *EmbeddableSdkLibraryComponent) OptionalSdkLibraryImplementation() *string {
Ulya Trafimovich78645fb2021-07-16 15:29:25 +01001275 // For shared libraries, this is the same as the SDK library name. If a Java library or app
1276 // depends on a component library (e.g. a stub library) it still needs to know the name of the
1277 // run-time library and the corresponding module that provides the implementation. This name is
1278 // passed to manifest_fixer (to be added to AndroidManifest.xml) and added to CLC (to be used
1279 // in dexpreopt).
1280 //
1281 // For non-shared SDK (component or not) libraries this returns `nil`, as they are not
1282 // <uses-library> and should not be added to the manifest or to CLC.
Ulya Trafimovich39b437b2020-09-23 16:42:35 +01001283 return e.sdkLibraryComponentProperties.SdkLibraryToImplicitlyTrack
1284}
1285
Paul Duffin859fe962020-05-15 10:20:31 +01001286// Implemented by modules that are (or possibly could be) a component of a java_sdk_library
1287// (including the java_sdk_library) itself.
1288type SdkLibraryComponentDependency interface {
Ulya Trafimovich31e444e2020-08-14 17:32:16 +01001289 UsesLibraryDependency
1290
Paul Duffin3f0290e2021-06-30 18:25:36 +01001291 // SdkLibraryName returns the name of the java_sdk_library/_import module.
1292 SdkLibraryName() *string
1293
Ulya Trafimovich39b437b2020-09-23 16:42:35 +01001294 // The name of the implementation library for the optional SDK library or nil, if there isn't one.
1295 OptionalSdkLibraryImplementation() *string
Paul Duffin859fe962020-05-15 10:20:31 +01001296}
1297
1298// Make sure that all the module types that are components of java_sdk_library/_import
1299// and which can be referenced (directly or indirectly) from an android app implement
1300// the SdkLibraryComponentDependency interface.
1301var _ SdkLibraryComponentDependency = (*Library)(nil)
1302var _ SdkLibraryComponentDependency = (*Import)(nil)
1303var _ SdkLibraryComponentDependency = (*SdkLibrary)(nil)
Paul Duffineedc5d52020-06-12 17:46:39 +01001304var _ SdkLibraryComponentDependency = (*SdkLibraryImport)(nil)
Paul Duffin859fe962020-05-15 10:20:31 +01001305
Paul Duffin32cf58a2021-05-18 16:32:50 +01001306// Provides access to sdk_version related files, e.g. header and implementation jars.
Paul Duffin859fe962020-05-15 10:20:31 +01001307type SdkLibraryDependency interface {
1308 SdkLibraryComponentDependency
1309
1310 // Get the header jars appropriate for the supplied sdk_version.
1311 //
1312 // These are turbine generated jars so they only change if the externals of the
1313 // class changes but it does not contain and implementation or JavaDoc.
Jiyong Parkf1691d22021-03-29 20:11:58 +09001314 SdkHeaderJars(ctx android.BaseModuleContext, sdkVersion android.SdkSpec) android.Paths
Paul Duffin859fe962020-05-15 10:20:31 +01001315
Jihoon Kangbd093452023-12-26 19:08:01 +00001316 // SdkApiStubDexJar returns the dex jar for the stubs for the prebuilt
1317 // java_sdk_library_import module. It is needed by the hiddenapi processing tool which
1318 // processes dex files.
Martin Stjernholm8be1e6d2021-09-15 03:34:04 +01001319 SdkApiStubDexJar(ctx android.BaseModuleContext, kind android.SdkKind) OptionalDexJarPath
Paul Duffinf4600f62021-05-13 22:34:45 +01001320
Jihoon Kangbd093452023-12-26 19:08:01 +00001321 // SdkApiExportableStubDexJar returns the exportable dex jar for the stubs for
1322 // java_sdk_library module. It is needed by the hiddenapi processing tool which processes
1323 // dex files.
1324 SdkApiExportableStubDexJar(ctx android.BaseModuleContext, kind android.SdkKind) OptionalDexJarPath
1325
Paul Duffin32cf58a2021-05-18 16:32:50 +01001326 // SdkRemovedTxtFile returns the optional path to the removed.txt file for the specified sdk kind.
1327 SdkRemovedTxtFile(ctx android.BaseModuleContext, kind android.SdkKind) android.OptionalPath
1328
Paul Duffinf4600f62021-05-13 22:34:45 +01001329 // sharedLibrary returns true if this can be used as a shared library.
1330 sharedLibrary() bool
Jihoon Kanga3a05462024-04-05 00:36:44 +00001331
1332 getImplLibraryModule() *Library
Paul Duffin859fe962020-05-15 10:20:31 +01001333}
1334
Inseob Kimc0907f12019-02-08 21:00:45 +09001335type SdkLibrary struct {
Sundong Ahn054b19a2018-10-19 13:46:09 +09001336 Library
Jiyong Parkc678ad32018-04-10 13:07:10 +09001337
Sundong Ahn054b19a2018-10-19 13:46:09 +09001338 sdkLibraryProperties sdkLibraryProperties
Jiyong Parkc678ad32018-04-10 13:07:10 +09001339
Paul Duffin3375e352020-04-28 10:44:03 +01001340 // Map from api scope to the scope specific property structure.
1341 scopeToProperties map[*apiScope]*ApiScopeProperties
1342
Paul Duffin56d44902020-01-31 13:36:25 +00001343 commonToSdkLibraryAndImport
Jihoon Kanga3a05462024-04-05 00:36:44 +00001344
1345 builtInstalledForApex []dexpreopterInstall
Jiyong Parkc678ad32018-04-10 13:07:10 +09001346}
1347
Inseob Kimc0907f12019-02-08 21:00:45 +09001348var _ SdkLibraryDependency = (*SdkLibrary)(nil)
Colin Cross897d2ed2019-02-11 14:03:51 -08001349
Paul Duffin3375e352020-04-28 10:44:03 +01001350func (module *SdkLibrary) generateTestAndSystemScopesByDefault() bool {
1351 return module.sdkLibraryProperties.Generate_system_and_test_apis
1352}
1353
Jihoon Kanga3a05462024-04-05 00:36:44 +00001354func (module *SdkLibrary) DexJarBuildPath(ctx android.ModuleErrorfContext) OptionalDexJarPath {
1355 if module.implLibraryModule != nil {
1356 return module.implLibraryModule.DexJarBuildPath(ctx)
1357 }
1358 return makeUnsetDexJarPath()
1359}
1360
1361func (module *SdkLibrary) DexJarInstallPath() android.Path {
1362 if module.implLibraryModule != nil {
1363 return module.implLibraryModule.DexJarInstallPath()
1364 }
1365 return nil
1366}
1367
Paul Duffin3375e352020-04-28 10:44:03 +01001368func (module *SdkLibrary) getGeneratedApiScopes(ctx android.EarlyModuleContext) apiScopes {
1369 // Check to see if any scopes have been explicitly enabled. If any have then all
1370 // must be.
1371 anyScopesExplicitlyEnabled := false
Jihoon Kang98aa8fa2024-06-07 11:06:57 +00001372 for _, scope := range AllApiScopes {
Paul Duffin3375e352020-04-28 10:44:03 +01001373 scopeProperties := module.scopeToProperties[scope]
1374 if scopeProperties.Enabled != nil {
1375 anyScopesExplicitlyEnabled = true
1376 break
1377 }
Paul Duffind1b3a922020-01-22 11:57:20 +00001378 }
Paul Duffin3375e352020-04-28 10:44:03 +01001379
1380 var generatedScopes apiScopes
1381 enabledScopes := make(map[*apiScope]struct{})
Jihoon Kang98aa8fa2024-06-07 11:06:57 +00001382 for _, scope := range AllApiScopes {
Paul Duffin3375e352020-04-28 10:44:03 +01001383 scopeProperties := module.scopeToProperties[scope]
1384 // If any scopes are explicitly enabled then ignore the legacy enabled status.
1385 // This is to ensure that any new usages of this module type do not rely on legacy
1386 // behaviour.
1387 defaultEnabledStatus := false
1388 if anyScopesExplicitlyEnabled {
1389 defaultEnabledStatus = scope.defaultEnabledStatus
1390 } else {
1391 defaultEnabledStatus = scope.legacyEnabledStatus(module)
1392 }
1393 enabled := proptools.BoolDefault(scopeProperties.Enabled, defaultEnabledStatus)
1394 if enabled {
1395 enabledScopes[scope] = struct{}{}
1396 generatedScopes = append(generatedScopes, scope)
1397 }
1398 }
1399
1400 // Now check to make sure that any scope that is extended by an enabled scope is also
1401 // enabled.
Jihoon Kang98aa8fa2024-06-07 11:06:57 +00001402 for _, scope := range AllApiScopes {
Paul Duffin3375e352020-04-28 10:44:03 +01001403 if _, ok := enabledScopes[scope]; ok {
1404 extends := scope.extends
1405 if extends != nil {
1406 if _, ok := enabledScopes[extends]; !ok {
1407 ctx.ModuleErrorf("enabled api scope %q depends on disabled scope %q", scope, extends)
1408 }
1409 }
1410 }
1411 }
1412
1413 return generatedScopes
Paul Duffind1b3a922020-01-22 11:57:20 +00001414}
1415
satayev758968a2021-12-06 11:42:40 +00001416var _ android.ModuleWithMinSdkVersionCheck = (*SdkLibrary)(nil)
1417
satayev8f088b02021-12-06 11:40:46 +00001418func (module *SdkLibrary) CheckMinSdkVersion(ctx android.ModuleContext) {
Jihoon Kanga3a05462024-04-05 00:36:44 +00001419 CheckMinSdkVersion(ctx, &module.Library)
1420}
1421
1422func CheckMinSdkVersion(ctx android.ModuleContext, module *Library) {
Spandan Das8c9ae7e2023-03-03 21:20:36 +00001423 android.CheckMinSdkVersion(ctx, module.MinSdkVersion(ctx), func(c android.ModuleContext, do android.PayloadDepsCallback) {
satayev8f088b02021-12-06 11:40:46 +00001424 ctx.WalkDeps(func(child android.Module, parent android.Module) bool {
1425 isExternal := !module.depIsInSameApex(ctx, child)
1426 if am, ok := child.(android.ApexModule); ok {
1427 if !do(ctx, parent, am, isExternal) {
1428 return false
1429 }
1430 }
1431 return !isExternal
1432 })
1433 })
1434}
1435
Paul Duffineedc5d52020-06-12 17:46:39 +01001436type sdkLibraryComponentTag struct {
1437 blueprint.BaseDependencyTag
1438 name string
1439}
1440
1441// Mark this tag so dependencies that use it are excluded from visibility enforcement.
1442func (t sdkLibraryComponentTag) ExcludeFromVisibilityEnforcement() {}
1443
1444var xmlPermissionsFileTag = sdkLibraryComponentTag{name: "xml-permissions-file"}
Paul Duffine74ac732020-02-06 13:51:46 +00001445
Jiyong Parke3833882020-02-17 17:28:10 +09001446func IsXmlPermissionsFileDepTag(depTag blueprint.DependencyTag) bool {
Paul Duffineedc5d52020-06-12 17:46:39 +01001447 if dt, ok := depTag.(sdkLibraryComponentTag); ok {
Jiyong Parke3833882020-02-17 17:28:10 +09001448 return dt == xmlPermissionsFileTag
1449 }
1450 return false
1451}
1452
Paul Duffineedc5d52020-06-12 17:46:39 +01001453var implLibraryTag = sdkLibraryComponentTag{name: "impl-library"}
Paul Duffin5df79302020-05-16 15:52:12 +01001454
Jeongik Chaaaa6dcd2024-05-22 00:41:28 +09001455var _ android.InstallNeededDependencyTag = sdkLibraryComponentTag{}
1456
Jihoon Kang46d66de2024-05-22 22:42:39 +00001457// To satisfy the CopyDirectlyInAnyApexTag interface. Implementation library of the sdk library
1458// in an apex is considered to be directly in the apex, as if it was listed in java_libs.
1459func (t sdkLibraryComponentTag) CopyDirectlyInAnyApex() {}
1460
1461var _ android.CopyDirectlyInAnyApexTag = implLibraryTag
1462
Jeongik Chaaaa6dcd2024-05-22 00:41:28 +09001463func (t sdkLibraryComponentTag) InstallDepNeeded() bool {
1464 return t.name == "xml-permissions-file" || t.name == "impl-library"
1465}
1466
Paul Duffin44f1d842020-06-26 20:17:02 +01001467// Add the dependencies on the child modules in the component deps mutator.
1468func (module *SdkLibrary) ComponentDepsMutator(ctx android.BottomUpMutatorContext) {
Paul Duffin3375e352020-04-28 10:44:03 +01001469 for _, apiScope := range module.getGeneratedApiScopes(ctx) {
Paul Duffind1b3a922020-01-22 11:57:20 +00001470 // Add dependencies to the stubs library
Spandan Das877f39d2023-03-29 16:19:51 +00001471 stubModuleName := module.stubsLibraryModuleName(apiScope)
Jihoon Kangbd093452023-12-26 19:08:01 +00001472 ctx.AddVariationDependencies(nil, apiScope.everythingStubsTag, stubModuleName)
Jihoon Kang1147b312023-06-08 23:25:57 +00001473
Jihoon Kangbd093452023-12-26 19:08:01 +00001474 exportableStubModuleName := module.exportableStubsLibraryModuleName(apiScope)
1475 ctx.AddVariationDependencies(nil, apiScope.exportableStubsTag, exportableStubModuleName)
Paul Duffind1b3a922020-01-22 11:57:20 +00001476
Paul Duffin15f34ef2020-07-20 18:04:44 +01001477 // Add a dependency on the stubs source in order to access both stubs source and api information.
1478 ctx.AddVariationDependencies(nil, apiScope.stubsSourceAndApiTag, module.stubsSourceModuleName(apiScope))
Paul Duffin958806b2022-05-16 13:10:47 +00001479
1480 if module.compareAgainstLatestApi(apiScope) {
1481 // Add dependencies on the latest finalized version of the API .txt file.
1482 latestApiModuleName := module.latestApiModuleName(apiScope)
1483 ctx.AddDependency(module, apiScope.latestApiModuleTag, latestApiModuleName)
1484
1485 // Add dependencies on the latest finalized version of the remove API .txt file.
1486 latestRemovedApiModuleName := module.latestRemovedApiModuleName(apiScope)
1487 ctx.AddDependency(module, apiScope.latestRemovedApiModuleTag, latestRemovedApiModuleName)
1488 }
Sundong Ahn054b19a2018-10-19 13:46:09 +09001489 }
1490
Paul Duffindfa131e2020-05-15 20:37:11 +01001491 if module.requiresRuntimeImplementationLibrary() {
Paul Duffin5df79302020-05-16 15:52:12 +01001492 // Add dependency to the rule for generating the implementation library.
1493 ctx.AddDependency(module, implLibraryTag, module.implLibraryModuleName())
1494
Paul Duffindfa131e2020-05-15 20:37:11 +01001495 if module.sharedLibrary() {
1496 // Add dependency to the rule for generating the xml permissions file
Paul Duffineedc5d52020-06-12 17:46:39 +01001497 ctx.AddDependency(module, xmlPermissionsFileTag, module.xmlPermissionsModuleName())
Paul Duffindfa131e2020-05-15 20:37:11 +01001498 }
Paul Duffin44f1d842020-06-26 20:17:02 +01001499 }
1500}
Paul Duffine74ac732020-02-06 13:51:46 +00001501
Paul Duffin44f1d842020-06-26 20:17:02 +01001502// Add other dependencies as normal.
1503func (module *SdkLibrary) DepsMutator(ctx android.BottomUpMutatorContext) {
Jihoon Kange4a90172024-07-18 22:49:08 +00001504 // If the module does not create an implementation library or defaults to stubs,
1505 // mark the top level sdk library as stubs module as the module will provide stubs via
1506 // "magic" when listed as a dependency in the Android.bp files.
1507 notCreateImplLib := proptools.Bool(module.sdkLibraryProperties.Api_only)
1508 preferStubs := proptools.Bool(module.sdkLibraryProperties.Default_to_stubs)
1509 module.properties.Is_stubs_module = proptools.BoolPtr(notCreateImplLib || preferStubs)
1510
Anton Hanssone77fccc2021-01-20 16:52:41 +00001511 var missingApiModules []string
1512 for _, apiScope := range module.getGeneratedApiScopes(ctx) {
1513 if apiScope.unstable {
1514 continue
1515 }
Paul Duffin958806b2022-05-16 13:10:47 +00001516 if m := module.latestApiModuleName(apiScope); !ctx.OtherModuleExists(m) {
Anton Hanssone77fccc2021-01-20 16:52:41 +00001517 missingApiModules = append(missingApiModules, m)
1518 }
Paul Duffin958806b2022-05-16 13:10:47 +00001519 if m := module.latestRemovedApiModuleName(apiScope); !ctx.OtherModuleExists(m) {
Anton Hanssone77fccc2021-01-20 16:52:41 +00001520 missingApiModules = append(missingApiModules, m)
1521 }
Paul Duffin958806b2022-05-16 13:10:47 +00001522 if m := module.latestIncompatibilitiesModuleName(apiScope); !ctx.OtherModuleExists(m) {
Jaewoong Jung1a97ee02021-03-09 13:25:02 -08001523 missingApiModules = append(missingApiModules, m)
1524 }
Anton Hanssone77fccc2021-01-20 16:52:41 +00001525 }
1526 if len(missingApiModules) != 0 && !module.sdkLibraryProperties.Unsafe_ignore_missing_latest_api {
1527 m := module.Name() + " is missing tracking files for previously released library versions.\n"
1528 m += "You need to do one of the following:\n"
1529 m += "- Add `unsafe_ignore_missing_latest_api: true` to your blueprint (to disable compat tracking)\n"
1530 m += "- Add a set of prebuilt txt files representing the last released version of this library for compat checking.\n"
1531 m += " (the current set of API files can be used as a seed for this compatibility tracking\n"
1532 m += "\n"
1533 m += "The following filegroup modules are missing:\n "
1534 m += strings.Join(missingApiModules, "\n ") + "\n"
1535 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."
1536 ctx.ModuleErrorf(m)
1537 }
Jiyong Parkc678ad32018-04-10 13:07:10 +09001538}
1539
Inseob Kimc0907f12019-02-08 21:00:45 +09001540func (module *SdkLibrary) GenerateAndroidBuildActions(ctx android.ModuleContext) {
Spandan Das5ae65ee2024-04-16 22:03:26 +00001541 if disableSourceApexVariant(ctx) {
1542 // Prebuilts are active, do not create the installation rules for the source javalib.
1543 // Even though the source javalib is not used, we need to hide it to prevent duplicate installation rules.
1544 // TODO (b/331665856): Implement a principled solution for this.
1545 module.HideFromMake()
1546 }
satayev8f088b02021-12-06 11:40:46 +00001547
Paul Duffina2ae7e02020-09-11 11:55:00 +01001548 module.generateCommonBuildActions(ctx)
1549
Jihoon Kanga3a05462024-04-05 00:36:44 +00001550 module.stem = proptools.StringDefault(module.overridableProperties.Stem, ctx.ModuleName())
1551
1552 module.provideHiddenAPIPropertyInfo(ctx)
Sundong Ahn054b19a2018-10-19 13:46:09 +09001553
Paul Duffinb97b1572021-04-29 21:50:40 +01001554 // Collate the components exported by this module. All scope specific modules are exported but
1555 // the impl and xml component modules are not.
1556 exportedComponents := map[string]struct{}{}
1557
Sundong Ahn57368eb2018-07-06 11:20:23 +09001558 // Record the paths to the header jars of the library (stubs and impl).
Paul Duffind1b3a922020-01-22 11:57:20 +00001559 // When this java_sdk_library is depended upon from others via "libs" property,
Jiyong Parkc678ad32018-04-10 13:07:10 +09001560 // the recorded paths will be returned depending on the link type of the caller.
1561 ctx.VisitDirectDeps(func(to android.Module) {
Jiyong Parkc678ad32018-04-10 13:07:10 +09001562 tag := ctx.OtherModuleDependencyTag(to)
1563
Paul Duffinc8782502020-04-29 20:45:27 +01001564 // Extract information from any of the scope specific dependencies.
1565 if scopeTag, ok := tag.(scopeDependencyTag); ok {
1566 apiScope := scopeTag.apiScope
Paul Duffin803a9562020-05-20 11:52:25 +01001567 scopePaths := module.getScopePathsCreateIfNeeded(apiScope)
Paul Duffinc8782502020-04-29 20:45:27 +01001568
1569 // Extract information from the dependency. The exact information extracted
1570 // is determined by the nature of the dependency which is determined by the tag.
1571 scopeTag.extractDepInfo(ctx, to, scopePaths)
Paul Duffinb97b1572021-04-29 21:50:40 +01001572
1573 exportedComponents[ctx.OtherModuleName(to)] = struct{}{}
Sundong Ahn20e998b2018-07-24 11:19:26 +09001574 }
Jihoon Kang8479dea2024-04-04 01:19:05 +00001575
1576 if tag == implLibraryTag {
1577 if dep, ok := android.OtherModuleProvider(ctx, to, JavaInfoProvider); ok {
1578 module.implLibraryHeaderJars = append(module.implLibraryHeaderJars, dep.HeaderJars...)
Jihoon Kanga3a05462024-04-05 00:36:44 +00001579 module.implLibraryModule = to.(*Library)
1580 android.SetProvider(ctx, JavaInfoProvider, dep)
Jihoon Kang8479dea2024-04-04 01:19:05 +00001581 }
1582 }
Jiyong Parkc678ad32018-04-10 13:07:10 +09001583 })
Paul Duffinb97b1572021-04-29 21:50:40 +01001584
Jihoon Kanga3a05462024-04-05 00:36:44 +00001585 apexInfo, _ := android.ModuleProvider(ctx, android.ApexInfoProvider)
1586 if !apexInfo.IsForPlatform() {
1587 module.hideApexVariantFromMake = true
1588 }
1589
1590 if module.implLibraryModule != nil {
1591 if ctx.Device() {
1592 module.classesJarPaths = android.Paths{module.implLibraryModule.implementationJarFile}
1593 module.bootDexJarPath = module.implLibraryModule.bootDexJarPath
1594 module.uncompressDexState = module.implLibraryModule.uncompressDexState
1595 module.active = module.implLibraryModule.active
1596 }
1597
1598 module.outputFile = module.implLibraryModule.outputFile
1599 module.dexJarFile = makeDexJarPathFromPath(module.implLibraryModule.dexJarFile.Path())
1600 module.headerJarFile = module.implLibraryModule.headerJarFile
1601 module.implementationAndResourcesJar = module.implLibraryModule.implementationAndResourcesJar
1602 module.builtInstalledForApex = module.implLibraryModule.builtInstalledForApex
1603 module.dexpreopter.configPath = module.implLibraryModule.dexpreopter.configPath
1604 module.dexpreopter.outputProfilePathOnHost = module.implLibraryModule.dexpreopter.outputProfilePathOnHost
1605
Jihoon Kang34155e32024-05-20 19:08:49 +00001606 // Properties required for Library.AndroidMkEntries
1607 module.logtagsSrcs = module.implLibraryModule.logtagsSrcs
1608 module.dexpreopter.builtInstalled = module.implLibraryModule.dexpreopter.builtInstalled
1609 module.jacocoReportClassesFile = module.implLibraryModule.jacocoReportClassesFile
1610 module.dexer.proguardDictionary = module.implLibraryModule.dexer.proguardDictionary
1611 module.dexer.proguardUsageZip = module.implLibraryModule.dexer.proguardUsageZip
1612 module.linter.reports = module.implLibraryModule.linter.reports
Jihoon Kang629e2a32024-06-25 20:47:49 +00001613 module.linter.outputs.depSets = module.implLibraryModule.LintDepSets()
Jihoon Kang34155e32024-05-20 19:08:49 +00001614
Jihoon Kanga3a05462024-04-05 00:36:44 +00001615 if !module.Host() {
1616 module.hostdexInstallFile = module.implLibraryModule.hostdexInstallFile
1617 }
1618
1619 android.SetProvider(ctx, blueprint.SrcsFileProviderKey, blueprint.SrcsFileProviderData{SrcPaths: module.implLibraryModule.uniqueSrcFiles.Strings()})
1620 }
1621
Paul Duffinb97b1572021-04-29 21:50:40 +01001622 // Make the set of components exported by this module available for use elsewhere.
Cole Faust18994c72023-02-28 16:02:16 -08001623 exportedComponentInfo := android.ExportedComponentsInfo{Components: android.SortedKeys(exportedComponents)}
Colin Cross40213022023-12-13 15:19:49 -08001624 android.SetProvider(ctx, android.ExportedComponentsInfoProvider, exportedComponentInfo)
Paul Duffin958806b2022-05-16 13:10:47 +00001625
1626 // Provide additional information for inclusion in an sdk's generated .info file.
1627 additionalSdkInfo := map[string]interface{}{}
1628 additionalSdkInfo["dist_stem"] = module.distStem()
Paul Duffine8409952022-09-22 16:24:46 +01001629 baseModuleName := module.distStem()
Paul Duffin958806b2022-05-16 13:10:47 +00001630 scopes := map[string]interface{}{}
1631 additionalSdkInfo["scopes"] = scopes
1632 for scope, scopePaths := range module.scopePaths {
1633 scopeInfo := map[string]interface{}{}
1634 scopes[scope.name] = scopeInfo
1635 scopeInfo["current_api"] = scope.snapshotRelativeCurrentApiTxtPath(baseModuleName)
1636 scopeInfo["removed_api"] = scope.snapshotRelativeRemovedApiTxtPath(baseModuleName)
Jihoon Kang5623e542024-01-31 23:27:26 +00001637 if p := scopePaths.latestApiPaths; len(p) > 0 {
1638 // The last path in the list is the one that applies to this scope, the
1639 // preceding ones, if any, are for the scope(s) that it extends.
1640 scopeInfo["latest_api"] = p[len(p)-1].String()
Paul Duffin958806b2022-05-16 13:10:47 +00001641 }
Jihoon Kang5623e542024-01-31 23:27:26 +00001642 if p := scopePaths.latestRemovedApiPaths; len(p) > 0 {
1643 // The last path in the list is the one that applies to this scope, the
1644 // preceding ones, if any, are for the scope(s) that it extends.
1645 scopeInfo["latest_removed_api"] = p[len(p)-1].String()
Paul Duffin958806b2022-05-16 13:10:47 +00001646 }
1647 }
Colin Cross40213022023-12-13 15:19:49 -08001648 android.SetProvider(ctx, android.AdditionalSdkInfoProvider, android.AdditionalSdkInfo{additionalSdkInfo})
mrziwang9f7b9f42024-07-10 12:18:06 -07001649 module.setOutputFiles(ctx)
1650 if module.requiresRuntimeImplementationLibrary() && module.implLibraryModule != nil {
1651 setOutputFiles(ctx, module.implLibraryModule.Module)
1652 }
Jiyong Parkc678ad32018-04-10 13:07:10 +09001653}
1654
Jihoon Kanga3a05462024-04-05 00:36:44 +00001655func (module *SdkLibrary) BuiltInstalledForApex() []dexpreopterInstall {
1656 return module.builtInstalledForApex
1657}
1658
Jiyong Park0b0e1b92019-12-03 13:24:29 +09001659func (module *SdkLibrary) AndroidMkEntries() []android.AndroidMkEntries {
Paul Duffindfa131e2020-05-15 20:37:11 +01001660 if !module.requiresRuntimeImplementationLibrary() {
Paul Duffin43db9be2019-12-30 17:35:49 +00001661 return nil
1662 }
Jiyong Park0b0e1b92019-12-03 13:24:29 +09001663 entriesList := module.Library.AndroidMkEntries()
Jihoon Kanga3a05462024-04-05 00:36:44 +00001664 entries := &entriesList[0]
1665 entries.Required = append(entries.Required, module.implLibraryModuleName())
Yo Chiang07d75072020-06-05 17:43:19 +08001666 if module.sharedLibrary() {
Yo Chiang07d75072020-06-05 17:43:19 +08001667 entries.Required = append(entries.Required, module.xmlPermissionsModuleName())
1668 }
Jiyong Park0b0e1b92019-12-03 13:24:29 +09001669 return entriesList
Jiyong Park82484c02018-04-23 21:41:26 +09001670}
1671
Anton Hansson5fd5d242020-03-27 19:43:19 +00001672// The dist path of the stub artifacts
1673func (module *SdkLibrary) apiDistPath(apiScope *apiScope) string {
Colin Crossf0eace92021-06-02 13:02:23 -07001674 return path.Join("apistubs", module.distGroup(), apiScope.name)
Anton Hansson5fd5d242020-03-27 19:43:19 +00001675}
1676
Paul Duffin12ceb462019-12-24 20:31:31 +00001677// Get the sdk version for use when compiling the stubs library.
Paul Duffin780c5f42020-05-12 15:52:55 +01001678func (module *SdkLibrary) sdkVersionForStubsLibrary(mctx android.EarlyModuleContext, apiScope *apiScope) string {
Paul Duffin87a05a32020-05-12 11:50:28 +01001679 scopeProperties := module.scopeToProperties[apiScope]
1680 if scopeProperties.Sdk_version != nil {
1681 return proptools.String(scopeProperties.Sdk_version)
1682 }
1683
Jiyong Parkf1691d22021-03-29 20:11:58 +09001684 sdkDep := decodeSdkDep(mctx, android.SdkContext(&module.Library))
Paul Duffin12ceb462019-12-24 20:31:31 +00001685 if sdkDep.hasStandardLibs() {
1686 // If building against a standard sdk then use the sdk version appropriate for the scope.
Paul Duffind1b3a922020-01-22 11:57:20 +00001687 return apiScope.sdkVersion
Paul Duffin12ceb462019-12-24 20:31:31 +00001688 } else {
1689 // Otherwise, use no system module.
1690 return "none"
1691 }
1692}
1693
Paul Duffin31310252020-11-20 21:26:20 +00001694func (module *SdkLibrary) distStem() string {
1695 return proptools.StringDefault(module.sdkLibraryProperties.Dist_stem, module.BaseModuleName())
1696}
1697
Colin Cross986b69a2021-06-01 13:13:40 -07001698// distGroup returns the subdirectory of the dist path of the stub artifacts.
1699func (module *SdkLibrary) distGroup() string {
Colin Cross59b92bf2021-06-01 14:07:56 -07001700 return proptools.StringDefault(module.sdkLibraryProperties.Dist_group, "unknown")
Colin Cross986b69a2021-06-01 13:13:40 -07001701}
1702
Paul Duffin958806b2022-05-16 13:10:47 +00001703func latestPrebuiltApiModuleName(name string, apiScope *apiScope) string {
1704 return PrebuiltApiModuleName(name, apiScope.name, "latest")
1705}
1706
Jihoon Kang748a24d2024-03-20 21:29:39 +00001707func latestPrebuiltApiCombinedModuleName(name string, apiScope *apiScope) string {
1708 return PrebuiltApiCombinedModuleName(name, apiScope.name, "latest")
1709}
1710
Paul Duffind1b3a922020-01-22 11:57:20 +00001711func (module *SdkLibrary) latestApiFilegroupName(apiScope *apiScope) string {
Paul Duffin958806b2022-05-16 13:10:47 +00001712 return ":" + module.latestApiModuleName(apiScope)
1713}
1714
1715func (module *SdkLibrary) latestApiModuleName(apiScope *apiScope) string {
Jihoon Kang748a24d2024-03-20 21:29:39 +00001716 return latestPrebuiltApiCombinedModuleName(module.distStem(), apiScope)
Jiyong Park58c518b2018-05-12 22:29:12 +09001717}
Jiyong Parkc678ad32018-04-10 13:07:10 +09001718
Paul Duffind1b3a922020-01-22 11:57:20 +00001719func (module *SdkLibrary) latestRemovedApiFilegroupName(apiScope *apiScope) string {
Paul Duffin958806b2022-05-16 13:10:47 +00001720 return ":" + module.latestRemovedApiModuleName(apiScope)
1721}
1722
1723func (module *SdkLibrary) latestRemovedApiModuleName(apiScope *apiScope) string {
Jihoon Kang748a24d2024-03-20 21:29:39 +00001724 return latestPrebuiltApiCombinedModuleName(module.distStem()+"-removed", apiScope)
Jiyong Parkc678ad32018-04-10 13:07:10 +09001725}
1726
Jaewoong Jung1a97ee02021-03-09 13:25:02 -08001727func (module *SdkLibrary) latestIncompatibilitiesFilegroupName(apiScope *apiScope) string {
Paul Duffin958806b2022-05-16 13:10:47 +00001728 return ":" + module.latestIncompatibilitiesModuleName(apiScope)
1729}
1730
1731func (module *SdkLibrary) latestIncompatibilitiesModuleName(apiScope *apiScope) string {
1732 return latestPrebuiltApiModuleName(module.distStem()+"-incompatibilities", apiScope)
Jaewoong Jung1a97ee02021-03-09 13:25:02 -08001733}
1734
Jihoon Kang0c705a42023-08-02 06:44:57 +00001735// The listed modules' stubs contents do not match the corresponding txt files,
1736// but require additional api contributions to generate the full stubs.
1737// This method returns the name of the additional api contribution module
1738// for corresponding sdk_library modules.
1739func (module *SdkLibrary) apiLibraryAdditionalApiContribution() string {
1740 if val, ok := apiLibraryAdditionalProperties[module.Name()]; ok {
Jihoon Kangb0f4c022024-08-06 00:15:25 +00001741 return val
Jihoon Kang0c705a42023-08-02 06:44:57 +00001742 }
1743 return ""
1744}
1745
Anton Hansson944e77d2020-08-19 11:40:22 +01001746func childModuleVisibility(childVisibility []string) []string {
1747 if childVisibility == nil {
1748 // No child visibility set. The child will use the visibility of the sdk_library.
1749 return nil
1750 }
1751
1752 // Prepend an override to ignore the sdk_library's visibility, and rely on the child visibility.
1753 var visibility []string
1754 visibility = append(visibility, "//visibility:override")
1755 visibility = append(visibility, childVisibility...)
1756 return visibility
1757}
1758
Paul Duffin5df79302020-05-16 15:52:12 +01001759// Creates the implementation java library
1760func (module *SdkLibrary) createImplLibrary(mctx android.DefaultableHookContext) {
Anton Hansson944e77d2020-08-19 11:40:22 +01001761 visibility := childModuleVisibility(module.sdkLibraryProperties.Impl_library_visibility)
1762
Paul Duffin5df79302020-05-16 15:52:12 +01001763 props := struct {
Paul Duffin77590a82022-04-28 14:13:30 +00001764 Name *string
1765 Visibility []string
Paul Duffin77590a82022-04-28 14:13:30 +00001766 Libs []string
1767 Static_libs []string
1768 Apex_available []string
Jihoon Kanga3a05462024-04-05 00:36:44 +00001769 Stem *string
Paul Duffin5df79302020-05-16 15:52:12 +01001770 }{
1771 Name: proptools.StringPtr(module.implLibraryModuleName()),
Anton Hansson944e77d2020-08-19 11:40:22 +01001772 Visibility: visibility,
Jihoon Kanga3a05462024-04-05 00:36:44 +00001773
1774 Libs: append(module.properties.Libs, module.sdkLibraryProperties.Impl_only_libs...),
1775
1776 Static_libs: append(module.properties.Static_libs, module.sdkLibraryProperties.Impl_only_static_libs...),
Paul Duffin77590a82022-04-28 14:13:30 +00001777 // Pass the apex_available settings down so that the impl library can be statically
1778 // embedded within a library that is added to an APEX. Needed for updatable-media.
1779 Apex_available: module.ApexAvailable(),
Jihoon Kanga3a05462024-04-05 00:36:44 +00001780
1781 Stem: proptools.StringPtr(module.Name()),
Paul Duffin5df79302020-05-16 15:52:12 +01001782 }
1783
1784 properties := []interface{}{
1785 &module.properties,
1786 &module.protoProperties,
1787 &module.deviceProperties,
Liz Kammera7a64f32020-07-09 15:16:41 -07001788 &module.dexProperties,
Paul Duffin5df79302020-05-16 15:52:12 +01001789 &module.dexpreoptProperties,
Colin Cross014489c2020-06-02 20:09:13 -07001790 &module.linter.properties,
Spandan Dasb9c58352024-05-13 18:29:45 +00001791 &module.overridableProperties,
Paul Duffin5df79302020-05-16 15:52:12 +01001792 &props,
1793 module.sdkComponentPropertiesForChildLibrary(),
1794 }
1795 mctx.CreateModule(LibraryFactory, properties...)
1796}
1797
Jihoon Kangfa4a90d2023-12-20 02:53:38 +00001798type libraryProperties struct {
1799 Name *string
1800 Visibility []string
1801 Srcs []string
1802 Installable *bool
1803 Sdk_version *string
1804 System_modules *string
1805 Patch_module *string
1806 Libs []string
1807 Static_libs []string
1808 Compile_dex *bool
1809 Java_version *string
1810 Openjdk9 struct {
1811 Srcs []string
1812 Javacflags []string
1813 }
1814 Dist struct {
1815 Targets []string
1816 Dest *string
1817 Dir *string
1818 Tag *string
1819 }
Jihoon Kangfe914ed2024-02-12 22:49:21 +00001820 Is_stubs_module *bool
Jihoon Kangfa4a90d2023-12-20 02:53:38 +00001821}
Jiyong Parkc678ad32018-04-10 13:07:10 +09001822
Jihoon Kangfa4a90d2023-12-20 02:53:38 +00001823func (module *SdkLibrary) stubsLibraryProps(mctx android.DefaultableHookContext, apiScope *apiScope) libraryProperties {
1824 props := libraryProperties{}
Jihoon Kang786df932023-09-07 01:18:31 +00001825 props.Visibility = []string{"//visibility:override", "//visibility:private"}
Jiyong Parkc678ad32018-04-10 13:07:10 +09001826 // sources are generated from the droiddoc
Paul Duffin12ceb462019-12-24 20:31:31 +00001827 sdkVersion := module.sdkVersionForStubsLibrary(mctx, apiScope)
Paul Duffin52d398a2019-06-11 12:31:14 +01001828 props.Sdk_version = proptools.StringPtr(sdkVersion)
Paul Duffina18abc22020-05-16 18:54:24 +01001829 props.System_modules = module.deviceProperties.System_modules
1830 props.Patch_module = module.properties.Patch_module
Paul Duffin367ab912019-12-23 19:40:36 +00001831 props.Installable = proptools.BoolPtr(false)
Sundong Ahn054b19a2018-10-19 13:46:09 +09001832 props.Libs = module.sdkLibraryProperties.Stub_only_libs
Mark White9421c4c2023-08-10 00:07:03 +00001833 props.Libs = append(props.Libs, module.scopeToProperties[apiScope].Libs...)
Anton Hanssondae54cd2021-04-21 16:30:10 +01001834 props.Static_libs = module.sdkLibraryProperties.Stub_only_static_libs
Paul Duffine22c2ab2020-05-20 19:35:27 +01001835 // The stub-annotations library contains special versions of the annotations
1836 // with CLASS retention policy, so that they're kept.
1837 if proptools.Bool(module.sdkLibraryProperties.Annotations_enabled) {
1838 props.Libs = append(props.Libs, "stub-annotations")
1839 }
Paul Duffina18abc22020-05-16 18:54:24 +01001840 props.Openjdk9.Srcs = module.properties.Openjdk9.Srcs
1841 props.Openjdk9.Javacflags = module.properties.Openjdk9.Javacflags
Anton Hansson83509b52020-05-21 09:21:57 +01001842 // We compile the stubs for 1.8 in line with the main android.jar stubs, and potential
1843 // interop with older developer tools that don't support 1.9.
1844 props.Java_version = proptools.StringPtr("1.8")
Jihoon Kangfe914ed2024-02-12 22:49:21 +00001845 props.Is_stubs_module = proptools.BoolPtr(true)
Paul Duffinf4600f62021-05-13 22:34:45 +01001846
Jihoon Kangfa4a90d2023-12-20 02:53:38 +00001847 return props
1848}
1849
1850// Creates a static java library that has API stubs
1851func (module *SdkLibrary) createStubsLibrary(mctx android.DefaultableHookContext, apiScope *apiScope) {
1852
1853 props := module.stubsLibraryProps(mctx, apiScope)
1854 props.Name = proptools.StringPtr(module.sourceStubsLibraryModuleName(apiScope))
1855 props.Srcs = []string{":" + module.stubsSourceModuleName(apiScope)}
1856
1857 mctx.CreateModule(LibraryFactory, &props, module.sdkComponentPropertiesForChildLibrary())
1858}
1859
1860// Create a static java library that compiles the "exportable" stubs
1861func (module *SdkLibrary) createExportableStubsLibrary(mctx android.DefaultableHookContext, apiScope *apiScope) {
1862 props := module.stubsLibraryProps(mctx, apiScope)
1863 props.Name = proptools.StringPtr(module.exportableSourceStubsLibraryModuleName(apiScope))
1864 props.Srcs = []string{":" + module.stubsSourceModuleName(apiScope) + "{.exportable}"}
1865
Paul Duffin859fe962020-05-15 10:20:31 +01001866 mctx.CreateModule(LibraryFactory, &props, module.sdkComponentPropertiesForChildLibrary())
Jiyong Parkc678ad32018-04-10 13:07:10 +09001867}
1868
Paul Duffin6d0886e2020-04-07 18:49:53 +01001869// Creates a droidstubs module that creates stubs source files from the given full source
Paul Duffinc8782502020-04-29 20:45:27 +01001870// files and also updates and checks the API specification files.
Paul Duffin15f34ef2020-07-20 18:04:44 +01001871func (module *SdkLibrary) createStubsSourcesAndApi(mctx android.DefaultableHookContext, apiScope *apiScope, name string, scopeSpecificDroidstubsArgs []string) {
Jiyong Parkc678ad32018-04-10 13:07:10 +09001872 props := struct {
Sundong Ahn054b19a2018-10-19 13:46:09 +09001873 Name *string
Paul Duffin4911a892020-04-29 23:35:13 +01001874 Visibility []string
Sundong Ahn054b19a2018-10-19 13:46:09 +09001875 Srcs []string
1876 Installable *bool
Paul Duffin52d398a2019-06-11 12:31:14 +01001877 Sdk_version *string
Jihoon Kang3198f3c2023-01-26 08:08:52 +00001878 Api_surface *string
Paul Duffin12ceb462019-12-24 20:31:31 +00001879 System_modules *string
Sundong Ahn054b19a2018-10-19 13:46:09 +09001880 Libs []string
Paul Duffin6877e6d2020-09-25 19:59:14 +01001881 Output_javadoc_comments *bool
Paul Duffin11512472019-02-11 15:55:17 +00001882 Arg_files []string
Sundong Ahn054b19a2018-10-19 13:46:09 +09001883 Args *string
Sundong Ahn054b19a2018-10-19 13:46:09 +09001884 Java_version *string
Paul Duffine22c2ab2020-05-20 19:35:27 +01001885 Annotations_enabled *bool
Sundong Ahn054b19a2018-10-19 13:46:09 +09001886 Merge_annotations_dirs []string
1887 Merge_inclusion_annotations_dirs []string
Paul Duffin0ff08bd2020-04-29 13:30:54 +01001888 Generate_stubs *bool
Anton Hanssone87b03d2020-12-21 15:29:34 +00001889 Previous_api *string
Jihoon Kang6592e872023-12-19 01:13:16 +00001890 Aconfig_declarations []string
Sundong Ahn054b19a2018-10-19 13:46:09 +09001891 Check_api struct {
Anton Hanssone6056152020-12-31 10:37:27 +00001892 Current ApiToCheck
1893 Last_released ApiToCheck
Paul Duffin160fe412020-05-10 19:32:20 +01001894
1895 Api_lint struct {
1896 Enabled *bool
1897 New_since *string
1898 Baseline_file *string
1899 }
Jiyong Park58c518b2018-05-12 22:29:12 +09001900 }
Sundong Ahn1b92c822018-05-29 11:35:17 +09001901 Aidl struct {
1902 Include_dirs []string
1903 Local_include_dirs []string
1904 }
Paul Duffin040e9062020-11-23 17:41:36 +00001905 Dists []android.Dist
Jiyong Parkc678ad32018-04-10 13:07:10 +09001906 }{}
1907
Paul Duffin7b78b4d2020-04-28 14:08:32 +01001908 // The stubs source processing uses the same compile time classpath when extracting the
1909 // API from the implementation library as it does when compiling it. i.e. the same
1910 // * sdk version
1911 // * system_modules
1912 // * libs (static_libs/libs)
Paul Duffin250e6192019-06-07 10:44:37 +01001913
Paul Duffin0ff08bd2020-04-29 13:30:54 +01001914 props.Name = proptools.StringPtr(name)
Anton Hansson944e77d2020-08-19 11:40:22 +01001915 props.Visibility = childModuleVisibility(module.sdkLibraryProperties.Stubs_source_visibility)
Paul Duffina18abc22020-05-16 18:54:24 +01001916 props.Srcs = append(props.Srcs, module.properties.Srcs...)
Anton Hanssonf8ea3722021-09-16 14:24:13 +01001917 props.Srcs = append(props.Srcs, module.sdkLibraryProperties.Api_srcs...)
Paul Duffina18abc22020-05-16 18:54:24 +01001918 props.Sdk_version = module.deviceProperties.Sdk_version
Jihoon Kang3198f3c2023-01-26 08:08:52 +00001919 props.Api_surface = &apiScope.name
Paul Duffina18abc22020-05-16 18:54:24 +01001920 props.System_modules = module.deviceProperties.System_modules
Jiyong Parkc678ad32018-04-10 13:07:10 +09001921 props.Installable = proptools.BoolPtr(false)
Sundong Ahne6f0b052018-06-05 16:46:14 +09001922 // A droiddoc module has only one Libs property and doesn't distinguish between
1923 // shared libs and static libs. So we need to add both of these libs to Libs property.
Paul Duffina18abc22020-05-16 18:54:24 +01001924 props.Libs = module.properties.Libs
1925 props.Libs = append(props.Libs, module.properties.Static_libs...)
Nikita Ioffed732da72022-11-21 12:38:25 +00001926 props.Libs = append(props.Libs, module.sdkLibraryProperties.Stub_only_libs...)
Mark White9421c4c2023-08-10 00:07:03 +00001927 props.Libs = append(props.Libs, module.scopeToProperties[apiScope].Libs...)
Paul Duffina18abc22020-05-16 18:54:24 +01001928 props.Aidl.Include_dirs = module.deviceProperties.Aidl.Include_dirs
1929 props.Aidl.Local_include_dirs = module.deviceProperties.Aidl.Local_include_dirs
1930 props.Java_version = module.properties.Java_version
Jiyong Parkc678ad32018-04-10 13:07:10 +09001931
Paul Duffine22c2ab2020-05-20 19:35:27 +01001932 props.Annotations_enabled = module.sdkLibraryProperties.Annotations_enabled
Sundong Ahn054b19a2018-10-19 13:46:09 +09001933 props.Merge_annotations_dirs = module.sdkLibraryProperties.Merge_annotations_dirs
1934 props.Merge_inclusion_annotations_dirs = module.sdkLibraryProperties.Merge_inclusion_annotations_dirs
Jihoon Kang6592e872023-12-19 01:13:16 +00001935 props.Aconfig_declarations = module.sdkLibraryProperties.Aconfig_declarations
Sundong Ahn054b19a2018-10-19 13:46:09 +09001936
Paul Duffin6d0886e2020-04-07 18:49:53 +01001937 droidstubsArgs := []string{}
Paul Duffin235ffff2019-12-24 10:41:30 +00001938 if len(module.sdkLibraryProperties.Api_packages) != 0 {
Paul Duffin6d0886e2020-04-07 18:49:53 +01001939 droidstubsArgs = append(droidstubsArgs, "--stub-packages "+strings.Join(module.sdkLibraryProperties.Api_packages, ":"))
Paul Duffin235ffff2019-12-24 10:41:30 +00001940 }
Paul Duffin6d0886e2020-04-07 18:49:53 +01001941 droidstubsArgs = append(droidstubsArgs, module.sdkLibraryProperties.Droiddoc_options...)
Anton Hanssonfd1c0d22023-11-02 15:18:09 +00001942 disabledWarnings := []string{"HiddenSuperclass"}
1943 if proptools.BoolDefault(module.sdkLibraryProperties.Api_lint.Legacy_errors_allowed, true) {
1944 disabledWarnings = append(disabledWarnings,
1945 "BroadcastBehavior",
1946 "DeprecationMismatch",
1947 "MissingPermission",
1948 "SdkConstant",
1949 "Todo",
1950 )
Paul Duffin235ffff2019-12-24 10:41:30 +00001951 }
Paul Duffin6d0886e2020-04-07 18:49:53 +01001952 droidstubsArgs = append(droidstubsArgs, android.JoinWithPrefix(disabledWarnings, "--hide "))
Sundong Ahnfb2721f2018-09-17 13:23:09 +09001953
Paul Duffin6877e6d2020-09-25 19:59:14 +01001954 // Output Javadoc comments for public scope.
1955 if apiScope == apiScopePublic {
1956 props.Output_javadoc_comments = proptools.BoolPtr(true)
1957 }
1958
Paul Duffin1fb487d2020-04-07 18:50:10 +01001959 // Add in scope specific arguments.
Paul Duffin0ff08bd2020-04-29 13:30:54 +01001960 droidstubsArgs = append(droidstubsArgs, scopeSpecificDroidstubsArgs...)
Paul Duffin11512472019-02-11 15:55:17 +00001961 props.Arg_files = module.sdkLibraryProperties.Droiddoc_option_files
Paul Duffin6d0886e2020-04-07 18:49:53 +01001962 props.Args = proptools.StringPtr(strings.Join(droidstubsArgs, " "))
Jiyong Parkc678ad32018-04-10 13:07:10 +09001963
Paul Duffin15f34ef2020-07-20 18:04:44 +01001964 // List of APIs identified from the provided source files are created. They are later
1965 // compared against to the not-yet-released (a.k.a current) list of APIs and to the
1966 // last-released (a.k.a numbered) list of API.
1967 currentApiFileName := apiScope.apiFilePrefix + "current.txt"
1968 removedApiFileName := apiScope.apiFilePrefix + "removed.txt"
1969 apiDir := module.getApiDir()
1970 currentApiFileName = path.Join(apiDir, currentApiFileName)
1971 removedApiFileName = path.Join(apiDir, removedApiFileName)
Jiyong Parkc678ad32018-04-10 13:07:10 +09001972
Paul Duffin15f34ef2020-07-20 18:04:44 +01001973 // check against the not-yet-release API
1974 props.Check_api.Current.Api_file = proptools.StringPtr(currentApiFileName)
1975 props.Check_api.Current.Removed_api_file = proptools.StringPtr(removedApiFileName)
Jiyong Park58c518b2018-05-12 22:29:12 +09001976
Paul Duffin958806b2022-05-16 13:10:47 +00001977 if module.compareAgainstLatestApi(apiScope) {
Paul Duffin15f34ef2020-07-20 18:04:44 +01001978 // check against the latest released API
1979 latestApiFilegroupName := proptools.StringPtr(module.latestApiFilegroupName(apiScope))
Anton Hanssone87b03d2020-12-21 15:29:34 +00001980 props.Previous_api = latestApiFilegroupName
Paul Duffin15f34ef2020-07-20 18:04:44 +01001981 props.Check_api.Last_released.Api_file = latestApiFilegroupName
1982 props.Check_api.Last_released.Removed_api_file = proptools.StringPtr(
1983 module.latestRemovedApiFilegroupName(apiScope))
Jaewoong Jung1a97ee02021-03-09 13:25:02 -08001984 props.Check_api.Last_released.Baseline_file = proptools.StringPtr(
1985 module.latestIncompatibilitiesFilegroupName(apiScope))
Paul Duffin160fe412020-05-10 19:32:20 +01001986
Paul Duffin15f34ef2020-07-20 18:04:44 +01001987 if proptools.Bool(module.sdkLibraryProperties.Api_lint.Enabled) {
1988 // Enable api lint.
1989 props.Check_api.Api_lint.Enabled = proptools.BoolPtr(true)
1990 props.Check_api.Api_lint.New_since = latestApiFilegroupName
Paul Duffin160fe412020-05-10 19:32:20 +01001991
Paul Duffin15f34ef2020-07-20 18:04:44 +01001992 // If it exists then pass a lint-baseline.txt through to droidstubs.
1993 baselinePath := path.Join(apiDir, apiScope.apiFilePrefix+"lint-baseline.txt")
1994 baselinePathRelativeToRoot := path.Join(mctx.ModuleDir(), baselinePath)
1995 paths, err := mctx.GlobWithDeps(baselinePathRelativeToRoot, nil)
1996 if err != nil {
1997 mctx.ModuleErrorf("error checking for presence of %s: %s", baselinePathRelativeToRoot, err)
1998 }
1999 if len(paths) == 1 {
2000 props.Check_api.Api_lint.Baseline_file = proptools.StringPtr(baselinePath)
2001 } else if len(paths) != 0 {
2002 mctx.ModuleErrorf("error checking for presence of %s: expected one path, found: %v", baselinePathRelativeToRoot, paths)
Paul Duffin160fe412020-05-10 19:32:20 +01002003 }
2004 }
Paul Duffin15f34ef2020-07-20 18:04:44 +01002005 }
Jiyong Park58c518b2018-05-12 22:29:12 +09002006
Paul Duffin15f34ef2020-07-20 18:04:44 +01002007 if !Bool(module.sdkLibraryProperties.No_dist) {
Paul Duffin040e9062020-11-23 17:41:36 +00002008 // Dist the api txt and removed api txt artifacts for sdk builds.
2009 distDir := proptools.StringPtr(path.Join(module.apiDistPath(apiScope), "api"))
Jihoon Kang02168052024-03-20 00:44:54 +00002010 stubsTypeTagPrefix := ""
2011 if mctx.Config().ReleaseHiddenApiExportableStubs() {
2012 stubsTypeTagPrefix = ".exportable"
2013 }
Paul Duffin040e9062020-11-23 17:41:36 +00002014 for _, p := range []struct {
2015 tag string
2016 pattern string
2017 }{
Jihoon Kangd1799f62024-02-20 23:01:38 +00002018 // "exportable" api files are copied to the dist directory instead of the
Jihoon Kang02168052024-03-20 00:44:54 +00002019 // "everything" api files when "RELEASE_HIDDEN_API_EXPORTABLE_STUBS" build flag
2020 // is set. Otherwise, the "everything" api files are copied to the dist directory.
2021 {tag: "%s.api.txt", pattern: "%s.txt"},
2022 {tag: "%s.removed-api.txt", pattern: "%s-removed.txt"},
Paul Duffin040e9062020-11-23 17:41:36 +00002023 } {
2024 props.Dists = append(props.Dists, android.Dist{
2025 Targets: []string{"sdk", "win_sdk"},
2026 Dir: distDir,
2027 Dest: proptools.StringPtr(fmt.Sprintf(p.pattern, module.distStem())),
Jihoon Kang02168052024-03-20 00:44:54 +00002028 Tag: proptools.StringPtr(fmt.Sprintf(p.tag, stubsTypeTagPrefix)),
Paul Duffin040e9062020-11-23 17:41:36 +00002029 })
2030 }
Anton Hansson5fd5d242020-03-27 19:43:19 +00002031 }
2032
Spandan Das2cc80ba2023-10-27 17:21:52 +00002033 mctx.CreateModule(DroidstubsFactory, &props, module.sdkComponentPropertiesForChildLibrary()).(*Droidstubs).CallHookIfAvailable(mctx)
Jiyong Parkc678ad32018-04-10 13:07:10 +09002034}
2035
Jihoon Kangb0f4c022024-08-06 00:15:25 +00002036func (module *SdkLibrary) createApiLibrary(mctx android.DefaultableHookContext, apiScope *apiScope) {
Jihoon Kang1c92c3e2023-03-23 17:44:51 +00002037 props := struct {
Jihoon Kangb0f4c022024-08-06 00:15:25 +00002038 Name *string
2039 Visibility []string
2040 Api_contributions []string
2041 Libs []string
2042 Static_libs []string
2043 System_modules *string
2044 Enable_validation *bool
2045 Stubs_type *string
2046 Sdk_version *string
2047 Previous_api *string
Jihoon Kang1c92c3e2023-03-23 17:44:51 +00002048 }{}
2049
2050 props.Name = proptools.StringPtr(module.apiLibraryModuleName(apiScope))
Jihoon Kang786df932023-09-07 01:18:31 +00002051 props.Visibility = []string{"//visibility:override", "//visibility:private"}
Jihoon Kang1c92c3e2023-03-23 17:44:51 +00002052
2053 apiContributions := []string{}
2054
2055 // Api surfaces are not independent of each other, but have subset relationships,
2056 // and so does the api files. To generate from-text stubs for api surfaces other than public,
2057 // all subset api domains' api_contriubtions must be added as well.
2058 scope := apiScope
2059 for scope != nil {
2060 apiContributions = append(apiContributions, module.stubsSourceModuleName(scope)+".api.contribution")
2061 scope = scope.extends
2062 }
Jihoon Kang0c705a42023-08-02 06:44:57 +00002063 if apiScope == apiScopePublic {
2064 additionalApiContribution := module.apiLibraryAdditionalApiContribution()
2065 if additionalApiContribution != "" {
2066 apiContributions = append(apiContributions, additionalApiContribution)
2067 }
2068 }
Jihoon Kang1c92c3e2023-03-23 17:44:51 +00002069
2070 props.Api_contributions = apiContributions
Jihoon Kangb0f4c022024-08-06 00:15:25 +00002071
2072 // Ensure that stub-annotations is added to the classpath before any other libs
2073 props.Libs = []string{"stub-annotations"}
2074 props.Libs = append(props.Libs, module.properties.Libs...)
2075 props.Libs = append(props.Libs, module.properties.Static_libs...)
Jihoon Kang1c92c3e2023-03-23 17:44:51 +00002076 props.Libs = append(props.Libs, module.sdkLibraryProperties.Stub_only_libs...)
Mark White9421c4c2023-08-10 00:07:03 +00002077 props.Libs = append(props.Libs, module.scopeToProperties[apiScope].Libs...)
Jihoon Kang1c92c3e2023-03-23 17:44:51 +00002078 props.Static_libs = module.sdkLibraryProperties.Stub_only_static_libs
Jihoon Kangd30ac8a2023-10-09 18:00:17 +00002079
Jihoon Kang4ec24872023-10-05 17:26:09 +00002080 props.System_modules = module.deviceProperties.System_modules
Jihoon Kang063ec002023-06-28 01:16:23 +00002081 props.Enable_validation = proptools.BoolPtr(true)
Jihoon Kang5d701272024-02-15 21:53:49 +00002082 props.Stubs_type = proptools.StringPtr("everything")
Jihoon Kang4ec24872023-10-05 17:26:09 +00002083
Jihoon Kangb0f4c022024-08-06 00:15:25 +00002084 if module.deviceProperties.Sdk_version != nil {
2085 props.Sdk_version = module.deviceProperties.Sdk_version
2086 }
2087
2088 if module.compareAgainstLatestApi(apiScope) {
2089 // check against the latest released API
2090 latestApiFilegroupName := proptools.StringPtr(module.latestApiFilegroupName(apiScope))
2091 props.Previous_api = latestApiFilegroupName
2092 }
2093
Spandan Das2cc80ba2023-10-27 17:21:52 +00002094 mctx.CreateModule(ApiLibraryFactory, &props, module.sdkComponentPropertiesForChildLibrary())
Jihoon Kang1c92c3e2023-03-23 17:44:51 +00002095}
2096
Jihoon Kang02168052024-03-20 00:44:54 +00002097func (module *SdkLibrary) topLevelStubsLibraryProps(mctx android.DefaultableHookContext, apiScope *apiScope, doDist bool) libraryProperties {
Jihoon Kangfa4a90d2023-12-20 02:53:38 +00002098 props := libraryProperties{}
2099
Jihoon Kang1147b312023-06-08 23:25:57 +00002100 props.Visibility = childModuleVisibility(module.sdkLibraryProperties.Stubs_library_visibility)
2101 sdkVersion := module.sdkVersionForStubsLibrary(mctx, apiScope)
2102 props.Sdk_version = proptools.StringPtr(sdkVersion)
2103
Jihoon Kang1147b312023-06-08 23:25:57 +00002104 props.System_modules = module.deviceProperties.System_modules
2105
Jihoon Kang1147b312023-06-08 23:25:57 +00002106 // The imports need to be compiled to dex if the java_sdk_library requests it.
2107 compileDex := module.dexProperties.Compile_dex
2108 if module.stubLibrariesCompiledForDex() {
2109 compileDex = proptools.BoolPtr(true)
2110 }
2111 props.Compile_dex = compileDex
2112
Jihoon Kang02168052024-03-20 00:44:54 +00002113 if !Bool(module.sdkLibraryProperties.No_dist) && doDist {
2114 props.Dist.Targets = []string{"sdk", "win_sdk"}
2115 props.Dist.Dest = proptools.StringPtr(fmt.Sprintf("%v.jar", module.distStem()))
2116 props.Dist.Dir = proptools.StringPtr(module.apiDistPath(apiScope))
2117 props.Dist.Tag = proptools.StringPtr(".jar")
2118 }
2119
Jihoon Kangfa4a90d2023-12-20 02:53:38 +00002120 return props
2121}
2122
2123func (module *SdkLibrary) createTopLevelStubsLibrary(
Jihoon Kangb0f4c022024-08-06 00:15:25 +00002124 mctx android.DefaultableHookContext, apiScope *apiScope) {
Jihoon Kangfa4a90d2023-12-20 02:53:38 +00002125
Jihoon Kang02168052024-03-20 00:44:54 +00002126 // Dist the "everything" stubs when the RELEASE_HIDDEN_API_EXPORTABLE_STUBS build flag is false
2127 doDist := !mctx.Config().ReleaseHiddenApiExportableStubs()
2128 props := module.topLevelStubsLibraryProps(mctx, apiScope, doDist)
Jihoon Kangfa4a90d2023-12-20 02:53:38 +00002129 props.Name = proptools.StringPtr(module.stubsLibraryModuleName(apiScope))
2130
2131 // Add the stub compiling java_library/java_api_library as static lib based on build config
2132 staticLib := module.sourceStubsLibraryModuleName(apiScope)
Jihoon Kangb0f4c022024-08-06 00:15:25 +00002133 if mctx.Config().BuildFromTextStub() && module.ModuleBuildFromTextStubs() {
Jihoon Kangfa4a90d2023-12-20 02:53:38 +00002134 staticLib = module.apiLibraryModuleName(apiScope)
2135 }
2136 props.Static_libs = append(props.Static_libs, staticLib)
2137
2138 mctx.CreateModule(LibraryFactory, &props, module.sdkComponentPropertiesForChildLibrary())
2139}
2140
2141func (module *SdkLibrary) createTopLevelExportableStubsLibrary(
2142 mctx android.DefaultableHookContext, apiScope *apiScope) {
2143
Jihoon Kang02168052024-03-20 00:44:54 +00002144 // Dist the "exportable" stubs when the RELEASE_HIDDEN_API_EXPORTABLE_STUBS build flag is true
2145 doDist := mctx.Config().ReleaseHiddenApiExportableStubs()
2146 props := module.topLevelStubsLibraryProps(mctx, apiScope, doDist)
Jihoon Kangfa4a90d2023-12-20 02:53:38 +00002147 props.Name = proptools.StringPtr(module.exportableStubsLibraryModuleName(apiScope))
2148
Jihoon Kangfa4a90d2023-12-20 02:53:38 +00002149 staticLib := module.exportableSourceStubsLibraryModuleName(apiScope)
2150 props.Static_libs = append(props.Static_libs, staticLib)
2151
Jihoon Kang1147b312023-06-08 23:25:57 +00002152 mctx.CreateModule(LibraryFactory, &props, module.sdkComponentPropertiesForChildLibrary())
2153}
2154
Paul Duffin958806b2022-05-16 13:10:47 +00002155func (module *SdkLibrary) compareAgainstLatestApi(apiScope *apiScope) bool {
2156 return !(apiScope.unstable || module.sdkLibraryProperties.Unsafe_ignore_missing_latest_api)
2157}
2158
Paul Duffinea8f8082021-06-24 13:25:57 +01002159// Implements android.ApexModule
Jooyung Han5e9013b2020-03-10 06:23:13 +09002160func (module *SdkLibrary) DepIsInSameApex(mctx android.BaseModuleContext, dep android.Module) bool {
2161 depTag := mctx.OtherModuleDependencyTag(dep)
2162 if depTag == xmlPermissionsFileTag {
2163 return true
2164 }
Jihoon Kanga3a05462024-04-05 00:36:44 +00002165 if dep.Name() == module.implLibraryModuleName() {
2166 return true
2167 }
Jooyung Han5e9013b2020-03-10 06:23:13 +09002168 return module.Library.DepIsInSameApex(mctx, dep)
2169}
2170
Paul Duffinea8f8082021-06-24 13:25:57 +01002171// Implements android.ApexModule
2172func (module *SdkLibrary) UniqueApexVariations() bool {
2173 return module.uniqueApexVariations()
2174}
2175
Jihoon Kangb0f4c022024-08-06 00:15:25 +00002176func (module *SdkLibrary) ModuleBuildFromTextStubs() bool {
2177 return proptools.BoolDefault(module.sdkLibraryProperties.Build_from_text_stub, true)
Jihoon Kang80456fd2023-11-15 19:22:14 +00002178}
2179
Jiyong Parkc678ad32018-04-10 13:07:10 +09002180// Creates the xml file that publicizes the runtime library
Paul Duffinf0229202020-04-29 16:47:28 +01002181func (module *SdkLibrary) createXmlFile(mctx android.DefaultableHookContext) {
Spandan Das8c9ae7e2023-03-03 21:20:36 +00002182 moduleMinApiLevel := module.Library.MinSdkVersion(mctx)
Pedro Loureiroc3621422021-09-28 15:40:23 +00002183 var moduleMinApiLevelStr = moduleMinApiLevel.String()
2184 if moduleMinApiLevel == android.NoneApiLevel {
2185 moduleMinApiLevelStr = "current"
2186 }
Jiyong Parke3833882020-02-17 17:28:10 +09002187 props := struct {
Pedro Loureiroc3621422021-09-28 15:40:23 +00002188 Name *string
2189 Lib_name *string
2190 Apex_available []string
2191 On_bootclasspath_since *string
2192 On_bootclasspath_before *string
2193 Min_device_sdk *string
2194 Max_device_sdk *string
2195 Sdk_library_min_api_level *string
Jamie Garsidee570ace2023-11-27 12:07:36 +00002196 Uses_libs_dependencies []string
Jiyong Parke3833882020-02-17 17:28:10 +09002197 }{
Pedro Loureiroc3621422021-09-28 15:40:23 +00002198 Name: proptools.StringPtr(module.xmlPermissionsModuleName()),
2199 Lib_name: proptools.StringPtr(module.BaseModuleName()),
2200 Apex_available: module.ApexProperties.Apex_available,
2201 On_bootclasspath_since: module.commonSdkLibraryProperties.On_bootclasspath_since,
2202 On_bootclasspath_before: module.commonSdkLibraryProperties.On_bootclasspath_before,
2203 Min_device_sdk: module.commonSdkLibraryProperties.Min_device_sdk,
2204 Max_device_sdk: module.commonSdkLibraryProperties.Max_device_sdk,
2205 Sdk_library_min_api_level: &moduleMinApiLevelStr,
Jamie Garsidee570ace2023-11-27 12:07:36 +00002206 Uses_libs_dependencies: module.usesLibraryProperties.Uses_libs,
Jiyong Parkc678ad32018-04-10 13:07:10 +09002207 }
Jiyong Parke3833882020-02-17 17:28:10 +09002208
Jiyong Parke3833882020-02-17 17:28:10 +09002209 mctx.CreateModule(sdkLibraryXmlFactory, &props)
Jiyong Parkc678ad32018-04-10 13:07:10 +09002210}
2211
Jiyong Parkf1691d22021-03-29 20:11:58 +09002212func PrebuiltJars(ctx android.BaseModuleContext, baseName string, s android.SdkSpec) android.Paths {
Jiyong Park54105c42021-03-31 18:17:53 +09002213 var ver android.ApiLevel
Jiyong Parkf1691d22021-03-29 20:11:58 +09002214 var kind android.SdkKind
2215 if s.UsePrebuilt(ctx) {
Jiyong Park54105c42021-03-31 18:17:53 +09002216 ver = s.ApiLevel
Jiyong Parkf1691d22021-03-29 20:11:58 +09002217 kind = s.Kind
Jiyong Parkc678ad32018-04-10 13:07:10 +09002218 } else {
Jiyong Park6a927c42020-01-21 02:03:43 +09002219 // We don't have prebuilt SDK for the specific sdkVersion.
2220 // Instead of breaking the build, fallback to use "system_current"
Jiyong Park54105c42021-03-31 18:17:53 +09002221 ver = android.FutureApiLevel
Jiyong Parkf1691d22021-03-29 20:11:58 +09002222 kind = android.SdkSystem
Sundong Ahn054b19a2018-10-19 13:46:09 +09002223 }
Jiyong Park6a927c42020-01-21 02:03:43 +09002224
2225 dir := filepath.Join("prebuilts", "sdk", ver.String(), kind.String())
Paul Duffin50061512020-01-21 16:31:05 +00002226 jar := filepath.Join(dir, baseName+".jar")
Sundong Ahn054b19a2018-10-19 13:46:09 +09002227 jarPath := android.ExistentPathForSource(ctx, jar)
Sundong Ahnae418ac2019-02-28 15:01:28 +09002228 if !jarPath.Valid() {
Colin Cross07c88562020-01-07 09:34:44 -08002229 if ctx.Config().AllowMissingDependencies() {
2230 return android.Paths{android.PathForSource(ctx, jar)}
2231 } else {
Jiyong Parkf1691d22021-03-29 20:11:58 +09002232 ctx.PropertyErrorf("sdk_library", "invalid sdk version %q, %q does not exist", s.Raw, jar)
Colin Cross07c88562020-01-07 09:34:44 -08002233 }
Sundong Ahnae418ac2019-02-28 15:01:28 +09002234 return nil
2235 }
Sundong Ahn054b19a2018-10-19 13:46:09 +09002236 return android.Paths{jarPath.Path()}
2237}
2238
Colin Crossaede88c2020-08-11 12:17:01 -07002239// 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 +01002240//
2241// If either this or the other module are on the platform then this will return
2242// false.
Colin Cross56a83212020-09-15 18:30:11 -07002243func withinSameApexesAs(ctx android.BaseModuleContext, other android.Module) bool {
Colin Crossff694a82023-12-13 15:54:49 -08002244 apexInfo, _ := android.ModuleProvider(ctx, android.ApexInfoProvider)
Colin Cross313aa542023-12-13 13:47:44 -08002245 otherApexInfo, _ := android.OtherModuleProvider(ctx, other, android.ApexInfoProvider)
Jiyong Parkab50b072021-05-12 17:13:56 +09002246 return len(otherApexInfo.InApexVariants) > 0 && reflect.DeepEqual(apexInfo.InApexVariants, otherApexInfo.InApexVariants)
Paul Duffin9b879592020-05-26 13:21:35 +01002247}
2248
Jihoon Kang8479dea2024-04-04 01:19:05 +00002249func (module *SdkLibrary) sdkJars(ctx android.BaseModuleContext, sdkVersion android.SdkSpec) android.Paths {
Jiyong Park932cdfe2020-05-28 00:19:53 +09002250 // If the client doesn't set sdk_version, but if this library prefers stubs over
2251 // the impl library, let's provide the widest API surface possible. To do so,
2252 // force override sdk_version to module_current so that the closest possible API
2253 // surface could be found in selectHeaderJarsForSdkVersion
Jiyong Parkf1691d22021-03-29 20:11:58 +09002254 if module.defaultsToStubs() && !sdkVersion.Specified() {
Jiyong Park92315372021-04-02 08:45:46 +09002255 sdkVersion = android.SdkSpecFrom(ctx, "module_current")
Jiyong Park932cdfe2020-05-28 00:19:53 +09002256 }
Paul Duffind1b3a922020-01-22 11:57:20 +00002257
Paul Duffindaaa3322020-05-26 18:13:57 +01002258 // Only provide access to the implementation library if it is actually built.
2259 if module.requiresRuntimeImplementationLibrary() {
2260 // Check any special cases for java_sdk_library.
2261 //
2262 // Only allow access to the implementation library in the following condition:
2263 // * No sdk_version specified on the referencing module.
Paul Duffin9b879592020-05-26 13:21:35 +01002264 // * The referencing module is in the same apex as this.
Jiyong Parkf1691d22021-03-29 20:11:58 +09002265 if sdkVersion.Kind == android.SdkPrivate || withinSameApexesAs(ctx, module) {
Jihoon Kang8479dea2024-04-04 01:19:05 +00002266 return module.implLibraryHeaderJars
Sundong Ahn054b19a2018-10-19 13:46:09 +09002267 }
Jiyong Parkc678ad32018-04-10 13:07:10 +09002268 }
Paul Duffinb05d4292020-05-20 12:19:10 +01002269
Paul Duffin23970f42020-05-20 14:20:02 +01002270 return module.selectHeaderJarsForSdkVersion(ctx, sdkVersion)
Jiyong Parkc678ad32018-04-10 13:07:10 +09002271}
2272
Sundong Ahn241cd372018-07-13 16:16:44 +09002273// to satisfy SdkLibraryDependency interface
Jiyong Parkf1691d22021-03-29 20:11:58 +09002274func (module *SdkLibrary) SdkHeaderJars(ctx android.BaseModuleContext, sdkVersion android.SdkSpec) android.Paths {
Jihoon Kang8479dea2024-04-04 01:19:05 +00002275 return module.sdkJars(ctx, sdkVersion)
Sundong Ahn241cd372018-07-13 16:16:44 +09002276}
2277
Colin Cross571cccf2019-02-04 11:22:08 -08002278var javaSdkLibrariesKey = android.NewOnceKey("javaSdkLibraries")
2279
Jiyong Park82484c02018-04-23 21:41:26 +09002280func javaSdkLibraries(config android.Config) *[]string {
Colin Cross571cccf2019-02-04 11:22:08 -08002281 return config.Once(javaSdkLibrariesKey, func() interface{} {
Jiyong Park82484c02018-04-23 21:41:26 +09002282 return &[]string{}
2283 }).(*[]string)
2284}
2285
Paul Duffin749f98f2019-12-30 17:23:46 +00002286func (module *SdkLibrary) getApiDir() string {
2287 return proptools.StringDefault(module.sdkLibraryProperties.Api_dir, "api")
2288}
2289
Jiyong Parkc678ad32018-04-10 13:07:10 +09002290// For a java_sdk_library module, create internal modules for stubs, docs,
2291// runtime libs and xml file. If requested, the stubs and docs are created twice
2292// once for public API level and once for system API level
Paul Duffinf0229202020-04-29 16:47:28 +01002293func (module *SdkLibrary) CreateInternalModules(mctx android.DefaultableHookContext) {
2294 // If the module has been disabled then don't create any child modules.
Cole Fausta963b942024-04-11 17:43:00 -07002295 if !module.Enabled(mctx) {
Paul Duffinf0229202020-04-29 16:47:28 +01002296 return
2297 }
2298
Paul Duffina18abc22020-05-16 18:54:24 +01002299 if len(module.properties.Srcs) == 0 {
Inseob Kimc0907f12019-02-08 21:00:45 +09002300 mctx.PropertyErrorf("srcs", "java_sdk_library must specify srcs")
Jooyung Han58f26ab2019-12-18 15:34:32 +09002301 return
Inseob Kimc0907f12019-02-08 21:00:45 +09002302 }
2303
Paul Duffin37e0b772019-12-30 17:20:10 +00002304 // If this builds against standard libraries (i.e. is not part of the core libraries)
Paul Duffin4f5c1ef2020-11-19 14:53:43 +00002305 // then assume it provides both system and test apis.
Jiyong Parkf1691d22021-03-29 20:11:58 +09002306 sdkDep := decodeSdkDep(mctx, android.SdkContext(&module.Library))
Paul Duffin37e0b772019-12-30 17:20:10 +00002307 hasSystemAndTestApis := sdkDep.hasStandardLibs()
Paul Duffin3375e352020-04-28 10:44:03 +01002308 module.sdkLibraryProperties.Generate_system_and_test_apis = hasSystemAndTestApis
Paul Duffin4f5c1ef2020-11-19 14:53:43 +00002309
Jaewoong Jung18aefc12020-12-21 09:11:10 -08002310 missingCurrentApi := false
Inseob Kim8098faa2019-03-18 10:19:51 +09002311
Paul Duffin3375e352020-04-28 10:44:03 +01002312 generatedScopes := module.getGeneratedApiScopes(mctx)
Paul Duffind1b3a922020-01-22 11:57:20 +00002313
Paul Duffin749f98f2019-12-30 17:23:46 +00002314 apiDir := module.getApiDir()
Paul Duffin3375e352020-04-28 10:44:03 +01002315 for _, scope := range generatedScopes {
Inseob Kim8098faa2019-03-18 10:19:51 +09002316 for _, api := range []string{"current.txt", "removed.txt"} {
Paul Duffind1b3a922020-01-22 11:57:20 +00002317 path := path.Join(mctx.ModuleDir(), apiDir, scope.apiFilePrefix+api)
Inseob Kim8098faa2019-03-18 10:19:51 +09002318 p := android.ExistentPathForSource(mctx, path)
2319 if !p.Valid() {
Colin Cross18f840c2021-05-20 17:56:54 -07002320 if mctx.Config().AllowMissingDependencies() {
2321 mctx.AddMissingDependencies([]string{path})
2322 } else {
2323 mctx.ModuleErrorf("Current api file %#v doesn't exist", path)
2324 missingCurrentApi = true
2325 }
Inseob Kim8098faa2019-03-18 10:19:51 +09002326 }
2327 }
2328 }
2329
Jaewoong Jung18aefc12020-12-21 09:11:10 -08002330 if missingCurrentApi {
Inseob Kim8098faa2019-03-18 10:19:51 +09002331 script := "build/soong/scripts/gen-java-current-api-files.sh"
2332 p := android.ExistentPathForSource(mctx, script)
2333
2334 if !p.Valid() {
2335 panic(fmt.Sprintf("script file %s doesn't exist", script))
2336 }
2337
2338 mctx.ModuleErrorf("One or more current api files are missing. "+
2339 "You can update them by:\n"+
Paul Duffin37e0b772019-12-30 17:20:10 +00002340 "%s %q %s && m update-api",
Paul Duffind1b3a922020-01-22 11:57:20 +00002341 script, filepath.Join(mctx.ModuleDir(), apiDir),
Paul Duffin3375e352020-04-28 10:44:03 +01002342 strings.Join(generatedScopes.Strings(func(s *apiScope) string { return s.apiFilePrefix }), " "))
Inseob Kim8098faa2019-03-18 10:19:51 +09002343 return
2344 }
2345
Paul Duffin3375e352020-04-28 10:44:03 +01002346 for _, scope := range generatedScopes {
Paul Duffin15f34ef2020-07-20 18:04:44 +01002347 // Use the stubs source name for legacy reasons.
2348 module.createStubsSourcesAndApi(mctx, scope, module.stubsSourceModuleName(scope), scope.droidstubsArgs)
Paul Duffin0ff08bd2020-04-29 13:30:54 +01002349
Paul Duffind1b3a922020-01-22 11:57:20 +00002350 module.createStubsLibrary(mctx, scope)
Jihoon Kangfa4a90d2023-12-20 02:53:38 +00002351 module.createExportableStubsLibrary(mctx, scope)
Jihoon Kang1c92c3e2023-03-23 17:44:51 +00002352
Jihoon Kangb0f4c022024-08-06 00:15:25 +00002353 if mctx.Config().BuildFromTextStub() && module.ModuleBuildFromTextStubs() {
2354 module.createApiLibrary(mctx, scope)
Jihoon Kang0c705a42023-08-02 06:44:57 +00002355 }
Jihoon Kangb0f4c022024-08-06 00:15:25 +00002356 module.createTopLevelStubsLibrary(mctx, scope)
Jihoon Kangfa4a90d2023-12-20 02:53:38 +00002357 module.createTopLevelExportableStubsLibrary(mctx, scope)
Inseob Kimc0907f12019-02-08 21:00:45 +09002358 }
2359
Paul Duffindfa131e2020-05-15 20:37:11 +01002360 if module.requiresRuntimeImplementationLibrary() {
Paul Duffin5df79302020-05-16 15:52:12 +01002361 // Create child module to create an implementation library.
2362 //
2363 // This temporarily creates a second implementation library that can be explicitly
2364 // referenced.
2365 //
2366 // TODO(b/156618935) - update comment once only one implementation library is created.
2367 module.createImplLibrary(mctx)
2368
Paul Duffindfa131e2020-05-15 20:37:11 +01002369 // Only create an XML permissions file that declares the library as being usable
2370 // as a shared library if required.
2371 if module.sharedLibrary() {
2372 module.createXmlFile(mctx)
2373 }
Paul Duffin43db9be2019-12-30 17:35:49 +00002374
2375 // record java_sdk_library modules so that they are exported to make
2376 javaSdkLibraries := javaSdkLibraries(mctx.Config())
2377 javaSdkLibrariesLock.Lock()
2378 defer javaSdkLibrariesLock.Unlock()
2379 *javaSdkLibraries = append(*javaSdkLibraries, module.BaseModuleName())
2380 }
Anton Hansson7f66efa2020-10-08 14:47:23 +01002381
Paul Duffin77590a82022-04-28 14:13:30 +00002382 // 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 +01002383 module.properties.Libs = append(module.properties.Libs, module.sdkLibraryProperties.Impl_only_libs...)
Paul Duffin77590a82022-04-28 14:13:30 +00002384 module.properties.Static_libs = append(module.properties.Static_libs, module.sdkLibraryProperties.Impl_only_static_libs...)
Inseob Kimc0907f12019-02-08 21:00:45 +09002385}
2386
2387func (module *SdkLibrary) InitSdkLibraryProperties() {
Colin Crossce6734e2020-06-15 16:09:53 -07002388 module.addHostAndDeviceProperties()
2389 module.AddProperties(&module.sdkLibraryProperties)
Sundong Ahn054b19a2018-10-19 13:46:09 +09002390
Paul Duffin71b33cc2021-06-23 11:39:47 +01002391 module.initSdkLibraryComponent(module)
Paul Duffin859fe962020-05-15 10:20:31 +01002392
Paul Duffina18abc22020-05-16 18:54:24 +01002393 module.properties.Installable = proptools.BoolPtr(true)
2394 module.deviceProperties.IsSDKLibrary = true
Inseob Kimc0907f12019-02-08 21:00:45 +09002395}
Sundong Ahn054b19a2018-10-19 13:46:09 +09002396
Paul Duffindfa131e2020-05-15 20:37:11 +01002397func (module *SdkLibrary) requiresRuntimeImplementationLibrary() bool {
2398 return !proptools.Bool(module.sdkLibraryProperties.Api_only)
2399}
2400
Jiyong Park932cdfe2020-05-28 00:19:53 +09002401func (module *SdkLibrary) defaultsToStubs() bool {
2402 return proptools.Bool(module.sdkLibraryProperties.Default_to_stubs)
2403}
2404
Paul Duffin1b1e8062020-05-08 13:44:43 +01002405// Defines how to name the individual component modules the sdk library creates.
2406type sdkLibraryComponentNamingScheme interface {
2407 stubsLibraryModuleName(scope *apiScope, baseName string) string
2408
2409 stubsSourceModuleName(scope *apiScope, baseName string) string
Jihoon Kang1c92c3e2023-03-23 17:44:51 +00002410
2411 apiLibraryModuleName(scope *apiScope, baseName string) string
Jihoon Kang1147b312023-06-08 23:25:57 +00002412
Jihoon Kangfa4a90d2023-12-20 02:53:38 +00002413 sourceStubsLibraryModuleName(scope *apiScope, baseName string) string
2414
2415 exportableStubsLibraryModuleName(scope *apiScope, baseName string) string
2416
2417 exportableSourceStubsLibraryModuleName(scope *apiScope, baseName string) string
Paul Duffin1b1e8062020-05-08 13:44:43 +01002418}
2419
2420type defaultNamingScheme struct {
2421}
2422
2423func (s *defaultNamingScheme) stubsLibraryModuleName(scope *apiScope, baseName string) string {
2424 return scope.stubsLibraryModuleName(baseName)
2425}
2426
2427func (s *defaultNamingScheme) stubsSourceModuleName(scope *apiScope, baseName string) string {
2428 return scope.stubsSourceModuleName(baseName)
2429}
2430
Jihoon Kang1c92c3e2023-03-23 17:44:51 +00002431func (s *defaultNamingScheme) apiLibraryModuleName(scope *apiScope, baseName string) string {
2432 return scope.apiLibraryModuleName(baseName)
2433}
2434
Jihoon Kangfa4a90d2023-12-20 02:53:38 +00002435func (s *defaultNamingScheme) sourceStubsLibraryModuleName(scope *apiScope, baseName string) string {
Jihoon Kang1147b312023-06-08 23:25:57 +00002436 return scope.sourceStubLibraryModuleName(baseName)
2437}
2438
Jihoon Kangfa4a90d2023-12-20 02:53:38 +00002439func (s *defaultNamingScheme) exportableStubsLibraryModuleName(scope *apiScope, baseName string) string {
2440 return scope.exportableStubsLibraryModuleName(baseName)
2441}
2442
2443func (s *defaultNamingScheme) exportableSourceStubsLibraryModuleName(scope *apiScope, baseName string) string {
2444 return scope.exportableSourceStubsLibraryModuleName(baseName)
2445}
2446
Paul Duffin1b1e8062020-05-08 13:44:43 +01002447var _ sdkLibraryComponentNamingScheme = (*defaultNamingScheme)(nil)
2448
Jihoon Kangfa4a90d2023-12-20 02:53:38 +00002449func hasStubsLibrarySuffix(name string, apiScope *apiScope) bool {
2450 return strings.HasSuffix(name, apiScope.stubsLibraryModuleNameSuffix()) ||
2451 strings.HasSuffix(name, apiScope.exportableStubsLibraryModuleNameSuffix())
2452}
2453
Jaewoong Jungbc15e3a2021-03-10 17:02:43 -08002454func moduleStubLinkType(name string) (stub bool, ret sdkLinkType) {
Jihoon Kang1147b312023-06-08 23:25:57 +00002455 name = strings.TrimSuffix(name, ".from-source")
2456
Anton Hansson2d0c1942020-05-25 12:20:51 +01002457 // This suffix-based approach is fragile and could potentially mis-trigger.
2458 // TODO(b/155164730): Clean this up when modules no longer reference sdk_lib stubs directly.
Jihoon Kangfa4a90d2023-12-20 02:53:38 +00002459 if hasStubsLibrarySuffix(name, apiScopePublic) {
Anton Hansson08f476b2021-04-07 15:32:19 +01002460 if name == "hwbinder.stubs" || name == "libcore_private.stubs" {
2461 // Due to a previous bug, these modules were not considered stubs, so we retain that.
2462 return false, javaPlatform
2463 }
Anton Hansson2d0c1942020-05-25 12:20:51 +01002464 return true, javaSdk
2465 }
Jihoon Kangfa4a90d2023-12-20 02:53:38 +00002466 if hasStubsLibrarySuffix(name, apiScopeSystem) {
Anton Hansson2d0c1942020-05-25 12:20:51 +01002467 return true, javaSystem
2468 }
Jihoon Kangfa4a90d2023-12-20 02:53:38 +00002469 if hasStubsLibrarySuffix(name, apiScopeModuleLib) {
Anton Hansson2d0c1942020-05-25 12:20:51 +01002470 return true, javaModule
2471 }
Jihoon Kangfa4a90d2023-12-20 02:53:38 +00002472 if hasStubsLibrarySuffix(name, apiScopeTest) {
Anton Hansson2d0c1942020-05-25 12:20:51 +01002473 return true, javaSystem
2474 }
Jihoon Kangfa4a90d2023-12-20 02:53:38 +00002475 if hasStubsLibrarySuffix(name, apiScopeSystemServer) {
Jihoon Kang1147b312023-06-08 23:25:57 +00002476 return true, javaSystemServer
2477 }
Anton Hansson2d0c1942020-05-25 12:20:51 +01002478 return false, javaPlatform
2479}
2480
Jaewoong Jung4f158ee2019-07-11 10:05:35 -07002481// java_sdk_library is a special Java library that provides optional platform APIs to apps.
2482// In practice, it can be viewed as a combination of several modules: 1) stubs library that clients
2483// are linked against to, 2) droiddoc module that internally generates API stubs source files,
2484// 3) the real runtime shared library that implements the APIs, and 4) XML file for adding
2485// the runtime lib to the classpath at runtime if requested via <uses-library>.
Inseob Kimc0907f12019-02-08 21:00:45 +09002486func SdkLibraryFactory() android.Module {
2487 module := &SdkLibrary{}
Paul Duffinc3091c82020-05-08 14:16:20 +01002488
2489 // Initialize information common between source and prebuilt.
Paul Duffin71b33cc2021-06-23 11:39:47 +01002490 module.initCommon(module)
Paul Duffinc3091c82020-05-08 14:16:20 +01002491
Inseob Kimc0907f12019-02-08 21:00:45 +09002492 module.InitSdkLibraryProperties()
Jooyung Han58f26ab2019-12-18 15:34:32 +09002493 android.InitApexModule(module)
Sundong Ahn054b19a2018-10-19 13:46:09 +09002494 InitJavaModule(module, android.HostAndDeviceSupported)
Paul Duffin3375e352020-04-28 10:44:03 +01002495
2496 // Initialize the map from scope to scope specific properties.
2497 scopeToProperties := make(map[*apiScope]*ApiScopeProperties)
Jihoon Kang98aa8fa2024-06-07 11:06:57 +00002498 for _, scope := range AllApiScopes {
Paul Duffin3375e352020-04-28 10:44:03 +01002499 scopeToProperties[scope] = scope.scopeSpecificProperties(module)
2500 }
2501 module.scopeToProperties = scopeToProperties
2502
Paul Duffin4911a892020-04-29 23:35:13 +01002503 // Add the properties containing visibility rules so that they are checked.
Paul Duffin5df79302020-05-16 15:52:12 +01002504 android.AddVisibilityProperty(module, "impl_library_visibility", &module.sdkLibraryProperties.Impl_library_visibility)
Paul Duffin4911a892020-04-29 23:35:13 +01002505 android.AddVisibilityProperty(module, "stubs_library_visibility", &module.sdkLibraryProperties.Stubs_library_visibility)
2506 android.AddVisibilityProperty(module, "stubs_source_visibility", &module.sdkLibraryProperties.Stubs_source_visibility)
2507
Paul Duffin1b1e8062020-05-08 13:44:43 +01002508 module.SetDefaultableHook(func(ctx android.DefaultableHookContext) {
Paul Duffindfa131e2020-05-15 20:37:11 +01002509 // If no implementation is required then it cannot be used as a shared library
2510 // either.
2511 if !module.requiresRuntimeImplementationLibrary() {
2512 // If shared_library has been explicitly set to true then it is incompatible
2513 // with api_only: true.
2514 if proptools.Bool(module.commonSdkLibraryProperties.Shared_library) {
2515 ctx.PropertyErrorf("api_only/shared_library", "inconsistent settings, shared_library and api_only cannot both be true")
2516 }
2517 // Set shared_library: false.
2518 module.commonSdkLibraryProperties.Shared_library = proptools.BoolPtr(false)
2519 }
2520
Paul Duffin1b1e8062020-05-08 13:44:43 +01002521 if module.initCommonAfterDefaultsApplied(ctx) {
2522 module.CreateInternalModules(ctx)
2523 }
2524 })
Jiyong Parkc678ad32018-04-10 13:07:10 +09002525 return module
2526}
Colin Cross79c7c262019-04-17 11:11:46 -07002527
2528//
2529// SDK library prebuilts
2530//
2531
Paul Duffin56d44902020-01-31 13:36:25 +00002532// Properties associated with each api scope.
2533type sdkLibraryScopeProperties struct {
Colin Cross79c7c262019-04-17 11:11:46 -07002534 Jars []string `android:"path"`
2535
2536 Sdk_version *string
2537
Colin Cross79c7c262019-04-17 11:11:46 -07002538 // List of shared java libs that this module has dependencies to
2539 Libs []string
Paul Duffin3d1248c2020-04-09 00:10:17 +01002540
Paul Duffinc8782502020-04-29 20:45:27 +01002541 // The stubs source.
Paul Duffin3d1248c2020-04-09 00:10:17 +01002542 Stub_srcs []string `android:"path"`
Paul Duffin1fd005d2020-04-09 01:08:11 +01002543
2544 // The current.txt
Paul Duffin0f8faff2020-05-20 16:18:00 +01002545 Current_api *string `android:"path"`
Paul Duffin1fd005d2020-04-09 01:08:11 +01002546
2547 // The removed.txt
Paul Duffin0f8faff2020-05-20 16:18:00 +01002548 Removed_api *string `android:"path"`
Anton Hanssond78eb762021-09-21 15:25:12 +01002549
2550 // Annotation zip
2551 Annotations *string `android:"path"`
Colin Cross79c7c262019-04-17 11:11:46 -07002552}
2553
Paul Duffin56d44902020-01-31 13:36:25 +00002554type sdkLibraryImportProperties struct {
Paul Duffinfcfd7912020-01-31 17:54:30 +00002555 // List of shared java libs, common to all scopes, that this module has
2556 // dependencies to
2557 Libs []string
Paul Duffin1267d872021-04-16 17:21:36 +01002558
2559 // If set to true, compile dex files for the stubs. Defaults to false.
2560 Compile_dex *bool
Paul Duffin869de142021-07-15 14:14:41 +01002561
2562 // If not empty, classes are restricted to the specified packages and their sub-packages.
Paul Duffin869de142021-07-15 14:14:41 +01002563 Permitted_packages []string
Spandan Das23956d12024-01-19 00:22:22 +00002564
2565 // Name of the source soong module that gets shadowed by this prebuilt
2566 // If unspecified, follows the naming convention that the source module of
2567 // the prebuilt is Name() without "prebuilt_" prefix
2568 Source_module_name *string
Paul Duffin56d44902020-01-31 13:36:25 +00002569}
2570
Paul Duffineedc5d52020-06-12 17:46:39 +01002571type SdkLibraryImport struct {
Colin Cross79c7c262019-04-17 11:11:46 -07002572 android.ModuleBase
2573 android.DefaultableModuleBase
2574 prebuilt android.Prebuilt
Paul Duffindd46f712020-02-10 13:37:10 +00002575 android.ApexModuleBase
Colin Cross79c7c262019-04-17 11:11:46 -07002576
Paul Duffin37856732021-02-26 14:24:15 +00002577 hiddenAPI
Jiakai Zhang204356f2021-09-09 08:12:46 +00002578 dexpreopter
Paul Duffin37856732021-02-26 14:24:15 +00002579
Colin Cross79c7c262019-04-17 11:11:46 -07002580 properties sdkLibraryImportProperties
2581
Paul Duffin46a26a82020-04-07 19:27:04 +01002582 // Map from api scope to the scope specific property structure.
2583 scopeProperties map[*apiScope]*sdkLibraryScopeProperties
2584
Paul Duffin56d44902020-01-31 13:36:25 +00002585 commonToSdkLibraryAndImport
Paul Duffineedc5d52020-06-12 17:46:39 +01002586
Paul Duffineedc5d52020-06-12 17:46:39 +01002587 // The reference to the xml permissions module created by the source module.
2588 // Is nil if the source module does not exist.
2589 xmlPermissionsFileModule *sdkLibraryXml
Paul Duffin39853512021-02-26 11:09:39 +00002590
Jeongik Chad5fe8782021-07-08 01:13:11 +09002591 // Build path to the dex implementation jar obtained from the prebuilt_apex, if any.
Spandan Dasfae468e2023-12-12 23:23:53 +00002592 dexJarFile OptionalDexJarPath
2593 dexJarFileErr error
Jeongik Chad5fe8782021-07-08 01:13:11 +09002594
2595 // Expected install file path of the source module(sdk_library)
2596 // or dex implementation jar obtained from the prebuilt_apex, if any.
2597 installFile android.Path
Colin Cross79c7c262019-04-17 11:11:46 -07002598}
2599
Paul Duffineedc5d52020-06-12 17:46:39 +01002600var _ SdkLibraryDependency = (*SdkLibraryImport)(nil)
Colin Cross79c7c262019-04-17 11:11:46 -07002601
Paul Duffin46a26a82020-04-07 19:27:04 +01002602// The type of a structure that contains a field of type sdkLibraryScopeProperties
2603// for each apiscope in allApiScopes, e.g. something like:
Colin Crossd079e0b2022-08-16 10:27:33 -07002604//
2605// struct {
2606// Public sdkLibraryScopeProperties
2607// System sdkLibraryScopeProperties
2608// ...
2609// }
Paul Duffin46a26a82020-04-07 19:27:04 +01002610var allScopeStructType = createAllScopePropertiesStructType()
2611
2612// Dynamically create a structure type for each apiscope in allApiScopes.
2613func createAllScopePropertiesStructType() reflect.Type {
2614 var fields []reflect.StructField
Jihoon Kang98aa8fa2024-06-07 11:06:57 +00002615 for _, apiScope := range AllApiScopes {
Paul Duffin46a26a82020-04-07 19:27:04 +01002616 field := reflect.StructField{
2617 Name: apiScope.fieldName,
2618 Type: reflect.TypeOf(sdkLibraryScopeProperties{}),
2619 }
2620 fields = append(fields, field)
2621 }
2622
2623 return reflect.StructOf(fields)
2624}
2625
2626// Create an instance of the scope specific structure type and return a map
2627// from apiscope to a pointer to each scope specific field.
2628func createPropertiesInstance() (interface{}, map[*apiScope]*sdkLibraryScopeProperties) {
2629 allScopePropertiesPtr := reflect.New(allScopeStructType)
2630 allScopePropertiesStruct := allScopePropertiesPtr.Elem()
2631 scopeProperties := make(map[*apiScope]*sdkLibraryScopeProperties)
2632
Jihoon Kang98aa8fa2024-06-07 11:06:57 +00002633 for _, apiScope := range AllApiScopes {
Paul Duffin46a26a82020-04-07 19:27:04 +01002634 field := allScopePropertiesStruct.FieldByName(apiScope.fieldName)
2635 scopeProperties[apiScope] = field.Addr().Interface().(*sdkLibraryScopeProperties)
2636 }
2637
2638 return allScopePropertiesPtr.Interface(), scopeProperties
2639}
2640
Jaewoong Jung4f158ee2019-07-11 10:05:35 -07002641// java_sdk_library_import imports a prebuilt java_sdk_library.
Colin Cross79c7c262019-04-17 11:11:46 -07002642func sdkLibraryImportFactory() android.Module {
Paul Duffineedc5d52020-06-12 17:46:39 +01002643 module := &SdkLibraryImport{}
Colin Cross79c7c262019-04-17 11:11:46 -07002644
Paul Duffin46a26a82020-04-07 19:27:04 +01002645 allScopeProperties, scopeToProperties := createPropertiesInstance()
2646 module.scopeProperties = scopeToProperties
Jiakai Zhang9c4dc192023-02-09 00:09:24 +08002647 module.AddProperties(&module.properties, allScopeProperties, &module.importDexpreoptProperties)
Colin Cross79c7c262019-04-17 11:11:46 -07002648
Paul Duffinc3091c82020-05-08 14:16:20 +01002649 // Initialize information common between source and prebuilt.
Paul Duffin71b33cc2021-06-23 11:39:47 +01002650 module.initCommon(module)
Paul Duffinc3091c82020-05-08 14:16:20 +01002651
Paul Duffin0bdcb272020-02-06 15:24:57 +00002652 android.InitPrebuiltModule(module, &[]string{""})
Paul Duffindd46f712020-02-10 13:37:10 +00002653 android.InitApexModule(module)
Colin Cross79c7c262019-04-17 11:11:46 -07002654 InitJavaModule(module, android.HostAndDeviceSupported)
2655
Paul Duffin1b1e8062020-05-08 13:44:43 +01002656 module.SetDefaultableHook(func(mctx android.DefaultableHookContext) {
2657 if module.initCommonAfterDefaultsApplied(mctx) {
2658 module.createInternalModules(mctx)
2659 }
2660 })
Colin Cross79c7c262019-04-17 11:11:46 -07002661 return module
2662}
2663
Paul Duffin630b11e2021-07-15 13:35:26 +01002664var _ PermittedPackagesForUpdatableBootJars = (*SdkLibraryImport)(nil)
2665
2666func (module *SdkLibraryImport) PermittedPackagesForUpdatableBootJars() []string {
2667 return module.properties.Permitted_packages
2668}
2669
Paul Duffineedc5d52020-06-12 17:46:39 +01002670func (module *SdkLibraryImport) Prebuilt() *android.Prebuilt {
Colin Cross79c7c262019-04-17 11:11:46 -07002671 return &module.prebuilt
2672}
2673
Paul Duffineedc5d52020-06-12 17:46:39 +01002674func (module *SdkLibraryImport) Name() string {
Colin Cross79c7c262019-04-17 11:11:46 -07002675 return module.prebuilt.Name(module.ModuleBase.Name())
2676}
2677
Spandan Das23956d12024-01-19 00:22:22 +00002678func (module *SdkLibraryImport) BaseModuleName() string {
2679 return proptools.StringDefault(module.properties.Source_module_name, module.ModuleBase.Name())
2680}
2681
Paul Duffineedc5d52020-06-12 17:46:39 +01002682func (module *SdkLibraryImport) createInternalModules(mctx android.DefaultableHookContext) {
Colin Cross79c7c262019-04-17 11:11:46 -07002683
Paul Duffin50061512020-01-21 16:31:05 +00002684 // If the build is configured to use prebuilts then force this to be preferred.
Jeongik Cha816a23a2020-07-08 01:09:23 +09002685 if mctx.Config().AlwaysUsePrebuiltSdks() {
Paul Duffin50061512020-01-21 16:31:05 +00002686 module.prebuilt.ForcePrefer()
2687 }
2688
Paul Duffin46a26a82020-04-07 19:27:04 +01002689 for apiScope, scopeProperties := range module.scopeProperties {
Paul Duffin56d44902020-01-31 13:36:25 +00002690 if len(scopeProperties.Jars) == 0 {
2691 continue
2692 }
2693
Paul Duffinbbb546b2020-04-09 00:07:11 +01002694 module.createJavaImportForStubs(mctx, apiScope, scopeProperties)
Paul Duffin3d1248c2020-04-09 00:10:17 +01002695
Paul Duffin0f8faff2020-05-20 16:18:00 +01002696 if len(scopeProperties.Stub_srcs) > 0 {
2697 module.createPrebuiltStubsSources(mctx, apiScope, scopeProperties)
2698 }
Jihoon Kang71c86832023-09-13 01:01:53 +00002699
2700 if scopeProperties.Current_api != nil {
2701 module.createPrebuiltApiContribution(mctx, apiScope, scopeProperties)
2702 }
Paul Duffin56d44902020-01-31 13:36:25 +00002703 }
Colin Cross79c7c262019-04-17 11:11:46 -07002704
2705 javaSdkLibraries := javaSdkLibraries(mctx.Config())
2706 javaSdkLibrariesLock.Lock()
2707 defer javaSdkLibrariesLock.Unlock()
2708 *javaSdkLibraries = append(*javaSdkLibraries, module.BaseModuleName())
2709}
2710
Paul Duffineedc5d52020-06-12 17:46:39 +01002711func (module *SdkLibraryImport) createJavaImportForStubs(mctx android.DefaultableHookContext, apiScope *apiScope, scopeProperties *sdkLibraryScopeProperties) {
Paul Duffinbbb546b2020-04-09 00:07:11 +01002712 // Creates a java import for the jar with ".stubs" suffix
2713 props := struct {
Spandan Das23956d12024-01-19 00:22:22 +00002714 Name *string
2715 Source_module_name *string
2716 Created_by_java_sdk_library_name *string
2717 Sdk_version *string
2718 Libs []string
2719 Jars []string
2720 Compile_dex *bool
Jihoon Kangfe914ed2024-02-12 22:49:21 +00002721 Is_stubs_module *bool
Paul Duffinbf4de042022-09-27 12:41:52 +01002722
2723 android.UserSuppliedPrebuiltProperties
Paul Duffinbbb546b2020-04-09 00:07:11 +01002724 }{}
Paul Duffinc3091c82020-05-08 14:16:20 +01002725 props.Name = proptools.StringPtr(module.stubsLibraryModuleName(apiScope))
Spandan Das23956d12024-01-19 00:22:22 +00002726 props.Source_module_name = proptools.StringPtr(apiScope.stubsLibraryModuleName(module.BaseModuleName()))
2727 props.Created_by_java_sdk_library_name = proptools.StringPtr(module.RootLibraryName())
Paul Duffinbbb546b2020-04-09 00:07:11 +01002728 props.Sdk_version = scopeProperties.Sdk_version
2729 // Prepend any of the libs from the legacy public properties to the libs for each of the
2730 // scopes to avoid having to duplicate them in each scope.
2731 props.Libs = append(module.properties.Libs, scopeProperties.Libs...)
2732 props.Jars = scopeProperties.Jars
Paul Duffin1dbe3ca2020-05-16 09:57:59 +01002733
Paul Duffin38b57852020-05-13 16:08:09 +01002734 // The imports are preferred if the java_sdk_library_import is preferred.
Paul Duffinbf4de042022-09-27 12:41:52 +01002735 props.CopyUserSuppliedPropertiesFromPrebuilt(&module.prebuilt)
Paul Duffin859fe962020-05-15 10:20:31 +01002736
Paul Duffin1267d872021-04-16 17:21:36 +01002737 // The imports need to be compiled to dex if the java_sdk_library_import requests it.
Paul Duffinf4600f62021-05-13 22:34:45 +01002738 compileDex := module.properties.Compile_dex
2739 if module.stubLibrariesCompiledForDex() {
2740 compileDex = proptools.BoolPtr(true)
2741 }
2742 props.Compile_dex = compileDex
Jihoon Kangfe914ed2024-02-12 22:49:21 +00002743 props.Is_stubs_module = proptools.BoolPtr(true)
Paul Duffin1267d872021-04-16 17:21:36 +01002744
Paul Duffin859fe962020-05-15 10:20:31 +01002745 mctx.CreateModule(ImportFactory, &props, module.sdkComponentPropertiesForChildLibrary())
Paul Duffinbbb546b2020-04-09 00:07:11 +01002746}
2747
Paul Duffineedc5d52020-06-12 17:46:39 +01002748func (module *SdkLibraryImport) createPrebuiltStubsSources(mctx android.DefaultableHookContext, apiScope *apiScope, scopeProperties *sdkLibraryScopeProperties) {
Paul Duffin3d1248c2020-04-09 00:10:17 +01002749 props := struct {
Spandan Das23956d12024-01-19 00:22:22 +00002750 Name *string
2751 Source_module_name *string
2752 Created_by_java_sdk_library_name *string
2753 Srcs []string
Paul Duffinbf4de042022-09-27 12:41:52 +01002754
2755 android.UserSuppliedPrebuiltProperties
Paul Duffin3d1248c2020-04-09 00:10:17 +01002756 }{}
Paul Duffinc3091c82020-05-08 14:16:20 +01002757 props.Name = proptools.StringPtr(module.stubsSourceModuleName(apiScope))
Spandan Das23956d12024-01-19 00:22:22 +00002758 props.Source_module_name = proptools.StringPtr(apiScope.stubsSourceModuleName(module.BaseModuleName()))
2759 props.Created_by_java_sdk_library_name = proptools.StringPtr(module.RootLibraryName())
Paul Duffin3d1248c2020-04-09 00:10:17 +01002760 props.Srcs = scopeProperties.Stub_srcs
Paul Duffin38b57852020-05-13 16:08:09 +01002761
2762 // The stubs source is preferred if the java_sdk_library_import is preferred.
Paul Duffinbf4de042022-09-27 12:41:52 +01002763 props.CopyUserSuppliedPropertiesFromPrebuilt(&module.prebuilt)
2764
Spandan Das2cc80ba2023-10-27 17:21:52 +00002765 mctx.CreateModule(PrebuiltStubsSourcesFactory, &props, module.sdkComponentPropertiesForChildLibrary())
Paul Duffin3d1248c2020-04-09 00:10:17 +01002766}
2767
Jihoon Kang71c86832023-09-13 01:01:53 +00002768func (module *SdkLibraryImport) createPrebuiltApiContribution(mctx android.DefaultableHookContext, apiScope *apiScope, scopeProperties *sdkLibraryScopeProperties) {
2769 api_file := scopeProperties.Current_api
2770 api_surface := &apiScope.name
2771
2772 props := struct {
Spandan Das23956d12024-01-19 00:22:22 +00002773 Name *string
2774 Source_module_name *string
2775 Created_by_java_sdk_library_name *string
2776 Api_surface *string
2777 Api_file *string
2778 Visibility []string
Jihoon Kang71c86832023-09-13 01:01:53 +00002779 }{}
2780
2781 props.Name = proptools.StringPtr(module.stubsSourceModuleName(apiScope) + ".api.contribution")
Spandan Das23956d12024-01-19 00:22:22 +00002782 props.Source_module_name = proptools.StringPtr(apiScope.stubsSourceModuleName(module.BaseModuleName()) + ".api.contribution")
2783 props.Created_by_java_sdk_library_name = proptools.StringPtr(module.RootLibraryName())
Jihoon Kang71c86832023-09-13 01:01:53 +00002784 props.Api_surface = api_surface
2785 props.Api_file = api_file
2786 props.Visibility = []string{"//visibility:override", "//visibility:public"}
2787
Spandan Das2cc80ba2023-10-27 17:21:52 +00002788 mctx.CreateModule(ApiContributionImportFactory, &props, module.sdkComponentPropertiesForChildLibrary())
Jihoon Kang71c86832023-09-13 01:01:53 +00002789}
2790
Paul Duffin44f1d842020-06-26 20:17:02 +01002791// Add the dependencies on the child module in the component deps mutator so that it
2792// creates references to the prebuilt and not the source modules.
2793func (module *SdkLibraryImport) ComponentDepsMutator(ctx android.BottomUpMutatorContext) {
Paul Duffin46a26a82020-04-07 19:27:04 +01002794 for apiScope, scopeProperties := range module.scopeProperties {
Paul Duffin56d44902020-01-31 13:36:25 +00002795 if len(scopeProperties.Jars) == 0 {
2796 continue
2797 }
2798
2799 // Add dependencies to the prebuilt stubs library
Jihoon Kangb7431552024-01-22 19:40:08 +00002800 ctx.AddVariationDependencies(nil, apiScope.prebuiltStubsTag, android.PrebuiltNameFromSource(module.stubsLibraryModuleName(apiScope)))
Paul Duffin0f8faff2020-05-20 16:18:00 +01002801
2802 if len(scopeProperties.Stub_srcs) > 0 {
2803 // Add dependencies to the prebuilt stubs source library
Paul Duffin864116c2021-04-02 10:24:13 +01002804 ctx.AddVariationDependencies(nil, apiScope.stubsSourceTag, android.PrebuiltNameFromSource(module.stubsSourceModuleName(apiScope)))
Paul Duffin0f8faff2020-05-20 16:18:00 +01002805 }
Paul Duffin56d44902020-01-31 13:36:25 +00002806 }
Paul Duffin44f1d842020-06-26 20:17:02 +01002807}
2808
2809// Add other dependencies as normal.
2810func (module *SdkLibraryImport) DepsMutator(ctx android.BottomUpMutatorContext) {
Paul Duffineedc5d52020-06-12 17:46:39 +01002811
2812 implName := module.implLibraryModuleName()
2813 if ctx.OtherModuleExists(implName) {
2814 ctx.AddVariationDependencies(nil, implLibraryTag, implName)
2815
2816 xmlPermissionsModuleName := module.xmlPermissionsModuleName()
2817 if module.sharedLibrary() && ctx.OtherModuleExists(xmlPermissionsModuleName) {
2818 // Add dependency to the rule for generating the xml permissions file
2819 ctx.AddDependency(module, xmlPermissionsFileTag, xmlPermissionsModuleName)
2820 }
2821 }
Colin Cross79c7c262019-04-17 11:11:46 -07002822}
2823
Jiyong Park45bf82e2020-12-15 22:29:02 +09002824var _ android.ApexModule = (*SdkLibraryImport)(nil)
2825
2826// Implements android.ApexModule
Paul Duffineedc5d52020-06-12 17:46:39 +01002827func (module *SdkLibraryImport) DepIsInSameApex(mctx android.BaseModuleContext, dep android.Module) bool {
2828 depTag := mctx.OtherModuleDependencyTag(dep)
2829 if depTag == xmlPermissionsFileTag {
2830 return true
2831 }
2832
2833 // None of the other dependencies of the java_sdk_library_import are in the same apex
2834 // as the one that references this module.
2835 return false
2836}
2837
Jiyong Park45bf82e2020-12-15 22:29:02 +09002838// Implements android.ApexModule
Dan Albertc8060532020-07-22 22:32:17 -07002839func (module *SdkLibraryImport) ShouldSupportSdkVersion(ctx android.BaseModuleContext,
2840 sdkVersion android.ApiLevel) error {
Jooyung Han749dc692020-04-15 11:03:39 +09002841 // we don't check prebuilt modules for sdk_version
2842 return nil
2843}
2844
Paul Duffinea8f8082021-06-24 13:25:57 +01002845// Implements android.ApexModule
2846func (module *SdkLibraryImport) UniqueApexVariations() bool {
2847 return module.uniqueApexVariations()
2848}
2849
Paul Duffin09817d62022-04-28 17:45:11 +01002850// MinSdkVersion - Implements hiddenAPIModule
Spandan Das8c9ae7e2023-03-03 21:20:36 +00002851func (module *SdkLibraryImport) MinSdkVersion(ctx android.EarlyModuleContext) android.ApiLevel {
2852 return android.NoneApiLevel
Paul Duffin09817d62022-04-28 17:45:11 +01002853}
2854
2855var _ hiddenAPIModule = (*SdkLibraryImport)(nil)
2856
Paul Duffineedc5d52020-06-12 17:46:39 +01002857func (module *SdkLibraryImport) GenerateAndroidBuildActions(ctx android.ModuleContext) {
Paul Duffina2ae7e02020-09-11 11:55:00 +01002858 module.generateCommonBuildActions(ctx)
2859
Jeongik Chad5fe8782021-07-08 01:13:11 +09002860 // Assume that source module(sdk_library) is installed in /<sdk_library partition>/framework
2861 module.installFile = android.PathForModuleInstall(ctx, "framework", module.Stem()+".jar")
2862
Paul Duffin0f8faff2020-05-20 16:18:00 +01002863 // Record the paths to the prebuilt stubs library and stubs source.
Colin Cross79c7c262019-04-17 11:11:46 -07002864 ctx.VisitDirectDeps(func(to android.Module) {
2865 tag := ctx.OtherModuleDependencyTag(to)
2866
Paul Duffin0f8faff2020-05-20 16:18:00 +01002867 // Extract information from any of the scope specific dependencies.
2868 if scopeTag, ok := tag.(scopeDependencyTag); ok {
2869 apiScope := scopeTag.apiScope
2870 scopePaths := module.getScopePathsCreateIfNeeded(apiScope)
2871
2872 // Extract information from the dependency. The exact information extracted
2873 // is determined by the nature of the dependency which is determined by the tag.
2874 scopeTag.extractDepInfo(ctx, to, scopePaths)
Paul Duffineedc5d52020-06-12 17:46:39 +01002875 } else if tag == implLibraryTag {
2876 if implLibrary, ok := to.(*Library); ok {
2877 module.implLibraryModule = implLibrary
2878 } else {
2879 ctx.ModuleErrorf("implementation library must be of type *java.Library but was %T", to)
2880 }
2881 } else if tag == xmlPermissionsFileTag {
2882 if xmlPermissionsFileModule, ok := to.(*sdkLibraryXml); ok {
2883 module.xmlPermissionsFileModule = xmlPermissionsFileModule
2884 } else {
2885 ctx.ModuleErrorf("xml permissions file module must be of type *sdkLibraryXml but was %T", to)
2886 }
Colin Cross79c7c262019-04-17 11:11:46 -07002887 }
2888 })
Paul Duffin0f8faff2020-05-20 16:18:00 +01002889
2890 // Populate the scope paths with information from the properties.
2891 for apiScope, scopeProperties := range module.scopeProperties {
2892 if len(scopeProperties.Jars) == 0 {
2893 continue
2894 }
2895
2896 paths := module.getScopePathsCreateIfNeeded(apiScope)
Anton Hanssond78eb762021-09-21 15:25:12 +01002897 paths.annotationsZip = android.OptionalPathForModuleSrc(ctx, scopeProperties.Annotations)
Paul Duffin0f8faff2020-05-20 16:18:00 +01002898 paths.currentApiFilePath = android.OptionalPathForModuleSrc(ctx, scopeProperties.Current_api)
2899 paths.removedApiFilePath = android.OptionalPathForModuleSrc(ctx, scopeProperties.Removed_api)
2900 }
Paul Duffin39853512021-02-26 11:09:39 +00002901
2902 if ctx.Device() {
2903 // If this is a variant created for a prebuilt_apex then use the dex implementation jar
2904 // obtained from the associated deapexer module.
Colin Crossff694a82023-12-13 15:54:49 -08002905 ai, _ := android.ModuleProvider(ctx, android.ApexInfoProvider)
Paul Duffin39853512021-02-26 11:09:39 +00002906 if ai.ForPrebuiltApex {
Paul Duffin39853512021-02-26 11:09:39 +00002907 // Get the path of the dex implementation jar from the `deapexer` module.
Spandan Dasfae468e2023-12-12 23:23:53 +00002908 di, err := android.FindDeapexerProviderForModule(ctx)
2909 if err != nil {
2910 // An error was found, possibly due to multiple apexes in the tree that export this library
2911 // Defer the error till a client tries to call DexJarBuildPath
2912 module.dexJarFileErr = err
Spandan Das3a392012024-01-17 18:26:27 +00002913 module.initHiddenAPIError(err)
Spandan Dasfae468e2023-12-12 23:23:53 +00002914 return
Martin Stjernholm44825602021-09-17 01:44:12 +01002915 }
Spandan Das5be63332023-12-13 00:06:32 +00002916 dexJarFileApexRootRelative := ApexRootRelativePathToJavaLib(module.BaseModuleName())
Jiakai Zhang81e46812023-02-08 21:56:07 +08002917 if dexOutputPath := di.PrebuiltExportPath(dexJarFileApexRootRelative); dexOutputPath != nil {
Martin Stjernholm8be1e6d2021-09-15 03:34:04 +01002918 dexJarFile := makeDexJarPathFromPath(dexOutputPath)
2919 module.dexJarFile = dexJarFile
Jiakai Zhang204356f2021-09-09 08:12:46 +00002920 installPath := android.PathForModuleInPartitionInstall(
Jiakai Zhang81e46812023-02-08 21:56:07 +08002921 ctx, "apex", ai.ApexVariationName, dexJarFileApexRootRelative)
Jiakai Zhang204356f2021-09-09 08:12:46 +00002922 module.installFile = installPath
Martin Stjernholm8be1e6d2021-09-15 03:34:04 +01002923 module.initHiddenAPI(ctx, dexJarFile, module.findScopePaths(apiScopePublic).stubsImplPath[0], nil)
Jiakai Zhang204356f2021-09-09 08:12:46 +00002924
Spandan Dase21a8d42024-01-23 23:56:29 +00002925 module.dexpreopter.installPath = module.dexpreopter.getInstallPath(ctx, android.RemoveOptionalPrebuiltPrefix(ctx.ModuleName()), installPath)
Jiakai Zhang204356f2021-09-09 08:12:46 +00002926 module.dexpreopter.isSDKLibrary = true
Spandan Dase21a8d42024-01-23 23:56:29 +00002927 module.dexpreopter.uncompressedDex = shouldUncompressDex(ctx, android.RemoveOptionalPrebuiltPrefix(ctx.ModuleName()), &module.dexpreopter)
Jiakai Zhang81e46812023-02-08 21:56:07 +08002928
2929 if profilePath := di.PrebuiltExportPath(dexJarFileApexRootRelative + ".prof"); profilePath != nil {
2930 module.dexpreopter.inputProfilePathOnHost = profilePath
2931 }
Paul Duffin39853512021-02-26 11:09:39 +00002932 } else {
2933 // This should never happen as a variant for a prebuilt_apex is only created if the
2934 // prebuilt_apex has been configured to export the java library dex file.
Martin Stjernholm44825602021-09-17 01:44:12 +01002935 ctx.ModuleErrorf("internal error: no dex implementation jar available from prebuilt APEX %s", di.ApexModuleName())
Paul Duffin39853512021-02-26 11:09:39 +00002936 }
2937 }
2938 }
mrziwang9f7b9f42024-07-10 12:18:06 -07002939
2940 module.setOutputFiles(ctx)
2941 if module.implLibraryModule != nil {
2942 setOutputFiles(ctx, module.implLibraryModule.Module)
2943 }
Colin Cross79c7c262019-04-17 11:11:46 -07002944}
2945
Jiyong Parkf1691d22021-03-29 20:11:58 +09002946func (module *SdkLibraryImport) sdkJars(ctx android.BaseModuleContext, sdkVersion android.SdkSpec, headerJars bool) android.Paths {
Paul Duffineedc5d52020-06-12 17:46:39 +01002947
2948 // For consistency with SdkLibrary make the implementation jar available to libraries that
2949 // are within the same APEX.
2950 implLibraryModule := module.implLibraryModule
Colin Cross56a83212020-09-15 18:30:11 -07002951 if implLibraryModule != nil && withinSameApexesAs(ctx, module) {
Paul Duffineedc5d52020-06-12 17:46:39 +01002952 if headerJars {
2953 return implLibraryModule.HeaderJars()
2954 } else {
2955 return implLibraryModule.ImplementationJars()
2956 }
2957 }
2958
Paul Duffin23970f42020-05-20 14:20:02 +01002959 return module.selectHeaderJarsForSdkVersion(ctx, sdkVersion)
Paul Duffin56d44902020-01-31 13:36:25 +00002960}
2961
Colin Cross79c7c262019-04-17 11:11:46 -07002962// to satisfy SdkLibraryDependency interface
Jiyong Parkf1691d22021-03-29 20:11:58 +09002963func (module *SdkLibraryImport) SdkHeaderJars(ctx android.BaseModuleContext, sdkVersion android.SdkSpec) android.Paths {
Colin Cross79c7c262019-04-17 11:11:46 -07002964 // This module is just a wrapper for the prebuilt stubs.
Paul Duffineedc5d52020-06-12 17:46:39 +01002965 return module.sdkJars(ctx, sdkVersion, true)
Colin Cross79c7c262019-04-17 11:11:46 -07002966}
2967
Ulya Trafimovichdbf31662020-12-17 12:07:54 +00002968// to satisfy UsesLibraryDependency interface
Spandan Das59a4a2b2024-01-09 21:35:56 +00002969func (module *SdkLibraryImport) DexJarBuildPath(ctx android.ModuleErrorfContext) OptionalDexJarPath {
Paul Duffin39853512021-02-26 11:09:39 +00002970 // The dex implementation jar extracted from the .apex file should be used in preference to the
2971 // source.
Spandan Dasfae468e2023-12-12 23:23:53 +00002972 if module.dexJarFileErr != nil {
Spandan Das59a4a2b2024-01-09 21:35:56 +00002973 ctx.ModuleErrorf(module.dexJarFileErr.Error())
Spandan Dasfae468e2023-12-12 23:23:53 +00002974 }
Martin Stjernholm8be1e6d2021-09-15 03:34:04 +01002975 if module.dexJarFile.IsSet() {
Paul Duffin39853512021-02-26 11:09:39 +00002976 return module.dexJarFile
2977 }
Paul Duffineedc5d52020-06-12 17:46:39 +01002978 if module.implLibraryModule == nil {
Martin Stjernholm8be1e6d2021-09-15 03:34:04 +01002979 return makeUnsetDexJarPath()
Paul Duffineedc5d52020-06-12 17:46:39 +01002980 } else {
Spandan Das59a4a2b2024-01-09 21:35:56 +00002981 return module.implLibraryModule.DexJarBuildPath(ctx)
Paul Duffineedc5d52020-06-12 17:46:39 +01002982 }
2983}
2984
Ulya Trafimovichdbf31662020-12-17 12:07:54 +00002985// to satisfy UsesLibraryDependency interface
Ulya Trafimovich31e444e2020-08-14 17:32:16 +01002986func (module *SdkLibraryImport) DexJarInstallPath() android.Path {
Jeongik Chad5fe8782021-07-08 01:13:11 +09002987 return module.installFile
Ulya Trafimovich31e444e2020-08-14 17:32:16 +01002988}
2989
Ulya Trafimovichdbf31662020-12-17 12:07:54 +00002990// to satisfy UsesLibraryDependency interface
2991func (module *SdkLibraryImport) ClassLoaderContexts() dexpreopt.ClassLoaderContextMap {
2992 return nil
2993}
2994
Paul Duffineedc5d52020-06-12 17:46:39 +01002995// to satisfy apex.javaDependency interface
2996func (module *SdkLibraryImport) JacocoReportClassesFile() android.Path {
2997 if module.implLibraryModule == nil {
2998 return nil
2999 } else {
3000 return module.implLibraryModule.JacocoReportClassesFile()
3001 }
3002}
3003
3004// to satisfy apex.javaDependency interface
Colin Cross08dca382020-07-21 20:31:17 -07003005func (module *SdkLibraryImport) LintDepSets() LintDepSets {
3006 if module.implLibraryModule == nil {
3007 return LintDepSets{}
3008 } else {
3009 return module.implLibraryModule.LintDepSets()
3010 }
3011}
3012
Spandan Das17854f52022-01-14 21:19:14 +00003013func (module *SdkLibraryImport) GetStrictUpdatabilityLinting() bool {
Jaewoong Jung476b9d62021-05-10 15:30:00 -07003014 if module.implLibraryModule == nil {
3015 return false
3016 } else {
Spandan Das17854f52022-01-14 21:19:14 +00003017 return module.implLibraryModule.GetStrictUpdatabilityLinting()
Jaewoong Jung476b9d62021-05-10 15:30:00 -07003018 }
3019}
3020
Spandan Das17854f52022-01-14 21:19:14 +00003021func (module *SdkLibraryImport) SetStrictUpdatabilityLinting(strictLinting bool) {
Jaewoong Jung476b9d62021-05-10 15:30:00 -07003022 if module.implLibraryModule != nil {
Spandan Das17854f52022-01-14 21:19:14 +00003023 module.implLibraryModule.SetStrictUpdatabilityLinting(strictLinting)
Jaewoong Jung476b9d62021-05-10 15:30:00 -07003024 }
3025}
3026
Colin Cross08dca382020-07-21 20:31:17 -07003027// to satisfy apex.javaDependency interface
Paul Duffineedc5d52020-06-12 17:46:39 +01003028func (module *SdkLibraryImport) Stem() string {
3029 return module.BaseModuleName()
Colin Cross79c7c262019-04-17 11:11:46 -07003030}
Jiyong Parke3833882020-02-17 17:28:10 +09003031
Paul Duffin44b481b2020-06-17 16:59:43 +01003032var _ ApexDependency = (*SdkLibraryImport)(nil)
3033
3034// to satisfy java.ApexDependency interface
3035func (module *SdkLibraryImport) HeaderJars() android.Paths {
3036 if module.implLibraryModule == nil {
3037 return nil
3038 } else {
3039 return module.implLibraryModule.HeaderJars()
3040 }
3041}
3042
3043// to satisfy java.ApexDependency interface
3044func (module *SdkLibraryImport) ImplementationAndResourcesJars() android.Paths {
3045 if module.implLibraryModule == nil {
3046 return nil
3047 } else {
3048 return module.implLibraryModule.ImplementationAndResourcesJars()
3049 }
3050}
3051
Jiakai Zhang204356f2021-09-09 08:12:46 +00003052// to satisfy java.DexpreopterInterface interface
3053func (module *SdkLibraryImport) IsInstallable() bool {
3054 return true
3055}
3056
Paul Duffinfef55002021-06-17 14:56:05 +01003057var _ android.RequiredFilesFromPrebuiltApex = (*SdkLibraryImport)(nil)
3058
Paul Duffinb4bbf2c2021-06-17 15:59:07 +01003059func (module *SdkLibraryImport) RequiredFilesFromPrebuiltApex(ctx android.BaseModuleContext) []string {
Paul Duffinfef55002021-06-17 14:56:05 +01003060 name := module.BaseModuleName()
Jiakai Zhang81e46812023-02-08 21:56:07 +08003061 return requiredFilesFromPrebuiltApexForImport(name, &module.dexpreopter)
Paul Duffinfef55002021-06-17 14:56:05 +01003062}
3063
Spandan Das2ea84dd2024-01-25 22:12:50 +00003064func (j *SdkLibraryImport) UseProfileGuidedDexpreopt() bool {
3065 return proptools.Bool(j.importDexpreoptProperties.Dex_preopt.Profile_guided)
3066}
3067
Jiyong Parke3833882020-02-17 17:28:10 +09003068// java_sdk_library_xml
Jiyong Parke3833882020-02-17 17:28:10 +09003069type sdkLibraryXml struct {
3070 android.ModuleBase
3071 android.DefaultableModuleBase
3072 android.ApexModuleBase
3073
3074 properties sdkLibraryXmlProperties
3075
3076 outputFilePath android.OutputPath
3077 installDirPath android.InstallPath
Colin Cross56a83212020-09-15 18:30:11 -07003078
3079 hideApexVariantFromMake bool
Jiyong Parke3833882020-02-17 17:28:10 +09003080}
3081
3082type sdkLibraryXmlProperties struct {
3083 // canonical name of the lib
3084 Lib_name *string
Pedro Loureiro9956e5e2021-09-07 17:21:59 +00003085
3086 // Signals that this shared library is part of the bootclasspath starting
3087 // on the version indicated in this attribute.
3088 //
3089 // This will make platforms at this level and above to ignore
3090 // <uses-library> tags with this library name because the library is already
3091 // available
3092 On_bootclasspath_since *string
3093
3094 // Signals that this shared library was part of the bootclasspath before
3095 // (but not including) the version indicated in this attribute.
3096 //
3097 // The system will automatically add a <uses-library> tag with this library to
3098 // apps that target any SDK less than the version indicated in this attribute.
3099 On_bootclasspath_before *string
3100
3101 // Indicates that PackageManager should ignore this shared library if the
3102 // platform is below the version indicated in this attribute.
3103 //
3104 // This means that the device won't recognise this library as installed.
3105 Min_device_sdk *string
3106
3107 // Indicates that PackageManager should ignore this shared library if the
3108 // platform is above the version indicated in this attribute.
3109 //
3110 // This means that the device won't recognise this library as installed.
3111 Max_device_sdk *string
Pedro Loureiroc3621422021-09-28 15:40:23 +00003112
3113 // The SdkLibrary's min api level as a string
3114 //
3115 // This value comes from the ApiLevel of the MinSdkVersion property.
3116 Sdk_library_min_api_level *string
Jamie Garsidee570ace2023-11-27 12:07:36 +00003117
3118 // Uses-libs dependencies that the shared library requires to work correctly.
3119 //
3120 // This will add dependency="foo:bar" to the <library> section.
3121 Uses_libs_dependencies []string
Jiyong Parke3833882020-02-17 17:28:10 +09003122}
3123
3124// java_sdk_library_xml builds the permission xml file for a java_sdk_library.
3125// Not to be used directly by users. java_sdk_library internally uses this.
3126func sdkLibraryXmlFactory() android.Module {
3127 module := &sdkLibraryXml{}
3128
3129 module.AddProperties(&module.properties)
3130
3131 android.InitApexModule(module)
3132 android.InitAndroidArchModule(module, android.DeviceSupported, android.MultilibCommon)
3133
3134 return module
3135}
3136
Colin Crossaede88c2020-08-11 12:17:01 -07003137func (module *sdkLibraryXml) UniqueApexVariations() bool {
3138 // sdkLibraryXml needs a unique variation per APEX because the generated XML file contains the path to the
3139 // mounted APEX, which contains the name of the APEX.
3140 return true
3141}
3142
Jiyong Parke3833882020-02-17 17:28:10 +09003143// from android.PrebuiltEtcModule
Jooyung Han0703fd82020-08-26 22:11:53 +09003144func (module *sdkLibraryXml) BaseDir() string {
3145 return "etc"
3146}
3147
3148// from android.PrebuiltEtcModule
Jiyong Parke3833882020-02-17 17:28:10 +09003149func (module *sdkLibraryXml) SubDir() string {
3150 return "permissions"
3151}
3152
ThiƩbaud Weksteen00e8b312024-03-18 14:06:00 +11003153var _ etc.PrebuiltEtcModule = (*sdkLibraryXml)(nil)
3154
Jiyong Parke3833882020-02-17 17:28:10 +09003155// from android.ApexModule
3156func (module *sdkLibraryXml) AvailableFor(what string) bool {
3157 return true
3158}
3159
3160func (module *sdkLibraryXml) DepsMutator(ctx android.BottomUpMutatorContext) {
3161 // do nothing
3162}
3163
Jiyong Park45bf82e2020-12-15 22:29:02 +09003164var _ android.ApexModule = (*sdkLibraryXml)(nil)
3165
3166// Implements android.ApexModule
Dan Albertc8060532020-07-22 22:32:17 -07003167func (module *sdkLibraryXml) ShouldSupportSdkVersion(ctx android.BaseModuleContext,
3168 sdkVersion android.ApiLevel) error {
Jooyung Han749dc692020-04-15 11:03:39 +09003169 // sdkLibraryXml doesn't need to be checked separately because java_sdk_library is checked
3170 return nil
3171}
3172
Jiyong Parke3833882020-02-17 17:28:10 +09003173// File path to the runtime implementation library
Colin Cross56a83212020-09-15 18:30:11 -07003174func (module *sdkLibraryXml) implPath(ctx android.ModuleContext) string {
Jiyong Parke3833882020-02-17 17:28:10 +09003175 implName := proptools.String(module.properties.Lib_name)
Colin Crossff694a82023-12-13 15:54:49 -08003176 if apexInfo, _ := android.ModuleProvider(ctx, android.ApexInfoProvider); !apexInfo.IsForPlatform() {
Colin Crosse07f2312020-08-13 11:24:56 -07003177 // TODO(b/146468504): ApexVariationName() is only a soong module name, not apex name.
Jiyong Parke3833882020-02-17 17:28:10 +09003178 // In most cases, this works fine. But when apex_name is set or override_apex is used
3179 // this can be wrong.
Spandan Das33bbeb22024-06-18 23:28:25 +00003180 return fmt.Sprintf("/apex/%s/javalib/%s.jar", apexInfo.BaseApexName, implName)
Jiyong Parke3833882020-02-17 17:28:10 +09003181 }
3182 partition := "system"
3183 if module.SocSpecific() {
3184 partition = "vendor"
3185 } else if module.DeviceSpecific() {
3186 partition = "odm"
3187 } else if module.ProductSpecific() {
3188 partition = "product"
3189 } else if module.SystemExtSpecific() {
3190 partition = "system_ext"
3191 }
3192 return "/" + partition + "/framework/" + implName + ".jar"
3193}
3194
Pedro Loureiro9956e5e2021-09-07 17:21:59 +00003195func formattedOptionalSdkLevelAttribute(ctx android.ModuleContext, attrName string, value *string) string {
3196 if value == nil {
3197 return ""
3198 }
3199 apiLevel, err := android.ApiLevelFromUser(ctx, *value)
3200 if err != nil {
Pedro Loureiroba6682f2021-10-29 09:32:32 +00003201 // attributes in bp files have underscores but in the xml have dashes.
3202 ctx.PropertyErrorf(strings.ReplaceAll(attrName, "-", "_"), err.Error())
Pedro Loureiro9956e5e2021-09-07 17:21:59 +00003203 return ""
3204 }
Pedro Loureirob638c622021-12-22 15:28:05 +00003205 if apiLevel.IsCurrent() {
3206 // passing "current" would always mean a future release, never the current (or the current in
3207 // progress) which means some conditions would never be triggered.
3208 ctx.PropertyErrorf(strings.ReplaceAll(attrName, "-", "_"),
3209 `"current" is not an allowed value for this attribute`)
3210 return ""
3211 }
Pedro Loureiro48991222022-06-17 20:01:21 +00003212 // "safeValue" is safe because it translates finalized codenames to a string
3213 // with their SDK int.
3214 safeValue := apiLevel.String()
3215 return formattedOptionalAttribute(attrName, &safeValue)
Pedro Loureiro9956e5e2021-09-07 17:21:59 +00003216}
3217
3218// formats an attribute for the xml permissions file if the value is not null
3219// returns empty string otherwise
3220func formattedOptionalAttribute(attrName string, value *string) string {
3221 if value == nil {
3222 return ""
3223 }
Paul Duffin1816cde2024-04-10 10:58:21 +01003224 return fmt.Sprintf(" %s=\"%s\"\n", attrName, *value)
Pedro Loureiro9956e5e2021-09-07 17:21:59 +00003225}
3226
Jamie Garsidee570ace2023-11-27 12:07:36 +00003227func formattedDependenciesAttribute(dependencies []string) string {
3228 if dependencies == nil {
3229 return ""
3230 }
Paul Duffin1816cde2024-04-10 10:58:21 +01003231 return fmt.Sprintf(" dependency=\"%s\"\n", strings.Join(dependencies, ":"))
Jamie Garsidee570ace2023-11-27 12:07:36 +00003232}
3233
Pedro Loureiro9956e5e2021-09-07 17:21:59 +00003234func (module *sdkLibraryXml) permissionsContents(ctx android.ModuleContext) string {
3235 libName := proptools.String(module.properties.Lib_name)
3236 libNameAttr := formattedOptionalAttribute("name", &libName)
3237 filePath := module.implPath(ctx)
3238 filePathAttr := formattedOptionalAttribute("file", &filePath)
Pedro Loureiroba6682f2021-10-29 09:32:32 +00003239 implicitFromAttr := formattedOptionalSdkLevelAttribute(ctx, "on-bootclasspath-since", module.properties.On_bootclasspath_since)
3240 implicitUntilAttr := formattedOptionalSdkLevelAttribute(ctx, "on-bootclasspath-before", module.properties.On_bootclasspath_before)
3241 minSdkAttr := formattedOptionalSdkLevelAttribute(ctx, "min-device-sdk", module.properties.Min_device_sdk)
3242 maxSdkAttr := formattedOptionalSdkLevelAttribute(ctx, "max-device-sdk", module.properties.Max_device_sdk)
Jamie Garsidee570ace2023-11-27 12:07:36 +00003243 dependenciesAttr := formattedDependenciesAttribute(module.properties.Uses_libs_dependencies)
Pedro Loureiro196d3e62021-12-22 19:53:01 +00003244 // <library> is understood in all android versions whereas <apex-library> is only understood from API T (and ignored before that).
3245 // 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 +00003246 var libraryTag string
3247 if module.properties.Min_device_sdk != nil {
Paul Duffin1816cde2024-04-10 10:58:21 +01003248 libraryTag = " <apex-library\n"
Pedro Loureiroc3621422021-09-28 15:40:23 +00003249 } else {
Paul Duffin1816cde2024-04-10 10:58:21 +01003250 libraryTag = " <library\n"
Pedro Loureiroc3621422021-09-28 15:40:23 +00003251 }
Pedro Loureiro9956e5e2021-09-07 17:21:59 +00003252
3253 return strings.Join([]string{
Paul Duffin1816cde2024-04-10 10:58:21 +01003254 "<?xml version=\"1.0\" encoding=\"utf-8\"?>\n",
3255 "<!-- Copyright (C) 2018 The Android Open Source Project\n",
3256 "\n",
3257 " Licensed under the Apache License, Version 2.0 (the \"License\");\n",
3258 " you may not use this file except in compliance with the License.\n",
3259 " You may obtain a copy of the License at\n",
3260 "\n",
3261 " http://www.apache.org/licenses/LICENSE-2.0\n",
3262 "\n",
3263 " Unless required by applicable law or agreed to in writing, software\n",
3264 " distributed under the License is distributed on an \"AS IS\" BASIS,\n",
3265 " WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\n",
3266 " See the License for the specific language governing permissions and\n",
3267 " limitations under the License.\n",
3268 "-->\n",
3269 "<permissions>\n",
Pedro Loureiroc3621422021-09-28 15:40:23 +00003270 libraryTag,
Pedro Loureiro9956e5e2021-09-07 17:21:59 +00003271 libNameAttr,
3272 filePathAttr,
3273 implicitFromAttr,
3274 implicitUntilAttr,
3275 minSdkAttr,
3276 maxSdkAttr,
Jamie Garsidee570ace2023-11-27 12:07:36 +00003277 dependenciesAttr,
Paul Duffin1816cde2024-04-10 10:58:21 +01003278 " />\n",
3279 "</permissions>\n",
3280 }, "")
Pedro Loureiro9956e5e2021-09-07 17:21:59 +00003281}
3282
Jiyong Parke3833882020-02-17 17:28:10 +09003283func (module *sdkLibraryXml) GenerateAndroidBuildActions(ctx android.ModuleContext) {
Colin Crossff694a82023-12-13 15:54:49 -08003284 apexInfo, _ := android.ModuleProvider(ctx, android.ApexInfoProvider)
3285 module.hideApexVariantFromMake = !apexInfo.IsForPlatform()
Colin Cross56a83212020-09-15 18:30:11 -07003286
Jiyong Parke3833882020-02-17 17:28:10 +09003287 libName := proptools.String(module.properties.Lib_name)
Pedro Loureiroc3621422021-09-28 15:40:23 +00003288 module.selfValidate(ctx)
Pedro Loureiro9956e5e2021-09-07 17:21:59 +00003289 xmlContent := module.permissionsContents(ctx)
Jiyong Parke3833882020-02-17 17:28:10 +09003290
3291 module.outputFilePath = android.PathForModuleOut(ctx, libName+".xml").OutputPath
Paul Duffin1816cde2024-04-10 10:58:21 +01003292 android.WriteFileRuleVerbatim(ctx, module.outputFilePath, xmlContent)
Jiyong Parke3833882020-02-17 17:28:10 +09003293
3294 module.installDirPath = android.PathForModuleInstall(ctx, "etc", module.SubDir())
Jeongik Cha00e09912024-04-23 05:07:13 +09003295 ctx.PackageFile(module.installDirPath, libName+".xml", module.outputFilePath)
mrziwange2346b82024-06-10 15:09:45 -07003296
3297 ctx.SetOutputFiles(android.OutputPaths{module.outputFilePath}.Paths(), "")
Jiyong Parke3833882020-02-17 17:28:10 +09003298}
3299
3300func (module *sdkLibraryXml) AndroidMkEntries() []android.AndroidMkEntries {
Colin Cross56a83212020-09-15 18:30:11 -07003301 if module.hideApexVariantFromMake {
satayev8f088b02021-12-06 11:40:46 +00003302 return []android.AndroidMkEntries{{
Jiyong Parke3833882020-02-17 17:28:10 +09003303 Disabled: true,
3304 }}
3305 }
3306
satayev8f088b02021-12-06 11:40:46 +00003307 return []android.AndroidMkEntries{{
Jiyong Parke3833882020-02-17 17:28:10 +09003308 Class: "ETC",
3309 OutputFile: android.OptionalPathForPath(module.outputFilePath),
3310 ExtraEntries: []android.AndroidMkExtraEntriesFunc{
Colin Crossaa255532020-07-03 13:18:24 -07003311 func(ctx android.AndroidMkExtraEntriesContext, entries *android.AndroidMkEntries) {
Jiyong Parke3833882020-02-17 17:28:10 +09003312 entries.SetString("LOCAL_MODULE_TAGS", "optional")
Colin Crossc68db4b2021-11-11 18:59:15 -08003313 entries.SetString("LOCAL_MODULE_PATH", module.installDirPath.String())
Jiyong Parke3833882020-02-17 17:28:10 +09003314 entries.SetString("LOCAL_INSTALLED_MODULE_STEM", module.outputFilePath.Base())
3315 },
3316 },
3317 }}
3318}
Paul Duffindd46f712020-02-10 13:37:10 +00003319
Pedro Loureiroc3621422021-09-28 15:40:23 +00003320func (module *sdkLibraryXml) selfValidate(ctx android.ModuleContext) {
3321 module.validateAtLeastTAttributes(ctx)
3322 module.validateMinAndMaxDeviceSdk(ctx)
3323 module.validateMinMaxDeviceSdkAndModuleMinSdk(ctx)
3324 module.validateOnBootclasspathBeforeRequirements(ctx)
3325}
3326
3327func (module *sdkLibraryXml) validateAtLeastTAttributes(ctx android.ModuleContext) {
3328 t := android.ApiLevelOrPanic(ctx, "Tiramisu")
3329 module.attrAtLeastT(ctx, t, module.properties.Min_device_sdk, "min_device_sdk")
3330 module.attrAtLeastT(ctx, t, module.properties.Max_device_sdk, "max_device_sdk")
3331 module.attrAtLeastT(ctx, t, module.properties.On_bootclasspath_before, "on_bootclasspath_before")
3332 module.attrAtLeastT(ctx, t, module.properties.On_bootclasspath_since, "on_bootclasspath_since")
3333}
3334
3335func (module *sdkLibraryXml) attrAtLeastT(ctx android.ModuleContext, t android.ApiLevel, attr *string, attrName string) {
3336 if attr != nil {
3337 if level, err := android.ApiLevelFromUser(ctx, *attr); err == nil {
3338 // we will inform the user of invalid inputs when we try to write the
3339 // permissions xml file so we don't need to do it here
3340 if t.GreaterThan(level) {
3341 ctx.PropertyErrorf(attrName, "Attribute value needs to be at least T")
3342 }
3343 }
3344 }
3345}
3346
3347func (module *sdkLibraryXml) validateMinAndMaxDeviceSdk(ctx android.ModuleContext) {
3348 if module.properties.Min_device_sdk != nil && module.properties.Max_device_sdk != nil {
3349 min, minErr := android.ApiLevelFromUser(ctx, *module.properties.Min_device_sdk)
3350 max, maxErr := android.ApiLevelFromUser(ctx, *module.properties.Max_device_sdk)
3351 if minErr == nil && maxErr == nil {
3352 // we will inform the user of invalid inputs when we try to write the
3353 // permissions xml file so we don't need to do it here
3354 if min.GreaterThan(max) {
3355 ctx.ModuleErrorf("min_device_sdk can't be greater than max_device_sdk")
3356 }
3357 }
3358 }
3359}
3360
3361func (module *sdkLibraryXml) validateMinMaxDeviceSdkAndModuleMinSdk(ctx android.ModuleContext) {
3362 moduleMinApi := android.ApiLevelOrPanic(ctx, *module.properties.Sdk_library_min_api_level)
3363 if module.properties.Min_device_sdk != nil {
3364 api, err := android.ApiLevelFromUser(ctx, *module.properties.Min_device_sdk)
3365 if err == nil {
3366 if moduleMinApi.GreaterThan(api) {
3367 ctx.PropertyErrorf("min_device_sdk", "Can't be less than module's min sdk (%s)", moduleMinApi)
3368 }
3369 }
3370 }
3371 if module.properties.Max_device_sdk != nil {
3372 api, err := android.ApiLevelFromUser(ctx, *module.properties.Max_device_sdk)
3373 if err == nil {
3374 if moduleMinApi.GreaterThan(api) {
3375 ctx.PropertyErrorf("max_device_sdk", "Can't be less than module's min sdk (%s)", moduleMinApi)
3376 }
3377 }
3378 }
3379}
3380
3381func (module *sdkLibraryXml) validateOnBootclasspathBeforeRequirements(ctx android.ModuleContext) {
3382 moduleMinApi := android.ApiLevelOrPanic(ctx, *module.properties.Sdk_library_min_api_level)
3383 if module.properties.On_bootclasspath_before != nil {
3384 t := android.ApiLevelOrPanic(ctx, "Tiramisu")
3385 // if we use the attribute, then we need to do this validation
3386 if moduleMinApi.LessThan(t) {
3387 // if minAPi is < T, then we need to have min_device_sdk (which only accepts T+)
3388 if module.properties.Min_device_sdk == nil {
3389 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")
3390 }
3391 }
3392 }
3393}
3394
Paul Duffindd46f712020-02-10 13:37:10 +00003395type sdkLibrarySdkMemberType struct {
3396 android.SdkMemberTypeBase
3397}
3398
Paul Duffin296701e2021-07-14 10:29:36 +01003399func (s *sdkLibrarySdkMemberType) AddDependencies(ctx android.SdkDependencyContext, dependencyTag blueprint.DependencyTag, names []string) {
3400 ctx.AddVariationDependencies(nil, dependencyTag, names...)
Paul Duffindd46f712020-02-10 13:37:10 +00003401}
3402
3403func (s *sdkLibrarySdkMemberType) IsInstance(module android.Module) bool {
3404 _, ok := module.(*SdkLibrary)
3405 return ok
3406}
3407
3408func (s *sdkLibrarySdkMemberType) AddPrebuiltModule(ctx android.SdkMemberContext, member android.SdkMember) android.BpModule {
3409 return ctx.SnapshotBuilder().AddPrebuiltModule(member, "java_sdk_library_import")
3410}
3411
3412func (s *sdkLibrarySdkMemberType) CreateVariantPropertiesStruct() android.SdkMemberProperties {
3413 return &sdkLibrarySdkMemberProperties{}
3414}
3415
Paul Duffin976b0e52021-04-27 23:20:26 +01003416var javaSdkLibrarySdkMemberType = &sdkLibrarySdkMemberType{
3417 android.SdkMemberTypeBase{
3418 PropertyName: "java_sdk_libs",
3419 SupportsSdk: true,
3420 },
3421}
3422
Paul Duffindd46f712020-02-10 13:37:10 +00003423type sdkLibrarySdkMemberProperties struct {
3424 android.SdkMemberPropertiesBase
3425
Paul Duffine8409952022-09-22 16:24:46 +01003426 // Stem name for files in the sdk snapshot.
3427 //
3428 // This is used to construct the path names of various sdk library files in the sdk snapshot to
3429 // make sure that they match the finalized versions of those files in prebuilts/sdk.
3430 //
3431 // This property is marked as keep so that it will be kept in all instances of this struct, will
3432 // not be cleared but will be copied to common structs. That is needed because this field is used
3433 // to construct many file names for other parts of this struct and so it needs to be present in
3434 // all structs. If it was not marked as keep then it would be cleared in some structs and so would
3435 // be unavailable for generating file names if there were other properties that were still set.
3436 Stem string `sdk:"keep"`
3437
Paul Duffindd46f712020-02-10 13:37:10 +00003438 // Scope to per scope properties.
Paul Duffin106a3a42022-01-27 16:39:06 +00003439 Scopes map[*apiScope]*scopeProperties
Paul Duffindd46f712020-02-10 13:37:10 +00003440
Paul Duffin3d1248c2020-04-09 00:10:17 +01003441 // The Java stubs source files.
3442 Stub_srcs []string
Paul Duffinf7a64332020-05-13 16:54:55 +01003443
3444 // The naming scheme.
3445 Naming_scheme *string
Paul Duffind7eb1c22020-05-26 20:57:10 +01003446
3447 // True if the java_sdk_library_import is for a shared library, false
3448 // otherwise.
3449 Shared_library *bool
Paul Duffina2ae7e02020-09-11 11:55:00 +01003450
Paul Duffin1267d872021-04-16 17:21:36 +01003451 // True if the stub imports should produce dex jars.
3452 Compile_dex *bool
3453
Paul Duffina2ae7e02020-09-11 11:55:00 +01003454 // The paths to the doctag files to add to the prebuilt.
3455 Doctag_paths android.Paths
Paul Duffin869de142021-07-15 14:14:41 +01003456
3457 Permitted_packages []string
Pedro Loureiro9956e5e2021-09-07 17:21:59 +00003458
3459 // Signals that this shared library is part of the bootclasspath starting
3460 // on the version indicated in this attribute.
3461 //
3462 // This will make platforms at this level and above to ignore
3463 // <uses-library> tags with this library name because the library is already
3464 // available
3465 On_bootclasspath_since *string
3466
3467 // Signals that this shared library was part of the bootclasspath before
3468 // (but not including) the version indicated in this attribute.
3469 //
3470 // The system will automatically add a <uses-library> tag with this library to
3471 // apps that target any SDK less than the version indicated in this attribute.
3472 On_bootclasspath_before *string
3473
3474 // Indicates that PackageManager should ignore this shared library if the
3475 // platform is below the version indicated in this attribute.
3476 //
3477 // This means that the device won't recognise this library as installed.
3478 Min_device_sdk *string
3479
3480 // Indicates that PackageManager should ignore this shared library if the
3481 // platform is above the version indicated in this attribute.
3482 //
3483 // This means that the device won't recognise this library as installed.
3484 Max_device_sdk *string
Jiakai Zhang9c4dc192023-02-09 00:09:24 +08003485
3486 DexPreoptProfileGuided *bool `supported_build_releases:"UpsideDownCake+"`
Paul Duffindd46f712020-02-10 13:37:10 +00003487}
3488
3489type scopeProperties struct {
Paul Duffin1fd005d2020-04-09 01:08:11 +01003490 Jars android.Paths
3491 StubsSrcJar android.Path
3492 CurrentApiFile android.Path
3493 RemovedApiFile android.Path
Paul Duffine7babdb2022-02-10 13:06:54 +00003494 AnnotationsZip android.Path `supported_build_releases:"Tiramisu+"`
Paul Duffin1fd005d2020-04-09 01:08:11 +01003495 SdkVersion string
Paul Duffindd46f712020-02-10 13:37:10 +00003496}
3497
3498func (s *sdkLibrarySdkMemberProperties) PopulateFromVariant(ctx android.SdkMemberContext, variant android.Module) {
3499 sdk := variant.(*SdkLibrary)
3500
Paul Duffine8409952022-09-22 16:24:46 +01003501 // Copy the stem name for files in the sdk snapshot.
3502 s.Stem = sdk.distStem()
3503
Paul Duffin106a3a42022-01-27 16:39:06 +00003504 s.Scopes = make(map[*apiScope]*scopeProperties)
Jihoon Kang98aa8fa2024-06-07 11:06:57 +00003505 for _, apiScope := range AllApiScopes {
Paul Duffin803a9562020-05-20 11:52:25 +01003506 paths := sdk.findScopePaths(apiScope)
3507 if paths == nil {
3508 continue
3509 }
3510
Paul Duffindd46f712020-02-10 13:37:10 +00003511 jars := paths.stubsImplPath
3512 if len(jars) > 0 {
3513 properties := scopeProperties{}
3514 properties.Jars = jars
Paul Duffin780c5f42020-05-12 15:52:55 +01003515 properties.SdkVersion = sdk.sdkVersionForStubsLibrary(ctx.SdkModuleContext(), apiScope)
Paul Duffin0f8faff2020-05-20 16:18:00 +01003516 properties.StubsSrcJar = paths.stubsSrcJar.Path()
Paul Duffin10269f12020-06-19 18:39:55 +01003517 if paths.currentApiFilePath.Valid() {
3518 properties.CurrentApiFile = paths.currentApiFilePath.Path()
3519 }
3520 if paths.removedApiFilePath.Valid() {
3521 properties.RemovedApiFile = paths.removedApiFilePath.Path()
3522 }
Anton Hanssond78eb762021-09-21 15:25:12 +01003523 // The annotations zip is only available for modules that set annotations_enabled: true.
3524 if paths.annotationsZip.Valid() {
3525 properties.AnnotationsZip = paths.annotationsZip.Path()
3526 }
Paul Duffin106a3a42022-01-27 16:39:06 +00003527 s.Scopes[apiScope] = &properties
Paul Duffindd46f712020-02-10 13:37:10 +00003528 }
3529 }
3530
Paul Duffindfa131e2020-05-15 20:37:11 +01003531 s.Naming_scheme = sdk.commonSdkLibraryProperties.Naming_scheme
Paul Duffind7eb1c22020-05-26 20:57:10 +01003532 s.Shared_library = proptools.BoolPtr(sdk.sharedLibrary())
Paul Duffin1267d872021-04-16 17:21:36 +01003533 s.Compile_dex = sdk.dexProperties.Compile_dex
Paul Duffina2ae7e02020-09-11 11:55:00 +01003534 s.Doctag_paths = sdk.doctagPaths
Paul Duffin869de142021-07-15 14:14:41 +01003535 s.Permitted_packages = sdk.PermittedPackagesForUpdatableBootJars()
Pedro Loureiro9956e5e2021-09-07 17:21:59 +00003536 s.On_bootclasspath_since = sdk.commonSdkLibraryProperties.On_bootclasspath_since
3537 s.On_bootclasspath_before = sdk.commonSdkLibraryProperties.On_bootclasspath_before
3538 s.Min_device_sdk = sdk.commonSdkLibraryProperties.Min_device_sdk
3539 s.Max_device_sdk = sdk.commonSdkLibraryProperties.Max_device_sdk
Jiakai Zhang9c4dc192023-02-09 00:09:24 +08003540
Jihoon Kanga3a05462024-04-05 00:36:44 +00003541 implLibrary := sdk.getImplLibraryModule()
3542 if implLibrary != nil && implLibrary.dexpreopter.dexpreoptProperties.Dex_preopt_result.Profile_guided {
Jiakai Zhang9c4dc192023-02-09 00:09:24 +08003543 s.DexPreoptProfileGuided = proptools.BoolPtr(true)
3544 }
Paul Duffindd46f712020-02-10 13:37:10 +00003545}
3546
3547func (s *sdkLibrarySdkMemberProperties) AddToPropertySet(ctx android.SdkMemberContext, propertySet android.BpPropertySet) {
Paul Duffinf7a64332020-05-13 16:54:55 +01003548 if s.Naming_scheme != nil {
3549 propertySet.AddProperty("naming_scheme", proptools.String(s.Naming_scheme))
3550 }
Paul Duffind7eb1c22020-05-26 20:57:10 +01003551 if s.Shared_library != nil {
3552 propertySet.AddProperty("shared_library", *s.Shared_library)
3553 }
Paul Duffin1267d872021-04-16 17:21:36 +01003554 if s.Compile_dex != nil {
3555 propertySet.AddProperty("compile_dex", *s.Compile_dex)
3556 }
Paul Duffin869de142021-07-15 14:14:41 +01003557 if len(s.Permitted_packages) > 0 {
3558 propertySet.AddProperty("permitted_packages", s.Permitted_packages)
3559 }
Jiakai Zhang9c4dc192023-02-09 00:09:24 +08003560 dexPreoptSet := propertySet.AddPropertySet("dex_preopt")
3561 if s.DexPreoptProfileGuided != nil {
3562 dexPreoptSet.AddProperty("profile_guided", proptools.Bool(s.DexPreoptProfileGuided))
3563 }
Paul Duffinf7a64332020-05-13 16:54:55 +01003564
Paul Duffine8409952022-09-22 16:24:46 +01003565 stem := s.Stem
3566
Jihoon Kang98aa8fa2024-06-07 11:06:57 +00003567 for _, apiScope := range AllApiScopes {
Paul Duffindd46f712020-02-10 13:37:10 +00003568 if properties, ok := s.Scopes[apiScope]; ok {
Paul Duffin6b836ba2020-05-13 19:19:49 +01003569 scopeSet := propertySet.AddPropertySet(apiScope.propertyName)
Paul Duffindd46f712020-02-10 13:37:10 +00003570
Paul Duffin958806b2022-05-16 13:10:47 +00003571 scopeDir := apiScope.snapshotRelativeDir()
Paul Duffin3d1248c2020-04-09 00:10:17 +01003572
Paul Duffindd46f712020-02-10 13:37:10 +00003573 var jars []string
3574 for _, p := range properties.Jars {
Paul Duffine8409952022-09-22 16:24:46 +01003575 dest := filepath.Join(scopeDir, stem+"-stubs.jar")
Paul Duffindd46f712020-02-10 13:37:10 +00003576 ctx.SnapshotBuilder().CopyToSnapshot(p, dest)
3577 jars = append(jars, dest)
3578 }
3579 scopeSet.AddProperty("jars", jars)
3580
Paul Duffin22628d52021-05-12 23:13:22 +01003581 if ctx.SdkModuleContext().Config().IsEnvTrue("SOONG_SDK_SNAPSHOT_USE_SRCJAR") {
3582 // Copy the stubs source jar into the snapshot zip as is.
Paul Duffine8409952022-09-22 16:24:46 +01003583 srcJarSnapshotPath := filepath.Join(scopeDir, stem+".srcjar")
Paul Duffin22628d52021-05-12 23:13:22 +01003584 ctx.SnapshotBuilder().CopyToSnapshot(properties.StubsSrcJar, srcJarSnapshotPath)
3585 scopeSet.AddProperty("stub_srcs", []string{srcJarSnapshotPath})
3586 } else {
3587 // Merge the stubs source jar into the snapshot zip so that when it is unpacked
3588 // the source files are also unpacked.
Paul Duffine8409952022-09-22 16:24:46 +01003589 snapshotRelativeDir := filepath.Join(scopeDir, stem+"_stub_sources")
Paul Duffin22628d52021-05-12 23:13:22 +01003590 ctx.SnapshotBuilder().UnzipToSnapshot(properties.StubsSrcJar, snapshotRelativeDir)
3591 scopeSet.AddProperty("stub_srcs", []string{snapshotRelativeDir})
3592 }
Paul Duffin3d1248c2020-04-09 00:10:17 +01003593
Paul Duffin1fd005d2020-04-09 01:08:11 +01003594 if properties.CurrentApiFile != nil {
Paul Duffine8409952022-09-22 16:24:46 +01003595 currentApiSnapshotPath := apiScope.snapshotRelativeCurrentApiTxtPath(stem)
Paul Duffin1fd005d2020-04-09 01:08:11 +01003596 ctx.SnapshotBuilder().CopyToSnapshot(properties.CurrentApiFile, currentApiSnapshotPath)
3597 scopeSet.AddProperty("current_api", currentApiSnapshotPath)
3598 }
3599
3600 if properties.RemovedApiFile != nil {
Paul Duffine8409952022-09-22 16:24:46 +01003601 removedApiSnapshotPath := apiScope.snapshotRelativeRemovedApiTxtPath(stem)
Paul Duffin3dbf9fd2020-06-02 13:00:02 +01003602 ctx.SnapshotBuilder().CopyToSnapshot(properties.RemovedApiFile, removedApiSnapshotPath)
Paul Duffin1fd005d2020-04-09 01:08:11 +01003603 scopeSet.AddProperty("removed_api", removedApiSnapshotPath)
3604 }
3605
Anton Hanssond78eb762021-09-21 15:25:12 +01003606 if properties.AnnotationsZip != nil {
Paul Duffine8409952022-09-22 16:24:46 +01003607 annotationsSnapshotPath := filepath.Join(scopeDir, stem+"_annotations.zip")
Anton Hanssond78eb762021-09-21 15:25:12 +01003608 ctx.SnapshotBuilder().CopyToSnapshot(properties.AnnotationsZip, annotationsSnapshotPath)
3609 scopeSet.AddProperty("annotations", annotationsSnapshotPath)
3610 }
3611
Paul Duffindd46f712020-02-10 13:37:10 +00003612 if properties.SdkVersion != "" {
3613 scopeSet.AddProperty("sdk_version", properties.SdkVersion)
3614 }
3615 }
3616 }
3617
Paul Duffina2ae7e02020-09-11 11:55:00 +01003618 if len(s.Doctag_paths) > 0 {
3619 dests := []string{}
3620 for _, p := range s.Doctag_paths {
3621 dest := filepath.Join("doctags", p.Rel())
3622 ctx.SnapshotBuilder().CopyToSnapshot(p, dest)
3623 dests = append(dests, dest)
3624 }
3625 propertySet.AddProperty("doctag_files", dests)
3626 }
Paul Duffindd46f712020-02-10 13:37:10 +00003627}