blob: dd71c43382c1e18d296327e04a490ef7abf571c2 [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 Duffin6a2bd112020-04-07 19:27:04 +010021 "reflect"
Paul Duffin46fdda82020-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 Duffin6a2bd112020-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 Duffin1c094a02020-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 Duffin5fb82132020-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 Duffin51a2bee2020-05-05 14:40:52 +010084 // The api scope that this scope extends.
85 extends *apiScope
86
Paul Duffin3a254982020-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 Duffin6a2bd112020-04-07 19:27:04 +010099 // The name of the field in the dynamically created structure.
100 fieldName string
101
Paul Duffin0f270632020-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 Duffina377e4c2020-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 Duffin5fb82132020-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 Duffin3c7c3472020-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 Duffin3c7c3472020-04-07 18:50:10 +0100132 // Extra arguments to pass to droidstubs for this scope.
Paul Duffina377e4c2020-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 Duffina377e4c2020-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 Duffina377e4c2020-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 Duffina377e4c2020-04-29 13:30:54 +0100146
Anton Hansson5ff28e52020-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 Duffin5fb82132020-04-29 20:45:27 +0100153 name := scope.name
Paul Duffin46fdda82020-05-14 15:39:10 +0100154 scopeByName[name] = scope
155 allScopeNames = append(allScopeNames, name)
Paul Duffin0f270632020-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 Duffin5fb82132020-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 Duffina377e4c2020-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 Duffin5fb82132020-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 Duffina377e4c2020-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 Duffina377e4c2020-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 Duffina377e4c2020-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 Duffina377e4c2020-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 Duffina377e4c2020-04-29 13:30:54 +0100199
Paul Duffind1b3a922020-01-22 11:57:20 +0000200 return scope
201}
202
Paul Duffinb74ee3e2020-05-08 14:16:20 +0100203func (scope *apiScope) stubsLibraryModuleName(baseName string) string {
Paul Duffin1c094a02020-05-08 15:52:37 +0100204 return baseName + ".stubs" + scope.moduleSuffix
Paul Duffind1b3a922020-01-22 11:57:20 +0000205}
206
Paul Duffin5fb82132020-04-29 20:45:27 +0100207func (scope *apiScope) stubsSourceModuleName(baseName string) string {
Paul Duffin1c094a02020-05-08 15:52:37 +0100208 return baseName + ".stubs.source" + scope.moduleSuffix
Paul Duffind1b3a922020-01-22 11:57:20 +0000209}
210
Paul Duffina377e4c2020-04-29 13:30:54 +0100211func (scope *apiScope) apiModuleName(baseName string) string {
Paul Duffin1c094a02020-05-08 15:52:37 +0100212 return baseName + ".api" + scope.moduleSuffix
Paul Duffina377e4c2020-04-29 13:30:54 +0100213}
214
Paul Duffin3a254982020-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 Duffin46fdda82020-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 Duffin3a254982020-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 Duffin3a254982020-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 Duffin3a254982020-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 Duffin6d7f0a72020-04-28 14:13:56 +0100271 apiScopeModuleLib = initApiScope(&apiScope{
Paul Duffin0f270632020-05-13 19:19:49 +0100272 name: "module-lib",
Paul Duffin6d7f0a72020-04-28 14:13:56 +0100273 extends: apiScopeSystem,
Paul Duffin5a757b12020-06-02 13:00:08 +0100274 // The module-lib scope is disabled by default in legacy mode.
Paul Duffin6d7f0a72020-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 Duffin6d7f0a72020-04-28 14:13:56 +0100287 })
Paul Duffin5a757b12020-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 Duffin5a757b12020-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 Duffin5a757b12020-06-02 13:00:08 +0100308 },
309 })
Paul Duffind1b3a922020-01-22 11:57:20 +0000310 allApiScopes = apiScopes{
311 apiScopePublic,
312 apiScopeSystem,
313 apiScopeTest,
Paul Duffin6d7f0a72020-04-28 14:13:56 +0100314 apiScopeModuleLib,
Paul Duffin5a757b12020-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 Duffin61871622020-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 Duffin3a254982020-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 Duffin080f5ee2020-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 Duffin3a254982020-04-28 10:44:03 +0100376}
377
Jiyong Parkc678ad32018-04-10 13:07:10 +0900378type sdkLibraryProperties struct {
Paul Duffin9d582cc2020-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 Duffin344c4ee2020-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 Duffind11e78e2020-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 Duffin2ce1e812020-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 Duffin3a254982020-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 Duffin5a757b12020-06-02 13:00:08 +0100463 // The properties specific to the module-lib api scope
Paul Duffin6d7f0a72020-04-28 14:13:56 +0100464 //
Paul Duffin5a757b12020-06-02 13:00:08 +0100465 // Unless explicitly specified by using test.enabled the module-lib api scope is
Paul Duffin6d7f0a72020-04-28 14:13:56 +0100466 // disabled by default.
467 Module_lib ApiScopeProperties
468
Paul Duffin5a757b12020-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 Park27fc4142020-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 Duffin8986cc92020-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 Duffin533f9c72020-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 Duffin533f9c72020-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 Duffin5fb82132020-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 Duffina377e4c2020-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 Duffin5fb82132020-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 Duffin533f9c72020-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 Duffina377e4c2020-04-29 13:30:54 +0100547func (paths *scopePaths) extractApiInfoFromApiStubsProvider(provider ApiStubsProvider) {
Paul Duffin533f9c72020-05-20 16:18:00 +0100548 paths.currentApiFilePath = android.OptionalPathForPath(provider.ApiFilePath())
549 paths.removedApiFilePath = android.OptionalPathForPath(provider.RemovedApiFilePath())
Paul Duffina377e4c2020-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 Duffin533f9c72020-05-20 16:18:00 +0100558func (paths *scopePaths) extractStubsSourceInfoFromApiStubsProviders(provider ApiStubsSrcProvider) {
559 paths.stubsSrcJar = android.OptionalPathForPath(provider.StubsSrcJar())
Paul Duffina377e4c2020-04-29 13:30:54 +0100560}
561
562func (paths *scopePaths) extractStubsSourceInfoFromDep(dep android.Module) error {
Paul Duffin533f9c72020-05-20 16:18:00 +0100563 return paths.treatDepAsApiStubsSrcProvider(dep, func(provider ApiStubsSrcProvider) {
Paul Duffina377e4c2020-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 Duffin1a724e62020-05-08 13:44:43 +0100576 // The naming scheme to use for the components that this module creates.
577 //
Paul Duffindef8a892020-05-08 15:36:30 +0100578 // If not specified then it defaults to "default". The other allowable value is
579 // "framework-modules" which matches the scheme currently used by framework modules
580 // for the equivalent components represented as separate Soong modules.
Paul Duffin1a724e62020-05-08 13:44:43 +0100581 //
582 // This is a temporary mechanism to simplify conversion from separate modules for each
583 // component that follow a different naming pattern to the default one.
584 //
585 // TODO(b/155480189) - Remove once naming inconsistencies have been resolved.
Paul Duffina377e4c2020-04-29 13:30:54 +0100586 Naming_scheme *string
Paul Duffind11e78e2020-05-15 20:37:11 +0100587
588 // Specifies whether this module can be used as an Android shared library; defaults
589 // to true.
590 //
591 // An Android shared library is one that can be referenced in a <uses-library> element
592 // in an AndroidManifest.xml.
593 Shared_library *bool
Paul Duffina377e4c2020-04-29 13:30:54 +0100594}
595
Paul Duffin56d44902020-01-31 13:36:25 +0000596// Common code between sdk library and sdk library import
597type commonToSdkLibraryAndImport struct {
Paul Duffinb74ee3e2020-05-08 14:16:20 +0100598 moduleBase *android.ModuleBase
599
Paul Duffin56d44902020-01-31 13:36:25 +0000600 scopePaths map[*apiScope]*scopePaths
Paul Duffin1a724e62020-05-08 13:44:43 +0100601
602 namingScheme sdkLibraryComponentNamingScheme
603
Paul Duffind11e78e2020-05-15 20:37:11 +0100604 commonSdkLibraryProperties commonToSdkLibraryAndImportProperties
Paul Duffin64e61992020-05-15 10:20:31 +0100605
606 // Functionality related to this being used as a component of a java_sdk_library.
607 EmbeddableSdkLibraryComponent
Paul Duffin56d44902020-01-31 13:36:25 +0000608}
609
Paul Duffinb74ee3e2020-05-08 14:16:20 +0100610func (c *commonToSdkLibraryAndImport) initCommon(moduleBase *android.ModuleBase) {
611 c.moduleBase = moduleBase
Paul Duffin1a724e62020-05-08 13:44:43 +0100612
Paul Duffind11e78e2020-05-15 20:37:11 +0100613 moduleBase.AddProperties(&c.commonSdkLibraryProperties)
Paul Duffin64e61992020-05-15 10:20:31 +0100614
615 // Initialize this as an sdk library component.
616 c.initSdkLibraryComponent(moduleBase)
Paul Duffin1a724e62020-05-08 13:44:43 +0100617}
618
619func (c *commonToSdkLibraryAndImport) initCommonAfterDefaultsApplied(ctx android.DefaultableHookContext) bool {
Paul Duffind11e78e2020-05-15 20:37:11 +0100620 schemeProperty := proptools.StringDefault(c.commonSdkLibraryProperties.Naming_scheme, "default")
Paul Duffin1a724e62020-05-08 13:44:43 +0100621 switch schemeProperty {
622 case "default":
623 c.namingScheme = &defaultNamingScheme{}
Paul Duffindef8a892020-05-08 15:36:30 +0100624 case "framework-modules":
625 c.namingScheme = &frameworkModulesNamingScheme{}
Paul Duffin1a724e62020-05-08 13:44:43 +0100626 default:
627 ctx.PropertyErrorf("naming_scheme", "expected 'default' but was %q", schemeProperty)
628 return false
629 }
630
Paul Duffind11e78e2020-05-15 20:37:11 +0100631 // Only track this sdk library if this can be used as a shared library.
632 if c.sharedLibrary() {
633 // Use the name specified in the module definition as the owner.
634 c.sdkLibraryComponentProperties.SdkLibraryToImplicitlyTrack = proptools.StringPtr(c.moduleBase.BaseModuleName())
635 }
Paul Duffin64e61992020-05-15 10:20:31 +0100636
Paul Duffin1a724e62020-05-08 13:44:43 +0100637 return true
Paul Duffinb74ee3e2020-05-08 14:16:20 +0100638}
639
Paul Duffineedc5d52020-06-12 17:46:39 +0100640// Module name of the runtime implementation library
641func (c *commonToSdkLibraryAndImport) implLibraryModuleName() string {
642 return c.moduleBase.BaseModuleName() + ".impl"
643}
644
645// Module name of the XML file for the lib
646func (c *commonToSdkLibraryAndImport) xmlPermissionsModuleName() string {
647 return c.moduleBase.BaseModuleName() + sdkXmlFileSuffix
648}
649
Paul Duffinb74ee3e2020-05-08 14:16:20 +0100650// Name of the java_library module that compiles the stubs source.
651func (c *commonToSdkLibraryAndImport) stubsLibraryModuleName(apiScope *apiScope) string {
Paul Duffin1a724e62020-05-08 13:44:43 +0100652 return c.namingScheme.stubsLibraryModuleName(apiScope, c.moduleBase.BaseModuleName())
Paul Duffinb74ee3e2020-05-08 14:16:20 +0100653}
654
655// Name of the droidstubs module that generates the stubs source and may also
656// generate/check the API.
657func (c *commonToSdkLibraryAndImport) stubsSourceModuleName(apiScope *apiScope) string {
Paul Duffin1a724e62020-05-08 13:44:43 +0100658 return c.namingScheme.stubsSourceModuleName(apiScope, c.moduleBase.BaseModuleName())
Paul Duffinb74ee3e2020-05-08 14:16:20 +0100659}
660
661// Name of the droidstubs module that generates/checks the API. Only used if it
662// requires different arts to the stubs source generating module.
663func (c *commonToSdkLibraryAndImport) apiModuleName(apiScope *apiScope) string {
Paul Duffin1a724e62020-05-08 13:44:43 +0100664 return c.namingScheme.apiModuleName(apiScope, c.moduleBase.BaseModuleName())
Paul Duffinb74ee3e2020-05-08 14:16:20 +0100665}
666
Paul Duffin46fdda82020-05-14 15:39:10 +0100667// The component names for different outputs of the java_sdk_library.
668//
669// They are similar to the names used for the child modules it creates
670const (
671 stubsSourceComponentName = "stubs.source"
672
673 apiTxtComponentName = "api.txt"
674
675 removedApiTxtComponentName = "removed-api.txt"
676)
677
678// A regular expression to match tags that reference a specific stubs component.
679//
680// It will only match if given a valid scope and a valid component. It is verfy strict
681// to ensure it does not accidentally match a similar looking tag that should be processed
682// by the embedded Library.
683var tagSplitter = func() *regexp.Regexp {
684 // Given a list of literal string items returns a regular expression that will
685 // match any one of the items.
686 choice := func(items ...string) string {
687 return `\Q` + strings.Join(items, `\E|\Q`) + `\E`
688 }
689
690 // Regular expression to match one of the scopes.
691 scopesRegexp := choice(allScopeNames...)
692
693 // Regular expression to match one of the components.
694 componentsRegexp := choice(stubsSourceComponentName, apiTxtComponentName, removedApiTxtComponentName)
695
696 // Regular expression to match any combination of one scope and one component.
697 return regexp.MustCompile(fmt.Sprintf(`^\.(%s)\.(%s)$`, scopesRegexp, componentsRegexp))
698}()
699
700// For OutputFileProducer interface
701//
702// .<scope>.stubs.source
703// .<scope>.api.txt
704// .<scope>.removed-api.txt
705func (c *commonToSdkLibraryAndImport) commonOutputFiles(tag string) (android.Paths, error) {
706 if groups := tagSplitter.FindStringSubmatch(tag); groups != nil {
707 scopeName := groups[1]
708 component := groups[2]
709
710 if scope, ok := scopeByName[scopeName]; ok {
711 paths := c.findScopePaths(scope)
712 if paths == nil {
713 return nil, fmt.Errorf("%q does not provide api scope %s", c.moduleBase.BaseModuleName(), scopeName)
714 }
715
716 switch component {
717 case stubsSourceComponentName:
718 if paths.stubsSrcJar.Valid() {
719 return android.Paths{paths.stubsSrcJar.Path()}, nil
720 }
721
722 case apiTxtComponentName:
723 if paths.currentApiFilePath.Valid() {
724 return android.Paths{paths.currentApiFilePath.Path()}, nil
725 }
726
727 case removedApiTxtComponentName:
728 if paths.removedApiFilePath.Valid() {
729 return android.Paths{paths.removedApiFilePath.Path()}, nil
730 }
731 }
732
733 return nil, fmt.Errorf("%s not available for api scope %s", component, scopeName)
734 } else {
735 return nil, fmt.Errorf("unknown scope %s in %s", scope, tag)
736 }
737
738 } else {
739 return nil, nil
740 }
741}
742
Paul Duffin5ae30792020-05-20 11:52:25 +0100743func (c *commonToSdkLibraryAndImport) getScopePathsCreateIfNeeded(scope *apiScope) *scopePaths {
Paul Duffin56d44902020-01-31 13:36:25 +0000744 if c.scopePaths == nil {
745 c.scopePaths = make(map[*apiScope]*scopePaths)
746 }
747 paths := c.scopePaths[scope]
748 if paths == nil {
749 paths = &scopePaths{}
750 c.scopePaths[scope] = paths
751 }
752
753 return paths
754}
755
Paul Duffin5ae30792020-05-20 11:52:25 +0100756func (c *commonToSdkLibraryAndImport) findScopePaths(scope *apiScope) *scopePaths {
757 if c.scopePaths == nil {
758 return nil
759 }
760
761 return c.scopePaths[scope]
762}
763
764// If this does not support the requested api scope then find the closest available
765// scope it does support. Returns nil if no such scope is available.
766func (c *commonToSdkLibraryAndImport) findClosestScopePath(scope *apiScope) *scopePaths {
767 for s := scope; s != nil; s = s.extends {
768 if paths := c.findScopePaths(s); paths != nil {
769 return paths
770 }
771 }
772
773 // This should never happen outside tests as public should be the base scope for every
774 // scope and is enabled by default.
775 return nil
776}
777
Paul Duffina3fb67d2020-05-20 14:20:02 +0100778func (c *commonToSdkLibraryAndImport) selectHeaderJarsForSdkVersion(ctx android.BaseModuleContext, sdkVersion sdkSpec) android.Paths {
Paul Duffin47624362020-05-20 12:19:10 +0100779
780 // If a specific numeric version has been requested then use prebuilt versions of the sdk.
781 if sdkVersion.version.isNumbered() {
782 return PrebuiltJars(ctx, c.moduleBase.BaseModuleName(), sdkVersion)
783 }
784
785 var apiScope *apiScope
786 switch sdkVersion.kind {
787 case sdkSystem:
788 apiScope = apiScopeSystem
Paul Duffin5ae30792020-05-20 11:52:25 +0100789 case sdkModule:
790 apiScope = apiScopeModuleLib
Paul Duffin47624362020-05-20 12:19:10 +0100791 case sdkTest:
792 apiScope = apiScopeTest
Paul Duffin5a757b12020-06-02 13:00:08 +0100793 case sdkSystemServer:
794 apiScope = apiScopeSystemServer
Paul Duffin47624362020-05-20 12:19:10 +0100795 default:
796 apiScope = apiScopePublic
797 }
798
Paul Duffin5ae30792020-05-20 11:52:25 +0100799 paths := c.findClosestScopePath(apiScope)
800 if paths == nil {
801 var scopes []string
802 for _, s := range allApiScopes {
803 if c.findScopePaths(s) != nil {
804 scopes = append(scopes, s.name)
805 }
806 }
807 ctx.ModuleErrorf("requires api scope %s from %s but it only has %q available", apiScope.name, c.moduleBase.BaseModuleName(), scopes)
808 return nil
809 }
810
Paul Duffina3fb67d2020-05-20 14:20:02 +0100811 return paths.stubsHeaderPath
Paul Duffin47624362020-05-20 12:19:10 +0100812}
813
Paul Duffin64e61992020-05-15 10:20:31 +0100814func (c *commonToSdkLibraryAndImport) sdkComponentPropertiesForChildLibrary() interface{} {
815 componentProps := &struct {
816 SdkLibraryToImplicitlyTrack *string
Paul Duffind11e78e2020-05-15 20:37:11 +0100817 }{}
818
819 if c.sharedLibrary() {
Paul Duffin64e61992020-05-15 10:20:31 +0100820 // Mark the stubs library as being components of this java_sdk_library so that
821 // any app that includes code which depends (directly or indirectly) on the stubs
822 // library will have the appropriate <uses-library> invocation inserted into its
823 // manifest if necessary.
Paul Duffind11e78e2020-05-15 20:37:11 +0100824 componentProps.SdkLibraryToImplicitlyTrack = proptools.StringPtr(c.moduleBase.BaseModuleName())
Paul Duffin64e61992020-05-15 10:20:31 +0100825 }
826
827 return componentProps
828}
829
Paul Duffind11e78e2020-05-15 20:37:11 +0100830// Check if this can be used as a shared library.
831func (c *commonToSdkLibraryAndImport) sharedLibrary() bool {
832 return proptools.BoolDefault(c.commonSdkLibraryProperties.Shared_library, true)
833}
834
Paul Duffin64e61992020-05-15 10:20:31 +0100835// Properties related to the use of a module as an component of a java_sdk_library.
836type SdkLibraryComponentProperties struct {
837
838 // The name of the java_sdk_library/_import to add to a <uses-library> entry
839 // in the AndroidManifest.xml of any Android app that includes code that references
840 // this module. If not set then no java_sdk_library/_import is tracked.
841 SdkLibraryToImplicitlyTrack *string `blueprint:"mutated"`
842}
843
844// Structure to be embedded in a module struct that needs to support the
845// SdkLibraryComponentDependency interface.
846type EmbeddableSdkLibraryComponent struct {
847 sdkLibraryComponentProperties SdkLibraryComponentProperties
848}
849
850func (e *EmbeddableSdkLibraryComponent) initSdkLibraryComponent(moduleBase *android.ModuleBase) {
851 moduleBase.AddProperties(&e.sdkLibraryComponentProperties)
852}
853
854// to satisfy SdkLibraryComponentDependency
855func (e *EmbeddableSdkLibraryComponent) OptionalImplicitSdkLibrary() []string {
856 if e.sdkLibraryComponentProperties.SdkLibraryToImplicitlyTrack != nil {
857 return []string{*e.sdkLibraryComponentProperties.SdkLibraryToImplicitlyTrack}
858 }
859 return nil
860}
861
862// Implemented by modules that are (or possibly could be) a component of a java_sdk_library
863// (including the java_sdk_library) itself.
864type SdkLibraryComponentDependency interface {
865 // The optional name of the sdk library that should be implicitly added to the
866 // AndroidManifest of an app that contains code which references the sdk library.
867 //
868 // Returns an array containing 0 or 1 items rather than a *string to make it easier
869 // to append this to the list of exported sdk libraries.
870 OptionalImplicitSdkLibrary() []string
871}
872
873// Make sure that all the module types that are components of java_sdk_library/_import
874// and which can be referenced (directly or indirectly) from an android app implement
875// the SdkLibraryComponentDependency interface.
876var _ SdkLibraryComponentDependency = (*Library)(nil)
877var _ SdkLibraryComponentDependency = (*Import)(nil)
878var _ SdkLibraryComponentDependency = (*SdkLibrary)(nil)
Paul Duffineedc5d52020-06-12 17:46:39 +0100879var _ SdkLibraryComponentDependency = (*SdkLibraryImport)(nil)
Paul Duffin64e61992020-05-15 10:20:31 +0100880
881// Provides access to sdk_version related header and implentation jars.
882type SdkLibraryDependency interface {
883 SdkLibraryComponentDependency
884
885 // Get the header jars appropriate for the supplied sdk_version.
886 //
887 // These are turbine generated jars so they only change if the externals of the
888 // class changes but it does not contain and implementation or JavaDoc.
889 SdkHeaderJars(ctx android.BaseModuleContext, sdkVersion sdkSpec) android.Paths
890
891 // Get the implementation jars appropriate for the supplied sdk version.
892 //
893 // These are either the implementation jar for the whole sdk library or the implementation
894 // jars for the stubs. The latter should only be needed when generating JavaDoc as otherwise
895 // they are identical to the corresponding header jars.
896 SdkImplementationJars(ctx android.BaseModuleContext, sdkVersion sdkSpec) android.Paths
897}
898
Inseob Kimc0907f12019-02-08 21:00:45 +0900899type SdkLibrary struct {
Sundong Ahn054b19a2018-10-19 13:46:09 +0900900 Library
Jiyong Parkc678ad32018-04-10 13:07:10 +0900901
Sundong Ahn054b19a2018-10-19 13:46:09 +0900902 sdkLibraryProperties sdkLibraryProperties
Jiyong Parkc678ad32018-04-10 13:07:10 +0900903
Paul Duffin3a254982020-04-28 10:44:03 +0100904 // Map from api scope to the scope specific property structure.
905 scopeToProperties map[*apiScope]*ApiScopeProperties
906
Paul Duffin56d44902020-01-31 13:36:25 +0000907 commonToSdkLibraryAndImport
Jiyong Parkc678ad32018-04-10 13:07:10 +0900908}
909
Inseob Kimc0907f12019-02-08 21:00:45 +0900910var _ Dependency = (*SdkLibrary)(nil)
911var _ SdkLibraryDependency = (*SdkLibrary)(nil)
Colin Cross897d2ed2019-02-11 14:03:51 -0800912
Paul Duffin3a254982020-04-28 10:44:03 +0100913func (module *SdkLibrary) generateTestAndSystemScopesByDefault() bool {
914 return module.sdkLibraryProperties.Generate_system_and_test_apis
915}
916
917func (module *SdkLibrary) getGeneratedApiScopes(ctx android.EarlyModuleContext) apiScopes {
918 // Check to see if any scopes have been explicitly enabled. If any have then all
919 // must be.
920 anyScopesExplicitlyEnabled := false
921 for _, scope := range allApiScopes {
922 scopeProperties := module.scopeToProperties[scope]
923 if scopeProperties.Enabled != nil {
924 anyScopesExplicitlyEnabled = true
925 break
926 }
Paul Duffind1b3a922020-01-22 11:57:20 +0000927 }
Paul Duffin3a254982020-04-28 10:44:03 +0100928
929 var generatedScopes apiScopes
930 enabledScopes := make(map[*apiScope]struct{})
931 for _, scope := range allApiScopes {
932 scopeProperties := module.scopeToProperties[scope]
933 // If any scopes are explicitly enabled then ignore the legacy enabled status.
934 // This is to ensure that any new usages of this module type do not rely on legacy
935 // behaviour.
936 defaultEnabledStatus := false
937 if anyScopesExplicitlyEnabled {
938 defaultEnabledStatus = scope.defaultEnabledStatus
939 } else {
940 defaultEnabledStatus = scope.legacyEnabledStatus(module)
941 }
942 enabled := proptools.BoolDefault(scopeProperties.Enabled, defaultEnabledStatus)
943 if enabled {
944 enabledScopes[scope] = struct{}{}
945 generatedScopes = append(generatedScopes, scope)
946 }
947 }
948
949 // Now check to make sure that any scope that is extended by an enabled scope is also
950 // enabled.
951 for _, scope := range allApiScopes {
952 if _, ok := enabledScopes[scope]; ok {
953 extends := scope.extends
954 if extends != nil {
955 if _, ok := enabledScopes[extends]; !ok {
956 ctx.ModuleErrorf("enabled api scope %q depends on disabled scope %q", scope, extends)
957 }
958 }
959 }
960 }
961
962 return generatedScopes
Paul Duffind1b3a922020-01-22 11:57:20 +0000963}
964
Paul Duffineedc5d52020-06-12 17:46:39 +0100965type sdkLibraryComponentTag struct {
966 blueprint.BaseDependencyTag
967 name string
968}
969
970// Mark this tag so dependencies that use it are excluded from visibility enforcement.
971func (t sdkLibraryComponentTag) ExcludeFromVisibilityEnforcement() {}
972
973var xmlPermissionsFileTag = sdkLibraryComponentTag{name: "xml-permissions-file"}
Paul Duffine74ac732020-02-06 13:51:46 +0000974
Jiyong Parke3833882020-02-17 17:28:10 +0900975func IsXmlPermissionsFileDepTag(depTag blueprint.DependencyTag) bool {
Paul Duffineedc5d52020-06-12 17:46:39 +0100976 if dt, ok := depTag.(sdkLibraryComponentTag); ok {
Jiyong Parke3833882020-02-17 17:28:10 +0900977 return dt == xmlPermissionsFileTag
978 }
979 return false
980}
981
Paul Duffineedc5d52020-06-12 17:46:39 +0100982var implLibraryTag = sdkLibraryComponentTag{name: "impl-library"}
Paul Duffin9d582cc2020-05-16 15:52:12 +0100983
Paul Duffin44f1d842020-06-26 20:17:02 +0100984// Add the dependencies on the child modules in the component deps mutator.
985func (module *SdkLibrary) ComponentDepsMutator(ctx android.BottomUpMutatorContext) {
Paul Duffin3a254982020-04-28 10:44:03 +0100986 for _, apiScope := range module.getGeneratedApiScopes(ctx) {
Paul Duffind1b3a922020-01-22 11:57:20 +0000987 // Add dependencies to the stubs library
Paul Duffinb74ee3e2020-05-08 14:16:20 +0100988 ctx.AddVariationDependencies(nil, apiScope.stubsTag, module.stubsLibraryModuleName(apiScope))
Paul Duffind1b3a922020-01-22 11:57:20 +0000989
Paul Duffin15f34ef2020-07-20 18:04:44 +0100990 // Add a dependency on the stubs source in order to access both stubs source and api information.
991 ctx.AddVariationDependencies(nil, apiScope.stubsSourceAndApiTag, module.stubsSourceModuleName(apiScope))
Sundong Ahn054b19a2018-10-19 13:46:09 +0900992 }
993
Paul Duffind11e78e2020-05-15 20:37:11 +0100994 if module.requiresRuntimeImplementationLibrary() {
Paul Duffin9d582cc2020-05-16 15:52:12 +0100995 // Add dependency to the rule for generating the implementation library.
996 ctx.AddDependency(module, implLibraryTag, module.implLibraryModuleName())
997
Paul Duffind11e78e2020-05-15 20:37:11 +0100998 if module.sharedLibrary() {
999 // Add dependency to the rule for generating the xml permissions file
Paul Duffineedc5d52020-06-12 17:46:39 +01001000 ctx.AddDependency(module, xmlPermissionsFileTag, module.xmlPermissionsModuleName())
Paul Duffind11e78e2020-05-15 20:37:11 +01001001 }
Paul Duffin44f1d842020-06-26 20:17:02 +01001002 }
1003}
Paul Duffine74ac732020-02-06 13:51:46 +00001004
Paul Duffin44f1d842020-06-26 20:17:02 +01001005// Add other dependencies as normal.
1006func (module *SdkLibrary) DepsMutator(ctx android.BottomUpMutatorContext) {
1007 if module.requiresRuntimeImplementationLibrary() {
Paul Duffind11e78e2020-05-15 20:37:11 +01001008 // Only add the deps for the library if it is actually going to be built.
1009 module.Library.deps(ctx)
1010 }
Jiyong Parkc678ad32018-04-10 13:07:10 +09001011}
1012
Paul Duffin46fdda82020-05-14 15:39:10 +01001013func (module *SdkLibrary) OutputFiles(tag string) (android.Paths, error) {
1014 paths, err := module.commonOutputFiles(tag)
1015 if paths == nil && err == nil {
1016 return module.Library.OutputFiles(tag)
1017 } else {
1018 return paths, err
1019 }
1020}
1021
Inseob Kimc0907f12019-02-08 21:00:45 +09001022func (module *SdkLibrary) GenerateAndroidBuildActions(ctx android.ModuleContext) {
Paul Duffind11e78e2020-05-15 20:37:11 +01001023 // Only build an implementation library if required.
1024 if module.requiresRuntimeImplementationLibrary() {
Paul Duffin43db9be2019-12-30 17:35:49 +00001025 module.Library.GenerateAndroidBuildActions(ctx)
1026 }
Sundong Ahn054b19a2018-10-19 13:46:09 +09001027
Sundong Ahn57368eb2018-07-06 11:20:23 +09001028 // Record the paths to the header jars of the library (stubs and impl).
Paul Duffind1b3a922020-01-22 11:57:20 +00001029 // When this java_sdk_library is depended upon from others via "libs" property,
Jiyong Parkc678ad32018-04-10 13:07:10 +09001030 // the recorded paths will be returned depending on the link type of the caller.
1031 ctx.VisitDirectDeps(func(to android.Module) {
Jiyong Parkc678ad32018-04-10 13:07:10 +09001032 tag := ctx.OtherModuleDependencyTag(to)
1033
Paul Duffin5fb82132020-04-29 20:45:27 +01001034 // Extract information from any of the scope specific dependencies.
1035 if scopeTag, ok := tag.(scopeDependencyTag); ok {
1036 apiScope := scopeTag.apiScope
Paul Duffin5ae30792020-05-20 11:52:25 +01001037 scopePaths := module.getScopePathsCreateIfNeeded(apiScope)
Paul Duffin5fb82132020-04-29 20:45:27 +01001038
1039 // Extract information from the dependency. The exact information extracted
1040 // is determined by the nature of the dependency which is determined by the tag.
1041 scopeTag.extractDepInfo(ctx, to, scopePaths)
Sundong Ahn20e998b2018-07-24 11:19:26 +09001042 }
Jiyong Parkc678ad32018-04-10 13:07:10 +09001043 })
1044}
1045
Jiyong Park0b0e1b92019-12-03 13:24:29 +09001046func (module *SdkLibrary) AndroidMkEntries() []android.AndroidMkEntries {
Paul Duffind11e78e2020-05-15 20:37:11 +01001047 if !module.requiresRuntimeImplementationLibrary() {
Paul Duffin43db9be2019-12-30 17:35:49 +00001048 return nil
1049 }
Jiyong Park0b0e1b92019-12-03 13:24:29 +09001050 entriesList := module.Library.AndroidMkEntries()
Yo Chiang07d75072020-06-05 17:43:19 +08001051 if module.sharedLibrary() {
1052 entries := &entriesList[0]
1053 entries.Required = append(entries.Required, module.xmlPermissionsModuleName())
1054 }
Jiyong Park0b0e1b92019-12-03 13:24:29 +09001055 return entriesList
Jiyong Park82484c02018-04-23 21:41:26 +09001056}
1057
Anton Hansson6bb88102020-03-27 19:43:19 +00001058// The dist path of the stub artifacts
1059func (module *SdkLibrary) apiDistPath(apiScope *apiScope) string {
1060 if module.ModuleBase.Owner() != "" {
1061 return path.Join("apistubs", module.ModuleBase.Owner(), apiScope.name)
1062 } else if Bool(module.sdkLibraryProperties.Core_lib) {
1063 return path.Join("apistubs", "core", apiScope.name)
1064 } else {
1065 return path.Join("apistubs", "android", apiScope.name)
1066 }
1067}
1068
Paul Duffin12ceb462019-12-24 20:31:31 +00001069// Get the sdk version for use when compiling the stubs library.
Paul Duffin153501f2020-05-12 15:52:55 +01001070func (module *SdkLibrary) sdkVersionForStubsLibrary(mctx android.EarlyModuleContext, apiScope *apiScope) string {
Paul Duffin080f5ee2020-05-12 11:50:28 +01001071 scopeProperties := module.scopeToProperties[apiScope]
1072 if scopeProperties.Sdk_version != nil {
1073 return proptools.String(scopeProperties.Sdk_version)
1074 }
1075
Paul Duffin12ceb462019-12-24 20:31:31 +00001076 sdkDep := decodeSdkDep(mctx, sdkContext(&module.Library))
1077 if sdkDep.hasStandardLibs() {
1078 // If building against a standard sdk then use the sdk version appropriate for the scope.
Paul Duffind1b3a922020-01-22 11:57:20 +00001079 return apiScope.sdkVersion
Paul Duffin12ceb462019-12-24 20:31:31 +00001080 } else {
1081 // Otherwise, use no system module.
1082 return "none"
1083 }
1084}
1085
Paul Duffind1b3a922020-01-22 11:57:20 +00001086func (module *SdkLibrary) latestApiFilegroupName(apiScope *apiScope) string {
1087 return ":" + module.BaseModuleName() + ".api." + apiScope.name + ".latest"
Jiyong Park58c518b2018-05-12 22:29:12 +09001088}
Jiyong Parkc678ad32018-04-10 13:07:10 +09001089
Paul Duffind1b3a922020-01-22 11:57:20 +00001090func (module *SdkLibrary) latestRemovedApiFilegroupName(apiScope *apiScope) string {
1091 return ":" + module.BaseModuleName() + "-removed.api." + apiScope.name + ".latest"
Jiyong Parkc678ad32018-04-10 13:07:10 +09001092}
1093
Paul Duffin9d582cc2020-05-16 15:52:12 +01001094// Creates the implementation java library
1095func (module *SdkLibrary) createImplLibrary(mctx android.DefaultableHookContext) {
Paul Duffinc4422102020-06-24 16:22:38 +01001096
1097 moduleNamePtr := proptools.StringPtr(module.BaseModuleName())
1098
Paul Duffin9d582cc2020-05-16 15:52:12 +01001099 props := struct {
Paul Duffinc4422102020-06-24 16:22:38 +01001100 Name *string
1101 Visibility []string
1102 Instrument bool
1103 ConfigurationName *string
Paul Duffin9d582cc2020-05-16 15:52:12 +01001104 }{
1105 Name: proptools.StringPtr(module.implLibraryModuleName()),
1106 Visibility: module.sdkLibraryProperties.Impl_library_visibility,
Paul Duffin296cf332020-06-18 21:09:55 +01001107 // Set the instrument property to ensure it is instrumented when instrumentation is required.
1108 Instrument: true,
Paul Duffinc4422102020-06-24 16:22:38 +01001109
1110 // Make the created library behave as if it had the same name as this module.
1111 ConfigurationName: moduleNamePtr,
Paul Duffin9d582cc2020-05-16 15:52:12 +01001112 }
1113
1114 properties := []interface{}{
1115 &module.properties,
1116 &module.protoProperties,
1117 &module.deviceProperties,
1118 &module.dexpreoptProperties,
Colin Cross014489c2020-06-02 20:09:13 -07001119 &module.linter.properties,
Paul Duffin9d582cc2020-05-16 15:52:12 +01001120 &props,
1121 module.sdkComponentPropertiesForChildLibrary(),
1122 }
1123 mctx.CreateModule(LibraryFactory, properties...)
1124}
1125
Jiyong Parkc678ad32018-04-10 13:07:10 +09001126// Creates a static java library that has API stubs
Paul Duffin2aaef532020-04-29 16:47:28 +01001127func (module *SdkLibrary) createStubsLibrary(mctx android.DefaultableHookContext, apiScope *apiScope) {
Jiyong Parkc678ad32018-04-10 13:07:10 +09001128 props := struct {
Paul Duffind41712d2020-05-16 09:57:59 +01001129 Name *string
1130 Visibility []string
1131 Srcs []string
1132 Installable *bool
1133 Sdk_version *string
1134 System_modules *string
1135 Patch_module *string
1136 Libs []string
1137 Compile_dex *bool
1138 Java_version *string
1139 Product_variables struct {
Jiyong Park82484c02018-04-23 21:41:26 +09001140 Pdk struct {
1141 Enabled *bool
1142 }
Jiyong Parkc678ad32018-04-10 13:07:10 +09001143 }
Sundong Ahn054b19a2018-10-19 13:46:09 +09001144 Openjdk9 struct {
1145 Srcs []string
1146 Javacflags []string
1147 }
Anton Hansson6bb88102020-03-27 19:43:19 +00001148 Dist struct {
1149 Targets []string
1150 Dest *string
1151 Dir *string
1152 Tag *string
1153 }
Jiyong Parkc678ad32018-04-10 13:07:10 +09001154 }{}
1155
Paul Duffinb74ee3e2020-05-08 14:16:20 +01001156 props.Name = proptools.StringPtr(module.stubsLibraryModuleName(apiScope))
Paul Duffin344c4ee2020-04-29 23:35:13 +01001157
1158 // If stubs_library_visibility is not set then the created module will use the
1159 // visibility of this module.
1160 visibility := module.sdkLibraryProperties.Stubs_library_visibility
1161 props.Visibility = visibility
1162
Jiyong Parkc678ad32018-04-10 13:07:10 +09001163 // sources are generated from the droiddoc
Paul Duffinb74ee3e2020-05-08 14:16:20 +01001164 props.Srcs = []string{":" + module.stubsSourceModuleName(apiScope)}
Paul Duffin12ceb462019-12-24 20:31:31 +00001165 sdkVersion := module.sdkVersionForStubsLibrary(mctx, apiScope)
Paul Duffin52d398a2019-06-11 12:31:14 +01001166 props.Sdk_version = proptools.StringPtr(sdkVersion)
Paul Duffinc5d954a2020-05-16 18:54:24 +01001167 props.System_modules = module.deviceProperties.System_modules
1168 props.Patch_module = module.properties.Patch_module
Paul Duffin367ab912019-12-23 19:40:36 +00001169 props.Installable = proptools.BoolPtr(false)
Sundong Ahn054b19a2018-10-19 13:46:09 +09001170 props.Libs = module.sdkLibraryProperties.Stub_only_libs
Paul Duffin2ce1e812020-05-20 19:35:27 +01001171 // The stub-annotations library contains special versions of the annotations
1172 // with CLASS retention policy, so that they're kept.
1173 if proptools.Bool(module.sdkLibraryProperties.Annotations_enabled) {
1174 props.Libs = append(props.Libs, "stub-annotations")
1175 }
Jiyong Park82484c02018-04-23 21:41:26 +09001176 props.Product_variables.Pdk.Enabled = proptools.BoolPtr(false)
Paul Duffinc5d954a2020-05-16 18:54:24 +01001177 props.Openjdk9.Srcs = module.properties.Openjdk9.Srcs
1178 props.Openjdk9.Javacflags = module.properties.Openjdk9.Javacflags
Anton Hanssoncf4dd4c2020-05-21 09:21:57 +01001179 // We compile the stubs for 1.8 in line with the main android.jar stubs, and potential
1180 // interop with older developer tools that don't support 1.9.
1181 props.Java_version = proptools.StringPtr("1.8")
Paul Duffinc5d954a2020-05-16 18:54:24 +01001182 if module.deviceProperties.Compile_dex != nil {
1183 props.Compile_dex = module.deviceProperties.Compile_dex
Sundong Ahndd567f92018-07-31 17:19:11 +09001184 }
Jiyong Parkc678ad32018-04-10 13:07:10 +09001185
Anton Hansson6bb88102020-03-27 19:43:19 +00001186 // Dist the class jar artifact for sdk builds.
1187 if !Bool(module.sdkLibraryProperties.No_dist) {
1188 props.Dist.Targets = []string{"sdk", "win_sdk"}
1189 props.Dist.Dest = proptools.StringPtr(fmt.Sprintf("%v.jar", module.BaseModuleName()))
1190 props.Dist.Dir = proptools.StringPtr(module.apiDistPath(apiScope))
1191 props.Dist.Tag = proptools.StringPtr(".jar")
1192 }
Jiyong Parkc678ad32018-04-10 13:07:10 +09001193
Paul Duffin64e61992020-05-15 10:20:31 +01001194 mctx.CreateModule(LibraryFactory, &props, module.sdkComponentPropertiesForChildLibrary())
Jiyong Parkc678ad32018-04-10 13:07:10 +09001195}
1196
Paul Duffincbcfcaa2020-04-07 18:49:53 +01001197// Creates a droidstubs module that creates stubs source files from the given full source
Paul Duffin5fb82132020-04-29 20:45:27 +01001198// files and also updates and checks the API specification files.
Paul Duffin15f34ef2020-07-20 18:04:44 +01001199func (module *SdkLibrary) createStubsSourcesAndApi(mctx android.DefaultableHookContext, apiScope *apiScope, name string, scopeSpecificDroidstubsArgs []string) {
Jiyong Parkc678ad32018-04-10 13:07:10 +09001200 props := struct {
Sundong Ahn054b19a2018-10-19 13:46:09 +09001201 Name *string
Paul Duffin344c4ee2020-04-29 23:35:13 +01001202 Visibility []string
Sundong Ahn054b19a2018-10-19 13:46:09 +09001203 Srcs []string
1204 Installable *bool
Paul Duffin52d398a2019-06-11 12:31:14 +01001205 Sdk_version *string
Paul Duffin12ceb462019-12-24 20:31:31 +00001206 System_modules *string
Sundong Ahn054b19a2018-10-19 13:46:09 +09001207 Libs []string
Paul Duffin11512472019-02-11 15:55:17 +00001208 Arg_files []string
Sundong Ahn054b19a2018-10-19 13:46:09 +09001209 Args *string
Sundong Ahn054b19a2018-10-19 13:46:09 +09001210 Java_version *string
Paul Duffin2ce1e812020-05-20 19:35:27 +01001211 Annotations_enabled *bool
Sundong Ahn054b19a2018-10-19 13:46:09 +09001212 Merge_annotations_dirs []string
1213 Merge_inclusion_annotations_dirs []string
Paul Duffina377e4c2020-04-29 13:30:54 +01001214 Generate_stubs *bool
Sundong Ahn054b19a2018-10-19 13:46:09 +09001215 Check_api struct {
Inseob Kim38449af2019-02-28 14:24:05 +09001216 Current ApiToCheck
1217 Last_released ApiToCheck
1218 Ignore_missing_latest_api *bool
Paul Duffin8986cc92020-05-10 19:32:20 +01001219
1220 Api_lint struct {
1221 Enabled *bool
1222 New_since *string
1223 Baseline_file *string
1224 }
Jiyong Park58c518b2018-05-12 22:29:12 +09001225 }
Sundong Ahn1b92c822018-05-29 11:35:17 +09001226 Aidl struct {
1227 Include_dirs []string
1228 Local_include_dirs []string
1229 }
Anton Hansson6bb88102020-03-27 19:43:19 +00001230 Dist struct {
1231 Targets []string
1232 Dest *string
1233 Dir *string
1234 }
Jiyong Parkc678ad32018-04-10 13:07:10 +09001235 }{}
1236
Paul Duffinda364252020-04-28 14:08:32 +01001237 // The stubs source processing uses the same compile time classpath when extracting the
1238 // API from the implementation library as it does when compiling it. i.e. the same
1239 // * sdk version
1240 // * system_modules
1241 // * libs (static_libs/libs)
Paul Duffin250e6192019-06-07 10:44:37 +01001242
Paul Duffina377e4c2020-04-29 13:30:54 +01001243 props.Name = proptools.StringPtr(name)
Paul Duffin344c4ee2020-04-29 23:35:13 +01001244
1245 // If stubs_source_visibility is not set then the created module will use the
1246 // visibility of this module.
1247 visibility := module.sdkLibraryProperties.Stubs_source_visibility
1248 props.Visibility = visibility
1249
Paul Duffinc5d954a2020-05-16 18:54:24 +01001250 props.Srcs = append(props.Srcs, module.properties.Srcs...)
1251 props.Sdk_version = module.deviceProperties.Sdk_version
1252 props.System_modules = module.deviceProperties.System_modules
Jiyong Parkc678ad32018-04-10 13:07:10 +09001253 props.Installable = proptools.BoolPtr(false)
Sundong Ahne6f0b052018-06-05 16:46:14 +09001254 // A droiddoc module has only one Libs property and doesn't distinguish between
1255 // shared libs and static libs. So we need to add both of these libs to Libs property.
Paul Duffinc5d954a2020-05-16 18:54:24 +01001256 props.Libs = module.properties.Libs
1257 props.Libs = append(props.Libs, module.properties.Static_libs...)
1258 props.Aidl.Include_dirs = module.deviceProperties.Aidl.Include_dirs
1259 props.Aidl.Local_include_dirs = module.deviceProperties.Aidl.Local_include_dirs
1260 props.Java_version = module.properties.Java_version
Jiyong Parkc678ad32018-04-10 13:07:10 +09001261
Paul Duffin2ce1e812020-05-20 19:35:27 +01001262 props.Annotations_enabled = module.sdkLibraryProperties.Annotations_enabled
Sundong Ahn054b19a2018-10-19 13:46:09 +09001263 props.Merge_annotations_dirs = module.sdkLibraryProperties.Merge_annotations_dirs
1264 props.Merge_inclusion_annotations_dirs = module.sdkLibraryProperties.Merge_inclusion_annotations_dirs
1265
Paul Duffincbcfcaa2020-04-07 18:49:53 +01001266 droidstubsArgs := []string{}
Paul Duffin235ffff2019-12-24 10:41:30 +00001267 if len(module.sdkLibraryProperties.Api_packages) != 0 {
Paul Duffincbcfcaa2020-04-07 18:49:53 +01001268 droidstubsArgs = append(droidstubsArgs, "--stub-packages "+strings.Join(module.sdkLibraryProperties.Api_packages, ":"))
Paul Duffin235ffff2019-12-24 10:41:30 +00001269 }
1270 if len(module.sdkLibraryProperties.Hidden_api_packages) != 0 {
Paul Duffincbcfcaa2020-04-07 18:49:53 +01001271 droidstubsArgs = append(droidstubsArgs,
Paul Duffin235ffff2019-12-24 10:41:30 +00001272 android.JoinWithPrefix(module.sdkLibraryProperties.Hidden_api_packages, " --hide-package "))
1273 }
Paul Duffincbcfcaa2020-04-07 18:49:53 +01001274 droidstubsArgs = append(droidstubsArgs, module.sdkLibraryProperties.Droiddoc_options...)
Paul Duffin235ffff2019-12-24 10:41:30 +00001275 disabledWarnings := []string{
1276 "MissingPermission",
1277 "BroadcastBehavior",
1278 "HiddenSuperclass",
1279 "DeprecationMismatch",
1280 "UnavailableSymbol",
1281 "SdkConstant",
1282 "HiddenTypeParameter",
1283 "Todo",
1284 "Typo",
1285 }
Paul Duffincbcfcaa2020-04-07 18:49:53 +01001286 droidstubsArgs = append(droidstubsArgs, android.JoinWithPrefix(disabledWarnings, "--hide "))
Sundong Ahnfb2721f2018-09-17 13:23:09 +09001287
Paul Duffin3c7c3472020-04-07 18:50:10 +01001288 // Add in scope specific arguments.
Paul Duffina377e4c2020-04-29 13:30:54 +01001289 droidstubsArgs = append(droidstubsArgs, scopeSpecificDroidstubsArgs...)
Paul Duffin11512472019-02-11 15:55:17 +00001290 props.Arg_files = module.sdkLibraryProperties.Droiddoc_option_files
Paul Duffincbcfcaa2020-04-07 18:49:53 +01001291 props.Args = proptools.StringPtr(strings.Join(droidstubsArgs, " "))
Jiyong Parkc678ad32018-04-10 13:07:10 +09001292
Paul Duffin15f34ef2020-07-20 18:04:44 +01001293 // List of APIs identified from the provided source files are created. They are later
1294 // compared against to the not-yet-released (a.k.a current) list of APIs and to the
1295 // last-released (a.k.a numbered) list of API.
1296 currentApiFileName := apiScope.apiFilePrefix + "current.txt"
1297 removedApiFileName := apiScope.apiFilePrefix + "removed.txt"
1298 apiDir := module.getApiDir()
1299 currentApiFileName = path.Join(apiDir, currentApiFileName)
1300 removedApiFileName = path.Join(apiDir, removedApiFileName)
Jiyong Parkc678ad32018-04-10 13:07:10 +09001301
Paul Duffin15f34ef2020-07-20 18:04:44 +01001302 // check against the not-yet-release API
1303 props.Check_api.Current.Api_file = proptools.StringPtr(currentApiFileName)
1304 props.Check_api.Current.Removed_api_file = proptools.StringPtr(removedApiFileName)
Jiyong Park58c518b2018-05-12 22:29:12 +09001305
Paul Duffin15f34ef2020-07-20 18:04:44 +01001306 if !apiScope.unstable {
1307 // check against the latest released API
1308 latestApiFilegroupName := proptools.StringPtr(module.latestApiFilegroupName(apiScope))
1309 props.Check_api.Last_released.Api_file = latestApiFilegroupName
1310 props.Check_api.Last_released.Removed_api_file = proptools.StringPtr(
1311 module.latestRemovedApiFilegroupName(apiScope))
1312 props.Check_api.Ignore_missing_latest_api = proptools.BoolPtr(true)
Paul Duffin8986cc92020-05-10 19:32:20 +01001313
Paul Duffin15f34ef2020-07-20 18:04:44 +01001314 if proptools.Bool(module.sdkLibraryProperties.Api_lint.Enabled) {
1315 // Enable api lint.
1316 props.Check_api.Api_lint.Enabled = proptools.BoolPtr(true)
1317 props.Check_api.Api_lint.New_since = latestApiFilegroupName
Paul Duffin8986cc92020-05-10 19:32:20 +01001318
Paul Duffin15f34ef2020-07-20 18:04:44 +01001319 // If it exists then pass a lint-baseline.txt through to droidstubs.
1320 baselinePath := path.Join(apiDir, apiScope.apiFilePrefix+"lint-baseline.txt")
1321 baselinePathRelativeToRoot := path.Join(mctx.ModuleDir(), baselinePath)
1322 paths, err := mctx.GlobWithDeps(baselinePathRelativeToRoot, nil)
1323 if err != nil {
1324 mctx.ModuleErrorf("error checking for presence of %s: %s", baselinePathRelativeToRoot, err)
1325 }
1326 if len(paths) == 1 {
1327 props.Check_api.Api_lint.Baseline_file = proptools.StringPtr(baselinePath)
1328 } else if len(paths) != 0 {
1329 mctx.ModuleErrorf("error checking for presence of %s: expected one path, found: %v", baselinePathRelativeToRoot, paths)
Paul Duffin8986cc92020-05-10 19:32:20 +01001330 }
1331 }
Paul Duffin15f34ef2020-07-20 18:04:44 +01001332 }
Jiyong Park58c518b2018-05-12 22:29:12 +09001333
Paul Duffin15f34ef2020-07-20 18:04:44 +01001334 // Dist the api txt artifact for sdk builds.
1335 if !Bool(module.sdkLibraryProperties.No_dist) {
1336 props.Dist.Targets = []string{"sdk", "win_sdk"}
1337 props.Dist.Dest = proptools.StringPtr(fmt.Sprintf("%v.txt", module.BaseModuleName()))
1338 props.Dist.Dir = proptools.StringPtr(path.Join(module.apiDistPath(apiScope), "api"))
Anton Hansson6bb88102020-03-27 19:43:19 +00001339 }
1340
Colin Cross84dfc3d2019-09-25 11:33:01 -07001341 mctx.CreateModule(DroidstubsFactory, &props)
Jiyong Parkc678ad32018-04-10 13:07:10 +09001342}
1343
Jooyung Hanb8fa86a2020-03-10 06:23:13 +09001344func (module *SdkLibrary) DepIsInSameApex(mctx android.BaseModuleContext, dep android.Module) bool {
1345 depTag := mctx.OtherModuleDependencyTag(dep)
1346 if depTag == xmlPermissionsFileTag {
1347 return true
1348 }
1349 return module.Library.DepIsInSameApex(mctx, dep)
1350}
1351
Jiyong Parkc678ad32018-04-10 13:07:10 +09001352// Creates the xml file that publicizes the runtime library
Paul Duffin2aaef532020-04-29 16:47:28 +01001353func (module *SdkLibrary) createXmlFile(mctx android.DefaultableHookContext) {
Jiyong Parke3833882020-02-17 17:28:10 +09001354 props := struct {
Paul Duffind41712d2020-05-16 09:57:59 +01001355 Name *string
1356 Lib_name *string
1357 Apex_available []string
Jiyong Parke3833882020-02-17 17:28:10 +09001358 }{
Paul Duffineedc5d52020-06-12 17:46:39 +01001359 Name: proptools.StringPtr(module.xmlPermissionsModuleName()),
Jooyung Hanb8fa86a2020-03-10 06:23:13 +09001360 Lib_name: proptools.StringPtr(module.BaseModuleName()),
1361 Apex_available: module.ApexProperties.Apex_available,
Jiyong Parkc678ad32018-04-10 13:07:10 +09001362 }
Jiyong Parke3833882020-02-17 17:28:10 +09001363
Jiyong Parke3833882020-02-17 17:28:10 +09001364 mctx.CreateModule(sdkLibraryXmlFactory, &props)
Jiyong Parkc678ad32018-04-10 13:07:10 +09001365}
1366
Paul Duffin50061512020-01-21 16:31:05 +00001367func PrebuiltJars(ctx android.BaseModuleContext, baseName string, s sdkSpec) android.Paths {
Jiyong Park6a927c42020-01-21 02:03:43 +09001368 var ver sdkVersion
1369 var kind sdkKind
1370 if s.usePrebuilt(ctx) {
1371 ver = s.version
1372 kind = s.kind
Jiyong Parkc678ad32018-04-10 13:07:10 +09001373 } else {
Jiyong Park6a927c42020-01-21 02:03:43 +09001374 // We don't have prebuilt SDK for the specific sdkVersion.
1375 // Instead of breaking the build, fallback to use "system_current"
1376 ver = sdkVersionCurrent
1377 kind = sdkSystem
Sundong Ahn054b19a2018-10-19 13:46:09 +09001378 }
Jiyong Park6a927c42020-01-21 02:03:43 +09001379
1380 dir := filepath.Join("prebuilts", "sdk", ver.String(), kind.String())
Paul Duffin50061512020-01-21 16:31:05 +00001381 jar := filepath.Join(dir, baseName+".jar")
Sundong Ahn054b19a2018-10-19 13:46:09 +09001382 jarPath := android.ExistentPathForSource(ctx, jar)
Sundong Ahnae418ac2019-02-28 15:01:28 +09001383 if !jarPath.Valid() {
Colin Cross07c88562020-01-07 09:34:44 -08001384 if ctx.Config().AllowMissingDependencies() {
1385 return android.Paths{android.PathForSource(ctx, jar)}
1386 } else {
Jiyong Park6a927c42020-01-21 02:03:43 +09001387 ctx.PropertyErrorf("sdk_library", "invalid sdk version %q, %q does not exist", s.raw, jar)
Colin Cross07c88562020-01-07 09:34:44 -08001388 }
Sundong Ahnae418ac2019-02-28 15:01:28 +09001389 return nil
1390 }
Sundong Ahn054b19a2018-10-19 13:46:09 +09001391 return android.Paths{jarPath.Path()}
1392}
1393
Paul Duffinbf19a972020-05-26 13:21:35 +01001394// Get the apex name for module, "" if it is for platform.
1395func getApexNameForModule(module android.Module) string {
1396 if apex, ok := module.(android.ApexModule); ok {
1397 return apex.ApexName()
1398 }
1399
1400 return ""
1401}
1402
1403// Check to see if the other module is within the same named APEX as this module.
1404//
1405// If either this or the other module are on the platform then this will return
1406// false.
Paul Duffineedc5d52020-06-12 17:46:39 +01001407func withinSameApexAs(module android.ApexModule, other android.Module) bool {
Paul Duffinbf19a972020-05-26 13:21:35 +01001408 name := module.ApexName()
1409 return name != "" && getApexNameForModule(other) == name
1410}
1411
Paul Duffin47624362020-05-20 12:19:10 +01001412func (module *SdkLibrary) sdkJars(ctx android.BaseModuleContext, sdkVersion sdkSpec, headerJars bool) android.Paths {
Jiyong Park27fc4142020-05-28 00:19:53 +09001413 // If the client doesn't set sdk_version, but if this library prefers stubs over
1414 // the impl library, let's provide the widest API surface possible. To do so,
1415 // force override sdk_version to module_current so that the closest possible API
1416 // surface could be found in selectHeaderJarsForSdkVersion
1417 if module.defaultsToStubs() && !sdkVersion.specified() {
1418 sdkVersion = sdkSpecFrom("module_current")
1419 }
Paul Duffind1b3a922020-01-22 11:57:20 +00001420
Paul Duffin2e7ed652020-05-26 18:13:57 +01001421 // Only provide access to the implementation library if it is actually built.
1422 if module.requiresRuntimeImplementationLibrary() {
1423 // Check any special cases for java_sdk_library.
1424 //
1425 // Only allow access to the implementation library in the following condition:
1426 // * No sdk_version specified on the referencing module.
Paul Duffinbf19a972020-05-26 13:21:35 +01001427 // * The referencing module is in the same apex as this.
Paul Duffineedc5d52020-06-12 17:46:39 +01001428 if sdkVersion.kind == sdkPrivate || withinSameApexAs(module, ctx.Module()) {
Paul Duffin2e7ed652020-05-26 18:13:57 +01001429 if headerJars {
1430 return module.HeaderJars()
1431 } else {
1432 return module.ImplementationJars()
1433 }
Sundong Ahn054b19a2018-10-19 13:46:09 +09001434 }
Jiyong Parkc678ad32018-04-10 13:07:10 +09001435 }
Paul Duffin47624362020-05-20 12:19:10 +01001436
Paul Duffina3fb67d2020-05-20 14:20:02 +01001437 return module.selectHeaderJarsForSdkVersion(ctx, sdkVersion)
Jiyong Parkc678ad32018-04-10 13:07:10 +09001438}
1439
Sundong Ahn241cd372018-07-13 16:16:44 +09001440// to satisfy SdkLibraryDependency interface
Paul Duffind1b3a922020-01-22 11:57:20 +00001441func (module *SdkLibrary) SdkHeaderJars(ctx android.BaseModuleContext, sdkVersion sdkSpec) android.Paths {
1442 return module.sdkJars(ctx, sdkVersion, true /*headerJars*/)
1443}
1444
1445// to satisfy SdkLibraryDependency interface
Jiyong Park6a927c42020-01-21 02:03:43 +09001446func (module *SdkLibrary) SdkImplementationJars(ctx android.BaseModuleContext, sdkVersion sdkSpec) android.Paths {
Paul Duffind1b3a922020-01-22 11:57:20 +00001447 return module.sdkJars(ctx, sdkVersion, false /*headerJars*/)
Sundong Ahn241cd372018-07-13 16:16:44 +09001448}
1449
Sundong Ahn80a87b32019-05-13 15:02:50 +09001450func (module *SdkLibrary) SetNoDist() {
1451 module.sdkLibraryProperties.No_dist = proptools.BoolPtr(true)
1452}
1453
Colin Cross571cccf2019-02-04 11:22:08 -08001454var javaSdkLibrariesKey = android.NewOnceKey("javaSdkLibraries")
1455
Jiyong Park82484c02018-04-23 21:41:26 +09001456func javaSdkLibraries(config android.Config) *[]string {
Colin Cross571cccf2019-02-04 11:22:08 -08001457 return config.Once(javaSdkLibrariesKey, func() interface{} {
Jiyong Park82484c02018-04-23 21:41:26 +09001458 return &[]string{}
1459 }).(*[]string)
1460}
1461
Paul Duffin749f98f2019-12-30 17:23:46 +00001462func (module *SdkLibrary) getApiDir() string {
1463 return proptools.StringDefault(module.sdkLibraryProperties.Api_dir, "api")
1464}
1465
Jiyong Parkc678ad32018-04-10 13:07:10 +09001466// For a java_sdk_library module, create internal modules for stubs, docs,
1467// runtime libs and xml file. If requested, the stubs and docs are created twice
1468// once for public API level and once for system API level
Paul Duffin2aaef532020-04-29 16:47:28 +01001469func (module *SdkLibrary) CreateInternalModules(mctx android.DefaultableHookContext) {
1470 // If the module has been disabled then don't create any child modules.
1471 if !module.Enabled() {
1472 return
1473 }
1474
Paul Duffinc5d954a2020-05-16 18:54:24 +01001475 if len(module.properties.Srcs) == 0 {
Inseob Kimc0907f12019-02-08 21:00:45 +09001476 mctx.PropertyErrorf("srcs", "java_sdk_library must specify srcs")
Jooyung Han58f26ab2019-12-18 15:34:32 +09001477 return
Inseob Kimc0907f12019-02-08 21:00:45 +09001478 }
1479
Paul Duffin37e0b772019-12-30 17:20:10 +00001480 // If this builds against standard libraries (i.e. is not part of the core libraries)
1481 // then assume it provides both system and test apis. Otherwise, assume it does not and
1482 // also assume it does not contribute to the dist build.
1483 sdkDep := decodeSdkDep(mctx, sdkContext(&module.Library))
1484 hasSystemAndTestApis := sdkDep.hasStandardLibs()
Paul Duffin3a254982020-04-28 10:44:03 +01001485 module.sdkLibraryProperties.Generate_system_and_test_apis = hasSystemAndTestApis
Paul Duffin37e0b772019-12-30 17:20:10 +00001486 module.sdkLibraryProperties.No_dist = proptools.BoolPtr(!hasSystemAndTestApis)
1487
Inseob Kim8098faa2019-03-18 10:19:51 +09001488 missing_current_api := false
1489
Paul Duffin3a254982020-04-28 10:44:03 +01001490 generatedScopes := module.getGeneratedApiScopes(mctx)
Paul Duffind1b3a922020-01-22 11:57:20 +00001491
Paul Duffin749f98f2019-12-30 17:23:46 +00001492 apiDir := module.getApiDir()
Paul Duffin3a254982020-04-28 10:44:03 +01001493 for _, scope := range generatedScopes {
Inseob Kim8098faa2019-03-18 10:19:51 +09001494 for _, api := range []string{"current.txt", "removed.txt"} {
Paul Duffind1b3a922020-01-22 11:57:20 +00001495 path := path.Join(mctx.ModuleDir(), apiDir, scope.apiFilePrefix+api)
Inseob Kim8098faa2019-03-18 10:19:51 +09001496 p := android.ExistentPathForSource(mctx, path)
1497 if !p.Valid() {
1498 mctx.ModuleErrorf("Current api file %#v doesn't exist", path)
1499 missing_current_api = true
1500 }
1501 }
1502 }
1503
1504 if missing_current_api {
1505 script := "build/soong/scripts/gen-java-current-api-files.sh"
1506 p := android.ExistentPathForSource(mctx, script)
1507
1508 if !p.Valid() {
1509 panic(fmt.Sprintf("script file %s doesn't exist", script))
1510 }
1511
1512 mctx.ModuleErrorf("One or more current api files are missing. "+
1513 "You can update them by:\n"+
Paul Duffin37e0b772019-12-30 17:20:10 +00001514 "%s %q %s && m update-api",
Paul Duffind1b3a922020-01-22 11:57:20 +00001515 script, filepath.Join(mctx.ModuleDir(), apiDir),
Paul Duffin3a254982020-04-28 10:44:03 +01001516 strings.Join(generatedScopes.Strings(func(s *apiScope) string { return s.apiFilePrefix }), " "))
Inseob Kim8098faa2019-03-18 10:19:51 +09001517 return
1518 }
1519
Paul Duffin3a254982020-04-28 10:44:03 +01001520 for _, scope := range generatedScopes {
Paul Duffin15f34ef2020-07-20 18:04:44 +01001521 // Use the stubs source name for legacy reasons.
1522 module.createStubsSourcesAndApi(mctx, scope, module.stubsSourceModuleName(scope), scope.droidstubsArgs)
Paul Duffina377e4c2020-04-29 13:30:54 +01001523
Paul Duffind1b3a922020-01-22 11:57:20 +00001524 module.createStubsLibrary(mctx, scope)
Inseob Kimc0907f12019-02-08 21:00:45 +09001525 }
1526
Paul Duffind11e78e2020-05-15 20:37:11 +01001527 if module.requiresRuntimeImplementationLibrary() {
Paul Duffin9d582cc2020-05-16 15:52:12 +01001528 // Create child module to create an implementation library.
1529 //
1530 // This temporarily creates a second implementation library that can be explicitly
1531 // referenced.
1532 //
1533 // TODO(b/156618935) - update comment once only one implementation library is created.
1534 module.createImplLibrary(mctx)
1535
Paul Duffind11e78e2020-05-15 20:37:11 +01001536 // Only create an XML permissions file that declares the library as being usable
1537 // as a shared library if required.
1538 if module.sharedLibrary() {
1539 module.createXmlFile(mctx)
1540 }
Paul Duffin43db9be2019-12-30 17:35:49 +00001541
1542 // record java_sdk_library modules so that they are exported to make
1543 javaSdkLibraries := javaSdkLibraries(mctx.Config())
1544 javaSdkLibrariesLock.Lock()
1545 defer javaSdkLibrariesLock.Unlock()
1546 *javaSdkLibraries = append(*javaSdkLibraries, module.BaseModuleName())
1547 }
Inseob Kimc0907f12019-02-08 21:00:45 +09001548}
1549
1550func (module *SdkLibrary) InitSdkLibraryProperties() {
Colin Crossce6734e2020-06-15 16:09:53 -07001551 module.addHostAndDeviceProperties()
1552 module.AddProperties(&module.sdkLibraryProperties)
Sundong Ahn054b19a2018-10-19 13:46:09 +09001553
Paul Duffin64e61992020-05-15 10:20:31 +01001554 module.initSdkLibraryComponent(&module.ModuleBase)
1555
Paul Duffinc5d954a2020-05-16 18:54:24 +01001556 module.properties.Installable = proptools.BoolPtr(true)
1557 module.deviceProperties.IsSDKLibrary = true
Inseob Kimc0907f12019-02-08 21:00:45 +09001558}
Sundong Ahn054b19a2018-10-19 13:46:09 +09001559
Paul Duffind11e78e2020-05-15 20:37:11 +01001560func (module *SdkLibrary) requiresRuntimeImplementationLibrary() bool {
1561 return !proptools.Bool(module.sdkLibraryProperties.Api_only)
1562}
1563
Jiyong Park27fc4142020-05-28 00:19:53 +09001564func (module *SdkLibrary) defaultsToStubs() bool {
1565 return proptools.Bool(module.sdkLibraryProperties.Default_to_stubs)
1566}
1567
Paul Duffin1a724e62020-05-08 13:44:43 +01001568// Defines how to name the individual component modules the sdk library creates.
1569type sdkLibraryComponentNamingScheme interface {
1570 stubsLibraryModuleName(scope *apiScope, baseName string) string
1571
1572 stubsSourceModuleName(scope *apiScope, baseName string) string
1573
1574 apiModuleName(scope *apiScope, baseName string) string
1575}
1576
1577type defaultNamingScheme struct {
1578}
1579
1580func (s *defaultNamingScheme) stubsLibraryModuleName(scope *apiScope, baseName string) string {
1581 return scope.stubsLibraryModuleName(baseName)
1582}
1583
1584func (s *defaultNamingScheme) stubsSourceModuleName(scope *apiScope, baseName string) string {
1585 return scope.stubsSourceModuleName(baseName)
1586}
1587
1588func (s *defaultNamingScheme) apiModuleName(scope *apiScope, baseName string) string {
1589 return scope.apiModuleName(baseName)
1590}
1591
1592var _ sdkLibraryComponentNamingScheme = (*defaultNamingScheme)(nil)
1593
Paul Duffindef8a892020-05-08 15:36:30 +01001594type frameworkModulesNamingScheme struct {
1595}
1596
1597func (s *frameworkModulesNamingScheme) moduleSuffix(scope *apiScope) string {
1598 suffix := scope.name
1599 if scope == apiScopeModuleLib {
1600 suffix = "module_libs_"
1601 }
1602 return suffix
1603}
1604
1605func (s *frameworkModulesNamingScheme) stubsLibraryModuleName(scope *apiScope, baseName string) string {
1606 return fmt.Sprintf("%s-stubs-%sapi", baseName, s.moduleSuffix(scope))
1607}
1608
1609func (s *frameworkModulesNamingScheme) stubsSourceModuleName(scope *apiScope, baseName string) string {
1610 return fmt.Sprintf("%s-stubs-srcs-%sapi", baseName, s.moduleSuffix(scope))
1611}
1612
1613func (s *frameworkModulesNamingScheme) apiModuleName(scope *apiScope, baseName string) string {
1614 return fmt.Sprintf("%s-api-%sapi", baseName, s.moduleSuffix(scope))
1615}
1616
1617var _ sdkLibraryComponentNamingScheme = (*frameworkModulesNamingScheme)(nil)
1618
Anton Hansson0bd88d02020-05-25 12:20:51 +01001619func moduleStubLinkType(name string) (stub bool, ret linkType) {
1620 // This suffix-based approach is fragile and could potentially mis-trigger.
1621 // TODO(b/155164730): Clean this up when modules no longer reference sdk_lib stubs directly.
1622 if strings.HasSuffix(name, ".stubs.public") || strings.HasSuffix(name, "-stubs-publicapi") {
1623 return true, javaSdk
1624 }
1625 if strings.HasSuffix(name, ".stubs.system") || strings.HasSuffix(name, "-stubs-systemapi") {
1626 return true, javaSystem
1627 }
1628 if strings.HasSuffix(name, ".stubs.module_lib") || strings.HasSuffix(name, "-stubs-module_libs_api") {
1629 return true, javaModule
1630 }
1631 if strings.HasSuffix(name, ".stubs.test") {
1632 return true, javaSystem
1633 }
1634 return false, javaPlatform
1635}
1636
Jaewoong Jung4f158ee2019-07-11 10:05:35 -07001637// java_sdk_library is a special Java library that provides optional platform APIs to apps.
1638// In practice, it can be viewed as a combination of several modules: 1) stubs library that clients
1639// are linked against to, 2) droiddoc module that internally generates API stubs source files,
1640// 3) the real runtime shared library that implements the APIs, and 4) XML file for adding
1641// the runtime lib to the classpath at runtime if requested via <uses-library>.
Inseob Kimc0907f12019-02-08 21:00:45 +09001642func SdkLibraryFactory() android.Module {
1643 module := &SdkLibrary{}
Paul Duffinb74ee3e2020-05-08 14:16:20 +01001644
1645 // Initialize information common between source and prebuilt.
1646 module.initCommon(&module.ModuleBase)
1647
Inseob Kimc0907f12019-02-08 21:00:45 +09001648 module.InitSdkLibraryProperties()
Jooyung Han58f26ab2019-12-18 15:34:32 +09001649 android.InitApexModule(module)
Sundong Ahn054b19a2018-10-19 13:46:09 +09001650 InitJavaModule(module, android.HostAndDeviceSupported)
Paul Duffin3a254982020-04-28 10:44:03 +01001651
1652 // Initialize the map from scope to scope specific properties.
1653 scopeToProperties := make(map[*apiScope]*ApiScopeProperties)
1654 for _, scope := range allApiScopes {
1655 scopeToProperties[scope] = scope.scopeSpecificProperties(module)
1656 }
1657 module.scopeToProperties = scopeToProperties
1658
Paul Duffin344c4ee2020-04-29 23:35:13 +01001659 // Add the properties containing visibility rules so that they are checked.
Paul Duffin9d582cc2020-05-16 15:52:12 +01001660 android.AddVisibilityProperty(module, "impl_library_visibility", &module.sdkLibraryProperties.Impl_library_visibility)
Paul Duffin344c4ee2020-04-29 23:35:13 +01001661 android.AddVisibilityProperty(module, "stubs_library_visibility", &module.sdkLibraryProperties.Stubs_library_visibility)
1662 android.AddVisibilityProperty(module, "stubs_source_visibility", &module.sdkLibraryProperties.Stubs_source_visibility)
1663
Paul Duffin1a724e62020-05-08 13:44:43 +01001664 module.SetDefaultableHook(func(ctx android.DefaultableHookContext) {
Paul Duffind11e78e2020-05-15 20:37:11 +01001665 // If no implementation is required then it cannot be used as a shared library
1666 // either.
1667 if !module.requiresRuntimeImplementationLibrary() {
1668 // If shared_library has been explicitly set to true then it is incompatible
1669 // with api_only: true.
1670 if proptools.Bool(module.commonSdkLibraryProperties.Shared_library) {
1671 ctx.PropertyErrorf("api_only/shared_library", "inconsistent settings, shared_library and api_only cannot both be true")
1672 }
1673 // Set shared_library: false.
1674 module.commonSdkLibraryProperties.Shared_library = proptools.BoolPtr(false)
1675 }
1676
Paul Duffin1a724e62020-05-08 13:44:43 +01001677 if module.initCommonAfterDefaultsApplied(ctx) {
1678 module.CreateInternalModules(ctx)
1679 }
1680 })
Jiyong Parkc678ad32018-04-10 13:07:10 +09001681 return module
1682}
Colin Cross79c7c262019-04-17 11:11:46 -07001683
1684//
1685// SDK library prebuilts
1686//
1687
Paul Duffin56d44902020-01-31 13:36:25 +00001688// Properties associated with each api scope.
1689type sdkLibraryScopeProperties struct {
Colin Cross79c7c262019-04-17 11:11:46 -07001690 Jars []string `android:"path"`
1691
1692 Sdk_version *string
1693
Colin Cross79c7c262019-04-17 11:11:46 -07001694 // List of shared java libs that this module has dependencies to
1695 Libs []string
Paul Duffinf488ef22020-04-09 00:10:17 +01001696
Paul Duffin5fb82132020-04-29 20:45:27 +01001697 // The stubs source.
Paul Duffinf488ef22020-04-09 00:10:17 +01001698 Stub_srcs []string `android:"path"`
Paul Duffin75dcc802020-04-09 01:08:11 +01001699
1700 // The current.txt
Paul Duffin533f9c72020-05-20 16:18:00 +01001701 Current_api *string `android:"path"`
Paul Duffin75dcc802020-04-09 01:08:11 +01001702
1703 // The removed.txt
Paul Duffin533f9c72020-05-20 16:18:00 +01001704 Removed_api *string `android:"path"`
Colin Cross79c7c262019-04-17 11:11:46 -07001705}
1706
Paul Duffin56d44902020-01-31 13:36:25 +00001707type sdkLibraryImportProperties struct {
Paul Duffinfcfd7912020-01-31 17:54:30 +00001708 // List of shared java libs, common to all scopes, that this module has
1709 // dependencies to
1710 Libs []string
Paul Duffin56d44902020-01-31 13:36:25 +00001711}
1712
Paul Duffineedc5d52020-06-12 17:46:39 +01001713type SdkLibraryImport struct {
Colin Cross79c7c262019-04-17 11:11:46 -07001714 android.ModuleBase
1715 android.DefaultableModuleBase
1716 prebuilt android.Prebuilt
Paul Duffin61871622020-02-10 13:37:10 +00001717 android.ApexModuleBase
1718 android.SdkBase
Colin Cross79c7c262019-04-17 11:11:46 -07001719
1720 properties sdkLibraryImportProperties
1721
Paul Duffin6a2bd112020-04-07 19:27:04 +01001722 // Map from api scope to the scope specific property structure.
1723 scopeProperties map[*apiScope]*sdkLibraryScopeProperties
1724
Paul Duffin56d44902020-01-31 13:36:25 +00001725 commonToSdkLibraryAndImport
Paul Duffineedc5d52020-06-12 17:46:39 +01001726
1727 // The reference to the implementation library created by the source module.
1728 // Is nil if the source module does not exist.
1729 implLibraryModule *Library
1730
1731 // The reference to the xml permissions module created by the source module.
1732 // Is nil if the source module does not exist.
1733 xmlPermissionsFileModule *sdkLibraryXml
Colin Cross79c7c262019-04-17 11:11:46 -07001734}
1735
Paul Duffineedc5d52020-06-12 17:46:39 +01001736var _ SdkLibraryDependency = (*SdkLibraryImport)(nil)
Colin Cross79c7c262019-04-17 11:11:46 -07001737
Paul Duffin6a2bd112020-04-07 19:27:04 +01001738// The type of a structure that contains a field of type sdkLibraryScopeProperties
1739// for each apiscope in allApiScopes, e.g. something like:
1740// struct {
1741// Public sdkLibraryScopeProperties
1742// System sdkLibraryScopeProperties
1743// ...
1744// }
1745var allScopeStructType = createAllScopePropertiesStructType()
1746
1747// Dynamically create a structure type for each apiscope in allApiScopes.
1748func createAllScopePropertiesStructType() reflect.Type {
1749 var fields []reflect.StructField
1750 for _, apiScope := range allApiScopes {
1751 field := reflect.StructField{
1752 Name: apiScope.fieldName,
1753 Type: reflect.TypeOf(sdkLibraryScopeProperties{}),
1754 }
1755 fields = append(fields, field)
1756 }
1757
1758 return reflect.StructOf(fields)
1759}
1760
1761// Create an instance of the scope specific structure type and return a map
1762// from apiscope to a pointer to each scope specific field.
1763func createPropertiesInstance() (interface{}, map[*apiScope]*sdkLibraryScopeProperties) {
1764 allScopePropertiesPtr := reflect.New(allScopeStructType)
1765 allScopePropertiesStruct := allScopePropertiesPtr.Elem()
1766 scopeProperties := make(map[*apiScope]*sdkLibraryScopeProperties)
1767
1768 for _, apiScope := range allApiScopes {
1769 field := allScopePropertiesStruct.FieldByName(apiScope.fieldName)
1770 scopeProperties[apiScope] = field.Addr().Interface().(*sdkLibraryScopeProperties)
1771 }
1772
1773 return allScopePropertiesPtr.Interface(), scopeProperties
1774}
1775
Jaewoong Jung4f158ee2019-07-11 10:05:35 -07001776// java_sdk_library_import imports a prebuilt java_sdk_library.
Colin Cross79c7c262019-04-17 11:11:46 -07001777func sdkLibraryImportFactory() android.Module {
Paul Duffineedc5d52020-06-12 17:46:39 +01001778 module := &SdkLibraryImport{}
Colin Cross79c7c262019-04-17 11:11:46 -07001779
Paul Duffin6a2bd112020-04-07 19:27:04 +01001780 allScopeProperties, scopeToProperties := createPropertiesInstance()
1781 module.scopeProperties = scopeToProperties
1782 module.AddProperties(&module.properties, allScopeProperties)
Colin Cross79c7c262019-04-17 11:11:46 -07001783
Paul Duffinb74ee3e2020-05-08 14:16:20 +01001784 // Initialize information common between source and prebuilt.
1785 module.initCommon(&module.ModuleBase)
1786
Paul Duffin0bdcb272020-02-06 15:24:57 +00001787 android.InitPrebuiltModule(module, &[]string{""})
Paul Duffin61871622020-02-10 13:37:10 +00001788 android.InitApexModule(module)
1789 android.InitSdkAwareModule(module)
Colin Cross79c7c262019-04-17 11:11:46 -07001790 InitJavaModule(module, android.HostAndDeviceSupported)
1791
Paul Duffin1a724e62020-05-08 13:44:43 +01001792 module.SetDefaultableHook(func(mctx android.DefaultableHookContext) {
1793 if module.initCommonAfterDefaultsApplied(mctx) {
1794 module.createInternalModules(mctx)
1795 }
1796 })
Colin Cross79c7c262019-04-17 11:11:46 -07001797 return module
1798}
1799
Paul Duffineedc5d52020-06-12 17:46:39 +01001800func (module *SdkLibraryImport) Prebuilt() *android.Prebuilt {
Colin Cross79c7c262019-04-17 11:11:46 -07001801 return &module.prebuilt
1802}
1803
Paul Duffineedc5d52020-06-12 17:46:39 +01001804func (module *SdkLibraryImport) Name() string {
Colin Cross79c7c262019-04-17 11:11:46 -07001805 return module.prebuilt.Name(module.ModuleBase.Name())
1806}
1807
Paul Duffineedc5d52020-06-12 17:46:39 +01001808func (module *SdkLibraryImport) createInternalModules(mctx android.DefaultableHookContext) {
Colin Cross79c7c262019-04-17 11:11:46 -07001809
Paul Duffin50061512020-01-21 16:31:05 +00001810 // If the build is configured to use prebuilts then force this to be preferred.
1811 if mctx.Config().UnbundledBuildUsePrebuiltSdks() {
1812 module.prebuilt.ForcePrefer()
1813 }
1814
Paul Duffin6a2bd112020-04-07 19:27:04 +01001815 for apiScope, scopeProperties := range module.scopeProperties {
Paul Duffin56d44902020-01-31 13:36:25 +00001816 if len(scopeProperties.Jars) == 0 {
1817 continue
1818 }
1819
Paul Duffinf6155722020-04-09 00:07:11 +01001820 module.createJavaImportForStubs(mctx, apiScope, scopeProperties)
Paul Duffinf488ef22020-04-09 00:10:17 +01001821
Paul Duffin533f9c72020-05-20 16:18:00 +01001822 if len(scopeProperties.Stub_srcs) > 0 {
1823 module.createPrebuiltStubsSources(mctx, apiScope, scopeProperties)
1824 }
Paul Duffin56d44902020-01-31 13:36:25 +00001825 }
Colin Cross79c7c262019-04-17 11:11:46 -07001826
1827 javaSdkLibraries := javaSdkLibraries(mctx.Config())
1828 javaSdkLibrariesLock.Lock()
1829 defer javaSdkLibrariesLock.Unlock()
1830 *javaSdkLibraries = append(*javaSdkLibraries, module.BaseModuleName())
1831}
1832
Paul Duffineedc5d52020-06-12 17:46:39 +01001833func (module *SdkLibraryImport) createJavaImportForStubs(mctx android.DefaultableHookContext, apiScope *apiScope, scopeProperties *sdkLibraryScopeProperties) {
Paul Duffinf6155722020-04-09 00:07:11 +01001834 // Creates a java import for the jar with ".stubs" suffix
1835 props := struct {
Paul Duffind41712d2020-05-16 09:57:59 +01001836 Name *string
1837 Sdk_version *string
1838 Libs []string
1839 Jars []string
1840 Prefer *bool
Paul Duffinf6155722020-04-09 00:07:11 +01001841 }{}
Paul Duffinb74ee3e2020-05-08 14:16:20 +01001842 props.Name = proptools.StringPtr(module.stubsLibraryModuleName(apiScope))
Paul Duffinf6155722020-04-09 00:07:11 +01001843 props.Sdk_version = scopeProperties.Sdk_version
1844 // Prepend any of the libs from the legacy public properties to the libs for each of the
1845 // scopes to avoid having to duplicate them in each scope.
1846 props.Libs = append(module.properties.Libs, scopeProperties.Libs...)
1847 props.Jars = scopeProperties.Jars
Paul Duffind41712d2020-05-16 09:57:59 +01001848
Paul Duffindd89a282020-05-13 16:08:09 +01001849 // The imports are preferred if the java_sdk_library_import is preferred.
1850 props.Prefer = proptools.BoolPtr(module.prebuilt.Prefer())
Paul Duffin64e61992020-05-15 10:20:31 +01001851
1852 mctx.CreateModule(ImportFactory, &props, module.sdkComponentPropertiesForChildLibrary())
Paul Duffinf6155722020-04-09 00:07:11 +01001853}
1854
Paul Duffineedc5d52020-06-12 17:46:39 +01001855func (module *SdkLibraryImport) createPrebuiltStubsSources(mctx android.DefaultableHookContext, apiScope *apiScope, scopeProperties *sdkLibraryScopeProperties) {
Paul Duffinf488ef22020-04-09 00:10:17 +01001856 props := struct {
Paul Duffindd89a282020-05-13 16:08:09 +01001857 Name *string
1858 Srcs []string
1859 Prefer *bool
Paul Duffinf488ef22020-04-09 00:10:17 +01001860 }{}
Paul Duffinb74ee3e2020-05-08 14:16:20 +01001861 props.Name = proptools.StringPtr(module.stubsSourceModuleName(apiScope))
Paul Duffinf488ef22020-04-09 00:10:17 +01001862 props.Srcs = scopeProperties.Stub_srcs
1863 mctx.CreateModule(PrebuiltStubsSourcesFactory, &props)
Paul Duffindd89a282020-05-13 16:08:09 +01001864
1865 // The stubs source is preferred if the java_sdk_library_import is preferred.
1866 props.Prefer = proptools.BoolPtr(module.prebuilt.Prefer())
Paul Duffinf488ef22020-04-09 00:10:17 +01001867}
1868
Paul Duffin44f1d842020-06-26 20:17:02 +01001869// Add the dependencies on the child module in the component deps mutator so that it
1870// creates references to the prebuilt and not the source modules.
1871func (module *SdkLibraryImport) ComponentDepsMutator(ctx android.BottomUpMutatorContext) {
Paul Duffin6a2bd112020-04-07 19:27:04 +01001872 for apiScope, scopeProperties := range module.scopeProperties {
Paul Duffin56d44902020-01-31 13:36:25 +00001873 if len(scopeProperties.Jars) == 0 {
1874 continue
1875 }
1876
1877 // Add dependencies to the prebuilt stubs library
Paul Duffin44f1d842020-06-26 20:17:02 +01001878 ctx.AddVariationDependencies(nil, apiScope.stubsTag, "prebuilt_"+module.stubsLibraryModuleName(apiScope))
Paul Duffin533f9c72020-05-20 16:18:00 +01001879
1880 if len(scopeProperties.Stub_srcs) > 0 {
1881 // Add dependencies to the prebuilt stubs source library
Paul Duffin44f1d842020-06-26 20:17:02 +01001882 ctx.AddVariationDependencies(nil, apiScope.stubsSourceTag, "prebuilt_"+module.stubsSourceModuleName(apiScope))
Paul Duffin533f9c72020-05-20 16:18:00 +01001883 }
Paul Duffin56d44902020-01-31 13:36:25 +00001884 }
Paul Duffin44f1d842020-06-26 20:17:02 +01001885}
1886
1887// Add other dependencies as normal.
1888func (module *SdkLibraryImport) DepsMutator(ctx android.BottomUpMutatorContext) {
Paul Duffineedc5d52020-06-12 17:46:39 +01001889
1890 implName := module.implLibraryModuleName()
1891 if ctx.OtherModuleExists(implName) {
1892 ctx.AddVariationDependencies(nil, implLibraryTag, implName)
1893
1894 xmlPermissionsModuleName := module.xmlPermissionsModuleName()
1895 if module.sharedLibrary() && ctx.OtherModuleExists(xmlPermissionsModuleName) {
1896 // Add dependency to the rule for generating the xml permissions file
1897 ctx.AddDependency(module, xmlPermissionsFileTag, xmlPermissionsModuleName)
1898 }
1899 }
Colin Cross79c7c262019-04-17 11:11:46 -07001900}
1901
Paul Duffineedc5d52020-06-12 17:46:39 +01001902func (module *SdkLibraryImport) DepIsInSameApex(mctx android.BaseModuleContext, dep android.Module) bool {
1903 depTag := mctx.OtherModuleDependencyTag(dep)
1904 if depTag == xmlPermissionsFileTag {
1905 return true
1906 }
1907
1908 // None of the other dependencies of the java_sdk_library_import are in the same apex
1909 // as the one that references this module.
1910 return false
1911}
1912
Jooyung Han749dc692020-04-15 11:03:39 +09001913func (module *SdkLibraryImport) ShouldSupportSdkVersion(ctx android.BaseModuleContext, sdkVersion int) error {
1914 // we don't check prebuilt modules for sdk_version
1915 return nil
1916}
1917
Paul Duffineedc5d52020-06-12 17:46:39 +01001918func (module *SdkLibraryImport) OutputFiles(tag string) (android.Paths, error) {
Paul Duffin46fdda82020-05-14 15:39:10 +01001919 return module.commonOutputFiles(tag)
1920}
1921
Paul Duffineedc5d52020-06-12 17:46:39 +01001922func (module *SdkLibraryImport) GenerateAndroidBuildActions(ctx android.ModuleContext) {
Paul Duffin533f9c72020-05-20 16:18:00 +01001923 // Record the paths to the prebuilt stubs library and stubs source.
Colin Cross79c7c262019-04-17 11:11:46 -07001924 ctx.VisitDirectDeps(func(to android.Module) {
1925 tag := ctx.OtherModuleDependencyTag(to)
1926
Paul Duffin533f9c72020-05-20 16:18:00 +01001927 // Extract information from any of the scope specific dependencies.
1928 if scopeTag, ok := tag.(scopeDependencyTag); ok {
1929 apiScope := scopeTag.apiScope
1930 scopePaths := module.getScopePathsCreateIfNeeded(apiScope)
1931
1932 // Extract information from the dependency. The exact information extracted
1933 // is determined by the nature of the dependency which is determined by the tag.
1934 scopeTag.extractDepInfo(ctx, to, scopePaths)
Paul Duffineedc5d52020-06-12 17:46:39 +01001935 } else if tag == implLibraryTag {
1936 if implLibrary, ok := to.(*Library); ok {
1937 module.implLibraryModule = implLibrary
1938 } else {
1939 ctx.ModuleErrorf("implementation library must be of type *java.Library but was %T", to)
1940 }
1941 } else if tag == xmlPermissionsFileTag {
1942 if xmlPermissionsFileModule, ok := to.(*sdkLibraryXml); ok {
1943 module.xmlPermissionsFileModule = xmlPermissionsFileModule
1944 } else {
1945 ctx.ModuleErrorf("xml permissions file module must be of type *sdkLibraryXml but was %T", to)
1946 }
Colin Cross79c7c262019-04-17 11:11:46 -07001947 }
1948 })
Paul Duffin533f9c72020-05-20 16:18:00 +01001949
1950 // Populate the scope paths with information from the properties.
1951 for apiScope, scopeProperties := range module.scopeProperties {
1952 if len(scopeProperties.Jars) == 0 {
1953 continue
1954 }
1955
1956 paths := module.getScopePathsCreateIfNeeded(apiScope)
1957 paths.currentApiFilePath = android.OptionalPathForModuleSrc(ctx, scopeProperties.Current_api)
1958 paths.removedApiFilePath = android.OptionalPathForModuleSrc(ctx, scopeProperties.Removed_api)
1959 }
Colin Cross79c7c262019-04-17 11:11:46 -07001960}
1961
Paul Duffineedc5d52020-06-12 17:46:39 +01001962func (module *SdkLibraryImport) sdkJars(ctx android.BaseModuleContext, sdkVersion sdkSpec, headerJars bool) android.Paths {
1963
1964 // For consistency with SdkLibrary make the implementation jar available to libraries that
1965 // are within the same APEX.
1966 implLibraryModule := module.implLibraryModule
1967 if implLibraryModule != nil && withinSameApexAs(module, ctx.Module()) {
1968 if headerJars {
1969 return implLibraryModule.HeaderJars()
1970 } else {
1971 return implLibraryModule.ImplementationJars()
1972 }
1973 }
1974
Paul Duffina3fb67d2020-05-20 14:20:02 +01001975 return module.selectHeaderJarsForSdkVersion(ctx, sdkVersion)
Paul Duffin56d44902020-01-31 13:36:25 +00001976}
1977
Colin Cross79c7c262019-04-17 11:11:46 -07001978// to satisfy SdkLibraryDependency interface
Paul Duffineedc5d52020-06-12 17:46:39 +01001979func (module *SdkLibraryImport) SdkHeaderJars(ctx android.BaseModuleContext, sdkVersion sdkSpec) android.Paths {
Colin Cross79c7c262019-04-17 11:11:46 -07001980 // This module is just a wrapper for the prebuilt stubs.
Paul Duffineedc5d52020-06-12 17:46:39 +01001981 return module.sdkJars(ctx, sdkVersion, true)
Colin Cross79c7c262019-04-17 11:11:46 -07001982}
1983
1984// to satisfy SdkLibraryDependency interface
Paul Duffineedc5d52020-06-12 17:46:39 +01001985func (module *SdkLibraryImport) SdkImplementationJars(ctx android.BaseModuleContext, sdkVersion sdkSpec) android.Paths {
Colin Cross79c7c262019-04-17 11:11:46 -07001986 // This module is just a wrapper for the stubs.
Paul Duffineedc5d52020-06-12 17:46:39 +01001987 return module.sdkJars(ctx, sdkVersion, false)
1988}
1989
1990// to satisfy apex.javaDependency interface
1991func (module *SdkLibraryImport) DexJarBuildPath() android.Path {
1992 if module.implLibraryModule == nil {
1993 return nil
1994 } else {
1995 return module.implLibraryModule.DexJarBuildPath()
1996 }
1997}
1998
1999// to satisfy apex.javaDependency interface
2000func (module *SdkLibraryImport) JacocoReportClassesFile() android.Path {
2001 if module.implLibraryModule == nil {
2002 return nil
2003 } else {
2004 return module.implLibraryModule.JacocoReportClassesFile()
2005 }
2006}
2007
2008// to satisfy apex.javaDependency interface
Colin Cross08dca382020-07-21 20:31:17 -07002009func (module *SdkLibraryImport) LintDepSets() LintDepSets {
2010 if module.implLibraryModule == nil {
2011 return LintDepSets{}
2012 } else {
2013 return module.implLibraryModule.LintDepSets()
2014 }
2015}
2016
2017// to satisfy apex.javaDependency interface
Paul Duffineedc5d52020-06-12 17:46:39 +01002018func (module *SdkLibraryImport) Stem() string {
2019 return module.BaseModuleName()
Colin Cross79c7c262019-04-17 11:11:46 -07002020}
Jiyong Parke3833882020-02-17 17:28:10 +09002021
Paul Duffin44b481b2020-06-17 16:59:43 +01002022var _ ApexDependency = (*SdkLibraryImport)(nil)
2023
2024// to satisfy java.ApexDependency interface
2025func (module *SdkLibraryImport) HeaderJars() android.Paths {
2026 if module.implLibraryModule == nil {
2027 return nil
2028 } else {
2029 return module.implLibraryModule.HeaderJars()
2030 }
2031}
2032
2033// to satisfy java.ApexDependency interface
2034func (module *SdkLibraryImport) ImplementationAndResourcesJars() android.Paths {
2035 if module.implLibraryModule == nil {
2036 return nil
2037 } else {
2038 return module.implLibraryModule.ImplementationAndResourcesJars()
2039 }
2040}
2041
Jiyong Parke3833882020-02-17 17:28:10 +09002042//
2043// java_sdk_library_xml
2044//
2045type sdkLibraryXml struct {
2046 android.ModuleBase
2047 android.DefaultableModuleBase
2048 android.ApexModuleBase
2049
2050 properties sdkLibraryXmlProperties
2051
2052 outputFilePath android.OutputPath
2053 installDirPath android.InstallPath
2054}
2055
2056type sdkLibraryXmlProperties struct {
2057 // canonical name of the lib
2058 Lib_name *string
2059}
2060
2061// java_sdk_library_xml builds the permission xml file for a java_sdk_library.
2062// Not to be used directly by users. java_sdk_library internally uses this.
2063func sdkLibraryXmlFactory() android.Module {
2064 module := &sdkLibraryXml{}
2065
2066 module.AddProperties(&module.properties)
2067
2068 android.InitApexModule(module)
2069 android.InitAndroidArchModule(module, android.DeviceSupported, android.MultilibCommon)
2070
2071 return module
2072}
2073
2074// from android.PrebuiltEtcModule
2075func (module *sdkLibraryXml) SubDir() string {
2076 return "permissions"
2077}
2078
2079// from android.PrebuiltEtcModule
2080func (module *sdkLibraryXml) OutputFile() android.OutputPath {
2081 return module.outputFilePath
2082}
2083
2084// from android.ApexModule
2085func (module *sdkLibraryXml) AvailableFor(what string) bool {
2086 return true
2087}
2088
2089func (module *sdkLibraryXml) DepsMutator(ctx android.BottomUpMutatorContext) {
2090 // do nothing
2091}
2092
Jooyung Han749dc692020-04-15 11:03:39 +09002093func (module *sdkLibraryXml) ShouldSupportSdkVersion(ctx android.BaseModuleContext, sdkVersion int) error {
2094 // sdkLibraryXml doesn't need to be checked separately because java_sdk_library is checked
2095 return nil
2096}
2097
Jiyong Parke3833882020-02-17 17:28:10 +09002098// File path to the runtime implementation library
2099func (module *sdkLibraryXml) implPath() string {
2100 implName := proptools.String(module.properties.Lib_name)
2101 if apexName := module.ApexName(); apexName != "" {
2102 // TODO(b/146468504): ApexName() is only a soong module name, not apex name.
2103 // In most cases, this works fine. But when apex_name is set or override_apex is used
2104 // this can be wrong.
2105 return fmt.Sprintf("/apex/%s/javalib/%s.jar", apexName, implName)
2106 }
2107 partition := "system"
2108 if module.SocSpecific() {
2109 partition = "vendor"
2110 } else if module.DeviceSpecific() {
2111 partition = "odm"
2112 } else if module.ProductSpecific() {
2113 partition = "product"
2114 } else if module.SystemExtSpecific() {
2115 partition = "system_ext"
2116 }
2117 return "/" + partition + "/framework/" + implName + ".jar"
2118}
2119
2120func (module *sdkLibraryXml) GenerateAndroidBuildActions(ctx android.ModuleContext) {
2121 libName := proptools.String(module.properties.Lib_name)
2122 xmlContent := fmt.Sprintf(permissionsTemplate, libName, module.implPath())
2123
2124 module.outputFilePath = android.PathForModuleOut(ctx, libName+".xml").OutputPath
2125 rule := android.NewRuleBuilder()
2126 rule.Command().
2127 Text("/bin/bash -c \"echo -e '" + xmlContent + "'\" > ").
2128 Output(module.outputFilePath)
2129
2130 rule.Build(pctx, ctx, "java_sdk_xml", "Permission XML")
2131
2132 module.installDirPath = android.PathForModuleInstall(ctx, "etc", module.SubDir())
2133}
2134
2135func (module *sdkLibraryXml) AndroidMkEntries() []android.AndroidMkEntries {
2136 if !module.IsForPlatform() {
2137 return []android.AndroidMkEntries{android.AndroidMkEntries{
2138 Disabled: true,
2139 }}
2140 }
2141
2142 return []android.AndroidMkEntries{android.AndroidMkEntries{
2143 Class: "ETC",
2144 OutputFile: android.OptionalPathForPath(module.outputFilePath),
2145 ExtraEntries: []android.AndroidMkExtraEntriesFunc{
2146 func(entries *android.AndroidMkEntries) {
2147 entries.SetString("LOCAL_MODULE_TAGS", "optional")
2148 entries.SetString("LOCAL_MODULE_PATH", module.installDirPath.ToMakePath().String())
2149 entries.SetString("LOCAL_INSTALLED_MODULE_STEM", module.outputFilePath.Base())
2150 },
2151 },
2152 }}
2153}
Paul Duffin61871622020-02-10 13:37:10 +00002154
2155type sdkLibrarySdkMemberType struct {
2156 android.SdkMemberTypeBase
2157}
2158
2159func (s *sdkLibrarySdkMemberType) AddDependencies(mctx android.BottomUpMutatorContext, dependencyTag blueprint.DependencyTag, names []string) {
2160 mctx.AddVariationDependencies(nil, dependencyTag, names...)
2161}
2162
2163func (s *sdkLibrarySdkMemberType) IsInstance(module android.Module) bool {
2164 _, ok := module.(*SdkLibrary)
2165 return ok
2166}
2167
2168func (s *sdkLibrarySdkMemberType) AddPrebuiltModule(ctx android.SdkMemberContext, member android.SdkMember) android.BpModule {
2169 return ctx.SnapshotBuilder().AddPrebuiltModule(member, "java_sdk_library_import")
2170}
2171
2172func (s *sdkLibrarySdkMemberType) CreateVariantPropertiesStruct() android.SdkMemberProperties {
2173 return &sdkLibrarySdkMemberProperties{}
2174}
2175
2176type sdkLibrarySdkMemberProperties struct {
2177 android.SdkMemberPropertiesBase
2178
2179 // Scope to per scope properties.
2180 Scopes map[*apiScope]scopeProperties
2181
2182 // Additional libraries that the exported stubs libraries depend upon.
2183 Libs []string
Paul Duffinf488ef22020-04-09 00:10:17 +01002184
2185 // The Java stubs source files.
2186 Stub_srcs []string
Paul Duffinf8e08b22020-05-13 16:54:55 +01002187
2188 // The naming scheme.
2189 Naming_scheme *string
Paul Duffina84756c2020-05-26 20:57:10 +01002190
2191 // True if the java_sdk_library_import is for a shared library, false
2192 // otherwise.
2193 Shared_library *bool
Paul Duffin61871622020-02-10 13:37:10 +00002194}
2195
2196type scopeProperties struct {
Paul Duffin75dcc802020-04-09 01:08:11 +01002197 Jars android.Paths
2198 StubsSrcJar android.Path
2199 CurrentApiFile android.Path
2200 RemovedApiFile android.Path
2201 SdkVersion string
Paul Duffin61871622020-02-10 13:37:10 +00002202}
2203
2204func (s *sdkLibrarySdkMemberProperties) PopulateFromVariant(ctx android.SdkMemberContext, variant android.Module) {
2205 sdk := variant.(*SdkLibrary)
2206
2207 s.Scopes = make(map[*apiScope]scopeProperties)
2208 for _, apiScope := range allApiScopes {
Paul Duffin5ae30792020-05-20 11:52:25 +01002209 paths := sdk.findScopePaths(apiScope)
2210 if paths == nil {
2211 continue
2212 }
2213
Paul Duffin61871622020-02-10 13:37:10 +00002214 jars := paths.stubsImplPath
2215 if len(jars) > 0 {
2216 properties := scopeProperties{}
2217 properties.Jars = jars
Paul Duffin153501f2020-05-12 15:52:55 +01002218 properties.SdkVersion = sdk.sdkVersionForStubsLibrary(ctx.SdkModuleContext(), apiScope)
Paul Duffin533f9c72020-05-20 16:18:00 +01002219 properties.StubsSrcJar = paths.stubsSrcJar.Path()
Paul Duffin86672f62020-06-19 18:39:55 +01002220 if paths.currentApiFilePath.Valid() {
2221 properties.CurrentApiFile = paths.currentApiFilePath.Path()
2222 }
2223 if paths.removedApiFilePath.Valid() {
2224 properties.RemovedApiFile = paths.removedApiFilePath.Path()
2225 }
Paul Duffin61871622020-02-10 13:37:10 +00002226 s.Scopes[apiScope] = properties
2227 }
2228 }
2229
2230 s.Libs = sdk.properties.Libs
Paul Duffind11e78e2020-05-15 20:37:11 +01002231 s.Naming_scheme = sdk.commonSdkLibraryProperties.Naming_scheme
Paul Duffina84756c2020-05-26 20:57:10 +01002232 s.Shared_library = proptools.BoolPtr(sdk.sharedLibrary())
Paul Duffin61871622020-02-10 13:37:10 +00002233}
2234
2235func (s *sdkLibrarySdkMemberProperties) AddToPropertySet(ctx android.SdkMemberContext, propertySet android.BpPropertySet) {
Paul Duffinf8e08b22020-05-13 16:54:55 +01002236 if s.Naming_scheme != nil {
2237 propertySet.AddProperty("naming_scheme", proptools.String(s.Naming_scheme))
2238 }
Paul Duffina84756c2020-05-26 20:57:10 +01002239 if s.Shared_library != nil {
2240 propertySet.AddProperty("shared_library", *s.Shared_library)
2241 }
Paul Duffinf8e08b22020-05-13 16:54:55 +01002242
Paul Duffin61871622020-02-10 13:37:10 +00002243 for _, apiScope := range allApiScopes {
2244 if properties, ok := s.Scopes[apiScope]; ok {
Paul Duffin0f270632020-05-13 19:19:49 +01002245 scopeSet := propertySet.AddPropertySet(apiScope.propertyName)
Paul Duffin61871622020-02-10 13:37:10 +00002246
Paul Duffinf488ef22020-04-09 00:10:17 +01002247 scopeDir := filepath.Join("sdk_library", s.OsPrefix(), apiScope.name)
2248
Paul Duffin61871622020-02-10 13:37:10 +00002249 var jars []string
2250 for _, p := range properties.Jars {
Paul Duffinf488ef22020-04-09 00:10:17 +01002251 dest := filepath.Join(scopeDir, ctx.Name()+"-stubs.jar")
Paul Duffin61871622020-02-10 13:37:10 +00002252 ctx.SnapshotBuilder().CopyToSnapshot(p, dest)
2253 jars = append(jars, dest)
2254 }
2255 scopeSet.AddProperty("jars", jars)
2256
Paul Duffinf488ef22020-04-09 00:10:17 +01002257 // Merge the stubs source jar into the snapshot zip so that when it is unpacked
2258 // the source files are also unpacked.
2259 snapshotRelativeDir := filepath.Join(scopeDir, ctx.Name()+"_stub_sources")
2260 ctx.SnapshotBuilder().UnzipToSnapshot(properties.StubsSrcJar, snapshotRelativeDir)
2261 scopeSet.AddProperty("stub_srcs", []string{snapshotRelativeDir})
2262
Paul Duffin75dcc802020-04-09 01:08:11 +01002263 if properties.CurrentApiFile != nil {
2264 currentApiSnapshotPath := filepath.Join(scopeDir, ctx.Name()+".txt")
2265 ctx.SnapshotBuilder().CopyToSnapshot(properties.CurrentApiFile, currentApiSnapshotPath)
2266 scopeSet.AddProperty("current_api", currentApiSnapshotPath)
2267 }
2268
2269 if properties.RemovedApiFile != nil {
2270 removedApiSnapshotPath := filepath.Join(scopeDir, ctx.Name()+"-removed.txt")
Paul Duffinb1787352020-06-02 13:00:02 +01002271 ctx.SnapshotBuilder().CopyToSnapshot(properties.RemovedApiFile, removedApiSnapshotPath)
Paul Duffin75dcc802020-04-09 01:08:11 +01002272 scopeSet.AddProperty("removed_api", removedApiSnapshotPath)
2273 }
2274
Paul Duffin61871622020-02-10 13:37:10 +00002275 if properties.SdkVersion != "" {
2276 scopeSet.AddProperty("sdk_version", properties.SdkVersion)
2277 }
2278 }
2279 }
2280
2281 if len(s.Libs) > 0 {
2282 propertySet.AddPropertyWithTag("libs", s.Libs, ctx.SnapshotBuilder().SdkMemberReferencePropertyTag(false))
2283 }
2284}