blob: 062a62f2c5da89c59e2bad780a80ca21bd635768 [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
121 // The tag to use to depend on the API file generating module (if separate from the stubs source module).
122 apiFileTag scopeDependencyTag
123
Paul Duffinc8782502020-04-29 20:45:27 +0100124 // The tag to use to depend on the stubs source and API module.
125 stubsSourceAndApiTag scopeDependencyTag
Paul Duffind1b3a922020-01-22 11:57:20 +0000126
Paul Duffin958806b2022-05-16 13:10:47 +0000127 // The tag to use to depend on the module that provides the latest version of the API .txt file.
128 latestApiModuleTag scopeDependencyTag
129
130 // The tag to use to depend on the module that provides the latest version of the API removed.txt
131 // file.
132 latestRemovedApiModuleTag scopeDependencyTag
133
Paul Duffind1b3a922020-01-22 11:57:20 +0000134 // The scope specific prefix to add to the api file base of "current.txt" or "removed.txt".
135 apiFilePrefix string
136
Paul Duffind0b9fca2022-09-30 18:11:41 +0100137 // The scope specific suffix to add to the sdk library module name to construct a scope specific
Paul Duffind1b3a922020-01-22 11:57:20 +0000138 // module name.
139 moduleSuffix string
140
Paul Duffind1b3a922020-01-22 11:57:20 +0000141 // SDK version that the stubs library is built against. Note that this is always
142 // *current. Older stubs library built with a numbered SDK version is created from
143 // the prebuilt jar.
144 sdkVersion string
Paul Duffin1fb487d2020-04-07 18:50:10 +0100145
Paul Duffin15f34ef2020-07-20 18:04:44 +0100146 // The annotation that identifies this API level, empty for the public API scope.
147 annotation string
148
Paul Duffin1fb487d2020-04-07 18:50:10 +0100149 // Extra arguments to pass to droidstubs for this scope.
Paul Duffin0ff08bd2020-04-29 13:30:54 +0100150 //
Paul Duffin15f34ef2020-07-20 18:04:44 +0100151 // This is not used directly but is used to construct the droidstubsArgs.
152 extraArgs []string
Paul Duffin0ff08bd2020-04-29 13:30:54 +0100153
Paul Duffin15f34ef2020-07-20 18:04:44 +0100154 // The args that must be passed to droidstubs to generate the API and stubs source
155 // for this scope, constructed dynamically by initApiScope().
Paul Duffin0ff08bd2020-04-29 13:30:54 +0100156 //
157 // The API only includes the additional members that this scope adds over the scope
158 // that it extends.
Paul Duffin15f34ef2020-07-20 18:04:44 +0100159 //
160 // The stubs source must include the definitions of everything that is in this
161 // api scope and all the scopes that this one extends.
162 droidstubsArgs []string
Paul Duffin0ff08bd2020-04-29 13:30:54 +0100163
Anton Hansson6478ac12020-05-02 11:19:36 +0100164 // Whether the api scope can be treated as unstable, and should skip compat checks.
165 unstable bool
Jihoon Kang1c92c3e2023-03-23 17:44:51 +0000166
167 // Represents the SDK kind of this scope.
168 kind android.SdkKind
Paul Duffind1b3a922020-01-22 11:57:20 +0000169}
170
171// Initialize a scope, creating and adding appropriate dependency tags
172func initApiScope(scope *apiScope) *apiScope {
Paul Duffinc8782502020-04-29 20:45:27 +0100173 name := scope.name
Paul Duffin46dc45a2020-05-14 15:39:10 +0100174 scopeByName[name] = scope
175 allScopeNames = append(allScopeNames, name)
Paul Duffin6b836ba2020-05-13 19:19:49 +0100176 scope.propertyName = strings.ReplaceAll(name, "-", "_")
177 scope.fieldName = proptools.FieldNameForProperty(scope.propertyName)
Jihoon Kangb7431552024-01-22 19:40:08 +0000178 scope.prebuiltStubsTag = scopeDependencyTag{
Paul Duffinc8782502020-04-29 20:45:27 +0100179 name: name + "-stubs",
180 apiScope: scope,
181 depInfoExtractor: (*scopePaths).extractStubsLibraryInfoFromDependency,
Paul Duffind1b3a922020-01-22 11:57:20 +0000182 }
Jihoon Kangbd093452023-12-26 19:08:01 +0000183 scope.everythingStubsTag = scopeDependencyTag{
184 name: name + "-stubs-everything",
185 apiScope: scope,
186 depInfoExtractor: (*scopePaths).extractEverythingStubsLibraryInfoFromDependency,
187 }
188 scope.exportableStubsTag = scopeDependencyTag{
189 name: name + "-stubs-exportable",
190 apiScope: scope,
191 depInfoExtractor: (*scopePaths).extractExportableStubsLibraryInfoFromDependency,
192 }
Paul Duffin0ff08bd2020-04-29 13:30:54 +0100193 scope.stubsSourceTag = scopeDependencyTag{
194 name: name + "-stubs-source",
195 apiScope: scope,
196 depInfoExtractor: (*scopePaths).extractStubsSourceInfoFromDep,
197 }
198 scope.apiFileTag = scopeDependencyTag{
199 name: name + "-api",
200 apiScope: scope,
201 depInfoExtractor: (*scopePaths).extractApiInfoFromDep,
202 }
Paul Duffinc8782502020-04-29 20:45:27 +0100203 scope.stubsSourceAndApiTag = scopeDependencyTag{
204 name: name + "-stubs-source-and-api",
205 apiScope: scope,
206 depInfoExtractor: (*scopePaths).extractStubsSourceAndApiInfoFromApiStubsProvider,
Paul Duffind1b3a922020-01-22 11:57:20 +0000207 }
Paul Duffin958806b2022-05-16 13:10:47 +0000208 scope.latestApiModuleTag = scopeDependencyTag{
209 name: name + "-latest-api",
210 apiScope: scope,
211 depInfoExtractor: (*scopePaths).extractLatestApiPath,
212 }
213 scope.latestRemovedApiModuleTag = scopeDependencyTag{
214 name: name + "-latest-removed-api",
215 apiScope: scope,
216 depInfoExtractor: (*scopePaths).extractLatestRemovedApiPath,
217 }
Paul Duffin0ff08bd2020-04-29 13:30:54 +0100218
219 // To get the args needed to generate the stubs source append all the args from
220 // this scope and all the scopes it extends as each set of args adds additional
221 // members to the stubs.
Paul Duffin15f34ef2020-07-20 18:04:44 +0100222 var scopeSpecificArgs []string
223 if scope.annotation != "" {
224 scopeSpecificArgs = []string{"--show-annotation", scope.annotation}
Paul Duffin0ff08bd2020-04-29 13:30:54 +0100225 }
Paul Duffin15f34ef2020-07-20 18:04:44 +0100226 for s := scope; s != nil; s = s.extends {
227 scopeSpecificArgs = append(scopeSpecificArgs, s.extraArgs...)
Paul Duffin0ff08bd2020-04-29 13:30:54 +0100228
Paul Duffin15f34ef2020-07-20 18:04:44 +0100229 // Ensure that the generated stubs includes all the API elements from the API scope
230 // that this scope extends.
231 if s != scope && s.annotation != "" {
232 scopeSpecificArgs = append(scopeSpecificArgs, "--show-for-stub-purposes-annotation", s.annotation)
233 }
234 }
Paul Duffin0ff08bd2020-04-29 13:30:54 +0100235
Paul Duffind0b9fca2022-09-30 18:11:41 +0100236 // By default, a library that can access a scope can also access the scope it extends.
237 if scope.canAccess == nil {
238 scope.canAccess = scope.extends
239 }
240
Paul Duffin15f34ef2020-07-20 18:04:44 +0100241 // Escape any special characters in the arguments. This is needed because droidstubs
242 // passes these directly to the shell command.
243 scope.droidstubsArgs = proptools.ShellEscapeList(scopeSpecificArgs)
Paul Duffin0ff08bd2020-04-29 13:30:54 +0100244
Paul Duffind1b3a922020-01-22 11:57:20 +0000245 return scope
246}
247
Anton Hansson08f476b2021-04-07 15:32:19 +0100248func (scope *apiScope) stubsLibraryModuleNameSuffix() string {
249 return ".stubs" + scope.moduleSuffix
250}
251
Jihoon Kangfa4a90d2023-12-20 02:53:38 +0000252func (scope *apiScope) exportableStubsLibraryModuleNameSuffix() string {
253 return ".stubs.exportable" + scope.moduleSuffix
254}
255
Jihoon Kang1c92c3e2023-03-23 17:44:51 +0000256func (scope *apiScope) apiLibraryModuleName(baseName string) string {
257 return scope.stubsLibraryModuleName(baseName) + ".from-text"
258}
259
Jihoon Kang1147b312023-06-08 23:25:57 +0000260func (scope *apiScope) sourceStubLibraryModuleName(baseName string) string {
261 return scope.stubsLibraryModuleName(baseName) + ".from-source"
262}
263
Jihoon Kangfa4a90d2023-12-20 02:53:38 +0000264func (scope *apiScope) exportableSourceStubsLibraryModuleName(baseName string) string {
265 return scope.exportableStubsLibraryModuleName(baseName) + ".from-source"
266}
267
Paul Duffinc3091c82020-05-08 14:16:20 +0100268func (scope *apiScope) stubsLibraryModuleName(baseName string) string {
Anton Hansson08f476b2021-04-07 15:32:19 +0100269 return baseName + scope.stubsLibraryModuleNameSuffix()
Paul Duffind1b3a922020-01-22 11:57:20 +0000270}
271
Jihoon Kangfa4a90d2023-12-20 02:53:38 +0000272func (scope *apiScope) exportableStubsLibraryModuleName(baseName string) string {
273 return baseName + scope.exportableStubsLibraryModuleNameSuffix()
274}
275
Paul Duffinc8782502020-04-29 20:45:27 +0100276func (scope *apiScope) stubsSourceModuleName(baseName string) string {
Paul Duffindd9d0742020-05-08 15:52:37 +0100277 return baseName + ".stubs.source" + scope.moduleSuffix
Paul Duffind1b3a922020-01-22 11:57:20 +0000278}
279
Paul Duffin0ff08bd2020-04-29 13:30:54 +0100280func (scope *apiScope) apiModuleName(baseName string) string {
Paul Duffindd9d0742020-05-08 15:52:37 +0100281 return baseName + ".api" + scope.moduleSuffix
Paul Duffin0ff08bd2020-04-29 13:30:54 +0100282}
283
Paul Duffin3375e352020-04-28 10:44:03 +0100284func (scope *apiScope) String() string {
285 return scope.name
286}
287
Paul Duffin958806b2022-05-16 13:10:47 +0000288// snapshotRelativeDir returns the snapshot directory into which the files related to scopes will
289// be stored.
290func (scope *apiScope) snapshotRelativeDir() string {
291 return filepath.Join("sdk_library", scope.name)
292}
293
294// snapshotRelativeCurrentApiTxtPath returns the snapshot path to the API .txt file for the named
295// library.
296func (scope *apiScope) snapshotRelativeCurrentApiTxtPath(name string) string {
297 return filepath.Join(scope.snapshotRelativeDir(), name+".txt")
298}
299
300// snapshotRelativeRemovedApiTxtPath returns the snapshot path to the removed API .txt file for the
301// named library.
302func (scope *apiScope) snapshotRelativeRemovedApiTxtPath(name string) string {
303 return filepath.Join(scope.snapshotRelativeDir(), name+"-removed.txt")
304}
305
Paul Duffind1b3a922020-01-22 11:57:20 +0000306type apiScopes []*apiScope
307
308func (scopes apiScopes) Strings(accessor func(*apiScope) string) []string {
309 var list []string
310 for _, scope := range scopes {
311 list = append(list, accessor(scope))
312 }
313 return list
314}
315
Jihoon Kanga96a7b12023-09-20 23:43:32 +0000316// Method that maps the apiScopes properties to the index of each apiScopes elements.
317// apiScopes property to be used as the key can be specified with the input accessor.
318// Only a string property of apiScope can be used as the key of the map.
319func (scopes apiScopes) MapToIndex(accessor func(*apiScope) string) map[string]int {
320 ret := make(map[string]int)
321 for i, scope := range scopes {
322 ret[accessor(scope)] = i
323 }
324 return ret
325}
326
Jiyong Parkc678ad32018-04-10 13:07:10 +0900327var (
Paul Duffin46dc45a2020-05-14 15:39:10 +0100328 scopeByName = make(map[string]*apiScope)
329 allScopeNames []string
Paul Duffind1b3a922020-01-22 11:57:20 +0000330 apiScopePublic = initApiScope(&apiScope{
Paul Duffin3375e352020-04-28 10:44:03 +0100331 name: "public",
332
333 // Public scope is enabled by default for both legacy and non-legacy modes.
334 legacyEnabledStatus: func(module *SdkLibrary) bool {
335 return true
336 },
337 defaultEnabledStatus: true,
338
339 scopeSpecificProperties: func(module *SdkLibrary) *ApiScopeProperties {
340 return &module.sdkLibraryProperties.Public
341 },
Paul Duffind1b3a922020-01-22 11:57:20 +0000342 sdkVersion: "current",
Jihoon Kang1c92c3e2023-03-23 17:44:51 +0000343 kind: android.SdkPublic,
Paul Duffind1b3a922020-01-22 11:57:20 +0000344 })
345 apiScopeSystem = initApiScope(&apiScope{
Paul Duffin3375e352020-04-28 10:44:03 +0100346 name: "system",
347 extends: apiScopePublic,
348 legacyEnabledStatus: (*SdkLibrary).generateTestAndSystemScopesByDefault,
349 scopeSpecificProperties: func(module *SdkLibrary) *ApiScopeProperties {
350 return &module.sdkLibraryProperties.System
351 },
Paul Duffin15f34ef2020-07-20 18:04:44 +0100352 apiFilePrefix: "system-",
353 moduleSuffix: ".system",
354 sdkVersion: "system_current",
355 annotation: "android.annotation.SystemApi(client=android.annotation.SystemApi.Client.PRIVILEGED_APPS)",
Jihoon Kang1c92c3e2023-03-23 17:44:51 +0000356 kind: android.SdkSystem,
Paul Duffind1b3a922020-01-22 11:57:20 +0000357 })
358 apiScopeTest = initApiScope(&apiScope{
Paul Duffin3375e352020-04-28 10:44:03 +0100359 name: "test",
Anton Hansson4fe970f2020-10-09 10:16:49 +0100360 extends: apiScopeSystem,
Paul Duffin3375e352020-04-28 10:44:03 +0100361 legacyEnabledStatus: (*SdkLibrary).generateTestAndSystemScopesByDefault,
362 scopeSpecificProperties: func(module *SdkLibrary) *ApiScopeProperties {
363 return &module.sdkLibraryProperties.Test
364 },
Paul Duffin15f34ef2020-07-20 18:04:44 +0100365 apiFilePrefix: "test-",
366 moduleSuffix: ".test",
367 sdkVersion: "test_current",
368 annotation: "android.annotation.TestApi",
369 unstable: true,
Jihoon Kang1c92c3e2023-03-23 17:44:51 +0000370 kind: android.SdkTest,
Paul Duffind1b3a922020-01-22 11:57:20 +0000371 })
Paul Duffin8f265b92020-04-28 14:13:56 +0100372 apiScopeModuleLib = initApiScope(&apiScope{
Paul Duffin6b836ba2020-05-13 19:19:49 +0100373 name: "module-lib",
Paul Duffin8f265b92020-04-28 14:13:56 +0100374 extends: apiScopeSystem,
Paul Duffin0c5bae52020-06-02 13:00:08 +0100375 // The module-lib scope is disabled by default in legacy mode.
Paul Duffin8f265b92020-04-28 14:13:56 +0100376 //
377 // Enabling this would break existing usages.
378 legacyEnabledStatus: func(module *SdkLibrary) bool {
379 return false
380 },
381 scopeSpecificProperties: func(module *SdkLibrary) *ApiScopeProperties {
382 return &module.sdkLibraryProperties.Module_lib
383 },
384 apiFilePrefix: "module-lib-",
385 moduleSuffix: ".module_lib",
386 sdkVersion: "module_current",
Paul Duffin15f34ef2020-07-20 18:04:44 +0100387 annotation: "android.annotation.SystemApi(client=android.annotation.SystemApi.Client.MODULE_LIBRARIES)",
Jihoon Kang1c92c3e2023-03-23 17:44:51 +0000388 kind: android.SdkModule,
Paul Duffin8f265b92020-04-28 14:13:56 +0100389 })
Paul Duffin0c5bae52020-06-02 13:00:08 +0100390 apiScopeSystemServer = initApiScope(&apiScope{
391 name: "system-server",
392 extends: apiScopePublic,
Paul Duffind0b9fca2022-09-30 18:11:41 +0100393
394 // The system-server scope can access the module-lib scope.
395 //
396 // A module that provides a system-server API is appended to the standard bootclasspath that is
397 // used by the system server. So, it should be able to access module-lib APIs provided by
398 // libraries on the bootclasspath.
399 canAccess: apiScopeModuleLib,
400
Paul Duffin0c5bae52020-06-02 13:00:08 +0100401 // The system-server scope is disabled by default in legacy mode.
402 //
403 // Enabling this would break existing usages.
404 legacyEnabledStatus: func(module *SdkLibrary) bool {
405 return false
406 },
407 scopeSpecificProperties: func(module *SdkLibrary) *ApiScopeProperties {
408 return &module.sdkLibraryProperties.System_server
409 },
410 apiFilePrefix: "system-server-",
411 moduleSuffix: ".system_server",
412 sdkVersion: "system_server_current",
Paul Duffin15f34ef2020-07-20 18:04:44 +0100413 annotation: "android.annotation.SystemApi(client=android.annotation.SystemApi.Client.SYSTEM_SERVER)",
414 extraArgs: []string{
415 "--hide-annotation", "android.annotation.Hide",
Paul Duffin0c5bae52020-06-02 13:00:08 +0100416 // com.android.* classes are okay in this interface"
Paul Duffin15f34ef2020-07-20 18:04:44 +0100417 "--hide", "InternalClasses",
Paul Duffin0c5bae52020-06-02 13:00:08 +0100418 },
Jihoon Kang1c92c3e2023-03-23 17:44:51 +0000419 kind: android.SdkSystemServer,
Paul Duffin0c5bae52020-06-02 13:00:08 +0100420 })
Paul Duffind1b3a922020-01-22 11:57:20 +0000421 allApiScopes = apiScopes{
422 apiScopePublic,
423 apiScopeSystem,
424 apiScopeTest,
Paul Duffin8f265b92020-04-28 14:13:56 +0100425 apiScopeModuleLib,
Paul Duffin0c5bae52020-06-02 13:00:08 +0100426 apiScopeSystemServer,
Paul Duffind1b3a922020-01-22 11:57:20 +0000427 }
Jihoon Kang0c705a42023-08-02 06:44:57 +0000428 apiLibraryAdditionalProperties = map[string]struct {
429 FullApiSurfaceStubLib string
430 AdditionalApiContribution string
431 }{
432 "legacy.i18n.module.platform.api": {
433 FullApiSurfaceStubLib: "legacy.core.platform.api.stubs",
434 AdditionalApiContribution: "i18n.module.public.api.stubs.source.api.contribution",
435 },
436 "stable.i18n.module.platform.api": {
437 FullApiSurfaceStubLib: "stable.core.platform.api.stubs",
438 AdditionalApiContribution: "i18n.module.public.api.stubs.source.api.contribution",
439 },
440 "conscrypt.module.platform.api": {
441 FullApiSurfaceStubLib: "stable.core.platform.api.stubs",
442 AdditionalApiContribution: "conscrypt.module.public.api.stubs.source.api.contribution",
443 },
444 }
Jiyong Parkc678ad32018-04-10 13:07:10 +0900445)
446
Jiyong Park82484c02018-04-23 21:41:26 +0900447var (
448 javaSdkLibrariesLock sync.Mutex
449)
450
Jiyong Parkc678ad32018-04-10 13:07:10 +0900451// TODO: these are big features that are currently missing
Jiyong Park1be96912018-05-28 18:02:19 +0900452// 1) disallowing linking to the runtime shared lib
453// 2) HTML generation
Jiyong Parkc678ad32018-04-10 13:07:10 +0900454
455func init() {
Paul Duffin43dc1cc2019-12-19 11:18:54 +0000456 RegisterSdkLibraryBuildComponents(android.InitRegistrationContext)
Jiyong Parkc678ad32018-04-10 13:07:10 +0900457
Jiyong Park82484c02018-04-23 21:41:26 +0900458 android.RegisterMakeVarsProvider(pctx, func(ctx android.MakeVarsContext) {
459 javaSdkLibraries := javaSdkLibraries(ctx.Config())
460 sort.Strings(*javaSdkLibraries)
461 ctx.Strict("JAVA_SDK_LIBRARIES", strings.Join(*javaSdkLibraries, " "))
462 })
Paul Duffindd46f712020-02-10 13:37:10 +0000463
464 // Register sdk member types.
Paul Duffin976b0e52021-04-27 23:20:26 +0100465 android.RegisterSdkMemberType(javaSdkLibrarySdkMemberType)
Jiyong Parkc678ad32018-04-10 13:07:10 +0900466}
467
Paul Duffin43dc1cc2019-12-19 11:18:54 +0000468func RegisterSdkLibraryBuildComponents(ctx android.RegistrationContext) {
469 ctx.RegisterModuleType("java_sdk_library", SdkLibraryFactory)
470 ctx.RegisterModuleType("java_sdk_library_import", sdkLibraryImportFactory)
471}
472
Paul Duffin3375e352020-04-28 10:44:03 +0100473// Properties associated with each api scope.
474type ApiScopeProperties struct {
475 // Indicates whether the api surface is generated.
476 //
477 // If this is set for any scope then all scopes must explicitly specify if they
478 // are enabled. This is to prevent new usages from depending on legacy behavior.
479 //
480 // Otherwise, if this is not set for any scope then the default behavior is
481 // scope specific so please refer to the scope specific property documentation.
482 Enabled *bool
Paul Duffin87a05a32020-05-12 11:50:28 +0100483
484 // The sdk_version to use for building the stubs.
485 //
486 // If not specified then it will use an sdk_version determined as follows:
Trevor Radcliffedf8aa1f2021-11-04 14:25:39 +0000487 //
Paul Duffin87a05a32020-05-12 11:50:28 +0100488 // 1) If the sdk_version specified on the java_sdk_library is none then this
Trevor Radcliffedf8aa1f2021-11-04 14:25:39 +0000489 // will be none. This is used for java_sdk_library instances that are used
490 // to create stubs that contribute to the core_current sdk version.
491 // 2) Otherwise, it is assumed that this library extends but does not
492 // contribute directly to a specific sdk_version and so this uses the
493 // sdk_version appropriate for the api scope. e.g. public will use
494 // sdk_version: current, system will use sdk_version: system_current, etc.
Paul Duffin87a05a32020-05-12 11:50:28 +0100495 //
496 // This does not affect the sdk_version used for either generating the stubs source
497 // or the API file. They both have to use the same sdk_version as is used for
498 // compiling the implementation library.
499 Sdk_version *string
Mark White9421c4c2023-08-10 00:07:03 +0000500
501 // Extra libs used when compiling stubs for this scope.
502 Libs []string
Paul Duffin3375e352020-04-28 10:44:03 +0100503}
504
Jiyong Parkc678ad32018-04-10 13:07:10 +0900505type sdkLibraryProperties struct {
Anton Hanssonf8ea3722021-09-16 14:24:13 +0100506 // List of source files that are needed to compile the API, but are not part of runtime library.
507 Api_srcs []string `android:"arch_variant"`
508
Paul Duffin5df79302020-05-16 15:52:12 +0100509 // Visibility for impl library module. If not specified then defaults to the
510 // visibility property.
511 Impl_library_visibility []string
512
Paul Duffin4911a892020-04-29 23:35:13 +0100513 // Visibility for stubs library modules. If not specified then defaults to the
514 // visibility property.
515 Stubs_library_visibility []string
516
517 // Visibility for stubs source modules. If not specified then defaults to the
518 // visibility property.
519 Stubs_source_visibility []string
520
Anton Hansson7f66efa2020-10-08 14:47:23 +0100521 // List of Java libraries that will be in the classpath when building the implementation lib
522 Impl_only_libs []string `android:"arch_variant"`
523
Paul Duffin77590a82022-04-28 14:13:30 +0000524 // List of Java libraries that will included in the implementation lib.
525 Impl_only_static_libs []string `android:"arch_variant"`
526
Sundong Ahnf043cf62018-06-25 16:04:37 +0900527 // List of Java libraries that will be in the classpath when building stubs
528 Stub_only_libs []string `android:"arch_variant"`
529
Anton Hanssondae54cd2021-04-21 16:30:10 +0100530 // List of Java libraries that will included in stub libraries
531 Stub_only_static_libs []string `android:"arch_variant"`
532
Paul Duffin7a586d32019-12-30 17:09:34 +0000533 // list of package names that will be documented and publicized as API.
534 // This allows the API to be restricted to a subset of the source files provided.
535 // If this is unspecified then all the source files will be treated as being part
536 // of the API.
Jiyong Parkc678ad32018-04-10 13:07:10 +0900537 Api_packages []string
538
Jiyong Park5a2c9d72018-05-01 22:25:41 +0900539 // list of package names that must be hidden from the API
540 Hidden_api_packages []string
541
Paul Duffin749f98f2019-12-30 17:23:46 +0000542 // the relative path to the directory containing the api specification files.
543 // Defaults to "api".
544 Api_dir *string
545
Paul Duffindfa131e2020-05-15 20:37:11 +0100546 // Determines whether a runtime implementation library is built; defaults to false.
547 //
548 // 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 +0200549 // it is as if shared_library: false, was set.
Paul Duffin43db9be2019-12-30 17:35:49 +0000550 Api_only *bool
551
Paul Duffin11512472019-02-11 15:55:17 +0000552 // local files that are used within user customized droiddoc options.
553 Droiddoc_option_files []string
554
Spandan Das93e95992021-07-29 18:26:39 +0000555 // additional droiddoc options.
Paul Duffin11512472019-02-11 15:55:17 +0000556 // Available variables for substitution:
557 //
558 // $(location <label>): the path to the droiddoc_option_files with name <label>
Sundong Ahndd567f92018-07-31 17:19:11 +0900559 Droiddoc_options []string
560
Paul Duffine22c2ab2020-05-20 19:35:27 +0100561 // is set to true, Metalava will allow framework SDK to contain annotations.
562 Annotations_enabled *bool
563
Sundong Ahn054b19a2018-10-19 13:46:09 +0900564 // a list of top-level directories containing files to merge qualifier annotations
565 // (i.e. those intended to be included in the stubs written) from.
566 Merge_annotations_dirs []string
567
568 // a list of top-level directories containing Java stub files to merge show/hide annotations from.
569 Merge_inclusion_annotations_dirs []string
570
Paul Duffin4f5c1ef2020-11-19 14:53:43 +0000571 // If set to true then don't create dist rules.
572 No_dist *bool
Sundong Ahn80a87b32019-05-13 15:02:50 +0900573
Paul Duffin31310252020-11-20 21:26:20 +0000574 // The stem for the artifacts that are copied to the dist, if not specified
575 // then defaults to the base module name.
576 //
577 // For each scope the following artifacts are copied to the apistubs/<scope>
578 // directory in the dist.
579 // * stubs impl jar -> <dist-stem>.jar
580 // * API specification file -> api/<dist-stem>.txt
581 // * Removed API specification file -> api/<dist-stem>-removed.txt
582 //
583 // Also used to construct the name of the filegroup (created by prebuilt_apis)
584 // that references the latest released API and remove API specification files.
585 // * API specification filegroup -> <dist-stem>.api.<scope>.latest
586 // * Removed API specification filegroup -> <dist-stem>-removed.api.<scope>.latest
Jaewoong Jung1a97ee02021-03-09 13:25:02 -0800587 // * API incompatibilities baseline filegroup -> <dist-stem>-incompatibilities.api.<scope>.latest
Paul Duffin31310252020-11-20 21:26:20 +0000588 Dist_stem *string
589
Colin Cross986b69a2021-06-01 13:13:40 -0700590 // The subdirectory for the artifacts that are copied to the dist directory. If not specified
Colin Cross3dd66252021-06-01 14:05:09 -0700591 // then defaults to "unknown". Should be set to "android" for anything that should be published
Colin Cross986b69a2021-06-01 13:13:40 -0700592 // in the public Android SDK.
593 Dist_group *string
594
Anton Hanssondff2c782020-12-21 17:10:01 +0000595 // A compatibility mode that allows historical API-tracking files to not exist.
596 // Do not use.
597 Unsafe_ignore_missing_latest_api bool
598
Paul Duffin3375e352020-04-28 10:44:03 +0100599 // indicates whether system and test apis should be generated.
600 Generate_system_and_test_apis bool `blueprint:"mutated"`
601
602 // The properties specific to the public api scope
603 //
604 // Unless explicitly specified by using public.enabled the public api scope is
605 // enabled by default in both legacy and non-legacy mode.
606 Public ApiScopeProperties
607
608 // The properties specific to the system api scope
609 //
610 // In legacy mode the system api scope is enabled by default when sdk_version
611 // is set to something other than "none".
612 //
613 // In non-legacy mode the system api scope is disabled by default.
614 System ApiScopeProperties
615
616 // The properties specific to the test api scope
617 //
618 // In legacy mode the test api scope is enabled by default when sdk_version
619 // is set to something other than "none".
620 //
621 // In non-legacy mode the test api scope is disabled by default.
622 Test ApiScopeProperties
Paul Duffin37e0b772019-12-30 17:20:10 +0000623
Paul Duffin0c5bae52020-06-02 13:00:08 +0100624 // The properties specific to the module-lib api scope
Paul Duffin8f265b92020-04-28 14:13:56 +0100625 //
Zi Wangb2179e32023-01-31 15:53:30 -0800626 // Unless explicitly specified by using module_lib.enabled the module_lib api
627 // scope is disabled by default.
Paul Duffin8f265b92020-04-28 14:13:56 +0100628 Module_lib ApiScopeProperties
629
Paul Duffin0c5bae52020-06-02 13:00:08 +0100630 // The properties specific to the system-server api scope
631 //
Zi Wangb2179e32023-01-31 15:53:30 -0800632 // Unless explicitly specified by using system_server.enabled the
633 // system_server api scope is disabled by default.
Paul Duffin0c5bae52020-06-02 13:00:08 +0100634 System_server ApiScopeProperties
635
Jiyong Park932cdfe2020-05-28 00:19:53 +0900636 // Determines if the stubs are preferred over the implementation library
637 // for linking, even when the client doesn't specify sdk_version. When this
638 // is set to true, such clients are provided with the widest API surface that
639 // this lib provides. Note however that this option doesn't affect the clients
640 // that are in the same APEX as this library. In that case, the clients are
641 // always linked with the implementation library. Default is false.
642 Default_to_stubs *bool
643
Paul Duffin160fe412020-05-10 19:32:20 +0100644 // Properties related to api linting.
645 Api_lint struct {
646 // Enable api linting.
647 Enabled *bool
Anton Hanssonfd1c0d22023-11-02 15:18:09 +0000648
649 // If API lint is enabled, this flag controls whether a set of legitimate lint errors
650 // are turned off. The default is true.
651 Legacy_errors_allowed *bool
Paul Duffin160fe412020-05-10 19:32:20 +0100652 }
653
Jihoon Kang80456fd2023-11-15 19:22:14 +0000654 // Determines if the module contributes to any api surfaces.
655 // This property should be set to true only if the module is listed under
656 // frameworks-base-api.bootclasspath in frameworks/base/api/Android.bp.
657 // Otherwise, this property should be set to false.
658 // Defaults to false.
659 Contribute_to_android_api *bool
660
Jihoon Kang6592e872023-12-19 01:13:16 +0000661 // a list of aconfig_declarations module names that the stubs generated in this module
662 // depend on.
663 Aconfig_declarations []string
664
Jiyong Parkc678ad32018-04-10 13:07:10 +0900665 // TODO: determines whether to create HTML doc or not
Paul Duffine8409952022-09-22 16:24:46 +0100666 // Html_doc *bool
Jiyong Parkc678ad32018-04-10 13:07:10 +0900667}
668
Paul Duffin0f8faff2020-05-20 16:18:00 +0100669// Paths to outputs from java_sdk_library and java_sdk_library_import.
670//
671// Fields that are android.Paths are always set (during GenerateAndroidBuildActions).
672// OptionalPaths are always set by java_sdk_library but may not be set by
673// java_sdk_library_import as not all instances provide that information.
Paul Duffind1b3a922020-01-22 11:57:20 +0000674type scopePaths struct {
Paul Duffin0f8faff2020-05-20 16:18:00 +0100675 // The path (represented as Paths for convenience when returning) to the stubs header jar.
676 //
677 // That is the jar that is created by turbine.
678 stubsHeaderPath android.Paths
679
680 // The path (represented as Paths for convenience when returning) to the stubs implementation jar.
681 //
682 // This is not the implementation jar, it still only contains stubs.
683 stubsImplPath android.Paths
684
Paul Duffin1267d872021-04-16 17:21:36 +0100685 // The dex jar for the stubs.
686 //
687 // This is not the implementation jar, it still only contains stubs.
Martin Stjernholm8be1e6d2021-09-15 03:34:04 +0100688 stubsDexJarPath OptionalDexJarPath
Paul Duffin1267d872021-04-16 17:21:36 +0100689
Jihoon Kangbd093452023-12-26 19:08:01 +0000690 // The exportable dex jar for the stubs.
691 // This is not the implementation jar, it still only contains stubs.
692 // Includes unflagged apis and flagged apis enabled by release configurations.
693 exportableStubsDexJarPath OptionalDexJarPath
694
Paul Duffin0f8faff2020-05-20 16:18:00 +0100695 // The API specification file, e.g. system_current.txt.
696 currentApiFilePath android.OptionalPath
697
698 // The specification of API elements removed since the last release.
699 removedApiFilePath android.OptionalPath
700
701 // The stubs source jar.
702 stubsSrcJar android.OptionalPath
Anton Hanssond78eb762021-09-21 15:25:12 +0100703
704 // Extracted annotations.
705 annotationsZip android.OptionalPath
Paul Duffin958806b2022-05-16 13:10:47 +0000706
707 // The path to the latest API file.
Jihoon Kang5623e542024-01-31 23:27:26 +0000708 latestApiPaths android.Paths
Paul Duffin958806b2022-05-16 13:10:47 +0000709
710 // The path to the latest removed API file.
Jihoon Kang5623e542024-01-31 23:27:26 +0000711 latestRemovedApiPaths android.Paths
Paul Duffind1b3a922020-01-22 11:57:20 +0000712}
713
Colin Crossdcf71b22021-02-01 13:59:03 -0800714func (paths *scopePaths) extractStubsLibraryInfoFromDependency(ctx android.ModuleContext, dep android.Module) error {
Colin Cross313aa542023-12-13 13:47:44 -0800715 if lib, ok := android.OtherModuleProvider(ctx, dep, JavaInfoProvider); ok {
Colin Crossdcf71b22021-02-01 13:59:03 -0800716 paths.stubsHeaderPath = lib.HeaderJars
717 paths.stubsImplPath = lib.ImplementationJars
Paul Duffin1267d872021-04-16 17:21:36 +0100718
719 libDep := dep.(UsesLibraryDependency)
Spandan Das59a4a2b2024-01-09 21:35:56 +0000720 paths.stubsDexJarPath = libDep.DexJarBuildPath(ctx)
Jihoon Kangbd093452023-12-26 19:08:01 +0000721 paths.exportableStubsDexJarPath = libDep.DexJarBuildPath(ctx)
722 return nil
723 } else {
724 return fmt.Errorf("expected module that has JavaInfoProvider, e.g. java_library")
725 }
726}
727
728func (paths *scopePaths) extractEverythingStubsLibraryInfoFromDependency(ctx android.ModuleContext, dep android.Module) error {
729 if lib, ok := android.OtherModuleProvider(ctx, dep, JavaInfoProvider); ok {
730 paths.stubsHeaderPath = lib.HeaderJars
Jihoon Kangf55a5f72024-01-08 08:56:20 +0000731 if !ctx.Config().ReleaseHiddenApiExportableStubs() {
732 paths.stubsImplPath = lib.ImplementationJars
733 }
Jihoon Kangbd093452023-12-26 19:08:01 +0000734
735 libDep := dep.(UsesLibraryDependency)
736 paths.stubsDexJarPath = libDep.DexJarBuildPath(ctx)
737 return nil
738 } else {
739 return fmt.Errorf("expected module that has JavaInfoProvider, e.g. java_library")
740 }
741}
742
743func (paths *scopePaths) extractExportableStubsLibraryInfoFromDependency(ctx android.ModuleContext, dep android.Module) error {
Jihoon Kangf55a5f72024-01-08 08:56:20 +0000744 if lib, ok := android.OtherModuleProvider(ctx, dep, JavaInfoProvider); ok {
745 if ctx.Config().ReleaseHiddenApiExportableStubs() {
746 paths.stubsImplPath = lib.ImplementationJars
747 }
748
Jihoon Kangbd093452023-12-26 19:08:01 +0000749 libDep := dep.(UsesLibraryDependency)
750 paths.exportableStubsDexJarPath = libDep.DexJarBuildPath(ctx)
Paul Duffinc8782502020-04-29 20:45:27 +0100751 return nil
752 } else {
Colin Crossdcf71b22021-02-01 13:59:03 -0800753 return fmt.Errorf("expected module that has JavaInfoProvider, e.g. java_library")
Paul Duffinc8782502020-04-29 20:45:27 +0100754 }
755}
756
Jihoon Kangee113282024-01-23 00:16:41 +0000757func (paths *scopePaths) treatDepAsApiStubsProvider(dep android.Module, action func(provider ApiStubsProvider) error) error {
Paul Duffin0ff08bd2020-04-29 13:30:54 +0100758 if apiStubsProvider, ok := dep.(ApiStubsProvider); ok {
Jihoon Kangee113282024-01-23 00:16:41 +0000759 err := action(apiStubsProvider)
760 if err != nil {
761 return err
762 }
Jihoon Kangf55a5f72024-01-08 08:56:20 +0000763 return nil
764 } else {
765 return fmt.Errorf("expected module that implements ExportableApiStubsSrcProvider, e.g. droidstubs")
766 }
767}
768
Jihoon Kangee113282024-01-23 00:16:41 +0000769func (paths *scopePaths) treatDepAsApiStubsSrcProvider(dep android.Module, action func(provider ApiStubsSrcProvider) error) error {
Paul Duffin0f8faff2020-05-20 16:18:00 +0100770 if apiStubsProvider, ok := dep.(ApiStubsSrcProvider); ok {
Jihoon Kangee113282024-01-23 00:16:41 +0000771 err := action(apiStubsProvider)
772 if err != nil {
773 return err
774 }
Paul Duffin0f8faff2020-05-20 16:18:00 +0100775 return nil
776 } else {
777 return fmt.Errorf("expected module that implements ApiStubsSrcProvider, e.g. droidstubs")
778 }
779}
780
Jihoon Kangee113282024-01-23 00:16:41 +0000781func (paths *scopePaths) extractApiInfoFromApiStubsProvider(provider ApiStubsProvider, stubsType StubsType) error {
782 var annotationsZip, currentApiFilePath, removedApiFilePath android.Path
783 annotationsZip, annotationsZipErr := provider.AnnotationsZip(stubsType)
784 currentApiFilePath, currentApiFilePathErr := provider.ApiFilePath(stubsType)
785 removedApiFilePath, removedApiFilePathErr := provider.RemovedApiFilePath(stubsType)
Paul Duffin0ff08bd2020-04-29 13:30:54 +0100786
Jihoon Kangee113282024-01-23 00:16:41 +0000787 combinedError := errors.Join(annotationsZipErr, currentApiFilePathErr, removedApiFilePathErr)
788
789 if combinedError == nil {
790 paths.annotationsZip = android.OptionalPathForPath(annotationsZip)
791 paths.currentApiFilePath = android.OptionalPathForPath(currentApiFilePath)
792 paths.removedApiFilePath = android.OptionalPathForPath(removedApiFilePath)
793 }
794 return combinedError
Jihoon Kangf55a5f72024-01-08 08:56:20 +0000795}
796
Colin Crossdcf71b22021-02-01 13:59:03 -0800797func (paths *scopePaths) extractApiInfoFromDep(ctx android.ModuleContext, dep android.Module) error {
Jihoon Kangee113282024-01-23 00:16:41 +0000798 return paths.treatDepAsApiStubsProvider(dep, func(provider ApiStubsProvider) error {
799 return paths.extractApiInfoFromApiStubsProvider(provider, Everything)
Paul Duffin0ff08bd2020-04-29 13:30:54 +0100800 })
801}
802
Jihoon Kangee113282024-01-23 00:16:41 +0000803func (paths *scopePaths) extractStubsSourceInfoFromApiStubsProviders(provider ApiStubsSrcProvider, stubsType StubsType) error {
804 stubsSrcJar, err := provider.StubsSrcJar(stubsType)
805 if err == nil {
806 paths.stubsSrcJar = android.OptionalPathForPath(stubsSrcJar)
807 }
808 return err
Jihoon Kangf55a5f72024-01-08 08:56:20 +0000809}
810
Colin Crossdcf71b22021-02-01 13:59:03 -0800811func (paths *scopePaths) extractStubsSourceInfoFromDep(ctx android.ModuleContext, dep android.Module) error {
Jihoon Kangee113282024-01-23 00:16:41 +0000812 return paths.treatDepAsApiStubsSrcProvider(dep, func(provider ApiStubsSrcProvider) error {
813 return paths.extractStubsSourceInfoFromApiStubsProviders(provider, Everything)
Paul Duffin0ff08bd2020-04-29 13:30:54 +0100814 })
815}
816
Colin Crossdcf71b22021-02-01 13:59:03 -0800817func (paths *scopePaths) extractStubsSourceAndApiInfoFromApiStubsProvider(ctx android.ModuleContext, dep android.Module) error {
Jihoon Kangf55a5f72024-01-08 08:56:20 +0000818 if ctx.Config().ReleaseHiddenApiExportableStubs() {
Jihoon Kangee113282024-01-23 00:16:41 +0000819 return paths.treatDepAsApiStubsProvider(dep, func(provider ApiStubsProvider) error {
820 extractApiInfoErr := paths.extractApiInfoFromApiStubsProvider(provider, Exportable)
821 extractStubsSourceInfoErr := paths.extractStubsSourceInfoFromApiStubsProviders(provider, Exportable)
822 return errors.Join(extractApiInfoErr, extractStubsSourceInfoErr)
Jihoon Kangf55a5f72024-01-08 08:56:20 +0000823 })
824 }
Jihoon Kangee113282024-01-23 00:16:41 +0000825 return paths.treatDepAsApiStubsProvider(dep, func(provider ApiStubsProvider) error {
826 extractApiInfoErr := paths.extractApiInfoFromApiStubsProvider(provider, Everything)
827 extractStubsSourceInfoErr := paths.extractStubsSourceInfoFromApiStubsProviders(provider, Everything)
828 return errors.Join(extractApiInfoErr, extractStubsSourceInfoErr)
Paul Duffin0ff08bd2020-04-29 13:30:54 +0100829 })
830}
831
Jihoon Kang5623e542024-01-31 23:27:26 +0000832func extractOutputPaths(dep android.Module) (android.Paths, error) {
Paul Duffin958806b2022-05-16 13:10:47 +0000833 var paths android.Paths
834 if sourceFileProducer, ok := dep.(android.SourceFileProducer); ok {
835 paths = sourceFileProducer.Srcs()
Jihoon Kang5623e542024-01-31 23:27:26 +0000836 return paths, nil
Paul Duffin958806b2022-05-16 13:10:47 +0000837 } else {
Jihoon Kang5623e542024-01-31 23:27:26 +0000838 return nil, fmt.Errorf("module %q does not produce source files", dep)
Paul Duffin958806b2022-05-16 13:10:47 +0000839 }
Paul Duffin958806b2022-05-16 13:10:47 +0000840}
841
842func (paths *scopePaths) extractLatestApiPath(ctx android.ModuleContext, dep android.Module) error {
Jihoon Kang5623e542024-01-31 23:27:26 +0000843 outputPaths, err := extractOutputPaths(dep)
844 paths.latestApiPaths = outputPaths
Paul Duffin958806b2022-05-16 13:10:47 +0000845 return err
846}
847
848func (paths *scopePaths) extractLatestRemovedApiPath(ctx android.ModuleContext, dep android.Module) error {
Jihoon Kang5623e542024-01-31 23:27:26 +0000849 outputPaths, err := extractOutputPaths(dep)
850 paths.latestRemovedApiPaths = outputPaths
Paul Duffin958806b2022-05-16 13:10:47 +0000851 return err
852}
853
Paul Duffin0ff08bd2020-04-29 13:30:54 +0100854type commonToSdkLibraryAndImportProperties struct {
Paul Duffin1b1e8062020-05-08 13:44:43 +0100855 // The naming scheme to use for the components that this module creates.
856 //
Paul Duffinee9ad5d2020-09-11 13:04:05 +0100857 // If not specified then it defaults to "default".
Paul Duffin1b1e8062020-05-08 13:44:43 +0100858 //
859 // This is a temporary mechanism to simplify conversion from separate modules for each
860 // component that follow a different naming pattern to the default one.
861 //
862 // TODO(b/155480189) - Remove once naming inconsistencies have been resolved.
Paul Duffin0ff08bd2020-04-29 13:30:54 +0100863 Naming_scheme *string
Paul Duffindfa131e2020-05-15 20:37:11 +0100864
865 // Specifies whether this module can be used as an Android shared library; defaults
866 // to true.
867 //
868 // An Android shared library is one that can be referenced in a <uses-library> element
869 // in an AndroidManifest.xml.
870 Shared_library *bool
Paul Duffina2ae7e02020-09-11 11:55:00 +0100871
872 // Files containing information about supported java doc tags.
873 Doctag_files []string `android:"path"`
Pedro Loureiro9956e5e2021-09-07 17:21:59 +0000874
875 // Signals that this shared library is part of the bootclasspath starting
876 // on the version indicated in this attribute.
877 //
878 // This will make platforms at this level and above to ignore
879 // <uses-library> tags with this library name because the library is already
880 // available
881 On_bootclasspath_since *string
882
883 // Signals that this shared library was part of the bootclasspath before
884 // (but not including) the version indicated in this attribute.
885 //
886 // The system will automatically add a <uses-library> tag with this library to
887 // apps that target any SDK less than the version indicated in this attribute.
888 On_bootclasspath_before *string
889
890 // Indicates that PackageManager should ignore this shared library if the
891 // platform is below the version indicated in this attribute.
892 //
893 // This means that the device won't recognise this library as installed.
894 Min_device_sdk *string
895
896 // Indicates that PackageManager should ignore this shared library if the
897 // platform is above the version indicated in this attribute.
898 //
899 // This means that the device won't recognise this library as installed.
900 Max_device_sdk *string
Paul Duffin0ff08bd2020-04-29 13:30:54 +0100901}
902
Paul Duffin71b33cc2021-06-23 11:39:47 +0100903// commonSdkLibraryAndImportModule defines the interface that must be provided by a module that
904// embeds the commonToSdkLibraryAndImport struct.
905type commonSdkLibraryAndImportModule interface {
Paul Duffind796f6f2022-11-23 23:06:05 +0000906 android.Module
Paul Duffin71b33cc2021-06-23 11:39:47 +0100907
Spandan Das23956d12024-01-19 00:22:22 +0000908 // Returns the name of the root java_sdk_library that creates the child stub libraries
909 // This is the `name` as it appears in Android.bp, and not the name in Soong's build graph
910 // (with the prebuilt_ prefix)
911 //
912 // e.g. in the following java_sdk_library_import
913 // java_sdk_library_import {
914 // name: "framework-foo.v1",
915 // source_module_name: "framework-foo",
916 // }
917 // the values returned by
918 // 1. Name(): prebuilt_framework-foo.v1 # unique
919 // 2. BaseModuleName(): framework-foo # the source
920 // 3. RootLibraryName: framework-foo.v1 # the undecordated `name` from Android.bp
921 RootLibraryName() string
922}
923
924func (m *SdkLibrary) RootLibraryName() string {
925 return m.BaseModuleName()
926}
927
928func (m *SdkLibraryImport) RootLibraryName() string {
929 // m.BaseModuleName refers to the source of the import
930 // use moduleBase.Name to get the name of the module as it appears in the .bp file
931 return m.ModuleBase.Name()
Paul Duffin71b33cc2021-06-23 11:39:47 +0100932}
933
Paul Duffin56d44902020-01-31 13:36:25 +0000934// Common code between sdk library and sdk library import
935type commonToSdkLibraryAndImport struct {
Paul Duffin71b33cc2021-06-23 11:39:47 +0100936 module commonSdkLibraryAndImportModule
Paul Duffinc3091c82020-05-08 14:16:20 +0100937
Paul Duffin56d44902020-01-31 13:36:25 +0000938 scopePaths map[*apiScope]*scopePaths
Paul Duffin1b1e8062020-05-08 13:44:43 +0100939
940 namingScheme sdkLibraryComponentNamingScheme
941
Paul Duffindfa131e2020-05-15 20:37:11 +0100942 commonSdkLibraryProperties commonToSdkLibraryAndImportProperties
Paul Duffin859fe962020-05-15 10:20:31 +0100943
Paul Duffina2ae7e02020-09-11 11:55:00 +0100944 // Paths to commonSdkLibraryProperties.Doctag_files
945 doctagPaths android.Paths
946
Paul Duffin859fe962020-05-15 10:20:31 +0100947 // Functionality related to this being used as a component of a java_sdk_library.
948 EmbeddableSdkLibraryComponent
Jihoon Kang8479dea2024-04-04 01:19:05 +0000949
950 // Path to the header jars of the implementation library
951 // This is non-empty only when api_only is false.
952 implLibraryHeaderJars android.Paths
Jihoon Kanga3a05462024-04-05 00:36:44 +0000953
954 // The reference to the implementation library created by the source module.
955 // Is nil if the source module does not exist.
956 implLibraryModule *Library
Paul Duffin56d44902020-01-31 13:36:25 +0000957}
958
Paul Duffin71b33cc2021-06-23 11:39:47 +0100959func (c *commonToSdkLibraryAndImport) initCommon(module commonSdkLibraryAndImportModule) {
960 c.module = module
Paul Duffin1b1e8062020-05-08 13:44:43 +0100961
Paul Duffin71b33cc2021-06-23 11:39:47 +0100962 module.AddProperties(&c.commonSdkLibraryProperties)
Paul Duffin859fe962020-05-15 10:20:31 +0100963
964 // Initialize this as an sdk library component.
Paul Duffin71b33cc2021-06-23 11:39:47 +0100965 c.initSdkLibraryComponent(module)
Paul Duffin1b1e8062020-05-08 13:44:43 +0100966}
967
968func (c *commonToSdkLibraryAndImport) initCommonAfterDefaultsApplied(ctx android.DefaultableHookContext) bool {
Paul Duffindfa131e2020-05-15 20:37:11 +0100969 schemeProperty := proptools.StringDefault(c.commonSdkLibraryProperties.Naming_scheme, "default")
Paul Duffin1b1e8062020-05-08 13:44:43 +0100970 switch schemeProperty {
971 case "default":
972 c.namingScheme = &defaultNamingScheme{}
973 default:
974 ctx.PropertyErrorf("naming_scheme", "expected 'default' but was %q", schemeProperty)
975 return false
976 }
977
Spandan Das23956d12024-01-19 00:22:22 +0000978 namePtr := proptools.StringPtr(c.module.RootLibraryName())
Paul Duffin3f0290e2021-06-30 18:25:36 +0100979 c.sdkLibraryComponentProperties.SdkLibraryName = namePtr
980
Paul Duffindfa131e2020-05-15 20:37:11 +0100981 // Only track this sdk library if this can be used as a shared library.
982 if c.sharedLibrary() {
983 // Use the name specified in the module definition as the owner.
Paul Duffin3f0290e2021-06-30 18:25:36 +0100984 c.sdkLibraryComponentProperties.SdkLibraryToImplicitlyTrack = namePtr
Paul Duffindfa131e2020-05-15 20:37:11 +0100985 }
Paul Duffin859fe962020-05-15 10:20:31 +0100986
Paul Duffin1b1e8062020-05-08 13:44:43 +0100987 return true
Paul Duffinc3091c82020-05-08 14:16:20 +0100988}
989
Paul Duffinea8f8082021-06-24 13:25:57 +0100990// uniqueApexVariations provides common implementation of the ApexModule.UniqueApexVariations
991// method.
992func (c *commonToSdkLibraryAndImport) uniqueApexVariations() bool {
993 // A java_sdk_library that is a shared library produces an XML file that makes the shared library
994 // usable from an AndroidManifest.xml's <uses-library> entry. That XML file contains the name of
995 // the APEX and so it needs a unique variation per APEX.
996 return c.sharedLibrary()
997}
998
Paul Duffina2ae7e02020-09-11 11:55:00 +0100999func (c *commonToSdkLibraryAndImport) generateCommonBuildActions(ctx android.ModuleContext) {
1000 c.doctagPaths = android.PathsForModuleSrc(ctx, c.commonSdkLibraryProperties.Doctag_files)
1001}
1002
Jihoon Kanga3a05462024-04-05 00:36:44 +00001003func (c *commonToSdkLibraryAndImport) getImplLibraryModule() *Library {
1004 return c.implLibraryModule
1005}
1006
Paul Duffineedc5d52020-06-12 17:46:39 +01001007// Module name of the runtime implementation library
1008func (c *commonToSdkLibraryAndImport) implLibraryModuleName() string {
Spandan Das23956d12024-01-19 00:22:22 +00001009 return c.module.RootLibraryName() + ".impl"
Paul Duffineedc5d52020-06-12 17:46:39 +01001010}
1011
1012// Module name of the XML file for the lib
1013func (c *commonToSdkLibraryAndImport) xmlPermissionsModuleName() string {
Spandan Das23956d12024-01-19 00:22:22 +00001014 return c.module.RootLibraryName() + sdkXmlFileSuffix
Paul Duffineedc5d52020-06-12 17:46:39 +01001015}
1016
Paul Duffinc3091c82020-05-08 14:16:20 +01001017// Name of the java_library module that compiles the stubs source.
1018func (c *commonToSdkLibraryAndImport) stubsLibraryModuleName(apiScope *apiScope) string {
Spandan Das23956d12024-01-19 00:22:22 +00001019 baseName := c.module.RootLibraryName()
Paul Duffin21787622022-11-25 12:48:20 +00001020 return c.namingScheme.stubsLibraryModuleName(apiScope, baseName)
Paul Duffinc3091c82020-05-08 14:16:20 +01001021}
1022
Jihoon Kangfa4a90d2023-12-20 02:53:38 +00001023// Name of the java_library module that compiles the exportable stubs source.
1024func (c *commonToSdkLibraryAndImport) exportableStubsLibraryModuleName(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.exportableStubsLibraryModuleName(apiScope, baseName)
1027}
1028
Paul Duffinc3091c82020-05-08 14:16:20 +01001029// Name of the droidstubs module that generates the stubs source and may also
1030// generate/check the API.
1031func (c *commonToSdkLibraryAndImport) stubsSourceModuleName(apiScope *apiScope) string {
Spandan Das23956d12024-01-19 00:22:22 +00001032 baseName := c.module.RootLibraryName()
Paul Duffin21787622022-11-25 12:48:20 +00001033 return c.namingScheme.stubsSourceModuleName(apiScope, baseName)
Paul Duffinc3091c82020-05-08 14:16:20 +01001034}
1035
Jihoon Kang1c92c3e2023-03-23 17:44:51 +00001036// Name of the java_api_library module that generates the from-text stubs source
1037// and compiles to a jar file.
1038func (c *commonToSdkLibraryAndImport) apiLibraryModuleName(apiScope *apiScope) string {
Spandan Das23956d12024-01-19 00:22:22 +00001039 baseName := c.module.RootLibraryName()
Jihoon Kang1c92c3e2023-03-23 17:44:51 +00001040 return c.namingScheme.apiLibraryModuleName(apiScope, baseName)
1041}
1042
Jihoon Kang1147b312023-06-08 23:25:57 +00001043// Name of the java_library module that compiles the stubs
1044// generated from source Java files.
Jihoon Kangfa4a90d2023-12-20 02:53:38 +00001045func (c *commonToSdkLibraryAndImport) sourceStubsLibraryModuleName(apiScope *apiScope) string {
Spandan Das23956d12024-01-19 00:22:22 +00001046 baseName := c.module.RootLibraryName()
Jihoon Kangfa4a90d2023-12-20 02:53:38 +00001047 return c.namingScheme.sourceStubsLibraryModuleName(apiScope, baseName)
1048}
1049
1050// Name of the java_library module that compiles the exportable stubs
1051// generated from source Java files.
1052func (c *commonToSdkLibraryAndImport) exportableSourceStubsLibraryModuleName(apiScope *apiScope) string {
Spandan Das23956d12024-01-19 00:22:22 +00001053 baseName := c.module.RootLibraryName()
Jihoon Kangfa4a90d2023-12-20 02:53:38 +00001054 return c.namingScheme.exportableSourceStubsLibraryModuleName(apiScope, baseName)
Jihoon Kang1147b312023-06-08 23:25:57 +00001055}
1056
Paul Duffin46dc45a2020-05-14 15:39:10 +01001057// The component names for different outputs of the java_sdk_library.
1058//
1059// They are similar to the names used for the child modules it creates
1060const (
1061 stubsSourceComponentName = "stubs.source"
1062
1063 apiTxtComponentName = "api.txt"
1064
1065 removedApiTxtComponentName = "removed-api.txt"
Anton Hanssond78eb762021-09-21 15:25:12 +01001066
1067 annotationsComponentName = "annotations.zip"
Paul Duffin46dc45a2020-05-14 15:39:10 +01001068)
1069
1070// A regular expression to match tags that reference a specific stubs component.
1071//
1072// It will only match if given a valid scope and a valid component. It is verfy strict
1073// to ensure it does not accidentally match a similar looking tag that should be processed
1074// by the embedded Library.
1075var tagSplitter = func() *regexp.Regexp {
1076 // Given a list of literal string items returns a regular expression that will
1077 // match any one of the items.
1078 choice := func(items ...string) string {
1079 return `\Q` + strings.Join(items, `\E|\Q`) + `\E`
1080 }
1081
1082 // Regular expression to match one of the scopes.
1083 scopesRegexp := choice(allScopeNames...)
1084
1085 // Regular expression to match one of the components.
Anton Hanssond78eb762021-09-21 15:25:12 +01001086 componentsRegexp := choice(stubsSourceComponentName, apiTxtComponentName, removedApiTxtComponentName, annotationsComponentName)
Paul Duffin46dc45a2020-05-14 15:39:10 +01001087
1088 // Regular expression to match any combination of one scope and one component.
1089 return regexp.MustCompile(fmt.Sprintf(`^\.(%s)\.(%s)$`, scopesRegexp, componentsRegexp))
1090}()
1091
1092// For OutputFileProducer interface
1093//
Anton Hanssond78eb762021-09-21 15:25:12 +01001094// .<scope>.<component name>, for all ComponentNames (for example: .public.removed-api.txt)
Paul Duffin46dc45a2020-05-14 15:39:10 +01001095func (c *commonToSdkLibraryAndImport) commonOutputFiles(tag string) (android.Paths, error) {
1096 if groups := tagSplitter.FindStringSubmatch(tag); groups != nil {
1097 scopeName := groups[1]
1098 component := groups[2]
1099
1100 if scope, ok := scopeByName[scopeName]; ok {
1101 paths := c.findScopePaths(scope)
1102 if paths == nil {
Spandan Das23956d12024-01-19 00:22:22 +00001103 return nil, fmt.Errorf("%q does not provide api scope %s", c.module.RootLibraryName(), scopeName)
Paul Duffin46dc45a2020-05-14 15:39:10 +01001104 }
1105
1106 switch component {
1107 case stubsSourceComponentName:
1108 if paths.stubsSrcJar.Valid() {
1109 return android.Paths{paths.stubsSrcJar.Path()}, nil
1110 }
1111
1112 case apiTxtComponentName:
1113 if paths.currentApiFilePath.Valid() {
1114 return android.Paths{paths.currentApiFilePath.Path()}, nil
1115 }
1116
1117 case removedApiTxtComponentName:
1118 if paths.removedApiFilePath.Valid() {
1119 return android.Paths{paths.removedApiFilePath.Path()}, nil
1120 }
Anton Hanssond78eb762021-09-21 15:25:12 +01001121
1122 case annotationsComponentName:
1123 if paths.annotationsZip.Valid() {
1124 return android.Paths{paths.annotationsZip.Path()}, nil
1125 }
Paul Duffin46dc45a2020-05-14 15:39:10 +01001126 }
1127
1128 return nil, fmt.Errorf("%s not available for api scope %s", component, scopeName)
1129 } else {
1130 return nil, fmt.Errorf("unknown scope %s in %s", scope, tag)
1131 }
1132
1133 } else {
Paul Duffina2ae7e02020-09-11 11:55:00 +01001134 switch tag {
1135 case ".doctags":
1136 if c.doctagPaths != nil {
1137 return c.doctagPaths, nil
1138 } else {
Spandan Das23956d12024-01-19 00:22:22 +00001139 return nil, fmt.Errorf("no doctag_files specified on %s", c.module.RootLibraryName())
Paul Duffina2ae7e02020-09-11 11:55:00 +01001140 }
1141 }
Paul Duffin46dc45a2020-05-14 15:39:10 +01001142 return nil, nil
1143 }
1144}
1145
Paul Duffin803a9562020-05-20 11:52:25 +01001146func (c *commonToSdkLibraryAndImport) getScopePathsCreateIfNeeded(scope *apiScope) *scopePaths {
Paul Duffin56d44902020-01-31 13:36:25 +00001147 if c.scopePaths == nil {
1148 c.scopePaths = make(map[*apiScope]*scopePaths)
1149 }
1150 paths := c.scopePaths[scope]
1151 if paths == nil {
1152 paths = &scopePaths{}
1153 c.scopePaths[scope] = paths
1154 }
1155
1156 return paths
1157}
1158
Paul Duffin803a9562020-05-20 11:52:25 +01001159func (c *commonToSdkLibraryAndImport) findScopePaths(scope *apiScope) *scopePaths {
1160 if c.scopePaths == nil {
1161 return nil
1162 }
1163
1164 return c.scopePaths[scope]
1165}
1166
1167// If this does not support the requested api scope then find the closest available
1168// scope it does support. Returns nil if no such scope is available.
1169func (c *commonToSdkLibraryAndImport) findClosestScopePath(scope *apiScope) *scopePaths {
Paul Duffind0b9fca2022-09-30 18:11:41 +01001170 for s := scope; s != nil; s = s.canAccess {
Paul Duffin803a9562020-05-20 11:52:25 +01001171 if paths := c.findScopePaths(s); paths != nil {
1172 return paths
1173 }
1174 }
1175
1176 // This should never happen outside tests as public should be the base scope for every
1177 // scope and is enabled by default.
1178 return nil
1179}
1180
Jiyong Parkf1691d22021-03-29 20:11:58 +09001181func (c *commonToSdkLibraryAndImport) selectHeaderJarsForSdkVersion(ctx android.BaseModuleContext, sdkVersion android.SdkSpec) android.Paths {
Paul Duffinb05d4292020-05-20 12:19:10 +01001182
1183 // If a specific numeric version has been requested then use prebuilt versions of the sdk.
Jiyong Park54105c42021-03-31 18:17:53 +09001184 if !sdkVersion.ApiLevel.IsPreview() {
Spandan Das23956d12024-01-19 00:22:22 +00001185 return PrebuiltJars(ctx, c.module.RootLibraryName(), sdkVersion)
Paul Duffinb05d4292020-05-20 12:19:10 +01001186 }
1187
Paul Duffin1267d872021-04-16 17:21:36 +01001188 paths := c.selectScopePaths(ctx, sdkVersion.Kind)
1189 if paths == nil {
1190 return nil
1191 }
1192
1193 return paths.stubsHeaderPath
1194}
1195
1196// selectScopePaths returns the *scopePaths appropriate for the specific kind.
1197//
1198// If the module does not support the specific kind then it will return the *scopePaths for the
1199// closest kind which is a subset of the requested kind. e.g. if requesting android.SdkModule then
1200// it will return *scopePaths for android.SdkSystem if available or android.SdkPublic of not.
1201func (c *commonToSdkLibraryAndImport) selectScopePaths(ctx android.BaseModuleContext, kind android.SdkKind) *scopePaths {
Paul Duffin32cf58a2021-05-18 16:32:50 +01001202 apiScope := sdkKindToApiScope(kind)
Paul Duffinb05d4292020-05-20 12:19:10 +01001203
Paul Duffin803a9562020-05-20 11:52:25 +01001204 paths := c.findClosestScopePath(apiScope)
1205 if paths == nil {
1206 var scopes []string
1207 for _, s := range allApiScopes {
1208 if c.findScopePaths(s) != nil {
1209 scopes = append(scopes, s.name)
1210 }
1211 }
Spandan Das23956d12024-01-19 00:22:22 +00001212 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 +01001213 return nil
1214 }
1215
Paul Duffin1267d872021-04-16 17:21:36 +01001216 return paths
1217}
1218
Paul Duffin32cf58a2021-05-18 16:32:50 +01001219// sdkKindToApiScope maps from android.SdkKind to apiScope.
1220func sdkKindToApiScope(kind android.SdkKind) *apiScope {
1221 var apiScope *apiScope
1222 switch kind {
1223 case android.SdkSystem:
1224 apiScope = apiScopeSystem
1225 case android.SdkModule:
1226 apiScope = apiScopeModuleLib
1227 case android.SdkTest:
1228 apiScope = apiScopeTest
1229 case android.SdkSystemServer:
1230 apiScope = apiScopeSystemServer
1231 default:
1232 apiScope = apiScopePublic
1233 }
1234 return apiScope
1235}
1236
Paul Duffin1267d872021-04-16 17:21:36 +01001237// to satisfy SdkLibraryDependency interface
Martin Stjernholm8be1e6d2021-09-15 03:34:04 +01001238func (c *commonToSdkLibraryAndImport) SdkApiStubDexJar(ctx android.BaseModuleContext, kind android.SdkKind) OptionalDexJarPath {
Paul Duffin1267d872021-04-16 17:21:36 +01001239 paths := c.selectScopePaths(ctx, kind)
1240 if paths == nil {
Martin Stjernholm8be1e6d2021-09-15 03:34:04 +01001241 return makeUnsetDexJarPath()
Paul Duffin1267d872021-04-16 17:21:36 +01001242 }
1243
1244 return paths.stubsDexJarPath
Paul Duffinb05d4292020-05-20 12:19:10 +01001245}
1246
Paul Duffin32cf58a2021-05-18 16:32:50 +01001247// to satisfy SdkLibraryDependency interface
Jihoon Kangbd093452023-12-26 19:08:01 +00001248func (c *commonToSdkLibraryAndImport) SdkApiExportableStubDexJar(ctx android.BaseModuleContext, kind android.SdkKind) OptionalDexJarPath {
1249 paths := c.selectScopePaths(ctx, kind)
1250 if paths == nil {
1251 return makeUnsetDexJarPath()
1252 }
1253
1254 return paths.exportableStubsDexJarPath
1255}
1256
1257// to satisfy SdkLibraryDependency interface
Paul Duffin32cf58a2021-05-18 16:32:50 +01001258func (c *commonToSdkLibraryAndImport) SdkRemovedTxtFile(ctx android.BaseModuleContext, kind android.SdkKind) android.OptionalPath {
1259 apiScope := sdkKindToApiScope(kind)
1260 paths := c.findScopePaths(apiScope)
1261 if paths == nil {
1262 return android.OptionalPath{}
1263 }
1264
1265 return paths.removedApiFilePath
1266}
1267
Paul Duffin859fe962020-05-15 10:20:31 +01001268func (c *commonToSdkLibraryAndImport) sdkComponentPropertiesForChildLibrary() interface{} {
1269 componentProps := &struct {
Paul Duffin3f0290e2021-06-30 18:25:36 +01001270 SdkLibraryName *string
Paul Duffin859fe962020-05-15 10:20:31 +01001271 SdkLibraryToImplicitlyTrack *string
Paul Duffindfa131e2020-05-15 20:37:11 +01001272 }{}
1273
Spandan Das23956d12024-01-19 00:22:22 +00001274 namePtr := proptools.StringPtr(c.module.RootLibraryName())
Paul Duffin3f0290e2021-06-30 18:25:36 +01001275 componentProps.SdkLibraryName = namePtr
1276
Paul Duffindfa131e2020-05-15 20:37:11 +01001277 if c.sharedLibrary() {
Paul Duffin859fe962020-05-15 10:20:31 +01001278 // Mark the stubs library as being components of this java_sdk_library so that
1279 // any app that includes code which depends (directly or indirectly) on the stubs
1280 // library will have the appropriate <uses-library> invocation inserted into its
1281 // manifest if necessary.
Paul Duffin3f0290e2021-06-30 18:25:36 +01001282 componentProps.SdkLibraryToImplicitlyTrack = namePtr
Paul Duffin859fe962020-05-15 10:20:31 +01001283 }
1284
1285 return componentProps
1286}
1287
Paul Duffindfa131e2020-05-15 20:37:11 +01001288func (c *commonToSdkLibraryAndImport) sharedLibrary() bool {
1289 return proptools.BoolDefault(c.commonSdkLibraryProperties.Shared_library, true)
1290}
1291
Paul Duffinf4600f62021-05-13 22:34:45 +01001292// Check if the stub libraries should be compiled for dex
1293func (c *commonToSdkLibraryAndImport) stubLibrariesCompiledForDex() bool {
1294 // Always compile the dex file files for the stub libraries if they will be used on the
1295 // bootclasspath.
1296 return !c.sharedLibrary()
1297}
1298
Paul Duffin859fe962020-05-15 10:20:31 +01001299// Properties related to the use of a module as an component of a java_sdk_library.
1300type SdkLibraryComponentProperties struct {
Paul Duffin3f0290e2021-06-30 18:25:36 +01001301 // The name of the java_sdk_library/_import module.
1302 SdkLibraryName *string `blueprint:"mutated"`
Paul Duffin859fe962020-05-15 10:20:31 +01001303
1304 // The name of the java_sdk_library/_import to add to a <uses-library> entry
1305 // in the AndroidManifest.xml of any Android app that includes code that references
1306 // this module. If not set then no java_sdk_library/_import is tracked.
1307 SdkLibraryToImplicitlyTrack *string `blueprint:"mutated"`
1308}
1309
1310// Structure to be embedded in a module struct that needs to support the
1311// SdkLibraryComponentDependency interface.
1312type EmbeddableSdkLibraryComponent struct {
1313 sdkLibraryComponentProperties SdkLibraryComponentProperties
1314}
1315
Paul Duffin71b33cc2021-06-23 11:39:47 +01001316func (e *EmbeddableSdkLibraryComponent) initSdkLibraryComponent(module android.Module) {
1317 module.AddProperties(&e.sdkLibraryComponentProperties)
Paul Duffin859fe962020-05-15 10:20:31 +01001318}
1319
1320// to satisfy SdkLibraryComponentDependency
Paul Duffin3f0290e2021-06-30 18:25:36 +01001321func (e *EmbeddableSdkLibraryComponent) SdkLibraryName() *string {
1322 return e.sdkLibraryComponentProperties.SdkLibraryName
1323}
1324
1325// to satisfy SdkLibraryComponentDependency
Ulya Trafimovich39b437b2020-09-23 16:42:35 +01001326func (e *EmbeddableSdkLibraryComponent) OptionalSdkLibraryImplementation() *string {
Ulya Trafimovich78645fb2021-07-16 15:29:25 +01001327 // For shared libraries, this is the same as the SDK library name. If a Java library or app
1328 // depends on a component library (e.g. a stub library) it still needs to know the name of the
1329 // run-time library and the corresponding module that provides the implementation. This name is
1330 // passed to manifest_fixer (to be added to AndroidManifest.xml) and added to CLC (to be used
1331 // in dexpreopt).
1332 //
1333 // For non-shared SDK (component or not) libraries this returns `nil`, as they are not
1334 // <uses-library> and should not be added to the manifest or to CLC.
Ulya Trafimovich39b437b2020-09-23 16:42:35 +01001335 return e.sdkLibraryComponentProperties.SdkLibraryToImplicitlyTrack
1336}
1337
Paul Duffin859fe962020-05-15 10:20:31 +01001338// Implemented by modules that are (or possibly could be) a component of a java_sdk_library
1339// (including the java_sdk_library) itself.
1340type SdkLibraryComponentDependency interface {
Ulya Trafimovich31e444e2020-08-14 17:32:16 +01001341 UsesLibraryDependency
1342
Paul Duffin3f0290e2021-06-30 18:25:36 +01001343 // SdkLibraryName returns the name of the java_sdk_library/_import module.
1344 SdkLibraryName() *string
1345
Ulya Trafimovich39b437b2020-09-23 16:42:35 +01001346 // The name of the implementation library for the optional SDK library or nil, if there isn't one.
1347 OptionalSdkLibraryImplementation() *string
Paul Duffin859fe962020-05-15 10:20:31 +01001348}
1349
1350// Make sure that all the module types that are components of java_sdk_library/_import
1351// and which can be referenced (directly or indirectly) from an android app implement
1352// the SdkLibraryComponentDependency interface.
1353var _ SdkLibraryComponentDependency = (*Library)(nil)
1354var _ SdkLibraryComponentDependency = (*Import)(nil)
1355var _ SdkLibraryComponentDependency = (*SdkLibrary)(nil)
Paul Duffineedc5d52020-06-12 17:46:39 +01001356var _ SdkLibraryComponentDependency = (*SdkLibraryImport)(nil)
Paul Duffin859fe962020-05-15 10:20:31 +01001357
Paul Duffin32cf58a2021-05-18 16:32:50 +01001358// Provides access to sdk_version related files, e.g. header and implementation jars.
Paul Duffin859fe962020-05-15 10:20:31 +01001359type SdkLibraryDependency interface {
1360 SdkLibraryComponentDependency
1361
1362 // Get the header jars appropriate for the supplied sdk_version.
1363 //
1364 // These are turbine generated jars so they only change if the externals of the
1365 // class changes but it does not contain and implementation or JavaDoc.
Jiyong Parkf1691d22021-03-29 20:11:58 +09001366 SdkHeaderJars(ctx android.BaseModuleContext, sdkVersion android.SdkSpec) android.Paths
Paul Duffin859fe962020-05-15 10:20:31 +01001367
Jihoon Kangbd093452023-12-26 19:08:01 +00001368 // SdkApiStubDexJar returns the dex jar for the stubs for the prebuilt
1369 // java_sdk_library_import module. It is needed by the hiddenapi processing tool which
1370 // processes dex files.
Martin Stjernholm8be1e6d2021-09-15 03:34:04 +01001371 SdkApiStubDexJar(ctx android.BaseModuleContext, kind android.SdkKind) OptionalDexJarPath
Paul Duffinf4600f62021-05-13 22:34:45 +01001372
Jihoon Kangbd093452023-12-26 19:08:01 +00001373 // SdkApiExportableStubDexJar returns the exportable dex jar for the stubs for
1374 // java_sdk_library module. It is needed by the hiddenapi processing tool which processes
1375 // dex files.
1376 SdkApiExportableStubDexJar(ctx android.BaseModuleContext, kind android.SdkKind) OptionalDexJarPath
1377
Paul Duffin32cf58a2021-05-18 16:32:50 +01001378 // SdkRemovedTxtFile returns the optional path to the removed.txt file for the specified sdk kind.
1379 SdkRemovedTxtFile(ctx android.BaseModuleContext, kind android.SdkKind) android.OptionalPath
1380
Paul Duffinf4600f62021-05-13 22:34:45 +01001381 // sharedLibrary returns true if this can be used as a shared library.
1382 sharedLibrary() bool
Jihoon Kanga3a05462024-04-05 00:36:44 +00001383
1384 getImplLibraryModule() *Library
Paul Duffin859fe962020-05-15 10:20:31 +01001385}
1386
Inseob Kimc0907f12019-02-08 21:00:45 +09001387type SdkLibrary struct {
Sundong Ahn054b19a2018-10-19 13:46:09 +09001388 Library
Jiyong Parkc678ad32018-04-10 13:07:10 +09001389
Sundong Ahn054b19a2018-10-19 13:46:09 +09001390 sdkLibraryProperties sdkLibraryProperties
Jiyong Parkc678ad32018-04-10 13:07:10 +09001391
Paul Duffin3375e352020-04-28 10:44:03 +01001392 // Map from api scope to the scope specific property structure.
1393 scopeToProperties map[*apiScope]*ApiScopeProperties
1394
Paul Duffin56d44902020-01-31 13:36:25 +00001395 commonToSdkLibraryAndImport
Jihoon Kanga3a05462024-04-05 00:36:44 +00001396
1397 builtInstalledForApex []dexpreopterInstall
Jiyong Parkc678ad32018-04-10 13:07:10 +09001398}
1399
Inseob Kimc0907f12019-02-08 21:00:45 +09001400var _ SdkLibraryDependency = (*SdkLibrary)(nil)
Colin Cross897d2ed2019-02-11 14:03:51 -08001401
Paul Duffin3375e352020-04-28 10:44:03 +01001402func (module *SdkLibrary) generateTestAndSystemScopesByDefault() bool {
1403 return module.sdkLibraryProperties.Generate_system_and_test_apis
1404}
1405
Jihoon Kanga3a05462024-04-05 00:36:44 +00001406func (module *SdkLibrary) DexJarBuildPath(ctx android.ModuleErrorfContext) OptionalDexJarPath {
1407 if module.implLibraryModule != nil {
1408 return module.implLibraryModule.DexJarBuildPath(ctx)
1409 }
1410 return makeUnsetDexJarPath()
1411}
1412
1413func (module *SdkLibrary) DexJarInstallPath() android.Path {
1414 if module.implLibraryModule != nil {
1415 return module.implLibraryModule.DexJarInstallPath()
1416 }
1417 return nil
1418}
1419
Paul Duffin3375e352020-04-28 10:44:03 +01001420func (module *SdkLibrary) getGeneratedApiScopes(ctx android.EarlyModuleContext) apiScopes {
1421 // Check to see if any scopes have been explicitly enabled. If any have then all
1422 // must be.
1423 anyScopesExplicitlyEnabled := false
1424 for _, scope := range allApiScopes {
1425 scopeProperties := module.scopeToProperties[scope]
1426 if scopeProperties.Enabled != nil {
1427 anyScopesExplicitlyEnabled = true
1428 break
1429 }
Paul Duffind1b3a922020-01-22 11:57:20 +00001430 }
Paul Duffin3375e352020-04-28 10:44:03 +01001431
1432 var generatedScopes apiScopes
1433 enabledScopes := make(map[*apiScope]struct{})
1434 for _, scope := range allApiScopes {
1435 scopeProperties := module.scopeToProperties[scope]
1436 // If any scopes are explicitly enabled then ignore the legacy enabled status.
1437 // This is to ensure that any new usages of this module type do not rely on legacy
1438 // behaviour.
1439 defaultEnabledStatus := false
1440 if anyScopesExplicitlyEnabled {
1441 defaultEnabledStatus = scope.defaultEnabledStatus
1442 } else {
1443 defaultEnabledStatus = scope.legacyEnabledStatus(module)
1444 }
1445 enabled := proptools.BoolDefault(scopeProperties.Enabled, defaultEnabledStatus)
1446 if enabled {
1447 enabledScopes[scope] = struct{}{}
1448 generatedScopes = append(generatedScopes, scope)
1449 }
1450 }
1451
1452 // Now check to make sure that any scope that is extended by an enabled scope is also
1453 // enabled.
1454 for _, scope := range allApiScopes {
1455 if _, ok := enabledScopes[scope]; ok {
1456 extends := scope.extends
1457 if extends != nil {
1458 if _, ok := enabledScopes[extends]; !ok {
1459 ctx.ModuleErrorf("enabled api scope %q depends on disabled scope %q", scope, extends)
1460 }
1461 }
1462 }
1463 }
1464
1465 return generatedScopes
Paul Duffind1b3a922020-01-22 11:57:20 +00001466}
1467
satayev758968a2021-12-06 11:42:40 +00001468var _ android.ModuleWithMinSdkVersionCheck = (*SdkLibrary)(nil)
1469
satayev8f088b02021-12-06 11:40:46 +00001470func (module *SdkLibrary) CheckMinSdkVersion(ctx android.ModuleContext) {
Jihoon Kanga3a05462024-04-05 00:36:44 +00001471 CheckMinSdkVersion(ctx, &module.Library)
1472}
1473
1474func CheckMinSdkVersion(ctx android.ModuleContext, module *Library) {
Spandan Das8c9ae7e2023-03-03 21:20:36 +00001475 android.CheckMinSdkVersion(ctx, module.MinSdkVersion(ctx), func(c android.ModuleContext, do android.PayloadDepsCallback) {
satayev8f088b02021-12-06 11:40:46 +00001476 ctx.WalkDeps(func(child android.Module, parent android.Module) bool {
1477 isExternal := !module.depIsInSameApex(ctx, child)
1478 if am, ok := child.(android.ApexModule); ok {
1479 if !do(ctx, parent, am, isExternal) {
1480 return false
1481 }
1482 }
1483 return !isExternal
1484 })
1485 })
1486}
1487
Paul Duffineedc5d52020-06-12 17:46:39 +01001488type sdkLibraryComponentTag struct {
1489 blueprint.BaseDependencyTag
1490 name string
1491}
1492
1493// Mark this tag so dependencies that use it are excluded from visibility enforcement.
1494func (t sdkLibraryComponentTag) ExcludeFromVisibilityEnforcement() {}
1495
1496var xmlPermissionsFileTag = sdkLibraryComponentTag{name: "xml-permissions-file"}
Paul Duffine74ac732020-02-06 13:51:46 +00001497
Jiyong Parke3833882020-02-17 17:28:10 +09001498func IsXmlPermissionsFileDepTag(depTag blueprint.DependencyTag) bool {
Paul Duffineedc5d52020-06-12 17:46:39 +01001499 if dt, ok := depTag.(sdkLibraryComponentTag); ok {
Jiyong Parke3833882020-02-17 17:28:10 +09001500 return dt == xmlPermissionsFileTag
1501 }
1502 return false
1503}
1504
Paul Duffineedc5d52020-06-12 17:46:39 +01001505var implLibraryTag = sdkLibraryComponentTag{name: "impl-library"}
Paul Duffin5df79302020-05-16 15:52:12 +01001506
Jeongik Chaaaa6dcd2024-05-22 00:41:28 +09001507var _ android.InstallNeededDependencyTag = sdkLibraryComponentTag{}
1508
1509func (t sdkLibraryComponentTag) InstallDepNeeded() bool {
1510 return t.name == "xml-permissions-file" || t.name == "impl-library"
1511}
1512
Paul Duffin44f1d842020-06-26 20:17:02 +01001513// Add the dependencies on the child modules in the component deps mutator.
1514func (module *SdkLibrary) ComponentDepsMutator(ctx android.BottomUpMutatorContext) {
Paul Duffin3375e352020-04-28 10:44:03 +01001515 for _, apiScope := range module.getGeneratedApiScopes(ctx) {
Paul Duffind1b3a922020-01-22 11:57:20 +00001516 // Add dependencies to the stubs library
Spandan Das877f39d2023-03-29 16:19:51 +00001517 stubModuleName := module.stubsLibraryModuleName(apiScope)
Jihoon Kangbd093452023-12-26 19:08:01 +00001518 ctx.AddVariationDependencies(nil, apiScope.everythingStubsTag, stubModuleName)
Jihoon Kang1147b312023-06-08 23:25:57 +00001519
Jihoon Kangbd093452023-12-26 19:08:01 +00001520 exportableStubModuleName := module.exportableStubsLibraryModuleName(apiScope)
1521 ctx.AddVariationDependencies(nil, apiScope.exportableStubsTag, exportableStubModuleName)
Paul Duffind1b3a922020-01-22 11:57:20 +00001522
Paul Duffin15f34ef2020-07-20 18:04:44 +01001523 // Add a dependency on the stubs source in order to access both stubs source and api information.
1524 ctx.AddVariationDependencies(nil, apiScope.stubsSourceAndApiTag, module.stubsSourceModuleName(apiScope))
Paul Duffin958806b2022-05-16 13:10:47 +00001525
1526 if module.compareAgainstLatestApi(apiScope) {
1527 // Add dependencies on the latest finalized version of the API .txt file.
1528 latestApiModuleName := module.latestApiModuleName(apiScope)
1529 ctx.AddDependency(module, apiScope.latestApiModuleTag, latestApiModuleName)
1530
1531 // Add dependencies on the latest finalized version of the remove API .txt file.
1532 latestRemovedApiModuleName := module.latestRemovedApiModuleName(apiScope)
1533 ctx.AddDependency(module, apiScope.latestRemovedApiModuleTag, latestRemovedApiModuleName)
1534 }
Sundong Ahn054b19a2018-10-19 13:46:09 +09001535 }
1536
Paul Duffindfa131e2020-05-15 20:37:11 +01001537 if module.requiresRuntimeImplementationLibrary() {
Paul Duffin5df79302020-05-16 15:52:12 +01001538 // Add dependency to the rule for generating the implementation library.
1539 ctx.AddDependency(module, implLibraryTag, module.implLibraryModuleName())
1540
Paul Duffindfa131e2020-05-15 20:37:11 +01001541 if module.sharedLibrary() {
1542 // Add dependency to the rule for generating the xml permissions file
Paul Duffineedc5d52020-06-12 17:46:39 +01001543 ctx.AddDependency(module, xmlPermissionsFileTag, module.xmlPermissionsModuleName())
Paul Duffindfa131e2020-05-15 20:37:11 +01001544 }
Paul Duffin44f1d842020-06-26 20:17:02 +01001545 }
1546}
Paul Duffine74ac732020-02-06 13:51:46 +00001547
Paul Duffin44f1d842020-06-26 20:17:02 +01001548// Add other dependencies as normal.
1549func (module *SdkLibrary) DepsMutator(ctx android.BottomUpMutatorContext) {
Anton Hanssone77fccc2021-01-20 16:52:41 +00001550 var missingApiModules []string
1551 for _, apiScope := range module.getGeneratedApiScopes(ctx) {
1552 if apiScope.unstable {
1553 continue
1554 }
Paul Duffin958806b2022-05-16 13:10:47 +00001555 if m := module.latestApiModuleName(apiScope); !ctx.OtherModuleExists(m) {
Anton Hanssone77fccc2021-01-20 16:52:41 +00001556 missingApiModules = append(missingApiModules, m)
1557 }
Paul Duffin958806b2022-05-16 13:10:47 +00001558 if m := module.latestRemovedApiModuleName(apiScope); !ctx.OtherModuleExists(m) {
Anton Hanssone77fccc2021-01-20 16:52:41 +00001559 missingApiModules = append(missingApiModules, m)
1560 }
Paul Duffin958806b2022-05-16 13:10:47 +00001561 if m := module.latestIncompatibilitiesModuleName(apiScope); !ctx.OtherModuleExists(m) {
Jaewoong Jung1a97ee02021-03-09 13:25:02 -08001562 missingApiModules = append(missingApiModules, m)
1563 }
Anton Hanssone77fccc2021-01-20 16:52:41 +00001564 }
1565 if len(missingApiModules) != 0 && !module.sdkLibraryProperties.Unsafe_ignore_missing_latest_api {
1566 m := module.Name() + " is missing tracking files for previously released library versions.\n"
1567 m += "You need to do one of the following:\n"
1568 m += "- Add `unsafe_ignore_missing_latest_api: true` to your blueprint (to disable compat tracking)\n"
1569 m += "- Add a set of prebuilt txt files representing the last released version of this library for compat checking.\n"
1570 m += " (the current set of API files can be used as a seed for this compatibility tracking\n"
1571 m += "\n"
1572 m += "The following filegroup modules are missing:\n "
1573 m += strings.Join(missingApiModules, "\n ") + "\n"
1574 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."
1575 ctx.ModuleErrorf(m)
1576 }
Jiyong Parkc678ad32018-04-10 13:07:10 +09001577}
1578
Paul Duffin46dc45a2020-05-14 15:39:10 +01001579func (module *SdkLibrary) OutputFiles(tag string) (android.Paths, error) {
1580 paths, err := module.commonOutputFiles(tag)
Colin Cross4acaea92021-12-10 23:05:02 +00001581 if paths != nil || err != nil {
Paul Duffin46dc45a2020-05-14 15:39:10 +01001582 return paths, err
1583 }
Colin Cross4acaea92021-12-10 23:05:02 +00001584 if module.requiresRuntimeImplementationLibrary() {
Jihoon Kanga3a05462024-04-05 00:36:44 +00001585 return module.implLibraryModule.OutputFiles(tag)
Colin Cross4acaea92021-12-10 23:05:02 +00001586 }
1587 if tag == "" {
1588 return nil, nil
1589 }
1590 return nil, fmt.Errorf("unsupported module reference tag %q", tag)
Paul Duffin46dc45a2020-05-14 15:39:10 +01001591}
1592
Inseob Kimc0907f12019-02-08 21:00:45 +09001593func (module *SdkLibrary) GenerateAndroidBuildActions(ctx android.ModuleContext) {
Spandan Das5ae65ee2024-04-16 22:03:26 +00001594 if disableSourceApexVariant(ctx) {
1595 // Prebuilts are active, do not create the installation rules for the source javalib.
1596 // Even though the source javalib is not used, we need to hide it to prevent duplicate installation rules.
1597 // TODO (b/331665856): Implement a principled solution for this.
1598 module.HideFromMake()
1599 }
satayev8f088b02021-12-06 11:40:46 +00001600
Paul Duffina2ae7e02020-09-11 11:55:00 +01001601 module.generateCommonBuildActions(ctx)
1602
Jihoon Kanga3a05462024-04-05 00:36:44 +00001603 module.stem = proptools.StringDefault(module.overridableProperties.Stem, ctx.ModuleName())
1604
1605 module.provideHiddenAPIPropertyInfo(ctx)
Sundong Ahn054b19a2018-10-19 13:46:09 +09001606
Paul Duffinb97b1572021-04-29 21:50:40 +01001607 // Collate the components exported by this module. All scope specific modules are exported but
1608 // the impl and xml component modules are not.
1609 exportedComponents := map[string]struct{}{}
1610
Sundong Ahn57368eb2018-07-06 11:20:23 +09001611 // Record the paths to the header jars of the library (stubs and impl).
Paul Duffind1b3a922020-01-22 11:57:20 +00001612 // When this java_sdk_library is depended upon from others via "libs" property,
Jiyong Parkc678ad32018-04-10 13:07:10 +09001613 // the recorded paths will be returned depending on the link type of the caller.
1614 ctx.VisitDirectDeps(func(to android.Module) {
Jiyong Parkc678ad32018-04-10 13:07:10 +09001615 tag := ctx.OtherModuleDependencyTag(to)
1616
Paul Duffinc8782502020-04-29 20:45:27 +01001617 // Extract information from any of the scope specific dependencies.
1618 if scopeTag, ok := tag.(scopeDependencyTag); ok {
1619 apiScope := scopeTag.apiScope
Paul Duffin803a9562020-05-20 11:52:25 +01001620 scopePaths := module.getScopePathsCreateIfNeeded(apiScope)
Paul Duffinc8782502020-04-29 20:45:27 +01001621
1622 // Extract information from the dependency. The exact information extracted
1623 // is determined by the nature of the dependency which is determined by the tag.
1624 scopeTag.extractDepInfo(ctx, to, scopePaths)
Paul Duffinb97b1572021-04-29 21:50:40 +01001625
1626 exportedComponents[ctx.OtherModuleName(to)] = struct{}{}
Sundong Ahn20e998b2018-07-24 11:19:26 +09001627 }
Jihoon Kang8479dea2024-04-04 01:19:05 +00001628
1629 if tag == implLibraryTag {
1630 if dep, ok := android.OtherModuleProvider(ctx, to, JavaInfoProvider); ok {
1631 module.implLibraryHeaderJars = append(module.implLibraryHeaderJars, dep.HeaderJars...)
Jihoon Kanga3a05462024-04-05 00:36:44 +00001632 module.implLibraryModule = to.(*Library)
1633 android.SetProvider(ctx, JavaInfoProvider, dep)
Jihoon Kang8479dea2024-04-04 01:19:05 +00001634 }
1635 }
Jiyong Parkc678ad32018-04-10 13:07:10 +09001636 })
Paul Duffinb97b1572021-04-29 21:50:40 +01001637
Jihoon Kanga3a05462024-04-05 00:36:44 +00001638 apexInfo, _ := android.ModuleProvider(ctx, android.ApexInfoProvider)
1639 if !apexInfo.IsForPlatform() {
1640 module.hideApexVariantFromMake = true
1641 }
1642
1643 if module.implLibraryModule != nil {
1644 if ctx.Device() {
1645 module.classesJarPaths = android.Paths{module.implLibraryModule.implementationJarFile}
1646 module.bootDexJarPath = module.implLibraryModule.bootDexJarPath
1647 module.uncompressDexState = module.implLibraryModule.uncompressDexState
1648 module.active = module.implLibraryModule.active
1649 }
1650
1651 module.outputFile = module.implLibraryModule.outputFile
1652 module.dexJarFile = makeDexJarPathFromPath(module.implLibraryModule.dexJarFile.Path())
1653 module.headerJarFile = module.implLibraryModule.headerJarFile
1654 module.implementationAndResourcesJar = module.implLibraryModule.implementationAndResourcesJar
1655 module.builtInstalledForApex = module.implLibraryModule.builtInstalledForApex
1656 module.dexpreopter.configPath = module.implLibraryModule.dexpreopter.configPath
1657 module.dexpreopter.outputProfilePathOnHost = module.implLibraryModule.dexpreopter.outputProfilePathOnHost
1658
1659 if !module.Host() {
1660 module.hostdexInstallFile = module.implLibraryModule.hostdexInstallFile
1661 }
1662
1663 android.SetProvider(ctx, blueprint.SrcsFileProviderKey, blueprint.SrcsFileProviderData{SrcPaths: module.implLibraryModule.uniqueSrcFiles.Strings()})
1664 }
1665
Paul Duffinb97b1572021-04-29 21:50:40 +01001666 // Make the set of components exported by this module available for use elsewhere.
Cole Faust18994c72023-02-28 16:02:16 -08001667 exportedComponentInfo := android.ExportedComponentsInfo{Components: android.SortedKeys(exportedComponents)}
Colin Cross40213022023-12-13 15:19:49 -08001668 android.SetProvider(ctx, android.ExportedComponentsInfoProvider, exportedComponentInfo)
Paul Duffin958806b2022-05-16 13:10:47 +00001669
1670 // Provide additional information for inclusion in an sdk's generated .info file.
1671 additionalSdkInfo := map[string]interface{}{}
1672 additionalSdkInfo["dist_stem"] = module.distStem()
Paul Duffine8409952022-09-22 16:24:46 +01001673 baseModuleName := module.distStem()
Paul Duffin958806b2022-05-16 13:10:47 +00001674 scopes := map[string]interface{}{}
1675 additionalSdkInfo["scopes"] = scopes
1676 for scope, scopePaths := range module.scopePaths {
1677 scopeInfo := map[string]interface{}{}
1678 scopes[scope.name] = scopeInfo
1679 scopeInfo["current_api"] = scope.snapshotRelativeCurrentApiTxtPath(baseModuleName)
1680 scopeInfo["removed_api"] = scope.snapshotRelativeRemovedApiTxtPath(baseModuleName)
Jihoon Kang5623e542024-01-31 23:27:26 +00001681 if p := scopePaths.latestApiPaths; len(p) > 0 {
1682 // The last path in the list is the one that applies to this scope, the
1683 // preceding ones, if any, are for the scope(s) that it extends.
1684 scopeInfo["latest_api"] = p[len(p)-1].String()
Paul Duffin958806b2022-05-16 13:10:47 +00001685 }
Jihoon Kang5623e542024-01-31 23:27:26 +00001686 if p := scopePaths.latestRemovedApiPaths; len(p) > 0 {
1687 // The last path in the list is the one that applies to this scope, the
1688 // preceding ones, if any, are for the scope(s) that it extends.
1689 scopeInfo["latest_removed_api"] = p[len(p)-1].String()
Paul Duffin958806b2022-05-16 13:10:47 +00001690 }
1691 }
Colin Cross40213022023-12-13 15:19:49 -08001692 android.SetProvider(ctx, android.AdditionalSdkInfoProvider, android.AdditionalSdkInfo{additionalSdkInfo})
Jiyong Parkc678ad32018-04-10 13:07:10 +09001693}
1694
Jihoon Kanga3a05462024-04-05 00:36:44 +00001695func (module *SdkLibrary) BuiltInstalledForApex() []dexpreopterInstall {
1696 return module.builtInstalledForApex
1697}
1698
Jiyong Park0b0e1b92019-12-03 13:24:29 +09001699func (module *SdkLibrary) AndroidMkEntries() []android.AndroidMkEntries {
Paul Duffindfa131e2020-05-15 20:37:11 +01001700 if !module.requiresRuntimeImplementationLibrary() {
Paul Duffin43db9be2019-12-30 17:35:49 +00001701 return nil
1702 }
Jiyong Park0b0e1b92019-12-03 13:24:29 +09001703 entriesList := module.Library.AndroidMkEntries()
Jihoon Kanga3a05462024-04-05 00:36:44 +00001704 entries := &entriesList[0]
1705 entries.Required = append(entries.Required, module.implLibraryModuleName())
Yo Chiang07d75072020-06-05 17:43:19 +08001706 if module.sharedLibrary() {
Yo Chiang07d75072020-06-05 17:43:19 +08001707 entries.Required = append(entries.Required, module.xmlPermissionsModuleName())
1708 }
Jiyong Park0b0e1b92019-12-03 13:24:29 +09001709 return entriesList
Jiyong Park82484c02018-04-23 21:41:26 +09001710}
1711
Anton Hansson5fd5d242020-03-27 19:43:19 +00001712// The dist path of the stub artifacts
1713func (module *SdkLibrary) apiDistPath(apiScope *apiScope) string {
Colin Crossf0eace92021-06-02 13:02:23 -07001714 return path.Join("apistubs", module.distGroup(), apiScope.name)
Anton Hansson5fd5d242020-03-27 19:43:19 +00001715}
1716
Paul Duffin12ceb462019-12-24 20:31:31 +00001717// Get the sdk version for use when compiling the stubs library.
Paul Duffin780c5f42020-05-12 15:52:55 +01001718func (module *SdkLibrary) sdkVersionForStubsLibrary(mctx android.EarlyModuleContext, apiScope *apiScope) string {
Paul Duffin87a05a32020-05-12 11:50:28 +01001719 scopeProperties := module.scopeToProperties[apiScope]
1720 if scopeProperties.Sdk_version != nil {
1721 return proptools.String(scopeProperties.Sdk_version)
1722 }
1723
Jiyong Parkf1691d22021-03-29 20:11:58 +09001724 sdkDep := decodeSdkDep(mctx, android.SdkContext(&module.Library))
Paul Duffin12ceb462019-12-24 20:31:31 +00001725 if sdkDep.hasStandardLibs() {
1726 // If building against a standard sdk then use the sdk version appropriate for the scope.
Paul Duffind1b3a922020-01-22 11:57:20 +00001727 return apiScope.sdkVersion
Paul Duffin12ceb462019-12-24 20:31:31 +00001728 } else {
1729 // Otherwise, use no system module.
1730 return "none"
1731 }
1732}
1733
Paul Duffin31310252020-11-20 21:26:20 +00001734func (module *SdkLibrary) distStem() string {
1735 return proptools.StringDefault(module.sdkLibraryProperties.Dist_stem, module.BaseModuleName())
1736}
1737
Colin Cross986b69a2021-06-01 13:13:40 -07001738// distGroup returns the subdirectory of the dist path of the stub artifacts.
1739func (module *SdkLibrary) distGroup() string {
Colin Cross59b92bf2021-06-01 14:07:56 -07001740 return proptools.StringDefault(module.sdkLibraryProperties.Dist_group, "unknown")
Colin Cross986b69a2021-06-01 13:13:40 -07001741}
1742
Paul Duffin958806b2022-05-16 13:10:47 +00001743func latestPrebuiltApiModuleName(name string, apiScope *apiScope) string {
1744 return PrebuiltApiModuleName(name, apiScope.name, "latest")
1745}
1746
Jihoon Kang748a24d2024-03-20 21:29:39 +00001747func latestPrebuiltApiCombinedModuleName(name string, apiScope *apiScope) string {
1748 return PrebuiltApiCombinedModuleName(name, apiScope.name, "latest")
1749}
1750
Paul Duffind1b3a922020-01-22 11:57:20 +00001751func (module *SdkLibrary) latestApiFilegroupName(apiScope *apiScope) string {
Paul Duffin958806b2022-05-16 13:10:47 +00001752 return ":" + module.latestApiModuleName(apiScope)
1753}
1754
1755func (module *SdkLibrary) latestApiModuleName(apiScope *apiScope) string {
Jihoon Kang748a24d2024-03-20 21:29:39 +00001756 return latestPrebuiltApiCombinedModuleName(module.distStem(), apiScope)
Jiyong Park58c518b2018-05-12 22:29:12 +09001757}
Jiyong Parkc678ad32018-04-10 13:07:10 +09001758
Paul Duffind1b3a922020-01-22 11:57:20 +00001759func (module *SdkLibrary) latestRemovedApiFilegroupName(apiScope *apiScope) string {
Paul Duffin958806b2022-05-16 13:10:47 +00001760 return ":" + module.latestRemovedApiModuleName(apiScope)
1761}
1762
1763func (module *SdkLibrary) latestRemovedApiModuleName(apiScope *apiScope) string {
Jihoon Kang748a24d2024-03-20 21:29:39 +00001764 return latestPrebuiltApiCombinedModuleName(module.distStem()+"-removed", apiScope)
Jiyong Parkc678ad32018-04-10 13:07:10 +09001765}
1766
Jaewoong Jung1a97ee02021-03-09 13:25:02 -08001767func (module *SdkLibrary) latestIncompatibilitiesFilegroupName(apiScope *apiScope) string {
Paul Duffin958806b2022-05-16 13:10:47 +00001768 return ":" + module.latestIncompatibilitiesModuleName(apiScope)
1769}
1770
1771func (module *SdkLibrary) latestIncompatibilitiesModuleName(apiScope *apiScope) string {
1772 return latestPrebuiltApiModuleName(module.distStem()+"-incompatibilities", apiScope)
Jaewoong Jung1a97ee02021-03-09 13:25:02 -08001773}
1774
Jihoon Kang1c92c3e2023-03-23 17:44:51 +00001775func (module *SdkLibrary) contributesToApiSurface(c android.Config) bool {
1776 _, exists := c.GetApiLibraries()[module.Name()]
1777 return exists
1778}
1779
Jihoon Kang0c705a42023-08-02 06:44:57 +00001780// The listed modules are the special java_sdk_libraries where apiScope.kind do not match the
1781// api surface that the module contribute to. For example, the public droidstubs and java_library
1782// do not contribute to the public api surface, but contributes to the core platform api surface.
1783// This method returns the full api surface stub lib that
1784// the generated java_api_library should depend on.
1785func (module *SdkLibrary) alternativeFullApiSurfaceStubLib() string {
1786 if val, ok := apiLibraryAdditionalProperties[module.Name()]; ok {
1787 return val.FullApiSurfaceStubLib
1788 }
1789 return ""
1790}
1791
1792// The listed modules' stubs contents do not match the corresponding txt files,
1793// but require additional api contributions to generate the full stubs.
1794// This method returns the name of the additional api contribution module
1795// for corresponding sdk_library modules.
1796func (module *SdkLibrary) apiLibraryAdditionalApiContribution() string {
1797 if val, ok := apiLibraryAdditionalProperties[module.Name()]; ok {
1798 return val.AdditionalApiContribution
1799 }
1800 return ""
1801}
1802
Anton Hansson944e77d2020-08-19 11:40:22 +01001803func childModuleVisibility(childVisibility []string) []string {
1804 if childVisibility == nil {
1805 // No child visibility set. The child will use the visibility of the sdk_library.
1806 return nil
1807 }
1808
1809 // Prepend an override to ignore the sdk_library's visibility, and rely on the child visibility.
1810 var visibility []string
1811 visibility = append(visibility, "//visibility:override")
1812 visibility = append(visibility, childVisibility...)
1813 return visibility
1814}
1815
Paul Duffin5df79302020-05-16 15:52:12 +01001816// Creates the implementation java library
1817func (module *SdkLibrary) createImplLibrary(mctx android.DefaultableHookContext) {
Anton Hansson944e77d2020-08-19 11:40:22 +01001818 visibility := childModuleVisibility(module.sdkLibraryProperties.Impl_library_visibility)
1819
Paul Duffin5df79302020-05-16 15:52:12 +01001820 props := struct {
Paul Duffin77590a82022-04-28 14:13:30 +00001821 Name *string
1822 Visibility []string
1823 Instrument bool
1824 Libs []string
1825 Static_libs []string
1826 Apex_available []string
Jihoon Kanga3a05462024-04-05 00:36:44 +00001827 Stem *string
Paul Duffin5df79302020-05-16 15:52:12 +01001828 }{
1829 Name: proptools.StringPtr(module.implLibraryModuleName()),
Anton Hansson944e77d2020-08-19 11:40:22 +01001830 Visibility: visibility,
Paul Duffin296cf332020-06-18 21:09:55 +01001831 // Set the instrument property to ensure it is instrumented when instrumentation is required.
1832 Instrument: true,
Jihoon Kanga3a05462024-04-05 00:36:44 +00001833
1834 Libs: append(module.properties.Libs, module.sdkLibraryProperties.Impl_only_libs...),
1835
1836 Static_libs: append(module.properties.Static_libs, module.sdkLibraryProperties.Impl_only_static_libs...),
Paul Duffin77590a82022-04-28 14:13:30 +00001837 // Pass the apex_available settings down so that the impl library can be statically
1838 // embedded within a library that is added to an APEX. Needed for updatable-media.
1839 Apex_available: module.ApexAvailable(),
Jihoon Kanga3a05462024-04-05 00:36:44 +00001840
1841 Stem: proptools.StringPtr(module.Name()),
Paul Duffin5df79302020-05-16 15:52:12 +01001842 }
1843
1844 properties := []interface{}{
1845 &module.properties,
1846 &module.protoProperties,
1847 &module.deviceProperties,
Liz Kammera7a64f32020-07-09 15:16:41 -07001848 &module.dexProperties,
Paul Duffin5df79302020-05-16 15:52:12 +01001849 &module.dexpreoptProperties,
Colin Cross014489c2020-06-02 20:09:13 -07001850 &module.linter.properties,
Paul Duffin5df79302020-05-16 15:52:12 +01001851 &props,
1852 module.sdkComponentPropertiesForChildLibrary(),
1853 }
1854 mctx.CreateModule(LibraryFactory, properties...)
1855}
1856
Jihoon Kangfa4a90d2023-12-20 02:53:38 +00001857type libraryProperties struct {
1858 Name *string
1859 Visibility []string
1860 Srcs []string
1861 Installable *bool
1862 Sdk_version *string
1863 System_modules *string
1864 Patch_module *string
1865 Libs []string
1866 Static_libs []string
1867 Compile_dex *bool
1868 Java_version *string
1869 Openjdk9 struct {
1870 Srcs []string
1871 Javacflags []string
1872 }
1873 Dist struct {
1874 Targets []string
1875 Dest *string
1876 Dir *string
1877 Tag *string
1878 }
Jihoon Kangfe914ed2024-02-12 22:49:21 +00001879 Is_stubs_module *bool
Jihoon Kangfa4a90d2023-12-20 02:53:38 +00001880}
Jiyong Parkc678ad32018-04-10 13:07:10 +09001881
Jihoon Kangfa4a90d2023-12-20 02:53:38 +00001882func (module *SdkLibrary) stubsLibraryProps(mctx android.DefaultableHookContext, apiScope *apiScope) libraryProperties {
1883 props := libraryProperties{}
Jihoon Kang786df932023-09-07 01:18:31 +00001884 props.Visibility = []string{"//visibility:override", "//visibility:private"}
Jiyong Parkc678ad32018-04-10 13:07:10 +09001885 // sources are generated from the droiddoc
Paul Duffin12ceb462019-12-24 20:31:31 +00001886 sdkVersion := module.sdkVersionForStubsLibrary(mctx, apiScope)
Paul Duffin52d398a2019-06-11 12:31:14 +01001887 props.Sdk_version = proptools.StringPtr(sdkVersion)
Paul Duffina18abc22020-05-16 18:54:24 +01001888 props.System_modules = module.deviceProperties.System_modules
1889 props.Patch_module = module.properties.Patch_module
Paul Duffin367ab912019-12-23 19:40:36 +00001890 props.Installable = proptools.BoolPtr(false)
Sundong Ahn054b19a2018-10-19 13:46:09 +09001891 props.Libs = module.sdkLibraryProperties.Stub_only_libs
Mark White9421c4c2023-08-10 00:07:03 +00001892 props.Libs = append(props.Libs, module.scopeToProperties[apiScope].Libs...)
Anton Hanssondae54cd2021-04-21 16:30:10 +01001893 props.Static_libs = module.sdkLibraryProperties.Stub_only_static_libs
Paul Duffine22c2ab2020-05-20 19:35:27 +01001894 // The stub-annotations library contains special versions of the annotations
1895 // with CLASS retention policy, so that they're kept.
1896 if proptools.Bool(module.sdkLibraryProperties.Annotations_enabled) {
1897 props.Libs = append(props.Libs, "stub-annotations")
1898 }
Paul Duffina18abc22020-05-16 18:54:24 +01001899 props.Openjdk9.Srcs = module.properties.Openjdk9.Srcs
1900 props.Openjdk9.Javacflags = module.properties.Openjdk9.Javacflags
Anton Hansson83509b52020-05-21 09:21:57 +01001901 // We compile the stubs for 1.8 in line with the main android.jar stubs, and potential
1902 // interop with older developer tools that don't support 1.9.
1903 props.Java_version = proptools.StringPtr("1.8")
Jihoon Kangfe914ed2024-02-12 22:49:21 +00001904 props.Is_stubs_module = proptools.BoolPtr(true)
Paul Duffinf4600f62021-05-13 22:34:45 +01001905
Jihoon Kangfa4a90d2023-12-20 02:53:38 +00001906 return props
1907}
1908
1909// Creates a static java library that has API stubs
1910func (module *SdkLibrary) createStubsLibrary(mctx android.DefaultableHookContext, apiScope *apiScope) {
1911
1912 props := module.stubsLibraryProps(mctx, apiScope)
1913 props.Name = proptools.StringPtr(module.sourceStubsLibraryModuleName(apiScope))
1914 props.Srcs = []string{":" + module.stubsSourceModuleName(apiScope)}
1915
1916 mctx.CreateModule(LibraryFactory, &props, module.sdkComponentPropertiesForChildLibrary())
1917}
1918
1919// Create a static java library that compiles the "exportable" stubs
1920func (module *SdkLibrary) createExportableStubsLibrary(mctx android.DefaultableHookContext, apiScope *apiScope) {
1921 props := module.stubsLibraryProps(mctx, apiScope)
1922 props.Name = proptools.StringPtr(module.exportableSourceStubsLibraryModuleName(apiScope))
1923 props.Srcs = []string{":" + module.stubsSourceModuleName(apiScope) + "{.exportable}"}
1924
Paul Duffin859fe962020-05-15 10:20:31 +01001925 mctx.CreateModule(LibraryFactory, &props, module.sdkComponentPropertiesForChildLibrary())
Jiyong Parkc678ad32018-04-10 13:07:10 +09001926}
1927
Paul Duffin6d0886e2020-04-07 18:49:53 +01001928// Creates a droidstubs module that creates stubs source files from the given full source
Paul Duffinc8782502020-04-29 20:45:27 +01001929// files and also updates and checks the API specification files.
Paul Duffin15f34ef2020-07-20 18:04:44 +01001930func (module *SdkLibrary) createStubsSourcesAndApi(mctx android.DefaultableHookContext, apiScope *apiScope, name string, scopeSpecificDroidstubsArgs []string) {
Jiyong Parkc678ad32018-04-10 13:07:10 +09001931 props := struct {
Sundong Ahn054b19a2018-10-19 13:46:09 +09001932 Name *string
Paul Duffin4911a892020-04-29 23:35:13 +01001933 Visibility []string
Sundong Ahn054b19a2018-10-19 13:46:09 +09001934 Srcs []string
1935 Installable *bool
Paul Duffin52d398a2019-06-11 12:31:14 +01001936 Sdk_version *string
Jihoon Kang3198f3c2023-01-26 08:08:52 +00001937 Api_surface *string
Paul Duffin12ceb462019-12-24 20:31:31 +00001938 System_modules *string
Sundong Ahn054b19a2018-10-19 13:46:09 +09001939 Libs []string
Paul Duffin6877e6d2020-09-25 19:59:14 +01001940 Output_javadoc_comments *bool
Paul Duffin11512472019-02-11 15:55:17 +00001941 Arg_files []string
Sundong Ahn054b19a2018-10-19 13:46:09 +09001942 Args *string
Sundong Ahn054b19a2018-10-19 13:46:09 +09001943 Java_version *string
Paul Duffine22c2ab2020-05-20 19:35:27 +01001944 Annotations_enabled *bool
Sundong Ahn054b19a2018-10-19 13:46:09 +09001945 Merge_annotations_dirs []string
1946 Merge_inclusion_annotations_dirs []string
Paul Duffin0ff08bd2020-04-29 13:30:54 +01001947 Generate_stubs *bool
Anton Hanssone87b03d2020-12-21 15:29:34 +00001948 Previous_api *string
Jihoon Kang6592e872023-12-19 01:13:16 +00001949 Aconfig_declarations []string
Sundong Ahn054b19a2018-10-19 13:46:09 +09001950 Check_api struct {
Anton Hanssone6056152020-12-31 10:37:27 +00001951 Current ApiToCheck
1952 Last_released ApiToCheck
Paul Duffin160fe412020-05-10 19:32:20 +01001953
1954 Api_lint struct {
1955 Enabled *bool
1956 New_since *string
1957 Baseline_file *string
1958 }
Jiyong Park58c518b2018-05-12 22:29:12 +09001959 }
Sundong Ahn1b92c822018-05-29 11:35:17 +09001960 Aidl struct {
1961 Include_dirs []string
1962 Local_include_dirs []string
1963 }
Paul Duffin040e9062020-11-23 17:41:36 +00001964 Dists []android.Dist
Jiyong Parkc678ad32018-04-10 13:07:10 +09001965 }{}
1966
Paul Duffin7b78b4d2020-04-28 14:08:32 +01001967 // The stubs source processing uses the same compile time classpath when extracting the
1968 // API from the implementation library as it does when compiling it. i.e. the same
1969 // * sdk version
1970 // * system_modules
1971 // * libs (static_libs/libs)
Paul Duffin250e6192019-06-07 10:44:37 +01001972
Paul Duffin0ff08bd2020-04-29 13:30:54 +01001973 props.Name = proptools.StringPtr(name)
Anton Hansson944e77d2020-08-19 11:40:22 +01001974 props.Visibility = childModuleVisibility(module.sdkLibraryProperties.Stubs_source_visibility)
Paul Duffina18abc22020-05-16 18:54:24 +01001975 props.Srcs = append(props.Srcs, module.properties.Srcs...)
Anton Hanssonf8ea3722021-09-16 14:24:13 +01001976 props.Srcs = append(props.Srcs, module.sdkLibraryProperties.Api_srcs...)
Paul Duffina18abc22020-05-16 18:54:24 +01001977 props.Sdk_version = module.deviceProperties.Sdk_version
Jihoon Kang3198f3c2023-01-26 08:08:52 +00001978 props.Api_surface = &apiScope.name
Paul Duffina18abc22020-05-16 18:54:24 +01001979 props.System_modules = module.deviceProperties.System_modules
Jiyong Parkc678ad32018-04-10 13:07:10 +09001980 props.Installable = proptools.BoolPtr(false)
Sundong Ahne6f0b052018-06-05 16:46:14 +09001981 // A droiddoc module has only one Libs property and doesn't distinguish between
1982 // shared libs and static libs. So we need to add both of these libs to Libs property.
Paul Duffina18abc22020-05-16 18:54:24 +01001983 props.Libs = module.properties.Libs
1984 props.Libs = append(props.Libs, module.properties.Static_libs...)
Nikita Ioffed732da72022-11-21 12:38:25 +00001985 props.Libs = append(props.Libs, module.sdkLibraryProperties.Stub_only_libs...)
Mark White9421c4c2023-08-10 00:07:03 +00001986 props.Libs = append(props.Libs, module.scopeToProperties[apiScope].Libs...)
Paul Duffina18abc22020-05-16 18:54:24 +01001987 props.Aidl.Include_dirs = module.deviceProperties.Aidl.Include_dirs
1988 props.Aidl.Local_include_dirs = module.deviceProperties.Aidl.Local_include_dirs
1989 props.Java_version = module.properties.Java_version
Jiyong Parkc678ad32018-04-10 13:07:10 +09001990
Paul Duffine22c2ab2020-05-20 19:35:27 +01001991 props.Annotations_enabled = module.sdkLibraryProperties.Annotations_enabled
Sundong Ahn054b19a2018-10-19 13:46:09 +09001992 props.Merge_annotations_dirs = module.sdkLibraryProperties.Merge_annotations_dirs
1993 props.Merge_inclusion_annotations_dirs = module.sdkLibraryProperties.Merge_inclusion_annotations_dirs
Jihoon Kang6592e872023-12-19 01:13:16 +00001994 props.Aconfig_declarations = module.sdkLibraryProperties.Aconfig_declarations
Sundong Ahn054b19a2018-10-19 13:46:09 +09001995
Paul Duffin6d0886e2020-04-07 18:49:53 +01001996 droidstubsArgs := []string{}
Paul Duffin235ffff2019-12-24 10:41:30 +00001997 if len(module.sdkLibraryProperties.Api_packages) != 0 {
Paul Duffin6d0886e2020-04-07 18:49:53 +01001998 droidstubsArgs = append(droidstubsArgs, "--stub-packages "+strings.Join(module.sdkLibraryProperties.Api_packages, ":"))
Paul Duffin235ffff2019-12-24 10:41:30 +00001999 }
2000 if len(module.sdkLibraryProperties.Hidden_api_packages) != 0 {
Paul Duffin6d0886e2020-04-07 18:49:53 +01002001 droidstubsArgs = append(droidstubsArgs,
Paul Duffin235ffff2019-12-24 10:41:30 +00002002 android.JoinWithPrefix(module.sdkLibraryProperties.Hidden_api_packages, " --hide-package "))
2003 }
Paul Duffin6d0886e2020-04-07 18:49:53 +01002004 droidstubsArgs = append(droidstubsArgs, module.sdkLibraryProperties.Droiddoc_options...)
Anton Hanssonfd1c0d22023-11-02 15:18:09 +00002005 disabledWarnings := []string{"HiddenSuperclass"}
2006 if proptools.BoolDefault(module.sdkLibraryProperties.Api_lint.Legacy_errors_allowed, true) {
2007 disabledWarnings = append(disabledWarnings,
2008 "BroadcastBehavior",
2009 "DeprecationMismatch",
2010 "MissingPermission",
2011 "SdkConstant",
2012 "Todo",
2013 )
Paul Duffin235ffff2019-12-24 10:41:30 +00002014 }
Paul Duffin6d0886e2020-04-07 18:49:53 +01002015 droidstubsArgs = append(droidstubsArgs, android.JoinWithPrefix(disabledWarnings, "--hide "))
Sundong Ahnfb2721f2018-09-17 13:23:09 +09002016
Paul Duffin6877e6d2020-09-25 19:59:14 +01002017 // Output Javadoc comments for public scope.
2018 if apiScope == apiScopePublic {
2019 props.Output_javadoc_comments = proptools.BoolPtr(true)
2020 }
2021
Paul Duffin1fb487d2020-04-07 18:50:10 +01002022 // Add in scope specific arguments.
Paul Duffin0ff08bd2020-04-29 13:30:54 +01002023 droidstubsArgs = append(droidstubsArgs, scopeSpecificDroidstubsArgs...)
Paul Duffin11512472019-02-11 15:55:17 +00002024 props.Arg_files = module.sdkLibraryProperties.Droiddoc_option_files
Paul Duffin6d0886e2020-04-07 18:49:53 +01002025 props.Args = proptools.StringPtr(strings.Join(droidstubsArgs, " "))
Jiyong Parkc678ad32018-04-10 13:07:10 +09002026
Paul Duffin15f34ef2020-07-20 18:04:44 +01002027 // List of APIs identified from the provided source files are created. They are later
2028 // compared against to the not-yet-released (a.k.a current) list of APIs and to the
2029 // last-released (a.k.a numbered) list of API.
2030 currentApiFileName := apiScope.apiFilePrefix + "current.txt"
2031 removedApiFileName := apiScope.apiFilePrefix + "removed.txt"
2032 apiDir := module.getApiDir()
2033 currentApiFileName = path.Join(apiDir, currentApiFileName)
2034 removedApiFileName = path.Join(apiDir, removedApiFileName)
Jiyong Parkc678ad32018-04-10 13:07:10 +09002035
Paul Duffin15f34ef2020-07-20 18:04:44 +01002036 // check against the not-yet-release API
2037 props.Check_api.Current.Api_file = proptools.StringPtr(currentApiFileName)
2038 props.Check_api.Current.Removed_api_file = proptools.StringPtr(removedApiFileName)
Jiyong Park58c518b2018-05-12 22:29:12 +09002039
Paul Duffin958806b2022-05-16 13:10:47 +00002040 if module.compareAgainstLatestApi(apiScope) {
Paul Duffin15f34ef2020-07-20 18:04:44 +01002041 // check against the latest released API
2042 latestApiFilegroupName := proptools.StringPtr(module.latestApiFilegroupName(apiScope))
Anton Hanssone87b03d2020-12-21 15:29:34 +00002043 props.Previous_api = latestApiFilegroupName
Paul Duffin15f34ef2020-07-20 18:04:44 +01002044 props.Check_api.Last_released.Api_file = latestApiFilegroupName
2045 props.Check_api.Last_released.Removed_api_file = proptools.StringPtr(
2046 module.latestRemovedApiFilegroupName(apiScope))
Jaewoong Jung1a97ee02021-03-09 13:25:02 -08002047 props.Check_api.Last_released.Baseline_file = proptools.StringPtr(
2048 module.latestIncompatibilitiesFilegroupName(apiScope))
Paul Duffin160fe412020-05-10 19:32:20 +01002049
Paul Duffin15f34ef2020-07-20 18:04:44 +01002050 if proptools.Bool(module.sdkLibraryProperties.Api_lint.Enabled) {
2051 // Enable api lint.
2052 props.Check_api.Api_lint.Enabled = proptools.BoolPtr(true)
2053 props.Check_api.Api_lint.New_since = latestApiFilegroupName
Paul Duffin160fe412020-05-10 19:32:20 +01002054
Paul Duffin15f34ef2020-07-20 18:04:44 +01002055 // If it exists then pass a lint-baseline.txt through to droidstubs.
2056 baselinePath := path.Join(apiDir, apiScope.apiFilePrefix+"lint-baseline.txt")
2057 baselinePathRelativeToRoot := path.Join(mctx.ModuleDir(), baselinePath)
2058 paths, err := mctx.GlobWithDeps(baselinePathRelativeToRoot, nil)
2059 if err != nil {
2060 mctx.ModuleErrorf("error checking for presence of %s: %s", baselinePathRelativeToRoot, err)
2061 }
2062 if len(paths) == 1 {
2063 props.Check_api.Api_lint.Baseline_file = proptools.StringPtr(baselinePath)
2064 } else if len(paths) != 0 {
2065 mctx.ModuleErrorf("error checking for presence of %s: expected one path, found: %v", baselinePathRelativeToRoot, paths)
Paul Duffin160fe412020-05-10 19:32:20 +01002066 }
2067 }
Paul Duffin15f34ef2020-07-20 18:04:44 +01002068 }
Jiyong Park58c518b2018-05-12 22:29:12 +09002069
Paul Duffin15f34ef2020-07-20 18:04:44 +01002070 if !Bool(module.sdkLibraryProperties.No_dist) {
Paul Duffin040e9062020-11-23 17:41:36 +00002071 // Dist the api txt and removed api txt artifacts for sdk builds.
2072 distDir := proptools.StringPtr(path.Join(module.apiDistPath(apiScope), "api"))
Jihoon Kang02168052024-03-20 00:44:54 +00002073 stubsTypeTagPrefix := ""
2074 if mctx.Config().ReleaseHiddenApiExportableStubs() {
2075 stubsTypeTagPrefix = ".exportable"
2076 }
Paul Duffin040e9062020-11-23 17:41:36 +00002077 for _, p := range []struct {
2078 tag string
2079 pattern string
2080 }{
Jihoon Kangd1799f62024-02-20 23:01:38 +00002081 // "exportable" api files are copied to the dist directory instead of the
Jihoon Kang02168052024-03-20 00:44:54 +00002082 // "everything" api files when "RELEASE_HIDDEN_API_EXPORTABLE_STUBS" build flag
2083 // is set. Otherwise, the "everything" api files are copied to the dist directory.
2084 {tag: "%s.api.txt", pattern: "%s.txt"},
2085 {tag: "%s.removed-api.txt", pattern: "%s-removed.txt"},
Paul Duffin040e9062020-11-23 17:41:36 +00002086 } {
2087 props.Dists = append(props.Dists, android.Dist{
2088 Targets: []string{"sdk", "win_sdk"},
2089 Dir: distDir,
2090 Dest: proptools.StringPtr(fmt.Sprintf(p.pattern, module.distStem())),
Jihoon Kang02168052024-03-20 00:44:54 +00002091 Tag: proptools.StringPtr(fmt.Sprintf(p.tag, stubsTypeTagPrefix)),
Paul Duffin040e9062020-11-23 17:41:36 +00002092 })
2093 }
Anton Hansson5fd5d242020-03-27 19:43:19 +00002094 }
2095
Spandan Das2cc80ba2023-10-27 17:21:52 +00002096 mctx.CreateModule(DroidstubsFactory, &props, module.sdkComponentPropertiesForChildLibrary()).(*Droidstubs).CallHookIfAvailable(mctx)
Jiyong Parkc678ad32018-04-10 13:07:10 +09002097}
2098
Jihoon Kang0c705a42023-08-02 06:44:57 +00002099func (module *SdkLibrary) createApiLibrary(mctx android.DefaultableHookContext, apiScope *apiScope, alternativeFullApiSurfaceStub string) {
Jihoon Kang1c92c3e2023-03-23 17:44:51 +00002100 props := struct {
Jihoon Kangca198c22023-06-22 23:13:51 +00002101 Name *string
2102 Visibility []string
2103 Api_contributions []string
2104 Libs []string
2105 Static_libs []string
2106 Full_api_surface_stub *string
Jihoon Kang4ec24872023-10-05 17:26:09 +00002107 System_modules *string
Jihoon Kang063ec002023-06-28 01:16:23 +00002108 Enable_validation *bool
Jihoon Kang5d701272024-02-15 21:53:49 +00002109 Stubs_type *string
Jihoon Kang1c92c3e2023-03-23 17:44:51 +00002110 }{}
2111
2112 props.Name = proptools.StringPtr(module.apiLibraryModuleName(apiScope))
Jihoon Kang786df932023-09-07 01:18:31 +00002113 props.Visibility = []string{"//visibility:override", "//visibility:private"}
Jihoon Kang1c92c3e2023-03-23 17:44:51 +00002114
2115 apiContributions := []string{}
2116
2117 // Api surfaces are not independent of each other, but have subset relationships,
2118 // and so does the api files. To generate from-text stubs for api surfaces other than public,
2119 // all subset api domains' api_contriubtions must be added as well.
2120 scope := apiScope
2121 for scope != nil {
2122 apiContributions = append(apiContributions, module.stubsSourceModuleName(scope)+".api.contribution")
2123 scope = scope.extends
2124 }
Jihoon Kang0c705a42023-08-02 06:44:57 +00002125 if apiScope == apiScopePublic {
2126 additionalApiContribution := module.apiLibraryAdditionalApiContribution()
2127 if additionalApiContribution != "" {
2128 apiContributions = append(apiContributions, additionalApiContribution)
2129 }
2130 }
Jihoon Kang1c92c3e2023-03-23 17:44:51 +00002131
2132 props.Api_contributions = apiContributions
2133 props.Libs = module.properties.Libs
2134 props.Libs = append(props.Libs, module.sdkLibraryProperties.Stub_only_libs...)
Mark White9421c4c2023-08-10 00:07:03 +00002135 props.Libs = append(props.Libs, module.scopeToProperties[apiScope].Libs...)
Jihoon Kang1c92c3e2023-03-23 17:44:51 +00002136 props.Libs = append(props.Libs, "stub-annotations")
2137 props.Static_libs = module.sdkLibraryProperties.Stub_only_static_libs
Jihoon Kange7ee2562023-07-25 05:51:46 +00002138 props.Full_api_surface_stub = proptools.StringPtr(apiScope.kind.DefaultJavaLibraryName())
Jihoon Kang0c705a42023-08-02 06:44:57 +00002139 if alternativeFullApiSurfaceStub != "" {
2140 props.Full_api_surface_stub = proptools.StringPtr(alternativeFullApiSurfaceStub)
2141 }
Jihoon Kang1c92c3e2023-03-23 17:44:51 +00002142
2143 // android_module_lib_stubs_current.from-text only comprises api contributions from art, conscrypt and i18n.
2144 // Thus, replace with android_module_lib_stubs_current_full.from-text, which comprises every api domains.
2145 if apiScope.kind == android.SdkModule {
Jihoon Kangca198c22023-06-22 23:13:51 +00002146 props.Full_api_surface_stub = proptools.StringPtr(apiScope.kind.DefaultJavaLibraryName() + "_full.from-text")
Jihoon Kang1c92c3e2023-03-23 17:44:51 +00002147 }
2148
Jihoon Kangd30ac8a2023-10-09 18:00:17 +00002149 // java_sdk_library modules that set sdk_version as none does not depend on other api
2150 // domains. Therefore, java_api_library created from such modules should not depend on
2151 // full_api_surface_stubs but create and compile stubs by the java_api_library module
2152 // itself.
2153 if module.SdkVersion(mctx).Kind == android.SdkNone {
2154 props.Full_api_surface_stub = nil
2155 }
2156
Jihoon Kang4ec24872023-10-05 17:26:09 +00002157 props.System_modules = module.deviceProperties.System_modules
Jihoon Kang063ec002023-06-28 01:16:23 +00002158 props.Enable_validation = proptools.BoolPtr(true)
Jihoon Kang5d701272024-02-15 21:53:49 +00002159 props.Stubs_type = proptools.StringPtr("everything")
Jihoon Kang4ec24872023-10-05 17:26:09 +00002160
Spandan Das2cc80ba2023-10-27 17:21:52 +00002161 mctx.CreateModule(ApiLibraryFactory, &props, module.sdkComponentPropertiesForChildLibrary())
Jihoon Kang1c92c3e2023-03-23 17:44:51 +00002162}
2163
Jihoon Kang02168052024-03-20 00:44:54 +00002164func (module *SdkLibrary) topLevelStubsLibraryProps(mctx android.DefaultableHookContext, apiScope *apiScope, doDist bool) libraryProperties {
Jihoon Kangfa4a90d2023-12-20 02:53:38 +00002165 props := libraryProperties{}
2166
Jihoon Kang1147b312023-06-08 23:25:57 +00002167 props.Visibility = childModuleVisibility(module.sdkLibraryProperties.Stubs_library_visibility)
2168 sdkVersion := module.sdkVersionForStubsLibrary(mctx, apiScope)
2169 props.Sdk_version = proptools.StringPtr(sdkVersion)
2170
Jihoon Kang1147b312023-06-08 23:25:57 +00002171 props.System_modules = module.deviceProperties.System_modules
2172
Jihoon Kang1147b312023-06-08 23:25:57 +00002173 // The imports need to be compiled to dex if the java_sdk_library requests it.
2174 compileDex := module.dexProperties.Compile_dex
2175 if module.stubLibrariesCompiledForDex() {
2176 compileDex = proptools.BoolPtr(true)
2177 }
2178 props.Compile_dex = compileDex
2179
Jihoon Kang02168052024-03-20 00:44:54 +00002180 if !Bool(module.sdkLibraryProperties.No_dist) && doDist {
2181 props.Dist.Targets = []string{"sdk", "win_sdk"}
2182 props.Dist.Dest = proptools.StringPtr(fmt.Sprintf("%v.jar", module.distStem()))
2183 props.Dist.Dir = proptools.StringPtr(module.apiDistPath(apiScope))
2184 props.Dist.Tag = proptools.StringPtr(".jar")
2185 }
2186
Jihoon Kangfa4a90d2023-12-20 02:53:38 +00002187 return props
2188}
2189
2190func (module *SdkLibrary) createTopLevelStubsLibrary(
2191 mctx android.DefaultableHookContext, apiScope *apiScope, contributesToApiSurface bool) {
2192
Jihoon Kang02168052024-03-20 00:44:54 +00002193 // Dist the "everything" stubs when the RELEASE_HIDDEN_API_EXPORTABLE_STUBS build flag is false
2194 doDist := !mctx.Config().ReleaseHiddenApiExportableStubs()
2195 props := module.topLevelStubsLibraryProps(mctx, apiScope, doDist)
Jihoon Kangfa4a90d2023-12-20 02:53:38 +00002196 props.Name = proptools.StringPtr(module.stubsLibraryModuleName(apiScope))
2197
2198 // Add the stub compiling java_library/java_api_library as static lib based on build config
2199 staticLib := module.sourceStubsLibraryModuleName(apiScope)
2200 if mctx.Config().BuildFromTextStub() && contributesToApiSurface {
2201 staticLib = module.apiLibraryModuleName(apiScope)
2202 }
2203 props.Static_libs = append(props.Static_libs, staticLib)
2204
2205 mctx.CreateModule(LibraryFactory, &props, module.sdkComponentPropertiesForChildLibrary())
2206}
2207
2208func (module *SdkLibrary) createTopLevelExportableStubsLibrary(
2209 mctx android.DefaultableHookContext, apiScope *apiScope) {
2210
Jihoon Kang02168052024-03-20 00:44:54 +00002211 // Dist the "exportable" stubs when the RELEASE_HIDDEN_API_EXPORTABLE_STUBS build flag is true
2212 doDist := mctx.Config().ReleaseHiddenApiExportableStubs()
2213 props := module.topLevelStubsLibraryProps(mctx, apiScope, doDist)
Jihoon Kangfa4a90d2023-12-20 02:53:38 +00002214 props.Name = proptools.StringPtr(module.exportableStubsLibraryModuleName(apiScope))
2215
Jihoon Kangfa4a90d2023-12-20 02:53:38 +00002216 staticLib := module.exportableSourceStubsLibraryModuleName(apiScope)
2217 props.Static_libs = append(props.Static_libs, staticLib)
2218
Jihoon Kang1147b312023-06-08 23:25:57 +00002219 mctx.CreateModule(LibraryFactory, &props, module.sdkComponentPropertiesForChildLibrary())
2220}
2221
Paul Duffin958806b2022-05-16 13:10:47 +00002222func (module *SdkLibrary) compareAgainstLatestApi(apiScope *apiScope) bool {
2223 return !(apiScope.unstable || module.sdkLibraryProperties.Unsafe_ignore_missing_latest_api)
2224}
2225
Paul Duffinea8f8082021-06-24 13:25:57 +01002226// Implements android.ApexModule
Jooyung Han5e9013b2020-03-10 06:23:13 +09002227func (module *SdkLibrary) DepIsInSameApex(mctx android.BaseModuleContext, dep android.Module) bool {
2228 depTag := mctx.OtherModuleDependencyTag(dep)
2229 if depTag == xmlPermissionsFileTag {
2230 return true
2231 }
Jihoon Kanga3a05462024-04-05 00:36:44 +00002232 if dep.Name() == module.implLibraryModuleName() {
2233 return true
2234 }
Jooyung Han5e9013b2020-03-10 06:23:13 +09002235 return module.Library.DepIsInSameApex(mctx, dep)
2236}
2237
Paul Duffinea8f8082021-06-24 13:25:57 +01002238// Implements android.ApexModule
2239func (module *SdkLibrary) UniqueApexVariations() bool {
2240 return module.uniqueApexVariations()
2241}
2242
Jihoon Kang80456fd2023-11-15 19:22:14 +00002243func (module *SdkLibrary) ContributeToApi() bool {
2244 return proptools.BoolDefault(module.sdkLibraryProperties.Contribute_to_android_api, false)
2245}
2246
Jiyong Parkc678ad32018-04-10 13:07:10 +09002247// Creates the xml file that publicizes the runtime library
Paul Duffinf0229202020-04-29 16:47:28 +01002248func (module *SdkLibrary) createXmlFile(mctx android.DefaultableHookContext) {
Spandan Das8c9ae7e2023-03-03 21:20:36 +00002249 moduleMinApiLevel := module.Library.MinSdkVersion(mctx)
Pedro Loureiroc3621422021-09-28 15:40:23 +00002250 var moduleMinApiLevelStr = moduleMinApiLevel.String()
2251 if moduleMinApiLevel == android.NoneApiLevel {
2252 moduleMinApiLevelStr = "current"
2253 }
Jiyong Parke3833882020-02-17 17:28:10 +09002254 props := struct {
Pedro Loureiroc3621422021-09-28 15:40:23 +00002255 Name *string
2256 Lib_name *string
2257 Apex_available []string
2258 On_bootclasspath_since *string
2259 On_bootclasspath_before *string
2260 Min_device_sdk *string
2261 Max_device_sdk *string
2262 Sdk_library_min_api_level *string
Jamie Garsidee570ace2023-11-27 12:07:36 +00002263 Uses_libs_dependencies []string
Jiyong Parke3833882020-02-17 17:28:10 +09002264 }{
Pedro Loureiroc3621422021-09-28 15:40:23 +00002265 Name: proptools.StringPtr(module.xmlPermissionsModuleName()),
2266 Lib_name: proptools.StringPtr(module.BaseModuleName()),
2267 Apex_available: module.ApexProperties.Apex_available,
2268 On_bootclasspath_since: module.commonSdkLibraryProperties.On_bootclasspath_since,
2269 On_bootclasspath_before: module.commonSdkLibraryProperties.On_bootclasspath_before,
2270 Min_device_sdk: module.commonSdkLibraryProperties.Min_device_sdk,
2271 Max_device_sdk: module.commonSdkLibraryProperties.Max_device_sdk,
2272 Sdk_library_min_api_level: &moduleMinApiLevelStr,
Jamie Garsidee570ace2023-11-27 12:07:36 +00002273 Uses_libs_dependencies: module.usesLibraryProperties.Uses_libs,
Jiyong Parkc678ad32018-04-10 13:07:10 +09002274 }
Jiyong Parke3833882020-02-17 17:28:10 +09002275
Jiyong Parke3833882020-02-17 17:28:10 +09002276 mctx.CreateModule(sdkLibraryXmlFactory, &props)
Jiyong Parkc678ad32018-04-10 13:07:10 +09002277}
2278
Jiyong Parkf1691d22021-03-29 20:11:58 +09002279func PrebuiltJars(ctx android.BaseModuleContext, baseName string, s android.SdkSpec) android.Paths {
Jiyong Park54105c42021-03-31 18:17:53 +09002280 var ver android.ApiLevel
Jiyong Parkf1691d22021-03-29 20:11:58 +09002281 var kind android.SdkKind
2282 if s.UsePrebuilt(ctx) {
Jiyong Park54105c42021-03-31 18:17:53 +09002283 ver = s.ApiLevel
Jiyong Parkf1691d22021-03-29 20:11:58 +09002284 kind = s.Kind
Jiyong Parkc678ad32018-04-10 13:07:10 +09002285 } else {
Jiyong Park6a927c42020-01-21 02:03:43 +09002286 // We don't have prebuilt SDK for the specific sdkVersion.
2287 // Instead of breaking the build, fallback to use "system_current"
Jiyong Park54105c42021-03-31 18:17:53 +09002288 ver = android.FutureApiLevel
Jiyong Parkf1691d22021-03-29 20:11:58 +09002289 kind = android.SdkSystem
Sundong Ahn054b19a2018-10-19 13:46:09 +09002290 }
Jiyong Park6a927c42020-01-21 02:03:43 +09002291
2292 dir := filepath.Join("prebuilts", "sdk", ver.String(), kind.String())
Paul Duffin50061512020-01-21 16:31:05 +00002293 jar := filepath.Join(dir, baseName+".jar")
Sundong Ahn054b19a2018-10-19 13:46:09 +09002294 jarPath := android.ExistentPathForSource(ctx, jar)
Sundong Ahnae418ac2019-02-28 15:01:28 +09002295 if !jarPath.Valid() {
Colin Cross07c88562020-01-07 09:34:44 -08002296 if ctx.Config().AllowMissingDependencies() {
2297 return android.Paths{android.PathForSource(ctx, jar)}
2298 } else {
Jiyong Parkf1691d22021-03-29 20:11:58 +09002299 ctx.PropertyErrorf("sdk_library", "invalid sdk version %q, %q does not exist", s.Raw, jar)
Colin Cross07c88562020-01-07 09:34:44 -08002300 }
Sundong Ahnae418ac2019-02-28 15:01:28 +09002301 return nil
2302 }
Sundong Ahn054b19a2018-10-19 13:46:09 +09002303 return android.Paths{jarPath.Path()}
2304}
2305
Colin Crossaede88c2020-08-11 12:17:01 -07002306// 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 +01002307//
2308// If either this or the other module are on the platform then this will return
2309// false.
Colin Cross56a83212020-09-15 18:30:11 -07002310func withinSameApexesAs(ctx android.BaseModuleContext, other android.Module) bool {
Colin Crossff694a82023-12-13 15:54:49 -08002311 apexInfo, _ := android.ModuleProvider(ctx, android.ApexInfoProvider)
Colin Cross313aa542023-12-13 13:47:44 -08002312 otherApexInfo, _ := android.OtherModuleProvider(ctx, other, android.ApexInfoProvider)
Jiyong Parkab50b072021-05-12 17:13:56 +09002313 return len(otherApexInfo.InApexVariants) > 0 && reflect.DeepEqual(apexInfo.InApexVariants, otherApexInfo.InApexVariants)
Paul Duffin9b879592020-05-26 13:21:35 +01002314}
2315
Jihoon Kang8479dea2024-04-04 01:19:05 +00002316func (module *SdkLibrary) sdkJars(ctx android.BaseModuleContext, sdkVersion android.SdkSpec) android.Paths {
Jiyong Park932cdfe2020-05-28 00:19:53 +09002317 // If the client doesn't set sdk_version, but if this library prefers stubs over
2318 // the impl library, let's provide the widest API surface possible. To do so,
2319 // force override sdk_version to module_current so that the closest possible API
2320 // surface could be found in selectHeaderJarsForSdkVersion
Jiyong Parkf1691d22021-03-29 20:11:58 +09002321 if module.defaultsToStubs() && !sdkVersion.Specified() {
Jiyong Park92315372021-04-02 08:45:46 +09002322 sdkVersion = android.SdkSpecFrom(ctx, "module_current")
Jiyong Park932cdfe2020-05-28 00:19:53 +09002323 }
Paul Duffind1b3a922020-01-22 11:57:20 +00002324
Paul Duffindaaa3322020-05-26 18:13:57 +01002325 // Only provide access to the implementation library if it is actually built.
2326 if module.requiresRuntimeImplementationLibrary() {
2327 // Check any special cases for java_sdk_library.
2328 //
2329 // Only allow access to the implementation library in the following condition:
2330 // * No sdk_version specified on the referencing module.
Paul Duffin9b879592020-05-26 13:21:35 +01002331 // * The referencing module is in the same apex as this.
Jiyong Parkf1691d22021-03-29 20:11:58 +09002332 if sdkVersion.Kind == android.SdkPrivate || withinSameApexesAs(ctx, module) {
Jihoon Kang8479dea2024-04-04 01:19:05 +00002333 return module.implLibraryHeaderJars
Sundong Ahn054b19a2018-10-19 13:46:09 +09002334 }
Jiyong Parkc678ad32018-04-10 13:07:10 +09002335 }
Paul Duffinb05d4292020-05-20 12:19:10 +01002336
Paul Duffin23970f42020-05-20 14:20:02 +01002337 return module.selectHeaderJarsForSdkVersion(ctx, sdkVersion)
Jiyong Parkc678ad32018-04-10 13:07:10 +09002338}
2339
Sundong Ahn241cd372018-07-13 16:16:44 +09002340// to satisfy SdkLibraryDependency interface
Jiyong Parkf1691d22021-03-29 20:11:58 +09002341func (module *SdkLibrary) SdkHeaderJars(ctx android.BaseModuleContext, sdkVersion android.SdkSpec) android.Paths {
Jihoon Kang8479dea2024-04-04 01:19:05 +00002342 return module.sdkJars(ctx, sdkVersion)
Sundong Ahn241cd372018-07-13 16:16:44 +09002343}
2344
Colin Cross571cccf2019-02-04 11:22:08 -08002345var javaSdkLibrariesKey = android.NewOnceKey("javaSdkLibraries")
2346
Jiyong Park82484c02018-04-23 21:41:26 +09002347func javaSdkLibraries(config android.Config) *[]string {
Colin Cross571cccf2019-02-04 11:22:08 -08002348 return config.Once(javaSdkLibrariesKey, func() interface{} {
Jiyong Park82484c02018-04-23 21:41:26 +09002349 return &[]string{}
2350 }).(*[]string)
2351}
2352
Paul Duffin749f98f2019-12-30 17:23:46 +00002353func (module *SdkLibrary) getApiDir() string {
2354 return proptools.StringDefault(module.sdkLibraryProperties.Api_dir, "api")
2355}
2356
Jiyong Parkc678ad32018-04-10 13:07:10 +09002357// For a java_sdk_library module, create internal modules for stubs, docs,
2358// runtime libs and xml file. If requested, the stubs and docs are created twice
2359// once for public API level and once for system API level
Paul Duffinf0229202020-04-29 16:47:28 +01002360func (module *SdkLibrary) CreateInternalModules(mctx android.DefaultableHookContext) {
2361 // If the module has been disabled then don't create any child modules.
Cole Fausta963b942024-04-11 17:43:00 -07002362 if !module.Enabled(mctx) {
Paul Duffinf0229202020-04-29 16:47:28 +01002363 return
2364 }
2365
Paul Duffina18abc22020-05-16 18:54:24 +01002366 if len(module.properties.Srcs) == 0 {
Inseob Kimc0907f12019-02-08 21:00:45 +09002367 mctx.PropertyErrorf("srcs", "java_sdk_library must specify srcs")
Jooyung Han58f26ab2019-12-18 15:34:32 +09002368 return
Inseob Kimc0907f12019-02-08 21:00:45 +09002369 }
2370
Paul Duffin37e0b772019-12-30 17:20:10 +00002371 // If this builds against standard libraries (i.e. is not part of the core libraries)
Paul Duffin4f5c1ef2020-11-19 14:53:43 +00002372 // then assume it provides both system and test apis.
Jiyong Parkf1691d22021-03-29 20:11:58 +09002373 sdkDep := decodeSdkDep(mctx, android.SdkContext(&module.Library))
Paul Duffin37e0b772019-12-30 17:20:10 +00002374 hasSystemAndTestApis := sdkDep.hasStandardLibs()
Paul Duffin3375e352020-04-28 10:44:03 +01002375 module.sdkLibraryProperties.Generate_system_and_test_apis = hasSystemAndTestApis
Paul Duffin4f5c1ef2020-11-19 14:53:43 +00002376
Jaewoong Jung18aefc12020-12-21 09:11:10 -08002377 missingCurrentApi := false
Inseob Kim8098faa2019-03-18 10:19:51 +09002378
Paul Duffin3375e352020-04-28 10:44:03 +01002379 generatedScopes := module.getGeneratedApiScopes(mctx)
Paul Duffind1b3a922020-01-22 11:57:20 +00002380
Paul Duffin749f98f2019-12-30 17:23:46 +00002381 apiDir := module.getApiDir()
Paul Duffin3375e352020-04-28 10:44:03 +01002382 for _, scope := range generatedScopes {
Inseob Kim8098faa2019-03-18 10:19:51 +09002383 for _, api := range []string{"current.txt", "removed.txt"} {
Paul Duffind1b3a922020-01-22 11:57:20 +00002384 path := path.Join(mctx.ModuleDir(), apiDir, scope.apiFilePrefix+api)
Inseob Kim8098faa2019-03-18 10:19:51 +09002385 p := android.ExistentPathForSource(mctx, path)
2386 if !p.Valid() {
Colin Cross18f840c2021-05-20 17:56:54 -07002387 if mctx.Config().AllowMissingDependencies() {
2388 mctx.AddMissingDependencies([]string{path})
2389 } else {
2390 mctx.ModuleErrorf("Current api file %#v doesn't exist", path)
2391 missingCurrentApi = true
2392 }
Inseob Kim8098faa2019-03-18 10:19:51 +09002393 }
2394 }
2395 }
2396
Jaewoong Jung18aefc12020-12-21 09:11:10 -08002397 if missingCurrentApi {
Inseob Kim8098faa2019-03-18 10:19:51 +09002398 script := "build/soong/scripts/gen-java-current-api-files.sh"
2399 p := android.ExistentPathForSource(mctx, script)
2400
2401 if !p.Valid() {
2402 panic(fmt.Sprintf("script file %s doesn't exist", script))
2403 }
2404
2405 mctx.ModuleErrorf("One or more current api files are missing. "+
2406 "You can update them by:\n"+
Paul Duffin37e0b772019-12-30 17:20:10 +00002407 "%s %q %s && m update-api",
Paul Duffind1b3a922020-01-22 11:57:20 +00002408 script, filepath.Join(mctx.ModuleDir(), apiDir),
Paul Duffin3375e352020-04-28 10:44:03 +01002409 strings.Join(generatedScopes.Strings(func(s *apiScope) string { return s.apiFilePrefix }), " "))
Inseob Kim8098faa2019-03-18 10:19:51 +09002410 return
2411 }
2412
Paul Duffin3375e352020-04-28 10:44:03 +01002413 for _, scope := range generatedScopes {
Paul Duffin15f34ef2020-07-20 18:04:44 +01002414 // Use the stubs source name for legacy reasons.
2415 module.createStubsSourcesAndApi(mctx, scope, module.stubsSourceModuleName(scope), scope.droidstubsArgs)
Paul Duffin0ff08bd2020-04-29 13:30:54 +01002416
Paul Duffind1b3a922020-01-22 11:57:20 +00002417 module.createStubsLibrary(mctx, scope)
Jihoon Kangfa4a90d2023-12-20 02:53:38 +00002418 module.createExportableStubsLibrary(mctx, scope)
Jihoon Kang1c92c3e2023-03-23 17:44:51 +00002419
Jihoon Kang0c705a42023-08-02 06:44:57 +00002420 alternativeFullApiSurfaceStubLib := ""
2421 if scope == apiScopePublic {
2422 alternativeFullApiSurfaceStubLib = module.alternativeFullApiSurfaceStubLib()
2423 }
2424 contributesToApiSurface := module.contributesToApiSurface(mctx.Config()) || alternativeFullApiSurfaceStubLib != ""
Jihoon Kang1147b312023-06-08 23:25:57 +00002425 if contributesToApiSurface {
Jihoon Kang0c705a42023-08-02 06:44:57 +00002426 module.createApiLibrary(mctx, scope, alternativeFullApiSurfaceStubLib)
Jihoon Kang1c92c3e2023-03-23 17:44:51 +00002427 }
Jihoon Kang1147b312023-06-08 23:25:57 +00002428
2429 module.createTopLevelStubsLibrary(mctx, scope, contributesToApiSurface)
Jihoon Kangfa4a90d2023-12-20 02:53:38 +00002430 module.createTopLevelExportableStubsLibrary(mctx, scope)
Inseob Kimc0907f12019-02-08 21:00:45 +09002431 }
2432
Paul Duffindfa131e2020-05-15 20:37:11 +01002433 if module.requiresRuntimeImplementationLibrary() {
Paul Duffin5df79302020-05-16 15:52:12 +01002434 // Create child module to create an implementation library.
2435 //
2436 // This temporarily creates a second implementation library that can be explicitly
2437 // referenced.
2438 //
2439 // TODO(b/156618935) - update comment once only one implementation library is created.
2440 module.createImplLibrary(mctx)
2441
Paul Duffindfa131e2020-05-15 20:37:11 +01002442 // Only create an XML permissions file that declares the library as being usable
2443 // as a shared library if required.
2444 if module.sharedLibrary() {
2445 module.createXmlFile(mctx)
2446 }
Paul Duffin43db9be2019-12-30 17:35:49 +00002447
2448 // record java_sdk_library modules so that they are exported to make
2449 javaSdkLibraries := javaSdkLibraries(mctx.Config())
2450 javaSdkLibrariesLock.Lock()
2451 defer javaSdkLibrariesLock.Unlock()
2452 *javaSdkLibraries = append(*javaSdkLibraries, module.BaseModuleName())
2453 }
Anton Hansson7f66efa2020-10-08 14:47:23 +01002454
Paul Duffin77590a82022-04-28 14:13:30 +00002455 // 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 +01002456 module.properties.Libs = append(module.properties.Libs, module.sdkLibraryProperties.Impl_only_libs...)
Paul Duffin77590a82022-04-28 14:13:30 +00002457 module.properties.Static_libs = append(module.properties.Static_libs, module.sdkLibraryProperties.Impl_only_static_libs...)
Inseob Kimc0907f12019-02-08 21:00:45 +09002458}
2459
2460func (module *SdkLibrary) InitSdkLibraryProperties() {
Colin Crossce6734e2020-06-15 16:09:53 -07002461 module.addHostAndDeviceProperties()
2462 module.AddProperties(&module.sdkLibraryProperties)
Sundong Ahn054b19a2018-10-19 13:46:09 +09002463
Paul Duffin71b33cc2021-06-23 11:39:47 +01002464 module.initSdkLibraryComponent(module)
Paul Duffin859fe962020-05-15 10:20:31 +01002465
Paul Duffina18abc22020-05-16 18:54:24 +01002466 module.properties.Installable = proptools.BoolPtr(true)
2467 module.deviceProperties.IsSDKLibrary = true
Inseob Kimc0907f12019-02-08 21:00:45 +09002468}
Sundong Ahn054b19a2018-10-19 13:46:09 +09002469
Paul Duffindfa131e2020-05-15 20:37:11 +01002470func (module *SdkLibrary) requiresRuntimeImplementationLibrary() bool {
2471 return !proptools.Bool(module.sdkLibraryProperties.Api_only)
2472}
2473
Jiyong Park932cdfe2020-05-28 00:19:53 +09002474func (module *SdkLibrary) defaultsToStubs() bool {
2475 return proptools.Bool(module.sdkLibraryProperties.Default_to_stubs)
2476}
2477
Paul Duffin1b1e8062020-05-08 13:44:43 +01002478// Defines how to name the individual component modules the sdk library creates.
2479type sdkLibraryComponentNamingScheme interface {
2480 stubsLibraryModuleName(scope *apiScope, baseName string) string
2481
2482 stubsSourceModuleName(scope *apiScope, baseName string) string
Jihoon Kang1c92c3e2023-03-23 17:44:51 +00002483
2484 apiLibraryModuleName(scope *apiScope, baseName string) string
Jihoon Kang1147b312023-06-08 23:25:57 +00002485
Jihoon Kangfa4a90d2023-12-20 02:53:38 +00002486 sourceStubsLibraryModuleName(scope *apiScope, baseName string) string
2487
2488 exportableStubsLibraryModuleName(scope *apiScope, baseName string) string
2489
2490 exportableSourceStubsLibraryModuleName(scope *apiScope, baseName string) string
Paul Duffin1b1e8062020-05-08 13:44:43 +01002491}
2492
2493type defaultNamingScheme struct {
2494}
2495
2496func (s *defaultNamingScheme) stubsLibraryModuleName(scope *apiScope, baseName string) string {
2497 return scope.stubsLibraryModuleName(baseName)
2498}
2499
2500func (s *defaultNamingScheme) stubsSourceModuleName(scope *apiScope, baseName string) string {
2501 return scope.stubsSourceModuleName(baseName)
2502}
2503
Jihoon Kang1c92c3e2023-03-23 17:44:51 +00002504func (s *defaultNamingScheme) apiLibraryModuleName(scope *apiScope, baseName string) string {
2505 return scope.apiLibraryModuleName(baseName)
2506}
2507
Jihoon Kangfa4a90d2023-12-20 02:53:38 +00002508func (s *defaultNamingScheme) sourceStubsLibraryModuleName(scope *apiScope, baseName string) string {
Jihoon Kang1147b312023-06-08 23:25:57 +00002509 return scope.sourceStubLibraryModuleName(baseName)
2510}
2511
Jihoon Kangfa4a90d2023-12-20 02:53:38 +00002512func (s *defaultNamingScheme) exportableStubsLibraryModuleName(scope *apiScope, baseName string) string {
2513 return scope.exportableStubsLibraryModuleName(baseName)
2514}
2515
2516func (s *defaultNamingScheme) exportableSourceStubsLibraryModuleName(scope *apiScope, baseName string) string {
2517 return scope.exportableSourceStubsLibraryModuleName(baseName)
2518}
2519
Paul Duffin1b1e8062020-05-08 13:44:43 +01002520var _ sdkLibraryComponentNamingScheme = (*defaultNamingScheme)(nil)
2521
Jihoon Kangfa4a90d2023-12-20 02:53:38 +00002522func hasStubsLibrarySuffix(name string, apiScope *apiScope) bool {
2523 return strings.HasSuffix(name, apiScope.stubsLibraryModuleNameSuffix()) ||
2524 strings.HasSuffix(name, apiScope.exportableStubsLibraryModuleNameSuffix())
2525}
2526
Jaewoong Jungbc15e3a2021-03-10 17:02:43 -08002527func moduleStubLinkType(name string) (stub bool, ret sdkLinkType) {
Jihoon Kang1147b312023-06-08 23:25:57 +00002528 name = strings.TrimSuffix(name, ".from-source")
2529
Anton Hansson2d0c1942020-05-25 12:20:51 +01002530 // This suffix-based approach is fragile and could potentially mis-trigger.
2531 // TODO(b/155164730): Clean this up when modules no longer reference sdk_lib stubs directly.
Jihoon Kangfa4a90d2023-12-20 02:53:38 +00002532 if hasStubsLibrarySuffix(name, apiScopePublic) {
Anton Hansson08f476b2021-04-07 15:32:19 +01002533 if name == "hwbinder.stubs" || name == "libcore_private.stubs" {
2534 // Due to a previous bug, these modules were not considered stubs, so we retain that.
2535 return false, javaPlatform
2536 }
Anton Hansson2d0c1942020-05-25 12:20:51 +01002537 return true, javaSdk
2538 }
Jihoon Kangfa4a90d2023-12-20 02:53:38 +00002539 if hasStubsLibrarySuffix(name, apiScopeSystem) {
Anton Hansson2d0c1942020-05-25 12:20:51 +01002540 return true, javaSystem
2541 }
Jihoon Kangfa4a90d2023-12-20 02:53:38 +00002542 if hasStubsLibrarySuffix(name, apiScopeModuleLib) {
Anton Hansson2d0c1942020-05-25 12:20:51 +01002543 return true, javaModule
2544 }
Jihoon Kangfa4a90d2023-12-20 02:53:38 +00002545 if hasStubsLibrarySuffix(name, apiScopeTest) {
Anton Hansson2d0c1942020-05-25 12:20:51 +01002546 return true, javaSystem
2547 }
Jihoon Kangfa4a90d2023-12-20 02:53:38 +00002548 if hasStubsLibrarySuffix(name, apiScopeSystemServer) {
Jihoon Kang1147b312023-06-08 23:25:57 +00002549 return true, javaSystemServer
2550 }
Anton Hansson2d0c1942020-05-25 12:20:51 +01002551 return false, javaPlatform
2552}
2553
Jaewoong Jung4f158ee2019-07-11 10:05:35 -07002554// java_sdk_library is a special Java library that provides optional platform APIs to apps.
2555// In practice, it can be viewed as a combination of several modules: 1) stubs library that clients
2556// are linked against to, 2) droiddoc module that internally generates API stubs source files,
2557// 3) the real runtime shared library that implements the APIs, and 4) XML file for adding
2558// the runtime lib to the classpath at runtime if requested via <uses-library>.
Inseob Kimc0907f12019-02-08 21:00:45 +09002559func SdkLibraryFactory() android.Module {
2560 module := &SdkLibrary{}
Paul Duffinc3091c82020-05-08 14:16:20 +01002561
2562 // Initialize information common between source and prebuilt.
Paul Duffin71b33cc2021-06-23 11:39:47 +01002563 module.initCommon(module)
Paul Duffinc3091c82020-05-08 14:16:20 +01002564
Inseob Kimc0907f12019-02-08 21:00:45 +09002565 module.InitSdkLibraryProperties()
Jooyung Han58f26ab2019-12-18 15:34:32 +09002566 android.InitApexModule(module)
Sundong Ahn054b19a2018-10-19 13:46:09 +09002567 InitJavaModule(module, android.HostAndDeviceSupported)
Paul Duffin3375e352020-04-28 10:44:03 +01002568
2569 // Initialize the map from scope to scope specific properties.
2570 scopeToProperties := make(map[*apiScope]*ApiScopeProperties)
2571 for _, scope := range allApiScopes {
2572 scopeToProperties[scope] = scope.scopeSpecificProperties(module)
2573 }
2574 module.scopeToProperties = scopeToProperties
2575
Paul Duffin4911a892020-04-29 23:35:13 +01002576 // Add the properties containing visibility rules so that they are checked.
Paul Duffin5df79302020-05-16 15:52:12 +01002577 android.AddVisibilityProperty(module, "impl_library_visibility", &module.sdkLibraryProperties.Impl_library_visibility)
Paul Duffin4911a892020-04-29 23:35:13 +01002578 android.AddVisibilityProperty(module, "stubs_library_visibility", &module.sdkLibraryProperties.Stubs_library_visibility)
2579 android.AddVisibilityProperty(module, "stubs_source_visibility", &module.sdkLibraryProperties.Stubs_source_visibility)
2580
Paul Duffin1b1e8062020-05-08 13:44:43 +01002581 module.SetDefaultableHook(func(ctx android.DefaultableHookContext) {
Paul Duffindfa131e2020-05-15 20:37:11 +01002582 // If no implementation is required then it cannot be used as a shared library
2583 // either.
2584 if !module.requiresRuntimeImplementationLibrary() {
2585 // If shared_library has been explicitly set to true then it is incompatible
2586 // with api_only: true.
2587 if proptools.Bool(module.commonSdkLibraryProperties.Shared_library) {
2588 ctx.PropertyErrorf("api_only/shared_library", "inconsistent settings, shared_library and api_only cannot both be true")
2589 }
2590 // Set shared_library: false.
2591 module.commonSdkLibraryProperties.Shared_library = proptools.BoolPtr(false)
2592 }
2593
Paul Duffin1b1e8062020-05-08 13:44:43 +01002594 if module.initCommonAfterDefaultsApplied(ctx) {
2595 module.CreateInternalModules(ctx)
2596 }
2597 })
Jiyong Parkc678ad32018-04-10 13:07:10 +09002598 return module
2599}
Colin Cross79c7c262019-04-17 11:11:46 -07002600
2601//
2602// SDK library prebuilts
2603//
2604
Paul Duffin56d44902020-01-31 13:36:25 +00002605// Properties associated with each api scope.
2606type sdkLibraryScopeProperties struct {
Colin Cross79c7c262019-04-17 11:11:46 -07002607 Jars []string `android:"path"`
2608
2609 Sdk_version *string
2610
Colin Cross79c7c262019-04-17 11:11:46 -07002611 // List of shared java libs that this module has dependencies to
2612 Libs []string
Paul Duffin3d1248c2020-04-09 00:10:17 +01002613
Paul Duffinc8782502020-04-29 20:45:27 +01002614 // The stubs source.
Paul Duffin3d1248c2020-04-09 00:10:17 +01002615 Stub_srcs []string `android:"path"`
Paul Duffin1fd005d2020-04-09 01:08:11 +01002616
2617 // The current.txt
Paul Duffin0f8faff2020-05-20 16:18:00 +01002618 Current_api *string `android:"path"`
Paul Duffin1fd005d2020-04-09 01:08:11 +01002619
2620 // The removed.txt
Paul Duffin0f8faff2020-05-20 16:18:00 +01002621 Removed_api *string `android:"path"`
Anton Hanssond78eb762021-09-21 15:25:12 +01002622
2623 // Annotation zip
2624 Annotations *string `android:"path"`
Colin Cross79c7c262019-04-17 11:11:46 -07002625}
2626
Paul Duffin56d44902020-01-31 13:36:25 +00002627type sdkLibraryImportProperties struct {
Paul Duffinfcfd7912020-01-31 17:54:30 +00002628 // List of shared java libs, common to all scopes, that this module has
2629 // dependencies to
2630 Libs []string
Paul Duffin1267d872021-04-16 17:21:36 +01002631
2632 // If set to true, compile dex files for the stubs. Defaults to false.
2633 Compile_dex *bool
Paul Duffin869de142021-07-15 14:14:41 +01002634
2635 // If not empty, classes are restricted to the specified packages and their sub-packages.
Paul Duffin869de142021-07-15 14:14:41 +01002636 Permitted_packages []string
Spandan Das23956d12024-01-19 00:22:22 +00002637
2638 // Name of the source soong module that gets shadowed by this prebuilt
2639 // If unspecified, follows the naming convention that the source module of
2640 // the prebuilt is Name() without "prebuilt_" prefix
2641 Source_module_name *string
Paul Duffin56d44902020-01-31 13:36:25 +00002642}
2643
Paul Duffineedc5d52020-06-12 17:46:39 +01002644type SdkLibraryImport struct {
Colin Cross79c7c262019-04-17 11:11:46 -07002645 android.ModuleBase
2646 android.DefaultableModuleBase
2647 prebuilt android.Prebuilt
Paul Duffindd46f712020-02-10 13:37:10 +00002648 android.ApexModuleBase
Colin Cross79c7c262019-04-17 11:11:46 -07002649
Paul Duffin37856732021-02-26 14:24:15 +00002650 hiddenAPI
Jiakai Zhang204356f2021-09-09 08:12:46 +00002651 dexpreopter
Paul Duffin37856732021-02-26 14:24:15 +00002652
Colin Cross79c7c262019-04-17 11:11:46 -07002653 properties sdkLibraryImportProperties
2654
Paul Duffin46a26a82020-04-07 19:27:04 +01002655 // Map from api scope to the scope specific property structure.
2656 scopeProperties map[*apiScope]*sdkLibraryScopeProperties
2657
Paul Duffin56d44902020-01-31 13:36:25 +00002658 commonToSdkLibraryAndImport
Paul Duffineedc5d52020-06-12 17:46:39 +01002659
Paul Duffineedc5d52020-06-12 17:46:39 +01002660 // The reference to the xml permissions module created by the source module.
2661 // Is nil if the source module does not exist.
2662 xmlPermissionsFileModule *sdkLibraryXml
Paul Duffin39853512021-02-26 11:09:39 +00002663
Jeongik Chad5fe8782021-07-08 01:13:11 +09002664 // Build path to the dex implementation jar obtained from the prebuilt_apex, if any.
Spandan Dasfae468e2023-12-12 23:23:53 +00002665 dexJarFile OptionalDexJarPath
2666 dexJarFileErr error
Jeongik Chad5fe8782021-07-08 01:13:11 +09002667
2668 // Expected install file path of the source module(sdk_library)
2669 // or dex implementation jar obtained from the prebuilt_apex, if any.
2670 installFile android.Path
Colin Cross79c7c262019-04-17 11:11:46 -07002671}
2672
Paul Duffineedc5d52020-06-12 17:46:39 +01002673var _ SdkLibraryDependency = (*SdkLibraryImport)(nil)
Colin Cross79c7c262019-04-17 11:11:46 -07002674
Paul Duffin46a26a82020-04-07 19:27:04 +01002675// The type of a structure that contains a field of type sdkLibraryScopeProperties
2676// for each apiscope in allApiScopes, e.g. something like:
Colin Crossd079e0b2022-08-16 10:27:33 -07002677//
2678// struct {
2679// Public sdkLibraryScopeProperties
2680// System sdkLibraryScopeProperties
2681// ...
2682// }
Paul Duffin46a26a82020-04-07 19:27:04 +01002683var allScopeStructType = createAllScopePropertiesStructType()
2684
2685// Dynamically create a structure type for each apiscope in allApiScopes.
2686func createAllScopePropertiesStructType() reflect.Type {
2687 var fields []reflect.StructField
2688 for _, apiScope := range allApiScopes {
2689 field := reflect.StructField{
2690 Name: apiScope.fieldName,
2691 Type: reflect.TypeOf(sdkLibraryScopeProperties{}),
2692 }
2693 fields = append(fields, field)
2694 }
2695
2696 return reflect.StructOf(fields)
2697}
2698
2699// Create an instance of the scope specific structure type and return a map
2700// from apiscope to a pointer to each scope specific field.
2701func createPropertiesInstance() (interface{}, map[*apiScope]*sdkLibraryScopeProperties) {
2702 allScopePropertiesPtr := reflect.New(allScopeStructType)
2703 allScopePropertiesStruct := allScopePropertiesPtr.Elem()
2704 scopeProperties := make(map[*apiScope]*sdkLibraryScopeProperties)
2705
2706 for _, apiScope := range allApiScopes {
2707 field := allScopePropertiesStruct.FieldByName(apiScope.fieldName)
2708 scopeProperties[apiScope] = field.Addr().Interface().(*sdkLibraryScopeProperties)
2709 }
2710
2711 return allScopePropertiesPtr.Interface(), scopeProperties
2712}
2713
Jaewoong Jung4f158ee2019-07-11 10:05:35 -07002714// java_sdk_library_import imports a prebuilt java_sdk_library.
Colin Cross79c7c262019-04-17 11:11:46 -07002715func sdkLibraryImportFactory() android.Module {
Paul Duffineedc5d52020-06-12 17:46:39 +01002716 module := &SdkLibraryImport{}
Colin Cross79c7c262019-04-17 11:11:46 -07002717
Paul Duffin46a26a82020-04-07 19:27:04 +01002718 allScopeProperties, scopeToProperties := createPropertiesInstance()
2719 module.scopeProperties = scopeToProperties
Jiakai Zhang9c4dc192023-02-09 00:09:24 +08002720 module.AddProperties(&module.properties, allScopeProperties, &module.importDexpreoptProperties)
Colin Cross79c7c262019-04-17 11:11:46 -07002721
Paul Duffinc3091c82020-05-08 14:16:20 +01002722 // Initialize information common between source and prebuilt.
Paul Duffin71b33cc2021-06-23 11:39:47 +01002723 module.initCommon(module)
Paul Duffinc3091c82020-05-08 14:16:20 +01002724
Paul Duffin0bdcb272020-02-06 15:24:57 +00002725 android.InitPrebuiltModule(module, &[]string{""})
Paul Duffindd46f712020-02-10 13:37:10 +00002726 android.InitApexModule(module)
Colin Cross79c7c262019-04-17 11:11:46 -07002727 InitJavaModule(module, android.HostAndDeviceSupported)
2728
Paul Duffin1b1e8062020-05-08 13:44:43 +01002729 module.SetDefaultableHook(func(mctx android.DefaultableHookContext) {
2730 if module.initCommonAfterDefaultsApplied(mctx) {
2731 module.createInternalModules(mctx)
2732 }
2733 })
Colin Cross79c7c262019-04-17 11:11:46 -07002734 return module
2735}
2736
Paul Duffin630b11e2021-07-15 13:35:26 +01002737var _ PermittedPackagesForUpdatableBootJars = (*SdkLibraryImport)(nil)
2738
2739func (module *SdkLibraryImport) PermittedPackagesForUpdatableBootJars() []string {
2740 return module.properties.Permitted_packages
2741}
2742
Paul Duffineedc5d52020-06-12 17:46:39 +01002743func (module *SdkLibraryImport) Prebuilt() *android.Prebuilt {
Colin Cross79c7c262019-04-17 11:11:46 -07002744 return &module.prebuilt
2745}
2746
Paul Duffineedc5d52020-06-12 17:46:39 +01002747func (module *SdkLibraryImport) Name() string {
Colin Cross79c7c262019-04-17 11:11:46 -07002748 return module.prebuilt.Name(module.ModuleBase.Name())
2749}
2750
Spandan Das23956d12024-01-19 00:22:22 +00002751func (module *SdkLibraryImport) BaseModuleName() string {
2752 return proptools.StringDefault(module.properties.Source_module_name, module.ModuleBase.Name())
2753}
2754
Paul Duffineedc5d52020-06-12 17:46:39 +01002755func (module *SdkLibraryImport) createInternalModules(mctx android.DefaultableHookContext) {
Colin Cross79c7c262019-04-17 11:11:46 -07002756
Paul Duffin50061512020-01-21 16:31:05 +00002757 // If the build is configured to use prebuilts then force this to be preferred.
Jeongik Cha816a23a2020-07-08 01:09:23 +09002758 if mctx.Config().AlwaysUsePrebuiltSdks() {
Paul Duffin50061512020-01-21 16:31:05 +00002759 module.prebuilt.ForcePrefer()
2760 }
2761
Paul Duffin46a26a82020-04-07 19:27:04 +01002762 for apiScope, scopeProperties := range module.scopeProperties {
Paul Duffin56d44902020-01-31 13:36:25 +00002763 if len(scopeProperties.Jars) == 0 {
2764 continue
2765 }
2766
Paul Duffinbbb546b2020-04-09 00:07:11 +01002767 module.createJavaImportForStubs(mctx, apiScope, scopeProperties)
Paul Duffin3d1248c2020-04-09 00:10:17 +01002768
Paul Duffin0f8faff2020-05-20 16:18:00 +01002769 if len(scopeProperties.Stub_srcs) > 0 {
2770 module.createPrebuiltStubsSources(mctx, apiScope, scopeProperties)
2771 }
Jihoon Kang71c86832023-09-13 01:01:53 +00002772
2773 if scopeProperties.Current_api != nil {
2774 module.createPrebuiltApiContribution(mctx, apiScope, scopeProperties)
2775 }
Paul Duffin56d44902020-01-31 13:36:25 +00002776 }
Colin Cross79c7c262019-04-17 11:11:46 -07002777
2778 javaSdkLibraries := javaSdkLibraries(mctx.Config())
2779 javaSdkLibrariesLock.Lock()
2780 defer javaSdkLibrariesLock.Unlock()
2781 *javaSdkLibraries = append(*javaSdkLibraries, module.BaseModuleName())
2782}
2783
Paul Duffineedc5d52020-06-12 17:46:39 +01002784func (module *SdkLibraryImport) createJavaImportForStubs(mctx android.DefaultableHookContext, apiScope *apiScope, scopeProperties *sdkLibraryScopeProperties) {
Paul Duffinbbb546b2020-04-09 00:07:11 +01002785 // Creates a java import for the jar with ".stubs" suffix
2786 props := struct {
Spandan Das23956d12024-01-19 00:22:22 +00002787 Name *string
2788 Source_module_name *string
2789 Created_by_java_sdk_library_name *string
2790 Sdk_version *string
2791 Libs []string
2792 Jars []string
2793 Compile_dex *bool
Jihoon Kangfe914ed2024-02-12 22:49:21 +00002794 Is_stubs_module *bool
Paul Duffinbf4de042022-09-27 12:41:52 +01002795
2796 android.UserSuppliedPrebuiltProperties
Paul Duffinbbb546b2020-04-09 00:07:11 +01002797 }{}
Paul Duffinc3091c82020-05-08 14:16:20 +01002798 props.Name = proptools.StringPtr(module.stubsLibraryModuleName(apiScope))
Spandan Das23956d12024-01-19 00:22:22 +00002799 props.Source_module_name = proptools.StringPtr(apiScope.stubsLibraryModuleName(module.BaseModuleName()))
2800 props.Created_by_java_sdk_library_name = proptools.StringPtr(module.RootLibraryName())
Paul Duffinbbb546b2020-04-09 00:07:11 +01002801 props.Sdk_version = scopeProperties.Sdk_version
2802 // Prepend any of the libs from the legacy public properties to the libs for each of the
2803 // scopes to avoid having to duplicate them in each scope.
2804 props.Libs = append(module.properties.Libs, scopeProperties.Libs...)
2805 props.Jars = scopeProperties.Jars
Paul Duffin1dbe3ca2020-05-16 09:57:59 +01002806
Paul Duffin38b57852020-05-13 16:08:09 +01002807 // The imports are preferred if the java_sdk_library_import is preferred.
Paul Duffinbf4de042022-09-27 12:41:52 +01002808 props.CopyUserSuppliedPropertiesFromPrebuilt(&module.prebuilt)
Paul Duffin859fe962020-05-15 10:20:31 +01002809
Paul Duffin1267d872021-04-16 17:21:36 +01002810 // The imports need to be compiled to dex if the java_sdk_library_import requests it.
Paul Duffinf4600f62021-05-13 22:34:45 +01002811 compileDex := module.properties.Compile_dex
2812 if module.stubLibrariesCompiledForDex() {
2813 compileDex = proptools.BoolPtr(true)
2814 }
2815 props.Compile_dex = compileDex
Jihoon Kangfe914ed2024-02-12 22:49:21 +00002816 props.Is_stubs_module = proptools.BoolPtr(true)
Paul Duffin1267d872021-04-16 17:21:36 +01002817
Paul Duffin859fe962020-05-15 10:20:31 +01002818 mctx.CreateModule(ImportFactory, &props, module.sdkComponentPropertiesForChildLibrary())
Paul Duffinbbb546b2020-04-09 00:07:11 +01002819}
2820
Paul Duffineedc5d52020-06-12 17:46:39 +01002821func (module *SdkLibraryImport) createPrebuiltStubsSources(mctx android.DefaultableHookContext, apiScope *apiScope, scopeProperties *sdkLibraryScopeProperties) {
Paul Duffin3d1248c2020-04-09 00:10:17 +01002822 props := struct {
Spandan Das23956d12024-01-19 00:22:22 +00002823 Name *string
2824 Source_module_name *string
2825 Created_by_java_sdk_library_name *string
2826 Srcs []string
Paul Duffinbf4de042022-09-27 12:41:52 +01002827
2828 android.UserSuppliedPrebuiltProperties
Paul Duffin3d1248c2020-04-09 00:10:17 +01002829 }{}
Paul Duffinc3091c82020-05-08 14:16:20 +01002830 props.Name = proptools.StringPtr(module.stubsSourceModuleName(apiScope))
Spandan Das23956d12024-01-19 00:22:22 +00002831 props.Source_module_name = proptools.StringPtr(apiScope.stubsSourceModuleName(module.BaseModuleName()))
2832 props.Created_by_java_sdk_library_name = proptools.StringPtr(module.RootLibraryName())
Paul Duffin3d1248c2020-04-09 00:10:17 +01002833 props.Srcs = scopeProperties.Stub_srcs
Paul Duffin38b57852020-05-13 16:08:09 +01002834
2835 // The stubs source is preferred if the java_sdk_library_import is preferred.
Paul Duffinbf4de042022-09-27 12:41:52 +01002836 props.CopyUserSuppliedPropertiesFromPrebuilt(&module.prebuilt)
2837
Spandan Das2cc80ba2023-10-27 17:21:52 +00002838 mctx.CreateModule(PrebuiltStubsSourcesFactory, &props, module.sdkComponentPropertiesForChildLibrary())
Paul Duffin3d1248c2020-04-09 00:10:17 +01002839}
2840
Jihoon Kang71c86832023-09-13 01:01:53 +00002841func (module *SdkLibraryImport) createPrebuiltApiContribution(mctx android.DefaultableHookContext, apiScope *apiScope, scopeProperties *sdkLibraryScopeProperties) {
2842 api_file := scopeProperties.Current_api
2843 api_surface := &apiScope.name
2844
2845 props := struct {
Spandan Das23956d12024-01-19 00:22:22 +00002846 Name *string
2847 Source_module_name *string
2848 Created_by_java_sdk_library_name *string
2849 Api_surface *string
2850 Api_file *string
2851 Visibility []string
Jihoon Kang71c86832023-09-13 01:01:53 +00002852 }{}
2853
2854 props.Name = proptools.StringPtr(module.stubsSourceModuleName(apiScope) + ".api.contribution")
Spandan Das23956d12024-01-19 00:22:22 +00002855 props.Source_module_name = proptools.StringPtr(apiScope.stubsSourceModuleName(module.BaseModuleName()) + ".api.contribution")
2856 props.Created_by_java_sdk_library_name = proptools.StringPtr(module.RootLibraryName())
Jihoon Kang71c86832023-09-13 01:01:53 +00002857 props.Api_surface = api_surface
2858 props.Api_file = api_file
2859 props.Visibility = []string{"//visibility:override", "//visibility:public"}
2860
Spandan Das2cc80ba2023-10-27 17:21:52 +00002861 mctx.CreateModule(ApiContributionImportFactory, &props, module.sdkComponentPropertiesForChildLibrary())
Jihoon Kang71c86832023-09-13 01:01:53 +00002862}
2863
Paul Duffin44f1d842020-06-26 20:17:02 +01002864// Add the dependencies on the child module in the component deps mutator so that it
2865// creates references to the prebuilt and not the source modules.
2866func (module *SdkLibraryImport) ComponentDepsMutator(ctx android.BottomUpMutatorContext) {
Paul Duffin46a26a82020-04-07 19:27:04 +01002867 for apiScope, scopeProperties := range module.scopeProperties {
Paul Duffin56d44902020-01-31 13:36:25 +00002868 if len(scopeProperties.Jars) == 0 {
2869 continue
2870 }
2871
2872 // Add dependencies to the prebuilt stubs library
Jihoon Kangb7431552024-01-22 19:40:08 +00002873 ctx.AddVariationDependencies(nil, apiScope.prebuiltStubsTag, android.PrebuiltNameFromSource(module.stubsLibraryModuleName(apiScope)))
Paul Duffin0f8faff2020-05-20 16:18:00 +01002874
2875 if len(scopeProperties.Stub_srcs) > 0 {
2876 // Add dependencies to the prebuilt stubs source library
Paul Duffin864116c2021-04-02 10:24:13 +01002877 ctx.AddVariationDependencies(nil, apiScope.stubsSourceTag, android.PrebuiltNameFromSource(module.stubsSourceModuleName(apiScope)))
Paul Duffin0f8faff2020-05-20 16:18:00 +01002878 }
Paul Duffin56d44902020-01-31 13:36:25 +00002879 }
Paul Duffin44f1d842020-06-26 20:17:02 +01002880}
2881
2882// Add other dependencies as normal.
2883func (module *SdkLibraryImport) DepsMutator(ctx android.BottomUpMutatorContext) {
Paul Duffineedc5d52020-06-12 17:46:39 +01002884
2885 implName := module.implLibraryModuleName()
2886 if ctx.OtherModuleExists(implName) {
2887 ctx.AddVariationDependencies(nil, implLibraryTag, implName)
2888
2889 xmlPermissionsModuleName := module.xmlPermissionsModuleName()
2890 if module.sharedLibrary() && ctx.OtherModuleExists(xmlPermissionsModuleName) {
2891 // Add dependency to the rule for generating the xml permissions file
2892 ctx.AddDependency(module, xmlPermissionsFileTag, xmlPermissionsModuleName)
2893 }
2894 }
Colin Cross79c7c262019-04-17 11:11:46 -07002895}
2896
Jiyong Park45bf82e2020-12-15 22:29:02 +09002897var _ android.ApexModule = (*SdkLibraryImport)(nil)
2898
2899// Implements android.ApexModule
Paul Duffineedc5d52020-06-12 17:46:39 +01002900func (module *SdkLibraryImport) DepIsInSameApex(mctx android.BaseModuleContext, dep android.Module) bool {
2901 depTag := mctx.OtherModuleDependencyTag(dep)
2902 if depTag == xmlPermissionsFileTag {
2903 return true
2904 }
2905
2906 // None of the other dependencies of the java_sdk_library_import are in the same apex
2907 // as the one that references this module.
2908 return false
2909}
2910
Jiyong Park45bf82e2020-12-15 22:29:02 +09002911// Implements android.ApexModule
Dan Albertc8060532020-07-22 22:32:17 -07002912func (module *SdkLibraryImport) ShouldSupportSdkVersion(ctx android.BaseModuleContext,
2913 sdkVersion android.ApiLevel) error {
Jooyung Han749dc692020-04-15 11:03:39 +09002914 // we don't check prebuilt modules for sdk_version
2915 return nil
2916}
2917
Paul Duffinea8f8082021-06-24 13:25:57 +01002918// Implements android.ApexModule
2919func (module *SdkLibraryImport) UniqueApexVariations() bool {
2920 return module.uniqueApexVariations()
2921}
2922
Paul Duffin09817d62022-04-28 17:45:11 +01002923// MinSdkVersion - Implements hiddenAPIModule
Spandan Das8c9ae7e2023-03-03 21:20:36 +00002924func (module *SdkLibraryImport) MinSdkVersion(ctx android.EarlyModuleContext) android.ApiLevel {
2925 return android.NoneApiLevel
Paul Duffin09817d62022-04-28 17:45:11 +01002926}
2927
2928var _ hiddenAPIModule = (*SdkLibraryImport)(nil)
2929
Paul Duffineedc5d52020-06-12 17:46:39 +01002930func (module *SdkLibraryImport) OutputFiles(tag string) (android.Paths, error) {
Paul Duffin1e940d52022-04-29 14:21:25 +01002931 paths, err := module.commonOutputFiles(tag)
2932 if paths != nil || err != nil {
2933 return paths, err
2934 }
2935 if module.implLibraryModule != nil {
2936 return module.implLibraryModule.OutputFiles(tag)
2937 } else {
2938 return nil, nil
2939 }
Paul Duffin46dc45a2020-05-14 15:39:10 +01002940}
2941
Paul Duffineedc5d52020-06-12 17:46:39 +01002942func (module *SdkLibraryImport) GenerateAndroidBuildActions(ctx android.ModuleContext) {
Paul Duffina2ae7e02020-09-11 11:55:00 +01002943 module.generateCommonBuildActions(ctx)
2944
Jeongik Chad5fe8782021-07-08 01:13:11 +09002945 // Assume that source module(sdk_library) is installed in /<sdk_library partition>/framework
2946 module.installFile = android.PathForModuleInstall(ctx, "framework", module.Stem()+".jar")
2947
Paul Duffin0f8faff2020-05-20 16:18:00 +01002948 // Record the paths to the prebuilt stubs library and stubs source.
Colin Cross79c7c262019-04-17 11:11:46 -07002949 ctx.VisitDirectDeps(func(to android.Module) {
2950 tag := ctx.OtherModuleDependencyTag(to)
2951
Paul Duffin0f8faff2020-05-20 16:18:00 +01002952 // Extract information from any of the scope specific dependencies.
2953 if scopeTag, ok := tag.(scopeDependencyTag); ok {
2954 apiScope := scopeTag.apiScope
2955 scopePaths := module.getScopePathsCreateIfNeeded(apiScope)
2956
2957 // Extract information from the dependency. The exact information extracted
2958 // is determined by the nature of the dependency which is determined by the tag.
2959 scopeTag.extractDepInfo(ctx, to, scopePaths)
Paul Duffineedc5d52020-06-12 17:46:39 +01002960 } else if tag == implLibraryTag {
2961 if implLibrary, ok := to.(*Library); ok {
2962 module.implLibraryModule = implLibrary
2963 } else {
2964 ctx.ModuleErrorf("implementation library must be of type *java.Library but was %T", to)
2965 }
2966 } else if tag == xmlPermissionsFileTag {
2967 if xmlPermissionsFileModule, ok := to.(*sdkLibraryXml); ok {
2968 module.xmlPermissionsFileModule = xmlPermissionsFileModule
2969 } else {
2970 ctx.ModuleErrorf("xml permissions file module must be of type *sdkLibraryXml but was %T", to)
2971 }
Colin Cross79c7c262019-04-17 11:11:46 -07002972 }
2973 })
Paul Duffin0f8faff2020-05-20 16:18:00 +01002974
2975 // Populate the scope paths with information from the properties.
2976 for apiScope, scopeProperties := range module.scopeProperties {
2977 if len(scopeProperties.Jars) == 0 {
2978 continue
2979 }
2980
2981 paths := module.getScopePathsCreateIfNeeded(apiScope)
Anton Hanssond78eb762021-09-21 15:25:12 +01002982 paths.annotationsZip = android.OptionalPathForModuleSrc(ctx, scopeProperties.Annotations)
Paul Duffin0f8faff2020-05-20 16:18:00 +01002983 paths.currentApiFilePath = android.OptionalPathForModuleSrc(ctx, scopeProperties.Current_api)
2984 paths.removedApiFilePath = android.OptionalPathForModuleSrc(ctx, scopeProperties.Removed_api)
2985 }
Paul Duffin39853512021-02-26 11:09:39 +00002986
2987 if ctx.Device() {
2988 // If this is a variant created for a prebuilt_apex then use the dex implementation jar
2989 // obtained from the associated deapexer module.
Colin Crossff694a82023-12-13 15:54:49 -08002990 ai, _ := android.ModuleProvider(ctx, android.ApexInfoProvider)
Paul Duffin39853512021-02-26 11:09:39 +00002991 if ai.ForPrebuiltApex {
Paul Duffin39853512021-02-26 11:09:39 +00002992 // Get the path of the dex implementation jar from the `deapexer` module.
Spandan Dasfae468e2023-12-12 23:23:53 +00002993 di, err := android.FindDeapexerProviderForModule(ctx)
2994 if err != nil {
2995 // An error was found, possibly due to multiple apexes in the tree that export this library
2996 // Defer the error till a client tries to call DexJarBuildPath
2997 module.dexJarFileErr = err
Spandan Das3a392012024-01-17 18:26:27 +00002998 module.initHiddenAPIError(err)
Spandan Dasfae468e2023-12-12 23:23:53 +00002999 return
Martin Stjernholm44825602021-09-17 01:44:12 +01003000 }
Spandan Das5be63332023-12-13 00:06:32 +00003001 dexJarFileApexRootRelative := ApexRootRelativePathToJavaLib(module.BaseModuleName())
Jiakai Zhang81e46812023-02-08 21:56:07 +08003002 if dexOutputPath := di.PrebuiltExportPath(dexJarFileApexRootRelative); dexOutputPath != nil {
Martin Stjernholm8be1e6d2021-09-15 03:34:04 +01003003 dexJarFile := makeDexJarPathFromPath(dexOutputPath)
3004 module.dexJarFile = dexJarFile
Jiakai Zhang204356f2021-09-09 08:12:46 +00003005 installPath := android.PathForModuleInPartitionInstall(
Jiakai Zhang81e46812023-02-08 21:56:07 +08003006 ctx, "apex", ai.ApexVariationName, dexJarFileApexRootRelative)
Jiakai Zhang204356f2021-09-09 08:12:46 +00003007 module.installFile = installPath
Martin Stjernholm8be1e6d2021-09-15 03:34:04 +01003008 module.initHiddenAPI(ctx, dexJarFile, module.findScopePaths(apiScopePublic).stubsImplPath[0], nil)
Jiakai Zhang204356f2021-09-09 08:12:46 +00003009
Spandan Dase21a8d42024-01-23 23:56:29 +00003010 module.dexpreopter.installPath = module.dexpreopter.getInstallPath(ctx, android.RemoveOptionalPrebuiltPrefix(ctx.ModuleName()), installPath)
Jiakai Zhang204356f2021-09-09 08:12:46 +00003011 module.dexpreopter.isSDKLibrary = true
Spandan Dase21a8d42024-01-23 23:56:29 +00003012 module.dexpreopter.uncompressedDex = shouldUncompressDex(ctx, android.RemoveOptionalPrebuiltPrefix(ctx.ModuleName()), &module.dexpreopter)
Jiakai Zhang81e46812023-02-08 21:56:07 +08003013
3014 if profilePath := di.PrebuiltExportPath(dexJarFileApexRootRelative + ".prof"); profilePath != nil {
3015 module.dexpreopter.inputProfilePathOnHost = profilePath
3016 }
Paul Duffin39853512021-02-26 11:09:39 +00003017 } else {
3018 // This should never happen as a variant for a prebuilt_apex is only created if the
3019 // prebuilt_apex has been configured to export the java library dex file.
Martin Stjernholm44825602021-09-17 01:44:12 +01003020 ctx.ModuleErrorf("internal error: no dex implementation jar available from prebuilt APEX %s", di.ApexModuleName())
Paul Duffin39853512021-02-26 11:09:39 +00003021 }
3022 }
3023 }
Colin Cross79c7c262019-04-17 11:11:46 -07003024}
3025
Jiyong Parkf1691d22021-03-29 20:11:58 +09003026func (module *SdkLibraryImport) sdkJars(ctx android.BaseModuleContext, sdkVersion android.SdkSpec, headerJars bool) android.Paths {
Paul Duffineedc5d52020-06-12 17:46:39 +01003027
3028 // For consistency with SdkLibrary make the implementation jar available to libraries that
3029 // are within the same APEX.
3030 implLibraryModule := module.implLibraryModule
Colin Cross56a83212020-09-15 18:30:11 -07003031 if implLibraryModule != nil && withinSameApexesAs(ctx, module) {
Paul Duffineedc5d52020-06-12 17:46:39 +01003032 if headerJars {
3033 return implLibraryModule.HeaderJars()
3034 } else {
3035 return implLibraryModule.ImplementationJars()
3036 }
3037 }
3038
Paul Duffin23970f42020-05-20 14:20:02 +01003039 return module.selectHeaderJarsForSdkVersion(ctx, sdkVersion)
Paul Duffin56d44902020-01-31 13:36:25 +00003040}
3041
Colin Cross79c7c262019-04-17 11:11:46 -07003042// to satisfy SdkLibraryDependency interface
Jiyong Parkf1691d22021-03-29 20:11:58 +09003043func (module *SdkLibraryImport) SdkHeaderJars(ctx android.BaseModuleContext, sdkVersion android.SdkSpec) android.Paths {
Colin Cross79c7c262019-04-17 11:11:46 -07003044 // This module is just a wrapper for the prebuilt stubs.
Paul Duffineedc5d52020-06-12 17:46:39 +01003045 return module.sdkJars(ctx, sdkVersion, true)
Colin Cross79c7c262019-04-17 11:11:46 -07003046}
3047
Ulya Trafimovichdbf31662020-12-17 12:07:54 +00003048// to satisfy UsesLibraryDependency interface
Spandan Das59a4a2b2024-01-09 21:35:56 +00003049func (module *SdkLibraryImport) DexJarBuildPath(ctx android.ModuleErrorfContext) OptionalDexJarPath {
Paul Duffin39853512021-02-26 11:09:39 +00003050 // The dex implementation jar extracted from the .apex file should be used in preference to the
3051 // source.
Spandan Dasfae468e2023-12-12 23:23:53 +00003052 if module.dexJarFileErr != nil {
Spandan Das59a4a2b2024-01-09 21:35:56 +00003053 ctx.ModuleErrorf(module.dexJarFileErr.Error())
Spandan Dasfae468e2023-12-12 23:23:53 +00003054 }
Martin Stjernholm8be1e6d2021-09-15 03:34:04 +01003055 if module.dexJarFile.IsSet() {
Paul Duffin39853512021-02-26 11:09:39 +00003056 return module.dexJarFile
3057 }
Paul Duffineedc5d52020-06-12 17:46:39 +01003058 if module.implLibraryModule == nil {
Martin Stjernholm8be1e6d2021-09-15 03:34:04 +01003059 return makeUnsetDexJarPath()
Paul Duffineedc5d52020-06-12 17:46:39 +01003060 } else {
Spandan Das59a4a2b2024-01-09 21:35:56 +00003061 return module.implLibraryModule.DexJarBuildPath(ctx)
Paul Duffineedc5d52020-06-12 17:46:39 +01003062 }
3063}
3064
Ulya Trafimovichdbf31662020-12-17 12:07:54 +00003065// to satisfy UsesLibraryDependency interface
Ulya Trafimovich31e444e2020-08-14 17:32:16 +01003066func (module *SdkLibraryImport) DexJarInstallPath() android.Path {
Jeongik Chad5fe8782021-07-08 01:13:11 +09003067 return module.installFile
Ulya Trafimovich31e444e2020-08-14 17:32:16 +01003068}
3069
Ulya Trafimovichdbf31662020-12-17 12:07:54 +00003070// to satisfy UsesLibraryDependency interface
3071func (module *SdkLibraryImport) ClassLoaderContexts() dexpreopt.ClassLoaderContextMap {
3072 return nil
3073}
3074
Paul Duffineedc5d52020-06-12 17:46:39 +01003075// to satisfy apex.javaDependency interface
3076func (module *SdkLibraryImport) JacocoReportClassesFile() android.Path {
3077 if module.implLibraryModule == nil {
3078 return nil
3079 } else {
3080 return module.implLibraryModule.JacocoReportClassesFile()
3081 }
3082}
3083
3084// to satisfy apex.javaDependency interface
Colin Cross08dca382020-07-21 20:31:17 -07003085func (module *SdkLibraryImport) LintDepSets() LintDepSets {
3086 if module.implLibraryModule == nil {
3087 return LintDepSets{}
3088 } else {
3089 return module.implLibraryModule.LintDepSets()
3090 }
3091}
3092
Spandan Das17854f52022-01-14 21:19:14 +00003093func (module *SdkLibraryImport) GetStrictUpdatabilityLinting() bool {
Jaewoong Jung476b9d62021-05-10 15:30:00 -07003094 if module.implLibraryModule == nil {
3095 return false
3096 } else {
Spandan Das17854f52022-01-14 21:19:14 +00003097 return module.implLibraryModule.GetStrictUpdatabilityLinting()
Jaewoong Jung476b9d62021-05-10 15:30:00 -07003098 }
3099}
3100
Spandan Das17854f52022-01-14 21:19:14 +00003101func (module *SdkLibraryImport) SetStrictUpdatabilityLinting(strictLinting bool) {
Jaewoong Jung476b9d62021-05-10 15:30:00 -07003102 if module.implLibraryModule != nil {
Spandan Das17854f52022-01-14 21:19:14 +00003103 module.implLibraryModule.SetStrictUpdatabilityLinting(strictLinting)
Jaewoong Jung476b9d62021-05-10 15:30:00 -07003104 }
3105}
3106
Colin Cross08dca382020-07-21 20:31:17 -07003107// to satisfy apex.javaDependency interface
Paul Duffineedc5d52020-06-12 17:46:39 +01003108func (module *SdkLibraryImport) Stem() string {
3109 return module.BaseModuleName()
Colin Cross79c7c262019-04-17 11:11:46 -07003110}
Jiyong Parke3833882020-02-17 17:28:10 +09003111
Paul Duffin44b481b2020-06-17 16:59:43 +01003112var _ ApexDependency = (*SdkLibraryImport)(nil)
3113
3114// to satisfy java.ApexDependency interface
3115func (module *SdkLibraryImport) HeaderJars() android.Paths {
3116 if module.implLibraryModule == nil {
3117 return nil
3118 } else {
3119 return module.implLibraryModule.HeaderJars()
3120 }
3121}
3122
3123// to satisfy java.ApexDependency interface
3124func (module *SdkLibraryImport) ImplementationAndResourcesJars() android.Paths {
3125 if module.implLibraryModule == nil {
3126 return nil
3127 } else {
3128 return module.implLibraryModule.ImplementationAndResourcesJars()
3129 }
3130}
3131
Jiakai Zhang204356f2021-09-09 08:12:46 +00003132// to satisfy java.DexpreopterInterface interface
3133func (module *SdkLibraryImport) IsInstallable() bool {
3134 return true
3135}
3136
Paul Duffinfef55002021-06-17 14:56:05 +01003137var _ android.RequiredFilesFromPrebuiltApex = (*SdkLibraryImport)(nil)
3138
Paul Duffinb4bbf2c2021-06-17 15:59:07 +01003139func (module *SdkLibraryImport) RequiredFilesFromPrebuiltApex(ctx android.BaseModuleContext) []string {
Paul Duffinfef55002021-06-17 14:56:05 +01003140 name := module.BaseModuleName()
Jiakai Zhang81e46812023-02-08 21:56:07 +08003141 return requiredFilesFromPrebuiltApexForImport(name, &module.dexpreopter)
Paul Duffinfef55002021-06-17 14:56:05 +01003142}
3143
Spandan Das2ea84dd2024-01-25 22:12:50 +00003144func (j *SdkLibraryImport) UseProfileGuidedDexpreopt() bool {
3145 return proptools.Bool(j.importDexpreoptProperties.Dex_preopt.Profile_guided)
3146}
3147
Jiyong Parke3833882020-02-17 17:28:10 +09003148// java_sdk_library_xml
Jiyong Parke3833882020-02-17 17:28:10 +09003149type sdkLibraryXml struct {
3150 android.ModuleBase
3151 android.DefaultableModuleBase
3152 android.ApexModuleBase
3153
3154 properties sdkLibraryXmlProperties
3155
3156 outputFilePath android.OutputPath
3157 installDirPath android.InstallPath
Colin Cross56a83212020-09-15 18:30:11 -07003158
3159 hideApexVariantFromMake bool
Jiyong Parke3833882020-02-17 17:28:10 +09003160}
3161
3162type sdkLibraryXmlProperties struct {
3163 // canonical name of the lib
3164 Lib_name *string
Pedro Loureiro9956e5e2021-09-07 17:21:59 +00003165
3166 // Signals that this shared library is part of the bootclasspath starting
3167 // on the version indicated in this attribute.
3168 //
3169 // This will make platforms at this level and above to ignore
3170 // <uses-library> tags with this library name because the library is already
3171 // available
3172 On_bootclasspath_since *string
3173
3174 // Signals that this shared library was part of the bootclasspath before
3175 // (but not including) the version indicated in this attribute.
3176 //
3177 // The system will automatically add a <uses-library> tag with this library to
3178 // apps that target any SDK less than the version indicated in this attribute.
3179 On_bootclasspath_before *string
3180
3181 // Indicates that PackageManager should ignore this shared library if the
3182 // platform is below the version indicated in this attribute.
3183 //
3184 // This means that the device won't recognise this library as installed.
3185 Min_device_sdk *string
3186
3187 // Indicates that PackageManager should ignore this shared library if the
3188 // platform is above the version indicated in this attribute.
3189 //
3190 // This means that the device won't recognise this library as installed.
3191 Max_device_sdk *string
Pedro Loureiroc3621422021-09-28 15:40:23 +00003192
3193 // The SdkLibrary's min api level as a string
3194 //
3195 // This value comes from the ApiLevel of the MinSdkVersion property.
3196 Sdk_library_min_api_level *string
Jamie Garsidee570ace2023-11-27 12:07:36 +00003197
3198 // Uses-libs dependencies that the shared library requires to work correctly.
3199 //
3200 // This will add dependency="foo:bar" to the <library> section.
3201 Uses_libs_dependencies []string
Jiyong Parke3833882020-02-17 17:28:10 +09003202}
3203
3204// java_sdk_library_xml builds the permission xml file for a java_sdk_library.
3205// Not to be used directly by users. java_sdk_library internally uses this.
3206func sdkLibraryXmlFactory() android.Module {
3207 module := &sdkLibraryXml{}
3208
3209 module.AddProperties(&module.properties)
3210
3211 android.InitApexModule(module)
3212 android.InitAndroidArchModule(module, android.DeviceSupported, android.MultilibCommon)
3213
3214 return module
3215}
3216
Colin Crossaede88c2020-08-11 12:17:01 -07003217func (module *sdkLibraryXml) UniqueApexVariations() bool {
3218 // sdkLibraryXml needs a unique variation per APEX because the generated XML file contains the path to the
3219 // mounted APEX, which contains the name of the APEX.
3220 return true
3221}
3222
Jiyong Parke3833882020-02-17 17:28:10 +09003223// from android.PrebuiltEtcModule
Jooyung Han0703fd82020-08-26 22:11:53 +09003224func (module *sdkLibraryXml) BaseDir() string {
3225 return "etc"
3226}
3227
3228// from android.PrebuiltEtcModule
Jiyong Parke3833882020-02-17 17:28:10 +09003229func (module *sdkLibraryXml) SubDir() string {
3230 return "permissions"
3231}
3232
3233// from android.PrebuiltEtcModule
ThiƩbaud Weksteen00e8b312024-03-18 14:06:00 +11003234func (module *sdkLibraryXml) OutputFiles(tag string) (android.Paths, error) {
3235 return android.OutputPaths{module.outputFilePath}.Paths(), nil
Jiyong Parke3833882020-02-17 17:28:10 +09003236}
3237
ThiƩbaud Weksteen00e8b312024-03-18 14:06:00 +11003238var _ etc.PrebuiltEtcModule = (*sdkLibraryXml)(nil)
3239
Jiyong Parke3833882020-02-17 17:28:10 +09003240// from android.ApexModule
3241func (module *sdkLibraryXml) AvailableFor(what string) bool {
3242 return true
3243}
3244
3245func (module *sdkLibraryXml) DepsMutator(ctx android.BottomUpMutatorContext) {
3246 // do nothing
3247}
3248
Jiyong Park45bf82e2020-12-15 22:29:02 +09003249var _ android.ApexModule = (*sdkLibraryXml)(nil)
3250
3251// Implements android.ApexModule
Dan Albertc8060532020-07-22 22:32:17 -07003252func (module *sdkLibraryXml) ShouldSupportSdkVersion(ctx android.BaseModuleContext,
3253 sdkVersion android.ApiLevel) error {
Jooyung Han749dc692020-04-15 11:03:39 +09003254 // sdkLibraryXml doesn't need to be checked separately because java_sdk_library is checked
3255 return nil
3256}
3257
Jiyong Parke3833882020-02-17 17:28:10 +09003258// File path to the runtime implementation library
Colin Cross56a83212020-09-15 18:30:11 -07003259func (module *sdkLibraryXml) implPath(ctx android.ModuleContext) string {
Jiyong Parke3833882020-02-17 17:28:10 +09003260 implName := proptools.String(module.properties.Lib_name)
Colin Crossff694a82023-12-13 15:54:49 -08003261 if apexInfo, _ := android.ModuleProvider(ctx, android.ApexInfoProvider); !apexInfo.IsForPlatform() {
Colin Crosse07f2312020-08-13 11:24:56 -07003262 // TODO(b/146468504): ApexVariationName() is only a soong module name, not apex name.
Jiyong Parke3833882020-02-17 17:28:10 +09003263 // In most cases, this works fine. But when apex_name is set or override_apex is used
3264 // this can be wrong.
Colin Cross56a83212020-09-15 18:30:11 -07003265 return fmt.Sprintf("/apex/%s/javalib/%s.jar", apexInfo.ApexVariationName, implName)
Jiyong Parke3833882020-02-17 17:28:10 +09003266 }
3267 partition := "system"
3268 if module.SocSpecific() {
3269 partition = "vendor"
3270 } else if module.DeviceSpecific() {
3271 partition = "odm"
3272 } else if module.ProductSpecific() {
3273 partition = "product"
3274 } else if module.SystemExtSpecific() {
3275 partition = "system_ext"
3276 }
3277 return "/" + partition + "/framework/" + implName + ".jar"
3278}
3279
Pedro Loureiro9956e5e2021-09-07 17:21:59 +00003280func formattedOptionalSdkLevelAttribute(ctx android.ModuleContext, attrName string, value *string) string {
3281 if value == nil {
3282 return ""
3283 }
3284 apiLevel, err := android.ApiLevelFromUser(ctx, *value)
3285 if err != nil {
Pedro Loureiroba6682f2021-10-29 09:32:32 +00003286 // attributes in bp files have underscores but in the xml have dashes.
3287 ctx.PropertyErrorf(strings.ReplaceAll(attrName, "-", "_"), err.Error())
Pedro Loureiro9956e5e2021-09-07 17:21:59 +00003288 return ""
3289 }
Pedro Loureirob638c622021-12-22 15:28:05 +00003290 if apiLevel.IsCurrent() {
3291 // passing "current" would always mean a future release, never the current (or the current in
3292 // progress) which means some conditions would never be triggered.
3293 ctx.PropertyErrorf(strings.ReplaceAll(attrName, "-", "_"),
3294 `"current" is not an allowed value for this attribute`)
3295 return ""
3296 }
Pedro Loureiro48991222022-06-17 20:01:21 +00003297 // "safeValue" is safe because it translates finalized codenames to a string
3298 // with their SDK int.
3299 safeValue := apiLevel.String()
3300 return formattedOptionalAttribute(attrName, &safeValue)
Pedro Loureiro9956e5e2021-09-07 17:21:59 +00003301}
3302
3303// formats an attribute for the xml permissions file if the value is not null
3304// returns empty string otherwise
3305func formattedOptionalAttribute(attrName string, value *string) string {
3306 if value == nil {
3307 return ""
3308 }
Paul Duffin1816cde2024-04-10 10:58:21 +01003309 return fmt.Sprintf(" %s=\"%s\"\n", attrName, *value)
Pedro Loureiro9956e5e2021-09-07 17:21:59 +00003310}
3311
Jamie Garsidee570ace2023-11-27 12:07:36 +00003312func formattedDependenciesAttribute(dependencies []string) string {
3313 if dependencies == nil {
3314 return ""
3315 }
Paul Duffin1816cde2024-04-10 10:58:21 +01003316 return fmt.Sprintf(" dependency=\"%s\"\n", strings.Join(dependencies, ":"))
Jamie Garsidee570ace2023-11-27 12:07:36 +00003317}
3318
Pedro Loureiro9956e5e2021-09-07 17:21:59 +00003319func (module *sdkLibraryXml) permissionsContents(ctx android.ModuleContext) string {
3320 libName := proptools.String(module.properties.Lib_name)
3321 libNameAttr := formattedOptionalAttribute("name", &libName)
3322 filePath := module.implPath(ctx)
3323 filePathAttr := formattedOptionalAttribute("file", &filePath)
Pedro Loureiroba6682f2021-10-29 09:32:32 +00003324 implicitFromAttr := formattedOptionalSdkLevelAttribute(ctx, "on-bootclasspath-since", module.properties.On_bootclasspath_since)
3325 implicitUntilAttr := formattedOptionalSdkLevelAttribute(ctx, "on-bootclasspath-before", module.properties.On_bootclasspath_before)
3326 minSdkAttr := formattedOptionalSdkLevelAttribute(ctx, "min-device-sdk", module.properties.Min_device_sdk)
3327 maxSdkAttr := formattedOptionalSdkLevelAttribute(ctx, "max-device-sdk", module.properties.Max_device_sdk)
Jamie Garsidee570ace2023-11-27 12:07:36 +00003328 dependenciesAttr := formattedDependenciesAttribute(module.properties.Uses_libs_dependencies)
Pedro Loureiro196d3e62021-12-22 19:53:01 +00003329 // <library> is understood in all android versions whereas <apex-library> is only understood from API T (and ignored before that).
3330 // 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 +00003331 var libraryTag string
3332 if module.properties.Min_device_sdk != nil {
Paul Duffin1816cde2024-04-10 10:58:21 +01003333 libraryTag = " <apex-library\n"
Pedro Loureiroc3621422021-09-28 15:40:23 +00003334 } else {
Paul Duffin1816cde2024-04-10 10:58:21 +01003335 libraryTag = " <library\n"
Pedro Loureiroc3621422021-09-28 15:40:23 +00003336 }
Pedro Loureiro9956e5e2021-09-07 17:21:59 +00003337
3338 return strings.Join([]string{
Paul Duffin1816cde2024-04-10 10:58:21 +01003339 "<?xml version=\"1.0\" encoding=\"utf-8\"?>\n",
3340 "<!-- Copyright (C) 2018 The Android Open Source Project\n",
3341 "\n",
3342 " Licensed under the Apache License, Version 2.0 (the \"License\");\n",
3343 " you may not use this file except in compliance with the License.\n",
3344 " You may obtain a copy of the License at\n",
3345 "\n",
3346 " http://www.apache.org/licenses/LICENSE-2.0\n",
3347 "\n",
3348 " Unless required by applicable law or agreed to in writing, software\n",
3349 " distributed under the License is distributed on an \"AS IS\" BASIS,\n",
3350 " WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\n",
3351 " See the License for the specific language governing permissions and\n",
3352 " limitations under the License.\n",
3353 "-->\n",
3354 "<permissions>\n",
Pedro Loureiroc3621422021-09-28 15:40:23 +00003355 libraryTag,
Pedro Loureiro9956e5e2021-09-07 17:21:59 +00003356 libNameAttr,
3357 filePathAttr,
3358 implicitFromAttr,
3359 implicitUntilAttr,
3360 minSdkAttr,
3361 maxSdkAttr,
Jamie Garsidee570ace2023-11-27 12:07:36 +00003362 dependenciesAttr,
Paul Duffin1816cde2024-04-10 10:58:21 +01003363 " />\n",
3364 "</permissions>\n",
3365 }, "")
Pedro Loureiro9956e5e2021-09-07 17:21:59 +00003366}
3367
Jiyong Parke3833882020-02-17 17:28:10 +09003368func (module *sdkLibraryXml) GenerateAndroidBuildActions(ctx android.ModuleContext) {
Colin Crossff694a82023-12-13 15:54:49 -08003369 apexInfo, _ := android.ModuleProvider(ctx, android.ApexInfoProvider)
3370 module.hideApexVariantFromMake = !apexInfo.IsForPlatform()
Colin Cross56a83212020-09-15 18:30:11 -07003371
Jiyong Parke3833882020-02-17 17:28:10 +09003372 libName := proptools.String(module.properties.Lib_name)
Pedro Loureiroc3621422021-09-28 15:40:23 +00003373 module.selfValidate(ctx)
Pedro Loureiro9956e5e2021-09-07 17:21:59 +00003374 xmlContent := module.permissionsContents(ctx)
Jiyong Parke3833882020-02-17 17:28:10 +09003375
3376 module.outputFilePath = android.PathForModuleOut(ctx, libName+".xml").OutputPath
Paul Duffin1816cde2024-04-10 10:58:21 +01003377 android.WriteFileRuleVerbatim(ctx, module.outputFilePath, xmlContent)
Jiyong Parke3833882020-02-17 17:28:10 +09003378
3379 module.installDirPath = android.PathForModuleInstall(ctx, "etc", module.SubDir())
Jeongik Cha00e09912024-04-23 05:07:13 +09003380 ctx.PackageFile(module.installDirPath, libName+".xml", module.outputFilePath)
Jiyong Parke3833882020-02-17 17:28:10 +09003381}
3382
3383func (module *sdkLibraryXml) AndroidMkEntries() []android.AndroidMkEntries {
Colin Cross56a83212020-09-15 18:30:11 -07003384 if module.hideApexVariantFromMake {
satayev8f088b02021-12-06 11:40:46 +00003385 return []android.AndroidMkEntries{{
Jiyong Parke3833882020-02-17 17:28:10 +09003386 Disabled: true,
3387 }}
3388 }
3389
satayev8f088b02021-12-06 11:40:46 +00003390 return []android.AndroidMkEntries{{
Jiyong Parke3833882020-02-17 17:28:10 +09003391 Class: "ETC",
3392 OutputFile: android.OptionalPathForPath(module.outputFilePath),
3393 ExtraEntries: []android.AndroidMkExtraEntriesFunc{
Colin Crossaa255532020-07-03 13:18:24 -07003394 func(ctx android.AndroidMkExtraEntriesContext, entries *android.AndroidMkEntries) {
Jiyong Parke3833882020-02-17 17:28:10 +09003395 entries.SetString("LOCAL_MODULE_TAGS", "optional")
Colin Crossc68db4b2021-11-11 18:59:15 -08003396 entries.SetString("LOCAL_MODULE_PATH", module.installDirPath.String())
Jiyong Parke3833882020-02-17 17:28:10 +09003397 entries.SetString("LOCAL_INSTALLED_MODULE_STEM", module.outputFilePath.Base())
3398 },
3399 },
3400 }}
3401}
Paul Duffindd46f712020-02-10 13:37:10 +00003402
Pedro Loureiroc3621422021-09-28 15:40:23 +00003403func (module *sdkLibraryXml) selfValidate(ctx android.ModuleContext) {
3404 module.validateAtLeastTAttributes(ctx)
3405 module.validateMinAndMaxDeviceSdk(ctx)
3406 module.validateMinMaxDeviceSdkAndModuleMinSdk(ctx)
3407 module.validateOnBootclasspathBeforeRequirements(ctx)
3408}
3409
3410func (module *sdkLibraryXml) validateAtLeastTAttributes(ctx android.ModuleContext) {
3411 t := android.ApiLevelOrPanic(ctx, "Tiramisu")
3412 module.attrAtLeastT(ctx, t, module.properties.Min_device_sdk, "min_device_sdk")
3413 module.attrAtLeastT(ctx, t, module.properties.Max_device_sdk, "max_device_sdk")
3414 module.attrAtLeastT(ctx, t, module.properties.On_bootclasspath_before, "on_bootclasspath_before")
3415 module.attrAtLeastT(ctx, t, module.properties.On_bootclasspath_since, "on_bootclasspath_since")
3416}
3417
3418func (module *sdkLibraryXml) attrAtLeastT(ctx android.ModuleContext, t android.ApiLevel, attr *string, attrName string) {
3419 if attr != nil {
3420 if level, err := android.ApiLevelFromUser(ctx, *attr); err == nil {
3421 // we will inform the user of invalid inputs when we try to write the
3422 // permissions xml file so we don't need to do it here
3423 if t.GreaterThan(level) {
3424 ctx.PropertyErrorf(attrName, "Attribute value needs to be at least T")
3425 }
3426 }
3427 }
3428}
3429
3430func (module *sdkLibraryXml) validateMinAndMaxDeviceSdk(ctx android.ModuleContext) {
3431 if module.properties.Min_device_sdk != nil && module.properties.Max_device_sdk != nil {
3432 min, minErr := android.ApiLevelFromUser(ctx, *module.properties.Min_device_sdk)
3433 max, maxErr := android.ApiLevelFromUser(ctx, *module.properties.Max_device_sdk)
3434 if minErr == nil && maxErr == nil {
3435 // we will inform the user of invalid inputs when we try to write the
3436 // permissions xml file so we don't need to do it here
3437 if min.GreaterThan(max) {
3438 ctx.ModuleErrorf("min_device_sdk can't be greater than max_device_sdk")
3439 }
3440 }
3441 }
3442}
3443
3444func (module *sdkLibraryXml) validateMinMaxDeviceSdkAndModuleMinSdk(ctx android.ModuleContext) {
3445 moduleMinApi := android.ApiLevelOrPanic(ctx, *module.properties.Sdk_library_min_api_level)
3446 if module.properties.Min_device_sdk != nil {
3447 api, err := android.ApiLevelFromUser(ctx, *module.properties.Min_device_sdk)
3448 if err == nil {
3449 if moduleMinApi.GreaterThan(api) {
3450 ctx.PropertyErrorf("min_device_sdk", "Can't be less than module's min sdk (%s)", moduleMinApi)
3451 }
3452 }
3453 }
3454 if module.properties.Max_device_sdk != nil {
3455 api, err := android.ApiLevelFromUser(ctx, *module.properties.Max_device_sdk)
3456 if err == nil {
3457 if moduleMinApi.GreaterThan(api) {
3458 ctx.PropertyErrorf("max_device_sdk", "Can't be less than module's min sdk (%s)", moduleMinApi)
3459 }
3460 }
3461 }
3462}
3463
3464func (module *sdkLibraryXml) validateOnBootclasspathBeforeRequirements(ctx android.ModuleContext) {
3465 moduleMinApi := android.ApiLevelOrPanic(ctx, *module.properties.Sdk_library_min_api_level)
3466 if module.properties.On_bootclasspath_before != nil {
3467 t := android.ApiLevelOrPanic(ctx, "Tiramisu")
3468 // if we use the attribute, then we need to do this validation
3469 if moduleMinApi.LessThan(t) {
3470 // if minAPi is < T, then we need to have min_device_sdk (which only accepts T+)
3471 if module.properties.Min_device_sdk == nil {
3472 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")
3473 }
3474 }
3475 }
3476}
3477
Paul Duffindd46f712020-02-10 13:37:10 +00003478type sdkLibrarySdkMemberType struct {
3479 android.SdkMemberTypeBase
3480}
3481
Paul Duffin296701e2021-07-14 10:29:36 +01003482func (s *sdkLibrarySdkMemberType) AddDependencies(ctx android.SdkDependencyContext, dependencyTag blueprint.DependencyTag, names []string) {
3483 ctx.AddVariationDependencies(nil, dependencyTag, names...)
Paul Duffindd46f712020-02-10 13:37:10 +00003484}
3485
3486func (s *sdkLibrarySdkMemberType) IsInstance(module android.Module) bool {
3487 _, ok := module.(*SdkLibrary)
3488 return ok
3489}
3490
3491func (s *sdkLibrarySdkMemberType) AddPrebuiltModule(ctx android.SdkMemberContext, member android.SdkMember) android.BpModule {
3492 return ctx.SnapshotBuilder().AddPrebuiltModule(member, "java_sdk_library_import")
3493}
3494
3495func (s *sdkLibrarySdkMemberType) CreateVariantPropertiesStruct() android.SdkMemberProperties {
3496 return &sdkLibrarySdkMemberProperties{}
3497}
3498
Paul Duffin976b0e52021-04-27 23:20:26 +01003499var javaSdkLibrarySdkMemberType = &sdkLibrarySdkMemberType{
3500 android.SdkMemberTypeBase{
3501 PropertyName: "java_sdk_libs",
3502 SupportsSdk: true,
3503 },
3504}
3505
Paul Duffindd46f712020-02-10 13:37:10 +00003506type sdkLibrarySdkMemberProperties struct {
3507 android.SdkMemberPropertiesBase
3508
Paul Duffine8409952022-09-22 16:24:46 +01003509 // Stem name for files in the sdk snapshot.
3510 //
3511 // This is used to construct the path names of various sdk library files in the sdk snapshot to
3512 // make sure that they match the finalized versions of those files in prebuilts/sdk.
3513 //
3514 // This property is marked as keep so that it will be kept in all instances of this struct, will
3515 // not be cleared but will be copied to common structs. That is needed because this field is used
3516 // to construct many file names for other parts of this struct and so it needs to be present in
3517 // all structs. If it was not marked as keep then it would be cleared in some structs and so would
3518 // be unavailable for generating file names if there were other properties that were still set.
3519 Stem string `sdk:"keep"`
3520
Paul Duffindd46f712020-02-10 13:37:10 +00003521 // Scope to per scope properties.
Paul Duffin106a3a42022-01-27 16:39:06 +00003522 Scopes map[*apiScope]*scopeProperties
Paul Duffindd46f712020-02-10 13:37:10 +00003523
Paul Duffin3d1248c2020-04-09 00:10:17 +01003524 // The Java stubs source files.
3525 Stub_srcs []string
Paul Duffinf7a64332020-05-13 16:54:55 +01003526
3527 // The naming scheme.
3528 Naming_scheme *string
Paul Duffind7eb1c22020-05-26 20:57:10 +01003529
3530 // True if the java_sdk_library_import is for a shared library, false
3531 // otherwise.
3532 Shared_library *bool
Paul Duffina2ae7e02020-09-11 11:55:00 +01003533
Paul Duffin1267d872021-04-16 17:21:36 +01003534 // True if the stub imports should produce dex jars.
3535 Compile_dex *bool
3536
Paul Duffina2ae7e02020-09-11 11:55:00 +01003537 // The paths to the doctag files to add to the prebuilt.
3538 Doctag_paths android.Paths
Paul Duffin869de142021-07-15 14:14:41 +01003539
3540 Permitted_packages []string
Pedro Loureiro9956e5e2021-09-07 17:21:59 +00003541
3542 // Signals that this shared library is part of the bootclasspath starting
3543 // on the version indicated in this attribute.
3544 //
3545 // This will make platforms at this level and above to ignore
3546 // <uses-library> tags with this library name because the library is already
3547 // available
3548 On_bootclasspath_since *string
3549
3550 // Signals that this shared library was part of the bootclasspath before
3551 // (but not including) the version indicated in this attribute.
3552 //
3553 // The system will automatically add a <uses-library> tag with this library to
3554 // apps that target any SDK less than the version indicated in this attribute.
3555 On_bootclasspath_before *string
3556
3557 // Indicates that PackageManager should ignore this shared library if the
3558 // platform is below the version indicated in this attribute.
3559 //
3560 // This means that the device won't recognise this library as installed.
3561 Min_device_sdk *string
3562
3563 // Indicates that PackageManager should ignore this shared library if the
3564 // platform is above the version indicated in this attribute.
3565 //
3566 // This means that the device won't recognise this library as installed.
3567 Max_device_sdk *string
Jiakai Zhang9c4dc192023-02-09 00:09:24 +08003568
3569 DexPreoptProfileGuided *bool `supported_build_releases:"UpsideDownCake+"`
Paul Duffindd46f712020-02-10 13:37:10 +00003570}
3571
3572type scopeProperties struct {
Paul Duffin1fd005d2020-04-09 01:08:11 +01003573 Jars android.Paths
3574 StubsSrcJar android.Path
3575 CurrentApiFile android.Path
3576 RemovedApiFile android.Path
Paul Duffine7babdb2022-02-10 13:06:54 +00003577 AnnotationsZip android.Path `supported_build_releases:"Tiramisu+"`
Paul Duffin1fd005d2020-04-09 01:08:11 +01003578 SdkVersion string
Paul Duffindd46f712020-02-10 13:37:10 +00003579}
3580
3581func (s *sdkLibrarySdkMemberProperties) PopulateFromVariant(ctx android.SdkMemberContext, variant android.Module) {
3582 sdk := variant.(*SdkLibrary)
3583
Paul Duffine8409952022-09-22 16:24:46 +01003584 // Copy the stem name for files in the sdk snapshot.
3585 s.Stem = sdk.distStem()
3586
Paul Duffin106a3a42022-01-27 16:39:06 +00003587 s.Scopes = make(map[*apiScope]*scopeProperties)
Paul Duffindd46f712020-02-10 13:37:10 +00003588 for _, apiScope := range allApiScopes {
Paul Duffin803a9562020-05-20 11:52:25 +01003589 paths := sdk.findScopePaths(apiScope)
3590 if paths == nil {
3591 continue
3592 }
3593
Paul Duffindd46f712020-02-10 13:37:10 +00003594 jars := paths.stubsImplPath
3595 if len(jars) > 0 {
3596 properties := scopeProperties{}
3597 properties.Jars = jars
Paul Duffin780c5f42020-05-12 15:52:55 +01003598 properties.SdkVersion = sdk.sdkVersionForStubsLibrary(ctx.SdkModuleContext(), apiScope)
Paul Duffin0f8faff2020-05-20 16:18:00 +01003599 properties.StubsSrcJar = paths.stubsSrcJar.Path()
Paul Duffin10269f12020-06-19 18:39:55 +01003600 if paths.currentApiFilePath.Valid() {
3601 properties.CurrentApiFile = paths.currentApiFilePath.Path()
3602 }
3603 if paths.removedApiFilePath.Valid() {
3604 properties.RemovedApiFile = paths.removedApiFilePath.Path()
3605 }
Anton Hanssond78eb762021-09-21 15:25:12 +01003606 // The annotations zip is only available for modules that set annotations_enabled: true.
3607 if paths.annotationsZip.Valid() {
3608 properties.AnnotationsZip = paths.annotationsZip.Path()
3609 }
Paul Duffin106a3a42022-01-27 16:39:06 +00003610 s.Scopes[apiScope] = &properties
Paul Duffindd46f712020-02-10 13:37:10 +00003611 }
3612 }
3613
Paul Duffindfa131e2020-05-15 20:37:11 +01003614 s.Naming_scheme = sdk.commonSdkLibraryProperties.Naming_scheme
Paul Duffind7eb1c22020-05-26 20:57:10 +01003615 s.Shared_library = proptools.BoolPtr(sdk.sharedLibrary())
Paul Duffin1267d872021-04-16 17:21:36 +01003616 s.Compile_dex = sdk.dexProperties.Compile_dex
Paul Duffina2ae7e02020-09-11 11:55:00 +01003617 s.Doctag_paths = sdk.doctagPaths
Paul Duffin869de142021-07-15 14:14:41 +01003618 s.Permitted_packages = sdk.PermittedPackagesForUpdatableBootJars()
Pedro Loureiro9956e5e2021-09-07 17:21:59 +00003619 s.On_bootclasspath_since = sdk.commonSdkLibraryProperties.On_bootclasspath_since
3620 s.On_bootclasspath_before = sdk.commonSdkLibraryProperties.On_bootclasspath_before
3621 s.Min_device_sdk = sdk.commonSdkLibraryProperties.Min_device_sdk
3622 s.Max_device_sdk = sdk.commonSdkLibraryProperties.Max_device_sdk
Jiakai Zhang9c4dc192023-02-09 00:09:24 +08003623
Jihoon Kanga3a05462024-04-05 00:36:44 +00003624 implLibrary := sdk.getImplLibraryModule()
3625 if implLibrary != nil && implLibrary.dexpreopter.dexpreoptProperties.Dex_preopt_result.Profile_guided {
Jiakai Zhang9c4dc192023-02-09 00:09:24 +08003626 s.DexPreoptProfileGuided = proptools.BoolPtr(true)
3627 }
Paul Duffindd46f712020-02-10 13:37:10 +00003628}
3629
3630func (s *sdkLibrarySdkMemberProperties) AddToPropertySet(ctx android.SdkMemberContext, propertySet android.BpPropertySet) {
Paul Duffinf7a64332020-05-13 16:54:55 +01003631 if s.Naming_scheme != nil {
3632 propertySet.AddProperty("naming_scheme", proptools.String(s.Naming_scheme))
3633 }
Paul Duffind7eb1c22020-05-26 20:57:10 +01003634 if s.Shared_library != nil {
3635 propertySet.AddProperty("shared_library", *s.Shared_library)
3636 }
Paul Duffin1267d872021-04-16 17:21:36 +01003637 if s.Compile_dex != nil {
3638 propertySet.AddProperty("compile_dex", *s.Compile_dex)
3639 }
Paul Duffin869de142021-07-15 14:14:41 +01003640 if len(s.Permitted_packages) > 0 {
3641 propertySet.AddProperty("permitted_packages", s.Permitted_packages)
3642 }
Jiakai Zhang9c4dc192023-02-09 00:09:24 +08003643 dexPreoptSet := propertySet.AddPropertySet("dex_preopt")
3644 if s.DexPreoptProfileGuided != nil {
3645 dexPreoptSet.AddProperty("profile_guided", proptools.Bool(s.DexPreoptProfileGuided))
3646 }
Paul Duffinf7a64332020-05-13 16:54:55 +01003647
Paul Duffine8409952022-09-22 16:24:46 +01003648 stem := s.Stem
3649
Paul Duffindd46f712020-02-10 13:37:10 +00003650 for _, apiScope := range allApiScopes {
3651 if properties, ok := s.Scopes[apiScope]; ok {
Paul Duffin6b836ba2020-05-13 19:19:49 +01003652 scopeSet := propertySet.AddPropertySet(apiScope.propertyName)
Paul Duffindd46f712020-02-10 13:37:10 +00003653
Paul Duffin958806b2022-05-16 13:10:47 +00003654 scopeDir := apiScope.snapshotRelativeDir()
Paul Duffin3d1248c2020-04-09 00:10:17 +01003655
Paul Duffindd46f712020-02-10 13:37:10 +00003656 var jars []string
3657 for _, p := range properties.Jars {
Paul Duffine8409952022-09-22 16:24:46 +01003658 dest := filepath.Join(scopeDir, stem+"-stubs.jar")
Paul Duffindd46f712020-02-10 13:37:10 +00003659 ctx.SnapshotBuilder().CopyToSnapshot(p, dest)
3660 jars = append(jars, dest)
3661 }
3662 scopeSet.AddProperty("jars", jars)
3663
Paul Duffin22628d52021-05-12 23:13:22 +01003664 if ctx.SdkModuleContext().Config().IsEnvTrue("SOONG_SDK_SNAPSHOT_USE_SRCJAR") {
3665 // Copy the stubs source jar into the snapshot zip as is.
Paul Duffine8409952022-09-22 16:24:46 +01003666 srcJarSnapshotPath := filepath.Join(scopeDir, stem+".srcjar")
Paul Duffin22628d52021-05-12 23:13:22 +01003667 ctx.SnapshotBuilder().CopyToSnapshot(properties.StubsSrcJar, srcJarSnapshotPath)
3668 scopeSet.AddProperty("stub_srcs", []string{srcJarSnapshotPath})
3669 } else {
3670 // Merge the stubs source jar into the snapshot zip so that when it is unpacked
3671 // the source files are also unpacked.
Paul Duffine8409952022-09-22 16:24:46 +01003672 snapshotRelativeDir := filepath.Join(scopeDir, stem+"_stub_sources")
Paul Duffin22628d52021-05-12 23:13:22 +01003673 ctx.SnapshotBuilder().UnzipToSnapshot(properties.StubsSrcJar, snapshotRelativeDir)
3674 scopeSet.AddProperty("stub_srcs", []string{snapshotRelativeDir})
3675 }
Paul Duffin3d1248c2020-04-09 00:10:17 +01003676
Paul Duffin1fd005d2020-04-09 01:08:11 +01003677 if properties.CurrentApiFile != nil {
Paul Duffine8409952022-09-22 16:24:46 +01003678 currentApiSnapshotPath := apiScope.snapshotRelativeCurrentApiTxtPath(stem)
Paul Duffin1fd005d2020-04-09 01:08:11 +01003679 ctx.SnapshotBuilder().CopyToSnapshot(properties.CurrentApiFile, currentApiSnapshotPath)
3680 scopeSet.AddProperty("current_api", currentApiSnapshotPath)
3681 }
3682
3683 if properties.RemovedApiFile != nil {
Paul Duffine8409952022-09-22 16:24:46 +01003684 removedApiSnapshotPath := apiScope.snapshotRelativeRemovedApiTxtPath(stem)
Paul Duffin3dbf9fd2020-06-02 13:00:02 +01003685 ctx.SnapshotBuilder().CopyToSnapshot(properties.RemovedApiFile, removedApiSnapshotPath)
Paul Duffin1fd005d2020-04-09 01:08:11 +01003686 scopeSet.AddProperty("removed_api", removedApiSnapshotPath)
3687 }
3688
Anton Hanssond78eb762021-09-21 15:25:12 +01003689 if properties.AnnotationsZip != nil {
Paul Duffine8409952022-09-22 16:24:46 +01003690 annotationsSnapshotPath := filepath.Join(scopeDir, stem+"_annotations.zip")
Anton Hanssond78eb762021-09-21 15:25:12 +01003691 ctx.SnapshotBuilder().CopyToSnapshot(properties.AnnotationsZip, annotationsSnapshotPath)
3692 scopeSet.AddProperty("annotations", annotationsSnapshotPath)
3693 }
3694
Paul Duffindd46f712020-02-10 13:37:10 +00003695 if properties.SdkVersion != "" {
3696 scopeSet.AddProperty("sdk_version", properties.SdkVersion)
3697 }
3698 }
3699 }
3700
Paul Duffina2ae7e02020-09-11 11:55:00 +01003701 if len(s.Doctag_paths) > 0 {
3702 dests := []string{}
3703 for _, p := range s.Doctag_paths {
3704 dest := filepath.Join("doctags", p.Rel())
3705 ctx.SnapshotBuilder().CopyToSnapshot(p, dest)
3706 dests = append(dests, dest)
3707 }
3708 propertySet.AddProperty("doctag_files", dests)
3709 }
Paul Duffindd46f712020-02-10 13:37:10 +00003710}