blob: d7f14d65942b05802e5dc9472c811cc620519dc6 [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.
Paul Duffin976b0e52021-04-27 23:20:26 +0100342 android.RegisterSdkMemberType(javaSdkLibrarySdkMemberType)
Jiyong Parkc678ad32018-04-10 13:07:10 +0900343}
344
Paul Duffin43dc1cc2019-12-19 11:18:54 +0000345func RegisterSdkLibraryBuildComponents(ctx android.RegistrationContext) {
346 ctx.RegisterModuleType("java_sdk_library", SdkLibraryFactory)
347 ctx.RegisterModuleType("java_sdk_library_import", sdkLibraryImportFactory)
348}
349
Paul Duffin3375e352020-04-28 10:44:03 +0100350// Properties associated with each api scope.
351type ApiScopeProperties struct {
352 // Indicates whether the api surface is generated.
353 //
354 // If this is set for any scope then all scopes must explicitly specify if they
355 // are enabled. This is to prevent new usages from depending on legacy behavior.
356 //
357 // Otherwise, if this is not set for any scope then the default behavior is
358 // scope specific so please refer to the scope specific property documentation.
359 Enabled *bool
Paul Duffin87a05a32020-05-12 11:50:28 +0100360
361 // The sdk_version to use for building the stubs.
362 //
363 // If not specified then it will use an sdk_version determined as follows:
Trevor Radcliffedf8aa1f2021-11-04 14:25:39 +0000364 //
Paul Duffin87a05a32020-05-12 11:50:28 +0100365 // 1) If the sdk_version specified on the java_sdk_library is none then this
Trevor Radcliffedf8aa1f2021-11-04 14:25:39 +0000366 // will be none. This is used for java_sdk_library instances that are used
367 // to create stubs that contribute to the core_current sdk version.
368 // 2) Otherwise, it is assumed that this library extends but does not
369 // contribute directly to a specific sdk_version and so this uses the
370 // sdk_version appropriate for the api scope. e.g. public will use
371 // sdk_version: current, system will use sdk_version: system_current, etc.
Paul Duffin87a05a32020-05-12 11:50:28 +0100372 //
373 // This does not affect the sdk_version used for either generating the stubs source
374 // or the API file. They both have to use the same sdk_version as is used for
375 // compiling the implementation library.
376 Sdk_version *string
Paul Duffin3375e352020-04-28 10:44:03 +0100377}
378
Jiyong Parkc678ad32018-04-10 13:07:10 +0900379type sdkLibraryProperties struct {
Anton Hanssonf8ea3722021-09-16 14:24:13 +0100380 // List of source files that are needed to compile the API, but are not part of runtime library.
381 Api_srcs []string `android:"arch_variant"`
382
Paul Duffin5df79302020-05-16 15:52:12 +0100383 // Visibility for impl library module. If not specified then defaults to the
384 // visibility property.
385 Impl_library_visibility []string
386
Paul Duffin4911a892020-04-29 23:35:13 +0100387 // Visibility for stubs library modules. If not specified then defaults to the
388 // visibility property.
389 Stubs_library_visibility []string
390
391 // Visibility for stubs source modules. If not specified then defaults to the
392 // visibility property.
393 Stubs_source_visibility []string
394
Anton Hansson7f66efa2020-10-08 14:47:23 +0100395 // List of Java libraries that will be in the classpath when building the implementation lib
396 Impl_only_libs []string `android:"arch_variant"`
397
Sundong Ahnf043cf62018-06-25 16:04:37 +0900398 // List of Java libraries that will be in the classpath when building stubs
399 Stub_only_libs []string `android:"arch_variant"`
400
Anton Hanssondae54cd2021-04-21 16:30:10 +0100401 // List of Java libraries that will included in stub libraries
402 Stub_only_static_libs []string `android:"arch_variant"`
403
Paul Duffin7a586d32019-12-30 17:09:34 +0000404 // list of package names that will be documented and publicized as API.
405 // This allows the API to be restricted to a subset of the source files provided.
406 // If this is unspecified then all the source files will be treated as being part
407 // of the API.
Jiyong Parkc678ad32018-04-10 13:07:10 +0900408 Api_packages []string
409
Jiyong Park5a2c9d72018-05-01 22:25:41 +0900410 // list of package names that must be hidden from the API
411 Hidden_api_packages []string
412
Paul Duffin749f98f2019-12-30 17:23:46 +0000413 // the relative path to the directory containing the api specification files.
414 // Defaults to "api".
415 Api_dir *string
416
Paul Duffindfa131e2020-05-15 20:37:11 +0100417 // Determines whether a runtime implementation library is built; defaults to false.
418 //
419 // If true then it also prevents the module from being used as a shared module, i.e.
420 // it is as is shared_library: false, was set.
Paul Duffin43db9be2019-12-30 17:35:49 +0000421 Api_only *bool
422
Paul Duffin11512472019-02-11 15:55:17 +0000423 // local files that are used within user customized droiddoc options.
424 Droiddoc_option_files []string
425
Spandan Das93e95992021-07-29 18:26:39 +0000426 // additional droiddoc options.
Paul Duffin11512472019-02-11 15:55:17 +0000427 // Available variables for substitution:
428 //
429 // $(location <label>): the path to the droiddoc_option_files with name <label>
Sundong Ahndd567f92018-07-31 17:19:11 +0900430 Droiddoc_options []string
431
Paul Duffine22c2ab2020-05-20 19:35:27 +0100432 // is set to true, Metalava will allow framework SDK to contain annotations.
433 Annotations_enabled *bool
434
Sundong Ahn054b19a2018-10-19 13:46:09 +0900435 // a list of top-level directories containing files to merge qualifier annotations
436 // (i.e. those intended to be included in the stubs written) from.
437 Merge_annotations_dirs []string
438
439 // a list of top-level directories containing Java stub files to merge show/hide annotations from.
440 Merge_inclusion_annotations_dirs []string
441
Paul Duffin4f5c1ef2020-11-19 14:53:43 +0000442 // If set to true then don't create dist rules.
443 No_dist *bool
Sundong Ahn80a87b32019-05-13 15:02:50 +0900444
Paul Duffin31310252020-11-20 21:26:20 +0000445 // The stem for the artifacts that are copied to the dist, if not specified
446 // then defaults to the base module name.
447 //
448 // For each scope the following artifacts are copied to the apistubs/<scope>
449 // directory in the dist.
450 // * stubs impl jar -> <dist-stem>.jar
451 // * API specification file -> api/<dist-stem>.txt
452 // * Removed API specification file -> api/<dist-stem>-removed.txt
453 //
454 // Also used to construct the name of the filegroup (created by prebuilt_apis)
455 // that references the latest released API and remove API specification files.
456 // * API specification filegroup -> <dist-stem>.api.<scope>.latest
457 // * Removed API specification filegroup -> <dist-stem>-removed.api.<scope>.latest
Jaewoong Jung1a97ee02021-03-09 13:25:02 -0800458 // * API incompatibilities baseline filegroup -> <dist-stem>-incompatibilities.api.<scope>.latest
Paul Duffin31310252020-11-20 21:26:20 +0000459 Dist_stem *string
460
Colin Cross986b69a2021-06-01 13:13:40 -0700461 // The subdirectory for the artifacts that are copied to the dist directory. If not specified
Colin Cross3dd66252021-06-01 14:05:09 -0700462 // then defaults to "unknown". Should be set to "android" for anything that should be published
Colin Cross986b69a2021-06-01 13:13:40 -0700463 // in the public Android SDK.
464 Dist_group *string
465
Anton Hanssondff2c782020-12-21 17:10:01 +0000466 // A compatibility mode that allows historical API-tracking files to not exist.
467 // Do not use.
468 Unsafe_ignore_missing_latest_api bool
469
Paul Duffin3375e352020-04-28 10:44:03 +0100470 // indicates whether system and test apis should be generated.
471 Generate_system_and_test_apis bool `blueprint:"mutated"`
472
473 // The properties specific to the public api scope
474 //
475 // Unless explicitly specified by using public.enabled the public api scope is
476 // enabled by default in both legacy and non-legacy mode.
477 Public ApiScopeProperties
478
479 // The properties specific to the system api scope
480 //
481 // In legacy mode the system api scope is enabled by default when sdk_version
482 // is set to something other than "none".
483 //
484 // In non-legacy mode the system api scope is disabled by default.
485 System ApiScopeProperties
486
487 // The properties specific to the test api scope
488 //
489 // In legacy mode the test api scope is enabled by default when sdk_version
490 // is set to something other than "none".
491 //
492 // In non-legacy mode the test api scope is disabled by default.
493 Test ApiScopeProperties
Paul Duffin37e0b772019-12-30 17:20:10 +0000494
Paul Duffin0c5bae52020-06-02 13:00:08 +0100495 // The properties specific to the module-lib api scope
Paul Duffin8f265b92020-04-28 14:13:56 +0100496 //
Paul Duffin0c5bae52020-06-02 13:00:08 +0100497 // Unless explicitly specified by using test.enabled the module-lib api scope is
Paul Duffin8f265b92020-04-28 14:13:56 +0100498 // disabled by default.
499 Module_lib ApiScopeProperties
500
Paul Duffin0c5bae52020-06-02 13:00:08 +0100501 // The properties specific to the system-server api scope
502 //
503 // Unless explicitly specified by using test.enabled the module-lib api scope is
504 // disabled by default.
505 System_server ApiScopeProperties
506
Jiyong Park932cdfe2020-05-28 00:19:53 +0900507 // Determines if the stubs are preferred over the implementation library
508 // for linking, even when the client doesn't specify sdk_version. When this
509 // is set to true, such clients are provided with the widest API surface that
510 // this lib provides. Note however that this option doesn't affect the clients
511 // that are in the same APEX as this library. In that case, the clients are
512 // always linked with the implementation library. Default is false.
513 Default_to_stubs *bool
514
Paul Duffin160fe412020-05-10 19:32:20 +0100515 // Properties related to api linting.
516 Api_lint struct {
517 // Enable api linting.
518 Enabled *bool
519 }
520
Jiyong Parkc678ad32018-04-10 13:07:10 +0900521 // TODO: determines whether to create HTML doc or not
522 //Html_doc *bool
523}
524
Paul Duffin0f8faff2020-05-20 16:18:00 +0100525// Paths to outputs from java_sdk_library and java_sdk_library_import.
526//
527// Fields that are android.Paths are always set (during GenerateAndroidBuildActions).
528// OptionalPaths are always set by java_sdk_library but may not be set by
529// java_sdk_library_import as not all instances provide that information.
Paul Duffind1b3a922020-01-22 11:57:20 +0000530type scopePaths struct {
Paul Duffin0f8faff2020-05-20 16:18:00 +0100531 // The path (represented as Paths for convenience when returning) to the stubs header jar.
532 //
533 // That is the jar that is created by turbine.
534 stubsHeaderPath android.Paths
535
536 // The path (represented as Paths for convenience when returning) to the stubs implementation jar.
537 //
538 // This is not the implementation jar, it still only contains stubs.
539 stubsImplPath android.Paths
540
Paul Duffin1267d872021-04-16 17:21:36 +0100541 // The dex jar for the stubs.
542 //
543 // This is not the implementation jar, it still only contains stubs.
Martin Stjernholm8be1e6d2021-09-15 03:34:04 +0100544 stubsDexJarPath OptionalDexJarPath
Paul Duffin1267d872021-04-16 17:21:36 +0100545
Paul Duffin0f8faff2020-05-20 16:18:00 +0100546 // The API specification file, e.g. system_current.txt.
547 currentApiFilePath android.OptionalPath
548
549 // The specification of API elements removed since the last release.
550 removedApiFilePath android.OptionalPath
551
552 // The stubs source jar.
553 stubsSrcJar android.OptionalPath
Anton Hanssond78eb762021-09-21 15:25:12 +0100554
555 // Extracted annotations.
556 annotationsZip android.OptionalPath
Paul Duffind1b3a922020-01-22 11:57:20 +0000557}
558
Colin Crossdcf71b22021-02-01 13:59:03 -0800559func (paths *scopePaths) extractStubsLibraryInfoFromDependency(ctx android.ModuleContext, dep android.Module) error {
560 if ctx.OtherModuleHasProvider(dep, JavaInfoProvider) {
561 lib := ctx.OtherModuleProvider(dep, JavaInfoProvider).(JavaInfo)
562 paths.stubsHeaderPath = lib.HeaderJars
563 paths.stubsImplPath = lib.ImplementationJars
Paul Duffin1267d872021-04-16 17:21:36 +0100564
565 libDep := dep.(UsesLibraryDependency)
566 paths.stubsDexJarPath = libDep.DexJarBuildPath()
Paul Duffinc8782502020-04-29 20:45:27 +0100567 return nil
568 } else {
Colin Crossdcf71b22021-02-01 13:59:03 -0800569 return fmt.Errorf("expected module that has JavaInfoProvider, e.g. java_library")
Paul Duffinc8782502020-04-29 20:45:27 +0100570 }
571}
572
Paul Duffin0ff08bd2020-04-29 13:30:54 +0100573func (paths *scopePaths) treatDepAsApiStubsProvider(dep android.Module, action func(provider ApiStubsProvider)) error {
574 if apiStubsProvider, ok := dep.(ApiStubsProvider); ok {
575 action(apiStubsProvider)
Paul Duffinc8782502020-04-29 20:45:27 +0100576 return nil
577 } else {
578 return fmt.Errorf("expected module that implements ApiStubsProvider, e.g. droidstubs")
579 }
580}
581
Paul Duffin0f8faff2020-05-20 16:18:00 +0100582func (paths *scopePaths) treatDepAsApiStubsSrcProvider(dep android.Module, action func(provider ApiStubsSrcProvider)) error {
583 if apiStubsProvider, ok := dep.(ApiStubsSrcProvider); ok {
584 action(apiStubsProvider)
585 return nil
586 } else {
587 return fmt.Errorf("expected module that implements ApiStubsSrcProvider, e.g. droidstubs")
588 }
589}
590
Paul Duffin0ff08bd2020-04-29 13:30:54 +0100591func (paths *scopePaths) extractApiInfoFromApiStubsProvider(provider ApiStubsProvider) {
Anton Hanssond78eb762021-09-21 15:25:12 +0100592 paths.annotationsZip = android.OptionalPathForPath(provider.AnnotationsZip())
Paul Duffin0f8faff2020-05-20 16:18:00 +0100593 paths.currentApiFilePath = android.OptionalPathForPath(provider.ApiFilePath())
594 paths.removedApiFilePath = android.OptionalPathForPath(provider.RemovedApiFilePath())
Paul Duffin0ff08bd2020-04-29 13:30:54 +0100595}
596
Colin Crossdcf71b22021-02-01 13:59:03 -0800597func (paths *scopePaths) extractApiInfoFromDep(ctx android.ModuleContext, dep android.Module) error {
Paul Duffin0ff08bd2020-04-29 13:30:54 +0100598 return paths.treatDepAsApiStubsProvider(dep, func(provider ApiStubsProvider) {
599 paths.extractApiInfoFromApiStubsProvider(provider)
600 })
601}
602
Paul Duffin0f8faff2020-05-20 16:18:00 +0100603func (paths *scopePaths) extractStubsSourceInfoFromApiStubsProviders(provider ApiStubsSrcProvider) {
604 paths.stubsSrcJar = android.OptionalPathForPath(provider.StubsSrcJar())
Paul Duffin0ff08bd2020-04-29 13:30:54 +0100605}
606
Colin Crossdcf71b22021-02-01 13:59:03 -0800607func (paths *scopePaths) extractStubsSourceInfoFromDep(ctx android.ModuleContext, dep android.Module) error {
Paul Duffin0f8faff2020-05-20 16:18:00 +0100608 return paths.treatDepAsApiStubsSrcProvider(dep, func(provider ApiStubsSrcProvider) {
Paul Duffin0ff08bd2020-04-29 13:30:54 +0100609 paths.extractStubsSourceInfoFromApiStubsProviders(provider)
610 })
611}
612
Colin Crossdcf71b22021-02-01 13:59:03 -0800613func (paths *scopePaths) extractStubsSourceAndApiInfoFromApiStubsProvider(ctx android.ModuleContext, dep android.Module) error {
Paul Duffin0ff08bd2020-04-29 13:30:54 +0100614 return paths.treatDepAsApiStubsProvider(dep, func(provider ApiStubsProvider) {
615 paths.extractApiInfoFromApiStubsProvider(provider)
616 paths.extractStubsSourceInfoFromApiStubsProviders(provider)
617 })
618}
619
620type commonToSdkLibraryAndImportProperties struct {
Paul Duffin1b1e8062020-05-08 13:44:43 +0100621 // The naming scheme to use for the components that this module creates.
622 //
Paul Duffinee9ad5d2020-09-11 13:04:05 +0100623 // If not specified then it defaults to "default".
Paul Duffin1b1e8062020-05-08 13:44:43 +0100624 //
625 // This is a temporary mechanism to simplify conversion from separate modules for each
626 // component that follow a different naming pattern to the default one.
627 //
628 // TODO(b/155480189) - Remove once naming inconsistencies have been resolved.
Paul Duffin0ff08bd2020-04-29 13:30:54 +0100629 Naming_scheme *string
Paul Duffindfa131e2020-05-15 20:37:11 +0100630
631 // Specifies whether this module can be used as an Android shared library; defaults
632 // to true.
633 //
634 // An Android shared library is one that can be referenced in a <uses-library> element
635 // in an AndroidManifest.xml.
636 Shared_library *bool
Paul Duffina2ae7e02020-09-11 11:55:00 +0100637
638 // Files containing information about supported java doc tags.
639 Doctag_files []string `android:"path"`
Paul Duffin0ff08bd2020-04-29 13:30:54 +0100640}
641
Paul Duffin71b33cc2021-06-23 11:39:47 +0100642// commonSdkLibraryAndImportModule defines the interface that must be provided by a module that
643// embeds the commonToSdkLibraryAndImport struct.
644type commonSdkLibraryAndImportModule interface {
Paul Duffinb97b1572021-04-29 21:50:40 +0100645 android.SdkAware
Paul Duffin71b33cc2021-06-23 11:39:47 +0100646
647 BaseModuleName() string
648}
649
Paul Duffin56d44902020-01-31 13:36:25 +0000650// Common code between sdk library and sdk library import
651type commonToSdkLibraryAndImport struct {
Paul Duffin71b33cc2021-06-23 11:39:47 +0100652 module commonSdkLibraryAndImportModule
Paul Duffinc3091c82020-05-08 14:16:20 +0100653
Paul Duffin56d44902020-01-31 13:36:25 +0000654 scopePaths map[*apiScope]*scopePaths
Paul Duffin1b1e8062020-05-08 13:44:43 +0100655
656 namingScheme sdkLibraryComponentNamingScheme
657
Paul Duffindfa131e2020-05-15 20:37:11 +0100658 commonSdkLibraryProperties commonToSdkLibraryAndImportProperties
Paul Duffin859fe962020-05-15 10:20:31 +0100659
Paul Duffina2ae7e02020-09-11 11:55:00 +0100660 // Paths to commonSdkLibraryProperties.Doctag_files
661 doctagPaths android.Paths
662
Paul Duffin859fe962020-05-15 10:20:31 +0100663 // Functionality related to this being used as a component of a java_sdk_library.
664 EmbeddableSdkLibraryComponent
Paul Duffin56d44902020-01-31 13:36:25 +0000665}
666
Paul Duffin71b33cc2021-06-23 11:39:47 +0100667func (c *commonToSdkLibraryAndImport) initCommon(module commonSdkLibraryAndImportModule) {
668 c.module = module
Paul Duffin1b1e8062020-05-08 13:44:43 +0100669
Paul Duffin71b33cc2021-06-23 11:39:47 +0100670 module.AddProperties(&c.commonSdkLibraryProperties)
Paul Duffin859fe962020-05-15 10:20:31 +0100671
672 // Initialize this as an sdk library component.
Paul Duffin71b33cc2021-06-23 11:39:47 +0100673 c.initSdkLibraryComponent(module)
Paul Duffin1b1e8062020-05-08 13:44:43 +0100674}
675
676func (c *commonToSdkLibraryAndImport) initCommonAfterDefaultsApplied(ctx android.DefaultableHookContext) bool {
Paul Duffindfa131e2020-05-15 20:37:11 +0100677 schemeProperty := proptools.StringDefault(c.commonSdkLibraryProperties.Naming_scheme, "default")
Paul Duffin1b1e8062020-05-08 13:44:43 +0100678 switch schemeProperty {
679 case "default":
680 c.namingScheme = &defaultNamingScheme{}
681 default:
682 ctx.PropertyErrorf("naming_scheme", "expected 'default' but was %q", schemeProperty)
683 return false
684 }
685
Paul Duffin3f0290e2021-06-30 18:25:36 +0100686 namePtr := proptools.StringPtr(c.module.BaseModuleName())
687 c.sdkLibraryComponentProperties.SdkLibraryName = namePtr
688
Paul Duffindfa131e2020-05-15 20:37:11 +0100689 // Only track this sdk library if this can be used as a shared library.
690 if c.sharedLibrary() {
691 // Use the name specified in the module definition as the owner.
Paul Duffin3f0290e2021-06-30 18:25:36 +0100692 c.sdkLibraryComponentProperties.SdkLibraryToImplicitlyTrack = namePtr
Paul Duffindfa131e2020-05-15 20:37:11 +0100693 }
Paul Duffin859fe962020-05-15 10:20:31 +0100694
Paul Duffin1b1e8062020-05-08 13:44:43 +0100695 return true
Paul Duffinc3091c82020-05-08 14:16:20 +0100696}
697
Paul Duffinea8f8082021-06-24 13:25:57 +0100698// uniqueApexVariations provides common implementation of the ApexModule.UniqueApexVariations
699// method.
700func (c *commonToSdkLibraryAndImport) uniqueApexVariations() bool {
701 // A java_sdk_library that is a shared library produces an XML file that makes the shared library
702 // usable from an AndroidManifest.xml's <uses-library> entry. That XML file contains the name of
703 // the APEX and so it needs a unique variation per APEX.
704 return c.sharedLibrary()
705}
706
Paul Duffina2ae7e02020-09-11 11:55:00 +0100707func (c *commonToSdkLibraryAndImport) generateCommonBuildActions(ctx android.ModuleContext) {
708 c.doctagPaths = android.PathsForModuleSrc(ctx, c.commonSdkLibraryProperties.Doctag_files)
709}
710
Paul Duffineedc5d52020-06-12 17:46:39 +0100711// Module name of the runtime implementation library
712func (c *commonToSdkLibraryAndImport) implLibraryModuleName() string {
Paul Duffin71b33cc2021-06-23 11:39:47 +0100713 return c.module.BaseModuleName() + ".impl"
Paul Duffineedc5d52020-06-12 17:46:39 +0100714}
715
716// Module name of the XML file for the lib
717func (c *commonToSdkLibraryAndImport) xmlPermissionsModuleName() string {
Paul Duffin71b33cc2021-06-23 11:39:47 +0100718 return c.module.BaseModuleName() + sdkXmlFileSuffix
Paul Duffineedc5d52020-06-12 17:46:39 +0100719}
720
Paul Duffinc3091c82020-05-08 14:16:20 +0100721// Name of the java_library module that compiles the stubs source.
722func (c *commonToSdkLibraryAndImport) stubsLibraryModuleName(apiScope *apiScope) string {
Paul Duffinb97b1572021-04-29 21:50:40 +0100723 baseName := c.module.BaseModuleName()
724 return c.module.SdkMemberComponentName(baseName, func(name string) string {
725 return c.namingScheme.stubsLibraryModuleName(apiScope, name)
726 })
Paul Duffinc3091c82020-05-08 14:16:20 +0100727}
728
729// Name of the droidstubs module that generates the stubs source and may also
730// generate/check the API.
731func (c *commonToSdkLibraryAndImport) stubsSourceModuleName(apiScope *apiScope) string {
Paul Duffinb97b1572021-04-29 21:50:40 +0100732 baseName := c.module.BaseModuleName()
733 return c.module.SdkMemberComponentName(baseName, func(name string) string {
734 return c.namingScheme.stubsSourceModuleName(apiScope, name)
735 })
Paul Duffinc3091c82020-05-08 14:16:20 +0100736}
737
Paul Duffin46dc45a2020-05-14 15:39:10 +0100738// The component names for different outputs of the java_sdk_library.
739//
740// They are similar to the names used for the child modules it creates
741const (
742 stubsSourceComponentName = "stubs.source"
743
744 apiTxtComponentName = "api.txt"
745
746 removedApiTxtComponentName = "removed-api.txt"
Anton Hanssond78eb762021-09-21 15:25:12 +0100747
748 annotationsComponentName = "annotations.zip"
Paul Duffin46dc45a2020-05-14 15:39:10 +0100749)
750
751// A regular expression to match tags that reference a specific stubs component.
752//
753// It will only match if given a valid scope and a valid component. It is verfy strict
754// to ensure it does not accidentally match a similar looking tag that should be processed
755// by the embedded Library.
756var tagSplitter = func() *regexp.Regexp {
757 // Given a list of literal string items returns a regular expression that will
758 // match any one of the items.
759 choice := func(items ...string) string {
760 return `\Q` + strings.Join(items, `\E|\Q`) + `\E`
761 }
762
763 // Regular expression to match one of the scopes.
764 scopesRegexp := choice(allScopeNames...)
765
766 // Regular expression to match one of the components.
Anton Hanssond78eb762021-09-21 15:25:12 +0100767 componentsRegexp := choice(stubsSourceComponentName, apiTxtComponentName, removedApiTxtComponentName, annotationsComponentName)
Paul Duffin46dc45a2020-05-14 15:39:10 +0100768
769 // Regular expression to match any combination of one scope and one component.
770 return regexp.MustCompile(fmt.Sprintf(`^\.(%s)\.(%s)$`, scopesRegexp, componentsRegexp))
771}()
772
773// For OutputFileProducer interface
774//
Anton Hanssond78eb762021-09-21 15:25:12 +0100775// .<scope>.<component name>, for all ComponentNames (for example: .public.removed-api.txt)
Paul Duffin46dc45a2020-05-14 15:39:10 +0100776func (c *commonToSdkLibraryAndImport) commonOutputFiles(tag string) (android.Paths, error) {
777 if groups := tagSplitter.FindStringSubmatch(tag); groups != nil {
778 scopeName := groups[1]
779 component := groups[2]
780
781 if scope, ok := scopeByName[scopeName]; ok {
782 paths := c.findScopePaths(scope)
783 if paths == nil {
Paul Duffin71b33cc2021-06-23 11:39:47 +0100784 return nil, fmt.Errorf("%q does not provide api scope %s", c.module.BaseModuleName(), scopeName)
Paul Duffin46dc45a2020-05-14 15:39:10 +0100785 }
786
787 switch component {
788 case stubsSourceComponentName:
789 if paths.stubsSrcJar.Valid() {
790 return android.Paths{paths.stubsSrcJar.Path()}, nil
791 }
792
793 case apiTxtComponentName:
794 if paths.currentApiFilePath.Valid() {
795 return android.Paths{paths.currentApiFilePath.Path()}, nil
796 }
797
798 case removedApiTxtComponentName:
799 if paths.removedApiFilePath.Valid() {
800 return android.Paths{paths.removedApiFilePath.Path()}, nil
801 }
Anton Hanssond78eb762021-09-21 15:25:12 +0100802
803 case annotationsComponentName:
804 if paths.annotationsZip.Valid() {
805 return android.Paths{paths.annotationsZip.Path()}, nil
806 }
Paul Duffin46dc45a2020-05-14 15:39:10 +0100807 }
808
809 return nil, fmt.Errorf("%s not available for api scope %s", component, scopeName)
810 } else {
811 return nil, fmt.Errorf("unknown scope %s in %s", scope, tag)
812 }
813
814 } else {
Paul Duffina2ae7e02020-09-11 11:55:00 +0100815 switch tag {
816 case ".doctags":
817 if c.doctagPaths != nil {
818 return c.doctagPaths, nil
819 } else {
Paul Duffin71b33cc2021-06-23 11:39:47 +0100820 return nil, fmt.Errorf("no doctag_files specified on %s", c.module.BaseModuleName())
Paul Duffina2ae7e02020-09-11 11:55:00 +0100821 }
822 }
Paul Duffin46dc45a2020-05-14 15:39:10 +0100823 return nil, nil
824 }
825}
826
Paul Duffin803a9562020-05-20 11:52:25 +0100827func (c *commonToSdkLibraryAndImport) getScopePathsCreateIfNeeded(scope *apiScope) *scopePaths {
Paul Duffin56d44902020-01-31 13:36:25 +0000828 if c.scopePaths == nil {
829 c.scopePaths = make(map[*apiScope]*scopePaths)
830 }
831 paths := c.scopePaths[scope]
832 if paths == nil {
833 paths = &scopePaths{}
834 c.scopePaths[scope] = paths
835 }
836
837 return paths
838}
839
Paul Duffin803a9562020-05-20 11:52:25 +0100840func (c *commonToSdkLibraryAndImport) findScopePaths(scope *apiScope) *scopePaths {
841 if c.scopePaths == nil {
842 return nil
843 }
844
845 return c.scopePaths[scope]
846}
847
848// If this does not support the requested api scope then find the closest available
849// scope it does support. Returns nil if no such scope is available.
850func (c *commonToSdkLibraryAndImport) findClosestScopePath(scope *apiScope) *scopePaths {
851 for s := scope; s != nil; s = s.extends {
852 if paths := c.findScopePaths(s); paths != nil {
853 return paths
854 }
855 }
856
857 // This should never happen outside tests as public should be the base scope for every
858 // scope and is enabled by default.
859 return nil
860}
861
Jiyong Parkf1691d22021-03-29 20:11:58 +0900862func (c *commonToSdkLibraryAndImport) selectHeaderJarsForSdkVersion(ctx android.BaseModuleContext, sdkVersion android.SdkSpec) android.Paths {
Paul Duffinb05d4292020-05-20 12:19:10 +0100863
864 // If a specific numeric version has been requested then use prebuilt versions of the sdk.
Jiyong Park54105c42021-03-31 18:17:53 +0900865 if !sdkVersion.ApiLevel.IsPreview() {
Paul Duffin71b33cc2021-06-23 11:39:47 +0100866 return PrebuiltJars(ctx, c.module.BaseModuleName(), sdkVersion)
Paul Duffinb05d4292020-05-20 12:19:10 +0100867 }
868
Paul Duffin1267d872021-04-16 17:21:36 +0100869 paths := c.selectScopePaths(ctx, sdkVersion.Kind)
870 if paths == nil {
871 return nil
872 }
873
874 return paths.stubsHeaderPath
875}
876
877// selectScopePaths returns the *scopePaths appropriate for the specific kind.
878//
879// If the module does not support the specific kind then it will return the *scopePaths for the
880// closest kind which is a subset of the requested kind. e.g. if requesting android.SdkModule then
881// it will return *scopePaths for android.SdkSystem if available or android.SdkPublic of not.
882func (c *commonToSdkLibraryAndImport) selectScopePaths(ctx android.BaseModuleContext, kind android.SdkKind) *scopePaths {
Paul Duffin32cf58a2021-05-18 16:32:50 +0100883 apiScope := sdkKindToApiScope(kind)
Paul Duffinb05d4292020-05-20 12:19:10 +0100884
Paul Duffin803a9562020-05-20 11:52:25 +0100885 paths := c.findClosestScopePath(apiScope)
886 if paths == nil {
887 var scopes []string
888 for _, s := range allApiScopes {
889 if c.findScopePaths(s) != nil {
890 scopes = append(scopes, s.name)
891 }
892 }
Paul Duffin71b33cc2021-06-23 11:39:47 +0100893 ctx.ModuleErrorf("requires api scope %s from %s but it only has %q available", apiScope.name, c.module.BaseModuleName(), scopes)
Paul Duffin803a9562020-05-20 11:52:25 +0100894 return nil
895 }
896
Paul Duffin1267d872021-04-16 17:21:36 +0100897 return paths
898}
899
Paul Duffin32cf58a2021-05-18 16:32:50 +0100900// sdkKindToApiScope maps from android.SdkKind to apiScope.
901func sdkKindToApiScope(kind android.SdkKind) *apiScope {
902 var apiScope *apiScope
903 switch kind {
904 case android.SdkSystem:
905 apiScope = apiScopeSystem
906 case android.SdkModule:
907 apiScope = apiScopeModuleLib
908 case android.SdkTest:
909 apiScope = apiScopeTest
910 case android.SdkSystemServer:
911 apiScope = apiScopeSystemServer
912 default:
913 apiScope = apiScopePublic
914 }
915 return apiScope
916}
917
Paul Duffin1267d872021-04-16 17:21:36 +0100918// to satisfy SdkLibraryDependency interface
Martin Stjernholm8be1e6d2021-09-15 03:34:04 +0100919func (c *commonToSdkLibraryAndImport) SdkApiStubDexJar(ctx android.BaseModuleContext, kind android.SdkKind) OptionalDexJarPath {
Paul Duffin1267d872021-04-16 17:21:36 +0100920 paths := c.selectScopePaths(ctx, kind)
921 if paths == nil {
Martin Stjernholm8be1e6d2021-09-15 03:34:04 +0100922 return makeUnsetDexJarPath()
Paul Duffin1267d872021-04-16 17:21:36 +0100923 }
924
925 return paths.stubsDexJarPath
Paul Duffinb05d4292020-05-20 12:19:10 +0100926}
927
Paul Duffin32cf58a2021-05-18 16:32:50 +0100928// to satisfy SdkLibraryDependency interface
929func (c *commonToSdkLibraryAndImport) SdkRemovedTxtFile(ctx android.BaseModuleContext, kind android.SdkKind) android.OptionalPath {
930 apiScope := sdkKindToApiScope(kind)
931 paths := c.findScopePaths(apiScope)
932 if paths == nil {
933 return android.OptionalPath{}
934 }
935
936 return paths.removedApiFilePath
937}
938
Paul Duffin859fe962020-05-15 10:20:31 +0100939func (c *commonToSdkLibraryAndImport) sdkComponentPropertiesForChildLibrary() interface{} {
940 componentProps := &struct {
Paul Duffin3f0290e2021-06-30 18:25:36 +0100941 SdkLibraryName *string
Paul Duffin859fe962020-05-15 10:20:31 +0100942 SdkLibraryToImplicitlyTrack *string
Paul Duffindfa131e2020-05-15 20:37:11 +0100943 }{}
944
Paul Duffin3f0290e2021-06-30 18:25:36 +0100945 namePtr := proptools.StringPtr(c.module.BaseModuleName())
946 componentProps.SdkLibraryName = namePtr
947
Paul Duffindfa131e2020-05-15 20:37:11 +0100948 if c.sharedLibrary() {
Paul Duffin859fe962020-05-15 10:20:31 +0100949 // Mark the stubs library as being components of this java_sdk_library so that
950 // any app that includes code which depends (directly or indirectly) on the stubs
951 // library will have the appropriate <uses-library> invocation inserted into its
952 // manifest if necessary.
Paul Duffin3f0290e2021-06-30 18:25:36 +0100953 componentProps.SdkLibraryToImplicitlyTrack = namePtr
Paul Duffin859fe962020-05-15 10:20:31 +0100954 }
955
956 return componentProps
957}
958
Paul Duffindfa131e2020-05-15 20:37:11 +0100959func (c *commonToSdkLibraryAndImport) sharedLibrary() bool {
960 return proptools.BoolDefault(c.commonSdkLibraryProperties.Shared_library, true)
961}
962
Paul Duffinf4600f62021-05-13 22:34:45 +0100963// Check if the stub libraries should be compiled for dex
964func (c *commonToSdkLibraryAndImport) stubLibrariesCompiledForDex() bool {
965 // Always compile the dex file files for the stub libraries if they will be used on the
966 // bootclasspath.
967 return !c.sharedLibrary()
968}
969
Paul Duffin859fe962020-05-15 10:20:31 +0100970// Properties related to the use of a module as an component of a java_sdk_library.
971type SdkLibraryComponentProperties struct {
Paul Duffin3f0290e2021-06-30 18:25:36 +0100972 // The name of the java_sdk_library/_import module.
973 SdkLibraryName *string `blueprint:"mutated"`
Paul Duffin859fe962020-05-15 10:20:31 +0100974
975 // The name of the java_sdk_library/_import to add to a <uses-library> entry
976 // in the AndroidManifest.xml of any Android app that includes code that references
977 // this module. If not set then no java_sdk_library/_import is tracked.
978 SdkLibraryToImplicitlyTrack *string `blueprint:"mutated"`
979}
980
981// Structure to be embedded in a module struct that needs to support the
982// SdkLibraryComponentDependency interface.
983type EmbeddableSdkLibraryComponent struct {
984 sdkLibraryComponentProperties SdkLibraryComponentProperties
985}
986
Paul Duffin71b33cc2021-06-23 11:39:47 +0100987func (e *EmbeddableSdkLibraryComponent) initSdkLibraryComponent(module android.Module) {
988 module.AddProperties(&e.sdkLibraryComponentProperties)
Paul Duffin859fe962020-05-15 10:20:31 +0100989}
990
991// to satisfy SdkLibraryComponentDependency
Paul Duffin3f0290e2021-06-30 18:25:36 +0100992func (e *EmbeddableSdkLibraryComponent) SdkLibraryName() *string {
993 return e.sdkLibraryComponentProperties.SdkLibraryName
994}
995
996// to satisfy SdkLibraryComponentDependency
Ulya Trafimovich39b437b2020-09-23 16:42:35 +0100997func (e *EmbeddableSdkLibraryComponent) OptionalSdkLibraryImplementation() *string {
Ulya Trafimovich78645fb2021-07-16 15:29:25 +0100998 // For shared libraries, this is the same as the SDK library name. If a Java library or app
999 // depends on a component library (e.g. a stub library) it still needs to know the name of the
1000 // run-time library and the corresponding module that provides the implementation. This name is
1001 // passed to manifest_fixer (to be added to AndroidManifest.xml) and added to CLC (to be used
1002 // in dexpreopt).
1003 //
1004 // For non-shared SDK (component or not) libraries this returns `nil`, as they are not
1005 // <uses-library> and should not be added to the manifest or to CLC.
Ulya Trafimovich39b437b2020-09-23 16:42:35 +01001006 return e.sdkLibraryComponentProperties.SdkLibraryToImplicitlyTrack
1007}
1008
Paul Duffin859fe962020-05-15 10:20:31 +01001009// Implemented by modules that are (or possibly could be) a component of a java_sdk_library
1010// (including the java_sdk_library) itself.
1011type SdkLibraryComponentDependency interface {
Ulya Trafimovich31e444e2020-08-14 17:32:16 +01001012 UsesLibraryDependency
1013
Paul Duffin3f0290e2021-06-30 18:25:36 +01001014 // SdkLibraryName returns the name of the java_sdk_library/_import module.
1015 SdkLibraryName() *string
1016
Ulya Trafimovich39b437b2020-09-23 16:42:35 +01001017 // The name of the implementation library for the optional SDK library or nil, if there isn't one.
1018 OptionalSdkLibraryImplementation() *string
Paul Duffin859fe962020-05-15 10:20:31 +01001019}
1020
1021// Make sure that all the module types that are components of java_sdk_library/_import
1022// and which can be referenced (directly or indirectly) from an android app implement
1023// the SdkLibraryComponentDependency interface.
1024var _ SdkLibraryComponentDependency = (*Library)(nil)
1025var _ SdkLibraryComponentDependency = (*Import)(nil)
1026var _ SdkLibraryComponentDependency = (*SdkLibrary)(nil)
Paul Duffineedc5d52020-06-12 17:46:39 +01001027var _ SdkLibraryComponentDependency = (*SdkLibraryImport)(nil)
Paul Duffin859fe962020-05-15 10:20:31 +01001028
Paul Duffin32cf58a2021-05-18 16:32:50 +01001029// Provides access to sdk_version related files, e.g. header and implementation jars.
Paul Duffin859fe962020-05-15 10:20:31 +01001030type SdkLibraryDependency interface {
1031 SdkLibraryComponentDependency
1032
1033 // Get the header jars appropriate for the supplied sdk_version.
1034 //
1035 // These are turbine generated jars so they only change if the externals of the
1036 // class changes but it does not contain and implementation or JavaDoc.
Jiyong Parkf1691d22021-03-29 20:11:58 +09001037 SdkHeaderJars(ctx android.BaseModuleContext, sdkVersion android.SdkSpec) android.Paths
Paul Duffin859fe962020-05-15 10:20:31 +01001038
1039 // Get the implementation jars appropriate for the supplied sdk version.
1040 //
1041 // These are either the implementation jar for the whole sdk library or the implementation
1042 // jars for the stubs. The latter should only be needed when generating JavaDoc as otherwise
1043 // they are identical to the corresponding header jars.
Jiyong Parkf1691d22021-03-29 20:11:58 +09001044 SdkImplementationJars(ctx android.BaseModuleContext, sdkVersion android.SdkSpec) android.Paths
Paul Duffin1267d872021-04-16 17:21:36 +01001045
1046 // SdkApiStubDexJar returns the dex jar for the stubs. It is needed by the hiddenapi processing
1047 // tool which processes dex files.
Martin Stjernholm8be1e6d2021-09-15 03:34:04 +01001048 SdkApiStubDexJar(ctx android.BaseModuleContext, kind android.SdkKind) OptionalDexJarPath
Paul Duffinf4600f62021-05-13 22:34:45 +01001049
Paul Duffin32cf58a2021-05-18 16:32:50 +01001050 // SdkRemovedTxtFile returns the optional path to the removed.txt file for the specified sdk kind.
1051 SdkRemovedTxtFile(ctx android.BaseModuleContext, kind android.SdkKind) android.OptionalPath
1052
Paul Duffinf4600f62021-05-13 22:34:45 +01001053 // sharedLibrary returns true if this can be used as a shared library.
1054 sharedLibrary() bool
Paul Duffin859fe962020-05-15 10:20:31 +01001055}
1056
Inseob Kimc0907f12019-02-08 21:00:45 +09001057type SdkLibrary struct {
Sundong Ahn054b19a2018-10-19 13:46:09 +09001058 Library
Jiyong Parkc678ad32018-04-10 13:07:10 +09001059
Sundong Ahn054b19a2018-10-19 13:46:09 +09001060 sdkLibraryProperties sdkLibraryProperties
Jiyong Parkc678ad32018-04-10 13:07:10 +09001061
Paul Duffin3375e352020-04-28 10:44:03 +01001062 // Map from api scope to the scope specific property structure.
1063 scopeToProperties map[*apiScope]*ApiScopeProperties
1064
Paul Duffin56d44902020-01-31 13:36:25 +00001065 commonToSdkLibraryAndImport
Jiyong Parkc678ad32018-04-10 13:07:10 +09001066}
1067
Inseob Kimc0907f12019-02-08 21:00:45 +09001068var _ SdkLibraryDependency = (*SdkLibrary)(nil)
Colin Cross897d2ed2019-02-11 14:03:51 -08001069
Paul Duffin3375e352020-04-28 10:44:03 +01001070func (module *SdkLibrary) generateTestAndSystemScopesByDefault() bool {
1071 return module.sdkLibraryProperties.Generate_system_and_test_apis
1072}
1073
1074func (module *SdkLibrary) getGeneratedApiScopes(ctx android.EarlyModuleContext) apiScopes {
1075 // Check to see if any scopes have been explicitly enabled. If any have then all
1076 // must be.
1077 anyScopesExplicitlyEnabled := false
1078 for _, scope := range allApiScopes {
1079 scopeProperties := module.scopeToProperties[scope]
1080 if scopeProperties.Enabled != nil {
1081 anyScopesExplicitlyEnabled = true
1082 break
1083 }
Paul Duffind1b3a922020-01-22 11:57:20 +00001084 }
Paul Duffin3375e352020-04-28 10:44:03 +01001085
1086 var generatedScopes apiScopes
1087 enabledScopes := make(map[*apiScope]struct{})
1088 for _, scope := range allApiScopes {
1089 scopeProperties := module.scopeToProperties[scope]
1090 // If any scopes are explicitly enabled then ignore the legacy enabled status.
1091 // This is to ensure that any new usages of this module type do not rely on legacy
1092 // behaviour.
1093 defaultEnabledStatus := false
1094 if anyScopesExplicitlyEnabled {
1095 defaultEnabledStatus = scope.defaultEnabledStatus
1096 } else {
1097 defaultEnabledStatus = scope.legacyEnabledStatus(module)
1098 }
1099 enabled := proptools.BoolDefault(scopeProperties.Enabled, defaultEnabledStatus)
1100 if enabled {
1101 enabledScopes[scope] = struct{}{}
1102 generatedScopes = append(generatedScopes, scope)
1103 }
1104 }
1105
1106 // Now check to make sure that any scope that is extended by an enabled scope is also
1107 // enabled.
1108 for _, scope := range allApiScopes {
1109 if _, ok := enabledScopes[scope]; ok {
1110 extends := scope.extends
1111 if extends != nil {
1112 if _, ok := enabledScopes[extends]; !ok {
1113 ctx.ModuleErrorf("enabled api scope %q depends on disabled scope %q", scope, extends)
1114 }
1115 }
1116 }
1117 }
1118
1119 return generatedScopes
Paul Duffind1b3a922020-01-22 11:57:20 +00001120}
1121
Paul Duffineedc5d52020-06-12 17:46:39 +01001122type sdkLibraryComponentTag struct {
1123 blueprint.BaseDependencyTag
1124 name string
1125}
1126
1127// Mark this tag so dependencies that use it are excluded from visibility enforcement.
1128func (t sdkLibraryComponentTag) ExcludeFromVisibilityEnforcement() {}
1129
1130var xmlPermissionsFileTag = sdkLibraryComponentTag{name: "xml-permissions-file"}
Paul Duffine74ac732020-02-06 13:51:46 +00001131
Jiyong Parke3833882020-02-17 17:28:10 +09001132func IsXmlPermissionsFileDepTag(depTag blueprint.DependencyTag) bool {
Paul Duffineedc5d52020-06-12 17:46:39 +01001133 if dt, ok := depTag.(sdkLibraryComponentTag); ok {
Jiyong Parke3833882020-02-17 17:28:10 +09001134 return dt == xmlPermissionsFileTag
1135 }
1136 return false
1137}
1138
Paul Duffineedc5d52020-06-12 17:46:39 +01001139var implLibraryTag = sdkLibraryComponentTag{name: "impl-library"}
Paul Duffin5df79302020-05-16 15:52:12 +01001140
Paul Duffin44f1d842020-06-26 20:17:02 +01001141// Add the dependencies on the child modules in the component deps mutator.
1142func (module *SdkLibrary) ComponentDepsMutator(ctx android.BottomUpMutatorContext) {
Paul Duffin3375e352020-04-28 10:44:03 +01001143 for _, apiScope := range module.getGeneratedApiScopes(ctx) {
Paul Duffind1b3a922020-01-22 11:57:20 +00001144 // Add dependencies to the stubs library
Paul Duffinc3091c82020-05-08 14:16:20 +01001145 ctx.AddVariationDependencies(nil, apiScope.stubsTag, module.stubsLibraryModuleName(apiScope))
Paul Duffind1b3a922020-01-22 11:57:20 +00001146
Paul Duffin15f34ef2020-07-20 18:04:44 +01001147 // Add a dependency on the stubs source in order to access both stubs source and api information.
1148 ctx.AddVariationDependencies(nil, apiScope.stubsSourceAndApiTag, module.stubsSourceModuleName(apiScope))
Sundong Ahn054b19a2018-10-19 13:46:09 +09001149 }
1150
Paul Duffindfa131e2020-05-15 20:37:11 +01001151 if module.requiresRuntimeImplementationLibrary() {
Paul Duffin5df79302020-05-16 15:52:12 +01001152 // Add dependency to the rule for generating the implementation library.
1153 ctx.AddDependency(module, implLibraryTag, module.implLibraryModuleName())
1154
Paul Duffindfa131e2020-05-15 20:37:11 +01001155 if module.sharedLibrary() {
1156 // Add dependency to the rule for generating the xml permissions file
Paul Duffineedc5d52020-06-12 17:46:39 +01001157 ctx.AddDependency(module, xmlPermissionsFileTag, module.xmlPermissionsModuleName())
Paul Duffindfa131e2020-05-15 20:37:11 +01001158 }
Paul Duffin44f1d842020-06-26 20:17:02 +01001159 }
1160}
Paul Duffine74ac732020-02-06 13:51:46 +00001161
Paul Duffin44f1d842020-06-26 20:17:02 +01001162// Add other dependencies as normal.
1163func (module *SdkLibrary) DepsMutator(ctx android.BottomUpMutatorContext) {
Anton Hanssone77fccc2021-01-20 16:52:41 +00001164 var missingApiModules []string
1165 for _, apiScope := range module.getGeneratedApiScopes(ctx) {
1166 if apiScope.unstable {
1167 continue
1168 }
1169 if m := android.SrcIsModule(module.latestApiFilegroupName(apiScope)); !ctx.OtherModuleExists(m) {
1170 missingApiModules = append(missingApiModules, m)
1171 }
1172 if m := android.SrcIsModule(module.latestRemovedApiFilegroupName(apiScope)); !ctx.OtherModuleExists(m) {
1173 missingApiModules = append(missingApiModules, m)
1174 }
Jaewoong Jung1a97ee02021-03-09 13:25:02 -08001175 if m := android.SrcIsModule(module.latestIncompatibilitiesFilegroupName(apiScope)); !ctx.OtherModuleExists(m) {
1176 missingApiModules = append(missingApiModules, m)
1177 }
Anton Hanssone77fccc2021-01-20 16:52:41 +00001178 }
1179 if len(missingApiModules) != 0 && !module.sdkLibraryProperties.Unsafe_ignore_missing_latest_api {
1180 m := module.Name() + " is missing tracking files for previously released library versions.\n"
1181 m += "You need to do one of the following:\n"
1182 m += "- Add `unsafe_ignore_missing_latest_api: true` to your blueprint (to disable compat tracking)\n"
1183 m += "- Add a set of prebuilt txt files representing the last released version of this library for compat checking.\n"
1184 m += " (the current set of API files can be used as a seed for this compatibility tracking\n"
1185 m += "\n"
1186 m += "The following filegroup modules are missing:\n "
1187 m += strings.Join(missingApiModules, "\n ") + "\n"
1188 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."
1189 ctx.ModuleErrorf(m)
1190 }
Paul Duffin44f1d842020-06-26 20:17:02 +01001191 if module.requiresRuntimeImplementationLibrary() {
Paul Duffindfa131e2020-05-15 20:37:11 +01001192 // Only add the deps for the library if it is actually going to be built.
1193 module.Library.deps(ctx)
1194 }
Jiyong Parkc678ad32018-04-10 13:07:10 +09001195}
1196
Paul Duffin46dc45a2020-05-14 15:39:10 +01001197func (module *SdkLibrary) OutputFiles(tag string) (android.Paths, error) {
1198 paths, err := module.commonOutputFiles(tag)
1199 if paths == nil && err == nil {
1200 return module.Library.OutputFiles(tag)
1201 } else {
1202 return paths, err
1203 }
1204}
1205
Inseob Kimc0907f12019-02-08 21:00:45 +09001206func (module *SdkLibrary) GenerateAndroidBuildActions(ctx android.ModuleContext) {
Paul Duffina2ae7e02020-09-11 11:55:00 +01001207 module.generateCommonBuildActions(ctx)
1208
Paul Duffindfa131e2020-05-15 20:37:11 +01001209 // Only build an implementation library if required.
1210 if module.requiresRuntimeImplementationLibrary() {
Paul Duffin43db9be2019-12-30 17:35:49 +00001211 module.Library.GenerateAndroidBuildActions(ctx)
1212 }
Sundong Ahn054b19a2018-10-19 13:46:09 +09001213
Paul Duffinb97b1572021-04-29 21:50:40 +01001214 // Collate the components exported by this module. All scope specific modules are exported but
1215 // the impl and xml component modules are not.
1216 exportedComponents := map[string]struct{}{}
1217
Sundong Ahn57368eb2018-07-06 11:20:23 +09001218 // Record the paths to the header jars of the library (stubs and impl).
Paul Duffind1b3a922020-01-22 11:57:20 +00001219 // When this java_sdk_library is depended upon from others via "libs" property,
Jiyong Parkc678ad32018-04-10 13:07:10 +09001220 // the recorded paths will be returned depending on the link type of the caller.
1221 ctx.VisitDirectDeps(func(to android.Module) {
Jiyong Parkc678ad32018-04-10 13:07:10 +09001222 tag := ctx.OtherModuleDependencyTag(to)
1223
Paul Duffinc8782502020-04-29 20:45:27 +01001224 // Extract information from any of the scope specific dependencies.
1225 if scopeTag, ok := tag.(scopeDependencyTag); ok {
1226 apiScope := scopeTag.apiScope
Paul Duffin803a9562020-05-20 11:52:25 +01001227 scopePaths := module.getScopePathsCreateIfNeeded(apiScope)
Paul Duffinc8782502020-04-29 20:45:27 +01001228
1229 // Extract information from the dependency. The exact information extracted
1230 // is determined by the nature of the dependency which is determined by the tag.
1231 scopeTag.extractDepInfo(ctx, to, scopePaths)
Paul Duffinb97b1572021-04-29 21:50:40 +01001232
1233 exportedComponents[ctx.OtherModuleName(to)] = struct{}{}
Sundong Ahn20e998b2018-07-24 11:19:26 +09001234 }
Jiyong Parkc678ad32018-04-10 13:07:10 +09001235 })
Paul Duffinb97b1572021-04-29 21:50:40 +01001236
1237 // Make the set of components exported by this module available for use elsewhere.
1238 exportedComponentInfo := android.ExportedComponentsInfo{Components: android.SortedStringKeys(exportedComponents)}
1239 ctx.SetProvider(android.ExportedComponentsInfoProvider, exportedComponentInfo)
Jiyong Parkc678ad32018-04-10 13:07:10 +09001240}
1241
Jiyong Park0b0e1b92019-12-03 13:24:29 +09001242func (module *SdkLibrary) AndroidMkEntries() []android.AndroidMkEntries {
Paul Duffindfa131e2020-05-15 20:37:11 +01001243 if !module.requiresRuntimeImplementationLibrary() {
Paul Duffin43db9be2019-12-30 17:35:49 +00001244 return nil
1245 }
Jiyong Park0b0e1b92019-12-03 13:24:29 +09001246 entriesList := module.Library.AndroidMkEntries()
Yo Chiang07d75072020-06-05 17:43:19 +08001247 if module.sharedLibrary() {
1248 entries := &entriesList[0]
1249 entries.Required = append(entries.Required, module.xmlPermissionsModuleName())
1250 }
Jiyong Park0b0e1b92019-12-03 13:24:29 +09001251 return entriesList
Jiyong Park82484c02018-04-23 21:41:26 +09001252}
1253
Anton Hansson5fd5d242020-03-27 19:43:19 +00001254// The dist path of the stub artifacts
1255func (module *SdkLibrary) apiDistPath(apiScope *apiScope) string {
Colin Crossf0eace92021-06-02 13:02:23 -07001256 return path.Join("apistubs", module.distGroup(), apiScope.name)
Anton Hansson5fd5d242020-03-27 19:43:19 +00001257}
1258
Paul Duffin12ceb462019-12-24 20:31:31 +00001259// Get the sdk version for use when compiling the stubs library.
Paul Duffin780c5f42020-05-12 15:52:55 +01001260func (module *SdkLibrary) sdkVersionForStubsLibrary(mctx android.EarlyModuleContext, apiScope *apiScope) string {
Paul Duffin87a05a32020-05-12 11:50:28 +01001261 scopeProperties := module.scopeToProperties[apiScope]
1262 if scopeProperties.Sdk_version != nil {
1263 return proptools.String(scopeProperties.Sdk_version)
1264 }
1265
Jiyong Parkf1691d22021-03-29 20:11:58 +09001266 sdkDep := decodeSdkDep(mctx, android.SdkContext(&module.Library))
Paul Duffin12ceb462019-12-24 20:31:31 +00001267 if sdkDep.hasStandardLibs() {
1268 // If building against a standard sdk then use the sdk version appropriate for the scope.
Paul Duffind1b3a922020-01-22 11:57:20 +00001269 return apiScope.sdkVersion
Paul Duffin12ceb462019-12-24 20:31:31 +00001270 } else {
1271 // Otherwise, use no system module.
1272 return "none"
1273 }
1274}
1275
Paul Duffin31310252020-11-20 21:26:20 +00001276func (module *SdkLibrary) distStem() string {
1277 return proptools.StringDefault(module.sdkLibraryProperties.Dist_stem, module.BaseModuleName())
1278}
1279
Colin Cross986b69a2021-06-01 13:13:40 -07001280// distGroup returns the subdirectory of the dist path of the stub artifacts.
1281func (module *SdkLibrary) distGroup() string {
Colin Cross59b92bf2021-06-01 14:07:56 -07001282 return proptools.StringDefault(module.sdkLibraryProperties.Dist_group, "unknown")
Colin Cross986b69a2021-06-01 13:13:40 -07001283}
1284
Paul Duffind1b3a922020-01-22 11:57:20 +00001285func (module *SdkLibrary) latestApiFilegroupName(apiScope *apiScope) string {
Paul Duffin31310252020-11-20 21:26:20 +00001286 return ":" + module.distStem() + ".api." + apiScope.name + ".latest"
Jiyong Park58c518b2018-05-12 22:29:12 +09001287}
Jiyong Parkc678ad32018-04-10 13:07:10 +09001288
Paul Duffind1b3a922020-01-22 11:57:20 +00001289func (module *SdkLibrary) latestRemovedApiFilegroupName(apiScope *apiScope) string {
Paul Duffin31310252020-11-20 21:26:20 +00001290 return ":" + module.distStem() + "-removed.api." + apiScope.name + ".latest"
Jiyong Parkc678ad32018-04-10 13:07:10 +09001291}
1292
Jaewoong Jung1a97ee02021-03-09 13:25:02 -08001293func (module *SdkLibrary) latestIncompatibilitiesFilegroupName(apiScope *apiScope) string {
1294 return ":" + module.distStem() + "-incompatibilities.api." + apiScope.name + ".latest"
1295}
1296
Anton Hansson944e77d2020-08-19 11:40:22 +01001297func childModuleVisibility(childVisibility []string) []string {
1298 if childVisibility == nil {
1299 // No child visibility set. The child will use the visibility of the sdk_library.
1300 return nil
1301 }
1302
1303 // Prepend an override to ignore the sdk_library's visibility, and rely on the child visibility.
1304 var visibility []string
1305 visibility = append(visibility, "//visibility:override")
1306 visibility = append(visibility, childVisibility...)
1307 return visibility
1308}
1309
Paul Duffin5df79302020-05-16 15:52:12 +01001310// Creates the implementation java library
1311func (module *SdkLibrary) createImplLibrary(mctx android.DefaultableHookContext) {
Anton Hansson944e77d2020-08-19 11:40:22 +01001312 visibility := childModuleVisibility(module.sdkLibraryProperties.Impl_library_visibility)
1313
Paul Duffin5df79302020-05-16 15:52:12 +01001314 props := struct {
Paul Duffin66cdbf02021-05-14 16:35:06 +01001315 Name *string
1316 Visibility []string
1317 Instrument bool
1318 Libs []string
Paul Duffin5df79302020-05-16 15:52:12 +01001319 }{
1320 Name: proptools.StringPtr(module.implLibraryModuleName()),
Anton Hansson944e77d2020-08-19 11:40:22 +01001321 Visibility: visibility,
Paul Duffin296cf332020-06-18 21:09:55 +01001322 // Set the instrument property to ensure it is instrumented when instrumentation is required.
1323 Instrument: true,
Anton Hansson7f66efa2020-10-08 14:47:23 +01001324 // Set the impl_only libs. Note that the module's "Libs" get appended as well, via the
1325 // addition of &module.properties below.
1326 Libs: module.sdkLibraryProperties.Impl_only_libs,
Paul Duffin5df79302020-05-16 15:52:12 +01001327 }
1328
1329 properties := []interface{}{
1330 &module.properties,
1331 &module.protoProperties,
1332 &module.deviceProperties,
Liz Kammera7a64f32020-07-09 15:16:41 -07001333 &module.dexProperties,
Paul Duffin5df79302020-05-16 15:52:12 +01001334 &module.dexpreoptProperties,
Colin Cross014489c2020-06-02 20:09:13 -07001335 &module.linter.properties,
Paul Duffin5df79302020-05-16 15:52:12 +01001336 &props,
1337 module.sdkComponentPropertiesForChildLibrary(),
1338 }
1339 mctx.CreateModule(LibraryFactory, properties...)
1340}
1341
Jiyong Parkc678ad32018-04-10 13:07:10 +09001342// Creates a static java library that has API stubs
Paul Duffinf0229202020-04-29 16:47:28 +01001343func (module *SdkLibrary) createStubsLibrary(mctx android.DefaultableHookContext, apiScope *apiScope) {
Jiyong Parkc678ad32018-04-10 13:07:10 +09001344 props := struct {
Dan Willemsen9f435972020-05-28 15:28:00 -07001345 Name *string
1346 Visibility []string
1347 Srcs []string
1348 Installable *bool
1349 Sdk_version *string
1350 System_modules *string
1351 Patch_module *string
1352 Libs []string
Anton Hanssondae54cd2021-04-21 16:30:10 +01001353 Static_libs []string
Dan Willemsen9f435972020-05-28 15:28:00 -07001354 Compile_dex *bool
1355 Java_version *string
1356 Openjdk9 struct {
Sundong Ahn054b19a2018-10-19 13:46:09 +09001357 Srcs []string
1358 Javacflags []string
1359 }
Anton Hansson5fd5d242020-03-27 19:43:19 +00001360 Dist struct {
1361 Targets []string
1362 Dest *string
1363 Dir *string
1364 Tag *string
1365 }
Jiyong Parkc678ad32018-04-10 13:07:10 +09001366 }{}
1367
Paul Duffinc3091c82020-05-08 14:16:20 +01001368 props.Name = proptools.StringPtr(module.stubsLibraryModuleName(apiScope))
Anton Hansson944e77d2020-08-19 11:40:22 +01001369 props.Visibility = childModuleVisibility(module.sdkLibraryProperties.Stubs_library_visibility)
Jiyong Parkc678ad32018-04-10 13:07:10 +09001370 // sources are generated from the droiddoc
Paul Duffinc3091c82020-05-08 14:16:20 +01001371 props.Srcs = []string{":" + module.stubsSourceModuleName(apiScope)}
Paul Duffin12ceb462019-12-24 20:31:31 +00001372 sdkVersion := module.sdkVersionForStubsLibrary(mctx, apiScope)
Paul Duffin52d398a2019-06-11 12:31:14 +01001373 props.Sdk_version = proptools.StringPtr(sdkVersion)
Paul Duffina18abc22020-05-16 18:54:24 +01001374 props.System_modules = module.deviceProperties.System_modules
1375 props.Patch_module = module.properties.Patch_module
Paul Duffin367ab912019-12-23 19:40:36 +00001376 props.Installable = proptools.BoolPtr(false)
Sundong Ahn054b19a2018-10-19 13:46:09 +09001377 props.Libs = module.sdkLibraryProperties.Stub_only_libs
Anton Hanssondae54cd2021-04-21 16:30:10 +01001378 props.Static_libs = module.sdkLibraryProperties.Stub_only_static_libs
Paul Duffine22c2ab2020-05-20 19:35:27 +01001379 // The stub-annotations library contains special versions of the annotations
1380 // with CLASS retention policy, so that they're kept.
1381 if proptools.Bool(module.sdkLibraryProperties.Annotations_enabled) {
1382 props.Libs = append(props.Libs, "stub-annotations")
1383 }
Paul Duffina18abc22020-05-16 18:54:24 +01001384 props.Openjdk9.Srcs = module.properties.Openjdk9.Srcs
1385 props.Openjdk9.Javacflags = module.properties.Openjdk9.Javacflags
Anton Hansson83509b52020-05-21 09:21:57 +01001386 // We compile the stubs for 1.8 in line with the main android.jar stubs, and potential
1387 // interop with older developer tools that don't support 1.9.
1388 props.Java_version = proptools.StringPtr("1.8")
Paul Duffinf4600f62021-05-13 22:34:45 +01001389
1390 // The imports need to be compiled to dex if the java_sdk_library requests it.
1391 compileDex := module.dexProperties.Compile_dex
1392 if module.stubLibrariesCompiledForDex() {
1393 compileDex = proptools.BoolPtr(true)
Sundong Ahndd567f92018-07-31 17:19:11 +09001394 }
Paul Duffinf4600f62021-05-13 22:34:45 +01001395 props.Compile_dex = compileDex
Jiyong Parkc678ad32018-04-10 13:07:10 +09001396
Anton Hansson5fd5d242020-03-27 19:43:19 +00001397 // Dist the class jar artifact for sdk builds.
1398 if !Bool(module.sdkLibraryProperties.No_dist) {
1399 props.Dist.Targets = []string{"sdk", "win_sdk"}
Paul Duffin31310252020-11-20 21:26:20 +00001400 props.Dist.Dest = proptools.StringPtr(fmt.Sprintf("%v.jar", module.distStem()))
Anton Hansson5fd5d242020-03-27 19:43:19 +00001401 props.Dist.Dir = proptools.StringPtr(module.apiDistPath(apiScope))
1402 props.Dist.Tag = proptools.StringPtr(".jar")
1403 }
Jiyong Parkc678ad32018-04-10 13:07:10 +09001404
Paul Duffin859fe962020-05-15 10:20:31 +01001405 mctx.CreateModule(LibraryFactory, &props, module.sdkComponentPropertiesForChildLibrary())
Jiyong Parkc678ad32018-04-10 13:07:10 +09001406}
1407
Paul Duffin6d0886e2020-04-07 18:49:53 +01001408// Creates a droidstubs module that creates stubs source files from the given full source
Paul Duffinc8782502020-04-29 20:45:27 +01001409// files and also updates and checks the API specification files.
Paul Duffin15f34ef2020-07-20 18:04:44 +01001410func (module *SdkLibrary) createStubsSourcesAndApi(mctx android.DefaultableHookContext, apiScope *apiScope, name string, scopeSpecificDroidstubsArgs []string) {
Jiyong Parkc678ad32018-04-10 13:07:10 +09001411 props := struct {
Sundong Ahn054b19a2018-10-19 13:46:09 +09001412 Name *string
Paul Duffin4911a892020-04-29 23:35:13 +01001413 Visibility []string
Sundong Ahn054b19a2018-10-19 13:46:09 +09001414 Srcs []string
1415 Installable *bool
Paul Duffin52d398a2019-06-11 12:31:14 +01001416 Sdk_version *string
Paul Duffin12ceb462019-12-24 20:31:31 +00001417 System_modules *string
Sundong Ahn054b19a2018-10-19 13:46:09 +09001418 Libs []string
Paul Duffin6877e6d2020-09-25 19:59:14 +01001419 Output_javadoc_comments *bool
Paul Duffin11512472019-02-11 15:55:17 +00001420 Arg_files []string
Sundong Ahn054b19a2018-10-19 13:46:09 +09001421 Args *string
Sundong Ahn054b19a2018-10-19 13:46:09 +09001422 Java_version *string
Paul Duffine22c2ab2020-05-20 19:35:27 +01001423 Annotations_enabled *bool
Sundong Ahn054b19a2018-10-19 13:46:09 +09001424 Merge_annotations_dirs []string
1425 Merge_inclusion_annotations_dirs []string
Paul Duffin0ff08bd2020-04-29 13:30:54 +01001426 Generate_stubs *bool
Anton Hanssone87b03d2020-12-21 15:29:34 +00001427 Previous_api *string
Sundong Ahn054b19a2018-10-19 13:46:09 +09001428 Check_api struct {
Anton Hanssone6056152020-12-31 10:37:27 +00001429 Current ApiToCheck
1430 Last_released ApiToCheck
Paul Duffin160fe412020-05-10 19:32:20 +01001431
1432 Api_lint struct {
1433 Enabled *bool
1434 New_since *string
1435 Baseline_file *string
1436 }
Jiyong Park58c518b2018-05-12 22:29:12 +09001437 }
Sundong Ahn1b92c822018-05-29 11:35:17 +09001438 Aidl struct {
1439 Include_dirs []string
1440 Local_include_dirs []string
1441 }
Paul Duffin040e9062020-11-23 17:41:36 +00001442 Dists []android.Dist
Jiyong Parkc678ad32018-04-10 13:07:10 +09001443 }{}
1444
Paul Duffin7b78b4d2020-04-28 14:08:32 +01001445 // The stubs source processing uses the same compile time classpath when extracting the
1446 // API from the implementation library as it does when compiling it. i.e. the same
1447 // * sdk version
1448 // * system_modules
1449 // * libs (static_libs/libs)
Paul Duffin250e6192019-06-07 10:44:37 +01001450
Paul Duffin0ff08bd2020-04-29 13:30:54 +01001451 props.Name = proptools.StringPtr(name)
Anton Hansson944e77d2020-08-19 11:40:22 +01001452 props.Visibility = childModuleVisibility(module.sdkLibraryProperties.Stubs_source_visibility)
Paul Duffina18abc22020-05-16 18:54:24 +01001453 props.Srcs = append(props.Srcs, module.properties.Srcs...)
Anton Hanssonf8ea3722021-09-16 14:24:13 +01001454 props.Srcs = append(props.Srcs, module.sdkLibraryProperties.Api_srcs...)
Paul Duffina18abc22020-05-16 18:54:24 +01001455 props.Sdk_version = module.deviceProperties.Sdk_version
1456 props.System_modules = module.deviceProperties.System_modules
Jiyong Parkc678ad32018-04-10 13:07:10 +09001457 props.Installable = proptools.BoolPtr(false)
Sundong Ahne6f0b052018-06-05 16:46:14 +09001458 // A droiddoc module has only one Libs property and doesn't distinguish between
1459 // shared libs and static libs. So we need to add both of these libs to Libs property.
Paul Duffina18abc22020-05-16 18:54:24 +01001460 props.Libs = module.properties.Libs
1461 props.Libs = append(props.Libs, module.properties.Static_libs...)
1462 props.Aidl.Include_dirs = module.deviceProperties.Aidl.Include_dirs
1463 props.Aidl.Local_include_dirs = module.deviceProperties.Aidl.Local_include_dirs
1464 props.Java_version = module.properties.Java_version
Jiyong Parkc678ad32018-04-10 13:07:10 +09001465
Paul Duffine22c2ab2020-05-20 19:35:27 +01001466 props.Annotations_enabled = module.sdkLibraryProperties.Annotations_enabled
Sundong Ahn054b19a2018-10-19 13:46:09 +09001467 props.Merge_annotations_dirs = module.sdkLibraryProperties.Merge_annotations_dirs
1468 props.Merge_inclusion_annotations_dirs = module.sdkLibraryProperties.Merge_inclusion_annotations_dirs
1469
Paul Duffin6d0886e2020-04-07 18:49:53 +01001470 droidstubsArgs := []string{}
Paul Duffin235ffff2019-12-24 10:41:30 +00001471 if len(module.sdkLibraryProperties.Api_packages) != 0 {
Paul Duffin6d0886e2020-04-07 18:49:53 +01001472 droidstubsArgs = append(droidstubsArgs, "--stub-packages "+strings.Join(module.sdkLibraryProperties.Api_packages, ":"))
Paul Duffin235ffff2019-12-24 10:41:30 +00001473 }
1474 if len(module.sdkLibraryProperties.Hidden_api_packages) != 0 {
Paul Duffin6d0886e2020-04-07 18:49:53 +01001475 droidstubsArgs = append(droidstubsArgs,
Paul Duffin235ffff2019-12-24 10:41:30 +00001476 android.JoinWithPrefix(module.sdkLibraryProperties.Hidden_api_packages, " --hide-package "))
1477 }
Paul Duffin6d0886e2020-04-07 18:49:53 +01001478 droidstubsArgs = append(droidstubsArgs, module.sdkLibraryProperties.Droiddoc_options...)
Paul Duffin235ffff2019-12-24 10:41:30 +00001479 disabledWarnings := []string{
1480 "MissingPermission",
1481 "BroadcastBehavior",
1482 "HiddenSuperclass",
1483 "DeprecationMismatch",
1484 "UnavailableSymbol",
1485 "SdkConstant",
1486 "HiddenTypeParameter",
1487 "Todo",
1488 "Typo",
1489 }
Paul Duffin6d0886e2020-04-07 18:49:53 +01001490 droidstubsArgs = append(droidstubsArgs, android.JoinWithPrefix(disabledWarnings, "--hide "))
Sundong Ahnfb2721f2018-09-17 13:23:09 +09001491
Paul Duffin6877e6d2020-09-25 19:59:14 +01001492 // Output Javadoc comments for public scope.
1493 if apiScope == apiScopePublic {
1494 props.Output_javadoc_comments = proptools.BoolPtr(true)
1495 }
1496
Paul Duffin1fb487d2020-04-07 18:50:10 +01001497 // Add in scope specific arguments.
Paul Duffin0ff08bd2020-04-29 13:30:54 +01001498 droidstubsArgs = append(droidstubsArgs, scopeSpecificDroidstubsArgs...)
Paul Duffin11512472019-02-11 15:55:17 +00001499 props.Arg_files = module.sdkLibraryProperties.Droiddoc_option_files
Paul Duffin6d0886e2020-04-07 18:49:53 +01001500 props.Args = proptools.StringPtr(strings.Join(droidstubsArgs, " "))
Jiyong Parkc678ad32018-04-10 13:07:10 +09001501
Paul Duffin15f34ef2020-07-20 18:04:44 +01001502 // List of APIs identified from the provided source files are created. They are later
1503 // compared against to the not-yet-released (a.k.a current) list of APIs and to the
1504 // last-released (a.k.a numbered) list of API.
1505 currentApiFileName := apiScope.apiFilePrefix + "current.txt"
1506 removedApiFileName := apiScope.apiFilePrefix + "removed.txt"
1507 apiDir := module.getApiDir()
1508 currentApiFileName = path.Join(apiDir, currentApiFileName)
1509 removedApiFileName = path.Join(apiDir, removedApiFileName)
Jiyong Parkc678ad32018-04-10 13:07:10 +09001510
Paul Duffin15f34ef2020-07-20 18:04:44 +01001511 // check against the not-yet-release API
1512 props.Check_api.Current.Api_file = proptools.StringPtr(currentApiFileName)
1513 props.Check_api.Current.Removed_api_file = proptools.StringPtr(removedApiFileName)
Jiyong Park58c518b2018-05-12 22:29:12 +09001514
Anton Hanssone6056152020-12-31 10:37:27 +00001515 if !(apiScope.unstable || module.sdkLibraryProperties.Unsafe_ignore_missing_latest_api) {
Paul Duffin15f34ef2020-07-20 18:04:44 +01001516 // check against the latest released API
1517 latestApiFilegroupName := proptools.StringPtr(module.latestApiFilegroupName(apiScope))
Anton Hanssone87b03d2020-12-21 15:29:34 +00001518 props.Previous_api = latestApiFilegroupName
Paul Duffin15f34ef2020-07-20 18:04:44 +01001519 props.Check_api.Last_released.Api_file = latestApiFilegroupName
1520 props.Check_api.Last_released.Removed_api_file = proptools.StringPtr(
1521 module.latestRemovedApiFilegroupName(apiScope))
Jaewoong Jung1a97ee02021-03-09 13:25:02 -08001522 props.Check_api.Last_released.Baseline_file = proptools.StringPtr(
1523 module.latestIncompatibilitiesFilegroupName(apiScope))
Paul Duffin160fe412020-05-10 19:32:20 +01001524
Paul Duffin15f34ef2020-07-20 18:04:44 +01001525 if proptools.Bool(module.sdkLibraryProperties.Api_lint.Enabled) {
1526 // Enable api lint.
1527 props.Check_api.Api_lint.Enabled = proptools.BoolPtr(true)
1528 props.Check_api.Api_lint.New_since = latestApiFilegroupName
Paul Duffin160fe412020-05-10 19:32:20 +01001529
Paul Duffin15f34ef2020-07-20 18:04:44 +01001530 // If it exists then pass a lint-baseline.txt through to droidstubs.
1531 baselinePath := path.Join(apiDir, apiScope.apiFilePrefix+"lint-baseline.txt")
1532 baselinePathRelativeToRoot := path.Join(mctx.ModuleDir(), baselinePath)
1533 paths, err := mctx.GlobWithDeps(baselinePathRelativeToRoot, nil)
1534 if err != nil {
1535 mctx.ModuleErrorf("error checking for presence of %s: %s", baselinePathRelativeToRoot, err)
1536 }
1537 if len(paths) == 1 {
1538 props.Check_api.Api_lint.Baseline_file = proptools.StringPtr(baselinePath)
1539 } else if len(paths) != 0 {
1540 mctx.ModuleErrorf("error checking for presence of %s: expected one path, found: %v", baselinePathRelativeToRoot, paths)
Paul Duffin160fe412020-05-10 19:32:20 +01001541 }
1542 }
Paul Duffin15f34ef2020-07-20 18:04:44 +01001543 }
Jiyong Park58c518b2018-05-12 22:29:12 +09001544
Paul Duffin15f34ef2020-07-20 18:04:44 +01001545 if !Bool(module.sdkLibraryProperties.No_dist) {
Paul Duffin040e9062020-11-23 17:41:36 +00001546 // Dist the api txt and removed api txt artifacts for sdk builds.
1547 distDir := proptools.StringPtr(path.Join(module.apiDistPath(apiScope), "api"))
1548 for _, p := range []struct {
1549 tag string
1550 pattern string
1551 }{
1552 {tag: ".api.txt", pattern: "%s.txt"},
1553 {tag: ".removed-api.txt", pattern: "%s-removed.txt"},
1554 } {
1555 props.Dists = append(props.Dists, android.Dist{
1556 Targets: []string{"sdk", "win_sdk"},
1557 Dir: distDir,
1558 Dest: proptools.StringPtr(fmt.Sprintf(p.pattern, module.distStem())),
1559 Tag: proptools.StringPtr(p.tag),
1560 })
1561 }
Anton Hansson5fd5d242020-03-27 19:43:19 +00001562 }
1563
Colin Cross84dfc3d2019-09-25 11:33:01 -07001564 mctx.CreateModule(DroidstubsFactory, &props)
Jiyong Parkc678ad32018-04-10 13:07:10 +09001565}
1566
Paul Duffinea8f8082021-06-24 13:25:57 +01001567// Implements android.ApexModule
Jooyung Han5e9013b2020-03-10 06:23:13 +09001568func (module *SdkLibrary) DepIsInSameApex(mctx android.BaseModuleContext, dep android.Module) bool {
1569 depTag := mctx.OtherModuleDependencyTag(dep)
1570 if depTag == xmlPermissionsFileTag {
1571 return true
1572 }
1573 return module.Library.DepIsInSameApex(mctx, dep)
1574}
1575
Paul Duffinea8f8082021-06-24 13:25:57 +01001576// Implements android.ApexModule
1577func (module *SdkLibrary) UniqueApexVariations() bool {
1578 return module.uniqueApexVariations()
1579}
1580
Jiyong Parkc678ad32018-04-10 13:07:10 +09001581// Creates the xml file that publicizes the runtime library
Paul Duffinf0229202020-04-29 16:47:28 +01001582func (module *SdkLibrary) createXmlFile(mctx android.DefaultableHookContext) {
Jiyong Parke3833882020-02-17 17:28:10 +09001583 props := struct {
Paul Duffin1dbe3ca2020-05-16 09:57:59 +01001584 Name *string
1585 Lib_name *string
1586 Apex_available []string
Jiyong Parke3833882020-02-17 17:28:10 +09001587 }{
Paul Duffineedc5d52020-06-12 17:46:39 +01001588 Name: proptools.StringPtr(module.xmlPermissionsModuleName()),
Jooyung Han5e9013b2020-03-10 06:23:13 +09001589 Lib_name: proptools.StringPtr(module.BaseModuleName()),
1590 Apex_available: module.ApexProperties.Apex_available,
Jiyong Parkc678ad32018-04-10 13:07:10 +09001591 }
Jiyong Parke3833882020-02-17 17:28:10 +09001592
Jiyong Parke3833882020-02-17 17:28:10 +09001593 mctx.CreateModule(sdkLibraryXmlFactory, &props)
Jiyong Parkc678ad32018-04-10 13:07:10 +09001594}
1595
Jiyong Parkf1691d22021-03-29 20:11:58 +09001596func PrebuiltJars(ctx android.BaseModuleContext, baseName string, s android.SdkSpec) android.Paths {
Jiyong Park54105c42021-03-31 18:17:53 +09001597 var ver android.ApiLevel
Jiyong Parkf1691d22021-03-29 20:11:58 +09001598 var kind android.SdkKind
1599 if s.UsePrebuilt(ctx) {
Jiyong Park54105c42021-03-31 18:17:53 +09001600 ver = s.ApiLevel
Jiyong Parkf1691d22021-03-29 20:11:58 +09001601 kind = s.Kind
Jiyong Parkc678ad32018-04-10 13:07:10 +09001602 } else {
Jiyong Park6a927c42020-01-21 02:03:43 +09001603 // We don't have prebuilt SDK for the specific sdkVersion.
1604 // Instead of breaking the build, fallback to use "system_current"
Jiyong Park54105c42021-03-31 18:17:53 +09001605 ver = android.FutureApiLevel
Jiyong Parkf1691d22021-03-29 20:11:58 +09001606 kind = android.SdkSystem
Sundong Ahn054b19a2018-10-19 13:46:09 +09001607 }
Jiyong Park6a927c42020-01-21 02:03:43 +09001608
1609 dir := filepath.Join("prebuilts", "sdk", ver.String(), kind.String())
Paul Duffin50061512020-01-21 16:31:05 +00001610 jar := filepath.Join(dir, baseName+".jar")
Sundong Ahn054b19a2018-10-19 13:46:09 +09001611 jarPath := android.ExistentPathForSource(ctx, jar)
Sundong Ahnae418ac2019-02-28 15:01:28 +09001612 if !jarPath.Valid() {
Colin Cross07c88562020-01-07 09:34:44 -08001613 if ctx.Config().AllowMissingDependencies() {
1614 return android.Paths{android.PathForSource(ctx, jar)}
1615 } else {
Jiyong Parkf1691d22021-03-29 20:11:58 +09001616 ctx.PropertyErrorf("sdk_library", "invalid sdk version %q, %q does not exist", s.Raw, jar)
Colin Cross07c88562020-01-07 09:34:44 -08001617 }
Sundong Ahnae418ac2019-02-28 15:01:28 +09001618 return nil
1619 }
Sundong Ahn054b19a2018-10-19 13:46:09 +09001620 return android.Paths{jarPath.Path()}
1621}
1622
Colin Crossaede88c2020-08-11 12:17:01 -07001623// 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 +01001624//
1625// If either this or the other module are on the platform then this will return
1626// false.
Colin Cross56a83212020-09-15 18:30:11 -07001627func withinSameApexesAs(ctx android.BaseModuleContext, other android.Module) bool {
1628 apexInfo := ctx.Provider(android.ApexInfoProvider).(android.ApexInfo)
1629 otherApexInfo := ctx.OtherModuleProvider(other, android.ApexInfoProvider).(android.ApexInfo)
Jiyong Parkab50b072021-05-12 17:13:56 +09001630 return len(otherApexInfo.InApexVariants) > 0 && reflect.DeepEqual(apexInfo.InApexVariants, otherApexInfo.InApexVariants)
Paul Duffin9b879592020-05-26 13:21:35 +01001631}
1632
Jiyong Parkf1691d22021-03-29 20:11:58 +09001633func (module *SdkLibrary) sdkJars(ctx android.BaseModuleContext, sdkVersion android.SdkSpec, headerJars bool) android.Paths {
Jiyong Park932cdfe2020-05-28 00:19:53 +09001634 // If the client doesn't set sdk_version, but if this library prefers stubs over
1635 // the impl library, let's provide the widest API surface possible. To do so,
1636 // force override sdk_version to module_current so that the closest possible API
1637 // surface could be found in selectHeaderJarsForSdkVersion
Jiyong Parkf1691d22021-03-29 20:11:58 +09001638 if module.defaultsToStubs() && !sdkVersion.Specified() {
Jiyong Park92315372021-04-02 08:45:46 +09001639 sdkVersion = android.SdkSpecFrom(ctx, "module_current")
Jiyong Park932cdfe2020-05-28 00:19:53 +09001640 }
Paul Duffind1b3a922020-01-22 11:57:20 +00001641
Paul Duffindaaa3322020-05-26 18:13:57 +01001642 // Only provide access to the implementation library if it is actually built.
1643 if module.requiresRuntimeImplementationLibrary() {
1644 // Check any special cases for java_sdk_library.
1645 //
1646 // Only allow access to the implementation library in the following condition:
1647 // * No sdk_version specified on the referencing module.
Paul Duffin9b879592020-05-26 13:21:35 +01001648 // * The referencing module is in the same apex as this.
Jiyong Parkf1691d22021-03-29 20:11:58 +09001649 if sdkVersion.Kind == android.SdkPrivate || withinSameApexesAs(ctx, module) {
Paul Duffindaaa3322020-05-26 18:13:57 +01001650 if headerJars {
1651 return module.HeaderJars()
1652 } else {
1653 return module.ImplementationJars()
1654 }
Sundong Ahn054b19a2018-10-19 13:46:09 +09001655 }
Jiyong Parkc678ad32018-04-10 13:07:10 +09001656 }
Paul Duffinb05d4292020-05-20 12:19:10 +01001657
Paul Duffin23970f42020-05-20 14:20:02 +01001658 return module.selectHeaderJarsForSdkVersion(ctx, sdkVersion)
Jiyong Parkc678ad32018-04-10 13:07:10 +09001659}
1660
Sundong Ahn241cd372018-07-13 16:16:44 +09001661// to satisfy SdkLibraryDependency interface
Jiyong Parkf1691d22021-03-29 20:11:58 +09001662func (module *SdkLibrary) SdkHeaderJars(ctx android.BaseModuleContext, sdkVersion android.SdkSpec) android.Paths {
Paul Duffind1b3a922020-01-22 11:57:20 +00001663 return module.sdkJars(ctx, sdkVersion, true /*headerJars*/)
1664}
1665
1666// to satisfy SdkLibraryDependency interface
Jiyong Parkf1691d22021-03-29 20:11:58 +09001667func (module *SdkLibrary) SdkImplementationJars(ctx android.BaseModuleContext, sdkVersion android.SdkSpec) android.Paths {
Paul Duffind1b3a922020-01-22 11:57:20 +00001668 return module.sdkJars(ctx, sdkVersion, false /*headerJars*/)
Sundong Ahn241cd372018-07-13 16:16:44 +09001669}
1670
Colin Cross571cccf2019-02-04 11:22:08 -08001671var javaSdkLibrariesKey = android.NewOnceKey("javaSdkLibraries")
1672
Jiyong Park82484c02018-04-23 21:41:26 +09001673func javaSdkLibraries(config android.Config) *[]string {
Colin Cross571cccf2019-02-04 11:22:08 -08001674 return config.Once(javaSdkLibrariesKey, func() interface{} {
Jiyong Park82484c02018-04-23 21:41:26 +09001675 return &[]string{}
1676 }).(*[]string)
1677}
1678
Paul Duffin749f98f2019-12-30 17:23:46 +00001679func (module *SdkLibrary) getApiDir() string {
1680 return proptools.StringDefault(module.sdkLibraryProperties.Api_dir, "api")
1681}
1682
Jiyong Parkc678ad32018-04-10 13:07:10 +09001683// For a java_sdk_library module, create internal modules for stubs, docs,
1684// runtime libs and xml file. If requested, the stubs and docs are created twice
1685// once for public API level and once for system API level
Paul Duffinf0229202020-04-29 16:47:28 +01001686func (module *SdkLibrary) CreateInternalModules(mctx android.DefaultableHookContext) {
1687 // If the module has been disabled then don't create any child modules.
1688 if !module.Enabled() {
1689 return
1690 }
1691
Paul Duffina18abc22020-05-16 18:54:24 +01001692 if len(module.properties.Srcs) == 0 {
Inseob Kimc0907f12019-02-08 21:00:45 +09001693 mctx.PropertyErrorf("srcs", "java_sdk_library must specify srcs")
Jooyung Han58f26ab2019-12-18 15:34:32 +09001694 return
Inseob Kimc0907f12019-02-08 21:00:45 +09001695 }
1696
Paul Duffin37e0b772019-12-30 17:20:10 +00001697 // If this builds against standard libraries (i.e. is not part of the core libraries)
Paul Duffin4f5c1ef2020-11-19 14:53:43 +00001698 // then assume it provides both system and test apis.
Jiyong Parkf1691d22021-03-29 20:11:58 +09001699 sdkDep := decodeSdkDep(mctx, android.SdkContext(&module.Library))
Paul Duffin37e0b772019-12-30 17:20:10 +00001700 hasSystemAndTestApis := sdkDep.hasStandardLibs()
Paul Duffin3375e352020-04-28 10:44:03 +01001701 module.sdkLibraryProperties.Generate_system_and_test_apis = hasSystemAndTestApis
Paul Duffin4f5c1ef2020-11-19 14:53:43 +00001702
Jaewoong Jung18aefc12020-12-21 09:11:10 -08001703 missingCurrentApi := false
Inseob Kim8098faa2019-03-18 10:19:51 +09001704
Paul Duffin3375e352020-04-28 10:44:03 +01001705 generatedScopes := module.getGeneratedApiScopes(mctx)
Paul Duffind1b3a922020-01-22 11:57:20 +00001706
Paul Duffin749f98f2019-12-30 17:23:46 +00001707 apiDir := module.getApiDir()
Paul Duffin3375e352020-04-28 10:44:03 +01001708 for _, scope := range generatedScopes {
Inseob Kim8098faa2019-03-18 10:19:51 +09001709 for _, api := range []string{"current.txt", "removed.txt"} {
Paul Duffind1b3a922020-01-22 11:57:20 +00001710 path := path.Join(mctx.ModuleDir(), apiDir, scope.apiFilePrefix+api)
Inseob Kim8098faa2019-03-18 10:19:51 +09001711 p := android.ExistentPathForSource(mctx, path)
1712 if !p.Valid() {
Colin Cross18f840c2021-05-20 17:56:54 -07001713 if mctx.Config().AllowMissingDependencies() {
1714 mctx.AddMissingDependencies([]string{path})
1715 } else {
1716 mctx.ModuleErrorf("Current api file %#v doesn't exist", path)
1717 missingCurrentApi = true
1718 }
Inseob Kim8098faa2019-03-18 10:19:51 +09001719 }
1720 }
1721 }
1722
Jaewoong Jung18aefc12020-12-21 09:11:10 -08001723 if missingCurrentApi {
Inseob Kim8098faa2019-03-18 10:19:51 +09001724 script := "build/soong/scripts/gen-java-current-api-files.sh"
1725 p := android.ExistentPathForSource(mctx, script)
1726
1727 if !p.Valid() {
1728 panic(fmt.Sprintf("script file %s doesn't exist", script))
1729 }
1730
1731 mctx.ModuleErrorf("One or more current api files are missing. "+
1732 "You can update them by:\n"+
Paul Duffin37e0b772019-12-30 17:20:10 +00001733 "%s %q %s && m update-api",
Paul Duffind1b3a922020-01-22 11:57:20 +00001734 script, filepath.Join(mctx.ModuleDir(), apiDir),
Paul Duffin3375e352020-04-28 10:44:03 +01001735 strings.Join(generatedScopes.Strings(func(s *apiScope) string { return s.apiFilePrefix }), " "))
Inseob Kim8098faa2019-03-18 10:19:51 +09001736 return
1737 }
1738
Paul Duffin3375e352020-04-28 10:44:03 +01001739 for _, scope := range generatedScopes {
Paul Duffin15f34ef2020-07-20 18:04:44 +01001740 // Use the stubs source name for legacy reasons.
1741 module.createStubsSourcesAndApi(mctx, scope, module.stubsSourceModuleName(scope), scope.droidstubsArgs)
Paul Duffin0ff08bd2020-04-29 13:30:54 +01001742
Paul Duffind1b3a922020-01-22 11:57:20 +00001743 module.createStubsLibrary(mctx, scope)
Inseob Kimc0907f12019-02-08 21:00:45 +09001744 }
1745
Paul Duffindfa131e2020-05-15 20:37:11 +01001746 if module.requiresRuntimeImplementationLibrary() {
Paul Duffin5df79302020-05-16 15:52:12 +01001747 // Create child module to create an implementation library.
1748 //
1749 // This temporarily creates a second implementation library that can be explicitly
1750 // referenced.
1751 //
1752 // TODO(b/156618935) - update comment once only one implementation library is created.
1753 module.createImplLibrary(mctx)
1754
Paul Duffindfa131e2020-05-15 20:37:11 +01001755 // Only create an XML permissions file that declares the library as being usable
1756 // as a shared library if required.
1757 if module.sharedLibrary() {
1758 module.createXmlFile(mctx)
1759 }
Paul Duffin43db9be2019-12-30 17:35:49 +00001760
1761 // record java_sdk_library modules so that they are exported to make
1762 javaSdkLibraries := javaSdkLibraries(mctx.Config())
1763 javaSdkLibrariesLock.Lock()
1764 defer javaSdkLibrariesLock.Unlock()
1765 *javaSdkLibraries = append(*javaSdkLibraries, module.BaseModuleName())
1766 }
Anton Hansson7f66efa2020-10-08 14:47:23 +01001767
1768 // Add the impl_only_libs *after* we're done using the Libs prop in submodules.
1769 module.properties.Libs = append(module.properties.Libs, module.sdkLibraryProperties.Impl_only_libs...)
Inseob Kimc0907f12019-02-08 21:00:45 +09001770}
1771
1772func (module *SdkLibrary) InitSdkLibraryProperties() {
Colin Crossce6734e2020-06-15 16:09:53 -07001773 module.addHostAndDeviceProperties()
1774 module.AddProperties(&module.sdkLibraryProperties)
Sundong Ahn054b19a2018-10-19 13:46:09 +09001775
Paul Duffin71b33cc2021-06-23 11:39:47 +01001776 module.initSdkLibraryComponent(module)
Paul Duffin859fe962020-05-15 10:20:31 +01001777
Paul Duffina18abc22020-05-16 18:54:24 +01001778 module.properties.Installable = proptools.BoolPtr(true)
1779 module.deviceProperties.IsSDKLibrary = true
Inseob Kimc0907f12019-02-08 21:00:45 +09001780}
Sundong Ahn054b19a2018-10-19 13:46:09 +09001781
Paul Duffindfa131e2020-05-15 20:37:11 +01001782func (module *SdkLibrary) requiresRuntimeImplementationLibrary() bool {
1783 return !proptools.Bool(module.sdkLibraryProperties.Api_only)
1784}
1785
Jiyong Park932cdfe2020-05-28 00:19:53 +09001786func (module *SdkLibrary) defaultsToStubs() bool {
1787 return proptools.Bool(module.sdkLibraryProperties.Default_to_stubs)
1788}
1789
Paul Duffin1b1e8062020-05-08 13:44:43 +01001790// Defines how to name the individual component modules the sdk library creates.
1791type sdkLibraryComponentNamingScheme interface {
1792 stubsLibraryModuleName(scope *apiScope, baseName string) string
1793
1794 stubsSourceModuleName(scope *apiScope, baseName string) string
Paul Duffin1b1e8062020-05-08 13:44:43 +01001795}
1796
1797type defaultNamingScheme struct {
1798}
1799
1800func (s *defaultNamingScheme) stubsLibraryModuleName(scope *apiScope, baseName string) string {
1801 return scope.stubsLibraryModuleName(baseName)
1802}
1803
1804func (s *defaultNamingScheme) stubsSourceModuleName(scope *apiScope, baseName string) string {
1805 return scope.stubsSourceModuleName(baseName)
1806}
1807
Paul Duffin1b1e8062020-05-08 13:44:43 +01001808var _ sdkLibraryComponentNamingScheme = (*defaultNamingScheme)(nil)
1809
Jaewoong Jungbc15e3a2021-03-10 17:02:43 -08001810func moduleStubLinkType(name string) (stub bool, ret sdkLinkType) {
Anton Hansson2d0c1942020-05-25 12:20:51 +01001811 // This suffix-based approach is fragile and could potentially mis-trigger.
1812 // TODO(b/155164730): Clean this up when modules no longer reference sdk_lib stubs directly.
Anton Hansson08f476b2021-04-07 15:32:19 +01001813 if strings.HasSuffix(name, apiScopePublic.stubsLibraryModuleNameSuffix()) {
1814 if name == "hwbinder.stubs" || name == "libcore_private.stubs" {
1815 // Due to a previous bug, these modules were not considered stubs, so we retain that.
1816 return false, javaPlatform
1817 }
Anton Hansson2d0c1942020-05-25 12:20:51 +01001818 return true, javaSdk
1819 }
Anton Hansson08f476b2021-04-07 15:32:19 +01001820 if strings.HasSuffix(name, apiScopeSystem.stubsLibraryModuleNameSuffix()) {
Anton Hansson2d0c1942020-05-25 12:20:51 +01001821 return true, javaSystem
1822 }
Anton Hansson08f476b2021-04-07 15:32:19 +01001823 if strings.HasSuffix(name, apiScopeModuleLib.stubsLibraryModuleNameSuffix()) {
Anton Hansson2d0c1942020-05-25 12:20:51 +01001824 return true, javaModule
1825 }
Anton Hansson08f476b2021-04-07 15:32:19 +01001826 if strings.HasSuffix(name, apiScopeTest.stubsLibraryModuleNameSuffix()) {
Anton Hansson2d0c1942020-05-25 12:20:51 +01001827 return true, javaSystem
1828 }
1829 return false, javaPlatform
1830}
1831
Jaewoong Jung4f158ee2019-07-11 10:05:35 -07001832// java_sdk_library is a special Java library that provides optional platform APIs to apps.
1833// In practice, it can be viewed as a combination of several modules: 1) stubs library that clients
1834// are linked against to, 2) droiddoc module that internally generates API stubs source files,
1835// 3) the real runtime shared library that implements the APIs, and 4) XML file for adding
1836// the runtime lib to the classpath at runtime if requested via <uses-library>.
Inseob Kimc0907f12019-02-08 21:00:45 +09001837func SdkLibraryFactory() android.Module {
1838 module := &SdkLibrary{}
Paul Duffinc3091c82020-05-08 14:16:20 +01001839
1840 // Initialize information common between source and prebuilt.
Paul Duffin71b33cc2021-06-23 11:39:47 +01001841 module.initCommon(module)
Paul Duffinc3091c82020-05-08 14:16:20 +01001842
Inseob Kimc0907f12019-02-08 21:00:45 +09001843 module.InitSdkLibraryProperties()
Jooyung Han58f26ab2019-12-18 15:34:32 +09001844 android.InitApexModule(module)
Paul Duffinb6b89a42021-05-06 16:33:43 +01001845 android.InitSdkAwareModule(module)
Sundong Ahn054b19a2018-10-19 13:46:09 +09001846 InitJavaModule(module, android.HostAndDeviceSupported)
Paul Duffin3375e352020-04-28 10:44:03 +01001847
1848 // Initialize the map from scope to scope specific properties.
1849 scopeToProperties := make(map[*apiScope]*ApiScopeProperties)
1850 for _, scope := range allApiScopes {
1851 scopeToProperties[scope] = scope.scopeSpecificProperties(module)
1852 }
1853 module.scopeToProperties = scopeToProperties
1854
Paul Duffin4911a892020-04-29 23:35:13 +01001855 // Add the properties containing visibility rules so that they are checked.
Paul Duffin5df79302020-05-16 15:52:12 +01001856 android.AddVisibilityProperty(module, "impl_library_visibility", &module.sdkLibraryProperties.Impl_library_visibility)
Paul Duffin4911a892020-04-29 23:35:13 +01001857 android.AddVisibilityProperty(module, "stubs_library_visibility", &module.sdkLibraryProperties.Stubs_library_visibility)
1858 android.AddVisibilityProperty(module, "stubs_source_visibility", &module.sdkLibraryProperties.Stubs_source_visibility)
1859
Paul Duffin1b1e8062020-05-08 13:44:43 +01001860 module.SetDefaultableHook(func(ctx android.DefaultableHookContext) {
Paul Duffindfa131e2020-05-15 20:37:11 +01001861 // If no implementation is required then it cannot be used as a shared library
1862 // either.
1863 if !module.requiresRuntimeImplementationLibrary() {
1864 // If shared_library has been explicitly set to true then it is incompatible
1865 // with api_only: true.
1866 if proptools.Bool(module.commonSdkLibraryProperties.Shared_library) {
1867 ctx.PropertyErrorf("api_only/shared_library", "inconsistent settings, shared_library and api_only cannot both be true")
1868 }
1869 // Set shared_library: false.
1870 module.commonSdkLibraryProperties.Shared_library = proptools.BoolPtr(false)
1871 }
1872
Paul Duffin1b1e8062020-05-08 13:44:43 +01001873 if module.initCommonAfterDefaultsApplied(ctx) {
1874 module.CreateInternalModules(ctx)
1875 }
1876 })
Jiyong Parkc678ad32018-04-10 13:07:10 +09001877 return module
1878}
Colin Cross79c7c262019-04-17 11:11:46 -07001879
1880//
1881// SDK library prebuilts
1882//
1883
Paul Duffin56d44902020-01-31 13:36:25 +00001884// Properties associated with each api scope.
1885type sdkLibraryScopeProperties struct {
Colin Cross79c7c262019-04-17 11:11:46 -07001886 Jars []string `android:"path"`
1887
1888 Sdk_version *string
1889
Colin Cross79c7c262019-04-17 11:11:46 -07001890 // List of shared java libs that this module has dependencies to
1891 Libs []string
Paul Duffin3d1248c2020-04-09 00:10:17 +01001892
Paul Duffinc8782502020-04-29 20:45:27 +01001893 // The stubs source.
Paul Duffin3d1248c2020-04-09 00:10:17 +01001894 Stub_srcs []string `android:"path"`
Paul Duffin1fd005d2020-04-09 01:08:11 +01001895
1896 // The current.txt
Paul Duffin0f8faff2020-05-20 16:18:00 +01001897 Current_api *string `android:"path"`
Paul Duffin1fd005d2020-04-09 01:08:11 +01001898
1899 // The removed.txt
Paul Duffin0f8faff2020-05-20 16:18:00 +01001900 Removed_api *string `android:"path"`
Anton Hanssond78eb762021-09-21 15:25:12 +01001901
1902 // Annotation zip
1903 Annotations *string `android:"path"`
Colin Cross79c7c262019-04-17 11:11:46 -07001904}
1905
Paul Duffin56d44902020-01-31 13:36:25 +00001906type sdkLibraryImportProperties struct {
Paul Duffinfcfd7912020-01-31 17:54:30 +00001907 // List of shared java libs, common to all scopes, that this module has
1908 // dependencies to
1909 Libs []string
Paul Duffin1267d872021-04-16 17:21:36 +01001910
1911 // If set to true, compile dex files for the stubs. Defaults to false.
1912 Compile_dex *bool
Paul Duffin869de142021-07-15 14:14:41 +01001913
1914 // If not empty, classes are restricted to the specified packages and their sub-packages.
Paul Duffin869de142021-07-15 14:14:41 +01001915 Permitted_packages []string
Paul Duffin56d44902020-01-31 13:36:25 +00001916}
1917
Paul Duffineedc5d52020-06-12 17:46:39 +01001918type SdkLibraryImport struct {
Colin Cross79c7c262019-04-17 11:11:46 -07001919 android.ModuleBase
1920 android.DefaultableModuleBase
1921 prebuilt android.Prebuilt
Paul Duffindd46f712020-02-10 13:37:10 +00001922 android.ApexModuleBase
1923 android.SdkBase
Colin Cross79c7c262019-04-17 11:11:46 -07001924
Paul Duffin37856732021-02-26 14:24:15 +00001925 hiddenAPI
Jiakai Zhang204356f2021-09-09 08:12:46 +00001926 dexpreopter
Paul Duffin37856732021-02-26 14:24:15 +00001927
Colin Cross79c7c262019-04-17 11:11:46 -07001928 properties sdkLibraryImportProperties
1929
Paul Duffin46a26a82020-04-07 19:27:04 +01001930 // Map from api scope to the scope specific property structure.
1931 scopeProperties map[*apiScope]*sdkLibraryScopeProperties
1932
Paul Duffin56d44902020-01-31 13:36:25 +00001933 commonToSdkLibraryAndImport
Paul Duffineedc5d52020-06-12 17:46:39 +01001934
1935 // The reference to the implementation library created by the source module.
1936 // Is nil if the source module does not exist.
1937 implLibraryModule *Library
1938
1939 // The reference to the xml permissions module created by the source module.
1940 // Is nil if the source module does not exist.
1941 xmlPermissionsFileModule *sdkLibraryXml
Paul Duffin39853512021-02-26 11:09:39 +00001942
Jeongik Chad5fe8782021-07-08 01:13:11 +09001943 // Build path to the dex implementation jar obtained from the prebuilt_apex, if any.
Martin Stjernholm8be1e6d2021-09-15 03:34:04 +01001944 dexJarFile OptionalDexJarPath
Jeongik Chad5fe8782021-07-08 01:13:11 +09001945
1946 // Expected install file path of the source module(sdk_library)
1947 // or dex implementation jar obtained from the prebuilt_apex, if any.
1948 installFile android.Path
Colin Cross79c7c262019-04-17 11:11:46 -07001949}
1950
Paul Duffineedc5d52020-06-12 17:46:39 +01001951var _ SdkLibraryDependency = (*SdkLibraryImport)(nil)
Colin Cross79c7c262019-04-17 11:11:46 -07001952
Paul Duffin46a26a82020-04-07 19:27:04 +01001953// The type of a structure that contains a field of type sdkLibraryScopeProperties
1954// for each apiscope in allApiScopes, e.g. something like:
1955// struct {
1956// Public sdkLibraryScopeProperties
1957// System sdkLibraryScopeProperties
1958// ...
1959// }
1960var allScopeStructType = createAllScopePropertiesStructType()
1961
1962// Dynamically create a structure type for each apiscope in allApiScopes.
1963func createAllScopePropertiesStructType() reflect.Type {
1964 var fields []reflect.StructField
1965 for _, apiScope := range allApiScopes {
1966 field := reflect.StructField{
1967 Name: apiScope.fieldName,
1968 Type: reflect.TypeOf(sdkLibraryScopeProperties{}),
1969 }
1970 fields = append(fields, field)
1971 }
1972
1973 return reflect.StructOf(fields)
1974}
1975
1976// Create an instance of the scope specific structure type and return a map
1977// from apiscope to a pointer to each scope specific field.
1978func createPropertiesInstance() (interface{}, map[*apiScope]*sdkLibraryScopeProperties) {
1979 allScopePropertiesPtr := reflect.New(allScopeStructType)
1980 allScopePropertiesStruct := allScopePropertiesPtr.Elem()
1981 scopeProperties := make(map[*apiScope]*sdkLibraryScopeProperties)
1982
1983 for _, apiScope := range allApiScopes {
1984 field := allScopePropertiesStruct.FieldByName(apiScope.fieldName)
1985 scopeProperties[apiScope] = field.Addr().Interface().(*sdkLibraryScopeProperties)
1986 }
1987
1988 return allScopePropertiesPtr.Interface(), scopeProperties
1989}
1990
Jaewoong Jung4f158ee2019-07-11 10:05:35 -07001991// java_sdk_library_import imports a prebuilt java_sdk_library.
Colin Cross79c7c262019-04-17 11:11:46 -07001992func sdkLibraryImportFactory() android.Module {
Paul Duffineedc5d52020-06-12 17:46:39 +01001993 module := &SdkLibraryImport{}
Colin Cross79c7c262019-04-17 11:11:46 -07001994
Paul Duffin46a26a82020-04-07 19:27:04 +01001995 allScopeProperties, scopeToProperties := createPropertiesInstance()
1996 module.scopeProperties = scopeToProperties
1997 module.AddProperties(&module.properties, allScopeProperties)
Colin Cross79c7c262019-04-17 11:11:46 -07001998
Paul Duffinc3091c82020-05-08 14:16:20 +01001999 // Initialize information common between source and prebuilt.
Paul Duffin71b33cc2021-06-23 11:39:47 +01002000 module.initCommon(module)
Paul Duffinc3091c82020-05-08 14:16:20 +01002001
Paul Duffin0bdcb272020-02-06 15:24:57 +00002002 android.InitPrebuiltModule(module, &[]string{""})
Paul Duffindd46f712020-02-10 13:37:10 +00002003 android.InitApexModule(module)
2004 android.InitSdkAwareModule(module)
Colin Cross79c7c262019-04-17 11:11:46 -07002005 InitJavaModule(module, android.HostAndDeviceSupported)
2006
Paul Duffin1b1e8062020-05-08 13:44:43 +01002007 module.SetDefaultableHook(func(mctx android.DefaultableHookContext) {
2008 if module.initCommonAfterDefaultsApplied(mctx) {
2009 module.createInternalModules(mctx)
2010 }
2011 })
Colin Cross79c7c262019-04-17 11:11:46 -07002012 return module
2013}
2014
Paul Duffin630b11e2021-07-15 13:35:26 +01002015var _ PermittedPackagesForUpdatableBootJars = (*SdkLibraryImport)(nil)
2016
2017func (module *SdkLibraryImport) PermittedPackagesForUpdatableBootJars() []string {
2018 return module.properties.Permitted_packages
2019}
2020
Paul Duffineedc5d52020-06-12 17:46:39 +01002021func (module *SdkLibraryImport) Prebuilt() *android.Prebuilt {
Colin Cross79c7c262019-04-17 11:11:46 -07002022 return &module.prebuilt
2023}
2024
Paul Duffineedc5d52020-06-12 17:46:39 +01002025func (module *SdkLibraryImport) Name() string {
Colin Cross79c7c262019-04-17 11:11:46 -07002026 return module.prebuilt.Name(module.ModuleBase.Name())
2027}
2028
Paul Duffineedc5d52020-06-12 17:46:39 +01002029func (module *SdkLibraryImport) createInternalModules(mctx android.DefaultableHookContext) {
Colin Cross79c7c262019-04-17 11:11:46 -07002030
Paul Duffin50061512020-01-21 16:31:05 +00002031 // If the build is configured to use prebuilts then force this to be preferred.
Jeongik Cha816a23a2020-07-08 01:09:23 +09002032 if mctx.Config().AlwaysUsePrebuiltSdks() {
Paul Duffin50061512020-01-21 16:31:05 +00002033 module.prebuilt.ForcePrefer()
2034 }
2035
Paul Duffin46a26a82020-04-07 19:27:04 +01002036 for apiScope, scopeProperties := range module.scopeProperties {
Paul Duffin56d44902020-01-31 13:36:25 +00002037 if len(scopeProperties.Jars) == 0 {
2038 continue
2039 }
2040
Paul Duffinbbb546b2020-04-09 00:07:11 +01002041 module.createJavaImportForStubs(mctx, apiScope, scopeProperties)
Paul Duffin3d1248c2020-04-09 00:10:17 +01002042
Paul Duffin0f8faff2020-05-20 16:18:00 +01002043 if len(scopeProperties.Stub_srcs) > 0 {
2044 module.createPrebuiltStubsSources(mctx, apiScope, scopeProperties)
2045 }
Paul Duffin56d44902020-01-31 13:36:25 +00002046 }
Colin Cross79c7c262019-04-17 11:11:46 -07002047
2048 javaSdkLibraries := javaSdkLibraries(mctx.Config())
2049 javaSdkLibrariesLock.Lock()
2050 defer javaSdkLibrariesLock.Unlock()
2051 *javaSdkLibraries = append(*javaSdkLibraries, module.BaseModuleName())
2052}
2053
Paul Duffineedc5d52020-06-12 17:46:39 +01002054func (module *SdkLibraryImport) createJavaImportForStubs(mctx android.DefaultableHookContext, apiScope *apiScope, scopeProperties *sdkLibraryScopeProperties) {
Paul Duffinbbb546b2020-04-09 00:07:11 +01002055 // Creates a java import for the jar with ".stubs" suffix
2056 props := struct {
Paul Duffin1dbe3ca2020-05-16 09:57:59 +01002057 Name *string
2058 Sdk_version *string
2059 Libs []string
2060 Jars []string
2061 Prefer *bool
Paul Duffin1267d872021-04-16 17:21:36 +01002062 Compile_dex *bool
Paul Duffinbbb546b2020-04-09 00:07:11 +01002063 }{}
Paul Duffinc3091c82020-05-08 14:16:20 +01002064 props.Name = proptools.StringPtr(module.stubsLibraryModuleName(apiScope))
Paul Duffinbbb546b2020-04-09 00:07:11 +01002065 props.Sdk_version = scopeProperties.Sdk_version
2066 // Prepend any of the libs from the legacy public properties to the libs for each of the
2067 // scopes to avoid having to duplicate them in each scope.
2068 props.Libs = append(module.properties.Libs, scopeProperties.Libs...)
2069 props.Jars = scopeProperties.Jars
Paul Duffin1dbe3ca2020-05-16 09:57:59 +01002070
Paul Duffin38b57852020-05-13 16:08:09 +01002071 // The imports are preferred if the java_sdk_library_import is preferred.
2072 props.Prefer = proptools.BoolPtr(module.prebuilt.Prefer())
Paul Duffin859fe962020-05-15 10:20:31 +01002073
Paul Duffin1267d872021-04-16 17:21:36 +01002074 // The imports need to be compiled to dex if the java_sdk_library_import requests it.
Paul Duffinf4600f62021-05-13 22:34:45 +01002075 compileDex := module.properties.Compile_dex
2076 if module.stubLibrariesCompiledForDex() {
2077 compileDex = proptools.BoolPtr(true)
2078 }
2079 props.Compile_dex = compileDex
Paul Duffin1267d872021-04-16 17:21:36 +01002080
Paul Duffin859fe962020-05-15 10:20:31 +01002081 mctx.CreateModule(ImportFactory, &props, module.sdkComponentPropertiesForChildLibrary())
Paul Duffinbbb546b2020-04-09 00:07:11 +01002082}
2083
Paul Duffineedc5d52020-06-12 17:46:39 +01002084func (module *SdkLibraryImport) createPrebuiltStubsSources(mctx android.DefaultableHookContext, apiScope *apiScope, scopeProperties *sdkLibraryScopeProperties) {
Paul Duffin3d1248c2020-04-09 00:10:17 +01002085 props := struct {
Paul Duffin38b57852020-05-13 16:08:09 +01002086 Name *string
2087 Srcs []string
2088 Prefer *bool
Paul Duffin3d1248c2020-04-09 00:10:17 +01002089 }{}
Paul Duffinc3091c82020-05-08 14:16:20 +01002090 props.Name = proptools.StringPtr(module.stubsSourceModuleName(apiScope))
Paul Duffin3d1248c2020-04-09 00:10:17 +01002091 props.Srcs = scopeProperties.Stub_srcs
2092 mctx.CreateModule(PrebuiltStubsSourcesFactory, &props)
Paul Duffin38b57852020-05-13 16:08:09 +01002093
2094 // The stubs source is preferred if the java_sdk_library_import is preferred.
2095 props.Prefer = proptools.BoolPtr(module.prebuilt.Prefer())
Paul Duffin3d1248c2020-04-09 00:10:17 +01002096}
2097
Paul Duffin44f1d842020-06-26 20:17:02 +01002098// Add the dependencies on the child module in the component deps mutator so that it
2099// creates references to the prebuilt and not the source modules.
2100func (module *SdkLibraryImport) ComponentDepsMutator(ctx android.BottomUpMutatorContext) {
Paul Duffin46a26a82020-04-07 19:27:04 +01002101 for apiScope, scopeProperties := range module.scopeProperties {
Paul Duffin56d44902020-01-31 13:36:25 +00002102 if len(scopeProperties.Jars) == 0 {
2103 continue
2104 }
2105
2106 // Add dependencies to the prebuilt stubs library
Paul Duffin864116c2021-04-02 10:24:13 +01002107 ctx.AddVariationDependencies(nil, apiScope.stubsTag, android.PrebuiltNameFromSource(module.stubsLibraryModuleName(apiScope)))
Paul Duffin0f8faff2020-05-20 16:18:00 +01002108
2109 if len(scopeProperties.Stub_srcs) > 0 {
2110 // Add dependencies to the prebuilt stubs source library
Paul Duffin864116c2021-04-02 10:24:13 +01002111 ctx.AddVariationDependencies(nil, apiScope.stubsSourceTag, android.PrebuiltNameFromSource(module.stubsSourceModuleName(apiScope)))
Paul Duffin0f8faff2020-05-20 16:18:00 +01002112 }
Paul Duffin56d44902020-01-31 13:36:25 +00002113 }
Paul Duffin44f1d842020-06-26 20:17:02 +01002114}
2115
2116// Add other dependencies as normal.
2117func (module *SdkLibraryImport) DepsMutator(ctx android.BottomUpMutatorContext) {
Paul Duffineedc5d52020-06-12 17:46:39 +01002118
2119 implName := module.implLibraryModuleName()
2120 if ctx.OtherModuleExists(implName) {
2121 ctx.AddVariationDependencies(nil, implLibraryTag, implName)
2122
2123 xmlPermissionsModuleName := module.xmlPermissionsModuleName()
2124 if module.sharedLibrary() && ctx.OtherModuleExists(xmlPermissionsModuleName) {
2125 // Add dependency to the rule for generating the xml permissions file
2126 ctx.AddDependency(module, xmlPermissionsFileTag, xmlPermissionsModuleName)
2127 }
2128 }
Colin Cross79c7c262019-04-17 11:11:46 -07002129}
2130
Jiakai Zhang204356f2021-09-09 08:12:46 +00002131func (module *SdkLibraryImport) AndroidMkEntries() []android.AndroidMkEntries {
2132 // For an SDK library imported from a prebuilt APEX, we don't need a Make module for itself, as we
2133 // don't need to install it. However, we need to add its dexpreopt outputs as sub-modules, if it
2134 // is preopted.
2135 dexpreoptEntries := module.dexpreopter.AndroidMkEntriesForApex()
2136 return append(dexpreoptEntries, android.AndroidMkEntries{Disabled: true})
2137}
2138
Jiyong Park45bf82e2020-12-15 22:29:02 +09002139var _ android.ApexModule = (*SdkLibraryImport)(nil)
2140
2141// Implements android.ApexModule
Paul Duffineedc5d52020-06-12 17:46:39 +01002142func (module *SdkLibraryImport) DepIsInSameApex(mctx android.BaseModuleContext, dep android.Module) bool {
2143 depTag := mctx.OtherModuleDependencyTag(dep)
2144 if depTag == xmlPermissionsFileTag {
2145 return true
2146 }
2147
2148 // None of the other dependencies of the java_sdk_library_import are in the same apex
2149 // as the one that references this module.
2150 return false
2151}
2152
Jiyong Park45bf82e2020-12-15 22:29:02 +09002153// Implements android.ApexModule
Dan Albertc8060532020-07-22 22:32:17 -07002154func (module *SdkLibraryImport) ShouldSupportSdkVersion(ctx android.BaseModuleContext,
2155 sdkVersion android.ApiLevel) error {
Jooyung Han749dc692020-04-15 11:03:39 +09002156 // we don't check prebuilt modules for sdk_version
2157 return nil
2158}
2159
Paul Duffinea8f8082021-06-24 13:25:57 +01002160// Implements android.ApexModule
2161func (module *SdkLibraryImport) UniqueApexVariations() bool {
2162 return module.uniqueApexVariations()
2163}
2164
Paul Duffineedc5d52020-06-12 17:46:39 +01002165func (module *SdkLibraryImport) OutputFiles(tag string) (android.Paths, error) {
Paul Duffin46dc45a2020-05-14 15:39:10 +01002166 return module.commonOutputFiles(tag)
2167}
2168
Paul Duffineedc5d52020-06-12 17:46:39 +01002169func (module *SdkLibraryImport) GenerateAndroidBuildActions(ctx android.ModuleContext) {
Paul Duffina2ae7e02020-09-11 11:55:00 +01002170 module.generateCommonBuildActions(ctx)
2171
Jeongik Chad5fe8782021-07-08 01:13:11 +09002172 // Assume that source module(sdk_library) is installed in /<sdk_library partition>/framework
2173 module.installFile = android.PathForModuleInstall(ctx, "framework", module.Stem()+".jar")
2174
Paul Duffin0f8faff2020-05-20 16:18:00 +01002175 // Record the paths to the prebuilt stubs library and stubs source.
Colin Cross79c7c262019-04-17 11:11:46 -07002176 ctx.VisitDirectDeps(func(to android.Module) {
2177 tag := ctx.OtherModuleDependencyTag(to)
2178
Paul Duffin0f8faff2020-05-20 16:18:00 +01002179 // Extract information from any of the scope specific dependencies.
2180 if scopeTag, ok := tag.(scopeDependencyTag); ok {
2181 apiScope := scopeTag.apiScope
2182 scopePaths := module.getScopePathsCreateIfNeeded(apiScope)
2183
2184 // Extract information from the dependency. The exact information extracted
2185 // is determined by the nature of the dependency which is determined by the tag.
2186 scopeTag.extractDepInfo(ctx, to, scopePaths)
Paul Duffineedc5d52020-06-12 17:46:39 +01002187 } else if tag == implLibraryTag {
2188 if implLibrary, ok := to.(*Library); ok {
2189 module.implLibraryModule = implLibrary
2190 } else {
2191 ctx.ModuleErrorf("implementation library must be of type *java.Library but was %T", to)
2192 }
2193 } else if tag == xmlPermissionsFileTag {
2194 if xmlPermissionsFileModule, ok := to.(*sdkLibraryXml); ok {
2195 module.xmlPermissionsFileModule = xmlPermissionsFileModule
2196 } else {
2197 ctx.ModuleErrorf("xml permissions file module must be of type *sdkLibraryXml but was %T", to)
2198 }
Colin Cross79c7c262019-04-17 11:11:46 -07002199 }
2200 })
Paul Duffin0f8faff2020-05-20 16:18:00 +01002201
2202 // Populate the scope paths with information from the properties.
2203 for apiScope, scopeProperties := range module.scopeProperties {
2204 if len(scopeProperties.Jars) == 0 {
2205 continue
2206 }
2207
2208 paths := module.getScopePathsCreateIfNeeded(apiScope)
Anton Hanssond78eb762021-09-21 15:25:12 +01002209 paths.annotationsZip = android.OptionalPathForModuleSrc(ctx, scopeProperties.Annotations)
Paul Duffin0f8faff2020-05-20 16:18:00 +01002210 paths.currentApiFilePath = android.OptionalPathForModuleSrc(ctx, scopeProperties.Current_api)
2211 paths.removedApiFilePath = android.OptionalPathForModuleSrc(ctx, scopeProperties.Removed_api)
2212 }
Paul Duffin39853512021-02-26 11:09:39 +00002213
2214 if ctx.Device() {
2215 // If this is a variant created for a prebuilt_apex then use the dex implementation jar
2216 // obtained from the associated deapexer module.
2217 ai := ctx.Provider(android.ApexInfoProvider).(android.ApexInfo)
2218 if ai.ForPrebuiltApex {
Paul Duffin39853512021-02-26 11:09:39 +00002219 // Get the path of the dex implementation jar from the `deapexer` module.
Martin Stjernholm44825602021-09-17 01:44:12 +01002220 di := android.FindDeapexerProviderForModule(ctx)
2221 if di == nil {
2222 return // An error has been reported by FindDeapexerProviderForModule.
2223 }
Paul Duffinb4bbf2c2021-06-17 15:59:07 +01002224 if dexOutputPath := di.PrebuiltExportPath(apexRootRelativePathToJavaLib(module.BaseModuleName())); dexOutputPath != nil {
Martin Stjernholm8be1e6d2021-09-15 03:34:04 +01002225 dexJarFile := makeDexJarPathFromPath(dexOutputPath)
2226 module.dexJarFile = dexJarFile
Jiakai Zhang204356f2021-09-09 08:12:46 +00002227 installPath := android.PathForModuleInPartitionInstall(
2228 ctx, "apex", ai.ApexVariationName, apexRootRelativePathToJavaLib(module.BaseModuleName()))
2229 module.installFile = installPath
Martin Stjernholm8be1e6d2021-09-15 03:34:04 +01002230 module.initHiddenAPI(ctx, dexJarFile, module.findScopePaths(apiScopePublic).stubsImplPath[0], nil)
Jiakai Zhang204356f2021-09-09 08:12:46 +00002231
2232 // Dexpreopting.
2233 module.dexpreopter.installPath = module.dexpreopter.getInstallPath(ctx, installPath)
2234 module.dexpreopter.isSDKLibrary = true
2235 module.dexpreopter.uncompressedDex = shouldUncompressDex(ctx, &module.dexpreopter)
2236 module.dexpreopt(ctx, dexOutputPath)
Paul Duffin39853512021-02-26 11:09:39 +00002237 } else {
2238 // This should never happen as a variant for a prebuilt_apex is only created if the
2239 // prebuilt_apex has been configured to export the java library dex file.
Martin Stjernholm44825602021-09-17 01:44:12 +01002240 ctx.ModuleErrorf("internal error: no dex implementation jar available from prebuilt APEX %s", di.ApexModuleName())
Paul Duffin39853512021-02-26 11:09:39 +00002241 }
2242 }
2243 }
Colin Cross79c7c262019-04-17 11:11:46 -07002244}
2245
Jiyong Parkf1691d22021-03-29 20:11:58 +09002246func (module *SdkLibraryImport) sdkJars(ctx android.BaseModuleContext, sdkVersion android.SdkSpec, headerJars bool) android.Paths {
Paul Duffineedc5d52020-06-12 17:46:39 +01002247
2248 // For consistency with SdkLibrary make the implementation jar available to libraries that
2249 // are within the same APEX.
2250 implLibraryModule := module.implLibraryModule
Colin Cross56a83212020-09-15 18:30:11 -07002251 if implLibraryModule != nil && withinSameApexesAs(ctx, module) {
Paul Duffineedc5d52020-06-12 17:46:39 +01002252 if headerJars {
2253 return implLibraryModule.HeaderJars()
2254 } else {
2255 return implLibraryModule.ImplementationJars()
2256 }
2257 }
2258
Paul Duffin23970f42020-05-20 14:20:02 +01002259 return module.selectHeaderJarsForSdkVersion(ctx, sdkVersion)
Paul Duffin56d44902020-01-31 13:36:25 +00002260}
2261
Colin Cross79c7c262019-04-17 11:11:46 -07002262// to satisfy SdkLibraryDependency interface
Jiyong Parkf1691d22021-03-29 20:11:58 +09002263func (module *SdkLibraryImport) SdkHeaderJars(ctx android.BaseModuleContext, sdkVersion android.SdkSpec) android.Paths {
Colin Cross79c7c262019-04-17 11:11:46 -07002264 // This module is just a wrapper for the prebuilt stubs.
Paul Duffineedc5d52020-06-12 17:46:39 +01002265 return module.sdkJars(ctx, sdkVersion, true)
Colin Cross79c7c262019-04-17 11:11:46 -07002266}
2267
2268// to satisfy SdkLibraryDependency interface
Jiyong Parkf1691d22021-03-29 20:11:58 +09002269func (module *SdkLibraryImport) SdkImplementationJars(ctx android.BaseModuleContext, sdkVersion android.SdkSpec) android.Paths {
Colin Cross79c7c262019-04-17 11:11:46 -07002270 // This module is just a wrapper for the stubs.
Paul Duffineedc5d52020-06-12 17:46:39 +01002271 return module.sdkJars(ctx, sdkVersion, false)
2272}
2273
Ulya Trafimovichdbf31662020-12-17 12:07:54 +00002274// to satisfy UsesLibraryDependency interface
Martin Stjernholm8be1e6d2021-09-15 03:34:04 +01002275func (module *SdkLibraryImport) DexJarBuildPath() OptionalDexJarPath {
Paul Duffin39853512021-02-26 11:09:39 +00002276 // The dex implementation jar extracted from the .apex file should be used in preference to the
2277 // source.
Martin Stjernholm8be1e6d2021-09-15 03:34:04 +01002278 if module.dexJarFile.IsSet() {
Paul Duffin39853512021-02-26 11:09:39 +00002279 return module.dexJarFile
2280 }
Paul Duffineedc5d52020-06-12 17:46:39 +01002281 if module.implLibraryModule == nil {
Martin Stjernholm8be1e6d2021-09-15 03:34:04 +01002282 return makeUnsetDexJarPath()
Paul Duffineedc5d52020-06-12 17:46:39 +01002283 } else {
2284 return module.implLibraryModule.DexJarBuildPath()
2285 }
2286}
2287
Ulya Trafimovichdbf31662020-12-17 12:07:54 +00002288// to satisfy UsesLibraryDependency interface
Ulya Trafimovich31e444e2020-08-14 17:32:16 +01002289func (module *SdkLibraryImport) DexJarInstallPath() android.Path {
Jeongik Chad5fe8782021-07-08 01:13:11 +09002290 return module.installFile
Ulya Trafimovich31e444e2020-08-14 17:32:16 +01002291}
2292
Ulya Trafimovichdbf31662020-12-17 12:07:54 +00002293// to satisfy UsesLibraryDependency interface
2294func (module *SdkLibraryImport) ClassLoaderContexts() dexpreopt.ClassLoaderContextMap {
2295 return nil
2296}
2297
Paul Duffineedc5d52020-06-12 17:46:39 +01002298// to satisfy apex.javaDependency interface
2299func (module *SdkLibraryImport) JacocoReportClassesFile() android.Path {
2300 if module.implLibraryModule == nil {
2301 return nil
2302 } else {
2303 return module.implLibraryModule.JacocoReportClassesFile()
2304 }
2305}
2306
2307// to satisfy apex.javaDependency interface
Colin Cross08dca382020-07-21 20:31:17 -07002308func (module *SdkLibraryImport) LintDepSets() LintDepSets {
2309 if module.implLibraryModule == nil {
2310 return LintDepSets{}
2311 } else {
2312 return module.implLibraryModule.LintDepSets()
2313 }
2314}
2315
Jaewoong Jung476b9d62021-05-10 15:30:00 -07002316func (module *SdkLibraryImport) getStrictUpdatabilityLinting() bool {
2317 if module.implLibraryModule == nil {
2318 return false
2319 } else {
2320 return module.implLibraryModule.getStrictUpdatabilityLinting()
2321 }
2322}
2323
2324func (module *SdkLibraryImport) setStrictUpdatabilityLinting(strictLinting bool) {
2325 if module.implLibraryModule != nil {
2326 module.implLibraryModule.setStrictUpdatabilityLinting(strictLinting)
2327 }
2328}
2329
Colin Cross08dca382020-07-21 20:31:17 -07002330// to satisfy apex.javaDependency interface
Paul Duffineedc5d52020-06-12 17:46:39 +01002331func (module *SdkLibraryImport) Stem() string {
2332 return module.BaseModuleName()
Colin Cross79c7c262019-04-17 11:11:46 -07002333}
Jiyong Parke3833882020-02-17 17:28:10 +09002334
Paul Duffin44b481b2020-06-17 16:59:43 +01002335var _ ApexDependency = (*SdkLibraryImport)(nil)
2336
2337// to satisfy java.ApexDependency interface
2338func (module *SdkLibraryImport) HeaderJars() android.Paths {
2339 if module.implLibraryModule == nil {
2340 return nil
2341 } else {
2342 return module.implLibraryModule.HeaderJars()
2343 }
2344}
2345
2346// to satisfy java.ApexDependency interface
2347func (module *SdkLibraryImport) ImplementationAndResourcesJars() android.Paths {
2348 if module.implLibraryModule == nil {
2349 return nil
2350 } else {
2351 return module.implLibraryModule.ImplementationAndResourcesJars()
2352 }
2353}
2354
Jiakai Zhang204356f2021-09-09 08:12:46 +00002355// to satisfy java.DexpreopterInterface interface
2356func (module *SdkLibraryImport) IsInstallable() bool {
2357 return true
2358}
2359
Paul Duffinfef55002021-06-17 14:56:05 +01002360var _ android.RequiredFilesFromPrebuiltApex = (*SdkLibraryImport)(nil)
2361
Paul Duffinb4bbf2c2021-06-17 15:59:07 +01002362func (module *SdkLibraryImport) RequiredFilesFromPrebuiltApex(ctx android.BaseModuleContext) []string {
Paul Duffinfef55002021-06-17 14:56:05 +01002363 name := module.BaseModuleName()
2364 return requiredFilesFromPrebuiltApexForImport(name)
2365}
2366
Jiyong Parke3833882020-02-17 17:28:10 +09002367//
2368// java_sdk_library_xml
2369//
2370type sdkLibraryXml struct {
2371 android.ModuleBase
2372 android.DefaultableModuleBase
2373 android.ApexModuleBase
2374
2375 properties sdkLibraryXmlProperties
2376
2377 outputFilePath android.OutputPath
2378 installDirPath android.InstallPath
Colin Cross56a83212020-09-15 18:30:11 -07002379
2380 hideApexVariantFromMake bool
Jiyong Parke3833882020-02-17 17:28:10 +09002381}
2382
2383type sdkLibraryXmlProperties struct {
2384 // canonical name of the lib
2385 Lib_name *string
2386}
2387
2388// java_sdk_library_xml builds the permission xml file for a java_sdk_library.
2389// Not to be used directly by users. java_sdk_library internally uses this.
2390func sdkLibraryXmlFactory() android.Module {
2391 module := &sdkLibraryXml{}
2392
2393 module.AddProperties(&module.properties)
2394
2395 android.InitApexModule(module)
2396 android.InitAndroidArchModule(module, android.DeviceSupported, android.MultilibCommon)
2397
2398 return module
2399}
2400
Colin Crossaede88c2020-08-11 12:17:01 -07002401func (module *sdkLibraryXml) UniqueApexVariations() bool {
2402 // sdkLibraryXml needs a unique variation per APEX because the generated XML file contains the path to the
2403 // mounted APEX, which contains the name of the APEX.
2404 return true
2405}
2406
Jiyong Parke3833882020-02-17 17:28:10 +09002407// from android.PrebuiltEtcModule
Jooyung Han0703fd82020-08-26 22:11:53 +09002408func (module *sdkLibraryXml) BaseDir() string {
2409 return "etc"
2410}
2411
2412// from android.PrebuiltEtcModule
Jiyong Parke3833882020-02-17 17:28:10 +09002413func (module *sdkLibraryXml) SubDir() string {
2414 return "permissions"
2415}
2416
2417// from android.PrebuiltEtcModule
2418func (module *sdkLibraryXml) OutputFile() android.OutputPath {
2419 return module.outputFilePath
2420}
2421
2422// from android.ApexModule
2423func (module *sdkLibraryXml) AvailableFor(what string) bool {
2424 return true
2425}
2426
2427func (module *sdkLibraryXml) DepsMutator(ctx android.BottomUpMutatorContext) {
2428 // do nothing
2429}
2430
Jiyong Park45bf82e2020-12-15 22:29:02 +09002431var _ android.ApexModule = (*sdkLibraryXml)(nil)
2432
2433// Implements android.ApexModule
Dan Albertc8060532020-07-22 22:32:17 -07002434func (module *sdkLibraryXml) ShouldSupportSdkVersion(ctx android.BaseModuleContext,
2435 sdkVersion android.ApiLevel) error {
Jooyung Han749dc692020-04-15 11:03:39 +09002436 // sdkLibraryXml doesn't need to be checked separately because java_sdk_library is checked
2437 return nil
2438}
2439
Jiyong Parke3833882020-02-17 17:28:10 +09002440// File path to the runtime implementation library
Colin Cross56a83212020-09-15 18:30:11 -07002441func (module *sdkLibraryXml) implPath(ctx android.ModuleContext) string {
Jiyong Parke3833882020-02-17 17:28:10 +09002442 implName := proptools.String(module.properties.Lib_name)
Colin Cross56a83212020-09-15 18:30:11 -07002443 if apexInfo := ctx.Provider(android.ApexInfoProvider).(android.ApexInfo); !apexInfo.IsForPlatform() {
Colin Crosse07f2312020-08-13 11:24:56 -07002444 // TODO(b/146468504): ApexVariationName() is only a soong module name, not apex name.
Jiyong Parke3833882020-02-17 17:28:10 +09002445 // In most cases, this works fine. But when apex_name is set or override_apex is used
2446 // this can be wrong.
Colin Cross56a83212020-09-15 18:30:11 -07002447 return fmt.Sprintf("/apex/%s/javalib/%s.jar", apexInfo.ApexVariationName, implName)
Jiyong Parke3833882020-02-17 17:28:10 +09002448 }
2449 partition := "system"
2450 if module.SocSpecific() {
2451 partition = "vendor"
2452 } else if module.DeviceSpecific() {
2453 partition = "odm"
2454 } else if module.ProductSpecific() {
2455 partition = "product"
2456 } else if module.SystemExtSpecific() {
2457 partition = "system_ext"
2458 }
2459 return "/" + partition + "/framework/" + implName + ".jar"
2460}
2461
2462func (module *sdkLibraryXml) GenerateAndroidBuildActions(ctx android.ModuleContext) {
Colin Cross56a83212020-09-15 18:30:11 -07002463 module.hideApexVariantFromMake = !ctx.Provider(android.ApexInfoProvider).(android.ApexInfo).IsForPlatform()
2464
Jiyong Parke3833882020-02-17 17:28:10 +09002465 libName := proptools.String(module.properties.Lib_name)
Colin Cross56a83212020-09-15 18:30:11 -07002466 xmlContent := fmt.Sprintf(permissionsTemplate, libName, module.implPath(ctx))
Jiyong Parke3833882020-02-17 17:28:10 +09002467
2468 module.outputFilePath = android.PathForModuleOut(ctx, libName+".xml").OutputPath
Colin Crossf1a035e2020-11-16 17:32:30 -08002469 rule := android.NewRuleBuilder(pctx, ctx)
Jiyong Parke3833882020-02-17 17:28:10 +09002470 rule.Command().
2471 Text("/bin/bash -c \"echo -e '" + xmlContent + "'\" > ").
2472 Output(module.outputFilePath)
2473
Colin Crossf1a035e2020-11-16 17:32:30 -08002474 rule.Build("java_sdk_xml", "Permission XML")
Jiyong Parke3833882020-02-17 17:28:10 +09002475
2476 module.installDirPath = android.PathForModuleInstall(ctx, "etc", module.SubDir())
2477}
2478
2479func (module *sdkLibraryXml) AndroidMkEntries() []android.AndroidMkEntries {
Colin Cross56a83212020-09-15 18:30:11 -07002480 if module.hideApexVariantFromMake {
Jiyong Parke3833882020-02-17 17:28:10 +09002481 return []android.AndroidMkEntries{android.AndroidMkEntries{
2482 Disabled: true,
2483 }}
2484 }
2485
2486 return []android.AndroidMkEntries{android.AndroidMkEntries{
2487 Class: "ETC",
2488 OutputFile: android.OptionalPathForPath(module.outputFilePath),
2489 ExtraEntries: []android.AndroidMkExtraEntriesFunc{
Colin Crossaa255532020-07-03 13:18:24 -07002490 func(ctx android.AndroidMkExtraEntriesContext, entries *android.AndroidMkEntries) {
Jiyong Parke3833882020-02-17 17:28:10 +09002491 entries.SetString("LOCAL_MODULE_TAGS", "optional")
2492 entries.SetString("LOCAL_MODULE_PATH", module.installDirPath.ToMakePath().String())
2493 entries.SetString("LOCAL_INSTALLED_MODULE_STEM", module.outputFilePath.Base())
2494 },
2495 },
2496 }}
2497}
Paul Duffindd46f712020-02-10 13:37:10 +00002498
2499type sdkLibrarySdkMemberType struct {
2500 android.SdkMemberTypeBase
2501}
2502
Paul Duffin296701e2021-07-14 10:29:36 +01002503func (s *sdkLibrarySdkMemberType) AddDependencies(ctx android.SdkDependencyContext, dependencyTag blueprint.DependencyTag, names []string) {
2504 ctx.AddVariationDependencies(nil, dependencyTag, names...)
Paul Duffindd46f712020-02-10 13:37:10 +00002505}
2506
2507func (s *sdkLibrarySdkMemberType) IsInstance(module android.Module) bool {
2508 _, ok := module.(*SdkLibrary)
2509 return ok
2510}
2511
2512func (s *sdkLibrarySdkMemberType) AddPrebuiltModule(ctx android.SdkMemberContext, member android.SdkMember) android.BpModule {
2513 return ctx.SnapshotBuilder().AddPrebuiltModule(member, "java_sdk_library_import")
2514}
2515
2516func (s *sdkLibrarySdkMemberType) CreateVariantPropertiesStruct() android.SdkMemberProperties {
2517 return &sdkLibrarySdkMemberProperties{}
2518}
2519
Paul Duffin976b0e52021-04-27 23:20:26 +01002520var javaSdkLibrarySdkMemberType = &sdkLibrarySdkMemberType{
2521 android.SdkMemberTypeBase{
2522 PropertyName: "java_sdk_libs",
2523 SupportsSdk: true,
2524 },
2525}
2526
Paul Duffindd46f712020-02-10 13:37:10 +00002527type sdkLibrarySdkMemberProperties struct {
2528 android.SdkMemberPropertiesBase
2529
2530 // Scope to per scope properties.
2531 Scopes map[*apiScope]scopeProperties
2532
Paul Duffin3d1248c2020-04-09 00:10:17 +01002533 // The Java stubs source files.
2534 Stub_srcs []string
Paul Duffinf7a64332020-05-13 16:54:55 +01002535
2536 // The naming scheme.
2537 Naming_scheme *string
Paul Duffind7eb1c22020-05-26 20:57:10 +01002538
2539 // True if the java_sdk_library_import is for a shared library, false
2540 // otherwise.
2541 Shared_library *bool
Paul Duffina2ae7e02020-09-11 11:55:00 +01002542
Paul Duffin1267d872021-04-16 17:21:36 +01002543 // True if the stub imports should produce dex jars.
2544 Compile_dex *bool
2545
Paul Duffina2ae7e02020-09-11 11:55:00 +01002546 // The paths to the doctag files to add to the prebuilt.
2547 Doctag_paths android.Paths
Paul Duffin869de142021-07-15 14:14:41 +01002548
2549 Permitted_packages []string
Paul Duffindd46f712020-02-10 13:37:10 +00002550}
2551
2552type scopeProperties struct {
Paul Duffin1fd005d2020-04-09 01:08:11 +01002553 Jars android.Paths
2554 StubsSrcJar android.Path
2555 CurrentApiFile android.Path
2556 RemovedApiFile android.Path
Anton Hanssond78eb762021-09-21 15:25:12 +01002557 AnnotationsZip android.Path
Paul Duffin1fd005d2020-04-09 01:08:11 +01002558 SdkVersion string
Paul Duffindd46f712020-02-10 13:37:10 +00002559}
2560
2561func (s *sdkLibrarySdkMemberProperties) PopulateFromVariant(ctx android.SdkMemberContext, variant android.Module) {
2562 sdk := variant.(*SdkLibrary)
2563
2564 s.Scopes = make(map[*apiScope]scopeProperties)
2565 for _, apiScope := range allApiScopes {
Paul Duffin803a9562020-05-20 11:52:25 +01002566 paths := sdk.findScopePaths(apiScope)
2567 if paths == nil {
2568 continue
2569 }
2570
Paul Duffindd46f712020-02-10 13:37:10 +00002571 jars := paths.stubsImplPath
2572 if len(jars) > 0 {
2573 properties := scopeProperties{}
2574 properties.Jars = jars
Paul Duffin780c5f42020-05-12 15:52:55 +01002575 properties.SdkVersion = sdk.sdkVersionForStubsLibrary(ctx.SdkModuleContext(), apiScope)
Paul Duffin0f8faff2020-05-20 16:18:00 +01002576 properties.StubsSrcJar = paths.stubsSrcJar.Path()
Paul Duffin10269f12020-06-19 18:39:55 +01002577 if paths.currentApiFilePath.Valid() {
2578 properties.CurrentApiFile = paths.currentApiFilePath.Path()
2579 }
2580 if paths.removedApiFilePath.Valid() {
2581 properties.RemovedApiFile = paths.removedApiFilePath.Path()
2582 }
Anton Hanssond78eb762021-09-21 15:25:12 +01002583 // The annotations zip is only available for modules that set annotations_enabled: true.
2584 if paths.annotationsZip.Valid() {
2585 properties.AnnotationsZip = paths.annotationsZip.Path()
2586 }
Paul Duffindd46f712020-02-10 13:37:10 +00002587 s.Scopes[apiScope] = properties
2588 }
2589 }
2590
Paul Duffindfa131e2020-05-15 20:37:11 +01002591 s.Naming_scheme = sdk.commonSdkLibraryProperties.Naming_scheme
Paul Duffind7eb1c22020-05-26 20:57:10 +01002592 s.Shared_library = proptools.BoolPtr(sdk.sharedLibrary())
Paul Duffin1267d872021-04-16 17:21:36 +01002593 s.Compile_dex = sdk.dexProperties.Compile_dex
Paul Duffina2ae7e02020-09-11 11:55:00 +01002594 s.Doctag_paths = sdk.doctagPaths
Paul Duffin869de142021-07-15 14:14:41 +01002595 s.Permitted_packages = sdk.PermittedPackagesForUpdatableBootJars()
Paul Duffindd46f712020-02-10 13:37:10 +00002596}
2597
2598func (s *sdkLibrarySdkMemberProperties) AddToPropertySet(ctx android.SdkMemberContext, propertySet android.BpPropertySet) {
Paul Duffinf7a64332020-05-13 16:54:55 +01002599 if s.Naming_scheme != nil {
2600 propertySet.AddProperty("naming_scheme", proptools.String(s.Naming_scheme))
2601 }
Paul Duffind7eb1c22020-05-26 20:57:10 +01002602 if s.Shared_library != nil {
2603 propertySet.AddProperty("shared_library", *s.Shared_library)
2604 }
Paul Duffin1267d872021-04-16 17:21:36 +01002605 if s.Compile_dex != nil {
2606 propertySet.AddProperty("compile_dex", *s.Compile_dex)
2607 }
Paul Duffin869de142021-07-15 14:14:41 +01002608 if len(s.Permitted_packages) > 0 {
2609 propertySet.AddProperty("permitted_packages", s.Permitted_packages)
2610 }
Paul Duffinf7a64332020-05-13 16:54:55 +01002611
Paul Duffindd46f712020-02-10 13:37:10 +00002612 for _, apiScope := range allApiScopes {
2613 if properties, ok := s.Scopes[apiScope]; ok {
Paul Duffin6b836ba2020-05-13 19:19:49 +01002614 scopeSet := propertySet.AddPropertySet(apiScope.propertyName)
Paul Duffindd46f712020-02-10 13:37:10 +00002615
Paul Duffin3d1248c2020-04-09 00:10:17 +01002616 scopeDir := filepath.Join("sdk_library", s.OsPrefix(), apiScope.name)
2617
Paul Duffindd46f712020-02-10 13:37:10 +00002618 var jars []string
2619 for _, p := range properties.Jars {
Paul Duffin3d1248c2020-04-09 00:10:17 +01002620 dest := filepath.Join(scopeDir, ctx.Name()+"-stubs.jar")
Paul Duffindd46f712020-02-10 13:37:10 +00002621 ctx.SnapshotBuilder().CopyToSnapshot(p, dest)
2622 jars = append(jars, dest)
2623 }
2624 scopeSet.AddProperty("jars", jars)
2625
Paul Duffin22628d52021-05-12 23:13:22 +01002626 if ctx.SdkModuleContext().Config().IsEnvTrue("SOONG_SDK_SNAPSHOT_USE_SRCJAR") {
2627 // Copy the stubs source jar into the snapshot zip as is.
2628 srcJarSnapshotPath := filepath.Join(scopeDir, ctx.Name()+".srcjar")
2629 ctx.SnapshotBuilder().CopyToSnapshot(properties.StubsSrcJar, srcJarSnapshotPath)
2630 scopeSet.AddProperty("stub_srcs", []string{srcJarSnapshotPath})
2631 } else {
2632 // Merge the stubs source jar into the snapshot zip so that when it is unpacked
2633 // the source files are also unpacked.
2634 snapshotRelativeDir := filepath.Join(scopeDir, ctx.Name()+"_stub_sources")
2635 ctx.SnapshotBuilder().UnzipToSnapshot(properties.StubsSrcJar, snapshotRelativeDir)
2636 scopeSet.AddProperty("stub_srcs", []string{snapshotRelativeDir})
2637 }
Paul Duffin3d1248c2020-04-09 00:10:17 +01002638
Paul Duffin1fd005d2020-04-09 01:08:11 +01002639 if properties.CurrentApiFile != nil {
2640 currentApiSnapshotPath := filepath.Join(scopeDir, ctx.Name()+".txt")
2641 ctx.SnapshotBuilder().CopyToSnapshot(properties.CurrentApiFile, currentApiSnapshotPath)
2642 scopeSet.AddProperty("current_api", currentApiSnapshotPath)
2643 }
2644
2645 if properties.RemovedApiFile != nil {
2646 removedApiSnapshotPath := filepath.Join(scopeDir, ctx.Name()+"-removed.txt")
Paul Duffin3dbf9fd2020-06-02 13:00:02 +01002647 ctx.SnapshotBuilder().CopyToSnapshot(properties.RemovedApiFile, removedApiSnapshotPath)
Paul Duffin1fd005d2020-04-09 01:08:11 +01002648 scopeSet.AddProperty("removed_api", removedApiSnapshotPath)
2649 }
2650
Anton Hanssond78eb762021-09-21 15:25:12 +01002651 if properties.AnnotationsZip != nil {
2652 annotationsSnapshotPath := filepath.Join(scopeDir, ctx.Name()+"_annotations.zip")
2653 ctx.SnapshotBuilder().CopyToSnapshot(properties.AnnotationsZip, annotationsSnapshotPath)
2654 scopeSet.AddProperty("annotations", annotationsSnapshotPath)
2655 }
2656
Paul Duffindd46f712020-02-10 13:37:10 +00002657 if properties.SdkVersion != "" {
2658 scopeSet.AddProperty("sdk_version", properties.SdkVersion)
2659 }
2660 }
2661 }
2662
Paul Duffina2ae7e02020-09-11 11:55:00 +01002663 if len(s.Doctag_paths) > 0 {
2664 dests := []string{}
2665 for _, p := range s.Doctag_paths {
2666 dest := filepath.Join("doctags", p.Rel())
2667 ctx.SnapshotBuilder().CopyToSnapshot(p, dest)
2668 dests = append(dests, dest)
2669 }
2670 propertySet.AddProperty("doctag_files", dests)
2671 }
Paul Duffindd46f712020-02-10 13:37:10 +00002672}