blob: 4e33d747b36c837df77eecc51a971a45a4fea8c1 [file] [log] [blame]
Jiyong Parkc678ad32018-04-10 13:07:10 +09001// Copyright 2018 Google Inc. All rights reserved.
2//
3// Licensed under the Apache License, Version 2.0 (the "License");
4// you may not use this file except in compliance with the License.
5// You may obtain a copy of the License at
6//
7// http://www.apache.org/licenses/LICENSE-2.0
8//
9// Unless required by applicable law or agreed to in writing, software
10// distributed under the License is distributed on an "AS IS" BASIS,
11// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12// See the License for the specific language governing permissions and
13// limitations under the License.
14
15package java
16
17import (
Jiyong Parkc678ad32018-04-10 13:07:10 +090018 "fmt"
19 "path"
Sundong Ahn054b19a2018-10-19 13:46:09 +090020 "path/filepath"
Paul Duffin46a26a82020-04-07 19:27:04 +010021 "reflect"
Paul Duffin46dc45a2020-05-14 15:39:10 +010022 "regexp"
Jiyong Park82484c02018-04-23 21:41:26 +090023 "sort"
Jiyong Parkc678ad32018-04-10 13:07:10 +090024 "strings"
Jiyong Park82484c02018-04-23 21:41:26 +090025 "sync"
Jiyong Parkc678ad32018-04-10 13:07:10 +090026
Paul Duffind1b3a922020-01-22 11:57:20 +000027 "github.com/google/blueprint"
Jiyong Parkc678ad32018-04-10 13:07:10 +090028 "github.com/google/blueprint/proptools"
Paul Duffin46a26a82020-04-07 19:27:04 +010029
30 "android/soong/android"
Jiyong Parkc678ad32018-04-10 13:07:10 +090031)
32
Jooyung Han58f26ab2019-12-18 15:34:32 +090033const (
Paul Duffindd9d0742020-05-08 15:52:37 +010034 sdkXmlFileSuffix = ".xml"
35 permissionsTemplate = `<?xml version=\"1.0\" encoding=\"utf-8\"?>\n` +
Jooyung Han624058e2019-12-24 18:38:06 +090036 `<!-- Copyright (C) 2018 The Android Open Source Project\n` +
37 `\n` +
Jiyong Parke3833882020-02-17 17:28:10 +090038 ` Licensed under the Apache License, Version 2.0 (the \"License\");\n` +
Jooyung Han624058e2019-12-24 18:38:06 +090039 ` you may not use this file except in compliance with the License.\n` +
40 ` You may obtain a copy of the License at\n` +
41 `\n` +
42 ` http://www.apache.org/licenses/LICENSE-2.0\n` +
43 `\n` +
44 ` Unless required by applicable law or agreed to in writing, software\n` +
Jiyong Parke3833882020-02-17 17:28:10 +090045 ` distributed under the License is distributed on an \"AS IS\" BASIS,\n` +
Jooyung Han624058e2019-12-24 18:38:06 +090046 ` WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\n` +
47 ` See the License for the specific language governing permissions and\n` +
48 ` limitations under the License.\n` +
49 `-->\n` +
50 `<permissions>\n` +
Jiyong Parke3833882020-02-17 17:28:10 +090051 ` <library name=\"%s\" file=\"%s\"/>\n` +
Jooyung Han624058e2019-12-24 18:38:06 +090052 `</permissions>\n`
Jiyong Parkc678ad32018-04-10 13:07:10 +090053)
54
Paul Duffind1b3a922020-01-22 11:57:20 +000055// A tag to associated a dependency with a specific api scope.
56type scopeDependencyTag struct {
57 blueprint.BaseDependencyTag
58 name string
59 apiScope *apiScope
Paul Duffinc8782502020-04-29 20:45:27 +010060
61 // Function for extracting appropriate path information from the dependency.
62 depInfoExtractor func(paths *scopePaths, dep android.Module) error
63}
64
65// Extract tag specific information from the dependency.
66func (tag scopeDependencyTag) extractDepInfo(ctx android.ModuleContext, dep android.Module, paths *scopePaths) {
67 err := tag.depInfoExtractor(paths, dep)
68 if err != nil {
69 ctx.ModuleErrorf("has an invalid {scopeDependencyTag: %s} dependency on module %s: %s", tag.name, ctx.OtherModuleName(dep), err.Error())
70 }
Paul Duffind1b3a922020-01-22 11:57:20 +000071}
72
Paul Duffin80342d72020-06-26 22:08:43 +010073var _ android.ReplaceSourceWithPrebuilt = (*scopeDependencyTag)(nil)
74
75func (tag scopeDependencyTag) ReplaceSourceWithPrebuilt() bool {
76 return false
77}
78
Paul Duffind1b3a922020-01-22 11:57:20 +000079// Provides information about an api scope, e.g. public, system, test.
80type apiScope struct {
81 // The name of the api scope, e.g. public, system, test
82 name string
83
Paul Duffin97b53b82020-05-05 14:40:52 +010084 // The api scope that this scope extends.
85 extends *apiScope
86
Paul Duffin3375e352020-04-28 10:44:03 +010087 // The legacy enabled status for a specific scope can be dependent on other
88 // properties that have been specified on the library so it is provided by
89 // a function that can determine the status by examining those properties.
90 legacyEnabledStatus func(module *SdkLibrary) bool
91
92 // The default enabled status for non-legacy behavior, which is triggered by
93 // explicitly enabling at least one api scope.
94 defaultEnabledStatus bool
95
96 // Gets a pointer to the scope specific properties.
97 scopeSpecificProperties func(module *SdkLibrary) *ApiScopeProperties
98
Paul Duffin46a26a82020-04-07 19:27:04 +010099 // The name of the field in the dynamically created structure.
100 fieldName string
101
Paul Duffin6b836ba2020-05-13 19:19:49 +0100102 // The name of the property in the java_sdk_library_import
103 propertyName string
104
Paul Duffind1b3a922020-01-22 11:57:20 +0000105 // The tag to use to depend on the stubs library module.
106 stubsTag scopeDependencyTag
107
Paul Duffin0ff08bd2020-04-29 13:30:54 +0100108 // The tag to use to depend on the stubs source module (if separate from the API module).
109 stubsSourceTag scopeDependencyTag
110
111 // The tag to use to depend on the API file generating module (if separate from the stubs source module).
112 apiFileTag scopeDependencyTag
113
Paul Duffinc8782502020-04-29 20:45:27 +0100114 // The tag to use to depend on the stubs source and API module.
115 stubsSourceAndApiTag scopeDependencyTag
Paul Duffind1b3a922020-01-22 11:57:20 +0000116
117 // The scope specific prefix to add to the api file base of "current.txt" or "removed.txt".
118 apiFilePrefix string
119
120 // The scope specific prefix to add to the sdk library module name to construct a scope specific
121 // module name.
122 moduleSuffix string
123
Paul Duffind1b3a922020-01-22 11:57:20 +0000124 // SDK version that the stubs library is built against. Note that this is always
125 // *current. Older stubs library built with a numbered SDK version is created from
126 // the prebuilt jar.
127 sdkVersion string
Paul Duffin1fb487d2020-04-07 18:50:10 +0100128
Paul Duffin15f34ef2020-07-20 18:04:44 +0100129 // The annotation that identifies this API level, empty for the public API scope.
130 annotation string
131
Paul Duffin1fb487d2020-04-07 18:50:10 +0100132 // Extra arguments to pass to droidstubs for this scope.
Paul Duffin0ff08bd2020-04-29 13:30:54 +0100133 //
Paul Duffin15f34ef2020-07-20 18:04:44 +0100134 // This is not used directly but is used to construct the droidstubsArgs.
135 extraArgs []string
Paul Duffin0ff08bd2020-04-29 13:30:54 +0100136
Paul Duffin15f34ef2020-07-20 18:04:44 +0100137 // The args that must be passed to droidstubs to generate the API and stubs source
138 // for this scope, constructed dynamically by initApiScope().
Paul Duffin0ff08bd2020-04-29 13:30:54 +0100139 //
140 // The API only includes the additional members that this scope adds over the scope
141 // that it extends.
Paul Duffin15f34ef2020-07-20 18:04:44 +0100142 //
143 // The stubs source must include the definitions of everything that is in this
144 // api scope and all the scopes that this one extends.
145 droidstubsArgs []string
Paul Duffin0ff08bd2020-04-29 13:30:54 +0100146
Anton Hansson6478ac12020-05-02 11:19:36 +0100147 // Whether the api scope can be treated as unstable, and should skip compat checks.
148 unstable bool
Paul Duffind1b3a922020-01-22 11:57:20 +0000149}
150
151// Initialize a scope, creating and adding appropriate dependency tags
152func initApiScope(scope *apiScope) *apiScope {
Paul Duffinc8782502020-04-29 20:45:27 +0100153 name := scope.name
Paul Duffin46dc45a2020-05-14 15:39:10 +0100154 scopeByName[name] = scope
155 allScopeNames = append(allScopeNames, name)
Paul Duffin6b836ba2020-05-13 19:19:49 +0100156 scope.propertyName = strings.ReplaceAll(name, "-", "_")
157 scope.fieldName = proptools.FieldNameForProperty(scope.propertyName)
Paul Duffind1b3a922020-01-22 11:57:20 +0000158 scope.stubsTag = scopeDependencyTag{
Paul Duffinc8782502020-04-29 20:45:27 +0100159 name: name + "-stubs",
160 apiScope: scope,
161 depInfoExtractor: (*scopePaths).extractStubsLibraryInfoFromDependency,
Paul Duffind1b3a922020-01-22 11:57:20 +0000162 }
Paul Duffin0ff08bd2020-04-29 13:30:54 +0100163 scope.stubsSourceTag = scopeDependencyTag{
164 name: name + "-stubs-source",
165 apiScope: scope,
166 depInfoExtractor: (*scopePaths).extractStubsSourceInfoFromDep,
167 }
168 scope.apiFileTag = scopeDependencyTag{
169 name: name + "-api",
170 apiScope: scope,
171 depInfoExtractor: (*scopePaths).extractApiInfoFromDep,
172 }
Paul Duffinc8782502020-04-29 20:45:27 +0100173 scope.stubsSourceAndApiTag = scopeDependencyTag{
174 name: name + "-stubs-source-and-api",
175 apiScope: scope,
176 depInfoExtractor: (*scopePaths).extractStubsSourceAndApiInfoFromApiStubsProvider,
Paul Duffind1b3a922020-01-22 11:57:20 +0000177 }
Paul Duffin0ff08bd2020-04-29 13:30:54 +0100178
179 // To get the args needed to generate the stubs source append all the args from
180 // this scope and all the scopes it extends as each set of args adds additional
181 // members to the stubs.
Paul Duffin15f34ef2020-07-20 18:04:44 +0100182 var scopeSpecificArgs []string
183 if scope.annotation != "" {
184 scopeSpecificArgs = []string{"--show-annotation", scope.annotation}
Paul Duffin0ff08bd2020-04-29 13:30:54 +0100185 }
Paul Duffin15f34ef2020-07-20 18:04:44 +0100186 for s := scope; s != nil; s = s.extends {
187 scopeSpecificArgs = append(scopeSpecificArgs, s.extraArgs...)
Paul Duffin0ff08bd2020-04-29 13:30:54 +0100188
Paul Duffin15f34ef2020-07-20 18:04:44 +0100189 // Ensure that the generated stubs includes all the API elements from the API scope
190 // that this scope extends.
191 if s != scope && s.annotation != "" {
192 scopeSpecificArgs = append(scopeSpecificArgs, "--show-for-stub-purposes-annotation", s.annotation)
193 }
194 }
Paul Duffin0ff08bd2020-04-29 13:30:54 +0100195
Paul Duffin15f34ef2020-07-20 18:04:44 +0100196 // Escape any special characters in the arguments. This is needed because droidstubs
197 // passes these directly to the shell command.
198 scope.droidstubsArgs = proptools.ShellEscapeList(scopeSpecificArgs)
Paul Duffin0ff08bd2020-04-29 13:30:54 +0100199
Paul Duffind1b3a922020-01-22 11:57:20 +0000200 return scope
201}
202
Paul Duffinc3091c82020-05-08 14:16:20 +0100203func (scope *apiScope) stubsLibraryModuleName(baseName string) string {
Paul Duffindd9d0742020-05-08 15:52:37 +0100204 return baseName + ".stubs" + scope.moduleSuffix
Paul Duffind1b3a922020-01-22 11:57:20 +0000205}
206
Paul Duffinc8782502020-04-29 20:45:27 +0100207func (scope *apiScope) stubsSourceModuleName(baseName string) string {
Paul Duffindd9d0742020-05-08 15:52:37 +0100208 return baseName + ".stubs.source" + scope.moduleSuffix
Paul Duffind1b3a922020-01-22 11:57:20 +0000209}
210
Paul Duffin0ff08bd2020-04-29 13:30:54 +0100211func (scope *apiScope) apiModuleName(baseName string) string {
Paul Duffindd9d0742020-05-08 15:52:37 +0100212 return baseName + ".api" + scope.moduleSuffix
Paul Duffin0ff08bd2020-04-29 13:30:54 +0100213}
214
Paul Duffin3375e352020-04-28 10:44:03 +0100215func (scope *apiScope) String() string {
216 return scope.name
217}
218
Paul Duffind1b3a922020-01-22 11:57:20 +0000219type apiScopes []*apiScope
220
221func (scopes apiScopes) Strings(accessor func(*apiScope) string) []string {
222 var list []string
223 for _, scope := range scopes {
224 list = append(list, accessor(scope))
225 }
226 return list
227}
228
Jiyong Parkc678ad32018-04-10 13:07:10 +0900229var (
Paul Duffin46dc45a2020-05-14 15:39:10 +0100230 scopeByName = make(map[string]*apiScope)
231 allScopeNames []string
Paul Duffind1b3a922020-01-22 11:57:20 +0000232 apiScopePublic = initApiScope(&apiScope{
Paul Duffin3375e352020-04-28 10:44:03 +0100233 name: "public",
234
235 // Public scope is enabled by default for both legacy and non-legacy modes.
236 legacyEnabledStatus: func(module *SdkLibrary) bool {
237 return true
238 },
239 defaultEnabledStatus: true,
240
241 scopeSpecificProperties: func(module *SdkLibrary) *ApiScopeProperties {
242 return &module.sdkLibraryProperties.Public
243 },
Paul Duffind1b3a922020-01-22 11:57:20 +0000244 sdkVersion: "current",
245 })
246 apiScopeSystem = initApiScope(&apiScope{
Paul Duffin3375e352020-04-28 10:44:03 +0100247 name: "system",
248 extends: apiScopePublic,
249 legacyEnabledStatus: (*SdkLibrary).generateTestAndSystemScopesByDefault,
250 scopeSpecificProperties: func(module *SdkLibrary) *ApiScopeProperties {
251 return &module.sdkLibraryProperties.System
252 },
Paul Duffin15f34ef2020-07-20 18:04:44 +0100253 apiFilePrefix: "system-",
254 moduleSuffix: ".system",
255 sdkVersion: "system_current",
256 annotation: "android.annotation.SystemApi(client=android.annotation.SystemApi.Client.PRIVILEGED_APPS)",
Paul Duffind1b3a922020-01-22 11:57:20 +0000257 })
258 apiScopeTest = initApiScope(&apiScope{
Paul Duffin3375e352020-04-28 10:44:03 +0100259 name: "test",
Anton Hansson4fe970f2020-10-09 10:16:49 +0100260 extends: apiScopeSystem,
Paul Duffin3375e352020-04-28 10:44:03 +0100261 legacyEnabledStatus: (*SdkLibrary).generateTestAndSystemScopesByDefault,
262 scopeSpecificProperties: func(module *SdkLibrary) *ApiScopeProperties {
263 return &module.sdkLibraryProperties.Test
264 },
Paul Duffin15f34ef2020-07-20 18:04:44 +0100265 apiFilePrefix: "test-",
266 moduleSuffix: ".test",
267 sdkVersion: "test_current",
268 annotation: "android.annotation.TestApi",
269 unstable: true,
Paul Duffind1b3a922020-01-22 11:57:20 +0000270 })
Paul Duffin8f265b92020-04-28 14:13:56 +0100271 apiScopeModuleLib = initApiScope(&apiScope{
Paul Duffin6b836ba2020-05-13 19:19:49 +0100272 name: "module-lib",
Paul Duffin8f265b92020-04-28 14:13:56 +0100273 extends: apiScopeSystem,
Paul Duffin0c5bae52020-06-02 13:00:08 +0100274 // The module-lib scope is disabled by default in legacy mode.
Paul Duffin8f265b92020-04-28 14:13:56 +0100275 //
276 // Enabling this would break existing usages.
277 legacyEnabledStatus: func(module *SdkLibrary) bool {
278 return false
279 },
280 scopeSpecificProperties: func(module *SdkLibrary) *ApiScopeProperties {
281 return &module.sdkLibraryProperties.Module_lib
282 },
283 apiFilePrefix: "module-lib-",
284 moduleSuffix: ".module_lib",
285 sdkVersion: "module_current",
Paul Duffin15f34ef2020-07-20 18:04:44 +0100286 annotation: "android.annotation.SystemApi(client=android.annotation.SystemApi.Client.MODULE_LIBRARIES)",
Paul Duffin8f265b92020-04-28 14:13:56 +0100287 })
Paul Duffin0c5bae52020-06-02 13:00:08 +0100288 apiScopeSystemServer = initApiScope(&apiScope{
289 name: "system-server",
290 extends: apiScopePublic,
291 // The system-server scope is disabled by default in legacy mode.
292 //
293 // Enabling this would break existing usages.
294 legacyEnabledStatus: func(module *SdkLibrary) bool {
295 return false
296 },
297 scopeSpecificProperties: func(module *SdkLibrary) *ApiScopeProperties {
298 return &module.sdkLibraryProperties.System_server
299 },
300 apiFilePrefix: "system-server-",
301 moduleSuffix: ".system_server",
302 sdkVersion: "system_server_current",
Paul Duffin15f34ef2020-07-20 18:04:44 +0100303 annotation: "android.annotation.SystemApi(client=android.annotation.SystemApi.Client.SYSTEM_SERVER)",
304 extraArgs: []string{
305 "--hide-annotation", "android.annotation.Hide",
Paul Duffin0c5bae52020-06-02 13:00:08 +0100306 // com.android.* classes are okay in this interface"
Paul Duffin15f34ef2020-07-20 18:04:44 +0100307 "--hide", "InternalClasses",
Paul Duffin0c5bae52020-06-02 13:00:08 +0100308 },
309 })
Paul Duffind1b3a922020-01-22 11:57:20 +0000310 allApiScopes = apiScopes{
311 apiScopePublic,
312 apiScopeSystem,
313 apiScopeTest,
Paul Duffin8f265b92020-04-28 14:13:56 +0100314 apiScopeModuleLib,
Paul Duffin0c5bae52020-06-02 13:00:08 +0100315 apiScopeSystemServer,
Paul Duffind1b3a922020-01-22 11:57:20 +0000316 }
Jiyong Parkc678ad32018-04-10 13:07:10 +0900317)
318
Jiyong Park82484c02018-04-23 21:41:26 +0900319var (
320 javaSdkLibrariesLock sync.Mutex
321)
322
Jiyong Parkc678ad32018-04-10 13:07:10 +0900323// TODO: these are big features that are currently missing
Jiyong Park1be96912018-05-28 18:02:19 +0900324// 1) disallowing linking to the runtime shared lib
325// 2) HTML generation
Jiyong Parkc678ad32018-04-10 13:07:10 +0900326
327func init() {
Paul Duffin43dc1cc2019-12-19 11:18:54 +0000328 RegisterSdkLibraryBuildComponents(android.InitRegistrationContext)
Jiyong Parkc678ad32018-04-10 13:07:10 +0900329
Jiyong Park82484c02018-04-23 21:41:26 +0900330 android.RegisterMakeVarsProvider(pctx, func(ctx android.MakeVarsContext) {
331 javaSdkLibraries := javaSdkLibraries(ctx.Config())
332 sort.Strings(*javaSdkLibraries)
333 ctx.Strict("JAVA_SDK_LIBRARIES", strings.Join(*javaSdkLibraries, " "))
334 })
Paul Duffindd46f712020-02-10 13:37:10 +0000335
336 // Register sdk member types.
337 android.RegisterSdkMemberType(&sdkLibrarySdkMemberType{
338 android.SdkMemberTypeBase{
339 PropertyName: "java_sdk_libs",
340 SupportsSdk: true,
341 },
342 })
Jiyong Parkc678ad32018-04-10 13:07:10 +0900343}
344
Paul Duffin43dc1cc2019-12-19 11:18:54 +0000345func RegisterSdkLibraryBuildComponents(ctx android.RegistrationContext) {
346 ctx.RegisterModuleType("java_sdk_library", SdkLibraryFactory)
347 ctx.RegisterModuleType("java_sdk_library_import", sdkLibraryImportFactory)
348}
349
Paul Duffin3375e352020-04-28 10:44:03 +0100350// Properties associated with each api scope.
351type ApiScopeProperties struct {
352 // Indicates whether the api surface is generated.
353 //
354 // If this is set for any scope then all scopes must explicitly specify if they
355 // are enabled. This is to prevent new usages from depending on legacy behavior.
356 //
357 // Otherwise, if this is not set for any scope then the default behavior is
358 // scope specific so please refer to the scope specific property documentation.
359 Enabled *bool
Paul Duffin87a05a32020-05-12 11:50:28 +0100360
361 // The sdk_version to use for building the stubs.
362 //
363 // If not specified then it will use an sdk_version determined as follows:
364 // 1) If the sdk_version specified on the java_sdk_library is none then this
365 // will be none. This is used for java_sdk_library instances that are used
366 // to create stubs that contribute to the core_current sdk version.
367 // 2) Otherwise, it is assumed that this library extends but does not contribute
368 // directly to a specific sdk_version and so this uses the sdk_version appropriate
369 // for the api scope. e.g. public will use sdk_version: current, system will use
370 // sdk_version: system_current, etc.
371 //
372 // This does not affect the sdk_version used for either generating the stubs source
373 // or the API file. They both have to use the same sdk_version as is used for
374 // compiling the implementation library.
375 Sdk_version *string
Paul Duffin3375e352020-04-28 10:44:03 +0100376}
377
Jiyong Parkc678ad32018-04-10 13:07:10 +0900378type sdkLibraryProperties struct {
Paul Duffin5df79302020-05-16 15:52:12 +0100379 // Visibility for impl library module. If not specified then defaults to the
380 // visibility property.
381 Impl_library_visibility []string
382
Paul Duffin4911a892020-04-29 23:35:13 +0100383 // Visibility for stubs library modules. If not specified then defaults to the
384 // visibility property.
385 Stubs_library_visibility []string
386
387 // Visibility for stubs source modules. If not specified then defaults to the
388 // visibility property.
389 Stubs_source_visibility []string
390
Anton Hansson7f66efa2020-10-08 14:47:23 +0100391 // List of Java libraries that will be in the classpath when building the implementation lib
392 Impl_only_libs []string `android:"arch_variant"`
393
Sundong Ahnf043cf62018-06-25 16:04:37 +0900394 // List of Java libraries that will be in the classpath when building stubs
395 Stub_only_libs []string `android:"arch_variant"`
396
Paul Duffin7a586d32019-12-30 17:09:34 +0000397 // list of package names that will be documented and publicized as API.
398 // This allows the API to be restricted to a subset of the source files provided.
399 // If this is unspecified then all the source files will be treated as being part
400 // of the API.
Jiyong Parkc678ad32018-04-10 13:07:10 +0900401 Api_packages []string
402
Jiyong Park5a2c9d72018-05-01 22:25:41 +0900403 // list of package names that must be hidden from the API
404 Hidden_api_packages []string
405
Paul Duffin749f98f2019-12-30 17:23:46 +0000406 // the relative path to the directory containing the api specification files.
407 // Defaults to "api".
408 Api_dir *string
409
Paul Duffindfa131e2020-05-15 20:37:11 +0100410 // Determines whether a runtime implementation library is built; defaults to false.
411 //
412 // If true then it also prevents the module from being used as a shared module, i.e.
413 // it is as is shared_library: false, was set.
Paul Duffin43db9be2019-12-30 17:35:49 +0000414 Api_only *bool
415
Paul Duffin11512472019-02-11 15:55:17 +0000416 // local files that are used within user customized droiddoc options.
417 Droiddoc_option_files []string
418
419 // additional droiddoc options
420 // Available variables for substitution:
421 //
422 // $(location <label>): the path to the droiddoc_option_files with name <label>
Sundong Ahndd567f92018-07-31 17:19:11 +0900423 Droiddoc_options []string
424
Paul Duffine22c2ab2020-05-20 19:35:27 +0100425 // is set to true, Metalava will allow framework SDK to contain annotations.
426 Annotations_enabled *bool
427
Sundong Ahn054b19a2018-10-19 13:46:09 +0900428 // a list of top-level directories containing files to merge qualifier annotations
429 // (i.e. those intended to be included in the stubs written) from.
430 Merge_annotations_dirs []string
431
432 // a list of top-level directories containing Java stub files to merge show/hide annotations from.
433 Merge_inclusion_annotations_dirs []string
434
435 // If set to true, the path of dist files is apistubs/core. Defaults to false.
436 Core_lib *bool
437
Paul Duffin4f5c1ef2020-11-19 14:53:43 +0000438 // If set to true then don't create dist rules.
439 No_dist *bool
Sundong Ahn80a87b32019-05-13 15:02:50 +0900440
Paul Duffin31310252020-11-20 21:26:20 +0000441 // The stem for the artifacts that are copied to the dist, if not specified
442 // then defaults to the base module name.
443 //
444 // For each scope the following artifacts are copied to the apistubs/<scope>
445 // directory in the dist.
446 // * stubs impl jar -> <dist-stem>.jar
447 // * API specification file -> api/<dist-stem>.txt
448 // * Removed API specification file -> api/<dist-stem>-removed.txt
449 //
450 // Also used to construct the name of the filegroup (created by prebuilt_apis)
451 // that references the latest released API and remove API specification files.
452 // * API specification filegroup -> <dist-stem>.api.<scope>.latest
453 // * Removed API specification filegroup -> <dist-stem>-removed.api.<scope>.latest
454 Dist_stem *string
455
Paul Duffin3375e352020-04-28 10:44:03 +0100456 // indicates whether system and test apis should be generated.
457 Generate_system_and_test_apis bool `blueprint:"mutated"`
458
459 // The properties specific to the public api scope
460 //
461 // Unless explicitly specified by using public.enabled the public api scope is
462 // enabled by default in both legacy and non-legacy mode.
463 Public ApiScopeProperties
464
465 // The properties specific to the system api scope
466 //
467 // In legacy mode the system api scope is enabled by default when sdk_version
468 // is set to something other than "none".
469 //
470 // In non-legacy mode the system api scope is disabled by default.
471 System ApiScopeProperties
472
473 // The properties specific to the test api scope
474 //
475 // In legacy mode the test api scope is enabled by default when sdk_version
476 // is set to something other than "none".
477 //
478 // In non-legacy mode the test api scope is disabled by default.
479 Test ApiScopeProperties
Paul Duffin37e0b772019-12-30 17:20:10 +0000480
Paul Duffin0c5bae52020-06-02 13:00:08 +0100481 // The properties specific to the module-lib api scope
Paul Duffin8f265b92020-04-28 14:13:56 +0100482 //
Paul Duffin0c5bae52020-06-02 13:00:08 +0100483 // Unless explicitly specified by using test.enabled the module-lib api scope is
Paul Duffin8f265b92020-04-28 14:13:56 +0100484 // disabled by default.
485 Module_lib ApiScopeProperties
486
Paul Duffin0c5bae52020-06-02 13:00:08 +0100487 // The properties specific to the system-server api scope
488 //
489 // Unless explicitly specified by using test.enabled the module-lib api scope is
490 // disabled by default.
491 System_server ApiScopeProperties
492
Jiyong Park932cdfe2020-05-28 00:19:53 +0900493 // Determines if the stubs are preferred over the implementation library
494 // for linking, even when the client doesn't specify sdk_version. When this
495 // is set to true, such clients are provided with the widest API surface that
496 // this lib provides. Note however that this option doesn't affect the clients
497 // that are in the same APEX as this library. In that case, the clients are
498 // always linked with the implementation library. Default is false.
499 Default_to_stubs *bool
500
Paul Duffin160fe412020-05-10 19:32:20 +0100501 // Properties related to api linting.
502 Api_lint struct {
503 // Enable api linting.
504 Enabled *bool
505 }
506
Jiyong Parkc678ad32018-04-10 13:07:10 +0900507 // TODO: determines whether to create HTML doc or not
508 //Html_doc *bool
509}
510
Paul Duffin0f8faff2020-05-20 16:18:00 +0100511// Paths to outputs from java_sdk_library and java_sdk_library_import.
512//
513// Fields that are android.Paths are always set (during GenerateAndroidBuildActions).
514// OptionalPaths are always set by java_sdk_library but may not be set by
515// java_sdk_library_import as not all instances provide that information.
Paul Duffind1b3a922020-01-22 11:57:20 +0000516type scopePaths struct {
Paul Duffin0f8faff2020-05-20 16:18:00 +0100517 // The path (represented as Paths for convenience when returning) to the stubs header jar.
518 //
519 // That is the jar that is created by turbine.
520 stubsHeaderPath android.Paths
521
522 // The path (represented as Paths for convenience when returning) to the stubs implementation jar.
523 //
524 // This is not the implementation jar, it still only contains stubs.
525 stubsImplPath android.Paths
526
527 // The API specification file, e.g. system_current.txt.
528 currentApiFilePath android.OptionalPath
529
530 // The specification of API elements removed since the last release.
531 removedApiFilePath android.OptionalPath
532
533 // The stubs source jar.
534 stubsSrcJar android.OptionalPath
Paul Duffind1b3a922020-01-22 11:57:20 +0000535}
536
Paul Duffinc8782502020-04-29 20:45:27 +0100537func (paths *scopePaths) extractStubsLibraryInfoFromDependency(dep android.Module) error {
538 if lib, ok := dep.(Dependency); ok {
539 paths.stubsHeaderPath = lib.HeaderJars()
540 paths.stubsImplPath = lib.ImplementationJars()
541 return nil
542 } else {
543 return fmt.Errorf("expected module that implements Dependency, e.g. java_library")
544 }
545}
546
Paul Duffin0ff08bd2020-04-29 13:30:54 +0100547func (paths *scopePaths) treatDepAsApiStubsProvider(dep android.Module, action func(provider ApiStubsProvider)) error {
548 if apiStubsProvider, ok := dep.(ApiStubsProvider); ok {
549 action(apiStubsProvider)
Paul Duffinc8782502020-04-29 20:45:27 +0100550 return nil
551 } else {
552 return fmt.Errorf("expected module that implements ApiStubsProvider, e.g. droidstubs")
553 }
554}
555
Paul Duffin0f8faff2020-05-20 16:18:00 +0100556func (paths *scopePaths) treatDepAsApiStubsSrcProvider(dep android.Module, action func(provider ApiStubsSrcProvider)) error {
557 if apiStubsProvider, ok := dep.(ApiStubsSrcProvider); ok {
558 action(apiStubsProvider)
559 return nil
560 } else {
561 return fmt.Errorf("expected module that implements ApiStubsSrcProvider, e.g. droidstubs")
562 }
563}
564
Paul Duffin0ff08bd2020-04-29 13:30:54 +0100565func (paths *scopePaths) extractApiInfoFromApiStubsProvider(provider ApiStubsProvider) {
Paul Duffin0f8faff2020-05-20 16:18:00 +0100566 paths.currentApiFilePath = android.OptionalPathForPath(provider.ApiFilePath())
567 paths.removedApiFilePath = android.OptionalPathForPath(provider.RemovedApiFilePath())
Paul Duffin0ff08bd2020-04-29 13:30:54 +0100568}
569
570func (paths *scopePaths) extractApiInfoFromDep(dep android.Module) error {
571 return paths.treatDepAsApiStubsProvider(dep, func(provider ApiStubsProvider) {
572 paths.extractApiInfoFromApiStubsProvider(provider)
573 })
574}
575
Paul Duffin0f8faff2020-05-20 16:18:00 +0100576func (paths *scopePaths) extractStubsSourceInfoFromApiStubsProviders(provider ApiStubsSrcProvider) {
577 paths.stubsSrcJar = android.OptionalPathForPath(provider.StubsSrcJar())
Paul Duffin0ff08bd2020-04-29 13:30:54 +0100578}
579
580func (paths *scopePaths) extractStubsSourceInfoFromDep(dep android.Module) error {
Paul Duffin0f8faff2020-05-20 16:18:00 +0100581 return paths.treatDepAsApiStubsSrcProvider(dep, func(provider ApiStubsSrcProvider) {
Paul Duffin0ff08bd2020-04-29 13:30:54 +0100582 paths.extractStubsSourceInfoFromApiStubsProviders(provider)
583 })
584}
585
586func (paths *scopePaths) extractStubsSourceAndApiInfoFromApiStubsProvider(dep android.Module) error {
587 return paths.treatDepAsApiStubsProvider(dep, func(provider ApiStubsProvider) {
588 paths.extractApiInfoFromApiStubsProvider(provider)
589 paths.extractStubsSourceInfoFromApiStubsProviders(provider)
590 })
591}
592
593type commonToSdkLibraryAndImportProperties struct {
Paul Duffin1b1e8062020-05-08 13:44:43 +0100594 // The naming scheme to use for the components that this module creates.
595 //
Paul Duffinee9ad5d2020-09-11 13:04:05 +0100596 // If not specified then it defaults to "default".
Paul Duffin1b1e8062020-05-08 13:44:43 +0100597 //
598 // This is a temporary mechanism to simplify conversion from separate modules for each
599 // component that follow a different naming pattern to the default one.
600 //
601 // TODO(b/155480189) - Remove once naming inconsistencies have been resolved.
Paul Duffin0ff08bd2020-04-29 13:30:54 +0100602 Naming_scheme *string
Paul Duffindfa131e2020-05-15 20:37:11 +0100603
604 // Specifies whether this module can be used as an Android shared library; defaults
605 // to true.
606 //
607 // An Android shared library is one that can be referenced in a <uses-library> element
608 // in an AndroidManifest.xml.
609 Shared_library *bool
Paul Duffina2ae7e02020-09-11 11:55:00 +0100610
611 // Files containing information about supported java doc tags.
612 Doctag_files []string `android:"path"`
Paul Duffin0ff08bd2020-04-29 13:30:54 +0100613}
614
Paul Duffin56d44902020-01-31 13:36:25 +0000615// Common code between sdk library and sdk library import
616type commonToSdkLibraryAndImport struct {
Paul Duffinc3091c82020-05-08 14:16:20 +0100617 moduleBase *android.ModuleBase
618
Paul Duffin56d44902020-01-31 13:36:25 +0000619 scopePaths map[*apiScope]*scopePaths
Paul Duffin1b1e8062020-05-08 13:44:43 +0100620
621 namingScheme sdkLibraryComponentNamingScheme
622
Paul Duffindfa131e2020-05-15 20:37:11 +0100623 commonSdkLibraryProperties commonToSdkLibraryAndImportProperties
Paul Duffin859fe962020-05-15 10:20:31 +0100624
Paul Duffina2ae7e02020-09-11 11:55:00 +0100625 // Paths to commonSdkLibraryProperties.Doctag_files
626 doctagPaths android.Paths
627
Paul Duffin859fe962020-05-15 10:20:31 +0100628 // Functionality related to this being used as a component of a java_sdk_library.
629 EmbeddableSdkLibraryComponent
Paul Duffin56d44902020-01-31 13:36:25 +0000630}
631
Paul Duffinc3091c82020-05-08 14:16:20 +0100632func (c *commonToSdkLibraryAndImport) initCommon(moduleBase *android.ModuleBase) {
633 c.moduleBase = moduleBase
Paul Duffin1b1e8062020-05-08 13:44:43 +0100634
Paul Duffindfa131e2020-05-15 20:37:11 +0100635 moduleBase.AddProperties(&c.commonSdkLibraryProperties)
Paul Duffin859fe962020-05-15 10:20:31 +0100636
637 // Initialize this as an sdk library component.
638 c.initSdkLibraryComponent(moduleBase)
Paul Duffin1b1e8062020-05-08 13:44:43 +0100639}
640
641func (c *commonToSdkLibraryAndImport) initCommonAfterDefaultsApplied(ctx android.DefaultableHookContext) bool {
Paul Duffindfa131e2020-05-15 20:37:11 +0100642 schemeProperty := proptools.StringDefault(c.commonSdkLibraryProperties.Naming_scheme, "default")
Paul Duffin1b1e8062020-05-08 13:44:43 +0100643 switch schemeProperty {
644 case "default":
645 c.namingScheme = &defaultNamingScheme{}
646 default:
647 ctx.PropertyErrorf("naming_scheme", "expected 'default' but was %q", schemeProperty)
648 return false
649 }
650
Paul Duffindfa131e2020-05-15 20:37:11 +0100651 // Only track this sdk library if this can be used as a shared library.
652 if c.sharedLibrary() {
653 // Use the name specified in the module definition as the owner.
654 c.sdkLibraryComponentProperties.SdkLibraryToImplicitlyTrack = proptools.StringPtr(c.moduleBase.BaseModuleName())
655 }
Paul Duffin859fe962020-05-15 10:20:31 +0100656
Paul Duffin1b1e8062020-05-08 13:44:43 +0100657 return true
Paul Duffinc3091c82020-05-08 14:16:20 +0100658}
659
Paul Duffina2ae7e02020-09-11 11:55:00 +0100660func (c *commonToSdkLibraryAndImport) generateCommonBuildActions(ctx android.ModuleContext) {
661 c.doctagPaths = android.PathsForModuleSrc(ctx, c.commonSdkLibraryProperties.Doctag_files)
662}
663
Paul Duffineedc5d52020-06-12 17:46:39 +0100664// Module name of the runtime implementation library
665func (c *commonToSdkLibraryAndImport) implLibraryModuleName() string {
666 return c.moduleBase.BaseModuleName() + ".impl"
667}
668
669// Module name of the XML file for the lib
670func (c *commonToSdkLibraryAndImport) xmlPermissionsModuleName() string {
671 return c.moduleBase.BaseModuleName() + sdkXmlFileSuffix
672}
673
Paul Duffinc3091c82020-05-08 14:16:20 +0100674// Name of the java_library module that compiles the stubs source.
675func (c *commonToSdkLibraryAndImport) stubsLibraryModuleName(apiScope *apiScope) string {
Paul Duffin1b1e8062020-05-08 13:44:43 +0100676 return c.namingScheme.stubsLibraryModuleName(apiScope, c.moduleBase.BaseModuleName())
Paul Duffinc3091c82020-05-08 14:16:20 +0100677}
678
679// Name of the droidstubs module that generates the stubs source and may also
680// generate/check the API.
681func (c *commonToSdkLibraryAndImport) stubsSourceModuleName(apiScope *apiScope) string {
Paul Duffin1b1e8062020-05-08 13:44:43 +0100682 return c.namingScheme.stubsSourceModuleName(apiScope, c.moduleBase.BaseModuleName())
Paul Duffinc3091c82020-05-08 14:16:20 +0100683}
684
685// Name of the droidstubs module that generates/checks the API. Only used if it
686// requires different arts to the stubs source generating module.
687func (c *commonToSdkLibraryAndImport) apiModuleName(apiScope *apiScope) string {
Paul Duffin1b1e8062020-05-08 13:44:43 +0100688 return c.namingScheme.apiModuleName(apiScope, c.moduleBase.BaseModuleName())
Paul Duffinc3091c82020-05-08 14:16:20 +0100689}
690
Paul Duffin46dc45a2020-05-14 15:39:10 +0100691// The component names for different outputs of the java_sdk_library.
692//
693// They are similar to the names used for the child modules it creates
694const (
695 stubsSourceComponentName = "stubs.source"
696
697 apiTxtComponentName = "api.txt"
698
699 removedApiTxtComponentName = "removed-api.txt"
700)
701
702// A regular expression to match tags that reference a specific stubs component.
703//
704// It will only match if given a valid scope and a valid component. It is verfy strict
705// to ensure it does not accidentally match a similar looking tag that should be processed
706// by the embedded Library.
707var tagSplitter = func() *regexp.Regexp {
708 // Given a list of literal string items returns a regular expression that will
709 // match any one of the items.
710 choice := func(items ...string) string {
711 return `\Q` + strings.Join(items, `\E|\Q`) + `\E`
712 }
713
714 // Regular expression to match one of the scopes.
715 scopesRegexp := choice(allScopeNames...)
716
717 // Regular expression to match one of the components.
718 componentsRegexp := choice(stubsSourceComponentName, apiTxtComponentName, removedApiTxtComponentName)
719
720 // Regular expression to match any combination of one scope and one component.
721 return regexp.MustCompile(fmt.Sprintf(`^\.(%s)\.(%s)$`, scopesRegexp, componentsRegexp))
722}()
723
724// For OutputFileProducer interface
725//
726// .<scope>.stubs.source
727// .<scope>.api.txt
728// .<scope>.removed-api.txt
729func (c *commonToSdkLibraryAndImport) commonOutputFiles(tag string) (android.Paths, error) {
730 if groups := tagSplitter.FindStringSubmatch(tag); groups != nil {
731 scopeName := groups[1]
732 component := groups[2]
733
734 if scope, ok := scopeByName[scopeName]; ok {
735 paths := c.findScopePaths(scope)
736 if paths == nil {
737 return nil, fmt.Errorf("%q does not provide api scope %s", c.moduleBase.BaseModuleName(), scopeName)
738 }
739
740 switch component {
741 case stubsSourceComponentName:
742 if paths.stubsSrcJar.Valid() {
743 return android.Paths{paths.stubsSrcJar.Path()}, nil
744 }
745
746 case apiTxtComponentName:
747 if paths.currentApiFilePath.Valid() {
748 return android.Paths{paths.currentApiFilePath.Path()}, nil
749 }
750
751 case removedApiTxtComponentName:
752 if paths.removedApiFilePath.Valid() {
753 return android.Paths{paths.removedApiFilePath.Path()}, nil
754 }
755 }
756
757 return nil, fmt.Errorf("%s not available for api scope %s", component, scopeName)
758 } else {
759 return nil, fmt.Errorf("unknown scope %s in %s", scope, tag)
760 }
761
762 } else {
Paul Duffina2ae7e02020-09-11 11:55:00 +0100763 switch tag {
764 case ".doctags":
765 if c.doctagPaths != nil {
766 return c.doctagPaths, nil
767 } else {
768 return nil, fmt.Errorf("no doctag_files specified on %s", c.moduleBase.BaseModuleName())
769 }
770 }
Paul Duffin46dc45a2020-05-14 15:39:10 +0100771 return nil, nil
772 }
773}
774
Paul Duffin803a9562020-05-20 11:52:25 +0100775func (c *commonToSdkLibraryAndImport) getScopePathsCreateIfNeeded(scope *apiScope) *scopePaths {
Paul Duffin56d44902020-01-31 13:36:25 +0000776 if c.scopePaths == nil {
777 c.scopePaths = make(map[*apiScope]*scopePaths)
778 }
779 paths := c.scopePaths[scope]
780 if paths == nil {
781 paths = &scopePaths{}
782 c.scopePaths[scope] = paths
783 }
784
785 return paths
786}
787
Paul Duffin803a9562020-05-20 11:52:25 +0100788func (c *commonToSdkLibraryAndImport) findScopePaths(scope *apiScope) *scopePaths {
789 if c.scopePaths == nil {
790 return nil
791 }
792
793 return c.scopePaths[scope]
794}
795
796// If this does not support the requested api scope then find the closest available
797// scope it does support. Returns nil if no such scope is available.
798func (c *commonToSdkLibraryAndImport) findClosestScopePath(scope *apiScope) *scopePaths {
799 for s := scope; s != nil; s = s.extends {
800 if paths := c.findScopePaths(s); paths != nil {
801 return paths
802 }
803 }
804
805 // This should never happen outside tests as public should be the base scope for every
806 // scope and is enabled by default.
807 return nil
808}
809
Paul Duffin23970f42020-05-20 14:20:02 +0100810func (c *commonToSdkLibraryAndImport) selectHeaderJarsForSdkVersion(ctx android.BaseModuleContext, sdkVersion sdkSpec) android.Paths {
Paul Duffinb05d4292020-05-20 12:19:10 +0100811
812 // If a specific numeric version has been requested then use prebuilt versions of the sdk.
813 if sdkVersion.version.isNumbered() {
814 return PrebuiltJars(ctx, c.moduleBase.BaseModuleName(), sdkVersion)
815 }
816
817 var apiScope *apiScope
818 switch sdkVersion.kind {
819 case sdkSystem:
820 apiScope = apiScopeSystem
Paul Duffin803a9562020-05-20 11:52:25 +0100821 case sdkModule:
822 apiScope = apiScopeModuleLib
Paul Duffinb05d4292020-05-20 12:19:10 +0100823 case sdkTest:
824 apiScope = apiScopeTest
Paul Duffin0c5bae52020-06-02 13:00:08 +0100825 case sdkSystemServer:
826 apiScope = apiScopeSystemServer
Paul Duffinb05d4292020-05-20 12:19:10 +0100827 default:
828 apiScope = apiScopePublic
829 }
830
Paul Duffin803a9562020-05-20 11:52:25 +0100831 paths := c.findClosestScopePath(apiScope)
832 if paths == nil {
833 var scopes []string
834 for _, s := range allApiScopes {
835 if c.findScopePaths(s) != nil {
836 scopes = append(scopes, s.name)
837 }
838 }
839 ctx.ModuleErrorf("requires api scope %s from %s but it only has %q available", apiScope.name, c.moduleBase.BaseModuleName(), scopes)
840 return nil
841 }
842
Paul Duffin23970f42020-05-20 14:20:02 +0100843 return paths.stubsHeaderPath
Paul Duffinb05d4292020-05-20 12:19:10 +0100844}
845
Paul Duffin859fe962020-05-15 10:20:31 +0100846func (c *commonToSdkLibraryAndImport) sdkComponentPropertiesForChildLibrary() interface{} {
847 componentProps := &struct {
848 SdkLibraryToImplicitlyTrack *string
Paul Duffindfa131e2020-05-15 20:37:11 +0100849 }{}
850
851 if c.sharedLibrary() {
Paul Duffin859fe962020-05-15 10:20:31 +0100852 // Mark the stubs library as being components of this java_sdk_library so that
853 // any app that includes code which depends (directly or indirectly) on the stubs
854 // library will have the appropriate <uses-library> invocation inserted into its
855 // manifest if necessary.
Paul Duffindfa131e2020-05-15 20:37:11 +0100856 componentProps.SdkLibraryToImplicitlyTrack = proptools.StringPtr(c.moduleBase.BaseModuleName())
Paul Duffin859fe962020-05-15 10:20:31 +0100857 }
858
859 return componentProps
860}
861
Paul Duffindfa131e2020-05-15 20:37:11 +0100862// Check if this can be used as a shared library.
863func (c *commonToSdkLibraryAndImport) sharedLibrary() bool {
864 return proptools.BoolDefault(c.commonSdkLibraryProperties.Shared_library, true)
865}
866
Paul Duffin859fe962020-05-15 10:20:31 +0100867// Properties related to the use of a module as an component of a java_sdk_library.
868type SdkLibraryComponentProperties struct {
869
870 // The name of the java_sdk_library/_import to add to a <uses-library> entry
871 // in the AndroidManifest.xml of any Android app that includes code that references
872 // this module. If not set then no java_sdk_library/_import is tracked.
873 SdkLibraryToImplicitlyTrack *string `blueprint:"mutated"`
874}
875
876// Structure to be embedded in a module struct that needs to support the
877// SdkLibraryComponentDependency interface.
878type EmbeddableSdkLibraryComponent struct {
879 sdkLibraryComponentProperties SdkLibraryComponentProperties
880}
881
882func (e *EmbeddableSdkLibraryComponent) initSdkLibraryComponent(moduleBase *android.ModuleBase) {
883 moduleBase.AddProperties(&e.sdkLibraryComponentProperties)
884}
885
886// to satisfy SdkLibraryComponentDependency
Ulya Trafimovich31e444e2020-08-14 17:32:16 +0100887func (e *EmbeddableSdkLibraryComponent) OptionalImplicitSdkLibrary() *string {
888 return e.sdkLibraryComponentProperties.SdkLibraryToImplicitlyTrack
Paul Duffin859fe962020-05-15 10:20:31 +0100889}
890
Ulya Trafimovich39b437b2020-09-23 16:42:35 +0100891// to satisfy SdkLibraryComponentDependency
892func (e *EmbeddableSdkLibraryComponent) OptionalSdkLibraryImplementation() *string {
893 // Currently implementation library name is the same as the SDK library name.
894 return e.sdkLibraryComponentProperties.SdkLibraryToImplicitlyTrack
895}
896
Paul Duffin859fe962020-05-15 10:20:31 +0100897// Implemented by modules that are (or possibly could be) a component of a java_sdk_library
898// (including the java_sdk_library) itself.
899type SdkLibraryComponentDependency interface {
Ulya Trafimovich31e444e2020-08-14 17:32:16 +0100900 UsesLibraryDependency
901
Paul Duffin859fe962020-05-15 10:20:31 +0100902 // The optional name of the sdk library that should be implicitly added to the
903 // AndroidManifest of an app that contains code which references the sdk library.
904 //
Ulya Trafimovich31e444e2020-08-14 17:32:16 +0100905 // Returns the name of the optional implicit SDK library or nil, if there isn't one.
906 OptionalImplicitSdkLibrary() *string
Ulya Trafimovich39b437b2020-09-23 16:42:35 +0100907
908 // The name of the implementation library for the optional SDK library or nil, if there isn't one.
909 OptionalSdkLibraryImplementation() *string
Paul Duffin859fe962020-05-15 10:20:31 +0100910}
911
912// Make sure that all the module types that are components of java_sdk_library/_import
913// and which can be referenced (directly or indirectly) from an android app implement
914// the SdkLibraryComponentDependency interface.
915var _ SdkLibraryComponentDependency = (*Library)(nil)
916var _ SdkLibraryComponentDependency = (*Import)(nil)
917var _ SdkLibraryComponentDependency = (*SdkLibrary)(nil)
Paul Duffineedc5d52020-06-12 17:46:39 +0100918var _ SdkLibraryComponentDependency = (*SdkLibraryImport)(nil)
Paul Duffin859fe962020-05-15 10:20:31 +0100919
920// Provides access to sdk_version related header and implentation jars.
921type SdkLibraryDependency interface {
922 SdkLibraryComponentDependency
923
924 // Get the header jars appropriate for the supplied sdk_version.
925 //
926 // These are turbine generated jars so they only change if the externals of the
927 // class changes but it does not contain and implementation or JavaDoc.
928 SdkHeaderJars(ctx android.BaseModuleContext, sdkVersion sdkSpec) android.Paths
929
930 // Get the implementation jars appropriate for the supplied sdk version.
931 //
932 // These are either the implementation jar for the whole sdk library or the implementation
933 // jars for the stubs. The latter should only be needed when generating JavaDoc as otherwise
934 // they are identical to the corresponding header jars.
935 SdkImplementationJars(ctx android.BaseModuleContext, sdkVersion sdkSpec) android.Paths
936}
937
Inseob Kimc0907f12019-02-08 21:00:45 +0900938type SdkLibrary struct {
Sundong Ahn054b19a2018-10-19 13:46:09 +0900939 Library
Jiyong Parkc678ad32018-04-10 13:07:10 +0900940
Sundong Ahn054b19a2018-10-19 13:46:09 +0900941 sdkLibraryProperties sdkLibraryProperties
Jiyong Parkc678ad32018-04-10 13:07:10 +0900942
Paul Duffin3375e352020-04-28 10:44:03 +0100943 // Map from api scope to the scope specific property structure.
944 scopeToProperties map[*apiScope]*ApiScopeProperties
945
Paul Duffin56d44902020-01-31 13:36:25 +0000946 commonToSdkLibraryAndImport
Jiyong Parkc678ad32018-04-10 13:07:10 +0900947}
948
Inseob Kimc0907f12019-02-08 21:00:45 +0900949var _ Dependency = (*SdkLibrary)(nil)
950var _ SdkLibraryDependency = (*SdkLibrary)(nil)
Colin Cross897d2ed2019-02-11 14:03:51 -0800951
Paul Duffin3375e352020-04-28 10:44:03 +0100952func (module *SdkLibrary) generateTestAndSystemScopesByDefault() bool {
953 return module.sdkLibraryProperties.Generate_system_and_test_apis
954}
955
956func (module *SdkLibrary) getGeneratedApiScopes(ctx android.EarlyModuleContext) apiScopes {
957 // Check to see if any scopes have been explicitly enabled. If any have then all
958 // must be.
959 anyScopesExplicitlyEnabled := false
960 for _, scope := range allApiScopes {
961 scopeProperties := module.scopeToProperties[scope]
962 if scopeProperties.Enabled != nil {
963 anyScopesExplicitlyEnabled = true
964 break
965 }
Paul Duffind1b3a922020-01-22 11:57:20 +0000966 }
Paul Duffin3375e352020-04-28 10:44:03 +0100967
968 var generatedScopes apiScopes
969 enabledScopes := make(map[*apiScope]struct{})
970 for _, scope := range allApiScopes {
971 scopeProperties := module.scopeToProperties[scope]
972 // If any scopes are explicitly enabled then ignore the legacy enabled status.
973 // This is to ensure that any new usages of this module type do not rely on legacy
974 // behaviour.
975 defaultEnabledStatus := false
976 if anyScopesExplicitlyEnabled {
977 defaultEnabledStatus = scope.defaultEnabledStatus
978 } else {
979 defaultEnabledStatus = scope.legacyEnabledStatus(module)
980 }
981 enabled := proptools.BoolDefault(scopeProperties.Enabled, defaultEnabledStatus)
982 if enabled {
983 enabledScopes[scope] = struct{}{}
984 generatedScopes = append(generatedScopes, scope)
985 }
986 }
987
988 // Now check to make sure that any scope that is extended by an enabled scope is also
989 // enabled.
990 for _, scope := range allApiScopes {
991 if _, ok := enabledScopes[scope]; ok {
992 extends := scope.extends
993 if extends != nil {
994 if _, ok := enabledScopes[extends]; !ok {
995 ctx.ModuleErrorf("enabled api scope %q depends on disabled scope %q", scope, extends)
996 }
997 }
998 }
999 }
1000
1001 return generatedScopes
Paul Duffind1b3a922020-01-22 11:57:20 +00001002}
1003
Paul Duffineedc5d52020-06-12 17:46:39 +01001004type sdkLibraryComponentTag struct {
1005 blueprint.BaseDependencyTag
1006 name string
1007}
1008
1009// Mark this tag so dependencies that use it are excluded from visibility enforcement.
1010func (t sdkLibraryComponentTag) ExcludeFromVisibilityEnforcement() {}
1011
1012var xmlPermissionsFileTag = sdkLibraryComponentTag{name: "xml-permissions-file"}
Paul Duffine74ac732020-02-06 13:51:46 +00001013
Jiyong Parke3833882020-02-17 17:28:10 +09001014func IsXmlPermissionsFileDepTag(depTag blueprint.DependencyTag) bool {
Paul Duffineedc5d52020-06-12 17:46:39 +01001015 if dt, ok := depTag.(sdkLibraryComponentTag); ok {
Jiyong Parke3833882020-02-17 17:28:10 +09001016 return dt == xmlPermissionsFileTag
1017 }
1018 return false
1019}
1020
Paul Duffineedc5d52020-06-12 17:46:39 +01001021var implLibraryTag = sdkLibraryComponentTag{name: "impl-library"}
Paul Duffin5df79302020-05-16 15:52:12 +01001022
Paul Duffin44f1d842020-06-26 20:17:02 +01001023// Add the dependencies on the child modules in the component deps mutator.
1024func (module *SdkLibrary) ComponentDepsMutator(ctx android.BottomUpMutatorContext) {
Paul Duffin3375e352020-04-28 10:44:03 +01001025 for _, apiScope := range module.getGeneratedApiScopes(ctx) {
Paul Duffind1b3a922020-01-22 11:57:20 +00001026 // Add dependencies to the stubs library
Paul Duffinc3091c82020-05-08 14:16:20 +01001027 ctx.AddVariationDependencies(nil, apiScope.stubsTag, module.stubsLibraryModuleName(apiScope))
Paul Duffind1b3a922020-01-22 11:57:20 +00001028
Paul Duffin15f34ef2020-07-20 18:04:44 +01001029 // Add a dependency on the stubs source in order to access both stubs source and api information.
1030 ctx.AddVariationDependencies(nil, apiScope.stubsSourceAndApiTag, module.stubsSourceModuleName(apiScope))
Sundong Ahn054b19a2018-10-19 13:46:09 +09001031 }
1032
Paul Duffindfa131e2020-05-15 20:37:11 +01001033 if module.requiresRuntimeImplementationLibrary() {
Paul Duffin5df79302020-05-16 15:52:12 +01001034 // Add dependency to the rule for generating the implementation library.
1035 ctx.AddDependency(module, implLibraryTag, module.implLibraryModuleName())
1036
Paul Duffindfa131e2020-05-15 20:37:11 +01001037 if module.sharedLibrary() {
1038 // Add dependency to the rule for generating the xml permissions file
Paul Duffineedc5d52020-06-12 17:46:39 +01001039 ctx.AddDependency(module, xmlPermissionsFileTag, module.xmlPermissionsModuleName())
Paul Duffindfa131e2020-05-15 20:37:11 +01001040 }
Paul Duffin44f1d842020-06-26 20:17:02 +01001041 }
1042}
Paul Duffine74ac732020-02-06 13:51:46 +00001043
Paul Duffin44f1d842020-06-26 20:17:02 +01001044// Add other dependencies as normal.
1045func (module *SdkLibrary) DepsMutator(ctx android.BottomUpMutatorContext) {
1046 if module.requiresRuntimeImplementationLibrary() {
Paul Duffindfa131e2020-05-15 20:37:11 +01001047 // Only add the deps for the library if it is actually going to be built.
1048 module.Library.deps(ctx)
1049 }
Jiyong Parkc678ad32018-04-10 13:07:10 +09001050}
1051
Paul Duffin46dc45a2020-05-14 15:39:10 +01001052func (module *SdkLibrary) OutputFiles(tag string) (android.Paths, error) {
1053 paths, err := module.commonOutputFiles(tag)
1054 if paths == nil && err == nil {
1055 return module.Library.OutputFiles(tag)
1056 } else {
1057 return paths, err
1058 }
1059}
1060
Inseob Kimc0907f12019-02-08 21:00:45 +09001061func (module *SdkLibrary) GenerateAndroidBuildActions(ctx android.ModuleContext) {
Paul Duffina2ae7e02020-09-11 11:55:00 +01001062 module.generateCommonBuildActions(ctx)
1063
Paul Duffindfa131e2020-05-15 20:37:11 +01001064 // Only build an implementation library if required.
1065 if module.requiresRuntimeImplementationLibrary() {
Paul Duffin43db9be2019-12-30 17:35:49 +00001066 module.Library.GenerateAndroidBuildActions(ctx)
1067 }
Sundong Ahn054b19a2018-10-19 13:46:09 +09001068
Sundong Ahn57368eb2018-07-06 11:20:23 +09001069 // Record the paths to the header jars of the library (stubs and impl).
Paul Duffind1b3a922020-01-22 11:57:20 +00001070 // When this java_sdk_library is depended upon from others via "libs" property,
Jiyong Parkc678ad32018-04-10 13:07:10 +09001071 // the recorded paths will be returned depending on the link type of the caller.
1072 ctx.VisitDirectDeps(func(to android.Module) {
Jiyong Parkc678ad32018-04-10 13:07:10 +09001073 tag := ctx.OtherModuleDependencyTag(to)
1074
Paul Duffinc8782502020-04-29 20:45:27 +01001075 // Extract information from any of the scope specific dependencies.
1076 if scopeTag, ok := tag.(scopeDependencyTag); ok {
1077 apiScope := scopeTag.apiScope
Paul Duffin803a9562020-05-20 11:52:25 +01001078 scopePaths := module.getScopePathsCreateIfNeeded(apiScope)
Paul Duffinc8782502020-04-29 20:45:27 +01001079
1080 // Extract information from the dependency. The exact information extracted
1081 // is determined by the nature of the dependency which is determined by the tag.
1082 scopeTag.extractDepInfo(ctx, to, scopePaths)
Sundong Ahn20e998b2018-07-24 11:19:26 +09001083 }
Jiyong Parkc678ad32018-04-10 13:07:10 +09001084 })
1085}
1086
Jiyong Park0b0e1b92019-12-03 13:24:29 +09001087func (module *SdkLibrary) AndroidMkEntries() []android.AndroidMkEntries {
Paul Duffindfa131e2020-05-15 20:37:11 +01001088 if !module.requiresRuntimeImplementationLibrary() {
Paul Duffin43db9be2019-12-30 17:35:49 +00001089 return nil
1090 }
Jiyong Park0b0e1b92019-12-03 13:24:29 +09001091 entriesList := module.Library.AndroidMkEntries()
Yo Chiang07d75072020-06-05 17:43:19 +08001092 if module.sharedLibrary() {
1093 entries := &entriesList[0]
1094 entries.Required = append(entries.Required, module.xmlPermissionsModuleName())
1095 }
Jiyong Park0b0e1b92019-12-03 13:24:29 +09001096 return entriesList
Jiyong Park82484c02018-04-23 21:41:26 +09001097}
1098
Anton Hansson5fd5d242020-03-27 19:43:19 +00001099// The dist path of the stub artifacts
1100func (module *SdkLibrary) apiDistPath(apiScope *apiScope) string {
1101 if module.ModuleBase.Owner() != "" {
1102 return path.Join("apistubs", module.ModuleBase.Owner(), apiScope.name)
1103 } else if Bool(module.sdkLibraryProperties.Core_lib) {
1104 return path.Join("apistubs", "core", apiScope.name)
1105 } else {
1106 return path.Join("apistubs", "android", apiScope.name)
1107 }
1108}
1109
Paul Duffin12ceb462019-12-24 20:31:31 +00001110// Get the sdk version for use when compiling the stubs library.
Paul Duffin780c5f42020-05-12 15:52:55 +01001111func (module *SdkLibrary) sdkVersionForStubsLibrary(mctx android.EarlyModuleContext, apiScope *apiScope) string {
Paul Duffin87a05a32020-05-12 11:50:28 +01001112 scopeProperties := module.scopeToProperties[apiScope]
1113 if scopeProperties.Sdk_version != nil {
1114 return proptools.String(scopeProperties.Sdk_version)
1115 }
1116
Paul Duffin12ceb462019-12-24 20:31:31 +00001117 sdkDep := decodeSdkDep(mctx, sdkContext(&module.Library))
1118 if sdkDep.hasStandardLibs() {
1119 // If building against a standard sdk then use the sdk version appropriate for the scope.
Paul Duffind1b3a922020-01-22 11:57:20 +00001120 return apiScope.sdkVersion
Paul Duffin12ceb462019-12-24 20:31:31 +00001121 } else {
1122 // Otherwise, use no system module.
1123 return "none"
1124 }
1125}
1126
Paul Duffin31310252020-11-20 21:26:20 +00001127func (module *SdkLibrary) distStem() string {
1128 return proptools.StringDefault(module.sdkLibraryProperties.Dist_stem, module.BaseModuleName())
1129}
1130
Paul Duffind1b3a922020-01-22 11:57:20 +00001131func (module *SdkLibrary) latestApiFilegroupName(apiScope *apiScope) string {
Paul Duffin31310252020-11-20 21:26:20 +00001132 return ":" + module.distStem() + ".api." + apiScope.name + ".latest"
Jiyong Park58c518b2018-05-12 22:29:12 +09001133}
Jiyong Parkc678ad32018-04-10 13:07:10 +09001134
Paul Duffind1b3a922020-01-22 11:57:20 +00001135func (module *SdkLibrary) latestRemovedApiFilegroupName(apiScope *apiScope) string {
Paul Duffin31310252020-11-20 21:26:20 +00001136 return ":" + module.distStem() + "-removed.api." + apiScope.name + ".latest"
Jiyong Parkc678ad32018-04-10 13:07:10 +09001137}
1138
Anton Hansson944e77d2020-08-19 11:40:22 +01001139func childModuleVisibility(childVisibility []string) []string {
1140 if childVisibility == nil {
1141 // No child visibility set. The child will use the visibility of the sdk_library.
1142 return nil
1143 }
1144
1145 // Prepend an override to ignore the sdk_library's visibility, and rely on the child visibility.
1146 var visibility []string
1147 visibility = append(visibility, "//visibility:override")
1148 visibility = append(visibility, childVisibility...)
1149 return visibility
1150}
1151
Paul Duffin5df79302020-05-16 15:52:12 +01001152// Creates the implementation java library
1153func (module *SdkLibrary) createImplLibrary(mctx android.DefaultableHookContext) {
Paul Duffina2058f82020-06-24 16:22:38 +01001154 moduleNamePtr := proptools.StringPtr(module.BaseModuleName())
1155
Anton Hansson944e77d2020-08-19 11:40:22 +01001156 visibility := childModuleVisibility(module.sdkLibraryProperties.Impl_library_visibility)
1157
Paul Duffin5df79302020-05-16 15:52:12 +01001158 props := struct {
Paul Duffina2058f82020-06-24 16:22:38 +01001159 Name *string
1160 Visibility []string
1161 Instrument bool
Anton Hansson7f66efa2020-10-08 14:47:23 +01001162 Libs []string
Paul Duffina2058f82020-06-24 16:22:38 +01001163 ConfigurationName *string
Paul Duffin5df79302020-05-16 15:52:12 +01001164 }{
1165 Name: proptools.StringPtr(module.implLibraryModuleName()),
Anton Hansson944e77d2020-08-19 11:40:22 +01001166 Visibility: visibility,
Paul Duffin296cf332020-06-18 21:09:55 +01001167 // Set the instrument property to ensure it is instrumented when instrumentation is required.
1168 Instrument: true,
Anton Hansson7f66efa2020-10-08 14:47:23 +01001169 // Set the impl_only libs. Note that the module's "Libs" get appended as well, via the
1170 // addition of &module.properties below.
1171 Libs: module.sdkLibraryProperties.Impl_only_libs,
Paul Duffina2058f82020-06-24 16:22:38 +01001172
1173 // Make the created library behave as if it had the same name as this module.
1174 ConfigurationName: moduleNamePtr,
Paul Duffin5df79302020-05-16 15:52:12 +01001175 }
1176
1177 properties := []interface{}{
1178 &module.properties,
1179 &module.protoProperties,
1180 &module.deviceProperties,
Liz Kammera7a64f32020-07-09 15:16:41 -07001181 &module.dexProperties,
Paul Duffin5df79302020-05-16 15:52:12 +01001182 &module.dexpreoptProperties,
Colin Cross014489c2020-06-02 20:09:13 -07001183 &module.linter.properties,
Paul Duffin5df79302020-05-16 15:52:12 +01001184 &props,
1185 module.sdkComponentPropertiesForChildLibrary(),
1186 }
1187 mctx.CreateModule(LibraryFactory, properties...)
1188}
1189
Jiyong Parkc678ad32018-04-10 13:07:10 +09001190// Creates a static java library that has API stubs
Paul Duffinf0229202020-04-29 16:47:28 +01001191func (module *SdkLibrary) createStubsLibrary(mctx android.DefaultableHookContext, apiScope *apiScope) {
Jiyong Parkc678ad32018-04-10 13:07:10 +09001192 props := struct {
Dan Willemsen9f435972020-05-28 15:28:00 -07001193 Name *string
1194 Visibility []string
1195 Srcs []string
1196 Installable *bool
1197 Sdk_version *string
1198 System_modules *string
1199 Patch_module *string
1200 Libs []string
1201 Compile_dex *bool
1202 Java_version *string
1203 Openjdk9 struct {
Sundong Ahn054b19a2018-10-19 13:46:09 +09001204 Srcs []string
1205 Javacflags []string
1206 }
Anton Hansson5fd5d242020-03-27 19:43:19 +00001207 Dist struct {
1208 Targets []string
1209 Dest *string
1210 Dir *string
1211 Tag *string
1212 }
Jiyong Parkc678ad32018-04-10 13:07:10 +09001213 }{}
1214
Paul Duffinc3091c82020-05-08 14:16:20 +01001215 props.Name = proptools.StringPtr(module.stubsLibraryModuleName(apiScope))
Anton Hansson944e77d2020-08-19 11:40:22 +01001216 props.Visibility = childModuleVisibility(module.sdkLibraryProperties.Stubs_library_visibility)
Jiyong Parkc678ad32018-04-10 13:07:10 +09001217 // sources are generated from the droiddoc
Paul Duffinc3091c82020-05-08 14:16:20 +01001218 props.Srcs = []string{":" + module.stubsSourceModuleName(apiScope)}
Paul Duffin12ceb462019-12-24 20:31:31 +00001219 sdkVersion := module.sdkVersionForStubsLibrary(mctx, apiScope)
Paul Duffin52d398a2019-06-11 12:31:14 +01001220 props.Sdk_version = proptools.StringPtr(sdkVersion)
Paul Duffina18abc22020-05-16 18:54:24 +01001221 props.System_modules = module.deviceProperties.System_modules
1222 props.Patch_module = module.properties.Patch_module
Paul Duffin367ab912019-12-23 19:40:36 +00001223 props.Installable = proptools.BoolPtr(false)
Sundong Ahn054b19a2018-10-19 13:46:09 +09001224 props.Libs = module.sdkLibraryProperties.Stub_only_libs
Paul Duffine22c2ab2020-05-20 19:35:27 +01001225 // The stub-annotations library contains special versions of the annotations
1226 // with CLASS retention policy, so that they're kept.
1227 if proptools.Bool(module.sdkLibraryProperties.Annotations_enabled) {
1228 props.Libs = append(props.Libs, "stub-annotations")
1229 }
Paul Duffina18abc22020-05-16 18:54:24 +01001230 props.Openjdk9.Srcs = module.properties.Openjdk9.Srcs
1231 props.Openjdk9.Javacflags = module.properties.Openjdk9.Javacflags
Anton Hansson83509b52020-05-21 09:21:57 +01001232 // We compile the stubs for 1.8 in line with the main android.jar stubs, and potential
1233 // interop with older developer tools that don't support 1.9.
1234 props.Java_version = proptools.StringPtr("1.8")
Liz Kammera7a64f32020-07-09 15:16:41 -07001235 if module.dexProperties.Compile_dex != nil {
1236 props.Compile_dex = module.dexProperties.Compile_dex
Sundong Ahndd567f92018-07-31 17:19:11 +09001237 }
Jiyong Parkc678ad32018-04-10 13:07:10 +09001238
Anton Hansson5fd5d242020-03-27 19:43:19 +00001239 // Dist the class jar artifact for sdk builds.
1240 if !Bool(module.sdkLibraryProperties.No_dist) {
1241 props.Dist.Targets = []string{"sdk", "win_sdk"}
Paul Duffin31310252020-11-20 21:26:20 +00001242 props.Dist.Dest = proptools.StringPtr(fmt.Sprintf("%v.jar", module.distStem()))
Anton Hansson5fd5d242020-03-27 19:43:19 +00001243 props.Dist.Dir = proptools.StringPtr(module.apiDistPath(apiScope))
1244 props.Dist.Tag = proptools.StringPtr(".jar")
1245 }
Jiyong Parkc678ad32018-04-10 13:07:10 +09001246
Paul Duffin859fe962020-05-15 10:20:31 +01001247 mctx.CreateModule(LibraryFactory, &props, module.sdkComponentPropertiesForChildLibrary())
Jiyong Parkc678ad32018-04-10 13:07:10 +09001248}
1249
Paul Duffin6d0886e2020-04-07 18:49:53 +01001250// Creates a droidstubs module that creates stubs source files from the given full source
Paul Duffinc8782502020-04-29 20:45:27 +01001251// files and also updates and checks the API specification files.
Paul Duffin15f34ef2020-07-20 18:04:44 +01001252func (module *SdkLibrary) createStubsSourcesAndApi(mctx android.DefaultableHookContext, apiScope *apiScope, name string, scopeSpecificDroidstubsArgs []string) {
Jiyong Parkc678ad32018-04-10 13:07:10 +09001253 props := struct {
Sundong Ahn054b19a2018-10-19 13:46:09 +09001254 Name *string
Paul Duffin4911a892020-04-29 23:35:13 +01001255 Visibility []string
Sundong Ahn054b19a2018-10-19 13:46:09 +09001256 Srcs []string
1257 Installable *bool
Paul Duffin52d398a2019-06-11 12:31:14 +01001258 Sdk_version *string
Paul Duffin12ceb462019-12-24 20:31:31 +00001259 System_modules *string
Sundong Ahn054b19a2018-10-19 13:46:09 +09001260 Libs []string
Paul Duffin6877e6d2020-09-25 19:59:14 +01001261 Output_javadoc_comments *bool
Paul Duffin11512472019-02-11 15:55:17 +00001262 Arg_files []string
Sundong Ahn054b19a2018-10-19 13:46:09 +09001263 Args *string
Sundong Ahn054b19a2018-10-19 13:46:09 +09001264 Java_version *string
Paul Duffine22c2ab2020-05-20 19:35:27 +01001265 Annotations_enabled *bool
Sundong Ahn054b19a2018-10-19 13:46:09 +09001266 Merge_annotations_dirs []string
1267 Merge_inclusion_annotations_dirs []string
Paul Duffin0ff08bd2020-04-29 13:30:54 +01001268 Generate_stubs *bool
Sundong Ahn054b19a2018-10-19 13:46:09 +09001269 Check_api struct {
Inseob Kim38449af2019-02-28 14:24:05 +09001270 Current ApiToCheck
1271 Last_released ApiToCheck
1272 Ignore_missing_latest_api *bool
Paul Duffin160fe412020-05-10 19:32:20 +01001273
1274 Api_lint struct {
1275 Enabled *bool
1276 New_since *string
1277 Baseline_file *string
1278 }
Jiyong Park58c518b2018-05-12 22:29:12 +09001279 }
Sundong Ahn1b92c822018-05-29 11:35:17 +09001280 Aidl struct {
1281 Include_dirs []string
1282 Local_include_dirs []string
1283 }
Paul Duffin040e9062020-11-23 17:41:36 +00001284 Dists []android.Dist
Jiyong Parkc678ad32018-04-10 13:07:10 +09001285 }{}
1286
Paul Duffin7b78b4d2020-04-28 14:08:32 +01001287 // The stubs source processing uses the same compile time classpath when extracting the
1288 // API from the implementation library as it does when compiling it. i.e. the same
1289 // * sdk version
1290 // * system_modules
1291 // * libs (static_libs/libs)
Paul Duffin250e6192019-06-07 10:44:37 +01001292
Paul Duffin0ff08bd2020-04-29 13:30:54 +01001293 props.Name = proptools.StringPtr(name)
Anton Hansson944e77d2020-08-19 11:40:22 +01001294 props.Visibility = childModuleVisibility(module.sdkLibraryProperties.Stubs_source_visibility)
Paul Duffina18abc22020-05-16 18:54:24 +01001295 props.Srcs = append(props.Srcs, module.properties.Srcs...)
1296 props.Sdk_version = module.deviceProperties.Sdk_version
1297 props.System_modules = module.deviceProperties.System_modules
Jiyong Parkc678ad32018-04-10 13:07:10 +09001298 props.Installable = proptools.BoolPtr(false)
Sundong Ahne6f0b052018-06-05 16:46:14 +09001299 // A droiddoc module has only one Libs property and doesn't distinguish between
1300 // shared libs and static libs. So we need to add both of these libs to Libs property.
Paul Duffina18abc22020-05-16 18:54:24 +01001301 props.Libs = module.properties.Libs
1302 props.Libs = append(props.Libs, module.properties.Static_libs...)
1303 props.Aidl.Include_dirs = module.deviceProperties.Aidl.Include_dirs
1304 props.Aidl.Local_include_dirs = module.deviceProperties.Aidl.Local_include_dirs
1305 props.Java_version = module.properties.Java_version
Jiyong Parkc678ad32018-04-10 13:07:10 +09001306
Paul Duffine22c2ab2020-05-20 19:35:27 +01001307 props.Annotations_enabled = module.sdkLibraryProperties.Annotations_enabled
Sundong Ahn054b19a2018-10-19 13:46:09 +09001308 props.Merge_annotations_dirs = module.sdkLibraryProperties.Merge_annotations_dirs
1309 props.Merge_inclusion_annotations_dirs = module.sdkLibraryProperties.Merge_inclusion_annotations_dirs
1310
Paul Duffin6d0886e2020-04-07 18:49:53 +01001311 droidstubsArgs := []string{}
Paul Duffin235ffff2019-12-24 10:41:30 +00001312 if len(module.sdkLibraryProperties.Api_packages) != 0 {
Paul Duffin6d0886e2020-04-07 18:49:53 +01001313 droidstubsArgs = append(droidstubsArgs, "--stub-packages "+strings.Join(module.sdkLibraryProperties.Api_packages, ":"))
Paul Duffin235ffff2019-12-24 10:41:30 +00001314 }
1315 if len(module.sdkLibraryProperties.Hidden_api_packages) != 0 {
Paul Duffin6d0886e2020-04-07 18:49:53 +01001316 droidstubsArgs = append(droidstubsArgs,
Paul Duffin235ffff2019-12-24 10:41:30 +00001317 android.JoinWithPrefix(module.sdkLibraryProperties.Hidden_api_packages, " --hide-package "))
1318 }
Paul Duffin6d0886e2020-04-07 18:49:53 +01001319 droidstubsArgs = append(droidstubsArgs, module.sdkLibraryProperties.Droiddoc_options...)
Paul Duffin235ffff2019-12-24 10:41:30 +00001320 disabledWarnings := []string{
1321 "MissingPermission",
1322 "BroadcastBehavior",
1323 "HiddenSuperclass",
1324 "DeprecationMismatch",
1325 "UnavailableSymbol",
1326 "SdkConstant",
1327 "HiddenTypeParameter",
1328 "Todo",
1329 "Typo",
1330 }
Paul Duffin6d0886e2020-04-07 18:49:53 +01001331 droidstubsArgs = append(droidstubsArgs, android.JoinWithPrefix(disabledWarnings, "--hide "))
Sundong Ahnfb2721f2018-09-17 13:23:09 +09001332
Paul Duffin6877e6d2020-09-25 19:59:14 +01001333 // Output Javadoc comments for public scope.
1334 if apiScope == apiScopePublic {
1335 props.Output_javadoc_comments = proptools.BoolPtr(true)
1336 }
1337
Paul Duffin1fb487d2020-04-07 18:50:10 +01001338 // Add in scope specific arguments.
Paul Duffin0ff08bd2020-04-29 13:30:54 +01001339 droidstubsArgs = append(droidstubsArgs, scopeSpecificDroidstubsArgs...)
Paul Duffin11512472019-02-11 15:55:17 +00001340 props.Arg_files = module.sdkLibraryProperties.Droiddoc_option_files
Paul Duffin6d0886e2020-04-07 18:49:53 +01001341 props.Args = proptools.StringPtr(strings.Join(droidstubsArgs, " "))
Jiyong Parkc678ad32018-04-10 13:07:10 +09001342
Paul Duffin15f34ef2020-07-20 18:04:44 +01001343 // List of APIs identified from the provided source files are created. They are later
1344 // compared against to the not-yet-released (a.k.a current) list of APIs and to the
1345 // last-released (a.k.a numbered) list of API.
1346 currentApiFileName := apiScope.apiFilePrefix + "current.txt"
1347 removedApiFileName := apiScope.apiFilePrefix + "removed.txt"
1348 apiDir := module.getApiDir()
1349 currentApiFileName = path.Join(apiDir, currentApiFileName)
1350 removedApiFileName = path.Join(apiDir, removedApiFileName)
Jiyong Parkc678ad32018-04-10 13:07:10 +09001351
Paul Duffin15f34ef2020-07-20 18:04:44 +01001352 // check against the not-yet-release API
1353 props.Check_api.Current.Api_file = proptools.StringPtr(currentApiFileName)
1354 props.Check_api.Current.Removed_api_file = proptools.StringPtr(removedApiFileName)
Jiyong Park58c518b2018-05-12 22:29:12 +09001355
Paul Duffin15f34ef2020-07-20 18:04:44 +01001356 if !apiScope.unstable {
1357 // check against the latest released API
1358 latestApiFilegroupName := proptools.StringPtr(module.latestApiFilegroupName(apiScope))
1359 props.Check_api.Last_released.Api_file = latestApiFilegroupName
1360 props.Check_api.Last_released.Removed_api_file = proptools.StringPtr(
1361 module.latestRemovedApiFilegroupName(apiScope))
1362 props.Check_api.Ignore_missing_latest_api = proptools.BoolPtr(true)
Paul Duffin160fe412020-05-10 19:32:20 +01001363
Paul Duffin15f34ef2020-07-20 18:04:44 +01001364 if proptools.Bool(module.sdkLibraryProperties.Api_lint.Enabled) {
1365 // Enable api lint.
1366 props.Check_api.Api_lint.Enabled = proptools.BoolPtr(true)
1367 props.Check_api.Api_lint.New_since = latestApiFilegroupName
Paul Duffin160fe412020-05-10 19:32:20 +01001368
Paul Duffin15f34ef2020-07-20 18:04:44 +01001369 // If it exists then pass a lint-baseline.txt through to droidstubs.
1370 baselinePath := path.Join(apiDir, apiScope.apiFilePrefix+"lint-baseline.txt")
1371 baselinePathRelativeToRoot := path.Join(mctx.ModuleDir(), baselinePath)
1372 paths, err := mctx.GlobWithDeps(baselinePathRelativeToRoot, nil)
1373 if err != nil {
1374 mctx.ModuleErrorf("error checking for presence of %s: %s", baselinePathRelativeToRoot, err)
1375 }
1376 if len(paths) == 1 {
1377 props.Check_api.Api_lint.Baseline_file = proptools.StringPtr(baselinePath)
1378 } else if len(paths) != 0 {
1379 mctx.ModuleErrorf("error checking for presence of %s: expected one path, found: %v", baselinePathRelativeToRoot, paths)
Paul Duffin160fe412020-05-10 19:32:20 +01001380 }
1381 }
Paul Duffin15f34ef2020-07-20 18:04:44 +01001382 }
Jiyong Park58c518b2018-05-12 22:29:12 +09001383
Paul Duffin15f34ef2020-07-20 18:04:44 +01001384 if !Bool(module.sdkLibraryProperties.No_dist) {
Paul Duffin040e9062020-11-23 17:41:36 +00001385 // Dist the api txt and removed api txt artifacts for sdk builds.
1386 distDir := proptools.StringPtr(path.Join(module.apiDistPath(apiScope), "api"))
1387 for _, p := range []struct {
1388 tag string
1389 pattern string
1390 }{
1391 {tag: ".api.txt", pattern: "%s.txt"},
1392 {tag: ".removed-api.txt", pattern: "%s-removed.txt"},
1393 } {
1394 props.Dists = append(props.Dists, android.Dist{
1395 Targets: []string{"sdk", "win_sdk"},
1396 Dir: distDir,
1397 Dest: proptools.StringPtr(fmt.Sprintf(p.pattern, module.distStem())),
1398 Tag: proptools.StringPtr(p.tag),
1399 })
1400 }
Anton Hansson5fd5d242020-03-27 19:43:19 +00001401 }
1402
Colin Cross84dfc3d2019-09-25 11:33:01 -07001403 mctx.CreateModule(DroidstubsFactory, &props)
Jiyong Parkc678ad32018-04-10 13:07:10 +09001404}
1405
Jooyung Han5e9013b2020-03-10 06:23:13 +09001406func (module *SdkLibrary) DepIsInSameApex(mctx android.BaseModuleContext, dep android.Module) bool {
1407 depTag := mctx.OtherModuleDependencyTag(dep)
1408 if depTag == xmlPermissionsFileTag {
1409 return true
1410 }
1411 return module.Library.DepIsInSameApex(mctx, dep)
1412}
1413
Jiyong Parkc678ad32018-04-10 13:07:10 +09001414// Creates the xml file that publicizes the runtime library
Paul Duffinf0229202020-04-29 16:47:28 +01001415func (module *SdkLibrary) createXmlFile(mctx android.DefaultableHookContext) {
Jiyong Parke3833882020-02-17 17:28:10 +09001416 props := struct {
Paul Duffin1dbe3ca2020-05-16 09:57:59 +01001417 Name *string
1418 Lib_name *string
1419 Apex_available []string
Jiyong Parke3833882020-02-17 17:28:10 +09001420 }{
Paul Duffineedc5d52020-06-12 17:46:39 +01001421 Name: proptools.StringPtr(module.xmlPermissionsModuleName()),
Jooyung Han5e9013b2020-03-10 06:23:13 +09001422 Lib_name: proptools.StringPtr(module.BaseModuleName()),
1423 Apex_available: module.ApexProperties.Apex_available,
Jiyong Parkc678ad32018-04-10 13:07:10 +09001424 }
Jiyong Parke3833882020-02-17 17:28:10 +09001425
Jiyong Parke3833882020-02-17 17:28:10 +09001426 mctx.CreateModule(sdkLibraryXmlFactory, &props)
Jiyong Parkc678ad32018-04-10 13:07:10 +09001427}
1428
Paul Duffin50061512020-01-21 16:31:05 +00001429func PrebuiltJars(ctx android.BaseModuleContext, baseName string, s sdkSpec) android.Paths {
Jiyong Park6a927c42020-01-21 02:03:43 +09001430 var ver sdkVersion
1431 var kind sdkKind
1432 if s.usePrebuilt(ctx) {
1433 ver = s.version
1434 kind = s.kind
Jiyong Parkc678ad32018-04-10 13:07:10 +09001435 } else {
Jiyong Park6a927c42020-01-21 02:03:43 +09001436 // We don't have prebuilt SDK for the specific sdkVersion.
1437 // Instead of breaking the build, fallback to use "system_current"
1438 ver = sdkVersionCurrent
1439 kind = sdkSystem
Sundong Ahn054b19a2018-10-19 13:46:09 +09001440 }
Jiyong Park6a927c42020-01-21 02:03:43 +09001441
1442 dir := filepath.Join("prebuilts", "sdk", ver.String(), kind.String())
Paul Duffin50061512020-01-21 16:31:05 +00001443 jar := filepath.Join(dir, baseName+".jar")
Sundong Ahn054b19a2018-10-19 13:46:09 +09001444 jarPath := android.ExistentPathForSource(ctx, jar)
Sundong Ahnae418ac2019-02-28 15:01:28 +09001445 if !jarPath.Valid() {
Colin Cross07c88562020-01-07 09:34:44 -08001446 if ctx.Config().AllowMissingDependencies() {
1447 return android.Paths{android.PathForSource(ctx, jar)}
1448 } else {
Jiyong Park6a927c42020-01-21 02:03:43 +09001449 ctx.PropertyErrorf("sdk_library", "invalid sdk version %q, %q does not exist", s.raw, jar)
Colin Cross07c88562020-01-07 09:34:44 -08001450 }
Sundong Ahnae418ac2019-02-28 15:01:28 +09001451 return nil
1452 }
Sundong Ahn054b19a2018-10-19 13:46:09 +09001453 return android.Paths{jarPath.Path()}
1454}
1455
Colin Crossaede88c2020-08-11 12:17:01 -07001456// Check to see if the other module is within the same set of named APEXes as this module.
Paul Duffin9b879592020-05-26 13:21:35 +01001457//
1458// If either this or the other module are on the platform then this will return
1459// false.
Colin Cross56a83212020-09-15 18:30:11 -07001460func withinSameApexesAs(ctx android.BaseModuleContext, other android.Module) bool {
1461 apexInfo := ctx.Provider(android.ApexInfoProvider).(android.ApexInfo)
1462 otherApexInfo := ctx.OtherModuleProvider(other, android.ApexInfoProvider).(android.ApexInfo)
1463 return len(otherApexInfo.InApexes) > 0 && reflect.DeepEqual(apexInfo.InApexes, otherApexInfo.InApexes)
Paul Duffin9b879592020-05-26 13:21:35 +01001464}
1465
Paul Duffinb05d4292020-05-20 12:19:10 +01001466func (module *SdkLibrary) sdkJars(ctx android.BaseModuleContext, sdkVersion sdkSpec, headerJars bool) android.Paths {
Jiyong Park932cdfe2020-05-28 00:19:53 +09001467 // If the client doesn't set sdk_version, but if this library prefers stubs over
1468 // the impl library, let's provide the widest API surface possible. To do so,
1469 // force override sdk_version to module_current so that the closest possible API
1470 // surface could be found in selectHeaderJarsForSdkVersion
1471 if module.defaultsToStubs() && !sdkVersion.specified() {
1472 sdkVersion = sdkSpecFrom("module_current")
1473 }
Paul Duffind1b3a922020-01-22 11:57:20 +00001474
Paul Duffindaaa3322020-05-26 18:13:57 +01001475 // Only provide access to the implementation library if it is actually built.
1476 if module.requiresRuntimeImplementationLibrary() {
1477 // Check any special cases for java_sdk_library.
1478 //
1479 // Only allow access to the implementation library in the following condition:
1480 // * No sdk_version specified on the referencing module.
Paul Duffin9b879592020-05-26 13:21:35 +01001481 // * The referencing module is in the same apex as this.
Colin Cross56a83212020-09-15 18:30:11 -07001482 if sdkVersion.kind == sdkPrivate || withinSameApexesAs(ctx, module) {
Paul Duffindaaa3322020-05-26 18:13:57 +01001483 if headerJars {
1484 return module.HeaderJars()
1485 } else {
1486 return module.ImplementationJars()
1487 }
Sundong Ahn054b19a2018-10-19 13:46:09 +09001488 }
Jiyong Parkc678ad32018-04-10 13:07:10 +09001489 }
Paul Duffinb05d4292020-05-20 12:19:10 +01001490
Paul Duffin23970f42020-05-20 14:20:02 +01001491 return module.selectHeaderJarsForSdkVersion(ctx, sdkVersion)
Jiyong Parkc678ad32018-04-10 13:07:10 +09001492}
1493
Sundong Ahn241cd372018-07-13 16:16:44 +09001494// to satisfy SdkLibraryDependency interface
Paul Duffind1b3a922020-01-22 11:57:20 +00001495func (module *SdkLibrary) SdkHeaderJars(ctx android.BaseModuleContext, sdkVersion sdkSpec) android.Paths {
1496 return module.sdkJars(ctx, sdkVersion, true /*headerJars*/)
1497}
1498
1499// to satisfy SdkLibraryDependency interface
Jiyong Park6a927c42020-01-21 02:03:43 +09001500func (module *SdkLibrary) SdkImplementationJars(ctx android.BaseModuleContext, sdkVersion sdkSpec) android.Paths {
Paul Duffind1b3a922020-01-22 11:57:20 +00001501 return module.sdkJars(ctx, sdkVersion, false /*headerJars*/)
Sundong Ahn241cd372018-07-13 16:16:44 +09001502}
1503
Colin Cross571cccf2019-02-04 11:22:08 -08001504var javaSdkLibrariesKey = android.NewOnceKey("javaSdkLibraries")
1505
Jiyong Park82484c02018-04-23 21:41:26 +09001506func javaSdkLibraries(config android.Config) *[]string {
Colin Cross571cccf2019-02-04 11:22:08 -08001507 return config.Once(javaSdkLibrariesKey, func() interface{} {
Jiyong Park82484c02018-04-23 21:41:26 +09001508 return &[]string{}
1509 }).(*[]string)
1510}
1511
Paul Duffin749f98f2019-12-30 17:23:46 +00001512func (module *SdkLibrary) getApiDir() string {
1513 return proptools.StringDefault(module.sdkLibraryProperties.Api_dir, "api")
1514}
1515
Jiyong Parkc678ad32018-04-10 13:07:10 +09001516// For a java_sdk_library module, create internal modules for stubs, docs,
1517// runtime libs and xml file. If requested, the stubs and docs are created twice
1518// once for public API level and once for system API level
Paul Duffinf0229202020-04-29 16:47:28 +01001519func (module *SdkLibrary) CreateInternalModules(mctx android.DefaultableHookContext) {
1520 // If the module has been disabled then don't create any child modules.
1521 if !module.Enabled() {
1522 return
1523 }
1524
Paul Duffina18abc22020-05-16 18:54:24 +01001525 if len(module.properties.Srcs) == 0 {
Inseob Kimc0907f12019-02-08 21:00:45 +09001526 mctx.PropertyErrorf("srcs", "java_sdk_library must specify srcs")
Jooyung Han58f26ab2019-12-18 15:34:32 +09001527 return
Inseob Kimc0907f12019-02-08 21:00:45 +09001528 }
1529
Paul Duffin37e0b772019-12-30 17:20:10 +00001530 // If this builds against standard libraries (i.e. is not part of the core libraries)
Paul Duffin4f5c1ef2020-11-19 14:53:43 +00001531 // then assume it provides both system and test apis.
Paul Duffin37e0b772019-12-30 17:20:10 +00001532 sdkDep := decodeSdkDep(mctx, sdkContext(&module.Library))
1533 hasSystemAndTestApis := sdkDep.hasStandardLibs()
Paul Duffin3375e352020-04-28 10:44:03 +01001534 module.sdkLibraryProperties.Generate_system_and_test_apis = hasSystemAndTestApis
Paul Duffin4f5c1ef2020-11-19 14:53:43 +00001535
Inseob Kim8098faa2019-03-18 10:19:51 +09001536 missing_current_api := false
1537
Paul Duffin3375e352020-04-28 10:44:03 +01001538 generatedScopes := module.getGeneratedApiScopes(mctx)
Paul Duffind1b3a922020-01-22 11:57:20 +00001539
Paul Duffin749f98f2019-12-30 17:23:46 +00001540 apiDir := module.getApiDir()
Paul Duffin3375e352020-04-28 10:44:03 +01001541 for _, scope := range generatedScopes {
Inseob Kim8098faa2019-03-18 10:19:51 +09001542 for _, api := range []string{"current.txt", "removed.txt"} {
Paul Duffind1b3a922020-01-22 11:57:20 +00001543 path := path.Join(mctx.ModuleDir(), apiDir, scope.apiFilePrefix+api)
Inseob Kim8098faa2019-03-18 10:19:51 +09001544 p := android.ExistentPathForSource(mctx, path)
1545 if !p.Valid() {
1546 mctx.ModuleErrorf("Current api file %#v doesn't exist", path)
1547 missing_current_api = true
1548 }
1549 }
1550 }
1551
1552 if missing_current_api {
1553 script := "build/soong/scripts/gen-java-current-api-files.sh"
1554 p := android.ExistentPathForSource(mctx, script)
1555
1556 if !p.Valid() {
1557 panic(fmt.Sprintf("script file %s doesn't exist", script))
1558 }
1559
1560 mctx.ModuleErrorf("One or more current api files are missing. "+
1561 "You can update them by:\n"+
Paul Duffin37e0b772019-12-30 17:20:10 +00001562 "%s %q %s && m update-api",
Paul Duffind1b3a922020-01-22 11:57:20 +00001563 script, filepath.Join(mctx.ModuleDir(), apiDir),
Paul Duffin3375e352020-04-28 10:44:03 +01001564 strings.Join(generatedScopes.Strings(func(s *apiScope) string { return s.apiFilePrefix }), " "))
Inseob Kim8098faa2019-03-18 10:19:51 +09001565 return
1566 }
1567
Paul Duffin3375e352020-04-28 10:44:03 +01001568 for _, scope := range generatedScopes {
Paul Duffin15f34ef2020-07-20 18:04:44 +01001569 // Use the stubs source name for legacy reasons.
1570 module.createStubsSourcesAndApi(mctx, scope, module.stubsSourceModuleName(scope), scope.droidstubsArgs)
Paul Duffin0ff08bd2020-04-29 13:30:54 +01001571
Paul Duffind1b3a922020-01-22 11:57:20 +00001572 module.createStubsLibrary(mctx, scope)
Inseob Kimc0907f12019-02-08 21:00:45 +09001573 }
1574
Paul Duffindfa131e2020-05-15 20:37:11 +01001575 if module.requiresRuntimeImplementationLibrary() {
Paul Duffin5df79302020-05-16 15:52:12 +01001576 // Create child module to create an implementation library.
1577 //
1578 // This temporarily creates a second implementation library that can be explicitly
1579 // referenced.
1580 //
1581 // TODO(b/156618935) - update comment once only one implementation library is created.
1582 module.createImplLibrary(mctx)
1583
Paul Duffindfa131e2020-05-15 20:37:11 +01001584 // Only create an XML permissions file that declares the library as being usable
1585 // as a shared library if required.
1586 if module.sharedLibrary() {
1587 module.createXmlFile(mctx)
1588 }
Paul Duffin43db9be2019-12-30 17:35:49 +00001589
1590 // record java_sdk_library modules so that they are exported to make
1591 javaSdkLibraries := javaSdkLibraries(mctx.Config())
1592 javaSdkLibrariesLock.Lock()
1593 defer javaSdkLibrariesLock.Unlock()
1594 *javaSdkLibraries = append(*javaSdkLibraries, module.BaseModuleName())
1595 }
Anton Hansson7f66efa2020-10-08 14:47:23 +01001596
1597 // Add the impl_only_libs *after* we're done using the Libs prop in submodules.
1598 module.properties.Libs = append(module.properties.Libs, module.sdkLibraryProperties.Impl_only_libs...)
Inseob Kimc0907f12019-02-08 21:00:45 +09001599}
1600
1601func (module *SdkLibrary) InitSdkLibraryProperties() {
Colin Crossce6734e2020-06-15 16:09:53 -07001602 module.addHostAndDeviceProperties()
1603 module.AddProperties(&module.sdkLibraryProperties)
Sundong Ahn054b19a2018-10-19 13:46:09 +09001604
Paul Duffin859fe962020-05-15 10:20:31 +01001605 module.initSdkLibraryComponent(&module.ModuleBase)
1606
Paul Duffina18abc22020-05-16 18:54:24 +01001607 module.properties.Installable = proptools.BoolPtr(true)
1608 module.deviceProperties.IsSDKLibrary = true
Inseob Kimc0907f12019-02-08 21:00:45 +09001609}
Sundong Ahn054b19a2018-10-19 13:46:09 +09001610
Paul Duffindfa131e2020-05-15 20:37:11 +01001611func (module *SdkLibrary) requiresRuntimeImplementationLibrary() bool {
1612 return !proptools.Bool(module.sdkLibraryProperties.Api_only)
1613}
1614
Jiyong Park932cdfe2020-05-28 00:19:53 +09001615func (module *SdkLibrary) defaultsToStubs() bool {
1616 return proptools.Bool(module.sdkLibraryProperties.Default_to_stubs)
1617}
1618
Paul Duffin1b1e8062020-05-08 13:44:43 +01001619// Defines how to name the individual component modules the sdk library creates.
1620type sdkLibraryComponentNamingScheme interface {
1621 stubsLibraryModuleName(scope *apiScope, baseName string) string
1622
1623 stubsSourceModuleName(scope *apiScope, baseName string) string
1624
1625 apiModuleName(scope *apiScope, baseName string) string
1626}
1627
1628type defaultNamingScheme struct {
1629}
1630
1631func (s *defaultNamingScheme) stubsLibraryModuleName(scope *apiScope, baseName string) string {
1632 return scope.stubsLibraryModuleName(baseName)
1633}
1634
1635func (s *defaultNamingScheme) stubsSourceModuleName(scope *apiScope, baseName string) string {
1636 return scope.stubsSourceModuleName(baseName)
1637}
1638
1639func (s *defaultNamingScheme) apiModuleName(scope *apiScope, baseName string) string {
1640 return scope.apiModuleName(baseName)
1641}
1642
1643var _ sdkLibraryComponentNamingScheme = (*defaultNamingScheme)(nil)
1644
Anton Hansson2d0c1942020-05-25 12:20:51 +01001645func moduleStubLinkType(name string) (stub bool, ret linkType) {
1646 // This suffix-based approach is fragile and could potentially mis-trigger.
1647 // TODO(b/155164730): Clean this up when modules no longer reference sdk_lib stubs directly.
1648 if strings.HasSuffix(name, ".stubs.public") || strings.HasSuffix(name, "-stubs-publicapi") {
1649 return true, javaSdk
1650 }
1651 if strings.HasSuffix(name, ".stubs.system") || strings.HasSuffix(name, "-stubs-systemapi") {
1652 return true, javaSystem
1653 }
1654 if strings.HasSuffix(name, ".stubs.module_lib") || strings.HasSuffix(name, "-stubs-module_libs_api") {
1655 return true, javaModule
1656 }
1657 if strings.HasSuffix(name, ".stubs.test") {
1658 return true, javaSystem
1659 }
1660 return false, javaPlatform
1661}
1662
Jaewoong Jung4f158ee2019-07-11 10:05:35 -07001663// java_sdk_library is a special Java library that provides optional platform APIs to apps.
1664// In practice, it can be viewed as a combination of several modules: 1) stubs library that clients
1665// are linked against to, 2) droiddoc module that internally generates API stubs source files,
1666// 3) the real runtime shared library that implements the APIs, and 4) XML file for adding
1667// the runtime lib to the classpath at runtime if requested via <uses-library>.
Inseob Kimc0907f12019-02-08 21:00:45 +09001668func SdkLibraryFactory() android.Module {
1669 module := &SdkLibrary{}
Paul Duffinc3091c82020-05-08 14:16:20 +01001670
1671 // Initialize information common between source and prebuilt.
1672 module.initCommon(&module.ModuleBase)
1673
Inseob Kimc0907f12019-02-08 21:00:45 +09001674 module.InitSdkLibraryProperties()
Jooyung Han58f26ab2019-12-18 15:34:32 +09001675 android.InitApexModule(module)
Sundong Ahn054b19a2018-10-19 13:46:09 +09001676 InitJavaModule(module, android.HostAndDeviceSupported)
Paul Duffin3375e352020-04-28 10:44:03 +01001677
1678 // Initialize the map from scope to scope specific properties.
1679 scopeToProperties := make(map[*apiScope]*ApiScopeProperties)
1680 for _, scope := range allApiScopes {
1681 scopeToProperties[scope] = scope.scopeSpecificProperties(module)
1682 }
1683 module.scopeToProperties = scopeToProperties
1684
Paul Duffin4911a892020-04-29 23:35:13 +01001685 // Add the properties containing visibility rules so that they are checked.
Paul Duffin5df79302020-05-16 15:52:12 +01001686 android.AddVisibilityProperty(module, "impl_library_visibility", &module.sdkLibraryProperties.Impl_library_visibility)
Paul Duffin4911a892020-04-29 23:35:13 +01001687 android.AddVisibilityProperty(module, "stubs_library_visibility", &module.sdkLibraryProperties.Stubs_library_visibility)
1688 android.AddVisibilityProperty(module, "stubs_source_visibility", &module.sdkLibraryProperties.Stubs_source_visibility)
1689
Paul Duffin1b1e8062020-05-08 13:44:43 +01001690 module.SetDefaultableHook(func(ctx android.DefaultableHookContext) {
Paul Duffindfa131e2020-05-15 20:37:11 +01001691 // If no implementation is required then it cannot be used as a shared library
1692 // either.
1693 if !module.requiresRuntimeImplementationLibrary() {
1694 // If shared_library has been explicitly set to true then it is incompatible
1695 // with api_only: true.
1696 if proptools.Bool(module.commonSdkLibraryProperties.Shared_library) {
1697 ctx.PropertyErrorf("api_only/shared_library", "inconsistent settings, shared_library and api_only cannot both be true")
1698 }
1699 // Set shared_library: false.
1700 module.commonSdkLibraryProperties.Shared_library = proptools.BoolPtr(false)
1701 }
1702
Paul Duffin1b1e8062020-05-08 13:44:43 +01001703 if module.initCommonAfterDefaultsApplied(ctx) {
1704 module.CreateInternalModules(ctx)
1705 }
1706 })
Jiyong Parkc678ad32018-04-10 13:07:10 +09001707 return module
1708}
Colin Cross79c7c262019-04-17 11:11:46 -07001709
1710//
1711// SDK library prebuilts
1712//
1713
Paul Duffin56d44902020-01-31 13:36:25 +00001714// Properties associated with each api scope.
1715type sdkLibraryScopeProperties struct {
Colin Cross79c7c262019-04-17 11:11:46 -07001716 Jars []string `android:"path"`
1717
1718 Sdk_version *string
1719
Colin Cross79c7c262019-04-17 11:11:46 -07001720 // List of shared java libs that this module has dependencies to
1721 Libs []string
Paul Duffin3d1248c2020-04-09 00:10:17 +01001722
Paul Duffinc8782502020-04-29 20:45:27 +01001723 // The stubs source.
Paul Duffin3d1248c2020-04-09 00:10:17 +01001724 Stub_srcs []string `android:"path"`
Paul Duffin1fd005d2020-04-09 01:08:11 +01001725
1726 // The current.txt
Paul Duffin0f8faff2020-05-20 16:18:00 +01001727 Current_api *string `android:"path"`
Paul Duffin1fd005d2020-04-09 01:08:11 +01001728
1729 // The removed.txt
Paul Duffin0f8faff2020-05-20 16:18:00 +01001730 Removed_api *string `android:"path"`
Colin Cross79c7c262019-04-17 11:11:46 -07001731}
1732
Paul Duffin56d44902020-01-31 13:36:25 +00001733type sdkLibraryImportProperties struct {
Paul Duffinfcfd7912020-01-31 17:54:30 +00001734 // List of shared java libs, common to all scopes, that this module has
1735 // dependencies to
1736 Libs []string
Paul Duffin56d44902020-01-31 13:36:25 +00001737}
1738
Paul Duffineedc5d52020-06-12 17:46:39 +01001739type SdkLibraryImport struct {
Colin Cross79c7c262019-04-17 11:11:46 -07001740 android.ModuleBase
1741 android.DefaultableModuleBase
1742 prebuilt android.Prebuilt
Paul Duffindd46f712020-02-10 13:37:10 +00001743 android.ApexModuleBase
1744 android.SdkBase
Colin Cross79c7c262019-04-17 11:11:46 -07001745
1746 properties sdkLibraryImportProperties
1747
Paul Duffin46a26a82020-04-07 19:27:04 +01001748 // Map from api scope to the scope specific property structure.
1749 scopeProperties map[*apiScope]*sdkLibraryScopeProperties
1750
Paul Duffin56d44902020-01-31 13:36:25 +00001751 commonToSdkLibraryAndImport
Paul Duffineedc5d52020-06-12 17:46:39 +01001752
1753 // The reference to the implementation library created by the source module.
1754 // Is nil if the source module does not exist.
1755 implLibraryModule *Library
1756
1757 // The reference to the xml permissions module created by the source module.
1758 // Is nil if the source module does not exist.
1759 xmlPermissionsFileModule *sdkLibraryXml
Colin Cross79c7c262019-04-17 11:11:46 -07001760}
1761
Paul Duffineedc5d52020-06-12 17:46:39 +01001762var _ SdkLibraryDependency = (*SdkLibraryImport)(nil)
Colin Cross79c7c262019-04-17 11:11:46 -07001763
Paul Duffin46a26a82020-04-07 19:27:04 +01001764// The type of a structure that contains a field of type sdkLibraryScopeProperties
1765// for each apiscope in allApiScopes, e.g. something like:
1766// struct {
1767// Public sdkLibraryScopeProperties
1768// System sdkLibraryScopeProperties
1769// ...
1770// }
1771var allScopeStructType = createAllScopePropertiesStructType()
1772
1773// Dynamically create a structure type for each apiscope in allApiScopes.
1774func createAllScopePropertiesStructType() reflect.Type {
1775 var fields []reflect.StructField
1776 for _, apiScope := range allApiScopes {
1777 field := reflect.StructField{
1778 Name: apiScope.fieldName,
1779 Type: reflect.TypeOf(sdkLibraryScopeProperties{}),
1780 }
1781 fields = append(fields, field)
1782 }
1783
1784 return reflect.StructOf(fields)
1785}
1786
1787// Create an instance of the scope specific structure type and return a map
1788// from apiscope to a pointer to each scope specific field.
1789func createPropertiesInstance() (interface{}, map[*apiScope]*sdkLibraryScopeProperties) {
1790 allScopePropertiesPtr := reflect.New(allScopeStructType)
1791 allScopePropertiesStruct := allScopePropertiesPtr.Elem()
1792 scopeProperties := make(map[*apiScope]*sdkLibraryScopeProperties)
1793
1794 for _, apiScope := range allApiScopes {
1795 field := allScopePropertiesStruct.FieldByName(apiScope.fieldName)
1796 scopeProperties[apiScope] = field.Addr().Interface().(*sdkLibraryScopeProperties)
1797 }
1798
1799 return allScopePropertiesPtr.Interface(), scopeProperties
1800}
1801
Jaewoong Jung4f158ee2019-07-11 10:05:35 -07001802// java_sdk_library_import imports a prebuilt java_sdk_library.
Colin Cross79c7c262019-04-17 11:11:46 -07001803func sdkLibraryImportFactory() android.Module {
Paul Duffineedc5d52020-06-12 17:46:39 +01001804 module := &SdkLibraryImport{}
Colin Cross79c7c262019-04-17 11:11:46 -07001805
Paul Duffin46a26a82020-04-07 19:27:04 +01001806 allScopeProperties, scopeToProperties := createPropertiesInstance()
1807 module.scopeProperties = scopeToProperties
1808 module.AddProperties(&module.properties, allScopeProperties)
Colin Cross79c7c262019-04-17 11:11:46 -07001809
Paul Duffinc3091c82020-05-08 14:16:20 +01001810 // Initialize information common between source and prebuilt.
1811 module.initCommon(&module.ModuleBase)
1812
Paul Duffin0bdcb272020-02-06 15:24:57 +00001813 android.InitPrebuiltModule(module, &[]string{""})
Paul Duffindd46f712020-02-10 13:37:10 +00001814 android.InitApexModule(module)
1815 android.InitSdkAwareModule(module)
Colin Cross79c7c262019-04-17 11:11:46 -07001816 InitJavaModule(module, android.HostAndDeviceSupported)
1817
Paul Duffin1b1e8062020-05-08 13:44:43 +01001818 module.SetDefaultableHook(func(mctx android.DefaultableHookContext) {
1819 if module.initCommonAfterDefaultsApplied(mctx) {
1820 module.createInternalModules(mctx)
1821 }
1822 })
Colin Cross79c7c262019-04-17 11:11:46 -07001823 return module
1824}
1825
Paul Duffineedc5d52020-06-12 17:46:39 +01001826func (module *SdkLibraryImport) Prebuilt() *android.Prebuilt {
Colin Cross79c7c262019-04-17 11:11:46 -07001827 return &module.prebuilt
1828}
1829
Paul Duffineedc5d52020-06-12 17:46:39 +01001830func (module *SdkLibraryImport) Name() string {
Colin Cross79c7c262019-04-17 11:11:46 -07001831 return module.prebuilt.Name(module.ModuleBase.Name())
1832}
1833
Paul Duffineedc5d52020-06-12 17:46:39 +01001834func (module *SdkLibraryImport) createInternalModules(mctx android.DefaultableHookContext) {
Colin Cross79c7c262019-04-17 11:11:46 -07001835
Paul Duffin50061512020-01-21 16:31:05 +00001836 // If the build is configured to use prebuilts then force this to be preferred.
Jeongik Cha816a23a2020-07-08 01:09:23 +09001837 if mctx.Config().AlwaysUsePrebuiltSdks() {
Paul Duffin50061512020-01-21 16:31:05 +00001838 module.prebuilt.ForcePrefer()
1839 }
1840
Paul Duffin46a26a82020-04-07 19:27:04 +01001841 for apiScope, scopeProperties := range module.scopeProperties {
Paul Duffin56d44902020-01-31 13:36:25 +00001842 if len(scopeProperties.Jars) == 0 {
1843 continue
1844 }
1845
Paul Duffinbbb546b2020-04-09 00:07:11 +01001846 module.createJavaImportForStubs(mctx, apiScope, scopeProperties)
Paul Duffin3d1248c2020-04-09 00:10:17 +01001847
Paul Duffin0f8faff2020-05-20 16:18:00 +01001848 if len(scopeProperties.Stub_srcs) > 0 {
1849 module.createPrebuiltStubsSources(mctx, apiScope, scopeProperties)
1850 }
Paul Duffin56d44902020-01-31 13:36:25 +00001851 }
Colin Cross79c7c262019-04-17 11:11:46 -07001852
1853 javaSdkLibraries := javaSdkLibraries(mctx.Config())
1854 javaSdkLibrariesLock.Lock()
1855 defer javaSdkLibrariesLock.Unlock()
1856 *javaSdkLibraries = append(*javaSdkLibraries, module.BaseModuleName())
1857}
1858
Paul Duffineedc5d52020-06-12 17:46:39 +01001859func (module *SdkLibraryImport) createJavaImportForStubs(mctx android.DefaultableHookContext, apiScope *apiScope, scopeProperties *sdkLibraryScopeProperties) {
Paul Duffinbbb546b2020-04-09 00:07:11 +01001860 // Creates a java import for the jar with ".stubs" suffix
1861 props := struct {
Paul Duffin1dbe3ca2020-05-16 09:57:59 +01001862 Name *string
1863 Sdk_version *string
1864 Libs []string
1865 Jars []string
1866 Prefer *bool
Paul Duffinbbb546b2020-04-09 00:07:11 +01001867 }{}
Paul Duffinc3091c82020-05-08 14:16:20 +01001868 props.Name = proptools.StringPtr(module.stubsLibraryModuleName(apiScope))
Paul Duffinbbb546b2020-04-09 00:07:11 +01001869 props.Sdk_version = scopeProperties.Sdk_version
1870 // Prepend any of the libs from the legacy public properties to the libs for each of the
1871 // scopes to avoid having to duplicate them in each scope.
1872 props.Libs = append(module.properties.Libs, scopeProperties.Libs...)
1873 props.Jars = scopeProperties.Jars
Paul Duffin1dbe3ca2020-05-16 09:57:59 +01001874
Paul Duffin38b57852020-05-13 16:08:09 +01001875 // The imports are preferred if the java_sdk_library_import is preferred.
1876 props.Prefer = proptools.BoolPtr(module.prebuilt.Prefer())
Paul Duffin859fe962020-05-15 10:20:31 +01001877
1878 mctx.CreateModule(ImportFactory, &props, module.sdkComponentPropertiesForChildLibrary())
Paul Duffinbbb546b2020-04-09 00:07:11 +01001879}
1880
Paul Duffineedc5d52020-06-12 17:46:39 +01001881func (module *SdkLibraryImport) createPrebuiltStubsSources(mctx android.DefaultableHookContext, apiScope *apiScope, scopeProperties *sdkLibraryScopeProperties) {
Paul Duffin3d1248c2020-04-09 00:10:17 +01001882 props := struct {
Paul Duffin38b57852020-05-13 16:08:09 +01001883 Name *string
1884 Srcs []string
1885 Prefer *bool
Paul Duffin3d1248c2020-04-09 00:10:17 +01001886 }{}
Paul Duffinc3091c82020-05-08 14:16:20 +01001887 props.Name = proptools.StringPtr(module.stubsSourceModuleName(apiScope))
Paul Duffin3d1248c2020-04-09 00:10:17 +01001888 props.Srcs = scopeProperties.Stub_srcs
1889 mctx.CreateModule(PrebuiltStubsSourcesFactory, &props)
Paul Duffin38b57852020-05-13 16:08:09 +01001890
1891 // The stubs source is preferred if the java_sdk_library_import is preferred.
1892 props.Prefer = proptools.BoolPtr(module.prebuilt.Prefer())
Paul Duffin3d1248c2020-04-09 00:10:17 +01001893}
1894
Paul Duffin44f1d842020-06-26 20:17:02 +01001895// Add the dependencies on the child module in the component deps mutator so that it
1896// creates references to the prebuilt and not the source modules.
1897func (module *SdkLibraryImport) ComponentDepsMutator(ctx android.BottomUpMutatorContext) {
Paul Duffin46a26a82020-04-07 19:27:04 +01001898 for apiScope, scopeProperties := range module.scopeProperties {
Paul Duffin56d44902020-01-31 13:36:25 +00001899 if len(scopeProperties.Jars) == 0 {
1900 continue
1901 }
1902
1903 // Add dependencies to the prebuilt stubs library
Paul Duffin44f1d842020-06-26 20:17:02 +01001904 ctx.AddVariationDependencies(nil, apiScope.stubsTag, "prebuilt_"+module.stubsLibraryModuleName(apiScope))
Paul Duffin0f8faff2020-05-20 16:18:00 +01001905
1906 if len(scopeProperties.Stub_srcs) > 0 {
1907 // Add dependencies to the prebuilt stubs source library
Paul Duffin44f1d842020-06-26 20:17:02 +01001908 ctx.AddVariationDependencies(nil, apiScope.stubsSourceTag, "prebuilt_"+module.stubsSourceModuleName(apiScope))
Paul Duffin0f8faff2020-05-20 16:18:00 +01001909 }
Paul Duffin56d44902020-01-31 13:36:25 +00001910 }
Paul Duffin44f1d842020-06-26 20:17:02 +01001911}
1912
1913// Add other dependencies as normal.
1914func (module *SdkLibraryImport) DepsMutator(ctx android.BottomUpMutatorContext) {
Paul Duffineedc5d52020-06-12 17:46:39 +01001915
1916 implName := module.implLibraryModuleName()
1917 if ctx.OtherModuleExists(implName) {
1918 ctx.AddVariationDependencies(nil, implLibraryTag, implName)
1919
1920 xmlPermissionsModuleName := module.xmlPermissionsModuleName()
1921 if module.sharedLibrary() && ctx.OtherModuleExists(xmlPermissionsModuleName) {
1922 // Add dependency to the rule for generating the xml permissions file
1923 ctx.AddDependency(module, xmlPermissionsFileTag, xmlPermissionsModuleName)
1924 }
1925 }
Colin Cross79c7c262019-04-17 11:11:46 -07001926}
1927
Paul Duffineedc5d52020-06-12 17:46:39 +01001928func (module *SdkLibraryImport) DepIsInSameApex(mctx android.BaseModuleContext, dep android.Module) bool {
1929 depTag := mctx.OtherModuleDependencyTag(dep)
1930 if depTag == xmlPermissionsFileTag {
1931 return true
1932 }
1933
1934 // None of the other dependencies of the java_sdk_library_import are in the same apex
1935 // as the one that references this module.
1936 return false
1937}
1938
Dan Albertc8060532020-07-22 22:32:17 -07001939func (module *SdkLibraryImport) ShouldSupportSdkVersion(ctx android.BaseModuleContext,
1940 sdkVersion android.ApiLevel) error {
Jooyung Han749dc692020-04-15 11:03:39 +09001941 // we don't check prebuilt modules for sdk_version
1942 return nil
1943}
1944
Paul Duffineedc5d52020-06-12 17:46:39 +01001945func (module *SdkLibraryImport) OutputFiles(tag string) (android.Paths, error) {
Paul Duffin46dc45a2020-05-14 15:39:10 +01001946 return module.commonOutputFiles(tag)
1947}
1948
Paul Duffineedc5d52020-06-12 17:46:39 +01001949func (module *SdkLibraryImport) GenerateAndroidBuildActions(ctx android.ModuleContext) {
Paul Duffina2ae7e02020-09-11 11:55:00 +01001950 module.generateCommonBuildActions(ctx)
1951
Paul Duffin0f8faff2020-05-20 16:18:00 +01001952 // Record the paths to the prebuilt stubs library and stubs source.
Colin Cross79c7c262019-04-17 11:11:46 -07001953 ctx.VisitDirectDeps(func(to android.Module) {
1954 tag := ctx.OtherModuleDependencyTag(to)
1955
Paul Duffin0f8faff2020-05-20 16:18:00 +01001956 // Extract information from any of the scope specific dependencies.
1957 if scopeTag, ok := tag.(scopeDependencyTag); ok {
1958 apiScope := scopeTag.apiScope
1959 scopePaths := module.getScopePathsCreateIfNeeded(apiScope)
1960
1961 // Extract information from the dependency. The exact information extracted
1962 // is determined by the nature of the dependency which is determined by the tag.
1963 scopeTag.extractDepInfo(ctx, to, scopePaths)
Paul Duffineedc5d52020-06-12 17:46:39 +01001964 } else if tag == implLibraryTag {
1965 if implLibrary, ok := to.(*Library); ok {
1966 module.implLibraryModule = implLibrary
1967 } else {
1968 ctx.ModuleErrorf("implementation library must be of type *java.Library but was %T", to)
1969 }
1970 } else if tag == xmlPermissionsFileTag {
1971 if xmlPermissionsFileModule, ok := to.(*sdkLibraryXml); ok {
1972 module.xmlPermissionsFileModule = xmlPermissionsFileModule
1973 } else {
1974 ctx.ModuleErrorf("xml permissions file module must be of type *sdkLibraryXml but was %T", to)
1975 }
Colin Cross79c7c262019-04-17 11:11:46 -07001976 }
1977 })
Paul Duffin0f8faff2020-05-20 16:18:00 +01001978
1979 // Populate the scope paths with information from the properties.
1980 for apiScope, scopeProperties := range module.scopeProperties {
1981 if len(scopeProperties.Jars) == 0 {
1982 continue
1983 }
1984
1985 paths := module.getScopePathsCreateIfNeeded(apiScope)
1986 paths.currentApiFilePath = android.OptionalPathForModuleSrc(ctx, scopeProperties.Current_api)
1987 paths.removedApiFilePath = android.OptionalPathForModuleSrc(ctx, scopeProperties.Removed_api)
1988 }
Colin Cross79c7c262019-04-17 11:11:46 -07001989}
1990
Paul Duffineedc5d52020-06-12 17:46:39 +01001991func (module *SdkLibraryImport) sdkJars(ctx android.BaseModuleContext, sdkVersion sdkSpec, headerJars bool) android.Paths {
1992
1993 // For consistency with SdkLibrary make the implementation jar available to libraries that
1994 // are within the same APEX.
1995 implLibraryModule := module.implLibraryModule
Colin Cross56a83212020-09-15 18:30:11 -07001996 if implLibraryModule != nil && withinSameApexesAs(ctx, module) {
Paul Duffineedc5d52020-06-12 17:46:39 +01001997 if headerJars {
1998 return implLibraryModule.HeaderJars()
1999 } else {
2000 return implLibraryModule.ImplementationJars()
2001 }
2002 }
2003
Paul Duffin23970f42020-05-20 14:20:02 +01002004 return module.selectHeaderJarsForSdkVersion(ctx, sdkVersion)
Paul Duffin56d44902020-01-31 13:36:25 +00002005}
2006
Colin Cross79c7c262019-04-17 11:11:46 -07002007// to satisfy SdkLibraryDependency interface
Paul Duffineedc5d52020-06-12 17:46:39 +01002008func (module *SdkLibraryImport) SdkHeaderJars(ctx android.BaseModuleContext, sdkVersion sdkSpec) android.Paths {
Colin Cross79c7c262019-04-17 11:11:46 -07002009 // This module is just a wrapper for the prebuilt stubs.
Paul Duffineedc5d52020-06-12 17:46:39 +01002010 return module.sdkJars(ctx, sdkVersion, true)
Colin Cross79c7c262019-04-17 11:11:46 -07002011}
2012
2013// to satisfy SdkLibraryDependency interface
Paul Duffineedc5d52020-06-12 17:46:39 +01002014func (module *SdkLibraryImport) SdkImplementationJars(ctx android.BaseModuleContext, sdkVersion sdkSpec) android.Paths {
Colin Cross79c7c262019-04-17 11:11:46 -07002015 // This module is just a wrapper for the stubs.
Paul Duffineedc5d52020-06-12 17:46:39 +01002016 return module.sdkJars(ctx, sdkVersion, false)
2017}
2018
Ulya Trafimovich31e444e2020-08-14 17:32:16 +01002019// to satisfy SdkLibraryDependency interface
Paul Duffineedc5d52020-06-12 17:46:39 +01002020func (module *SdkLibraryImport) DexJarBuildPath() android.Path {
2021 if module.implLibraryModule == nil {
2022 return nil
2023 } else {
2024 return module.implLibraryModule.DexJarBuildPath()
2025 }
2026}
2027
Ulya Trafimovich31e444e2020-08-14 17:32:16 +01002028// to satisfy SdkLibraryDependency interface
2029func (module *SdkLibraryImport) DexJarInstallPath() android.Path {
2030 if module.implLibraryModule == nil {
2031 return nil
2032 } else {
2033 return module.implLibraryModule.DexJarInstallPath()
2034 }
2035}
2036
Paul Duffineedc5d52020-06-12 17:46:39 +01002037// to satisfy apex.javaDependency interface
2038func (module *SdkLibraryImport) JacocoReportClassesFile() android.Path {
2039 if module.implLibraryModule == nil {
2040 return nil
2041 } else {
2042 return module.implLibraryModule.JacocoReportClassesFile()
2043 }
2044}
2045
2046// to satisfy apex.javaDependency interface
Colin Cross08dca382020-07-21 20:31:17 -07002047func (module *SdkLibraryImport) LintDepSets() LintDepSets {
2048 if module.implLibraryModule == nil {
2049 return LintDepSets{}
2050 } else {
2051 return module.implLibraryModule.LintDepSets()
2052 }
2053}
2054
2055// to satisfy apex.javaDependency interface
Paul Duffineedc5d52020-06-12 17:46:39 +01002056func (module *SdkLibraryImport) Stem() string {
2057 return module.BaseModuleName()
Colin Cross79c7c262019-04-17 11:11:46 -07002058}
Jiyong Parke3833882020-02-17 17:28:10 +09002059
Paul Duffin44b481b2020-06-17 16:59:43 +01002060var _ ApexDependency = (*SdkLibraryImport)(nil)
2061
2062// to satisfy java.ApexDependency interface
2063func (module *SdkLibraryImport) HeaderJars() android.Paths {
2064 if module.implLibraryModule == nil {
2065 return nil
2066 } else {
2067 return module.implLibraryModule.HeaderJars()
2068 }
2069}
2070
2071// to satisfy java.ApexDependency interface
2072func (module *SdkLibraryImport) ImplementationAndResourcesJars() android.Paths {
2073 if module.implLibraryModule == nil {
2074 return nil
2075 } else {
2076 return module.implLibraryModule.ImplementationAndResourcesJars()
2077 }
2078}
2079
Jiyong Parke3833882020-02-17 17:28:10 +09002080//
2081// java_sdk_library_xml
2082//
2083type sdkLibraryXml struct {
2084 android.ModuleBase
2085 android.DefaultableModuleBase
2086 android.ApexModuleBase
2087
2088 properties sdkLibraryXmlProperties
2089
2090 outputFilePath android.OutputPath
2091 installDirPath android.InstallPath
Colin Cross56a83212020-09-15 18:30:11 -07002092
2093 hideApexVariantFromMake bool
Jiyong Parke3833882020-02-17 17:28:10 +09002094}
2095
2096type sdkLibraryXmlProperties struct {
2097 // canonical name of the lib
2098 Lib_name *string
2099}
2100
2101// java_sdk_library_xml builds the permission xml file for a java_sdk_library.
2102// Not to be used directly by users. java_sdk_library internally uses this.
2103func sdkLibraryXmlFactory() android.Module {
2104 module := &sdkLibraryXml{}
2105
2106 module.AddProperties(&module.properties)
2107
2108 android.InitApexModule(module)
2109 android.InitAndroidArchModule(module, android.DeviceSupported, android.MultilibCommon)
2110
2111 return module
2112}
2113
Colin Crossaede88c2020-08-11 12:17:01 -07002114func (module *sdkLibraryXml) UniqueApexVariations() bool {
2115 // sdkLibraryXml needs a unique variation per APEX because the generated XML file contains the path to the
2116 // mounted APEX, which contains the name of the APEX.
2117 return true
2118}
2119
Jiyong Parke3833882020-02-17 17:28:10 +09002120// from android.PrebuiltEtcModule
Jooyung Han0703fd82020-08-26 22:11:53 +09002121func (module *sdkLibraryXml) BaseDir() string {
2122 return "etc"
2123}
2124
2125// from android.PrebuiltEtcModule
Jiyong Parke3833882020-02-17 17:28:10 +09002126func (module *sdkLibraryXml) SubDir() string {
2127 return "permissions"
2128}
2129
2130// from android.PrebuiltEtcModule
2131func (module *sdkLibraryXml) OutputFile() android.OutputPath {
2132 return module.outputFilePath
2133}
2134
2135// from android.ApexModule
2136func (module *sdkLibraryXml) AvailableFor(what string) bool {
2137 return true
2138}
2139
2140func (module *sdkLibraryXml) DepsMutator(ctx android.BottomUpMutatorContext) {
2141 // do nothing
2142}
2143
Dan Albertc8060532020-07-22 22:32:17 -07002144func (module *sdkLibraryXml) ShouldSupportSdkVersion(ctx android.BaseModuleContext,
2145 sdkVersion android.ApiLevel) error {
Jooyung Han749dc692020-04-15 11:03:39 +09002146 // sdkLibraryXml doesn't need to be checked separately because java_sdk_library is checked
2147 return nil
2148}
2149
Jiyong Parke3833882020-02-17 17:28:10 +09002150// File path to the runtime implementation library
Colin Cross56a83212020-09-15 18:30:11 -07002151func (module *sdkLibraryXml) implPath(ctx android.ModuleContext) string {
Jiyong Parke3833882020-02-17 17:28:10 +09002152 implName := proptools.String(module.properties.Lib_name)
Colin Cross56a83212020-09-15 18:30:11 -07002153 if apexInfo := ctx.Provider(android.ApexInfoProvider).(android.ApexInfo); !apexInfo.IsForPlatform() {
Colin Crosse07f2312020-08-13 11:24:56 -07002154 // TODO(b/146468504): ApexVariationName() is only a soong module name, not apex name.
Jiyong Parke3833882020-02-17 17:28:10 +09002155 // In most cases, this works fine. But when apex_name is set or override_apex is used
2156 // this can be wrong.
Colin Cross56a83212020-09-15 18:30:11 -07002157 return fmt.Sprintf("/apex/%s/javalib/%s.jar", apexInfo.ApexVariationName, implName)
Jiyong Parke3833882020-02-17 17:28:10 +09002158 }
2159 partition := "system"
2160 if module.SocSpecific() {
2161 partition = "vendor"
2162 } else if module.DeviceSpecific() {
2163 partition = "odm"
2164 } else if module.ProductSpecific() {
2165 partition = "product"
2166 } else if module.SystemExtSpecific() {
2167 partition = "system_ext"
2168 }
2169 return "/" + partition + "/framework/" + implName + ".jar"
2170}
2171
2172func (module *sdkLibraryXml) GenerateAndroidBuildActions(ctx android.ModuleContext) {
Colin Cross56a83212020-09-15 18:30:11 -07002173 module.hideApexVariantFromMake = !ctx.Provider(android.ApexInfoProvider).(android.ApexInfo).IsForPlatform()
2174
Jiyong Parke3833882020-02-17 17:28:10 +09002175 libName := proptools.String(module.properties.Lib_name)
Colin Cross56a83212020-09-15 18:30:11 -07002176 xmlContent := fmt.Sprintf(permissionsTemplate, libName, module.implPath(ctx))
Jiyong Parke3833882020-02-17 17:28:10 +09002177
2178 module.outputFilePath = android.PathForModuleOut(ctx, libName+".xml").OutputPath
Colin Crossf1a035e2020-11-16 17:32:30 -08002179 rule := android.NewRuleBuilder(pctx, ctx)
Jiyong Parke3833882020-02-17 17:28:10 +09002180 rule.Command().
2181 Text("/bin/bash -c \"echo -e '" + xmlContent + "'\" > ").
2182 Output(module.outputFilePath)
2183
Colin Crossf1a035e2020-11-16 17:32:30 -08002184 rule.Build("java_sdk_xml", "Permission XML")
Jiyong Parke3833882020-02-17 17:28:10 +09002185
2186 module.installDirPath = android.PathForModuleInstall(ctx, "etc", module.SubDir())
2187}
2188
2189func (module *sdkLibraryXml) AndroidMkEntries() []android.AndroidMkEntries {
Colin Cross56a83212020-09-15 18:30:11 -07002190 if module.hideApexVariantFromMake {
Jiyong Parke3833882020-02-17 17:28:10 +09002191 return []android.AndroidMkEntries{android.AndroidMkEntries{
2192 Disabled: true,
2193 }}
2194 }
2195
2196 return []android.AndroidMkEntries{android.AndroidMkEntries{
2197 Class: "ETC",
2198 OutputFile: android.OptionalPathForPath(module.outputFilePath),
2199 ExtraEntries: []android.AndroidMkExtraEntriesFunc{
2200 func(entries *android.AndroidMkEntries) {
2201 entries.SetString("LOCAL_MODULE_TAGS", "optional")
2202 entries.SetString("LOCAL_MODULE_PATH", module.installDirPath.ToMakePath().String())
2203 entries.SetString("LOCAL_INSTALLED_MODULE_STEM", module.outputFilePath.Base())
2204 },
2205 },
2206 }}
2207}
Paul Duffindd46f712020-02-10 13:37:10 +00002208
2209type sdkLibrarySdkMemberType struct {
2210 android.SdkMemberTypeBase
2211}
2212
2213func (s *sdkLibrarySdkMemberType) AddDependencies(mctx android.BottomUpMutatorContext, dependencyTag blueprint.DependencyTag, names []string) {
2214 mctx.AddVariationDependencies(nil, dependencyTag, names...)
2215}
2216
2217func (s *sdkLibrarySdkMemberType) IsInstance(module android.Module) bool {
2218 _, ok := module.(*SdkLibrary)
2219 return ok
2220}
2221
2222func (s *sdkLibrarySdkMemberType) AddPrebuiltModule(ctx android.SdkMemberContext, member android.SdkMember) android.BpModule {
2223 return ctx.SnapshotBuilder().AddPrebuiltModule(member, "java_sdk_library_import")
2224}
2225
2226func (s *sdkLibrarySdkMemberType) CreateVariantPropertiesStruct() android.SdkMemberProperties {
2227 return &sdkLibrarySdkMemberProperties{}
2228}
2229
2230type sdkLibrarySdkMemberProperties struct {
2231 android.SdkMemberPropertiesBase
2232
2233 // Scope to per scope properties.
2234 Scopes map[*apiScope]scopeProperties
2235
2236 // Additional libraries that the exported stubs libraries depend upon.
2237 Libs []string
Paul Duffin3d1248c2020-04-09 00:10:17 +01002238
2239 // The Java stubs source files.
2240 Stub_srcs []string
Paul Duffinf7a64332020-05-13 16:54:55 +01002241
2242 // The naming scheme.
2243 Naming_scheme *string
Paul Duffind7eb1c22020-05-26 20:57:10 +01002244
2245 // True if the java_sdk_library_import is for a shared library, false
2246 // otherwise.
2247 Shared_library *bool
Paul Duffina2ae7e02020-09-11 11:55:00 +01002248
2249 // The paths to the doctag files to add to the prebuilt.
2250 Doctag_paths android.Paths
Paul Duffindd46f712020-02-10 13:37:10 +00002251}
2252
2253type scopeProperties struct {
Paul Duffin1fd005d2020-04-09 01:08:11 +01002254 Jars android.Paths
2255 StubsSrcJar android.Path
2256 CurrentApiFile android.Path
2257 RemovedApiFile android.Path
2258 SdkVersion string
Paul Duffindd46f712020-02-10 13:37:10 +00002259}
2260
2261func (s *sdkLibrarySdkMemberProperties) PopulateFromVariant(ctx android.SdkMemberContext, variant android.Module) {
2262 sdk := variant.(*SdkLibrary)
2263
2264 s.Scopes = make(map[*apiScope]scopeProperties)
2265 for _, apiScope := range allApiScopes {
Paul Duffin803a9562020-05-20 11:52:25 +01002266 paths := sdk.findScopePaths(apiScope)
2267 if paths == nil {
2268 continue
2269 }
2270
Paul Duffindd46f712020-02-10 13:37:10 +00002271 jars := paths.stubsImplPath
2272 if len(jars) > 0 {
2273 properties := scopeProperties{}
2274 properties.Jars = jars
Paul Duffin780c5f42020-05-12 15:52:55 +01002275 properties.SdkVersion = sdk.sdkVersionForStubsLibrary(ctx.SdkModuleContext(), apiScope)
Paul Duffin0f8faff2020-05-20 16:18:00 +01002276 properties.StubsSrcJar = paths.stubsSrcJar.Path()
Paul Duffin10269f12020-06-19 18:39:55 +01002277 if paths.currentApiFilePath.Valid() {
2278 properties.CurrentApiFile = paths.currentApiFilePath.Path()
2279 }
2280 if paths.removedApiFilePath.Valid() {
2281 properties.RemovedApiFile = paths.removedApiFilePath.Path()
2282 }
Paul Duffindd46f712020-02-10 13:37:10 +00002283 s.Scopes[apiScope] = properties
2284 }
2285 }
2286
2287 s.Libs = sdk.properties.Libs
Paul Duffindfa131e2020-05-15 20:37:11 +01002288 s.Naming_scheme = sdk.commonSdkLibraryProperties.Naming_scheme
Paul Duffind7eb1c22020-05-26 20:57:10 +01002289 s.Shared_library = proptools.BoolPtr(sdk.sharedLibrary())
Paul Duffina2ae7e02020-09-11 11:55:00 +01002290 s.Doctag_paths = sdk.doctagPaths
Paul Duffindd46f712020-02-10 13:37:10 +00002291}
2292
2293func (s *sdkLibrarySdkMemberProperties) AddToPropertySet(ctx android.SdkMemberContext, propertySet android.BpPropertySet) {
Paul Duffinf7a64332020-05-13 16:54:55 +01002294 if s.Naming_scheme != nil {
2295 propertySet.AddProperty("naming_scheme", proptools.String(s.Naming_scheme))
2296 }
Paul Duffind7eb1c22020-05-26 20:57:10 +01002297 if s.Shared_library != nil {
2298 propertySet.AddProperty("shared_library", *s.Shared_library)
2299 }
Paul Duffinf7a64332020-05-13 16:54:55 +01002300
Paul Duffindd46f712020-02-10 13:37:10 +00002301 for _, apiScope := range allApiScopes {
2302 if properties, ok := s.Scopes[apiScope]; ok {
Paul Duffin6b836ba2020-05-13 19:19:49 +01002303 scopeSet := propertySet.AddPropertySet(apiScope.propertyName)
Paul Duffindd46f712020-02-10 13:37:10 +00002304
Paul Duffin3d1248c2020-04-09 00:10:17 +01002305 scopeDir := filepath.Join("sdk_library", s.OsPrefix(), apiScope.name)
2306
Paul Duffindd46f712020-02-10 13:37:10 +00002307 var jars []string
2308 for _, p := range properties.Jars {
Paul Duffin3d1248c2020-04-09 00:10:17 +01002309 dest := filepath.Join(scopeDir, ctx.Name()+"-stubs.jar")
Paul Duffindd46f712020-02-10 13:37:10 +00002310 ctx.SnapshotBuilder().CopyToSnapshot(p, dest)
2311 jars = append(jars, dest)
2312 }
2313 scopeSet.AddProperty("jars", jars)
2314
Paul Duffin3d1248c2020-04-09 00:10:17 +01002315 // Merge the stubs source jar into the snapshot zip so that when it is unpacked
Paul Duffinab5ac8f2020-11-18 16:37:35 +00002316 // the source files are also unpacked.
Paul Duffin3d1248c2020-04-09 00:10:17 +01002317 snapshotRelativeDir := filepath.Join(scopeDir, ctx.Name()+"_stub_sources")
2318 ctx.SnapshotBuilder().UnzipToSnapshot(properties.StubsSrcJar, snapshotRelativeDir)
Paul Duffinab5ac8f2020-11-18 16:37:35 +00002319 scopeSet.AddProperty("stub_srcs", []string{snapshotRelativeDir})
Paul Duffin3d1248c2020-04-09 00:10:17 +01002320
Paul Duffin1fd005d2020-04-09 01:08:11 +01002321 if properties.CurrentApiFile != nil {
2322 currentApiSnapshotPath := filepath.Join(scopeDir, ctx.Name()+".txt")
2323 ctx.SnapshotBuilder().CopyToSnapshot(properties.CurrentApiFile, currentApiSnapshotPath)
2324 scopeSet.AddProperty("current_api", currentApiSnapshotPath)
2325 }
2326
2327 if properties.RemovedApiFile != nil {
2328 removedApiSnapshotPath := filepath.Join(scopeDir, ctx.Name()+"-removed.txt")
Paul Duffin3dbf9fd2020-06-02 13:00:02 +01002329 ctx.SnapshotBuilder().CopyToSnapshot(properties.RemovedApiFile, removedApiSnapshotPath)
Paul Duffin1fd005d2020-04-09 01:08:11 +01002330 scopeSet.AddProperty("removed_api", removedApiSnapshotPath)
2331 }
2332
Paul Duffindd46f712020-02-10 13:37:10 +00002333 if properties.SdkVersion != "" {
2334 scopeSet.AddProperty("sdk_version", properties.SdkVersion)
2335 }
2336 }
2337 }
2338
Paul Duffina2ae7e02020-09-11 11:55:00 +01002339 if len(s.Doctag_paths) > 0 {
2340 dests := []string{}
2341 for _, p := range s.Doctag_paths {
2342 dest := filepath.Join("doctags", p.Rel())
2343 ctx.SnapshotBuilder().CopyToSnapshot(p, dest)
2344 dests = append(dests, dest)
2345 }
2346 propertySet.AddProperty("doctag_files", dests)
2347 }
2348
Paul Duffindd46f712020-02-10 13:37:10 +00002349 if len(s.Libs) > 0 {
2350 propertySet.AddPropertyWithTag("libs", s.Libs, ctx.SnapshotBuilder().SdkMemberReferencePropertyTag(false))
2351 }
2352}