blob: 05ce97aea502c8beae4361ed9c1efe1302997318 [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 (
Paul Duffindd9d0742020-05-08 15:52:37 +010035 sdkXmlFileSuffix = ".xml"
36 permissionsTemplate = `<?xml version=\"1.0\" encoding=\"utf-8\"?>\n` +
Jooyung Han624058e2019-12-24 18:38:06 +090037 `<!-- Copyright (C) 2018 The Android Open Source Project\n` +
38 `\n` +
Jiyong Parke3833882020-02-17 17:28:10 +090039 ` Licensed under the Apache License, Version 2.0 (the \"License\");\n` +
Jooyung Han624058e2019-12-24 18:38:06 +090040 ` you may not use this file except in compliance with the License.\n` +
41 ` You may obtain a copy of the License at\n` +
42 `\n` +
43 ` http://www.apache.org/licenses/LICENSE-2.0\n` +
44 `\n` +
45 ` Unless required by applicable law or agreed to in writing, software\n` +
Jiyong Parke3833882020-02-17 17:28:10 +090046 ` distributed under the License is distributed on an \"AS IS\" BASIS,\n` +
Jooyung Han624058e2019-12-24 18:38:06 +090047 ` WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\n` +
48 ` See the License for the specific language governing permissions and\n` +
49 ` limitations under the License.\n` +
50 `-->\n` +
51 `<permissions>\n` +
Jiyong Parke3833882020-02-17 17:28:10 +090052 ` <library name=\"%s\" file=\"%s\"/>\n` +
Jooyung Han624058e2019-12-24 18:38:06 +090053 `</permissions>\n`
Jiyong Parkc678ad32018-04-10 13:07:10 +090054)
55
Paul Duffind1b3a922020-01-22 11:57:20 +000056// A tag to associated a dependency with a specific api scope.
57type scopeDependencyTag struct {
58 blueprint.BaseDependencyTag
59 name string
60 apiScope *apiScope
Paul Duffinc8782502020-04-29 20:45:27 +010061
62 // Function for extracting appropriate path information from the dependency.
Colin Crossdcf71b22021-02-01 13:59:03 -080063 depInfoExtractor func(paths *scopePaths, ctx android.ModuleContext, dep android.Module) error
Paul Duffinc8782502020-04-29 20:45:27 +010064}
65
66// Extract tag specific information from the dependency.
67func (tag scopeDependencyTag) extractDepInfo(ctx android.ModuleContext, dep android.Module, paths *scopePaths) {
Colin Crossdcf71b22021-02-01 13:59:03 -080068 err := tag.depInfoExtractor(paths, ctx, dep)
Paul Duffinc8782502020-04-29 20:45:27 +010069 if err != nil {
70 ctx.ModuleErrorf("has an invalid {scopeDependencyTag: %s} dependency on module %s: %s", tag.name, ctx.OtherModuleName(dep), err.Error())
71 }
Paul Duffind1b3a922020-01-22 11:57:20 +000072}
73
Paul Duffin80342d72020-06-26 22:08:43 +010074var _ android.ReplaceSourceWithPrebuilt = (*scopeDependencyTag)(nil)
75
76func (tag scopeDependencyTag) ReplaceSourceWithPrebuilt() bool {
77 return false
78}
79
Paul Duffind1b3a922020-01-22 11:57:20 +000080// Provides information about an api scope, e.g. public, system, test.
81type apiScope struct {
82 // The name of the api scope, e.g. public, system, test
83 name string
84
Paul Duffin97b53b82020-05-05 14:40:52 +010085 // The api scope that this scope extends.
86 extends *apiScope
87
Paul Duffin3375e352020-04-28 10:44:03 +010088 // The legacy enabled status for a specific scope can be dependent on other
89 // properties that have been specified on the library so it is provided by
90 // a function that can determine the status by examining those properties.
91 legacyEnabledStatus func(module *SdkLibrary) bool
92
93 // The default enabled status for non-legacy behavior, which is triggered by
94 // explicitly enabling at least one api scope.
95 defaultEnabledStatus bool
96
97 // Gets a pointer to the scope specific properties.
98 scopeSpecificProperties func(module *SdkLibrary) *ApiScopeProperties
99
Paul Duffin46a26a82020-04-07 19:27:04 +0100100 // The name of the field in the dynamically created structure.
101 fieldName string
102
Paul Duffin6b836ba2020-05-13 19:19:49 +0100103 // The name of the property in the java_sdk_library_import
104 propertyName string
105
Paul Duffind1b3a922020-01-22 11:57:20 +0000106 // The tag to use to depend on the stubs library module.
107 stubsTag scopeDependencyTag
108
Paul Duffin0ff08bd2020-04-29 13:30:54 +0100109 // The tag to use to depend on the stubs source module (if separate from the API module).
110 stubsSourceTag scopeDependencyTag
111
112 // The tag to use to depend on the API file generating module (if separate from the stubs source module).
113 apiFileTag scopeDependencyTag
114
Paul Duffinc8782502020-04-29 20:45:27 +0100115 // The tag to use to depend on the stubs source and API module.
116 stubsSourceAndApiTag scopeDependencyTag
Paul Duffind1b3a922020-01-22 11:57:20 +0000117
118 // The scope specific prefix to add to the api file base of "current.txt" or "removed.txt".
119 apiFilePrefix string
120
121 // The scope specific prefix to add to the sdk library module name to construct a scope specific
122 // module name.
123 moduleSuffix string
124
Paul Duffind1b3a922020-01-22 11:57:20 +0000125 // SDK version that the stubs library is built against. Note that this is always
126 // *current. Older stubs library built with a numbered SDK version is created from
127 // the prebuilt jar.
128 sdkVersion string
Paul Duffin1fb487d2020-04-07 18:50:10 +0100129
Paul Duffin15f34ef2020-07-20 18:04:44 +0100130 // The annotation that identifies this API level, empty for the public API scope.
131 annotation string
132
Paul Duffin1fb487d2020-04-07 18:50:10 +0100133 // Extra arguments to pass to droidstubs for this scope.
Paul Duffin0ff08bd2020-04-29 13:30:54 +0100134 //
Paul Duffin15f34ef2020-07-20 18:04:44 +0100135 // This is not used directly but is used to construct the droidstubsArgs.
136 extraArgs []string
Paul Duffin0ff08bd2020-04-29 13:30:54 +0100137
Paul Duffin15f34ef2020-07-20 18:04:44 +0100138 // The args that must be passed to droidstubs to generate the API and stubs source
139 // for this scope, constructed dynamically by initApiScope().
Paul Duffin0ff08bd2020-04-29 13:30:54 +0100140 //
141 // The API only includes the additional members that this scope adds over the scope
142 // that it extends.
Paul Duffin15f34ef2020-07-20 18:04:44 +0100143 //
144 // The stubs source must include the definitions of everything that is in this
145 // api scope and all the scopes that this one extends.
146 droidstubsArgs []string
Paul Duffin0ff08bd2020-04-29 13:30:54 +0100147
Anton Hansson6478ac12020-05-02 11:19:36 +0100148 // Whether the api scope can be treated as unstable, and should skip compat checks.
149 unstable bool
Paul Duffind1b3a922020-01-22 11:57:20 +0000150}
151
152// Initialize a scope, creating and adding appropriate dependency tags
153func initApiScope(scope *apiScope) *apiScope {
Paul Duffinc8782502020-04-29 20:45:27 +0100154 name := scope.name
Paul Duffin46dc45a2020-05-14 15:39:10 +0100155 scopeByName[name] = scope
156 allScopeNames = append(allScopeNames, name)
Paul Duffin6b836ba2020-05-13 19:19:49 +0100157 scope.propertyName = strings.ReplaceAll(name, "-", "_")
158 scope.fieldName = proptools.FieldNameForProperty(scope.propertyName)
Paul Duffind1b3a922020-01-22 11:57:20 +0000159 scope.stubsTag = scopeDependencyTag{
Paul Duffinc8782502020-04-29 20:45:27 +0100160 name: name + "-stubs",
161 apiScope: scope,
162 depInfoExtractor: (*scopePaths).extractStubsLibraryInfoFromDependency,
Paul Duffind1b3a922020-01-22 11:57:20 +0000163 }
Paul Duffin0ff08bd2020-04-29 13:30:54 +0100164 scope.stubsSourceTag = scopeDependencyTag{
165 name: name + "-stubs-source",
166 apiScope: scope,
167 depInfoExtractor: (*scopePaths).extractStubsSourceInfoFromDep,
168 }
169 scope.apiFileTag = scopeDependencyTag{
170 name: name + "-api",
171 apiScope: scope,
172 depInfoExtractor: (*scopePaths).extractApiInfoFromDep,
173 }
Paul Duffinc8782502020-04-29 20:45:27 +0100174 scope.stubsSourceAndApiTag = scopeDependencyTag{
175 name: name + "-stubs-source-and-api",
176 apiScope: scope,
177 depInfoExtractor: (*scopePaths).extractStubsSourceAndApiInfoFromApiStubsProvider,
Paul Duffind1b3a922020-01-22 11:57:20 +0000178 }
Paul Duffin0ff08bd2020-04-29 13:30:54 +0100179
180 // To get the args needed to generate the stubs source append all the args from
181 // this scope and all the scopes it extends as each set of args adds additional
182 // members to the stubs.
Paul Duffin15f34ef2020-07-20 18:04:44 +0100183 var scopeSpecificArgs []string
184 if scope.annotation != "" {
185 scopeSpecificArgs = []string{"--show-annotation", scope.annotation}
Paul Duffin0ff08bd2020-04-29 13:30:54 +0100186 }
Paul Duffin15f34ef2020-07-20 18:04:44 +0100187 for s := scope; s != nil; s = s.extends {
188 scopeSpecificArgs = append(scopeSpecificArgs, s.extraArgs...)
Paul Duffin0ff08bd2020-04-29 13:30:54 +0100189
Paul Duffin15f34ef2020-07-20 18:04:44 +0100190 // Ensure that the generated stubs includes all the API elements from the API scope
191 // that this scope extends.
192 if s != scope && s.annotation != "" {
193 scopeSpecificArgs = append(scopeSpecificArgs, "--show-for-stub-purposes-annotation", s.annotation)
194 }
195 }
Paul Duffin0ff08bd2020-04-29 13:30:54 +0100196
Paul Duffin15f34ef2020-07-20 18:04:44 +0100197 // Escape any special characters in the arguments. This is needed because droidstubs
198 // passes these directly to the shell command.
199 scope.droidstubsArgs = proptools.ShellEscapeList(scopeSpecificArgs)
Paul Duffin0ff08bd2020-04-29 13:30:54 +0100200
Paul Duffind1b3a922020-01-22 11:57:20 +0000201 return scope
202}
203
Anton Hansson08f476b2021-04-07 15:32:19 +0100204func (scope *apiScope) stubsLibraryModuleNameSuffix() string {
205 return ".stubs" + scope.moduleSuffix
206}
207
Paul Duffinc3091c82020-05-08 14:16:20 +0100208func (scope *apiScope) stubsLibraryModuleName(baseName string) string {
Anton Hansson08f476b2021-04-07 15:32:19 +0100209 return baseName + scope.stubsLibraryModuleNameSuffix()
Paul Duffind1b3a922020-01-22 11:57:20 +0000210}
211
Paul Duffinc8782502020-04-29 20:45:27 +0100212func (scope *apiScope) stubsSourceModuleName(baseName string) string {
Paul Duffindd9d0742020-05-08 15:52:37 +0100213 return baseName + ".stubs.source" + scope.moduleSuffix
Paul Duffind1b3a922020-01-22 11:57:20 +0000214}
215
Paul Duffin0ff08bd2020-04-29 13:30:54 +0100216func (scope *apiScope) apiModuleName(baseName string) string {
Paul Duffindd9d0742020-05-08 15:52:37 +0100217 return baseName + ".api" + scope.moduleSuffix
Paul Duffin0ff08bd2020-04-29 13:30:54 +0100218}
219
Paul Duffin3375e352020-04-28 10:44:03 +0100220func (scope *apiScope) String() string {
221 return scope.name
222}
223
Paul Duffind1b3a922020-01-22 11:57:20 +0000224type apiScopes []*apiScope
225
226func (scopes apiScopes) Strings(accessor func(*apiScope) string) []string {
227 var list []string
228 for _, scope := range scopes {
229 list = append(list, accessor(scope))
230 }
231 return list
232}
233
Jiyong Parkc678ad32018-04-10 13:07:10 +0900234var (
Paul Duffin46dc45a2020-05-14 15:39:10 +0100235 scopeByName = make(map[string]*apiScope)
236 allScopeNames []string
Paul Duffind1b3a922020-01-22 11:57:20 +0000237 apiScopePublic = initApiScope(&apiScope{
Paul Duffin3375e352020-04-28 10:44:03 +0100238 name: "public",
239
240 // Public scope is enabled by default for both legacy and non-legacy modes.
241 legacyEnabledStatus: func(module *SdkLibrary) bool {
242 return true
243 },
244 defaultEnabledStatus: true,
245
246 scopeSpecificProperties: func(module *SdkLibrary) *ApiScopeProperties {
247 return &module.sdkLibraryProperties.Public
248 },
Paul Duffind1b3a922020-01-22 11:57:20 +0000249 sdkVersion: "current",
250 })
251 apiScopeSystem = initApiScope(&apiScope{
Paul Duffin3375e352020-04-28 10:44:03 +0100252 name: "system",
253 extends: apiScopePublic,
254 legacyEnabledStatus: (*SdkLibrary).generateTestAndSystemScopesByDefault,
255 scopeSpecificProperties: func(module *SdkLibrary) *ApiScopeProperties {
256 return &module.sdkLibraryProperties.System
257 },
Paul Duffin15f34ef2020-07-20 18:04:44 +0100258 apiFilePrefix: "system-",
259 moduleSuffix: ".system",
260 sdkVersion: "system_current",
261 annotation: "android.annotation.SystemApi(client=android.annotation.SystemApi.Client.PRIVILEGED_APPS)",
Paul Duffind1b3a922020-01-22 11:57:20 +0000262 })
263 apiScopeTest = initApiScope(&apiScope{
Paul Duffin3375e352020-04-28 10:44:03 +0100264 name: "test",
Anton Hansson4fe970f2020-10-09 10:16:49 +0100265 extends: apiScopeSystem,
Paul Duffin3375e352020-04-28 10:44:03 +0100266 legacyEnabledStatus: (*SdkLibrary).generateTestAndSystemScopesByDefault,
267 scopeSpecificProperties: func(module *SdkLibrary) *ApiScopeProperties {
268 return &module.sdkLibraryProperties.Test
269 },
Paul Duffin15f34ef2020-07-20 18:04:44 +0100270 apiFilePrefix: "test-",
271 moduleSuffix: ".test",
272 sdkVersion: "test_current",
273 annotation: "android.annotation.TestApi",
274 unstable: true,
Paul Duffind1b3a922020-01-22 11:57:20 +0000275 })
Paul Duffin8f265b92020-04-28 14:13:56 +0100276 apiScopeModuleLib = initApiScope(&apiScope{
Paul Duffin6b836ba2020-05-13 19:19:49 +0100277 name: "module-lib",
Paul Duffin8f265b92020-04-28 14:13:56 +0100278 extends: apiScopeSystem,
Paul Duffin0c5bae52020-06-02 13:00:08 +0100279 // The module-lib scope is disabled by default in legacy mode.
Paul Duffin8f265b92020-04-28 14:13:56 +0100280 //
281 // Enabling this would break existing usages.
282 legacyEnabledStatus: func(module *SdkLibrary) bool {
283 return false
284 },
285 scopeSpecificProperties: func(module *SdkLibrary) *ApiScopeProperties {
286 return &module.sdkLibraryProperties.Module_lib
287 },
288 apiFilePrefix: "module-lib-",
289 moduleSuffix: ".module_lib",
290 sdkVersion: "module_current",
Paul Duffin15f34ef2020-07-20 18:04:44 +0100291 annotation: "android.annotation.SystemApi(client=android.annotation.SystemApi.Client.MODULE_LIBRARIES)",
Paul Duffin8f265b92020-04-28 14:13:56 +0100292 })
Paul Duffin0c5bae52020-06-02 13:00:08 +0100293 apiScopeSystemServer = initApiScope(&apiScope{
294 name: "system-server",
295 extends: apiScopePublic,
296 // The system-server scope is disabled by default in legacy mode.
297 //
298 // Enabling this would break existing usages.
299 legacyEnabledStatus: func(module *SdkLibrary) bool {
300 return false
301 },
302 scopeSpecificProperties: func(module *SdkLibrary) *ApiScopeProperties {
303 return &module.sdkLibraryProperties.System_server
304 },
305 apiFilePrefix: "system-server-",
306 moduleSuffix: ".system_server",
307 sdkVersion: "system_server_current",
Paul Duffin15f34ef2020-07-20 18:04:44 +0100308 annotation: "android.annotation.SystemApi(client=android.annotation.SystemApi.Client.SYSTEM_SERVER)",
309 extraArgs: []string{
310 "--hide-annotation", "android.annotation.Hide",
Paul Duffin0c5bae52020-06-02 13:00:08 +0100311 // com.android.* classes are okay in this interface"
Paul Duffin15f34ef2020-07-20 18:04:44 +0100312 "--hide", "InternalClasses",
Paul Duffin0c5bae52020-06-02 13:00:08 +0100313 },
314 })
Paul Duffind1b3a922020-01-22 11:57:20 +0000315 allApiScopes = apiScopes{
316 apiScopePublic,
317 apiScopeSystem,
318 apiScopeTest,
Paul Duffin8f265b92020-04-28 14:13:56 +0100319 apiScopeModuleLib,
Paul Duffin0c5bae52020-06-02 13:00:08 +0100320 apiScopeSystemServer,
Paul Duffind1b3a922020-01-22 11:57:20 +0000321 }
Jiyong Parkc678ad32018-04-10 13:07:10 +0900322)
323
Jiyong Park82484c02018-04-23 21:41:26 +0900324var (
325 javaSdkLibrariesLock sync.Mutex
326)
327
Jiyong Parkc678ad32018-04-10 13:07:10 +0900328// TODO: these are big features that are currently missing
Jiyong Park1be96912018-05-28 18:02:19 +0900329// 1) disallowing linking to the runtime shared lib
330// 2) HTML generation
Jiyong Parkc678ad32018-04-10 13:07:10 +0900331
332func init() {
Paul Duffin43dc1cc2019-12-19 11:18:54 +0000333 RegisterSdkLibraryBuildComponents(android.InitRegistrationContext)
Jiyong Parkc678ad32018-04-10 13:07:10 +0900334
Jiyong Park82484c02018-04-23 21:41:26 +0900335 android.RegisterMakeVarsProvider(pctx, func(ctx android.MakeVarsContext) {
336 javaSdkLibraries := javaSdkLibraries(ctx.Config())
337 sort.Strings(*javaSdkLibraries)
338 ctx.Strict("JAVA_SDK_LIBRARIES", strings.Join(*javaSdkLibraries, " "))
339 })
Paul Duffindd46f712020-02-10 13:37:10 +0000340
341 // Register sdk member types.
342 android.RegisterSdkMemberType(&sdkLibrarySdkMemberType{
343 android.SdkMemberTypeBase{
344 PropertyName: "java_sdk_libs",
345 SupportsSdk: true,
346 },
347 })
Jiyong Parkc678ad32018-04-10 13:07:10 +0900348}
349
Paul Duffin43dc1cc2019-12-19 11:18:54 +0000350func RegisterSdkLibraryBuildComponents(ctx android.RegistrationContext) {
351 ctx.RegisterModuleType("java_sdk_library", SdkLibraryFactory)
352 ctx.RegisterModuleType("java_sdk_library_import", sdkLibraryImportFactory)
353}
354
Paul Duffin3375e352020-04-28 10:44:03 +0100355// Properties associated with each api scope.
356type ApiScopeProperties struct {
357 // Indicates whether the api surface is generated.
358 //
359 // If this is set for any scope then all scopes must explicitly specify if they
360 // are enabled. This is to prevent new usages from depending on legacy behavior.
361 //
362 // Otherwise, if this is not set for any scope then the default behavior is
363 // scope specific so please refer to the scope specific property documentation.
364 Enabled *bool
Paul Duffin87a05a32020-05-12 11:50:28 +0100365
366 // The sdk_version to use for building the stubs.
367 //
368 // If not specified then it will use an sdk_version determined as follows:
369 // 1) If the sdk_version specified on the java_sdk_library is none then this
370 // will be none. This is used for java_sdk_library instances that are used
371 // to create stubs that contribute to the core_current sdk version.
372 // 2) Otherwise, it is assumed that this library extends but does not contribute
373 // directly to a specific sdk_version and so this uses the sdk_version appropriate
374 // for the api scope. e.g. public will use sdk_version: current, system will use
375 // sdk_version: system_current, etc.
376 //
377 // This does not affect the sdk_version used for either generating the stubs source
378 // or the API file. They both have to use the same sdk_version as is used for
379 // compiling the implementation library.
380 Sdk_version *string
Paul Duffin3375e352020-04-28 10:44:03 +0100381}
382
Jiyong Parkc678ad32018-04-10 13:07:10 +0900383type sdkLibraryProperties struct {
Paul Duffin5df79302020-05-16 15:52:12 +0100384 // Visibility for impl library module. If not specified then defaults to the
385 // visibility property.
386 Impl_library_visibility []string
387
Paul Duffin4911a892020-04-29 23:35:13 +0100388 // Visibility for stubs library modules. If not specified then defaults to the
389 // visibility property.
390 Stubs_library_visibility []string
391
392 // Visibility for stubs source modules. If not specified then defaults to the
393 // visibility property.
394 Stubs_source_visibility []string
395
Anton Hansson7f66efa2020-10-08 14:47:23 +0100396 // List of Java libraries that will be in the classpath when building the implementation lib
397 Impl_only_libs []string `android:"arch_variant"`
398
Sundong Ahnf043cf62018-06-25 16:04:37 +0900399 // List of Java libraries that will be in the classpath when building stubs
400 Stub_only_libs []string `android:"arch_variant"`
401
Anton Hanssondae54cd2021-04-21 16:30:10 +0100402 // List of Java libraries that will included in stub libraries
403 Stub_only_static_libs []string `android:"arch_variant"`
404
Paul Duffin7a586d32019-12-30 17:09:34 +0000405 // list of package names that will be documented and publicized as API.
406 // This allows the API to be restricted to a subset of the source files provided.
407 // If this is unspecified then all the source files will be treated as being part
408 // of the API.
Jiyong Parkc678ad32018-04-10 13:07:10 +0900409 Api_packages []string
410
Jiyong Park5a2c9d72018-05-01 22:25:41 +0900411 // list of package names that must be hidden from the API
412 Hidden_api_packages []string
413
Paul Duffin749f98f2019-12-30 17:23:46 +0000414 // the relative path to the directory containing the api specification files.
415 // Defaults to "api".
416 Api_dir *string
417
Paul Duffindfa131e2020-05-15 20:37:11 +0100418 // Determines whether a runtime implementation library is built; defaults to false.
419 //
420 // If true then it also prevents the module from being used as a shared module, i.e.
421 // it is as is shared_library: false, was set.
Paul Duffin43db9be2019-12-30 17:35:49 +0000422 Api_only *bool
423
Paul Duffin11512472019-02-11 15:55:17 +0000424 // local files that are used within user customized droiddoc options.
425 Droiddoc_option_files []string
426
427 // additional droiddoc options
428 // Available variables for substitution:
429 //
430 // $(location <label>): the path to the droiddoc_option_files with name <label>
Sundong Ahndd567f92018-07-31 17:19:11 +0900431 Droiddoc_options []string
432
Paul Duffine22c2ab2020-05-20 19:35:27 +0100433 // is set to true, Metalava will allow framework SDK to contain annotations.
434 Annotations_enabled *bool
435
Sundong Ahn054b19a2018-10-19 13:46:09 +0900436 // a list of top-level directories containing files to merge qualifier annotations
437 // (i.e. those intended to be included in the stubs written) from.
438 Merge_annotations_dirs []string
439
440 // a list of top-level directories containing Java stub files to merge show/hide annotations from.
441 Merge_inclusion_annotations_dirs []string
442
443 // If set to true, the path of dist files is apistubs/core. Defaults to false.
444 Core_lib *bool
445
Paul Duffin4f5c1ef2020-11-19 14:53:43 +0000446 // If set to true then don't create dist rules.
447 No_dist *bool
Sundong Ahn80a87b32019-05-13 15:02:50 +0900448
Paul Duffin31310252020-11-20 21:26:20 +0000449 // The stem for the artifacts that are copied to the dist, if not specified
450 // then defaults to the base module name.
451 //
452 // For each scope the following artifacts are copied to the apistubs/<scope>
453 // directory in the dist.
454 // * stubs impl jar -> <dist-stem>.jar
455 // * API specification file -> api/<dist-stem>.txt
456 // * Removed API specification file -> api/<dist-stem>-removed.txt
457 //
458 // Also used to construct the name of the filegroup (created by prebuilt_apis)
459 // that references the latest released API and remove API specification files.
460 // * API specification filegroup -> <dist-stem>.api.<scope>.latest
461 // * Removed API specification filegroup -> <dist-stem>-removed.api.<scope>.latest
Jaewoong Jung1a97ee02021-03-09 13:25:02 -0800462 // * API incompatibilities baseline filegroup -> <dist-stem>-incompatibilities.api.<scope>.latest
Paul Duffin31310252020-11-20 21:26:20 +0000463 Dist_stem *string
464
Anton Hanssondff2c782020-12-21 17:10:01 +0000465 // A compatibility mode that allows historical API-tracking files to not exist.
466 // Do not use.
467 Unsafe_ignore_missing_latest_api bool
468
Paul Duffin3375e352020-04-28 10:44:03 +0100469 // indicates whether system and test apis should be generated.
470 Generate_system_and_test_apis bool `blueprint:"mutated"`
471
472 // The properties specific to the public api scope
473 //
474 // Unless explicitly specified by using public.enabled the public api scope is
475 // enabled by default in both legacy and non-legacy mode.
476 Public ApiScopeProperties
477
478 // The properties specific to the system api scope
479 //
480 // In legacy mode the system api scope is enabled by default when sdk_version
481 // is set to something other than "none".
482 //
483 // In non-legacy mode the system api scope is disabled by default.
484 System ApiScopeProperties
485
486 // The properties specific to the test api scope
487 //
488 // In legacy mode the test api scope is enabled by default when sdk_version
489 // is set to something other than "none".
490 //
491 // In non-legacy mode the test api scope is disabled by default.
492 Test ApiScopeProperties
Paul Duffin37e0b772019-12-30 17:20:10 +0000493
Paul Duffin0c5bae52020-06-02 13:00:08 +0100494 // The properties specific to the module-lib api scope
Paul Duffin8f265b92020-04-28 14:13:56 +0100495 //
Paul Duffin0c5bae52020-06-02 13:00:08 +0100496 // Unless explicitly specified by using test.enabled the module-lib api scope is
Paul Duffin8f265b92020-04-28 14:13:56 +0100497 // disabled by default.
498 Module_lib ApiScopeProperties
499
Paul Duffin0c5bae52020-06-02 13:00:08 +0100500 // The properties specific to the system-server api scope
501 //
502 // Unless explicitly specified by using test.enabled the module-lib api scope is
503 // disabled by default.
504 System_server ApiScopeProperties
505
Jiyong Park932cdfe2020-05-28 00:19:53 +0900506 // Determines if the stubs are preferred over the implementation library
507 // for linking, even when the client doesn't specify sdk_version. When this
508 // is set to true, such clients are provided with the widest API surface that
509 // this lib provides. Note however that this option doesn't affect the clients
510 // that are in the same APEX as this library. In that case, the clients are
511 // always linked with the implementation library. Default is false.
512 Default_to_stubs *bool
513
Paul Duffin160fe412020-05-10 19:32:20 +0100514 // Properties related to api linting.
515 Api_lint struct {
516 // Enable api linting.
517 Enabled *bool
518 }
519
Jiyong Parkc678ad32018-04-10 13:07:10 +0900520 // TODO: determines whether to create HTML doc or not
521 //Html_doc *bool
522}
523
Paul Duffin0f8faff2020-05-20 16:18:00 +0100524// Paths to outputs from java_sdk_library and java_sdk_library_import.
525//
526// Fields that are android.Paths are always set (during GenerateAndroidBuildActions).
527// OptionalPaths are always set by java_sdk_library but may not be set by
528// java_sdk_library_import as not all instances provide that information.
Paul Duffind1b3a922020-01-22 11:57:20 +0000529type scopePaths struct {
Paul Duffin0f8faff2020-05-20 16:18:00 +0100530 // The path (represented as Paths for convenience when returning) to the stubs header jar.
531 //
532 // That is the jar that is created by turbine.
533 stubsHeaderPath android.Paths
534
535 // The path (represented as Paths for convenience when returning) to the stubs implementation jar.
536 //
537 // This is not the implementation jar, it still only contains stubs.
538 stubsImplPath android.Paths
539
Paul Duffin1267d872021-04-16 17:21:36 +0100540 // The dex jar for the stubs.
541 //
542 // This is not the implementation jar, it still only contains stubs.
543 stubsDexJarPath android.Path
544
Paul Duffin0f8faff2020-05-20 16:18:00 +0100545 // The API specification file, e.g. system_current.txt.
546 currentApiFilePath android.OptionalPath
547
548 // The specification of API elements removed since the last release.
549 removedApiFilePath android.OptionalPath
550
551 // The stubs source jar.
552 stubsSrcJar android.OptionalPath
Paul Duffind1b3a922020-01-22 11:57:20 +0000553}
554
Colin Crossdcf71b22021-02-01 13:59:03 -0800555func (paths *scopePaths) extractStubsLibraryInfoFromDependency(ctx android.ModuleContext, dep android.Module) error {
556 if ctx.OtherModuleHasProvider(dep, JavaInfoProvider) {
557 lib := ctx.OtherModuleProvider(dep, JavaInfoProvider).(JavaInfo)
558 paths.stubsHeaderPath = lib.HeaderJars
559 paths.stubsImplPath = lib.ImplementationJars
Paul Duffin1267d872021-04-16 17:21:36 +0100560
561 libDep := dep.(UsesLibraryDependency)
562 paths.stubsDexJarPath = libDep.DexJarBuildPath()
Paul Duffinc8782502020-04-29 20:45:27 +0100563 return nil
564 } else {
Colin Crossdcf71b22021-02-01 13:59:03 -0800565 return fmt.Errorf("expected module that has JavaInfoProvider, e.g. java_library")
Paul Duffinc8782502020-04-29 20:45:27 +0100566 }
567}
568
Paul Duffin0ff08bd2020-04-29 13:30:54 +0100569func (paths *scopePaths) treatDepAsApiStubsProvider(dep android.Module, action func(provider ApiStubsProvider)) error {
570 if apiStubsProvider, ok := dep.(ApiStubsProvider); ok {
571 action(apiStubsProvider)
Paul Duffinc8782502020-04-29 20:45:27 +0100572 return nil
573 } else {
574 return fmt.Errorf("expected module that implements ApiStubsProvider, e.g. droidstubs")
575 }
576}
577
Paul Duffin0f8faff2020-05-20 16:18:00 +0100578func (paths *scopePaths) treatDepAsApiStubsSrcProvider(dep android.Module, action func(provider ApiStubsSrcProvider)) error {
579 if apiStubsProvider, ok := dep.(ApiStubsSrcProvider); ok {
580 action(apiStubsProvider)
581 return nil
582 } else {
583 return fmt.Errorf("expected module that implements ApiStubsSrcProvider, e.g. droidstubs")
584 }
585}
586
Paul Duffin0ff08bd2020-04-29 13:30:54 +0100587func (paths *scopePaths) extractApiInfoFromApiStubsProvider(provider ApiStubsProvider) {
Paul Duffin0f8faff2020-05-20 16:18:00 +0100588 paths.currentApiFilePath = android.OptionalPathForPath(provider.ApiFilePath())
589 paths.removedApiFilePath = android.OptionalPathForPath(provider.RemovedApiFilePath())
Paul Duffin0ff08bd2020-04-29 13:30:54 +0100590}
591
Colin Crossdcf71b22021-02-01 13:59:03 -0800592func (paths *scopePaths) extractApiInfoFromDep(ctx android.ModuleContext, dep android.Module) error {
Paul Duffin0ff08bd2020-04-29 13:30:54 +0100593 return paths.treatDepAsApiStubsProvider(dep, func(provider ApiStubsProvider) {
594 paths.extractApiInfoFromApiStubsProvider(provider)
595 })
596}
597
Paul Duffin0f8faff2020-05-20 16:18:00 +0100598func (paths *scopePaths) extractStubsSourceInfoFromApiStubsProviders(provider ApiStubsSrcProvider) {
599 paths.stubsSrcJar = android.OptionalPathForPath(provider.StubsSrcJar())
Paul Duffin0ff08bd2020-04-29 13:30:54 +0100600}
601
Colin Crossdcf71b22021-02-01 13:59:03 -0800602func (paths *scopePaths) extractStubsSourceInfoFromDep(ctx android.ModuleContext, dep android.Module) error {
Paul Duffin0f8faff2020-05-20 16:18:00 +0100603 return paths.treatDepAsApiStubsSrcProvider(dep, func(provider ApiStubsSrcProvider) {
Paul Duffin0ff08bd2020-04-29 13:30:54 +0100604 paths.extractStubsSourceInfoFromApiStubsProviders(provider)
605 })
606}
607
Colin Crossdcf71b22021-02-01 13:59:03 -0800608func (paths *scopePaths) extractStubsSourceAndApiInfoFromApiStubsProvider(ctx android.ModuleContext, dep android.Module) error {
Paul Duffin0ff08bd2020-04-29 13:30:54 +0100609 return paths.treatDepAsApiStubsProvider(dep, func(provider ApiStubsProvider) {
610 paths.extractApiInfoFromApiStubsProvider(provider)
611 paths.extractStubsSourceInfoFromApiStubsProviders(provider)
612 })
613}
614
615type commonToSdkLibraryAndImportProperties struct {
Paul Duffin1b1e8062020-05-08 13:44:43 +0100616 // The naming scheme to use for the components that this module creates.
617 //
Paul Duffinee9ad5d2020-09-11 13:04:05 +0100618 // If not specified then it defaults to "default".
Paul Duffin1b1e8062020-05-08 13:44:43 +0100619 //
620 // This is a temporary mechanism to simplify conversion from separate modules for each
621 // component that follow a different naming pattern to the default one.
622 //
623 // TODO(b/155480189) - Remove once naming inconsistencies have been resolved.
Paul Duffin0ff08bd2020-04-29 13:30:54 +0100624 Naming_scheme *string
Paul Duffindfa131e2020-05-15 20:37:11 +0100625
626 // Specifies whether this module can be used as an Android shared library; defaults
627 // to true.
628 //
629 // An Android shared library is one that can be referenced in a <uses-library> element
630 // in an AndroidManifest.xml.
631 Shared_library *bool
Paul Duffina2ae7e02020-09-11 11:55:00 +0100632
633 // Files containing information about supported java doc tags.
634 Doctag_files []string `android:"path"`
Paul Duffin0ff08bd2020-04-29 13:30:54 +0100635}
636
Paul Duffin56d44902020-01-31 13:36:25 +0000637// Common code between sdk library and sdk library import
638type commonToSdkLibraryAndImport struct {
Paul Duffinc3091c82020-05-08 14:16:20 +0100639 moduleBase *android.ModuleBase
640
Paul Duffin56d44902020-01-31 13:36:25 +0000641 scopePaths map[*apiScope]*scopePaths
Paul Duffin1b1e8062020-05-08 13:44:43 +0100642
643 namingScheme sdkLibraryComponentNamingScheme
644
Paul Duffindfa131e2020-05-15 20:37:11 +0100645 commonSdkLibraryProperties commonToSdkLibraryAndImportProperties
Paul Duffin859fe962020-05-15 10:20:31 +0100646
Paul Duffina2ae7e02020-09-11 11:55:00 +0100647 // Paths to commonSdkLibraryProperties.Doctag_files
648 doctagPaths android.Paths
649
Paul Duffin859fe962020-05-15 10:20:31 +0100650 // Functionality related to this being used as a component of a java_sdk_library.
651 EmbeddableSdkLibraryComponent
Paul Duffin56d44902020-01-31 13:36:25 +0000652}
653
Paul Duffinc3091c82020-05-08 14:16:20 +0100654func (c *commonToSdkLibraryAndImport) initCommon(moduleBase *android.ModuleBase) {
655 c.moduleBase = moduleBase
Paul Duffin1b1e8062020-05-08 13:44:43 +0100656
Paul Duffindfa131e2020-05-15 20:37:11 +0100657 moduleBase.AddProperties(&c.commonSdkLibraryProperties)
Paul Duffin859fe962020-05-15 10:20:31 +0100658
659 // Initialize this as an sdk library component.
660 c.initSdkLibraryComponent(moduleBase)
Paul Duffin1b1e8062020-05-08 13:44:43 +0100661}
662
663func (c *commonToSdkLibraryAndImport) initCommonAfterDefaultsApplied(ctx android.DefaultableHookContext) bool {
Paul Duffindfa131e2020-05-15 20:37:11 +0100664 schemeProperty := proptools.StringDefault(c.commonSdkLibraryProperties.Naming_scheme, "default")
Paul Duffin1b1e8062020-05-08 13:44:43 +0100665 switch schemeProperty {
666 case "default":
667 c.namingScheme = &defaultNamingScheme{}
668 default:
669 ctx.PropertyErrorf("naming_scheme", "expected 'default' but was %q", schemeProperty)
670 return false
671 }
672
Paul Duffindfa131e2020-05-15 20:37:11 +0100673 // Only track this sdk library if this can be used as a shared library.
674 if c.sharedLibrary() {
675 // Use the name specified in the module definition as the owner.
676 c.sdkLibraryComponentProperties.SdkLibraryToImplicitlyTrack = proptools.StringPtr(c.moduleBase.BaseModuleName())
677 }
Paul Duffin859fe962020-05-15 10:20:31 +0100678
Paul Duffin1b1e8062020-05-08 13:44:43 +0100679 return true
Paul Duffinc3091c82020-05-08 14:16:20 +0100680}
681
Paul Duffina2ae7e02020-09-11 11:55:00 +0100682func (c *commonToSdkLibraryAndImport) generateCommonBuildActions(ctx android.ModuleContext) {
683 c.doctagPaths = android.PathsForModuleSrc(ctx, c.commonSdkLibraryProperties.Doctag_files)
684}
685
Paul Duffineedc5d52020-06-12 17:46:39 +0100686// Module name of the runtime implementation library
687func (c *commonToSdkLibraryAndImport) implLibraryModuleName() string {
688 return c.moduleBase.BaseModuleName() + ".impl"
689}
690
691// Module name of the XML file for the lib
692func (c *commonToSdkLibraryAndImport) xmlPermissionsModuleName() string {
693 return c.moduleBase.BaseModuleName() + sdkXmlFileSuffix
694}
695
Paul Duffinc3091c82020-05-08 14:16:20 +0100696// Name of the java_library module that compiles the stubs source.
697func (c *commonToSdkLibraryAndImport) stubsLibraryModuleName(apiScope *apiScope) string {
Paul Duffin1b1e8062020-05-08 13:44:43 +0100698 return c.namingScheme.stubsLibraryModuleName(apiScope, c.moduleBase.BaseModuleName())
Paul Duffinc3091c82020-05-08 14:16:20 +0100699}
700
701// Name of the droidstubs module that generates the stubs source and may also
702// generate/check the API.
703func (c *commonToSdkLibraryAndImport) stubsSourceModuleName(apiScope *apiScope) string {
Paul Duffin1b1e8062020-05-08 13:44:43 +0100704 return c.namingScheme.stubsSourceModuleName(apiScope, c.moduleBase.BaseModuleName())
Paul Duffinc3091c82020-05-08 14:16:20 +0100705}
706
707// Name of the droidstubs module that generates/checks the API. Only used if it
708// requires different arts to the stubs source generating module.
709func (c *commonToSdkLibraryAndImport) apiModuleName(apiScope *apiScope) string {
Paul Duffin1b1e8062020-05-08 13:44:43 +0100710 return c.namingScheme.apiModuleName(apiScope, c.moduleBase.BaseModuleName())
Paul Duffinc3091c82020-05-08 14:16:20 +0100711}
712
Paul Duffin46dc45a2020-05-14 15:39:10 +0100713// The component names for different outputs of the java_sdk_library.
714//
715// They are similar to the names used for the child modules it creates
716const (
717 stubsSourceComponentName = "stubs.source"
718
719 apiTxtComponentName = "api.txt"
720
721 removedApiTxtComponentName = "removed-api.txt"
722)
723
724// A regular expression to match tags that reference a specific stubs component.
725//
726// It will only match if given a valid scope and a valid component. It is verfy strict
727// to ensure it does not accidentally match a similar looking tag that should be processed
728// by the embedded Library.
729var tagSplitter = func() *regexp.Regexp {
730 // Given a list of literal string items returns a regular expression that will
731 // match any one of the items.
732 choice := func(items ...string) string {
733 return `\Q` + strings.Join(items, `\E|\Q`) + `\E`
734 }
735
736 // Regular expression to match one of the scopes.
737 scopesRegexp := choice(allScopeNames...)
738
739 // Regular expression to match one of the components.
740 componentsRegexp := choice(stubsSourceComponentName, apiTxtComponentName, removedApiTxtComponentName)
741
742 // Regular expression to match any combination of one scope and one component.
743 return regexp.MustCompile(fmt.Sprintf(`^\.(%s)\.(%s)$`, scopesRegexp, componentsRegexp))
744}()
745
746// For OutputFileProducer interface
747//
748// .<scope>.stubs.source
749// .<scope>.api.txt
750// .<scope>.removed-api.txt
751func (c *commonToSdkLibraryAndImport) commonOutputFiles(tag string) (android.Paths, error) {
752 if groups := tagSplitter.FindStringSubmatch(tag); groups != nil {
753 scopeName := groups[1]
754 component := groups[2]
755
756 if scope, ok := scopeByName[scopeName]; ok {
757 paths := c.findScopePaths(scope)
758 if paths == nil {
759 return nil, fmt.Errorf("%q does not provide api scope %s", c.moduleBase.BaseModuleName(), scopeName)
760 }
761
762 switch component {
763 case stubsSourceComponentName:
764 if paths.stubsSrcJar.Valid() {
765 return android.Paths{paths.stubsSrcJar.Path()}, nil
766 }
767
768 case apiTxtComponentName:
769 if paths.currentApiFilePath.Valid() {
770 return android.Paths{paths.currentApiFilePath.Path()}, nil
771 }
772
773 case removedApiTxtComponentName:
774 if paths.removedApiFilePath.Valid() {
775 return android.Paths{paths.removedApiFilePath.Path()}, nil
776 }
777 }
778
779 return nil, fmt.Errorf("%s not available for api scope %s", component, scopeName)
780 } else {
781 return nil, fmt.Errorf("unknown scope %s in %s", scope, tag)
782 }
783
784 } else {
Paul Duffina2ae7e02020-09-11 11:55:00 +0100785 switch tag {
786 case ".doctags":
787 if c.doctagPaths != nil {
788 return c.doctagPaths, nil
789 } else {
790 return nil, fmt.Errorf("no doctag_files specified on %s", c.moduleBase.BaseModuleName())
791 }
792 }
Paul Duffin46dc45a2020-05-14 15:39:10 +0100793 return nil, nil
794 }
795}
796
Paul Duffin803a9562020-05-20 11:52:25 +0100797func (c *commonToSdkLibraryAndImport) getScopePathsCreateIfNeeded(scope *apiScope) *scopePaths {
Paul Duffin56d44902020-01-31 13:36:25 +0000798 if c.scopePaths == nil {
799 c.scopePaths = make(map[*apiScope]*scopePaths)
800 }
801 paths := c.scopePaths[scope]
802 if paths == nil {
803 paths = &scopePaths{}
804 c.scopePaths[scope] = paths
805 }
806
807 return paths
808}
809
Paul Duffin803a9562020-05-20 11:52:25 +0100810func (c *commonToSdkLibraryAndImport) findScopePaths(scope *apiScope) *scopePaths {
811 if c.scopePaths == nil {
812 return nil
813 }
814
815 return c.scopePaths[scope]
816}
817
818// If this does not support the requested api scope then find the closest available
819// scope it does support. Returns nil if no such scope is available.
820func (c *commonToSdkLibraryAndImport) findClosestScopePath(scope *apiScope) *scopePaths {
821 for s := scope; s != nil; s = s.extends {
822 if paths := c.findScopePaths(s); paths != nil {
823 return paths
824 }
825 }
826
827 // This should never happen outside tests as public should be the base scope for every
828 // scope and is enabled by default.
829 return nil
830}
831
Jiyong Parkf1691d22021-03-29 20:11:58 +0900832func (c *commonToSdkLibraryAndImport) selectHeaderJarsForSdkVersion(ctx android.BaseModuleContext, sdkVersion android.SdkSpec) android.Paths {
Paul Duffinb05d4292020-05-20 12:19:10 +0100833
834 // If a specific numeric version has been requested then use prebuilt versions of the sdk.
Jiyong Park54105c42021-03-31 18:17:53 +0900835 if !sdkVersion.ApiLevel.IsPreview() {
Paul Duffinb05d4292020-05-20 12:19:10 +0100836 return PrebuiltJars(ctx, c.moduleBase.BaseModuleName(), sdkVersion)
837 }
838
Paul Duffin1267d872021-04-16 17:21:36 +0100839 paths := c.selectScopePaths(ctx, sdkVersion.Kind)
840 if paths == nil {
841 return nil
842 }
843
844 return paths.stubsHeaderPath
845}
846
847// selectScopePaths returns the *scopePaths appropriate for the specific kind.
848//
849// If the module does not support the specific kind then it will return the *scopePaths for the
850// closest kind which is a subset of the requested kind. e.g. if requesting android.SdkModule then
851// it will return *scopePaths for android.SdkSystem if available or android.SdkPublic of not.
852func (c *commonToSdkLibraryAndImport) selectScopePaths(ctx android.BaseModuleContext, kind android.SdkKind) *scopePaths {
Paul Duffinb05d4292020-05-20 12:19:10 +0100853 var apiScope *apiScope
Paul Duffin1267d872021-04-16 17:21:36 +0100854 switch kind {
Jiyong Parkf1691d22021-03-29 20:11:58 +0900855 case android.SdkSystem:
Paul Duffinb05d4292020-05-20 12:19:10 +0100856 apiScope = apiScopeSystem
Jiyong Parkf1691d22021-03-29 20:11:58 +0900857 case android.SdkModule:
Paul Duffin803a9562020-05-20 11:52:25 +0100858 apiScope = apiScopeModuleLib
Jiyong Parkf1691d22021-03-29 20:11:58 +0900859 case android.SdkTest:
Paul Duffinb05d4292020-05-20 12:19:10 +0100860 apiScope = apiScopeTest
Jiyong Parkf1691d22021-03-29 20:11:58 +0900861 case android.SdkSystemServer:
Paul Duffin0c5bae52020-06-02 13:00:08 +0100862 apiScope = apiScopeSystemServer
Paul Duffinb05d4292020-05-20 12:19:10 +0100863 default:
864 apiScope = apiScopePublic
865 }
866
Paul Duffin803a9562020-05-20 11:52:25 +0100867 paths := c.findClosestScopePath(apiScope)
868 if paths == nil {
869 var scopes []string
870 for _, s := range allApiScopes {
871 if c.findScopePaths(s) != nil {
872 scopes = append(scopes, s.name)
873 }
874 }
875 ctx.ModuleErrorf("requires api scope %s from %s but it only has %q available", apiScope.name, c.moduleBase.BaseModuleName(), scopes)
876 return nil
877 }
878
Paul Duffin1267d872021-04-16 17:21:36 +0100879 return paths
880}
881
882// to satisfy SdkLibraryDependency interface
883func (c *commonToSdkLibraryAndImport) SdkApiStubDexJar(ctx android.BaseModuleContext, kind android.SdkKind) android.Path {
884 paths := c.selectScopePaths(ctx, kind)
885 if paths == nil {
886 return nil
887 }
888
889 return paths.stubsDexJarPath
Paul Duffinb05d4292020-05-20 12:19:10 +0100890}
891
Paul Duffin859fe962020-05-15 10:20:31 +0100892func (c *commonToSdkLibraryAndImport) sdkComponentPropertiesForChildLibrary() interface{} {
893 componentProps := &struct {
894 SdkLibraryToImplicitlyTrack *string
Paul Duffindfa131e2020-05-15 20:37:11 +0100895 }{}
896
897 if c.sharedLibrary() {
Paul Duffin859fe962020-05-15 10:20:31 +0100898 // Mark the stubs library as being components of this java_sdk_library so that
899 // any app that includes code which depends (directly or indirectly) on the stubs
900 // library will have the appropriate <uses-library> invocation inserted into its
901 // manifest if necessary.
Paul Duffindfa131e2020-05-15 20:37:11 +0100902 componentProps.SdkLibraryToImplicitlyTrack = proptools.StringPtr(c.moduleBase.BaseModuleName())
Paul Duffin859fe962020-05-15 10:20:31 +0100903 }
904
905 return componentProps
906}
907
Paul Duffindfa131e2020-05-15 20:37:11 +0100908// Check if this can be used as a shared library.
909func (c *commonToSdkLibraryAndImport) sharedLibrary() bool {
910 return proptools.BoolDefault(c.commonSdkLibraryProperties.Shared_library, true)
911}
912
Paul Duffin859fe962020-05-15 10:20:31 +0100913// Properties related to the use of a module as an component of a java_sdk_library.
914type SdkLibraryComponentProperties struct {
915
916 // The name of the java_sdk_library/_import to add to a <uses-library> entry
917 // in the AndroidManifest.xml of any Android app that includes code that references
918 // this module. If not set then no java_sdk_library/_import is tracked.
919 SdkLibraryToImplicitlyTrack *string `blueprint:"mutated"`
920}
921
922// Structure to be embedded in a module struct that needs to support the
923// SdkLibraryComponentDependency interface.
924type EmbeddableSdkLibraryComponent struct {
925 sdkLibraryComponentProperties SdkLibraryComponentProperties
926}
927
928func (e *EmbeddableSdkLibraryComponent) initSdkLibraryComponent(moduleBase *android.ModuleBase) {
929 moduleBase.AddProperties(&e.sdkLibraryComponentProperties)
930}
931
932// to satisfy SdkLibraryComponentDependency
Ulya Trafimovich31e444e2020-08-14 17:32:16 +0100933func (e *EmbeddableSdkLibraryComponent) OptionalImplicitSdkLibrary() *string {
934 return e.sdkLibraryComponentProperties.SdkLibraryToImplicitlyTrack
Paul Duffin859fe962020-05-15 10:20:31 +0100935}
936
Ulya Trafimovich39b437b2020-09-23 16:42:35 +0100937// to satisfy SdkLibraryComponentDependency
938func (e *EmbeddableSdkLibraryComponent) OptionalSdkLibraryImplementation() *string {
939 // Currently implementation library name is the same as the SDK library name.
940 return e.sdkLibraryComponentProperties.SdkLibraryToImplicitlyTrack
941}
942
Paul Duffin859fe962020-05-15 10:20:31 +0100943// Implemented by modules that are (or possibly could be) a component of a java_sdk_library
944// (including the java_sdk_library) itself.
945type SdkLibraryComponentDependency interface {
Ulya Trafimovich31e444e2020-08-14 17:32:16 +0100946 UsesLibraryDependency
947
Paul Duffin859fe962020-05-15 10:20:31 +0100948 // The optional name of the sdk library that should be implicitly added to the
949 // AndroidManifest of an app that contains code which references the sdk library.
950 //
Ulya Trafimovich31e444e2020-08-14 17:32:16 +0100951 // Returns the name of the optional implicit SDK library or nil, if there isn't one.
952 OptionalImplicitSdkLibrary() *string
Ulya Trafimovich39b437b2020-09-23 16:42:35 +0100953
954 // The name of the implementation library for the optional SDK library or nil, if there isn't one.
955 OptionalSdkLibraryImplementation() *string
Paul Duffin859fe962020-05-15 10:20:31 +0100956}
957
958// Make sure that all the module types that are components of java_sdk_library/_import
959// and which can be referenced (directly or indirectly) from an android app implement
960// the SdkLibraryComponentDependency interface.
961var _ SdkLibraryComponentDependency = (*Library)(nil)
962var _ SdkLibraryComponentDependency = (*Import)(nil)
963var _ SdkLibraryComponentDependency = (*SdkLibrary)(nil)
Paul Duffineedc5d52020-06-12 17:46:39 +0100964var _ SdkLibraryComponentDependency = (*SdkLibraryImport)(nil)
Paul Duffin859fe962020-05-15 10:20:31 +0100965
966// Provides access to sdk_version related header and implentation jars.
967type SdkLibraryDependency interface {
968 SdkLibraryComponentDependency
969
970 // Get the header jars appropriate for the supplied sdk_version.
971 //
972 // These are turbine generated jars so they only change if the externals of the
973 // class changes but it does not contain and implementation or JavaDoc.
Jiyong Parkf1691d22021-03-29 20:11:58 +0900974 SdkHeaderJars(ctx android.BaseModuleContext, sdkVersion android.SdkSpec) android.Paths
Paul Duffin859fe962020-05-15 10:20:31 +0100975
976 // Get the implementation jars appropriate for the supplied sdk version.
977 //
978 // These are either the implementation jar for the whole sdk library or the implementation
979 // jars for the stubs. The latter should only be needed when generating JavaDoc as otherwise
980 // they are identical to the corresponding header jars.
Jiyong Parkf1691d22021-03-29 20:11:58 +0900981 SdkImplementationJars(ctx android.BaseModuleContext, sdkVersion android.SdkSpec) android.Paths
Paul Duffin1267d872021-04-16 17:21:36 +0100982
983 // SdkApiStubDexJar returns the dex jar for the stubs. It is needed by the hiddenapi processing
984 // tool which processes dex files.
985 SdkApiStubDexJar(ctx android.BaseModuleContext, kind android.SdkKind) android.Path
Paul Duffin859fe962020-05-15 10:20:31 +0100986}
987
Inseob Kimc0907f12019-02-08 21:00:45 +0900988type SdkLibrary struct {
Sundong Ahn054b19a2018-10-19 13:46:09 +0900989 Library
Jiyong Parkc678ad32018-04-10 13:07:10 +0900990
Sundong Ahn054b19a2018-10-19 13:46:09 +0900991 sdkLibraryProperties sdkLibraryProperties
Jiyong Parkc678ad32018-04-10 13:07:10 +0900992
Paul Duffin3375e352020-04-28 10:44:03 +0100993 // Map from api scope to the scope specific property structure.
994 scopeToProperties map[*apiScope]*ApiScopeProperties
995
Paul Duffin56d44902020-01-31 13:36:25 +0000996 commonToSdkLibraryAndImport
Jiyong Parkc678ad32018-04-10 13:07:10 +0900997}
998
Inseob Kimc0907f12019-02-08 21:00:45 +0900999var _ SdkLibraryDependency = (*SdkLibrary)(nil)
Colin Cross897d2ed2019-02-11 14:03:51 -08001000
Paul Duffin3375e352020-04-28 10:44:03 +01001001func (module *SdkLibrary) generateTestAndSystemScopesByDefault() bool {
1002 return module.sdkLibraryProperties.Generate_system_and_test_apis
1003}
1004
1005func (module *SdkLibrary) getGeneratedApiScopes(ctx android.EarlyModuleContext) apiScopes {
1006 // Check to see if any scopes have been explicitly enabled. If any have then all
1007 // must be.
1008 anyScopesExplicitlyEnabled := false
1009 for _, scope := range allApiScopes {
1010 scopeProperties := module.scopeToProperties[scope]
1011 if scopeProperties.Enabled != nil {
1012 anyScopesExplicitlyEnabled = true
1013 break
1014 }
Paul Duffind1b3a922020-01-22 11:57:20 +00001015 }
Paul Duffin3375e352020-04-28 10:44:03 +01001016
1017 var generatedScopes apiScopes
1018 enabledScopes := make(map[*apiScope]struct{})
1019 for _, scope := range allApiScopes {
1020 scopeProperties := module.scopeToProperties[scope]
1021 // If any scopes are explicitly enabled then ignore the legacy enabled status.
1022 // This is to ensure that any new usages of this module type do not rely on legacy
1023 // behaviour.
1024 defaultEnabledStatus := false
1025 if anyScopesExplicitlyEnabled {
1026 defaultEnabledStatus = scope.defaultEnabledStatus
1027 } else {
1028 defaultEnabledStatus = scope.legacyEnabledStatus(module)
1029 }
1030 enabled := proptools.BoolDefault(scopeProperties.Enabled, defaultEnabledStatus)
1031 if enabled {
1032 enabledScopes[scope] = struct{}{}
1033 generatedScopes = append(generatedScopes, scope)
1034 }
1035 }
1036
1037 // Now check to make sure that any scope that is extended by an enabled scope is also
1038 // enabled.
1039 for _, scope := range allApiScopes {
1040 if _, ok := enabledScopes[scope]; ok {
1041 extends := scope.extends
1042 if extends != nil {
1043 if _, ok := enabledScopes[extends]; !ok {
1044 ctx.ModuleErrorf("enabled api scope %q depends on disabled scope %q", scope, extends)
1045 }
1046 }
1047 }
1048 }
1049
1050 return generatedScopes
Paul Duffind1b3a922020-01-22 11:57:20 +00001051}
1052
Paul Duffineedc5d52020-06-12 17:46:39 +01001053type sdkLibraryComponentTag struct {
1054 blueprint.BaseDependencyTag
1055 name string
1056}
1057
1058// Mark this tag so dependencies that use it are excluded from visibility enforcement.
1059func (t sdkLibraryComponentTag) ExcludeFromVisibilityEnforcement() {}
1060
1061var xmlPermissionsFileTag = sdkLibraryComponentTag{name: "xml-permissions-file"}
Paul Duffine74ac732020-02-06 13:51:46 +00001062
Jiyong Parke3833882020-02-17 17:28:10 +09001063func IsXmlPermissionsFileDepTag(depTag blueprint.DependencyTag) bool {
Paul Duffineedc5d52020-06-12 17:46:39 +01001064 if dt, ok := depTag.(sdkLibraryComponentTag); ok {
Jiyong Parke3833882020-02-17 17:28:10 +09001065 return dt == xmlPermissionsFileTag
1066 }
1067 return false
1068}
1069
Paul Duffineedc5d52020-06-12 17:46:39 +01001070var implLibraryTag = sdkLibraryComponentTag{name: "impl-library"}
Paul Duffin5df79302020-05-16 15:52:12 +01001071
Paul Duffin44f1d842020-06-26 20:17:02 +01001072// Add the dependencies on the child modules in the component deps mutator.
1073func (module *SdkLibrary) ComponentDepsMutator(ctx android.BottomUpMutatorContext) {
Paul Duffin3375e352020-04-28 10:44:03 +01001074 for _, apiScope := range module.getGeneratedApiScopes(ctx) {
Paul Duffind1b3a922020-01-22 11:57:20 +00001075 // Add dependencies to the stubs library
Paul Duffinc3091c82020-05-08 14:16:20 +01001076 ctx.AddVariationDependencies(nil, apiScope.stubsTag, module.stubsLibraryModuleName(apiScope))
Paul Duffind1b3a922020-01-22 11:57:20 +00001077
Paul Duffin15f34ef2020-07-20 18:04:44 +01001078 // Add a dependency on the stubs source in order to access both stubs source and api information.
1079 ctx.AddVariationDependencies(nil, apiScope.stubsSourceAndApiTag, module.stubsSourceModuleName(apiScope))
Sundong Ahn054b19a2018-10-19 13:46:09 +09001080 }
1081
Paul Duffindfa131e2020-05-15 20:37:11 +01001082 if module.requiresRuntimeImplementationLibrary() {
Paul Duffin5df79302020-05-16 15:52:12 +01001083 // Add dependency to the rule for generating the implementation library.
1084 ctx.AddDependency(module, implLibraryTag, module.implLibraryModuleName())
1085
Paul Duffindfa131e2020-05-15 20:37:11 +01001086 if module.sharedLibrary() {
1087 // Add dependency to the rule for generating the xml permissions file
Paul Duffineedc5d52020-06-12 17:46:39 +01001088 ctx.AddDependency(module, xmlPermissionsFileTag, module.xmlPermissionsModuleName())
Paul Duffindfa131e2020-05-15 20:37:11 +01001089 }
Paul Duffin44f1d842020-06-26 20:17:02 +01001090 }
1091}
Paul Duffine74ac732020-02-06 13:51:46 +00001092
Paul Duffin44f1d842020-06-26 20:17:02 +01001093// Add other dependencies as normal.
1094func (module *SdkLibrary) DepsMutator(ctx android.BottomUpMutatorContext) {
Anton Hanssone77fccc2021-01-20 16:52:41 +00001095 var missingApiModules []string
1096 for _, apiScope := range module.getGeneratedApiScopes(ctx) {
1097 if apiScope.unstable {
1098 continue
1099 }
1100 if m := android.SrcIsModule(module.latestApiFilegroupName(apiScope)); !ctx.OtherModuleExists(m) {
1101 missingApiModules = append(missingApiModules, m)
1102 }
1103 if m := android.SrcIsModule(module.latestRemovedApiFilegroupName(apiScope)); !ctx.OtherModuleExists(m) {
1104 missingApiModules = append(missingApiModules, m)
1105 }
Jaewoong Jung1a97ee02021-03-09 13:25:02 -08001106 if m := android.SrcIsModule(module.latestIncompatibilitiesFilegroupName(apiScope)); !ctx.OtherModuleExists(m) {
1107 missingApiModules = append(missingApiModules, m)
1108 }
Anton Hanssone77fccc2021-01-20 16:52:41 +00001109 }
1110 if len(missingApiModules) != 0 && !module.sdkLibraryProperties.Unsafe_ignore_missing_latest_api {
1111 m := module.Name() + " is missing tracking files for previously released library versions.\n"
1112 m += "You need to do one of the following:\n"
1113 m += "- Add `unsafe_ignore_missing_latest_api: true` to your blueprint (to disable compat tracking)\n"
1114 m += "- Add a set of prebuilt txt files representing the last released version of this library for compat checking.\n"
1115 m += " (the current set of API files can be used as a seed for this compatibility tracking\n"
1116 m += "\n"
1117 m += "The following filegroup modules are missing:\n "
1118 m += strings.Join(missingApiModules, "\n ") + "\n"
1119 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."
1120 ctx.ModuleErrorf(m)
1121 }
Paul Duffin44f1d842020-06-26 20:17:02 +01001122 if module.requiresRuntimeImplementationLibrary() {
Paul Duffindfa131e2020-05-15 20:37:11 +01001123 // Only add the deps for the library if it is actually going to be built.
1124 module.Library.deps(ctx)
1125 }
Jiyong Parkc678ad32018-04-10 13:07:10 +09001126}
1127
Paul Duffin46dc45a2020-05-14 15:39:10 +01001128func (module *SdkLibrary) OutputFiles(tag string) (android.Paths, error) {
1129 paths, err := module.commonOutputFiles(tag)
1130 if paths == nil && err == nil {
1131 return module.Library.OutputFiles(tag)
1132 } else {
1133 return paths, err
1134 }
1135}
1136
Inseob Kimc0907f12019-02-08 21:00:45 +09001137func (module *SdkLibrary) GenerateAndroidBuildActions(ctx android.ModuleContext) {
Paul Duffina2ae7e02020-09-11 11:55:00 +01001138 module.generateCommonBuildActions(ctx)
1139
Paul Duffindfa131e2020-05-15 20:37:11 +01001140 // Only build an implementation library if required.
1141 if module.requiresRuntimeImplementationLibrary() {
Paul Duffin43db9be2019-12-30 17:35:49 +00001142 module.Library.GenerateAndroidBuildActions(ctx)
1143 }
Sundong Ahn054b19a2018-10-19 13:46:09 +09001144
Sundong Ahn57368eb2018-07-06 11:20:23 +09001145 // Record the paths to the header jars of the library (stubs and impl).
Paul Duffind1b3a922020-01-22 11:57:20 +00001146 // When this java_sdk_library is depended upon from others via "libs" property,
Jiyong Parkc678ad32018-04-10 13:07:10 +09001147 // the recorded paths will be returned depending on the link type of the caller.
1148 ctx.VisitDirectDeps(func(to android.Module) {
Jiyong Parkc678ad32018-04-10 13:07:10 +09001149 tag := ctx.OtherModuleDependencyTag(to)
1150
Paul Duffinc8782502020-04-29 20:45:27 +01001151 // Extract information from any of the scope specific dependencies.
1152 if scopeTag, ok := tag.(scopeDependencyTag); ok {
1153 apiScope := scopeTag.apiScope
Paul Duffin803a9562020-05-20 11:52:25 +01001154 scopePaths := module.getScopePathsCreateIfNeeded(apiScope)
Paul Duffinc8782502020-04-29 20:45:27 +01001155
1156 // Extract information from the dependency. The exact information extracted
1157 // is determined by the nature of the dependency which is determined by the tag.
1158 scopeTag.extractDepInfo(ctx, to, scopePaths)
Sundong Ahn20e998b2018-07-24 11:19:26 +09001159 }
Jiyong Parkc678ad32018-04-10 13:07:10 +09001160 })
1161}
1162
Jiyong Park0b0e1b92019-12-03 13:24:29 +09001163func (module *SdkLibrary) AndroidMkEntries() []android.AndroidMkEntries {
Paul Duffindfa131e2020-05-15 20:37:11 +01001164 if !module.requiresRuntimeImplementationLibrary() {
Paul Duffin43db9be2019-12-30 17:35:49 +00001165 return nil
1166 }
Jiyong Park0b0e1b92019-12-03 13:24:29 +09001167 entriesList := module.Library.AndroidMkEntries()
Yo Chiang07d75072020-06-05 17:43:19 +08001168 if module.sharedLibrary() {
1169 entries := &entriesList[0]
1170 entries.Required = append(entries.Required, module.xmlPermissionsModuleName())
1171 }
Jiyong Park0b0e1b92019-12-03 13:24:29 +09001172 return entriesList
Jiyong Park82484c02018-04-23 21:41:26 +09001173}
1174
Anton Hansson5fd5d242020-03-27 19:43:19 +00001175// The dist path of the stub artifacts
1176func (module *SdkLibrary) apiDistPath(apiScope *apiScope) string {
1177 if module.ModuleBase.Owner() != "" {
1178 return path.Join("apistubs", module.ModuleBase.Owner(), apiScope.name)
1179 } else if Bool(module.sdkLibraryProperties.Core_lib) {
1180 return path.Join("apistubs", "core", apiScope.name)
1181 } else {
1182 return path.Join("apistubs", "android", apiScope.name)
1183 }
1184}
1185
Paul Duffin12ceb462019-12-24 20:31:31 +00001186// Get the sdk version for use when compiling the stubs library.
Paul Duffin780c5f42020-05-12 15:52:55 +01001187func (module *SdkLibrary) sdkVersionForStubsLibrary(mctx android.EarlyModuleContext, apiScope *apiScope) string {
Paul Duffin87a05a32020-05-12 11:50:28 +01001188 scopeProperties := module.scopeToProperties[apiScope]
1189 if scopeProperties.Sdk_version != nil {
1190 return proptools.String(scopeProperties.Sdk_version)
1191 }
1192
Jiyong Parkf1691d22021-03-29 20:11:58 +09001193 sdkDep := decodeSdkDep(mctx, android.SdkContext(&module.Library))
Paul Duffin12ceb462019-12-24 20:31:31 +00001194 if sdkDep.hasStandardLibs() {
1195 // If building against a standard sdk then use the sdk version appropriate for the scope.
Paul Duffind1b3a922020-01-22 11:57:20 +00001196 return apiScope.sdkVersion
Paul Duffin12ceb462019-12-24 20:31:31 +00001197 } else {
1198 // Otherwise, use no system module.
1199 return "none"
1200 }
1201}
1202
Paul Duffin31310252020-11-20 21:26:20 +00001203func (module *SdkLibrary) distStem() string {
1204 return proptools.StringDefault(module.sdkLibraryProperties.Dist_stem, module.BaseModuleName())
1205}
1206
Paul Duffind1b3a922020-01-22 11:57:20 +00001207func (module *SdkLibrary) latestApiFilegroupName(apiScope *apiScope) string {
Paul Duffin31310252020-11-20 21:26:20 +00001208 return ":" + module.distStem() + ".api." + apiScope.name + ".latest"
Jiyong Park58c518b2018-05-12 22:29:12 +09001209}
Jiyong Parkc678ad32018-04-10 13:07:10 +09001210
Paul Duffind1b3a922020-01-22 11:57:20 +00001211func (module *SdkLibrary) latestRemovedApiFilegroupName(apiScope *apiScope) string {
Paul Duffin31310252020-11-20 21:26:20 +00001212 return ":" + module.distStem() + "-removed.api." + apiScope.name + ".latest"
Jiyong Parkc678ad32018-04-10 13:07:10 +09001213}
1214
Jaewoong Jung1a97ee02021-03-09 13:25:02 -08001215func (module *SdkLibrary) latestIncompatibilitiesFilegroupName(apiScope *apiScope) string {
1216 return ":" + module.distStem() + "-incompatibilities.api." + apiScope.name + ".latest"
1217}
1218
Anton Hansson944e77d2020-08-19 11:40:22 +01001219func childModuleVisibility(childVisibility []string) []string {
1220 if childVisibility == nil {
1221 // No child visibility set. The child will use the visibility of the sdk_library.
1222 return nil
1223 }
1224
1225 // Prepend an override to ignore the sdk_library's visibility, and rely on the child visibility.
1226 var visibility []string
1227 visibility = append(visibility, "//visibility:override")
1228 visibility = append(visibility, childVisibility...)
1229 return visibility
1230}
1231
Paul Duffin5df79302020-05-16 15:52:12 +01001232// Creates the implementation java library
1233func (module *SdkLibrary) createImplLibrary(mctx android.DefaultableHookContext) {
Paul Duffina2058f82020-06-24 16:22:38 +01001234 moduleNamePtr := proptools.StringPtr(module.BaseModuleName())
1235
Anton Hansson944e77d2020-08-19 11:40:22 +01001236 visibility := childModuleVisibility(module.sdkLibraryProperties.Impl_library_visibility)
1237
Paul Duffin5df79302020-05-16 15:52:12 +01001238 props := struct {
Paul Duffina2058f82020-06-24 16:22:38 +01001239 Name *string
1240 Visibility []string
1241 Instrument bool
Anton Hansson7f66efa2020-10-08 14:47:23 +01001242 Libs []string
Paul Duffina2058f82020-06-24 16:22:38 +01001243 ConfigurationName *string
Paul Duffin5df79302020-05-16 15:52:12 +01001244 }{
1245 Name: proptools.StringPtr(module.implLibraryModuleName()),
Anton Hansson944e77d2020-08-19 11:40:22 +01001246 Visibility: visibility,
Paul Duffin296cf332020-06-18 21:09:55 +01001247 // Set the instrument property to ensure it is instrumented when instrumentation is required.
1248 Instrument: true,
Anton Hansson7f66efa2020-10-08 14:47:23 +01001249 // Set the impl_only libs. Note that the module's "Libs" get appended as well, via the
1250 // addition of &module.properties below.
1251 Libs: module.sdkLibraryProperties.Impl_only_libs,
Paul Duffina2058f82020-06-24 16:22:38 +01001252
1253 // Make the created library behave as if it had the same name as this module.
1254 ConfigurationName: moduleNamePtr,
Paul Duffin5df79302020-05-16 15:52:12 +01001255 }
1256
1257 properties := []interface{}{
1258 &module.properties,
1259 &module.protoProperties,
1260 &module.deviceProperties,
Liz Kammera7a64f32020-07-09 15:16:41 -07001261 &module.dexProperties,
Paul Duffin5df79302020-05-16 15:52:12 +01001262 &module.dexpreoptProperties,
Colin Cross014489c2020-06-02 20:09:13 -07001263 &module.linter.properties,
Paul Duffin5df79302020-05-16 15:52:12 +01001264 &props,
1265 module.sdkComponentPropertiesForChildLibrary(),
1266 }
1267 mctx.CreateModule(LibraryFactory, properties...)
1268}
1269
Jiyong Parkc678ad32018-04-10 13:07:10 +09001270// Creates a static java library that has API stubs
Paul Duffinf0229202020-04-29 16:47:28 +01001271func (module *SdkLibrary) createStubsLibrary(mctx android.DefaultableHookContext, apiScope *apiScope) {
Jiyong Parkc678ad32018-04-10 13:07:10 +09001272 props := struct {
Dan Willemsen9f435972020-05-28 15:28:00 -07001273 Name *string
1274 Visibility []string
1275 Srcs []string
1276 Installable *bool
1277 Sdk_version *string
1278 System_modules *string
1279 Patch_module *string
1280 Libs []string
Anton Hanssondae54cd2021-04-21 16:30:10 +01001281 Static_libs []string
Dan Willemsen9f435972020-05-28 15:28:00 -07001282 Compile_dex *bool
1283 Java_version *string
1284 Openjdk9 struct {
Sundong Ahn054b19a2018-10-19 13:46:09 +09001285 Srcs []string
1286 Javacflags []string
1287 }
Anton Hansson5fd5d242020-03-27 19:43:19 +00001288 Dist struct {
1289 Targets []string
1290 Dest *string
1291 Dir *string
1292 Tag *string
1293 }
Jiyong Parkc678ad32018-04-10 13:07:10 +09001294 }{}
1295
Paul Duffinc3091c82020-05-08 14:16:20 +01001296 props.Name = proptools.StringPtr(module.stubsLibraryModuleName(apiScope))
Anton Hansson944e77d2020-08-19 11:40:22 +01001297 props.Visibility = childModuleVisibility(module.sdkLibraryProperties.Stubs_library_visibility)
Jiyong Parkc678ad32018-04-10 13:07:10 +09001298 // sources are generated from the droiddoc
Paul Duffinc3091c82020-05-08 14:16:20 +01001299 props.Srcs = []string{":" + module.stubsSourceModuleName(apiScope)}
Paul Duffin12ceb462019-12-24 20:31:31 +00001300 sdkVersion := module.sdkVersionForStubsLibrary(mctx, apiScope)
Paul Duffin52d398a2019-06-11 12:31:14 +01001301 props.Sdk_version = proptools.StringPtr(sdkVersion)
Paul Duffina18abc22020-05-16 18:54:24 +01001302 props.System_modules = module.deviceProperties.System_modules
1303 props.Patch_module = module.properties.Patch_module
Paul Duffin367ab912019-12-23 19:40:36 +00001304 props.Installable = proptools.BoolPtr(false)
Sundong Ahn054b19a2018-10-19 13:46:09 +09001305 props.Libs = module.sdkLibraryProperties.Stub_only_libs
Anton Hanssondae54cd2021-04-21 16:30:10 +01001306 props.Static_libs = module.sdkLibraryProperties.Stub_only_static_libs
Paul Duffine22c2ab2020-05-20 19:35:27 +01001307 // The stub-annotations library contains special versions of the annotations
1308 // with CLASS retention policy, so that they're kept.
1309 if proptools.Bool(module.sdkLibraryProperties.Annotations_enabled) {
1310 props.Libs = append(props.Libs, "stub-annotations")
1311 }
Paul Duffina18abc22020-05-16 18:54:24 +01001312 props.Openjdk9.Srcs = module.properties.Openjdk9.Srcs
1313 props.Openjdk9.Javacflags = module.properties.Openjdk9.Javacflags
Anton Hansson83509b52020-05-21 09:21:57 +01001314 // We compile the stubs for 1.8 in line with the main android.jar stubs, and potential
1315 // interop with older developer tools that don't support 1.9.
1316 props.Java_version = proptools.StringPtr("1.8")
Liz Kammera7a64f32020-07-09 15:16:41 -07001317 if module.dexProperties.Compile_dex != nil {
1318 props.Compile_dex = module.dexProperties.Compile_dex
Sundong Ahndd567f92018-07-31 17:19:11 +09001319 }
Jiyong Parkc678ad32018-04-10 13:07:10 +09001320
Anton Hansson5fd5d242020-03-27 19:43:19 +00001321 // Dist the class jar artifact for sdk builds.
1322 if !Bool(module.sdkLibraryProperties.No_dist) {
1323 props.Dist.Targets = []string{"sdk", "win_sdk"}
Paul Duffin31310252020-11-20 21:26:20 +00001324 props.Dist.Dest = proptools.StringPtr(fmt.Sprintf("%v.jar", module.distStem()))
Anton Hansson5fd5d242020-03-27 19:43:19 +00001325 props.Dist.Dir = proptools.StringPtr(module.apiDistPath(apiScope))
1326 props.Dist.Tag = proptools.StringPtr(".jar")
1327 }
Jiyong Parkc678ad32018-04-10 13:07:10 +09001328
Paul Duffin859fe962020-05-15 10:20:31 +01001329 mctx.CreateModule(LibraryFactory, &props, module.sdkComponentPropertiesForChildLibrary())
Jiyong Parkc678ad32018-04-10 13:07:10 +09001330}
1331
Paul Duffin6d0886e2020-04-07 18:49:53 +01001332// Creates a droidstubs module that creates stubs source files from the given full source
Paul Duffinc8782502020-04-29 20:45:27 +01001333// files and also updates and checks the API specification files.
Paul Duffin15f34ef2020-07-20 18:04:44 +01001334func (module *SdkLibrary) createStubsSourcesAndApi(mctx android.DefaultableHookContext, apiScope *apiScope, name string, scopeSpecificDroidstubsArgs []string) {
Jiyong Parkc678ad32018-04-10 13:07:10 +09001335 props := struct {
Sundong Ahn054b19a2018-10-19 13:46:09 +09001336 Name *string
Paul Duffin4911a892020-04-29 23:35:13 +01001337 Visibility []string
Sundong Ahn054b19a2018-10-19 13:46:09 +09001338 Srcs []string
1339 Installable *bool
Paul Duffin52d398a2019-06-11 12:31:14 +01001340 Sdk_version *string
Paul Duffin12ceb462019-12-24 20:31:31 +00001341 System_modules *string
Sundong Ahn054b19a2018-10-19 13:46:09 +09001342 Libs []string
Paul Duffin6877e6d2020-09-25 19:59:14 +01001343 Output_javadoc_comments *bool
Paul Duffin11512472019-02-11 15:55:17 +00001344 Arg_files []string
Sundong Ahn054b19a2018-10-19 13:46:09 +09001345 Args *string
Sundong Ahn054b19a2018-10-19 13:46:09 +09001346 Java_version *string
Paul Duffine22c2ab2020-05-20 19:35:27 +01001347 Annotations_enabled *bool
Sundong Ahn054b19a2018-10-19 13:46:09 +09001348 Merge_annotations_dirs []string
1349 Merge_inclusion_annotations_dirs []string
Paul Duffin0ff08bd2020-04-29 13:30:54 +01001350 Generate_stubs *bool
Anton Hanssone87b03d2020-12-21 15:29:34 +00001351 Previous_api *string
Sundong Ahn054b19a2018-10-19 13:46:09 +09001352 Check_api struct {
Anton Hanssone6056152020-12-31 10:37:27 +00001353 Current ApiToCheck
1354 Last_released ApiToCheck
Paul Duffin160fe412020-05-10 19:32:20 +01001355
1356 Api_lint struct {
1357 Enabled *bool
1358 New_since *string
1359 Baseline_file *string
1360 }
Jiyong Park58c518b2018-05-12 22:29:12 +09001361 }
Sundong Ahn1b92c822018-05-29 11:35:17 +09001362 Aidl struct {
1363 Include_dirs []string
1364 Local_include_dirs []string
1365 }
Paul Duffin040e9062020-11-23 17:41:36 +00001366 Dists []android.Dist
Jiyong Parkc678ad32018-04-10 13:07:10 +09001367 }{}
1368
Paul Duffin7b78b4d2020-04-28 14:08:32 +01001369 // The stubs source processing uses the same compile time classpath when extracting the
1370 // API from the implementation library as it does when compiling it. i.e. the same
1371 // * sdk version
1372 // * system_modules
1373 // * libs (static_libs/libs)
Paul Duffin250e6192019-06-07 10:44:37 +01001374
Paul Duffin0ff08bd2020-04-29 13:30:54 +01001375 props.Name = proptools.StringPtr(name)
Anton Hansson944e77d2020-08-19 11:40:22 +01001376 props.Visibility = childModuleVisibility(module.sdkLibraryProperties.Stubs_source_visibility)
Paul Duffina18abc22020-05-16 18:54:24 +01001377 props.Srcs = append(props.Srcs, module.properties.Srcs...)
1378 props.Sdk_version = module.deviceProperties.Sdk_version
1379 props.System_modules = module.deviceProperties.System_modules
Jiyong Parkc678ad32018-04-10 13:07:10 +09001380 props.Installable = proptools.BoolPtr(false)
Sundong Ahne6f0b052018-06-05 16:46:14 +09001381 // A droiddoc module has only one Libs property and doesn't distinguish between
1382 // shared libs and static libs. So we need to add both of these libs to Libs property.
Paul Duffina18abc22020-05-16 18:54:24 +01001383 props.Libs = module.properties.Libs
1384 props.Libs = append(props.Libs, module.properties.Static_libs...)
1385 props.Aidl.Include_dirs = module.deviceProperties.Aidl.Include_dirs
1386 props.Aidl.Local_include_dirs = module.deviceProperties.Aidl.Local_include_dirs
1387 props.Java_version = module.properties.Java_version
Jiyong Parkc678ad32018-04-10 13:07:10 +09001388
Paul Duffine22c2ab2020-05-20 19:35:27 +01001389 props.Annotations_enabled = module.sdkLibraryProperties.Annotations_enabled
Sundong Ahn054b19a2018-10-19 13:46:09 +09001390 props.Merge_annotations_dirs = module.sdkLibraryProperties.Merge_annotations_dirs
1391 props.Merge_inclusion_annotations_dirs = module.sdkLibraryProperties.Merge_inclusion_annotations_dirs
1392
Paul Duffin6d0886e2020-04-07 18:49:53 +01001393 droidstubsArgs := []string{}
Paul Duffin235ffff2019-12-24 10:41:30 +00001394 if len(module.sdkLibraryProperties.Api_packages) != 0 {
Paul Duffin6d0886e2020-04-07 18:49:53 +01001395 droidstubsArgs = append(droidstubsArgs, "--stub-packages "+strings.Join(module.sdkLibraryProperties.Api_packages, ":"))
Paul Duffin235ffff2019-12-24 10:41:30 +00001396 }
1397 if len(module.sdkLibraryProperties.Hidden_api_packages) != 0 {
Paul Duffin6d0886e2020-04-07 18:49:53 +01001398 droidstubsArgs = append(droidstubsArgs,
Paul Duffin235ffff2019-12-24 10:41:30 +00001399 android.JoinWithPrefix(module.sdkLibraryProperties.Hidden_api_packages, " --hide-package "))
1400 }
Paul Duffin6d0886e2020-04-07 18:49:53 +01001401 droidstubsArgs = append(droidstubsArgs, module.sdkLibraryProperties.Droiddoc_options...)
Paul Duffin235ffff2019-12-24 10:41:30 +00001402 disabledWarnings := []string{
1403 "MissingPermission",
1404 "BroadcastBehavior",
1405 "HiddenSuperclass",
1406 "DeprecationMismatch",
1407 "UnavailableSymbol",
1408 "SdkConstant",
1409 "HiddenTypeParameter",
1410 "Todo",
1411 "Typo",
1412 }
Paul Duffin6d0886e2020-04-07 18:49:53 +01001413 droidstubsArgs = append(droidstubsArgs, android.JoinWithPrefix(disabledWarnings, "--hide "))
Sundong Ahnfb2721f2018-09-17 13:23:09 +09001414
Paul Duffin6877e6d2020-09-25 19:59:14 +01001415 // Output Javadoc comments for public scope.
1416 if apiScope == apiScopePublic {
1417 props.Output_javadoc_comments = proptools.BoolPtr(true)
1418 }
1419
Paul Duffin1fb487d2020-04-07 18:50:10 +01001420 // Add in scope specific arguments.
Paul Duffin0ff08bd2020-04-29 13:30:54 +01001421 droidstubsArgs = append(droidstubsArgs, scopeSpecificDroidstubsArgs...)
Paul Duffin11512472019-02-11 15:55:17 +00001422 props.Arg_files = module.sdkLibraryProperties.Droiddoc_option_files
Paul Duffin6d0886e2020-04-07 18:49:53 +01001423 props.Args = proptools.StringPtr(strings.Join(droidstubsArgs, " "))
Jiyong Parkc678ad32018-04-10 13:07:10 +09001424
Paul Duffin15f34ef2020-07-20 18:04:44 +01001425 // List of APIs identified from the provided source files are created. They are later
1426 // compared against to the not-yet-released (a.k.a current) list of APIs and to the
1427 // last-released (a.k.a numbered) list of API.
1428 currentApiFileName := apiScope.apiFilePrefix + "current.txt"
1429 removedApiFileName := apiScope.apiFilePrefix + "removed.txt"
1430 apiDir := module.getApiDir()
1431 currentApiFileName = path.Join(apiDir, currentApiFileName)
1432 removedApiFileName = path.Join(apiDir, removedApiFileName)
Jiyong Parkc678ad32018-04-10 13:07:10 +09001433
Paul Duffin15f34ef2020-07-20 18:04:44 +01001434 // check against the not-yet-release API
1435 props.Check_api.Current.Api_file = proptools.StringPtr(currentApiFileName)
1436 props.Check_api.Current.Removed_api_file = proptools.StringPtr(removedApiFileName)
Jiyong Park58c518b2018-05-12 22:29:12 +09001437
Anton Hanssone6056152020-12-31 10:37:27 +00001438 if !(apiScope.unstable || module.sdkLibraryProperties.Unsafe_ignore_missing_latest_api) {
Paul Duffin15f34ef2020-07-20 18:04:44 +01001439 // check against the latest released API
1440 latestApiFilegroupName := proptools.StringPtr(module.latestApiFilegroupName(apiScope))
Anton Hanssone87b03d2020-12-21 15:29:34 +00001441 props.Previous_api = latestApiFilegroupName
Paul Duffin15f34ef2020-07-20 18:04:44 +01001442 props.Check_api.Last_released.Api_file = latestApiFilegroupName
1443 props.Check_api.Last_released.Removed_api_file = proptools.StringPtr(
1444 module.latestRemovedApiFilegroupName(apiScope))
Jaewoong Jung1a97ee02021-03-09 13:25:02 -08001445 props.Check_api.Last_released.Baseline_file = proptools.StringPtr(
1446 module.latestIncompatibilitiesFilegroupName(apiScope))
Paul Duffin160fe412020-05-10 19:32:20 +01001447
Paul Duffin15f34ef2020-07-20 18:04:44 +01001448 if proptools.Bool(module.sdkLibraryProperties.Api_lint.Enabled) {
1449 // Enable api lint.
1450 props.Check_api.Api_lint.Enabled = proptools.BoolPtr(true)
1451 props.Check_api.Api_lint.New_since = latestApiFilegroupName
Paul Duffin160fe412020-05-10 19:32:20 +01001452
Paul Duffin15f34ef2020-07-20 18:04:44 +01001453 // If it exists then pass a lint-baseline.txt through to droidstubs.
1454 baselinePath := path.Join(apiDir, apiScope.apiFilePrefix+"lint-baseline.txt")
1455 baselinePathRelativeToRoot := path.Join(mctx.ModuleDir(), baselinePath)
1456 paths, err := mctx.GlobWithDeps(baselinePathRelativeToRoot, nil)
1457 if err != nil {
1458 mctx.ModuleErrorf("error checking for presence of %s: %s", baselinePathRelativeToRoot, err)
1459 }
1460 if len(paths) == 1 {
1461 props.Check_api.Api_lint.Baseline_file = proptools.StringPtr(baselinePath)
1462 } else if len(paths) != 0 {
1463 mctx.ModuleErrorf("error checking for presence of %s: expected one path, found: %v", baselinePathRelativeToRoot, paths)
Paul Duffin160fe412020-05-10 19:32:20 +01001464 }
1465 }
Paul Duffin15f34ef2020-07-20 18:04:44 +01001466 }
Jiyong Park58c518b2018-05-12 22:29:12 +09001467
Paul Duffin15f34ef2020-07-20 18:04:44 +01001468 if !Bool(module.sdkLibraryProperties.No_dist) {
Paul Duffin040e9062020-11-23 17:41:36 +00001469 // Dist the api txt and removed api txt artifacts for sdk builds.
1470 distDir := proptools.StringPtr(path.Join(module.apiDistPath(apiScope), "api"))
1471 for _, p := range []struct {
1472 tag string
1473 pattern string
1474 }{
1475 {tag: ".api.txt", pattern: "%s.txt"},
1476 {tag: ".removed-api.txt", pattern: "%s-removed.txt"},
1477 } {
1478 props.Dists = append(props.Dists, android.Dist{
1479 Targets: []string{"sdk", "win_sdk"},
1480 Dir: distDir,
1481 Dest: proptools.StringPtr(fmt.Sprintf(p.pattern, module.distStem())),
1482 Tag: proptools.StringPtr(p.tag),
1483 })
1484 }
Anton Hansson5fd5d242020-03-27 19:43:19 +00001485 }
1486
Colin Cross84dfc3d2019-09-25 11:33:01 -07001487 mctx.CreateModule(DroidstubsFactory, &props)
Jiyong Parkc678ad32018-04-10 13:07:10 +09001488}
1489
Jooyung Han5e9013b2020-03-10 06:23:13 +09001490func (module *SdkLibrary) DepIsInSameApex(mctx android.BaseModuleContext, dep android.Module) bool {
1491 depTag := mctx.OtherModuleDependencyTag(dep)
1492 if depTag == xmlPermissionsFileTag {
1493 return true
1494 }
1495 return module.Library.DepIsInSameApex(mctx, dep)
1496}
1497
Jiyong Parkc678ad32018-04-10 13:07:10 +09001498// Creates the xml file that publicizes the runtime library
Paul Duffinf0229202020-04-29 16:47:28 +01001499func (module *SdkLibrary) createXmlFile(mctx android.DefaultableHookContext) {
Jiyong Parke3833882020-02-17 17:28:10 +09001500 props := struct {
Paul Duffin1dbe3ca2020-05-16 09:57:59 +01001501 Name *string
1502 Lib_name *string
1503 Apex_available []string
Jiyong Parke3833882020-02-17 17:28:10 +09001504 }{
Paul Duffineedc5d52020-06-12 17:46:39 +01001505 Name: proptools.StringPtr(module.xmlPermissionsModuleName()),
Jooyung Han5e9013b2020-03-10 06:23:13 +09001506 Lib_name: proptools.StringPtr(module.BaseModuleName()),
1507 Apex_available: module.ApexProperties.Apex_available,
Jiyong Parkc678ad32018-04-10 13:07:10 +09001508 }
Jiyong Parke3833882020-02-17 17:28:10 +09001509
Jiyong Parke3833882020-02-17 17:28:10 +09001510 mctx.CreateModule(sdkLibraryXmlFactory, &props)
Jiyong Parkc678ad32018-04-10 13:07:10 +09001511}
1512
Jiyong Parkf1691d22021-03-29 20:11:58 +09001513func PrebuiltJars(ctx android.BaseModuleContext, baseName string, s android.SdkSpec) android.Paths {
Jiyong Park54105c42021-03-31 18:17:53 +09001514 var ver android.ApiLevel
Jiyong Parkf1691d22021-03-29 20:11:58 +09001515 var kind android.SdkKind
1516 if s.UsePrebuilt(ctx) {
Jiyong Park54105c42021-03-31 18:17:53 +09001517 ver = s.ApiLevel
Jiyong Parkf1691d22021-03-29 20:11:58 +09001518 kind = s.Kind
Jiyong Parkc678ad32018-04-10 13:07:10 +09001519 } else {
Jiyong Park6a927c42020-01-21 02:03:43 +09001520 // We don't have prebuilt SDK for the specific sdkVersion.
1521 // Instead of breaking the build, fallback to use "system_current"
Jiyong Park54105c42021-03-31 18:17:53 +09001522 ver = android.FutureApiLevel
Jiyong Parkf1691d22021-03-29 20:11:58 +09001523 kind = android.SdkSystem
Sundong Ahn054b19a2018-10-19 13:46:09 +09001524 }
Jiyong Park6a927c42020-01-21 02:03:43 +09001525
1526 dir := filepath.Join("prebuilts", "sdk", ver.String(), kind.String())
Paul Duffin50061512020-01-21 16:31:05 +00001527 jar := filepath.Join(dir, baseName+".jar")
Sundong Ahn054b19a2018-10-19 13:46:09 +09001528 jarPath := android.ExistentPathForSource(ctx, jar)
Sundong Ahnae418ac2019-02-28 15:01:28 +09001529 if !jarPath.Valid() {
Colin Cross07c88562020-01-07 09:34:44 -08001530 if ctx.Config().AllowMissingDependencies() {
1531 return android.Paths{android.PathForSource(ctx, jar)}
1532 } else {
Jiyong Parkf1691d22021-03-29 20:11:58 +09001533 ctx.PropertyErrorf("sdk_library", "invalid sdk version %q, %q does not exist", s.Raw, jar)
Colin Cross07c88562020-01-07 09:34:44 -08001534 }
Sundong Ahnae418ac2019-02-28 15:01:28 +09001535 return nil
1536 }
Sundong Ahn054b19a2018-10-19 13:46:09 +09001537 return android.Paths{jarPath.Path()}
1538}
1539
Colin Crossaede88c2020-08-11 12:17:01 -07001540// 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 +01001541//
1542// If either this or the other module are on the platform then this will return
1543// false.
Colin Cross56a83212020-09-15 18:30:11 -07001544func withinSameApexesAs(ctx android.BaseModuleContext, other android.Module) bool {
1545 apexInfo := ctx.Provider(android.ApexInfoProvider).(android.ApexInfo)
1546 otherApexInfo := ctx.OtherModuleProvider(other, android.ApexInfoProvider).(android.ApexInfo)
1547 return len(otherApexInfo.InApexes) > 0 && reflect.DeepEqual(apexInfo.InApexes, otherApexInfo.InApexes)
Paul Duffin9b879592020-05-26 13:21:35 +01001548}
1549
Jiyong Parkf1691d22021-03-29 20:11:58 +09001550func (module *SdkLibrary) sdkJars(ctx android.BaseModuleContext, sdkVersion android.SdkSpec, headerJars bool) android.Paths {
Jiyong Park932cdfe2020-05-28 00:19:53 +09001551 // If the client doesn't set sdk_version, but if this library prefers stubs over
1552 // the impl library, let's provide the widest API surface possible. To do so,
1553 // force override sdk_version to module_current so that the closest possible API
1554 // surface could be found in selectHeaderJarsForSdkVersion
Jiyong Parkf1691d22021-03-29 20:11:58 +09001555 if module.defaultsToStubs() && !sdkVersion.Specified() {
Jiyong Park92315372021-04-02 08:45:46 +09001556 sdkVersion = android.SdkSpecFrom(ctx, "module_current")
Jiyong Park932cdfe2020-05-28 00:19:53 +09001557 }
Paul Duffind1b3a922020-01-22 11:57:20 +00001558
Paul Duffindaaa3322020-05-26 18:13:57 +01001559 // Only provide access to the implementation library if it is actually built.
1560 if module.requiresRuntimeImplementationLibrary() {
1561 // Check any special cases for java_sdk_library.
1562 //
1563 // Only allow access to the implementation library in the following condition:
1564 // * No sdk_version specified on the referencing module.
Paul Duffin9b879592020-05-26 13:21:35 +01001565 // * The referencing module is in the same apex as this.
Jiyong Parkf1691d22021-03-29 20:11:58 +09001566 if sdkVersion.Kind == android.SdkPrivate || withinSameApexesAs(ctx, module) {
Paul Duffindaaa3322020-05-26 18:13:57 +01001567 if headerJars {
1568 return module.HeaderJars()
1569 } else {
1570 return module.ImplementationJars()
1571 }
Sundong Ahn054b19a2018-10-19 13:46:09 +09001572 }
Jiyong Parkc678ad32018-04-10 13:07:10 +09001573 }
Paul Duffinb05d4292020-05-20 12:19:10 +01001574
Paul Duffin23970f42020-05-20 14:20:02 +01001575 return module.selectHeaderJarsForSdkVersion(ctx, sdkVersion)
Jiyong Parkc678ad32018-04-10 13:07:10 +09001576}
1577
Sundong Ahn241cd372018-07-13 16:16:44 +09001578// to satisfy SdkLibraryDependency interface
Jiyong Parkf1691d22021-03-29 20:11:58 +09001579func (module *SdkLibrary) SdkHeaderJars(ctx android.BaseModuleContext, sdkVersion android.SdkSpec) android.Paths {
Paul Duffind1b3a922020-01-22 11:57:20 +00001580 return module.sdkJars(ctx, sdkVersion, true /*headerJars*/)
1581}
1582
1583// to satisfy SdkLibraryDependency interface
Jiyong Parkf1691d22021-03-29 20:11:58 +09001584func (module *SdkLibrary) SdkImplementationJars(ctx android.BaseModuleContext, sdkVersion android.SdkSpec) android.Paths {
Paul Duffind1b3a922020-01-22 11:57:20 +00001585 return module.sdkJars(ctx, sdkVersion, false /*headerJars*/)
Sundong Ahn241cd372018-07-13 16:16:44 +09001586}
1587
Colin Cross571cccf2019-02-04 11:22:08 -08001588var javaSdkLibrariesKey = android.NewOnceKey("javaSdkLibraries")
1589
Jiyong Park82484c02018-04-23 21:41:26 +09001590func javaSdkLibraries(config android.Config) *[]string {
Colin Cross571cccf2019-02-04 11:22:08 -08001591 return config.Once(javaSdkLibrariesKey, func() interface{} {
Jiyong Park82484c02018-04-23 21:41:26 +09001592 return &[]string{}
1593 }).(*[]string)
1594}
1595
Paul Duffin749f98f2019-12-30 17:23:46 +00001596func (module *SdkLibrary) getApiDir() string {
1597 return proptools.StringDefault(module.sdkLibraryProperties.Api_dir, "api")
1598}
1599
Jiyong Parkc678ad32018-04-10 13:07:10 +09001600// For a java_sdk_library module, create internal modules for stubs, docs,
1601// runtime libs and xml file. If requested, the stubs and docs are created twice
1602// once for public API level and once for system API level
Paul Duffinf0229202020-04-29 16:47:28 +01001603func (module *SdkLibrary) CreateInternalModules(mctx android.DefaultableHookContext) {
1604 // If the module has been disabled then don't create any child modules.
1605 if !module.Enabled() {
1606 return
1607 }
1608
Paul Duffina18abc22020-05-16 18:54:24 +01001609 if len(module.properties.Srcs) == 0 {
Inseob Kimc0907f12019-02-08 21:00:45 +09001610 mctx.PropertyErrorf("srcs", "java_sdk_library must specify srcs")
Jooyung Han58f26ab2019-12-18 15:34:32 +09001611 return
Inseob Kimc0907f12019-02-08 21:00:45 +09001612 }
1613
Paul Duffin37e0b772019-12-30 17:20:10 +00001614 // If this builds against standard libraries (i.e. is not part of the core libraries)
Paul Duffin4f5c1ef2020-11-19 14:53:43 +00001615 // then assume it provides both system and test apis.
Jiyong Parkf1691d22021-03-29 20:11:58 +09001616 sdkDep := decodeSdkDep(mctx, android.SdkContext(&module.Library))
Paul Duffin37e0b772019-12-30 17:20:10 +00001617 hasSystemAndTestApis := sdkDep.hasStandardLibs()
Paul Duffin3375e352020-04-28 10:44:03 +01001618 module.sdkLibraryProperties.Generate_system_and_test_apis = hasSystemAndTestApis
Paul Duffin4f5c1ef2020-11-19 14:53:43 +00001619
Jaewoong Jung18aefc12020-12-21 09:11:10 -08001620 missingCurrentApi := false
Inseob Kim8098faa2019-03-18 10:19:51 +09001621
Paul Duffin3375e352020-04-28 10:44:03 +01001622 generatedScopes := module.getGeneratedApiScopes(mctx)
Paul Duffind1b3a922020-01-22 11:57:20 +00001623
Paul Duffin749f98f2019-12-30 17:23:46 +00001624 apiDir := module.getApiDir()
Paul Duffin3375e352020-04-28 10:44:03 +01001625 for _, scope := range generatedScopes {
Inseob Kim8098faa2019-03-18 10:19:51 +09001626 for _, api := range []string{"current.txt", "removed.txt"} {
Paul Duffind1b3a922020-01-22 11:57:20 +00001627 path := path.Join(mctx.ModuleDir(), apiDir, scope.apiFilePrefix+api)
Inseob Kim8098faa2019-03-18 10:19:51 +09001628 p := android.ExistentPathForSource(mctx, path)
1629 if !p.Valid() {
1630 mctx.ModuleErrorf("Current api file %#v doesn't exist", path)
Jaewoong Jung18aefc12020-12-21 09:11:10 -08001631 missingCurrentApi = true
Inseob Kim8098faa2019-03-18 10:19:51 +09001632 }
1633 }
1634 }
1635
Jaewoong Jung18aefc12020-12-21 09:11:10 -08001636 if missingCurrentApi {
Inseob Kim8098faa2019-03-18 10:19:51 +09001637 script := "build/soong/scripts/gen-java-current-api-files.sh"
1638 p := android.ExistentPathForSource(mctx, script)
1639
1640 if !p.Valid() {
1641 panic(fmt.Sprintf("script file %s doesn't exist", script))
1642 }
1643
1644 mctx.ModuleErrorf("One or more current api files are missing. "+
1645 "You can update them by:\n"+
Paul Duffin37e0b772019-12-30 17:20:10 +00001646 "%s %q %s && m update-api",
Paul Duffind1b3a922020-01-22 11:57:20 +00001647 script, filepath.Join(mctx.ModuleDir(), apiDir),
Paul Duffin3375e352020-04-28 10:44:03 +01001648 strings.Join(generatedScopes.Strings(func(s *apiScope) string { return s.apiFilePrefix }), " "))
Inseob Kim8098faa2019-03-18 10:19:51 +09001649 return
1650 }
1651
Paul Duffin3375e352020-04-28 10:44:03 +01001652 for _, scope := range generatedScopes {
Paul Duffin15f34ef2020-07-20 18:04:44 +01001653 // Use the stubs source name for legacy reasons.
1654 module.createStubsSourcesAndApi(mctx, scope, module.stubsSourceModuleName(scope), scope.droidstubsArgs)
Paul Duffin0ff08bd2020-04-29 13:30:54 +01001655
Paul Duffind1b3a922020-01-22 11:57:20 +00001656 module.createStubsLibrary(mctx, scope)
Inseob Kimc0907f12019-02-08 21:00:45 +09001657 }
1658
Paul Duffindfa131e2020-05-15 20:37:11 +01001659 if module.requiresRuntimeImplementationLibrary() {
Paul Duffin5df79302020-05-16 15:52:12 +01001660 // Create child module to create an implementation library.
1661 //
1662 // This temporarily creates a second implementation library that can be explicitly
1663 // referenced.
1664 //
1665 // TODO(b/156618935) - update comment once only one implementation library is created.
1666 module.createImplLibrary(mctx)
1667
Paul Duffindfa131e2020-05-15 20:37:11 +01001668 // Only create an XML permissions file that declares the library as being usable
1669 // as a shared library if required.
1670 if module.sharedLibrary() {
1671 module.createXmlFile(mctx)
1672 }
Paul Duffin43db9be2019-12-30 17:35:49 +00001673
1674 // record java_sdk_library modules so that they are exported to make
1675 javaSdkLibraries := javaSdkLibraries(mctx.Config())
1676 javaSdkLibrariesLock.Lock()
1677 defer javaSdkLibrariesLock.Unlock()
1678 *javaSdkLibraries = append(*javaSdkLibraries, module.BaseModuleName())
1679 }
Anton Hansson7f66efa2020-10-08 14:47:23 +01001680
1681 // Add the impl_only_libs *after* we're done using the Libs prop in submodules.
1682 module.properties.Libs = append(module.properties.Libs, module.sdkLibraryProperties.Impl_only_libs...)
Inseob Kimc0907f12019-02-08 21:00:45 +09001683}
1684
1685func (module *SdkLibrary) InitSdkLibraryProperties() {
Colin Crossce6734e2020-06-15 16:09:53 -07001686 module.addHostAndDeviceProperties()
1687 module.AddProperties(&module.sdkLibraryProperties)
Sundong Ahn054b19a2018-10-19 13:46:09 +09001688
Paul Duffin859fe962020-05-15 10:20:31 +01001689 module.initSdkLibraryComponent(&module.ModuleBase)
1690
Paul Duffina18abc22020-05-16 18:54:24 +01001691 module.properties.Installable = proptools.BoolPtr(true)
1692 module.deviceProperties.IsSDKLibrary = true
Inseob Kimc0907f12019-02-08 21:00:45 +09001693}
Sundong Ahn054b19a2018-10-19 13:46:09 +09001694
Paul Duffindfa131e2020-05-15 20:37:11 +01001695func (module *SdkLibrary) requiresRuntimeImplementationLibrary() bool {
1696 return !proptools.Bool(module.sdkLibraryProperties.Api_only)
1697}
1698
Jiyong Park932cdfe2020-05-28 00:19:53 +09001699func (module *SdkLibrary) defaultsToStubs() bool {
1700 return proptools.Bool(module.sdkLibraryProperties.Default_to_stubs)
1701}
1702
Paul Duffin1b1e8062020-05-08 13:44:43 +01001703// Defines how to name the individual component modules the sdk library creates.
1704type sdkLibraryComponentNamingScheme interface {
1705 stubsLibraryModuleName(scope *apiScope, baseName string) string
1706
1707 stubsSourceModuleName(scope *apiScope, baseName string) string
1708
1709 apiModuleName(scope *apiScope, baseName string) string
1710}
1711
1712type defaultNamingScheme struct {
1713}
1714
1715func (s *defaultNamingScheme) stubsLibraryModuleName(scope *apiScope, baseName string) string {
1716 return scope.stubsLibraryModuleName(baseName)
1717}
1718
1719func (s *defaultNamingScheme) stubsSourceModuleName(scope *apiScope, baseName string) string {
1720 return scope.stubsSourceModuleName(baseName)
1721}
1722
1723func (s *defaultNamingScheme) apiModuleName(scope *apiScope, baseName string) string {
1724 return scope.apiModuleName(baseName)
1725}
1726
1727var _ sdkLibraryComponentNamingScheme = (*defaultNamingScheme)(nil)
1728
Jaewoong Jungbc15e3a2021-03-10 17:02:43 -08001729func moduleStubLinkType(name string) (stub bool, ret sdkLinkType) {
Anton Hansson2d0c1942020-05-25 12:20:51 +01001730 // This suffix-based approach is fragile and could potentially mis-trigger.
1731 // TODO(b/155164730): Clean this up when modules no longer reference sdk_lib stubs directly.
Anton Hansson08f476b2021-04-07 15:32:19 +01001732 if strings.HasSuffix(name, apiScopePublic.stubsLibraryModuleNameSuffix()) {
1733 if name == "hwbinder.stubs" || name == "libcore_private.stubs" {
1734 // Due to a previous bug, these modules were not considered stubs, so we retain that.
1735 return false, javaPlatform
1736 }
Anton Hansson2d0c1942020-05-25 12:20:51 +01001737 return true, javaSdk
1738 }
Anton Hansson08f476b2021-04-07 15:32:19 +01001739 if strings.HasSuffix(name, apiScopeSystem.stubsLibraryModuleNameSuffix()) {
Anton Hansson2d0c1942020-05-25 12:20:51 +01001740 return true, javaSystem
1741 }
Anton Hansson08f476b2021-04-07 15:32:19 +01001742 if strings.HasSuffix(name, apiScopeModuleLib.stubsLibraryModuleNameSuffix()) {
Anton Hansson2d0c1942020-05-25 12:20:51 +01001743 return true, javaModule
1744 }
Anton Hansson08f476b2021-04-07 15:32:19 +01001745 if strings.HasSuffix(name, apiScopeTest.stubsLibraryModuleNameSuffix()) {
Anton Hansson2d0c1942020-05-25 12:20:51 +01001746 return true, javaSystem
1747 }
1748 return false, javaPlatform
1749}
1750
Jaewoong Jung4f158ee2019-07-11 10:05:35 -07001751// java_sdk_library is a special Java library that provides optional platform APIs to apps.
1752// In practice, it can be viewed as a combination of several modules: 1) stubs library that clients
1753// are linked against to, 2) droiddoc module that internally generates API stubs source files,
1754// 3) the real runtime shared library that implements the APIs, and 4) XML file for adding
1755// the runtime lib to the classpath at runtime if requested via <uses-library>.
Inseob Kimc0907f12019-02-08 21:00:45 +09001756func SdkLibraryFactory() android.Module {
1757 module := &SdkLibrary{}
Paul Duffinc3091c82020-05-08 14:16:20 +01001758
1759 // Initialize information common between source and prebuilt.
1760 module.initCommon(&module.ModuleBase)
1761
Inseob Kimc0907f12019-02-08 21:00:45 +09001762 module.InitSdkLibraryProperties()
Jooyung Han58f26ab2019-12-18 15:34:32 +09001763 android.InitApexModule(module)
Sundong Ahn054b19a2018-10-19 13:46:09 +09001764 InitJavaModule(module, android.HostAndDeviceSupported)
Paul Duffin3375e352020-04-28 10:44:03 +01001765
1766 // Initialize the map from scope to scope specific properties.
1767 scopeToProperties := make(map[*apiScope]*ApiScopeProperties)
1768 for _, scope := range allApiScopes {
1769 scopeToProperties[scope] = scope.scopeSpecificProperties(module)
1770 }
1771 module.scopeToProperties = scopeToProperties
1772
Paul Duffin4911a892020-04-29 23:35:13 +01001773 // Add the properties containing visibility rules so that they are checked.
Paul Duffin5df79302020-05-16 15:52:12 +01001774 android.AddVisibilityProperty(module, "impl_library_visibility", &module.sdkLibraryProperties.Impl_library_visibility)
Paul Duffin4911a892020-04-29 23:35:13 +01001775 android.AddVisibilityProperty(module, "stubs_library_visibility", &module.sdkLibraryProperties.Stubs_library_visibility)
1776 android.AddVisibilityProperty(module, "stubs_source_visibility", &module.sdkLibraryProperties.Stubs_source_visibility)
1777
Paul Duffin1b1e8062020-05-08 13:44:43 +01001778 module.SetDefaultableHook(func(ctx android.DefaultableHookContext) {
Paul Duffindfa131e2020-05-15 20:37:11 +01001779 // If no implementation is required then it cannot be used as a shared library
1780 // either.
1781 if !module.requiresRuntimeImplementationLibrary() {
1782 // If shared_library has been explicitly set to true then it is incompatible
1783 // with api_only: true.
1784 if proptools.Bool(module.commonSdkLibraryProperties.Shared_library) {
1785 ctx.PropertyErrorf("api_only/shared_library", "inconsistent settings, shared_library and api_only cannot both be true")
1786 }
1787 // Set shared_library: false.
1788 module.commonSdkLibraryProperties.Shared_library = proptools.BoolPtr(false)
1789 }
1790
Paul Duffin1b1e8062020-05-08 13:44:43 +01001791 if module.initCommonAfterDefaultsApplied(ctx) {
1792 module.CreateInternalModules(ctx)
1793 }
1794 })
Jiyong Parkc678ad32018-04-10 13:07:10 +09001795 return module
1796}
Colin Cross79c7c262019-04-17 11:11:46 -07001797
1798//
1799// SDK library prebuilts
1800//
1801
Paul Duffin56d44902020-01-31 13:36:25 +00001802// Properties associated with each api scope.
1803type sdkLibraryScopeProperties struct {
Colin Cross79c7c262019-04-17 11:11:46 -07001804 Jars []string `android:"path"`
1805
1806 Sdk_version *string
1807
Colin Cross79c7c262019-04-17 11:11:46 -07001808 // List of shared java libs that this module has dependencies to
1809 Libs []string
Paul Duffin3d1248c2020-04-09 00:10:17 +01001810
Paul Duffinc8782502020-04-29 20:45:27 +01001811 // The stubs source.
Paul Duffin3d1248c2020-04-09 00:10:17 +01001812 Stub_srcs []string `android:"path"`
Paul Duffin1fd005d2020-04-09 01:08:11 +01001813
1814 // The current.txt
Paul Duffin0f8faff2020-05-20 16:18:00 +01001815 Current_api *string `android:"path"`
Paul Duffin1fd005d2020-04-09 01:08:11 +01001816
1817 // The removed.txt
Paul Duffin0f8faff2020-05-20 16:18:00 +01001818 Removed_api *string `android:"path"`
Colin Cross79c7c262019-04-17 11:11:46 -07001819}
1820
Paul Duffin56d44902020-01-31 13:36:25 +00001821type sdkLibraryImportProperties struct {
Paul Duffinfcfd7912020-01-31 17:54:30 +00001822 // List of shared java libs, common to all scopes, that this module has
1823 // dependencies to
1824 Libs []string
Paul Duffin1267d872021-04-16 17:21:36 +01001825
1826 // If set to true, compile dex files for the stubs. Defaults to false.
1827 Compile_dex *bool
Paul Duffin56d44902020-01-31 13:36:25 +00001828}
1829
Paul Duffineedc5d52020-06-12 17:46:39 +01001830type SdkLibraryImport struct {
Colin Cross79c7c262019-04-17 11:11:46 -07001831 android.ModuleBase
1832 android.DefaultableModuleBase
1833 prebuilt android.Prebuilt
Paul Duffindd46f712020-02-10 13:37:10 +00001834 android.ApexModuleBase
1835 android.SdkBase
Colin Cross79c7c262019-04-17 11:11:46 -07001836
Paul Duffin37856732021-02-26 14:24:15 +00001837 hiddenAPI
1838
Colin Cross79c7c262019-04-17 11:11:46 -07001839 properties sdkLibraryImportProperties
1840
Paul Duffin46a26a82020-04-07 19:27:04 +01001841 // Map from api scope to the scope specific property structure.
1842 scopeProperties map[*apiScope]*sdkLibraryScopeProperties
1843
Paul Duffin56d44902020-01-31 13:36:25 +00001844 commonToSdkLibraryAndImport
Paul Duffineedc5d52020-06-12 17:46:39 +01001845
1846 // The reference to the implementation library created by the source module.
1847 // Is nil if the source module does not exist.
1848 implLibraryModule *Library
1849
1850 // The reference to the xml permissions module created by the source module.
1851 // Is nil if the source module does not exist.
1852 xmlPermissionsFileModule *sdkLibraryXml
Paul Duffin39853512021-02-26 11:09:39 +00001853
1854 // Path to the dex implementation jar obtained from the prebuilt_apex, if any.
1855 dexJarFile android.Path
Colin Cross79c7c262019-04-17 11:11:46 -07001856}
1857
Paul Duffineedc5d52020-06-12 17:46:39 +01001858var _ SdkLibraryDependency = (*SdkLibraryImport)(nil)
Colin Cross79c7c262019-04-17 11:11:46 -07001859
Paul Duffin46a26a82020-04-07 19:27:04 +01001860// The type of a structure that contains a field of type sdkLibraryScopeProperties
1861// for each apiscope in allApiScopes, e.g. something like:
1862// struct {
1863// Public sdkLibraryScopeProperties
1864// System sdkLibraryScopeProperties
1865// ...
1866// }
1867var allScopeStructType = createAllScopePropertiesStructType()
1868
1869// Dynamically create a structure type for each apiscope in allApiScopes.
1870func createAllScopePropertiesStructType() reflect.Type {
1871 var fields []reflect.StructField
1872 for _, apiScope := range allApiScopes {
1873 field := reflect.StructField{
1874 Name: apiScope.fieldName,
1875 Type: reflect.TypeOf(sdkLibraryScopeProperties{}),
1876 }
1877 fields = append(fields, field)
1878 }
1879
1880 return reflect.StructOf(fields)
1881}
1882
1883// Create an instance of the scope specific structure type and return a map
1884// from apiscope to a pointer to each scope specific field.
1885func createPropertiesInstance() (interface{}, map[*apiScope]*sdkLibraryScopeProperties) {
1886 allScopePropertiesPtr := reflect.New(allScopeStructType)
1887 allScopePropertiesStruct := allScopePropertiesPtr.Elem()
1888 scopeProperties := make(map[*apiScope]*sdkLibraryScopeProperties)
1889
1890 for _, apiScope := range allApiScopes {
1891 field := allScopePropertiesStruct.FieldByName(apiScope.fieldName)
1892 scopeProperties[apiScope] = field.Addr().Interface().(*sdkLibraryScopeProperties)
1893 }
1894
1895 return allScopePropertiesPtr.Interface(), scopeProperties
1896}
1897
Jaewoong Jung4f158ee2019-07-11 10:05:35 -07001898// java_sdk_library_import imports a prebuilt java_sdk_library.
Colin Cross79c7c262019-04-17 11:11:46 -07001899func sdkLibraryImportFactory() android.Module {
Paul Duffineedc5d52020-06-12 17:46:39 +01001900 module := &SdkLibraryImport{}
Colin Cross79c7c262019-04-17 11:11:46 -07001901
Paul Duffin46a26a82020-04-07 19:27:04 +01001902 allScopeProperties, scopeToProperties := createPropertiesInstance()
1903 module.scopeProperties = scopeToProperties
1904 module.AddProperties(&module.properties, allScopeProperties)
Colin Cross79c7c262019-04-17 11:11:46 -07001905
Paul Duffinc3091c82020-05-08 14:16:20 +01001906 // Initialize information common between source and prebuilt.
1907 module.initCommon(&module.ModuleBase)
1908
Paul Duffin0bdcb272020-02-06 15:24:57 +00001909 android.InitPrebuiltModule(module, &[]string{""})
Paul Duffindd46f712020-02-10 13:37:10 +00001910 android.InitApexModule(module)
1911 android.InitSdkAwareModule(module)
Colin Cross79c7c262019-04-17 11:11:46 -07001912 InitJavaModule(module, android.HostAndDeviceSupported)
1913
Paul Duffin1b1e8062020-05-08 13:44:43 +01001914 module.SetDefaultableHook(func(mctx android.DefaultableHookContext) {
1915 if module.initCommonAfterDefaultsApplied(mctx) {
1916 module.createInternalModules(mctx)
1917 }
1918 })
Colin Cross79c7c262019-04-17 11:11:46 -07001919 return module
1920}
1921
Paul Duffineedc5d52020-06-12 17:46:39 +01001922func (module *SdkLibraryImport) Prebuilt() *android.Prebuilt {
Colin Cross79c7c262019-04-17 11:11:46 -07001923 return &module.prebuilt
1924}
1925
Paul Duffineedc5d52020-06-12 17:46:39 +01001926func (module *SdkLibraryImport) Name() string {
Colin Cross79c7c262019-04-17 11:11:46 -07001927 return module.prebuilt.Name(module.ModuleBase.Name())
1928}
1929
Paul Duffineedc5d52020-06-12 17:46:39 +01001930func (module *SdkLibraryImport) createInternalModules(mctx android.DefaultableHookContext) {
Colin Cross79c7c262019-04-17 11:11:46 -07001931
Paul Duffin50061512020-01-21 16:31:05 +00001932 // If the build is configured to use prebuilts then force this to be preferred.
Jeongik Cha816a23a2020-07-08 01:09:23 +09001933 if mctx.Config().AlwaysUsePrebuiltSdks() {
Paul Duffin50061512020-01-21 16:31:05 +00001934 module.prebuilt.ForcePrefer()
1935 }
1936
Paul Duffin46a26a82020-04-07 19:27:04 +01001937 for apiScope, scopeProperties := range module.scopeProperties {
Paul Duffin56d44902020-01-31 13:36:25 +00001938 if len(scopeProperties.Jars) == 0 {
1939 continue
1940 }
1941
Paul Duffinbbb546b2020-04-09 00:07:11 +01001942 module.createJavaImportForStubs(mctx, apiScope, scopeProperties)
Paul Duffin3d1248c2020-04-09 00:10:17 +01001943
Paul Duffin0f8faff2020-05-20 16:18:00 +01001944 if len(scopeProperties.Stub_srcs) > 0 {
1945 module.createPrebuiltStubsSources(mctx, apiScope, scopeProperties)
1946 }
Paul Duffin56d44902020-01-31 13:36:25 +00001947 }
Colin Cross79c7c262019-04-17 11:11:46 -07001948
1949 javaSdkLibraries := javaSdkLibraries(mctx.Config())
1950 javaSdkLibrariesLock.Lock()
1951 defer javaSdkLibrariesLock.Unlock()
1952 *javaSdkLibraries = append(*javaSdkLibraries, module.BaseModuleName())
1953}
1954
Paul Duffineedc5d52020-06-12 17:46:39 +01001955func (module *SdkLibraryImport) createJavaImportForStubs(mctx android.DefaultableHookContext, apiScope *apiScope, scopeProperties *sdkLibraryScopeProperties) {
Paul Duffinbbb546b2020-04-09 00:07:11 +01001956 // Creates a java import for the jar with ".stubs" suffix
1957 props := struct {
Paul Duffin1dbe3ca2020-05-16 09:57:59 +01001958 Name *string
1959 Sdk_version *string
1960 Libs []string
1961 Jars []string
1962 Prefer *bool
Paul Duffin1267d872021-04-16 17:21:36 +01001963 Compile_dex *bool
Paul Duffinbbb546b2020-04-09 00:07:11 +01001964 }{}
Paul Duffinc3091c82020-05-08 14:16:20 +01001965 props.Name = proptools.StringPtr(module.stubsLibraryModuleName(apiScope))
Paul Duffinbbb546b2020-04-09 00:07:11 +01001966 props.Sdk_version = scopeProperties.Sdk_version
1967 // Prepend any of the libs from the legacy public properties to the libs for each of the
1968 // scopes to avoid having to duplicate them in each scope.
1969 props.Libs = append(module.properties.Libs, scopeProperties.Libs...)
1970 props.Jars = scopeProperties.Jars
Paul Duffin1dbe3ca2020-05-16 09:57:59 +01001971
Paul Duffin38b57852020-05-13 16:08:09 +01001972 // The imports are preferred if the java_sdk_library_import is preferred.
1973 props.Prefer = proptools.BoolPtr(module.prebuilt.Prefer())
Paul Duffin859fe962020-05-15 10:20:31 +01001974
Paul Duffin1267d872021-04-16 17:21:36 +01001975 // The imports need to be compiled to dex if the java_sdk_library_import requests it.
1976 props.Compile_dex = module.properties.Compile_dex
1977
Paul Duffin859fe962020-05-15 10:20:31 +01001978 mctx.CreateModule(ImportFactory, &props, module.sdkComponentPropertiesForChildLibrary())
Paul Duffinbbb546b2020-04-09 00:07:11 +01001979}
1980
Paul Duffineedc5d52020-06-12 17:46:39 +01001981func (module *SdkLibraryImport) createPrebuiltStubsSources(mctx android.DefaultableHookContext, apiScope *apiScope, scopeProperties *sdkLibraryScopeProperties) {
Paul Duffin3d1248c2020-04-09 00:10:17 +01001982 props := struct {
Paul Duffin38b57852020-05-13 16:08:09 +01001983 Name *string
1984 Srcs []string
1985 Prefer *bool
Paul Duffin3d1248c2020-04-09 00:10:17 +01001986 }{}
Paul Duffinc3091c82020-05-08 14:16:20 +01001987 props.Name = proptools.StringPtr(module.stubsSourceModuleName(apiScope))
Paul Duffin3d1248c2020-04-09 00:10:17 +01001988 props.Srcs = scopeProperties.Stub_srcs
1989 mctx.CreateModule(PrebuiltStubsSourcesFactory, &props)
Paul Duffin38b57852020-05-13 16:08:09 +01001990
1991 // The stubs source is preferred if the java_sdk_library_import is preferred.
1992 props.Prefer = proptools.BoolPtr(module.prebuilt.Prefer())
Paul Duffin3d1248c2020-04-09 00:10:17 +01001993}
1994
Paul Duffin44f1d842020-06-26 20:17:02 +01001995// Add the dependencies on the child module in the component deps mutator so that it
1996// creates references to the prebuilt and not the source modules.
1997func (module *SdkLibraryImport) ComponentDepsMutator(ctx android.BottomUpMutatorContext) {
Paul Duffin46a26a82020-04-07 19:27:04 +01001998 for apiScope, scopeProperties := range module.scopeProperties {
Paul Duffin56d44902020-01-31 13:36:25 +00001999 if len(scopeProperties.Jars) == 0 {
2000 continue
2001 }
2002
2003 // Add dependencies to the prebuilt stubs library
Paul Duffin864116c2021-04-02 10:24:13 +01002004 ctx.AddVariationDependencies(nil, apiScope.stubsTag, android.PrebuiltNameFromSource(module.stubsLibraryModuleName(apiScope)))
Paul Duffin0f8faff2020-05-20 16:18:00 +01002005
2006 if len(scopeProperties.Stub_srcs) > 0 {
2007 // Add dependencies to the prebuilt stubs source library
Paul Duffin864116c2021-04-02 10:24:13 +01002008 ctx.AddVariationDependencies(nil, apiScope.stubsSourceTag, android.PrebuiltNameFromSource(module.stubsSourceModuleName(apiScope)))
Paul Duffin0f8faff2020-05-20 16:18:00 +01002009 }
Paul Duffin56d44902020-01-31 13:36:25 +00002010 }
Paul Duffin44f1d842020-06-26 20:17:02 +01002011}
2012
2013// Add other dependencies as normal.
2014func (module *SdkLibraryImport) DepsMutator(ctx android.BottomUpMutatorContext) {
Paul Duffineedc5d52020-06-12 17:46:39 +01002015
2016 implName := module.implLibraryModuleName()
2017 if ctx.OtherModuleExists(implName) {
2018 ctx.AddVariationDependencies(nil, implLibraryTag, implName)
2019
2020 xmlPermissionsModuleName := module.xmlPermissionsModuleName()
2021 if module.sharedLibrary() && ctx.OtherModuleExists(xmlPermissionsModuleName) {
2022 // Add dependency to the rule for generating the xml permissions file
2023 ctx.AddDependency(module, xmlPermissionsFileTag, xmlPermissionsModuleName)
2024 }
2025 }
Colin Cross79c7c262019-04-17 11:11:46 -07002026}
2027
Jiyong Park45bf82e2020-12-15 22:29:02 +09002028var _ android.ApexModule = (*SdkLibraryImport)(nil)
2029
2030// Implements android.ApexModule
Paul Duffineedc5d52020-06-12 17:46:39 +01002031func (module *SdkLibraryImport) DepIsInSameApex(mctx android.BaseModuleContext, dep android.Module) bool {
2032 depTag := mctx.OtherModuleDependencyTag(dep)
2033 if depTag == xmlPermissionsFileTag {
2034 return true
2035 }
2036
2037 // None of the other dependencies of the java_sdk_library_import are in the same apex
2038 // as the one that references this module.
2039 return false
2040}
2041
Jiyong Park45bf82e2020-12-15 22:29:02 +09002042// Implements android.ApexModule
Dan Albertc8060532020-07-22 22:32:17 -07002043func (module *SdkLibraryImport) ShouldSupportSdkVersion(ctx android.BaseModuleContext,
2044 sdkVersion android.ApiLevel) error {
Jooyung Han749dc692020-04-15 11:03:39 +09002045 // we don't check prebuilt modules for sdk_version
2046 return nil
2047}
2048
Paul Duffineedc5d52020-06-12 17:46:39 +01002049func (module *SdkLibraryImport) OutputFiles(tag string) (android.Paths, error) {
Paul Duffin46dc45a2020-05-14 15:39:10 +01002050 return module.commonOutputFiles(tag)
2051}
2052
Paul Duffineedc5d52020-06-12 17:46:39 +01002053func (module *SdkLibraryImport) GenerateAndroidBuildActions(ctx android.ModuleContext) {
Paul Duffina2ae7e02020-09-11 11:55:00 +01002054 module.generateCommonBuildActions(ctx)
2055
Paul Duffin39853512021-02-26 11:09:39 +00002056 var deapexerModule android.Module
2057
Paul Duffin0f8faff2020-05-20 16:18:00 +01002058 // Record the paths to the prebuilt stubs library and stubs source.
Colin Cross79c7c262019-04-17 11:11:46 -07002059 ctx.VisitDirectDeps(func(to android.Module) {
2060 tag := ctx.OtherModuleDependencyTag(to)
2061
Paul Duffin0f8faff2020-05-20 16:18:00 +01002062 // Extract information from any of the scope specific dependencies.
2063 if scopeTag, ok := tag.(scopeDependencyTag); ok {
2064 apiScope := scopeTag.apiScope
2065 scopePaths := module.getScopePathsCreateIfNeeded(apiScope)
2066
2067 // Extract information from the dependency. The exact information extracted
2068 // is determined by the nature of the dependency which is determined by the tag.
2069 scopeTag.extractDepInfo(ctx, to, scopePaths)
Paul Duffineedc5d52020-06-12 17:46:39 +01002070 } else if tag == implLibraryTag {
2071 if implLibrary, ok := to.(*Library); ok {
2072 module.implLibraryModule = implLibrary
2073 } else {
2074 ctx.ModuleErrorf("implementation library must be of type *java.Library but was %T", to)
2075 }
2076 } else if tag == xmlPermissionsFileTag {
2077 if xmlPermissionsFileModule, ok := to.(*sdkLibraryXml); ok {
2078 module.xmlPermissionsFileModule = xmlPermissionsFileModule
2079 } else {
2080 ctx.ModuleErrorf("xml permissions file module must be of type *sdkLibraryXml but was %T", to)
2081 }
Colin Cross79c7c262019-04-17 11:11:46 -07002082 }
Paul Duffin39853512021-02-26 11:09:39 +00002083
2084 // Save away the `deapexer` module on which this depends, if any.
2085 if tag == android.DeapexerTag {
2086 deapexerModule = to
2087 }
Colin Cross79c7c262019-04-17 11:11:46 -07002088 })
Paul Duffin0f8faff2020-05-20 16:18:00 +01002089
2090 // Populate the scope paths with information from the properties.
2091 for apiScope, scopeProperties := range module.scopeProperties {
2092 if len(scopeProperties.Jars) == 0 {
2093 continue
2094 }
2095
2096 paths := module.getScopePathsCreateIfNeeded(apiScope)
2097 paths.currentApiFilePath = android.OptionalPathForModuleSrc(ctx, scopeProperties.Current_api)
2098 paths.removedApiFilePath = android.OptionalPathForModuleSrc(ctx, scopeProperties.Removed_api)
2099 }
Paul Duffin39853512021-02-26 11:09:39 +00002100
2101 if ctx.Device() {
2102 // If this is a variant created for a prebuilt_apex then use the dex implementation jar
2103 // obtained from the associated deapexer module.
2104 ai := ctx.Provider(android.ApexInfoProvider).(android.ApexInfo)
2105 if ai.ForPrebuiltApex {
2106 if deapexerModule == nil {
2107 // This should never happen as a variant for a prebuilt_apex is only created if the
2108 // deapxer module has been configured to export the dex implementation jar for this module.
2109 ctx.ModuleErrorf("internal error: module %q does not depend on a `deapexer` module for prebuilt_apex %q",
2110 module.Name(), ai.ApexVariationName)
2111 }
2112
2113 // Get the path of the dex implementation jar from the `deapexer` module.
2114 di := ctx.OtherModuleProvider(deapexerModule, android.DeapexerProvider).(android.DeapexerInfo)
2115 if dexOutputPath := di.PrebuiltExportPath(module.BaseModuleName(), ".dexjar"); dexOutputPath != nil {
2116 module.dexJarFile = dexOutputPath
Paul Duffin37856732021-02-26 14:24:15 +00002117 module.initHiddenAPI(ctx, module.configurationName)
2118 module.hiddenAPIExtractInformation(ctx, dexOutputPath, module.findScopePaths(apiScopePublic).stubsImplPath[0])
Paul Duffin39853512021-02-26 11:09:39 +00002119 } else {
2120 // This should never happen as a variant for a prebuilt_apex is only created if the
2121 // prebuilt_apex has been configured to export the java library dex file.
2122 ctx.ModuleErrorf("internal error: no dex implementation jar available from prebuilt_apex %q", deapexerModule.Name())
2123 }
2124 }
2125 }
Colin Cross79c7c262019-04-17 11:11:46 -07002126}
2127
Jiyong Parkf1691d22021-03-29 20:11:58 +09002128func (module *SdkLibraryImport) sdkJars(ctx android.BaseModuleContext, sdkVersion android.SdkSpec, headerJars bool) android.Paths {
Paul Duffineedc5d52020-06-12 17:46:39 +01002129
2130 // For consistency with SdkLibrary make the implementation jar available to libraries that
2131 // are within the same APEX.
2132 implLibraryModule := module.implLibraryModule
Colin Cross56a83212020-09-15 18:30:11 -07002133 if implLibraryModule != nil && withinSameApexesAs(ctx, module) {
Paul Duffineedc5d52020-06-12 17:46:39 +01002134 if headerJars {
2135 return implLibraryModule.HeaderJars()
2136 } else {
2137 return implLibraryModule.ImplementationJars()
2138 }
2139 }
2140
Paul Duffin23970f42020-05-20 14:20:02 +01002141 return module.selectHeaderJarsForSdkVersion(ctx, sdkVersion)
Paul Duffin56d44902020-01-31 13:36:25 +00002142}
2143
Colin Cross79c7c262019-04-17 11:11:46 -07002144// to satisfy SdkLibraryDependency interface
Jiyong Parkf1691d22021-03-29 20:11:58 +09002145func (module *SdkLibraryImport) SdkHeaderJars(ctx android.BaseModuleContext, sdkVersion android.SdkSpec) android.Paths {
Colin Cross79c7c262019-04-17 11:11:46 -07002146 // This module is just a wrapper for the prebuilt stubs.
Paul Duffineedc5d52020-06-12 17:46:39 +01002147 return module.sdkJars(ctx, sdkVersion, true)
Colin Cross79c7c262019-04-17 11:11:46 -07002148}
2149
2150// to satisfy SdkLibraryDependency interface
Jiyong Parkf1691d22021-03-29 20:11:58 +09002151func (module *SdkLibraryImport) SdkImplementationJars(ctx android.BaseModuleContext, sdkVersion android.SdkSpec) android.Paths {
Colin Cross79c7c262019-04-17 11:11:46 -07002152 // This module is just a wrapper for the stubs.
Paul Duffineedc5d52020-06-12 17:46:39 +01002153 return module.sdkJars(ctx, sdkVersion, false)
2154}
2155
Ulya Trafimovichdbf31662020-12-17 12:07:54 +00002156// to satisfy UsesLibraryDependency interface
Paul Duffineedc5d52020-06-12 17:46:39 +01002157func (module *SdkLibraryImport) DexJarBuildPath() android.Path {
Paul Duffin39853512021-02-26 11:09:39 +00002158 // The dex implementation jar extracted from the .apex file should be used in preference to the
2159 // source.
2160 if module.dexJarFile != nil {
2161 return module.dexJarFile
2162 }
Paul Duffineedc5d52020-06-12 17:46:39 +01002163 if module.implLibraryModule == nil {
2164 return nil
2165 } else {
2166 return module.implLibraryModule.DexJarBuildPath()
2167 }
2168}
2169
Ulya Trafimovichdbf31662020-12-17 12:07:54 +00002170// to satisfy UsesLibraryDependency interface
Ulya Trafimovich31e444e2020-08-14 17:32:16 +01002171func (module *SdkLibraryImport) DexJarInstallPath() android.Path {
2172 if module.implLibraryModule == nil {
2173 return nil
2174 } else {
2175 return module.implLibraryModule.DexJarInstallPath()
2176 }
2177}
2178
Ulya Trafimovichdbf31662020-12-17 12:07:54 +00002179// to satisfy UsesLibraryDependency interface
2180func (module *SdkLibraryImport) ClassLoaderContexts() dexpreopt.ClassLoaderContextMap {
2181 return nil
2182}
2183
Paul Duffineedc5d52020-06-12 17:46:39 +01002184// to satisfy apex.javaDependency interface
2185func (module *SdkLibraryImport) JacocoReportClassesFile() android.Path {
2186 if module.implLibraryModule == nil {
2187 return nil
2188 } else {
2189 return module.implLibraryModule.JacocoReportClassesFile()
2190 }
2191}
2192
2193// to satisfy apex.javaDependency interface
Colin Cross08dca382020-07-21 20:31:17 -07002194func (module *SdkLibraryImport) LintDepSets() LintDepSets {
2195 if module.implLibraryModule == nil {
2196 return LintDepSets{}
2197 } else {
2198 return module.implLibraryModule.LintDepSets()
2199 }
2200}
2201
2202// to satisfy apex.javaDependency interface
Paul Duffineedc5d52020-06-12 17:46:39 +01002203func (module *SdkLibraryImport) Stem() string {
2204 return module.BaseModuleName()
Colin Cross79c7c262019-04-17 11:11:46 -07002205}
Jiyong Parke3833882020-02-17 17:28:10 +09002206
Paul Duffin44b481b2020-06-17 16:59:43 +01002207var _ ApexDependency = (*SdkLibraryImport)(nil)
2208
2209// to satisfy java.ApexDependency interface
2210func (module *SdkLibraryImport) HeaderJars() android.Paths {
2211 if module.implLibraryModule == nil {
2212 return nil
2213 } else {
2214 return module.implLibraryModule.HeaderJars()
2215 }
2216}
2217
2218// to satisfy java.ApexDependency interface
2219func (module *SdkLibraryImport) ImplementationAndResourcesJars() android.Paths {
2220 if module.implLibraryModule == nil {
2221 return nil
2222 } else {
2223 return module.implLibraryModule.ImplementationAndResourcesJars()
2224 }
2225}
2226
Jiyong Parke3833882020-02-17 17:28:10 +09002227//
2228// java_sdk_library_xml
2229//
2230type sdkLibraryXml struct {
2231 android.ModuleBase
2232 android.DefaultableModuleBase
2233 android.ApexModuleBase
2234
2235 properties sdkLibraryXmlProperties
2236
2237 outputFilePath android.OutputPath
2238 installDirPath android.InstallPath
Colin Cross56a83212020-09-15 18:30:11 -07002239
2240 hideApexVariantFromMake bool
Jiyong Parke3833882020-02-17 17:28:10 +09002241}
2242
2243type sdkLibraryXmlProperties struct {
2244 // canonical name of the lib
2245 Lib_name *string
2246}
2247
2248// java_sdk_library_xml builds the permission xml file for a java_sdk_library.
2249// Not to be used directly by users. java_sdk_library internally uses this.
2250func sdkLibraryXmlFactory() android.Module {
2251 module := &sdkLibraryXml{}
2252
2253 module.AddProperties(&module.properties)
2254
2255 android.InitApexModule(module)
2256 android.InitAndroidArchModule(module, android.DeviceSupported, android.MultilibCommon)
2257
2258 return module
2259}
2260
Colin Crossaede88c2020-08-11 12:17:01 -07002261func (module *sdkLibraryXml) UniqueApexVariations() bool {
2262 // sdkLibraryXml needs a unique variation per APEX because the generated XML file contains the path to the
2263 // mounted APEX, which contains the name of the APEX.
2264 return true
2265}
2266
Jiyong Parke3833882020-02-17 17:28:10 +09002267// from android.PrebuiltEtcModule
Jooyung Han0703fd82020-08-26 22:11:53 +09002268func (module *sdkLibraryXml) BaseDir() string {
2269 return "etc"
2270}
2271
2272// from android.PrebuiltEtcModule
Jiyong Parke3833882020-02-17 17:28:10 +09002273func (module *sdkLibraryXml) SubDir() string {
2274 return "permissions"
2275}
2276
2277// from android.PrebuiltEtcModule
2278func (module *sdkLibraryXml) OutputFile() android.OutputPath {
2279 return module.outputFilePath
2280}
2281
2282// from android.ApexModule
2283func (module *sdkLibraryXml) AvailableFor(what string) bool {
2284 return true
2285}
2286
2287func (module *sdkLibraryXml) DepsMutator(ctx android.BottomUpMutatorContext) {
2288 // do nothing
2289}
2290
Jiyong Park45bf82e2020-12-15 22:29:02 +09002291var _ android.ApexModule = (*sdkLibraryXml)(nil)
2292
2293// Implements android.ApexModule
Dan Albertc8060532020-07-22 22:32:17 -07002294func (module *sdkLibraryXml) ShouldSupportSdkVersion(ctx android.BaseModuleContext,
2295 sdkVersion android.ApiLevel) error {
Jooyung Han749dc692020-04-15 11:03:39 +09002296 // sdkLibraryXml doesn't need to be checked separately because java_sdk_library is checked
2297 return nil
2298}
2299
Jiyong Parke3833882020-02-17 17:28:10 +09002300// File path to the runtime implementation library
Colin Cross56a83212020-09-15 18:30:11 -07002301func (module *sdkLibraryXml) implPath(ctx android.ModuleContext) string {
Jiyong Parke3833882020-02-17 17:28:10 +09002302 implName := proptools.String(module.properties.Lib_name)
Colin Cross56a83212020-09-15 18:30:11 -07002303 if apexInfo := ctx.Provider(android.ApexInfoProvider).(android.ApexInfo); !apexInfo.IsForPlatform() {
Colin Crosse07f2312020-08-13 11:24:56 -07002304 // TODO(b/146468504): ApexVariationName() is only a soong module name, not apex name.
Jiyong Parke3833882020-02-17 17:28:10 +09002305 // In most cases, this works fine. But when apex_name is set or override_apex is used
2306 // this can be wrong.
Colin Cross56a83212020-09-15 18:30:11 -07002307 return fmt.Sprintf("/apex/%s/javalib/%s.jar", apexInfo.ApexVariationName, implName)
Jiyong Parke3833882020-02-17 17:28:10 +09002308 }
2309 partition := "system"
2310 if module.SocSpecific() {
2311 partition = "vendor"
2312 } else if module.DeviceSpecific() {
2313 partition = "odm"
2314 } else if module.ProductSpecific() {
2315 partition = "product"
2316 } else if module.SystemExtSpecific() {
2317 partition = "system_ext"
2318 }
2319 return "/" + partition + "/framework/" + implName + ".jar"
2320}
2321
2322func (module *sdkLibraryXml) GenerateAndroidBuildActions(ctx android.ModuleContext) {
Colin Cross56a83212020-09-15 18:30:11 -07002323 module.hideApexVariantFromMake = !ctx.Provider(android.ApexInfoProvider).(android.ApexInfo).IsForPlatform()
2324
Jiyong Parke3833882020-02-17 17:28:10 +09002325 libName := proptools.String(module.properties.Lib_name)
Colin Cross56a83212020-09-15 18:30:11 -07002326 xmlContent := fmt.Sprintf(permissionsTemplate, libName, module.implPath(ctx))
Jiyong Parke3833882020-02-17 17:28:10 +09002327
2328 module.outputFilePath = android.PathForModuleOut(ctx, libName+".xml").OutputPath
Colin Crossf1a035e2020-11-16 17:32:30 -08002329 rule := android.NewRuleBuilder(pctx, ctx)
Jiyong Parke3833882020-02-17 17:28:10 +09002330 rule.Command().
2331 Text("/bin/bash -c \"echo -e '" + xmlContent + "'\" > ").
2332 Output(module.outputFilePath)
2333
Colin Crossf1a035e2020-11-16 17:32:30 -08002334 rule.Build("java_sdk_xml", "Permission XML")
Jiyong Parke3833882020-02-17 17:28:10 +09002335
2336 module.installDirPath = android.PathForModuleInstall(ctx, "etc", module.SubDir())
2337}
2338
2339func (module *sdkLibraryXml) AndroidMkEntries() []android.AndroidMkEntries {
Colin Cross56a83212020-09-15 18:30:11 -07002340 if module.hideApexVariantFromMake {
Jiyong Parke3833882020-02-17 17:28:10 +09002341 return []android.AndroidMkEntries{android.AndroidMkEntries{
2342 Disabled: true,
2343 }}
2344 }
2345
2346 return []android.AndroidMkEntries{android.AndroidMkEntries{
2347 Class: "ETC",
2348 OutputFile: android.OptionalPathForPath(module.outputFilePath),
2349 ExtraEntries: []android.AndroidMkExtraEntriesFunc{
Colin Crossaa255532020-07-03 13:18:24 -07002350 func(ctx android.AndroidMkExtraEntriesContext, entries *android.AndroidMkEntries) {
Jiyong Parke3833882020-02-17 17:28:10 +09002351 entries.SetString("LOCAL_MODULE_TAGS", "optional")
2352 entries.SetString("LOCAL_MODULE_PATH", module.installDirPath.ToMakePath().String())
2353 entries.SetString("LOCAL_INSTALLED_MODULE_STEM", module.outputFilePath.Base())
2354 },
2355 },
2356 }}
2357}
Paul Duffindd46f712020-02-10 13:37:10 +00002358
2359type sdkLibrarySdkMemberType struct {
2360 android.SdkMemberTypeBase
2361}
2362
2363func (s *sdkLibrarySdkMemberType) AddDependencies(mctx android.BottomUpMutatorContext, dependencyTag blueprint.DependencyTag, names []string) {
2364 mctx.AddVariationDependencies(nil, dependencyTag, names...)
2365}
2366
2367func (s *sdkLibrarySdkMemberType) IsInstance(module android.Module) bool {
2368 _, ok := module.(*SdkLibrary)
2369 return ok
2370}
2371
2372func (s *sdkLibrarySdkMemberType) AddPrebuiltModule(ctx android.SdkMemberContext, member android.SdkMember) android.BpModule {
2373 return ctx.SnapshotBuilder().AddPrebuiltModule(member, "java_sdk_library_import")
2374}
2375
2376func (s *sdkLibrarySdkMemberType) CreateVariantPropertiesStruct() android.SdkMemberProperties {
2377 return &sdkLibrarySdkMemberProperties{}
2378}
2379
2380type sdkLibrarySdkMemberProperties struct {
2381 android.SdkMemberPropertiesBase
2382
2383 // Scope to per scope properties.
2384 Scopes map[*apiScope]scopeProperties
2385
2386 // Additional libraries that the exported stubs libraries depend upon.
2387 Libs []string
Paul Duffin3d1248c2020-04-09 00:10:17 +01002388
2389 // The Java stubs source files.
2390 Stub_srcs []string
Paul Duffinf7a64332020-05-13 16:54:55 +01002391
2392 // The naming scheme.
2393 Naming_scheme *string
Paul Duffind7eb1c22020-05-26 20:57:10 +01002394
2395 // True if the java_sdk_library_import is for a shared library, false
2396 // otherwise.
2397 Shared_library *bool
Paul Duffina2ae7e02020-09-11 11:55:00 +01002398
Paul Duffin1267d872021-04-16 17:21:36 +01002399 // True if the stub imports should produce dex jars.
2400 Compile_dex *bool
2401
Paul Duffina2ae7e02020-09-11 11:55:00 +01002402 // The paths to the doctag files to add to the prebuilt.
2403 Doctag_paths android.Paths
Paul Duffindd46f712020-02-10 13:37:10 +00002404}
2405
2406type scopeProperties struct {
Paul Duffin1fd005d2020-04-09 01:08:11 +01002407 Jars android.Paths
2408 StubsSrcJar android.Path
2409 CurrentApiFile android.Path
2410 RemovedApiFile android.Path
2411 SdkVersion string
Paul Duffindd46f712020-02-10 13:37:10 +00002412}
2413
2414func (s *sdkLibrarySdkMemberProperties) PopulateFromVariant(ctx android.SdkMemberContext, variant android.Module) {
2415 sdk := variant.(*SdkLibrary)
2416
2417 s.Scopes = make(map[*apiScope]scopeProperties)
2418 for _, apiScope := range allApiScopes {
Paul Duffin803a9562020-05-20 11:52:25 +01002419 paths := sdk.findScopePaths(apiScope)
2420 if paths == nil {
2421 continue
2422 }
2423
Paul Duffindd46f712020-02-10 13:37:10 +00002424 jars := paths.stubsImplPath
2425 if len(jars) > 0 {
2426 properties := scopeProperties{}
2427 properties.Jars = jars
Paul Duffin780c5f42020-05-12 15:52:55 +01002428 properties.SdkVersion = sdk.sdkVersionForStubsLibrary(ctx.SdkModuleContext(), apiScope)
Paul Duffin0f8faff2020-05-20 16:18:00 +01002429 properties.StubsSrcJar = paths.stubsSrcJar.Path()
Paul Duffin10269f12020-06-19 18:39:55 +01002430 if paths.currentApiFilePath.Valid() {
2431 properties.CurrentApiFile = paths.currentApiFilePath.Path()
2432 }
2433 if paths.removedApiFilePath.Valid() {
2434 properties.RemovedApiFile = paths.removedApiFilePath.Path()
2435 }
Paul Duffindd46f712020-02-10 13:37:10 +00002436 s.Scopes[apiScope] = properties
2437 }
2438 }
2439
2440 s.Libs = sdk.properties.Libs
Paul Duffindfa131e2020-05-15 20:37:11 +01002441 s.Naming_scheme = sdk.commonSdkLibraryProperties.Naming_scheme
Paul Duffind7eb1c22020-05-26 20:57:10 +01002442 s.Shared_library = proptools.BoolPtr(sdk.sharedLibrary())
Paul Duffin1267d872021-04-16 17:21:36 +01002443 s.Compile_dex = sdk.dexProperties.Compile_dex
Paul Duffina2ae7e02020-09-11 11:55:00 +01002444 s.Doctag_paths = sdk.doctagPaths
Paul Duffindd46f712020-02-10 13:37:10 +00002445}
2446
2447func (s *sdkLibrarySdkMemberProperties) AddToPropertySet(ctx android.SdkMemberContext, propertySet android.BpPropertySet) {
Paul Duffinf7a64332020-05-13 16:54:55 +01002448 if s.Naming_scheme != nil {
2449 propertySet.AddProperty("naming_scheme", proptools.String(s.Naming_scheme))
2450 }
Paul Duffind7eb1c22020-05-26 20:57:10 +01002451 if s.Shared_library != nil {
2452 propertySet.AddProperty("shared_library", *s.Shared_library)
2453 }
Paul Duffin1267d872021-04-16 17:21:36 +01002454 if s.Compile_dex != nil {
2455 propertySet.AddProperty("compile_dex", *s.Compile_dex)
2456 }
Paul Duffinf7a64332020-05-13 16:54:55 +01002457
Paul Duffindd46f712020-02-10 13:37:10 +00002458 for _, apiScope := range allApiScopes {
2459 if properties, ok := s.Scopes[apiScope]; ok {
Paul Duffin6b836ba2020-05-13 19:19:49 +01002460 scopeSet := propertySet.AddPropertySet(apiScope.propertyName)
Paul Duffindd46f712020-02-10 13:37:10 +00002461
Paul Duffin3d1248c2020-04-09 00:10:17 +01002462 scopeDir := filepath.Join("sdk_library", s.OsPrefix(), apiScope.name)
2463
Paul Duffindd46f712020-02-10 13:37:10 +00002464 var jars []string
2465 for _, p := range properties.Jars {
Paul Duffin3d1248c2020-04-09 00:10:17 +01002466 dest := filepath.Join(scopeDir, ctx.Name()+"-stubs.jar")
Paul Duffindd46f712020-02-10 13:37:10 +00002467 ctx.SnapshotBuilder().CopyToSnapshot(p, dest)
2468 jars = append(jars, dest)
2469 }
2470 scopeSet.AddProperty("jars", jars)
2471
Paul Duffin3d1248c2020-04-09 00:10:17 +01002472 // Merge the stubs source jar into the snapshot zip so that when it is unpacked
Paul Duffinab5ac8f2020-11-18 16:37:35 +00002473 // the source files are also unpacked.
Paul Duffin3d1248c2020-04-09 00:10:17 +01002474 snapshotRelativeDir := filepath.Join(scopeDir, ctx.Name()+"_stub_sources")
2475 ctx.SnapshotBuilder().UnzipToSnapshot(properties.StubsSrcJar, snapshotRelativeDir)
Paul Duffinab5ac8f2020-11-18 16:37:35 +00002476 scopeSet.AddProperty("stub_srcs", []string{snapshotRelativeDir})
Paul Duffin3d1248c2020-04-09 00:10:17 +01002477
Paul Duffin1fd005d2020-04-09 01:08:11 +01002478 if properties.CurrentApiFile != nil {
2479 currentApiSnapshotPath := filepath.Join(scopeDir, ctx.Name()+".txt")
2480 ctx.SnapshotBuilder().CopyToSnapshot(properties.CurrentApiFile, currentApiSnapshotPath)
2481 scopeSet.AddProperty("current_api", currentApiSnapshotPath)
2482 }
2483
2484 if properties.RemovedApiFile != nil {
2485 removedApiSnapshotPath := filepath.Join(scopeDir, ctx.Name()+"-removed.txt")
Paul Duffin3dbf9fd2020-06-02 13:00:02 +01002486 ctx.SnapshotBuilder().CopyToSnapshot(properties.RemovedApiFile, removedApiSnapshotPath)
Paul Duffin1fd005d2020-04-09 01:08:11 +01002487 scopeSet.AddProperty("removed_api", removedApiSnapshotPath)
2488 }
2489
Paul Duffindd46f712020-02-10 13:37:10 +00002490 if properties.SdkVersion != "" {
2491 scopeSet.AddProperty("sdk_version", properties.SdkVersion)
2492 }
2493 }
2494 }
2495
Paul Duffina2ae7e02020-09-11 11:55:00 +01002496 if len(s.Doctag_paths) > 0 {
2497 dests := []string{}
2498 for _, p := range s.Doctag_paths {
2499 dest := filepath.Join("doctags", p.Rel())
2500 ctx.SnapshotBuilder().CopyToSnapshot(p, dest)
2501 dests = append(dests, dest)
2502 }
2503 propertySet.AddProperty("doctag_files", dests)
2504 }
2505
Paul Duffindd46f712020-02-10 13:37:10 +00002506 if len(s.Libs) > 0 {
2507 propertySet.AddPropertyWithTag("libs", s.Libs, ctx.SnapshotBuilder().SdkMemberReferencePropertyTag(false))
2508 }
2509}