blob: 591ec05d0907227117e9669b2483a5a37f201f52 [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
Paul Duffind1b3a922020-01-22 11:57:20 +0000158}
159
160// Initialize a scope, creating and adding appropriate dependency tags
161func initApiScope(scope *apiScope) *apiScope {
Paul Duffinc8782502020-04-29 20:45:27 +0100162 name := scope.name
Paul Duffin46dc45a2020-05-14 15:39:10 +0100163 scopeByName[name] = scope
164 allScopeNames = append(allScopeNames, name)
Paul Duffin6b836ba2020-05-13 19:19:49 +0100165 scope.propertyName = strings.ReplaceAll(name, "-", "_")
166 scope.fieldName = proptools.FieldNameForProperty(scope.propertyName)
Paul Duffind1b3a922020-01-22 11:57:20 +0000167 scope.stubsTag = scopeDependencyTag{
Paul Duffinc8782502020-04-29 20:45:27 +0100168 name: name + "-stubs",
169 apiScope: scope,
170 depInfoExtractor: (*scopePaths).extractStubsLibraryInfoFromDependency,
Paul Duffind1b3a922020-01-22 11:57:20 +0000171 }
Paul Duffin0ff08bd2020-04-29 13:30:54 +0100172 scope.stubsSourceTag = scopeDependencyTag{
173 name: name + "-stubs-source",
174 apiScope: scope,
175 depInfoExtractor: (*scopePaths).extractStubsSourceInfoFromDep,
176 }
177 scope.apiFileTag = scopeDependencyTag{
178 name: name + "-api",
179 apiScope: scope,
180 depInfoExtractor: (*scopePaths).extractApiInfoFromDep,
181 }
Paul Duffinc8782502020-04-29 20:45:27 +0100182 scope.stubsSourceAndApiTag = scopeDependencyTag{
183 name: name + "-stubs-source-and-api",
184 apiScope: scope,
185 depInfoExtractor: (*scopePaths).extractStubsSourceAndApiInfoFromApiStubsProvider,
Paul Duffind1b3a922020-01-22 11:57:20 +0000186 }
Paul Duffin958806b2022-05-16 13:10:47 +0000187 scope.latestApiModuleTag = scopeDependencyTag{
188 name: name + "-latest-api",
189 apiScope: scope,
190 depInfoExtractor: (*scopePaths).extractLatestApiPath,
191 }
192 scope.latestRemovedApiModuleTag = scopeDependencyTag{
193 name: name + "-latest-removed-api",
194 apiScope: scope,
195 depInfoExtractor: (*scopePaths).extractLatestRemovedApiPath,
196 }
Paul Duffin0ff08bd2020-04-29 13:30:54 +0100197
198 // To get the args needed to generate the stubs source append all the args from
199 // this scope and all the scopes it extends as each set of args adds additional
200 // members to the stubs.
Paul Duffin15f34ef2020-07-20 18:04:44 +0100201 var scopeSpecificArgs []string
202 if scope.annotation != "" {
203 scopeSpecificArgs = []string{"--show-annotation", scope.annotation}
Paul Duffin0ff08bd2020-04-29 13:30:54 +0100204 }
Paul Duffin15f34ef2020-07-20 18:04:44 +0100205 for s := scope; s != nil; s = s.extends {
206 scopeSpecificArgs = append(scopeSpecificArgs, s.extraArgs...)
Paul Duffin0ff08bd2020-04-29 13:30:54 +0100207
Paul Duffin15f34ef2020-07-20 18:04:44 +0100208 // Ensure that the generated stubs includes all the API elements from the API scope
209 // that this scope extends.
210 if s != scope && s.annotation != "" {
211 scopeSpecificArgs = append(scopeSpecificArgs, "--show-for-stub-purposes-annotation", s.annotation)
212 }
213 }
Paul Duffin0ff08bd2020-04-29 13:30:54 +0100214
Paul Duffind0b9fca2022-09-30 18:11:41 +0100215 // By default, a library that can access a scope can also access the scope it extends.
216 if scope.canAccess == nil {
217 scope.canAccess = scope.extends
218 }
219
Paul Duffin15f34ef2020-07-20 18:04:44 +0100220 // Escape any special characters in the arguments. This is needed because droidstubs
221 // passes these directly to the shell command.
222 scope.droidstubsArgs = proptools.ShellEscapeList(scopeSpecificArgs)
Paul Duffin0ff08bd2020-04-29 13:30:54 +0100223
Paul Duffind1b3a922020-01-22 11:57:20 +0000224 return scope
225}
226
Anton Hansson08f476b2021-04-07 15:32:19 +0100227func (scope *apiScope) stubsLibraryModuleNameSuffix() string {
228 return ".stubs" + scope.moduleSuffix
229}
230
Paul Duffinc3091c82020-05-08 14:16:20 +0100231func (scope *apiScope) stubsLibraryModuleName(baseName string) string {
Anton Hansson08f476b2021-04-07 15:32:19 +0100232 return baseName + scope.stubsLibraryModuleNameSuffix()
Paul Duffind1b3a922020-01-22 11:57:20 +0000233}
234
Paul Duffinc8782502020-04-29 20:45:27 +0100235func (scope *apiScope) stubsSourceModuleName(baseName string) string {
Paul Duffindd9d0742020-05-08 15:52:37 +0100236 return baseName + ".stubs.source" + scope.moduleSuffix
Paul Duffind1b3a922020-01-22 11:57:20 +0000237}
238
Paul Duffin0ff08bd2020-04-29 13:30:54 +0100239func (scope *apiScope) apiModuleName(baseName string) string {
Paul Duffindd9d0742020-05-08 15:52:37 +0100240 return baseName + ".api" + scope.moduleSuffix
Paul Duffin0ff08bd2020-04-29 13:30:54 +0100241}
242
Paul Duffin3375e352020-04-28 10:44:03 +0100243func (scope *apiScope) String() string {
244 return scope.name
245}
246
Paul Duffin958806b2022-05-16 13:10:47 +0000247// snapshotRelativeDir returns the snapshot directory into which the files related to scopes will
248// be stored.
249func (scope *apiScope) snapshotRelativeDir() string {
250 return filepath.Join("sdk_library", scope.name)
251}
252
253// snapshotRelativeCurrentApiTxtPath returns the snapshot path to the API .txt file for the named
254// library.
255func (scope *apiScope) snapshotRelativeCurrentApiTxtPath(name string) string {
256 return filepath.Join(scope.snapshotRelativeDir(), name+".txt")
257}
258
259// snapshotRelativeRemovedApiTxtPath returns the snapshot path to the removed API .txt file for the
260// named library.
261func (scope *apiScope) snapshotRelativeRemovedApiTxtPath(name string) string {
262 return filepath.Join(scope.snapshotRelativeDir(), name+"-removed.txt")
263}
264
Paul Duffind1b3a922020-01-22 11:57:20 +0000265type apiScopes []*apiScope
266
267func (scopes apiScopes) Strings(accessor func(*apiScope) string) []string {
268 var list []string
269 for _, scope := range scopes {
270 list = append(list, accessor(scope))
271 }
272 return list
273}
274
Jiyong Parkc678ad32018-04-10 13:07:10 +0900275var (
Paul Duffin46dc45a2020-05-14 15:39:10 +0100276 scopeByName = make(map[string]*apiScope)
277 allScopeNames []string
Paul Duffind1b3a922020-01-22 11:57:20 +0000278 apiScopePublic = initApiScope(&apiScope{
Paul Duffin3375e352020-04-28 10:44:03 +0100279 name: "public",
280
281 // Public scope is enabled by default for both legacy and non-legacy modes.
282 legacyEnabledStatus: func(module *SdkLibrary) bool {
283 return true
284 },
285 defaultEnabledStatus: true,
286
287 scopeSpecificProperties: func(module *SdkLibrary) *ApiScopeProperties {
288 return &module.sdkLibraryProperties.Public
289 },
Paul Duffind1b3a922020-01-22 11:57:20 +0000290 sdkVersion: "current",
291 })
292 apiScopeSystem = initApiScope(&apiScope{
Paul Duffin3375e352020-04-28 10:44:03 +0100293 name: "system",
294 extends: apiScopePublic,
295 legacyEnabledStatus: (*SdkLibrary).generateTestAndSystemScopesByDefault,
296 scopeSpecificProperties: func(module *SdkLibrary) *ApiScopeProperties {
297 return &module.sdkLibraryProperties.System
298 },
Paul Duffin15f34ef2020-07-20 18:04:44 +0100299 apiFilePrefix: "system-",
300 moduleSuffix: ".system",
301 sdkVersion: "system_current",
302 annotation: "android.annotation.SystemApi(client=android.annotation.SystemApi.Client.PRIVILEGED_APPS)",
Paul Duffind1b3a922020-01-22 11:57:20 +0000303 })
304 apiScopeTest = initApiScope(&apiScope{
Paul Duffin3375e352020-04-28 10:44:03 +0100305 name: "test",
Anton Hansson4fe970f2020-10-09 10:16:49 +0100306 extends: apiScopeSystem,
Paul Duffin3375e352020-04-28 10:44:03 +0100307 legacyEnabledStatus: (*SdkLibrary).generateTestAndSystemScopesByDefault,
308 scopeSpecificProperties: func(module *SdkLibrary) *ApiScopeProperties {
309 return &module.sdkLibraryProperties.Test
310 },
Paul Duffin15f34ef2020-07-20 18:04:44 +0100311 apiFilePrefix: "test-",
312 moduleSuffix: ".test",
313 sdkVersion: "test_current",
314 annotation: "android.annotation.TestApi",
315 unstable: true,
Paul Duffind1b3a922020-01-22 11:57:20 +0000316 })
Paul Duffin8f265b92020-04-28 14:13:56 +0100317 apiScopeModuleLib = initApiScope(&apiScope{
Paul Duffin6b836ba2020-05-13 19:19:49 +0100318 name: "module-lib",
Paul Duffin8f265b92020-04-28 14:13:56 +0100319 extends: apiScopeSystem,
Paul Duffin0c5bae52020-06-02 13:00:08 +0100320 // The module-lib scope is disabled by default in legacy mode.
Paul Duffin8f265b92020-04-28 14:13:56 +0100321 //
322 // Enabling this would break existing usages.
323 legacyEnabledStatus: func(module *SdkLibrary) bool {
324 return false
325 },
326 scopeSpecificProperties: func(module *SdkLibrary) *ApiScopeProperties {
327 return &module.sdkLibraryProperties.Module_lib
328 },
329 apiFilePrefix: "module-lib-",
330 moduleSuffix: ".module_lib",
331 sdkVersion: "module_current",
Paul Duffin15f34ef2020-07-20 18:04:44 +0100332 annotation: "android.annotation.SystemApi(client=android.annotation.SystemApi.Client.MODULE_LIBRARIES)",
Paul Duffin8f265b92020-04-28 14:13:56 +0100333 })
Paul Duffin0c5bae52020-06-02 13:00:08 +0100334 apiScopeSystemServer = initApiScope(&apiScope{
335 name: "system-server",
336 extends: apiScopePublic,
Paul Duffind0b9fca2022-09-30 18:11:41 +0100337
338 // The system-server scope can access the module-lib scope.
339 //
340 // A module that provides a system-server API is appended to the standard bootclasspath that is
341 // used by the system server. So, it should be able to access module-lib APIs provided by
342 // libraries on the bootclasspath.
343 canAccess: apiScopeModuleLib,
344
Paul Duffin0c5bae52020-06-02 13:00:08 +0100345 // The system-server scope is disabled by default in legacy mode.
346 //
347 // Enabling this would break existing usages.
348 legacyEnabledStatus: func(module *SdkLibrary) bool {
349 return false
350 },
351 scopeSpecificProperties: func(module *SdkLibrary) *ApiScopeProperties {
352 return &module.sdkLibraryProperties.System_server
353 },
354 apiFilePrefix: "system-server-",
355 moduleSuffix: ".system_server",
356 sdkVersion: "system_server_current",
Paul Duffin15f34ef2020-07-20 18:04:44 +0100357 annotation: "android.annotation.SystemApi(client=android.annotation.SystemApi.Client.SYSTEM_SERVER)",
358 extraArgs: []string{
359 "--hide-annotation", "android.annotation.Hide",
Paul Duffin0c5bae52020-06-02 13:00:08 +0100360 // com.android.* classes are okay in this interface"
Paul Duffin15f34ef2020-07-20 18:04:44 +0100361 "--hide", "InternalClasses",
Paul Duffin0c5bae52020-06-02 13:00:08 +0100362 },
363 })
Paul Duffind1b3a922020-01-22 11:57:20 +0000364 allApiScopes = apiScopes{
365 apiScopePublic,
366 apiScopeSystem,
367 apiScopeTest,
Paul Duffin8f265b92020-04-28 14:13:56 +0100368 apiScopeModuleLib,
Paul Duffin0c5bae52020-06-02 13:00:08 +0100369 apiScopeSystemServer,
Paul Duffind1b3a922020-01-22 11:57:20 +0000370 }
Jiyong Parkc678ad32018-04-10 13:07:10 +0900371)
372
Jiyong Park82484c02018-04-23 21:41:26 +0900373var (
374 javaSdkLibrariesLock sync.Mutex
375)
376
Jiyong Parkc678ad32018-04-10 13:07:10 +0900377// TODO: these are big features that are currently missing
Jiyong Park1be96912018-05-28 18:02:19 +0900378// 1) disallowing linking to the runtime shared lib
379// 2) HTML generation
Jiyong Parkc678ad32018-04-10 13:07:10 +0900380
381func init() {
Paul Duffin43dc1cc2019-12-19 11:18:54 +0000382 RegisterSdkLibraryBuildComponents(android.InitRegistrationContext)
Jiyong Parkc678ad32018-04-10 13:07:10 +0900383
Jiyong Park82484c02018-04-23 21:41:26 +0900384 android.RegisterMakeVarsProvider(pctx, func(ctx android.MakeVarsContext) {
385 javaSdkLibraries := javaSdkLibraries(ctx.Config())
386 sort.Strings(*javaSdkLibraries)
387 ctx.Strict("JAVA_SDK_LIBRARIES", strings.Join(*javaSdkLibraries, " "))
388 })
Paul Duffindd46f712020-02-10 13:37:10 +0000389
390 // Register sdk member types.
Paul Duffin976b0e52021-04-27 23:20:26 +0100391 android.RegisterSdkMemberType(javaSdkLibrarySdkMemberType)
Jiyong Parkc678ad32018-04-10 13:07:10 +0900392}
393
Paul Duffin43dc1cc2019-12-19 11:18:54 +0000394func RegisterSdkLibraryBuildComponents(ctx android.RegistrationContext) {
395 ctx.RegisterModuleType("java_sdk_library", SdkLibraryFactory)
396 ctx.RegisterModuleType("java_sdk_library_import", sdkLibraryImportFactory)
397}
398
Paul Duffin3375e352020-04-28 10:44:03 +0100399// Properties associated with each api scope.
400type ApiScopeProperties struct {
401 // Indicates whether the api surface is generated.
402 //
403 // If this is set for any scope then all scopes must explicitly specify if they
404 // are enabled. This is to prevent new usages from depending on legacy behavior.
405 //
406 // Otherwise, if this is not set for any scope then the default behavior is
407 // scope specific so please refer to the scope specific property documentation.
408 Enabled *bool
Paul Duffin87a05a32020-05-12 11:50:28 +0100409
410 // The sdk_version to use for building the stubs.
411 //
412 // If not specified then it will use an sdk_version determined as follows:
Trevor Radcliffedf8aa1f2021-11-04 14:25:39 +0000413 //
Paul Duffin87a05a32020-05-12 11:50:28 +0100414 // 1) If the sdk_version specified on the java_sdk_library is none then this
Trevor Radcliffedf8aa1f2021-11-04 14:25:39 +0000415 // will be none. This is used for java_sdk_library instances that are used
416 // to create stubs that contribute to the core_current sdk version.
417 // 2) Otherwise, it is assumed that this library extends but does not
418 // contribute directly to a specific sdk_version and so this uses the
419 // sdk_version appropriate for the api scope. e.g. public will use
420 // sdk_version: current, system will use sdk_version: system_current, etc.
Paul Duffin87a05a32020-05-12 11:50:28 +0100421 //
422 // This does not affect the sdk_version used for either generating the stubs source
423 // or the API file. They both have to use the same sdk_version as is used for
424 // compiling the implementation library.
425 Sdk_version *string
Paul Duffin3375e352020-04-28 10:44:03 +0100426}
427
Jiyong Parkc678ad32018-04-10 13:07:10 +0900428type sdkLibraryProperties struct {
Anton Hanssonf8ea3722021-09-16 14:24:13 +0100429 // List of source files that are needed to compile the API, but are not part of runtime library.
430 Api_srcs []string `android:"arch_variant"`
431
Paul Duffin5df79302020-05-16 15:52:12 +0100432 // Visibility for impl library module. If not specified then defaults to the
433 // visibility property.
434 Impl_library_visibility []string
435
Paul Duffin4911a892020-04-29 23:35:13 +0100436 // Visibility for stubs library modules. If not specified then defaults to the
437 // visibility property.
438 Stubs_library_visibility []string
439
440 // Visibility for stubs source modules. If not specified then defaults to the
441 // visibility property.
442 Stubs_source_visibility []string
443
Anton Hansson7f66efa2020-10-08 14:47:23 +0100444 // List of Java libraries that will be in the classpath when building the implementation lib
445 Impl_only_libs []string `android:"arch_variant"`
446
Paul Duffin77590a82022-04-28 14:13:30 +0000447 // List of Java libraries that will included in the implementation lib.
448 Impl_only_static_libs []string `android:"arch_variant"`
449
Sundong Ahnf043cf62018-06-25 16:04:37 +0900450 // List of Java libraries that will be in the classpath when building stubs
451 Stub_only_libs []string `android:"arch_variant"`
452
Anton Hanssondae54cd2021-04-21 16:30:10 +0100453 // List of Java libraries that will included in stub libraries
454 Stub_only_static_libs []string `android:"arch_variant"`
455
Paul Duffin7a586d32019-12-30 17:09:34 +0000456 // list of package names that will be documented and publicized as API.
457 // This allows the API to be restricted to a subset of the source files provided.
458 // If this is unspecified then all the source files will be treated as being part
459 // of the API.
Jiyong Parkc678ad32018-04-10 13:07:10 +0900460 Api_packages []string
461
Jiyong Park5a2c9d72018-05-01 22:25:41 +0900462 // list of package names that must be hidden from the API
463 Hidden_api_packages []string
464
Paul Duffin749f98f2019-12-30 17:23:46 +0000465 // the relative path to the directory containing the api specification files.
466 // Defaults to "api".
467 Api_dir *string
468
Paul Duffindfa131e2020-05-15 20:37:11 +0100469 // Determines whether a runtime implementation library is built; defaults to false.
470 //
471 // 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 +0200472 // it is as if shared_library: false, was set.
Paul Duffin43db9be2019-12-30 17:35:49 +0000473 Api_only *bool
474
Paul Duffin11512472019-02-11 15:55:17 +0000475 // local files that are used within user customized droiddoc options.
476 Droiddoc_option_files []string
477
Spandan Das93e95992021-07-29 18:26:39 +0000478 // additional droiddoc options.
Paul Duffin11512472019-02-11 15:55:17 +0000479 // Available variables for substitution:
480 //
481 // $(location <label>): the path to the droiddoc_option_files with name <label>
Sundong Ahndd567f92018-07-31 17:19:11 +0900482 Droiddoc_options []string
483
Paul Duffine22c2ab2020-05-20 19:35:27 +0100484 // is set to true, Metalava will allow framework SDK to contain annotations.
485 Annotations_enabled *bool
486
Sundong Ahn054b19a2018-10-19 13:46:09 +0900487 // a list of top-level directories containing files to merge qualifier annotations
488 // (i.e. those intended to be included in the stubs written) from.
489 Merge_annotations_dirs []string
490
491 // a list of top-level directories containing Java stub files to merge show/hide annotations from.
492 Merge_inclusion_annotations_dirs []string
493
Paul Duffin4f5c1ef2020-11-19 14:53:43 +0000494 // If set to true then don't create dist rules.
495 No_dist *bool
Sundong Ahn80a87b32019-05-13 15:02:50 +0900496
Paul Duffin31310252020-11-20 21:26:20 +0000497 // The stem for the artifacts that are copied to the dist, if not specified
498 // then defaults to the base module name.
499 //
500 // For each scope the following artifacts are copied to the apistubs/<scope>
501 // directory in the dist.
502 // * stubs impl jar -> <dist-stem>.jar
503 // * API specification file -> api/<dist-stem>.txt
504 // * Removed API specification file -> api/<dist-stem>-removed.txt
505 //
506 // Also used to construct the name of the filegroup (created by prebuilt_apis)
507 // that references the latest released API and remove API specification files.
508 // * API specification filegroup -> <dist-stem>.api.<scope>.latest
509 // * Removed API specification filegroup -> <dist-stem>-removed.api.<scope>.latest
Jaewoong Jung1a97ee02021-03-09 13:25:02 -0800510 // * API incompatibilities baseline filegroup -> <dist-stem>-incompatibilities.api.<scope>.latest
Paul Duffin31310252020-11-20 21:26:20 +0000511 Dist_stem *string
512
Colin Cross986b69a2021-06-01 13:13:40 -0700513 // The subdirectory for the artifacts that are copied to the dist directory. If not specified
Colin Cross3dd66252021-06-01 14:05:09 -0700514 // then defaults to "unknown". Should be set to "android" for anything that should be published
Colin Cross986b69a2021-06-01 13:13:40 -0700515 // in the public Android SDK.
516 Dist_group *string
517
Anton Hanssondff2c782020-12-21 17:10:01 +0000518 // A compatibility mode that allows historical API-tracking files to not exist.
519 // Do not use.
520 Unsafe_ignore_missing_latest_api bool
521
Paul Duffin3375e352020-04-28 10:44:03 +0100522 // indicates whether system and test apis should be generated.
523 Generate_system_and_test_apis bool `blueprint:"mutated"`
524
525 // The properties specific to the public api scope
526 //
527 // Unless explicitly specified by using public.enabled the public api scope is
528 // enabled by default in both legacy and non-legacy mode.
529 Public ApiScopeProperties
530
531 // The properties specific to the system api scope
532 //
533 // In legacy mode the system api scope is enabled by default when sdk_version
534 // is set to something other than "none".
535 //
536 // In non-legacy mode the system api scope is disabled by default.
537 System ApiScopeProperties
538
539 // The properties specific to the test api scope
540 //
541 // In legacy mode the test api scope is enabled by default when sdk_version
542 // is set to something other than "none".
543 //
544 // In non-legacy mode the test api scope is disabled by default.
545 Test ApiScopeProperties
Paul Duffin37e0b772019-12-30 17:20:10 +0000546
Paul Duffin0c5bae52020-06-02 13:00:08 +0100547 // The properties specific to the module-lib api scope
Paul Duffin8f265b92020-04-28 14:13:56 +0100548 //
Paul Duffin0c5bae52020-06-02 13:00:08 +0100549 // Unless explicitly specified by using test.enabled the module-lib api scope is
Paul Duffin8f265b92020-04-28 14:13:56 +0100550 // disabled by default.
551 Module_lib ApiScopeProperties
552
Paul Duffin0c5bae52020-06-02 13:00:08 +0100553 // The properties specific to the system-server api scope
554 //
555 // Unless explicitly specified by using test.enabled the module-lib api scope is
556 // disabled by default.
557 System_server ApiScopeProperties
558
Jiyong Park932cdfe2020-05-28 00:19:53 +0900559 // Determines if the stubs are preferred over the implementation library
560 // for linking, even when the client doesn't specify sdk_version. When this
561 // is set to true, such clients are provided with the widest API surface that
562 // this lib provides. Note however that this option doesn't affect the clients
563 // that are in the same APEX as this library. In that case, the clients are
564 // always linked with the implementation library. Default is false.
565 Default_to_stubs *bool
566
Paul Duffin160fe412020-05-10 19:32:20 +0100567 // Properties related to api linting.
568 Api_lint struct {
569 // Enable api linting.
570 Enabled *bool
571 }
572
Jiyong Parkc678ad32018-04-10 13:07:10 +0900573 // TODO: determines whether to create HTML doc or not
Paul Duffine8409952022-09-22 16:24:46 +0100574 // Html_doc *bool
Jiyong Parkc678ad32018-04-10 13:07:10 +0900575}
576
Paul Duffin0f8faff2020-05-20 16:18:00 +0100577// Paths to outputs from java_sdk_library and java_sdk_library_import.
578//
579// Fields that are android.Paths are always set (during GenerateAndroidBuildActions).
580// OptionalPaths are always set by java_sdk_library but may not be set by
581// java_sdk_library_import as not all instances provide that information.
Paul Duffind1b3a922020-01-22 11:57:20 +0000582type scopePaths struct {
Paul Duffin0f8faff2020-05-20 16:18:00 +0100583 // The path (represented as Paths for convenience when returning) to the stubs header jar.
584 //
585 // That is the jar that is created by turbine.
586 stubsHeaderPath android.Paths
587
588 // The path (represented as Paths for convenience when returning) to the stubs implementation jar.
589 //
590 // This is not the implementation jar, it still only contains stubs.
591 stubsImplPath android.Paths
592
Paul Duffin1267d872021-04-16 17:21:36 +0100593 // The dex jar for the stubs.
594 //
595 // This is not the implementation jar, it still only contains stubs.
Martin Stjernholm8be1e6d2021-09-15 03:34:04 +0100596 stubsDexJarPath OptionalDexJarPath
Paul Duffin1267d872021-04-16 17:21:36 +0100597
Paul Duffin0f8faff2020-05-20 16:18:00 +0100598 // The API specification file, e.g. system_current.txt.
599 currentApiFilePath android.OptionalPath
600
601 // The specification of API elements removed since the last release.
602 removedApiFilePath android.OptionalPath
603
604 // The stubs source jar.
605 stubsSrcJar android.OptionalPath
Anton Hanssond78eb762021-09-21 15:25:12 +0100606
607 // Extracted annotations.
608 annotationsZip android.OptionalPath
Paul Duffin958806b2022-05-16 13:10:47 +0000609
610 // The path to the latest API file.
611 latestApiPath android.OptionalPath
612
613 // The path to the latest removed API file.
614 latestRemovedApiPath android.OptionalPath
Paul Duffind1b3a922020-01-22 11:57:20 +0000615}
616
Colin Crossdcf71b22021-02-01 13:59:03 -0800617func (paths *scopePaths) extractStubsLibraryInfoFromDependency(ctx android.ModuleContext, dep android.Module) error {
618 if ctx.OtherModuleHasProvider(dep, JavaInfoProvider) {
619 lib := ctx.OtherModuleProvider(dep, JavaInfoProvider).(JavaInfo)
620 paths.stubsHeaderPath = lib.HeaderJars
621 paths.stubsImplPath = lib.ImplementationJars
Paul Duffin1267d872021-04-16 17:21:36 +0100622
623 libDep := dep.(UsesLibraryDependency)
624 paths.stubsDexJarPath = libDep.DexJarBuildPath()
Paul Duffinc8782502020-04-29 20:45:27 +0100625 return nil
626 } else {
Colin Crossdcf71b22021-02-01 13:59:03 -0800627 return fmt.Errorf("expected module that has JavaInfoProvider, e.g. java_library")
Paul Duffinc8782502020-04-29 20:45:27 +0100628 }
629}
630
Paul Duffin0ff08bd2020-04-29 13:30:54 +0100631func (paths *scopePaths) treatDepAsApiStubsProvider(dep android.Module, action func(provider ApiStubsProvider)) error {
632 if apiStubsProvider, ok := dep.(ApiStubsProvider); ok {
633 action(apiStubsProvider)
Paul Duffinc8782502020-04-29 20:45:27 +0100634 return nil
635 } else {
636 return fmt.Errorf("expected module that implements ApiStubsProvider, e.g. droidstubs")
637 }
638}
639
Paul Duffin0f8faff2020-05-20 16:18:00 +0100640func (paths *scopePaths) treatDepAsApiStubsSrcProvider(dep android.Module, action func(provider ApiStubsSrcProvider)) error {
641 if apiStubsProvider, ok := dep.(ApiStubsSrcProvider); ok {
642 action(apiStubsProvider)
643 return nil
644 } else {
645 return fmt.Errorf("expected module that implements ApiStubsSrcProvider, e.g. droidstubs")
646 }
647}
648
Paul Duffin0ff08bd2020-04-29 13:30:54 +0100649func (paths *scopePaths) extractApiInfoFromApiStubsProvider(provider ApiStubsProvider) {
Anton Hanssond78eb762021-09-21 15:25:12 +0100650 paths.annotationsZip = android.OptionalPathForPath(provider.AnnotationsZip())
Paul Duffin0f8faff2020-05-20 16:18:00 +0100651 paths.currentApiFilePath = android.OptionalPathForPath(provider.ApiFilePath())
652 paths.removedApiFilePath = android.OptionalPathForPath(provider.RemovedApiFilePath())
Paul Duffin0ff08bd2020-04-29 13:30:54 +0100653}
654
Colin Crossdcf71b22021-02-01 13:59:03 -0800655func (paths *scopePaths) extractApiInfoFromDep(ctx android.ModuleContext, dep android.Module) error {
Paul Duffin0ff08bd2020-04-29 13:30:54 +0100656 return paths.treatDepAsApiStubsProvider(dep, func(provider ApiStubsProvider) {
657 paths.extractApiInfoFromApiStubsProvider(provider)
658 })
659}
660
Paul Duffin0f8faff2020-05-20 16:18:00 +0100661func (paths *scopePaths) extractStubsSourceInfoFromApiStubsProviders(provider ApiStubsSrcProvider) {
662 paths.stubsSrcJar = android.OptionalPathForPath(provider.StubsSrcJar())
Paul Duffin0ff08bd2020-04-29 13:30:54 +0100663}
664
Colin Crossdcf71b22021-02-01 13:59:03 -0800665func (paths *scopePaths) extractStubsSourceInfoFromDep(ctx android.ModuleContext, dep android.Module) error {
Paul Duffin0f8faff2020-05-20 16:18:00 +0100666 return paths.treatDepAsApiStubsSrcProvider(dep, func(provider ApiStubsSrcProvider) {
Paul Duffin0ff08bd2020-04-29 13:30:54 +0100667 paths.extractStubsSourceInfoFromApiStubsProviders(provider)
668 })
669}
670
Colin Crossdcf71b22021-02-01 13:59:03 -0800671func (paths *scopePaths) extractStubsSourceAndApiInfoFromApiStubsProvider(ctx android.ModuleContext, dep android.Module) error {
Paul Duffin0ff08bd2020-04-29 13:30:54 +0100672 return paths.treatDepAsApiStubsProvider(dep, func(provider ApiStubsProvider) {
673 paths.extractApiInfoFromApiStubsProvider(provider)
674 paths.extractStubsSourceInfoFromApiStubsProviders(provider)
675 })
676}
677
Paul Duffin958806b2022-05-16 13:10:47 +0000678func extractSingleOptionalOutputPath(dep android.Module) (android.OptionalPath, error) {
679 var paths android.Paths
680 if sourceFileProducer, ok := dep.(android.SourceFileProducer); ok {
681 paths = sourceFileProducer.Srcs()
682 } else {
683 return android.OptionalPath{}, fmt.Errorf("module %q does not produce source files", dep)
684 }
685 if len(paths) != 1 {
686 return android.OptionalPath{}, fmt.Errorf("expected one path from %q, got %q", dep, paths)
687 }
688 return android.OptionalPathForPath(paths[0]), nil
689}
690
691func (paths *scopePaths) extractLatestApiPath(ctx android.ModuleContext, dep android.Module) error {
692 outputPath, err := extractSingleOptionalOutputPath(dep)
693 paths.latestApiPath = outputPath
694 return err
695}
696
697func (paths *scopePaths) extractLatestRemovedApiPath(ctx android.ModuleContext, dep android.Module) error {
698 outputPath, err := extractSingleOptionalOutputPath(dep)
699 paths.latestRemovedApiPath = outputPath
700 return err
701}
702
Paul Duffin0ff08bd2020-04-29 13:30:54 +0100703type commonToSdkLibraryAndImportProperties struct {
Paul Duffin1b1e8062020-05-08 13:44:43 +0100704 // The naming scheme to use for the components that this module creates.
705 //
Paul Duffinee9ad5d2020-09-11 13:04:05 +0100706 // If not specified then it defaults to "default".
Paul Duffin1b1e8062020-05-08 13:44:43 +0100707 //
708 // This is a temporary mechanism to simplify conversion from separate modules for each
709 // component that follow a different naming pattern to the default one.
710 //
711 // TODO(b/155480189) - Remove once naming inconsistencies have been resolved.
Paul Duffin0ff08bd2020-04-29 13:30:54 +0100712 Naming_scheme *string
Paul Duffindfa131e2020-05-15 20:37:11 +0100713
714 // Specifies whether this module can be used as an Android shared library; defaults
715 // to true.
716 //
717 // An Android shared library is one that can be referenced in a <uses-library> element
718 // in an AndroidManifest.xml.
719 Shared_library *bool
Paul Duffina2ae7e02020-09-11 11:55:00 +0100720
721 // Files containing information about supported java doc tags.
722 Doctag_files []string `android:"path"`
Pedro Loureiro9956e5e2021-09-07 17:21:59 +0000723
724 // Signals that this shared library is part of the bootclasspath starting
725 // on the version indicated in this attribute.
726 //
727 // This will make platforms at this level and above to ignore
728 // <uses-library> tags with this library name because the library is already
729 // available
730 On_bootclasspath_since *string
731
732 // Signals that this shared library was part of the bootclasspath before
733 // (but not including) the version indicated in this attribute.
734 //
735 // The system will automatically add a <uses-library> tag with this library to
736 // apps that target any SDK less than the version indicated in this attribute.
737 On_bootclasspath_before *string
738
739 // Indicates that PackageManager should ignore this shared library if the
740 // platform is below the version indicated in this attribute.
741 //
742 // This means that the device won't recognise this library as installed.
743 Min_device_sdk *string
744
745 // Indicates that PackageManager should ignore this shared library if the
746 // platform is above the version indicated in this attribute.
747 //
748 // This means that the device won't recognise this library as installed.
749 Max_device_sdk *string
Paul Duffin0ff08bd2020-04-29 13:30:54 +0100750}
751
Paul Duffin71b33cc2021-06-23 11:39:47 +0100752// commonSdkLibraryAndImportModule defines the interface that must be provided by a module that
753// embeds the commonToSdkLibraryAndImport struct.
754type commonSdkLibraryAndImportModule interface {
Paul Duffind796f6f2022-11-23 23:06:05 +0000755 android.Module
Paul Duffin71b33cc2021-06-23 11:39:47 +0100756
757 BaseModuleName() string
758}
759
Paul Duffin56d44902020-01-31 13:36:25 +0000760// Common code between sdk library and sdk library import
761type commonToSdkLibraryAndImport struct {
Paul Duffin71b33cc2021-06-23 11:39:47 +0100762 module commonSdkLibraryAndImportModule
Paul Duffinc3091c82020-05-08 14:16:20 +0100763
Paul Duffin56d44902020-01-31 13:36:25 +0000764 scopePaths map[*apiScope]*scopePaths
Paul Duffin1b1e8062020-05-08 13:44:43 +0100765
766 namingScheme sdkLibraryComponentNamingScheme
767
Paul Duffindfa131e2020-05-15 20:37:11 +0100768 commonSdkLibraryProperties commonToSdkLibraryAndImportProperties
Paul Duffin859fe962020-05-15 10:20:31 +0100769
Paul Duffina2ae7e02020-09-11 11:55:00 +0100770 // Paths to commonSdkLibraryProperties.Doctag_files
771 doctagPaths android.Paths
772
Paul Duffin859fe962020-05-15 10:20:31 +0100773 // Functionality related to this being used as a component of a java_sdk_library.
774 EmbeddableSdkLibraryComponent
Paul Duffin56d44902020-01-31 13:36:25 +0000775}
776
Paul Duffin71b33cc2021-06-23 11:39:47 +0100777func (c *commonToSdkLibraryAndImport) initCommon(module commonSdkLibraryAndImportModule) {
778 c.module = module
Paul Duffin1b1e8062020-05-08 13:44:43 +0100779
Paul Duffin71b33cc2021-06-23 11:39:47 +0100780 module.AddProperties(&c.commonSdkLibraryProperties)
Paul Duffin859fe962020-05-15 10:20:31 +0100781
782 // Initialize this as an sdk library component.
Paul Duffin71b33cc2021-06-23 11:39:47 +0100783 c.initSdkLibraryComponent(module)
Paul Duffin1b1e8062020-05-08 13:44:43 +0100784}
785
786func (c *commonToSdkLibraryAndImport) initCommonAfterDefaultsApplied(ctx android.DefaultableHookContext) bool {
Paul Duffindfa131e2020-05-15 20:37:11 +0100787 schemeProperty := proptools.StringDefault(c.commonSdkLibraryProperties.Naming_scheme, "default")
Paul Duffin1b1e8062020-05-08 13:44:43 +0100788 switch schemeProperty {
789 case "default":
790 c.namingScheme = &defaultNamingScheme{}
791 default:
792 ctx.PropertyErrorf("naming_scheme", "expected 'default' but was %q", schemeProperty)
793 return false
794 }
795
Paul Duffin3f0290e2021-06-30 18:25:36 +0100796 namePtr := proptools.StringPtr(c.module.BaseModuleName())
797 c.sdkLibraryComponentProperties.SdkLibraryName = namePtr
798
Paul Duffindfa131e2020-05-15 20:37:11 +0100799 // Only track this sdk library if this can be used as a shared library.
800 if c.sharedLibrary() {
801 // Use the name specified in the module definition as the owner.
Paul Duffin3f0290e2021-06-30 18:25:36 +0100802 c.sdkLibraryComponentProperties.SdkLibraryToImplicitlyTrack = namePtr
Paul Duffindfa131e2020-05-15 20:37:11 +0100803 }
Paul Duffin859fe962020-05-15 10:20:31 +0100804
Paul Duffin1b1e8062020-05-08 13:44:43 +0100805 return true
Paul Duffinc3091c82020-05-08 14:16:20 +0100806}
807
Paul Duffinea8f8082021-06-24 13:25:57 +0100808// uniqueApexVariations provides common implementation of the ApexModule.UniqueApexVariations
809// method.
810func (c *commonToSdkLibraryAndImport) uniqueApexVariations() bool {
811 // A java_sdk_library that is a shared library produces an XML file that makes the shared library
812 // usable from an AndroidManifest.xml's <uses-library> entry. That XML file contains the name of
813 // the APEX and so it needs a unique variation per APEX.
814 return c.sharedLibrary()
815}
816
Paul Duffina2ae7e02020-09-11 11:55:00 +0100817func (c *commonToSdkLibraryAndImport) generateCommonBuildActions(ctx android.ModuleContext) {
818 c.doctagPaths = android.PathsForModuleSrc(ctx, c.commonSdkLibraryProperties.Doctag_files)
819}
820
Paul Duffineedc5d52020-06-12 17:46:39 +0100821// Module name of the runtime implementation library
822func (c *commonToSdkLibraryAndImport) implLibraryModuleName() string {
Paul Duffin71b33cc2021-06-23 11:39:47 +0100823 return c.module.BaseModuleName() + ".impl"
Paul Duffineedc5d52020-06-12 17:46:39 +0100824}
825
826// Module name of the XML file for the lib
827func (c *commonToSdkLibraryAndImport) xmlPermissionsModuleName() string {
Paul Duffin71b33cc2021-06-23 11:39:47 +0100828 return c.module.BaseModuleName() + sdkXmlFileSuffix
Paul Duffineedc5d52020-06-12 17:46:39 +0100829}
830
Paul Duffinc3091c82020-05-08 14:16:20 +0100831// Name of the java_library module that compiles the stubs source.
832func (c *commonToSdkLibraryAndImport) stubsLibraryModuleName(apiScope *apiScope) string {
Paul Duffinb97b1572021-04-29 21:50:40 +0100833 baseName := c.module.BaseModuleName()
Paul Duffin21787622022-11-25 12:48:20 +0000834 return c.namingScheme.stubsLibraryModuleName(apiScope, baseName)
Paul Duffinc3091c82020-05-08 14:16:20 +0100835}
836
837// Name of the droidstubs module that generates the stubs source and may also
838// generate/check the API.
839func (c *commonToSdkLibraryAndImport) stubsSourceModuleName(apiScope *apiScope) string {
Paul Duffinb97b1572021-04-29 21:50:40 +0100840 baseName := c.module.BaseModuleName()
Paul Duffin21787622022-11-25 12:48:20 +0000841 return c.namingScheme.stubsSourceModuleName(apiScope, baseName)
Paul Duffinc3091c82020-05-08 14:16:20 +0100842}
843
Paul Duffin46dc45a2020-05-14 15:39:10 +0100844// The component names for different outputs of the java_sdk_library.
845//
846// They are similar to the names used for the child modules it creates
847const (
848 stubsSourceComponentName = "stubs.source"
849
850 apiTxtComponentName = "api.txt"
851
852 removedApiTxtComponentName = "removed-api.txt"
Anton Hanssond78eb762021-09-21 15:25:12 +0100853
854 annotationsComponentName = "annotations.zip"
Paul Duffin46dc45a2020-05-14 15:39:10 +0100855)
856
857// A regular expression to match tags that reference a specific stubs component.
858//
859// It will only match if given a valid scope and a valid component. It is verfy strict
860// to ensure it does not accidentally match a similar looking tag that should be processed
861// by the embedded Library.
862var tagSplitter = func() *regexp.Regexp {
863 // Given a list of literal string items returns a regular expression that will
864 // match any one of the items.
865 choice := func(items ...string) string {
866 return `\Q` + strings.Join(items, `\E|\Q`) + `\E`
867 }
868
869 // Regular expression to match one of the scopes.
870 scopesRegexp := choice(allScopeNames...)
871
872 // Regular expression to match one of the components.
Anton Hanssond78eb762021-09-21 15:25:12 +0100873 componentsRegexp := choice(stubsSourceComponentName, apiTxtComponentName, removedApiTxtComponentName, annotationsComponentName)
Paul Duffin46dc45a2020-05-14 15:39:10 +0100874
875 // Regular expression to match any combination of one scope and one component.
876 return regexp.MustCompile(fmt.Sprintf(`^\.(%s)\.(%s)$`, scopesRegexp, componentsRegexp))
877}()
878
879// For OutputFileProducer interface
880//
Anton Hanssond78eb762021-09-21 15:25:12 +0100881// .<scope>.<component name>, for all ComponentNames (for example: .public.removed-api.txt)
Paul Duffin46dc45a2020-05-14 15:39:10 +0100882func (c *commonToSdkLibraryAndImport) commonOutputFiles(tag string) (android.Paths, error) {
883 if groups := tagSplitter.FindStringSubmatch(tag); groups != nil {
884 scopeName := groups[1]
885 component := groups[2]
886
887 if scope, ok := scopeByName[scopeName]; ok {
888 paths := c.findScopePaths(scope)
889 if paths == nil {
Paul Duffin71b33cc2021-06-23 11:39:47 +0100890 return nil, fmt.Errorf("%q does not provide api scope %s", c.module.BaseModuleName(), scopeName)
Paul Duffin46dc45a2020-05-14 15:39:10 +0100891 }
892
893 switch component {
894 case stubsSourceComponentName:
895 if paths.stubsSrcJar.Valid() {
896 return android.Paths{paths.stubsSrcJar.Path()}, nil
897 }
898
899 case apiTxtComponentName:
900 if paths.currentApiFilePath.Valid() {
901 return android.Paths{paths.currentApiFilePath.Path()}, nil
902 }
903
904 case removedApiTxtComponentName:
905 if paths.removedApiFilePath.Valid() {
906 return android.Paths{paths.removedApiFilePath.Path()}, nil
907 }
Anton Hanssond78eb762021-09-21 15:25:12 +0100908
909 case annotationsComponentName:
910 if paths.annotationsZip.Valid() {
911 return android.Paths{paths.annotationsZip.Path()}, nil
912 }
Paul Duffin46dc45a2020-05-14 15:39:10 +0100913 }
914
915 return nil, fmt.Errorf("%s not available for api scope %s", component, scopeName)
916 } else {
917 return nil, fmt.Errorf("unknown scope %s in %s", scope, tag)
918 }
919
920 } else {
Paul Duffina2ae7e02020-09-11 11:55:00 +0100921 switch tag {
922 case ".doctags":
923 if c.doctagPaths != nil {
924 return c.doctagPaths, nil
925 } else {
Paul Duffin71b33cc2021-06-23 11:39:47 +0100926 return nil, fmt.Errorf("no doctag_files specified on %s", c.module.BaseModuleName())
Paul Duffina2ae7e02020-09-11 11:55:00 +0100927 }
928 }
Paul Duffin46dc45a2020-05-14 15:39:10 +0100929 return nil, nil
930 }
931}
932
Paul Duffin803a9562020-05-20 11:52:25 +0100933func (c *commonToSdkLibraryAndImport) getScopePathsCreateIfNeeded(scope *apiScope) *scopePaths {
Paul Duffin56d44902020-01-31 13:36:25 +0000934 if c.scopePaths == nil {
935 c.scopePaths = make(map[*apiScope]*scopePaths)
936 }
937 paths := c.scopePaths[scope]
938 if paths == nil {
939 paths = &scopePaths{}
940 c.scopePaths[scope] = paths
941 }
942
943 return paths
944}
945
Paul Duffin803a9562020-05-20 11:52:25 +0100946func (c *commonToSdkLibraryAndImport) findScopePaths(scope *apiScope) *scopePaths {
947 if c.scopePaths == nil {
948 return nil
949 }
950
951 return c.scopePaths[scope]
952}
953
954// If this does not support the requested api scope then find the closest available
955// scope it does support. Returns nil if no such scope is available.
956func (c *commonToSdkLibraryAndImport) findClosestScopePath(scope *apiScope) *scopePaths {
Paul Duffind0b9fca2022-09-30 18:11:41 +0100957 for s := scope; s != nil; s = s.canAccess {
Paul Duffin803a9562020-05-20 11:52:25 +0100958 if paths := c.findScopePaths(s); paths != nil {
959 return paths
960 }
961 }
962
963 // This should never happen outside tests as public should be the base scope for every
964 // scope and is enabled by default.
965 return nil
966}
967
Jiyong Parkf1691d22021-03-29 20:11:58 +0900968func (c *commonToSdkLibraryAndImport) selectHeaderJarsForSdkVersion(ctx android.BaseModuleContext, sdkVersion android.SdkSpec) android.Paths {
Paul Duffinb05d4292020-05-20 12:19:10 +0100969
970 // If a specific numeric version has been requested then use prebuilt versions of the sdk.
Jiyong Park54105c42021-03-31 18:17:53 +0900971 if !sdkVersion.ApiLevel.IsPreview() {
Paul Duffin71b33cc2021-06-23 11:39:47 +0100972 return PrebuiltJars(ctx, c.module.BaseModuleName(), sdkVersion)
Paul Duffinb05d4292020-05-20 12:19:10 +0100973 }
974
Paul Duffin1267d872021-04-16 17:21:36 +0100975 paths := c.selectScopePaths(ctx, sdkVersion.Kind)
976 if paths == nil {
977 return nil
978 }
979
980 return paths.stubsHeaderPath
981}
982
983// selectScopePaths returns the *scopePaths appropriate for the specific kind.
984//
985// If the module does not support the specific kind then it will return the *scopePaths for the
986// closest kind which is a subset of the requested kind. e.g. if requesting android.SdkModule then
987// it will return *scopePaths for android.SdkSystem if available or android.SdkPublic of not.
988func (c *commonToSdkLibraryAndImport) selectScopePaths(ctx android.BaseModuleContext, kind android.SdkKind) *scopePaths {
Paul Duffin32cf58a2021-05-18 16:32:50 +0100989 apiScope := sdkKindToApiScope(kind)
Paul Duffinb05d4292020-05-20 12:19:10 +0100990
Paul Duffin803a9562020-05-20 11:52:25 +0100991 paths := c.findClosestScopePath(apiScope)
992 if paths == nil {
993 var scopes []string
994 for _, s := range allApiScopes {
995 if c.findScopePaths(s) != nil {
996 scopes = append(scopes, s.name)
997 }
998 }
Paul Duffin71b33cc2021-06-23 11:39:47 +0100999 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 +01001000 return nil
1001 }
1002
Paul Duffin1267d872021-04-16 17:21:36 +01001003 return paths
1004}
1005
Paul Duffin32cf58a2021-05-18 16:32:50 +01001006// sdkKindToApiScope maps from android.SdkKind to apiScope.
1007func sdkKindToApiScope(kind android.SdkKind) *apiScope {
1008 var apiScope *apiScope
1009 switch kind {
1010 case android.SdkSystem:
1011 apiScope = apiScopeSystem
1012 case android.SdkModule:
1013 apiScope = apiScopeModuleLib
1014 case android.SdkTest:
1015 apiScope = apiScopeTest
1016 case android.SdkSystemServer:
1017 apiScope = apiScopeSystemServer
1018 default:
1019 apiScope = apiScopePublic
1020 }
1021 return apiScope
1022}
1023
Paul Duffin1267d872021-04-16 17:21:36 +01001024// to satisfy SdkLibraryDependency interface
Martin Stjernholm8be1e6d2021-09-15 03:34:04 +01001025func (c *commonToSdkLibraryAndImport) SdkApiStubDexJar(ctx android.BaseModuleContext, kind android.SdkKind) OptionalDexJarPath {
Paul Duffin1267d872021-04-16 17:21:36 +01001026 paths := c.selectScopePaths(ctx, kind)
1027 if paths == nil {
Martin Stjernholm8be1e6d2021-09-15 03:34:04 +01001028 return makeUnsetDexJarPath()
Paul Duffin1267d872021-04-16 17:21:36 +01001029 }
1030
1031 return paths.stubsDexJarPath
Paul Duffinb05d4292020-05-20 12:19:10 +01001032}
1033
Paul Duffin32cf58a2021-05-18 16:32:50 +01001034// to satisfy SdkLibraryDependency interface
1035func (c *commonToSdkLibraryAndImport) SdkRemovedTxtFile(ctx android.BaseModuleContext, kind android.SdkKind) android.OptionalPath {
1036 apiScope := sdkKindToApiScope(kind)
1037 paths := c.findScopePaths(apiScope)
1038 if paths == nil {
1039 return android.OptionalPath{}
1040 }
1041
1042 return paths.removedApiFilePath
1043}
1044
Paul Duffin859fe962020-05-15 10:20:31 +01001045func (c *commonToSdkLibraryAndImport) sdkComponentPropertiesForChildLibrary() interface{} {
1046 componentProps := &struct {
Paul Duffin3f0290e2021-06-30 18:25:36 +01001047 SdkLibraryName *string
Paul Duffin859fe962020-05-15 10:20:31 +01001048 SdkLibraryToImplicitlyTrack *string
Paul Duffindfa131e2020-05-15 20:37:11 +01001049 }{}
1050
Paul Duffin3f0290e2021-06-30 18:25:36 +01001051 namePtr := proptools.StringPtr(c.module.BaseModuleName())
1052 componentProps.SdkLibraryName = namePtr
1053
Paul Duffindfa131e2020-05-15 20:37:11 +01001054 if c.sharedLibrary() {
Paul Duffin859fe962020-05-15 10:20:31 +01001055 // Mark the stubs library as being components of this java_sdk_library so that
1056 // any app that includes code which depends (directly or indirectly) on the stubs
1057 // library will have the appropriate <uses-library> invocation inserted into its
1058 // manifest if necessary.
Paul Duffin3f0290e2021-06-30 18:25:36 +01001059 componentProps.SdkLibraryToImplicitlyTrack = namePtr
Paul Duffin859fe962020-05-15 10:20:31 +01001060 }
1061
1062 return componentProps
1063}
1064
Paul Duffindfa131e2020-05-15 20:37:11 +01001065func (c *commonToSdkLibraryAndImport) sharedLibrary() bool {
1066 return proptools.BoolDefault(c.commonSdkLibraryProperties.Shared_library, true)
1067}
1068
Paul Duffinf4600f62021-05-13 22:34:45 +01001069// Check if the stub libraries should be compiled for dex
1070func (c *commonToSdkLibraryAndImport) stubLibrariesCompiledForDex() bool {
1071 // Always compile the dex file files for the stub libraries if they will be used on the
1072 // bootclasspath.
1073 return !c.sharedLibrary()
1074}
1075
Paul Duffin859fe962020-05-15 10:20:31 +01001076// Properties related to the use of a module as an component of a java_sdk_library.
1077type SdkLibraryComponentProperties struct {
Paul Duffin3f0290e2021-06-30 18:25:36 +01001078 // The name of the java_sdk_library/_import module.
1079 SdkLibraryName *string `blueprint:"mutated"`
Paul Duffin859fe962020-05-15 10:20:31 +01001080
1081 // The name of the java_sdk_library/_import to add to a <uses-library> entry
1082 // in the AndroidManifest.xml of any Android app that includes code that references
1083 // this module. If not set then no java_sdk_library/_import is tracked.
1084 SdkLibraryToImplicitlyTrack *string `blueprint:"mutated"`
1085}
1086
1087// Structure to be embedded in a module struct that needs to support the
1088// SdkLibraryComponentDependency interface.
1089type EmbeddableSdkLibraryComponent struct {
1090 sdkLibraryComponentProperties SdkLibraryComponentProperties
1091}
1092
Paul Duffin71b33cc2021-06-23 11:39:47 +01001093func (e *EmbeddableSdkLibraryComponent) initSdkLibraryComponent(module android.Module) {
1094 module.AddProperties(&e.sdkLibraryComponentProperties)
Paul Duffin859fe962020-05-15 10:20:31 +01001095}
1096
1097// to satisfy SdkLibraryComponentDependency
Paul Duffin3f0290e2021-06-30 18:25:36 +01001098func (e *EmbeddableSdkLibraryComponent) SdkLibraryName() *string {
1099 return e.sdkLibraryComponentProperties.SdkLibraryName
1100}
1101
1102// to satisfy SdkLibraryComponentDependency
Ulya Trafimovich39b437b2020-09-23 16:42:35 +01001103func (e *EmbeddableSdkLibraryComponent) OptionalSdkLibraryImplementation() *string {
Ulya Trafimovich78645fb2021-07-16 15:29:25 +01001104 // For shared libraries, this is the same as the SDK library name. If a Java library or app
1105 // depends on a component library (e.g. a stub library) it still needs to know the name of the
1106 // run-time library and the corresponding module that provides the implementation. This name is
1107 // passed to manifest_fixer (to be added to AndroidManifest.xml) and added to CLC (to be used
1108 // in dexpreopt).
1109 //
1110 // For non-shared SDK (component or not) libraries this returns `nil`, as they are not
1111 // <uses-library> and should not be added to the manifest or to CLC.
Ulya Trafimovich39b437b2020-09-23 16:42:35 +01001112 return e.sdkLibraryComponentProperties.SdkLibraryToImplicitlyTrack
1113}
1114
Paul Duffin859fe962020-05-15 10:20:31 +01001115// Implemented by modules that are (or possibly could be) a component of a java_sdk_library
1116// (including the java_sdk_library) itself.
1117type SdkLibraryComponentDependency interface {
Ulya Trafimovich31e444e2020-08-14 17:32:16 +01001118 UsesLibraryDependency
1119
Paul Duffin3f0290e2021-06-30 18:25:36 +01001120 // SdkLibraryName returns the name of the java_sdk_library/_import module.
1121 SdkLibraryName() *string
1122
Ulya Trafimovich39b437b2020-09-23 16:42:35 +01001123 // The name of the implementation library for the optional SDK library or nil, if there isn't one.
1124 OptionalSdkLibraryImplementation() *string
Paul Duffin859fe962020-05-15 10:20:31 +01001125}
1126
1127// Make sure that all the module types that are components of java_sdk_library/_import
1128// and which can be referenced (directly or indirectly) from an android app implement
1129// the SdkLibraryComponentDependency interface.
1130var _ SdkLibraryComponentDependency = (*Library)(nil)
1131var _ SdkLibraryComponentDependency = (*Import)(nil)
1132var _ SdkLibraryComponentDependency = (*SdkLibrary)(nil)
Paul Duffineedc5d52020-06-12 17:46:39 +01001133var _ SdkLibraryComponentDependency = (*SdkLibraryImport)(nil)
Paul Duffin859fe962020-05-15 10:20:31 +01001134
Paul Duffin32cf58a2021-05-18 16:32:50 +01001135// Provides access to sdk_version related files, e.g. header and implementation jars.
Paul Duffin859fe962020-05-15 10:20:31 +01001136type SdkLibraryDependency interface {
1137 SdkLibraryComponentDependency
1138
1139 // Get the header jars appropriate for the supplied sdk_version.
1140 //
1141 // These are turbine generated jars so they only change if the externals of the
1142 // class changes but it does not contain and implementation or JavaDoc.
Jiyong Parkf1691d22021-03-29 20:11:58 +09001143 SdkHeaderJars(ctx android.BaseModuleContext, sdkVersion android.SdkSpec) android.Paths
Paul Duffin859fe962020-05-15 10:20:31 +01001144
1145 // Get the implementation jars appropriate for the supplied sdk version.
1146 //
1147 // These are either the implementation jar for the whole sdk library or the implementation
1148 // jars for the stubs. The latter should only be needed when generating JavaDoc as otherwise
1149 // they are identical to the corresponding header jars.
Jiyong Parkf1691d22021-03-29 20:11:58 +09001150 SdkImplementationJars(ctx android.BaseModuleContext, sdkVersion android.SdkSpec) android.Paths
Paul Duffin1267d872021-04-16 17:21:36 +01001151
1152 // SdkApiStubDexJar returns the dex jar for the stubs. It is needed by the hiddenapi processing
1153 // tool which processes dex files.
Martin Stjernholm8be1e6d2021-09-15 03:34:04 +01001154 SdkApiStubDexJar(ctx android.BaseModuleContext, kind android.SdkKind) OptionalDexJarPath
Paul Duffinf4600f62021-05-13 22:34:45 +01001155
Paul Duffin32cf58a2021-05-18 16:32:50 +01001156 // SdkRemovedTxtFile returns the optional path to the removed.txt file for the specified sdk kind.
1157 SdkRemovedTxtFile(ctx android.BaseModuleContext, kind android.SdkKind) android.OptionalPath
1158
Paul Duffinf4600f62021-05-13 22:34:45 +01001159 // sharedLibrary returns true if this can be used as a shared library.
1160 sharedLibrary() bool
Paul Duffin859fe962020-05-15 10:20:31 +01001161}
1162
Inseob Kimc0907f12019-02-08 21:00:45 +09001163type SdkLibrary struct {
Sundong Ahn054b19a2018-10-19 13:46:09 +09001164 Library
Jiyong Parkc678ad32018-04-10 13:07:10 +09001165
Sundong Ahn054b19a2018-10-19 13:46:09 +09001166 sdkLibraryProperties sdkLibraryProperties
Jiyong Parkc678ad32018-04-10 13:07:10 +09001167
Paul Duffin3375e352020-04-28 10:44:03 +01001168 // Map from api scope to the scope specific property structure.
1169 scopeToProperties map[*apiScope]*ApiScopeProperties
1170
Paul Duffin56d44902020-01-31 13:36:25 +00001171 commonToSdkLibraryAndImport
Jiyong Parkc678ad32018-04-10 13:07:10 +09001172}
1173
Inseob Kimc0907f12019-02-08 21:00:45 +09001174var _ SdkLibraryDependency = (*SdkLibrary)(nil)
Colin Cross897d2ed2019-02-11 14:03:51 -08001175
Paul Duffin3375e352020-04-28 10:44:03 +01001176func (module *SdkLibrary) generateTestAndSystemScopesByDefault() bool {
1177 return module.sdkLibraryProperties.Generate_system_and_test_apis
1178}
1179
1180func (module *SdkLibrary) getGeneratedApiScopes(ctx android.EarlyModuleContext) apiScopes {
1181 // Check to see if any scopes have been explicitly enabled. If any have then all
1182 // must be.
1183 anyScopesExplicitlyEnabled := false
1184 for _, scope := range allApiScopes {
1185 scopeProperties := module.scopeToProperties[scope]
1186 if scopeProperties.Enabled != nil {
1187 anyScopesExplicitlyEnabled = true
1188 break
1189 }
Paul Duffind1b3a922020-01-22 11:57:20 +00001190 }
Paul Duffin3375e352020-04-28 10:44:03 +01001191
1192 var generatedScopes apiScopes
1193 enabledScopes := make(map[*apiScope]struct{})
1194 for _, scope := range allApiScopes {
1195 scopeProperties := module.scopeToProperties[scope]
1196 // If any scopes are explicitly enabled then ignore the legacy enabled status.
1197 // This is to ensure that any new usages of this module type do not rely on legacy
1198 // behaviour.
1199 defaultEnabledStatus := false
1200 if anyScopesExplicitlyEnabled {
1201 defaultEnabledStatus = scope.defaultEnabledStatus
1202 } else {
1203 defaultEnabledStatus = scope.legacyEnabledStatus(module)
1204 }
1205 enabled := proptools.BoolDefault(scopeProperties.Enabled, defaultEnabledStatus)
1206 if enabled {
1207 enabledScopes[scope] = struct{}{}
1208 generatedScopes = append(generatedScopes, scope)
1209 }
1210 }
1211
1212 // Now check to make sure that any scope that is extended by an enabled scope is also
1213 // enabled.
1214 for _, scope := range allApiScopes {
1215 if _, ok := enabledScopes[scope]; ok {
1216 extends := scope.extends
1217 if extends != nil {
1218 if _, ok := enabledScopes[extends]; !ok {
1219 ctx.ModuleErrorf("enabled api scope %q depends on disabled scope %q", scope, extends)
1220 }
1221 }
1222 }
1223 }
1224
1225 return generatedScopes
Paul Duffind1b3a922020-01-22 11:57:20 +00001226}
1227
satayev758968a2021-12-06 11:42:40 +00001228var _ android.ModuleWithMinSdkVersionCheck = (*SdkLibrary)(nil)
1229
satayev8f088b02021-12-06 11:40:46 +00001230func (module *SdkLibrary) CheckMinSdkVersion(ctx android.ModuleContext) {
1231 android.CheckMinSdkVersion(ctx, module.MinSdkVersion(ctx).ApiLevel, func(c android.ModuleContext, do android.PayloadDepsCallback) {
1232 ctx.WalkDeps(func(child android.Module, parent android.Module) bool {
1233 isExternal := !module.depIsInSameApex(ctx, child)
1234 if am, ok := child.(android.ApexModule); ok {
1235 if !do(ctx, parent, am, isExternal) {
1236 return false
1237 }
1238 }
1239 return !isExternal
1240 })
1241 })
1242}
1243
Paul Duffineedc5d52020-06-12 17:46:39 +01001244type sdkLibraryComponentTag struct {
1245 blueprint.BaseDependencyTag
1246 name string
1247}
1248
1249// Mark this tag so dependencies that use it are excluded from visibility enforcement.
1250func (t sdkLibraryComponentTag) ExcludeFromVisibilityEnforcement() {}
1251
1252var xmlPermissionsFileTag = sdkLibraryComponentTag{name: "xml-permissions-file"}
Paul Duffine74ac732020-02-06 13:51:46 +00001253
Jiyong Parke3833882020-02-17 17:28:10 +09001254func IsXmlPermissionsFileDepTag(depTag blueprint.DependencyTag) bool {
Paul Duffineedc5d52020-06-12 17:46:39 +01001255 if dt, ok := depTag.(sdkLibraryComponentTag); ok {
Jiyong Parke3833882020-02-17 17:28:10 +09001256 return dt == xmlPermissionsFileTag
1257 }
1258 return false
1259}
1260
Paul Duffineedc5d52020-06-12 17:46:39 +01001261var implLibraryTag = sdkLibraryComponentTag{name: "impl-library"}
Paul Duffin5df79302020-05-16 15:52:12 +01001262
Paul Duffin44f1d842020-06-26 20:17:02 +01001263// Add the dependencies on the child modules in the component deps mutator.
1264func (module *SdkLibrary) ComponentDepsMutator(ctx android.BottomUpMutatorContext) {
Paul Duffin3375e352020-04-28 10:44:03 +01001265 for _, apiScope := range module.getGeneratedApiScopes(ctx) {
Paul Duffind1b3a922020-01-22 11:57:20 +00001266 // Add dependencies to the stubs library
Paul Duffinc3091c82020-05-08 14:16:20 +01001267 ctx.AddVariationDependencies(nil, apiScope.stubsTag, module.stubsLibraryModuleName(apiScope))
Paul Duffind1b3a922020-01-22 11:57:20 +00001268
Paul Duffin15f34ef2020-07-20 18:04:44 +01001269 // Add a dependency on the stubs source in order to access both stubs source and api information.
1270 ctx.AddVariationDependencies(nil, apiScope.stubsSourceAndApiTag, module.stubsSourceModuleName(apiScope))
Paul Duffin958806b2022-05-16 13:10:47 +00001271
1272 if module.compareAgainstLatestApi(apiScope) {
1273 // Add dependencies on the latest finalized version of the API .txt file.
1274 latestApiModuleName := module.latestApiModuleName(apiScope)
1275 ctx.AddDependency(module, apiScope.latestApiModuleTag, latestApiModuleName)
1276
1277 // Add dependencies on the latest finalized version of the remove API .txt file.
1278 latestRemovedApiModuleName := module.latestRemovedApiModuleName(apiScope)
1279 ctx.AddDependency(module, apiScope.latestRemovedApiModuleTag, latestRemovedApiModuleName)
1280 }
Sundong Ahn054b19a2018-10-19 13:46:09 +09001281 }
1282
Paul Duffindfa131e2020-05-15 20:37:11 +01001283 if module.requiresRuntimeImplementationLibrary() {
Paul Duffin5df79302020-05-16 15:52:12 +01001284 // Add dependency to the rule for generating the implementation library.
1285 ctx.AddDependency(module, implLibraryTag, module.implLibraryModuleName())
1286
Paul Duffindfa131e2020-05-15 20:37:11 +01001287 if module.sharedLibrary() {
1288 // Add dependency to the rule for generating the xml permissions file
Paul Duffineedc5d52020-06-12 17:46:39 +01001289 ctx.AddDependency(module, xmlPermissionsFileTag, module.xmlPermissionsModuleName())
Paul Duffindfa131e2020-05-15 20:37:11 +01001290 }
Paul Duffin44f1d842020-06-26 20:17:02 +01001291 }
1292}
Paul Duffine74ac732020-02-06 13:51:46 +00001293
Paul Duffin44f1d842020-06-26 20:17:02 +01001294// Add other dependencies as normal.
1295func (module *SdkLibrary) DepsMutator(ctx android.BottomUpMutatorContext) {
Anton Hanssone77fccc2021-01-20 16:52:41 +00001296 var missingApiModules []string
1297 for _, apiScope := range module.getGeneratedApiScopes(ctx) {
1298 if apiScope.unstable {
1299 continue
1300 }
Paul Duffin958806b2022-05-16 13:10:47 +00001301 if m := module.latestApiModuleName(apiScope); !ctx.OtherModuleExists(m) {
Anton Hanssone77fccc2021-01-20 16:52:41 +00001302 missingApiModules = append(missingApiModules, m)
1303 }
Paul Duffin958806b2022-05-16 13:10:47 +00001304 if m := module.latestRemovedApiModuleName(apiScope); !ctx.OtherModuleExists(m) {
Anton Hanssone77fccc2021-01-20 16:52:41 +00001305 missingApiModules = append(missingApiModules, m)
1306 }
Paul Duffin958806b2022-05-16 13:10:47 +00001307 if m := module.latestIncompatibilitiesModuleName(apiScope); !ctx.OtherModuleExists(m) {
Jaewoong Jung1a97ee02021-03-09 13:25:02 -08001308 missingApiModules = append(missingApiModules, m)
1309 }
Anton Hanssone77fccc2021-01-20 16:52:41 +00001310 }
1311 if len(missingApiModules) != 0 && !module.sdkLibraryProperties.Unsafe_ignore_missing_latest_api {
1312 m := module.Name() + " is missing tracking files for previously released library versions.\n"
1313 m += "You need to do one of the following:\n"
1314 m += "- Add `unsafe_ignore_missing_latest_api: true` to your blueprint (to disable compat tracking)\n"
1315 m += "- Add a set of prebuilt txt files representing the last released version of this library for compat checking.\n"
1316 m += " (the current set of API files can be used as a seed for this compatibility tracking\n"
1317 m += "\n"
1318 m += "The following filegroup modules are missing:\n "
1319 m += strings.Join(missingApiModules, "\n ") + "\n"
1320 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."
1321 ctx.ModuleErrorf(m)
1322 }
Paul Duffin44f1d842020-06-26 20:17:02 +01001323 if module.requiresRuntimeImplementationLibrary() {
Paul Duffindfa131e2020-05-15 20:37:11 +01001324 // Only add the deps for the library if it is actually going to be built.
1325 module.Library.deps(ctx)
1326 }
Jiyong Parkc678ad32018-04-10 13:07:10 +09001327}
1328
Paul Duffin46dc45a2020-05-14 15:39:10 +01001329func (module *SdkLibrary) OutputFiles(tag string) (android.Paths, error) {
1330 paths, err := module.commonOutputFiles(tag)
Colin Cross4acaea92021-12-10 23:05:02 +00001331 if paths != nil || err != nil {
Paul Duffin46dc45a2020-05-14 15:39:10 +01001332 return paths, err
1333 }
Colin Cross4acaea92021-12-10 23:05:02 +00001334 if module.requiresRuntimeImplementationLibrary() {
1335 return module.Library.OutputFiles(tag)
1336 }
1337 if tag == "" {
1338 return nil, nil
1339 }
1340 return nil, fmt.Errorf("unsupported module reference tag %q", tag)
Paul Duffin46dc45a2020-05-14 15:39:10 +01001341}
1342
Inseob Kimc0907f12019-02-08 21:00:45 +09001343func (module *SdkLibrary) GenerateAndroidBuildActions(ctx android.ModuleContext) {
satayev8f088b02021-12-06 11:40:46 +00001344 if proptools.String(module.deviceProperties.Min_sdk_version) != "" {
1345 module.CheckMinSdkVersion(ctx)
1346 }
1347
Paul Duffina2ae7e02020-09-11 11:55:00 +01001348 module.generateCommonBuildActions(ctx)
1349
Paul Duffindfa131e2020-05-15 20:37:11 +01001350 // Only build an implementation library if required.
1351 if module.requiresRuntimeImplementationLibrary() {
Paul Duffin43db9be2019-12-30 17:35:49 +00001352 module.Library.GenerateAndroidBuildActions(ctx)
1353 }
Sundong Ahn054b19a2018-10-19 13:46:09 +09001354
Paul Duffinb97b1572021-04-29 21:50:40 +01001355 // Collate the components exported by this module. All scope specific modules are exported but
1356 // the impl and xml component modules are not.
1357 exportedComponents := map[string]struct{}{}
1358
Sundong Ahn57368eb2018-07-06 11:20:23 +09001359 // Record the paths to the header jars of the library (stubs and impl).
Paul Duffind1b3a922020-01-22 11:57:20 +00001360 // When this java_sdk_library is depended upon from others via "libs" property,
Jiyong Parkc678ad32018-04-10 13:07:10 +09001361 // the recorded paths will be returned depending on the link type of the caller.
1362 ctx.VisitDirectDeps(func(to android.Module) {
Jiyong Parkc678ad32018-04-10 13:07:10 +09001363 tag := ctx.OtherModuleDependencyTag(to)
1364
Paul Duffinc8782502020-04-29 20:45:27 +01001365 // Extract information from any of the scope specific dependencies.
1366 if scopeTag, ok := tag.(scopeDependencyTag); ok {
1367 apiScope := scopeTag.apiScope
Paul Duffin803a9562020-05-20 11:52:25 +01001368 scopePaths := module.getScopePathsCreateIfNeeded(apiScope)
Paul Duffinc8782502020-04-29 20:45:27 +01001369
1370 // Extract information from the dependency. The exact information extracted
1371 // is determined by the nature of the dependency which is determined by the tag.
1372 scopeTag.extractDepInfo(ctx, to, scopePaths)
Paul Duffinb97b1572021-04-29 21:50:40 +01001373
1374 exportedComponents[ctx.OtherModuleName(to)] = struct{}{}
Sundong Ahn20e998b2018-07-24 11:19:26 +09001375 }
Jiyong Parkc678ad32018-04-10 13:07:10 +09001376 })
Paul Duffinb97b1572021-04-29 21:50:40 +01001377
1378 // Make the set of components exported by this module available for use elsewhere.
1379 exportedComponentInfo := android.ExportedComponentsInfo{Components: android.SortedStringKeys(exportedComponents)}
1380 ctx.SetProvider(android.ExportedComponentsInfoProvider, exportedComponentInfo)
Paul Duffin958806b2022-05-16 13:10:47 +00001381
1382 // Provide additional information for inclusion in an sdk's generated .info file.
1383 additionalSdkInfo := map[string]interface{}{}
1384 additionalSdkInfo["dist_stem"] = module.distStem()
Paul Duffine8409952022-09-22 16:24:46 +01001385 baseModuleName := module.distStem()
Paul Duffin958806b2022-05-16 13:10:47 +00001386 scopes := map[string]interface{}{}
1387 additionalSdkInfo["scopes"] = scopes
1388 for scope, scopePaths := range module.scopePaths {
1389 scopeInfo := map[string]interface{}{}
1390 scopes[scope.name] = scopeInfo
1391 scopeInfo["current_api"] = scope.snapshotRelativeCurrentApiTxtPath(baseModuleName)
1392 scopeInfo["removed_api"] = scope.snapshotRelativeRemovedApiTxtPath(baseModuleName)
1393 if p := scopePaths.latestApiPath; p.Valid() {
1394 scopeInfo["latest_api"] = p.Path().String()
1395 }
1396 if p := scopePaths.latestRemovedApiPath; p.Valid() {
1397 scopeInfo["latest_removed_api"] = p.Path().String()
1398 }
1399 }
1400 ctx.SetProvider(android.AdditionalSdkInfoProvider, android.AdditionalSdkInfo{additionalSdkInfo})
Jiyong Parkc678ad32018-04-10 13:07:10 +09001401}
1402
Jiyong Park0b0e1b92019-12-03 13:24:29 +09001403func (module *SdkLibrary) AndroidMkEntries() []android.AndroidMkEntries {
Paul Duffindfa131e2020-05-15 20:37:11 +01001404 if !module.requiresRuntimeImplementationLibrary() {
Paul Duffin43db9be2019-12-30 17:35:49 +00001405 return nil
1406 }
Jiyong Park0b0e1b92019-12-03 13:24:29 +09001407 entriesList := module.Library.AndroidMkEntries()
Yo Chiang07d75072020-06-05 17:43:19 +08001408 if module.sharedLibrary() {
1409 entries := &entriesList[0]
1410 entries.Required = append(entries.Required, module.xmlPermissionsModuleName())
1411 }
Jiyong Park0b0e1b92019-12-03 13:24:29 +09001412 return entriesList
Jiyong Park82484c02018-04-23 21:41:26 +09001413}
1414
Anton Hansson5fd5d242020-03-27 19:43:19 +00001415// The dist path of the stub artifacts
1416func (module *SdkLibrary) apiDistPath(apiScope *apiScope) string {
Colin Crossf0eace92021-06-02 13:02:23 -07001417 return path.Join("apistubs", module.distGroup(), apiScope.name)
Anton Hansson5fd5d242020-03-27 19:43:19 +00001418}
1419
Paul Duffin12ceb462019-12-24 20:31:31 +00001420// Get the sdk version for use when compiling the stubs library.
Paul Duffin780c5f42020-05-12 15:52:55 +01001421func (module *SdkLibrary) sdkVersionForStubsLibrary(mctx android.EarlyModuleContext, apiScope *apiScope) string {
Paul Duffin87a05a32020-05-12 11:50:28 +01001422 scopeProperties := module.scopeToProperties[apiScope]
1423 if scopeProperties.Sdk_version != nil {
1424 return proptools.String(scopeProperties.Sdk_version)
1425 }
1426
Jiyong Parkf1691d22021-03-29 20:11:58 +09001427 sdkDep := decodeSdkDep(mctx, android.SdkContext(&module.Library))
Paul Duffin12ceb462019-12-24 20:31:31 +00001428 if sdkDep.hasStandardLibs() {
1429 // If building against a standard sdk then use the sdk version appropriate for the scope.
Paul Duffind1b3a922020-01-22 11:57:20 +00001430 return apiScope.sdkVersion
Paul Duffin12ceb462019-12-24 20:31:31 +00001431 } else {
1432 // Otherwise, use no system module.
1433 return "none"
1434 }
1435}
1436
Paul Duffin31310252020-11-20 21:26:20 +00001437func (module *SdkLibrary) distStem() string {
1438 return proptools.StringDefault(module.sdkLibraryProperties.Dist_stem, module.BaseModuleName())
1439}
1440
Colin Cross986b69a2021-06-01 13:13:40 -07001441// distGroup returns the subdirectory of the dist path of the stub artifacts.
1442func (module *SdkLibrary) distGroup() string {
Colin Cross59b92bf2021-06-01 14:07:56 -07001443 return proptools.StringDefault(module.sdkLibraryProperties.Dist_group, "unknown")
Colin Cross986b69a2021-06-01 13:13:40 -07001444}
1445
Paul Duffin958806b2022-05-16 13:10:47 +00001446func latestPrebuiltApiModuleName(name string, apiScope *apiScope) string {
1447 return PrebuiltApiModuleName(name, apiScope.name, "latest")
1448}
1449
Paul Duffind1b3a922020-01-22 11:57:20 +00001450func (module *SdkLibrary) latestApiFilegroupName(apiScope *apiScope) string {
Paul Duffin958806b2022-05-16 13:10:47 +00001451 return ":" + module.latestApiModuleName(apiScope)
1452}
1453
1454func (module *SdkLibrary) latestApiModuleName(apiScope *apiScope) string {
1455 return latestPrebuiltApiModuleName(module.distStem(), apiScope)
Jiyong Park58c518b2018-05-12 22:29:12 +09001456}
Jiyong Parkc678ad32018-04-10 13:07:10 +09001457
Paul Duffind1b3a922020-01-22 11:57:20 +00001458func (module *SdkLibrary) latestRemovedApiFilegroupName(apiScope *apiScope) string {
Paul Duffin958806b2022-05-16 13:10:47 +00001459 return ":" + module.latestRemovedApiModuleName(apiScope)
1460}
1461
1462func (module *SdkLibrary) latestRemovedApiModuleName(apiScope *apiScope) string {
1463 return latestPrebuiltApiModuleName(module.distStem()+"-removed", apiScope)
Jiyong Parkc678ad32018-04-10 13:07:10 +09001464}
1465
Jaewoong Jung1a97ee02021-03-09 13:25:02 -08001466func (module *SdkLibrary) latestIncompatibilitiesFilegroupName(apiScope *apiScope) string {
Paul Duffin958806b2022-05-16 13:10:47 +00001467 return ":" + module.latestIncompatibilitiesModuleName(apiScope)
1468}
1469
1470func (module *SdkLibrary) latestIncompatibilitiesModuleName(apiScope *apiScope) string {
1471 return latestPrebuiltApiModuleName(module.distStem()+"-incompatibilities", apiScope)
Jaewoong Jung1a97ee02021-03-09 13:25:02 -08001472}
1473
Anton Hansson944e77d2020-08-19 11:40:22 +01001474func childModuleVisibility(childVisibility []string) []string {
1475 if childVisibility == nil {
1476 // No child visibility set. The child will use the visibility of the sdk_library.
1477 return nil
1478 }
1479
1480 // Prepend an override to ignore the sdk_library's visibility, and rely on the child visibility.
1481 var visibility []string
1482 visibility = append(visibility, "//visibility:override")
1483 visibility = append(visibility, childVisibility...)
1484 return visibility
1485}
1486
Paul Duffin5df79302020-05-16 15:52:12 +01001487// Creates the implementation java library
1488func (module *SdkLibrary) createImplLibrary(mctx android.DefaultableHookContext) {
Anton Hansson944e77d2020-08-19 11:40:22 +01001489 visibility := childModuleVisibility(module.sdkLibraryProperties.Impl_library_visibility)
1490
Paul Duffin5df79302020-05-16 15:52:12 +01001491 props := struct {
Paul Duffin77590a82022-04-28 14:13:30 +00001492 Name *string
1493 Visibility []string
1494 Instrument bool
1495 Libs []string
1496 Static_libs []string
1497 Apex_available []string
Paul Duffin5df79302020-05-16 15:52:12 +01001498 }{
1499 Name: proptools.StringPtr(module.implLibraryModuleName()),
Anton Hansson944e77d2020-08-19 11:40:22 +01001500 Visibility: visibility,
Paul Duffin296cf332020-06-18 21:09:55 +01001501 // Set the instrument property to ensure it is instrumented when instrumentation is required.
1502 Instrument: true,
Anton Hansson7f66efa2020-10-08 14:47:23 +01001503 // Set the impl_only libs. Note that the module's "Libs" get appended as well, via the
1504 // addition of &module.properties below.
1505 Libs: module.sdkLibraryProperties.Impl_only_libs,
Paul Duffin77590a82022-04-28 14:13:30 +00001506 // Set the impl_only static libs. Note that the module's "static_libs" get appended as well, via the
1507 // addition of &module.properties below.
1508 Static_libs: module.sdkLibraryProperties.Impl_only_static_libs,
1509 // Pass the apex_available settings down so that the impl library can be statically
1510 // embedded within a library that is added to an APEX. Needed for updatable-media.
1511 Apex_available: module.ApexAvailable(),
Paul Duffin5df79302020-05-16 15:52:12 +01001512 }
1513
1514 properties := []interface{}{
1515 &module.properties,
1516 &module.protoProperties,
1517 &module.deviceProperties,
Liz Kammera7a64f32020-07-09 15:16:41 -07001518 &module.dexProperties,
Paul Duffin5df79302020-05-16 15:52:12 +01001519 &module.dexpreoptProperties,
Colin Cross014489c2020-06-02 20:09:13 -07001520 &module.linter.properties,
Paul Duffin5df79302020-05-16 15:52:12 +01001521 &props,
1522 module.sdkComponentPropertiesForChildLibrary(),
1523 }
1524 mctx.CreateModule(LibraryFactory, properties...)
1525}
1526
Jiyong Parkc678ad32018-04-10 13:07:10 +09001527// Creates a static java library that has API stubs
Paul Duffinf0229202020-04-29 16:47:28 +01001528func (module *SdkLibrary) createStubsLibrary(mctx android.DefaultableHookContext, apiScope *apiScope) {
Jiyong Parkc678ad32018-04-10 13:07:10 +09001529 props := struct {
Dan Willemsen9f435972020-05-28 15:28:00 -07001530 Name *string
1531 Visibility []string
1532 Srcs []string
1533 Installable *bool
1534 Sdk_version *string
1535 System_modules *string
1536 Patch_module *string
1537 Libs []string
Anton Hanssondae54cd2021-04-21 16:30:10 +01001538 Static_libs []string
Dan Willemsen9f435972020-05-28 15:28:00 -07001539 Compile_dex *bool
1540 Java_version *string
1541 Openjdk9 struct {
Sundong Ahn054b19a2018-10-19 13:46:09 +09001542 Srcs []string
1543 Javacflags []string
1544 }
Anton Hansson5fd5d242020-03-27 19:43:19 +00001545 Dist struct {
1546 Targets []string
1547 Dest *string
1548 Dir *string
1549 Tag *string
1550 }
Jiyong Parkc678ad32018-04-10 13:07:10 +09001551 }{}
1552
Paul Duffinc3091c82020-05-08 14:16:20 +01001553 props.Name = proptools.StringPtr(module.stubsLibraryModuleName(apiScope))
Anton Hansson944e77d2020-08-19 11:40:22 +01001554 props.Visibility = childModuleVisibility(module.sdkLibraryProperties.Stubs_library_visibility)
Jiyong Parkc678ad32018-04-10 13:07:10 +09001555 // sources are generated from the droiddoc
Paul Duffinc3091c82020-05-08 14:16:20 +01001556 props.Srcs = []string{":" + module.stubsSourceModuleName(apiScope)}
Paul Duffin12ceb462019-12-24 20:31:31 +00001557 sdkVersion := module.sdkVersionForStubsLibrary(mctx, apiScope)
Paul Duffin52d398a2019-06-11 12:31:14 +01001558 props.Sdk_version = proptools.StringPtr(sdkVersion)
Paul Duffina18abc22020-05-16 18:54:24 +01001559 props.System_modules = module.deviceProperties.System_modules
1560 props.Patch_module = module.properties.Patch_module
Paul Duffin367ab912019-12-23 19:40:36 +00001561 props.Installable = proptools.BoolPtr(false)
Sundong Ahn054b19a2018-10-19 13:46:09 +09001562 props.Libs = module.sdkLibraryProperties.Stub_only_libs
Anton Hanssondae54cd2021-04-21 16:30:10 +01001563 props.Static_libs = module.sdkLibraryProperties.Stub_only_static_libs
Paul Duffine22c2ab2020-05-20 19:35:27 +01001564 // The stub-annotations library contains special versions of the annotations
1565 // with CLASS retention policy, so that they're kept.
1566 if proptools.Bool(module.sdkLibraryProperties.Annotations_enabled) {
1567 props.Libs = append(props.Libs, "stub-annotations")
1568 }
Paul Duffina18abc22020-05-16 18:54:24 +01001569 props.Openjdk9.Srcs = module.properties.Openjdk9.Srcs
1570 props.Openjdk9.Javacflags = module.properties.Openjdk9.Javacflags
Anton Hansson83509b52020-05-21 09:21:57 +01001571 // We compile the stubs for 1.8 in line with the main android.jar stubs, and potential
1572 // interop with older developer tools that don't support 1.9.
1573 props.Java_version = proptools.StringPtr("1.8")
Paul Duffinf4600f62021-05-13 22:34:45 +01001574
1575 // The imports need to be compiled to dex if the java_sdk_library requests it.
1576 compileDex := module.dexProperties.Compile_dex
1577 if module.stubLibrariesCompiledForDex() {
1578 compileDex = proptools.BoolPtr(true)
Sundong Ahndd567f92018-07-31 17:19:11 +09001579 }
Paul Duffinf4600f62021-05-13 22:34:45 +01001580 props.Compile_dex = compileDex
Jiyong Parkc678ad32018-04-10 13:07:10 +09001581
Anton Hansson5fd5d242020-03-27 19:43:19 +00001582 // Dist the class jar artifact for sdk builds.
1583 if !Bool(module.sdkLibraryProperties.No_dist) {
1584 props.Dist.Targets = []string{"sdk", "win_sdk"}
Paul Duffin31310252020-11-20 21:26:20 +00001585 props.Dist.Dest = proptools.StringPtr(fmt.Sprintf("%v.jar", module.distStem()))
Anton Hansson5fd5d242020-03-27 19:43:19 +00001586 props.Dist.Dir = proptools.StringPtr(module.apiDistPath(apiScope))
1587 props.Dist.Tag = proptools.StringPtr(".jar")
1588 }
Jiyong Parkc678ad32018-04-10 13:07:10 +09001589
Paul Duffin859fe962020-05-15 10:20:31 +01001590 mctx.CreateModule(LibraryFactory, &props, module.sdkComponentPropertiesForChildLibrary())
Jiyong Parkc678ad32018-04-10 13:07:10 +09001591}
1592
Paul Duffin6d0886e2020-04-07 18:49:53 +01001593// Creates a droidstubs module that creates stubs source files from the given full source
Paul Duffinc8782502020-04-29 20:45:27 +01001594// files and also updates and checks the API specification files.
Paul Duffin15f34ef2020-07-20 18:04:44 +01001595func (module *SdkLibrary) createStubsSourcesAndApi(mctx android.DefaultableHookContext, apiScope *apiScope, name string, scopeSpecificDroidstubsArgs []string) {
Jiyong Parkc678ad32018-04-10 13:07:10 +09001596 props := struct {
Sundong Ahn054b19a2018-10-19 13:46:09 +09001597 Name *string
Paul Duffin4911a892020-04-29 23:35:13 +01001598 Visibility []string
Sundong Ahn054b19a2018-10-19 13:46:09 +09001599 Srcs []string
1600 Installable *bool
Paul Duffin52d398a2019-06-11 12:31:14 +01001601 Sdk_version *string
Jihoon Kang3198f3c2023-01-26 08:08:52 +00001602 Api_surface *string
Paul Duffin12ceb462019-12-24 20:31:31 +00001603 System_modules *string
Sundong Ahn054b19a2018-10-19 13:46:09 +09001604 Libs []string
Paul Duffin6877e6d2020-09-25 19:59:14 +01001605 Output_javadoc_comments *bool
Paul Duffin11512472019-02-11 15:55:17 +00001606 Arg_files []string
Sundong Ahn054b19a2018-10-19 13:46:09 +09001607 Args *string
Sundong Ahn054b19a2018-10-19 13:46:09 +09001608 Java_version *string
Paul Duffine22c2ab2020-05-20 19:35:27 +01001609 Annotations_enabled *bool
Sundong Ahn054b19a2018-10-19 13:46:09 +09001610 Merge_annotations_dirs []string
1611 Merge_inclusion_annotations_dirs []string
Paul Duffin0ff08bd2020-04-29 13:30:54 +01001612 Generate_stubs *bool
Anton Hanssone87b03d2020-12-21 15:29:34 +00001613 Previous_api *string
Sundong Ahn054b19a2018-10-19 13:46:09 +09001614 Check_api struct {
Anton Hanssone6056152020-12-31 10:37:27 +00001615 Current ApiToCheck
1616 Last_released ApiToCheck
Paul Duffin160fe412020-05-10 19:32:20 +01001617
1618 Api_lint struct {
1619 Enabled *bool
1620 New_since *string
1621 Baseline_file *string
1622 }
Jiyong Park58c518b2018-05-12 22:29:12 +09001623 }
Sundong Ahn1b92c822018-05-29 11:35:17 +09001624 Aidl struct {
1625 Include_dirs []string
1626 Local_include_dirs []string
1627 }
Paul Duffin040e9062020-11-23 17:41:36 +00001628 Dists []android.Dist
Jiyong Parkc678ad32018-04-10 13:07:10 +09001629 }{}
1630
Paul Duffin7b78b4d2020-04-28 14:08:32 +01001631 // The stubs source processing uses the same compile time classpath when extracting the
1632 // API from the implementation library as it does when compiling it. i.e. the same
1633 // * sdk version
1634 // * system_modules
1635 // * libs (static_libs/libs)
Paul Duffin250e6192019-06-07 10:44:37 +01001636
Paul Duffin0ff08bd2020-04-29 13:30:54 +01001637 props.Name = proptools.StringPtr(name)
Anton Hansson944e77d2020-08-19 11:40:22 +01001638 props.Visibility = childModuleVisibility(module.sdkLibraryProperties.Stubs_source_visibility)
Paul Duffina18abc22020-05-16 18:54:24 +01001639 props.Srcs = append(props.Srcs, module.properties.Srcs...)
Anton Hanssonf8ea3722021-09-16 14:24:13 +01001640 props.Srcs = append(props.Srcs, module.sdkLibraryProperties.Api_srcs...)
Paul Duffina18abc22020-05-16 18:54:24 +01001641 props.Sdk_version = module.deviceProperties.Sdk_version
Jihoon Kang3198f3c2023-01-26 08:08:52 +00001642 props.Api_surface = &apiScope.name
Paul Duffina18abc22020-05-16 18:54:24 +01001643 props.System_modules = module.deviceProperties.System_modules
Jiyong Parkc678ad32018-04-10 13:07:10 +09001644 props.Installable = proptools.BoolPtr(false)
Sundong Ahne6f0b052018-06-05 16:46:14 +09001645 // A droiddoc module has only one Libs property and doesn't distinguish between
1646 // shared libs and static libs. So we need to add both of these libs to Libs property.
Paul Duffina18abc22020-05-16 18:54:24 +01001647 props.Libs = module.properties.Libs
1648 props.Libs = append(props.Libs, module.properties.Static_libs...)
Nikita Ioffed732da72022-11-21 12:38:25 +00001649 props.Libs = append(props.Libs, module.sdkLibraryProperties.Stub_only_libs...)
Paul Duffina18abc22020-05-16 18:54:24 +01001650 props.Aidl.Include_dirs = module.deviceProperties.Aidl.Include_dirs
1651 props.Aidl.Local_include_dirs = module.deviceProperties.Aidl.Local_include_dirs
1652 props.Java_version = module.properties.Java_version
Jiyong Parkc678ad32018-04-10 13:07:10 +09001653
Paul Duffine22c2ab2020-05-20 19:35:27 +01001654 props.Annotations_enabled = module.sdkLibraryProperties.Annotations_enabled
Sundong Ahn054b19a2018-10-19 13:46:09 +09001655 props.Merge_annotations_dirs = module.sdkLibraryProperties.Merge_annotations_dirs
1656 props.Merge_inclusion_annotations_dirs = module.sdkLibraryProperties.Merge_inclusion_annotations_dirs
1657
Paul Duffin6d0886e2020-04-07 18:49:53 +01001658 droidstubsArgs := []string{}
Paul Duffin235ffff2019-12-24 10:41:30 +00001659 if len(module.sdkLibraryProperties.Api_packages) != 0 {
Paul Duffin6d0886e2020-04-07 18:49:53 +01001660 droidstubsArgs = append(droidstubsArgs, "--stub-packages "+strings.Join(module.sdkLibraryProperties.Api_packages, ":"))
Paul Duffin235ffff2019-12-24 10:41:30 +00001661 }
1662 if len(module.sdkLibraryProperties.Hidden_api_packages) != 0 {
Paul Duffin6d0886e2020-04-07 18:49:53 +01001663 droidstubsArgs = append(droidstubsArgs,
Paul Duffin235ffff2019-12-24 10:41:30 +00001664 android.JoinWithPrefix(module.sdkLibraryProperties.Hidden_api_packages, " --hide-package "))
1665 }
Paul Duffin6d0886e2020-04-07 18:49:53 +01001666 droidstubsArgs = append(droidstubsArgs, module.sdkLibraryProperties.Droiddoc_options...)
Paul Duffin235ffff2019-12-24 10:41:30 +00001667 disabledWarnings := []string{
Paul Duffin235ffff2019-12-24 10:41:30 +00001668 "BroadcastBehavior",
Paul Duffin235ffff2019-12-24 10:41:30 +00001669 "DeprecationMismatch",
Anton Hansson3c0779a2022-02-18 19:24:30 +00001670 "HiddenSuperclass",
Paul Duffin235ffff2019-12-24 10:41:30 +00001671 "HiddenTypeParameter",
Anton Hansson3c0779a2022-02-18 19:24:30 +00001672 "MissingPermission",
1673 "SdkConstant",
Paul Duffin235ffff2019-12-24 10:41:30 +00001674 "Todo",
1675 "Typo",
Anton Hansson3c0779a2022-02-18 19:24:30 +00001676 "UnavailableSymbol",
Paul Duffin235ffff2019-12-24 10:41:30 +00001677 }
Paul Duffin6d0886e2020-04-07 18:49:53 +01001678 droidstubsArgs = append(droidstubsArgs, android.JoinWithPrefix(disabledWarnings, "--hide "))
Sundong Ahnfb2721f2018-09-17 13:23:09 +09001679
Paul Duffin6877e6d2020-09-25 19:59:14 +01001680 // Output Javadoc comments for public scope.
1681 if apiScope == apiScopePublic {
1682 props.Output_javadoc_comments = proptools.BoolPtr(true)
1683 }
1684
Paul Duffin1fb487d2020-04-07 18:50:10 +01001685 // Add in scope specific arguments.
Paul Duffin0ff08bd2020-04-29 13:30:54 +01001686 droidstubsArgs = append(droidstubsArgs, scopeSpecificDroidstubsArgs...)
Paul Duffin11512472019-02-11 15:55:17 +00001687 props.Arg_files = module.sdkLibraryProperties.Droiddoc_option_files
Paul Duffin6d0886e2020-04-07 18:49:53 +01001688 props.Args = proptools.StringPtr(strings.Join(droidstubsArgs, " "))
Jiyong Parkc678ad32018-04-10 13:07:10 +09001689
Paul Duffin15f34ef2020-07-20 18:04:44 +01001690 // List of APIs identified from the provided source files are created. They are later
1691 // compared against to the not-yet-released (a.k.a current) list of APIs and to the
1692 // last-released (a.k.a numbered) list of API.
1693 currentApiFileName := apiScope.apiFilePrefix + "current.txt"
1694 removedApiFileName := apiScope.apiFilePrefix + "removed.txt"
1695 apiDir := module.getApiDir()
1696 currentApiFileName = path.Join(apiDir, currentApiFileName)
1697 removedApiFileName = path.Join(apiDir, removedApiFileName)
Jiyong Parkc678ad32018-04-10 13:07:10 +09001698
Paul Duffin15f34ef2020-07-20 18:04:44 +01001699 // check against the not-yet-release API
1700 props.Check_api.Current.Api_file = proptools.StringPtr(currentApiFileName)
1701 props.Check_api.Current.Removed_api_file = proptools.StringPtr(removedApiFileName)
Jiyong Park58c518b2018-05-12 22:29:12 +09001702
Paul Duffin958806b2022-05-16 13:10:47 +00001703 if module.compareAgainstLatestApi(apiScope) {
Paul Duffin15f34ef2020-07-20 18:04:44 +01001704 // check against the latest released API
1705 latestApiFilegroupName := proptools.StringPtr(module.latestApiFilegroupName(apiScope))
Anton Hanssone87b03d2020-12-21 15:29:34 +00001706 props.Previous_api = latestApiFilegroupName
Paul Duffin15f34ef2020-07-20 18:04:44 +01001707 props.Check_api.Last_released.Api_file = latestApiFilegroupName
1708 props.Check_api.Last_released.Removed_api_file = proptools.StringPtr(
1709 module.latestRemovedApiFilegroupName(apiScope))
Jaewoong Jung1a97ee02021-03-09 13:25:02 -08001710 props.Check_api.Last_released.Baseline_file = proptools.StringPtr(
1711 module.latestIncompatibilitiesFilegroupName(apiScope))
Paul Duffin160fe412020-05-10 19:32:20 +01001712
Paul Duffin15f34ef2020-07-20 18:04:44 +01001713 if proptools.Bool(module.sdkLibraryProperties.Api_lint.Enabled) {
1714 // Enable api lint.
1715 props.Check_api.Api_lint.Enabled = proptools.BoolPtr(true)
1716 props.Check_api.Api_lint.New_since = latestApiFilegroupName
Paul Duffin160fe412020-05-10 19:32:20 +01001717
Paul Duffin15f34ef2020-07-20 18:04:44 +01001718 // If it exists then pass a lint-baseline.txt through to droidstubs.
1719 baselinePath := path.Join(apiDir, apiScope.apiFilePrefix+"lint-baseline.txt")
1720 baselinePathRelativeToRoot := path.Join(mctx.ModuleDir(), baselinePath)
1721 paths, err := mctx.GlobWithDeps(baselinePathRelativeToRoot, nil)
1722 if err != nil {
1723 mctx.ModuleErrorf("error checking for presence of %s: %s", baselinePathRelativeToRoot, err)
1724 }
1725 if len(paths) == 1 {
1726 props.Check_api.Api_lint.Baseline_file = proptools.StringPtr(baselinePath)
1727 } else if len(paths) != 0 {
1728 mctx.ModuleErrorf("error checking for presence of %s: expected one path, found: %v", baselinePathRelativeToRoot, paths)
Paul Duffin160fe412020-05-10 19:32:20 +01001729 }
1730 }
Paul Duffin15f34ef2020-07-20 18:04:44 +01001731 }
Jiyong Park58c518b2018-05-12 22:29:12 +09001732
Paul Duffin15f34ef2020-07-20 18:04:44 +01001733 if !Bool(module.sdkLibraryProperties.No_dist) {
Paul Duffin040e9062020-11-23 17:41:36 +00001734 // Dist the api txt and removed api txt artifacts for sdk builds.
1735 distDir := proptools.StringPtr(path.Join(module.apiDistPath(apiScope), "api"))
1736 for _, p := range []struct {
1737 tag string
1738 pattern string
1739 }{
1740 {tag: ".api.txt", pattern: "%s.txt"},
1741 {tag: ".removed-api.txt", pattern: "%s-removed.txt"},
1742 } {
1743 props.Dists = append(props.Dists, android.Dist{
1744 Targets: []string{"sdk", "win_sdk"},
1745 Dir: distDir,
1746 Dest: proptools.StringPtr(fmt.Sprintf(p.pattern, module.distStem())),
1747 Tag: proptools.StringPtr(p.tag),
1748 })
1749 }
Anton Hansson5fd5d242020-03-27 19:43:19 +00001750 }
1751
Jihoon Kangd48abd52023-02-02 22:32:31 +00001752 mctx.CreateModule(DroidstubsFactory, &props).(*Droidstubs).CallHookIfAvailable(mctx)
Jiyong Parkc678ad32018-04-10 13:07:10 +09001753}
1754
Paul Duffin958806b2022-05-16 13:10:47 +00001755func (module *SdkLibrary) compareAgainstLatestApi(apiScope *apiScope) bool {
1756 return !(apiScope.unstable || module.sdkLibraryProperties.Unsafe_ignore_missing_latest_api)
1757}
1758
Paul Duffinea8f8082021-06-24 13:25:57 +01001759// Implements android.ApexModule
Jooyung Han5e9013b2020-03-10 06:23:13 +09001760func (module *SdkLibrary) DepIsInSameApex(mctx android.BaseModuleContext, dep android.Module) bool {
1761 depTag := mctx.OtherModuleDependencyTag(dep)
1762 if depTag == xmlPermissionsFileTag {
1763 return true
1764 }
1765 return module.Library.DepIsInSameApex(mctx, dep)
1766}
1767
Paul Duffinea8f8082021-06-24 13:25:57 +01001768// Implements android.ApexModule
1769func (module *SdkLibrary) UniqueApexVariations() bool {
1770 return module.uniqueApexVariations()
1771}
1772
Jiyong Parkc678ad32018-04-10 13:07:10 +09001773// Creates the xml file that publicizes the runtime library
Paul Duffinf0229202020-04-29 16:47:28 +01001774func (module *SdkLibrary) createXmlFile(mctx android.DefaultableHookContext) {
Pedro Loureiroc3621422021-09-28 15:40:23 +00001775 moduleMinApiLevel := module.Library.MinSdkVersion(mctx).ApiLevel
1776 var moduleMinApiLevelStr = moduleMinApiLevel.String()
1777 if moduleMinApiLevel == android.NoneApiLevel {
1778 moduleMinApiLevelStr = "current"
1779 }
Jiyong Parke3833882020-02-17 17:28:10 +09001780 props := struct {
Pedro Loureiroc3621422021-09-28 15:40:23 +00001781 Name *string
1782 Lib_name *string
1783 Apex_available []string
1784 On_bootclasspath_since *string
1785 On_bootclasspath_before *string
1786 Min_device_sdk *string
1787 Max_device_sdk *string
1788 Sdk_library_min_api_level *string
Jiyong Parke3833882020-02-17 17:28:10 +09001789 }{
Pedro Loureiroc3621422021-09-28 15:40:23 +00001790 Name: proptools.StringPtr(module.xmlPermissionsModuleName()),
1791 Lib_name: proptools.StringPtr(module.BaseModuleName()),
1792 Apex_available: module.ApexProperties.Apex_available,
1793 On_bootclasspath_since: module.commonSdkLibraryProperties.On_bootclasspath_since,
1794 On_bootclasspath_before: module.commonSdkLibraryProperties.On_bootclasspath_before,
1795 Min_device_sdk: module.commonSdkLibraryProperties.Min_device_sdk,
1796 Max_device_sdk: module.commonSdkLibraryProperties.Max_device_sdk,
1797 Sdk_library_min_api_level: &moduleMinApiLevelStr,
Jiyong Parkc678ad32018-04-10 13:07:10 +09001798 }
Jiyong Parke3833882020-02-17 17:28:10 +09001799
Jiyong Parke3833882020-02-17 17:28:10 +09001800 mctx.CreateModule(sdkLibraryXmlFactory, &props)
Jiyong Parkc678ad32018-04-10 13:07:10 +09001801}
1802
Jiyong Parkf1691d22021-03-29 20:11:58 +09001803func PrebuiltJars(ctx android.BaseModuleContext, baseName string, s android.SdkSpec) android.Paths {
Jiyong Park54105c42021-03-31 18:17:53 +09001804 var ver android.ApiLevel
Jiyong Parkf1691d22021-03-29 20:11:58 +09001805 var kind android.SdkKind
1806 if s.UsePrebuilt(ctx) {
Jiyong Park54105c42021-03-31 18:17:53 +09001807 ver = s.ApiLevel
Jiyong Parkf1691d22021-03-29 20:11:58 +09001808 kind = s.Kind
Jiyong Parkc678ad32018-04-10 13:07:10 +09001809 } else {
Jiyong Park6a927c42020-01-21 02:03:43 +09001810 // We don't have prebuilt SDK for the specific sdkVersion.
1811 // Instead of breaking the build, fallback to use "system_current"
Jiyong Park54105c42021-03-31 18:17:53 +09001812 ver = android.FutureApiLevel
Jiyong Parkf1691d22021-03-29 20:11:58 +09001813 kind = android.SdkSystem
Sundong Ahn054b19a2018-10-19 13:46:09 +09001814 }
Jiyong Park6a927c42020-01-21 02:03:43 +09001815
1816 dir := filepath.Join("prebuilts", "sdk", ver.String(), kind.String())
Paul Duffin50061512020-01-21 16:31:05 +00001817 jar := filepath.Join(dir, baseName+".jar")
Sundong Ahn054b19a2018-10-19 13:46:09 +09001818 jarPath := android.ExistentPathForSource(ctx, jar)
Sundong Ahnae418ac2019-02-28 15:01:28 +09001819 if !jarPath.Valid() {
Colin Cross07c88562020-01-07 09:34:44 -08001820 if ctx.Config().AllowMissingDependencies() {
1821 return android.Paths{android.PathForSource(ctx, jar)}
1822 } else {
Jiyong Parkf1691d22021-03-29 20:11:58 +09001823 ctx.PropertyErrorf("sdk_library", "invalid sdk version %q, %q does not exist", s.Raw, jar)
Colin Cross07c88562020-01-07 09:34:44 -08001824 }
Sundong Ahnae418ac2019-02-28 15:01:28 +09001825 return nil
1826 }
Sundong Ahn054b19a2018-10-19 13:46:09 +09001827 return android.Paths{jarPath.Path()}
1828}
1829
Colin Crossaede88c2020-08-11 12:17:01 -07001830// 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 +01001831//
1832// If either this or the other module are on the platform then this will return
1833// false.
Colin Cross56a83212020-09-15 18:30:11 -07001834func withinSameApexesAs(ctx android.BaseModuleContext, other android.Module) bool {
1835 apexInfo := ctx.Provider(android.ApexInfoProvider).(android.ApexInfo)
1836 otherApexInfo := ctx.OtherModuleProvider(other, android.ApexInfoProvider).(android.ApexInfo)
Jiyong Parkab50b072021-05-12 17:13:56 +09001837 return len(otherApexInfo.InApexVariants) > 0 && reflect.DeepEqual(apexInfo.InApexVariants, otherApexInfo.InApexVariants)
Paul Duffin9b879592020-05-26 13:21:35 +01001838}
1839
Jiyong Parkf1691d22021-03-29 20:11:58 +09001840func (module *SdkLibrary) sdkJars(ctx android.BaseModuleContext, sdkVersion android.SdkSpec, headerJars bool) android.Paths {
Jiyong Park932cdfe2020-05-28 00:19:53 +09001841 // If the client doesn't set sdk_version, but if this library prefers stubs over
1842 // the impl library, let's provide the widest API surface possible. To do so,
1843 // force override sdk_version to module_current so that the closest possible API
1844 // surface could be found in selectHeaderJarsForSdkVersion
Jiyong Parkf1691d22021-03-29 20:11:58 +09001845 if module.defaultsToStubs() && !sdkVersion.Specified() {
Jiyong Park92315372021-04-02 08:45:46 +09001846 sdkVersion = android.SdkSpecFrom(ctx, "module_current")
Jiyong Park932cdfe2020-05-28 00:19:53 +09001847 }
Paul Duffind1b3a922020-01-22 11:57:20 +00001848
Paul Duffindaaa3322020-05-26 18:13:57 +01001849 // Only provide access to the implementation library if it is actually built.
1850 if module.requiresRuntimeImplementationLibrary() {
1851 // Check any special cases for java_sdk_library.
1852 //
1853 // Only allow access to the implementation library in the following condition:
1854 // * No sdk_version specified on the referencing module.
Paul Duffin9b879592020-05-26 13:21:35 +01001855 // * The referencing module is in the same apex as this.
Jiyong Parkf1691d22021-03-29 20:11:58 +09001856 if sdkVersion.Kind == android.SdkPrivate || withinSameApexesAs(ctx, module) {
Paul Duffindaaa3322020-05-26 18:13:57 +01001857 if headerJars {
1858 return module.HeaderJars()
1859 } else {
1860 return module.ImplementationJars()
1861 }
Sundong Ahn054b19a2018-10-19 13:46:09 +09001862 }
Jiyong Parkc678ad32018-04-10 13:07:10 +09001863 }
Paul Duffinb05d4292020-05-20 12:19:10 +01001864
Paul Duffin23970f42020-05-20 14:20:02 +01001865 return module.selectHeaderJarsForSdkVersion(ctx, sdkVersion)
Jiyong Parkc678ad32018-04-10 13:07:10 +09001866}
1867
Sundong Ahn241cd372018-07-13 16:16:44 +09001868// to satisfy SdkLibraryDependency interface
Jiyong Parkf1691d22021-03-29 20:11:58 +09001869func (module *SdkLibrary) SdkHeaderJars(ctx android.BaseModuleContext, sdkVersion android.SdkSpec) android.Paths {
Paul Duffind1b3a922020-01-22 11:57:20 +00001870 return module.sdkJars(ctx, sdkVersion, true /*headerJars*/)
1871}
1872
1873// to satisfy SdkLibraryDependency interface
Jiyong Parkf1691d22021-03-29 20:11:58 +09001874func (module *SdkLibrary) SdkImplementationJars(ctx android.BaseModuleContext, sdkVersion android.SdkSpec) android.Paths {
Paul Duffind1b3a922020-01-22 11:57:20 +00001875 return module.sdkJars(ctx, sdkVersion, false /*headerJars*/)
Sundong Ahn241cd372018-07-13 16:16:44 +09001876}
1877
Colin Cross571cccf2019-02-04 11:22:08 -08001878var javaSdkLibrariesKey = android.NewOnceKey("javaSdkLibraries")
1879
Jiyong Park82484c02018-04-23 21:41:26 +09001880func javaSdkLibraries(config android.Config) *[]string {
Colin Cross571cccf2019-02-04 11:22:08 -08001881 return config.Once(javaSdkLibrariesKey, func() interface{} {
Jiyong Park82484c02018-04-23 21:41:26 +09001882 return &[]string{}
1883 }).(*[]string)
1884}
1885
Paul Duffin749f98f2019-12-30 17:23:46 +00001886func (module *SdkLibrary) getApiDir() string {
1887 return proptools.StringDefault(module.sdkLibraryProperties.Api_dir, "api")
1888}
1889
Jiyong Parkc678ad32018-04-10 13:07:10 +09001890// For a java_sdk_library module, create internal modules for stubs, docs,
1891// runtime libs and xml file. If requested, the stubs and docs are created twice
1892// once for public API level and once for system API level
Paul Duffinf0229202020-04-29 16:47:28 +01001893func (module *SdkLibrary) CreateInternalModules(mctx android.DefaultableHookContext) {
1894 // If the module has been disabled then don't create any child modules.
1895 if !module.Enabled() {
1896 return
1897 }
1898
Paul Duffina18abc22020-05-16 18:54:24 +01001899 if len(module.properties.Srcs) == 0 {
Inseob Kimc0907f12019-02-08 21:00:45 +09001900 mctx.PropertyErrorf("srcs", "java_sdk_library must specify srcs")
Jooyung Han58f26ab2019-12-18 15:34:32 +09001901 return
Inseob Kimc0907f12019-02-08 21:00:45 +09001902 }
1903
Paul Duffin37e0b772019-12-30 17:20:10 +00001904 // If this builds against standard libraries (i.e. is not part of the core libraries)
Paul Duffin4f5c1ef2020-11-19 14:53:43 +00001905 // then assume it provides both system and test apis.
Jiyong Parkf1691d22021-03-29 20:11:58 +09001906 sdkDep := decodeSdkDep(mctx, android.SdkContext(&module.Library))
Paul Duffin37e0b772019-12-30 17:20:10 +00001907 hasSystemAndTestApis := sdkDep.hasStandardLibs()
Paul Duffin3375e352020-04-28 10:44:03 +01001908 module.sdkLibraryProperties.Generate_system_and_test_apis = hasSystemAndTestApis
Paul Duffin4f5c1ef2020-11-19 14:53:43 +00001909
Jaewoong Jung18aefc12020-12-21 09:11:10 -08001910 missingCurrentApi := false
Inseob Kim8098faa2019-03-18 10:19:51 +09001911
Paul Duffin3375e352020-04-28 10:44:03 +01001912 generatedScopes := module.getGeneratedApiScopes(mctx)
Paul Duffind1b3a922020-01-22 11:57:20 +00001913
Paul Duffin749f98f2019-12-30 17:23:46 +00001914 apiDir := module.getApiDir()
Paul Duffin3375e352020-04-28 10:44:03 +01001915 for _, scope := range generatedScopes {
Inseob Kim8098faa2019-03-18 10:19:51 +09001916 for _, api := range []string{"current.txt", "removed.txt"} {
Paul Duffind1b3a922020-01-22 11:57:20 +00001917 path := path.Join(mctx.ModuleDir(), apiDir, scope.apiFilePrefix+api)
Inseob Kim8098faa2019-03-18 10:19:51 +09001918 p := android.ExistentPathForSource(mctx, path)
1919 if !p.Valid() {
Colin Cross18f840c2021-05-20 17:56:54 -07001920 if mctx.Config().AllowMissingDependencies() {
1921 mctx.AddMissingDependencies([]string{path})
1922 } else {
1923 mctx.ModuleErrorf("Current api file %#v doesn't exist", path)
1924 missingCurrentApi = true
1925 }
Inseob Kim8098faa2019-03-18 10:19:51 +09001926 }
1927 }
1928 }
1929
Jaewoong Jung18aefc12020-12-21 09:11:10 -08001930 if missingCurrentApi {
Inseob Kim8098faa2019-03-18 10:19:51 +09001931 script := "build/soong/scripts/gen-java-current-api-files.sh"
1932 p := android.ExistentPathForSource(mctx, script)
1933
1934 if !p.Valid() {
1935 panic(fmt.Sprintf("script file %s doesn't exist", script))
1936 }
1937
1938 mctx.ModuleErrorf("One or more current api files are missing. "+
1939 "You can update them by:\n"+
Paul Duffin37e0b772019-12-30 17:20:10 +00001940 "%s %q %s && m update-api",
Paul Duffind1b3a922020-01-22 11:57:20 +00001941 script, filepath.Join(mctx.ModuleDir(), apiDir),
Paul Duffin3375e352020-04-28 10:44:03 +01001942 strings.Join(generatedScopes.Strings(func(s *apiScope) string { return s.apiFilePrefix }), " "))
Inseob Kim8098faa2019-03-18 10:19:51 +09001943 return
1944 }
1945
Paul Duffin3375e352020-04-28 10:44:03 +01001946 for _, scope := range generatedScopes {
Paul Duffin15f34ef2020-07-20 18:04:44 +01001947 // Use the stubs source name for legacy reasons.
1948 module.createStubsSourcesAndApi(mctx, scope, module.stubsSourceModuleName(scope), scope.droidstubsArgs)
Paul Duffin0ff08bd2020-04-29 13:30:54 +01001949
Paul Duffind1b3a922020-01-22 11:57:20 +00001950 module.createStubsLibrary(mctx, scope)
Inseob Kimc0907f12019-02-08 21:00:45 +09001951 }
1952
Paul Duffindfa131e2020-05-15 20:37:11 +01001953 if module.requiresRuntimeImplementationLibrary() {
Paul Duffin5df79302020-05-16 15:52:12 +01001954 // Create child module to create an implementation library.
1955 //
1956 // This temporarily creates a second implementation library that can be explicitly
1957 // referenced.
1958 //
1959 // TODO(b/156618935) - update comment once only one implementation library is created.
1960 module.createImplLibrary(mctx)
1961
Paul Duffindfa131e2020-05-15 20:37:11 +01001962 // Only create an XML permissions file that declares the library as being usable
1963 // as a shared library if required.
1964 if module.sharedLibrary() {
1965 module.createXmlFile(mctx)
1966 }
Paul Duffin43db9be2019-12-30 17:35:49 +00001967
1968 // record java_sdk_library modules so that they are exported to make
1969 javaSdkLibraries := javaSdkLibraries(mctx.Config())
1970 javaSdkLibrariesLock.Lock()
1971 defer javaSdkLibrariesLock.Unlock()
1972 *javaSdkLibraries = append(*javaSdkLibraries, module.BaseModuleName())
1973 }
Anton Hansson7f66efa2020-10-08 14:47:23 +01001974
Paul Duffin77590a82022-04-28 14:13:30 +00001975 // 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 +01001976 module.properties.Libs = append(module.properties.Libs, module.sdkLibraryProperties.Impl_only_libs...)
Paul Duffin77590a82022-04-28 14:13:30 +00001977 module.properties.Static_libs = append(module.properties.Static_libs, module.sdkLibraryProperties.Impl_only_static_libs...)
Inseob Kimc0907f12019-02-08 21:00:45 +09001978}
1979
1980func (module *SdkLibrary) InitSdkLibraryProperties() {
Colin Crossce6734e2020-06-15 16:09:53 -07001981 module.addHostAndDeviceProperties()
1982 module.AddProperties(&module.sdkLibraryProperties)
Sundong Ahn054b19a2018-10-19 13:46:09 +09001983
Paul Duffin71b33cc2021-06-23 11:39:47 +01001984 module.initSdkLibraryComponent(module)
Paul Duffin859fe962020-05-15 10:20:31 +01001985
Paul Duffina18abc22020-05-16 18:54:24 +01001986 module.properties.Installable = proptools.BoolPtr(true)
1987 module.deviceProperties.IsSDKLibrary = true
Inseob Kimc0907f12019-02-08 21:00:45 +09001988}
Sundong Ahn054b19a2018-10-19 13:46:09 +09001989
Paul Duffindfa131e2020-05-15 20:37:11 +01001990func (module *SdkLibrary) requiresRuntimeImplementationLibrary() bool {
1991 return !proptools.Bool(module.sdkLibraryProperties.Api_only)
1992}
1993
Jiyong Park932cdfe2020-05-28 00:19:53 +09001994func (module *SdkLibrary) defaultsToStubs() bool {
1995 return proptools.Bool(module.sdkLibraryProperties.Default_to_stubs)
1996}
1997
Paul Duffin1b1e8062020-05-08 13:44:43 +01001998// Defines how to name the individual component modules the sdk library creates.
1999type sdkLibraryComponentNamingScheme interface {
2000 stubsLibraryModuleName(scope *apiScope, baseName string) string
2001
2002 stubsSourceModuleName(scope *apiScope, baseName string) string
Paul Duffin1b1e8062020-05-08 13:44:43 +01002003}
2004
2005type defaultNamingScheme struct {
2006}
2007
2008func (s *defaultNamingScheme) stubsLibraryModuleName(scope *apiScope, baseName string) string {
2009 return scope.stubsLibraryModuleName(baseName)
2010}
2011
2012func (s *defaultNamingScheme) stubsSourceModuleName(scope *apiScope, baseName string) string {
2013 return scope.stubsSourceModuleName(baseName)
2014}
2015
Paul Duffin1b1e8062020-05-08 13:44:43 +01002016var _ sdkLibraryComponentNamingScheme = (*defaultNamingScheme)(nil)
2017
Jaewoong Jungbc15e3a2021-03-10 17:02:43 -08002018func moduleStubLinkType(name string) (stub bool, ret sdkLinkType) {
Anton Hansson2d0c1942020-05-25 12:20:51 +01002019 // This suffix-based approach is fragile and could potentially mis-trigger.
2020 // TODO(b/155164730): Clean this up when modules no longer reference sdk_lib stubs directly.
Anton Hansson08f476b2021-04-07 15:32:19 +01002021 if strings.HasSuffix(name, apiScopePublic.stubsLibraryModuleNameSuffix()) {
2022 if name == "hwbinder.stubs" || name == "libcore_private.stubs" {
2023 // Due to a previous bug, these modules were not considered stubs, so we retain that.
2024 return false, javaPlatform
2025 }
Anton Hansson2d0c1942020-05-25 12:20:51 +01002026 return true, javaSdk
2027 }
Anton Hansson08f476b2021-04-07 15:32:19 +01002028 if strings.HasSuffix(name, apiScopeSystem.stubsLibraryModuleNameSuffix()) {
Anton Hansson2d0c1942020-05-25 12:20:51 +01002029 return true, javaSystem
2030 }
Anton Hansson08f476b2021-04-07 15:32:19 +01002031 if strings.HasSuffix(name, apiScopeModuleLib.stubsLibraryModuleNameSuffix()) {
Anton Hansson2d0c1942020-05-25 12:20:51 +01002032 return true, javaModule
2033 }
Anton Hansson08f476b2021-04-07 15:32:19 +01002034 if strings.HasSuffix(name, apiScopeTest.stubsLibraryModuleNameSuffix()) {
Anton Hansson2d0c1942020-05-25 12:20:51 +01002035 return true, javaSystem
2036 }
2037 return false, javaPlatform
2038}
2039
Jaewoong Jung4f158ee2019-07-11 10:05:35 -07002040// java_sdk_library is a special Java library that provides optional platform APIs to apps.
2041// In practice, it can be viewed as a combination of several modules: 1) stubs library that clients
2042// are linked against to, 2) droiddoc module that internally generates API stubs source files,
2043// 3) the real runtime shared library that implements the APIs, and 4) XML file for adding
2044// the runtime lib to the classpath at runtime if requested via <uses-library>.
Inseob Kimc0907f12019-02-08 21:00:45 +09002045func SdkLibraryFactory() android.Module {
2046 module := &SdkLibrary{}
Paul Duffinc3091c82020-05-08 14:16:20 +01002047
2048 // Initialize information common between source and prebuilt.
Paul Duffin71b33cc2021-06-23 11:39:47 +01002049 module.initCommon(module)
Paul Duffinc3091c82020-05-08 14:16:20 +01002050
Inseob Kimc0907f12019-02-08 21:00:45 +09002051 module.InitSdkLibraryProperties()
Jooyung Han58f26ab2019-12-18 15:34:32 +09002052 android.InitApexModule(module)
Sundong Ahn054b19a2018-10-19 13:46:09 +09002053 InitJavaModule(module, android.HostAndDeviceSupported)
Paul Duffin3375e352020-04-28 10:44:03 +01002054
2055 // Initialize the map from scope to scope specific properties.
2056 scopeToProperties := make(map[*apiScope]*ApiScopeProperties)
2057 for _, scope := range allApiScopes {
2058 scopeToProperties[scope] = scope.scopeSpecificProperties(module)
2059 }
2060 module.scopeToProperties = scopeToProperties
2061
Paul Duffin4911a892020-04-29 23:35:13 +01002062 // Add the properties containing visibility rules so that they are checked.
Paul Duffin5df79302020-05-16 15:52:12 +01002063 android.AddVisibilityProperty(module, "impl_library_visibility", &module.sdkLibraryProperties.Impl_library_visibility)
Paul Duffin4911a892020-04-29 23:35:13 +01002064 android.AddVisibilityProperty(module, "stubs_library_visibility", &module.sdkLibraryProperties.Stubs_library_visibility)
2065 android.AddVisibilityProperty(module, "stubs_source_visibility", &module.sdkLibraryProperties.Stubs_source_visibility)
2066
Paul Duffin1b1e8062020-05-08 13:44:43 +01002067 module.SetDefaultableHook(func(ctx android.DefaultableHookContext) {
Paul Duffindfa131e2020-05-15 20:37:11 +01002068 // If no implementation is required then it cannot be used as a shared library
2069 // either.
2070 if !module.requiresRuntimeImplementationLibrary() {
2071 // If shared_library has been explicitly set to true then it is incompatible
2072 // with api_only: true.
2073 if proptools.Bool(module.commonSdkLibraryProperties.Shared_library) {
2074 ctx.PropertyErrorf("api_only/shared_library", "inconsistent settings, shared_library and api_only cannot both be true")
2075 }
2076 // Set shared_library: false.
2077 module.commonSdkLibraryProperties.Shared_library = proptools.BoolPtr(false)
2078 }
2079
Paul Duffin1b1e8062020-05-08 13:44:43 +01002080 if module.initCommonAfterDefaultsApplied(ctx) {
2081 module.CreateInternalModules(ctx)
2082 }
2083 })
Jiyong Parkc678ad32018-04-10 13:07:10 +09002084 return module
2085}
Colin Cross79c7c262019-04-17 11:11:46 -07002086
2087//
2088// SDK library prebuilts
2089//
2090
Paul Duffin56d44902020-01-31 13:36:25 +00002091// Properties associated with each api scope.
2092type sdkLibraryScopeProperties struct {
Colin Cross79c7c262019-04-17 11:11:46 -07002093 Jars []string `android:"path"`
2094
2095 Sdk_version *string
2096
Colin Cross79c7c262019-04-17 11:11:46 -07002097 // List of shared java libs that this module has dependencies to
2098 Libs []string
Paul Duffin3d1248c2020-04-09 00:10:17 +01002099
Paul Duffinc8782502020-04-29 20:45:27 +01002100 // The stubs source.
Paul Duffin3d1248c2020-04-09 00:10:17 +01002101 Stub_srcs []string `android:"path"`
Paul Duffin1fd005d2020-04-09 01:08:11 +01002102
2103 // The current.txt
Paul Duffin0f8faff2020-05-20 16:18:00 +01002104 Current_api *string `android:"path"`
Paul Duffin1fd005d2020-04-09 01:08:11 +01002105
2106 // The removed.txt
Paul Duffin0f8faff2020-05-20 16:18:00 +01002107 Removed_api *string `android:"path"`
Anton Hanssond78eb762021-09-21 15:25:12 +01002108
2109 // Annotation zip
2110 Annotations *string `android:"path"`
Colin Cross79c7c262019-04-17 11:11:46 -07002111}
2112
Paul Duffin56d44902020-01-31 13:36:25 +00002113type sdkLibraryImportProperties struct {
Paul Duffinfcfd7912020-01-31 17:54:30 +00002114 // List of shared java libs, common to all scopes, that this module has
2115 // dependencies to
2116 Libs []string
Paul Duffin1267d872021-04-16 17:21:36 +01002117
2118 // If set to true, compile dex files for the stubs. Defaults to false.
2119 Compile_dex *bool
Paul Duffin869de142021-07-15 14:14:41 +01002120
2121 // If not empty, classes are restricted to the specified packages and their sub-packages.
Paul Duffin869de142021-07-15 14:14:41 +01002122 Permitted_packages []string
Paul Duffin56d44902020-01-31 13:36:25 +00002123}
2124
Paul Duffineedc5d52020-06-12 17:46:39 +01002125type SdkLibraryImport struct {
Colin Cross79c7c262019-04-17 11:11:46 -07002126 android.ModuleBase
2127 android.DefaultableModuleBase
2128 prebuilt android.Prebuilt
Paul Duffindd46f712020-02-10 13:37:10 +00002129 android.ApexModuleBase
Colin Cross79c7c262019-04-17 11:11:46 -07002130
Paul Duffin37856732021-02-26 14:24:15 +00002131 hiddenAPI
Jiakai Zhang204356f2021-09-09 08:12:46 +00002132 dexpreopter
Paul Duffin37856732021-02-26 14:24:15 +00002133
Colin Cross79c7c262019-04-17 11:11:46 -07002134 properties sdkLibraryImportProperties
2135
Paul Duffin46a26a82020-04-07 19:27:04 +01002136 // Map from api scope to the scope specific property structure.
2137 scopeProperties map[*apiScope]*sdkLibraryScopeProperties
2138
Paul Duffin56d44902020-01-31 13:36:25 +00002139 commonToSdkLibraryAndImport
Paul Duffineedc5d52020-06-12 17:46:39 +01002140
2141 // The reference to the implementation library created by the source module.
2142 // Is nil if the source module does not exist.
2143 implLibraryModule *Library
2144
2145 // The reference to the xml permissions module created by the source module.
2146 // Is nil if the source module does not exist.
2147 xmlPermissionsFileModule *sdkLibraryXml
Paul Duffin39853512021-02-26 11:09:39 +00002148
Jeongik Chad5fe8782021-07-08 01:13:11 +09002149 // Build path to the dex implementation jar obtained from the prebuilt_apex, if any.
Martin Stjernholm8be1e6d2021-09-15 03:34:04 +01002150 dexJarFile OptionalDexJarPath
Jeongik Chad5fe8782021-07-08 01:13:11 +09002151
2152 // Expected install file path of the source module(sdk_library)
2153 // or dex implementation jar obtained from the prebuilt_apex, if any.
2154 installFile android.Path
Colin Cross79c7c262019-04-17 11:11:46 -07002155}
2156
Paul Duffineedc5d52020-06-12 17:46:39 +01002157var _ SdkLibraryDependency = (*SdkLibraryImport)(nil)
Colin Cross79c7c262019-04-17 11:11:46 -07002158
Paul Duffin46a26a82020-04-07 19:27:04 +01002159// The type of a structure that contains a field of type sdkLibraryScopeProperties
2160// for each apiscope in allApiScopes, e.g. something like:
Colin Crossd079e0b2022-08-16 10:27:33 -07002161//
2162// struct {
2163// Public sdkLibraryScopeProperties
2164// System sdkLibraryScopeProperties
2165// ...
2166// }
Paul Duffin46a26a82020-04-07 19:27:04 +01002167var allScopeStructType = createAllScopePropertiesStructType()
2168
2169// Dynamically create a structure type for each apiscope in allApiScopes.
2170func createAllScopePropertiesStructType() reflect.Type {
2171 var fields []reflect.StructField
2172 for _, apiScope := range allApiScopes {
2173 field := reflect.StructField{
2174 Name: apiScope.fieldName,
2175 Type: reflect.TypeOf(sdkLibraryScopeProperties{}),
2176 }
2177 fields = append(fields, field)
2178 }
2179
2180 return reflect.StructOf(fields)
2181}
2182
2183// Create an instance of the scope specific structure type and return a map
2184// from apiscope to a pointer to each scope specific field.
2185func createPropertiesInstance() (interface{}, map[*apiScope]*sdkLibraryScopeProperties) {
2186 allScopePropertiesPtr := reflect.New(allScopeStructType)
2187 allScopePropertiesStruct := allScopePropertiesPtr.Elem()
2188 scopeProperties := make(map[*apiScope]*sdkLibraryScopeProperties)
2189
2190 for _, apiScope := range allApiScopes {
2191 field := allScopePropertiesStruct.FieldByName(apiScope.fieldName)
2192 scopeProperties[apiScope] = field.Addr().Interface().(*sdkLibraryScopeProperties)
2193 }
2194
2195 return allScopePropertiesPtr.Interface(), scopeProperties
2196}
2197
Jaewoong Jung4f158ee2019-07-11 10:05:35 -07002198// java_sdk_library_import imports a prebuilt java_sdk_library.
Colin Cross79c7c262019-04-17 11:11:46 -07002199func sdkLibraryImportFactory() android.Module {
Paul Duffineedc5d52020-06-12 17:46:39 +01002200 module := &SdkLibraryImport{}
Colin Cross79c7c262019-04-17 11:11:46 -07002201
Paul Duffin46a26a82020-04-07 19:27:04 +01002202 allScopeProperties, scopeToProperties := createPropertiesInstance()
2203 module.scopeProperties = scopeToProperties
Jiakai Zhang9c4dc192023-02-09 00:09:24 +08002204 module.AddProperties(&module.properties, allScopeProperties, &module.importDexpreoptProperties)
Colin Cross79c7c262019-04-17 11:11:46 -07002205
Paul Duffinc3091c82020-05-08 14:16:20 +01002206 // Initialize information common between source and prebuilt.
Paul Duffin71b33cc2021-06-23 11:39:47 +01002207 module.initCommon(module)
Paul Duffinc3091c82020-05-08 14:16:20 +01002208
Paul Duffin0bdcb272020-02-06 15:24:57 +00002209 android.InitPrebuiltModule(module, &[]string{""})
Paul Duffindd46f712020-02-10 13:37:10 +00002210 android.InitApexModule(module)
Colin Cross79c7c262019-04-17 11:11:46 -07002211 InitJavaModule(module, android.HostAndDeviceSupported)
2212
Paul Duffin1b1e8062020-05-08 13:44:43 +01002213 module.SetDefaultableHook(func(mctx android.DefaultableHookContext) {
2214 if module.initCommonAfterDefaultsApplied(mctx) {
2215 module.createInternalModules(mctx)
2216 }
2217 })
Colin Cross79c7c262019-04-17 11:11:46 -07002218 return module
2219}
2220
Paul Duffin630b11e2021-07-15 13:35:26 +01002221var _ PermittedPackagesForUpdatableBootJars = (*SdkLibraryImport)(nil)
2222
2223func (module *SdkLibraryImport) PermittedPackagesForUpdatableBootJars() []string {
2224 return module.properties.Permitted_packages
2225}
2226
Paul Duffineedc5d52020-06-12 17:46:39 +01002227func (module *SdkLibraryImport) Prebuilt() *android.Prebuilt {
Colin Cross79c7c262019-04-17 11:11:46 -07002228 return &module.prebuilt
2229}
2230
Paul Duffineedc5d52020-06-12 17:46:39 +01002231func (module *SdkLibraryImport) Name() string {
Colin Cross79c7c262019-04-17 11:11:46 -07002232 return module.prebuilt.Name(module.ModuleBase.Name())
2233}
2234
Paul Duffineedc5d52020-06-12 17:46:39 +01002235func (module *SdkLibraryImport) createInternalModules(mctx android.DefaultableHookContext) {
Colin Cross79c7c262019-04-17 11:11:46 -07002236
Paul Duffin50061512020-01-21 16:31:05 +00002237 // If the build is configured to use prebuilts then force this to be preferred.
Jeongik Cha816a23a2020-07-08 01:09:23 +09002238 if mctx.Config().AlwaysUsePrebuiltSdks() {
Paul Duffin50061512020-01-21 16:31:05 +00002239 module.prebuilt.ForcePrefer()
2240 }
2241
Paul Duffin46a26a82020-04-07 19:27:04 +01002242 for apiScope, scopeProperties := range module.scopeProperties {
Paul Duffin56d44902020-01-31 13:36:25 +00002243 if len(scopeProperties.Jars) == 0 {
2244 continue
2245 }
2246
Paul Duffinbbb546b2020-04-09 00:07:11 +01002247 module.createJavaImportForStubs(mctx, apiScope, scopeProperties)
Paul Duffin3d1248c2020-04-09 00:10:17 +01002248
Paul Duffin0f8faff2020-05-20 16:18:00 +01002249 if len(scopeProperties.Stub_srcs) > 0 {
2250 module.createPrebuiltStubsSources(mctx, apiScope, scopeProperties)
2251 }
Paul Duffin56d44902020-01-31 13:36:25 +00002252 }
Colin Cross79c7c262019-04-17 11:11:46 -07002253
2254 javaSdkLibraries := javaSdkLibraries(mctx.Config())
2255 javaSdkLibrariesLock.Lock()
2256 defer javaSdkLibrariesLock.Unlock()
2257 *javaSdkLibraries = append(*javaSdkLibraries, module.BaseModuleName())
2258}
2259
Paul Duffineedc5d52020-06-12 17:46:39 +01002260func (module *SdkLibraryImport) createJavaImportForStubs(mctx android.DefaultableHookContext, apiScope *apiScope, scopeProperties *sdkLibraryScopeProperties) {
Paul Duffinbbb546b2020-04-09 00:07:11 +01002261 // Creates a java import for the jar with ".stubs" suffix
2262 props := struct {
Paul Duffin1dbe3ca2020-05-16 09:57:59 +01002263 Name *string
2264 Sdk_version *string
2265 Libs []string
2266 Jars []string
Paul Duffin1267d872021-04-16 17:21:36 +01002267 Compile_dex *bool
Paul Duffinbf4de042022-09-27 12:41:52 +01002268
2269 android.UserSuppliedPrebuiltProperties
Paul Duffinbbb546b2020-04-09 00:07:11 +01002270 }{}
Paul Duffinc3091c82020-05-08 14:16:20 +01002271 props.Name = proptools.StringPtr(module.stubsLibraryModuleName(apiScope))
Paul Duffinbbb546b2020-04-09 00:07:11 +01002272 props.Sdk_version = scopeProperties.Sdk_version
2273 // Prepend any of the libs from the legacy public properties to the libs for each of the
2274 // scopes to avoid having to duplicate them in each scope.
2275 props.Libs = append(module.properties.Libs, scopeProperties.Libs...)
2276 props.Jars = scopeProperties.Jars
Paul Duffin1dbe3ca2020-05-16 09:57:59 +01002277
Paul Duffin38b57852020-05-13 16:08:09 +01002278 // The imports are preferred if the java_sdk_library_import is preferred.
Paul Duffinbf4de042022-09-27 12:41:52 +01002279 props.CopyUserSuppliedPropertiesFromPrebuilt(&module.prebuilt)
Paul Duffin859fe962020-05-15 10:20:31 +01002280
Paul Duffin1267d872021-04-16 17:21:36 +01002281 // The imports need to be compiled to dex if the java_sdk_library_import requests it.
Paul Duffinf4600f62021-05-13 22:34:45 +01002282 compileDex := module.properties.Compile_dex
2283 if module.stubLibrariesCompiledForDex() {
2284 compileDex = proptools.BoolPtr(true)
2285 }
2286 props.Compile_dex = compileDex
Paul Duffin1267d872021-04-16 17:21:36 +01002287
Paul Duffin859fe962020-05-15 10:20:31 +01002288 mctx.CreateModule(ImportFactory, &props, module.sdkComponentPropertiesForChildLibrary())
Paul Duffinbbb546b2020-04-09 00:07:11 +01002289}
2290
Paul Duffineedc5d52020-06-12 17:46:39 +01002291func (module *SdkLibraryImport) createPrebuiltStubsSources(mctx android.DefaultableHookContext, apiScope *apiScope, scopeProperties *sdkLibraryScopeProperties) {
Paul Duffin3d1248c2020-04-09 00:10:17 +01002292 props := struct {
Paul Duffinbf4de042022-09-27 12:41:52 +01002293 Name *string
2294 Srcs []string
2295
2296 android.UserSuppliedPrebuiltProperties
Paul Duffin3d1248c2020-04-09 00:10:17 +01002297 }{}
Paul Duffinc3091c82020-05-08 14:16:20 +01002298 props.Name = proptools.StringPtr(module.stubsSourceModuleName(apiScope))
Paul Duffin3d1248c2020-04-09 00:10:17 +01002299 props.Srcs = scopeProperties.Stub_srcs
Paul Duffin38b57852020-05-13 16:08:09 +01002300
2301 // The stubs source is preferred if the java_sdk_library_import is preferred.
Paul Duffinbf4de042022-09-27 12:41:52 +01002302 props.CopyUserSuppliedPropertiesFromPrebuilt(&module.prebuilt)
2303
2304 mctx.CreateModule(PrebuiltStubsSourcesFactory, &props)
Paul Duffin3d1248c2020-04-09 00:10:17 +01002305}
2306
Paul Duffin44f1d842020-06-26 20:17:02 +01002307// Add the dependencies on the child module in the component deps mutator so that it
2308// creates references to the prebuilt and not the source modules.
2309func (module *SdkLibraryImport) ComponentDepsMutator(ctx android.BottomUpMutatorContext) {
Paul Duffin46a26a82020-04-07 19:27:04 +01002310 for apiScope, scopeProperties := range module.scopeProperties {
Paul Duffin56d44902020-01-31 13:36:25 +00002311 if len(scopeProperties.Jars) == 0 {
2312 continue
2313 }
2314
2315 // Add dependencies to the prebuilt stubs library
Paul Duffin864116c2021-04-02 10:24:13 +01002316 ctx.AddVariationDependencies(nil, apiScope.stubsTag, android.PrebuiltNameFromSource(module.stubsLibraryModuleName(apiScope)))
Paul Duffin0f8faff2020-05-20 16:18:00 +01002317
2318 if len(scopeProperties.Stub_srcs) > 0 {
2319 // Add dependencies to the prebuilt stubs source library
Paul Duffin864116c2021-04-02 10:24:13 +01002320 ctx.AddVariationDependencies(nil, apiScope.stubsSourceTag, android.PrebuiltNameFromSource(module.stubsSourceModuleName(apiScope)))
Paul Duffin0f8faff2020-05-20 16:18:00 +01002321 }
Paul Duffin56d44902020-01-31 13:36:25 +00002322 }
Paul Duffin44f1d842020-06-26 20:17:02 +01002323}
2324
2325// Add other dependencies as normal.
2326func (module *SdkLibraryImport) DepsMutator(ctx android.BottomUpMutatorContext) {
Paul Duffineedc5d52020-06-12 17:46:39 +01002327
2328 implName := module.implLibraryModuleName()
2329 if ctx.OtherModuleExists(implName) {
2330 ctx.AddVariationDependencies(nil, implLibraryTag, implName)
2331
2332 xmlPermissionsModuleName := module.xmlPermissionsModuleName()
2333 if module.sharedLibrary() && ctx.OtherModuleExists(xmlPermissionsModuleName) {
2334 // Add dependency to the rule for generating the xml permissions file
2335 ctx.AddDependency(module, xmlPermissionsFileTag, xmlPermissionsModuleName)
2336 }
2337 }
Colin Cross79c7c262019-04-17 11:11:46 -07002338}
2339
Jiakai Zhang204356f2021-09-09 08:12:46 +00002340func (module *SdkLibraryImport) AndroidMkEntries() []android.AndroidMkEntries {
2341 // For an SDK library imported from a prebuilt APEX, we don't need a Make module for itself, as we
2342 // don't need to install it. However, we need to add its dexpreopt outputs as sub-modules, if it
2343 // is preopted.
2344 dexpreoptEntries := module.dexpreopter.AndroidMkEntriesForApex()
2345 return append(dexpreoptEntries, android.AndroidMkEntries{Disabled: true})
2346}
2347
Jiyong Park45bf82e2020-12-15 22:29:02 +09002348var _ android.ApexModule = (*SdkLibraryImport)(nil)
2349
2350// Implements android.ApexModule
Paul Duffineedc5d52020-06-12 17:46:39 +01002351func (module *SdkLibraryImport) DepIsInSameApex(mctx android.BaseModuleContext, dep android.Module) bool {
2352 depTag := mctx.OtherModuleDependencyTag(dep)
2353 if depTag == xmlPermissionsFileTag {
2354 return true
2355 }
2356
2357 // None of the other dependencies of the java_sdk_library_import are in the same apex
2358 // as the one that references this module.
2359 return false
2360}
2361
Jiyong Park45bf82e2020-12-15 22:29:02 +09002362// Implements android.ApexModule
Dan Albertc8060532020-07-22 22:32:17 -07002363func (module *SdkLibraryImport) ShouldSupportSdkVersion(ctx android.BaseModuleContext,
2364 sdkVersion android.ApiLevel) error {
Jooyung Han749dc692020-04-15 11:03:39 +09002365 // we don't check prebuilt modules for sdk_version
2366 return nil
2367}
2368
Paul Duffinea8f8082021-06-24 13:25:57 +01002369// Implements android.ApexModule
2370func (module *SdkLibraryImport) UniqueApexVariations() bool {
2371 return module.uniqueApexVariations()
2372}
2373
Paul Duffin09817d62022-04-28 17:45:11 +01002374// MinSdkVersion - Implements hiddenAPIModule
2375func (module *SdkLibraryImport) MinSdkVersion(ctx android.EarlyModuleContext) android.SdkSpec {
2376 return android.SdkSpecNone
2377}
2378
2379var _ hiddenAPIModule = (*SdkLibraryImport)(nil)
2380
Paul Duffineedc5d52020-06-12 17:46:39 +01002381func (module *SdkLibraryImport) OutputFiles(tag string) (android.Paths, error) {
Paul Duffin1e940d52022-04-29 14:21:25 +01002382 paths, err := module.commonOutputFiles(tag)
2383 if paths != nil || err != nil {
2384 return paths, err
2385 }
2386 if module.implLibraryModule != nil {
2387 return module.implLibraryModule.OutputFiles(tag)
2388 } else {
2389 return nil, nil
2390 }
Paul Duffin46dc45a2020-05-14 15:39:10 +01002391}
2392
Paul Duffineedc5d52020-06-12 17:46:39 +01002393func (module *SdkLibraryImport) GenerateAndroidBuildActions(ctx android.ModuleContext) {
Paul Duffina2ae7e02020-09-11 11:55:00 +01002394 module.generateCommonBuildActions(ctx)
2395
Jeongik Chad5fe8782021-07-08 01:13:11 +09002396 // Assume that source module(sdk_library) is installed in /<sdk_library partition>/framework
2397 module.installFile = android.PathForModuleInstall(ctx, "framework", module.Stem()+".jar")
2398
Paul Duffin0f8faff2020-05-20 16:18:00 +01002399 // Record the paths to the prebuilt stubs library and stubs source.
Colin Cross79c7c262019-04-17 11:11:46 -07002400 ctx.VisitDirectDeps(func(to android.Module) {
2401 tag := ctx.OtherModuleDependencyTag(to)
2402
Paul Duffin0f8faff2020-05-20 16:18:00 +01002403 // Extract information from any of the scope specific dependencies.
2404 if scopeTag, ok := tag.(scopeDependencyTag); ok {
2405 apiScope := scopeTag.apiScope
2406 scopePaths := module.getScopePathsCreateIfNeeded(apiScope)
2407
2408 // Extract information from the dependency. The exact information extracted
2409 // is determined by the nature of the dependency which is determined by the tag.
2410 scopeTag.extractDepInfo(ctx, to, scopePaths)
Paul Duffineedc5d52020-06-12 17:46:39 +01002411 } else if tag == implLibraryTag {
2412 if implLibrary, ok := to.(*Library); ok {
2413 module.implLibraryModule = implLibrary
2414 } else {
2415 ctx.ModuleErrorf("implementation library must be of type *java.Library but was %T", to)
2416 }
2417 } else if tag == xmlPermissionsFileTag {
2418 if xmlPermissionsFileModule, ok := to.(*sdkLibraryXml); ok {
2419 module.xmlPermissionsFileModule = xmlPermissionsFileModule
2420 } else {
2421 ctx.ModuleErrorf("xml permissions file module must be of type *sdkLibraryXml but was %T", to)
2422 }
Colin Cross79c7c262019-04-17 11:11:46 -07002423 }
2424 })
Paul Duffin0f8faff2020-05-20 16:18:00 +01002425
2426 // Populate the scope paths with information from the properties.
2427 for apiScope, scopeProperties := range module.scopeProperties {
2428 if len(scopeProperties.Jars) == 0 {
2429 continue
2430 }
2431
2432 paths := module.getScopePathsCreateIfNeeded(apiScope)
Anton Hanssond78eb762021-09-21 15:25:12 +01002433 paths.annotationsZip = android.OptionalPathForModuleSrc(ctx, scopeProperties.Annotations)
Paul Duffin0f8faff2020-05-20 16:18:00 +01002434 paths.currentApiFilePath = android.OptionalPathForModuleSrc(ctx, scopeProperties.Current_api)
2435 paths.removedApiFilePath = android.OptionalPathForModuleSrc(ctx, scopeProperties.Removed_api)
2436 }
Paul Duffin39853512021-02-26 11:09:39 +00002437
2438 if ctx.Device() {
2439 // If this is a variant created for a prebuilt_apex then use the dex implementation jar
2440 // obtained from the associated deapexer module.
2441 ai := ctx.Provider(android.ApexInfoProvider).(android.ApexInfo)
2442 if ai.ForPrebuiltApex {
Paul Duffin39853512021-02-26 11:09:39 +00002443 // Get the path of the dex implementation jar from the `deapexer` module.
Martin Stjernholm44825602021-09-17 01:44:12 +01002444 di := android.FindDeapexerProviderForModule(ctx)
2445 if di == nil {
2446 return // An error has been reported by FindDeapexerProviderForModule.
2447 }
Jiakai Zhang81e46812023-02-08 21:56:07 +08002448 dexJarFileApexRootRelative := apexRootRelativePathToJavaLib(module.BaseModuleName())
2449 if dexOutputPath := di.PrebuiltExportPath(dexJarFileApexRootRelative); dexOutputPath != nil {
Martin Stjernholm8be1e6d2021-09-15 03:34:04 +01002450 dexJarFile := makeDexJarPathFromPath(dexOutputPath)
2451 module.dexJarFile = dexJarFile
Jiakai Zhang204356f2021-09-09 08:12:46 +00002452 installPath := android.PathForModuleInPartitionInstall(
Jiakai Zhang81e46812023-02-08 21:56:07 +08002453 ctx, "apex", ai.ApexVariationName, dexJarFileApexRootRelative)
Jiakai Zhang204356f2021-09-09 08:12:46 +00002454 module.installFile = installPath
Martin Stjernholm8be1e6d2021-09-15 03:34:04 +01002455 module.initHiddenAPI(ctx, dexJarFile, module.findScopePaths(apiScopePublic).stubsImplPath[0], nil)
Jiakai Zhang204356f2021-09-09 08:12:46 +00002456
Jiakai Zhang204356f2021-09-09 08:12:46 +00002457 module.dexpreopter.installPath = module.dexpreopter.getInstallPath(ctx, installPath)
2458 module.dexpreopter.isSDKLibrary = true
2459 module.dexpreopter.uncompressedDex = shouldUncompressDex(ctx, &module.dexpreopter)
Jiakai Zhang81e46812023-02-08 21:56:07 +08002460
2461 if profilePath := di.PrebuiltExportPath(dexJarFileApexRootRelative + ".prof"); profilePath != nil {
2462 module.dexpreopter.inputProfilePathOnHost = profilePath
2463 }
2464
2465 // Dexpreopting.
Jiakai Zhang204356f2021-09-09 08:12:46 +00002466 module.dexpreopt(ctx, dexOutputPath)
Paul Duffin39853512021-02-26 11:09:39 +00002467 } else {
2468 // This should never happen as a variant for a prebuilt_apex is only created if the
2469 // prebuilt_apex has been configured to export the java library dex file.
Martin Stjernholm44825602021-09-17 01:44:12 +01002470 ctx.ModuleErrorf("internal error: no dex implementation jar available from prebuilt APEX %s", di.ApexModuleName())
Paul Duffin39853512021-02-26 11:09:39 +00002471 }
2472 }
2473 }
Colin Cross79c7c262019-04-17 11:11:46 -07002474}
2475
Jiyong Parkf1691d22021-03-29 20:11:58 +09002476func (module *SdkLibraryImport) sdkJars(ctx android.BaseModuleContext, sdkVersion android.SdkSpec, headerJars bool) android.Paths {
Paul Duffineedc5d52020-06-12 17:46:39 +01002477
2478 // For consistency with SdkLibrary make the implementation jar available to libraries that
2479 // are within the same APEX.
2480 implLibraryModule := module.implLibraryModule
Colin Cross56a83212020-09-15 18:30:11 -07002481 if implLibraryModule != nil && withinSameApexesAs(ctx, module) {
Paul Duffineedc5d52020-06-12 17:46:39 +01002482 if headerJars {
2483 return implLibraryModule.HeaderJars()
2484 } else {
2485 return implLibraryModule.ImplementationJars()
2486 }
2487 }
2488
Paul Duffin23970f42020-05-20 14:20:02 +01002489 return module.selectHeaderJarsForSdkVersion(ctx, sdkVersion)
Paul Duffin56d44902020-01-31 13:36:25 +00002490}
2491
Colin Cross79c7c262019-04-17 11:11:46 -07002492// to satisfy SdkLibraryDependency interface
Jiyong Parkf1691d22021-03-29 20:11:58 +09002493func (module *SdkLibraryImport) SdkHeaderJars(ctx android.BaseModuleContext, sdkVersion android.SdkSpec) android.Paths {
Colin Cross79c7c262019-04-17 11:11:46 -07002494 // This module is just a wrapper for the prebuilt stubs.
Paul Duffineedc5d52020-06-12 17:46:39 +01002495 return module.sdkJars(ctx, sdkVersion, true)
Colin Cross79c7c262019-04-17 11:11:46 -07002496}
2497
2498// to satisfy SdkLibraryDependency interface
Jiyong Parkf1691d22021-03-29 20:11:58 +09002499func (module *SdkLibraryImport) SdkImplementationJars(ctx android.BaseModuleContext, sdkVersion android.SdkSpec) android.Paths {
Colin Cross79c7c262019-04-17 11:11:46 -07002500 // This module is just a wrapper for the stubs.
Paul Duffineedc5d52020-06-12 17:46:39 +01002501 return module.sdkJars(ctx, sdkVersion, false)
2502}
2503
Ulya Trafimovichdbf31662020-12-17 12:07:54 +00002504// to satisfy UsesLibraryDependency interface
Martin Stjernholm8be1e6d2021-09-15 03:34:04 +01002505func (module *SdkLibraryImport) DexJarBuildPath() OptionalDexJarPath {
Paul Duffin39853512021-02-26 11:09:39 +00002506 // The dex implementation jar extracted from the .apex file should be used in preference to the
2507 // source.
Martin Stjernholm8be1e6d2021-09-15 03:34:04 +01002508 if module.dexJarFile.IsSet() {
Paul Duffin39853512021-02-26 11:09:39 +00002509 return module.dexJarFile
2510 }
Paul Duffineedc5d52020-06-12 17:46:39 +01002511 if module.implLibraryModule == nil {
Martin Stjernholm8be1e6d2021-09-15 03:34:04 +01002512 return makeUnsetDexJarPath()
Paul Duffineedc5d52020-06-12 17:46:39 +01002513 } else {
2514 return module.implLibraryModule.DexJarBuildPath()
2515 }
2516}
2517
Ulya Trafimovichdbf31662020-12-17 12:07:54 +00002518// to satisfy UsesLibraryDependency interface
Ulya Trafimovich31e444e2020-08-14 17:32:16 +01002519func (module *SdkLibraryImport) DexJarInstallPath() android.Path {
Jeongik Chad5fe8782021-07-08 01:13:11 +09002520 return module.installFile
Ulya Trafimovich31e444e2020-08-14 17:32:16 +01002521}
2522
Ulya Trafimovichdbf31662020-12-17 12:07:54 +00002523// to satisfy UsesLibraryDependency interface
2524func (module *SdkLibraryImport) ClassLoaderContexts() dexpreopt.ClassLoaderContextMap {
2525 return nil
2526}
2527
Paul Duffineedc5d52020-06-12 17:46:39 +01002528// to satisfy apex.javaDependency interface
2529func (module *SdkLibraryImport) JacocoReportClassesFile() android.Path {
2530 if module.implLibraryModule == nil {
2531 return nil
2532 } else {
2533 return module.implLibraryModule.JacocoReportClassesFile()
2534 }
2535}
2536
2537// to satisfy apex.javaDependency interface
Colin Cross08dca382020-07-21 20:31:17 -07002538func (module *SdkLibraryImport) LintDepSets() LintDepSets {
2539 if module.implLibraryModule == nil {
2540 return LintDepSets{}
2541 } else {
2542 return module.implLibraryModule.LintDepSets()
2543 }
2544}
2545
Spandan Das17854f52022-01-14 21:19:14 +00002546func (module *SdkLibraryImport) GetStrictUpdatabilityLinting() bool {
Jaewoong Jung476b9d62021-05-10 15:30:00 -07002547 if module.implLibraryModule == nil {
2548 return false
2549 } else {
Spandan Das17854f52022-01-14 21:19:14 +00002550 return module.implLibraryModule.GetStrictUpdatabilityLinting()
Jaewoong Jung476b9d62021-05-10 15:30:00 -07002551 }
2552}
2553
Spandan Das17854f52022-01-14 21:19:14 +00002554func (module *SdkLibraryImport) SetStrictUpdatabilityLinting(strictLinting bool) {
Jaewoong Jung476b9d62021-05-10 15:30:00 -07002555 if module.implLibraryModule != nil {
Spandan Das17854f52022-01-14 21:19:14 +00002556 module.implLibraryModule.SetStrictUpdatabilityLinting(strictLinting)
Jaewoong Jung476b9d62021-05-10 15:30:00 -07002557 }
2558}
2559
Colin Cross08dca382020-07-21 20:31:17 -07002560// to satisfy apex.javaDependency interface
Paul Duffineedc5d52020-06-12 17:46:39 +01002561func (module *SdkLibraryImport) Stem() string {
2562 return module.BaseModuleName()
Colin Cross79c7c262019-04-17 11:11:46 -07002563}
Jiyong Parke3833882020-02-17 17:28:10 +09002564
Paul Duffin44b481b2020-06-17 16:59:43 +01002565var _ ApexDependency = (*SdkLibraryImport)(nil)
2566
2567// to satisfy java.ApexDependency interface
2568func (module *SdkLibraryImport) HeaderJars() android.Paths {
2569 if module.implLibraryModule == nil {
2570 return nil
2571 } else {
2572 return module.implLibraryModule.HeaderJars()
2573 }
2574}
2575
2576// to satisfy java.ApexDependency interface
2577func (module *SdkLibraryImport) ImplementationAndResourcesJars() android.Paths {
2578 if module.implLibraryModule == nil {
2579 return nil
2580 } else {
2581 return module.implLibraryModule.ImplementationAndResourcesJars()
2582 }
2583}
2584
Jiakai Zhang204356f2021-09-09 08:12:46 +00002585// to satisfy java.DexpreopterInterface interface
2586func (module *SdkLibraryImport) IsInstallable() bool {
2587 return true
2588}
2589
Paul Duffinfef55002021-06-17 14:56:05 +01002590var _ android.RequiredFilesFromPrebuiltApex = (*SdkLibraryImport)(nil)
2591
Paul Duffinb4bbf2c2021-06-17 15:59:07 +01002592func (module *SdkLibraryImport) RequiredFilesFromPrebuiltApex(ctx android.BaseModuleContext) []string {
Paul Duffinfef55002021-06-17 14:56:05 +01002593 name := module.BaseModuleName()
Jiakai Zhang81e46812023-02-08 21:56:07 +08002594 return requiredFilesFromPrebuiltApexForImport(name, &module.dexpreopter)
Paul Duffinfef55002021-06-17 14:56:05 +01002595}
2596
Jiyong Parke3833882020-02-17 17:28:10 +09002597// java_sdk_library_xml
Jiyong Parke3833882020-02-17 17:28:10 +09002598type sdkLibraryXml struct {
2599 android.ModuleBase
2600 android.DefaultableModuleBase
2601 android.ApexModuleBase
2602
2603 properties sdkLibraryXmlProperties
2604
2605 outputFilePath android.OutputPath
2606 installDirPath android.InstallPath
Colin Cross56a83212020-09-15 18:30:11 -07002607
2608 hideApexVariantFromMake bool
Jiyong Parke3833882020-02-17 17:28:10 +09002609}
2610
2611type sdkLibraryXmlProperties struct {
2612 // canonical name of the lib
2613 Lib_name *string
Pedro Loureiro9956e5e2021-09-07 17:21:59 +00002614
2615 // Signals that this shared library is part of the bootclasspath starting
2616 // on the version indicated in this attribute.
2617 //
2618 // This will make platforms at this level and above to ignore
2619 // <uses-library> tags with this library name because the library is already
2620 // available
2621 On_bootclasspath_since *string
2622
2623 // Signals that this shared library was part of the bootclasspath before
2624 // (but not including) the version indicated in this attribute.
2625 //
2626 // The system will automatically add a <uses-library> tag with this library to
2627 // apps that target any SDK less than the version indicated in this attribute.
2628 On_bootclasspath_before *string
2629
2630 // Indicates that PackageManager should ignore this shared library if the
2631 // platform is below the version indicated in this attribute.
2632 //
2633 // This means that the device won't recognise this library as installed.
2634 Min_device_sdk *string
2635
2636 // Indicates that PackageManager should ignore this shared library if the
2637 // platform is above the version indicated in this attribute.
2638 //
2639 // This means that the device won't recognise this library as installed.
2640 Max_device_sdk *string
Pedro Loureiroc3621422021-09-28 15:40:23 +00002641
2642 // The SdkLibrary's min api level as a string
2643 //
2644 // This value comes from the ApiLevel of the MinSdkVersion property.
2645 Sdk_library_min_api_level *string
Jiyong Parke3833882020-02-17 17:28:10 +09002646}
2647
2648// java_sdk_library_xml builds the permission xml file for a java_sdk_library.
2649// Not to be used directly by users. java_sdk_library internally uses this.
2650func sdkLibraryXmlFactory() android.Module {
2651 module := &sdkLibraryXml{}
2652
2653 module.AddProperties(&module.properties)
2654
2655 android.InitApexModule(module)
2656 android.InitAndroidArchModule(module, android.DeviceSupported, android.MultilibCommon)
2657
2658 return module
2659}
2660
Colin Crossaede88c2020-08-11 12:17:01 -07002661func (module *sdkLibraryXml) UniqueApexVariations() bool {
2662 // sdkLibraryXml needs a unique variation per APEX because the generated XML file contains the path to the
2663 // mounted APEX, which contains the name of the APEX.
2664 return true
2665}
2666
Jiyong Parke3833882020-02-17 17:28:10 +09002667// from android.PrebuiltEtcModule
Jooyung Han0703fd82020-08-26 22:11:53 +09002668func (module *sdkLibraryXml) BaseDir() string {
2669 return "etc"
2670}
2671
2672// from android.PrebuiltEtcModule
Jiyong Parke3833882020-02-17 17:28:10 +09002673func (module *sdkLibraryXml) SubDir() string {
2674 return "permissions"
2675}
2676
2677// from android.PrebuiltEtcModule
2678func (module *sdkLibraryXml) OutputFile() android.OutputPath {
2679 return module.outputFilePath
2680}
2681
2682// from android.ApexModule
2683func (module *sdkLibraryXml) AvailableFor(what string) bool {
2684 return true
2685}
2686
2687func (module *sdkLibraryXml) DepsMutator(ctx android.BottomUpMutatorContext) {
2688 // do nothing
2689}
2690
Jiyong Park45bf82e2020-12-15 22:29:02 +09002691var _ android.ApexModule = (*sdkLibraryXml)(nil)
2692
2693// Implements android.ApexModule
Dan Albertc8060532020-07-22 22:32:17 -07002694func (module *sdkLibraryXml) ShouldSupportSdkVersion(ctx android.BaseModuleContext,
2695 sdkVersion android.ApiLevel) error {
Jooyung Han749dc692020-04-15 11:03:39 +09002696 // sdkLibraryXml doesn't need to be checked separately because java_sdk_library is checked
2697 return nil
2698}
2699
Jiyong Parke3833882020-02-17 17:28:10 +09002700// File path to the runtime implementation library
Colin Cross56a83212020-09-15 18:30:11 -07002701func (module *sdkLibraryXml) implPath(ctx android.ModuleContext) string {
Jiyong Parke3833882020-02-17 17:28:10 +09002702 implName := proptools.String(module.properties.Lib_name)
Colin Cross56a83212020-09-15 18:30:11 -07002703 if apexInfo := ctx.Provider(android.ApexInfoProvider).(android.ApexInfo); !apexInfo.IsForPlatform() {
Colin Crosse07f2312020-08-13 11:24:56 -07002704 // TODO(b/146468504): ApexVariationName() is only a soong module name, not apex name.
Jiyong Parke3833882020-02-17 17:28:10 +09002705 // In most cases, this works fine. But when apex_name is set or override_apex is used
2706 // this can be wrong.
Colin Cross56a83212020-09-15 18:30:11 -07002707 return fmt.Sprintf("/apex/%s/javalib/%s.jar", apexInfo.ApexVariationName, implName)
Jiyong Parke3833882020-02-17 17:28:10 +09002708 }
2709 partition := "system"
2710 if module.SocSpecific() {
2711 partition = "vendor"
2712 } else if module.DeviceSpecific() {
2713 partition = "odm"
2714 } else if module.ProductSpecific() {
2715 partition = "product"
2716 } else if module.SystemExtSpecific() {
2717 partition = "system_ext"
2718 }
2719 return "/" + partition + "/framework/" + implName + ".jar"
2720}
2721
Pedro Loureiro9956e5e2021-09-07 17:21:59 +00002722func formattedOptionalSdkLevelAttribute(ctx android.ModuleContext, attrName string, value *string) string {
2723 if value == nil {
2724 return ""
2725 }
2726 apiLevel, err := android.ApiLevelFromUser(ctx, *value)
2727 if err != nil {
Pedro Loureiroba6682f2021-10-29 09:32:32 +00002728 // attributes in bp files have underscores but in the xml have dashes.
2729 ctx.PropertyErrorf(strings.ReplaceAll(attrName, "-", "_"), err.Error())
Pedro Loureiro9956e5e2021-09-07 17:21:59 +00002730 return ""
2731 }
Pedro Loureirob638c622021-12-22 15:28:05 +00002732 if apiLevel.IsCurrent() {
2733 // passing "current" would always mean a future release, never the current (or the current in
2734 // progress) which means some conditions would never be triggered.
2735 ctx.PropertyErrorf(strings.ReplaceAll(attrName, "-", "_"),
2736 `"current" is not an allowed value for this attribute`)
2737 return ""
2738 }
Pedro Loureiro48991222022-06-17 20:01:21 +00002739 // "safeValue" is safe because it translates finalized codenames to a string
2740 // with their SDK int.
2741 safeValue := apiLevel.String()
2742 return formattedOptionalAttribute(attrName, &safeValue)
Pedro Loureiro9956e5e2021-09-07 17:21:59 +00002743}
2744
2745// formats an attribute for the xml permissions file if the value is not null
2746// returns empty string otherwise
2747func formattedOptionalAttribute(attrName string, value *string) string {
2748 if value == nil {
2749 return ""
2750 }
2751 return fmt.Sprintf(` %s=\"%s\"\n`, attrName, *value)
2752}
2753
2754func (module *sdkLibraryXml) permissionsContents(ctx android.ModuleContext) string {
2755 libName := proptools.String(module.properties.Lib_name)
2756 libNameAttr := formattedOptionalAttribute("name", &libName)
2757 filePath := module.implPath(ctx)
2758 filePathAttr := formattedOptionalAttribute("file", &filePath)
Pedro Loureiroba6682f2021-10-29 09:32:32 +00002759 implicitFromAttr := formattedOptionalSdkLevelAttribute(ctx, "on-bootclasspath-since", module.properties.On_bootclasspath_since)
2760 implicitUntilAttr := formattedOptionalSdkLevelAttribute(ctx, "on-bootclasspath-before", module.properties.On_bootclasspath_before)
2761 minSdkAttr := formattedOptionalSdkLevelAttribute(ctx, "min-device-sdk", module.properties.Min_device_sdk)
2762 maxSdkAttr := formattedOptionalSdkLevelAttribute(ctx, "max-device-sdk", module.properties.Max_device_sdk)
Pedro Loureiro196d3e62021-12-22 19:53:01 +00002763 // <library> is understood in all android versions whereas <apex-library> is only understood from API T (and ignored before that).
2764 // 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 +00002765 var libraryTag string
2766 if module.properties.Min_device_sdk != nil {
Pedro Loureiro196d3e62021-12-22 19:53:01 +00002767 libraryTag = ` <apex-library\n`
Pedro Loureiroc3621422021-09-28 15:40:23 +00002768 } else {
2769 libraryTag = ` <library\n`
2770 }
Pedro Loureiro9956e5e2021-09-07 17:21:59 +00002771
2772 return strings.Join([]string{
2773 `<?xml version=\"1.0\" encoding=\"utf-8\"?>\n`,
2774 `<!-- Copyright (C) 2018 The Android Open Source Project\n`,
2775 `\n`,
2776 ` Licensed under the Apache License, Version 2.0 (the \"License\");\n`,
2777 ` you may not use this file except in compliance with the License.\n`,
2778 ` You may obtain a copy of the License at\n`,
2779 `\n`,
2780 ` http://www.apache.org/licenses/LICENSE-2.0\n`,
2781 `\n`,
2782 ` Unless required by applicable law or agreed to in writing, software\n`,
2783 ` distributed under the License is distributed on an \"AS IS\" BASIS,\n`,
2784 ` WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\n`,
2785 ` See the License for the specific language governing permissions and\n`,
2786 ` limitations under the License.\n`,
2787 `-->\n`,
2788 `<permissions>\n`,
Pedro Loureiroc3621422021-09-28 15:40:23 +00002789 libraryTag,
Pedro Loureiro9956e5e2021-09-07 17:21:59 +00002790 libNameAttr,
2791 filePathAttr,
2792 implicitFromAttr,
2793 implicitUntilAttr,
2794 minSdkAttr,
2795 maxSdkAttr,
2796 ` />\n`,
2797 `</permissions>\n`}, "")
2798}
2799
Jiyong Parke3833882020-02-17 17:28:10 +09002800func (module *sdkLibraryXml) GenerateAndroidBuildActions(ctx android.ModuleContext) {
Colin Cross56a83212020-09-15 18:30:11 -07002801 module.hideApexVariantFromMake = !ctx.Provider(android.ApexInfoProvider).(android.ApexInfo).IsForPlatform()
2802
Jiyong Parke3833882020-02-17 17:28:10 +09002803 libName := proptools.String(module.properties.Lib_name)
Pedro Loureiroc3621422021-09-28 15:40:23 +00002804 module.selfValidate(ctx)
Pedro Loureiro9956e5e2021-09-07 17:21:59 +00002805 xmlContent := module.permissionsContents(ctx)
Jiyong Parke3833882020-02-17 17:28:10 +09002806
2807 module.outputFilePath = android.PathForModuleOut(ctx, libName+".xml").OutputPath
Colin Crossf1a035e2020-11-16 17:32:30 -08002808 rule := android.NewRuleBuilder(pctx, ctx)
Jiyong Parke3833882020-02-17 17:28:10 +09002809 rule.Command().
2810 Text("/bin/bash -c \"echo -e '" + xmlContent + "'\" > ").
2811 Output(module.outputFilePath)
2812
Colin Crossf1a035e2020-11-16 17:32:30 -08002813 rule.Build("java_sdk_xml", "Permission XML")
Jiyong Parke3833882020-02-17 17:28:10 +09002814
2815 module.installDirPath = android.PathForModuleInstall(ctx, "etc", module.SubDir())
2816}
2817
2818func (module *sdkLibraryXml) AndroidMkEntries() []android.AndroidMkEntries {
Colin Cross56a83212020-09-15 18:30:11 -07002819 if module.hideApexVariantFromMake {
satayev8f088b02021-12-06 11:40:46 +00002820 return []android.AndroidMkEntries{{
Jiyong Parke3833882020-02-17 17:28:10 +09002821 Disabled: true,
2822 }}
2823 }
2824
satayev8f088b02021-12-06 11:40:46 +00002825 return []android.AndroidMkEntries{{
Jiyong Parke3833882020-02-17 17:28:10 +09002826 Class: "ETC",
2827 OutputFile: android.OptionalPathForPath(module.outputFilePath),
2828 ExtraEntries: []android.AndroidMkExtraEntriesFunc{
Colin Crossaa255532020-07-03 13:18:24 -07002829 func(ctx android.AndroidMkExtraEntriesContext, entries *android.AndroidMkEntries) {
Jiyong Parke3833882020-02-17 17:28:10 +09002830 entries.SetString("LOCAL_MODULE_TAGS", "optional")
Colin Crossc68db4b2021-11-11 18:59:15 -08002831 entries.SetString("LOCAL_MODULE_PATH", module.installDirPath.String())
Jiyong Parke3833882020-02-17 17:28:10 +09002832 entries.SetString("LOCAL_INSTALLED_MODULE_STEM", module.outputFilePath.Base())
2833 },
2834 },
2835 }}
2836}
Paul Duffindd46f712020-02-10 13:37:10 +00002837
Pedro Loureiroc3621422021-09-28 15:40:23 +00002838func (module *sdkLibraryXml) selfValidate(ctx android.ModuleContext) {
2839 module.validateAtLeastTAttributes(ctx)
2840 module.validateMinAndMaxDeviceSdk(ctx)
2841 module.validateMinMaxDeviceSdkAndModuleMinSdk(ctx)
2842 module.validateOnBootclasspathBeforeRequirements(ctx)
2843}
2844
2845func (module *sdkLibraryXml) validateAtLeastTAttributes(ctx android.ModuleContext) {
2846 t := android.ApiLevelOrPanic(ctx, "Tiramisu")
2847 module.attrAtLeastT(ctx, t, module.properties.Min_device_sdk, "min_device_sdk")
2848 module.attrAtLeastT(ctx, t, module.properties.Max_device_sdk, "max_device_sdk")
2849 module.attrAtLeastT(ctx, t, module.properties.On_bootclasspath_before, "on_bootclasspath_before")
2850 module.attrAtLeastT(ctx, t, module.properties.On_bootclasspath_since, "on_bootclasspath_since")
2851}
2852
2853func (module *sdkLibraryXml) attrAtLeastT(ctx android.ModuleContext, t android.ApiLevel, attr *string, attrName string) {
2854 if attr != nil {
2855 if level, err := android.ApiLevelFromUser(ctx, *attr); err == nil {
2856 // we will inform the user of invalid inputs when we try to write the
2857 // permissions xml file so we don't need to do it here
2858 if t.GreaterThan(level) {
2859 ctx.PropertyErrorf(attrName, "Attribute value needs to be at least T")
2860 }
2861 }
2862 }
2863}
2864
2865func (module *sdkLibraryXml) validateMinAndMaxDeviceSdk(ctx android.ModuleContext) {
2866 if module.properties.Min_device_sdk != nil && module.properties.Max_device_sdk != nil {
2867 min, minErr := android.ApiLevelFromUser(ctx, *module.properties.Min_device_sdk)
2868 max, maxErr := android.ApiLevelFromUser(ctx, *module.properties.Max_device_sdk)
2869 if minErr == nil && maxErr == nil {
2870 // we will inform the user of invalid inputs when we try to write the
2871 // permissions xml file so we don't need to do it here
2872 if min.GreaterThan(max) {
2873 ctx.ModuleErrorf("min_device_sdk can't be greater than max_device_sdk")
2874 }
2875 }
2876 }
2877}
2878
2879func (module *sdkLibraryXml) validateMinMaxDeviceSdkAndModuleMinSdk(ctx android.ModuleContext) {
2880 moduleMinApi := android.ApiLevelOrPanic(ctx, *module.properties.Sdk_library_min_api_level)
2881 if module.properties.Min_device_sdk != nil {
2882 api, err := android.ApiLevelFromUser(ctx, *module.properties.Min_device_sdk)
2883 if err == nil {
2884 if moduleMinApi.GreaterThan(api) {
2885 ctx.PropertyErrorf("min_device_sdk", "Can't be less than module's min sdk (%s)", moduleMinApi)
2886 }
2887 }
2888 }
2889 if module.properties.Max_device_sdk != nil {
2890 api, err := android.ApiLevelFromUser(ctx, *module.properties.Max_device_sdk)
2891 if err == nil {
2892 if moduleMinApi.GreaterThan(api) {
2893 ctx.PropertyErrorf("max_device_sdk", "Can't be less than module's min sdk (%s)", moduleMinApi)
2894 }
2895 }
2896 }
2897}
2898
2899func (module *sdkLibraryXml) validateOnBootclasspathBeforeRequirements(ctx android.ModuleContext) {
2900 moduleMinApi := android.ApiLevelOrPanic(ctx, *module.properties.Sdk_library_min_api_level)
2901 if module.properties.On_bootclasspath_before != nil {
2902 t := android.ApiLevelOrPanic(ctx, "Tiramisu")
2903 // if we use the attribute, then we need to do this validation
2904 if moduleMinApi.LessThan(t) {
2905 // if minAPi is < T, then we need to have min_device_sdk (which only accepts T+)
2906 if module.properties.Min_device_sdk == nil {
2907 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")
2908 }
2909 }
2910 }
2911}
2912
Paul Duffindd46f712020-02-10 13:37:10 +00002913type sdkLibrarySdkMemberType struct {
2914 android.SdkMemberTypeBase
2915}
2916
Paul Duffin296701e2021-07-14 10:29:36 +01002917func (s *sdkLibrarySdkMemberType) AddDependencies(ctx android.SdkDependencyContext, dependencyTag blueprint.DependencyTag, names []string) {
2918 ctx.AddVariationDependencies(nil, dependencyTag, names...)
Paul Duffindd46f712020-02-10 13:37:10 +00002919}
2920
2921func (s *sdkLibrarySdkMemberType) IsInstance(module android.Module) bool {
2922 _, ok := module.(*SdkLibrary)
2923 return ok
2924}
2925
2926func (s *sdkLibrarySdkMemberType) AddPrebuiltModule(ctx android.SdkMemberContext, member android.SdkMember) android.BpModule {
2927 return ctx.SnapshotBuilder().AddPrebuiltModule(member, "java_sdk_library_import")
2928}
2929
2930func (s *sdkLibrarySdkMemberType) CreateVariantPropertiesStruct() android.SdkMemberProperties {
2931 return &sdkLibrarySdkMemberProperties{}
2932}
2933
Paul Duffin976b0e52021-04-27 23:20:26 +01002934var javaSdkLibrarySdkMemberType = &sdkLibrarySdkMemberType{
2935 android.SdkMemberTypeBase{
2936 PropertyName: "java_sdk_libs",
2937 SupportsSdk: true,
2938 },
2939}
2940
Paul Duffindd46f712020-02-10 13:37:10 +00002941type sdkLibrarySdkMemberProperties struct {
2942 android.SdkMemberPropertiesBase
2943
Paul Duffine8409952022-09-22 16:24:46 +01002944 // Stem name for files in the sdk snapshot.
2945 //
2946 // This is used to construct the path names of various sdk library files in the sdk snapshot to
2947 // make sure that they match the finalized versions of those files in prebuilts/sdk.
2948 //
2949 // This property is marked as keep so that it will be kept in all instances of this struct, will
2950 // not be cleared but will be copied to common structs. That is needed because this field is used
2951 // to construct many file names for other parts of this struct and so it needs to be present in
2952 // all structs. If it was not marked as keep then it would be cleared in some structs and so would
2953 // be unavailable for generating file names if there were other properties that were still set.
2954 Stem string `sdk:"keep"`
2955
Paul Duffindd46f712020-02-10 13:37:10 +00002956 // Scope to per scope properties.
Paul Duffin106a3a42022-01-27 16:39:06 +00002957 Scopes map[*apiScope]*scopeProperties
Paul Duffindd46f712020-02-10 13:37:10 +00002958
Paul Duffin3d1248c2020-04-09 00:10:17 +01002959 // The Java stubs source files.
2960 Stub_srcs []string
Paul Duffinf7a64332020-05-13 16:54:55 +01002961
2962 // The naming scheme.
2963 Naming_scheme *string
Paul Duffind7eb1c22020-05-26 20:57:10 +01002964
2965 // True if the java_sdk_library_import is for a shared library, false
2966 // otherwise.
2967 Shared_library *bool
Paul Duffina2ae7e02020-09-11 11:55:00 +01002968
Paul Duffin1267d872021-04-16 17:21:36 +01002969 // True if the stub imports should produce dex jars.
2970 Compile_dex *bool
2971
Paul Duffina2ae7e02020-09-11 11:55:00 +01002972 // The paths to the doctag files to add to the prebuilt.
2973 Doctag_paths android.Paths
Paul Duffin869de142021-07-15 14:14:41 +01002974
2975 Permitted_packages []string
Pedro Loureiro9956e5e2021-09-07 17:21:59 +00002976
2977 // Signals that this shared library is part of the bootclasspath starting
2978 // on the version indicated in this attribute.
2979 //
2980 // This will make platforms at this level and above to ignore
2981 // <uses-library> tags with this library name because the library is already
2982 // available
2983 On_bootclasspath_since *string
2984
2985 // Signals that this shared library was part of the bootclasspath before
2986 // (but not including) the version indicated in this attribute.
2987 //
2988 // The system will automatically add a <uses-library> tag with this library to
2989 // apps that target any SDK less than the version indicated in this attribute.
2990 On_bootclasspath_before *string
2991
2992 // Indicates that PackageManager should ignore this shared library if the
2993 // platform is below the version indicated in this attribute.
2994 //
2995 // This means that the device won't recognise this library as installed.
2996 Min_device_sdk *string
2997
2998 // Indicates that PackageManager should ignore this shared library if the
2999 // platform is above the version indicated in this attribute.
3000 //
3001 // This means that the device won't recognise this library as installed.
3002 Max_device_sdk *string
Jiakai Zhang9c4dc192023-02-09 00:09:24 +08003003
3004 DexPreoptProfileGuided *bool `supported_build_releases:"UpsideDownCake+"`
Paul Duffindd46f712020-02-10 13:37:10 +00003005}
3006
3007type scopeProperties struct {
Paul Duffin1fd005d2020-04-09 01:08:11 +01003008 Jars android.Paths
3009 StubsSrcJar android.Path
3010 CurrentApiFile android.Path
3011 RemovedApiFile android.Path
Paul Duffine7babdb2022-02-10 13:06:54 +00003012 AnnotationsZip android.Path `supported_build_releases:"Tiramisu+"`
Paul Duffin1fd005d2020-04-09 01:08:11 +01003013 SdkVersion string
Paul Duffindd46f712020-02-10 13:37:10 +00003014}
3015
3016func (s *sdkLibrarySdkMemberProperties) PopulateFromVariant(ctx android.SdkMemberContext, variant android.Module) {
3017 sdk := variant.(*SdkLibrary)
3018
Paul Duffine8409952022-09-22 16:24:46 +01003019 // Copy the stem name for files in the sdk snapshot.
3020 s.Stem = sdk.distStem()
3021
Paul Duffin106a3a42022-01-27 16:39:06 +00003022 s.Scopes = make(map[*apiScope]*scopeProperties)
Paul Duffindd46f712020-02-10 13:37:10 +00003023 for _, apiScope := range allApiScopes {
Paul Duffin803a9562020-05-20 11:52:25 +01003024 paths := sdk.findScopePaths(apiScope)
3025 if paths == nil {
3026 continue
3027 }
3028
Paul Duffindd46f712020-02-10 13:37:10 +00003029 jars := paths.stubsImplPath
3030 if len(jars) > 0 {
3031 properties := scopeProperties{}
3032 properties.Jars = jars
Paul Duffin780c5f42020-05-12 15:52:55 +01003033 properties.SdkVersion = sdk.sdkVersionForStubsLibrary(ctx.SdkModuleContext(), apiScope)
Paul Duffin0f8faff2020-05-20 16:18:00 +01003034 properties.StubsSrcJar = paths.stubsSrcJar.Path()
Paul Duffin10269f12020-06-19 18:39:55 +01003035 if paths.currentApiFilePath.Valid() {
3036 properties.CurrentApiFile = paths.currentApiFilePath.Path()
3037 }
3038 if paths.removedApiFilePath.Valid() {
3039 properties.RemovedApiFile = paths.removedApiFilePath.Path()
3040 }
Anton Hanssond78eb762021-09-21 15:25:12 +01003041 // The annotations zip is only available for modules that set annotations_enabled: true.
3042 if paths.annotationsZip.Valid() {
3043 properties.AnnotationsZip = paths.annotationsZip.Path()
3044 }
Paul Duffin106a3a42022-01-27 16:39:06 +00003045 s.Scopes[apiScope] = &properties
Paul Duffindd46f712020-02-10 13:37:10 +00003046 }
3047 }
3048
Paul Duffindfa131e2020-05-15 20:37:11 +01003049 s.Naming_scheme = sdk.commonSdkLibraryProperties.Naming_scheme
Paul Duffind7eb1c22020-05-26 20:57:10 +01003050 s.Shared_library = proptools.BoolPtr(sdk.sharedLibrary())
Paul Duffin1267d872021-04-16 17:21:36 +01003051 s.Compile_dex = sdk.dexProperties.Compile_dex
Paul Duffina2ae7e02020-09-11 11:55:00 +01003052 s.Doctag_paths = sdk.doctagPaths
Paul Duffin869de142021-07-15 14:14:41 +01003053 s.Permitted_packages = sdk.PermittedPackagesForUpdatableBootJars()
Pedro Loureiro9956e5e2021-09-07 17:21:59 +00003054 s.On_bootclasspath_since = sdk.commonSdkLibraryProperties.On_bootclasspath_since
3055 s.On_bootclasspath_before = sdk.commonSdkLibraryProperties.On_bootclasspath_before
3056 s.Min_device_sdk = sdk.commonSdkLibraryProperties.Min_device_sdk
3057 s.Max_device_sdk = sdk.commonSdkLibraryProperties.Max_device_sdk
Jiakai Zhang9c4dc192023-02-09 00:09:24 +08003058
3059 if sdk.dexpreopter.dexpreoptProperties.Dex_preopt_result.Profile_guided {
3060 s.DexPreoptProfileGuided = proptools.BoolPtr(true)
3061 }
Paul Duffindd46f712020-02-10 13:37:10 +00003062}
3063
3064func (s *sdkLibrarySdkMemberProperties) AddToPropertySet(ctx android.SdkMemberContext, propertySet android.BpPropertySet) {
Paul Duffinf7a64332020-05-13 16:54:55 +01003065 if s.Naming_scheme != nil {
3066 propertySet.AddProperty("naming_scheme", proptools.String(s.Naming_scheme))
3067 }
Paul Duffind7eb1c22020-05-26 20:57:10 +01003068 if s.Shared_library != nil {
3069 propertySet.AddProperty("shared_library", *s.Shared_library)
3070 }
Paul Duffin1267d872021-04-16 17:21:36 +01003071 if s.Compile_dex != nil {
3072 propertySet.AddProperty("compile_dex", *s.Compile_dex)
3073 }
Paul Duffin869de142021-07-15 14:14:41 +01003074 if len(s.Permitted_packages) > 0 {
3075 propertySet.AddProperty("permitted_packages", s.Permitted_packages)
3076 }
Jiakai Zhang9c4dc192023-02-09 00:09:24 +08003077 dexPreoptSet := propertySet.AddPropertySet("dex_preopt")
3078 if s.DexPreoptProfileGuided != nil {
3079 dexPreoptSet.AddProperty("profile_guided", proptools.Bool(s.DexPreoptProfileGuided))
3080 }
Paul Duffinf7a64332020-05-13 16:54:55 +01003081
Paul Duffine8409952022-09-22 16:24:46 +01003082 stem := s.Stem
3083
Paul Duffindd46f712020-02-10 13:37:10 +00003084 for _, apiScope := range allApiScopes {
3085 if properties, ok := s.Scopes[apiScope]; ok {
Paul Duffin6b836ba2020-05-13 19:19:49 +01003086 scopeSet := propertySet.AddPropertySet(apiScope.propertyName)
Paul Duffindd46f712020-02-10 13:37:10 +00003087
Paul Duffin958806b2022-05-16 13:10:47 +00003088 scopeDir := apiScope.snapshotRelativeDir()
Paul Duffin3d1248c2020-04-09 00:10:17 +01003089
Paul Duffindd46f712020-02-10 13:37:10 +00003090 var jars []string
3091 for _, p := range properties.Jars {
Paul Duffine8409952022-09-22 16:24:46 +01003092 dest := filepath.Join(scopeDir, stem+"-stubs.jar")
Paul Duffindd46f712020-02-10 13:37:10 +00003093 ctx.SnapshotBuilder().CopyToSnapshot(p, dest)
3094 jars = append(jars, dest)
3095 }
3096 scopeSet.AddProperty("jars", jars)
3097
Paul Duffin22628d52021-05-12 23:13:22 +01003098 if ctx.SdkModuleContext().Config().IsEnvTrue("SOONG_SDK_SNAPSHOT_USE_SRCJAR") {
3099 // Copy the stubs source jar into the snapshot zip as is.
Paul Duffine8409952022-09-22 16:24:46 +01003100 srcJarSnapshotPath := filepath.Join(scopeDir, stem+".srcjar")
Paul Duffin22628d52021-05-12 23:13:22 +01003101 ctx.SnapshotBuilder().CopyToSnapshot(properties.StubsSrcJar, srcJarSnapshotPath)
3102 scopeSet.AddProperty("stub_srcs", []string{srcJarSnapshotPath})
3103 } else {
3104 // Merge the stubs source jar into the snapshot zip so that when it is unpacked
3105 // the source files are also unpacked.
Paul Duffine8409952022-09-22 16:24:46 +01003106 snapshotRelativeDir := filepath.Join(scopeDir, stem+"_stub_sources")
Paul Duffin22628d52021-05-12 23:13:22 +01003107 ctx.SnapshotBuilder().UnzipToSnapshot(properties.StubsSrcJar, snapshotRelativeDir)
3108 scopeSet.AddProperty("stub_srcs", []string{snapshotRelativeDir})
3109 }
Paul Duffin3d1248c2020-04-09 00:10:17 +01003110
Paul Duffin1fd005d2020-04-09 01:08:11 +01003111 if properties.CurrentApiFile != nil {
Paul Duffine8409952022-09-22 16:24:46 +01003112 currentApiSnapshotPath := apiScope.snapshotRelativeCurrentApiTxtPath(stem)
Paul Duffin1fd005d2020-04-09 01:08:11 +01003113 ctx.SnapshotBuilder().CopyToSnapshot(properties.CurrentApiFile, currentApiSnapshotPath)
3114 scopeSet.AddProperty("current_api", currentApiSnapshotPath)
3115 }
3116
3117 if properties.RemovedApiFile != nil {
Paul Duffine8409952022-09-22 16:24:46 +01003118 removedApiSnapshotPath := apiScope.snapshotRelativeRemovedApiTxtPath(stem)
Paul Duffin3dbf9fd2020-06-02 13:00:02 +01003119 ctx.SnapshotBuilder().CopyToSnapshot(properties.RemovedApiFile, removedApiSnapshotPath)
Paul Duffin1fd005d2020-04-09 01:08:11 +01003120 scopeSet.AddProperty("removed_api", removedApiSnapshotPath)
3121 }
3122
Anton Hanssond78eb762021-09-21 15:25:12 +01003123 if properties.AnnotationsZip != nil {
Paul Duffine8409952022-09-22 16:24:46 +01003124 annotationsSnapshotPath := filepath.Join(scopeDir, stem+"_annotations.zip")
Anton Hanssond78eb762021-09-21 15:25:12 +01003125 ctx.SnapshotBuilder().CopyToSnapshot(properties.AnnotationsZip, annotationsSnapshotPath)
3126 scopeSet.AddProperty("annotations", annotationsSnapshotPath)
3127 }
3128
Paul Duffindd46f712020-02-10 13:37:10 +00003129 if properties.SdkVersion != "" {
3130 scopeSet.AddProperty("sdk_version", properties.SdkVersion)
3131 }
3132 }
3133 }
3134
Paul Duffina2ae7e02020-09-11 11:55:00 +01003135 if len(s.Doctag_paths) > 0 {
3136 dests := []string{}
3137 for _, p := range s.Doctag_paths {
3138 dest := filepath.Join("doctags", p.Rel())
3139 ctx.SnapshotBuilder().CopyToSnapshot(p, dest)
3140 dests = append(dests, dest)
3141 }
3142 propertySet.AddProperty("doctag_files", dests)
3143 }
Paul Duffindd46f712020-02-10 13:37:10 +00003144}