blob: 567e2921900d647ae2cb37699742efaefe55f859 [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 {
Paul Duffin5df79302020-05-16 15:52:12 +0100379 // Visibility for impl library module. If not specified then defaults to the
380 // visibility property.
381 Impl_library_visibility []string
382
Paul Duffin4911a892020-04-29 23:35:13 +0100383 // Visibility for stubs library modules. If not specified then defaults to the
384 // visibility property.
385 Stubs_library_visibility []string
386
387 // Visibility for stubs source modules. If not specified then defaults to the
388 // visibility property.
389 Stubs_source_visibility []string
390
Anton Hansson7f66efa2020-10-08 14:47:23 +0100391 // List of Java libraries that will be in the classpath when building the implementation lib
392 Impl_only_libs []string `android:"arch_variant"`
393
Sundong Ahnf043cf62018-06-25 16:04:37 +0900394 // List of Java libraries that will be in the classpath when building stubs
395 Stub_only_libs []string `android:"arch_variant"`
396
Anton Hanssondae54cd2021-04-21 16:30:10 +0100397 // List of Java libraries that will included in stub libraries
398 Stub_only_static_libs []string `android:"arch_variant"`
399
Paul Duffin7a586d32019-12-30 17:09:34 +0000400 // list of package names that will be documented and publicized as API.
401 // This allows the API to be restricted to a subset of the source files provided.
402 // If this is unspecified then all the source files will be treated as being part
403 // of the API.
Jiyong Parkc678ad32018-04-10 13:07:10 +0900404 Api_packages []string
405
Jiyong Park5a2c9d72018-05-01 22:25:41 +0900406 // list of package names that must be hidden from the API
407 Hidden_api_packages []string
408
Paul Duffin749f98f2019-12-30 17:23:46 +0000409 // the relative path to the directory containing the api specification files.
410 // Defaults to "api".
411 Api_dir *string
412
Paul Duffindfa131e2020-05-15 20:37:11 +0100413 // Determines whether a runtime implementation library is built; defaults to false.
414 //
415 // If true then it also prevents the module from being used as a shared module, i.e.
416 // it is as is shared_library: false, was set.
Paul Duffin43db9be2019-12-30 17:35:49 +0000417 Api_only *bool
418
Paul Duffin11512472019-02-11 15:55:17 +0000419 // local files that are used within user customized droiddoc options.
420 Droiddoc_option_files []string
421
422 // additional droiddoc options
423 // Available variables for substitution:
424 //
425 // $(location <label>): the path to the droiddoc_option_files with name <label>
Sundong Ahndd567f92018-07-31 17:19:11 +0900426 Droiddoc_options []string
427
Paul Duffine22c2ab2020-05-20 19:35:27 +0100428 // is set to true, Metalava will allow framework SDK to contain annotations.
429 Annotations_enabled *bool
430
Sundong Ahn054b19a2018-10-19 13:46:09 +0900431 // a list of top-level directories containing files to merge qualifier annotations
432 // (i.e. those intended to be included in the stubs written) from.
433 Merge_annotations_dirs []string
434
435 // a list of top-level directories containing Java stub files to merge show/hide annotations from.
436 Merge_inclusion_annotations_dirs []string
437
Paul Duffin4f5c1ef2020-11-19 14:53:43 +0000438 // If set to true then don't create dist rules.
439 No_dist *bool
Sundong Ahn80a87b32019-05-13 15:02:50 +0900440
Paul Duffin31310252020-11-20 21:26:20 +0000441 // The stem for the artifacts that are copied to the dist, if not specified
442 // then defaults to the base module name.
443 //
444 // For each scope the following artifacts are copied to the apistubs/<scope>
445 // directory in the dist.
446 // * stubs impl jar -> <dist-stem>.jar
447 // * API specification file -> api/<dist-stem>.txt
448 // * Removed API specification file -> api/<dist-stem>-removed.txt
449 //
450 // Also used to construct the name of the filegroup (created by prebuilt_apis)
451 // that references the latest released API and remove API specification files.
452 // * API specification filegroup -> <dist-stem>.api.<scope>.latest
453 // * Removed API specification filegroup -> <dist-stem>-removed.api.<scope>.latest
Jaewoong Jung1a97ee02021-03-09 13:25:02 -0800454 // * API incompatibilities baseline filegroup -> <dist-stem>-incompatibilities.api.<scope>.latest
Paul Duffin31310252020-11-20 21:26:20 +0000455 Dist_stem *string
456
Colin Cross986b69a2021-06-01 13:13:40 -0700457 // The subdirectory for the artifacts that are copied to the dist directory. If not specified
Colin Cross3dd66252021-06-01 14:05:09 -0700458 // then defaults to "unknown". Should be set to "android" for anything that should be published
Colin Cross986b69a2021-06-01 13:13:40 -0700459 // in the public Android SDK.
460 Dist_group *string
461
Anton Hanssondff2c782020-12-21 17:10:01 +0000462 // A compatibility mode that allows historical API-tracking files to not exist.
463 // Do not use.
464 Unsafe_ignore_missing_latest_api bool
465
Paul Duffin3375e352020-04-28 10:44:03 +0100466 // indicates whether system and test apis should be generated.
467 Generate_system_and_test_apis bool `blueprint:"mutated"`
468
469 // The properties specific to the public api scope
470 //
471 // Unless explicitly specified by using public.enabled the public api scope is
472 // enabled by default in both legacy and non-legacy mode.
473 Public ApiScopeProperties
474
475 // The properties specific to the system api scope
476 //
477 // In legacy mode the system api scope is enabled by default when sdk_version
478 // is set to something other than "none".
479 //
480 // In non-legacy mode the system api scope is disabled by default.
481 System ApiScopeProperties
482
483 // The properties specific to the test api scope
484 //
485 // In legacy mode the test api scope is enabled by default when sdk_version
486 // is set to something other than "none".
487 //
488 // In non-legacy mode the test api scope is disabled by default.
489 Test ApiScopeProperties
Paul Duffin37e0b772019-12-30 17:20:10 +0000490
Paul Duffin0c5bae52020-06-02 13:00:08 +0100491 // The properties specific to the module-lib api scope
Paul Duffin8f265b92020-04-28 14:13:56 +0100492 //
Paul Duffin0c5bae52020-06-02 13:00:08 +0100493 // Unless explicitly specified by using test.enabled the module-lib api scope is
Paul Duffin8f265b92020-04-28 14:13:56 +0100494 // disabled by default.
495 Module_lib ApiScopeProperties
496
Paul Duffin0c5bae52020-06-02 13:00:08 +0100497 // The properties specific to the system-server api scope
498 //
499 // Unless explicitly specified by using test.enabled the module-lib api scope is
500 // disabled by default.
501 System_server ApiScopeProperties
502
Jiyong Park932cdfe2020-05-28 00:19:53 +0900503 // Determines if the stubs are preferred over the implementation library
504 // for linking, even when the client doesn't specify sdk_version. When this
505 // is set to true, such clients are provided with the widest API surface that
506 // this lib provides. Note however that this option doesn't affect the clients
507 // that are in the same APEX as this library. In that case, the clients are
508 // always linked with the implementation library. Default is false.
509 Default_to_stubs *bool
510
Paul Duffin160fe412020-05-10 19:32:20 +0100511 // Properties related to api linting.
512 Api_lint struct {
513 // Enable api linting.
514 Enabled *bool
515 }
516
Jiyong Parkc678ad32018-04-10 13:07:10 +0900517 // TODO: determines whether to create HTML doc or not
518 //Html_doc *bool
519}
520
Paul Duffin0f8faff2020-05-20 16:18:00 +0100521// Paths to outputs from java_sdk_library and java_sdk_library_import.
522//
523// Fields that are android.Paths are always set (during GenerateAndroidBuildActions).
524// OptionalPaths are always set by java_sdk_library but may not be set by
525// java_sdk_library_import as not all instances provide that information.
Paul Duffind1b3a922020-01-22 11:57:20 +0000526type scopePaths struct {
Paul Duffin0f8faff2020-05-20 16:18:00 +0100527 // The path (represented as Paths for convenience when returning) to the stubs header jar.
528 //
529 // That is the jar that is created by turbine.
530 stubsHeaderPath android.Paths
531
532 // The path (represented as Paths for convenience when returning) to the stubs implementation jar.
533 //
534 // This is not the implementation jar, it still only contains stubs.
535 stubsImplPath android.Paths
536
Paul Duffin1267d872021-04-16 17:21:36 +0100537 // The dex jar for the stubs.
538 //
539 // This is not the implementation jar, it still only contains stubs.
540 stubsDexJarPath android.Path
541
Paul Duffin0f8faff2020-05-20 16:18:00 +0100542 // The API specification file, e.g. system_current.txt.
543 currentApiFilePath android.OptionalPath
544
545 // The specification of API elements removed since the last release.
546 removedApiFilePath android.OptionalPath
547
548 // The stubs source jar.
549 stubsSrcJar android.OptionalPath
Paul Duffind1b3a922020-01-22 11:57:20 +0000550}
551
Colin Crossdcf71b22021-02-01 13:59:03 -0800552func (paths *scopePaths) extractStubsLibraryInfoFromDependency(ctx android.ModuleContext, dep android.Module) error {
553 if ctx.OtherModuleHasProvider(dep, JavaInfoProvider) {
554 lib := ctx.OtherModuleProvider(dep, JavaInfoProvider).(JavaInfo)
555 paths.stubsHeaderPath = lib.HeaderJars
556 paths.stubsImplPath = lib.ImplementationJars
Paul Duffin1267d872021-04-16 17:21:36 +0100557
558 libDep := dep.(UsesLibraryDependency)
559 paths.stubsDexJarPath = libDep.DexJarBuildPath()
Paul Duffinc8782502020-04-29 20:45:27 +0100560 return nil
561 } else {
Colin Crossdcf71b22021-02-01 13:59:03 -0800562 return fmt.Errorf("expected module that has JavaInfoProvider, e.g. java_library")
Paul Duffinc8782502020-04-29 20:45:27 +0100563 }
564}
565
Paul Duffin0ff08bd2020-04-29 13:30:54 +0100566func (paths *scopePaths) treatDepAsApiStubsProvider(dep android.Module, action func(provider ApiStubsProvider)) error {
567 if apiStubsProvider, ok := dep.(ApiStubsProvider); ok {
568 action(apiStubsProvider)
Paul Duffinc8782502020-04-29 20:45:27 +0100569 return nil
570 } else {
571 return fmt.Errorf("expected module that implements ApiStubsProvider, e.g. droidstubs")
572 }
573}
574
Paul Duffin0f8faff2020-05-20 16:18:00 +0100575func (paths *scopePaths) treatDepAsApiStubsSrcProvider(dep android.Module, action func(provider ApiStubsSrcProvider)) error {
576 if apiStubsProvider, ok := dep.(ApiStubsSrcProvider); ok {
577 action(apiStubsProvider)
578 return nil
579 } else {
580 return fmt.Errorf("expected module that implements ApiStubsSrcProvider, e.g. droidstubs")
581 }
582}
583
Paul Duffin0ff08bd2020-04-29 13:30:54 +0100584func (paths *scopePaths) extractApiInfoFromApiStubsProvider(provider ApiStubsProvider) {
Paul Duffin0f8faff2020-05-20 16:18:00 +0100585 paths.currentApiFilePath = android.OptionalPathForPath(provider.ApiFilePath())
586 paths.removedApiFilePath = android.OptionalPathForPath(provider.RemovedApiFilePath())
Paul Duffin0ff08bd2020-04-29 13:30:54 +0100587}
588
Colin Crossdcf71b22021-02-01 13:59:03 -0800589func (paths *scopePaths) extractApiInfoFromDep(ctx android.ModuleContext, dep android.Module) error {
Paul Duffin0ff08bd2020-04-29 13:30:54 +0100590 return paths.treatDepAsApiStubsProvider(dep, func(provider ApiStubsProvider) {
591 paths.extractApiInfoFromApiStubsProvider(provider)
592 })
593}
594
Paul Duffin0f8faff2020-05-20 16:18:00 +0100595func (paths *scopePaths) extractStubsSourceInfoFromApiStubsProviders(provider ApiStubsSrcProvider) {
596 paths.stubsSrcJar = android.OptionalPathForPath(provider.StubsSrcJar())
Paul Duffin0ff08bd2020-04-29 13:30:54 +0100597}
598
Colin Crossdcf71b22021-02-01 13:59:03 -0800599func (paths *scopePaths) extractStubsSourceInfoFromDep(ctx android.ModuleContext, dep android.Module) error {
Paul Duffin0f8faff2020-05-20 16:18:00 +0100600 return paths.treatDepAsApiStubsSrcProvider(dep, func(provider ApiStubsSrcProvider) {
Paul Duffin0ff08bd2020-04-29 13:30:54 +0100601 paths.extractStubsSourceInfoFromApiStubsProviders(provider)
602 })
603}
604
Colin Crossdcf71b22021-02-01 13:59:03 -0800605func (paths *scopePaths) extractStubsSourceAndApiInfoFromApiStubsProvider(ctx android.ModuleContext, dep android.Module) error {
Paul Duffin0ff08bd2020-04-29 13:30:54 +0100606 return paths.treatDepAsApiStubsProvider(dep, func(provider ApiStubsProvider) {
607 paths.extractApiInfoFromApiStubsProvider(provider)
608 paths.extractStubsSourceInfoFromApiStubsProviders(provider)
609 })
610}
611
612type commonToSdkLibraryAndImportProperties struct {
Paul Duffin1b1e8062020-05-08 13:44:43 +0100613 // The naming scheme to use for the components that this module creates.
614 //
Paul Duffinee9ad5d2020-09-11 13:04:05 +0100615 // If not specified then it defaults to "default".
Paul Duffin1b1e8062020-05-08 13:44:43 +0100616 //
617 // This is a temporary mechanism to simplify conversion from separate modules for each
618 // component that follow a different naming pattern to the default one.
619 //
620 // TODO(b/155480189) - Remove once naming inconsistencies have been resolved.
Paul Duffin0ff08bd2020-04-29 13:30:54 +0100621 Naming_scheme *string
Paul Duffindfa131e2020-05-15 20:37:11 +0100622
623 // Specifies whether this module can be used as an Android shared library; defaults
624 // to true.
625 //
626 // An Android shared library is one that can be referenced in a <uses-library> element
627 // in an AndroidManifest.xml.
628 Shared_library *bool
Paul Duffina2ae7e02020-09-11 11:55:00 +0100629
630 // Files containing information about supported java doc tags.
631 Doctag_files []string `android:"path"`
Paul Duffin0ff08bd2020-04-29 13:30:54 +0100632}
633
Paul Duffin71b33cc2021-06-23 11:39:47 +0100634// commonSdkLibraryAndImportModule defines the interface that must be provided by a module that
635// embeds the commonToSdkLibraryAndImport struct.
636type commonSdkLibraryAndImportModule interface {
Paul Duffinb97b1572021-04-29 21:50:40 +0100637 android.SdkAware
Paul Duffin71b33cc2021-06-23 11:39:47 +0100638
639 BaseModuleName() string
640}
641
Paul Duffin56d44902020-01-31 13:36:25 +0000642// Common code between sdk library and sdk library import
643type commonToSdkLibraryAndImport struct {
Paul Duffin71b33cc2021-06-23 11:39:47 +0100644 module commonSdkLibraryAndImportModule
Paul Duffinc3091c82020-05-08 14:16:20 +0100645
Paul Duffin56d44902020-01-31 13:36:25 +0000646 scopePaths map[*apiScope]*scopePaths
Paul Duffin1b1e8062020-05-08 13:44:43 +0100647
648 namingScheme sdkLibraryComponentNamingScheme
649
Paul Duffindfa131e2020-05-15 20:37:11 +0100650 commonSdkLibraryProperties commonToSdkLibraryAndImportProperties
Paul Duffin859fe962020-05-15 10:20:31 +0100651
Paul Duffina2ae7e02020-09-11 11:55:00 +0100652 // Paths to commonSdkLibraryProperties.Doctag_files
653 doctagPaths android.Paths
654
Paul Duffin859fe962020-05-15 10:20:31 +0100655 // Functionality related to this being used as a component of a java_sdk_library.
656 EmbeddableSdkLibraryComponent
Paul Duffin56d44902020-01-31 13:36:25 +0000657}
658
Paul Duffin71b33cc2021-06-23 11:39:47 +0100659func (c *commonToSdkLibraryAndImport) initCommon(module commonSdkLibraryAndImportModule) {
660 c.module = module
Paul Duffin1b1e8062020-05-08 13:44:43 +0100661
Paul Duffin71b33cc2021-06-23 11:39:47 +0100662 module.AddProperties(&c.commonSdkLibraryProperties)
Paul Duffin859fe962020-05-15 10:20:31 +0100663
664 // Initialize this as an sdk library component.
Paul Duffin71b33cc2021-06-23 11:39:47 +0100665 c.initSdkLibraryComponent(module)
Paul Duffin1b1e8062020-05-08 13:44:43 +0100666}
667
668func (c *commonToSdkLibraryAndImport) initCommonAfterDefaultsApplied(ctx android.DefaultableHookContext) bool {
Paul Duffindfa131e2020-05-15 20:37:11 +0100669 schemeProperty := proptools.StringDefault(c.commonSdkLibraryProperties.Naming_scheme, "default")
Paul Duffin1b1e8062020-05-08 13:44:43 +0100670 switch schemeProperty {
671 case "default":
672 c.namingScheme = &defaultNamingScheme{}
673 default:
674 ctx.PropertyErrorf("naming_scheme", "expected 'default' but was %q", schemeProperty)
675 return false
676 }
677
Paul Duffindfa131e2020-05-15 20:37:11 +0100678 // Only track this sdk library if this can be used as a shared library.
679 if c.sharedLibrary() {
680 // Use the name specified in the module definition as the owner.
Paul Duffin71b33cc2021-06-23 11:39:47 +0100681 c.sdkLibraryComponentProperties.SdkLibraryToImplicitlyTrack = proptools.StringPtr(c.module.BaseModuleName())
Paul Duffindfa131e2020-05-15 20:37:11 +0100682 }
Paul Duffin859fe962020-05-15 10:20:31 +0100683
Paul Duffin1b1e8062020-05-08 13:44:43 +0100684 return true
Paul Duffinc3091c82020-05-08 14:16:20 +0100685}
686
Paul Duffinea8f8082021-06-24 13:25:57 +0100687// uniqueApexVariations provides common implementation of the ApexModule.UniqueApexVariations
688// method.
689func (c *commonToSdkLibraryAndImport) uniqueApexVariations() bool {
690 // A java_sdk_library that is a shared library produces an XML file that makes the shared library
691 // usable from an AndroidManifest.xml's <uses-library> entry. That XML file contains the name of
692 // the APEX and so it needs a unique variation per APEX.
693 return c.sharedLibrary()
694}
695
Paul Duffina2ae7e02020-09-11 11:55:00 +0100696func (c *commonToSdkLibraryAndImport) generateCommonBuildActions(ctx android.ModuleContext) {
697 c.doctagPaths = android.PathsForModuleSrc(ctx, c.commonSdkLibraryProperties.Doctag_files)
698}
699
Paul Duffineedc5d52020-06-12 17:46:39 +0100700// Module name of the runtime implementation library
701func (c *commonToSdkLibraryAndImport) implLibraryModuleName() string {
Paul Duffin71b33cc2021-06-23 11:39:47 +0100702 return c.module.BaseModuleName() + ".impl"
Paul Duffineedc5d52020-06-12 17:46:39 +0100703}
704
705// Module name of the XML file for the lib
706func (c *commonToSdkLibraryAndImport) xmlPermissionsModuleName() string {
Paul Duffin71b33cc2021-06-23 11:39:47 +0100707 return c.module.BaseModuleName() + sdkXmlFileSuffix
Paul Duffineedc5d52020-06-12 17:46:39 +0100708}
709
Paul Duffinc3091c82020-05-08 14:16:20 +0100710// Name of the java_library module that compiles the stubs source.
711func (c *commonToSdkLibraryAndImport) stubsLibraryModuleName(apiScope *apiScope) string {
Paul Duffinb97b1572021-04-29 21:50:40 +0100712 baseName := c.module.BaseModuleName()
713 return c.module.SdkMemberComponentName(baseName, func(name string) string {
714 return c.namingScheme.stubsLibraryModuleName(apiScope, name)
715 })
Paul Duffinc3091c82020-05-08 14:16:20 +0100716}
717
718// Name of the droidstubs module that generates the stubs source and may also
719// generate/check the API.
720func (c *commonToSdkLibraryAndImport) stubsSourceModuleName(apiScope *apiScope) string {
Paul Duffinb97b1572021-04-29 21:50:40 +0100721 baseName := c.module.BaseModuleName()
722 return c.module.SdkMemberComponentName(baseName, func(name string) string {
723 return c.namingScheme.stubsSourceModuleName(apiScope, name)
724 })
Paul Duffinc3091c82020-05-08 14:16:20 +0100725}
726
Paul Duffin46dc45a2020-05-14 15:39:10 +0100727// The component names for different outputs of the java_sdk_library.
728//
729// They are similar to the names used for the child modules it creates
730const (
731 stubsSourceComponentName = "stubs.source"
732
733 apiTxtComponentName = "api.txt"
734
735 removedApiTxtComponentName = "removed-api.txt"
736)
737
738// A regular expression to match tags that reference a specific stubs component.
739//
740// It will only match if given a valid scope and a valid component. It is verfy strict
741// to ensure it does not accidentally match a similar looking tag that should be processed
742// by the embedded Library.
743var tagSplitter = func() *regexp.Regexp {
744 // Given a list of literal string items returns a regular expression that will
745 // match any one of the items.
746 choice := func(items ...string) string {
747 return `\Q` + strings.Join(items, `\E|\Q`) + `\E`
748 }
749
750 // Regular expression to match one of the scopes.
751 scopesRegexp := choice(allScopeNames...)
752
753 // Regular expression to match one of the components.
754 componentsRegexp := choice(stubsSourceComponentName, apiTxtComponentName, removedApiTxtComponentName)
755
756 // Regular expression to match any combination of one scope and one component.
757 return regexp.MustCompile(fmt.Sprintf(`^\.(%s)\.(%s)$`, scopesRegexp, componentsRegexp))
758}()
759
760// For OutputFileProducer interface
761//
762// .<scope>.stubs.source
763// .<scope>.api.txt
764// .<scope>.removed-api.txt
765func (c *commonToSdkLibraryAndImport) commonOutputFiles(tag string) (android.Paths, error) {
766 if groups := tagSplitter.FindStringSubmatch(tag); groups != nil {
767 scopeName := groups[1]
768 component := groups[2]
769
770 if scope, ok := scopeByName[scopeName]; ok {
771 paths := c.findScopePaths(scope)
772 if paths == nil {
Paul Duffin71b33cc2021-06-23 11:39:47 +0100773 return nil, fmt.Errorf("%q does not provide api scope %s", c.module.BaseModuleName(), scopeName)
Paul Duffin46dc45a2020-05-14 15:39:10 +0100774 }
775
776 switch component {
777 case stubsSourceComponentName:
778 if paths.stubsSrcJar.Valid() {
779 return android.Paths{paths.stubsSrcJar.Path()}, nil
780 }
781
782 case apiTxtComponentName:
783 if paths.currentApiFilePath.Valid() {
784 return android.Paths{paths.currentApiFilePath.Path()}, nil
785 }
786
787 case removedApiTxtComponentName:
788 if paths.removedApiFilePath.Valid() {
789 return android.Paths{paths.removedApiFilePath.Path()}, nil
790 }
791 }
792
793 return nil, fmt.Errorf("%s not available for api scope %s", component, scopeName)
794 } else {
795 return nil, fmt.Errorf("unknown scope %s in %s", scope, tag)
796 }
797
798 } else {
Paul Duffina2ae7e02020-09-11 11:55:00 +0100799 switch tag {
800 case ".doctags":
801 if c.doctagPaths != nil {
802 return c.doctagPaths, nil
803 } else {
Paul Duffin71b33cc2021-06-23 11:39:47 +0100804 return nil, fmt.Errorf("no doctag_files specified on %s", c.module.BaseModuleName())
Paul Duffina2ae7e02020-09-11 11:55:00 +0100805 }
806 }
Paul Duffin46dc45a2020-05-14 15:39:10 +0100807 return nil, nil
808 }
809}
810
Paul Duffin803a9562020-05-20 11:52:25 +0100811func (c *commonToSdkLibraryAndImport) getScopePathsCreateIfNeeded(scope *apiScope) *scopePaths {
Paul Duffin56d44902020-01-31 13:36:25 +0000812 if c.scopePaths == nil {
813 c.scopePaths = make(map[*apiScope]*scopePaths)
814 }
815 paths := c.scopePaths[scope]
816 if paths == nil {
817 paths = &scopePaths{}
818 c.scopePaths[scope] = paths
819 }
820
821 return paths
822}
823
Paul Duffin803a9562020-05-20 11:52:25 +0100824func (c *commonToSdkLibraryAndImport) findScopePaths(scope *apiScope) *scopePaths {
825 if c.scopePaths == nil {
826 return nil
827 }
828
829 return c.scopePaths[scope]
830}
831
832// If this does not support the requested api scope then find the closest available
833// scope it does support. Returns nil if no such scope is available.
834func (c *commonToSdkLibraryAndImport) findClosestScopePath(scope *apiScope) *scopePaths {
835 for s := scope; s != nil; s = s.extends {
836 if paths := c.findScopePaths(s); paths != nil {
837 return paths
838 }
839 }
840
841 // This should never happen outside tests as public should be the base scope for every
842 // scope and is enabled by default.
843 return nil
844}
845
Jiyong Parkf1691d22021-03-29 20:11:58 +0900846func (c *commonToSdkLibraryAndImport) selectHeaderJarsForSdkVersion(ctx android.BaseModuleContext, sdkVersion android.SdkSpec) android.Paths {
Paul Duffinb05d4292020-05-20 12:19:10 +0100847
848 // If a specific numeric version has been requested then use prebuilt versions of the sdk.
Jiyong Park54105c42021-03-31 18:17:53 +0900849 if !sdkVersion.ApiLevel.IsPreview() {
Paul Duffin71b33cc2021-06-23 11:39:47 +0100850 return PrebuiltJars(ctx, c.module.BaseModuleName(), sdkVersion)
Paul Duffinb05d4292020-05-20 12:19:10 +0100851 }
852
Paul Duffin1267d872021-04-16 17:21:36 +0100853 paths := c.selectScopePaths(ctx, sdkVersion.Kind)
854 if paths == nil {
855 return nil
856 }
857
858 return paths.stubsHeaderPath
859}
860
861// selectScopePaths returns the *scopePaths appropriate for the specific kind.
862//
863// If the module does not support the specific kind then it will return the *scopePaths for the
864// closest kind which is a subset of the requested kind. e.g. if requesting android.SdkModule then
865// it will return *scopePaths for android.SdkSystem if available or android.SdkPublic of not.
866func (c *commonToSdkLibraryAndImport) selectScopePaths(ctx android.BaseModuleContext, kind android.SdkKind) *scopePaths {
Paul Duffin32cf58a2021-05-18 16:32:50 +0100867 apiScope := sdkKindToApiScope(kind)
Paul Duffinb05d4292020-05-20 12:19:10 +0100868
Paul Duffin803a9562020-05-20 11:52:25 +0100869 paths := c.findClosestScopePath(apiScope)
870 if paths == nil {
871 var scopes []string
872 for _, s := range allApiScopes {
873 if c.findScopePaths(s) != nil {
874 scopes = append(scopes, s.name)
875 }
876 }
Paul Duffin71b33cc2021-06-23 11:39:47 +0100877 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 +0100878 return nil
879 }
880
Paul Duffin1267d872021-04-16 17:21:36 +0100881 return paths
882}
883
Paul Duffin32cf58a2021-05-18 16:32:50 +0100884// sdkKindToApiScope maps from android.SdkKind to apiScope.
885func sdkKindToApiScope(kind android.SdkKind) *apiScope {
886 var apiScope *apiScope
887 switch kind {
888 case android.SdkSystem:
889 apiScope = apiScopeSystem
890 case android.SdkModule:
891 apiScope = apiScopeModuleLib
892 case android.SdkTest:
893 apiScope = apiScopeTest
894 case android.SdkSystemServer:
895 apiScope = apiScopeSystemServer
896 default:
897 apiScope = apiScopePublic
898 }
899 return apiScope
900}
901
Paul Duffin1267d872021-04-16 17:21:36 +0100902// to satisfy SdkLibraryDependency interface
903func (c *commonToSdkLibraryAndImport) SdkApiStubDexJar(ctx android.BaseModuleContext, kind android.SdkKind) android.Path {
904 paths := c.selectScopePaths(ctx, kind)
905 if paths == nil {
906 return nil
907 }
908
909 return paths.stubsDexJarPath
Paul Duffinb05d4292020-05-20 12:19:10 +0100910}
911
Paul Duffin32cf58a2021-05-18 16:32:50 +0100912// to satisfy SdkLibraryDependency interface
913func (c *commonToSdkLibraryAndImport) SdkRemovedTxtFile(ctx android.BaseModuleContext, kind android.SdkKind) android.OptionalPath {
914 apiScope := sdkKindToApiScope(kind)
915 paths := c.findScopePaths(apiScope)
916 if paths == nil {
917 return android.OptionalPath{}
918 }
919
920 return paths.removedApiFilePath
921}
922
Paul Duffin859fe962020-05-15 10:20:31 +0100923func (c *commonToSdkLibraryAndImport) sdkComponentPropertiesForChildLibrary() interface{} {
924 componentProps := &struct {
925 SdkLibraryToImplicitlyTrack *string
Paul Duffindfa131e2020-05-15 20:37:11 +0100926 }{}
927
928 if c.sharedLibrary() {
Paul Duffin859fe962020-05-15 10:20:31 +0100929 // Mark the stubs library as being components of this java_sdk_library so that
930 // any app that includes code which depends (directly or indirectly) on the stubs
931 // library will have the appropriate <uses-library> invocation inserted into its
932 // manifest if necessary.
Paul Duffin71b33cc2021-06-23 11:39:47 +0100933 componentProps.SdkLibraryToImplicitlyTrack = proptools.StringPtr(c.module.BaseModuleName())
Paul Duffin859fe962020-05-15 10:20:31 +0100934 }
935
936 return componentProps
937}
938
Paul Duffindfa131e2020-05-15 20:37:11 +0100939func (c *commonToSdkLibraryAndImport) sharedLibrary() bool {
940 return proptools.BoolDefault(c.commonSdkLibraryProperties.Shared_library, true)
941}
942
Paul Duffinf4600f62021-05-13 22:34:45 +0100943// Check if the stub libraries should be compiled for dex
944func (c *commonToSdkLibraryAndImport) stubLibrariesCompiledForDex() bool {
945 // Always compile the dex file files for the stub libraries if they will be used on the
946 // bootclasspath.
947 return !c.sharedLibrary()
948}
949
Paul Duffin859fe962020-05-15 10:20:31 +0100950// Properties related to the use of a module as an component of a java_sdk_library.
951type SdkLibraryComponentProperties struct {
952
953 // The name of the java_sdk_library/_import to add to a <uses-library> entry
954 // in the AndroidManifest.xml of any Android app that includes code that references
955 // this module. If not set then no java_sdk_library/_import is tracked.
956 SdkLibraryToImplicitlyTrack *string `blueprint:"mutated"`
957}
958
959// Structure to be embedded in a module struct that needs to support the
960// SdkLibraryComponentDependency interface.
961type EmbeddableSdkLibraryComponent struct {
962 sdkLibraryComponentProperties SdkLibraryComponentProperties
963}
964
Paul Duffin71b33cc2021-06-23 11:39:47 +0100965func (e *EmbeddableSdkLibraryComponent) initSdkLibraryComponent(module android.Module) {
966 module.AddProperties(&e.sdkLibraryComponentProperties)
Paul Duffin859fe962020-05-15 10:20:31 +0100967}
968
969// to satisfy SdkLibraryComponentDependency
Ulya Trafimovich31e444e2020-08-14 17:32:16 +0100970func (e *EmbeddableSdkLibraryComponent) OptionalImplicitSdkLibrary() *string {
971 return e.sdkLibraryComponentProperties.SdkLibraryToImplicitlyTrack
Paul Duffin859fe962020-05-15 10:20:31 +0100972}
973
Ulya Trafimovich39b437b2020-09-23 16:42:35 +0100974// to satisfy SdkLibraryComponentDependency
975func (e *EmbeddableSdkLibraryComponent) OptionalSdkLibraryImplementation() *string {
976 // Currently implementation library name is the same as the SDK library name.
977 return e.sdkLibraryComponentProperties.SdkLibraryToImplicitlyTrack
978}
979
Paul Duffin859fe962020-05-15 10:20:31 +0100980// Implemented by modules that are (or possibly could be) a component of a java_sdk_library
981// (including the java_sdk_library) itself.
982type SdkLibraryComponentDependency interface {
Ulya Trafimovich31e444e2020-08-14 17:32:16 +0100983 UsesLibraryDependency
984
Paul Duffin859fe962020-05-15 10:20:31 +0100985 // The optional name of the sdk library that should be implicitly added to the
986 // AndroidManifest of an app that contains code which references the sdk library.
987 //
Ulya Trafimovich31e444e2020-08-14 17:32:16 +0100988 // Returns the name of the optional implicit SDK library or nil, if there isn't one.
989 OptionalImplicitSdkLibrary() *string
Ulya Trafimovich39b437b2020-09-23 16:42:35 +0100990
991 // The name of the implementation library for the optional SDK library or nil, if there isn't one.
992 OptionalSdkLibraryImplementation() *string
Paul Duffin859fe962020-05-15 10:20:31 +0100993}
994
995// Make sure that all the module types that are components of java_sdk_library/_import
996// and which can be referenced (directly or indirectly) from an android app implement
997// the SdkLibraryComponentDependency interface.
998var _ SdkLibraryComponentDependency = (*Library)(nil)
999var _ SdkLibraryComponentDependency = (*Import)(nil)
1000var _ SdkLibraryComponentDependency = (*SdkLibrary)(nil)
Paul Duffineedc5d52020-06-12 17:46:39 +01001001var _ SdkLibraryComponentDependency = (*SdkLibraryImport)(nil)
Paul Duffin859fe962020-05-15 10:20:31 +01001002
Paul Duffin32cf58a2021-05-18 16:32:50 +01001003// Provides access to sdk_version related files, e.g. header and implementation jars.
Paul Duffin859fe962020-05-15 10:20:31 +01001004type SdkLibraryDependency interface {
1005 SdkLibraryComponentDependency
1006
1007 // Get the header jars appropriate for the supplied sdk_version.
1008 //
1009 // These are turbine generated jars so they only change if the externals of the
1010 // class changes but it does not contain and implementation or JavaDoc.
Jiyong Parkf1691d22021-03-29 20:11:58 +09001011 SdkHeaderJars(ctx android.BaseModuleContext, sdkVersion android.SdkSpec) android.Paths
Paul Duffin859fe962020-05-15 10:20:31 +01001012
1013 // Get the implementation jars appropriate for the supplied sdk version.
1014 //
1015 // These are either the implementation jar for the whole sdk library or the implementation
1016 // jars for the stubs. The latter should only be needed when generating JavaDoc as otherwise
1017 // they are identical to the corresponding header jars.
Jiyong Parkf1691d22021-03-29 20:11:58 +09001018 SdkImplementationJars(ctx android.BaseModuleContext, sdkVersion android.SdkSpec) android.Paths
Paul Duffin1267d872021-04-16 17:21:36 +01001019
1020 // SdkApiStubDexJar returns the dex jar for the stubs. It is needed by the hiddenapi processing
1021 // tool which processes dex files.
1022 SdkApiStubDexJar(ctx android.BaseModuleContext, kind android.SdkKind) android.Path
Paul Duffinf4600f62021-05-13 22:34:45 +01001023
Paul Duffin32cf58a2021-05-18 16:32:50 +01001024 // SdkRemovedTxtFile returns the optional path to the removed.txt file for the specified sdk kind.
1025 SdkRemovedTxtFile(ctx android.BaseModuleContext, kind android.SdkKind) android.OptionalPath
1026
Paul Duffinf4600f62021-05-13 22:34:45 +01001027 // sharedLibrary returns true if this can be used as a shared library.
1028 sharedLibrary() bool
Paul Duffin859fe962020-05-15 10:20:31 +01001029}
1030
Inseob Kimc0907f12019-02-08 21:00:45 +09001031type SdkLibrary struct {
Sundong Ahn054b19a2018-10-19 13:46:09 +09001032 Library
Jiyong Parkc678ad32018-04-10 13:07:10 +09001033
Sundong Ahn054b19a2018-10-19 13:46:09 +09001034 sdkLibraryProperties sdkLibraryProperties
Jiyong Parkc678ad32018-04-10 13:07:10 +09001035
Paul Duffin3375e352020-04-28 10:44:03 +01001036 // Map from api scope to the scope specific property structure.
1037 scopeToProperties map[*apiScope]*ApiScopeProperties
1038
Paul Duffin56d44902020-01-31 13:36:25 +00001039 commonToSdkLibraryAndImport
Jiyong Parkc678ad32018-04-10 13:07:10 +09001040}
1041
Inseob Kimc0907f12019-02-08 21:00:45 +09001042var _ SdkLibraryDependency = (*SdkLibrary)(nil)
Colin Cross897d2ed2019-02-11 14:03:51 -08001043
Paul Duffin3375e352020-04-28 10:44:03 +01001044func (module *SdkLibrary) generateTestAndSystemScopesByDefault() bool {
1045 return module.sdkLibraryProperties.Generate_system_and_test_apis
1046}
1047
1048func (module *SdkLibrary) getGeneratedApiScopes(ctx android.EarlyModuleContext) apiScopes {
1049 // Check to see if any scopes have been explicitly enabled. If any have then all
1050 // must be.
1051 anyScopesExplicitlyEnabled := false
1052 for _, scope := range allApiScopes {
1053 scopeProperties := module.scopeToProperties[scope]
1054 if scopeProperties.Enabled != nil {
1055 anyScopesExplicitlyEnabled = true
1056 break
1057 }
Paul Duffind1b3a922020-01-22 11:57:20 +00001058 }
Paul Duffin3375e352020-04-28 10:44:03 +01001059
1060 var generatedScopes apiScopes
1061 enabledScopes := make(map[*apiScope]struct{})
1062 for _, scope := range allApiScopes {
1063 scopeProperties := module.scopeToProperties[scope]
1064 // If any scopes are explicitly enabled then ignore the legacy enabled status.
1065 // This is to ensure that any new usages of this module type do not rely on legacy
1066 // behaviour.
1067 defaultEnabledStatus := false
1068 if anyScopesExplicitlyEnabled {
1069 defaultEnabledStatus = scope.defaultEnabledStatus
1070 } else {
1071 defaultEnabledStatus = scope.legacyEnabledStatus(module)
1072 }
1073 enabled := proptools.BoolDefault(scopeProperties.Enabled, defaultEnabledStatus)
1074 if enabled {
1075 enabledScopes[scope] = struct{}{}
1076 generatedScopes = append(generatedScopes, scope)
1077 }
1078 }
1079
1080 // Now check to make sure that any scope that is extended by an enabled scope is also
1081 // enabled.
1082 for _, scope := range allApiScopes {
1083 if _, ok := enabledScopes[scope]; ok {
1084 extends := scope.extends
1085 if extends != nil {
1086 if _, ok := enabledScopes[extends]; !ok {
1087 ctx.ModuleErrorf("enabled api scope %q depends on disabled scope %q", scope, extends)
1088 }
1089 }
1090 }
1091 }
1092
1093 return generatedScopes
Paul Duffind1b3a922020-01-22 11:57:20 +00001094}
1095
Paul Duffineedc5d52020-06-12 17:46:39 +01001096type sdkLibraryComponentTag struct {
1097 blueprint.BaseDependencyTag
1098 name string
1099}
1100
1101// Mark this tag so dependencies that use it are excluded from visibility enforcement.
1102func (t sdkLibraryComponentTag) ExcludeFromVisibilityEnforcement() {}
1103
1104var xmlPermissionsFileTag = sdkLibraryComponentTag{name: "xml-permissions-file"}
Paul Duffine74ac732020-02-06 13:51:46 +00001105
Jiyong Parke3833882020-02-17 17:28:10 +09001106func IsXmlPermissionsFileDepTag(depTag blueprint.DependencyTag) bool {
Paul Duffineedc5d52020-06-12 17:46:39 +01001107 if dt, ok := depTag.(sdkLibraryComponentTag); ok {
Jiyong Parke3833882020-02-17 17:28:10 +09001108 return dt == xmlPermissionsFileTag
1109 }
1110 return false
1111}
1112
Paul Duffineedc5d52020-06-12 17:46:39 +01001113var implLibraryTag = sdkLibraryComponentTag{name: "impl-library"}
Paul Duffin5df79302020-05-16 15:52:12 +01001114
Paul Duffin44f1d842020-06-26 20:17:02 +01001115// Add the dependencies on the child modules in the component deps mutator.
1116func (module *SdkLibrary) ComponentDepsMutator(ctx android.BottomUpMutatorContext) {
Paul Duffin3375e352020-04-28 10:44:03 +01001117 for _, apiScope := range module.getGeneratedApiScopes(ctx) {
Paul Duffind1b3a922020-01-22 11:57:20 +00001118 // Add dependencies to the stubs library
Paul Duffinc3091c82020-05-08 14:16:20 +01001119 ctx.AddVariationDependencies(nil, apiScope.stubsTag, module.stubsLibraryModuleName(apiScope))
Paul Duffind1b3a922020-01-22 11:57:20 +00001120
Paul Duffin15f34ef2020-07-20 18:04:44 +01001121 // Add a dependency on the stubs source in order to access both stubs source and api information.
1122 ctx.AddVariationDependencies(nil, apiScope.stubsSourceAndApiTag, module.stubsSourceModuleName(apiScope))
Sundong Ahn054b19a2018-10-19 13:46:09 +09001123 }
1124
Paul Duffindfa131e2020-05-15 20:37:11 +01001125 if module.requiresRuntimeImplementationLibrary() {
Paul Duffin5df79302020-05-16 15:52:12 +01001126 // Add dependency to the rule for generating the implementation library.
1127 ctx.AddDependency(module, implLibraryTag, module.implLibraryModuleName())
1128
Paul Duffindfa131e2020-05-15 20:37:11 +01001129 if module.sharedLibrary() {
1130 // Add dependency to the rule for generating the xml permissions file
Paul Duffineedc5d52020-06-12 17:46:39 +01001131 ctx.AddDependency(module, xmlPermissionsFileTag, module.xmlPermissionsModuleName())
Paul Duffindfa131e2020-05-15 20:37:11 +01001132 }
Paul Duffin44f1d842020-06-26 20:17:02 +01001133 }
1134}
Paul Duffine74ac732020-02-06 13:51:46 +00001135
Paul Duffin44f1d842020-06-26 20:17:02 +01001136// Add other dependencies as normal.
1137func (module *SdkLibrary) DepsMutator(ctx android.BottomUpMutatorContext) {
Anton Hanssone77fccc2021-01-20 16:52:41 +00001138 var missingApiModules []string
1139 for _, apiScope := range module.getGeneratedApiScopes(ctx) {
1140 if apiScope.unstable {
1141 continue
1142 }
1143 if m := android.SrcIsModule(module.latestApiFilegroupName(apiScope)); !ctx.OtherModuleExists(m) {
1144 missingApiModules = append(missingApiModules, m)
1145 }
1146 if m := android.SrcIsModule(module.latestRemovedApiFilegroupName(apiScope)); !ctx.OtherModuleExists(m) {
1147 missingApiModules = append(missingApiModules, m)
1148 }
Jaewoong Jung1a97ee02021-03-09 13:25:02 -08001149 if m := android.SrcIsModule(module.latestIncompatibilitiesFilegroupName(apiScope)); !ctx.OtherModuleExists(m) {
1150 missingApiModules = append(missingApiModules, m)
1151 }
Anton Hanssone77fccc2021-01-20 16:52:41 +00001152 }
1153 if len(missingApiModules) != 0 && !module.sdkLibraryProperties.Unsafe_ignore_missing_latest_api {
1154 m := module.Name() + " is missing tracking files for previously released library versions.\n"
1155 m += "You need to do one of the following:\n"
1156 m += "- Add `unsafe_ignore_missing_latest_api: true` to your blueprint (to disable compat tracking)\n"
1157 m += "- Add a set of prebuilt txt files representing the last released version of this library for compat checking.\n"
1158 m += " (the current set of API files can be used as a seed for this compatibility tracking\n"
1159 m += "\n"
1160 m += "The following filegroup modules are missing:\n "
1161 m += strings.Join(missingApiModules, "\n ") + "\n"
1162 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."
1163 ctx.ModuleErrorf(m)
1164 }
Paul Duffin44f1d842020-06-26 20:17:02 +01001165 if module.requiresRuntimeImplementationLibrary() {
Paul Duffindfa131e2020-05-15 20:37:11 +01001166 // Only add the deps for the library if it is actually going to be built.
1167 module.Library.deps(ctx)
1168 }
Jiyong Parkc678ad32018-04-10 13:07:10 +09001169}
1170
Paul Duffin46dc45a2020-05-14 15:39:10 +01001171func (module *SdkLibrary) OutputFiles(tag string) (android.Paths, error) {
1172 paths, err := module.commonOutputFiles(tag)
1173 if paths == nil && err == nil {
1174 return module.Library.OutputFiles(tag)
1175 } else {
1176 return paths, err
1177 }
1178}
1179
Inseob Kimc0907f12019-02-08 21:00:45 +09001180func (module *SdkLibrary) GenerateAndroidBuildActions(ctx android.ModuleContext) {
Paul Duffina2ae7e02020-09-11 11:55:00 +01001181 module.generateCommonBuildActions(ctx)
1182
Paul Duffindfa131e2020-05-15 20:37:11 +01001183 // Only build an implementation library if required.
1184 if module.requiresRuntimeImplementationLibrary() {
Paul Duffin43db9be2019-12-30 17:35:49 +00001185 module.Library.GenerateAndroidBuildActions(ctx)
1186 }
Sundong Ahn054b19a2018-10-19 13:46:09 +09001187
Paul Duffinb97b1572021-04-29 21:50:40 +01001188 // Collate the components exported by this module. All scope specific modules are exported but
1189 // the impl and xml component modules are not.
1190 exportedComponents := map[string]struct{}{}
1191
Sundong Ahn57368eb2018-07-06 11:20:23 +09001192 // Record the paths to the header jars of the library (stubs and impl).
Paul Duffind1b3a922020-01-22 11:57:20 +00001193 // When this java_sdk_library is depended upon from others via "libs" property,
Jiyong Parkc678ad32018-04-10 13:07:10 +09001194 // the recorded paths will be returned depending on the link type of the caller.
1195 ctx.VisitDirectDeps(func(to android.Module) {
Jiyong Parkc678ad32018-04-10 13:07:10 +09001196 tag := ctx.OtherModuleDependencyTag(to)
1197
Paul Duffinc8782502020-04-29 20:45:27 +01001198 // Extract information from any of the scope specific dependencies.
1199 if scopeTag, ok := tag.(scopeDependencyTag); ok {
1200 apiScope := scopeTag.apiScope
Paul Duffin803a9562020-05-20 11:52:25 +01001201 scopePaths := module.getScopePathsCreateIfNeeded(apiScope)
Paul Duffinc8782502020-04-29 20:45:27 +01001202
1203 // Extract information from the dependency. The exact information extracted
1204 // is determined by the nature of the dependency which is determined by the tag.
1205 scopeTag.extractDepInfo(ctx, to, scopePaths)
Paul Duffinb97b1572021-04-29 21:50:40 +01001206
1207 exportedComponents[ctx.OtherModuleName(to)] = struct{}{}
Sundong Ahn20e998b2018-07-24 11:19:26 +09001208 }
Jiyong Parkc678ad32018-04-10 13:07:10 +09001209 })
Paul Duffinb97b1572021-04-29 21:50:40 +01001210
1211 // Make the set of components exported by this module available for use elsewhere.
1212 exportedComponentInfo := android.ExportedComponentsInfo{Components: android.SortedStringKeys(exportedComponents)}
1213 ctx.SetProvider(android.ExportedComponentsInfoProvider, exportedComponentInfo)
Jiyong Parkc678ad32018-04-10 13:07:10 +09001214}
1215
Jiyong Park0b0e1b92019-12-03 13:24:29 +09001216func (module *SdkLibrary) AndroidMkEntries() []android.AndroidMkEntries {
Paul Duffindfa131e2020-05-15 20:37:11 +01001217 if !module.requiresRuntimeImplementationLibrary() {
Paul Duffin43db9be2019-12-30 17:35:49 +00001218 return nil
1219 }
Jiyong Park0b0e1b92019-12-03 13:24:29 +09001220 entriesList := module.Library.AndroidMkEntries()
Yo Chiang07d75072020-06-05 17:43:19 +08001221 if module.sharedLibrary() {
1222 entries := &entriesList[0]
1223 entries.Required = append(entries.Required, module.xmlPermissionsModuleName())
1224 }
Jiyong Park0b0e1b92019-12-03 13:24:29 +09001225 return entriesList
Jiyong Park82484c02018-04-23 21:41:26 +09001226}
1227
Anton Hansson5fd5d242020-03-27 19:43:19 +00001228// The dist path of the stub artifacts
1229func (module *SdkLibrary) apiDistPath(apiScope *apiScope) string {
Colin Crossf0eace92021-06-02 13:02:23 -07001230 return path.Join("apistubs", module.distGroup(), apiScope.name)
Anton Hansson5fd5d242020-03-27 19:43:19 +00001231}
1232
Paul Duffin12ceb462019-12-24 20:31:31 +00001233// Get the sdk version for use when compiling the stubs library.
Paul Duffin780c5f42020-05-12 15:52:55 +01001234func (module *SdkLibrary) sdkVersionForStubsLibrary(mctx android.EarlyModuleContext, apiScope *apiScope) string {
Paul Duffin87a05a32020-05-12 11:50:28 +01001235 scopeProperties := module.scopeToProperties[apiScope]
1236 if scopeProperties.Sdk_version != nil {
1237 return proptools.String(scopeProperties.Sdk_version)
1238 }
1239
Jiyong Parkf1691d22021-03-29 20:11:58 +09001240 sdkDep := decodeSdkDep(mctx, android.SdkContext(&module.Library))
Paul Duffin12ceb462019-12-24 20:31:31 +00001241 if sdkDep.hasStandardLibs() {
1242 // If building against a standard sdk then use the sdk version appropriate for the scope.
Paul Duffind1b3a922020-01-22 11:57:20 +00001243 return apiScope.sdkVersion
Paul Duffin12ceb462019-12-24 20:31:31 +00001244 } else {
1245 // Otherwise, use no system module.
1246 return "none"
1247 }
1248}
1249
Paul Duffin31310252020-11-20 21:26:20 +00001250func (module *SdkLibrary) distStem() string {
1251 return proptools.StringDefault(module.sdkLibraryProperties.Dist_stem, module.BaseModuleName())
1252}
1253
Colin Cross986b69a2021-06-01 13:13:40 -07001254// distGroup returns the subdirectory of the dist path of the stub artifacts.
1255func (module *SdkLibrary) distGroup() string {
Colin Cross59b92bf2021-06-01 14:07:56 -07001256 return proptools.StringDefault(module.sdkLibraryProperties.Dist_group, "unknown")
Colin Cross986b69a2021-06-01 13:13:40 -07001257}
1258
Paul Duffind1b3a922020-01-22 11:57:20 +00001259func (module *SdkLibrary) latestApiFilegroupName(apiScope *apiScope) string {
Paul Duffin31310252020-11-20 21:26:20 +00001260 return ":" + module.distStem() + ".api." + apiScope.name + ".latest"
Jiyong Park58c518b2018-05-12 22:29:12 +09001261}
Jiyong Parkc678ad32018-04-10 13:07:10 +09001262
Paul Duffind1b3a922020-01-22 11:57:20 +00001263func (module *SdkLibrary) latestRemovedApiFilegroupName(apiScope *apiScope) string {
Paul Duffin31310252020-11-20 21:26:20 +00001264 return ":" + module.distStem() + "-removed.api." + apiScope.name + ".latest"
Jiyong Parkc678ad32018-04-10 13:07:10 +09001265}
1266
Jaewoong Jung1a97ee02021-03-09 13:25:02 -08001267func (module *SdkLibrary) latestIncompatibilitiesFilegroupName(apiScope *apiScope) string {
1268 return ":" + module.distStem() + "-incompatibilities.api." + apiScope.name + ".latest"
1269}
1270
Anton Hansson944e77d2020-08-19 11:40:22 +01001271func childModuleVisibility(childVisibility []string) []string {
1272 if childVisibility == nil {
1273 // No child visibility set. The child will use the visibility of the sdk_library.
1274 return nil
1275 }
1276
1277 // Prepend an override to ignore the sdk_library's visibility, and rely on the child visibility.
1278 var visibility []string
1279 visibility = append(visibility, "//visibility:override")
1280 visibility = append(visibility, childVisibility...)
1281 return visibility
1282}
1283
Paul Duffin5df79302020-05-16 15:52:12 +01001284// Creates the implementation java library
1285func (module *SdkLibrary) createImplLibrary(mctx android.DefaultableHookContext) {
Anton Hansson944e77d2020-08-19 11:40:22 +01001286 visibility := childModuleVisibility(module.sdkLibraryProperties.Impl_library_visibility)
1287
Paul Duffin5df79302020-05-16 15:52:12 +01001288 props := struct {
Paul Duffin66cdbf02021-05-14 16:35:06 +01001289 Name *string
1290 Visibility []string
1291 Instrument bool
1292 Libs []string
Paul Duffin5df79302020-05-16 15:52:12 +01001293 }{
1294 Name: proptools.StringPtr(module.implLibraryModuleName()),
Anton Hansson944e77d2020-08-19 11:40:22 +01001295 Visibility: visibility,
Paul Duffin296cf332020-06-18 21:09:55 +01001296 // Set the instrument property to ensure it is instrumented when instrumentation is required.
1297 Instrument: true,
Anton Hansson7f66efa2020-10-08 14:47:23 +01001298 // Set the impl_only libs. Note that the module's "Libs" get appended as well, via the
1299 // addition of &module.properties below.
1300 Libs: module.sdkLibraryProperties.Impl_only_libs,
Paul Duffin5df79302020-05-16 15:52:12 +01001301 }
1302
1303 properties := []interface{}{
1304 &module.properties,
1305 &module.protoProperties,
1306 &module.deviceProperties,
Liz Kammera7a64f32020-07-09 15:16:41 -07001307 &module.dexProperties,
Paul Duffin5df79302020-05-16 15:52:12 +01001308 &module.dexpreoptProperties,
Colin Cross014489c2020-06-02 20:09:13 -07001309 &module.linter.properties,
Paul Duffin5df79302020-05-16 15:52:12 +01001310 &props,
1311 module.sdkComponentPropertiesForChildLibrary(),
1312 }
1313 mctx.CreateModule(LibraryFactory, properties...)
1314}
1315
Jiyong Parkc678ad32018-04-10 13:07:10 +09001316// Creates a static java library that has API stubs
Paul Duffinf0229202020-04-29 16:47:28 +01001317func (module *SdkLibrary) createStubsLibrary(mctx android.DefaultableHookContext, apiScope *apiScope) {
Jiyong Parkc678ad32018-04-10 13:07:10 +09001318 props := struct {
Dan Willemsen9f435972020-05-28 15:28:00 -07001319 Name *string
1320 Visibility []string
1321 Srcs []string
1322 Installable *bool
1323 Sdk_version *string
1324 System_modules *string
1325 Patch_module *string
1326 Libs []string
Anton Hanssondae54cd2021-04-21 16:30:10 +01001327 Static_libs []string
Dan Willemsen9f435972020-05-28 15:28:00 -07001328 Compile_dex *bool
1329 Java_version *string
1330 Openjdk9 struct {
Sundong Ahn054b19a2018-10-19 13:46:09 +09001331 Srcs []string
1332 Javacflags []string
1333 }
Anton Hansson5fd5d242020-03-27 19:43:19 +00001334 Dist struct {
1335 Targets []string
1336 Dest *string
1337 Dir *string
1338 Tag *string
1339 }
Jiyong Parkc678ad32018-04-10 13:07:10 +09001340 }{}
1341
Paul Duffinc3091c82020-05-08 14:16:20 +01001342 props.Name = proptools.StringPtr(module.stubsLibraryModuleName(apiScope))
Anton Hansson944e77d2020-08-19 11:40:22 +01001343 props.Visibility = childModuleVisibility(module.sdkLibraryProperties.Stubs_library_visibility)
Jiyong Parkc678ad32018-04-10 13:07:10 +09001344 // sources are generated from the droiddoc
Paul Duffinc3091c82020-05-08 14:16:20 +01001345 props.Srcs = []string{":" + module.stubsSourceModuleName(apiScope)}
Paul Duffin12ceb462019-12-24 20:31:31 +00001346 sdkVersion := module.sdkVersionForStubsLibrary(mctx, apiScope)
Paul Duffin52d398a2019-06-11 12:31:14 +01001347 props.Sdk_version = proptools.StringPtr(sdkVersion)
Paul Duffina18abc22020-05-16 18:54:24 +01001348 props.System_modules = module.deviceProperties.System_modules
1349 props.Patch_module = module.properties.Patch_module
Paul Duffin367ab912019-12-23 19:40:36 +00001350 props.Installable = proptools.BoolPtr(false)
Sundong Ahn054b19a2018-10-19 13:46:09 +09001351 props.Libs = module.sdkLibraryProperties.Stub_only_libs
Anton Hanssondae54cd2021-04-21 16:30:10 +01001352 props.Static_libs = module.sdkLibraryProperties.Stub_only_static_libs
Paul Duffine22c2ab2020-05-20 19:35:27 +01001353 // The stub-annotations library contains special versions of the annotations
1354 // with CLASS retention policy, so that they're kept.
1355 if proptools.Bool(module.sdkLibraryProperties.Annotations_enabled) {
1356 props.Libs = append(props.Libs, "stub-annotations")
1357 }
Paul Duffina18abc22020-05-16 18:54:24 +01001358 props.Openjdk9.Srcs = module.properties.Openjdk9.Srcs
1359 props.Openjdk9.Javacflags = module.properties.Openjdk9.Javacflags
Anton Hansson83509b52020-05-21 09:21:57 +01001360 // We compile the stubs for 1.8 in line with the main android.jar stubs, and potential
1361 // interop with older developer tools that don't support 1.9.
1362 props.Java_version = proptools.StringPtr("1.8")
Paul Duffinf4600f62021-05-13 22:34:45 +01001363
1364 // The imports need to be compiled to dex if the java_sdk_library requests it.
1365 compileDex := module.dexProperties.Compile_dex
1366 if module.stubLibrariesCompiledForDex() {
1367 compileDex = proptools.BoolPtr(true)
Sundong Ahndd567f92018-07-31 17:19:11 +09001368 }
Paul Duffinf4600f62021-05-13 22:34:45 +01001369 props.Compile_dex = compileDex
Jiyong Parkc678ad32018-04-10 13:07:10 +09001370
Anton Hansson5fd5d242020-03-27 19:43:19 +00001371 // Dist the class jar artifact for sdk builds.
1372 if !Bool(module.sdkLibraryProperties.No_dist) {
1373 props.Dist.Targets = []string{"sdk", "win_sdk"}
Paul Duffin31310252020-11-20 21:26:20 +00001374 props.Dist.Dest = proptools.StringPtr(fmt.Sprintf("%v.jar", module.distStem()))
Anton Hansson5fd5d242020-03-27 19:43:19 +00001375 props.Dist.Dir = proptools.StringPtr(module.apiDistPath(apiScope))
1376 props.Dist.Tag = proptools.StringPtr(".jar")
1377 }
Jiyong Parkc678ad32018-04-10 13:07:10 +09001378
Paul Duffin859fe962020-05-15 10:20:31 +01001379 mctx.CreateModule(LibraryFactory, &props, module.sdkComponentPropertiesForChildLibrary())
Jiyong Parkc678ad32018-04-10 13:07:10 +09001380}
1381
Paul Duffin6d0886e2020-04-07 18:49:53 +01001382// Creates a droidstubs module that creates stubs source files from the given full source
Paul Duffinc8782502020-04-29 20:45:27 +01001383// files and also updates and checks the API specification files.
Paul Duffin15f34ef2020-07-20 18:04:44 +01001384func (module *SdkLibrary) createStubsSourcesAndApi(mctx android.DefaultableHookContext, apiScope *apiScope, name string, scopeSpecificDroidstubsArgs []string) {
Jiyong Parkc678ad32018-04-10 13:07:10 +09001385 props := struct {
Sundong Ahn054b19a2018-10-19 13:46:09 +09001386 Name *string
Paul Duffin4911a892020-04-29 23:35:13 +01001387 Visibility []string
Sundong Ahn054b19a2018-10-19 13:46:09 +09001388 Srcs []string
1389 Installable *bool
Paul Duffin52d398a2019-06-11 12:31:14 +01001390 Sdk_version *string
Paul Duffin12ceb462019-12-24 20:31:31 +00001391 System_modules *string
Sundong Ahn054b19a2018-10-19 13:46:09 +09001392 Libs []string
Paul Duffin6877e6d2020-09-25 19:59:14 +01001393 Output_javadoc_comments *bool
Paul Duffin11512472019-02-11 15:55:17 +00001394 Arg_files []string
Sundong Ahn054b19a2018-10-19 13:46:09 +09001395 Args *string
Sundong Ahn054b19a2018-10-19 13:46:09 +09001396 Java_version *string
Paul Duffine22c2ab2020-05-20 19:35:27 +01001397 Annotations_enabled *bool
Sundong Ahn054b19a2018-10-19 13:46:09 +09001398 Merge_annotations_dirs []string
1399 Merge_inclusion_annotations_dirs []string
Paul Duffin0ff08bd2020-04-29 13:30:54 +01001400 Generate_stubs *bool
Anton Hanssone87b03d2020-12-21 15:29:34 +00001401 Previous_api *string
Sundong Ahn054b19a2018-10-19 13:46:09 +09001402 Check_api struct {
Anton Hanssone6056152020-12-31 10:37:27 +00001403 Current ApiToCheck
1404 Last_released ApiToCheck
Paul Duffin160fe412020-05-10 19:32:20 +01001405
1406 Api_lint struct {
1407 Enabled *bool
1408 New_since *string
1409 Baseline_file *string
1410 }
Jiyong Park58c518b2018-05-12 22:29:12 +09001411 }
Sundong Ahn1b92c822018-05-29 11:35:17 +09001412 Aidl struct {
1413 Include_dirs []string
1414 Local_include_dirs []string
1415 }
Paul Duffin040e9062020-11-23 17:41:36 +00001416 Dists []android.Dist
Jiyong Parkc678ad32018-04-10 13:07:10 +09001417 }{}
1418
Paul Duffin7b78b4d2020-04-28 14:08:32 +01001419 // The stubs source processing uses the same compile time classpath when extracting the
1420 // API from the implementation library as it does when compiling it. i.e. the same
1421 // * sdk version
1422 // * system_modules
1423 // * libs (static_libs/libs)
Paul Duffin250e6192019-06-07 10:44:37 +01001424
Paul Duffin0ff08bd2020-04-29 13:30:54 +01001425 props.Name = proptools.StringPtr(name)
Anton Hansson944e77d2020-08-19 11:40:22 +01001426 props.Visibility = childModuleVisibility(module.sdkLibraryProperties.Stubs_source_visibility)
Paul Duffina18abc22020-05-16 18:54:24 +01001427 props.Srcs = append(props.Srcs, module.properties.Srcs...)
1428 props.Sdk_version = module.deviceProperties.Sdk_version
1429 props.System_modules = module.deviceProperties.System_modules
Jiyong Parkc678ad32018-04-10 13:07:10 +09001430 props.Installable = proptools.BoolPtr(false)
Sundong Ahne6f0b052018-06-05 16:46:14 +09001431 // A droiddoc module has only one Libs property and doesn't distinguish between
1432 // shared libs and static libs. So we need to add both of these libs to Libs property.
Paul Duffina18abc22020-05-16 18:54:24 +01001433 props.Libs = module.properties.Libs
1434 props.Libs = append(props.Libs, module.properties.Static_libs...)
1435 props.Aidl.Include_dirs = module.deviceProperties.Aidl.Include_dirs
1436 props.Aidl.Local_include_dirs = module.deviceProperties.Aidl.Local_include_dirs
1437 props.Java_version = module.properties.Java_version
Jiyong Parkc678ad32018-04-10 13:07:10 +09001438
Paul Duffine22c2ab2020-05-20 19:35:27 +01001439 props.Annotations_enabled = module.sdkLibraryProperties.Annotations_enabled
Sundong Ahn054b19a2018-10-19 13:46:09 +09001440 props.Merge_annotations_dirs = module.sdkLibraryProperties.Merge_annotations_dirs
1441 props.Merge_inclusion_annotations_dirs = module.sdkLibraryProperties.Merge_inclusion_annotations_dirs
1442
Paul Duffin6d0886e2020-04-07 18:49:53 +01001443 droidstubsArgs := []string{}
Paul Duffin235ffff2019-12-24 10:41:30 +00001444 if len(module.sdkLibraryProperties.Api_packages) != 0 {
Paul Duffin6d0886e2020-04-07 18:49:53 +01001445 droidstubsArgs = append(droidstubsArgs, "--stub-packages "+strings.Join(module.sdkLibraryProperties.Api_packages, ":"))
Paul Duffin235ffff2019-12-24 10:41:30 +00001446 }
1447 if len(module.sdkLibraryProperties.Hidden_api_packages) != 0 {
Paul Duffin6d0886e2020-04-07 18:49:53 +01001448 droidstubsArgs = append(droidstubsArgs,
Paul Duffin235ffff2019-12-24 10:41:30 +00001449 android.JoinWithPrefix(module.sdkLibraryProperties.Hidden_api_packages, " --hide-package "))
1450 }
Paul Duffin6d0886e2020-04-07 18:49:53 +01001451 droidstubsArgs = append(droidstubsArgs, module.sdkLibraryProperties.Droiddoc_options...)
Paul Duffin235ffff2019-12-24 10:41:30 +00001452 disabledWarnings := []string{
1453 "MissingPermission",
1454 "BroadcastBehavior",
1455 "HiddenSuperclass",
1456 "DeprecationMismatch",
1457 "UnavailableSymbol",
1458 "SdkConstant",
1459 "HiddenTypeParameter",
1460 "Todo",
1461 "Typo",
1462 }
Paul Duffin6d0886e2020-04-07 18:49:53 +01001463 droidstubsArgs = append(droidstubsArgs, android.JoinWithPrefix(disabledWarnings, "--hide "))
Sundong Ahnfb2721f2018-09-17 13:23:09 +09001464
Paul Duffin6877e6d2020-09-25 19:59:14 +01001465 // Output Javadoc comments for public scope.
1466 if apiScope == apiScopePublic {
1467 props.Output_javadoc_comments = proptools.BoolPtr(true)
1468 }
1469
Paul Duffin1fb487d2020-04-07 18:50:10 +01001470 // Add in scope specific arguments.
Paul Duffin0ff08bd2020-04-29 13:30:54 +01001471 droidstubsArgs = append(droidstubsArgs, scopeSpecificDroidstubsArgs...)
Paul Duffin11512472019-02-11 15:55:17 +00001472 props.Arg_files = module.sdkLibraryProperties.Droiddoc_option_files
Paul Duffin6d0886e2020-04-07 18:49:53 +01001473 props.Args = proptools.StringPtr(strings.Join(droidstubsArgs, " "))
Jiyong Parkc678ad32018-04-10 13:07:10 +09001474
Paul Duffin15f34ef2020-07-20 18:04:44 +01001475 // List of APIs identified from the provided source files are created. They are later
1476 // compared against to the not-yet-released (a.k.a current) list of APIs and to the
1477 // last-released (a.k.a numbered) list of API.
1478 currentApiFileName := apiScope.apiFilePrefix + "current.txt"
1479 removedApiFileName := apiScope.apiFilePrefix + "removed.txt"
1480 apiDir := module.getApiDir()
1481 currentApiFileName = path.Join(apiDir, currentApiFileName)
1482 removedApiFileName = path.Join(apiDir, removedApiFileName)
Jiyong Parkc678ad32018-04-10 13:07:10 +09001483
Paul Duffin15f34ef2020-07-20 18:04:44 +01001484 // check against the not-yet-release API
1485 props.Check_api.Current.Api_file = proptools.StringPtr(currentApiFileName)
1486 props.Check_api.Current.Removed_api_file = proptools.StringPtr(removedApiFileName)
Jiyong Park58c518b2018-05-12 22:29:12 +09001487
Anton Hanssone6056152020-12-31 10:37:27 +00001488 if !(apiScope.unstable || module.sdkLibraryProperties.Unsafe_ignore_missing_latest_api) {
Paul Duffin15f34ef2020-07-20 18:04:44 +01001489 // check against the latest released API
1490 latestApiFilegroupName := proptools.StringPtr(module.latestApiFilegroupName(apiScope))
Anton Hanssone87b03d2020-12-21 15:29:34 +00001491 props.Previous_api = latestApiFilegroupName
Paul Duffin15f34ef2020-07-20 18:04:44 +01001492 props.Check_api.Last_released.Api_file = latestApiFilegroupName
1493 props.Check_api.Last_released.Removed_api_file = proptools.StringPtr(
1494 module.latestRemovedApiFilegroupName(apiScope))
Jaewoong Jung1a97ee02021-03-09 13:25:02 -08001495 props.Check_api.Last_released.Baseline_file = proptools.StringPtr(
1496 module.latestIncompatibilitiesFilegroupName(apiScope))
Paul Duffin160fe412020-05-10 19:32:20 +01001497
Paul Duffin15f34ef2020-07-20 18:04:44 +01001498 if proptools.Bool(module.sdkLibraryProperties.Api_lint.Enabled) {
1499 // Enable api lint.
1500 props.Check_api.Api_lint.Enabled = proptools.BoolPtr(true)
1501 props.Check_api.Api_lint.New_since = latestApiFilegroupName
Paul Duffin160fe412020-05-10 19:32:20 +01001502
Paul Duffin15f34ef2020-07-20 18:04:44 +01001503 // If it exists then pass a lint-baseline.txt through to droidstubs.
1504 baselinePath := path.Join(apiDir, apiScope.apiFilePrefix+"lint-baseline.txt")
1505 baselinePathRelativeToRoot := path.Join(mctx.ModuleDir(), baselinePath)
1506 paths, err := mctx.GlobWithDeps(baselinePathRelativeToRoot, nil)
1507 if err != nil {
1508 mctx.ModuleErrorf("error checking for presence of %s: %s", baselinePathRelativeToRoot, err)
1509 }
1510 if len(paths) == 1 {
1511 props.Check_api.Api_lint.Baseline_file = proptools.StringPtr(baselinePath)
1512 } else if len(paths) != 0 {
1513 mctx.ModuleErrorf("error checking for presence of %s: expected one path, found: %v", baselinePathRelativeToRoot, paths)
Paul Duffin160fe412020-05-10 19:32:20 +01001514 }
1515 }
Paul Duffin15f34ef2020-07-20 18:04:44 +01001516 }
Jiyong Park58c518b2018-05-12 22:29:12 +09001517
Paul Duffin15f34ef2020-07-20 18:04:44 +01001518 if !Bool(module.sdkLibraryProperties.No_dist) {
Paul Duffin040e9062020-11-23 17:41:36 +00001519 // Dist the api txt and removed api txt artifacts for sdk builds.
1520 distDir := proptools.StringPtr(path.Join(module.apiDistPath(apiScope), "api"))
1521 for _, p := range []struct {
1522 tag string
1523 pattern string
1524 }{
1525 {tag: ".api.txt", pattern: "%s.txt"},
1526 {tag: ".removed-api.txt", pattern: "%s-removed.txt"},
1527 } {
1528 props.Dists = append(props.Dists, android.Dist{
1529 Targets: []string{"sdk", "win_sdk"},
1530 Dir: distDir,
1531 Dest: proptools.StringPtr(fmt.Sprintf(p.pattern, module.distStem())),
1532 Tag: proptools.StringPtr(p.tag),
1533 })
1534 }
Anton Hansson5fd5d242020-03-27 19:43:19 +00001535 }
1536
Colin Cross84dfc3d2019-09-25 11:33:01 -07001537 mctx.CreateModule(DroidstubsFactory, &props)
Jiyong Parkc678ad32018-04-10 13:07:10 +09001538}
1539
Paul Duffinea8f8082021-06-24 13:25:57 +01001540// Implements android.ApexModule
Jooyung Han5e9013b2020-03-10 06:23:13 +09001541func (module *SdkLibrary) DepIsInSameApex(mctx android.BaseModuleContext, dep android.Module) bool {
1542 depTag := mctx.OtherModuleDependencyTag(dep)
1543 if depTag == xmlPermissionsFileTag {
1544 return true
1545 }
1546 return module.Library.DepIsInSameApex(mctx, dep)
1547}
1548
Paul Duffinea8f8082021-06-24 13:25:57 +01001549// Implements android.ApexModule
1550func (module *SdkLibrary) UniqueApexVariations() bool {
1551 return module.uniqueApexVariations()
1552}
1553
Jiyong Parkc678ad32018-04-10 13:07:10 +09001554// Creates the xml file that publicizes the runtime library
Paul Duffinf0229202020-04-29 16:47:28 +01001555func (module *SdkLibrary) createXmlFile(mctx android.DefaultableHookContext) {
Jiyong Parke3833882020-02-17 17:28:10 +09001556 props := struct {
Paul Duffin1dbe3ca2020-05-16 09:57:59 +01001557 Name *string
1558 Lib_name *string
1559 Apex_available []string
Jiyong Parke3833882020-02-17 17:28:10 +09001560 }{
Paul Duffineedc5d52020-06-12 17:46:39 +01001561 Name: proptools.StringPtr(module.xmlPermissionsModuleName()),
Jooyung Han5e9013b2020-03-10 06:23:13 +09001562 Lib_name: proptools.StringPtr(module.BaseModuleName()),
1563 Apex_available: module.ApexProperties.Apex_available,
Jiyong Parkc678ad32018-04-10 13:07:10 +09001564 }
Jiyong Parke3833882020-02-17 17:28:10 +09001565
Jiyong Parke3833882020-02-17 17:28:10 +09001566 mctx.CreateModule(sdkLibraryXmlFactory, &props)
Jiyong Parkc678ad32018-04-10 13:07:10 +09001567}
1568
Jiyong Parkf1691d22021-03-29 20:11:58 +09001569func PrebuiltJars(ctx android.BaseModuleContext, baseName string, s android.SdkSpec) android.Paths {
Jiyong Park54105c42021-03-31 18:17:53 +09001570 var ver android.ApiLevel
Jiyong Parkf1691d22021-03-29 20:11:58 +09001571 var kind android.SdkKind
1572 if s.UsePrebuilt(ctx) {
Jiyong Park54105c42021-03-31 18:17:53 +09001573 ver = s.ApiLevel
Jiyong Parkf1691d22021-03-29 20:11:58 +09001574 kind = s.Kind
Jiyong Parkc678ad32018-04-10 13:07:10 +09001575 } else {
Jiyong Park6a927c42020-01-21 02:03:43 +09001576 // We don't have prebuilt SDK for the specific sdkVersion.
1577 // Instead of breaking the build, fallback to use "system_current"
Jiyong Park54105c42021-03-31 18:17:53 +09001578 ver = android.FutureApiLevel
Jiyong Parkf1691d22021-03-29 20:11:58 +09001579 kind = android.SdkSystem
Sundong Ahn054b19a2018-10-19 13:46:09 +09001580 }
Jiyong Park6a927c42020-01-21 02:03:43 +09001581
1582 dir := filepath.Join("prebuilts", "sdk", ver.String(), kind.String())
Paul Duffin50061512020-01-21 16:31:05 +00001583 jar := filepath.Join(dir, baseName+".jar")
Sundong Ahn054b19a2018-10-19 13:46:09 +09001584 jarPath := android.ExistentPathForSource(ctx, jar)
Sundong Ahnae418ac2019-02-28 15:01:28 +09001585 if !jarPath.Valid() {
Colin Cross07c88562020-01-07 09:34:44 -08001586 if ctx.Config().AllowMissingDependencies() {
1587 return android.Paths{android.PathForSource(ctx, jar)}
1588 } else {
Jiyong Parkf1691d22021-03-29 20:11:58 +09001589 ctx.PropertyErrorf("sdk_library", "invalid sdk version %q, %q does not exist", s.Raw, jar)
Colin Cross07c88562020-01-07 09:34:44 -08001590 }
Sundong Ahnae418ac2019-02-28 15:01:28 +09001591 return nil
1592 }
Sundong Ahn054b19a2018-10-19 13:46:09 +09001593 return android.Paths{jarPath.Path()}
1594}
1595
Colin Crossaede88c2020-08-11 12:17:01 -07001596// 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 +01001597//
1598// If either this or the other module are on the platform then this will return
1599// false.
Colin Cross56a83212020-09-15 18:30:11 -07001600func withinSameApexesAs(ctx android.BaseModuleContext, other android.Module) bool {
1601 apexInfo := ctx.Provider(android.ApexInfoProvider).(android.ApexInfo)
1602 otherApexInfo := ctx.OtherModuleProvider(other, android.ApexInfoProvider).(android.ApexInfo)
Jiyong Parkab50b072021-05-12 17:13:56 +09001603 return len(otherApexInfo.InApexVariants) > 0 && reflect.DeepEqual(apexInfo.InApexVariants, otherApexInfo.InApexVariants)
Paul Duffin9b879592020-05-26 13:21:35 +01001604}
1605
Jiyong Parkf1691d22021-03-29 20:11:58 +09001606func (module *SdkLibrary) sdkJars(ctx android.BaseModuleContext, sdkVersion android.SdkSpec, headerJars bool) android.Paths {
Jiyong Park932cdfe2020-05-28 00:19:53 +09001607 // If the client doesn't set sdk_version, but if this library prefers stubs over
1608 // the impl library, let's provide the widest API surface possible. To do so,
1609 // force override sdk_version to module_current so that the closest possible API
1610 // surface could be found in selectHeaderJarsForSdkVersion
Jiyong Parkf1691d22021-03-29 20:11:58 +09001611 if module.defaultsToStubs() && !sdkVersion.Specified() {
Jiyong Park92315372021-04-02 08:45:46 +09001612 sdkVersion = android.SdkSpecFrom(ctx, "module_current")
Jiyong Park932cdfe2020-05-28 00:19:53 +09001613 }
Paul Duffind1b3a922020-01-22 11:57:20 +00001614
Paul Duffindaaa3322020-05-26 18:13:57 +01001615 // Only provide access to the implementation library if it is actually built.
1616 if module.requiresRuntimeImplementationLibrary() {
1617 // Check any special cases for java_sdk_library.
1618 //
1619 // Only allow access to the implementation library in the following condition:
1620 // * No sdk_version specified on the referencing module.
Paul Duffin9b879592020-05-26 13:21:35 +01001621 // * The referencing module is in the same apex as this.
Jiyong Parkf1691d22021-03-29 20:11:58 +09001622 if sdkVersion.Kind == android.SdkPrivate || withinSameApexesAs(ctx, module) {
Paul Duffindaaa3322020-05-26 18:13:57 +01001623 if headerJars {
1624 return module.HeaderJars()
1625 } else {
1626 return module.ImplementationJars()
1627 }
Sundong Ahn054b19a2018-10-19 13:46:09 +09001628 }
Jiyong Parkc678ad32018-04-10 13:07:10 +09001629 }
Paul Duffinb05d4292020-05-20 12:19:10 +01001630
Paul Duffin23970f42020-05-20 14:20:02 +01001631 return module.selectHeaderJarsForSdkVersion(ctx, sdkVersion)
Jiyong Parkc678ad32018-04-10 13:07:10 +09001632}
1633
Sundong Ahn241cd372018-07-13 16:16:44 +09001634// to satisfy SdkLibraryDependency interface
Jiyong Parkf1691d22021-03-29 20:11:58 +09001635func (module *SdkLibrary) SdkHeaderJars(ctx android.BaseModuleContext, sdkVersion android.SdkSpec) android.Paths {
Paul Duffind1b3a922020-01-22 11:57:20 +00001636 return module.sdkJars(ctx, sdkVersion, true /*headerJars*/)
1637}
1638
1639// to satisfy SdkLibraryDependency interface
Jiyong Parkf1691d22021-03-29 20:11:58 +09001640func (module *SdkLibrary) SdkImplementationJars(ctx android.BaseModuleContext, sdkVersion android.SdkSpec) android.Paths {
Paul Duffind1b3a922020-01-22 11:57:20 +00001641 return module.sdkJars(ctx, sdkVersion, false /*headerJars*/)
Sundong Ahn241cd372018-07-13 16:16:44 +09001642}
1643
Colin Cross571cccf2019-02-04 11:22:08 -08001644var javaSdkLibrariesKey = android.NewOnceKey("javaSdkLibraries")
1645
Jiyong Park82484c02018-04-23 21:41:26 +09001646func javaSdkLibraries(config android.Config) *[]string {
Colin Cross571cccf2019-02-04 11:22:08 -08001647 return config.Once(javaSdkLibrariesKey, func() interface{} {
Jiyong Park82484c02018-04-23 21:41:26 +09001648 return &[]string{}
1649 }).(*[]string)
1650}
1651
Paul Duffin749f98f2019-12-30 17:23:46 +00001652func (module *SdkLibrary) getApiDir() string {
1653 return proptools.StringDefault(module.sdkLibraryProperties.Api_dir, "api")
1654}
1655
Jiyong Parkc678ad32018-04-10 13:07:10 +09001656// For a java_sdk_library module, create internal modules for stubs, docs,
1657// runtime libs and xml file. If requested, the stubs and docs are created twice
1658// once for public API level and once for system API level
Paul Duffinf0229202020-04-29 16:47:28 +01001659func (module *SdkLibrary) CreateInternalModules(mctx android.DefaultableHookContext) {
1660 // If the module has been disabled then don't create any child modules.
1661 if !module.Enabled() {
1662 return
1663 }
1664
Paul Duffina18abc22020-05-16 18:54:24 +01001665 if len(module.properties.Srcs) == 0 {
Inseob Kimc0907f12019-02-08 21:00:45 +09001666 mctx.PropertyErrorf("srcs", "java_sdk_library must specify srcs")
Jooyung Han58f26ab2019-12-18 15:34:32 +09001667 return
Inseob Kimc0907f12019-02-08 21:00:45 +09001668 }
1669
Paul Duffin37e0b772019-12-30 17:20:10 +00001670 // If this builds against standard libraries (i.e. is not part of the core libraries)
Paul Duffin4f5c1ef2020-11-19 14:53:43 +00001671 // then assume it provides both system and test apis.
Jiyong Parkf1691d22021-03-29 20:11:58 +09001672 sdkDep := decodeSdkDep(mctx, android.SdkContext(&module.Library))
Paul Duffin37e0b772019-12-30 17:20:10 +00001673 hasSystemAndTestApis := sdkDep.hasStandardLibs()
Paul Duffin3375e352020-04-28 10:44:03 +01001674 module.sdkLibraryProperties.Generate_system_and_test_apis = hasSystemAndTestApis
Paul Duffin4f5c1ef2020-11-19 14:53:43 +00001675
Jaewoong Jung18aefc12020-12-21 09:11:10 -08001676 missingCurrentApi := false
Inseob Kim8098faa2019-03-18 10:19:51 +09001677
Paul Duffin3375e352020-04-28 10:44:03 +01001678 generatedScopes := module.getGeneratedApiScopes(mctx)
Paul Duffind1b3a922020-01-22 11:57:20 +00001679
Paul Duffin749f98f2019-12-30 17:23:46 +00001680 apiDir := module.getApiDir()
Paul Duffin3375e352020-04-28 10:44:03 +01001681 for _, scope := range generatedScopes {
Inseob Kim8098faa2019-03-18 10:19:51 +09001682 for _, api := range []string{"current.txt", "removed.txt"} {
Paul Duffind1b3a922020-01-22 11:57:20 +00001683 path := path.Join(mctx.ModuleDir(), apiDir, scope.apiFilePrefix+api)
Inseob Kim8098faa2019-03-18 10:19:51 +09001684 p := android.ExistentPathForSource(mctx, path)
1685 if !p.Valid() {
Colin Cross18f840c2021-05-20 17:56:54 -07001686 if mctx.Config().AllowMissingDependencies() {
1687 mctx.AddMissingDependencies([]string{path})
1688 } else {
1689 mctx.ModuleErrorf("Current api file %#v doesn't exist", path)
1690 missingCurrentApi = true
1691 }
Inseob Kim8098faa2019-03-18 10:19:51 +09001692 }
1693 }
1694 }
1695
Jaewoong Jung18aefc12020-12-21 09:11:10 -08001696 if missingCurrentApi {
Inseob Kim8098faa2019-03-18 10:19:51 +09001697 script := "build/soong/scripts/gen-java-current-api-files.sh"
1698 p := android.ExistentPathForSource(mctx, script)
1699
1700 if !p.Valid() {
1701 panic(fmt.Sprintf("script file %s doesn't exist", script))
1702 }
1703
1704 mctx.ModuleErrorf("One or more current api files are missing. "+
1705 "You can update them by:\n"+
Paul Duffin37e0b772019-12-30 17:20:10 +00001706 "%s %q %s && m update-api",
Paul Duffind1b3a922020-01-22 11:57:20 +00001707 script, filepath.Join(mctx.ModuleDir(), apiDir),
Paul Duffin3375e352020-04-28 10:44:03 +01001708 strings.Join(generatedScopes.Strings(func(s *apiScope) string { return s.apiFilePrefix }), " "))
Inseob Kim8098faa2019-03-18 10:19:51 +09001709 return
1710 }
1711
Paul Duffin3375e352020-04-28 10:44:03 +01001712 for _, scope := range generatedScopes {
Paul Duffin15f34ef2020-07-20 18:04:44 +01001713 // Use the stubs source name for legacy reasons.
1714 module.createStubsSourcesAndApi(mctx, scope, module.stubsSourceModuleName(scope), scope.droidstubsArgs)
Paul Duffin0ff08bd2020-04-29 13:30:54 +01001715
Paul Duffind1b3a922020-01-22 11:57:20 +00001716 module.createStubsLibrary(mctx, scope)
Inseob Kimc0907f12019-02-08 21:00:45 +09001717 }
1718
Paul Duffindfa131e2020-05-15 20:37:11 +01001719 if module.requiresRuntimeImplementationLibrary() {
Paul Duffin5df79302020-05-16 15:52:12 +01001720 // Create child module to create an implementation library.
1721 //
1722 // This temporarily creates a second implementation library that can be explicitly
1723 // referenced.
1724 //
1725 // TODO(b/156618935) - update comment once only one implementation library is created.
1726 module.createImplLibrary(mctx)
1727
Paul Duffindfa131e2020-05-15 20:37:11 +01001728 // Only create an XML permissions file that declares the library as being usable
1729 // as a shared library if required.
1730 if module.sharedLibrary() {
1731 module.createXmlFile(mctx)
1732 }
Paul Duffin43db9be2019-12-30 17:35:49 +00001733
1734 // record java_sdk_library modules so that they are exported to make
1735 javaSdkLibraries := javaSdkLibraries(mctx.Config())
1736 javaSdkLibrariesLock.Lock()
1737 defer javaSdkLibrariesLock.Unlock()
1738 *javaSdkLibraries = append(*javaSdkLibraries, module.BaseModuleName())
1739 }
Anton Hansson7f66efa2020-10-08 14:47:23 +01001740
1741 // Add the impl_only_libs *after* we're done using the Libs prop in submodules.
1742 module.properties.Libs = append(module.properties.Libs, module.sdkLibraryProperties.Impl_only_libs...)
Inseob Kimc0907f12019-02-08 21:00:45 +09001743}
1744
1745func (module *SdkLibrary) InitSdkLibraryProperties() {
Colin Crossce6734e2020-06-15 16:09:53 -07001746 module.addHostAndDeviceProperties()
1747 module.AddProperties(&module.sdkLibraryProperties)
Sundong Ahn054b19a2018-10-19 13:46:09 +09001748
Paul Duffin71b33cc2021-06-23 11:39:47 +01001749 module.initSdkLibraryComponent(module)
Paul Duffin859fe962020-05-15 10:20:31 +01001750
Paul Duffina18abc22020-05-16 18:54:24 +01001751 module.properties.Installable = proptools.BoolPtr(true)
1752 module.deviceProperties.IsSDKLibrary = true
Inseob Kimc0907f12019-02-08 21:00:45 +09001753}
Sundong Ahn054b19a2018-10-19 13:46:09 +09001754
Paul Duffindfa131e2020-05-15 20:37:11 +01001755func (module *SdkLibrary) requiresRuntimeImplementationLibrary() bool {
1756 return !proptools.Bool(module.sdkLibraryProperties.Api_only)
1757}
1758
Jiyong Park932cdfe2020-05-28 00:19:53 +09001759func (module *SdkLibrary) defaultsToStubs() bool {
1760 return proptools.Bool(module.sdkLibraryProperties.Default_to_stubs)
1761}
1762
Paul Duffin1b1e8062020-05-08 13:44:43 +01001763// Defines how to name the individual component modules the sdk library creates.
1764type sdkLibraryComponentNamingScheme interface {
1765 stubsLibraryModuleName(scope *apiScope, baseName string) string
1766
1767 stubsSourceModuleName(scope *apiScope, baseName string) string
Paul Duffin1b1e8062020-05-08 13:44:43 +01001768}
1769
1770type defaultNamingScheme struct {
1771}
1772
1773func (s *defaultNamingScheme) stubsLibraryModuleName(scope *apiScope, baseName string) string {
1774 return scope.stubsLibraryModuleName(baseName)
1775}
1776
1777func (s *defaultNamingScheme) stubsSourceModuleName(scope *apiScope, baseName string) string {
1778 return scope.stubsSourceModuleName(baseName)
1779}
1780
Paul Duffin1b1e8062020-05-08 13:44:43 +01001781var _ sdkLibraryComponentNamingScheme = (*defaultNamingScheme)(nil)
1782
Jaewoong Jungbc15e3a2021-03-10 17:02:43 -08001783func moduleStubLinkType(name string) (stub bool, ret sdkLinkType) {
Anton Hansson2d0c1942020-05-25 12:20:51 +01001784 // This suffix-based approach is fragile and could potentially mis-trigger.
1785 // TODO(b/155164730): Clean this up when modules no longer reference sdk_lib stubs directly.
Anton Hansson08f476b2021-04-07 15:32:19 +01001786 if strings.HasSuffix(name, apiScopePublic.stubsLibraryModuleNameSuffix()) {
1787 if name == "hwbinder.stubs" || name == "libcore_private.stubs" {
1788 // Due to a previous bug, these modules were not considered stubs, so we retain that.
1789 return false, javaPlatform
1790 }
Anton Hansson2d0c1942020-05-25 12:20:51 +01001791 return true, javaSdk
1792 }
Anton Hansson08f476b2021-04-07 15:32:19 +01001793 if strings.HasSuffix(name, apiScopeSystem.stubsLibraryModuleNameSuffix()) {
Anton Hansson2d0c1942020-05-25 12:20:51 +01001794 return true, javaSystem
1795 }
Anton Hansson08f476b2021-04-07 15:32:19 +01001796 if strings.HasSuffix(name, apiScopeModuleLib.stubsLibraryModuleNameSuffix()) {
Anton Hansson2d0c1942020-05-25 12:20:51 +01001797 return true, javaModule
1798 }
Anton Hansson08f476b2021-04-07 15:32:19 +01001799 if strings.HasSuffix(name, apiScopeTest.stubsLibraryModuleNameSuffix()) {
Anton Hansson2d0c1942020-05-25 12:20:51 +01001800 return true, javaSystem
1801 }
1802 return false, javaPlatform
1803}
1804
Jaewoong Jung4f158ee2019-07-11 10:05:35 -07001805// java_sdk_library is a special Java library that provides optional platform APIs to apps.
1806// In practice, it can be viewed as a combination of several modules: 1) stubs library that clients
1807// are linked against to, 2) droiddoc module that internally generates API stubs source files,
1808// 3) the real runtime shared library that implements the APIs, and 4) XML file for adding
1809// the runtime lib to the classpath at runtime if requested via <uses-library>.
Inseob Kimc0907f12019-02-08 21:00:45 +09001810func SdkLibraryFactory() android.Module {
1811 module := &SdkLibrary{}
Paul Duffinc3091c82020-05-08 14:16:20 +01001812
1813 // Initialize information common between source and prebuilt.
Paul Duffin71b33cc2021-06-23 11:39:47 +01001814 module.initCommon(module)
Paul Duffinc3091c82020-05-08 14:16:20 +01001815
Inseob Kimc0907f12019-02-08 21:00:45 +09001816 module.InitSdkLibraryProperties()
Jooyung Han58f26ab2019-12-18 15:34:32 +09001817 android.InitApexModule(module)
Paul Duffinb6b89a42021-05-06 16:33:43 +01001818 android.InitSdkAwareModule(module)
Sundong Ahn054b19a2018-10-19 13:46:09 +09001819 InitJavaModule(module, android.HostAndDeviceSupported)
Paul Duffin3375e352020-04-28 10:44:03 +01001820
1821 // Initialize the map from scope to scope specific properties.
1822 scopeToProperties := make(map[*apiScope]*ApiScopeProperties)
1823 for _, scope := range allApiScopes {
1824 scopeToProperties[scope] = scope.scopeSpecificProperties(module)
1825 }
1826 module.scopeToProperties = scopeToProperties
1827
Paul Duffin4911a892020-04-29 23:35:13 +01001828 // Add the properties containing visibility rules so that they are checked.
Paul Duffin5df79302020-05-16 15:52:12 +01001829 android.AddVisibilityProperty(module, "impl_library_visibility", &module.sdkLibraryProperties.Impl_library_visibility)
Paul Duffin4911a892020-04-29 23:35:13 +01001830 android.AddVisibilityProperty(module, "stubs_library_visibility", &module.sdkLibraryProperties.Stubs_library_visibility)
1831 android.AddVisibilityProperty(module, "stubs_source_visibility", &module.sdkLibraryProperties.Stubs_source_visibility)
1832
Paul Duffin1b1e8062020-05-08 13:44:43 +01001833 module.SetDefaultableHook(func(ctx android.DefaultableHookContext) {
Paul Duffindfa131e2020-05-15 20:37:11 +01001834 // If no implementation is required then it cannot be used as a shared library
1835 // either.
1836 if !module.requiresRuntimeImplementationLibrary() {
1837 // If shared_library has been explicitly set to true then it is incompatible
1838 // with api_only: true.
1839 if proptools.Bool(module.commonSdkLibraryProperties.Shared_library) {
1840 ctx.PropertyErrorf("api_only/shared_library", "inconsistent settings, shared_library and api_only cannot both be true")
1841 }
1842 // Set shared_library: false.
1843 module.commonSdkLibraryProperties.Shared_library = proptools.BoolPtr(false)
1844 }
1845
Paul Duffin1b1e8062020-05-08 13:44:43 +01001846 if module.initCommonAfterDefaultsApplied(ctx) {
1847 module.CreateInternalModules(ctx)
1848 }
1849 })
Jiyong Parkc678ad32018-04-10 13:07:10 +09001850 return module
1851}
Colin Cross79c7c262019-04-17 11:11:46 -07001852
1853//
1854// SDK library prebuilts
1855//
1856
Paul Duffin56d44902020-01-31 13:36:25 +00001857// Properties associated with each api scope.
1858type sdkLibraryScopeProperties struct {
Colin Cross79c7c262019-04-17 11:11:46 -07001859 Jars []string `android:"path"`
1860
1861 Sdk_version *string
1862
Colin Cross79c7c262019-04-17 11:11:46 -07001863 // List of shared java libs that this module has dependencies to
1864 Libs []string
Paul Duffin3d1248c2020-04-09 00:10:17 +01001865
Paul Duffinc8782502020-04-29 20:45:27 +01001866 // The stubs source.
Paul Duffin3d1248c2020-04-09 00:10:17 +01001867 Stub_srcs []string `android:"path"`
Paul Duffin1fd005d2020-04-09 01:08:11 +01001868
1869 // The current.txt
Paul Duffin0f8faff2020-05-20 16:18:00 +01001870 Current_api *string `android:"path"`
Paul Duffin1fd005d2020-04-09 01:08:11 +01001871
1872 // The removed.txt
Paul Duffin0f8faff2020-05-20 16:18:00 +01001873 Removed_api *string `android:"path"`
Colin Cross79c7c262019-04-17 11:11:46 -07001874}
1875
Paul Duffin56d44902020-01-31 13:36:25 +00001876type sdkLibraryImportProperties struct {
Paul Duffinfcfd7912020-01-31 17:54:30 +00001877 // List of shared java libs, common to all scopes, that this module has
1878 // dependencies to
1879 Libs []string
Paul Duffin1267d872021-04-16 17:21:36 +01001880
1881 // If set to true, compile dex files for the stubs. Defaults to false.
1882 Compile_dex *bool
Paul Duffin56d44902020-01-31 13:36:25 +00001883}
1884
Paul Duffineedc5d52020-06-12 17:46:39 +01001885type SdkLibraryImport struct {
Colin Cross79c7c262019-04-17 11:11:46 -07001886 android.ModuleBase
1887 android.DefaultableModuleBase
1888 prebuilt android.Prebuilt
Paul Duffindd46f712020-02-10 13:37:10 +00001889 android.ApexModuleBase
1890 android.SdkBase
Colin Cross79c7c262019-04-17 11:11:46 -07001891
Paul Duffin37856732021-02-26 14:24:15 +00001892 hiddenAPI
1893
Colin Cross79c7c262019-04-17 11:11:46 -07001894 properties sdkLibraryImportProperties
1895
Paul Duffin46a26a82020-04-07 19:27:04 +01001896 // Map from api scope to the scope specific property structure.
1897 scopeProperties map[*apiScope]*sdkLibraryScopeProperties
1898
Paul Duffin56d44902020-01-31 13:36:25 +00001899 commonToSdkLibraryAndImport
Paul Duffineedc5d52020-06-12 17:46:39 +01001900
1901 // The reference to the implementation library created by the source module.
1902 // Is nil if the source module does not exist.
1903 implLibraryModule *Library
1904
1905 // The reference to the xml permissions module created by the source module.
1906 // Is nil if the source module does not exist.
1907 xmlPermissionsFileModule *sdkLibraryXml
Paul Duffin39853512021-02-26 11:09:39 +00001908
1909 // Path to the dex implementation jar obtained from the prebuilt_apex, if any.
1910 dexJarFile android.Path
Colin Cross79c7c262019-04-17 11:11:46 -07001911}
1912
Paul Duffineedc5d52020-06-12 17:46:39 +01001913var _ SdkLibraryDependency = (*SdkLibraryImport)(nil)
Colin Cross79c7c262019-04-17 11:11:46 -07001914
Paul Duffin46a26a82020-04-07 19:27:04 +01001915// The type of a structure that contains a field of type sdkLibraryScopeProperties
1916// for each apiscope in allApiScopes, e.g. something like:
1917// struct {
1918// Public sdkLibraryScopeProperties
1919// System sdkLibraryScopeProperties
1920// ...
1921// }
1922var allScopeStructType = createAllScopePropertiesStructType()
1923
1924// Dynamically create a structure type for each apiscope in allApiScopes.
1925func createAllScopePropertiesStructType() reflect.Type {
1926 var fields []reflect.StructField
1927 for _, apiScope := range allApiScopes {
1928 field := reflect.StructField{
1929 Name: apiScope.fieldName,
1930 Type: reflect.TypeOf(sdkLibraryScopeProperties{}),
1931 }
1932 fields = append(fields, field)
1933 }
1934
1935 return reflect.StructOf(fields)
1936}
1937
1938// Create an instance of the scope specific structure type and return a map
1939// from apiscope to a pointer to each scope specific field.
1940func createPropertiesInstance() (interface{}, map[*apiScope]*sdkLibraryScopeProperties) {
1941 allScopePropertiesPtr := reflect.New(allScopeStructType)
1942 allScopePropertiesStruct := allScopePropertiesPtr.Elem()
1943 scopeProperties := make(map[*apiScope]*sdkLibraryScopeProperties)
1944
1945 for _, apiScope := range allApiScopes {
1946 field := allScopePropertiesStruct.FieldByName(apiScope.fieldName)
1947 scopeProperties[apiScope] = field.Addr().Interface().(*sdkLibraryScopeProperties)
1948 }
1949
1950 return allScopePropertiesPtr.Interface(), scopeProperties
1951}
1952
Jaewoong Jung4f158ee2019-07-11 10:05:35 -07001953// java_sdk_library_import imports a prebuilt java_sdk_library.
Colin Cross79c7c262019-04-17 11:11:46 -07001954func sdkLibraryImportFactory() android.Module {
Paul Duffineedc5d52020-06-12 17:46:39 +01001955 module := &SdkLibraryImport{}
Colin Cross79c7c262019-04-17 11:11:46 -07001956
Paul Duffin46a26a82020-04-07 19:27:04 +01001957 allScopeProperties, scopeToProperties := createPropertiesInstance()
1958 module.scopeProperties = scopeToProperties
1959 module.AddProperties(&module.properties, allScopeProperties)
Colin Cross79c7c262019-04-17 11:11:46 -07001960
Paul Duffinc3091c82020-05-08 14:16:20 +01001961 // Initialize information common between source and prebuilt.
Paul Duffin71b33cc2021-06-23 11:39:47 +01001962 module.initCommon(module)
Paul Duffinc3091c82020-05-08 14:16:20 +01001963
Paul Duffin0bdcb272020-02-06 15:24:57 +00001964 android.InitPrebuiltModule(module, &[]string{""})
Paul Duffindd46f712020-02-10 13:37:10 +00001965 android.InitApexModule(module)
1966 android.InitSdkAwareModule(module)
Colin Cross79c7c262019-04-17 11:11:46 -07001967 InitJavaModule(module, android.HostAndDeviceSupported)
1968
Paul Duffin1b1e8062020-05-08 13:44:43 +01001969 module.SetDefaultableHook(func(mctx android.DefaultableHookContext) {
1970 if module.initCommonAfterDefaultsApplied(mctx) {
1971 module.createInternalModules(mctx)
1972 }
1973 })
Colin Cross79c7c262019-04-17 11:11:46 -07001974 return module
1975}
1976
Paul Duffineedc5d52020-06-12 17:46:39 +01001977func (module *SdkLibraryImport) Prebuilt() *android.Prebuilt {
Colin Cross79c7c262019-04-17 11:11:46 -07001978 return &module.prebuilt
1979}
1980
Paul Duffineedc5d52020-06-12 17:46:39 +01001981func (module *SdkLibraryImport) Name() string {
Colin Cross79c7c262019-04-17 11:11:46 -07001982 return module.prebuilt.Name(module.ModuleBase.Name())
1983}
1984
Paul Duffineedc5d52020-06-12 17:46:39 +01001985func (module *SdkLibraryImport) createInternalModules(mctx android.DefaultableHookContext) {
Colin Cross79c7c262019-04-17 11:11:46 -07001986
Paul Duffin50061512020-01-21 16:31:05 +00001987 // If the build is configured to use prebuilts then force this to be preferred.
Jeongik Cha816a23a2020-07-08 01:09:23 +09001988 if mctx.Config().AlwaysUsePrebuiltSdks() {
Paul Duffin50061512020-01-21 16:31:05 +00001989 module.prebuilt.ForcePrefer()
1990 }
1991
Paul Duffin46a26a82020-04-07 19:27:04 +01001992 for apiScope, scopeProperties := range module.scopeProperties {
Paul Duffin56d44902020-01-31 13:36:25 +00001993 if len(scopeProperties.Jars) == 0 {
1994 continue
1995 }
1996
Paul Duffinbbb546b2020-04-09 00:07:11 +01001997 module.createJavaImportForStubs(mctx, apiScope, scopeProperties)
Paul Duffin3d1248c2020-04-09 00:10:17 +01001998
Paul Duffin0f8faff2020-05-20 16:18:00 +01001999 if len(scopeProperties.Stub_srcs) > 0 {
2000 module.createPrebuiltStubsSources(mctx, apiScope, scopeProperties)
2001 }
Paul Duffin56d44902020-01-31 13:36:25 +00002002 }
Colin Cross79c7c262019-04-17 11:11:46 -07002003
2004 javaSdkLibraries := javaSdkLibraries(mctx.Config())
2005 javaSdkLibrariesLock.Lock()
2006 defer javaSdkLibrariesLock.Unlock()
2007 *javaSdkLibraries = append(*javaSdkLibraries, module.BaseModuleName())
2008}
2009
Paul Duffineedc5d52020-06-12 17:46:39 +01002010func (module *SdkLibraryImport) createJavaImportForStubs(mctx android.DefaultableHookContext, apiScope *apiScope, scopeProperties *sdkLibraryScopeProperties) {
Paul Duffinbbb546b2020-04-09 00:07:11 +01002011 // Creates a java import for the jar with ".stubs" suffix
2012 props := struct {
Paul Duffin1dbe3ca2020-05-16 09:57:59 +01002013 Name *string
2014 Sdk_version *string
2015 Libs []string
2016 Jars []string
2017 Prefer *bool
Paul Duffin1267d872021-04-16 17:21:36 +01002018 Compile_dex *bool
Paul Duffinbbb546b2020-04-09 00:07:11 +01002019 }{}
Paul Duffinc3091c82020-05-08 14:16:20 +01002020 props.Name = proptools.StringPtr(module.stubsLibraryModuleName(apiScope))
Paul Duffinbbb546b2020-04-09 00:07:11 +01002021 props.Sdk_version = scopeProperties.Sdk_version
2022 // Prepend any of the libs from the legacy public properties to the libs for each of the
2023 // scopes to avoid having to duplicate them in each scope.
2024 props.Libs = append(module.properties.Libs, scopeProperties.Libs...)
2025 props.Jars = scopeProperties.Jars
Paul Duffin1dbe3ca2020-05-16 09:57:59 +01002026
Paul Duffin38b57852020-05-13 16:08:09 +01002027 // The imports are preferred if the java_sdk_library_import is preferred.
2028 props.Prefer = proptools.BoolPtr(module.prebuilt.Prefer())
Paul Duffin859fe962020-05-15 10:20:31 +01002029
Paul Duffin1267d872021-04-16 17:21:36 +01002030 // The imports need to be compiled to dex if the java_sdk_library_import requests it.
Paul Duffinf4600f62021-05-13 22:34:45 +01002031 compileDex := module.properties.Compile_dex
2032 if module.stubLibrariesCompiledForDex() {
2033 compileDex = proptools.BoolPtr(true)
2034 }
2035 props.Compile_dex = compileDex
Paul Duffin1267d872021-04-16 17:21:36 +01002036
Paul Duffin859fe962020-05-15 10:20:31 +01002037 mctx.CreateModule(ImportFactory, &props, module.sdkComponentPropertiesForChildLibrary())
Paul Duffinbbb546b2020-04-09 00:07:11 +01002038}
2039
Paul Duffineedc5d52020-06-12 17:46:39 +01002040func (module *SdkLibraryImport) createPrebuiltStubsSources(mctx android.DefaultableHookContext, apiScope *apiScope, scopeProperties *sdkLibraryScopeProperties) {
Paul Duffin3d1248c2020-04-09 00:10:17 +01002041 props := struct {
Paul Duffin38b57852020-05-13 16:08:09 +01002042 Name *string
2043 Srcs []string
2044 Prefer *bool
Paul Duffin3d1248c2020-04-09 00:10:17 +01002045 }{}
Paul Duffinc3091c82020-05-08 14:16:20 +01002046 props.Name = proptools.StringPtr(module.stubsSourceModuleName(apiScope))
Paul Duffin3d1248c2020-04-09 00:10:17 +01002047 props.Srcs = scopeProperties.Stub_srcs
2048 mctx.CreateModule(PrebuiltStubsSourcesFactory, &props)
Paul Duffin38b57852020-05-13 16:08:09 +01002049
2050 // The stubs source is preferred if the java_sdk_library_import is preferred.
2051 props.Prefer = proptools.BoolPtr(module.prebuilt.Prefer())
Paul Duffin3d1248c2020-04-09 00:10:17 +01002052}
2053
Paul Duffin44f1d842020-06-26 20:17:02 +01002054// Add the dependencies on the child module in the component deps mutator so that it
2055// creates references to the prebuilt and not the source modules.
2056func (module *SdkLibraryImport) ComponentDepsMutator(ctx android.BottomUpMutatorContext) {
Paul Duffin46a26a82020-04-07 19:27:04 +01002057 for apiScope, scopeProperties := range module.scopeProperties {
Paul Duffin56d44902020-01-31 13:36:25 +00002058 if len(scopeProperties.Jars) == 0 {
2059 continue
2060 }
2061
2062 // Add dependencies to the prebuilt stubs library
Paul Duffin864116c2021-04-02 10:24:13 +01002063 ctx.AddVariationDependencies(nil, apiScope.stubsTag, android.PrebuiltNameFromSource(module.stubsLibraryModuleName(apiScope)))
Paul Duffin0f8faff2020-05-20 16:18:00 +01002064
2065 if len(scopeProperties.Stub_srcs) > 0 {
2066 // Add dependencies to the prebuilt stubs source library
Paul Duffin864116c2021-04-02 10:24:13 +01002067 ctx.AddVariationDependencies(nil, apiScope.stubsSourceTag, android.PrebuiltNameFromSource(module.stubsSourceModuleName(apiScope)))
Paul Duffin0f8faff2020-05-20 16:18:00 +01002068 }
Paul Duffin56d44902020-01-31 13:36:25 +00002069 }
Paul Duffin44f1d842020-06-26 20:17:02 +01002070}
2071
2072// Add other dependencies as normal.
2073func (module *SdkLibraryImport) DepsMutator(ctx android.BottomUpMutatorContext) {
Paul Duffineedc5d52020-06-12 17:46:39 +01002074
2075 implName := module.implLibraryModuleName()
2076 if ctx.OtherModuleExists(implName) {
2077 ctx.AddVariationDependencies(nil, implLibraryTag, implName)
2078
2079 xmlPermissionsModuleName := module.xmlPermissionsModuleName()
2080 if module.sharedLibrary() && ctx.OtherModuleExists(xmlPermissionsModuleName) {
2081 // Add dependency to the rule for generating the xml permissions file
2082 ctx.AddDependency(module, xmlPermissionsFileTag, xmlPermissionsModuleName)
2083 }
2084 }
Colin Cross79c7c262019-04-17 11:11:46 -07002085}
2086
Jiyong Park45bf82e2020-12-15 22:29:02 +09002087var _ android.ApexModule = (*SdkLibraryImport)(nil)
2088
2089// Implements android.ApexModule
Paul Duffineedc5d52020-06-12 17:46:39 +01002090func (module *SdkLibraryImport) DepIsInSameApex(mctx android.BaseModuleContext, dep android.Module) bool {
2091 depTag := mctx.OtherModuleDependencyTag(dep)
2092 if depTag == xmlPermissionsFileTag {
2093 return true
2094 }
2095
2096 // None of the other dependencies of the java_sdk_library_import are in the same apex
2097 // as the one that references this module.
2098 return false
2099}
2100
Jiyong Park45bf82e2020-12-15 22:29:02 +09002101// Implements android.ApexModule
Dan Albertc8060532020-07-22 22:32:17 -07002102func (module *SdkLibraryImport) ShouldSupportSdkVersion(ctx android.BaseModuleContext,
2103 sdkVersion android.ApiLevel) error {
Jooyung Han749dc692020-04-15 11:03:39 +09002104 // we don't check prebuilt modules for sdk_version
2105 return nil
2106}
2107
Paul Duffinea8f8082021-06-24 13:25:57 +01002108// Implements android.ApexModule
2109func (module *SdkLibraryImport) UniqueApexVariations() bool {
2110 return module.uniqueApexVariations()
2111}
2112
Paul Duffineedc5d52020-06-12 17:46:39 +01002113func (module *SdkLibraryImport) OutputFiles(tag string) (android.Paths, error) {
Paul Duffin46dc45a2020-05-14 15:39:10 +01002114 return module.commonOutputFiles(tag)
2115}
2116
Paul Duffineedc5d52020-06-12 17:46:39 +01002117func (module *SdkLibraryImport) GenerateAndroidBuildActions(ctx android.ModuleContext) {
Paul Duffina2ae7e02020-09-11 11:55:00 +01002118 module.generateCommonBuildActions(ctx)
2119
Paul Duffin39853512021-02-26 11:09:39 +00002120 var deapexerModule android.Module
2121
Paul Duffin0f8faff2020-05-20 16:18:00 +01002122 // Record the paths to the prebuilt stubs library and stubs source.
Colin Cross79c7c262019-04-17 11:11:46 -07002123 ctx.VisitDirectDeps(func(to android.Module) {
2124 tag := ctx.OtherModuleDependencyTag(to)
2125
Paul Duffin0f8faff2020-05-20 16:18:00 +01002126 // Extract information from any of the scope specific dependencies.
2127 if scopeTag, ok := tag.(scopeDependencyTag); ok {
2128 apiScope := scopeTag.apiScope
2129 scopePaths := module.getScopePathsCreateIfNeeded(apiScope)
2130
2131 // Extract information from the dependency. The exact information extracted
2132 // is determined by the nature of the dependency which is determined by the tag.
2133 scopeTag.extractDepInfo(ctx, to, scopePaths)
Paul Duffineedc5d52020-06-12 17:46:39 +01002134 } else if tag == implLibraryTag {
2135 if implLibrary, ok := to.(*Library); ok {
2136 module.implLibraryModule = implLibrary
2137 } else {
2138 ctx.ModuleErrorf("implementation library must be of type *java.Library but was %T", to)
2139 }
2140 } else if tag == xmlPermissionsFileTag {
2141 if xmlPermissionsFileModule, ok := to.(*sdkLibraryXml); ok {
2142 module.xmlPermissionsFileModule = xmlPermissionsFileModule
2143 } else {
2144 ctx.ModuleErrorf("xml permissions file module must be of type *sdkLibraryXml but was %T", to)
2145 }
Colin Cross79c7c262019-04-17 11:11:46 -07002146 }
Paul Duffin39853512021-02-26 11:09:39 +00002147
2148 // Save away the `deapexer` module on which this depends, if any.
2149 if tag == android.DeapexerTag {
2150 deapexerModule = to
2151 }
Colin Cross79c7c262019-04-17 11:11:46 -07002152 })
Paul Duffin0f8faff2020-05-20 16:18:00 +01002153
2154 // Populate the scope paths with information from the properties.
2155 for apiScope, scopeProperties := range module.scopeProperties {
2156 if len(scopeProperties.Jars) == 0 {
2157 continue
2158 }
2159
2160 paths := module.getScopePathsCreateIfNeeded(apiScope)
2161 paths.currentApiFilePath = android.OptionalPathForModuleSrc(ctx, scopeProperties.Current_api)
2162 paths.removedApiFilePath = android.OptionalPathForModuleSrc(ctx, scopeProperties.Removed_api)
2163 }
Paul Duffin39853512021-02-26 11:09:39 +00002164
2165 if ctx.Device() {
2166 // If this is a variant created for a prebuilt_apex then use the dex implementation jar
2167 // obtained from the associated deapexer module.
2168 ai := ctx.Provider(android.ApexInfoProvider).(android.ApexInfo)
2169 if ai.ForPrebuiltApex {
2170 if deapexerModule == nil {
2171 // This should never happen as a variant for a prebuilt_apex is only created if the
2172 // deapxer module has been configured to export the dex implementation jar for this module.
2173 ctx.ModuleErrorf("internal error: module %q does not depend on a `deapexer` module for prebuilt_apex %q",
2174 module.Name(), ai.ApexVariationName)
2175 }
2176
2177 // Get the path of the dex implementation jar from the `deapexer` module.
2178 di := ctx.OtherModuleProvider(deapexerModule, android.DeapexerProvider).(android.DeapexerInfo)
Paul Duffinb4bbf2c2021-06-17 15:59:07 +01002179 if dexOutputPath := di.PrebuiltExportPath(apexRootRelativePathToJavaLib(module.BaseModuleName())); dexOutputPath != nil {
Paul Duffin39853512021-02-26 11:09:39 +00002180 module.dexJarFile = dexOutputPath
Paul Duffin1bbd0622021-05-14 15:52:25 +01002181 module.initHiddenAPI(ctx, dexOutputPath, module.findScopePaths(apiScopePublic).stubsImplPath[0], nil)
Paul Duffin39853512021-02-26 11:09:39 +00002182 } else {
2183 // This should never happen as a variant for a prebuilt_apex is only created if the
2184 // prebuilt_apex has been configured to export the java library dex file.
2185 ctx.ModuleErrorf("internal error: no dex implementation jar available from prebuilt_apex %q", deapexerModule.Name())
2186 }
2187 }
2188 }
Colin Cross79c7c262019-04-17 11:11:46 -07002189}
2190
Jiyong Parkf1691d22021-03-29 20:11:58 +09002191func (module *SdkLibraryImport) sdkJars(ctx android.BaseModuleContext, sdkVersion android.SdkSpec, headerJars bool) android.Paths {
Paul Duffineedc5d52020-06-12 17:46:39 +01002192
2193 // For consistency with SdkLibrary make the implementation jar available to libraries that
2194 // are within the same APEX.
2195 implLibraryModule := module.implLibraryModule
Colin Cross56a83212020-09-15 18:30:11 -07002196 if implLibraryModule != nil && withinSameApexesAs(ctx, module) {
Paul Duffineedc5d52020-06-12 17:46:39 +01002197 if headerJars {
2198 return implLibraryModule.HeaderJars()
2199 } else {
2200 return implLibraryModule.ImplementationJars()
2201 }
2202 }
2203
Paul Duffin23970f42020-05-20 14:20:02 +01002204 return module.selectHeaderJarsForSdkVersion(ctx, sdkVersion)
Paul Duffin56d44902020-01-31 13:36:25 +00002205}
2206
Colin Cross79c7c262019-04-17 11:11:46 -07002207// to satisfy SdkLibraryDependency interface
Jiyong Parkf1691d22021-03-29 20:11:58 +09002208func (module *SdkLibraryImport) SdkHeaderJars(ctx android.BaseModuleContext, sdkVersion android.SdkSpec) android.Paths {
Colin Cross79c7c262019-04-17 11:11:46 -07002209 // This module is just a wrapper for the prebuilt stubs.
Paul Duffineedc5d52020-06-12 17:46:39 +01002210 return module.sdkJars(ctx, sdkVersion, true)
Colin Cross79c7c262019-04-17 11:11:46 -07002211}
2212
2213// to satisfy SdkLibraryDependency interface
Jiyong Parkf1691d22021-03-29 20:11:58 +09002214func (module *SdkLibraryImport) SdkImplementationJars(ctx android.BaseModuleContext, sdkVersion android.SdkSpec) android.Paths {
Colin Cross79c7c262019-04-17 11:11:46 -07002215 // This module is just a wrapper for the stubs.
Paul Duffineedc5d52020-06-12 17:46:39 +01002216 return module.sdkJars(ctx, sdkVersion, false)
2217}
2218
Ulya Trafimovichdbf31662020-12-17 12:07:54 +00002219// to satisfy UsesLibraryDependency interface
Paul Duffineedc5d52020-06-12 17:46:39 +01002220func (module *SdkLibraryImport) DexJarBuildPath() android.Path {
Paul Duffin39853512021-02-26 11:09:39 +00002221 // The dex implementation jar extracted from the .apex file should be used in preference to the
2222 // source.
2223 if module.dexJarFile != nil {
2224 return module.dexJarFile
2225 }
Paul Duffineedc5d52020-06-12 17:46:39 +01002226 if module.implLibraryModule == nil {
2227 return nil
2228 } else {
2229 return module.implLibraryModule.DexJarBuildPath()
2230 }
2231}
2232
Ulya Trafimovichdbf31662020-12-17 12:07:54 +00002233// to satisfy UsesLibraryDependency interface
Ulya Trafimovich31e444e2020-08-14 17:32:16 +01002234func (module *SdkLibraryImport) DexJarInstallPath() android.Path {
2235 if module.implLibraryModule == nil {
2236 return nil
2237 } else {
2238 return module.implLibraryModule.DexJarInstallPath()
2239 }
2240}
2241
Ulya Trafimovichdbf31662020-12-17 12:07:54 +00002242// to satisfy UsesLibraryDependency interface
2243func (module *SdkLibraryImport) ClassLoaderContexts() dexpreopt.ClassLoaderContextMap {
2244 return nil
2245}
2246
Paul Duffineedc5d52020-06-12 17:46:39 +01002247// to satisfy apex.javaDependency interface
2248func (module *SdkLibraryImport) JacocoReportClassesFile() android.Path {
2249 if module.implLibraryModule == nil {
2250 return nil
2251 } else {
2252 return module.implLibraryModule.JacocoReportClassesFile()
2253 }
2254}
2255
2256// to satisfy apex.javaDependency interface
Colin Cross08dca382020-07-21 20:31:17 -07002257func (module *SdkLibraryImport) LintDepSets() LintDepSets {
2258 if module.implLibraryModule == nil {
2259 return LintDepSets{}
2260 } else {
2261 return module.implLibraryModule.LintDepSets()
2262 }
2263}
2264
Jaewoong Jung476b9d62021-05-10 15:30:00 -07002265func (module *SdkLibraryImport) getStrictUpdatabilityLinting() bool {
2266 if module.implLibraryModule == nil {
2267 return false
2268 } else {
2269 return module.implLibraryModule.getStrictUpdatabilityLinting()
2270 }
2271}
2272
2273func (module *SdkLibraryImport) setStrictUpdatabilityLinting(strictLinting bool) {
2274 if module.implLibraryModule != nil {
2275 module.implLibraryModule.setStrictUpdatabilityLinting(strictLinting)
2276 }
2277}
2278
Colin Cross08dca382020-07-21 20:31:17 -07002279// to satisfy apex.javaDependency interface
Paul Duffineedc5d52020-06-12 17:46:39 +01002280func (module *SdkLibraryImport) Stem() string {
2281 return module.BaseModuleName()
Colin Cross79c7c262019-04-17 11:11:46 -07002282}
Jiyong Parke3833882020-02-17 17:28:10 +09002283
Paul Duffin44b481b2020-06-17 16:59:43 +01002284var _ ApexDependency = (*SdkLibraryImport)(nil)
2285
2286// to satisfy java.ApexDependency interface
2287func (module *SdkLibraryImport) HeaderJars() android.Paths {
2288 if module.implLibraryModule == nil {
2289 return nil
2290 } else {
2291 return module.implLibraryModule.HeaderJars()
2292 }
2293}
2294
2295// to satisfy java.ApexDependency interface
2296func (module *SdkLibraryImport) ImplementationAndResourcesJars() android.Paths {
2297 if module.implLibraryModule == nil {
2298 return nil
2299 } else {
2300 return module.implLibraryModule.ImplementationAndResourcesJars()
2301 }
2302}
2303
Paul Duffinfef55002021-06-17 14:56:05 +01002304var _ android.RequiredFilesFromPrebuiltApex = (*SdkLibraryImport)(nil)
2305
Paul Duffinb4bbf2c2021-06-17 15:59:07 +01002306func (module *SdkLibraryImport) RequiredFilesFromPrebuiltApex(ctx android.BaseModuleContext) []string {
Paul Duffinfef55002021-06-17 14:56:05 +01002307 name := module.BaseModuleName()
2308 return requiredFilesFromPrebuiltApexForImport(name)
2309}
2310
Jiyong Parke3833882020-02-17 17:28:10 +09002311//
2312// java_sdk_library_xml
2313//
2314type sdkLibraryXml struct {
2315 android.ModuleBase
2316 android.DefaultableModuleBase
2317 android.ApexModuleBase
2318
2319 properties sdkLibraryXmlProperties
2320
2321 outputFilePath android.OutputPath
2322 installDirPath android.InstallPath
Colin Cross56a83212020-09-15 18:30:11 -07002323
2324 hideApexVariantFromMake bool
Jiyong Parke3833882020-02-17 17:28:10 +09002325}
2326
2327type sdkLibraryXmlProperties struct {
2328 // canonical name of the lib
2329 Lib_name *string
2330}
2331
2332// java_sdk_library_xml builds the permission xml file for a java_sdk_library.
2333// Not to be used directly by users. java_sdk_library internally uses this.
2334func sdkLibraryXmlFactory() android.Module {
2335 module := &sdkLibraryXml{}
2336
2337 module.AddProperties(&module.properties)
2338
2339 android.InitApexModule(module)
2340 android.InitAndroidArchModule(module, android.DeviceSupported, android.MultilibCommon)
2341
2342 return module
2343}
2344
Colin Crossaede88c2020-08-11 12:17:01 -07002345func (module *sdkLibraryXml) UniqueApexVariations() bool {
2346 // sdkLibraryXml needs a unique variation per APEX because the generated XML file contains the path to the
2347 // mounted APEX, which contains the name of the APEX.
2348 return true
2349}
2350
Jiyong Parke3833882020-02-17 17:28:10 +09002351// from android.PrebuiltEtcModule
Jooyung Han0703fd82020-08-26 22:11:53 +09002352func (module *sdkLibraryXml) BaseDir() string {
2353 return "etc"
2354}
2355
2356// from android.PrebuiltEtcModule
Jiyong Parke3833882020-02-17 17:28:10 +09002357func (module *sdkLibraryXml) SubDir() string {
2358 return "permissions"
2359}
2360
2361// from android.PrebuiltEtcModule
2362func (module *sdkLibraryXml) OutputFile() android.OutputPath {
2363 return module.outputFilePath
2364}
2365
2366// from android.ApexModule
2367func (module *sdkLibraryXml) AvailableFor(what string) bool {
2368 return true
2369}
2370
2371func (module *sdkLibraryXml) DepsMutator(ctx android.BottomUpMutatorContext) {
2372 // do nothing
2373}
2374
Jiyong Park45bf82e2020-12-15 22:29:02 +09002375var _ android.ApexModule = (*sdkLibraryXml)(nil)
2376
2377// Implements android.ApexModule
Dan Albertc8060532020-07-22 22:32:17 -07002378func (module *sdkLibraryXml) ShouldSupportSdkVersion(ctx android.BaseModuleContext,
2379 sdkVersion android.ApiLevel) error {
Jooyung Han749dc692020-04-15 11:03:39 +09002380 // sdkLibraryXml doesn't need to be checked separately because java_sdk_library is checked
2381 return nil
2382}
2383
Jiyong Parke3833882020-02-17 17:28:10 +09002384// File path to the runtime implementation library
Colin Cross56a83212020-09-15 18:30:11 -07002385func (module *sdkLibraryXml) implPath(ctx android.ModuleContext) string {
Jiyong Parke3833882020-02-17 17:28:10 +09002386 implName := proptools.String(module.properties.Lib_name)
Colin Cross56a83212020-09-15 18:30:11 -07002387 if apexInfo := ctx.Provider(android.ApexInfoProvider).(android.ApexInfo); !apexInfo.IsForPlatform() {
Colin Crosse07f2312020-08-13 11:24:56 -07002388 // TODO(b/146468504): ApexVariationName() is only a soong module name, not apex name.
Jiyong Parke3833882020-02-17 17:28:10 +09002389 // In most cases, this works fine. But when apex_name is set or override_apex is used
2390 // this can be wrong.
Colin Cross56a83212020-09-15 18:30:11 -07002391 return fmt.Sprintf("/apex/%s/javalib/%s.jar", apexInfo.ApexVariationName, implName)
Jiyong Parke3833882020-02-17 17:28:10 +09002392 }
2393 partition := "system"
2394 if module.SocSpecific() {
2395 partition = "vendor"
2396 } else if module.DeviceSpecific() {
2397 partition = "odm"
2398 } else if module.ProductSpecific() {
2399 partition = "product"
2400 } else if module.SystemExtSpecific() {
2401 partition = "system_ext"
2402 }
2403 return "/" + partition + "/framework/" + implName + ".jar"
2404}
2405
2406func (module *sdkLibraryXml) GenerateAndroidBuildActions(ctx android.ModuleContext) {
Colin Cross56a83212020-09-15 18:30:11 -07002407 module.hideApexVariantFromMake = !ctx.Provider(android.ApexInfoProvider).(android.ApexInfo).IsForPlatform()
2408
Jiyong Parke3833882020-02-17 17:28:10 +09002409 libName := proptools.String(module.properties.Lib_name)
Colin Cross56a83212020-09-15 18:30:11 -07002410 xmlContent := fmt.Sprintf(permissionsTemplate, libName, module.implPath(ctx))
Jiyong Parke3833882020-02-17 17:28:10 +09002411
2412 module.outputFilePath = android.PathForModuleOut(ctx, libName+".xml").OutputPath
Colin Crossf1a035e2020-11-16 17:32:30 -08002413 rule := android.NewRuleBuilder(pctx, ctx)
Jiyong Parke3833882020-02-17 17:28:10 +09002414 rule.Command().
2415 Text("/bin/bash -c \"echo -e '" + xmlContent + "'\" > ").
2416 Output(module.outputFilePath)
2417
Colin Crossf1a035e2020-11-16 17:32:30 -08002418 rule.Build("java_sdk_xml", "Permission XML")
Jiyong Parke3833882020-02-17 17:28:10 +09002419
2420 module.installDirPath = android.PathForModuleInstall(ctx, "etc", module.SubDir())
2421}
2422
2423func (module *sdkLibraryXml) AndroidMkEntries() []android.AndroidMkEntries {
Colin Cross56a83212020-09-15 18:30:11 -07002424 if module.hideApexVariantFromMake {
Jiyong Parke3833882020-02-17 17:28:10 +09002425 return []android.AndroidMkEntries{android.AndroidMkEntries{
2426 Disabled: true,
2427 }}
2428 }
2429
2430 return []android.AndroidMkEntries{android.AndroidMkEntries{
2431 Class: "ETC",
2432 OutputFile: android.OptionalPathForPath(module.outputFilePath),
2433 ExtraEntries: []android.AndroidMkExtraEntriesFunc{
Colin Crossaa255532020-07-03 13:18:24 -07002434 func(ctx android.AndroidMkExtraEntriesContext, entries *android.AndroidMkEntries) {
Jiyong Parke3833882020-02-17 17:28:10 +09002435 entries.SetString("LOCAL_MODULE_TAGS", "optional")
2436 entries.SetString("LOCAL_MODULE_PATH", module.installDirPath.ToMakePath().String())
2437 entries.SetString("LOCAL_INSTALLED_MODULE_STEM", module.outputFilePath.Base())
2438 },
2439 },
2440 }}
2441}
Paul Duffindd46f712020-02-10 13:37:10 +00002442
2443type sdkLibrarySdkMemberType struct {
2444 android.SdkMemberTypeBase
2445}
2446
2447func (s *sdkLibrarySdkMemberType) AddDependencies(mctx android.BottomUpMutatorContext, dependencyTag blueprint.DependencyTag, names []string) {
2448 mctx.AddVariationDependencies(nil, dependencyTag, names...)
2449}
2450
2451func (s *sdkLibrarySdkMemberType) IsInstance(module android.Module) bool {
2452 _, ok := module.(*SdkLibrary)
2453 return ok
2454}
2455
2456func (s *sdkLibrarySdkMemberType) AddPrebuiltModule(ctx android.SdkMemberContext, member android.SdkMember) android.BpModule {
2457 return ctx.SnapshotBuilder().AddPrebuiltModule(member, "java_sdk_library_import")
2458}
2459
2460func (s *sdkLibrarySdkMemberType) CreateVariantPropertiesStruct() android.SdkMemberProperties {
2461 return &sdkLibrarySdkMemberProperties{}
2462}
2463
Paul Duffin976b0e52021-04-27 23:20:26 +01002464var javaSdkLibrarySdkMemberType = &sdkLibrarySdkMemberType{
2465 android.SdkMemberTypeBase{
2466 PropertyName: "java_sdk_libs",
2467 SupportsSdk: true,
2468 },
2469}
2470
Paul Duffindd46f712020-02-10 13:37:10 +00002471type sdkLibrarySdkMemberProperties struct {
2472 android.SdkMemberPropertiesBase
2473
2474 // Scope to per scope properties.
2475 Scopes map[*apiScope]scopeProperties
2476
Paul Duffin3d1248c2020-04-09 00:10:17 +01002477 // The Java stubs source files.
2478 Stub_srcs []string
Paul Duffinf7a64332020-05-13 16:54:55 +01002479
2480 // The naming scheme.
2481 Naming_scheme *string
Paul Duffind7eb1c22020-05-26 20:57:10 +01002482
2483 // True if the java_sdk_library_import is for a shared library, false
2484 // otherwise.
2485 Shared_library *bool
Paul Duffina2ae7e02020-09-11 11:55:00 +01002486
Paul Duffin1267d872021-04-16 17:21:36 +01002487 // True if the stub imports should produce dex jars.
2488 Compile_dex *bool
2489
Paul Duffina2ae7e02020-09-11 11:55:00 +01002490 // The paths to the doctag files to add to the prebuilt.
2491 Doctag_paths android.Paths
Paul Duffindd46f712020-02-10 13:37:10 +00002492}
2493
2494type scopeProperties struct {
Paul Duffin1fd005d2020-04-09 01:08:11 +01002495 Jars android.Paths
2496 StubsSrcJar android.Path
2497 CurrentApiFile android.Path
2498 RemovedApiFile android.Path
2499 SdkVersion string
Paul Duffindd46f712020-02-10 13:37:10 +00002500}
2501
2502func (s *sdkLibrarySdkMemberProperties) PopulateFromVariant(ctx android.SdkMemberContext, variant android.Module) {
2503 sdk := variant.(*SdkLibrary)
2504
2505 s.Scopes = make(map[*apiScope]scopeProperties)
2506 for _, apiScope := range allApiScopes {
Paul Duffin803a9562020-05-20 11:52:25 +01002507 paths := sdk.findScopePaths(apiScope)
2508 if paths == nil {
2509 continue
2510 }
2511
Paul Duffindd46f712020-02-10 13:37:10 +00002512 jars := paths.stubsImplPath
2513 if len(jars) > 0 {
2514 properties := scopeProperties{}
2515 properties.Jars = jars
Paul Duffin780c5f42020-05-12 15:52:55 +01002516 properties.SdkVersion = sdk.sdkVersionForStubsLibrary(ctx.SdkModuleContext(), apiScope)
Paul Duffin0f8faff2020-05-20 16:18:00 +01002517 properties.StubsSrcJar = paths.stubsSrcJar.Path()
Paul Duffin10269f12020-06-19 18:39:55 +01002518 if paths.currentApiFilePath.Valid() {
2519 properties.CurrentApiFile = paths.currentApiFilePath.Path()
2520 }
2521 if paths.removedApiFilePath.Valid() {
2522 properties.RemovedApiFile = paths.removedApiFilePath.Path()
2523 }
Paul Duffindd46f712020-02-10 13:37:10 +00002524 s.Scopes[apiScope] = properties
2525 }
2526 }
2527
Paul Duffindfa131e2020-05-15 20:37:11 +01002528 s.Naming_scheme = sdk.commonSdkLibraryProperties.Naming_scheme
Paul Duffind7eb1c22020-05-26 20:57:10 +01002529 s.Shared_library = proptools.BoolPtr(sdk.sharedLibrary())
Paul Duffin1267d872021-04-16 17:21:36 +01002530 s.Compile_dex = sdk.dexProperties.Compile_dex
Paul Duffina2ae7e02020-09-11 11:55:00 +01002531 s.Doctag_paths = sdk.doctagPaths
Paul Duffindd46f712020-02-10 13:37:10 +00002532}
2533
2534func (s *sdkLibrarySdkMemberProperties) AddToPropertySet(ctx android.SdkMemberContext, propertySet android.BpPropertySet) {
Paul Duffinf7a64332020-05-13 16:54:55 +01002535 if s.Naming_scheme != nil {
2536 propertySet.AddProperty("naming_scheme", proptools.String(s.Naming_scheme))
2537 }
Paul Duffind7eb1c22020-05-26 20:57:10 +01002538 if s.Shared_library != nil {
2539 propertySet.AddProperty("shared_library", *s.Shared_library)
2540 }
Paul Duffin1267d872021-04-16 17:21:36 +01002541 if s.Compile_dex != nil {
2542 propertySet.AddProperty("compile_dex", *s.Compile_dex)
2543 }
Paul Duffinf7a64332020-05-13 16:54:55 +01002544
Paul Duffindd46f712020-02-10 13:37:10 +00002545 for _, apiScope := range allApiScopes {
2546 if properties, ok := s.Scopes[apiScope]; ok {
Paul Duffin6b836ba2020-05-13 19:19:49 +01002547 scopeSet := propertySet.AddPropertySet(apiScope.propertyName)
Paul Duffindd46f712020-02-10 13:37:10 +00002548
Paul Duffin3d1248c2020-04-09 00:10:17 +01002549 scopeDir := filepath.Join("sdk_library", s.OsPrefix(), apiScope.name)
2550
Paul Duffindd46f712020-02-10 13:37:10 +00002551 var jars []string
2552 for _, p := range properties.Jars {
Paul Duffin3d1248c2020-04-09 00:10:17 +01002553 dest := filepath.Join(scopeDir, ctx.Name()+"-stubs.jar")
Paul Duffindd46f712020-02-10 13:37:10 +00002554 ctx.SnapshotBuilder().CopyToSnapshot(p, dest)
2555 jars = append(jars, dest)
2556 }
2557 scopeSet.AddProperty("jars", jars)
2558
Paul Duffin22628d52021-05-12 23:13:22 +01002559 if ctx.SdkModuleContext().Config().IsEnvTrue("SOONG_SDK_SNAPSHOT_USE_SRCJAR") {
2560 // Copy the stubs source jar into the snapshot zip as is.
2561 srcJarSnapshotPath := filepath.Join(scopeDir, ctx.Name()+".srcjar")
2562 ctx.SnapshotBuilder().CopyToSnapshot(properties.StubsSrcJar, srcJarSnapshotPath)
2563 scopeSet.AddProperty("stub_srcs", []string{srcJarSnapshotPath})
2564 } else {
2565 // Merge the stubs source jar into the snapshot zip so that when it is unpacked
2566 // the source files are also unpacked.
2567 snapshotRelativeDir := filepath.Join(scopeDir, ctx.Name()+"_stub_sources")
2568 ctx.SnapshotBuilder().UnzipToSnapshot(properties.StubsSrcJar, snapshotRelativeDir)
2569 scopeSet.AddProperty("stub_srcs", []string{snapshotRelativeDir})
2570 }
Paul Duffin3d1248c2020-04-09 00:10:17 +01002571
Paul Duffin1fd005d2020-04-09 01:08:11 +01002572 if properties.CurrentApiFile != nil {
2573 currentApiSnapshotPath := filepath.Join(scopeDir, ctx.Name()+".txt")
2574 ctx.SnapshotBuilder().CopyToSnapshot(properties.CurrentApiFile, currentApiSnapshotPath)
2575 scopeSet.AddProperty("current_api", currentApiSnapshotPath)
2576 }
2577
2578 if properties.RemovedApiFile != nil {
2579 removedApiSnapshotPath := filepath.Join(scopeDir, ctx.Name()+"-removed.txt")
Paul Duffin3dbf9fd2020-06-02 13:00:02 +01002580 ctx.SnapshotBuilder().CopyToSnapshot(properties.RemovedApiFile, removedApiSnapshotPath)
Paul Duffin1fd005d2020-04-09 01:08:11 +01002581 scopeSet.AddProperty("removed_api", removedApiSnapshotPath)
2582 }
2583
Paul Duffindd46f712020-02-10 13:37:10 +00002584 if properties.SdkVersion != "" {
2585 scopeSet.AddProperty("sdk_version", properties.SdkVersion)
2586 }
2587 }
2588 }
2589
Paul Duffina2ae7e02020-09-11 11:55:00 +01002590 if len(s.Doctag_paths) > 0 {
2591 dests := []string{}
2592 for _, p := range s.Doctag_paths {
2593 dest := filepath.Join("doctags", p.Rel())
2594 ctx.SnapshotBuilder().CopyToSnapshot(p, dest)
2595 dests = append(dests, dest)
2596 }
2597 propertySet.AddProperty("doctag_files", dests)
2598 }
Paul Duffindd46f712020-02-10 13:37:10 +00002599}