blob: 5ddc6751cca96615bbec83e59138ecc654021fe7 [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.
708 latestApiPath android.OptionalPath
709
710 // The path to the latest removed API file.
711 latestRemovedApiPath android.OptionalPath
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
Paul Duffin958806b2022-05-16 13:10:47 +0000832func extractSingleOptionalOutputPath(dep android.Module) (android.OptionalPath, error) {
833 var paths android.Paths
834 if sourceFileProducer, ok := dep.(android.SourceFileProducer); ok {
835 paths = sourceFileProducer.Srcs()
836 } else {
837 return android.OptionalPath{}, fmt.Errorf("module %q does not produce source files", dep)
838 }
839 if len(paths) != 1 {
840 return android.OptionalPath{}, fmt.Errorf("expected one path from %q, got %q", dep, paths)
841 }
842 return android.OptionalPathForPath(paths[0]), nil
843}
844
845func (paths *scopePaths) extractLatestApiPath(ctx android.ModuleContext, dep android.Module) error {
846 outputPath, err := extractSingleOptionalOutputPath(dep)
847 paths.latestApiPath = outputPath
848 return err
849}
850
851func (paths *scopePaths) extractLatestRemovedApiPath(ctx android.ModuleContext, dep android.Module) error {
852 outputPath, err := extractSingleOptionalOutputPath(dep)
853 paths.latestRemovedApiPath = outputPath
854 return err
855}
856
Paul Duffin0ff08bd2020-04-29 13:30:54 +0100857type commonToSdkLibraryAndImportProperties struct {
Paul Duffin1b1e8062020-05-08 13:44:43 +0100858 // The naming scheme to use for the components that this module creates.
859 //
Paul Duffinee9ad5d2020-09-11 13:04:05 +0100860 // If not specified then it defaults to "default".
Paul Duffin1b1e8062020-05-08 13:44:43 +0100861 //
862 // This is a temporary mechanism to simplify conversion from separate modules for each
863 // component that follow a different naming pattern to the default one.
864 //
865 // TODO(b/155480189) - Remove once naming inconsistencies have been resolved.
Paul Duffin0ff08bd2020-04-29 13:30:54 +0100866 Naming_scheme *string
Paul Duffindfa131e2020-05-15 20:37:11 +0100867
868 // Specifies whether this module can be used as an Android shared library; defaults
869 // to true.
870 //
871 // An Android shared library is one that can be referenced in a <uses-library> element
872 // in an AndroidManifest.xml.
873 Shared_library *bool
Paul Duffina2ae7e02020-09-11 11:55:00 +0100874
875 // Files containing information about supported java doc tags.
876 Doctag_files []string `android:"path"`
Pedro Loureiro9956e5e2021-09-07 17:21:59 +0000877
878 // Signals that this shared library is part of the bootclasspath starting
879 // on the version indicated in this attribute.
880 //
881 // This will make platforms at this level and above to ignore
882 // <uses-library> tags with this library name because the library is already
883 // available
884 On_bootclasspath_since *string
885
886 // Signals that this shared library was part of the bootclasspath before
887 // (but not including) the version indicated in this attribute.
888 //
889 // The system will automatically add a <uses-library> tag with this library to
890 // apps that target any SDK less than the version indicated in this attribute.
891 On_bootclasspath_before *string
892
893 // Indicates that PackageManager should ignore this shared library if the
894 // platform is below the version indicated in this attribute.
895 //
896 // This means that the device won't recognise this library as installed.
897 Min_device_sdk *string
898
899 // Indicates that PackageManager should ignore this shared library if the
900 // platform is above the version indicated in this attribute.
901 //
902 // This means that the device won't recognise this library as installed.
903 Max_device_sdk *string
Paul Duffin0ff08bd2020-04-29 13:30:54 +0100904}
905
Paul Duffin71b33cc2021-06-23 11:39:47 +0100906// commonSdkLibraryAndImportModule defines the interface that must be provided by a module that
907// embeds the commonToSdkLibraryAndImport struct.
908type commonSdkLibraryAndImportModule interface {
Paul Duffind796f6f2022-11-23 23:06:05 +0000909 android.Module
Paul Duffin71b33cc2021-06-23 11:39:47 +0100910
Spandan Das23956d12024-01-19 00:22:22 +0000911 // Returns the name of the root java_sdk_library that creates the child stub libraries
912 // This is the `name` as it appears in Android.bp, and not the name in Soong's build graph
913 // (with the prebuilt_ prefix)
914 //
915 // e.g. in the following java_sdk_library_import
916 // java_sdk_library_import {
917 // name: "framework-foo.v1",
918 // source_module_name: "framework-foo",
919 // }
920 // the values returned by
921 // 1. Name(): prebuilt_framework-foo.v1 # unique
922 // 2. BaseModuleName(): framework-foo # the source
923 // 3. RootLibraryName: framework-foo.v1 # the undecordated `name` from Android.bp
924 RootLibraryName() string
925}
926
927func (m *SdkLibrary) RootLibraryName() string {
928 return m.BaseModuleName()
929}
930
931func (m *SdkLibraryImport) RootLibraryName() string {
932 // m.BaseModuleName refers to the source of the import
933 // use moduleBase.Name to get the name of the module as it appears in the .bp file
934 return m.ModuleBase.Name()
Paul Duffin71b33cc2021-06-23 11:39:47 +0100935}
936
Paul Duffin56d44902020-01-31 13:36:25 +0000937// Common code between sdk library and sdk library import
938type commonToSdkLibraryAndImport struct {
Paul Duffin71b33cc2021-06-23 11:39:47 +0100939 module commonSdkLibraryAndImportModule
Paul Duffinc3091c82020-05-08 14:16:20 +0100940
Paul Duffin56d44902020-01-31 13:36:25 +0000941 scopePaths map[*apiScope]*scopePaths
Paul Duffin1b1e8062020-05-08 13:44:43 +0100942
943 namingScheme sdkLibraryComponentNamingScheme
944
Paul Duffindfa131e2020-05-15 20:37:11 +0100945 commonSdkLibraryProperties commonToSdkLibraryAndImportProperties
Paul Duffin859fe962020-05-15 10:20:31 +0100946
Paul Duffina2ae7e02020-09-11 11:55:00 +0100947 // Paths to commonSdkLibraryProperties.Doctag_files
948 doctagPaths android.Paths
949
Paul Duffin859fe962020-05-15 10:20:31 +0100950 // Functionality related to this being used as a component of a java_sdk_library.
951 EmbeddableSdkLibraryComponent
Paul Duffin56d44902020-01-31 13:36:25 +0000952}
953
Paul Duffin71b33cc2021-06-23 11:39:47 +0100954func (c *commonToSdkLibraryAndImport) initCommon(module commonSdkLibraryAndImportModule) {
955 c.module = module
Paul Duffin1b1e8062020-05-08 13:44:43 +0100956
Paul Duffin71b33cc2021-06-23 11:39:47 +0100957 module.AddProperties(&c.commonSdkLibraryProperties)
Paul Duffin859fe962020-05-15 10:20:31 +0100958
959 // Initialize this as an sdk library component.
Paul Duffin71b33cc2021-06-23 11:39:47 +0100960 c.initSdkLibraryComponent(module)
Paul Duffin1b1e8062020-05-08 13:44:43 +0100961}
962
963func (c *commonToSdkLibraryAndImport) initCommonAfterDefaultsApplied(ctx android.DefaultableHookContext) bool {
Paul Duffindfa131e2020-05-15 20:37:11 +0100964 schemeProperty := proptools.StringDefault(c.commonSdkLibraryProperties.Naming_scheme, "default")
Paul Duffin1b1e8062020-05-08 13:44:43 +0100965 switch schemeProperty {
966 case "default":
967 c.namingScheme = &defaultNamingScheme{}
968 default:
969 ctx.PropertyErrorf("naming_scheme", "expected 'default' but was %q", schemeProperty)
970 return false
971 }
972
Spandan Das23956d12024-01-19 00:22:22 +0000973 namePtr := proptools.StringPtr(c.module.RootLibraryName())
Paul Duffin3f0290e2021-06-30 18:25:36 +0100974 c.sdkLibraryComponentProperties.SdkLibraryName = namePtr
975
Paul Duffindfa131e2020-05-15 20:37:11 +0100976 // Only track this sdk library if this can be used as a shared library.
977 if c.sharedLibrary() {
978 // Use the name specified in the module definition as the owner.
Paul Duffin3f0290e2021-06-30 18:25:36 +0100979 c.sdkLibraryComponentProperties.SdkLibraryToImplicitlyTrack = namePtr
Paul Duffindfa131e2020-05-15 20:37:11 +0100980 }
Paul Duffin859fe962020-05-15 10:20:31 +0100981
Paul Duffin1b1e8062020-05-08 13:44:43 +0100982 return true
Paul Duffinc3091c82020-05-08 14:16:20 +0100983}
984
Paul Duffinea8f8082021-06-24 13:25:57 +0100985// uniqueApexVariations provides common implementation of the ApexModule.UniqueApexVariations
986// method.
987func (c *commonToSdkLibraryAndImport) uniqueApexVariations() bool {
988 // A java_sdk_library that is a shared library produces an XML file that makes the shared library
989 // usable from an AndroidManifest.xml's <uses-library> entry. That XML file contains the name of
990 // the APEX and so it needs a unique variation per APEX.
991 return c.sharedLibrary()
992}
993
Paul Duffina2ae7e02020-09-11 11:55:00 +0100994func (c *commonToSdkLibraryAndImport) generateCommonBuildActions(ctx android.ModuleContext) {
995 c.doctagPaths = android.PathsForModuleSrc(ctx, c.commonSdkLibraryProperties.Doctag_files)
996}
997
Paul Duffineedc5d52020-06-12 17:46:39 +0100998// Module name of the runtime implementation library
999func (c *commonToSdkLibraryAndImport) implLibraryModuleName() string {
Spandan Das23956d12024-01-19 00:22:22 +00001000 return c.module.RootLibraryName() + ".impl"
Paul Duffineedc5d52020-06-12 17:46:39 +01001001}
1002
1003// Module name of the XML file for the lib
1004func (c *commonToSdkLibraryAndImport) xmlPermissionsModuleName() string {
Spandan Das23956d12024-01-19 00:22:22 +00001005 return c.module.RootLibraryName() + sdkXmlFileSuffix
Paul Duffineedc5d52020-06-12 17:46:39 +01001006}
1007
Paul Duffinc3091c82020-05-08 14:16:20 +01001008// Name of the java_library module that compiles the stubs source.
1009func (c *commonToSdkLibraryAndImport) stubsLibraryModuleName(apiScope *apiScope) string {
Spandan Das23956d12024-01-19 00:22:22 +00001010 baseName := c.module.RootLibraryName()
Paul Duffin21787622022-11-25 12:48:20 +00001011 return c.namingScheme.stubsLibraryModuleName(apiScope, baseName)
Paul Duffinc3091c82020-05-08 14:16:20 +01001012}
1013
Jihoon Kangfa4a90d2023-12-20 02:53:38 +00001014// Name of the java_library module that compiles the exportable stubs source.
1015func (c *commonToSdkLibraryAndImport) exportableStubsLibraryModuleName(apiScope *apiScope) string {
Spandan Das23956d12024-01-19 00:22:22 +00001016 baseName := c.module.RootLibraryName()
Jihoon Kangfa4a90d2023-12-20 02:53:38 +00001017 return c.namingScheme.exportableStubsLibraryModuleName(apiScope, baseName)
1018}
1019
Paul Duffinc3091c82020-05-08 14:16:20 +01001020// Name of the droidstubs module that generates the stubs source and may also
1021// generate/check the API.
1022func (c *commonToSdkLibraryAndImport) stubsSourceModuleName(apiScope *apiScope) string {
Spandan Das23956d12024-01-19 00:22:22 +00001023 baseName := c.module.RootLibraryName()
Paul Duffin21787622022-11-25 12:48:20 +00001024 return c.namingScheme.stubsSourceModuleName(apiScope, baseName)
Paul Duffinc3091c82020-05-08 14:16:20 +01001025}
1026
Jihoon Kang1c92c3e2023-03-23 17:44:51 +00001027// Name of the java_api_library module that generates the from-text stubs source
1028// and compiles to a jar file.
1029func (c *commonToSdkLibraryAndImport) apiLibraryModuleName(apiScope *apiScope) string {
Spandan Das23956d12024-01-19 00:22:22 +00001030 baseName := c.module.RootLibraryName()
Jihoon Kang1c92c3e2023-03-23 17:44:51 +00001031 return c.namingScheme.apiLibraryModuleName(apiScope, baseName)
1032}
1033
Jihoon Kang1147b312023-06-08 23:25:57 +00001034// Name of the java_library module that compiles the stubs
1035// generated from source Java files.
Jihoon Kangfa4a90d2023-12-20 02:53:38 +00001036func (c *commonToSdkLibraryAndImport) sourceStubsLibraryModuleName(apiScope *apiScope) string {
Spandan Das23956d12024-01-19 00:22:22 +00001037 baseName := c.module.RootLibraryName()
Jihoon Kangfa4a90d2023-12-20 02:53:38 +00001038 return c.namingScheme.sourceStubsLibraryModuleName(apiScope, baseName)
1039}
1040
1041// Name of the java_library module that compiles the exportable stubs
1042// generated from source Java files.
1043func (c *commonToSdkLibraryAndImport) exportableSourceStubsLibraryModuleName(apiScope *apiScope) string {
Spandan Das23956d12024-01-19 00:22:22 +00001044 baseName := c.module.RootLibraryName()
Jihoon Kangfa4a90d2023-12-20 02:53:38 +00001045 return c.namingScheme.exportableSourceStubsLibraryModuleName(apiScope, baseName)
Jihoon Kang1147b312023-06-08 23:25:57 +00001046}
1047
Paul Duffin46dc45a2020-05-14 15:39:10 +01001048// The component names for different outputs of the java_sdk_library.
1049//
1050// They are similar to the names used for the child modules it creates
1051const (
1052 stubsSourceComponentName = "stubs.source"
1053
1054 apiTxtComponentName = "api.txt"
1055
1056 removedApiTxtComponentName = "removed-api.txt"
Anton Hanssond78eb762021-09-21 15:25:12 +01001057
1058 annotationsComponentName = "annotations.zip"
Paul Duffin46dc45a2020-05-14 15:39:10 +01001059)
1060
1061// A regular expression to match tags that reference a specific stubs component.
1062//
1063// It will only match if given a valid scope and a valid component. It is verfy strict
1064// to ensure it does not accidentally match a similar looking tag that should be processed
1065// by the embedded Library.
1066var tagSplitter = func() *regexp.Regexp {
1067 // Given a list of literal string items returns a regular expression that will
1068 // match any one of the items.
1069 choice := func(items ...string) string {
1070 return `\Q` + strings.Join(items, `\E|\Q`) + `\E`
1071 }
1072
1073 // Regular expression to match one of the scopes.
1074 scopesRegexp := choice(allScopeNames...)
1075
1076 // Regular expression to match one of the components.
Anton Hanssond78eb762021-09-21 15:25:12 +01001077 componentsRegexp := choice(stubsSourceComponentName, apiTxtComponentName, removedApiTxtComponentName, annotationsComponentName)
Paul Duffin46dc45a2020-05-14 15:39:10 +01001078
1079 // Regular expression to match any combination of one scope and one component.
1080 return regexp.MustCompile(fmt.Sprintf(`^\.(%s)\.(%s)$`, scopesRegexp, componentsRegexp))
1081}()
1082
1083// For OutputFileProducer interface
1084//
Anton Hanssond78eb762021-09-21 15:25:12 +01001085// .<scope>.<component name>, for all ComponentNames (for example: .public.removed-api.txt)
Paul Duffin46dc45a2020-05-14 15:39:10 +01001086func (c *commonToSdkLibraryAndImport) commonOutputFiles(tag string) (android.Paths, error) {
1087 if groups := tagSplitter.FindStringSubmatch(tag); groups != nil {
1088 scopeName := groups[1]
1089 component := groups[2]
1090
1091 if scope, ok := scopeByName[scopeName]; ok {
1092 paths := c.findScopePaths(scope)
1093 if paths == nil {
Spandan Das23956d12024-01-19 00:22:22 +00001094 return nil, fmt.Errorf("%q does not provide api scope %s", c.module.RootLibraryName(), scopeName)
Paul Duffin46dc45a2020-05-14 15:39:10 +01001095 }
1096
1097 switch component {
1098 case stubsSourceComponentName:
1099 if paths.stubsSrcJar.Valid() {
1100 return android.Paths{paths.stubsSrcJar.Path()}, nil
1101 }
1102
1103 case apiTxtComponentName:
1104 if paths.currentApiFilePath.Valid() {
1105 return android.Paths{paths.currentApiFilePath.Path()}, nil
1106 }
1107
1108 case removedApiTxtComponentName:
1109 if paths.removedApiFilePath.Valid() {
1110 return android.Paths{paths.removedApiFilePath.Path()}, nil
1111 }
Anton Hanssond78eb762021-09-21 15:25:12 +01001112
1113 case annotationsComponentName:
1114 if paths.annotationsZip.Valid() {
1115 return android.Paths{paths.annotationsZip.Path()}, nil
1116 }
Paul Duffin46dc45a2020-05-14 15:39:10 +01001117 }
1118
1119 return nil, fmt.Errorf("%s not available for api scope %s", component, scopeName)
1120 } else {
1121 return nil, fmt.Errorf("unknown scope %s in %s", scope, tag)
1122 }
1123
1124 } else {
Paul Duffina2ae7e02020-09-11 11:55:00 +01001125 switch tag {
1126 case ".doctags":
1127 if c.doctagPaths != nil {
1128 return c.doctagPaths, nil
1129 } else {
Spandan Das23956d12024-01-19 00:22:22 +00001130 return nil, fmt.Errorf("no doctag_files specified on %s", c.module.RootLibraryName())
Paul Duffina2ae7e02020-09-11 11:55:00 +01001131 }
1132 }
Paul Duffin46dc45a2020-05-14 15:39:10 +01001133 return nil, nil
1134 }
1135}
1136
Paul Duffin803a9562020-05-20 11:52:25 +01001137func (c *commonToSdkLibraryAndImport) getScopePathsCreateIfNeeded(scope *apiScope) *scopePaths {
Paul Duffin56d44902020-01-31 13:36:25 +00001138 if c.scopePaths == nil {
1139 c.scopePaths = make(map[*apiScope]*scopePaths)
1140 }
1141 paths := c.scopePaths[scope]
1142 if paths == nil {
1143 paths = &scopePaths{}
1144 c.scopePaths[scope] = paths
1145 }
1146
1147 return paths
1148}
1149
Paul Duffin803a9562020-05-20 11:52:25 +01001150func (c *commonToSdkLibraryAndImport) findScopePaths(scope *apiScope) *scopePaths {
1151 if c.scopePaths == nil {
1152 return nil
1153 }
1154
1155 return c.scopePaths[scope]
1156}
1157
1158// If this does not support the requested api scope then find the closest available
1159// scope it does support. Returns nil if no such scope is available.
1160func (c *commonToSdkLibraryAndImport) findClosestScopePath(scope *apiScope) *scopePaths {
Paul Duffind0b9fca2022-09-30 18:11:41 +01001161 for s := scope; s != nil; s = s.canAccess {
Paul Duffin803a9562020-05-20 11:52:25 +01001162 if paths := c.findScopePaths(s); paths != nil {
1163 return paths
1164 }
1165 }
1166
1167 // This should never happen outside tests as public should be the base scope for every
1168 // scope and is enabled by default.
1169 return nil
1170}
1171
Jiyong Parkf1691d22021-03-29 20:11:58 +09001172func (c *commonToSdkLibraryAndImport) selectHeaderJarsForSdkVersion(ctx android.BaseModuleContext, sdkVersion android.SdkSpec) android.Paths {
Paul Duffinb05d4292020-05-20 12:19:10 +01001173
1174 // If a specific numeric version has been requested then use prebuilt versions of the sdk.
Jiyong Park54105c42021-03-31 18:17:53 +09001175 if !sdkVersion.ApiLevel.IsPreview() {
Spandan Das23956d12024-01-19 00:22:22 +00001176 return PrebuiltJars(ctx, c.module.RootLibraryName(), sdkVersion)
Paul Duffinb05d4292020-05-20 12:19:10 +01001177 }
1178
Paul Duffin1267d872021-04-16 17:21:36 +01001179 paths := c.selectScopePaths(ctx, sdkVersion.Kind)
1180 if paths == nil {
1181 return nil
1182 }
1183
1184 return paths.stubsHeaderPath
1185}
1186
1187// selectScopePaths returns the *scopePaths appropriate for the specific kind.
1188//
1189// If the module does not support the specific kind then it will return the *scopePaths for the
1190// closest kind which is a subset of the requested kind. e.g. if requesting android.SdkModule then
1191// it will return *scopePaths for android.SdkSystem if available or android.SdkPublic of not.
1192func (c *commonToSdkLibraryAndImport) selectScopePaths(ctx android.BaseModuleContext, kind android.SdkKind) *scopePaths {
Paul Duffin32cf58a2021-05-18 16:32:50 +01001193 apiScope := sdkKindToApiScope(kind)
Paul Duffinb05d4292020-05-20 12:19:10 +01001194
Paul Duffin803a9562020-05-20 11:52:25 +01001195 paths := c.findClosestScopePath(apiScope)
1196 if paths == nil {
1197 var scopes []string
1198 for _, s := range allApiScopes {
1199 if c.findScopePaths(s) != nil {
1200 scopes = append(scopes, s.name)
1201 }
1202 }
Spandan Das23956d12024-01-19 00:22:22 +00001203 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 +01001204 return nil
1205 }
1206
Paul Duffin1267d872021-04-16 17:21:36 +01001207 return paths
1208}
1209
Paul Duffin32cf58a2021-05-18 16:32:50 +01001210// sdkKindToApiScope maps from android.SdkKind to apiScope.
1211func sdkKindToApiScope(kind android.SdkKind) *apiScope {
1212 var apiScope *apiScope
1213 switch kind {
1214 case android.SdkSystem:
1215 apiScope = apiScopeSystem
1216 case android.SdkModule:
1217 apiScope = apiScopeModuleLib
1218 case android.SdkTest:
1219 apiScope = apiScopeTest
1220 case android.SdkSystemServer:
1221 apiScope = apiScopeSystemServer
1222 default:
1223 apiScope = apiScopePublic
1224 }
1225 return apiScope
1226}
1227
Paul Duffin1267d872021-04-16 17:21:36 +01001228// to satisfy SdkLibraryDependency interface
Martin Stjernholm8be1e6d2021-09-15 03:34:04 +01001229func (c *commonToSdkLibraryAndImport) SdkApiStubDexJar(ctx android.BaseModuleContext, kind android.SdkKind) OptionalDexJarPath {
Paul Duffin1267d872021-04-16 17:21:36 +01001230 paths := c.selectScopePaths(ctx, kind)
1231 if paths == nil {
Martin Stjernholm8be1e6d2021-09-15 03:34:04 +01001232 return makeUnsetDexJarPath()
Paul Duffin1267d872021-04-16 17:21:36 +01001233 }
1234
1235 return paths.stubsDexJarPath
Paul Duffinb05d4292020-05-20 12:19:10 +01001236}
1237
Paul Duffin32cf58a2021-05-18 16:32:50 +01001238// to satisfy SdkLibraryDependency interface
Jihoon Kangbd093452023-12-26 19:08:01 +00001239func (c *commonToSdkLibraryAndImport) SdkApiExportableStubDexJar(ctx android.BaseModuleContext, kind android.SdkKind) OptionalDexJarPath {
1240 paths := c.selectScopePaths(ctx, kind)
1241 if paths == nil {
1242 return makeUnsetDexJarPath()
1243 }
1244
1245 return paths.exportableStubsDexJarPath
1246}
1247
1248// to satisfy SdkLibraryDependency interface
Paul Duffin32cf58a2021-05-18 16:32:50 +01001249func (c *commonToSdkLibraryAndImport) SdkRemovedTxtFile(ctx android.BaseModuleContext, kind android.SdkKind) android.OptionalPath {
1250 apiScope := sdkKindToApiScope(kind)
1251 paths := c.findScopePaths(apiScope)
1252 if paths == nil {
1253 return android.OptionalPath{}
1254 }
1255
1256 return paths.removedApiFilePath
1257}
1258
Paul Duffin859fe962020-05-15 10:20:31 +01001259func (c *commonToSdkLibraryAndImport) sdkComponentPropertiesForChildLibrary() interface{} {
1260 componentProps := &struct {
Paul Duffin3f0290e2021-06-30 18:25:36 +01001261 SdkLibraryName *string
Paul Duffin859fe962020-05-15 10:20:31 +01001262 SdkLibraryToImplicitlyTrack *string
Paul Duffindfa131e2020-05-15 20:37:11 +01001263 }{}
1264
Spandan Das23956d12024-01-19 00:22:22 +00001265 namePtr := proptools.StringPtr(c.module.RootLibraryName())
Paul Duffin3f0290e2021-06-30 18:25:36 +01001266 componentProps.SdkLibraryName = namePtr
1267
Paul Duffindfa131e2020-05-15 20:37:11 +01001268 if c.sharedLibrary() {
Paul Duffin859fe962020-05-15 10:20:31 +01001269 // Mark the stubs library as being components of this java_sdk_library so that
1270 // any app that includes code which depends (directly or indirectly) on the stubs
1271 // library will have the appropriate <uses-library> invocation inserted into its
1272 // manifest if necessary.
Paul Duffin3f0290e2021-06-30 18:25:36 +01001273 componentProps.SdkLibraryToImplicitlyTrack = namePtr
Paul Duffin859fe962020-05-15 10:20:31 +01001274 }
1275
1276 return componentProps
1277}
1278
Paul Duffindfa131e2020-05-15 20:37:11 +01001279func (c *commonToSdkLibraryAndImport) sharedLibrary() bool {
1280 return proptools.BoolDefault(c.commonSdkLibraryProperties.Shared_library, true)
1281}
1282
Paul Duffinf4600f62021-05-13 22:34:45 +01001283// Check if the stub libraries should be compiled for dex
1284func (c *commonToSdkLibraryAndImport) stubLibrariesCompiledForDex() bool {
1285 // Always compile the dex file files for the stub libraries if they will be used on the
1286 // bootclasspath.
1287 return !c.sharedLibrary()
1288}
1289
Paul Duffin859fe962020-05-15 10:20:31 +01001290// Properties related to the use of a module as an component of a java_sdk_library.
1291type SdkLibraryComponentProperties struct {
Paul Duffin3f0290e2021-06-30 18:25:36 +01001292 // The name of the java_sdk_library/_import module.
1293 SdkLibraryName *string `blueprint:"mutated"`
Paul Duffin859fe962020-05-15 10:20:31 +01001294
1295 // The name of the java_sdk_library/_import to add to a <uses-library> entry
1296 // in the AndroidManifest.xml of any Android app that includes code that references
1297 // this module. If not set then no java_sdk_library/_import is tracked.
1298 SdkLibraryToImplicitlyTrack *string `blueprint:"mutated"`
1299}
1300
1301// Structure to be embedded in a module struct that needs to support the
1302// SdkLibraryComponentDependency interface.
1303type EmbeddableSdkLibraryComponent struct {
1304 sdkLibraryComponentProperties SdkLibraryComponentProperties
1305}
1306
Paul Duffin71b33cc2021-06-23 11:39:47 +01001307func (e *EmbeddableSdkLibraryComponent) initSdkLibraryComponent(module android.Module) {
1308 module.AddProperties(&e.sdkLibraryComponentProperties)
Paul Duffin859fe962020-05-15 10:20:31 +01001309}
1310
1311// to satisfy SdkLibraryComponentDependency
Paul Duffin3f0290e2021-06-30 18:25:36 +01001312func (e *EmbeddableSdkLibraryComponent) SdkLibraryName() *string {
1313 return e.sdkLibraryComponentProperties.SdkLibraryName
1314}
1315
1316// to satisfy SdkLibraryComponentDependency
Ulya Trafimovich39b437b2020-09-23 16:42:35 +01001317func (e *EmbeddableSdkLibraryComponent) OptionalSdkLibraryImplementation() *string {
Ulya Trafimovich78645fb2021-07-16 15:29:25 +01001318 // For shared libraries, this is the same as the SDK library name. If a Java library or app
1319 // depends on a component library (e.g. a stub library) it still needs to know the name of the
1320 // run-time library and the corresponding module that provides the implementation. This name is
1321 // passed to manifest_fixer (to be added to AndroidManifest.xml) and added to CLC (to be used
1322 // in dexpreopt).
1323 //
1324 // For non-shared SDK (component or not) libraries this returns `nil`, as they are not
1325 // <uses-library> and should not be added to the manifest or to CLC.
Ulya Trafimovich39b437b2020-09-23 16:42:35 +01001326 return e.sdkLibraryComponentProperties.SdkLibraryToImplicitlyTrack
1327}
1328
Paul Duffin859fe962020-05-15 10:20:31 +01001329// Implemented by modules that are (or possibly could be) a component of a java_sdk_library
1330// (including the java_sdk_library) itself.
1331type SdkLibraryComponentDependency interface {
Ulya Trafimovich31e444e2020-08-14 17:32:16 +01001332 UsesLibraryDependency
1333
Paul Duffin3f0290e2021-06-30 18:25:36 +01001334 // SdkLibraryName returns the name of the java_sdk_library/_import module.
1335 SdkLibraryName() *string
1336
Ulya Trafimovich39b437b2020-09-23 16:42:35 +01001337 // The name of the implementation library for the optional SDK library or nil, if there isn't one.
1338 OptionalSdkLibraryImplementation() *string
Paul Duffin859fe962020-05-15 10:20:31 +01001339}
1340
1341// Make sure that all the module types that are components of java_sdk_library/_import
1342// and which can be referenced (directly or indirectly) from an android app implement
1343// the SdkLibraryComponentDependency interface.
1344var _ SdkLibraryComponentDependency = (*Library)(nil)
1345var _ SdkLibraryComponentDependency = (*Import)(nil)
1346var _ SdkLibraryComponentDependency = (*SdkLibrary)(nil)
Paul Duffineedc5d52020-06-12 17:46:39 +01001347var _ SdkLibraryComponentDependency = (*SdkLibraryImport)(nil)
Paul Duffin859fe962020-05-15 10:20:31 +01001348
Paul Duffin32cf58a2021-05-18 16:32:50 +01001349// Provides access to sdk_version related files, e.g. header and implementation jars.
Paul Duffin859fe962020-05-15 10:20:31 +01001350type SdkLibraryDependency interface {
1351 SdkLibraryComponentDependency
1352
1353 // Get the header jars appropriate for the supplied sdk_version.
1354 //
1355 // These are turbine generated jars so they only change if the externals of the
1356 // class changes but it does not contain and implementation or JavaDoc.
Jiyong Parkf1691d22021-03-29 20:11:58 +09001357 SdkHeaderJars(ctx android.BaseModuleContext, sdkVersion android.SdkSpec) android.Paths
Paul Duffin859fe962020-05-15 10:20:31 +01001358
1359 // Get the implementation jars appropriate for the supplied sdk version.
1360 //
1361 // These are either the implementation jar for the whole sdk library or the implementation
1362 // jars for the stubs. The latter should only be needed when generating JavaDoc as otherwise
1363 // they are identical to the corresponding header jars.
Jiyong Parkf1691d22021-03-29 20:11:58 +09001364 SdkImplementationJars(ctx android.BaseModuleContext, sdkVersion android.SdkSpec) android.Paths
Paul Duffin1267d872021-04-16 17:21:36 +01001365
Jihoon Kangbd093452023-12-26 19:08:01 +00001366 // SdkApiStubDexJar returns the dex jar for the stubs for the prebuilt
1367 // java_sdk_library_import module. It is needed by the hiddenapi processing tool which
1368 // processes dex files.
Martin Stjernholm8be1e6d2021-09-15 03:34:04 +01001369 SdkApiStubDexJar(ctx android.BaseModuleContext, kind android.SdkKind) OptionalDexJarPath
Paul Duffinf4600f62021-05-13 22:34:45 +01001370
Jihoon Kangbd093452023-12-26 19:08:01 +00001371 // SdkApiExportableStubDexJar returns the exportable dex jar for the stubs for
1372 // java_sdk_library module. It is needed by the hiddenapi processing tool which processes
1373 // dex files.
1374 SdkApiExportableStubDexJar(ctx android.BaseModuleContext, kind android.SdkKind) OptionalDexJarPath
1375
Paul Duffin32cf58a2021-05-18 16:32:50 +01001376 // SdkRemovedTxtFile returns the optional path to the removed.txt file for the specified sdk kind.
1377 SdkRemovedTxtFile(ctx android.BaseModuleContext, kind android.SdkKind) android.OptionalPath
1378
Paul Duffinf4600f62021-05-13 22:34:45 +01001379 // sharedLibrary returns true if this can be used as a shared library.
1380 sharedLibrary() bool
Paul Duffin859fe962020-05-15 10:20:31 +01001381}
1382
Inseob Kimc0907f12019-02-08 21:00:45 +09001383type SdkLibrary struct {
Sundong Ahn054b19a2018-10-19 13:46:09 +09001384 Library
Jiyong Parkc678ad32018-04-10 13:07:10 +09001385
Sundong Ahn054b19a2018-10-19 13:46:09 +09001386 sdkLibraryProperties sdkLibraryProperties
Jiyong Parkc678ad32018-04-10 13:07:10 +09001387
Paul Duffin3375e352020-04-28 10:44:03 +01001388 // Map from api scope to the scope specific property structure.
1389 scopeToProperties map[*apiScope]*ApiScopeProperties
1390
Paul Duffin56d44902020-01-31 13:36:25 +00001391 commonToSdkLibraryAndImport
Jiyong Parkc678ad32018-04-10 13:07:10 +09001392}
1393
Inseob Kimc0907f12019-02-08 21:00:45 +09001394var _ SdkLibraryDependency = (*SdkLibrary)(nil)
Colin Cross897d2ed2019-02-11 14:03:51 -08001395
Paul Duffin3375e352020-04-28 10:44:03 +01001396func (module *SdkLibrary) generateTestAndSystemScopesByDefault() bool {
1397 return module.sdkLibraryProperties.Generate_system_and_test_apis
1398}
1399
1400func (module *SdkLibrary) getGeneratedApiScopes(ctx android.EarlyModuleContext) apiScopes {
1401 // Check to see if any scopes have been explicitly enabled. If any have then all
1402 // must be.
1403 anyScopesExplicitlyEnabled := false
1404 for _, scope := range allApiScopes {
1405 scopeProperties := module.scopeToProperties[scope]
1406 if scopeProperties.Enabled != nil {
1407 anyScopesExplicitlyEnabled = true
1408 break
1409 }
Paul Duffind1b3a922020-01-22 11:57:20 +00001410 }
Paul Duffin3375e352020-04-28 10:44:03 +01001411
1412 var generatedScopes apiScopes
1413 enabledScopes := make(map[*apiScope]struct{})
1414 for _, scope := range allApiScopes {
1415 scopeProperties := module.scopeToProperties[scope]
1416 // If any scopes are explicitly enabled then ignore the legacy enabled status.
1417 // This is to ensure that any new usages of this module type do not rely on legacy
1418 // behaviour.
1419 defaultEnabledStatus := false
1420 if anyScopesExplicitlyEnabled {
1421 defaultEnabledStatus = scope.defaultEnabledStatus
1422 } else {
1423 defaultEnabledStatus = scope.legacyEnabledStatus(module)
1424 }
1425 enabled := proptools.BoolDefault(scopeProperties.Enabled, defaultEnabledStatus)
1426 if enabled {
1427 enabledScopes[scope] = struct{}{}
1428 generatedScopes = append(generatedScopes, scope)
1429 }
1430 }
1431
1432 // Now check to make sure that any scope that is extended by an enabled scope is also
1433 // enabled.
1434 for _, scope := range allApiScopes {
1435 if _, ok := enabledScopes[scope]; ok {
1436 extends := scope.extends
1437 if extends != nil {
1438 if _, ok := enabledScopes[extends]; !ok {
1439 ctx.ModuleErrorf("enabled api scope %q depends on disabled scope %q", scope, extends)
1440 }
1441 }
1442 }
1443 }
1444
1445 return generatedScopes
Paul Duffind1b3a922020-01-22 11:57:20 +00001446}
1447
satayev758968a2021-12-06 11:42:40 +00001448var _ android.ModuleWithMinSdkVersionCheck = (*SdkLibrary)(nil)
1449
satayev8f088b02021-12-06 11:40:46 +00001450func (module *SdkLibrary) CheckMinSdkVersion(ctx android.ModuleContext) {
Spandan Das8c9ae7e2023-03-03 21:20:36 +00001451 android.CheckMinSdkVersion(ctx, module.MinSdkVersion(ctx), func(c android.ModuleContext, do android.PayloadDepsCallback) {
satayev8f088b02021-12-06 11:40:46 +00001452 ctx.WalkDeps(func(child android.Module, parent android.Module) bool {
1453 isExternal := !module.depIsInSameApex(ctx, child)
1454 if am, ok := child.(android.ApexModule); ok {
1455 if !do(ctx, parent, am, isExternal) {
1456 return false
1457 }
1458 }
1459 return !isExternal
1460 })
1461 })
1462}
1463
Paul Duffineedc5d52020-06-12 17:46:39 +01001464type sdkLibraryComponentTag struct {
1465 blueprint.BaseDependencyTag
1466 name string
1467}
1468
1469// Mark this tag so dependencies that use it are excluded from visibility enforcement.
1470func (t sdkLibraryComponentTag) ExcludeFromVisibilityEnforcement() {}
1471
1472var xmlPermissionsFileTag = sdkLibraryComponentTag{name: "xml-permissions-file"}
Paul Duffine74ac732020-02-06 13:51:46 +00001473
Jiyong Parke3833882020-02-17 17:28:10 +09001474func IsXmlPermissionsFileDepTag(depTag blueprint.DependencyTag) bool {
Paul Duffineedc5d52020-06-12 17:46:39 +01001475 if dt, ok := depTag.(sdkLibraryComponentTag); ok {
Jiyong Parke3833882020-02-17 17:28:10 +09001476 return dt == xmlPermissionsFileTag
1477 }
1478 return false
1479}
1480
Paul Duffineedc5d52020-06-12 17:46:39 +01001481var implLibraryTag = sdkLibraryComponentTag{name: "impl-library"}
Paul Duffin5df79302020-05-16 15:52:12 +01001482
Paul Duffin44f1d842020-06-26 20:17:02 +01001483// Add the dependencies on the child modules in the component deps mutator.
1484func (module *SdkLibrary) ComponentDepsMutator(ctx android.BottomUpMutatorContext) {
Paul Duffin3375e352020-04-28 10:44:03 +01001485 for _, apiScope := range module.getGeneratedApiScopes(ctx) {
Paul Duffind1b3a922020-01-22 11:57:20 +00001486 // Add dependencies to the stubs library
Spandan Das877f39d2023-03-29 16:19:51 +00001487 stubModuleName := module.stubsLibraryModuleName(apiScope)
Jihoon Kangbd093452023-12-26 19:08:01 +00001488 ctx.AddVariationDependencies(nil, apiScope.everythingStubsTag, stubModuleName)
Jihoon Kang1147b312023-06-08 23:25:57 +00001489
Jihoon Kangbd093452023-12-26 19:08:01 +00001490 exportableStubModuleName := module.exportableStubsLibraryModuleName(apiScope)
1491 ctx.AddVariationDependencies(nil, apiScope.exportableStubsTag, exportableStubModuleName)
Paul Duffind1b3a922020-01-22 11:57:20 +00001492
Paul Duffin15f34ef2020-07-20 18:04:44 +01001493 // Add a dependency on the stubs source in order to access both stubs source and api information.
1494 ctx.AddVariationDependencies(nil, apiScope.stubsSourceAndApiTag, module.stubsSourceModuleName(apiScope))
Paul Duffin958806b2022-05-16 13:10:47 +00001495
1496 if module.compareAgainstLatestApi(apiScope) {
1497 // Add dependencies on the latest finalized version of the API .txt file.
1498 latestApiModuleName := module.latestApiModuleName(apiScope)
1499 ctx.AddDependency(module, apiScope.latestApiModuleTag, latestApiModuleName)
1500
1501 // Add dependencies on the latest finalized version of the remove API .txt file.
1502 latestRemovedApiModuleName := module.latestRemovedApiModuleName(apiScope)
1503 ctx.AddDependency(module, apiScope.latestRemovedApiModuleTag, latestRemovedApiModuleName)
1504 }
Sundong Ahn054b19a2018-10-19 13:46:09 +09001505 }
1506
Paul Duffindfa131e2020-05-15 20:37:11 +01001507 if module.requiresRuntimeImplementationLibrary() {
Paul Duffin5df79302020-05-16 15:52:12 +01001508 // Add dependency to the rule for generating the implementation library.
1509 ctx.AddDependency(module, implLibraryTag, module.implLibraryModuleName())
1510
Paul Duffindfa131e2020-05-15 20:37:11 +01001511 if module.sharedLibrary() {
1512 // Add dependency to the rule for generating the xml permissions file
Paul Duffineedc5d52020-06-12 17:46:39 +01001513 ctx.AddDependency(module, xmlPermissionsFileTag, module.xmlPermissionsModuleName())
Paul Duffindfa131e2020-05-15 20:37:11 +01001514 }
Paul Duffin44f1d842020-06-26 20:17:02 +01001515 }
1516}
Paul Duffine74ac732020-02-06 13:51:46 +00001517
Paul Duffin44f1d842020-06-26 20:17:02 +01001518// Add other dependencies as normal.
1519func (module *SdkLibrary) DepsMutator(ctx android.BottomUpMutatorContext) {
Anton Hanssone77fccc2021-01-20 16:52:41 +00001520 var missingApiModules []string
1521 for _, apiScope := range module.getGeneratedApiScopes(ctx) {
1522 if apiScope.unstable {
1523 continue
1524 }
Paul Duffin958806b2022-05-16 13:10:47 +00001525 if m := module.latestApiModuleName(apiScope); !ctx.OtherModuleExists(m) {
Anton Hanssone77fccc2021-01-20 16:52:41 +00001526 missingApiModules = append(missingApiModules, m)
1527 }
Paul Duffin958806b2022-05-16 13:10:47 +00001528 if m := module.latestRemovedApiModuleName(apiScope); !ctx.OtherModuleExists(m) {
Anton Hanssone77fccc2021-01-20 16:52:41 +00001529 missingApiModules = append(missingApiModules, m)
1530 }
Paul Duffin958806b2022-05-16 13:10:47 +00001531 if m := module.latestIncompatibilitiesModuleName(apiScope); !ctx.OtherModuleExists(m) {
Jaewoong Jung1a97ee02021-03-09 13:25:02 -08001532 missingApiModules = append(missingApiModules, m)
1533 }
Anton Hanssone77fccc2021-01-20 16:52:41 +00001534 }
1535 if len(missingApiModules) != 0 && !module.sdkLibraryProperties.Unsafe_ignore_missing_latest_api {
1536 m := module.Name() + " is missing tracking files for previously released library versions.\n"
1537 m += "You need to do one of the following:\n"
1538 m += "- Add `unsafe_ignore_missing_latest_api: true` to your blueprint (to disable compat tracking)\n"
1539 m += "- Add a set of prebuilt txt files representing the last released version of this library for compat checking.\n"
1540 m += " (the current set of API files can be used as a seed for this compatibility tracking\n"
1541 m += "\n"
1542 m += "The following filegroup modules are missing:\n "
1543 m += strings.Join(missingApiModules, "\n ") + "\n"
1544 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."
1545 ctx.ModuleErrorf(m)
1546 }
Paul Duffin44f1d842020-06-26 20:17:02 +01001547 if module.requiresRuntimeImplementationLibrary() {
Paul Duffindfa131e2020-05-15 20:37:11 +01001548 // Only add the deps for the library if it is actually going to be built.
1549 module.Library.deps(ctx)
1550 }
Jiyong Parkc678ad32018-04-10 13:07:10 +09001551}
1552
Paul Duffin46dc45a2020-05-14 15:39:10 +01001553func (module *SdkLibrary) OutputFiles(tag string) (android.Paths, error) {
1554 paths, err := module.commonOutputFiles(tag)
Colin Cross4acaea92021-12-10 23:05:02 +00001555 if paths != nil || err != nil {
Paul Duffin46dc45a2020-05-14 15:39:10 +01001556 return paths, err
1557 }
Colin Cross4acaea92021-12-10 23:05:02 +00001558 if module.requiresRuntimeImplementationLibrary() {
1559 return module.Library.OutputFiles(tag)
1560 }
1561 if tag == "" {
1562 return nil, nil
1563 }
1564 return nil, fmt.Errorf("unsupported module reference tag %q", tag)
Paul Duffin46dc45a2020-05-14 15:39:10 +01001565}
1566
Inseob Kimc0907f12019-02-08 21:00:45 +09001567func (module *SdkLibrary) GenerateAndroidBuildActions(ctx android.ModuleContext) {
satayev8f088b02021-12-06 11:40:46 +00001568 if proptools.String(module.deviceProperties.Min_sdk_version) != "" {
1569 module.CheckMinSdkVersion(ctx)
1570 }
1571
Paul Duffina2ae7e02020-09-11 11:55:00 +01001572 module.generateCommonBuildActions(ctx)
1573
Paul Duffindfa131e2020-05-15 20:37:11 +01001574 // Only build an implementation library if required.
1575 if module.requiresRuntimeImplementationLibrary() {
Jihoon Kangfe914ed2024-02-12 22:49:21 +00001576 // stubsLinkType must be set before calling Library.GenerateAndroidBuildActions
1577 module.Library.stubsLinkType = Unknown
Paul Duffin43db9be2019-12-30 17:35:49 +00001578 module.Library.GenerateAndroidBuildActions(ctx)
1579 }
Sundong Ahn054b19a2018-10-19 13:46:09 +09001580
Paul Duffinb97b1572021-04-29 21:50:40 +01001581 // Collate the components exported by this module. All scope specific modules are exported but
1582 // the impl and xml component modules are not.
1583 exportedComponents := map[string]struct{}{}
1584
Sundong Ahn57368eb2018-07-06 11:20:23 +09001585 // Record the paths to the header jars of the library (stubs and impl).
Paul Duffind1b3a922020-01-22 11:57:20 +00001586 // When this java_sdk_library is depended upon from others via "libs" property,
Jiyong Parkc678ad32018-04-10 13:07:10 +09001587 // the recorded paths will be returned depending on the link type of the caller.
1588 ctx.VisitDirectDeps(func(to android.Module) {
Jiyong Parkc678ad32018-04-10 13:07:10 +09001589 tag := ctx.OtherModuleDependencyTag(to)
1590
Paul Duffinc8782502020-04-29 20:45:27 +01001591 // Extract information from any of the scope specific dependencies.
1592 if scopeTag, ok := tag.(scopeDependencyTag); ok {
1593 apiScope := scopeTag.apiScope
Paul Duffin803a9562020-05-20 11:52:25 +01001594 scopePaths := module.getScopePathsCreateIfNeeded(apiScope)
Paul Duffinc8782502020-04-29 20:45:27 +01001595
1596 // Extract information from the dependency. The exact information extracted
1597 // is determined by the nature of the dependency which is determined by the tag.
1598 scopeTag.extractDepInfo(ctx, to, scopePaths)
Paul Duffinb97b1572021-04-29 21:50:40 +01001599
1600 exportedComponents[ctx.OtherModuleName(to)] = struct{}{}
Sundong Ahn20e998b2018-07-24 11:19:26 +09001601 }
Jiyong Parkc678ad32018-04-10 13:07:10 +09001602 })
Paul Duffinb97b1572021-04-29 21:50:40 +01001603
1604 // Make the set of components exported by this module available for use elsewhere.
Cole Faust18994c72023-02-28 16:02:16 -08001605 exportedComponentInfo := android.ExportedComponentsInfo{Components: android.SortedKeys(exportedComponents)}
Colin Cross40213022023-12-13 15:19:49 -08001606 android.SetProvider(ctx, android.ExportedComponentsInfoProvider, exportedComponentInfo)
Paul Duffin958806b2022-05-16 13:10:47 +00001607
1608 // Provide additional information for inclusion in an sdk's generated .info file.
1609 additionalSdkInfo := map[string]interface{}{}
1610 additionalSdkInfo["dist_stem"] = module.distStem()
Paul Duffine8409952022-09-22 16:24:46 +01001611 baseModuleName := module.distStem()
Paul Duffin958806b2022-05-16 13:10:47 +00001612 scopes := map[string]interface{}{}
1613 additionalSdkInfo["scopes"] = scopes
1614 for scope, scopePaths := range module.scopePaths {
1615 scopeInfo := map[string]interface{}{}
1616 scopes[scope.name] = scopeInfo
1617 scopeInfo["current_api"] = scope.snapshotRelativeCurrentApiTxtPath(baseModuleName)
1618 scopeInfo["removed_api"] = scope.snapshotRelativeRemovedApiTxtPath(baseModuleName)
1619 if p := scopePaths.latestApiPath; p.Valid() {
1620 scopeInfo["latest_api"] = p.Path().String()
1621 }
1622 if p := scopePaths.latestRemovedApiPath; p.Valid() {
1623 scopeInfo["latest_removed_api"] = p.Path().String()
1624 }
1625 }
Colin Cross40213022023-12-13 15:19:49 -08001626 android.SetProvider(ctx, android.AdditionalSdkInfoProvider, android.AdditionalSdkInfo{additionalSdkInfo})
Jiyong Parkc678ad32018-04-10 13:07:10 +09001627}
1628
Jiyong Park0b0e1b92019-12-03 13:24:29 +09001629func (module *SdkLibrary) AndroidMkEntries() []android.AndroidMkEntries {
Paul Duffindfa131e2020-05-15 20:37:11 +01001630 if !module.requiresRuntimeImplementationLibrary() {
Paul Duffin43db9be2019-12-30 17:35:49 +00001631 return nil
1632 }
Jiyong Park0b0e1b92019-12-03 13:24:29 +09001633 entriesList := module.Library.AndroidMkEntries()
Yo Chiang07d75072020-06-05 17:43:19 +08001634 if module.sharedLibrary() {
1635 entries := &entriesList[0]
1636 entries.Required = append(entries.Required, module.xmlPermissionsModuleName())
1637 }
Jiyong Park0b0e1b92019-12-03 13:24:29 +09001638 return entriesList
Jiyong Park82484c02018-04-23 21:41:26 +09001639}
1640
Anton Hansson5fd5d242020-03-27 19:43:19 +00001641// The dist path of the stub artifacts
1642func (module *SdkLibrary) apiDistPath(apiScope *apiScope) string {
Colin Crossf0eace92021-06-02 13:02:23 -07001643 return path.Join("apistubs", module.distGroup(), apiScope.name)
Anton Hansson5fd5d242020-03-27 19:43:19 +00001644}
1645
Paul Duffin12ceb462019-12-24 20:31:31 +00001646// Get the sdk version for use when compiling the stubs library.
Paul Duffin780c5f42020-05-12 15:52:55 +01001647func (module *SdkLibrary) sdkVersionForStubsLibrary(mctx android.EarlyModuleContext, apiScope *apiScope) string {
Paul Duffin87a05a32020-05-12 11:50:28 +01001648 scopeProperties := module.scopeToProperties[apiScope]
1649 if scopeProperties.Sdk_version != nil {
1650 return proptools.String(scopeProperties.Sdk_version)
1651 }
1652
Jiyong Parkf1691d22021-03-29 20:11:58 +09001653 sdkDep := decodeSdkDep(mctx, android.SdkContext(&module.Library))
Paul Duffin12ceb462019-12-24 20:31:31 +00001654 if sdkDep.hasStandardLibs() {
1655 // If building against a standard sdk then use the sdk version appropriate for the scope.
Paul Duffind1b3a922020-01-22 11:57:20 +00001656 return apiScope.sdkVersion
Paul Duffin12ceb462019-12-24 20:31:31 +00001657 } else {
1658 // Otherwise, use no system module.
1659 return "none"
1660 }
1661}
1662
Paul Duffin31310252020-11-20 21:26:20 +00001663func (module *SdkLibrary) distStem() string {
1664 return proptools.StringDefault(module.sdkLibraryProperties.Dist_stem, module.BaseModuleName())
1665}
1666
Colin Cross986b69a2021-06-01 13:13:40 -07001667// distGroup returns the subdirectory of the dist path of the stub artifacts.
1668func (module *SdkLibrary) distGroup() string {
Colin Cross59b92bf2021-06-01 14:07:56 -07001669 return proptools.StringDefault(module.sdkLibraryProperties.Dist_group, "unknown")
Colin Cross986b69a2021-06-01 13:13:40 -07001670}
1671
Paul Duffin958806b2022-05-16 13:10:47 +00001672func latestPrebuiltApiModuleName(name string, apiScope *apiScope) string {
1673 return PrebuiltApiModuleName(name, apiScope.name, "latest")
1674}
1675
Jihoon Kang748a24d2024-03-20 21:29:39 +00001676func latestPrebuiltApiCombinedModuleName(name string, apiScope *apiScope) string {
1677 return PrebuiltApiCombinedModuleName(name, apiScope.name, "latest")
1678}
1679
Paul Duffind1b3a922020-01-22 11:57:20 +00001680func (module *SdkLibrary) latestApiFilegroupName(apiScope *apiScope) string {
Paul Duffin958806b2022-05-16 13:10:47 +00001681 return ":" + module.latestApiModuleName(apiScope)
1682}
1683
1684func (module *SdkLibrary) latestApiModuleName(apiScope *apiScope) string {
Jihoon Kang748a24d2024-03-20 21:29:39 +00001685 return latestPrebuiltApiCombinedModuleName(module.distStem(), apiScope)
Jiyong Park58c518b2018-05-12 22:29:12 +09001686}
Jiyong Parkc678ad32018-04-10 13:07:10 +09001687
Paul Duffind1b3a922020-01-22 11:57:20 +00001688func (module *SdkLibrary) latestRemovedApiFilegroupName(apiScope *apiScope) string {
Paul Duffin958806b2022-05-16 13:10:47 +00001689 return ":" + module.latestRemovedApiModuleName(apiScope)
1690}
1691
1692func (module *SdkLibrary) latestRemovedApiModuleName(apiScope *apiScope) string {
Jihoon Kang748a24d2024-03-20 21:29:39 +00001693 return latestPrebuiltApiCombinedModuleName(module.distStem()+"-removed", apiScope)
Jiyong Parkc678ad32018-04-10 13:07:10 +09001694}
1695
Jaewoong Jung1a97ee02021-03-09 13:25:02 -08001696func (module *SdkLibrary) latestIncompatibilitiesFilegroupName(apiScope *apiScope) string {
Paul Duffin958806b2022-05-16 13:10:47 +00001697 return ":" + module.latestIncompatibilitiesModuleName(apiScope)
1698}
1699
1700func (module *SdkLibrary) latestIncompatibilitiesModuleName(apiScope *apiScope) string {
1701 return latestPrebuiltApiModuleName(module.distStem()+"-incompatibilities", apiScope)
Jaewoong Jung1a97ee02021-03-09 13:25:02 -08001702}
1703
Jihoon Kang1c92c3e2023-03-23 17:44:51 +00001704func (module *SdkLibrary) contributesToApiSurface(c android.Config) bool {
1705 _, exists := c.GetApiLibraries()[module.Name()]
1706 return exists
1707}
1708
Jihoon Kang0c705a42023-08-02 06:44:57 +00001709// The listed modules are the special java_sdk_libraries where apiScope.kind do not match the
1710// api surface that the module contribute to. For example, the public droidstubs and java_library
1711// do not contribute to the public api surface, but contributes to the core platform api surface.
1712// This method returns the full api surface stub lib that
1713// the generated java_api_library should depend on.
1714func (module *SdkLibrary) alternativeFullApiSurfaceStubLib() string {
1715 if val, ok := apiLibraryAdditionalProperties[module.Name()]; ok {
1716 return val.FullApiSurfaceStubLib
1717 }
1718 return ""
1719}
1720
1721// The listed modules' stubs contents do not match the corresponding txt files,
1722// but require additional api contributions to generate the full stubs.
1723// This method returns the name of the additional api contribution module
1724// for corresponding sdk_library modules.
1725func (module *SdkLibrary) apiLibraryAdditionalApiContribution() string {
1726 if val, ok := apiLibraryAdditionalProperties[module.Name()]; ok {
1727 return val.AdditionalApiContribution
1728 }
1729 return ""
1730}
1731
Anton Hansson944e77d2020-08-19 11:40:22 +01001732func childModuleVisibility(childVisibility []string) []string {
1733 if childVisibility == nil {
1734 // No child visibility set. The child will use the visibility of the sdk_library.
1735 return nil
1736 }
1737
1738 // Prepend an override to ignore the sdk_library's visibility, and rely on the child visibility.
1739 var visibility []string
1740 visibility = append(visibility, "//visibility:override")
1741 visibility = append(visibility, childVisibility...)
1742 return visibility
1743}
1744
Paul Duffin5df79302020-05-16 15:52:12 +01001745// Creates the implementation java library
1746func (module *SdkLibrary) createImplLibrary(mctx android.DefaultableHookContext) {
Anton Hansson944e77d2020-08-19 11:40:22 +01001747 visibility := childModuleVisibility(module.sdkLibraryProperties.Impl_library_visibility)
1748
Paul Duffin5df79302020-05-16 15:52:12 +01001749 props := struct {
Paul Duffin77590a82022-04-28 14:13:30 +00001750 Name *string
1751 Visibility []string
1752 Instrument bool
1753 Libs []string
1754 Static_libs []string
1755 Apex_available []string
Paul Duffin5df79302020-05-16 15:52:12 +01001756 }{
1757 Name: proptools.StringPtr(module.implLibraryModuleName()),
Anton Hansson944e77d2020-08-19 11:40:22 +01001758 Visibility: visibility,
Paul Duffin296cf332020-06-18 21:09:55 +01001759 // Set the instrument property to ensure it is instrumented when instrumentation is required.
1760 Instrument: true,
Anton Hansson7f66efa2020-10-08 14:47:23 +01001761 // Set the impl_only libs. Note that the module's "Libs" get appended as well, via the
1762 // addition of &module.properties below.
1763 Libs: module.sdkLibraryProperties.Impl_only_libs,
Paul Duffin77590a82022-04-28 14:13:30 +00001764 // Set the impl_only static libs. Note that the module's "static_libs" get appended as well, via the
1765 // addition of &module.properties below.
1766 Static_libs: module.sdkLibraryProperties.Impl_only_static_libs,
1767 // Pass the apex_available settings down so that the impl library can be statically
1768 // embedded within a library that is added to an APEX. Needed for updatable-media.
1769 Apex_available: module.ApexAvailable(),
Paul Duffin5df79302020-05-16 15:52:12 +01001770 }
1771
1772 properties := []interface{}{
1773 &module.properties,
1774 &module.protoProperties,
1775 &module.deviceProperties,
Liz Kammera7a64f32020-07-09 15:16:41 -07001776 &module.dexProperties,
Paul Duffin5df79302020-05-16 15:52:12 +01001777 &module.dexpreoptProperties,
Colin Cross014489c2020-06-02 20:09:13 -07001778 &module.linter.properties,
Paul Duffin5df79302020-05-16 15:52:12 +01001779 &props,
1780 module.sdkComponentPropertiesForChildLibrary(),
1781 }
1782 mctx.CreateModule(LibraryFactory, properties...)
1783}
1784
Jihoon Kangfa4a90d2023-12-20 02:53:38 +00001785type libraryProperties struct {
1786 Name *string
1787 Visibility []string
1788 Srcs []string
1789 Installable *bool
1790 Sdk_version *string
1791 System_modules *string
1792 Patch_module *string
1793 Libs []string
1794 Static_libs []string
1795 Compile_dex *bool
1796 Java_version *string
1797 Openjdk9 struct {
1798 Srcs []string
1799 Javacflags []string
1800 }
1801 Dist struct {
1802 Targets []string
1803 Dest *string
1804 Dir *string
1805 Tag *string
1806 }
Jihoon Kangfe914ed2024-02-12 22:49:21 +00001807 Is_stubs_module *bool
Jihoon Kangfa4a90d2023-12-20 02:53:38 +00001808}
Jiyong Parkc678ad32018-04-10 13:07:10 +09001809
Jihoon Kangfa4a90d2023-12-20 02:53:38 +00001810func (module *SdkLibrary) stubsLibraryProps(mctx android.DefaultableHookContext, apiScope *apiScope) libraryProperties {
1811 props := libraryProperties{}
Jihoon Kang786df932023-09-07 01:18:31 +00001812 props.Visibility = []string{"//visibility:override", "//visibility:private"}
Jiyong Parkc678ad32018-04-10 13:07:10 +09001813 // sources are generated from the droiddoc
Paul Duffin12ceb462019-12-24 20:31:31 +00001814 sdkVersion := module.sdkVersionForStubsLibrary(mctx, apiScope)
Paul Duffin52d398a2019-06-11 12:31:14 +01001815 props.Sdk_version = proptools.StringPtr(sdkVersion)
Paul Duffina18abc22020-05-16 18:54:24 +01001816 props.System_modules = module.deviceProperties.System_modules
1817 props.Patch_module = module.properties.Patch_module
Paul Duffin367ab912019-12-23 19:40:36 +00001818 props.Installable = proptools.BoolPtr(false)
Sundong Ahn054b19a2018-10-19 13:46:09 +09001819 props.Libs = module.sdkLibraryProperties.Stub_only_libs
Mark White9421c4c2023-08-10 00:07:03 +00001820 props.Libs = append(props.Libs, module.scopeToProperties[apiScope].Libs...)
Anton Hanssondae54cd2021-04-21 16:30:10 +01001821 props.Static_libs = module.sdkLibraryProperties.Stub_only_static_libs
Paul Duffine22c2ab2020-05-20 19:35:27 +01001822 // The stub-annotations library contains special versions of the annotations
1823 // with CLASS retention policy, so that they're kept.
1824 if proptools.Bool(module.sdkLibraryProperties.Annotations_enabled) {
1825 props.Libs = append(props.Libs, "stub-annotations")
1826 }
Paul Duffina18abc22020-05-16 18:54:24 +01001827 props.Openjdk9.Srcs = module.properties.Openjdk9.Srcs
1828 props.Openjdk9.Javacflags = module.properties.Openjdk9.Javacflags
Anton Hansson83509b52020-05-21 09:21:57 +01001829 // We compile the stubs for 1.8 in line with the main android.jar stubs, and potential
1830 // interop with older developer tools that don't support 1.9.
1831 props.Java_version = proptools.StringPtr("1.8")
Jihoon Kangfe914ed2024-02-12 22:49:21 +00001832 props.Is_stubs_module = proptools.BoolPtr(true)
Paul Duffinf4600f62021-05-13 22:34:45 +01001833
Jihoon Kangfa4a90d2023-12-20 02:53:38 +00001834 return props
1835}
1836
1837// Creates a static java library that has API stubs
1838func (module *SdkLibrary) createStubsLibrary(mctx android.DefaultableHookContext, apiScope *apiScope) {
1839
1840 props := module.stubsLibraryProps(mctx, apiScope)
1841 props.Name = proptools.StringPtr(module.sourceStubsLibraryModuleName(apiScope))
1842 props.Srcs = []string{":" + module.stubsSourceModuleName(apiScope)}
1843
1844 mctx.CreateModule(LibraryFactory, &props, module.sdkComponentPropertiesForChildLibrary())
1845}
1846
1847// Create a static java library that compiles the "exportable" stubs
1848func (module *SdkLibrary) createExportableStubsLibrary(mctx android.DefaultableHookContext, apiScope *apiScope) {
1849 props := module.stubsLibraryProps(mctx, apiScope)
1850 props.Name = proptools.StringPtr(module.exportableSourceStubsLibraryModuleName(apiScope))
1851 props.Srcs = []string{":" + module.stubsSourceModuleName(apiScope) + "{.exportable}"}
1852
Paul Duffin859fe962020-05-15 10:20:31 +01001853 mctx.CreateModule(LibraryFactory, &props, module.sdkComponentPropertiesForChildLibrary())
Jiyong Parkc678ad32018-04-10 13:07:10 +09001854}
1855
Paul Duffin6d0886e2020-04-07 18:49:53 +01001856// Creates a droidstubs module that creates stubs source files from the given full source
Paul Duffinc8782502020-04-29 20:45:27 +01001857// files and also updates and checks the API specification files.
Paul Duffin15f34ef2020-07-20 18:04:44 +01001858func (module *SdkLibrary) createStubsSourcesAndApi(mctx android.DefaultableHookContext, apiScope *apiScope, name string, scopeSpecificDroidstubsArgs []string) {
Jiyong Parkc678ad32018-04-10 13:07:10 +09001859 props := struct {
Sundong Ahn054b19a2018-10-19 13:46:09 +09001860 Name *string
Paul Duffin4911a892020-04-29 23:35:13 +01001861 Visibility []string
Sundong Ahn054b19a2018-10-19 13:46:09 +09001862 Srcs []string
1863 Installable *bool
Paul Duffin52d398a2019-06-11 12:31:14 +01001864 Sdk_version *string
Jihoon Kang3198f3c2023-01-26 08:08:52 +00001865 Api_surface *string
Paul Duffin12ceb462019-12-24 20:31:31 +00001866 System_modules *string
Sundong Ahn054b19a2018-10-19 13:46:09 +09001867 Libs []string
Paul Duffin6877e6d2020-09-25 19:59:14 +01001868 Output_javadoc_comments *bool
Paul Duffin11512472019-02-11 15:55:17 +00001869 Arg_files []string
Sundong Ahn054b19a2018-10-19 13:46:09 +09001870 Args *string
Sundong Ahn054b19a2018-10-19 13:46:09 +09001871 Java_version *string
Paul Duffine22c2ab2020-05-20 19:35:27 +01001872 Annotations_enabled *bool
Sundong Ahn054b19a2018-10-19 13:46:09 +09001873 Merge_annotations_dirs []string
1874 Merge_inclusion_annotations_dirs []string
Paul Duffin0ff08bd2020-04-29 13:30:54 +01001875 Generate_stubs *bool
Anton Hanssone87b03d2020-12-21 15:29:34 +00001876 Previous_api *string
Jihoon Kang6592e872023-12-19 01:13:16 +00001877 Aconfig_declarations []string
Sundong Ahn054b19a2018-10-19 13:46:09 +09001878 Check_api struct {
Anton Hanssone6056152020-12-31 10:37:27 +00001879 Current ApiToCheck
1880 Last_released ApiToCheck
Paul Duffin160fe412020-05-10 19:32:20 +01001881
1882 Api_lint struct {
1883 Enabled *bool
1884 New_since *string
1885 Baseline_file *string
1886 }
Jiyong Park58c518b2018-05-12 22:29:12 +09001887 }
Sundong Ahn1b92c822018-05-29 11:35:17 +09001888 Aidl struct {
1889 Include_dirs []string
1890 Local_include_dirs []string
1891 }
Paul Duffin040e9062020-11-23 17:41:36 +00001892 Dists []android.Dist
Jiyong Parkc678ad32018-04-10 13:07:10 +09001893 }{}
1894
Paul Duffin7b78b4d2020-04-28 14:08:32 +01001895 // The stubs source processing uses the same compile time classpath when extracting the
1896 // API from the implementation library as it does when compiling it. i.e. the same
1897 // * sdk version
1898 // * system_modules
1899 // * libs (static_libs/libs)
Paul Duffin250e6192019-06-07 10:44:37 +01001900
Paul Duffin0ff08bd2020-04-29 13:30:54 +01001901 props.Name = proptools.StringPtr(name)
Anton Hansson944e77d2020-08-19 11:40:22 +01001902 props.Visibility = childModuleVisibility(module.sdkLibraryProperties.Stubs_source_visibility)
Paul Duffina18abc22020-05-16 18:54:24 +01001903 props.Srcs = append(props.Srcs, module.properties.Srcs...)
Anton Hanssonf8ea3722021-09-16 14:24:13 +01001904 props.Srcs = append(props.Srcs, module.sdkLibraryProperties.Api_srcs...)
Paul Duffina18abc22020-05-16 18:54:24 +01001905 props.Sdk_version = module.deviceProperties.Sdk_version
Jihoon Kang3198f3c2023-01-26 08:08:52 +00001906 props.Api_surface = &apiScope.name
Paul Duffina18abc22020-05-16 18:54:24 +01001907 props.System_modules = module.deviceProperties.System_modules
Jiyong Parkc678ad32018-04-10 13:07:10 +09001908 props.Installable = proptools.BoolPtr(false)
Sundong Ahne6f0b052018-06-05 16:46:14 +09001909 // A droiddoc module has only one Libs property and doesn't distinguish between
1910 // shared libs and static libs. So we need to add both of these libs to Libs property.
Paul Duffina18abc22020-05-16 18:54:24 +01001911 props.Libs = module.properties.Libs
1912 props.Libs = append(props.Libs, module.properties.Static_libs...)
Nikita Ioffed732da72022-11-21 12:38:25 +00001913 props.Libs = append(props.Libs, module.sdkLibraryProperties.Stub_only_libs...)
Mark White9421c4c2023-08-10 00:07:03 +00001914 props.Libs = append(props.Libs, module.scopeToProperties[apiScope].Libs...)
Paul Duffina18abc22020-05-16 18:54:24 +01001915 props.Aidl.Include_dirs = module.deviceProperties.Aidl.Include_dirs
1916 props.Aidl.Local_include_dirs = module.deviceProperties.Aidl.Local_include_dirs
1917 props.Java_version = module.properties.Java_version
Jiyong Parkc678ad32018-04-10 13:07:10 +09001918
Paul Duffine22c2ab2020-05-20 19:35:27 +01001919 props.Annotations_enabled = module.sdkLibraryProperties.Annotations_enabled
Sundong Ahn054b19a2018-10-19 13:46:09 +09001920 props.Merge_annotations_dirs = module.sdkLibraryProperties.Merge_annotations_dirs
1921 props.Merge_inclusion_annotations_dirs = module.sdkLibraryProperties.Merge_inclusion_annotations_dirs
Jihoon Kang6592e872023-12-19 01:13:16 +00001922 props.Aconfig_declarations = module.sdkLibraryProperties.Aconfig_declarations
Sundong Ahn054b19a2018-10-19 13:46:09 +09001923
Paul Duffin6d0886e2020-04-07 18:49:53 +01001924 droidstubsArgs := []string{}
Paul Duffin235ffff2019-12-24 10:41:30 +00001925 if len(module.sdkLibraryProperties.Api_packages) != 0 {
Paul Duffin6d0886e2020-04-07 18:49:53 +01001926 droidstubsArgs = append(droidstubsArgs, "--stub-packages "+strings.Join(module.sdkLibraryProperties.Api_packages, ":"))
Paul Duffin235ffff2019-12-24 10:41:30 +00001927 }
1928 if len(module.sdkLibraryProperties.Hidden_api_packages) != 0 {
Paul Duffin6d0886e2020-04-07 18:49:53 +01001929 droidstubsArgs = append(droidstubsArgs,
Paul Duffin235ffff2019-12-24 10:41:30 +00001930 android.JoinWithPrefix(module.sdkLibraryProperties.Hidden_api_packages, " --hide-package "))
1931 }
Paul Duffin6d0886e2020-04-07 18:49:53 +01001932 droidstubsArgs = append(droidstubsArgs, module.sdkLibraryProperties.Droiddoc_options...)
Anton Hanssonfd1c0d22023-11-02 15:18:09 +00001933 disabledWarnings := []string{"HiddenSuperclass"}
1934 if proptools.BoolDefault(module.sdkLibraryProperties.Api_lint.Legacy_errors_allowed, true) {
1935 disabledWarnings = append(disabledWarnings,
1936 "BroadcastBehavior",
1937 "DeprecationMismatch",
1938 "MissingPermission",
1939 "SdkConstant",
1940 "Todo",
1941 )
Paul Duffin235ffff2019-12-24 10:41:30 +00001942 }
Paul Duffin6d0886e2020-04-07 18:49:53 +01001943 droidstubsArgs = append(droidstubsArgs, android.JoinWithPrefix(disabledWarnings, "--hide "))
Sundong Ahnfb2721f2018-09-17 13:23:09 +09001944
Paul Duffin6877e6d2020-09-25 19:59:14 +01001945 // Output Javadoc comments for public scope.
1946 if apiScope == apiScopePublic {
1947 props.Output_javadoc_comments = proptools.BoolPtr(true)
1948 }
1949
Paul Duffin1fb487d2020-04-07 18:50:10 +01001950 // Add in scope specific arguments.
Paul Duffin0ff08bd2020-04-29 13:30:54 +01001951 droidstubsArgs = append(droidstubsArgs, scopeSpecificDroidstubsArgs...)
Paul Duffin11512472019-02-11 15:55:17 +00001952 props.Arg_files = module.sdkLibraryProperties.Droiddoc_option_files
Paul Duffin6d0886e2020-04-07 18:49:53 +01001953 props.Args = proptools.StringPtr(strings.Join(droidstubsArgs, " "))
Jiyong Parkc678ad32018-04-10 13:07:10 +09001954
Paul Duffin15f34ef2020-07-20 18:04:44 +01001955 // List of APIs identified from the provided source files are created. They are later
1956 // compared against to the not-yet-released (a.k.a current) list of APIs and to the
1957 // last-released (a.k.a numbered) list of API.
1958 currentApiFileName := apiScope.apiFilePrefix + "current.txt"
1959 removedApiFileName := apiScope.apiFilePrefix + "removed.txt"
1960 apiDir := module.getApiDir()
1961 currentApiFileName = path.Join(apiDir, currentApiFileName)
1962 removedApiFileName = path.Join(apiDir, removedApiFileName)
Jiyong Parkc678ad32018-04-10 13:07:10 +09001963
Paul Duffin15f34ef2020-07-20 18:04:44 +01001964 // check against the not-yet-release API
1965 props.Check_api.Current.Api_file = proptools.StringPtr(currentApiFileName)
1966 props.Check_api.Current.Removed_api_file = proptools.StringPtr(removedApiFileName)
Jiyong Park58c518b2018-05-12 22:29:12 +09001967
Paul Duffin958806b2022-05-16 13:10:47 +00001968 if module.compareAgainstLatestApi(apiScope) {
Paul Duffin15f34ef2020-07-20 18:04:44 +01001969 // check against the latest released API
1970 latestApiFilegroupName := proptools.StringPtr(module.latestApiFilegroupName(apiScope))
Anton Hanssone87b03d2020-12-21 15:29:34 +00001971 props.Previous_api = latestApiFilegroupName
Paul Duffin15f34ef2020-07-20 18:04:44 +01001972 props.Check_api.Last_released.Api_file = latestApiFilegroupName
1973 props.Check_api.Last_released.Removed_api_file = proptools.StringPtr(
1974 module.latestRemovedApiFilegroupName(apiScope))
Jaewoong Jung1a97ee02021-03-09 13:25:02 -08001975 props.Check_api.Last_released.Baseline_file = proptools.StringPtr(
1976 module.latestIncompatibilitiesFilegroupName(apiScope))
Paul Duffin160fe412020-05-10 19:32:20 +01001977
Paul Duffin15f34ef2020-07-20 18:04:44 +01001978 if proptools.Bool(module.sdkLibraryProperties.Api_lint.Enabled) {
1979 // Enable api lint.
1980 props.Check_api.Api_lint.Enabled = proptools.BoolPtr(true)
1981 props.Check_api.Api_lint.New_since = latestApiFilegroupName
Paul Duffin160fe412020-05-10 19:32:20 +01001982
Paul Duffin15f34ef2020-07-20 18:04:44 +01001983 // If it exists then pass a lint-baseline.txt through to droidstubs.
1984 baselinePath := path.Join(apiDir, apiScope.apiFilePrefix+"lint-baseline.txt")
1985 baselinePathRelativeToRoot := path.Join(mctx.ModuleDir(), baselinePath)
1986 paths, err := mctx.GlobWithDeps(baselinePathRelativeToRoot, nil)
1987 if err != nil {
1988 mctx.ModuleErrorf("error checking for presence of %s: %s", baselinePathRelativeToRoot, err)
1989 }
1990 if len(paths) == 1 {
1991 props.Check_api.Api_lint.Baseline_file = proptools.StringPtr(baselinePath)
1992 } else if len(paths) != 0 {
1993 mctx.ModuleErrorf("error checking for presence of %s: expected one path, found: %v", baselinePathRelativeToRoot, paths)
Paul Duffin160fe412020-05-10 19:32:20 +01001994 }
1995 }
Paul Duffin15f34ef2020-07-20 18:04:44 +01001996 }
Jiyong Park58c518b2018-05-12 22:29:12 +09001997
Paul Duffin15f34ef2020-07-20 18:04:44 +01001998 if !Bool(module.sdkLibraryProperties.No_dist) {
Paul Duffin040e9062020-11-23 17:41:36 +00001999 // Dist the api txt and removed api txt artifacts for sdk builds.
2000 distDir := proptools.StringPtr(path.Join(module.apiDistPath(apiScope), "api"))
Jihoon Kang02168052024-03-20 00:44:54 +00002001 stubsTypeTagPrefix := ""
2002 if mctx.Config().ReleaseHiddenApiExportableStubs() {
2003 stubsTypeTagPrefix = ".exportable"
2004 }
Paul Duffin040e9062020-11-23 17:41:36 +00002005 for _, p := range []struct {
2006 tag string
2007 pattern string
2008 }{
Jihoon Kangd1799f62024-02-20 23:01:38 +00002009 // "exportable" api files are copied to the dist directory instead of the
Jihoon Kang02168052024-03-20 00:44:54 +00002010 // "everything" api files when "RELEASE_HIDDEN_API_EXPORTABLE_STUBS" build flag
2011 // is set. Otherwise, the "everything" api files are copied to the dist directory.
2012 {tag: "%s.api.txt", pattern: "%s.txt"},
2013 {tag: "%s.removed-api.txt", pattern: "%s-removed.txt"},
Paul Duffin040e9062020-11-23 17:41:36 +00002014 } {
2015 props.Dists = append(props.Dists, android.Dist{
2016 Targets: []string{"sdk", "win_sdk"},
2017 Dir: distDir,
2018 Dest: proptools.StringPtr(fmt.Sprintf(p.pattern, module.distStem())),
Jihoon Kang02168052024-03-20 00:44:54 +00002019 Tag: proptools.StringPtr(fmt.Sprintf(p.tag, stubsTypeTagPrefix)),
Paul Duffin040e9062020-11-23 17:41:36 +00002020 })
2021 }
Anton Hansson5fd5d242020-03-27 19:43:19 +00002022 }
2023
Spandan Das2cc80ba2023-10-27 17:21:52 +00002024 mctx.CreateModule(DroidstubsFactory, &props, module.sdkComponentPropertiesForChildLibrary()).(*Droidstubs).CallHookIfAvailable(mctx)
Jiyong Parkc678ad32018-04-10 13:07:10 +09002025}
2026
Jihoon Kang0c705a42023-08-02 06:44:57 +00002027func (module *SdkLibrary) createApiLibrary(mctx android.DefaultableHookContext, apiScope *apiScope, alternativeFullApiSurfaceStub string) {
Jihoon Kang1c92c3e2023-03-23 17:44:51 +00002028 props := struct {
Jihoon Kangca198c22023-06-22 23:13:51 +00002029 Name *string
2030 Visibility []string
2031 Api_contributions []string
2032 Libs []string
2033 Static_libs []string
2034 Full_api_surface_stub *string
Jihoon Kang4ec24872023-10-05 17:26:09 +00002035 System_modules *string
Jihoon Kang063ec002023-06-28 01:16:23 +00002036 Enable_validation *bool
Jihoon Kang5d701272024-02-15 21:53:49 +00002037 Stubs_type *string
Jihoon Kang1c92c3e2023-03-23 17:44:51 +00002038 }{}
2039
2040 props.Name = proptools.StringPtr(module.apiLibraryModuleName(apiScope))
Jihoon Kang786df932023-09-07 01:18:31 +00002041 props.Visibility = []string{"//visibility:override", "//visibility:private"}
Jihoon Kang1c92c3e2023-03-23 17:44:51 +00002042
2043 apiContributions := []string{}
2044
2045 // Api surfaces are not independent of each other, but have subset relationships,
2046 // and so does the api files. To generate from-text stubs for api surfaces other than public,
2047 // all subset api domains' api_contriubtions must be added as well.
2048 scope := apiScope
2049 for scope != nil {
2050 apiContributions = append(apiContributions, module.stubsSourceModuleName(scope)+".api.contribution")
2051 scope = scope.extends
2052 }
Jihoon Kang0c705a42023-08-02 06:44:57 +00002053 if apiScope == apiScopePublic {
2054 additionalApiContribution := module.apiLibraryAdditionalApiContribution()
2055 if additionalApiContribution != "" {
2056 apiContributions = append(apiContributions, additionalApiContribution)
2057 }
2058 }
Jihoon Kang1c92c3e2023-03-23 17:44:51 +00002059
2060 props.Api_contributions = apiContributions
2061 props.Libs = module.properties.Libs
2062 props.Libs = append(props.Libs, module.sdkLibraryProperties.Stub_only_libs...)
Mark White9421c4c2023-08-10 00:07:03 +00002063 props.Libs = append(props.Libs, module.scopeToProperties[apiScope].Libs...)
Jihoon Kang1c92c3e2023-03-23 17:44:51 +00002064 props.Libs = append(props.Libs, "stub-annotations")
2065 props.Static_libs = module.sdkLibraryProperties.Stub_only_static_libs
Jihoon Kange7ee2562023-07-25 05:51:46 +00002066 props.Full_api_surface_stub = proptools.StringPtr(apiScope.kind.DefaultJavaLibraryName())
Jihoon Kang0c705a42023-08-02 06:44:57 +00002067 if alternativeFullApiSurfaceStub != "" {
2068 props.Full_api_surface_stub = proptools.StringPtr(alternativeFullApiSurfaceStub)
2069 }
Jihoon Kang1c92c3e2023-03-23 17:44:51 +00002070
2071 // android_module_lib_stubs_current.from-text only comprises api contributions from art, conscrypt and i18n.
2072 // Thus, replace with android_module_lib_stubs_current_full.from-text, which comprises every api domains.
2073 if apiScope.kind == android.SdkModule {
Jihoon Kangca198c22023-06-22 23:13:51 +00002074 props.Full_api_surface_stub = proptools.StringPtr(apiScope.kind.DefaultJavaLibraryName() + "_full.from-text")
Jihoon Kang1c92c3e2023-03-23 17:44:51 +00002075 }
2076
Jihoon Kangd30ac8a2023-10-09 18:00:17 +00002077 // java_sdk_library modules that set sdk_version as none does not depend on other api
2078 // domains. Therefore, java_api_library created from such modules should not depend on
2079 // full_api_surface_stubs but create and compile stubs by the java_api_library module
2080 // itself.
2081 if module.SdkVersion(mctx).Kind == android.SdkNone {
2082 props.Full_api_surface_stub = nil
2083 }
2084
Jihoon Kang4ec24872023-10-05 17:26:09 +00002085 props.System_modules = module.deviceProperties.System_modules
Jihoon Kang063ec002023-06-28 01:16:23 +00002086 props.Enable_validation = proptools.BoolPtr(true)
Jihoon Kang5d701272024-02-15 21:53:49 +00002087 props.Stubs_type = proptools.StringPtr("everything")
Jihoon Kang4ec24872023-10-05 17:26:09 +00002088
Spandan Das2cc80ba2023-10-27 17:21:52 +00002089 mctx.CreateModule(ApiLibraryFactory, &props, module.sdkComponentPropertiesForChildLibrary())
Jihoon Kang1c92c3e2023-03-23 17:44:51 +00002090}
2091
Jihoon Kang02168052024-03-20 00:44:54 +00002092func (module *SdkLibrary) topLevelStubsLibraryProps(mctx android.DefaultableHookContext, apiScope *apiScope, doDist bool) libraryProperties {
Jihoon Kangfa4a90d2023-12-20 02:53:38 +00002093 props := libraryProperties{}
2094
Jihoon Kang1147b312023-06-08 23:25:57 +00002095 props.Visibility = childModuleVisibility(module.sdkLibraryProperties.Stubs_library_visibility)
2096 sdkVersion := module.sdkVersionForStubsLibrary(mctx, apiScope)
2097 props.Sdk_version = proptools.StringPtr(sdkVersion)
2098
Jihoon Kang1147b312023-06-08 23:25:57 +00002099 props.System_modules = module.deviceProperties.System_modules
2100
Jihoon Kang1147b312023-06-08 23:25:57 +00002101 // The imports need to be compiled to dex if the java_sdk_library requests it.
2102 compileDex := module.dexProperties.Compile_dex
2103 if module.stubLibrariesCompiledForDex() {
2104 compileDex = proptools.BoolPtr(true)
2105 }
2106 props.Compile_dex = compileDex
2107
Jihoon Kang02168052024-03-20 00:44:54 +00002108 if !Bool(module.sdkLibraryProperties.No_dist) && doDist {
2109 props.Dist.Targets = []string{"sdk", "win_sdk"}
2110 props.Dist.Dest = proptools.StringPtr(fmt.Sprintf("%v.jar", module.distStem()))
2111 props.Dist.Dir = proptools.StringPtr(module.apiDistPath(apiScope))
2112 props.Dist.Tag = proptools.StringPtr(".jar")
2113 }
2114
Jihoon Kangfa4a90d2023-12-20 02:53:38 +00002115 return props
2116}
2117
2118func (module *SdkLibrary) createTopLevelStubsLibrary(
2119 mctx android.DefaultableHookContext, apiScope *apiScope, contributesToApiSurface bool) {
2120
Jihoon Kang02168052024-03-20 00:44:54 +00002121 // Dist the "everything" stubs when the RELEASE_HIDDEN_API_EXPORTABLE_STUBS build flag is false
2122 doDist := !mctx.Config().ReleaseHiddenApiExportableStubs()
2123 props := module.topLevelStubsLibraryProps(mctx, apiScope, doDist)
Jihoon Kangfa4a90d2023-12-20 02:53:38 +00002124 props.Name = proptools.StringPtr(module.stubsLibraryModuleName(apiScope))
2125
2126 // Add the stub compiling java_library/java_api_library as static lib based on build config
2127 staticLib := module.sourceStubsLibraryModuleName(apiScope)
2128 if mctx.Config().BuildFromTextStub() && contributesToApiSurface {
2129 staticLib = module.apiLibraryModuleName(apiScope)
2130 }
2131 props.Static_libs = append(props.Static_libs, staticLib)
2132
2133 mctx.CreateModule(LibraryFactory, &props, module.sdkComponentPropertiesForChildLibrary())
2134}
2135
2136func (module *SdkLibrary) createTopLevelExportableStubsLibrary(
2137 mctx android.DefaultableHookContext, apiScope *apiScope) {
2138
Jihoon Kang02168052024-03-20 00:44:54 +00002139 // Dist the "exportable" stubs when the RELEASE_HIDDEN_API_EXPORTABLE_STUBS build flag is true
2140 doDist := mctx.Config().ReleaseHiddenApiExportableStubs()
2141 props := module.topLevelStubsLibraryProps(mctx, apiScope, doDist)
Jihoon Kangfa4a90d2023-12-20 02:53:38 +00002142 props.Name = proptools.StringPtr(module.exportableStubsLibraryModuleName(apiScope))
2143
Jihoon Kangfa4a90d2023-12-20 02:53:38 +00002144 staticLib := module.exportableSourceStubsLibraryModuleName(apiScope)
2145 props.Static_libs = append(props.Static_libs, staticLib)
2146
Jihoon Kang1147b312023-06-08 23:25:57 +00002147 mctx.CreateModule(LibraryFactory, &props, module.sdkComponentPropertiesForChildLibrary())
2148}
2149
Paul Duffin958806b2022-05-16 13:10:47 +00002150func (module *SdkLibrary) compareAgainstLatestApi(apiScope *apiScope) bool {
2151 return !(apiScope.unstable || module.sdkLibraryProperties.Unsafe_ignore_missing_latest_api)
2152}
2153
Paul Duffinea8f8082021-06-24 13:25:57 +01002154// Implements android.ApexModule
Jooyung Han5e9013b2020-03-10 06:23:13 +09002155func (module *SdkLibrary) DepIsInSameApex(mctx android.BaseModuleContext, dep android.Module) bool {
2156 depTag := mctx.OtherModuleDependencyTag(dep)
2157 if depTag == xmlPermissionsFileTag {
2158 return true
2159 }
2160 return module.Library.DepIsInSameApex(mctx, dep)
2161}
2162
Paul Duffinea8f8082021-06-24 13:25:57 +01002163// Implements android.ApexModule
2164func (module *SdkLibrary) UniqueApexVariations() bool {
2165 return module.uniqueApexVariations()
2166}
2167
Jihoon Kang80456fd2023-11-15 19:22:14 +00002168func (module *SdkLibrary) ContributeToApi() bool {
2169 return proptools.BoolDefault(module.sdkLibraryProperties.Contribute_to_android_api, false)
2170}
2171
Jiyong Parkc678ad32018-04-10 13:07:10 +09002172// Creates the xml file that publicizes the runtime library
Paul Duffinf0229202020-04-29 16:47:28 +01002173func (module *SdkLibrary) createXmlFile(mctx android.DefaultableHookContext) {
Spandan Das8c9ae7e2023-03-03 21:20:36 +00002174 moduleMinApiLevel := module.Library.MinSdkVersion(mctx)
Pedro Loureiroc3621422021-09-28 15:40:23 +00002175 var moduleMinApiLevelStr = moduleMinApiLevel.String()
2176 if moduleMinApiLevel == android.NoneApiLevel {
2177 moduleMinApiLevelStr = "current"
2178 }
Jiyong Parke3833882020-02-17 17:28:10 +09002179 props := struct {
Pedro Loureiroc3621422021-09-28 15:40:23 +00002180 Name *string
2181 Lib_name *string
2182 Apex_available []string
2183 On_bootclasspath_since *string
2184 On_bootclasspath_before *string
2185 Min_device_sdk *string
2186 Max_device_sdk *string
2187 Sdk_library_min_api_level *string
Jamie Garsidee570ace2023-11-27 12:07:36 +00002188 Uses_libs_dependencies []string
Jiyong Parke3833882020-02-17 17:28:10 +09002189 }{
Pedro Loureiroc3621422021-09-28 15:40:23 +00002190 Name: proptools.StringPtr(module.xmlPermissionsModuleName()),
2191 Lib_name: proptools.StringPtr(module.BaseModuleName()),
2192 Apex_available: module.ApexProperties.Apex_available,
2193 On_bootclasspath_since: module.commonSdkLibraryProperties.On_bootclasspath_since,
2194 On_bootclasspath_before: module.commonSdkLibraryProperties.On_bootclasspath_before,
2195 Min_device_sdk: module.commonSdkLibraryProperties.Min_device_sdk,
2196 Max_device_sdk: module.commonSdkLibraryProperties.Max_device_sdk,
2197 Sdk_library_min_api_level: &moduleMinApiLevelStr,
Jamie Garsidee570ace2023-11-27 12:07:36 +00002198 Uses_libs_dependencies: module.usesLibraryProperties.Uses_libs,
Jiyong Parkc678ad32018-04-10 13:07:10 +09002199 }
Jiyong Parke3833882020-02-17 17:28:10 +09002200
Jiyong Parke3833882020-02-17 17:28:10 +09002201 mctx.CreateModule(sdkLibraryXmlFactory, &props)
Jiyong Parkc678ad32018-04-10 13:07:10 +09002202}
2203
Jiyong Parkf1691d22021-03-29 20:11:58 +09002204func PrebuiltJars(ctx android.BaseModuleContext, baseName string, s android.SdkSpec) android.Paths {
Jiyong Park54105c42021-03-31 18:17:53 +09002205 var ver android.ApiLevel
Jiyong Parkf1691d22021-03-29 20:11:58 +09002206 var kind android.SdkKind
2207 if s.UsePrebuilt(ctx) {
Jiyong Park54105c42021-03-31 18:17:53 +09002208 ver = s.ApiLevel
Jiyong Parkf1691d22021-03-29 20:11:58 +09002209 kind = s.Kind
Jiyong Parkc678ad32018-04-10 13:07:10 +09002210 } else {
Jiyong Park6a927c42020-01-21 02:03:43 +09002211 // We don't have prebuilt SDK for the specific sdkVersion.
2212 // Instead of breaking the build, fallback to use "system_current"
Jiyong Park54105c42021-03-31 18:17:53 +09002213 ver = android.FutureApiLevel
Jiyong Parkf1691d22021-03-29 20:11:58 +09002214 kind = android.SdkSystem
Sundong Ahn054b19a2018-10-19 13:46:09 +09002215 }
Jiyong Park6a927c42020-01-21 02:03:43 +09002216
2217 dir := filepath.Join("prebuilts", "sdk", ver.String(), kind.String())
Paul Duffin50061512020-01-21 16:31:05 +00002218 jar := filepath.Join(dir, baseName+".jar")
Sundong Ahn054b19a2018-10-19 13:46:09 +09002219 jarPath := android.ExistentPathForSource(ctx, jar)
Sundong Ahnae418ac2019-02-28 15:01:28 +09002220 if !jarPath.Valid() {
Colin Cross07c88562020-01-07 09:34:44 -08002221 if ctx.Config().AllowMissingDependencies() {
2222 return android.Paths{android.PathForSource(ctx, jar)}
2223 } else {
Jiyong Parkf1691d22021-03-29 20:11:58 +09002224 ctx.PropertyErrorf("sdk_library", "invalid sdk version %q, %q does not exist", s.Raw, jar)
Colin Cross07c88562020-01-07 09:34:44 -08002225 }
Sundong Ahnae418ac2019-02-28 15:01:28 +09002226 return nil
2227 }
Sundong Ahn054b19a2018-10-19 13:46:09 +09002228 return android.Paths{jarPath.Path()}
2229}
2230
Colin Crossaede88c2020-08-11 12:17:01 -07002231// 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 +01002232//
2233// If either this or the other module are on the platform then this will return
2234// false.
Colin Cross56a83212020-09-15 18:30:11 -07002235func withinSameApexesAs(ctx android.BaseModuleContext, other android.Module) bool {
Colin Crossff694a82023-12-13 15:54:49 -08002236 apexInfo, _ := android.ModuleProvider(ctx, android.ApexInfoProvider)
Colin Cross313aa542023-12-13 13:47:44 -08002237 otherApexInfo, _ := android.OtherModuleProvider(ctx, other, android.ApexInfoProvider)
Jiyong Parkab50b072021-05-12 17:13:56 +09002238 return len(otherApexInfo.InApexVariants) > 0 && reflect.DeepEqual(apexInfo.InApexVariants, otherApexInfo.InApexVariants)
Paul Duffin9b879592020-05-26 13:21:35 +01002239}
2240
Jiyong Parkf1691d22021-03-29 20:11:58 +09002241func (module *SdkLibrary) sdkJars(ctx android.BaseModuleContext, sdkVersion android.SdkSpec, headerJars bool) android.Paths {
Jiyong Park932cdfe2020-05-28 00:19:53 +09002242 // If the client doesn't set sdk_version, but if this library prefers stubs over
2243 // the impl library, let's provide the widest API surface possible. To do so,
2244 // force override sdk_version to module_current so that the closest possible API
2245 // surface could be found in selectHeaderJarsForSdkVersion
Jiyong Parkf1691d22021-03-29 20:11:58 +09002246 if module.defaultsToStubs() && !sdkVersion.Specified() {
Jiyong Park92315372021-04-02 08:45:46 +09002247 sdkVersion = android.SdkSpecFrom(ctx, "module_current")
Jiyong Park932cdfe2020-05-28 00:19:53 +09002248 }
Paul Duffind1b3a922020-01-22 11:57:20 +00002249
Paul Duffindaaa3322020-05-26 18:13:57 +01002250 // Only provide access to the implementation library if it is actually built.
2251 if module.requiresRuntimeImplementationLibrary() {
2252 // Check any special cases for java_sdk_library.
2253 //
2254 // Only allow access to the implementation library in the following condition:
2255 // * No sdk_version specified on the referencing module.
Paul Duffin9b879592020-05-26 13:21:35 +01002256 // * The referencing module is in the same apex as this.
Jiyong Parkf1691d22021-03-29 20:11:58 +09002257 if sdkVersion.Kind == android.SdkPrivate || withinSameApexesAs(ctx, module) {
Paul Duffindaaa3322020-05-26 18:13:57 +01002258 if headerJars {
2259 return module.HeaderJars()
2260 } else {
2261 return module.ImplementationJars()
2262 }
Sundong Ahn054b19a2018-10-19 13:46:09 +09002263 }
Jiyong Parkc678ad32018-04-10 13:07:10 +09002264 }
Paul Duffinb05d4292020-05-20 12:19:10 +01002265
Paul Duffin23970f42020-05-20 14:20:02 +01002266 return module.selectHeaderJarsForSdkVersion(ctx, sdkVersion)
Jiyong Parkc678ad32018-04-10 13:07:10 +09002267}
2268
Sundong Ahn241cd372018-07-13 16:16:44 +09002269// to satisfy SdkLibraryDependency interface
Jiyong Parkf1691d22021-03-29 20:11:58 +09002270func (module *SdkLibrary) SdkHeaderJars(ctx android.BaseModuleContext, sdkVersion android.SdkSpec) android.Paths {
Paul Duffind1b3a922020-01-22 11:57:20 +00002271 return module.sdkJars(ctx, sdkVersion, true /*headerJars*/)
2272}
2273
2274// to satisfy SdkLibraryDependency interface
Jiyong Parkf1691d22021-03-29 20:11:58 +09002275func (module *SdkLibrary) SdkImplementationJars(ctx android.BaseModuleContext, sdkVersion android.SdkSpec) android.Paths {
Paul Duffind1b3a922020-01-22 11:57:20 +00002276 return module.sdkJars(ctx, sdkVersion, false /*headerJars*/)
Sundong Ahn241cd372018-07-13 16:16:44 +09002277}
2278
Colin Cross571cccf2019-02-04 11:22:08 -08002279var javaSdkLibrariesKey = android.NewOnceKey("javaSdkLibraries")
2280
Jiyong Park82484c02018-04-23 21:41:26 +09002281func javaSdkLibraries(config android.Config) *[]string {
Colin Cross571cccf2019-02-04 11:22:08 -08002282 return config.Once(javaSdkLibrariesKey, func() interface{} {
Jiyong Park82484c02018-04-23 21:41:26 +09002283 return &[]string{}
2284 }).(*[]string)
2285}
2286
Paul Duffin749f98f2019-12-30 17:23:46 +00002287func (module *SdkLibrary) getApiDir() string {
2288 return proptools.StringDefault(module.sdkLibraryProperties.Api_dir, "api")
2289}
2290
Jiyong Parkc678ad32018-04-10 13:07:10 +09002291// For a java_sdk_library module, create internal modules for stubs, docs,
2292// runtime libs and xml file. If requested, the stubs and docs are created twice
2293// once for public API level and once for system API level
Paul Duffinf0229202020-04-29 16:47:28 +01002294func (module *SdkLibrary) CreateInternalModules(mctx android.DefaultableHookContext) {
2295 // If the module has been disabled then don't create any child modules.
2296 if !module.Enabled() {
2297 return
2298 }
2299
Paul Duffina18abc22020-05-16 18:54:24 +01002300 if len(module.properties.Srcs) == 0 {
Inseob Kimc0907f12019-02-08 21:00:45 +09002301 mctx.PropertyErrorf("srcs", "java_sdk_library must specify srcs")
Jooyung Han58f26ab2019-12-18 15:34:32 +09002302 return
Inseob Kimc0907f12019-02-08 21:00:45 +09002303 }
2304
Paul Duffin37e0b772019-12-30 17:20:10 +00002305 // If this builds against standard libraries (i.e. is not part of the core libraries)
Paul Duffin4f5c1ef2020-11-19 14:53:43 +00002306 // then assume it provides both system and test apis.
Jiyong Parkf1691d22021-03-29 20:11:58 +09002307 sdkDep := decodeSdkDep(mctx, android.SdkContext(&module.Library))
Paul Duffin37e0b772019-12-30 17:20:10 +00002308 hasSystemAndTestApis := sdkDep.hasStandardLibs()
Paul Duffin3375e352020-04-28 10:44:03 +01002309 module.sdkLibraryProperties.Generate_system_and_test_apis = hasSystemAndTestApis
Paul Duffin4f5c1ef2020-11-19 14:53:43 +00002310
Jaewoong Jung18aefc12020-12-21 09:11:10 -08002311 missingCurrentApi := false
Inseob Kim8098faa2019-03-18 10:19:51 +09002312
Paul Duffin3375e352020-04-28 10:44:03 +01002313 generatedScopes := module.getGeneratedApiScopes(mctx)
Paul Duffind1b3a922020-01-22 11:57:20 +00002314
Paul Duffin749f98f2019-12-30 17:23:46 +00002315 apiDir := module.getApiDir()
Paul Duffin3375e352020-04-28 10:44:03 +01002316 for _, scope := range generatedScopes {
Inseob Kim8098faa2019-03-18 10:19:51 +09002317 for _, api := range []string{"current.txt", "removed.txt"} {
Paul Duffind1b3a922020-01-22 11:57:20 +00002318 path := path.Join(mctx.ModuleDir(), apiDir, scope.apiFilePrefix+api)
Inseob Kim8098faa2019-03-18 10:19:51 +09002319 p := android.ExistentPathForSource(mctx, path)
2320 if !p.Valid() {
Colin Cross18f840c2021-05-20 17:56:54 -07002321 if mctx.Config().AllowMissingDependencies() {
2322 mctx.AddMissingDependencies([]string{path})
2323 } else {
2324 mctx.ModuleErrorf("Current api file %#v doesn't exist", path)
2325 missingCurrentApi = true
2326 }
Inseob Kim8098faa2019-03-18 10:19:51 +09002327 }
2328 }
2329 }
2330
Jaewoong Jung18aefc12020-12-21 09:11:10 -08002331 if missingCurrentApi {
Inseob Kim8098faa2019-03-18 10:19:51 +09002332 script := "build/soong/scripts/gen-java-current-api-files.sh"
2333 p := android.ExistentPathForSource(mctx, script)
2334
2335 if !p.Valid() {
2336 panic(fmt.Sprintf("script file %s doesn't exist", script))
2337 }
2338
2339 mctx.ModuleErrorf("One or more current api files are missing. "+
2340 "You can update them by:\n"+
Paul Duffin37e0b772019-12-30 17:20:10 +00002341 "%s %q %s && m update-api",
Paul Duffind1b3a922020-01-22 11:57:20 +00002342 script, filepath.Join(mctx.ModuleDir(), apiDir),
Paul Duffin3375e352020-04-28 10:44:03 +01002343 strings.Join(generatedScopes.Strings(func(s *apiScope) string { return s.apiFilePrefix }), " "))
Inseob Kim8098faa2019-03-18 10:19:51 +09002344 return
2345 }
2346
Paul Duffin3375e352020-04-28 10:44:03 +01002347 for _, scope := range generatedScopes {
Paul Duffin15f34ef2020-07-20 18:04:44 +01002348 // Use the stubs source name for legacy reasons.
2349 module.createStubsSourcesAndApi(mctx, scope, module.stubsSourceModuleName(scope), scope.droidstubsArgs)
Paul Duffin0ff08bd2020-04-29 13:30:54 +01002350
Paul Duffind1b3a922020-01-22 11:57:20 +00002351 module.createStubsLibrary(mctx, scope)
Jihoon Kangfa4a90d2023-12-20 02:53:38 +00002352 module.createExportableStubsLibrary(mctx, scope)
Jihoon Kang1c92c3e2023-03-23 17:44:51 +00002353
Jihoon Kang0c705a42023-08-02 06:44:57 +00002354 alternativeFullApiSurfaceStubLib := ""
2355 if scope == apiScopePublic {
2356 alternativeFullApiSurfaceStubLib = module.alternativeFullApiSurfaceStubLib()
2357 }
2358 contributesToApiSurface := module.contributesToApiSurface(mctx.Config()) || alternativeFullApiSurfaceStubLib != ""
Jihoon Kang1147b312023-06-08 23:25:57 +00002359 if contributesToApiSurface {
Jihoon Kang0c705a42023-08-02 06:44:57 +00002360 module.createApiLibrary(mctx, scope, alternativeFullApiSurfaceStubLib)
Jihoon Kang1c92c3e2023-03-23 17:44:51 +00002361 }
Jihoon Kang1147b312023-06-08 23:25:57 +00002362
2363 module.createTopLevelStubsLibrary(mctx, scope, contributesToApiSurface)
Jihoon Kangfa4a90d2023-12-20 02:53:38 +00002364 module.createTopLevelExportableStubsLibrary(mctx, scope)
Inseob Kimc0907f12019-02-08 21:00:45 +09002365 }
2366
Paul Duffindfa131e2020-05-15 20:37:11 +01002367 if module.requiresRuntimeImplementationLibrary() {
Paul Duffin5df79302020-05-16 15:52:12 +01002368 // Create child module to create an implementation library.
2369 //
2370 // This temporarily creates a second implementation library that can be explicitly
2371 // referenced.
2372 //
2373 // TODO(b/156618935) - update comment once only one implementation library is created.
2374 module.createImplLibrary(mctx)
2375
Paul Duffindfa131e2020-05-15 20:37:11 +01002376 // Only create an XML permissions file that declares the library as being usable
2377 // as a shared library if required.
2378 if module.sharedLibrary() {
2379 module.createXmlFile(mctx)
2380 }
Paul Duffin43db9be2019-12-30 17:35:49 +00002381
2382 // record java_sdk_library modules so that they are exported to make
2383 javaSdkLibraries := javaSdkLibraries(mctx.Config())
2384 javaSdkLibrariesLock.Lock()
2385 defer javaSdkLibrariesLock.Unlock()
2386 *javaSdkLibraries = append(*javaSdkLibraries, module.BaseModuleName())
2387 }
Anton Hansson7f66efa2020-10-08 14:47:23 +01002388
Paul Duffin77590a82022-04-28 14:13:30 +00002389 // 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 +01002390 module.properties.Libs = append(module.properties.Libs, module.sdkLibraryProperties.Impl_only_libs...)
Paul Duffin77590a82022-04-28 14:13:30 +00002391 module.properties.Static_libs = append(module.properties.Static_libs, module.sdkLibraryProperties.Impl_only_static_libs...)
Inseob Kimc0907f12019-02-08 21:00:45 +09002392}
2393
2394func (module *SdkLibrary) InitSdkLibraryProperties() {
Colin Crossce6734e2020-06-15 16:09:53 -07002395 module.addHostAndDeviceProperties()
2396 module.AddProperties(&module.sdkLibraryProperties)
Sundong Ahn054b19a2018-10-19 13:46:09 +09002397
Paul Duffin71b33cc2021-06-23 11:39:47 +01002398 module.initSdkLibraryComponent(module)
Paul Duffin859fe962020-05-15 10:20:31 +01002399
Paul Duffina18abc22020-05-16 18:54:24 +01002400 module.properties.Installable = proptools.BoolPtr(true)
2401 module.deviceProperties.IsSDKLibrary = true
Inseob Kimc0907f12019-02-08 21:00:45 +09002402}
Sundong Ahn054b19a2018-10-19 13:46:09 +09002403
Paul Duffindfa131e2020-05-15 20:37:11 +01002404func (module *SdkLibrary) requiresRuntimeImplementationLibrary() bool {
2405 return !proptools.Bool(module.sdkLibraryProperties.Api_only)
2406}
2407
Jiyong Park932cdfe2020-05-28 00:19:53 +09002408func (module *SdkLibrary) defaultsToStubs() bool {
2409 return proptools.Bool(module.sdkLibraryProperties.Default_to_stubs)
2410}
2411
Paul Duffin1b1e8062020-05-08 13:44:43 +01002412// Defines how to name the individual component modules the sdk library creates.
2413type sdkLibraryComponentNamingScheme interface {
2414 stubsLibraryModuleName(scope *apiScope, baseName string) string
2415
2416 stubsSourceModuleName(scope *apiScope, baseName string) string
Jihoon Kang1c92c3e2023-03-23 17:44:51 +00002417
2418 apiLibraryModuleName(scope *apiScope, baseName string) string
Jihoon Kang1147b312023-06-08 23:25:57 +00002419
Jihoon Kangfa4a90d2023-12-20 02:53:38 +00002420 sourceStubsLibraryModuleName(scope *apiScope, baseName string) string
2421
2422 exportableStubsLibraryModuleName(scope *apiScope, baseName string) string
2423
2424 exportableSourceStubsLibraryModuleName(scope *apiScope, baseName string) string
Paul Duffin1b1e8062020-05-08 13:44:43 +01002425}
2426
2427type defaultNamingScheme struct {
2428}
2429
2430func (s *defaultNamingScheme) stubsLibraryModuleName(scope *apiScope, baseName string) string {
2431 return scope.stubsLibraryModuleName(baseName)
2432}
2433
2434func (s *defaultNamingScheme) stubsSourceModuleName(scope *apiScope, baseName string) string {
2435 return scope.stubsSourceModuleName(baseName)
2436}
2437
Jihoon Kang1c92c3e2023-03-23 17:44:51 +00002438func (s *defaultNamingScheme) apiLibraryModuleName(scope *apiScope, baseName string) string {
2439 return scope.apiLibraryModuleName(baseName)
2440}
2441
Jihoon Kangfa4a90d2023-12-20 02:53:38 +00002442func (s *defaultNamingScheme) sourceStubsLibraryModuleName(scope *apiScope, baseName string) string {
Jihoon Kang1147b312023-06-08 23:25:57 +00002443 return scope.sourceStubLibraryModuleName(baseName)
2444}
2445
Jihoon Kangfa4a90d2023-12-20 02:53:38 +00002446func (s *defaultNamingScheme) exportableStubsLibraryModuleName(scope *apiScope, baseName string) string {
2447 return scope.exportableStubsLibraryModuleName(baseName)
2448}
2449
2450func (s *defaultNamingScheme) exportableSourceStubsLibraryModuleName(scope *apiScope, baseName string) string {
2451 return scope.exportableSourceStubsLibraryModuleName(baseName)
2452}
2453
Paul Duffin1b1e8062020-05-08 13:44:43 +01002454var _ sdkLibraryComponentNamingScheme = (*defaultNamingScheme)(nil)
2455
Jihoon Kangfa4a90d2023-12-20 02:53:38 +00002456func hasStubsLibrarySuffix(name string, apiScope *apiScope) bool {
2457 return strings.HasSuffix(name, apiScope.stubsLibraryModuleNameSuffix()) ||
2458 strings.HasSuffix(name, apiScope.exportableStubsLibraryModuleNameSuffix())
2459}
2460
Jaewoong Jungbc15e3a2021-03-10 17:02:43 -08002461func moduleStubLinkType(name string) (stub bool, ret sdkLinkType) {
Jihoon Kang1147b312023-06-08 23:25:57 +00002462 name = strings.TrimSuffix(name, ".from-source")
2463
Anton Hansson2d0c1942020-05-25 12:20:51 +01002464 // This suffix-based approach is fragile and could potentially mis-trigger.
2465 // TODO(b/155164730): Clean this up when modules no longer reference sdk_lib stubs directly.
Jihoon Kangfa4a90d2023-12-20 02:53:38 +00002466 if hasStubsLibrarySuffix(name, apiScopePublic) {
Anton Hansson08f476b2021-04-07 15:32:19 +01002467 if name == "hwbinder.stubs" || name == "libcore_private.stubs" {
2468 // Due to a previous bug, these modules were not considered stubs, so we retain that.
2469 return false, javaPlatform
2470 }
Anton Hansson2d0c1942020-05-25 12:20:51 +01002471 return true, javaSdk
2472 }
Jihoon Kangfa4a90d2023-12-20 02:53:38 +00002473 if hasStubsLibrarySuffix(name, apiScopeSystem) {
Anton Hansson2d0c1942020-05-25 12:20:51 +01002474 return true, javaSystem
2475 }
Jihoon Kangfa4a90d2023-12-20 02:53:38 +00002476 if hasStubsLibrarySuffix(name, apiScopeModuleLib) {
Anton Hansson2d0c1942020-05-25 12:20:51 +01002477 return true, javaModule
2478 }
Jihoon Kangfa4a90d2023-12-20 02:53:38 +00002479 if hasStubsLibrarySuffix(name, apiScopeTest) {
Anton Hansson2d0c1942020-05-25 12:20:51 +01002480 return true, javaSystem
2481 }
Jihoon Kangfa4a90d2023-12-20 02:53:38 +00002482 if hasStubsLibrarySuffix(name, apiScopeSystemServer) {
Jihoon Kang1147b312023-06-08 23:25:57 +00002483 return true, javaSystemServer
2484 }
Anton Hansson2d0c1942020-05-25 12:20:51 +01002485 return false, javaPlatform
2486}
2487
Jaewoong Jung4f158ee2019-07-11 10:05:35 -07002488// java_sdk_library is a special Java library that provides optional platform APIs to apps.
2489// In practice, it can be viewed as a combination of several modules: 1) stubs library that clients
2490// are linked against to, 2) droiddoc module that internally generates API stubs source files,
2491// 3) the real runtime shared library that implements the APIs, and 4) XML file for adding
2492// the runtime lib to the classpath at runtime if requested via <uses-library>.
Inseob Kimc0907f12019-02-08 21:00:45 +09002493func SdkLibraryFactory() android.Module {
2494 module := &SdkLibrary{}
Paul Duffinc3091c82020-05-08 14:16:20 +01002495
2496 // Initialize information common between source and prebuilt.
Paul Duffin71b33cc2021-06-23 11:39:47 +01002497 module.initCommon(module)
Paul Duffinc3091c82020-05-08 14:16:20 +01002498
Inseob Kimc0907f12019-02-08 21:00:45 +09002499 module.InitSdkLibraryProperties()
Jooyung Han58f26ab2019-12-18 15:34:32 +09002500 android.InitApexModule(module)
Sundong Ahn054b19a2018-10-19 13:46:09 +09002501 InitJavaModule(module, android.HostAndDeviceSupported)
Paul Duffin3375e352020-04-28 10:44:03 +01002502
2503 // Initialize the map from scope to scope specific properties.
2504 scopeToProperties := make(map[*apiScope]*ApiScopeProperties)
2505 for _, scope := range allApiScopes {
2506 scopeToProperties[scope] = scope.scopeSpecificProperties(module)
2507 }
2508 module.scopeToProperties = scopeToProperties
2509
Paul Duffin4911a892020-04-29 23:35:13 +01002510 // Add the properties containing visibility rules so that they are checked.
Paul Duffin5df79302020-05-16 15:52:12 +01002511 android.AddVisibilityProperty(module, "impl_library_visibility", &module.sdkLibraryProperties.Impl_library_visibility)
Paul Duffin4911a892020-04-29 23:35:13 +01002512 android.AddVisibilityProperty(module, "stubs_library_visibility", &module.sdkLibraryProperties.Stubs_library_visibility)
2513 android.AddVisibilityProperty(module, "stubs_source_visibility", &module.sdkLibraryProperties.Stubs_source_visibility)
2514
Paul Duffin1b1e8062020-05-08 13:44:43 +01002515 module.SetDefaultableHook(func(ctx android.DefaultableHookContext) {
Paul Duffindfa131e2020-05-15 20:37:11 +01002516 // If no implementation is required then it cannot be used as a shared library
2517 // either.
2518 if !module.requiresRuntimeImplementationLibrary() {
2519 // If shared_library has been explicitly set to true then it is incompatible
2520 // with api_only: true.
2521 if proptools.Bool(module.commonSdkLibraryProperties.Shared_library) {
2522 ctx.PropertyErrorf("api_only/shared_library", "inconsistent settings, shared_library and api_only cannot both be true")
2523 }
2524 // Set shared_library: false.
2525 module.commonSdkLibraryProperties.Shared_library = proptools.BoolPtr(false)
2526 }
2527
Paul Duffin1b1e8062020-05-08 13:44:43 +01002528 if module.initCommonAfterDefaultsApplied(ctx) {
2529 module.CreateInternalModules(ctx)
2530 }
2531 })
Jiyong Parkc678ad32018-04-10 13:07:10 +09002532 return module
2533}
Colin Cross79c7c262019-04-17 11:11:46 -07002534
2535//
2536// SDK library prebuilts
2537//
2538
Paul Duffin56d44902020-01-31 13:36:25 +00002539// Properties associated with each api scope.
2540type sdkLibraryScopeProperties struct {
Colin Cross79c7c262019-04-17 11:11:46 -07002541 Jars []string `android:"path"`
2542
2543 Sdk_version *string
2544
Colin Cross79c7c262019-04-17 11:11:46 -07002545 // List of shared java libs that this module has dependencies to
2546 Libs []string
Paul Duffin3d1248c2020-04-09 00:10:17 +01002547
Paul Duffinc8782502020-04-29 20:45:27 +01002548 // The stubs source.
Paul Duffin3d1248c2020-04-09 00:10:17 +01002549 Stub_srcs []string `android:"path"`
Paul Duffin1fd005d2020-04-09 01:08:11 +01002550
2551 // The current.txt
Paul Duffin0f8faff2020-05-20 16:18:00 +01002552 Current_api *string `android:"path"`
Paul Duffin1fd005d2020-04-09 01:08:11 +01002553
2554 // The removed.txt
Paul Duffin0f8faff2020-05-20 16:18:00 +01002555 Removed_api *string `android:"path"`
Anton Hanssond78eb762021-09-21 15:25:12 +01002556
2557 // Annotation zip
2558 Annotations *string `android:"path"`
Colin Cross79c7c262019-04-17 11:11:46 -07002559}
2560
Paul Duffin56d44902020-01-31 13:36:25 +00002561type sdkLibraryImportProperties struct {
Paul Duffinfcfd7912020-01-31 17:54:30 +00002562 // List of shared java libs, common to all scopes, that this module has
2563 // dependencies to
2564 Libs []string
Paul Duffin1267d872021-04-16 17:21:36 +01002565
2566 // If set to true, compile dex files for the stubs. Defaults to false.
2567 Compile_dex *bool
Paul Duffin869de142021-07-15 14:14:41 +01002568
2569 // If not empty, classes are restricted to the specified packages and their sub-packages.
Paul Duffin869de142021-07-15 14:14:41 +01002570 Permitted_packages []string
Spandan Das23956d12024-01-19 00:22:22 +00002571
2572 // Name of the source soong module that gets shadowed by this prebuilt
2573 // If unspecified, follows the naming convention that the source module of
2574 // the prebuilt is Name() without "prebuilt_" prefix
2575 Source_module_name *string
Paul Duffin56d44902020-01-31 13:36:25 +00002576}
2577
Paul Duffineedc5d52020-06-12 17:46:39 +01002578type SdkLibraryImport struct {
Colin Cross79c7c262019-04-17 11:11:46 -07002579 android.ModuleBase
2580 android.DefaultableModuleBase
2581 prebuilt android.Prebuilt
Paul Duffindd46f712020-02-10 13:37:10 +00002582 android.ApexModuleBase
Colin Cross79c7c262019-04-17 11:11:46 -07002583
Paul Duffin37856732021-02-26 14:24:15 +00002584 hiddenAPI
Jiakai Zhang204356f2021-09-09 08:12:46 +00002585 dexpreopter
Paul Duffin37856732021-02-26 14:24:15 +00002586
Colin Cross79c7c262019-04-17 11:11:46 -07002587 properties sdkLibraryImportProperties
2588
Paul Duffin46a26a82020-04-07 19:27:04 +01002589 // Map from api scope to the scope specific property structure.
2590 scopeProperties map[*apiScope]*sdkLibraryScopeProperties
2591
Paul Duffin56d44902020-01-31 13:36:25 +00002592 commonToSdkLibraryAndImport
Paul Duffineedc5d52020-06-12 17:46:39 +01002593
2594 // The reference to the implementation library created by the source module.
2595 // Is nil if the source module does not exist.
2596 implLibraryModule *Library
2597
2598 // The reference to the xml permissions module created by the source module.
2599 // Is nil if the source module does not exist.
2600 xmlPermissionsFileModule *sdkLibraryXml
Paul Duffin39853512021-02-26 11:09:39 +00002601
Jeongik Chad5fe8782021-07-08 01:13:11 +09002602 // Build path to the dex implementation jar obtained from the prebuilt_apex, if any.
Spandan Dasfae468e2023-12-12 23:23:53 +00002603 dexJarFile OptionalDexJarPath
2604 dexJarFileErr error
Jeongik Chad5fe8782021-07-08 01:13:11 +09002605
2606 // Expected install file path of the source module(sdk_library)
2607 // or dex implementation jar obtained from the prebuilt_apex, if any.
2608 installFile android.Path
Colin Cross79c7c262019-04-17 11:11:46 -07002609}
2610
Paul Duffineedc5d52020-06-12 17:46:39 +01002611var _ SdkLibraryDependency = (*SdkLibraryImport)(nil)
Colin Cross79c7c262019-04-17 11:11:46 -07002612
Paul Duffin46a26a82020-04-07 19:27:04 +01002613// The type of a structure that contains a field of type sdkLibraryScopeProperties
2614// for each apiscope in allApiScopes, e.g. something like:
Colin Crossd079e0b2022-08-16 10:27:33 -07002615//
2616// struct {
2617// Public sdkLibraryScopeProperties
2618// System sdkLibraryScopeProperties
2619// ...
2620// }
Paul Duffin46a26a82020-04-07 19:27:04 +01002621var allScopeStructType = createAllScopePropertiesStructType()
2622
2623// Dynamically create a structure type for each apiscope in allApiScopes.
2624func createAllScopePropertiesStructType() reflect.Type {
2625 var fields []reflect.StructField
2626 for _, apiScope := range allApiScopes {
2627 field := reflect.StructField{
2628 Name: apiScope.fieldName,
2629 Type: reflect.TypeOf(sdkLibraryScopeProperties{}),
2630 }
2631 fields = append(fields, field)
2632 }
2633
2634 return reflect.StructOf(fields)
2635}
2636
2637// Create an instance of the scope specific structure type and return a map
2638// from apiscope to a pointer to each scope specific field.
2639func createPropertiesInstance() (interface{}, map[*apiScope]*sdkLibraryScopeProperties) {
2640 allScopePropertiesPtr := reflect.New(allScopeStructType)
2641 allScopePropertiesStruct := allScopePropertiesPtr.Elem()
2642 scopeProperties := make(map[*apiScope]*sdkLibraryScopeProperties)
2643
2644 for _, apiScope := range allApiScopes {
2645 field := allScopePropertiesStruct.FieldByName(apiScope.fieldName)
2646 scopeProperties[apiScope] = field.Addr().Interface().(*sdkLibraryScopeProperties)
2647 }
2648
2649 return allScopePropertiesPtr.Interface(), scopeProperties
2650}
2651
Jaewoong Jung4f158ee2019-07-11 10:05:35 -07002652// java_sdk_library_import imports a prebuilt java_sdk_library.
Colin Cross79c7c262019-04-17 11:11:46 -07002653func sdkLibraryImportFactory() android.Module {
Paul Duffineedc5d52020-06-12 17:46:39 +01002654 module := &SdkLibraryImport{}
Colin Cross79c7c262019-04-17 11:11:46 -07002655
Paul Duffin46a26a82020-04-07 19:27:04 +01002656 allScopeProperties, scopeToProperties := createPropertiesInstance()
2657 module.scopeProperties = scopeToProperties
Jiakai Zhang9c4dc192023-02-09 00:09:24 +08002658 module.AddProperties(&module.properties, allScopeProperties, &module.importDexpreoptProperties)
Colin Cross79c7c262019-04-17 11:11:46 -07002659
Paul Duffinc3091c82020-05-08 14:16:20 +01002660 // Initialize information common between source and prebuilt.
Paul Duffin71b33cc2021-06-23 11:39:47 +01002661 module.initCommon(module)
Paul Duffinc3091c82020-05-08 14:16:20 +01002662
Paul Duffin0bdcb272020-02-06 15:24:57 +00002663 android.InitPrebuiltModule(module, &[]string{""})
Paul Duffindd46f712020-02-10 13:37:10 +00002664 android.InitApexModule(module)
Colin Cross79c7c262019-04-17 11:11:46 -07002665 InitJavaModule(module, android.HostAndDeviceSupported)
2666
Paul Duffin1b1e8062020-05-08 13:44:43 +01002667 module.SetDefaultableHook(func(mctx android.DefaultableHookContext) {
2668 if module.initCommonAfterDefaultsApplied(mctx) {
2669 module.createInternalModules(mctx)
2670 }
2671 })
Colin Cross79c7c262019-04-17 11:11:46 -07002672 return module
2673}
2674
Paul Duffin630b11e2021-07-15 13:35:26 +01002675var _ PermittedPackagesForUpdatableBootJars = (*SdkLibraryImport)(nil)
2676
2677func (module *SdkLibraryImport) PermittedPackagesForUpdatableBootJars() []string {
2678 return module.properties.Permitted_packages
2679}
2680
Paul Duffineedc5d52020-06-12 17:46:39 +01002681func (module *SdkLibraryImport) Prebuilt() *android.Prebuilt {
Colin Cross79c7c262019-04-17 11:11:46 -07002682 return &module.prebuilt
2683}
2684
Paul Duffineedc5d52020-06-12 17:46:39 +01002685func (module *SdkLibraryImport) Name() string {
Colin Cross79c7c262019-04-17 11:11:46 -07002686 return module.prebuilt.Name(module.ModuleBase.Name())
2687}
2688
Spandan Das23956d12024-01-19 00:22:22 +00002689func (module *SdkLibraryImport) BaseModuleName() string {
2690 return proptools.StringDefault(module.properties.Source_module_name, module.ModuleBase.Name())
2691}
2692
Paul Duffineedc5d52020-06-12 17:46:39 +01002693func (module *SdkLibraryImport) createInternalModules(mctx android.DefaultableHookContext) {
Colin Cross79c7c262019-04-17 11:11:46 -07002694
Paul Duffin50061512020-01-21 16:31:05 +00002695 // If the build is configured to use prebuilts then force this to be preferred.
Jeongik Cha816a23a2020-07-08 01:09:23 +09002696 if mctx.Config().AlwaysUsePrebuiltSdks() {
Paul Duffin50061512020-01-21 16:31:05 +00002697 module.prebuilt.ForcePrefer()
2698 }
2699
Paul Duffin46a26a82020-04-07 19:27:04 +01002700 for apiScope, scopeProperties := range module.scopeProperties {
Paul Duffin56d44902020-01-31 13:36:25 +00002701 if len(scopeProperties.Jars) == 0 {
2702 continue
2703 }
2704
Paul Duffinbbb546b2020-04-09 00:07:11 +01002705 module.createJavaImportForStubs(mctx, apiScope, scopeProperties)
Paul Duffin3d1248c2020-04-09 00:10:17 +01002706
Paul Duffin0f8faff2020-05-20 16:18:00 +01002707 if len(scopeProperties.Stub_srcs) > 0 {
2708 module.createPrebuiltStubsSources(mctx, apiScope, scopeProperties)
2709 }
Jihoon Kang71c86832023-09-13 01:01:53 +00002710
2711 if scopeProperties.Current_api != nil {
2712 module.createPrebuiltApiContribution(mctx, apiScope, scopeProperties)
2713 }
Paul Duffin56d44902020-01-31 13:36:25 +00002714 }
Colin Cross79c7c262019-04-17 11:11:46 -07002715
2716 javaSdkLibraries := javaSdkLibraries(mctx.Config())
2717 javaSdkLibrariesLock.Lock()
2718 defer javaSdkLibrariesLock.Unlock()
2719 *javaSdkLibraries = append(*javaSdkLibraries, module.BaseModuleName())
2720}
2721
Paul Duffineedc5d52020-06-12 17:46:39 +01002722func (module *SdkLibraryImport) createJavaImportForStubs(mctx android.DefaultableHookContext, apiScope *apiScope, scopeProperties *sdkLibraryScopeProperties) {
Paul Duffinbbb546b2020-04-09 00:07:11 +01002723 // Creates a java import for the jar with ".stubs" suffix
2724 props := struct {
Spandan Das23956d12024-01-19 00:22:22 +00002725 Name *string
2726 Source_module_name *string
2727 Created_by_java_sdk_library_name *string
2728 Sdk_version *string
2729 Libs []string
2730 Jars []string
2731 Compile_dex *bool
Jihoon Kangfe914ed2024-02-12 22:49:21 +00002732 Is_stubs_module *bool
Paul Duffinbf4de042022-09-27 12:41:52 +01002733
2734 android.UserSuppliedPrebuiltProperties
Paul Duffinbbb546b2020-04-09 00:07:11 +01002735 }{}
Paul Duffinc3091c82020-05-08 14:16:20 +01002736 props.Name = proptools.StringPtr(module.stubsLibraryModuleName(apiScope))
Spandan Das23956d12024-01-19 00:22:22 +00002737 props.Source_module_name = proptools.StringPtr(apiScope.stubsLibraryModuleName(module.BaseModuleName()))
2738 props.Created_by_java_sdk_library_name = proptools.StringPtr(module.RootLibraryName())
Paul Duffinbbb546b2020-04-09 00:07:11 +01002739 props.Sdk_version = scopeProperties.Sdk_version
2740 // Prepend any of the libs from the legacy public properties to the libs for each of the
2741 // scopes to avoid having to duplicate them in each scope.
2742 props.Libs = append(module.properties.Libs, scopeProperties.Libs...)
2743 props.Jars = scopeProperties.Jars
Paul Duffin1dbe3ca2020-05-16 09:57:59 +01002744
Paul Duffin38b57852020-05-13 16:08:09 +01002745 // The imports are preferred if the java_sdk_library_import is preferred.
Paul Duffinbf4de042022-09-27 12:41:52 +01002746 props.CopyUserSuppliedPropertiesFromPrebuilt(&module.prebuilt)
Paul Duffin859fe962020-05-15 10:20:31 +01002747
Paul Duffin1267d872021-04-16 17:21:36 +01002748 // The imports need to be compiled to dex if the java_sdk_library_import requests it.
Paul Duffinf4600f62021-05-13 22:34:45 +01002749 compileDex := module.properties.Compile_dex
2750 if module.stubLibrariesCompiledForDex() {
2751 compileDex = proptools.BoolPtr(true)
2752 }
2753 props.Compile_dex = compileDex
Jihoon Kangfe914ed2024-02-12 22:49:21 +00002754 props.Is_stubs_module = proptools.BoolPtr(true)
Paul Duffin1267d872021-04-16 17:21:36 +01002755
Paul Duffin859fe962020-05-15 10:20:31 +01002756 mctx.CreateModule(ImportFactory, &props, module.sdkComponentPropertiesForChildLibrary())
Paul Duffinbbb546b2020-04-09 00:07:11 +01002757}
2758
Paul Duffineedc5d52020-06-12 17:46:39 +01002759func (module *SdkLibraryImport) createPrebuiltStubsSources(mctx android.DefaultableHookContext, apiScope *apiScope, scopeProperties *sdkLibraryScopeProperties) {
Paul Duffin3d1248c2020-04-09 00:10:17 +01002760 props := struct {
Spandan Das23956d12024-01-19 00:22:22 +00002761 Name *string
2762 Source_module_name *string
2763 Created_by_java_sdk_library_name *string
2764 Srcs []string
Paul Duffinbf4de042022-09-27 12:41:52 +01002765
2766 android.UserSuppliedPrebuiltProperties
Paul Duffin3d1248c2020-04-09 00:10:17 +01002767 }{}
Paul Duffinc3091c82020-05-08 14:16:20 +01002768 props.Name = proptools.StringPtr(module.stubsSourceModuleName(apiScope))
Spandan Das23956d12024-01-19 00:22:22 +00002769 props.Source_module_name = proptools.StringPtr(apiScope.stubsSourceModuleName(module.BaseModuleName()))
2770 props.Created_by_java_sdk_library_name = proptools.StringPtr(module.RootLibraryName())
Paul Duffin3d1248c2020-04-09 00:10:17 +01002771 props.Srcs = scopeProperties.Stub_srcs
Paul Duffin38b57852020-05-13 16:08:09 +01002772
2773 // The stubs source is preferred if the java_sdk_library_import is preferred.
Paul Duffinbf4de042022-09-27 12:41:52 +01002774 props.CopyUserSuppliedPropertiesFromPrebuilt(&module.prebuilt)
2775
Spandan Das2cc80ba2023-10-27 17:21:52 +00002776 mctx.CreateModule(PrebuiltStubsSourcesFactory, &props, module.sdkComponentPropertiesForChildLibrary())
Paul Duffin3d1248c2020-04-09 00:10:17 +01002777}
2778
Jihoon Kang71c86832023-09-13 01:01:53 +00002779func (module *SdkLibraryImport) createPrebuiltApiContribution(mctx android.DefaultableHookContext, apiScope *apiScope, scopeProperties *sdkLibraryScopeProperties) {
2780 api_file := scopeProperties.Current_api
2781 api_surface := &apiScope.name
2782
2783 props := struct {
Spandan Das23956d12024-01-19 00:22:22 +00002784 Name *string
2785 Source_module_name *string
2786 Created_by_java_sdk_library_name *string
2787 Api_surface *string
2788 Api_file *string
2789 Visibility []string
Jihoon Kang71c86832023-09-13 01:01:53 +00002790 }{}
2791
2792 props.Name = proptools.StringPtr(module.stubsSourceModuleName(apiScope) + ".api.contribution")
Spandan Das23956d12024-01-19 00:22:22 +00002793 props.Source_module_name = proptools.StringPtr(apiScope.stubsSourceModuleName(module.BaseModuleName()) + ".api.contribution")
2794 props.Created_by_java_sdk_library_name = proptools.StringPtr(module.RootLibraryName())
Jihoon Kang71c86832023-09-13 01:01:53 +00002795 props.Api_surface = api_surface
2796 props.Api_file = api_file
2797 props.Visibility = []string{"//visibility:override", "//visibility:public"}
2798
Spandan Das2cc80ba2023-10-27 17:21:52 +00002799 mctx.CreateModule(ApiContributionImportFactory, &props, module.sdkComponentPropertiesForChildLibrary())
Jihoon Kang71c86832023-09-13 01:01:53 +00002800}
2801
Paul Duffin44f1d842020-06-26 20:17:02 +01002802// Add the dependencies on the child module in the component deps mutator so that it
2803// creates references to the prebuilt and not the source modules.
2804func (module *SdkLibraryImport) ComponentDepsMutator(ctx android.BottomUpMutatorContext) {
Paul Duffin46a26a82020-04-07 19:27:04 +01002805 for apiScope, scopeProperties := range module.scopeProperties {
Paul Duffin56d44902020-01-31 13:36:25 +00002806 if len(scopeProperties.Jars) == 0 {
2807 continue
2808 }
2809
2810 // Add dependencies to the prebuilt stubs library
Jihoon Kangb7431552024-01-22 19:40:08 +00002811 ctx.AddVariationDependencies(nil, apiScope.prebuiltStubsTag, android.PrebuiltNameFromSource(module.stubsLibraryModuleName(apiScope)))
Paul Duffin0f8faff2020-05-20 16:18:00 +01002812
2813 if len(scopeProperties.Stub_srcs) > 0 {
2814 // Add dependencies to the prebuilt stubs source library
Paul Duffin864116c2021-04-02 10:24:13 +01002815 ctx.AddVariationDependencies(nil, apiScope.stubsSourceTag, android.PrebuiltNameFromSource(module.stubsSourceModuleName(apiScope)))
Paul Duffin0f8faff2020-05-20 16:18:00 +01002816 }
Paul Duffin56d44902020-01-31 13:36:25 +00002817 }
Paul Duffin44f1d842020-06-26 20:17:02 +01002818}
2819
2820// Add other dependencies as normal.
2821func (module *SdkLibraryImport) DepsMutator(ctx android.BottomUpMutatorContext) {
Paul Duffineedc5d52020-06-12 17:46:39 +01002822
2823 implName := module.implLibraryModuleName()
2824 if ctx.OtherModuleExists(implName) {
2825 ctx.AddVariationDependencies(nil, implLibraryTag, implName)
2826
2827 xmlPermissionsModuleName := module.xmlPermissionsModuleName()
2828 if module.sharedLibrary() && ctx.OtherModuleExists(xmlPermissionsModuleName) {
2829 // Add dependency to the rule for generating the xml permissions file
2830 ctx.AddDependency(module, xmlPermissionsFileTag, xmlPermissionsModuleName)
2831 }
2832 }
Colin Cross79c7c262019-04-17 11:11:46 -07002833}
2834
Jiyong Park45bf82e2020-12-15 22:29:02 +09002835var _ android.ApexModule = (*SdkLibraryImport)(nil)
2836
2837// Implements android.ApexModule
Paul Duffineedc5d52020-06-12 17:46:39 +01002838func (module *SdkLibraryImport) DepIsInSameApex(mctx android.BaseModuleContext, dep android.Module) bool {
2839 depTag := mctx.OtherModuleDependencyTag(dep)
2840 if depTag == xmlPermissionsFileTag {
2841 return true
2842 }
2843
2844 // None of the other dependencies of the java_sdk_library_import are in the same apex
2845 // as the one that references this module.
2846 return false
2847}
2848
Jiyong Park45bf82e2020-12-15 22:29:02 +09002849// Implements android.ApexModule
Dan Albertc8060532020-07-22 22:32:17 -07002850func (module *SdkLibraryImport) ShouldSupportSdkVersion(ctx android.BaseModuleContext,
2851 sdkVersion android.ApiLevel) error {
Jooyung Han749dc692020-04-15 11:03:39 +09002852 // we don't check prebuilt modules for sdk_version
2853 return nil
2854}
2855
Paul Duffinea8f8082021-06-24 13:25:57 +01002856// Implements android.ApexModule
2857func (module *SdkLibraryImport) UniqueApexVariations() bool {
2858 return module.uniqueApexVariations()
2859}
2860
Paul Duffin09817d62022-04-28 17:45:11 +01002861// MinSdkVersion - Implements hiddenAPIModule
Spandan Das8c9ae7e2023-03-03 21:20:36 +00002862func (module *SdkLibraryImport) MinSdkVersion(ctx android.EarlyModuleContext) android.ApiLevel {
2863 return android.NoneApiLevel
Paul Duffin09817d62022-04-28 17:45:11 +01002864}
2865
2866var _ hiddenAPIModule = (*SdkLibraryImport)(nil)
2867
Paul Duffineedc5d52020-06-12 17:46:39 +01002868func (module *SdkLibraryImport) OutputFiles(tag string) (android.Paths, error) {
Paul Duffin1e940d52022-04-29 14:21:25 +01002869 paths, err := module.commonOutputFiles(tag)
2870 if paths != nil || err != nil {
2871 return paths, err
2872 }
2873 if module.implLibraryModule != nil {
2874 return module.implLibraryModule.OutputFiles(tag)
2875 } else {
2876 return nil, nil
2877 }
Paul Duffin46dc45a2020-05-14 15:39:10 +01002878}
2879
Paul Duffineedc5d52020-06-12 17:46:39 +01002880func (module *SdkLibraryImport) GenerateAndroidBuildActions(ctx android.ModuleContext) {
Paul Duffina2ae7e02020-09-11 11:55:00 +01002881 module.generateCommonBuildActions(ctx)
2882
Jeongik Chad5fe8782021-07-08 01:13:11 +09002883 // Assume that source module(sdk_library) is installed in /<sdk_library partition>/framework
2884 module.installFile = android.PathForModuleInstall(ctx, "framework", module.Stem()+".jar")
2885
Paul Duffin0f8faff2020-05-20 16:18:00 +01002886 // Record the paths to the prebuilt stubs library and stubs source.
Colin Cross79c7c262019-04-17 11:11:46 -07002887 ctx.VisitDirectDeps(func(to android.Module) {
2888 tag := ctx.OtherModuleDependencyTag(to)
2889
Paul Duffin0f8faff2020-05-20 16:18:00 +01002890 // Extract information from any of the scope specific dependencies.
2891 if scopeTag, ok := tag.(scopeDependencyTag); ok {
2892 apiScope := scopeTag.apiScope
2893 scopePaths := module.getScopePathsCreateIfNeeded(apiScope)
2894
2895 // Extract information from the dependency. The exact information extracted
2896 // is determined by the nature of the dependency which is determined by the tag.
2897 scopeTag.extractDepInfo(ctx, to, scopePaths)
Paul Duffineedc5d52020-06-12 17:46:39 +01002898 } else if tag == implLibraryTag {
2899 if implLibrary, ok := to.(*Library); ok {
2900 module.implLibraryModule = implLibrary
2901 } else {
2902 ctx.ModuleErrorf("implementation library must be of type *java.Library but was %T", to)
2903 }
2904 } else if tag == xmlPermissionsFileTag {
2905 if xmlPermissionsFileModule, ok := to.(*sdkLibraryXml); ok {
2906 module.xmlPermissionsFileModule = xmlPermissionsFileModule
2907 } else {
2908 ctx.ModuleErrorf("xml permissions file module must be of type *sdkLibraryXml but was %T", to)
2909 }
Colin Cross79c7c262019-04-17 11:11:46 -07002910 }
2911 })
Paul Duffin0f8faff2020-05-20 16:18:00 +01002912
2913 // Populate the scope paths with information from the properties.
2914 for apiScope, scopeProperties := range module.scopeProperties {
2915 if len(scopeProperties.Jars) == 0 {
2916 continue
2917 }
2918
2919 paths := module.getScopePathsCreateIfNeeded(apiScope)
Anton Hanssond78eb762021-09-21 15:25:12 +01002920 paths.annotationsZip = android.OptionalPathForModuleSrc(ctx, scopeProperties.Annotations)
Paul Duffin0f8faff2020-05-20 16:18:00 +01002921 paths.currentApiFilePath = android.OptionalPathForModuleSrc(ctx, scopeProperties.Current_api)
2922 paths.removedApiFilePath = android.OptionalPathForModuleSrc(ctx, scopeProperties.Removed_api)
2923 }
Paul Duffin39853512021-02-26 11:09:39 +00002924
2925 if ctx.Device() {
2926 // If this is a variant created for a prebuilt_apex then use the dex implementation jar
2927 // obtained from the associated deapexer module.
Colin Crossff694a82023-12-13 15:54:49 -08002928 ai, _ := android.ModuleProvider(ctx, android.ApexInfoProvider)
Paul Duffin39853512021-02-26 11:09:39 +00002929 if ai.ForPrebuiltApex {
Paul Duffin39853512021-02-26 11:09:39 +00002930 // Get the path of the dex implementation jar from the `deapexer` module.
Spandan Dasfae468e2023-12-12 23:23:53 +00002931 di, err := android.FindDeapexerProviderForModule(ctx)
2932 if err != nil {
2933 // An error was found, possibly due to multiple apexes in the tree that export this library
2934 // Defer the error till a client tries to call DexJarBuildPath
2935 module.dexJarFileErr = err
Spandan Das3a392012024-01-17 18:26:27 +00002936 module.initHiddenAPIError(err)
Spandan Dasfae468e2023-12-12 23:23:53 +00002937 return
Martin Stjernholm44825602021-09-17 01:44:12 +01002938 }
Spandan Das5be63332023-12-13 00:06:32 +00002939 dexJarFileApexRootRelative := ApexRootRelativePathToJavaLib(module.BaseModuleName())
Jiakai Zhang81e46812023-02-08 21:56:07 +08002940 if dexOutputPath := di.PrebuiltExportPath(dexJarFileApexRootRelative); dexOutputPath != nil {
Martin Stjernholm8be1e6d2021-09-15 03:34:04 +01002941 dexJarFile := makeDexJarPathFromPath(dexOutputPath)
2942 module.dexJarFile = dexJarFile
Jiakai Zhang204356f2021-09-09 08:12:46 +00002943 installPath := android.PathForModuleInPartitionInstall(
Jiakai Zhang81e46812023-02-08 21:56:07 +08002944 ctx, "apex", ai.ApexVariationName, dexJarFileApexRootRelative)
Jiakai Zhang204356f2021-09-09 08:12:46 +00002945 module.installFile = installPath
Martin Stjernholm8be1e6d2021-09-15 03:34:04 +01002946 module.initHiddenAPI(ctx, dexJarFile, module.findScopePaths(apiScopePublic).stubsImplPath[0], nil)
Jiakai Zhang204356f2021-09-09 08:12:46 +00002947
Spandan Dase21a8d42024-01-23 23:56:29 +00002948 module.dexpreopter.installPath = module.dexpreopter.getInstallPath(ctx, android.RemoveOptionalPrebuiltPrefix(ctx.ModuleName()), installPath)
Jiakai Zhang204356f2021-09-09 08:12:46 +00002949 module.dexpreopter.isSDKLibrary = true
Spandan Dase21a8d42024-01-23 23:56:29 +00002950 module.dexpreopter.uncompressedDex = shouldUncompressDex(ctx, android.RemoveOptionalPrebuiltPrefix(ctx.ModuleName()), &module.dexpreopter)
Jiakai Zhang81e46812023-02-08 21:56:07 +08002951
2952 if profilePath := di.PrebuiltExportPath(dexJarFileApexRootRelative + ".prof"); profilePath != nil {
2953 module.dexpreopter.inputProfilePathOnHost = profilePath
2954 }
Paul Duffin39853512021-02-26 11:09:39 +00002955 } else {
2956 // This should never happen as a variant for a prebuilt_apex is only created if the
2957 // prebuilt_apex has been configured to export the java library dex file.
Martin Stjernholm44825602021-09-17 01:44:12 +01002958 ctx.ModuleErrorf("internal error: no dex implementation jar available from prebuilt APEX %s", di.ApexModuleName())
Paul Duffin39853512021-02-26 11:09:39 +00002959 }
2960 }
2961 }
Colin Cross79c7c262019-04-17 11:11:46 -07002962}
2963
Jiyong Parkf1691d22021-03-29 20:11:58 +09002964func (module *SdkLibraryImport) sdkJars(ctx android.BaseModuleContext, sdkVersion android.SdkSpec, headerJars bool) android.Paths {
Paul Duffineedc5d52020-06-12 17:46:39 +01002965
2966 // For consistency with SdkLibrary make the implementation jar available to libraries that
2967 // are within the same APEX.
2968 implLibraryModule := module.implLibraryModule
Colin Cross56a83212020-09-15 18:30:11 -07002969 if implLibraryModule != nil && withinSameApexesAs(ctx, module) {
Paul Duffineedc5d52020-06-12 17:46:39 +01002970 if headerJars {
2971 return implLibraryModule.HeaderJars()
2972 } else {
2973 return implLibraryModule.ImplementationJars()
2974 }
2975 }
2976
Paul Duffin23970f42020-05-20 14:20:02 +01002977 return module.selectHeaderJarsForSdkVersion(ctx, sdkVersion)
Paul Duffin56d44902020-01-31 13:36:25 +00002978}
2979
Colin Cross79c7c262019-04-17 11:11:46 -07002980// to satisfy SdkLibraryDependency interface
Jiyong Parkf1691d22021-03-29 20:11:58 +09002981func (module *SdkLibraryImport) SdkHeaderJars(ctx android.BaseModuleContext, sdkVersion android.SdkSpec) android.Paths {
Colin Cross79c7c262019-04-17 11:11:46 -07002982 // This module is just a wrapper for the prebuilt stubs.
Paul Duffineedc5d52020-06-12 17:46:39 +01002983 return module.sdkJars(ctx, sdkVersion, true)
Colin Cross79c7c262019-04-17 11:11:46 -07002984}
2985
2986// to satisfy SdkLibraryDependency interface
Jiyong Parkf1691d22021-03-29 20:11:58 +09002987func (module *SdkLibraryImport) SdkImplementationJars(ctx android.BaseModuleContext, sdkVersion android.SdkSpec) android.Paths {
Colin Cross79c7c262019-04-17 11:11:46 -07002988 // This module is just a wrapper for the stubs.
Paul Duffineedc5d52020-06-12 17:46:39 +01002989 return module.sdkJars(ctx, sdkVersion, false)
2990}
2991
Ulya Trafimovichdbf31662020-12-17 12:07:54 +00002992// to satisfy UsesLibraryDependency interface
Spandan Das59a4a2b2024-01-09 21:35:56 +00002993func (module *SdkLibraryImport) DexJarBuildPath(ctx android.ModuleErrorfContext) OptionalDexJarPath {
Paul Duffin39853512021-02-26 11:09:39 +00002994 // The dex implementation jar extracted from the .apex file should be used in preference to the
2995 // source.
Spandan Dasfae468e2023-12-12 23:23:53 +00002996 if module.dexJarFileErr != nil {
Spandan Das59a4a2b2024-01-09 21:35:56 +00002997 ctx.ModuleErrorf(module.dexJarFileErr.Error())
Spandan Dasfae468e2023-12-12 23:23:53 +00002998 }
Martin Stjernholm8be1e6d2021-09-15 03:34:04 +01002999 if module.dexJarFile.IsSet() {
Paul Duffin39853512021-02-26 11:09:39 +00003000 return module.dexJarFile
3001 }
Paul Duffineedc5d52020-06-12 17:46:39 +01003002 if module.implLibraryModule == nil {
Martin Stjernholm8be1e6d2021-09-15 03:34:04 +01003003 return makeUnsetDexJarPath()
Paul Duffineedc5d52020-06-12 17:46:39 +01003004 } else {
Spandan Das59a4a2b2024-01-09 21:35:56 +00003005 return module.implLibraryModule.DexJarBuildPath(ctx)
Paul Duffineedc5d52020-06-12 17:46:39 +01003006 }
3007}
3008
Ulya Trafimovichdbf31662020-12-17 12:07:54 +00003009// to satisfy UsesLibraryDependency interface
Ulya Trafimovich31e444e2020-08-14 17:32:16 +01003010func (module *SdkLibraryImport) DexJarInstallPath() android.Path {
Jeongik Chad5fe8782021-07-08 01:13:11 +09003011 return module.installFile
Ulya Trafimovich31e444e2020-08-14 17:32:16 +01003012}
3013
Ulya Trafimovichdbf31662020-12-17 12:07:54 +00003014// to satisfy UsesLibraryDependency interface
3015func (module *SdkLibraryImport) ClassLoaderContexts() dexpreopt.ClassLoaderContextMap {
3016 return nil
3017}
3018
Paul Duffineedc5d52020-06-12 17:46:39 +01003019// to satisfy apex.javaDependency interface
3020func (module *SdkLibraryImport) JacocoReportClassesFile() android.Path {
3021 if module.implLibraryModule == nil {
3022 return nil
3023 } else {
3024 return module.implLibraryModule.JacocoReportClassesFile()
3025 }
3026}
3027
3028// to satisfy apex.javaDependency interface
Colin Cross08dca382020-07-21 20:31:17 -07003029func (module *SdkLibraryImport) LintDepSets() LintDepSets {
3030 if module.implLibraryModule == nil {
3031 return LintDepSets{}
3032 } else {
3033 return module.implLibraryModule.LintDepSets()
3034 }
3035}
3036
Spandan Das17854f52022-01-14 21:19:14 +00003037func (module *SdkLibraryImport) GetStrictUpdatabilityLinting() bool {
Jaewoong Jung476b9d62021-05-10 15:30:00 -07003038 if module.implLibraryModule == nil {
3039 return false
3040 } else {
Spandan Das17854f52022-01-14 21:19:14 +00003041 return module.implLibraryModule.GetStrictUpdatabilityLinting()
Jaewoong Jung476b9d62021-05-10 15:30:00 -07003042 }
3043}
3044
Spandan Das17854f52022-01-14 21:19:14 +00003045func (module *SdkLibraryImport) SetStrictUpdatabilityLinting(strictLinting bool) {
Jaewoong Jung476b9d62021-05-10 15:30:00 -07003046 if module.implLibraryModule != nil {
Spandan Das17854f52022-01-14 21:19:14 +00003047 module.implLibraryModule.SetStrictUpdatabilityLinting(strictLinting)
Jaewoong Jung476b9d62021-05-10 15:30:00 -07003048 }
3049}
3050
Colin Cross08dca382020-07-21 20:31:17 -07003051// to satisfy apex.javaDependency interface
Paul Duffineedc5d52020-06-12 17:46:39 +01003052func (module *SdkLibraryImport) Stem() string {
3053 return module.BaseModuleName()
Colin Cross79c7c262019-04-17 11:11:46 -07003054}
Jiyong Parke3833882020-02-17 17:28:10 +09003055
Paul Duffin44b481b2020-06-17 16:59:43 +01003056var _ ApexDependency = (*SdkLibraryImport)(nil)
3057
3058// to satisfy java.ApexDependency interface
3059func (module *SdkLibraryImport) HeaderJars() android.Paths {
3060 if module.implLibraryModule == nil {
3061 return nil
3062 } else {
3063 return module.implLibraryModule.HeaderJars()
3064 }
3065}
3066
3067// to satisfy java.ApexDependency interface
3068func (module *SdkLibraryImport) ImplementationAndResourcesJars() android.Paths {
3069 if module.implLibraryModule == nil {
3070 return nil
3071 } else {
3072 return module.implLibraryModule.ImplementationAndResourcesJars()
3073 }
3074}
3075
Jiakai Zhang204356f2021-09-09 08:12:46 +00003076// to satisfy java.DexpreopterInterface interface
3077func (module *SdkLibraryImport) IsInstallable() bool {
3078 return true
3079}
3080
Paul Duffinfef55002021-06-17 14:56:05 +01003081var _ android.RequiredFilesFromPrebuiltApex = (*SdkLibraryImport)(nil)
3082
Paul Duffinb4bbf2c2021-06-17 15:59:07 +01003083func (module *SdkLibraryImport) RequiredFilesFromPrebuiltApex(ctx android.BaseModuleContext) []string {
Paul Duffinfef55002021-06-17 14:56:05 +01003084 name := module.BaseModuleName()
Jiakai Zhang81e46812023-02-08 21:56:07 +08003085 return requiredFilesFromPrebuiltApexForImport(name, &module.dexpreopter)
Paul Duffinfef55002021-06-17 14:56:05 +01003086}
3087
Spandan Das2ea84dd2024-01-25 22:12:50 +00003088func (j *SdkLibraryImport) UseProfileGuidedDexpreopt() bool {
3089 return proptools.Bool(j.importDexpreoptProperties.Dex_preopt.Profile_guided)
3090}
3091
Jiyong Parke3833882020-02-17 17:28:10 +09003092// java_sdk_library_xml
Jiyong Parke3833882020-02-17 17:28:10 +09003093type sdkLibraryXml struct {
3094 android.ModuleBase
3095 android.DefaultableModuleBase
3096 android.ApexModuleBase
3097
3098 properties sdkLibraryXmlProperties
3099
3100 outputFilePath android.OutputPath
3101 installDirPath android.InstallPath
Colin Cross56a83212020-09-15 18:30:11 -07003102
3103 hideApexVariantFromMake bool
Jiyong Parke3833882020-02-17 17:28:10 +09003104}
3105
3106type sdkLibraryXmlProperties struct {
3107 // canonical name of the lib
3108 Lib_name *string
Pedro Loureiro9956e5e2021-09-07 17:21:59 +00003109
3110 // Signals that this shared library is part of the bootclasspath starting
3111 // on the version indicated in this attribute.
3112 //
3113 // This will make platforms at this level and above to ignore
3114 // <uses-library> tags with this library name because the library is already
3115 // available
3116 On_bootclasspath_since *string
3117
3118 // Signals that this shared library was part of the bootclasspath before
3119 // (but not including) the version indicated in this attribute.
3120 //
3121 // The system will automatically add a <uses-library> tag with this library to
3122 // apps that target any SDK less than the version indicated in this attribute.
3123 On_bootclasspath_before *string
3124
3125 // Indicates that PackageManager should ignore this shared library if the
3126 // platform is below the version indicated in this attribute.
3127 //
3128 // This means that the device won't recognise this library as installed.
3129 Min_device_sdk *string
3130
3131 // Indicates that PackageManager should ignore this shared library if the
3132 // platform is above the version indicated in this attribute.
3133 //
3134 // This means that the device won't recognise this library as installed.
3135 Max_device_sdk *string
Pedro Loureiroc3621422021-09-28 15:40:23 +00003136
3137 // The SdkLibrary's min api level as a string
3138 //
3139 // This value comes from the ApiLevel of the MinSdkVersion property.
3140 Sdk_library_min_api_level *string
Jamie Garsidee570ace2023-11-27 12:07:36 +00003141
3142 // Uses-libs dependencies that the shared library requires to work correctly.
3143 //
3144 // This will add dependency="foo:bar" to the <library> section.
3145 Uses_libs_dependencies []string
Jiyong Parke3833882020-02-17 17:28:10 +09003146}
3147
3148// java_sdk_library_xml builds the permission xml file for a java_sdk_library.
3149// Not to be used directly by users. java_sdk_library internally uses this.
3150func sdkLibraryXmlFactory() android.Module {
3151 module := &sdkLibraryXml{}
3152
3153 module.AddProperties(&module.properties)
3154
3155 android.InitApexModule(module)
3156 android.InitAndroidArchModule(module, android.DeviceSupported, android.MultilibCommon)
3157
3158 return module
3159}
3160
Colin Crossaede88c2020-08-11 12:17:01 -07003161func (module *sdkLibraryXml) UniqueApexVariations() bool {
3162 // sdkLibraryXml needs a unique variation per APEX because the generated XML file contains the path to the
3163 // mounted APEX, which contains the name of the APEX.
3164 return true
3165}
3166
Jiyong Parke3833882020-02-17 17:28:10 +09003167// from android.PrebuiltEtcModule
Jooyung Han0703fd82020-08-26 22:11:53 +09003168func (module *sdkLibraryXml) BaseDir() string {
3169 return "etc"
3170}
3171
3172// from android.PrebuiltEtcModule
Jiyong Parke3833882020-02-17 17:28:10 +09003173func (module *sdkLibraryXml) SubDir() string {
3174 return "permissions"
3175}
3176
3177// from android.PrebuiltEtcModule
ThiƩbaud Weksteen00e8b312024-03-18 14:06:00 +11003178func (module *sdkLibraryXml) OutputFiles(tag string) (android.Paths, error) {
3179 return android.OutputPaths{module.outputFilePath}.Paths(), nil
Jiyong Parke3833882020-02-17 17:28:10 +09003180}
3181
ThiƩbaud Weksteen00e8b312024-03-18 14:06:00 +11003182var _ etc.PrebuiltEtcModule = (*sdkLibraryXml)(nil)
3183
Jiyong Parke3833882020-02-17 17:28:10 +09003184// from android.ApexModule
3185func (module *sdkLibraryXml) AvailableFor(what string) bool {
3186 return true
3187}
3188
3189func (module *sdkLibraryXml) DepsMutator(ctx android.BottomUpMutatorContext) {
3190 // do nothing
3191}
3192
Jiyong Park45bf82e2020-12-15 22:29:02 +09003193var _ android.ApexModule = (*sdkLibraryXml)(nil)
3194
3195// Implements android.ApexModule
Dan Albertc8060532020-07-22 22:32:17 -07003196func (module *sdkLibraryXml) ShouldSupportSdkVersion(ctx android.BaseModuleContext,
3197 sdkVersion android.ApiLevel) error {
Jooyung Han749dc692020-04-15 11:03:39 +09003198 // sdkLibraryXml doesn't need to be checked separately because java_sdk_library is checked
3199 return nil
3200}
3201
Jiyong Parke3833882020-02-17 17:28:10 +09003202// File path to the runtime implementation library
Colin Cross56a83212020-09-15 18:30:11 -07003203func (module *sdkLibraryXml) implPath(ctx android.ModuleContext) string {
Jiyong Parke3833882020-02-17 17:28:10 +09003204 implName := proptools.String(module.properties.Lib_name)
Colin Crossff694a82023-12-13 15:54:49 -08003205 if apexInfo, _ := android.ModuleProvider(ctx, android.ApexInfoProvider); !apexInfo.IsForPlatform() {
Colin Crosse07f2312020-08-13 11:24:56 -07003206 // TODO(b/146468504): ApexVariationName() is only a soong module name, not apex name.
Jiyong Parke3833882020-02-17 17:28:10 +09003207 // In most cases, this works fine. But when apex_name is set or override_apex is used
3208 // this can be wrong.
Colin Cross56a83212020-09-15 18:30:11 -07003209 return fmt.Sprintf("/apex/%s/javalib/%s.jar", apexInfo.ApexVariationName, implName)
Jiyong Parke3833882020-02-17 17:28:10 +09003210 }
3211 partition := "system"
3212 if module.SocSpecific() {
3213 partition = "vendor"
3214 } else if module.DeviceSpecific() {
3215 partition = "odm"
3216 } else if module.ProductSpecific() {
3217 partition = "product"
3218 } else if module.SystemExtSpecific() {
3219 partition = "system_ext"
3220 }
3221 return "/" + partition + "/framework/" + implName + ".jar"
3222}
3223
Pedro Loureiro9956e5e2021-09-07 17:21:59 +00003224func formattedOptionalSdkLevelAttribute(ctx android.ModuleContext, attrName string, value *string) string {
3225 if value == nil {
3226 return ""
3227 }
3228 apiLevel, err := android.ApiLevelFromUser(ctx, *value)
3229 if err != nil {
Pedro Loureiroba6682f2021-10-29 09:32:32 +00003230 // attributes in bp files have underscores but in the xml have dashes.
3231 ctx.PropertyErrorf(strings.ReplaceAll(attrName, "-", "_"), err.Error())
Pedro Loureiro9956e5e2021-09-07 17:21:59 +00003232 return ""
3233 }
Pedro Loureirob638c622021-12-22 15:28:05 +00003234 if apiLevel.IsCurrent() {
3235 // passing "current" would always mean a future release, never the current (or the current in
3236 // progress) which means some conditions would never be triggered.
3237 ctx.PropertyErrorf(strings.ReplaceAll(attrName, "-", "_"),
3238 `"current" is not an allowed value for this attribute`)
3239 return ""
3240 }
Pedro Loureiro48991222022-06-17 20:01:21 +00003241 // "safeValue" is safe because it translates finalized codenames to a string
3242 // with their SDK int.
3243 safeValue := apiLevel.String()
3244 return formattedOptionalAttribute(attrName, &safeValue)
Pedro Loureiro9956e5e2021-09-07 17:21:59 +00003245}
3246
3247// formats an attribute for the xml permissions file if the value is not null
3248// returns empty string otherwise
3249func formattedOptionalAttribute(attrName string, value *string) string {
3250 if value == nil {
3251 return ""
3252 }
3253 return fmt.Sprintf(` %s=\"%s\"\n`, attrName, *value)
3254}
3255
Jamie Garsidee570ace2023-11-27 12:07:36 +00003256func formattedDependenciesAttribute(dependencies []string) string {
3257 if dependencies == nil {
3258 return ""
3259 }
3260 return fmt.Sprintf(` dependency=\"%s\"\n`, strings.Join(dependencies, ":"))
3261}
3262
Pedro Loureiro9956e5e2021-09-07 17:21:59 +00003263func (module *sdkLibraryXml) permissionsContents(ctx android.ModuleContext) string {
3264 libName := proptools.String(module.properties.Lib_name)
3265 libNameAttr := formattedOptionalAttribute("name", &libName)
3266 filePath := module.implPath(ctx)
3267 filePathAttr := formattedOptionalAttribute("file", &filePath)
Pedro Loureiroba6682f2021-10-29 09:32:32 +00003268 implicitFromAttr := formattedOptionalSdkLevelAttribute(ctx, "on-bootclasspath-since", module.properties.On_bootclasspath_since)
3269 implicitUntilAttr := formattedOptionalSdkLevelAttribute(ctx, "on-bootclasspath-before", module.properties.On_bootclasspath_before)
3270 minSdkAttr := formattedOptionalSdkLevelAttribute(ctx, "min-device-sdk", module.properties.Min_device_sdk)
3271 maxSdkAttr := formattedOptionalSdkLevelAttribute(ctx, "max-device-sdk", module.properties.Max_device_sdk)
Jamie Garsidee570ace2023-11-27 12:07:36 +00003272 dependenciesAttr := formattedDependenciesAttribute(module.properties.Uses_libs_dependencies)
Pedro Loureiro196d3e62021-12-22 19:53:01 +00003273 // <library> is understood in all android versions whereas <apex-library> is only understood from API T (and ignored before that).
3274 // 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 +00003275 var libraryTag string
3276 if module.properties.Min_device_sdk != nil {
Pedro Loureiro196d3e62021-12-22 19:53:01 +00003277 libraryTag = ` <apex-library\n`
Pedro Loureiroc3621422021-09-28 15:40:23 +00003278 } else {
3279 libraryTag = ` <library\n`
3280 }
Pedro Loureiro9956e5e2021-09-07 17:21:59 +00003281
3282 return strings.Join([]string{
3283 `<?xml version=\"1.0\" encoding=\"utf-8\"?>\n`,
3284 `<!-- Copyright (C) 2018 The Android Open Source Project\n`,
3285 `\n`,
3286 ` Licensed under the Apache License, Version 2.0 (the \"License\");\n`,
3287 ` you may not use this file except in compliance with the License.\n`,
3288 ` You may obtain a copy of the License at\n`,
3289 `\n`,
3290 ` http://www.apache.org/licenses/LICENSE-2.0\n`,
3291 `\n`,
3292 ` Unless required by applicable law or agreed to in writing, software\n`,
3293 ` distributed under the License is distributed on an \"AS IS\" BASIS,\n`,
3294 ` WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\n`,
3295 ` See the License for the specific language governing permissions and\n`,
3296 ` limitations under the License.\n`,
3297 `-->\n`,
3298 `<permissions>\n`,
Pedro Loureiroc3621422021-09-28 15:40:23 +00003299 libraryTag,
Pedro Loureiro9956e5e2021-09-07 17:21:59 +00003300 libNameAttr,
3301 filePathAttr,
3302 implicitFromAttr,
3303 implicitUntilAttr,
3304 minSdkAttr,
3305 maxSdkAttr,
Jamie Garsidee570ace2023-11-27 12:07:36 +00003306 dependenciesAttr,
Pedro Loureiro9956e5e2021-09-07 17:21:59 +00003307 ` />\n`,
3308 `</permissions>\n`}, "")
3309}
3310
Jiyong Parke3833882020-02-17 17:28:10 +09003311func (module *sdkLibraryXml) GenerateAndroidBuildActions(ctx android.ModuleContext) {
Colin Crossff694a82023-12-13 15:54:49 -08003312 apexInfo, _ := android.ModuleProvider(ctx, android.ApexInfoProvider)
3313 module.hideApexVariantFromMake = !apexInfo.IsForPlatform()
Colin Cross56a83212020-09-15 18:30:11 -07003314
Jiyong Parke3833882020-02-17 17:28:10 +09003315 libName := proptools.String(module.properties.Lib_name)
Pedro Loureiroc3621422021-09-28 15:40:23 +00003316 module.selfValidate(ctx)
Pedro Loureiro9956e5e2021-09-07 17:21:59 +00003317 xmlContent := module.permissionsContents(ctx)
Jiyong Parke3833882020-02-17 17:28:10 +09003318
3319 module.outputFilePath = android.PathForModuleOut(ctx, libName+".xml").OutputPath
Colin Crossf1a035e2020-11-16 17:32:30 -08003320 rule := android.NewRuleBuilder(pctx, ctx)
Jiyong Parke3833882020-02-17 17:28:10 +09003321 rule.Command().
3322 Text("/bin/bash -c \"echo -e '" + xmlContent + "'\" > ").
3323 Output(module.outputFilePath)
3324
Colin Crossf1a035e2020-11-16 17:32:30 -08003325 rule.Build("java_sdk_xml", "Permission XML")
Jiyong Parke3833882020-02-17 17:28:10 +09003326
3327 module.installDirPath = android.PathForModuleInstall(ctx, "etc", module.SubDir())
3328}
3329
3330func (module *sdkLibraryXml) AndroidMkEntries() []android.AndroidMkEntries {
Colin Cross56a83212020-09-15 18:30:11 -07003331 if module.hideApexVariantFromMake {
satayev8f088b02021-12-06 11:40:46 +00003332 return []android.AndroidMkEntries{{
Jiyong Parke3833882020-02-17 17:28:10 +09003333 Disabled: true,
3334 }}
3335 }
3336
satayev8f088b02021-12-06 11:40:46 +00003337 return []android.AndroidMkEntries{{
Jiyong Parke3833882020-02-17 17:28:10 +09003338 Class: "ETC",
3339 OutputFile: android.OptionalPathForPath(module.outputFilePath),
3340 ExtraEntries: []android.AndroidMkExtraEntriesFunc{
Colin Crossaa255532020-07-03 13:18:24 -07003341 func(ctx android.AndroidMkExtraEntriesContext, entries *android.AndroidMkEntries) {
Jiyong Parke3833882020-02-17 17:28:10 +09003342 entries.SetString("LOCAL_MODULE_TAGS", "optional")
Colin Crossc68db4b2021-11-11 18:59:15 -08003343 entries.SetString("LOCAL_MODULE_PATH", module.installDirPath.String())
Jiyong Parke3833882020-02-17 17:28:10 +09003344 entries.SetString("LOCAL_INSTALLED_MODULE_STEM", module.outputFilePath.Base())
3345 },
3346 },
3347 }}
3348}
Paul Duffindd46f712020-02-10 13:37:10 +00003349
Pedro Loureiroc3621422021-09-28 15:40:23 +00003350func (module *sdkLibraryXml) selfValidate(ctx android.ModuleContext) {
3351 module.validateAtLeastTAttributes(ctx)
3352 module.validateMinAndMaxDeviceSdk(ctx)
3353 module.validateMinMaxDeviceSdkAndModuleMinSdk(ctx)
3354 module.validateOnBootclasspathBeforeRequirements(ctx)
3355}
3356
3357func (module *sdkLibraryXml) validateAtLeastTAttributes(ctx android.ModuleContext) {
3358 t := android.ApiLevelOrPanic(ctx, "Tiramisu")
3359 module.attrAtLeastT(ctx, t, module.properties.Min_device_sdk, "min_device_sdk")
3360 module.attrAtLeastT(ctx, t, module.properties.Max_device_sdk, "max_device_sdk")
3361 module.attrAtLeastT(ctx, t, module.properties.On_bootclasspath_before, "on_bootclasspath_before")
3362 module.attrAtLeastT(ctx, t, module.properties.On_bootclasspath_since, "on_bootclasspath_since")
3363}
3364
3365func (module *sdkLibraryXml) attrAtLeastT(ctx android.ModuleContext, t android.ApiLevel, attr *string, attrName string) {
3366 if attr != nil {
3367 if level, err := android.ApiLevelFromUser(ctx, *attr); err == nil {
3368 // we will inform the user of invalid inputs when we try to write the
3369 // permissions xml file so we don't need to do it here
3370 if t.GreaterThan(level) {
3371 ctx.PropertyErrorf(attrName, "Attribute value needs to be at least T")
3372 }
3373 }
3374 }
3375}
3376
3377func (module *sdkLibraryXml) validateMinAndMaxDeviceSdk(ctx android.ModuleContext) {
3378 if module.properties.Min_device_sdk != nil && module.properties.Max_device_sdk != nil {
3379 min, minErr := android.ApiLevelFromUser(ctx, *module.properties.Min_device_sdk)
3380 max, maxErr := android.ApiLevelFromUser(ctx, *module.properties.Max_device_sdk)
3381 if minErr == nil && maxErr == nil {
3382 // we will inform the user of invalid inputs when we try to write the
3383 // permissions xml file so we don't need to do it here
3384 if min.GreaterThan(max) {
3385 ctx.ModuleErrorf("min_device_sdk can't be greater than max_device_sdk")
3386 }
3387 }
3388 }
3389}
3390
3391func (module *sdkLibraryXml) validateMinMaxDeviceSdkAndModuleMinSdk(ctx android.ModuleContext) {
3392 moduleMinApi := android.ApiLevelOrPanic(ctx, *module.properties.Sdk_library_min_api_level)
3393 if module.properties.Min_device_sdk != nil {
3394 api, err := android.ApiLevelFromUser(ctx, *module.properties.Min_device_sdk)
3395 if err == nil {
3396 if moduleMinApi.GreaterThan(api) {
3397 ctx.PropertyErrorf("min_device_sdk", "Can't be less than module's min sdk (%s)", moduleMinApi)
3398 }
3399 }
3400 }
3401 if module.properties.Max_device_sdk != nil {
3402 api, err := android.ApiLevelFromUser(ctx, *module.properties.Max_device_sdk)
3403 if err == nil {
3404 if moduleMinApi.GreaterThan(api) {
3405 ctx.PropertyErrorf("max_device_sdk", "Can't be less than module's min sdk (%s)", moduleMinApi)
3406 }
3407 }
3408 }
3409}
3410
3411func (module *sdkLibraryXml) validateOnBootclasspathBeforeRequirements(ctx android.ModuleContext) {
3412 moduleMinApi := android.ApiLevelOrPanic(ctx, *module.properties.Sdk_library_min_api_level)
3413 if module.properties.On_bootclasspath_before != nil {
3414 t := android.ApiLevelOrPanic(ctx, "Tiramisu")
3415 // if we use the attribute, then we need to do this validation
3416 if moduleMinApi.LessThan(t) {
3417 // if minAPi is < T, then we need to have min_device_sdk (which only accepts T+)
3418 if module.properties.Min_device_sdk == nil {
3419 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")
3420 }
3421 }
3422 }
3423}
3424
Paul Duffindd46f712020-02-10 13:37:10 +00003425type sdkLibrarySdkMemberType struct {
3426 android.SdkMemberTypeBase
3427}
3428
Paul Duffin296701e2021-07-14 10:29:36 +01003429func (s *sdkLibrarySdkMemberType) AddDependencies(ctx android.SdkDependencyContext, dependencyTag blueprint.DependencyTag, names []string) {
3430 ctx.AddVariationDependencies(nil, dependencyTag, names...)
Paul Duffindd46f712020-02-10 13:37:10 +00003431}
3432
3433func (s *sdkLibrarySdkMemberType) IsInstance(module android.Module) bool {
3434 _, ok := module.(*SdkLibrary)
3435 return ok
3436}
3437
3438func (s *sdkLibrarySdkMemberType) AddPrebuiltModule(ctx android.SdkMemberContext, member android.SdkMember) android.BpModule {
3439 return ctx.SnapshotBuilder().AddPrebuiltModule(member, "java_sdk_library_import")
3440}
3441
3442func (s *sdkLibrarySdkMemberType) CreateVariantPropertiesStruct() android.SdkMemberProperties {
3443 return &sdkLibrarySdkMemberProperties{}
3444}
3445
Paul Duffin976b0e52021-04-27 23:20:26 +01003446var javaSdkLibrarySdkMemberType = &sdkLibrarySdkMemberType{
3447 android.SdkMemberTypeBase{
3448 PropertyName: "java_sdk_libs",
3449 SupportsSdk: true,
3450 },
3451}
3452
Paul Duffindd46f712020-02-10 13:37:10 +00003453type sdkLibrarySdkMemberProperties struct {
3454 android.SdkMemberPropertiesBase
3455
Paul Duffine8409952022-09-22 16:24:46 +01003456 // Stem name for files in the sdk snapshot.
3457 //
3458 // This is used to construct the path names of various sdk library files in the sdk snapshot to
3459 // make sure that they match the finalized versions of those files in prebuilts/sdk.
3460 //
3461 // This property is marked as keep so that it will be kept in all instances of this struct, will
3462 // not be cleared but will be copied to common structs. That is needed because this field is used
3463 // to construct many file names for other parts of this struct and so it needs to be present in
3464 // all structs. If it was not marked as keep then it would be cleared in some structs and so would
3465 // be unavailable for generating file names if there were other properties that were still set.
3466 Stem string `sdk:"keep"`
3467
Paul Duffindd46f712020-02-10 13:37:10 +00003468 // Scope to per scope properties.
Paul Duffin106a3a42022-01-27 16:39:06 +00003469 Scopes map[*apiScope]*scopeProperties
Paul Duffindd46f712020-02-10 13:37:10 +00003470
Paul Duffin3d1248c2020-04-09 00:10:17 +01003471 // The Java stubs source files.
3472 Stub_srcs []string
Paul Duffinf7a64332020-05-13 16:54:55 +01003473
3474 // The naming scheme.
3475 Naming_scheme *string
Paul Duffind7eb1c22020-05-26 20:57:10 +01003476
3477 // True if the java_sdk_library_import is for a shared library, false
3478 // otherwise.
3479 Shared_library *bool
Paul Duffina2ae7e02020-09-11 11:55:00 +01003480
Paul Duffin1267d872021-04-16 17:21:36 +01003481 // True if the stub imports should produce dex jars.
3482 Compile_dex *bool
3483
Paul Duffina2ae7e02020-09-11 11:55:00 +01003484 // The paths to the doctag files to add to the prebuilt.
3485 Doctag_paths android.Paths
Paul Duffin869de142021-07-15 14:14:41 +01003486
3487 Permitted_packages []string
Pedro Loureiro9956e5e2021-09-07 17:21:59 +00003488
3489 // Signals that this shared library is part of the bootclasspath starting
3490 // on the version indicated in this attribute.
3491 //
3492 // This will make platforms at this level and above to ignore
3493 // <uses-library> tags with this library name because the library is already
3494 // available
3495 On_bootclasspath_since *string
3496
3497 // Signals that this shared library was part of the bootclasspath before
3498 // (but not including) the version indicated in this attribute.
3499 //
3500 // The system will automatically add a <uses-library> tag with this library to
3501 // apps that target any SDK less than the version indicated in this attribute.
3502 On_bootclasspath_before *string
3503
3504 // Indicates that PackageManager should ignore this shared library if the
3505 // platform is below the version indicated in this attribute.
3506 //
3507 // This means that the device won't recognise this library as installed.
3508 Min_device_sdk *string
3509
3510 // Indicates that PackageManager should ignore this shared library if the
3511 // platform is above the version indicated in this attribute.
3512 //
3513 // This means that the device won't recognise this library as installed.
3514 Max_device_sdk *string
Jiakai Zhang9c4dc192023-02-09 00:09:24 +08003515
3516 DexPreoptProfileGuided *bool `supported_build_releases:"UpsideDownCake+"`
Paul Duffindd46f712020-02-10 13:37:10 +00003517}
3518
3519type scopeProperties struct {
Paul Duffin1fd005d2020-04-09 01:08:11 +01003520 Jars android.Paths
3521 StubsSrcJar android.Path
3522 CurrentApiFile android.Path
3523 RemovedApiFile android.Path
Paul Duffine7babdb2022-02-10 13:06:54 +00003524 AnnotationsZip android.Path `supported_build_releases:"Tiramisu+"`
Paul Duffin1fd005d2020-04-09 01:08:11 +01003525 SdkVersion string
Paul Duffindd46f712020-02-10 13:37:10 +00003526}
3527
3528func (s *sdkLibrarySdkMemberProperties) PopulateFromVariant(ctx android.SdkMemberContext, variant android.Module) {
3529 sdk := variant.(*SdkLibrary)
3530
Paul Duffine8409952022-09-22 16:24:46 +01003531 // Copy the stem name for files in the sdk snapshot.
3532 s.Stem = sdk.distStem()
3533
Paul Duffin106a3a42022-01-27 16:39:06 +00003534 s.Scopes = make(map[*apiScope]*scopeProperties)
Paul Duffindd46f712020-02-10 13:37:10 +00003535 for _, apiScope := range allApiScopes {
Paul Duffin803a9562020-05-20 11:52:25 +01003536 paths := sdk.findScopePaths(apiScope)
3537 if paths == nil {
3538 continue
3539 }
3540
Paul Duffindd46f712020-02-10 13:37:10 +00003541 jars := paths.stubsImplPath
3542 if len(jars) > 0 {
3543 properties := scopeProperties{}
3544 properties.Jars = jars
Paul Duffin780c5f42020-05-12 15:52:55 +01003545 properties.SdkVersion = sdk.sdkVersionForStubsLibrary(ctx.SdkModuleContext(), apiScope)
Paul Duffin0f8faff2020-05-20 16:18:00 +01003546 properties.StubsSrcJar = paths.stubsSrcJar.Path()
Paul Duffin10269f12020-06-19 18:39:55 +01003547 if paths.currentApiFilePath.Valid() {
3548 properties.CurrentApiFile = paths.currentApiFilePath.Path()
3549 }
3550 if paths.removedApiFilePath.Valid() {
3551 properties.RemovedApiFile = paths.removedApiFilePath.Path()
3552 }
Anton Hanssond78eb762021-09-21 15:25:12 +01003553 // The annotations zip is only available for modules that set annotations_enabled: true.
3554 if paths.annotationsZip.Valid() {
3555 properties.AnnotationsZip = paths.annotationsZip.Path()
3556 }
Paul Duffin106a3a42022-01-27 16:39:06 +00003557 s.Scopes[apiScope] = &properties
Paul Duffindd46f712020-02-10 13:37:10 +00003558 }
3559 }
3560
Paul Duffindfa131e2020-05-15 20:37:11 +01003561 s.Naming_scheme = sdk.commonSdkLibraryProperties.Naming_scheme
Paul Duffind7eb1c22020-05-26 20:57:10 +01003562 s.Shared_library = proptools.BoolPtr(sdk.sharedLibrary())
Paul Duffin1267d872021-04-16 17:21:36 +01003563 s.Compile_dex = sdk.dexProperties.Compile_dex
Paul Duffina2ae7e02020-09-11 11:55:00 +01003564 s.Doctag_paths = sdk.doctagPaths
Paul Duffin869de142021-07-15 14:14:41 +01003565 s.Permitted_packages = sdk.PermittedPackagesForUpdatableBootJars()
Pedro Loureiro9956e5e2021-09-07 17:21:59 +00003566 s.On_bootclasspath_since = sdk.commonSdkLibraryProperties.On_bootclasspath_since
3567 s.On_bootclasspath_before = sdk.commonSdkLibraryProperties.On_bootclasspath_before
3568 s.Min_device_sdk = sdk.commonSdkLibraryProperties.Min_device_sdk
3569 s.Max_device_sdk = sdk.commonSdkLibraryProperties.Max_device_sdk
Jiakai Zhang9c4dc192023-02-09 00:09:24 +08003570
3571 if sdk.dexpreopter.dexpreoptProperties.Dex_preopt_result.Profile_guided {
3572 s.DexPreoptProfileGuided = proptools.BoolPtr(true)
3573 }
Paul Duffindd46f712020-02-10 13:37:10 +00003574}
3575
3576func (s *sdkLibrarySdkMemberProperties) AddToPropertySet(ctx android.SdkMemberContext, propertySet android.BpPropertySet) {
Paul Duffinf7a64332020-05-13 16:54:55 +01003577 if s.Naming_scheme != nil {
3578 propertySet.AddProperty("naming_scheme", proptools.String(s.Naming_scheme))
3579 }
Paul Duffind7eb1c22020-05-26 20:57:10 +01003580 if s.Shared_library != nil {
3581 propertySet.AddProperty("shared_library", *s.Shared_library)
3582 }
Paul Duffin1267d872021-04-16 17:21:36 +01003583 if s.Compile_dex != nil {
3584 propertySet.AddProperty("compile_dex", *s.Compile_dex)
3585 }
Paul Duffin869de142021-07-15 14:14:41 +01003586 if len(s.Permitted_packages) > 0 {
3587 propertySet.AddProperty("permitted_packages", s.Permitted_packages)
3588 }
Jiakai Zhang9c4dc192023-02-09 00:09:24 +08003589 dexPreoptSet := propertySet.AddPropertySet("dex_preopt")
3590 if s.DexPreoptProfileGuided != nil {
3591 dexPreoptSet.AddProperty("profile_guided", proptools.Bool(s.DexPreoptProfileGuided))
3592 }
Paul Duffinf7a64332020-05-13 16:54:55 +01003593
Paul Duffine8409952022-09-22 16:24:46 +01003594 stem := s.Stem
3595
Paul Duffindd46f712020-02-10 13:37:10 +00003596 for _, apiScope := range allApiScopes {
3597 if properties, ok := s.Scopes[apiScope]; ok {
Paul Duffin6b836ba2020-05-13 19:19:49 +01003598 scopeSet := propertySet.AddPropertySet(apiScope.propertyName)
Paul Duffindd46f712020-02-10 13:37:10 +00003599
Paul Duffin958806b2022-05-16 13:10:47 +00003600 scopeDir := apiScope.snapshotRelativeDir()
Paul Duffin3d1248c2020-04-09 00:10:17 +01003601
Paul Duffindd46f712020-02-10 13:37:10 +00003602 var jars []string
3603 for _, p := range properties.Jars {
Paul Duffine8409952022-09-22 16:24:46 +01003604 dest := filepath.Join(scopeDir, stem+"-stubs.jar")
Paul Duffindd46f712020-02-10 13:37:10 +00003605 ctx.SnapshotBuilder().CopyToSnapshot(p, dest)
3606 jars = append(jars, dest)
3607 }
3608 scopeSet.AddProperty("jars", jars)
3609
Paul Duffin22628d52021-05-12 23:13:22 +01003610 if ctx.SdkModuleContext().Config().IsEnvTrue("SOONG_SDK_SNAPSHOT_USE_SRCJAR") {
3611 // Copy the stubs source jar into the snapshot zip as is.
Paul Duffine8409952022-09-22 16:24:46 +01003612 srcJarSnapshotPath := filepath.Join(scopeDir, stem+".srcjar")
Paul Duffin22628d52021-05-12 23:13:22 +01003613 ctx.SnapshotBuilder().CopyToSnapshot(properties.StubsSrcJar, srcJarSnapshotPath)
3614 scopeSet.AddProperty("stub_srcs", []string{srcJarSnapshotPath})
3615 } else {
3616 // Merge the stubs source jar into the snapshot zip so that when it is unpacked
3617 // the source files are also unpacked.
Paul Duffine8409952022-09-22 16:24:46 +01003618 snapshotRelativeDir := filepath.Join(scopeDir, stem+"_stub_sources")
Paul Duffin22628d52021-05-12 23:13:22 +01003619 ctx.SnapshotBuilder().UnzipToSnapshot(properties.StubsSrcJar, snapshotRelativeDir)
3620 scopeSet.AddProperty("stub_srcs", []string{snapshotRelativeDir})
3621 }
Paul Duffin3d1248c2020-04-09 00:10:17 +01003622
Paul Duffin1fd005d2020-04-09 01:08:11 +01003623 if properties.CurrentApiFile != nil {
Paul Duffine8409952022-09-22 16:24:46 +01003624 currentApiSnapshotPath := apiScope.snapshotRelativeCurrentApiTxtPath(stem)
Paul Duffin1fd005d2020-04-09 01:08:11 +01003625 ctx.SnapshotBuilder().CopyToSnapshot(properties.CurrentApiFile, currentApiSnapshotPath)
3626 scopeSet.AddProperty("current_api", currentApiSnapshotPath)
3627 }
3628
3629 if properties.RemovedApiFile != nil {
Paul Duffine8409952022-09-22 16:24:46 +01003630 removedApiSnapshotPath := apiScope.snapshotRelativeRemovedApiTxtPath(stem)
Paul Duffin3dbf9fd2020-06-02 13:00:02 +01003631 ctx.SnapshotBuilder().CopyToSnapshot(properties.RemovedApiFile, removedApiSnapshotPath)
Paul Duffin1fd005d2020-04-09 01:08:11 +01003632 scopeSet.AddProperty("removed_api", removedApiSnapshotPath)
3633 }
3634
Anton Hanssond78eb762021-09-21 15:25:12 +01003635 if properties.AnnotationsZip != nil {
Paul Duffine8409952022-09-22 16:24:46 +01003636 annotationsSnapshotPath := filepath.Join(scopeDir, stem+"_annotations.zip")
Anton Hanssond78eb762021-09-21 15:25:12 +01003637 ctx.SnapshotBuilder().CopyToSnapshot(properties.AnnotationsZip, annotationsSnapshotPath)
3638 scopeSet.AddProperty("annotations", annotationsSnapshotPath)
3639 }
3640
Paul Duffindd46f712020-02-10 13:37:10 +00003641 if properties.SdkVersion != "" {
3642 scopeSet.AddProperty("sdk_version", properties.SdkVersion)
3643 }
3644 }
3645 }
3646
Paul Duffina2ae7e02020-09-11 11:55:00 +01003647 if len(s.Doctag_paths) > 0 {
3648 dests := []string{}
3649 for _, p := range s.Doctag_paths {
3650 dest := filepath.Join("doctags", p.Rel())
3651 ctx.SnapshotBuilder().CopyToSnapshot(p, dest)
3652 dests = append(dests, dest)
3653 }
3654 propertySet.AddProperty("doctag_files", dests)
3655 }
Paul Duffindd46f712020-02-10 13:37:10 +00003656}