blob: c6747c7305f5e88e218a0e31326adc45949cfe48 [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:
364 // 1) If the sdk_version specified on the java_sdk_library is none then this
365 // will be none. This is used for java_sdk_library instances that are used
366 // to create stubs that contribute to the core_current sdk version.
367 // 2) Otherwise, it is assumed that this library extends but does not contribute
368 // directly to a specific sdk_version and so this uses the sdk_version appropriate
369 // for the api scope. e.g. public will use sdk_version: current, system will use
370 // sdk_version: system_current, etc.
371 //
372 // This does not affect the sdk_version used for either generating the stubs source
373 // or the API file. They both have to use the same sdk_version as is used for
374 // compiling the implementation library.
375 Sdk_version *string
Paul Duffin3375e352020-04-28 10:44:03 +0100376}
377
Jiyong Parkc678ad32018-04-10 13:07:10 +0900378type sdkLibraryProperties struct {
Anton Hanssonb727c1e2021-09-16 14:24:13 +0100379 // List of source files that are needed to compile the API, but are not part of runtime library.
380 Api_srcs []string `android:"arch_variant"`
381
Paul Duffin5df79302020-05-16 15:52:12 +0100382 // Visibility for impl library module. If not specified then defaults to the
383 // visibility property.
384 Impl_library_visibility []string
385
Paul Duffin4911a892020-04-29 23:35:13 +0100386 // Visibility for stubs library modules. If not specified then defaults to the
387 // visibility property.
388 Stubs_library_visibility []string
389
390 // Visibility for stubs source modules. If not specified then defaults to the
391 // visibility property.
392 Stubs_source_visibility []string
393
Anton Hansson7f66efa2020-10-08 14:47:23 +0100394 // List of Java libraries that will be in the classpath when building the implementation lib
395 Impl_only_libs []string `android:"arch_variant"`
396
Sundong Ahnf043cf62018-06-25 16:04:37 +0900397 // List of Java libraries that will be in the classpath when building stubs
398 Stub_only_libs []string `android:"arch_variant"`
399
Anton Hanssondae54cd2021-04-21 16:30:10 +0100400 // List of Java libraries that will included in stub libraries
401 Stub_only_static_libs []string `android:"arch_variant"`
402
Paul Duffin7a586d32019-12-30 17:09:34 +0000403 // list of package names that will be documented and publicized as API.
404 // This allows the API to be restricted to a subset of the source files provided.
405 // If this is unspecified then all the source files will be treated as being part
406 // of the API.
Jiyong Parkc678ad32018-04-10 13:07:10 +0900407 Api_packages []string
408
Jiyong Park5a2c9d72018-05-01 22:25:41 +0900409 // list of package names that must be hidden from the API
410 Hidden_api_packages []string
411
Paul Duffin749f98f2019-12-30 17:23:46 +0000412 // the relative path to the directory containing the api specification files.
413 // Defaults to "api".
414 Api_dir *string
415
Paul Duffindfa131e2020-05-15 20:37:11 +0100416 // Determines whether a runtime implementation library is built; defaults to false.
417 //
418 // If true then it also prevents the module from being used as a shared module, i.e.
419 // it is as is shared_library: false, was set.
Paul Duffin43db9be2019-12-30 17:35:49 +0000420 Api_only *bool
421
Paul Duffin11512472019-02-11 15:55:17 +0000422 // local files that are used within user customized droiddoc options.
423 Droiddoc_option_files []string
424
425 // additional droiddoc options
426 // Available variables for substitution:
427 //
428 // $(location <label>): the path to the droiddoc_option_files with name <label>
Sundong Ahndd567f92018-07-31 17:19:11 +0900429 Droiddoc_options []string
430
Paul Duffine22c2ab2020-05-20 19:35:27 +0100431 // is set to true, Metalava will allow framework SDK to contain annotations.
432 Annotations_enabled *bool
433
Sundong Ahn054b19a2018-10-19 13:46:09 +0900434 // a list of top-level directories containing files to merge qualifier annotations
435 // (i.e. those intended to be included in the stubs written) from.
436 Merge_annotations_dirs []string
437
438 // a list of top-level directories containing Java stub files to merge show/hide annotations from.
439 Merge_inclusion_annotations_dirs []string
440
Paul Duffin4f5c1ef2020-11-19 14:53:43 +0000441 // If set to true then don't create dist rules.
442 No_dist *bool
Sundong Ahn80a87b32019-05-13 15:02:50 +0900443
Paul Duffin31310252020-11-20 21:26:20 +0000444 // The stem for the artifacts that are copied to the dist, if not specified
445 // then defaults to the base module name.
446 //
447 // For each scope the following artifacts are copied to the apistubs/<scope>
448 // directory in the dist.
449 // * stubs impl jar -> <dist-stem>.jar
450 // * API specification file -> api/<dist-stem>.txt
451 // * Removed API specification file -> api/<dist-stem>-removed.txt
452 //
453 // Also used to construct the name of the filegroup (created by prebuilt_apis)
454 // that references the latest released API and remove API specification files.
455 // * API specification filegroup -> <dist-stem>.api.<scope>.latest
456 // * Removed API specification filegroup -> <dist-stem>-removed.api.<scope>.latest
Jaewoong Jung1a97ee02021-03-09 13:25:02 -0800457 // * API incompatibilities baseline filegroup -> <dist-stem>-incompatibilities.api.<scope>.latest
Paul Duffin31310252020-11-20 21:26:20 +0000458 Dist_stem *string
459
Colin Cross0d3dd062021-06-01 13:13:40 -0700460 // The subdirectory for the artifacts that are copied to the dist directory. If not specified
Colin Cross0f9eeb72021-06-01 14:05:09 -0700461 // then defaults to "unknown". Should be set to "android" for anything that should be published
Colin Cross0d3dd062021-06-01 13:13:40 -0700462 // in the public Android SDK.
463 Dist_group *string
464
Anton Hanssondff2c782020-12-21 17:10:01 +0000465 // A compatibility mode that allows historical API-tracking files to not exist.
466 // Do not use.
467 Unsafe_ignore_missing_latest_api bool
468
Paul Duffin3375e352020-04-28 10:44:03 +0100469 // indicates whether system and test apis should be generated.
470 Generate_system_and_test_apis bool `blueprint:"mutated"`
471
472 // The properties specific to the public api scope
473 //
474 // Unless explicitly specified by using public.enabled the public api scope is
475 // enabled by default in both legacy and non-legacy mode.
476 Public ApiScopeProperties
477
478 // The properties specific to the system api scope
479 //
480 // In legacy mode the system api scope is enabled by default when sdk_version
481 // is set to something other than "none".
482 //
483 // In non-legacy mode the system api scope is disabled by default.
484 System ApiScopeProperties
485
486 // The properties specific to the test api scope
487 //
488 // In legacy mode the test api scope is enabled by default when sdk_version
489 // is set to something other than "none".
490 //
491 // In non-legacy mode the test api scope is disabled by default.
492 Test ApiScopeProperties
Paul Duffin37e0b772019-12-30 17:20:10 +0000493
Paul Duffin0c5bae52020-06-02 13:00:08 +0100494 // The properties specific to the module-lib api scope
Paul Duffin8f265b92020-04-28 14:13:56 +0100495 //
Paul Duffin0c5bae52020-06-02 13:00:08 +0100496 // Unless explicitly specified by using test.enabled the module-lib api scope is
Paul Duffin8f265b92020-04-28 14:13:56 +0100497 // disabled by default.
498 Module_lib ApiScopeProperties
499
Paul Duffin0c5bae52020-06-02 13:00:08 +0100500 // The properties specific to the system-server api scope
501 //
502 // Unless explicitly specified by using test.enabled the module-lib api scope is
503 // disabled by default.
504 System_server ApiScopeProperties
505
Jiyong Park932cdfe2020-05-28 00:19:53 +0900506 // Determines if the stubs are preferred over the implementation library
507 // for linking, even when the client doesn't specify sdk_version. When this
508 // is set to true, such clients are provided with the widest API surface that
509 // this lib provides. Note however that this option doesn't affect the clients
510 // that are in the same APEX as this library. In that case, the clients are
511 // always linked with the implementation library. Default is false.
512 Default_to_stubs *bool
513
Paul Duffin160fe412020-05-10 19:32:20 +0100514 // Properties related to api linting.
515 Api_lint struct {
516 // Enable api linting.
517 Enabled *bool
518 }
519
Jiyong Parkc678ad32018-04-10 13:07:10 +0900520 // TODO: determines whether to create HTML doc or not
521 //Html_doc *bool
522}
523
Paul Duffin0f8faff2020-05-20 16:18:00 +0100524// Paths to outputs from java_sdk_library and java_sdk_library_import.
525//
526// Fields that are android.Paths are always set (during GenerateAndroidBuildActions).
527// OptionalPaths are always set by java_sdk_library but may not be set by
528// java_sdk_library_import as not all instances provide that information.
Paul Duffind1b3a922020-01-22 11:57:20 +0000529type scopePaths struct {
Paul Duffin0f8faff2020-05-20 16:18:00 +0100530 // The path (represented as Paths for convenience when returning) to the stubs header jar.
531 //
532 // That is the jar that is created by turbine.
533 stubsHeaderPath android.Paths
534
535 // The path (represented as Paths for convenience when returning) to the stubs implementation jar.
536 //
537 // This is not the implementation jar, it still only contains stubs.
538 stubsImplPath android.Paths
539
Paul Duffin1267d872021-04-16 17:21:36 +0100540 // The dex jar for the stubs.
541 //
542 // This is not the implementation jar, it still only contains stubs.
543 stubsDexJarPath android.Path
544
Paul Duffin0f8faff2020-05-20 16:18:00 +0100545 // The API specification file, e.g. system_current.txt.
546 currentApiFilePath android.OptionalPath
547
548 // The specification of API elements removed since the last release.
549 removedApiFilePath android.OptionalPath
550
551 // The stubs source jar.
552 stubsSrcJar android.OptionalPath
Anton Hansson3adf3c52021-09-21 15:25:12 +0100553
554 // Extracted annotations.
555 annotationsZip android.OptionalPath
Paul Duffind1b3a922020-01-22 11:57:20 +0000556}
557
Colin Crossdcf71b22021-02-01 13:59:03 -0800558func (paths *scopePaths) extractStubsLibraryInfoFromDependency(ctx android.ModuleContext, dep android.Module) error {
559 if ctx.OtherModuleHasProvider(dep, JavaInfoProvider) {
560 lib := ctx.OtherModuleProvider(dep, JavaInfoProvider).(JavaInfo)
561 paths.stubsHeaderPath = lib.HeaderJars
562 paths.stubsImplPath = lib.ImplementationJars
Paul Duffin1267d872021-04-16 17:21:36 +0100563
564 libDep := dep.(UsesLibraryDependency)
565 paths.stubsDexJarPath = libDep.DexJarBuildPath()
Paul Duffinc8782502020-04-29 20:45:27 +0100566 return nil
567 } else {
Colin Crossdcf71b22021-02-01 13:59:03 -0800568 return fmt.Errorf("expected module that has JavaInfoProvider, e.g. java_library")
Paul Duffinc8782502020-04-29 20:45:27 +0100569 }
570}
571
Paul Duffin0ff08bd2020-04-29 13:30:54 +0100572func (paths *scopePaths) treatDepAsApiStubsProvider(dep android.Module, action func(provider ApiStubsProvider)) error {
573 if apiStubsProvider, ok := dep.(ApiStubsProvider); ok {
574 action(apiStubsProvider)
Paul Duffinc8782502020-04-29 20:45:27 +0100575 return nil
576 } else {
577 return fmt.Errorf("expected module that implements ApiStubsProvider, e.g. droidstubs")
578 }
579}
580
Paul Duffin0f8faff2020-05-20 16:18:00 +0100581func (paths *scopePaths) treatDepAsApiStubsSrcProvider(dep android.Module, action func(provider ApiStubsSrcProvider)) error {
582 if apiStubsProvider, ok := dep.(ApiStubsSrcProvider); ok {
583 action(apiStubsProvider)
584 return nil
585 } else {
586 return fmt.Errorf("expected module that implements ApiStubsSrcProvider, e.g. droidstubs")
587 }
588}
589
Paul Duffin0ff08bd2020-04-29 13:30:54 +0100590func (paths *scopePaths) extractApiInfoFromApiStubsProvider(provider ApiStubsProvider) {
Anton Hansson3adf3c52021-09-21 15:25:12 +0100591 paths.annotationsZip = android.OptionalPathForPath(provider.AnnotationsZip())
Paul Duffin0f8faff2020-05-20 16:18:00 +0100592 paths.currentApiFilePath = android.OptionalPathForPath(provider.ApiFilePath())
593 paths.removedApiFilePath = android.OptionalPathForPath(provider.RemovedApiFilePath())
Paul Duffin0ff08bd2020-04-29 13:30:54 +0100594}
595
Colin Crossdcf71b22021-02-01 13:59:03 -0800596func (paths *scopePaths) extractApiInfoFromDep(ctx android.ModuleContext, dep android.Module) error {
Paul Duffin0ff08bd2020-04-29 13:30:54 +0100597 return paths.treatDepAsApiStubsProvider(dep, func(provider ApiStubsProvider) {
598 paths.extractApiInfoFromApiStubsProvider(provider)
599 })
600}
601
Paul Duffin0f8faff2020-05-20 16:18:00 +0100602func (paths *scopePaths) extractStubsSourceInfoFromApiStubsProviders(provider ApiStubsSrcProvider) {
603 paths.stubsSrcJar = android.OptionalPathForPath(provider.StubsSrcJar())
Paul Duffin0ff08bd2020-04-29 13:30:54 +0100604}
605
Colin Crossdcf71b22021-02-01 13:59:03 -0800606func (paths *scopePaths) extractStubsSourceInfoFromDep(ctx android.ModuleContext, dep android.Module) error {
Paul Duffin0f8faff2020-05-20 16:18:00 +0100607 return paths.treatDepAsApiStubsSrcProvider(dep, func(provider ApiStubsSrcProvider) {
Paul Duffin0ff08bd2020-04-29 13:30:54 +0100608 paths.extractStubsSourceInfoFromApiStubsProviders(provider)
609 })
610}
611
Colin Crossdcf71b22021-02-01 13:59:03 -0800612func (paths *scopePaths) extractStubsSourceAndApiInfoFromApiStubsProvider(ctx android.ModuleContext, dep android.Module) error {
Paul Duffin0ff08bd2020-04-29 13:30:54 +0100613 return paths.treatDepAsApiStubsProvider(dep, func(provider ApiStubsProvider) {
614 paths.extractApiInfoFromApiStubsProvider(provider)
615 paths.extractStubsSourceInfoFromApiStubsProviders(provider)
616 })
617}
618
619type commonToSdkLibraryAndImportProperties struct {
Paul Duffin1b1e8062020-05-08 13:44:43 +0100620 // The naming scheme to use for the components that this module creates.
621 //
Paul Duffinee9ad5d2020-09-11 13:04:05 +0100622 // If not specified then it defaults to "default".
Paul Duffin1b1e8062020-05-08 13:44:43 +0100623 //
624 // This is a temporary mechanism to simplify conversion from separate modules for each
625 // component that follow a different naming pattern to the default one.
626 //
627 // TODO(b/155480189) - Remove once naming inconsistencies have been resolved.
Paul Duffin0ff08bd2020-04-29 13:30:54 +0100628 Naming_scheme *string
Paul Duffindfa131e2020-05-15 20:37:11 +0100629
630 // Specifies whether this module can be used as an Android shared library; defaults
631 // to true.
632 //
633 // An Android shared library is one that can be referenced in a <uses-library> element
634 // in an AndroidManifest.xml.
635 Shared_library *bool
Paul Duffina2ae7e02020-09-11 11:55:00 +0100636
637 // Files containing information about supported java doc tags.
638 Doctag_files []string `android:"path"`
Paul Duffin0ff08bd2020-04-29 13:30:54 +0100639}
640
Paul Duffin3accbb52021-06-23 11:39:47 +0100641// commonSdkLibraryAndImportModule defines the interface that must be provided by a module that
642// embeds the commonToSdkLibraryAndImport struct.
643type commonSdkLibraryAndImportModule interface {
Paul Duffina1aa7382021-04-29 21:50:40 +0100644 android.SdkAware
Paul Duffin3accbb52021-06-23 11:39:47 +0100645
646 BaseModuleName() string
647}
648
Paul Duffin56d44902020-01-31 13:36:25 +0000649// Common code between sdk library and sdk library import
650type commonToSdkLibraryAndImport struct {
Paul Duffin3accbb52021-06-23 11:39:47 +0100651 module commonSdkLibraryAndImportModule
Paul Duffinc3091c82020-05-08 14:16:20 +0100652
Paul Duffin56d44902020-01-31 13:36:25 +0000653 scopePaths map[*apiScope]*scopePaths
Paul Duffin1b1e8062020-05-08 13:44:43 +0100654
655 namingScheme sdkLibraryComponentNamingScheme
656
Paul Duffindfa131e2020-05-15 20:37:11 +0100657 commonSdkLibraryProperties commonToSdkLibraryAndImportProperties
Paul Duffin859fe962020-05-15 10:20:31 +0100658
Paul Duffina2ae7e02020-09-11 11:55:00 +0100659 // Paths to commonSdkLibraryProperties.Doctag_files
660 doctagPaths android.Paths
661
Paul Duffin859fe962020-05-15 10:20:31 +0100662 // Functionality related to this being used as a component of a java_sdk_library.
663 EmbeddableSdkLibraryComponent
Paul Duffin56d44902020-01-31 13:36:25 +0000664}
665
Paul Duffin3accbb52021-06-23 11:39:47 +0100666func (c *commonToSdkLibraryAndImport) initCommon(module commonSdkLibraryAndImportModule) {
667 c.module = module
Paul Duffin1b1e8062020-05-08 13:44:43 +0100668
Paul Duffin3accbb52021-06-23 11:39:47 +0100669 module.AddProperties(&c.commonSdkLibraryProperties)
Paul Duffin859fe962020-05-15 10:20:31 +0100670
671 // Initialize this as an sdk library component.
Paul Duffin3accbb52021-06-23 11:39:47 +0100672 c.initSdkLibraryComponent(module)
Paul Duffin1b1e8062020-05-08 13:44:43 +0100673}
674
675func (c *commonToSdkLibraryAndImport) initCommonAfterDefaultsApplied(ctx android.DefaultableHookContext) bool {
Paul Duffindfa131e2020-05-15 20:37:11 +0100676 schemeProperty := proptools.StringDefault(c.commonSdkLibraryProperties.Naming_scheme, "default")
Paul Duffin1b1e8062020-05-08 13:44:43 +0100677 switch schemeProperty {
678 case "default":
679 c.namingScheme = &defaultNamingScheme{}
680 default:
681 ctx.PropertyErrorf("naming_scheme", "expected 'default' but was %q", schemeProperty)
682 return false
683 }
684
Paul Duffin58423872021-06-30 18:25:36 +0100685 namePtr := proptools.StringPtr(c.module.BaseModuleName())
686 c.sdkLibraryComponentProperties.SdkLibraryName = namePtr
687
Paul Duffindfa131e2020-05-15 20:37:11 +0100688 // Only track this sdk library if this can be used as a shared library.
689 if c.sharedLibrary() {
690 // Use the name specified in the module definition as the owner.
Paul Duffin58423872021-06-30 18:25:36 +0100691 c.sdkLibraryComponentProperties.SdkLibraryToImplicitlyTrack = namePtr
Paul Duffindfa131e2020-05-15 20:37:11 +0100692 }
Paul Duffin859fe962020-05-15 10:20:31 +0100693
Paul Duffin1b1e8062020-05-08 13:44:43 +0100694 return true
Paul Duffinc3091c82020-05-08 14:16:20 +0100695}
696
Paul Duffin2f7ccd52021-06-24 13:25:57 +0100697// uniqueApexVariations provides common implementation of the ApexModule.UniqueApexVariations
698// method.
699func (c *commonToSdkLibraryAndImport) uniqueApexVariations() bool {
700 // A java_sdk_library that is a shared library produces an XML file that makes the shared library
701 // usable from an AndroidManifest.xml's <uses-library> entry. That XML file contains the name of
702 // the APEX and so it needs a unique variation per APEX.
703 return c.sharedLibrary()
704}
705
Paul Duffina2ae7e02020-09-11 11:55:00 +0100706func (c *commonToSdkLibraryAndImport) generateCommonBuildActions(ctx android.ModuleContext) {
707 c.doctagPaths = android.PathsForModuleSrc(ctx, c.commonSdkLibraryProperties.Doctag_files)
708}
709
Paul Duffineedc5d52020-06-12 17:46:39 +0100710// Module name of the runtime implementation library
711func (c *commonToSdkLibraryAndImport) implLibraryModuleName() string {
Paul Duffin3accbb52021-06-23 11:39:47 +0100712 return c.module.BaseModuleName() + ".impl"
Paul Duffineedc5d52020-06-12 17:46:39 +0100713}
714
715// Module name of the XML file for the lib
716func (c *commonToSdkLibraryAndImport) xmlPermissionsModuleName() string {
Paul Duffin3accbb52021-06-23 11:39:47 +0100717 return c.module.BaseModuleName() + sdkXmlFileSuffix
Paul Duffineedc5d52020-06-12 17:46:39 +0100718}
719
Paul Duffinc3091c82020-05-08 14:16:20 +0100720// Name of the java_library module that compiles the stubs source.
721func (c *commonToSdkLibraryAndImport) stubsLibraryModuleName(apiScope *apiScope) string {
Paul Duffina1aa7382021-04-29 21:50:40 +0100722 baseName := c.module.BaseModuleName()
723 return c.module.SdkMemberComponentName(baseName, func(name string) string {
724 return c.namingScheme.stubsLibraryModuleName(apiScope, name)
725 })
Paul Duffinc3091c82020-05-08 14:16:20 +0100726}
727
728// Name of the droidstubs module that generates the stubs source and may also
729// generate/check the API.
730func (c *commonToSdkLibraryAndImport) stubsSourceModuleName(apiScope *apiScope) string {
Paul Duffina1aa7382021-04-29 21:50:40 +0100731 baseName := c.module.BaseModuleName()
732 return c.module.SdkMemberComponentName(baseName, func(name string) string {
733 return c.namingScheme.stubsSourceModuleName(apiScope, name)
734 })
Paul Duffinc3091c82020-05-08 14:16:20 +0100735}
736
Paul Duffin46dc45a2020-05-14 15:39:10 +0100737// The component names for different outputs of the java_sdk_library.
738//
739// They are similar to the names used for the child modules it creates
740const (
741 stubsSourceComponentName = "stubs.source"
742
743 apiTxtComponentName = "api.txt"
744
745 removedApiTxtComponentName = "removed-api.txt"
Anton Hansson3adf3c52021-09-21 15:25:12 +0100746
747 annotationsComponentName = "annotations.zip"
Paul Duffin46dc45a2020-05-14 15:39:10 +0100748)
749
750// A regular expression to match tags that reference a specific stubs component.
751//
752// It will only match if given a valid scope and a valid component. It is verfy strict
753// to ensure it does not accidentally match a similar looking tag that should be processed
754// by the embedded Library.
755var tagSplitter = func() *regexp.Regexp {
756 // Given a list of literal string items returns a regular expression that will
757 // match any one of the items.
758 choice := func(items ...string) string {
759 return `\Q` + strings.Join(items, `\E|\Q`) + `\E`
760 }
761
762 // Regular expression to match one of the scopes.
763 scopesRegexp := choice(allScopeNames...)
764
765 // Regular expression to match one of the components.
Anton Hansson3adf3c52021-09-21 15:25:12 +0100766 componentsRegexp := choice(stubsSourceComponentName, apiTxtComponentName, removedApiTxtComponentName, annotationsComponentName)
Paul Duffin46dc45a2020-05-14 15:39:10 +0100767
768 // Regular expression to match any combination of one scope and one component.
769 return regexp.MustCompile(fmt.Sprintf(`^\.(%s)\.(%s)$`, scopesRegexp, componentsRegexp))
770}()
771
772// For OutputFileProducer interface
773//
Anton Hansson3adf3c52021-09-21 15:25:12 +0100774// .<scope>.<component name>, for all ComponentNames (for example: .public.removed-api.txt)
Paul Duffin46dc45a2020-05-14 15:39:10 +0100775func (c *commonToSdkLibraryAndImport) commonOutputFiles(tag string) (android.Paths, error) {
776 if groups := tagSplitter.FindStringSubmatch(tag); groups != nil {
777 scopeName := groups[1]
778 component := groups[2]
779
780 if scope, ok := scopeByName[scopeName]; ok {
781 paths := c.findScopePaths(scope)
782 if paths == nil {
Paul Duffin3accbb52021-06-23 11:39:47 +0100783 return nil, fmt.Errorf("%q does not provide api scope %s", c.module.BaseModuleName(), scopeName)
Paul Duffin46dc45a2020-05-14 15:39:10 +0100784 }
785
786 switch component {
787 case stubsSourceComponentName:
788 if paths.stubsSrcJar.Valid() {
789 return android.Paths{paths.stubsSrcJar.Path()}, nil
790 }
791
792 case apiTxtComponentName:
793 if paths.currentApiFilePath.Valid() {
794 return android.Paths{paths.currentApiFilePath.Path()}, nil
795 }
796
797 case removedApiTxtComponentName:
798 if paths.removedApiFilePath.Valid() {
799 return android.Paths{paths.removedApiFilePath.Path()}, nil
800 }
Anton Hansson3adf3c52021-09-21 15:25:12 +0100801
802 case annotationsComponentName:
803 if paths.annotationsZip.Valid() {
804 return android.Paths{paths.annotationsZip.Path()}, nil
805 }
Paul Duffin46dc45a2020-05-14 15:39:10 +0100806 }
807
808 return nil, fmt.Errorf("%s not available for api scope %s", component, scopeName)
809 } else {
810 return nil, fmt.Errorf("unknown scope %s in %s", scope, tag)
811 }
812
813 } else {
Paul Duffina2ae7e02020-09-11 11:55:00 +0100814 switch tag {
815 case ".doctags":
816 if c.doctagPaths != nil {
817 return c.doctagPaths, nil
818 } else {
Paul Duffin3accbb52021-06-23 11:39:47 +0100819 return nil, fmt.Errorf("no doctag_files specified on %s", c.module.BaseModuleName())
Paul Duffina2ae7e02020-09-11 11:55:00 +0100820 }
821 }
Paul Duffin46dc45a2020-05-14 15:39:10 +0100822 return nil, nil
823 }
824}
825
Paul Duffin803a9562020-05-20 11:52:25 +0100826func (c *commonToSdkLibraryAndImport) getScopePathsCreateIfNeeded(scope *apiScope) *scopePaths {
Paul Duffin56d44902020-01-31 13:36:25 +0000827 if c.scopePaths == nil {
828 c.scopePaths = make(map[*apiScope]*scopePaths)
829 }
830 paths := c.scopePaths[scope]
831 if paths == nil {
832 paths = &scopePaths{}
833 c.scopePaths[scope] = paths
834 }
835
836 return paths
837}
838
Paul Duffin803a9562020-05-20 11:52:25 +0100839func (c *commonToSdkLibraryAndImport) findScopePaths(scope *apiScope) *scopePaths {
840 if c.scopePaths == nil {
841 return nil
842 }
843
844 return c.scopePaths[scope]
845}
846
847// If this does not support the requested api scope then find the closest available
848// scope it does support. Returns nil if no such scope is available.
849func (c *commonToSdkLibraryAndImport) findClosestScopePath(scope *apiScope) *scopePaths {
850 for s := scope; s != nil; s = s.extends {
851 if paths := c.findScopePaths(s); paths != nil {
852 return paths
853 }
854 }
855
856 // This should never happen outside tests as public should be the base scope for every
857 // scope and is enabled by default.
858 return nil
859}
860
Jiyong Parkf1691d22021-03-29 20:11:58 +0900861func (c *commonToSdkLibraryAndImport) selectHeaderJarsForSdkVersion(ctx android.BaseModuleContext, sdkVersion android.SdkSpec) android.Paths {
Paul Duffinb05d4292020-05-20 12:19:10 +0100862
863 // If a specific numeric version has been requested then use prebuilt versions of the sdk.
Jiyong Park54105c42021-03-31 18:17:53 +0900864 if !sdkVersion.ApiLevel.IsPreview() {
Paul Duffin3accbb52021-06-23 11:39:47 +0100865 return PrebuiltJars(ctx, c.module.BaseModuleName(), sdkVersion)
Paul Duffinb05d4292020-05-20 12:19:10 +0100866 }
867
Paul Duffin1267d872021-04-16 17:21:36 +0100868 paths := c.selectScopePaths(ctx, sdkVersion.Kind)
869 if paths == nil {
870 return nil
871 }
872
873 return paths.stubsHeaderPath
874}
875
876// selectScopePaths returns the *scopePaths appropriate for the specific kind.
877//
878// If the module does not support the specific kind then it will return the *scopePaths for the
879// closest kind which is a subset of the requested kind. e.g. if requesting android.SdkModule then
880// it will return *scopePaths for android.SdkSystem if available or android.SdkPublic of not.
881func (c *commonToSdkLibraryAndImport) selectScopePaths(ctx android.BaseModuleContext, kind android.SdkKind) *scopePaths {
Paul Duffin54c1f082021-05-18 16:32:50 +0100882 apiScope := sdkKindToApiScope(kind)
Paul Duffinb05d4292020-05-20 12:19:10 +0100883
Paul Duffin803a9562020-05-20 11:52:25 +0100884 paths := c.findClosestScopePath(apiScope)
885 if paths == nil {
886 var scopes []string
887 for _, s := range allApiScopes {
888 if c.findScopePaths(s) != nil {
889 scopes = append(scopes, s.name)
890 }
891 }
Paul Duffin3accbb52021-06-23 11:39:47 +0100892 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 +0100893 return nil
894 }
895
Paul Duffin1267d872021-04-16 17:21:36 +0100896 return paths
897}
898
Paul Duffin54c1f082021-05-18 16:32:50 +0100899// sdkKindToApiScope maps from android.SdkKind to apiScope.
900func sdkKindToApiScope(kind android.SdkKind) *apiScope {
901 var apiScope *apiScope
902 switch kind {
903 case android.SdkSystem:
904 apiScope = apiScopeSystem
905 case android.SdkModule:
906 apiScope = apiScopeModuleLib
907 case android.SdkTest:
908 apiScope = apiScopeTest
909 case android.SdkSystemServer:
910 apiScope = apiScopeSystemServer
911 default:
912 apiScope = apiScopePublic
913 }
914 return apiScope
915}
916
Paul Duffin1267d872021-04-16 17:21:36 +0100917// to satisfy SdkLibraryDependency interface
918func (c *commonToSdkLibraryAndImport) SdkApiStubDexJar(ctx android.BaseModuleContext, kind android.SdkKind) android.Path {
919 paths := c.selectScopePaths(ctx, kind)
920 if paths == nil {
921 return nil
922 }
923
924 return paths.stubsDexJarPath
Paul Duffinb05d4292020-05-20 12:19:10 +0100925}
926
Paul Duffin54c1f082021-05-18 16:32:50 +0100927// to satisfy SdkLibraryDependency interface
928func (c *commonToSdkLibraryAndImport) SdkRemovedTxtFile(ctx android.BaseModuleContext, kind android.SdkKind) android.OptionalPath {
929 apiScope := sdkKindToApiScope(kind)
930 paths := c.findScopePaths(apiScope)
931 if paths == nil {
932 return android.OptionalPath{}
933 }
934
935 return paths.removedApiFilePath
936}
937
Paul Duffin859fe962020-05-15 10:20:31 +0100938func (c *commonToSdkLibraryAndImport) sdkComponentPropertiesForChildLibrary() interface{} {
939 componentProps := &struct {
Paul Duffin58423872021-06-30 18:25:36 +0100940 SdkLibraryName *string
Paul Duffin859fe962020-05-15 10:20:31 +0100941 SdkLibraryToImplicitlyTrack *string
Paul Duffindfa131e2020-05-15 20:37:11 +0100942 }{}
943
Paul Duffin58423872021-06-30 18:25:36 +0100944 namePtr := proptools.StringPtr(c.module.BaseModuleName())
945 componentProps.SdkLibraryName = namePtr
946
Paul Duffindfa131e2020-05-15 20:37:11 +0100947 if c.sharedLibrary() {
Paul Duffin859fe962020-05-15 10:20:31 +0100948 // Mark the stubs library as being components of this java_sdk_library so that
949 // any app that includes code which depends (directly or indirectly) on the stubs
950 // library will have the appropriate <uses-library> invocation inserted into its
951 // manifest if necessary.
Paul Duffin58423872021-06-30 18:25:36 +0100952 componentProps.SdkLibraryToImplicitlyTrack = namePtr
Paul Duffin859fe962020-05-15 10:20:31 +0100953 }
954
955 return componentProps
956}
957
Paul Duffindfa131e2020-05-15 20:37:11 +0100958func (c *commonToSdkLibraryAndImport) sharedLibrary() bool {
959 return proptools.BoolDefault(c.commonSdkLibraryProperties.Shared_library, true)
960}
961
Paul Duffinf4600f62021-05-13 22:34:45 +0100962// Check if the stub libraries should be compiled for dex
963func (c *commonToSdkLibraryAndImport) stubLibrariesCompiledForDex() bool {
964 // Always compile the dex file files for the stub libraries if they will be used on the
965 // bootclasspath.
966 return !c.sharedLibrary()
967}
968
Paul Duffin859fe962020-05-15 10:20:31 +0100969// Properties related to the use of a module as an component of a java_sdk_library.
970type SdkLibraryComponentProperties struct {
Paul Duffin58423872021-06-30 18:25:36 +0100971 // The name of the java_sdk_library/_import module.
972 SdkLibraryName *string `blueprint:"mutated"`
Paul Duffin859fe962020-05-15 10:20:31 +0100973
974 // The name of the java_sdk_library/_import to add to a <uses-library> entry
975 // in the AndroidManifest.xml of any Android app that includes code that references
976 // this module. If not set then no java_sdk_library/_import is tracked.
977 SdkLibraryToImplicitlyTrack *string `blueprint:"mutated"`
978}
979
980// Structure to be embedded in a module struct that needs to support the
981// SdkLibraryComponentDependency interface.
982type EmbeddableSdkLibraryComponent struct {
983 sdkLibraryComponentProperties SdkLibraryComponentProperties
984}
985
Paul Duffin3accbb52021-06-23 11:39:47 +0100986func (e *EmbeddableSdkLibraryComponent) initSdkLibraryComponent(module android.Module) {
987 module.AddProperties(&e.sdkLibraryComponentProperties)
Paul Duffin859fe962020-05-15 10:20:31 +0100988}
989
990// to satisfy SdkLibraryComponentDependency
Paul Duffin58423872021-06-30 18:25:36 +0100991func (e *EmbeddableSdkLibraryComponent) SdkLibraryName() *string {
992 return e.sdkLibraryComponentProperties.SdkLibraryName
993}
994
995// to satisfy SdkLibraryComponentDependency
Ulya Trafimovich31e444e2020-08-14 17:32:16 +0100996func (e *EmbeddableSdkLibraryComponent) OptionalImplicitSdkLibrary() *string {
997 return e.sdkLibraryComponentProperties.SdkLibraryToImplicitlyTrack
Paul Duffin859fe962020-05-15 10:20:31 +0100998}
999
Ulya Trafimovich39b437b2020-09-23 16:42:35 +01001000// to satisfy SdkLibraryComponentDependency
1001func (e *EmbeddableSdkLibraryComponent) OptionalSdkLibraryImplementation() *string {
1002 // Currently implementation library name is the same as the SDK library name.
1003 return e.sdkLibraryComponentProperties.SdkLibraryToImplicitlyTrack
1004}
1005
Paul Duffin859fe962020-05-15 10:20:31 +01001006// Implemented by modules that are (or possibly could be) a component of a java_sdk_library
1007// (including the java_sdk_library) itself.
1008type SdkLibraryComponentDependency interface {
Ulya Trafimovich31e444e2020-08-14 17:32:16 +01001009 UsesLibraryDependency
1010
Paul Duffin58423872021-06-30 18:25:36 +01001011 // SdkLibraryName returns the name of the java_sdk_library/_import module.
1012 SdkLibraryName() *string
1013
Paul Duffin859fe962020-05-15 10:20:31 +01001014 // The optional name of the sdk library that should be implicitly added to the
1015 // AndroidManifest of an app that contains code which references the sdk library.
1016 //
Ulya Trafimovich31e444e2020-08-14 17:32:16 +01001017 // Returns the name of the optional implicit SDK library or nil, if there isn't one.
1018 OptionalImplicitSdkLibrary() *string
Ulya Trafimovich39b437b2020-09-23 16:42:35 +01001019
1020 // The name of the implementation library for the optional SDK library or nil, if there isn't one.
1021 OptionalSdkLibraryImplementation() *string
Paul Duffin859fe962020-05-15 10:20:31 +01001022}
1023
1024// Make sure that all the module types that are components of java_sdk_library/_import
1025// and which can be referenced (directly or indirectly) from an android app implement
1026// the SdkLibraryComponentDependency interface.
1027var _ SdkLibraryComponentDependency = (*Library)(nil)
1028var _ SdkLibraryComponentDependency = (*Import)(nil)
1029var _ SdkLibraryComponentDependency = (*SdkLibrary)(nil)
Paul Duffineedc5d52020-06-12 17:46:39 +01001030var _ SdkLibraryComponentDependency = (*SdkLibraryImport)(nil)
Paul Duffin859fe962020-05-15 10:20:31 +01001031
Paul Duffin54c1f082021-05-18 16:32:50 +01001032// Provides access to sdk_version related files, e.g. header and implementation jars.
Paul Duffin859fe962020-05-15 10:20:31 +01001033type SdkLibraryDependency interface {
1034 SdkLibraryComponentDependency
1035
1036 // Get the header jars appropriate for the supplied sdk_version.
1037 //
1038 // These are turbine generated jars so they only change if the externals of the
1039 // class changes but it does not contain and implementation or JavaDoc.
Jiyong Parkf1691d22021-03-29 20:11:58 +09001040 SdkHeaderJars(ctx android.BaseModuleContext, sdkVersion android.SdkSpec) android.Paths
Paul Duffin859fe962020-05-15 10:20:31 +01001041
1042 // Get the implementation jars appropriate for the supplied sdk version.
1043 //
1044 // These are either the implementation jar for the whole sdk library or the implementation
1045 // jars for the stubs. The latter should only be needed when generating JavaDoc as otherwise
1046 // they are identical to the corresponding header jars.
Jiyong Parkf1691d22021-03-29 20:11:58 +09001047 SdkImplementationJars(ctx android.BaseModuleContext, sdkVersion android.SdkSpec) android.Paths
Paul Duffin1267d872021-04-16 17:21:36 +01001048
1049 // SdkApiStubDexJar returns the dex jar for the stubs. It is needed by the hiddenapi processing
1050 // tool which processes dex files.
1051 SdkApiStubDexJar(ctx android.BaseModuleContext, kind android.SdkKind) android.Path
Paul Duffinf4600f62021-05-13 22:34:45 +01001052
Paul Duffin54c1f082021-05-18 16:32:50 +01001053 // SdkRemovedTxtFile returns the optional path to the removed.txt file for the specified sdk kind.
1054 SdkRemovedTxtFile(ctx android.BaseModuleContext, kind android.SdkKind) android.OptionalPath
1055
Paul Duffinf4600f62021-05-13 22:34:45 +01001056 // sharedLibrary returns true if this can be used as a shared library.
1057 sharedLibrary() bool
Paul Duffin859fe962020-05-15 10:20:31 +01001058}
1059
Inseob Kimc0907f12019-02-08 21:00:45 +09001060type SdkLibrary struct {
Sundong Ahn054b19a2018-10-19 13:46:09 +09001061 Library
Jiyong Parkc678ad32018-04-10 13:07:10 +09001062
Sundong Ahn054b19a2018-10-19 13:46:09 +09001063 sdkLibraryProperties sdkLibraryProperties
Jiyong Parkc678ad32018-04-10 13:07:10 +09001064
Paul Duffin3375e352020-04-28 10:44:03 +01001065 // Map from api scope to the scope specific property structure.
1066 scopeToProperties map[*apiScope]*ApiScopeProperties
1067
Paul Duffin56d44902020-01-31 13:36:25 +00001068 commonToSdkLibraryAndImport
Jiyong Parkc678ad32018-04-10 13:07:10 +09001069}
1070
Inseob Kimc0907f12019-02-08 21:00:45 +09001071var _ SdkLibraryDependency = (*SdkLibrary)(nil)
Colin Cross897d2ed2019-02-11 14:03:51 -08001072
Paul Duffin3375e352020-04-28 10:44:03 +01001073func (module *SdkLibrary) generateTestAndSystemScopesByDefault() bool {
1074 return module.sdkLibraryProperties.Generate_system_and_test_apis
1075}
1076
1077func (module *SdkLibrary) getGeneratedApiScopes(ctx android.EarlyModuleContext) apiScopes {
1078 // Check to see if any scopes have been explicitly enabled. If any have then all
1079 // must be.
1080 anyScopesExplicitlyEnabled := false
1081 for _, scope := range allApiScopes {
1082 scopeProperties := module.scopeToProperties[scope]
1083 if scopeProperties.Enabled != nil {
1084 anyScopesExplicitlyEnabled = true
1085 break
1086 }
Paul Duffind1b3a922020-01-22 11:57:20 +00001087 }
Paul Duffin3375e352020-04-28 10:44:03 +01001088
1089 var generatedScopes apiScopes
1090 enabledScopes := make(map[*apiScope]struct{})
1091 for _, scope := range allApiScopes {
1092 scopeProperties := module.scopeToProperties[scope]
1093 // If any scopes are explicitly enabled then ignore the legacy enabled status.
1094 // This is to ensure that any new usages of this module type do not rely on legacy
1095 // behaviour.
1096 defaultEnabledStatus := false
1097 if anyScopesExplicitlyEnabled {
1098 defaultEnabledStatus = scope.defaultEnabledStatus
1099 } else {
1100 defaultEnabledStatus = scope.legacyEnabledStatus(module)
1101 }
1102 enabled := proptools.BoolDefault(scopeProperties.Enabled, defaultEnabledStatus)
1103 if enabled {
1104 enabledScopes[scope] = struct{}{}
1105 generatedScopes = append(generatedScopes, scope)
1106 }
1107 }
1108
1109 // Now check to make sure that any scope that is extended by an enabled scope is also
1110 // enabled.
1111 for _, scope := range allApiScopes {
1112 if _, ok := enabledScopes[scope]; ok {
1113 extends := scope.extends
1114 if extends != nil {
1115 if _, ok := enabledScopes[extends]; !ok {
1116 ctx.ModuleErrorf("enabled api scope %q depends on disabled scope %q", scope, extends)
1117 }
1118 }
1119 }
1120 }
1121
1122 return generatedScopes
Paul Duffind1b3a922020-01-22 11:57:20 +00001123}
1124
satayev812683e2021-12-06 11:42:40 +00001125var _ android.ModuleWithMinSdkVersionCheck = (*SdkLibrary)(nil)
1126
satayev531330e2021-12-06 11:40:46 +00001127func (module *SdkLibrary) CheckMinSdkVersion(ctx android.ModuleContext) {
1128 android.CheckMinSdkVersion(ctx, module.MinSdkVersion(ctx).ApiLevel, func(c android.ModuleContext, do android.PayloadDepsCallback) {
1129 ctx.WalkDeps(func(child android.Module, parent android.Module) bool {
1130 isExternal := !module.depIsInSameApex(ctx, child)
1131 if am, ok := child.(android.ApexModule); ok {
1132 if !do(ctx, parent, am, isExternal) {
1133 return false
1134 }
1135 }
1136 return !isExternal
1137 })
1138 })
1139}
1140
Paul Duffineedc5d52020-06-12 17:46:39 +01001141type sdkLibraryComponentTag struct {
1142 blueprint.BaseDependencyTag
1143 name string
1144}
1145
1146// Mark this tag so dependencies that use it are excluded from visibility enforcement.
1147func (t sdkLibraryComponentTag) ExcludeFromVisibilityEnforcement() {}
1148
1149var xmlPermissionsFileTag = sdkLibraryComponentTag{name: "xml-permissions-file"}
Paul Duffine74ac732020-02-06 13:51:46 +00001150
Jiyong Parke3833882020-02-17 17:28:10 +09001151func IsXmlPermissionsFileDepTag(depTag blueprint.DependencyTag) bool {
Paul Duffineedc5d52020-06-12 17:46:39 +01001152 if dt, ok := depTag.(sdkLibraryComponentTag); ok {
Jiyong Parke3833882020-02-17 17:28:10 +09001153 return dt == xmlPermissionsFileTag
1154 }
1155 return false
1156}
1157
Paul Duffineedc5d52020-06-12 17:46:39 +01001158var implLibraryTag = sdkLibraryComponentTag{name: "impl-library"}
Paul Duffin5df79302020-05-16 15:52:12 +01001159
Paul Duffin44f1d842020-06-26 20:17:02 +01001160// Add the dependencies on the child modules in the component deps mutator.
1161func (module *SdkLibrary) ComponentDepsMutator(ctx android.BottomUpMutatorContext) {
Paul Duffin3375e352020-04-28 10:44:03 +01001162 for _, apiScope := range module.getGeneratedApiScopes(ctx) {
Paul Duffind1b3a922020-01-22 11:57:20 +00001163 // Add dependencies to the stubs library
Paul Duffinc3091c82020-05-08 14:16:20 +01001164 ctx.AddVariationDependencies(nil, apiScope.stubsTag, module.stubsLibraryModuleName(apiScope))
Paul Duffind1b3a922020-01-22 11:57:20 +00001165
Paul Duffin15f34ef2020-07-20 18:04:44 +01001166 // Add a dependency on the stubs source in order to access both stubs source and api information.
1167 ctx.AddVariationDependencies(nil, apiScope.stubsSourceAndApiTag, module.stubsSourceModuleName(apiScope))
Sundong Ahn054b19a2018-10-19 13:46:09 +09001168 }
1169
Paul Duffindfa131e2020-05-15 20:37:11 +01001170 if module.requiresRuntimeImplementationLibrary() {
Paul Duffin5df79302020-05-16 15:52:12 +01001171 // Add dependency to the rule for generating the implementation library.
1172 ctx.AddDependency(module, implLibraryTag, module.implLibraryModuleName())
1173
Paul Duffindfa131e2020-05-15 20:37:11 +01001174 if module.sharedLibrary() {
1175 // Add dependency to the rule for generating the xml permissions file
Paul Duffineedc5d52020-06-12 17:46:39 +01001176 ctx.AddDependency(module, xmlPermissionsFileTag, module.xmlPermissionsModuleName())
Paul Duffindfa131e2020-05-15 20:37:11 +01001177 }
Paul Duffin44f1d842020-06-26 20:17:02 +01001178 }
1179}
Paul Duffine74ac732020-02-06 13:51:46 +00001180
Paul Duffin44f1d842020-06-26 20:17:02 +01001181// Add other dependencies as normal.
1182func (module *SdkLibrary) DepsMutator(ctx android.BottomUpMutatorContext) {
Anton Hanssone77fccc2021-01-20 16:52:41 +00001183 var missingApiModules []string
1184 for _, apiScope := range module.getGeneratedApiScopes(ctx) {
1185 if apiScope.unstable {
1186 continue
1187 }
1188 if m := android.SrcIsModule(module.latestApiFilegroupName(apiScope)); !ctx.OtherModuleExists(m) {
1189 missingApiModules = append(missingApiModules, m)
1190 }
1191 if m := android.SrcIsModule(module.latestRemovedApiFilegroupName(apiScope)); !ctx.OtherModuleExists(m) {
1192 missingApiModules = append(missingApiModules, m)
1193 }
Jaewoong Jung1a97ee02021-03-09 13:25:02 -08001194 if m := android.SrcIsModule(module.latestIncompatibilitiesFilegroupName(apiScope)); !ctx.OtherModuleExists(m) {
1195 missingApiModules = append(missingApiModules, m)
1196 }
Anton Hanssone77fccc2021-01-20 16:52:41 +00001197 }
1198 if len(missingApiModules) != 0 && !module.sdkLibraryProperties.Unsafe_ignore_missing_latest_api {
1199 m := module.Name() + " is missing tracking files for previously released library versions.\n"
1200 m += "You need to do one of the following:\n"
1201 m += "- Add `unsafe_ignore_missing_latest_api: true` to your blueprint (to disable compat tracking)\n"
1202 m += "- Add a set of prebuilt txt files representing the last released version of this library for compat checking.\n"
1203 m += " (the current set of API files can be used as a seed for this compatibility tracking\n"
1204 m += "\n"
1205 m += "The following filegroup modules are missing:\n "
1206 m += strings.Join(missingApiModules, "\n ") + "\n"
1207 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."
1208 ctx.ModuleErrorf(m)
1209 }
Paul Duffin44f1d842020-06-26 20:17:02 +01001210 if module.requiresRuntimeImplementationLibrary() {
Paul Duffindfa131e2020-05-15 20:37:11 +01001211 // Only add the deps for the library if it is actually going to be built.
1212 module.Library.deps(ctx)
1213 }
Jiyong Parkc678ad32018-04-10 13:07:10 +09001214}
1215
Paul Duffin46dc45a2020-05-14 15:39:10 +01001216func (module *SdkLibrary) OutputFiles(tag string) (android.Paths, error) {
1217 paths, err := module.commonOutputFiles(tag)
1218 if paths == nil && err == nil {
1219 return module.Library.OutputFiles(tag)
1220 } else {
1221 return paths, err
1222 }
1223}
1224
Inseob Kimc0907f12019-02-08 21:00:45 +09001225func (module *SdkLibrary) GenerateAndroidBuildActions(ctx android.ModuleContext) {
satayev531330e2021-12-06 11:40:46 +00001226 if proptools.String(module.deviceProperties.Min_sdk_version) != "" {
1227 module.CheckMinSdkVersion(ctx)
1228 }
1229
Paul Duffina2ae7e02020-09-11 11:55:00 +01001230 module.generateCommonBuildActions(ctx)
1231
Paul Duffindfa131e2020-05-15 20:37:11 +01001232 // Only build an implementation library if required.
1233 if module.requiresRuntimeImplementationLibrary() {
Paul Duffin43db9be2019-12-30 17:35:49 +00001234 module.Library.GenerateAndroidBuildActions(ctx)
1235 }
Sundong Ahn054b19a2018-10-19 13:46:09 +09001236
Paul Duffina1aa7382021-04-29 21:50:40 +01001237 // Collate the components exported by this module. All scope specific modules are exported but
1238 // the impl and xml component modules are not.
1239 exportedComponents := map[string]struct{}{}
1240
Sundong Ahn57368eb2018-07-06 11:20:23 +09001241 // Record the paths to the header jars of the library (stubs and impl).
Paul Duffind1b3a922020-01-22 11:57:20 +00001242 // When this java_sdk_library is depended upon from others via "libs" property,
Jiyong Parkc678ad32018-04-10 13:07:10 +09001243 // the recorded paths will be returned depending on the link type of the caller.
1244 ctx.VisitDirectDeps(func(to android.Module) {
Jiyong Parkc678ad32018-04-10 13:07:10 +09001245 tag := ctx.OtherModuleDependencyTag(to)
1246
Paul Duffinc8782502020-04-29 20:45:27 +01001247 // Extract information from any of the scope specific dependencies.
1248 if scopeTag, ok := tag.(scopeDependencyTag); ok {
1249 apiScope := scopeTag.apiScope
Paul Duffin803a9562020-05-20 11:52:25 +01001250 scopePaths := module.getScopePathsCreateIfNeeded(apiScope)
Paul Duffinc8782502020-04-29 20:45:27 +01001251
1252 // Extract information from the dependency. The exact information extracted
1253 // is determined by the nature of the dependency which is determined by the tag.
1254 scopeTag.extractDepInfo(ctx, to, scopePaths)
Paul Duffina1aa7382021-04-29 21:50:40 +01001255
1256 exportedComponents[ctx.OtherModuleName(to)] = struct{}{}
Sundong Ahn20e998b2018-07-24 11:19:26 +09001257 }
Jiyong Parkc678ad32018-04-10 13:07:10 +09001258 })
Paul Duffina1aa7382021-04-29 21:50:40 +01001259
1260 // Make the set of components exported by this module available for use elsewhere.
1261 exportedComponentInfo := android.ExportedComponentsInfo{Components: android.SortedStringKeys(exportedComponents)}
1262 ctx.SetProvider(android.ExportedComponentsInfoProvider, exportedComponentInfo)
Jiyong Parkc678ad32018-04-10 13:07:10 +09001263}
1264
Jiyong Park0b0e1b92019-12-03 13:24:29 +09001265func (module *SdkLibrary) AndroidMkEntries() []android.AndroidMkEntries {
Paul Duffindfa131e2020-05-15 20:37:11 +01001266 if !module.requiresRuntimeImplementationLibrary() {
Paul Duffin43db9be2019-12-30 17:35:49 +00001267 return nil
1268 }
Jiyong Park0b0e1b92019-12-03 13:24:29 +09001269 entriesList := module.Library.AndroidMkEntries()
Yo Chiang07d75072020-06-05 17:43:19 +08001270 if module.sharedLibrary() {
1271 entries := &entriesList[0]
1272 entries.Required = append(entries.Required, module.xmlPermissionsModuleName())
1273 }
Jiyong Park0b0e1b92019-12-03 13:24:29 +09001274 return entriesList
Jiyong Park82484c02018-04-23 21:41:26 +09001275}
1276
Anton Hansson5fd5d242020-03-27 19:43:19 +00001277// The dist path of the stub artifacts
1278func (module *SdkLibrary) apiDistPath(apiScope *apiScope) string {
Colin Crossea0e6982021-06-02 13:02:23 -07001279 return path.Join("apistubs", module.distGroup(), apiScope.name)
Anton Hansson5fd5d242020-03-27 19:43:19 +00001280}
1281
Paul Duffin12ceb462019-12-24 20:31:31 +00001282// Get the sdk version for use when compiling the stubs library.
Paul Duffin780c5f42020-05-12 15:52:55 +01001283func (module *SdkLibrary) sdkVersionForStubsLibrary(mctx android.EarlyModuleContext, apiScope *apiScope) string {
Paul Duffin87a05a32020-05-12 11:50:28 +01001284 scopeProperties := module.scopeToProperties[apiScope]
1285 if scopeProperties.Sdk_version != nil {
1286 return proptools.String(scopeProperties.Sdk_version)
1287 }
1288
Jiyong Parkf1691d22021-03-29 20:11:58 +09001289 sdkDep := decodeSdkDep(mctx, android.SdkContext(&module.Library))
Paul Duffin12ceb462019-12-24 20:31:31 +00001290 if sdkDep.hasStandardLibs() {
1291 // If building against a standard sdk then use the sdk version appropriate for the scope.
Paul Duffind1b3a922020-01-22 11:57:20 +00001292 return apiScope.sdkVersion
Paul Duffin12ceb462019-12-24 20:31:31 +00001293 } else {
1294 // Otherwise, use no system module.
1295 return "none"
1296 }
1297}
1298
Paul Duffin31310252020-11-20 21:26:20 +00001299func (module *SdkLibrary) distStem() string {
1300 return proptools.StringDefault(module.sdkLibraryProperties.Dist_stem, module.BaseModuleName())
1301}
1302
Colin Cross0d3dd062021-06-01 13:13:40 -07001303// distGroup returns the subdirectory of the dist path of the stub artifacts.
1304func (module *SdkLibrary) distGroup() string {
Colin Cross1072a712021-06-01 14:07:56 -07001305 return proptools.StringDefault(module.sdkLibraryProperties.Dist_group, "unknown")
Colin Cross0d3dd062021-06-01 13:13:40 -07001306}
1307
Paul Duffind1b3a922020-01-22 11:57:20 +00001308func (module *SdkLibrary) latestApiFilegroupName(apiScope *apiScope) string {
Paul Duffin31310252020-11-20 21:26:20 +00001309 return ":" + module.distStem() + ".api." + apiScope.name + ".latest"
Jiyong Park58c518b2018-05-12 22:29:12 +09001310}
Jiyong Parkc678ad32018-04-10 13:07:10 +09001311
Paul Duffind1b3a922020-01-22 11:57:20 +00001312func (module *SdkLibrary) latestRemovedApiFilegroupName(apiScope *apiScope) string {
Paul Duffin31310252020-11-20 21:26:20 +00001313 return ":" + module.distStem() + "-removed.api." + apiScope.name + ".latest"
Jiyong Parkc678ad32018-04-10 13:07:10 +09001314}
1315
Jaewoong Jung1a97ee02021-03-09 13:25:02 -08001316func (module *SdkLibrary) latestIncompatibilitiesFilegroupName(apiScope *apiScope) string {
1317 return ":" + module.distStem() + "-incompatibilities.api." + apiScope.name + ".latest"
1318}
1319
Anton Hansson944e77d2020-08-19 11:40:22 +01001320func childModuleVisibility(childVisibility []string) []string {
1321 if childVisibility == nil {
1322 // No child visibility set. The child will use the visibility of the sdk_library.
1323 return nil
1324 }
1325
1326 // Prepend an override to ignore the sdk_library's visibility, and rely on the child visibility.
1327 var visibility []string
1328 visibility = append(visibility, "//visibility:override")
1329 visibility = append(visibility, childVisibility...)
1330 return visibility
1331}
1332
Paul Duffin5df79302020-05-16 15:52:12 +01001333// Creates the implementation java library
1334func (module *SdkLibrary) createImplLibrary(mctx android.DefaultableHookContext) {
Anton Hansson944e77d2020-08-19 11:40:22 +01001335 visibility := childModuleVisibility(module.sdkLibraryProperties.Impl_library_visibility)
1336
Paul Duffin5df79302020-05-16 15:52:12 +01001337 props := struct {
Paul Duffin59190602021-05-14 16:35:06 +01001338 Name *string
1339 Visibility []string
1340 Instrument bool
1341 Libs []string
Paul Duffin5df79302020-05-16 15:52:12 +01001342 }{
1343 Name: proptools.StringPtr(module.implLibraryModuleName()),
Anton Hansson944e77d2020-08-19 11:40:22 +01001344 Visibility: visibility,
Paul Duffin296cf332020-06-18 21:09:55 +01001345 // Set the instrument property to ensure it is instrumented when instrumentation is required.
1346 Instrument: true,
Anton Hansson7f66efa2020-10-08 14:47:23 +01001347 // Set the impl_only libs. Note that the module's "Libs" get appended as well, via the
1348 // addition of &module.properties below.
1349 Libs: module.sdkLibraryProperties.Impl_only_libs,
Paul Duffin5df79302020-05-16 15:52:12 +01001350 }
1351
1352 properties := []interface{}{
1353 &module.properties,
1354 &module.protoProperties,
1355 &module.deviceProperties,
Liz Kammera7a64f32020-07-09 15:16:41 -07001356 &module.dexProperties,
Paul Duffin5df79302020-05-16 15:52:12 +01001357 &module.dexpreoptProperties,
Colin Cross014489c2020-06-02 20:09:13 -07001358 &module.linter.properties,
Paul Duffin5df79302020-05-16 15:52:12 +01001359 &props,
1360 module.sdkComponentPropertiesForChildLibrary(),
1361 }
1362 mctx.CreateModule(LibraryFactory, properties...)
1363}
1364
Jiyong Parkc678ad32018-04-10 13:07:10 +09001365// Creates a static java library that has API stubs
Paul Duffinf0229202020-04-29 16:47:28 +01001366func (module *SdkLibrary) createStubsLibrary(mctx android.DefaultableHookContext, apiScope *apiScope) {
Jiyong Parkc678ad32018-04-10 13:07:10 +09001367 props := struct {
Dan Willemsen9f435972020-05-28 15:28:00 -07001368 Name *string
1369 Visibility []string
1370 Srcs []string
1371 Installable *bool
1372 Sdk_version *string
1373 System_modules *string
1374 Patch_module *string
1375 Libs []string
Anton Hanssondae54cd2021-04-21 16:30:10 +01001376 Static_libs []string
Dan Willemsen9f435972020-05-28 15:28:00 -07001377 Compile_dex *bool
1378 Java_version *string
1379 Openjdk9 struct {
Sundong Ahn054b19a2018-10-19 13:46:09 +09001380 Srcs []string
1381 Javacflags []string
1382 }
Anton Hansson5fd5d242020-03-27 19:43:19 +00001383 Dist struct {
1384 Targets []string
1385 Dest *string
1386 Dir *string
1387 Tag *string
1388 }
Jiyong Parkc678ad32018-04-10 13:07:10 +09001389 }{}
1390
Paul Duffinc3091c82020-05-08 14:16:20 +01001391 props.Name = proptools.StringPtr(module.stubsLibraryModuleName(apiScope))
Anton Hansson944e77d2020-08-19 11:40:22 +01001392 props.Visibility = childModuleVisibility(module.sdkLibraryProperties.Stubs_library_visibility)
Jiyong Parkc678ad32018-04-10 13:07:10 +09001393 // sources are generated from the droiddoc
Paul Duffinc3091c82020-05-08 14:16:20 +01001394 props.Srcs = []string{":" + module.stubsSourceModuleName(apiScope)}
Paul Duffin12ceb462019-12-24 20:31:31 +00001395 sdkVersion := module.sdkVersionForStubsLibrary(mctx, apiScope)
Paul Duffin52d398a2019-06-11 12:31:14 +01001396 props.Sdk_version = proptools.StringPtr(sdkVersion)
Paul Duffina18abc22020-05-16 18:54:24 +01001397 props.System_modules = module.deviceProperties.System_modules
1398 props.Patch_module = module.properties.Patch_module
Paul Duffin367ab912019-12-23 19:40:36 +00001399 props.Installable = proptools.BoolPtr(false)
Sundong Ahn054b19a2018-10-19 13:46:09 +09001400 props.Libs = module.sdkLibraryProperties.Stub_only_libs
Anton Hanssondae54cd2021-04-21 16:30:10 +01001401 props.Static_libs = module.sdkLibraryProperties.Stub_only_static_libs
Paul Duffine22c2ab2020-05-20 19:35:27 +01001402 // The stub-annotations library contains special versions of the annotations
1403 // with CLASS retention policy, so that they're kept.
1404 if proptools.Bool(module.sdkLibraryProperties.Annotations_enabled) {
1405 props.Libs = append(props.Libs, "stub-annotations")
1406 }
Paul Duffina18abc22020-05-16 18:54:24 +01001407 props.Openjdk9.Srcs = module.properties.Openjdk9.Srcs
1408 props.Openjdk9.Javacflags = module.properties.Openjdk9.Javacflags
Anton Hansson83509b52020-05-21 09:21:57 +01001409 // We compile the stubs for 1.8 in line with the main android.jar stubs, and potential
1410 // interop with older developer tools that don't support 1.9.
1411 props.Java_version = proptools.StringPtr("1.8")
Paul Duffinf4600f62021-05-13 22:34:45 +01001412
1413 // The imports need to be compiled to dex if the java_sdk_library requests it.
1414 compileDex := module.dexProperties.Compile_dex
1415 if module.stubLibrariesCompiledForDex() {
1416 compileDex = proptools.BoolPtr(true)
Sundong Ahndd567f92018-07-31 17:19:11 +09001417 }
Paul Duffinf4600f62021-05-13 22:34:45 +01001418 props.Compile_dex = compileDex
Jiyong Parkc678ad32018-04-10 13:07:10 +09001419
Anton Hansson5fd5d242020-03-27 19:43:19 +00001420 // Dist the class jar artifact for sdk builds.
1421 if !Bool(module.sdkLibraryProperties.No_dist) {
1422 props.Dist.Targets = []string{"sdk", "win_sdk"}
Paul Duffin31310252020-11-20 21:26:20 +00001423 props.Dist.Dest = proptools.StringPtr(fmt.Sprintf("%v.jar", module.distStem()))
Anton Hansson5fd5d242020-03-27 19:43:19 +00001424 props.Dist.Dir = proptools.StringPtr(module.apiDistPath(apiScope))
1425 props.Dist.Tag = proptools.StringPtr(".jar")
1426 }
Jiyong Parkc678ad32018-04-10 13:07:10 +09001427
Paul Duffin859fe962020-05-15 10:20:31 +01001428 mctx.CreateModule(LibraryFactory, &props, module.sdkComponentPropertiesForChildLibrary())
Jiyong Parkc678ad32018-04-10 13:07:10 +09001429}
1430
Paul Duffin6d0886e2020-04-07 18:49:53 +01001431// Creates a droidstubs module that creates stubs source files from the given full source
Paul Duffinc8782502020-04-29 20:45:27 +01001432// files and also updates and checks the API specification files.
Paul Duffin15f34ef2020-07-20 18:04:44 +01001433func (module *SdkLibrary) createStubsSourcesAndApi(mctx android.DefaultableHookContext, apiScope *apiScope, name string, scopeSpecificDroidstubsArgs []string) {
Jiyong Parkc678ad32018-04-10 13:07:10 +09001434 props := struct {
Sundong Ahn054b19a2018-10-19 13:46:09 +09001435 Name *string
Paul Duffin4911a892020-04-29 23:35:13 +01001436 Visibility []string
Sundong Ahn054b19a2018-10-19 13:46:09 +09001437 Srcs []string
1438 Installable *bool
Paul Duffin52d398a2019-06-11 12:31:14 +01001439 Sdk_version *string
Paul Duffin12ceb462019-12-24 20:31:31 +00001440 System_modules *string
Sundong Ahn054b19a2018-10-19 13:46:09 +09001441 Libs []string
Paul Duffin6877e6d2020-09-25 19:59:14 +01001442 Output_javadoc_comments *bool
Paul Duffin11512472019-02-11 15:55:17 +00001443 Arg_files []string
Sundong Ahn054b19a2018-10-19 13:46:09 +09001444 Args *string
Sundong Ahn054b19a2018-10-19 13:46:09 +09001445 Java_version *string
Paul Duffine22c2ab2020-05-20 19:35:27 +01001446 Annotations_enabled *bool
Sundong Ahn054b19a2018-10-19 13:46:09 +09001447 Merge_annotations_dirs []string
1448 Merge_inclusion_annotations_dirs []string
Paul Duffin0ff08bd2020-04-29 13:30:54 +01001449 Generate_stubs *bool
Anton Hanssone87b03d2020-12-21 15:29:34 +00001450 Previous_api *string
Sundong Ahn054b19a2018-10-19 13:46:09 +09001451 Check_api struct {
Anton Hanssone6056152020-12-31 10:37:27 +00001452 Current ApiToCheck
1453 Last_released ApiToCheck
Paul Duffin160fe412020-05-10 19:32:20 +01001454
1455 Api_lint struct {
1456 Enabled *bool
1457 New_since *string
1458 Baseline_file *string
1459 }
Jiyong Park58c518b2018-05-12 22:29:12 +09001460 }
Sundong Ahn1b92c822018-05-29 11:35:17 +09001461 Aidl struct {
1462 Include_dirs []string
1463 Local_include_dirs []string
1464 }
Paul Duffin040e9062020-11-23 17:41:36 +00001465 Dists []android.Dist
Jiyong Parkc678ad32018-04-10 13:07:10 +09001466 }{}
1467
Paul Duffin7b78b4d2020-04-28 14:08:32 +01001468 // The stubs source processing uses the same compile time classpath when extracting the
1469 // API from the implementation library as it does when compiling it. i.e. the same
1470 // * sdk version
1471 // * system_modules
1472 // * libs (static_libs/libs)
Paul Duffin250e6192019-06-07 10:44:37 +01001473
Paul Duffin0ff08bd2020-04-29 13:30:54 +01001474 props.Name = proptools.StringPtr(name)
Anton Hansson944e77d2020-08-19 11:40:22 +01001475 props.Visibility = childModuleVisibility(module.sdkLibraryProperties.Stubs_source_visibility)
Paul Duffina18abc22020-05-16 18:54:24 +01001476 props.Srcs = append(props.Srcs, module.properties.Srcs...)
Anton Hanssonb727c1e2021-09-16 14:24:13 +01001477 props.Srcs = append(props.Srcs, module.sdkLibraryProperties.Api_srcs...)
Paul Duffina18abc22020-05-16 18:54:24 +01001478 props.Sdk_version = module.deviceProperties.Sdk_version
1479 props.System_modules = module.deviceProperties.System_modules
Jiyong Parkc678ad32018-04-10 13:07:10 +09001480 props.Installable = proptools.BoolPtr(false)
Sundong Ahne6f0b052018-06-05 16:46:14 +09001481 // A droiddoc module has only one Libs property and doesn't distinguish between
1482 // shared libs and static libs. So we need to add both of these libs to Libs property.
Paul Duffina18abc22020-05-16 18:54:24 +01001483 props.Libs = module.properties.Libs
1484 props.Libs = append(props.Libs, module.properties.Static_libs...)
1485 props.Aidl.Include_dirs = module.deviceProperties.Aidl.Include_dirs
1486 props.Aidl.Local_include_dirs = module.deviceProperties.Aidl.Local_include_dirs
1487 props.Java_version = module.properties.Java_version
Jiyong Parkc678ad32018-04-10 13:07:10 +09001488
Paul Duffine22c2ab2020-05-20 19:35:27 +01001489 props.Annotations_enabled = module.sdkLibraryProperties.Annotations_enabled
Sundong Ahn054b19a2018-10-19 13:46:09 +09001490 props.Merge_annotations_dirs = module.sdkLibraryProperties.Merge_annotations_dirs
1491 props.Merge_inclusion_annotations_dirs = module.sdkLibraryProperties.Merge_inclusion_annotations_dirs
1492
Paul Duffin6d0886e2020-04-07 18:49:53 +01001493 droidstubsArgs := []string{}
Paul Duffin235ffff2019-12-24 10:41:30 +00001494 if len(module.sdkLibraryProperties.Api_packages) != 0 {
Paul Duffin6d0886e2020-04-07 18:49:53 +01001495 droidstubsArgs = append(droidstubsArgs, "--stub-packages "+strings.Join(module.sdkLibraryProperties.Api_packages, ":"))
Paul Duffin235ffff2019-12-24 10:41:30 +00001496 }
1497 if len(module.sdkLibraryProperties.Hidden_api_packages) != 0 {
Paul Duffin6d0886e2020-04-07 18:49:53 +01001498 droidstubsArgs = append(droidstubsArgs,
Paul Duffin235ffff2019-12-24 10:41:30 +00001499 android.JoinWithPrefix(module.sdkLibraryProperties.Hidden_api_packages, " --hide-package "))
1500 }
Paul Duffin6d0886e2020-04-07 18:49:53 +01001501 droidstubsArgs = append(droidstubsArgs, module.sdkLibraryProperties.Droiddoc_options...)
Paul Duffin235ffff2019-12-24 10:41:30 +00001502 disabledWarnings := []string{
1503 "MissingPermission",
1504 "BroadcastBehavior",
1505 "HiddenSuperclass",
1506 "DeprecationMismatch",
1507 "UnavailableSymbol",
1508 "SdkConstant",
1509 "HiddenTypeParameter",
1510 "Todo",
1511 "Typo",
1512 }
Paul Duffin6d0886e2020-04-07 18:49:53 +01001513 droidstubsArgs = append(droidstubsArgs, android.JoinWithPrefix(disabledWarnings, "--hide "))
Sundong Ahnfb2721f2018-09-17 13:23:09 +09001514
Paul Duffin6877e6d2020-09-25 19:59:14 +01001515 // Output Javadoc comments for public scope.
1516 if apiScope == apiScopePublic {
1517 props.Output_javadoc_comments = proptools.BoolPtr(true)
1518 }
1519
Paul Duffin1fb487d2020-04-07 18:50:10 +01001520 // Add in scope specific arguments.
Paul Duffin0ff08bd2020-04-29 13:30:54 +01001521 droidstubsArgs = append(droidstubsArgs, scopeSpecificDroidstubsArgs...)
Paul Duffin11512472019-02-11 15:55:17 +00001522 props.Arg_files = module.sdkLibraryProperties.Droiddoc_option_files
Paul Duffin6d0886e2020-04-07 18:49:53 +01001523 props.Args = proptools.StringPtr(strings.Join(droidstubsArgs, " "))
Jiyong Parkc678ad32018-04-10 13:07:10 +09001524
Paul Duffin15f34ef2020-07-20 18:04:44 +01001525 // List of APIs identified from the provided source files are created. They are later
1526 // compared against to the not-yet-released (a.k.a current) list of APIs and to the
1527 // last-released (a.k.a numbered) list of API.
1528 currentApiFileName := apiScope.apiFilePrefix + "current.txt"
1529 removedApiFileName := apiScope.apiFilePrefix + "removed.txt"
1530 apiDir := module.getApiDir()
1531 currentApiFileName = path.Join(apiDir, currentApiFileName)
1532 removedApiFileName = path.Join(apiDir, removedApiFileName)
Jiyong Parkc678ad32018-04-10 13:07:10 +09001533
Paul Duffin15f34ef2020-07-20 18:04:44 +01001534 // check against the not-yet-release API
1535 props.Check_api.Current.Api_file = proptools.StringPtr(currentApiFileName)
1536 props.Check_api.Current.Removed_api_file = proptools.StringPtr(removedApiFileName)
Jiyong Park58c518b2018-05-12 22:29:12 +09001537
Anton Hanssone6056152020-12-31 10:37:27 +00001538 if !(apiScope.unstable || module.sdkLibraryProperties.Unsafe_ignore_missing_latest_api) {
Paul Duffin15f34ef2020-07-20 18:04:44 +01001539 // check against the latest released API
1540 latestApiFilegroupName := proptools.StringPtr(module.latestApiFilegroupName(apiScope))
Anton Hanssone87b03d2020-12-21 15:29:34 +00001541 props.Previous_api = latestApiFilegroupName
Paul Duffin15f34ef2020-07-20 18:04:44 +01001542 props.Check_api.Last_released.Api_file = latestApiFilegroupName
1543 props.Check_api.Last_released.Removed_api_file = proptools.StringPtr(
1544 module.latestRemovedApiFilegroupName(apiScope))
Jaewoong Jung1a97ee02021-03-09 13:25:02 -08001545 props.Check_api.Last_released.Baseline_file = proptools.StringPtr(
1546 module.latestIncompatibilitiesFilegroupName(apiScope))
Paul Duffin160fe412020-05-10 19:32:20 +01001547
Paul Duffin15f34ef2020-07-20 18:04:44 +01001548 if proptools.Bool(module.sdkLibraryProperties.Api_lint.Enabled) {
1549 // Enable api lint.
1550 props.Check_api.Api_lint.Enabled = proptools.BoolPtr(true)
1551 props.Check_api.Api_lint.New_since = latestApiFilegroupName
Paul Duffin160fe412020-05-10 19:32:20 +01001552
Paul Duffin15f34ef2020-07-20 18:04:44 +01001553 // If it exists then pass a lint-baseline.txt through to droidstubs.
1554 baselinePath := path.Join(apiDir, apiScope.apiFilePrefix+"lint-baseline.txt")
1555 baselinePathRelativeToRoot := path.Join(mctx.ModuleDir(), baselinePath)
1556 paths, err := mctx.GlobWithDeps(baselinePathRelativeToRoot, nil)
1557 if err != nil {
1558 mctx.ModuleErrorf("error checking for presence of %s: %s", baselinePathRelativeToRoot, err)
1559 }
1560 if len(paths) == 1 {
1561 props.Check_api.Api_lint.Baseline_file = proptools.StringPtr(baselinePath)
1562 } else if len(paths) != 0 {
1563 mctx.ModuleErrorf("error checking for presence of %s: expected one path, found: %v", baselinePathRelativeToRoot, paths)
Paul Duffin160fe412020-05-10 19:32:20 +01001564 }
1565 }
Paul Duffin15f34ef2020-07-20 18:04:44 +01001566 }
Jiyong Park58c518b2018-05-12 22:29:12 +09001567
Paul Duffin15f34ef2020-07-20 18:04:44 +01001568 if !Bool(module.sdkLibraryProperties.No_dist) {
Paul Duffin040e9062020-11-23 17:41:36 +00001569 // Dist the api txt and removed api txt artifacts for sdk builds.
1570 distDir := proptools.StringPtr(path.Join(module.apiDistPath(apiScope), "api"))
1571 for _, p := range []struct {
1572 tag string
1573 pattern string
1574 }{
1575 {tag: ".api.txt", pattern: "%s.txt"},
1576 {tag: ".removed-api.txt", pattern: "%s-removed.txt"},
1577 } {
1578 props.Dists = append(props.Dists, android.Dist{
1579 Targets: []string{"sdk", "win_sdk"},
1580 Dir: distDir,
1581 Dest: proptools.StringPtr(fmt.Sprintf(p.pattern, module.distStem())),
1582 Tag: proptools.StringPtr(p.tag),
1583 })
1584 }
Anton Hansson5fd5d242020-03-27 19:43:19 +00001585 }
1586
Colin Cross84dfc3d2019-09-25 11:33:01 -07001587 mctx.CreateModule(DroidstubsFactory, &props)
Jiyong Parkc678ad32018-04-10 13:07:10 +09001588}
1589
Paul Duffin2f7ccd52021-06-24 13:25:57 +01001590// Implements android.ApexModule
Jooyung Han5e9013b2020-03-10 06:23:13 +09001591func (module *SdkLibrary) DepIsInSameApex(mctx android.BaseModuleContext, dep android.Module) bool {
1592 depTag := mctx.OtherModuleDependencyTag(dep)
1593 if depTag == xmlPermissionsFileTag {
1594 return true
1595 }
1596 return module.Library.DepIsInSameApex(mctx, dep)
1597}
1598
Paul Duffin2f7ccd52021-06-24 13:25:57 +01001599// Implements android.ApexModule
1600func (module *SdkLibrary) UniqueApexVariations() bool {
1601 return module.uniqueApexVariations()
1602}
1603
Jiyong Parkc678ad32018-04-10 13:07:10 +09001604// Creates the xml file that publicizes the runtime library
Paul Duffinf0229202020-04-29 16:47:28 +01001605func (module *SdkLibrary) createXmlFile(mctx android.DefaultableHookContext) {
Jiyong Parke3833882020-02-17 17:28:10 +09001606 props := struct {
Paul Duffin1dbe3ca2020-05-16 09:57:59 +01001607 Name *string
1608 Lib_name *string
1609 Apex_available []string
Jiyong Parke3833882020-02-17 17:28:10 +09001610 }{
Paul Duffineedc5d52020-06-12 17:46:39 +01001611 Name: proptools.StringPtr(module.xmlPermissionsModuleName()),
Jooyung Han5e9013b2020-03-10 06:23:13 +09001612 Lib_name: proptools.StringPtr(module.BaseModuleName()),
1613 Apex_available: module.ApexProperties.Apex_available,
Jiyong Parkc678ad32018-04-10 13:07:10 +09001614 }
Jiyong Parke3833882020-02-17 17:28:10 +09001615
Jiyong Parke3833882020-02-17 17:28:10 +09001616 mctx.CreateModule(sdkLibraryXmlFactory, &props)
Jiyong Parkc678ad32018-04-10 13:07:10 +09001617}
1618
Jiyong Parkf1691d22021-03-29 20:11:58 +09001619func PrebuiltJars(ctx android.BaseModuleContext, baseName string, s android.SdkSpec) android.Paths {
Jiyong Park54105c42021-03-31 18:17:53 +09001620 var ver android.ApiLevel
Jiyong Parkf1691d22021-03-29 20:11:58 +09001621 var kind android.SdkKind
1622 if s.UsePrebuilt(ctx) {
Jiyong Park54105c42021-03-31 18:17:53 +09001623 ver = s.ApiLevel
Jiyong Parkf1691d22021-03-29 20:11:58 +09001624 kind = s.Kind
Jiyong Parkc678ad32018-04-10 13:07:10 +09001625 } else {
Jiyong Park6a927c42020-01-21 02:03:43 +09001626 // We don't have prebuilt SDK for the specific sdkVersion.
1627 // Instead of breaking the build, fallback to use "system_current"
Jiyong Park54105c42021-03-31 18:17:53 +09001628 ver = android.FutureApiLevel
Jiyong Parkf1691d22021-03-29 20:11:58 +09001629 kind = android.SdkSystem
Sundong Ahn054b19a2018-10-19 13:46:09 +09001630 }
Jiyong Park6a927c42020-01-21 02:03:43 +09001631
1632 dir := filepath.Join("prebuilts", "sdk", ver.String(), kind.String())
Paul Duffin50061512020-01-21 16:31:05 +00001633 jar := filepath.Join(dir, baseName+".jar")
Sundong Ahn054b19a2018-10-19 13:46:09 +09001634 jarPath := android.ExistentPathForSource(ctx, jar)
Sundong Ahnae418ac2019-02-28 15:01:28 +09001635 if !jarPath.Valid() {
Colin Cross07c88562020-01-07 09:34:44 -08001636 if ctx.Config().AllowMissingDependencies() {
1637 return android.Paths{android.PathForSource(ctx, jar)}
1638 } else {
Jiyong Parkf1691d22021-03-29 20:11:58 +09001639 ctx.PropertyErrorf("sdk_library", "invalid sdk version %q, %q does not exist", s.Raw, jar)
Colin Cross07c88562020-01-07 09:34:44 -08001640 }
Sundong Ahnae418ac2019-02-28 15:01:28 +09001641 return nil
1642 }
Sundong Ahn054b19a2018-10-19 13:46:09 +09001643 return android.Paths{jarPath.Path()}
1644}
1645
Colin Crossaede88c2020-08-11 12:17:01 -07001646// 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 +01001647//
1648// If either this or the other module are on the platform then this will return
1649// false.
Colin Cross56a83212020-09-15 18:30:11 -07001650func withinSameApexesAs(ctx android.BaseModuleContext, other android.Module) bool {
1651 apexInfo := ctx.Provider(android.ApexInfoProvider).(android.ApexInfo)
1652 otherApexInfo := ctx.OtherModuleProvider(other, android.ApexInfoProvider).(android.ApexInfo)
Jiyong Park712e8b52021-05-12 17:13:56 +09001653 return len(otherApexInfo.InApexVariants) > 0 && reflect.DeepEqual(apexInfo.InApexVariants, otherApexInfo.InApexVariants)
Paul Duffin9b879592020-05-26 13:21:35 +01001654}
1655
Jiyong Parkf1691d22021-03-29 20:11:58 +09001656func (module *SdkLibrary) sdkJars(ctx android.BaseModuleContext, sdkVersion android.SdkSpec, headerJars bool) android.Paths {
Jiyong Park932cdfe2020-05-28 00:19:53 +09001657 // If the client doesn't set sdk_version, but if this library prefers stubs over
1658 // the impl library, let's provide the widest API surface possible. To do so,
1659 // force override sdk_version to module_current so that the closest possible API
1660 // surface could be found in selectHeaderJarsForSdkVersion
Jiyong Parkf1691d22021-03-29 20:11:58 +09001661 if module.defaultsToStubs() && !sdkVersion.Specified() {
Jiyong Park92315372021-04-02 08:45:46 +09001662 sdkVersion = android.SdkSpecFrom(ctx, "module_current")
Jiyong Park932cdfe2020-05-28 00:19:53 +09001663 }
Paul Duffind1b3a922020-01-22 11:57:20 +00001664
Paul Duffindaaa3322020-05-26 18:13:57 +01001665 // Only provide access to the implementation library if it is actually built.
1666 if module.requiresRuntimeImplementationLibrary() {
1667 // Check any special cases for java_sdk_library.
1668 //
1669 // Only allow access to the implementation library in the following condition:
1670 // * No sdk_version specified on the referencing module.
Paul Duffin9b879592020-05-26 13:21:35 +01001671 // * The referencing module is in the same apex as this.
Jiyong Parkf1691d22021-03-29 20:11:58 +09001672 if sdkVersion.Kind == android.SdkPrivate || withinSameApexesAs(ctx, module) {
Paul Duffindaaa3322020-05-26 18:13:57 +01001673 if headerJars {
1674 return module.HeaderJars()
1675 } else {
1676 return module.ImplementationJars()
1677 }
Sundong Ahn054b19a2018-10-19 13:46:09 +09001678 }
Jiyong Parkc678ad32018-04-10 13:07:10 +09001679 }
Paul Duffinb05d4292020-05-20 12:19:10 +01001680
Paul Duffin23970f42020-05-20 14:20:02 +01001681 return module.selectHeaderJarsForSdkVersion(ctx, sdkVersion)
Jiyong Parkc678ad32018-04-10 13:07:10 +09001682}
1683
Sundong Ahn241cd372018-07-13 16:16:44 +09001684// to satisfy SdkLibraryDependency interface
Jiyong Parkf1691d22021-03-29 20:11:58 +09001685func (module *SdkLibrary) SdkHeaderJars(ctx android.BaseModuleContext, sdkVersion android.SdkSpec) android.Paths {
Paul Duffind1b3a922020-01-22 11:57:20 +00001686 return module.sdkJars(ctx, sdkVersion, true /*headerJars*/)
1687}
1688
1689// to satisfy SdkLibraryDependency interface
Jiyong Parkf1691d22021-03-29 20:11:58 +09001690func (module *SdkLibrary) SdkImplementationJars(ctx android.BaseModuleContext, sdkVersion android.SdkSpec) android.Paths {
Paul Duffind1b3a922020-01-22 11:57:20 +00001691 return module.sdkJars(ctx, sdkVersion, false /*headerJars*/)
Sundong Ahn241cd372018-07-13 16:16:44 +09001692}
1693
Colin Cross571cccf2019-02-04 11:22:08 -08001694var javaSdkLibrariesKey = android.NewOnceKey("javaSdkLibraries")
1695
Jiyong Park82484c02018-04-23 21:41:26 +09001696func javaSdkLibraries(config android.Config) *[]string {
Colin Cross571cccf2019-02-04 11:22:08 -08001697 return config.Once(javaSdkLibrariesKey, func() interface{} {
Jiyong Park82484c02018-04-23 21:41:26 +09001698 return &[]string{}
1699 }).(*[]string)
1700}
1701
Paul Duffin749f98f2019-12-30 17:23:46 +00001702func (module *SdkLibrary) getApiDir() string {
1703 return proptools.StringDefault(module.sdkLibraryProperties.Api_dir, "api")
1704}
1705
Jiyong Parkc678ad32018-04-10 13:07:10 +09001706// For a java_sdk_library module, create internal modules for stubs, docs,
1707// runtime libs and xml file. If requested, the stubs and docs are created twice
1708// once for public API level and once for system API level
Paul Duffinf0229202020-04-29 16:47:28 +01001709func (module *SdkLibrary) CreateInternalModules(mctx android.DefaultableHookContext) {
1710 // If the module has been disabled then don't create any child modules.
1711 if !module.Enabled() {
1712 return
1713 }
1714
Paul Duffina18abc22020-05-16 18:54:24 +01001715 if len(module.properties.Srcs) == 0 {
Inseob Kimc0907f12019-02-08 21:00:45 +09001716 mctx.PropertyErrorf("srcs", "java_sdk_library must specify srcs")
Jooyung Han58f26ab2019-12-18 15:34:32 +09001717 return
Inseob Kimc0907f12019-02-08 21:00:45 +09001718 }
1719
Paul Duffin37e0b772019-12-30 17:20:10 +00001720 // If this builds against standard libraries (i.e. is not part of the core libraries)
Paul Duffin4f5c1ef2020-11-19 14:53:43 +00001721 // then assume it provides both system and test apis.
Jiyong Parkf1691d22021-03-29 20:11:58 +09001722 sdkDep := decodeSdkDep(mctx, android.SdkContext(&module.Library))
Paul Duffin37e0b772019-12-30 17:20:10 +00001723 hasSystemAndTestApis := sdkDep.hasStandardLibs()
Paul Duffin3375e352020-04-28 10:44:03 +01001724 module.sdkLibraryProperties.Generate_system_and_test_apis = hasSystemAndTestApis
Paul Duffin4f5c1ef2020-11-19 14:53:43 +00001725
Jaewoong Jung18aefc12020-12-21 09:11:10 -08001726 missingCurrentApi := false
Inseob Kim8098faa2019-03-18 10:19:51 +09001727
Paul Duffin3375e352020-04-28 10:44:03 +01001728 generatedScopes := module.getGeneratedApiScopes(mctx)
Paul Duffind1b3a922020-01-22 11:57:20 +00001729
Paul Duffin749f98f2019-12-30 17:23:46 +00001730 apiDir := module.getApiDir()
Paul Duffin3375e352020-04-28 10:44:03 +01001731 for _, scope := range generatedScopes {
Inseob Kim8098faa2019-03-18 10:19:51 +09001732 for _, api := range []string{"current.txt", "removed.txt"} {
Paul Duffind1b3a922020-01-22 11:57:20 +00001733 path := path.Join(mctx.ModuleDir(), apiDir, scope.apiFilePrefix+api)
Inseob Kim8098faa2019-03-18 10:19:51 +09001734 p := android.ExistentPathForSource(mctx, path)
1735 if !p.Valid() {
1736 mctx.ModuleErrorf("Current api file %#v doesn't exist", path)
Jaewoong Jung18aefc12020-12-21 09:11:10 -08001737 missingCurrentApi = true
Inseob Kim8098faa2019-03-18 10:19:51 +09001738 }
1739 }
1740 }
1741
Jaewoong Jung18aefc12020-12-21 09:11:10 -08001742 if missingCurrentApi {
Inseob Kim8098faa2019-03-18 10:19:51 +09001743 script := "build/soong/scripts/gen-java-current-api-files.sh"
1744 p := android.ExistentPathForSource(mctx, script)
1745
1746 if !p.Valid() {
1747 panic(fmt.Sprintf("script file %s doesn't exist", script))
1748 }
1749
1750 mctx.ModuleErrorf("One or more current api files are missing. "+
1751 "You can update them by:\n"+
Paul Duffin37e0b772019-12-30 17:20:10 +00001752 "%s %q %s && m update-api",
Paul Duffind1b3a922020-01-22 11:57:20 +00001753 script, filepath.Join(mctx.ModuleDir(), apiDir),
Paul Duffin3375e352020-04-28 10:44:03 +01001754 strings.Join(generatedScopes.Strings(func(s *apiScope) string { return s.apiFilePrefix }), " "))
Inseob Kim8098faa2019-03-18 10:19:51 +09001755 return
1756 }
1757
Paul Duffin3375e352020-04-28 10:44:03 +01001758 for _, scope := range generatedScopes {
Paul Duffin15f34ef2020-07-20 18:04:44 +01001759 // Use the stubs source name for legacy reasons.
1760 module.createStubsSourcesAndApi(mctx, scope, module.stubsSourceModuleName(scope), scope.droidstubsArgs)
Paul Duffin0ff08bd2020-04-29 13:30:54 +01001761
Paul Duffind1b3a922020-01-22 11:57:20 +00001762 module.createStubsLibrary(mctx, scope)
Inseob Kimc0907f12019-02-08 21:00:45 +09001763 }
1764
Paul Duffindfa131e2020-05-15 20:37:11 +01001765 if module.requiresRuntimeImplementationLibrary() {
Paul Duffin5df79302020-05-16 15:52:12 +01001766 // Create child module to create an implementation library.
1767 //
1768 // This temporarily creates a second implementation library that can be explicitly
1769 // referenced.
1770 //
1771 // TODO(b/156618935) - update comment once only one implementation library is created.
1772 module.createImplLibrary(mctx)
1773
Paul Duffindfa131e2020-05-15 20:37:11 +01001774 // Only create an XML permissions file that declares the library as being usable
1775 // as a shared library if required.
1776 if module.sharedLibrary() {
1777 module.createXmlFile(mctx)
1778 }
Paul Duffin43db9be2019-12-30 17:35:49 +00001779
1780 // record java_sdk_library modules so that they are exported to make
1781 javaSdkLibraries := javaSdkLibraries(mctx.Config())
1782 javaSdkLibrariesLock.Lock()
1783 defer javaSdkLibrariesLock.Unlock()
1784 *javaSdkLibraries = append(*javaSdkLibraries, module.BaseModuleName())
1785 }
Anton Hansson7f66efa2020-10-08 14:47:23 +01001786
1787 // Add the impl_only_libs *after* we're done using the Libs prop in submodules.
1788 module.properties.Libs = append(module.properties.Libs, module.sdkLibraryProperties.Impl_only_libs...)
Inseob Kimc0907f12019-02-08 21:00:45 +09001789}
1790
1791func (module *SdkLibrary) InitSdkLibraryProperties() {
Colin Crossce6734e2020-06-15 16:09:53 -07001792 module.addHostAndDeviceProperties()
1793 module.AddProperties(&module.sdkLibraryProperties)
Sundong Ahn054b19a2018-10-19 13:46:09 +09001794
Paul Duffin3accbb52021-06-23 11:39:47 +01001795 module.initSdkLibraryComponent(module)
Paul Duffin859fe962020-05-15 10:20:31 +01001796
Paul Duffina18abc22020-05-16 18:54:24 +01001797 module.properties.Installable = proptools.BoolPtr(true)
1798 module.deviceProperties.IsSDKLibrary = true
Inseob Kimc0907f12019-02-08 21:00:45 +09001799}
Sundong Ahn054b19a2018-10-19 13:46:09 +09001800
Paul Duffindfa131e2020-05-15 20:37:11 +01001801func (module *SdkLibrary) requiresRuntimeImplementationLibrary() bool {
1802 return !proptools.Bool(module.sdkLibraryProperties.Api_only)
1803}
1804
Jiyong Park932cdfe2020-05-28 00:19:53 +09001805func (module *SdkLibrary) defaultsToStubs() bool {
1806 return proptools.Bool(module.sdkLibraryProperties.Default_to_stubs)
1807}
1808
Paul Duffin1b1e8062020-05-08 13:44:43 +01001809// Defines how to name the individual component modules the sdk library creates.
1810type sdkLibraryComponentNamingScheme interface {
1811 stubsLibraryModuleName(scope *apiScope, baseName string) string
1812
1813 stubsSourceModuleName(scope *apiScope, baseName string) string
Paul Duffin1b1e8062020-05-08 13:44:43 +01001814}
1815
1816type defaultNamingScheme struct {
1817}
1818
1819func (s *defaultNamingScheme) stubsLibraryModuleName(scope *apiScope, baseName string) string {
1820 return scope.stubsLibraryModuleName(baseName)
1821}
1822
1823func (s *defaultNamingScheme) stubsSourceModuleName(scope *apiScope, baseName string) string {
1824 return scope.stubsSourceModuleName(baseName)
1825}
1826
Paul Duffin1b1e8062020-05-08 13:44:43 +01001827var _ sdkLibraryComponentNamingScheme = (*defaultNamingScheme)(nil)
1828
Jaewoong Jungbc15e3a2021-03-10 17:02:43 -08001829func moduleStubLinkType(name string) (stub bool, ret sdkLinkType) {
Anton Hansson2d0c1942020-05-25 12:20:51 +01001830 // This suffix-based approach is fragile and could potentially mis-trigger.
1831 // TODO(b/155164730): Clean this up when modules no longer reference sdk_lib stubs directly.
Anton Hansson08f476b2021-04-07 15:32:19 +01001832 if strings.HasSuffix(name, apiScopePublic.stubsLibraryModuleNameSuffix()) {
1833 if name == "hwbinder.stubs" || name == "libcore_private.stubs" {
1834 // Due to a previous bug, these modules were not considered stubs, so we retain that.
1835 return false, javaPlatform
1836 }
Anton Hansson2d0c1942020-05-25 12:20:51 +01001837 return true, javaSdk
1838 }
Anton Hansson08f476b2021-04-07 15:32:19 +01001839 if strings.HasSuffix(name, apiScopeSystem.stubsLibraryModuleNameSuffix()) {
Anton Hansson2d0c1942020-05-25 12:20:51 +01001840 return true, javaSystem
1841 }
Anton Hansson08f476b2021-04-07 15:32:19 +01001842 if strings.HasSuffix(name, apiScopeModuleLib.stubsLibraryModuleNameSuffix()) {
Anton Hansson2d0c1942020-05-25 12:20:51 +01001843 return true, javaModule
1844 }
Anton Hansson08f476b2021-04-07 15:32:19 +01001845 if strings.HasSuffix(name, apiScopeTest.stubsLibraryModuleNameSuffix()) {
Anton Hansson2d0c1942020-05-25 12:20:51 +01001846 return true, javaSystem
1847 }
1848 return false, javaPlatform
1849}
1850
Jaewoong Jung4f158ee2019-07-11 10:05:35 -07001851// java_sdk_library is a special Java library that provides optional platform APIs to apps.
1852// In practice, it can be viewed as a combination of several modules: 1) stubs library that clients
1853// are linked against to, 2) droiddoc module that internally generates API stubs source files,
1854// 3) the real runtime shared library that implements the APIs, and 4) XML file for adding
1855// the runtime lib to the classpath at runtime if requested via <uses-library>.
Inseob Kimc0907f12019-02-08 21:00:45 +09001856func SdkLibraryFactory() android.Module {
1857 module := &SdkLibrary{}
Paul Duffinc3091c82020-05-08 14:16:20 +01001858
1859 // Initialize information common between source and prebuilt.
Paul Duffin3accbb52021-06-23 11:39:47 +01001860 module.initCommon(module)
Paul Duffinc3091c82020-05-08 14:16:20 +01001861
Inseob Kimc0907f12019-02-08 21:00:45 +09001862 module.InitSdkLibraryProperties()
Jooyung Han58f26ab2019-12-18 15:34:32 +09001863 android.InitApexModule(module)
Paul Duffinb6b89a42021-05-06 16:33:43 +01001864 android.InitSdkAwareModule(module)
Sundong Ahn054b19a2018-10-19 13:46:09 +09001865 InitJavaModule(module, android.HostAndDeviceSupported)
Paul Duffin3375e352020-04-28 10:44:03 +01001866
1867 // Initialize the map from scope to scope specific properties.
1868 scopeToProperties := make(map[*apiScope]*ApiScopeProperties)
1869 for _, scope := range allApiScopes {
1870 scopeToProperties[scope] = scope.scopeSpecificProperties(module)
1871 }
1872 module.scopeToProperties = scopeToProperties
1873
Paul Duffin4911a892020-04-29 23:35:13 +01001874 // Add the properties containing visibility rules so that they are checked.
Paul Duffin5df79302020-05-16 15:52:12 +01001875 android.AddVisibilityProperty(module, "impl_library_visibility", &module.sdkLibraryProperties.Impl_library_visibility)
Paul Duffin4911a892020-04-29 23:35:13 +01001876 android.AddVisibilityProperty(module, "stubs_library_visibility", &module.sdkLibraryProperties.Stubs_library_visibility)
1877 android.AddVisibilityProperty(module, "stubs_source_visibility", &module.sdkLibraryProperties.Stubs_source_visibility)
1878
Paul Duffin1b1e8062020-05-08 13:44:43 +01001879 module.SetDefaultableHook(func(ctx android.DefaultableHookContext) {
Paul Duffindfa131e2020-05-15 20:37:11 +01001880 // If no implementation is required then it cannot be used as a shared library
1881 // either.
1882 if !module.requiresRuntimeImplementationLibrary() {
1883 // If shared_library has been explicitly set to true then it is incompatible
1884 // with api_only: true.
1885 if proptools.Bool(module.commonSdkLibraryProperties.Shared_library) {
1886 ctx.PropertyErrorf("api_only/shared_library", "inconsistent settings, shared_library and api_only cannot both be true")
1887 }
1888 // Set shared_library: false.
1889 module.commonSdkLibraryProperties.Shared_library = proptools.BoolPtr(false)
1890 }
1891
Paul Duffin1b1e8062020-05-08 13:44:43 +01001892 if module.initCommonAfterDefaultsApplied(ctx) {
1893 module.CreateInternalModules(ctx)
1894 }
1895 })
Jiyong Parkc678ad32018-04-10 13:07:10 +09001896 return module
1897}
Colin Cross79c7c262019-04-17 11:11:46 -07001898
1899//
1900// SDK library prebuilts
1901//
1902
Paul Duffin56d44902020-01-31 13:36:25 +00001903// Properties associated with each api scope.
1904type sdkLibraryScopeProperties struct {
Colin Cross79c7c262019-04-17 11:11:46 -07001905 Jars []string `android:"path"`
1906
1907 Sdk_version *string
1908
Colin Cross79c7c262019-04-17 11:11:46 -07001909 // List of shared java libs that this module has dependencies to
1910 Libs []string
Paul Duffin3d1248c2020-04-09 00:10:17 +01001911
Paul Duffinc8782502020-04-29 20:45:27 +01001912 // The stubs source.
Paul Duffin3d1248c2020-04-09 00:10:17 +01001913 Stub_srcs []string `android:"path"`
Paul Duffin1fd005d2020-04-09 01:08:11 +01001914
1915 // The current.txt
Paul Duffin0f8faff2020-05-20 16:18:00 +01001916 Current_api *string `android:"path"`
Paul Duffin1fd005d2020-04-09 01:08:11 +01001917
1918 // The removed.txt
Paul Duffin0f8faff2020-05-20 16:18:00 +01001919 Removed_api *string `android:"path"`
Anton Hansson3adf3c52021-09-21 15:25:12 +01001920
1921 // Annotation zip
1922 Annotations *string `android:"path"`
Colin Cross79c7c262019-04-17 11:11:46 -07001923}
1924
Paul Duffin56d44902020-01-31 13:36:25 +00001925type sdkLibraryImportProperties struct {
Paul Duffinfcfd7912020-01-31 17:54:30 +00001926 // List of shared java libs, common to all scopes, that this module has
1927 // dependencies to
1928 Libs []string
Paul Duffin1267d872021-04-16 17:21:36 +01001929
1930 // If set to true, compile dex files for the stubs. Defaults to false.
1931 Compile_dex *bool
Paul Duffin7b3e10a2021-07-15 14:14:41 +01001932
1933 // If not empty, classes are restricted to the specified packages and their sub-packages.
1934 // This information is used to generate the updatable-bcp-packages.txt file.
1935 Permitted_packages []string
Paul Duffin56d44902020-01-31 13:36:25 +00001936}
1937
Paul Duffineedc5d52020-06-12 17:46:39 +01001938type SdkLibraryImport struct {
Colin Cross79c7c262019-04-17 11:11:46 -07001939 android.ModuleBase
1940 android.DefaultableModuleBase
1941 prebuilt android.Prebuilt
Paul Duffindd46f712020-02-10 13:37:10 +00001942 android.ApexModuleBase
1943 android.SdkBase
Colin Cross79c7c262019-04-17 11:11:46 -07001944
Paul Duffin37856732021-02-26 14:24:15 +00001945 hiddenAPI
1946
Colin Cross79c7c262019-04-17 11:11:46 -07001947 properties sdkLibraryImportProperties
1948
Paul Duffin46a26a82020-04-07 19:27:04 +01001949 // Map from api scope to the scope specific property structure.
1950 scopeProperties map[*apiScope]*sdkLibraryScopeProperties
1951
Paul Duffin56d44902020-01-31 13:36:25 +00001952 commonToSdkLibraryAndImport
Paul Duffineedc5d52020-06-12 17:46:39 +01001953
1954 // The reference to the implementation library created by the source module.
1955 // Is nil if the source module does not exist.
1956 implLibraryModule *Library
1957
1958 // The reference to the xml permissions module created by the source module.
1959 // Is nil if the source module does not exist.
1960 xmlPermissionsFileModule *sdkLibraryXml
Paul Duffin39853512021-02-26 11:09:39 +00001961
1962 // Path to the dex implementation jar obtained from the prebuilt_apex, if any.
1963 dexJarFile android.Path
Colin Cross79c7c262019-04-17 11:11:46 -07001964}
1965
Paul Duffineedc5d52020-06-12 17:46:39 +01001966var _ SdkLibraryDependency = (*SdkLibraryImport)(nil)
Colin Cross79c7c262019-04-17 11:11:46 -07001967
Paul Duffin46a26a82020-04-07 19:27:04 +01001968// The type of a structure that contains a field of type sdkLibraryScopeProperties
1969// for each apiscope in allApiScopes, e.g. something like:
1970// struct {
1971// Public sdkLibraryScopeProperties
1972// System sdkLibraryScopeProperties
1973// ...
1974// }
1975var allScopeStructType = createAllScopePropertiesStructType()
1976
1977// Dynamically create a structure type for each apiscope in allApiScopes.
1978func createAllScopePropertiesStructType() reflect.Type {
1979 var fields []reflect.StructField
1980 for _, apiScope := range allApiScopes {
1981 field := reflect.StructField{
1982 Name: apiScope.fieldName,
1983 Type: reflect.TypeOf(sdkLibraryScopeProperties{}),
1984 }
1985 fields = append(fields, field)
1986 }
1987
1988 return reflect.StructOf(fields)
1989}
1990
1991// Create an instance of the scope specific structure type and return a map
1992// from apiscope to a pointer to each scope specific field.
1993func createPropertiesInstance() (interface{}, map[*apiScope]*sdkLibraryScopeProperties) {
1994 allScopePropertiesPtr := reflect.New(allScopeStructType)
1995 allScopePropertiesStruct := allScopePropertiesPtr.Elem()
1996 scopeProperties := make(map[*apiScope]*sdkLibraryScopeProperties)
1997
1998 for _, apiScope := range allApiScopes {
1999 field := allScopePropertiesStruct.FieldByName(apiScope.fieldName)
2000 scopeProperties[apiScope] = field.Addr().Interface().(*sdkLibraryScopeProperties)
2001 }
2002
2003 return allScopePropertiesPtr.Interface(), scopeProperties
2004}
2005
Jaewoong Jung4f158ee2019-07-11 10:05:35 -07002006// java_sdk_library_import imports a prebuilt java_sdk_library.
Colin Cross79c7c262019-04-17 11:11:46 -07002007func sdkLibraryImportFactory() android.Module {
Paul Duffineedc5d52020-06-12 17:46:39 +01002008 module := &SdkLibraryImport{}
Colin Cross79c7c262019-04-17 11:11:46 -07002009
Paul Duffin46a26a82020-04-07 19:27:04 +01002010 allScopeProperties, scopeToProperties := createPropertiesInstance()
2011 module.scopeProperties = scopeToProperties
2012 module.AddProperties(&module.properties, allScopeProperties)
Colin Cross79c7c262019-04-17 11:11:46 -07002013
Paul Duffinc3091c82020-05-08 14:16:20 +01002014 // Initialize information common between source and prebuilt.
Paul Duffin3accbb52021-06-23 11:39:47 +01002015 module.initCommon(module)
Paul Duffinc3091c82020-05-08 14:16:20 +01002016
Paul Duffin0bdcb272020-02-06 15:24:57 +00002017 android.InitPrebuiltModule(module, &[]string{""})
Paul Duffindd46f712020-02-10 13:37:10 +00002018 android.InitApexModule(module)
2019 android.InitSdkAwareModule(module)
Colin Cross79c7c262019-04-17 11:11:46 -07002020 InitJavaModule(module, android.HostAndDeviceSupported)
2021
Paul Duffin1b1e8062020-05-08 13:44:43 +01002022 module.SetDefaultableHook(func(mctx android.DefaultableHookContext) {
2023 if module.initCommonAfterDefaultsApplied(mctx) {
2024 module.createInternalModules(mctx)
2025 }
2026 })
Colin Cross79c7c262019-04-17 11:11:46 -07002027 return module
2028}
2029
Paul Duffindbcc2962021-07-15 13:35:26 +01002030var _ PermittedPackagesForUpdatableBootJars = (*SdkLibraryImport)(nil)
2031
2032func (module *SdkLibraryImport) PermittedPackagesForUpdatableBootJars() []string {
2033 return module.properties.Permitted_packages
2034}
2035
Paul Duffineedc5d52020-06-12 17:46:39 +01002036func (module *SdkLibraryImport) Prebuilt() *android.Prebuilt {
Colin Cross79c7c262019-04-17 11:11:46 -07002037 return &module.prebuilt
2038}
2039
Paul Duffineedc5d52020-06-12 17:46:39 +01002040func (module *SdkLibraryImport) Name() string {
Colin Cross79c7c262019-04-17 11:11:46 -07002041 return module.prebuilt.Name(module.ModuleBase.Name())
2042}
2043
Paul Duffineedc5d52020-06-12 17:46:39 +01002044func (module *SdkLibraryImport) createInternalModules(mctx android.DefaultableHookContext) {
Colin Cross79c7c262019-04-17 11:11:46 -07002045
Paul Duffin50061512020-01-21 16:31:05 +00002046 // If the build is configured to use prebuilts then force this to be preferred.
Jeongik Cha816a23a2020-07-08 01:09:23 +09002047 if mctx.Config().AlwaysUsePrebuiltSdks() {
Paul Duffin50061512020-01-21 16:31:05 +00002048 module.prebuilt.ForcePrefer()
2049 }
2050
Paul Duffin46a26a82020-04-07 19:27:04 +01002051 for apiScope, scopeProperties := range module.scopeProperties {
Paul Duffin56d44902020-01-31 13:36:25 +00002052 if len(scopeProperties.Jars) == 0 {
2053 continue
2054 }
2055
Paul Duffinbbb546b2020-04-09 00:07:11 +01002056 module.createJavaImportForStubs(mctx, apiScope, scopeProperties)
Paul Duffin3d1248c2020-04-09 00:10:17 +01002057
Paul Duffin0f8faff2020-05-20 16:18:00 +01002058 if len(scopeProperties.Stub_srcs) > 0 {
2059 module.createPrebuiltStubsSources(mctx, apiScope, scopeProperties)
2060 }
Paul Duffin56d44902020-01-31 13:36:25 +00002061 }
Colin Cross79c7c262019-04-17 11:11:46 -07002062
2063 javaSdkLibraries := javaSdkLibraries(mctx.Config())
2064 javaSdkLibrariesLock.Lock()
2065 defer javaSdkLibrariesLock.Unlock()
2066 *javaSdkLibraries = append(*javaSdkLibraries, module.BaseModuleName())
2067}
2068
Paul Duffineedc5d52020-06-12 17:46:39 +01002069func (module *SdkLibraryImport) createJavaImportForStubs(mctx android.DefaultableHookContext, apiScope *apiScope, scopeProperties *sdkLibraryScopeProperties) {
Paul Duffinbbb546b2020-04-09 00:07:11 +01002070 // Creates a java import for the jar with ".stubs" suffix
2071 props := struct {
Paul Duffin1dbe3ca2020-05-16 09:57:59 +01002072 Name *string
2073 Sdk_version *string
2074 Libs []string
2075 Jars []string
2076 Prefer *bool
Paul Duffin1267d872021-04-16 17:21:36 +01002077 Compile_dex *bool
Paul Duffinbbb546b2020-04-09 00:07:11 +01002078 }{}
Paul Duffinc3091c82020-05-08 14:16:20 +01002079 props.Name = proptools.StringPtr(module.stubsLibraryModuleName(apiScope))
Paul Duffinbbb546b2020-04-09 00:07:11 +01002080 props.Sdk_version = scopeProperties.Sdk_version
2081 // Prepend any of the libs from the legacy public properties to the libs for each of the
2082 // scopes to avoid having to duplicate them in each scope.
2083 props.Libs = append(module.properties.Libs, scopeProperties.Libs...)
2084 props.Jars = scopeProperties.Jars
Paul Duffin1dbe3ca2020-05-16 09:57:59 +01002085
Paul Duffin38b57852020-05-13 16:08:09 +01002086 // The imports are preferred if the java_sdk_library_import is preferred.
2087 props.Prefer = proptools.BoolPtr(module.prebuilt.Prefer())
Paul Duffin859fe962020-05-15 10:20:31 +01002088
Paul Duffin1267d872021-04-16 17:21:36 +01002089 // The imports need to be compiled to dex if the java_sdk_library_import requests it.
Paul Duffinf4600f62021-05-13 22:34:45 +01002090 compileDex := module.properties.Compile_dex
2091 if module.stubLibrariesCompiledForDex() {
2092 compileDex = proptools.BoolPtr(true)
2093 }
2094 props.Compile_dex = compileDex
Paul Duffin1267d872021-04-16 17:21:36 +01002095
Paul Duffin859fe962020-05-15 10:20:31 +01002096 mctx.CreateModule(ImportFactory, &props, module.sdkComponentPropertiesForChildLibrary())
Paul Duffinbbb546b2020-04-09 00:07:11 +01002097}
2098
Paul Duffineedc5d52020-06-12 17:46:39 +01002099func (module *SdkLibraryImport) createPrebuiltStubsSources(mctx android.DefaultableHookContext, apiScope *apiScope, scopeProperties *sdkLibraryScopeProperties) {
Paul Duffin3d1248c2020-04-09 00:10:17 +01002100 props := struct {
Paul Duffin38b57852020-05-13 16:08:09 +01002101 Name *string
2102 Srcs []string
2103 Prefer *bool
Paul Duffin3d1248c2020-04-09 00:10:17 +01002104 }{}
Paul Duffinc3091c82020-05-08 14:16:20 +01002105 props.Name = proptools.StringPtr(module.stubsSourceModuleName(apiScope))
Paul Duffin3d1248c2020-04-09 00:10:17 +01002106 props.Srcs = scopeProperties.Stub_srcs
2107 mctx.CreateModule(PrebuiltStubsSourcesFactory, &props)
Paul Duffin38b57852020-05-13 16:08:09 +01002108
2109 // The stubs source is preferred if the java_sdk_library_import is preferred.
2110 props.Prefer = proptools.BoolPtr(module.prebuilt.Prefer())
Paul Duffin3d1248c2020-04-09 00:10:17 +01002111}
2112
Paul Duffin44f1d842020-06-26 20:17:02 +01002113// Add the dependencies on the child module in the component deps mutator so that it
2114// creates references to the prebuilt and not the source modules.
2115func (module *SdkLibraryImport) ComponentDepsMutator(ctx android.BottomUpMutatorContext) {
Paul Duffin46a26a82020-04-07 19:27:04 +01002116 for apiScope, scopeProperties := range module.scopeProperties {
Paul Duffin56d44902020-01-31 13:36:25 +00002117 if len(scopeProperties.Jars) == 0 {
2118 continue
2119 }
2120
2121 // Add dependencies to the prebuilt stubs library
Paul Duffin864116c2021-04-02 10:24:13 +01002122 ctx.AddVariationDependencies(nil, apiScope.stubsTag, android.PrebuiltNameFromSource(module.stubsLibraryModuleName(apiScope)))
Paul Duffin0f8faff2020-05-20 16:18:00 +01002123
2124 if len(scopeProperties.Stub_srcs) > 0 {
2125 // Add dependencies to the prebuilt stubs source library
Paul Duffin864116c2021-04-02 10:24:13 +01002126 ctx.AddVariationDependencies(nil, apiScope.stubsSourceTag, android.PrebuiltNameFromSource(module.stubsSourceModuleName(apiScope)))
Paul Duffin0f8faff2020-05-20 16:18:00 +01002127 }
Paul Duffin56d44902020-01-31 13:36:25 +00002128 }
Paul Duffin44f1d842020-06-26 20:17:02 +01002129}
2130
2131// Add other dependencies as normal.
2132func (module *SdkLibraryImport) DepsMutator(ctx android.BottomUpMutatorContext) {
Paul Duffineedc5d52020-06-12 17:46:39 +01002133
2134 implName := module.implLibraryModuleName()
2135 if ctx.OtherModuleExists(implName) {
2136 ctx.AddVariationDependencies(nil, implLibraryTag, implName)
2137
2138 xmlPermissionsModuleName := module.xmlPermissionsModuleName()
2139 if module.sharedLibrary() && ctx.OtherModuleExists(xmlPermissionsModuleName) {
2140 // Add dependency to the rule for generating the xml permissions file
2141 ctx.AddDependency(module, xmlPermissionsFileTag, xmlPermissionsModuleName)
2142 }
2143 }
Colin Cross79c7c262019-04-17 11:11:46 -07002144}
2145
Jiyong Park45bf82e2020-12-15 22:29:02 +09002146var _ android.ApexModule = (*SdkLibraryImport)(nil)
2147
2148// Implements android.ApexModule
Paul Duffineedc5d52020-06-12 17:46:39 +01002149func (module *SdkLibraryImport) DepIsInSameApex(mctx android.BaseModuleContext, dep android.Module) bool {
2150 depTag := mctx.OtherModuleDependencyTag(dep)
2151 if depTag == xmlPermissionsFileTag {
2152 return true
2153 }
2154
2155 // None of the other dependencies of the java_sdk_library_import are in the same apex
2156 // as the one that references this module.
2157 return false
2158}
2159
Jiyong Park45bf82e2020-12-15 22:29:02 +09002160// Implements android.ApexModule
Dan Albertc8060532020-07-22 22:32:17 -07002161func (module *SdkLibraryImport) ShouldSupportSdkVersion(ctx android.BaseModuleContext,
2162 sdkVersion android.ApiLevel) error {
Jooyung Han749dc692020-04-15 11:03:39 +09002163 // we don't check prebuilt modules for sdk_version
2164 return nil
2165}
2166
Paul Duffin2f7ccd52021-06-24 13:25:57 +01002167// Implements android.ApexModule
2168func (module *SdkLibraryImport) UniqueApexVariations() bool {
2169 return module.uniqueApexVariations()
2170}
2171
Paul Duffineedc5d52020-06-12 17:46:39 +01002172func (module *SdkLibraryImport) OutputFiles(tag string) (android.Paths, error) {
Paul Duffin46dc45a2020-05-14 15:39:10 +01002173 return module.commonOutputFiles(tag)
2174}
2175
Paul Duffineedc5d52020-06-12 17:46:39 +01002176func (module *SdkLibraryImport) GenerateAndroidBuildActions(ctx android.ModuleContext) {
Paul Duffina2ae7e02020-09-11 11:55:00 +01002177 module.generateCommonBuildActions(ctx)
2178
Paul Duffin39853512021-02-26 11:09:39 +00002179 var deapexerModule android.Module
2180
Paul Duffin0f8faff2020-05-20 16:18:00 +01002181 // Record the paths to the prebuilt stubs library and stubs source.
Colin Cross79c7c262019-04-17 11:11:46 -07002182 ctx.VisitDirectDeps(func(to android.Module) {
2183 tag := ctx.OtherModuleDependencyTag(to)
2184
Paul Duffin0f8faff2020-05-20 16:18:00 +01002185 // Extract information from any of the scope specific dependencies.
2186 if scopeTag, ok := tag.(scopeDependencyTag); ok {
2187 apiScope := scopeTag.apiScope
2188 scopePaths := module.getScopePathsCreateIfNeeded(apiScope)
2189
2190 // Extract information from the dependency. The exact information extracted
2191 // is determined by the nature of the dependency which is determined by the tag.
2192 scopeTag.extractDepInfo(ctx, to, scopePaths)
Paul Duffineedc5d52020-06-12 17:46:39 +01002193 } else if tag == implLibraryTag {
2194 if implLibrary, ok := to.(*Library); ok {
2195 module.implLibraryModule = implLibrary
2196 } else {
2197 ctx.ModuleErrorf("implementation library must be of type *java.Library but was %T", to)
2198 }
2199 } else if tag == xmlPermissionsFileTag {
2200 if xmlPermissionsFileModule, ok := to.(*sdkLibraryXml); ok {
2201 module.xmlPermissionsFileModule = xmlPermissionsFileModule
2202 } else {
2203 ctx.ModuleErrorf("xml permissions file module must be of type *sdkLibraryXml but was %T", to)
2204 }
Colin Cross79c7c262019-04-17 11:11:46 -07002205 }
Paul Duffin39853512021-02-26 11:09:39 +00002206
2207 // Save away the `deapexer` module on which this depends, if any.
2208 if tag == android.DeapexerTag {
Martin Stjernholm95994062021-06-30 16:35:07 +01002209 if deapexerModule != nil {
2210 ctx.ModuleErrorf("Ambiguous duplicate deapexer module dependencies %q and %q",
2211 deapexerModule.Name(), to.Name())
2212 }
Paul Duffin39853512021-02-26 11:09:39 +00002213 deapexerModule = to
2214 }
Colin Cross79c7c262019-04-17 11:11:46 -07002215 })
Paul Duffin0f8faff2020-05-20 16:18:00 +01002216
2217 // Populate the scope paths with information from the properties.
2218 for apiScope, scopeProperties := range module.scopeProperties {
2219 if len(scopeProperties.Jars) == 0 {
2220 continue
2221 }
2222
2223 paths := module.getScopePathsCreateIfNeeded(apiScope)
Anton Hansson3adf3c52021-09-21 15:25:12 +01002224 paths.annotationsZip = android.OptionalPathForModuleSrc(ctx, scopeProperties.Annotations)
Paul Duffin0f8faff2020-05-20 16:18:00 +01002225 paths.currentApiFilePath = android.OptionalPathForModuleSrc(ctx, scopeProperties.Current_api)
2226 paths.removedApiFilePath = android.OptionalPathForModuleSrc(ctx, scopeProperties.Removed_api)
2227 }
Paul Duffin39853512021-02-26 11:09:39 +00002228
2229 if ctx.Device() {
2230 // If this is a variant created for a prebuilt_apex then use the dex implementation jar
2231 // obtained from the associated deapexer module.
2232 ai := ctx.Provider(android.ApexInfoProvider).(android.ApexInfo)
2233 if ai.ForPrebuiltApex {
2234 if deapexerModule == nil {
2235 // This should never happen as a variant for a prebuilt_apex is only created if the
2236 // deapxer module has been configured to export the dex implementation jar for this module.
2237 ctx.ModuleErrorf("internal error: module %q does not depend on a `deapexer` module for prebuilt_apex %q",
2238 module.Name(), ai.ApexVariationName)
2239 }
2240
2241 // Get the path of the dex implementation jar from the `deapexer` module.
2242 di := ctx.OtherModuleProvider(deapexerModule, android.DeapexerProvider).(android.DeapexerInfo)
Paul Duffin034196d2021-06-17 15:59:07 +01002243 if dexOutputPath := di.PrebuiltExportPath(apexRootRelativePathToJavaLib(module.BaseModuleName())); dexOutputPath != nil {
Paul Duffin39853512021-02-26 11:09:39 +00002244 module.dexJarFile = dexOutputPath
Paul Duffin6c6dde02021-05-14 15:52:25 +01002245 module.initHiddenAPI(ctx, dexOutputPath, module.findScopePaths(apiScopePublic).stubsImplPath[0], nil)
Paul Duffin39853512021-02-26 11:09:39 +00002246 } else {
2247 // This should never happen as a variant for a prebuilt_apex is only created if the
2248 // prebuilt_apex has been configured to export the java library dex file.
2249 ctx.ModuleErrorf("internal error: no dex implementation jar available from prebuilt_apex %q", deapexerModule.Name())
2250 }
2251 }
2252 }
Colin Cross79c7c262019-04-17 11:11:46 -07002253}
2254
Jiyong Parkf1691d22021-03-29 20:11:58 +09002255func (module *SdkLibraryImport) sdkJars(ctx android.BaseModuleContext, sdkVersion android.SdkSpec, headerJars bool) android.Paths {
Paul Duffineedc5d52020-06-12 17:46:39 +01002256
2257 // For consistency with SdkLibrary make the implementation jar available to libraries that
2258 // are within the same APEX.
2259 implLibraryModule := module.implLibraryModule
Colin Cross56a83212020-09-15 18:30:11 -07002260 if implLibraryModule != nil && withinSameApexesAs(ctx, module) {
Paul Duffineedc5d52020-06-12 17:46:39 +01002261 if headerJars {
2262 return implLibraryModule.HeaderJars()
2263 } else {
2264 return implLibraryModule.ImplementationJars()
2265 }
2266 }
2267
Paul Duffin23970f42020-05-20 14:20:02 +01002268 return module.selectHeaderJarsForSdkVersion(ctx, sdkVersion)
Paul Duffin56d44902020-01-31 13:36:25 +00002269}
2270
Colin Cross79c7c262019-04-17 11:11:46 -07002271// to satisfy SdkLibraryDependency interface
Jiyong Parkf1691d22021-03-29 20:11:58 +09002272func (module *SdkLibraryImport) SdkHeaderJars(ctx android.BaseModuleContext, sdkVersion android.SdkSpec) android.Paths {
Colin Cross79c7c262019-04-17 11:11:46 -07002273 // This module is just a wrapper for the prebuilt stubs.
Paul Duffineedc5d52020-06-12 17:46:39 +01002274 return module.sdkJars(ctx, sdkVersion, true)
Colin Cross79c7c262019-04-17 11:11:46 -07002275}
2276
2277// to satisfy SdkLibraryDependency interface
Jiyong Parkf1691d22021-03-29 20:11:58 +09002278func (module *SdkLibraryImport) SdkImplementationJars(ctx android.BaseModuleContext, sdkVersion android.SdkSpec) android.Paths {
Colin Cross79c7c262019-04-17 11:11:46 -07002279 // This module is just a wrapper for the stubs.
Paul Duffineedc5d52020-06-12 17:46:39 +01002280 return module.sdkJars(ctx, sdkVersion, false)
2281}
2282
Ulya Trafimovichdbf31662020-12-17 12:07:54 +00002283// to satisfy UsesLibraryDependency interface
Paul Duffineedc5d52020-06-12 17:46:39 +01002284func (module *SdkLibraryImport) DexJarBuildPath() android.Path {
Paul Duffin39853512021-02-26 11:09:39 +00002285 // The dex implementation jar extracted from the .apex file should be used in preference to the
2286 // source.
2287 if module.dexJarFile != nil {
2288 return module.dexJarFile
2289 }
Paul Duffineedc5d52020-06-12 17:46:39 +01002290 if module.implLibraryModule == nil {
2291 return nil
2292 } else {
2293 return module.implLibraryModule.DexJarBuildPath()
2294 }
2295}
2296
Ulya Trafimovichdbf31662020-12-17 12:07:54 +00002297// to satisfy UsesLibraryDependency interface
Ulya Trafimovich31e444e2020-08-14 17:32:16 +01002298func (module *SdkLibraryImport) DexJarInstallPath() android.Path {
2299 if module.implLibraryModule == nil {
2300 return nil
2301 } else {
2302 return module.implLibraryModule.DexJarInstallPath()
2303 }
2304}
2305
Ulya Trafimovichdbf31662020-12-17 12:07:54 +00002306// to satisfy UsesLibraryDependency interface
2307func (module *SdkLibraryImport) ClassLoaderContexts() dexpreopt.ClassLoaderContextMap {
2308 return nil
2309}
2310
Paul Duffineedc5d52020-06-12 17:46:39 +01002311// to satisfy apex.javaDependency interface
2312func (module *SdkLibraryImport) JacocoReportClassesFile() android.Path {
2313 if module.implLibraryModule == nil {
2314 return nil
2315 } else {
2316 return module.implLibraryModule.JacocoReportClassesFile()
2317 }
2318}
2319
2320// to satisfy apex.javaDependency interface
Colin Cross08dca382020-07-21 20:31:17 -07002321func (module *SdkLibraryImport) LintDepSets() LintDepSets {
2322 if module.implLibraryModule == nil {
2323 return LintDepSets{}
2324 } else {
2325 return module.implLibraryModule.LintDepSets()
2326 }
2327}
2328
Jaewoong Jung476b9d62021-05-10 15:30:00 -07002329func (module *SdkLibraryImport) getStrictUpdatabilityLinting() bool {
2330 if module.implLibraryModule == nil {
2331 return false
2332 } else {
2333 return module.implLibraryModule.getStrictUpdatabilityLinting()
2334 }
2335}
2336
2337func (module *SdkLibraryImport) setStrictUpdatabilityLinting(strictLinting bool) {
2338 if module.implLibraryModule != nil {
2339 module.implLibraryModule.setStrictUpdatabilityLinting(strictLinting)
2340 }
2341}
2342
Colin Cross08dca382020-07-21 20:31:17 -07002343// to satisfy apex.javaDependency interface
Paul Duffineedc5d52020-06-12 17:46:39 +01002344func (module *SdkLibraryImport) Stem() string {
2345 return module.BaseModuleName()
Colin Cross79c7c262019-04-17 11:11:46 -07002346}
Jiyong Parke3833882020-02-17 17:28:10 +09002347
Paul Duffin44b481b2020-06-17 16:59:43 +01002348var _ ApexDependency = (*SdkLibraryImport)(nil)
2349
2350// to satisfy java.ApexDependency interface
2351func (module *SdkLibraryImport) HeaderJars() android.Paths {
2352 if module.implLibraryModule == nil {
2353 return nil
2354 } else {
2355 return module.implLibraryModule.HeaderJars()
2356 }
2357}
2358
2359// to satisfy java.ApexDependency interface
2360func (module *SdkLibraryImport) ImplementationAndResourcesJars() android.Paths {
2361 if module.implLibraryModule == nil {
2362 return nil
2363 } else {
2364 return module.implLibraryModule.ImplementationAndResourcesJars()
2365 }
2366}
2367
Paul Duffin7db57e02021-06-17 14:56:05 +01002368var _ android.RequiredFilesFromPrebuiltApex = (*SdkLibraryImport)(nil)
2369
Paul Duffin034196d2021-06-17 15:59:07 +01002370func (module *SdkLibraryImport) RequiredFilesFromPrebuiltApex(ctx android.BaseModuleContext) []string {
Paul Duffin7db57e02021-06-17 14:56:05 +01002371 name := module.BaseModuleName()
2372 return requiredFilesFromPrebuiltApexForImport(name)
2373}
2374
Jiyong Parke3833882020-02-17 17:28:10 +09002375//
2376// java_sdk_library_xml
2377//
2378type sdkLibraryXml struct {
2379 android.ModuleBase
2380 android.DefaultableModuleBase
2381 android.ApexModuleBase
2382
2383 properties sdkLibraryXmlProperties
2384
2385 outputFilePath android.OutputPath
2386 installDirPath android.InstallPath
Colin Cross56a83212020-09-15 18:30:11 -07002387
2388 hideApexVariantFromMake bool
Jiyong Parke3833882020-02-17 17:28:10 +09002389}
2390
2391type sdkLibraryXmlProperties struct {
2392 // canonical name of the lib
2393 Lib_name *string
2394}
2395
2396// java_sdk_library_xml builds the permission xml file for a java_sdk_library.
2397// Not to be used directly by users. java_sdk_library internally uses this.
2398func sdkLibraryXmlFactory() android.Module {
2399 module := &sdkLibraryXml{}
2400
2401 module.AddProperties(&module.properties)
2402
2403 android.InitApexModule(module)
2404 android.InitAndroidArchModule(module, android.DeviceSupported, android.MultilibCommon)
2405
2406 return module
2407}
2408
Colin Crossaede88c2020-08-11 12:17:01 -07002409func (module *sdkLibraryXml) UniqueApexVariations() bool {
2410 // sdkLibraryXml needs a unique variation per APEX because the generated XML file contains the path to the
2411 // mounted APEX, which contains the name of the APEX.
2412 return true
2413}
2414
Jiyong Parke3833882020-02-17 17:28:10 +09002415// from android.PrebuiltEtcModule
Jooyung Han0703fd82020-08-26 22:11:53 +09002416func (module *sdkLibraryXml) BaseDir() string {
2417 return "etc"
2418}
2419
2420// from android.PrebuiltEtcModule
Jiyong Parke3833882020-02-17 17:28:10 +09002421func (module *sdkLibraryXml) SubDir() string {
2422 return "permissions"
2423}
2424
2425// from android.PrebuiltEtcModule
2426func (module *sdkLibraryXml) OutputFile() android.OutputPath {
2427 return module.outputFilePath
2428}
2429
2430// from android.ApexModule
2431func (module *sdkLibraryXml) AvailableFor(what string) bool {
2432 return true
2433}
2434
2435func (module *sdkLibraryXml) DepsMutator(ctx android.BottomUpMutatorContext) {
2436 // do nothing
2437}
2438
Jiyong Park45bf82e2020-12-15 22:29:02 +09002439var _ android.ApexModule = (*sdkLibraryXml)(nil)
2440
2441// Implements android.ApexModule
Dan Albertc8060532020-07-22 22:32:17 -07002442func (module *sdkLibraryXml) ShouldSupportSdkVersion(ctx android.BaseModuleContext,
2443 sdkVersion android.ApiLevel) error {
Jooyung Han749dc692020-04-15 11:03:39 +09002444 // sdkLibraryXml doesn't need to be checked separately because java_sdk_library is checked
2445 return nil
2446}
2447
Jiyong Parke3833882020-02-17 17:28:10 +09002448// File path to the runtime implementation library
Colin Cross56a83212020-09-15 18:30:11 -07002449func (module *sdkLibraryXml) implPath(ctx android.ModuleContext) string {
Jiyong Parke3833882020-02-17 17:28:10 +09002450 implName := proptools.String(module.properties.Lib_name)
Colin Cross56a83212020-09-15 18:30:11 -07002451 if apexInfo := ctx.Provider(android.ApexInfoProvider).(android.ApexInfo); !apexInfo.IsForPlatform() {
Colin Crosse07f2312020-08-13 11:24:56 -07002452 // TODO(b/146468504): ApexVariationName() is only a soong module name, not apex name.
Jiyong Parke3833882020-02-17 17:28:10 +09002453 // In most cases, this works fine. But when apex_name is set or override_apex is used
2454 // this can be wrong.
Colin Cross56a83212020-09-15 18:30:11 -07002455 return fmt.Sprintf("/apex/%s/javalib/%s.jar", apexInfo.ApexVariationName, implName)
Jiyong Parke3833882020-02-17 17:28:10 +09002456 }
2457 partition := "system"
2458 if module.SocSpecific() {
2459 partition = "vendor"
2460 } else if module.DeviceSpecific() {
2461 partition = "odm"
2462 } else if module.ProductSpecific() {
2463 partition = "product"
2464 } else if module.SystemExtSpecific() {
2465 partition = "system_ext"
2466 }
2467 return "/" + partition + "/framework/" + implName + ".jar"
2468}
2469
2470func (module *sdkLibraryXml) GenerateAndroidBuildActions(ctx android.ModuleContext) {
Colin Cross56a83212020-09-15 18:30:11 -07002471 module.hideApexVariantFromMake = !ctx.Provider(android.ApexInfoProvider).(android.ApexInfo).IsForPlatform()
2472
Jiyong Parke3833882020-02-17 17:28:10 +09002473 libName := proptools.String(module.properties.Lib_name)
Colin Cross56a83212020-09-15 18:30:11 -07002474 xmlContent := fmt.Sprintf(permissionsTemplate, libName, module.implPath(ctx))
Jiyong Parke3833882020-02-17 17:28:10 +09002475
2476 module.outputFilePath = android.PathForModuleOut(ctx, libName+".xml").OutputPath
Colin Crossf1a035e2020-11-16 17:32:30 -08002477 rule := android.NewRuleBuilder(pctx, ctx)
Jiyong Parke3833882020-02-17 17:28:10 +09002478 rule.Command().
2479 Text("/bin/bash -c \"echo -e '" + xmlContent + "'\" > ").
2480 Output(module.outputFilePath)
2481
Colin Crossf1a035e2020-11-16 17:32:30 -08002482 rule.Build("java_sdk_xml", "Permission XML")
Jiyong Parke3833882020-02-17 17:28:10 +09002483
2484 module.installDirPath = android.PathForModuleInstall(ctx, "etc", module.SubDir())
2485}
2486
2487func (module *sdkLibraryXml) AndroidMkEntries() []android.AndroidMkEntries {
Colin Cross56a83212020-09-15 18:30:11 -07002488 if module.hideApexVariantFromMake {
satayev531330e2021-12-06 11:40:46 +00002489 return []android.AndroidMkEntries{{
Jiyong Parke3833882020-02-17 17:28:10 +09002490 Disabled: true,
2491 }}
2492 }
2493
satayev531330e2021-12-06 11:40:46 +00002494 return []android.AndroidMkEntries{{
Jiyong Parke3833882020-02-17 17:28:10 +09002495 Class: "ETC",
2496 OutputFile: android.OptionalPathForPath(module.outputFilePath),
2497 ExtraEntries: []android.AndroidMkExtraEntriesFunc{
Colin Crossaa255532020-07-03 13:18:24 -07002498 func(ctx android.AndroidMkExtraEntriesContext, entries *android.AndroidMkEntries) {
Jiyong Parke3833882020-02-17 17:28:10 +09002499 entries.SetString("LOCAL_MODULE_TAGS", "optional")
2500 entries.SetString("LOCAL_MODULE_PATH", module.installDirPath.ToMakePath().String())
2501 entries.SetString("LOCAL_INSTALLED_MODULE_STEM", module.outputFilePath.Base())
2502 },
2503 },
2504 }}
2505}
Paul Duffindd46f712020-02-10 13:37:10 +00002506
2507type sdkLibrarySdkMemberType struct {
2508 android.SdkMemberTypeBase
2509}
2510
2511func (s *sdkLibrarySdkMemberType) AddDependencies(mctx android.BottomUpMutatorContext, dependencyTag blueprint.DependencyTag, names []string) {
2512 mctx.AddVariationDependencies(nil, dependencyTag, names...)
2513}
2514
2515func (s *sdkLibrarySdkMemberType) IsInstance(module android.Module) bool {
2516 _, ok := module.(*SdkLibrary)
2517 return ok
2518}
2519
2520func (s *sdkLibrarySdkMemberType) AddPrebuiltModule(ctx android.SdkMemberContext, member android.SdkMember) android.BpModule {
2521 return ctx.SnapshotBuilder().AddPrebuiltModule(member, "java_sdk_library_import")
2522}
2523
2524func (s *sdkLibrarySdkMemberType) CreateVariantPropertiesStruct() android.SdkMemberProperties {
2525 return &sdkLibrarySdkMemberProperties{}
2526}
2527
Paul Duffin976b0e52021-04-27 23:20:26 +01002528var javaSdkLibrarySdkMemberType = &sdkLibrarySdkMemberType{
2529 android.SdkMemberTypeBase{
2530 PropertyName: "java_sdk_libs",
2531 SupportsSdk: true,
2532 },
2533}
2534
Paul Duffindd46f712020-02-10 13:37:10 +00002535type sdkLibrarySdkMemberProperties struct {
2536 android.SdkMemberPropertiesBase
2537
2538 // Scope to per scope properties.
2539 Scopes map[*apiScope]scopeProperties
2540
Paul Duffin3d1248c2020-04-09 00:10:17 +01002541 // The Java stubs source files.
2542 Stub_srcs []string
Paul Duffinf7a64332020-05-13 16:54:55 +01002543
2544 // The naming scheme.
2545 Naming_scheme *string
Paul Duffind7eb1c22020-05-26 20:57:10 +01002546
2547 // True if the java_sdk_library_import is for a shared library, false
2548 // otherwise.
2549 Shared_library *bool
Paul Duffina2ae7e02020-09-11 11:55:00 +01002550
Paul Duffin1267d872021-04-16 17:21:36 +01002551 // True if the stub imports should produce dex jars.
2552 Compile_dex *bool
2553
Paul Duffina2ae7e02020-09-11 11:55:00 +01002554 // The paths to the doctag files to add to the prebuilt.
2555 Doctag_paths android.Paths
Paul Duffin7b3e10a2021-07-15 14:14:41 +01002556
2557 Permitted_packages []string
Paul Duffindd46f712020-02-10 13:37:10 +00002558}
2559
2560type scopeProperties struct {
Paul Duffin1fd005d2020-04-09 01:08:11 +01002561 Jars android.Paths
2562 StubsSrcJar android.Path
2563 CurrentApiFile android.Path
2564 RemovedApiFile android.Path
Anton Hansson3adf3c52021-09-21 15:25:12 +01002565 AnnotationsZip android.Path
Paul Duffin1fd005d2020-04-09 01:08:11 +01002566 SdkVersion string
Paul Duffindd46f712020-02-10 13:37:10 +00002567}
2568
2569func (s *sdkLibrarySdkMemberProperties) PopulateFromVariant(ctx android.SdkMemberContext, variant android.Module) {
2570 sdk := variant.(*SdkLibrary)
2571
2572 s.Scopes = make(map[*apiScope]scopeProperties)
2573 for _, apiScope := range allApiScopes {
Paul Duffin803a9562020-05-20 11:52:25 +01002574 paths := sdk.findScopePaths(apiScope)
2575 if paths == nil {
2576 continue
2577 }
2578
Paul Duffindd46f712020-02-10 13:37:10 +00002579 jars := paths.stubsImplPath
2580 if len(jars) > 0 {
2581 properties := scopeProperties{}
2582 properties.Jars = jars
Paul Duffin780c5f42020-05-12 15:52:55 +01002583 properties.SdkVersion = sdk.sdkVersionForStubsLibrary(ctx.SdkModuleContext(), apiScope)
Paul Duffin0f8faff2020-05-20 16:18:00 +01002584 properties.StubsSrcJar = paths.stubsSrcJar.Path()
Paul Duffin10269f12020-06-19 18:39:55 +01002585 if paths.currentApiFilePath.Valid() {
2586 properties.CurrentApiFile = paths.currentApiFilePath.Path()
2587 }
2588 if paths.removedApiFilePath.Valid() {
2589 properties.RemovedApiFile = paths.removedApiFilePath.Path()
2590 }
Anton Hansson3adf3c52021-09-21 15:25:12 +01002591 // The annotations zip is only available for modules that set annotations_enabled: true.
2592 if paths.annotationsZip.Valid() {
2593 properties.AnnotationsZip = paths.annotationsZip.Path()
2594 }
Paul Duffindd46f712020-02-10 13:37:10 +00002595 s.Scopes[apiScope] = properties
2596 }
2597 }
2598
Paul Duffindfa131e2020-05-15 20:37:11 +01002599 s.Naming_scheme = sdk.commonSdkLibraryProperties.Naming_scheme
Paul Duffind7eb1c22020-05-26 20:57:10 +01002600 s.Shared_library = proptools.BoolPtr(sdk.sharedLibrary())
Paul Duffin1267d872021-04-16 17:21:36 +01002601 s.Compile_dex = sdk.dexProperties.Compile_dex
Paul Duffina2ae7e02020-09-11 11:55:00 +01002602 s.Doctag_paths = sdk.doctagPaths
Paul Duffin7b3e10a2021-07-15 14:14:41 +01002603 s.Permitted_packages = sdk.PermittedPackagesForUpdatableBootJars()
Paul Duffindd46f712020-02-10 13:37:10 +00002604}
2605
2606func (s *sdkLibrarySdkMemberProperties) AddToPropertySet(ctx android.SdkMemberContext, propertySet android.BpPropertySet) {
Paul Duffinf7a64332020-05-13 16:54:55 +01002607 if s.Naming_scheme != nil {
2608 propertySet.AddProperty("naming_scheme", proptools.String(s.Naming_scheme))
2609 }
Paul Duffind7eb1c22020-05-26 20:57:10 +01002610 if s.Shared_library != nil {
2611 propertySet.AddProperty("shared_library", *s.Shared_library)
2612 }
Paul Duffin1267d872021-04-16 17:21:36 +01002613 if s.Compile_dex != nil {
2614 propertySet.AddProperty("compile_dex", *s.Compile_dex)
2615 }
Paul Duffin7b3e10a2021-07-15 14:14:41 +01002616 if len(s.Permitted_packages) > 0 {
2617 propertySet.AddProperty("permitted_packages", s.Permitted_packages)
2618 }
Paul Duffinf7a64332020-05-13 16:54:55 +01002619
Paul Duffindd46f712020-02-10 13:37:10 +00002620 for _, apiScope := range allApiScopes {
2621 if properties, ok := s.Scopes[apiScope]; ok {
Paul Duffin6b836ba2020-05-13 19:19:49 +01002622 scopeSet := propertySet.AddPropertySet(apiScope.propertyName)
Paul Duffindd46f712020-02-10 13:37:10 +00002623
Paul Duffin3d1248c2020-04-09 00:10:17 +01002624 scopeDir := filepath.Join("sdk_library", s.OsPrefix(), apiScope.name)
2625
Paul Duffindd46f712020-02-10 13:37:10 +00002626 var jars []string
2627 for _, p := range properties.Jars {
Paul Duffin3d1248c2020-04-09 00:10:17 +01002628 dest := filepath.Join(scopeDir, ctx.Name()+"-stubs.jar")
Paul Duffindd46f712020-02-10 13:37:10 +00002629 ctx.SnapshotBuilder().CopyToSnapshot(p, dest)
2630 jars = append(jars, dest)
2631 }
2632 scopeSet.AddProperty("jars", jars)
2633
Paul Duffin22628d52021-05-12 23:13:22 +01002634 if ctx.SdkModuleContext().Config().IsEnvTrue("SOONG_SDK_SNAPSHOT_USE_SRCJAR") {
2635 // Copy the stubs source jar into the snapshot zip as is.
2636 srcJarSnapshotPath := filepath.Join(scopeDir, ctx.Name()+".srcjar")
2637 ctx.SnapshotBuilder().CopyToSnapshot(properties.StubsSrcJar, srcJarSnapshotPath)
2638 scopeSet.AddProperty("stub_srcs", []string{srcJarSnapshotPath})
2639 } else {
2640 // Merge the stubs source jar into the snapshot zip so that when it is unpacked
2641 // the source files are also unpacked.
2642 snapshotRelativeDir := filepath.Join(scopeDir, ctx.Name()+"_stub_sources")
2643 ctx.SnapshotBuilder().UnzipToSnapshot(properties.StubsSrcJar, snapshotRelativeDir)
2644 scopeSet.AddProperty("stub_srcs", []string{snapshotRelativeDir})
2645 }
Paul Duffin3d1248c2020-04-09 00:10:17 +01002646
Paul Duffin1fd005d2020-04-09 01:08:11 +01002647 if properties.CurrentApiFile != nil {
2648 currentApiSnapshotPath := filepath.Join(scopeDir, ctx.Name()+".txt")
2649 ctx.SnapshotBuilder().CopyToSnapshot(properties.CurrentApiFile, currentApiSnapshotPath)
2650 scopeSet.AddProperty("current_api", currentApiSnapshotPath)
2651 }
2652
2653 if properties.RemovedApiFile != nil {
2654 removedApiSnapshotPath := filepath.Join(scopeDir, ctx.Name()+"-removed.txt")
Paul Duffin3dbf9fd2020-06-02 13:00:02 +01002655 ctx.SnapshotBuilder().CopyToSnapshot(properties.RemovedApiFile, removedApiSnapshotPath)
Paul Duffin1fd005d2020-04-09 01:08:11 +01002656 scopeSet.AddProperty("removed_api", removedApiSnapshotPath)
2657 }
2658
Anton Hansson3adf3c52021-09-21 15:25:12 +01002659 if properties.AnnotationsZip != nil {
2660 annotationsSnapshotPath := filepath.Join(scopeDir, ctx.Name()+"_annotations.zip")
2661 ctx.SnapshotBuilder().CopyToSnapshot(properties.AnnotationsZip, annotationsSnapshotPath)
2662 scopeSet.AddProperty("annotations", annotationsSnapshotPath)
2663 }
2664
Paul Duffindd46f712020-02-10 13:37:10 +00002665 if properties.SdkVersion != "" {
2666 scopeSet.AddProperty("sdk_version", properties.SdkVersion)
2667 }
2668 }
2669 }
2670
Paul Duffina2ae7e02020-09-11 11:55:00 +01002671 if len(s.Doctag_paths) > 0 {
2672 dests := []string{}
2673 for _, p := range s.Doctag_paths {
2674 dest := filepath.Join("doctags", p.Rel())
2675 ctx.SnapshotBuilder().CopyToSnapshot(p, dest)
2676 dests = append(dests, dest)
2677 }
2678 propertySet.AddProperty("doctag_files", dests)
2679 }
Paul Duffindd46f712020-02-10 13:37:10 +00002680}