blob: fb51e10a5d28f406ad4348dec474f74df559feb6 [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"
Jiyong Park82484c02018-04-23 21:41:26 +090022 "sort"
Jiyong Parkc678ad32018-04-10 13:07:10 +090023 "strings"
Jiyong Park82484c02018-04-23 21:41:26 +090024 "sync"
Jiyong Parkc678ad32018-04-10 13:07:10 +090025
Paul Duffind1b3a922020-01-22 11:57:20 +000026 "github.com/google/blueprint"
Jiyong Parkc678ad32018-04-10 13:07:10 +090027 "github.com/google/blueprint/proptools"
Paul Duffin46a26a82020-04-07 19:27:04 +010028
29 "android/soong/android"
Jiyong Parkc678ad32018-04-10 13:07:10 +090030)
31
Jooyung Han58f26ab2019-12-18 15:34:32 +090032const (
Jiyong Parkc678ad32018-04-10 13:07:10 +090033 sdkStubsLibrarySuffix = ".stubs"
34 sdkSystemApiSuffix = ".system"
Jiyong Parkdf130542018-04-27 16:29:21 +090035 sdkTestApiSuffix = ".test"
Paul Duffin91b883d2020-02-11 13:05:28 +000036 sdkStubsSourceSuffix = ".stubs.source"
Jiyong Parkc678ad32018-04-10 13:07:10 +090037 sdkXmlFileSuffix = ".xml"
Jiyong Parke3833882020-02-17 17:28:10 +090038 permissionsTemplate = `<?xml version=\"1.0\" encoding=\"utf-8\"?>\n` +
Jooyung Han624058e2019-12-24 18:38:06 +090039 `<!-- Copyright (C) 2018 The Android Open Source Project\n` +
40 `\n` +
Jiyong Parke3833882020-02-17 17:28:10 +090041 ` Licensed under the Apache License, Version 2.0 (the \"License\");\n` +
Jooyung Han624058e2019-12-24 18:38:06 +090042 ` you may not use this file except in compliance with the License.\n` +
43 ` You may obtain a copy of the License at\n` +
44 `\n` +
45 ` http://www.apache.org/licenses/LICENSE-2.0\n` +
46 `\n` +
47 ` Unless required by applicable law or agreed to in writing, software\n` +
Jiyong Parke3833882020-02-17 17:28:10 +090048 ` distributed under the License is distributed on an \"AS IS\" BASIS,\n` +
Jooyung Han624058e2019-12-24 18:38:06 +090049 ` WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\n` +
50 ` See the License for the specific language governing permissions and\n` +
51 ` limitations under the License.\n` +
52 `-->\n` +
53 `<permissions>\n` +
Jiyong Parke3833882020-02-17 17:28:10 +090054 ` <library name=\"%s\" file=\"%s\"/>\n` +
Jooyung Han624058e2019-12-24 18:38:06 +090055 `</permissions>\n`
Jiyong Parkc678ad32018-04-10 13:07:10 +090056)
57
Paul Duffind1b3a922020-01-22 11:57:20 +000058// A tag to associated a dependency with a specific api scope.
59type scopeDependencyTag struct {
60 blueprint.BaseDependencyTag
61 name string
62 apiScope *apiScope
63}
64
65// Provides information about an api scope, e.g. public, system, test.
66type apiScope struct {
67 // The name of the api scope, e.g. public, system, test
68 name string
69
Paul Duffin97b53b82020-05-05 14:40:52 +010070 // The api scope that this scope extends.
71 extends *apiScope
72
Paul Duffin3375e352020-04-28 10:44:03 +010073 // The legacy enabled status for a specific scope can be dependent on other
74 // properties that have been specified on the library so it is provided by
75 // a function that can determine the status by examining those properties.
76 legacyEnabledStatus func(module *SdkLibrary) bool
77
78 // The default enabled status for non-legacy behavior, which is triggered by
79 // explicitly enabling at least one api scope.
80 defaultEnabledStatus bool
81
82 // Gets a pointer to the scope specific properties.
83 scopeSpecificProperties func(module *SdkLibrary) *ApiScopeProperties
84
Paul Duffin46a26a82020-04-07 19:27:04 +010085 // The name of the field in the dynamically created structure.
86 fieldName string
87
Paul Duffind1b3a922020-01-22 11:57:20 +000088 // The tag to use to depend on the stubs library module.
89 stubsTag scopeDependencyTag
90
91 // The tag to use to depend on the stubs
92 apiFileTag scopeDependencyTag
93
94 // The scope specific prefix to add to the api file base of "current.txt" or "removed.txt".
95 apiFilePrefix string
96
97 // The scope specific prefix to add to the sdk library module name to construct a scope specific
98 // module name.
99 moduleSuffix string
100
Paul Duffind1b3a922020-01-22 11:57:20 +0000101 // SDK version that the stubs library is built against. Note that this is always
102 // *current. Older stubs library built with a numbered SDK version is created from
103 // the prebuilt jar.
104 sdkVersion string
Paul Duffin1fb487d2020-04-07 18:50:10 +0100105
106 // Extra arguments to pass to droidstubs for this scope.
107 droidstubsArgs []string
Anton Hansson6478ac12020-05-02 11:19:36 +0100108
109 // Whether the api scope can be treated as unstable, and should skip compat checks.
110 unstable bool
Paul Duffind1b3a922020-01-22 11:57:20 +0000111}
112
113// Initialize a scope, creating and adding appropriate dependency tags
114func initApiScope(scope *apiScope) *apiScope {
Paul Duffin46a26a82020-04-07 19:27:04 +0100115 scope.fieldName = proptools.FieldNameForProperty(scope.name)
Paul Duffind1b3a922020-01-22 11:57:20 +0000116 scope.stubsTag = scopeDependencyTag{
117 name: scope.name + "-stubs",
118 apiScope: scope,
119 }
120 scope.apiFileTag = scopeDependencyTag{
121 name: scope.name + "-api",
122 apiScope: scope,
123 }
124 return scope
125}
126
127func (scope *apiScope) stubsModuleName(baseName string) string {
128 return baseName + sdkStubsLibrarySuffix + scope.moduleSuffix
129}
130
131func (scope *apiScope) docsModuleName(baseName string) string {
Paul Duffin91b883d2020-02-11 13:05:28 +0000132 return baseName + sdkStubsSourceSuffix + scope.moduleSuffix
Paul Duffind1b3a922020-01-22 11:57:20 +0000133}
134
Paul Duffin3375e352020-04-28 10:44:03 +0100135func (scope *apiScope) String() string {
136 return scope.name
137}
138
Paul Duffind1b3a922020-01-22 11:57:20 +0000139type apiScopes []*apiScope
140
141func (scopes apiScopes) Strings(accessor func(*apiScope) string) []string {
142 var list []string
143 for _, scope := range scopes {
144 list = append(list, accessor(scope))
145 }
146 return list
147}
148
Jiyong Parkc678ad32018-04-10 13:07:10 +0900149var (
Paul Duffind1b3a922020-01-22 11:57:20 +0000150 apiScopePublic = initApiScope(&apiScope{
Paul Duffin3375e352020-04-28 10:44:03 +0100151 name: "public",
152
153 // Public scope is enabled by default for both legacy and non-legacy modes.
154 legacyEnabledStatus: func(module *SdkLibrary) bool {
155 return true
156 },
157 defaultEnabledStatus: true,
158
159 scopeSpecificProperties: func(module *SdkLibrary) *ApiScopeProperties {
160 return &module.sdkLibraryProperties.Public
161 },
Paul Duffind1b3a922020-01-22 11:57:20 +0000162 sdkVersion: "current",
163 })
164 apiScopeSystem = initApiScope(&apiScope{
Paul Duffin3375e352020-04-28 10:44:03 +0100165 name: "system",
166 extends: apiScopePublic,
167 legacyEnabledStatus: (*SdkLibrary).generateTestAndSystemScopesByDefault,
168 scopeSpecificProperties: func(module *SdkLibrary) *ApiScopeProperties {
169 return &module.sdkLibraryProperties.System
170 },
Anton Hansson6affb1f2020-04-28 16:47:41 +0100171 apiFilePrefix: "system-",
172 moduleSuffix: sdkSystemApiSuffix,
173 sdkVersion: "system_current",
174 droidstubsArgs: []string{"-showAnnotation android.annotation.SystemApi"},
Paul Duffind1b3a922020-01-22 11:57:20 +0000175 })
176 apiScopeTest = initApiScope(&apiScope{
Paul Duffin3375e352020-04-28 10:44:03 +0100177 name: "test",
178 extends: apiScopePublic,
179 legacyEnabledStatus: (*SdkLibrary).generateTestAndSystemScopesByDefault,
180 scopeSpecificProperties: func(module *SdkLibrary) *ApiScopeProperties {
181 return &module.sdkLibraryProperties.Test
182 },
Anton Hansson6affb1f2020-04-28 16:47:41 +0100183 apiFilePrefix: "test-",
184 moduleSuffix: sdkTestApiSuffix,
185 sdkVersion: "test_current",
186 droidstubsArgs: []string{"-showAnnotation android.annotation.TestApi"},
Anton Hansson6478ac12020-05-02 11:19:36 +0100187 unstable: true,
Paul Duffind1b3a922020-01-22 11:57:20 +0000188 })
Paul Duffin8f265b92020-04-28 14:13:56 +0100189 apiScopeModuleLib = initApiScope(&apiScope{
190 name: "module_lib",
191 extends: apiScopeSystem,
192 // Module_lib scope is disabled by default in legacy mode.
193 //
194 // Enabling this would break existing usages.
195 legacyEnabledStatus: func(module *SdkLibrary) bool {
196 return false
197 },
198 scopeSpecificProperties: func(module *SdkLibrary) *ApiScopeProperties {
199 return &module.sdkLibraryProperties.Module_lib
200 },
201 apiFilePrefix: "module-lib-",
202 moduleSuffix: ".module_lib",
203 sdkVersion: "module_current",
204 droidstubsArgs: []string{
205 "--show-annotation android.annotation.SystemApi\\(client=android.annotation.SystemApi.Client.MODULE_LIBRARIES\\)",
206 },
207 })
Paul Duffind1b3a922020-01-22 11:57:20 +0000208 allApiScopes = apiScopes{
209 apiScopePublic,
210 apiScopeSystem,
211 apiScopeTest,
Paul Duffin8f265b92020-04-28 14:13:56 +0100212 apiScopeModuleLib,
Paul Duffind1b3a922020-01-22 11:57:20 +0000213 }
Jiyong Parkc678ad32018-04-10 13:07:10 +0900214)
215
Jiyong Park82484c02018-04-23 21:41:26 +0900216var (
217 javaSdkLibrariesLock sync.Mutex
218)
219
Jiyong Parkc678ad32018-04-10 13:07:10 +0900220// TODO: these are big features that are currently missing
Jiyong Park1be96912018-05-28 18:02:19 +0900221// 1) disallowing linking to the runtime shared lib
222// 2) HTML generation
Jiyong Parkc678ad32018-04-10 13:07:10 +0900223
224func init() {
Paul Duffin43dc1cc2019-12-19 11:18:54 +0000225 RegisterSdkLibraryBuildComponents(android.InitRegistrationContext)
Jiyong Parkc678ad32018-04-10 13:07:10 +0900226
Jiyong Park82484c02018-04-23 21:41:26 +0900227 android.RegisterMakeVarsProvider(pctx, func(ctx android.MakeVarsContext) {
228 javaSdkLibraries := javaSdkLibraries(ctx.Config())
229 sort.Strings(*javaSdkLibraries)
230 ctx.Strict("JAVA_SDK_LIBRARIES", strings.Join(*javaSdkLibraries, " "))
231 })
Paul Duffindd46f712020-02-10 13:37:10 +0000232
233 // Register sdk member types.
234 android.RegisterSdkMemberType(&sdkLibrarySdkMemberType{
235 android.SdkMemberTypeBase{
236 PropertyName: "java_sdk_libs",
237 SupportsSdk: true,
238 },
239 })
Jiyong Parkc678ad32018-04-10 13:07:10 +0900240}
241
Paul Duffin43dc1cc2019-12-19 11:18:54 +0000242func RegisterSdkLibraryBuildComponents(ctx android.RegistrationContext) {
243 ctx.RegisterModuleType("java_sdk_library", SdkLibraryFactory)
244 ctx.RegisterModuleType("java_sdk_library_import", sdkLibraryImportFactory)
245}
246
Paul Duffin3375e352020-04-28 10:44:03 +0100247// Properties associated with each api scope.
248type ApiScopeProperties struct {
249 // Indicates whether the api surface is generated.
250 //
251 // If this is set for any scope then all scopes must explicitly specify if they
252 // are enabled. This is to prevent new usages from depending on legacy behavior.
253 //
254 // Otherwise, if this is not set for any scope then the default behavior is
255 // scope specific so please refer to the scope specific property documentation.
256 Enabled *bool
257}
258
Jiyong Parkc678ad32018-04-10 13:07:10 +0900259type sdkLibraryProperties struct {
Sundong Ahnf043cf62018-06-25 16:04:37 +0900260 // List of Java libraries that will be in the classpath when building stubs
261 Stub_only_libs []string `android:"arch_variant"`
262
Paul Duffin7a586d32019-12-30 17:09:34 +0000263 // list of package names that will be documented and publicized as API.
264 // This allows the API to be restricted to a subset of the source files provided.
265 // If this is unspecified then all the source files will be treated as being part
266 // of the API.
Jiyong Parkc678ad32018-04-10 13:07:10 +0900267 Api_packages []string
268
Jiyong Park5a2c9d72018-05-01 22:25:41 +0900269 // list of package names that must be hidden from the API
270 Hidden_api_packages []string
271
Paul Duffin749f98f2019-12-30 17:23:46 +0000272 // the relative path to the directory containing the api specification files.
273 // Defaults to "api".
274 Api_dir *string
275
Paul Duffin43db9be2019-12-30 17:35:49 +0000276 // If set to true there is no runtime library.
277 Api_only *bool
278
Paul Duffin11512472019-02-11 15:55:17 +0000279 // local files that are used within user customized droiddoc options.
280 Droiddoc_option_files []string
281
282 // additional droiddoc options
283 // Available variables for substitution:
284 //
285 // $(location <label>): the path to the droiddoc_option_files with name <label>
Sundong Ahndd567f92018-07-31 17:19:11 +0900286 Droiddoc_options []string
287
Sundong Ahn054b19a2018-10-19 13:46:09 +0900288 // a list of top-level directories containing files to merge qualifier annotations
289 // (i.e. those intended to be included in the stubs written) from.
290 Merge_annotations_dirs []string
291
292 // a list of top-level directories containing Java stub files to merge show/hide annotations from.
293 Merge_inclusion_annotations_dirs []string
294
295 // If set to true, the path of dist files is apistubs/core. Defaults to false.
296 Core_lib *bool
297
Sundong Ahn80a87b32019-05-13 15:02:50 +0900298 // don't create dist rules.
299 No_dist *bool `blueprint:"mutated"`
300
Paul Duffin3375e352020-04-28 10:44:03 +0100301 // indicates whether system and test apis should be generated.
302 Generate_system_and_test_apis bool `blueprint:"mutated"`
303
304 // The properties specific to the public api scope
305 //
306 // Unless explicitly specified by using public.enabled the public api scope is
307 // enabled by default in both legacy and non-legacy mode.
308 Public ApiScopeProperties
309
310 // The properties specific to the system api scope
311 //
312 // In legacy mode the system api scope is enabled by default when sdk_version
313 // is set to something other than "none".
314 //
315 // In non-legacy mode the system api scope is disabled by default.
316 System ApiScopeProperties
317
318 // The properties specific to the test api scope
319 //
320 // In legacy mode the test api scope is enabled by default when sdk_version
321 // is set to something other than "none".
322 //
323 // In non-legacy mode the test api scope is disabled by default.
324 Test ApiScopeProperties
Paul Duffin37e0b772019-12-30 17:20:10 +0000325
Paul Duffin8f265b92020-04-28 14:13:56 +0100326 // The properties specific to the module_lib api scope
327 //
328 // Unless explicitly specified by using test.enabled the module_lib api scope is
329 // disabled by default.
330 Module_lib ApiScopeProperties
331
Jiyong Parkc678ad32018-04-10 13:07:10 +0900332 // TODO: determines whether to create HTML doc or not
333 //Html_doc *bool
334}
335
Paul Duffind1b3a922020-01-22 11:57:20 +0000336type scopePaths struct {
Paul Duffin1fd005d2020-04-09 01:08:11 +0100337 stubsHeaderPath android.Paths
338 stubsImplPath android.Paths
339 currentApiFilePath android.Path
340 removedApiFilePath android.Path
341 stubsSrcJar android.Path
Paul Duffind1b3a922020-01-22 11:57:20 +0000342}
343
Paul Duffin56d44902020-01-31 13:36:25 +0000344// Common code between sdk library and sdk library import
345type commonToSdkLibraryAndImport struct {
346 scopePaths map[*apiScope]*scopePaths
347}
348
349func (c *commonToSdkLibraryAndImport) getScopePaths(scope *apiScope) *scopePaths {
350 if c.scopePaths == nil {
351 c.scopePaths = make(map[*apiScope]*scopePaths)
352 }
353 paths := c.scopePaths[scope]
354 if paths == nil {
355 paths = &scopePaths{}
356 c.scopePaths[scope] = paths
357 }
358
359 return paths
360}
361
Inseob Kimc0907f12019-02-08 21:00:45 +0900362type SdkLibrary struct {
Sundong Ahn054b19a2018-10-19 13:46:09 +0900363 Library
Jiyong Parkc678ad32018-04-10 13:07:10 +0900364
Sundong Ahn054b19a2018-10-19 13:46:09 +0900365 sdkLibraryProperties sdkLibraryProperties
Jiyong Parkc678ad32018-04-10 13:07:10 +0900366
Paul Duffin3375e352020-04-28 10:44:03 +0100367 // Map from api scope to the scope specific property structure.
368 scopeToProperties map[*apiScope]*ApiScopeProperties
369
Paul Duffin56d44902020-01-31 13:36:25 +0000370 commonToSdkLibraryAndImport
Jiyong Parkc678ad32018-04-10 13:07:10 +0900371}
372
Inseob Kimc0907f12019-02-08 21:00:45 +0900373var _ Dependency = (*SdkLibrary)(nil)
374var _ SdkLibraryDependency = (*SdkLibrary)(nil)
Colin Cross897d2ed2019-02-11 14:03:51 -0800375
Paul Duffin3375e352020-04-28 10:44:03 +0100376func (module *SdkLibrary) generateTestAndSystemScopesByDefault() bool {
377 return module.sdkLibraryProperties.Generate_system_and_test_apis
378}
379
380func (module *SdkLibrary) getGeneratedApiScopes(ctx android.EarlyModuleContext) apiScopes {
381 // Check to see if any scopes have been explicitly enabled. If any have then all
382 // must be.
383 anyScopesExplicitlyEnabled := false
384 for _, scope := range allApiScopes {
385 scopeProperties := module.scopeToProperties[scope]
386 if scopeProperties.Enabled != nil {
387 anyScopesExplicitlyEnabled = true
388 break
389 }
Paul Duffind1b3a922020-01-22 11:57:20 +0000390 }
Paul Duffin3375e352020-04-28 10:44:03 +0100391
392 var generatedScopes apiScopes
393 enabledScopes := make(map[*apiScope]struct{})
394 for _, scope := range allApiScopes {
395 scopeProperties := module.scopeToProperties[scope]
396 // If any scopes are explicitly enabled then ignore the legacy enabled status.
397 // This is to ensure that any new usages of this module type do not rely on legacy
398 // behaviour.
399 defaultEnabledStatus := false
400 if anyScopesExplicitlyEnabled {
401 defaultEnabledStatus = scope.defaultEnabledStatus
402 } else {
403 defaultEnabledStatus = scope.legacyEnabledStatus(module)
404 }
405 enabled := proptools.BoolDefault(scopeProperties.Enabled, defaultEnabledStatus)
406 if enabled {
407 enabledScopes[scope] = struct{}{}
408 generatedScopes = append(generatedScopes, scope)
409 }
410 }
411
412 // Now check to make sure that any scope that is extended by an enabled scope is also
413 // enabled.
414 for _, scope := range allApiScopes {
415 if _, ok := enabledScopes[scope]; ok {
416 extends := scope.extends
417 if extends != nil {
418 if _, ok := enabledScopes[extends]; !ok {
419 ctx.ModuleErrorf("enabled api scope %q depends on disabled scope %q", scope, extends)
420 }
421 }
422 }
423 }
424
425 return generatedScopes
Paul Duffind1b3a922020-01-22 11:57:20 +0000426}
427
Paul Duffine74ac732020-02-06 13:51:46 +0000428var xmlPermissionsFileTag = dependencyTag{name: "xml-permissions-file"}
429
Jiyong Parke3833882020-02-17 17:28:10 +0900430func IsXmlPermissionsFileDepTag(depTag blueprint.DependencyTag) bool {
431 if dt, ok := depTag.(dependencyTag); ok {
432 return dt == xmlPermissionsFileTag
433 }
434 return false
435}
436
Inseob Kimc0907f12019-02-08 21:00:45 +0900437func (module *SdkLibrary) DepsMutator(ctx android.BottomUpMutatorContext) {
Paul Duffin3375e352020-04-28 10:44:03 +0100438 for _, apiScope := range module.getGeneratedApiScopes(ctx) {
Paul Duffind1b3a922020-01-22 11:57:20 +0000439 // Add dependencies to the stubs library
Paul Duffin50061512020-01-21 16:31:05 +0000440 ctx.AddVariationDependencies(nil, apiScope.stubsTag, module.stubsName(apiScope))
Paul Duffind1b3a922020-01-22 11:57:20 +0000441
Paul Duffin50061512020-01-21 16:31:05 +0000442 // And the api file
Paul Duffind1b3a922020-01-22 11:57:20 +0000443 ctx.AddVariationDependencies(nil, apiScope.apiFileTag, module.docsName(apiScope))
Sundong Ahn054b19a2018-10-19 13:46:09 +0900444 }
445
Paul Duffine74ac732020-02-06 13:51:46 +0000446 if !proptools.Bool(module.sdkLibraryProperties.Api_only) {
447 // Add dependency to the rule for generating the xml permissions file
Jiyong Parke3833882020-02-17 17:28:10 +0900448 ctx.AddDependency(module, xmlPermissionsFileTag, module.xmlFileName())
Paul Duffine74ac732020-02-06 13:51:46 +0000449 }
450
Sundong Ahn054b19a2018-10-19 13:46:09 +0900451 module.Library.deps(ctx)
Jiyong Parkc678ad32018-04-10 13:07:10 +0900452}
453
Inseob Kimc0907f12019-02-08 21:00:45 +0900454func (module *SdkLibrary) GenerateAndroidBuildActions(ctx android.ModuleContext) {
Paul Duffin43db9be2019-12-30 17:35:49 +0000455 // Don't build an implementation library if this is api only.
456 if !proptools.Bool(module.sdkLibraryProperties.Api_only) {
457 module.Library.GenerateAndroidBuildActions(ctx)
458 }
Sundong Ahn054b19a2018-10-19 13:46:09 +0900459
Sundong Ahn57368eb2018-07-06 11:20:23 +0900460 // Record the paths to the header jars of the library (stubs and impl).
Paul Duffind1b3a922020-01-22 11:57:20 +0000461 // When this java_sdk_library is depended upon from others via "libs" property,
Jiyong Parkc678ad32018-04-10 13:07:10 +0900462 // the recorded paths will be returned depending on the link type of the caller.
463 ctx.VisitDirectDeps(func(to android.Module) {
464 otherName := ctx.OtherModuleName(to)
465 tag := ctx.OtherModuleDependencyTag(to)
466
Sundong Ahn57368eb2018-07-06 11:20:23 +0900467 if lib, ok := to.(Dependency); ok {
Paul Duffind1b3a922020-01-22 11:57:20 +0000468 if scopeTag, ok := tag.(scopeDependencyTag); ok {
469 apiScope := scopeTag.apiScope
470 scopePaths := module.getScopePaths(apiScope)
471 scopePaths.stubsHeaderPath = lib.HeaderJars()
472 scopePaths.stubsImplPath = lib.ImplementationJars()
Jiyong Parkc678ad32018-04-10 13:07:10 +0900473 }
474 }
Paul Duffin3d1248c2020-04-09 00:10:17 +0100475 if doc, ok := to.(ApiStubsProvider); ok {
Paul Duffind1b3a922020-01-22 11:57:20 +0000476 if scopeTag, ok := tag.(scopeDependencyTag); ok {
477 apiScope := scopeTag.apiScope
478 scopePaths := module.getScopePaths(apiScope)
Paul Duffin1fd005d2020-04-09 01:08:11 +0100479 scopePaths.currentApiFilePath = doc.ApiFilePath()
480 scopePaths.removedApiFilePath = doc.RemovedApiFilePath()
Paul Duffin3d1248c2020-04-09 00:10:17 +0100481 scopePaths.stubsSrcJar = doc.StubsSrcJar()
Paul Duffind1b3a922020-01-22 11:57:20 +0000482 } else {
Sundong Ahn20e998b2018-07-24 11:19:26 +0900483 ctx.ModuleErrorf("depends on module %q of unknown tag %q", otherName, tag)
484 }
485 }
Jiyong Parkc678ad32018-04-10 13:07:10 +0900486 })
487}
488
Jiyong Park0b0e1b92019-12-03 13:24:29 +0900489func (module *SdkLibrary) AndroidMkEntries() []android.AndroidMkEntries {
Paul Duffin43db9be2019-12-30 17:35:49 +0000490 if proptools.Bool(module.sdkLibraryProperties.Api_only) {
491 return nil
492 }
Jiyong Park0b0e1b92019-12-03 13:24:29 +0900493 entriesList := module.Library.AndroidMkEntries()
494 entries := &entriesList[0]
Jaewoong Jungb0c127c2019-08-29 14:56:03 -0700495 entries.Required = append(entries.Required, module.xmlFileName())
Jiyong Park0b0e1b92019-12-03 13:24:29 +0900496 return entriesList
Jiyong Park82484c02018-04-23 21:41:26 +0900497}
498
Jiyong Parkc678ad32018-04-10 13:07:10 +0900499// Module name of the stubs library
Paul Duffind1b3a922020-01-22 11:57:20 +0000500func (module *SdkLibrary) stubsName(apiScope *apiScope) string {
501 return apiScope.stubsModuleName(module.BaseModuleName())
Jiyong Parkc678ad32018-04-10 13:07:10 +0900502}
503
504// Module name of the docs
Paul Duffind1b3a922020-01-22 11:57:20 +0000505func (module *SdkLibrary) docsName(apiScope *apiScope) string {
506 return apiScope.docsModuleName(module.BaseModuleName())
Jiyong Parkc678ad32018-04-10 13:07:10 +0900507}
508
509// Module name of the runtime implementation library
Inseob Kimc0907f12019-02-08 21:00:45 +0900510func (module *SdkLibrary) implName() string {
Sundong Ahn054b19a2018-10-19 13:46:09 +0900511 return module.BaseModuleName()
Jiyong Parkc678ad32018-04-10 13:07:10 +0900512}
513
Jiyong Parkc678ad32018-04-10 13:07:10 +0900514// Module name of the XML file for the lib
Inseob Kimc0907f12019-02-08 21:00:45 +0900515func (module *SdkLibrary) xmlFileName() string {
Jiyong Parkc678ad32018-04-10 13:07:10 +0900516 return module.BaseModuleName() + sdkXmlFileSuffix
517}
518
Anton Hansson5fd5d242020-03-27 19:43:19 +0000519// The dist path of the stub artifacts
520func (module *SdkLibrary) apiDistPath(apiScope *apiScope) string {
521 if module.ModuleBase.Owner() != "" {
522 return path.Join("apistubs", module.ModuleBase.Owner(), apiScope.name)
523 } else if Bool(module.sdkLibraryProperties.Core_lib) {
524 return path.Join("apistubs", "core", apiScope.name)
525 } else {
526 return path.Join("apistubs", "android", apiScope.name)
527 }
528}
529
Paul Duffin12ceb462019-12-24 20:31:31 +0000530// Get the sdk version for use when compiling the stubs library.
Paul Duffinf0229202020-04-29 16:47:28 +0100531func (module *SdkLibrary) sdkVersionForStubsLibrary(mctx android.DefaultableHookContext, apiScope *apiScope) string {
Paul Duffin12ceb462019-12-24 20:31:31 +0000532 sdkDep := decodeSdkDep(mctx, sdkContext(&module.Library))
533 if sdkDep.hasStandardLibs() {
534 // If building against a standard sdk then use the sdk version appropriate for the scope.
Paul Duffind1b3a922020-01-22 11:57:20 +0000535 return apiScope.sdkVersion
Paul Duffin12ceb462019-12-24 20:31:31 +0000536 } else {
537 // Otherwise, use no system module.
538 return "none"
539 }
540}
541
Paul Duffind1b3a922020-01-22 11:57:20 +0000542func (module *SdkLibrary) latestApiFilegroupName(apiScope *apiScope) string {
543 return ":" + module.BaseModuleName() + ".api." + apiScope.name + ".latest"
Jiyong Park58c518b2018-05-12 22:29:12 +0900544}
Jiyong Parkc678ad32018-04-10 13:07:10 +0900545
Paul Duffind1b3a922020-01-22 11:57:20 +0000546func (module *SdkLibrary) latestRemovedApiFilegroupName(apiScope *apiScope) string {
547 return ":" + module.BaseModuleName() + "-removed.api." + apiScope.name + ".latest"
Jiyong Parkc678ad32018-04-10 13:07:10 +0900548}
549
550// Creates a static java library that has API stubs
Paul Duffinf0229202020-04-29 16:47:28 +0100551func (module *SdkLibrary) createStubsLibrary(mctx android.DefaultableHookContext, apiScope *apiScope) {
Jiyong Parkc678ad32018-04-10 13:07:10 +0900552 props := struct {
Sundong Ahn0d7dff42019-12-04 12:53:44 +0900553 Name *string
554 Srcs []string
Paul Duffin367ab912019-12-23 19:40:36 +0000555 Installable *bool
Sundong Ahn0d7dff42019-12-04 12:53:44 +0900556 Sdk_version *string
Paul Duffin12ceb462019-12-24 20:31:31 +0000557 System_modules *string
Paul Duffinab8da5d2020-02-07 16:12:04 +0000558 Patch_module *string
Sundong Ahn0d7dff42019-12-04 12:53:44 +0900559 Libs []string
560 Soc_specific *bool
561 Device_specific *bool
562 Product_specific *bool
563 System_ext_specific *bool
564 Compile_dex *bool
Sundong Ahn0d7dff42019-12-04 12:53:44 +0900565 Java_version *string
566 Product_variables struct {
Jiyong Park82484c02018-04-23 21:41:26 +0900567 Pdk struct {
568 Enabled *bool
569 }
Jiyong Parkc678ad32018-04-10 13:07:10 +0900570 }
Sundong Ahn054b19a2018-10-19 13:46:09 +0900571 Openjdk9 struct {
572 Srcs []string
573 Javacflags []string
574 }
Anton Hansson5fd5d242020-03-27 19:43:19 +0000575 Dist struct {
576 Targets []string
577 Dest *string
578 Dir *string
579 Tag *string
580 }
Jiyong Parkc678ad32018-04-10 13:07:10 +0900581 }{}
582
Jiyong Parkdf130542018-04-27 16:29:21 +0900583 props.Name = proptools.StringPtr(module.stubsName(apiScope))
Jiyong Parkc678ad32018-04-10 13:07:10 +0900584 // sources are generated from the droiddoc
Jiyong Parkdf130542018-04-27 16:29:21 +0900585 props.Srcs = []string{":" + module.docsName(apiScope)}
Paul Duffin12ceb462019-12-24 20:31:31 +0000586 sdkVersion := module.sdkVersionForStubsLibrary(mctx, apiScope)
Paul Duffin52d398a2019-06-11 12:31:14 +0100587 props.Sdk_version = proptools.StringPtr(sdkVersion)
Paul Duffin12ceb462019-12-24 20:31:31 +0000588 props.System_modules = module.Library.Module.deviceProperties.System_modules
Paul Duffinab8da5d2020-02-07 16:12:04 +0000589 props.Patch_module = module.Library.Module.properties.Patch_module
Paul Duffin367ab912019-12-23 19:40:36 +0000590 props.Installable = proptools.BoolPtr(false)
Sundong Ahn054b19a2018-10-19 13:46:09 +0900591 props.Libs = module.sdkLibraryProperties.Stub_only_libs
Jiyong Park82484c02018-04-23 21:41:26 +0900592 props.Product_variables.Pdk.Enabled = proptools.BoolPtr(false)
Sundong Ahn054b19a2018-10-19 13:46:09 +0900593 props.Openjdk9.Srcs = module.Library.Module.properties.Openjdk9.Srcs
594 props.Openjdk9.Javacflags = module.Library.Module.properties.Openjdk9.Javacflags
595 props.Java_version = module.Library.Module.properties.Java_version
596 if module.Library.Module.deviceProperties.Compile_dex != nil {
597 props.Compile_dex = module.Library.Module.deviceProperties.Compile_dex
Sundong Ahndd567f92018-07-31 17:19:11 +0900598 }
Jiyong Parkc678ad32018-04-10 13:07:10 +0900599
600 if module.SocSpecific() {
601 props.Soc_specific = proptools.BoolPtr(true)
602 } else if module.DeviceSpecific() {
603 props.Device_specific = proptools.BoolPtr(true)
604 } else if module.ProductSpecific() {
605 props.Product_specific = proptools.BoolPtr(true)
Sundong Ahn0d7dff42019-12-04 12:53:44 +0900606 } else if module.SystemExtSpecific() {
607 props.System_ext_specific = proptools.BoolPtr(true)
Jiyong Parkc678ad32018-04-10 13:07:10 +0900608 }
Anton Hansson5fd5d242020-03-27 19:43:19 +0000609 // Dist the class jar artifact for sdk builds.
610 if !Bool(module.sdkLibraryProperties.No_dist) {
611 props.Dist.Targets = []string{"sdk", "win_sdk"}
612 props.Dist.Dest = proptools.StringPtr(fmt.Sprintf("%v.jar", module.BaseModuleName()))
613 props.Dist.Dir = proptools.StringPtr(module.apiDistPath(apiScope))
614 props.Dist.Tag = proptools.StringPtr(".jar")
615 }
Jiyong Parkc678ad32018-04-10 13:07:10 +0900616
Colin Cross84dfc3d2019-09-25 11:33:01 -0700617 mctx.CreateModule(LibraryFactory, &props)
Jiyong Parkc678ad32018-04-10 13:07:10 +0900618}
619
Paul Duffin6d0886e2020-04-07 18:49:53 +0100620// Creates a droidstubs module that creates stubs source files from the given full source
Jiyong Parkc678ad32018-04-10 13:07:10 +0900621// files
Paul Duffinf0229202020-04-29 16:47:28 +0100622func (module *SdkLibrary) createStubsSources(mctx android.DefaultableHookContext, apiScope *apiScope) {
Jiyong Parkc678ad32018-04-10 13:07:10 +0900623 props := struct {
Sundong Ahn054b19a2018-10-19 13:46:09 +0900624 Name *string
625 Srcs []string
626 Installable *bool
Paul Duffin52d398a2019-06-11 12:31:14 +0100627 Sdk_version *string
Paul Duffin12ceb462019-12-24 20:31:31 +0000628 System_modules *string
Sundong Ahn054b19a2018-10-19 13:46:09 +0900629 Libs []string
Paul Duffin11512472019-02-11 15:55:17 +0000630 Arg_files []string
Sundong Ahn054b19a2018-10-19 13:46:09 +0900631 Args *string
Sundong Ahn054b19a2018-10-19 13:46:09 +0900632 Java_version *string
633 Merge_annotations_dirs []string
634 Merge_inclusion_annotations_dirs []string
635 Check_api struct {
Inseob Kim38449af2019-02-28 14:24:05 +0900636 Current ApiToCheck
637 Last_released ApiToCheck
638 Ignore_missing_latest_api *bool
Jiyong Park58c518b2018-05-12 22:29:12 +0900639 }
Sundong Ahn1b92c822018-05-29 11:35:17 +0900640 Aidl struct {
641 Include_dirs []string
642 Local_include_dirs []string
643 }
Anton Hansson5fd5d242020-03-27 19:43:19 +0000644 Dist struct {
645 Targets []string
646 Dest *string
647 Dir *string
648 }
Jiyong Parkc678ad32018-04-10 13:07:10 +0900649 }{}
650
Paul Duffin7b78b4d2020-04-28 14:08:32 +0100651 // The stubs source processing uses the same compile time classpath when extracting the
652 // API from the implementation library as it does when compiling it. i.e. the same
653 // * sdk version
654 // * system_modules
655 // * libs (static_libs/libs)
Paul Duffin250e6192019-06-07 10:44:37 +0100656
Jiyong Parkdf130542018-04-27 16:29:21 +0900657 props.Name = proptools.StringPtr(module.docsName(apiScope))
Sundong Ahn054b19a2018-10-19 13:46:09 +0900658 props.Srcs = append(props.Srcs, module.Library.Module.properties.Srcs...)
Paul Duffin7b78b4d2020-04-28 14:08:32 +0100659 props.Sdk_version = module.Library.Module.deviceProperties.Sdk_version
Paul Duffin12ceb462019-12-24 20:31:31 +0000660 props.System_modules = module.Library.Module.deviceProperties.System_modules
Jiyong Parkc678ad32018-04-10 13:07:10 +0900661 props.Installable = proptools.BoolPtr(false)
Sundong Ahne6f0b052018-06-05 16:46:14 +0900662 // A droiddoc module has only one Libs property and doesn't distinguish between
663 // shared libs and static libs. So we need to add both of these libs to Libs property.
Sundong Ahn054b19a2018-10-19 13:46:09 +0900664 props.Libs = module.Library.Module.properties.Libs
665 props.Libs = append(props.Libs, module.Library.Module.properties.Static_libs...)
666 props.Aidl.Include_dirs = module.Library.Module.deviceProperties.Aidl.Include_dirs
667 props.Aidl.Local_include_dirs = module.Library.Module.deviceProperties.Aidl.Local_include_dirs
Sundong Ahn054b19a2018-10-19 13:46:09 +0900668 props.Java_version = module.Library.Module.properties.Java_version
Jiyong Parkc678ad32018-04-10 13:07:10 +0900669
Sundong Ahn054b19a2018-10-19 13:46:09 +0900670 props.Merge_annotations_dirs = module.sdkLibraryProperties.Merge_annotations_dirs
671 props.Merge_inclusion_annotations_dirs = module.sdkLibraryProperties.Merge_inclusion_annotations_dirs
672
Paul Duffin6d0886e2020-04-07 18:49:53 +0100673 droidstubsArgs := []string{}
Paul Duffin235ffff2019-12-24 10:41:30 +0000674 if len(module.sdkLibraryProperties.Api_packages) != 0 {
Paul Duffin6d0886e2020-04-07 18:49:53 +0100675 droidstubsArgs = append(droidstubsArgs, "--stub-packages "+strings.Join(module.sdkLibraryProperties.Api_packages, ":"))
Paul Duffin235ffff2019-12-24 10:41:30 +0000676 }
677 if len(module.sdkLibraryProperties.Hidden_api_packages) != 0 {
Paul Duffin6d0886e2020-04-07 18:49:53 +0100678 droidstubsArgs = append(droidstubsArgs,
Paul Duffin235ffff2019-12-24 10:41:30 +0000679 android.JoinWithPrefix(module.sdkLibraryProperties.Hidden_api_packages, " --hide-package "))
680 }
Paul Duffin6d0886e2020-04-07 18:49:53 +0100681 droidstubsArgs = append(droidstubsArgs, module.sdkLibraryProperties.Droiddoc_options...)
Paul Duffin235ffff2019-12-24 10:41:30 +0000682 disabledWarnings := []string{
683 "MissingPermission",
684 "BroadcastBehavior",
685 "HiddenSuperclass",
686 "DeprecationMismatch",
687 "UnavailableSymbol",
688 "SdkConstant",
689 "HiddenTypeParameter",
690 "Todo",
691 "Typo",
692 }
Paul Duffin6d0886e2020-04-07 18:49:53 +0100693 droidstubsArgs = append(droidstubsArgs, android.JoinWithPrefix(disabledWarnings, "--hide "))
Sundong Ahnfb2721f2018-09-17 13:23:09 +0900694
Paul Duffin1fb487d2020-04-07 18:50:10 +0100695 // Add in scope specific arguments.
696 droidstubsArgs = append(droidstubsArgs, apiScope.droidstubsArgs...)
Paul Duffin11512472019-02-11 15:55:17 +0000697 props.Arg_files = module.sdkLibraryProperties.Droiddoc_option_files
Paul Duffin6d0886e2020-04-07 18:49:53 +0100698 props.Args = proptools.StringPtr(strings.Join(droidstubsArgs, " "))
Jiyong Parkc678ad32018-04-10 13:07:10 +0900699
700 // List of APIs identified from the provided source files are created. They are later
701 // compared against to the not-yet-released (a.k.a current) list of APIs and to the
702 // last-released (a.k.a numbered) list of API.
Paul Duffind1b3a922020-01-22 11:57:20 +0000703 currentApiFileName := apiScope.apiFilePrefix + "current.txt"
704 removedApiFileName := apiScope.apiFilePrefix + "removed.txt"
Paul Duffin749f98f2019-12-30 17:23:46 +0000705 apiDir := module.getApiDir()
706 currentApiFileName = path.Join(apiDir, currentApiFileName)
707 removedApiFileName = path.Join(apiDir, removedApiFileName)
Jiyong Parkc678ad32018-04-10 13:07:10 +0900708
Jiyong Park58c518b2018-05-12 22:29:12 +0900709 // check against the not-yet-release API
710 props.Check_api.Current.Api_file = proptools.StringPtr(currentApiFileName)
711 props.Check_api.Current.Removed_api_file = proptools.StringPtr(removedApiFileName)
Jiyong Park58c518b2018-05-12 22:29:12 +0900712
Anton Hansson6478ac12020-05-02 11:19:36 +0100713 if !apiScope.unstable {
714 // check against the latest released API
715 props.Check_api.Last_released.Api_file = proptools.StringPtr(
716 module.latestApiFilegroupName(apiScope))
717 props.Check_api.Last_released.Removed_api_file = proptools.StringPtr(
718 module.latestRemovedApiFilegroupName(apiScope))
719 props.Check_api.Ignore_missing_latest_api = proptools.BoolPtr(true)
720 }
Jiyong Park58c518b2018-05-12 22:29:12 +0900721
Anton Hansson5fd5d242020-03-27 19:43:19 +0000722 // Dist the api txt artifact for sdk builds.
723 if !Bool(module.sdkLibraryProperties.No_dist) {
724 props.Dist.Targets = []string{"sdk", "win_sdk"}
725 props.Dist.Dest = proptools.StringPtr(fmt.Sprintf("%v.txt", module.BaseModuleName()))
726 props.Dist.Dir = proptools.StringPtr(path.Join(module.apiDistPath(apiScope), "api"))
727 }
728
Colin Cross84dfc3d2019-09-25 11:33:01 -0700729 mctx.CreateModule(DroidstubsFactory, &props)
Jiyong Parkc678ad32018-04-10 13:07:10 +0900730}
731
Jooyung Han5e9013b2020-03-10 06:23:13 +0900732func (module *SdkLibrary) DepIsInSameApex(mctx android.BaseModuleContext, dep android.Module) bool {
733 depTag := mctx.OtherModuleDependencyTag(dep)
734 if depTag == xmlPermissionsFileTag {
735 return true
736 }
737 return module.Library.DepIsInSameApex(mctx, dep)
738}
739
Jiyong Parkc678ad32018-04-10 13:07:10 +0900740// Creates the xml file that publicizes the runtime library
Paul Duffinf0229202020-04-29 16:47:28 +0100741func (module *SdkLibrary) createXmlFile(mctx android.DefaultableHookContext) {
Jiyong Parke3833882020-02-17 17:28:10 +0900742 props := struct {
Sundong Ahn0d7dff42019-12-04 12:53:44 +0900743 Name *string
Jiyong Parke3833882020-02-17 17:28:10 +0900744 Lib_name *string
Sundong Ahn0d7dff42019-12-04 12:53:44 +0900745 Soc_specific *bool
746 Device_specific *bool
747 Product_specific *bool
748 System_ext_specific *bool
Jooyung Han5e9013b2020-03-10 06:23:13 +0900749 Apex_available []string
Jiyong Parke3833882020-02-17 17:28:10 +0900750 }{
Jooyung Han5e9013b2020-03-10 06:23:13 +0900751 Name: proptools.StringPtr(module.xmlFileName()),
752 Lib_name: proptools.StringPtr(module.BaseModuleName()),
753 Apex_available: module.ApexProperties.Apex_available,
Jiyong Parkc678ad32018-04-10 13:07:10 +0900754 }
Jiyong Parke3833882020-02-17 17:28:10 +0900755
756 if module.SocSpecific() {
757 props.Soc_specific = proptools.BoolPtr(true)
758 } else if module.DeviceSpecific() {
759 props.Device_specific = proptools.BoolPtr(true)
760 } else if module.ProductSpecific() {
761 props.Product_specific = proptools.BoolPtr(true)
762 } else if module.SystemExtSpecific() {
763 props.System_ext_specific = proptools.BoolPtr(true)
764 }
765
766 mctx.CreateModule(sdkLibraryXmlFactory, &props)
Jiyong Parkc678ad32018-04-10 13:07:10 +0900767}
768
Paul Duffin50061512020-01-21 16:31:05 +0000769func PrebuiltJars(ctx android.BaseModuleContext, baseName string, s sdkSpec) android.Paths {
Jiyong Park6a927c42020-01-21 02:03:43 +0900770 var ver sdkVersion
771 var kind sdkKind
772 if s.usePrebuilt(ctx) {
773 ver = s.version
774 kind = s.kind
Jiyong Parkc678ad32018-04-10 13:07:10 +0900775 } else {
Jiyong Park6a927c42020-01-21 02:03:43 +0900776 // We don't have prebuilt SDK for the specific sdkVersion.
777 // Instead of breaking the build, fallback to use "system_current"
778 ver = sdkVersionCurrent
779 kind = sdkSystem
Sundong Ahn054b19a2018-10-19 13:46:09 +0900780 }
Jiyong Park6a927c42020-01-21 02:03:43 +0900781
782 dir := filepath.Join("prebuilts", "sdk", ver.String(), kind.String())
Paul Duffin50061512020-01-21 16:31:05 +0000783 jar := filepath.Join(dir, baseName+".jar")
Sundong Ahn054b19a2018-10-19 13:46:09 +0900784 jarPath := android.ExistentPathForSource(ctx, jar)
Sundong Ahnae418ac2019-02-28 15:01:28 +0900785 if !jarPath.Valid() {
Colin Cross07c88562020-01-07 09:34:44 -0800786 if ctx.Config().AllowMissingDependencies() {
787 return android.Paths{android.PathForSource(ctx, jar)}
788 } else {
Jiyong Park6a927c42020-01-21 02:03:43 +0900789 ctx.PropertyErrorf("sdk_library", "invalid sdk version %q, %q does not exist", s.raw, jar)
Colin Cross07c88562020-01-07 09:34:44 -0800790 }
Sundong Ahnae418ac2019-02-28 15:01:28 +0900791 return nil
792 }
Sundong Ahn054b19a2018-10-19 13:46:09 +0900793 return android.Paths{jarPath.Path()}
794}
795
Paul Duffind1b3a922020-01-22 11:57:20 +0000796func (module *SdkLibrary) sdkJars(
797 ctx android.BaseModuleContext,
798 sdkVersion sdkSpec,
799 headerJars bool) android.Paths {
800
Paul Duffin50061512020-01-21 16:31:05 +0000801 // If a specific numeric version has been requested then use prebuilt versions of the sdk.
802 if sdkVersion.version.isNumbered() {
803 return PrebuiltJars(ctx, module.BaseModuleName(), sdkVersion)
Sundong Ahn054b19a2018-10-19 13:46:09 +0900804 } else {
Paul Duffind1b3a922020-01-22 11:57:20 +0000805 if !sdkVersion.specified() {
806 if headerJars {
807 return module.Library.HeaderJars()
808 } else {
809 return module.Library.ImplementationJars()
810 }
811 }
Paul Duffin726d23c2020-01-22 16:30:37 +0000812 var apiScope *apiScope
Jiyong Park6a927c42020-01-21 02:03:43 +0900813 switch sdkVersion.kind {
814 case sdkSystem:
Paul Duffin726d23c2020-01-22 16:30:37 +0000815 apiScope = apiScopeSystem
816 case sdkTest:
817 apiScope = apiScopeTest
Jiyong Park6a927c42020-01-21 02:03:43 +0900818 case sdkPrivate:
Sundong Ahn054b19a2018-10-19 13:46:09 +0900819 return module.Library.HeaderJars()
Jiyong Park6a927c42020-01-21 02:03:43 +0900820 default:
Paul Duffin726d23c2020-01-22 16:30:37 +0000821 apiScope = apiScopePublic
Paul Duffind1b3a922020-01-22 11:57:20 +0000822 }
823
Paul Duffin726d23c2020-01-22 16:30:37 +0000824 paths := module.getScopePaths(apiScope)
Paul Duffind1b3a922020-01-22 11:57:20 +0000825 if headerJars {
826 return paths.stubsHeaderPath
827 } else {
828 return paths.stubsImplPath
Sundong Ahn054b19a2018-10-19 13:46:09 +0900829 }
Jiyong Parkc678ad32018-04-10 13:07:10 +0900830 }
831}
832
Sundong Ahn241cd372018-07-13 16:16:44 +0900833// to satisfy SdkLibraryDependency interface
Paul Duffind1b3a922020-01-22 11:57:20 +0000834func (module *SdkLibrary) SdkHeaderJars(ctx android.BaseModuleContext, sdkVersion sdkSpec) android.Paths {
835 return module.sdkJars(ctx, sdkVersion, true /*headerJars*/)
836}
837
838// to satisfy SdkLibraryDependency interface
Jiyong Park6a927c42020-01-21 02:03:43 +0900839func (module *SdkLibrary) SdkImplementationJars(ctx android.BaseModuleContext, sdkVersion sdkSpec) android.Paths {
Paul Duffind1b3a922020-01-22 11:57:20 +0000840 return module.sdkJars(ctx, sdkVersion, false /*headerJars*/)
Sundong Ahn241cd372018-07-13 16:16:44 +0900841}
842
Sundong Ahn80a87b32019-05-13 15:02:50 +0900843func (module *SdkLibrary) SetNoDist() {
844 module.sdkLibraryProperties.No_dist = proptools.BoolPtr(true)
845}
846
Colin Cross571cccf2019-02-04 11:22:08 -0800847var javaSdkLibrariesKey = android.NewOnceKey("javaSdkLibraries")
848
Jiyong Park82484c02018-04-23 21:41:26 +0900849func javaSdkLibraries(config android.Config) *[]string {
Colin Cross571cccf2019-02-04 11:22:08 -0800850 return config.Once(javaSdkLibrariesKey, func() interface{} {
Jiyong Park82484c02018-04-23 21:41:26 +0900851 return &[]string{}
852 }).(*[]string)
853}
854
Paul Duffin749f98f2019-12-30 17:23:46 +0000855func (module *SdkLibrary) getApiDir() string {
856 return proptools.StringDefault(module.sdkLibraryProperties.Api_dir, "api")
857}
858
Jiyong Parkc678ad32018-04-10 13:07:10 +0900859// For a java_sdk_library module, create internal modules for stubs, docs,
860// runtime libs and xml file. If requested, the stubs and docs are created twice
861// once for public API level and once for system API level
Paul Duffinf0229202020-04-29 16:47:28 +0100862func (module *SdkLibrary) CreateInternalModules(mctx android.DefaultableHookContext) {
863 // If the module has been disabled then don't create any child modules.
864 if !module.Enabled() {
865 return
866 }
867
Inseob Kim6e93ac92019-03-21 17:43:49 +0900868 if len(module.Library.Module.properties.Srcs) == 0 {
Inseob Kimc0907f12019-02-08 21:00:45 +0900869 mctx.PropertyErrorf("srcs", "java_sdk_library must specify srcs")
Jooyung Han58f26ab2019-12-18 15:34:32 +0900870 return
Inseob Kimc0907f12019-02-08 21:00:45 +0900871 }
872
Paul Duffin37e0b772019-12-30 17:20:10 +0000873 // If this builds against standard libraries (i.e. is not part of the core libraries)
874 // then assume it provides both system and test apis. Otherwise, assume it does not and
875 // also assume it does not contribute to the dist build.
876 sdkDep := decodeSdkDep(mctx, sdkContext(&module.Library))
877 hasSystemAndTestApis := sdkDep.hasStandardLibs()
Paul Duffin3375e352020-04-28 10:44:03 +0100878 module.sdkLibraryProperties.Generate_system_and_test_apis = hasSystemAndTestApis
Paul Duffin37e0b772019-12-30 17:20:10 +0000879 module.sdkLibraryProperties.No_dist = proptools.BoolPtr(!hasSystemAndTestApis)
880
Inseob Kim8098faa2019-03-18 10:19:51 +0900881 missing_current_api := false
882
Paul Duffin3375e352020-04-28 10:44:03 +0100883 generatedScopes := module.getGeneratedApiScopes(mctx)
Paul Duffind1b3a922020-01-22 11:57:20 +0000884
Paul Duffin749f98f2019-12-30 17:23:46 +0000885 apiDir := module.getApiDir()
Paul Duffin3375e352020-04-28 10:44:03 +0100886 for _, scope := range generatedScopes {
Inseob Kim8098faa2019-03-18 10:19:51 +0900887 for _, api := range []string{"current.txt", "removed.txt"} {
Paul Duffind1b3a922020-01-22 11:57:20 +0000888 path := path.Join(mctx.ModuleDir(), apiDir, scope.apiFilePrefix+api)
Inseob Kim8098faa2019-03-18 10:19:51 +0900889 p := android.ExistentPathForSource(mctx, path)
890 if !p.Valid() {
891 mctx.ModuleErrorf("Current api file %#v doesn't exist", path)
892 missing_current_api = true
893 }
894 }
895 }
896
897 if missing_current_api {
898 script := "build/soong/scripts/gen-java-current-api-files.sh"
899 p := android.ExistentPathForSource(mctx, script)
900
901 if !p.Valid() {
902 panic(fmt.Sprintf("script file %s doesn't exist", script))
903 }
904
905 mctx.ModuleErrorf("One or more current api files are missing. "+
906 "You can update them by:\n"+
Paul Duffin37e0b772019-12-30 17:20:10 +0000907 "%s %q %s && m update-api",
Paul Duffind1b3a922020-01-22 11:57:20 +0000908 script, filepath.Join(mctx.ModuleDir(), apiDir),
Paul Duffin3375e352020-04-28 10:44:03 +0100909 strings.Join(generatedScopes.Strings(func(s *apiScope) string { return s.apiFilePrefix }), " "))
Inseob Kim8098faa2019-03-18 10:19:51 +0900910 return
911 }
912
Paul Duffin3375e352020-04-28 10:44:03 +0100913 for _, scope := range generatedScopes {
Paul Duffind1b3a922020-01-22 11:57:20 +0000914 module.createStubsLibrary(mctx, scope)
915 module.createStubsSources(mctx, scope)
Inseob Kimc0907f12019-02-08 21:00:45 +0900916 }
917
Paul Duffin43db9be2019-12-30 17:35:49 +0000918 if !proptools.Bool(module.sdkLibraryProperties.Api_only) {
919 // for runtime
920 module.createXmlFile(mctx)
921
922 // record java_sdk_library modules so that they are exported to make
923 javaSdkLibraries := javaSdkLibraries(mctx.Config())
924 javaSdkLibrariesLock.Lock()
925 defer javaSdkLibrariesLock.Unlock()
926 *javaSdkLibraries = append(*javaSdkLibraries, module.BaseModuleName())
927 }
Inseob Kimc0907f12019-02-08 21:00:45 +0900928}
929
930func (module *SdkLibrary) InitSdkLibraryProperties() {
Sundong Ahn054b19a2018-10-19 13:46:09 +0900931 module.AddProperties(
932 &module.sdkLibraryProperties,
933 &module.Library.Module.properties,
934 &module.Library.Module.dexpreoptProperties,
935 &module.Library.Module.deviceProperties,
936 &module.Library.Module.protoProperties,
937 )
938
939 module.Library.Module.properties.Installable = proptools.BoolPtr(true)
940 module.Library.Module.deviceProperties.IsSDKLibrary = true
Inseob Kimc0907f12019-02-08 21:00:45 +0900941}
Sundong Ahn054b19a2018-10-19 13:46:09 +0900942
Jaewoong Jung4f158ee2019-07-11 10:05:35 -0700943// java_sdk_library is a special Java library that provides optional platform APIs to apps.
944// In practice, it can be viewed as a combination of several modules: 1) stubs library that clients
945// are linked against to, 2) droiddoc module that internally generates API stubs source files,
946// 3) the real runtime shared library that implements the APIs, and 4) XML file for adding
947// the runtime lib to the classpath at runtime if requested via <uses-library>.
Inseob Kimc0907f12019-02-08 21:00:45 +0900948func SdkLibraryFactory() android.Module {
949 module := &SdkLibrary{}
950 module.InitSdkLibraryProperties()
Jooyung Han58f26ab2019-12-18 15:34:32 +0900951 android.InitApexModule(module)
Sundong Ahn054b19a2018-10-19 13:46:09 +0900952 InitJavaModule(module, android.HostAndDeviceSupported)
Paul Duffin3375e352020-04-28 10:44:03 +0100953
954 // Initialize the map from scope to scope specific properties.
955 scopeToProperties := make(map[*apiScope]*ApiScopeProperties)
956 for _, scope := range allApiScopes {
957 scopeToProperties[scope] = scope.scopeSpecificProperties(module)
958 }
959 module.scopeToProperties = scopeToProperties
960
Paul Duffinf0229202020-04-29 16:47:28 +0100961 module.SetDefaultableHook(func(ctx android.DefaultableHookContext) { module.CreateInternalModules(ctx) })
Jiyong Parkc678ad32018-04-10 13:07:10 +0900962 return module
963}
Colin Cross79c7c262019-04-17 11:11:46 -0700964
965//
966// SDK library prebuilts
967//
968
Paul Duffin56d44902020-01-31 13:36:25 +0000969// Properties associated with each api scope.
970type sdkLibraryScopeProperties struct {
Colin Cross79c7c262019-04-17 11:11:46 -0700971 Jars []string `android:"path"`
972
973 Sdk_version *string
974
Colin Cross79c7c262019-04-17 11:11:46 -0700975 // List of shared java libs that this module has dependencies to
976 Libs []string
Paul Duffin3d1248c2020-04-09 00:10:17 +0100977
978 // The stub sources.
979 Stub_srcs []string `android:"path"`
Paul Duffin1fd005d2020-04-09 01:08:11 +0100980
981 // The current.txt
982 Current_api string `android:"path"`
983
984 // The removed.txt
985 Removed_api string `android:"path"`
Colin Cross79c7c262019-04-17 11:11:46 -0700986}
987
Paul Duffin56d44902020-01-31 13:36:25 +0000988type sdkLibraryImportProperties struct {
Paul Duffinfcfd7912020-01-31 17:54:30 +0000989 // List of shared java libs, common to all scopes, that this module has
990 // dependencies to
991 Libs []string
Paul Duffin56d44902020-01-31 13:36:25 +0000992}
993
Colin Cross79c7c262019-04-17 11:11:46 -0700994type sdkLibraryImport struct {
995 android.ModuleBase
996 android.DefaultableModuleBase
997 prebuilt android.Prebuilt
Paul Duffindd46f712020-02-10 13:37:10 +0000998 android.ApexModuleBase
999 android.SdkBase
Colin Cross79c7c262019-04-17 11:11:46 -07001000
1001 properties sdkLibraryImportProperties
1002
Paul Duffin46a26a82020-04-07 19:27:04 +01001003 // Map from api scope to the scope specific property structure.
1004 scopeProperties map[*apiScope]*sdkLibraryScopeProperties
1005
Paul Duffin56d44902020-01-31 13:36:25 +00001006 commonToSdkLibraryAndImport
Colin Cross79c7c262019-04-17 11:11:46 -07001007}
1008
1009var _ SdkLibraryDependency = (*sdkLibraryImport)(nil)
1010
Paul Duffin46a26a82020-04-07 19:27:04 +01001011// The type of a structure that contains a field of type sdkLibraryScopeProperties
1012// for each apiscope in allApiScopes, e.g. something like:
1013// struct {
1014// Public sdkLibraryScopeProperties
1015// System sdkLibraryScopeProperties
1016// ...
1017// }
1018var allScopeStructType = createAllScopePropertiesStructType()
1019
1020// Dynamically create a structure type for each apiscope in allApiScopes.
1021func createAllScopePropertiesStructType() reflect.Type {
1022 var fields []reflect.StructField
1023 for _, apiScope := range allApiScopes {
1024 field := reflect.StructField{
1025 Name: apiScope.fieldName,
1026 Type: reflect.TypeOf(sdkLibraryScopeProperties{}),
1027 }
1028 fields = append(fields, field)
1029 }
1030
1031 return reflect.StructOf(fields)
1032}
1033
1034// Create an instance of the scope specific structure type and return a map
1035// from apiscope to a pointer to each scope specific field.
1036func createPropertiesInstance() (interface{}, map[*apiScope]*sdkLibraryScopeProperties) {
1037 allScopePropertiesPtr := reflect.New(allScopeStructType)
1038 allScopePropertiesStruct := allScopePropertiesPtr.Elem()
1039 scopeProperties := make(map[*apiScope]*sdkLibraryScopeProperties)
1040
1041 for _, apiScope := range allApiScopes {
1042 field := allScopePropertiesStruct.FieldByName(apiScope.fieldName)
1043 scopeProperties[apiScope] = field.Addr().Interface().(*sdkLibraryScopeProperties)
1044 }
1045
1046 return allScopePropertiesPtr.Interface(), scopeProperties
1047}
1048
Jaewoong Jung4f158ee2019-07-11 10:05:35 -07001049// java_sdk_library_import imports a prebuilt java_sdk_library.
Colin Cross79c7c262019-04-17 11:11:46 -07001050func sdkLibraryImportFactory() android.Module {
1051 module := &sdkLibraryImport{}
1052
Paul Duffin46a26a82020-04-07 19:27:04 +01001053 allScopeProperties, scopeToProperties := createPropertiesInstance()
1054 module.scopeProperties = scopeToProperties
1055 module.AddProperties(&module.properties, allScopeProperties)
Colin Cross79c7c262019-04-17 11:11:46 -07001056
Paul Duffin0bdcb272020-02-06 15:24:57 +00001057 android.InitPrebuiltModule(module, &[]string{""})
Paul Duffindd46f712020-02-10 13:37:10 +00001058 android.InitApexModule(module)
1059 android.InitSdkAwareModule(module)
Colin Cross79c7c262019-04-17 11:11:46 -07001060 InitJavaModule(module, android.HostAndDeviceSupported)
1061
1062 android.AddLoadHook(module, func(mctx android.LoadHookContext) { module.createInternalModules(mctx) })
1063 return module
1064}
1065
1066func (module *sdkLibraryImport) Prebuilt() *android.Prebuilt {
1067 return &module.prebuilt
1068}
1069
1070func (module *sdkLibraryImport) Name() string {
1071 return module.prebuilt.Name(module.ModuleBase.Name())
1072}
1073
1074func (module *sdkLibraryImport) createInternalModules(mctx android.LoadHookContext) {
Colin Cross79c7c262019-04-17 11:11:46 -07001075
Paul Duffin50061512020-01-21 16:31:05 +00001076 // If the build is configured to use prebuilts then force this to be preferred.
1077 if mctx.Config().UnbundledBuildUsePrebuiltSdks() {
1078 module.prebuilt.ForcePrefer()
1079 }
1080
Paul Duffin46a26a82020-04-07 19:27:04 +01001081 for apiScope, scopeProperties := range module.scopeProperties {
Paul Duffin56d44902020-01-31 13:36:25 +00001082 if len(scopeProperties.Jars) == 0 {
1083 continue
1084 }
1085
Paul Duffinbbb546b2020-04-09 00:07:11 +01001086 module.createJavaImportForStubs(mctx, apiScope, scopeProperties)
Paul Duffin3d1248c2020-04-09 00:10:17 +01001087
1088 module.createPrebuiltStubsSources(mctx, apiScope, scopeProperties)
Paul Duffin56d44902020-01-31 13:36:25 +00001089 }
Colin Cross79c7c262019-04-17 11:11:46 -07001090
1091 javaSdkLibraries := javaSdkLibraries(mctx.Config())
1092 javaSdkLibrariesLock.Lock()
1093 defer javaSdkLibrariesLock.Unlock()
1094 *javaSdkLibraries = append(*javaSdkLibraries, module.BaseModuleName())
1095}
1096
Paul Duffinbbb546b2020-04-09 00:07:11 +01001097func (module *sdkLibraryImport) createJavaImportForStubs(mctx android.LoadHookContext, apiScope *apiScope, scopeProperties *sdkLibraryScopeProperties) {
1098 // Creates a java import for the jar with ".stubs" suffix
1099 props := struct {
1100 Name *string
1101 Soc_specific *bool
1102 Device_specific *bool
1103 Product_specific *bool
1104 System_ext_specific *bool
1105 Sdk_version *string
1106 Libs []string
1107 Jars []string
1108 Prefer *bool
1109 }{}
1110 props.Name = proptools.StringPtr(apiScope.stubsModuleName(module.BaseModuleName()))
1111 props.Sdk_version = scopeProperties.Sdk_version
1112 // Prepend any of the libs from the legacy public properties to the libs for each of the
1113 // scopes to avoid having to duplicate them in each scope.
1114 props.Libs = append(module.properties.Libs, scopeProperties.Libs...)
1115 props.Jars = scopeProperties.Jars
1116 if module.SocSpecific() {
1117 props.Soc_specific = proptools.BoolPtr(true)
1118 } else if module.DeviceSpecific() {
1119 props.Device_specific = proptools.BoolPtr(true)
1120 } else if module.ProductSpecific() {
1121 props.Product_specific = proptools.BoolPtr(true)
1122 } else if module.SystemExtSpecific() {
1123 props.System_ext_specific = proptools.BoolPtr(true)
1124 }
1125 // If the build should use prebuilt sdks then set prefer to true on the stubs library.
1126 // That will cause the prebuilt version of the stubs to override the source version.
1127 if mctx.Config().UnbundledBuildUsePrebuiltSdks() {
1128 props.Prefer = proptools.BoolPtr(true)
1129 }
1130 mctx.CreateModule(ImportFactory, &props)
1131}
1132
Paul Duffin3d1248c2020-04-09 00:10:17 +01001133func (module *sdkLibraryImport) createPrebuiltStubsSources(mctx android.LoadHookContext, apiScope *apiScope, scopeProperties *sdkLibraryScopeProperties) {
1134 props := struct {
1135 Name *string
1136 Srcs []string
1137 }{}
1138 props.Name = proptools.StringPtr(apiScope.docsModuleName(module.BaseModuleName()))
1139 props.Srcs = scopeProperties.Stub_srcs
1140 mctx.CreateModule(PrebuiltStubsSourcesFactory, &props)
1141}
1142
Colin Cross79c7c262019-04-17 11:11:46 -07001143func (module *sdkLibraryImport) DepsMutator(ctx android.BottomUpMutatorContext) {
Paul Duffin46a26a82020-04-07 19:27:04 +01001144 for apiScope, scopeProperties := range module.scopeProperties {
Paul Duffin56d44902020-01-31 13:36:25 +00001145 if len(scopeProperties.Jars) == 0 {
1146 continue
1147 }
1148
1149 // Add dependencies to the prebuilt stubs library
1150 ctx.AddVariationDependencies(nil, apiScope.stubsTag, apiScope.stubsModuleName(module.BaseModuleName()))
1151 }
Colin Cross79c7c262019-04-17 11:11:46 -07001152}
1153
1154func (module *sdkLibraryImport) GenerateAndroidBuildActions(ctx android.ModuleContext) {
1155 // Record the paths to the prebuilt stubs library.
1156 ctx.VisitDirectDeps(func(to android.Module) {
1157 tag := ctx.OtherModuleDependencyTag(to)
1158
Paul Duffin56d44902020-01-31 13:36:25 +00001159 if lib, ok := to.(Dependency); ok {
1160 if scopeTag, ok := tag.(scopeDependencyTag); ok {
1161 apiScope := scopeTag.apiScope
1162 scopePaths := module.getScopePaths(apiScope)
1163 scopePaths.stubsHeaderPath = lib.HeaderJars()
1164 }
Colin Cross79c7c262019-04-17 11:11:46 -07001165 }
1166 })
1167}
1168
Paul Duffin56d44902020-01-31 13:36:25 +00001169func (module *sdkLibraryImport) sdkJars(
1170 ctx android.BaseModuleContext,
1171 sdkVersion sdkSpec) android.Paths {
1172
Paul Duffin50061512020-01-21 16:31:05 +00001173 // If a specific numeric version has been requested then use prebuilt versions of the sdk.
1174 if sdkVersion.version.isNumbered() {
1175 return PrebuiltJars(ctx, module.BaseModuleName(), sdkVersion)
1176 }
1177
Paul Duffin56d44902020-01-31 13:36:25 +00001178 var apiScope *apiScope
1179 switch sdkVersion.kind {
1180 case sdkSystem:
1181 apiScope = apiScopeSystem
1182 case sdkTest:
1183 apiScope = apiScopeTest
1184 default:
1185 apiScope = apiScopePublic
1186 }
1187
1188 paths := module.getScopePaths(apiScope)
1189 return paths.stubsHeaderPath
1190}
1191
Colin Cross79c7c262019-04-17 11:11:46 -07001192// to satisfy SdkLibraryDependency interface
Jiyong Park6a927c42020-01-21 02:03:43 +09001193func (module *sdkLibraryImport) SdkHeaderJars(ctx android.BaseModuleContext, sdkVersion sdkSpec) android.Paths {
Colin Cross79c7c262019-04-17 11:11:46 -07001194 // This module is just a wrapper for the prebuilt stubs.
Paul Duffin56d44902020-01-31 13:36:25 +00001195 return module.sdkJars(ctx, sdkVersion)
Colin Cross79c7c262019-04-17 11:11:46 -07001196}
1197
1198// to satisfy SdkLibraryDependency interface
Jiyong Park6a927c42020-01-21 02:03:43 +09001199func (module *sdkLibraryImport) SdkImplementationJars(ctx android.BaseModuleContext, sdkVersion sdkSpec) android.Paths {
Colin Cross79c7c262019-04-17 11:11:46 -07001200 // This module is just a wrapper for the stubs.
Paul Duffin56d44902020-01-31 13:36:25 +00001201 return module.sdkJars(ctx, sdkVersion)
Colin Cross79c7c262019-04-17 11:11:46 -07001202}
Jiyong Parke3833882020-02-17 17:28:10 +09001203
1204//
1205// java_sdk_library_xml
1206//
1207type sdkLibraryXml struct {
1208 android.ModuleBase
1209 android.DefaultableModuleBase
1210 android.ApexModuleBase
1211
1212 properties sdkLibraryXmlProperties
1213
1214 outputFilePath android.OutputPath
1215 installDirPath android.InstallPath
1216}
1217
1218type sdkLibraryXmlProperties struct {
1219 // canonical name of the lib
1220 Lib_name *string
1221}
1222
1223// java_sdk_library_xml builds the permission xml file for a java_sdk_library.
1224// Not to be used directly by users. java_sdk_library internally uses this.
1225func sdkLibraryXmlFactory() android.Module {
1226 module := &sdkLibraryXml{}
1227
1228 module.AddProperties(&module.properties)
1229
1230 android.InitApexModule(module)
1231 android.InitAndroidArchModule(module, android.DeviceSupported, android.MultilibCommon)
1232
1233 return module
1234}
1235
1236// from android.PrebuiltEtcModule
1237func (module *sdkLibraryXml) SubDir() string {
1238 return "permissions"
1239}
1240
1241// from android.PrebuiltEtcModule
1242func (module *sdkLibraryXml) OutputFile() android.OutputPath {
1243 return module.outputFilePath
1244}
1245
1246// from android.ApexModule
1247func (module *sdkLibraryXml) AvailableFor(what string) bool {
1248 return true
1249}
1250
1251func (module *sdkLibraryXml) DepsMutator(ctx android.BottomUpMutatorContext) {
1252 // do nothing
1253}
1254
1255// File path to the runtime implementation library
1256func (module *sdkLibraryXml) implPath() string {
1257 implName := proptools.String(module.properties.Lib_name)
1258 if apexName := module.ApexName(); apexName != "" {
1259 // TODO(b/146468504): ApexName() is only a soong module name, not apex name.
1260 // In most cases, this works fine. But when apex_name is set or override_apex is used
1261 // this can be wrong.
1262 return fmt.Sprintf("/apex/%s/javalib/%s.jar", apexName, implName)
1263 }
1264 partition := "system"
1265 if module.SocSpecific() {
1266 partition = "vendor"
1267 } else if module.DeviceSpecific() {
1268 partition = "odm"
1269 } else if module.ProductSpecific() {
1270 partition = "product"
1271 } else if module.SystemExtSpecific() {
1272 partition = "system_ext"
1273 }
1274 return "/" + partition + "/framework/" + implName + ".jar"
1275}
1276
1277func (module *sdkLibraryXml) GenerateAndroidBuildActions(ctx android.ModuleContext) {
1278 libName := proptools.String(module.properties.Lib_name)
1279 xmlContent := fmt.Sprintf(permissionsTemplate, libName, module.implPath())
1280
1281 module.outputFilePath = android.PathForModuleOut(ctx, libName+".xml").OutputPath
1282 rule := android.NewRuleBuilder()
1283 rule.Command().
1284 Text("/bin/bash -c \"echo -e '" + xmlContent + "'\" > ").
1285 Output(module.outputFilePath)
1286
1287 rule.Build(pctx, ctx, "java_sdk_xml", "Permission XML")
1288
1289 module.installDirPath = android.PathForModuleInstall(ctx, "etc", module.SubDir())
1290}
1291
1292func (module *sdkLibraryXml) AndroidMkEntries() []android.AndroidMkEntries {
1293 if !module.IsForPlatform() {
1294 return []android.AndroidMkEntries{android.AndroidMkEntries{
1295 Disabled: true,
1296 }}
1297 }
1298
1299 return []android.AndroidMkEntries{android.AndroidMkEntries{
1300 Class: "ETC",
1301 OutputFile: android.OptionalPathForPath(module.outputFilePath),
1302 ExtraEntries: []android.AndroidMkExtraEntriesFunc{
1303 func(entries *android.AndroidMkEntries) {
1304 entries.SetString("LOCAL_MODULE_TAGS", "optional")
1305 entries.SetString("LOCAL_MODULE_PATH", module.installDirPath.ToMakePath().String())
1306 entries.SetString("LOCAL_INSTALLED_MODULE_STEM", module.outputFilePath.Base())
1307 },
1308 },
1309 }}
1310}
Paul Duffindd46f712020-02-10 13:37:10 +00001311
1312type sdkLibrarySdkMemberType struct {
1313 android.SdkMemberTypeBase
1314}
1315
1316func (s *sdkLibrarySdkMemberType) AddDependencies(mctx android.BottomUpMutatorContext, dependencyTag blueprint.DependencyTag, names []string) {
1317 mctx.AddVariationDependencies(nil, dependencyTag, names...)
1318}
1319
1320func (s *sdkLibrarySdkMemberType) IsInstance(module android.Module) bool {
1321 _, ok := module.(*SdkLibrary)
1322 return ok
1323}
1324
1325func (s *sdkLibrarySdkMemberType) AddPrebuiltModule(ctx android.SdkMemberContext, member android.SdkMember) android.BpModule {
1326 return ctx.SnapshotBuilder().AddPrebuiltModule(member, "java_sdk_library_import")
1327}
1328
1329func (s *sdkLibrarySdkMemberType) CreateVariantPropertiesStruct() android.SdkMemberProperties {
1330 return &sdkLibrarySdkMemberProperties{}
1331}
1332
1333type sdkLibrarySdkMemberProperties struct {
1334 android.SdkMemberPropertiesBase
1335
1336 // Scope to per scope properties.
1337 Scopes map[*apiScope]scopeProperties
1338
1339 // Additional libraries that the exported stubs libraries depend upon.
1340 Libs []string
Paul Duffin3d1248c2020-04-09 00:10:17 +01001341
1342 // The Java stubs source files.
1343 Stub_srcs []string
Paul Duffindd46f712020-02-10 13:37:10 +00001344}
1345
1346type scopeProperties struct {
Paul Duffin1fd005d2020-04-09 01:08:11 +01001347 Jars android.Paths
1348 StubsSrcJar android.Path
1349 CurrentApiFile android.Path
1350 RemovedApiFile android.Path
1351 SdkVersion string
Paul Duffindd46f712020-02-10 13:37:10 +00001352}
1353
1354func (s *sdkLibrarySdkMemberProperties) PopulateFromVariant(ctx android.SdkMemberContext, variant android.Module) {
1355 sdk := variant.(*SdkLibrary)
1356
1357 s.Scopes = make(map[*apiScope]scopeProperties)
1358 for _, apiScope := range allApiScopes {
1359 paths := sdk.getScopePaths(apiScope)
1360 jars := paths.stubsImplPath
1361 if len(jars) > 0 {
1362 properties := scopeProperties{}
1363 properties.Jars = jars
1364 properties.SdkVersion = apiScope.sdkVersion
Paul Duffin3d1248c2020-04-09 00:10:17 +01001365 properties.StubsSrcJar = paths.stubsSrcJar
Paul Duffin1fd005d2020-04-09 01:08:11 +01001366 properties.CurrentApiFile = paths.currentApiFilePath
1367 properties.RemovedApiFile = paths.removedApiFilePath
Paul Duffindd46f712020-02-10 13:37:10 +00001368 s.Scopes[apiScope] = properties
1369 }
1370 }
1371
1372 s.Libs = sdk.properties.Libs
1373}
1374
1375func (s *sdkLibrarySdkMemberProperties) AddToPropertySet(ctx android.SdkMemberContext, propertySet android.BpPropertySet) {
1376 for _, apiScope := range allApiScopes {
1377 if properties, ok := s.Scopes[apiScope]; ok {
1378 scopeSet := propertySet.AddPropertySet(apiScope.name)
1379
Paul Duffin3d1248c2020-04-09 00:10:17 +01001380 scopeDir := filepath.Join("sdk_library", s.OsPrefix(), apiScope.name)
1381
Paul Duffindd46f712020-02-10 13:37:10 +00001382 var jars []string
1383 for _, p := range properties.Jars {
Paul Duffin3d1248c2020-04-09 00:10:17 +01001384 dest := filepath.Join(scopeDir, ctx.Name()+"-stubs.jar")
Paul Duffindd46f712020-02-10 13:37:10 +00001385 ctx.SnapshotBuilder().CopyToSnapshot(p, dest)
1386 jars = append(jars, dest)
1387 }
1388 scopeSet.AddProperty("jars", jars)
1389
Paul Duffin3d1248c2020-04-09 00:10:17 +01001390 // Merge the stubs source jar into the snapshot zip so that when it is unpacked
1391 // the source files are also unpacked.
1392 snapshotRelativeDir := filepath.Join(scopeDir, ctx.Name()+"_stub_sources")
1393 ctx.SnapshotBuilder().UnzipToSnapshot(properties.StubsSrcJar, snapshotRelativeDir)
1394 scopeSet.AddProperty("stub_srcs", []string{snapshotRelativeDir})
1395
Paul Duffin1fd005d2020-04-09 01:08:11 +01001396 if properties.CurrentApiFile != nil {
1397 currentApiSnapshotPath := filepath.Join(scopeDir, ctx.Name()+".txt")
1398 ctx.SnapshotBuilder().CopyToSnapshot(properties.CurrentApiFile, currentApiSnapshotPath)
1399 scopeSet.AddProperty("current_api", currentApiSnapshotPath)
1400 }
1401
1402 if properties.RemovedApiFile != nil {
1403 removedApiSnapshotPath := filepath.Join(scopeDir, ctx.Name()+"-removed.txt")
1404 ctx.SnapshotBuilder().CopyToSnapshot(properties.CurrentApiFile, removedApiSnapshotPath)
1405 scopeSet.AddProperty("removed_api", removedApiSnapshotPath)
1406 }
1407
Paul Duffindd46f712020-02-10 13:37:10 +00001408 if properties.SdkVersion != "" {
1409 scopeSet.AddProperty("sdk_version", properties.SdkVersion)
1410 }
1411 }
1412 }
1413
1414 if len(s.Libs) > 0 {
1415 propertySet.AddPropertyWithTag("libs", s.Libs, ctx.SnapshotBuilder().SdkMemberReferencePropertyTag(false))
1416 }
1417}