blob: 058862b432996d3dca721350c6ca00e99cca84cc [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 (
Jiyong Parkc678ad32018-04-10 13:07:10 +090018 "fmt"
19 "path"
Sundong Ahn054b19a2018-10-19 13:46:09 +090020 "path/filepath"
Paul Duffin46a26a82020-04-07 19:27:04 +010021 "reflect"
Paul Duffin46dc45a2020-05-14 15:39:10 +010022 "regexp"
Jiyong Park82484c02018-04-23 21:41:26 +090023 "sort"
Jiyong Parkc678ad32018-04-10 13:07:10 +090024 "strings"
Jiyong Park82484c02018-04-23 21:41:26 +090025 "sync"
Jiyong Parkc678ad32018-04-10 13:07:10 +090026
Paul Duffind1b3a922020-01-22 11:57:20 +000027 "github.com/google/blueprint"
Jiyong Parkc678ad32018-04-10 13:07:10 +090028 "github.com/google/blueprint/proptools"
Paul Duffin46a26a82020-04-07 19:27:04 +010029
30 "android/soong/android"
Ulya Trafimovichdbf31662020-12-17 12:07:54 +000031 "android/soong/dexpreopt"
Jiyong Parkc678ad32018-04-10 13:07:10 +090032)
33
Jooyung Han58f26ab2019-12-18 15:34:32 +090034const (
Pedro Loureiro9956e5e2021-09-07 17:21:59 +000035 sdkXmlFileSuffix = ".xml"
Jiyong Parkc678ad32018-04-10 13:07:10 +090036)
37
Paul Duffind1b3a922020-01-22 11:57:20 +000038// A tag to associated a dependency with a specific api scope.
39type scopeDependencyTag struct {
40 blueprint.BaseDependencyTag
41 name string
42 apiScope *apiScope
Paul Duffinc8782502020-04-29 20:45:27 +010043
44 // Function for extracting appropriate path information from the dependency.
Colin Crossdcf71b22021-02-01 13:59:03 -080045 depInfoExtractor func(paths *scopePaths, ctx android.ModuleContext, dep android.Module) error
Paul Duffinc8782502020-04-29 20:45:27 +010046}
47
48// Extract tag specific information from the dependency.
49func (tag scopeDependencyTag) extractDepInfo(ctx android.ModuleContext, dep android.Module, paths *scopePaths) {
Colin Crossdcf71b22021-02-01 13:59:03 -080050 err := tag.depInfoExtractor(paths, ctx, dep)
Paul Duffinc8782502020-04-29 20:45:27 +010051 if err != nil {
52 ctx.ModuleErrorf("has an invalid {scopeDependencyTag: %s} dependency on module %s: %s", tag.name, ctx.OtherModuleName(dep), err.Error())
53 }
Paul Duffind1b3a922020-01-22 11:57:20 +000054}
55
Paul Duffin80342d72020-06-26 22:08:43 +010056var _ android.ReplaceSourceWithPrebuilt = (*scopeDependencyTag)(nil)
57
58func (tag scopeDependencyTag) ReplaceSourceWithPrebuilt() bool {
59 return false
60}
61
Paul Duffind1b3a922020-01-22 11:57:20 +000062// Provides information about an api scope, e.g. public, system, test.
63type apiScope struct {
64 // The name of the api scope, e.g. public, system, test
65 name string
66
Paul Duffin97b53b82020-05-05 14:40:52 +010067 // The api scope that this scope extends.
Paul Duffind0b9fca2022-09-30 18:11:41 +010068 //
69 // This organizes the scopes into an extension hierarchy.
70 //
71 // If set this means that the API provided by this scope includes the API provided by the scope
72 // set in this field.
Paul Duffin97b53b82020-05-05 14:40:52 +010073 extends *apiScope
74
Paul Duffind0b9fca2022-09-30 18:11:41 +010075 // The next api scope that a library that uses this scope can access.
76 //
77 // This organizes the scopes into an access hierarchy.
78 //
79 // If set this means that a library that can access this API can also access the API provided by
80 // the scope set in this field.
81 //
82 // A module that sets sdk_version: "<scope>_current" should have access to the <scope> API of
83 // every java_sdk_library that it depends on. If the library does not provide an API for <scope>
84 // then it will traverse up this access hierarchy to find an API that it does provide.
85 //
86 // If this is not set then it defaults to the scope set in extends.
87 canAccess *apiScope
88
Paul Duffin3375e352020-04-28 10:44:03 +010089 // The legacy enabled status for a specific scope can be dependent on other
90 // properties that have been specified on the library so it is provided by
91 // a function that can determine the status by examining those properties.
92 legacyEnabledStatus func(module *SdkLibrary) bool
93
94 // The default enabled status for non-legacy behavior, which is triggered by
95 // explicitly enabling at least one api scope.
96 defaultEnabledStatus bool
97
98 // Gets a pointer to the scope specific properties.
99 scopeSpecificProperties func(module *SdkLibrary) *ApiScopeProperties
100
Paul Duffin46a26a82020-04-07 19:27:04 +0100101 // The name of the field in the dynamically created structure.
102 fieldName string
103
Paul Duffin6b836ba2020-05-13 19:19:49 +0100104 // The name of the property in the java_sdk_library_import
105 propertyName string
106
Paul Duffind1b3a922020-01-22 11:57:20 +0000107 // The tag to use to depend on the stubs library module.
108 stubsTag scopeDependencyTag
109
Paul Duffin0ff08bd2020-04-29 13:30:54 +0100110 // The tag to use to depend on the stubs source module (if separate from the API module).
111 stubsSourceTag scopeDependencyTag
112
113 // The tag to use to depend on the API file generating module (if separate from the stubs source module).
114 apiFileTag scopeDependencyTag
115
Paul Duffinc8782502020-04-29 20:45:27 +0100116 // The tag to use to depend on the stubs source and API module.
117 stubsSourceAndApiTag scopeDependencyTag
Paul Duffind1b3a922020-01-22 11:57:20 +0000118
Paul Duffin958806b2022-05-16 13:10:47 +0000119 // The tag to use to depend on the module that provides the latest version of the API .txt file.
120 latestApiModuleTag scopeDependencyTag
121
122 // The tag to use to depend on the module that provides the latest version of the API removed.txt
123 // file.
124 latestRemovedApiModuleTag scopeDependencyTag
125
Paul Duffind1b3a922020-01-22 11:57:20 +0000126 // The scope specific prefix to add to the api file base of "current.txt" or "removed.txt".
127 apiFilePrefix string
128
Paul Duffind0b9fca2022-09-30 18:11:41 +0100129 // The scope specific suffix to add to the sdk library module name to construct a scope specific
Paul Duffind1b3a922020-01-22 11:57:20 +0000130 // module name.
131 moduleSuffix string
132
Paul Duffind1b3a922020-01-22 11:57:20 +0000133 // SDK version that the stubs library is built against. Note that this is always
134 // *current. Older stubs library built with a numbered SDK version is created from
135 // the prebuilt jar.
136 sdkVersion string
Paul Duffin1fb487d2020-04-07 18:50:10 +0100137
Paul Duffin15f34ef2020-07-20 18:04:44 +0100138 // The annotation that identifies this API level, empty for the public API scope.
139 annotation string
140
Paul Duffin1fb487d2020-04-07 18:50:10 +0100141 // Extra arguments to pass to droidstubs for this scope.
Paul Duffin0ff08bd2020-04-29 13:30:54 +0100142 //
Paul Duffin15f34ef2020-07-20 18:04:44 +0100143 // This is not used directly but is used to construct the droidstubsArgs.
144 extraArgs []string
Paul Duffin0ff08bd2020-04-29 13:30:54 +0100145
Paul Duffin15f34ef2020-07-20 18:04:44 +0100146 // The args that must be passed to droidstubs to generate the API and stubs source
147 // for this scope, constructed dynamically by initApiScope().
Paul Duffin0ff08bd2020-04-29 13:30:54 +0100148 //
149 // The API only includes the additional members that this scope adds over the scope
150 // that it extends.
Paul Duffin15f34ef2020-07-20 18:04:44 +0100151 //
152 // The stubs source must include the definitions of everything that is in this
153 // api scope and all the scopes that this one extends.
154 droidstubsArgs []string
Paul Duffin0ff08bd2020-04-29 13:30:54 +0100155
Anton Hansson6478ac12020-05-02 11:19:36 +0100156 // Whether the api scope can be treated as unstable, and should skip compat checks.
157 unstable bool
Jihoon Kang1c92c3e2023-03-23 17:44:51 +0000158
159 // Represents the SDK kind of this scope.
160 kind android.SdkKind
Paul Duffind1b3a922020-01-22 11:57:20 +0000161}
162
163// Initialize a scope, creating and adding appropriate dependency tags
164func initApiScope(scope *apiScope) *apiScope {
Paul Duffinc8782502020-04-29 20:45:27 +0100165 name := scope.name
Paul Duffin46dc45a2020-05-14 15:39:10 +0100166 scopeByName[name] = scope
167 allScopeNames = append(allScopeNames, name)
Paul Duffin6b836ba2020-05-13 19:19:49 +0100168 scope.propertyName = strings.ReplaceAll(name, "-", "_")
169 scope.fieldName = proptools.FieldNameForProperty(scope.propertyName)
Paul Duffind1b3a922020-01-22 11:57:20 +0000170 scope.stubsTag = scopeDependencyTag{
Paul Duffinc8782502020-04-29 20:45:27 +0100171 name: name + "-stubs",
172 apiScope: scope,
173 depInfoExtractor: (*scopePaths).extractStubsLibraryInfoFromDependency,
Paul Duffind1b3a922020-01-22 11:57:20 +0000174 }
Paul Duffin0ff08bd2020-04-29 13:30:54 +0100175 scope.stubsSourceTag = scopeDependencyTag{
176 name: name + "-stubs-source",
177 apiScope: scope,
178 depInfoExtractor: (*scopePaths).extractStubsSourceInfoFromDep,
179 }
180 scope.apiFileTag = scopeDependencyTag{
181 name: name + "-api",
182 apiScope: scope,
183 depInfoExtractor: (*scopePaths).extractApiInfoFromDep,
184 }
Paul Duffinc8782502020-04-29 20:45:27 +0100185 scope.stubsSourceAndApiTag = scopeDependencyTag{
186 name: name + "-stubs-source-and-api",
187 apiScope: scope,
188 depInfoExtractor: (*scopePaths).extractStubsSourceAndApiInfoFromApiStubsProvider,
Paul Duffind1b3a922020-01-22 11:57:20 +0000189 }
Paul Duffin958806b2022-05-16 13:10:47 +0000190 scope.latestApiModuleTag = scopeDependencyTag{
191 name: name + "-latest-api",
192 apiScope: scope,
193 depInfoExtractor: (*scopePaths).extractLatestApiPath,
194 }
195 scope.latestRemovedApiModuleTag = scopeDependencyTag{
196 name: name + "-latest-removed-api",
197 apiScope: scope,
198 depInfoExtractor: (*scopePaths).extractLatestRemovedApiPath,
199 }
Paul Duffin0ff08bd2020-04-29 13:30:54 +0100200
201 // To get the args needed to generate the stubs source append all the args from
202 // this scope and all the scopes it extends as each set of args adds additional
203 // members to the stubs.
Paul Duffin15f34ef2020-07-20 18:04:44 +0100204 var scopeSpecificArgs []string
205 if scope.annotation != "" {
206 scopeSpecificArgs = []string{"--show-annotation", scope.annotation}
Paul Duffin0ff08bd2020-04-29 13:30:54 +0100207 }
Paul Duffin15f34ef2020-07-20 18:04:44 +0100208 for s := scope; s != nil; s = s.extends {
209 scopeSpecificArgs = append(scopeSpecificArgs, s.extraArgs...)
Paul Duffin0ff08bd2020-04-29 13:30:54 +0100210
Paul Duffin15f34ef2020-07-20 18:04:44 +0100211 // Ensure that the generated stubs includes all the API elements from the API scope
212 // that this scope extends.
213 if s != scope && s.annotation != "" {
214 scopeSpecificArgs = append(scopeSpecificArgs, "--show-for-stub-purposes-annotation", s.annotation)
215 }
216 }
Paul Duffin0ff08bd2020-04-29 13:30:54 +0100217
Paul Duffind0b9fca2022-09-30 18:11:41 +0100218 // By default, a library that can access a scope can also access the scope it extends.
219 if scope.canAccess == nil {
220 scope.canAccess = scope.extends
221 }
222
Paul Duffin15f34ef2020-07-20 18:04:44 +0100223 // Escape any special characters in the arguments. This is needed because droidstubs
224 // passes these directly to the shell command.
225 scope.droidstubsArgs = proptools.ShellEscapeList(scopeSpecificArgs)
Paul Duffin0ff08bd2020-04-29 13:30:54 +0100226
Paul Duffind1b3a922020-01-22 11:57:20 +0000227 return scope
228}
229
Anton Hansson08f476b2021-04-07 15:32:19 +0100230func (scope *apiScope) stubsLibraryModuleNameSuffix() string {
231 return ".stubs" + scope.moduleSuffix
232}
233
Jihoon Kangfa4a90d2023-12-20 02:53:38 +0000234func (scope *apiScope) exportableStubsLibraryModuleNameSuffix() string {
235 return ".stubs.exportable" + scope.moduleSuffix
236}
237
Jihoon Kang1c92c3e2023-03-23 17:44:51 +0000238func (scope *apiScope) apiLibraryModuleName(baseName string) string {
239 return scope.stubsLibraryModuleName(baseName) + ".from-text"
240}
241
Jihoon Kang1147b312023-06-08 23:25:57 +0000242func (scope *apiScope) sourceStubLibraryModuleName(baseName string) string {
243 return scope.stubsLibraryModuleName(baseName) + ".from-source"
244}
245
Jihoon Kangfa4a90d2023-12-20 02:53:38 +0000246func (scope *apiScope) exportableSourceStubsLibraryModuleName(baseName string) string {
247 return scope.exportableStubsLibraryModuleName(baseName) + ".from-source"
248}
249
Paul Duffinc3091c82020-05-08 14:16:20 +0100250func (scope *apiScope) stubsLibraryModuleName(baseName string) string {
Anton Hansson08f476b2021-04-07 15:32:19 +0100251 return baseName + scope.stubsLibraryModuleNameSuffix()
Paul Duffind1b3a922020-01-22 11:57:20 +0000252}
253
Jihoon Kangfa4a90d2023-12-20 02:53:38 +0000254func (scope *apiScope) exportableStubsLibraryModuleName(baseName string) string {
255 return baseName + scope.exportableStubsLibraryModuleNameSuffix()
256}
257
Paul Duffinc8782502020-04-29 20:45:27 +0100258func (scope *apiScope) stubsSourceModuleName(baseName string) string {
Paul Duffindd9d0742020-05-08 15:52:37 +0100259 return baseName + ".stubs.source" + scope.moduleSuffix
Paul Duffind1b3a922020-01-22 11:57:20 +0000260}
261
Paul Duffin0ff08bd2020-04-29 13:30:54 +0100262func (scope *apiScope) apiModuleName(baseName string) string {
Paul Duffindd9d0742020-05-08 15:52:37 +0100263 return baseName + ".api" + scope.moduleSuffix
Paul Duffin0ff08bd2020-04-29 13:30:54 +0100264}
265
Paul Duffin3375e352020-04-28 10:44:03 +0100266func (scope *apiScope) String() string {
267 return scope.name
268}
269
Paul Duffin958806b2022-05-16 13:10:47 +0000270// snapshotRelativeDir returns the snapshot directory into which the files related to scopes will
271// be stored.
272func (scope *apiScope) snapshotRelativeDir() string {
273 return filepath.Join("sdk_library", scope.name)
274}
275
276// snapshotRelativeCurrentApiTxtPath returns the snapshot path to the API .txt file for the named
277// library.
278func (scope *apiScope) snapshotRelativeCurrentApiTxtPath(name string) string {
279 return filepath.Join(scope.snapshotRelativeDir(), name+".txt")
280}
281
282// snapshotRelativeRemovedApiTxtPath returns the snapshot path to the removed API .txt file for the
283// named library.
284func (scope *apiScope) snapshotRelativeRemovedApiTxtPath(name string) string {
285 return filepath.Join(scope.snapshotRelativeDir(), name+"-removed.txt")
286}
287
Paul Duffind1b3a922020-01-22 11:57:20 +0000288type apiScopes []*apiScope
289
290func (scopes apiScopes) Strings(accessor func(*apiScope) string) []string {
291 var list []string
292 for _, scope := range scopes {
293 list = append(list, accessor(scope))
294 }
295 return list
296}
297
Jihoon Kanga96a7b12023-09-20 23:43:32 +0000298// Method that maps the apiScopes properties to the index of each apiScopes elements.
299// apiScopes property to be used as the key can be specified with the input accessor.
300// Only a string property of apiScope can be used as the key of the map.
301func (scopes apiScopes) MapToIndex(accessor func(*apiScope) string) map[string]int {
302 ret := make(map[string]int)
303 for i, scope := range scopes {
304 ret[accessor(scope)] = i
305 }
306 return ret
307}
308
Jiyong Parkc678ad32018-04-10 13:07:10 +0900309var (
Paul Duffin46dc45a2020-05-14 15:39:10 +0100310 scopeByName = make(map[string]*apiScope)
311 allScopeNames []string
Paul Duffind1b3a922020-01-22 11:57:20 +0000312 apiScopePublic = initApiScope(&apiScope{
Paul Duffin3375e352020-04-28 10:44:03 +0100313 name: "public",
314
315 // Public scope is enabled by default for both legacy and non-legacy modes.
316 legacyEnabledStatus: func(module *SdkLibrary) bool {
317 return true
318 },
319 defaultEnabledStatus: true,
320
321 scopeSpecificProperties: func(module *SdkLibrary) *ApiScopeProperties {
322 return &module.sdkLibraryProperties.Public
323 },
Paul Duffind1b3a922020-01-22 11:57:20 +0000324 sdkVersion: "current",
Jihoon Kang1c92c3e2023-03-23 17:44:51 +0000325 kind: android.SdkPublic,
Paul Duffind1b3a922020-01-22 11:57:20 +0000326 })
327 apiScopeSystem = initApiScope(&apiScope{
Paul Duffin3375e352020-04-28 10:44:03 +0100328 name: "system",
329 extends: apiScopePublic,
330 legacyEnabledStatus: (*SdkLibrary).generateTestAndSystemScopesByDefault,
331 scopeSpecificProperties: func(module *SdkLibrary) *ApiScopeProperties {
332 return &module.sdkLibraryProperties.System
333 },
Paul Duffin15f34ef2020-07-20 18:04:44 +0100334 apiFilePrefix: "system-",
335 moduleSuffix: ".system",
336 sdkVersion: "system_current",
337 annotation: "android.annotation.SystemApi(client=android.annotation.SystemApi.Client.PRIVILEGED_APPS)",
Jihoon Kang1c92c3e2023-03-23 17:44:51 +0000338 kind: android.SdkSystem,
Paul Duffind1b3a922020-01-22 11:57:20 +0000339 })
340 apiScopeTest = initApiScope(&apiScope{
Paul Duffin3375e352020-04-28 10:44:03 +0100341 name: "test",
Anton Hansson4fe970f2020-10-09 10:16:49 +0100342 extends: apiScopeSystem,
Paul Duffin3375e352020-04-28 10:44:03 +0100343 legacyEnabledStatus: (*SdkLibrary).generateTestAndSystemScopesByDefault,
344 scopeSpecificProperties: func(module *SdkLibrary) *ApiScopeProperties {
345 return &module.sdkLibraryProperties.Test
346 },
Paul Duffin15f34ef2020-07-20 18:04:44 +0100347 apiFilePrefix: "test-",
348 moduleSuffix: ".test",
349 sdkVersion: "test_current",
350 annotation: "android.annotation.TestApi",
351 unstable: true,
Jihoon Kang1c92c3e2023-03-23 17:44:51 +0000352 kind: android.SdkTest,
Paul Duffind1b3a922020-01-22 11:57:20 +0000353 })
Paul Duffin8f265b92020-04-28 14:13:56 +0100354 apiScopeModuleLib = initApiScope(&apiScope{
Paul Duffin6b836ba2020-05-13 19:19:49 +0100355 name: "module-lib",
Paul Duffin8f265b92020-04-28 14:13:56 +0100356 extends: apiScopeSystem,
Paul Duffin0c5bae52020-06-02 13:00:08 +0100357 // The module-lib scope is disabled by default in legacy mode.
Paul Duffin8f265b92020-04-28 14:13:56 +0100358 //
359 // Enabling this would break existing usages.
360 legacyEnabledStatus: func(module *SdkLibrary) bool {
361 return false
362 },
363 scopeSpecificProperties: func(module *SdkLibrary) *ApiScopeProperties {
364 return &module.sdkLibraryProperties.Module_lib
365 },
366 apiFilePrefix: "module-lib-",
367 moduleSuffix: ".module_lib",
368 sdkVersion: "module_current",
Paul Duffin15f34ef2020-07-20 18:04:44 +0100369 annotation: "android.annotation.SystemApi(client=android.annotation.SystemApi.Client.MODULE_LIBRARIES)",
Jihoon Kang1c92c3e2023-03-23 17:44:51 +0000370 kind: android.SdkModule,
Paul Duffin8f265b92020-04-28 14:13:56 +0100371 })
Paul Duffin0c5bae52020-06-02 13:00:08 +0100372 apiScopeSystemServer = initApiScope(&apiScope{
373 name: "system-server",
374 extends: apiScopePublic,
Paul Duffind0b9fca2022-09-30 18:11:41 +0100375
376 // The system-server scope can access the module-lib scope.
377 //
378 // A module that provides a system-server API is appended to the standard bootclasspath that is
379 // used by the system server. So, it should be able to access module-lib APIs provided by
380 // libraries on the bootclasspath.
381 canAccess: apiScopeModuleLib,
382
Paul Duffin0c5bae52020-06-02 13:00:08 +0100383 // The system-server scope is disabled by default in legacy mode.
384 //
385 // Enabling this would break existing usages.
386 legacyEnabledStatus: func(module *SdkLibrary) bool {
387 return false
388 },
389 scopeSpecificProperties: func(module *SdkLibrary) *ApiScopeProperties {
390 return &module.sdkLibraryProperties.System_server
391 },
392 apiFilePrefix: "system-server-",
393 moduleSuffix: ".system_server",
394 sdkVersion: "system_server_current",
Paul Duffin15f34ef2020-07-20 18:04:44 +0100395 annotation: "android.annotation.SystemApi(client=android.annotation.SystemApi.Client.SYSTEM_SERVER)",
396 extraArgs: []string{
397 "--hide-annotation", "android.annotation.Hide",
Paul Duffin0c5bae52020-06-02 13:00:08 +0100398 // com.android.* classes are okay in this interface"
Paul Duffin15f34ef2020-07-20 18:04:44 +0100399 "--hide", "InternalClasses",
Paul Duffin0c5bae52020-06-02 13:00:08 +0100400 },
Jihoon Kang1c92c3e2023-03-23 17:44:51 +0000401 kind: android.SdkSystemServer,
Paul Duffin0c5bae52020-06-02 13:00:08 +0100402 })
Paul Duffind1b3a922020-01-22 11:57:20 +0000403 allApiScopes = apiScopes{
404 apiScopePublic,
405 apiScopeSystem,
406 apiScopeTest,
Paul Duffin8f265b92020-04-28 14:13:56 +0100407 apiScopeModuleLib,
Paul Duffin0c5bae52020-06-02 13:00:08 +0100408 apiScopeSystemServer,
Paul Duffind1b3a922020-01-22 11:57:20 +0000409 }
Jihoon Kang0c705a42023-08-02 06:44:57 +0000410 apiLibraryAdditionalProperties = map[string]struct {
411 FullApiSurfaceStubLib string
412 AdditionalApiContribution string
413 }{
414 "legacy.i18n.module.platform.api": {
415 FullApiSurfaceStubLib: "legacy.core.platform.api.stubs",
416 AdditionalApiContribution: "i18n.module.public.api.stubs.source.api.contribution",
417 },
418 "stable.i18n.module.platform.api": {
419 FullApiSurfaceStubLib: "stable.core.platform.api.stubs",
420 AdditionalApiContribution: "i18n.module.public.api.stubs.source.api.contribution",
421 },
422 "conscrypt.module.platform.api": {
423 FullApiSurfaceStubLib: "stable.core.platform.api.stubs",
424 AdditionalApiContribution: "conscrypt.module.public.api.stubs.source.api.contribution",
425 },
426 }
Jiyong Parkc678ad32018-04-10 13:07:10 +0900427)
428
Jiyong Park82484c02018-04-23 21:41:26 +0900429var (
430 javaSdkLibrariesLock sync.Mutex
431)
432
Jiyong Parkc678ad32018-04-10 13:07:10 +0900433// TODO: these are big features that are currently missing
Jiyong Park1be96912018-05-28 18:02:19 +0900434// 1) disallowing linking to the runtime shared lib
435// 2) HTML generation
Jiyong Parkc678ad32018-04-10 13:07:10 +0900436
437func init() {
Paul Duffin43dc1cc2019-12-19 11:18:54 +0000438 RegisterSdkLibraryBuildComponents(android.InitRegistrationContext)
Jiyong Parkc678ad32018-04-10 13:07:10 +0900439
Jiyong Park82484c02018-04-23 21:41:26 +0900440 android.RegisterMakeVarsProvider(pctx, func(ctx android.MakeVarsContext) {
441 javaSdkLibraries := javaSdkLibraries(ctx.Config())
442 sort.Strings(*javaSdkLibraries)
443 ctx.Strict("JAVA_SDK_LIBRARIES", strings.Join(*javaSdkLibraries, " "))
444 })
Paul Duffindd46f712020-02-10 13:37:10 +0000445
446 // Register sdk member types.
Paul Duffin976b0e52021-04-27 23:20:26 +0100447 android.RegisterSdkMemberType(javaSdkLibrarySdkMemberType)
Jiyong Parkc678ad32018-04-10 13:07:10 +0900448}
449
Paul Duffin43dc1cc2019-12-19 11:18:54 +0000450func RegisterSdkLibraryBuildComponents(ctx android.RegistrationContext) {
451 ctx.RegisterModuleType("java_sdk_library", SdkLibraryFactory)
452 ctx.RegisterModuleType("java_sdk_library_import", sdkLibraryImportFactory)
453}
454
Paul Duffin3375e352020-04-28 10:44:03 +0100455// Properties associated with each api scope.
456type ApiScopeProperties struct {
457 // Indicates whether the api surface is generated.
458 //
459 // If this is set for any scope then all scopes must explicitly specify if they
460 // are enabled. This is to prevent new usages from depending on legacy behavior.
461 //
462 // Otherwise, if this is not set for any scope then the default behavior is
463 // scope specific so please refer to the scope specific property documentation.
464 Enabled *bool
Paul Duffin87a05a32020-05-12 11:50:28 +0100465
466 // The sdk_version to use for building the stubs.
467 //
468 // If not specified then it will use an sdk_version determined as follows:
Trevor Radcliffedf8aa1f2021-11-04 14:25:39 +0000469 //
Paul Duffin87a05a32020-05-12 11:50:28 +0100470 // 1) If the sdk_version specified on the java_sdk_library is none then this
Trevor Radcliffedf8aa1f2021-11-04 14:25:39 +0000471 // will be none. This is used for java_sdk_library instances that are used
472 // to create stubs that contribute to the core_current sdk version.
473 // 2) Otherwise, it is assumed that this library extends but does not
474 // contribute directly to a specific sdk_version and so this uses the
475 // sdk_version appropriate for the api scope. e.g. public will use
476 // sdk_version: current, system will use sdk_version: system_current, etc.
Paul Duffin87a05a32020-05-12 11:50:28 +0100477 //
478 // This does not affect the sdk_version used for either generating the stubs source
479 // or the API file. They both have to use the same sdk_version as is used for
480 // compiling the implementation library.
481 Sdk_version *string
Mark White9421c4c2023-08-10 00:07:03 +0000482
483 // Extra libs used when compiling stubs for this scope.
484 Libs []string
Paul Duffin3375e352020-04-28 10:44:03 +0100485}
486
Jiyong Parkc678ad32018-04-10 13:07:10 +0900487type sdkLibraryProperties struct {
Anton Hanssonf8ea3722021-09-16 14:24:13 +0100488 // List of source files that are needed to compile the API, but are not part of runtime library.
489 Api_srcs []string `android:"arch_variant"`
490
Paul Duffin5df79302020-05-16 15:52:12 +0100491 // Visibility for impl library module. If not specified then defaults to the
492 // visibility property.
493 Impl_library_visibility []string
494
Paul Duffin4911a892020-04-29 23:35:13 +0100495 // Visibility for stubs library modules. If not specified then defaults to the
496 // visibility property.
497 Stubs_library_visibility []string
498
499 // Visibility for stubs source modules. If not specified then defaults to the
500 // visibility property.
501 Stubs_source_visibility []string
502
Anton Hansson7f66efa2020-10-08 14:47:23 +0100503 // List of Java libraries that will be in the classpath when building the implementation lib
504 Impl_only_libs []string `android:"arch_variant"`
505
Paul Duffin77590a82022-04-28 14:13:30 +0000506 // List of Java libraries that will included in the implementation lib.
507 Impl_only_static_libs []string `android:"arch_variant"`
508
Sundong Ahnf043cf62018-06-25 16:04:37 +0900509 // List of Java libraries that will be in the classpath when building stubs
510 Stub_only_libs []string `android:"arch_variant"`
511
Anton Hanssondae54cd2021-04-21 16:30:10 +0100512 // List of Java libraries that will included in stub libraries
513 Stub_only_static_libs []string `android:"arch_variant"`
514
Paul Duffin7a586d32019-12-30 17:09:34 +0000515 // list of package names that will be documented and publicized as API.
516 // This allows the API to be restricted to a subset of the source files provided.
517 // If this is unspecified then all the source files will be treated as being part
518 // of the API.
Jiyong Parkc678ad32018-04-10 13:07:10 +0900519 Api_packages []string
520
Jiyong Park5a2c9d72018-05-01 22:25:41 +0900521 // list of package names that must be hidden from the API
522 Hidden_api_packages []string
523
Paul Duffin749f98f2019-12-30 17:23:46 +0000524 // the relative path to the directory containing the api specification files.
525 // Defaults to "api".
526 Api_dir *string
527
Paul Duffindfa131e2020-05-15 20:37:11 +0100528 // Determines whether a runtime implementation library is built; defaults to false.
529 //
530 // 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 +0200531 // it is as if shared_library: false, was set.
Paul Duffin43db9be2019-12-30 17:35:49 +0000532 Api_only *bool
533
Paul Duffin11512472019-02-11 15:55:17 +0000534 // local files that are used within user customized droiddoc options.
535 Droiddoc_option_files []string
536
Spandan Das93e95992021-07-29 18:26:39 +0000537 // additional droiddoc options.
Paul Duffin11512472019-02-11 15:55:17 +0000538 // Available variables for substitution:
539 //
540 // $(location <label>): the path to the droiddoc_option_files with name <label>
Sundong Ahndd567f92018-07-31 17:19:11 +0900541 Droiddoc_options []string
542
Paul Duffine22c2ab2020-05-20 19:35:27 +0100543 // is set to true, Metalava will allow framework SDK to contain annotations.
544 Annotations_enabled *bool
545
Sundong Ahn054b19a2018-10-19 13:46:09 +0900546 // a list of top-level directories containing files to merge qualifier annotations
547 // (i.e. those intended to be included in the stubs written) from.
548 Merge_annotations_dirs []string
549
550 // a list of top-level directories containing Java stub files to merge show/hide annotations from.
551 Merge_inclusion_annotations_dirs []string
552
Paul Duffin4f5c1ef2020-11-19 14:53:43 +0000553 // If set to true then don't create dist rules.
554 No_dist *bool
Sundong Ahn80a87b32019-05-13 15:02:50 +0900555
Paul Duffin31310252020-11-20 21:26:20 +0000556 // The stem for the artifacts that are copied to the dist, if not specified
557 // then defaults to the base module name.
558 //
559 // For each scope the following artifacts are copied to the apistubs/<scope>
560 // directory in the dist.
561 // * stubs impl jar -> <dist-stem>.jar
562 // * API specification file -> api/<dist-stem>.txt
563 // * Removed API specification file -> api/<dist-stem>-removed.txt
564 //
565 // Also used to construct the name of the filegroup (created by prebuilt_apis)
566 // that references the latest released API and remove API specification files.
567 // * API specification filegroup -> <dist-stem>.api.<scope>.latest
568 // * Removed API specification filegroup -> <dist-stem>-removed.api.<scope>.latest
Jaewoong Jung1a97ee02021-03-09 13:25:02 -0800569 // * API incompatibilities baseline filegroup -> <dist-stem>-incompatibilities.api.<scope>.latest
Paul Duffin31310252020-11-20 21:26:20 +0000570 Dist_stem *string
571
Colin Cross986b69a2021-06-01 13:13:40 -0700572 // The subdirectory for the artifacts that are copied to the dist directory. If not specified
Colin Cross3dd66252021-06-01 14:05:09 -0700573 // then defaults to "unknown". Should be set to "android" for anything that should be published
Colin Cross986b69a2021-06-01 13:13:40 -0700574 // in the public Android SDK.
575 Dist_group *string
576
Anton Hanssondff2c782020-12-21 17:10:01 +0000577 // A compatibility mode that allows historical API-tracking files to not exist.
578 // Do not use.
579 Unsafe_ignore_missing_latest_api bool
580
Paul Duffin3375e352020-04-28 10:44:03 +0100581 // indicates whether system and test apis should be generated.
582 Generate_system_and_test_apis bool `blueprint:"mutated"`
583
584 // The properties specific to the public api scope
585 //
586 // Unless explicitly specified by using public.enabled the public api scope is
587 // enabled by default in both legacy and non-legacy mode.
588 Public ApiScopeProperties
589
590 // The properties specific to the system api scope
591 //
592 // In legacy mode the system api scope is enabled by default when sdk_version
593 // is set to something other than "none".
594 //
595 // In non-legacy mode the system api scope is disabled by default.
596 System ApiScopeProperties
597
598 // The properties specific to the test api scope
599 //
600 // In legacy mode the test api scope is enabled by default when sdk_version
601 // is set to something other than "none".
602 //
603 // In non-legacy mode the test api scope is disabled by default.
604 Test ApiScopeProperties
Paul Duffin37e0b772019-12-30 17:20:10 +0000605
Paul Duffin0c5bae52020-06-02 13:00:08 +0100606 // The properties specific to the module-lib api scope
Paul Duffin8f265b92020-04-28 14:13:56 +0100607 //
Zi Wangb2179e32023-01-31 15:53:30 -0800608 // Unless explicitly specified by using module_lib.enabled the module_lib api
609 // scope is disabled by default.
Paul Duffin8f265b92020-04-28 14:13:56 +0100610 Module_lib ApiScopeProperties
611
Paul Duffin0c5bae52020-06-02 13:00:08 +0100612 // The properties specific to the system-server api scope
613 //
Zi Wangb2179e32023-01-31 15:53:30 -0800614 // Unless explicitly specified by using system_server.enabled the
615 // system_server api scope is disabled by default.
Paul Duffin0c5bae52020-06-02 13:00:08 +0100616 System_server ApiScopeProperties
617
Jiyong Park932cdfe2020-05-28 00:19:53 +0900618 // Determines if the stubs are preferred over the implementation library
619 // for linking, even when the client doesn't specify sdk_version. When this
620 // is set to true, such clients are provided with the widest API surface that
621 // this lib provides. Note however that this option doesn't affect the clients
622 // that are in the same APEX as this library. In that case, the clients are
623 // always linked with the implementation library. Default is false.
624 Default_to_stubs *bool
625
Paul Duffin160fe412020-05-10 19:32:20 +0100626 // Properties related to api linting.
627 Api_lint struct {
628 // Enable api linting.
629 Enabled *bool
Anton Hanssonfd1c0d22023-11-02 15:18:09 +0000630
631 // If API lint is enabled, this flag controls whether a set of legitimate lint errors
632 // are turned off. The default is true.
633 Legacy_errors_allowed *bool
Paul Duffin160fe412020-05-10 19:32:20 +0100634 }
635
Jihoon Kang80456fd2023-11-15 19:22:14 +0000636 // Determines if the module contributes to any api surfaces.
637 // This property should be set to true only if the module is listed under
638 // frameworks-base-api.bootclasspath in frameworks/base/api/Android.bp.
639 // Otherwise, this property should be set to false.
640 // Defaults to false.
641 Contribute_to_android_api *bool
642
Jihoon Kang6592e872023-12-19 01:13:16 +0000643 // a list of aconfig_declarations module names that the stubs generated in this module
644 // depend on.
645 Aconfig_declarations []string
646
Jiyong Parkc678ad32018-04-10 13:07:10 +0900647 // TODO: determines whether to create HTML doc or not
Paul Duffine8409952022-09-22 16:24:46 +0100648 // Html_doc *bool
Jiyong Parkc678ad32018-04-10 13:07:10 +0900649}
650
Paul Duffin0f8faff2020-05-20 16:18:00 +0100651// Paths to outputs from java_sdk_library and java_sdk_library_import.
652//
653// Fields that are android.Paths are always set (during GenerateAndroidBuildActions).
654// OptionalPaths are always set by java_sdk_library but may not be set by
655// java_sdk_library_import as not all instances provide that information.
Paul Duffind1b3a922020-01-22 11:57:20 +0000656type scopePaths struct {
Paul Duffin0f8faff2020-05-20 16:18:00 +0100657 // The path (represented as Paths for convenience when returning) to the stubs header jar.
658 //
659 // That is the jar that is created by turbine.
660 stubsHeaderPath android.Paths
661
662 // The path (represented as Paths for convenience when returning) to the stubs implementation jar.
663 //
664 // This is not the implementation jar, it still only contains stubs.
665 stubsImplPath android.Paths
666
Paul Duffin1267d872021-04-16 17:21:36 +0100667 // The dex jar for the stubs.
668 //
669 // This is not the implementation jar, it still only contains stubs.
Martin Stjernholm8be1e6d2021-09-15 03:34:04 +0100670 stubsDexJarPath OptionalDexJarPath
Paul Duffin1267d872021-04-16 17:21:36 +0100671
Paul Duffin0f8faff2020-05-20 16:18:00 +0100672 // The API specification file, e.g. system_current.txt.
673 currentApiFilePath android.OptionalPath
674
675 // The specification of API elements removed since the last release.
676 removedApiFilePath android.OptionalPath
677
678 // The stubs source jar.
679 stubsSrcJar android.OptionalPath
Anton Hanssond78eb762021-09-21 15:25:12 +0100680
681 // Extracted annotations.
682 annotationsZip android.OptionalPath
Paul Duffin958806b2022-05-16 13:10:47 +0000683
684 // The path to the latest API file.
685 latestApiPath android.OptionalPath
686
687 // The path to the latest removed API file.
688 latestRemovedApiPath android.OptionalPath
Paul Duffind1b3a922020-01-22 11:57:20 +0000689}
690
Colin Crossdcf71b22021-02-01 13:59:03 -0800691func (paths *scopePaths) extractStubsLibraryInfoFromDependency(ctx android.ModuleContext, dep android.Module) error {
Colin Cross313aa542023-12-13 13:47:44 -0800692 if lib, ok := android.OtherModuleProvider(ctx, dep, JavaInfoProvider); ok {
Colin Crossdcf71b22021-02-01 13:59:03 -0800693 paths.stubsHeaderPath = lib.HeaderJars
694 paths.stubsImplPath = lib.ImplementationJars
Paul Duffin1267d872021-04-16 17:21:36 +0100695
696 libDep := dep.(UsesLibraryDependency)
Spandan Das59a4a2b2024-01-09 21:35:56 +0000697 paths.stubsDexJarPath = libDep.DexJarBuildPath(ctx)
Paul Duffinc8782502020-04-29 20:45:27 +0100698 return nil
699 } else {
Colin Crossdcf71b22021-02-01 13:59:03 -0800700 return fmt.Errorf("expected module that has JavaInfoProvider, e.g. java_library")
Paul Duffinc8782502020-04-29 20:45:27 +0100701 }
702}
703
Paul Duffin0ff08bd2020-04-29 13:30:54 +0100704func (paths *scopePaths) treatDepAsApiStubsProvider(dep android.Module, action func(provider ApiStubsProvider)) error {
705 if apiStubsProvider, ok := dep.(ApiStubsProvider); ok {
706 action(apiStubsProvider)
Paul Duffinc8782502020-04-29 20:45:27 +0100707 return nil
708 } else {
709 return fmt.Errorf("expected module that implements ApiStubsProvider, e.g. droidstubs")
710 }
711}
712
Paul Duffin0f8faff2020-05-20 16:18:00 +0100713func (paths *scopePaths) treatDepAsApiStubsSrcProvider(dep android.Module, action func(provider ApiStubsSrcProvider)) error {
714 if apiStubsProvider, ok := dep.(ApiStubsSrcProvider); ok {
715 action(apiStubsProvider)
716 return nil
717 } else {
718 return fmt.Errorf("expected module that implements ApiStubsSrcProvider, e.g. droidstubs")
719 }
720}
721
Paul Duffin0ff08bd2020-04-29 13:30:54 +0100722func (paths *scopePaths) extractApiInfoFromApiStubsProvider(provider ApiStubsProvider) {
Anton Hanssond78eb762021-09-21 15:25:12 +0100723 paths.annotationsZip = android.OptionalPathForPath(provider.AnnotationsZip())
Paul Duffin0f8faff2020-05-20 16:18:00 +0100724 paths.currentApiFilePath = android.OptionalPathForPath(provider.ApiFilePath())
725 paths.removedApiFilePath = android.OptionalPathForPath(provider.RemovedApiFilePath())
Paul Duffin0ff08bd2020-04-29 13:30:54 +0100726}
727
Colin Crossdcf71b22021-02-01 13:59:03 -0800728func (paths *scopePaths) extractApiInfoFromDep(ctx android.ModuleContext, dep android.Module) error {
Paul Duffin0ff08bd2020-04-29 13:30:54 +0100729 return paths.treatDepAsApiStubsProvider(dep, func(provider ApiStubsProvider) {
730 paths.extractApiInfoFromApiStubsProvider(provider)
731 })
732}
733
Paul Duffin0f8faff2020-05-20 16:18:00 +0100734func (paths *scopePaths) extractStubsSourceInfoFromApiStubsProviders(provider ApiStubsSrcProvider) {
735 paths.stubsSrcJar = android.OptionalPathForPath(provider.StubsSrcJar())
Paul Duffin0ff08bd2020-04-29 13:30:54 +0100736}
737
Colin Crossdcf71b22021-02-01 13:59:03 -0800738func (paths *scopePaths) extractStubsSourceInfoFromDep(ctx android.ModuleContext, dep android.Module) error {
Paul Duffin0f8faff2020-05-20 16:18:00 +0100739 return paths.treatDepAsApiStubsSrcProvider(dep, func(provider ApiStubsSrcProvider) {
Paul Duffin0ff08bd2020-04-29 13:30:54 +0100740 paths.extractStubsSourceInfoFromApiStubsProviders(provider)
741 })
742}
743
Colin Crossdcf71b22021-02-01 13:59:03 -0800744func (paths *scopePaths) extractStubsSourceAndApiInfoFromApiStubsProvider(ctx android.ModuleContext, dep android.Module) error {
Paul Duffin0ff08bd2020-04-29 13:30:54 +0100745 return paths.treatDepAsApiStubsProvider(dep, func(provider ApiStubsProvider) {
746 paths.extractApiInfoFromApiStubsProvider(provider)
747 paths.extractStubsSourceInfoFromApiStubsProviders(provider)
748 })
749}
750
Paul Duffin958806b2022-05-16 13:10:47 +0000751func extractSingleOptionalOutputPath(dep android.Module) (android.OptionalPath, error) {
752 var paths android.Paths
753 if sourceFileProducer, ok := dep.(android.SourceFileProducer); ok {
754 paths = sourceFileProducer.Srcs()
755 } else {
756 return android.OptionalPath{}, fmt.Errorf("module %q does not produce source files", dep)
757 }
758 if len(paths) != 1 {
759 return android.OptionalPath{}, fmt.Errorf("expected one path from %q, got %q", dep, paths)
760 }
761 return android.OptionalPathForPath(paths[0]), nil
762}
763
764func (paths *scopePaths) extractLatestApiPath(ctx android.ModuleContext, dep android.Module) error {
765 outputPath, err := extractSingleOptionalOutputPath(dep)
766 paths.latestApiPath = outputPath
767 return err
768}
769
770func (paths *scopePaths) extractLatestRemovedApiPath(ctx android.ModuleContext, dep android.Module) error {
771 outputPath, err := extractSingleOptionalOutputPath(dep)
772 paths.latestRemovedApiPath = outputPath
773 return err
774}
775
Paul Duffin0ff08bd2020-04-29 13:30:54 +0100776type commonToSdkLibraryAndImportProperties struct {
Paul Duffin1b1e8062020-05-08 13:44:43 +0100777 // The naming scheme to use for the components that this module creates.
778 //
Paul Duffinee9ad5d2020-09-11 13:04:05 +0100779 // If not specified then it defaults to "default".
Paul Duffin1b1e8062020-05-08 13:44:43 +0100780 //
781 // This is a temporary mechanism to simplify conversion from separate modules for each
782 // component that follow a different naming pattern to the default one.
783 //
784 // TODO(b/155480189) - Remove once naming inconsistencies have been resolved.
Paul Duffin0ff08bd2020-04-29 13:30:54 +0100785 Naming_scheme *string
Paul Duffindfa131e2020-05-15 20:37:11 +0100786
787 // Specifies whether this module can be used as an Android shared library; defaults
788 // to true.
789 //
790 // An Android shared library is one that can be referenced in a <uses-library> element
791 // in an AndroidManifest.xml.
792 Shared_library *bool
Paul Duffina2ae7e02020-09-11 11:55:00 +0100793
794 // Files containing information about supported java doc tags.
795 Doctag_files []string `android:"path"`
Pedro Loureiro9956e5e2021-09-07 17:21:59 +0000796
797 // Signals that this shared library is part of the bootclasspath starting
798 // on the version indicated in this attribute.
799 //
800 // This will make platforms at this level and above to ignore
801 // <uses-library> tags with this library name because the library is already
802 // available
803 On_bootclasspath_since *string
804
805 // Signals that this shared library was part of the bootclasspath before
806 // (but not including) the version indicated in this attribute.
807 //
808 // The system will automatically add a <uses-library> tag with this library to
809 // apps that target any SDK less than the version indicated in this attribute.
810 On_bootclasspath_before *string
811
812 // Indicates that PackageManager should ignore this shared library if the
813 // platform is below the version indicated in this attribute.
814 //
815 // This means that the device won't recognise this library as installed.
816 Min_device_sdk *string
817
818 // Indicates that PackageManager should ignore this shared library if the
819 // platform is above the version indicated in this attribute.
820 //
821 // This means that the device won't recognise this library as installed.
822 Max_device_sdk *string
Paul Duffin0ff08bd2020-04-29 13:30:54 +0100823}
824
Paul Duffin71b33cc2021-06-23 11:39:47 +0100825// commonSdkLibraryAndImportModule defines the interface that must be provided by a module that
826// embeds the commonToSdkLibraryAndImport struct.
827type commonSdkLibraryAndImportModule interface {
Paul Duffind796f6f2022-11-23 23:06:05 +0000828 android.Module
Paul Duffin71b33cc2021-06-23 11:39:47 +0100829
830 BaseModuleName() string
831}
832
Paul Duffin56d44902020-01-31 13:36:25 +0000833// Common code between sdk library and sdk library import
834type commonToSdkLibraryAndImport struct {
Paul Duffin71b33cc2021-06-23 11:39:47 +0100835 module commonSdkLibraryAndImportModule
Paul Duffinc3091c82020-05-08 14:16:20 +0100836
Paul Duffin56d44902020-01-31 13:36:25 +0000837 scopePaths map[*apiScope]*scopePaths
Paul Duffin1b1e8062020-05-08 13:44:43 +0100838
839 namingScheme sdkLibraryComponentNamingScheme
840
Paul Duffindfa131e2020-05-15 20:37:11 +0100841 commonSdkLibraryProperties commonToSdkLibraryAndImportProperties
Paul Duffin859fe962020-05-15 10:20:31 +0100842
Paul Duffina2ae7e02020-09-11 11:55:00 +0100843 // Paths to commonSdkLibraryProperties.Doctag_files
844 doctagPaths android.Paths
845
Paul Duffin859fe962020-05-15 10:20:31 +0100846 // Functionality related to this being used as a component of a java_sdk_library.
847 EmbeddableSdkLibraryComponent
Paul Duffin56d44902020-01-31 13:36:25 +0000848}
849
Paul Duffin71b33cc2021-06-23 11:39:47 +0100850func (c *commonToSdkLibraryAndImport) initCommon(module commonSdkLibraryAndImportModule) {
851 c.module = module
Paul Duffin1b1e8062020-05-08 13:44:43 +0100852
Paul Duffin71b33cc2021-06-23 11:39:47 +0100853 module.AddProperties(&c.commonSdkLibraryProperties)
Paul Duffin859fe962020-05-15 10:20:31 +0100854
855 // Initialize this as an sdk library component.
Paul Duffin71b33cc2021-06-23 11:39:47 +0100856 c.initSdkLibraryComponent(module)
Paul Duffin1b1e8062020-05-08 13:44:43 +0100857}
858
859func (c *commonToSdkLibraryAndImport) initCommonAfterDefaultsApplied(ctx android.DefaultableHookContext) bool {
Paul Duffindfa131e2020-05-15 20:37:11 +0100860 schemeProperty := proptools.StringDefault(c.commonSdkLibraryProperties.Naming_scheme, "default")
Paul Duffin1b1e8062020-05-08 13:44:43 +0100861 switch schemeProperty {
862 case "default":
863 c.namingScheme = &defaultNamingScheme{}
864 default:
865 ctx.PropertyErrorf("naming_scheme", "expected 'default' but was %q", schemeProperty)
866 return false
867 }
868
Paul Duffin3f0290e2021-06-30 18:25:36 +0100869 namePtr := proptools.StringPtr(c.module.BaseModuleName())
870 c.sdkLibraryComponentProperties.SdkLibraryName = namePtr
871
Paul Duffindfa131e2020-05-15 20:37:11 +0100872 // Only track this sdk library if this can be used as a shared library.
873 if c.sharedLibrary() {
874 // Use the name specified in the module definition as the owner.
Paul Duffin3f0290e2021-06-30 18:25:36 +0100875 c.sdkLibraryComponentProperties.SdkLibraryToImplicitlyTrack = namePtr
Paul Duffindfa131e2020-05-15 20:37:11 +0100876 }
Paul Duffin859fe962020-05-15 10:20:31 +0100877
Paul Duffin1b1e8062020-05-08 13:44:43 +0100878 return true
Paul Duffinc3091c82020-05-08 14:16:20 +0100879}
880
Paul Duffinea8f8082021-06-24 13:25:57 +0100881// uniqueApexVariations provides common implementation of the ApexModule.UniqueApexVariations
882// method.
883func (c *commonToSdkLibraryAndImport) uniqueApexVariations() bool {
884 // A java_sdk_library that is a shared library produces an XML file that makes the shared library
885 // usable from an AndroidManifest.xml's <uses-library> entry. That XML file contains the name of
886 // the APEX and so it needs a unique variation per APEX.
887 return c.sharedLibrary()
888}
889
Paul Duffina2ae7e02020-09-11 11:55:00 +0100890func (c *commonToSdkLibraryAndImport) generateCommonBuildActions(ctx android.ModuleContext) {
891 c.doctagPaths = android.PathsForModuleSrc(ctx, c.commonSdkLibraryProperties.Doctag_files)
892}
893
Paul Duffineedc5d52020-06-12 17:46:39 +0100894// Module name of the runtime implementation library
895func (c *commonToSdkLibraryAndImport) implLibraryModuleName() string {
Paul Duffin71b33cc2021-06-23 11:39:47 +0100896 return c.module.BaseModuleName() + ".impl"
Paul Duffineedc5d52020-06-12 17:46:39 +0100897}
898
899// Module name of the XML file for the lib
900func (c *commonToSdkLibraryAndImport) xmlPermissionsModuleName() string {
Paul Duffin71b33cc2021-06-23 11:39:47 +0100901 return c.module.BaseModuleName() + sdkXmlFileSuffix
Paul Duffineedc5d52020-06-12 17:46:39 +0100902}
903
Paul Duffinc3091c82020-05-08 14:16:20 +0100904// Name of the java_library module that compiles the stubs source.
905func (c *commonToSdkLibraryAndImport) stubsLibraryModuleName(apiScope *apiScope) string {
Paul Duffinb97b1572021-04-29 21:50:40 +0100906 baseName := c.module.BaseModuleName()
Paul Duffin21787622022-11-25 12:48:20 +0000907 return c.namingScheme.stubsLibraryModuleName(apiScope, baseName)
Paul Duffinc3091c82020-05-08 14:16:20 +0100908}
909
Jihoon Kangfa4a90d2023-12-20 02:53:38 +0000910// Name of the java_library module that compiles the exportable stubs source.
911func (c *commonToSdkLibraryAndImport) exportableStubsLibraryModuleName(apiScope *apiScope) string {
912 baseName := c.module.BaseModuleName()
913 return c.namingScheme.exportableStubsLibraryModuleName(apiScope, baseName)
914}
915
Paul Duffinc3091c82020-05-08 14:16:20 +0100916// Name of the droidstubs module that generates the stubs source and may also
917// generate/check the API.
918func (c *commonToSdkLibraryAndImport) stubsSourceModuleName(apiScope *apiScope) string {
Paul Duffinb97b1572021-04-29 21:50:40 +0100919 baseName := c.module.BaseModuleName()
Paul Duffin21787622022-11-25 12:48:20 +0000920 return c.namingScheme.stubsSourceModuleName(apiScope, baseName)
Paul Duffinc3091c82020-05-08 14:16:20 +0100921}
922
Jihoon Kang1c92c3e2023-03-23 17:44:51 +0000923// Name of the java_api_library module that generates the from-text stubs source
924// and compiles to a jar file.
925func (c *commonToSdkLibraryAndImport) apiLibraryModuleName(apiScope *apiScope) string {
926 baseName := c.module.BaseModuleName()
927 return c.namingScheme.apiLibraryModuleName(apiScope, baseName)
928}
929
Jihoon Kang1147b312023-06-08 23:25:57 +0000930// Name of the java_library module that compiles the stubs
931// generated from source Java files.
Jihoon Kangfa4a90d2023-12-20 02:53:38 +0000932func (c *commonToSdkLibraryAndImport) sourceStubsLibraryModuleName(apiScope *apiScope) string {
Jihoon Kang1147b312023-06-08 23:25:57 +0000933 baseName := c.module.BaseModuleName()
Jihoon Kangfa4a90d2023-12-20 02:53:38 +0000934 return c.namingScheme.sourceStubsLibraryModuleName(apiScope, baseName)
935}
936
937// Name of the java_library module that compiles the exportable stubs
938// generated from source Java files.
939func (c *commonToSdkLibraryAndImport) exportableSourceStubsLibraryModuleName(apiScope *apiScope) string {
940 baseName := c.module.BaseModuleName()
941 return c.namingScheme.exportableSourceStubsLibraryModuleName(apiScope, baseName)
Jihoon Kang1147b312023-06-08 23:25:57 +0000942}
943
Paul Duffin46dc45a2020-05-14 15:39:10 +0100944// The component names for different outputs of the java_sdk_library.
945//
946// They are similar to the names used for the child modules it creates
947const (
948 stubsSourceComponentName = "stubs.source"
949
950 apiTxtComponentName = "api.txt"
951
952 removedApiTxtComponentName = "removed-api.txt"
Anton Hanssond78eb762021-09-21 15:25:12 +0100953
954 annotationsComponentName = "annotations.zip"
Paul Duffin46dc45a2020-05-14 15:39:10 +0100955)
956
957// A regular expression to match tags that reference a specific stubs component.
958//
959// It will only match if given a valid scope and a valid component. It is verfy strict
960// to ensure it does not accidentally match a similar looking tag that should be processed
961// by the embedded Library.
962var tagSplitter = func() *regexp.Regexp {
963 // Given a list of literal string items returns a regular expression that will
964 // match any one of the items.
965 choice := func(items ...string) string {
966 return `\Q` + strings.Join(items, `\E|\Q`) + `\E`
967 }
968
969 // Regular expression to match one of the scopes.
970 scopesRegexp := choice(allScopeNames...)
971
972 // Regular expression to match one of the components.
Anton Hanssond78eb762021-09-21 15:25:12 +0100973 componentsRegexp := choice(stubsSourceComponentName, apiTxtComponentName, removedApiTxtComponentName, annotationsComponentName)
Paul Duffin46dc45a2020-05-14 15:39:10 +0100974
975 // Regular expression to match any combination of one scope and one component.
976 return regexp.MustCompile(fmt.Sprintf(`^\.(%s)\.(%s)$`, scopesRegexp, componentsRegexp))
977}()
978
979// For OutputFileProducer interface
980//
Anton Hanssond78eb762021-09-21 15:25:12 +0100981// .<scope>.<component name>, for all ComponentNames (for example: .public.removed-api.txt)
Paul Duffin46dc45a2020-05-14 15:39:10 +0100982func (c *commonToSdkLibraryAndImport) commonOutputFiles(tag string) (android.Paths, error) {
983 if groups := tagSplitter.FindStringSubmatch(tag); groups != nil {
984 scopeName := groups[1]
985 component := groups[2]
986
987 if scope, ok := scopeByName[scopeName]; ok {
988 paths := c.findScopePaths(scope)
989 if paths == nil {
Paul Duffin71b33cc2021-06-23 11:39:47 +0100990 return nil, fmt.Errorf("%q does not provide api scope %s", c.module.BaseModuleName(), scopeName)
Paul Duffin46dc45a2020-05-14 15:39:10 +0100991 }
992
993 switch component {
994 case stubsSourceComponentName:
995 if paths.stubsSrcJar.Valid() {
996 return android.Paths{paths.stubsSrcJar.Path()}, nil
997 }
998
999 case apiTxtComponentName:
1000 if paths.currentApiFilePath.Valid() {
1001 return android.Paths{paths.currentApiFilePath.Path()}, nil
1002 }
1003
1004 case removedApiTxtComponentName:
1005 if paths.removedApiFilePath.Valid() {
1006 return android.Paths{paths.removedApiFilePath.Path()}, nil
1007 }
Anton Hanssond78eb762021-09-21 15:25:12 +01001008
1009 case annotationsComponentName:
1010 if paths.annotationsZip.Valid() {
1011 return android.Paths{paths.annotationsZip.Path()}, nil
1012 }
Paul Duffin46dc45a2020-05-14 15:39:10 +01001013 }
1014
1015 return nil, fmt.Errorf("%s not available for api scope %s", component, scopeName)
1016 } else {
1017 return nil, fmt.Errorf("unknown scope %s in %s", scope, tag)
1018 }
1019
1020 } else {
Paul Duffina2ae7e02020-09-11 11:55:00 +01001021 switch tag {
1022 case ".doctags":
1023 if c.doctagPaths != nil {
1024 return c.doctagPaths, nil
1025 } else {
Paul Duffin71b33cc2021-06-23 11:39:47 +01001026 return nil, fmt.Errorf("no doctag_files specified on %s", c.module.BaseModuleName())
Paul Duffina2ae7e02020-09-11 11:55:00 +01001027 }
1028 }
Paul Duffin46dc45a2020-05-14 15:39:10 +01001029 return nil, nil
1030 }
1031}
1032
Paul Duffin803a9562020-05-20 11:52:25 +01001033func (c *commonToSdkLibraryAndImport) getScopePathsCreateIfNeeded(scope *apiScope) *scopePaths {
Paul Duffin56d44902020-01-31 13:36:25 +00001034 if c.scopePaths == nil {
1035 c.scopePaths = make(map[*apiScope]*scopePaths)
1036 }
1037 paths := c.scopePaths[scope]
1038 if paths == nil {
1039 paths = &scopePaths{}
1040 c.scopePaths[scope] = paths
1041 }
1042
1043 return paths
1044}
1045
Paul Duffin803a9562020-05-20 11:52:25 +01001046func (c *commonToSdkLibraryAndImport) findScopePaths(scope *apiScope) *scopePaths {
1047 if c.scopePaths == nil {
1048 return nil
1049 }
1050
1051 return c.scopePaths[scope]
1052}
1053
1054// If this does not support the requested api scope then find the closest available
1055// scope it does support. Returns nil if no such scope is available.
1056func (c *commonToSdkLibraryAndImport) findClosestScopePath(scope *apiScope) *scopePaths {
Paul Duffind0b9fca2022-09-30 18:11:41 +01001057 for s := scope; s != nil; s = s.canAccess {
Paul Duffin803a9562020-05-20 11:52:25 +01001058 if paths := c.findScopePaths(s); paths != nil {
1059 return paths
1060 }
1061 }
1062
1063 // This should never happen outside tests as public should be the base scope for every
1064 // scope and is enabled by default.
1065 return nil
1066}
1067
Jiyong Parkf1691d22021-03-29 20:11:58 +09001068func (c *commonToSdkLibraryAndImport) selectHeaderJarsForSdkVersion(ctx android.BaseModuleContext, sdkVersion android.SdkSpec) android.Paths {
Paul Duffinb05d4292020-05-20 12:19:10 +01001069
1070 // If a specific numeric version has been requested then use prebuilt versions of the sdk.
Jiyong Park54105c42021-03-31 18:17:53 +09001071 if !sdkVersion.ApiLevel.IsPreview() {
Paul Duffin71b33cc2021-06-23 11:39:47 +01001072 return PrebuiltJars(ctx, c.module.BaseModuleName(), sdkVersion)
Paul Duffinb05d4292020-05-20 12:19:10 +01001073 }
1074
Paul Duffin1267d872021-04-16 17:21:36 +01001075 paths := c.selectScopePaths(ctx, sdkVersion.Kind)
1076 if paths == nil {
1077 return nil
1078 }
1079
1080 return paths.stubsHeaderPath
1081}
1082
1083// selectScopePaths returns the *scopePaths appropriate for the specific kind.
1084//
1085// If the module does not support the specific kind then it will return the *scopePaths for the
1086// closest kind which is a subset of the requested kind. e.g. if requesting android.SdkModule then
1087// it will return *scopePaths for android.SdkSystem if available or android.SdkPublic of not.
1088func (c *commonToSdkLibraryAndImport) selectScopePaths(ctx android.BaseModuleContext, kind android.SdkKind) *scopePaths {
Paul Duffin32cf58a2021-05-18 16:32:50 +01001089 apiScope := sdkKindToApiScope(kind)
Paul Duffinb05d4292020-05-20 12:19:10 +01001090
Paul Duffin803a9562020-05-20 11:52:25 +01001091 paths := c.findClosestScopePath(apiScope)
1092 if paths == nil {
1093 var scopes []string
1094 for _, s := range allApiScopes {
1095 if c.findScopePaths(s) != nil {
1096 scopes = append(scopes, s.name)
1097 }
1098 }
Paul Duffin71b33cc2021-06-23 11:39:47 +01001099 ctx.ModuleErrorf("requires api scope %s from %s but it only has %q available", apiScope.name, c.module.BaseModuleName(), scopes)
Paul Duffin803a9562020-05-20 11:52:25 +01001100 return nil
1101 }
1102
Paul Duffin1267d872021-04-16 17:21:36 +01001103 return paths
1104}
1105
Paul Duffin32cf58a2021-05-18 16:32:50 +01001106// sdkKindToApiScope maps from android.SdkKind to apiScope.
1107func sdkKindToApiScope(kind android.SdkKind) *apiScope {
1108 var apiScope *apiScope
1109 switch kind {
1110 case android.SdkSystem:
1111 apiScope = apiScopeSystem
1112 case android.SdkModule:
1113 apiScope = apiScopeModuleLib
1114 case android.SdkTest:
1115 apiScope = apiScopeTest
1116 case android.SdkSystemServer:
1117 apiScope = apiScopeSystemServer
1118 default:
1119 apiScope = apiScopePublic
1120 }
1121 return apiScope
1122}
1123
Paul Duffin1267d872021-04-16 17:21:36 +01001124// to satisfy SdkLibraryDependency interface
Martin Stjernholm8be1e6d2021-09-15 03:34:04 +01001125func (c *commonToSdkLibraryAndImport) SdkApiStubDexJar(ctx android.BaseModuleContext, kind android.SdkKind) OptionalDexJarPath {
Paul Duffin1267d872021-04-16 17:21:36 +01001126 paths := c.selectScopePaths(ctx, kind)
1127 if paths == nil {
Martin Stjernholm8be1e6d2021-09-15 03:34:04 +01001128 return makeUnsetDexJarPath()
Paul Duffin1267d872021-04-16 17:21:36 +01001129 }
1130
1131 return paths.stubsDexJarPath
Paul Duffinb05d4292020-05-20 12:19:10 +01001132}
1133
Paul Duffin32cf58a2021-05-18 16:32:50 +01001134// to satisfy SdkLibraryDependency interface
1135func (c *commonToSdkLibraryAndImport) SdkRemovedTxtFile(ctx android.BaseModuleContext, kind android.SdkKind) android.OptionalPath {
1136 apiScope := sdkKindToApiScope(kind)
1137 paths := c.findScopePaths(apiScope)
1138 if paths == nil {
1139 return android.OptionalPath{}
1140 }
1141
1142 return paths.removedApiFilePath
1143}
1144
Paul Duffin859fe962020-05-15 10:20:31 +01001145func (c *commonToSdkLibraryAndImport) sdkComponentPropertiesForChildLibrary() interface{} {
1146 componentProps := &struct {
Paul Duffin3f0290e2021-06-30 18:25:36 +01001147 SdkLibraryName *string
Paul Duffin859fe962020-05-15 10:20:31 +01001148 SdkLibraryToImplicitlyTrack *string
Paul Duffindfa131e2020-05-15 20:37:11 +01001149 }{}
1150
Paul Duffin3f0290e2021-06-30 18:25:36 +01001151 namePtr := proptools.StringPtr(c.module.BaseModuleName())
1152 componentProps.SdkLibraryName = namePtr
1153
Paul Duffindfa131e2020-05-15 20:37:11 +01001154 if c.sharedLibrary() {
Paul Duffin859fe962020-05-15 10:20:31 +01001155 // Mark the stubs library as being components of this java_sdk_library so that
1156 // any app that includes code which depends (directly or indirectly) on the stubs
1157 // library will have the appropriate <uses-library> invocation inserted into its
1158 // manifest if necessary.
Paul Duffin3f0290e2021-06-30 18:25:36 +01001159 componentProps.SdkLibraryToImplicitlyTrack = namePtr
Paul Duffin859fe962020-05-15 10:20:31 +01001160 }
1161
1162 return componentProps
1163}
1164
Paul Duffindfa131e2020-05-15 20:37:11 +01001165func (c *commonToSdkLibraryAndImport) sharedLibrary() bool {
1166 return proptools.BoolDefault(c.commonSdkLibraryProperties.Shared_library, true)
1167}
1168
Paul Duffinf4600f62021-05-13 22:34:45 +01001169// Check if the stub libraries should be compiled for dex
1170func (c *commonToSdkLibraryAndImport) stubLibrariesCompiledForDex() bool {
1171 // Always compile the dex file files for the stub libraries if they will be used on the
1172 // bootclasspath.
1173 return !c.sharedLibrary()
1174}
1175
Paul Duffin859fe962020-05-15 10:20:31 +01001176// Properties related to the use of a module as an component of a java_sdk_library.
1177type SdkLibraryComponentProperties struct {
Paul Duffin3f0290e2021-06-30 18:25:36 +01001178 // The name of the java_sdk_library/_import module.
1179 SdkLibraryName *string `blueprint:"mutated"`
Paul Duffin859fe962020-05-15 10:20:31 +01001180
1181 // The name of the java_sdk_library/_import to add to a <uses-library> entry
1182 // in the AndroidManifest.xml of any Android app that includes code that references
1183 // this module. If not set then no java_sdk_library/_import is tracked.
1184 SdkLibraryToImplicitlyTrack *string `blueprint:"mutated"`
1185}
1186
1187// Structure to be embedded in a module struct that needs to support the
1188// SdkLibraryComponentDependency interface.
1189type EmbeddableSdkLibraryComponent struct {
1190 sdkLibraryComponentProperties SdkLibraryComponentProperties
1191}
1192
Paul Duffin71b33cc2021-06-23 11:39:47 +01001193func (e *EmbeddableSdkLibraryComponent) initSdkLibraryComponent(module android.Module) {
1194 module.AddProperties(&e.sdkLibraryComponentProperties)
Paul Duffin859fe962020-05-15 10:20:31 +01001195}
1196
1197// to satisfy SdkLibraryComponentDependency
Paul Duffin3f0290e2021-06-30 18:25:36 +01001198func (e *EmbeddableSdkLibraryComponent) SdkLibraryName() *string {
1199 return e.sdkLibraryComponentProperties.SdkLibraryName
1200}
1201
1202// to satisfy SdkLibraryComponentDependency
Ulya Trafimovich39b437b2020-09-23 16:42:35 +01001203func (e *EmbeddableSdkLibraryComponent) OptionalSdkLibraryImplementation() *string {
Ulya Trafimovich78645fb2021-07-16 15:29:25 +01001204 // For shared libraries, this is the same as the SDK library name. If a Java library or app
1205 // depends on a component library (e.g. a stub library) it still needs to know the name of the
1206 // run-time library and the corresponding module that provides the implementation. This name is
1207 // passed to manifest_fixer (to be added to AndroidManifest.xml) and added to CLC (to be used
1208 // in dexpreopt).
1209 //
1210 // For non-shared SDK (component or not) libraries this returns `nil`, as they are not
1211 // <uses-library> and should not be added to the manifest or to CLC.
Ulya Trafimovich39b437b2020-09-23 16:42:35 +01001212 return e.sdkLibraryComponentProperties.SdkLibraryToImplicitlyTrack
1213}
1214
Paul Duffin859fe962020-05-15 10:20:31 +01001215// Implemented by modules that are (or possibly could be) a component of a java_sdk_library
1216// (including the java_sdk_library) itself.
1217type SdkLibraryComponentDependency interface {
Ulya Trafimovich31e444e2020-08-14 17:32:16 +01001218 UsesLibraryDependency
1219
Paul Duffin3f0290e2021-06-30 18:25:36 +01001220 // SdkLibraryName returns the name of the java_sdk_library/_import module.
1221 SdkLibraryName() *string
1222
Ulya Trafimovich39b437b2020-09-23 16:42:35 +01001223 // The name of the implementation library for the optional SDK library or nil, if there isn't one.
1224 OptionalSdkLibraryImplementation() *string
Paul Duffin859fe962020-05-15 10:20:31 +01001225}
1226
1227// Make sure that all the module types that are components of java_sdk_library/_import
1228// and which can be referenced (directly or indirectly) from an android app implement
1229// the SdkLibraryComponentDependency interface.
1230var _ SdkLibraryComponentDependency = (*Library)(nil)
1231var _ SdkLibraryComponentDependency = (*Import)(nil)
1232var _ SdkLibraryComponentDependency = (*SdkLibrary)(nil)
Paul Duffineedc5d52020-06-12 17:46:39 +01001233var _ SdkLibraryComponentDependency = (*SdkLibraryImport)(nil)
Paul Duffin859fe962020-05-15 10:20:31 +01001234
Paul Duffin32cf58a2021-05-18 16:32:50 +01001235// Provides access to sdk_version related files, e.g. header and implementation jars.
Paul Duffin859fe962020-05-15 10:20:31 +01001236type SdkLibraryDependency interface {
1237 SdkLibraryComponentDependency
1238
1239 // Get the header jars appropriate for the supplied sdk_version.
1240 //
1241 // These are turbine generated jars so they only change if the externals of the
1242 // class changes but it does not contain and implementation or JavaDoc.
Jiyong Parkf1691d22021-03-29 20:11:58 +09001243 SdkHeaderJars(ctx android.BaseModuleContext, sdkVersion android.SdkSpec) android.Paths
Paul Duffin859fe962020-05-15 10:20:31 +01001244
1245 // Get the implementation jars appropriate for the supplied sdk version.
1246 //
1247 // These are either the implementation jar for the whole sdk library or the implementation
1248 // jars for the stubs. The latter should only be needed when generating JavaDoc as otherwise
1249 // they are identical to the corresponding header jars.
Jiyong Parkf1691d22021-03-29 20:11:58 +09001250 SdkImplementationJars(ctx android.BaseModuleContext, sdkVersion android.SdkSpec) android.Paths
Paul Duffin1267d872021-04-16 17:21:36 +01001251
1252 // SdkApiStubDexJar returns the dex jar for the stubs. It is needed by the hiddenapi processing
1253 // tool which processes dex files.
Martin Stjernholm8be1e6d2021-09-15 03:34:04 +01001254 SdkApiStubDexJar(ctx android.BaseModuleContext, kind android.SdkKind) OptionalDexJarPath
Paul Duffinf4600f62021-05-13 22:34:45 +01001255
Paul Duffin32cf58a2021-05-18 16:32:50 +01001256 // SdkRemovedTxtFile returns the optional path to the removed.txt file for the specified sdk kind.
1257 SdkRemovedTxtFile(ctx android.BaseModuleContext, kind android.SdkKind) android.OptionalPath
1258
Paul Duffinf4600f62021-05-13 22:34:45 +01001259 // sharedLibrary returns true if this can be used as a shared library.
1260 sharedLibrary() bool
Paul Duffin859fe962020-05-15 10:20:31 +01001261}
1262
Inseob Kimc0907f12019-02-08 21:00:45 +09001263type SdkLibrary struct {
Sundong Ahn054b19a2018-10-19 13:46:09 +09001264 Library
Jiyong Parkc678ad32018-04-10 13:07:10 +09001265
Sundong Ahn054b19a2018-10-19 13:46:09 +09001266 sdkLibraryProperties sdkLibraryProperties
Jiyong Parkc678ad32018-04-10 13:07:10 +09001267
Paul Duffin3375e352020-04-28 10:44:03 +01001268 // Map from api scope to the scope specific property structure.
1269 scopeToProperties map[*apiScope]*ApiScopeProperties
1270
Paul Duffin56d44902020-01-31 13:36:25 +00001271 commonToSdkLibraryAndImport
Jiyong Parkc678ad32018-04-10 13:07:10 +09001272}
1273
Inseob Kimc0907f12019-02-08 21:00:45 +09001274var _ SdkLibraryDependency = (*SdkLibrary)(nil)
Colin Cross897d2ed2019-02-11 14:03:51 -08001275
Paul Duffin3375e352020-04-28 10:44:03 +01001276func (module *SdkLibrary) generateTestAndSystemScopesByDefault() bool {
1277 return module.sdkLibraryProperties.Generate_system_and_test_apis
1278}
1279
1280func (module *SdkLibrary) getGeneratedApiScopes(ctx android.EarlyModuleContext) apiScopes {
1281 // Check to see if any scopes have been explicitly enabled. If any have then all
1282 // must be.
1283 anyScopesExplicitlyEnabled := false
1284 for _, scope := range allApiScopes {
1285 scopeProperties := module.scopeToProperties[scope]
1286 if scopeProperties.Enabled != nil {
1287 anyScopesExplicitlyEnabled = true
1288 break
1289 }
Paul Duffind1b3a922020-01-22 11:57:20 +00001290 }
Paul Duffin3375e352020-04-28 10:44:03 +01001291
1292 var generatedScopes apiScopes
1293 enabledScopes := make(map[*apiScope]struct{})
1294 for _, scope := range allApiScopes {
1295 scopeProperties := module.scopeToProperties[scope]
1296 // If any scopes are explicitly enabled then ignore the legacy enabled status.
1297 // This is to ensure that any new usages of this module type do not rely on legacy
1298 // behaviour.
1299 defaultEnabledStatus := false
1300 if anyScopesExplicitlyEnabled {
1301 defaultEnabledStatus = scope.defaultEnabledStatus
1302 } else {
1303 defaultEnabledStatus = scope.legacyEnabledStatus(module)
1304 }
1305 enabled := proptools.BoolDefault(scopeProperties.Enabled, defaultEnabledStatus)
1306 if enabled {
1307 enabledScopes[scope] = struct{}{}
1308 generatedScopes = append(generatedScopes, scope)
1309 }
1310 }
1311
1312 // Now check to make sure that any scope that is extended by an enabled scope is also
1313 // enabled.
1314 for _, scope := range allApiScopes {
1315 if _, ok := enabledScopes[scope]; ok {
1316 extends := scope.extends
1317 if extends != nil {
1318 if _, ok := enabledScopes[extends]; !ok {
1319 ctx.ModuleErrorf("enabled api scope %q depends on disabled scope %q", scope, extends)
1320 }
1321 }
1322 }
1323 }
1324
1325 return generatedScopes
Paul Duffind1b3a922020-01-22 11:57:20 +00001326}
1327
satayev758968a2021-12-06 11:42:40 +00001328var _ android.ModuleWithMinSdkVersionCheck = (*SdkLibrary)(nil)
1329
satayev8f088b02021-12-06 11:40:46 +00001330func (module *SdkLibrary) CheckMinSdkVersion(ctx android.ModuleContext) {
Spandan Das8c9ae7e2023-03-03 21:20:36 +00001331 android.CheckMinSdkVersion(ctx, module.MinSdkVersion(ctx), func(c android.ModuleContext, do android.PayloadDepsCallback) {
satayev8f088b02021-12-06 11:40:46 +00001332 ctx.WalkDeps(func(child android.Module, parent android.Module) bool {
1333 isExternal := !module.depIsInSameApex(ctx, child)
1334 if am, ok := child.(android.ApexModule); ok {
1335 if !do(ctx, parent, am, isExternal) {
1336 return false
1337 }
1338 }
1339 return !isExternal
1340 })
1341 })
1342}
1343
Paul Duffineedc5d52020-06-12 17:46:39 +01001344type sdkLibraryComponentTag struct {
1345 blueprint.BaseDependencyTag
1346 name string
1347}
1348
1349// Mark this tag so dependencies that use it are excluded from visibility enforcement.
1350func (t sdkLibraryComponentTag) ExcludeFromVisibilityEnforcement() {}
1351
1352var xmlPermissionsFileTag = sdkLibraryComponentTag{name: "xml-permissions-file"}
Paul Duffine74ac732020-02-06 13:51:46 +00001353
Jiyong Parke3833882020-02-17 17:28:10 +09001354func IsXmlPermissionsFileDepTag(depTag blueprint.DependencyTag) bool {
Paul Duffineedc5d52020-06-12 17:46:39 +01001355 if dt, ok := depTag.(sdkLibraryComponentTag); ok {
Jiyong Parke3833882020-02-17 17:28:10 +09001356 return dt == xmlPermissionsFileTag
1357 }
1358 return false
1359}
1360
Paul Duffineedc5d52020-06-12 17:46:39 +01001361var implLibraryTag = sdkLibraryComponentTag{name: "impl-library"}
Paul Duffin5df79302020-05-16 15:52:12 +01001362
Paul Duffin44f1d842020-06-26 20:17:02 +01001363// Add the dependencies on the child modules in the component deps mutator.
1364func (module *SdkLibrary) ComponentDepsMutator(ctx android.BottomUpMutatorContext) {
Paul Duffin3375e352020-04-28 10:44:03 +01001365 for _, apiScope := range module.getGeneratedApiScopes(ctx) {
Paul Duffind1b3a922020-01-22 11:57:20 +00001366 // Add dependencies to the stubs library
Spandan Das877f39d2023-03-29 16:19:51 +00001367 stubModuleName := module.stubsLibraryModuleName(apiScope)
Jihoon Kang1147b312023-06-08 23:25:57 +00001368
Spandan Das877f39d2023-03-29 16:19:51 +00001369 ctx.AddVariationDependencies(nil, apiScope.stubsTag, stubModuleName)
Paul Duffind1b3a922020-01-22 11:57:20 +00001370
Paul Duffin15f34ef2020-07-20 18:04:44 +01001371 // Add a dependency on the stubs source in order to access both stubs source and api information.
1372 ctx.AddVariationDependencies(nil, apiScope.stubsSourceAndApiTag, module.stubsSourceModuleName(apiScope))
Paul Duffin958806b2022-05-16 13:10:47 +00001373
1374 if module.compareAgainstLatestApi(apiScope) {
1375 // Add dependencies on the latest finalized version of the API .txt file.
1376 latestApiModuleName := module.latestApiModuleName(apiScope)
1377 ctx.AddDependency(module, apiScope.latestApiModuleTag, latestApiModuleName)
1378
1379 // Add dependencies on the latest finalized version of the remove API .txt file.
1380 latestRemovedApiModuleName := module.latestRemovedApiModuleName(apiScope)
1381 ctx.AddDependency(module, apiScope.latestRemovedApiModuleTag, latestRemovedApiModuleName)
1382 }
Sundong Ahn054b19a2018-10-19 13:46:09 +09001383 }
1384
Paul Duffindfa131e2020-05-15 20:37:11 +01001385 if module.requiresRuntimeImplementationLibrary() {
Paul Duffin5df79302020-05-16 15:52:12 +01001386 // Add dependency to the rule for generating the implementation library.
1387 ctx.AddDependency(module, implLibraryTag, module.implLibraryModuleName())
1388
Paul Duffindfa131e2020-05-15 20:37:11 +01001389 if module.sharedLibrary() {
1390 // Add dependency to the rule for generating the xml permissions file
Paul Duffineedc5d52020-06-12 17:46:39 +01001391 ctx.AddDependency(module, xmlPermissionsFileTag, module.xmlPermissionsModuleName())
Paul Duffindfa131e2020-05-15 20:37:11 +01001392 }
Paul Duffin44f1d842020-06-26 20:17:02 +01001393 }
1394}
Paul Duffine74ac732020-02-06 13:51:46 +00001395
Paul Duffin44f1d842020-06-26 20:17:02 +01001396// Add other dependencies as normal.
1397func (module *SdkLibrary) DepsMutator(ctx android.BottomUpMutatorContext) {
Anton Hanssone77fccc2021-01-20 16:52:41 +00001398 var missingApiModules []string
1399 for _, apiScope := range module.getGeneratedApiScopes(ctx) {
1400 if apiScope.unstable {
1401 continue
1402 }
Paul Duffin958806b2022-05-16 13:10:47 +00001403 if m := module.latestApiModuleName(apiScope); !ctx.OtherModuleExists(m) {
Anton Hanssone77fccc2021-01-20 16:52:41 +00001404 missingApiModules = append(missingApiModules, m)
1405 }
Paul Duffin958806b2022-05-16 13:10:47 +00001406 if m := module.latestRemovedApiModuleName(apiScope); !ctx.OtherModuleExists(m) {
Anton Hanssone77fccc2021-01-20 16:52:41 +00001407 missingApiModules = append(missingApiModules, m)
1408 }
Paul Duffin958806b2022-05-16 13:10:47 +00001409 if m := module.latestIncompatibilitiesModuleName(apiScope); !ctx.OtherModuleExists(m) {
Jaewoong Jung1a97ee02021-03-09 13:25:02 -08001410 missingApiModules = append(missingApiModules, m)
1411 }
Anton Hanssone77fccc2021-01-20 16:52:41 +00001412 }
1413 if len(missingApiModules) != 0 && !module.sdkLibraryProperties.Unsafe_ignore_missing_latest_api {
1414 m := module.Name() + " is missing tracking files for previously released library versions.\n"
1415 m += "You need to do one of the following:\n"
1416 m += "- Add `unsafe_ignore_missing_latest_api: true` to your blueprint (to disable compat tracking)\n"
1417 m += "- Add a set of prebuilt txt files representing the last released version of this library for compat checking.\n"
1418 m += " (the current set of API files can be used as a seed for this compatibility tracking\n"
1419 m += "\n"
1420 m += "The following filegroup modules are missing:\n "
1421 m += strings.Join(missingApiModules, "\n ") + "\n"
1422 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."
1423 ctx.ModuleErrorf(m)
1424 }
Paul Duffin44f1d842020-06-26 20:17:02 +01001425 if module.requiresRuntimeImplementationLibrary() {
Paul Duffindfa131e2020-05-15 20:37:11 +01001426 // Only add the deps for the library if it is actually going to be built.
1427 module.Library.deps(ctx)
1428 }
Jiyong Parkc678ad32018-04-10 13:07:10 +09001429}
1430
Paul Duffin46dc45a2020-05-14 15:39:10 +01001431func (module *SdkLibrary) OutputFiles(tag string) (android.Paths, error) {
1432 paths, err := module.commonOutputFiles(tag)
Colin Cross4acaea92021-12-10 23:05:02 +00001433 if paths != nil || err != nil {
Paul Duffin46dc45a2020-05-14 15:39:10 +01001434 return paths, err
1435 }
Colin Cross4acaea92021-12-10 23:05:02 +00001436 if module.requiresRuntimeImplementationLibrary() {
1437 return module.Library.OutputFiles(tag)
1438 }
1439 if tag == "" {
1440 return nil, nil
1441 }
1442 return nil, fmt.Errorf("unsupported module reference tag %q", tag)
Paul Duffin46dc45a2020-05-14 15:39:10 +01001443}
1444
Inseob Kimc0907f12019-02-08 21:00:45 +09001445func (module *SdkLibrary) GenerateAndroidBuildActions(ctx android.ModuleContext) {
satayev8f088b02021-12-06 11:40:46 +00001446 if proptools.String(module.deviceProperties.Min_sdk_version) != "" {
1447 module.CheckMinSdkVersion(ctx)
1448 }
1449
Paul Duffina2ae7e02020-09-11 11:55:00 +01001450 module.generateCommonBuildActions(ctx)
1451
Paul Duffindfa131e2020-05-15 20:37:11 +01001452 // Only build an implementation library if required.
1453 if module.requiresRuntimeImplementationLibrary() {
Paul Duffin43db9be2019-12-30 17:35:49 +00001454 module.Library.GenerateAndroidBuildActions(ctx)
1455 }
Sundong Ahn054b19a2018-10-19 13:46:09 +09001456
Paul Duffinb97b1572021-04-29 21:50:40 +01001457 // Collate the components exported by this module. All scope specific modules are exported but
1458 // the impl and xml component modules are not.
1459 exportedComponents := map[string]struct{}{}
1460
Sundong Ahn57368eb2018-07-06 11:20:23 +09001461 // Record the paths to the header jars of the library (stubs and impl).
Paul Duffind1b3a922020-01-22 11:57:20 +00001462 // When this java_sdk_library is depended upon from others via "libs" property,
Jiyong Parkc678ad32018-04-10 13:07:10 +09001463 // the recorded paths will be returned depending on the link type of the caller.
1464 ctx.VisitDirectDeps(func(to android.Module) {
Jiyong Parkc678ad32018-04-10 13:07:10 +09001465 tag := ctx.OtherModuleDependencyTag(to)
1466
Paul Duffinc8782502020-04-29 20:45:27 +01001467 // Extract information from any of the scope specific dependencies.
1468 if scopeTag, ok := tag.(scopeDependencyTag); ok {
1469 apiScope := scopeTag.apiScope
Paul Duffin803a9562020-05-20 11:52:25 +01001470 scopePaths := module.getScopePathsCreateIfNeeded(apiScope)
Paul Duffinc8782502020-04-29 20:45:27 +01001471
1472 // Extract information from the dependency. The exact information extracted
1473 // is determined by the nature of the dependency which is determined by the tag.
1474 scopeTag.extractDepInfo(ctx, to, scopePaths)
Paul Duffinb97b1572021-04-29 21:50:40 +01001475
1476 exportedComponents[ctx.OtherModuleName(to)] = struct{}{}
Sundong Ahn20e998b2018-07-24 11:19:26 +09001477 }
Jiyong Parkc678ad32018-04-10 13:07:10 +09001478 })
Paul Duffinb97b1572021-04-29 21:50:40 +01001479
1480 // Make the set of components exported by this module available for use elsewhere.
Cole Faust18994c72023-02-28 16:02:16 -08001481 exportedComponentInfo := android.ExportedComponentsInfo{Components: android.SortedKeys(exportedComponents)}
Colin Cross40213022023-12-13 15:19:49 -08001482 android.SetProvider(ctx, android.ExportedComponentsInfoProvider, exportedComponentInfo)
Paul Duffin958806b2022-05-16 13:10:47 +00001483
1484 // Provide additional information for inclusion in an sdk's generated .info file.
1485 additionalSdkInfo := map[string]interface{}{}
1486 additionalSdkInfo["dist_stem"] = module.distStem()
Paul Duffine8409952022-09-22 16:24:46 +01001487 baseModuleName := module.distStem()
Paul Duffin958806b2022-05-16 13:10:47 +00001488 scopes := map[string]interface{}{}
1489 additionalSdkInfo["scopes"] = scopes
1490 for scope, scopePaths := range module.scopePaths {
1491 scopeInfo := map[string]interface{}{}
1492 scopes[scope.name] = scopeInfo
1493 scopeInfo["current_api"] = scope.snapshotRelativeCurrentApiTxtPath(baseModuleName)
1494 scopeInfo["removed_api"] = scope.snapshotRelativeRemovedApiTxtPath(baseModuleName)
1495 if p := scopePaths.latestApiPath; p.Valid() {
1496 scopeInfo["latest_api"] = p.Path().String()
1497 }
1498 if p := scopePaths.latestRemovedApiPath; p.Valid() {
1499 scopeInfo["latest_removed_api"] = p.Path().String()
1500 }
1501 }
Colin Cross40213022023-12-13 15:19:49 -08001502 android.SetProvider(ctx, android.AdditionalSdkInfoProvider, android.AdditionalSdkInfo{additionalSdkInfo})
Jiyong Parkc678ad32018-04-10 13:07:10 +09001503}
1504
Jiyong Park0b0e1b92019-12-03 13:24:29 +09001505func (module *SdkLibrary) AndroidMkEntries() []android.AndroidMkEntries {
Paul Duffindfa131e2020-05-15 20:37:11 +01001506 if !module.requiresRuntimeImplementationLibrary() {
Paul Duffin43db9be2019-12-30 17:35:49 +00001507 return nil
1508 }
Jiyong Park0b0e1b92019-12-03 13:24:29 +09001509 entriesList := module.Library.AndroidMkEntries()
Yo Chiang07d75072020-06-05 17:43:19 +08001510 if module.sharedLibrary() {
1511 entries := &entriesList[0]
1512 entries.Required = append(entries.Required, module.xmlPermissionsModuleName())
1513 }
Jiyong Park0b0e1b92019-12-03 13:24:29 +09001514 return entriesList
Jiyong Park82484c02018-04-23 21:41:26 +09001515}
1516
Anton Hansson5fd5d242020-03-27 19:43:19 +00001517// The dist path of the stub artifacts
1518func (module *SdkLibrary) apiDistPath(apiScope *apiScope) string {
Colin Crossf0eace92021-06-02 13:02:23 -07001519 return path.Join("apistubs", module.distGroup(), apiScope.name)
Anton Hansson5fd5d242020-03-27 19:43:19 +00001520}
1521
Paul Duffin12ceb462019-12-24 20:31:31 +00001522// Get the sdk version for use when compiling the stubs library.
Paul Duffin780c5f42020-05-12 15:52:55 +01001523func (module *SdkLibrary) sdkVersionForStubsLibrary(mctx android.EarlyModuleContext, apiScope *apiScope) string {
Paul Duffin87a05a32020-05-12 11:50:28 +01001524 scopeProperties := module.scopeToProperties[apiScope]
1525 if scopeProperties.Sdk_version != nil {
1526 return proptools.String(scopeProperties.Sdk_version)
1527 }
1528
Jiyong Parkf1691d22021-03-29 20:11:58 +09001529 sdkDep := decodeSdkDep(mctx, android.SdkContext(&module.Library))
Paul Duffin12ceb462019-12-24 20:31:31 +00001530 if sdkDep.hasStandardLibs() {
1531 // If building against a standard sdk then use the sdk version appropriate for the scope.
Paul Duffind1b3a922020-01-22 11:57:20 +00001532 return apiScope.sdkVersion
Paul Duffin12ceb462019-12-24 20:31:31 +00001533 } else {
1534 // Otherwise, use no system module.
1535 return "none"
1536 }
1537}
1538
Paul Duffin31310252020-11-20 21:26:20 +00001539func (module *SdkLibrary) distStem() string {
1540 return proptools.StringDefault(module.sdkLibraryProperties.Dist_stem, module.BaseModuleName())
1541}
1542
Colin Cross986b69a2021-06-01 13:13:40 -07001543// distGroup returns the subdirectory of the dist path of the stub artifacts.
1544func (module *SdkLibrary) distGroup() string {
Colin Cross59b92bf2021-06-01 14:07:56 -07001545 return proptools.StringDefault(module.sdkLibraryProperties.Dist_group, "unknown")
Colin Cross986b69a2021-06-01 13:13:40 -07001546}
1547
Paul Duffin958806b2022-05-16 13:10:47 +00001548func latestPrebuiltApiModuleName(name string, apiScope *apiScope) string {
1549 return PrebuiltApiModuleName(name, apiScope.name, "latest")
1550}
1551
Paul Duffind1b3a922020-01-22 11:57:20 +00001552func (module *SdkLibrary) latestApiFilegroupName(apiScope *apiScope) string {
Paul Duffin958806b2022-05-16 13:10:47 +00001553 return ":" + module.latestApiModuleName(apiScope)
1554}
1555
1556func (module *SdkLibrary) latestApiModuleName(apiScope *apiScope) string {
1557 return latestPrebuiltApiModuleName(module.distStem(), apiScope)
Jiyong Park58c518b2018-05-12 22:29:12 +09001558}
Jiyong Parkc678ad32018-04-10 13:07:10 +09001559
Paul Duffind1b3a922020-01-22 11:57:20 +00001560func (module *SdkLibrary) latestRemovedApiFilegroupName(apiScope *apiScope) string {
Paul Duffin958806b2022-05-16 13:10:47 +00001561 return ":" + module.latestRemovedApiModuleName(apiScope)
1562}
1563
1564func (module *SdkLibrary) latestRemovedApiModuleName(apiScope *apiScope) string {
1565 return latestPrebuiltApiModuleName(module.distStem()+"-removed", apiScope)
Jiyong Parkc678ad32018-04-10 13:07:10 +09001566}
1567
Jaewoong Jung1a97ee02021-03-09 13:25:02 -08001568func (module *SdkLibrary) latestIncompatibilitiesFilegroupName(apiScope *apiScope) string {
Paul Duffin958806b2022-05-16 13:10:47 +00001569 return ":" + module.latestIncompatibilitiesModuleName(apiScope)
1570}
1571
1572func (module *SdkLibrary) latestIncompatibilitiesModuleName(apiScope *apiScope) string {
1573 return latestPrebuiltApiModuleName(module.distStem()+"-incompatibilities", apiScope)
Jaewoong Jung1a97ee02021-03-09 13:25:02 -08001574}
1575
Jihoon Kang1c92c3e2023-03-23 17:44:51 +00001576func (module *SdkLibrary) contributesToApiSurface(c android.Config) bool {
1577 _, exists := c.GetApiLibraries()[module.Name()]
1578 return exists
1579}
1580
Jihoon Kang0c705a42023-08-02 06:44:57 +00001581// The listed modules are the special java_sdk_libraries where apiScope.kind do not match the
1582// api surface that the module contribute to. For example, the public droidstubs and java_library
1583// do not contribute to the public api surface, but contributes to the core platform api surface.
1584// This method returns the full api surface stub lib that
1585// the generated java_api_library should depend on.
1586func (module *SdkLibrary) alternativeFullApiSurfaceStubLib() string {
1587 if val, ok := apiLibraryAdditionalProperties[module.Name()]; ok {
1588 return val.FullApiSurfaceStubLib
1589 }
1590 return ""
1591}
1592
1593// The listed modules' stubs contents do not match the corresponding txt files,
1594// but require additional api contributions to generate the full stubs.
1595// This method returns the name of the additional api contribution module
1596// for corresponding sdk_library modules.
1597func (module *SdkLibrary) apiLibraryAdditionalApiContribution() string {
1598 if val, ok := apiLibraryAdditionalProperties[module.Name()]; ok {
1599 return val.AdditionalApiContribution
1600 }
1601 return ""
1602}
1603
Anton Hansson944e77d2020-08-19 11:40:22 +01001604func childModuleVisibility(childVisibility []string) []string {
1605 if childVisibility == nil {
1606 // No child visibility set. The child will use the visibility of the sdk_library.
1607 return nil
1608 }
1609
1610 // Prepend an override to ignore the sdk_library's visibility, and rely on the child visibility.
1611 var visibility []string
1612 visibility = append(visibility, "//visibility:override")
1613 visibility = append(visibility, childVisibility...)
1614 return visibility
1615}
1616
Paul Duffin5df79302020-05-16 15:52:12 +01001617// Creates the implementation java library
1618func (module *SdkLibrary) createImplLibrary(mctx android.DefaultableHookContext) {
Anton Hansson944e77d2020-08-19 11:40:22 +01001619 visibility := childModuleVisibility(module.sdkLibraryProperties.Impl_library_visibility)
1620
Paul Duffin5df79302020-05-16 15:52:12 +01001621 props := struct {
Paul Duffin77590a82022-04-28 14:13:30 +00001622 Name *string
1623 Visibility []string
1624 Instrument bool
1625 Libs []string
1626 Static_libs []string
1627 Apex_available []string
Paul Duffin5df79302020-05-16 15:52:12 +01001628 }{
1629 Name: proptools.StringPtr(module.implLibraryModuleName()),
Anton Hansson944e77d2020-08-19 11:40:22 +01001630 Visibility: visibility,
Paul Duffin296cf332020-06-18 21:09:55 +01001631 // Set the instrument property to ensure it is instrumented when instrumentation is required.
1632 Instrument: true,
Anton Hansson7f66efa2020-10-08 14:47:23 +01001633 // Set the impl_only libs. Note that the module's "Libs" get appended as well, via the
1634 // addition of &module.properties below.
1635 Libs: module.sdkLibraryProperties.Impl_only_libs,
Paul Duffin77590a82022-04-28 14:13:30 +00001636 // Set the impl_only static libs. Note that the module's "static_libs" get appended as well, via the
1637 // addition of &module.properties below.
1638 Static_libs: module.sdkLibraryProperties.Impl_only_static_libs,
1639 // Pass the apex_available settings down so that the impl library can be statically
1640 // embedded within a library that is added to an APEX. Needed for updatable-media.
1641 Apex_available: module.ApexAvailable(),
Paul Duffin5df79302020-05-16 15:52:12 +01001642 }
1643
1644 properties := []interface{}{
1645 &module.properties,
1646 &module.protoProperties,
1647 &module.deviceProperties,
Liz Kammera7a64f32020-07-09 15:16:41 -07001648 &module.dexProperties,
Paul Duffin5df79302020-05-16 15:52:12 +01001649 &module.dexpreoptProperties,
Colin Cross014489c2020-06-02 20:09:13 -07001650 &module.linter.properties,
Paul Duffin5df79302020-05-16 15:52:12 +01001651 &props,
1652 module.sdkComponentPropertiesForChildLibrary(),
1653 }
1654 mctx.CreateModule(LibraryFactory, properties...)
1655}
1656
Jihoon Kangfa4a90d2023-12-20 02:53:38 +00001657type libraryProperties struct {
1658 Name *string
1659 Visibility []string
1660 Srcs []string
1661 Installable *bool
1662 Sdk_version *string
1663 System_modules *string
1664 Patch_module *string
1665 Libs []string
1666 Static_libs []string
1667 Compile_dex *bool
1668 Java_version *string
1669 Openjdk9 struct {
1670 Srcs []string
1671 Javacflags []string
1672 }
1673 Dist struct {
1674 Targets []string
1675 Dest *string
1676 Dir *string
1677 Tag *string
1678 }
1679}
Jiyong Parkc678ad32018-04-10 13:07:10 +09001680
Jihoon Kangfa4a90d2023-12-20 02:53:38 +00001681func (module *SdkLibrary) stubsLibraryProps(mctx android.DefaultableHookContext, apiScope *apiScope) libraryProperties {
1682 props := libraryProperties{}
Jihoon Kang786df932023-09-07 01:18:31 +00001683 props.Visibility = []string{"//visibility:override", "//visibility:private"}
Jiyong Parkc678ad32018-04-10 13:07:10 +09001684 // sources are generated from the droiddoc
Paul Duffin12ceb462019-12-24 20:31:31 +00001685 sdkVersion := module.sdkVersionForStubsLibrary(mctx, apiScope)
Paul Duffin52d398a2019-06-11 12:31:14 +01001686 props.Sdk_version = proptools.StringPtr(sdkVersion)
Paul Duffina18abc22020-05-16 18:54:24 +01001687 props.System_modules = module.deviceProperties.System_modules
1688 props.Patch_module = module.properties.Patch_module
Paul Duffin367ab912019-12-23 19:40:36 +00001689 props.Installable = proptools.BoolPtr(false)
Sundong Ahn054b19a2018-10-19 13:46:09 +09001690 props.Libs = module.sdkLibraryProperties.Stub_only_libs
Mark White9421c4c2023-08-10 00:07:03 +00001691 props.Libs = append(props.Libs, module.scopeToProperties[apiScope].Libs...)
Anton Hanssondae54cd2021-04-21 16:30:10 +01001692 props.Static_libs = module.sdkLibraryProperties.Stub_only_static_libs
Paul Duffine22c2ab2020-05-20 19:35:27 +01001693 // The stub-annotations library contains special versions of the annotations
1694 // with CLASS retention policy, so that they're kept.
1695 if proptools.Bool(module.sdkLibraryProperties.Annotations_enabled) {
1696 props.Libs = append(props.Libs, "stub-annotations")
1697 }
Paul Duffina18abc22020-05-16 18:54:24 +01001698 props.Openjdk9.Srcs = module.properties.Openjdk9.Srcs
1699 props.Openjdk9.Javacflags = module.properties.Openjdk9.Javacflags
Anton Hansson83509b52020-05-21 09:21:57 +01001700 // We compile the stubs for 1.8 in line with the main android.jar stubs, and potential
1701 // interop with older developer tools that don't support 1.9.
1702 props.Java_version = proptools.StringPtr("1.8")
Paul Duffinf4600f62021-05-13 22:34:45 +01001703
Jihoon Kangfa4a90d2023-12-20 02:53:38 +00001704 return props
1705}
1706
1707// Creates a static java library that has API stubs
1708func (module *SdkLibrary) createStubsLibrary(mctx android.DefaultableHookContext, apiScope *apiScope) {
1709
1710 props := module.stubsLibraryProps(mctx, apiScope)
1711 props.Name = proptools.StringPtr(module.sourceStubsLibraryModuleName(apiScope))
1712 props.Srcs = []string{":" + module.stubsSourceModuleName(apiScope)}
1713
1714 mctx.CreateModule(LibraryFactory, &props, module.sdkComponentPropertiesForChildLibrary())
1715}
1716
1717// Create a static java library that compiles the "exportable" stubs
1718func (module *SdkLibrary) createExportableStubsLibrary(mctx android.DefaultableHookContext, apiScope *apiScope) {
1719 props := module.stubsLibraryProps(mctx, apiScope)
1720 props.Name = proptools.StringPtr(module.exportableSourceStubsLibraryModuleName(apiScope))
1721 props.Srcs = []string{":" + module.stubsSourceModuleName(apiScope) + "{.exportable}"}
1722
Paul Duffin859fe962020-05-15 10:20:31 +01001723 mctx.CreateModule(LibraryFactory, &props, module.sdkComponentPropertiesForChildLibrary())
Jiyong Parkc678ad32018-04-10 13:07:10 +09001724}
1725
Paul Duffin6d0886e2020-04-07 18:49:53 +01001726// Creates a droidstubs module that creates stubs source files from the given full source
Paul Duffinc8782502020-04-29 20:45:27 +01001727// files and also updates and checks the API specification files.
Paul Duffin15f34ef2020-07-20 18:04:44 +01001728func (module *SdkLibrary) createStubsSourcesAndApi(mctx android.DefaultableHookContext, apiScope *apiScope, name string, scopeSpecificDroidstubsArgs []string) {
Jiyong Parkc678ad32018-04-10 13:07:10 +09001729 props := struct {
Sundong Ahn054b19a2018-10-19 13:46:09 +09001730 Name *string
Paul Duffin4911a892020-04-29 23:35:13 +01001731 Visibility []string
Sundong Ahn054b19a2018-10-19 13:46:09 +09001732 Srcs []string
1733 Installable *bool
Paul Duffin52d398a2019-06-11 12:31:14 +01001734 Sdk_version *string
Jihoon Kang3198f3c2023-01-26 08:08:52 +00001735 Api_surface *string
Paul Duffin12ceb462019-12-24 20:31:31 +00001736 System_modules *string
Sundong Ahn054b19a2018-10-19 13:46:09 +09001737 Libs []string
Paul Duffin6877e6d2020-09-25 19:59:14 +01001738 Output_javadoc_comments *bool
Paul Duffin11512472019-02-11 15:55:17 +00001739 Arg_files []string
Sundong Ahn054b19a2018-10-19 13:46:09 +09001740 Args *string
Sundong Ahn054b19a2018-10-19 13:46:09 +09001741 Java_version *string
Paul Duffine22c2ab2020-05-20 19:35:27 +01001742 Annotations_enabled *bool
Sundong Ahn054b19a2018-10-19 13:46:09 +09001743 Merge_annotations_dirs []string
1744 Merge_inclusion_annotations_dirs []string
Paul Duffin0ff08bd2020-04-29 13:30:54 +01001745 Generate_stubs *bool
Anton Hanssone87b03d2020-12-21 15:29:34 +00001746 Previous_api *string
Jihoon Kang6592e872023-12-19 01:13:16 +00001747 Aconfig_declarations []string
Sundong Ahn054b19a2018-10-19 13:46:09 +09001748 Check_api struct {
Anton Hanssone6056152020-12-31 10:37:27 +00001749 Current ApiToCheck
1750 Last_released ApiToCheck
Paul Duffin160fe412020-05-10 19:32:20 +01001751
1752 Api_lint struct {
1753 Enabled *bool
1754 New_since *string
1755 Baseline_file *string
1756 }
Jiyong Park58c518b2018-05-12 22:29:12 +09001757 }
Sundong Ahn1b92c822018-05-29 11:35:17 +09001758 Aidl struct {
1759 Include_dirs []string
1760 Local_include_dirs []string
1761 }
Paul Duffin040e9062020-11-23 17:41:36 +00001762 Dists []android.Dist
Jiyong Parkc678ad32018-04-10 13:07:10 +09001763 }{}
1764
Paul Duffin7b78b4d2020-04-28 14:08:32 +01001765 // The stubs source processing uses the same compile time classpath when extracting the
1766 // API from the implementation library as it does when compiling it. i.e. the same
1767 // * sdk version
1768 // * system_modules
1769 // * libs (static_libs/libs)
Paul Duffin250e6192019-06-07 10:44:37 +01001770
Paul Duffin0ff08bd2020-04-29 13:30:54 +01001771 props.Name = proptools.StringPtr(name)
Anton Hansson944e77d2020-08-19 11:40:22 +01001772 props.Visibility = childModuleVisibility(module.sdkLibraryProperties.Stubs_source_visibility)
Paul Duffina18abc22020-05-16 18:54:24 +01001773 props.Srcs = append(props.Srcs, module.properties.Srcs...)
Anton Hanssonf8ea3722021-09-16 14:24:13 +01001774 props.Srcs = append(props.Srcs, module.sdkLibraryProperties.Api_srcs...)
Paul Duffina18abc22020-05-16 18:54:24 +01001775 props.Sdk_version = module.deviceProperties.Sdk_version
Jihoon Kang3198f3c2023-01-26 08:08:52 +00001776 props.Api_surface = &apiScope.name
Paul Duffina18abc22020-05-16 18:54:24 +01001777 props.System_modules = module.deviceProperties.System_modules
Jiyong Parkc678ad32018-04-10 13:07:10 +09001778 props.Installable = proptools.BoolPtr(false)
Sundong Ahne6f0b052018-06-05 16:46:14 +09001779 // A droiddoc module has only one Libs property and doesn't distinguish between
1780 // shared libs and static libs. So we need to add both of these libs to Libs property.
Paul Duffina18abc22020-05-16 18:54:24 +01001781 props.Libs = module.properties.Libs
1782 props.Libs = append(props.Libs, module.properties.Static_libs...)
Nikita Ioffed732da72022-11-21 12:38:25 +00001783 props.Libs = append(props.Libs, module.sdkLibraryProperties.Stub_only_libs...)
Mark White9421c4c2023-08-10 00:07:03 +00001784 props.Libs = append(props.Libs, module.scopeToProperties[apiScope].Libs...)
Paul Duffina18abc22020-05-16 18:54:24 +01001785 props.Aidl.Include_dirs = module.deviceProperties.Aidl.Include_dirs
1786 props.Aidl.Local_include_dirs = module.deviceProperties.Aidl.Local_include_dirs
1787 props.Java_version = module.properties.Java_version
Jiyong Parkc678ad32018-04-10 13:07:10 +09001788
Paul Duffine22c2ab2020-05-20 19:35:27 +01001789 props.Annotations_enabled = module.sdkLibraryProperties.Annotations_enabled
Sundong Ahn054b19a2018-10-19 13:46:09 +09001790 props.Merge_annotations_dirs = module.sdkLibraryProperties.Merge_annotations_dirs
1791 props.Merge_inclusion_annotations_dirs = module.sdkLibraryProperties.Merge_inclusion_annotations_dirs
Jihoon Kang6592e872023-12-19 01:13:16 +00001792 props.Aconfig_declarations = module.sdkLibraryProperties.Aconfig_declarations
Sundong Ahn054b19a2018-10-19 13:46:09 +09001793
Paul Duffin6d0886e2020-04-07 18:49:53 +01001794 droidstubsArgs := []string{}
Paul Duffin235ffff2019-12-24 10:41:30 +00001795 if len(module.sdkLibraryProperties.Api_packages) != 0 {
Paul Duffin6d0886e2020-04-07 18:49:53 +01001796 droidstubsArgs = append(droidstubsArgs, "--stub-packages "+strings.Join(module.sdkLibraryProperties.Api_packages, ":"))
Paul Duffin235ffff2019-12-24 10:41:30 +00001797 }
1798 if len(module.sdkLibraryProperties.Hidden_api_packages) != 0 {
Paul Duffin6d0886e2020-04-07 18:49:53 +01001799 droidstubsArgs = append(droidstubsArgs,
Paul Duffin235ffff2019-12-24 10:41:30 +00001800 android.JoinWithPrefix(module.sdkLibraryProperties.Hidden_api_packages, " --hide-package "))
1801 }
Paul Duffin6d0886e2020-04-07 18:49:53 +01001802 droidstubsArgs = append(droidstubsArgs, module.sdkLibraryProperties.Droiddoc_options...)
Anton Hanssonfd1c0d22023-11-02 15:18:09 +00001803 disabledWarnings := []string{"HiddenSuperclass"}
1804 if proptools.BoolDefault(module.sdkLibraryProperties.Api_lint.Legacy_errors_allowed, true) {
1805 disabledWarnings = append(disabledWarnings,
1806 "BroadcastBehavior",
1807 "DeprecationMismatch",
1808 "MissingPermission",
1809 "SdkConstant",
1810 "Todo",
1811 )
Paul Duffin235ffff2019-12-24 10:41:30 +00001812 }
Paul Duffin6d0886e2020-04-07 18:49:53 +01001813 droidstubsArgs = append(droidstubsArgs, android.JoinWithPrefix(disabledWarnings, "--hide "))
Sundong Ahnfb2721f2018-09-17 13:23:09 +09001814
Paul Duffin6877e6d2020-09-25 19:59:14 +01001815 // Output Javadoc comments for public scope.
1816 if apiScope == apiScopePublic {
1817 props.Output_javadoc_comments = proptools.BoolPtr(true)
1818 }
1819
Paul Duffin1fb487d2020-04-07 18:50:10 +01001820 // Add in scope specific arguments.
Paul Duffin0ff08bd2020-04-29 13:30:54 +01001821 droidstubsArgs = append(droidstubsArgs, scopeSpecificDroidstubsArgs...)
Paul Duffin11512472019-02-11 15:55:17 +00001822 props.Arg_files = module.sdkLibraryProperties.Droiddoc_option_files
Paul Duffin6d0886e2020-04-07 18:49:53 +01001823 props.Args = proptools.StringPtr(strings.Join(droidstubsArgs, " "))
Jiyong Parkc678ad32018-04-10 13:07:10 +09001824
Paul Duffin15f34ef2020-07-20 18:04:44 +01001825 // List of APIs identified from the provided source files are created. They are later
1826 // compared against to the not-yet-released (a.k.a current) list of APIs and to the
1827 // last-released (a.k.a numbered) list of API.
1828 currentApiFileName := apiScope.apiFilePrefix + "current.txt"
1829 removedApiFileName := apiScope.apiFilePrefix + "removed.txt"
1830 apiDir := module.getApiDir()
1831 currentApiFileName = path.Join(apiDir, currentApiFileName)
1832 removedApiFileName = path.Join(apiDir, removedApiFileName)
Jiyong Parkc678ad32018-04-10 13:07:10 +09001833
Paul Duffin15f34ef2020-07-20 18:04:44 +01001834 // check against the not-yet-release API
1835 props.Check_api.Current.Api_file = proptools.StringPtr(currentApiFileName)
1836 props.Check_api.Current.Removed_api_file = proptools.StringPtr(removedApiFileName)
Jiyong Park58c518b2018-05-12 22:29:12 +09001837
Paul Duffin958806b2022-05-16 13:10:47 +00001838 if module.compareAgainstLatestApi(apiScope) {
Paul Duffin15f34ef2020-07-20 18:04:44 +01001839 // check against the latest released API
1840 latestApiFilegroupName := proptools.StringPtr(module.latestApiFilegroupName(apiScope))
Anton Hanssone87b03d2020-12-21 15:29:34 +00001841 props.Previous_api = latestApiFilegroupName
Paul Duffin15f34ef2020-07-20 18:04:44 +01001842 props.Check_api.Last_released.Api_file = latestApiFilegroupName
1843 props.Check_api.Last_released.Removed_api_file = proptools.StringPtr(
1844 module.latestRemovedApiFilegroupName(apiScope))
Jaewoong Jung1a97ee02021-03-09 13:25:02 -08001845 props.Check_api.Last_released.Baseline_file = proptools.StringPtr(
1846 module.latestIncompatibilitiesFilegroupName(apiScope))
Paul Duffin160fe412020-05-10 19:32:20 +01001847
Paul Duffin15f34ef2020-07-20 18:04:44 +01001848 if proptools.Bool(module.sdkLibraryProperties.Api_lint.Enabled) {
1849 // Enable api lint.
1850 props.Check_api.Api_lint.Enabled = proptools.BoolPtr(true)
1851 props.Check_api.Api_lint.New_since = latestApiFilegroupName
Paul Duffin160fe412020-05-10 19:32:20 +01001852
Paul Duffin15f34ef2020-07-20 18:04:44 +01001853 // If it exists then pass a lint-baseline.txt through to droidstubs.
1854 baselinePath := path.Join(apiDir, apiScope.apiFilePrefix+"lint-baseline.txt")
1855 baselinePathRelativeToRoot := path.Join(mctx.ModuleDir(), baselinePath)
1856 paths, err := mctx.GlobWithDeps(baselinePathRelativeToRoot, nil)
1857 if err != nil {
1858 mctx.ModuleErrorf("error checking for presence of %s: %s", baselinePathRelativeToRoot, err)
1859 }
1860 if len(paths) == 1 {
1861 props.Check_api.Api_lint.Baseline_file = proptools.StringPtr(baselinePath)
1862 } else if len(paths) != 0 {
1863 mctx.ModuleErrorf("error checking for presence of %s: expected one path, found: %v", baselinePathRelativeToRoot, paths)
Paul Duffin160fe412020-05-10 19:32:20 +01001864 }
1865 }
Paul Duffin15f34ef2020-07-20 18:04:44 +01001866 }
Jiyong Park58c518b2018-05-12 22:29:12 +09001867
Paul Duffin15f34ef2020-07-20 18:04:44 +01001868 if !Bool(module.sdkLibraryProperties.No_dist) {
Paul Duffin040e9062020-11-23 17:41:36 +00001869 // Dist the api txt and removed api txt artifacts for sdk builds.
1870 distDir := proptools.StringPtr(path.Join(module.apiDistPath(apiScope), "api"))
1871 for _, p := range []struct {
1872 tag string
1873 pattern string
1874 }{
1875 {tag: ".api.txt", pattern: "%s.txt"},
1876 {tag: ".removed-api.txt", pattern: "%s-removed.txt"},
1877 } {
1878 props.Dists = append(props.Dists, android.Dist{
1879 Targets: []string{"sdk", "win_sdk"},
1880 Dir: distDir,
1881 Dest: proptools.StringPtr(fmt.Sprintf(p.pattern, module.distStem())),
1882 Tag: proptools.StringPtr(p.tag),
1883 })
1884 }
Anton Hansson5fd5d242020-03-27 19:43:19 +00001885 }
1886
Spandan Das2cc80ba2023-10-27 17:21:52 +00001887 mctx.CreateModule(DroidstubsFactory, &props, module.sdkComponentPropertiesForChildLibrary()).(*Droidstubs).CallHookIfAvailable(mctx)
Jiyong Parkc678ad32018-04-10 13:07:10 +09001888}
1889
Jihoon Kang0c705a42023-08-02 06:44:57 +00001890func (module *SdkLibrary) createApiLibrary(mctx android.DefaultableHookContext, apiScope *apiScope, alternativeFullApiSurfaceStub string) {
Jihoon Kang1c92c3e2023-03-23 17:44:51 +00001891 props := struct {
Jihoon Kangca198c22023-06-22 23:13:51 +00001892 Name *string
1893 Visibility []string
1894 Api_contributions []string
1895 Libs []string
1896 Static_libs []string
1897 Full_api_surface_stub *string
Jihoon Kang4ec24872023-10-05 17:26:09 +00001898 System_modules *string
Jihoon Kang063ec002023-06-28 01:16:23 +00001899 Enable_validation *bool
Jihoon Kang1c92c3e2023-03-23 17:44:51 +00001900 }{}
1901
1902 props.Name = proptools.StringPtr(module.apiLibraryModuleName(apiScope))
Jihoon Kang786df932023-09-07 01:18:31 +00001903 props.Visibility = []string{"//visibility:override", "//visibility:private"}
Jihoon Kang1c92c3e2023-03-23 17:44:51 +00001904
1905 apiContributions := []string{}
1906
1907 // Api surfaces are not independent of each other, but have subset relationships,
1908 // and so does the api files. To generate from-text stubs for api surfaces other than public,
1909 // all subset api domains' api_contriubtions must be added as well.
1910 scope := apiScope
1911 for scope != nil {
1912 apiContributions = append(apiContributions, module.stubsSourceModuleName(scope)+".api.contribution")
1913 scope = scope.extends
1914 }
Jihoon Kang0c705a42023-08-02 06:44:57 +00001915 if apiScope == apiScopePublic {
1916 additionalApiContribution := module.apiLibraryAdditionalApiContribution()
1917 if additionalApiContribution != "" {
1918 apiContributions = append(apiContributions, additionalApiContribution)
1919 }
1920 }
Jihoon Kang1c92c3e2023-03-23 17:44:51 +00001921
1922 props.Api_contributions = apiContributions
1923 props.Libs = module.properties.Libs
1924 props.Libs = append(props.Libs, module.sdkLibraryProperties.Stub_only_libs...)
Mark White9421c4c2023-08-10 00:07:03 +00001925 props.Libs = append(props.Libs, module.scopeToProperties[apiScope].Libs...)
Jihoon Kang1c92c3e2023-03-23 17:44:51 +00001926 props.Libs = append(props.Libs, "stub-annotations")
1927 props.Static_libs = module.sdkLibraryProperties.Stub_only_static_libs
Jihoon Kange7ee2562023-07-25 05:51:46 +00001928 props.Full_api_surface_stub = proptools.StringPtr(apiScope.kind.DefaultJavaLibraryName())
Jihoon Kang0c705a42023-08-02 06:44:57 +00001929 if alternativeFullApiSurfaceStub != "" {
1930 props.Full_api_surface_stub = proptools.StringPtr(alternativeFullApiSurfaceStub)
1931 }
Jihoon Kang1c92c3e2023-03-23 17:44:51 +00001932
1933 // android_module_lib_stubs_current.from-text only comprises api contributions from art, conscrypt and i18n.
1934 // Thus, replace with android_module_lib_stubs_current_full.from-text, which comprises every api domains.
1935 if apiScope.kind == android.SdkModule {
Jihoon Kangca198c22023-06-22 23:13:51 +00001936 props.Full_api_surface_stub = proptools.StringPtr(apiScope.kind.DefaultJavaLibraryName() + "_full.from-text")
Jihoon Kang1c92c3e2023-03-23 17:44:51 +00001937 }
1938
Jihoon Kangd30ac8a2023-10-09 18:00:17 +00001939 // java_sdk_library modules that set sdk_version as none does not depend on other api
1940 // domains. Therefore, java_api_library created from such modules should not depend on
1941 // full_api_surface_stubs but create and compile stubs by the java_api_library module
1942 // itself.
1943 if module.SdkVersion(mctx).Kind == android.SdkNone {
1944 props.Full_api_surface_stub = nil
1945 }
1946
Jihoon Kang4ec24872023-10-05 17:26:09 +00001947 props.System_modules = module.deviceProperties.System_modules
Jihoon Kang063ec002023-06-28 01:16:23 +00001948 props.Enable_validation = proptools.BoolPtr(true)
Jihoon Kang4ec24872023-10-05 17:26:09 +00001949
Spandan Das2cc80ba2023-10-27 17:21:52 +00001950 mctx.CreateModule(ApiLibraryFactory, &props, module.sdkComponentPropertiesForChildLibrary())
Jihoon Kang1c92c3e2023-03-23 17:44:51 +00001951}
1952
Jihoon Kangfa4a90d2023-12-20 02:53:38 +00001953func (module *SdkLibrary) topLevelStubsLibraryProps(mctx android.DefaultableHookContext, apiScope *apiScope) libraryProperties {
1954 props := libraryProperties{}
1955
Jihoon Kang1147b312023-06-08 23:25:57 +00001956 props.Visibility = childModuleVisibility(module.sdkLibraryProperties.Stubs_library_visibility)
1957 sdkVersion := module.sdkVersionForStubsLibrary(mctx, apiScope)
1958 props.Sdk_version = proptools.StringPtr(sdkVersion)
1959
Jihoon Kang1147b312023-06-08 23:25:57 +00001960 props.System_modules = module.deviceProperties.System_modules
1961
Jihoon Kang1147b312023-06-08 23:25:57 +00001962 // The imports need to be compiled to dex if the java_sdk_library requests it.
1963 compileDex := module.dexProperties.Compile_dex
1964 if module.stubLibrariesCompiledForDex() {
1965 compileDex = proptools.BoolPtr(true)
1966 }
1967 props.Compile_dex = compileDex
1968
Jihoon Kangfa4a90d2023-12-20 02:53:38 +00001969 return props
1970}
1971
1972func (module *SdkLibrary) createTopLevelStubsLibrary(
1973 mctx android.DefaultableHookContext, apiScope *apiScope, contributesToApiSurface bool) {
1974
1975 props := module.topLevelStubsLibraryProps(mctx, apiScope)
1976 props.Name = proptools.StringPtr(module.stubsLibraryModuleName(apiScope))
1977
1978 // Add the stub compiling java_library/java_api_library as static lib based on build config
1979 staticLib := module.sourceStubsLibraryModuleName(apiScope)
1980 if mctx.Config().BuildFromTextStub() && contributesToApiSurface {
1981 staticLib = module.apiLibraryModuleName(apiScope)
1982 }
1983 props.Static_libs = append(props.Static_libs, staticLib)
1984
1985 mctx.CreateModule(LibraryFactory, &props, module.sdkComponentPropertiesForChildLibrary())
1986}
1987
1988func (module *SdkLibrary) createTopLevelExportableStubsLibrary(
1989 mctx android.DefaultableHookContext, apiScope *apiScope) {
1990
1991 props := module.topLevelStubsLibraryProps(mctx, apiScope)
1992 props.Name = proptools.StringPtr(module.exportableStubsLibraryModuleName(apiScope))
1993
1994 // Dist the class jar artifact for sdk builds.
1995 // "exportable" stubs are copied to dist for sdk builds instead of the "everything" stubs.
1996 if !Bool(module.sdkLibraryProperties.No_dist) {
1997 props.Dist.Targets = []string{"sdk", "win_sdk"}
1998 props.Dist.Dest = proptools.StringPtr(fmt.Sprintf("%v.jar", module.distStem()))
1999 props.Dist.Dir = proptools.StringPtr(module.apiDistPath(apiScope))
2000 props.Dist.Tag = proptools.StringPtr(".jar")
2001 }
2002
2003 staticLib := module.exportableSourceStubsLibraryModuleName(apiScope)
2004 props.Static_libs = append(props.Static_libs, staticLib)
2005
Jihoon Kang1147b312023-06-08 23:25:57 +00002006 mctx.CreateModule(LibraryFactory, &props, module.sdkComponentPropertiesForChildLibrary())
2007}
2008
Paul Duffin958806b2022-05-16 13:10:47 +00002009func (module *SdkLibrary) compareAgainstLatestApi(apiScope *apiScope) bool {
2010 return !(apiScope.unstable || module.sdkLibraryProperties.Unsafe_ignore_missing_latest_api)
2011}
2012
Paul Duffinea8f8082021-06-24 13:25:57 +01002013// Implements android.ApexModule
Jooyung Han5e9013b2020-03-10 06:23:13 +09002014func (module *SdkLibrary) DepIsInSameApex(mctx android.BaseModuleContext, dep android.Module) bool {
2015 depTag := mctx.OtherModuleDependencyTag(dep)
2016 if depTag == xmlPermissionsFileTag {
2017 return true
2018 }
2019 return module.Library.DepIsInSameApex(mctx, dep)
2020}
2021
Paul Duffinea8f8082021-06-24 13:25:57 +01002022// Implements android.ApexModule
2023func (module *SdkLibrary) UniqueApexVariations() bool {
2024 return module.uniqueApexVariations()
2025}
2026
Jihoon Kang80456fd2023-11-15 19:22:14 +00002027func (module *SdkLibrary) ContributeToApi() bool {
2028 return proptools.BoolDefault(module.sdkLibraryProperties.Contribute_to_android_api, false)
2029}
2030
Jiyong Parkc678ad32018-04-10 13:07:10 +09002031// Creates the xml file that publicizes the runtime library
Paul Duffinf0229202020-04-29 16:47:28 +01002032func (module *SdkLibrary) createXmlFile(mctx android.DefaultableHookContext) {
Spandan Das8c9ae7e2023-03-03 21:20:36 +00002033 moduleMinApiLevel := module.Library.MinSdkVersion(mctx)
Pedro Loureiroc3621422021-09-28 15:40:23 +00002034 var moduleMinApiLevelStr = moduleMinApiLevel.String()
2035 if moduleMinApiLevel == android.NoneApiLevel {
2036 moduleMinApiLevelStr = "current"
2037 }
Jiyong Parke3833882020-02-17 17:28:10 +09002038 props := struct {
Pedro Loureiroc3621422021-09-28 15:40:23 +00002039 Name *string
2040 Lib_name *string
2041 Apex_available []string
2042 On_bootclasspath_since *string
2043 On_bootclasspath_before *string
2044 Min_device_sdk *string
2045 Max_device_sdk *string
2046 Sdk_library_min_api_level *string
Jamie Garsidee570ace2023-11-27 12:07:36 +00002047 Uses_libs_dependencies []string
Jiyong Parke3833882020-02-17 17:28:10 +09002048 }{
Pedro Loureiroc3621422021-09-28 15:40:23 +00002049 Name: proptools.StringPtr(module.xmlPermissionsModuleName()),
2050 Lib_name: proptools.StringPtr(module.BaseModuleName()),
2051 Apex_available: module.ApexProperties.Apex_available,
2052 On_bootclasspath_since: module.commonSdkLibraryProperties.On_bootclasspath_since,
2053 On_bootclasspath_before: module.commonSdkLibraryProperties.On_bootclasspath_before,
2054 Min_device_sdk: module.commonSdkLibraryProperties.Min_device_sdk,
2055 Max_device_sdk: module.commonSdkLibraryProperties.Max_device_sdk,
2056 Sdk_library_min_api_level: &moduleMinApiLevelStr,
Jamie Garsidee570ace2023-11-27 12:07:36 +00002057 Uses_libs_dependencies: module.usesLibraryProperties.Uses_libs,
Jiyong Parkc678ad32018-04-10 13:07:10 +09002058 }
Jiyong Parke3833882020-02-17 17:28:10 +09002059
Jiyong Parke3833882020-02-17 17:28:10 +09002060 mctx.CreateModule(sdkLibraryXmlFactory, &props)
Jiyong Parkc678ad32018-04-10 13:07:10 +09002061}
2062
Jiyong Parkf1691d22021-03-29 20:11:58 +09002063func PrebuiltJars(ctx android.BaseModuleContext, baseName string, s android.SdkSpec) android.Paths {
Jiyong Park54105c42021-03-31 18:17:53 +09002064 var ver android.ApiLevel
Jiyong Parkf1691d22021-03-29 20:11:58 +09002065 var kind android.SdkKind
2066 if s.UsePrebuilt(ctx) {
Jiyong Park54105c42021-03-31 18:17:53 +09002067 ver = s.ApiLevel
Jiyong Parkf1691d22021-03-29 20:11:58 +09002068 kind = s.Kind
Jiyong Parkc678ad32018-04-10 13:07:10 +09002069 } else {
Jiyong Park6a927c42020-01-21 02:03:43 +09002070 // We don't have prebuilt SDK for the specific sdkVersion.
2071 // Instead of breaking the build, fallback to use "system_current"
Jiyong Park54105c42021-03-31 18:17:53 +09002072 ver = android.FutureApiLevel
Jiyong Parkf1691d22021-03-29 20:11:58 +09002073 kind = android.SdkSystem
Sundong Ahn054b19a2018-10-19 13:46:09 +09002074 }
Jiyong Park6a927c42020-01-21 02:03:43 +09002075
2076 dir := filepath.Join("prebuilts", "sdk", ver.String(), kind.String())
Paul Duffin50061512020-01-21 16:31:05 +00002077 jar := filepath.Join(dir, baseName+".jar")
Sundong Ahn054b19a2018-10-19 13:46:09 +09002078 jarPath := android.ExistentPathForSource(ctx, jar)
Sundong Ahnae418ac2019-02-28 15:01:28 +09002079 if !jarPath.Valid() {
Colin Cross07c88562020-01-07 09:34:44 -08002080 if ctx.Config().AllowMissingDependencies() {
2081 return android.Paths{android.PathForSource(ctx, jar)}
2082 } else {
Jiyong Parkf1691d22021-03-29 20:11:58 +09002083 ctx.PropertyErrorf("sdk_library", "invalid sdk version %q, %q does not exist", s.Raw, jar)
Colin Cross07c88562020-01-07 09:34:44 -08002084 }
Sundong Ahnae418ac2019-02-28 15:01:28 +09002085 return nil
2086 }
Sundong Ahn054b19a2018-10-19 13:46:09 +09002087 return android.Paths{jarPath.Path()}
2088}
2089
Colin Crossaede88c2020-08-11 12:17:01 -07002090// 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 +01002091//
2092// If either this or the other module are on the platform then this will return
2093// false.
Colin Cross56a83212020-09-15 18:30:11 -07002094func withinSameApexesAs(ctx android.BaseModuleContext, other android.Module) bool {
Colin Crossff694a82023-12-13 15:54:49 -08002095 apexInfo, _ := android.ModuleProvider(ctx, android.ApexInfoProvider)
Colin Cross313aa542023-12-13 13:47:44 -08002096 otherApexInfo, _ := android.OtherModuleProvider(ctx, other, android.ApexInfoProvider)
Jiyong Parkab50b072021-05-12 17:13:56 +09002097 return len(otherApexInfo.InApexVariants) > 0 && reflect.DeepEqual(apexInfo.InApexVariants, otherApexInfo.InApexVariants)
Paul Duffin9b879592020-05-26 13:21:35 +01002098}
2099
Jiyong Parkf1691d22021-03-29 20:11:58 +09002100func (module *SdkLibrary) sdkJars(ctx android.BaseModuleContext, sdkVersion android.SdkSpec, headerJars bool) android.Paths {
Jiyong Park932cdfe2020-05-28 00:19:53 +09002101 // If the client doesn't set sdk_version, but if this library prefers stubs over
2102 // the impl library, let's provide the widest API surface possible. To do so,
2103 // force override sdk_version to module_current so that the closest possible API
2104 // surface could be found in selectHeaderJarsForSdkVersion
Jiyong Parkf1691d22021-03-29 20:11:58 +09002105 if module.defaultsToStubs() && !sdkVersion.Specified() {
Jiyong Park92315372021-04-02 08:45:46 +09002106 sdkVersion = android.SdkSpecFrom(ctx, "module_current")
Jiyong Park932cdfe2020-05-28 00:19:53 +09002107 }
Paul Duffind1b3a922020-01-22 11:57:20 +00002108
Paul Duffindaaa3322020-05-26 18:13:57 +01002109 // Only provide access to the implementation library if it is actually built.
2110 if module.requiresRuntimeImplementationLibrary() {
2111 // Check any special cases for java_sdk_library.
2112 //
2113 // Only allow access to the implementation library in the following condition:
2114 // * No sdk_version specified on the referencing module.
Paul Duffin9b879592020-05-26 13:21:35 +01002115 // * The referencing module is in the same apex as this.
Jiyong Parkf1691d22021-03-29 20:11:58 +09002116 if sdkVersion.Kind == android.SdkPrivate || withinSameApexesAs(ctx, module) {
Paul Duffindaaa3322020-05-26 18:13:57 +01002117 if headerJars {
2118 return module.HeaderJars()
2119 } else {
2120 return module.ImplementationJars()
2121 }
Sundong Ahn054b19a2018-10-19 13:46:09 +09002122 }
Jiyong Parkc678ad32018-04-10 13:07:10 +09002123 }
Paul Duffinb05d4292020-05-20 12:19:10 +01002124
Paul Duffin23970f42020-05-20 14:20:02 +01002125 return module.selectHeaderJarsForSdkVersion(ctx, sdkVersion)
Jiyong Parkc678ad32018-04-10 13:07:10 +09002126}
2127
Sundong Ahn241cd372018-07-13 16:16:44 +09002128// to satisfy SdkLibraryDependency interface
Jiyong Parkf1691d22021-03-29 20:11:58 +09002129func (module *SdkLibrary) SdkHeaderJars(ctx android.BaseModuleContext, sdkVersion android.SdkSpec) android.Paths {
Paul Duffind1b3a922020-01-22 11:57:20 +00002130 return module.sdkJars(ctx, sdkVersion, true /*headerJars*/)
2131}
2132
2133// to satisfy SdkLibraryDependency interface
Jiyong Parkf1691d22021-03-29 20:11:58 +09002134func (module *SdkLibrary) SdkImplementationJars(ctx android.BaseModuleContext, sdkVersion android.SdkSpec) android.Paths {
Paul Duffind1b3a922020-01-22 11:57:20 +00002135 return module.sdkJars(ctx, sdkVersion, false /*headerJars*/)
Sundong Ahn241cd372018-07-13 16:16:44 +09002136}
2137
Colin Cross571cccf2019-02-04 11:22:08 -08002138var javaSdkLibrariesKey = android.NewOnceKey("javaSdkLibraries")
2139
Jiyong Park82484c02018-04-23 21:41:26 +09002140func javaSdkLibraries(config android.Config) *[]string {
Colin Cross571cccf2019-02-04 11:22:08 -08002141 return config.Once(javaSdkLibrariesKey, func() interface{} {
Jiyong Park82484c02018-04-23 21:41:26 +09002142 return &[]string{}
2143 }).(*[]string)
2144}
2145
Paul Duffin749f98f2019-12-30 17:23:46 +00002146func (module *SdkLibrary) getApiDir() string {
2147 return proptools.StringDefault(module.sdkLibraryProperties.Api_dir, "api")
2148}
2149
Jiyong Parkc678ad32018-04-10 13:07:10 +09002150// For a java_sdk_library module, create internal modules for stubs, docs,
2151// runtime libs and xml file. If requested, the stubs and docs are created twice
2152// once for public API level and once for system API level
Paul Duffinf0229202020-04-29 16:47:28 +01002153func (module *SdkLibrary) CreateInternalModules(mctx android.DefaultableHookContext) {
2154 // If the module has been disabled then don't create any child modules.
2155 if !module.Enabled() {
2156 return
2157 }
2158
Paul Duffina18abc22020-05-16 18:54:24 +01002159 if len(module.properties.Srcs) == 0 {
Inseob Kimc0907f12019-02-08 21:00:45 +09002160 mctx.PropertyErrorf("srcs", "java_sdk_library must specify srcs")
Jooyung Han58f26ab2019-12-18 15:34:32 +09002161 return
Inseob Kimc0907f12019-02-08 21:00:45 +09002162 }
2163
Paul Duffin37e0b772019-12-30 17:20:10 +00002164 // If this builds against standard libraries (i.e. is not part of the core libraries)
Paul Duffin4f5c1ef2020-11-19 14:53:43 +00002165 // then assume it provides both system and test apis.
Jiyong Parkf1691d22021-03-29 20:11:58 +09002166 sdkDep := decodeSdkDep(mctx, android.SdkContext(&module.Library))
Paul Duffin37e0b772019-12-30 17:20:10 +00002167 hasSystemAndTestApis := sdkDep.hasStandardLibs()
Paul Duffin3375e352020-04-28 10:44:03 +01002168 module.sdkLibraryProperties.Generate_system_and_test_apis = hasSystemAndTestApis
Paul Duffin4f5c1ef2020-11-19 14:53:43 +00002169
Jaewoong Jung18aefc12020-12-21 09:11:10 -08002170 missingCurrentApi := false
Inseob Kim8098faa2019-03-18 10:19:51 +09002171
Paul Duffin3375e352020-04-28 10:44:03 +01002172 generatedScopes := module.getGeneratedApiScopes(mctx)
Paul Duffind1b3a922020-01-22 11:57:20 +00002173
Paul Duffin749f98f2019-12-30 17:23:46 +00002174 apiDir := module.getApiDir()
Paul Duffin3375e352020-04-28 10:44:03 +01002175 for _, scope := range generatedScopes {
Inseob Kim8098faa2019-03-18 10:19:51 +09002176 for _, api := range []string{"current.txt", "removed.txt"} {
Paul Duffind1b3a922020-01-22 11:57:20 +00002177 path := path.Join(mctx.ModuleDir(), apiDir, scope.apiFilePrefix+api)
Inseob Kim8098faa2019-03-18 10:19:51 +09002178 p := android.ExistentPathForSource(mctx, path)
2179 if !p.Valid() {
Colin Cross18f840c2021-05-20 17:56:54 -07002180 if mctx.Config().AllowMissingDependencies() {
2181 mctx.AddMissingDependencies([]string{path})
2182 } else {
2183 mctx.ModuleErrorf("Current api file %#v doesn't exist", path)
2184 missingCurrentApi = true
2185 }
Inseob Kim8098faa2019-03-18 10:19:51 +09002186 }
2187 }
2188 }
2189
Jaewoong Jung18aefc12020-12-21 09:11:10 -08002190 if missingCurrentApi {
Inseob Kim8098faa2019-03-18 10:19:51 +09002191 script := "build/soong/scripts/gen-java-current-api-files.sh"
2192 p := android.ExistentPathForSource(mctx, script)
2193
2194 if !p.Valid() {
2195 panic(fmt.Sprintf("script file %s doesn't exist", script))
2196 }
2197
2198 mctx.ModuleErrorf("One or more current api files are missing. "+
2199 "You can update them by:\n"+
Paul Duffin37e0b772019-12-30 17:20:10 +00002200 "%s %q %s && m update-api",
Paul Duffind1b3a922020-01-22 11:57:20 +00002201 script, filepath.Join(mctx.ModuleDir(), apiDir),
Paul Duffin3375e352020-04-28 10:44:03 +01002202 strings.Join(generatedScopes.Strings(func(s *apiScope) string { return s.apiFilePrefix }), " "))
Inseob Kim8098faa2019-03-18 10:19:51 +09002203 return
2204 }
2205
Paul Duffin3375e352020-04-28 10:44:03 +01002206 for _, scope := range generatedScopes {
Paul Duffin15f34ef2020-07-20 18:04:44 +01002207 // Use the stubs source name for legacy reasons.
2208 module.createStubsSourcesAndApi(mctx, scope, module.stubsSourceModuleName(scope), scope.droidstubsArgs)
Paul Duffin0ff08bd2020-04-29 13:30:54 +01002209
Paul Duffind1b3a922020-01-22 11:57:20 +00002210 module.createStubsLibrary(mctx, scope)
Jihoon Kangfa4a90d2023-12-20 02:53:38 +00002211 module.createExportableStubsLibrary(mctx, scope)
Jihoon Kang1c92c3e2023-03-23 17:44:51 +00002212
Jihoon Kang0c705a42023-08-02 06:44:57 +00002213 alternativeFullApiSurfaceStubLib := ""
2214 if scope == apiScopePublic {
2215 alternativeFullApiSurfaceStubLib = module.alternativeFullApiSurfaceStubLib()
2216 }
2217 contributesToApiSurface := module.contributesToApiSurface(mctx.Config()) || alternativeFullApiSurfaceStubLib != ""
Jihoon Kang1147b312023-06-08 23:25:57 +00002218 if contributesToApiSurface {
Jihoon Kang0c705a42023-08-02 06:44:57 +00002219 module.createApiLibrary(mctx, scope, alternativeFullApiSurfaceStubLib)
Jihoon Kang1c92c3e2023-03-23 17:44:51 +00002220 }
Jihoon Kang1147b312023-06-08 23:25:57 +00002221
2222 module.createTopLevelStubsLibrary(mctx, scope, contributesToApiSurface)
Jihoon Kangfa4a90d2023-12-20 02:53:38 +00002223 module.createTopLevelExportableStubsLibrary(mctx, scope)
Inseob Kimc0907f12019-02-08 21:00:45 +09002224 }
2225
Paul Duffindfa131e2020-05-15 20:37:11 +01002226 if module.requiresRuntimeImplementationLibrary() {
Paul Duffin5df79302020-05-16 15:52:12 +01002227 // Create child module to create an implementation library.
2228 //
2229 // This temporarily creates a second implementation library that can be explicitly
2230 // referenced.
2231 //
2232 // TODO(b/156618935) - update comment once only one implementation library is created.
2233 module.createImplLibrary(mctx)
2234
Paul Duffindfa131e2020-05-15 20:37:11 +01002235 // Only create an XML permissions file that declares the library as being usable
2236 // as a shared library if required.
2237 if module.sharedLibrary() {
2238 module.createXmlFile(mctx)
2239 }
Paul Duffin43db9be2019-12-30 17:35:49 +00002240
2241 // record java_sdk_library modules so that they are exported to make
2242 javaSdkLibraries := javaSdkLibraries(mctx.Config())
2243 javaSdkLibrariesLock.Lock()
2244 defer javaSdkLibrariesLock.Unlock()
2245 *javaSdkLibraries = append(*javaSdkLibraries, module.BaseModuleName())
2246 }
Anton Hansson7f66efa2020-10-08 14:47:23 +01002247
Paul Duffin77590a82022-04-28 14:13:30 +00002248 // 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 +01002249 module.properties.Libs = append(module.properties.Libs, module.sdkLibraryProperties.Impl_only_libs...)
Paul Duffin77590a82022-04-28 14:13:30 +00002250 module.properties.Static_libs = append(module.properties.Static_libs, module.sdkLibraryProperties.Impl_only_static_libs...)
Inseob Kimc0907f12019-02-08 21:00:45 +09002251}
2252
2253func (module *SdkLibrary) InitSdkLibraryProperties() {
Colin Crossce6734e2020-06-15 16:09:53 -07002254 module.addHostAndDeviceProperties()
2255 module.AddProperties(&module.sdkLibraryProperties)
Sundong Ahn054b19a2018-10-19 13:46:09 +09002256
Paul Duffin71b33cc2021-06-23 11:39:47 +01002257 module.initSdkLibraryComponent(module)
Paul Duffin859fe962020-05-15 10:20:31 +01002258
Paul Duffina18abc22020-05-16 18:54:24 +01002259 module.properties.Installable = proptools.BoolPtr(true)
2260 module.deviceProperties.IsSDKLibrary = true
Inseob Kimc0907f12019-02-08 21:00:45 +09002261}
Sundong Ahn054b19a2018-10-19 13:46:09 +09002262
Paul Duffindfa131e2020-05-15 20:37:11 +01002263func (module *SdkLibrary) requiresRuntimeImplementationLibrary() bool {
2264 return !proptools.Bool(module.sdkLibraryProperties.Api_only)
2265}
2266
Jiyong Park932cdfe2020-05-28 00:19:53 +09002267func (module *SdkLibrary) defaultsToStubs() bool {
2268 return proptools.Bool(module.sdkLibraryProperties.Default_to_stubs)
2269}
2270
Paul Duffin1b1e8062020-05-08 13:44:43 +01002271// Defines how to name the individual component modules the sdk library creates.
2272type sdkLibraryComponentNamingScheme interface {
2273 stubsLibraryModuleName(scope *apiScope, baseName string) string
2274
2275 stubsSourceModuleName(scope *apiScope, baseName string) string
Jihoon Kang1c92c3e2023-03-23 17:44:51 +00002276
2277 apiLibraryModuleName(scope *apiScope, baseName string) string
Jihoon Kang1147b312023-06-08 23:25:57 +00002278
Jihoon Kangfa4a90d2023-12-20 02:53:38 +00002279 sourceStubsLibraryModuleName(scope *apiScope, baseName string) string
2280
2281 exportableStubsLibraryModuleName(scope *apiScope, baseName string) string
2282
2283 exportableSourceStubsLibraryModuleName(scope *apiScope, baseName string) string
Paul Duffin1b1e8062020-05-08 13:44:43 +01002284}
2285
2286type defaultNamingScheme struct {
2287}
2288
2289func (s *defaultNamingScheme) stubsLibraryModuleName(scope *apiScope, baseName string) string {
2290 return scope.stubsLibraryModuleName(baseName)
2291}
2292
2293func (s *defaultNamingScheme) stubsSourceModuleName(scope *apiScope, baseName string) string {
2294 return scope.stubsSourceModuleName(baseName)
2295}
2296
Jihoon Kang1c92c3e2023-03-23 17:44:51 +00002297func (s *defaultNamingScheme) apiLibraryModuleName(scope *apiScope, baseName string) string {
2298 return scope.apiLibraryModuleName(baseName)
2299}
2300
Jihoon Kangfa4a90d2023-12-20 02:53:38 +00002301func (s *defaultNamingScheme) sourceStubsLibraryModuleName(scope *apiScope, baseName string) string {
Jihoon Kang1147b312023-06-08 23:25:57 +00002302 return scope.sourceStubLibraryModuleName(baseName)
2303}
2304
Jihoon Kangfa4a90d2023-12-20 02:53:38 +00002305func (s *defaultNamingScheme) exportableStubsLibraryModuleName(scope *apiScope, baseName string) string {
2306 return scope.exportableStubsLibraryModuleName(baseName)
2307}
2308
2309func (s *defaultNamingScheme) exportableSourceStubsLibraryModuleName(scope *apiScope, baseName string) string {
2310 return scope.exportableSourceStubsLibraryModuleName(baseName)
2311}
2312
Paul Duffin1b1e8062020-05-08 13:44:43 +01002313var _ sdkLibraryComponentNamingScheme = (*defaultNamingScheme)(nil)
2314
Jihoon Kangfa4a90d2023-12-20 02:53:38 +00002315func hasStubsLibrarySuffix(name string, apiScope *apiScope) bool {
2316 return strings.HasSuffix(name, apiScope.stubsLibraryModuleNameSuffix()) ||
2317 strings.HasSuffix(name, apiScope.exportableStubsLibraryModuleNameSuffix())
2318}
2319
Jaewoong Jungbc15e3a2021-03-10 17:02:43 -08002320func moduleStubLinkType(name string) (stub bool, ret sdkLinkType) {
Jihoon Kang1147b312023-06-08 23:25:57 +00002321 name = strings.TrimSuffix(name, ".from-source")
2322
Anton Hansson2d0c1942020-05-25 12:20:51 +01002323 // This suffix-based approach is fragile and could potentially mis-trigger.
2324 // TODO(b/155164730): Clean this up when modules no longer reference sdk_lib stubs directly.
Jihoon Kangfa4a90d2023-12-20 02:53:38 +00002325 if hasStubsLibrarySuffix(name, apiScopePublic) {
Anton Hansson08f476b2021-04-07 15:32:19 +01002326 if name == "hwbinder.stubs" || name == "libcore_private.stubs" {
2327 // Due to a previous bug, these modules were not considered stubs, so we retain that.
2328 return false, javaPlatform
2329 }
Anton Hansson2d0c1942020-05-25 12:20:51 +01002330 return true, javaSdk
2331 }
Jihoon Kangfa4a90d2023-12-20 02:53:38 +00002332 if hasStubsLibrarySuffix(name, apiScopeSystem) {
Anton Hansson2d0c1942020-05-25 12:20:51 +01002333 return true, javaSystem
2334 }
Jihoon Kangfa4a90d2023-12-20 02:53:38 +00002335 if hasStubsLibrarySuffix(name, apiScopeModuleLib) {
Anton Hansson2d0c1942020-05-25 12:20:51 +01002336 return true, javaModule
2337 }
Jihoon Kangfa4a90d2023-12-20 02:53:38 +00002338 if hasStubsLibrarySuffix(name, apiScopeTest) {
Anton Hansson2d0c1942020-05-25 12:20:51 +01002339 return true, javaSystem
2340 }
Jihoon Kangfa4a90d2023-12-20 02:53:38 +00002341 if hasStubsLibrarySuffix(name, apiScopeSystemServer) {
Jihoon Kang1147b312023-06-08 23:25:57 +00002342 return true, javaSystemServer
2343 }
Anton Hansson2d0c1942020-05-25 12:20:51 +01002344 return false, javaPlatform
2345}
2346
Jaewoong Jung4f158ee2019-07-11 10:05:35 -07002347// java_sdk_library is a special Java library that provides optional platform APIs to apps.
2348// In practice, it can be viewed as a combination of several modules: 1) stubs library that clients
2349// are linked against to, 2) droiddoc module that internally generates API stubs source files,
2350// 3) the real runtime shared library that implements the APIs, and 4) XML file for adding
2351// the runtime lib to the classpath at runtime if requested via <uses-library>.
Inseob Kimc0907f12019-02-08 21:00:45 +09002352func SdkLibraryFactory() android.Module {
2353 module := &SdkLibrary{}
Paul Duffinc3091c82020-05-08 14:16:20 +01002354
2355 // Initialize information common between source and prebuilt.
Paul Duffin71b33cc2021-06-23 11:39:47 +01002356 module.initCommon(module)
Paul Duffinc3091c82020-05-08 14:16:20 +01002357
Inseob Kimc0907f12019-02-08 21:00:45 +09002358 module.InitSdkLibraryProperties()
Jooyung Han58f26ab2019-12-18 15:34:32 +09002359 android.InitApexModule(module)
Sundong Ahn054b19a2018-10-19 13:46:09 +09002360 InitJavaModule(module, android.HostAndDeviceSupported)
Paul Duffin3375e352020-04-28 10:44:03 +01002361
2362 // Initialize the map from scope to scope specific properties.
2363 scopeToProperties := make(map[*apiScope]*ApiScopeProperties)
2364 for _, scope := range allApiScopes {
2365 scopeToProperties[scope] = scope.scopeSpecificProperties(module)
2366 }
2367 module.scopeToProperties = scopeToProperties
2368
Paul Duffin4911a892020-04-29 23:35:13 +01002369 // Add the properties containing visibility rules so that they are checked.
Paul Duffin5df79302020-05-16 15:52:12 +01002370 android.AddVisibilityProperty(module, "impl_library_visibility", &module.sdkLibraryProperties.Impl_library_visibility)
Paul Duffin4911a892020-04-29 23:35:13 +01002371 android.AddVisibilityProperty(module, "stubs_library_visibility", &module.sdkLibraryProperties.Stubs_library_visibility)
2372 android.AddVisibilityProperty(module, "stubs_source_visibility", &module.sdkLibraryProperties.Stubs_source_visibility)
2373
Paul Duffin1b1e8062020-05-08 13:44:43 +01002374 module.SetDefaultableHook(func(ctx android.DefaultableHookContext) {
Paul Duffindfa131e2020-05-15 20:37:11 +01002375 // If no implementation is required then it cannot be used as a shared library
2376 // either.
2377 if !module.requiresRuntimeImplementationLibrary() {
2378 // If shared_library has been explicitly set to true then it is incompatible
2379 // with api_only: true.
2380 if proptools.Bool(module.commonSdkLibraryProperties.Shared_library) {
2381 ctx.PropertyErrorf("api_only/shared_library", "inconsistent settings, shared_library and api_only cannot both be true")
2382 }
2383 // Set shared_library: false.
2384 module.commonSdkLibraryProperties.Shared_library = proptools.BoolPtr(false)
2385 }
2386
Paul Duffin1b1e8062020-05-08 13:44:43 +01002387 if module.initCommonAfterDefaultsApplied(ctx) {
2388 module.CreateInternalModules(ctx)
2389 }
2390 })
Jiyong Parkc678ad32018-04-10 13:07:10 +09002391 return module
2392}
Colin Cross79c7c262019-04-17 11:11:46 -07002393
2394//
2395// SDK library prebuilts
2396//
2397
Paul Duffin56d44902020-01-31 13:36:25 +00002398// Properties associated with each api scope.
2399type sdkLibraryScopeProperties struct {
Colin Cross79c7c262019-04-17 11:11:46 -07002400 Jars []string `android:"path"`
2401
2402 Sdk_version *string
2403
Colin Cross79c7c262019-04-17 11:11:46 -07002404 // List of shared java libs that this module has dependencies to
2405 Libs []string
Paul Duffin3d1248c2020-04-09 00:10:17 +01002406
Paul Duffinc8782502020-04-29 20:45:27 +01002407 // The stubs source.
Paul Duffin3d1248c2020-04-09 00:10:17 +01002408 Stub_srcs []string `android:"path"`
Paul Duffin1fd005d2020-04-09 01:08:11 +01002409
2410 // The current.txt
Paul Duffin0f8faff2020-05-20 16:18:00 +01002411 Current_api *string `android:"path"`
Paul Duffin1fd005d2020-04-09 01:08:11 +01002412
2413 // The removed.txt
Paul Duffin0f8faff2020-05-20 16:18:00 +01002414 Removed_api *string `android:"path"`
Anton Hanssond78eb762021-09-21 15:25:12 +01002415
2416 // Annotation zip
2417 Annotations *string `android:"path"`
Colin Cross79c7c262019-04-17 11:11:46 -07002418}
2419
Paul Duffin56d44902020-01-31 13:36:25 +00002420type sdkLibraryImportProperties struct {
Paul Duffinfcfd7912020-01-31 17:54:30 +00002421 // List of shared java libs, common to all scopes, that this module has
2422 // dependencies to
2423 Libs []string
Paul Duffin1267d872021-04-16 17:21:36 +01002424
2425 // If set to true, compile dex files for the stubs. Defaults to false.
2426 Compile_dex *bool
Paul Duffin869de142021-07-15 14:14:41 +01002427
2428 // If not empty, classes are restricted to the specified packages and their sub-packages.
Paul Duffin869de142021-07-15 14:14:41 +01002429 Permitted_packages []string
Paul Duffin56d44902020-01-31 13:36:25 +00002430}
2431
Paul Duffineedc5d52020-06-12 17:46:39 +01002432type SdkLibraryImport struct {
Colin Cross79c7c262019-04-17 11:11:46 -07002433 android.ModuleBase
2434 android.DefaultableModuleBase
2435 prebuilt android.Prebuilt
Paul Duffindd46f712020-02-10 13:37:10 +00002436 android.ApexModuleBase
Colin Cross79c7c262019-04-17 11:11:46 -07002437
Paul Duffin37856732021-02-26 14:24:15 +00002438 hiddenAPI
Jiakai Zhang204356f2021-09-09 08:12:46 +00002439 dexpreopter
Paul Duffin37856732021-02-26 14:24:15 +00002440
Colin Cross79c7c262019-04-17 11:11:46 -07002441 properties sdkLibraryImportProperties
2442
Paul Duffin46a26a82020-04-07 19:27:04 +01002443 // Map from api scope to the scope specific property structure.
2444 scopeProperties map[*apiScope]*sdkLibraryScopeProperties
2445
Paul Duffin56d44902020-01-31 13:36:25 +00002446 commonToSdkLibraryAndImport
Paul Duffineedc5d52020-06-12 17:46:39 +01002447
2448 // The reference to the implementation library created by the source module.
2449 // Is nil if the source module does not exist.
2450 implLibraryModule *Library
2451
2452 // The reference to the xml permissions module created by the source module.
2453 // Is nil if the source module does not exist.
2454 xmlPermissionsFileModule *sdkLibraryXml
Paul Duffin39853512021-02-26 11:09:39 +00002455
Jeongik Chad5fe8782021-07-08 01:13:11 +09002456 // Build path to the dex implementation jar obtained from the prebuilt_apex, if any.
Spandan Dasfae468e2023-12-12 23:23:53 +00002457 dexJarFile OptionalDexJarPath
2458 dexJarFileErr error
Jeongik Chad5fe8782021-07-08 01:13:11 +09002459
2460 // Expected install file path of the source module(sdk_library)
2461 // or dex implementation jar obtained from the prebuilt_apex, if any.
2462 installFile android.Path
Colin Cross79c7c262019-04-17 11:11:46 -07002463}
2464
Paul Duffineedc5d52020-06-12 17:46:39 +01002465var _ SdkLibraryDependency = (*SdkLibraryImport)(nil)
Colin Cross79c7c262019-04-17 11:11:46 -07002466
Paul Duffin46a26a82020-04-07 19:27:04 +01002467// The type of a structure that contains a field of type sdkLibraryScopeProperties
2468// for each apiscope in allApiScopes, e.g. something like:
Colin Crossd079e0b2022-08-16 10:27:33 -07002469//
2470// struct {
2471// Public sdkLibraryScopeProperties
2472// System sdkLibraryScopeProperties
2473// ...
2474// }
Paul Duffin46a26a82020-04-07 19:27:04 +01002475var allScopeStructType = createAllScopePropertiesStructType()
2476
2477// Dynamically create a structure type for each apiscope in allApiScopes.
2478func createAllScopePropertiesStructType() reflect.Type {
2479 var fields []reflect.StructField
2480 for _, apiScope := range allApiScopes {
2481 field := reflect.StructField{
2482 Name: apiScope.fieldName,
2483 Type: reflect.TypeOf(sdkLibraryScopeProperties{}),
2484 }
2485 fields = append(fields, field)
2486 }
2487
2488 return reflect.StructOf(fields)
2489}
2490
2491// Create an instance of the scope specific structure type and return a map
2492// from apiscope to a pointer to each scope specific field.
2493func createPropertiesInstance() (interface{}, map[*apiScope]*sdkLibraryScopeProperties) {
2494 allScopePropertiesPtr := reflect.New(allScopeStructType)
2495 allScopePropertiesStruct := allScopePropertiesPtr.Elem()
2496 scopeProperties := make(map[*apiScope]*sdkLibraryScopeProperties)
2497
2498 for _, apiScope := range allApiScopes {
2499 field := allScopePropertiesStruct.FieldByName(apiScope.fieldName)
2500 scopeProperties[apiScope] = field.Addr().Interface().(*sdkLibraryScopeProperties)
2501 }
2502
2503 return allScopePropertiesPtr.Interface(), scopeProperties
2504}
2505
Jaewoong Jung4f158ee2019-07-11 10:05:35 -07002506// java_sdk_library_import imports a prebuilt java_sdk_library.
Colin Cross79c7c262019-04-17 11:11:46 -07002507func sdkLibraryImportFactory() android.Module {
Paul Duffineedc5d52020-06-12 17:46:39 +01002508 module := &SdkLibraryImport{}
Colin Cross79c7c262019-04-17 11:11:46 -07002509
Paul Duffin46a26a82020-04-07 19:27:04 +01002510 allScopeProperties, scopeToProperties := createPropertiesInstance()
2511 module.scopeProperties = scopeToProperties
Jiakai Zhang9c4dc192023-02-09 00:09:24 +08002512 module.AddProperties(&module.properties, allScopeProperties, &module.importDexpreoptProperties)
Colin Cross79c7c262019-04-17 11:11:46 -07002513
Paul Duffinc3091c82020-05-08 14:16:20 +01002514 // Initialize information common between source and prebuilt.
Paul Duffin71b33cc2021-06-23 11:39:47 +01002515 module.initCommon(module)
Paul Duffinc3091c82020-05-08 14:16:20 +01002516
Paul Duffin0bdcb272020-02-06 15:24:57 +00002517 android.InitPrebuiltModule(module, &[]string{""})
Paul Duffindd46f712020-02-10 13:37:10 +00002518 android.InitApexModule(module)
Colin Cross79c7c262019-04-17 11:11:46 -07002519 InitJavaModule(module, android.HostAndDeviceSupported)
2520
Paul Duffin1b1e8062020-05-08 13:44:43 +01002521 module.SetDefaultableHook(func(mctx android.DefaultableHookContext) {
2522 if module.initCommonAfterDefaultsApplied(mctx) {
2523 module.createInternalModules(mctx)
2524 }
2525 })
Colin Cross79c7c262019-04-17 11:11:46 -07002526 return module
2527}
2528
Paul Duffin630b11e2021-07-15 13:35:26 +01002529var _ PermittedPackagesForUpdatableBootJars = (*SdkLibraryImport)(nil)
2530
2531func (module *SdkLibraryImport) PermittedPackagesForUpdatableBootJars() []string {
2532 return module.properties.Permitted_packages
2533}
2534
Paul Duffineedc5d52020-06-12 17:46:39 +01002535func (module *SdkLibraryImport) Prebuilt() *android.Prebuilt {
Colin Cross79c7c262019-04-17 11:11:46 -07002536 return &module.prebuilt
2537}
2538
Paul Duffineedc5d52020-06-12 17:46:39 +01002539func (module *SdkLibraryImport) Name() string {
Colin Cross79c7c262019-04-17 11:11:46 -07002540 return module.prebuilt.Name(module.ModuleBase.Name())
2541}
2542
Paul Duffineedc5d52020-06-12 17:46:39 +01002543func (module *SdkLibraryImport) createInternalModules(mctx android.DefaultableHookContext) {
Colin Cross79c7c262019-04-17 11:11:46 -07002544
Paul Duffin50061512020-01-21 16:31:05 +00002545 // If the build is configured to use prebuilts then force this to be preferred.
Jeongik Cha816a23a2020-07-08 01:09:23 +09002546 if mctx.Config().AlwaysUsePrebuiltSdks() {
Paul Duffin50061512020-01-21 16:31:05 +00002547 module.prebuilt.ForcePrefer()
2548 }
2549
Paul Duffin46a26a82020-04-07 19:27:04 +01002550 for apiScope, scopeProperties := range module.scopeProperties {
Paul Duffin56d44902020-01-31 13:36:25 +00002551 if len(scopeProperties.Jars) == 0 {
2552 continue
2553 }
2554
Paul Duffinbbb546b2020-04-09 00:07:11 +01002555 module.createJavaImportForStubs(mctx, apiScope, scopeProperties)
Paul Duffin3d1248c2020-04-09 00:10:17 +01002556
Paul Duffin0f8faff2020-05-20 16:18:00 +01002557 if len(scopeProperties.Stub_srcs) > 0 {
2558 module.createPrebuiltStubsSources(mctx, apiScope, scopeProperties)
2559 }
Jihoon Kang71c86832023-09-13 01:01:53 +00002560
2561 if scopeProperties.Current_api != nil {
2562 module.createPrebuiltApiContribution(mctx, apiScope, scopeProperties)
2563 }
Paul Duffin56d44902020-01-31 13:36:25 +00002564 }
Colin Cross79c7c262019-04-17 11:11:46 -07002565
2566 javaSdkLibraries := javaSdkLibraries(mctx.Config())
2567 javaSdkLibrariesLock.Lock()
2568 defer javaSdkLibrariesLock.Unlock()
2569 *javaSdkLibraries = append(*javaSdkLibraries, module.BaseModuleName())
2570}
2571
Paul Duffineedc5d52020-06-12 17:46:39 +01002572func (module *SdkLibraryImport) createJavaImportForStubs(mctx android.DefaultableHookContext, apiScope *apiScope, scopeProperties *sdkLibraryScopeProperties) {
Paul Duffinbbb546b2020-04-09 00:07:11 +01002573 // Creates a java import for the jar with ".stubs" suffix
2574 props := struct {
Paul Duffin1dbe3ca2020-05-16 09:57:59 +01002575 Name *string
2576 Sdk_version *string
2577 Libs []string
2578 Jars []string
Paul Duffin1267d872021-04-16 17:21:36 +01002579 Compile_dex *bool
Paul Duffinbf4de042022-09-27 12:41:52 +01002580
2581 android.UserSuppliedPrebuiltProperties
Paul Duffinbbb546b2020-04-09 00:07:11 +01002582 }{}
Paul Duffinc3091c82020-05-08 14:16:20 +01002583 props.Name = proptools.StringPtr(module.stubsLibraryModuleName(apiScope))
Paul Duffinbbb546b2020-04-09 00:07:11 +01002584 props.Sdk_version = scopeProperties.Sdk_version
2585 // Prepend any of the libs from the legacy public properties to the libs for each of the
2586 // scopes to avoid having to duplicate them in each scope.
2587 props.Libs = append(module.properties.Libs, scopeProperties.Libs...)
2588 props.Jars = scopeProperties.Jars
Paul Duffin1dbe3ca2020-05-16 09:57:59 +01002589
Paul Duffin38b57852020-05-13 16:08:09 +01002590 // The imports are preferred if the java_sdk_library_import is preferred.
Paul Duffinbf4de042022-09-27 12:41:52 +01002591 props.CopyUserSuppliedPropertiesFromPrebuilt(&module.prebuilt)
Paul Duffin859fe962020-05-15 10:20:31 +01002592
Paul Duffin1267d872021-04-16 17:21:36 +01002593 // The imports need to be compiled to dex if the java_sdk_library_import requests it.
Paul Duffinf4600f62021-05-13 22:34:45 +01002594 compileDex := module.properties.Compile_dex
2595 if module.stubLibrariesCompiledForDex() {
2596 compileDex = proptools.BoolPtr(true)
2597 }
2598 props.Compile_dex = compileDex
Paul Duffin1267d872021-04-16 17:21:36 +01002599
Paul Duffin859fe962020-05-15 10:20:31 +01002600 mctx.CreateModule(ImportFactory, &props, module.sdkComponentPropertiesForChildLibrary())
Paul Duffinbbb546b2020-04-09 00:07:11 +01002601}
2602
Paul Duffineedc5d52020-06-12 17:46:39 +01002603func (module *SdkLibraryImport) createPrebuiltStubsSources(mctx android.DefaultableHookContext, apiScope *apiScope, scopeProperties *sdkLibraryScopeProperties) {
Paul Duffin3d1248c2020-04-09 00:10:17 +01002604 props := struct {
Paul Duffinbf4de042022-09-27 12:41:52 +01002605 Name *string
2606 Srcs []string
2607
2608 android.UserSuppliedPrebuiltProperties
Paul Duffin3d1248c2020-04-09 00:10:17 +01002609 }{}
Paul Duffinc3091c82020-05-08 14:16:20 +01002610 props.Name = proptools.StringPtr(module.stubsSourceModuleName(apiScope))
Paul Duffin3d1248c2020-04-09 00:10:17 +01002611 props.Srcs = scopeProperties.Stub_srcs
Paul Duffin38b57852020-05-13 16:08:09 +01002612
2613 // The stubs source is preferred if the java_sdk_library_import is preferred.
Paul Duffinbf4de042022-09-27 12:41:52 +01002614 props.CopyUserSuppliedPropertiesFromPrebuilt(&module.prebuilt)
2615
Spandan Das2cc80ba2023-10-27 17:21:52 +00002616 mctx.CreateModule(PrebuiltStubsSourcesFactory, &props, module.sdkComponentPropertiesForChildLibrary())
Paul Duffin3d1248c2020-04-09 00:10:17 +01002617}
2618
Jihoon Kang71c86832023-09-13 01:01:53 +00002619func (module *SdkLibraryImport) createPrebuiltApiContribution(mctx android.DefaultableHookContext, apiScope *apiScope, scopeProperties *sdkLibraryScopeProperties) {
2620 api_file := scopeProperties.Current_api
2621 api_surface := &apiScope.name
2622
2623 props := struct {
2624 Name *string
2625 Api_surface *string
2626 Api_file *string
2627 Visibility []string
2628 }{}
2629
2630 props.Name = proptools.StringPtr(module.stubsSourceModuleName(apiScope) + ".api.contribution")
2631 props.Api_surface = api_surface
2632 props.Api_file = api_file
2633 props.Visibility = []string{"//visibility:override", "//visibility:public"}
2634
Spandan Das2cc80ba2023-10-27 17:21:52 +00002635 mctx.CreateModule(ApiContributionImportFactory, &props, module.sdkComponentPropertiesForChildLibrary())
Jihoon Kang71c86832023-09-13 01:01:53 +00002636}
2637
Paul Duffin44f1d842020-06-26 20:17:02 +01002638// Add the dependencies on the child module in the component deps mutator so that it
2639// creates references to the prebuilt and not the source modules.
2640func (module *SdkLibraryImport) ComponentDepsMutator(ctx android.BottomUpMutatorContext) {
Paul Duffin46a26a82020-04-07 19:27:04 +01002641 for apiScope, scopeProperties := range module.scopeProperties {
Paul Duffin56d44902020-01-31 13:36:25 +00002642 if len(scopeProperties.Jars) == 0 {
2643 continue
2644 }
2645
2646 // Add dependencies to the prebuilt stubs library
Paul Duffin864116c2021-04-02 10:24:13 +01002647 ctx.AddVariationDependencies(nil, apiScope.stubsTag, android.PrebuiltNameFromSource(module.stubsLibraryModuleName(apiScope)))
Paul Duffin0f8faff2020-05-20 16:18:00 +01002648
2649 if len(scopeProperties.Stub_srcs) > 0 {
2650 // Add dependencies to the prebuilt stubs source library
Paul Duffin864116c2021-04-02 10:24:13 +01002651 ctx.AddVariationDependencies(nil, apiScope.stubsSourceTag, android.PrebuiltNameFromSource(module.stubsSourceModuleName(apiScope)))
Paul Duffin0f8faff2020-05-20 16:18:00 +01002652 }
Paul Duffin56d44902020-01-31 13:36:25 +00002653 }
Paul Duffin44f1d842020-06-26 20:17:02 +01002654}
2655
2656// Add other dependencies as normal.
2657func (module *SdkLibraryImport) DepsMutator(ctx android.BottomUpMutatorContext) {
Paul Duffineedc5d52020-06-12 17:46:39 +01002658
2659 implName := module.implLibraryModuleName()
2660 if ctx.OtherModuleExists(implName) {
2661 ctx.AddVariationDependencies(nil, implLibraryTag, implName)
2662
2663 xmlPermissionsModuleName := module.xmlPermissionsModuleName()
2664 if module.sharedLibrary() && ctx.OtherModuleExists(xmlPermissionsModuleName) {
2665 // Add dependency to the rule for generating the xml permissions file
2666 ctx.AddDependency(module, xmlPermissionsFileTag, xmlPermissionsModuleName)
2667 }
2668 }
Colin Cross79c7c262019-04-17 11:11:46 -07002669}
2670
Jiyong Park45bf82e2020-12-15 22:29:02 +09002671var _ android.ApexModule = (*SdkLibraryImport)(nil)
2672
2673// Implements android.ApexModule
Paul Duffineedc5d52020-06-12 17:46:39 +01002674func (module *SdkLibraryImport) DepIsInSameApex(mctx android.BaseModuleContext, dep android.Module) bool {
2675 depTag := mctx.OtherModuleDependencyTag(dep)
2676 if depTag == xmlPermissionsFileTag {
2677 return true
2678 }
2679
2680 // None of the other dependencies of the java_sdk_library_import are in the same apex
2681 // as the one that references this module.
2682 return false
2683}
2684
Jiyong Park45bf82e2020-12-15 22:29:02 +09002685// Implements android.ApexModule
Dan Albertc8060532020-07-22 22:32:17 -07002686func (module *SdkLibraryImport) ShouldSupportSdkVersion(ctx android.BaseModuleContext,
2687 sdkVersion android.ApiLevel) error {
Jooyung Han749dc692020-04-15 11:03:39 +09002688 // we don't check prebuilt modules for sdk_version
2689 return nil
2690}
2691
Paul Duffinea8f8082021-06-24 13:25:57 +01002692// Implements android.ApexModule
2693func (module *SdkLibraryImport) UniqueApexVariations() bool {
2694 return module.uniqueApexVariations()
2695}
2696
Paul Duffin09817d62022-04-28 17:45:11 +01002697// MinSdkVersion - Implements hiddenAPIModule
Spandan Das8c9ae7e2023-03-03 21:20:36 +00002698func (module *SdkLibraryImport) MinSdkVersion(ctx android.EarlyModuleContext) android.ApiLevel {
2699 return android.NoneApiLevel
Paul Duffin09817d62022-04-28 17:45:11 +01002700}
2701
2702var _ hiddenAPIModule = (*SdkLibraryImport)(nil)
2703
Paul Duffineedc5d52020-06-12 17:46:39 +01002704func (module *SdkLibraryImport) OutputFiles(tag string) (android.Paths, error) {
Paul Duffin1e940d52022-04-29 14:21:25 +01002705 paths, err := module.commonOutputFiles(tag)
2706 if paths != nil || err != nil {
2707 return paths, err
2708 }
2709 if module.implLibraryModule != nil {
2710 return module.implLibraryModule.OutputFiles(tag)
2711 } else {
2712 return nil, nil
2713 }
Paul Duffin46dc45a2020-05-14 15:39:10 +01002714}
2715
Paul Duffineedc5d52020-06-12 17:46:39 +01002716func (module *SdkLibraryImport) GenerateAndroidBuildActions(ctx android.ModuleContext) {
Paul Duffina2ae7e02020-09-11 11:55:00 +01002717 module.generateCommonBuildActions(ctx)
2718
Jeongik Chad5fe8782021-07-08 01:13:11 +09002719 // Assume that source module(sdk_library) is installed in /<sdk_library partition>/framework
2720 module.installFile = android.PathForModuleInstall(ctx, "framework", module.Stem()+".jar")
2721
Paul Duffin0f8faff2020-05-20 16:18:00 +01002722 // Record the paths to the prebuilt stubs library and stubs source.
Colin Cross79c7c262019-04-17 11:11:46 -07002723 ctx.VisitDirectDeps(func(to android.Module) {
2724 tag := ctx.OtherModuleDependencyTag(to)
2725
Paul Duffin0f8faff2020-05-20 16:18:00 +01002726 // Extract information from any of the scope specific dependencies.
2727 if scopeTag, ok := tag.(scopeDependencyTag); ok {
2728 apiScope := scopeTag.apiScope
2729 scopePaths := module.getScopePathsCreateIfNeeded(apiScope)
2730
2731 // Extract information from the dependency. The exact information extracted
2732 // is determined by the nature of the dependency which is determined by the tag.
2733 scopeTag.extractDepInfo(ctx, to, scopePaths)
Paul Duffineedc5d52020-06-12 17:46:39 +01002734 } else if tag == implLibraryTag {
2735 if implLibrary, ok := to.(*Library); ok {
2736 module.implLibraryModule = implLibrary
2737 } else {
2738 ctx.ModuleErrorf("implementation library must be of type *java.Library but was %T", to)
2739 }
2740 } else if tag == xmlPermissionsFileTag {
2741 if xmlPermissionsFileModule, ok := to.(*sdkLibraryXml); ok {
2742 module.xmlPermissionsFileModule = xmlPermissionsFileModule
2743 } else {
2744 ctx.ModuleErrorf("xml permissions file module must be of type *sdkLibraryXml but was %T", to)
2745 }
Colin Cross79c7c262019-04-17 11:11:46 -07002746 }
2747 })
Paul Duffin0f8faff2020-05-20 16:18:00 +01002748
2749 // Populate the scope paths with information from the properties.
2750 for apiScope, scopeProperties := range module.scopeProperties {
2751 if len(scopeProperties.Jars) == 0 {
2752 continue
2753 }
2754
2755 paths := module.getScopePathsCreateIfNeeded(apiScope)
Anton Hanssond78eb762021-09-21 15:25:12 +01002756 paths.annotationsZip = android.OptionalPathForModuleSrc(ctx, scopeProperties.Annotations)
Paul Duffin0f8faff2020-05-20 16:18:00 +01002757 paths.currentApiFilePath = android.OptionalPathForModuleSrc(ctx, scopeProperties.Current_api)
2758 paths.removedApiFilePath = android.OptionalPathForModuleSrc(ctx, scopeProperties.Removed_api)
2759 }
Paul Duffin39853512021-02-26 11:09:39 +00002760
2761 if ctx.Device() {
2762 // If this is a variant created for a prebuilt_apex then use the dex implementation jar
2763 // obtained from the associated deapexer module.
Colin Crossff694a82023-12-13 15:54:49 -08002764 ai, _ := android.ModuleProvider(ctx, android.ApexInfoProvider)
Paul Duffin39853512021-02-26 11:09:39 +00002765 if ai.ForPrebuiltApex {
Paul Duffin39853512021-02-26 11:09:39 +00002766 // Get the path of the dex implementation jar from the `deapexer` module.
Spandan Dasfae468e2023-12-12 23:23:53 +00002767 di, err := android.FindDeapexerProviderForModule(ctx)
2768 if err != nil {
2769 // An error was found, possibly due to multiple apexes in the tree that export this library
2770 // Defer the error till a client tries to call DexJarBuildPath
2771 module.dexJarFileErr = err
2772 return
Martin Stjernholm44825602021-09-17 01:44:12 +01002773 }
Spandan Das5be63332023-12-13 00:06:32 +00002774 dexJarFileApexRootRelative := ApexRootRelativePathToJavaLib(module.BaseModuleName())
Jiakai Zhang81e46812023-02-08 21:56:07 +08002775 if dexOutputPath := di.PrebuiltExportPath(dexJarFileApexRootRelative); dexOutputPath != nil {
Martin Stjernholm8be1e6d2021-09-15 03:34:04 +01002776 dexJarFile := makeDexJarPathFromPath(dexOutputPath)
2777 module.dexJarFile = dexJarFile
Jiakai Zhang204356f2021-09-09 08:12:46 +00002778 installPath := android.PathForModuleInPartitionInstall(
Jiakai Zhang81e46812023-02-08 21:56:07 +08002779 ctx, "apex", ai.ApexVariationName, dexJarFileApexRootRelative)
Jiakai Zhang204356f2021-09-09 08:12:46 +00002780 module.installFile = installPath
Martin Stjernholm8be1e6d2021-09-15 03:34:04 +01002781 module.initHiddenAPI(ctx, dexJarFile, module.findScopePaths(apiScopePublic).stubsImplPath[0], nil)
Jiakai Zhang204356f2021-09-09 08:12:46 +00002782
Jiakai Zhang204356f2021-09-09 08:12:46 +00002783 module.dexpreopter.installPath = module.dexpreopter.getInstallPath(ctx, installPath)
2784 module.dexpreopter.isSDKLibrary = true
2785 module.dexpreopter.uncompressedDex = shouldUncompressDex(ctx, &module.dexpreopter)
Jiakai Zhang81e46812023-02-08 21:56:07 +08002786
2787 if profilePath := di.PrebuiltExportPath(dexJarFileApexRootRelative + ".prof"); profilePath != nil {
2788 module.dexpreopter.inputProfilePathOnHost = profilePath
2789 }
2790
2791 // Dexpreopting.
Jiakai Zhang204356f2021-09-09 08:12:46 +00002792 module.dexpreopt(ctx, dexOutputPath)
Paul Duffin39853512021-02-26 11:09:39 +00002793 } else {
2794 // This should never happen as a variant for a prebuilt_apex is only created if the
2795 // prebuilt_apex has been configured to export the java library dex file.
Martin Stjernholm44825602021-09-17 01:44:12 +01002796 ctx.ModuleErrorf("internal error: no dex implementation jar available from prebuilt APEX %s", di.ApexModuleName())
Paul Duffin39853512021-02-26 11:09:39 +00002797 }
2798 }
2799 }
Colin Cross79c7c262019-04-17 11:11:46 -07002800}
2801
Jiyong Parkf1691d22021-03-29 20:11:58 +09002802func (module *SdkLibraryImport) sdkJars(ctx android.BaseModuleContext, sdkVersion android.SdkSpec, headerJars bool) android.Paths {
Paul Duffineedc5d52020-06-12 17:46:39 +01002803
2804 // For consistency with SdkLibrary make the implementation jar available to libraries that
2805 // are within the same APEX.
2806 implLibraryModule := module.implLibraryModule
Colin Cross56a83212020-09-15 18:30:11 -07002807 if implLibraryModule != nil && withinSameApexesAs(ctx, module) {
Paul Duffineedc5d52020-06-12 17:46:39 +01002808 if headerJars {
2809 return implLibraryModule.HeaderJars()
2810 } else {
2811 return implLibraryModule.ImplementationJars()
2812 }
2813 }
2814
Paul Duffin23970f42020-05-20 14:20:02 +01002815 return module.selectHeaderJarsForSdkVersion(ctx, sdkVersion)
Paul Duffin56d44902020-01-31 13:36:25 +00002816}
2817
Colin Cross79c7c262019-04-17 11:11:46 -07002818// to satisfy SdkLibraryDependency interface
Jiyong Parkf1691d22021-03-29 20:11:58 +09002819func (module *SdkLibraryImport) SdkHeaderJars(ctx android.BaseModuleContext, sdkVersion android.SdkSpec) android.Paths {
Colin Cross79c7c262019-04-17 11:11:46 -07002820 // This module is just a wrapper for the prebuilt stubs.
Paul Duffineedc5d52020-06-12 17:46:39 +01002821 return module.sdkJars(ctx, sdkVersion, true)
Colin Cross79c7c262019-04-17 11:11:46 -07002822}
2823
2824// to satisfy SdkLibraryDependency interface
Jiyong Parkf1691d22021-03-29 20:11:58 +09002825func (module *SdkLibraryImport) SdkImplementationJars(ctx android.BaseModuleContext, sdkVersion android.SdkSpec) android.Paths {
Colin Cross79c7c262019-04-17 11:11:46 -07002826 // This module is just a wrapper for the stubs.
Paul Duffineedc5d52020-06-12 17:46:39 +01002827 return module.sdkJars(ctx, sdkVersion, false)
2828}
2829
Ulya Trafimovichdbf31662020-12-17 12:07:54 +00002830// to satisfy UsesLibraryDependency interface
Spandan Das59a4a2b2024-01-09 21:35:56 +00002831func (module *SdkLibraryImport) DexJarBuildPath(ctx android.ModuleErrorfContext) OptionalDexJarPath {
Paul Duffin39853512021-02-26 11:09:39 +00002832 // The dex implementation jar extracted from the .apex file should be used in preference to the
2833 // source.
Spandan Dasfae468e2023-12-12 23:23:53 +00002834 if module.dexJarFileErr != nil {
Spandan Das59a4a2b2024-01-09 21:35:56 +00002835 ctx.ModuleErrorf(module.dexJarFileErr.Error())
Spandan Dasfae468e2023-12-12 23:23:53 +00002836 }
Martin Stjernholm8be1e6d2021-09-15 03:34:04 +01002837 if module.dexJarFile.IsSet() {
Paul Duffin39853512021-02-26 11:09:39 +00002838 return module.dexJarFile
2839 }
Paul Duffineedc5d52020-06-12 17:46:39 +01002840 if module.implLibraryModule == nil {
Martin Stjernholm8be1e6d2021-09-15 03:34:04 +01002841 return makeUnsetDexJarPath()
Paul Duffineedc5d52020-06-12 17:46:39 +01002842 } else {
Spandan Das59a4a2b2024-01-09 21:35:56 +00002843 return module.implLibraryModule.DexJarBuildPath(ctx)
Paul Duffineedc5d52020-06-12 17:46:39 +01002844 }
2845}
2846
Ulya Trafimovichdbf31662020-12-17 12:07:54 +00002847// to satisfy UsesLibraryDependency interface
Ulya Trafimovich31e444e2020-08-14 17:32:16 +01002848func (module *SdkLibraryImport) DexJarInstallPath() android.Path {
Jeongik Chad5fe8782021-07-08 01:13:11 +09002849 return module.installFile
Ulya Trafimovich31e444e2020-08-14 17:32:16 +01002850}
2851
Ulya Trafimovichdbf31662020-12-17 12:07:54 +00002852// to satisfy UsesLibraryDependency interface
2853func (module *SdkLibraryImport) ClassLoaderContexts() dexpreopt.ClassLoaderContextMap {
2854 return nil
2855}
2856
Paul Duffineedc5d52020-06-12 17:46:39 +01002857// to satisfy apex.javaDependency interface
2858func (module *SdkLibraryImport) JacocoReportClassesFile() android.Path {
2859 if module.implLibraryModule == nil {
2860 return nil
2861 } else {
2862 return module.implLibraryModule.JacocoReportClassesFile()
2863 }
2864}
2865
2866// to satisfy apex.javaDependency interface
Colin Cross08dca382020-07-21 20:31:17 -07002867func (module *SdkLibraryImport) LintDepSets() LintDepSets {
2868 if module.implLibraryModule == nil {
2869 return LintDepSets{}
2870 } else {
2871 return module.implLibraryModule.LintDepSets()
2872 }
2873}
2874
Spandan Das17854f52022-01-14 21:19:14 +00002875func (module *SdkLibraryImport) GetStrictUpdatabilityLinting() bool {
Jaewoong Jung476b9d62021-05-10 15:30:00 -07002876 if module.implLibraryModule == nil {
2877 return false
2878 } else {
Spandan Das17854f52022-01-14 21:19:14 +00002879 return module.implLibraryModule.GetStrictUpdatabilityLinting()
Jaewoong Jung476b9d62021-05-10 15:30:00 -07002880 }
2881}
2882
Spandan Das17854f52022-01-14 21:19:14 +00002883func (module *SdkLibraryImport) SetStrictUpdatabilityLinting(strictLinting bool) {
Jaewoong Jung476b9d62021-05-10 15:30:00 -07002884 if module.implLibraryModule != nil {
Spandan Das17854f52022-01-14 21:19:14 +00002885 module.implLibraryModule.SetStrictUpdatabilityLinting(strictLinting)
Jaewoong Jung476b9d62021-05-10 15:30:00 -07002886 }
2887}
2888
Colin Cross08dca382020-07-21 20:31:17 -07002889// to satisfy apex.javaDependency interface
Paul Duffineedc5d52020-06-12 17:46:39 +01002890func (module *SdkLibraryImport) Stem() string {
2891 return module.BaseModuleName()
Colin Cross79c7c262019-04-17 11:11:46 -07002892}
Jiyong Parke3833882020-02-17 17:28:10 +09002893
Paul Duffin44b481b2020-06-17 16:59:43 +01002894var _ ApexDependency = (*SdkLibraryImport)(nil)
2895
2896// to satisfy java.ApexDependency interface
2897func (module *SdkLibraryImport) HeaderJars() android.Paths {
2898 if module.implLibraryModule == nil {
2899 return nil
2900 } else {
2901 return module.implLibraryModule.HeaderJars()
2902 }
2903}
2904
2905// to satisfy java.ApexDependency interface
2906func (module *SdkLibraryImport) ImplementationAndResourcesJars() android.Paths {
2907 if module.implLibraryModule == nil {
2908 return nil
2909 } else {
2910 return module.implLibraryModule.ImplementationAndResourcesJars()
2911 }
2912}
2913
Jiakai Zhang204356f2021-09-09 08:12:46 +00002914// to satisfy java.DexpreopterInterface interface
2915func (module *SdkLibraryImport) IsInstallable() bool {
2916 return true
2917}
2918
Paul Duffinfef55002021-06-17 14:56:05 +01002919var _ android.RequiredFilesFromPrebuiltApex = (*SdkLibraryImport)(nil)
2920
Paul Duffinb4bbf2c2021-06-17 15:59:07 +01002921func (module *SdkLibraryImport) RequiredFilesFromPrebuiltApex(ctx android.BaseModuleContext) []string {
Paul Duffinfef55002021-06-17 14:56:05 +01002922 name := module.BaseModuleName()
Jiakai Zhang81e46812023-02-08 21:56:07 +08002923 return requiredFilesFromPrebuiltApexForImport(name, &module.dexpreopter)
Paul Duffinfef55002021-06-17 14:56:05 +01002924}
2925
Jiyong Parke3833882020-02-17 17:28:10 +09002926// java_sdk_library_xml
Jiyong Parke3833882020-02-17 17:28:10 +09002927type sdkLibraryXml struct {
2928 android.ModuleBase
2929 android.DefaultableModuleBase
2930 android.ApexModuleBase
2931
2932 properties sdkLibraryXmlProperties
2933
2934 outputFilePath android.OutputPath
2935 installDirPath android.InstallPath
Colin Cross56a83212020-09-15 18:30:11 -07002936
2937 hideApexVariantFromMake bool
Jiyong Parke3833882020-02-17 17:28:10 +09002938}
2939
2940type sdkLibraryXmlProperties struct {
2941 // canonical name of the lib
2942 Lib_name *string
Pedro Loureiro9956e5e2021-09-07 17:21:59 +00002943
2944 // Signals that this shared library is part of the bootclasspath starting
2945 // on the version indicated in this attribute.
2946 //
2947 // This will make platforms at this level and above to ignore
2948 // <uses-library> tags with this library name because the library is already
2949 // available
2950 On_bootclasspath_since *string
2951
2952 // Signals that this shared library was part of the bootclasspath before
2953 // (but not including) the version indicated in this attribute.
2954 //
2955 // The system will automatically add a <uses-library> tag with this library to
2956 // apps that target any SDK less than the version indicated in this attribute.
2957 On_bootclasspath_before *string
2958
2959 // Indicates that PackageManager should ignore this shared library if the
2960 // platform is below the version indicated in this attribute.
2961 //
2962 // This means that the device won't recognise this library as installed.
2963 Min_device_sdk *string
2964
2965 // Indicates that PackageManager should ignore this shared library if the
2966 // platform is above the version indicated in this attribute.
2967 //
2968 // This means that the device won't recognise this library as installed.
2969 Max_device_sdk *string
Pedro Loureiroc3621422021-09-28 15:40:23 +00002970
2971 // The SdkLibrary's min api level as a string
2972 //
2973 // This value comes from the ApiLevel of the MinSdkVersion property.
2974 Sdk_library_min_api_level *string
Jamie Garsidee570ace2023-11-27 12:07:36 +00002975
2976 // Uses-libs dependencies that the shared library requires to work correctly.
2977 //
2978 // This will add dependency="foo:bar" to the <library> section.
2979 Uses_libs_dependencies []string
Jiyong Parke3833882020-02-17 17:28:10 +09002980}
2981
2982// java_sdk_library_xml builds the permission xml file for a java_sdk_library.
2983// Not to be used directly by users. java_sdk_library internally uses this.
2984func sdkLibraryXmlFactory() android.Module {
2985 module := &sdkLibraryXml{}
2986
2987 module.AddProperties(&module.properties)
2988
2989 android.InitApexModule(module)
2990 android.InitAndroidArchModule(module, android.DeviceSupported, android.MultilibCommon)
2991
2992 return module
2993}
2994
Colin Crossaede88c2020-08-11 12:17:01 -07002995func (module *sdkLibraryXml) UniqueApexVariations() bool {
2996 // sdkLibraryXml needs a unique variation per APEX because the generated XML file contains the path to the
2997 // mounted APEX, which contains the name of the APEX.
2998 return true
2999}
3000
Jiyong Parke3833882020-02-17 17:28:10 +09003001// from android.PrebuiltEtcModule
Jooyung Han0703fd82020-08-26 22:11:53 +09003002func (module *sdkLibraryXml) BaseDir() string {
3003 return "etc"
3004}
3005
3006// from android.PrebuiltEtcModule
Jiyong Parke3833882020-02-17 17:28:10 +09003007func (module *sdkLibraryXml) SubDir() string {
3008 return "permissions"
3009}
3010
3011// from android.PrebuiltEtcModule
3012func (module *sdkLibraryXml) OutputFile() android.OutputPath {
3013 return module.outputFilePath
3014}
3015
3016// from android.ApexModule
3017func (module *sdkLibraryXml) AvailableFor(what string) bool {
3018 return true
3019}
3020
3021func (module *sdkLibraryXml) DepsMutator(ctx android.BottomUpMutatorContext) {
3022 // do nothing
3023}
3024
Jiyong Park45bf82e2020-12-15 22:29:02 +09003025var _ android.ApexModule = (*sdkLibraryXml)(nil)
3026
3027// Implements android.ApexModule
Dan Albertc8060532020-07-22 22:32:17 -07003028func (module *sdkLibraryXml) ShouldSupportSdkVersion(ctx android.BaseModuleContext,
3029 sdkVersion android.ApiLevel) error {
Jooyung Han749dc692020-04-15 11:03:39 +09003030 // sdkLibraryXml doesn't need to be checked separately because java_sdk_library is checked
3031 return nil
3032}
3033
Jiyong Parke3833882020-02-17 17:28:10 +09003034// File path to the runtime implementation library
Colin Cross56a83212020-09-15 18:30:11 -07003035func (module *sdkLibraryXml) implPath(ctx android.ModuleContext) string {
Jiyong Parke3833882020-02-17 17:28:10 +09003036 implName := proptools.String(module.properties.Lib_name)
Colin Crossff694a82023-12-13 15:54:49 -08003037 if apexInfo, _ := android.ModuleProvider(ctx, android.ApexInfoProvider); !apexInfo.IsForPlatform() {
Colin Crosse07f2312020-08-13 11:24:56 -07003038 // TODO(b/146468504): ApexVariationName() is only a soong module name, not apex name.
Jiyong Parke3833882020-02-17 17:28:10 +09003039 // In most cases, this works fine. But when apex_name is set or override_apex is used
3040 // this can be wrong.
Colin Cross56a83212020-09-15 18:30:11 -07003041 return fmt.Sprintf("/apex/%s/javalib/%s.jar", apexInfo.ApexVariationName, implName)
Jiyong Parke3833882020-02-17 17:28:10 +09003042 }
3043 partition := "system"
3044 if module.SocSpecific() {
3045 partition = "vendor"
3046 } else if module.DeviceSpecific() {
3047 partition = "odm"
3048 } else if module.ProductSpecific() {
3049 partition = "product"
3050 } else if module.SystemExtSpecific() {
3051 partition = "system_ext"
3052 }
3053 return "/" + partition + "/framework/" + implName + ".jar"
3054}
3055
Pedro Loureiro9956e5e2021-09-07 17:21:59 +00003056func formattedOptionalSdkLevelAttribute(ctx android.ModuleContext, attrName string, value *string) string {
3057 if value == nil {
3058 return ""
3059 }
3060 apiLevel, err := android.ApiLevelFromUser(ctx, *value)
3061 if err != nil {
Pedro Loureiroba6682f2021-10-29 09:32:32 +00003062 // attributes in bp files have underscores but in the xml have dashes.
3063 ctx.PropertyErrorf(strings.ReplaceAll(attrName, "-", "_"), err.Error())
Pedro Loureiro9956e5e2021-09-07 17:21:59 +00003064 return ""
3065 }
Pedro Loureirob638c622021-12-22 15:28:05 +00003066 if apiLevel.IsCurrent() {
3067 // passing "current" would always mean a future release, never the current (or the current in
3068 // progress) which means some conditions would never be triggered.
3069 ctx.PropertyErrorf(strings.ReplaceAll(attrName, "-", "_"),
3070 `"current" is not an allowed value for this attribute`)
3071 return ""
3072 }
Pedro Loureiro48991222022-06-17 20:01:21 +00003073 // "safeValue" is safe because it translates finalized codenames to a string
3074 // with their SDK int.
3075 safeValue := apiLevel.String()
3076 return formattedOptionalAttribute(attrName, &safeValue)
Pedro Loureiro9956e5e2021-09-07 17:21:59 +00003077}
3078
3079// formats an attribute for the xml permissions file if the value is not null
3080// returns empty string otherwise
3081func formattedOptionalAttribute(attrName string, value *string) string {
3082 if value == nil {
3083 return ""
3084 }
3085 return fmt.Sprintf(` %s=\"%s\"\n`, attrName, *value)
3086}
3087
Jamie Garsidee570ace2023-11-27 12:07:36 +00003088func formattedDependenciesAttribute(dependencies []string) string {
3089 if dependencies == nil {
3090 return ""
3091 }
3092 return fmt.Sprintf(` dependency=\"%s\"\n`, strings.Join(dependencies, ":"))
3093}
3094
Pedro Loureiro9956e5e2021-09-07 17:21:59 +00003095func (module *sdkLibraryXml) permissionsContents(ctx android.ModuleContext) string {
3096 libName := proptools.String(module.properties.Lib_name)
3097 libNameAttr := formattedOptionalAttribute("name", &libName)
3098 filePath := module.implPath(ctx)
3099 filePathAttr := formattedOptionalAttribute("file", &filePath)
Pedro Loureiroba6682f2021-10-29 09:32:32 +00003100 implicitFromAttr := formattedOptionalSdkLevelAttribute(ctx, "on-bootclasspath-since", module.properties.On_bootclasspath_since)
3101 implicitUntilAttr := formattedOptionalSdkLevelAttribute(ctx, "on-bootclasspath-before", module.properties.On_bootclasspath_before)
3102 minSdkAttr := formattedOptionalSdkLevelAttribute(ctx, "min-device-sdk", module.properties.Min_device_sdk)
3103 maxSdkAttr := formattedOptionalSdkLevelAttribute(ctx, "max-device-sdk", module.properties.Max_device_sdk)
Jamie Garsidee570ace2023-11-27 12:07:36 +00003104 dependenciesAttr := formattedDependenciesAttribute(module.properties.Uses_libs_dependencies)
Pedro Loureiro196d3e62021-12-22 19:53:01 +00003105 // <library> is understood in all android versions whereas <apex-library> is only understood from API T (and ignored before that).
3106 // 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 +00003107 var libraryTag string
3108 if module.properties.Min_device_sdk != nil {
Pedro Loureiro196d3e62021-12-22 19:53:01 +00003109 libraryTag = ` <apex-library\n`
Pedro Loureiroc3621422021-09-28 15:40:23 +00003110 } else {
3111 libraryTag = ` <library\n`
3112 }
Pedro Loureiro9956e5e2021-09-07 17:21:59 +00003113
3114 return strings.Join([]string{
3115 `<?xml version=\"1.0\" encoding=\"utf-8\"?>\n`,
3116 `<!-- Copyright (C) 2018 The Android Open Source Project\n`,
3117 `\n`,
3118 ` Licensed under the Apache License, Version 2.0 (the \"License\");\n`,
3119 ` you may not use this file except in compliance with the License.\n`,
3120 ` You may obtain a copy of the License at\n`,
3121 `\n`,
3122 ` http://www.apache.org/licenses/LICENSE-2.0\n`,
3123 `\n`,
3124 ` Unless required by applicable law or agreed to in writing, software\n`,
3125 ` distributed under the License is distributed on an \"AS IS\" BASIS,\n`,
3126 ` WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\n`,
3127 ` See the License for the specific language governing permissions and\n`,
3128 ` limitations under the License.\n`,
3129 `-->\n`,
3130 `<permissions>\n`,
Pedro Loureiroc3621422021-09-28 15:40:23 +00003131 libraryTag,
Pedro Loureiro9956e5e2021-09-07 17:21:59 +00003132 libNameAttr,
3133 filePathAttr,
3134 implicitFromAttr,
3135 implicitUntilAttr,
3136 minSdkAttr,
3137 maxSdkAttr,
Jamie Garsidee570ace2023-11-27 12:07:36 +00003138 dependenciesAttr,
Pedro Loureiro9956e5e2021-09-07 17:21:59 +00003139 ` />\n`,
3140 `</permissions>\n`}, "")
3141}
3142
Jiyong Parke3833882020-02-17 17:28:10 +09003143func (module *sdkLibraryXml) GenerateAndroidBuildActions(ctx android.ModuleContext) {
Colin Crossff694a82023-12-13 15:54:49 -08003144 apexInfo, _ := android.ModuleProvider(ctx, android.ApexInfoProvider)
3145 module.hideApexVariantFromMake = !apexInfo.IsForPlatform()
Colin Cross56a83212020-09-15 18:30:11 -07003146
Jiyong Parke3833882020-02-17 17:28:10 +09003147 libName := proptools.String(module.properties.Lib_name)
Pedro Loureiroc3621422021-09-28 15:40:23 +00003148 module.selfValidate(ctx)
Pedro Loureiro9956e5e2021-09-07 17:21:59 +00003149 xmlContent := module.permissionsContents(ctx)
Jiyong Parke3833882020-02-17 17:28:10 +09003150
3151 module.outputFilePath = android.PathForModuleOut(ctx, libName+".xml").OutputPath
Colin Crossf1a035e2020-11-16 17:32:30 -08003152 rule := android.NewRuleBuilder(pctx, ctx)
Jiyong Parke3833882020-02-17 17:28:10 +09003153 rule.Command().
3154 Text("/bin/bash -c \"echo -e '" + xmlContent + "'\" > ").
3155 Output(module.outputFilePath)
3156
Colin Crossf1a035e2020-11-16 17:32:30 -08003157 rule.Build("java_sdk_xml", "Permission XML")
Jiyong Parke3833882020-02-17 17:28:10 +09003158
3159 module.installDirPath = android.PathForModuleInstall(ctx, "etc", module.SubDir())
3160}
3161
3162func (module *sdkLibraryXml) AndroidMkEntries() []android.AndroidMkEntries {
Colin Cross56a83212020-09-15 18:30:11 -07003163 if module.hideApexVariantFromMake {
satayev8f088b02021-12-06 11:40:46 +00003164 return []android.AndroidMkEntries{{
Jiyong Parke3833882020-02-17 17:28:10 +09003165 Disabled: true,
3166 }}
3167 }
3168
satayev8f088b02021-12-06 11:40:46 +00003169 return []android.AndroidMkEntries{{
Jiyong Parke3833882020-02-17 17:28:10 +09003170 Class: "ETC",
3171 OutputFile: android.OptionalPathForPath(module.outputFilePath),
3172 ExtraEntries: []android.AndroidMkExtraEntriesFunc{
Colin Crossaa255532020-07-03 13:18:24 -07003173 func(ctx android.AndroidMkExtraEntriesContext, entries *android.AndroidMkEntries) {
Jiyong Parke3833882020-02-17 17:28:10 +09003174 entries.SetString("LOCAL_MODULE_TAGS", "optional")
Colin Crossc68db4b2021-11-11 18:59:15 -08003175 entries.SetString("LOCAL_MODULE_PATH", module.installDirPath.String())
Jiyong Parke3833882020-02-17 17:28:10 +09003176 entries.SetString("LOCAL_INSTALLED_MODULE_STEM", module.outputFilePath.Base())
3177 },
3178 },
3179 }}
3180}
Paul Duffindd46f712020-02-10 13:37:10 +00003181
Pedro Loureiroc3621422021-09-28 15:40:23 +00003182func (module *sdkLibraryXml) selfValidate(ctx android.ModuleContext) {
3183 module.validateAtLeastTAttributes(ctx)
3184 module.validateMinAndMaxDeviceSdk(ctx)
3185 module.validateMinMaxDeviceSdkAndModuleMinSdk(ctx)
3186 module.validateOnBootclasspathBeforeRequirements(ctx)
3187}
3188
3189func (module *sdkLibraryXml) validateAtLeastTAttributes(ctx android.ModuleContext) {
3190 t := android.ApiLevelOrPanic(ctx, "Tiramisu")
3191 module.attrAtLeastT(ctx, t, module.properties.Min_device_sdk, "min_device_sdk")
3192 module.attrAtLeastT(ctx, t, module.properties.Max_device_sdk, "max_device_sdk")
3193 module.attrAtLeastT(ctx, t, module.properties.On_bootclasspath_before, "on_bootclasspath_before")
3194 module.attrAtLeastT(ctx, t, module.properties.On_bootclasspath_since, "on_bootclasspath_since")
3195}
3196
3197func (module *sdkLibraryXml) attrAtLeastT(ctx android.ModuleContext, t android.ApiLevel, attr *string, attrName string) {
3198 if attr != nil {
3199 if level, err := android.ApiLevelFromUser(ctx, *attr); err == nil {
3200 // we will inform the user of invalid inputs when we try to write the
3201 // permissions xml file so we don't need to do it here
3202 if t.GreaterThan(level) {
3203 ctx.PropertyErrorf(attrName, "Attribute value needs to be at least T")
3204 }
3205 }
3206 }
3207}
3208
3209func (module *sdkLibraryXml) validateMinAndMaxDeviceSdk(ctx android.ModuleContext) {
3210 if module.properties.Min_device_sdk != nil && module.properties.Max_device_sdk != nil {
3211 min, minErr := android.ApiLevelFromUser(ctx, *module.properties.Min_device_sdk)
3212 max, maxErr := android.ApiLevelFromUser(ctx, *module.properties.Max_device_sdk)
3213 if minErr == nil && maxErr == nil {
3214 // we will inform the user of invalid inputs when we try to write the
3215 // permissions xml file so we don't need to do it here
3216 if min.GreaterThan(max) {
3217 ctx.ModuleErrorf("min_device_sdk can't be greater than max_device_sdk")
3218 }
3219 }
3220 }
3221}
3222
3223func (module *sdkLibraryXml) validateMinMaxDeviceSdkAndModuleMinSdk(ctx android.ModuleContext) {
3224 moduleMinApi := android.ApiLevelOrPanic(ctx, *module.properties.Sdk_library_min_api_level)
3225 if module.properties.Min_device_sdk != nil {
3226 api, err := android.ApiLevelFromUser(ctx, *module.properties.Min_device_sdk)
3227 if err == nil {
3228 if moduleMinApi.GreaterThan(api) {
3229 ctx.PropertyErrorf("min_device_sdk", "Can't be less than module's min sdk (%s)", moduleMinApi)
3230 }
3231 }
3232 }
3233 if module.properties.Max_device_sdk != nil {
3234 api, err := android.ApiLevelFromUser(ctx, *module.properties.Max_device_sdk)
3235 if err == nil {
3236 if moduleMinApi.GreaterThan(api) {
3237 ctx.PropertyErrorf("max_device_sdk", "Can't be less than module's min sdk (%s)", moduleMinApi)
3238 }
3239 }
3240 }
3241}
3242
3243func (module *sdkLibraryXml) validateOnBootclasspathBeforeRequirements(ctx android.ModuleContext) {
3244 moduleMinApi := android.ApiLevelOrPanic(ctx, *module.properties.Sdk_library_min_api_level)
3245 if module.properties.On_bootclasspath_before != nil {
3246 t := android.ApiLevelOrPanic(ctx, "Tiramisu")
3247 // if we use the attribute, then we need to do this validation
3248 if moduleMinApi.LessThan(t) {
3249 // if minAPi is < T, then we need to have min_device_sdk (which only accepts T+)
3250 if module.properties.Min_device_sdk == nil {
3251 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")
3252 }
3253 }
3254 }
3255}
3256
Paul Duffindd46f712020-02-10 13:37:10 +00003257type sdkLibrarySdkMemberType struct {
3258 android.SdkMemberTypeBase
3259}
3260
Paul Duffin296701e2021-07-14 10:29:36 +01003261func (s *sdkLibrarySdkMemberType) AddDependencies(ctx android.SdkDependencyContext, dependencyTag blueprint.DependencyTag, names []string) {
3262 ctx.AddVariationDependencies(nil, dependencyTag, names...)
Paul Duffindd46f712020-02-10 13:37:10 +00003263}
3264
3265func (s *sdkLibrarySdkMemberType) IsInstance(module android.Module) bool {
3266 _, ok := module.(*SdkLibrary)
3267 return ok
3268}
3269
3270func (s *sdkLibrarySdkMemberType) AddPrebuiltModule(ctx android.SdkMemberContext, member android.SdkMember) android.BpModule {
3271 return ctx.SnapshotBuilder().AddPrebuiltModule(member, "java_sdk_library_import")
3272}
3273
3274func (s *sdkLibrarySdkMemberType) CreateVariantPropertiesStruct() android.SdkMemberProperties {
3275 return &sdkLibrarySdkMemberProperties{}
3276}
3277
Paul Duffin976b0e52021-04-27 23:20:26 +01003278var javaSdkLibrarySdkMemberType = &sdkLibrarySdkMemberType{
3279 android.SdkMemberTypeBase{
3280 PropertyName: "java_sdk_libs",
3281 SupportsSdk: true,
3282 },
3283}
3284
Paul Duffindd46f712020-02-10 13:37:10 +00003285type sdkLibrarySdkMemberProperties struct {
3286 android.SdkMemberPropertiesBase
3287
Paul Duffine8409952022-09-22 16:24:46 +01003288 // Stem name for files in the sdk snapshot.
3289 //
3290 // This is used to construct the path names of various sdk library files in the sdk snapshot to
3291 // make sure that they match the finalized versions of those files in prebuilts/sdk.
3292 //
3293 // This property is marked as keep so that it will be kept in all instances of this struct, will
3294 // not be cleared but will be copied to common structs. That is needed because this field is used
3295 // to construct many file names for other parts of this struct and so it needs to be present in
3296 // all structs. If it was not marked as keep then it would be cleared in some structs and so would
3297 // be unavailable for generating file names if there were other properties that were still set.
3298 Stem string `sdk:"keep"`
3299
Paul Duffindd46f712020-02-10 13:37:10 +00003300 // Scope to per scope properties.
Paul Duffin106a3a42022-01-27 16:39:06 +00003301 Scopes map[*apiScope]*scopeProperties
Paul Duffindd46f712020-02-10 13:37:10 +00003302
Paul Duffin3d1248c2020-04-09 00:10:17 +01003303 // The Java stubs source files.
3304 Stub_srcs []string
Paul Duffinf7a64332020-05-13 16:54:55 +01003305
3306 // The naming scheme.
3307 Naming_scheme *string
Paul Duffind7eb1c22020-05-26 20:57:10 +01003308
3309 // True if the java_sdk_library_import is for a shared library, false
3310 // otherwise.
3311 Shared_library *bool
Paul Duffina2ae7e02020-09-11 11:55:00 +01003312
Paul Duffin1267d872021-04-16 17:21:36 +01003313 // True if the stub imports should produce dex jars.
3314 Compile_dex *bool
3315
Paul Duffina2ae7e02020-09-11 11:55:00 +01003316 // The paths to the doctag files to add to the prebuilt.
3317 Doctag_paths android.Paths
Paul Duffin869de142021-07-15 14:14:41 +01003318
3319 Permitted_packages []string
Pedro Loureiro9956e5e2021-09-07 17:21:59 +00003320
3321 // Signals that this shared library is part of the bootclasspath starting
3322 // on the version indicated in this attribute.
3323 //
3324 // This will make platforms at this level and above to ignore
3325 // <uses-library> tags with this library name because the library is already
3326 // available
3327 On_bootclasspath_since *string
3328
3329 // Signals that this shared library was part of the bootclasspath before
3330 // (but not including) the version indicated in this attribute.
3331 //
3332 // The system will automatically add a <uses-library> tag with this library to
3333 // apps that target any SDK less than the version indicated in this attribute.
3334 On_bootclasspath_before *string
3335
3336 // Indicates that PackageManager should ignore this shared library if the
3337 // platform is below the version indicated in this attribute.
3338 //
3339 // This means that the device won't recognise this library as installed.
3340 Min_device_sdk *string
3341
3342 // Indicates that PackageManager should ignore this shared library if the
3343 // platform is above the version indicated in this attribute.
3344 //
3345 // This means that the device won't recognise this library as installed.
3346 Max_device_sdk *string
Jiakai Zhang9c4dc192023-02-09 00:09:24 +08003347
3348 DexPreoptProfileGuided *bool `supported_build_releases:"UpsideDownCake+"`
Paul Duffindd46f712020-02-10 13:37:10 +00003349}
3350
3351type scopeProperties struct {
Paul Duffin1fd005d2020-04-09 01:08:11 +01003352 Jars android.Paths
3353 StubsSrcJar android.Path
3354 CurrentApiFile android.Path
3355 RemovedApiFile android.Path
Paul Duffine7babdb2022-02-10 13:06:54 +00003356 AnnotationsZip android.Path `supported_build_releases:"Tiramisu+"`
Paul Duffin1fd005d2020-04-09 01:08:11 +01003357 SdkVersion string
Paul Duffindd46f712020-02-10 13:37:10 +00003358}
3359
3360func (s *sdkLibrarySdkMemberProperties) PopulateFromVariant(ctx android.SdkMemberContext, variant android.Module) {
3361 sdk := variant.(*SdkLibrary)
3362
Paul Duffine8409952022-09-22 16:24:46 +01003363 // Copy the stem name for files in the sdk snapshot.
3364 s.Stem = sdk.distStem()
3365
Paul Duffin106a3a42022-01-27 16:39:06 +00003366 s.Scopes = make(map[*apiScope]*scopeProperties)
Paul Duffindd46f712020-02-10 13:37:10 +00003367 for _, apiScope := range allApiScopes {
Paul Duffin803a9562020-05-20 11:52:25 +01003368 paths := sdk.findScopePaths(apiScope)
3369 if paths == nil {
3370 continue
3371 }
3372
Paul Duffindd46f712020-02-10 13:37:10 +00003373 jars := paths.stubsImplPath
3374 if len(jars) > 0 {
3375 properties := scopeProperties{}
3376 properties.Jars = jars
Paul Duffin780c5f42020-05-12 15:52:55 +01003377 properties.SdkVersion = sdk.sdkVersionForStubsLibrary(ctx.SdkModuleContext(), apiScope)
Paul Duffin0f8faff2020-05-20 16:18:00 +01003378 properties.StubsSrcJar = paths.stubsSrcJar.Path()
Paul Duffin10269f12020-06-19 18:39:55 +01003379 if paths.currentApiFilePath.Valid() {
3380 properties.CurrentApiFile = paths.currentApiFilePath.Path()
3381 }
3382 if paths.removedApiFilePath.Valid() {
3383 properties.RemovedApiFile = paths.removedApiFilePath.Path()
3384 }
Anton Hanssond78eb762021-09-21 15:25:12 +01003385 // The annotations zip is only available for modules that set annotations_enabled: true.
3386 if paths.annotationsZip.Valid() {
3387 properties.AnnotationsZip = paths.annotationsZip.Path()
3388 }
Paul Duffin106a3a42022-01-27 16:39:06 +00003389 s.Scopes[apiScope] = &properties
Paul Duffindd46f712020-02-10 13:37:10 +00003390 }
3391 }
3392
Paul Duffindfa131e2020-05-15 20:37:11 +01003393 s.Naming_scheme = sdk.commonSdkLibraryProperties.Naming_scheme
Paul Duffind7eb1c22020-05-26 20:57:10 +01003394 s.Shared_library = proptools.BoolPtr(sdk.sharedLibrary())
Paul Duffin1267d872021-04-16 17:21:36 +01003395 s.Compile_dex = sdk.dexProperties.Compile_dex
Paul Duffina2ae7e02020-09-11 11:55:00 +01003396 s.Doctag_paths = sdk.doctagPaths
Paul Duffin869de142021-07-15 14:14:41 +01003397 s.Permitted_packages = sdk.PermittedPackagesForUpdatableBootJars()
Pedro Loureiro9956e5e2021-09-07 17:21:59 +00003398 s.On_bootclasspath_since = sdk.commonSdkLibraryProperties.On_bootclasspath_since
3399 s.On_bootclasspath_before = sdk.commonSdkLibraryProperties.On_bootclasspath_before
3400 s.Min_device_sdk = sdk.commonSdkLibraryProperties.Min_device_sdk
3401 s.Max_device_sdk = sdk.commonSdkLibraryProperties.Max_device_sdk
Jiakai Zhang9c4dc192023-02-09 00:09:24 +08003402
3403 if sdk.dexpreopter.dexpreoptProperties.Dex_preopt_result.Profile_guided {
3404 s.DexPreoptProfileGuided = proptools.BoolPtr(true)
3405 }
Paul Duffindd46f712020-02-10 13:37:10 +00003406}
3407
3408func (s *sdkLibrarySdkMemberProperties) AddToPropertySet(ctx android.SdkMemberContext, propertySet android.BpPropertySet) {
Paul Duffinf7a64332020-05-13 16:54:55 +01003409 if s.Naming_scheme != nil {
3410 propertySet.AddProperty("naming_scheme", proptools.String(s.Naming_scheme))
3411 }
Paul Duffind7eb1c22020-05-26 20:57:10 +01003412 if s.Shared_library != nil {
3413 propertySet.AddProperty("shared_library", *s.Shared_library)
3414 }
Paul Duffin1267d872021-04-16 17:21:36 +01003415 if s.Compile_dex != nil {
3416 propertySet.AddProperty("compile_dex", *s.Compile_dex)
3417 }
Paul Duffin869de142021-07-15 14:14:41 +01003418 if len(s.Permitted_packages) > 0 {
3419 propertySet.AddProperty("permitted_packages", s.Permitted_packages)
3420 }
Jiakai Zhang9c4dc192023-02-09 00:09:24 +08003421 dexPreoptSet := propertySet.AddPropertySet("dex_preopt")
3422 if s.DexPreoptProfileGuided != nil {
3423 dexPreoptSet.AddProperty("profile_guided", proptools.Bool(s.DexPreoptProfileGuided))
3424 }
Paul Duffinf7a64332020-05-13 16:54:55 +01003425
Paul Duffine8409952022-09-22 16:24:46 +01003426 stem := s.Stem
3427
Paul Duffindd46f712020-02-10 13:37:10 +00003428 for _, apiScope := range allApiScopes {
3429 if properties, ok := s.Scopes[apiScope]; ok {
Paul Duffin6b836ba2020-05-13 19:19:49 +01003430 scopeSet := propertySet.AddPropertySet(apiScope.propertyName)
Paul Duffindd46f712020-02-10 13:37:10 +00003431
Paul Duffin958806b2022-05-16 13:10:47 +00003432 scopeDir := apiScope.snapshotRelativeDir()
Paul Duffin3d1248c2020-04-09 00:10:17 +01003433
Paul Duffindd46f712020-02-10 13:37:10 +00003434 var jars []string
3435 for _, p := range properties.Jars {
Paul Duffine8409952022-09-22 16:24:46 +01003436 dest := filepath.Join(scopeDir, stem+"-stubs.jar")
Paul Duffindd46f712020-02-10 13:37:10 +00003437 ctx.SnapshotBuilder().CopyToSnapshot(p, dest)
3438 jars = append(jars, dest)
3439 }
3440 scopeSet.AddProperty("jars", jars)
3441
Paul Duffin22628d52021-05-12 23:13:22 +01003442 if ctx.SdkModuleContext().Config().IsEnvTrue("SOONG_SDK_SNAPSHOT_USE_SRCJAR") {
3443 // Copy the stubs source jar into the snapshot zip as is.
Paul Duffine8409952022-09-22 16:24:46 +01003444 srcJarSnapshotPath := filepath.Join(scopeDir, stem+".srcjar")
Paul Duffin22628d52021-05-12 23:13:22 +01003445 ctx.SnapshotBuilder().CopyToSnapshot(properties.StubsSrcJar, srcJarSnapshotPath)
3446 scopeSet.AddProperty("stub_srcs", []string{srcJarSnapshotPath})
3447 } else {
3448 // Merge the stubs source jar into the snapshot zip so that when it is unpacked
3449 // the source files are also unpacked.
Paul Duffine8409952022-09-22 16:24:46 +01003450 snapshotRelativeDir := filepath.Join(scopeDir, stem+"_stub_sources")
Paul Duffin22628d52021-05-12 23:13:22 +01003451 ctx.SnapshotBuilder().UnzipToSnapshot(properties.StubsSrcJar, snapshotRelativeDir)
3452 scopeSet.AddProperty("stub_srcs", []string{snapshotRelativeDir})
3453 }
Paul Duffin3d1248c2020-04-09 00:10:17 +01003454
Paul Duffin1fd005d2020-04-09 01:08:11 +01003455 if properties.CurrentApiFile != nil {
Paul Duffine8409952022-09-22 16:24:46 +01003456 currentApiSnapshotPath := apiScope.snapshotRelativeCurrentApiTxtPath(stem)
Paul Duffin1fd005d2020-04-09 01:08:11 +01003457 ctx.SnapshotBuilder().CopyToSnapshot(properties.CurrentApiFile, currentApiSnapshotPath)
3458 scopeSet.AddProperty("current_api", currentApiSnapshotPath)
3459 }
3460
3461 if properties.RemovedApiFile != nil {
Paul Duffine8409952022-09-22 16:24:46 +01003462 removedApiSnapshotPath := apiScope.snapshotRelativeRemovedApiTxtPath(stem)
Paul Duffin3dbf9fd2020-06-02 13:00:02 +01003463 ctx.SnapshotBuilder().CopyToSnapshot(properties.RemovedApiFile, removedApiSnapshotPath)
Paul Duffin1fd005d2020-04-09 01:08:11 +01003464 scopeSet.AddProperty("removed_api", removedApiSnapshotPath)
3465 }
3466
Anton Hanssond78eb762021-09-21 15:25:12 +01003467 if properties.AnnotationsZip != nil {
Paul Duffine8409952022-09-22 16:24:46 +01003468 annotationsSnapshotPath := filepath.Join(scopeDir, stem+"_annotations.zip")
Anton Hanssond78eb762021-09-21 15:25:12 +01003469 ctx.SnapshotBuilder().CopyToSnapshot(properties.AnnotationsZip, annotationsSnapshotPath)
3470 scopeSet.AddProperty("annotations", annotationsSnapshotPath)
3471 }
3472
Paul Duffindd46f712020-02-10 13:37:10 +00003473 if properties.SdkVersion != "" {
3474 scopeSet.AddProperty("sdk_version", properties.SdkVersion)
3475 }
3476 }
3477 }
3478
Paul Duffina2ae7e02020-09-11 11:55:00 +01003479 if len(s.Doctag_paths) > 0 {
3480 dests := []string{}
3481 for _, p := range s.Doctag_paths {
3482 dest := filepath.Join("doctags", p.Rel())
3483 ctx.SnapshotBuilder().CopyToSnapshot(p, dest)
3484 dests = append(dests, dest)
3485 }
3486 propertySet.AddProperty("doctag_files", dests)
3487 }
Paul Duffindd46f712020-02-10 13:37:10 +00003488}