blob: d6ef4e9ea61aff542fa27fcadd187d9e3ec522d3 [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"
Jiyong Parkc678ad32018-04-10 13:07:10 +090031)
32
Jooyung Han58f26ab2019-12-18 15:34:32 +090033const (
Paul Duffindd9d0742020-05-08 15:52:37 +010034 sdkXmlFileSuffix = ".xml"
35 permissionsTemplate = `<?xml version=\"1.0\" encoding=\"utf-8\"?>\n` +
Jooyung Han624058e2019-12-24 18:38:06 +090036 `<!-- Copyright (C) 2018 The Android Open Source Project\n` +
37 `\n` +
Jiyong Parke3833882020-02-17 17:28:10 +090038 ` Licensed under the Apache License, Version 2.0 (the \"License\");\n` +
Jooyung Han624058e2019-12-24 18:38:06 +090039 ` you may not use this file except in compliance with the License.\n` +
40 ` You may obtain a copy of the License at\n` +
41 `\n` +
42 ` http://www.apache.org/licenses/LICENSE-2.0\n` +
43 `\n` +
44 ` Unless required by applicable law or agreed to in writing, software\n` +
Jiyong Parke3833882020-02-17 17:28:10 +090045 ` distributed under the License is distributed on an \"AS IS\" BASIS,\n` +
Jooyung Han624058e2019-12-24 18:38:06 +090046 ` WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\n` +
47 ` See the License for the specific language governing permissions and\n` +
48 ` limitations under the License.\n` +
49 `-->\n` +
50 `<permissions>\n` +
Jiyong Parke3833882020-02-17 17:28:10 +090051 ` <library name=\"%s\" file=\"%s\"/>\n` +
Jooyung Han624058e2019-12-24 18:38:06 +090052 `</permissions>\n`
Jiyong Parkc678ad32018-04-10 13:07:10 +090053)
54
Paul Duffind1b3a922020-01-22 11:57:20 +000055// A tag to associated a dependency with a specific api scope.
56type scopeDependencyTag struct {
57 blueprint.BaseDependencyTag
58 name string
59 apiScope *apiScope
Paul Duffinc8782502020-04-29 20:45:27 +010060
61 // Function for extracting appropriate path information from the dependency.
62 depInfoExtractor func(paths *scopePaths, dep android.Module) error
63}
64
65// Extract tag specific information from the dependency.
66func (tag scopeDependencyTag) extractDepInfo(ctx android.ModuleContext, dep android.Module, paths *scopePaths) {
67 err := tag.depInfoExtractor(paths, dep)
68 if err != nil {
69 ctx.ModuleErrorf("has an invalid {scopeDependencyTag: %s} dependency on module %s: %s", tag.name, ctx.OtherModuleName(dep), err.Error())
70 }
Paul Duffind1b3a922020-01-22 11:57:20 +000071}
72
Paul Duffin80342d72020-06-26 22:08:43 +010073var _ android.ReplaceSourceWithPrebuilt = (*scopeDependencyTag)(nil)
74
75func (tag scopeDependencyTag) ReplaceSourceWithPrebuilt() bool {
76 return false
77}
78
Paul Duffind1b3a922020-01-22 11:57:20 +000079// Provides information about an api scope, e.g. public, system, test.
80type apiScope struct {
81 // The name of the api scope, e.g. public, system, test
82 name string
83
Paul Duffin97b53b82020-05-05 14:40:52 +010084 // The api scope that this scope extends.
85 extends *apiScope
86
Paul Duffin3375e352020-04-28 10:44:03 +010087 // The legacy enabled status for a specific scope can be dependent on other
88 // properties that have been specified on the library so it is provided by
89 // a function that can determine the status by examining those properties.
90 legacyEnabledStatus func(module *SdkLibrary) bool
91
92 // The default enabled status for non-legacy behavior, which is triggered by
93 // explicitly enabling at least one api scope.
94 defaultEnabledStatus bool
95
96 // Gets a pointer to the scope specific properties.
97 scopeSpecificProperties func(module *SdkLibrary) *ApiScopeProperties
98
Paul Duffin46a26a82020-04-07 19:27:04 +010099 // The name of the field in the dynamically created structure.
100 fieldName string
101
Paul Duffin6b836ba2020-05-13 19:19:49 +0100102 // The name of the property in the java_sdk_library_import
103 propertyName string
104
Paul Duffind1b3a922020-01-22 11:57:20 +0000105 // The tag to use to depend on the stubs library module.
106 stubsTag scopeDependencyTag
107
Paul Duffin0ff08bd2020-04-29 13:30:54 +0100108 // The tag to use to depend on the stubs source module (if separate from the API module).
109 stubsSourceTag scopeDependencyTag
110
111 // The tag to use to depend on the API file generating module (if separate from the stubs source module).
112 apiFileTag scopeDependencyTag
113
Paul Duffinc8782502020-04-29 20:45:27 +0100114 // The tag to use to depend on the stubs source and API module.
115 stubsSourceAndApiTag scopeDependencyTag
Paul Duffind1b3a922020-01-22 11:57:20 +0000116
117 // The scope specific prefix to add to the api file base of "current.txt" or "removed.txt".
118 apiFilePrefix string
119
120 // The scope specific prefix to add to the sdk library module name to construct a scope specific
121 // module name.
122 moduleSuffix string
123
Paul Duffind1b3a922020-01-22 11:57:20 +0000124 // SDK version that the stubs library is built against. Note that this is always
125 // *current. Older stubs library built with a numbered SDK version is created from
126 // the prebuilt jar.
127 sdkVersion string
Paul Duffin1fb487d2020-04-07 18:50:10 +0100128
Paul Duffin15f34ef2020-07-20 18:04:44 +0100129 // The annotation that identifies this API level, empty for the public API scope.
130 annotation string
131
Paul Duffin1fb487d2020-04-07 18:50:10 +0100132 // Extra arguments to pass to droidstubs for this scope.
Paul Duffin0ff08bd2020-04-29 13:30:54 +0100133 //
Paul Duffin15f34ef2020-07-20 18:04:44 +0100134 // This is not used directly but is used to construct the droidstubsArgs.
135 extraArgs []string
Paul Duffin0ff08bd2020-04-29 13:30:54 +0100136
Paul Duffin15f34ef2020-07-20 18:04:44 +0100137 // The args that must be passed to droidstubs to generate the API and stubs source
138 // for this scope, constructed dynamically by initApiScope().
Paul Duffin0ff08bd2020-04-29 13:30:54 +0100139 //
140 // The API only includes the additional members that this scope adds over the scope
141 // that it extends.
Paul Duffin15f34ef2020-07-20 18:04:44 +0100142 //
143 // The stubs source must include the definitions of everything that is in this
144 // api scope and all the scopes that this one extends.
145 droidstubsArgs []string
Paul Duffin0ff08bd2020-04-29 13:30:54 +0100146
Anton Hansson6478ac12020-05-02 11:19:36 +0100147 // Whether the api scope can be treated as unstable, and should skip compat checks.
148 unstable bool
Paul Duffind1b3a922020-01-22 11:57:20 +0000149}
150
151// Initialize a scope, creating and adding appropriate dependency tags
152func initApiScope(scope *apiScope) *apiScope {
Paul Duffinc8782502020-04-29 20:45:27 +0100153 name := scope.name
Paul Duffin46dc45a2020-05-14 15:39:10 +0100154 scopeByName[name] = scope
155 allScopeNames = append(allScopeNames, name)
Paul Duffin6b836ba2020-05-13 19:19:49 +0100156 scope.propertyName = strings.ReplaceAll(name, "-", "_")
157 scope.fieldName = proptools.FieldNameForProperty(scope.propertyName)
Paul Duffind1b3a922020-01-22 11:57:20 +0000158 scope.stubsTag = scopeDependencyTag{
Paul Duffinc8782502020-04-29 20:45:27 +0100159 name: name + "-stubs",
160 apiScope: scope,
161 depInfoExtractor: (*scopePaths).extractStubsLibraryInfoFromDependency,
Paul Duffind1b3a922020-01-22 11:57:20 +0000162 }
Paul Duffin0ff08bd2020-04-29 13:30:54 +0100163 scope.stubsSourceTag = scopeDependencyTag{
164 name: name + "-stubs-source",
165 apiScope: scope,
166 depInfoExtractor: (*scopePaths).extractStubsSourceInfoFromDep,
167 }
168 scope.apiFileTag = scopeDependencyTag{
169 name: name + "-api",
170 apiScope: scope,
171 depInfoExtractor: (*scopePaths).extractApiInfoFromDep,
172 }
Paul Duffinc8782502020-04-29 20:45:27 +0100173 scope.stubsSourceAndApiTag = scopeDependencyTag{
174 name: name + "-stubs-source-and-api",
175 apiScope: scope,
176 depInfoExtractor: (*scopePaths).extractStubsSourceAndApiInfoFromApiStubsProvider,
Paul Duffind1b3a922020-01-22 11:57:20 +0000177 }
Paul Duffin0ff08bd2020-04-29 13:30:54 +0100178
179 // To get the args needed to generate the stubs source append all the args from
180 // this scope and all the scopes it extends as each set of args adds additional
181 // members to the stubs.
Paul Duffin15f34ef2020-07-20 18:04:44 +0100182 var scopeSpecificArgs []string
183 if scope.annotation != "" {
184 scopeSpecificArgs = []string{"--show-annotation", scope.annotation}
Paul Duffin0ff08bd2020-04-29 13:30:54 +0100185 }
Paul Duffin15f34ef2020-07-20 18:04:44 +0100186 for s := scope; s != nil; s = s.extends {
187 scopeSpecificArgs = append(scopeSpecificArgs, s.extraArgs...)
Paul Duffin0ff08bd2020-04-29 13:30:54 +0100188
Paul Duffin15f34ef2020-07-20 18:04:44 +0100189 // Ensure that the generated stubs includes all the API elements from the API scope
190 // that this scope extends.
191 if s != scope && s.annotation != "" {
192 scopeSpecificArgs = append(scopeSpecificArgs, "--show-for-stub-purposes-annotation", s.annotation)
193 }
194 }
Paul Duffin0ff08bd2020-04-29 13:30:54 +0100195
Paul Duffin15f34ef2020-07-20 18:04:44 +0100196 // Escape any special characters in the arguments. This is needed because droidstubs
197 // passes these directly to the shell command.
198 scope.droidstubsArgs = proptools.ShellEscapeList(scopeSpecificArgs)
Paul Duffin0ff08bd2020-04-29 13:30:54 +0100199
Paul Duffind1b3a922020-01-22 11:57:20 +0000200 return scope
201}
202
Paul Duffinc3091c82020-05-08 14:16:20 +0100203func (scope *apiScope) stubsLibraryModuleName(baseName string) string {
Paul Duffindd9d0742020-05-08 15:52:37 +0100204 return baseName + ".stubs" + scope.moduleSuffix
Paul Duffind1b3a922020-01-22 11:57:20 +0000205}
206
Paul Duffinc8782502020-04-29 20:45:27 +0100207func (scope *apiScope) stubsSourceModuleName(baseName string) string {
Paul Duffindd9d0742020-05-08 15:52:37 +0100208 return baseName + ".stubs.source" + scope.moduleSuffix
Paul Duffind1b3a922020-01-22 11:57:20 +0000209}
210
Paul Duffin0ff08bd2020-04-29 13:30:54 +0100211func (scope *apiScope) apiModuleName(baseName string) string {
Paul Duffindd9d0742020-05-08 15:52:37 +0100212 return baseName + ".api" + scope.moduleSuffix
Paul Duffin0ff08bd2020-04-29 13:30:54 +0100213}
214
Paul Duffin3375e352020-04-28 10:44:03 +0100215func (scope *apiScope) String() string {
216 return scope.name
217}
218
Paul Duffind1b3a922020-01-22 11:57:20 +0000219type apiScopes []*apiScope
220
221func (scopes apiScopes) Strings(accessor func(*apiScope) string) []string {
222 var list []string
223 for _, scope := range scopes {
224 list = append(list, accessor(scope))
225 }
226 return list
227}
228
Jiyong Parkc678ad32018-04-10 13:07:10 +0900229var (
Paul Duffin46dc45a2020-05-14 15:39:10 +0100230 scopeByName = make(map[string]*apiScope)
231 allScopeNames []string
Paul Duffind1b3a922020-01-22 11:57:20 +0000232 apiScopePublic = initApiScope(&apiScope{
Paul Duffin3375e352020-04-28 10:44:03 +0100233 name: "public",
234
235 // Public scope is enabled by default for both legacy and non-legacy modes.
236 legacyEnabledStatus: func(module *SdkLibrary) bool {
237 return true
238 },
239 defaultEnabledStatus: true,
240
241 scopeSpecificProperties: func(module *SdkLibrary) *ApiScopeProperties {
242 return &module.sdkLibraryProperties.Public
243 },
Paul Duffind1b3a922020-01-22 11:57:20 +0000244 sdkVersion: "current",
245 })
246 apiScopeSystem = initApiScope(&apiScope{
Paul Duffin3375e352020-04-28 10:44:03 +0100247 name: "system",
248 extends: apiScopePublic,
249 legacyEnabledStatus: (*SdkLibrary).generateTestAndSystemScopesByDefault,
250 scopeSpecificProperties: func(module *SdkLibrary) *ApiScopeProperties {
251 return &module.sdkLibraryProperties.System
252 },
Paul Duffin15f34ef2020-07-20 18:04:44 +0100253 apiFilePrefix: "system-",
254 moduleSuffix: ".system",
255 sdkVersion: "system_current",
256 annotation: "android.annotation.SystemApi(client=android.annotation.SystemApi.Client.PRIVILEGED_APPS)",
Paul Duffind1b3a922020-01-22 11:57:20 +0000257 })
258 apiScopeTest = initApiScope(&apiScope{
Paul Duffin3375e352020-04-28 10:44:03 +0100259 name: "test",
Anton Hansson4fe970f2020-10-09 10:16:49 +0100260 extends: apiScopeSystem,
Paul Duffin3375e352020-04-28 10:44:03 +0100261 legacyEnabledStatus: (*SdkLibrary).generateTestAndSystemScopesByDefault,
262 scopeSpecificProperties: func(module *SdkLibrary) *ApiScopeProperties {
263 return &module.sdkLibraryProperties.Test
264 },
Paul Duffin15f34ef2020-07-20 18:04:44 +0100265 apiFilePrefix: "test-",
266 moduleSuffix: ".test",
267 sdkVersion: "test_current",
268 annotation: "android.annotation.TestApi",
269 unstable: true,
Paul Duffind1b3a922020-01-22 11:57:20 +0000270 })
Paul Duffin8f265b92020-04-28 14:13:56 +0100271 apiScopeModuleLib = initApiScope(&apiScope{
Paul Duffin6b836ba2020-05-13 19:19:49 +0100272 name: "module-lib",
Paul Duffin8f265b92020-04-28 14:13:56 +0100273 extends: apiScopeSystem,
Paul Duffin0c5bae52020-06-02 13:00:08 +0100274 // The module-lib scope is disabled by default in legacy mode.
Paul Duffin8f265b92020-04-28 14:13:56 +0100275 //
276 // Enabling this would break existing usages.
277 legacyEnabledStatus: func(module *SdkLibrary) bool {
278 return false
279 },
280 scopeSpecificProperties: func(module *SdkLibrary) *ApiScopeProperties {
281 return &module.sdkLibraryProperties.Module_lib
282 },
283 apiFilePrefix: "module-lib-",
284 moduleSuffix: ".module_lib",
285 sdkVersion: "module_current",
Paul Duffin15f34ef2020-07-20 18:04:44 +0100286 annotation: "android.annotation.SystemApi(client=android.annotation.SystemApi.Client.MODULE_LIBRARIES)",
Paul Duffin8f265b92020-04-28 14:13:56 +0100287 })
Paul Duffin0c5bae52020-06-02 13:00:08 +0100288 apiScopeSystemServer = initApiScope(&apiScope{
289 name: "system-server",
290 extends: apiScopePublic,
291 // The system-server scope is disabled by default in legacy mode.
292 //
293 // Enabling this would break existing usages.
294 legacyEnabledStatus: func(module *SdkLibrary) bool {
295 return false
296 },
297 scopeSpecificProperties: func(module *SdkLibrary) *ApiScopeProperties {
298 return &module.sdkLibraryProperties.System_server
299 },
300 apiFilePrefix: "system-server-",
301 moduleSuffix: ".system_server",
302 sdkVersion: "system_server_current",
Paul Duffin15f34ef2020-07-20 18:04:44 +0100303 annotation: "android.annotation.SystemApi(client=android.annotation.SystemApi.Client.SYSTEM_SERVER)",
304 extraArgs: []string{
305 "--hide-annotation", "android.annotation.Hide",
Paul Duffin0c5bae52020-06-02 13:00:08 +0100306 // com.android.* classes are okay in this interface"
Paul Duffin15f34ef2020-07-20 18:04:44 +0100307 "--hide", "InternalClasses",
Paul Duffin0c5bae52020-06-02 13:00:08 +0100308 },
309 })
Paul Duffind1b3a922020-01-22 11:57:20 +0000310 allApiScopes = apiScopes{
311 apiScopePublic,
312 apiScopeSystem,
313 apiScopeTest,
Paul Duffin8f265b92020-04-28 14:13:56 +0100314 apiScopeModuleLib,
Paul Duffin0c5bae52020-06-02 13:00:08 +0100315 apiScopeSystemServer,
Paul Duffind1b3a922020-01-22 11:57:20 +0000316 }
Jiyong Parkc678ad32018-04-10 13:07:10 +0900317)
318
Jiyong Park82484c02018-04-23 21:41:26 +0900319var (
320 javaSdkLibrariesLock sync.Mutex
321)
322
Jiyong Parkc678ad32018-04-10 13:07:10 +0900323// TODO: these are big features that are currently missing
Jiyong Park1be96912018-05-28 18:02:19 +0900324// 1) disallowing linking to the runtime shared lib
325// 2) HTML generation
Jiyong Parkc678ad32018-04-10 13:07:10 +0900326
327func init() {
Paul Duffin43dc1cc2019-12-19 11:18:54 +0000328 RegisterSdkLibraryBuildComponents(android.InitRegistrationContext)
Jiyong Parkc678ad32018-04-10 13:07:10 +0900329
Jiyong Park82484c02018-04-23 21:41:26 +0900330 android.RegisterMakeVarsProvider(pctx, func(ctx android.MakeVarsContext) {
331 javaSdkLibraries := javaSdkLibraries(ctx.Config())
332 sort.Strings(*javaSdkLibraries)
333 ctx.Strict("JAVA_SDK_LIBRARIES", strings.Join(*javaSdkLibraries, " "))
334 })
Paul Duffindd46f712020-02-10 13:37:10 +0000335
336 // Register sdk member types.
337 android.RegisterSdkMemberType(&sdkLibrarySdkMemberType{
338 android.SdkMemberTypeBase{
339 PropertyName: "java_sdk_libs",
340 SupportsSdk: true,
341 },
342 })
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
Paul Duffin7a586d32019-12-30 17:09:34 +0000397 // list of package names that will be documented and publicized as API.
398 // This allows the API to be restricted to a subset of the source files provided.
399 // If this is unspecified then all the source files will be treated as being part
400 // of the API.
Jiyong Parkc678ad32018-04-10 13:07:10 +0900401 Api_packages []string
402
Jiyong Park5a2c9d72018-05-01 22:25:41 +0900403 // list of package names that must be hidden from the API
404 Hidden_api_packages []string
405
Paul Duffin749f98f2019-12-30 17:23:46 +0000406 // the relative path to the directory containing the api specification files.
407 // Defaults to "api".
408 Api_dir *string
409
Paul Duffindfa131e2020-05-15 20:37:11 +0100410 // Determines whether a runtime implementation library is built; defaults to false.
411 //
412 // If true then it also prevents the module from being used as a shared module, i.e.
413 // it is as is shared_library: false, was set.
Paul Duffin43db9be2019-12-30 17:35:49 +0000414 Api_only *bool
415
Paul Duffin11512472019-02-11 15:55:17 +0000416 // local files that are used within user customized droiddoc options.
417 Droiddoc_option_files []string
418
419 // additional droiddoc options
420 // Available variables for substitution:
421 //
422 // $(location <label>): the path to the droiddoc_option_files with name <label>
Sundong Ahndd567f92018-07-31 17:19:11 +0900423 Droiddoc_options []string
424
Paul Duffine22c2ab2020-05-20 19:35:27 +0100425 // is set to true, Metalava will allow framework SDK to contain annotations.
426 Annotations_enabled *bool
427
Sundong Ahn054b19a2018-10-19 13:46:09 +0900428 // a list of top-level directories containing files to merge qualifier annotations
429 // (i.e. those intended to be included in the stubs written) from.
430 Merge_annotations_dirs []string
431
432 // a list of top-level directories containing Java stub files to merge show/hide annotations from.
433 Merge_inclusion_annotations_dirs []string
434
435 // If set to true, the path of dist files is apistubs/core. Defaults to false.
436 Core_lib *bool
437
Sundong Ahn80a87b32019-05-13 15:02:50 +0900438 // don't create dist rules.
439 No_dist *bool `blueprint:"mutated"`
440
Paul Duffin3375e352020-04-28 10:44:03 +0100441 // indicates whether system and test apis should be generated.
442 Generate_system_and_test_apis bool `blueprint:"mutated"`
443
444 // The properties specific to the public api scope
445 //
446 // Unless explicitly specified by using public.enabled the public api scope is
447 // enabled by default in both legacy and non-legacy mode.
448 Public ApiScopeProperties
449
450 // The properties specific to the system api scope
451 //
452 // In legacy mode the system api scope is enabled by default when sdk_version
453 // is set to something other than "none".
454 //
455 // In non-legacy mode the system api scope is disabled by default.
456 System ApiScopeProperties
457
458 // The properties specific to the test api scope
459 //
460 // In legacy mode the test api scope is enabled by default when sdk_version
461 // is set to something other than "none".
462 //
463 // In non-legacy mode the test api scope is disabled by default.
464 Test ApiScopeProperties
Paul Duffin37e0b772019-12-30 17:20:10 +0000465
Paul Duffin0c5bae52020-06-02 13:00:08 +0100466 // The properties specific to the module-lib api scope
Paul Duffin8f265b92020-04-28 14:13:56 +0100467 //
Paul Duffin0c5bae52020-06-02 13:00:08 +0100468 // Unless explicitly specified by using test.enabled the module-lib api scope is
Paul Duffin8f265b92020-04-28 14:13:56 +0100469 // disabled by default.
470 Module_lib ApiScopeProperties
471
Paul Duffin0c5bae52020-06-02 13:00:08 +0100472 // The properties specific to the system-server api scope
473 //
474 // Unless explicitly specified by using test.enabled the module-lib api scope is
475 // disabled by default.
476 System_server ApiScopeProperties
477
Jiyong Park932cdfe2020-05-28 00:19:53 +0900478 // Determines if the stubs are preferred over the implementation library
479 // for linking, even when the client doesn't specify sdk_version. When this
480 // is set to true, such clients are provided with the widest API surface that
481 // this lib provides. Note however that this option doesn't affect the clients
482 // that are in the same APEX as this library. In that case, the clients are
483 // always linked with the implementation library. Default is false.
484 Default_to_stubs *bool
485
Paul Duffin160fe412020-05-10 19:32:20 +0100486 // Properties related to api linting.
487 Api_lint struct {
488 // Enable api linting.
489 Enabled *bool
490 }
491
Jiyong Parkc678ad32018-04-10 13:07:10 +0900492 // TODO: determines whether to create HTML doc or not
493 //Html_doc *bool
494}
495
Paul Duffin0f8faff2020-05-20 16:18:00 +0100496// Paths to outputs from java_sdk_library and java_sdk_library_import.
497//
498// Fields that are android.Paths are always set (during GenerateAndroidBuildActions).
499// OptionalPaths are always set by java_sdk_library but may not be set by
500// java_sdk_library_import as not all instances provide that information.
Paul Duffind1b3a922020-01-22 11:57:20 +0000501type scopePaths struct {
Paul Duffin0f8faff2020-05-20 16:18:00 +0100502 // The path (represented as Paths for convenience when returning) to the stubs header jar.
503 //
504 // That is the jar that is created by turbine.
505 stubsHeaderPath android.Paths
506
507 // The path (represented as Paths for convenience when returning) to the stubs implementation jar.
508 //
509 // This is not the implementation jar, it still only contains stubs.
510 stubsImplPath android.Paths
511
512 // The API specification file, e.g. system_current.txt.
513 currentApiFilePath android.OptionalPath
514
515 // The specification of API elements removed since the last release.
516 removedApiFilePath android.OptionalPath
517
518 // The stubs source jar.
519 stubsSrcJar android.OptionalPath
Paul Duffind1b3a922020-01-22 11:57:20 +0000520}
521
Paul Duffinc8782502020-04-29 20:45:27 +0100522func (paths *scopePaths) extractStubsLibraryInfoFromDependency(dep android.Module) error {
523 if lib, ok := dep.(Dependency); ok {
524 paths.stubsHeaderPath = lib.HeaderJars()
525 paths.stubsImplPath = lib.ImplementationJars()
526 return nil
527 } else {
528 return fmt.Errorf("expected module that implements Dependency, e.g. java_library")
529 }
530}
531
Paul Duffin0ff08bd2020-04-29 13:30:54 +0100532func (paths *scopePaths) treatDepAsApiStubsProvider(dep android.Module, action func(provider ApiStubsProvider)) error {
533 if apiStubsProvider, ok := dep.(ApiStubsProvider); ok {
534 action(apiStubsProvider)
Paul Duffinc8782502020-04-29 20:45:27 +0100535 return nil
536 } else {
537 return fmt.Errorf("expected module that implements ApiStubsProvider, e.g. droidstubs")
538 }
539}
540
Paul Duffin0f8faff2020-05-20 16:18:00 +0100541func (paths *scopePaths) treatDepAsApiStubsSrcProvider(dep android.Module, action func(provider ApiStubsSrcProvider)) error {
542 if apiStubsProvider, ok := dep.(ApiStubsSrcProvider); ok {
543 action(apiStubsProvider)
544 return nil
545 } else {
546 return fmt.Errorf("expected module that implements ApiStubsSrcProvider, e.g. droidstubs")
547 }
548}
549
Paul Duffin0ff08bd2020-04-29 13:30:54 +0100550func (paths *scopePaths) extractApiInfoFromApiStubsProvider(provider ApiStubsProvider) {
Paul Duffin0f8faff2020-05-20 16:18:00 +0100551 paths.currentApiFilePath = android.OptionalPathForPath(provider.ApiFilePath())
552 paths.removedApiFilePath = android.OptionalPathForPath(provider.RemovedApiFilePath())
Paul Duffin0ff08bd2020-04-29 13:30:54 +0100553}
554
555func (paths *scopePaths) extractApiInfoFromDep(dep android.Module) error {
556 return paths.treatDepAsApiStubsProvider(dep, func(provider ApiStubsProvider) {
557 paths.extractApiInfoFromApiStubsProvider(provider)
558 })
559}
560
Paul Duffin0f8faff2020-05-20 16:18:00 +0100561func (paths *scopePaths) extractStubsSourceInfoFromApiStubsProviders(provider ApiStubsSrcProvider) {
562 paths.stubsSrcJar = android.OptionalPathForPath(provider.StubsSrcJar())
Paul Duffin0ff08bd2020-04-29 13:30:54 +0100563}
564
565func (paths *scopePaths) extractStubsSourceInfoFromDep(dep android.Module) error {
Paul Duffin0f8faff2020-05-20 16:18:00 +0100566 return paths.treatDepAsApiStubsSrcProvider(dep, func(provider ApiStubsSrcProvider) {
Paul Duffin0ff08bd2020-04-29 13:30:54 +0100567 paths.extractStubsSourceInfoFromApiStubsProviders(provider)
568 })
569}
570
571func (paths *scopePaths) extractStubsSourceAndApiInfoFromApiStubsProvider(dep android.Module) error {
572 return paths.treatDepAsApiStubsProvider(dep, func(provider ApiStubsProvider) {
573 paths.extractApiInfoFromApiStubsProvider(provider)
574 paths.extractStubsSourceInfoFromApiStubsProviders(provider)
575 })
576}
577
578type commonToSdkLibraryAndImportProperties struct {
Paul Duffin1b1e8062020-05-08 13:44:43 +0100579 // The naming scheme to use for the components that this module creates.
580 //
Paul Duffinee9ad5d2020-09-11 13:04:05 +0100581 // If not specified then it defaults to "default".
Paul Duffin1b1e8062020-05-08 13:44:43 +0100582 //
583 // This is a temporary mechanism to simplify conversion from separate modules for each
584 // component that follow a different naming pattern to the default one.
585 //
586 // TODO(b/155480189) - Remove once naming inconsistencies have been resolved.
Paul Duffin0ff08bd2020-04-29 13:30:54 +0100587 Naming_scheme *string
Paul Duffindfa131e2020-05-15 20:37:11 +0100588
589 // Specifies whether this module can be used as an Android shared library; defaults
590 // to true.
591 //
592 // An Android shared library is one that can be referenced in a <uses-library> element
593 // in an AndroidManifest.xml.
594 Shared_library *bool
Paul Duffina2ae7e02020-09-11 11:55:00 +0100595
596 // Files containing information about supported java doc tags.
597 Doctag_files []string `android:"path"`
Paul Duffin0ff08bd2020-04-29 13:30:54 +0100598}
599
Paul Duffin56d44902020-01-31 13:36:25 +0000600// Common code between sdk library and sdk library import
601type commonToSdkLibraryAndImport struct {
Paul Duffinc3091c82020-05-08 14:16:20 +0100602 moduleBase *android.ModuleBase
603
Paul Duffin56d44902020-01-31 13:36:25 +0000604 scopePaths map[*apiScope]*scopePaths
Paul Duffin1b1e8062020-05-08 13:44:43 +0100605
606 namingScheme sdkLibraryComponentNamingScheme
607
Paul Duffindfa131e2020-05-15 20:37:11 +0100608 commonSdkLibraryProperties commonToSdkLibraryAndImportProperties
Paul Duffin859fe962020-05-15 10:20:31 +0100609
Paul Duffina2ae7e02020-09-11 11:55:00 +0100610 // Paths to commonSdkLibraryProperties.Doctag_files
611 doctagPaths android.Paths
612
Paul Duffin859fe962020-05-15 10:20:31 +0100613 // Functionality related to this being used as a component of a java_sdk_library.
614 EmbeddableSdkLibraryComponent
Paul Duffin56d44902020-01-31 13:36:25 +0000615}
616
Paul Duffinc3091c82020-05-08 14:16:20 +0100617func (c *commonToSdkLibraryAndImport) initCommon(moduleBase *android.ModuleBase) {
618 c.moduleBase = moduleBase
Paul Duffin1b1e8062020-05-08 13:44:43 +0100619
Paul Duffindfa131e2020-05-15 20:37:11 +0100620 moduleBase.AddProperties(&c.commonSdkLibraryProperties)
Paul Duffin859fe962020-05-15 10:20:31 +0100621
622 // Initialize this as an sdk library component.
623 c.initSdkLibraryComponent(moduleBase)
Paul Duffin1b1e8062020-05-08 13:44:43 +0100624}
625
626func (c *commonToSdkLibraryAndImport) initCommonAfterDefaultsApplied(ctx android.DefaultableHookContext) bool {
Paul Duffindfa131e2020-05-15 20:37:11 +0100627 schemeProperty := proptools.StringDefault(c.commonSdkLibraryProperties.Naming_scheme, "default")
Paul Duffin1b1e8062020-05-08 13:44:43 +0100628 switch schemeProperty {
629 case "default":
630 c.namingScheme = &defaultNamingScheme{}
631 default:
632 ctx.PropertyErrorf("naming_scheme", "expected 'default' but was %q", schemeProperty)
633 return false
634 }
635
Paul Duffindfa131e2020-05-15 20:37:11 +0100636 // Only track this sdk library if this can be used as a shared library.
637 if c.sharedLibrary() {
638 // Use the name specified in the module definition as the owner.
639 c.sdkLibraryComponentProperties.SdkLibraryToImplicitlyTrack = proptools.StringPtr(c.moduleBase.BaseModuleName())
640 }
Paul Duffin859fe962020-05-15 10:20:31 +0100641
Paul Duffin1b1e8062020-05-08 13:44:43 +0100642 return true
Paul Duffinc3091c82020-05-08 14:16:20 +0100643}
644
Paul Duffina2ae7e02020-09-11 11:55:00 +0100645func (c *commonToSdkLibraryAndImport) generateCommonBuildActions(ctx android.ModuleContext) {
646 c.doctagPaths = android.PathsForModuleSrc(ctx, c.commonSdkLibraryProperties.Doctag_files)
647}
648
Paul Duffineedc5d52020-06-12 17:46:39 +0100649// Module name of the runtime implementation library
650func (c *commonToSdkLibraryAndImport) implLibraryModuleName() string {
651 return c.moduleBase.BaseModuleName() + ".impl"
652}
653
654// Module name of the XML file for the lib
655func (c *commonToSdkLibraryAndImport) xmlPermissionsModuleName() string {
656 return c.moduleBase.BaseModuleName() + sdkXmlFileSuffix
657}
658
Paul Duffinc3091c82020-05-08 14:16:20 +0100659// Name of the java_library module that compiles the stubs source.
660func (c *commonToSdkLibraryAndImport) stubsLibraryModuleName(apiScope *apiScope) string {
Paul Duffin1b1e8062020-05-08 13:44:43 +0100661 return c.namingScheme.stubsLibraryModuleName(apiScope, c.moduleBase.BaseModuleName())
Paul Duffinc3091c82020-05-08 14:16:20 +0100662}
663
664// Name of the droidstubs module that generates the stubs source and may also
665// generate/check the API.
666func (c *commonToSdkLibraryAndImport) stubsSourceModuleName(apiScope *apiScope) string {
Paul Duffin1b1e8062020-05-08 13:44:43 +0100667 return c.namingScheme.stubsSourceModuleName(apiScope, c.moduleBase.BaseModuleName())
Paul Duffinc3091c82020-05-08 14:16:20 +0100668}
669
670// Name of the droidstubs module that generates/checks the API. Only used if it
671// requires different arts to the stubs source generating module.
672func (c *commonToSdkLibraryAndImport) apiModuleName(apiScope *apiScope) string {
Paul Duffin1b1e8062020-05-08 13:44:43 +0100673 return c.namingScheme.apiModuleName(apiScope, c.moduleBase.BaseModuleName())
Paul Duffinc3091c82020-05-08 14:16:20 +0100674}
675
Paul Duffin46dc45a2020-05-14 15:39:10 +0100676// The component names for different outputs of the java_sdk_library.
677//
678// They are similar to the names used for the child modules it creates
679const (
680 stubsSourceComponentName = "stubs.source"
681
682 apiTxtComponentName = "api.txt"
683
684 removedApiTxtComponentName = "removed-api.txt"
685)
686
687// A regular expression to match tags that reference a specific stubs component.
688//
689// It will only match if given a valid scope and a valid component. It is verfy strict
690// to ensure it does not accidentally match a similar looking tag that should be processed
691// by the embedded Library.
692var tagSplitter = func() *regexp.Regexp {
693 // Given a list of literal string items returns a regular expression that will
694 // match any one of the items.
695 choice := func(items ...string) string {
696 return `\Q` + strings.Join(items, `\E|\Q`) + `\E`
697 }
698
699 // Regular expression to match one of the scopes.
700 scopesRegexp := choice(allScopeNames...)
701
702 // Regular expression to match one of the components.
703 componentsRegexp := choice(stubsSourceComponentName, apiTxtComponentName, removedApiTxtComponentName)
704
705 // Regular expression to match any combination of one scope and one component.
706 return regexp.MustCompile(fmt.Sprintf(`^\.(%s)\.(%s)$`, scopesRegexp, componentsRegexp))
707}()
708
709// For OutputFileProducer interface
710//
711// .<scope>.stubs.source
712// .<scope>.api.txt
713// .<scope>.removed-api.txt
714func (c *commonToSdkLibraryAndImport) commonOutputFiles(tag string) (android.Paths, error) {
715 if groups := tagSplitter.FindStringSubmatch(tag); groups != nil {
716 scopeName := groups[1]
717 component := groups[2]
718
719 if scope, ok := scopeByName[scopeName]; ok {
720 paths := c.findScopePaths(scope)
721 if paths == nil {
722 return nil, fmt.Errorf("%q does not provide api scope %s", c.moduleBase.BaseModuleName(), scopeName)
723 }
724
725 switch component {
726 case stubsSourceComponentName:
727 if paths.stubsSrcJar.Valid() {
728 return android.Paths{paths.stubsSrcJar.Path()}, nil
729 }
730
731 case apiTxtComponentName:
732 if paths.currentApiFilePath.Valid() {
733 return android.Paths{paths.currentApiFilePath.Path()}, nil
734 }
735
736 case removedApiTxtComponentName:
737 if paths.removedApiFilePath.Valid() {
738 return android.Paths{paths.removedApiFilePath.Path()}, nil
739 }
740 }
741
742 return nil, fmt.Errorf("%s not available for api scope %s", component, scopeName)
743 } else {
744 return nil, fmt.Errorf("unknown scope %s in %s", scope, tag)
745 }
746
747 } else {
Paul Duffina2ae7e02020-09-11 11:55:00 +0100748 switch tag {
749 case ".doctags":
750 if c.doctagPaths != nil {
751 return c.doctagPaths, nil
752 } else {
753 return nil, fmt.Errorf("no doctag_files specified on %s", c.moduleBase.BaseModuleName())
754 }
755 }
Paul Duffin46dc45a2020-05-14 15:39:10 +0100756 return nil, nil
757 }
758}
759
Paul Duffin803a9562020-05-20 11:52:25 +0100760func (c *commonToSdkLibraryAndImport) getScopePathsCreateIfNeeded(scope *apiScope) *scopePaths {
Paul Duffin56d44902020-01-31 13:36:25 +0000761 if c.scopePaths == nil {
762 c.scopePaths = make(map[*apiScope]*scopePaths)
763 }
764 paths := c.scopePaths[scope]
765 if paths == nil {
766 paths = &scopePaths{}
767 c.scopePaths[scope] = paths
768 }
769
770 return paths
771}
772
Paul Duffin803a9562020-05-20 11:52:25 +0100773func (c *commonToSdkLibraryAndImport) findScopePaths(scope *apiScope) *scopePaths {
774 if c.scopePaths == nil {
775 return nil
776 }
777
778 return c.scopePaths[scope]
779}
780
781// If this does not support the requested api scope then find the closest available
782// scope it does support. Returns nil if no such scope is available.
783func (c *commonToSdkLibraryAndImport) findClosestScopePath(scope *apiScope) *scopePaths {
784 for s := scope; s != nil; s = s.extends {
785 if paths := c.findScopePaths(s); paths != nil {
786 return paths
787 }
788 }
789
790 // This should never happen outside tests as public should be the base scope for every
791 // scope and is enabled by default.
792 return nil
793}
794
Paul Duffin23970f42020-05-20 14:20:02 +0100795func (c *commonToSdkLibraryAndImport) selectHeaderJarsForSdkVersion(ctx android.BaseModuleContext, sdkVersion sdkSpec) android.Paths {
Paul Duffinb05d4292020-05-20 12:19:10 +0100796
797 // If a specific numeric version has been requested then use prebuilt versions of the sdk.
798 if sdkVersion.version.isNumbered() {
799 return PrebuiltJars(ctx, c.moduleBase.BaseModuleName(), sdkVersion)
800 }
801
802 var apiScope *apiScope
803 switch sdkVersion.kind {
804 case sdkSystem:
805 apiScope = apiScopeSystem
Paul Duffin803a9562020-05-20 11:52:25 +0100806 case sdkModule:
807 apiScope = apiScopeModuleLib
Paul Duffinb05d4292020-05-20 12:19:10 +0100808 case sdkTest:
809 apiScope = apiScopeTest
Paul Duffin0c5bae52020-06-02 13:00:08 +0100810 case sdkSystemServer:
811 apiScope = apiScopeSystemServer
Paul Duffinb05d4292020-05-20 12:19:10 +0100812 default:
813 apiScope = apiScopePublic
814 }
815
Paul Duffin803a9562020-05-20 11:52:25 +0100816 paths := c.findClosestScopePath(apiScope)
817 if paths == nil {
818 var scopes []string
819 for _, s := range allApiScopes {
820 if c.findScopePaths(s) != nil {
821 scopes = append(scopes, s.name)
822 }
823 }
824 ctx.ModuleErrorf("requires api scope %s from %s but it only has %q available", apiScope.name, c.moduleBase.BaseModuleName(), scopes)
825 return nil
826 }
827
Paul Duffin23970f42020-05-20 14:20:02 +0100828 return paths.stubsHeaderPath
Paul Duffinb05d4292020-05-20 12:19:10 +0100829}
830
Paul Duffin859fe962020-05-15 10:20:31 +0100831func (c *commonToSdkLibraryAndImport) sdkComponentPropertiesForChildLibrary() interface{} {
832 componentProps := &struct {
833 SdkLibraryToImplicitlyTrack *string
Paul Duffindfa131e2020-05-15 20:37:11 +0100834 }{}
835
836 if c.sharedLibrary() {
Paul Duffin859fe962020-05-15 10:20:31 +0100837 // Mark the stubs library as being components of this java_sdk_library so that
838 // any app that includes code which depends (directly or indirectly) on the stubs
839 // library will have the appropriate <uses-library> invocation inserted into its
840 // manifest if necessary.
Paul Duffindfa131e2020-05-15 20:37:11 +0100841 componentProps.SdkLibraryToImplicitlyTrack = proptools.StringPtr(c.moduleBase.BaseModuleName())
Paul Duffin859fe962020-05-15 10:20:31 +0100842 }
843
844 return componentProps
845}
846
Paul Duffindfa131e2020-05-15 20:37:11 +0100847// Check if this can be used as a shared library.
848func (c *commonToSdkLibraryAndImport) sharedLibrary() bool {
849 return proptools.BoolDefault(c.commonSdkLibraryProperties.Shared_library, true)
850}
851
Paul Duffin859fe962020-05-15 10:20:31 +0100852// Properties related to the use of a module as an component of a java_sdk_library.
853type SdkLibraryComponentProperties struct {
854
855 // The name of the java_sdk_library/_import to add to a <uses-library> entry
856 // in the AndroidManifest.xml of any Android app that includes code that references
857 // this module. If not set then no java_sdk_library/_import is tracked.
858 SdkLibraryToImplicitlyTrack *string `blueprint:"mutated"`
859}
860
861// Structure to be embedded in a module struct that needs to support the
862// SdkLibraryComponentDependency interface.
863type EmbeddableSdkLibraryComponent struct {
864 sdkLibraryComponentProperties SdkLibraryComponentProperties
865}
866
867func (e *EmbeddableSdkLibraryComponent) initSdkLibraryComponent(moduleBase *android.ModuleBase) {
868 moduleBase.AddProperties(&e.sdkLibraryComponentProperties)
869}
870
871// to satisfy SdkLibraryComponentDependency
Ulya Trafimovich31e444e2020-08-14 17:32:16 +0100872func (e *EmbeddableSdkLibraryComponent) OptionalImplicitSdkLibrary() *string {
873 return e.sdkLibraryComponentProperties.SdkLibraryToImplicitlyTrack
Paul Duffin859fe962020-05-15 10:20:31 +0100874}
875
Ulya Trafimovich39b437b2020-09-23 16:42:35 +0100876// to satisfy SdkLibraryComponentDependency
877func (e *EmbeddableSdkLibraryComponent) OptionalSdkLibraryImplementation() *string {
878 // Currently implementation library name is the same as the SDK library name.
879 return e.sdkLibraryComponentProperties.SdkLibraryToImplicitlyTrack
880}
881
Paul Duffin859fe962020-05-15 10:20:31 +0100882// Implemented by modules that are (or possibly could be) a component of a java_sdk_library
883// (including the java_sdk_library) itself.
884type SdkLibraryComponentDependency interface {
Ulya Trafimovich31e444e2020-08-14 17:32:16 +0100885 UsesLibraryDependency
886
Paul Duffin859fe962020-05-15 10:20:31 +0100887 // The optional name of the sdk library that should be implicitly added to the
888 // AndroidManifest of an app that contains code which references the sdk library.
889 //
Ulya Trafimovich31e444e2020-08-14 17:32:16 +0100890 // Returns the name of the optional implicit SDK library or nil, if there isn't one.
891 OptionalImplicitSdkLibrary() *string
Ulya Trafimovich39b437b2020-09-23 16:42:35 +0100892
893 // The name of the implementation library for the optional SDK library or nil, if there isn't one.
894 OptionalSdkLibraryImplementation() *string
Paul Duffin859fe962020-05-15 10:20:31 +0100895}
896
897// Make sure that all the module types that are components of java_sdk_library/_import
898// and which can be referenced (directly or indirectly) from an android app implement
899// the SdkLibraryComponentDependency interface.
900var _ SdkLibraryComponentDependency = (*Library)(nil)
901var _ SdkLibraryComponentDependency = (*Import)(nil)
902var _ SdkLibraryComponentDependency = (*SdkLibrary)(nil)
Paul Duffineedc5d52020-06-12 17:46:39 +0100903var _ SdkLibraryComponentDependency = (*SdkLibraryImport)(nil)
Paul Duffin859fe962020-05-15 10:20:31 +0100904
905// Provides access to sdk_version related header and implentation jars.
906type SdkLibraryDependency interface {
907 SdkLibraryComponentDependency
908
909 // Get the header jars appropriate for the supplied sdk_version.
910 //
911 // These are turbine generated jars so they only change if the externals of the
912 // class changes but it does not contain and implementation or JavaDoc.
913 SdkHeaderJars(ctx android.BaseModuleContext, sdkVersion sdkSpec) android.Paths
914
915 // Get the implementation jars appropriate for the supplied sdk version.
916 //
917 // These are either the implementation jar for the whole sdk library or the implementation
918 // jars for the stubs. The latter should only be needed when generating JavaDoc as otherwise
919 // they are identical to the corresponding header jars.
920 SdkImplementationJars(ctx android.BaseModuleContext, sdkVersion sdkSpec) android.Paths
921}
922
Inseob Kimc0907f12019-02-08 21:00:45 +0900923type SdkLibrary struct {
Sundong Ahn054b19a2018-10-19 13:46:09 +0900924 Library
Jiyong Parkc678ad32018-04-10 13:07:10 +0900925
Sundong Ahn054b19a2018-10-19 13:46:09 +0900926 sdkLibraryProperties sdkLibraryProperties
Jiyong Parkc678ad32018-04-10 13:07:10 +0900927
Paul Duffin3375e352020-04-28 10:44:03 +0100928 // Map from api scope to the scope specific property structure.
929 scopeToProperties map[*apiScope]*ApiScopeProperties
930
Paul Duffin56d44902020-01-31 13:36:25 +0000931 commonToSdkLibraryAndImport
Jiyong Parkc678ad32018-04-10 13:07:10 +0900932}
933
Inseob Kimc0907f12019-02-08 21:00:45 +0900934var _ Dependency = (*SdkLibrary)(nil)
935var _ SdkLibraryDependency = (*SdkLibrary)(nil)
Colin Cross897d2ed2019-02-11 14:03:51 -0800936
Paul Duffin3375e352020-04-28 10:44:03 +0100937func (module *SdkLibrary) generateTestAndSystemScopesByDefault() bool {
938 return module.sdkLibraryProperties.Generate_system_and_test_apis
939}
940
941func (module *SdkLibrary) getGeneratedApiScopes(ctx android.EarlyModuleContext) apiScopes {
942 // Check to see if any scopes have been explicitly enabled. If any have then all
943 // must be.
944 anyScopesExplicitlyEnabled := false
945 for _, scope := range allApiScopes {
946 scopeProperties := module.scopeToProperties[scope]
947 if scopeProperties.Enabled != nil {
948 anyScopesExplicitlyEnabled = true
949 break
950 }
Paul Duffind1b3a922020-01-22 11:57:20 +0000951 }
Paul Duffin3375e352020-04-28 10:44:03 +0100952
953 var generatedScopes apiScopes
954 enabledScopes := make(map[*apiScope]struct{})
955 for _, scope := range allApiScopes {
956 scopeProperties := module.scopeToProperties[scope]
957 // If any scopes are explicitly enabled then ignore the legacy enabled status.
958 // This is to ensure that any new usages of this module type do not rely on legacy
959 // behaviour.
960 defaultEnabledStatus := false
961 if anyScopesExplicitlyEnabled {
962 defaultEnabledStatus = scope.defaultEnabledStatus
963 } else {
964 defaultEnabledStatus = scope.legacyEnabledStatus(module)
965 }
966 enabled := proptools.BoolDefault(scopeProperties.Enabled, defaultEnabledStatus)
967 if enabled {
968 enabledScopes[scope] = struct{}{}
969 generatedScopes = append(generatedScopes, scope)
970 }
971 }
972
973 // Now check to make sure that any scope that is extended by an enabled scope is also
974 // enabled.
975 for _, scope := range allApiScopes {
976 if _, ok := enabledScopes[scope]; ok {
977 extends := scope.extends
978 if extends != nil {
979 if _, ok := enabledScopes[extends]; !ok {
980 ctx.ModuleErrorf("enabled api scope %q depends on disabled scope %q", scope, extends)
981 }
982 }
983 }
984 }
985
986 return generatedScopes
Paul Duffind1b3a922020-01-22 11:57:20 +0000987}
988
Paul Duffineedc5d52020-06-12 17:46:39 +0100989type sdkLibraryComponentTag struct {
990 blueprint.BaseDependencyTag
991 name string
992}
993
994// Mark this tag so dependencies that use it are excluded from visibility enforcement.
995func (t sdkLibraryComponentTag) ExcludeFromVisibilityEnforcement() {}
996
997var xmlPermissionsFileTag = sdkLibraryComponentTag{name: "xml-permissions-file"}
Paul Duffine74ac732020-02-06 13:51:46 +0000998
Jiyong Parke3833882020-02-17 17:28:10 +0900999func IsXmlPermissionsFileDepTag(depTag blueprint.DependencyTag) bool {
Paul Duffineedc5d52020-06-12 17:46:39 +01001000 if dt, ok := depTag.(sdkLibraryComponentTag); ok {
Jiyong Parke3833882020-02-17 17:28:10 +09001001 return dt == xmlPermissionsFileTag
1002 }
1003 return false
1004}
1005
Paul Duffineedc5d52020-06-12 17:46:39 +01001006var implLibraryTag = sdkLibraryComponentTag{name: "impl-library"}
Paul Duffin5df79302020-05-16 15:52:12 +01001007
Paul Duffin44f1d842020-06-26 20:17:02 +01001008// Add the dependencies on the child modules in the component deps mutator.
1009func (module *SdkLibrary) ComponentDepsMutator(ctx android.BottomUpMutatorContext) {
Paul Duffin3375e352020-04-28 10:44:03 +01001010 for _, apiScope := range module.getGeneratedApiScopes(ctx) {
Paul Duffind1b3a922020-01-22 11:57:20 +00001011 // Add dependencies to the stubs library
Paul Duffinc3091c82020-05-08 14:16:20 +01001012 ctx.AddVariationDependencies(nil, apiScope.stubsTag, module.stubsLibraryModuleName(apiScope))
Paul Duffind1b3a922020-01-22 11:57:20 +00001013
Paul Duffin15f34ef2020-07-20 18:04:44 +01001014 // Add a dependency on the stubs source in order to access both stubs source and api information.
1015 ctx.AddVariationDependencies(nil, apiScope.stubsSourceAndApiTag, module.stubsSourceModuleName(apiScope))
Sundong Ahn054b19a2018-10-19 13:46:09 +09001016 }
1017
Paul Duffindfa131e2020-05-15 20:37:11 +01001018 if module.requiresRuntimeImplementationLibrary() {
Paul Duffin5df79302020-05-16 15:52:12 +01001019 // Add dependency to the rule for generating the implementation library.
1020 ctx.AddDependency(module, implLibraryTag, module.implLibraryModuleName())
1021
Paul Duffindfa131e2020-05-15 20:37:11 +01001022 if module.sharedLibrary() {
1023 // Add dependency to the rule for generating the xml permissions file
Paul Duffineedc5d52020-06-12 17:46:39 +01001024 ctx.AddDependency(module, xmlPermissionsFileTag, module.xmlPermissionsModuleName())
Paul Duffindfa131e2020-05-15 20:37:11 +01001025 }
Paul Duffin44f1d842020-06-26 20:17:02 +01001026 }
1027}
Paul Duffine74ac732020-02-06 13:51:46 +00001028
Paul Duffin44f1d842020-06-26 20:17:02 +01001029// Add other dependencies as normal.
1030func (module *SdkLibrary) DepsMutator(ctx android.BottomUpMutatorContext) {
1031 if module.requiresRuntimeImplementationLibrary() {
Paul Duffindfa131e2020-05-15 20:37:11 +01001032 // Only add the deps for the library if it is actually going to be built.
1033 module.Library.deps(ctx)
1034 }
Jiyong Parkc678ad32018-04-10 13:07:10 +09001035}
1036
Paul Duffin46dc45a2020-05-14 15:39:10 +01001037func (module *SdkLibrary) OutputFiles(tag string) (android.Paths, error) {
1038 paths, err := module.commonOutputFiles(tag)
1039 if paths == nil && err == nil {
1040 return module.Library.OutputFiles(tag)
1041 } else {
1042 return paths, err
1043 }
1044}
1045
Inseob Kimc0907f12019-02-08 21:00:45 +09001046func (module *SdkLibrary) GenerateAndroidBuildActions(ctx android.ModuleContext) {
Paul Duffina2ae7e02020-09-11 11:55:00 +01001047 module.generateCommonBuildActions(ctx)
1048
Paul Duffindfa131e2020-05-15 20:37:11 +01001049 // Only build an implementation library if required.
1050 if module.requiresRuntimeImplementationLibrary() {
Paul Duffin43db9be2019-12-30 17:35:49 +00001051 module.Library.GenerateAndroidBuildActions(ctx)
1052 }
Sundong Ahn054b19a2018-10-19 13:46:09 +09001053
Sundong Ahn57368eb2018-07-06 11:20:23 +09001054 // Record the paths to the header jars of the library (stubs and impl).
Paul Duffind1b3a922020-01-22 11:57:20 +00001055 // When this java_sdk_library is depended upon from others via "libs" property,
Jiyong Parkc678ad32018-04-10 13:07:10 +09001056 // the recorded paths will be returned depending on the link type of the caller.
1057 ctx.VisitDirectDeps(func(to android.Module) {
Jiyong Parkc678ad32018-04-10 13:07:10 +09001058 tag := ctx.OtherModuleDependencyTag(to)
1059
Paul Duffinc8782502020-04-29 20:45:27 +01001060 // Extract information from any of the scope specific dependencies.
1061 if scopeTag, ok := tag.(scopeDependencyTag); ok {
1062 apiScope := scopeTag.apiScope
Paul Duffin803a9562020-05-20 11:52:25 +01001063 scopePaths := module.getScopePathsCreateIfNeeded(apiScope)
Paul Duffinc8782502020-04-29 20:45:27 +01001064
1065 // Extract information from the dependency. The exact information extracted
1066 // is determined by the nature of the dependency which is determined by the tag.
1067 scopeTag.extractDepInfo(ctx, to, scopePaths)
Sundong Ahn20e998b2018-07-24 11:19:26 +09001068 }
Jiyong Parkc678ad32018-04-10 13:07:10 +09001069 })
1070}
1071
Jiyong Park0b0e1b92019-12-03 13:24:29 +09001072func (module *SdkLibrary) AndroidMkEntries() []android.AndroidMkEntries {
Paul Duffindfa131e2020-05-15 20:37:11 +01001073 if !module.requiresRuntimeImplementationLibrary() {
Paul Duffin43db9be2019-12-30 17:35:49 +00001074 return nil
1075 }
Jiyong Park0b0e1b92019-12-03 13:24:29 +09001076 entriesList := module.Library.AndroidMkEntries()
Yo Chiang07d75072020-06-05 17:43:19 +08001077 if module.sharedLibrary() {
1078 entries := &entriesList[0]
1079 entries.Required = append(entries.Required, module.xmlPermissionsModuleName())
1080 }
Jiyong Park0b0e1b92019-12-03 13:24:29 +09001081 return entriesList
Jiyong Park82484c02018-04-23 21:41:26 +09001082}
1083
Anton Hansson5fd5d242020-03-27 19:43:19 +00001084// The dist path of the stub artifacts
1085func (module *SdkLibrary) apiDistPath(apiScope *apiScope) string {
1086 if module.ModuleBase.Owner() != "" {
1087 return path.Join("apistubs", module.ModuleBase.Owner(), apiScope.name)
1088 } else if Bool(module.sdkLibraryProperties.Core_lib) {
1089 return path.Join("apistubs", "core", apiScope.name)
1090 } else {
1091 return path.Join("apistubs", "android", apiScope.name)
1092 }
1093}
1094
Paul Duffin12ceb462019-12-24 20:31:31 +00001095// Get the sdk version for use when compiling the stubs library.
Paul Duffin780c5f42020-05-12 15:52:55 +01001096func (module *SdkLibrary) sdkVersionForStubsLibrary(mctx android.EarlyModuleContext, apiScope *apiScope) string {
Paul Duffin87a05a32020-05-12 11:50:28 +01001097 scopeProperties := module.scopeToProperties[apiScope]
1098 if scopeProperties.Sdk_version != nil {
1099 return proptools.String(scopeProperties.Sdk_version)
1100 }
1101
Paul Duffin12ceb462019-12-24 20:31:31 +00001102 sdkDep := decodeSdkDep(mctx, sdkContext(&module.Library))
1103 if sdkDep.hasStandardLibs() {
1104 // If building against a standard sdk then use the sdk version appropriate for the scope.
Paul Duffind1b3a922020-01-22 11:57:20 +00001105 return apiScope.sdkVersion
Paul Duffin12ceb462019-12-24 20:31:31 +00001106 } else {
1107 // Otherwise, use no system module.
1108 return "none"
1109 }
1110}
1111
Paul Duffind1b3a922020-01-22 11:57:20 +00001112func (module *SdkLibrary) latestApiFilegroupName(apiScope *apiScope) string {
1113 return ":" + module.BaseModuleName() + ".api." + apiScope.name + ".latest"
Jiyong Park58c518b2018-05-12 22:29:12 +09001114}
Jiyong Parkc678ad32018-04-10 13:07:10 +09001115
Paul Duffind1b3a922020-01-22 11:57:20 +00001116func (module *SdkLibrary) latestRemovedApiFilegroupName(apiScope *apiScope) string {
1117 return ":" + module.BaseModuleName() + "-removed.api." + apiScope.name + ".latest"
Jiyong Parkc678ad32018-04-10 13:07:10 +09001118}
1119
Anton Hansson944e77d2020-08-19 11:40:22 +01001120func childModuleVisibility(childVisibility []string) []string {
1121 if childVisibility == nil {
1122 // No child visibility set. The child will use the visibility of the sdk_library.
1123 return nil
1124 }
1125
1126 // Prepend an override to ignore the sdk_library's visibility, and rely on the child visibility.
1127 var visibility []string
1128 visibility = append(visibility, "//visibility:override")
1129 visibility = append(visibility, childVisibility...)
1130 return visibility
1131}
1132
Paul Duffin5df79302020-05-16 15:52:12 +01001133// Creates the implementation java library
1134func (module *SdkLibrary) createImplLibrary(mctx android.DefaultableHookContext) {
Paul Duffina2058f82020-06-24 16:22:38 +01001135 moduleNamePtr := proptools.StringPtr(module.BaseModuleName())
1136
Anton Hansson944e77d2020-08-19 11:40:22 +01001137 visibility := childModuleVisibility(module.sdkLibraryProperties.Impl_library_visibility)
1138
Paul Duffin5df79302020-05-16 15:52:12 +01001139 props := struct {
Paul Duffina2058f82020-06-24 16:22:38 +01001140 Name *string
1141 Visibility []string
1142 Instrument bool
Anton Hansson7f66efa2020-10-08 14:47:23 +01001143 Libs []string
Paul Duffina2058f82020-06-24 16:22:38 +01001144 ConfigurationName *string
Paul Duffin5df79302020-05-16 15:52:12 +01001145 }{
1146 Name: proptools.StringPtr(module.implLibraryModuleName()),
Anton Hansson944e77d2020-08-19 11:40:22 +01001147 Visibility: visibility,
Paul Duffin296cf332020-06-18 21:09:55 +01001148 // Set the instrument property to ensure it is instrumented when instrumentation is required.
1149 Instrument: true,
Anton Hansson7f66efa2020-10-08 14:47:23 +01001150 // Set the impl_only libs. Note that the module's "Libs" get appended as well, via the
1151 // addition of &module.properties below.
1152 Libs: module.sdkLibraryProperties.Impl_only_libs,
Paul Duffina2058f82020-06-24 16:22:38 +01001153
1154 // Make the created library behave as if it had the same name as this module.
1155 ConfigurationName: moduleNamePtr,
Paul Duffin5df79302020-05-16 15:52:12 +01001156 }
1157
1158 properties := []interface{}{
1159 &module.properties,
1160 &module.protoProperties,
1161 &module.deviceProperties,
Liz Kammera7a64f32020-07-09 15:16:41 -07001162 &module.dexProperties,
Paul Duffin5df79302020-05-16 15:52:12 +01001163 &module.dexpreoptProperties,
Colin Cross014489c2020-06-02 20:09:13 -07001164 &module.linter.properties,
Paul Duffin5df79302020-05-16 15:52:12 +01001165 &props,
1166 module.sdkComponentPropertiesForChildLibrary(),
1167 }
1168 mctx.CreateModule(LibraryFactory, properties...)
1169}
1170
Jiyong Parkc678ad32018-04-10 13:07:10 +09001171// Creates a static java library that has API stubs
Paul Duffinf0229202020-04-29 16:47:28 +01001172func (module *SdkLibrary) createStubsLibrary(mctx android.DefaultableHookContext, apiScope *apiScope) {
Jiyong Parkc678ad32018-04-10 13:07:10 +09001173 props := struct {
Dan Willemsen9f435972020-05-28 15:28:00 -07001174 Name *string
1175 Visibility []string
1176 Srcs []string
1177 Installable *bool
1178 Sdk_version *string
1179 System_modules *string
1180 Patch_module *string
1181 Libs []string
1182 Compile_dex *bool
1183 Java_version *string
1184 Openjdk9 struct {
Sundong Ahn054b19a2018-10-19 13:46:09 +09001185 Srcs []string
1186 Javacflags []string
1187 }
Anton Hansson5fd5d242020-03-27 19:43:19 +00001188 Dist struct {
1189 Targets []string
1190 Dest *string
1191 Dir *string
1192 Tag *string
1193 }
Jiyong Parkc678ad32018-04-10 13:07:10 +09001194 }{}
1195
Paul Duffinc3091c82020-05-08 14:16:20 +01001196 props.Name = proptools.StringPtr(module.stubsLibraryModuleName(apiScope))
Anton Hansson944e77d2020-08-19 11:40:22 +01001197 props.Visibility = childModuleVisibility(module.sdkLibraryProperties.Stubs_library_visibility)
Jiyong Parkc678ad32018-04-10 13:07:10 +09001198 // sources are generated from the droiddoc
Paul Duffinc3091c82020-05-08 14:16:20 +01001199 props.Srcs = []string{":" + module.stubsSourceModuleName(apiScope)}
Paul Duffin12ceb462019-12-24 20:31:31 +00001200 sdkVersion := module.sdkVersionForStubsLibrary(mctx, apiScope)
Paul Duffin52d398a2019-06-11 12:31:14 +01001201 props.Sdk_version = proptools.StringPtr(sdkVersion)
Paul Duffina18abc22020-05-16 18:54:24 +01001202 props.System_modules = module.deviceProperties.System_modules
1203 props.Patch_module = module.properties.Patch_module
Paul Duffin367ab912019-12-23 19:40:36 +00001204 props.Installable = proptools.BoolPtr(false)
Sundong Ahn054b19a2018-10-19 13:46:09 +09001205 props.Libs = module.sdkLibraryProperties.Stub_only_libs
Paul Duffine22c2ab2020-05-20 19:35:27 +01001206 // The stub-annotations library contains special versions of the annotations
1207 // with CLASS retention policy, so that they're kept.
1208 if proptools.Bool(module.sdkLibraryProperties.Annotations_enabled) {
1209 props.Libs = append(props.Libs, "stub-annotations")
1210 }
Paul Duffina18abc22020-05-16 18:54:24 +01001211 props.Openjdk9.Srcs = module.properties.Openjdk9.Srcs
1212 props.Openjdk9.Javacflags = module.properties.Openjdk9.Javacflags
Anton Hansson83509b52020-05-21 09:21:57 +01001213 // We compile the stubs for 1.8 in line with the main android.jar stubs, and potential
1214 // interop with older developer tools that don't support 1.9.
1215 props.Java_version = proptools.StringPtr("1.8")
Liz Kammera7a64f32020-07-09 15:16:41 -07001216 if module.dexProperties.Compile_dex != nil {
1217 props.Compile_dex = module.dexProperties.Compile_dex
Sundong Ahndd567f92018-07-31 17:19:11 +09001218 }
Jiyong Parkc678ad32018-04-10 13:07:10 +09001219
Anton Hansson5fd5d242020-03-27 19:43:19 +00001220 // Dist the class jar artifact for sdk builds.
1221 if !Bool(module.sdkLibraryProperties.No_dist) {
1222 props.Dist.Targets = []string{"sdk", "win_sdk"}
1223 props.Dist.Dest = proptools.StringPtr(fmt.Sprintf("%v.jar", module.BaseModuleName()))
1224 props.Dist.Dir = proptools.StringPtr(module.apiDistPath(apiScope))
1225 props.Dist.Tag = proptools.StringPtr(".jar")
1226 }
Jiyong Parkc678ad32018-04-10 13:07:10 +09001227
Paul Duffin859fe962020-05-15 10:20:31 +01001228 mctx.CreateModule(LibraryFactory, &props, module.sdkComponentPropertiesForChildLibrary())
Jiyong Parkc678ad32018-04-10 13:07:10 +09001229}
1230
Paul Duffin6d0886e2020-04-07 18:49:53 +01001231// Creates a droidstubs module that creates stubs source files from the given full source
Paul Duffinc8782502020-04-29 20:45:27 +01001232// files and also updates and checks the API specification files.
Paul Duffin15f34ef2020-07-20 18:04:44 +01001233func (module *SdkLibrary) createStubsSourcesAndApi(mctx android.DefaultableHookContext, apiScope *apiScope, name string, scopeSpecificDroidstubsArgs []string) {
Jiyong Parkc678ad32018-04-10 13:07:10 +09001234 props := struct {
Sundong Ahn054b19a2018-10-19 13:46:09 +09001235 Name *string
Paul Duffin4911a892020-04-29 23:35:13 +01001236 Visibility []string
Sundong Ahn054b19a2018-10-19 13:46:09 +09001237 Srcs []string
1238 Installable *bool
Paul Duffin52d398a2019-06-11 12:31:14 +01001239 Sdk_version *string
Paul Duffin12ceb462019-12-24 20:31:31 +00001240 System_modules *string
Sundong Ahn054b19a2018-10-19 13:46:09 +09001241 Libs []string
Paul Duffin6877e6d2020-09-25 19:59:14 +01001242 Output_javadoc_comments *bool
Paul Duffin11512472019-02-11 15:55:17 +00001243 Arg_files []string
Sundong Ahn054b19a2018-10-19 13:46:09 +09001244 Args *string
Sundong Ahn054b19a2018-10-19 13:46:09 +09001245 Java_version *string
Paul Duffine22c2ab2020-05-20 19:35:27 +01001246 Annotations_enabled *bool
Sundong Ahn054b19a2018-10-19 13:46:09 +09001247 Merge_annotations_dirs []string
1248 Merge_inclusion_annotations_dirs []string
Paul Duffin0ff08bd2020-04-29 13:30:54 +01001249 Generate_stubs *bool
Sundong Ahn054b19a2018-10-19 13:46:09 +09001250 Check_api struct {
Inseob Kim38449af2019-02-28 14:24:05 +09001251 Current ApiToCheck
1252 Last_released ApiToCheck
1253 Ignore_missing_latest_api *bool
Paul Duffin160fe412020-05-10 19:32:20 +01001254
1255 Api_lint struct {
1256 Enabled *bool
1257 New_since *string
1258 Baseline_file *string
1259 }
Jiyong Park58c518b2018-05-12 22:29:12 +09001260 }
Sundong Ahn1b92c822018-05-29 11:35:17 +09001261 Aidl struct {
1262 Include_dirs []string
1263 Local_include_dirs []string
1264 }
Anton Hansson5fd5d242020-03-27 19:43:19 +00001265 Dist struct {
1266 Targets []string
1267 Dest *string
1268 Dir *string
1269 }
Jiyong Parkc678ad32018-04-10 13:07:10 +09001270 }{}
1271
Paul Duffin7b78b4d2020-04-28 14:08:32 +01001272 // The stubs source processing uses the same compile time classpath when extracting the
1273 // API from the implementation library as it does when compiling it. i.e. the same
1274 // * sdk version
1275 // * system_modules
1276 // * libs (static_libs/libs)
Paul Duffin250e6192019-06-07 10:44:37 +01001277
Paul Duffin0ff08bd2020-04-29 13:30:54 +01001278 props.Name = proptools.StringPtr(name)
Anton Hansson944e77d2020-08-19 11:40:22 +01001279 props.Visibility = childModuleVisibility(module.sdkLibraryProperties.Stubs_source_visibility)
Paul Duffina18abc22020-05-16 18:54:24 +01001280 props.Srcs = append(props.Srcs, module.properties.Srcs...)
1281 props.Sdk_version = module.deviceProperties.Sdk_version
1282 props.System_modules = module.deviceProperties.System_modules
Jiyong Parkc678ad32018-04-10 13:07:10 +09001283 props.Installable = proptools.BoolPtr(false)
Sundong Ahne6f0b052018-06-05 16:46:14 +09001284 // A droiddoc module has only one Libs property and doesn't distinguish between
1285 // shared libs and static libs. So we need to add both of these libs to Libs property.
Paul Duffina18abc22020-05-16 18:54:24 +01001286 props.Libs = module.properties.Libs
1287 props.Libs = append(props.Libs, module.properties.Static_libs...)
1288 props.Aidl.Include_dirs = module.deviceProperties.Aidl.Include_dirs
1289 props.Aidl.Local_include_dirs = module.deviceProperties.Aidl.Local_include_dirs
1290 props.Java_version = module.properties.Java_version
Jiyong Parkc678ad32018-04-10 13:07:10 +09001291
Paul Duffine22c2ab2020-05-20 19:35:27 +01001292 props.Annotations_enabled = module.sdkLibraryProperties.Annotations_enabled
Sundong Ahn054b19a2018-10-19 13:46:09 +09001293 props.Merge_annotations_dirs = module.sdkLibraryProperties.Merge_annotations_dirs
1294 props.Merge_inclusion_annotations_dirs = module.sdkLibraryProperties.Merge_inclusion_annotations_dirs
1295
Paul Duffin6d0886e2020-04-07 18:49:53 +01001296 droidstubsArgs := []string{}
Paul Duffin235ffff2019-12-24 10:41:30 +00001297 if len(module.sdkLibraryProperties.Api_packages) != 0 {
Paul Duffin6d0886e2020-04-07 18:49:53 +01001298 droidstubsArgs = append(droidstubsArgs, "--stub-packages "+strings.Join(module.sdkLibraryProperties.Api_packages, ":"))
Paul Duffin235ffff2019-12-24 10:41:30 +00001299 }
1300 if len(module.sdkLibraryProperties.Hidden_api_packages) != 0 {
Paul Duffin6d0886e2020-04-07 18:49:53 +01001301 droidstubsArgs = append(droidstubsArgs,
Paul Duffin235ffff2019-12-24 10:41:30 +00001302 android.JoinWithPrefix(module.sdkLibraryProperties.Hidden_api_packages, " --hide-package "))
1303 }
Paul Duffin6d0886e2020-04-07 18:49:53 +01001304 droidstubsArgs = append(droidstubsArgs, module.sdkLibraryProperties.Droiddoc_options...)
Paul Duffin235ffff2019-12-24 10:41:30 +00001305 disabledWarnings := []string{
1306 "MissingPermission",
1307 "BroadcastBehavior",
1308 "HiddenSuperclass",
1309 "DeprecationMismatch",
1310 "UnavailableSymbol",
1311 "SdkConstant",
1312 "HiddenTypeParameter",
1313 "Todo",
1314 "Typo",
1315 }
Paul Duffin6d0886e2020-04-07 18:49:53 +01001316 droidstubsArgs = append(droidstubsArgs, android.JoinWithPrefix(disabledWarnings, "--hide "))
Sundong Ahnfb2721f2018-09-17 13:23:09 +09001317
Paul Duffin6877e6d2020-09-25 19:59:14 +01001318 // Output Javadoc comments for public scope.
1319 if apiScope == apiScopePublic {
1320 props.Output_javadoc_comments = proptools.BoolPtr(true)
1321 }
1322
Paul Duffin1fb487d2020-04-07 18:50:10 +01001323 // Add in scope specific arguments.
Paul Duffin0ff08bd2020-04-29 13:30:54 +01001324 droidstubsArgs = append(droidstubsArgs, scopeSpecificDroidstubsArgs...)
Paul Duffin11512472019-02-11 15:55:17 +00001325 props.Arg_files = module.sdkLibraryProperties.Droiddoc_option_files
Paul Duffin6d0886e2020-04-07 18:49:53 +01001326 props.Args = proptools.StringPtr(strings.Join(droidstubsArgs, " "))
Jiyong Parkc678ad32018-04-10 13:07:10 +09001327
Paul Duffin15f34ef2020-07-20 18:04:44 +01001328 // List of APIs identified from the provided source files are created. They are later
1329 // compared against to the not-yet-released (a.k.a current) list of APIs and to the
1330 // last-released (a.k.a numbered) list of API.
1331 currentApiFileName := apiScope.apiFilePrefix + "current.txt"
1332 removedApiFileName := apiScope.apiFilePrefix + "removed.txt"
1333 apiDir := module.getApiDir()
1334 currentApiFileName = path.Join(apiDir, currentApiFileName)
1335 removedApiFileName = path.Join(apiDir, removedApiFileName)
Jiyong Parkc678ad32018-04-10 13:07:10 +09001336
Paul Duffin15f34ef2020-07-20 18:04:44 +01001337 // check against the not-yet-release API
1338 props.Check_api.Current.Api_file = proptools.StringPtr(currentApiFileName)
1339 props.Check_api.Current.Removed_api_file = proptools.StringPtr(removedApiFileName)
Jiyong Park58c518b2018-05-12 22:29:12 +09001340
Paul Duffin15f34ef2020-07-20 18:04:44 +01001341 if !apiScope.unstable {
1342 // check against the latest released API
1343 latestApiFilegroupName := proptools.StringPtr(module.latestApiFilegroupName(apiScope))
1344 props.Check_api.Last_released.Api_file = latestApiFilegroupName
1345 props.Check_api.Last_released.Removed_api_file = proptools.StringPtr(
1346 module.latestRemovedApiFilegroupName(apiScope))
1347 props.Check_api.Ignore_missing_latest_api = proptools.BoolPtr(true)
Paul Duffin160fe412020-05-10 19:32:20 +01001348
Paul Duffin15f34ef2020-07-20 18:04:44 +01001349 if proptools.Bool(module.sdkLibraryProperties.Api_lint.Enabled) {
1350 // Enable api lint.
1351 props.Check_api.Api_lint.Enabled = proptools.BoolPtr(true)
1352 props.Check_api.Api_lint.New_since = latestApiFilegroupName
Paul Duffin160fe412020-05-10 19:32:20 +01001353
Paul Duffin15f34ef2020-07-20 18:04:44 +01001354 // If it exists then pass a lint-baseline.txt through to droidstubs.
1355 baselinePath := path.Join(apiDir, apiScope.apiFilePrefix+"lint-baseline.txt")
1356 baselinePathRelativeToRoot := path.Join(mctx.ModuleDir(), baselinePath)
1357 paths, err := mctx.GlobWithDeps(baselinePathRelativeToRoot, nil)
1358 if err != nil {
1359 mctx.ModuleErrorf("error checking for presence of %s: %s", baselinePathRelativeToRoot, err)
1360 }
1361 if len(paths) == 1 {
1362 props.Check_api.Api_lint.Baseline_file = proptools.StringPtr(baselinePath)
1363 } else if len(paths) != 0 {
1364 mctx.ModuleErrorf("error checking for presence of %s: expected one path, found: %v", baselinePathRelativeToRoot, paths)
Paul Duffin160fe412020-05-10 19:32:20 +01001365 }
1366 }
Paul Duffin15f34ef2020-07-20 18:04:44 +01001367 }
Jiyong Park58c518b2018-05-12 22:29:12 +09001368
Paul Duffin15f34ef2020-07-20 18:04:44 +01001369 // Dist the api txt artifact for sdk builds.
1370 if !Bool(module.sdkLibraryProperties.No_dist) {
1371 props.Dist.Targets = []string{"sdk", "win_sdk"}
1372 props.Dist.Dest = proptools.StringPtr(fmt.Sprintf("%v.txt", module.BaseModuleName()))
1373 props.Dist.Dir = proptools.StringPtr(path.Join(module.apiDistPath(apiScope), "api"))
Anton Hansson5fd5d242020-03-27 19:43:19 +00001374 }
1375
Colin Cross84dfc3d2019-09-25 11:33:01 -07001376 mctx.CreateModule(DroidstubsFactory, &props)
Jiyong Parkc678ad32018-04-10 13:07:10 +09001377}
1378
Jooyung Han5e9013b2020-03-10 06:23:13 +09001379func (module *SdkLibrary) DepIsInSameApex(mctx android.BaseModuleContext, dep android.Module) bool {
1380 depTag := mctx.OtherModuleDependencyTag(dep)
1381 if depTag == xmlPermissionsFileTag {
1382 return true
1383 }
1384 return module.Library.DepIsInSameApex(mctx, dep)
1385}
1386
Jiyong Parkc678ad32018-04-10 13:07:10 +09001387// Creates the xml file that publicizes the runtime library
Paul Duffinf0229202020-04-29 16:47:28 +01001388func (module *SdkLibrary) createXmlFile(mctx android.DefaultableHookContext) {
Jiyong Parke3833882020-02-17 17:28:10 +09001389 props := struct {
Paul Duffin1dbe3ca2020-05-16 09:57:59 +01001390 Name *string
1391 Lib_name *string
1392 Apex_available []string
Jiyong Parke3833882020-02-17 17:28:10 +09001393 }{
Paul Duffineedc5d52020-06-12 17:46:39 +01001394 Name: proptools.StringPtr(module.xmlPermissionsModuleName()),
Jooyung Han5e9013b2020-03-10 06:23:13 +09001395 Lib_name: proptools.StringPtr(module.BaseModuleName()),
1396 Apex_available: module.ApexProperties.Apex_available,
Jiyong Parkc678ad32018-04-10 13:07:10 +09001397 }
Jiyong Parke3833882020-02-17 17:28:10 +09001398
Jiyong Parke3833882020-02-17 17:28:10 +09001399 mctx.CreateModule(sdkLibraryXmlFactory, &props)
Jiyong Parkc678ad32018-04-10 13:07:10 +09001400}
1401
Paul Duffin50061512020-01-21 16:31:05 +00001402func PrebuiltJars(ctx android.BaseModuleContext, baseName string, s sdkSpec) android.Paths {
Jiyong Park6a927c42020-01-21 02:03:43 +09001403 var ver sdkVersion
1404 var kind sdkKind
1405 if s.usePrebuilt(ctx) {
1406 ver = s.version
1407 kind = s.kind
Jiyong Parkc678ad32018-04-10 13:07:10 +09001408 } else {
Jiyong Park6a927c42020-01-21 02:03:43 +09001409 // We don't have prebuilt SDK for the specific sdkVersion.
1410 // Instead of breaking the build, fallback to use "system_current"
1411 ver = sdkVersionCurrent
1412 kind = sdkSystem
Sundong Ahn054b19a2018-10-19 13:46:09 +09001413 }
Jiyong Park6a927c42020-01-21 02:03:43 +09001414
1415 dir := filepath.Join("prebuilts", "sdk", ver.String(), kind.String())
Paul Duffin50061512020-01-21 16:31:05 +00001416 jar := filepath.Join(dir, baseName+".jar")
Sundong Ahn054b19a2018-10-19 13:46:09 +09001417 jarPath := android.ExistentPathForSource(ctx, jar)
Sundong Ahnae418ac2019-02-28 15:01:28 +09001418 if !jarPath.Valid() {
Colin Cross07c88562020-01-07 09:34:44 -08001419 if ctx.Config().AllowMissingDependencies() {
1420 return android.Paths{android.PathForSource(ctx, jar)}
1421 } else {
Jiyong Park6a927c42020-01-21 02:03:43 +09001422 ctx.PropertyErrorf("sdk_library", "invalid sdk version %q, %q does not exist", s.raw, jar)
Colin Cross07c88562020-01-07 09:34:44 -08001423 }
Sundong Ahnae418ac2019-02-28 15:01:28 +09001424 return nil
1425 }
Sundong Ahn054b19a2018-10-19 13:46:09 +09001426 return android.Paths{jarPath.Path()}
1427}
1428
Colin Crossaede88c2020-08-11 12:17:01 -07001429// 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 +01001430//
1431// If either this or the other module are on the platform then this will return
1432// false.
Colin Cross56a83212020-09-15 18:30:11 -07001433func withinSameApexesAs(ctx android.BaseModuleContext, other android.Module) bool {
1434 apexInfo := ctx.Provider(android.ApexInfoProvider).(android.ApexInfo)
1435 otherApexInfo := ctx.OtherModuleProvider(other, android.ApexInfoProvider).(android.ApexInfo)
1436 return len(otherApexInfo.InApexes) > 0 && reflect.DeepEqual(apexInfo.InApexes, otherApexInfo.InApexes)
Paul Duffin9b879592020-05-26 13:21:35 +01001437}
1438
Paul Duffinb05d4292020-05-20 12:19:10 +01001439func (module *SdkLibrary) sdkJars(ctx android.BaseModuleContext, sdkVersion sdkSpec, headerJars bool) android.Paths {
Jiyong Park932cdfe2020-05-28 00:19:53 +09001440 // If the client doesn't set sdk_version, but if this library prefers stubs over
1441 // the impl library, let's provide the widest API surface possible. To do so,
1442 // force override sdk_version to module_current so that the closest possible API
1443 // surface could be found in selectHeaderJarsForSdkVersion
1444 if module.defaultsToStubs() && !sdkVersion.specified() {
1445 sdkVersion = sdkSpecFrom("module_current")
1446 }
Paul Duffind1b3a922020-01-22 11:57:20 +00001447
Paul Duffindaaa3322020-05-26 18:13:57 +01001448 // Only provide access to the implementation library if it is actually built.
1449 if module.requiresRuntimeImplementationLibrary() {
1450 // Check any special cases for java_sdk_library.
1451 //
1452 // Only allow access to the implementation library in the following condition:
1453 // * No sdk_version specified on the referencing module.
Paul Duffin9b879592020-05-26 13:21:35 +01001454 // * The referencing module is in the same apex as this.
Colin Cross56a83212020-09-15 18:30:11 -07001455 if sdkVersion.kind == sdkPrivate || withinSameApexesAs(ctx, module) {
Paul Duffindaaa3322020-05-26 18:13:57 +01001456 if headerJars {
1457 return module.HeaderJars()
1458 } else {
1459 return module.ImplementationJars()
1460 }
Sundong Ahn054b19a2018-10-19 13:46:09 +09001461 }
Jiyong Parkc678ad32018-04-10 13:07:10 +09001462 }
Paul Duffinb05d4292020-05-20 12:19:10 +01001463
Paul Duffin23970f42020-05-20 14:20:02 +01001464 return module.selectHeaderJarsForSdkVersion(ctx, sdkVersion)
Jiyong Parkc678ad32018-04-10 13:07:10 +09001465}
1466
Sundong Ahn241cd372018-07-13 16:16:44 +09001467// to satisfy SdkLibraryDependency interface
Paul Duffind1b3a922020-01-22 11:57:20 +00001468func (module *SdkLibrary) SdkHeaderJars(ctx android.BaseModuleContext, sdkVersion sdkSpec) android.Paths {
1469 return module.sdkJars(ctx, sdkVersion, true /*headerJars*/)
1470}
1471
1472// to satisfy SdkLibraryDependency interface
Jiyong Park6a927c42020-01-21 02:03:43 +09001473func (module *SdkLibrary) SdkImplementationJars(ctx android.BaseModuleContext, sdkVersion sdkSpec) android.Paths {
Paul Duffind1b3a922020-01-22 11:57:20 +00001474 return module.sdkJars(ctx, sdkVersion, false /*headerJars*/)
Sundong Ahn241cd372018-07-13 16:16:44 +09001475}
1476
Sundong Ahn80a87b32019-05-13 15:02:50 +09001477func (module *SdkLibrary) SetNoDist() {
1478 module.sdkLibraryProperties.No_dist = proptools.BoolPtr(true)
1479}
1480
Colin Cross571cccf2019-02-04 11:22:08 -08001481var javaSdkLibrariesKey = android.NewOnceKey("javaSdkLibraries")
1482
Jiyong Park82484c02018-04-23 21:41:26 +09001483func javaSdkLibraries(config android.Config) *[]string {
Colin Cross571cccf2019-02-04 11:22:08 -08001484 return config.Once(javaSdkLibrariesKey, func() interface{} {
Jiyong Park82484c02018-04-23 21:41:26 +09001485 return &[]string{}
1486 }).(*[]string)
1487}
1488
Paul Duffin749f98f2019-12-30 17:23:46 +00001489func (module *SdkLibrary) getApiDir() string {
1490 return proptools.StringDefault(module.sdkLibraryProperties.Api_dir, "api")
1491}
1492
Jiyong Parkc678ad32018-04-10 13:07:10 +09001493// For a java_sdk_library module, create internal modules for stubs, docs,
1494// runtime libs and xml file. If requested, the stubs and docs are created twice
1495// once for public API level and once for system API level
Paul Duffinf0229202020-04-29 16:47:28 +01001496func (module *SdkLibrary) CreateInternalModules(mctx android.DefaultableHookContext) {
1497 // If the module has been disabled then don't create any child modules.
1498 if !module.Enabled() {
1499 return
1500 }
1501
Paul Duffina18abc22020-05-16 18:54:24 +01001502 if len(module.properties.Srcs) == 0 {
Inseob Kimc0907f12019-02-08 21:00:45 +09001503 mctx.PropertyErrorf("srcs", "java_sdk_library must specify srcs")
Jooyung Han58f26ab2019-12-18 15:34:32 +09001504 return
Inseob Kimc0907f12019-02-08 21:00:45 +09001505 }
1506
Paul Duffin37e0b772019-12-30 17:20:10 +00001507 // If this builds against standard libraries (i.e. is not part of the core libraries)
1508 // then assume it provides both system and test apis. Otherwise, assume it does not and
1509 // also assume it does not contribute to the dist build.
1510 sdkDep := decodeSdkDep(mctx, sdkContext(&module.Library))
1511 hasSystemAndTestApis := sdkDep.hasStandardLibs()
Paul Duffin3375e352020-04-28 10:44:03 +01001512 module.sdkLibraryProperties.Generate_system_and_test_apis = hasSystemAndTestApis
Paul Duffin37e0b772019-12-30 17:20:10 +00001513 module.sdkLibraryProperties.No_dist = proptools.BoolPtr(!hasSystemAndTestApis)
1514
Inseob Kim8098faa2019-03-18 10:19:51 +09001515 missing_current_api := false
1516
Paul Duffin3375e352020-04-28 10:44:03 +01001517 generatedScopes := module.getGeneratedApiScopes(mctx)
Paul Duffind1b3a922020-01-22 11:57:20 +00001518
Paul Duffin749f98f2019-12-30 17:23:46 +00001519 apiDir := module.getApiDir()
Paul Duffin3375e352020-04-28 10:44:03 +01001520 for _, scope := range generatedScopes {
Inseob Kim8098faa2019-03-18 10:19:51 +09001521 for _, api := range []string{"current.txt", "removed.txt"} {
Paul Duffind1b3a922020-01-22 11:57:20 +00001522 path := path.Join(mctx.ModuleDir(), apiDir, scope.apiFilePrefix+api)
Inseob Kim8098faa2019-03-18 10:19:51 +09001523 p := android.ExistentPathForSource(mctx, path)
1524 if !p.Valid() {
1525 mctx.ModuleErrorf("Current api file %#v doesn't exist", path)
1526 missing_current_api = true
1527 }
1528 }
1529 }
1530
1531 if missing_current_api {
1532 script := "build/soong/scripts/gen-java-current-api-files.sh"
1533 p := android.ExistentPathForSource(mctx, script)
1534
1535 if !p.Valid() {
1536 panic(fmt.Sprintf("script file %s doesn't exist", script))
1537 }
1538
1539 mctx.ModuleErrorf("One or more current api files are missing. "+
1540 "You can update them by:\n"+
Paul Duffin37e0b772019-12-30 17:20:10 +00001541 "%s %q %s && m update-api",
Paul Duffind1b3a922020-01-22 11:57:20 +00001542 script, filepath.Join(mctx.ModuleDir(), apiDir),
Paul Duffin3375e352020-04-28 10:44:03 +01001543 strings.Join(generatedScopes.Strings(func(s *apiScope) string { return s.apiFilePrefix }), " "))
Inseob Kim8098faa2019-03-18 10:19:51 +09001544 return
1545 }
1546
Paul Duffin3375e352020-04-28 10:44:03 +01001547 for _, scope := range generatedScopes {
Paul Duffin15f34ef2020-07-20 18:04:44 +01001548 // Use the stubs source name for legacy reasons.
1549 module.createStubsSourcesAndApi(mctx, scope, module.stubsSourceModuleName(scope), scope.droidstubsArgs)
Paul Duffin0ff08bd2020-04-29 13:30:54 +01001550
Paul Duffind1b3a922020-01-22 11:57:20 +00001551 module.createStubsLibrary(mctx, scope)
Inseob Kimc0907f12019-02-08 21:00:45 +09001552 }
1553
Paul Duffindfa131e2020-05-15 20:37:11 +01001554 if module.requiresRuntimeImplementationLibrary() {
Paul Duffin5df79302020-05-16 15:52:12 +01001555 // Create child module to create an implementation library.
1556 //
1557 // This temporarily creates a second implementation library that can be explicitly
1558 // referenced.
1559 //
1560 // TODO(b/156618935) - update comment once only one implementation library is created.
1561 module.createImplLibrary(mctx)
1562
Paul Duffindfa131e2020-05-15 20:37:11 +01001563 // Only create an XML permissions file that declares the library as being usable
1564 // as a shared library if required.
1565 if module.sharedLibrary() {
1566 module.createXmlFile(mctx)
1567 }
Paul Duffin43db9be2019-12-30 17:35:49 +00001568
1569 // record java_sdk_library modules so that they are exported to make
1570 javaSdkLibraries := javaSdkLibraries(mctx.Config())
1571 javaSdkLibrariesLock.Lock()
1572 defer javaSdkLibrariesLock.Unlock()
1573 *javaSdkLibraries = append(*javaSdkLibraries, module.BaseModuleName())
1574 }
Anton Hansson7f66efa2020-10-08 14:47:23 +01001575
1576 // Add the impl_only_libs *after* we're done using the Libs prop in submodules.
1577 module.properties.Libs = append(module.properties.Libs, module.sdkLibraryProperties.Impl_only_libs...)
Inseob Kimc0907f12019-02-08 21:00:45 +09001578}
1579
1580func (module *SdkLibrary) InitSdkLibraryProperties() {
Colin Crossce6734e2020-06-15 16:09:53 -07001581 module.addHostAndDeviceProperties()
1582 module.AddProperties(&module.sdkLibraryProperties)
Sundong Ahn054b19a2018-10-19 13:46:09 +09001583
Paul Duffin859fe962020-05-15 10:20:31 +01001584 module.initSdkLibraryComponent(&module.ModuleBase)
1585
Paul Duffina18abc22020-05-16 18:54:24 +01001586 module.properties.Installable = proptools.BoolPtr(true)
1587 module.deviceProperties.IsSDKLibrary = true
Inseob Kimc0907f12019-02-08 21:00:45 +09001588}
Sundong Ahn054b19a2018-10-19 13:46:09 +09001589
Paul Duffindfa131e2020-05-15 20:37:11 +01001590func (module *SdkLibrary) requiresRuntimeImplementationLibrary() bool {
1591 return !proptools.Bool(module.sdkLibraryProperties.Api_only)
1592}
1593
Jiyong Park932cdfe2020-05-28 00:19:53 +09001594func (module *SdkLibrary) defaultsToStubs() bool {
1595 return proptools.Bool(module.sdkLibraryProperties.Default_to_stubs)
1596}
1597
Paul Duffin1b1e8062020-05-08 13:44:43 +01001598// Defines how to name the individual component modules the sdk library creates.
1599type sdkLibraryComponentNamingScheme interface {
1600 stubsLibraryModuleName(scope *apiScope, baseName string) string
1601
1602 stubsSourceModuleName(scope *apiScope, baseName string) string
1603
1604 apiModuleName(scope *apiScope, baseName string) string
1605}
1606
1607type defaultNamingScheme struct {
1608}
1609
1610func (s *defaultNamingScheme) stubsLibraryModuleName(scope *apiScope, baseName string) string {
1611 return scope.stubsLibraryModuleName(baseName)
1612}
1613
1614func (s *defaultNamingScheme) stubsSourceModuleName(scope *apiScope, baseName string) string {
1615 return scope.stubsSourceModuleName(baseName)
1616}
1617
1618func (s *defaultNamingScheme) apiModuleName(scope *apiScope, baseName string) string {
1619 return scope.apiModuleName(baseName)
1620}
1621
1622var _ sdkLibraryComponentNamingScheme = (*defaultNamingScheme)(nil)
1623
Anton Hansson2d0c1942020-05-25 12:20:51 +01001624func moduleStubLinkType(name string) (stub bool, ret linkType) {
1625 // This suffix-based approach is fragile and could potentially mis-trigger.
1626 // TODO(b/155164730): Clean this up when modules no longer reference sdk_lib stubs directly.
1627 if strings.HasSuffix(name, ".stubs.public") || strings.HasSuffix(name, "-stubs-publicapi") {
1628 return true, javaSdk
1629 }
1630 if strings.HasSuffix(name, ".stubs.system") || strings.HasSuffix(name, "-stubs-systemapi") {
1631 return true, javaSystem
1632 }
1633 if strings.HasSuffix(name, ".stubs.module_lib") || strings.HasSuffix(name, "-stubs-module_libs_api") {
1634 return true, javaModule
1635 }
1636 if strings.HasSuffix(name, ".stubs.test") {
1637 return true, javaSystem
1638 }
1639 return false, javaPlatform
1640}
1641
Jaewoong Jung4f158ee2019-07-11 10:05:35 -07001642// java_sdk_library is a special Java library that provides optional platform APIs to apps.
1643// In practice, it can be viewed as a combination of several modules: 1) stubs library that clients
1644// are linked against to, 2) droiddoc module that internally generates API stubs source files,
1645// 3) the real runtime shared library that implements the APIs, and 4) XML file for adding
1646// the runtime lib to the classpath at runtime if requested via <uses-library>.
Inseob Kimc0907f12019-02-08 21:00:45 +09001647func SdkLibraryFactory() android.Module {
1648 module := &SdkLibrary{}
Paul Duffinc3091c82020-05-08 14:16:20 +01001649
1650 // Initialize information common between source and prebuilt.
1651 module.initCommon(&module.ModuleBase)
1652
Inseob Kimc0907f12019-02-08 21:00:45 +09001653 module.InitSdkLibraryProperties()
Jooyung Han58f26ab2019-12-18 15:34:32 +09001654 android.InitApexModule(module)
Sundong Ahn054b19a2018-10-19 13:46:09 +09001655 InitJavaModule(module, android.HostAndDeviceSupported)
Paul Duffin3375e352020-04-28 10:44:03 +01001656
1657 // Initialize the map from scope to scope specific properties.
1658 scopeToProperties := make(map[*apiScope]*ApiScopeProperties)
1659 for _, scope := range allApiScopes {
1660 scopeToProperties[scope] = scope.scopeSpecificProperties(module)
1661 }
1662 module.scopeToProperties = scopeToProperties
1663
Paul Duffin4911a892020-04-29 23:35:13 +01001664 // Add the properties containing visibility rules so that they are checked.
Paul Duffin5df79302020-05-16 15:52:12 +01001665 android.AddVisibilityProperty(module, "impl_library_visibility", &module.sdkLibraryProperties.Impl_library_visibility)
Paul Duffin4911a892020-04-29 23:35:13 +01001666 android.AddVisibilityProperty(module, "stubs_library_visibility", &module.sdkLibraryProperties.Stubs_library_visibility)
1667 android.AddVisibilityProperty(module, "stubs_source_visibility", &module.sdkLibraryProperties.Stubs_source_visibility)
1668
Paul Duffin1b1e8062020-05-08 13:44:43 +01001669 module.SetDefaultableHook(func(ctx android.DefaultableHookContext) {
Paul Duffindfa131e2020-05-15 20:37:11 +01001670 // If no implementation is required then it cannot be used as a shared library
1671 // either.
1672 if !module.requiresRuntimeImplementationLibrary() {
1673 // If shared_library has been explicitly set to true then it is incompatible
1674 // with api_only: true.
1675 if proptools.Bool(module.commonSdkLibraryProperties.Shared_library) {
1676 ctx.PropertyErrorf("api_only/shared_library", "inconsistent settings, shared_library and api_only cannot both be true")
1677 }
1678 // Set shared_library: false.
1679 module.commonSdkLibraryProperties.Shared_library = proptools.BoolPtr(false)
1680 }
1681
Paul Duffin1b1e8062020-05-08 13:44:43 +01001682 if module.initCommonAfterDefaultsApplied(ctx) {
1683 module.CreateInternalModules(ctx)
1684 }
1685 })
Jiyong Parkc678ad32018-04-10 13:07:10 +09001686 return module
1687}
Colin Cross79c7c262019-04-17 11:11:46 -07001688
1689//
1690// SDK library prebuilts
1691//
1692
Paul Duffin56d44902020-01-31 13:36:25 +00001693// Properties associated with each api scope.
1694type sdkLibraryScopeProperties struct {
Colin Cross79c7c262019-04-17 11:11:46 -07001695 Jars []string `android:"path"`
1696
1697 Sdk_version *string
1698
Colin Cross79c7c262019-04-17 11:11:46 -07001699 // List of shared java libs that this module has dependencies to
1700 Libs []string
Paul Duffin3d1248c2020-04-09 00:10:17 +01001701
Paul Duffinc8782502020-04-29 20:45:27 +01001702 // The stubs source.
Paul Duffin3d1248c2020-04-09 00:10:17 +01001703 Stub_srcs []string `android:"path"`
Paul Duffin1fd005d2020-04-09 01:08:11 +01001704
1705 // The current.txt
Paul Duffin0f8faff2020-05-20 16:18:00 +01001706 Current_api *string `android:"path"`
Paul Duffin1fd005d2020-04-09 01:08:11 +01001707
1708 // The removed.txt
Paul Duffin0f8faff2020-05-20 16:18:00 +01001709 Removed_api *string `android:"path"`
Colin Cross79c7c262019-04-17 11:11:46 -07001710}
1711
Paul Duffin56d44902020-01-31 13:36:25 +00001712type sdkLibraryImportProperties struct {
Paul Duffinfcfd7912020-01-31 17:54:30 +00001713 // List of shared java libs, common to all scopes, that this module has
1714 // dependencies to
1715 Libs []string
Paul Duffin56d44902020-01-31 13:36:25 +00001716}
1717
Paul Duffineedc5d52020-06-12 17:46:39 +01001718type SdkLibraryImport struct {
Colin Cross79c7c262019-04-17 11:11:46 -07001719 android.ModuleBase
1720 android.DefaultableModuleBase
1721 prebuilt android.Prebuilt
Paul Duffindd46f712020-02-10 13:37:10 +00001722 android.ApexModuleBase
1723 android.SdkBase
Colin Cross79c7c262019-04-17 11:11:46 -07001724
1725 properties sdkLibraryImportProperties
1726
Paul Duffin46a26a82020-04-07 19:27:04 +01001727 // Map from api scope to the scope specific property structure.
1728 scopeProperties map[*apiScope]*sdkLibraryScopeProperties
1729
Paul Duffin56d44902020-01-31 13:36:25 +00001730 commonToSdkLibraryAndImport
Paul Duffineedc5d52020-06-12 17:46:39 +01001731
1732 // The reference to the implementation library created by the source module.
1733 // Is nil if the source module does not exist.
1734 implLibraryModule *Library
1735
1736 // The reference to the xml permissions module created by the source module.
1737 // Is nil if the source module does not exist.
1738 xmlPermissionsFileModule *sdkLibraryXml
Colin Cross79c7c262019-04-17 11:11:46 -07001739}
1740
Paul Duffineedc5d52020-06-12 17:46:39 +01001741var _ SdkLibraryDependency = (*SdkLibraryImport)(nil)
Colin Cross79c7c262019-04-17 11:11:46 -07001742
Paul Duffin46a26a82020-04-07 19:27:04 +01001743// The type of a structure that contains a field of type sdkLibraryScopeProperties
1744// for each apiscope in allApiScopes, e.g. something like:
1745// struct {
1746// Public sdkLibraryScopeProperties
1747// System sdkLibraryScopeProperties
1748// ...
1749// }
1750var allScopeStructType = createAllScopePropertiesStructType()
1751
1752// Dynamically create a structure type for each apiscope in allApiScopes.
1753func createAllScopePropertiesStructType() reflect.Type {
1754 var fields []reflect.StructField
1755 for _, apiScope := range allApiScopes {
1756 field := reflect.StructField{
1757 Name: apiScope.fieldName,
1758 Type: reflect.TypeOf(sdkLibraryScopeProperties{}),
1759 }
1760 fields = append(fields, field)
1761 }
1762
1763 return reflect.StructOf(fields)
1764}
1765
1766// Create an instance of the scope specific structure type and return a map
1767// from apiscope to a pointer to each scope specific field.
1768func createPropertiesInstance() (interface{}, map[*apiScope]*sdkLibraryScopeProperties) {
1769 allScopePropertiesPtr := reflect.New(allScopeStructType)
1770 allScopePropertiesStruct := allScopePropertiesPtr.Elem()
1771 scopeProperties := make(map[*apiScope]*sdkLibraryScopeProperties)
1772
1773 for _, apiScope := range allApiScopes {
1774 field := allScopePropertiesStruct.FieldByName(apiScope.fieldName)
1775 scopeProperties[apiScope] = field.Addr().Interface().(*sdkLibraryScopeProperties)
1776 }
1777
1778 return allScopePropertiesPtr.Interface(), scopeProperties
1779}
1780
Jaewoong Jung4f158ee2019-07-11 10:05:35 -07001781// java_sdk_library_import imports a prebuilt java_sdk_library.
Colin Cross79c7c262019-04-17 11:11:46 -07001782func sdkLibraryImportFactory() android.Module {
Paul Duffineedc5d52020-06-12 17:46:39 +01001783 module := &SdkLibraryImport{}
Colin Cross79c7c262019-04-17 11:11:46 -07001784
Paul Duffin46a26a82020-04-07 19:27:04 +01001785 allScopeProperties, scopeToProperties := createPropertiesInstance()
1786 module.scopeProperties = scopeToProperties
1787 module.AddProperties(&module.properties, allScopeProperties)
Colin Cross79c7c262019-04-17 11:11:46 -07001788
Paul Duffinc3091c82020-05-08 14:16:20 +01001789 // Initialize information common between source and prebuilt.
1790 module.initCommon(&module.ModuleBase)
1791
Paul Duffin0bdcb272020-02-06 15:24:57 +00001792 android.InitPrebuiltModule(module, &[]string{""})
Paul Duffindd46f712020-02-10 13:37:10 +00001793 android.InitApexModule(module)
1794 android.InitSdkAwareModule(module)
Colin Cross79c7c262019-04-17 11:11:46 -07001795 InitJavaModule(module, android.HostAndDeviceSupported)
1796
Paul Duffin1b1e8062020-05-08 13:44:43 +01001797 module.SetDefaultableHook(func(mctx android.DefaultableHookContext) {
1798 if module.initCommonAfterDefaultsApplied(mctx) {
1799 module.createInternalModules(mctx)
1800 }
1801 })
Colin Cross79c7c262019-04-17 11:11:46 -07001802 return module
1803}
1804
Paul Duffineedc5d52020-06-12 17:46:39 +01001805func (module *SdkLibraryImport) Prebuilt() *android.Prebuilt {
Colin Cross79c7c262019-04-17 11:11:46 -07001806 return &module.prebuilt
1807}
1808
Paul Duffineedc5d52020-06-12 17:46:39 +01001809func (module *SdkLibraryImport) Name() string {
Colin Cross79c7c262019-04-17 11:11:46 -07001810 return module.prebuilt.Name(module.ModuleBase.Name())
1811}
1812
Paul Duffineedc5d52020-06-12 17:46:39 +01001813func (module *SdkLibraryImport) createInternalModules(mctx android.DefaultableHookContext) {
Colin Cross79c7c262019-04-17 11:11:46 -07001814
Paul Duffin50061512020-01-21 16:31:05 +00001815 // If the build is configured to use prebuilts then force this to be preferred.
Jeongik Cha816a23a2020-07-08 01:09:23 +09001816 if mctx.Config().AlwaysUsePrebuiltSdks() {
Paul Duffin50061512020-01-21 16:31:05 +00001817 module.prebuilt.ForcePrefer()
1818 }
1819
Paul Duffin46a26a82020-04-07 19:27:04 +01001820 for apiScope, scopeProperties := range module.scopeProperties {
Paul Duffin56d44902020-01-31 13:36:25 +00001821 if len(scopeProperties.Jars) == 0 {
1822 continue
1823 }
1824
Paul Duffinbbb546b2020-04-09 00:07:11 +01001825 module.createJavaImportForStubs(mctx, apiScope, scopeProperties)
Paul Duffin3d1248c2020-04-09 00:10:17 +01001826
Paul Duffin0f8faff2020-05-20 16:18:00 +01001827 if len(scopeProperties.Stub_srcs) > 0 {
1828 module.createPrebuiltStubsSources(mctx, apiScope, scopeProperties)
1829 }
Paul Duffin56d44902020-01-31 13:36:25 +00001830 }
Colin Cross79c7c262019-04-17 11:11:46 -07001831
1832 javaSdkLibraries := javaSdkLibraries(mctx.Config())
1833 javaSdkLibrariesLock.Lock()
1834 defer javaSdkLibrariesLock.Unlock()
1835 *javaSdkLibraries = append(*javaSdkLibraries, module.BaseModuleName())
1836}
1837
Paul Duffineedc5d52020-06-12 17:46:39 +01001838func (module *SdkLibraryImport) createJavaImportForStubs(mctx android.DefaultableHookContext, apiScope *apiScope, scopeProperties *sdkLibraryScopeProperties) {
Paul Duffinbbb546b2020-04-09 00:07:11 +01001839 // Creates a java import for the jar with ".stubs" suffix
1840 props := struct {
Paul Duffin1dbe3ca2020-05-16 09:57:59 +01001841 Name *string
1842 Sdk_version *string
1843 Libs []string
1844 Jars []string
1845 Prefer *bool
Paul Duffinbbb546b2020-04-09 00:07:11 +01001846 }{}
Paul Duffinc3091c82020-05-08 14:16:20 +01001847 props.Name = proptools.StringPtr(module.stubsLibraryModuleName(apiScope))
Paul Duffinbbb546b2020-04-09 00:07:11 +01001848 props.Sdk_version = scopeProperties.Sdk_version
1849 // Prepend any of the libs from the legacy public properties to the libs for each of the
1850 // scopes to avoid having to duplicate them in each scope.
1851 props.Libs = append(module.properties.Libs, scopeProperties.Libs...)
1852 props.Jars = scopeProperties.Jars
Paul Duffin1dbe3ca2020-05-16 09:57:59 +01001853
Paul Duffin38b57852020-05-13 16:08:09 +01001854 // The imports are preferred if the java_sdk_library_import is preferred.
1855 props.Prefer = proptools.BoolPtr(module.prebuilt.Prefer())
Paul Duffin859fe962020-05-15 10:20:31 +01001856
1857 mctx.CreateModule(ImportFactory, &props, module.sdkComponentPropertiesForChildLibrary())
Paul Duffinbbb546b2020-04-09 00:07:11 +01001858}
1859
Paul Duffineedc5d52020-06-12 17:46:39 +01001860func (module *SdkLibraryImport) createPrebuiltStubsSources(mctx android.DefaultableHookContext, apiScope *apiScope, scopeProperties *sdkLibraryScopeProperties) {
Paul Duffin3d1248c2020-04-09 00:10:17 +01001861 props := struct {
Paul Duffin38b57852020-05-13 16:08:09 +01001862 Name *string
1863 Srcs []string
1864 Prefer *bool
Paul Duffin3d1248c2020-04-09 00:10:17 +01001865 }{}
Paul Duffinc3091c82020-05-08 14:16:20 +01001866 props.Name = proptools.StringPtr(module.stubsSourceModuleName(apiScope))
Paul Duffin3d1248c2020-04-09 00:10:17 +01001867 props.Srcs = scopeProperties.Stub_srcs
1868 mctx.CreateModule(PrebuiltStubsSourcesFactory, &props)
Paul Duffin38b57852020-05-13 16:08:09 +01001869
1870 // The stubs source is preferred if the java_sdk_library_import is preferred.
1871 props.Prefer = proptools.BoolPtr(module.prebuilt.Prefer())
Paul Duffin3d1248c2020-04-09 00:10:17 +01001872}
1873
Paul Duffin44f1d842020-06-26 20:17:02 +01001874// Add the dependencies on the child module in the component deps mutator so that it
1875// creates references to the prebuilt and not the source modules.
1876func (module *SdkLibraryImport) ComponentDepsMutator(ctx android.BottomUpMutatorContext) {
Paul Duffin46a26a82020-04-07 19:27:04 +01001877 for apiScope, scopeProperties := range module.scopeProperties {
Paul Duffin56d44902020-01-31 13:36:25 +00001878 if len(scopeProperties.Jars) == 0 {
1879 continue
1880 }
1881
1882 // Add dependencies to the prebuilt stubs library
Paul Duffin44f1d842020-06-26 20:17:02 +01001883 ctx.AddVariationDependencies(nil, apiScope.stubsTag, "prebuilt_"+module.stubsLibraryModuleName(apiScope))
Paul Duffin0f8faff2020-05-20 16:18:00 +01001884
1885 if len(scopeProperties.Stub_srcs) > 0 {
1886 // Add dependencies to the prebuilt stubs source library
Paul Duffin44f1d842020-06-26 20:17:02 +01001887 ctx.AddVariationDependencies(nil, apiScope.stubsSourceTag, "prebuilt_"+module.stubsSourceModuleName(apiScope))
Paul Duffin0f8faff2020-05-20 16:18:00 +01001888 }
Paul Duffin56d44902020-01-31 13:36:25 +00001889 }
Paul Duffin44f1d842020-06-26 20:17:02 +01001890}
1891
1892// Add other dependencies as normal.
1893func (module *SdkLibraryImport) DepsMutator(ctx android.BottomUpMutatorContext) {
Paul Duffineedc5d52020-06-12 17:46:39 +01001894
1895 implName := module.implLibraryModuleName()
1896 if ctx.OtherModuleExists(implName) {
1897 ctx.AddVariationDependencies(nil, implLibraryTag, implName)
1898
1899 xmlPermissionsModuleName := module.xmlPermissionsModuleName()
1900 if module.sharedLibrary() && ctx.OtherModuleExists(xmlPermissionsModuleName) {
1901 // Add dependency to the rule for generating the xml permissions file
1902 ctx.AddDependency(module, xmlPermissionsFileTag, xmlPermissionsModuleName)
1903 }
1904 }
Colin Cross79c7c262019-04-17 11:11:46 -07001905}
1906
Paul Duffineedc5d52020-06-12 17:46:39 +01001907func (module *SdkLibraryImport) DepIsInSameApex(mctx android.BaseModuleContext, dep android.Module) bool {
1908 depTag := mctx.OtherModuleDependencyTag(dep)
1909 if depTag == xmlPermissionsFileTag {
1910 return true
1911 }
1912
1913 // None of the other dependencies of the java_sdk_library_import are in the same apex
1914 // as the one that references this module.
1915 return false
1916}
1917
Dan Albertc8060532020-07-22 22:32:17 -07001918func (module *SdkLibraryImport) ShouldSupportSdkVersion(ctx android.BaseModuleContext,
1919 sdkVersion android.ApiLevel) error {
Jooyung Han749dc692020-04-15 11:03:39 +09001920 // we don't check prebuilt modules for sdk_version
1921 return nil
1922}
1923
Paul Duffineedc5d52020-06-12 17:46:39 +01001924func (module *SdkLibraryImport) OutputFiles(tag string) (android.Paths, error) {
Paul Duffin46dc45a2020-05-14 15:39:10 +01001925 return module.commonOutputFiles(tag)
1926}
1927
Paul Duffineedc5d52020-06-12 17:46:39 +01001928func (module *SdkLibraryImport) GenerateAndroidBuildActions(ctx android.ModuleContext) {
Paul Duffina2ae7e02020-09-11 11:55:00 +01001929 module.generateCommonBuildActions(ctx)
1930
Paul Duffin0f8faff2020-05-20 16:18:00 +01001931 // Record the paths to the prebuilt stubs library and stubs source.
Colin Cross79c7c262019-04-17 11:11:46 -07001932 ctx.VisitDirectDeps(func(to android.Module) {
1933 tag := ctx.OtherModuleDependencyTag(to)
1934
Paul Duffin0f8faff2020-05-20 16:18:00 +01001935 // Extract information from any of the scope specific dependencies.
1936 if scopeTag, ok := tag.(scopeDependencyTag); ok {
1937 apiScope := scopeTag.apiScope
1938 scopePaths := module.getScopePathsCreateIfNeeded(apiScope)
1939
1940 // Extract information from the dependency. The exact information extracted
1941 // is determined by the nature of the dependency which is determined by the tag.
1942 scopeTag.extractDepInfo(ctx, to, scopePaths)
Paul Duffineedc5d52020-06-12 17:46:39 +01001943 } else if tag == implLibraryTag {
1944 if implLibrary, ok := to.(*Library); ok {
1945 module.implLibraryModule = implLibrary
1946 } else {
1947 ctx.ModuleErrorf("implementation library must be of type *java.Library but was %T", to)
1948 }
1949 } else if tag == xmlPermissionsFileTag {
1950 if xmlPermissionsFileModule, ok := to.(*sdkLibraryXml); ok {
1951 module.xmlPermissionsFileModule = xmlPermissionsFileModule
1952 } else {
1953 ctx.ModuleErrorf("xml permissions file module must be of type *sdkLibraryXml but was %T", to)
1954 }
Colin Cross79c7c262019-04-17 11:11:46 -07001955 }
1956 })
Paul Duffin0f8faff2020-05-20 16:18:00 +01001957
1958 // Populate the scope paths with information from the properties.
1959 for apiScope, scopeProperties := range module.scopeProperties {
1960 if len(scopeProperties.Jars) == 0 {
1961 continue
1962 }
1963
1964 paths := module.getScopePathsCreateIfNeeded(apiScope)
1965 paths.currentApiFilePath = android.OptionalPathForModuleSrc(ctx, scopeProperties.Current_api)
1966 paths.removedApiFilePath = android.OptionalPathForModuleSrc(ctx, scopeProperties.Removed_api)
1967 }
Colin Cross79c7c262019-04-17 11:11:46 -07001968}
1969
Paul Duffineedc5d52020-06-12 17:46:39 +01001970func (module *SdkLibraryImport) sdkJars(ctx android.BaseModuleContext, sdkVersion sdkSpec, headerJars bool) android.Paths {
1971
1972 // For consistency with SdkLibrary make the implementation jar available to libraries that
1973 // are within the same APEX.
1974 implLibraryModule := module.implLibraryModule
Colin Cross56a83212020-09-15 18:30:11 -07001975 if implLibraryModule != nil && withinSameApexesAs(ctx, module) {
Paul Duffineedc5d52020-06-12 17:46:39 +01001976 if headerJars {
1977 return implLibraryModule.HeaderJars()
1978 } else {
1979 return implLibraryModule.ImplementationJars()
1980 }
1981 }
1982
Paul Duffin23970f42020-05-20 14:20:02 +01001983 return module.selectHeaderJarsForSdkVersion(ctx, sdkVersion)
Paul Duffin56d44902020-01-31 13:36:25 +00001984}
1985
Colin Cross79c7c262019-04-17 11:11:46 -07001986// to satisfy SdkLibraryDependency interface
Paul Duffineedc5d52020-06-12 17:46:39 +01001987func (module *SdkLibraryImport) SdkHeaderJars(ctx android.BaseModuleContext, sdkVersion sdkSpec) android.Paths {
Colin Cross79c7c262019-04-17 11:11:46 -07001988 // This module is just a wrapper for the prebuilt stubs.
Paul Duffineedc5d52020-06-12 17:46:39 +01001989 return module.sdkJars(ctx, sdkVersion, true)
Colin Cross79c7c262019-04-17 11:11:46 -07001990}
1991
1992// to satisfy SdkLibraryDependency interface
Paul Duffineedc5d52020-06-12 17:46:39 +01001993func (module *SdkLibraryImport) SdkImplementationJars(ctx android.BaseModuleContext, sdkVersion sdkSpec) android.Paths {
Colin Cross79c7c262019-04-17 11:11:46 -07001994 // This module is just a wrapper for the stubs.
Paul Duffineedc5d52020-06-12 17:46:39 +01001995 return module.sdkJars(ctx, sdkVersion, false)
1996}
1997
Ulya Trafimovich31e444e2020-08-14 17:32:16 +01001998// to satisfy SdkLibraryDependency interface
Paul Duffineedc5d52020-06-12 17:46:39 +01001999func (module *SdkLibraryImport) DexJarBuildPath() android.Path {
2000 if module.implLibraryModule == nil {
2001 return nil
2002 } else {
2003 return module.implLibraryModule.DexJarBuildPath()
2004 }
2005}
2006
Ulya Trafimovich31e444e2020-08-14 17:32:16 +01002007// to satisfy SdkLibraryDependency interface
2008func (module *SdkLibraryImport) DexJarInstallPath() android.Path {
2009 if module.implLibraryModule == nil {
2010 return nil
2011 } else {
2012 return module.implLibraryModule.DexJarInstallPath()
2013 }
2014}
2015
Paul Duffineedc5d52020-06-12 17:46:39 +01002016// to satisfy apex.javaDependency interface
2017func (module *SdkLibraryImport) JacocoReportClassesFile() android.Path {
2018 if module.implLibraryModule == nil {
2019 return nil
2020 } else {
2021 return module.implLibraryModule.JacocoReportClassesFile()
2022 }
2023}
2024
2025// to satisfy apex.javaDependency interface
Colin Cross08dca382020-07-21 20:31:17 -07002026func (module *SdkLibraryImport) LintDepSets() LintDepSets {
2027 if module.implLibraryModule == nil {
2028 return LintDepSets{}
2029 } else {
2030 return module.implLibraryModule.LintDepSets()
2031 }
2032}
2033
2034// to satisfy apex.javaDependency interface
Paul Duffineedc5d52020-06-12 17:46:39 +01002035func (module *SdkLibraryImport) Stem() string {
2036 return module.BaseModuleName()
Colin Cross79c7c262019-04-17 11:11:46 -07002037}
Jiyong Parke3833882020-02-17 17:28:10 +09002038
Paul Duffin44b481b2020-06-17 16:59:43 +01002039var _ ApexDependency = (*SdkLibraryImport)(nil)
2040
2041// to satisfy java.ApexDependency interface
2042func (module *SdkLibraryImport) HeaderJars() android.Paths {
2043 if module.implLibraryModule == nil {
2044 return nil
2045 } else {
2046 return module.implLibraryModule.HeaderJars()
2047 }
2048}
2049
2050// to satisfy java.ApexDependency interface
2051func (module *SdkLibraryImport) ImplementationAndResourcesJars() android.Paths {
2052 if module.implLibraryModule == nil {
2053 return nil
2054 } else {
2055 return module.implLibraryModule.ImplementationAndResourcesJars()
2056 }
2057}
2058
Jiyong Parke3833882020-02-17 17:28:10 +09002059//
2060// java_sdk_library_xml
2061//
2062type sdkLibraryXml struct {
2063 android.ModuleBase
2064 android.DefaultableModuleBase
2065 android.ApexModuleBase
2066
2067 properties sdkLibraryXmlProperties
2068
2069 outputFilePath android.OutputPath
2070 installDirPath android.InstallPath
Colin Cross56a83212020-09-15 18:30:11 -07002071
2072 hideApexVariantFromMake bool
Jiyong Parke3833882020-02-17 17:28:10 +09002073}
2074
2075type sdkLibraryXmlProperties struct {
2076 // canonical name of the lib
2077 Lib_name *string
2078}
2079
2080// java_sdk_library_xml builds the permission xml file for a java_sdk_library.
2081// Not to be used directly by users. java_sdk_library internally uses this.
2082func sdkLibraryXmlFactory() android.Module {
2083 module := &sdkLibraryXml{}
2084
2085 module.AddProperties(&module.properties)
2086
2087 android.InitApexModule(module)
2088 android.InitAndroidArchModule(module, android.DeviceSupported, android.MultilibCommon)
2089
2090 return module
2091}
2092
Colin Crossaede88c2020-08-11 12:17:01 -07002093func (module *sdkLibraryXml) UniqueApexVariations() bool {
2094 // sdkLibraryXml needs a unique variation per APEX because the generated XML file contains the path to the
2095 // mounted APEX, which contains the name of the APEX.
2096 return true
2097}
2098
Jiyong Parke3833882020-02-17 17:28:10 +09002099// from android.PrebuiltEtcModule
Jooyung Han0703fd82020-08-26 22:11:53 +09002100func (module *sdkLibraryXml) BaseDir() string {
2101 return "etc"
2102}
2103
2104// from android.PrebuiltEtcModule
Jiyong Parke3833882020-02-17 17:28:10 +09002105func (module *sdkLibraryXml) SubDir() string {
2106 return "permissions"
2107}
2108
2109// from android.PrebuiltEtcModule
2110func (module *sdkLibraryXml) OutputFile() android.OutputPath {
2111 return module.outputFilePath
2112}
2113
2114// from android.ApexModule
2115func (module *sdkLibraryXml) AvailableFor(what string) bool {
2116 return true
2117}
2118
2119func (module *sdkLibraryXml) DepsMutator(ctx android.BottomUpMutatorContext) {
2120 // do nothing
2121}
2122
Dan Albertc8060532020-07-22 22:32:17 -07002123func (module *sdkLibraryXml) ShouldSupportSdkVersion(ctx android.BaseModuleContext,
2124 sdkVersion android.ApiLevel) error {
Jooyung Han749dc692020-04-15 11:03:39 +09002125 // sdkLibraryXml doesn't need to be checked separately because java_sdk_library is checked
2126 return nil
2127}
2128
Jiyong Parke3833882020-02-17 17:28:10 +09002129// File path to the runtime implementation library
Colin Cross56a83212020-09-15 18:30:11 -07002130func (module *sdkLibraryXml) implPath(ctx android.ModuleContext) string {
Jiyong Parke3833882020-02-17 17:28:10 +09002131 implName := proptools.String(module.properties.Lib_name)
Colin Cross56a83212020-09-15 18:30:11 -07002132 if apexInfo := ctx.Provider(android.ApexInfoProvider).(android.ApexInfo); !apexInfo.IsForPlatform() {
Colin Crosse07f2312020-08-13 11:24:56 -07002133 // TODO(b/146468504): ApexVariationName() is only a soong module name, not apex name.
Jiyong Parke3833882020-02-17 17:28:10 +09002134 // In most cases, this works fine. But when apex_name is set or override_apex is used
2135 // this can be wrong.
Colin Cross56a83212020-09-15 18:30:11 -07002136 return fmt.Sprintf("/apex/%s/javalib/%s.jar", apexInfo.ApexVariationName, implName)
Jiyong Parke3833882020-02-17 17:28:10 +09002137 }
2138 partition := "system"
2139 if module.SocSpecific() {
2140 partition = "vendor"
2141 } else if module.DeviceSpecific() {
2142 partition = "odm"
2143 } else if module.ProductSpecific() {
2144 partition = "product"
2145 } else if module.SystemExtSpecific() {
2146 partition = "system_ext"
2147 }
2148 return "/" + partition + "/framework/" + implName + ".jar"
2149}
2150
2151func (module *sdkLibraryXml) GenerateAndroidBuildActions(ctx android.ModuleContext) {
Colin Cross56a83212020-09-15 18:30:11 -07002152 module.hideApexVariantFromMake = !ctx.Provider(android.ApexInfoProvider).(android.ApexInfo).IsForPlatform()
2153
Jiyong Parke3833882020-02-17 17:28:10 +09002154 libName := proptools.String(module.properties.Lib_name)
Colin Cross56a83212020-09-15 18:30:11 -07002155 xmlContent := fmt.Sprintf(permissionsTemplate, libName, module.implPath(ctx))
Jiyong Parke3833882020-02-17 17:28:10 +09002156
2157 module.outputFilePath = android.PathForModuleOut(ctx, libName+".xml").OutputPath
2158 rule := android.NewRuleBuilder()
2159 rule.Command().
2160 Text("/bin/bash -c \"echo -e '" + xmlContent + "'\" > ").
2161 Output(module.outputFilePath)
2162
2163 rule.Build(pctx, ctx, "java_sdk_xml", "Permission XML")
2164
2165 module.installDirPath = android.PathForModuleInstall(ctx, "etc", module.SubDir())
2166}
2167
2168func (module *sdkLibraryXml) AndroidMkEntries() []android.AndroidMkEntries {
Colin Cross56a83212020-09-15 18:30:11 -07002169 if module.hideApexVariantFromMake {
Jiyong Parke3833882020-02-17 17:28:10 +09002170 return []android.AndroidMkEntries{android.AndroidMkEntries{
2171 Disabled: true,
2172 }}
2173 }
2174
2175 return []android.AndroidMkEntries{android.AndroidMkEntries{
2176 Class: "ETC",
2177 OutputFile: android.OptionalPathForPath(module.outputFilePath),
2178 ExtraEntries: []android.AndroidMkExtraEntriesFunc{
2179 func(entries *android.AndroidMkEntries) {
2180 entries.SetString("LOCAL_MODULE_TAGS", "optional")
2181 entries.SetString("LOCAL_MODULE_PATH", module.installDirPath.ToMakePath().String())
2182 entries.SetString("LOCAL_INSTALLED_MODULE_STEM", module.outputFilePath.Base())
2183 },
2184 },
2185 }}
2186}
Paul Duffindd46f712020-02-10 13:37:10 +00002187
2188type sdkLibrarySdkMemberType struct {
2189 android.SdkMemberTypeBase
2190}
2191
2192func (s *sdkLibrarySdkMemberType) AddDependencies(mctx android.BottomUpMutatorContext, dependencyTag blueprint.DependencyTag, names []string) {
2193 mctx.AddVariationDependencies(nil, dependencyTag, names...)
2194}
2195
2196func (s *sdkLibrarySdkMemberType) IsInstance(module android.Module) bool {
2197 _, ok := module.(*SdkLibrary)
2198 return ok
2199}
2200
2201func (s *sdkLibrarySdkMemberType) AddPrebuiltModule(ctx android.SdkMemberContext, member android.SdkMember) android.BpModule {
2202 return ctx.SnapshotBuilder().AddPrebuiltModule(member, "java_sdk_library_import")
2203}
2204
2205func (s *sdkLibrarySdkMemberType) CreateVariantPropertiesStruct() android.SdkMemberProperties {
2206 return &sdkLibrarySdkMemberProperties{}
2207}
2208
2209type sdkLibrarySdkMemberProperties struct {
2210 android.SdkMemberPropertiesBase
2211
2212 // Scope to per scope properties.
2213 Scopes map[*apiScope]scopeProperties
2214
2215 // Additional libraries that the exported stubs libraries depend upon.
2216 Libs []string
Paul Duffin3d1248c2020-04-09 00:10:17 +01002217
2218 // The Java stubs source files.
2219 Stub_srcs []string
Paul Duffinf7a64332020-05-13 16:54:55 +01002220
2221 // The naming scheme.
2222 Naming_scheme *string
Paul Duffind7eb1c22020-05-26 20:57:10 +01002223
2224 // True if the java_sdk_library_import is for a shared library, false
2225 // otherwise.
2226 Shared_library *bool
Paul Duffina2ae7e02020-09-11 11:55:00 +01002227
2228 // The paths to the doctag files to add to the prebuilt.
2229 Doctag_paths android.Paths
Paul Duffindd46f712020-02-10 13:37:10 +00002230}
2231
2232type scopeProperties struct {
Paul Duffin1fd005d2020-04-09 01:08:11 +01002233 Jars android.Paths
2234 StubsSrcJar android.Path
2235 CurrentApiFile android.Path
2236 RemovedApiFile android.Path
2237 SdkVersion string
Paul Duffindd46f712020-02-10 13:37:10 +00002238}
2239
2240func (s *sdkLibrarySdkMemberProperties) PopulateFromVariant(ctx android.SdkMemberContext, variant android.Module) {
2241 sdk := variant.(*SdkLibrary)
2242
2243 s.Scopes = make(map[*apiScope]scopeProperties)
2244 for _, apiScope := range allApiScopes {
Paul Duffin803a9562020-05-20 11:52:25 +01002245 paths := sdk.findScopePaths(apiScope)
2246 if paths == nil {
2247 continue
2248 }
2249
Paul Duffindd46f712020-02-10 13:37:10 +00002250 jars := paths.stubsImplPath
2251 if len(jars) > 0 {
2252 properties := scopeProperties{}
2253 properties.Jars = jars
Paul Duffin780c5f42020-05-12 15:52:55 +01002254 properties.SdkVersion = sdk.sdkVersionForStubsLibrary(ctx.SdkModuleContext(), apiScope)
Paul Duffin0f8faff2020-05-20 16:18:00 +01002255 properties.StubsSrcJar = paths.stubsSrcJar.Path()
Paul Duffin10269f12020-06-19 18:39:55 +01002256 if paths.currentApiFilePath.Valid() {
2257 properties.CurrentApiFile = paths.currentApiFilePath.Path()
2258 }
2259 if paths.removedApiFilePath.Valid() {
2260 properties.RemovedApiFile = paths.removedApiFilePath.Path()
2261 }
Paul Duffindd46f712020-02-10 13:37:10 +00002262 s.Scopes[apiScope] = properties
2263 }
2264 }
2265
2266 s.Libs = sdk.properties.Libs
Paul Duffindfa131e2020-05-15 20:37:11 +01002267 s.Naming_scheme = sdk.commonSdkLibraryProperties.Naming_scheme
Paul Duffind7eb1c22020-05-26 20:57:10 +01002268 s.Shared_library = proptools.BoolPtr(sdk.sharedLibrary())
Paul Duffina2ae7e02020-09-11 11:55:00 +01002269 s.Doctag_paths = sdk.doctagPaths
Paul Duffindd46f712020-02-10 13:37:10 +00002270}
2271
2272func (s *sdkLibrarySdkMemberProperties) AddToPropertySet(ctx android.SdkMemberContext, propertySet android.BpPropertySet) {
Paul Duffinf7a64332020-05-13 16:54:55 +01002273 if s.Naming_scheme != nil {
2274 propertySet.AddProperty("naming_scheme", proptools.String(s.Naming_scheme))
2275 }
Paul Duffind7eb1c22020-05-26 20:57:10 +01002276 if s.Shared_library != nil {
2277 propertySet.AddProperty("shared_library", *s.Shared_library)
2278 }
Paul Duffinf7a64332020-05-13 16:54:55 +01002279
Paul Duffindd46f712020-02-10 13:37:10 +00002280 for _, apiScope := range allApiScopes {
2281 if properties, ok := s.Scopes[apiScope]; ok {
Paul Duffin6b836ba2020-05-13 19:19:49 +01002282 scopeSet := propertySet.AddPropertySet(apiScope.propertyName)
Paul Duffindd46f712020-02-10 13:37:10 +00002283
Paul Duffin3d1248c2020-04-09 00:10:17 +01002284 scopeDir := filepath.Join("sdk_library", s.OsPrefix(), apiScope.name)
2285
Paul Duffindd46f712020-02-10 13:37:10 +00002286 var jars []string
2287 for _, p := range properties.Jars {
Paul Duffin3d1248c2020-04-09 00:10:17 +01002288 dest := filepath.Join(scopeDir, ctx.Name()+"-stubs.jar")
Paul Duffindd46f712020-02-10 13:37:10 +00002289 ctx.SnapshotBuilder().CopyToSnapshot(p, dest)
2290 jars = append(jars, dest)
2291 }
2292 scopeSet.AddProperty("jars", jars)
2293
Paul Duffin3d1248c2020-04-09 00:10:17 +01002294 // Merge the stubs source jar into the snapshot zip so that when it is unpacked
2295 // the source files are also unpacked.
2296 snapshotRelativeDir := filepath.Join(scopeDir, ctx.Name()+"_stub_sources")
2297 ctx.SnapshotBuilder().UnzipToSnapshot(properties.StubsSrcJar, snapshotRelativeDir)
2298 scopeSet.AddProperty("stub_srcs", []string{snapshotRelativeDir})
2299
Paul Duffin1fd005d2020-04-09 01:08:11 +01002300 if properties.CurrentApiFile != nil {
2301 currentApiSnapshotPath := filepath.Join(scopeDir, ctx.Name()+".txt")
2302 ctx.SnapshotBuilder().CopyToSnapshot(properties.CurrentApiFile, currentApiSnapshotPath)
2303 scopeSet.AddProperty("current_api", currentApiSnapshotPath)
2304 }
2305
2306 if properties.RemovedApiFile != nil {
2307 removedApiSnapshotPath := filepath.Join(scopeDir, ctx.Name()+"-removed.txt")
Paul Duffin3dbf9fd2020-06-02 13:00:02 +01002308 ctx.SnapshotBuilder().CopyToSnapshot(properties.RemovedApiFile, removedApiSnapshotPath)
Paul Duffin1fd005d2020-04-09 01:08:11 +01002309 scopeSet.AddProperty("removed_api", removedApiSnapshotPath)
2310 }
2311
Paul Duffindd46f712020-02-10 13:37:10 +00002312 if properties.SdkVersion != "" {
2313 scopeSet.AddProperty("sdk_version", properties.SdkVersion)
2314 }
2315 }
2316 }
2317
Paul Duffina2ae7e02020-09-11 11:55:00 +01002318 if len(s.Doctag_paths) > 0 {
2319 dests := []string{}
2320 for _, p := range s.Doctag_paths {
2321 dest := filepath.Join("doctags", p.Rel())
2322 ctx.SnapshotBuilder().CopyToSnapshot(p, dest)
2323 dests = append(dests, dest)
2324 }
2325 propertySet.AddProperty("doctag_files", dests)
2326 }
2327
Paul Duffindd46f712020-02-10 13:37:10 +00002328 if len(s.Libs) > 0 {
2329 propertySet.AddPropertyWithTag("libs", s.Libs, ctx.SnapshotBuilder().SdkMemberReferencePropertyTag(false))
2330 }
2331}