blob: cb48058233a32f9a04677d98f331e3a9ccb38ecc [file] [log] [blame]
Jiyong Parkc678ad32018-04-10 13:07:10 +09001// Copyright 2018 Google Inc. All rights reserved.
2//
3// Licensed under the Apache License, Version 2.0 (the "License");
4// you may not use this file except in compliance with the License.
5// You may obtain a copy of the License at
6//
7// http://www.apache.org/licenses/LICENSE-2.0
8//
9// Unless required by applicable law or agreed to in writing, software
10// distributed under the License is distributed on an "AS IS" BASIS,
11// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12// See the License for the specific language governing permissions and
13// limitations under the License.
14
15package java
16
17import (
Jiyong Parkc678ad32018-04-10 13:07:10 +090018 "fmt"
19 "path"
Sundong Ahn054b19a2018-10-19 13:46:09 +090020 "path/filepath"
Paul Duffin46a26a82020-04-07 19:27:04 +010021 "reflect"
Paul Duffin46dc45a2020-05-14 15:39:10 +010022 "regexp"
Jiyong Park82484c02018-04-23 21:41:26 +090023 "sort"
Jiyong Parkc678ad32018-04-10 13:07:10 +090024 "strings"
Jiyong Park82484c02018-04-23 21:41:26 +090025 "sync"
Jiyong Parkc678ad32018-04-10 13:07:10 +090026
Paul Duffind1b3a922020-01-22 11:57:20 +000027 "github.com/google/blueprint"
Jiyong Parkc678ad32018-04-10 13:07:10 +090028 "github.com/google/blueprint/proptools"
Paul Duffin46a26a82020-04-07 19:27:04 +010029
30 "android/soong/android"
Ulya Trafimovichdbf31662020-12-17 12:07:54 +000031 "android/soong/dexpreopt"
Jiyong Parkc678ad32018-04-10 13:07:10 +090032)
33
Jooyung Han58f26ab2019-12-18 15:34:32 +090034const (
Pedro Loureiro9956e5e2021-09-07 17:21:59 +000035 sdkXmlFileSuffix = ".xml"
Jiyong Parkc678ad32018-04-10 13:07:10 +090036)
37
Paul Duffind1b3a922020-01-22 11:57:20 +000038// A tag to associated a dependency with a specific api scope.
39type scopeDependencyTag struct {
40 blueprint.BaseDependencyTag
41 name string
42 apiScope *apiScope
Paul Duffinc8782502020-04-29 20:45:27 +010043
44 // Function for extracting appropriate path information from the dependency.
Colin Crossdcf71b22021-02-01 13:59:03 -080045 depInfoExtractor func(paths *scopePaths, ctx android.ModuleContext, dep android.Module) error
Paul Duffinc8782502020-04-29 20:45:27 +010046}
47
48// Extract tag specific information from the dependency.
49func (tag scopeDependencyTag) extractDepInfo(ctx android.ModuleContext, dep android.Module, paths *scopePaths) {
Colin Crossdcf71b22021-02-01 13:59:03 -080050 err := tag.depInfoExtractor(paths, ctx, dep)
Paul Duffinc8782502020-04-29 20:45:27 +010051 if err != nil {
52 ctx.ModuleErrorf("has an invalid {scopeDependencyTag: %s} dependency on module %s: %s", tag.name, ctx.OtherModuleName(dep), err.Error())
53 }
Paul Duffind1b3a922020-01-22 11:57:20 +000054}
55
Paul Duffin80342d72020-06-26 22:08:43 +010056var _ android.ReplaceSourceWithPrebuilt = (*scopeDependencyTag)(nil)
57
58func (tag scopeDependencyTag) ReplaceSourceWithPrebuilt() bool {
59 return false
60}
61
Paul Duffind1b3a922020-01-22 11:57:20 +000062// Provides information about an api scope, e.g. public, system, test.
63type apiScope struct {
64 // The name of the api scope, e.g. public, system, test
65 name string
66
Paul Duffin97b53b82020-05-05 14:40:52 +010067 // The api scope that this scope extends.
68 extends *apiScope
69
Paul Duffin3375e352020-04-28 10:44:03 +010070 // The legacy enabled status for a specific scope can be dependent on other
71 // properties that have been specified on the library so it is provided by
72 // a function that can determine the status by examining those properties.
73 legacyEnabledStatus func(module *SdkLibrary) bool
74
75 // The default enabled status for non-legacy behavior, which is triggered by
76 // explicitly enabling at least one api scope.
77 defaultEnabledStatus bool
78
79 // Gets a pointer to the scope specific properties.
80 scopeSpecificProperties func(module *SdkLibrary) *ApiScopeProperties
81
Paul Duffin46a26a82020-04-07 19:27:04 +010082 // The name of the field in the dynamically created structure.
83 fieldName string
84
Paul Duffin6b836ba2020-05-13 19:19:49 +010085 // The name of the property in the java_sdk_library_import
86 propertyName string
87
Paul Duffind1b3a922020-01-22 11:57:20 +000088 // The tag to use to depend on the stubs library module.
89 stubsTag scopeDependencyTag
90
Paul Duffin0ff08bd2020-04-29 13:30:54 +010091 // The tag to use to depend on the stubs source module (if separate from the API module).
92 stubsSourceTag scopeDependencyTag
93
94 // The tag to use to depend on the API file generating module (if separate from the stubs source module).
95 apiFileTag scopeDependencyTag
96
Paul Duffinc8782502020-04-29 20:45:27 +010097 // The tag to use to depend on the stubs source and API module.
98 stubsSourceAndApiTag scopeDependencyTag
Paul Duffind1b3a922020-01-22 11:57:20 +000099
100 // The scope specific prefix to add to the api file base of "current.txt" or "removed.txt".
101 apiFilePrefix string
102
103 // The scope specific prefix to add to the sdk library module name to construct a scope specific
104 // module name.
105 moduleSuffix string
106
Paul Duffind1b3a922020-01-22 11:57:20 +0000107 // SDK version that the stubs library is built against. Note that this is always
108 // *current. Older stubs library built with a numbered SDK version is created from
109 // the prebuilt jar.
110 sdkVersion string
Paul Duffin1fb487d2020-04-07 18:50:10 +0100111
Paul Duffin15f34ef2020-07-20 18:04:44 +0100112 // The annotation that identifies this API level, empty for the public API scope.
113 annotation string
114
Paul Duffin1fb487d2020-04-07 18:50:10 +0100115 // Extra arguments to pass to droidstubs for this scope.
Paul Duffin0ff08bd2020-04-29 13:30:54 +0100116 //
Paul Duffin15f34ef2020-07-20 18:04:44 +0100117 // This is not used directly but is used to construct the droidstubsArgs.
118 extraArgs []string
Paul Duffin0ff08bd2020-04-29 13:30:54 +0100119
Paul Duffin15f34ef2020-07-20 18:04:44 +0100120 // The args that must be passed to droidstubs to generate the API and stubs source
121 // for this scope, constructed dynamically by initApiScope().
Paul Duffin0ff08bd2020-04-29 13:30:54 +0100122 //
123 // The API only includes the additional members that this scope adds over the scope
124 // that it extends.
Paul Duffin15f34ef2020-07-20 18:04:44 +0100125 //
126 // The stubs source must include the definitions of everything that is in this
127 // api scope and all the scopes that this one extends.
128 droidstubsArgs []string
Paul Duffin0ff08bd2020-04-29 13:30:54 +0100129
Anton Hansson6478ac12020-05-02 11:19:36 +0100130 // Whether the api scope can be treated as unstable, and should skip compat checks.
131 unstable bool
Paul Duffind1b3a922020-01-22 11:57:20 +0000132}
133
134// Initialize a scope, creating and adding appropriate dependency tags
135func initApiScope(scope *apiScope) *apiScope {
Paul Duffinc8782502020-04-29 20:45:27 +0100136 name := scope.name
Paul Duffin46dc45a2020-05-14 15:39:10 +0100137 scopeByName[name] = scope
138 allScopeNames = append(allScopeNames, name)
Paul Duffin6b836ba2020-05-13 19:19:49 +0100139 scope.propertyName = strings.ReplaceAll(name, "-", "_")
140 scope.fieldName = proptools.FieldNameForProperty(scope.propertyName)
Paul Duffind1b3a922020-01-22 11:57:20 +0000141 scope.stubsTag = scopeDependencyTag{
Paul Duffinc8782502020-04-29 20:45:27 +0100142 name: name + "-stubs",
143 apiScope: scope,
144 depInfoExtractor: (*scopePaths).extractStubsLibraryInfoFromDependency,
Paul Duffind1b3a922020-01-22 11:57:20 +0000145 }
Paul Duffin0ff08bd2020-04-29 13:30:54 +0100146 scope.stubsSourceTag = scopeDependencyTag{
147 name: name + "-stubs-source",
148 apiScope: scope,
149 depInfoExtractor: (*scopePaths).extractStubsSourceInfoFromDep,
150 }
151 scope.apiFileTag = scopeDependencyTag{
152 name: name + "-api",
153 apiScope: scope,
154 depInfoExtractor: (*scopePaths).extractApiInfoFromDep,
155 }
Paul Duffinc8782502020-04-29 20:45:27 +0100156 scope.stubsSourceAndApiTag = scopeDependencyTag{
157 name: name + "-stubs-source-and-api",
158 apiScope: scope,
159 depInfoExtractor: (*scopePaths).extractStubsSourceAndApiInfoFromApiStubsProvider,
Paul Duffind1b3a922020-01-22 11:57:20 +0000160 }
Paul Duffin0ff08bd2020-04-29 13:30:54 +0100161
162 // To get the args needed to generate the stubs source append all the args from
163 // this scope and all the scopes it extends as each set of args adds additional
164 // members to the stubs.
Paul Duffin15f34ef2020-07-20 18:04:44 +0100165 var scopeSpecificArgs []string
166 if scope.annotation != "" {
167 scopeSpecificArgs = []string{"--show-annotation", scope.annotation}
Paul Duffin0ff08bd2020-04-29 13:30:54 +0100168 }
Paul Duffin15f34ef2020-07-20 18:04:44 +0100169 for s := scope; s != nil; s = s.extends {
170 scopeSpecificArgs = append(scopeSpecificArgs, s.extraArgs...)
Paul Duffin0ff08bd2020-04-29 13:30:54 +0100171
Paul Duffin15f34ef2020-07-20 18:04:44 +0100172 // Ensure that the generated stubs includes all the API elements from the API scope
173 // that this scope extends.
174 if s != scope && s.annotation != "" {
175 scopeSpecificArgs = append(scopeSpecificArgs, "--show-for-stub-purposes-annotation", s.annotation)
176 }
177 }
Paul Duffin0ff08bd2020-04-29 13:30:54 +0100178
Paul Duffin15f34ef2020-07-20 18:04:44 +0100179 // Escape any special characters in the arguments. This is needed because droidstubs
180 // passes these directly to the shell command.
181 scope.droidstubsArgs = proptools.ShellEscapeList(scopeSpecificArgs)
Paul Duffin0ff08bd2020-04-29 13:30:54 +0100182
Paul Duffind1b3a922020-01-22 11:57:20 +0000183 return scope
184}
185
Anton Hansson08f476b2021-04-07 15:32:19 +0100186func (scope *apiScope) stubsLibraryModuleNameSuffix() string {
187 return ".stubs" + scope.moduleSuffix
188}
189
Paul Duffinc3091c82020-05-08 14:16:20 +0100190func (scope *apiScope) stubsLibraryModuleName(baseName string) string {
Anton Hansson08f476b2021-04-07 15:32:19 +0100191 return baseName + scope.stubsLibraryModuleNameSuffix()
Paul Duffind1b3a922020-01-22 11:57:20 +0000192}
193
Paul Duffinc8782502020-04-29 20:45:27 +0100194func (scope *apiScope) stubsSourceModuleName(baseName string) string {
Paul Duffindd9d0742020-05-08 15:52:37 +0100195 return baseName + ".stubs.source" + scope.moduleSuffix
Paul Duffind1b3a922020-01-22 11:57:20 +0000196}
197
Paul Duffin0ff08bd2020-04-29 13:30:54 +0100198func (scope *apiScope) apiModuleName(baseName string) string {
Paul Duffindd9d0742020-05-08 15:52:37 +0100199 return baseName + ".api" + scope.moduleSuffix
Paul Duffin0ff08bd2020-04-29 13:30:54 +0100200}
201
Paul Duffin3375e352020-04-28 10:44:03 +0100202func (scope *apiScope) String() string {
203 return scope.name
204}
205
Paul Duffind1b3a922020-01-22 11:57:20 +0000206type apiScopes []*apiScope
207
208func (scopes apiScopes) Strings(accessor func(*apiScope) string) []string {
209 var list []string
210 for _, scope := range scopes {
211 list = append(list, accessor(scope))
212 }
213 return list
214}
215
Jiyong Parkc678ad32018-04-10 13:07:10 +0900216var (
Paul Duffin46dc45a2020-05-14 15:39:10 +0100217 scopeByName = make(map[string]*apiScope)
218 allScopeNames []string
Paul Duffind1b3a922020-01-22 11:57:20 +0000219 apiScopePublic = initApiScope(&apiScope{
Paul Duffin3375e352020-04-28 10:44:03 +0100220 name: "public",
221
222 // Public scope is enabled by default for both legacy and non-legacy modes.
223 legacyEnabledStatus: func(module *SdkLibrary) bool {
224 return true
225 },
226 defaultEnabledStatus: true,
227
228 scopeSpecificProperties: func(module *SdkLibrary) *ApiScopeProperties {
229 return &module.sdkLibraryProperties.Public
230 },
Paul Duffind1b3a922020-01-22 11:57:20 +0000231 sdkVersion: "current",
232 })
233 apiScopeSystem = initApiScope(&apiScope{
Paul Duffin3375e352020-04-28 10:44:03 +0100234 name: "system",
235 extends: apiScopePublic,
236 legacyEnabledStatus: (*SdkLibrary).generateTestAndSystemScopesByDefault,
237 scopeSpecificProperties: func(module *SdkLibrary) *ApiScopeProperties {
238 return &module.sdkLibraryProperties.System
239 },
Paul Duffin15f34ef2020-07-20 18:04:44 +0100240 apiFilePrefix: "system-",
241 moduleSuffix: ".system",
242 sdkVersion: "system_current",
243 annotation: "android.annotation.SystemApi(client=android.annotation.SystemApi.Client.PRIVILEGED_APPS)",
Paul Duffind1b3a922020-01-22 11:57:20 +0000244 })
245 apiScopeTest = initApiScope(&apiScope{
Paul Duffin3375e352020-04-28 10:44:03 +0100246 name: "test",
Anton Hansson4fe970f2020-10-09 10:16:49 +0100247 extends: apiScopeSystem,
Paul Duffin3375e352020-04-28 10:44:03 +0100248 legacyEnabledStatus: (*SdkLibrary).generateTestAndSystemScopesByDefault,
249 scopeSpecificProperties: func(module *SdkLibrary) *ApiScopeProperties {
250 return &module.sdkLibraryProperties.Test
251 },
Paul Duffin15f34ef2020-07-20 18:04:44 +0100252 apiFilePrefix: "test-",
253 moduleSuffix: ".test",
254 sdkVersion: "test_current",
255 annotation: "android.annotation.TestApi",
256 unstable: true,
Paul Duffind1b3a922020-01-22 11:57:20 +0000257 })
Paul Duffin8f265b92020-04-28 14:13:56 +0100258 apiScopeModuleLib = initApiScope(&apiScope{
Paul Duffin6b836ba2020-05-13 19:19:49 +0100259 name: "module-lib",
Paul Duffin8f265b92020-04-28 14:13:56 +0100260 extends: apiScopeSystem,
Paul Duffin0c5bae52020-06-02 13:00:08 +0100261 // The module-lib scope is disabled by default in legacy mode.
Paul Duffin8f265b92020-04-28 14:13:56 +0100262 //
263 // Enabling this would break existing usages.
264 legacyEnabledStatus: func(module *SdkLibrary) bool {
265 return false
266 },
267 scopeSpecificProperties: func(module *SdkLibrary) *ApiScopeProperties {
268 return &module.sdkLibraryProperties.Module_lib
269 },
270 apiFilePrefix: "module-lib-",
271 moduleSuffix: ".module_lib",
272 sdkVersion: "module_current",
Paul Duffin15f34ef2020-07-20 18:04:44 +0100273 annotation: "android.annotation.SystemApi(client=android.annotation.SystemApi.Client.MODULE_LIBRARIES)",
Paul Duffin8f265b92020-04-28 14:13:56 +0100274 })
Paul Duffin0c5bae52020-06-02 13:00:08 +0100275 apiScopeSystemServer = initApiScope(&apiScope{
276 name: "system-server",
277 extends: apiScopePublic,
278 // The system-server scope is disabled by default in legacy mode.
279 //
280 // Enabling this would break existing usages.
281 legacyEnabledStatus: func(module *SdkLibrary) bool {
282 return false
283 },
284 scopeSpecificProperties: func(module *SdkLibrary) *ApiScopeProperties {
285 return &module.sdkLibraryProperties.System_server
286 },
287 apiFilePrefix: "system-server-",
288 moduleSuffix: ".system_server",
289 sdkVersion: "system_server_current",
Paul Duffin15f34ef2020-07-20 18:04:44 +0100290 annotation: "android.annotation.SystemApi(client=android.annotation.SystemApi.Client.SYSTEM_SERVER)",
291 extraArgs: []string{
292 "--hide-annotation", "android.annotation.Hide",
Paul Duffin0c5bae52020-06-02 13:00:08 +0100293 // com.android.* classes are okay in this interface"
Paul Duffin15f34ef2020-07-20 18:04:44 +0100294 "--hide", "InternalClasses",
Paul Duffin0c5bae52020-06-02 13:00:08 +0100295 },
296 })
Paul Duffind1b3a922020-01-22 11:57:20 +0000297 allApiScopes = apiScopes{
298 apiScopePublic,
299 apiScopeSystem,
300 apiScopeTest,
Paul Duffin8f265b92020-04-28 14:13:56 +0100301 apiScopeModuleLib,
Paul Duffin0c5bae52020-06-02 13:00:08 +0100302 apiScopeSystemServer,
Paul Duffind1b3a922020-01-22 11:57:20 +0000303 }
Jiyong Parkc678ad32018-04-10 13:07:10 +0900304)
305
Jiyong Park82484c02018-04-23 21:41:26 +0900306var (
307 javaSdkLibrariesLock sync.Mutex
308)
309
Jiyong Parkc678ad32018-04-10 13:07:10 +0900310// TODO: these are big features that are currently missing
Jiyong Park1be96912018-05-28 18:02:19 +0900311// 1) disallowing linking to the runtime shared lib
312// 2) HTML generation
Jiyong Parkc678ad32018-04-10 13:07:10 +0900313
314func init() {
Paul Duffin43dc1cc2019-12-19 11:18:54 +0000315 RegisterSdkLibraryBuildComponents(android.InitRegistrationContext)
Jiyong Parkc678ad32018-04-10 13:07:10 +0900316
Jiyong Park82484c02018-04-23 21:41:26 +0900317 android.RegisterMakeVarsProvider(pctx, func(ctx android.MakeVarsContext) {
318 javaSdkLibraries := javaSdkLibraries(ctx.Config())
319 sort.Strings(*javaSdkLibraries)
320 ctx.Strict("JAVA_SDK_LIBRARIES", strings.Join(*javaSdkLibraries, " "))
321 })
Paul Duffindd46f712020-02-10 13:37:10 +0000322
323 // Register sdk member types.
Paul Duffin976b0e52021-04-27 23:20:26 +0100324 android.RegisterSdkMemberType(javaSdkLibrarySdkMemberType)
Jiyong Parkc678ad32018-04-10 13:07:10 +0900325}
326
Paul Duffin43dc1cc2019-12-19 11:18:54 +0000327func RegisterSdkLibraryBuildComponents(ctx android.RegistrationContext) {
328 ctx.RegisterModuleType("java_sdk_library", SdkLibraryFactory)
329 ctx.RegisterModuleType("java_sdk_library_import", sdkLibraryImportFactory)
330}
331
Paul Duffin3375e352020-04-28 10:44:03 +0100332// Properties associated with each api scope.
333type ApiScopeProperties struct {
334 // Indicates whether the api surface is generated.
335 //
336 // If this is set for any scope then all scopes must explicitly specify if they
337 // are enabled. This is to prevent new usages from depending on legacy behavior.
338 //
339 // Otherwise, if this is not set for any scope then the default behavior is
340 // scope specific so please refer to the scope specific property documentation.
341 Enabled *bool
Paul Duffin87a05a32020-05-12 11:50:28 +0100342
343 // The sdk_version to use for building the stubs.
344 //
345 // If not specified then it will use an sdk_version determined as follows:
Trevor Radcliffedf8aa1f2021-11-04 14:25:39 +0000346 //
Paul Duffin87a05a32020-05-12 11:50:28 +0100347 // 1) If the sdk_version specified on the java_sdk_library is none then this
Trevor Radcliffedf8aa1f2021-11-04 14:25:39 +0000348 // will be none. This is used for java_sdk_library instances that are used
349 // to create stubs that contribute to the core_current sdk version.
350 // 2) Otherwise, it is assumed that this library extends but does not
351 // contribute directly to a specific sdk_version and so this uses the
352 // sdk_version appropriate for the api scope. e.g. public will use
353 // sdk_version: current, system will use sdk_version: system_current, etc.
Paul Duffin87a05a32020-05-12 11:50:28 +0100354 //
355 // This does not affect the sdk_version used for either generating the stubs source
356 // or the API file. They both have to use the same sdk_version as is used for
357 // compiling the implementation library.
358 Sdk_version *string
Paul Duffin3375e352020-04-28 10:44:03 +0100359}
360
Jiyong Parkc678ad32018-04-10 13:07:10 +0900361type sdkLibraryProperties struct {
Anton Hanssonf8ea3722021-09-16 14:24:13 +0100362 // List of source files that are needed to compile the API, but are not part of runtime library.
363 Api_srcs []string `android:"arch_variant"`
364
Paul Duffin5df79302020-05-16 15:52:12 +0100365 // Visibility for impl library module. If not specified then defaults to the
366 // visibility property.
367 Impl_library_visibility []string
368
Paul Duffin4911a892020-04-29 23:35:13 +0100369 // Visibility for stubs library modules. If not specified then defaults to the
370 // visibility property.
371 Stubs_library_visibility []string
372
373 // Visibility for stubs source modules. If not specified then defaults to the
374 // visibility property.
375 Stubs_source_visibility []string
376
Anton Hansson7f66efa2020-10-08 14:47:23 +0100377 // List of Java libraries that will be in the classpath when building the implementation lib
378 Impl_only_libs []string `android:"arch_variant"`
379
Paul Duffina083ec42022-04-28 14:13:30 +0000380 // List of Java libraries that will included in the implementation lib.
381 Impl_only_static_libs []string `android:"arch_variant"`
382
Sundong Ahnf043cf62018-06-25 16:04:37 +0900383 // List of Java libraries that will be in the classpath when building stubs
384 Stub_only_libs []string `android:"arch_variant"`
385
Anton Hanssondae54cd2021-04-21 16:30:10 +0100386 // List of Java libraries that will included in stub libraries
387 Stub_only_static_libs []string `android:"arch_variant"`
388
Paul Duffin7a586d32019-12-30 17:09:34 +0000389 // list of package names that will be documented and publicized as API.
390 // This allows the API to be restricted to a subset of the source files provided.
391 // If this is unspecified then all the source files will be treated as being part
392 // of the API.
Jiyong Parkc678ad32018-04-10 13:07:10 +0900393 Api_packages []string
394
Jiyong Park5a2c9d72018-05-01 22:25:41 +0900395 // list of package names that must be hidden from the API
396 Hidden_api_packages []string
397
Paul Duffin749f98f2019-12-30 17:23:46 +0000398 // the relative path to the directory containing the api specification files.
399 // Defaults to "api".
400 Api_dir *string
401
Paul Duffindfa131e2020-05-15 20:37:11 +0100402 // Determines whether a runtime implementation library is built; defaults to false.
403 //
404 // If true then it also prevents the module from being used as a shared module, i.e.
405 // it is as is shared_library: false, was set.
Paul Duffin43db9be2019-12-30 17:35:49 +0000406 Api_only *bool
407
Paul Duffin11512472019-02-11 15:55:17 +0000408 // local files that are used within user customized droiddoc options.
409 Droiddoc_option_files []string
410
Spandan Das93e95992021-07-29 18:26:39 +0000411 // additional droiddoc options.
Paul Duffin11512472019-02-11 15:55:17 +0000412 // Available variables for substitution:
413 //
414 // $(location <label>): the path to the droiddoc_option_files with name <label>
Sundong Ahndd567f92018-07-31 17:19:11 +0900415 Droiddoc_options []string
416
Paul Duffine22c2ab2020-05-20 19:35:27 +0100417 // is set to true, Metalava will allow framework SDK to contain annotations.
418 Annotations_enabled *bool
419
Sundong Ahn054b19a2018-10-19 13:46:09 +0900420 // a list of top-level directories containing files to merge qualifier annotations
421 // (i.e. those intended to be included in the stubs written) from.
422 Merge_annotations_dirs []string
423
424 // a list of top-level directories containing Java stub files to merge show/hide annotations from.
425 Merge_inclusion_annotations_dirs []string
426
Paul Duffin4f5c1ef2020-11-19 14:53:43 +0000427 // If set to true then don't create dist rules.
428 No_dist *bool
Sundong Ahn80a87b32019-05-13 15:02:50 +0900429
Paul Duffin31310252020-11-20 21:26:20 +0000430 // The stem for the artifacts that are copied to the dist, if not specified
431 // then defaults to the base module name.
432 //
433 // For each scope the following artifacts are copied to the apistubs/<scope>
434 // directory in the dist.
435 // * stubs impl jar -> <dist-stem>.jar
436 // * API specification file -> api/<dist-stem>.txt
437 // * Removed API specification file -> api/<dist-stem>-removed.txt
438 //
439 // Also used to construct the name of the filegroup (created by prebuilt_apis)
440 // that references the latest released API and remove API specification files.
441 // * API specification filegroup -> <dist-stem>.api.<scope>.latest
442 // * Removed API specification filegroup -> <dist-stem>-removed.api.<scope>.latest
Jaewoong Jung1a97ee02021-03-09 13:25:02 -0800443 // * API incompatibilities baseline filegroup -> <dist-stem>-incompatibilities.api.<scope>.latest
Paul Duffin31310252020-11-20 21:26:20 +0000444 Dist_stem *string
445
Colin Cross986b69a2021-06-01 13:13:40 -0700446 // The subdirectory for the artifacts that are copied to the dist directory. If not specified
Colin Cross3dd66252021-06-01 14:05:09 -0700447 // then defaults to "unknown". Should be set to "android" for anything that should be published
Colin Cross986b69a2021-06-01 13:13:40 -0700448 // in the public Android SDK.
449 Dist_group *string
450
Anton Hanssondff2c782020-12-21 17:10:01 +0000451 // A compatibility mode that allows historical API-tracking files to not exist.
452 // Do not use.
453 Unsafe_ignore_missing_latest_api bool
454
Paul Duffin3375e352020-04-28 10:44:03 +0100455 // indicates whether system and test apis should be generated.
456 Generate_system_and_test_apis bool `blueprint:"mutated"`
457
458 // The properties specific to the public api scope
459 //
460 // Unless explicitly specified by using public.enabled the public api scope is
461 // enabled by default in both legacy and non-legacy mode.
462 Public ApiScopeProperties
463
464 // The properties specific to the system api scope
465 //
466 // In legacy mode the system api scope is enabled by default when sdk_version
467 // is set to something other than "none".
468 //
469 // In non-legacy mode the system api scope is disabled by default.
470 System ApiScopeProperties
471
472 // The properties specific to the test api scope
473 //
474 // In legacy mode the test api scope is enabled by default when sdk_version
475 // is set to something other than "none".
476 //
477 // In non-legacy mode the test api scope is disabled by default.
478 Test ApiScopeProperties
Paul Duffin37e0b772019-12-30 17:20:10 +0000479
Paul Duffin0c5bae52020-06-02 13:00:08 +0100480 // The properties specific to the module-lib api scope
Paul Duffin8f265b92020-04-28 14:13:56 +0100481 //
Paul Duffin0c5bae52020-06-02 13:00:08 +0100482 // Unless explicitly specified by using test.enabled the module-lib api scope is
Paul Duffin8f265b92020-04-28 14:13:56 +0100483 // disabled by default.
484 Module_lib ApiScopeProperties
485
Paul Duffin0c5bae52020-06-02 13:00:08 +0100486 // The properties specific to the system-server api scope
487 //
488 // Unless explicitly specified by using test.enabled the module-lib api scope is
489 // disabled by default.
490 System_server ApiScopeProperties
491
Jiyong Park932cdfe2020-05-28 00:19:53 +0900492 // Determines if the stubs are preferred over the implementation library
493 // for linking, even when the client doesn't specify sdk_version. When this
494 // is set to true, such clients are provided with the widest API surface that
495 // this lib provides. Note however that this option doesn't affect the clients
496 // that are in the same APEX as this library. In that case, the clients are
497 // always linked with the implementation library. Default is false.
498 Default_to_stubs *bool
499
Paul Duffin160fe412020-05-10 19:32:20 +0100500 // Properties related to api linting.
501 Api_lint struct {
502 // Enable api linting.
503 Enabled *bool
504 }
505
Jiyong Parkc678ad32018-04-10 13:07:10 +0900506 // TODO: determines whether to create HTML doc or not
507 //Html_doc *bool
508}
509
Paul Duffin0f8faff2020-05-20 16:18:00 +0100510// Paths to outputs from java_sdk_library and java_sdk_library_import.
511//
512// Fields that are android.Paths are always set (during GenerateAndroidBuildActions).
513// OptionalPaths are always set by java_sdk_library but may not be set by
514// java_sdk_library_import as not all instances provide that information.
Paul Duffind1b3a922020-01-22 11:57:20 +0000515type scopePaths struct {
Paul Duffin0f8faff2020-05-20 16:18:00 +0100516 // The path (represented as Paths for convenience when returning) to the stubs header jar.
517 //
518 // That is the jar that is created by turbine.
519 stubsHeaderPath android.Paths
520
521 // The path (represented as Paths for convenience when returning) to the stubs implementation jar.
522 //
523 // This is not the implementation jar, it still only contains stubs.
524 stubsImplPath android.Paths
525
Paul Duffin1267d872021-04-16 17:21:36 +0100526 // The dex jar for the stubs.
527 //
528 // This is not the implementation jar, it still only contains stubs.
Martin Stjernholm8be1e6d2021-09-15 03:34:04 +0100529 stubsDexJarPath OptionalDexJarPath
Paul Duffin1267d872021-04-16 17:21:36 +0100530
Paul Duffin0f8faff2020-05-20 16:18:00 +0100531 // The API specification file, e.g. system_current.txt.
532 currentApiFilePath android.OptionalPath
533
534 // The specification of API elements removed since the last release.
535 removedApiFilePath android.OptionalPath
536
537 // The stubs source jar.
538 stubsSrcJar android.OptionalPath
Anton Hanssond78eb762021-09-21 15:25:12 +0100539
540 // Extracted annotations.
541 annotationsZip android.OptionalPath
Paul Duffind1b3a922020-01-22 11:57:20 +0000542}
543
Colin Crossdcf71b22021-02-01 13:59:03 -0800544func (paths *scopePaths) extractStubsLibraryInfoFromDependency(ctx android.ModuleContext, dep android.Module) error {
545 if ctx.OtherModuleHasProvider(dep, JavaInfoProvider) {
546 lib := ctx.OtherModuleProvider(dep, JavaInfoProvider).(JavaInfo)
547 paths.stubsHeaderPath = lib.HeaderJars
548 paths.stubsImplPath = lib.ImplementationJars
Paul Duffin1267d872021-04-16 17:21:36 +0100549
550 libDep := dep.(UsesLibraryDependency)
551 paths.stubsDexJarPath = libDep.DexJarBuildPath()
Paul Duffinc8782502020-04-29 20:45:27 +0100552 return nil
553 } else {
Colin Crossdcf71b22021-02-01 13:59:03 -0800554 return fmt.Errorf("expected module that has JavaInfoProvider, e.g. java_library")
Paul Duffinc8782502020-04-29 20:45:27 +0100555 }
556}
557
Paul Duffin0ff08bd2020-04-29 13:30:54 +0100558func (paths *scopePaths) treatDepAsApiStubsProvider(dep android.Module, action func(provider ApiStubsProvider)) error {
559 if apiStubsProvider, ok := dep.(ApiStubsProvider); ok {
560 action(apiStubsProvider)
Paul Duffinc8782502020-04-29 20:45:27 +0100561 return nil
562 } else {
563 return fmt.Errorf("expected module that implements ApiStubsProvider, e.g. droidstubs")
564 }
565}
566
Paul Duffin0f8faff2020-05-20 16:18:00 +0100567func (paths *scopePaths) treatDepAsApiStubsSrcProvider(dep android.Module, action func(provider ApiStubsSrcProvider)) error {
568 if apiStubsProvider, ok := dep.(ApiStubsSrcProvider); ok {
569 action(apiStubsProvider)
570 return nil
571 } else {
572 return fmt.Errorf("expected module that implements ApiStubsSrcProvider, e.g. droidstubs")
573 }
574}
575
Paul Duffin0ff08bd2020-04-29 13:30:54 +0100576func (paths *scopePaths) extractApiInfoFromApiStubsProvider(provider ApiStubsProvider) {
Anton Hanssond78eb762021-09-21 15:25:12 +0100577 paths.annotationsZip = android.OptionalPathForPath(provider.AnnotationsZip())
Paul Duffin0f8faff2020-05-20 16:18:00 +0100578 paths.currentApiFilePath = android.OptionalPathForPath(provider.ApiFilePath())
579 paths.removedApiFilePath = android.OptionalPathForPath(provider.RemovedApiFilePath())
Paul Duffin0ff08bd2020-04-29 13:30:54 +0100580}
581
Colin Crossdcf71b22021-02-01 13:59:03 -0800582func (paths *scopePaths) extractApiInfoFromDep(ctx android.ModuleContext, dep android.Module) error {
Paul Duffin0ff08bd2020-04-29 13:30:54 +0100583 return paths.treatDepAsApiStubsProvider(dep, func(provider ApiStubsProvider) {
584 paths.extractApiInfoFromApiStubsProvider(provider)
585 })
586}
587
Paul Duffin0f8faff2020-05-20 16:18:00 +0100588func (paths *scopePaths) extractStubsSourceInfoFromApiStubsProviders(provider ApiStubsSrcProvider) {
589 paths.stubsSrcJar = android.OptionalPathForPath(provider.StubsSrcJar())
Paul Duffin0ff08bd2020-04-29 13:30:54 +0100590}
591
Colin Crossdcf71b22021-02-01 13:59:03 -0800592func (paths *scopePaths) extractStubsSourceInfoFromDep(ctx android.ModuleContext, dep android.Module) error {
Paul Duffin0f8faff2020-05-20 16:18:00 +0100593 return paths.treatDepAsApiStubsSrcProvider(dep, func(provider ApiStubsSrcProvider) {
Paul Duffin0ff08bd2020-04-29 13:30:54 +0100594 paths.extractStubsSourceInfoFromApiStubsProviders(provider)
595 })
596}
597
Colin Crossdcf71b22021-02-01 13:59:03 -0800598func (paths *scopePaths) extractStubsSourceAndApiInfoFromApiStubsProvider(ctx android.ModuleContext, dep android.Module) error {
Paul Duffin0ff08bd2020-04-29 13:30:54 +0100599 return paths.treatDepAsApiStubsProvider(dep, func(provider ApiStubsProvider) {
600 paths.extractApiInfoFromApiStubsProvider(provider)
601 paths.extractStubsSourceInfoFromApiStubsProviders(provider)
602 })
603}
604
605type commonToSdkLibraryAndImportProperties struct {
Paul Duffin1b1e8062020-05-08 13:44:43 +0100606 // The naming scheme to use for the components that this module creates.
607 //
Paul Duffinee9ad5d2020-09-11 13:04:05 +0100608 // If not specified then it defaults to "default".
Paul Duffin1b1e8062020-05-08 13:44:43 +0100609 //
610 // This is a temporary mechanism to simplify conversion from separate modules for each
611 // component that follow a different naming pattern to the default one.
612 //
613 // TODO(b/155480189) - Remove once naming inconsistencies have been resolved.
Paul Duffin0ff08bd2020-04-29 13:30:54 +0100614 Naming_scheme *string
Paul Duffindfa131e2020-05-15 20:37:11 +0100615
616 // Specifies whether this module can be used as an Android shared library; defaults
617 // to true.
618 //
619 // An Android shared library is one that can be referenced in a <uses-library> element
620 // in an AndroidManifest.xml.
621 Shared_library *bool
Paul Duffina2ae7e02020-09-11 11:55:00 +0100622
623 // Files containing information about supported java doc tags.
624 Doctag_files []string `android:"path"`
Pedro Loureiro9956e5e2021-09-07 17:21:59 +0000625
626 // Signals that this shared library is part of the bootclasspath starting
627 // on the version indicated in this attribute.
628 //
629 // This will make platforms at this level and above to ignore
630 // <uses-library> tags with this library name because the library is already
631 // available
632 On_bootclasspath_since *string
633
634 // Signals that this shared library was part of the bootclasspath before
635 // (but not including) the version indicated in this attribute.
636 //
637 // The system will automatically add a <uses-library> tag with this library to
638 // apps that target any SDK less than the version indicated in this attribute.
639 On_bootclasspath_before *string
640
641 // Indicates that PackageManager should ignore this shared library if the
642 // platform is below the version indicated in this attribute.
643 //
644 // This means that the device won't recognise this library as installed.
645 Min_device_sdk *string
646
647 // Indicates that PackageManager should ignore this shared library if the
648 // platform is above the version indicated in this attribute.
649 //
650 // This means that the device won't recognise this library as installed.
651 Max_device_sdk *string
Paul Duffin0ff08bd2020-04-29 13:30:54 +0100652}
653
Paul Duffin71b33cc2021-06-23 11:39:47 +0100654// commonSdkLibraryAndImportModule defines the interface that must be provided by a module that
655// embeds the commonToSdkLibraryAndImport struct.
656type commonSdkLibraryAndImportModule interface {
Paul Duffinb97b1572021-04-29 21:50:40 +0100657 android.SdkAware
Paul Duffin71b33cc2021-06-23 11:39:47 +0100658
659 BaseModuleName() string
660}
661
Paul Duffin56d44902020-01-31 13:36:25 +0000662// Common code between sdk library and sdk library import
663type commonToSdkLibraryAndImport struct {
Paul Duffin71b33cc2021-06-23 11:39:47 +0100664 module commonSdkLibraryAndImportModule
Paul Duffinc3091c82020-05-08 14:16:20 +0100665
Paul Duffin56d44902020-01-31 13:36:25 +0000666 scopePaths map[*apiScope]*scopePaths
Paul Duffin1b1e8062020-05-08 13:44:43 +0100667
668 namingScheme sdkLibraryComponentNamingScheme
669
Paul Duffindfa131e2020-05-15 20:37:11 +0100670 commonSdkLibraryProperties commonToSdkLibraryAndImportProperties
Paul Duffin859fe962020-05-15 10:20:31 +0100671
Paul Duffina2ae7e02020-09-11 11:55:00 +0100672 // Paths to commonSdkLibraryProperties.Doctag_files
673 doctagPaths android.Paths
674
Paul Duffin859fe962020-05-15 10:20:31 +0100675 // Functionality related to this being used as a component of a java_sdk_library.
676 EmbeddableSdkLibraryComponent
Paul Duffin56d44902020-01-31 13:36:25 +0000677}
678
Paul Duffin71b33cc2021-06-23 11:39:47 +0100679func (c *commonToSdkLibraryAndImport) initCommon(module commonSdkLibraryAndImportModule) {
680 c.module = module
Paul Duffin1b1e8062020-05-08 13:44:43 +0100681
Paul Duffin71b33cc2021-06-23 11:39:47 +0100682 module.AddProperties(&c.commonSdkLibraryProperties)
Paul Duffin859fe962020-05-15 10:20:31 +0100683
684 // Initialize this as an sdk library component.
Paul Duffin71b33cc2021-06-23 11:39:47 +0100685 c.initSdkLibraryComponent(module)
Paul Duffin1b1e8062020-05-08 13:44:43 +0100686}
687
688func (c *commonToSdkLibraryAndImport) initCommonAfterDefaultsApplied(ctx android.DefaultableHookContext) bool {
Paul Duffindfa131e2020-05-15 20:37:11 +0100689 schemeProperty := proptools.StringDefault(c.commonSdkLibraryProperties.Naming_scheme, "default")
Paul Duffin1b1e8062020-05-08 13:44:43 +0100690 switch schemeProperty {
691 case "default":
692 c.namingScheme = &defaultNamingScheme{}
693 default:
694 ctx.PropertyErrorf("naming_scheme", "expected 'default' but was %q", schemeProperty)
695 return false
696 }
697
Paul Duffin3f0290e2021-06-30 18:25:36 +0100698 namePtr := proptools.StringPtr(c.module.BaseModuleName())
699 c.sdkLibraryComponentProperties.SdkLibraryName = namePtr
700
Paul Duffindfa131e2020-05-15 20:37:11 +0100701 // Only track this sdk library if this can be used as a shared library.
702 if c.sharedLibrary() {
703 // Use the name specified in the module definition as the owner.
Paul Duffin3f0290e2021-06-30 18:25:36 +0100704 c.sdkLibraryComponentProperties.SdkLibraryToImplicitlyTrack = namePtr
Paul Duffindfa131e2020-05-15 20:37:11 +0100705 }
Paul Duffin859fe962020-05-15 10:20:31 +0100706
Paul Duffin1b1e8062020-05-08 13:44:43 +0100707 return true
Paul Duffinc3091c82020-05-08 14:16:20 +0100708}
709
Paul Duffinea8f8082021-06-24 13:25:57 +0100710// uniqueApexVariations provides common implementation of the ApexModule.UniqueApexVariations
711// method.
712func (c *commonToSdkLibraryAndImport) uniqueApexVariations() bool {
713 // A java_sdk_library that is a shared library produces an XML file that makes the shared library
714 // usable from an AndroidManifest.xml's <uses-library> entry. That XML file contains the name of
715 // the APEX and so it needs a unique variation per APEX.
716 return c.sharedLibrary()
717}
718
Paul Duffina2ae7e02020-09-11 11:55:00 +0100719func (c *commonToSdkLibraryAndImport) generateCommonBuildActions(ctx android.ModuleContext) {
720 c.doctagPaths = android.PathsForModuleSrc(ctx, c.commonSdkLibraryProperties.Doctag_files)
721}
722
Paul Duffineedc5d52020-06-12 17:46:39 +0100723// Module name of the runtime implementation library
724func (c *commonToSdkLibraryAndImport) implLibraryModuleName() string {
Paul Duffin71b33cc2021-06-23 11:39:47 +0100725 return c.module.BaseModuleName() + ".impl"
Paul Duffineedc5d52020-06-12 17:46:39 +0100726}
727
728// Module name of the XML file for the lib
729func (c *commonToSdkLibraryAndImport) xmlPermissionsModuleName() string {
Paul Duffin71b33cc2021-06-23 11:39:47 +0100730 return c.module.BaseModuleName() + sdkXmlFileSuffix
Paul Duffineedc5d52020-06-12 17:46:39 +0100731}
732
Paul Duffinc3091c82020-05-08 14:16:20 +0100733// Name of the java_library module that compiles the stubs source.
734func (c *commonToSdkLibraryAndImport) stubsLibraryModuleName(apiScope *apiScope) string {
Paul Duffinb97b1572021-04-29 21:50:40 +0100735 baseName := c.module.BaseModuleName()
736 return c.module.SdkMemberComponentName(baseName, func(name string) string {
737 return c.namingScheme.stubsLibraryModuleName(apiScope, name)
738 })
Paul Duffinc3091c82020-05-08 14:16:20 +0100739}
740
741// Name of the droidstubs module that generates the stubs source and may also
742// generate/check the API.
743func (c *commonToSdkLibraryAndImport) stubsSourceModuleName(apiScope *apiScope) string {
Paul Duffinb97b1572021-04-29 21:50:40 +0100744 baseName := c.module.BaseModuleName()
745 return c.module.SdkMemberComponentName(baseName, func(name string) string {
746 return c.namingScheme.stubsSourceModuleName(apiScope, name)
747 })
Paul Duffinc3091c82020-05-08 14:16:20 +0100748}
749
Paul Duffin46dc45a2020-05-14 15:39:10 +0100750// The component names for different outputs of the java_sdk_library.
751//
752// They are similar to the names used for the child modules it creates
753const (
754 stubsSourceComponentName = "stubs.source"
755
756 apiTxtComponentName = "api.txt"
757
758 removedApiTxtComponentName = "removed-api.txt"
Anton Hanssond78eb762021-09-21 15:25:12 +0100759
760 annotationsComponentName = "annotations.zip"
Paul Duffin46dc45a2020-05-14 15:39:10 +0100761)
762
763// A regular expression to match tags that reference a specific stubs component.
764//
765// It will only match if given a valid scope and a valid component. It is verfy strict
766// to ensure it does not accidentally match a similar looking tag that should be processed
767// by the embedded Library.
768var tagSplitter = func() *regexp.Regexp {
769 // Given a list of literal string items returns a regular expression that will
770 // match any one of the items.
771 choice := func(items ...string) string {
772 return `\Q` + strings.Join(items, `\E|\Q`) + `\E`
773 }
774
775 // Regular expression to match one of the scopes.
776 scopesRegexp := choice(allScopeNames...)
777
778 // Regular expression to match one of the components.
Anton Hanssond78eb762021-09-21 15:25:12 +0100779 componentsRegexp := choice(stubsSourceComponentName, apiTxtComponentName, removedApiTxtComponentName, annotationsComponentName)
Paul Duffin46dc45a2020-05-14 15:39:10 +0100780
781 // Regular expression to match any combination of one scope and one component.
782 return regexp.MustCompile(fmt.Sprintf(`^\.(%s)\.(%s)$`, scopesRegexp, componentsRegexp))
783}()
784
785// For OutputFileProducer interface
786//
Anton Hanssond78eb762021-09-21 15:25:12 +0100787// .<scope>.<component name>, for all ComponentNames (for example: .public.removed-api.txt)
Paul Duffin46dc45a2020-05-14 15:39:10 +0100788func (c *commonToSdkLibraryAndImport) commonOutputFiles(tag string) (android.Paths, error) {
789 if groups := tagSplitter.FindStringSubmatch(tag); groups != nil {
790 scopeName := groups[1]
791 component := groups[2]
792
793 if scope, ok := scopeByName[scopeName]; ok {
794 paths := c.findScopePaths(scope)
795 if paths == nil {
Paul Duffin71b33cc2021-06-23 11:39:47 +0100796 return nil, fmt.Errorf("%q does not provide api scope %s", c.module.BaseModuleName(), scopeName)
Paul Duffin46dc45a2020-05-14 15:39:10 +0100797 }
798
799 switch component {
800 case stubsSourceComponentName:
801 if paths.stubsSrcJar.Valid() {
802 return android.Paths{paths.stubsSrcJar.Path()}, nil
803 }
804
805 case apiTxtComponentName:
806 if paths.currentApiFilePath.Valid() {
807 return android.Paths{paths.currentApiFilePath.Path()}, nil
808 }
809
810 case removedApiTxtComponentName:
811 if paths.removedApiFilePath.Valid() {
812 return android.Paths{paths.removedApiFilePath.Path()}, nil
813 }
Anton Hanssond78eb762021-09-21 15:25:12 +0100814
815 case annotationsComponentName:
816 if paths.annotationsZip.Valid() {
817 return android.Paths{paths.annotationsZip.Path()}, nil
818 }
Paul Duffin46dc45a2020-05-14 15:39:10 +0100819 }
820
821 return nil, fmt.Errorf("%s not available for api scope %s", component, scopeName)
822 } else {
823 return nil, fmt.Errorf("unknown scope %s in %s", scope, tag)
824 }
825
826 } else {
Paul Duffina2ae7e02020-09-11 11:55:00 +0100827 switch tag {
828 case ".doctags":
829 if c.doctagPaths != nil {
830 return c.doctagPaths, nil
831 } else {
Paul Duffin71b33cc2021-06-23 11:39:47 +0100832 return nil, fmt.Errorf("no doctag_files specified on %s", c.module.BaseModuleName())
Paul Duffina2ae7e02020-09-11 11:55:00 +0100833 }
834 }
Paul Duffin46dc45a2020-05-14 15:39:10 +0100835 return nil, nil
836 }
837}
838
Paul Duffin803a9562020-05-20 11:52:25 +0100839func (c *commonToSdkLibraryAndImport) getScopePathsCreateIfNeeded(scope *apiScope) *scopePaths {
Paul Duffin56d44902020-01-31 13:36:25 +0000840 if c.scopePaths == nil {
841 c.scopePaths = make(map[*apiScope]*scopePaths)
842 }
843 paths := c.scopePaths[scope]
844 if paths == nil {
845 paths = &scopePaths{}
846 c.scopePaths[scope] = paths
847 }
848
849 return paths
850}
851
Paul Duffin803a9562020-05-20 11:52:25 +0100852func (c *commonToSdkLibraryAndImport) findScopePaths(scope *apiScope) *scopePaths {
853 if c.scopePaths == nil {
854 return nil
855 }
856
857 return c.scopePaths[scope]
858}
859
860// If this does not support the requested api scope then find the closest available
861// scope it does support. Returns nil if no such scope is available.
862func (c *commonToSdkLibraryAndImport) findClosestScopePath(scope *apiScope) *scopePaths {
863 for s := scope; s != nil; s = s.extends {
864 if paths := c.findScopePaths(s); paths != nil {
865 return paths
866 }
867 }
868
869 // This should never happen outside tests as public should be the base scope for every
870 // scope and is enabled by default.
871 return nil
872}
873
Jiyong Parkf1691d22021-03-29 20:11:58 +0900874func (c *commonToSdkLibraryAndImport) selectHeaderJarsForSdkVersion(ctx android.BaseModuleContext, sdkVersion android.SdkSpec) android.Paths {
Paul Duffinb05d4292020-05-20 12:19:10 +0100875
876 // If a specific numeric version has been requested then use prebuilt versions of the sdk.
Jiyong Park54105c42021-03-31 18:17:53 +0900877 if !sdkVersion.ApiLevel.IsPreview() {
Paul Duffin71b33cc2021-06-23 11:39:47 +0100878 return PrebuiltJars(ctx, c.module.BaseModuleName(), sdkVersion)
Paul Duffinb05d4292020-05-20 12:19:10 +0100879 }
880
Paul Duffin1267d872021-04-16 17:21:36 +0100881 paths := c.selectScopePaths(ctx, sdkVersion.Kind)
882 if paths == nil {
883 return nil
884 }
885
886 return paths.stubsHeaderPath
887}
888
889// selectScopePaths returns the *scopePaths appropriate for the specific kind.
890//
891// If the module does not support the specific kind then it will return the *scopePaths for the
892// closest kind which is a subset of the requested kind. e.g. if requesting android.SdkModule then
893// it will return *scopePaths for android.SdkSystem if available or android.SdkPublic of not.
894func (c *commonToSdkLibraryAndImport) selectScopePaths(ctx android.BaseModuleContext, kind android.SdkKind) *scopePaths {
Paul Duffin32cf58a2021-05-18 16:32:50 +0100895 apiScope := sdkKindToApiScope(kind)
Paul Duffinb05d4292020-05-20 12:19:10 +0100896
Paul Duffin803a9562020-05-20 11:52:25 +0100897 paths := c.findClosestScopePath(apiScope)
898 if paths == nil {
899 var scopes []string
900 for _, s := range allApiScopes {
901 if c.findScopePaths(s) != nil {
902 scopes = append(scopes, s.name)
903 }
904 }
Paul Duffin71b33cc2021-06-23 11:39:47 +0100905 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 +0100906 return nil
907 }
908
Paul Duffin1267d872021-04-16 17:21:36 +0100909 return paths
910}
911
Paul Duffin32cf58a2021-05-18 16:32:50 +0100912// sdkKindToApiScope maps from android.SdkKind to apiScope.
913func sdkKindToApiScope(kind android.SdkKind) *apiScope {
914 var apiScope *apiScope
915 switch kind {
916 case android.SdkSystem:
917 apiScope = apiScopeSystem
918 case android.SdkModule:
919 apiScope = apiScopeModuleLib
920 case android.SdkTest:
921 apiScope = apiScopeTest
922 case android.SdkSystemServer:
923 apiScope = apiScopeSystemServer
924 default:
925 apiScope = apiScopePublic
926 }
927 return apiScope
928}
929
Paul Duffin1267d872021-04-16 17:21:36 +0100930// to satisfy SdkLibraryDependency interface
Martin Stjernholm8be1e6d2021-09-15 03:34:04 +0100931func (c *commonToSdkLibraryAndImport) SdkApiStubDexJar(ctx android.BaseModuleContext, kind android.SdkKind) OptionalDexJarPath {
Paul Duffin1267d872021-04-16 17:21:36 +0100932 paths := c.selectScopePaths(ctx, kind)
933 if paths == nil {
Martin Stjernholm8be1e6d2021-09-15 03:34:04 +0100934 return makeUnsetDexJarPath()
Paul Duffin1267d872021-04-16 17:21:36 +0100935 }
936
937 return paths.stubsDexJarPath
Paul Duffinb05d4292020-05-20 12:19:10 +0100938}
939
Paul Duffin32cf58a2021-05-18 16:32:50 +0100940// to satisfy SdkLibraryDependency interface
941func (c *commonToSdkLibraryAndImport) SdkRemovedTxtFile(ctx android.BaseModuleContext, kind android.SdkKind) android.OptionalPath {
942 apiScope := sdkKindToApiScope(kind)
943 paths := c.findScopePaths(apiScope)
944 if paths == nil {
945 return android.OptionalPath{}
946 }
947
948 return paths.removedApiFilePath
949}
950
Paul Duffin859fe962020-05-15 10:20:31 +0100951func (c *commonToSdkLibraryAndImport) sdkComponentPropertiesForChildLibrary() interface{} {
952 componentProps := &struct {
Paul Duffin3f0290e2021-06-30 18:25:36 +0100953 SdkLibraryName *string
Paul Duffin859fe962020-05-15 10:20:31 +0100954 SdkLibraryToImplicitlyTrack *string
Paul Duffindfa131e2020-05-15 20:37:11 +0100955 }{}
956
Paul Duffin3f0290e2021-06-30 18:25:36 +0100957 namePtr := proptools.StringPtr(c.module.BaseModuleName())
958 componentProps.SdkLibraryName = namePtr
959
Paul Duffindfa131e2020-05-15 20:37:11 +0100960 if c.sharedLibrary() {
Paul Duffin859fe962020-05-15 10:20:31 +0100961 // Mark the stubs library as being components of this java_sdk_library so that
962 // any app that includes code which depends (directly or indirectly) on the stubs
963 // library will have the appropriate <uses-library> invocation inserted into its
964 // manifest if necessary.
Paul Duffin3f0290e2021-06-30 18:25:36 +0100965 componentProps.SdkLibraryToImplicitlyTrack = namePtr
Paul Duffin859fe962020-05-15 10:20:31 +0100966 }
967
968 return componentProps
969}
970
Paul Duffindfa131e2020-05-15 20:37:11 +0100971func (c *commonToSdkLibraryAndImport) sharedLibrary() bool {
972 return proptools.BoolDefault(c.commonSdkLibraryProperties.Shared_library, true)
973}
974
Paul Duffinf4600f62021-05-13 22:34:45 +0100975// Check if the stub libraries should be compiled for dex
976func (c *commonToSdkLibraryAndImport) stubLibrariesCompiledForDex() bool {
977 // Always compile the dex file files for the stub libraries if they will be used on the
978 // bootclasspath.
979 return !c.sharedLibrary()
980}
981
Paul Duffin859fe962020-05-15 10:20:31 +0100982// Properties related to the use of a module as an component of a java_sdk_library.
983type SdkLibraryComponentProperties struct {
Paul Duffin3f0290e2021-06-30 18:25:36 +0100984 // The name of the java_sdk_library/_import module.
985 SdkLibraryName *string `blueprint:"mutated"`
Paul Duffin859fe962020-05-15 10:20:31 +0100986
987 // The name of the java_sdk_library/_import to add to a <uses-library> entry
988 // in the AndroidManifest.xml of any Android app that includes code that references
989 // this module. If not set then no java_sdk_library/_import is tracked.
990 SdkLibraryToImplicitlyTrack *string `blueprint:"mutated"`
991}
992
993// Structure to be embedded in a module struct that needs to support the
994// SdkLibraryComponentDependency interface.
995type EmbeddableSdkLibraryComponent struct {
996 sdkLibraryComponentProperties SdkLibraryComponentProperties
997}
998
Paul Duffin71b33cc2021-06-23 11:39:47 +0100999func (e *EmbeddableSdkLibraryComponent) initSdkLibraryComponent(module android.Module) {
1000 module.AddProperties(&e.sdkLibraryComponentProperties)
Paul Duffin859fe962020-05-15 10:20:31 +01001001}
1002
1003// to satisfy SdkLibraryComponentDependency
Paul Duffin3f0290e2021-06-30 18:25:36 +01001004func (e *EmbeddableSdkLibraryComponent) SdkLibraryName() *string {
1005 return e.sdkLibraryComponentProperties.SdkLibraryName
1006}
1007
1008// to satisfy SdkLibraryComponentDependency
Ulya Trafimovich39b437b2020-09-23 16:42:35 +01001009func (e *EmbeddableSdkLibraryComponent) OptionalSdkLibraryImplementation() *string {
Ulya Trafimovich78645fb2021-07-16 15:29:25 +01001010 // For shared libraries, this is the same as the SDK library name. If a Java library or app
1011 // depends on a component library (e.g. a stub library) it still needs to know the name of the
1012 // run-time library and the corresponding module that provides the implementation. This name is
1013 // passed to manifest_fixer (to be added to AndroidManifest.xml) and added to CLC (to be used
1014 // in dexpreopt).
1015 //
1016 // For non-shared SDK (component or not) libraries this returns `nil`, as they are not
1017 // <uses-library> and should not be added to the manifest or to CLC.
Ulya Trafimovich39b437b2020-09-23 16:42:35 +01001018 return e.sdkLibraryComponentProperties.SdkLibraryToImplicitlyTrack
1019}
1020
Paul Duffin859fe962020-05-15 10:20:31 +01001021// Implemented by modules that are (or possibly could be) a component of a java_sdk_library
1022// (including the java_sdk_library) itself.
1023type SdkLibraryComponentDependency interface {
Ulya Trafimovich31e444e2020-08-14 17:32:16 +01001024 UsesLibraryDependency
1025
Paul Duffin3f0290e2021-06-30 18:25:36 +01001026 // SdkLibraryName returns the name of the java_sdk_library/_import module.
1027 SdkLibraryName() *string
1028
Ulya Trafimovich39b437b2020-09-23 16:42:35 +01001029 // The name of the implementation library for the optional SDK library or nil, if there isn't one.
1030 OptionalSdkLibraryImplementation() *string
Paul Duffin859fe962020-05-15 10:20:31 +01001031}
1032
1033// Make sure that all the module types that are components of java_sdk_library/_import
1034// and which can be referenced (directly or indirectly) from an android app implement
1035// the SdkLibraryComponentDependency interface.
1036var _ SdkLibraryComponentDependency = (*Library)(nil)
1037var _ SdkLibraryComponentDependency = (*Import)(nil)
1038var _ SdkLibraryComponentDependency = (*SdkLibrary)(nil)
Paul Duffineedc5d52020-06-12 17:46:39 +01001039var _ SdkLibraryComponentDependency = (*SdkLibraryImport)(nil)
Paul Duffin859fe962020-05-15 10:20:31 +01001040
Paul Duffin32cf58a2021-05-18 16:32:50 +01001041// Provides access to sdk_version related files, e.g. header and implementation jars.
Paul Duffin859fe962020-05-15 10:20:31 +01001042type SdkLibraryDependency interface {
1043 SdkLibraryComponentDependency
1044
1045 // Get the header jars appropriate for the supplied sdk_version.
1046 //
1047 // These are turbine generated jars so they only change if the externals of the
1048 // class changes but it does not contain and implementation or JavaDoc.
Jiyong Parkf1691d22021-03-29 20:11:58 +09001049 SdkHeaderJars(ctx android.BaseModuleContext, sdkVersion android.SdkSpec) android.Paths
Paul Duffin859fe962020-05-15 10:20:31 +01001050
1051 // Get the implementation jars appropriate for the supplied sdk version.
1052 //
1053 // These are either the implementation jar for the whole sdk library or the implementation
1054 // jars for the stubs. The latter should only be needed when generating JavaDoc as otherwise
1055 // they are identical to the corresponding header jars.
Jiyong Parkf1691d22021-03-29 20:11:58 +09001056 SdkImplementationJars(ctx android.BaseModuleContext, sdkVersion android.SdkSpec) android.Paths
Paul Duffin1267d872021-04-16 17:21:36 +01001057
1058 // SdkApiStubDexJar returns the dex jar for the stubs. It is needed by the hiddenapi processing
1059 // tool which processes dex files.
Martin Stjernholm8be1e6d2021-09-15 03:34:04 +01001060 SdkApiStubDexJar(ctx android.BaseModuleContext, kind android.SdkKind) OptionalDexJarPath
Paul Duffinf4600f62021-05-13 22:34:45 +01001061
Paul Duffin32cf58a2021-05-18 16:32:50 +01001062 // SdkRemovedTxtFile returns the optional path to the removed.txt file for the specified sdk kind.
1063 SdkRemovedTxtFile(ctx android.BaseModuleContext, kind android.SdkKind) android.OptionalPath
1064
Paul Duffinf4600f62021-05-13 22:34:45 +01001065 // sharedLibrary returns true if this can be used as a shared library.
1066 sharedLibrary() bool
Paul Duffin859fe962020-05-15 10:20:31 +01001067}
1068
Inseob Kimc0907f12019-02-08 21:00:45 +09001069type SdkLibrary struct {
Sundong Ahn054b19a2018-10-19 13:46:09 +09001070 Library
Jiyong Parkc678ad32018-04-10 13:07:10 +09001071
Sundong Ahn054b19a2018-10-19 13:46:09 +09001072 sdkLibraryProperties sdkLibraryProperties
Jiyong Parkc678ad32018-04-10 13:07:10 +09001073
Paul Duffin3375e352020-04-28 10:44:03 +01001074 // Map from api scope to the scope specific property structure.
1075 scopeToProperties map[*apiScope]*ApiScopeProperties
1076
Paul Duffin56d44902020-01-31 13:36:25 +00001077 commonToSdkLibraryAndImport
Jiyong Parkc678ad32018-04-10 13:07:10 +09001078}
1079
Inseob Kimc0907f12019-02-08 21:00:45 +09001080var _ SdkLibraryDependency = (*SdkLibrary)(nil)
Colin Cross897d2ed2019-02-11 14:03:51 -08001081
Paul Duffin3375e352020-04-28 10:44:03 +01001082func (module *SdkLibrary) generateTestAndSystemScopesByDefault() bool {
1083 return module.sdkLibraryProperties.Generate_system_and_test_apis
1084}
1085
1086func (module *SdkLibrary) getGeneratedApiScopes(ctx android.EarlyModuleContext) apiScopes {
1087 // Check to see if any scopes have been explicitly enabled. If any have then all
1088 // must be.
1089 anyScopesExplicitlyEnabled := false
1090 for _, scope := range allApiScopes {
1091 scopeProperties := module.scopeToProperties[scope]
1092 if scopeProperties.Enabled != nil {
1093 anyScopesExplicitlyEnabled = true
1094 break
1095 }
Paul Duffind1b3a922020-01-22 11:57:20 +00001096 }
Paul Duffin3375e352020-04-28 10:44:03 +01001097
1098 var generatedScopes apiScopes
1099 enabledScopes := make(map[*apiScope]struct{})
1100 for _, scope := range allApiScopes {
1101 scopeProperties := module.scopeToProperties[scope]
1102 // If any scopes are explicitly enabled then ignore the legacy enabled status.
1103 // This is to ensure that any new usages of this module type do not rely on legacy
1104 // behaviour.
1105 defaultEnabledStatus := false
1106 if anyScopesExplicitlyEnabled {
1107 defaultEnabledStatus = scope.defaultEnabledStatus
1108 } else {
1109 defaultEnabledStatus = scope.legacyEnabledStatus(module)
1110 }
1111 enabled := proptools.BoolDefault(scopeProperties.Enabled, defaultEnabledStatus)
1112 if enabled {
1113 enabledScopes[scope] = struct{}{}
1114 generatedScopes = append(generatedScopes, scope)
1115 }
1116 }
1117
1118 // Now check to make sure that any scope that is extended by an enabled scope is also
1119 // enabled.
1120 for _, scope := range allApiScopes {
1121 if _, ok := enabledScopes[scope]; ok {
1122 extends := scope.extends
1123 if extends != nil {
1124 if _, ok := enabledScopes[extends]; !ok {
1125 ctx.ModuleErrorf("enabled api scope %q depends on disabled scope %q", scope, extends)
1126 }
1127 }
1128 }
1129 }
1130
1131 return generatedScopes
Paul Duffind1b3a922020-01-22 11:57:20 +00001132}
1133
satayev758968a2021-12-06 11:42:40 +00001134var _ android.ModuleWithMinSdkVersionCheck = (*SdkLibrary)(nil)
1135
satayev8f088b02021-12-06 11:40:46 +00001136func (module *SdkLibrary) CheckMinSdkVersion(ctx android.ModuleContext) {
1137 android.CheckMinSdkVersion(ctx, module.MinSdkVersion(ctx).ApiLevel, func(c android.ModuleContext, do android.PayloadDepsCallback) {
1138 ctx.WalkDeps(func(child android.Module, parent android.Module) bool {
1139 isExternal := !module.depIsInSameApex(ctx, child)
1140 if am, ok := child.(android.ApexModule); ok {
1141 if !do(ctx, parent, am, isExternal) {
1142 return false
1143 }
1144 }
1145 return !isExternal
1146 })
1147 })
1148}
1149
Paul Duffineedc5d52020-06-12 17:46:39 +01001150type sdkLibraryComponentTag struct {
1151 blueprint.BaseDependencyTag
1152 name string
1153}
1154
1155// Mark this tag so dependencies that use it are excluded from visibility enforcement.
1156func (t sdkLibraryComponentTag) ExcludeFromVisibilityEnforcement() {}
1157
1158var xmlPermissionsFileTag = sdkLibraryComponentTag{name: "xml-permissions-file"}
Paul Duffine74ac732020-02-06 13:51:46 +00001159
Jiyong Parke3833882020-02-17 17:28:10 +09001160func IsXmlPermissionsFileDepTag(depTag blueprint.DependencyTag) bool {
Paul Duffineedc5d52020-06-12 17:46:39 +01001161 if dt, ok := depTag.(sdkLibraryComponentTag); ok {
Jiyong Parke3833882020-02-17 17:28:10 +09001162 return dt == xmlPermissionsFileTag
1163 }
1164 return false
1165}
1166
Paul Duffineedc5d52020-06-12 17:46:39 +01001167var implLibraryTag = sdkLibraryComponentTag{name: "impl-library"}
Paul Duffin5df79302020-05-16 15:52:12 +01001168
Paul Duffin44f1d842020-06-26 20:17:02 +01001169// Add the dependencies on the child modules in the component deps mutator.
1170func (module *SdkLibrary) ComponentDepsMutator(ctx android.BottomUpMutatorContext) {
Paul Duffin3375e352020-04-28 10:44:03 +01001171 for _, apiScope := range module.getGeneratedApiScopes(ctx) {
Paul Duffind1b3a922020-01-22 11:57:20 +00001172 // Add dependencies to the stubs library
Paul Duffinc3091c82020-05-08 14:16:20 +01001173 ctx.AddVariationDependencies(nil, apiScope.stubsTag, module.stubsLibraryModuleName(apiScope))
Paul Duffind1b3a922020-01-22 11:57:20 +00001174
Paul Duffin15f34ef2020-07-20 18:04:44 +01001175 // Add a dependency on the stubs source in order to access both stubs source and api information.
1176 ctx.AddVariationDependencies(nil, apiScope.stubsSourceAndApiTag, module.stubsSourceModuleName(apiScope))
Sundong Ahn054b19a2018-10-19 13:46:09 +09001177 }
1178
Paul Duffindfa131e2020-05-15 20:37:11 +01001179 if module.requiresRuntimeImplementationLibrary() {
Paul Duffin5df79302020-05-16 15:52:12 +01001180 // Add dependency to the rule for generating the implementation library.
1181 ctx.AddDependency(module, implLibraryTag, module.implLibraryModuleName())
1182
Paul Duffindfa131e2020-05-15 20:37:11 +01001183 if module.sharedLibrary() {
1184 // Add dependency to the rule for generating the xml permissions file
Paul Duffineedc5d52020-06-12 17:46:39 +01001185 ctx.AddDependency(module, xmlPermissionsFileTag, module.xmlPermissionsModuleName())
Paul Duffindfa131e2020-05-15 20:37:11 +01001186 }
Paul Duffin44f1d842020-06-26 20:17:02 +01001187 }
1188}
Paul Duffine74ac732020-02-06 13:51:46 +00001189
Paul Duffin44f1d842020-06-26 20:17:02 +01001190// Add other dependencies as normal.
1191func (module *SdkLibrary) DepsMutator(ctx android.BottomUpMutatorContext) {
Anton Hanssone77fccc2021-01-20 16:52:41 +00001192 var missingApiModules []string
1193 for _, apiScope := range module.getGeneratedApiScopes(ctx) {
1194 if apiScope.unstable {
1195 continue
1196 }
1197 if m := android.SrcIsModule(module.latestApiFilegroupName(apiScope)); !ctx.OtherModuleExists(m) {
1198 missingApiModules = append(missingApiModules, m)
1199 }
1200 if m := android.SrcIsModule(module.latestRemovedApiFilegroupName(apiScope)); !ctx.OtherModuleExists(m) {
1201 missingApiModules = append(missingApiModules, m)
1202 }
Jaewoong Jung1a97ee02021-03-09 13:25:02 -08001203 if m := android.SrcIsModule(module.latestIncompatibilitiesFilegroupName(apiScope)); !ctx.OtherModuleExists(m) {
1204 missingApiModules = append(missingApiModules, m)
1205 }
Anton Hanssone77fccc2021-01-20 16:52:41 +00001206 }
1207 if len(missingApiModules) != 0 && !module.sdkLibraryProperties.Unsafe_ignore_missing_latest_api {
1208 m := module.Name() + " is missing tracking files for previously released library versions.\n"
1209 m += "You need to do one of the following:\n"
1210 m += "- Add `unsafe_ignore_missing_latest_api: true` to your blueprint (to disable compat tracking)\n"
1211 m += "- Add a set of prebuilt txt files representing the last released version of this library for compat checking.\n"
1212 m += " (the current set of API files can be used as a seed for this compatibility tracking\n"
1213 m += "\n"
1214 m += "The following filegroup modules are missing:\n "
1215 m += strings.Join(missingApiModules, "\n ") + "\n"
1216 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."
1217 ctx.ModuleErrorf(m)
1218 }
Paul Duffin44f1d842020-06-26 20:17:02 +01001219 if module.requiresRuntimeImplementationLibrary() {
Paul Duffindfa131e2020-05-15 20:37:11 +01001220 // Only add the deps for the library if it is actually going to be built.
1221 module.Library.deps(ctx)
1222 }
Jiyong Parkc678ad32018-04-10 13:07:10 +09001223}
1224
Paul Duffin46dc45a2020-05-14 15:39:10 +01001225func (module *SdkLibrary) OutputFiles(tag string) (android.Paths, error) {
1226 paths, err := module.commonOutputFiles(tag)
Colin Cross4acaea92021-12-10 23:05:02 +00001227 if paths != nil || err != nil {
Paul Duffin46dc45a2020-05-14 15:39:10 +01001228 return paths, err
1229 }
Colin Cross4acaea92021-12-10 23:05:02 +00001230 if module.requiresRuntimeImplementationLibrary() {
1231 return module.Library.OutputFiles(tag)
1232 }
1233 if tag == "" {
1234 return nil, nil
1235 }
1236 return nil, fmt.Errorf("unsupported module reference tag %q", tag)
Paul Duffin46dc45a2020-05-14 15:39:10 +01001237}
1238
Inseob Kimc0907f12019-02-08 21:00:45 +09001239func (module *SdkLibrary) GenerateAndroidBuildActions(ctx android.ModuleContext) {
satayev8f088b02021-12-06 11:40:46 +00001240 if proptools.String(module.deviceProperties.Min_sdk_version) != "" {
1241 module.CheckMinSdkVersion(ctx)
1242 }
1243
Paul Duffina2ae7e02020-09-11 11:55:00 +01001244 module.generateCommonBuildActions(ctx)
1245
Paul Duffindfa131e2020-05-15 20:37:11 +01001246 // Only build an implementation library if required.
1247 if module.requiresRuntimeImplementationLibrary() {
Paul Duffin43db9be2019-12-30 17:35:49 +00001248 module.Library.GenerateAndroidBuildActions(ctx)
1249 }
Sundong Ahn054b19a2018-10-19 13:46:09 +09001250
Paul Duffinb97b1572021-04-29 21:50:40 +01001251 // Collate the components exported by this module. All scope specific modules are exported but
1252 // the impl and xml component modules are not.
1253 exportedComponents := map[string]struct{}{}
1254
Sundong Ahn57368eb2018-07-06 11:20:23 +09001255 // Record the paths to the header jars of the library (stubs and impl).
Paul Duffind1b3a922020-01-22 11:57:20 +00001256 // When this java_sdk_library is depended upon from others via "libs" property,
Jiyong Parkc678ad32018-04-10 13:07:10 +09001257 // the recorded paths will be returned depending on the link type of the caller.
1258 ctx.VisitDirectDeps(func(to android.Module) {
Jiyong Parkc678ad32018-04-10 13:07:10 +09001259 tag := ctx.OtherModuleDependencyTag(to)
1260
Paul Duffinc8782502020-04-29 20:45:27 +01001261 // Extract information from any of the scope specific dependencies.
1262 if scopeTag, ok := tag.(scopeDependencyTag); ok {
1263 apiScope := scopeTag.apiScope
Paul Duffin803a9562020-05-20 11:52:25 +01001264 scopePaths := module.getScopePathsCreateIfNeeded(apiScope)
Paul Duffinc8782502020-04-29 20:45:27 +01001265
1266 // Extract information from the dependency. The exact information extracted
1267 // is determined by the nature of the dependency which is determined by the tag.
1268 scopeTag.extractDepInfo(ctx, to, scopePaths)
Paul Duffinb97b1572021-04-29 21:50:40 +01001269
1270 exportedComponents[ctx.OtherModuleName(to)] = struct{}{}
Sundong Ahn20e998b2018-07-24 11:19:26 +09001271 }
Jiyong Parkc678ad32018-04-10 13:07:10 +09001272 })
Paul Duffinb97b1572021-04-29 21:50:40 +01001273
1274 // Make the set of components exported by this module available for use elsewhere.
1275 exportedComponentInfo := android.ExportedComponentsInfo{Components: android.SortedStringKeys(exportedComponents)}
1276 ctx.SetProvider(android.ExportedComponentsInfoProvider, exportedComponentInfo)
Jiyong Parkc678ad32018-04-10 13:07:10 +09001277}
1278
Jiyong Park0b0e1b92019-12-03 13:24:29 +09001279func (module *SdkLibrary) AndroidMkEntries() []android.AndroidMkEntries {
Paul Duffindfa131e2020-05-15 20:37:11 +01001280 if !module.requiresRuntimeImplementationLibrary() {
Paul Duffin43db9be2019-12-30 17:35:49 +00001281 return nil
1282 }
Jiyong Park0b0e1b92019-12-03 13:24:29 +09001283 entriesList := module.Library.AndroidMkEntries()
Yo Chiang07d75072020-06-05 17:43:19 +08001284 if module.sharedLibrary() {
1285 entries := &entriesList[0]
1286 entries.Required = append(entries.Required, module.xmlPermissionsModuleName())
1287 }
Jiyong Park0b0e1b92019-12-03 13:24:29 +09001288 return entriesList
Jiyong Park82484c02018-04-23 21:41:26 +09001289}
1290
Anton Hansson5fd5d242020-03-27 19:43:19 +00001291// The dist path of the stub artifacts
1292func (module *SdkLibrary) apiDistPath(apiScope *apiScope) string {
Colin Crossf0eace92021-06-02 13:02:23 -07001293 return path.Join("apistubs", module.distGroup(), apiScope.name)
Anton Hansson5fd5d242020-03-27 19:43:19 +00001294}
1295
Paul Duffin12ceb462019-12-24 20:31:31 +00001296// Get the sdk version for use when compiling the stubs library.
Paul Duffin780c5f42020-05-12 15:52:55 +01001297func (module *SdkLibrary) sdkVersionForStubsLibrary(mctx android.EarlyModuleContext, apiScope *apiScope) string {
Paul Duffin87a05a32020-05-12 11:50:28 +01001298 scopeProperties := module.scopeToProperties[apiScope]
1299 if scopeProperties.Sdk_version != nil {
1300 return proptools.String(scopeProperties.Sdk_version)
1301 }
1302
Jiyong Parkf1691d22021-03-29 20:11:58 +09001303 sdkDep := decodeSdkDep(mctx, android.SdkContext(&module.Library))
Paul Duffin12ceb462019-12-24 20:31:31 +00001304 if sdkDep.hasStandardLibs() {
1305 // If building against a standard sdk then use the sdk version appropriate for the scope.
Paul Duffind1b3a922020-01-22 11:57:20 +00001306 return apiScope.sdkVersion
Paul Duffin12ceb462019-12-24 20:31:31 +00001307 } else {
1308 // Otherwise, use no system module.
1309 return "none"
1310 }
1311}
1312
Paul Duffin31310252020-11-20 21:26:20 +00001313func (module *SdkLibrary) distStem() string {
1314 return proptools.StringDefault(module.sdkLibraryProperties.Dist_stem, module.BaseModuleName())
1315}
1316
Colin Cross986b69a2021-06-01 13:13:40 -07001317// distGroup returns the subdirectory of the dist path of the stub artifacts.
1318func (module *SdkLibrary) distGroup() string {
Colin Cross59b92bf2021-06-01 14:07:56 -07001319 return proptools.StringDefault(module.sdkLibraryProperties.Dist_group, "unknown")
Colin Cross986b69a2021-06-01 13:13:40 -07001320}
1321
Paul Duffind1b3a922020-01-22 11:57:20 +00001322func (module *SdkLibrary) latestApiFilegroupName(apiScope *apiScope) string {
Paul Duffin31310252020-11-20 21:26:20 +00001323 return ":" + module.distStem() + ".api." + apiScope.name + ".latest"
Jiyong Park58c518b2018-05-12 22:29:12 +09001324}
Jiyong Parkc678ad32018-04-10 13:07:10 +09001325
Paul Duffind1b3a922020-01-22 11:57:20 +00001326func (module *SdkLibrary) latestRemovedApiFilegroupName(apiScope *apiScope) string {
Paul Duffin31310252020-11-20 21:26:20 +00001327 return ":" + module.distStem() + "-removed.api." + apiScope.name + ".latest"
Jiyong Parkc678ad32018-04-10 13:07:10 +09001328}
1329
Jaewoong Jung1a97ee02021-03-09 13:25:02 -08001330func (module *SdkLibrary) latestIncompatibilitiesFilegroupName(apiScope *apiScope) string {
1331 return ":" + module.distStem() + "-incompatibilities.api." + apiScope.name + ".latest"
1332}
1333
Anton Hansson944e77d2020-08-19 11:40:22 +01001334func childModuleVisibility(childVisibility []string) []string {
1335 if childVisibility == nil {
1336 // No child visibility set. The child will use the visibility of the sdk_library.
1337 return nil
1338 }
1339
1340 // Prepend an override to ignore the sdk_library's visibility, and rely on the child visibility.
1341 var visibility []string
1342 visibility = append(visibility, "//visibility:override")
1343 visibility = append(visibility, childVisibility...)
1344 return visibility
1345}
1346
Paul Duffin5df79302020-05-16 15:52:12 +01001347// Creates the implementation java library
1348func (module *SdkLibrary) createImplLibrary(mctx android.DefaultableHookContext) {
Anton Hansson944e77d2020-08-19 11:40:22 +01001349 visibility := childModuleVisibility(module.sdkLibraryProperties.Impl_library_visibility)
1350
Paul Duffin5df79302020-05-16 15:52:12 +01001351 props := struct {
Paul Duffina083ec42022-04-28 14:13:30 +00001352 Name *string
1353 Visibility []string
1354 Instrument bool
1355 Libs []string
1356 Static_libs []string
1357 Apex_available []string
Paul Duffin5df79302020-05-16 15:52:12 +01001358 }{
1359 Name: proptools.StringPtr(module.implLibraryModuleName()),
Anton Hansson944e77d2020-08-19 11:40:22 +01001360 Visibility: visibility,
Paul Duffin296cf332020-06-18 21:09:55 +01001361 // Set the instrument property to ensure it is instrumented when instrumentation is required.
1362 Instrument: true,
Anton Hansson7f66efa2020-10-08 14:47:23 +01001363 // Set the impl_only libs. Note that the module's "Libs" get appended as well, via the
1364 // addition of &module.properties below.
1365 Libs: module.sdkLibraryProperties.Impl_only_libs,
Paul Duffina083ec42022-04-28 14:13:30 +00001366 // Set the impl_only static libs. Note that the module's "static_libs" get appended as well, via the
1367 // addition of &module.properties below.
1368 Static_libs: module.sdkLibraryProperties.Impl_only_static_libs,
1369 // Pass the apex_available settings down so that the impl library can be statically
1370 // embedded within a library that is added to an APEX. Needed for updatable-media.
1371 Apex_available: module.ApexAvailable(),
Paul Duffin5df79302020-05-16 15:52:12 +01001372 }
1373
1374 properties := []interface{}{
1375 &module.properties,
1376 &module.protoProperties,
1377 &module.deviceProperties,
Liz Kammera7a64f32020-07-09 15:16:41 -07001378 &module.dexProperties,
Paul Duffin5df79302020-05-16 15:52:12 +01001379 &module.dexpreoptProperties,
Colin Cross014489c2020-06-02 20:09:13 -07001380 &module.linter.properties,
Paul Duffin5df79302020-05-16 15:52:12 +01001381 &props,
1382 module.sdkComponentPropertiesForChildLibrary(),
1383 }
1384 mctx.CreateModule(LibraryFactory, properties...)
1385}
1386
Jiyong Parkc678ad32018-04-10 13:07:10 +09001387// Creates a static java library that has API stubs
Paul Duffinf0229202020-04-29 16:47:28 +01001388func (module *SdkLibrary) createStubsLibrary(mctx android.DefaultableHookContext, apiScope *apiScope) {
Jiyong Parkc678ad32018-04-10 13:07:10 +09001389 props := struct {
Dan Willemsen9f435972020-05-28 15:28:00 -07001390 Name *string
1391 Visibility []string
1392 Srcs []string
1393 Installable *bool
1394 Sdk_version *string
1395 System_modules *string
1396 Patch_module *string
1397 Libs []string
Anton Hanssondae54cd2021-04-21 16:30:10 +01001398 Static_libs []string
Dan Willemsen9f435972020-05-28 15:28:00 -07001399 Compile_dex *bool
1400 Java_version *string
1401 Openjdk9 struct {
Sundong Ahn054b19a2018-10-19 13:46:09 +09001402 Srcs []string
1403 Javacflags []string
1404 }
Anton Hansson5fd5d242020-03-27 19:43:19 +00001405 Dist struct {
1406 Targets []string
1407 Dest *string
1408 Dir *string
1409 Tag *string
1410 }
Jiyong Parkc678ad32018-04-10 13:07:10 +09001411 }{}
1412
Paul Duffinc3091c82020-05-08 14:16:20 +01001413 props.Name = proptools.StringPtr(module.stubsLibraryModuleName(apiScope))
Anton Hansson944e77d2020-08-19 11:40:22 +01001414 props.Visibility = childModuleVisibility(module.sdkLibraryProperties.Stubs_library_visibility)
Jiyong Parkc678ad32018-04-10 13:07:10 +09001415 // sources are generated from the droiddoc
Paul Duffinc3091c82020-05-08 14:16:20 +01001416 props.Srcs = []string{":" + module.stubsSourceModuleName(apiScope)}
Paul Duffin12ceb462019-12-24 20:31:31 +00001417 sdkVersion := module.sdkVersionForStubsLibrary(mctx, apiScope)
Paul Duffin52d398a2019-06-11 12:31:14 +01001418 props.Sdk_version = proptools.StringPtr(sdkVersion)
Paul Duffina18abc22020-05-16 18:54:24 +01001419 props.System_modules = module.deviceProperties.System_modules
1420 props.Patch_module = module.properties.Patch_module
Paul Duffin367ab912019-12-23 19:40:36 +00001421 props.Installable = proptools.BoolPtr(false)
Sundong Ahn054b19a2018-10-19 13:46:09 +09001422 props.Libs = module.sdkLibraryProperties.Stub_only_libs
Anton Hanssondae54cd2021-04-21 16:30:10 +01001423 props.Static_libs = module.sdkLibraryProperties.Stub_only_static_libs
Paul Duffine22c2ab2020-05-20 19:35:27 +01001424 // The stub-annotations library contains special versions of the annotations
1425 // with CLASS retention policy, so that they're kept.
1426 if proptools.Bool(module.sdkLibraryProperties.Annotations_enabled) {
1427 props.Libs = append(props.Libs, "stub-annotations")
1428 }
Paul Duffina18abc22020-05-16 18:54:24 +01001429 props.Openjdk9.Srcs = module.properties.Openjdk9.Srcs
1430 props.Openjdk9.Javacflags = module.properties.Openjdk9.Javacflags
Anton Hansson83509b52020-05-21 09:21:57 +01001431 // We compile the stubs for 1.8 in line with the main android.jar stubs, and potential
1432 // interop with older developer tools that don't support 1.9.
1433 props.Java_version = proptools.StringPtr("1.8")
Paul Duffinf4600f62021-05-13 22:34:45 +01001434
1435 // The imports need to be compiled to dex if the java_sdk_library requests it.
1436 compileDex := module.dexProperties.Compile_dex
1437 if module.stubLibrariesCompiledForDex() {
1438 compileDex = proptools.BoolPtr(true)
Sundong Ahndd567f92018-07-31 17:19:11 +09001439 }
Paul Duffinf4600f62021-05-13 22:34:45 +01001440 props.Compile_dex = compileDex
Jiyong Parkc678ad32018-04-10 13:07:10 +09001441
Anton Hansson5fd5d242020-03-27 19:43:19 +00001442 // Dist the class jar artifact for sdk builds.
1443 if !Bool(module.sdkLibraryProperties.No_dist) {
1444 props.Dist.Targets = []string{"sdk", "win_sdk"}
Paul Duffin31310252020-11-20 21:26:20 +00001445 props.Dist.Dest = proptools.StringPtr(fmt.Sprintf("%v.jar", module.distStem()))
Anton Hansson5fd5d242020-03-27 19:43:19 +00001446 props.Dist.Dir = proptools.StringPtr(module.apiDistPath(apiScope))
1447 props.Dist.Tag = proptools.StringPtr(".jar")
1448 }
Jiyong Parkc678ad32018-04-10 13:07:10 +09001449
Paul Duffin859fe962020-05-15 10:20:31 +01001450 mctx.CreateModule(LibraryFactory, &props, module.sdkComponentPropertiesForChildLibrary())
Jiyong Parkc678ad32018-04-10 13:07:10 +09001451}
1452
Paul Duffin6d0886e2020-04-07 18:49:53 +01001453// Creates a droidstubs module that creates stubs source files from the given full source
Paul Duffinc8782502020-04-29 20:45:27 +01001454// files and also updates and checks the API specification files.
Paul Duffin15f34ef2020-07-20 18:04:44 +01001455func (module *SdkLibrary) createStubsSourcesAndApi(mctx android.DefaultableHookContext, apiScope *apiScope, name string, scopeSpecificDroidstubsArgs []string) {
Jiyong Parkc678ad32018-04-10 13:07:10 +09001456 props := struct {
Sundong Ahn054b19a2018-10-19 13:46:09 +09001457 Name *string
Paul Duffin4911a892020-04-29 23:35:13 +01001458 Visibility []string
Sundong Ahn054b19a2018-10-19 13:46:09 +09001459 Srcs []string
1460 Installable *bool
Paul Duffin52d398a2019-06-11 12:31:14 +01001461 Sdk_version *string
Paul Duffin12ceb462019-12-24 20:31:31 +00001462 System_modules *string
Sundong Ahn054b19a2018-10-19 13:46:09 +09001463 Libs []string
Paul Duffin6877e6d2020-09-25 19:59:14 +01001464 Output_javadoc_comments *bool
Paul Duffin11512472019-02-11 15:55:17 +00001465 Arg_files []string
Sundong Ahn054b19a2018-10-19 13:46:09 +09001466 Args *string
Sundong Ahn054b19a2018-10-19 13:46:09 +09001467 Java_version *string
Paul Duffine22c2ab2020-05-20 19:35:27 +01001468 Annotations_enabled *bool
Sundong Ahn054b19a2018-10-19 13:46:09 +09001469 Merge_annotations_dirs []string
1470 Merge_inclusion_annotations_dirs []string
Paul Duffin0ff08bd2020-04-29 13:30:54 +01001471 Generate_stubs *bool
Anton Hanssone87b03d2020-12-21 15:29:34 +00001472 Previous_api *string
Sundong Ahn054b19a2018-10-19 13:46:09 +09001473 Check_api struct {
Anton Hanssone6056152020-12-31 10:37:27 +00001474 Current ApiToCheck
1475 Last_released ApiToCheck
Paul Duffin160fe412020-05-10 19:32:20 +01001476
1477 Api_lint struct {
1478 Enabled *bool
1479 New_since *string
1480 Baseline_file *string
1481 }
Jiyong Park58c518b2018-05-12 22:29:12 +09001482 }
Sundong Ahn1b92c822018-05-29 11:35:17 +09001483 Aidl struct {
1484 Include_dirs []string
1485 Local_include_dirs []string
1486 }
Paul Duffin040e9062020-11-23 17:41:36 +00001487 Dists []android.Dist
Jiyong Parkc678ad32018-04-10 13:07:10 +09001488 }{}
1489
Paul Duffin7b78b4d2020-04-28 14:08:32 +01001490 // The stubs source processing uses the same compile time classpath when extracting the
1491 // API from the implementation library as it does when compiling it. i.e. the same
1492 // * sdk version
1493 // * system_modules
1494 // * libs (static_libs/libs)
Paul Duffin250e6192019-06-07 10:44:37 +01001495
Paul Duffin0ff08bd2020-04-29 13:30:54 +01001496 props.Name = proptools.StringPtr(name)
Anton Hansson944e77d2020-08-19 11:40:22 +01001497 props.Visibility = childModuleVisibility(module.sdkLibraryProperties.Stubs_source_visibility)
Paul Duffina18abc22020-05-16 18:54:24 +01001498 props.Srcs = append(props.Srcs, module.properties.Srcs...)
Anton Hanssonf8ea3722021-09-16 14:24:13 +01001499 props.Srcs = append(props.Srcs, module.sdkLibraryProperties.Api_srcs...)
Paul Duffina18abc22020-05-16 18:54:24 +01001500 props.Sdk_version = module.deviceProperties.Sdk_version
1501 props.System_modules = module.deviceProperties.System_modules
Jiyong Parkc678ad32018-04-10 13:07:10 +09001502 props.Installable = proptools.BoolPtr(false)
Sundong Ahne6f0b052018-06-05 16:46:14 +09001503 // A droiddoc module has only one Libs property and doesn't distinguish between
1504 // shared libs and static libs. So we need to add both of these libs to Libs property.
Paul Duffina18abc22020-05-16 18:54:24 +01001505 props.Libs = module.properties.Libs
1506 props.Libs = append(props.Libs, module.properties.Static_libs...)
1507 props.Aidl.Include_dirs = module.deviceProperties.Aidl.Include_dirs
1508 props.Aidl.Local_include_dirs = module.deviceProperties.Aidl.Local_include_dirs
1509 props.Java_version = module.properties.Java_version
Jiyong Parkc678ad32018-04-10 13:07:10 +09001510
Paul Duffine22c2ab2020-05-20 19:35:27 +01001511 props.Annotations_enabled = module.sdkLibraryProperties.Annotations_enabled
Sundong Ahn054b19a2018-10-19 13:46:09 +09001512 props.Merge_annotations_dirs = module.sdkLibraryProperties.Merge_annotations_dirs
1513 props.Merge_inclusion_annotations_dirs = module.sdkLibraryProperties.Merge_inclusion_annotations_dirs
1514
Paul Duffin6d0886e2020-04-07 18:49:53 +01001515 droidstubsArgs := []string{}
Paul Duffin235ffff2019-12-24 10:41:30 +00001516 if len(module.sdkLibraryProperties.Api_packages) != 0 {
Paul Duffin6d0886e2020-04-07 18:49:53 +01001517 droidstubsArgs = append(droidstubsArgs, "--stub-packages "+strings.Join(module.sdkLibraryProperties.Api_packages, ":"))
Paul Duffin235ffff2019-12-24 10:41:30 +00001518 }
1519 if len(module.sdkLibraryProperties.Hidden_api_packages) != 0 {
Paul Duffin6d0886e2020-04-07 18:49:53 +01001520 droidstubsArgs = append(droidstubsArgs,
Paul Duffin235ffff2019-12-24 10:41:30 +00001521 android.JoinWithPrefix(module.sdkLibraryProperties.Hidden_api_packages, " --hide-package "))
1522 }
Paul Duffin6d0886e2020-04-07 18:49:53 +01001523 droidstubsArgs = append(droidstubsArgs, module.sdkLibraryProperties.Droiddoc_options...)
Paul Duffin235ffff2019-12-24 10:41:30 +00001524 disabledWarnings := []string{
Paul Duffin235ffff2019-12-24 10:41:30 +00001525 "BroadcastBehavior",
Paul Duffin235ffff2019-12-24 10:41:30 +00001526 "DeprecationMismatch",
Anton Hansson3c0779a2022-02-18 19:24:30 +00001527 "HiddenSuperclass",
Paul Duffin235ffff2019-12-24 10:41:30 +00001528 "HiddenTypeParameter",
Anton Hansson3c0779a2022-02-18 19:24:30 +00001529 "MissingPermission",
1530 "SdkConstant",
Paul Duffin235ffff2019-12-24 10:41:30 +00001531 "Todo",
1532 "Typo",
Anton Hansson3c0779a2022-02-18 19:24:30 +00001533 "UnavailableSymbol",
Paul Duffin235ffff2019-12-24 10:41:30 +00001534 }
Paul Duffin6d0886e2020-04-07 18:49:53 +01001535 droidstubsArgs = append(droidstubsArgs, android.JoinWithPrefix(disabledWarnings, "--hide "))
Sundong Ahnfb2721f2018-09-17 13:23:09 +09001536
Paul Duffin6877e6d2020-09-25 19:59:14 +01001537 // Output Javadoc comments for public scope.
1538 if apiScope == apiScopePublic {
1539 props.Output_javadoc_comments = proptools.BoolPtr(true)
1540 }
1541
Paul Duffin1fb487d2020-04-07 18:50:10 +01001542 // Add in scope specific arguments.
Paul Duffin0ff08bd2020-04-29 13:30:54 +01001543 droidstubsArgs = append(droidstubsArgs, scopeSpecificDroidstubsArgs...)
Paul Duffin11512472019-02-11 15:55:17 +00001544 props.Arg_files = module.sdkLibraryProperties.Droiddoc_option_files
Paul Duffin6d0886e2020-04-07 18:49:53 +01001545 props.Args = proptools.StringPtr(strings.Join(droidstubsArgs, " "))
Jiyong Parkc678ad32018-04-10 13:07:10 +09001546
Paul Duffin15f34ef2020-07-20 18:04:44 +01001547 // List of APIs identified from the provided source files are created. They are later
1548 // compared against to the not-yet-released (a.k.a current) list of APIs and to the
1549 // last-released (a.k.a numbered) list of API.
1550 currentApiFileName := apiScope.apiFilePrefix + "current.txt"
1551 removedApiFileName := apiScope.apiFilePrefix + "removed.txt"
1552 apiDir := module.getApiDir()
1553 currentApiFileName = path.Join(apiDir, currentApiFileName)
1554 removedApiFileName = path.Join(apiDir, removedApiFileName)
Jiyong Parkc678ad32018-04-10 13:07:10 +09001555
Paul Duffin15f34ef2020-07-20 18:04:44 +01001556 // check against the not-yet-release API
1557 props.Check_api.Current.Api_file = proptools.StringPtr(currentApiFileName)
1558 props.Check_api.Current.Removed_api_file = proptools.StringPtr(removedApiFileName)
Jiyong Park58c518b2018-05-12 22:29:12 +09001559
Anton Hanssone6056152020-12-31 10:37:27 +00001560 if !(apiScope.unstable || module.sdkLibraryProperties.Unsafe_ignore_missing_latest_api) {
Paul Duffin15f34ef2020-07-20 18:04:44 +01001561 // check against the latest released API
1562 latestApiFilegroupName := proptools.StringPtr(module.latestApiFilegroupName(apiScope))
Anton Hanssone87b03d2020-12-21 15:29:34 +00001563 props.Previous_api = latestApiFilegroupName
Paul Duffin15f34ef2020-07-20 18:04:44 +01001564 props.Check_api.Last_released.Api_file = latestApiFilegroupName
1565 props.Check_api.Last_released.Removed_api_file = proptools.StringPtr(
1566 module.latestRemovedApiFilegroupName(apiScope))
Jaewoong Jung1a97ee02021-03-09 13:25:02 -08001567 props.Check_api.Last_released.Baseline_file = proptools.StringPtr(
1568 module.latestIncompatibilitiesFilegroupName(apiScope))
Paul Duffin160fe412020-05-10 19:32:20 +01001569
Paul Duffin15f34ef2020-07-20 18:04:44 +01001570 if proptools.Bool(module.sdkLibraryProperties.Api_lint.Enabled) {
1571 // Enable api lint.
1572 props.Check_api.Api_lint.Enabled = proptools.BoolPtr(true)
1573 props.Check_api.Api_lint.New_since = latestApiFilegroupName
Paul Duffin160fe412020-05-10 19:32:20 +01001574
Paul Duffin15f34ef2020-07-20 18:04:44 +01001575 // If it exists then pass a lint-baseline.txt through to droidstubs.
1576 baselinePath := path.Join(apiDir, apiScope.apiFilePrefix+"lint-baseline.txt")
1577 baselinePathRelativeToRoot := path.Join(mctx.ModuleDir(), baselinePath)
1578 paths, err := mctx.GlobWithDeps(baselinePathRelativeToRoot, nil)
1579 if err != nil {
1580 mctx.ModuleErrorf("error checking for presence of %s: %s", baselinePathRelativeToRoot, err)
1581 }
1582 if len(paths) == 1 {
1583 props.Check_api.Api_lint.Baseline_file = proptools.StringPtr(baselinePath)
1584 } else if len(paths) != 0 {
1585 mctx.ModuleErrorf("error checking for presence of %s: expected one path, found: %v", baselinePathRelativeToRoot, paths)
Paul Duffin160fe412020-05-10 19:32:20 +01001586 }
1587 }
Paul Duffin15f34ef2020-07-20 18:04:44 +01001588 }
Jiyong Park58c518b2018-05-12 22:29:12 +09001589
Paul Duffin15f34ef2020-07-20 18:04:44 +01001590 if !Bool(module.sdkLibraryProperties.No_dist) {
Paul Duffin040e9062020-11-23 17:41:36 +00001591 // Dist the api txt and removed api txt artifacts for sdk builds.
1592 distDir := proptools.StringPtr(path.Join(module.apiDistPath(apiScope), "api"))
1593 for _, p := range []struct {
1594 tag string
1595 pattern string
1596 }{
1597 {tag: ".api.txt", pattern: "%s.txt"},
1598 {tag: ".removed-api.txt", pattern: "%s-removed.txt"},
1599 } {
1600 props.Dists = append(props.Dists, android.Dist{
1601 Targets: []string{"sdk", "win_sdk"},
1602 Dir: distDir,
1603 Dest: proptools.StringPtr(fmt.Sprintf(p.pattern, module.distStem())),
1604 Tag: proptools.StringPtr(p.tag),
1605 })
1606 }
Anton Hansson5fd5d242020-03-27 19:43:19 +00001607 }
1608
Colin Cross84dfc3d2019-09-25 11:33:01 -07001609 mctx.CreateModule(DroidstubsFactory, &props)
Jiyong Parkc678ad32018-04-10 13:07:10 +09001610}
1611
Paul Duffinea8f8082021-06-24 13:25:57 +01001612// Implements android.ApexModule
Jooyung Han5e9013b2020-03-10 06:23:13 +09001613func (module *SdkLibrary) DepIsInSameApex(mctx android.BaseModuleContext, dep android.Module) bool {
1614 depTag := mctx.OtherModuleDependencyTag(dep)
1615 if depTag == xmlPermissionsFileTag {
1616 return true
1617 }
1618 return module.Library.DepIsInSameApex(mctx, dep)
1619}
1620
Paul Duffinea8f8082021-06-24 13:25:57 +01001621// Implements android.ApexModule
1622func (module *SdkLibrary) UniqueApexVariations() bool {
1623 return module.uniqueApexVariations()
1624}
1625
Jiyong Parkc678ad32018-04-10 13:07:10 +09001626// Creates the xml file that publicizes the runtime library
Paul Duffinf0229202020-04-29 16:47:28 +01001627func (module *SdkLibrary) createXmlFile(mctx android.DefaultableHookContext) {
Pedro Loureiroc3621422021-09-28 15:40:23 +00001628 moduleMinApiLevel := module.Library.MinSdkVersion(mctx).ApiLevel
1629 var moduleMinApiLevelStr = moduleMinApiLevel.String()
1630 if moduleMinApiLevel == android.NoneApiLevel {
1631 moduleMinApiLevelStr = "current"
1632 }
Jiyong Parke3833882020-02-17 17:28:10 +09001633 props := struct {
Pedro Loureiroc3621422021-09-28 15:40:23 +00001634 Name *string
1635 Lib_name *string
1636 Apex_available []string
1637 On_bootclasspath_since *string
1638 On_bootclasspath_before *string
1639 Min_device_sdk *string
1640 Max_device_sdk *string
1641 Sdk_library_min_api_level *string
Jiyong Parke3833882020-02-17 17:28:10 +09001642 }{
Pedro Loureiroc3621422021-09-28 15:40:23 +00001643 Name: proptools.StringPtr(module.xmlPermissionsModuleName()),
1644 Lib_name: proptools.StringPtr(module.BaseModuleName()),
1645 Apex_available: module.ApexProperties.Apex_available,
1646 On_bootclasspath_since: module.commonSdkLibraryProperties.On_bootclasspath_since,
1647 On_bootclasspath_before: module.commonSdkLibraryProperties.On_bootclasspath_before,
1648 Min_device_sdk: module.commonSdkLibraryProperties.Min_device_sdk,
1649 Max_device_sdk: module.commonSdkLibraryProperties.Max_device_sdk,
1650 Sdk_library_min_api_level: &moduleMinApiLevelStr,
Jiyong Parkc678ad32018-04-10 13:07:10 +09001651 }
Jiyong Parke3833882020-02-17 17:28:10 +09001652
Jiyong Parke3833882020-02-17 17:28:10 +09001653 mctx.CreateModule(sdkLibraryXmlFactory, &props)
Jiyong Parkc678ad32018-04-10 13:07:10 +09001654}
1655
Jiyong Parkf1691d22021-03-29 20:11:58 +09001656func PrebuiltJars(ctx android.BaseModuleContext, baseName string, s android.SdkSpec) android.Paths {
Jiyong Park54105c42021-03-31 18:17:53 +09001657 var ver android.ApiLevel
Jiyong Parkf1691d22021-03-29 20:11:58 +09001658 var kind android.SdkKind
1659 if s.UsePrebuilt(ctx) {
Jiyong Park54105c42021-03-31 18:17:53 +09001660 ver = s.ApiLevel
Jiyong Parkf1691d22021-03-29 20:11:58 +09001661 kind = s.Kind
Jiyong Parkc678ad32018-04-10 13:07:10 +09001662 } else {
Jiyong Park6a927c42020-01-21 02:03:43 +09001663 // We don't have prebuilt SDK for the specific sdkVersion.
1664 // Instead of breaking the build, fallback to use "system_current"
Jiyong Park54105c42021-03-31 18:17:53 +09001665 ver = android.FutureApiLevel
Jiyong Parkf1691d22021-03-29 20:11:58 +09001666 kind = android.SdkSystem
Sundong Ahn054b19a2018-10-19 13:46:09 +09001667 }
Jiyong Park6a927c42020-01-21 02:03:43 +09001668
1669 dir := filepath.Join("prebuilts", "sdk", ver.String(), kind.String())
Paul Duffin50061512020-01-21 16:31:05 +00001670 jar := filepath.Join(dir, baseName+".jar")
Sundong Ahn054b19a2018-10-19 13:46:09 +09001671 jarPath := android.ExistentPathForSource(ctx, jar)
Sundong Ahnae418ac2019-02-28 15:01:28 +09001672 if !jarPath.Valid() {
Colin Cross07c88562020-01-07 09:34:44 -08001673 if ctx.Config().AllowMissingDependencies() {
1674 return android.Paths{android.PathForSource(ctx, jar)}
1675 } else {
Jiyong Parkf1691d22021-03-29 20:11:58 +09001676 ctx.PropertyErrorf("sdk_library", "invalid sdk version %q, %q does not exist", s.Raw, jar)
Colin Cross07c88562020-01-07 09:34:44 -08001677 }
Sundong Ahnae418ac2019-02-28 15:01:28 +09001678 return nil
1679 }
Sundong Ahn054b19a2018-10-19 13:46:09 +09001680 return android.Paths{jarPath.Path()}
1681}
1682
Colin Crossaede88c2020-08-11 12:17:01 -07001683// 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 +01001684//
1685// If either this or the other module are on the platform then this will return
1686// false.
Colin Cross56a83212020-09-15 18:30:11 -07001687func withinSameApexesAs(ctx android.BaseModuleContext, other android.Module) bool {
1688 apexInfo := ctx.Provider(android.ApexInfoProvider).(android.ApexInfo)
1689 otherApexInfo := ctx.OtherModuleProvider(other, android.ApexInfoProvider).(android.ApexInfo)
Jiyong Parkab50b072021-05-12 17:13:56 +09001690 return len(otherApexInfo.InApexVariants) > 0 && reflect.DeepEqual(apexInfo.InApexVariants, otherApexInfo.InApexVariants)
Paul Duffin9b879592020-05-26 13:21:35 +01001691}
1692
Jiyong Parkf1691d22021-03-29 20:11:58 +09001693func (module *SdkLibrary) sdkJars(ctx android.BaseModuleContext, sdkVersion android.SdkSpec, headerJars bool) android.Paths {
Jiyong Park932cdfe2020-05-28 00:19:53 +09001694 // If the client doesn't set sdk_version, but if this library prefers stubs over
1695 // the impl library, let's provide the widest API surface possible. To do so,
1696 // force override sdk_version to module_current so that the closest possible API
1697 // surface could be found in selectHeaderJarsForSdkVersion
Jiyong Parkf1691d22021-03-29 20:11:58 +09001698 if module.defaultsToStubs() && !sdkVersion.Specified() {
Jiyong Park92315372021-04-02 08:45:46 +09001699 sdkVersion = android.SdkSpecFrom(ctx, "module_current")
Jiyong Park932cdfe2020-05-28 00:19:53 +09001700 }
Paul Duffind1b3a922020-01-22 11:57:20 +00001701
Paul Duffindaaa3322020-05-26 18:13:57 +01001702 // Only provide access to the implementation library if it is actually built.
1703 if module.requiresRuntimeImplementationLibrary() {
1704 // Check any special cases for java_sdk_library.
1705 //
1706 // Only allow access to the implementation library in the following condition:
1707 // * No sdk_version specified on the referencing module.
Paul Duffin9b879592020-05-26 13:21:35 +01001708 // * The referencing module is in the same apex as this.
Jiyong Parkf1691d22021-03-29 20:11:58 +09001709 if sdkVersion.Kind == android.SdkPrivate || withinSameApexesAs(ctx, module) {
Paul Duffindaaa3322020-05-26 18:13:57 +01001710 if headerJars {
1711 return module.HeaderJars()
1712 } else {
1713 return module.ImplementationJars()
1714 }
Sundong Ahn054b19a2018-10-19 13:46:09 +09001715 }
Jiyong Parkc678ad32018-04-10 13:07:10 +09001716 }
Paul Duffinb05d4292020-05-20 12:19:10 +01001717
Paul Duffin23970f42020-05-20 14:20:02 +01001718 return module.selectHeaderJarsForSdkVersion(ctx, sdkVersion)
Jiyong Parkc678ad32018-04-10 13:07:10 +09001719}
1720
Sundong Ahn241cd372018-07-13 16:16:44 +09001721// to satisfy SdkLibraryDependency interface
Jiyong Parkf1691d22021-03-29 20:11:58 +09001722func (module *SdkLibrary) SdkHeaderJars(ctx android.BaseModuleContext, sdkVersion android.SdkSpec) android.Paths {
Paul Duffind1b3a922020-01-22 11:57:20 +00001723 return module.sdkJars(ctx, sdkVersion, true /*headerJars*/)
1724}
1725
1726// to satisfy SdkLibraryDependency interface
Jiyong Parkf1691d22021-03-29 20:11:58 +09001727func (module *SdkLibrary) SdkImplementationJars(ctx android.BaseModuleContext, sdkVersion android.SdkSpec) android.Paths {
Paul Duffind1b3a922020-01-22 11:57:20 +00001728 return module.sdkJars(ctx, sdkVersion, false /*headerJars*/)
Sundong Ahn241cd372018-07-13 16:16:44 +09001729}
1730
Colin Cross571cccf2019-02-04 11:22:08 -08001731var javaSdkLibrariesKey = android.NewOnceKey("javaSdkLibraries")
1732
Jiyong Park82484c02018-04-23 21:41:26 +09001733func javaSdkLibraries(config android.Config) *[]string {
Colin Cross571cccf2019-02-04 11:22:08 -08001734 return config.Once(javaSdkLibrariesKey, func() interface{} {
Jiyong Park82484c02018-04-23 21:41:26 +09001735 return &[]string{}
1736 }).(*[]string)
1737}
1738
Paul Duffin749f98f2019-12-30 17:23:46 +00001739func (module *SdkLibrary) getApiDir() string {
1740 return proptools.StringDefault(module.sdkLibraryProperties.Api_dir, "api")
1741}
1742
Jiyong Parkc678ad32018-04-10 13:07:10 +09001743// For a java_sdk_library module, create internal modules for stubs, docs,
1744// runtime libs and xml file. If requested, the stubs and docs are created twice
1745// once for public API level and once for system API level
Paul Duffinf0229202020-04-29 16:47:28 +01001746func (module *SdkLibrary) CreateInternalModules(mctx android.DefaultableHookContext) {
1747 // If the module has been disabled then don't create any child modules.
1748 if !module.Enabled() {
1749 return
1750 }
1751
Paul Duffina18abc22020-05-16 18:54:24 +01001752 if len(module.properties.Srcs) == 0 {
Inseob Kimc0907f12019-02-08 21:00:45 +09001753 mctx.PropertyErrorf("srcs", "java_sdk_library must specify srcs")
Jooyung Han58f26ab2019-12-18 15:34:32 +09001754 return
Inseob Kimc0907f12019-02-08 21:00:45 +09001755 }
1756
Paul Duffin37e0b772019-12-30 17:20:10 +00001757 // If this builds against standard libraries (i.e. is not part of the core libraries)
Paul Duffin4f5c1ef2020-11-19 14:53:43 +00001758 // then assume it provides both system and test apis.
Jiyong Parkf1691d22021-03-29 20:11:58 +09001759 sdkDep := decodeSdkDep(mctx, android.SdkContext(&module.Library))
Paul Duffin37e0b772019-12-30 17:20:10 +00001760 hasSystemAndTestApis := sdkDep.hasStandardLibs()
Paul Duffin3375e352020-04-28 10:44:03 +01001761 module.sdkLibraryProperties.Generate_system_and_test_apis = hasSystemAndTestApis
Paul Duffin4f5c1ef2020-11-19 14:53:43 +00001762
Jaewoong Jung18aefc12020-12-21 09:11:10 -08001763 missingCurrentApi := false
Inseob Kim8098faa2019-03-18 10:19:51 +09001764
Paul Duffin3375e352020-04-28 10:44:03 +01001765 generatedScopes := module.getGeneratedApiScopes(mctx)
Paul Duffind1b3a922020-01-22 11:57:20 +00001766
Paul Duffin749f98f2019-12-30 17:23:46 +00001767 apiDir := module.getApiDir()
Paul Duffin3375e352020-04-28 10:44:03 +01001768 for _, scope := range generatedScopes {
Inseob Kim8098faa2019-03-18 10:19:51 +09001769 for _, api := range []string{"current.txt", "removed.txt"} {
Paul Duffind1b3a922020-01-22 11:57:20 +00001770 path := path.Join(mctx.ModuleDir(), apiDir, scope.apiFilePrefix+api)
Inseob Kim8098faa2019-03-18 10:19:51 +09001771 p := android.ExistentPathForSource(mctx, path)
1772 if !p.Valid() {
Colin Cross18f840c2021-05-20 17:56:54 -07001773 if mctx.Config().AllowMissingDependencies() {
1774 mctx.AddMissingDependencies([]string{path})
1775 } else {
1776 mctx.ModuleErrorf("Current api file %#v doesn't exist", path)
1777 missingCurrentApi = true
1778 }
Inseob Kim8098faa2019-03-18 10:19:51 +09001779 }
1780 }
1781 }
1782
Jaewoong Jung18aefc12020-12-21 09:11:10 -08001783 if missingCurrentApi {
Inseob Kim8098faa2019-03-18 10:19:51 +09001784 script := "build/soong/scripts/gen-java-current-api-files.sh"
1785 p := android.ExistentPathForSource(mctx, script)
1786
1787 if !p.Valid() {
1788 panic(fmt.Sprintf("script file %s doesn't exist", script))
1789 }
1790
1791 mctx.ModuleErrorf("One or more current api files are missing. "+
1792 "You can update them by:\n"+
Paul Duffin37e0b772019-12-30 17:20:10 +00001793 "%s %q %s && m update-api",
Paul Duffind1b3a922020-01-22 11:57:20 +00001794 script, filepath.Join(mctx.ModuleDir(), apiDir),
Paul Duffin3375e352020-04-28 10:44:03 +01001795 strings.Join(generatedScopes.Strings(func(s *apiScope) string { return s.apiFilePrefix }), " "))
Inseob Kim8098faa2019-03-18 10:19:51 +09001796 return
1797 }
1798
Paul Duffin3375e352020-04-28 10:44:03 +01001799 for _, scope := range generatedScopes {
Paul Duffin15f34ef2020-07-20 18:04:44 +01001800 // Use the stubs source name for legacy reasons.
1801 module.createStubsSourcesAndApi(mctx, scope, module.stubsSourceModuleName(scope), scope.droidstubsArgs)
Paul Duffin0ff08bd2020-04-29 13:30:54 +01001802
Paul Duffind1b3a922020-01-22 11:57:20 +00001803 module.createStubsLibrary(mctx, scope)
Inseob Kimc0907f12019-02-08 21:00:45 +09001804 }
1805
Paul Duffindfa131e2020-05-15 20:37:11 +01001806 if module.requiresRuntimeImplementationLibrary() {
Paul Duffin5df79302020-05-16 15:52:12 +01001807 // Create child module to create an implementation library.
1808 //
1809 // This temporarily creates a second implementation library that can be explicitly
1810 // referenced.
1811 //
1812 // TODO(b/156618935) - update comment once only one implementation library is created.
1813 module.createImplLibrary(mctx)
1814
Paul Duffindfa131e2020-05-15 20:37:11 +01001815 // Only create an XML permissions file that declares the library as being usable
1816 // as a shared library if required.
1817 if module.sharedLibrary() {
1818 module.createXmlFile(mctx)
1819 }
Paul Duffin43db9be2019-12-30 17:35:49 +00001820
1821 // record java_sdk_library modules so that they are exported to make
1822 javaSdkLibraries := javaSdkLibraries(mctx.Config())
1823 javaSdkLibrariesLock.Lock()
1824 defer javaSdkLibrariesLock.Unlock()
1825 *javaSdkLibraries = append(*javaSdkLibraries, module.BaseModuleName())
1826 }
Anton Hansson7f66efa2020-10-08 14:47:23 +01001827
Paul Duffina083ec42022-04-28 14:13:30 +00001828 // Add the impl_only_libs and impl_only_static_libs *after* we're done using them in submodules.
Anton Hansson7f66efa2020-10-08 14:47:23 +01001829 module.properties.Libs = append(module.properties.Libs, module.sdkLibraryProperties.Impl_only_libs...)
Paul Duffina083ec42022-04-28 14:13:30 +00001830 module.properties.Static_libs = append(module.properties.Static_libs, module.sdkLibraryProperties.Impl_only_static_libs...)
Inseob Kimc0907f12019-02-08 21:00:45 +09001831}
1832
1833func (module *SdkLibrary) InitSdkLibraryProperties() {
Colin Crossce6734e2020-06-15 16:09:53 -07001834 module.addHostAndDeviceProperties()
1835 module.AddProperties(&module.sdkLibraryProperties)
Sundong Ahn054b19a2018-10-19 13:46:09 +09001836
Paul Duffin71b33cc2021-06-23 11:39:47 +01001837 module.initSdkLibraryComponent(module)
Paul Duffin859fe962020-05-15 10:20:31 +01001838
Paul Duffina18abc22020-05-16 18:54:24 +01001839 module.properties.Installable = proptools.BoolPtr(true)
1840 module.deviceProperties.IsSDKLibrary = true
Inseob Kimc0907f12019-02-08 21:00:45 +09001841}
Sundong Ahn054b19a2018-10-19 13:46:09 +09001842
Paul Duffindfa131e2020-05-15 20:37:11 +01001843func (module *SdkLibrary) requiresRuntimeImplementationLibrary() bool {
1844 return !proptools.Bool(module.sdkLibraryProperties.Api_only)
1845}
1846
Jiyong Park932cdfe2020-05-28 00:19:53 +09001847func (module *SdkLibrary) defaultsToStubs() bool {
1848 return proptools.Bool(module.sdkLibraryProperties.Default_to_stubs)
1849}
1850
Paul Duffin1b1e8062020-05-08 13:44:43 +01001851// Defines how to name the individual component modules the sdk library creates.
1852type sdkLibraryComponentNamingScheme interface {
1853 stubsLibraryModuleName(scope *apiScope, baseName string) string
1854
1855 stubsSourceModuleName(scope *apiScope, baseName string) string
Paul Duffin1b1e8062020-05-08 13:44:43 +01001856}
1857
1858type defaultNamingScheme struct {
1859}
1860
1861func (s *defaultNamingScheme) stubsLibraryModuleName(scope *apiScope, baseName string) string {
1862 return scope.stubsLibraryModuleName(baseName)
1863}
1864
1865func (s *defaultNamingScheme) stubsSourceModuleName(scope *apiScope, baseName string) string {
1866 return scope.stubsSourceModuleName(baseName)
1867}
1868
Paul Duffin1b1e8062020-05-08 13:44:43 +01001869var _ sdkLibraryComponentNamingScheme = (*defaultNamingScheme)(nil)
1870
Jaewoong Jungbc15e3a2021-03-10 17:02:43 -08001871func moduleStubLinkType(name string) (stub bool, ret sdkLinkType) {
Anton Hansson2d0c1942020-05-25 12:20:51 +01001872 // This suffix-based approach is fragile and could potentially mis-trigger.
1873 // TODO(b/155164730): Clean this up when modules no longer reference sdk_lib stubs directly.
Anton Hansson08f476b2021-04-07 15:32:19 +01001874 if strings.HasSuffix(name, apiScopePublic.stubsLibraryModuleNameSuffix()) {
1875 if name == "hwbinder.stubs" || name == "libcore_private.stubs" {
1876 // Due to a previous bug, these modules were not considered stubs, so we retain that.
1877 return false, javaPlatform
1878 }
Anton Hansson2d0c1942020-05-25 12:20:51 +01001879 return true, javaSdk
1880 }
Anton Hansson08f476b2021-04-07 15:32:19 +01001881 if strings.HasSuffix(name, apiScopeSystem.stubsLibraryModuleNameSuffix()) {
Anton Hansson2d0c1942020-05-25 12:20:51 +01001882 return true, javaSystem
1883 }
Anton Hansson08f476b2021-04-07 15:32:19 +01001884 if strings.HasSuffix(name, apiScopeModuleLib.stubsLibraryModuleNameSuffix()) {
Anton Hansson2d0c1942020-05-25 12:20:51 +01001885 return true, javaModule
1886 }
Anton Hansson08f476b2021-04-07 15:32:19 +01001887 if strings.HasSuffix(name, apiScopeTest.stubsLibraryModuleNameSuffix()) {
Anton Hansson2d0c1942020-05-25 12:20:51 +01001888 return true, javaSystem
1889 }
1890 return false, javaPlatform
1891}
1892
Jaewoong Jung4f158ee2019-07-11 10:05:35 -07001893// java_sdk_library is a special Java library that provides optional platform APIs to apps.
1894// In practice, it can be viewed as a combination of several modules: 1) stubs library that clients
1895// are linked against to, 2) droiddoc module that internally generates API stubs source files,
1896// 3) the real runtime shared library that implements the APIs, and 4) XML file for adding
1897// the runtime lib to the classpath at runtime if requested via <uses-library>.
Inseob Kimc0907f12019-02-08 21:00:45 +09001898func SdkLibraryFactory() android.Module {
1899 module := &SdkLibrary{}
Paul Duffinc3091c82020-05-08 14:16:20 +01001900
1901 // Initialize information common between source and prebuilt.
Paul Duffin71b33cc2021-06-23 11:39:47 +01001902 module.initCommon(module)
Paul Duffinc3091c82020-05-08 14:16:20 +01001903
Inseob Kimc0907f12019-02-08 21:00:45 +09001904 module.InitSdkLibraryProperties()
Jooyung Han58f26ab2019-12-18 15:34:32 +09001905 android.InitApexModule(module)
Paul Duffinb6b89a42021-05-06 16:33:43 +01001906 android.InitSdkAwareModule(module)
Sundong Ahn054b19a2018-10-19 13:46:09 +09001907 InitJavaModule(module, android.HostAndDeviceSupported)
Paul Duffin3375e352020-04-28 10:44:03 +01001908
1909 // Initialize the map from scope to scope specific properties.
1910 scopeToProperties := make(map[*apiScope]*ApiScopeProperties)
1911 for _, scope := range allApiScopes {
1912 scopeToProperties[scope] = scope.scopeSpecificProperties(module)
1913 }
1914 module.scopeToProperties = scopeToProperties
1915
Paul Duffin4911a892020-04-29 23:35:13 +01001916 // Add the properties containing visibility rules so that they are checked.
Paul Duffin5df79302020-05-16 15:52:12 +01001917 android.AddVisibilityProperty(module, "impl_library_visibility", &module.sdkLibraryProperties.Impl_library_visibility)
Paul Duffin4911a892020-04-29 23:35:13 +01001918 android.AddVisibilityProperty(module, "stubs_library_visibility", &module.sdkLibraryProperties.Stubs_library_visibility)
1919 android.AddVisibilityProperty(module, "stubs_source_visibility", &module.sdkLibraryProperties.Stubs_source_visibility)
1920
Paul Duffin1b1e8062020-05-08 13:44:43 +01001921 module.SetDefaultableHook(func(ctx android.DefaultableHookContext) {
Paul Duffindfa131e2020-05-15 20:37:11 +01001922 // If no implementation is required then it cannot be used as a shared library
1923 // either.
1924 if !module.requiresRuntimeImplementationLibrary() {
1925 // If shared_library has been explicitly set to true then it is incompatible
1926 // with api_only: true.
1927 if proptools.Bool(module.commonSdkLibraryProperties.Shared_library) {
1928 ctx.PropertyErrorf("api_only/shared_library", "inconsistent settings, shared_library and api_only cannot both be true")
1929 }
1930 // Set shared_library: false.
1931 module.commonSdkLibraryProperties.Shared_library = proptools.BoolPtr(false)
1932 }
1933
Paul Duffin1b1e8062020-05-08 13:44:43 +01001934 if module.initCommonAfterDefaultsApplied(ctx) {
1935 module.CreateInternalModules(ctx)
1936 }
1937 })
Jiyong Parkc678ad32018-04-10 13:07:10 +09001938 return module
1939}
Colin Cross79c7c262019-04-17 11:11:46 -07001940
1941//
1942// SDK library prebuilts
1943//
1944
Paul Duffin56d44902020-01-31 13:36:25 +00001945// Properties associated with each api scope.
1946type sdkLibraryScopeProperties struct {
Colin Cross79c7c262019-04-17 11:11:46 -07001947 Jars []string `android:"path"`
1948
1949 Sdk_version *string
1950
Colin Cross79c7c262019-04-17 11:11:46 -07001951 // List of shared java libs that this module has dependencies to
1952 Libs []string
Paul Duffin3d1248c2020-04-09 00:10:17 +01001953
Paul Duffinc8782502020-04-29 20:45:27 +01001954 // The stubs source.
Paul Duffin3d1248c2020-04-09 00:10:17 +01001955 Stub_srcs []string `android:"path"`
Paul Duffin1fd005d2020-04-09 01:08:11 +01001956
1957 // The current.txt
Paul Duffin0f8faff2020-05-20 16:18:00 +01001958 Current_api *string `android:"path"`
Paul Duffin1fd005d2020-04-09 01:08:11 +01001959
1960 // The removed.txt
Paul Duffin0f8faff2020-05-20 16:18:00 +01001961 Removed_api *string `android:"path"`
Anton Hanssond78eb762021-09-21 15:25:12 +01001962
1963 // Annotation zip
1964 Annotations *string `android:"path"`
Colin Cross79c7c262019-04-17 11:11:46 -07001965}
1966
Paul Duffin56d44902020-01-31 13:36:25 +00001967type sdkLibraryImportProperties struct {
Paul Duffinfcfd7912020-01-31 17:54:30 +00001968 // List of shared java libs, common to all scopes, that this module has
1969 // dependencies to
1970 Libs []string
Paul Duffin1267d872021-04-16 17:21:36 +01001971
1972 // If set to true, compile dex files for the stubs. Defaults to false.
1973 Compile_dex *bool
Paul Duffin869de142021-07-15 14:14:41 +01001974
1975 // If not empty, classes are restricted to the specified packages and their sub-packages.
Paul Duffin869de142021-07-15 14:14:41 +01001976 Permitted_packages []string
Paul Duffin56d44902020-01-31 13:36:25 +00001977}
1978
Paul Duffineedc5d52020-06-12 17:46:39 +01001979type SdkLibraryImport struct {
Colin Cross79c7c262019-04-17 11:11:46 -07001980 android.ModuleBase
1981 android.DefaultableModuleBase
1982 prebuilt android.Prebuilt
Paul Duffindd46f712020-02-10 13:37:10 +00001983 android.ApexModuleBase
1984 android.SdkBase
Colin Cross79c7c262019-04-17 11:11:46 -07001985
Paul Duffin37856732021-02-26 14:24:15 +00001986 hiddenAPI
Jiakai Zhang204356f2021-09-09 08:12:46 +00001987 dexpreopter
Paul Duffin37856732021-02-26 14:24:15 +00001988
Colin Cross79c7c262019-04-17 11:11:46 -07001989 properties sdkLibraryImportProperties
1990
Paul Duffin46a26a82020-04-07 19:27:04 +01001991 // Map from api scope to the scope specific property structure.
1992 scopeProperties map[*apiScope]*sdkLibraryScopeProperties
1993
Paul Duffin56d44902020-01-31 13:36:25 +00001994 commonToSdkLibraryAndImport
Paul Duffineedc5d52020-06-12 17:46:39 +01001995
1996 // The reference to the implementation library created by the source module.
1997 // Is nil if the source module does not exist.
1998 implLibraryModule *Library
1999
2000 // The reference to the xml permissions module created by the source module.
2001 // Is nil if the source module does not exist.
2002 xmlPermissionsFileModule *sdkLibraryXml
Paul Duffin39853512021-02-26 11:09:39 +00002003
Jeongik Chad5fe8782021-07-08 01:13:11 +09002004 // Build path to the dex implementation jar obtained from the prebuilt_apex, if any.
Martin Stjernholm8be1e6d2021-09-15 03:34:04 +01002005 dexJarFile OptionalDexJarPath
Jeongik Chad5fe8782021-07-08 01:13:11 +09002006
2007 // Expected install file path of the source module(sdk_library)
2008 // or dex implementation jar obtained from the prebuilt_apex, if any.
2009 installFile android.Path
Colin Cross79c7c262019-04-17 11:11:46 -07002010}
2011
Paul Duffineedc5d52020-06-12 17:46:39 +01002012var _ SdkLibraryDependency = (*SdkLibraryImport)(nil)
Colin Cross79c7c262019-04-17 11:11:46 -07002013
Paul Duffin46a26a82020-04-07 19:27:04 +01002014// The type of a structure that contains a field of type sdkLibraryScopeProperties
2015// for each apiscope in allApiScopes, e.g. something like:
2016// struct {
2017// Public sdkLibraryScopeProperties
2018// System sdkLibraryScopeProperties
2019// ...
2020// }
2021var allScopeStructType = createAllScopePropertiesStructType()
2022
2023// Dynamically create a structure type for each apiscope in allApiScopes.
2024func createAllScopePropertiesStructType() reflect.Type {
2025 var fields []reflect.StructField
2026 for _, apiScope := range allApiScopes {
2027 field := reflect.StructField{
2028 Name: apiScope.fieldName,
2029 Type: reflect.TypeOf(sdkLibraryScopeProperties{}),
2030 }
2031 fields = append(fields, field)
2032 }
2033
2034 return reflect.StructOf(fields)
2035}
2036
2037// Create an instance of the scope specific structure type and return a map
2038// from apiscope to a pointer to each scope specific field.
2039func createPropertiesInstance() (interface{}, map[*apiScope]*sdkLibraryScopeProperties) {
2040 allScopePropertiesPtr := reflect.New(allScopeStructType)
2041 allScopePropertiesStruct := allScopePropertiesPtr.Elem()
2042 scopeProperties := make(map[*apiScope]*sdkLibraryScopeProperties)
2043
2044 for _, apiScope := range allApiScopes {
2045 field := allScopePropertiesStruct.FieldByName(apiScope.fieldName)
2046 scopeProperties[apiScope] = field.Addr().Interface().(*sdkLibraryScopeProperties)
2047 }
2048
2049 return allScopePropertiesPtr.Interface(), scopeProperties
2050}
2051
Jaewoong Jung4f158ee2019-07-11 10:05:35 -07002052// java_sdk_library_import imports a prebuilt java_sdk_library.
Colin Cross79c7c262019-04-17 11:11:46 -07002053func sdkLibraryImportFactory() android.Module {
Paul Duffineedc5d52020-06-12 17:46:39 +01002054 module := &SdkLibraryImport{}
Colin Cross79c7c262019-04-17 11:11:46 -07002055
Paul Duffin46a26a82020-04-07 19:27:04 +01002056 allScopeProperties, scopeToProperties := createPropertiesInstance()
2057 module.scopeProperties = scopeToProperties
2058 module.AddProperties(&module.properties, allScopeProperties)
Colin Cross79c7c262019-04-17 11:11:46 -07002059
Paul Duffinc3091c82020-05-08 14:16:20 +01002060 // Initialize information common between source and prebuilt.
Paul Duffin71b33cc2021-06-23 11:39:47 +01002061 module.initCommon(module)
Paul Duffinc3091c82020-05-08 14:16:20 +01002062
Paul Duffin0bdcb272020-02-06 15:24:57 +00002063 android.InitPrebuiltModule(module, &[]string{""})
Paul Duffindd46f712020-02-10 13:37:10 +00002064 android.InitApexModule(module)
2065 android.InitSdkAwareModule(module)
Colin Cross79c7c262019-04-17 11:11:46 -07002066 InitJavaModule(module, android.HostAndDeviceSupported)
2067
Paul Duffin1b1e8062020-05-08 13:44:43 +01002068 module.SetDefaultableHook(func(mctx android.DefaultableHookContext) {
2069 if module.initCommonAfterDefaultsApplied(mctx) {
2070 module.createInternalModules(mctx)
2071 }
2072 })
Colin Cross79c7c262019-04-17 11:11:46 -07002073 return module
2074}
2075
Paul Duffin630b11e2021-07-15 13:35:26 +01002076var _ PermittedPackagesForUpdatableBootJars = (*SdkLibraryImport)(nil)
2077
2078func (module *SdkLibraryImport) PermittedPackagesForUpdatableBootJars() []string {
2079 return module.properties.Permitted_packages
2080}
2081
Paul Duffineedc5d52020-06-12 17:46:39 +01002082func (module *SdkLibraryImport) Prebuilt() *android.Prebuilt {
Colin Cross79c7c262019-04-17 11:11:46 -07002083 return &module.prebuilt
2084}
2085
Paul Duffineedc5d52020-06-12 17:46:39 +01002086func (module *SdkLibraryImport) Name() string {
Colin Cross79c7c262019-04-17 11:11:46 -07002087 return module.prebuilt.Name(module.ModuleBase.Name())
2088}
2089
Paul Duffineedc5d52020-06-12 17:46:39 +01002090func (module *SdkLibraryImport) createInternalModules(mctx android.DefaultableHookContext) {
Colin Cross79c7c262019-04-17 11:11:46 -07002091
Paul Duffin50061512020-01-21 16:31:05 +00002092 // If the build is configured to use prebuilts then force this to be preferred.
Jeongik Cha816a23a2020-07-08 01:09:23 +09002093 if mctx.Config().AlwaysUsePrebuiltSdks() {
Paul Duffin50061512020-01-21 16:31:05 +00002094 module.prebuilt.ForcePrefer()
2095 }
2096
Paul Duffin46a26a82020-04-07 19:27:04 +01002097 for apiScope, scopeProperties := range module.scopeProperties {
Paul Duffin56d44902020-01-31 13:36:25 +00002098 if len(scopeProperties.Jars) == 0 {
2099 continue
2100 }
2101
Paul Duffinbbb546b2020-04-09 00:07:11 +01002102 module.createJavaImportForStubs(mctx, apiScope, scopeProperties)
Paul Duffin3d1248c2020-04-09 00:10:17 +01002103
Paul Duffin0f8faff2020-05-20 16:18:00 +01002104 if len(scopeProperties.Stub_srcs) > 0 {
2105 module.createPrebuiltStubsSources(mctx, apiScope, scopeProperties)
2106 }
Paul Duffin56d44902020-01-31 13:36:25 +00002107 }
Colin Cross79c7c262019-04-17 11:11:46 -07002108
2109 javaSdkLibraries := javaSdkLibraries(mctx.Config())
2110 javaSdkLibrariesLock.Lock()
2111 defer javaSdkLibrariesLock.Unlock()
2112 *javaSdkLibraries = append(*javaSdkLibraries, module.BaseModuleName())
2113}
2114
Paul Duffineedc5d52020-06-12 17:46:39 +01002115func (module *SdkLibraryImport) createJavaImportForStubs(mctx android.DefaultableHookContext, apiScope *apiScope, scopeProperties *sdkLibraryScopeProperties) {
Paul Duffinbbb546b2020-04-09 00:07:11 +01002116 // Creates a java import for the jar with ".stubs" suffix
2117 props := struct {
Paul Duffin1dbe3ca2020-05-16 09:57:59 +01002118 Name *string
2119 Sdk_version *string
2120 Libs []string
2121 Jars []string
Paul Duffin1267d872021-04-16 17:21:36 +01002122 Compile_dex *bool
Paul Duffindadb5ae2022-09-27 12:41:52 +01002123
2124 android.UserSuppliedPrebuiltProperties
Paul Duffinbbb546b2020-04-09 00:07:11 +01002125 }{}
Paul Duffinc3091c82020-05-08 14:16:20 +01002126 props.Name = proptools.StringPtr(module.stubsLibraryModuleName(apiScope))
Paul Duffinbbb546b2020-04-09 00:07:11 +01002127 props.Sdk_version = scopeProperties.Sdk_version
2128 // Prepend any of the libs from the legacy public properties to the libs for each of the
2129 // scopes to avoid having to duplicate them in each scope.
2130 props.Libs = append(module.properties.Libs, scopeProperties.Libs...)
2131 props.Jars = scopeProperties.Jars
Paul Duffin1dbe3ca2020-05-16 09:57:59 +01002132
Paul Duffin38b57852020-05-13 16:08:09 +01002133 // The imports are preferred if the java_sdk_library_import is preferred.
Paul Duffindadb5ae2022-09-27 12:41:52 +01002134 props.CopyUserSuppliedPropertiesFromPrebuilt(&module.prebuilt)
Paul Duffin859fe962020-05-15 10:20:31 +01002135
Paul Duffin1267d872021-04-16 17:21:36 +01002136 // The imports need to be compiled to dex if the java_sdk_library_import requests it.
Paul Duffinf4600f62021-05-13 22:34:45 +01002137 compileDex := module.properties.Compile_dex
2138 if module.stubLibrariesCompiledForDex() {
2139 compileDex = proptools.BoolPtr(true)
2140 }
2141 props.Compile_dex = compileDex
Paul Duffin1267d872021-04-16 17:21:36 +01002142
Paul Duffin859fe962020-05-15 10:20:31 +01002143 mctx.CreateModule(ImportFactory, &props, module.sdkComponentPropertiesForChildLibrary())
Paul Duffinbbb546b2020-04-09 00:07:11 +01002144}
2145
Paul Duffineedc5d52020-06-12 17:46:39 +01002146func (module *SdkLibraryImport) createPrebuiltStubsSources(mctx android.DefaultableHookContext, apiScope *apiScope, scopeProperties *sdkLibraryScopeProperties) {
Paul Duffin3d1248c2020-04-09 00:10:17 +01002147 props := struct {
Paul Duffindadb5ae2022-09-27 12:41:52 +01002148 Name *string
2149 Srcs []string
2150
2151 android.UserSuppliedPrebuiltProperties
Paul Duffin3d1248c2020-04-09 00:10:17 +01002152 }{}
Paul Duffinc3091c82020-05-08 14:16:20 +01002153 props.Name = proptools.StringPtr(module.stubsSourceModuleName(apiScope))
Paul Duffin3d1248c2020-04-09 00:10:17 +01002154 props.Srcs = scopeProperties.Stub_srcs
Paul Duffin38b57852020-05-13 16:08:09 +01002155
2156 // The stubs source is preferred if the java_sdk_library_import is preferred.
Paul Duffindadb5ae2022-09-27 12:41:52 +01002157 props.CopyUserSuppliedPropertiesFromPrebuilt(&module.prebuilt)
2158
2159 mctx.CreateModule(PrebuiltStubsSourcesFactory, &props)
Paul Duffin3d1248c2020-04-09 00:10:17 +01002160}
2161
Paul Duffin44f1d842020-06-26 20:17:02 +01002162// Add the dependencies on the child module in the component deps mutator so that it
2163// creates references to the prebuilt and not the source modules.
2164func (module *SdkLibraryImport) ComponentDepsMutator(ctx android.BottomUpMutatorContext) {
Paul Duffin46a26a82020-04-07 19:27:04 +01002165 for apiScope, scopeProperties := range module.scopeProperties {
Paul Duffin56d44902020-01-31 13:36:25 +00002166 if len(scopeProperties.Jars) == 0 {
2167 continue
2168 }
2169
2170 // Add dependencies to the prebuilt stubs library
Paul Duffin864116c2021-04-02 10:24:13 +01002171 ctx.AddVariationDependencies(nil, apiScope.stubsTag, android.PrebuiltNameFromSource(module.stubsLibraryModuleName(apiScope)))
Paul Duffin0f8faff2020-05-20 16:18:00 +01002172
2173 if len(scopeProperties.Stub_srcs) > 0 {
2174 // Add dependencies to the prebuilt stubs source library
Paul Duffin864116c2021-04-02 10:24:13 +01002175 ctx.AddVariationDependencies(nil, apiScope.stubsSourceTag, android.PrebuiltNameFromSource(module.stubsSourceModuleName(apiScope)))
Paul Duffin0f8faff2020-05-20 16:18:00 +01002176 }
Paul Duffin56d44902020-01-31 13:36:25 +00002177 }
Paul Duffin44f1d842020-06-26 20:17:02 +01002178}
2179
2180// Add other dependencies as normal.
2181func (module *SdkLibraryImport) DepsMutator(ctx android.BottomUpMutatorContext) {
Paul Duffineedc5d52020-06-12 17:46:39 +01002182
2183 implName := module.implLibraryModuleName()
2184 if ctx.OtherModuleExists(implName) {
2185 ctx.AddVariationDependencies(nil, implLibraryTag, implName)
2186
2187 xmlPermissionsModuleName := module.xmlPermissionsModuleName()
2188 if module.sharedLibrary() && ctx.OtherModuleExists(xmlPermissionsModuleName) {
2189 // Add dependency to the rule for generating the xml permissions file
2190 ctx.AddDependency(module, xmlPermissionsFileTag, xmlPermissionsModuleName)
2191 }
2192 }
Colin Cross79c7c262019-04-17 11:11:46 -07002193}
2194
Jiakai Zhang204356f2021-09-09 08:12:46 +00002195func (module *SdkLibraryImport) AndroidMkEntries() []android.AndroidMkEntries {
2196 // For an SDK library imported from a prebuilt APEX, we don't need a Make module for itself, as we
2197 // don't need to install it. However, we need to add its dexpreopt outputs as sub-modules, if it
2198 // is preopted.
2199 dexpreoptEntries := module.dexpreopter.AndroidMkEntriesForApex()
2200 return append(dexpreoptEntries, android.AndroidMkEntries{Disabled: true})
2201}
2202
Jiyong Park45bf82e2020-12-15 22:29:02 +09002203var _ android.ApexModule = (*SdkLibraryImport)(nil)
2204
2205// Implements android.ApexModule
Paul Duffineedc5d52020-06-12 17:46:39 +01002206func (module *SdkLibraryImport) DepIsInSameApex(mctx android.BaseModuleContext, dep android.Module) bool {
2207 depTag := mctx.OtherModuleDependencyTag(dep)
2208 if depTag == xmlPermissionsFileTag {
2209 return true
2210 }
2211
2212 // None of the other dependencies of the java_sdk_library_import are in the same apex
2213 // as the one that references this module.
2214 return false
2215}
2216
Jiyong Park45bf82e2020-12-15 22:29:02 +09002217// Implements android.ApexModule
Dan Albertc8060532020-07-22 22:32:17 -07002218func (module *SdkLibraryImport) ShouldSupportSdkVersion(ctx android.BaseModuleContext,
2219 sdkVersion android.ApiLevel) error {
Jooyung Han749dc692020-04-15 11:03:39 +09002220 // we don't check prebuilt modules for sdk_version
2221 return nil
2222}
2223
Paul Duffinea8f8082021-06-24 13:25:57 +01002224// Implements android.ApexModule
2225func (module *SdkLibraryImport) UniqueApexVariations() bool {
2226 return module.uniqueApexVariations()
2227}
2228
Paul Duffina80cc172022-04-28 17:45:11 +01002229// MinSdkVersion - Implements hiddenAPIModule
2230func (module *SdkLibraryImport) MinSdkVersion(ctx android.EarlyModuleContext) android.SdkSpec {
2231 return android.SdkSpecNone
2232}
2233
2234var _ hiddenAPIModule = (*SdkLibraryImport)(nil)
2235
Paul Duffineedc5d52020-06-12 17:46:39 +01002236func (module *SdkLibraryImport) OutputFiles(tag string) (android.Paths, error) {
Paul Duffin3cf140f2022-04-29 14:21:25 +01002237 paths, err := module.commonOutputFiles(tag)
2238 if paths != nil || err != nil {
2239 return paths, err
2240 }
2241 if module.implLibraryModule != nil {
2242 return module.implLibraryModule.OutputFiles(tag)
2243 } else {
2244 return nil, nil
2245 }
Paul Duffin46dc45a2020-05-14 15:39:10 +01002246}
2247
Paul Duffineedc5d52020-06-12 17:46:39 +01002248func (module *SdkLibraryImport) GenerateAndroidBuildActions(ctx android.ModuleContext) {
Paul Duffina2ae7e02020-09-11 11:55:00 +01002249 module.generateCommonBuildActions(ctx)
2250
Jeongik Chad5fe8782021-07-08 01:13:11 +09002251 // Assume that source module(sdk_library) is installed in /<sdk_library partition>/framework
2252 module.installFile = android.PathForModuleInstall(ctx, "framework", module.Stem()+".jar")
2253
Paul Duffin0f8faff2020-05-20 16:18:00 +01002254 // Record the paths to the prebuilt stubs library and stubs source.
Colin Cross79c7c262019-04-17 11:11:46 -07002255 ctx.VisitDirectDeps(func(to android.Module) {
2256 tag := ctx.OtherModuleDependencyTag(to)
2257
Paul Duffin0f8faff2020-05-20 16:18:00 +01002258 // Extract information from any of the scope specific dependencies.
2259 if scopeTag, ok := tag.(scopeDependencyTag); ok {
2260 apiScope := scopeTag.apiScope
2261 scopePaths := module.getScopePathsCreateIfNeeded(apiScope)
2262
2263 // Extract information from the dependency. The exact information extracted
2264 // is determined by the nature of the dependency which is determined by the tag.
2265 scopeTag.extractDepInfo(ctx, to, scopePaths)
Paul Duffineedc5d52020-06-12 17:46:39 +01002266 } else if tag == implLibraryTag {
2267 if implLibrary, ok := to.(*Library); ok {
2268 module.implLibraryModule = implLibrary
2269 } else {
2270 ctx.ModuleErrorf("implementation library must be of type *java.Library but was %T", to)
2271 }
2272 } else if tag == xmlPermissionsFileTag {
2273 if xmlPermissionsFileModule, ok := to.(*sdkLibraryXml); ok {
2274 module.xmlPermissionsFileModule = xmlPermissionsFileModule
2275 } else {
2276 ctx.ModuleErrorf("xml permissions file module must be of type *sdkLibraryXml but was %T", to)
2277 }
Colin Cross79c7c262019-04-17 11:11:46 -07002278 }
2279 })
Paul Duffin0f8faff2020-05-20 16:18:00 +01002280
2281 // Populate the scope paths with information from the properties.
2282 for apiScope, scopeProperties := range module.scopeProperties {
2283 if len(scopeProperties.Jars) == 0 {
2284 continue
2285 }
2286
2287 paths := module.getScopePathsCreateIfNeeded(apiScope)
Anton Hanssond78eb762021-09-21 15:25:12 +01002288 paths.annotationsZip = android.OptionalPathForModuleSrc(ctx, scopeProperties.Annotations)
Paul Duffin0f8faff2020-05-20 16:18:00 +01002289 paths.currentApiFilePath = android.OptionalPathForModuleSrc(ctx, scopeProperties.Current_api)
2290 paths.removedApiFilePath = android.OptionalPathForModuleSrc(ctx, scopeProperties.Removed_api)
2291 }
Paul Duffin39853512021-02-26 11:09:39 +00002292
2293 if ctx.Device() {
2294 // If this is a variant created for a prebuilt_apex then use the dex implementation jar
2295 // obtained from the associated deapexer module.
2296 ai := ctx.Provider(android.ApexInfoProvider).(android.ApexInfo)
2297 if ai.ForPrebuiltApex {
Paul Duffin39853512021-02-26 11:09:39 +00002298 // Get the path of the dex implementation jar from the `deapexer` module.
Martin Stjernholm44825602021-09-17 01:44:12 +01002299 di := android.FindDeapexerProviderForModule(ctx)
2300 if di == nil {
2301 return // An error has been reported by FindDeapexerProviderForModule.
2302 }
Paul Duffinb4bbf2c2021-06-17 15:59:07 +01002303 if dexOutputPath := di.PrebuiltExportPath(apexRootRelativePathToJavaLib(module.BaseModuleName())); dexOutputPath != nil {
Martin Stjernholm8be1e6d2021-09-15 03:34:04 +01002304 dexJarFile := makeDexJarPathFromPath(dexOutputPath)
2305 module.dexJarFile = dexJarFile
Jiakai Zhang204356f2021-09-09 08:12:46 +00002306 installPath := android.PathForModuleInPartitionInstall(
2307 ctx, "apex", ai.ApexVariationName, apexRootRelativePathToJavaLib(module.BaseModuleName()))
2308 module.installFile = installPath
Martin Stjernholm8be1e6d2021-09-15 03:34:04 +01002309 module.initHiddenAPI(ctx, dexJarFile, module.findScopePaths(apiScopePublic).stubsImplPath[0], nil)
Jiakai Zhang204356f2021-09-09 08:12:46 +00002310
2311 // Dexpreopting.
2312 module.dexpreopter.installPath = module.dexpreopter.getInstallPath(ctx, installPath)
2313 module.dexpreopter.isSDKLibrary = true
2314 module.dexpreopter.uncompressedDex = shouldUncompressDex(ctx, &module.dexpreopter)
2315 module.dexpreopt(ctx, dexOutputPath)
Paul Duffin39853512021-02-26 11:09:39 +00002316 } else {
2317 // This should never happen as a variant for a prebuilt_apex is only created if the
2318 // prebuilt_apex has been configured to export the java library dex file.
Martin Stjernholm44825602021-09-17 01:44:12 +01002319 ctx.ModuleErrorf("internal error: no dex implementation jar available from prebuilt APEX %s", di.ApexModuleName())
Paul Duffin39853512021-02-26 11:09:39 +00002320 }
2321 }
2322 }
Colin Cross79c7c262019-04-17 11:11:46 -07002323}
2324
Jiyong Parkf1691d22021-03-29 20:11:58 +09002325func (module *SdkLibraryImport) sdkJars(ctx android.BaseModuleContext, sdkVersion android.SdkSpec, headerJars bool) android.Paths {
Paul Duffineedc5d52020-06-12 17:46:39 +01002326
2327 // For consistency with SdkLibrary make the implementation jar available to libraries that
2328 // are within the same APEX.
2329 implLibraryModule := module.implLibraryModule
Colin Cross56a83212020-09-15 18:30:11 -07002330 if implLibraryModule != nil && withinSameApexesAs(ctx, module) {
Paul Duffineedc5d52020-06-12 17:46:39 +01002331 if headerJars {
2332 return implLibraryModule.HeaderJars()
2333 } else {
2334 return implLibraryModule.ImplementationJars()
2335 }
2336 }
2337
Paul Duffin23970f42020-05-20 14:20:02 +01002338 return module.selectHeaderJarsForSdkVersion(ctx, sdkVersion)
Paul Duffin56d44902020-01-31 13:36:25 +00002339}
2340
Colin Cross79c7c262019-04-17 11:11:46 -07002341// to satisfy SdkLibraryDependency interface
Jiyong Parkf1691d22021-03-29 20:11:58 +09002342func (module *SdkLibraryImport) SdkHeaderJars(ctx android.BaseModuleContext, sdkVersion android.SdkSpec) android.Paths {
Colin Cross79c7c262019-04-17 11:11:46 -07002343 // This module is just a wrapper for the prebuilt stubs.
Paul Duffineedc5d52020-06-12 17:46:39 +01002344 return module.sdkJars(ctx, sdkVersion, true)
Colin Cross79c7c262019-04-17 11:11:46 -07002345}
2346
2347// to satisfy SdkLibraryDependency interface
Jiyong Parkf1691d22021-03-29 20:11:58 +09002348func (module *SdkLibraryImport) SdkImplementationJars(ctx android.BaseModuleContext, sdkVersion android.SdkSpec) android.Paths {
Colin Cross79c7c262019-04-17 11:11:46 -07002349 // This module is just a wrapper for the stubs.
Paul Duffineedc5d52020-06-12 17:46:39 +01002350 return module.sdkJars(ctx, sdkVersion, false)
2351}
2352
Ulya Trafimovichdbf31662020-12-17 12:07:54 +00002353// to satisfy UsesLibraryDependency interface
Martin Stjernholm8be1e6d2021-09-15 03:34:04 +01002354func (module *SdkLibraryImport) DexJarBuildPath() OptionalDexJarPath {
Paul Duffin39853512021-02-26 11:09:39 +00002355 // The dex implementation jar extracted from the .apex file should be used in preference to the
2356 // source.
Martin Stjernholm8be1e6d2021-09-15 03:34:04 +01002357 if module.dexJarFile.IsSet() {
Paul Duffin39853512021-02-26 11:09:39 +00002358 return module.dexJarFile
2359 }
Paul Duffineedc5d52020-06-12 17:46:39 +01002360 if module.implLibraryModule == nil {
Martin Stjernholm8be1e6d2021-09-15 03:34:04 +01002361 return makeUnsetDexJarPath()
Paul Duffineedc5d52020-06-12 17:46:39 +01002362 } else {
2363 return module.implLibraryModule.DexJarBuildPath()
2364 }
2365}
2366
Ulya Trafimovichdbf31662020-12-17 12:07:54 +00002367// to satisfy UsesLibraryDependency interface
Ulya Trafimovich31e444e2020-08-14 17:32:16 +01002368func (module *SdkLibraryImport) DexJarInstallPath() android.Path {
Jeongik Chad5fe8782021-07-08 01:13:11 +09002369 return module.installFile
Ulya Trafimovich31e444e2020-08-14 17:32:16 +01002370}
2371
Ulya Trafimovichdbf31662020-12-17 12:07:54 +00002372// to satisfy UsesLibraryDependency interface
2373func (module *SdkLibraryImport) ClassLoaderContexts() dexpreopt.ClassLoaderContextMap {
2374 return nil
2375}
2376
Paul Duffineedc5d52020-06-12 17:46:39 +01002377// to satisfy apex.javaDependency interface
2378func (module *SdkLibraryImport) JacocoReportClassesFile() android.Path {
2379 if module.implLibraryModule == nil {
2380 return nil
2381 } else {
2382 return module.implLibraryModule.JacocoReportClassesFile()
2383 }
2384}
2385
2386// to satisfy apex.javaDependency interface
Colin Cross08dca382020-07-21 20:31:17 -07002387func (module *SdkLibraryImport) LintDepSets() LintDepSets {
2388 if module.implLibraryModule == nil {
2389 return LintDepSets{}
2390 } else {
2391 return module.implLibraryModule.LintDepSets()
2392 }
2393}
2394
Spandan Das17854f52022-01-14 21:19:14 +00002395func (module *SdkLibraryImport) GetStrictUpdatabilityLinting() bool {
Jaewoong Jung476b9d62021-05-10 15:30:00 -07002396 if module.implLibraryModule == nil {
2397 return false
2398 } else {
Spandan Das17854f52022-01-14 21:19:14 +00002399 return module.implLibraryModule.GetStrictUpdatabilityLinting()
Jaewoong Jung476b9d62021-05-10 15:30:00 -07002400 }
2401}
2402
Spandan Das17854f52022-01-14 21:19:14 +00002403func (module *SdkLibraryImport) SetStrictUpdatabilityLinting(strictLinting bool) {
Jaewoong Jung476b9d62021-05-10 15:30:00 -07002404 if module.implLibraryModule != nil {
Spandan Das17854f52022-01-14 21:19:14 +00002405 module.implLibraryModule.SetStrictUpdatabilityLinting(strictLinting)
Jaewoong Jung476b9d62021-05-10 15:30:00 -07002406 }
2407}
2408
Colin Cross08dca382020-07-21 20:31:17 -07002409// to satisfy apex.javaDependency interface
Paul Duffineedc5d52020-06-12 17:46:39 +01002410func (module *SdkLibraryImport) Stem() string {
2411 return module.BaseModuleName()
Colin Cross79c7c262019-04-17 11:11:46 -07002412}
Jiyong Parke3833882020-02-17 17:28:10 +09002413
Paul Duffin44b481b2020-06-17 16:59:43 +01002414var _ ApexDependency = (*SdkLibraryImport)(nil)
2415
2416// to satisfy java.ApexDependency interface
2417func (module *SdkLibraryImport) HeaderJars() android.Paths {
2418 if module.implLibraryModule == nil {
2419 return nil
2420 } else {
2421 return module.implLibraryModule.HeaderJars()
2422 }
2423}
2424
2425// to satisfy java.ApexDependency interface
2426func (module *SdkLibraryImport) ImplementationAndResourcesJars() android.Paths {
2427 if module.implLibraryModule == nil {
2428 return nil
2429 } else {
2430 return module.implLibraryModule.ImplementationAndResourcesJars()
2431 }
2432}
2433
Jiakai Zhang204356f2021-09-09 08:12:46 +00002434// to satisfy java.DexpreopterInterface interface
2435func (module *SdkLibraryImport) IsInstallable() bool {
2436 return true
2437}
2438
Paul Duffinfef55002021-06-17 14:56:05 +01002439var _ android.RequiredFilesFromPrebuiltApex = (*SdkLibraryImport)(nil)
2440
Paul Duffinb4bbf2c2021-06-17 15:59:07 +01002441func (module *SdkLibraryImport) RequiredFilesFromPrebuiltApex(ctx android.BaseModuleContext) []string {
Paul Duffinfef55002021-06-17 14:56:05 +01002442 name := module.BaseModuleName()
2443 return requiredFilesFromPrebuiltApexForImport(name)
2444}
2445
Jiyong Parke3833882020-02-17 17:28:10 +09002446//
2447// java_sdk_library_xml
2448//
2449type sdkLibraryXml struct {
2450 android.ModuleBase
2451 android.DefaultableModuleBase
2452 android.ApexModuleBase
2453
2454 properties sdkLibraryXmlProperties
2455
2456 outputFilePath android.OutputPath
2457 installDirPath android.InstallPath
Colin Cross56a83212020-09-15 18:30:11 -07002458
2459 hideApexVariantFromMake bool
Jiyong Parke3833882020-02-17 17:28:10 +09002460}
2461
2462type sdkLibraryXmlProperties struct {
2463 // canonical name of the lib
2464 Lib_name *string
Pedro Loureiro9956e5e2021-09-07 17:21:59 +00002465
2466 // Signals that this shared library is part of the bootclasspath starting
2467 // on the version indicated in this attribute.
2468 //
2469 // This will make platforms at this level and above to ignore
2470 // <uses-library> tags with this library name because the library is already
2471 // available
2472 On_bootclasspath_since *string
2473
2474 // Signals that this shared library was part of the bootclasspath before
2475 // (but not including) the version indicated in this attribute.
2476 //
2477 // The system will automatically add a <uses-library> tag with this library to
2478 // apps that target any SDK less than the version indicated in this attribute.
2479 On_bootclasspath_before *string
2480
2481 // Indicates that PackageManager should ignore this shared library if the
2482 // platform is below the version indicated in this attribute.
2483 //
2484 // This means that the device won't recognise this library as installed.
2485 Min_device_sdk *string
2486
2487 // Indicates that PackageManager should ignore this shared library if the
2488 // platform is above the version indicated in this attribute.
2489 //
2490 // This means that the device won't recognise this library as installed.
2491 Max_device_sdk *string
Pedro Loureiroc3621422021-09-28 15:40:23 +00002492
2493 // The SdkLibrary's min api level as a string
2494 //
2495 // This value comes from the ApiLevel of the MinSdkVersion property.
2496 Sdk_library_min_api_level *string
Jiyong Parke3833882020-02-17 17:28:10 +09002497}
2498
2499// java_sdk_library_xml builds the permission xml file for a java_sdk_library.
2500// Not to be used directly by users. java_sdk_library internally uses this.
2501func sdkLibraryXmlFactory() android.Module {
2502 module := &sdkLibraryXml{}
2503
2504 module.AddProperties(&module.properties)
2505
2506 android.InitApexModule(module)
2507 android.InitAndroidArchModule(module, android.DeviceSupported, android.MultilibCommon)
2508
2509 return module
2510}
2511
Colin Crossaede88c2020-08-11 12:17:01 -07002512func (module *sdkLibraryXml) UniqueApexVariations() bool {
2513 // sdkLibraryXml needs a unique variation per APEX because the generated XML file contains the path to the
2514 // mounted APEX, which contains the name of the APEX.
2515 return true
2516}
2517
Jiyong Parke3833882020-02-17 17:28:10 +09002518// from android.PrebuiltEtcModule
Jooyung Han0703fd82020-08-26 22:11:53 +09002519func (module *sdkLibraryXml) BaseDir() string {
2520 return "etc"
2521}
2522
2523// from android.PrebuiltEtcModule
Jiyong Parke3833882020-02-17 17:28:10 +09002524func (module *sdkLibraryXml) SubDir() string {
2525 return "permissions"
2526}
2527
2528// from android.PrebuiltEtcModule
2529func (module *sdkLibraryXml) OutputFile() android.OutputPath {
2530 return module.outputFilePath
2531}
2532
2533// from android.ApexModule
2534func (module *sdkLibraryXml) AvailableFor(what string) bool {
2535 return true
2536}
2537
2538func (module *sdkLibraryXml) DepsMutator(ctx android.BottomUpMutatorContext) {
2539 // do nothing
2540}
2541
Jiyong Park45bf82e2020-12-15 22:29:02 +09002542var _ android.ApexModule = (*sdkLibraryXml)(nil)
2543
2544// Implements android.ApexModule
Dan Albertc8060532020-07-22 22:32:17 -07002545func (module *sdkLibraryXml) ShouldSupportSdkVersion(ctx android.BaseModuleContext,
2546 sdkVersion android.ApiLevel) error {
Jooyung Han749dc692020-04-15 11:03:39 +09002547 // sdkLibraryXml doesn't need to be checked separately because java_sdk_library is checked
2548 return nil
2549}
2550
Jiyong Parke3833882020-02-17 17:28:10 +09002551// File path to the runtime implementation library
Colin Cross56a83212020-09-15 18:30:11 -07002552func (module *sdkLibraryXml) implPath(ctx android.ModuleContext) string {
Jiyong Parke3833882020-02-17 17:28:10 +09002553 implName := proptools.String(module.properties.Lib_name)
Colin Cross56a83212020-09-15 18:30:11 -07002554 if apexInfo := ctx.Provider(android.ApexInfoProvider).(android.ApexInfo); !apexInfo.IsForPlatform() {
Colin Crosse07f2312020-08-13 11:24:56 -07002555 // TODO(b/146468504): ApexVariationName() is only a soong module name, not apex name.
Jiyong Parke3833882020-02-17 17:28:10 +09002556 // In most cases, this works fine. But when apex_name is set or override_apex is used
2557 // this can be wrong.
Colin Cross56a83212020-09-15 18:30:11 -07002558 return fmt.Sprintf("/apex/%s/javalib/%s.jar", apexInfo.ApexVariationName, implName)
Jiyong Parke3833882020-02-17 17:28:10 +09002559 }
2560 partition := "system"
2561 if module.SocSpecific() {
2562 partition = "vendor"
2563 } else if module.DeviceSpecific() {
2564 partition = "odm"
2565 } else if module.ProductSpecific() {
2566 partition = "product"
2567 } else if module.SystemExtSpecific() {
2568 partition = "system_ext"
2569 }
2570 return "/" + partition + "/framework/" + implName + ".jar"
2571}
2572
Pedro Loureiro9956e5e2021-09-07 17:21:59 +00002573func formattedOptionalSdkLevelAttribute(ctx android.ModuleContext, attrName string, value *string) string {
2574 if value == nil {
2575 return ""
2576 }
2577 apiLevel, err := android.ApiLevelFromUser(ctx, *value)
2578 if err != nil {
Pedro Loureiroba6682f2021-10-29 09:32:32 +00002579 // attributes in bp files have underscores but in the xml have dashes.
2580 ctx.PropertyErrorf(strings.ReplaceAll(attrName, "-", "_"), err.Error())
Pedro Loureiro9956e5e2021-09-07 17:21:59 +00002581 return ""
2582 }
Pedro Loureirob638c622021-12-22 15:28:05 +00002583 if apiLevel.IsCurrent() {
2584 // passing "current" would always mean a future release, never the current (or the current in
2585 // progress) which means some conditions would never be triggered.
2586 ctx.PropertyErrorf(strings.ReplaceAll(attrName, "-", "_"),
2587 `"current" is not an allowed value for this attribute`)
2588 return ""
2589 }
Pedro Loureiro48991222022-06-17 20:01:21 +00002590 // "safeValue" is safe because it translates finalized codenames to a string
2591 // with their SDK int.
2592 safeValue := apiLevel.String()
2593 return formattedOptionalAttribute(attrName, &safeValue)
Pedro Loureiro9956e5e2021-09-07 17:21:59 +00002594}
2595
2596// formats an attribute for the xml permissions file if the value is not null
2597// returns empty string otherwise
2598func formattedOptionalAttribute(attrName string, value *string) string {
2599 if value == nil {
2600 return ""
2601 }
2602 return fmt.Sprintf(` %s=\"%s\"\n`, attrName, *value)
2603}
2604
2605func (module *sdkLibraryXml) permissionsContents(ctx android.ModuleContext) string {
2606 libName := proptools.String(module.properties.Lib_name)
2607 libNameAttr := formattedOptionalAttribute("name", &libName)
2608 filePath := module.implPath(ctx)
2609 filePathAttr := formattedOptionalAttribute("file", &filePath)
Pedro Loureiroba6682f2021-10-29 09:32:32 +00002610 implicitFromAttr := formattedOptionalSdkLevelAttribute(ctx, "on-bootclasspath-since", module.properties.On_bootclasspath_since)
2611 implicitUntilAttr := formattedOptionalSdkLevelAttribute(ctx, "on-bootclasspath-before", module.properties.On_bootclasspath_before)
2612 minSdkAttr := formattedOptionalSdkLevelAttribute(ctx, "min-device-sdk", module.properties.Min_device_sdk)
2613 maxSdkAttr := formattedOptionalSdkLevelAttribute(ctx, "max-device-sdk", module.properties.Max_device_sdk)
Pedro Loureiro196d3e62021-12-22 19:53:01 +00002614 // <library> is understood in all android versions whereas <apex-library> is only understood from API T (and ignored before that).
2615 // 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 +00002616 var libraryTag string
2617 if module.properties.Min_device_sdk != nil {
Pedro Loureiro196d3e62021-12-22 19:53:01 +00002618 libraryTag = ` <apex-library\n`
Pedro Loureiroc3621422021-09-28 15:40:23 +00002619 } else {
2620 libraryTag = ` <library\n`
2621 }
Pedro Loureiro9956e5e2021-09-07 17:21:59 +00002622
2623 return strings.Join([]string{
2624 `<?xml version=\"1.0\" encoding=\"utf-8\"?>\n`,
2625 `<!-- Copyright (C) 2018 The Android Open Source Project\n`,
2626 `\n`,
2627 ` Licensed under the Apache License, Version 2.0 (the \"License\");\n`,
2628 ` you may not use this file except in compliance with the License.\n`,
2629 ` You may obtain a copy of the License at\n`,
2630 `\n`,
2631 ` http://www.apache.org/licenses/LICENSE-2.0\n`,
2632 `\n`,
2633 ` Unless required by applicable law or agreed to in writing, software\n`,
2634 ` distributed under the License is distributed on an \"AS IS\" BASIS,\n`,
2635 ` WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\n`,
2636 ` See the License for the specific language governing permissions and\n`,
2637 ` limitations under the License.\n`,
2638 `-->\n`,
2639 `<permissions>\n`,
Pedro Loureiroc3621422021-09-28 15:40:23 +00002640 libraryTag,
Pedro Loureiro9956e5e2021-09-07 17:21:59 +00002641 libNameAttr,
2642 filePathAttr,
2643 implicitFromAttr,
2644 implicitUntilAttr,
2645 minSdkAttr,
2646 maxSdkAttr,
2647 ` />\n`,
2648 `</permissions>\n`}, "")
2649}
2650
Jiyong Parke3833882020-02-17 17:28:10 +09002651func (module *sdkLibraryXml) GenerateAndroidBuildActions(ctx android.ModuleContext) {
Colin Cross56a83212020-09-15 18:30:11 -07002652 module.hideApexVariantFromMake = !ctx.Provider(android.ApexInfoProvider).(android.ApexInfo).IsForPlatform()
2653
Jiyong Parke3833882020-02-17 17:28:10 +09002654 libName := proptools.String(module.properties.Lib_name)
Pedro Loureiroc3621422021-09-28 15:40:23 +00002655 module.selfValidate(ctx)
Pedro Loureiro9956e5e2021-09-07 17:21:59 +00002656 xmlContent := module.permissionsContents(ctx)
Jiyong Parke3833882020-02-17 17:28:10 +09002657
2658 module.outputFilePath = android.PathForModuleOut(ctx, libName+".xml").OutputPath
Colin Crossf1a035e2020-11-16 17:32:30 -08002659 rule := android.NewRuleBuilder(pctx, ctx)
Jiyong Parke3833882020-02-17 17:28:10 +09002660 rule.Command().
2661 Text("/bin/bash -c \"echo -e '" + xmlContent + "'\" > ").
2662 Output(module.outputFilePath)
2663
Colin Crossf1a035e2020-11-16 17:32:30 -08002664 rule.Build("java_sdk_xml", "Permission XML")
Jiyong Parke3833882020-02-17 17:28:10 +09002665
2666 module.installDirPath = android.PathForModuleInstall(ctx, "etc", module.SubDir())
2667}
2668
2669func (module *sdkLibraryXml) AndroidMkEntries() []android.AndroidMkEntries {
Colin Cross56a83212020-09-15 18:30:11 -07002670 if module.hideApexVariantFromMake {
satayev8f088b02021-12-06 11:40:46 +00002671 return []android.AndroidMkEntries{{
Jiyong Parke3833882020-02-17 17:28:10 +09002672 Disabled: true,
2673 }}
2674 }
2675
satayev8f088b02021-12-06 11:40:46 +00002676 return []android.AndroidMkEntries{{
Jiyong Parke3833882020-02-17 17:28:10 +09002677 Class: "ETC",
2678 OutputFile: android.OptionalPathForPath(module.outputFilePath),
2679 ExtraEntries: []android.AndroidMkExtraEntriesFunc{
Colin Crossaa255532020-07-03 13:18:24 -07002680 func(ctx android.AndroidMkExtraEntriesContext, entries *android.AndroidMkEntries) {
Jiyong Parke3833882020-02-17 17:28:10 +09002681 entries.SetString("LOCAL_MODULE_TAGS", "optional")
Colin Crossc68db4b2021-11-11 18:59:15 -08002682 entries.SetString("LOCAL_MODULE_PATH", module.installDirPath.String())
Jiyong Parke3833882020-02-17 17:28:10 +09002683 entries.SetString("LOCAL_INSTALLED_MODULE_STEM", module.outputFilePath.Base())
2684 },
2685 },
2686 }}
2687}
Paul Duffindd46f712020-02-10 13:37:10 +00002688
Pedro Loureiroc3621422021-09-28 15:40:23 +00002689func (module *sdkLibraryXml) selfValidate(ctx android.ModuleContext) {
2690 module.validateAtLeastTAttributes(ctx)
2691 module.validateMinAndMaxDeviceSdk(ctx)
2692 module.validateMinMaxDeviceSdkAndModuleMinSdk(ctx)
2693 module.validateOnBootclasspathBeforeRequirements(ctx)
2694}
2695
2696func (module *sdkLibraryXml) validateAtLeastTAttributes(ctx android.ModuleContext) {
2697 t := android.ApiLevelOrPanic(ctx, "Tiramisu")
2698 module.attrAtLeastT(ctx, t, module.properties.Min_device_sdk, "min_device_sdk")
2699 module.attrAtLeastT(ctx, t, module.properties.Max_device_sdk, "max_device_sdk")
2700 module.attrAtLeastT(ctx, t, module.properties.On_bootclasspath_before, "on_bootclasspath_before")
2701 module.attrAtLeastT(ctx, t, module.properties.On_bootclasspath_since, "on_bootclasspath_since")
2702}
2703
2704func (module *sdkLibraryXml) attrAtLeastT(ctx android.ModuleContext, t android.ApiLevel, attr *string, attrName string) {
2705 if attr != nil {
2706 if level, err := android.ApiLevelFromUser(ctx, *attr); err == nil {
2707 // we will inform the user of invalid inputs when we try to write the
2708 // permissions xml file so we don't need to do it here
2709 if t.GreaterThan(level) {
2710 ctx.PropertyErrorf(attrName, "Attribute value needs to be at least T")
2711 }
2712 }
2713 }
2714}
2715
2716func (module *sdkLibraryXml) validateMinAndMaxDeviceSdk(ctx android.ModuleContext) {
2717 if module.properties.Min_device_sdk != nil && module.properties.Max_device_sdk != nil {
2718 min, minErr := android.ApiLevelFromUser(ctx, *module.properties.Min_device_sdk)
2719 max, maxErr := android.ApiLevelFromUser(ctx, *module.properties.Max_device_sdk)
2720 if minErr == nil && maxErr == nil {
2721 // we will inform the user of invalid inputs when we try to write the
2722 // permissions xml file so we don't need to do it here
2723 if min.GreaterThan(max) {
2724 ctx.ModuleErrorf("min_device_sdk can't be greater than max_device_sdk")
2725 }
2726 }
2727 }
2728}
2729
2730func (module *sdkLibraryXml) validateMinMaxDeviceSdkAndModuleMinSdk(ctx android.ModuleContext) {
2731 moduleMinApi := android.ApiLevelOrPanic(ctx, *module.properties.Sdk_library_min_api_level)
2732 if module.properties.Min_device_sdk != nil {
2733 api, err := android.ApiLevelFromUser(ctx, *module.properties.Min_device_sdk)
2734 if err == nil {
2735 if moduleMinApi.GreaterThan(api) {
2736 ctx.PropertyErrorf("min_device_sdk", "Can't be less than module's min sdk (%s)", moduleMinApi)
2737 }
2738 }
2739 }
2740 if module.properties.Max_device_sdk != nil {
2741 api, err := android.ApiLevelFromUser(ctx, *module.properties.Max_device_sdk)
2742 if err == nil {
2743 if moduleMinApi.GreaterThan(api) {
2744 ctx.PropertyErrorf("max_device_sdk", "Can't be less than module's min sdk (%s)", moduleMinApi)
2745 }
2746 }
2747 }
2748}
2749
2750func (module *sdkLibraryXml) validateOnBootclasspathBeforeRequirements(ctx android.ModuleContext) {
2751 moduleMinApi := android.ApiLevelOrPanic(ctx, *module.properties.Sdk_library_min_api_level)
2752 if module.properties.On_bootclasspath_before != nil {
2753 t := android.ApiLevelOrPanic(ctx, "Tiramisu")
2754 // if we use the attribute, then we need to do this validation
2755 if moduleMinApi.LessThan(t) {
2756 // if minAPi is < T, then we need to have min_device_sdk (which only accepts T+)
2757 if module.properties.Min_device_sdk == nil {
2758 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")
2759 }
2760 }
2761 }
2762}
2763
Paul Duffindd46f712020-02-10 13:37:10 +00002764type sdkLibrarySdkMemberType struct {
2765 android.SdkMemberTypeBase
2766}
2767
Paul Duffin296701e2021-07-14 10:29:36 +01002768func (s *sdkLibrarySdkMemberType) AddDependencies(ctx android.SdkDependencyContext, dependencyTag blueprint.DependencyTag, names []string) {
2769 ctx.AddVariationDependencies(nil, dependencyTag, names...)
Paul Duffindd46f712020-02-10 13:37:10 +00002770}
2771
2772func (s *sdkLibrarySdkMemberType) IsInstance(module android.Module) bool {
2773 _, ok := module.(*SdkLibrary)
2774 return ok
2775}
2776
2777func (s *sdkLibrarySdkMemberType) AddPrebuiltModule(ctx android.SdkMemberContext, member android.SdkMember) android.BpModule {
2778 return ctx.SnapshotBuilder().AddPrebuiltModule(member, "java_sdk_library_import")
2779}
2780
2781func (s *sdkLibrarySdkMemberType) CreateVariantPropertiesStruct() android.SdkMemberProperties {
2782 return &sdkLibrarySdkMemberProperties{}
2783}
2784
Paul Duffin976b0e52021-04-27 23:20:26 +01002785var javaSdkLibrarySdkMemberType = &sdkLibrarySdkMemberType{
2786 android.SdkMemberTypeBase{
2787 PropertyName: "java_sdk_libs",
2788 SupportsSdk: true,
2789 },
2790}
2791
Paul Duffindd46f712020-02-10 13:37:10 +00002792type sdkLibrarySdkMemberProperties struct {
2793 android.SdkMemberPropertiesBase
2794
2795 // Scope to per scope properties.
Paul Duffin106a3a42022-01-27 16:39:06 +00002796 Scopes map[*apiScope]*scopeProperties
Paul Duffindd46f712020-02-10 13:37:10 +00002797
Paul Duffin3d1248c2020-04-09 00:10:17 +01002798 // The Java stubs source files.
2799 Stub_srcs []string
Paul Duffinf7a64332020-05-13 16:54:55 +01002800
2801 // The naming scheme.
2802 Naming_scheme *string
Paul Duffind7eb1c22020-05-26 20:57:10 +01002803
2804 // True if the java_sdk_library_import is for a shared library, false
2805 // otherwise.
2806 Shared_library *bool
Paul Duffina2ae7e02020-09-11 11:55:00 +01002807
Paul Duffin1267d872021-04-16 17:21:36 +01002808 // True if the stub imports should produce dex jars.
2809 Compile_dex *bool
2810
Paul Duffina2ae7e02020-09-11 11:55:00 +01002811 // The paths to the doctag files to add to the prebuilt.
2812 Doctag_paths android.Paths
Paul Duffin869de142021-07-15 14:14:41 +01002813
2814 Permitted_packages []string
Pedro Loureiro9956e5e2021-09-07 17:21:59 +00002815
2816 // Signals that this shared library is part of the bootclasspath starting
2817 // on the version indicated in this attribute.
2818 //
2819 // This will make platforms at this level and above to ignore
2820 // <uses-library> tags with this library name because the library is already
2821 // available
2822 On_bootclasspath_since *string
2823
2824 // Signals that this shared library was part of the bootclasspath before
2825 // (but not including) the version indicated in this attribute.
2826 //
2827 // The system will automatically add a <uses-library> tag with this library to
2828 // apps that target any SDK less than the version indicated in this attribute.
2829 On_bootclasspath_before *string
2830
2831 // Indicates that PackageManager should ignore this shared library if the
2832 // platform is below the version indicated in this attribute.
2833 //
2834 // This means that the device won't recognise this library as installed.
2835 Min_device_sdk *string
2836
2837 // Indicates that PackageManager should ignore this shared library if the
2838 // platform is above the version indicated in this attribute.
2839 //
2840 // This means that the device won't recognise this library as installed.
2841 Max_device_sdk *string
Paul Duffindd46f712020-02-10 13:37:10 +00002842}
2843
2844type scopeProperties struct {
Paul Duffin1fd005d2020-04-09 01:08:11 +01002845 Jars android.Paths
2846 StubsSrcJar android.Path
2847 CurrentApiFile android.Path
2848 RemovedApiFile android.Path
Paul Duffine7babdb2022-02-10 13:06:54 +00002849 AnnotationsZip android.Path `supported_build_releases:"Tiramisu+"`
Paul Duffin1fd005d2020-04-09 01:08:11 +01002850 SdkVersion string
Paul Duffindd46f712020-02-10 13:37:10 +00002851}
2852
2853func (s *sdkLibrarySdkMemberProperties) PopulateFromVariant(ctx android.SdkMemberContext, variant android.Module) {
2854 sdk := variant.(*SdkLibrary)
2855
Paul Duffin106a3a42022-01-27 16:39:06 +00002856 s.Scopes = make(map[*apiScope]*scopeProperties)
Paul Duffindd46f712020-02-10 13:37:10 +00002857 for _, apiScope := range allApiScopes {
Paul Duffin803a9562020-05-20 11:52:25 +01002858 paths := sdk.findScopePaths(apiScope)
2859 if paths == nil {
2860 continue
2861 }
2862
Paul Duffindd46f712020-02-10 13:37:10 +00002863 jars := paths.stubsImplPath
2864 if len(jars) > 0 {
2865 properties := scopeProperties{}
2866 properties.Jars = jars
Paul Duffin780c5f42020-05-12 15:52:55 +01002867 properties.SdkVersion = sdk.sdkVersionForStubsLibrary(ctx.SdkModuleContext(), apiScope)
Paul Duffin0f8faff2020-05-20 16:18:00 +01002868 properties.StubsSrcJar = paths.stubsSrcJar.Path()
Paul Duffin10269f12020-06-19 18:39:55 +01002869 if paths.currentApiFilePath.Valid() {
2870 properties.CurrentApiFile = paths.currentApiFilePath.Path()
2871 }
2872 if paths.removedApiFilePath.Valid() {
2873 properties.RemovedApiFile = paths.removedApiFilePath.Path()
2874 }
Anton Hanssond78eb762021-09-21 15:25:12 +01002875 // The annotations zip is only available for modules that set annotations_enabled: true.
2876 if paths.annotationsZip.Valid() {
2877 properties.AnnotationsZip = paths.annotationsZip.Path()
2878 }
Paul Duffin106a3a42022-01-27 16:39:06 +00002879 s.Scopes[apiScope] = &properties
Paul Duffindd46f712020-02-10 13:37:10 +00002880 }
2881 }
2882
Paul Duffindfa131e2020-05-15 20:37:11 +01002883 s.Naming_scheme = sdk.commonSdkLibraryProperties.Naming_scheme
Paul Duffind7eb1c22020-05-26 20:57:10 +01002884 s.Shared_library = proptools.BoolPtr(sdk.sharedLibrary())
Paul Duffin1267d872021-04-16 17:21:36 +01002885 s.Compile_dex = sdk.dexProperties.Compile_dex
Paul Duffina2ae7e02020-09-11 11:55:00 +01002886 s.Doctag_paths = sdk.doctagPaths
Paul Duffin869de142021-07-15 14:14:41 +01002887 s.Permitted_packages = sdk.PermittedPackagesForUpdatableBootJars()
Pedro Loureiro9956e5e2021-09-07 17:21:59 +00002888 s.On_bootclasspath_since = sdk.commonSdkLibraryProperties.On_bootclasspath_since
2889 s.On_bootclasspath_before = sdk.commonSdkLibraryProperties.On_bootclasspath_before
2890 s.Min_device_sdk = sdk.commonSdkLibraryProperties.Min_device_sdk
2891 s.Max_device_sdk = sdk.commonSdkLibraryProperties.Max_device_sdk
Paul Duffindd46f712020-02-10 13:37:10 +00002892}
2893
2894func (s *sdkLibrarySdkMemberProperties) AddToPropertySet(ctx android.SdkMemberContext, propertySet android.BpPropertySet) {
Paul Duffinf7a64332020-05-13 16:54:55 +01002895 if s.Naming_scheme != nil {
2896 propertySet.AddProperty("naming_scheme", proptools.String(s.Naming_scheme))
2897 }
Paul Duffind7eb1c22020-05-26 20:57:10 +01002898 if s.Shared_library != nil {
2899 propertySet.AddProperty("shared_library", *s.Shared_library)
2900 }
Paul Duffin1267d872021-04-16 17:21:36 +01002901 if s.Compile_dex != nil {
2902 propertySet.AddProperty("compile_dex", *s.Compile_dex)
2903 }
Paul Duffin869de142021-07-15 14:14:41 +01002904 if len(s.Permitted_packages) > 0 {
2905 propertySet.AddProperty("permitted_packages", s.Permitted_packages)
2906 }
Paul Duffinf7a64332020-05-13 16:54:55 +01002907
Paul Duffindd46f712020-02-10 13:37:10 +00002908 for _, apiScope := range allApiScopes {
2909 if properties, ok := s.Scopes[apiScope]; ok {
Paul Duffin6b836ba2020-05-13 19:19:49 +01002910 scopeSet := propertySet.AddPropertySet(apiScope.propertyName)
Paul Duffindd46f712020-02-10 13:37:10 +00002911
Paul Duffin3d1248c2020-04-09 00:10:17 +01002912 scopeDir := filepath.Join("sdk_library", s.OsPrefix(), apiScope.name)
2913
Paul Duffindd46f712020-02-10 13:37:10 +00002914 var jars []string
2915 for _, p := range properties.Jars {
Paul Duffin3d1248c2020-04-09 00:10:17 +01002916 dest := filepath.Join(scopeDir, ctx.Name()+"-stubs.jar")
Paul Duffindd46f712020-02-10 13:37:10 +00002917 ctx.SnapshotBuilder().CopyToSnapshot(p, dest)
2918 jars = append(jars, dest)
2919 }
2920 scopeSet.AddProperty("jars", jars)
2921
Paul Duffin22628d52021-05-12 23:13:22 +01002922 if ctx.SdkModuleContext().Config().IsEnvTrue("SOONG_SDK_SNAPSHOT_USE_SRCJAR") {
2923 // Copy the stubs source jar into the snapshot zip as is.
2924 srcJarSnapshotPath := filepath.Join(scopeDir, ctx.Name()+".srcjar")
2925 ctx.SnapshotBuilder().CopyToSnapshot(properties.StubsSrcJar, srcJarSnapshotPath)
2926 scopeSet.AddProperty("stub_srcs", []string{srcJarSnapshotPath})
2927 } else {
2928 // Merge the stubs source jar into the snapshot zip so that when it is unpacked
2929 // the source files are also unpacked.
2930 snapshotRelativeDir := filepath.Join(scopeDir, ctx.Name()+"_stub_sources")
2931 ctx.SnapshotBuilder().UnzipToSnapshot(properties.StubsSrcJar, snapshotRelativeDir)
2932 scopeSet.AddProperty("stub_srcs", []string{snapshotRelativeDir})
2933 }
Paul Duffin3d1248c2020-04-09 00:10:17 +01002934
Paul Duffin1fd005d2020-04-09 01:08:11 +01002935 if properties.CurrentApiFile != nil {
2936 currentApiSnapshotPath := filepath.Join(scopeDir, ctx.Name()+".txt")
2937 ctx.SnapshotBuilder().CopyToSnapshot(properties.CurrentApiFile, currentApiSnapshotPath)
2938 scopeSet.AddProperty("current_api", currentApiSnapshotPath)
2939 }
2940
2941 if properties.RemovedApiFile != nil {
2942 removedApiSnapshotPath := filepath.Join(scopeDir, ctx.Name()+"-removed.txt")
Paul Duffin3dbf9fd2020-06-02 13:00:02 +01002943 ctx.SnapshotBuilder().CopyToSnapshot(properties.RemovedApiFile, removedApiSnapshotPath)
Paul Duffin1fd005d2020-04-09 01:08:11 +01002944 scopeSet.AddProperty("removed_api", removedApiSnapshotPath)
2945 }
2946
Anton Hanssond78eb762021-09-21 15:25:12 +01002947 if properties.AnnotationsZip != nil {
2948 annotationsSnapshotPath := filepath.Join(scopeDir, ctx.Name()+"_annotations.zip")
2949 ctx.SnapshotBuilder().CopyToSnapshot(properties.AnnotationsZip, annotationsSnapshotPath)
2950 scopeSet.AddProperty("annotations", annotationsSnapshotPath)
2951 }
2952
Paul Duffindd46f712020-02-10 13:37:10 +00002953 if properties.SdkVersion != "" {
2954 scopeSet.AddProperty("sdk_version", properties.SdkVersion)
2955 }
2956 }
2957 }
2958
Paul Duffina2ae7e02020-09-11 11:55:00 +01002959 if len(s.Doctag_paths) > 0 {
2960 dests := []string{}
2961 for _, p := range s.Doctag_paths {
2962 dest := filepath.Join("doctags", p.Rel())
2963 ctx.SnapshotBuilder().CopyToSnapshot(p, dest)
2964 dests = append(dests, dest)
2965 }
2966 propertySet.AddProperty("doctag_files", dests)
2967 }
Paul Duffindd46f712020-02-10 13:37:10 +00002968}