blob: bfdfffcd90427bdc73a1e65784aa596ff2f92481 [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
Sundong Ahnf043cf62018-06-25 16:04:37 +0900380 // List of Java libraries that will be in the classpath when building stubs
381 Stub_only_libs []string `android:"arch_variant"`
382
Anton Hanssondae54cd2021-04-21 16:30:10 +0100383 // List of Java libraries that will included in stub libraries
384 Stub_only_static_libs []string `android:"arch_variant"`
385
Paul Duffin7a586d32019-12-30 17:09:34 +0000386 // list of package names that will be documented and publicized as API.
387 // This allows the API to be restricted to a subset of the source files provided.
388 // If this is unspecified then all the source files will be treated as being part
389 // of the API.
Jiyong Parkc678ad32018-04-10 13:07:10 +0900390 Api_packages []string
391
Jiyong Park5a2c9d72018-05-01 22:25:41 +0900392 // list of package names that must be hidden from the API
393 Hidden_api_packages []string
394
Paul Duffin749f98f2019-12-30 17:23:46 +0000395 // the relative path to the directory containing the api specification files.
396 // Defaults to "api".
397 Api_dir *string
398
Paul Duffindfa131e2020-05-15 20:37:11 +0100399 // Determines whether a runtime implementation library is built; defaults to false.
400 //
401 // If true then it also prevents the module from being used as a shared module, i.e.
402 // it is as is shared_library: false, was set.
Paul Duffin43db9be2019-12-30 17:35:49 +0000403 Api_only *bool
404
Paul Duffin11512472019-02-11 15:55:17 +0000405 // local files that are used within user customized droiddoc options.
406 Droiddoc_option_files []string
407
Spandan Das93e95992021-07-29 18:26:39 +0000408 // additional droiddoc options.
Paul Duffin11512472019-02-11 15:55:17 +0000409 // Available variables for substitution:
410 //
411 // $(location <label>): the path to the droiddoc_option_files with name <label>
Sundong Ahndd567f92018-07-31 17:19:11 +0900412 Droiddoc_options []string
413
Paul Duffine22c2ab2020-05-20 19:35:27 +0100414 // is set to true, Metalava will allow framework SDK to contain annotations.
415 Annotations_enabled *bool
416
Sundong Ahn054b19a2018-10-19 13:46:09 +0900417 // a list of top-level directories containing files to merge qualifier annotations
418 // (i.e. those intended to be included in the stubs written) from.
419 Merge_annotations_dirs []string
420
421 // a list of top-level directories containing Java stub files to merge show/hide annotations from.
422 Merge_inclusion_annotations_dirs []string
423
Paul Duffin4f5c1ef2020-11-19 14:53:43 +0000424 // If set to true then don't create dist rules.
425 No_dist *bool
Sundong Ahn80a87b32019-05-13 15:02:50 +0900426
Paul Duffin31310252020-11-20 21:26:20 +0000427 // The stem for the artifacts that are copied to the dist, if not specified
428 // then defaults to the base module name.
429 //
430 // For each scope the following artifacts are copied to the apistubs/<scope>
431 // directory in the dist.
432 // * stubs impl jar -> <dist-stem>.jar
433 // * API specification file -> api/<dist-stem>.txt
434 // * Removed API specification file -> api/<dist-stem>-removed.txt
435 //
436 // Also used to construct the name of the filegroup (created by prebuilt_apis)
437 // that references the latest released API and remove API specification files.
438 // * API specification filegroup -> <dist-stem>.api.<scope>.latest
439 // * Removed API specification filegroup -> <dist-stem>-removed.api.<scope>.latest
Jaewoong Jung1a97ee02021-03-09 13:25:02 -0800440 // * API incompatibilities baseline filegroup -> <dist-stem>-incompatibilities.api.<scope>.latest
Paul Duffin31310252020-11-20 21:26:20 +0000441 Dist_stem *string
442
Colin Cross986b69a2021-06-01 13:13:40 -0700443 // The subdirectory for the artifacts that are copied to the dist directory. If not specified
Colin Cross3dd66252021-06-01 14:05:09 -0700444 // then defaults to "unknown". Should be set to "android" for anything that should be published
Colin Cross986b69a2021-06-01 13:13:40 -0700445 // in the public Android SDK.
446 Dist_group *string
447
Anton Hanssondff2c782020-12-21 17:10:01 +0000448 // A compatibility mode that allows historical API-tracking files to not exist.
449 // Do not use.
450 Unsafe_ignore_missing_latest_api bool
451
Paul Duffin3375e352020-04-28 10:44:03 +0100452 // indicates whether system and test apis should be generated.
453 Generate_system_and_test_apis bool `blueprint:"mutated"`
454
455 // The properties specific to the public api scope
456 //
457 // Unless explicitly specified by using public.enabled the public api scope is
458 // enabled by default in both legacy and non-legacy mode.
459 Public ApiScopeProperties
460
461 // The properties specific to the system api scope
462 //
463 // In legacy mode the system api scope is enabled by default when sdk_version
464 // is set to something other than "none".
465 //
466 // In non-legacy mode the system api scope is disabled by default.
467 System ApiScopeProperties
468
469 // The properties specific to the test api scope
470 //
471 // In legacy mode the test api scope is enabled by default when sdk_version
472 // is set to something other than "none".
473 //
474 // In non-legacy mode the test api scope is disabled by default.
475 Test ApiScopeProperties
Paul Duffin37e0b772019-12-30 17:20:10 +0000476
Paul Duffin0c5bae52020-06-02 13:00:08 +0100477 // The properties specific to the module-lib api scope
Paul Duffin8f265b92020-04-28 14:13:56 +0100478 //
Paul Duffin0c5bae52020-06-02 13:00:08 +0100479 // Unless explicitly specified by using test.enabled the module-lib api scope is
Paul Duffin8f265b92020-04-28 14:13:56 +0100480 // disabled by default.
481 Module_lib ApiScopeProperties
482
Paul Duffin0c5bae52020-06-02 13:00:08 +0100483 // The properties specific to the system-server api scope
484 //
485 // Unless explicitly specified by using test.enabled the module-lib api scope is
486 // disabled by default.
487 System_server ApiScopeProperties
488
Jiyong Park932cdfe2020-05-28 00:19:53 +0900489 // Determines if the stubs are preferred over the implementation library
490 // for linking, even when the client doesn't specify sdk_version. When this
491 // is set to true, such clients are provided with the widest API surface that
492 // this lib provides. Note however that this option doesn't affect the clients
493 // that are in the same APEX as this library. In that case, the clients are
494 // always linked with the implementation library. Default is false.
495 Default_to_stubs *bool
496
Paul Duffin160fe412020-05-10 19:32:20 +0100497 // Properties related to api linting.
498 Api_lint struct {
499 // Enable api linting.
500 Enabled *bool
501 }
502
Jiyong Parkc678ad32018-04-10 13:07:10 +0900503 // TODO: determines whether to create HTML doc or not
504 //Html_doc *bool
505}
506
Paul Duffin0f8faff2020-05-20 16:18:00 +0100507// Paths to outputs from java_sdk_library and java_sdk_library_import.
508//
509// Fields that are android.Paths are always set (during GenerateAndroidBuildActions).
510// OptionalPaths are always set by java_sdk_library but may not be set by
511// java_sdk_library_import as not all instances provide that information.
Paul Duffind1b3a922020-01-22 11:57:20 +0000512type scopePaths struct {
Paul Duffin0f8faff2020-05-20 16:18:00 +0100513 // The path (represented as Paths for convenience when returning) to the stubs header jar.
514 //
515 // That is the jar that is created by turbine.
516 stubsHeaderPath android.Paths
517
518 // The path (represented as Paths for convenience when returning) to the stubs implementation jar.
519 //
520 // This is not the implementation jar, it still only contains stubs.
521 stubsImplPath android.Paths
522
Paul Duffin1267d872021-04-16 17:21:36 +0100523 // The dex jar for the stubs.
524 //
525 // This is not the implementation jar, it still only contains stubs.
Martin Stjernholm8be1e6d2021-09-15 03:34:04 +0100526 stubsDexJarPath OptionalDexJarPath
Paul Duffin1267d872021-04-16 17:21:36 +0100527
Paul Duffin0f8faff2020-05-20 16:18:00 +0100528 // The API specification file, e.g. system_current.txt.
529 currentApiFilePath android.OptionalPath
530
531 // The specification of API elements removed since the last release.
532 removedApiFilePath android.OptionalPath
533
534 // The stubs source jar.
535 stubsSrcJar android.OptionalPath
Anton Hanssond78eb762021-09-21 15:25:12 +0100536
537 // Extracted annotations.
538 annotationsZip android.OptionalPath
Paul Duffind1b3a922020-01-22 11:57:20 +0000539}
540
Colin Crossdcf71b22021-02-01 13:59:03 -0800541func (paths *scopePaths) extractStubsLibraryInfoFromDependency(ctx android.ModuleContext, dep android.Module) error {
542 if ctx.OtherModuleHasProvider(dep, JavaInfoProvider) {
543 lib := ctx.OtherModuleProvider(dep, JavaInfoProvider).(JavaInfo)
544 paths.stubsHeaderPath = lib.HeaderJars
545 paths.stubsImplPath = lib.ImplementationJars
Paul Duffin1267d872021-04-16 17:21:36 +0100546
547 libDep := dep.(UsesLibraryDependency)
548 paths.stubsDexJarPath = libDep.DexJarBuildPath()
Paul Duffinc8782502020-04-29 20:45:27 +0100549 return nil
550 } else {
Colin Crossdcf71b22021-02-01 13:59:03 -0800551 return fmt.Errorf("expected module that has JavaInfoProvider, e.g. java_library")
Paul Duffinc8782502020-04-29 20:45:27 +0100552 }
553}
554
Paul Duffin0ff08bd2020-04-29 13:30:54 +0100555func (paths *scopePaths) treatDepAsApiStubsProvider(dep android.Module, action func(provider ApiStubsProvider)) error {
556 if apiStubsProvider, ok := dep.(ApiStubsProvider); ok {
557 action(apiStubsProvider)
Paul Duffinc8782502020-04-29 20:45:27 +0100558 return nil
559 } else {
560 return fmt.Errorf("expected module that implements ApiStubsProvider, e.g. droidstubs")
561 }
562}
563
Paul Duffin0f8faff2020-05-20 16:18:00 +0100564func (paths *scopePaths) treatDepAsApiStubsSrcProvider(dep android.Module, action func(provider ApiStubsSrcProvider)) error {
565 if apiStubsProvider, ok := dep.(ApiStubsSrcProvider); ok {
566 action(apiStubsProvider)
567 return nil
568 } else {
569 return fmt.Errorf("expected module that implements ApiStubsSrcProvider, e.g. droidstubs")
570 }
571}
572
Paul Duffin0ff08bd2020-04-29 13:30:54 +0100573func (paths *scopePaths) extractApiInfoFromApiStubsProvider(provider ApiStubsProvider) {
Anton Hanssond78eb762021-09-21 15:25:12 +0100574 paths.annotationsZip = android.OptionalPathForPath(provider.AnnotationsZip())
Paul Duffin0f8faff2020-05-20 16:18:00 +0100575 paths.currentApiFilePath = android.OptionalPathForPath(provider.ApiFilePath())
576 paths.removedApiFilePath = android.OptionalPathForPath(provider.RemovedApiFilePath())
Paul Duffin0ff08bd2020-04-29 13:30:54 +0100577}
578
Colin Crossdcf71b22021-02-01 13:59:03 -0800579func (paths *scopePaths) extractApiInfoFromDep(ctx android.ModuleContext, dep android.Module) error {
Paul Duffin0ff08bd2020-04-29 13:30:54 +0100580 return paths.treatDepAsApiStubsProvider(dep, func(provider ApiStubsProvider) {
581 paths.extractApiInfoFromApiStubsProvider(provider)
582 })
583}
584
Paul Duffin0f8faff2020-05-20 16:18:00 +0100585func (paths *scopePaths) extractStubsSourceInfoFromApiStubsProviders(provider ApiStubsSrcProvider) {
586 paths.stubsSrcJar = android.OptionalPathForPath(provider.StubsSrcJar())
Paul Duffin0ff08bd2020-04-29 13:30:54 +0100587}
588
Colin Crossdcf71b22021-02-01 13:59:03 -0800589func (paths *scopePaths) extractStubsSourceInfoFromDep(ctx android.ModuleContext, dep android.Module) error {
Paul Duffin0f8faff2020-05-20 16:18:00 +0100590 return paths.treatDepAsApiStubsSrcProvider(dep, func(provider ApiStubsSrcProvider) {
Paul Duffin0ff08bd2020-04-29 13:30:54 +0100591 paths.extractStubsSourceInfoFromApiStubsProviders(provider)
592 })
593}
594
Colin Crossdcf71b22021-02-01 13:59:03 -0800595func (paths *scopePaths) extractStubsSourceAndApiInfoFromApiStubsProvider(ctx android.ModuleContext, dep android.Module) error {
Paul Duffin0ff08bd2020-04-29 13:30:54 +0100596 return paths.treatDepAsApiStubsProvider(dep, func(provider ApiStubsProvider) {
597 paths.extractApiInfoFromApiStubsProvider(provider)
598 paths.extractStubsSourceInfoFromApiStubsProviders(provider)
599 })
600}
601
602type commonToSdkLibraryAndImportProperties struct {
Paul Duffin1b1e8062020-05-08 13:44:43 +0100603 // The naming scheme to use for the components that this module creates.
604 //
Paul Duffinee9ad5d2020-09-11 13:04:05 +0100605 // If not specified then it defaults to "default".
Paul Duffin1b1e8062020-05-08 13:44:43 +0100606 //
607 // This is a temporary mechanism to simplify conversion from separate modules for each
608 // component that follow a different naming pattern to the default one.
609 //
610 // TODO(b/155480189) - Remove once naming inconsistencies have been resolved.
Paul Duffin0ff08bd2020-04-29 13:30:54 +0100611 Naming_scheme *string
Paul Duffindfa131e2020-05-15 20:37:11 +0100612
613 // Specifies whether this module can be used as an Android shared library; defaults
614 // to true.
615 //
616 // An Android shared library is one that can be referenced in a <uses-library> element
617 // in an AndroidManifest.xml.
618 Shared_library *bool
Paul Duffina2ae7e02020-09-11 11:55:00 +0100619
620 // Files containing information about supported java doc tags.
621 Doctag_files []string `android:"path"`
Pedro Loureiro9956e5e2021-09-07 17:21:59 +0000622
623 // Signals that this shared library is part of the bootclasspath starting
624 // on the version indicated in this attribute.
625 //
626 // This will make platforms at this level and above to ignore
627 // <uses-library> tags with this library name because the library is already
628 // available
629 On_bootclasspath_since *string
630
631 // Signals that this shared library was part of the bootclasspath before
632 // (but not including) the version indicated in this attribute.
633 //
634 // The system will automatically add a <uses-library> tag with this library to
635 // apps that target any SDK less than the version indicated in this attribute.
636 On_bootclasspath_before *string
637
638 // Indicates that PackageManager should ignore this shared library if the
639 // platform is below the version indicated in this attribute.
640 //
641 // This means that the device won't recognise this library as installed.
642 Min_device_sdk *string
643
644 // Indicates that PackageManager should ignore this shared library if the
645 // platform is above the version indicated in this attribute.
646 //
647 // This means that the device won't recognise this library as installed.
648 Max_device_sdk *string
Paul Duffin0ff08bd2020-04-29 13:30:54 +0100649}
650
Paul Duffin71b33cc2021-06-23 11:39:47 +0100651// commonSdkLibraryAndImportModule defines the interface that must be provided by a module that
652// embeds the commonToSdkLibraryAndImport struct.
653type commonSdkLibraryAndImportModule interface {
Paul Duffinb97b1572021-04-29 21:50:40 +0100654 android.SdkAware
Paul Duffin71b33cc2021-06-23 11:39:47 +0100655
656 BaseModuleName() string
657}
658
Paul Duffin56d44902020-01-31 13:36:25 +0000659// Common code between sdk library and sdk library import
660type commonToSdkLibraryAndImport struct {
Paul Duffin71b33cc2021-06-23 11:39:47 +0100661 module commonSdkLibraryAndImportModule
Paul Duffinc3091c82020-05-08 14:16:20 +0100662
Paul Duffin56d44902020-01-31 13:36:25 +0000663 scopePaths map[*apiScope]*scopePaths
Paul Duffin1b1e8062020-05-08 13:44:43 +0100664
665 namingScheme sdkLibraryComponentNamingScheme
666
Paul Duffindfa131e2020-05-15 20:37:11 +0100667 commonSdkLibraryProperties commonToSdkLibraryAndImportProperties
Paul Duffin859fe962020-05-15 10:20:31 +0100668
Paul Duffina2ae7e02020-09-11 11:55:00 +0100669 // Paths to commonSdkLibraryProperties.Doctag_files
670 doctagPaths android.Paths
671
Paul Duffin859fe962020-05-15 10:20:31 +0100672 // Functionality related to this being used as a component of a java_sdk_library.
673 EmbeddableSdkLibraryComponent
Paul Duffin56d44902020-01-31 13:36:25 +0000674}
675
Paul Duffin71b33cc2021-06-23 11:39:47 +0100676func (c *commonToSdkLibraryAndImport) initCommon(module commonSdkLibraryAndImportModule) {
677 c.module = module
Paul Duffin1b1e8062020-05-08 13:44:43 +0100678
Paul Duffin71b33cc2021-06-23 11:39:47 +0100679 module.AddProperties(&c.commonSdkLibraryProperties)
Paul Duffin859fe962020-05-15 10:20:31 +0100680
681 // Initialize this as an sdk library component.
Paul Duffin71b33cc2021-06-23 11:39:47 +0100682 c.initSdkLibraryComponent(module)
Paul Duffin1b1e8062020-05-08 13:44:43 +0100683}
684
685func (c *commonToSdkLibraryAndImport) initCommonAfterDefaultsApplied(ctx android.DefaultableHookContext) bool {
Paul Duffindfa131e2020-05-15 20:37:11 +0100686 schemeProperty := proptools.StringDefault(c.commonSdkLibraryProperties.Naming_scheme, "default")
Paul Duffin1b1e8062020-05-08 13:44:43 +0100687 switch schemeProperty {
688 case "default":
689 c.namingScheme = &defaultNamingScheme{}
690 default:
691 ctx.PropertyErrorf("naming_scheme", "expected 'default' but was %q", schemeProperty)
692 return false
693 }
694
Paul Duffin3f0290e2021-06-30 18:25:36 +0100695 namePtr := proptools.StringPtr(c.module.BaseModuleName())
696 c.sdkLibraryComponentProperties.SdkLibraryName = namePtr
697
Paul Duffindfa131e2020-05-15 20:37:11 +0100698 // Only track this sdk library if this can be used as a shared library.
699 if c.sharedLibrary() {
700 // Use the name specified in the module definition as the owner.
Paul Duffin3f0290e2021-06-30 18:25:36 +0100701 c.sdkLibraryComponentProperties.SdkLibraryToImplicitlyTrack = namePtr
Paul Duffindfa131e2020-05-15 20:37:11 +0100702 }
Paul Duffin859fe962020-05-15 10:20:31 +0100703
Paul Duffin1b1e8062020-05-08 13:44:43 +0100704 return true
Paul Duffinc3091c82020-05-08 14:16:20 +0100705}
706
Paul Duffinea8f8082021-06-24 13:25:57 +0100707// uniqueApexVariations provides common implementation of the ApexModule.UniqueApexVariations
708// method.
709func (c *commonToSdkLibraryAndImport) uniqueApexVariations() bool {
710 // A java_sdk_library that is a shared library produces an XML file that makes the shared library
711 // usable from an AndroidManifest.xml's <uses-library> entry. That XML file contains the name of
712 // the APEX and so it needs a unique variation per APEX.
713 return c.sharedLibrary()
714}
715
Paul Duffina2ae7e02020-09-11 11:55:00 +0100716func (c *commonToSdkLibraryAndImport) generateCommonBuildActions(ctx android.ModuleContext) {
717 c.doctagPaths = android.PathsForModuleSrc(ctx, c.commonSdkLibraryProperties.Doctag_files)
718}
719
Paul Duffineedc5d52020-06-12 17:46:39 +0100720// Module name of the runtime implementation library
721func (c *commonToSdkLibraryAndImport) implLibraryModuleName() string {
Paul Duffin71b33cc2021-06-23 11:39:47 +0100722 return c.module.BaseModuleName() + ".impl"
Paul Duffineedc5d52020-06-12 17:46:39 +0100723}
724
725// Module name of the XML file for the lib
726func (c *commonToSdkLibraryAndImport) xmlPermissionsModuleName() string {
Paul Duffin71b33cc2021-06-23 11:39:47 +0100727 return c.module.BaseModuleName() + sdkXmlFileSuffix
Paul Duffineedc5d52020-06-12 17:46:39 +0100728}
729
Paul Duffinc3091c82020-05-08 14:16:20 +0100730// Name of the java_library module that compiles the stubs source.
731func (c *commonToSdkLibraryAndImport) stubsLibraryModuleName(apiScope *apiScope) string {
Paul Duffinb97b1572021-04-29 21:50:40 +0100732 baseName := c.module.BaseModuleName()
733 return c.module.SdkMemberComponentName(baseName, func(name string) string {
734 return c.namingScheme.stubsLibraryModuleName(apiScope, name)
735 })
Paul Duffinc3091c82020-05-08 14:16:20 +0100736}
737
738// Name of the droidstubs module that generates the stubs source and may also
739// generate/check the API.
740func (c *commonToSdkLibraryAndImport) stubsSourceModuleName(apiScope *apiScope) string {
Paul Duffinb97b1572021-04-29 21:50:40 +0100741 baseName := c.module.BaseModuleName()
742 return c.module.SdkMemberComponentName(baseName, func(name string) string {
743 return c.namingScheme.stubsSourceModuleName(apiScope, name)
744 })
Paul Duffinc3091c82020-05-08 14:16:20 +0100745}
746
Paul Duffin46dc45a2020-05-14 15:39:10 +0100747// The component names for different outputs of the java_sdk_library.
748//
749// They are similar to the names used for the child modules it creates
750const (
751 stubsSourceComponentName = "stubs.source"
752
753 apiTxtComponentName = "api.txt"
754
755 removedApiTxtComponentName = "removed-api.txt"
Anton Hanssond78eb762021-09-21 15:25:12 +0100756
757 annotationsComponentName = "annotations.zip"
Paul Duffin46dc45a2020-05-14 15:39:10 +0100758)
759
760// A regular expression to match tags that reference a specific stubs component.
761//
762// It will only match if given a valid scope and a valid component. It is verfy strict
763// to ensure it does not accidentally match a similar looking tag that should be processed
764// by the embedded Library.
765var tagSplitter = func() *regexp.Regexp {
766 // Given a list of literal string items returns a regular expression that will
767 // match any one of the items.
768 choice := func(items ...string) string {
769 return `\Q` + strings.Join(items, `\E|\Q`) + `\E`
770 }
771
772 // Regular expression to match one of the scopes.
773 scopesRegexp := choice(allScopeNames...)
774
775 // Regular expression to match one of the components.
Anton Hanssond78eb762021-09-21 15:25:12 +0100776 componentsRegexp := choice(stubsSourceComponentName, apiTxtComponentName, removedApiTxtComponentName, annotationsComponentName)
Paul Duffin46dc45a2020-05-14 15:39:10 +0100777
778 // Regular expression to match any combination of one scope and one component.
779 return regexp.MustCompile(fmt.Sprintf(`^\.(%s)\.(%s)$`, scopesRegexp, componentsRegexp))
780}()
781
782// For OutputFileProducer interface
783//
Anton Hanssond78eb762021-09-21 15:25:12 +0100784// .<scope>.<component name>, for all ComponentNames (for example: .public.removed-api.txt)
Paul Duffin46dc45a2020-05-14 15:39:10 +0100785func (c *commonToSdkLibraryAndImport) commonOutputFiles(tag string) (android.Paths, error) {
786 if groups := tagSplitter.FindStringSubmatch(tag); groups != nil {
787 scopeName := groups[1]
788 component := groups[2]
789
790 if scope, ok := scopeByName[scopeName]; ok {
791 paths := c.findScopePaths(scope)
792 if paths == nil {
Paul Duffin71b33cc2021-06-23 11:39:47 +0100793 return nil, fmt.Errorf("%q does not provide api scope %s", c.module.BaseModuleName(), scopeName)
Paul Duffin46dc45a2020-05-14 15:39:10 +0100794 }
795
796 switch component {
797 case stubsSourceComponentName:
798 if paths.stubsSrcJar.Valid() {
799 return android.Paths{paths.stubsSrcJar.Path()}, nil
800 }
801
802 case apiTxtComponentName:
803 if paths.currentApiFilePath.Valid() {
804 return android.Paths{paths.currentApiFilePath.Path()}, nil
805 }
806
807 case removedApiTxtComponentName:
808 if paths.removedApiFilePath.Valid() {
809 return android.Paths{paths.removedApiFilePath.Path()}, nil
810 }
Anton Hanssond78eb762021-09-21 15:25:12 +0100811
812 case annotationsComponentName:
813 if paths.annotationsZip.Valid() {
814 return android.Paths{paths.annotationsZip.Path()}, nil
815 }
Paul Duffin46dc45a2020-05-14 15:39:10 +0100816 }
817
818 return nil, fmt.Errorf("%s not available for api scope %s", component, scopeName)
819 } else {
820 return nil, fmt.Errorf("unknown scope %s in %s", scope, tag)
821 }
822
823 } else {
Paul Duffina2ae7e02020-09-11 11:55:00 +0100824 switch tag {
825 case ".doctags":
826 if c.doctagPaths != nil {
827 return c.doctagPaths, nil
828 } else {
Paul Duffin71b33cc2021-06-23 11:39:47 +0100829 return nil, fmt.Errorf("no doctag_files specified on %s", c.module.BaseModuleName())
Paul Duffina2ae7e02020-09-11 11:55:00 +0100830 }
831 }
Paul Duffin46dc45a2020-05-14 15:39:10 +0100832 return nil, nil
833 }
834}
835
Paul Duffin803a9562020-05-20 11:52:25 +0100836func (c *commonToSdkLibraryAndImport) getScopePathsCreateIfNeeded(scope *apiScope) *scopePaths {
Paul Duffin56d44902020-01-31 13:36:25 +0000837 if c.scopePaths == nil {
838 c.scopePaths = make(map[*apiScope]*scopePaths)
839 }
840 paths := c.scopePaths[scope]
841 if paths == nil {
842 paths = &scopePaths{}
843 c.scopePaths[scope] = paths
844 }
845
846 return paths
847}
848
Paul Duffin803a9562020-05-20 11:52:25 +0100849func (c *commonToSdkLibraryAndImport) findScopePaths(scope *apiScope) *scopePaths {
850 if c.scopePaths == nil {
851 return nil
852 }
853
854 return c.scopePaths[scope]
855}
856
857// If this does not support the requested api scope then find the closest available
858// scope it does support. Returns nil if no such scope is available.
859func (c *commonToSdkLibraryAndImport) findClosestScopePath(scope *apiScope) *scopePaths {
860 for s := scope; s != nil; s = s.extends {
861 if paths := c.findScopePaths(s); paths != nil {
862 return paths
863 }
864 }
865
866 // This should never happen outside tests as public should be the base scope for every
867 // scope and is enabled by default.
868 return nil
869}
870
Jiyong Parkf1691d22021-03-29 20:11:58 +0900871func (c *commonToSdkLibraryAndImport) selectHeaderJarsForSdkVersion(ctx android.BaseModuleContext, sdkVersion android.SdkSpec) android.Paths {
Paul Duffinb05d4292020-05-20 12:19:10 +0100872
873 // If a specific numeric version has been requested then use prebuilt versions of the sdk.
Jiyong Park54105c42021-03-31 18:17:53 +0900874 if !sdkVersion.ApiLevel.IsPreview() {
Paul Duffin71b33cc2021-06-23 11:39:47 +0100875 return PrebuiltJars(ctx, c.module.BaseModuleName(), sdkVersion)
Paul Duffinb05d4292020-05-20 12:19:10 +0100876 }
877
Paul Duffin1267d872021-04-16 17:21:36 +0100878 paths := c.selectScopePaths(ctx, sdkVersion.Kind)
879 if paths == nil {
880 return nil
881 }
882
883 return paths.stubsHeaderPath
884}
885
886// selectScopePaths returns the *scopePaths appropriate for the specific kind.
887//
888// If the module does not support the specific kind then it will return the *scopePaths for the
889// closest kind which is a subset of the requested kind. e.g. if requesting android.SdkModule then
890// it will return *scopePaths for android.SdkSystem if available or android.SdkPublic of not.
891func (c *commonToSdkLibraryAndImport) selectScopePaths(ctx android.BaseModuleContext, kind android.SdkKind) *scopePaths {
Paul Duffin32cf58a2021-05-18 16:32:50 +0100892 apiScope := sdkKindToApiScope(kind)
Paul Duffinb05d4292020-05-20 12:19:10 +0100893
Paul Duffin803a9562020-05-20 11:52:25 +0100894 paths := c.findClosestScopePath(apiScope)
895 if paths == nil {
896 var scopes []string
897 for _, s := range allApiScopes {
898 if c.findScopePaths(s) != nil {
899 scopes = append(scopes, s.name)
900 }
901 }
Paul Duffin71b33cc2021-06-23 11:39:47 +0100902 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 +0100903 return nil
904 }
905
Paul Duffin1267d872021-04-16 17:21:36 +0100906 return paths
907}
908
Paul Duffin32cf58a2021-05-18 16:32:50 +0100909// sdkKindToApiScope maps from android.SdkKind to apiScope.
910func sdkKindToApiScope(kind android.SdkKind) *apiScope {
911 var apiScope *apiScope
912 switch kind {
913 case android.SdkSystem:
914 apiScope = apiScopeSystem
915 case android.SdkModule:
916 apiScope = apiScopeModuleLib
917 case android.SdkTest:
918 apiScope = apiScopeTest
919 case android.SdkSystemServer:
920 apiScope = apiScopeSystemServer
921 default:
922 apiScope = apiScopePublic
923 }
924 return apiScope
925}
926
Paul Duffin1267d872021-04-16 17:21:36 +0100927// to satisfy SdkLibraryDependency interface
Martin Stjernholm8be1e6d2021-09-15 03:34:04 +0100928func (c *commonToSdkLibraryAndImport) SdkApiStubDexJar(ctx android.BaseModuleContext, kind android.SdkKind) OptionalDexJarPath {
Paul Duffin1267d872021-04-16 17:21:36 +0100929 paths := c.selectScopePaths(ctx, kind)
930 if paths == nil {
Martin Stjernholm8be1e6d2021-09-15 03:34:04 +0100931 return makeUnsetDexJarPath()
Paul Duffin1267d872021-04-16 17:21:36 +0100932 }
933
934 return paths.stubsDexJarPath
Paul Duffinb05d4292020-05-20 12:19:10 +0100935}
936
Paul Duffin32cf58a2021-05-18 16:32:50 +0100937// to satisfy SdkLibraryDependency interface
938func (c *commonToSdkLibraryAndImport) SdkRemovedTxtFile(ctx android.BaseModuleContext, kind android.SdkKind) android.OptionalPath {
939 apiScope := sdkKindToApiScope(kind)
940 paths := c.findScopePaths(apiScope)
941 if paths == nil {
942 return android.OptionalPath{}
943 }
944
945 return paths.removedApiFilePath
946}
947
Paul Duffin859fe962020-05-15 10:20:31 +0100948func (c *commonToSdkLibraryAndImport) sdkComponentPropertiesForChildLibrary() interface{} {
949 componentProps := &struct {
Paul Duffin3f0290e2021-06-30 18:25:36 +0100950 SdkLibraryName *string
Paul Duffin859fe962020-05-15 10:20:31 +0100951 SdkLibraryToImplicitlyTrack *string
Paul Duffindfa131e2020-05-15 20:37:11 +0100952 }{}
953
Paul Duffin3f0290e2021-06-30 18:25:36 +0100954 namePtr := proptools.StringPtr(c.module.BaseModuleName())
955 componentProps.SdkLibraryName = namePtr
956
Paul Duffindfa131e2020-05-15 20:37:11 +0100957 if c.sharedLibrary() {
Paul Duffin859fe962020-05-15 10:20:31 +0100958 // Mark the stubs library as being components of this java_sdk_library so that
959 // any app that includes code which depends (directly or indirectly) on the stubs
960 // library will have the appropriate <uses-library> invocation inserted into its
961 // manifest if necessary.
Paul Duffin3f0290e2021-06-30 18:25:36 +0100962 componentProps.SdkLibraryToImplicitlyTrack = namePtr
Paul Duffin859fe962020-05-15 10:20:31 +0100963 }
964
965 return componentProps
966}
967
Paul Duffindfa131e2020-05-15 20:37:11 +0100968func (c *commonToSdkLibraryAndImport) sharedLibrary() bool {
969 return proptools.BoolDefault(c.commonSdkLibraryProperties.Shared_library, true)
970}
971
Paul Duffinf4600f62021-05-13 22:34:45 +0100972// Check if the stub libraries should be compiled for dex
973func (c *commonToSdkLibraryAndImport) stubLibrariesCompiledForDex() bool {
974 // Always compile the dex file files for the stub libraries if they will be used on the
975 // bootclasspath.
976 return !c.sharedLibrary()
977}
978
Paul Duffin859fe962020-05-15 10:20:31 +0100979// Properties related to the use of a module as an component of a java_sdk_library.
980type SdkLibraryComponentProperties struct {
Paul Duffin3f0290e2021-06-30 18:25:36 +0100981 // The name of the java_sdk_library/_import module.
982 SdkLibraryName *string `blueprint:"mutated"`
Paul Duffin859fe962020-05-15 10:20:31 +0100983
984 // The name of the java_sdk_library/_import to add to a <uses-library> entry
985 // in the AndroidManifest.xml of any Android app that includes code that references
986 // this module. If not set then no java_sdk_library/_import is tracked.
987 SdkLibraryToImplicitlyTrack *string `blueprint:"mutated"`
988}
989
990// Structure to be embedded in a module struct that needs to support the
991// SdkLibraryComponentDependency interface.
992type EmbeddableSdkLibraryComponent struct {
993 sdkLibraryComponentProperties SdkLibraryComponentProperties
994}
995
Paul Duffin71b33cc2021-06-23 11:39:47 +0100996func (e *EmbeddableSdkLibraryComponent) initSdkLibraryComponent(module android.Module) {
997 module.AddProperties(&e.sdkLibraryComponentProperties)
Paul Duffin859fe962020-05-15 10:20:31 +0100998}
999
1000// to satisfy SdkLibraryComponentDependency
Paul Duffin3f0290e2021-06-30 18:25:36 +01001001func (e *EmbeddableSdkLibraryComponent) SdkLibraryName() *string {
1002 return e.sdkLibraryComponentProperties.SdkLibraryName
1003}
1004
1005// to satisfy SdkLibraryComponentDependency
Ulya Trafimovich39b437b2020-09-23 16:42:35 +01001006func (e *EmbeddableSdkLibraryComponent) OptionalSdkLibraryImplementation() *string {
Ulya Trafimovich78645fb2021-07-16 15:29:25 +01001007 // For shared libraries, this is the same as the SDK library name. If a Java library or app
1008 // depends on a component library (e.g. a stub library) it still needs to know the name of the
1009 // run-time library and the corresponding module that provides the implementation. This name is
1010 // passed to manifest_fixer (to be added to AndroidManifest.xml) and added to CLC (to be used
1011 // in dexpreopt).
1012 //
1013 // For non-shared SDK (component or not) libraries this returns `nil`, as they are not
1014 // <uses-library> and should not be added to the manifest or to CLC.
Ulya Trafimovich39b437b2020-09-23 16:42:35 +01001015 return e.sdkLibraryComponentProperties.SdkLibraryToImplicitlyTrack
1016}
1017
Paul Duffin859fe962020-05-15 10:20:31 +01001018// Implemented by modules that are (or possibly could be) a component of a java_sdk_library
1019// (including the java_sdk_library) itself.
1020type SdkLibraryComponentDependency interface {
Ulya Trafimovich31e444e2020-08-14 17:32:16 +01001021 UsesLibraryDependency
1022
Paul Duffin3f0290e2021-06-30 18:25:36 +01001023 // SdkLibraryName returns the name of the java_sdk_library/_import module.
1024 SdkLibraryName() *string
1025
Ulya Trafimovich39b437b2020-09-23 16:42:35 +01001026 // The name of the implementation library for the optional SDK library or nil, if there isn't one.
1027 OptionalSdkLibraryImplementation() *string
Paul Duffin859fe962020-05-15 10:20:31 +01001028}
1029
1030// Make sure that all the module types that are components of java_sdk_library/_import
1031// and which can be referenced (directly or indirectly) from an android app implement
1032// the SdkLibraryComponentDependency interface.
1033var _ SdkLibraryComponentDependency = (*Library)(nil)
1034var _ SdkLibraryComponentDependency = (*Import)(nil)
1035var _ SdkLibraryComponentDependency = (*SdkLibrary)(nil)
Paul Duffineedc5d52020-06-12 17:46:39 +01001036var _ SdkLibraryComponentDependency = (*SdkLibraryImport)(nil)
Paul Duffin859fe962020-05-15 10:20:31 +01001037
Paul Duffin32cf58a2021-05-18 16:32:50 +01001038// Provides access to sdk_version related files, e.g. header and implementation jars.
Paul Duffin859fe962020-05-15 10:20:31 +01001039type SdkLibraryDependency interface {
1040 SdkLibraryComponentDependency
1041
1042 // Get the header jars appropriate for the supplied sdk_version.
1043 //
1044 // These are turbine generated jars so they only change if the externals of the
1045 // class changes but it does not contain and implementation or JavaDoc.
Jiyong Parkf1691d22021-03-29 20:11:58 +09001046 SdkHeaderJars(ctx android.BaseModuleContext, sdkVersion android.SdkSpec) android.Paths
Paul Duffin859fe962020-05-15 10:20:31 +01001047
1048 // Get the implementation jars appropriate for the supplied sdk version.
1049 //
1050 // These are either the implementation jar for the whole sdk library or the implementation
1051 // jars for the stubs. The latter should only be needed when generating JavaDoc as otherwise
1052 // they are identical to the corresponding header jars.
Jiyong Parkf1691d22021-03-29 20:11:58 +09001053 SdkImplementationJars(ctx android.BaseModuleContext, sdkVersion android.SdkSpec) android.Paths
Paul Duffin1267d872021-04-16 17:21:36 +01001054
1055 // SdkApiStubDexJar returns the dex jar for the stubs. It is needed by the hiddenapi processing
1056 // tool which processes dex files.
Martin Stjernholm8be1e6d2021-09-15 03:34:04 +01001057 SdkApiStubDexJar(ctx android.BaseModuleContext, kind android.SdkKind) OptionalDexJarPath
Paul Duffinf4600f62021-05-13 22:34:45 +01001058
Paul Duffin32cf58a2021-05-18 16:32:50 +01001059 // SdkRemovedTxtFile returns the optional path to the removed.txt file for the specified sdk kind.
1060 SdkRemovedTxtFile(ctx android.BaseModuleContext, kind android.SdkKind) android.OptionalPath
1061
Paul Duffinf4600f62021-05-13 22:34:45 +01001062 // sharedLibrary returns true if this can be used as a shared library.
1063 sharedLibrary() bool
Paul Duffin859fe962020-05-15 10:20:31 +01001064}
1065
Inseob Kimc0907f12019-02-08 21:00:45 +09001066type SdkLibrary struct {
Sundong Ahn054b19a2018-10-19 13:46:09 +09001067 Library
Jiyong Parkc678ad32018-04-10 13:07:10 +09001068
Sundong Ahn054b19a2018-10-19 13:46:09 +09001069 sdkLibraryProperties sdkLibraryProperties
Jiyong Parkc678ad32018-04-10 13:07:10 +09001070
Paul Duffin3375e352020-04-28 10:44:03 +01001071 // Map from api scope to the scope specific property structure.
1072 scopeToProperties map[*apiScope]*ApiScopeProperties
1073
Paul Duffin56d44902020-01-31 13:36:25 +00001074 commonToSdkLibraryAndImport
Jiyong Parkc678ad32018-04-10 13:07:10 +09001075}
1076
Inseob Kimc0907f12019-02-08 21:00:45 +09001077var _ SdkLibraryDependency = (*SdkLibrary)(nil)
Colin Cross897d2ed2019-02-11 14:03:51 -08001078
Paul Duffin3375e352020-04-28 10:44:03 +01001079func (module *SdkLibrary) generateTestAndSystemScopesByDefault() bool {
1080 return module.sdkLibraryProperties.Generate_system_and_test_apis
1081}
1082
1083func (module *SdkLibrary) getGeneratedApiScopes(ctx android.EarlyModuleContext) apiScopes {
1084 // Check to see if any scopes have been explicitly enabled. If any have then all
1085 // must be.
1086 anyScopesExplicitlyEnabled := false
1087 for _, scope := range allApiScopes {
1088 scopeProperties := module.scopeToProperties[scope]
1089 if scopeProperties.Enabled != nil {
1090 anyScopesExplicitlyEnabled = true
1091 break
1092 }
Paul Duffind1b3a922020-01-22 11:57:20 +00001093 }
Paul Duffin3375e352020-04-28 10:44:03 +01001094
1095 var generatedScopes apiScopes
1096 enabledScopes := make(map[*apiScope]struct{})
1097 for _, scope := range allApiScopes {
1098 scopeProperties := module.scopeToProperties[scope]
1099 // If any scopes are explicitly enabled then ignore the legacy enabled status.
1100 // This is to ensure that any new usages of this module type do not rely on legacy
1101 // behaviour.
1102 defaultEnabledStatus := false
1103 if anyScopesExplicitlyEnabled {
1104 defaultEnabledStatus = scope.defaultEnabledStatus
1105 } else {
1106 defaultEnabledStatus = scope.legacyEnabledStatus(module)
1107 }
1108 enabled := proptools.BoolDefault(scopeProperties.Enabled, defaultEnabledStatus)
1109 if enabled {
1110 enabledScopes[scope] = struct{}{}
1111 generatedScopes = append(generatedScopes, scope)
1112 }
1113 }
1114
1115 // Now check to make sure that any scope that is extended by an enabled scope is also
1116 // enabled.
1117 for _, scope := range allApiScopes {
1118 if _, ok := enabledScopes[scope]; ok {
1119 extends := scope.extends
1120 if extends != nil {
1121 if _, ok := enabledScopes[extends]; !ok {
1122 ctx.ModuleErrorf("enabled api scope %q depends on disabled scope %q", scope, extends)
1123 }
1124 }
1125 }
1126 }
1127
1128 return generatedScopes
Paul Duffind1b3a922020-01-22 11:57:20 +00001129}
1130
satayev758968a2021-12-06 11:42:40 +00001131var _ android.ModuleWithMinSdkVersionCheck = (*SdkLibrary)(nil)
1132
satayev8f088b02021-12-06 11:40:46 +00001133func (module *SdkLibrary) CheckMinSdkVersion(ctx android.ModuleContext) {
1134 android.CheckMinSdkVersion(ctx, module.MinSdkVersion(ctx).ApiLevel, func(c android.ModuleContext, do android.PayloadDepsCallback) {
1135 ctx.WalkDeps(func(child android.Module, parent android.Module) bool {
1136 isExternal := !module.depIsInSameApex(ctx, child)
1137 if am, ok := child.(android.ApexModule); ok {
1138 if !do(ctx, parent, am, isExternal) {
1139 return false
1140 }
1141 }
1142 return !isExternal
1143 })
1144 })
1145}
1146
Paul Duffineedc5d52020-06-12 17:46:39 +01001147type sdkLibraryComponentTag struct {
1148 blueprint.BaseDependencyTag
1149 name string
1150}
1151
1152// Mark this tag so dependencies that use it are excluded from visibility enforcement.
1153func (t sdkLibraryComponentTag) ExcludeFromVisibilityEnforcement() {}
1154
1155var xmlPermissionsFileTag = sdkLibraryComponentTag{name: "xml-permissions-file"}
Paul Duffine74ac732020-02-06 13:51:46 +00001156
Jiyong Parke3833882020-02-17 17:28:10 +09001157func IsXmlPermissionsFileDepTag(depTag blueprint.DependencyTag) bool {
Paul Duffineedc5d52020-06-12 17:46:39 +01001158 if dt, ok := depTag.(sdkLibraryComponentTag); ok {
Jiyong Parke3833882020-02-17 17:28:10 +09001159 return dt == xmlPermissionsFileTag
1160 }
1161 return false
1162}
1163
Paul Duffineedc5d52020-06-12 17:46:39 +01001164var implLibraryTag = sdkLibraryComponentTag{name: "impl-library"}
Paul Duffin5df79302020-05-16 15:52:12 +01001165
Paul Duffin44f1d842020-06-26 20:17:02 +01001166// Add the dependencies on the child modules in the component deps mutator.
1167func (module *SdkLibrary) ComponentDepsMutator(ctx android.BottomUpMutatorContext) {
Paul Duffin3375e352020-04-28 10:44:03 +01001168 for _, apiScope := range module.getGeneratedApiScopes(ctx) {
Paul Duffind1b3a922020-01-22 11:57:20 +00001169 // Add dependencies to the stubs library
Paul Duffinc3091c82020-05-08 14:16:20 +01001170 ctx.AddVariationDependencies(nil, apiScope.stubsTag, module.stubsLibraryModuleName(apiScope))
Paul Duffind1b3a922020-01-22 11:57:20 +00001171
Paul Duffin15f34ef2020-07-20 18:04:44 +01001172 // Add a dependency on the stubs source in order to access both stubs source and api information.
1173 ctx.AddVariationDependencies(nil, apiScope.stubsSourceAndApiTag, module.stubsSourceModuleName(apiScope))
Sundong Ahn054b19a2018-10-19 13:46:09 +09001174 }
1175
Paul Duffindfa131e2020-05-15 20:37:11 +01001176 if module.requiresRuntimeImplementationLibrary() {
Paul Duffin5df79302020-05-16 15:52:12 +01001177 // Add dependency to the rule for generating the implementation library.
1178 ctx.AddDependency(module, implLibraryTag, module.implLibraryModuleName())
1179
Paul Duffindfa131e2020-05-15 20:37:11 +01001180 if module.sharedLibrary() {
1181 // Add dependency to the rule for generating the xml permissions file
Paul Duffineedc5d52020-06-12 17:46:39 +01001182 ctx.AddDependency(module, xmlPermissionsFileTag, module.xmlPermissionsModuleName())
Paul Duffindfa131e2020-05-15 20:37:11 +01001183 }
Paul Duffin44f1d842020-06-26 20:17:02 +01001184 }
1185}
Paul Duffine74ac732020-02-06 13:51:46 +00001186
Paul Duffin44f1d842020-06-26 20:17:02 +01001187// Add other dependencies as normal.
1188func (module *SdkLibrary) DepsMutator(ctx android.BottomUpMutatorContext) {
Anton Hanssone77fccc2021-01-20 16:52:41 +00001189 var missingApiModules []string
1190 for _, apiScope := range module.getGeneratedApiScopes(ctx) {
1191 if apiScope.unstable {
1192 continue
1193 }
1194 if m := android.SrcIsModule(module.latestApiFilegroupName(apiScope)); !ctx.OtherModuleExists(m) {
1195 missingApiModules = append(missingApiModules, m)
1196 }
1197 if m := android.SrcIsModule(module.latestRemovedApiFilegroupName(apiScope)); !ctx.OtherModuleExists(m) {
1198 missingApiModules = append(missingApiModules, m)
1199 }
Jaewoong Jung1a97ee02021-03-09 13:25:02 -08001200 if m := android.SrcIsModule(module.latestIncompatibilitiesFilegroupName(apiScope)); !ctx.OtherModuleExists(m) {
1201 missingApiModules = append(missingApiModules, m)
1202 }
Anton Hanssone77fccc2021-01-20 16:52:41 +00001203 }
1204 if len(missingApiModules) != 0 && !module.sdkLibraryProperties.Unsafe_ignore_missing_latest_api {
1205 m := module.Name() + " is missing tracking files for previously released library versions.\n"
1206 m += "You need to do one of the following:\n"
1207 m += "- Add `unsafe_ignore_missing_latest_api: true` to your blueprint (to disable compat tracking)\n"
1208 m += "- Add a set of prebuilt txt files representing the last released version of this library for compat checking.\n"
1209 m += " (the current set of API files can be used as a seed for this compatibility tracking\n"
1210 m += "\n"
1211 m += "The following filegroup modules are missing:\n "
1212 m += strings.Join(missingApiModules, "\n ") + "\n"
1213 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."
1214 ctx.ModuleErrorf(m)
1215 }
Paul Duffin44f1d842020-06-26 20:17:02 +01001216 if module.requiresRuntimeImplementationLibrary() {
Paul Duffindfa131e2020-05-15 20:37:11 +01001217 // Only add the deps for the library if it is actually going to be built.
1218 module.Library.deps(ctx)
1219 }
Jiyong Parkc678ad32018-04-10 13:07:10 +09001220}
1221
Paul Duffin46dc45a2020-05-14 15:39:10 +01001222func (module *SdkLibrary) OutputFiles(tag string) (android.Paths, error) {
1223 paths, err := module.commonOutputFiles(tag)
Colin Cross4acaea92021-12-10 23:05:02 +00001224 if paths != nil || err != nil {
Paul Duffin46dc45a2020-05-14 15:39:10 +01001225 return paths, err
1226 }
Colin Cross4acaea92021-12-10 23:05:02 +00001227 if module.requiresRuntimeImplementationLibrary() {
1228 return module.Library.OutputFiles(tag)
1229 }
1230 if tag == "" {
1231 return nil, nil
1232 }
1233 return nil, fmt.Errorf("unsupported module reference tag %q", tag)
Paul Duffin46dc45a2020-05-14 15:39:10 +01001234}
1235
Inseob Kimc0907f12019-02-08 21:00:45 +09001236func (module *SdkLibrary) GenerateAndroidBuildActions(ctx android.ModuleContext) {
satayev8f088b02021-12-06 11:40:46 +00001237 if proptools.String(module.deviceProperties.Min_sdk_version) != "" {
1238 module.CheckMinSdkVersion(ctx)
1239 }
1240
Paul Duffina2ae7e02020-09-11 11:55:00 +01001241 module.generateCommonBuildActions(ctx)
1242
Paul Duffindfa131e2020-05-15 20:37:11 +01001243 // Only build an implementation library if required.
1244 if module.requiresRuntimeImplementationLibrary() {
Paul Duffin43db9be2019-12-30 17:35:49 +00001245 module.Library.GenerateAndroidBuildActions(ctx)
1246 }
Sundong Ahn054b19a2018-10-19 13:46:09 +09001247
Paul Duffinb97b1572021-04-29 21:50:40 +01001248 // Collate the components exported by this module. All scope specific modules are exported but
1249 // the impl and xml component modules are not.
1250 exportedComponents := map[string]struct{}{}
1251
Sundong Ahn57368eb2018-07-06 11:20:23 +09001252 // Record the paths to the header jars of the library (stubs and impl).
Paul Duffind1b3a922020-01-22 11:57:20 +00001253 // When this java_sdk_library is depended upon from others via "libs" property,
Jiyong Parkc678ad32018-04-10 13:07:10 +09001254 // the recorded paths will be returned depending on the link type of the caller.
1255 ctx.VisitDirectDeps(func(to android.Module) {
Jiyong Parkc678ad32018-04-10 13:07:10 +09001256 tag := ctx.OtherModuleDependencyTag(to)
1257
Paul Duffinc8782502020-04-29 20:45:27 +01001258 // Extract information from any of the scope specific dependencies.
1259 if scopeTag, ok := tag.(scopeDependencyTag); ok {
1260 apiScope := scopeTag.apiScope
Paul Duffin803a9562020-05-20 11:52:25 +01001261 scopePaths := module.getScopePathsCreateIfNeeded(apiScope)
Paul Duffinc8782502020-04-29 20:45:27 +01001262
1263 // Extract information from the dependency. The exact information extracted
1264 // is determined by the nature of the dependency which is determined by the tag.
1265 scopeTag.extractDepInfo(ctx, to, scopePaths)
Paul Duffinb97b1572021-04-29 21:50:40 +01001266
1267 exportedComponents[ctx.OtherModuleName(to)] = struct{}{}
Sundong Ahn20e998b2018-07-24 11:19:26 +09001268 }
Jiyong Parkc678ad32018-04-10 13:07:10 +09001269 })
Paul Duffinb97b1572021-04-29 21:50:40 +01001270
1271 // Make the set of components exported by this module available for use elsewhere.
1272 exportedComponentInfo := android.ExportedComponentsInfo{Components: android.SortedStringKeys(exportedComponents)}
1273 ctx.SetProvider(android.ExportedComponentsInfoProvider, exportedComponentInfo)
Jiyong Parkc678ad32018-04-10 13:07:10 +09001274}
1275
Jiyong Park0b0e1b92019-12-03 13:24:29 +09001276func (module *SdkLibrary) AndroidMkEntries() []android.AndroidMkEntries {
Paul Duffindfa131e2020-05-15 20:37:11 +01001277 if !module.requiresRuntimeImplementationLibrary() {
Paul Duffin43db9be2019-12-30 17:35:49 +00001278 return nil
1279 }
Jiyong Park0b0e1b92019-12-03 13:24:29 +09001280 entriesList := module.Library.AndroidMkEntries()
Yo Chiang07d75072020-06-05 17:43:19 +08001281 if module.sharedLibrary() {
1282 entries := &entriesList[0]
1283 entries.Required = append(entries.Required, module.xmlPermissionsModuleName())
1284 }
Jiyong Park0b0e1b92019-12-03 13:24:29 +09001285 return entriesList
Jiyong Park82484c02018-04-23 21:41:26 +09001286}
1287
Anton Hansson5fd5d242020-03-27 19:43:19 +00001288// The dist path of the stub artifacts
1289func (module *SdkLibrary) apiDistPath(apiScope *apiScope) string {
Colin Crossf0eace92021-06-02 13:02:23 -07001290 return path.Join("apistubs", module.distGroup(), apiScope.name)
Anton Hansson5fd5d242020-03-27 19:43:19 +00001291}
1292
Paul Duffin12ceb462019-12-24 20:31:31 +00001293// Get the sdk version for use when compiling the stubs library.
Paul Duffin780c5f42020-05-12 15:52:55 +01001294func (module *SdkLibrary) sdkVersionForStubsLibrary(mctx android.EarlyModuleContext, apiScope *apiScope) string {
Paul Duffin87a05a32020-05-12 11:50:28 +01001295 scopeProperties := module.scopeToProperties[apiScope]
1296 if scopeProperties.Sdk_version != nil {
1297 return proptools.String(scopeProperties.Sdk_version)
1298 }
1299
Jiyong Parkf1691d22021-03-29 20:11:58 +09001300 sdkDep := decodeSdkDep(mctx, android.SdkContext(&module.Library))
Paul Duffin12ceb462019-12-24 20:31:31 +00001301 if sdkDep.hasStandardLibs() {
1302 // If building against a standard sdk then use the sdk version appropriate for the scope.
Paul Duffind1b3a922020-01-22 11:57:20 +00001303 return apiScope.sdkVersion
Paul Duffin12ceb462019-12-24 20:31:31 +00001304 } else {
1305 // Otherwise, use no system module.
1306 return "none"
1307 }
1308}
1309
Paul Duffin31310252020-11-20 21:26:20 +00001310func (module *SdkLibrary) distStem() string {
1311 return proptools.StringDefault(module.sdkLibraryProperties.Dist_stem, module.BaseModuleName())
1312}
1313
Colin Cross986b69a2021-06-01 13:13:40 -07001314// distGroup returns the subdirectory of the dist path of the stub artifacts.
1315func (module *SdkLibrary) distGroup() string {
Colin Cross59b92bf2021-06-01 14:07:56 -07001316 return proptools.StringDefault(module.sdkLibraryProperties.Dist_group, "unknown")
Colin Cross986b69a2021-06-01 13:13:40 -07001317}
1318
Paul Duffind1b3a922020-01-22 11:57:20 +00001319func (module *SdkLibrary) latestApiFilegroupName(apiScope *apiScope) string {
Paul Duffin31310252020-11-20 21:26:20 +00001320 return ":" + module.distStem() + ".api." + apiScope.name + ".latest"
Jiyong Park58c518b2018-05-12 22:29:12 +09001321}
Jiyong Parkc678ad32018-04-10 13:07:10 +09001322
Paul Duffind1b3a922020-01-22 11:57:20 +00001323func (module *SdkLibrary) latestRemovedApiFilegroupName(apiScope *apiScope) string {
Paul Duffin31310252020-11-20 21:26:20 +00001324 return ":" + module.distStem() + "-removed.api." + apiScope.name + ".latest"
Jiyong Parkc678ad32018-04-10 13:07:10 +09001325}
1326
Jaewoong Jung1a97ee02021-03-09 13:25:02 -08001327func (module *SdkLibrary) latestIncompatibilitiesFilegroupName(apiScope *apiScope) string {
1328 return ":" + module.distStem() + "-incompatibilities.api." + apiScope.name + ".latest"
1329}
1330
Anton Hansson944e77d2020-08-19 11:40:22 +01001331func childModuleVisibility(childVisibility []string) []string {
1332 if childVisibility == nil {
1333 // No child visibility set. The child will use the visibility of the sdk_library.
1334 return nil
1335 }
1336
1337 // Prepend an override to ignore the sdk_library's visibility, and rely on the child visibility.
1338 var visibility []string
1339 visibility = append(visibility, "//visibility:override")
1340 visibility = append(visibility, childVisibility...)
1341 return visibility
1342}
1343
Paul Duffin5df79302020-05-16 15:52:12 +01001344// Creates the implementation java library
1345func (module *SdkLibrary) createImplLibrary(mctx android.DefaultableHookContext) {
Anton Hansson944e77d2020-08-19 11:40:22 +01001346 visibility := childModuleVisibility(module.sdkLibraryProperties.Impl_library_visibility)
1347
Paul Duffin5df79302020-05-16 15:52:12 +01001348 props := struct {
Paul Duffin66cdbf02021-05-14 16:35:06 +01001349 Name *string
1350 Visibility []string
1351 Instrument bool
1352 Libs []string
Paul Duffin5df79302020-05-16 15:52:12 +01001353 }{
1354 Name: proptools.StringPtr(module.implLibraryModuleName()),
Anton Hansson944e77d2020-08-19 11:40:22 +01001355 Visibility: visibility,
Paul Duffin296cf332020-06-18 21:09:55 +01001356 // Set the instrument property to ensure it is instrumented when instrumentation is required.
1357 Instrument: true,
Anton Hansson7f66efa2020-10-08 14:47:23 +01001358 // Set the impl_only libs. Note that the module's "Libs" get appended as well, via the
1359 // addition of &module.properties below.
1360 Libs: module.sdkLibraryProperties.Impl_only_libs,
Paul Duffin5df79302020-05-16 15:52:12 +01001361 }
1362
1363 properties := []interface{}{
1364 &module.properties,
1365 &module.protoProperties,
1366 &module.deviceProperties,
Liz Kammera7a64f32020-07-09 15:16:41 -07001367 &module.dexProperties,
Paul Duffin5df79302020-05-16 15:52:12 +01001368 &module.dexpreoptProperties,
Colin Cross014489c2020-06-02 20:09:13 -07001369 &module.linter.properties,
Paul Duffin5df79302020-05-16 15:52:12 +01001370 &props,
1371 module.sdkComponentPropertiesForChildLibrary(),
1372 }
1373 mctx.CreateModule(LibraryFactory, properties...)
1374}
1375
Jiyong Parkc678ad32018-04-10 13:07:10 +09001376// Creates a static java library that has API stubs
Paul Duffinf0229202020-04-29 16:47:28 +01001377func (module *SdkLibrary) createStubsLibrary(mctx android.DefaultableHookContext, apiScope *apiScope) {
Jiyong Parkc678ad32018-04-10 13:07:10 +09001378 props := struct {
Dan Willemsen9f435972020-05-28 15:28:00 -07001379 Name *string
1380 Visibility []string
1381 Srcs []string
1382 Installable *bool
1383 Sdk_version *string
1384 System_modules *string
1385 Patch_module *string
1386 Libs []string
Anton Hanssondae54cd2021-04-21 16:30:10 +01001387 Static_libs []string
Dan Willemsen9f435972020-05-28 15:28:00 -07001388 Compile_dex *bool
1389 Java_version *string
1390 Openjdk9 struct {
Sundong Ahn054b19a2018-10-19 13:46:09 +09001391 Srcs []string
1392 Javacflags []string
1393 }
Anton Hansson5fd5d242020-03-27 19:43:19 +00001394 Dist struct {
1395 Targets []string
1396 Dest *string
1397 Dir *string
1398 Tag *string
1399 }
Jiyong Parkc678ad32018-04-10 13:07:10 +09001400 }{}
1401
Paul Duffinc3091c82020-05-08 14:16:20 +01001402 props.Name = proptools.StringPtr(module.stubsLibraryModuleName(apiScope))
Anton Hansson944e77d2020-08-19 11:40:22 +01001403 props.Visibility = childModuleVisibility(module.sdkLibraryProperties.Stubs_library_visibility)
Jiyong Parkc678ad32018-04-10 13:07:10 +09001404 // sources are generated from the droiddoc
Paul Duffinc3091c82020-05-08 14:16:20 +01001405 props.Srcs = []string{":" + module.stubsSourceModuleName(apiScope)}
Paul Duffin12ceb462019-12-24 20:31:31 +00001406 sdkVersion := module.sdkVersionForStubsLibrary(mctx, apiScope)
Paul Duffin52d398a2019-06-11 12:31:14 +01001407 props.Sdk_version = proptools.StringPtr(sdkVersion)
Paul Duffina18abc22020-05-16 18:54:24 +01001408 props.System_modules = module.deviceProperties.System_modules
1409 props.Patch_module = module.properties.Patch_module
Paul Duffin367ab912019-12-23 19:40:36 +00001410 props.Installable = proptools.BoolPtr(false)
Sundong Ahn054b19a2018-10-19 13:46:09 +09001411 props.Libs = module.sdkLibraryProperties.Stub_only_libs
Anton Hanssondae54cd2021-04-21 16:30:10 +01001412 props.Static_libs = module.sdkLibraryProperties.Stub_only_static_libs
Paul Duffine22c2ab2020-05-20 19:35:27 +01001413 // The stub-annotations library contains special versions of the annotations
1414 // with CLASS retention policy, so that they're kept.
1415 if proptools.Bool(module.sdkLibraryProperties.Annotations_enabled) {
1416 props.Libs = append(props.Libs, "stub-annotations")
1417 }
Paul Duffina18abc22020-05-16 18:54:24 +01001418 props.Openjdk9.Srcs = module.properties.Openjdk9.Srcs
1419 props.Openjdk9.Javacflags = module.properties.Openjdk9.Javacflags
Anton Hansson83509b52020-05-21 09:21:57 +01001420 // We compile the stubs for 1.8 in line with the main android.jar stubs, and potential
1421 // interop with older developer tools that don't support 1.9.
1422 props.Java_version = proptools.StringPtr("1.8")
Paul Duffinf4600f62021-05-13 22:34:45 +01001423
1424 // The imports need to be compiled to dex if the java_sdk_library requests it.
1425 compileDex := module.dexProperties.Compile_dex
1426 if module.stubLibrariesCompiledForDex() {
1427 compileDex = proptools.BoolPtr(true)
Sundong Ahndd567f92018-07-31 17:19:11 +09001428 }
Paul Duffinf4600f62021-05-13 22:34:45 +01001429 props.Compile_dex = compileDex
Jiyong Parkc678ad32018-04-10 13:07:10 +09001430
Anton Hansson5fd5d242020-03-27 19:43:19 +00001431 // Dist the class jar artifact for sdk builds.
1432 if !Bool(module.sdkLibraryProperties.No_dist) {
1433 props.Dist.Targets = []string{"sdk", "win_sdk"}
Paul Duffin31310252020-11-20 21:26:20 +00001434 props.Dist.Dest = proptools.StringPtr(fmt.Sprintf("%v.jar", module.distStem()))
Anton Hansson5fd5d242020-03-27 19:43:19 +00001435 props.Dist.Dir = proptools.StringPtr(module.apiDistPath(apiScope))
1436 props.Dist.Tag = proptools.StringPtr(".jar")
1437 }
Jiyong Parkc678ad32018-04-10 13:07:10 +09001438
Paul Duffin859fe962020-05-15 10:20:31 +01001439 mctx.CreateModule(LibraryFactory, &props, module.sdkComponentPropertiesForChildLibrary())
Jiyong Parkc678ad32018-04-10 13:07:10 +09001440}
1441
Paul Duffin6d0886e2020-04-07 18:49:53 +01001442// Creates a droidstubs module that creates stubs source files from the given full source
Paul Duffinc8782502020-04-29 20:45:27 +01001443// files and also updates and checks the API specification files.
Paul Duffin15f34ef2020-07-20 18:04:44 +01001444func (module *SdkLibrary) createStubsSourcesAndApi(mctx android.DefaultableHookContext, apiScope *apiScope, name string, scopeSpecificDroidstubsArgs []string) {
Jiyong Parkc678ad32018-04-10 13:07:10 +09001445 props := struct {
Sundong Ahn054b19a2018-10-19 13:46:09 +09001446 Name *string
Paul Duffin4911a892020-04-29 23:35:13 +01001447 Visibility []string
Sundong Ahn054b19a2018-10-19 13:46:09 +09001448 Srcs []string
1449 Installable *bool
Paul Duffin52d398a2019-06-11 12:31:14 +01001450 Sdk_version *string
Paul Duffin12ceb462019-12-24 20:31:31 +00001451 System_modules *string
Sundong Ahn054b19a2018-10-19 13:46:09 +09001452 Libs []string
Paul Duffin6877e6d2020-09-25 19:59:14 +01001453 Output_javadoc_comments *bool
Paul Duffin11512472019-02-11 15:55:17 +00001454 Arg_files []string
Sundong Ahn054b19a2018-10-19 13:46:09 +09001455 Args *string
Sundong Ahn054b19a2018-10-19 13:46:09 +09001456 Java_version *string
Paul Duffine22c2ab2020-05-20 19:35:27 +01001457 Annotations_enabled *bool
Sundong Ahn054b19a2018-10-19 13:46:09 +09001458 Merge_annotations_dirs []string
1459 Merge_inclusion_annotations_dirs []string
Paul Duffin0ff08bd2020-04-29 13:30:54 +01001460 Generate_stubs *bool
Anton Hanssone87b03d2020-12-21 15:29:34 +00001461 Previous_api *string
Sundong Ahn054b19a2018-10-19 13:46:09 +09001462 Check_api struct {
Anton Hanssone6056152020-12-31 10:37:27 +00001463 Current ApiToCheck
1464 Last_released ApiToCheck
Paul Duffin160fe412020-05-10 19:32:20 +01001465
1466 Api_lint struct {
1467 Enabled *bool
1468 New_since *string
1469 Baseline_file *string
1470 }
Jiyong Park58c518b2018-05-12 22:29:12 +09001471 }
Sundong Ahn1b92c822018-05-29 11:35:17 +09001472 Aidl struct {
1473 Include_dirs []string
1474 Local_include_dirs []string
1475 }
Paul Duffin040e9062020-11-23 17:41:36 +00001476 Dists []android.Dist
Jiyong Parkc678ad32018-04-10 13:07:10 +09001477 }{}
1478
Paul Duffin7b78b4d2020-04-28 14:08:32 +01001479 // The stubs source processing uses the same compile time classpath when extracting the
1480 // API from the implementation library as it does when compiling it. i.e. the same
1481 // * sdk version
1482 // * system_modules
1483 // * libs (static_libs/libs)
Paul Duffin250e6192019-06-07 10:44:37 +01001484
Paul Duffin0ff08bd2020-04-29 13:30:54 +01001485 props.Name = proptools.StringPtr(name)
Anton Hansson944e77d2020-08-19 11:40:22 +01001486 props.Visibility = childModuleVisibility(module.sdkLibraryProperties.Stubs_source_visibility)
Paul Duffina18abc22020-05-16 18:54:24 +01001487 props.Srcs = append(props.Srcs, module.properties.Srcs...)
Anton Hanssonf8ea3722021-09-16 14:24:13 +01001488 props.Srcs = append(props.Srcs, module.sdkLibraryProperties.Api_srcs...)
Paul Duffina18abc22020-05-16 18:54:24 +01001489 props.Sdk_version = module.deviceProperties.Sdk_version
1490 props.System_modules = module.deviceProperties.System_modules
Jiyong Parkc678ad32018-04-10 13:07:10 +09001491 props.Installable = proptools.BoolPtr(false)
Sundong Ahne6f0b052018-06-05 16:46:14 +09001492 // A droiddoc module has only one Libs property and doesn't distinguish between
1493 // shared libs and static libs. So we need to add both of these libs to Libs property.
Paul Duffina18abc22020-05-16 18:54:24 +01001494 props.Libs = module.properties.Libs
1495 props.Libs = append(props.Libs, module.properties.Static_libs...)
1496 props.Aidl.Include_dirs = module.deviceProperties.Aidl.Include_dirs
1497 props.Aidl.Local_include_dirs = module.deviceProperties.Aidl.Local_include_dirs
1498 props.Java_version = module.properties.Java_version
Jiyong Parkc678ad32018-04-10 13:07:10 +09001499
Paul Duffine22c2ab2020-05-20 19:35:27 +01001500 props.Annotations_enabled = module.sdkLibraryProperties.Annotations_enabled
Sundong Ahn054b19a2018-10-19 13:46:09 +09001501 props.Merge_annotations_dirs = module.sdkLibraryProperties.Merge_annotations_dirs
1502 props.Merge_inclusion_annotations_dirs = module.sdkLibraryProperties.Merge_inclusion_annotations_dirs
1503
Paul Duffin6d0886e2020-04-07 18:49:53 +01001504 droidstubsArgs := []string{}
Paul Duffin235ffff2019-12-24 10:41:30 +00001505 if len(module.sdkLibraryProperties.Api_packages) != 0 {
Paul Duffin6d0886e2020-04-07 18:49:53 +01001506 droidstubsArgs = append(droidstubsArgs, "--stub-packages "+strings.Join(module.sdkLibraryProperties.Api_packages, ":"))
Paul Duffin235ffff2019-12-24 10:41:30 +00001507 }
1508 if len(module.sdkLibraryProperties.Hidden_api_packages) != 0 {
Paul Duffin6d0886e2020-04-07 18:49:53 +01001509 droidstubsArgs = append(droidstubsArgs,
Paul Duffin235ffff2019-12-24 10:41:30 +00001510 android.JoinWithPrefix(module.sdkLibraryProperties.Hidden_api_packages, " --hide-package "))
1511 }
Paul Duffin6d0886e2020-04-07 18:49:53 +01001512 droidstubsArgs = append(droidstubsArgs, module.sdkLibraryProperties.Droiddoc_options...)
Paul Duffin235ffff2019-12-24 10:41:30 +00001513 disabledWarnings := []string{
Paul Duffin235ffff2019-12-24 10:41:30 +00001514 "BroadcastBehavior",
Paul Duffin235ffff2019-12-24 10:41:30 +00001515 "DeprecationMismatch",
Anton Hansson3c0779a2022-02-18 19:24:30 +00001516 "HiddenSuperclass",
Paul Duffin235ffff2019-12-24 10:41:30 +00001517 "HiddenTypeParameter",
Anton Hansson3c0779a2022-02-18 19:24:30 +00001518 "MissingPermission",
1519 "SdkConstant",
Paul Duffin235ffff2019-12-24 10:41:30 +00001520 "Todo",
1521 "Typo",
Anton Hansson3c0779a2022-02-18 19:24:30 +00001522 "UnavailableSymbol",
Paul Duffin235ffff2019-12-24 10:41:30 +00001523 }
Paul Duffin6d0886e2020-04-07 18:49:53 +01001524 droidstubsArgs = append(droidstubsArgs, android.JoinWithPrefix(disabledWarnings, "--hide "))
Sundong Ahnfb2721f2018-09-17 13:23:09 +09001525
Paul Duffin6877e6d2020-09-25 19:59:14 +01001526 // Output Javadoc comments for public scope.
1527 if apiScope == apiScopePublic {
1528 props.Output_javadoc_comments = proptools.BoolPtr(true)
1529 }
1530
Paul Duffin1fb487d2020-04-07 18:50:10 +01001531 // Add in scope specific arguments.
Paul Duffin0ff08bd2020-04-29 13:30:54 +01001532 droidstubsArgs = append(droidstubsArgs, scopeSpecificDroidstubsArgs...)
Paul Duffin11512472019-02-11 15:55:17 +00001533 props.Arg_files = module.sdkLibraryProperties.Droiddoc_option_files
Paul Duffin6d0886e2020-04-07 18:49:53 +01001534 props.Args = proptools.StringPtr(strings.Join(droidstubsArgs, " "))
Jiyong Parkc678ad32018-04-10 13:07:10 +09001535
Paul Duffin15f34ef2020-07-20 18:04:44 +01001536 // List of APIs identified from the provided source files are created. They are later
1537 // compared against to the not-yet-released (a.k.a current) list of APIs and to the
1538 // last-released (a.k.a numbered) list of API.
1539 currentApiFileName := apiScope.apiFilePrefix + "current.txt"
1540 removedApiFileName := apiScope.apiFilePrefix + "removed.txt"
1541 apiDir := module.getApiDir()
1542 currentApiFileName = path.Join(apiDir, currentApiFileName)
1543 removedApiFileName = path.Join(apiDir, removedApiFileName)
Jiyong Parkc678ad32018-04-10 13:07:10 +09001544
Paul Duffin15f34ef2020-07-20 18:04:44 +01001545 // check against the not-yet-release API
1546 props.Check_api.Current.Api_file = proptools.StringPtr(currentApiFileName)
1547 props.Check_api.Current.Removed_api_file = proptools.StringPtr(removedApiFileName)
Jiyong Park58c518b2018-05-12 22:29:12 +09001548
Anton Hanssone6056152020-12-31 10:37:27 +00001549 if !(apiScope.unstable || module.sdkLibraryProperties.Unsafe_ignore_missing_latest_api) {
Paul Duffin15f34ef2020-07-20 18:04:44 +01001550 // check against the latest released API
1551 latestApiFilegroupName := proptools.StringPtr(module.latestApiFilegroupName(apiScope))
Anton Hanssone87b03d2020-12-21 15:29:34 +00001552 props.Previous_api = latestApiFilegroupName
Paul Duffin15f34ef2020-07-20 18:04:44 +01001553 props.Check_api.Last_released.Api_file = latestApiFilegroupName
1554 props.Check_api.Last_released.Removed_api_file = proptools.StringPtr(
1555 module.latestRemovedApiFilegroupName(apiScope))
Jaewoong Jung1a97ee02021-03-09 13:25:02 -08001556 props.Check_api.Last_released.Baseline_file = proptools.StringPtr(
1557 module.latestIncompatibilitiesFilegroupName(apiScope))
Paul Duffin160fe412020-05-10 19:32:20 +01001558
Paul Duffin15f34ef2020-07-20 18:04:44 +01001559 if proptools.Bool(module.sdkLibraryProperties.Api_lint.Enabled) {
1560 // Enable api lint.
1561 props.Check_api.Api_lint.Enabled = proptools.BoolPtr(true)
1562 props.Check_api.Api_lint.New_since = latestApiFilegroupName
Paul Duffin160fe412020-05-10 19:32:20 +01001563
Paul Duffin15f34ef2020-07-20 18:04:44 +01001564 // If it exists then pass a lint-baseline.txt through to droidstubs.
1565 baselinePath := path.Join(apiDir, apiScope.apiFilePrefix+"lint-baseline.txt")
1566 baselinePathRelativeToRoot := path.Join(mctx.ModuleDir(), baselinePath)
1567 paths, err := mctx.GlobWithDeps(baselinePathRelativeToRoot, nil)
1568 if err != nil {
1569 mctx.ModuleErrorf("error checking for presence of %s: %s", baselinePathRelativeToRoot, err)
1570 }
1571 if len(paths) == 1 {
1572 props.Check_api.Api_lint.Baseline_file = proptools.StringPtr(baselinePath)
1573 } else if len(paths) != 0 {
1574 mctx.ModuleErrorf("error checking for presence of %s: expected one path, found: %v", baselinePathRelativeToRoot, paths)
Paul Duffin160fe412020-05-10 19:32:20 +01001575 }
1576 }
Paul Duffin15f34ef2020-07-20 18:04:44 +01001577 }
Jiyong Park58c518b2018-05-12 22:29:12 +09001578
Paul Duffin15f34ef2020-07-20 18:04:44 +01001579 if !Bool(module.sdkLibraryProperties.No_dist) {
Paul Duffin040e9062020-11-23 17:41:36 +00001580 // Dist the api txt and removed api txt artifacts for sdk builds.
1581 distDir := proptools.StringPtr(path.Join(module.apiDistPath(apiScope), "api"))
1582 for _, p := range []struct {
1583 tag string
1584 pattern string
1585 }{
1586 {tag: ".api.txt", pattern: "%s.txt"},
1587 {tag: ".removed-api.txt", pattern: "%s-removed.txt"},
1588 } {
1589 props.Dists = append(props.Dists, android.Dist{
1590 Targets: []string{"sdk", "win_sdk"},
1591 Dir: distDir,
1592 Dest: proptools.StringPtr(fmt.Sprintf(p.pattern, module.distStem())),
1593 Tag: proptools.StringPtr(p.tag),
1594 })
1595 }
Anton Hansson5fd5d242020-03-27 19:43:19 +00001596 }
1597
Colin Cross84dfc3d2019-09-25 11:33:01 -07001598 mctx.CreateModule(DroidstubsFactory, &props)
Jiyong Parkc678ad32018-04-10 13:07:10 +09001599}
1600
Paul Duffinea8f8082021-06-24 13:25:57 +01001601// Implements android.ApexModule
Jooyung Han5e9013b2020-03-10 06:23:13 +09001602func (module *SdkLibrary) DepIsInSameApex(mctx android.BaseModuleContext, dep android.Module) bool {
1603 depTag := mctx.OtherModuleDependencyTag(dep)
1604 if depTag == xmlPermissionsFileTag {
1605 return true
1606 }
1607 return module.Library.DepIsInSameApex(mctx, dep)
1608}
1609
Paul Duffinea8f8082021-06-24 13:25:57 +01001610// Implements android.ApexModule
1611func (module *SdkLibrary) UniqueApexVariations() bool {
1612 return module.uniqueApexVariations()
1613}
1614
Jiyong Parkc678ad32018-04-10 13:07:10 +09001615// Creates the xml file that publicizes the runtime library
Paul Duffinf0229202020-04-29 16:47:28 +01001616func (module *SdkLibrary) createXmlFile(mctx android.DefaultableHookContext) {
Pedro Loureiroc3621422021-09-28 15:40:23 +00001617 moduleMinApiLevel := module.Library.MinSdkVersion(mctx).ApiLevel
1618 var moduleMinApiLevelStr = moduleMinApiLevel.String()
1619 if moduleMinApiLevel == android.NoneApiLevel {
1620 moduleMinApiLevelStr = "current"
1621 }
Jiyong Parke3833882020-02-17 17:28:10 +09001622 props := struct {
Pedro Loureiroc3621422021-09-28 15:40:23 +00001623 Name *string
1624 Lib_name *string
1625 Apex_available []string
1626 On_bootclasspath_since *string
1627 On_bootclasspath_before *string
1628 Min_device_sdk *string
1629 Max_device_sdk *string
1630 Sdk_library_min_api_level *string
Jiyong Parke3833882020-02-17 17:28:10 +09001631 }{
Pedro Loureiroc3621422021-09-28 15:40:23 +00001632 Name: proptools.StringPtr(module.xmlPermissionsModuleName()),
1633 Lib_name: proptools.StringPtr(module.BaseModuleName()),
1634 Apex_available: module.ApexProperties.Apex_available,
1635 On_bootclasspath_since: module.commonSdkLibraryProperties.On_bootclasspath_since,
1636 On_bootclasspath_before: module.commonSdkLibraryProperties.On_bootclasspath_before,
1637 Min_device_sdk: module.commonSdkLibraryProperties.Min_device_sdk,
1638 Max_device_sdk: module.commonSdkLibraryProperties.Max_device_sdk,
1639 Sdk_library_min_api_level: &moduleMinApiLevelStr,
Jiyong Parkc678ad32018-04-10 13:07:10 +09001640 }
Jiyong Parke3833882020-02-17 17:28:10 +09001641
Jiyong Parke3833882020-02-17 17:28:10 +09001642 mctx.CreateModule(sdkLibraryXmlFactory, &props)
Jiyong Parkc678ad32018-04-10 13:07:10 +09001643}
1644
Jiyong Parkf1691d22021-03-29 20:11:58 +09001645func PrebuiltJars(ctx android.BaseModuleContext, baseName string, s android.SdkSpec) android.Paths {
Jiyong Park54105c42021-03-31 18:17:53 +09001646 var ver android.ApiLevel
Jiyong Parkf1691d22021-03-29 20:11:58 +09001647 var kind android.SdkKind
1648 if s.UsePrebuilt(ctx) {
Jiyong Park54105c42021-03-31 18:17:53 +09001649 ver = s.ApiLevel
Jiyong Parkf1691d22021-03-29 20:11:58 +09001650 kind = s.Kind
Jiyong Parkc678ad32018-04-10 13:07:10 +09001651 } else {
Jiyong Park6a927c42020-01-21 02:03:43 +09001652 // We don't have prebuilt SDK for the specific sdkVersion.
1653 // Instead of breaking the build, fallback to use "system_current"
Jiyong Park54105c42021-03-31 18:17:53 +09001654 ver = android.FutureApiLevel
Jiyong Parkf1691d22021-03-29 20:11:58 +09001655 kind = android.SdkSystem
Sundong Ahn054b19a2018-10-19 13:46:09 +09001656 }
Jiyong Park6a927c42020-01-21 02:03:43 +09001657
1658 dir := filepath.Join("prebuilts", "sdk", ver.String(), kind.String())
Paul Duffin50061512020-01-21 16:31:05 +00001659 jar := filepath.Join(dir, baseName+".jar")
Sundong Ahn054b19a2018-10-19 13:46:09 +09001660 jarPath := android.ExistentPathForSource(ctx, jar)
Sundong Ahnae418ac2019-02-28 15:01:28 +09001661 if !jarPath.Valid() {
Colin Cross07c88562020-01-07 09:34:44 -08001662 if ctx.Config().AllowMissingDependencies() {
1663 return android.Paths{android.PathForSource(ctx, jar)}
1664 } else {
Jiyong Parkf1691d22021-03-29 20:11:58 +09001665 ctx.PropertyErrorf("sdk_library", "invalid sdk version %q, %q does not exist", s.Raw, jar)
Colin Cross07c88562020-01-07 09:34:44 -08001666 }
Sundong Ahnae418ac2019-02-28 15:01:28 +09001667 return nil
1668 }
Sundong Ahn054b19a2018-10-19 13:46:09 +09001669 return android.Paths{jarPath.Path()}
1670}
1671
Colin Crossaede88c2020-08-11 12:17:01 -07001672// 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 +01001673//
1674// If either this or the other module are on the platform then this will return
1675// false.
Colin Cross56a83212020-09-15 18:30:11 -07001676func withinSameApexesAs(ctx android.BaseModuleContext, other android.Module) bool {
1677 apexInfo := ctx.Provider(android.ApexInfoProvider).(android.ApexInfo)
1678 otherApexInfo := ctx.OtherModuleProvider(other, android.ApexInfoProvider).(android.ApexInfo)
Jiyong Parkab50b072021-05-12 17:13:56 +09001679 return len(otherApexInfo.InApexVariants) > 0 && reflect.DeepEqual(apexInfo.InApexVariants, otherApexInfo.InApexVariants)
Paul Duffin9b879592020-05-26 13:21:35 +01001680}
1681
Jiyong Parkf1691d22021-03-29 20:11:58 +09001682func (module *SdkLibrary) sdkJars(ctx android.BaseModuleContext, sdkVersion android.SdkSpec, headerJars bool) android.Paths {
Jiyong Park932cdfe2020-05-28 00:19:53 +09001683 // If the client doesn't set sdk_version, but if this library prefers stubs over
1684 // the impl library, let's provide the widest API surface possible. To do so,
1685 // force override sdk_version to module_current so that the closest possible API
1686 // surface could be found in selectHeaderJarsForSdkVersion
Jiyong Parkf1691d22021-03-29 20:11:58 +09001687 if module.defaultsToStubs() && !sdkVersion.Specified() {
Jiyong Park92315372021-04-02 08:45:46 +09001688 sdkVersion = android.SdkSpecFrom(ctx, "module_current")
Jiyong Park932cdfe2020-05-28 00:19:53 +09001689 }
Paul Duffind1b3a922020-01-22 11:57:20 +00001690
Paul Duffindaaa3322020-05-26 18:13:57 +01001691 // Only provide access to the implementation library if it is actually built.
1692 if module.requiresRuntimeImplementationLibrary() {
1693 // Check any special cases for java_sdk_library.
1694 //
1695 // Only allow access to the implementation library in the following condition:
1696 // * No sdk_version specified on the referencing module.
Paul Duffin9b879592020-05-26 13:21:35 +01001697 // * The referencing module is in the same apex as this.
Jiyong Parkf1691d22021-03-29 20:11:58 +09001698 if sdkVersion.Kind == android.SdkPrivate || withinSameApexesAs(ctx, module) {
Paul Duffindaaa3322020-05-26 18:13:57 +01001699 if headerJars {
1700 return module.HeaderJars()
1701 } else {
1702 return module.ImplementationJars()
1703 }
Sundong Ahn054b19a2018-10-19 13:46:09 +09001704 }
Jiyong Parkc678ad32018-04-10 13:07:10 +09001705 }
Paul Duffinb05d4292020-05-20 12:19:10 +01001706
Paul Duffin23970f42020-05-20 14:20:02 +01001707 return module.selectHeaderJarsForSdkVersion(ctx, sdkVersion)
Jiyong Parkc678ad32018-04-10 13:07:10 +09001708}
1709
Sundong Ahn241cd372018-07-13 16:16:44 +09001710// to satisfy SdkLibraryDependency interface
Jiyong Parkf1691d22021-03-29 20:11:58 +09001711func (module *SdkLibrary) SdkHeaderJars(ctx android.BaseModuleContext, sdkVersion android.SdkSpec) android.Paths {
Paul Duffind1b3a922020-01-22 11:57:20 +00001712 return module.sdkJars(ctx, sdkVersion, true /*headerJars*/)
1713}
1714
1715// to satisfy SdkLibraryDependency interface
Jiyong Parkf1691d22021-03-29 20:11:58 +09001716func (module *SdkLibrary) SdkImplementationJars(ctx android.BaseModuleContext, sdkVersion android.SdkSpec) android.Paths {
Paul Duffind1b3a922020-01-22 11:57:20 +00001717 return module.sdkJars(ctx, sdkVersion, false /*headerJars*/)
Sundong Ahn241cd372018-07-13 16:16:44 +09001718}
1719
Colin Cross571cccf2019-02-04 11:22:08 -08001720var javaSdkLibrariesKey = android.NewOnceKey("javaSdkLibraries")
1721
Jiyong Park82484c02018-04-23 21:41:26 +09001722func javaSdkLibraries(config android.Config) *[]string {
Colin Cross571cccf2019-02-04 11:22:08 -08001723 return config.Once(javaSdkLibrariesKey, func() interface{} {
Jiyong Park82484c02018-04-23 21:41:26 +09001724 return &[]string{}
1725 }).(*[]string)
1726}
1727
Paul Duffin749f98f2019-12-30 17:23:46 +00001728func (module *SdkLibrary) getApiDir() string {
1729 return proptools.StringDefault(module.sdkLibraryProperties.Api_dir, "api")
1730}
1731
Jiyong Parkc678ad32018-04-10 13:07:10 +09001732// For a java_sdk_library module, create internal modules for stubs, docs,
1733// runtime libs and xml file. If requested, the stubs and docs are created twice
1734// once for public API level and once for system API level
Paul Duffinf0229202020-04-29 16:47:28 +01001735func (module *SdkLibrary) CreateInternalModules(mctx android.DefaultableHookContext) {
1736 // If the module has been disabled then don't create any child modules.
1737 if !module.Enabled() {
1738 return
1739 }
1740
Paul Duffina18abc22020-05-16 18:54:24 +01001741 if len(module.properties.Srcs) == 0 {
Inseob Kimc0907f12019-02-08 21:00:45 +09001742 mctx.PropertyErrorf("srcs", "java_sdk_library must specify srcs")
Jooyung Han58f26ab2019-12-18 15:34:32 +09001743 return
Inseob Kimc0907f12019-02-08 21:00:45 +09001744 }
1745
Paul Duffin37e0b772019-12-30 17:20:10 +00001746 // If this builds against standard libraries (i.e. is not part of the core libraries)
Paul Duffin4f5c1ef2020-11-19 14:53:43 +00001747 // then assume it provides both system and test apis.
Jiyong Parkf1691d22021-03-29 20:11:58 +09001748 sdkDep := decodeSdkDep(mctx, android.SdkContext(&module.Library))
Paul Duffin37e0b772019-12-30 17:20:10 +00001749 hasSystemAndTestApis := sdkDep.hasStandardLibs()
Paul Duffin3375e352020-04-28 10:44:03 +01001750 module.sdkLibraryProperties.Generate_system_and_test_apis = hasSystemAndTestApis
Paul Duffin4f5c1ef2020-11-19 14:53:43 +00001751
Jaewoong Jung18aefc12020-12-21 09:11:10 -08001752 missingCurrentApi := false
Inseob Kim8098faa2019-03-18 10:19:51 +09001753
Paul Duffin3375e352020-04-28 10:44:03 +01001754 generatedScopes := module.getGeneratedApiScopes(mctx)
Paul Duffind1b3a922020-01-22 11:57:20 +00001755
Paul Duffin749f98f2019-12-30 17:23:46 +00001756 apiDir := module.getApiDir()
Paul Duffin3375e352020-04-28 10:44:03 +01001757 for _, scope := range generatedScopes {
Inseob Kim8098faa2019-03-18 10:19:51 +09001758 for _, api := range []string{"current.txt", "removed.txt"} {
Paul Duffind1b3a922020-01-22 11:57:20 +00001759 path := path.Join(mctx.ModuleDir(), apiDir, scope.apiFilePrefix+api)
Inseob Kim8098faa2019-03-18 10:19:51 +09001760 p := android.ExistentPathForSource(mctx, path)
1761 if !p.Valid() {
Colin Cross18f840c2021-05-20 17:56:54 -07001762 if mctx.Config().AllowMissingDependencies() {
1763 mctx.AddMissingDependencies([]string{path})
1764 } else {
1765 mctx.ModuleErrorf("Current api file %#v doesn't exist", path)
1766 missingCurrentApi = true
1767 }
Inseob Kim8098faa2019-03-18 10:19:51 +09001768 }
1769 }
1770 }
1771
Jaewoong Jung18aefc12020-12-21 09:11:10 -08001772 if missingCurrentApi {
Inseob Kim8098faa2019-03-18 10:19:51 +09001773 script := "build/soong/scripts/gen-java-current-api-files.sh"
1774 p := android.ExistentPathForSource(mctx, script)
1775
1776 if !p.Valid() {
1777 panic(fmt.Sprintf("script file %s doesn't exist", script))
1778 }
1779
1780 mctx.ModuleErrorf("One or more current api files are missing. "+
1781 "You can update them by:\n"+
Paul Duffin37e0b772019-12-30 17:20:10 +00001782 "%s %q %s && m update-api",
Paul Duffind1b3a922020-01-22 11:57:20 +00001783 script, filepath.Join(mctx.ModuleDir(), apiDir),
Paul Duffin3375e352020-04-28 10:44:03 +01001784 strings.Join(generatedScopes.Strings(func(s *apiScope) string { return s.apiFilePrefix }), " "))
Inseob Kim8098faa2019-03-18 10:19:51 +09001785 return
1786 }
1787
Paul Duffin3375e352020-04-28 10:44:03 +01001788 for _, scope := range generatedScopes {
Paul Duffin15f34ef2020-07-20 18:04:44 +01001789 // Use the stubs source name for legacy reasons.
1790 module.createStubsSourcesAndApi(mctx, scope, module.stubsSourceModuleName(scope), scope.droidstubsArgs)
Paul Duffin0ff08bd2020-04-29 13:30:54 +01001791
Paul Duffind1b3a922020-01-22 11:57:20 +00001792 module.createStubsLibrary(mctx, scope)
Inseob Kimc0907f12019-02-08 21:00:45 +09001793 }
1794
Paul Duffindfa131e2020-05-15 20:37:11 +01001795 if module.requiresRuntimeImplementationLibrary() {
Paul Duffin5df79302020-05-16 15:52:12 +01001796 // Create child module to create an implementation library.
1797 //
1798 // This temporarily creates a second implementation library that can be explicitly
1799 // referenced.
1800 //
1801 // TODO(b/156618935) - update comment once only one implementation library is created.
1802 module.createImplLibrary(mctx)
1803
Paul Duffindfa131e2020-05-15 20:37:11 +01001804 // Only create an XML permissions file that declares the library as being usable
1805 // as a shared library if required.
1806 if module.sharedLibrary() {
1807 module.createXmlFile(mctx)
1808 }
Paul Duffin43db9be2019-12-30 17:35:49 +00001809
1810 // record java_sdk_library modules so that they are exported to make
1811 javaSdkLibraries := javaSdkLibraries(mctx.Config())
1812 javaSdkLibrariesLock.Lock()
1813 defer javaSdkLibrariesLock.Unlock()
1814 *javaSdkLibraries = append(*javaSdkLibraries, module.BaseModuleName())
1815 }
Anton Hansson7f66efa2020-10-08 14:47:23 +01001816
1817 // Add the impl_only_libs *after* we're done using the Libs prop in submodules.
1818 module.properties.Libs = append(module.properties.Libs, module.sdkLibraryProperties.Impl_only_libs...)
Inseob Kimc0907f12019-02-08 21:00:45 +09001819}
1820
1821func (module *SdkLibrary) InitSdkLibraryProperties() {
Colin Crossce6734e2020-06-15 16:09:53 -07001822 module.addHostAndDeviceProperties()
1823 module.AddProperties(&module.sdkLibraryProperties)
Sundong Ahn054b19a2018-10-19 13:46:09 +09001824
Paul Duffin71b33cc2021-06-23 11:39:47 +01001825 module.initSdkLibraryComponent(module)
Paul Duffin859fe962020-05-15 10:20:31 +01001826
Paul Duffina18abc22020-05-16 18:54:24 +01001827 module.properties.Installable = proptools.BoolPtr(true)
1828 module.deviceProperties.IsSDKLibrary = true
Inseob Kimc0907f12019-02-08 21:00:45 +09001829}
Sundong Ahn054b19a2018-10-19 13:46:09 +09001830
Paul Duffindfa131e2020-05-15 20:37:11 +01001831func (module *SdkLibrary) requiresRuntimeImplementationLibrary() bool {
1832 return !proptools.Bool(module.sdkLibraryProperties.Api_only)
1833}
1834
Jiyong Park932cdfe2020-05-28 00:19:53 +09001835func (module *SdkLibrary) defaultsToStubs() bool {
1836 return proptools.Bool(module.sdkLibraryProperties.Default_to_stubs)
1837}
1838
Paul Duffin1b1e8062020-05-08 13:44:43 +01001839// Defines how to name the individual component modules the sdk library creates.
1840type sdkLibraryComponentNamingScheme interface {
1841 stubsLibraryModuleName(scope *apiScope, baseName string) string
1842
1843 stubsSourceModuleName(scope *apiScope, baseName string) string
Paul Duffin1b1e8062020-05-08 13:44:43 +01001844}
1845
1846type defaultNamingScheme struct {
1847}
1848
1849func (s *defaultNamingScheme) stubsLibraryModuleName(scope *apiScope, baseName string) string {
1850 return scope.stubsLibraryModuleName(baseName)
1851}
1852
1853func (s *defaultNamingScheme) stubsSourceModuleName(scope *apiScope, baseName string) string {
1854 return scope.stubsSourceModuleName(baseName)
1855}
1856
Paul Duffin1b1e8062020-05-08 13:44:43 +01001857var _ sdkLibraryComponentNamingScheme = (*defaultNamingScheme)(nil)
1858
Jaewoong Jungbc15e3a2021-03-10 17:02:43 -08001859func moduleStubLinkType(name string) (stub bool, ret sdkLinkType) {
Anton Hansson2d0c1942020-05-25 12:20:51 +01001860 // This suffix-based approach is fragile and could potentially mis-trigger.
1861 // TODO(b/155164730): Clean this up when modules no longer reference sdk_lib stubs directly.
Anton Hansson08f476b2021-04-07 15:32:19 +01001862 if strings.HasSuffix(name, apiScopePublic.stubsLibraryModuleNameSuffix()) {
1863 if name == "hwbinder.stubs" || name == "libcore_private.stubs" {
1864 // Due to a previous bug, these modules were not considered stubs, so we retain that.
1865 return false, javaPlatform
1866 }
Anton Hansson2d0c1942020-05-25 12:20:51 +01001867 return true, javaSdk
1868 }
Anton Hansson08f476b2021-04-07 15:32:19 +01001869 if strings.HasSuffix(name, apiScopeSystem.stubsLibraryModuleNameSuffix()) {
Anton Hansson2d0c1942020-05-25 12:20:51 +01001870 return true, javaSystem
1871 }
Anton Hansson08f476b2021-04-07 15:32:19 +01001872 if strings.HasSuffix(name, apiScopeModuleLib.stubsLibraryModuleNameSuffix()) {
Anton Hansson2d0c1942020-05-25 12:20:51 +01001873 return true, javaModule
1874 }
Anton Hansson08f476b2021-04-07 15:32:19 +01001875 if strings.HasSuffix(name, apiScopeTest.stubsLibraryModuleNameSuffix()) {
Anton Hansson2d0c1942020-05-25 12:20:51 +01001876 return true, javaSystem
1877 }
1878 return false, javaPlatform
1879}
1880
Jaewoong Jung4f158ee2019-07-11 10:05:35 -07001881// java_sdk_library is a special Java library that provides optional platform APIs to apps.
1882// In practice, it can be viewed as a combination of several modules: 1) stubs library that clients
1883// are linked against to, 2) droiddoc module that internally generates API stubs source files,
1884// 3) the real runtime shared library that implements the APIs, and 4) XML file for adding
1885// the runtime lib to the classpath at runtime if requested via <uses-library>.
Inseob Kimc0907f12019-02-08 21:00:45 +09001886func SdkLibraryFactory() android.Module {
1887 module := &SdkLibrary{}
Paul Duffinc3091c82020-05-08 14:16:20 +01001888
1889 // Initialize information common between source and prebuilt.
Paul Duffin71b33cc2021-06-23 11:39:47 +01001890 module.initCommon(module)
Paul Duffinc3091c82020-05-08 14:16:20 +01001891
Inseob Kimc0907f12019-02-08 21:00:45 +09001892 module.InitSdkLibraryProperties()
Jooyung Han58f26ab2019-12-18 15:34:32 +09001893 android.InitApexModule(module)
Paul Duffinb6b89a42021-05-06 16:33:43 +01001894 android.InitSdkAwareModule(module)
Sundong Ahn054b19a2018-10-19 13:46:09 +09001895 InitJavaModule(module, android.HostAndDeviceSupported)
Paul Duffin3375e352020-04-28 10:44:03 +01001896
1897 // Initialize the map from scope to scope specific properties.
1898 scopeToProperties := make(map[*apiScope]*ApiScopeProperties)
1899 for _, scope := range allApiScopes {
1900 scopeToProperties[scope] = scope.scopeSpecificProperties(module)
1901 }
1902 module.scopeToProperties = scopeToProperties
1903
Paul Duffin4911a892020-04-29 23:35:13 +01001904 // Add the properties containing visibility rules so that they are checked.
Paul Duffin5df79302020-05-16 15:52:12 +01001905 android.AddVisibilityProperty(module, "impl_library_visibility", &module.sdkLibraryProperties.Impl_library_visibility)
Paul Duffin4911a892020-04-29 23:35:13 +01001906 android.AddVisibilityProperty(module, "stubs_library_visibility", &module.sdkLibraryProperties.Stubs_library_visibility)
1907 android.AddVisibilityProperty(module, "stubs_source_visibility", &module.sdkLibraryProperties.Stubs_source_visibility)
1908
Paul Duffin1b1e8062020-05-08 13:44:43 +01001909 module.SetDefaultableHook(func(ctx android.DefaultableHookContext) {
Paul Duffindfa131e2020-05-15 20:37:11 +01001910 // If no implementation is required then it cannot be used as a shared library
1911 // either.
1912 if !module.requiresRuntimeImplementationLibrary() {
1913 // If shared_library has been explicitly set to true then it is incompatible
1914 // with api_only: true.
1915 if proptools.Bool(module.commonSdkLibraryProperties.Shared_library) {
1916 ctx.PropertyErrorf("api_only/shared_library", "inconsistent settings, shared_library and api_only cannot both be true")
1917 }
1918 // Set shared_library: false.
1919 module.commonSdkLibraryProperties.Shared_library = proptools.BoolPtr(false)
1920 }
1921
Paul Duffin1b1e8062020-05-08 13:44:43 +01001922 if module.initCommonAfterDefaultsApplied(ctx) {
1923 module.CreateInternalModules(ctx)
1924 }
1925 })
Jiyong Parkc678ad32018-04-10 13:07:10 +09001926 return module
1927}
Colin Cross79c7c262019-04-17 11:11:46 -07001928
1929//
1930// SDK library prebuilts
1931//
1932
Paul Duffin56d44902020-01-31 13:36:25 +00001933// Properties associated with each api scope.
1934type sdkLibraryScopeProperties struct {
Colin Cross79c7c262019-04-17 11:11:46 -07001935 Jars []string `android:"path"`
1936
1937 Sdk_version *string
1938
Colin Cross79c7c262019-04-17 11:11:46 -07001939 // List of shared java libs that this module has dependencies to
1940 Libs []string
Paul Duffin3d1248c2020-04-09 00:10:17 +01001941
Paul Duffinc8782502020-04-29 20:45:27 +01001942 // The stubs source.
Paul Duffin3d1248c2020-04-09 00:10:17 +01001943 Stub_srcs []string `android:"path"`
Paul Duffin1fd005d2020-04-09 01:08:11 +01001944
1945 // The current.txt
Paul Duffin0f8faff2020-05-20 16:18:00 +01001946 Current_api *string `android:"path"`
Paul Duffin1fd005d2020-04-09 01:08:11 +01001947
1948 // The removed.txt
Paul Duffin0f8faff2020-05-20 16:18:00 +01001949 Removed_api *string `android:"path"`
Anton Hanssond78eb762021-09-21 15:25:12 +01001950
1951 // Annotation zip
1952 Annotations *string `android:"path"`
Colin Cross79c7c262019-04-17 11:11:46 -07001953}
1954
Paul Duffin56d44902020-01-31 13:36:25 +00001955type sdkLibraryImportProperties struct {
Paul Duffinfcfd7912020-01-31 17:54:30 +00001956 // List of shared java libs, common to all scopes, that this module has
1957 // dependencies to
1958 Libs []string
Paul Duffin1267d872021-04-16 17:21:36 +01001959
1960 // If set to true, compile dex files for the stubs. Defaults to false.
1961 Compile_dex *bool
Paul Duffin869de142021-07-15 14:14:41 +01001962
1963 // If not empty, classes are restricted to the specified packages and their sub-packages.
Paul Duffin869de142021-07-15 14:14:41 +01001964 Permitted_packages []string
Paul Duffin56d44902020-01-31 13:36:25 +00001965}
1966
Paul Duffineedc5d52020-06-12 17:46:39 +01001967type SdkLibraryImport struct {
Colin Cross79c7c262019-04-17 11:11:46 -07001968 android.ModuleBase
1969 android.DefaultableModuleBase
1970 prebuilt android.Prebuilt
Paul Duffindd46f712020-02-10 13:37:10 +00001971 android.ApexModuleBase
1972 android.SdkBase
Colin Cross79c7c262019-04-17 11:11:46 -07001973
Paul Duffin37856732021-02-26 14:24:15 +00001974 hiddenAPI
Jiakai Zhang204356f2021-09-09 08:12:46 +00001975 dexpreopter
Paul Duffin37856732021-02-26 14:24:15 +00001976
Colin Cross79c7c262019-04-17 11:11:46 -07001977 properties sdkLibraryImportProperties
1978
Paul Duffin46a26a82020-04-07 19:27:04 +01001979 // Map from api scope to the scope specific property structure.
1980 scopeProperties map[*apiScope]*sdkLibraryScopeProperties
1981
Paul Duffin56d44902020-01-31 13:36:25 +00001982 commonToSdkLibraryAndImport
Paul Duffineedc5d52020-06-12 17:46:39 +01001983
1984 // The reference to the implementation library created by the source module.
1985 // Is nil if the source module does not exist.
1986 implLibraryModule *Library
1987
1988 // The reference to the xml permissions module created by the source module.
1989 // Is nil if the source module does not exist.
1990 xmlPermissionsFileModule *sdkLibraryXml
Paul Duffin39853512021-02-26 11:09:39 +00001991
Jeongik Chad5fe8782021-07-08 01:13:11 +09001992 // Build path to the dex implementation jar obtained from the prebuilt_apex, if any.
Martin Stjernholm8be1e6d2021-09-15 03:34:04 +01001993 dexJarFile OptionalDexJarPath
Jeongik Chad5fe8782021-07-08 01:13:11 +09001994
1995 // Expected install file path of the source module(sdk_library)
1996 // or dex implementation jar obtained from the prebuilt_apex, if any.
1997 installFile android.Path
Colin Cross79c7c262019-04-17 11:11:46 -07001998}
1999
Paul Duffineedc5d52020-06-12 17:46:39 +01002000var _ SdkLibraryDependency = (*SdkLibraryImport)(nil)
Colin Cross79c7c262019-04-17 11:11:46 -07002001
Paul Duffin46a26a82020-04-07 19:27:04 +01002002// The type of a structure that contains a field of type sdkLibraryScopeProperties
2003// for each apiscope in allApiScopes, e.g. something like:
2004// struct {
2005// Public sdkLibraryScopeProperties
2006// System sdkLibraryScopeProperties
2007// ...
2008// }
2009var allScopeStructType = createAllScopePropertiesStructType()
2010
2011// Dynamically create a structure type for each apiscope in allApiScopes.
2012func createAllScopePropertiesStructType() reflect.Type {
2013 var fields []reflect.StructField
2014 for _, apiScope := range allApiScopes {
2015 field := reflect.StructField{
2016 Name: apiScope.fieldName,
2017 Type: reflect.TypeOf(sdkLibraryScopeProperties{}),
2018 }
2019 fields = append(fields, field)
2020 }
2021
2022 return reflect.StructOf(fields)
2023}
2024
2025// Create an instance of the scope specific structure type and return a map
2026// from apiscope to a pointer to each scope specific field.
2027func createPropertiesInstance() (interface{}, map[*apiScope]*sdkLibraryScopeProperties) {
2028 allScopePropertiesPtr := reflect.New(allScopeStructType)
2029 allScopePropertiesStruct := allScopePropertiesPtr.Elem()
2030 scopeProperties := make(map[*apiScope]*sdkLibraryScopeProperties)
2031
2032 for _, apiScope := range allApiScopes {
2033 field := allScopePropertiesStruct.FieldByName(apiScope.fieldName)
2034 scopeProperties[apiScope] = field.Addr().Interface().(*sdkLibraryScopeProperties)
2035 }
2036
2037 return allScopePropertiesPtr.Interface(), scopeProperties
2038}
2039
Jaewoong Jung4f158ee2019-07-11 10:05:35 -07002040// java_sdk_library_import imports a prebuilt java_sdk_library.
Colin Cross79c7c262019-04-17 11:11:46 -07002041func sdkLibraryImportFactory() android.Module {
Paul Duffineedc5d52020-06-12 17:46:39 +01002042 module := &SdkLibraryImport{}
Colin Cross79c7c262019-04-17 11:11:46 -07002043
Paul Duffin46a26a82020-04-07 19:27:04 +01002044 allScopeProperties, scopeToProperties := createPropertiesInstance()
2045 module.scopeProperties = scopeToProperties
2046 module.AddProperties(&module.properties, allScopeProperties)
Colin Cross79c7c262019-04-17 11:11:46 -07002047
Paul Duffinc3091c82020-05-08 14:16:20 +01002048 // Initialize information common between source and prebuilt.
Paul Duffin71b33cc2021-06-23 11:39:47 +01002049 module.initCommon(module)
Paul Duffinc3091c82020-05-08 14:16:20 +01002050
Paul Duffin0bdcb272020-02-06 15:24:57 +00002051 android.InitPrebuiltModule(module, &[]string{""})
Paul Duffindd46f712020-02-10 13:37:10 +00002052 android.InitApexModule(module)
2053 android.InitSdkAwareModule(module)
Colin Cross79c7c262019-04-17 11:11:46 -07002054 InitJavaModule(module, android.HostAndDeviceSupported)
2055
Paul Duffin1b1e8062020-05-08 13:44:43 +01002056 module.SetDefaultableHook(func(mctx android.DefaultableHookContext) {
2057 if module.initCommonAfterDefaultsApplied(mctx) {
2058 module.createInternalModules(mctx)
2059 }
2060 })
Colin Cross79c7c262019-04-17 11:11:46 -07002061 return module
2062}
2063
Paul Duffin630b11e2021-07-15 13:35:26 +01002064var _ PermittedPackagesForUpdatableBootJars = (*SdkLibraryImport)(nil)
2065
2066func (module *SdkLibraryImport) PermittedPackagesForUpdatableBootJars() []string {
2067 return module.properties.Permitted_packages
2068}
2069
Paul Duffineedc5d52020-06-12 17:46:39 +01002070func (module *SdkLibraryImport) Prebuilt() *android.Prebuilt {
Colin Cross79c7c262019-04-17 11:11:46 -07002071 return &module.prebuilt
2072}
2073
Paul Duffineedc5d52020-06-12 17:46:39 +01002074func (module *SdkLibraryImport) Name() string {
Colin Cross79c7c262019-04-17 11:11:46 -07002075 return module.prebuilt.Name(module.ModuleBase.Name())
2076}
2077
Paul Duffineedc5d52020-06-12 17:46:39 +01002078func (module *SdkLibraryImport) createInternalModules(mctx android.DefaultableHookContext) {
Colin Cross79c7c262019-04-17 11:11:46 -07002079
Paul Duffin50061512020-01-21 16:31:05 +00002080 // If the build is configured to use prebuilts then force this to be preferred.
Jeongik Cha816a23a2020-07-08 01:09:23 +09002081 if mctx.Config().AlwaysUsePrebuiltSdks() {
Paul Duffin50061512020-01-21 16:31:05 +00002082 module.prebuilt.ForcePrefer()
2083 }
2084
Paul Duffin46a26a82020-04-07 19:27:04 +01002085 for apiScope, scopeProperties := range module.scopeProperties {
Paul Duffin56d44902020-01-31 13:36:25 +00002086 if len(scopeProperties.Jars) == 0 {
2087 continue
2088 }
2089
Paul Duffinbbb546b2020-04-09 00:07:11 +01002090 module.createJavaImportForStubs(mctx, apiScope, scopeProperties)
Paul Duffin3d1248c2020-04-09 00:10:17 +01002091
Paul Duffin0f8faff2020-05-20 16:18:00 +01002092 if len(scopeProperties.Stub_srcs) > 0 {
2093 module.createPrebuiltStubsSources(mctx, apiScope, scopeProperties)
2094 }
Paul Duffin56d44902020-01-31 13:36:25 +00002095 }
Colin Cross79c7c262019-04-17 11:11:46 -07002096
2097 javaSdkLibraries := javaSdkLibraries(mctx.Config())
2098 javaSdkLibrariesLock.Lock()
2099 defer javaSdkLibrariesLock.Unlock()
2100 *javaSdkLibraries = append(*javaSdkLibraries, module.BaseModuleName())
2101}
2102
Paul Duffineedc5d52020-06-12 17:46:39 +01002103func (module *SdkLibraryImport) createJavaImportForStubs(mctx android.DefaultableHookContext, apiScope *apiScope, scopeProperties *sdkLibraryScopeProperties) {
Paul Duffinbbb546b2020-04-09 00:07:11 +01002104 // Creates a java import for the jar with ".stubs" suffix
2105 props := struct {
Paul Duffin1dbe3ca2020-05-16 09:57:59 +01002106 Name *string
2107 Sdk_version *string
2108 Libs []string
2109 Jars []string
2110 Prefer *bool
Paul Duffin1267d872021-04-16 17:21:36 +01002111 Compile_dex *bool
Paul Duffinbbb546b2020-04-09 00:07:11 +01002112 }{}
Paul Duffinc3091c82020-05-08 14:16:20 +01002113 props.Name = proptools.StringPtr(module.stubsLibraryModuleName(apiScope))
Paul Duffinbbb546b2020-04-09 00:07:11 +01002114 props.Sdk_version = scopeProperties.Sdk_version
2115 // Prepend any of the libs from the legacy public properties to the libs for each of the
2116 // scopes to avoid having to duplicate them in each scope.
2117 props.Libs = append(module.properties.Libs, scopeProperties.Libs...)
2118 props.Jars = scopeProperties.Jars
Paul Duffin1dbe3ca2020-05-16 09:57:59 +01002119
Paul Duffin38b57852020-05-13 16:08:09 +01002120 // The imports are preferred if the java_sdk_library_import is preferred.
2121 props.Prefer = proptools.BoolPtr(module.prebuilt.Prefer())
Paul Duffin859fe962020-05-15 10:20:31 +01002122
Paul Duffin1267d872021-04-16 17:21:36 +01002123 // The imports need to be compiled to dex if the java_sdk_library_import requests it.
Paul Duffinf4600f62021-05-13 22:34:45 +01002124 compileDex := module.properties.Compile_dex
2125 if module.stubLibrariesCompiledForDex() {
2126 compileDex = proptools.BoolPtr(true)
2127 }
2128 props.Compile_dex = compileDex
Paul Duffin1267d872021-04-16 17:21:36 +01002129
Paul Duffin859fe962020-05-15 10:20:31 +01002130 mctx.CreateModule(ImportFactory, &props, module.sdkComponentPropertiesForChildLibrary())
Paul Duffinbbb546b2020-04-09 00:07:11 +01002131}
2132
Paul Duffineedc5d52020-06-12 17:46:39 +01002133func (module *SdkLibraryImport) createPrebuiltStubsSources(mctx android.DefaultableHookContext, apiScope *apiScope, scopeProperties *sdkLibraryScopeProperties) {
Paul Duffin3d1248c2020-04-09 00:10:17 +01002134 props := struct {
Paul Duffin38b57852020-05-13 16:08:09 +01002135 Name *string
2136 Srcs []string
2137 Prefer *bool
Paul Duffin3d1248c2020-04-09 00:10:17 +01002138 }{}
Paul Duffinc3091c82020-05-08 14:16:20 +01002139 props.Name = proptools.StringPtr(module.stubsSourceModuleName(apiScope))
Paul Duffin3d1248c2020-04-09 00:10:17 +01002140 props.Srcs = scopeProperties.Stub_srcs
2141 mctx.CreateModule(PrebuiltStubsSourcesFactory, &props)
Paul Duffin38b57852020-05-13 16:08:09 +01002142
2143 // The stubs source is preferred if the java_sdk_library_import is preferred.
2144 props.Prefer = proptools.BoolPtr(module.prebuilt.Prefer())
Paul Duffin3d1248c2020-04-09 00:10:17 +01002145}
2146
Paul Duffin44f1d842020-06-26 20:17:02 +01002147// Add the dependencies on the child module in the component deps mutator so that it
2148// creates references to the prebuilt and not the source modules.
2149func (module *SdkLibraryImport) ComponentDepsMutator(ctx android.BottomUpMutatorContext) {
Paul Duffin46a26a82020-04-07 19:27:04 +01002150 for apiScope, scopeProperties := range module.scopeProperties {
Paul Duffin56d44902020-01-31 13:36:25 +00002151 if len(scopeProperties.Jars) == 0 {
2152 continue
2153 }
2154
2155 // Add dependencies to the prebuilt stubs library
Paul Duffin864116c2021-04-02 10:24:13 +01002156 ctx.AddVariationDependencies(nil, apiScope.stubsTag, android.PrebuiltNameFromSource(module.stubsLibraryModuleName(apiScope)))
Paul Duffin0f8faff2020-05-20 16:18:00 +01002157
2158 if len(scopeProperties.Stub_srcs) > 0 {
2159 // Add dependencies to the prebuilt stubs source library
Paul Duffin864116c2021-04-02 10:24:13 +01002160 ctx.AddVariationDependencies(nil, apiScope.stubsSourceTag, android.PrebuiltNameFromSource(module.stubsSourceModuleName(apiScope)))
Paul Duffin0f8faff2020-05-20 16:18:00 +01002161 }
Paul Duffin56d44902020-01-31 13:36:25 +00002162 }
Paul Duffin44f1d842020-06-26 20:17:02 +01002163}
2164
2165// Add other dependencies as normal.
2166func (module *SdkLibraryImport) DepsMutator(ctx android.BottomUpMutatorContext) {
Paul Duffineedc5d52020-06-12 17:46:39 +01002167
2168 implName := module.implLibraryModuleName()
2169 if ctx.OtherModuleExists(implName) {
2170 ctx.AddVariationDependencies(nil, implLibraryTag, implName)
2171
2172 xmlPermissionsModuleName := module.xmlPermissionsModuleName()
2173 if module.sharedLibrary() && ctx.OtherModuleExists(xmlPermissionsModuleName) {
2174 // Add dependency to the rule for generating the xml permissions file
2175 ctx.AddDependency(module, xmlPermissionsFileTag, xmlPermissionsModuleName)
2176 }
2177 }
Colin Cross79c7c262019-04-17 11:11:46 -07002178}
2179
Jiakai Zhang204356f2021-09-09 08:12:46 +00002180func (module *SdkLibraryImport) AndroidMkEntries() []android.AndroidMkEntries {
2181 // For an SDK library imported from a prebuilt APEX, we don't need a Make module for itself, as we
2182 // don't need to install it. However, we need to add its dexpreopt outputs as sub-modules, if it
2183 // is preopted.
2184 dexpreoptEntries := module.dexpreopter.AndroidMkEntriesForApex()
2185 return append(dexpreoptEntries, android.AndroidMkEntries{Disabled: true})
2186}
2187
Jiyong Park45bf82e2020-12-15 22:29:02 +09002188var _ android.ApexModule = (*SdkLibraryImport)(nil)
2189
2190// Implements android.ApexModule
Paul Duffineedc5d52020-06-12 17:46:39 +01002191func (module *SdkLibraryImport) DepIsInSameApex(mctx android.BaseModuleContext, dep android.Module) bool {
2192 depTag := mctx.OtherModuleDependencyTag(dep)
2193 if depTag == xmlPermissionsFileTag {
2194 return true
2195 }
2196
2197 // None of the other dependencies of the java_sdk_library_import are in the same apex
2198 // as the one that references this module.
2199 return false
2200}
2201
Jiyong Park45bf82e2020-12-15 22:29:02 +09002202// Implements android.ApexModule
Dan Albertc8060532020-07-22 22:32:17 -07002203func (module *SdkLibraryImport) ShouldSupportSdkVersion(ctx android.BaseModuleContext,
2204 sdkVersion android.ApiLevel) error {
Jooyung Han749dc692020-04-15 11:03:39 +09002205 // we don't check prebuilt modules for sdk_version
2206 return nil
2207}
2208
Paul Duffinea8f8082021-06-24 13:25:57 +01002209// Implements android.ApexModule
2210func (module *SdkLibraryImport) UniqueApexVariations() bool {
2211 return module.uniqueApexVariations()
2212}
2213
Paul Duffineedc5d52020-06-12 17:46:39 +01002214func (module *SdkLibraryImport) OutputFiles(tag string) (android.Paths, error) {
Paul Duffin46dc45a2020-05-14 15:39:10 +01002215 return module.commonOutputFiles(tag)
2216}
2217
Paul Duffineedc5d52020-06-12 17:46:39 +01002218func (module *SdkLibraryImport) GenerateAndroidBuildActions(ctx android.ModuleContext) {
Paul Duffina2ae7e02020-09-11 11:55:00 +01002219 module.generateCommonBuildActions(ctx)
2220
Jeongik Chad5fe8782021-07-08 01:13:11 +09002221 // Assume that source module(sdk_library) is installed in /<sdk_library partition>/framework
2222 module.installFile = android.PathForModuleInstall(ctx, "framework", module.Stem()+".jar")
2223
Paul Duffin0f8faff2020-05-20 16:18:00 +01002224 // Record the paths to the prebuilt stubs library and stubs source.
Colin Cross79c7c262019-04-17 11:11:46 -07002225 ctx.VisitDirectDeps(func(to android.Module) {
2226 tag := ctx.OtherModuleDependencyTag(to)
2227
Paul Duffin0f8faff2020-05-20 16:18:00 +01002228 // Extract information from any of the scope specific dependencies.
2229 if scopeTag, ok := tag.(scopeDependencyTag); ok {
2230 apiScope := scopeTag.apiScope
2231 scopePaths := module.getScopePathsCreateIfNeeded(apiScope)
2232
2233 // Extract information from the dependency. The exact information extracted
2234 // is determined by the nature of the dependency which is determined by the tag.
2235 scopeTag.extractDepInfo(ctx, to, scopePaths)
Paul Duffineedc5d52020-06-12 17:46:39 +01002236 } else if tag == implLibraryTag {
2237 if implLibrary, ok := to.(*Library); ok {
2238 module.implLibraryModule = implLibrary
2239 } else {
2240 ctx.ModuleErrorf("implementation library must be of type *java.Library but was %T", to)
2241 }
2242 } else if tag == xmlPermissionsFileTag {
2243 if xmlPermissionsFileModule, ok := to.(*sdkLibraryXml); ok {
2244 module.xmlPermissionsFileModule = xmlPermissionsFileModule
2245 } else {
2246 ctx.ModuleErrorf("xml permissions file module must be of type *sdkLibraryXml but was %T", to)
2247 }
Colin Cross79c7c262019-04-17 11:11:46 -07002248 }
2249 })
Paul Duffin0f8faff2020-05-20 16:18:00 +01002250
2251 // Populate the scope paths with information from the properties.
2252 for apiScope, scopeProperties := range module.scopeProperties {
2253 if len(scopeProperties.Jars) == 0 {
2254 continue
2255 }
2256
2257 paths := module.getScopePathsCreateIfNeeded(apiScope)
Anton Hanssond78eb762021-09-21 15:25:12 +01002258 paths.annotationsZip = android.OptionalPathForModuleSrc(ctx, scopeProperties.Annotations)
Paul Duffin0f8faff2020-05-20 16:18:00 +01002259 paths.currentApiFilePath = android.OptionalPathForModuleSrc(ctx, scopeProperties.Current_api)
2260 paths.removedApiFilePath = android.OptionalPathForModuleSrc(ctx, scopeProperties.Removed_api)
2261 }
Paul Duffin39853512021-02-26 11:09:39 +00002262
2263 if ctx.Device() {
2264 // If this is a variant created for a prebuilt_apex then use the dex implementation jar
2265 // obtained from the associated deapexer module.
2266 ai := ctx.Provider(android.ApexInfoProvider).(android.ApexInfo)
2267 if ai.ForPrebuiltApex {
Paul Duffin39853512021-02-26 11:09:39 +00002268 // Get the path of the dex implementation jar from the `deapexer` module.
Martin Stjernholm44825602021-09-17 01:44:12 +01002269 di := android.FindDeapexerProviderForModule(ctx)
2270 if di == nil {
2271 return // An error has been reported by FindDeapexerProviderForModule.
2272 }
Paul Duffinb4bbf2c2021-06-17 15:59:07 +01002273 if dexOutputPath := di.PrebuiltExportPath(apexRootRelativePathToJavaLib(module.BaseModuleName())); dexOutputPath != nil {
Martin Stjernholm8be1e6d2021-09-15 03:34:04 +01002274 dexJarFile := makeDexJarPathFromPath(dexOutputPath)
2275 module.dexJarFile = dexJarFile
Jiakai Zhang204356f2021-09-09 08:12:46 +00002276 installPath := android.PathForModuleInPartitionInstall(
2277 ctx, "apex", ai.ApexVariationName, apexRootRelativePathToJavaLib(module.BaseModuleName()))
2278 module.installFile = installPath
Martin Stjernholm8be1e6d2021-09-15 03:34:04 +01002279 module.initHiddenAPI(ctx, dexJarFile, module.findScopePaths(apiScopePublic).stubsImplPath[0], nil)
Jiakai Zhang204356f2021-09-09 08:12:46 +00002280
2281 // Dexpreopting.
2282 module.dexpreopter.installPath = module.dexpreopter.getInstallPath(ctx, installPath)
2283 module.dexpreopter.isSDKLibrary = true
2284 module.dexpreopter.uncompressedDex = shouldUncompressDex(ctx, &module.dexpreopter)
2285 module.dexpreopt(ctx, dexOutputPath)
Paul Duffin39853512021-02-26 11:09:39 +00002286 } else {
2287 // This should never happen as a variant for a prebuilt_apex is only created if the
2288 // prebuilt_apex has been configured to export the java library dex file.
Martin Stjernholm44825602021-09-17 01:44:12 +01002289 ctx.ModuleErrorf("internal error: no dex implementation jar available from prebuilt APEX %s", di.ApexModuleName())
Paul Duffin39853512021-02-26 11:09:39 +00002290 }
2291 }
2292 }
Colin Cross79c7c262019-04-17 11:11:46 -07002293}
2294
Jiyong Parkf1691d22021-03-29 20:11:58 +09002295func (module *SdkLibraryImport) sdkJars(ctx android.BaseModuleContext, sdkVersion android.SdkSpec, headerJars bool) android.Paths {
Paul Duffineedc5d52020-06-12 17:46:39 +01002296
2297 // For consistency with SdkLibrary make the implementation jar available to libraries that
2298 // are within the same APEX.
2299 implLibraryModule := module.implLibraryModule
Colin Cross56a83212020-09-15 18:30:11 -07002300 if implLibraryModule != nil && withinSameApexesAs(ctx, module) {
Paul Duffineedc5d52020-06-12 17:46:39 +01002301 if headerJars {
2302 return implLibraryModule.HeaderJars()
2303 } else {
2304 return implLibraryModule.ImplementationJars()
2305 }
2306 }
2307
Paul Duffin23970f42020-05-20 14:20:02 +01002308 return module.selectHeaderJarsForSdkVersion(ctx, sdkVersion)
Paul Duffin56d44902020-01-31 13:36:25 +00002309}
2310
Colin Cross79c7c262019-04-17 11:11:46 -07002311// to satisfy SdkLibraryDependency interface
Jiyong Parkf1691d22021-03-29 20:11:58 +09002312func (module *SdkLibraryImport) SdkHeaderJars(ctx android.BaseModuleContext, sdkVersion android.SdkSpec) android.Paths {
Colin Cross79c7c262019-04-17 11:11:46 -07002313 // This module is just a wrapper for the prebuilt stubs.
Paul Duffineedc5d52020-06-12 17:46:39 +01002314 return module.sdkJars(ctx, sdkVersion, true)
Colin Cross79c7c262019-04-17 11:11:46 -07002315}
2316
2317// to satisfy SdkLibraryDependency interface
Jiyong Parkf1691d22021-03-29 20:11:58 +09002318func (module *SdkLibraryImport) SdkImplementationJars(ctx android.BaseModuleContext, sdkVersion android.SdkSpec) android.Paths {
Colin Cross79c7c262019-04-17 11:11:46 -07002319 // This module is just a wrapper for the stubs.
Paul Duffineedc5d52020-06-12 17:46:39 +01002320 return module.sdkJars(ctx, sdkVersion, false)
2321}
2322
Ulya Trafimovichdbf31662020-12-17 12:07:54 +00002323// to satisfy UsesLibraryDependency interface
Martin Stjernholm8be1e6d2021-09-15 03:34:04 +01002324func (module *SdkLibraryImport) DexJarBuildPath() OptionalDexJarPath {
Paul Duffin39853512021-02-26 11:09:39 +00002325 // The dex implementation jar extracted from the .apex file should be used in preference to the
2326 // source.
Martin Stjernholm8be1e6d2021-09-15 03:34:04 +01002327 if module.dexJarFile.IsSet() {
Paul Duffin39853512021-02-26 11:09:39 +00002328 return module.dexJarFile
2329 }
Paul Duffineedc5d52020-06-12 17:46:39 +01002330 if module.implLibraryModule == nil {
Martin Stjernholm8be1e6d2021-09-15 03:34:04 +01002331 return makeUnsetDexJarPath()
Paul Duffineedc5d52020-06-12 17:46:39 +01002332 } else {
2333 return module.implLibraryModule.DexJarBuildPath()
2334 }
2335}
2336
Ulya Trafimovichdbf31662020-12-17 12:07:54 +00002337// to satisfy UsesLibraryDependency interface
Ulya Trafimovich31e444e2020-08-14 17:32:16 +01002338func (module *SdkLibraryImport) DexJarInstallPath() android.Path {
Jeongik Chad5fe8782021-07-08 01:13:11 +09002339 return module.installFile
Ulya Trafimovich31e444e2020-08-14 17:32:16 +01002340}
2341
Ulya Trafimovichdbf31662020-12-17 12:07:54 +00002342// to satisfy UsesLibraryDependency interface
2343func (module *SdkLibraryImport) ClassLoaderContexts() dexpreopt.ClassLoaderContextMap {
2344 return nil
2345}
2346
Paul Duffineedc5d52020-06-12 17:46:39 +01002347// to satisfy apex.javaDependency interface
2348func (module *SdkLibraryImport) JacocoReportClassesFile() android.Path {
2349 if module.implLibraryModule == nil {
2350 return nil
2351 } else {
2352 return module.implLibraryModule.JacocoReportClassesFile()
2353 }
2354}
2355
2356// to satisfy apex.javaDependency interface
Colin Cross08dca382020-07-21 20:31:17 -07002357func (module *SdkLibraryImport) LintDepSets() LintDepSets {
2358 if module.implLibraryModule == nil {
2359 return LintDepSets{}
2360 } else {
2361 return module.implLibraryModule.LintDepSets()
2362 }
2363}
2364
Jaewoong Jung476b9d62021-05-10 15:30:00 -07002365func (module *SdkLibraryImport) getStrictUpdatabilityLinting() bool {
2366 if module.implLibraryModule == nil {
2367 return false
2368 } else {
2369 return module.implLibraryModule.getStrictUpdatabilityLinting()
2370 }
2371}
2372
2373func (module *SdkLibraryImport) setStrictUpdatabilityLinting(strictLinting bool) {
2374 if module.implLibraryModule != nil {
2375 module.implLibraryModule.setStrictUpdatabilityLinting(strictLinting)
2376 }
2377}
2378
Colin Cross08dca382020-07-21 20:31:17 -07002379// to satisfy apex.javaDependency interface
Paul Duffineedc5d52020-06-12 17:46:39 +01002380func (module *SdkLibraryImport) Stem() string {
2381 return module.BaseModuleName()
Colin Cross79c7c262019-04-17 11:11:46 -07002382}
Jiyong Parke3833882020-02-17 17:28:10 +09002383
Paul Duffin44b481b2020-06-17 16:59:43 +01002384var _ ApexDependency = (*SdkLibraryImport)(nil)
2385
2386// to satisfy java.ApexDependency interface
2387func (module *SdkLibraryImport) HeaderJars() android.Paths {
2388 if module.implLibraryModule == nil {
2389 return nil
2390 } else {
2391 return module.implLibraryModule.HeaderJars()
2392 }
2393}
2394
2395// to satisfy java.ApexDependency interface
2396func (module *SdkLibraryImport) ImplementationAndResourcesJars() android.Paths {
2397 if module.implLibraryModule == nil {
2398 return nil
2399 } else {
2400 return module.implLibraryModule.ImplementationAndResourcesJars()
2401 }
2402}
2403
Jiakai Zhang204356f2021-09-09 08:12:46 +00002404// to satisfy java.DexpreopterInterface interface
2405func (module *SdkLibraryImport) IsInstallable() bool {
2406 return true
2407}
2408
Paul Duffinfef55002021-06-17 14:56:05 +01002409var _ android.RequiredFilesFromPrebuiltApex = (*SdkLibraryImport)(nil)
2410
Paul Duffinb4bbf2c2021-06-17 15:59:07 +01002411func (module *SdkLibraryImport) RequiredFilesFromPrebuiltApex(ctx android.BaseModuleContext) []string {
Paul Duffinfef55002021-06-17 14:56:05 +01002412 name := module.BaseModuleName()
2413 return requiredFilesFromPrebuiltApexForImport(name)
2414}
2415
Jiyong Parke3833882020-02-17 17:28:10 +09002416//
2417// java_sdk_library_xml
2418//
2419type sdkLibraryXml struct {
2420 android.ModuleBase
2421 android.DefaultableModuleBase
2422 android.ApexModuleBase
2423
2424 properties sdkLibraryXmlProperties
2425
2426 outputFilePath android.OutputPath
2427 installDirPath android.InstallPath
Colin Cross56a83212020-09-15 18:30:11 -07002428
2429 hideApexVariantFromMake bool
Jiyong Parke3833882020-02-17 17:28:10 +09002430}
2431
2432type sdkLibraryXmlProperties struct {
2433 // canonical name of the lib
2434 Lib_name *string
Pedro Loureiro9956e5e2021-09-07 17:21:59 +00002435
2436 // Signals that this shared library is part of the bootclasspath starting
2437 // on the version indicated in this attribute.
2438 //
2439 // This will make platforms at this level and above to ignore
2440 // <uses-library> tags with this library name because the library is already
2441 // available
2442 On_bootclasspath_since *string
2443
2444 // Signals that this shared library was part of the bootclasspath before
2445 // (but not including) the version indicated in this attribute.
2446 //
2447 // The system will automatically add a <uses-library> tag with this library to
2448 // apps that target any SDK less than the version indicated in this attribute.
2449 On_bootclasspath_before *string
2450
2451 // Indicates that PackageManager should ignore this shared library if the
2452 // platform is below the version indicated in this attribute.
2453 //
2454 // This means that the device won't recognise this library as installed.
2455 Min_device_sdk *string
2456
2457 // Indicates that PackageManager should ignore this shared library if the
2458 // platform is above the version indicated in this attribute.
2459 //
2460 // This means that the device won't recognise this library as installed.
2461 Max_device_sdk *string
Pedro Loureiroc3621422021-09-28 15:40:23 +00002462
2463 // The SdkLibrary's min api level as a string
2464 //
2465 // This value comes from the ApiLevel of the MinSdkVersion property.
2466 Sdk_library_min_api_level *string
Jiyong Parke3833882020-02-17 17:28:10 +09002467}
2468
2469// java_sdk_library_xml builds the permission xml file for a java_sdk_library.
2470// Not to be used directly by users. java_sdk_library internally uses this.
2471func sdkLibraryXmlFactory() android.Module {
2472 module := &sdkLibraryXml{}
2473
2474 module.AddProperties(&module.properties)
2475
2476 android.InitApexModule(module)
2477 android.InitAndroidArchModule(module, android.DeviceSupported, android.MultilibCommon)
2478
2479 return module
2480}
2481
Colin Crossaede88c2020-08-11 12:17:01 -07002482func (module *sdkLibraryXml) UniqueApexVariations() bool {
2483 // sdkLibraryXml needs a unique variation per APEX because the generated XML file contains the path to the
2484 // mounted APEX, which contains the name of the APEX.
2485 return true
2486}
2487
Jiyong Parke3833882020-02-17 17:28:10 +09002488// from android.PrebuiltEtcModule
Jooyung Han0703fd82020-08-26 22:11:53 +09002489func (module *sdkLibraryXml) BaseDir() string {
2490 return "etc"
2491}
2492
2493// from android.PrebuiltEtcModule
Jiyong Parke3833882020-02-17 17:28:10 +09002494func (module *sdkLibraryXml) SubDir() string {
2495 return "permissions"
2496}
2497
2498// from android.PrebuiltEtcModule
2499func (module *sdkLibraryXml) OutputFile() android.OutputPath {
2500 return module.outputFilePath
2501}
2502
2503// from android.ApexModule
2504func (module *sdkLibraryXml) AvailableFor(what string) bool {
2505 return true
2506}
2507
2508func (module *sdkLibraryXml) DepsMutator(ctx android.BottomUpMutatorContext) {
2509 // do nothing
2510}
2511
Jiyong Park45bf82e2020-12-15 22:29:02 +09002512var _ android.ApexModule = (*sdkLibraryXml)(nil)
2513
2514// Implements android.ApexModule
Dan Albertc8060532020-07-22 22:32:17 -07002515func (module *sdkLibraryXml) ShouldSupportSdkVersion(ctx android.BaseModuleContext,
2516 sdkVersion android.ApiLevel) error {
Jooyung Han749dc692020-04-15 11:03:39 +09002517 // sdkLibraryXml doesn't need to be checked separately because java_sdk_library is checked
2518 return nil
2519}
2520
Jiyong Parke3833882020-02-17 17:28:10 +09002521// File path to the runtime implementation library
Colin Cross56a83212020-09-15 18:30:11 -07002522func (module *sdkLibraryXml) implPath(ctx android.ModuleContext) string {
Jiyong Parke3833882020-02-17 17:28:10 +09002523 implName := proptools.String(module.properties.Lib_name)
Colin Cross56a83212020-09-15 18:30:11 -07002524 if apexInfo := ctx.Provider(android.ApexInfoProvider).(android.ApexInfo); !apexInfo.IsForPlatform() {
Colin Crosse07f2312020-08-13 11:24:56 -07002525 // TODO(b/146468504): ApexVariationName() is only a soong module name, not apex name.
Jiyong Parke3833882020-02-17 17:28:10 +09002526 // In most cases, this works fine. But when apex_name is set or override_apex is used
2527 // this can be wrong.
Colin Cross56a83212020-09-15 18:30:11 -07002528 return fmt.Sprintf("/apex/%s/javalib/%s.jar", apexInfo.ApexVariationName, implName)
Jiyong Parke3833882020-02-17 17:28:10 +09002529 }
2530 partition := "system"
2531 if module.SocSpecific() {
2532 partition = "vendor"
2533 } else if module.DeviceSpecific() {
2534 partition = "odm"
2535 } else if module.ProductSpecific() {
2536 partition = "product"
2537 } else if module.SystemExtSpecific() {
2538 partition = "system_ext"
2539 }
2540 return "/" + partition + "/framework/" + implName + ".jar"
2541}
2542
Pedro Loureiro9956e5e2021-09-07 17:21:59 +00002543func formattedOptionalSdkLevelAttribute(ctx android.ModuleContext, attrName string, value *string) string {
2544 if value == nil {
2545 return ""
2546 }
2547 apiLevel, err := android.ApiLevelFromUser(ctx, *value)
2548 if err != nil {
Pedro Loureiroba6682f2021-10-29 09:32:32 +00002549 // attributes in bp files have underscores but in the xml have dashes.
2550 ctx.PropertyErrorf(strings.ReplaceAll(attrName, "-", "_"), err.Error())
Pedro Loureiro9956e5e2021-09-07 17:21:59 +00002551 return ""
2552 }
Pedro Loureirob638c622021-12-22 15:28:05 +00002553 if apiLevel.IsCurrent() {
2554 // passing "current" would always mean a future release, never the current (or the current in
2555 // progress) which means some conditions would never be triggered.
2556 ctx.PropertyErrorf(strings.ReplaceAll(attrName, "-", "_"),
2557 `"current" is not an allowed value for this attribute`)
2558 return ""
2559 }
2560 return formattedOptionalAttribute(attrName, value)
Pedro Loureiro9956e5e2021-09-07 17:21:59 +00002561}
2562
2563// formats an attribute for the xml permissions file if the value is not null
2564// returns empty string otherwise
2565func formattedOptionalAttribute(attrName string, value *string) string {
2566 if value == nil {
2567 return ""
2568 }
2569 return fmt.Sprintf(` %s=\"%s\"\n`, attrName, *value)
2570}
2571
2572func (module *sdkLibraryXml) permissionsContents(ctx android.ModuleContext) string {
2573 libName := proptools.String(module.properties.Lib_name)
2574 libNameAttr := formattedOptionalAttribute("name", &libName)
2575 filePath := module.implPath(ctx)
2576 filePathAttr := formattedOptionalAttribute("file", &filePath)
Pedro Loureiroba6682f2021-10-29 09:32:32 +00002577 implicitFromAttr := formattedOptionalSdkLevelAttribute(ctx, "on-bootclasspath-since", module.properties.On_bootclasspath_since)
2578 implicitUntilAttr := formattedOptionalSdkLevelAttribute(ctx, "on-bootclasspath-before", module.properties.On_bootclasspath_before)
2579 minSdkAttr := formattedOptionalSdkLevelAttribute(ctx, "min-device-sdk", module.properties.Min_device_sdk)
2580 maxSdkAttr := formattedOptionalSdkLevelAttribute(ctx, "max-device-sdk", module.properties.Max_device_sdk)
Pedro Loureiro196d3e62021-12-22 19:53:01 +00002581 // <library> is understood in all android versions whereas <apex-library> is only understood from API T (and ignored before that).
2582 // 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 +00002583 var libraryTag string
2584 if module.properties.Min_device_sdk != nil {
Pedro Loureiro196d3e62021-12-22 19:53:01 +00002585 libraryTag = ` <apex-library\n`
Pedro Loureiroc3621422021-09-28 15:40:23 +00002586 } else {
2587 libraryTag = ` <library\n`
2588 }
Pedro Loureiro9956e5e2021-09-07 17:21:59 +00002589
2590 return strings.Join([]string{
2591 `<?xml version=\"1.0\" encoding=\"utf-8\"?>\n`,
2592 `<!-- Copyright (C) 2018 The Android Open Source Project\n`,
2593 `\n`,
2594 ` Licensed under the Apache License, Version 2.0 (the \"License\");\n`,
2595 ` you may not use this file except in compliance with the License.\n`,
2596 ` You may obtain a copy of the License at\n`,
2597 `\n`,
2598 ` http://www.apache.org/licenses/LICENSE-2.0\n`,
2599 `\n`,
2600 ` Unless required by applicable law or agreed to in writing, software\n`,
2601 ` distributed under the License is distributed on an \"AS IS\" BASIS,\n`,
2602 ` WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\n`,
2603 ` See the License for the specific language governing permissions and\n`,
2604 ` limitations under the License.\n`,
2605 `-->\n`,
2606 `<permissions>\n`,
Pedro Loureiroc3621422021-09-28 15:40:23 +00002607 libraryTag,
Pedro Loureiro9956e5e2021-09-07 17:21:59 +00002608 libNameAttr,
2609 filePathAttr,
2610 implicitFromAttr,
2611 implicitUntilAttr,
2612 minSdkAttr,
2613 maxSdkAttr,
2614 ` />\n`,
2615 `</permissions>\n`}, "")
2616}
2617
Jiyong Parke3833882020-02-17 17:28:10 +09002618func (module *sdkLibraryXml) GenerateAndroidBuildActions(ctx android.ModuleContext) {
Colin Cross56a83212020-09-15 18:30:11 -07002619 module.hideApexVariantFromMake = !ctx.Provider(android.ApexInfoProvider).(android.ApexInfo).IsForPlatform()
2620
Jiyong Parke3833882020-02-17 17:28:10 +09002621 libName := proptools.String(module.properties.Lib_name)
Pedro Loureiroc3621422021-09-28 15:40:23 +00002622 module.selfValidate(ctx)
Pedro Loureiro9956e5e2021-09-07 17:21:59 +00002623 xmlContent := module.permissionsContents(ctx)
Jiyong Parke3833882020-02-17 17:28:10 +09002624
2625 module.outputFilePath = android.PathForModuleOut(ctx, libName+".xml").OutputPath
Colin Crossf1a035e2020-11-16 17:32:30 -08002626 rule := android.NewRuleBuilder(pctx, ctx)
Jiyong Parke3833882020-02-17 17:28:10 +09002627 rule.Command().
2628 Text("/bin/bash -c \"echo -e '" + xmlContent + "'\" > ").
2629 Output(module.outputFilePath)
2630
Colin Crossf1a035e2020-11-16 17:32:30 -08002631 rule.Build("java_sdk_xml", "Permission XML")
Jiyong Parke3833882020-02-17 17:28:10 +09002632
2633 module.installDirPath = android.PathForModuleInstall(ctx, "etc", module.SubDir())
2634}
2635
2636func (module *sdkLibraryXml) AndroidMkEntries() []android.AndroidMkEntries {
Colin Cross56a83212020-09-15 18:30:11 -07002637 if module.hideApexVariantFromMake {
satayev8f088b02021-12-06 11:40:46 +00002638 return []android.AndroidMkEntries{{
Jiyong Parke3833882020-02-17 17:28:10 +09002639 Disabled: true,
2640 }}
2641 }
2642
satayev8f088b02021-12-06 11:40:46 +00002643 return []android.AndroidMkEntries{{
Jiyong Parke3833882020-02-17 17:28:10 +09002644 Class: "ETC",
2645 OutputFile: android.OptionalPathForPath(module.outputFilePath),
2646 ExtraEntries: []android.AndroidMkExtraEntriesFunc{
Colin Crossaa255532020-07-03 13:18:24 -07002647 func(ctx android.AndroidMkExtraEntriesContext, entries *android.AndroidMkEntries) {
Jiyong Parke3833882020-02-17 17:28:10 +09002648 entries.SetString("LOCAL_MODULE_TAGS", "optional")
Colin Crossc68db4b2021-11-11 18:59:15 -08002649 entries.SetString("LOCAL_MODULE_PATH", module.installDirPath.String())
Jiyong Parke3833882020-02-17 17:28:10 +09002650 entries.SetString("LOCAL_INSTALLED_MODULE_STEM", module.outputFilePath.Base())
2651 },
2652 },
2653 }}
2654}
Paul Duffindd46f712020-02-10 13:37:10 +00002655
Pedro Loureiroc3621422021-09-28 15:40:23 +00002656func (module *sdkLibraryXml) selfValidate(ctx android.ModuleContext) {
2657 module.validateAtLeastTAttributes(ctx)
2658 module.validateMinAndMaxDeviceSdk(ctx)
2659 module.validateMinMaxDeviceSdkAndModuleMinSdk(ctx)
2660 module.validateOnBootclasspathBeforeRequirements(ctx)
2661}
2662
2663func (module *sdkLibraryXml) validateAtLeastTAttributes(ctx android.ModuleContext) {
2664 t := android.ApiLevelOrPanic(ctx, "Tiramisu")
2665 module.attrAtLeastT(ctx, t, module.properties.Min_device_sdk, "min_device_sdk")
2666 module.attrAtLeastT(ctx, t, module.properties.Max_device_sdk, "max_device_sdk")
2667 module.attrAtLeastT(ctx, t, module.properties.On_bootclasspath_before, "on_bootclasspath_before")
2668 module.attrAtLeastT(ctx, t, module.properties.On_bootclasspath_since, "on_bootclasspath_since")
2669}
2670
2671func (module *sdkLibraryXml) attrAtLeastT(ctx android.ModuleContext, t android.ApiLevel, attr *string, attrName string) {
2672 if attr != nil {
2673 if level, err := android.ApiLevelFromUser(ctx, *attr); err == nil {
2674 // we will inform the user of invalid inputs when we try to write the
2675 // permissions xml file so we don't need to do it here
2676 if t.GreaterThan(level) {
2677 ctx.PropertyErrorf(attrName, "Attribute value needs to be at least T")
2678 }
2679 }
2680 }
2681}
2682
2683func (module *sdkLibraryXml) validateMinAndMaxDeviceSdk(ctx android.ModuleContext) {
2684 if module.properties.Min_device_sdk != nil && module.properties.Max_device_sdk != nil {
2685 min, minErr := android.ApiLevelFromUser(ctx, *module.properties.Min_device_sdk)
2686 max, maxErr := android.ApiLevelFromUser(ctx, *module.properties.Max_device_sdk)
2687 if minErr == nil && maxErr == nil {
2688 // we will inform the user of invalid inputs when we try to write the
2689 // permissions xml file so we don't need to do it here
2690 if min.GreaterThan(max) {
2691 ctx.ModuleErrorf("min_device_sdk can't be greater than max_device_sdk")
2692 }
2693 }
2694 }
2695}
2696
2697func (module *sdkLibraryXml) validateMinMaxDeviceSdkAndModuleMinSdk(ctx android.ModuleContext) {
2698 moduleMinApi := android.ApiLevelOrPanic(ctx, *module.properties.Sdk_library_min_api_level)
2699 if module.properties.Min_device_sdk != nil {
2700 api, err := android.ApiLevelFromUser(ctx, *module.properties.Min_device_sdk)
2701 if err == nil {
2702 if moduleMinApi.GreaterThan(api) {
2703 ctx.PropertyErrorf("min_device_sdk", "Can't be less than module's min sdk (%s)", moduleMinApi)
2704 }
2705 }
2706 }
2707 if module.properties.Max_device_sdk != nil {
2708 api, err := android.ApiLevelFromUser(ctx, *module.properties.Max_device_sdk)
2709 if err == nil {
2710 if moduleMinApi.GreaterThan(api) {
2711 ctx.PropertyErrorf("max_device_sdk", "Can't be less than module's min sdk (%s)", moduleMinApi)
2712 }
2713 }
2714 }
2715}
2716
2717func (module *sdkLibraryXml) validateOnBootclasspathBeforeRequirements(ctx android.ModuleContext) {
2718 moduleMinApi := android.ApiLevelOrPanic(ctx, *module.properties.Sdk_library_min_api_level)
2719 if module.properties.On_bootclasspath_before != nil {
2720 t := android.ApiLevelOrPanic(ctx, "Tiramisu")
2721 // if we use the attribute, then we need to do this validation
2722 if moduleMinApi.LessThan(t) {
2723 // if minAPi is < T, then we need to have min_device_sdk (which only accepts T+)
2724 if module.properties.Min_device_sdk == nil {
2725 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")
2726 }
2727 }
2728 }
2729}
2730
Paul Duffindd46f712020-02-10 13:37:10 +00002731type sdkLibrarySdkMemberType struct {
2732 android.SdkMemberTypeBase
2733}
2734
Paul Duffin296701e2021-07-14 10:29:36 +01002735func (s *sdkLibrarySdkMemberType) AddDependencies(ctx android.SdkDependencyContext, dependencyTag blueprint.DependencyTag, names []string) {
2736 ctx.AddVariationDependencies(nil, dependencyTag, names...)
Paul Duffindd46f712020-02-10 13:37:10 +00002737}
2738
2739func (s *sdkLibrarySdkMemberType) IsInstance(module android.Module) bool {
2740 _, ok := module.(*SdkLibrary)
2741 return ok
2742}
2743
2744func (s *sdkLibrarySdkMemberType) AddPrebuiltModule(ctx android.SdkMemberContext, member android.SdkMember) android.BpModule {
2745 return ctx.SnapshotBuilder().AddPrebuiltModule(member, "java_sdk_library_import")
2746}
2747
2748func (s *sdkLibrarySdkMemberType) CreateVariantPropertiesStruct() android.SdkMemberProperties {
2749 return &sdkLibrarySdkMemberProperties{}
2750}
2751
Paul Duffin976b0e52021-04-27 23:20:26 +01002752var javaSdkLibrarySdkMemberType = &sdkLibrarySdkMemberType{
2753 android.SdkMemberTypeBase{
2754 PropertyName: "java_sdk_libs",
2755 SupportsSdk: true,
2756 },
2757}
2758
Paul Duffindd46f712020-02-10 13:37:10 +00002759type sdkLibrarySdkMemberProperties struct {
2760 android.SdkMemberPropertiesBase
2761
2762 // Scope to per scope properties.
Paul Duffin106a3a42022-01-27 16:39:06 +00002763 Scopes map[*apiScope]*scopeProperties
Paul Duffindd46f712020-02-10 13:37:10 +00002764
Paul Duffin3d1248c2020-04-09 00:10:17 +01002765 // The Java stubs source files.
2766 Stub_srcs []string
Paul Duffinf7a64332020-05-13 16:54:55 +01002767
2768 // The naming scheme.
2769 Naming_scheme *string
Paul Duffind7eb1c22020-05-26 20:57:10 +01002770
2771 // True if the java_sdk_library_import is for a shared library, false
2772 // otherwise.
2773 Shared_library *bool
Paul Duffina2ae7e02020-09-11 11:55:00 +01002774
Paul Duffin1267d872021-04-16 17:21:36 +01002775 // True if the stub imports should produce dex jars.
2776 Compile_dex *bool
2777
Paul Duffina2ae7e02020-09-11 11:55:00 +01002778 // The paths to the doctag files to add to the prebuilt.
2779 Doctag_paths android.Paths
Paul Duffin869de142021-07-15 14:14:41 +01002780
2781 Permitted_packages []string
Pedro Loureiro9956e5e2021-09-07 17:21:59 +00002782
2783 // Signals that this shared library is part of the bootclasspath starting
2784 // on the version indicated in this attribute.
2785 //
2786 // This will make platforms at this level and above to ignore
2787 // <uses-library> tags with this library name because the library is already
2788 // available
2789 On_bootclasspath_since *string
2790
2791 // Signals that this shared library was part of the bootclasspath before
2792 // (but not including) the version indicated in this attribute.
2793 //
2794 // The system will automatically add a <uses-library> tag with this library to
2795 // apps that target any SDK less than the version indicated in this attribute.
2796 On_bootclasspath_before *string
2797
2798 // Indicates that PackageManager should ignore this shared library if the
2799 // platform is below the version indicated in this attribute.
2800 //
2801 // This means that the device won't recognise this library as installed.
2802 Min_device_sdk *string
2803
2804 // Indicates that PackageManager should ignore this shared library if the
2805 // platform is above the version indicated in this attribute.
2806 //
2807 // This means that the device won't recognise this library as installed.
2808 Max_device_sdk *string
Paul Duffindd46f712020-02-10 13:37:10 +00002809}
2810
2811type scopeProperties struct {
Paul Duffin1fd005d2020-04-09 01:08:11 +01002812 Jars android.Paths
2813 StubsSrcJar android.Path
2814 CurrentApiFile android.Path
2815 RemovedApiFile android.Path
Paul Duffine7babdb2022-02-10 13:06:54 +00002816 AnnotationsZip android.Path `supported_build_releases:"Tiramisu+"`
Paul Duffin1fd005d2020-04-09 01:08:11 +01002817 SdkVersion string
Paul Duffindd46f712020-02-10 13:37:10 +00002818}
2819
2820func (s *sdkLibrarySdkMemberProperties) PopulateFromVariant(ctx android.SdkMemberContext, variant android.Module) {
2821 sdk := variant.(*SdkLibrary)
2822
Paul Duffin106a3a42022-01-27 16:39:06 +00002823 s.Scopes = make(map[*apiScope]*scopeProperties)
Paul Duffindd46f712020-02-10 13:37:10 +00002824 for _, apiScope := range allApiScopes {
Paul Duffin803a9562020-05-20 11:52:25 +01002825 paths := sdk.findScopePaths(apiScope)
2826 if paths == nil {
2827 continue
2828 }
2829
Paul Duffindd46f712020-02-10 13:37:10 +00002830 jars := paths.stubsImplPath
2831 if len(jars) > 0 {
2832 properties := scopeProperties{}
2833 properties.Jars = jars
Paul Duffin780c5f42020-05-12 15:52:55 +01002834 properties.SdkVersion = sdk.sdkVersionForStubsLibrary(ctx.SdkModuleContext(), apiScope)
Paul Duffin0f8faff2020-05-20 16:18:00 +01002835 properties.StubsSrcJar = paths.stubsSrcJar.Path()
Paul Duffin10269f12020-06-19 18:39:55 +01002836 if paths.currentApiFilePath.Valid() {
2837 properties.CurrentApiFile = paths.currentApiFilePath.Path()
2838 }
2839 if paths.removedApiFilePath.Valid() {
2840 properties.RemovedApiFile = paths.removedApiFilePath.Path()
2841 }
Anton Hanssond78eb762021-09-21 15:25:12 +01002842 // The annotations zip is only available for modules that set annotations_enabled: true.
2843 if paths.annotationsZip.Valid() {
2844 properties.AnnotationsZip = paths.annotationsZip.Path()
2845 }
Paul Duffin106a3a42022-01-27 16:39:06 +00002846 s.Scopes[apiScope] = &properties
Paul Duffindd46f712020-02-10 13:37:10 +00002847 }
2848 }
2849
Paul Duffindfa131e2020-05-15 20:37:11 +01002850 s.Naming_scheme = sdk.commonSdkLibraryProperties.Naming_scheme
Paul Duffind7eb1c22020-05-26 20:57:10 +01002851 s.Shared_library = proptools.BoolPtr(sdk.sharedLibrary())
Paul Duffin1267d872021-04-16 17:21:36 +01002852 s.Compile_dex = sdk.dexProperties.Compile_dex
Paul Duffina2ae7e02020-09-11 11:55:00 +01002853 s.Doctag_paths = sdk.doctagPaths
Paul Duffin869de142021-07-15 14:14:41 +01002854 s.Permitted_packages = sdk.PermittedPackagesForUpdatableBootJars()
Pedro Loureiro9956e5e2021-09-07 17:21:59 +00002855 s.On_bootclasspath_since = sdk.commonSdkLibraryProperties.On_bootclasspath_since
2856 s.On_bootclasspath_before = sdk.commonSdkLibraryProperties.On_bootclasspath_before
2857 s.Min_device_sdk = sdk.commonSdkLibraryProperties.Min_device_sdk
2858 s.Max_device_sdk = sdk.commonSdkLibraryProperties.Max_device_sdk
Paul Duffindd46f712020-02-10 13:37:10 +00002859}
2860
2861func (s *sdkLibrarySdkMemberProperties) AddToPropertySet(ctx android.SdkMemberContext, propertySet android.BpPropertySet) {
Paul Duffinf7a64332020-05-13 16:54:55 +01002862 if s.Naming_scheme != nil {
2863 propertySet.AddProperty("naming_scheme", proptools.String(s.Naming_scheme))
2864 }
Paul Duffind7eb1c22020-05-26 20:57:10 +01002865 if s.Shared_library != nil {
2866 propertySet.AddProperty("shared_library", *s.Shared_library)
2867 }
Paul Duffin1267d872021-04-16 17:21:36 +01002868 if s.Compile_dex != nil {
2869 propertySet.AddProperty("compile_dex", *s.Compile_dex)
2870 }
Paul Duffin869de142021-07-15 14:14:41 +01002871 if len(s.Permitted_packages) > 0 {
2872 propertySet.AddProperty("permitted_packages", s.Permitted_packages)
2873 }
Paul Duffinf7a64332020-05-13 16:54:55 +01002874
Paul Duffindd46f712020-02-10 13:37:10 +00002875 for _, apiScope := range allApiScopes {
2876 if properties, ok := s.Scopes[apiScope]; ok {
Paul Duffin6b836ba2020-05-13 19:19:49 +01002877 scopeSet := propertySet.AddPropertySet(apiScope.propertyName)
Paul Duffindd46f712020-02-10 13:37:10 +00002878
Paul Duffin3d1248c2020-04-09 00:10:17 +01002879 scopeDir := filepath.Join("sdk_library", s.OsPrefix(), apiScope.name)
2880
Paul Duffindd46f712020-02-10 13:37:10 +00002881 var jars []string
2882 for _, p := range properties.Jars {
Paul Duffin3d1248c2020-04-09 00:10:17 +01002883 dest := filepath.Join(scopeDir, ctx.Name()+"-stubs.jar")
Paul Duffindd46f712020-02-10 13:37:10 +00002884 ctx.SnapshotBuilder().CopyToSnapshot(p, dest)
2885 jars = append(jars, dest)
2886 }
2887 scopeSet.AddProperty("jars", jars)
2888
Paul Duffin22628d52021-05-12 23:13:22 +01002889 if ctx.SdkModuleContext().Config().IsEnvTrue("SOONG_SDK_SNAPSHOT_USE_SRCJAR") {
2890 // Copy the stubs source jar into the snapshot zip as is.
2891 srcJarSnapshotPath := filepath.Join(scopeDir, ctx.Name()+".srcjar")
2892 ctx.SnapshotBuilder().CopyToSnapshot(properties.StubsSrcJar, srcJarSnapshotPath)
2893 scopeSet.AddProperty("stub_srcs", []string{srcJarSnapshotPath})
2894 } else {
2895 // Merge the stubs source jar into the snapshot zip so that when it is unpacked
2896 // the source files are also unpacked.
2897 snapshotRelativeDir := filepath.Join(scopeDir, ctx.Name()+"_stub_sources")
2898 ctx.SnapshotBuilder().UnzipToSnapshot(properties.StubsSrcJar, snapshotRelativeDir)
2899 scopeSet.AddProperty("stub_srcs", []string{snapshotRelativeDir})
2900 }
Paul Duffin3d1248c2020-04-09 00:10:17 +01002901
Paul Duffin1fd005d2020-04-09 01:08:11 +01002902 if properties.CurrentApiFile != nil {
2903 currentApiSnapshotPath := filepath.Join(scopeDir, ctx.Name()+".txt")
2904 ctx.SnapshotBuilder().CopyToSnapshot(properties.CurrentApiFile, currentApiSnapshotPath)
2905 scopeSet.AddProperty("current_api", currentApiSnapshotPath)
2906 }
2907
2908 if properties.RemovedApiFile != nil {
2909 removedApiSnapshotPath := filepath.Join(scopeDir, ctx.Name()+"-removed.txt")
Paul Duffin3dbf9fd2020-06-02 13:00:02 +01002910 ctx.SnapshotBuilder().CopyToSnapshot(properties.RemovedApiFile, removedApiSnapshotPath)
Paul Duffin1fd005d2020-04-09 01:08:11 +01002911 scopeSet.AddProperty("removed_api", removedApiSnapshotPath)
2912 }
2913
Anton Hanssond78eb762021-09-21 15:25:12 +01002914 if properties.AnnotationsZip != nil {
2915 annotationsSnapshotPath := filepath.Join(scopeDir, ctx.Name()+"_annotations.zip")
2916 ctx.SnapshotBuilder().CopyToSnapshot(properties.AnnotationsZip, annotationsSnapshotPath)
2917 scopeSet.AddProperty("annotations", annotationsSnapshotPath)
2918 }
2919
Paul Duffindd46f712020-02-10 13:37:10 +00002920 if properties.SdkVersion != "" {
2921 scopeSet.AddProperty("sdk_version", properties.SdkVersion)
2922 }
2923 }
2924 }
2925
Paul Duffina2ae7e02020-09-11 11:55:00 +01002926 if len(s.Doctag_paths) > 0 {
2927 dests := []string{}
2928 for _, p := range s.Doctag_paths {
2929 dest := filepath.Join("doctags", p.Rel())
2930 ctx.SnapshotBuilder().CopyToSnapshot(p, dest)
2931 dests = append(dests, dest)
2932 }
2933 propertySet.AddProperty("doctag_files", dests)
2934 }
Paul Duffindd46f712020-02-10 13:37:10 +00002935}