blob: e7e53a2a88d538df120a056d2e620f59be60cc0c [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
Jihoon Kang8479dea2024-04-04 01:19:05 +0000952
953 // Path to the header jars of the implementation library
954 // This is non-empty only when api_only is false.
955 implLibraryHeaderJars android.Paths
Paul Duffin56d44902020-01-31 13:36:25 +0000956}
957
Paul Duffin71b33cc2021-06-23 11:39:47 +0100958func (c *commonToSdkLibraryAndImport) initCommon(module commonSdkLibraryAndImportModule) {
959 c.module = module
Paul Duffin1b1e8062020-05-08 13:44:43 +0100960
Paul Duffin71b33cc2021-06-23 11:39:47 +0100961 module.AddProperties(&c.commonSdkLibraryProperties)
Paul Duffin859fe962020-05-15 10:20:31 +0100962
963 // Initialize this as an sdk library component.
Paul Duffin71b33cc2021-06-23 11:39:47 +0100964 c.initSdkLibraryComponent(module)
Paul Duffin1b1e8062020-05-08 13:44:43 +0100965}
966
967func (c *commonToSdkLibraryAndImport) initCommonAfterDefaultsApplied(ctx android.DefaultableHookContext) bool {
Paul Duffindfa131e2020-05-15 20:37:11 +0100968 schemeProperty := proptools.StringDefault(c.commonSdkLibraryProperties.Naming_scheme, "default")
Paul Duffin1b1e8062020-05-08 13:44:43 +0100969 switch schemeProperty {
970 case "default":
971 c.namingScheme = &defaultNamingScheme{}
972 default:
973 ctx.PropertyErrorf("naming_scheme", "expected 'default' but was %q", schemeProperty)
974 return false
975 }
976
Spandan Das23956d12024-01-19 00:22:22 +0000977 namePtr := proptools.StringPtr(c.module.RootLibraryName())
Paul Duffin3f0290e2021-06-30 18:25:36 +0100978 c.sdkLibraryComponentProperties.SdkLibraryName = namePtr
979
Paul Duffindfa131e2020-05-15 20:37:11 +0100980 // Only track this sdk library if this can be used as a shared library.
981 if c.sharedLibrary() {
982 // Use the name specified in the module definition as the owner.
Paul Duffin3f0290e2021-06-30 18:25:36 +0100983 c.sdkLibraryComponentProperties.SdkLibraryToImplicitlyTrack = namePtr
Paul Duffindfa131e2020-05-15 20:37:11 +0100984 }
Paul Duffin859fe962020-05-15 10:20:31 +0100985
Paul Duffin1b1e8062020-05-08 13:44:43 +0100986 return true
Paul Duffinc3091c82020-05-08 14:16:20 +0100987}
988
Paul Duffinea8f8082021-06-24 13:25:57 +0100989// uniqueApexVariations provides common implementation of the ApexModule.UniqueApexVariations
990// method.
991func (c *commonToSdkLibraryAndImport) uniqueApexVariations() bool {
992 // A java_sdk_library that is a shared library produces an XML file that makes the shared library
993 // usable from an AndroidManifest.xml's <uses-library> entry. That XML file contains the name of
994 // the APEX and so it needs a unique variation per APEX.
995 return c.sharedLibrary()
996}
997
Paul Duffina2ae7e02020-09-11 11:55:00 +0100998func (c *commonToSdkLibraryAndImport) generateCommonBuildActions(ctx android.ModuleContext) {
999 c.doctagPaths = android.PathsForModuleSrc(ctx, c.commonSdkLibraryProperties.Doctag_files)
1000}
1001
Paul Duffineedc5d52020-06-12 17:46:39 +01001002// Module name of the runtime implementation library
1003func (c *commonToSdkLibraryAndImport) implLibraryModuleName() string {
Spandan Das23956d12024-01-19 00:22:22 +00001004 return c.module.RootLibraryName() + ".impl"
Paul Duffineedc5d52020-06-12 17:46:39 +01001005}
1006
1007// Module name of the XML file for the lib
1008func (c *commonToSdkLibraryAndImport) xmlPermissionsModuleName() string {
Spandan Das23956d12024-01-19 00:22:22 +00001009 return c.module.RootLibraryName() + sdkXmlFileSuffix
Paul Duffineedc5d52020-06-12 17:46:39 +01001010}
1011
Paul Duffinc3091c82020-05-08 14:16:20 +01001012// Name of the java_library module that compiles the stubs source.
1013func (c *commonToSdkLibraryAndImport) stubsLibraryModuleName(apiScope *apiScope) string {
Spandan Das23956d12024-01-19 00:22:22 +00001014 baseName := c.module.RootLibraryName()
Paul Duffin21787622022-11-25 12:48:20 +00001015 return c.namingScheme.stubsLibraryModuleName(apiScope, baseName)
Paul Duffinc3091c82020-05-08 14:16:20 +01001016}
1017
Jihoon Kangfa4a90d2023-12-20 02:53:38 +00001018// Name of the java_library module that compiles the exportable stubs source.
1019func (c *commonToSdkLibraryAndImport) exportableStubsLibraryModuleName(apiScope *apiScope) string {
Spandan Das23956d12024-01-19 00:22:22 +00001020 baseName := c.module.RootLibraryName()
Jihoon Kangfa4a90d2023-12-20 02:53:38 +00001021 return c.namingScheme.exportableStubsLibraryModuleName(apiScope, baseName)
1022}
1023
Paul Duffinc3091c82020-05-08 14:16:20 +01001024// Name of the droidstubs module that generates the stubs source and may also
1025// generate/check the API.
1026func (c *commonToSdkLibraryAndImport) stubsSourceModuleName(apiScope *apiScope) string {
Spandan Das23956d12024-01-19 00:22:22 +00001027 baseName := c.module.RootLibraryName()
Paul Duffin21787622022-11-25 12:48:20 +00001028 return c.namingScheme.stubsSourceModuleName(apiScope, baseName)
Paul Duffinc3091c82020-05-08 14:16:20 +01001029}
1030
Jihoon Kang1c92c3e2023-03-23 17:44:51 +00001031// Name of the java_api_library module that generates the from-text stubs source
1032// and compiles to a jar file.
1033func (c *commonToSdkLibraryAndImport) apiLibraryModuleName(apiScope *apiScope) string {
Spandan Das23956d12024-01-19 00:22:22 +00001034 baseName := c.module.RootLibraryName()
Jihoon Kang1c92c3e2023-03-23 17:44:51 +00001035 return c.namingScheme.apiLibraryModuleName(apiScope, baseName)
1036}
1037
Jihoon Kang1147b312023-06-08 23:25:57 +00001038// Name of the java_library module that compiles the stubs
1039// generated from source Java files.
Jihoon Kangfa4a90d2023-12-20 02:53:38 +00001040func (c *commonToSdkLibraryAndImport) sourceStubsLibraryModuleName(apiScope *apiScope) string {
Spandan Das23956d12024-01-19 00:22:22 +00001041 baseName := c.module.RootLibraryName()
Jihoon Kangfa4a90d2023-12-20 02:53:38 +00001042 return c.namingScheme.sourceStubsLibraryModuleName(apiScope, baseName)
1043}
1044
1045// Name of the java_library module that compiles the exportable stubs
1046// generated from source Java files.
1047func (c *commonToSdkLibraryAndImport) exportableSourceStubsLibraryModuleName(apiScope *apiScope) string {
Spandan Das23956d12024-01-19 00:22:22 +00001048 baseName := c.module.RootLibraryName()
Jihoon Kangfa4a90d2023-12-20 02:53:38 +00001049 return c.namingScheme.exportableSourceStubsLibraryModuleName(apiScope, baseName)
Jihoon Kang1147b312023-06-08 23:25:57 +00001050}
1051
Paul Duffin46dc45a2020-05-14 15:39:10 +01001052// The component names for different outputs of the java_sdk_library.
1053//
1054// They are similar to the names used for the child modules it creates
1055const (
1056 stubsSourceComponentName = "stubs.source"
1057
1058 apiTxtComponentName = "api.txt"
1059
1060 removedApiTxtComponentName = "removed-api.txt"
Anton Hanssond78eb762021-09-21 15:25:12 +01001061
1062 annotationsComponentName = "annotations.zip"
Paul Duffin46dc45a2020-05-14 15:39:10 +01001063)
1064
1065// A regular expression to match tags that reference a specific stubs component.
1066//
1067// It will only match if given a valid scope and a valid component. It is verfy strict
1068// to ensure it does not accidentally match a similar looking tag that should be processed
1069// by the embedded Library.
1070var tagSplitter = func() *regexp.Regexp {
1071 // Given a list of literal string items returns a regular expression that will
1072 // match any one of the items.
1073 choice := func(items ...string) string {
1074 return `\Q` + strings.Join(items, `\E|\Q`) + `\E`
1075 }
1076
1077 // Regular expression to match one of the scopes.
1078 scopesRegexp := choice(allScopeNames...)
1079
1080 // Regular expression to match one of the components.
Anton Hanssond78eb762021-09-21 15:25:12 +01001081 componentsRegexp := choice(stubsSourceComponentName, apiTxtComponentName, removedApiTxtComponentName, annotationsComponentName)
Paul Duffin46dc45a2020-05-14 15:39:10 +01001082
1083 // Regular expression to match any combination of one scope and one component.
1084 return regexp.MustCompile(fmt.Sprintf(`^\.(%s)\.(%s)$`, scopesRegexp, componentsRegexp))
1085}()
1086
1087// For OutputFileProducer interface
1088//
Anton Hanssond78eb762021-09-21 15:25:12 +01001089// .<scope>.<component name>, for all ComponentNames (for example: .public.removed-api.txt)
Paul Duffin46dc45a2020-05-14 15:39:10 +01001090func (c *commonToSdkLibraryAndImport) commonOutputFiles(tag string) (android.Paths, error) {
1091 if groups := tagSplitter.FindStringSubmatch(tag); groups != nil {
1092 scopeName := groups[1]
1093 component := groups[2]
1094
1095 if scope, ok := scopeByName[scopeName]; ok {
1096 paths := c.findScopePaths(scope)
1097 if paths == nil {
Spandan Das23956d12024-01-19 00:22:22 +00001098 return nil, fmt.Errorf("%q does not provide api scope %s", c.module.RootLibraryName(), scopeName)
Paul Duffin46dc45a2020-05-14 15:39:10 +01001099 }
1100
1101 switch component {
1102 case stubsSourceComponentName:
1103 if paths.stubsSrcJar.Valid() {
1104 return android.Paths{paths.stubsSrcJar.Path()}, nil
1105 }
1106
1107 case apiTxtComponentName:
1108 if paths.currentApiFilePath.Valid() {
1109 return android.Paths{paths.currentApiFilePath.Path()}, nil
1110 }
1111
1112 case removedApiTxtComponentName:
1113 if paths.removedApiFilePath.Valid() {
1114 return android.Paths{paths.removedApiFilePath.Path()}, nil
1115 }
Anton Hanssond78eb762021-09-21 15:25:12 +01001116
1117 case annotationsComponentName:
1118 if paths.annotationsZip.Valid() {
1119 return android.Paths{paths.annotationsZip.Path()}, nil
1120 }
Paul Duffin46dc45a2020-05-14 15:39:10 +01001121 }
1122
1123 return nil, fmt.Errorf("%s not available for api scope %s", component, scopeName)
1124 } else {
1125 return nil, fmt.Errorf("unknown scope %s in %s", scope, tag)
1126 }
1127
1128 } else {
Paul Duffina2ae7e02020-09-11 11:55:00 +01001129 switch tag {
1130 case ".doctags":
1131 if c.doctagPaths != nil {
1132 return c.doctagPaths, nil
1133 } else {
Spandan Das23956d12024-01-19 00:22:22 +00001134 return nil, fmt.Errorf("no doctag_files specified on %s", c.module.RootLibraryName())
Paul Duffina2ae7e02020-09-11 11:55:00 +01001135 }
1136 }
Paul Duffin46dc45a2020-05-14 15:39:10 +01001137 return nil, nil
1138 }
1139}
1140
Paul Duffin803a9562020-05-20 11:52:25 +01001141func (c *commonToSdkLibraryAndImport) getScopePathsCreateIfNeeded(scope *apiScope) *scopePaths {
Paul Duffin56d44902020-01-31 13:36:25 +00001142 if c.scopePaths == nil {
1143 c.scopePaths = make(map[*apiScope]*scopePaths)
1144 }
1145 paths := c.scopePaths[scope]
1146 if paths == nil {
1147 paths = &scopePaths{}
1148 c.scopePaths[scope] = paths
1149 }
1150
1151 return paths
1152}
1153
Paul Duffin803a9562020-05-20 11:52:25 +01001154func (c *commonToSdkLibraryAndImport) findScopePaths(scope *apiScope) *scopePaths {
1155 if c.scopePaths == nil {
1156 return nil
1157 }
1158
1159 return c.scopePaths[scope]
1160}
1161
1162// If this does not support the requested api scope then find the closest available
1163// scope it does support. Returns nil if no such scope is available.
1164func (c *commonToSdkLibraryAndImport) findClosestScopePath(scope *apiScope) *scopePaths {
Paul Duffind0b9fca2022-09-30 18:11:41 +01001165 for s := scope; s != nil; s = s.canAccess {
Paul Duffin803a9562020-05-20 11:52:25 +01001166 if paths := c.findScopePaths(s); paths != nil {
1167 return paths
1168 }
1169 }
1170
1171 // This should never happen outside tests as public should be the base scope for every
1172 // scope and is enabled by default.
1173 return nil
1174}
1175
Jiyong Parkf1691d22021-03-29 20:11:58 +09001176func (c *commonToSdkLibraryAndImport) selectHeaderJarsForSdkVersion(ctx android.BaseModuleContext, sdkVersion android.SdkSpec) android.Paths {
Paul Duffinb05d4292020-05-20 12:19:10 +01001177
1178 // If a specific numeric version has been requested then use prebuilt versions of the sdk.
Jiyong Park54105c42021-03-31 18:17:53 +09001179 if !sdkVersion.ApiLevel.IsPreview() {
Spandan Das23956d12024-01-19 00:22:22 +00001180 return PrebuiltJars(ctx, c.module.RootLibraryName(), sdkVersion)
Paul Duffinb05d4292020-05-20 12:19:10 +01001181 }
1182
Paul Duffin1267d872021-04-16 17:21:36 +01001183 paths := c.selectScopePaths(ctx, sdkVersion.Kind)
1184 if paths == nil {
1185 return nil
1186 }
1187
1188 return paths.stubsHeaderPath
1189}
1190
1191// selectScopePaths returns the *scopePaths appropriate for the specific kind.
1192//
1193// If the module does not support the specific kind then it will return the *scopePaths for the
1194// closest kind which is a subset of the requested kind. e.g. if requesting android.SdkModule then
1195// it will return *scopePaths for android.SdkSystem if available or android.SdkPublic of not.
1196func (c *commonToSdkLibraryAndImport) selectScopePaths(ctx android.BaseModuleContext, kind android.SdkKind) *scopePaths {
Paul Duffin32cf58a2021-05-18 16:32:50 +01001197 apiScope := sdkKindToApiScope(kind)
Paul Duffinb05d4292020-05-20 12:19:10 +01001198
Paul Duffin803a9562020-05-20 11:52:25 +01001199 paths := c.findClosestScopePath(apiScope)
1200 if paths == nil {
1201 var scopes []string
1202 for _, s := range allApiScopes {
1203 if c.findScopePaths(s) != nil {
1204 scopes = append(scopes, s.name)
1205 }
1206 }
Spandan Das23956d12024-01-19 00:22:22 +00001207 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 +01001208 return nil
1209 }
1210
Paul Duffin1267d872021-04-16 17:21:36 +01001211 return paths
1212}
1213
Paul Duffin32cf58a2021-05-18 16:32:50 +01001214// sdkKindToApiScope maps from android.SdkKind to apiScope.
1215func sdkKindToApiScope(kind android.SdkKind) *apiScope {
1216 var apiScope *apiScope
1217 switch kind {
1218 case android.SdkSystem:
1219 apiScope = apiScopeSystem
1220 case android.SdkModule:
1221 apiScope = apiScopeModuleLib
1222 case android.SdkTest:
1223 apiScope = apiScopeTest
1224 case android.SdkSystemServer:
1225 apiScope = apiScopeSystemServer
1226 default:
1227 apiScope = apiScopePublic
1228 }
1229 return apiScope
1230}
1231
Paul Duffin1267d872021-04-16 17:21:36 +01001232// to satisfy SdkLibraryDependency interface
Martin Stjernholm8be1e6d2021-09-15 03:34:04 +01001233func (c *commonToSdkLibraryAndImport) SdkApiStubDexJar(ctx android.BaseModuleContext, kind android.SdkKind) OptionalDexJarPath {
Paul Duffin1267d872021-04-16 17:21:36 +01001234 paths := c.selectScopePaths(ctx, kind)
1235 if paths == nil {
Martin Stjernholm8be1e6d2021-09-15 03:34:04 +01001236 return makeUnsetDexJarPath()
Paul Duffin1267d872021-04-16 17:21:36 +01001237 }
1238
1239 return paths.stubsDexJarPath
Paul Duffinb05d4292020-05-20 12:19:10 +01001240}
1241
Paul Duffin32cf58a2021-05-18 16:32:50 +01001242// to satisfy SdkLibraryDependency interface
Jihoon Kangbd093452023-12-26 19:08:01 +00001243func (c *commonToSdkLibraryAndImport) SdkApiExportableStubDexJar(ctx android.BaseModuleContext, kind android.SdkKind) OptionalDexJarPath {
1244 paths := c.selectScopePaths(ctx, kind)
1245 if paths == nil {
1246 return makeUnsetDexJarPath()
1247 }
1248
1249 return paths.exportableStubsDexJarPath
1250}
1251
1252// to satisfy SdkLibraryDependency interface
Paul Duffin32cf58a2021-05-18 16:32:50 +01001253func (c *commonToSdkLibraryAndImport) SdkRemovedTxtFile(ctx android.BaseModuleContext, kind android.SdkKind) android.OptionalPath {
1254 apiScope := sdkKindToApiScope(kind)
1255 paths := c.findScopePaths(apiScope)
1256 if paths == nil {
1257 return android.OptionalPath{}
1258 }
1259
1260 return paths.removedApiFilePath
1261}
1262
Paul Duffin859fe962020-05-15 10:20:31 +01001263func (c *commonToSdkLibraryAndImport) sdkComponentPropertiesForChildLibrary() interface{} {
1264 componentProps := &struct {
Paul Duffin3f0290e2021-06-30 18:25:36 +01001265 SdkLibraryName *string
Paul Duffin859fe962020-05-15 10:20:31 +01001266 SdkLibraryToImplicitlyTrack *string
Paul Duffindfa131e2020-05-15 20:37:11 +01001267 }{}
1268
Spandan Das23956d12024-01-19 00:22:22 +00001269 namePtr := proptools.StringPtr(c.module.RootLibraryName())
Paul Duffin3f0290e2021-06-30 18:25:36 +01001270 componentProps.SdkLibraryName = namePtr
1271
Paul Duffindfa131e2020-05-15 20:37:11 +01001272 if c.sharedLibrary() {
Paul Duffin859fe962020-05-15 10:20:31 +01001273 // Mark the stubs library as being components of this java_sdk_library so that
1274 // any app that includes code which depends (directly or indirectly) on the stubs
1275 // library will have the appropriate <uses-library> invocation inserted into its
1276 // manifest if necessary.
Paul Duffin3f0290e2021-06-30 18:25:36 +01001277 componentProps.SdkLibraryToImplicitlyTrack = namePtr
Paul Duffin859fe962020-05-15 10:20:31 +01001278 }
1279
1280 return componentProps
1281}
1282
Paul Duffindfa131e2020-05-15 20:37:11 +01001283func (c *commonToSdkLibraryAndImport) sharedLibrary() bool {
1284 return proptools.BoolDefault(c.commonSdkLibraryProperties.Shared_library, true)
1285}
1286
Paul Duffinf4600f62021-05-13 22:34:45 +01001287// Check if the stub libraries should be compiled for dex
1288func (c *commonToSdkLibraryAndImport) stubLibrariesCompiledForDex() bool {
1289 // Always compile the dex file files for the stub libraries if they will be used on the
1290 // bootclasspath.
1291 return !c.sharedLibrary()
1292}
1293
Paul Duffin859fe962020-05-15 10:20:31 +01001294// Properties related to the use of a module as an component of a java_sdk_library.
1295type SdkLibraryComponentProperties struct {
Paul Duffin3f0290e2021-06-30 18:25:36 +01001296 // The name of the java_sdk_library/_import module.
1297 SdkLibraryName *string `blueprint:"mutated"`
Paul Duffin859fe962020-05-15 10:20:31 +01001298
1299 // The name of the java_sdk_library/_import to add to a <uses-library> entry
1300 // in the AndroidManifest.xml of any Android app that includes code that references
1301 // this module. If not set then no java_sdk_library/_import is tracked.
1302 SdkLibraryToImplicitlyTrack *string `blueprint:"mutated"`
1303}
1304
1305// Structure to be embedded in a module struct that needs to support the
1306// SdkLibraryComponentDependency interface.
1307type EmbeddableSdkLibraryComponent struct {
1308 sdkLibraryComponentProperties SdkLibraryComponentProperties
1309}
1310
Paul Duffin71b33cc2021-06-23 11:39:47 +01001311func (e *EmbeddableSdkLibraryComponent) initSdkLibraryComponent(module android.Module) {
1312 module.AddProperties(&e.sdkLibraryComponentProperties)
Paul Duffin859fe962020-05-15 10:20:31 +01001313}
1314
1315// to satisfy SdkLibraryComponentDependency
Paul Duffin3f0290e2021-06-30 18:25:36 +01001316func (e *EmbeddableSdkLibraryComponent) SdkLibraryName() *string {
1317 return e.sdkLibraryComponentProperties.SdkLibraryName
1318}
1319
1320// to satisfy SdkLibraryComponentDependency
Ulya Trafimovich39b437b2020-09-23 16:42:35 +01001321func (e *EmbeddableSdkLibraryComponent) OptionalSdkLibraryImplementation() *string {
Ulya Trafimovich78645fb2021-07-16 15:29:25 +01001322 // For shared libraries, this is the same as the SDK library name. If a Java library or app
1323 // depends on a component library (e.g. a stub library) it still needs to know the name of the
1324 // run-time library and the corresponding module that provides the implementation. This name is
1325 // passed to manifest_fixer (to be added to AndroidManifest.xml) and added to CLC (to be used
1326 // in dexpreopt).
1327 //
1328 // For non-shared SDK (component or not) libraries this returns `nil`, as they are not
1329 // <uses-library> and should not be added to the manifest or to CLC.
Ulya Trafimovich39b437b2020-09-23 16:42:35 +01001330 return e.sdkLibraryComponentProperties.SdkLibraryToImplicitlyTrack
1331}
1332
Paul Duffin859fe962020-05-15 10:20:31 +01001333// Implemented by modules that are (or possibly could be) a component of a java_sdk_library
1334// (including the java_sdk_library) itself.
1335type SdkLibraryComponentDependency interface {
Ulya Trafimovich31e444e2020-08-14 17:32:16 +01001336 UsesLibraryDependency
1337
Paul Duffin3f0290e2021-06-30 18:25:36 +01001338 // SdkLibraryName returns the name of the java_sdk_library/_import module.
1339 SdkLibraryName() *string
1340
Ulya Trafimovich39b437b2020-09-23 16:42:35 +01001341 // The name of the implementation library for the optional SDK library or nil, if there isn't one.
1342 OptionalSdkLibraryImplementation() *string
Paul Duffin859fe962020-05-15 10:20:31 +01001343}
1344
1345// Make sure that all the module types that are components of java_sdk_library/_import
1346// and which can be referenced (directly or indirectly) from an android app implement
1347// the SdkLibraryComponentDependency interface.
1348var _ SdkLibraryComponentDependency = (*Library)(nil)
1349var _ SdkLibraryComponentDependency = (*Import)(nil)
1350var _ SdkLibraryComponentDependency = (*SdkLibrary)(nil)
Paul Duffineedc5d52020-06-12 17:46:39 +01001351var _ SdkLibraryComponentDependency = (*SdkLibraryImport)(nil)
Paul Duffin859fe962020-05-15 10:20:31 +01001352
Paul Duffin32cf58a2021-05-18 16:32:50 +01001353// Provides access to sdk_version related files, e.g. header and implementation jars.
Paul Duffin859fe962020-05-15 10:20:31 +01001354type SdkLibraryDependency interface {
1355 SdkLibraryComponentDependency
1356
1357 // Get the header jars appropriate for the supplied sdk_version.
1358 //
1359 // These are turbine generated jars so they only change if the externals of the
1360 // class changes but it does not contain and implementation or JavaDoc.
Jiyong Parkf1691d22021-03-29 20:11:58 +09001361 SdkHeaderJars(ctx android.BaseModuleContext, sdkVersion android.SdkSpec) android.Paths
Paul Duffin859fe962020-05-15 10:20:31 +01001362
Jihoon Kangbd093452023-12-26 19:08:01 +00001363 // SdkApiStubDexJar returns the dex jar for the stubs for the prebuilt
1364 // java_sdk_library_import module. It is needed by the hiddenapi processing tool which
1365 // processes dex files.
Martin Stjernholm8be1e6d2021-09-15 03:34:04 +01001366 SdkApiStubDexJar(ctx android.BaseModuleContext, kind android.SdkKind) OptionalDexJarPath
Paul Duffinf4600f62021-05-13 22:34:45 +01001367
Jihoon Kangbd093452023-12-26 19:08:01 +00001368 // SdkApiExportableStubDexJar returns the exportable dex jar for the stubs for
1369 // java_sdk_library module. It is needed by the hiddenapi processing tool which processes
1370 // dex files.
1371 SdkApiExportableStubDexJar(ctx android.BaseModuleContext, kind android.SdkKind) OptionalDexJarPath
1372
Paul Duffin32cf58a2021-05-18 16:32:50 +01001373 // SdkRemovedTxtFile returns the optional path to the removed.txt file for the specified sdk kind.
1374 SdkRemovedTxtFile(ctx android.BaseModuleContext, kind android.SdkKind) android.OptionalPath
1375
Paul Duffinf4600f62021-05-13 22:34:45 +01001376 // sharedLibrary returns true if this can be used as a shared library.
1377 sharedLibrary() bool
Paul Duffin859fe962020-05-15 10:20:31 +01001378}
1379
Inseob Kimc0907f12019-02-08 21:00:45 +09001380type SdkLibrary struct {
Sundong Ahn054b19a2018-10-19 13:46:09 +09001381 Library
Jiyong Parkc678ad32018-04-10 13:07:10 +09001382
Sundong Ahn054b19a2018-10-19 13:46:09 +09001383 sdkLibraryProperties sdkLibraryProperties
Jiyong Parkc678ad32018-04-10 13:07:10 +09001384
Paul Duffin3375e352020-04-28 10:44:03 +01001385 // Map from api scope to the scope specific property structure.
1386 scopeToProperties map[*apiScope]*ApiScopeProperties
1387
Paul Duffin56d44902020-01-31 13:36:25 +00001388 commonToSdkLibraryAndImport
Jiyong Parkc678ad32018-04-10 13:07:10 +09001389}
1390
Inseob Kimc0907f12019-02-08 21:00:45 +09001391var _ SdkLibraryDependency = (*SdkLibrary)(nil)
Colin Cross897d2ed2019-02-11 14:03:51 -08001392
Paul Duffin3375e352020-04-28 10:44:03 +01001393func (module *SdkLibrary) generateTestAndSystemScopesByDefault() bool {
1394 return module.sdkLibraryProperties.Generate_system_and_test_apis
1395}
1396
1397func (module *SdkLibrary) getGeneratedApiScopes(ctx android.EarlyModuleContext) apiScopes {
1398 // Check to see if any scopes have been explicitly enabled. If any have then all
1399 // must be.
1400 anyScopesExplicitlyEnabled := false
1401 for _, scope := range allApiScopes {
1402 scopeProperties := module.scopeToProperties[scope]
1403 if scopeProperties.Enabled != nil {
1404 anyScopesExplicitlyEnabled = true
1405 break
1406 }
Paul Duffind1b3a922020-01-22 11:57:20 +00001407 }
Paul Duffin3375e352020-04-28 10:44:03 +01001408
1409 var generatedScopes apiScopes
1410 enabledScopes := make(map[*apiScope]struct{})
1411 for _, scope := range allApiScopes {
1412 scopeProperties := module.scopeToProperties[scope]
1413 // If any scopes are explicitly enabled then ignore the legacy enabled status.
1414 // This is to ensure that any new usages of this module type do not rely on legacy
1415 // behaviour.
1416 defaultEnabledStatus := false
1417 if anyScopesExplicitlyEnabled {
1418 defaultEnabledStatus = scope.defaultEnabledStatus
1419 } else {
1420 defaultEnabledStatus = scope.legacyEnabledStatus(module)
1421 }
1422 enabled := proptools.BoolDefault(scopeProperties.Enabled, defaultEnabledStatus)
1423 if enabled {
1424 enabledScopes[scope] = struct{}{}
1425 generatedScopes = append(generatedScopes, scope)
1426 }
1427 }
1428
1429 // Now check to make sure that any scope that is extended by an enabled scope is also
1430 // enabled.
1431 for _, scope := range allApiScopes {
1432 if _, ok := enabledScopes[scope]; ok {
1433 extends := scope.extends
1434 if extends != nil {
1435 if _, ok := enabledScopes[extends]; !ok {
1436 ctx.ModuleErrorf("enabled api scope %q depends on disabled scope %q", scope, extends)
1437 }
1438 }
1439 }
1440 }
1441
1442 return generatedScopes
Paul Duffind1b3a922020-01-22 11:57:20 +00001443}
1444
satayev758968a2021-12-06 11:42:40 +00001445var _ android.ModuleWithMinSdkVersionCheck = (*SdkLibrary)(nil)
1446
satayev8f088b02021-12-06 11:40:46 +00001447func (module *SdkLibrary) CheckMinSdkVersion(ctx android.ModuleContext) {
Spandan Das8c9ae7e2023-03-03 21:20:36 +00001448 android.CheckMinSdkVersion(ctx, module.MinSdkVersion(ctx), func(c android.ModuleContext, do android.PayloadDepsCallback) {
satayev8f088b02021-12-06 11:40:46 +00001449 ctx.WalkDeps(func(child android.Module, parent android.Module) bool {
1450 isExternal := !module.depIsInSameApex(ctx, child)
1451 if am, ok := child.(android.ApexModule); ok {
1452 if !do(ctx, parent, am, isExternal) {
1453 return false
1454 }
1455 }
1456 return !isExternal
1457 })
1458 })
1459}
1460
Paul Duffineedc5d52020-06-12 17:46:39 +01001461type sdkLibraryComponentTag struct {
1462 blueprint.BaseDependencyTag
1463 name string
1464}
1465
1466// Mark this tag so dependencies that use it are excluded from visibility enforcement.
1467func (t sdkLibraryComponentTag) ExcludeFromVisibilityEnforcement() {}
1468
1469var xmlPermissionsFileTag = sdkLibraryComponentTag{name: "xml-permissions-file"}
Paul Duffine74ac732020-02-06 13:51:46 +00001470
Jiyong Parke3833882020-02-17 17:28:10 +09001471func IsXmlPermissionsFileDepTag(depTag blueprint.DependencyTag) bool {
Paul Duffineedc5d52020-06-12 17:46:39 +01001472 if dt, ok := depTag.(sdkLibraryComponentTag); ok {
Jiyong Parke3833882020-02-17 17:28:10 +09001473 return dt == xmlPermissionsFileTag
1474 }
1475 return false
1476}
1477
Paul Duffineedc5d52020-06-12 17:46:39 +01001478var implLibraryTag = sdkLibraryComponentTag{name: "impl-library"}
Paul Duffin5df79302020-05-16 15:52:12 +01001479
Paul Duffin44f1d842020-06-26 20:17:02 +01001480// Add the dependencies on the child modules in the component deps mutator.
1481func (module *SdkLibrary) ComponentDepsMutator(ctx android.BottomUpMutatorContext) {
Paul Duffin3375e352020-04-28 10:44:03 +01001482 for _, apiScope := range module.getGeneratedApiScopes(ctx) {
Paul Duffind1b3a922020-01-22 11:57:20 +00001483 // Add dependencies to the stubs library
Spandan Das877f39d2023-03-29 16:19:51 +00001484 stubModuleName := module.stubsLibraryModuleName(apiScope)
Jihoon Kangbd093452023-12-26 19:08:01 +00001485 ctx.AddVariationDependencies(nil, apiScope.everythingStubsTag, stubModuleName)
Jihoon Kang1147b312023-06-08 23:25:57 +00001486
Jihoon Kangbd093452023-12-26 19:08:01 +00001487 exportableStubModuleName := module.exportableStubsLibraryModuleName(apiScope)
1488 ctx.AddVariationDependencies(nil, apiScope.exportableStubsTag, exportableStubModuleName)
Paul Duffind1b3a922020-01-22 11:57:20 +00001489
Paul Duffin15f34ef2020-07-20 18:04:44 +01001490 // Add a dependency on the stubs source in order to access both stubs source and api information.
1491 ctx.AddVariationDependencies(nil, apiScope.stubsSourceAndApiTag, module.stubsSourceModuleName(apiScope))
Paul Duffin958806b2022-05-16 13:10:47 +00001492
1493 if module.compareAgainstLatestApi(apiScope) {
1494 // Add dependencies on the latest finalized version of the API .txt file.
1495 latestApiModuleName := module.latestApiModuleName(apiScope)
1496 ctx.AddDependency(module, apiScope.latestApiModuleTag, latestApiModuleName)
1497
1498 // Add dependencies on the latest finalized version of the remove API .txt file.
1499 latestRemovedApiModuleName := module.latestRemovedApiModuleName(apiScope)
1500 ctx.AddDependency(module, apiScope.latestRemovedApiModuleTag, latestRemovedApiModuleName)
1501 }
Sundong Ahn054b19a2018-10-19 13:46:09 +09001502 }
1503
Paul Duffindfa131e2020-05-15 20:37:11 +01001504 if module.requiresRuntimeImplementationLibrary() {
Paul Duffin5df79302020-05-16 15:52:12 +01001505 // Add dependency to the rule for generating the implementation library.
1506 ctx.AddDependency(module, implLibraryTag, module.implLibraryModuleName())
1507
Paul Duffindfa131e2020-05-15 20:37:11 +01001508 if module.sharedLibrary() {
1509 // Add dependency to the rule for generating the xml permissions file
Paul Duffineedc5d52020-06-12 17:46:39 +01001510 ctx.AddDependency(module, xmlPermissionsFileTag, module.xmlPermissionsModuleName())
Paul Duffindfa131e2020-05-15 20:37:11 +01001511 }
Paul Duffin44f1d842020-06-26 20:17:02 +01001512 }
1513}
Paul Duffine74ac732020-02-06 13:51:46 +00001514
Paul Duffin44f1d842020-06-26 20:17:02 +01001515// Add other dependencies as normal.
1516func (module *SdkLibrary) DepsMutator(ctx android.BottomUpMutatorContext) {
Anton Hanssone77fccc2021-01-20 16:52:41 +00001517 var missingApiModules []string
1518 for _, apiScope := range module.getGeneratedApiScopes(ctx) {
1519 if apiScope.unstable {
1520 continue
1521 }
Paul Duffin958806b2022-05-16 13:10:47 +00001522 if m := module.latestApiModuleName(apiScope); !ctx.OtherModuleExists(m) {
Anton Hanssone77fccc2021-01-20 16:52:41 +00001523 missingApiModules = append(missingApiModules, m)
1524 }
Paul Duffin958806b2022-05-16 13:10:47 +00001525 if m := module.latestRemovedApiModuleName(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.latestIncompatibilitiesModuleName(apiScope); !ctx.OtherModuleExists(m) {
Jaewoong Jung1a97ee02021-03-09 13:25:02 -08001529 missingApiModules = append(missingApiModules, m)
1530 }
Anton Hanssone77fccc2021-01-20 16:52:41 +00001531 }
1532 if len(missingApiModules) != 0 && !module.sdkLibraryProperties.Unsafe_ignore_missing_latest_api {
1533 m := module.Name() + " is missing tracking files for previously released library versions.\n"
1534 m += "You need to do one of the following:\n"
1535 m += "- Add `unsafe_ignore_missing_latest_api: true` to your blueprint (to disable compat tracking)\n"
1536 m += "- Add a set of prebuilt txt files representing the last released version of this library for compat checking.\n"
1537 m += " (the current set of API files can be used as a seed for this compatibility tracking\n"
1538 m += "\n"
1539 m += "The following filegroup modules are missing:\n "
1540 m += strings.Join(missingApiModules, "\n ") + "\n"
1541 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."
1542 ctx.ModuleErrorf(m)
1543 }
Paul Duffin44f1d842020-06-26 20:17:02 +01001544 if module.requiresRuntimeImplementationLibrary() {
Paul Duffindfa131e2020-05-15 20:37:11 +01001545 // Only add the deps for the library if it is actually going to be built.
1546 module.Library.deps(ctx)
1547 }
Jiyong Parkc678ad32018-04-10 13:07:10 +09001548}
1549
Paul Duffin46dc45a2020-05-14 15:39:10 +01001550func (module *SdkLibrary) OutputFiles(tag string) (android.Paths, error) {
1551 paths, err := module.commonOutputFiles(tag)
Colin Cross4acaea92021-12-10 23:05:02 +00001552 if paths != nil || err != nil {
Paul Duffin46dc45a2020-05-14 15:39:10 +01001553 return paths, err
1554 }
Colin Cross4acaea92021-12-10 23:05:02 +00001555 if module.requiresRuntimeImplementationLibrary() {
1556 return module.Library.OutputFiles(tag)
1557 }
1558 if tag == "" {
1559 return nil, nil
1560 }
1561 return nil, fmt.Errorf("unsupported module reference tag %q", tag)
Paul Duffin46dc45a2020-05-14 15:39:10 +01001562}
1563
Inseob Kimc0907f12019-02-08 21:00:45 +09001564func (module *SdkLibrary) GenerateAndroidBuildActions(ctx android.ModuleContext) {
satayev8f088b02021-12-06 11:40:46 +00001565 if proptools.String(module.deviceProperties.Min_sdk_version) != "" {
1566 module.CheckMinSdkVersion(ctx)
1567 }
1568
Paul Duffina2ae7e02020-09-11 11:55:00 +01001569 module.generateCommonBuildActions(ctx)
1570
Paul Duffindfa131e2020-05-15 20:37:11 +01001571 // Only build an implementation library if required.
1572 if module.requiresRuntimeImplementationLibrary() {
Jihoon Kangfe914ed2024-02-12 22:49:21 +00001573 // stubsLinkType must be set before calling Library.GenerateAndroidBuildActions
1574 module.Library.stubsLinkType = Unknown
Paul Duffin43db9be2019-12-30 17:35:49 +00001575 module.Library.GenerateAndroidBuildActions(ctx)
1576 }
Sundong Ahn054b19a2018-10-19 13:46:09 +09001577
Paul Duffinb97b1572021-04-29 21:50:40 +01001578 // Collate the components exported by this module. All scope specific modules are exported but
1579 // the impl and xml component modules are not.
1580 exportedComponents := map[string]struct{}{}
1581
Sundong Ahn57368eb2018-07-06 11:20:23 +09001582 // Record the paths to the header jars of the library (stubs and impl).
Paul Duffind1b3a922020-01-22 11:57:20 +00001583 // When this java_sdk_library is depended upon from others via "libs" property,
Jiyong Parkc678ad32018-04-10 13:07:10 +09001584 // the recorded paths will be returned depending on the link type of the caller.
1585 ctx.VisitDirectDeps(func(to android.Module) {
Jiyong Parkc678ad32018-04-10 13:07:10 +09001586 tag := ctx.OtherModuleDependencyTag(to)
1587
Paul Duffinc8782502020-04-29 20:45:27 +01001588 // Extract information from any of the scope specific dependencies.
1589 if scopeTag, ok := tag.(scopeDependencyTag); ok {
1590 apiScope := scopeTag.apiScope
Paul Duffin803a9562020-05-20 11:52:25 +01001591 scopePaths := module.getScopePathsCreateIfNeeded(apiScope)
Paul Duffinc8782502020-04-29 20:45:27 +01001592
1593 // Extract information from the dependency. The exact information extracted
1594 // is determined by the nature of the dependency which is determined by the tag.
1595 scopeTag.extractDepInfo(ctx, to, scopePaths)
Paul Duffinb97b1572021-04-29 21:50:40 +01001596
1597 exportedComponents[ctx.OtherModuleName(to)] = struct{}{}
Sundong Ahn20e998b2018-07-24 11:19:26 +09001598 }
Jihoon Kang8479dea2024-04-04 01:19:05 +00001599
1600 if tag == implLibraryTag {
1601 if dep, ok := android.OtherModuleProvider(ctx, to, JavaInfoProvider); ok {
1602 module.implLibraryHeaderJars = append(module.implLibraryHeaderJars, dep.HeaderJars...)
1603 }
1604 }
Jiyong Parkc678ad32018-04-10 13:07:10 +09001605 })
Paul Duffinb97b1572021-04-29 21:50:40 +01001606
1607 // Make the set of components exported by this module available for use elsewhere.
Cole Faust18994c72023-02-28 16:02:16 -08001608 exportedComponentInfo := android.ExportedComponentsInfo{Components: android.SortedKeys(exportedComponents)}
Colin Cross40213022023-12-13 15:19:49 -08001609 android.SetProvider(ctx, android.ExportedComponentsInfoProvider, exportedComponentInfo)
Paul Duffin958806b2022-05-16 13:10:47 +00001610
1611 // Provide additional information for inclusion in an sdk's generated .info file.
1612 additionalSdkInfo := map[string]interface{}{}
1613 additionalSdkInfo["dist_stem"] = module.distStem()
Paul Duffine8409952022-09-22 16:24:46 +01001614 baseModuleName := module.distStem()
Paul Duffin958806b2022-05-16 13:10:47 +00001615 scopes := map[string]interface{}{}
1616 additionalSdkInfo["scopes"] = scopes
1617 for scope, scopePaths := range module.scopePaths {
1618 scopeInfo := map[string]interface{}{}
1619 scopes[scope.name] = scopeInfo
1620 scopeInfo["current_api"] = scope.snapshotRelativeCurrentApiTxtPath(baseModuleName)
1621 scopeInfo["removed_api"] = scope.snapshotRelativeRemovedApiTxtPath(baseModuleName)
1622 if p := scopePaths.latestApiPath; p.Valid() {
1623 scopeInfo["latest_api"] = p.Path().String()
1624 }
1625 if p := scopePaths.latestRemovedApiPath; p.Valid() {
1626 scopeInfo["latest_removed_api"] = p.Path().String()
1627 }
1628 }
Colin Cross40213022023-12-13 15:19:49 -08001629 android.SetProvider(ctx, android.AdditionalSdkInfoProvider, android.AdditionalSdkInfo{additionalSdkInfo})
Jiyong Parkc678ad32018-04-10 13:07:10 +09001630}
1631
Jiyong Park0b0e1b92019-12-03 13:24:29 +09001632func (module *SdkLibrary) AndroidMkEntries() []android.AndroidMkEntries {
Paul Duffindfa131e2020-05-15 20:37:11 +01001633 if !module.requiresRuntimeImplementationLibrary() {
Paul Duffin43db9be2019-12-30 17:35:49 +00001634 return nil
1635 }
Jiyong Park0b0e1b92019-12-03 13:24:29 +09001636 entriesList := module.Library.AndroidMkEntries()
Yo Chiang07d75072020-06-05 17:43:19 +08001637 if module.sharedLibrary() {
1638 entries := &entriesList[0]
1639 entries.Required = append(entries.Required, module.xmlPermissionsModuleName())
1640 }
Jiyong Park0b0e1b92019-12-03 13:24:29 +09001641 return entriesList
Jiyong Park82484c02018-04-23 21:41:26 +09001642}
1643
Anton Hansson5fd5d242020-03-27 19:43:19 +00001644// The dist path of the stub artifacts
1645func (module *SdkLibrary) apiDistPath(apiScope *apiScope) string {
Colin Crossf0eace92021-06-02 13:02:23 -07001646 return path.Join("apistubs", module.distGroup(), apiScope.name)
Anton Hansson5fd5d242020-03-27 19:43:19 +00001647}
1648
Paul Duffin12ceb462019-12-24 20:31:31 +00001649// Get the sdk version for use when compiling the stubs library.
Paul Duffin780c5f42020-05-12 15:52:55 +01001650func (module *SdkLibrary) sdkVersionForStubsLibrary(mctx android.EarlyModuleContext, apiScope *apiScope) string {
Paul Duffin87a05a32020-05-12 11:50:28 +01001651 scopeProperties := module.scopeToProperties[apiScope]
1652 if scopeProperties.Sdk_version != nil {
1653 return proptools.String(scopeProperties.Sdk_version)
1654 }
1655
Jiyong Parkf1691d22021-03-29 20:11:58 +09001656 sdkDep := decodeSdkDep(mctx, android.SdkContext(&module.Library))
Paul Duffin12ceb462019-12-24 20:31:31 +00001657 if sdkDep.hasStandardLibs() {
1658 // If building against a standard sdk then use the sdk version appropriate for the scope.
Paul Duffind1b3a922020-01-22 11:57:20 +00001659 return apiScope.sdkVersion
Paul Duffin12ceb462019-12-24 20:31:31 +00001660 } else {
1661 // Otherwise, use no system module.
1662 return "none"
1663 }
1664}
1665
Paul Duffin31310252020-11-20 21:26:20 +00001666func (module *SdkLibrary) distStem() string {
1667 return proptools.StringDefault(module.sdkLibraryProperties.Dist_stem, module.BaseModuleName())
1668}
1669
Colin Cross986b69a2021-06-01 13:13:40 -07001670// distGroup returns the subdirectory of the dist path of the stub artifacts.
1671func (module *SdkLibrary) distGroup() string {
Colin Cross59b92bf2021-06-01 14:07:56 -07001672 return proptools.StringDefault(module.sdkLibraryProperties.Dist_group, "unknown")
Colin Cross986b69a2021-06-01 13:13:40 -07001673}
1674
Paul Duffin958806b2022-05-16 13:10:47 +00001675func latestPrebuiltApiModuleName(name string, apiScope *apiScope) string {
1676 return PrebuiltApiModuleName(name, apiScope.name, "latest")
1677}
1678
Jihoon Kang748a24d2024-03-20 21:29:39 +00001679func latestPrebuiltApiCombinedModuleName(name string, apiScope *apiScope) string {
1680 return PrebuiltApiCombinedModuleName(name, apiScope.name, "latest")
1681}
1682
Paul Duffind1b3a922020-01-22 11:57:20 +00001683func (module *SdkLibrary) latestApiFilegroupName(apiScope *apiScope) string {
Paul Duffin958806b2022-05-16 13:10:47 +00001684 return ":" + module.latestApiModuleName(apiScope)
1685}
1686
1687func (module *SdkLibrary) latestApiModuleName(apiScope *apiScope) string {
Jihoon Kang748a24d2024-03-20 21:29:39 +00001688 return latestPrebuiltApiCombinedModuleName(module.distStem(), apiScope)
Jiyong Park58c518b2018-05-12 22:29:12 +09001689}
Jiyong Parkc678ad32018-04-10 13:07:10 +09001690
Paul Duffind1b3a922020-01-22 11:57:20 +00001691func (module *SdkLibrary) latestRemovedApiFilegroupName(apiScope *apiScope) string {
Paul Duffin958806b2022-05-16 13:10:47 +00001692 return ":" + module.latestRemovedApiModuleName(apiScope)
1693}
1694
1695func (module *SdkLibrary) latestRemovedApiModuleName(apiScope *apiScope) string {
Jihoon Kang748a24d2024-03-20 21:29:39 +00001696 return latestPrebuiltApiCombinedModuleName(module.distStem()+"-removed", apiScope)
Jiyong Parkc678ad32018-04-10 13:07:10 +09001697}
1698
Jaewoong Jung1a97ee02021-03-09 13:25:02 -08001699func (module *SdkLibrary) latestIncompatibilitiesFilegroupName(apiScope *apiScope) string {
Paul Duffin958806b2022-05-16 13:10:47 +00001700 return ":" + module.latestIncompatibilitiesModuleName(apiScope)
1701}
1702
1703func (module *SdkLibrary) latestIncompatibilitiesModuleName(apiScope *apiScope) string {
1704 return latestPrebuiltApiModuleName(module.distStem()+"-incompatibilities", apiScope)
Jaewoong Jung1a97ee02021-03-09 13:25:02 -08001705}
1706
Jihoon Kang1c92c3e2023-03-23 17:44:51 +00001707func (module *SdkLibrary) contributesToApiSurface(c android.Config) bool {
1708 _, exists := c.GetApiLibraries()[module.Name()]
1709 return exists
1710}
1711
Jihoon Kang0c705a42023-08-02 06:44:57 +00001712// The listed modules are the special java_sdk_libraries where apiScope.kind do not match the
1713// api surface that the module contribute to. For example, the public droidstubs and java_library
1714// do not contribute to the public api surface, but contributes to the core platform api surface.
1715// This method returns the full api surface stub lib that
1716// the generated java_api_library should depend on.
1717func (module *SdkLibrary) alternativeFullApiSurfaceStubLib() string {
1718 if val, ok := apiLibraryAdditionalProperties[module.Name()]; ok {
1719 return val.FullApiSurfaceStubLib
1720 }
1721 return ""
1722}
1723
1724// The listed modules' stubs contents do not match the corresponding txt files,
1725// but require additional api contributions to generate the full stubs.
1726// This method returns the name of the additional api contribution module
1727// for corresponding sdk_library modules.
1728func (module *SdkLibrary) apiLibraryAdditionalApiContribution() string {
1729 if val, ok := apiLibraryAdditionalProperties[module.Name()]; ok {
1730 return val.AdditionalApiContribution
1731 }
1732 return ""
1733}
1734
Anton Hansson944e77d2020-08-19 11:40:22 +01001735func childModuleVisibility(childVisibility []string) []string {
1736 if childVisibility == nil {
1737 // No child visibility set. The child will use the visibility of the sdk_library.
1738 return nil
1739 }
1740
1741 // Prepend an override to ignore the sdk_library's visibility, and rely on the child visibility.
1742 var visibility []string
1743 visibility = append(visibility, "//visibility:override")
1744 visibility = append(visibility, childVisibility...)
1745 return visibility
1746}
1747
Paul Duffin5df79302020-05-16 15:52:12 +01001748// Creates the implementation java library
1749func (module *SdkLibrary) createImplLibrary(mctx android.DefaultableHookContext) {
Anton Hansson944e77d2020-08-19 11:40:22 +01001750 visibility := childModuleVisibility(module.sdkLibraryProperties.Impl_library_visibility)
1751
Paul Duffin5df79302020-05-16 15:52:12 +01001752 props := struct {
Paul Duffin77590a82022-04-28 14:13:30 +00001753 Name *string
1754 Visibility []string
1755 Instrument bool
1756 Libs []string
1757 Static_libs []string
1758 Apex_available []string
Paul Duffin5df79302020-05-16 15:52:12 +01001759 }{
1760 Name: proptools.StringPtr(module.implLibraryModuleName()),
Anton Hansson944e77d2020-08-19 11:40:22 +01001761 Visibility: visibility,
Paul Duffin296cf332020-06-18 21:09:55 +01001762 // Set the instrument property to ensure it is instrumented when instrumentation is required.
1763 Instrument: true,
Anton Hansson7f66efa2020-10-08 14:47:23 +01001764 // Set the impl_only libs. Note that the module's "Libs" get appended as well, via the
1765 // addition of &module.properties below.
1766 Libs: module.sdkLibraryProperties.Impl_only_libs,
Paul Duffin77590a82022-04-28 14:13:30 +00001767 // Set the impl_only static libs. Note that the module's "static_libs" get appended as well, via the
1768 // addition of &module.properties below.
1769 Static_libs: module.sdkLibraryProperties.Impl_only_static_libs,
1770 // Pass the apex_available settings down so that the impl library can be statically
1771 // embedded within a library that is added to an APEX. Needed for updatable-media.
1772 Apex_available: module.ApexAvailable(),
Paul Duffin5df79302020-05-16 15:52:12 +01001773 }
1774
1775 properties := []interface{}{
1776 &module.properties,
1777 &module.protoProperties,
1778 &module.deviceProperties,
Liz Kammera7a64f32020-07-09 15:16:41 -07001779 &module.dexProperties,
Paul Duffin5df79302020-05-16 15:52:12 +01001780 &module.dexpreoptProperties,
Colin Cross014489c2020-06-02 20:09:13 -07001781 &module.linter.properties,
Paul Duffin5df79302020-05-16 15:52:12 +01001782 &props,
1783 module.sdkComponentPropertiesForChildLibrary(),
1784 }
1785 mctx.CreateModule(LibraryFactory, properties...)
1786}
1787
Jihoon Kangfa4a90d2023-12-20 02:53:38 +00001788type libraryProperties struct {
1789 Name *string
1790 Visibility []string
1791 Srcs []string
1792 Installable *bool
1793 Sdk_version *string
1794 System_modules *string
1795 Patch_module *string
1796 Libs []string
1797 Static_libs []string
1798 Compile_dex *bool
1799 Java_version *string
1800 Openjdk9 struct {
1801 Srcs []string
1802 Javacflags []string
1803 }
1804 Dist struct {
1805 Targets []string
1806 Dest *string
1807 Dir *string
1808 Tag *string
1809 }
Jihoon Kangfe914ed2024-02-12 22:49:21 +00001810 Is_stubs_module *bool
Jihoon Kangfa4a90d2023-12-20 02:53:38 +00001811}
Jiyong Parkc678ad32018-04-10 13:07:10 +09001812
Jihoon Kangfa4a90d2023-12-20 02:53:38 +00001813func (module *SdkLibrary) stubsLibraryProps(mctx android.DefaultableHookContext, apiScope *apiScope) libraryProperties {
1814 props := libraryProperties{}
Jihoon Kang786df932023-09-07 01:18:31 +00001815 props.Visibility = []string{"//visibility:override", "//visibility:private"}
Jiyong Parkc678ad32018-04-10 13:07:10 +09001816 // sources are generated from the droiddoc
Paul Duffin12ceb462019-12-24 20:31:31 +00001817 sdkVersion := module.sdkVersionForStubsLibrary(mctx, apiScope)
Paul Duffin52d398a2019-06-11 12:31:14 +01001818 props.Sdk_version = proptools.StringPtr(sdkVersion)
Paul Duffina18abc22020-05-16 18:54:24 +01001819 props.System_modules = module.deviceProperties.System_modules
1820 props.Patch_module = module.properties.Patch_module
Paul Duffin367ab912019-12-23 19:40:36 +00001821 props.Installable = proptools.BoolPtr(false)
Sundong Ahn054b19a2018-10-19 13:46:09 +09001822 props.Libs = module.sdkLibraryProperties.Stub_only_libs
Mark White9421c4c2023-08-10 00:07:03 +00001823 props.Libs = append(props.Libs, module.scopeToProperties[apiScope].Libs...)
Anton Hanssondae54cd2021-04-21 16:30:10 +01001824 props.Static_libs = module.sdkLibraryProperties.Stub_only_static_libs
Paul Duffine22c2ab2020-05-20 19:35:27 +01001825 // The stub-annotations library contains special versions of the annotations
1826 // with CLASS retention policy, so that they're kept.
1827 if proptools.Bool(module.sdkLibraryProperties.Annotations_enabled) {
1828 props.Libs = append(props.Libs, "stub-annotations")
1829 }
Paul Duffina18abc22020-05-16 18:54:24 +01001830 props.Openjdk9.Srcs = module.properties.Openjdk9.Srcs
1831 props.Openjdk9.Javacflags = module.properties.Openjdk9.Javacflags
Anton Hansson83509b52020-05-21 09:21:57 +01001832 // We compile the stubs for 1.8 in line with the main android.jar stubs, and potential
1833 // interop with older developer tools that don't support 1.9.
1834 props.Java_version = proptools.StringPtr("1.8")
Jihoon Kangfe914ed2024-02-12 22:49:21 +00001835 props.Is_stubs_module = proptools.BoolPtr(true)
Paul Duffinf4600f62021-05-13 22:34:45 +01001836
Jihoon Kangfa4a90d2023-12-20 02:53:38 +00001837 return props
1838}
1839
1840// Creates a static java library that has API stubs
1841func (module *SdkLibrary) createStubsLibrary(mctx android.DefaultableHookContext, apiScope *apiScope) {
1842
1843 props := module.stubsLibraryProps(mctx, apiScope)
1844 props.Name = proptools.StringPtr(module.sourceStubsLibraryModuleName(apiScope))
1845 props.Srcs = []string{":" + module.stubsSourceModuleName(apiScope)}
1846
1847 mctx.CreateModule(LibraryFactory, &props, module.sdkComponentPropertiesForChildLibrary())
1848}
1849
1850// Create a static java library that compiles the "exportable" stubs
1851func (module *SdkLibrary) createExportableStubsLibrary(mctx android.DefaultableHookContext, apiScope *apiScope) {
1852 props := module.stubsLibraryProps(mctx, apiScope)
1853 props.Name = proptools.StringPtr(module.exportableSourceStubsLibraryModuleName(apiScope))
1854 props.Srcs = []string{":" + module.stubsSourceModuleName(apiScope) + "{.exportable}"}
1855
Paul Duffin859fe962020-05-15 10:20:31 +01001856 mctx.CreateModule(LibraryFactory, &props, module.sdkComponentPropertiesForChildLibrary())
Jiyong Parkc678ad32018-04-10 13:07:10 +09001857}
1858
Paul Duffin6d0886e2020-04-07 18:49:53 +01001859// Creates a droidstubs module that creates stubs source files from the given full source
Paul Duffinc8782502020-04-29 20:45:27 +01001860// files and also updates and checks the API specification files.
Paul Duffin15f34ef2020-07-20 18:04:44 +01001861func (module *SdkLibrary) createStubsSourcesAndApi(mctx android.DefaultableHookContext, apiScope *apiScope, name string, scopeSpecificDroidstubsArgs []string) {
Jiyong Parkc678ad32018-04-10 13:07:10 +09001862 props := struct {
Sundong Ahn054b19a2018-10-19 13:46:09 +09001863 Name *string
Paul Duffin4911a892020-04-29 23:35:13 +01001864 Visibility []string
Sundong Ahn054b19a2018-10-19 13:46:09 +09001865 Srcs []string
1866 Installable *bool
Paul Duffin52d398a2019-06-11 12:31:14 +01001867 Sdk_version *string
Jihoon Kang3198f3c2023-01-26 08:08:52 +00001868 Api_surface *string
Paul Duffin12ceb462019-12-24 20:31:31 +00001869 System_modules *string
Sundong Ahn054b19a2018-10-19 13:46:09 +09001870 Libs []string
Paul Duffin6877e6d2020-09-25 19:59:14 +01001871 Output_javadoc_comments *bool
Paul Duffin11512472019-02-11 15:55:17 +00001872 Arg_files []string
Sundong Ahn054b19a2018-10-19 13:46:09 +09001873 Args *string
Sundong Ahn054b19a2018-10-19 13:46:09 +09001874 Java_version *string
Paul Duffine22c2ab2020-05-20 19:35:27 +01001875 Annotations_enabled *bool
Sundong Ahn054b19a2018-10-19 13:46:09 +09001876 Merge_annotations_dirs []string
1877 Merge_inclusion_annotations_dirs []string
Paul Duffin0ff08bd2020-04-29 13:30:54 +01001878 Generate_stubs *bool
Anton Hanssone87b03d2020-12-21 15:29:34 +00001879 Previous_api *string
Jihoon Kang6592e872023-12-19 01:13:16 +00001880 Aconfig_declarations []string
Sundong Ahn054b19a2018-10-19 13:46:09 +09001881 Check_api struct {
Anton Hanssone6056152020-12-31 10:37:27 +00001882 Current ApiToCheck
1883 Last_released ApiToCheck
Paul Duffin160fe412020-05-10 19:32:20 +01001884
1885 Api_lint struct {
1886 Enabled *bool
1887 New_since *string
1888 Baseline_file *string
1889 }
Jiyong Park58c518b2018-05-12 22:29:12 +09001890 }
Sundong Ahn1b92c822018-05-29 11:35:17 +09001891 Aidl struct {
1892 Include_dirs []string
1893 Local_include_dirs []string
1894 }
Paul Duffin040e9062020-11-23 17:41:36 +00001895 Dists []android.Dist
Jiyong Parkc678ad32018-04-10 13:07:10 +09001896 }{}
1897
Paul Duffin7b78b4d2020-04-28 14:08:32 +01001898 // The stubs source processing uses the same compile time classpath when extracting the
1899 // API from the implementation library as it does when compiling it. i.e. the same
1900 // * sdk version
1901 // * system_modules
1902 // * libs (static_libs/libs)
Paul Duffin250e6192019-06-07 10:44:37 +01001903
Paul Duffin0ff08bd2020-04-29 13:30:54 +01001904 props.Name = proptools.StringPtr(name)
Anton Hansson944e77d2020-08-19 11:40:22 +01001905 props.Visibility = childModuleVisibility(module.sdkLibraryProperties.Stubs_source_visibility)
Paul Duffina18abc22020-05-16 18:54:24 +01001906 props.Srcs = append(props.Srcs, module.properties.Srcs...)
Anton Hanssonf8ea3722021-09-16 14:24:13 +01001907 props.Srcs = append(props.Srcs, module.sdkLibraryProperties.Api_srcs...)
Paul Duffina18abc22020-05-16 18:54:24 +01001908 props.Sdk_version = module.deviceProperties.Sdk_version
Jihoon Kang3198f3c2023-01-26 08:08:52 +00001909 props.Api_surface = &apiScope.name
Paul Duffina18abc22020-05-16 18:54:24 +01001910 props.System_modules = module.deviceProperties.System_modules
Jiyong Parkc678ad32018-04-10 13:07:10 +09001911 props.Installable = proptools.BoolPtr(false)
Sundong Ahne6f0b052018-06-05 16:46:14 +09001912 // A droiddoc module has only one Libs property and doesn't distinguish between
1913 // shared libs and static libs. So we need to add both of these libs to Libs property.
Paul Duffina18abc22020-05-16 18:54:24 +01001914 props.Libs = module.properties.Libs
1915 props.Libs = append(props.Libs, module.properties.Static_libs...)
Nikita Ioffed732da72022-11-21 12:38:25 +00001916 props.Libs = append(props.Libs, module.sdkLibraryProperties.Stub_only_libs...)
Mark White9421c4c2023-08-10 00:07:03 +00001917 props.Libs = append(props.Libs, module.scopeToProperties[apiScope].Libs...)
Paul Duffina18abc22020-05-16 18:54:24 +01001918 props.Aidl.Include_dirs = module.deviceProperties.Aidl.Include_dirs
1919 props.Aidl.Local_include_dirs = module.deviceProperties.Aidl.Local_include_dirs
1920 props.Java_version = module.properties.Java_version
Jiyong Parkc678ad32018-04-10 13:07:10 +09001921
Paul Duffine22c2ab2020-05-20 19:35:27 +01001922 props.Annotations_enabled = module.sdkLibraryProperties.Annotations_enabled
Sundong Ahn054b19a2018-10-19 13:46:09 +09001923 props.Merge_annotations_dirs = module.sdkLibraryProperties.Merge_annotations_dirs
1924 props.Merge_inclusion_annotations_dirs = module.sdkLibraryProperties.Merge_inclusion_annotations_dirs
Jihoon Kang6592e872023-12-19 01:13:16 +00001925 props.Aconfig_declarations = module.sdkLibraryProperties.Aconfig_declarations
Sundong Ahn054b19a2018-10-19 13:46:09 +09001926
Paul Duffin6d0886e2020-04-07 18:49:53 +01001927 droidstubsArgs := []string{}
Paul Duffin235ffff2019-12-24 10:41:30 +00001928 if len(module.sdkLibraryProperties.Api_packages) != 0 {
Paul Duffin6d0886e2020-04-07 18:49:53 +01001929 droidstubsArgs = append(droidstubsArgs, "--stub-packages "+strings.Join(module.sdkLibraryProperties.Api_packages, ":"))
Paul Duffin235ffff2019-12-24 10:41:30 +00001930 }
1931 if len(module.sdkLibraryProperties.Hidden_api_packages) != 0 {
Paul Duffin6d0886e2020-04-07 18:49:53 +01001932 droidstubsArgs = append(droidstubsArgs,
Paul Duffin235ffff2019-12-24 10:41:30 +00001933 android.JoinWithPrefix(module.sdkLibraryProperties.Hidden_api_packages, " --hide-package "))
1934 }
Paul Duffin6d0886e2020-04-07 18:49:53 +01001935 droidstubsArgs = append(droidstubsArgs, module.sdkLibraryProperties.Droiddoc_options...)
Anton Hanssonfd1c0d22023-11-02 15:18:09 +00001936 disabledWarnings := []string{"HiddenSuperclass"}
1937 if proptools.BoolDefault(module.sdkLibraryProperties.Api_lint.Legacy_errors_allowed, true) {
1938 disabledWarnings = append(disabledWarnings,
1939 "BroadcastBehavior",
1940 "DeprecationMismatch",
1941 "MissingPermission",
1942 "SdkConstant",
1943 "Todo",
1944 )
Paul Duffin235ffff2019-12-24 10:41:30 +00001945 }
Paul Duffin6d0886e2020-04-07 18:49:53 +01001946 droidstubsArgs = append(droidstubsArgs, android.JoinWithPrefix(disabledWarnings, "--hide "))
Sundong Ahnfb2721f2018-09-17 13:23:09 +09001947
Paul Duffin6877e6d2020-09-25 19:59:14 +01001948 // Output Javadoc comments for public scope.
1949 if apiScope == apiScopePublic {
1950 props.Output_javadoc_comments = proptools.BoolPtr(true)
1951 }
1952
Paul Duffin1fb487d2020-04-07 18:50:10 +01001953 // Add in scope specific arguments.
Paul Duffin0ff08bd2020-04-29 13:30:54 +01001954 droidstubsArgs = append(droidstubsArgs, scopeSpecificDroidstubsArgs...)
Paul Duffin11512472019-02-11 15:55:17 +00001955 props.Arg_files = module.sdkLibraryProperties.Droiddoc_option_files
Paul Duffin6d0886e2020-04-07 18:49:53 +01001956 props.Args = proptools.StringPtr(strings.Join(droidstubsArgs, " "))
Jiyong Parkc678ad32018-04-10 13:07:10 +09001957
Paul Duffin15f34ef2020-07-20 18:04:44 +01001958 // List of APIs identified from the provided source files are created. They are later
1959 // compared against to the not-yet-released (a.k.a current) list of APIs and to the
1960 // last-released (a.k.a numbered) list of API.
1961 currentApiFileName := apiScope.apiFilePrefix + "current.txt"
1962 removedApiFileName := apiScope.apiFilePrefix + "removed.txt"
1963 apiDir := module.getApiDir()
1964 currentApiFileName = path.Join(apiDir, currentApiFileName)
1965 removedApiFileName = path.Join(apiDir, removedApiFileName)
Jiyong Parkc678ad32018-04-10 13:07:10 +09001966
Paul Duffin15f34ef2020-07-20 18:04:44 +01001967 // check against the not-yet-release API
1968 props.Check_api.Current.Api_file = proptools.StringPtr(currentApiFileName)
1969 props.Check_api.Current.Removed_api_file = proptools.StringPtr(removedApiFileName)
Jiyong Park58c518b2018-05-12 22:29:12 +09001970
Paul Duffin958806b2022-05-16 13:10:47 +00001971 if module.compareAgainstLatestApi(apiScope) {
Paul Duffin15f34ef2020-07-20 18:04:44 +01001972 // check against the latest released API
1973 latestApiFilegroupName := proptools.StringPtr(module.latestApiFilegroupName(apiScope))
Anton Hanssone87b03d2020-12-21 15:29:34 +00001974 props.Previous_api = latestApiFilegroupName
Paul Duffin15f34ef2020-07-20 18:04:44 +01001975 props.Check_api.Last_released.Api_file = latestApiFilegroupName
1976 props.Check_api.Last_released.Removed_api_file = proptools.StringPtr(
1977 module.latestRemovedApiFilegroupName(apiScope))
Jaewoong Jung1a97ee02021-03-09 13:25:02 -08001978 props.Check_api.Last_released.Baseline_file = proptools.StringPtr(
1979 module.latestIncompatibilitiesFilegroupName(apiScope))
Paul Duffin160fe412020-05-10 19:32:20 +01001980
Paul Duffin15f34ef2020-07-20 18:04:44 +01001981 if proptools.Bool(module.sdkLibraryProperties.Api_lint.Enabled) {
1982 // Enable api lint.
1983 props.Check_api.Api_lint.Enabled = proptools.BoolPtr(true)
1984 props.Check_api.Api_lint.New_since = latestApiFilegroupName
Paul Duffin160fe412020-05-10 19:32:20 +01001985
Paul Duffin15f34ef2020-07-20 18:04:44 +01001986 // If it exists then pass a lint-baseline.txt through to droidstubs.
1987 baselinePath := path.Join(apiDir, apiScope.apiFilePrefix+"lint-baseline.txt")
1988 baselinePathRelativeToRoot := path.Join(mctx.ModuleDir(), baselinePath)
1989 paths, err := mctx.GlobWithDeps(baselinePathRelativeToRoot, nil)
1990 if err != nil {
1991 mctx.ModuleErrorf("error checking for presence of %s: %s", baselinePathRelativeToRoot, err)
1992 }
1993 if len(paths) == 1 {
1994 props.Check_api.Api_lint.Baseline_file = proptools.StringPtr(baselinePath)
1995 } else if len(paths) != 0 {
1996 mctx.ModuleErrorf("error checking for presence of %s: expected one path, found: %v", baselinePathRelativeToRoot, paths)
Paul Duffin160fe412020-05-10 19:32:20 +01001997 }
1998 }
Paul Duffin15f34ef2020-07-20 18:04:44 +01001999 }
Jiyong Park58c518b2018-05-12 22:29:12 +09002000
Paul Duffin15f34ef2020-07-20 18:04:44 +01002001 if !Bool(module.sdkLibraryProperties.No_dist) {
Paul Duffin040e9062020-11-23 17:41:36 +00002002 // Dist the api txt and removed api txt artifacts for sdk builds.
2003 distDir := proptools.StringPtr(path.Join(module.apiDistPath(apiScope), "api"))
Jihoon Kang02168052024-03-20 00:44:54 +00002004 stubsTypeTagPrefix := ""
2005 if mctx.Config().ReleaseHiddenApiExportableStubs() {
2006 stubsTypeTagPrefix = ".exportable"
2007 }
Paul Duffin040e9062020-11-23 17:41:36 +00002008 for _, p := range []struct {
2009 tag string
2010 pattern string
2011 }{
Jihoon Kangd1799f62024-02-20 23:01:38 +00002012 // "exportable" api files are copied to the dist directory instead of the
Jihoon Kang02168052024-03-20 00:44:54 +00002013 // "everything" api files when "RELEASE_HIDDEN_API_EXPORTABLE_STUBS" build flag
2014 // is set. Otherwise, the "everything" api files are copied to the dist directory.
2015 {tag: "%s.api.txt", pattern: "%s.txt"},
2016 {tag: "%s.removed-api.txt", pattern: "%s-removed.txt"},
Paul Duffin040e9062020-11-23 17:41:36 +00002017 } {
2018 props.Dists = append(props.Dists, android.Dist{
2019 Targets: []string{"sdk", "win_sdk"},
2020 Dir: distDir,
2021 Dest: proptools.StringPtr(fmt.Sprintf(p.pattern, module.distStem())),
Jihoon Kang02168052024-03-20 00:44:54 +00002022 Tag: proptools.StringPtr(fmt.Sprintf(p.tag, stubsTypeTagPrefix)),
Paul Duffin040e9062020-11-23 17:41:36 +00002023 })
2024 }
Anton Hansson5fd5d242020-03-27 19:43:19 +00002025 }
2026
Spandan Das2cc80ba2023-10-27 17:21:52 +00002027 mctx.CreateModule(DroidstubsFactory, &props, module.sdkComponentPropertiesForChildLibrary()).(*Droidstubs).CallHookIfAvailable(mctx)
Jiyong Parkc678ad32018-04-10 13:07:10 +09002028}
2029
Jihoon Kang0c705a42023-08-02 06:44:57 +00002030func (module *SdkLibrary) createApiLibrary(mctx android.DefaultableHookContext, apiScope *apiScope, alternativeFullApiSurfaceStub string) {
Jihoon Kang1c92c3e2023-03-23 17:44:51 +00002031 props := struct {
Jihoon Kangca198c22023-06-22 23:13:51 +00002032 Name *string
2033 Visibility []string
2034 Api_contributions []string
2035 Libs []string
2036 Static_libs []string
2037 Full_api_surface_stub *string
Jihoon Kang4ec24872023-10-05 17:26:09 +00002038 System_modules *string
Jihoon Kang063ec002023-06-28 01:16:23 +00002039 Enable_validation *bool
Jihoon Kang5d701272024-02-15 21:53:49 +00002040 Stubs_type *string
Jihoon Kang1c92c3e2023-03-23 17:44:51 +00002041 }{}
2042
2043 props.Name = proptools.StringPtr(module.apiLibraryModuleName(apiScope))
Jihoon Kang786df932023-09-07 01:18:31 +00002044 props.Visibility = []string{"//visibility:override", "//visibility:private"}
Jihoon Kang1c92c3e2023-03-23 17:44:51 +00002045
2046 apiContributions := []string{}
2047
2048 // Api surfaces are not independent of each other, but have subset relationships,
2049 // and so does the api files. To generate from-text stubs for api surfaces other than public,
2050 // all subset api domains' api_contriubtions must be added as well.
2051 scope := apiScope
2052 for scope != nil {
2053 apiContributions = append(apiContributions, module.stubsSourceModuleName(scope)+".api.contribution")
2054 scope = scope.extends
2055 }
Jihoon Kang0c705a42023-08-02 06:44:57 +00002056 if apiScope == apiScopePublic {
2057 additionalApiContribution := module.apiLibraryAdditionalApiContribution()
2058 if additionalApiContribution != "" {
2059 apiContributions = append(apiContributions, additionalApiContribution)
2060 }
2061 }
Jihoon Kang1c92c3e2023-03-23 17:44:51 +00002062
2063 props.Api_contributions = apiContributions
2064 props.Libs = module.properties.Libs
2065 props.Libs = append(props.Libs, module.sdkLibraryProperties.Stub_only_libs...)
Mark White9421c4c2023-08-10 00:07:03 +00002066 props.Libs = append(props.Libs, module.scopeToProperties[apiScope].Libs...)
Jihoon Kang1c92c3e2023-03-23 17:44:51 +00002067 props.Libs = append(props.Libs, "stub-annotations")
2068 props.Static_libs = module.sdkLibraryProperties.Stub_only_static_libs
Jihoon Kange7ee2562023-07-25 05:51:46 +00002069 props.Full_api_surface_stub = proptools.StringPtr(apiScope.kind.DefaultJavaLibraryName())
Jihoon Kang0c705a42023-08-02 06:44:57 +00002070 if alternativeFullApiSurfaceStub != "" {
2071 props.Full_api_surface_stub = proptools.StringPtr(alternativeFullApiSurfaceStub)
2072 }
Jihoon Kang1c92c3e2023-03-23 17:44:51 +00002073
2074 // android_module_lib_stubs_current.from-text only comprises api contributions from art, conscrypt and i18n.
2075 // Thus, replace with android_module_lib_stubs_current_full.from-text, which comprises every api domains.
2076 if apiScope.kind == android.SdkModule {
Jihoon Kangca198c22023-06-22 23:13:51 +00002077 props.Full_api_surface_stub = proptools.StringPtr(apiScope.kind.DefaultJavaLibraryName() + "_full.from-text")
Jihoon Kang1c92c3e2023-03-23 17:44:51 +00002078 }
2079
Jihoon Kangd30ac8a2023-10-09 18:00:17 +00002080 // java_sdk_library modules that set sdk_version as none does not depend on other api
2081 // domains. Therefore, java_api_library created from such modules should not depend on
2082 // full_api_surface_stubs but create and compile stubs by the java_api_library module
2083 // itself.
2084 if module.SdkVersion(mctx).Kind == android.SdkNone {
2085 props.Full_api_surface_stub = nil
2086 }
2087
Jihoon Kang4ec24872023-10-05 17:26:09 +00002088 props.System_modules = module.deviceProperties.System_modules
Jihoon Kang063ec002023-06-28 01:16:23 +00002089 props.Enable_validation = proptools.BoolPtr(true)
Jihoon Kang5d701272024-02-15 21:53:49 +00002090 props.Stubs_type = proptools.StringPtr("everything")
Jihoon Kang4ec24872023-10-05 17:26:09 +00002091
Spandan Das2cc80ba2023-10-27 17:21:52 +00002092 mctx.CreateModule(ApiLibraryFactory, &props, module.sdkComponentPropertiesForChildLibrary())
Jihoon Kang1c92c3e2023-03-23 17:44:51 +00002093}
2094
Jihoon Kang02168052024-03-20 00:44:54 +00002095func (module *SdkLibrary) topLevelStubsLibraryProps(mctx android.DefaultableHookContext, apiScope *apiScope, doDist bool) libraryProperties {
Jihoon Kangfa4a90d2023-12-20 02:53:38 +00002096 props := libraryProperties{}
2097
Jihoon Kang1147b312023-06-08 23:25:57 +00002098 props.Visibility = childModuleVisibility(module.sdkLibraryProperties.Stubs_library_visibility)
2099 sdkVersion := module.sdkVersionForStubsLibrary(mctx, apiScope)
2100 props.Sdk_version = proptools.StringPtr(sdkVersion)
2101
Jihoon Kang1147b312023-06-08 23:25:57 +00002102 props.System_modules = module.deviceProperties.System_modules
2103
Jihoon Kang1147b312023-06-08 23:25:57 +00002104 // The imports need to be compiled to dex if the java_sdk_library requests it.
2105 compileDex := module.dexProperties.Compile_dex
2106 if module.stubLibrariesCompiledForDex() {
2107 compileDex = proptools.BoolPtr(true)
2108 }
2109 props.Compile_dex = compileDex
2110
Jihoon Kang02168052024-03-20 00:44:54 +00002111 if !Bool(module.sdkLibraryProperties.No_dist) && doDist {
2112 props.Dist.Targets = []string{"sdk", "win_sdk"}
2113 props.Dist.Dest = proptools.StringPtr(fmt.Sprintf("%v.jar", module.distStem()))
2114 props.Dist.Dir = proptools.StringPtr(module.apiDistPath(apiScope))
2115 props.Dist.Tag = proptools.StringPtr(".jar")
2116 }
2117
Jihoon Kangfa4a90d2023-12-20 02:53:38 +00002118 return props
2119}
2120
2121func (module *SdkLibrary) createTopLevelStubsLibrary(
2122 mctx android.DefaultableHookContext, apiScope *apiScope, contributesToApiSurface bool) {
2123
Jihoon Kang02168052024-03-20 00:44:54 +00002124 // Dist the "everything" stubs when the RELEASE_HIDDEN_API_EXPORTABLE_STUBS build flag is false
2125 doDist := !mctx.Config().ReleaseHiddenApiExportableStubs()
2126 props := module.topLevelStubsLibraryProps(mctx, apiScope, doDist)
Jihoon Kangfa4a90d2023-12-20 02:53:38 +00002127 props.Name = proptools.StringPtr(module.stubsLibraryModuleName(apiScope))
2128
2129 // Add the stub compiling java_library/java_api_library as static lib based on build config
2130 staticLib := module.sourceStubsLibraryModuleName(apiScope)
2131 if mctx.Config().BuildFromTextStub() && contributesToApiSurface {
2132 staticLib = module.apiLibraryModuleName(apiScope)
2133 }
2134 props.Static_libs = append(props.Static_libs, staticLib)
2135
2136 mctx.CreateModule(LibraryFactory, &props, module.sdkComponentPropertiesForChildLibrary())
2137}
2138
2139func (module *SdkLibrary) createTopLevelExportableStubsLibrary(
2140 mctx android.DefaultableHookContext, apiScope *apiScope) {
2141
Jihoon Kang02168052024-03-20 00:44:54 +00002142 // Dist the "exportable" stubs when the RELEASE_HIDDEN_API_EXPORTABLE_STUBS build flag is true
2143 doDist := mctx.Config().ReleaseHiddenApiExportableStubs()
2144 props := module.topLevelStubsLibraryProps(mctx, apiScope, doDist)
Jihoon Kangfa4a90d2023-12-20 02:53:38 +00002145 props.Name = proptools.StringPtr(module.exportableStubsLibraryModuleName(apiScope))
2146
Jihoon Kangfa4a90d2023-12-20 02:53:38 +00002147 staticLib := module.exportableSourceStubsLibraryModuleName(apiScope)
2148 props.Static_libs = append(props.Static_libs, staticLib)
2149
Jihoon Kang1147b312023-06-08 23:25:57 +00002150 mctx.CreateModule(LibraryFactory, &props, module.sdkComponentPropertiesForChildLibrary())
2151}
2152
Paul Duffin958806b2022-05-16 13:10:47 +00002153func (module *SdkLibrary) compareAgainstLatestApi(apiScope *apiScope) bool {
2154 return !(apiScope.unstable || module.sdkLibraryProperties.Unsafe_ignore_missing_latest_api)
2155}
2156
Paul Duffinea8f8082021-06-24 13:25:57 +01002157// Implements android.ApexModule
Jooyung Han5e9013b2020-03-10 06:23:13 +09002158func (module *SdkLibrary) DepIsInSameApex(mctx android.BaseModuleContext, dep android.Module) bool {
2159 depTag := mctx.OtherModuleDependencyTag(dep)
2160 if depTag == xmlPermissionsFileTag {
2161 return true
2162 }
2163 return module.Library.DepIsInSameApex(mctx, dep)
2164}
2165
Paul Duffinea8f8082021-06-24 13:25:57 +01002166// Implements android.ApexModule
2167func (module *SdkLibrary) UniqueApexVariations() bool {
2168 return module.uniqueApexVariations()
2169}
2170
Jihoon Kang80456fd2023-11-15 19:22:14 +00002171func (module *SdkLibrary) ContributeToApi() bool {
2172 return proptools.BoolDefault(module.sdkLibraryProperties.Contribute_to_android_api, false)
2173}
2174
Jiyong Parkc678ad32018-04-10 13:07:10 +09002175// Creates the xml file that publicizes the runtime library
Paul Duffinf0229202020-04-29 16:47:28 +01002176func (module *SdkLibrary) createXmlFile(mctx android.DefaultableHookContext) {
Spandan Das8c9ae7e2023-03-03 21:20:36 +00002177 moduleMinApiLevel := module.Library.MinSdkVersion(mctx)
Pedro Loureiroc3621422021-09-28 15:40:23 +00002178 var moduleMinApiLevelStr = moduleMinApiLevel.String()
2179 if moduleMinApiLevel == android.NoneApiLevel {
2180 moduleMinApiLevelStr = "current"
2181 }
Jiyong Parke3833882020-02-17 17:28:10 +09002182 props := struct {
Pedro Loureiroc3621422021-09-28 15:40:23 +00002183 Name *string
2184 Lib_name *string
2185 Apex_available []string
2186 On_bootclasspath_since *string
2187 On_bootclasspath_before *string
2188 Min_device_sdk *string
2189 Max_device_sdk *string
2190 Sdk_library_min_api_level *string
Jamie Garsidee570ace2023-11-27 12:07:36 +00002191 Uses_libs_dependencies []string
Jiyong Parke3833882020-02-17 17:28:10 +09002192 }{
Pedro Loureiroc3621422021-09-28 15:40:23 +00002193 Name: proptools.StringPtr(module.xmlPermissionsModuleName()),
2194 Lib_name: proptools.StringPtr(module.BaseModuleName()),
2195 Apex_available: module.ApexProperties.Apex_available,
2196 On_bootclasspath_since: module.commonSdkLibraryProperties.On_bootclasspath_since,
2197 On_bootclasspath_before: module.commonSdkLibraryProperties.On_bootclasspath_before,
2198 Min_device_sdk: module.commonSdkLibraryProperties.Min_device_sdk,
2199 Max_device_sdk: module.commonSdkLibraryProperties.Max_device_sdk,
2200 Sdk_library_min_api_level: &moduleMinApiLevelStr,
Jamie Garsidee570ace2023-11-27 12:07:36 +00002201 Uses_libs_dependencies: module.usesLibraryProperties.Uses_libs,
Jiyong Parkc678ad32018-04-10 13:07:10 +09002202 }
Jiyong Parke3833882020-02-17 17:28:10 +09002203
Jiyong Parke3833882020-02-17 17:28:10 +09002204 mctx.CreateModule(sdkLibraryXmlFactory, &props)
Jiyong Parkc678ad32018-04-10 13:07:10 +09002205}
2206
Jiyong Parkf1691d22021-03-29 20:11:58 +09002207func PrebuiltJars(ctx android.BaseModuleContext, baseName string, s android.SdkSpec) android.Paths {
Jiyong Park54105c42021-03-31 18:17:53 +09002208 var ver android.ApiLevel
Jiyong Parkf1691d22021-03-29 20:11:58 +09002209 var kind android.SdkKind
2210 if s.UsePrebuilt(ctx) {
Jiyong Park54105c42021-03-31 18:17:53 +09002211 ver = s.ApiLevel
Jiyong Parkf1691d22021-03-29 20:11:58 +09002212 kind = s.Kind
Jiyong Parkc678ad32018-04-10 13:07:10 +09002213 } else {
Jiyong Park6a927c42020-01-21 02:03:43 +09002214 // We don't have prebuilt SDK for the specific sdkVersion.
2215 // Instead of breaking the build, fallback to use "system_current"
Jiyong Park54105c42021-03-31 18:17:53 +09002216 ver = android.FutureApiLevel
Jiyong Parkf1691d22021-03-29 20:11:58 +09002217 kind = android.SdkSystem
Sundong Ahn054b19a2018-10-19 13:46:09 +09002218 }
Jiyong Park6a927c42020-01-21 02:03:43 +09002219
2220 dir := filepath.Join("prebuilts", "sdk", ver.String(), kind.String())
Paul Duffin50061512020-01-21 16:31:05 +00002221 jar := filepath.Join(dir, baseName+".jar")
Sundong Ahn054b19a2018-10-19 13:46:09 +09002222 jarPath := android.ExistentPathForSource(ctx, jar)
Sundong Ahnae418ac2019-02-28 15:01:28 +09002223 if !jarPath.Valid() {
Colin Cross07c88562020-01-07 09:34:44 -08002224 if ctx.Config().AllowMissingDependencies() {
2225 return android.Paths{android.PathForSource(ctx, jar)}
2226 } else {
Jiyong Parkf1691d22021-03-29 20:11:58 +09002227 ctx.PropertyErrorf("sdk_library", "invalid sdk version %q, %q does not exist", s.Raw, jar)
Colin Cross07c88562020-01-07 09:34:44 -08002228 }
Sundong Ahnae418ac2019-02-28 15:01:28 +09002229 return nil
2230 }
Sundong Ahn054b19a2018-10-19 13:46:09 +09002231 return android.Paths{jarPath.Path()}
2232}
2233
Colin Crossaede88c2020-08-11 12:17:01 -07002234// 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 +01002235//
2236// If either this or the other module are on the platform then this will return
2237// false.
Colin Cross56a83212020-09-15 18:30:11 -07002238func withinSameApexesAs(ctx android.BaseModuleContext, other android.Module) bool {
Colin Crossff694a82023-12-13 15:54:49 -08002239 apexInfo, _ := android.ModuleProvider(ctx, android.ApexInfoProvider)
Colin Cross313aa542023-12-13 13:47:44 -08002240 otherApexInfo, _ := android.OtherModuleProvider(ctx, other, android.ApexInfoProvider)
Jiyong Parkab50b072021-05-12 17:13:56 +09002241 return len(otherApexInfo.InApexVariants) > 0 && reflect.DeepEqual(apexInfo.InApexVariants, otherApexInfo.InApexVariants)
Paul Duffin9b879592020-05-26 13:21:35 +01002242}
2243
Jihoon Kang8479dea2024-04-04 01:19:05 +00002244func (module *SdkLibrary) sdkJars(ctx android.BaseModuleContext, sdkVersion android.SdkSpec) android.Paths {
Jiyong Park932cdfe2020-05-28 00:19:53 +09002245 // If the client doesn't set sdk_version, but if this library prefers stubs over
2246 // the impl library, let's provide the widest API surface possible. To do so,
2247 // force override sdk_version to module_current so that the closest possible API
2248 // surface could be found in selectHeaderJarsForSdkVersion
Jiyong Parkf1691d22021-03-29 20:11:58 +09002249 if module.defaultsToStubs() && !sdkVersion.Specified() {
Jiyong Park92315372021-04-02 08:45:46 +09002250 sdkVersion = android.SdkSpecFrom(ctx, "module_current")
Jiyong Park932cdfe2020-05-28 00:19:53 +09002251 }
Paul Duffind1b3a922020-01-22 11:57:20 +00002252
Paul Duffindaaa3322020-05-26 18:13:57 +01002253 // Only provide access to the implementation library if it is actually built.
2254 if module.requiresRuntimeImplementationLibrary() {
2255 // Check any special cases for java_sdk_library.
2256 //
2257 // Only allow access to the implementation library in the following condition:
2258 // * No sdk_version specified on the referencing module.
Paul Duffin9b879592020-05-26 13:21:35 +01002259 // * The referencing module is in the same apex as this.
Jiyong Parkf1691d22021-03-29 20:11:58 +09002260 if sdkVersion.Kind == android.SdkPrivate || withinSameApexesAs(ctx, module) {
Jihoon Kang8479dea2024-04-04 01:19:05 +00002261 return module.implLibraryHeaderJars
Sundong Ahn054b19a2018-10-19 13:46:09 +09002262 }
Jiyong Parkc678ad32018-04-10 13:07:10 +09002263 }
Paul Duffinb05d4292020-05-20 12:19:10 +01002264
Paul Duffin23970f42020-05-20 14:20:02 +01002265 return module.selectHeaderJarsForSdkVersion(ctx, sdkVersion)
Jiyong Parkc678ad32018-04-10 13:07:10 +09002266}
2267
Sundong Ahn241cd372018-07-13 16:16:44 +09002268// to satisfy SdkLibraryDependency interface
Jiyong Parkf1691d22021-03-29 20:11:58 +09002269func (module *SdkLibrary) SdkHeaderJars(ctx android.BaseModuleContext, sdkVersion android.SdkSpec) android.Paths {
Jihoon Kang8479dea2024-04-04 01:19:05 +00002270 return module.sdkJars(ctx, sdkVersion)
Sundong Ahn241cd372018-07-13 16:16:44 +09002271}
2272
Colin Cross571cccf2019-02-04 11:22:08 -08002273var javaSdkLibrariesKey = android.NewOnceKey("javaSdkLibraries")
2274
Jiyong Park82484c02018-04-23 21:41:26 +09002275func javaSdkLibraries(config android.Config) *[]string {
Colin Cross571cccf2019-02-04 11:22:08 -08002276 return config.Once(javaSdkLibrariesKey, func() interface{} {
Jiyong Park82484c02018-04-23 21:41:26 +09002277 return &[]string{}
2278 }).(*[]string)
2279}
2280
Paul Duffin749f98f2019-12-30 17:23:46 +00002281func (module *SdkLibrary) getApiDir() string {
2282 return proptools.StringDefault(module.sdkLibraryProperties.Api_dir, "api")
2283}
2284
Jiyong Parkc678ad32018-04-10 13:07:10 +09002285// For a java_sdk_library module, create internal modules for stubs, docs,
2286// runtime libs and xml file. If requested, the stubs and docs are created twice
2287// once for public API level and once for system API level
Paul Duffinf0229202020-04-29 16:47:28 +01002288func (module *SdkLibrary) CreateInternalModules(mctx android.DefaultableHookContext) {
2289 // If the module has been disabled then don't create any child modules.
2290 if !module.Enabled() {
2291 return
2292 }
2293
Paul Duffina18abc22020-05-16 18:54:24 +01002294 if len(module.properties.Srcs) == 0 {
Inseob Kimc0907f12019-02-08 21:00:45 +09002295 mctx.PropertyErrorf("srcs", "java_sdk_library must specify srcs")
Jooyung Han58f26ab2019-12-18 15:34:32 +09002296 return
Inseob Kimc0907f12019-02-08 21:00:45 +09002297 }
2298
Paul Duffin37e0b772019-12-30 17:20:10 +00002299 // If this builds against standard libraries (i.e. is not part of the core libraries)
Paul Duffin4f5c1ef2020-11-19 14:53:43 +00002300 // then assume it provides both system and test apis.
Jiyong Parkf1691d22021-03-29 20:11:58 +09002301 sdkDep := decodeSdkDep(mctx, android.SdkContext(&module.Library))
Paul Duffin37e0b772019-12-30 17:20:10 +00002302 hasSystemAndTestApis := sdkDep.hasStandardLibs()
Paul Duffin3375e352020-04-28 10:44:03 +01002303 module.sdkLibraryProperties.Generate_system_and_test_apis = hasSystemAndTestApis
Paul Duffin4f5c1ef2020-11-19 14:53:43 +00002304
Jaewoong Jung18aefc12020-12-21 09:11:10 -08002305 missingCurrentApi := false
Inseob Kim8098faa2019-03-18 10:19:51 +09002306
Paul Duffin3375e352020-04-28 10:44:03 +01002307 generatedScopes := module.getGeneratedApiScopes(mctx)
Paul Duffind1b3a922020-01-22 11:57:20 +00002308
Paul Duffin749f98f2019-12-30 17:23:46 +00002309 apiDir := module.getApiDir()
Paul Duffin3375e352020-04-28 10:44:03 +01002310 for _, scope := range generatedScopes {
Inseob Kim8098faa2019-03-18 10:19:51 +09002311 for _, api := range []string{"current.txt", "removed.txt"} {
Paul Duffind1b3a922020-01-22 11:57:20 +00002312 path := path.Join(mctx.ModuleDir(), apiDir, scope.apiFilePrefix+api)
Inseob Kim8098faa2019-03-18 10:19:51 +09002313 p := android.ExistentPathForSource(mctx, path)
2314 if !p.Valid() {
Colin Cross18f840c2021-05-20 17:56:54 -07002315 if mctx.Config().AllowMissingDependencies() {
2316 mctx.AddMissingDependencies([]string{path})
2317 } else {
2318 mctx.ModuleErrorf("Current api file %#v doesn't exist", path)
2319 missingCurrentApi = true
2320 }
Inseob Kim8098faa2019-03-18 10:19:51 +09002321 }
2322 }
2323 }
2324
Jaewoong Jung18aefc12020-12-21 09:11:10 -08002325 if missingCurrentApi {
Inseob Kim8098faa2019-03-18 10:19:51 +09002326 script := "build/soong/scripts/gen-java-current-api-files.sh"
2327 p := android.ExistentPathForSource(mctx, script)
2328
2329 if !p.Valid() {
2330 panic(fmt.Sprintf("script file %s doesn't exist", script))
2331 }
2332
2333 mctx.ModuleErrorf("One or more current api files are missing. "+
2334 "You can update them by:\n"+
Paul Duffin37e0b772019-12-30 17:20:10 +00002335 "%s %q %s && m update-api",
Paul Duffind1b3a922020-01-22 11:57:20 +00002336 script, filepath.Join(mctx.ModuleDir(), apiDir),
Paul Duffin3375e352020-04-28 10:44:03 +01002337 strings.Join(generatedScopes.Strings(func(s *apiScope) string { return s.apiFilePrefix }), " "))
Inseob Kim8098faa2019-03-18 10:19:51 +09002338 return
2339 }
2340
Paul Duffin3375e352020-04-28 10:44:03 +01002341 for _, scope := range generatedScopes {
Paul Duffin15f34ef2020-07-20 18:04:44 +01002342 // Use the stubs source name for legacy reasons.
2343 module.createStubsSourcesAndApi(mctx, scope, module.stubsSourceModuleName(scope), scope.droidstubsArgs)
Paul Duffin0ff08bd2020-04-29 13:30:54 +01002344
Paul Duffind1b3a922020-01-22 11:57:20 +00002345 module.createStubsLibrary(mctx, scope)
Jihoon Kangfa4a90d2023-12-20 02:53:38 +00002346 module.createExportableStubsLibrary(mctx, scope)
Jihoon Kang1c92c3e2023-03-23 17:44:51 +00002347
Jihoon Kang0c705a42023-08-02 06:44:57 +00002348 alternativeFullApiSurfaceStubLib := ""
2349 if scope == apiScopePublic {
2350 alternativeFullApiSurfaceStubLib = module.alternativeFullApiSurfaceStubLib()
2351 }
2352 contributesToApiSurface := module.contributesToApiSurface(mctx.Config()) || alternativeFullApiSurfaceStubLib != ""
Jihoon Kang1147b312023-06-08 23:25:57 +00002353 if contributesToApiSurface {
Jihoon Kang0c705a42023-08-02 06:44:57 +00002354 module.createApiLibrary(mctx, scope, alternativeFullApiSurfaceStubLib)
Jihoon Kang1c92c3e2023-03-23 17:44:51 +00002355 }
Jihoon Kang1147b312023-06-08 23:25:57 +00002356
2357 module.createTopLevelStubsLibrary(mctx, scope, contributesToApiSurface)
Jihoon Kangfa4a90d2023-12-20 02:53:38 +00002358 module.createTopLevelExportableStubsLibrary(mctx, scope)
Inseob Kimc0907f12019-02-08 21:00:45 +09002359 }
2360
Paul Duffindfa131e2020-05-15 20:37:11 +01002361 if module.requiresRuntimeImplementationLibrary() {
Paul Duffin5df79302020-05-16 15:52:12 +01002362 // Create child module to create an implementation library.
2363 //
2364 // This temporarily creates a second implementation library that can be explicitly
2365 // referenced.
2366 //
2367 // TODO(b/156618935) - update comment once only one implementation library is created.
2368 module.createImplLibrary(mctx)
2369
Paul Duffindfa131e2020-05-15 20:37:11 +01002370 // Only create an XML permissions file that declares the library as being usable
2371 // as a shared library if required.
2372 if module.sharedLibrary() {
2373 module.createXmlFile(mctx)
2374 }
Paul Duffin43db9be2019-12-30 17:35:49 +00002375
2376 // record java_sdk_library modules so that they are exported to make
2377 javaSdkLibraries := javaSdkLibraries(mctx.Config())
2378 javaSdkLibrariesLock.Lock()
2379 defer javaSdkLibrariesLock.Unlock()
2380 *javaSdkLibraries = append(*javaSdkLibraries, module.BaseModuleName())
2381 }
Anton Hansson7f66efa2020-10-08 14:47:23 +01002382
Paul Duffin77590a82022-04-28 14:13:30 +00002383 // 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 +01002384 module.properties.Libs = append(module.properties.Libs, module.sdkLibraryProperties.Impl_only_libs...)
Paul Duffin77590a82022-04-28 14:13:30 +00002385 module.properties.Static_libs = append(module.properties.Static_libs, module.sdkLibraryProperties.Impl_only_static_libs...)
Inseob Kimc0907f12019-02-08 21:00:45 +09002386}
2387
2388func (module *SdkLibrary) InitSdkLibraryProperties() {
Colin Crossce6734e2020-06-15 16:09:53 -07002389 module.addHostAndDeviceProperties()
2390 module.AddProperties(&module.sdkLibraryProperties)
Sundong Ahn054b19a2018-10-19 13:46:09 +09002391
Paul Duffin71b33cc2021-06-23 11:39:47 +01002392 module.initSdkLibraryComponent(module)
Paul Duffin859fe962020-05-15 10:20:31 +01002393
Paul Duffina18abc22020-05-16 18:54:24 +01002394 module.properties.Installable = proptools.BoolPtr(true)
2395 module.deviceProperties.IsSDKLibrary = true
Inseob Kimc0907f12019-02-08 21:00:45 +09002396}
Sundong Ahn054b19a2018-10-19 13:46:09 +09002397
Paul Duffindfa131e2020-05-15 20:37:11 +01002398func (module *SdkLibrary) requiresRuntimeImplementationLibrary() bool {
2399 return !proptools.Bool(module.sdkLibraryProperties.Api_only)
2400}
2401
Jiyong Park932cdfe2020-05-28 00:19:53 +09002402func (module *SdkLibrary) defaultsToStubs() bool {
2403 return proptools.Bool(module.sdkLibraryProperties.Default_to_stubs)
2404}
2405
Paul Duffin1b1e8062020-05-08 13:44:43 +01002406// Defines how to name the individual component modules the sdk library creates.
2407type sdkLibraryComponentNamingScheme interface {
2408 stubsLibraryModuleName(scope *apiScope, baseName string) string
2409
2410 stubsSourceModuleName(scope *apiScope, baseName string) string
Jihoon Kang1c92c3e2023-03-23 17:44:51 +00002411
2412 apiLibraryModuleName(scope *apiScope, baseName string) string
Jihoon Kang1147b312023-06-08 23:25:57 +00002413
Jihoon Kangfa4a90d2023-12-20 02:53:38 +00002414 sourceStubsLibraryModuleName(scope *apiScope, baseName string) string
2415
2416 exportableStubsLibraryModuleName(scope *apiScope, baseName string) string
2417
2418 exportableSourceStubsLibraryModuleName(scope *apiScope, baseName string) string
Paul Duffin1b1e8062020-05-08 13:44:43 +01002419}
2420
2421type defaultNamingScheme struct {
2422}
2423
2424func (s *defaultNamingScheme) stubsLibraryModuleName(scope *apiScope, baseName string) string {
2425 return scope.stubsLibraryModuleName(baseName)
2426}
2427
2428func (s *defaultNamingScheme) stubsSourceModuleName(scope *apiScope, baseName string) string {
2429 return scope.stubsSourceModuleName(baseName)
2430}
2431
Jihoon Kang1c92c3e2023-03-23 17:44:51 +00002432func (s *defaultNamingScheme) apiLibraryModuleName(scope *apiScope, baseName string) string {
2433 return scope.apiLibraryModuleName(baseName)
2434}
2435
Jihoon Kangfa4a90d2023-12-20 02:53:38 +00002436func (s *defaultNamingScheme) sourceStubsLibraryModuleName(scope *apiScope, baseName string) string {
Jihoon Kang1147b312023-06-08 23:25:57 +00002437 return scope.sourceStubLibraryModuleName(baseName)
2438}
2439
Jihoon Kangfa4a90d2023-12-20 02:53:38 +00002440func (s *defaultNamingScheme) exportableStubsLibraryModuleName(scope *apiScope, baseName string) string {
2441 return scope.exportableStubsLibraryModuleName(baseName)
2442}
2443
2444func (s *defaultNamingScheme) exportableSourceStubsLibraryModuleName(scope *apiScope, baseName string) string {
2445 return scope.exportableSourceStubsLibraryModuleName(baseName)
2446}
2447
Paul Duffin1b1e8062020-05-08 13:44:43 +01002448var _ sdkLibraryComponentNamingScheme = (*defaultNamingScheme)(nil)
2449
Jihoon Kangfa4a90d2023-12-20 02:53:38 +00002450func hasStubsLibrarySuffix(name string, apiScope *apiScope) bool {
2451 return strings.HasSuffix(name, apiScope.stubsLibraryModuleNameSuffix()) ||
2452 strings.HasSuffix(name, apiScope.exportableStubsLibraryModuleNameSuffix())
2453}
2454
Jaewoong Jungbc15e3a2021-03-10 17:02:43 -08002455func moduleStubLinkType(name string) (stub bool, ret sdkLinkType) {
Jihoon Kang1147b312023-06-08 23:25:57 +00002456 name = strings.TrimSuffix(name, ".from-source")
2457
Anton Hansson2d0c1942020-05-25 12:20:51 +01002458 // This suffix-based approach is fragile and could potentially mis-trigger.
2459 // TODO(b/155164730): Clean this up when modules no longer reference sdk_lib stubs directly.
Jihoon Kangfa4a90d2023-12-20 02:53:38 +00002460 if hasStubsLibrarySuffix(name, apiScopePublic) {
Anton Hansson08f476b2021-04-07 15:32:19 +01002461 if name == "hwbinder.stubs" || name == "libcore_private.stubs" {
2462 // Due to a previous bug, these modules were not considered stubs, so we retain that.
2463 return false, javaPlatform
2464 }
Anton Hansson2d0c1942020-05-25 12:20:51 +01002465 return true, javaSdk
2466 }
Jihoon Kangfa4a90d2023-12-20 02:53:38 +00002467 if hasStubsLibrarySuffix(name, apiScopeSystem) {
Anton Hansson2d0c1942020-05-25 12:20:51 +01002468 return true, javaSystem
2469 }
Jihoon Kangfa4a90d2023-12-20 02:53:38 +00002470 if hasStubsLibrarySuffix(name, apiScopeModuleLib) {
Anton Hansson2d0c1942020-05-25 12:20:51 +01002471 return true, javaModule
2472 }
Jihoon Kangfa4a90d2023-12-20 02:53:38 +00002473 if hasStubsLibrarySuffix(name, apiScopeTest) {
Anton Hansson2d0c1942020-05-25 12:20:51 +01002474 return true, javaSystem
2475 }
Jihoon Kangfa4a90d2023-12-20 02:53:38 +00002476 if hasStubsLibrarySuffix(name, apiScopeSystemServer) {
Jihoon Kang1147b312023-06-08 23:25:57 +00002477 return true, javaSystemServer
2478 }
Anton Hansson2d0c1942020-05-25 12:20:51 +01002479 return false, javaPlatform
2480}
2481
Jaewoong Jung4f158ee2019-07-11 10:05:35 -07002482// java_sdk_library is a special Java library that provides optional platform APIs to apps.
2483// In practice, it can be viewed as a combination of several modules: 1) stubs library that clients
2484// are linked against to, 2) droiddoc module that internally generates API stubs source files,
2485// 3) the real runtime shared library that implements the APIs, and 4) XML file for adding
2486// the runtime lib to the classpath at runtime if requested via <uses-library>.
Inseob Kimc0907f12019-02-08 21:00:45 +09002487func SdkLibraryFactory() android.Module {
2488 module := &SdkLibrary{}
Paul Duffinc3091c82020-05-08 14:16:20 +01002489
2490 // Initialize information common between source and prebuilt.
Paul Duffin71b33cc2021-06-23 11:39:47 +01002491 module.initCommon(module)
Paul Duffinc3091c82020-05-08 14:16:20 +01002492
Inseob Kimc0907f12019-02-08 21:00:45 +09002493 module.InitSdkLibraryProperties()
Jooyung Han58f26ab2019-12-18 15:34:32 +09002494 android.InitApexModule(module)
Sundong Ahn054b19a2018-10-19 13:46:09 +09002495 InitJavaModule(module, android.HostAndDeviceSupported)
Paul Duffin3375e352020-04-28 10:44:03 +01002496
2497 // Initialize the map from scope to scope specific properties.
2498 scopeToProperties := make(map[*apiScope]*ApiScopeProperties)
2499 for _, scope := range allApiScopes {
2500 scopeToProperties[scope] = scope.scopeSpecificProperties(module)
2501 }
2502 module.scopeToProperties = scopeToProperties
2503
Paul Duffin4911a892020-04-29 23:35:13 +01002504 // Add the properties containing visibility rules so that they are checked.
Paul Duffin5df79302020-05-16 15:52:12 +01002505 android.AddVisibilityProperty(module, "impl_library_visibility", &module.sdkLibraryProperties.Impl_library_visibility)
Paul Duffin4911a892020-04-29 23:35:13 +01002506 android.AddVisibilityProperty(module, "stubs_library_visibility", &module.sdkLibraryProperties.Stubs_library_visibility)
2507 android.AddVisibilityProperty(module, "stubs_source_visibility", &module.sdkLibraryProperties.Stubs_source_visibility)
2508
Paul Duffin1b1e8062020-05-08 13:44:43 +01002509 module.SetDefaultableHook(func(ctx android.DefaultableHookContext) {
Paul Duffindfa131e2020-05-15 20:37:11 +01002510 // If no implementation is required then it cannot be used as a shared library
2511 // either.
2512 if !module.requiresRuntimeImplementationLibrary() {
2513 // If shared_library has been explicitly set to true then it is incompatible
2514 // with api_only: true.
2515 if proptools.Bool(module.commonSdkLibraryProperties.Shared_library) {
2516 ctx.PropertyErrorf("api_only/shared_library", "inconsistent settings, shared_library and api_only cannot both be true")
2517 }
2518 // Set shared_library: false.
2519 module.commonSdkLibraryProperties.Shared_library = proptools.BoolPtr(false)
2520 }
2521
Paul Duffin1b1e8062020-05-08 13:44:43 +01002522 if module.initCommonAfterDefaultsApplied(ctx) {
2523 module.CreateInternalModules(ctx)
2524 }
2525 })
Jiyong Parkc678ad32018-04-10 13:07:10 +09002526 return module
2527}
Colin Cross79c7c262019-04-17 11:11:46 -07002528
2529//
2530// SDK library prebuilts
2531//
2532
Paul Duffin56d44902020-01-31 13:36:25 +00002533// Properties associated with each api scope.
2534type sdkLibraryScopeProperties struct {
Colin Cross79c7c262019-04-17 11:11:46 -07002535 Jars []string `android:"path"`
2536
2537 Sdk_version *string
2538
Colin Cross79c7c262019-04-17 11:11:46 -07002539 // List of shared java libs that this module has dependencies to
2540 Libs []string
Paul Duffin3d1248c2020-04-09 00:10:17 +01002541
Paul Duffinc8782502020-04-29 20:45:27 +01002542 // The stubs source.
Paul Duffin3d1248c2020-04-09 00:10:17 +01002543 Stub_srcs []string `android:"path"`
Paul Duffin1fd005d2020-04-09 01:08:11 +01002544
2545 // The current.txt
Paul Duffin0f8faff2020-05-20 16:18:00 +01002546 Current_api *string `android:"path"`
Paul Duffin1fd005d2020-04-09 01:08:11 +01002547
2548 // The removed.txt
Paul Duffin0f8faff2020-05-20 16:18:00 +01002549 Removed_api *string `android:"path"`
Anton Hanssond78eb762021-09-21 15:25:12 +01002550
2551 // Annotation zip
2552 Annotations *string `android:"path"`
Colin Cross79c7c262019-04-17 11:11:46 -07002553}
2554
Paul Duffin56d44902020-01-31 13:36:25 +00002555type sdkLibraryImportProperties struct {
Paul Duffinfcfd7912020-01-31 17:54:30 +00002556 // List of shared java libs, common to all scopes, that this module has
2557 // dependencies to
2558 Libs []string
Paul Duffin1267d872021-04-16 17:21:36 +01002559
2560 // If set to true, compile dex files for the stubs. Defaults to false.
2561 Compile_dex *bool
Paul Duffin869de142021-07-15 14:14:41 +01002562
2563 // If not empty, classes are restricted to the specified packages and their sub-packages.
Paul Duffin869de142021-07-15 14:14:41 +01002564 Permitted_packages []string
Spandan Das23956d12024-01-19 00:22:22 +00002565
2566 // Name of the source soong module that gets shadowed by this prebuilt
2567 // If unspecified, follows the naming convention that the source module of
2568 // the prebuilt is Name() without "prebuilt_" prefix
2569 Source_module_name *string
Paul Duffin56d44902020-01-31 13:36:25 +00002570}
2571
Paul Duffineedc5d52020-06-12 17:46:39 +01002572type SdkLibraryImport struct {
Colin Cross79c7c262019-04-17 11:11:46 -07002573 android.ModuleBase
2574 android.DefaultableModuleBase
2575 prebuilt android.Prebuilt
Paul Duffindd46f712020-02-10 13:37:10 +00002576 android.ApexModuleBase
Colin Cross79c7c262019-04-17 11:11:46 -07002577
Paul Duffin37856732021-02-26 14:24:15 +00002578 hiddenAPI
Jiakai Zhang204356f2021-09-09 08:12:46 +00002579 dexpreopter
Paul Duffin37856732021-02-26 14:24:15 +00002580
Colin Cross79c7c262019-04-17 11:11:46 -07002581 properties sdkLibraryImportProperties
2582
Paul Duffin46a26a82020-04-07 19:27:04 +01002583 // Map from api scope to the scope specific property structure.
2584 scopeProperties map[*apiScope]*sdkLibraryScopeProperties
2585
Paul Duffin56d44902020-01-31 13:36:25 +00002586 commonToSdkLibraryAndImport
Paul Duffineedc5d52020-06-12 17:46:39 +01002587
2588 // The reference to the implementation library created by the source module.
2589 // Is nil if the source module does not exist.
2590 implLibraryModule *Library
2591
2592 // The reference to the xml permissions module created by the source module.
2593 // Is nil if the source module does not exist.
2594 xmlPermissionsFileModule *sdkLibraryXml
Paul Duffin39853512021-02-26 11:09:39 +00002595
Jeongik Chad5fe8782021-07-08 01:13:11 +09002596 // Build path to the dex implementation jar obtained from the prebuilt_apex, if any.
Spandan Dasfae468e2023-12-12 23:23:53 +00002597 dexJarFile OptionalDexJarPath
2598 dexJarFileErr error
Jeongik Chad5fe8782021-07-08 01:13:11 +09002599
2600 // Expected install file path of the source module(sdk_library)
2601 // or dex implementation jar obtained from the prebuilt_apex, if any.
2602 installFile android.Path
Colin Cross79c7c262019-04-17 11:11:46 -07002603}
2604
Paul Duffineedc5d52020-06-12 17:46:39 +01002605var _ SdkLibraryDependency = (*SdkLibraryImport)(nil)
Colin Cross79c7c262019-04-17 11:11:46 -07002606
Paul Duffin46a26a82020-04-07 19:27:04 +01002607// The type of a structure that contains a field of type sdkLibraryScopeProperties
2608// for each apiscope in allApiScopes, e.g. something like:
Colin Crossd079e0b2022-08-16 10:27:33 -07002609//
2610// struct {
2611// Public sdkLibraryScopeProperties
2612// System sdkLibraryScopeProperties
2613// ...
2614// }
Paul Duffin46a26a82020-04-07 19:27:04 +01002615var allScopeStructType = createAllScopePropertiesStructType()
2616
2617// Dynamically create a structure type for each apiscope in allApiScopes.
2618func createAllScopePropertiesStructType() reflect.Type {
2619 var fields []reflect.StructField
2620 for _, apiScope := range allApiScopes {
2621 field := reflect.StructField{
2622 Name: apiScope.fieldName,
2623 Type: reflect.TypeOf(sdkLibraryScopeProperties{}),
2624 }
2625 fields = append(fields, field)
2626 }
2627
2628 return reflect.StructOf(fields)
2629}
2630
2631// Create an instance of the scope specific structure type and return a map
2632// from apiscope to a pointer to each scope specific field.
2633func createPropertiesInstance() (interface{}, map[*apiScope]*sdkLibraryScopeProperties) {
2634 allScopePropertiesPtr := reflect.New(allScopeStructType)
2635 allScopePropertiesStruct := allScopePropertiesPtr.Elem()
2636 scopeProperties := make(map[*apiScope]*sdkLibraryScopeProperties)
2637
2638 for _, apiScope := range allApiScopes {
2639 field := allScopePropertiesStruct.FieldByName(apiScope.fieldName)
2640 scopeProperties[apiScope] = field.Addr().Interface().(*sdkLibraryScopeProperties)
2641 }
2642
2643 return allScopePropertiesPtr.Interface(), scopeProperties
2644}
2645
Jaewoong Jung4f158ee2019-07-11 10:05:35 -07002646// java_sdk_library_import imports a prebuilt java_sdk_library.
Colin Cross79c7c262019-04-17 11:11:46 -07002647func sdkLibraryImportFactory() android.Module {
Paul Duffineedc5d52020-06-12 17:46:39 +01002648 module := &SdkLibraryImport{}
Colin Cross79c7c262019-04-17 11:11:46 -07002649
Paul Duffin46a26a82020-04-07 19:27:04 +01002650 allScopeProperties, scopeToProperties := createPropertiesInstance()
2651 module.scopeProperties = scopeToProperties
Jiakai Zhang9c4dc192023-02-09 00:09:24 +08002652 module.AddProperties(&module.properties, allScopeProperties, &module.importDexpreoptProperties)
Colin Cross79c7c262019-04-17 11:11:46 -07002653
Paul Duffinc3091c82020-05-08 14:16:20 +01002654 // Initialize information common between source and prebuilt.
Paul Duffin71b33cc2021-06-23 11:39:47 +01002655 module.initCommon(module)
Paul Duffinc3091c82020-05-08 14:16:20 +01002656
Paul Duffin0bdcb272020-02-06 15:24:57 +00002657 android.InitPrebuiltModule(module, &[]string{""})
Paul Duffindd46f712020-02-10 13:37:10 +00002658 android.InitApexModule(module)
Colin Cross79c7c262019-04-17 11:11:46 -07002659 InitJavaModule(module, android.HostAndDeviceSupported)
2660
Paul Duffin1b1e8062020-05-08 13:44:43 +01002661 module.SetDefaultableHook(func(mctx android.DefaultableHookContext) {
2662 if module.initCommonAfterDefaultsApplied(mctx) {
2663 module.createInternalModules(mctx)
2664 }
2665 })
Colin Cross79c7c262019-04-17 11:11:46 -07002666 return module
2667}
2668
Paul Duffin630b11e2021-07-15 13:35:26 +01002669var _ PermittedPackagesForUpdatableBootJars = (*SdkLibraryImport)(nil)
2670
2671func (module *SdkLibraryImport) PermittedPackagesForUpdatableBootJars() []string {
2672 return module.properties.Permitted_packages
2673}
2674
Paul Duffineedc5d52020-06-12 17:46:39 +01002675func (module *SdkLibraryImport) Prebuilt() *android.Prebuilt {
Colin Cross79c7c262019-04-17 11:11:46 -07002676 return &module.prebuilt
2677}
2678
Paul Duffineedc5d52020-06-12 17:46:39 +01002679func (module *SdkLibraryImport) Name() string {
Colin Cross79c7c262019-04-17 11:11:46 -07002680 return module.prebuilt.Name(module.ModuleBase.Name())
2681}
2682
Spandan Das23956d12024-01-19 00:22:22 +00002683func (module *SdkLibraryImport) BaseModuleName() string {
2684 return proptools.StringDefault(module.properties.Source_module_name, module.ModuleBase.Name())
2685}
2686
Paul Duffineedc5d52020-06-12 17:46:39 +01002687func (module *SdkLibraryImport) createInternalModules(mctx android.DefaultableHookContext) {
Colin Cross79c7c262019-04-17 11:11:46 -07002688
Paul Duffin50061512020-01-21 16:31:05 +00002689 // If the build is configured to use prebuilts then force this to be preferred.
Jeongik Cha816a23a2020-07-08 01:09:23 +09002690 if mctx.Config().AlwaysUsePrebuiltSdks() {
Paul Duffin50061512020-01-21 16:31:05 +00002691 module.prebuilt.ForcePrefer()
2692 }
2693
Paul Duffin46a26a82020-04-07 19:27:04 +01002694 for apiScope, scopeProperties := range module.scopeProperties {
Paul Duffin56d44902020-01-31 13:36:25 +00002695 if len(scopeProperties.Jars) == 0 {
2696 continue
2697 }
2698
Paul Duffinbbb546b2020-04-09 00:07:11 +01002699 module.createJavaImportForStubs(mctx, apiScope, scopeProperties)
Paul Duffin3d1248c2020-04-09 00:10:17 +01002700
Paul Duffin0f8faff2020-05-20 16:18:00 +01002701 if len(scopeProperties.Stub_srcs) > 0 {
2702 module.createPrebuiltStubsSources(mctx, apiScope, scopeProperties)
2703 }
Jihoon Kang71c86832023-09-13 01:01:53 +00002704
2705 if scopeProperties.Current_api != nil {
2706 module.createPrebuiltApiContribution(mctx, apiScope, scopeProperties)
2707 }
Paul Duffin56d44902020-01-31 13:36:25 +00002708 }
Colin Cross79c7c262019-04-17 11:11:46 -07002709
2710 javaSdkLibraries := javaSdkLibraries(mctx.Config())
2711 javaSdkLibrariesLock.Lock()
2712 defer javaSdkLibrariesLock.Unlock()
2713 *javaSdkLibraries = append(*javaSdkLibraries, module.BaseModuleName())
2714}
2715
Paul Duffineedc5d52020-06-12 17:46:39 +01002716func (module *SdkLibraryImport) createJavaImportForStubs(mctx android.DefaultableHookContext, apiScope *apiScope, scopeProperties *sdkLibraryScopeProperties) {
Paul Duffinbbb546b2020-04-09 00:07:11 +01002717 // Creates a java import for the jar with ".stubs" suffix
2718 props := struct {
Spandan Das23956d12024-01-19 00:22:22 +00002719 Name *string
2720 Source_module_name *string
2721 Created_by_java_sdk_library_name *string
2722 Sdk_version *string
2723 Libs []string
2724 Jars []string
2725 Compile_dex *bool
Jihoon Kangfe914ed2024-02-12 22:49:21 +00002726 Is_stubs_module *bool
Paul Duffinbf4de042022-09-27 12:41:52 +01002727
2728 android.UserSuppliedPrebuiltProperties
Paul Duffinbbb546b2020-04-09 00:07:11 +01002729 }{}
Paul Duffinc3091c82020-05-08 14:16:20 +01002730 props.Name = proptools.StringPtr(module.stubsLibraryModuleName(apiScope))
Spandan Das23956d12024-01-19 00:22:22 +00002731 props.Source_module_name = proptools.StringPtr(apiScope.stubsLibraryModuleName(module.BaseModuleName()))
2732 props.Created_by_java_sdk_library_name = proptools.StringPtr(module.RootLibraryName())
Paul Duffinbbb546b2020-04-09 00:07:11 +01002733 props.Sdk_version = scopeProperties.Sdk_version
2734 // Prepend any of the libs from the legacy public properties to the libs for each of the
2735 // scopes to avoid having to duplicate them in each scope.
2736 props.Libs = append(module.properties.Libs, scopeProperties.Libs...)
2737 props.Jars = scopeProperties.Jars
Paul Duffin1dbe3ca2020-05-16 09:57:59 +01002738
Paul Duffin38b57852020-05-13 16:08:09 +01002739 // The imports are preferred if the java_sdk_library_import is preferred.
Paul Duffinbf4de042022-09-27 12:41:52 +01002740 props.CopyUserSuppliedPropertiesFromPrebuilt(&module.prebuilt)
Paul Duffin859fe962020-05-15 10:20:31 +01002741
Paul Duffin1267d872021-04-16 17:21:36 +01002742 // The imports need to be compiled to dex if the java_sdk_library_import requests it.
Paul Duffinf4600f62021-05-13 22:34:45 +01002743 compileDex := module.properties.Compile_dex
2744 if module.stubLibrariesCompiledForDex() {
2745 compileDex = proptools.BoolPtr(true)
2746 }
2747 props.Compile_dex = compileDex
Jihoon Kangfe914ed2024-02-12 22:49:21 +00002748 props.Is_stubs_module = proptools.BoolPtr(true)
Paul Duffin1267d872021-04-16 17:21:36 +01002749
Paul Duffin859fe962020-05-15 10:20:31 +01002750 mctx.CreateModule(ImportFactory, &props, module.sdkComponentPropertiesForChildLibrary())
Paul Duffinbbb546b2020-04-09 00:07:11 +01002751}
2752
Paul Duffineedc5d52020-06-12 17:46:39 +01002753func (module *SdkLibraryImport) createPrebuiltStubsSources(mctx android.DefaultableHookContext, apiScope *apiScope, scopeProperties *sdkLibraryScopeProperties) {
Paul Duffin3d1248c2020-04-09 00:10:17 +01002754 props := struct {
Spandan Das23956d12024-01-19 00:22:22 +00002755 Name *string
2756 Source_module_name *string
2757 Created_by_java_sdk_library_name *string
2758 Srcs []string
Paul Duffinbf4de042022-09-27 12:41:52 +01002759
2760 android.UserSuppliedPrebuiltProperties
Paul Duffin3d1248c2020-04-09 00:10:17 +01002761 }{}
Paul Duffinc3091c82020-05-08 14:16:20 +01002762 props.Name = proptools.StringPtr(module.stubsSourceModuleName(apiScope))
Spandan Das23956d12024-01-19 00:22:22 +00002763 props.Source_module_name = proptools.StringPtr(apiScope.stubsSourceModuleName(module.BaseModuleName()))
2764 props.Created_by_java_sdk_library_name = proptools.StringPtr(module.RootLibraryName())
Paul Duffin3d1248c2020-04-09 00:10:17 +01002765 props.Srcs = scopeProperties.Stub_srcs
Paul Duffin38b57852020-05-13 16:08:09 +01002766
2767 // The stubs source is preferred if the java_sdk_library_import is preferred.
Paul Duffinbf4de042022-09-27 12:41:52 +01002768 props.CopyUserSuppliedPropertiesFromPrebuilt(&module.prebuilt)
2769
Spandan Das2cc80ba2023-10-27 17:21:52 +00002770 mctx.CreateModule(PrebuiltStubsSourcesFactory, &props, module.sdkComponentPropertiesForChildLibrary())
Paul Duffin3d1248c2020-04-09 00:10:17 +01002771}
2772
Jihoon Kang71c86832023-09-13 01:01:53 +00002773func (module *SdkLibraryImport) createPrebuiltApiContribution(mctx android.DefaultableHookContext, apiScope *apiScope, scopeProperties *sdkLibraryScopeProperties) {
2774 api_file := scopeProperties.Current_api
2775 api_surface := &apiScope.name
2776
2777 props := struct {
Spandan Das23956d12024-01-19 00:22:22 +00002778 Name *string
2779 Source_module_name *string
2780 Created_by_java_sdk_library_name *string
2781 Api_surface *string
2782 Api_file *string
2783 Visibility []string
Jihoon Kang71c86832023-09-13 01:01:53 +00002784 }{}
2785
2786 props.Name = proptools.StringPtr(module.stubsSourceModuleName(apiScope) + ".api.contribution")
Spandan Das23956d12024-01-19 00:22:22 +00002787 props.Source_module_name = proptools.StringPtr(apiScope.stubsSourceModuleName(module.BaseModuleName()) + ".api.contribution")
2788 props.Created_by_java_sdk_library_name = proptools.StringPtr(module.RootLibraryName())
Jihoon Kang71c86832023-09-13 01:01:53 +00002789 props.Api_surface = api_surface
2790 props.Api_file = api_file
2791 props.Visibility = []string{"//visibility:override", "//visibility:public"}
2792
Spandan Das2cc80ba2023-10-27 17:21:52 +00002793 mctx.CreateModule(ApiContributionImportFactory, &props, module.sdkComponentPropertiesForChildLibrary())
Jihoon Kang71c86832023-09-13 01:01:53 +00002794}
2795
Paul Duffin44f1d842020-06-26 20:17:02 +01002796// Add the dependencies on the child module in the component deps mutator so that it
2797// creates references to the prebuilt and not the source modules.
2798func (module *SdkLibraryImport) ComponentDepsMutator(ctx android.BottomUpMutatorContext) {
Paul Duffin46a26a82020-04-07 19:27:04 +01002799 for apiScope, scopeProperties := range module.scopeProperties {
Paul Duffin56d44902020-01-31 13:36:25 +00002800 if len(scopeProperties.Jars) == 0 {
2801 continue
2802 }
2803
2804 // Add dependencies to the prebuilt stubs library
Jihoon Kangb7431552024-01-22 19:40:08 +00002805 ctx.AddVariationDependencies(nil, apiScope.prebuiltStubsTag, android.PrebuiltNameFromSource(module.stubsLibraryModuleName(apiScope)))
Paul Duffin0f8faff2020-05-20 16:18:00 +01002806
2807 if len(scopeProperties.Stub_srcs) > 0 {
2808 // Add dependencies to the prebuilt stubs source library
Paul Duffin864116c2021-04-02 10:24:13 +01002809 ctx.AddVariationDependencies(nil, apiScope.stubsSourceTag, android.PrebuiltNameFromSource(module.stubsSourceModuleName(apiScope)))
Paul Duffin0f8faff2020-05-20 16:18:00 +01002810 }
Paul Duffin56d44902020-01-31 13:36:25 +00002811 }
Paul Duffin44f1d842020-06-26 20:17:02 +01002812}
2813
2814// Add other dependencies as normal.
2815func (module *SdkLibraryImport) DepsMutator(ctx android.BottomUpMutatorContext) {
Paul Duffineedc5d52020-06-12 17:46:39 +01002816
2817 implName := module.implLibraryModuleName()
2818 if ctx.OtherModuleExists(implName) {
2819 ctx.AddVariationDependencies(nil, implLibraryTag, implName)
2820
2821 xmlPermissionsModuleName := module.xmlPermissionsModuleName()
2822 if module.sharedLibrary() && ctx.OtherModuleExists(xmlPermissionsModuleName) {
2823 // Add dependency to the rule for generating the xml permissions file
2824 ctx.AddDependency(module, xmlPermissionsFileTag, xmlPermissionsModuleName)
2825 }
2826 }
Colin Cross79c7c262019-04-17 11:11:46 -07002827}
2828
Jiyong Park45bf82e2020-12-15 22:29:02 +09002829var _ android.ApexModule = (*SdkLibraryImport)(nil)
2830
2831// Implements android.ApexModule
Paul Duffineedc5d52020-06-12 17:46:39 +01002832func (module *SdkLibraryImport) DepIsInSameApex(mctx android.BaseModuleContext, dep android.Module) bool {
2833 depTag := mctx.OtherModuleDependencyTag(dep)
2834 if depTag == xmlPermissionsFileTag {
2835 return true
2836 }
2837
2838 // None of the other dependencies of the java_sdk_library_import are in the same apex
2839 // as the one that references this module.
2840 return false
2841}
2842
Jiyong Park45bf82e2020-12-15 22:29:02 +09002843// Implements android.ApexModule
Dan Albertc8060532020-07-22 22:32:17 -07002844func (module *SdkLibraryImport) ShouldSupportSdkVersion(ctx android.BaseModuleContext,
2845 sdkVersion android.ApiLevel) error {
Jooyung Han749dc692020-04-15 11:03:39 +09002846 // we don't check prebuilt modules for sdk_version
2847 return nil
2848}
2849
Paul Duffinea8f8082021-06-24 13:25:57 +01002850// Implements android.ApexModule
2851func (module *SdkLibraryImport) UniqueApexVariations() bool {
2852 return module.uniqueApexVariations()
2853}
2854
Paul Duffin09817d62022-04-28 17:45:11 +01002855// MinSdkVersion - Implements hiddenAPIModule
Spandan Das8c9ae7e2023-03-03 21:20:36 +00002856func (module *SdkLibraryImport) MinSdkVersion(ctx android.EarlyModuleContext) android.ApiLevel {
2857 return android.NoneApiLevel
Paul Duffin09817d62022-04-28 17:45:11 +01002858}
2859
2860var _ hiddenAPIModule = (*SdkLibraryImport)(nil)
2861
Paul Duffineedc5d52020-06-12 17:46:39 +01002862func (module *SdkLibraryImport) OutputFiles(tag string) (android.Paths, error) {
Paul Duffin1e940d52022-04-29 14:21:25 +01002863 paths, err := module.commonOutputFiles(tag)
2864 if paths != nil || err != nil {
2865 return paths, err
2866 }
2867 if module.implLibraryModule != nil {
2868 return module.implLibraryModule.OutputFiles(tag)
2869 } else {
2870 return nil, nil
2871 }
Paul Duffin46dc45a2020-05-14 15:39:10 +01002872}
2873
Paul Duffineedc5d52020-06-12 17:46:39 +01002874func (module *SdkLibraryImport) GenerateAndroidBuildActions(ctx android.ModuleContext) {
Paul Duffina2ae7e02020-09-11 11:55:00 +01002875 module.generateCommonBuildActions(ctx)
2876
Jeongik Chad5fe8782021-07-08 01:13:11 +09002877 // Assume that source module(sdk_library) is installed in /<sdk_library partition>/framework
2878 module.installFile = android.PathForModuleInstall(ctx, "framework", module.Stem()+".jar")
2879
Paul Duffin0f8faff2020-05-20 16:18:00 +01002880 // Record the paths to the prebuilt stubs library and stubs source.
Colin Cross79c7c262019-04-17 11:11:46 -07002881 ctx.VisitDirectDeps(func(to android.Module) {
2882 tag := ctx.OtherModuleDependencyTag(to)
2883
Paul Duffin0f8faff2020-05-20 16:18:00 +01002884 // Extract information from any of the scope specific dependencies.
2885 if scopeTag, ok := tag.(scopeDependencyTag); ok {
2886 apiScope := scopeTag.apiScope
2887 scopePaths := module.getScopePathsCreateIfNeeded(apiScope)
2888
2889 // Extract information from the dependency. The exact information extracted
2890 // is determined by the nature of the dependency which is determined by the tag.
2891 scopeTag.extractDepInfo(ctx, to, scopePaths)
Paul Duffineedc5d52020-06-12 17:46:39 +01002892 } else if tag == implLibraryTag {
2893 if implLibrary, ok := to.(*Library); ok {
2894 module.implLibraryModule = implLibrary
2895 } else {
2896 ctx.ModuleErrorf("implementation library must be of type *java.Library but was %T", to)
2897 }
2898 } else if tag == xmlPermissionsFileTag {
2899 if xmlPermissionsFileModule, ok := to.(*sdkLibraryXml); ok {
2900 module.xmlPermissionsFileModule = xmlPermissionsFileModule
2901 } else {
2902 ctx.ModuleErrorf("xml permissions file module must be of type *sdkLibraryXml but was %T", to)
2903 }
Colin Cross79c7c262019-04-17 11:11:46 -07002904 }
2905 })
Paul Duffin0f8faff2020-05-20 16:18:00 +01002906
2907 // Populate the scope paths with information from the properties.
2908 for apiScope, scopeProperties := range module.scopeProperties {
2909 if len(scopeProperties.Jars) == 0 {
2910 continue
2911 }
2912
2913 paths := module.getScopePathsCreateIfNeeded(apiScope)
Anton Hanssond78eb762021-09-21 15:25:12 +01002914 paths.annotationsZip = android.OptionalPathForModuleSrc(ctx, scopeProperties.Annotations)
Paul Duffin0f8faff2020-05-20 16:18:00 +01002915 paths.currentApiFilePath = android.OptionalPathForModuleSrc(ctx, scopeProperties.Current_api)
2916 paths.removedApiFilePath = android.OptionalPathForModuleSrc(ctx, scopeProperties.Removed_api)
2917 }
Paul Duffin39853512021-02-26 11:09:39 +00002918
2919 if ctx.Device() {
2920 // If this is a variant created for a prebuilt_apex then use the dex implementation jar
2921 // obtained from the associated deapexer module.
Colin Crossff694a82023-12-13 15:54:49 -08002922 ai, _ := android.ModuleProvider(ctx, android.ApexInfoProvider)
Paul Duffin39853512021-02-26 11:09:39 +00002923 if ai.ForPrebuiltApex {
Paul Duffin39853512021-02-26 11:09:39 +00002924 // Get the path of the dex implementation jar from the `deapexer` module.
Spandan Dasfae468e2023-12-12 23:23:53 +00002925 di, err := android.FindDeapexerProviderForModule(ctx)
2926 if err != nil {
2927 // An error was found, possibly due to multiple apexes in the tree that export this library
2928 // Defer the error till a client tries to call DexJarBuildPath
2929 module.dexJarFileErr = err
Spandan Das3a392012024-01-17 18:26:27 +00002930 module.initHiddenAPIError(err)
Spandan Dasfae468e2023-12-12 23:23:53 +00002931 return
Martin Stjernholm44825602021-09-17 01:44:12 +01002932 }
Spandan Das5be63332023-12-13 00:06:32 +00002933 dexJarFileApexRootRelative := ApexRootRelativePathToJavaLib(module.BaseModuleName())
Jiakai Zhang81e46812023-02-08 21:56:07 +08002934 if dexOutputPath := di.PrebuiltExportPath(dexJarFileApexRootRelative); dexOutputPath != nil {
Martin Stjernholm8be1e6d2021-09-15 03:34:04 +01002935 dexJarFile := makeDexJarPathFromPath(dexOutputPath)
2936 module.dexJarFile = dexJarFile
Jiakai Zhang204356f2021-09-09 08:12:46 +00002937 installPath := android.PathForModuleInPartitionInstall(
Jiakai Zhang81e46812023-02-08 21:56:07 +08002938 ctx, "apex", ai.ApexVariationName, dexJarFileApexRootRelative)
Jiakai Zhang204356f2021-09-09 08:12:46 +00002939 module.installFile = installPath
Martin Stjernholm8be1e6d2021-09-15 03:34:04 +01002940 module.initHiddenAPI(ctx, dexJarFile, module.findScopePaths(apiScopePublic).stubsImplPath[0], nil)
Jiakai Zhang204356f2021-09-09 08:12:46 +00002941
Spandan Dase21a8d42024-01-23 23:56:29 +00002942 module.dexpreopter.installPath = module.dexpreopter.getInstallPath(ctx, android.RemoveOptionalPrebuiltPrefix(ctx.ModuleName()), installPath)
Jiakai Zhang204356f2021-09-09 08:12:46 +00002943 module.dexpreopter.isSDKLibrary = true
Spandan Dase21a8d42024-01-23 23:56:29 +00002944 module.dexpreopter.uncompressedDex = shouldUncompressDex(ctx, android.RemoveOptionalPrebuiltPrefix(ctx.ModuleName()), &module.dexpreopter)
Jiakai Zhang81e46812023-02-08 21:56:07 +08002945
2946 if profilePath := di.PrebuiltExportPath(dexJarFileApexRootRelative + ".prof"); profilePath != nil {
2947 module.dexpreopter.inputProfilePathOnHost = profilePath
2948 }
Paul Duffin39853512021-02-26 11:09:39 +00002949 } else {
2950 // This should never happen as a variant for a prebuilt_apex is only created if the
2951 // prebuilt_apex has been configured to export the java library dex file.
Martin Stjernholm44825602021-09-17 01:44:12 +01002952 ctx.ModuleErrorf("internal error: no dex implementation jar available from prebuilt APEX %s", di.ApexModuleName())
Paul Duffin39853512021-02-26 11:09:39 +00002953 }
2954 }
2955 }
Colin Cross79c7c262019-04-17 11:11:46 -07002956}
2957
Jiyong Parkf1691d22021-03-29 20:11:58 +09002958func (module *SdkLibraryImport) sdkJars(ctx android.BaseModuleContext, sdkVersion android.SdkSpec, headerJars bool) android.Paths {
Paul Duffineedc5d52020-06-12 17:46:39 +01002959
2960 // For consistency with SdkLibrary make the implementation jar available to libraries that
2961 // are within the same APEX.
2962 implLibraryModule := module.implLibraryModule
Colin Cross56a83212020-09-15 18:30:11 -07002963 if implLibraryModule != nil && withinSameApexesAs(ctx, module) {
Paul Duffineedc5d52020-06-12 17:46:39 +01002964 if headerJars {
2965 return implLibraryModule.HeaderJars()
2966 } else {
2967 return implLibraryModule.ImplementationJars()
2968 }
2969 }
2970
Paul Duffin23970f42020-05-20 14:20:02 +01002971 return module.selectHeaderJarsForSdkVersion(ctx, sdkVersion)
Paul Duffin56d44902020-01-31 13:36:25 +00002972}
2973
Colin Cross79c7c262019-04-17 11:11:46 -07002974// to satisfy SdkLibraryDependency interface
Jiyong Parkf1691d22021-03-29 20:11:58 +09002975func (module *SdkLibraryImport) SdkHeaderJars(ctx android.BaseModuleContext, sdkVersion android.SdkSpec) android.Paths {
Colin Cross79c7c262019-04-17 11:11:46 -07002976 // This module is just a wrapper for the prebuilt stubs.
Paul Duffineedc5d52020-06-12 17:46:39 +01002977 return module.sdkJars(ctx, sdkVersion, true)
Colin Cross79c7c262019-04-17 11:11:46 -07002978}
2979
Ulya Trafimovichdbf31662020-12-17 12:07:54 +00002980// to satisfy UsesLibraryDependency interface
Spandan Das59a4a2b2024-01-09 21:35:56 +00002981func (module *SdkLibraryImport) DexJarBuildPath(ctx android.ModuleErrorfContext) OptionalDexJarPath {
Paul Duffin39853512021-02-26 11:09:39 +00002982 // The dex implementation jar extracted from the .apex file should be used in preference to the
2983 // source.
Spandan Dasfae468e2023-12-12 23:23:53 +00002984 if module.dexJarFileErr != nil {
Spandan Das59a4a2b2024-01-09 21:35:56 +00002985 ctx.ModuleErrorf(module.dexJarFileErr.Error())
Spandan Dasfae468e2023-12-12 23:23:53 +00002986 }
Martin Stjernholm8be1e6d2021-09-15 03:34:04 +01002987 if module.dexJarFile.IsSet() {
Paul Duffin39853512021-02-26 11:09:39 +00002988 return module.dexJarFile
2989 }
Paul Duffineedc5d52020-06-12 17:46:39 +01002990 if module.implLibraryModule == nil {
Martin Stjernholm8be1e6d2021-09-15 03:34:04 +01002991 return makeUnsetDexJarPath()
Paul Duffineedc5d52020-06-12 17:46:39 +01002992 } else {
Spandan Das59a4a2b2024-01-09 21:35:56 +00002993 return module.implLibraryModule.DexJarBuildPath(ctx)
Paul Duffineedc5d52020-06-12 17:46:39 +01002994 }
2995}
2996
Ulya Trafimovichdbf31662020-12-17 12:07:54 +00002997// to satisfy UsesLibraryDependency interface
Ulya Trafimovich31e444e2020-08-14 17:32:16 +01002998func (module *SdkLibraryImport) DexJarInstallPath() android.Path {
Jeongik Chad5fe8782021-07-08 01:13:11 +09002999 return module.installFile
Ulya Trafimovich31e444e2020-08-14 17:32:16 +01003000}
3001
Ulya Trafimovichdbf31662020-12-17 12:07:54 +00003002// to satisfy UsesLibraryDependency interface
3003func (module *SdkLibraryImport) ClassLoaderContexts() dexpreopt.ClassLoaderContextMap {
3004 return nil
3005}
3006
Paul Duffineedc5d52020-06-12 17:46:39 +01003007// to satisfy apex.javaDependency interface
3008func (module *SdkLibraryImport) JacocoReportClassesFile() android.Path {
3009 if module.implLibraryModule == nil {
3010 return nil
3011 } else {
3012 return module.implLibraryModule.JacocoReportClassesFile()
3013 }
3014}
3015
3016// to satisfy apex.javaDependency interface
Colin Cross08dca382020-07-21 20:31:17 -07003017func (module *SdkLibraryImport) LintDepSets() LintDepSets {
3018 if module.implLibraryModule == nil {
3019 return LintDepSets{}
3020 } else {
3021 return module.implLibraryModule.LintDepSets()
3022 }
3023}
3024
Spandan Das17854f52022-01-14 21:19:14 +00003025func (module *SdkLibraryImport) GetStrictUpdatabilityLinting() bool {
Jaewoong Jung476b9d62021-05-10 15:30:00 -07003026 if module.implLibraryModule == nil {
3027 return false
3028 } else {
Spandan Das17854f52022-01-14 21:19:14 +00003029 return module.implLibraryModule.GetStrictUpdatabilityLinting()
Jaewoong Jung476b9d62021-05-10 15:30:00 -07003030 }
3031}
3032
Spandan Das17854f52022-01-14 21:19:14 +00003033func (module *SdkLibraryImport) SetStrictUpdatabilityLinting(strictLinting bool) {
Jaewoong Jung476b9d62021-05-10 15:30:00 -07003034 if module.implLibraryModule != nil {
Spandan Das17854f52022-01-14 21:19:14 +00003035 module.implLibraryModule.SetStrictUpdatabilityLinting(strictLinting)
Jaewoong Jung476b9d62021-05-10 15:30:00 -07003036 }
3037}
3038
Colin Cross08dca382020-07-21 20:31:17 -07003039// to satisfy apex.javaDependency interface
Paul Duffineedc5d52020-06-12 17:46:39 +01003040func (module *SdkLibraryImport) Stem() string {
3041 return module.BaseModuleName()
Colin Cross79c7c262019-04-17 11:11:46 -07003042}
Jiyong Parke3833882020-02-17 17:28:10 +09003043
Paul Duffin44b481b2020-06-17 16:59:43 +01003044var _ ApexDependency = (*SdkLibraryImport)(nil)
3045
3046// to satisfy java.ApexDependency interface
3047func (module *SdkLibraryImport) HeaderJars() android.Paths {
3048 if module.implLibraryModule == nil {
3049 return nil
3050 } else {
3051 return module.implLibraryModule.HeaderJars()
3052 }
3053}
3054
3055// to satisfy java.ApexDependency interface
3056func (module *SdkLibraryImport) ImplementationAndResourcesJars() android.Paths {
3057 if module.implLibraryModule == nil {
3058 return nil
3059 } else {
3060 return module.implLibraryModule.ImplementationAndResourcesJars()
3061 }
3062}
3063
Jiakai Zhang204356f2021-09-09 08:12:46 +00003064// to satisfy java.DexpreopterInterface interface
3065func (module *SdkLibraryImport) IsInstallable() bool {
3066 return true
3067}
3068
Paul Duffinfef55002021-06-17 14:56:05 +01003069var _ android.RequiredFilesFromPrebuiltApex = (*SdkLibraryImport)(nil)
3070
Paul Duffinb4bbf2c2021-06-17 15:59:07 +01003071func (module *SdkLibraryImport) RequiredFilesFromPrebuiltApex(ctx android.BaseModuleContext) []string {
Paul Duffinfef55002021-06-17 14:56:05 +01003072 name := module.BaseModuleName()
Jiakai Zhang81e46812023-02-08 21:56:07 +08003073 return requiredFilesFromPrebuiltApexForImport(name, &module.dexpreopter)
Paul Duffinfef55002021-06-17 14:56:05 +01003074}
3075
Spandan Das2ea84dd2024-01-25 22:12:50 +00003076func (j *SdkLibraryImport) UseProfileGuidedDexpreopt() bool {
3077 return proptools.Bool(j.importDexpreoptProperties.Dex_preopt.Profile_guided)
3078}
3079
Jiyong Parke3833882020-02-17 17:28:10 +09003080// java_sdk_library_xml
Jiyong Parke3833882020-02-17 17:28:10 +09003081type sdkLibraryXml struct {
3082 android.ModuleBase
3083 android.DefaultableModuleBase
3084 android.ApexModuleBase
3085
3086 properties sdkLibraryXmlProperties
3087
3088 outputFilePath android.OutputPath
3089 installDirPath android.InstallPath
Colin Cross56a83212020-09-15 18:30:11 -07003090
3091 hideApexVariantFromMake bool
Jiyong Parke3833882020-02-17 17:28:10 +09003092}
3093
3094type sdkLibraryXmlProperties struct {
3095 // canonical name of the lib
3096 Lib_name *string
Pedro Loureiro9956e5e2021-09-07 17:21:59 +00003097
3098 // Signals that this shared library is part of the bootclasspath starting
3099 // on the version indicated in this attribute.
3100 //
3101 // This will make platforms at this level and above to ignore
3102 // <uses-library> tags with this library name because the library is already
3103 // available
3104 On_bootclasspath_since *string
3105
3106 // Signals that this shared library was part of the bootclasspath before
3107 // (but not including) the version indicated in this attribute.
3108 //
3109 // The system will automatically add a <uses-library> tag with this library to
3110 // apps that target any SDK less than the version indicated in this attribute.
3111 On_bootclasspath_before *string
3112
3113 // Indicates that PackageManager should ignore this shared library if the
3114 // platform is below the version indicated in this attribute.
3115 //
3116 // This means that the device won't recognise this library as installed.
3117 Min_device_sdk *string
3118
3119 // Indicates that PackageManager should ignore this shared library if the
3120 // platform is above the version indicated in this attribute.
3121 //
3122 // This means that the device won't recognise this library as installed.
3123 Max_device_sdk *string
Pedro Loureiroc3621422021-09-28 15:40:23 +00003124
3125 // The SdkLibrary's min api level as a string
3126 //
3127 // This value comes from the ApiLevel of the MinSdkVersion property.
3128 Sdk_library_min_api_level *string
Jamie Garsidee570ace2023-11-27 12:07:36 +00003129
3130 // Uses-libs dependencies that the shared library requires to work correctly.
3131 //
3132 // This will add dependency="foo:bar" to the <library> section.
3133 Uses_libs_dependencies []string
Jiyong Parke3833882020-02-17 17:28:10 +09003134}
3135
3136// java_sdk_library_xml builds the permission xml file for a java_sdk_library.
3137// Not to be used directly by users. java_sdk_library internally uses this.
3138func sdkLibraryXmlFactory() android.Module {
3139 module := &sdkLibraryXml{}
3140
3141 module.AddProperties(&module.properties)
3142
3143 android.InitApexModule(module)
3144 android.InitAndroidArchModule(module, android.DeviceSupported, android.MultilibCommon)
3145
3146 return module
3147}
3148
Colin Crossaede88c2020-08-11 12:17:01 -07003149func (module *sdkLibraryXml) UniqueApexVariations() bool {
3150 // sdkLibraryXml needs a unique variation per APEX because the generated XML file contains the path to the
3151 // mounted APEX, which contains the name of the APEX.
3152 return true
3153}
3154
Jiyong Parke3833882020-02-17 17:28:10 +09003155// from android.PrebuiltEtcModule
Jooyung Han0703fd82020-08-26 22:11:53 +09003156func (module *sdkLibraryXml) BaseDir() string {
3157 return "etc"
3158}
3159
3160// from android.PrebuiltEtcModule
Jiyong Parke3833882020-02-17 17:28:10 +09003161func (module *sdkLibraryXml) SubDir() string {
3162 return "permissions"
3163}
3164
3165// from android.PrebuiltEtcModule
ThiƩbaud Weksteen00e8b312024-03-18 14:06:00 +11003166func (module *sdkLibraryXml) OutputFiles(tag string) (android.Paths, error) {
3167 return android.OutputPaths{module.outputFilePath}.Paths(), nil
Jiyong Parke3833882020-02-17 17:28:10 +09003168}
3169
ThiƩbaud Weksteen00e8b312024-03-18 14:06:00 +11003170var _ etc.PrebuiltEtcModule = (*sdkLibraryXml)(nil)
3171
Jiyong Parke3833882020-02-17 17:28:10 +09003172// from android.ApexModule
3173func (module *sdkLibraryXml) AvailableFor(what string) bool {
3174 return true
3175}
3176
3177func (module *sdkLibraryXml) DepsMutator(ctx android.BottomUpMutatorContext) {
3178 // do nothing
3179}
3180
Jiyong Park45bf82e2020-12-15 22:29:02 +09003181var _ android.ApexModule = (*sdkLibraryXml)(nil)
3182
3183// Implements android.ApexModule
Dan Albertc8060532020-07-22 22:32:17 -07003184func (module *sdkLibraryXml) ShouldSupportSdkVersion(ctx android.BaseModuleContext,
3185 sdkVersion android.ApiLevel) error {
Jooyung Han749dc692020-04-15 11:03:39 +09003186 // sdkLibraryXml doesn't need to be checked separately because java_sdk_library is checked
3187 return nil
3188}
3189
Jiyong Parke3833882020-02-17 17:28:10 +09003190// File path to the runtime implementation library
Colin Cross56a83212020-09-15 18:30:11 -07003191func (module *sdkLibraryXml) implPath(ctx android.ModuleContext) string {
Jiyong Parke3833882020-02-17 17:28:10 +09003192 implName := proptools.String(module.properties.Lib_name)
Colin Crossff694a82023-12-13 15:54:49 -08003193 if apexInfo, _ := android.ModuleProvider(ctx, android.ApexInfoProvider); !apexInfo.IsForPlatform() {
Colin Crosse07f2312020-08-13 11:24:56 -07003194 // TODO(b/146468504): ApexVariationName() is only a soong module name, not apex name.
Jiyong Parke3833882020-02-17 17:28:10 +09003195 // In most cases, this works fine. But when apex_name is set or override_apex is used
3196 // this can be wrong.
Colin Cross56a83212020-09-15 18:30:11 -07003197 return fmt.Sprintf("/apex/%s/javalib/%s.jar", apexInfo.ApexVariationName, implName)
Jiyong Parke3833882020-02-17 17:28:10 +09003198 }
3199 partition := "system"
3200 if module.SocSpecific() {
3201 partition = "vendor"
3202 } else if module.DeviceSpecific() {
3203 partition = "odm"
3204 } else if module.ProductSpecific() {
3205 partition = "product"
3206 } else if module.SystemExtSpecific() {
3207 partition = "system_ext"
3208 }
3209 return "/" + partition + "/framework/" + implName + ".jar"
3210}
3211
Pedro Loureiro9956e5e2021-09-07 17:21:59 +00003212func formattedOptionalSdkLevelAttribute(ctx android.ModuleContext, attrName string, value *string) string {
3213 if value == nil {
3214 return ""
3215 }
3216 apiLevel, err := android.ApiLevelFromUser(ctx, *value)
3217 if err != nil {
Pedro Loureiroba6682f2021-10-29 09:32:32 +00003218 // attributes in bp files have underscores but in the xml have dashes.
3219 ctx.PropertyErrorf(strings.ReplaceAll(attrName, "-", "_"), err.Error())
Pedro Loureiro9956e5e2021-09-07 17:21:59 +00003220 return ""
3221 }
Pedro Loureirob638c622021-12-22 15:28:05 +00003222 if apiLevel.IsCurrent() {
3223 // passing "current" would always mean a future release, never the current (or the current in
3224 // progress) which means some conditions would never be triggered.
3225 ctx.PropertyErrorf(strings.ReplaceAll(attrName, "-", "_"),
3226 `"current" is not an allowed value for this attribute`)
3227 return ""
3228 }
Pedro Loureiro48991222022-06-17 20:01:21 +00003229 // "safeValue" is safe because it translates finalized codenames to a string
3230 // with their SDK int.
3231 safeValue := apiLevel.String()
3232 return formattedOptionalAttribute(attrName, &safeValue)
Pedro Loureiro9956e5e2021-09-07 17:21:59 +00003233}
3234
3235// formats an attribute for the xml permissions file if the value is not null
3236// returns empty string otherwise
3237func formattedOptionalAttribute(attrName string, value *string) string {
3238 if value == nil {
3239 return ""
3240 }
Paul Duffin1816cde2024-04-10 10:58:21 +01003241 return fmt.Sprintf(" %s=\"%s\"\n", attrName, *value)
Pedro Loureiro9956e5e2021-09-07 17:21:59 +00003242}
3243
Jamie Garsidee570ace2023-11-27 12:07:36 +00003244func formattedDependenciesAttribute(dependencies []string) string {
3245 if dependencies == nil {
3246 return ""
3247 }
Paul Duffin1816cde2024-04-10 10:58:21 +01003248 return fmt.Sprintf(" dependency=\"%s\"\n", strings.Join(dependencies, ":"))
Jamie Garsidee570ace2023-11-27 12:07:36 +00003249}
3250
Pedro Loureiro9956e5e2021-09-07 17:21:59 +00003251func (module *sdkLibraryXml) permissionsContents(ctx android.ModuleContext) string {
3252 libName := proptools.String(module.properties.Lib_name)
3253 libNameAttr := formattedOptionalAttribute("name", &libName)
3254 filePath := module.implPath(ctx)
3255 filePathAttr := formattedOptionalAttribute("file", &filePath)
Pedro Loureiroba6682f2021-10-29 09:32:32 +00003256 implicitFromAttr := formattedOptionalSdkLevelAttribute(ctx, "on-bootclasspath-since", module.properties.On_bootclasspath_since)
3257 implicitUntilAttr := formattedOptionalSdkLevelAttribute(ctx, "on-bootclasspath-before", module.properties.On_bootclasspath_before)
3258 minSdkAttr := formattedOptionalSdkLevelAttribute(ctx, "min-device-sdk", module.properties.Min_device_sdk)
3259 maxSdkAttr := formattedOptionalSdkLevelAttribute(ctx, "max-device-sdk", module.properties.Max_device_sdk)
Jamie Garsidee570ace2023-11-27 12:07:36 +00003260 dependenciesAttr := formattedDependenciesAttribute(module.properties.Uses_libs_dependencies)
Pedro Loureiro196d3e62021-12-22 19:53:01 +00003261 // <library> is understood in all android versions whereas <apex-library> is only understood from API T (and ignored before that).
3262 // 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 +00003263 var libraryTag string
3264 if module.properties.Min_device_sdk != nil {
Paul Duffin1816cde2024-04-10 10:58:21 +01003265 libraryTag = " <apex-library\n"
Pedro Loureiroc3621422021-09-28 15:40:23 +00003266 } else {
Paul Duffin1816cde2024-04-10 10:58:21 +01003267 libraryTag = " <library\n"
Pedro Loureiroc3621422021-09-28 15:40:23 +00003268 }
Pedro Loureiro9956e5e2021-09-07 17:21:59 +00003269
3270 return strings.Join([]string{
Paul Duffin1816cde2024-04-10 10:58:21 +01003271 "<?xml version=\"1.0\" encoding=\"utf-8\"?>\n",
3272 "<!-- Copyright (C) 2018 The Android Open Source Project\n",
3273 "\n",
3274 " Licensed under the Apache License, Version 2.0 (the \"License\");\n",
3275 " you may not use this file except in compliance with the License.\n",
3276 " You may obtain a copy of the License at\n",
3277 "\n",
3278 " http://www.apache.org/licenses/LICENSE-2.0\n",
3279 "\n",
3280 " Unless required by applicable law or agreed to in writing, software\n",
3281 " distributed under the License is distributed on an \"AS IS\" BASIS,\n",
3282 " WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\n",
3283 " See the License for the specific language governing permissions and\n",
3284 " limitations under the License.\n",
3285 "-->\n",
3286 "<permissions>\n",
Pedro Loureiroc3621422021-09-28 15:40:23 +00003287 libraryTag,
Pedro Loureiro9956e5e2021-09-07 17:21:59 +00003288 libNameAttr,
3289 filePathAttr,
3290 implicitFromAttr,
3291 implicitUntilAttr,
3292 minSdkAttr,
3293 maxSdkAttr,
Jamie Garsidee570ace2023-11-27 12:07:36 +00003294 dependenciesAttr,
Paul Duffin1816cde2024-04-10 10:58:21 +01003295 " />\n",
3296 "</permissions>\n",
3297 }, "")
Pedro Loureiro9956e5e2021-09-07 17:21:59 +00003298}
3299
Jiyong Parke3833882020-02-17 17:28:10 +09003300func (module *sdkLibraryXml) GenerateAndroidBuildActions(ctx android.ModuleContext) {
Colin Crossff694a82023-12-13 15:54:49 -08003301 apexInfo, _ := android.ModuleProvider(ctx, android.ApexInfoProvider)
3302 module.hideApexVariantFromMake = !apexInfo.IsForPlatform()
Colin Cross56a83212020-09-15 18:30:11 -07003303
Jiyong Parke3833882020-02-17 17:28:10 +09003304 libName := proptools.String(module.properties.Lib_name)
Pedro Loureiroc3621422021-09-28 15:40:23 +00003305 module.selfValidate(ctx)
Pedro Loureiro9956e5e2021-09-07 17:21:59 +00003306 xmlContent := module.permissionsContents(ctx)
Jiyong Parke3833882020-02-17 17:28:10 +09003307
3308 module.outputFilePath = android.PathForModuleOut(ctx, libName+".xml").OutputPath
Paul Duffin1816cde2024-04-10 10:58:21 +01003309 android.WriteFileRuleVerbatim(ctx, module.outputFilePath, xmlContent)
Jiyong Parke3833882020-02-17 17:28:10 +09003310
3311 module.installDirPath = android.PathForModuleInstall(ctx, "etc", module.SubDir())
3312}
3313
3314func (module *sdkLibraryXml) AndroidMkEntries() []android.AndroidMkEntries {
Colin Cross56a83212020-09-15 18:30:11 -07003315 if module.hideApexVariantFromMake {
satayev8f088b02021-12-06 11:40:46 +00003316 return []android.AndroidMkEntries{{
Jiyong Parke3833882020-02-17 17:28:10 +09003317 Disabled: true,
3318 }}
3319 }
3320
satayev8f088b02021-12-06 11:40:46 +00003321 return []android.AndroidMkEntries{{
Jiyong Parke3833882020-02-17 17:28:10 +09003322 Class: "ETC",
3323 OutputFile: android.OptionalPathForPath(module.outputFilePath),
3324 ExtraEntries: []android.AndroidMkExtraEntriesFunc{
Colin Crossaa255532020-07-03 13:18:24 -07003325 func(ctx android.AndroidMkExtraEntriesContext, entries *android.AndroidMkEntries) {
Jiyong Parke3833882020-02-17 17:28:10 +09003326 entries.SetString("LOCAL_MODULE_TAGS", "optional")
Colin Crossc68db4b2021-11-11 18:59:15 -08003327 entries.SetString("LOCAL_MODULE_PATH", module.installDirPath.String())
Jiyong Parke3833882020-02-17 17:28:10 +09003328 entries.SetString("LOCAL_INSTALLED_MODULE_STEM", module.outputFilePath.Base())
3329 },
3330 },
3331 }}
3332}
Paul Duffindd46f712020-02-10 13:37:10 +00003333
Pedro Loureiroc3621422021-09-28 15:40:23 +00003334func (module *sdkLibraryXml) selfValidate(ctx android.ModuleContext) {
3335 module.validateAtLeastTAttributes(ctx)
3336 module.validateMinAndMaxDeviceSdk(ctx)
3337 module.validateMinMaxDeviceSdkAndModuleMinSdk(ctx)
3338 module.validateOnBootclasspathBeforeRequirements(ctx)
3339}
3340
3341func (module *sdkLibraryXml) validateAtLeastTAttributes(ctx android.ModuleContext) {
3342 t := android.ApiLevelOrPanic(ctx, "Tiramisu")
3343 module.attrAtLeastT(ctx, t, module.properties.Min_device_sdk, "min_device_sdk")
3344 module.attrAtLeastT(ctx, t, module.properties.Max_device_sdk, "max_device_sdk")
3345 module.attrAtLeastT(ctx, t, module.properties.On_bootclasspath_before, "on_bootclasspath_before")
3346 module.attrAtLeastT(ctx, t, module.properties.On_bootclasspath_since, "on_bootclasspath_since")
3347}
3348
3349func (module *sdkLibraryXml) attrAtLeastT(ctx android.ModuleContext, t android.ApiLevel, attr *string, attrName string) {
3350 if attr != nil {
3351 if level, err := android.ApiLevelFromUser(ctx, *attr); err == nil {
3352 // we will inform the user of invalid inputs when we try to write the
3353 // permissions xml file so we don't need to do it here
3354 if t.GreaterThan(level) {
3355 ctx.PropertyErrorf(attrName, "Attribute value needs to be at least T")
3356 }
3357 }
3358 }
3359}
3360
3361func (module *sdkLibraryXml) validateMinAndMaxDeviceSdk(ctx android.ModuleContext) {
3362 if module.properties.Min_device_sdk != nil && module.properties.Max_device_sdk != nil {
3363 min, minErr := android.ApiLevelFromUser(ctx, *module.properties.Min_device_sdk)
3364 max, maxErr := android.ApiLevelFromUser(ctx, *module.properties.Max_device_sdk)
3365 if minErr == nil && maxErr == nil {
3366 // we will inform the user of invalid inputs when we try to write the
3367 // permissions xml file so we don't need to do it here
3368 if min.GreaterThan(max) {
3369 ctx.ModuleErrorf("min_device_sdk can't be greater than max_device_sdk")
3370 }
3371 }
3372 }
3373}
3374
3375func (module *sdkLibraryXml) validateMinMaxDeviceSdkAndModuleMinSdk(ctx android.ModuleContext) {
3376 moduleMinApi := android.ApiLevelOrPanic(ctx, *module.properties.Sdk_library_min_api_level)
3377 if module.properties.Min_device_sdk != nil {
3378 api, err := android.ApiLevelFromUser(ctx, *module.properties.Min_device_sdk)
3379 if err == nil {
3380 if moduleMinApi.GreaterThan(api) {
3381 ctx.PropertyErrorf("min_device_sdk", "Can't be less than module's min sdk (%s)", moduleMinApi)
3382 }
3383 }
3384 }
3385 if module.properties.Max_device_sdk != nil {
3386 api, err := android.ApiLevelFromUser(ctx, *module.properties.Max_device_sdk)
3387 if err == nil {
3388 if moduleMinApi.GreaterThan(api) {
3389 ctx.PropertyErrorf("max_device_sdk", "Can't be less than module's min sdk (%s)", moduleMinApi)
3390 }
3391 }
3392 }
3393}
3394
3395func (module *sdkLibraryXml) validateOnBootclasspathBeforeRequirements(ctx android.ModuleContext) {
3396 moduleMinApi := android.ApiLevelOrPanic(ctx, *module.properties.Sdk_library_min_api_level)
3397 if module.properties.On_bootclasspath_before != nil {
3398 t := android.ApiLevelOrPanic(ctx, "Tiramisu")
3399 // if we use the attribute, then we need to do this validation
3400 if moduleMinApi.LessThan(t) {
3401 // if minAPi is < T, then we need to have min_device_sdk (which only accepts T+)
3402 if module.properties.Min_device_sdk == nil {
3403 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")
3404 }
3405 }
3406 }
3407}
3408
Paul Duffindd46f712020-02-10 13:37:10 +00003409type sdkLibrarySdkMemberType struct {
3410 android.SdkMemberTypeBase
3411}
3412
Paul Duffin296701e2021-07-14 10:29:36 +01003413func (s *sdkLibrarySdkMemberType) AddDependencies(ctx android.SdkDependencyContext, dependencyTag blueprint.DependencyTag, names []string) {
3414 ctx.AddVariationDependencies(nil, dependencyTag, names...)
Paul Duffindd46f712020-02-10 13:37:10 +00003415}
3416
3417func (s *sdkLibrarySdkMemberType) IsInstance(module android.Module) bool {
3418 _, ok := module.(*SdkLibrary)
3419 return ok
3420}
3421
3422func (s *sdkLibrarySdkMemberType) AddPrebuiltModule(ctx android.SdkMemberContext, member android.SdkMember) android.BpModule {
3423 return ctx.SnapshotBuilder().AddPrebuiltModule(member, "java_sdk_library_import")
3424}
3425
3426func (s *sdkLibrarySdkMemberType) CreateVariantPropertiesStruct() android.SdkMemberProperties {
3427 return &sdkLibrarySdkMemberProperties{}
3428}
3429
Paul Duffin976b0e52021-04-27 23:20:26 +01003430var javaSdkLibrarySdkMemberType = &sdkLibrarySdkMemberType{
3431 android.SdkMemberTypeBase{
3432 PropertyName: "java_sdk_libs",
3433 SupportsSdk: true,
3434 },
3435}
3436
Paul Duffindd46f712020-02-10 13:37:10 +00003437type sdkLibrarySdkMemberProperties struct {
3438 android.SdkMemberPropertiesBase
3439
Paul Duffine8409952022-09-22 16:24:46 +01003440 // Stem name for files in the sdk snapshot.
3441 //
3442 // This is used to construct the path names of various sdk library files in the sdk snapshot to
3443 // make sure that they match the finalized versions of those files in prebuilts/sdk.
3444 //
3445 // This property is marked as keep so that it will be kept in all instances of this struct, will
3446 // not be cleared but will be copied to common structs. That is needed because this field is used
3447 // to construct many file names for other parts of this struct and so it needs to be present in
3448 // all structs. If it was not marked as keep then it would be cleared in some structs and so would
3449 // be unavailable for generating file names if there were other properties that were still set.
3450 Stem string `sdk:"keep"`
3451
Paul Duffindd46f712020-02-10 13:37:10 +00003452 // Scope to per scope properties.
Paul Duffin106a3a42022-01-27 16:39:06 +00003453 Scopes map[*apiScope]*scopeProperties
Paul Duffindd46f712020-02-10 13:37:10 +00003454
Paul Duffin3d1248c2020-04-09 00:10:17 +01003455 // The Java stubs source files.
3456 Stub_srcs []string
Paul Duffinf7a64332020-05-13 16:54:55 +01003457
3458 // The naming scheme.
3459 Naming_scheme *string
Paul Duffind7eb1c22020-05-26 20:57:10 +01003460
3461 // True if the java_sdk_library_import is for a shared library, false
3462 // otherwise.
3463 Shared_library *bool
Paul Duffina2ae7e02020-09-11 11:55:00 +01003464
Paul Duffin1267d872021-04-16 17:21:36 +01003465 // True if the stub imports should produce dex jars.
3466 Compile_dex *bool
3467
Paul Duffina2ae7e02020-09-11 11:55:00 +01003468 // The paths to the doctag files to add to the prebuilt.
3469 Doctag_paths android.Paths
Paul Duffin869de142021-07-15 14:14:41 +01003470
3471 Permitted_packages []string
Pedro Loureiro9956e5e2021-09-07 17:21:59 +00003472
3473 // Signals that this shared library is part of the bootclasspath starting
3474 // on the version indicated in this attribute.
3475 //
3476 // This will make platforms at this level and above to ignore
3477 // <uses-library> tags with this library name because the library is already
3478 // available
3479 On_bootclasspath_since *string
3480
3481 // Signals that this shared library was part of the bootclasspath before
3482 // (but not including) the version indicated in this attribute.
3483 //
3484 // The system will automatically add a <uses-library> tag with this library to
3485 // apps that target any SDK less than the version indicated in this attribute.
3486 On_bootclasspath_before *string
3487
3488 // Indicates that PackageManager should ignore this shared library if the
3489 // platform is below the version indicated in this attribute.
3490 //
3491 // This means that the device won't recognise this library as installed.
3492 Min_device_sdk *string
3493
3494 // Indicates that PackageManager should ignore this shared library if the
3495 // platform is above the version indicated in this attribute.
3496 //
3497 // This means that the device won't recognise this library as installed.
3498 Max_device_sdk *string
Jiakai Zhang9c4dc192023-02-09 00:09:24 +08003499
3500 DexPreoptProfileGuided *bool `supported_build_releases:"UpsideDownCake+"`
Paul Duffindd46f712020-02-10 13:37:10 +00003501}
3502
3503type scopeProperties struct {
Paul Duffin1fd005d2020-04-09 01:08:11 +01003504 Jars android.Paths
3505 StubsSrcJar android.Path
3506 CurrentApiFile android.Path
3507 RemovedApiFile android.Path
Paul Duffine7babdb2022-02-10 13:06:54 +00003508 AnnotationsZip android.Path `supported_build_releases:"Tiramisu+"`
Paul Duffin1fd005d2020-04-09 01:08:11 +01003509 SdkVersion string
Paul Duffindd46f712020-02-10 13:37:10 +00003510}
3511
3512func (s *sdkLibrarySdkMemberProperties) PopulateFromVariant(ctx android.SdkMemberContext, variant android.Module) {
3513 sdk := variant.(*SdkLibrary)
3514
Paul Duffine8409952022-09-22 16:24:46 +01003515 // Copy the stem name for files in the sdk snapshot.
3516 s.Stem = sdk.distStem()
3517
Paul Duffin106a3a42022-01-27 16:39:06 +00003518 s.Scopes = make(map[*apiScope]*scopeProperties)
Paul Duffindd46f712020-02-10 13:37:10 +00003519 for _, apiScope := range allApiScopes {
Paul Duffin803a9562020-05-20 11:52:25 +01003520 paths := sdk.findScopePaths(apiScope)
3521 if paths == nil {
3522 continue
3523 }
3524
Paul Duffindd46f712020-02-10 13:37:10 +00003525 jars := paths.stubsImplPath
3526 if len(jars) > 0 {
3527 properties := scopeProperties{}
3528 properties.Jars = jars
Paul Duffin780c5f42020-05-12 15:52:55 +01003529 properties.SdkVersion = sdk.sdkVersionForStubsLibrary(ctx.SdkModuleContext(), apiScope)
Paul Duffin0f8faff2020-05-20 16:18:00 +01003530 properties.StubsSrcJar = paths.stubsSrcJar.Path()
Paul Duffin10269f12020-06-19 18:39:55 +01003531 if paths.currentApiFilePath.Valid() {
3532 properties.CurrentApiFile = paths.currentApiFilePath.Path()
3533 }
3534 if paths.removedApiFilePath.Valid() {
3535 properties.RemovedApiFile = paths.removedApiFilePath.Path()
3536 }
Anton Hanssond78eb762021-09-21 15:25:12 +01003537 // The annotations zip is only available for modules that set annotations_enabled: true.
3538 if paths.annotationsZip.Valid() {
3539 properties.AnnotationsZip = paths.annotationsZip.Path()
3540 }
Paul Duffin106a3a42022-01-27 16:39:06 +00003541 s.Scopes[apiScope] = &properties
Paul Duffindd46f712020-02-10 13:37:10 +00003542 }
3543 }
3544
Paul Duffindfa131e2020-05-15 20:37:11 +01003545 s.Naming_scheme = sdk.commonSdkLibraryProperties.Naming_scheme
Paul Duffind7eb1c22020-05-26 20:57:10 +01003546 s.Shared_library = proptools.BoolPtr(sdk.sharedLibrary())
Paul Duffin1267d872021-04-16 17:21:36 +01003547 s.Compile_dex = sdk.dexProperties.Compile_dex
Paul Duffina2ae7e02020-09-11 11:55:00 +01003548 s.Doctag_paths = sdk.doctagPaths
Paul Duffin869de142021-07-15 14:14:41 +01003549 s.Permitted_packages = sdk.PermittedPackagesForUpdatableBootJars()
Pedro Loureiro9956e5e2021-09-07 17:21:59 +00003550 s.On_bootclasspath_since = sdk.commonSdkLibraryProperties.On_bootclasspath_since
3551 s.On_bootclasspath_before = sdk.commonSdkLibraryProperties.On_bootclasspath_before
3552 s.Min_device_sdk = sdk.commonSdkLibraryProperties.Min_device_sdk
3553 s.Max_device_sdk = sdk.commonSdkLibraryProperties.Max_device_sdk
Jiakai Zhang9c4dc192023-02-09 00:09:24 +08003554
3555 if sdk.dexpreopter.dexpreoptProperties.Dex_preopt_result.Profile_guided {
3556 s.DexPreoptProfileGuided = proptools.BoolPtr(true)
3557 }
Paul Duffindd46f712020-02-10 13:37:10 +00003558}
3559
3560func (s *sdkLibrarySdkMemberProperties) AddToPropertySet(ctx android.SdkMemberContext, propertySet android.BpPropertySet) {
Paul Duffinf7a64332020-05-13 16:54:55 +01003561 if s.Naming_scheme != nil {
3562 propertySet.AddProperty("naming_scheme", proptools.String(s.Naming_scheme))
3563 }
Paul Duffind7eb1c22020-05-26 20:57:10 +01003564 if s.Shared_library != nil {
3565 propertySet.AddProperty("shared_library", *s.Shared_library)
3566 }
Paul Duffin1267d872021-04-16 17:21:36 +01003567 if s.Compile_dex != nil {
3568 propertySet.AddProperty("compile_dex", *s.Compile_dex)
3569 }
Paul Duffin869de142021-07-15 14:14:41 +01003570 if len(s.Permitted_packages) > 0 {
3571 propertySet.AddProperty("permitted_packages", s.Permitted_packages)
3572 }
Jiakai Zhang9c4dc192023-02-09 00:09:24 +08003573 dexPreoptSet := propertySet.AddPropertySet("dex_preopt")
3574 if s.DexPreoptProfileGuided != nil {
3575 dexPreoptSet.AddProperty("profile_guided", proptools.Bool(s.DexPreoptProfileGuided))
3576 }
Paul Duffinf7a64332020-05-13 16:54:55 +01003577
Paul Duffine8409952022-09-22 16:24:46 +01003578 stem := s.Stem
3579
Paul Duffindd46f712020-02-10 13:37:10 +00003580 for _, apiScope := range allApiScopes {
3581 if properties, ok := s.Scopes[apiScope]; ok {
Paul Duffin6b836ba2020-05-13 19:19:49 +01003582 scopeSet := propertySet.AddPropertySet(apiScope.propertyName)
Paul Duffindd46f712020-02-10 13:37:10 +00003583
Paul Duffin958806b2022-05-16 13:10:47 +00003584 scopeDir := apiScope.snapshotRelativeDir()
Paul Duffin3d1248c2020-04-09 00:10:17 +01003585
Paul Duffindd46f712020-02-10 13:37:10 +00003586 var jars []string
3587 for _, p := range properties.Jars {
Paul Duffine8409952022-09-22 16:24:46 +01003588 dest := filepath.Join(scopeDir, stem+"-stubs.jar")
Paul Duffindd46f712020-02-10 13:37:10 +00003589 ctx.SnapshotBuilder().CopyToSnapshot(p, dest)
3590 jars = append(jars, dest)
3591 }
3592 scopeSet.AddProperty("jars", jars)
3593
Paul Duffin22628d52021-05-12 23:13:22 +01003594 if ctx.SdkModuleContext().Config().IsEnvTrue("SOONG_SDK_SNAPSHOT_USE_SRCJAR") {
3595 // Copy the stubs source jar into the snapshot zip as is.
Paul Duffine8409952022-09-22 16:24:46 +01003596 srcJarSnapshotPath := filepath.Join(scopeDir, stem+".srcjar")
Paul Duffin22628d52021-05-12 23:13:22 +01003597 ctx.SnapshotBuilder().CopyToSnapshot(properties.StubsSrcJar, srcJarSnapshotPath)
3598 scopeSet.AddProperty("stub_srcs", []string{srcJarSnapshotPath})
3599 } else {
3600 // Merge the stubs source jar into the snapshot zip so that when it is unpacked
3601 // the source files are also unpacked.
Paul Duffine8409952022-09-22 16:24:46 +01003602 snapshotRelativeDir := filepath.Join(scopeDir, stem+"_stub_sources")
Paul Duffin22628d52021-05-12 23:13:22 +01003603 ctx.SnapshotBuilder().UnzipToSnapshot(properties.StubsSrcJar, snapshotRelativeDir)
3604 scopeSet.AddProperty("stub_srcs", []string{snapshotRelativeDir})
3605 }
Paul Duffin3d1248c2020-04-09 00:10:17 +01003606
Paul Duffin1fd005d2020-04-09 01:08:11 +01003607 if properties.CurrentApiFile != nil {
Paul Duffine8409952022-09-22 16:24:46 +01003608 currentApiSnapshotPath := apiScope.snapshotRelativeCurrentApiTxtPath(stem)
Paul Duffin1fd005d2020-04-09 01:08:11 +01003609 ctx.SnapshotBuilder().CopyToSnapshot(properties.CurrentApiFile, currentApiSnapshotPath)
3610 scopeSet.AddProperty("current_api", currentApiSnapshotPath)
3611 }
3612
3613 if properties.RemovedApiFile != nil {
Paul Duffine8409952022-09-22 16:24:46 +01003614 removedApiSnapshotPath := apiScope.snapshotRelativeRemovedApiTxtPath(stem)
Paul Duffin3dbf9fd2020-06-02 13:00:02 +01003615 ctx.SnapshotBuilder().CopyToSnapshot(properties.RemovedApiFile, removedApiSnapshotPath)
Paul Duffin1fd005d2020-04-09 01:08:11 +01003616 scopeSet.AddProperty("removed_api", removedApiSnapshotPath)
3617 }
3618
Anton Hanssond78eb762021-09-21 15:25:12 +01003619 if properties.AnnotationsZip != nil {
Paul Duffine8409952022-09-22 16:24:46 +01003620 annotationsSnapshotPath := filepath.Join(scopeDir, stem+"_annotations.zip")
Anton Hanssond78eb762021-09-21 15:25:12 +01003621 ctx.SnapshotBuilder().CopyToSnapshot(properties.AnnotationsZip, annotationsSnapshotPath)
3622 scopeSet.AddProperty("annotations", annotationsSnapshotPath)
3623 }
3624
Paul Duffindd46f712020-02-10 13:37:10 +00003625 if properties.SdkVersion != "" {
3626 scopeSet.AddProperty("sdk_version", properties.SdkVersion)
3627 }
3628 }
3629 }
3630
Paul Duffina2ae7e02020-09-11 11:55:00 +01003631 if len(s.Doctag_paths) > 0 {
3632 dests := []string{}
3633 for _, p := range s.Doctag_paths {
3634 dest := filepath.Join("doctags", p.Rel())
3635 ctx.SnapshotBuilder().CopyToSnapshot(p, dest)
3636 dests = append(dests, dest)
3637 }
3638 propertySet.AddProperty("doctag_files", dests)
3639 }
Paul Duffindd46f712020-02-10 13:37:10 +00003640}