blob: 98f29990d8bfaad6e15fb10f58875da835c0fa4a [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"
Pedro Loureiro9956e5e2021-09-07 17:21:59 +000024 "strconv"
Jiyong Parkc678ad32018-04-10 13:07:10 +090025 "strings"
Jiyong Park82484c02018-04-23 21:41:26 +090026 "sync"
Jiyong Parkc678ad32018-04-10 13:07:10 +090027
Paul Duffind1b3a922020-01-22 11:57:20 +000028 "github.com/google/blueprint"
Jiyong Parkc678ad32018-04-10 13:07:10 +090029 "github.com/google/blueprint/proptools"
Paul Duffin46a26a82020-04-07 19:27:04 +010030
31 "android/soong/android"
Ulya Trafimovichdbf31662020-12-17 12:07:54 +000032 "android/soong/dexpreopt"
Jiyong Parkc678ad32018-04-10 13:07:10 +090033)
34
Jooyung Han58f26ab2019-12-18 15:34:32 +090035const (
Pedro Loureiro9956e5e2021-09-07 17:21:59 +000036 sdkXmlFileSuffix = ".xml"
Jiyong Parkc678ad32018-04-10 13:07:10 +090037)
38
Paul Duffind1b3a922020-01-22 11:57:20 +000039// A tag to associated a dependency with a specific api scope.
40type scopeDependencyTag struct {
41 blueprint.BaseDependencyTag
42 name string
43 apiScope *apiScope
Paul Duffinc8782502020-04-29 20:45:27 +010044
45 // Function for extracting appropriate path information from the dependency.
Colin Crossdcf71b22021-02-01 13:59:03 -080046 depInfoExtractor func(paths *scopePaths, ctx android.ModuleContext, dep android.Module) error
Paul Duffinc8782502020-04-29 20:45:27 +010047}
48
49// Extract tag specific information from the dependency.
50func (tag scopeDependencyTag) extractDepInfo(ctx android.ModuleContext, dep android.Module, paths *scopePaths) {
Colin Crossdcf71b22021-02-01 13:59:03 -080051 err := tag.depInfoExtractor(paths, ctx, dep)
Paul Duffinc8782502020-04-29 20:45:27 +010052 if err != nil {
53 ctx.ModuleErrorf("has an invalid {scopeDependencyTag: %s} dependency on module %s: %s", tag.name, ctx.OtherModuleName(dep), err.Error())
54 }
Paul Duffind1b3a922020-01-22 11:57:20 +000055}
56
Paul Duffin80342d72020-06-26 22:08:43 +010057var _ android.ReplaceSourceWithPrebuilt = (*scopeDependencyTag)(nil)
58
59func (tag scopeDependencyTag) ReplaceSourceWithPrebuilt() bool {
60 return false
61}
62
Paul Duffind1b3a922020-01-22 11:57:20 +000063// Provides information about an api scope, e.g. public, system, test.
64type apiScope struct {
65 // The name of the api scope, e.g. public, system, test
66 name string
67
Paul Duffin97b53b82020-05-05 14:40:52 +010068 // The api scope that this scope extends.
69 extends *apiScope
70
Paul Duffin3375e352020-04-28 10:44:03 +010071 // The legacy enabled status for a specific scope can be dependent on other
72 // properties that have been specified on the library so it is provided by
73 // a function that can determine the status by examining those properties.
74 legacyEnabledStatus func(module *SdkLibrary) bool
75
76 // The default enabled status for non-legacy behavior, which is triggered by
77 // explicitly enabling at least one api scope.
78 defaultEnabledStatus bool
79
80 // Gets a pointer to the scope specific properties.
81 scopeSpecificProperties func(module *SdkLibrary) *ApiScopeProperties
82
Paul Duffin46a26a82020-04-07 19:27:04 +010083 // The name of the field in the dynamically created structure.
84 fieldName string
85
Paul Duffin6b836ba2020-05-13 19:19:49 +010086 // The name of the property in the java_sdk_library_import
87 propertyName string
88
Paul Duffind1b3a922020-01-22 11:57:20 +000089 // The tag to use to depend on the stubs library module.
90 stubsTag scopeDependencyTag
91
Paul Duffin0ff08bd2020-04-29 13:30:54 +010092 // The tag to use to depend on the stubs source module (if separate from the API module).
93 stubsSourceTag scopeDependencyTag
94
95 // The tag to use to depend on the API file generating module (if separate from the stubs source module).
96 apiFileTag scopeDependencyTag
97
Paul Duffinc8782502020-04-29 20:45:27 +010098 // The tag to use to depend on the stubs source and API module.
99 stubsSourceAndApiTag scopeDependencyTag
Paul Duffind1b3a922020-01-22 11:57:20 +0000100
101 // The scope specific prefix to add to the api file base of "current.txt" or "removed.txt".
102 apiFilePrefix string
103
104 // The scope specific prefix to add to the sdk library module name to construct a scope specific
105 // module name.
106 moduleSuffix string
107
Paul Duffind1b3a922020-01-22 11:57:20 +0000108 // SDK version that the stubs library is built against. Note that this is always
109 // *current. Older stubs library built with a numbered SDK version is created from
110 // the prebuilt jar.
111 sdkVersion string
Paul Duffin1fb487d2020-04-07 18:50:10 +0100112
Paul Duffin15f34ef2020-07-20 18:04:44 +0100113 // The annotation that identifies this API level, empty for the public API scope.
114 annotation string
115
Paul Duffin1fb487d2020-04-07 18:50:10 +0100116 // Extra arguments to pass to droidstubs for this scope.
Paul Duffin0ff08bd2020-04-29 13:30:54 +0100117 //
Paul Duffin15f34ef2020-07-20 18:04:44 +0100118 // This is not used directly but is used to construct the droidstubsArgs.
119 extraArgs []string
Paul Duffin0ff08bd2020-04-29 13:30:54 +0100120
Paul Duffin15f34ef2020-07-20 18:04:44 +0100121 // The args that must be passed to droidstubs to generate the API and stubs source
122 // for this scope, constructed dynamically by initApiScope().
Paul Duffin0ff08bd2020-04-29 13:30:54 +0100123 //
124 // The API only includes the additional members that this scope adds over the scope
125 // that it extends.
Paul Duffin15f34ef2020-07-20 18:04:44 +0100126 //
127 // The stubs source must include the definitions of everything that is in this
128 // api scope and all the scopes that this one extends.
129 droidstubsArgs []string
Paul Duffin0ff08bd2020-04-29 13:30:54 +0100130
Anton Hansson6478ac12020-05-02 11:19:36 +0100131 // Whether the api scope can be treated as unstable, and should skip compat checks.
132 unstable bool
Paul Duffind1b3a922020-01-22 11:57:20 +0000133}
134
135// Initialize a scope, creating and adding appropriate dependency tags
136func initApiScope(scope *apiScope) *apiScope {
Paul Duffinc8782502020-04-29 20:45:27 +0100137 name := scope.name
Paul Duffin46dc45a2020-05-14 15:39:10 +0100138 scopeByName[name] = scope
139 allScopeNames = append(allScopeNames, name)
Paul Duffin6b836ba2020-05-13 19:19:49 +0100140 scope.propertyName = strings.ReplaceAll(name, "-", "_")
141 scope.fieldName = proptools.FieldNameForProperty(scope.propertyName)
Paul Duffind1b3a922020-01-22 11:57:20 +0000142 scope.stubsTag = scopeDependencyTag{
Paul Duffinc8782502020-04-29 20:45:27 +0100143 name: name + "-stubs",
144 apiScope: scope,
145 depInfoExtractor: (*scopePaths).extractStubsLibraryInfoFromDependency,
Paul Duffind1b3a922020-01-22 11:57:20 +0000146 }
Paul Duffin0ff08bd2020-04-29 13:30:54 +0100147 scope.stubsSourceTag = scopeDependencyTag{
148 name: name + "-stubs-source",
149 apiScope: scope,
150 depInfoExtractor: (*scopePaths).extractStubsSourceInfoFromDep,
151 }
152 scope.apiFileTag = scopeDependencyTag{
153 name: name + "-api",
154 apiScope: scope,
155 depInfoExtractor: (*scopePaths).extractApiInfoFromDep,
156 }
Paul Duffinc8782502020-04-29 20:45:27 +0100157 scope.stubsSourceAndApiTag = scopeDependencyTag{
158 name: name + "-stubs-source-and-api",
159 apiScope: scope,
160 depInfoExtractor: (*scopePaths).extractStubsSourceAndApiInfoFromApiStubsProvider,
Paul Duffind1b3a922020-01-22 11:57:20 +0000161 }
Paul Duffin0ff08bd2020-04-29 13:30:54 +0100162
163 // To get the args needed to generate the stubs source append all the args from
164 // this scope and all the scopes it extends as each set of args adds additional
165 // members to the stubs.
Paul Duffin15f34ef2020-07-20 18:04:44 +0100166 var scopeSpecificArgs []string
167 if scope.annotation != "" {
168 scopeSpecificArgs = []string{"--show-annotation", scope.annotation}
Paul Duffin0ff08bd2020-04-29 13:30:54 +0100169 }
Paul Duffin15f34ef2020-07-20 18:04:44 +0100170 for s := scope; s != nil; s = s.extends {
171 scopeSpecificArgs = append(scopeSpecificArgs, s.extraArgs...)
Paul Duffin0ff08bd2020-04-29 13:30:54 +0100172
Paul Duffin15f34ef2020-07-20 18:04:44 +0100173 // Ensure that the generated stubs includes all the API elements from the API scope
174 // that this scope extends.
175 if s != scope && s.annotation != "" {
176 scopeSpecificArgs = append(scopeSpecificArgs, "--show-for-stub-purposes-annotation", s.annotation)
177 }
178 }
Paul Duffin0ff08bd2020-04-29 13:30:54 +0100179
Paul Duffin15f34ef2020-07-20 18:04:44 +0100180 // Escape any special characters in the arguments. This is needed because droidstubs
181 // passes these directly to the shell command.
182 scope.droidstubsArgs = proptools.ShellEscapeList(scopeSpecificArgs)
Paul Duffin0ff08bd2020-04-29 13:30:54 +0100183
Paul Duffind1b3a922020-01-22 11:57:20 +0000184 return scope
185}
186
Anton Hansson08f476b2021-04-07 15:32:19 +0100187func (scope *apiScope) stubsLibraryModuleNameSuffix() string {
188 return ".stubs" + scope.moduleSuffix
189}
190
Paul Duffinc3091c82020-05-08 14:16:20 +0100191func (scope *apiScope) stubsLibraryModuleName(baseName string) string {
Anton Hansson08f476b2021-04-07 15:32:19 +0100192 return baseName + scope.stubsLibraryModuleNameSuffix()
Paul Duffind1b3a922020-01-22 11:57:20 +0000193}
194
Paul Duffinc8782502020-04-29 20:45:27 +0100195func (scope *apiScope) stubsSourceModuleName(baseName string) string {
Paul Duffindd9d0742020-05-08 15:52:37 +0100196 return baseName + ".stubs.source" + scope.moduleSuffix
Paul Duffind1b3a922020-01-22 11:57:20 +0000197}
198
Paul Duffin0ff08bd2020-04-29 13:30:54 +0100199func (scope *apiScope) apiModuleName(baseName string) string {
Paul Duffindd9d0742020-05-08 15:52:37 +0100200 return baseName + ".api" + scope.moduleSuffix
Paul Duffin0ff08bd2020-04-29 13:30:54 +0100201}
202
Paul Duffin3375e352020-04-28 10:44:03 +0100203func (scope *apiScope) String() string {
204 return scope.name
205}
206
Paul Duffind1b3a922020-01-22 11:57:20 +0000207type apiScopes []*apiScope
208
209func (scopes apiScopes) Strings(accessor func(*apiScope) string) []string {
210 var list []string
211 for _, scope := range scopes {
212 list = append(list, accessor(scope))
213 }
214 return list
215}
216
Jiyong Parkc678ad32018-04-10 13:07:10 +0900217var (
Paul Duffin46dc45a2020-05-14 15:39:10 +0100218 scopeByName = make(map[string]*apiScope)
219 allScopeNames []string
Paul Duffind1b3a922020-01-22 11:57:20 +0000220 apiScopePublic = initApiScope(&apiScope{
Paul Duffin3375e352020-04-28 10:44:03 +0100221 name: "public",
222
223 // Public scope is enabled by default for both legacy and non-legacy modes.
224 legacyEnabledStatus: func(module *SdkLibrary) bool {
225 return true
226 },
227 defaultEnabledStatus: true,
228
229 scopeSpecificProperties: func(module *SdkLibrary) *ApiScopeProperties {
230 return &module.sdkLibraryProperties.Public
231 },
Paul Duffind1b3a922020-01-22 11:57:20 +0000232 sdkVersion: "current",
233 })
234 apiScopeSystem = initApiScope(&apiScope{
Paul Duffin3375e352020-04-28 10:44:03 +0100235 name: "system",
236 extends: apiScopePublic,
237 legacyEnabledStatus: (*SdkLibrary).generateTestAndSystemScopesByDefault,
238 scopeSpecificProperties: func(module *SdkLibrary) *ApiScopeProperties {
239 return &module.sdkLibraryProperties.System
240 },
Paul Duffin15f34ef2020-07-20 18:04:44 +0100241 apiFilePrefix: "system-",
242 moduleSuffix: ".system",
243 sdkVersion: "system_current",
244 annotation: "android.annotation.SystemApi(client=android.annotation.SystemApi.Client.PRIVILEGED_APPS)",
Paul Duffind1b3a922020-01-22 11:57:20 +0000245 })
246 apiScopeTest = initApiScope(&apiScope{
Paul Duffin3375e352020-04-28 10:44:03 +0100247 name: "test",
Anton Hansson4fe970f2020-10-09 10:16:49 +0100248 extends: apiScopeSystem,
Paul Duffin3375e352020-04-28 10:44:03 +0100249 legacyEnabledStatus: (*SdkLibrary).generateTestAndSystemScopesByDefault,
250 scopeSpecificProperties: func(module *SdkLibrary) *ApiScopeProperties {
251 return &module.sdkLibraryProperties.Test
252 },
Paul Duffin15f34ef2020-07-20 18:04:44 +0100253 apiFilePrefix: "test-",
254 moduleSuffix: ".test",
255 sdkVersion: "test_current",
256 annotation: "android.annotation.TestApi",
257 unstable: true,
Paul Duffind1b3a922020-01-22 11:57:20 +0000258 })
Paul Duffin8f265b92020-04-28 14:13:56 +0100259 apiScopeModuleLib = initApiScope(&apiScope{
Paul Duffin6b836ba2020-05-13 19:19:49 +0100260 name: "module-lib",
Paul Duffin8f265b92020-04-28 14:13:56 +0100261 extends: apiScopeSystem,
Paul Duffin0c5bae52020-06-02 13:00:08 +0100262 // The module-lib scope is disabled by default in legacy mode.
Paul Duffin8f265b92020-04-28 14:13:56 +0100263 //
264 // Enabling this would break existing usages.
265 legacyEnabledStatus: func(module *SdkLibrary) bool {
266 return false
267 },
268 scopeSpecificProperties: func(module *SdkLibrary) *ApiScopeProperties {
269 return &module.sdkLibraryProperties.Module_lib
270 },
271 apiFilePrefix: "module-lib-",
272 moduleSuffix: ".module_lib",
273 sdkVersion: "module_current",
Paul Duffin15f34ef2020-07-20 18:04:44 +0100274 annotation: "android.annotation.SystemApi(client=android.annotation.SystemApi.Client.MODULE_LIBRARIES)",
Paul Duffin8f265b92020-04-28 14:13:56 +0100275 })
Paul Duffin0c5bae52020-06-02 13:00:08 +0100276 apiScopeSystemServer = initApiScope(&apiScope{
277 name: "system-server",
278 extends: apiScopePublic,
279 // The system-server scope is disabled by default in legacy mode.
280 //
281 // Enabling this would break existing usages.
282 legacyEnabledStatus: func(module *SdkLibrary) bool {
283 return false
284 },
285 scopeSpecificProperties: func(module *SdkLibrary) *ApiScopeProperties {
286 return &module.sdkLibraryProperties.System_server
287 },
288 apiFilePrefix: "system-server-",
289 moduleSuffix: ".system_server",
290 sdkVersion: "system_server_current",
Paul Duffin15f34ef2020-07-20 18:04:44 +0100291 annotation: "android.annotation.SystemApi(client=android.annotation.SystemApi.Client.SYSTEM_SERVER)",
292 extraArgs: []string{
293 "--hide-annotation", "android.annotation.Hide",
Paul Duffin0c5bae52020-06-02 13:00:08 +0100294 // com.android.* classes are okay in this interface"
Paul Duffin15f34ef2020-07-20 18:04:44 +0100295 "--hide", "InternalClasses",
Paul Duffin0c5bae52020-06-02 13:00:08 +0100296 },
297 })
Paul Duffind1b3a922020-01-22 11:57:20 +0000298 allApiScopes = apiScopes{
299 apiScopePublic,
300 apiScopeSystem,
301 apiScopeTest,
Paul Duffin8f265b92020-04-28 14:13:56 +0100302 apiScopeModuleLib,
Paul Duffin0c5bae52020-06-02 13:00:08 +0100303 apiScopeSystemServer,
Paul Duffind1b3a922020-01-22 11:57:20 +0000304 }
Jiyong Parkc678ad32018-04-10 13:07:10 +0900305)
306
Jiyong Park82484c02018-04-23 21:41:26 +0900307var (
308 javaSdkLibrariesLock sync.Mutex
309)
310
Jiyong Parkc678ad32018-04-10 13:07:10 +0900311// TODO: these are big features that are currently missing
Jiyong Park1be96912018-05-28 18:02:19 +0900312// 1) disallowing linking to the runtime shared lib
313// 2) HTML generation
Jiyong Parkc678ad32018-04-10 13:07:10 +0900314
315func init() {
Paul Duffin43dc1cc2019-12-19 11:18:54 +0000316 RegisterSdkLibraryBuildComponents(android.InitRegistrationContext)
Jiyong Parkc678ad32018-04-10 13:07:10 +0900317
Jiyong Park82484c02018-04-23 21:41:26 +0900318 android.RegisterMakeVarsProvider(pctx, func(ctx android.MakeVarsContext) {
319 javaSdkLibraries := javaSdkLibraries(ctx.Config())
320 sort.Strings(*javaSdkLibraries)
321 ctx.Strict("JAVA_SDK_LIBRARIES", strings.Join(*javaSdkLibraries, " "))
322 })
Paul Duffindd46f712020-02-10 13:37:10 +0000323
324 // Register sdk member types.
Paul Duffin976b0e52021-04-27 23:20:26 +0100325 android.RegisterSdkMemberType(javaSdkLibrarySdkMemberType)
Jiyong Parkc678ad32018-04-10 13:07:10 +0900326}
327
Paul Duffin43dc1cc2019-12-19 11:18:54 +0000328func RegisterSdkLibraryBuildComponents(ctx android.RegistrationContext) {
329 ctx.RegisterModuleType("java_sdk_library", SdkLibraryFactory)
330 ctx.RegisterModuleType("java_sdk_library_import", sdkLibraryImportFactory)
331}
332
Paul Duffin3375e352020-04-28 10:44:03 +0100333// Properties associated with each api scope.
334type ApiScopeProperties struct {
335 // Indicates whether the api surface is generated.
336 //
337 // If this is set for any scope then all scopes must explicitly specify if they
338 // are enabled. This is to prevent new usages from depending on legacy behavior.
339 //
340 // Otherwise, if this is not set for any scope then the default behavior is
341 // scope specific so please refer to the scope specific property documentation.
342 Enabled *bool
Paul Duffin87a05a32020-05-12 11:50:28 +0100343
344 // The sdk_version to use for building the stubs.
345 //
346 // If not specified then it will use an sdk_version determined as follows:
Trevor Radcliffedf8aa1f2021-11-04 14:25:39 +0000347 //
Paul Duffin87a05a32020-05-12 11:50:28 +0100348 // 1) If the sdk_version specified on the java_sdk_library is none then this
Trevor Radcliffedf8aa1f2021-11-04 14:25:39 +0000349 // will be none. This is used for java_sdk_library instances that are used
350 // to create stubs that contribute to the core_current sdk version.
351 // 2) Otherwise, it is assumed that this library extends but does not
352 // contribute directly to a specific sdk_version and so this uses the
353 // sdk_version appropriate for the api scope. e.g. public will use
354 // sdk_version: current, system will use sdk_version: system_current, etc.
Paul Duffin87a05a32020-05-12 11:50:28 +0100355 //
356 // This does not affect the sdk_version used for either generating the stubs source
357 // or the API file. They both have to use the same sdk_version as is used for
358 // compiling the implementation library.
359 Sdk_version *string
Paul Duffin3375e352020-04-28 10:44:03 +0100360}
361
Jiyong Parkc678ad32018-04-10 13:07:10 +0900362type sdkLibraryProperties struct {
Anton Hanssonf8ea3722021-09-16 14:24:13 +0100363 // List of source files that are needed to compile the API, but are not part of runtime library.
364 Api_srcs []string `android:"arch_variant"`
365
Paul Duffin5df79302020-05-16 15:52:12 +0100366 // Visibility for impl library module. If not specified then defaults to the
367 // visibility property.
368 Impl_library_visibility []string
369
Paul Duffin4911a892020-04-29 23:35:13 +0100370 // Visibility for stubs library modules. If not specified then defaults to the
371 // visibility property.
372 Stubs_library_visibility []string
373
374 // Visibility for stubs source modules. If not specified then defaults to the
375 // visibility property.
376 Stubs_source_visibility []string
377
Anton Hansson7f66efa2020-10-08 14:47:23 +0100378 // List of Java libraries that will be in the classpath when building the implementation lib
379 Impl_only_libs []string `android:"arch_variant"`
380
Sundong Ahnf043cf62018-06-25 16:04:37 +0900381 // List of Java libraries that will be in the classpath when building stubs
382 Stub_only_libs []string `android:"arch_variant"`
383
Anton Hanssondae54cd2021-04-21 16:30:10 +0100384 // List of Java libraries that will included in stub libraries
385 Stub_only_static_libs []string `android:"arch_variant"`
386
Paul Duffin7a586d32019-12-30 17:09:34 +0000387 // list of package names that will be documented and publicized as API.
388 // This allows the API to be restricted to a subset of the source files provided.
389 // If this is unspecified then all the source files will be treated as being part
390 // of the API.
Jiyong Parkc678ad32018-04-10 13:07:10 +0900391 Api_packages []string
392
Jiyong Park5a2c9d72018-05-01 22:25:41 +0900393 // list of package names that must be hidden from the API
394 Hidden_api_packages []string
395
Paul Duffin749f98f2019-12-30 17:23:46 +0000396 // the relative path to the directory containing the api specification files.
397 // Defaults to "api".
398 Api_dir *string
399
Paul Duffindfa131e2020-05-15 20:37:11 +0100400 // Determines whether a runtime implementation library is built; defaults to false.
401 //
402 // If true then it also prevents the module from being used as a shared module, i.e.
403 // it is as is shared_library: false, was set.
Paul Duffin43db9be2019-12-30 17:35:49 +0000404 Api_only *bool
405
Paul Duffin11512472019-02-11 15:55:17 +0000406 // local files that are used within user customized droiddoc options.
407 Droiddoc_option_files []string
408
Spandan Das93e95992021-07-29 18:26:39 +0000409 // additional droiddoc options.
Paul Duffin11512472019-02-11 15:55:17 +0000410 // Available variables for substitution:
411 //
412 // $(location <label>): the path to the droiddoc_option_files with name <label>
Sundong Ahndd567f92018-07-31 17:19:11 +0900413 Droiddoc_options []string
414
Paul Duffine22c2ab2020-05-20 19:35:27 +0100415 // is set to true, Metalava will allow framework SDK to contain annotations.
416 Annotations_enabled *bool
417
Sundong Ahn054b19a2018-10-19 13:46:09 +0900418 // a list of top-level directories containing files to merge qualifier annotations
419 // (i.e. those intended to be included in the stubs written) from.
420 Merge_annotations_dirs []string
421
422 // a list of top-level directories containing Java stub files to merge show/hide annotations from.
423 Merge_inclusion_annotations_dirs []string
424
Paul Duffin4f5c1ef2020-11-19 14:53:43 +0000425 // If set to true then don't create dist rules.
426 No_dist *bool
Sundong Ahn80a87b32019-05-13 15:02:50 +0900427
Paul Duffin31310252020-11-20 21:26:20 +0000428 // The stem for the artifacts that are copied to the dist, if not specified
429 // then defaults to the base module name.
430 //
431 // For each scope the following artifacts are copied to the apistubs/<scope>
432 // directory in the dist.
433 // * stubs impl jar -> <dist-stem>.jar
434 // * API specification file -> api/<dist-stem>.txt
435 // * Removed API specification file -> api/<dist-stem>-removed.txt
436 //
437 // Also used to construct the name of the filegroup (created by prebuilt_apis)
438 // that references the latest released API and remove API specification files.
439 // * API specification filegroup -> <dist-stem>.api.<scope>.latest
440 // * Removed API specification filegroup -> <dist-stem>-removed.api.<scope>.latest
Jaewoong Jung1a97ee02021-03-09 13:25:02 -0800441 // * API incompatibilities baseline filegroup -> <dist-stem>-incompatibilities.api.<scope>.latest
Paul Duffin31310252020-11-20 21:26:20 +0000442 Dist_stem *string
443
Colin Cross986b69a2021-06-01 13:13:40 -0700444 // The subdirectory for the artifacts that are copied to the dist directory. If not specified
Colin Cross3dd66252021-06-01 14:05:09 -0700445 // then defaults to "unknown". Should be set to "android" for anything that should be published
Colin Cross986b69a2021-06-01 13:13:40 -0700446 // in the public Android SDK.
447 Dist_group *string
448
Anton Hanssondff2c782020-12-21 17:10:01 +0000449 // A compatibility mode that allows historical API-tracking files to not exist.
450 // Do not use.
451 Unsafe_ignore_missing_latest_api bool
452
Paul Duffin3375e352020-04-28 10:44:03 +0100453 // indicates whether system and test apis should be generated.
454 Generate_system_and_test_apis bool `blueprint:"mutated"`
455
456 // The properties specific to the public api scope
457 //
458 // Unless explicitly specified by using public.enabled the public api scope is
459 // enabled by default in both legacy and non-legacy mode.
460 Public ApiScopeProperties
461
462 // The properties specific to the system api scope
463 //
464 // In legacy mode the system api scope is enabled by default when sdk_version
465 // is set to something other than "none".
466 //
467 // In non-legacy mode the system api scope is disabled by default.
468 System ApiScopeProperties
469
470 // The properties specific to the test api scope
471 //
472 // In legacy mode the test api scope is enabled by default when sdk_version
473 // is set to something other than "none".
474 //
475 // In non-legacy mode the test api scope is disabled by default.
476 Test ApiScopeProperties
Paul Duffin37e0b772019-12-30 17:20:10 +0000477
Paul Duffin0c5bae52020-06-02 13:00:08 +0100478 // The properties specific to the module-lib api scope
Paul Duffin8f265b92020-04-28 14:13:56 +0100479 //
Paul Duffin0c5bae52020-06-02 13:00:08 +0100480 // Unless explicitly specified by using test.enabled the module-lib api scope is
Paul Duffin8f265b92020-04-28 14:13:56 +0100481 // disabled by default.
482 Module_lib ApiScopeProperties
483
Paul Duffin0c5bae52020-06-02 13:00:08 +0100484 // The properties specific to the system-server api scope
485 //
486 // Unless explicitly specified by using test.enabled the module-lib api scope is
487 // disabled by default.
488 System_server ApiScopeProperties
489
Jiyong Park932cdfe2020-05-28 00:19:53 +0900490 // Determines if the stubs are preferred over the implementation library
491 // for linking, even when the client doesn't specify sdk_version. When this
492 // is set to true, such clients are provided with the widest API surface that
493 // this lib provides. Note however that this option doesn't affect the clients
494 // that are in the same APEX as this library. In that case, the clients are
495 // always linked with the implementation library. Default is false.
496 Default_to_stubs *bool
497
Paul Duffin160fe412020-05-10 19:32:20 +0100498 // Properties related to api linting.
499 Api_lint struct {
500 // Enable api linting.
501 Enabled *bool
502 }
503
Jiyong Parkc678ad32018-04-10 13:07:10 +0900504 // TODO: determines whether to create HTML doc or not
505 //Html_doc *bool
506}
507
Paul Duffin0f8faff2020-05-20 16:18:00 +0100508// Paths to outputs from java_sdk_library and java_sdk_library_import.
509//
510// Fields that are android.Paths are always set (during GenerateAndroidBuildActions).
511// OptionalPaths are always set by java_sdk_library but may not be set by
512// java_sdk_library_import as not all instances provide that information.
Paul Duffind1b3a922020-01-22 11:57:20 +0000513type scopePaths struct {
Paul Duffin0f8faff2020-05-20 16:18:00 +0100514 // The path (represented as Paths for convenience when returning) to the stubs header jar.
515 //
516 // That is the jar that is created by turbine.
517 stubsHeaderPath android.Paths
518
519 // The path (represented as Paths for convenience when returning) to the stubs implementation jar.
520 //
521 // This is not the implementation jar, it still only contains stubs.
522 stubsImplPath android.Paths
523
Paul Duffin1267d872021-04-16 17:21:36 +0100524 // The dex jar for the stubs.
525 //
526 // This is not the implementation jar, it still only contains stubs.
Martin Stjernholm8be1e6d2021-09-15 03:34:04 +0100527 stubsDexJarPath OptionalDexJarPath
Paul Duffin1267d872021-04-16 17:21:36 +0100528
Paul Duffin0f8faff2020-05-20 16:18:00 +0100529 // The API specification file, e.g. system_current.txt.
530 currentApiFilePath android.OptionalPath
531
532 // The specification of API elements removed since the last release.
533 removedApiFilePath android.OptionalPath
534
535 // The stubs source jar.
536 stubsSrcJar android.OptionalPath
Anton Hanssond78eb762021-09-21 15:25:12 +0100537
538 // Extracted annotations.
539 annotationsZip android.OptionalPath
Paul Duffind1b3a922020-01-22 11:57:20 +0000540}
541
Colin Crossdcf71b22021-02-01 13:59:03 -0800542func (paths *scopePaths) extractStubsLibraryInfoFromDependency(ctx android.ModuleContext, dep android.Module) error {
543 if ctx.OtherModuleHasProvider(dep, JavaInfoProvider) {
544 lib := ctx.OtherModuleProvider(dep, JavaInfoProvider).(JavaInfo)
545 paths.stubsHeaderPath = lib.HeaderJars
546 paths.stubsImplPath = lib.ImplementationJars
Paul Duffin1267d872021-04-16 17:21:36 +0100547
548 libDep := dep.(UsesLibraryDependency)
549 paths.stubsDexJarPath = libDep.DexJarBuildPath()
Paul Duffinc8782502020-04-29 20:45:27 +0100550 return nil
551 } else {
Colin Crossdcf71b22021-02-01 13:59:03 -0800552 return fmt.Errorf("expected module that has JavaInfoProvider, e.g. java_library")
Paul Duffinc8782502020-04-29 20:45:27 +0100553 }
554}
555
Paul Duffin0ff08bd2020-04-29 13:30:54 +0100556func (paths *scopePaths) treatDepAsApiStubsProvider(dep android.Module, action func(provider ApiStubsProvider)) error {
557 if apiStubsProvider, ok := dep.(ApiStubsProvider); ok {
558 action(apiStubsProvider)
Paul Duffinc8782502020-04-29 20:45:27 +0100559 return nil
560 } else {
561 return fmt.Errorf("expected module that implements ApiStubsProvider, e.g. droidstubs")
562 }
563}
564
Paul Duffin0f8faff2020-05-20 16:18:00 +0100565func (paths *scopePaths) treatDepAsApiStubsSrcProvider(dep android.Module, action func(provider ApiStubsSrcProvider)) error {
566 if apiStubsProvider, ok := dep.(ApiStubsSrcProvider); ok {
567 action(apiStubsProvider)
568 return nil
569 } else {
570 return fmt.Errorf("expected module that implements ApiStubsSrcProvider, e.g. droidstubs")
571 }
572}
573
Paul Duffin0ff08bd2020-04-29 13:30:54 +0100574func (paths *scopePaths) extractApiInfoFromApiStubsProvider(provider ApiStubsProvider) {
Anton Hanssond78eb762021-09-21 15:25:12 +0100575 paths.annotationsZip = android.OptionalPathForPath(provider.AnnotationsZip())
Paul Duffin0f8faff2020-05-20 16:18:00 +0100576 paths.currentApiFilePath = android.OptionalPathForPath(provider.ApiFilePath())
577 paths.removedApiFilePath = android.OptionalPathForPath(provider.RemovedApiFilePath())
Paul Duffin0ff08bd2020-04-29 13:30:54 +0100578}
579
Colin Crossdcf71b22021-02-01 13:59:03 -0800580func (paths *scopePaths) extractApiInfoFromDep(ctx android.ModuleContext, dep android.Module) error {
Paul Duffin0ff08bd2020-04-29 13:30:54 +0100581 return paths.treatDepAsApiStubsProvider(dep, func(provider ApiStubsProvider) {
582 paths.extractApiInfoFromApiStubsProvider(provider)
583 })
584}
585
Paul Duffin0f8faff2020-05-20 16:18:00 +0100586func (paths *scopePaths) extractStubsSourceInfoFromApiStubsProviders(provider ApiStubsSrcProvider) {
587 paths.stubsSrcJar = android.OptionalPathForPath(provider.StubsSrcJar())
Paul Duffin0ff08bd2020-04-29 13:30:54 +0100588}
589
Colin Crossdcf71b22021-02-01 13:59:03 -0800590func (paths *scopePaths) extractStubsSourceInfoFromDep(ctx android.ModuleContext, dep android.Module) error {
Paul Duffin0f8faff2020-05-20 16:18:00 +0100591 return paths.treatDepAsApiStubsSrcProvider(dep, func(provider ApiStubsSrcProvider) {
Paul Duffin0ff08bd2020-04-29 13:30:54 +0100592 paths.extractStubsSourceInfoFromApiStubsProviders(provider)
593 })
594}
595
Colin Crossdcf71b22021-02-01 13:59:03 -0800596func (paths *scopePaths) extractStubsSourceAndApiInfoFromApiStubsProvider(ctx android.ModuleContext, dep android.Module) error {
Paul Duffin0ff08bd2020-04-29 13:30:54 +0100597 return paths.treatDepAsApiStubsProvider(dep, func(provider ApiStubsProvider) {
598 paths.extractApiInfoFromApiStubsProvider(provider)
599 paths.extractStubsSourceInfoFromApiStubsProviders(provider)
600 })
601}
602
603type commonToSdkLibraryAndImportProperties struct {
Paul Duffin1b1e8062020-05-08 13:44:43 +0100604 // The naming scheme to use for the components that this module creates.
605 //
Paul Duffinee9ad5d2020-09-11 13:04:05 +0100606 // If not specified then it defaults to "default".
Paul Duffin1b1e8062020-05-08 13:44:43 +0100607 //
608 // This is a temporary mechanism to simplify conversion from separate modules for each
609 // component that follow a different naming pattern to the default one.
610 //
611 // TODO(b/155480189) - Remove once naming inconsistencies have been resolved.
Paul Duffin0ff08bd2020-04-29 13:30:54 +0100612 Naming_scheme *string
Paul Duffindfa131e2020-05-15 20:37:11 +0100613
614 // Specifies whether this module can be used as an Android shared library; defaults
615 // to true.
616 //
617 // An Android shared library is one that can be referenced in a <uses-library> element
618 // in an AndroidManifest.xml.
619 Shared_library *bool
Paul Duffina2ae7e02020-09-11 11:55:00 +0100620
621 // Files containing information about supported java doc tags.
622 Doctag_files []string `android:"path"`
Pedro Loureiro9956e5e2021-09-07 17:21:59 +0000623
624 // Signals that this shared library is part of the bootclasspath starting
625 // on the version indicated in this attribute.
626 //
627 // This will make platforms at this level and above to ignore
628 // <uses-library> tags with this library name because the library is already
629 // available
630 On_bootclasspath_since *string
631
632 // Signals that this shared library was part of the bootclasspath before
633 // (but not including) the version indicated in this attribute.
634 //
635 // The system will automatically add a <uses-library> tag with this library to
636 // apps that target any SDK less than the version indicated in this attribute.
637 On_bootclasspath_before *string
638
639 // Indicates that PackageManager should ignore this shared library if the
640 // platform is below the version indicated in this attribute.
641 //
642 // This means that the device won't recognise this library as installed.
643 Min_device_sdk *string
644
645 // Indicates that PackageManager should ignore this shared library if the
646 // platform is above the version indicated in this attribute.
647 //
648 // This means that the device won't recognise this library as installed.
649 Max_device_sdk *string
Paul Duffin0ff08bd2020-04-29 13:30:54 +0100650}
651
Paul Duffin71b33cc2021-06-23 11:39:47 +0100652// commonSdkLibraryAndImportModule defines the interface that must be provided by a module that
653// embeds the commonToSdkLibraryAndImport struct.
654type commonSdkLibraryAndImportModule interface {
Paul Duffinb97b1572021-04-29 21:50:40 +0100655 android.SdkAware
Paul Duffin71b33cc2021-06-23 11:39:47 +0100656
657 BaseModuleName() string
658}
659
Paul Duffin56d44902020-01-31 13:36:25 +0000660// Common code between sdk library and sdk library import
661type commonToSdkLibraryAndImport struct {
Paul Duffin71b33cc2021-06-23 11:39:47 +0100662 module commonSdkLibraryAndImportModule
Paul Duffinc3091c82020-05-08 14:16:20 +0100663
Paul Duffin56d44902020-01-31 13:36:25 +0000664 scopePaths map[*apiScope]*scopePaths
Paul Duffin1b1e8062020-05-08 13:44:43 +0100665
666 namingScheme sdkLibraryComponentNamingScheme
667
Paul Duffindfa131e2020-05-15 20:37:11 +0100668 commonSdkLibraryProperties commonToSdkLibraryAndImportProperties
Paul Duffin859fe962020-05-15 10:20:31 +0100669
Paul Duffina2ae7e02020-09-11 11:55:00 +0100670 // Paths to commonSdkLibraryProperties.Doctag_files
671 doctagPaths android.Paths
672
Paul Duffin859fe962020-05-15 10:20:31 +0100673 // Functionality related to this being used as a component of a java_sdk_library.
674 EmbeddableSdkLibraryComponent
Paul Duffin56d44902020-01-31 13:36:25 +0000675}
676
Paul Duffin71b33cc2021-06-23 11:39:47 +0100677func (c *commonToSdkLibraryAndImport) initCommon(module commonSdkLibraryAndImportModule) {
678 c.module = module
Paul Duffin1b1e8062020-05-08 13:44:43 +0100679
Paul Duffin71b33cc2021-06-23 11:39:47 +0100680 module.AddProperties(&c.commonSdkLibraryProperties)
Paul Duffin859fe962020-05-15 10:20:31 +0100681
682 // Initialize this as an sdk library component.
Paul Duffin71b33cc2021-06-23 11:39:47 +0100683 c.initSdkLibraryComponent(module)
Paul Duffin1b1e8062020-05-08 13:44:43 +0100684}
685
686func (c *commonToSdkLibraryAndImport) initCommonAfterDefaultsApplied(ctx android.DefaultableHookContext) bool {
Paul Duffindfa131e2020-05-15 20:37:11 +0100687 schemeProperty := proptools.StringDefault(c.commonSdkLibraryProperties.Naming_scheme, "default")
Paul Duffin1b1e8062020-05-08 13:44:43 +0100688 switch schemeProperty {
689 case "default":
690 c.namingScheme = &defaultNamingScheme{}
691 default:
692 ctx.PropertyErrorf("naming_scheme", "expected 'default' but was %q", schemeProperty)
693 return false
694 }
695
Paul Duffin3f0290e2021-06-30 18:25:36 +0100696 namePtr := proptools.StringPtr(c.module.BaseModuleName())
697 c.sdkLibraryComponentProperties.SdkLibraryName = namePtr
698
Paul Duffindfa131e2020-05-15 20:37:11 +0100699 // Only track this sdk library if this can be used as a shared library.
700 if c.sharedLibrary() {
701 // Use the name specified in the module definition as the owner.
Paul Duffin3f0290e2021-06-30 18:25:36 +0100702 c.sdkLibraryComponentProperties.SdkLibraryToImplicitlyTrack = namePtr
Paul Duffindfa131e2020-05-15 20:37:11 +0100703 }
Paul Duffin859fe962020-05-15 10:20:31 +0100704
Paul Duffin1b1e8062020-05-08 13:44:43 +0100705 return true
Paul Duffinc3091c82020-05-08 14:16:20 +0100706}
707
Paul Duffinea8f8082021-06-24 13:25:57 +0100708// uniqueApexVariations provides common implementation of the ApexModule.UniqueApexVariations
709// method.
710func (c *commonToSdkLibraryAndImport) uniqueApexVariations() bool {
711 // A java_sdk_library that is a shared library produces an XML file that makes the shared library
712 // usable from an AndroidManifest.xml's <uses-library> entry. That XML file contains the name of
713 // the APEX and so it needs a unique variation per APEX.
714 return c.sharedLibrary()
715}
716
Paul Duffina2ae7e02020-09-11 11:55:00 +0100717func (c *commonToSdkLibraryAndImport) generateCommonBuildActions(ctx android.ModuleContext) {
718 c.doctagPaths = android.PathsForModuleSrc(ctx, c.commonSdkLibraryProperties.Doctag_files)
719}
720
Paul Duffineedc5d52020-06-12 17:46:39 +0100721// Module name of the runtime implementation library
722func (c *commonToSdkLibraryAndImport) implLibraryModuleName() string {
Paul Duffin71b33cc2021-06-23 11:39:47 +0100723 return c.module.BaseModuleName() + ".impl"
Paul Duffineedc5d52020-06-12 17:46:39 +0100724}
725
726// Module name of the XML file for the lib
727func (c *commonToSdkLibraryAndImport) xmlPermissionsModuleName() string {
Paul Duffin71b33cc2021-06-23 11:39:47 +0100728 return c.module.BaseModuleName() + sdkXmlFileSuffix
Paul Duffineedc5d52020-06-12 17:46:39 +0100729}
730
Paul Duffinc3091c82020-05-08 14:16:20 +0100731// Name of the java_library module that compiles the stubs source.
732func (c *commonToSdkLibraryAndImport) stubsLibraryModuleName(apiScope *apiScope) string {
Paul Duffinb97b1572021-04-29 21:50:40 +0100733 baseName := c.module.BaseModuleName()
734 return c.module.SdkMemberComponentName(baseName, func(name string) string {
735 return c.namingScheme.stubsLibraryModuleName(apiScope, name)
736 })
Paul Duffinc3091c82020-05-08 14:16:20 +0100737}
738
739// Name of the droidstubs module that generates the stubs source and may also
740// generate/check the API.
741func (c *commonToSdkLibraryAndImport) stubsSourceModuleName(apiScope *apiScope) string {
Paul Duffinb97b1572021-04-29 21:50:40 +0100742 baseName := c.module.BaseModuleName()
743 return c.module.SdkMemberComponentName(baseName, func(name string) string {
744 return c.namingScheme.stubsSourceModuleName(apiScope, name)
745 })
Paul Duffinc3091c82020-05-08 14:16:20 +0100746}
747
Paul Duffin46dc45a2020-05-14 15:39:10 +0100748// The component names for different outputs of the java_sdk_library.
749//
750// They are similar to the names used for the child modules it creates
751const (
752 stubsSourceComponentName = "stubs.source"
753
754 apiTxtComponentName = "api.txt"
755
756 removedApiTxtComponentName = "removed-api.txt"
Anton Hanssond78eb762021-09-21 15:25:12 +0100757
758 annotationsComponentName = "annotations.zip"
Paul Duffin46dc45a2020-05-14 15:39:10 +0100759)
760
761// A regular expression to match tags that reference a specific stubs component.
762//
763// It will only match if given a valid scope and a valid component. It is verfy strict
764// to ensure it does not accidentally match a similar looking tag that should be processed
765// by the embedded Library.
766var tagSplitter = func() *regexp.Regexp {
767 // Given a list of literal string items returns a regular expression that will
768 // match any one of the items.
769 choice := func(items ...string) string {
770 return `\Q` + strings.Join(items, `\E|\Q`) + `\E`
771 }
772
773 // Regular expression to match one of the scopes.
774 scopesRegexp := choice(allScopeNames...)
775
776 // Regular expression to match one of the components.
Anton Hanssond78eb762021-09-21 15:25:12 +0100777 componentsRegexp := choice(stubsSourceComponentName, apiTxtComponentName, removedApiTxtComponentName, annotationsComponentName)
Paul Duffin46dc45a2020-05-14 15:39:10 +0100778
779 // Regular expression to match any combination of one scope and one component.
780 return regexp.MustCompile(fmt.Sprintf(`^\.(%s)\.(%s)$`, scopesRegexp, componentsRegexp))
781}()
782
783// For OutputFileProducer interface
784//
Anton Hanssond78eb762021-09-21 15:25:12 +0100785// .<scope>.<component name>, for all ComponentNames (for example: .public.removed-api.txt)
Paul Duffin46dc45a2020-05-14 15:39:10 +0100786func (c *commonToSdkLibraryAndImport) commonOutputFiles(tag string) (android.Paths, error) {
787 if groups := tagSplitter.FindStringSubmatch(tag); groups != nil {
788 scopeName := groups[1]
789 component := groups[2]
790
791 if scope, ok := scopeByName[scopeName]; ok {
792 paths := c.findScopePaths(scope)
793 if paths == nil {
Paul Duffin71b33cc2021-06-23 11:39:47 +0100794 return nil, fmt.Errorf("%q does not provide api scope %s", c.module.BaseModuleName(), scopeName)
Paul Duffin46dc45a2020-05-14 15:39:10 +0100795 }
796
797 switch component {
798 case stubsSourceComponentName:
799 if paths.stubsSrcJar.Valid() {
800 return android.Paths{paths.stubsSrcJar.Path()}, nil
801 }
802
803 case apiTxtComponentName:
804 if paths.currentApiFilePath.Valid() {
805 return android.Paths{paths.currentApiFilePath.Path()}, nil
806 }
807
808 case removedApiTxtComponentName:
809 if paths.removedApiFilePath.Valid() {
810 return android.Paths{paths.removedApiFilePath.Path()}, nil
811 }
Anton Hanssond78eb762021-09-21 15:25:12 +0100812
813 case annotationsComponentName:
814 if paths.annotationsZip.Valid() {
815 return android.Paths{paths.annotationsZip.Path()}, nil
816 }
Paul Duffin46dc45a2020-05-14 15:39:10 +0100817 }
818
819 return nil, fmt.Errorf("%s not available for api scope %s", component, scopeName)
820 } else {
821 return nil, fmt.Errorf("unknown scope %s in %s", scope, tag)
822 }
823
824 } else {
Paul Duffina2ae7e02020-09-11 11:55:00 +0100825 switch tag {
826 case ".doctags":
827 if c.doctagPaths != nil {
828 return c.doctagPaths, nil
829 } else {
Paul Duffin71b33cc2021-06-23 11:39:47 +0100830 return nil, fmt.Errorf("no doctag_files specified on %s", c.module.BaseModuleName())
Paul Duffina2ae7e02020-09-11 11:55:00 +0100831 }
832 }
Paul Duffin46dc45a2020-05-14 15:39:10 +0100833 return nil, nil
834 }
835}
836
Paul Duffin803a9562020-05-20 11:52:25 +0100837func (c *commonToSdkLibraryAndImport) getScopePathsCreateIfNeeded(scope *apiScope) *scopePaths {
Paul Duffin56d44902020-01-31 13:36:25 +0000838 if c.scopePaths == nil {
839 c.scopePaths = make(map[*apiScope]*scopePaths)
840 }
841 paths := c.scopePaths[scope]
842 if paths == nil {
843 paths = &scopePaths{}
844 c.scopePaths[scope] = paths
845 }
846
847 return paths
848}
849
Paul Duffin803a9562020-05-20 11:52:25 +0100850func (c *commonToSdkLibraryAndImport) findScopePaths(scope *apiScope) *scopePaths {
851 if c.scopePaths == nil {
852 return nil
853 }
854
855 return c.scopePaths[scope]
856}
857
858// If this does not support the requested api scope then find the closest available
859// scope it does support. Returns nil if no such scope is available.
860func (c *commonToSdkLibraryAndImport) findClosestScopePath(scope *apiScope) *scopePaths {
861 for s := scope; s != nil; s = s.extends {
862 if paths := c.findScopePaths(s); paths != nil {
863 return paths
864 }
865 }
866
867 // This should never happen outside tests as public should be the base scope for every
868 // scope and is enabled by default.
869 return nil
870}
871
Jiyong Parkf1691d22021-03-29 20:11:58 +0900872func (c *commonToSdkLibraryAndImport) selectHeaderJarsForSdkVersion(ctx android.BaseModuleContext, sdkVersion android.SdkSpec) android.Paths {
Paul Duffinb05d4292020-05-20 12:19:10 +0100873
874 // If a specific numeric version has been requested then use prebuilt versions of the sdk.
Jiyong Park54105c42021-03-31 18:17:53 +0900875 if !sdkVersion.ApiLevel.IsPreview() {
Paul Duffin71b33cc2021-06-23 11:39:47 +0100876 return PrebuiltJars(ctx, c.module.BaseModuleName(), sdkVersion)
Paul Duffinb05d4292020-05-20 12:19:10 +0100877 }
878
Paul Duffin1267d872021-04-16 17:21:36 +0100879 paths := c.selectScopePaths(ctx, sdkVersion.Kind)
880 if paths == nil {
881 return nil
882 }
883
884 return paths.stubsHeaderPath
885}
886
887// selectScopePaths returns the *scopePaths appropriate for the specific kind.
888//
889// If the module does not support the specific kind then it will return the *scopePaths for the
890// closest kind which is a subset of the requested kind. e.g. if requesting android.SdkModule then
891// it will return *scopePaths for android.SdkSystem if available or android.SdkPublic of not.
892func (c *commonToSdkLibraryAndImport) selectScopePaths(ctx android.BaseModuleContext, kind android.SdkKind) *scopePaths {
Paul Duffin32cf58a2021-05-18 16:32:50 +0100893 apiScope := sdkKindToApiScope(kind)
Paul Duffinb05d4292020-05-20 12:19:10 +0100894
Paul Duffin803a9562020-05-20 11:52:25 +0100895 paths := c.findClosestScopePath(apiScope)
896 if paths == nil {
897 var scopes []string
898 for _, s := range allApiScopes {
899 if c.findScopePaths(s) != nil {
900 scopes = append(scopes, s.name)
901 }
902 }
Paul Duffin71b33cc2021-06-23 11:39:47 +0100903 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 +0100904 return nil
905 }
906
Paul Duffin1267d872021-04-16 17:21:36 +0100907 return paths
908}
909
Paul Duffin32cf58a2021-05-18 16:32:50 +0100910// sdkKindToApiScope maps from android.SdkKind to apiScope.
911func sdkKindToApiScope(kind android.SdkKind) *apiScope {
912 var apiScope *apiScope
913 switch kind {
914 case android.SdkSystem:
915 apiScope = apiScopeSystem
916 case android.SdkModule:
917 apiScope = apiScopeModuleLib
918 case android.SdkTest:
919 apiScope = apiScopeTest
920 case android.SdkSystemServer:
921 apiScope = apiScopeSystemServer
922 default:
923 apiScope = apiScopePublic
924 }
925 return apiScope
926}
927
Paul Duffin1267d872021-04-16 17:21:36 +0100928// to satisfy SdkLibraryDependency interface
Martin Stjernholm8be1e6d2021-09-15 03:34:04 +0100929func (c *commonToSdkLibraryAndImport) SdkApiStubDexJar(ctx android.BaseModuleContext, kind android.SdkKind) OptionalDexJarPath {
Paul Duffin1267d872021-04-16 17:21:36 +0100930 paths := c.selectScopePaths(ctx, kind)
931 if paths == nil {
Martin Stjernholm8be1e6d2021-09-15 03:34:04 +0100932 return makeUnsetDexJarPath()
Paul Duffin1267d872021-04-16 17:21:36 +0100933 }
934
935 return paths.stubsDexJarPath
Paul Duffinb05d4292020-05-20 12:19:10 +0100936}
937
Paul Duffin32cf58a2021-05-18 16:32:50 +0100938// to satisfy SdkLibraryDependency interface
939func (c *commonToSdkLibraryAndImport) SdkRemovedTxtFile(ctx android.BaseModuleContext, kind android.SdkKind) android.OptionalPath {
940 apiScope := sdkKindToApiScope(kind)
941 paths := c.findScopePaths(apiScope)
942 if paths == nil {
943 return android.OptionalPath{}
944 }
945
946 return paths.removedApiFilePath
947}
948
Paul Duffin859fe962020-05-15 10:20:31 +0100949func (c *commonToSdkLibraryAndImport) sdkComponentPropertiesForChildLibrary() interface{} {
950 componentProps := &struct {
Paul Duffin3f0290e2021-06-30 18:25:36 +0100951 SdkLibraryName *string
Paul Duffin859fe962020-05-15 10:20:31 +0100952 SdkLibraryToImplicitlyTrack *string
Paul Duffindfa131e2020-05-15 20:37:11 +0100953 }{}
954
Paul Duffin3f0290e2021-06-30 18:25:36 +0100955 namePtr := proptools.StringPtr(c.module.BaseModuleName())
956 componentProps.SdkLibraryName = namePtr
957
Paul Duffindfa131e2020-05-15 20:37:11 +0100958 if c.sharedLibrary() {
Paul Duffin859fe962020-05-15 10:20:31 +0100959 // Mark the stubs library as being components of this java_sdk_library so that
960 // any app that includes code which depends (directly or indirectly) on the stubs
961 // library will have the appropriate <uses-library> invocation inserted into its
962 // manifest if necessary.
Paul Duffin3f0290e2021-06-30 18:25:36 +0100963 componentProps.SdkLibraryToImplicitlyTrack = namePtr
Paul Duffin859fe962020-05-15 10:20:31 +0100964 }
965
966 return componentProps
967}
968
Paul Duffindfa131e2020-05-15 20:37:11 +0100969func (c *commonToSdkLibraryAndImport) sharedLibrary() bool {
970 return proptools.BoolDefault(c.commonSdkLibraryProperties.Shared_library, true)
971}
972
Paul Duffinf4600f62021-05-13 22:34:45 +0100973// Check if the stub libraries should be compiled for dex
974func (c *commonToSdkLibraryAndImport) stubLibrariesCompiledForDex() bool {
975 // Always compile the dex file files for the stub libraries if they will be used on the
976 // bootclasspath.
977 return !c.sharedLibrary()
978}
979
Paul Duffin859fe962020-05-15 10:20:31 +0100980// Properties related to the use of a module as an component of a java_sdk_library.
981type SdkLibraryComponentProperties struct {
Paul Duffin3f0290e2021-06-30 18:25:36 +0100982 // The name of the java_sdk_library/_import module.
983 SdkLibraryName *string `blueprint:"mutated"`
Paul Duffin859fe962020-05-15 10:20:31 +0100984
985 // The name of the java_sdk_library/_import to add to a <uses-library> entry
986 // in the AndroidManifest.xml of any Android app that includes code that references
987 // this module. If not set then no java_sdk_library/_import is tracked.
988 SdkLibraryToImplicitlyTrack *string `blueprint:"mutated"`
989}
990
991// Structure to be embedded in a module struct that needs to support the
992// SdkLibraryComponentDependency interface.
993type EmbeddableSdkLibraryComponent struct {
994 sdkLibraryComponentProperties SdkLibraryComponentProperties
995}
996
Paul Duffin71b33cc2021-06-23 11:39:47 +0100997func (e *EmbeddableSdkLibraryComponent) initSdkLibraryComponent(module android.Module) {
998 module.AddProperties(&e.sdkLibraryComponentProperties)
Paul Duffin859fe962020-05-15 10:20:31 +0100999}
1000
1001// to satisfy SdkLibraryComponentDependency
Paul Duffin3f0290e2021-06-30 18:25:36 +01001002func (e *EmbeddableSdkLibraryComponent) SdkLibraryName() *string {
1003 return e.sdkLibraryComponentProperties.SdkLibraryName
1004}
1005
1006// to satisfy SdkLibraryComponentDependency
Ulya Trafimovich39b437b2020-09-23 16:42:35 +01001007func (e *EmbeddableSdkLibraryComponent) OptionalSdkLibraryImplementation() *string {
Ulya Trafimovich78645fb2021-07-16 15:29:25 +01001008 // For shared libraries, this is the same as the SDK library name. If a Java library or app
1009 // depends on a component library (e.g. a stub library) it still needs to know the name of the
1010 // run-time library and the corresponding module that provides the implementation. This name is
1011 // passed to manifest_fixer (to be added to AndroidManifest.xml) and added to CLC (to be used
1012 // in dexpreopt).
1013 //
1014 // For non-shared SDK (component or not) libraries this returns `nil`, as they are not
1015 // <uses-library> and should not be added to the manifest or to CLC.
Ulya Trafimovich39b437b2020-09-23 16:42:35 +01001016 return e.sdkLibraryComponentProperties.SdkLibraryToImplicitlyTrack
1017}
1018
Paul Duffin859fe962020-05-15 10:20:31 +01001019// Implemented by modules that are (or possibly could be) a component of a java_sdk_library
1020// (including the java_sdk_library) itself.
1021type SdkLibraryComponentDependency interface {
Ulya Trafimovich31e444e2020-08-14 17:32:16 +01001022 UsesLibraryDependency
1023
Paul Duffin3f0290e2021-06-30 18:25:36 +01001024 // SdkLibraryName returns the name of the java_sdk_library/_import module.
1025 SdkLibraryName() *string
1026
Ulya Trafimovich39b437b2020-09-23 16:42:35 +01001027 // The name of the implementation library for the optional SDK library or nil, if there isn't one.
1028 OptionalSdkLibraryImplementation() *string
Paul Duffin859fe962020-05-15 10:20:31 +01001029}
1030
1031// Make sure that all the module types that are components of java_sdk_library/_import
1032// and which can be referenced (directly or indirectly) from an android app implement
1033// the SdkLibraryComponentDependency interface.
1034var _ SdkLibraryComponentDependency = (*Library)(nil)
1035var _ SdkLibraryComponentDependency = (*Import)(nil)
1036var _ SdkLibraryComponentDependency = (*SdkLibrary)(nil)
Paul Duffineedc5d52020-06-12 17:46:39 +01001037var _ SdkLibraryComponentDependency = (*SdkLibraryImport)(nil)
Paul Duffin859fe962020-05-15 10:20:31 +01001038
Paul Duffin32cf58a2021-05-18 16:32:50 +01001039// Provides access to sdk_version related files, e.g. header and implementation jars.
Paul Duffin859fe962020-05-15 10:20:31 +01001040type SdkLibraryDependency interface {
1041 SdkLibraryComponentDependency
1042
1043 // Get the header jars appropriate for the supplied sdk_version.
1044 //
1045 // These are turbine generated jars so they only change if the externals of the
1046 // class changes but it does not contain and implementation or JavaDoc.
Jiyong Parkf1691d22021-03-29 20:11:58 +09001047 SdkHeaderJars(ctx android.BaseModuleContext, sdkVersion android.SdkSpec) android.Paths
Paul Duffin859fe962020-05-15 10:20:31 +01001048
1049 // Get the implementation jars appropriate for the supplied sdk version.
1050 //
1051 // These are either the implementation jar for the whole sdk library or the implementation
1052 // jars for the stubs. The latter should only be needed when generating JavaDoc as otherwise
1053 // they are identical to the corresponding header jars.
Jiyong Parkf1691d22021-03-29 20:11:58 +09001054 SdkImplementationJars(ctx android.BaseModuleContext, sdkVersion android.SdkSpec) android.Paths
Paul Duffin1267d872021-04-16 17:21:36 +01001055
1056 // SdkApiStubDexJar returns the dex jar for the stubs. It is needed by the hiddenapi processing
1057 // tool which processes dex files.
Martin Stjernholm8be1e6d2021-09-15 03:34:04 +01001058 SdkApiStubDexJar(ctx android.BaseModuleContext, kind android.SdkKind) OptionalDexJarPath
Paul Duffinf4600f62021-05-13 22:34:45 +01001059
Paul Duffin32cf58a2021-05-18 16:32:50 +01001060 // SdkRemovedTxtFile returns the optional path to the removed.txt file for the specified sdk kind.
1061 SdkRemovedTxtFile(ctx android.BaseModuleContext, kind android.SdkKind) android.OptionalPath
1062
Paul Duffinf4600f62021-05-13 22:34:45 +01001063 // sharedLibrary returns true if this can be used as a shared library.
1064 sharedLibrary() bool
Paul Duffin859fe962020-05-15 10:20:31 +01001065}
1066
Inseob Kimc0907f12019-02-08 21:00:45 +09001067type SdkLibrary struct {
Sundong Ahn054b19a2018-10-19 13:46:09 +09001068 Library
Jiyong Parkc678ad32018-04-10 13:07:10 +09001069
Sundong Ahn054b19a2018-10-19 13:46:09 +09001070 sdkLibraryProperties sdkLibraryProperties
Jiyong Parkc678ad32018-04-10 13:07:10 +09001071
Paul Duffin3375e352020-04-28 10:44:03 +01001072 // Map from api scope to the scope specific property structure.
1073 scopeToProperties map[*apiScope]*ApiScopeProperties
1074
Paul Duffin56d44902020-01-31 13:36:25 +00001075 commonToSdkLibraryAndImport
Jiyong Parkc678ad32018-04-10 13:07:10 +09001076}
1077
Inseob Kimc0907f12019-02-08 21:00:45 +09001078var _ SdkLibraryDependency = (*SdkLibrary)(nil)
Colin Cross897d2ed2019-02-11 14:03:51 -08001079
Paul Duffin3375e352020-04-28 10:44:03 +01001080func (module *SdkLibrary) generateTestAndSystemScopesByDefault() bool {
1081 return module.sdkLibraryProperties.Generate_system_and_test_apis
1082}
1083
1084func (module *SdkLibrary) getGeneratedApiScopes(ctx android.EarlyModuleContext) apiScopes {
1085 // Check to see if any scopes have been explicitly enabled. If any have then all
1086 // must be.
1087 anyScopesExplicitlyEnabled := false
1088 for _, scope := range allApiScopes {
1089 scopeProperties := module.scopeToProperties[scope]
1090 if scopeProperties.Enabled != nil {
1091 anyScopesExplicitlyEnabled = true
1092 break
1093 }
Paul Duffind1b3a922020-01-22 11:57:20 +00001094 }
Paul Duffin3375e352020-04-28 10:44:03 +01001095
1096 var generatedScopes apiScopes
1097 enabledScopes := make(map[*apiScope]struct{})
1098 for _, scope := range allApiScopes {
1099 scopeProperties := module.scopeToProperties[scope]
1100 // If any scopes are explicitly enabled then ignore the legacy enabled status.
1101 // This is to ensure that any new usages of this module type do not rely on legacy
1102 // behaviour.
1103 defaultEnabledStatus := false
1104 if anyScopesExplicitlyEnabled {
1105 defaultEnabledStatus = scope.defaultEnabledStatus
1106 } else {
1107 defaultEnabledStatus = scope.legacyEnabledStatus(module)
1108 }
1109 enabled := proptools.BoolDefault(scopeProperties.Enabled, defaultEnabledStatus)
1110 if enabled {
1111 enabledScopes[scope] = struct{}{}
1112 generatedScopes = append(generatedScopes, scope)
1113 }
1114 }
1115
1116 // Now check to make sure that any scope that is extended by an enabled scope is also
1117 // enabled.
1118 for _, scope := range allApiScopes {
1119 if _, ok := enabledScopes[scope]; ok {
1120 extends := scope.extends
1121 if extends != nil {
1122 if _, ok := enabledScopes[extends]; !ok {
1123 ctx.ModuleErrorf("enabled api scope %q depends on disabled scope %q", scope, extends)
1124 }
1125 }
1126 }
1127 }
1128
1129 return generatedScopes
Paul Duffind1b3a922020-01-22 11:57:20 +00001130}
1131
satayev758968a2021-12-06 11:42:40 +00001132var _ android.ModuleWithMinSdkVersionCheck = (*SdkLibrary)(nil)
1133
satayev8f088b02021-12-06 11:40:46 +00001134func (module *SdkLibrary) CheckMinSdkVersion(ctx android.ModuleContext) {
1135 android.CheckMinSdkVersion(ctx, module.MinSdkVersion(ctx).ApiLevel, func(c android.ModuleContext, do android.PayloadDepsCallback) {
1136 ctx.WalkDeps(func(child android.Module, parent android.Module) bool {
1137 isExternal := !module.depIsInSameApex(ctx, child)
1138 if am, ok := child.(android.ApexModule); ok {
1139 if !do(ctx, parent, am, isExternal) {
1140 return false
1141 }
1142 }
1143 return !isExternal
1144 })
1145 })
1146}
1147
Paul Duffineedc5d52020-06-12 17:46:39 +01001148type sdkLibraryComponentTag struct {
1149 blueprint.BaseDependencyTag
1150 name string
1151}
1152
1153// Mark this tag so dependencies that use it are excluded from visibility enforcement.
1154func (t sdkLibraryComponentTag) ExcludeFromVisibilityEnforcement() {}
1155
1156var xmlPermissionsFileTag = sdkLibraryComponentTag{name: "xml-permissions-file"}
Paul Duffine74ac732020-02-06 13:51:46 +00001157
Jiyong Parke3833882020-02-17 17:28:10 +09001158func IsXmlPermissionsFileDepTag(depTag blueprint.DependencyTag) bool {
Paul Duffineedc5d52020-06-12 17:46:39 +01001159 if dt, ok := depTag.(sdkLibraryComponentTag); ok {
Jiyong Parke3833882020-02-17 17:28:10 +09001160 return dt == xmlPermissionsFileTag
1161 }
1162 return false
1163}
1164
Paul Duffineedc5d52020-06-12 17:46:39 +01001165var implLibraryTag = sdkLibraryComponentTag{name: "impl-library"}
Paul Duffin5df79302020-05-16 15:52:12 +01001166
Paul Duffin44f1d842020-06-26 20:17:02 +01001167// Add the dependencies on the child modules in the component deps mutator.
1168func (module *SdkLibrary) ComponentDepsMutator(ctx android.BottomUpMutatorContext) {
Paul Duffin3375e352020-04-28 10:44:03 +01001169 for _, apiScope := range module.getGeneratedApiScopes(ctx) {
Paul Duffind1b3a922020-01-22 11:57:20 +00001170 // Add dependencies to the stubs library
Paul Duffinc3091c82020-05-08 14:16:20 +01001171 ctx.AddVariationDependencies(nil, apiScope.stubsTag, module.stubsLibraryModuleName(apiScope))
Paul Duffind1b3a922020-01-22 11:57:20 +00001172
Paul Duffin15f34ef2020-07-20 18:04:44 +01001173 // Add a dependency on the stubs source in order to access both stubs source and api information.
1174 ctx.AddVariationDependencies(nil, apiScope.stubsSourceAndApiTag, module.stubsSourceModuleName(apiScope))
Sundong Ahn054b19a2018-10-19 13:46:09 +09001175 }
1176
Paul Duffindfa131e2020-05-15 20:37:11 +01001177 if module.requiresRuntimeImplementationLibrary() {
Paul Duffin5df79302020-05-16 15:52:12 +01001178 // Add dependency to the rule for generating the implementation library.
1179 ctx.AddDependency(module, implLibraryTag, module.implLibraryModuleName())
1180
Paul Duffindfa131e2020-05-15 20:37:11 +01001181 if module.sharedLibrary() {
1182 // Add dependency to the rule for generating the xml permissions file
Paul Duffineedc5d52020-06-12 17:46:39 +01001183 ctx.AddDependency(module, xmlPermissionsFileTag, module.xmlPermissionsModuleName())
Paul Duffindfa131e2020-05-15 20:37:11 +01001184 }
Paul Duffin44f1d842020-06-26 20:17:02 +01001185 }
1186}
Paul Duffine74ac732020-02-06 13:51:46 +00001187
Paul Duffin44f1d842020-06-26 20:17:02 +01001188// Add other dependencies as normal.
1189func (module *SdkLibrary) DepsMutator(ctx android.BottomUpMutatorContext) {
Anton Hanssone77fccc2021-01-20 16:52:41 +00001190 var missingApiModules []string
1191 for _, apiScope := range module.getGeneratedApiScopes(ctx) {
1192 if apiScope.unstable {
1193 continue
1194 }
1195 if m := android.SrcIsModule(module.latestApiFilegroupName(apiScope)); !ctx.OtherModuleExists(m) {
1196 missingApiModules = append(missingApiModules, m)
1197 }
1198 if m := android.SrcIsModule(module.latestRemovedApiFilegroupName(apiScope)); !ctx.OtherModuleExists(m) {
1199 missingApiModules = append(missingApiModules, m)
1200 }
Jaewoong Jung1a97ee02021-03-09 13:25:02 -08001201 if m := android.SrcIsModule(module.latestIncompatibilitiesFilegroupName(apiScope)); !ctx.OtherModuleExists(m) {
1202 missingApiModules = append(missingApiModules, m)
1203 }
Anton Hanssone77fccc2021-01-20 16:52:41 +00001204 }
1205 if len(missingApiModules) != 0 && !module.sdkLibraryProperties.Unsafe_ignore_missing_latest_api {
1206 m := module.Name() + " is missing tracking files for previously released library versions.\n"
1207 m += "You need to do one of the following:\n"
1208 m += "- Add `unsafe_ignore_missing_latest_api: true` to your blueprint (to disable compat tracking)\n"
1209 m += "- Add a set of prebuilt txt files representing the last released version of this library for compat checking.\n"
1210 m += " (the current set of API files can be used as a seed for this compatibility tracking\n"
1211 m += "\n"
1212 m += "The following filegroup modules are missing:\n "
1213 m += strings.Join(missingApiModules, "\n ") + "\n"
1214 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."
1215 ctx.ModuleErrorf(m)
1216 }
Paul Duffin44f1d842020-06-26 20:17:02 +01001217 if module.requiresRuntimeImplementationLibrary() {
Paul Duffindfa131e2020-05-15 20:37:11 +01001218 // Only add the deps for the library if it is actually going to be built.
1219 module.Library.deps(ctx)
1220 }
Jiyong Parkc678ad32018-04-10 13:07:10 +09001221}
1222
Paul Duffin46dc45a2020-05-14 15:39:10 +01001223func (module *SdkLibrary) OutputFiles(tag string) (android.Paths, error) {
1224 paths, err := module.commonOutputFiles(tag)
Colin Cross4acaea92021-12-10 23:05:02 +00001225 if paths != nil || err != nil {
Paul Duffin46dc45a2020-05-14 15:39:10 +01001226 return paths, err
1227 }
Colin Cross4acaea92021-12-10 23:05:02 +00001228 if module.requiresRuntimeImplementationLibrary() {
1229 return module.Library.OutputFiles(tag)
1230 }
1231 if tag == "" {
1232 return nil, nil
1233 }
1234 return nil, fmt.Errorf("unsupported module reference tag %q", tag)
Paul Duffin46dc45a2020-05-14 15:39:10 +01001235}
1236
Inseob Kimc0907f12019-02-08 21:00:45 +09001237func (module *SdkLibrary) GenerateAndroidBuildActions(ctx android.ModuleContext) {
satayev8f088b02021-12-06 11:40:46 +00001238 if proptools.String(module.deviceProperties.Min_sdk_version) != "" {
1239 module.CheckMinSdkVersion(ctx)
1240 }
1241
Paul Duffina2ae7e02020-09-11 11:55:00 +01001242 module.generateCommonBuildActions(ctx)
1243
Paul Duffindfa131e2020-05-15 20:37:11 +01001244 // Only build an implementation library if required.
1245 if module.requiresRuntimeImplementationLibrary() {
Paul Duffin43db9be2019-12-30 17:35:49 +00001246 module.Library.GenerateAndroidBuildActions(ctx)
1247 }
Sundong Ahn054b19a2018-10-19 13:46:09 +09001248
Paul Duffinb97b1572021-04-29 21:50:40 +01001249 // Collate the components exported by this module. All scope specific modules are exported but
1250 // the impl and xml component modules are not.
1251 exportedComponents := map[string]struct{}{}
1252
Sundong Ahn57368eb2018-07-06 11:20:23 +09001253 // Record the paths to the header jars of the library (stubs and impl).
Paul Duffind1b3a922020-01-22 11:57:20 +00001254 // When this java_sdk_library is depended upon from others via "libs" property,
Jiyong Parkc678ad32018-04-10 13:07:10 +09001255 // the recorded paths will be returned depending on the link type of the caller.
1256 ctx.VisitDirectDeps(func(to android.Module) {
Jiyong Parkc678ad32018-04-10 13:07:10 +09001257 tag := ctx.OtherModuleDependencyTag(to)
1258
Paul Duffinc8782502020-04-29 20:45:27 +01001259 // Extract information from any of the scope specific dependencies.
1260 if scopeTag, ok := tag.(scopeDependencyTag); ok {
1261 apiScope := scopeTag.apiScope
Paul Duffin803a9562020-05-20 11:52:25 +01001262 scopePaths := module.getScopePathsCreateIfNeeded(apiScope)
Paul Duffinc8782502020-04-29 20:45:27 +01001263
1264 // Extract information from the dependency. The exact information extracted
1265 // is determined by the nature of the dependency which is determined by the tag.
1266 scopeTag.extractDepInfo(ctx, to, scopePaths)
Paul Duffinb97b1572021-04-29 21:50:40 +01001267
1268 exportedComponents[ctx.OtherModuleName(to)] = struct{}{}
Sundong Ahn20e998b2018-07-24 11:19:26 +09001269 }
Jiyong Parkc678ad32018-04-10 13:07:10 +09001270 })
Paul Duffinb97b1572021-04-29 21:50:40 +01001271
1272 // Make the set of components exported by this module available for use elsewhere.
1273 exportedComponentInfo := android.ExportedComponentsInfo{Components: android.SortedStringKeys(exportedComponents)}
1274 ctx.SetProvider(android.ExportedComponentsInfoProvider, exportedComponentInfo)
Jiyong Parkc678ad32018-04-10 13:07:10 +09001275}
1276
Jiyong Park0b0e1b92019-12-03 13:24:29 +09001277func (module *SdkLibrary) AndroidMkEntries() []android.AndroidMkEntries {
Paul Duffindfa131e2020-05-15 20:37:11 +01001278 if !module.requiresRuntimeImplementationLibrary() {
Paul Duffin43db9be2019-12-30 17:35:49 +00001279 return nil
1280 }
Jiyong Park0b0e1b92019-12-03 13:24:29 +09001281 entriesList := module.Library.AndroidMkEntries()
Yo Chiang07d75072020-06-05 17:43:19 +08001282 if module.sharedLibrary() {
1283 entries := &entriesList[0]
1284 entries.Required = append(entries.Required, module.xmlPermissionsModuleName())
1285 }
Jiyong Park0b0e1b92019-12-03 13:24:29 +09001286 return entriesList
Jiyong Park82484c02018-04-23 21:41:26 +09001287}
1288
Anton Hansson5fd5d242020-03-27 19:43:19 +00001289// The dist path of the stub artifacts
1290func (module *SdkLibrary) apiDistPath(apiScope *apiScope) string {
Colin Crossf0eace92021-06-02 13:02:23 -07001291 return path.Join("apistubs", module.distGroup(), apiScope.name)
Anton Hansson5fd5d242020-03-27 19:43:19 +00001292}
1293
Paul Duffin12ceb462019-12-24 20:31:31 +00001294// Get the sdk version for use when compiling the stubs library.
Paul Duffin780c5f42020-05-12 15:52:55 +01001295func (module *SdkLibrary) sdkVersionForStubsLibrary(mctx android.EarlyModuleContext, apiScope *apiScope) string {
Paul Duffin87a05a32020-05-12 11:50:28 +01001296 scopeProperties := module.scopeToProperties[apiScope]
1297 if scopeProperties.Sdk_version != nil {
1298 return proptools.String(scopeProperties.Sdk_version)
1299 }
1300
Jiyong Parkf1691d22021-03-29 20:11:58 +09001301 sdkDep := decodeSdkDep(mctx, android.SdkContext(&module.Library))
Paul Duffin12ceb462019-12-24 20:31:31 +00001302 if sdkDep.hasStandardLibs() {
1303 // If building against a standard sdk then use the sdk version appropriate for the scope.
Paul Duffind1b3a922020-01-22 11:57:20 +00001304 return apiScope.sdkVersion
Paul Duffin12ceb462019-12-24 20:31:31 +00001305 } else {
1306 // Otherwise, use no system module.
1307 return "none"
1308 }
1309}
1310
Paul Duffin31310252020-11-20 21:26:20 +00001311func (module *SdkLibrary) distStem() string {
1312 return proptools.StringDefault(module.sdkLibraryProperties.Dist_stem, module.BaseModuleName())
1313}
1314
Colin Cross986b69a2021-06-01 13:13:40 -07001315// distGroup returns the subdirectory of the dist path of the stub artifacts.
1316func (module *SdkLibrary) distGroup() string {
Colin Cross59b92bf2021-06-01 14:07:56 -07001317 return proptools.StringDefault(module.sdkLibraryProperties.Dist_group, "unknown")
Colin Cross986b69a2021-06-01 13:13:40 -07001318}
1319
Paul Duffind1b3a922020-01-22 11:57:20 +00001320func (module *SdkLibrary) latestApiFilegroupName(apiScope *apiScope) string {
Paul Duffin31310252020-11-20 21:26:20 +00001321 return ":" + module.distStem() + ".api." + apiScope.name + ".latest"
Jiyong Park58c518b2018-05-12 22:29:12 +09001322}
Jiyong Parkc678ad32018-04-10 13:07:10 +09001323
Paul Duffind1b3a922020-01-22 11:57:20 +00001324func (module *SdkLibrary) latestRemovedApiFilegroupName(apiScope *apiScope) string {
Paul Duffin31310252020-11-20 21:26:20 +00001325 return ":" + module.distStem() + "-removed.api." + apiScope.name + ".latest"
Jiyong Parkc678ad32018-04-10 13:07:10 +09001326}
1327
Jaewoong Jung1a97ee02021-03-09 13:25:02 -08001328func (module *SdkLibrary) latestIncompatibilitiesFilegroupName(apiScope *apiScope) string {
1329 return ":" + module.distStem() + "-incompatibilities.api." + apiScope.name + ".latest"
1330}
1331
Anton Hansson944e77d2020-08-19 11:40:22 +01001332func childModuleVisibility(childVisibility []string) []string {
1333 if childVisibility == nil {
1334 // No child visibility set. The child will use the visibility of the sdk_library.
1335 return nil
1336 }
1337
1338 // Prepend an override to ignore the sdk_library's visibility, and rely on the child visibility.
1339 var visibility []string
1340 visibility = append(visibility, "//visibility:override")
1341 visibility = append(visibility, childVisibility...)
1342 return visibility
1343}
1344
Paul Duffin5df79302020-05-16 15:52:12 +01001345// Creates the implementation java library
1346func (module *SdkLibrary) createImplLibrary(mctx android.DefaultableHookContext) {
Anton Hansson944e77d2020-08-19 11:40:22 +01001347 visibility := childModuleVisibility(module.sdkLibraryProperties.Impl_library_visibility)
1348
Paul Duffin5df79302020-05-16 15:52:12 +01001349 props := struct {
Paul Duffin66cdbf02021-05-14 16:35:06 +01001350 Name *string
1351 Visibility []string
1352 Instrument bool
1353 Libs []string
Paul Duffin5df79302020-05-16 15:52:12 +01001354 }{
1355 Name: proptools.StringPtr(module.implLibraryModuleName()),
Anton Hansson944e77d2020-08-19 11:40:22 +01001356 Visibility: visibility,
Paul Duffin296cf332020-06-18 21:09:55 +01001357 // Set the instrument property to ensure it is instrumented when instrumentation is required.
1358 Instrument: true,
Anton Hansson7f66efa2020-10-08 14:47:23 +01001359 // Set the impl_only libs. Note that the module's "Libs" get appended as well, via the
1360 // addition of &module.properties below.
1361 Libs: module.sdkLibraryProperties.Impl_only_libs,
Paul Duffin5df79302020-05-16 15:52:12 +01001362 }
1363
1364 properties := []interface{}{
1365 &module.properties,
1366 &module.protoProperties,
1367 &module.deviceProperties,
Liz Kammera7a64f32020-07-09 15:16:41 -07001368 &module.dexProperties,
Paul Duffin5df79302020-05-16 15:52:12 +01001369 &module.dexpreoptProperties,
Colin Cross014489c2020-06-02 20:09:13 -07001370 &module.linter.properties,
Paul Duffin5df79302020-05-16 15:52:12 +01001371 &props,
1372 module.sdkComponentPropertiesForChildLibrary(),
1373 }
1374 mctx.CreateModule(LibraryFactory, properties...)
1375}
1376
Jiyong Parkc678ad32018-04-10 13:07:10 +09001377// Creates a static java library that has API stubs
Paul Duffinf0229202020-04-29 16:47:28 +01001378func (module *SdkLibrary) createStubsLibrary(mctx android.DefaultableHookContext, apiScope *apiScope) {
Jiyong Parkc678ad32018-04-10 13:07:10 +09001379 props := struct {
Dan Willemsen9f435972020-05-28 15:28:00 -07001380 Name *string
1381 Visibility []string
1382 Srcs []string
1383 Installable *bool
1384 Sdk_version *string
1385 System_modules *string
1386 Patch_module *string
1387 Libs []string
Anton Hanssondae54cd2021-04-21 16:30:10 +01001388 Static_libs []string
Dan Willemsen9f435972020-05-28 15:28:00 -07001389 Compile_dex *bool
1390 Java_version *string
1391 Openjdk9 struct {
Sundong Ahn054b19a2018-10-19 13:46:09 +09001392 Srcs []string
1393 Javacflags []string
1394 }
Anton Hansson5fd5d242020-03-27 19:43:19 +00001395 Dist struct {
1396 Targets []string
1397 Dest *string
1398 Dir *string
1399 Tag *string
1400 }
Jiyong Parkc678ad32018-04-10 13:07:10 +09001401 }{}
1402
Paul Duffinc3091c82020-05-08 14:16:20 +01001403 props.Name = proptools.StringPtr(module.stubsLibraryModuleName(apiScope))
Anton Hansson944e77d2020-08-19 11:40:22 +01001404 props.Visibility = childModuleVisibility(module.sdkLibraryProperties.Stubs_library_visibility)
Jiyong Parkc678ad32018-04-10 13:07:10 +09001405 // sources are generated from the droiddoc
Paul Duffinc3091c82020-05-08 14:16:20 +01001406 props.Srcs = []string{":" + module.stubsSourceModuleName(apiScope)}
Paul Duffin12ceb462019-12-24 20:31:31 +00001407 sdkVersion := module.sdkVersionForStubsLibrary(mctx, apiScope)
Paul Duffin52d398a2019-06-11 12:31:14 +01001408 props.Sdk_version = proptools.StringPtr(sdkVersion)
Paul Duffina18abc22020-05-16 18:54:24 +01001409 props.System_modules = module.deviceProperties.System_modules
1410 props.Patch_module = module.properties.Patch_module
Paul Duffin367ab912019-12-23 19:40:36 +00001411 props.Installable = proptools.BoolPtr(false)
Sundong Ahn054b19a2018-10-19 13:46:09 +09001412 props.Libs = module.sdkLibraryProperties.Stub_only_libs
Anton Hanssondae54cd2021-04-21 16:30:10 +01001413 props.Static_libs = module.sdkLibraryProperties.Stub_only_static_libs
Paul Duffine22c2ab2020-05-20 19:35:27 +01001414 // The stub-annotations library contains special versions of the annotations
1415 // with CLASS retention policy, so that they're kept.
1416 if proptools.Bool(module.sdkLibraryProperties.Annotations_enabled) {
1417 props.Libs = append(props.Libs, "stub-annotations")
1418 }
Paul Duffina18abc22020-05-16 18:54:24 +01001419 props.Openjdk9.Srcs = module.properties.Openjdk9.Srcs
1420 props.Openjdk9.Javacflags = module.properties.Openjdk9.Javacflags
Anton Hansson83509b52020-05-21 09:21:57 +01001421 // We compile the stubs for 1.8 in line with the main android.jar stubs, and potential
1422 // interop with older developer tools that don't support 1.9.
1423 props.Java_version = proptools.StringPtr("1.8")
Paul Duffinf4600f62021-05-13 22:34:45 +01001424
1425 // The imports need to be compiled to dex if the java_sdk_library requests it.
1426 compileDex := module.dexProperties.Compile_dex
1427 if module.stubLibrariesCompiledForDex() {
1428 compileDex = proptools.BoolPtr(true)
Sundong Ahndd567f92018-07-31 17:19:11 +09001429 }
Paul Duffinf4600f62021-05-13 22:34:45 +01001430 props.Compile_dex = compileDex
Jiyong Parkc678ad32018-04-10 13:07:10 +09001431
Anton Hansson5fd5d242020-03-27 19:43:19 +00001432 // Dist the class jar artifact for sdk builds.
1433 if !Bool(module.sdkLibraryProperties.No_dist) {
1434 props.Dist.Targets = []string{"sdk", "win_sdk"}
Paul Duffin31310252020-11-20 21:26:20 +00001435 props.Dist.Dest = proptools.StringPtr(fmt.Sprintf("%v.jar", module.distStem()))
Anton Hansson5fd5d242020-03-27 19:43:19 +00001436 props.Dist.Dir = proptools.StringPtr(module.apiDistPath(apiScope))
1437 props.Dist.Tag = proptools.StringPtr(".jar")
1438 }
Jiyong Parkc678ad32018-04-10 13:07:10 +09001439
Paul Duffin859fe962020-05-15 10:20:31 +01001440 mctx.CreateModule(LibraryFactory, &props, module.sdkComponentPropertiesForChildLibrary())
Jiyong Parkc678ad32018-04-10 13:07:10 +09001441}
1442
Paul Duffin6d0886e2020-04-07 18:49:53 +01001443// Creates a droidstubs module that creates stubs source files from the given full source
Paul Duffinc8782502020-04-29 20:45:27 +01001444// files and also updates and checks the API specification files.
Paul Duffin15f34ef2020-07-20 18:04:44 +01001445func (module *SdkLibrary) createStubsSourcesAndApi(mctx android.DefaultableHookContext, apiScope *apiScope, name string, scopeSpecificDroidstubsArgs []string) {
Jiyong Parkc678ad32018-04-10 13:07:10 +09001446 props := struct {
Sundong Ahn054b19a2018-10-19 13:46:09 +09001447 Name *string
Paul Duffin4911a892020-04-29 23:35:13 +01001448 Visibility []string
Sundong Ahn054b19a2018-10-19 13:46:09 +09001449 Srcs []string
1450 Installable *bool
Paul Duffin52d398a2019-06-11 12:31:14 +01001451 Sdk_version *string
Paul Duffin12ceb462019-12-24 20:31:31 +00001452 System_modules *string
Sundong Ahn054b19a2018-10-19 13:46:09 +09001453 Libs []string
Paul Duffin6877e6d2020-09-25 19:59:14 +01001454 Output_javadoc_comments *bool
Paul Duffin11512472019-02-11 15:55:17 +00001455 Arg_files []string
Sundong Ahn054b19a2018-10-19 13:46:09 +09001456 Args *string
Sundong Ahn054b19a2018-10-19 13:46:09 +09001457 Java_version *string
Paul Duffine22c2ab2020-05-20 19:35:27 +01001458 Annotations_enabled *bool
Sundong Ahn054b19a2018-10-19 13:46:09 +09001459 Merge_annotations_dirs []string
1460 Merge_inclusion_annotations_dirs []string
Paul Duffin0ff08bd2020-04-29 13:30:54 +01001461 Generate_stubs *bool
Anton Hanssone87b03d2020-12-21 15:29:34 +00001462 Previous_api *string
Sundong Ahn054b19a2018-10-19 13:46:09 +09001463 Check_api struct {
Anton Hanssone6056152020-12-31 10:37:27 +00001464 Current ApiToCheck
1465 Last_released ApiToCheck
Paul Duffin160fe412020-05-10 19:32:20 +01001466
1467 Api_lint struct {
1468 Enabled *bool
1469 New_since *string
1470 Baseline_file *string
1471 }
Jiyong Park58c518b2018-05-12 22:29:12 +09001472 }
Sundong Ahn1b92c822018-05-29 11:35:17 +09001473 Aidl struct {
1474 Include_dirs []string
1475 Local_include_dirs []string
1476 }
Paul Duffin040e9062020-11-23 17:41:36 +00001477 Dists []android.Dist
Jiyong Parkc678ad32018-04-10 13:07:10 +09001478 }{}
1479
Paul Duffin7b78b4d2020-04-28 14:08:32 +01001480 // The stubs source processing uses the same compile time classpath when extracting the
1481 // API from the implementation library as it does when compiling it. i.e. the same
1482 // * sdk version
1483 // * system_modules
1484 // * libs (static_libs/libs)
Paul Duffin250e6192019-06-07 10:44:37 +01001485
Paul Duffin0ff08bd2020-04-29 13:30:54 +01001486 props.Name = proptools.StringPtr(name)
Anton Hansson944e77d2020-08-19 11:40:22 +01001487 props.Visibility = childModuleVisibility(module.sdkLibraryProperties.Stubs_source_visibility)
Paul Duffina18abc22020-05-16 18:54:24 +01001488 props.Srcs = append(props.Srcs, module.properties.Srcs...)
Anton Hanssonf8ea3722021-09-16 14:24:13 +01001489 props.Srcs = append(props.Srcs, module.sdkLibraryProperties.Api_srcs...)
Paul Duffina18abc22020-05-16 18:54:24 +01001490 props.Sdk_version = module.deviceProperties.Sdk_version
1491 props.System_modules = module.deviceProperties.System_modules
Jiyong Parkc678ad32018-04-10 13:07:10 +09001492 props.Installable = proptools.BoolPtr(false)
Sundong Ahne6f0b052018-06-05 16:46:14 +09001493 // A droiddoc module has only one Libs property and doesn't distinguish between
1494 // shared libs and static libs. So we need to add both of these libs to Libs property.
Paul Duffina18abc22020-05-16 18:54:24 +01001495 props.Libs = module.properties.Libs
1496 props.Libs = append(props.Libs, module.properties.Static_libs...)
1497 props.Aidl.Include_dirs = module.deviceProperties.Aidl.Include_dirs
1498 props.Aidl.Local_include_dirs = module.deviceProperties.Aidl.Local_include_dirs
1499 props.Java_version = module.properties.Java_version
Jiyong Parkc678ad32018-04-10 13:07:10 +09001500
Paul Duffine22c2ab2020-05-20 19:35:27 +01001501 props.Annotations_enabled = module.sdkLibraryProperties.Annotations_enabled
Sundong Ahn054b19a2018-10-19 13:46:09 +09001502 props.Merge_annotations_dirs = module.sdkLibraryProperties.Merge_annotations_dirs
1503 props.Merge_inclusion_annotations_dirs = module.sdkLibraryProperties.Merge_inclusion_annotations_dirs
1504
Paul Duffin6d0886e2020-04-07 18:49:53 +01001505 droidstubsArgs := []string{}
Paul Duffin235ffff2019-12-24 10:41:30 +00001506 if len(module.sdkLibraryProperties.Api_packages) != 0 {
Paul Duffin6d0886e2020-04-07 18:49:53 +01001507 droidstubsArgs = append(droidstubsArgs, "--stub-packages "+strings.Join(module.sdkLibraryProperties.Api_packages, ":"))
Paul Duffin235ffff2019-12-24 10:41:30 +00001508 }
1509 if len(module.sdkLibraryProperties.Hidden_api_packages) != 0 {
Paul Duffin6d0886e2020-04-07 18:49:53 +01001510 droidstubsArgs = append(droidstubsArgs,
Paul Duffin235ffff2019-12-24 10:41:30 +00001511 android.JoinWithPrefix(module.sdkLibraryProperties.Hidden_api_packages, " --hide-package "))
1512 }
Paul Duffin6d0886e2020-04-07 18:49:53 +01001513 droidstubsArgs = append(droidstubsArgs, module.sdkLibraryProperties.Droiddoc_options...)
Paul Duffin235ffff2019-12-24 10:41:30 +00001514 disabledWarnings := []string{
1515 "MissingPermission",
1516 "BroadcastBehavior",
1517 "HiddenSuperclass",
1518 "DeprecationMismatch",
1519 "UnavailableSymbol",
1520 "SdkConstant",
1521 "HiddenTypeParameter",
1522 "Todo",
1523 "Typo",
1524 }
Paul Duffin6d0886e2020-04-07 18:49:53 +01001525 droidstubsArgs = append(droidstubsArgs, android.JoinWithPrefix(disabledWarnings, "--hide "))
Sundong Ahnfb2721f2018-09-17 13:23:09 +09001526
Paul Duffin6877e6d2020-09-25 19:59:14 +01001527 // Output Javadoc comments for public scope.
1528 if apiScope == apiScopePublic {
1529 props.Output_javadoc_comments = proptools.BoolPtr(true)
1530 }
1531
Paul Duffin1fb487d2020-04-07 18:50:10 +01001532 // Add in scope specific arguments.
Paul Duffin0ff08bd2020-04-29 13:30:54 +01001533 droidstubsArgs = append(droidstubsArgs, scopeSpecificDroidstubsArgs...)
Paul Duffin11512472019-02-11 15:55:17 +00001534 props.Arg_files = module.sdkLibraryProperties.Droiddoc_option_files
Paul Duffin6d0886e2020-04-07 18:49:53 +01001535 props.Args = proptools.StringPtr(strings.Join(droidstubsArgs, " "))
Jiyong Parkc678ad32018-04-10 13:07:10 +09001536
Paul Duffin15f34ef2020-07-20 18:04:44 +01001537 // List of APIs identified from the provided source files are created. They are later
1538 // compared against to the not-yet-released (a.k.a current) list of APIs and to the
1539 // last-released (a.k.a numbered) list of API.
1540 currentApiFileName := apiScope.apiFilePrefix + "current.txt"
1541 removedApiFileName := apiScope.apiFilePrefix + "removed.txt"
1542 apiDir := module.getApiDir()
1543 currentApiFileName = path.Join(apiDir, currentApiFileName)
1544 removedApiFileName = path.Join(apiDir, removedApiFileName)
Jiyong Parkc678ad32018-04-10 13:07:10 +09001545
Paul Duffin15f34ef2020-07-20 18:04:44 +01001546 // check against the not-yet-release API
1547 props.Check_api.Current.Api_file = proptools.StringPtr(currentApiFileName)
1548 props.Check_api.Current.Removed_api_file = proptools.StringPtr(removedApiFileName)
Jiyong Park58c518b2018-05-12 22:29:12 +09001549
Anton Hanssone6056152020-12-31 10:37:27 +00001550 if !(apiScope.unstable || module.sdkLibraryProperties.Unsafe_ignore_missing_latest_api) {
Paul Duffin15f34ef2020-07-20 18:04:44 +01001551 // check against the latest released API
1552 latestApiFilegroupName := proptools.StringPtr(module.latestApiFilegroupName(apiScope))
Anton Hanssone87b03d2020-12-21 15:29:34 +00001553 props.Previous_api = latestApiFilegroupName
Paul Duffin15f34ef2020-07-20 18:04:44 +01001554 props.Check_api.Last_released.Api_file = latestApiFilegroupName
1555 props.Check_api.Last_released.Removed_api_file = proptools.StringPtr(
1556 module.latestRemovedApiFilegroupName(apiScope))
Jaewoong Jung1a97ee02021-03-09 13:25:02 -08001557 props.Check_api.Last_released.Baseline_file = proptools.StringPtr(
1558 module.latestIncompatibilitiesFilegroupName(apiScope))
Paul Duffin160fe412020-05-10 19:32:20 +01001559
Paul Duffin15f34ef2020-07-20 18:04:44 +01001560 if proptools.Bool(module.sdkLibraryProperties.Api_lint.Enabled) {
1561 // Enable api lint.
1562 props.Check_api.Api_lint.Enabled = proptools.BoolPtr(true)
1563 props.Check_api.Api_lint.New_since = latestApiFilegroupName
Paul Duffin160fe412020-05-10 19:32:20 +01001564
Paul Duffin15f34ef2020-07-20 18:04:44 +01001565 // If it exists then pass a lint-baseline.txt through to droidstubs.
1566 baselinePath := path.Join(apiDir, apiScope.apiFilePrefix+"lint-baseline.txt")
1567 baselinePathRelativeToRoot := path.Join(mctx.ModuleDir(), baselinePath)
1568 paths, err := mctx.GlobWithDeps(baselinePathRelativeToRoot, nil)
1569 if err != nil {
1570 mctx.ModuleErrorf("error checking for presence of %s: %s", baselinePathRelativeToRoot, err)
1571 }
1572 if len(paths) == 1 {
1573 props.Check_api.Api_lint.Baseline_file = proptools.StringPtr(baselinePath)
1574 } else if len(paths) != 0 {
1575 mctx.ModuleErrorf("error checking for presence of %s: expected one path, found: %v", baselinePathRelativeToRoot, paths)
Paul Duffin160fe412020-05-10 19:32:20 +01001576 }
1577 }
Paul Duffin15f34ef2020-07-20 18:04:44 +01001578 }
Jiyong Park58c518b2018-05-12 22:29:12 +09001579
Paul Duffin15f34ef2020-07-20 18:04:44 +01001580 if !Bool(module.sdkLibraryProperties.No_dist) {
Paul Duffin040e9062020-11-23 17:41:36 +00001581 // Dist the api txt and removed api txt artifacts for sdk builds.
1582 distDir := proptools.StringPtr(path.Join(module.apiDistPath(apiScope), "api"))
1583 for _, p := range []struct {
1584 tag string
1585 pattern string
1586 }{
1587 {tag: ".api.txt", pattern: "%s.txt"},
1588 {tag: ".removed-api.txt", pattern: "%s-removed.txt"},
1589 } {
1590 props.Dists = append(props.Dists, android.Dist{
1591 Targets: []string{"sdk", "win_sdk"},
1592 Dir: distDir,
1593 Dest: proptools.StringPtr(fmt.Sprintf(p.pattern, module.distStem())),
1594 Tag: proptools.StringPtr(p.tag),
1595 })
1596 }
Anton Hansson5fd5d242020-03-27 19:43:19 +00001597 }
1598
Colin Cross84dfc3d2019-09-25 11:33:01 -07001599 mctx.CreateModule(DroidstubsFactory, &props)
Jiyong Parkc678ad32018-04-10 13:07:10 +09001600}
1601
Paul Duffinea8f8082021-06-24 13:25:57 +01001602// Implements android.ApexModule
Jooyung Han5e9013b2020-03-10 06:23:13 +09001603func (module *SdkLibrary) DepIsInSameApex(mctx android.BaseModuleContext, dep android.Module) bool {
1604 depTag := mctx.OtherModuleDependencyTag(dep)
1605 if depTag == xmlPermissionsFileTag {
1606 return true
1607 }
1608 return module.Library.DepIsInSameApex(mctx, dep)
1609}
1610
Paul Duffinea8f8082021-06-24 13:25:57 +01001611// Implements android.ApexModule
1612func (module *SdkLibrary) UniqueApexVariations() bool {
1613 return module.uniqueApexVariations()
1614}
1615
Jiyong Parkc678ad32018-04-10 13:07:10 +09001616// Creates the xml file that publicizes the runtime library
Paul Duffinf0229202020-04-29 16:47:28 +01001617func (module *SdkLibrary) createXmlFile(mctx android.DefaultableHookContext) {
Pedro Loureiroc3621422021-09-28 15:40:23 +00001618 moduleMinApiLevel := module.Library.MinSdkVersion(mctx).ApiLevel
1619 var moduleMinApiLevelStr = moduleMinApiLevel.String()
1620 if moduleMinApiLevel == android.NoneApiLevel {
1621 moduleMinApiLevelStr = "current"
1622 }
Jiyong Parke3833882020-02-17 17:28:10 +09001623 props := struct {
Pedro Loureiroc3621422021-09-28 15:40:23 +00001624 Name *string
1625 Lib_name *string
1626 Apex_available []string
1627 On_bootclasspath_since *string
1628 On_bootclasspath_before *string
1629 Min_device_sdk *string
1630 Max_device_sdk *string
1631 Sdk_library_min_api_level *string
Jiyong Parke3833882020-02-17 17:28:10 +09001632 }{
Pedro Loureiroc3621422021-09-28 15:40:23 +00001633 Name: proptools.StringPtr(module.xmlPermissionsModuleName()),
1634 Lib_name: proptools.StringPtr(module.BaseModuleName()),
1635 Apex_available: module.ApexProperties.Apex_available,
1636 On_bootclasspath_since: module.commonSdkLibraryProperties.On_bootclasspath_since,
1637 On_bootclasspath_before: module.commonSdkLibraryProperties.On_bootclasspath_before,
1638 Min_device_sdk: module.commonSdkLibraryProperties.Min_device_sdk,
1639 Max_device_sdk: module.commonSdkLibraryProperties.Max_device_sdk,
1640 Sdk_library_min_api_level: &moduleMinApiLevelStr,
Jiyong Parkc678ad32018-04-10 13:07:10 +09001641 }
Jiyong Parke3833882020-02-17 17:28:10 +09001642
Jiyong Parke3833882020-02-17 17:28:10 +09001643 mctx.CreateModule(sdkLibraryXmlFactory, &props)
Jiyong Parkc678ad32018-04-10 13:07:10 +09001644}
1645
Jiyong Parkf1691d22021-03-29 20:11:58 +09001646func PrebuiltJars(ctx android.BaseModuleContext, baseName string, s android.SdkSpec) android.Paths {
Jiyong Park54105c42021-03-31 18:17:53 +09001647 var ver android.ApiLevel
Jiyong Parkf1691d22021-03-29 20:11:58 +09001648 var kind android.SdkKind
1649 if s.UsePrebuilt(ctx) {
Jiyong Park54105c42021-03-31 18:17:53 +09001650 ver = s.ApiLevel
Jiyong Parkf1691d22021-03-29 20:11:58 +09001651 kind = s.Kind
Jiyong Parkc678ad32018-04-10 13:07:10 +09001652 } else {
Jiyong Park6a927c42020-01-21 02:03:43 +09001653 // We don't have prebuilt SDK for the specific sdkVersion.
1654 // Instead of breaking the build, fallback to use "system_current"
Jiyong Park54105c42021-03-31 18:17:53 +09001655 ver = android.FutureApiLevel
Jiyong Parkf1691d22021-03-29 20:11:58 +09001656 kind = android.SdkSystem
Sundong Ahn054b19a2018-10-19 13:46:09 +09001657 }
Jiyong Park6a927c42020-01-21 02:03:43 +09001658
1659 dir := filepath.Join("prebuilts", "sdk", ver.String(), kind.String())
Paul Duffin50061512020-01-21 16:31:05 +00001660 jar := filepath.Join(dir, baseName+".jar")
Sundong Ahn054b19a2018-10-19 13:46:09 +09001661 jarPath := android.ExistentPathForSource(ctx, jar)
Sundong Ahnae418ac2019-02-28 15:01:28 +09001662 if !jarPath.Valid() {
Colin Cross07c88562020-01-07 09:34:44 -08001663 if ctx.Config().AllowMissingDependencies() {
1664 return android.Paths{android.PathForSource(ctx, jar)}
1665 } else {
Jiyong Parkf1691d22021-03-29 20:11:58 +09001666 ctx.PropertyErrorf("sdk_library", "invalid sdk version %q, %q does not exist", s.Raw, jar)
Colin Cross07c88562020-01-07 09:34:44 -08001667 }
Sundong Ahnae418ac2019-02-28 15:01:28 +09001668 return nil
1669 }
Sundong Ahn054b19a2018-10-19 13:46:09 +09001670 return android.Paths{jarPath.Path()}
1671}
1672
Colin Crossaede88c2020-08-11 12:17:01 -07001673// 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 +01001674//
1675// If either this or the other module are on the platform then this will return
1676// false.
Colin Cross56a83212020-09-15 18:30:11 -07001677func withinSameApexesAs(ctx android.BaseModuleContext, other android.Module) bool {
1678 apexInfo := ctx.Provider(android.ApexInfoProvider).(android.ApexInfo)
1679 otherApexInfo := ctx.OtherModuleProvider(other, android.ApexInfoProvider).(android.ApexInfo)
Jiyong Parkab50b072021-05-12 17:13:56 +09001680 return len(otherApexInfo.InApexVariants) > 0 && reflect.DeepEqual(apexInfo.InApexVariants, otherApexInfo.InApexVariants)
Paul Duffin9b879592020-05-26 13:21:35 +01001681}
1682
Jiyong Parkf1691d22021-03-29 20:11:58 +09001683func (module *SdkLibrary) sdkJars(ctx android.BaseModuleContext, sdkVersion android.SdkSpec, headerJars bool) android.Paths {
Jiyong Park932cdfe2020-05-28 00:19:53 +09001684 // If the client doesn't set sdk_version, but if this library prefers stubs over
1685 // the impl library, let's provide the widest API surface possible. To do so,
1686 // force override sdk_version to module_current so that the closest possible API
1687 // surface could be found in selectHeaderJarsForSdkVersion
Jiyong Parkf1691d22021-03-29 20:11:58 +09001688 if module.defaultsToStubs() && !sdkVersion.Specified() {
Jiyong Park92315372021-04-02 08:45:46 +09001689 sdkVersion = android.SdkSpecFrom(ctx, "module_current")
Jiyong Park932cdfe2020-05-28 00:19:53 +09001690 }
Paul Duffind1b3a922020-01-22 11:57:20 +00001691
Paul Duffindaaa3322020-05-26 18:13:57 +01001692 // Only provide access to the implementation library if it is actually built.
1693 if module.requiresRuntimeImplementationLibrary() {
1694 // Check any special cases for java_sdk_library.
1695 //
1696 // Only allow access to the implementation library in the following condition:
1697 // * No sdk_version specified on the referencing module.
Paul Duffin9b879592020-05-26 13:21:35 +01001698 // * The referencing module is in the same apex as this.
Jiyong Parkf1691d22021-03-29 20:11:58 +09001699 if sdkVersion.Kind == android.SdkPrivate || withinSameApexesAs(ctx, module) {
Paul Duffindaaa3322020-05-26 18:13:57 +01001700 if headerJars {
1701 return module.HeaderJars()
1702 } else {
1703 return module.ImplementationJars()
1704 }
Sundong Ahn054b19a2018-10-19 13:46:09 +09001705 }
Jiyong Parkc678ad32018-04-10 13:07:10 +09001706 }
Paul Duffinb05d4292020-05-20 12:19:10 +01001707
Paul Duffin23970f42020-05-20 14:20:02 +01001708 return module.selectHeaderJarsForSdkVersion(ctx, sdkVersion)
Jiyong Parkc678ad32018-04-10 13:07:10 +09001709}
1710
Sundong Ahn241cd372018-07-13 16:16:44 +09001711// to satisfy SdkLibraryDependency interface
Jiyong Parkf1691d22021-03-29 20:11:58 +09001712func (module *SdkLibrary) SdkHeaderJars(ctx android.BaseModuleContext, sdkVersion android.SdkSpec) android.Paths {
Paul Duffind1b3a922020-01-22 11:57:20 +00001713 return module.sdkJars(ctx, sdkVersion, true /*headerJars*/)
1714}
1715
1716// to satisfy SdkLibraryDependency interface
Jiyong Parkf1691d22021-03-29 20:11:58 +09001717func (module *SdkLibrary) SdkImplementationJars(ctx android.BaseModuleContext, sdkVersion android.SdkSpec) android.Paths {
Paul Duffind1b3a922020-01-22 11:57:20 +00001718 return module.sdkJars(ctx, sdkVersion, false /*headerJars*/)
Sundong Ahn241cd372018-07-13 16:16:44 +09001719}
1720
Colin Cross571cccf2019-02-04 11:22:08 -08001721var javaSdkLibrariesKey = android.NewOnceKey("javaSdkLibraries")
1722
Jiyong Park82484c02018-04-23 21:41:26 +09001723func javaSdkLibraries(config android.Config) *[]string {
Colin Cross571cccf2019-02-04 11:22:08 -08001724 return config.Once(javaSdkLibrariesKey, func() interface{} {
Jiyong Park82484c02018-04-23 21:41:26 +09001725 return &[]string{}
1726 }).(*[]string)
1727}
1728
Paul Duffin749f98f2019-12-30 17:23:46 +00001729func (module *SdkLibrary) getApiDir() string {
1730 return proptools.StringDefault(module.sdkLibraryProperties.Api_dir, "api")
1731}
1732
Jiyong Parkc678ad32018-04-10 13:07:10 +09001733// For a java_sdk_library module, create internal modules for stubs, docs,
1734// runtime libs and xml file. If requested, the stubs and docs are created twice
1735// once for public API level and once for system API level
Paul Duffinf0229202020-04-29 16:47:28 +01001736func (module *SdkLibrary) CreateInternalModules(mctx android.DefaultableHookContext) {
1737 // If the module has been disabled then don't create any child modules.
1738 if !module.Enabled() {
1739 return
1740 }
1741
Paul Duffina18abc22020-05-16 18:54:24 +01001742 if len(module.properties.Srcs) == 0 {
Inseob Kimc0907f12019-02-08 21:00:45 +09001743 mctx.PropertyErrorf("srcs", "java_sdk_library must specify srcs")
Jooyung Han58f26ab2019-12-18 15:34:32 +09001744 return
Inseob Kimc0907f12019-02-08 21:00:45 +09001745 }
1746
Paul Duffin37e0b772019-12-30 17:20:10 +00001747 // If this builds against standard libraries (i.e. is not part of the core libraries)
Paul Duffin4f5c1ef2020-11-19 14:53:43 +00001748 // then assume it provides both system and test apis.
Jiyong Parkf1691d22021-03-29 20:11:58 +09001749 sdkDep := decodeSdkDep(mctx, android.SdkContext(&module.Library))
Paul Duffin37e0b772019-12-30 17:20:10 +00001750 hasSystemAndTestApis := sdkDep.hasStandardLibs()
Paul Duffin3375e352020-04-28 10:44:03 +01001751 module.sdkLibraryProperties.Generate_system_and_test_apis = hasSystemAndTestApis
Paul Duffin4f5c1ef2020-11-19 14:53:43 +00001752
Jaewoong Jung18aefc12020-12-21 09:11:10 -08001753 missingCurrentApi := false
Inseob Kim8098faa2019-03-18 10:19:51 +09001754
Paul Duffin3375e352020-04-28 10:44:03 +01001755 generatedScopes := module.getGeneratedApiScopes(mctx)
Paul Duffind1b3a922020-01-22 11:57:20 +00001756
Paul Duffin749f98f2019-12-30 17:23:46 +00001757 apiDir := module.getApiDir()
Paul Duffin3375e352020-04-28 10:44:03 +01001758 for _, scope := range generatedScopes {
Inseob Kim8098faa2019-03-18 10:19:51 +09001759 for _, api := range []string{"current.txt", "removed.txt"} {
Paul Duffind1b3a922020-01-22 11:57:20 +00001760 path := path.Join(mctx.ModuleDir(), apiDir, scope.apiFilePrefix+api)
Inseob Kim8098faa2019-03-18 10:19:51 +09001761 p := android.ExistentPathForSource(mctx, path)
1762 if !p.Valid() {
Colin Cross18f840c2021-05-20 17:56:54 -07001763 if mctx.Config().AllowMissingDependencies() {
1764 mctx.AddMissingDependencies([]string{path})
1765 } else {
1766 mctx.ModuleErrorf("Current api file %#v doesn't exist", path)
1767 missingCurrentApi = true
1768 }
Inseob Kim8098faa2019-03-18 10:19:51 +09001769 }
1770 }
1771 }
1772
Jaewoong Jung18aefc12020-12-21 09:11:10 -08001773 if missingCurrentApi {
Inseob Kim8098faa2019-03-18 10:19:51 +09001774 script := "build/soong/scripts/gen-java-current-api-files.sh"
1775 p := android.ExistentPathForSource(mctx, script)
1776
1777 if !p.Valid() {
1778 panic(fmt.Sprintf("script file %s doesn't exist", script))
1779 }
1780
1781 mctx.ModuleErrorf("One or more current api files are missing. "+
1782 "You can update them by:\n"+
Paul Duffin37e0b772019-12-30 17:20:10 +00001783 "%s %q %s && m update-api",
Paul Duffind1b3a922020-01-22 11:57:20 +00001784 script, filepath.Join(mctx.ModuleDir(), apiDir),
Paul Duffin3375e352020-04-28 10:44:03 +01001785 strings.Join(generatedScopes.Strings(func(s *apiScope) string { return s.apiFilePrefix }), " "))
Inseob Kim8098faa2019-03-18 10:19:51 +09001786 return
1787 }
1788
Paul Duffin3375e352020-04-28 10:44:03 +01001789 for _, scope := range generatedScopes {
Paul Duffin15f34ef2020-07-20 18:04:44 +01001790 // Use the stubs source name for legacy reasons.
1791 module.createStubsSourcesAndApi(mctx, scope, module.stubsSourceModuleName(scope), scope.droidstubsArgs)
Paul Duffin0ff08bd2020-04-29 13:30:54 +01001792
Paul Duffind1b3a922020-01-22 11:57:20 +00001793 module.createStubsLibrary(mctx, scope)
Inseob Kimc0907f12019-02-08 21:00:45 +09001794 }
1795
Paul Duffindfa131e2020-05-15 20:37:11 +01001796 if module.requiresRuntimeImplementationLibrary() {
Paul Duffin5df79302020-05-16 15:52:12 +01001797 // Create child module to create an implementation library.
1798 //
1799 // This temporarily creates a second implementation library that can be explicitly
1800 // referenced.
1801 //
1802 // TODO(b/156618935) - update comment once only one implementation library is created.
1803 module.createImplLibrary(mctx)
1804
Paul Duffindfa131e2020-05-15 20:37:11 +01001805 // Only create an XML permissions file that declares the library as being usable
1806 // as a shared library if required.
1807 if module.sharedLibrary() {
1808 module.createXmlFile(mctx)
1809 }
Paul Duffin43db9be2019-12-30 17:35:49 +00001810
1811 // record java_sdk_library modules so that they are exported to make
1812 javaSdkLibraries := javaSdkLibraries(mctx.Config())
1813 javaSdkLibrariesLock.Lock()
1814 defer javaSdkLibrariesLock.Unlock()
1815 *javaSdkLibraries = append(*javaSdkLibraries, module.BaseModuleName())
1816 }
Anton Hansson7f66efa2020-10-08 14:47:23 +01001817
1818 // Add the impl_only_libs *after* we're done using the Libs prop in submodules.
1819 module.properties.Libs = append(module.properties.Libs, module.sdkLibraryProperties.Impl_only_libs...)
Inseob Kimc0907f12019-02-08 21:00:45 +09001820}
1821
1822func (module *SdkLibrary) InitSdkLibraryProperties() {
Colin Crossce6734e2020-06-15 16:09:53 -07001823 module.addHostAndDeviceProperties()
1824 module.AddProperties(&module.sdkLibraryProperties)
Sundong Ahn054b19a2018-10-19 13:46:09 +09001825
Paul Duffin71b33cc2021-06-23 11:39:47 +01001826 module.initSdkLibraryComponent(module)
Paul Duffin859fe962020-05-15 10:20:31 +01001827
Paul Duffina18abc22020-05-16 18:54:24 +01001828 module.properties.Installable = proptools.BoolPtr(true)
1829 module.deviceProperties.IsSDKLibrary = true
Inseob Kimc0907f12019-02-08 21:00:45 +09001830}
Sundong Ahn054b19a2018-10-19 13:46:09 +09001831
Paul Duffindfa131e2020-05-15 20:37:11 +01001832func (module *SdkLibrary) requiresRuntimeImplementationLibrary() bool {
1833 return !proptools.Bool(module.sdkLibraryProperties.Api_only)
1834}
1835
Jiyong Park932cdfe2020-05-28 00:19:53 +09001836func (module *SdkLibrary) defaultsToStubs() bool {
1837 return proptools.Bool(module.sdkLibraryProperties.Default_to_stubs)
1838}
1839
Paul Duffin1b1e8062020-05-08 13:44:43 +01001840// Defines how to name the individual component modules the sdk library creates.
1841type sdkLibraryComponentNamingScheme interface {
1842 stubsLibraryModuleName(scope *apiScope, baseName string) string
1843
1844 stubsSourceModuleName(scope *apiScope, baseName string) string
Paul Duffin1b1e8062020-05-08 13:44:43 +01001845}
1846
1847type defaultNamingScheme struct {
1848}
1849
1850func (s *defaultNamingScheme) stubsLibraryModuleName(scope *apiScope, baseName string) string {
1851 return scope.stubsLibraryModuleName(baseName)
1852}
1853
1854func (s *defaultNamingScheme) stubsSourceModuleName(scope *apiScope, baseName string) string {
1855 return scope.stubsSourceModuleName(baseName)
1856}
1857
Paul Duffin1b1e8062020-05-08 13:44:43 +01001858var _ sdkLibraryComponentNamingScheme = (*defaultNamingScheme)(nil)
1859
Jaewoong Jungbc15e3a2021-03-10 17:02:43 -08001860func moduleStubLinkType(name string) (stub bool, ret sdkLinkType) {
Anton Hansson2d0c1942020-05-25 12:20:51 +01001861 // This suffix-based approach is fragile and could potentially mis-trigger.
1862 // TODO(b/155164730): Clean this up when modules no longer reference sdk_lib stubs directly.
Anton Hansson08f476b2021-04-07 15:32:19 +01001863 if strings.HasSuffix(name, apiScopePublic.stubsLibraryModuleNameSuffix()) {
1864 if name == "hwbinder.stubs" || name == "libcore_private.stubs" {
1865 // Due to a previous bug, these modules were not considered stubs, so we retain that.
1866 return false, javaPlatform
1867 }
Anton Hansson2d0c1942020-05-25 12:20:51 +01001868 return true, javaSdk
1869 }
Anton Hansson08f476b2021-04-07 15:32:19 +01001870 if strings.HasSuffix(name, apiScopeSystem.stubsLibraryModuleNameSuffix()) {
Anton Hansson2d0c1942020-05-25 12:20:51 +01001871 return true, javaSystem
1872 }
Anton Hansson08f476b2021-04-07 15:32:19 +01001873 if strings.HasSuffix(name, apiScopeModuleLib.stubsLibraryModuleNameSuffix()) {
Anton Hansson2d0c1942020-05-25 12:20:51 +01001874 return true, javaModule
1875 }
Anton Hansson08f476b2021-04-07 15:32:19 +01001876 if strings.HasSuffix(name, apiScopeTest.stubsLibraryModuleNameSuffix()) {
Anton Hansson2d0c1942020-05-25 12:20:51 +01001877 return true, javaSystem
1878 }
1879 return false, javaPlatform
1880}
1881
Jaewoong Jung4f158ee2019-07-11 10:05:35 -07001882// java_sdk_library is a special Java library that provides optional platform APIs to apps.
1883// In practice, it can be viewed as a combination of several modules: 1) stubs library that clients
1884// are linked against to, 2) droiddoc module that internally generates API stubs source files,
1885// 3) the real runtime shared library that implements the APIs, and 4) XML file for adding
1886// the runtime lib to the classpath at runtime if requested via <uses-library>.
Inseob Kimc0907f12019-02-08 21:00:45 +09001887func SdkLibraryFactory() android.Module {
1888 module := &SdkLibrary{}
Paul Duffinc3091c82020-05-08 14:16:20 +01001889
1890 // Initialize information common between source and prebuilt.
Paul Duffin71b33cc2021-06-23 11:39:47 +01001891 module.initCommon(module)
Paul Duffinc3091c82020-05-08 14:16:20 +01001892
Inseob Kimc0907f12019-02-08 21:00:45 +09001893 module.InitSdkLibraryProperties()
Jooyung Han58f26ab2019-12-18 15:34:32 +09001894 android.InitApexModule(module)
Paul Duffinb6b89a42021-05-06 16:33:43 +01001895 android.InitSdkAwareModule(module)
Sundong Ahn054b19a2018-10-19 13:46:09 +09001896 InitJavaModule(module, android.HostAndDeviceSupported)
Paul Duffin3375e352020-04-28 10:44:03 +01001897
1898 // Initialize the map from scope to scope specific properties.
1899 scopeToProperties := make(map[*apiScope]*ApiScopeProperties)
1900 for _, scope := range allApiScopes {
1901 scopeToProperties[scope] = scope.scopeSpecificProperties(module)
1902 }
1903 module.scopeToProperties = scopeToProperties
1904
Paul Duffin4911a892020-04-29 23:35:13 +01001905 // Add the properties containing visibility rules so that they are checked.
Paul Duffin5df79302020-05-16 15:52:12 +01001906 android.AddVisibilityProperty(module, "impl_library_visibility", &module.sdkLibraryProperties.Impl_library_visibility)
Paul Duffin4911a892020-04-29 23:35:13 +01001907 android.AddVisibilityProperty(module, "stubs_library_visibility", &module.sdkLibraryProperties.Stubs_library_visibility)
1908 android.AddVisibilityProperty(module, "stubs_source_visibility", &module.sdkLibraryProperties.Stubs_source_visibility)
1909
Paul Duffin1b1e8062020-05-08 13:44:43 +01001910 module.SetDefaultableHook(func(ctx android.DefaultableHookContext) {
Paul Duffindfa131e2020-05-15 20:37:11 +01001911 // If no implementation is required then it cannot be used as a shared library
1912 // either.
1913 if !module.requiresRuntimeImplementationLibrary() {
1914 // If shared_library has been explicitly set to true then it is incompatible
1915 // with api_only: true.
1916 if proptools.Bool(module.commonSdkLibraryProperties.Shared_library) {
1917 ctx.PropertyErrorf("api_only/shared_library", "inconsistent settings, shared_library and api_only cannot both be true")
1918 }
1919 // Set shared_library: false.
1920 module.commonSdkLibraryProperties.Shared_library = proptools.BoolPtr(false)
1921 }
1922
Paul Duffin1b1e8062020-05-08 13:44:43 +01001923 if module.initCommonAfterDefaultsApplied(ctx) {
1924 module.CreateInternalModules(ctx)
1925 }
1926 })
Jiyong Parkc678ad32018-04-10 13:07:10 +09001927 return module
1928}
Colin Cross79c7c262019-04-17 11:11:46 -07001929
1930//
1931// SDK library prebuilts
1932//
1933
Paul Duffin56d44902020-01-31 13:36:25 +00001934// Properties associated with each api scope.
1935type sdkLibraryScopeProperties struct {
Colin Cross79c7c262019-04-17 11:11:46 -07001936 Jars []string `android:"path"`
1937
1938 Sdk_version *string
1939
Colin Cross79c7c262019-04-17 11:11:46 -07001940 // List of shared java libs that this module has dependencies to
1941 Libs []string
Paul Duffin3d1248c2020-04-09 00:10:17 +01001942
Paul Duffinc8782502020-04-29 20:45:27 +01001943 // The stubs source.
Paul Duffin3d1248c2020-04-09 00:10:17 +01001944 Stub_srcs []string `android:"path"`
Paul Duffin1fd005d2020-04-09 01:08:11 +01001945
1946 // The current.txt
Paul Duffin0f8faff2020-05-20 16:18:00 +01001947 Current_api *string `android:"path"`
Paul Duffin1fd005d2020-04-09 01:08:11 +01001948
1949 // The removed.txt
Paul Duffin0f8faff2020-05-20 16:18:00 +01001950 Removed_api *string `android:"path"`
Anton Hanssond78eb762021-09-21 15:25:12 +01001951
1952 // Annotation zip
1953 Annotations *string `android:"path"`
Colin Cross79c7c262019-04-17 11:11:46 -07001954}
1955
Paul Duffin56d44902020-01-31 13:36:25 +00001956type sdkLibraryImportProperties struct {
Paul Duffinfcfd7912020-01-31 17:54:30 +00001957 // List of shared java libs, common to all scopes, that this module has
1958 // dependencies to
1959 Libs []string
Paul Duffin1267d872021-04-16 17:21:36 +01001960
1961 // If set to true, compile dex files for the stubs. Defaults to false.
1962 Compile_dex *bool
Paul Duffin869de142021-07-15 14:14:41 +01001963
1964 // If not empty, classes are restricted to the specified packages and their sub-packages.
Paul Duffin869de142021-07-15 14:14:41 +01001965 Permitted_packages []string
Paul Duffin56d44902020-01-31 13:36:25 +00001966}
1967
Paul Duffineedc5d52020-06-12 17:46:39 +01001968type SdkLibraryImport struct {
Colin Cross79c7c262019-04-17 11:11:46 -07001969 android.ModuleBase
1970 android.DefaultableModuleBase
1971 prebuilt android.Prebuilt
Paul Duffindd46f712020-02-10 13:37:10 +00001972 android.ApexModuleBase
1973 android.SdkBase
Colin Cross79c7c262019-04-17 11:11:46 -07001974
Paul Duffin37856732021-02-26 14:24:15 +00001975 hiddenAPI
Jiakai Zhang204356f2021-09-09 08:12:46 +00001976 dexpreopter
Paul Duffin37856732021-02-26 14:24:15 +00001977
Colin Cross79c7c262019-04-17 11:11:46 -07001978 properties sdkLibraryImportProperties
1979
Paul Duffin46a26a82020-04-07 19:27:04 +01001980 // Map from api scope to the scope specific property structure.
1981 scopeProperties map[*apiScope]*sdkLibraryScopeProperties
1982
Paul Duffin56d44902020-01-31 13:36:25 +00001983 commonToSdkLibraryAndImport
Paul Duffineedc5d52020-06-12 17:46:39 +01001984
1985 // The reference to the implementation library created by the source module.
1986 // Is nil if the source module does not exist.
1987 implLibraryModule *Library
1988
1989 // The reference to the xml permissions module created by the source module.
1990 // Is nil if the source module does not exist.
1991 xmlPermissionsFileModule *sdkLibraryXml
Paul Duffin39853512021-02-26 11:09:39 +00001992
Jeongik Chad5fe8782021-07-08 01:13:11 +09001993 // Build path to the dex implementation jar obtained from the prebuilt_apex, if any.
Martin Stjernholm8be1e6d2021-09-15 03:34:04 +01001994 dexJarFile OptionalDexJarPath
Jeongik Chad5fe8782021-07-08 01:13:11 +09001995
1996 // Expected install file path of the source module(sdk_library)
1997 // or dex implementation jar obtained from the prebuilt_apex, if any.
1998 installFile android.Path
Colin Cross79c7c262019-04-17 11:11:46 -07001999}
2000
Paul Duffineedc5d52020-06-12 17:46:39 +01002001var _ SdkLibraryDependency = (*SdkLibraryImport)(nil)
Colin Cross79c7c262019-04-17 11:11:46 -07002002
Paul Duffin46a26a82020-04-07 19:27:04 +01002003// The type of a structure that contains a field of type sdkLibraryScopeProperties
2004// for each apiscope in allApiScopes, e.g. something like:
2005// struct {
2006// Public sdkLibraryScopeProperties
2007// System sdkLibraryScopeProperties
2008// ...
2009// }
2010var allScopeStructType = createAllScopePropertiesStructType()
2011
2012// Dynamically create a structure type for each apiscope in allApiScopes.
2013func createAllScopePropertiesStructType() reflect.Type {
2014 var fields []reflect.StructField
2015 for _, apiScope := range allApiScopes {
2016 field := reflect.StructField{
2017 Name: apiScope.fieldName,
2018 Type: reflect.TypeOf(sdkLibraryScopeProperties{}),
2019 }
2020 fields = append(fields, field)
2021 }
2022
2023 return reflect.StructOf(fields)
2024}
2025
2026// Create an instance of the scope specific structure type and return a map
2027// from apiscope to a pointer to each scope specific field.
2028func createPropertiesInstance() (interface{}, map[*apiScope]*sdkLibraryScopeProperties) {
2029 allScopePropertiesPtr := reflect.New(allScopeStructType)
2030 allScopePropertiesStruct := allScopePropertiesPtr.Elem()
2031 scopeProperties := make(map[*apiScope]*sdkLibraryScopeProperties)
2032
2033 for _, apiScope := range allApiScopes {
2034 field := allScopePropertiesStruct.FieldByName(apiScope.fieldName)
2035 scopeProperties[apiScope] = field.Addr().Interface().(*sdkLibraryScopeProperties)
2036 }
2037
2038 return allScopePropertiesPtr.Interface(), scopeProperties
2039}
2040
Jaewoong Jung4f158ee2019-07-11 10:05:35 -07002041// java_sdk_library_import imports a prebuilt java_sdk_library.
Colin Cross79c7c262019-04-17 11:11:46 -07002042func sdkLibraryImportFactory() android.Module {
Paul Duffineedc5d52020-06-12 17:46:39 +01002043 module := &SdkLibraryImport{}
Colin Cross79c7c262019-04-17 11:11:46 -07002044
Paul Duffin46a26a82020-04-07 19:27:04 +01002045 allScopeProperties, scopeToProperties := createPropertiesInstance()
2046 module.scopeProperties = scopeToProperties
2047 module.AddProperties(&module.properties, allScopeProperties)
Colin Cross79c7c262019-04-17 11:11:46 -07002048
Paul Duffinc3091c82020-05-08 14:16:20 +01002049 // Initialize information common between source and prebuilt.
Paul Duffin71b33cc2021-06-23 11:39:47 +01002050 module.initCommon(module)
Paul Duffinc3091c82020-05-08 14:16:20 +01002051
Paul Duffin0bdcb272020-02-06 15:24:57 +00002052 android.InitPrebuiltModule(module, &[]string{""})
Paul Duffindd46f712020-02-10 13:37:10 +00002053 android.InitApexModule(module)
2054 android.InitSdkAwareModule(module)
Colin Cross79c7c262019-04-17 11:11:46 -07002055 InitJavaModule(module, android.HostAndDeviceSupported)
2056
Paul Duffin1b1e8062020-05-08 13:44:43 +01002057 module.SetDefaultableHook(func(mctx android.DefaultableHookContext) {
2058 if module.initCommonAfterDefaultsApplied(mctx) {
2059 module.createInternalModules(mctx)
2060 }
2061 })
Colin Cross79c7c262019-04-17 11:11:46 -07002062 return module
2063}
2064
Paul Duffin630b11e2021-07-15 13:35:26 +01002065var _ PermittedPackagesForUpdatableBootJars = (*SdkLibraryImport)(nil)
2066
2067func (module *SdkLibraryImport) PermittedPackagesForUpdatableBootJars() []string {
2068 return module.properties.Permitted_packages
2069}
2070
Paul Duffineedc5d52020-06-12 17:46:39 +01002071func (module *SdkLibraryImport) Prebuilt() *android.Prebuilt {
Colin Cross79c7c262019-04-17 11:11:46 -07002072 return &module.prebuilt
2073}
2074
Paul Duffineedc5d52020-06-12 17:46:39 +01002075func (module *SdkLibraryImport) Name() string {
Colin Cross79c7c262019-04-17 11:11:46 -07002076 return module.prebuilt.Name(module.ModuleBase.Name())
2077}
2078
Paul Duffineedc5d52020-06-12 17:46:39 +01002079func (module *SdkLibraryImport) createInternalModules(mctx android.DefaultableHookContext) {
Colin Cross79c7c262019-04-17 11:11:46 -07002080
Paul Duffin50061512020-01-21 16:31:05 +00002081 // If the build is configured to use prebuilts then force this to be preferred.
Jeongik Cha816a23a2020-07-08 01:09:23 +09002082 if mctx.Config().AlwaysUsePrebuiltSdks() {
Paul Duffin50061512020-01-21 16:31:05 +00002083 module.prebuilt.ForcePrefer()
2084 }
2085
Paul Duffin46a26a82020-04-07 19:27:04 +01002086 for apiScope, scopeProperties := range module.scopeProperties {
Paul Duffin56d44902020-01-31 13:36:25 +00002087 if len(scopeProperties.Jars) == 0 {
2088 continue
2089 }
2090
Paul Duffinbbb546b2020-04-09 00:07:11 +01002091 module.createJavaImportForStubs(mctx, apiScope, scopeProperties)
Paul Duffin3d1248c2020-04-09 00:10:17 +01002092
Paul Duffin0f8faff2020-05-20 16:18:00 +01002093 if len(scopeProperties.Stub_srcs) > 0 {
2094 module.createPrebuiltStubsSources(mctx, apiScope, scopeProperties)
2095 }
Paul Duffin56d44902020-01-31 13:36:25 +00002096 }
Colin Cross79c7c262019-04-17 11:11:46 -07002097
2098 javaSdkLibraries := javaSdkLibraries(mctx.Config())
2099 javaSdkLibrariesLock.Lock()
2100 defer javaSdkLibrariesLock.Unlock()
2101 *javaSdkLibraries = append(*javaSdkLibraries, module.BaseModuleName())
2102}
2103
Paul Duffineedc5d52020-06-12 17:46:39 +01002104func (module *SdkLibraryImport) createJavaImportForStubs(mctx android.DefaultableHookContext, apiScope *apiScope, scopeProperties *sdkLibraryScopeProperties) {
Paul Duffinbbb546b2020-04-09 00:07:11 +01002105 // Creates a java import for the jar with ".stubs" suffix
2106 props := struct {
Paul Duffin1dbe3ca2020-05-16 09:57:59 +01002107 Name *string
2108 Sdk_version *string
2109 Libs []string
2110 Jars []string
2111 Prefer *bool
Paul Duffin1267d872021-04-16 17:21:36 +01002112 Compile_dex *bool
Paul Duffinbbb546b2020-04-09 00:07:11 +01002113 }{}
Paul Duffinc3091c82020-05-08 14:16:20 +01002114 props.Name = proptools.StringPtr(module.stubsLibraryModuleName(apiScope))
Paul Duffinbbb546b2020-04-09 00:07:11 +01002115 props.Sdk_version = scopeProperties.Sdk_version
2116 // Prepend any of the libs from the legacy public properties to the libs for each of the
2117 // scopes to avoid having to duplicate them in each scope.
2118 props.Libs = append(module.properties.Libs, scopeProperties.Libs...)
2119 props.Jars = scopeProperties.Jars
Paul Duffin1dbe3ca2020-05-16 09:57:59 +01002120
Paul Duffin38b57852020-05-13 16:08:09 +01002121 // The imports are preferred if the java_sdk_library_import is preferred.
2122 props.Prefer = proptools.BoolPtr(module.prebuilt.Prefer())
Paul Duffin859fe962020-05-15 10:20:31 +01002123
Paul Duffin1267d872021-04-16 17:21:36 +01002124 // The imports need to be compiled to dex if the java_sdk_library_import requests it.
Paul Duffinf4600f62021-05-13 22:34:45 +01002125 compileDex := module.properties.Compile_dex
2126 if module.stubLibrariesCompiledForDex() {
2127 compileDex = proptools.BoolPtr(true)
2128 }
2129 props.Compile_dex = compileDex
Paul Duffin1267d872021-04-16 17:21:36 +01002130
Paul Duffin859fe962020-05-15 10:20:31 +01002131 mctx.CreateModule(ImportFactory, &props, module.sdkComponentPropertiesForChildLibrary())
Paul Duffinbbb546b2020-04-09 00:07:11 +01002132}
2133
Paul Duffineedc5d52020-06-12 17:46:39 +01002134func (module *SdkLibraryImport) createPrebuiltStubsSources(mctx android.DefaultableHookContext, apiScope *apiScope, scopeProperties *sdkLibraryScopeProperties) {
Paul Duffin3d1248c2020-04-09 00:10:17 +01002135 props := struct {
Paul Duffin38b57852020-05-13 16:08:09 +01002136 Name *string
2137 Srcs []string
2138 Prefer *bool
Paul Duffin3d1248c2020-04-09 00:10:17 +01002139 }{}
Paul Duffinc3091c82020-05-08 14:16:20 +01002140 props.Name = proptools.StringPtr(module.stubsSourceModuleName(apiScope))
Paul Duffin3d1248c2020-04-09 00:10:17 +01002141 props.Srcs = scopeProperties.Stub_srcs
2142 mctx.CreateModule(PrebuiltStubsSourcesFactory, &props)
Paul Duffin38b57852020-05-13 16:08:09 +01002143
2144 // The stubs source is preferred if the java_sdk_library_import is preferred.
2145 props.Prefer = proptools.BoolPtr(module.prebuilt.Prefer())
Paul Duffin3d1248c2020-04-09 00:10:17 +01002146}
2147
Paul Duffin44f1d842020-06-26 20:17:02 +01002148// Add the dependencies on the child module in the component deps mutator so that it
2149// creates references to the prebuilt and not the source modules.
2150func (module *SdkLibraryImport) ComponentDepsMutator(ctx android.BottomUpMutatorContext) {
Paul Duffin46a26a82020-04-07 19:27:04 +01002151 for apiScope, scopeProperties := range module.scopeProperties {
Paul Duffin56d44902020-01-31 13:36:25 +00002152 if len(scopeProperties.Jars) == 0 {
2153 continue
2154 }
2155
2156 // Add dependencies to the prebuilt stubs library
Paul Duffin864116c2021-04-02 10:24:13 +01002157 ctx.AddVariationDependencies(nil, apiScope.stubsTag, android.PrebuiltNameFromSource(module.stubsLibraryModuleName(apiScope)))
Paul Duffin0f8faff2020-05-20 16:18:00 +01002158
2159 if len(scopeProperties.Stub_srcs) > 0 {
2160 // Add dependencies to the prebuilt stubs source library
Paul Duffin864116c2021-04-02 10:24:13 +01002161 ctx.AddVariationDependencies(nil, apiScope.stubsSourceTag, android.PrebuiltNameFromSource(module.stubsSourceModuleName(apiScope)))
Paul Duffin0f8faff2020-05-20 16:18:00 +01002162 }
Paul Duffin56d44902020-01-31 13:36:25 +00002163 }
Paul Duffin44f1d842020-06-26 20:17:02 +01002164}
2165
2166// Add other dependencies as normal.
2167func (module *SdkLibraryImport) DepsMutator(ctx android.BottomUpMutatorContext) {
Paul Duffineedc5d52020-06-12 17:46:39 +01002168
2169 implName := module.implLibraryModuleName()
2170 if ctx.OtherModuleExists(implName) {
2171 ctx.AddVariationDependencies(nil, implLibraryTag, implName)
2172
2173 xmlPermissionsModuleName := module.xmlPermissionsModuleName()
2174 if module.sharedLibrary() && ctx.OtherModuleExists(xmlPermissionsModuleName) {
2175 // Add dependency to the rule for generating the xml permissions file
2176 ctx.AddDependency(module, xmlPermissionsFileTag, xmlPermissionsModuleName)
2177 }
2178 }
Colin Cross79c7c262019-04-17 11:11:46 -07002179}
2180
Jiakai Zhang204356f2021-09-09 08:12:46 +00002181func (module *SdkLibraryImport) AndroidMkEntries() []android.AndroidMkEntries {
2182 // For an SDK library imported from a prebuilt APEX, we don't need a Make module for itself, as we
2183 // don't need to install it. However, we need to add its dexpreopt outputs as sub-modules, if it
2184 // is preopted.
2185 dexpreoptEntries := module.dexpreopter.AndroidMkEntriesForApex()
2186 return append(dexpreoptEntries, android.AndroidMkEntries{Disabled: true})
2187}
2188
Jiyong Park45bf82e2020-12-15 22:29:02 +09002189var _ android.ApexModule = (*SdkLibraryImport)(nil)
2190
2191// Implements android.ApexModule
Paul Duffineedc5d52020-06-12 17:46:39 +01002192func (module *SdkLibraryImport) DepIsInSameApex(mctx android.BaseModuleContext, dep android.Module) bool {
2193 depTag := mctx.OtherModuleDependencyTag(dep)
2194 if depTag == xmlPermissionsFileTag {
2195 return true
2196 }
2197
2198 // None of the other dependencies of the java_sdk_library_import are in the same apex
2199 // as the one that references this module.
2200 return false
2201}
2202
Jiyong Park45bf82e2020-12-15 22:29:02 +09002203// Implements android.ApexModule
Dan Albertc8060532020-07-22 22:32:17 -07002204func (module *SdkLibraryImport) ShouldSupportSdkVersion(ctx android.BaseModuleContext,
2205 sdkVersion android.ApiLevel) error {
Jooyung Han749dc692020-04-15 11:03:39 +09002206 // we don't check prebuilt modules for sdk_version
2207 return nil
2208}
2209
Paul Duffinea8f8082021-06-24 13:25:57 +01002210// Implements android.ApexModule
2211func (module *SdkLibraryImport) UniqueApexVariations() bool {
2212 return module.uniqueApexVariations()
2213}
2214
Paul Duffineedc5d52020-06-12 17:46:39 +01002215func (module *SdkLibraryImport) OutputFiles(tag string) (android.Paths, error) {
Paul Duffin46dc45a2020-05-14 15:39:10 +01002216 return module.commonOutputFiles(tag)
2217}
2218
Paul Duffineedc5d52020-06-12 17:46:39 +01002219func (module *SdkLibraryImport) GenerateAndroidBuildActions(ctx android.ModuleContext) {
Paul Duffina2ae7e02020-09-11 11:55:00 +01002220 module.generateCommonBuildActions(ctx)
2221
Jeongik Chad5fe8782021-07-08 01:13:11 +09002222 // Assume that source module(sdk_library) is installed in /<sdk_library partition>/framework
2223 module.installFile = android.PathForModuleInstall(ctx, "framework", module.Stem()+".jar")
2224
Paul Duffin0f8faff2020-05-20 16:18:00 +01002225 // Record the paths to the prebuilt stubs library and stubs source.
Colin Cross79c7c262019-04-17 11:11:46 -07002226 ctx.VisitDirectDeps(func(to android.Module) {
2227 tag := ctx.OtherModuleDependencyTag(to)
2228
Paul Duffin0f8faff2020-05-20 16:18:00 +01002229 // Extract information from any of the scope specific dependencies.
2230 if scopeTag, ok := tag.(scopeDependencyTag); ok {
2231 apiScope := scopeTag.apiScope
2232 scopePaths := module.getScopePathsCreateIfNeeded(apiScope)
2233
2234 // Extract information from the dependency. The exact information extracted
2235 // is determined by the nature of the dependency which is determined by the tag.
2236 scopeTag.extractDepInfo(ctx, to, scopePaths)
Paul Duffineedc5d52020-06-12 17:46:39 +01002237 } else if tag == implLibraryTag {
2238 if implLibrary, ok := to.(*Library); ok {
2239 module.implLibraryModule = implLibrary
2240 } else {
2241 ctx.ModuleErrorf("implementation library must be of type *java.Library but was %T", to)
2242 }
2243 } else if tag == xmlPermissionsFileTag {
2244 if xmlPermissionsFileModule, ok := to.(*sdkLibraryXml); ok {
2245 module.xmlPermissionsFileModule = xmlPermissionsFileModule
2246 } else {
2247 ctx.ModuleErrorf("xml permissions file module must be of type *sdkLibraryXml but was %T", to)
2248 }
Colin Cross79c7c262019-04-17 11:11:46 -07002249 }
2250 })
Paul Duffin0f8faff2020-05-20 16:18:00 +01002251
2252 // Populate the scope paths with information from the properties.
2253 for apiScope, scopeProperties := range module.scopeProperties {
2254 if len(scopeProperties.Jars) == 0 {
2255 continue
2256 }
2257
2258 paths := module.getScopePathsCreateIfNeeded(apiScope)
Anton Hanssond78eb762021-09-21 15:25:12 +01002259 paths.annotationsZip = android.OptionalPathForModuleSrc(ctx, scopeProperties.Annotations)
Paul Duffin0f8faff2020-05-20 16:18:00 +01002260 paths.currentApiFilePath = android.OptionalPathForModuleSrc(ctx, scopeProperties.Current_api)
2261 paths.removedApiFilePath = android.OptionalPathForModuleSrc(ctx, scopeProperties.Removed_api)
2262 }
Paul Duffin39853512021-02-26 11:09:39 +00002263
2264 if ctx.Device() {
2265 // If this is a variant created for a prebuilt_apex then use the dex implementation jar
2266 // obtained from the associated deapexer module.
2267 ai := ctx.Provider(android.ApexInfoProvider).(android.ApexInfo)
2268 if ai.ForPrebuiltApex {
Paul Duffin39853512021-02-26 11:09:39 +00002269 // Get the path of the dex implementation jar from the `deapexer` module.
Martin Stjernholm44825602021-09-17 01:44:12 +01002270 di := android.FindDeapexerProviderForModule(ctx)
2271 if di == nil {
2272 return // An error has been reported by FindDeapexerProviderForModule.
2273 }
Paul Duffinb4bbf2c2021-06-17 15:59:07 +01002274 if dexOutputPath := di.PrebuiltExportPath(apexRootRelativePathToJavaLib(module.BaseModuleName())); dexOutputPath != nil {
Martin Stjernholm8be1e6d2021-09-15 03:34:04 +01002275 dexJarFile := makeDexJarPathFromPath(dexOutputPath)
2276 module.dexJarFile = dexJarFile
Jiakai Zhang204356f2021-09-09 08:12:46 +00002277 installPath := android.PathForModuleInPartitionInstall(
2278 ctx, "apex", ai.ApexVariationName, apexRootRelativePathToJavaLib(module.BaseModuleName()))
2279 module.installFile = installPath
Martin Stjernholm8be1e6d2021-09-15 03:34:04 +01002280 module.initHiddenAPI(ctx, dexJarFile, module.findScopePaths(apiScopePublic).stubsImplPath[0], nil)
Jiakai Zhang204356f2021-09-09 08:12:46 +00002281
2282 // Dexpreopting.
2283 module.dexpreopter.installPath = module.dexpreopter.getInstallPath(ctx, installPath)
2284 module.dexpreopter.isSDKLibrary = true
2285 module.dexpreopter.uncompressedDex = shouldUncompressDex(ctx, &module.dexpreopter)
2286 module.dexpreopt(ctx, dexOutputPath)
Paul Duffin39853512021-02-26 11:09:39 +00002287 } else {
2288 // This should never happen as a variant for a prebuilt_apex is only created if the
2289 // prebuilt_apex has been configured to export the java library dex file.
Martin Stjernholm44825602021-09-17 01:44:12 +01002290 ctx.ModuleErrorf("internal error: no dex implementation jar available from prebuilt APEX %s", di.ApexModuleName())
Paul Duffin39853512021-02-26 11:09:39 +00002291 }
2292 }
2293 }
Colin Cross79c7c262019-04-17 11:11:46 -07002294}
2295
Jiyong Parkf1691d22021-03-29 20:11:58 +09002296func (module *SdkLibraryImport) sdkJars(ctx android.BaseModuleContext, sdkVersion android.SdkSpec, headerJars bool) android.Paths {
Paul Duffineedc5d52020-06-12 17:46:39 +01002297
2298 // For consistency with SdkLibrary make the implementation jar available to libraries that
2299 // are within the same APEX.
2300 implLibraryModule := module.implLibraryModule
Colin Cross56a83212020-09-15 18:30:11 -07002301 if implLibraryModule != nil && withinSameApexesAs(ctx, module) {
Paul Duffineedc5d52020-06-12 17:46:39 +01002302 if headerJars {
2303 return implLibraryModule.HeaderJars()
2304 } else {
2305 return implLibraryModule.ImplementationJars()
2306 }
2307 }
2308
Paul Duffin23970f42020-05-20 14:20:02 +01002309 return module.selectHeaderJarsForSdkVersion(ctx, sdkVersion)
Paul Duffin56d44902020-01-31 13:36:25 +00002310}
2311
Colin Cross79c7c262019-04-17 11:11:46 -07002312// to satisfy SdkLibraryDependency interface
Jiyong Parkf1691d22021-03-29 20:11:58 +09002313func (module *SdkLibraryImport) SdkHeaderJars(ctx android.BaseModuleContext, sdkVersion android.SdkSpec) android.Paths {
Colin Cross79c7c262019-04-17 11:11:46 -07002314 // This module is just a wrapper for the prebuilt stubs.
Paul Duffineedc5d52020-06-12 17:46:39 +01002315 return module.sdkJars(ctx, sdkVersion, true)
Colin Cross79c7c262019-04-17 11:11:46 -07002316}
2317
2318// to satisfy SdkLibraryDependency interface
Jiyong Parkf1691d22021-03-29 20:11:58 +09002319func (module *SdkLibraryImport) SdkImplementationJars(ctx android.BaseModuleContext, sdkVersion android.SdkSpec) android.Paths {
Colin Cross79c7c262019-04-17 11:11:46 -07002320 // This module is just a wrapper for the stubs.
Paul Duffineedc5d52020-06-12 17:46:39 +01002321 return module.sdkJars(ctx, sdkVersion, false)
2322}
2323
Ulya Trafimovichdbf31662020-12-17 12:07:54 +00002324// to satisfy UsesLibraryDependency interface
Martin Stjernholm8be1e6d2021-09-15 03:34:04 +01002325func (module *SdkLibraryImport) DexJarBuildPath() OptionalDexJarPath {
Paul Duffin39853512021-02-26 11:09:39 +00002326 // The dex implementation jar extracted from the .apex file should be used in preference to the
2327 // source.
Martin Stjernholm8be1e6d2021-09-15 03:34:04 +01002328 if module.dexJarFile.IsSet() {
Paul Duffin39853512021-02-26 11:09:39 +00002329 return module.dexJarFile
2330 }
Paul Duffineedc5d52020-06-12 17:46:39 +01002331 if module.implLibraryModule == nil {
Martin Stjernholm8be1e6d2021-09-15 03:34:04 +01002332 return makeUnsetDexJarPath()
Paul Duffineedc5d52020-06-12 17:46:39 +01002333 } else {
2334 return module.implLibraryModule.DexJarBuildPath()
2335 }
2336}
2337
Ulya Trafimovichdbf31662020-12-17 12:07:54 +00002338// to satisfy UsesLibraryDependency interface
Ulya Trafimovich31e444e2020-08-14 17:32:16 +01002339func (module *SdkLibraryImport) DexJarInstallPath() android.Path {
Jeongik Chad5fe8782021-07-08 01:13:11 +09002340 return module.installFile
Ulya Trafimovich31e444e2020-08-14 17:32:16 +01002341}
2342
Ulya Trafimovichdbf31662020-12-17 12:07:54 +00002343// to satisfy UsesLibraryDependency interface
2344func (module *SdkLibraryImport) ClassLoaderContexts() dexpreopt.ClassLoaderContextMap {
2345 return nil
2346}
2347
Paul Duffineedc5d52020-06-12 17:46:39 +01002348// to satisfy apex.javaDependency interface
2349func (module *SdkLibraryImport) JacocoReportClassesFile() android.Path {
2350 if module.implLibraryModule == nil {
2351 return nil
2352 } else {
2353 return module.implLibraryModule.JacocoReportClassesFile()
2354 }
2355}
2356
2357// to satisfy apex.javaDependency interface
Colin Cross08dca382020-07-21 20:31:17 -07002358func (module *SdkLibraryImport) LintDepSets() LintDepSets {
2359 if module.implLibraryModule == nil {
2360 return LintDepSets{}
2361 } else {
2362 return module.implLibraryModule.LintDepSets()
2363 }
2364}
2365
Jaewoong Jung476b9d62021-05-10 15:30:00 -07002366func (module *SdkLibraryImport) getStrictUpdatabilityLinting() bool {
2367 if module.implLibraryModule == nil {
2368 return false
2369 } else {
2370 return module.implLibraryModule.getStrictUpdatabilityLinting()
2371 }
2372}
2373
2374func (module *SdkLibraryImport) setStrictUpdatabilityLinting(strictLinting bool) {
2375 if module.implLibraryModule != nil {
2376 module.implLibraryModule.setStrictUpdatabilityLinting(strictLinting)
2377 }
2378}
2379
Colin Cross08dca382020-07-21 20:31:17 -07002380// to satisfy apex.javaDependency interface
Paul Duffineedc5d52020-06-12 17:46:39 +01002381func (module *SdkLibraryImport) Stem() string {
2382 return module.BaseModuleName()
Colin Cross79c7c262019-04-17 11:11:46 -07002383}
Jiyong Parke3833882020-02-17 17:28:10 +09002384
Paul Duffin44b481b2020-06-17 16:59:43 +01002385var _ ApexDependency = (*SdkLibraryImport)(nil)
2386
2387// to satisfy java.ApexDependency interface
2388func (module *SdkLibraryImport) HeaderJars() android.Paths {
2389 if module.implLibraryModule == nil {
2390 return nil
2391 } else {
2392 return module.implLibraryModule.HeaderJars()
2393 }
2394}
2395
2396// to satisfy java.ApexDependency interface
2397func (module *SdkLibraryImport) ImplementationAndResourcesJars() android.Paths {
2398 if module.implLibraryModule == nil {
2399 return nil
2400 } else {
2401 return module.implLibraryModule.ImplementationAndResourcesJars()
2402 }
2403}
2404
Jiakai Zhang204356f2021-09-09 08:12:46 +00002405// to satisfy java.DexpreopterInterface interface
2406func (module *SdkLibraryImport) IsInstallable() bool {
2407 return true
2408}
2409
Paul Duffinfef55002021-06-17 14:56:05 +01002410var _ android.RequiredFilesFromPrebuiltApex = (*SdkLibraryImport)(nil)
2411
Paul Duffinb4bbf2c2021-06-17 15:59:07 +01002412func (module *SdkLibraryImport) RequiredFilesFromPrebuiltApex(ctx android.BaseModuleContext) []string {
Paul Duffinfef55002021-06-17 14:56:05 +01002413 name := module.BaseModuleName()
2414 return requiredFilesFromPrebuiltApexForImport(name)
2415}
2416
Jiyong Parke3833882020-02-17 17:28:10 +09002417//
2418// java_sdk_library_xml
2419//
2420type sdkLibraryXml struct {
2421 android.ModuleBase
2422 android.DefaultableModuleBase
2423 android.ApexModuleBase
2424
2425 properties sdkLibraryXmlProperties
2426
2427 outputFilePath android.OutputPath
2428 installDirPath android.InstallPath
Colin Cross56a83212020-09-15 18:30:11 -07002429
2430 hideApexVariantFromMake bool
Jiyong Parke3833882020-02-17 17:28:10 +09002431}
2432
2433type sdkLibraryXmlProperties struct {
2434 // canonical name of the lib
2435 Lib_name *string
Pedro Loureiro9956e5e2021-09-07 17:21:59 +00002436
2437 // Signals that this shared library is part of the bootclasspath starting
2438 // on the version indicated in this attribute.
2439 //
2440 // This will make platforms at this level and above to ignore
2441 // <uses-library> tags with this library name because the library is already
2442 // available
2443 On_bootclasspath_since *string
2444
2445 // Signals that this shared library was part of the bootclasspath before
2446 // (but not including) the version indicated in this attribute.
2447 //
2448 // The system will automatically add a <uses-library> tag with this library to
2449 // apps that target any SDK less than the version indicated in this attribute.
2450 On_bootclasspath_before *string
2451
2452 // Indicates that PackageManager should ignore this shared library if the
2453 // platform is below the version indicated in this attribute.
2454 //
2455 // This means that the device won't recognise this library as installed.
2456 Min_device_sdk *string
2457
2458 // Indicates that PackageManager should ignore this shared library if the
2459 // platform is above the version indicated in this attribute.
2460 //
2461 // This means that the device won't recognise this library as installed.
2462 Max_device_sdk *string
Pedro Loureiroc3621422021-09-28 15:40:23 +00002463
2464 // The SdkLibrary's min api level as a string
2465 //
2466 // This value comes from the ApiLevel of the MinSdkVersion property.
2467 Sdk_library_min_api_level *string
Jiyong Parke3833882020-02-17 17:28:10 +09002468}
2469
2470// java_sdk_library_xml builds the permission xml file for a java_sdk_library.
2471// Not to be used directly by users. java_sdk_library internally uses this.
2472func sdkLibraryXmlFactory() android.Module {
2473 module := &sdkLibraryXml{}
2474
2475 module.AddProperties(&module.properties)
2476
2477 android.InitApexModule(module)
2478 android.InitAndroidArchModule(module, android.DeviceSupported, android.MultilibCommon)
2479
2480 return module
2481}
2482
Colin Crossaede88c2020-08-11 12:17:01 -07002483func (module *sdkLibraryXml) UniqueApexVariations() bool {
2484 // sdkLibraryXml needs a unique variation per APEX because the generated XML file contains the path to the
2485 // mounted APEX, which contains the name of the APEX.
2486 return true
2487}
2488
Jiyong Parke3833882020-02-17 17:28:10 +09002489// from android.PrebuiltEtcModule
Jooyung Han0703fd82020-08-26 22:11:53 +09002490func (module *sdkLibraryXml) BaseDir() string {
2491 return "etc"
2492}
2493
2494// from android.PrebuiltEtcModule
Jiyong Parke3833882020-02-17 17:28:10 +09002495func (module *sdkLibraryXml) SubDir() string {
2496 return "permissions"
2497}
2498
2499// from android.PrebuiltEtcModule
2500func (module *sdkLibraryXml) OutputFile() android.OutputPath {
2501 return module.outputFilePath
2502}
2503
2504// from android.ApexModule
2505func (module *sdkLibraryXml) AvailableFor(what string) bool {
2506 return true
2507}
2508
2509func (module *sdkLibraryXml) DepsMutator(ctx android.BottomUpMutatorContext) {
2510 // do nothing
2511}
2512
Jiyong Park45bf82e2020-12-15 22:29:02 +09002513var _ android.ApexModule = (*sdkLibraryXml)(nil)
2514
2515// Implements android.ApexModule
Dan Albertc8060532020-07-22 22:32:17 -07002516func (module *sdkLibraryXml) ShouldSupportSdkVersion(ctx android.BaseModuleContext,
2517 sdkVersion android.ApiLevel) error {
Jooyung Han749dc692020-04-15 11:03:39 +09002518 // sdkLibraryXml doesn't need to be checked separately because java_sdk_library is checked
2519 return nil
2520}
2521
Jiyong Parke3833882020-02-17 17:28:10 +09002522// File path to the runtime implementation library
Colin Cross56a83212020-09-15 18:30:11 -07002523func (module *sdkLibraryXml) implPath(ctx android.ModuleContext) string {
Jiyong Parke3833882020-02-17 17:28:10 +09002524 implName := proptools.String(module.properties.Lib_name)
Colin Cross56a83212020-09-15 18:30:11 -07002525 if apexInfo := ctx.Provider(android.ApexInfoProvider).(android.ApexInfo); !apexInfo.IsForPlatform() {
Colin Crosse07f2312020-08-13 11:24:56 -07002526 // TODO(b/146468504): ApexVariationName() is only a soong module name, not apex name.
Jiyong Parke3833882020-02-17 17:28:10 +09002527 // In most cases, this works fine. But when apex_name is set or override_apex is used
2528 // this can be wrong.
Colin Cross56a83212020-09-15 18:30:11 -07002529 return fmt.Sprintf("/apex/%s/javalib/%s.jar", apexInfo.ApexVariationName, implName)
Jiyong Parke3833882020-02-17 17:28:10 +09002530 }
2531 partition := "system"
2532 if module.SocSpecific() {
2533 partition = "vendor"
2534 } else if module.DeviceSpecific() {
2535 partition = "odm"
2536 } else if module.ProductSpecific() {
2537 partition = "product"
2538 } else if module.SystemExtSpecific() {
2539 partition = "system_ext"
2540 }
2541 return "/" + partition + "/framework/" + implName + ".jar"
2542}
2543
Pedro Loureiro9956e5e2021-09-07 17:21:59 +00002544func formattedOptionalSdkLevelAttribute(ctx android.ModuleContext, attrName string, value *string) string {
2545 if value == nil {
2546 return ""
2547 }
2548 apiLevel, err := android.ApiLevelFromUser(ctx, *value)
2549 if err != nil {
Pedro Loureiroba6682f2021-10-29 09:32:32 +00002550 // attributes in bp files have underscores but in the xml have dashes.
2551 ctx.PropertyErrorf(strings.ReplaceAll(attrName, "-", "_"), err.Error())
Pedro Loureiro9956e5e2021-09-07 17:21:59 +00002552 return ""
2553 }
2554 intStr := strconv.Itoa(apiLevel.FinalOrPreviewInt())
2555 return formattedOptionalAttribute(attrName, &intStr)
2556}
2557
2558// formats an attribute for the xml permissions file if the value is not null
2559// returns empty string otherwise
2560func formattedOptionalAttribute(attrName string, value *string) string {
2561 if value == nil {
2562 return ""
2563 }
2564 return fmt.Sprintf(` %s=\"%s\"\n`, attrName, *value)
2565}
2566
2567func (module *sdkLibraryXml) permissionsContents(ctx android.ModuleContext) string {
2568 libName := proptools.String(module.properties.Lib_name)
2569 libNameAttr := formattedOptionalAttribute("name", &libName)
2570 filePath := module.implPath(ctx)
2571 filePathAttr := formattedOptionalAttribute("file", &filePath)
Pedro Loureiroba6682f2021-10-29 09:32:32 +00002572 implicitFromAttr := formattedOptionalSdkLevelAttribute(ctx, "on-bootclasspath-since", module.properties.On_bootclasspath_since)
2573 implicitUntilAttr := formattedOptionalSdkLevelAttribute(ctx, "on-bootclasspath-before", module.properties.On_bootclasspath_before)
2574 minSdkAttr := formattedOptionalSdkLevelAttribute(ctx, "min-device-sdk", module.properties.Min_device_sdk)
2575 maxSdkAttr := formattedOptionalSdkLevelAttribute(ctx, "max-device-sdk", module.properties.Max_device_sdk)
Pedro Loureiro196d3e62021-12-22 19:53:01 +00002576 // <library> is understood in all android versions whereas <apex-library> is only understood from API T (and ignored before that).
2577 // 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 +00002578 var libraryTag string
2579 if module.properties.Min_device_sdk != nil {
Pedro Loureiro196d3e62021-12-22 19:53:01 +00002580 libraryTag = ` <apex-library\n`
Pedro Loureiroc3621422021-09-28 15:40:23 +00002581 } else {
2582 libraryTag = ` <library\n`
2583 }
Pedro Loureiro9956e5e2021-09-07 17:21:59 +00002584
2585 return strings.Join([]string{
2586 `<?xml version=\"1.0\" encoding=\"utf-8\"?>\n`,
2587 `<!-- Copyright (C) 2018 The Android Open Source Project\n`,
2588 `\n`,
2589 ` Licensed under the Apache License, Version 2.0 (the \"License\");\n`,
2590 ` you may not use this file except in compliance with the License.\n`,
2591 ` You may obtain a copy of the License at\n`,
2592 `\n`,
2593 ` http://www.apache.org/licenses/LICENSE-2.0\n`,
2594 `\n`,
2595 ` Unless required by applicable law or agreed to in writing, software\n`,
2596 ` distributed under the License is distributed on an \"AS IS\" BASIS,\n`,
2597 ` WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\n`,
2598 ` See the License for the specific language governing permissions and\n`,
2599 ` limitations under the License.\n`,
2600 `-->\n`,
2601 `<permissions>\n`,
Pedro Loureiroc3621422021-09-28 15:40:23 +00002602 libraryTag,
Pedro Loureiro9956e5e2021-09-07 17:21:59 +00002603 libNameAttr,
2604 filePathAttr,
2605 implicitFromAttr,
2606 implicitUntilAttr,
2607 minSdkAttr,
2608 maxSdkAttr,
2609 ` />\n`,
2610 `</permissions>\n`}, "")
2611}
2612
Jiyong Parke3833882020-02-17 17:28:10 +09002613func (module *sdkLibraryXml) GenerateAndroidBuildActions(ctx android.ModuleContext) {
Colin Cross56a83212020-09-15 18:30:11 -07002614 module.hideApexVariantFromMake = !ctx.Provider(android.ApexInfoProvider).(android.ApexInfo).IsForPlatform()
2615
Jiyong Parke3833882020-02-17 17:28:10 +09002616 libName := proptools.String(module.properties.Lib_name)
Pedro Loureiroc3621422021-09-28 15:40:23 +00002617 module.selfValidate(ctx)
Pedro Loureiro9956e5e2021-09-07 17:21:59 +00002618 xmlContent := module.permissionsContents(ctx)
Jiyong Parke3833882020-02-17 17:28:10 +09002619
2620 module.outputFilePath = android.PathForModuleOut(ctx, libName+".xml").OutputPath
Colin Crossf1a035e2020-11-16 17:32:30 -08002621 rule := android.NewRuleBuilder(pctx, ctx)
Jiyong Parke3833882020-02-17 17:28:10 +09002622 rule.Command().
2623 Text("/bin/bash -c \"echo -e '" + xmlContent + "'\" > ").
2624 Output(module.outputFilePath)
2625
Colin Crossf1a035e2020-11-16 17:32:30 -08002626 rule.Build("java_sdk_xml", "Permission XML")
Jiyong Parke3833882020-02-17 17:28:10 +09002627
2628 module.installDirPath = android.PathForModuleInstall(ctx, "etc", module.SubDir())
2629}
2630
2631func (module *sdkLibraryXml) AndroidMkEntries() []android.AndroidMkEntries {
Colin Cross56a83212020-09-15 18:30:11 -07002632 if module.hideApexVariantFromMake {
satayev8f088b02021-12-06 11:40:46 +00002633 return []android.AndroidMkEntries{{
Jiyong Parke3833882020-02-17 17:28:10 +09002634 Disabled: true,
2635 }}
2636 }
2637
satayev8f088b02021-12-06 11:40:46 +00002638 return []android.AndroidMkEntries{{
Jiyong Parke3833882020-02-17 17:28:10 +09002639 Class: "ETC",
2640 OutputFile: android.OptionalPathForPath(module.outputFilePath),
2641 ExtraEntries: []android.AndroidMkExtraEntriesFunc{
Colin Crossaa255532020-07-03 13:18:24 -07002642 func(ctx android.AndroidMkExtraEntriesContext, entries *android.AndroidMkEntries) {
Jiyong Parke3833882020-02-17 17:28:10 +09002643 entries.SetString("LOCAL_MODULE_TAGS", "optional")
Colin Crossc68db4b2021-11-11 18:59:15 -08002644 entries.SetString("LOCAL_MODULE_PATH", module.installDirPath.String())
Jiyong Parke3833882020-02-17 17:28:10 +09002645 entries.SetString("LOCAL_INSTALLED_MODULE_STEM", module.outputFilePath.Base())
2646 },
2647 },
2648 }}
2649}
Paul Duffindd46f712020-02-10 13:37:10 +00002650
Pedro Loureiroc3621422021-09-28 15:40:23 +00002651func (module *sdkLibraryXml) selfValidate(ctx android.ModuleContext) {
2652 module.validateAtLeastTAttributes(ctx)
2653 module.validateMinAndMaxDeviceSdk(ctx)
2654 module.validateMinMaxDeviceSdkAndModuleMinSdk(ctx)
2655 module.validateOnBootclasspathBeforeRequirements(ctx)
2656}
2657
2658func (module *sdkLibraryXml) validateAtLeastTAttributes(ctx android.ModuleContext) {
2659 t := android.ApiLevelOrPanic(ctx, "Tiramisu")
2660 module.attrAtLeastT(ctx, t, module.properties.Min_device_sdk, "min_device_sdk")
2661 module.attrAtLeastT(ctx, t, module.properties.Max_device_sdk, "max_device_sdk")
2662 module.attrAtLeastT(ctx, t, module.properties.On_bootclasspath_before, "on_bootclasspath_before")
2663 module.attrAtLeastT(ctx, t, module.properties.On_bootclasspath_since, "on_bootclasspath_since")
2664}
2665
2666func (module *sdkLibraryXml) attrAtLeastT(ctx android.ModuleContext, t android.ApiLevel, attr *string, attrName string) {
2667 if attr != nil {
2668 if level, err := android.ApiLevelFromUser(ctx, *attr); err == nil {
2669 // we will inform the user of invalid inputs when we try to write the
2670 // permissions xml file so we don't need to do it here
2671 if t.GreaterThan(level) {
2672 ctx.PropertyErrorf(attrName, "Attribute value needs to be at least T")
2673 }
2674 }
2675 }
2676}
2677
2678func (module *sdkLibraryXml) validateMinAndMaxDeviceSdk(ctx android.ModuleContext) {
2679 if module.properties.Min_device_sdk != nil && module.properties.Max_device_sdk != nil {
2680 min, minErr := android.ApiLevelFromUser(ctx, *module.properties.Min_device_sdk)
2681 max, maxErr := android.ApiLevelFromUser(ctx, *module.properties.Max_device_sdk)
2682 if minErr == nil && maxErr == nil {
2683 // we will inform the user of invalid inputs when we try to write the
2684 // permissions xml file so we don't need to do it here
2685 if min.GreaterThan(max) {
2686 ctx.ModuleErrorf("min_device_sdk can't be greater than max_device_sdk")
2687 }
2688 }
2689 }
2690}
2691
2692func (module *sdkLibraryXml) validateMinMaxDeviceSdkAndModuleMinSdk(ctx android.ModuleContext) {
2693 moduleMinApi := android.ApiLevelOrPanic(ctx, *module.properties.Sdk_library_min_api_level)
2694 if module.properties.Min_device_sdk != nil {
2695 api, err := android.ApiLevelFromUser(ctx, *module.properties.Min_device_sdk)
2696 if err == nil {
2697 if moduleMinApi.GreaterThan(api) {
2698 ctx.PropertyErrorf("min_device_sdk", "Can't be less than module's min sdk (%s)", moduleMinApi)
2699 }
2700 }
2701 }
2702 if module.properties.Max_device_sdk != nil {
2703 api, err := android.ApiLevelFromUser(ctx, *module.properties.Max_device_sdk)
2704 if err == nil {
2705 if moduleMinApi.GreaterThan(api) {
2706 ctx.PropertyErrorf("max_device_sdk", "Can't be less than module's min sdk (%s)", moduleMinApi)
2707 }
2708 }
2709 }
2710}
2711
2712func (module *sdkLibraryXml) validateOnBootclasspathBeforeRequirements(ctx android.ModuleContext) {
2713 moduleMinApi := android.ApiLevelOrPanic(ctx, *module.properties.Sdk_library_min_api_level)
2714 if module.properties.On_bootclasspath_before != nil {
2715 t := android.ApiLevelOrPanic(ctx, "Tiramisu")
2716 // if we use the attribute, then we need to do this validation
2717 if moduleMinApi.LessThan(t) {
2718 // if minAPi is < T, then we need to have min_device_sdk (which only accepts T+)
2719 if module.properties.Min_device_sdk == nil {
2720 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")
2721 }
2722 }
2723 }
2724}
2725
Paul Duffindd46f712020-02-10 13:37:10 +00002726type sdkLibrarySdkMemberType struct {
2727 android.SdkMemberTypeBase
2728}
2729
Paul Duffin296701e2021-07-14 10:29:36 +01002730func (s *sdkLibrarySdkMemberType) AddDependencies(ctx android.SdkDependencyContext, dependencyTag blueprint.DependencyTag, names []string) {
2731 ctx.AddVariationDependencies(nil, dependencyTag, names...)
Paul Duffindd46f712020-02-10 13:37:10 +00002732}
2733
2734func (s *sdkLibrarySdkMemberType) IsInstance(module android.Module) bool {
2735 _, ok := module.(*SdkLibrary)
2736 return ok
2737}
2738
2739func (s *sdkLibrarySdkMemberType) AddPrebuiltModule(ctx android.SdkMemberContext, member android.SdkMember) android.BpModule {
2740 return ctx.SnapshotBuilder().AddPrebuiltModule(member, "java_sdk_library_import")
2741}
2742
2743func (s *sdkLibrarySdkMemberType) CreateVariantPropertiesStruct() android.SdkMemberProperties {
2744 return &sdkLibrarySdkMemberProperties{}
2745}
2746
Paul Duffin976b0e52021-04-27 23:20:26 +01002747var javaSdkLibrarySdkMemberType = &sdkLibrarySdkMemberType{
2748 android.SdkMemberTypeBase{
2749 PropertyName: "java_sdk_libs",
2750 SupportsSdk: true,
2751 },
2752}
2753
Paul Duffindd46f712020-02-10 13:37:10 +00002754type sdkLibrarySdkMemberProperties struct {
2755 android.SdkMemberPropertiesBase
2756
2757 // Scope to per scope properties.
Paul Duffin106a3a42022-01-27 16:39:06 +00002758 Scopes map[*apiScope]*scopeProperties
Paul Duffindd46f712020-02-10 13:37:10 +00002759
Paul Duffin3d1248c2020-04-09 00:10:17 +01002760 // The Java stubs source files.
2761 Stub_srcs []string
Paul Duffinf7a64332020-05-13 16:54:55 +01002762
2763 // The naming scheme.
2764 Naming_scheme *string
Paul Duffind7eb1c22020-05-26 20:57:10 +01002765
2766 // True if the java_sdk_library_import is for a shared library, false
2767 // otherwise.
2768 Shared_library *bool
Paul Duffina2ae7e02020-09-11 11:55:00 +01002769
Paul Duffin1267d872021-04-16 17:21:36 +01002770 // True if the stub imports should produce dex jars.
2771 Compile_dex *bool
2772
Paul Duffina2ae7e02020-09-11 11:55:00 +01002773 // The paths to the doctag files to add to the prebuilt.
2774 Doctag_paths android.Paths
Paul Duffin869de142021-07-15 14:14:41 +01002775
2776 Permitted_packages []string
Pedro Loureiro9956e5e2021-09-07 17:21:59 +00002777
2778 // Signals that this shared library is part of the bootclasspath starting
2779 // on the version indicated in this attribute.
2780 //
2781 // This will make platforms at this level and above to ignore
2782 // <uses-library> tags with this library name because the library is already
2783 // available
2784 On_bootclasspath_since *string
2785
2786 // Signals that this shared library was part of the bootclasspath before
2787 // (but not including) the version indicated in this attribute.
2788 //
2789 // The system will automatically add a <uses-library> tag with this library to
2790 // apps that target any SDK less than the version indicated in this attribute.
2791 On_bootclasspath_before *string
2792
2793 // Indicates that PackageManager should ignore this shared library if the
2794 // platform is below the version indicated in this attribute.
2795 //
2796 // This means that the device won't recognise this library as installed.
2797 Min_device_sdk *string
2798
2799 // Indicates that PackageManager should ignore this shared library if the
2800 // platform is above the version indicated in this attribute.
2801 //
2802 // This means that the device won't recognise this library as installed.
2803 Max_device_sdk *string
Paul Duffindd46f712020-02-10 13:37:10 +00002804}
2805
2806type scopeProperties struct {
Paul Duffin1fd005d2020-04-09 01:08:11 +01002807 Jars android.Paths
2808 StubsSrcJar android.Path
2809 CurrentApiFile android.Path
2810 RemovedApiFile android.Path
Anton Hanssond78eb762021-09-21 15:25:12 +01002811 AnnotationsZip android.Path
Paul Duffin1fd005d2020-04-09 01:08:11 +01002812 SdkVersion string
Paul Duffindd46f712020-02-10 13:37:10 +00002813}
2814
2815func (s *sdkLibrarySdkMemberProperties) PopulateFromVariant(ctx android.SdkMemberContext, variant android.Module) {
2816 sdk := variant.(*SdkLibrary)
2817
Paul Duffin106a3a42022-01-27 16:39:06 +00002818 s.Scopes = make(map[*apiScope]*scopeProperties)
Paul Duffindd46f712020-02-10 13:37:10 +00002819 for _, apiScope := range allApiScopes {
Paul Duffin803a9562020-05-20 11:52:25 +01002820 paths := sdk.findScopePaths(apiScope)
2821 if paths == nil {
2822 continue
2823 }
2824
Paul Duffindd46f712020-02-10 13:37:10 +00002825 jars := paths.stubsImplPath
2826 if len(jars) > 0 {
2827 properties := scopeProperties{}
2828 properties.Jars = jars
Paul Duffin780c5f42020-05-12 15:52:55 +01002829 properties.SdkVersion = sdk.sdkVersionForStubsLibrary(ctx.SdkModuleContext(), apiScope)
Paul Duffin0f8faff2020-05-20 16:18:00 +01002830 properties.StubsSrcJar = paths.stubsSrcJar.Path()
Paul Duffin10269f12020-06-19 18:39:55 +01002831 if paths.currentApiFilePath.Valid() {
2832 properties.CurrentApiFile = paths.currentApiFilePath.Path()
2833 }
2834 if paths.removedApiFilePath.Valid() {
2835 properties.RemovedApiFile = paths.removedApiFilePath.Path()
2836 }
Anton Hanssond78eb762021-09-21 15:25:12 +01002837 // The annotations zip is only available for modules that set annotations_enabled: true.
2838 if paths.annotationsZip.Valid() {
2839 properties.AnnotationsZip = paths.annotationsZip.Path()
2840 }
Paul Duffin106a3a42022-01-27 16:39:06 +00002841 s.Scopes[apiScope] = &properties
Paul Duffindd46f712020-02-10 13:37:10 +00002842 }
2843 }
2844
Paul Duffindfa131e2020-05-15 20:37:11 +01002845 s.Naming_scheme = sdk.commonSdkLibraryProperties.Naming_scheme
Paul Duffind7eb1c22020-05-26 20:57:10 +01002846 s.Shared_library = proptools.BoolPtr(sdk.sharedLibrary())
Paul Duffin1267d872021-04-16 17:21:36 +01002847 s.Compile_dex = sdk.dexProperties.Compile_dex
Paul Duffina2ae7e02020-09-11 11:55:00 +01002848 s.Doctag_paths = sdk.doctagPaths
Paul Duffin869de142021-07-15 14:14:41 +01002849 s.Permitted_packages = sdk.PermittedPackagesForUpdatableBootJars()
Pedro Loureiro9956e5e2021-09-07 17:21:59 +00002850 s.On_bootclasspath_since = sdk.commonSdkLibraryProperties.On_bootclasspath_since
2851 s.On_bootclasspath_before = sdk.commonSdkLibraryProperties.On_bootclasspath_before
2852 s.Min_device_sdk = sdk.commonSdkLibraryProperties.Min_device_sdk
2853 s.Max_device_sdk = sdk.commonSdkLibraryProperties.Max_device_sdk
Paul Duffindd46f712020-02-10 13:37:10 +00002854}
2855
2856func (s *sdkLibrarySdkMemberProperties) AddToPropertySet(ctx android.SdkMemberContext, propertySet android.BpPropertySet) {
Paul Duffinf7a64332020-05-13 16:54:55 +01002857 if s.Naming_scheme != nil {
2858 propertySet.AddProperty("naming_scheme", proptools.String(s.Naming_scheme))
2859 }
Paul Duffind7eb1c22020-05-26 20:57:10 +01002860 if s.Shared_library != nil {
2861 propertySet.AddProperty("shared_library", *s.Shared_library)
2862 }
Paul Duffin1267d872021-04-16 17:21:36 +01002863 if s.Compile_dex != nil {
2864 propertySet.AddProperty("compile_dex", *s.Compile_dex)
2865 }
Paul Duffin869de142021-07-15 14:14:41 +01002866 if len(s.Permitted_packages) > 0 {
2867 propertySet.AddProperty("permitted_packages", s.Permitted_packages)
2868 }
Paul Duffinf7a64332020-05-13 16:54:55 +01002869
Paul Duffindd46f712020-02-10 13:37:10 +00002870 for _, apiScope := range allApiScopes {
2871 if properties, ok := s.Scopes[apiScope]; ok {
Paul Duffin6b836ba2020-05-13 19:19:49 +01002872 scopeSet := propertySet.AddPropertySet(apiScope.propertyName)
Paul Duffindd46f712020-02-10 13:37:10 +00002873
Paul Duffin3d1248c2020-04-09 00:10:17 +01002874 scopeDir := filepath.Join("sdk_library", s.OsPrefix(), apiScope.name)
2875
Paul Duffindd46f712020-02-10 13:37:10 +00002876 var jars []string
2877 for _, p := range properties.Jars {
Paul Duffin3d1248c2020-04-09 00:10:17 +01002878 dest := filepath.Join(scopeDir, ctx.Name()+"-stubs.jar")
Paul Duffindd46f712020-02-10 13:37:10 +00002879 ctx.SnapshotBuilder().CopyToSnapshot(p, dest)
2880 jars = append(jars, dest)
2881 }
2882 scopeSet.AddProperty("jars", jars)
2883
Paul Duffin22628d52021-05-12 23:13:22 +01002884 if ctx.SdkModuleContext().Config().IsEnvTrue("SOONG_SDK_SNAPSHOT_USE_SRCJAR") {
2885 // Copy the stubs source jar into the snapshot zip as is.
2886 srcJarSnapshotPath := filepath.Join(scopeDir, ctx.Name()+".srcjar")
2887 ctx.SnapshotBuilder().CopyToSnapshot(properties.StubsSrcJar, srcJarSnapshotPath)
2888 scopeSet.AddProperty("stub_srcs", []string{srcJarSnapshotPath})
2889 } else {
2890 // Merge the stubs source jar into the snapshot zip so that when it is unpacked
2891 // the source files are also unpacked.
2892 snapshotRelativeDir := filepath.Join(scopeDir, ctx.Name()+"_stub_sources")
2893 ctx.SnapshotBuilder().UnzipToSnapshot(properties.StubsSrcJar, snapshotRelativeDir)
2894 scopeSet.AddProperty("stub_srcs", []string{snapshotRelativeDir})
2895 }
Paul Duffin3d1248c2020-04-09 00:10:17 +01002896
Paul Duffin1fd005d2020-04-09 01:08:11 +01002897 if properties.CurrentApiFile != nil {
2898 currentApiSnapshotPath := filepath.Join(scopeDir, ctx.Name()+".txt")
2899 ctx.SnapshotBuilder().CopyToSnapshot(properties.CurrentApiFile, currentApiSnapshotPath)
2900 scopeSet.AddProperty("current_api", currentApiSnapshotPath)
2901 }
2902
2903 if properties.RemovedApiFile != nil {
2904 removedApiSnapshotPath := filepath.Join(scopeDir, ctx.Name()+"-removed.txt")
Paul Duffin3dbf9fd2020-06-02 13:00:02 +01002905 ctx.SnapshotBuilder().CopyToSnapshot(properties.RemovedApiFile, removedApiSnapshotPath)
Paul Duffin1fd005d2020-04-09 01:08:11 +01002906 scopeSet.AddProperty("removed_api", removedApiSnapshotPath)
2907 }
2908
Anton Hanssond78eb762021-09-21 15:25:12 +01002909 if properties.AnnotationsZip != nil {
2910 annotationsSnapshotPath := filepath.Join(scopeDir, ctx.Name()+"_annotations.zip")
2911 ctx.SnapshotBuilder().CopyToSnapshot(properties.AnnotationsZip, annotationsSnapshotPath)
2912 scopeSet.AddProperty("annotations", annotationsSnapshotPath)
2913 }
2914
Paul Duffindd46f712020-02-10 13:37:10 +00002915 if properties.SdkVersion != "" {
2916 scopeSet.AddProperty("sdk_version", properties.SdkVersion)
2917 }
2918 }
2919 }
2920
Paul Duffina2ae7e02020-09-11 11:55:00 +01002921 if len(s.Doctag_paths) > 0 {
2922 dests := []string{}
2923 for _, p := range s.Doctag_paths {
2924 dest := filepath.Join("doctags", p.Rel())
2925 ctx.SnapshotBuilder().CopyToSnapshot(p, dest)
2926 dests = append(dests, dest)
2927 }
2928 propertySet.AddProperty("doctag_files", dests)
2929 }
Paul Duffindd46f712020-02-10 13:37:10 +00002930}