blob: 58d69ed537e8f9324575eef7be8dde92a7ec5f66 [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",
260 extends: apiScopePublic,
261 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
Sundong Ahnf043cf62018-06-25 16:04:37 +0900391 // List of Java libraries that will be in the classpath when building stubs
392 Stub_only_libs []string `android:"arch_variant"`
393
Paul Duffin7a586d32019-12-30 17:09:34 +0000394 // list of package names that will be documented and publicized as API.
395 // This allows the API to be restricted to a subset of the source files provided.
396 // If this is unspecified then all the source files will be treated as being part
397 // of the API.
Jiyong Parkc678ad32018-04-10 13:07:10 +0900398 Api_packages []string
399
Jiyong Park5a2c9d72018-05-01 22:25:41 +0900400 // list of package names that must be hidden from the API
401 Hidden_api_packages []string
402
Paul Duffin749f98f2019-12-30 17:23:46 +0000403 // the relative path to the directory containing the api specification files.
404 // Defaults to "api".
405 Api_dir *string
406
Paul Duffindfa131e2020-05-15 20:37:11 +0100407 // Determines whether a runtime implementation library is built; defaults to false.
408 //
409 // If true then it also prevents the module from being used as a shared module, i.e.
410 // it is as is shared_library: false, was set.
Paul Duffin43db9be2019-12-30 17:35:49 +0000411 Api_only *bool
412
Paul Duffin11512472019-02-11 15:55:17 +0000413 // local files that are used within user customized droiddoc options.
414 Droiddoc_option_files []string
415
416 // additional droiddoc options
417 // Available variables for substitution:
418 //
419 // $(location <label>): the path to the droiddoc_option_files with name <label>
Sundong Ahndd567f92018-07-31 17:19:11 +0900420 Droiddoc_options []string
421
Paul Duffine22c2ab2020-05-20 19:35:27 +0100422 // is set to true, Metalava will allow framework SDK to contain annotations.
423 Annotations_enabled *bool
424
Sundong Ahn054b19a2018-10-19 13:46:09 +0900425 // a list of top-level directories containing files to merge qualifier annotations
426 // (i.e. those intended to be included in the stubs written) from.
427 Merge_annotations_dirs []string
428
429 // a list of top-level directories containing Java stub files to merge show/hide annotations from.
430 Merge_inclusion_annotations_dirs []string
431
432 // If set to true, the path of dist files is apistubs/core. Defaults to false.
433 Core_lib *bool
434
Sundong Ahn80a87b32019-05-13 15:02:50 +0900435 // don't create dist rules.
436 No_dist *bool `blueprint:"mutated"`
437
Paul Duffin3375e352020-04-28 10:44:03 +0100438 // indicates whether system and test apis should be generated.
439 Generate_system_and_test_apis bool `blueprint:"mutated"`
440
441 // The properties specific to the public api scope
442 //
443 // Unless explicitly specified by using public.enabled the public api scope is
444 // enabled by default in both legacy and non-legacy mode.
445 Public ApiScopeProperties
446
447 // The properties specific to the system api scope
448 //
449 // In legacy mode the system api scope is enabled by default when sdk_version
450 // is set to something other than "none".
451 //
452 // In non-legacy mode the system api scope is disabled by default.
453 System ApiScopeProperties
454
455 // The properties specific to the test api scope
456 //
457 // In legacy mode the test api scope is enabled by default when sdk_version
458 // is set to something other than "none".
459 //
460 // In non-legacy mode the test api scope is disabled by default.
461 Test ApiScopeProperties
Paul Duffin37e0b772019-12-30 17:20:10 +0000462
Paul Duffin0c5bae52020-06-02 13:00:08 +0100463 // The properties specific to the module-lib api scope
Paul Duffin8f265b92020-04-28 14:13:56 +0100464 //
Paul Duffin0c5bae52020-06-02 13:00:08 +0100465 // Unless explicitly specified by using test.enabled the module-lib api scope is
Paul Duffin8f265b92020-04-28 14:13:56 +0100466 // disabled by default.
467 Module_lib ApiScopeProperties
468
Paul Duffin0c5bae52020-06-02 13:00:08 +0100469 // The properties specific to the system-server api scope
470 //
471 // Unless explicitly specified by using test.enabled the module-lib api scope is
472 // disabled by default.
473 System_server ApiScopeProperties
474
Jiyong Park932cdfe2020-05-28 00:19:53 +0900475 // Determines if the stubs are preferred over the implementation library
476 // for linking, even when the client doesn't specify sdk_version. When this
477 // is set to true, such clients are provided with the widest API surface that
478 // this lib provides. Note however that this option doesn't affect the clients
479 // that are in the same APEX as this library. In that case, the clients are
480 // always linked with the implementation library. Default is false.
481 Default_to_stubs *bool
482
Paul Duffin160fe412020-05-10 19:32:20 +0100483 // Properties related to api linting.
484 Api_lint struct {
485 // Enable api linting.
486 Enabled *bool
487 }
488
Jiyong Parkc678ad32018-04-10 13:07:10 +0900489 // TODO: determines whether to create HTML doc or not
490 //Html_doc *bool
491}
492
Paul Duffin0f8faff2020-05-20 16:18:00 +0100493// Paths to outputs from java_sdk_library and java_sdk_library_import.
494//
495// Fields that are android.Paths are always set (during GenerateAndroidBuildActions).
496// OptionalPaths are always set by java_sdk_library but may not be set by
497// java_sdk_library_import as not all instances provide that information.
Paul Duffind1b3a922020-01-22 11:57:20 +0000498type scopePaths struct {
Paul Duffin0f8faff2020-05-20 16:18:00 +0100499 // The path (represented as Paths for convenience when returning) to the stubs header jar.
500 //
501 // That is the jar that is created by turbine.
502 stubsHeaderPath android.Paths
503
504 // The path (represented as Paths for convenience when returning) to the stubs implementation jar.
505 //
506 // This is not the implementation jar, it still only contains stubs.
507 stubsImplPath android.Paths
508
509 // The API specification file, e.g. system_current.txt.
510 currentApiFilePath android.OptionalPath
511
512 // The specification of API elements removed since the last release.
513 removedApiFilePath android.OptionalPath
514
515 // The stubs source jar.
516 stubsSrcJar android.OptionalPath
Paul Duffind1b3a922020-01-22 11:57:20 +0000517}
518
Paul Duffinc8782502020-04-29 20:45:27 +0100519func (paths *scopePaths) extractStubsLibraryInfoFromDependency(dep android.Module) error {
520 if lib, ok := dep.(Dependency); ok {
521 paths.stubsHeaderPath = lib.HeaderJars()
522 paths.stubsImplPath = lib.ImplementationJars()
523 return nil
524 } else {
525 return fmt.Errorf("expected module that implements Dependency, e.g. java_library")
526 }
527}
528
Paul Duffin0ff08bd2020-04-29 13:30:54 +0100529func (paths *scopePaths) treatDepAsApiStubsProvider(dep android.Module, action func(provider ApiStubsProvider)) error {
530 if apiStubsProvider, ok := dep.(ApiStubsProvider); ok {
531 action(apiStubsProvider)
Paul Duffinc8782502020-04-29 20:45:27 +0100532 return nil
533 } else {
534 return fmt.Errorf("expected module that implements ApiStubsProvider, e.g. droidstubs")
535 }
536}
537
Paul Duffin0f8faff2020-05-20 16:18:00 +0100538func (paths *scopePaths) treatDepAsApiStubsSrcProvider(dep android.Module, action func(provider ApiStubsSrcProvider)) error {
539 if apiStubsProvider, ok := dep.(ApiStubsSrcProvider); ok {
540 action(apiStubsProvider)
541 return nil
542 } else {
543 return fmt.Errorf("expected module that implements ApiStubsSrcProvider, e.g. droidstubs")
544 }
545}
546
Paul Duffin0ff08bd2020-04-29 13:30:54 +0100547func (paths *scopePaths) extractApiInfoFromApiStubsProvider(provider ApiStubsProvider) {
Paul Duffin0f8faff2020-05-20 16:18:00 +0100548 paths.currentApiFilePath = android.OptionalPathForPath(provider.ApiFilePath())
549 paths.removedApiFilePath = android.OptionalPathForPath(provider.RemovedApiFilePath())
Paul Duffin0ff08bd2020-04-29 13:30:54 +0100550}
551
552func (paths *scopePaths) extractApiInfoFromDep(dep android.Module) error {
553 return paths.treatDepAsApiStubsProvider(dep, func(provider ApiStubsProvider) {
554 paths.extractApiInfoFromApiStubsProvider(provider)
555 })
556}
557
Paul Duffin0f8faff2020-05-20 16:18:00 +0100558func (paths *scopePaths) extractStubsSourceInfoFromApiStubsProviders(provider ApiStubsSrcProvider) {
559 paths.stubsSrcJar = android.OptionalPathForPath(provider.StubsSrcJar())
Paul Duffin0ff08bd2020-04-29 13:30:54 +0100560}
561
562func (paths *scopePaths) extractStubsSourceInfoFromDep(dep android.Module) error {
Paul Duffin0f8faff2020-05-20 16:18:00 +0100563 return paths.treatDepAsApiStubsSrcProvider(dep, func(provider ApiStubsSrcProvider) {
Paul Duffin0ff08bd2020-04-29 13:30:54 +0100564 paths.extractStubsSourceInfoFromApiStubsProviders(provider)
565 })
566}
567
568func (paths *scopePaths) extractStubsSourceAndApiInfoFromApiStubsProvider(dep android.Module) error {
569 return paths.treatDepAsApiStubsProvider(dep, func(provider ApiStubsProvider) {
570 paths.extractApiInfoFromApiStubsProvider(provider)
571 paths.extractStubsSourceInfoFromApiStubsProviders(provider)
572 })
573}
574
575type commonToSdkLibraryAndImportProperties struct {
Paul Duffin1b1e8062020-05-08 13:44:43 +0100576 // The naming scheme to use for the components that this module creates.
577 //
Paul Duffinee9ad5d2020-09-11 13:04:05 +0100578 // If not specified then it defaults to "default".
Paul Duffin1b1e8062020-05-08 13:44:43 +0100579 //
580 // This is a temporary mechanism to simplify conversion from separate modules for each
581 // component that follow a different naming pattern to the default one.
582 //
583 // TODO(b/155480189) - Remove once naming inconsistencies have been resolved.
Paul Duffin0ff08bd2020-04-29 13:30:54 +0100584 Naming_scheme *string
Paul Duffindfa131e2020-05-15 20:37:11 +0100585
586 // Specifies whether this module can be used as an Android shared library; defaults
587 // to true.
588 //
589 // An Android shared library is one that can be referenced in a <uses-library> element
590 // in an AndroidManifest.xml.
591 Shared_library *bool
Paul Duffin0ff08bd2020-04-29 13:30:54 +0100592}
593
Paul Duffin56d44902020-01-31 13:36:25 +0000594// Common code between sdk library and sdk library import
595type commonToSdkLibraryAndImport struct {
Paul Duffinc3091c82020-05-08 14:16:20 +0100596 moduleBase *android.ModuleBase
597
Paul Duffin56d44902020-01-31 13:36:25 +0000598 scopePaths map[*apiScope]*scopePaths
Paul Duffin1b1e8062020-05-08 13:44:43 +0100599
600 namingScheme sdkLibraryComponentNamingScheme
601
Paul Duffindfa131e2020-05-15 20:37:11 +0100602 commonSdkLibraryProperties commonToSdkLibraryAndImportProperties
Paul Duffin859fe962020-05-15 10:20:31 +0100603
604 // Functionality related to this being used as a component of a java_sdk_library.
605 EmbeddableSdkLibraryComponent
Paul Duffin56d44902020-01-31 13:36:25 +0000606}
607
Paul Duffinc3091c82020-05-08 14:16:20 +0100608func (c *commonToSdkLibraryAndImport) initCommon(moduleBase *android.ModuleBase) {
609 c.moduleBase = moduleBase
Paul Duffin1b1e8062020-05-08 13:44:43 +0100610
Paul Duffindfa131e2020-05-15 20:37:11 +0100611 moduleBase.AddProperties(&c.commonSdkLibraryProperties)
Paul Duffin859fe962020-05-15 10:20:31 +0100612
613 // Initialize this as an sdk library component.
614 c.initSdkLibraryComponent(moduleBase)
Paul Duffin1b1e8062020-05-08 13:44:43 +0100615}
616
617func (c *commonToSdkLibraryAndImport) initCommonAfterDefaultsApplied(ctx android.DefaultableHookContext) bool {
Paul Duffindfa131e2020-05-15 20:37:11 +0100618 schemeProperty := proptools.StringDefault(c.commonSdkLibraryProperties.Naming_scheme, "default")
Paul Duffin1b1e8062020-05-08 13:44:43 +0100619 switch schemeProperty {
620 case "default":
621 c.namingScheme = &defaultNamingScheme{}
622 default:
623 ctx.PropertyErrorf("naming_scheme", "expected 'default' but was %q", schemeProperty)
624 return false
625 }
626
Paul Duffindfa131e2020-05-15 20:37:11 +0100627 // Only track this sdk library if this can be used as a shared library.
628 if c.sharedLibrary() {
629 // Use the name specified in the module definition as the owner.
630 c.sdkLibraryComponentProperties.SdkLibraryToImplicitlyTrack = proptools.StringPtr(c.moduleBase.BaseModuleName())
631 }
Paul Duffin859fe962020-05-15 10:20:31 +0100632
Paul Duffin1b1e8062020-05-08 13:44:43 +0100633 return true
Paul Duffinc3091c82020-05-08 14:16:20 +0100634}
635
Paul Duffineedc5d52020-06-12 17:46:39 +0100636// Module name of the runtime implementation library
637func (c *commonToSdkLibraryAndImport) implLibraryModuleName() string {
638 return c.moduleBase.BaseModuleName() + ".impl"
639}
640
641// Module name of the XML file for the lib
642func (c *commonToSdkLibraryAndImport) xmlPermissionsModuleName() string {
643 return c.moduleBase.BaseModuleName() + sdkXmlFileSuffix
644}
645
Paul Duffinc3091c82020-05-08 14:16:20 +0100646// Name of the java_library module that compiles the stubs source.
647func (c *commonToSdkLibraryAndImport) stubsLibraryModuleName(apiScope *apiScope) string {
Paul Duffin1b1e8062020-05-08 13:44:43 +0100648 return c.namingScheme.stubsLibraryModuleName(apiScope, c.moduleBase.BaseModuleName())
Paul Duffinc3091c82020-05-08 14:16:20 +0100649}
650
651// Name of the droidstubs module that generates the stubs source and may also
652// generate/check the API.
653func (c *commonToSdkLibraryAndImport) stubsSourceModuleName(apiScope *apiScope) string {
Paul Duffin1b1e8062020-05-08 13:44:43 +0100654 return c.namingScheme.stubsSourceModuleName(apiScope, c.moduleBase.BaseModuleName())
Paul Duffinc3091c82020-05-08 14:16:20 +0100655}
656
657// Name of the droidstubs module that generates/checks the API. Only used if it
658// requires different arts to the stubs source generating module.
659func (c *commonToSdkLibraryAndImport) apiModuleName(apiScope *apiScope) string {
Paul Duffin1b1e8062020-05-08 13:44:43 +0100660 return c.namingScheme.apiModuleName(apiScope, c.moduleBase.BaseModuleName())
Paul Duffinc3091c82020-05-08 14:16:20 +0100661}
662
Paul Duffin46dc45a2020-05-14 15:39:10 +0100663// The component names for different outputs of the java_sdk_library.
664//
665// They are similar to the names used for the child modules it creates
666const (
667 stubsSourceComponentName = "stubs.source"
668
669 apiTxtComponentName = "api.txt"
670
671 removedApiTxtComponentName = "removed-api.txt"
672)
673
674// A regular expression to match tags that reference a specific stubs component.
675//
676// It will only match if given a valid scope and a valid component. It is verfy strict
677// to ensure it does not accidentally match a similar looking tag that should be processed
678// by the embedded Library.
679var tagSplitter = func() *regexp.Regexp {
680 // Given a list of literal string items returns a regular expression that will
681 // match any one of the items.
682 choice := func(items ...string) string {
683 return `\Q` + strings.Join(items, `\E|\Q`) + `\E`
684 }
685
686 // Regular expression to match one of the scopes.
687 scopesRegexp := choice(allScopeNames...)
688
689 // Regular expression to match one of the components.
690 componentsRegexp := choice(stubsSourceComponentName, apiTxtComponentName, removedApiTxtComponentName)
691
692 // Regular expression to match any combination of one scope and one component.
693 return regexp.MustCompile(fmt.Sprintf(`^\.(%s)\.(%s)$`, scopesRegexp, componentsRegexp))
694}()
695
696// For OutputFileProducer interface
697//
698// .<scope>.stubs.source
699// .<scope>.api.txt
700// .<scope>.removed-api.txt
701func (c *commonToSdkLibraryAndImport) commonOutputFiles(tag string) (android.Paths, error) {
702 if groups := tagSplitter.FindStringSubmatch(tag); groups != nil {
703 scopeName := groups[1]
704 component := groups[2]
705
706 if scope, ok := scopeByName[scopeName]; ok {
707 paths := c.findScopePaths(scope)
708 if paths == nil {
709 return nil, fmt.Errorf("%q does not provide api scope %s", c.moduleBase.BaseModuleName(), scopeName)
710 }
711
712 switch component {
713 case stubsSourceComponentName:
714 if paths.stubsSrcJar.Valid() {
715 return android.Paths{paths.stubsSrcJar.Path()}, nil
716 }
717
718 case apiTxtComponentName:
719 if paths.currentApiFilePath.Valid() {
720 return android.Paths{paths.currentApiFilePath.Path()}, nil
721 }
722
723 case removedApiTxtComponentName:
724 if paths.removedApiFilePath.Valid() {
725 return android.Paths{paths.removedApiFilePath.Path()}, nil
726 }
727 }
728
729 return nil, fmt.Errorf("%s not available for api scope %s", component, scopeName)
730 } else {
731 return nil, fmt.Errorf("unknown scope %s in %s", scope, tag)
732 }
733
734 } else {
735 return nil, nil
736 }
737}
738
Paul Duffin803a9562020-05-20 11:52:25 +0100739func (c *commonToSdkLibraryAndImport) getScopePathsCreateIfNeeded(scope *apiScope) *scopePaths {
Paul Duffin56d44902020-01-31 13:36:25 +0000740 if c.scopePaths == nil {
741 c.scopePaths = make(map[*apiScope]*scopePaths)
742 }
743 paths := c.scopePaths[scope]
744 if paths == nil {
745 paths = &scopePaths{}
746 c.scopePaths[scope] = paths
747 }
748
749 return paths
750}
751
Paul Duffin803a9562020-05-20 11:52:25 +0100752func (c *commonToSdkLibraryAndImport) findScopePaths(scope *apiScope) *scopePaths {
753 if c.scopePaths == nil {
754 return nil
755 }
756
757 return c.scopePaths[scope]
758}
759
760// If this does not support the requested api scope then find the closest available
761// scope it does support. Returns nil if no such scope is available.
762func (c *commonToSdkLibraryAndImport) findClosestScopePath(scope *apiScope) *scopePaths {
763 for s := scope; s != nil; s = s.extends {
764 if paths := c.findScopePaths(s); paths != nil {
765 return paths
766 }
767 }
768
769 // This should never happen outside tests as public should be the base scope for every
770 // scope and is enabled by default.
771 return nil
772}
773
Paul Duffin23970f42020-05-20 14:20:02 +0100774func (c *commonToSdkLibraryAndImport) selectHeaderJarsForSdkVersion(ctx android.BaseModuleContext, sdkVersion sdkSpec) android.Paths {
Paul Duffinb05d4292020-05-20 12:19:10 +0100775
776 // If a specific numeric version has been requested then use prebuilt versions of the sdk.
777 if sdkVersion.version.isNumbered() {
778 return PrebuiltJars(ctx, c.moduleBase.BaseModuleName(), sdkVersion)
779 }
780
781 var apiScope *apiScope
782 switch sdkVersion.kind {
783 case sdkSystem:
784 apiScope = apiScopeSystem
Paul Duffin803a9562020-05-20 11:52:25 +0100785 case sdkModule:
786 apiScope = apiScopeModuleLib
Paul Duffinb05d4292020-05-20 12:19:10 +0100787 case sdkTest:
788 apiScope = apiScopeTest
Paul Duffin0c5bae52020-06-02 13:00:08 +0100789 case sdkSystemServer:
790 apiScope = apiScopeSystemServer
Paul Duffinb05d4292020-05-20 12:19:10 +0100791 default:
792 apiScope = apiScopePublic
793 }
794
Paul Duffin803a9562020-05-20 11:52:25 +0100795 paths := c.findClosestScopePath(apiScope)
796 if paths == nil {
797 var scopes []string
798 for _, s := range allApiScopes {
799 if c.findScopePaths(s) != nil {
800 scopes = append(scopes, s.name)
801 }
802 }
803 ctx.ModuleErrorf("requires api scope %s from %s but it only has %q available", apiScope.name, c.moduleBase.BaseModuleName(), scopes)
804 return nil
805 }
806
Paul Duffin23970f42020-05-20 14:20:02 +0100807 return paths.stubsHeaderPath
Paul Duffinb05d4292020-05-20 12:19:10 +0100808}
809
Paul Duffin859fe962020-05-15 10:20:31 +0100810func (c *commonToSdkLibraryAndImport) sdkComponentPropertiesForChildLibrary() interface{} {
811 componentProps := &struct {
812 SdkLibraryToImplicitlyTrack *string
Paul Duffindfa131e2020-05-15 20:37:11 +0100813 }{}
814
815 if c.sharedLibrary() {
Paul Duffin859fe962020-05-15 10:20:31 +0100816 // Mark the stubs library as being components of this java_sdk_library so that
817 // any app that includes code which depends (directly or indirectly) on the stubs
818 // library will have the appropriate <uses-library> invocation inserted into its
819 // manifest if necessary.
Paul Duffindfa131e2020-05-15 20:37:11 +0100820 componentProps.SdkLibraryToImplicitlyTrack = proptools.StringPtr(c.moduleBase.BaseModuleName())
Paul Duffin859fe962020-05-15 10:20:31 +0100821 }
822
823 return componentProps
824}
825
Paul Duffindfa131e2020-05-15 20:37:11 +0100826// Check if this can be used as a shared library.
827func (c *commonToSdkLibraryAndImport) sharedLibrary() bool {
828 return proptools.BoolDefault(c.commonSdkLibraryProperties.Shared_library, true)
829}
830
Paul Duffin859fe962020-05-15 10:20:31 +0100831// Properties related to the use of a module as an component of a java_sdk_library.
832type SdkLibraryComponentProperties struct {
833
834 // The name of the java_sdk_library/_import to add to a <uses-library> entry
835 // in the AndroidManifest.xml of any Android app that includes code that references
836 // this module. If not set then no java_sdk_library/_import is tracked.
837 SdkLibraryToImplicitlyTrack *string `blueprint:"mutated"`
838}
839
840// Structure to be embedded in a module struct that needs to support the
841// SdkLibraryComponentDependency interface.
842type EmbeddableSdkLibraryComponent struct {
843 sdkLibraryComponentProperties SdkLibraryComponentProperties
844}
845
846func (e *EmbeddableSdkLibraryComponent) initSdkLibraryComponent(moduleBase *android.ModuleBase) {
847 moduleBase.AddProperties(&e.sdkLibraryComponentProperties)
848}
849
850// to satisfy SdkLibraryComponentDependency
Ulya Trafimovich31e444e2020-08-14 17:32:16 +0100851func (e *EmbeddableSdkLibraryComponent) OptionalImplicitSdkLibrary() *string {
852 return e.sdkLibraryComponentProperties.SdkLibraryToImplicitlyTrack
Paul Duffin859fe962020-05-15 10:20:31 +0100853}
854
Ulya Trafimovich39b437b2020-09-23 16:42:35 +0100855// to satisfy SdkLibraryComponentDependency
856func (e *EmbeddableSdkLibraryComponent) OptionalSdkLibraryImplementation() *string {
857 // Currently implementation library name is the same as the SDK library name.
858 return e.sdkLibraryComponentProperties.SdkLibraryToImplicitlyTrack
859}
860
Paul Duffin859fe962020-05-15 10:20:31 +0100861// Implemented by modules that are (or possibly could be) a component of a java_sdk_library
862// (including the java_sdk_library) itself.
863type SdkLibraryComponentDependency interface {
Ulya Trafimovich31e444e2020-08-14 17:32:16 +0100864 UsesLibraryDependency
865
Paul Duffin859fe962020-05-15 10:20:31 +0100866 // The optional name of the sdk library that should be implicitly added to the
867 // AndroidManifest of an app that contains code which references the sdk library.
868 //
Ulya Trafimovich31e444e2020-08-14 17:32:16 +0100869 // Returns the name of the optional implicit SDK library or nil, if there isn't one.
870 OptionalImplicitSdkLibrary() *string
Ulya Trafimovich39b437b2020-09-23 16:42:35 +0100871
872 // The name of the implementation library for the optional SDK library or nil, if there isn't one.
873 OptionalSdkLibraryImplementation() *string
Paul Duffin859fe962020-05-15 10:20:31 +0100874}
875
876// Make sure that all the module types that are components of java_sdk_library/_import
877// and which can be referenced (directly or indirectly) from an android app implement
878// the SdkLibraryComponentDependency interface.
879var _ SdkLibraryComponentDependency = (*Library)(nil)
880var _ SdkLibraryComponentDependency = (*Import)(nil)
881var _ SdkLibraryComponentDependency = (*SdkLibrary)(nil)
Paul Duffineedc5d52020-06-12 17:46:39 +0100882var _ SdkLibraryComponentDependency = (*SdkLibraryImport)(nil)
Paul Duffin859fe962020-05-15 10:20:31 +0100883
884// Provides access to sdk_version related header and implentation jars.
885type SdkLibraryDependency interface {
886 SdkLibraryComponentDependency
887
888 // Get the header jars appropriate for the supplied sdk_version.
889 //
890 // These are turbine generated jars so they only change if the externals of the
891 // class changes but it does not contain and implementation or JavaDoc.
892 SdkHeaderJars(ctx android.BaseModuleContext, sdkVersion sdkSpec) android.Paths
893
894 // Get the implementation jars appropriate for the supplied sdk version.
895 //
896 // These are either the implementation jar for the whole sdk library or the implementation
897 // jars for the stubs. The latter should only be needed when generating JavaDoc as otherwise
898 // they are identical to the corresponding header jars.
899 SdkImplementationJars(ctx android.BaseModuleContext, sdkVersion sdkSpec) android.Paths
900}
901
Inseob Kimc0907f12019-02-08 21:00:45 +0900902type SdkLibrary struct {
Sundong Ahn054b19a2018-10-19 13:46:09 +0900903 Library
Jiyong Parkc678ad32018-04-10 13:07:10 +0900904
Sundong Ahn054b19a2018-10-19 13:46:09 +0900905 sdkLibraryProperties sdkLibraryProperties
Jiyong Parkc678ad32018-04-10 13:07:10 +0900906
Paul Duffin3375e352020-04-28 10:44:03 +0100907 // Map from api scope to the scope specific property structure.
908 scopeToProperties map[*apiScope]*ApiScopeProperties
909
Paul Duffin56d44902020-01-31 13:36:25 +0000910 commonToSdkLibraryAndImport
Jiyong Parkc678ad32018-04-10 13:07:10 +0900911}
912
Inseob Kimc0907f12019-02-08 21:00:45 +0900913var _ Dependency = (*SdkLibrary)(nil)
914var _ SdkLibraryDependency = (*SdkLibrary)(nil)
Colin Cross897d2ed2019-02-11 14:03:51 -0800915
Paul Duffin3375e352020-04-28 10:44:03 +0100916func (module *SdkLibrary) generateTestAndSystemScopesByDefault() bool {
917 return module.sdkLibraryProperties.Generate_system_and_test_apis
918}
919
920func (module *SdkLibrary) getGeneratedApiScopes(ctx android.EarlyModuleContext) apiScopes {
921 // Check to see if any scopes have been explicitly enabled. If any have then all
922 // must be.
923 anyScopesExplicitlyEnabled := false
924 for _, scope := range allApiScopes {
925 scopeProperties := module.scopeToProperties[scope]
926 if scopeProperties.Enabled != nil {
927 anyScopesExplicitlyEnabled = true
928 break
929 }
Paul Duffind1b3a922020-01-22 11:57:20 +0000930 }
Paul Duffin3375e352020-04-28 10:44:03 +0100931
932 var generatedScopes apiScopes
933 enabledScopes := make(map[*apiScope]struct{})
934 for _, scope := range allApiScopes {
935 scopeProperties := module.scopeToProperties[scope]
936 // If any scopes are explicitly enabled then ignore the legacy enabled status.
937 // This is to ensure that any new usages of this module type do not rely on legacy
938 // behaviour.
939 defaultEnabledStatus := false
940 if anyScopesExplicitlyEnabled {
941 defaultEnabledStatus = scope.defaultEnabledStatus
942 } else {
943 defaultEnabledStatus = scope.legacyEnabledStatus(module)
944 }
945 enabled := proptools.BoolDefault(scopeProperties.Enabled, defaultEnabledStatus)
946 if enabled {
947 enabledScopes[scope] = struct{}{}
948 generatedScopes = append(generatedScopes, scope)
949 }
950 }
951
952 // Now check to make sure that any scope that is extended by an enabled scope is also
953 // enabled.
954 for _, scope := range allApiScopes {
955 if _, ok := enabledScopes[scope]; ok {
956 extends := scope.extends
957 if extends != nil {
958 if _, ok := enabledScopes[extends]; !ok {
959 ctx.ModuleErrorf("enabled api scope %q depends on disabled scope %q", scope, extends)
960 }
961 }
962 }
963 }
964
965 return generatedScopes
Paul Duffind1b3a922020-01-22 11:57:20 +0000966}
967
Paul Duffineedc5d52020-06-12 17:46:39 +0100968type sdkLibraryComponentTag struct {
969 blueprint.BaseDependencyTag
970 name string
971}
972
973// Mark this tag so dependencies that use it are excluded from visibility enforcement.
974func (t sdkLibraryComponentTag) ExcludeFromVisibilityEnforcement() {}
975
976var xmlPermissionsFileTag = sdkLibraryComponentTag{name: "xml-permissions-file"}
Paul Duffine74ac732020-02-06 13:51:46 +0000977
Jiyong Parke3833882020-02-17 17:28:10 +0900978func IsXmlPermissionsFileDepTag(depTag blueprint.DependencyTag) bool {
Paul Duffineedc5d52020-06-12 17:46:39 +0100979 if dt, ok := depTag.(sdkLibraryComponentTag); ok {
Jiyong Parke3833882020-02-17 17:28:10 +0900980 return dt == xmlPermissionsFileTag
981 }
982 return false
983}
984
Paul Duffineedc5d52020-06-12 17:46:39 +0100985var implLibraryTag = sdkLibraryComponentTag{name: "impl-library"}
Paul Duffin5df79302020-05-16 15:52:12 +0100986
Paul Duffin44f1d842020-06-26 20:17:02 +0100987// Add the dependencies on the child modules in the component deps mutator.
988func (module *SdkLibrary) ComponentDepsMutator(ctx android.BottomUpMutatorContext) {
Paul Duffin3375e352020-04-28 10:44:03 +0100989 for _, apiScope := range module.getGeneratedApiScopes(ctx) {
Paul Duffind1b3a922020-01-22 11:57:20 +0000990 // Add dependencies to the stubs library
Paul Duffinc3091c82020-05-08 14:16:20 +0100991 ctx.AddVariationDependencies(nil, apiScope.stubsTag, module.stubsLibraryModuleName(apiScope))
Paul Duffind1b3a922020-01-22 11:57:20 +0000992
Paul Duffin15f34ef2020-07-20 18:04:44 +0100993 // Add a dependency on the stubs source in order to access both stubs source and api information.
994 ctx.AddVariationDependencies(nil, apiScope.stubsSourceAndApiTag, module.stubsSourceModuleName(apiScope))
Sundong Ahn054b19a2018-10-19 13:46:09 +0900995 }
996
Paul Duffindfa131e2020-05-15 20:37:11 +0100997 if module.requiresRuntimeImplementationLibrary() {
Paul Duffin5df79302020-05-16 15:52:12 +0100998 // Add dependency to the rule for generating the implementation library.
999 ctx.AddDependency(module, implLibraryTag, module.implLibraryModuleName())
1000
Paul Duffindfa131e2020-05-15 20:37:11 +01001001 if module.sharedLibrary() {
1002 // Add dependency to the rule for generating the xml permissions file
Paul Duffineedc5d52020-06-12 17:46:39 +01001003 ctx.AddDependency(module, xmlPermissionsFileTag, module.xmlPermissionsModuleName())
Paul Duffindfa131e2020-05-15 20:37:11 +01001004 }
Paul Duffin44f1d842020-06-26 20:17:02 +01001005 }
1006}
Paul Duffine74ac732020-02-06 13:51:46 +00001007
Paul Duffin44f1d842020-06-26 20:17:02 +01001008// Add other dependencies as normal.
1009func (module *SdkLibrary) DepsMutator(ctx android.BottomUpMutatorContext) {
1010 if module.requiresRuntimeImplementationLibrary() {
Paul Duffindfa131e2020-05-15 20:37:11 +01001011 // Only add the deps for the library if it is actually going to be built.
1012 module.Library.deps(ctx)
1013 }
Jiyong Parkc678ad32018-04-10 13:07:10 +09001014}
1015
Paul Duffin46dc45a2020-05-14 15:39:10 +01001016func (module *SdkLibrary) OutputFiles(tag string) (android.Paths, error) {
1017 paths, err := module.commonOutputFiles(tag)
1018 if paths == nil && err == nil {
1019 return module.Library.OutputFiles(tag)
1020 } else {
1021 return paths, err
1022 }
1023}
1024
Inseob Kimc0907f12019-02-08 21:00:45 +09001025func (module *SdkLibrary) GenerateAndroidBuildActions(ctx android.ModuleContext) {
Paul Duffindfa131e2020-05-15 20:37:11 +01001026 // Only build an implementation library if required.
1027 if module.requiresRuntimeImplementationLibrary() {
Paul Duffin43db9be2019-12-30 17:35:49 +00001028 module.Library.GenerateAndroidBuildActions(ctx)
1029 }
Sundong Ahn054b19a2018-10-19 13:46:09 +09001030
Sundong Ahn57368eb2018-07-06 11:20:23 +09001031 // Record the paths to the header jars of the library (stubs and impl).
Paul Duffind1b3a922020-01-22 11:57:20 +00001032 // When this java_sdk_library is depended upon from others via "libs" property,
Jiyong Parkc678ad32018-04-10 13:07:10 +09001033 // the recorded paths will be returned depending on the link type of the caller.
1034 ctx.VisitDirectDeps(func(to android.Module) {
Jiyong Parkc678ad32018-04-10 13:07:10 +09001035 tag := ctx.OtherModuleDependencyTag(to)
1036
Paul Duffinc8782502020-04-29 20:45:27 +01001037 // Extract information from any of the scope specific dependencies.
1038 if scopeTag, ok := tag.(scopeDependencyTag); ok {
1039 apiScope := scopeTag.apiScope
Paul Duffin803a9562020-05-20 11:52:25 +01001040 scopePaths := module.getScopePathsCreateIfNeeded(apiScope)
Paul Duffinc8782502020-04-29 20:45:27 +01001041
1042 // Extract information from the dependency. The exact information extracted
1043 // is determined by the nature of the dependency which is determined by the tag.
1044 scopeTag.extractDepInfo(ctx, to, scopePaths)
Sundong Ahn20e998b2018-07-24 11:19:26 +09001045 }
Jiyong Parkc678ad32018-04-10 13:07:10 +09001046 })
1047}
1048
Jiyong Park0b0e1b92019-12-03 13:24:29 +09001049func (module *SdkLibrary) AndroidMkEntries() []android.AndroidMkEntries {
Paul Duffindfa131e2020-05-15 20:37:11 +01001050 if !module.requiresRuntimeImplementationLibrary() {
Paul Duffin43db9be2019-12-30 17:35:49 +00001051 return nil
1052 }
Jiyong Park0b0e1b92019-12-03 13:24:29 +09001053 entriesList := module.Library.AndroidMkEntries()
Yo Chiang07d75072020-06-05 17:43:19 +08001054 if module.sharedLibrary() {
1055 entries := &entriesList[0]
1056 entries.Required = append(entries.Required, module.xmlPermissionsModuleName())
1057 }
Jiyong Park0b0e1b92019-12-03 13:24:29 +09001058 return entriesList
Jiyong Park82484c02018-04-23 21:41:26 +09001059}
1060
Anton Hansson5fd5d242020-03-27 19:43:19 +00001061// The dist path of the stub artifacts
1062func (module *SdkLibrary) apiDistPath(apiScope *apiScope) string {
1063 if module.ModuleBase.Owner() != "" {
1064 return path.Join("apistubs", module.ModuleBase.Owner(), apiScope.name)
1065 } else if Bool(module.sdkLibraryProperties.Core_lib) {
1066 return path.Join("apistubs", "core", apiScope.name)
1067 } else {
1068 return path.Join("apistubs", "android", apiScope.name)
1069 }
1070}
1071
Paul Duffin12ceb462019-12-24 20:31:31 +00001072// Get the sdk version for use when compiling the stubs library.
Paul Duffin780c5f42020-05-12 15:52:55 +01001073func (module *SdkLibrary) sdkVersionForStubsLibrary(mctx android.EarlyModuleContext, apiScope *apiScope) string {
Paul Duffin87a05a32020-05-12 11:50:28 +01001074 scopeProperties := module.scopeToProperties[apiScope]
1075 if scopeProperties.Sdk_version != nil {
1076 return proptools.String(scopeProperties.Sdk_version)
1077 }
1078
Paul Duffin12ceb462019-12-24 20:31:31 +00001079 sdkDep := decodeSdkDep(mctx, sdkContext(&module.Library))
1080 if sdkDep.hasStandardLibs() {
1081 // If building against a standard sdk then use the sdk version appropriate for the scope.
Paul Duffind1b3a922020-01-22 11:57:20 +00001082 return apiScope.sdkVersion
Paul Duffin12ceb462019-12-24 20:31:31 +00001083 } else {
1084 // Otherwise, use no system module.
1085 return "none"
1086 }
1087}
1088
Paul Duffind1b3a922020-01-22 11:57:20 +00001089func (module *SdkLibrary) latestApiFilegroupName(apiScope *apiScope) string {
1090 return ":" + module.BaseModuleName() + ".api." + apiScope.name + ".latest"
Jiyong Park58c518b2018-05-12 22:29:12 +09001091}
Jiyong Parkc678ad32018-04-10 13:07:10 +09001092
Paul Duffind1b3a922020-01-22 11:57:20 +00001093func (module *SdkLibrary) latestRemovedApiFilegroupName(apiScope *apiScope) string {
1094 return ":" + module.BaseModuleName() + "-removed.api." + apiScope.name + ".latest"
Jiyong Parkc678ad32018-04-10 13:07:10 +09001095}
1096
Anton Hansson944e77d2020-08-19 11:40:22 +01001097func childModuleVisibility(childVisibility []string) []string {
1098 if childVisibility == nil {
1099 // No child visibility set. The child will use the visibility of the sdk_library.
1100 return nil
1101 }
1102
1103 // Prepend an override to ignore the sdk_library's visibility, and rely on the child visibility.
1104 var visibility []string
1105 visibility = append(visibility, "//visibility:override")
1106 visibility = append(visibility, childVisibility...)
1107 return visibility
1108}
1109
Paul Duffin5df79302020-05-16 15:52:12 +01001110// Creates the implementation java library
1111func (module *SdkLibrary) createImplLibrary(mctx android.DefaultableHookContext) {
Paul Duffina2058f82020-06-24 16:22:38 +01001112 moduleNamePtr := proptools.StringPtr(module.BaseModuleName())
1113
Anton Hansson944e77d2020-08-19 11:40:22 +01001114 visibility := childModuleVisibility(module.sdkLibraryProperties.Impl_library_visibility)
1115
Paul Duffin5df79302020-05-16 15:52:12 +01001116 props := struct {
Paul Duffina2058f82020-06-24 16:22:38 +01001117 Name *string
1118 Visibility []string
1119 Instrument bool
1120 ConfigurationName *string
Paul Duffin5df79302020-05-16 15:52:12 +01001121 }{
1122 Name: proptools.StringPtr(module.implLibraryModuleName()),
Anton Hansson944e77d2020-08-19 11:40:22 +01001123 Visibility: visibility,
Paul Duffin296cf332020-06-18 21:09:55 +01001124 // Set the instrument property to ensure it is instrumented when instrumentation is required.
1125 Instrument: true,
Paul Duffina2058f82020-06-24 16:22:38 +01001126
1127 // Make the created library behave as if it had the same name as this module.
1128 ConfigurationName: moduleNamePtr,
Paul Duffin5df79302020-05-16 15:52:12 +01001129 }
1130
1131 properties := []interface{}{
1132 &module.properties,
1133 &module.protoProperties,
1134 &module.deviceProperties,
Liz Kammera7a64f32020-07-09 15:16:41 -07001135 &module.dexProperties,
Paul Duffin5df79302020-05-16 15:52:12 +01001136 &module.dexpreoptProperties,
Colin Cross014489c2020-06-02 20:09:13 -07001137 &module.linter.properties,
Paul Duffin5df79302020-05-16 15:52:12 +01001138 &props,
1139 module.sdkComponentPropertiesForChildLibrary(),
1140 }
1141 mctx.CreateModule(LibraryFactory, properties...)
1142}
1143
Jiyong Parkc678ad32018-04-10 13:07:10 +09001144// Creates a static java library that has API stubs
Paul Duffinf0229202020-04-29 16:47:28 +01001145func (module *SdkLibrary) createStubsLibrary(mctx android.DefaultableHookContext, apiScope *apiScope) {
Jiyong Parkc678ad32018-04-10 13:07:10 +09001146 props := struct {
Dan Willemsen9f435972020-05-28 15:28:00 -07001147 Name *string
1148 Visibility []string
1149 Srcs []string
1150 Installable *bool
1151 Sdk_version *string
1152 System_modules *string
1153 Patch_module *string
1154 Libs []string
1155 Compile_dex *bool
1156 Java_version *string
1157 Openjdk9 struct {
Sundong Ahn054b19a2018-10-19 13:46:09 +09001158 Srcs []string
1159 Javacflags []string
1160 }
Anton Hansson5fd5d242020-03-27 19:43:19 +00001161 Dist struct {
1162 Targets []string
1163 Dest *string
1164 Dir *string
1165 Tag *string
1166 }
Jiyong Parkc678ad32018-04-10 13:07:10 +09001167 }{}
1168
Paul Duffinc3091c82020-05-08 14:16:20 +01001169 props.Name = proptools.StringPtr(module.stubsLibraryModuleName(apiScope))
Anton Hansson944e77d2020-08-19 11:40:22 +01001170 props.Visibility = childModuleVisibility(module.sdkLibraryProperties.Stubs_library_visibility)
Jiyong Parkc678ad32018-04-10 13:07:10 +09001171 // sources are generated from the droiddoc
Paul Duffinc3091c82020-05-08 14:16:20 +01001172 props.Srcs = []string{":" + module.stubsSourceModuleName(apiScope)}
Paul Duffin12ceb462019-12-24 20:31:31 +00001173 sdkVersion := module.sdkVersionForStubsLibrary(mctx, apiScope)
Paul Duffin52d398a2019-06-11 12:31:14 +01001174 props.Sdk_version = proptools.StringPtr(sdkVersion)
Paul Duffina18abc22020-05-16 18:54:24 +01001175 props.System_modules = module.deviceProperties.System_modules
1176 props.Patch_module = module.properties.Patch_module
Paul Duffin367ab912019-12-23 19:40:36 +00001177 props.Installable = proptools.BoolPtr(false)
Sundong Ahn054b19a2018-10-19 13:46:09 +09001178 props.Libs = module.sdkLibraryProperties.Stub_only_libs
Paul Duffine22c2ab2020-05-20 19:35:27 +01001179 // The stub-annotations library contains special versions of the annotations
1180 // with CLASS retention policy, so that they're kept.
1181 if proptools.Bool(module.sdkLibraryProperties.Annotations_enabled) {
1182 props.Libs = append(props.Libs, "stub-annotations")
1183 }
Paul Duffina18abc22020-05-16 18:54:24 +01001184 props.Openjdk9.Srcs = module.properties.Openjdk9.Srcs
1185 props.Openjdk9.Javacflags = module.properties.Openjdk9.Javacflags
Anton Hansson83509b52020-05-21 09:21:57 +01001186 // We compile the stubs for 1.8 in line with the main android.jar stubs, and potential
1187 // interop with older developer tools that don't support 1.9.
1188 props.Java_version = proptools.StringPtr("1.8")
Liz Kammera7a64f32020-07-09 15:16:41 -07001189 if module.dexProperties.Compile_dex != nil {
1190 props.Compile_dex = module.dexProperties.Compile_dex
Sundong Ahndd567f92018-07-31 17:19:11 +09001191 }
Jiyong Parkc678ad32018-04-10 13:07:10 +09001192
Anton Hansson5fd5d242020-03-27 19:43:19 +00001193 // Dist the class jar artifact for sdk builds.
1194 if !Bool(module.sdkLibraryProperties.No_dist) {
1195 props.Dist.Targets = []string{"sdk", "win_sdk"}
1196 props.Dist.Dest = proptools.StringPtr(fmt.Sprintf("%v.jar", module.BaseModuleName()))
1197 props.Dist.Dir = proptools.StringPtr(module.apiDistPath(apiScope))
1198 props.Dist.Tag = proptools.StringPtr(".jar")
1199 }
Jiyong Parkc678ad32018-04-10 13:07:10 +09001200
Paul Duffin859fe962020-05-15 10:20:31 +01001201 mctx.CreateModule(LibraryFactory, &props, module.sdkComponentPropertiesForChildLibrary())
Jiyong Parkc678ad32018-04-10 13:07:10 +09001202}
1203
Paul Duffin6d0886e2020-04-07 18:49:53 +01001204// Creates a droidstubs module that creates stubs source files from the given full source
Paul Duffinc8782502020-04-29 20:45:27 +01001205// files and also updates and checks the API specification files.
Paul Duffin15f34ef2020-07-20 18:04:44 +01001206func (module *SdkLibrary) createStubsSourcesAndApi(mctx android.DefaultableHookContext, apiScope *apiScope, name string, scopeSpecificDroidstubsArgs []string) {
Jiyong Parkc678ad32018-04-10 13:07:10 +09001207 props := struct {
Sundong Ahn054b19a2018-10-19 13:46:09 +09001208 Name *string
Paul Duffin4911a892020-04-29 23:35:13 +01001209 Visibility []string
Sundong Ahn054b19a2018-10-19 13:46:09 +09001210 Srcs []string
1211 Installable *bool
Paul Duffin52d398a2019-06-11 12:31:14 +01001212 Sdk_version *string
Paul Duffin12ceb462019-12-24 20:31:31 +00001213 System_modules *string
Sundong Ahn054b19a2018-10-19 13:46:09 +09001214 Libs []string
Paul Duffin11512472019-02-11 15:55:17 +00001215 Arg_files []string
Sundong Ahn054b19a2018-10-19 13:46:09 +09001216 Args *string
Sundong Ahn054b19a2018-10-19 13:46:09 +09001217 Java_version *string
Paul Duffine22c2ab2020-05-20 19:35:27 +01001218 Annotations_enabled *bool
Sundong Ahn054b19a2018-10-19 13:46:09 +09001219 Merge_annotations_dirs []string
1220 Merge_inclusion_annotations_dirs []string
Paul Duffin0ff08bd2020-04-29 13:30:54 +01001221 Generate_stubs *bool
Sundong Ahn054b19a2018-10-19 13:46:09 +09001222 Check_api struct {
Inseob Kim38449af2019-02-28 14:24:05 +09001223 Current ApiToCheck
1224 Last_released ApiToCheck
1225 Ignore_missing_latest_api *bool
Paul Duffin160fe412020-05-10 19:32:20 +01001226
1227 Api_lint struct {
1228 Enabled *bool
1229 New_since *string
1230 Baseline_file *string
1231 }
Jiyong Park58c518b2018-05-12 22:29:12 +09001232 }
Sundong Ahn1b92c822018-05-29 11:35:17 +09001233 Aidl struct {
1234 Include_dirs []string
1235 Local_include_dirs []string
1236 }
Anton Hansson5fd5d242020-03-27 19:43:19 +00001237 Dist struct {
1238 Targets []string
1239 Dest *string
1240 Dir *string
1241 }
Jiyong Parkc678ad32018-04-10 13:07:10 +09001242 }{}
1243
Paul Duffin7b78b4d2020-04-28 14:08:32 +01001244 // The stubs source processing uses the same compile time classpath when extracting the
1245 // API from the implementation library as it does when compiling it. i.e. the same
1246 // * sdk version
1247 // * system_modules
1248 // * libs (static_libs/libs)
Paul Duffin250e6192019-06-07 10:44:37 +01001249
Paul Duffin0ff08bd2020-04-29 13:30:54 +01001250 props.Name = proptools.StringPtr(name)
Anton Hansson944e77d2020-08-19 11:40:22 +01001251 props.Visibility = childModuleVisibility(module.sdkLibraryProperties.Stubs_source_visibility)
Paul Duffina18abc22020-05-16 18:54:24 +01001252 props.Srcs = append(props.Srcs, module.properties.Srcs...)
1253 props.Sdk_version = module.deviceProperties.Sdk_version
1254 props.System_modules = module.deviceProperties.System_modules
Jiyong Parkc678ad32018-04-10 13:07:10 +09001255 props.Installable = proptools.BoolPtr(false)
Sundong Ahne6f0b052018-06-05 16:46:14 +09001256 // A droiddoc module has only one Libs property and doesn't distinguish between
1257 // shared libs and static libs. So we need to add both of these libs to Libs property.
Paul Duffina18abc22020-05-16 18:54:24 +01001258 props.Libs = module.properties.Libs
1259 props.Libs = append(props.Libs, module.properties.Static_libs...)
1260 props.Aidl.Include_dirs = module.deviceProperties.Aidl.Include_dirs
1261 props.Aidl.Local_include_dirs = module.deviceProperties.Aidl.Local_include_dirs
1262 props.Java_version = module.properties.Java_version
Jiyong Parkc678ad32018-04-10 13:07:10 +09001263
Paul Duffine22c2ab2020-05-20 19:35:27 +01001264 props.Annotations_enabled = module.sdkLibraryProperties.Annotations_enabled
Sundong Ahn054b19a2018-10-19 13:46:09 +09001265 props.Merge_annotations_dirs = module.sdkLibraryProperties.Merge_annotations_dirs
1266 props.Merge_inclusion_annotations_dirs = module.sdkLibraryProperties.Merge_inclusion_annotations_dirs
1267
Paul Duffin6d0886e2020-04-07 18:49:53 +01001268 droidstubsArgs := []string{}
Paul Duffin235ffff2019-12-24 10:41:30 +00001269 if len(module.sdkLibraryProperties.Api_packages) != 0 {
Paul Duffin6d0886e2020-04-07 18:49:53 +01001270 droidstubsArgs = append(droidstubsArgs, "--stub-packages "+strings.Join(module.sdkLibraryProperties.Api_packages, ":"))
Paul Duffin235ffff2019-12-24 10:41:30 +00001271 }
1272 if len(module.sdkLibraryProperties.Hidden_api_packages) != 0 {
Paul Duffin6d0886e2020-04-07 18:49:53 +01001273 droidstubsArgs = append(droidstubsArgs,
Paul Duffin235ffff2019-12-24 10:41:30 +00001274 android.JoinWithPrefix(module.sdkLibraryProperties.Hidden_api_packages, " --hide-package "))
1275 }
Paul Duffin6d0886e2020-04-07 18:49:53 +01001276 droidstubsArgs = append(droidstubsArgs, module.sdkLibraryProperties.Droiddoc_options...)
Paul Duffin235ffff2019-12-24 10:41:30 +00001277 disabledWarnings := []string{
1278 "MissingPermission",
1279 "BroadcastBehavior",
1280 "HiddenSuperclass",
1281 "DeprecationMismatch",
1282 "UnavailableSymbol",
1283 "SdkConstant",
1284 "HiddenTypeParameter",
1285 "Todo",
1286 "Typo",
1287 }
Paul Duffin6d0886e2020-04-07 18:49:53 +01001288 droidstubsArgs = append(droidstubsArgs, android.JoinWithPrefix(disabledWarnings, "--hide "))
Sundong Ahnfb2721f2018-09-17 13:23:09 +09001289
Paul Duffin1fb487d2020-04-07 18:50:10 +01001290 // Add in scope specific arguments.
Paul Duffin0ff08bd2020-04-29 13:30:54 +01001291 droidstubsArgs = append(droidstubsArgs, scopeSpecificDroidstubsArgs...)
Paul Duffin11512472019-02-11 15:55:17 +00001292 props.Arg_files = module.sdkLibraryProperties.Droiddoc_option_files
Paul Duffin6d0886e2020-04-07 18:49:53 +01001293 props.Args = proptools.StringPtr(strings.Join(droidstubsArgs, " "))
Jiyong Parkc678ad32018-04-10 13:07:10 +09001294
Paul Duffin15f34ef2020-07-20 18:04:44 +01001295 // List of APIs identified from the provided source files are created. They are later
1296 // compared against to the not-yet-released (a.k.a current) list of APIs and to the
1297 // last-released (a.k.a numbered) list of API.
1298 currentApiFileName := apiScope.apiFilePrefix + "current.txt"
1299 removedApiFileName := apiScope.apiFilePrefix + "removed.txt"
1300 apiDir := module.getApiDir()
1301 currentApiFileName = path.Join(apiDir, currentApiFileName)
1302 removedApiFileName = path.Join(apiDir, removedApiFileName)
Jiyong Parkc678ad32018-04-10 13:07:10 +09001303
Paul Duffin15f34ef2020-07-20 18:04:44 +01001304 // check against the not-yet-release API
1305 props.Check_api.Current.Api_file = proptools.StringPtr(currentApiFileName)
1306 props.Check_api.Current.Removed_api_file = proptools.StringPtr(removedApiFileName)
Jiyong Park58c518b2018-05-12 22:29:12 +09001307
Paul Duffin15f34ef2020-07-20 18:04:44 +01001308 if !apiScope.unstable {
1309 // check against the latest released API
1310 latestApiFilegroupName := proptools.StringPtr(module.latestApiFilegroupName(apiScope))
1311 props.Check_api.Last_released.Api_file = latestApiFilegroupName
1312 props.Check_api.Last_released.Removed_api_file = proptools.StringPtr(
1313 module.latestRemovedApiFilegroupName(apiScope))
1314 props.Check_api.Ignore_missing_latest_api = proptools.BoolPtr(true)
Paul Duffin160fe412020-05-10 19:32:20 +01001315
Paul Duffin15f34ef2020-07-20 18:04:44 +01001316 if proptools.Bool(module.sdkLibraryProperties.Api_lint.Enabled) {
1317 // Enable api lint.
1318 props.Check_api.Api_lint.Enabled = proptools.BoolPtr(true)
1319 props.Check_api.Api_lint.New_since = latestApiFilegroupName
Paul Duffin160fe412020-05-10 19:32:20 +01001320
Paul Duffin15f34ef2020-07-20 18:04:44 +01001321 // If it exists then pass a lint-baseline.txt through to droidstubs.
1322 baselinePath := path.Join(apiDir, apiScope.apiFilePrefix+"lint-baseline.txt")
1323 baselinePathRelativeToRoot := path.Join(mctx.ModuleDir(), baselinePath)
1324 paths, err := mctx.GlobWithDeps(baselinePathRelativeToRoot, nil)
1325 if err != nil {
1326 mctx.ModuleErrorf("error checking for presence of %s: %s", baselinePathRelativeToRoot, err)
1327 }
1328 if len(paths) == 1 {
1329 props.Check_api.Api_lint.Baseline_file = proptools.StringPtr(baselinePath)
1330 } else if len(paths) != 0 {
1331 mctx.ModuleErrorf("error checking for presence of %s: expected one path, found: %v", baselinePathRelativeToRoot, paths)
Paul Duffin160fe412020-05-10 19:32:20 +01001332 }
1333 }
Paul Duffin15f34ef2020-07-20 18:04:44 +01001334 }
Jiyong Park58c518b2018-05-12 22:29:12 +09001335
Paul Duffin15f34ef2020-07-20 18:04:44 +01001336 // Dist the api txt artifact for sdk builds.
1337 if !Bool(module.sdkLibraryProperties.No_dist) {
1338 props.Dist.Targets = []string{"sdk", "win_sdk"}
1339 props.Dist.Dest = proptools.StringPtr(fmt.Sprintf("%v.txt", module.BaseModuleName()))
1340 props.Dist.Dir = proptools.StringPtr(path.Join(module.apiDistPath(apiScope), "api"))
Anton Hansson5fd5d242020-03-27 19:43:19 +00001341 }
1342
Colin Cross84dfc3d2019-09-25 11:33:01 -07001343 mctx.CreateModule(DroidstubsFactory, &props)
Jiyong Parkc678ad32018-04-10 13:07:10 +09001344}
1345
Jooyung Han5e9013b2020-03-10 06:23:13 +09001346func (module *SdkLibrary) DepIsInSameApex(mctx android.BaseModuleContext, dep android.Module) bool {
1347 depTag := mctx.OtherModuleDependencyTag(dep)
1348 if depTag == xmlPermissionsFileTag {
1349 return true
1350 }
1351 return module.Library.DepIsInSameApex(mctx, dep)
1352}
1353
Jiyong Parkc678ad32018-04-10 13:07:10 +09001354// Creates the xml file that publicizes the runtime library
Paul Duffinf0229202020-04-29 16:47:28 +01001355func (module *SdkLibrary) createXmlFile(mctx android.DefaultableHookContext) {
Jiyong Parke3833882020-02-17 17:28:10 +09001356 props := struct {
Paul Duffin1dbe3ca2020-05-16 09:57:59 +01001357 Name *string
1358 Lib_name *string
1359 Apex_available []string
Jiyong Parke3833882020-02-17 17:28:10 +09001360 }{
Paul Duffineedc5d52020-06-12 17:46:39 +01001361 Name: proptools.StringPtr(module.xmlPermissionsModuleName()),
Jooyung Han5e9013b2020-03-10 06:23:13 +09001362 Lib_name: proptools.StringPtr(module.BaseModuleName()),
1363 Apex_available: module.ApexProperties.Apex_available,
Jiyong Parkc678ad32018-04-10 13:07:10 +09001364 }
Jiyong Parke3833882020-02-17 17:28:10 +09001365
Jiyong Parke3833882020-02-17 17:28:10 +09001366 mctx.CreateModule(sdkLibraryXmlFactory, &props)
Jiyong Parkc678ad32018-04-10 13:07:10 +09001367}
1368
Paul Duffin50061512020-01-21 16:31:05 +00001369func PrebuiltJars(ctx android.BaseModuleContext, baseName string, s sdkSpec) android.Paths {
Jiyong Park6a927c42020-01-21 02:03:43 +09001370 var ver sdkVersion
1371 var kind sdkKind
1372 if s.usePrebuilt(ctx) {
1373 ver = s.version
1374 kind = s.kind
Jiyong Parkc678ad32018-04-10 13:07:10 +09001375 } else {
Jiyong Park6a927c42020-01-21 02:03:43 +09001376 // We don't have prebuilt SDK for the specific sdkVersion.
1377 // Instead of breaking the build, fallback to use "system_current"
1378 ver = sdkVersionCurrent
1379 kind = sdkSystem
Sundong Ahn054b19a2018-10-19 13:46:09 +09001380 }
Jiyong Park6a927c42020-01-21 02:03:43 +09001381
1382 dir := filepath.Join("prebuilts", "sdk", ver.String(), kind.String())
Paul Duffin50061512020-01-21 16:31:05 +00001383 jar := filepath.Join(dir, baseName+".jar")
Sundong Ahn054b19a2018-10-19 13:46:09 +09001384 jarPath := android.ExistentPathForSource(ctx, jar)
Sundong Ahnae418ac2019-02-28 15:01:28 +09001385 if !jarPath.Valid() {
Colin Cross07c88562020-01-07 09:34:44 -08001386 if ctx.Config().AllowMissingDependencies() {
1387 return android.Paths{android.PathForSource(ctx, jar)}
1388 } else {
Jiyong Park6a927c42020-01-21 02:03:43 +09001389 ctx.PropertyErrorf("sdk_library", "invalid sdk version %q, %q does not exist", s.raw, jar)
Colin Cross07c88562020-01-07 09:34:44 -08001390 }
Sundong Ahnae418ac2019-02-28 15:01:28 +09001391 return nil
1392 }
Sundong Ahn054b19a2018-10-19 13:46:09 +09001393 return android.Paths{jarPath.Path()}
1394}
1395
Colin Crossaede88c2020-08-11 12:17:01 -07001396// Get the apex names for module, nil if it is for platform.
1397func getApexNamesForModule(module android.Module) []string {
Paul Duffin9b879592020-05-26 13:21:35 +01001398 if apex, ok := module.(android.ApexModule); ok {
Colin Crossaede88c2020-08-11 12:17:01 -07001399 return apex.InApexes()
Paul Duffin9b879592020-05-26 13:21:35 +01001400 }
1401
Colin Crossaede88c2020-08-11 12:17:01 -07001402 return nil
Paul Duffin9b879592020-05-26 13:21:35 +01001403}
1404
Colin Crossaede88c2020-08-11 12:17:01 -07001405// 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 +01001406//
1407// If either this or the other module are on the platform then this will return
1408// false.
Colin Crossaede88c2020-08-11 12:17:01 -07001409func withinSameApexesAs(module android.ApexModule, other android.Module) bool {
1410 names := module.InApexes()
1411 return len(names) > 0 && reflect.DeepEqual(names, getApexNamesForModule(other))
Paul Duffin9b879592020-05-26 13:21:35 +01001412}
1413
Paul Duffinb05d4292020-05-20 12:19:10 +01001414func (module *SdkLibrary) sdkJars(ctx android.BaseModuleContext, sdkVersion sdkSpec, headerJars bool) android.Paths {
Jiyong Park932cdfe2020-05-28 00:19:53 +09001415 // If the client doesn't set sdk_version, but if this library prefers stubs over
1416 // the impl library, let's provide the widest API surface possible. To do so,
1417 // force override sdk_version to module_current so that the closest possible API
1418 // surface could be found in selectHeaderJarsForSdkVersion
1419 if module.defaultsToStubs() && !sdkVersion.specified() {
1420 sdkVersion = sdkSpecFrom("module_current")
1421 }
Paul Duffind1b3a922020-01-22 11:57:20 +00001422
Paul Duffindaaa3322020-05-26 18:13:57 +01001423 // Only provide access to the implementation library if it is actually built.
1424 if module.requiresRuntimeImplementationLibrary() {
1425 // Check any special cases for java_sdk_library.
1426 //
1427 // Only allow access to the implementation library in the following condition:
1428 // * No sdk_version specified on the referencing module.
Paul Duffin9b879592020-05-26 13:21:35 +01001429 // * The referencing module is in the same apex as this.
Colin Crossaede88c2020-08-11 12:17:01 -07001430 if sdkVersion.kind == sdkPrivate || withinSameApexesAs(module, ctx.Module()) {
Paul Duffindaaa3322020-05-26 18:13:57 +01001431 if headerJars {
1432 return module.HeaderJars()
1433 } else {
1434 return module.ImplementationJars()
1435 }
Sundong Ahn054b19a2018-10-19 13:46:09 +09001436 }
Jiyong Parkc678ad32018-04-10 13:07:10 +09001437 }
Paul Duffinb05d4292020-05-20 12:19:10 +01001438
Paul Duffin23970f42020-05-20 14:20:02 +01001439 return module.selectHeaderJarsForSdkVersion(ctx, sdkVersion)
Jiyong Parkc678ad32018-04-10 13:07:10 +09001440}
1441
Sundong Ahn241cd372018-07-13 16:16:44 +09001442// to satisfy SdkLibraryDependency interface
Paul Duffind1b3a922020-01-22 11:57:20 +00001443func (module *SdkLibrary) SdkHeaderJars(ctx android.BaseModuleContext, sdkVersion sdkSpec) android.Paths {
1444 return module.sdkJars(ctx, sdkVersion, true /*headerJars*/)
1445}
1446
1447// to satisfy SdkLibraryDependency interface
Jiyong Park6a927c42020-01-21 02:03:43 +09001448func (module *SdkLibrary) SdkImplementationJars(ctx android.BaseModuleContext, sdkVersion sdkSpec) android.Paths {
Paul Duffind1b3a922020-01-22 11:57:20 +00001449 return module.sdkJars(ctx, sdkVersion, false /*headerJars*/)
Sundong Ahn241cd372018-07-13 16:16:44 +09001450}
1451
Sundong Ahn80a87b32019-05-13 15:02:50 +09001452func (module *SdkLibrary) SetNoDist() {
1453 module.sdkLibraryProperties.No_dist = proptools.BoolPtr(true)
1454}
1455
Colin Cross571cccf2019-02-04 11:22:08 -08001456var javaSdkLibrariesKey = android.NewOnceKey("javaSdkLibraries")
1457
Jiyong Park82484c02018-04-23 21:41:26 +09001458func javaSdkLibraries(config android.Config) *[]string {
Colin Cross571cccf2019-02-04 11:22:08 -08001459 return config.Once(javaSdkLibrariesKey, func() interface{} {
Jiyong Park82484c02018-04-23 21:41:26 +09001460 return &[]string{}
1461 }).(*[]string)
1462}
1463
Paul Duffin749f98f2019-12-30 17:23:46 +00001464func (module *SdkLibrary) getApiDir() string {
1465 return proptools.StringDefault(module.sdkLibraryProperties.Api_dir, "api")
1466}
1467
Jiyong Parkc678ad32018-04-10 13:07:10 +09001468// For a java_sdk_library module, create internal modules for stubs, docs,
1469// runtime libs and xml file. If requested, the stubs and docs are created twice
1470// once for public API level and once for system API level
Paul Duffinf0229202020-04-29 16:47:28 +01001471func (module *SdkLibrary) CreateInternalModules(mctx android.DefaultableHookContext) {
1472 // If the module has been disabled then don't create any child modules.
1473 if !module.Enabled() {
1474 return
1475 }
1476
Paul Duffina18abc22020-05-16 18:54:24 +01001477 if len(module.properties.Srcs) == 0 {
Inseob Kimc0907f12019-02-08 21:00:45 +09001478 mctx.PropertyErrorf("srcs", "java_sdk_library must specify srcs")
Jooyung Han58f26ab2019-12-18 15:34:32 +09001479 return
Inseob Kimc0907f12019-02-08 21:00:45 +09001480 }
1481
Paul Duffin37e0b772019-12-30 17:20:10 +00001482 // If this builds against standard libraries (i.e. is not part of the core libraries)
1483 // then assume it provides both system and test apis. Otherwise, assume it does not and
1484 // also assume it does not contribute to the dist build.
1485 sdkDep := decodeSdkDep(mctx, sdkContext(&module.Library))
1486 hasSystemAndTestApis := sdkDep.hasStandardLibs()
Paul Duffin3375e352020-04-28 10:44:03 +01001487 module.sdkLibraryProperties.Generate_system_and_test_apis = hasSystemAndTestApis
Paul Duffin37e0b772019-12-30 17:20:10 +00001488 module.sdkLibraryProperties.No_dist = proptools.BoolPtr(!hasSystemAndTestApis)
1489
Inseob Kim8098faa2019-03-18 10:19:51 +09001490 missing_current_api := false
1491
Paul Duffin3375e352020-04-28 10:44:03 +01001492 generatedScopes := module.getGeneratedApiScopes(mctx)
Paul Duffind1b3a922020-01-22 11:57:20 +00001493
Paul Duffin749f98f2019-12-30 17:23:46 +00001494 apiDir := module.getApiDir()
Paul Duffin3375e352020-04-28 10:44:03 +01001495 for _, scope := range generatedScopes {
Inseob Kim8098faa2019-03-18 10:19:51 +09001496 for _, api := range []string{"current.txt", "removed.txt"} {
Paul Duffind1b3a922020-01-22 11:57:20 +00001497 path := path.Join(mctx.ModuleDir(), apiDir, scope.apiFilePrefix+api)
Inseob Kim8098faa2019-03-18 10:19:51 +09001498 p := android.ExistentPathForSource(mctx, path)
1499 if !p.Valid() {
1500 mctx.ModuleErrorf("Current api file %#v doesn't exist", path)
1501 missing_current_api = true
1502 }
1503 }
1504 }
1505
1506 if missing_current_api {
1507 script := "build/soong/scripts/gen-java-current-api-files.sh"
1508 p := android.ExistentPathForSource(mctx, script)
1509
1510 if !p.Valid() {
1511 panic(fmt.Sprintf("script file %s doesn't exist", script))
1512 }
1513
1514 mctx.ModuleErrorf("One or more current api files are missing. "+
1515 "You can update them by:\n"+
Paul Duffin37e0b772019-12-30 17:20:10 +00001516 "%s %q %s && m update-api",
Paul Duffind1b3a922020-01-22 11:57:20 +00001517 script, filepath.Join(mctx.ModuleDir(), apiDir),
Paul Duffin3375e352020-04-28 10:44:03 +01001518 strings.Join(generatedScopes.Strings(func(s *apiScope) string { return s.apiFilePrefix }), " "))
Inseob Kim8098faa2019-03-18 10:19:51 +09001519 return
1520 }
1521
Paul Duffin3375e352020-04-28 10:44:03 +01001522 for _, scope := range generatedScopes {
Paul Duffin15f34ef2020-07-20 18:04:44 +01001523 // Use the stubs source name for legacy reasons.
1524 module.createStubsSourcesAndApi(mctx, scope, module.stubsSourceModuleName(scope), scope.droidstubsArgs)
Paul Duffin0ff08bd2020-04-29 13:30:54 +01001525
Paul Duffind1b3a922020-01-22 11:57:20 +00001526 module.createStubsLibrary(mctx, scope)
Inseob Kimc0907f12019-02-08 21:00:45 +09001527 }
1528
Paul Duffindfa131e2020-05-15 20:37:11 +01001529 if module.requiresRuntimeImplementationLibrary() {
Paul Duffin5df79302020-05-16 15:52:12 +01001530 // Create child module to create an implementation library.
1531 //
1532 // This temporarily creates a second implementation library that can be explicitly
1533 // referenced.
1534 //
1535 // TODO(b/156618935) - update comment once only one implementation library is created.
1536 module.createImplLibrary(mctx)
1537
Paul Duffindfa131e2020-05-15 20:37:11 +01001538 // Only create an XML permissions file that declares the library as being usable
1539 // as a shared library if required.
1540 if module.sharedLibrary() {
1541 module.createXmlFile(mctx)
1542 }
Paul Duffin43db9be2019-12-30 17:35:49 +00001543
1544 // record java_sdk_library modules so that they are exported to make
1545 javaSdkLibraries := javaSdkLibraries(mctx.Config())
1546 javaSdkLibrariesLock.Lock()
1547 defer javaSdkLibrariesLock.Unlock()
1548 *javaSdkLibraries = append(*javaSdkLibraries, module.BaseModuleName())
1549 }
Inseob Kimc0907f12019-02-08 21:00:45 +09001550}
1551
1552func (module *SdkLibrary) InitSdkLibraryProperties() {
Colin Crossce6734e2020-06-15 16:09:53 -07001553 module.addHostAndDeviceProperties()
1554 module.AddProperties(&module.sdkLibraryProperties)
Sundong Ahn054b19a2018-10-19 13:46:09 +09001555
Paul Duffin859fe962020-05-15 10:20:31 +01001556 module.initSdkLibraryComponent(&module.ModuleBase)
1557
Paul Duffina18abc22020-05-16 18:54:24 +01001558 module.properties.Installable = proptools.BoolPtr(true)
1559 module.deviceProperties.IsSDKLibrary = true
Inseob Kimc0907f12019-02-08 21:00:45 +09001560}
Sundong Ahn054b19a2018-10-19 13:46:09 +09001561
Paul Duffindfa131e2020-05-15 20:37:11 +01001562func (module *SdkLibrary) requiresRuntimeImplementationLibrary() bool {
1563 return !proptools.Bool(module.sdkLibraryProperties.Api_only)
1564}
1565
Jiyong Park932cdfe2020-05-28 00:19:53 +09001566func (module *SdkLibrary) defaultsToStubs() bool {
1567 return proptools.Bool(module.sdkLibraryProperties.Default_to_stubs)
1568}
1569
Paul Duffin1b1e8062020-05-08 13:44:43 +01001570// Defines how to name the individual component modules the sdk library creates.
1571type sdkLibraryComponentNamingScheme interface {
1572 stubsLibraryModuleName(scope *apiScope, baseName string) string
1573
1574 stubsSourceModuleName(scope *apiScope, baseName string) string
1575
1576 apiModuleName(scope *apiScope, baseName string) string
1577}
1578
1579type defaultNamingScheme struct {
1580}
1581
1582func (s *defaultNamingScheme) stubsLibraryModuleName(scope *apiScope, baseName string) string {
1583 return scope.stubsLibraryModuleName(baseName)
1584}
1585
1586func (s *defaultNamingScheme) stubsSourceModuleName(scope *apiScope, baseName string) string {
1587 return scope.stubsSourceModuleName(baseName)
1588}
1589
1590func (s *defaultNamingScheme) apiModuleName(scope *apiScope, baseName string) string {
1591 return scope.apiModuleName(baseName)
1592}
1593
1594var _ sdkLibraryComponentNamingScheme = (*defaultNamingScheme)(nil)
1595
Anton Hansson2d0c1942020-05-25 12:20:51 +01001596func moduleStubLinkType(name string) (stub bool, ret linkType) {
1597 // This suffix-based approach is fragile and could potentially mis-trigger.
1598 // TODO(b/155164730): Clean this up when modules no longer reference sdk_lib stubs directly.
1599 if strings.HasSuffix(name, ".stubs.public") || strings.HasSuffix(name, "-stubs-publicapi") {
1600 return true, javaSdk
1601 }
1602 if strings.HasSuffix(name, ".stubs.system") || strings.HasSuffix(name, "-stubs-systemapi") {
1603 return true, javaSystem
1604 }
1605 if strings.HasSuffix(name, ".stubs.module_lib") || strings.HasSuffix(name, "-stubs-module_libs_api") {
1606 return true, javaModule
1607 }
1608 if strings.HasSuffix(name, ".stubs.test") {
1609 return true, javaSystem
1610 }
1611 return false, javaPlatform
1612}
1613
Jaewoong Jung4f158ee2019-07-11 10:05:35 -07001614// java_sdk_library is a special Java library that provides optional platform APIs to apps.
1615// In practice, it can be viewed as a combination of several modules: 1) stubs library that clients
1616// are linked against to, 2) droiddoc module that internally generates API stubs source files,
1617// 3) the real runtime shared library that implements the APIs, and 4) XML file for adding
1618// the runtime lib to the classpath at runtime if requested via <uses-library>.
Inseob Kimc0907f12019-02-08 21:00:45 +09001619func SdkLibraryFactory() android.Module {
1620 module := &SdkLibrary{}
Paul Duffinc3091c82020-05-08 14:16:20 +01001621
1622 // Initialize information common between source and prebuilt.
1623 module.initCommon(&module.ModuleBase)
1624
Inseob Kimc0907f12019-02-08 21:00:45 +09001625 module.InitSdkLibraryProperties()
Jooyung Han58f26ab2019-12-18 15:34:32 +09001626 android.InitApexModule(module)
Sundong Ahn054b19a2018-10-19 13:46:09 +09001627 InitJavaModule(module, android.HostAndDeviceSupported)
Paul Duffin3375e352020-04-28 10:44:03 +01001628
1629 // Initialize the map from scope to scope specific properties.
1630 scopeToProperties := make(map[*apiScope]*ApiScopeProperties)
1631 for _, scope := range allApiScopes {
1632 scopeToProperties[scope] = scope.scopeSpecificProperties(module)
1633 }
1634 module.scopeToProperties = scopeToProperties
1635
Paul Duffin4911a892020-04-29 23:35:13 +01001636 // Add the properties containing visibility rules so that they are checked.
Paul Duffin5df79302020-05-16 15:52:12 +01001637 android.AddVisibilityProperty(module, "impl_library_visibility", &module.sdkLibraryProperties.Impl_library_visibility)
Paul Duffin4911a892020-04-29 23:35:13 +01001638 android.AddVisibilityProperty(module, "stubs_library_visibility", &module.sdkLibraryProperties.Stubs_library_visibility)
1639 android.AddVisibilityProperty(module, "stubs_source_visibility", &module.sdkLibraryProperties.Stubs_source_visibility)
1640
Paul Duffin1b1e8062020-05-08 13:44:43 +01001641 module.SetDefaultableHook(func(ctx android.DefaultableHookContext) {
Paul Duffindfa131e2020-05-15 20:37:11 +01001642 // If no implementation is required then it cannot be used as a shared library
1643 // either.
1644 if !module.requiresRuntimeImplementationLibrary() {
1645 // If shared_library has been explicitly set to true then it is incompatible
1646 // with api_only: true.
1647 if proptools.Bool(module.commonSdkLibraryProperties.Shared_library) {
1648 ctx.PropertyErrorf("api_only/shared_library", "inconsistent settings, shared_library and api_only cannot both be true")
1649 }
1650 // Set shared_library: false.
1651 module.commonSdkLibraryProperties.Shared_library = proptools.BoolPtr(false)
1652 }
1653
Paul Duffin1b1e8062020-05-08 13:44:43 +01001654 if module.initCommonAfterDefaultsApplied(ctx) {
1655 module.CreateInternalModules(ctx)
1656 }
1657 })
Jiyong Parkc678ad32018-04-10 13:07:10 +09001658 return module
1659}
Colin Cross79c7c262019-04-17 11:11:46 -07001660
1661//
1662// SDK library prebuilts
1663//
1664
Paul Duffin56d44902020-01-31 13:36:25 +00001665// Properties associated with each api scope.
1666type sdkLibraryScopeProperties struct {
Colin Cross79c7c262019-04-17 11:11:46 -07001667 Jars []string `android:"path"`
1668
1669 Sdk_version *string
1670
Colin Cross79c7c262019-04-17 11:11:46 -07001671 // List of shared java libs that this module has dependencies to
1672 Libs []string
Paul Duffin3d1248c2020-04-09 00:10:17 +01001673
Paul Duffinc8782502020-04-29 20:45:27 +01001674 // The stubs source.
Paul Duffin3d1248c2020-04-09 00:10:17 +01001675 Stub_srcs []string `android:"path"`
Paul Duffin1fd005d2020-04-09 01:08:11 +01001676
1677 // The current.txt
Paul Duffin0f8faff2020-05-20 16:18:00 +01001678 Current_api *string `android:"path"`
Paul Duffin1fd005d2020-04-09 01:08:11 +01001679
1680 // The removed.txt
Paul Duffin0f8faff2020-05-20 16:18:00 +01001681 Removed_api *string `android:"path"`
Colin Cross79c7c262019-04-17 11:11:46 -07001682}
1683
Paul Duffin56d44902020-01-31 13:36:25 +00001684type sdkLibraryImportProperties struct {
Paul Duffinfcfd7912020-01-31 17:54:30 +00001685 // List of shared java libs, common to all scopes, that this module has
1686 // dependencies to
1687 Libs []string
Paul Duffin56d44902020-01-31 13:36:25 +00001688}
1689
Paul Duffineedc5d52020-06-12 17:46:39 +01001690type SdkLibraryImport struct {
Colin Cross79c7c262019-04-17 11:11:46 -07001691 android.ModuleBase
1692 android.DefaultableModuleBase
1693 prebuilt android.Prebuilt
Paul Duffindd46f712020-02-10 13:37:10 +00001694 android.ApexModuleBase
1695 android.SdkBase
Colin Cross79c7c262019-04-17 11:11:46 -07001696
1697 properties sdkLibraryImportProperties
1698
Paul Duffin46a26a82020-04-07 19:27:04 +01001699 // Map from api scope to the scope specific property structure.
1700 scopeProperties map[*apiScope]*sdkLibraryScopeProperties
1701
Paul Duffin56d44902020-01-31 13:36:25 +00001702 commonToSdkLibraryAndImport
Paul Duffineedc5d52020-06-12 17:46:39 +01001703
1704 // The reference to the implementation library created by the source module.
1705 // Is nil if the source module does not exist.
1706 implLibraryModule *Library
1707
1708 // The reference to the xml permissions module created by the source module.
1709 // Is nil if the source module does not exist.
1710 xmlPermissionsFileModule *sdkLibraryXml
Colin Cross79c7c262019-04-17 11:11:46 -07001711}
1712
Paul Duffineedc5d52020-06-12 17:46:39 +01001713var _ SdkLibraryDependency = (*SdkLibraryImport)(nil)
Colin Cross79c7c262019-04-17 11:11:46 -07001714
Paul Duffin46a26a82020-04-07 19:27:04 +01001715// The type of a structure that contains a field of type sdkLibraryScopeProperties
1716// for each apiscope in allApiScopes, e.g. something like:
1717// struct {
1718// Public sdkLibraryScopeProperties
1719// System sdkLibraryScopeProperties
1720// ...
1721// }
1722var allScopeStructType = createAllScopePropertiesStructType()
1723
1724// Dynamically create a structure type for each apiscope in allApiScopes.
1725func createAllScopePropertiesStructType() reflect.Type {
1726 var fields []reflect.StructField
1727 for _, apiScope := range allApiScopes {
1728 field := reflect.StructField{
1729 Name: apiScope.fieldName,
1730 Type: reflect.TypeOf(sdkLibraryScopeProperties{}),
1731 }
1732 fields = append(fields, field)
1733 }
1734
1735 return reflect.StructOf(fields)
1736}
1737
1738// Create an instance of the scope specific structure type and return a map
1739// from apiscope to a pointer to each scope specific field.
1740func createPropertiesInstance() (interface{}, map[*apiScope]*sdkLibraryScopeProperties) {
1741 allScopePropertiesPtr := reflect.New(allScopeStructType)
1742 allScopePropertiesStruct := allScopePropertiesPtr.Elem()
1743 scopeProperties := make(map[*apiScope]*sdkLibraryScopeProperties)
1744
1745 for _, apiScope := range allApiScopes {
1746 field := allScopePropertiesStruct.FieldByName(apiScope.fieldName)
1747 scopeProperties[apiScope] = field.Addr().Interface().(*sdkLibraryScopeProperties)
1748 }
1749
1750 return allScopePropertiesPtr.Interface(), scopeProperties
1751}
1752
Jaewoong Jung4f158ee2019-07-11 10:05:35 -07001753// java_sdk_library_import imports a prebuilt java_sdk_library.
Colin Cross79c7c262019-04-17 11:11:46 -07001754func sdkLibraryImportFactory() android.Module {
Paul Duffineedc5d52020-06-12 17:46:39 +01001755 module := &SdkLibraryImport{}
Colin Cross79c7c262019-04-17 11:11:46 -07001756
Paul Duffin46a26a82020-04-07 19:27:04 +01001757 allScopeProperties, scopeToProperties := createPropertiesInstance()
1758 module.scopeProperties = scopeToProperties
1759 module.AddProperties(&module.properties, allScopeProperties)
Colin Cross79c7c262019-04-17 11:11:46 -07001760
Paul Duffinc3091c82020-05-08 14:16:20 +01001761 // Initialize information common between source and prebuilt.
1762 module.initCommon(&module.ModuleBase)
1763
Paul Duffin0bdcb272020-02-06 15:24:57 +00001764 android.InitPrebuiltModule(module, &[]string{""})
Paul Duffindd46f712020-02-10 13:37:10 +00001765 android.InitApexModule(module)
1766 android.InitSdkAwareModule(module)
Colin Cross79c7c262019-04-17 11:11:46 -07001767 InitJavaModule(module, android.HostAndDeviceSupported)
1768
Paul Duffin1b1e8062020-05-08 13:44:43 +01001769 module.SetDefaultableHook(func(mctx android.DefaultableHookContext) {
1770 if module.initCommonAfterDefaultsApplied(mctx) {
1771 module.createInternalModules(mctx)
1772 }
1773 })
Colin Cross79c7c262019-04-17 11:11:46 -07001774 return module
1775}
1776
Paul Duffineedc5d52020-06-12 17:46:39 +01001777func (module *SdkLibraryImport) Prebuilt() *android.Prebuilt {
Colin Cross79c7c262019-04-17 11:11:46 -07001778 return &module.prebuilt
1779}
1780
Paul Duffineedc5d52020-06-12 17:46:39 +01001781func (module *SdkLibraryImport) Name() string {
Colin Cross79c7c262019-04-17 11:11:46 -07001782 return module.prebuilt.Name(module.ModuleBase.Name())
1783}
1784
Paul Duffineedc5d52020-06-12 17:46:39 +01001785func (module *SdkLibraryImport) createInternalModules(mctx android.DefaultableHookContext) {
Colin Cross79c7c262019-04-17 11:11:46 -07001786
Paul Duffin50061512020-01-21 16:31:05 +00001787 // If the build is configured to use prebuilts then force this to be preferred.
Jeongik Cha816a23a2020-07-08 01:09:23 +09001788 if mctx.Config().AlwaysUsePrebuiltSdks() {
Paul Duffin50061512020-01-21 16:31:05 +00001789 module.prebuilt.ForcePrefer()
1790 }
1791
Paul Duffin46a26a82020-04-07 19:27:04 +01001792 for apiScope, scopeProperties := range module.scopeProperties {
Paul Duffin56d44902020-01-31 13:36:25 +00001793 if len(scopeProperties.Jars) == 0 {
1794 continue
1795 }
1796
Paul Duffinbbb546b2020-04-09 00:07:11 +01001797 module.createJavaImportForStubs(mctx, apiScope, scopeProperties)
Paul Duffin3d1248c2020-04-09 00:10:17 +01001798
Paul Duffin0f8faff2020-05-20 16:18:00 +01001799 if len(scopeProperties.Stub_srcs) > 0 {
1800 module.createPrebuiltStubsSources(mctx, apiScope, scopeProperties)
1801 }
Paul Duffin56d44902020-01-31 13:36:25 +00001802 }
Colin Cross79c7c262019-04-17 11:11:46 -07001803
1804 javaSdkLibraries := javaSdkLibraries(mctx.Config())
1805 javaSdkLibrariesLock.Lock()
1806 defer javaSdkLibrariesLock.Unlock()
1807 *javaSdkLibraries = append(*javaSdkLibraries, module.BaseModuleName())
1808}
1809
Paul Duffineedc5d52020-06-12 17:46:39 +01001810func (module *SdkLibraryImport) createJavaImportForStubs(mctx android.DefaultableHookContext, apiScope *apiScope, scopeProperties *sdkLibraryScopeProperties) {
Paul Duffinbbb546b2020-04-09 00:07:11 +01001811 // Creates a java import for the jar with ".stubs" suffix
1812 props := struct {
Paul Duffin1dbe3ca2020-05-16 09:57:59 +01001813 Name *string
1814 Sdk_version *string
1815 Libs []string
1816 Jars []string
1817 Prefer *bool
Paul Duffinbbb546b2020-04-09 00:07:11 +01001818 }{}
Paul Duffinc3091c82020-05-08 14:16:20 +01001819 props.Name = proptools.StringPtr(module.stubsLibraryModuleName(apiScope))
Paul Duffinbbb546b2020-04-09 00:07:11 +01001820 props.Sdk_version = scopeProperties.Sdk_version
1821 // Prepend any of the libs from the legacy public properties to the libs for each of the
1822 // scopes to avoid having to duplicate them in each scope.
1823 props.Libs = append(module.properties.Libs, scopeProperties.Libs...)
1824 props.Jars = scopeProperties.Jars
Paul Duffin1dbe3ca2020-05-16 09:57:59 +01001825
Paul Duffin38b57852020-05-13 16:08:09 +01001826 // The imports are preferred if the java_sdk_library_import is preferred.
1827 props.Prefer = proptools.BoolPtr(module.prebuilt.Prefer())
Paul Duffin859fe962020-05-15 10:20:31 +01001828
1829 mctx.CreateModule(ImportFactory, &props, module.sdkComponentPropertiesForChildLibrary())
Paul Duffinbbb546b2020-04-09 00:07:11 +01001830}
1831
Paul Duffineedc5d52020-06-12 17:46:39 +01001832func (module *SdkLibraryImport) createPrebuiltStubsSources(mctx android.DefaultableHookContext, apiScope *apiScope, scopeProperties *sdkLibraryScopeProperties) {
Paul Duffin3d1248c2020-04-09 00:10:17 +01001833 props := struct {
Paul Duffin38b57852020-05-13 16:08:09 +01001834 Name *string
1835 Srcs []string
1836 Prefer *bool
Paul Duffin3d1248c2020-04-09 00:10:17 +01001837 }{}
Paul Duffinc3091c82020-05-08 14:16:20 +01001838 props.Name = proptools.StringPtr(module.stubsSourceModuleName(apiScope))
Paul Duffin3d1248c2020-04-09 00:10:17 +01001839 props.Srcs = scopeProperties.Stub_srcs
1840 mctx.CreateModule(PrebuiltStubsSourcesFactory, &props)
Paul Duffin38b57852020-05-13 16:08:09 +01001841
1842 // The stubs source is preferred if the java_sdk_library_import is preferred.
1843 props.Prefer = proptools.BoolPtr(module.prebuilt.Prefer())
Paul Duffin3d1248c2020-04-09 00:10:17 +01001844}
1845
Paul Duffin44f1d842020-06-26 20:17:02 +01001846// Add the dependencies on the child module in the component deps mutator so that it
1847// creates references to the prebuilt and not the source modules.
1848func (module *SdkLibraryImport) ComponentDepsMutator(ctx android.BottomUpMutatorContext) {
Paul Duffin46a26a82020-04-07 19:27:04 +01001849 for apiScope, scopeProperties := range module.scopeProperties {
Paul Duffin56d44902020-01-31 13:36:25 +00001850 if len(scopeProperties.Jars) == 0 {
1851 continue
1852 }
1853
1854 // Add dependencies to the prebuilt stubs library
Paul Duffin44f1d842020-06-26 20:17:02 +01001855 ctx.AddVariationDependencies(nil, apiScope.stubsTag, "prebuilt_"+module.stubsLibraryModuleName(apiScope))
Paul Duffin0f8faff2020-05-20 16:18:00 +01001856
1857 if len(scopeProperties.Stub_srcs) > 0 {
1858 // Add dependencies to the prebuilt stubs source library
Paul Duffin44f1d842020-06-26 20:17:02 +01001859 ctx.AddVariationDependencies(nil, apiScope.stubsSourceTag, "prebuilt_"+module.stubsSourceModuleName(apiScope))
Paul Duffin0f8faff2020-05-20 16:18:00 +01001860 }
Paul Duffin56d44902020-01-31 13:36:25 +00001861 }
Paul Duffin44f1d842020-06-26 20:17:02 +01001862}
1863
1864// Add other dependencies as normal.
1865func (module *SdkLibraryImport) DepsMutator(ctx android.BottomUpMutatorContext) {
Paul Duffineedc5d52020-06-12 17:46:39 +01001866
1867 implName := module.implLibraryModuleName()
1868 if ctx.OtherModuleExists(implName) {
1869 ctx.AddVariationDependencies(nil, implLibraryTag, implName)
1870
1871 xmlPermissionsModuleName := module.xmlPermissionsModuleName()
1872 if module.sharedLibrary() && ctx.OtherModuleExists(xmlPermissionsModuleName) {
1873 // Add dependency to the rule for generating the xml permissions file
1874 ctx.AddDependency(module, xmlPermissionsFileTag, xmlPermissionsModuleName)
1875 }
1876 }
Colin Cross79c7c262019-04-17 11:11:46 -07001877}
1878
Paul Duffineedc5d52020-06-12 17:46:39 +01001879func (module *SdkLibraryImport) DepIsInSameApex(mctx android.BaseModuleContext, dep android.Module) bool {
1880 depTag := mctx.OtherModuleDependencyTag(dep)
1881 if depTag == xmlPermissionsFileTag {
1882 return true
1883 }
1884
1885 // None of the other dependencies of the java_sdk_library_import are in the same apex
1886 // as the one that references this module.
1887 return false
1888}
1889
Jooyung Han749dc692020-04-15 11:03:39 +09001890func (module *SdkLibraryImport) ShouldSupportSdkVersion(ctx android.BaseModuleContext, sdkVersion int) error {
1891 // we don't check prebuilt modules for sdk_version
1892 return nil
1893}
1894
Paul Duffineedc5d52020-06-12 17:46:39 +01001895func (module *SdkLibraryImport) OutputFiles(tag string) (android.Paths, error) {
Paul Duffin46dc45a2020-05-14 15:39:10 +01001896 return module.commonOutputFiles(tag)
1897}
1898
Paul Duffineedc5d52020-06-12 17:46:39 +01001899func (module *SdkLibraryImport) GenerateAndroidBuildActions(ctx android.ModuleContext) {
Paul Duffin0f8faff2020-05-20 16:18:00 +01001900 // Record the paths to the prebuilt stubs library and stubs source.
Colin Cross79c7c262019-04-17 11:11:46 -07001901 ctx.VisitDirectDeps(func(to android.Module) {
1902 tag := ctx.OtherModuleDependencyTag(to)
1903
Paul Duffin0f8faff2020-05-20 16:18:00 +01001904 // Extract information from any of the scope specific dependencies.
1905 if scopeTag, ok := tag.(scopeDependencyTag); ok {
1906 apiScope := scopeTag.apiScope
1907 scopePaths := module.getScopePathsCreateIfNeeded(apiScope)
1908
1909 // Extract information from the dependency. The exact information extracted
1910 // is determined by the nature of the dependency which is determined by the tag.
1911 scopeTag.extractDepInfo(ctx, to, scopePaths)
Paul Duffineedc5d52020-06-12 17:46:39 +01001912 } else if tag == implLibraryTag {
1913 if implLibrary, ok := to.(*Library); ok {
1914 module.implLibraryModule = implLibrary
1915 } else {
1916 ctx.ModuleErrorf("implementation library must be of type *java.Library but was %T", to)
1917 }
1918 } else if tag == xmlPermissionsFileTag {
1919 if xmlPermissionsFileModule, ok := to.(*sdkLibraryXml); ok {
1920 module.xmlPermissionsFileModule = xmlPermissionsFileModule
1921 } else {
1922 ctx.ModuleErrorf("xml permissions file module must be of type *sdkLibraryXml but was %T", to)
1923 }
Colin Cross79c7c262019-04-17 11:11:46 -07001924 }
1925 })
Paul Duffin0f8faff2020-05-20 16:18:00 +01001926
1927 // Populate the scope paths with information from the properties.
1928 for apiScope, scopeProperties := range module.scopeProperties {
1929 if len(scopeProperties.Jars) == 0 {
1930 continue
1931 }
1932
1933 paths := module.getScopePathsCreateIfNeeded(apiScope)
1934 paths.currentApiFilePath = android.OptionalPathForModuleSrc(ctx, scopeProperties.Current_api)
1935 paths.removedApiFilePath = android.OptionalPathForModuleSrc(ctx, scopeProperties.Removed_api)
1936 }
Colin Cross79c7c262019-04-17 11:11:46 -07001937}
1938
Paul Duffineedc5d52020-06-12 17:46:39 +01001939func (module *SdkLibraryImport) sdkJars(ctx android.BaseModuleContext, sdkVersion sdkSpec, headerJars bool) android.Paths {
1940
1941 // For consistency with SdkLibrary make the implementation jar available to libraries that
1942 // are within the same APEX.
1943 implLibraryModule := module.implLibraryModule
Colin Crossaede88c2020-08-11 12:17:01 -07001944 if implLibraryModule != nil && withinSameApexesAs(module, ctx.Module()) {
Paul Duffineedc5d52020-06-12 17:46:39 +01001945 if headerJars {
1946 return implLibraryModule.HeaderJars()
1947 } else {
1948 return implLibraryModule.ImplementationJars()
1949 }
1950 }
1951
Paul Duffin23970f42020-05-20 14:20:02 +01001952 return module.selectHeaderJarsForSdkVersion(ctx, sdkVersion)
Paul Duffin56d44902020-01-31 13:36:25 +00001953}
1954
Colin Cross79c7c262019-04-17 11:11:46 -07001955// to satisfy SdkLibraryDependency interface
Paul Duffineedc5d52020-06-12 17:46:39 +01001956func (module *SdkLibraryImport) SdkHeaderJars(ctx android.BaseModuleContext, sdkVersion sdkSpec) android.Paths {
Colin Cross79c7c262019-04-17 11:11:46 -07001957 // This module is just a wrapper for the prebuilt stubs.
Paul Duffineedc5d52020-06-12 17:46:39 +01001958 return module.sdkJars(ctx, sdkVersion, true)
Colin Cross79c7c262019-04-17 11:11:46 -07001959}
1960
1961// to satisfy SdkLibraryDependency interface
Paul Duffineedc5d52020-06-12 17:46:39 +01001962func (module *SdkLibraryImport) SdkImplementationJars(ctx android.BaseModuleContext, sdkVersion sdkSpec) android.Paths {
Colin Cross79c7c262019-04-17 11:11:46 -07001963 // This module is just a wrapper for the stubs.
Paul Duffineedc5d52020-06-12 17:46:39 +01001964 return module.sdkJars(ctx, sdkVersion, false)
1965}
1966
Ulya Trafimovich31e444e2020-08-14 17:32:16 +01001967// to satisfy SdkLibraryDependency interface
Paul Duffineedc5d52020-06-12 17:46:39 +01001968func (module *SdkLibraryImport) DexJarBuildPath() android.Path {
1969 if module.implLibraryModule == nil {
1970 return nil
1971 } else {
1972 return module.implLibraryModule.DexJarBuildPath()
1973 }
1974}
1975
Ulya Trafimovich31e444e2020-08-14 17:32:16 +01001976// to satisfy SdkLibraryDependency interface
1977func (module *SdkLibraryImport) DexJarInstallPath() android.Path {
1978 if module.implLibraryModule == nil {
1979 return nil
1980 } else {
1981 return module.implLibraryModule.DexJarInstallPath()
1982 }
1983}
1984
Paul Duffineedc5d52020-06-12 17:46:39 +01001985// to satisfy apex.javaDependency interface
1986func (module *SdkLibraryImport) JacocoReportClassesFile() android.Path {
1987 if module.implLibraryModule == nil {
1988 return nil
1989 } else {
1990 return module.implLibraryModule.JacocoReportClassesFile()
1991 }
1992}
1993
1994// to satisfy apex.javaDependency interface
Colin Cross08dca382020-07-21 20:31:17 -07001995func (module *SdkLibraryImport) LintDepSets() LintDepSets {
1996 if module.implLibraryModule == nil {
1997 return LintDepSets{}
1998 } else {
1999 return module.implLibraryModule.LintDepSets()
2000 }
2001}
2002
2003// to satisfy apex.javaDependency interface
Paul Duffineedc5d52020-06-12 17:46:39 +01002004func (module *SdkLibraryImport) Stem() string {
2005 return module.BaseModuleName()
Colin Cross79c7c262019-04-17 11:11:46 -07002006}
Jiyong Parke3833882020-02-17 17:28:10 +09002007
Paul Duffin44b481b2020-06-17 16:59:43 +01002008var _ ApexDependency = (*SdkLibraryImport)(nil)
2009
2010// to satisfy java.ApexDependency interface
2011func (module *SdkLibraryImport) HeaderJars() android.Paths {
2012 if module.implLibraryModule == nil {
2013 return nil
2014 } else {
2015 return module.implLibraryModule.HeaderJars()
2016 }
2017}
2018
2019// to satisfy java.ApexDependency interface
2020func (module *SdkLibraryImport) ImplementationAndResourcesJars() android.Paths {
2021 if module.implLibraryModule == nil {
2022 return nil
2023 } else {
2024 return module.implLibraryModule.ImplementationAndResourcesJars()
2025 }
2026}
2027
Jiyong Parke3833882020-02-17 17:28:10 +09002028//
2029// java_sdk_library_xml
2030//
2031type sdkLibraryXml struct {
2032 android.ModuleBase
2033 android.DefaultableModuleBase
2034 android.ApexModuleBase
2035
2036 properties sdkLibraryXmlProperties
2037
2038 outputFilePath android.OutputPath
2039 installDirPath android.InstallPath
2040}
2041
2042type sdkLibraryXmlProperties struct {
2043 // canonical name of the lib
2044 Lib_name *string
2045}
2046
2047// java_sdk_library_xml builds the permission xml file for a java_sdk_library.
2048// Not to be used directly by users. java_sdk_library internally uses this.
2049func sdkLibraryXmlFactory() android.Module {
2050 module := &sdkLibraryXml{}
2051
2052 module.AddProperties(&module.properties)
2053
2054 android.InitApexModule(module)
2055 android.InitAndroidArchModule(module, android.DeviceSupported, android.MultilibCommon)
2056
2057 return module
2058}
2059
Colin Crossaede88c2020-08-11 12:17:01 -07002060func (module *sdkLibraryXml) UniqueApexVariations() bool {
2061 // sdkLibraryXml needs a unique variation per APEX because the generated XML file contains the path to the
2062 // mounted APEX, which contains the name of the APEX.
2063 return true
2064}
2065
Jiyong Parke3833882020-02-17 17:28:10 +09002066// from android.PrebuiltEtcModule
Jooyung Han0703fd82020-08-26 22:11:53 +09002067func (module *sdkLibraryXml) BaseDir() string {
2068 return "etc"
2069}
2070
2071// from android.PrebuiltEtcModule
Jiyong Parke3833882020-02-17 17:28:10 +09002072func (module *sdkLibraryXml) SubDir() string {
2073 return "permissions"
2074}
2075
2076// from android.PrebuiltEtcModule
2077func (module *sdkLibraryXml) OutputFile() android.OutputPath {
2078 return module.outputFilePath
2079}
2080
2081// from android.ApexModule
2082func (module *sdkLibraryXml) AvailableFor(what string) bool {
2083 return true
2084}
2085
2086func (module *sdkLibraryXml) DepsMutator(ctx android.BottomUpMutatorContext) {
2087 // do nothing
2088}
2089
Jooyung Han749dc692020-04-15 11:03:39 +09002090func (module *sdkLibraryXml) ShouldSupportSdkVersion(ctx android.BaseModuleContext, sdkVersion int) error {
2091 // sdkLibraryXml doesn't need to be checked separately because java_sdk_library is checked
2092 return nil
2093}
2094
Jiyong Parke3833882020-02-17 17:28:10 +09002095// File path to the runtime implementation library
2096func (module *sdkLibraryXml) implPath() string {
2097 implName := proptools.String(module.properties.Lib_name)
Colin Crosse07f2312020-08-13 11:24:56 -07002098 if apexName := module.ApexVariationName(); apexName != "" {
2099 // TODO(b/146468504): ApexVariationName() is only a soong module name, not apex name.
Jiyong Parke3833882020-02-17 17:28:10 +09002100 // In most cases, this works fine. But when apex_name is set or override_apex is used
2101 // this can be wrong.
2102 return fmt.Sprintf("/apex/%s/javalib/%s.jar", apexName, implName)
2103 }
2104 partition := "system"
2105 if module.SocSpecific() {
2106 partition = "vendor"
2107 } else if module.DeviceSpecific() {
2108 partition = "odm"
2109 } else if module.ProductSpecific() {
2110 partition = "product"
2111 } else if module.SystemExtSpecific() {
2112 partition = "system_ext"
2113 }
2114 return "/" + partition + "/framework/" + implName + ".jar"
2115}
2116
2117func (module *sdkLibraryXml) GenerateAndroidBuildActions(ctx android.ModuleContext) {
2118 libName := proptools.String(module.properties.Lib_name)
2119 xmlContent := fmt.Sprintf(permissionsTemplate, libName, module.implPath())
2120
2121 module.outputFilePath = android.PathForModuleOut(ctx, libName+".xml").OutputPath
2122 rule := android.NewRuleBuilder()
2123 rule.Command().
2124 Text("/bin/bash -c \"echo -e '" + xmlContent + "'\" > ").
2125 Output(module.outputFilePath)
2126
2127 rule.Build(pctx, ctx, "java_sdk_xml", "Permission XML")
2128
2129 module.installDirPath = android.PathForModuleInstall(ctx, "etc", module.SubDir())
2130}
2131
2132func (module *sdkLibraryXml) AndroidMkEntries() []android.AndroidMkEntries {
2133 if !module.IsForPlatform() {
2134 return []android.AndroidMkEntries{android.AndroidMkEntries{
2135 Disabled: true,
2136 }}
2137 }
2138
2139 return []android.AndroidMkEntries{android.AndroidMkEntries{
2140 Class: "ETC",
2141 OutputFile: android.OptionalPathForPath(module.outputFilePath),
2142 ExtraEntries: []android.AndroidMkExtraEntriesFunc{
2143 func(entries *android.AndroidMkEntries) {
2144 entries.SetString("LOCAL_MODULE_TAGS", "optional")
2145 entries.SetString("LOCAL_MODULE_PATH", module.installDirPath.ToMakePath().String())
2146 entries.SetString("LOCAL_INSTALLED_MODULE_STEM", module.outputFilePath.Base())
2147 },
2148 },
2149 }}
2150}
Paul Duffindd46f712020-02-10 13:37:10 +00002151
2152type sdkLibrarySdkMemberType struct {
2153 android.SdkMemberTypeBase
2154}
2155
2156func (s *sdkLibrarySdkMemberType) AddDependencies(mctx android.BottomUpMutatorContext, dependencyTag blueprint.DependencyTag, names []string) {
2157 mctx.AddVariationDependencies(nil, dependencyTag, names...)
2158}
2159
2160func (s *sdkLibrarySdkMemberType) IsInstance(module android.Module) bool {
2161 _, ok := module.(*SdkLibrary)
2162 return ok
2163}
2164
2165func (s *sdkLibrarySdkMemberType) AddPrebuiltModule(ctx android.SdkMemberContext, member android.SdkMember) android.BpModule {
2166 return ctx.SnapshotBuilder().AddPrebuiltModule(member, "java_sdk_library_import")
2167}
2168
2169func (s *sdkLibrarySdkMemberType) CreateVariantPropertiesStruct() android.SdkMemberProperties {
2170 return &sdkLibrarySdkMemberProperties{}
2171}
2172
2173type sdkLibrarySdkMemberProperties struct {
2174 android.SdkMemberPropertiesBase
2175
2176 // Scope to per scope properties.
2177 Scopes map[*apiScope]scopeProperties
2178
2179 // Additional libraries that the exported stubs libraries depend upon.
2180 Libs []string
Paul Duffin3d1248c2020-04-09 00:10:17 +01002181
2182 // The Java stubs source files.
2183 Stub_srcs []string
Paul Duffinf7a64332020-05-13 16:54:55 +01002184
2185 // The naming scheme.
2186 Naming_scheme *string
Paul Duffind7eb1c22020-05-26 20:57:10 +01002187
2188 // True if the java_sdk_library_import is for a shared library, false
2189 // otherwise.
2190 Shared_library *bool
Paul Duffindd46f712020-02-10 13:37:10 +00002191}
2192
2193type scopeProperties struct {
Paul Duffin1fd005d2020-04-09 01:08:11 +01002194 Jars android.Paths
2195 StubsSrcJar android.Path
2196 CurrentApiFile android.Path
2197 RemovedApiFile android.Path
2198 SdkVersion string
Paul Duffindd46f712020-02-10 13:37:10 +00002199}
2200
2201func (s *sdkLibrarySdkMemberProperties) PopulateFromVariant(ctx android.SdkMemberContext, variant android.Module) {
2202 sdk := variant.(*SdkLibrary)
2203
2204 s.Scopes = make(map[*apiScope]scopeProperties)
2205 for _, apiScope := range allApiScopes {
Paul Duffin803a9562020-05-20 11:52:25 +01002206 paths := sdk.findScopePaths(apiScope)
2207 if paths == nil {
2208 continue
2209 }
2210
Paul Duffindd46f712020-02-10 13:37:10 +00002211 jars := paths.stubsImplPath
2212 if len(jars) > 0 {
2213 properties := scopeProperties{}
2214 properties.Jars = jars
Paul Duffin780c5f42020-05-12 15:52:55 +01002215 properties.SdkVersion = sdk.sdkVersionForStubsLibrary(ctx.SdkModuleContext(), apiScope)
Paul Duffin0f8faff2020-05-20 16:18:00 +01002216 properties.StubsSrcJar = paths.stubsSrcJar.Path()
Paul Duffin10269f12020-06-19 18:39:55 +01002217 if paths.currentApiFilePath.Valid() {
2218 properties.CurrentApiFile = paths.currentApiFilePath.Path()
2219 }
2220 if paths.removedApiFilePath.Valid() {
2221 properties.RemovedApiFile = paths.removedApiFilePath.Path()
2222 }
Paul Duffindd46f712020-02-10 13:37:10 +00002223 s.Scopes[apiScope] = properties
2224 }
2225 }
2226
2227 s.Libs = sdk.properties.Libs
Paul Duffindfa131e2020-05-15 20:37:11 +01002228 s.Naming_scheme = sdk.commonSdkLibraryProperties.Naming_scheme
Paul Duffind7eb1c22020-05-26 20:57:10 +01002229 s.Shared_library = proptools.BoolPtr(sdk.sharedLibrary())
Paul Duffindd46f712020-02-10 13:37:10 +00002230}
2231
2232func (s *sdkLibrarySdkMemberProperties) AddToPropertySet(ctx android.SdkMemberContext, propertySet android.BpPropertySet) {
Paul Duffinf7a64332020-05-13 16:54:55 +01002233 if s.Naming_scheme != nil {
2234 propertySet.AddProperty("naming_scheme", proptools.String(s.Naming_scheme))
2235 }
Paul Duffind7eb1c22020-05-26 20:57:10 +01002236 if s.Shared_library != nil {
2237 propertySet.AddProperty("shared_library", *s.Shared_library)
2238 }
Paul Duffinf7a64332020-05-13 16:54:55 +01002239
Paul Duffindd46f712020-02-10 13:37:10 +00002240 for _, apiScope := range allApiScopes {
2241 if properties, ok := s.Scopes[apiScope]; ok {
Paul Duffin6b836ba2020-05-13 19:19:49 +01002242 scopeSet := propertySet.AddPropertySet(apiScope.propertyName)
Paul Duffindd46f712020-02-10 13:37:10 +00002243
Paul Duffin3d1248c2020-04-09 00:10:17 +01002244 scopeDir := filepath.Join("sdk_library", s.OsPrefix(), apiScope.name)
2245
Paul Duffindd46f712020-02-10 13:37:10 +00002246 var jars []string
2247 for _, p := range properties.Jars {
Paul Duffin3d1248c2020-04-09 00:10:17 +01002248 dest := filepath.Join(scopeDir, ctx.Name()+"-stubs.jar")
Paul Duffindd46f712020-02-10 13:37:10 +00002249 ctx.SnapshotBuilder().CopyToSnapshot(p, dest)
2250 jars = append(jars, dest)
2251 }
2252 scopeSet.AddProperty("jars", jars)
2253
Paul Duffin3d1248c2020-04-09 00:10:17 +01002254 // Merge the stubs source jar into the snapshot zip so that when it is unpacked
2255 // the source files are also unpacked.
2256 snapshotRelativeDir := filepath.Join(scopeDir, ctx.Name()+"_stub_sources")
2257 ctx.SnapshotBuilder().UnzipToSnapshot(properties.StubsSrcJar, snapshotRelativeDir)
2258 scopeSet.AddProperty("stub_srcs", []string{snapshotRelativeDir})
2259
Paul Duffin1fd005d2020-04-09 01:08:11 +01002260 if properties.CurrentApiFile != nil {
2261 currentApiSnapshotPath := filepath.Join(scopeDir, ctx.Name()+".txt")
2262 ctx.SnapshotBuilder().CopyToSnapshot(properties.CurrentApiFile, currentApiSnapshotPath)
2263 scopeSet.AddProperty("current_api", currentApiSnapshotPath)
2264 }
2265
2266 if properties.RemovedApiFile != nil {
2267 removedApiSnapshotPath := filepath.Join(scopeDir, ctx.Name()+"-removed.txt")
Paul Duffin3dbf9fd2020-06-02 13:00:02 +01002268 ctx.SnapshotBuilder().CopyToSnapshot(properties.RemovedApiFile, removedApiSnapshotPath)
Paul Duffin1fd005d2020-04-09 01:08:11 +01002269 scopeSet.AddProperty("removed_api", removedApiSnapshotPath)
2270 }
2271
Paul Duffindd46f712020-02-10 13:37:10 +00002272 if properties.SdkVersion != "" {
2273 scopeSet.AddProperty("sdk_version", properties.SdkVersion)
2274 }
2275 }
2276 }
2277
2278 if len(s.Libs) > 0 {
2279 propertySet.AddPropertyWithTag("libs", s.Libs, ctx.SnapshotBuilder().SdkMemberReferencePropertyTag(false))
2280 }
2281}