blob: c44c352dd643dadb6c90dc07d864d0de5ef37ce4 [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 {
Paul Duffin4911a892020-04-29 23:35:13 +0100260 // Visibility for stubs library modules. If not specified then defaults to the
261 // visibility property.
262 Stubs_library_visibility []string
263
264 // Visibility for stubs source modules. If not specified then defaults to the
265 // visibility property.
266 Stubs_source_visibility []string
267
Sundong Ahnf043cf62018-06-25 16:04:37 +0900268 // List of Java libraries that will be in the classpath when building stubs
269 Stub_only_libs []string `android:"arch_variant"`
270
Paul Duffin7a586d32019-12-30 17:09:34 +0000271 // list of package names that will be documented and publicized as API.
272 // This allows the API to be restricted to a subset of the source files provided.
273 // If this is unspecified then all the source files will be treated as being part
274 // of the API.
Jiyong Parkc678ad32018-04-10 13:07:10 +0900275 Api_packages []string
276
Jiyong Park5a2c9d72018-05-01 22:25:41 +0900277 // list of package names that must be hidden from the API
278 Hidden_api_packages []string
279
Paul Duffin749f98f2019-12-30 17:23:46 +0000280 // the relative path to the directory containing the api specification files.
281 // Defaults to "api".
282 Api_dir *string
283
Paul Duffin43db9be2019-12-30 17:35:49 +0000284 // If set to true there is no runtime library.
285 Api_only *bool
286
Paul Duffin11512472019-02-11 15:55:17 +0000287 // local files that are used within user customized droiddoc options.
288 Droiddoc_option_files []string
289
290 // additional droiddoc options
291 // Available variables for substitution:
292 //
293 // $(location <label>): the path to the droiddoc_option_files with name <label>
Sundong Ahndd567f92018-07-31 17:19:11 +0900294 Droiddoc_options []string
295
Sundong Ahn054b19a2018-10-19 13:46:09 +0900296 // a list of top-level directories containing files to merge qualifier annotations
297 // (i.e. those intended to be included in the stubs written) from.
298 Merge_annotations_dirs []string
299
300 // a list of top-level directories containing Java stub files to merge show/hide annotations from.
301 Merge_inclusion_annotations_dirs []string
302
303 // If set to true, the path of dist files is apistubs/core. Defaults to false.
304 Core_lib *bool
305
Sundong Ahn80a87b32019-05-13 15:02:50 +0900306 // don't create dist rules.
307 No_dist *bool `blueprint:"mutated"`
308
Paul Duffin3375e352020-04-28 10:44:03 +0100309 // indicates whether system and test apis should be generated.
310 Generate_system_and_test_apis bool `blueprint:"mutated"`
311
312 // The properties specific to the public api scope
313 //
314 // Unless explicitly specified by using public.enabled the public api scope is
315 // enabled by default in both legacy and non-legacy mode.
316 Public ApiScopeProperties
317
318 // The properties specific to the system api scope
319 //
320 // In legacy mode the system api scope is enabled by default when sdk_version
321 // is set to something other than "none".
322 //
323 // In non-legacy mode the system api scope is disabled by default.
324 System ApiScopeProperties
325
326 // The properties specific to the test api scope
327 //
328 // In legacy mode the test api scope is enabled by default when sdk_version
329 // is set to something other than "none".
330 //
331 // In non-legacy mode the test api scope is disabled by default.
332 Test ApiScopeProperties
Paul Duffin37e0b772019-12-30 17:20:10 +0000333
Paul Duffin8f265b92020-04-28 14:13:56 +0100334 // The properties specific to the module_lib api scope
335 //
336 // Unless explicitly specified by using test.enabled the module_lib api scope is
337 // disabled by default.
338 Module_lib ApiScopeProperties
339
Paul Duffin160fe412020-05-10 19:32:20 +0100340 // Properties related to api linting.
341 Api_lint struct {
342 // Enable api linting.
343 Enabled *bool
344 }
345
Jiyong Parkc678ad32018-04-10 13:07:10 +0900346 // TODO: determines whether to create HTML doc or not
347 //Html_doc *bool
348}
349
Paul Duffind1b3a922020-01-22 11:57:20 +0000350type scopePaths struct {
Paul Duffin1fd005d2020-04-09 01:08:11 +0100351 stubsHeaderPath android.Paths
352 stubsImplPath android.Paths
353 currentApiFilePath android.Path
354 removedApiFilePath android.Path
355 stubsSrcJar android.Path
Paul Duffind1b3a922020-01-22 11:57:20 +0000356}
357
Paul Duffin56d44902020-01-31 13:36:25 +0000358// Common code between sdk library and sdk library import
359type commonToSdkLibraryAndImport struct {
360 scopePaths map[*apiScope]*scopePaths
361}
362
363func (c *commonToSdkLibraryAndImport) getScopePaths(scope *apiScope) *scopePaths {
364 if c.scopePaths == nil {
365 c.scopePaths = make(map[*apiScope]*scopePaths)
366 }
367 paths := c.scopePaths[scope]
368 if paths == nil {
369 paths = &scopePaths{}
370 c.scopePaths[scope] = paths
371 }
372
373 return paths
374}
375
Inseob Kimc0907f12019-02-08 21:00:45 +0900376type SdkLibrary struct {
Sundong Ahn054b19a2018-10-19 13:46:09 +0900377 Library
Jiyong Parkc678ad32018-04-10 13:07:10 +0900378
Sundong Ahn054b19a2018-10-19 13:46:09 +0900379 sdkLibraryProperties sdkLibraryProperties
Jiyong Parkc678ad32018-04-10 13:07:10 +0900380
Paul Duffin3375e352020-04-28 10:44:03 +0100381 // Map from api scope to the scope specific property structure.
382 scopeToProperties map[*apiScope]*ApiScopeProperties
383
Paul Duffin56d44902020-01-31 13:36:25 +0000384 commonToSdkLibraryAndImport
Jiyong Parkc678ad32018-04-10 13:07:10 +0900385}
386
Inseob Kimc0907f12019-02-08 21:00:45 +0900387var _ Dependency = (*SdkLibrary)(nil)
388var _ SdkLibraryDependency = (*SdkLibrary)(nil)
Colin Cross897d2ed2019-02-11 14:03:51 -0800389
Paul Duffin3375e352020-04-28 10:44:03 +0100390func (module *SdkLibrary) generateTestAndSystemScopesByDefault() bool {
391 return module.sdkLibraryProperties.Generate_system_and_test_apis
392}
393
394func (module *SdkLibrary) getGeneratedApiScopes(ctx android.EarlyModuleContext) apiScopes {
395 // Check to see if any scopes have been explicitly enabled. If any have then all
396 // must be.
397 anyScopesExplicitlyEnabled := false
398 for _, scope := range allApiScopes {
399 scopeProperties := module.scopeToProperties[scope]
400 if scopeProperties.Enabled != nil {
401 anyScopesExplicitlyEnabled = true
402 break
403 }
Paul Duffind1b3a922020-01-22 11:57:20 +0000404 }
Paul Duffin3375e352020-04-28 10:44:03 +0100405
406 var generatedScopes apiScopes
407 enabledScopes := make(map[*apiScope]struct{})
408 for _, scope := range allApiScopes {
409 scopeProperties := module.scopeToProperties[scope]
410 // If any scopes are explicitly enabled then ignore the legacy enabled status.
411 // This is to ensure that any new usages of this module type do not rely on legacy
412 // behaviour.
413 defaultEnabledStatus := false
414 if anyScopesExplicitlyEnabled {
415 defaultEnabledStatus = scope.defaultEnabledStatus
416 } else {
417 defaultEnabledStatus = scope.legacyEnabledStatus(module)
418 }
419 enabled := proptools.BoolDefault(scopeProperties.Enabled, defaultEnabledStatus)
420 if enabled {
421 enabledScopes[scope] = struct{}{}
422 generatedScopes = append(generatedScopes, scope)
423 }
424 }
425
426 // Now check to make sure that any scope that is extended by an enabled scope is also
427 // enabled.
428 for _, scope := range allApiScopes {
429 if _, ok := enabledScopes[scope]; ok {
430 extends := scope.extends
431 if extends != nil {
432 if _, ok := enabledScopes[extends]; !ok {
433 ctx.ModuleErrorf("enabled api scope %q depends on disabled scope %q", scope, extends)
434 }
435 }
436 }
437 }
438
439 return generatedScopes
Paul Duffind1b3a922020-01-22 11:57:20 +0000440}
441
Paul Duffine74ac732020-02-06 13:51:46 +0000442var xmlPermissionsFileTag = dependencyTag{name: "xml-permissions-file"}
443
Jiyong Parke3833882020-02-17 17:28:10 +0900444func IsXmlPermissionsFileDepTag(depTag blueprint.DependencyTag) bool {
445 if dt, ok := depTag.(dependencyTag); ok {
446 return dt == xmlPermissionsFileTag
447 }
448 return false
449}
450
Inseob Kimc0907f12019-02-08 21:00:45 +0900451func (module *SdkLibrary) DepsMutator(ctx android.BottomUpMutatorContext) {
Paul Duffin3375e352020-04-28 10:44:03 +0100452 for _, apiScope := range module.getGeneratedApiScopes(ctx) {
Paul Duffind1b3a922020-01-22 11:57:20 +0000453 // Add dependencies to the stubs library
Paul Duffin50061512020-01-21 16:31:05 +0000454 ctx.AddVariationDependencies(nil, apiScope.stubsTag, module.stubsName(apiScope))
Paul Duffind1b3a922020-01-22 11:57:20 +0000455
Paul Duffin50061512020-01-21 16:31:05 +0000456 // And the api file
Paul Duffind1b3a922020-01-22 11:57:20 +0000457 ctx.AddVariationDependencies(nil, apiScope.apiFileTag, module.docsName(apiScope))
Sundong Ahn054b19a2018-10-19 13:46:09 +0900458 }
459
Paul Duffine74ac732020-02-06 13:51:46 +0000460 if !proptools.Bool(module.sdkLibraryProperties.Api_only) {
461 // Add dependency to the rule for generating the xml permissions file
Jiyong Parke3833882020-02-17 17:28:10 +0900462 ctx.AddDependency(module, xmlPermissionsFileTag, module.xmlFileName())
Paul Duffine74ac732020-02-06 13:51:46 +0000463 }
464
Sundong Ahn054b19a2018-10-19 13:46:09 +0900465 module.Library.deps(ctx)
Jiyong Parkc678ad32018-04-10 13:07:10 +0900466}
467
Inseob Kimc0907f12019-02-08 21:00:45 +0900468func (module *SdkLibrary) GenerateAndroidBuildActions(ctx android.ModuleContext) {
Paul Duffin43db9be2019-12-30 17:35:49 +0000469 // Don't build an implementation library if this is api only.
470 if !proptools.Bool(module.sdkLibraryProperties.Api_only) {
471 module.Library.GenerateAndroidBuildActions(ctx)
472 }
Sundong Ahn054b19a2018-10-19 13:46:09 +0900473
Sundong Ahn57368eb2018-07-06 11:20:23 +0900474 // Record the paths to the header jars of the library (stubs and impl).
Paul Duffind1b3a922020-01-22 11:57:20 +0000475 // When this java_sdk_library is depended upon from others via "libs" property,
Jiyong Parkc678ad32018-04-10 13:07:10 +0900476 // the recorded paths will be returned depending on the link type of the caller.
477 ctx.VisitDirectDeps(func(to android.Module) {
478 otherName := ctx.OtherModuleName(to)
479 tag := ctx.OtherModuleDependencyTag(to)
480
Sundong Ahn57368eb2018-07-06 11:20:23 +0900481 if lib, ok := to.(Dependency); ok {
Paul Duffind1b3a922020-01-22 11:57:20 +0000482 if scopeTag, ok := tag.(scopeDependencyTag); ok {
483 apiScope := scopeTag.apiScope
484 scopePaths := module.getScopePaths(apiScope)
485 scopePaths.stubsHeaderPath = lib.HeaderJars()
486 scopePaths.stubsImplPath = lib.ImplementationJars()
Jiyong Parkc678ad32018-04-10 13:07:10 +0900487 }
488 }
Paul Duffin3d1248c2020-04-09 00:10:17 +0100489 if doc, ok := to.(ApiStubsProvider); ok {
Paul Duffind1b3a922020-01-22 11:57:20 +0000490 if scopeTag, ok := tag.(scopeDependencyTag); ok {
491 apiScope := scopeTag.apiScope
492 scopePaths := module.getScopePaths(apiScope)
Paul Duffin1fd005d2020-04-09 01:08:11 +0100493 scopePaths.currentApiFilePath = doc.ApiFilePath()
494 scopePaths.removedApiFilePath = doc.RemovedApiFilePath()
Paul Duffin3d1248c2020-04-09 00:10:17 +0100495 scopePaths.stubsSrcJar = doc.StubsSrcJar()
Paul Duffind1b3a922020-01-22 11:57:20 +0000496 } else {
Sundong Ahn20e998b2018-07-24 11:19:26 +0900497 ctx.ModuleErrorf("depends on module %q of unknown tag %q", otherName, tag)
498 }
499 }
Jiyong Parkc678ad32018-04-10 13:07:10 +0900500 })
501}
502
Jiyong Park0b0e1b92019-12-03 13:24:29 +0900503func (module *SdkLibrary) AndroidMkEntries() []android.AndroidMkEntries {
Paul Duffin43db9be2019-12-30 17:35:49 +0000504 if proptools.Bool(module.sdkLibraryProperties.Api_only) {
505 return nil
506 }
Jiyong Park0b0e1b92019-12-03 13:24:29 +0900507 entriesList := module.Library.AndroidMkEntries()
508 entries := &entriesList[0]
Jaewoong Jungb0c127c2019-08-29 14:56:03 -0700509 entries.Required = append(entries.Required, module.xmlFileName())
Jiyong Park0b0e1b92019-12-03 13:24:29 +0900510 return entriesList
Jiyong Park82484c02018-04-23 21:41:26 +0900511}
512
Jiyong Parkc678ad32018-04-10 13:07:10 +0900513// Module name of the stubs library
Paul Duffind1b3a922020-01-22 11:57:20 +0000514func (module *SdkLibrary) stubsName(apiScope *apiScope) string {
515 return apiScope.stubsModuleName(module.BaseModuleName())
Jiyong Parkc678ad32018-04-10 13:07:10 +0900516}
517
518// Module name of the docs
Paul Duffind1b3a922020-01-22 11:57:20 +0000519func (module *SdkLibrary) docsName(apiScope *apiScope) string {
520 return apiScope.docsModuleName(module.BaseModuleName())
Jiyong Parkc678ad32018-04-10 13:07:10 +0900521}
522
523// Module name of the runtime implementation library
Inseob Kimc0907f12019-02-08 21:00:45 +0900524func (module *SdkLibrary) implName() string {
Sundong Ahn054b19a2018-10-19 13:46:09 +0900525 return module.BaseModuleName()
Jiyong Parkc678ad32018-04-10 13:07:10 +0900526}
527
Jiyong Parkc678ad32018-04-10 13:07:10 +0900528// Module name of the XML file for the lib
Inseob Kimc0907f12019-02-08 21:00:45 +0900529func (module *SdkLibrary) xmlFileName() string {
Jiyong Parkc678ad32018-04-10 13:07:10 +0900530 return module.BaseModuleName() + sdkXmlFileSuffix
531}
532
Anton Hansson5fd5d242020-03-27 19:43:19 +0000533// The dist path of the stub artifacts
534func (module *SdkLibrary) apiDistPath(apiScope *apiScope) string {
535 if module.ModuleBase.Owner() != "" {
536 return path.Join("apistubs", module.ModuleBase.Owner(), apiScope.name)
537 } else if Bool(module.sdkLibraryProperties.Core_lib) {
538 return path.Join("apistubs", "core", apiScope.name)
539 } else {
540 return path.Join("apistubs", "android", apiScope.name)
541 }
542}
543
Paul Duffin12ceb462019-12-24 20:31:31 +0000544// Get the sdk version for use when compiling the stubs library.
Paul Duffinf0229202020-04-29 16:47:28 +0100545func (module *SdkLibrary) sdkVersionForStubsLibrary(mctx android.DefaultableHookContext, apiScope *apiScope) string {
Paul Duffin12ceb462019-12-24 20:31:31 +0000546 sdkDep := decodeSdkDep(mctx, sdkContext(&module.Library))
547 if sdkDep.hasStandardLibs() {
548 // If building against a standard sdk then use the sdk version appropriate for the scope.
Paul Duffind1b3a922020-01-22 11:57:20 +0000549 return apiScope.sdkVersion
Paul Duffin12ceb462019-12-24 20:31:31 +0000550 } else {
551 // Otherwise, use no system module.
552 return "none"
553 }
554}
555
Paul Duffind1b3a922020-01-22 11:57:20 +0000556func (module *SdkLibrary) latestApiFilegroupName(apiScope *apiScope) string {
557 return ":" + module.BaseModuleName() + ".api." + apiScope.name + ".latest"
Jiyong Park58c518b2018-05-12 22:29:12 +0900558}
Jiyong Parkc678ad32018-04-10 13:07:10 +0900559
Paul Duffind1b3a922020-01-22 11:57:20 +0000560func (module *SdkLibrary) latestRemovedApiFilegroupName(apiScope *apiScope) string {
561 return ":" + module.BaseModuleName() + "-removed.api." + apiScope.name + ".latest"
Jiyong Parkc678ad32018-04-10 13:07:10 +0900562}
563
564// Creates a static java library that has API stubs
Paul Duffinf0229202020-04-29 16:47:28 +0100565func (module *SdkLibrary) createStubsLibrary(mctx android.DefaultableHookContext, apiScope *apiScope) {
Jiyong Parkc678ad32018-04-10 13:07:10 +0900566 props := struct {
Sundong Ahn0d7dff42019-12-04 12:53:44 +0900567 Name *string
Paul Duffin4911a892020-04-29 23:35:13 +0100568 Visibility []string
Sundong Ahn0d7dff42019-12-04 12:53:44 +0900569 Srcs []string
Paul Duffin367ab912019-12-23 19:40:36 +0000570 Installable *bool
Sundong Ahn0d7dff42019-12-04 12:53:44 +0900571 Sdk_version *string
Paul Duffin12ceb462019-12-24 20:31:31 +0000572 System_modules *string
Paul Duffinab8da5d2020-02-07 16:12:04 +0000573 Patch_module *string
Sundong Ahn0d7dff42019-12-04 12:53:44 +0900574 Libs []string
575 Soc_specific *bool
576 Device_specific *bool
577 Product_specific *bool
578 System_ext_specific *bool
579 Compile_dex *bool
Sundong Ahn0d7dff42019-12-04 12:53:44 +0900580 Java_version *string
581 Product_variables struct {
Jiyong Park82484c02018-04-23 21:41:26 +0900582 Pdk struct {
583 Enabled *bool
584 }
Jiyong Parkc678ad32018-04-10 13:07:10 +0900585 }
Sundong Ahn054b19a2018-10-19 13:46:09 +0900586 Openjdk9 struct {
587 Srcs []string
588 Javacflags []string
589 }
Anton Hansson5fd5d242020-03-27 19:43:19 +0000590 Dist struct {
591 Targets []string
592 Dest *string
593 Dir *string
594 Tag *string
595 }
Jiyong Parkc678ad32018-04-10 13:07:10 +0900596 }{}
597
Jiyong Parkdf130542018-04-27 16:29:21 +0900598 props.Name = proptools.StringPtr(module.stubsName(apiScope))
Paul Duffin4911a892020-04-29 23:35:13 +0100599
600 // If stubs_library_visibility is not set then the created module will use the
601 // visibility of this module.
602 visibility := module.sdkLibraryProperties.Stubs_library_visibility
603 props.Visibility = visibility
604
Jiyong Parkc678ad32018-04-10 13:07:10 +0900605 // sources are generated from the droiddoc
Jiyong Parkdf130542018-04-27 16:29:21 +0900606 props.Srcs = []string{":" + module.docsName(apiScope)}
Paul Duffin12ceb462019-12-24 20:31:31 +0000607 sdkVersion := module.sdkVersionForStubsLibrary(mctx, apiScope)
Paul Duffin52d398a2019-06-11 12:31:14 +0100608 props.Sdk_version = proptools.StringPtr(sdkVersion)
Paul Duffin12ceb462019-12-24 20:31:31 +0000609 props.System_modules = module.Library.Module.deviceProperties.System_modules
Paul Duffinab8da5d2020-02-07 16:12:04 +0000610 props.Patch_module = module.Library.Module.properties.Patch_module
Paul Duffin367ab912019-12-23 19:40:36 +0000611 props.Installable = proptools.BoolPtr(false)
Sundong Ahn054b19a2018-10-19 13:46:09 +0900612 props.Libs = module.sdkLibraryProperties.Stub_only_libs
Jiyong Park82484c02018-04-23 21:41:26 +0900613 props.Product_variables.Pdk.Enabled = proptools.BoolPtr(false)
Sundong Ahn054b19a2018-10-19 13:46:09 +0900614 props.Openjdk9.Srcs = module.Library.Module.properties.Openjdk9.Srcs
615 props.Openjdk9.Javacflags = module.Library.Module.properties.Openjdk9.Javacflags
616 props.Java_version = module.Library.Module.properties.Java_version
617 if module.Library.Module.deviceProperties.Compile_dex != nil {
618 props.Compile_dex = module.Library.Module.deviceProperties.Compile_dex
Sundong Ahndd567f92018-07-31 17:19:11 +0900619 }
Jiyong Parkc678ad32018-04-10 13:07:10 +0900620
621 if module.SocSpecific() {
622 props.Soc_specific = proptools.BoolPtr(true)
623 } else if module.DeviceSpecific() {
624 props.Device_specific = proptools.BoolPtr(true)
625 } else if module.ProductSpecific() {
626 props.Product_specific = proptools.BoolPtr(true)
Sundong Ahn0d7dff42019-12-04 12:53:44 +0900627 } else if module.SystemExtSpecific() {
628 props.System_ext_specific = proptools.BoolPtr(true)
Jiyong Parkc678ad32018-04-10 13:07:10 +0900629 }
Anton Hansson5fd5d242020-03-27 19:43:19 +0000630 // Dist the class jar artifact for sdk builds.
631 if !Bool(module.sdkLibraryProperties.No_dist) {
632 props.Dist.Targets = []string{"sdk", "win_sdk"}
633 props.Dist.Dest = proptools.StringPtr(fmt.Sprintf("%v.jar", module.BaseModuleName()))
634 props.Dist.Dir = proptools.StringPtr(module.apiDistPath(apiScope))
635 props.Dist.Tag = proptools.StringPtr(".jar")
636 }
Jiyong Parkc678ad32018-04-10 13:07:10 +0900637
Colin Cross84dfc3d2019-09-25 11:33:01 -0700638 mctx.CreateModule(LibraryFactory, &props)
Jiyong Parkc678ad32018-04-10 13:07:10 +0900639}
640
Paul Duffin6d0886e2020-04-07 18:49:53 +0100641// Creates a droidstubs module that creates stubs source files from the given full source
Jiyong Parkc678ad32018-04-10 13:07:10 +0900642// files
Paul Duffinf0229202020-04-29 16:47:28 +0100643func (module *SdkLibrary) createStubsSources(mctx android.DefaultableHookContext, apiScope *apiScope) {
Jiyong Parkc678ad32018-04-10 13:07:10 +0900644 props := struct {
Sundong Ahn054b19a2018-10-19 13:46:09 +0900645 Name *string
Paul Duffin4911a892020-04-29 23:35:13 +0100646 Visibility []string
Sundong Ahn054b19a2018-10-19 13:46:09 +0900647 Srcs []string
648 Installable *bool
Paul Duffin52d398a2019-06-11 12:31:14 +0100649 Sdk_version *string
Paul Duffin12ceb462019-12-24 20:31:31 +0000650 System_modules *string
Sundong Ahn054b19a2018-10-19 13:46:09 +0900651 Libs []string
Paul Duffin11512472019-02-11 15:55:17 +0000652 Arg_files []string
Sundong Ahn054b19a2018-10-19 13:46:09 +0900653 Args *string
Sundong Ahn054b19a2018-10-19 13:46:09 +0900654 Java_version *string
655 Merge_annotations_dirs []string
656 Merge_inclusion_annotations_dirs []string
657 Check_api struct {
Inseob Kim38449af2019-02-28 14:24:05 +0900658 Current ApiToCheck
659 Last_released ApiToCheck
660 Ignore_missing_latest_api *bool
Paul Duffin160fe412020-05-10 19:32:20 +0100661
662 Api_lint struct {
663 Enabled *bool
664 New_since *string
665 Baseline_file *string
666 }
Jiyong Park58c518b2018-05-12 22:29:12 +0900667 }
Sundong Ahn1b92c822018-05-29 11:35:17 +0900668 Aidl struct {
669 Include_dirs []string
670 Local_include_dirs []string
671 }
Anton Hansson5fd5d242020-03-27 19:43:19 +0000672 Dist struct {
673 Targets []string
674 Dest *string
675 Dir *string
676 }
Jiyong Parkc678ad32018-04-10 13:07:10 +0900677 }{}
678
Paul Duffin7b78b4d2020-04-28 14:08:32 +0100679 // The stubs source processing uses the same compile time classpath when extracting the
680 // API from the implementation library as it does when compiling it. i.e. the same
681 // * sdk version
682 // * system_modules
683 // * libs (static_libs/libs)
Paul Duffin250e6192019-06-07 10:44:37 +0100684
Jiyong Parkdf130542018-04-27 16:29:21 +0900685 props.Name = proptools.StringPtr(module.docsName(apiScope))
Paul Duffin4911a892020-04-29 23:35:13 +0100686
687 // If stubs_source_visibility is not set then the created module will use the
688 // visibility of this module.
689 visibility := module.sdkLibraryProperties.Stubs_source_visibility
690 props.Visibility = visibility
691
Sundong Ahn054b19a2018-10-19 13:46:09 +0900692 props.Srcs = append(props.Srcs, module.Library.Module.properties.Srcs...)
Paul Duffin7b78b4d2020-04-28 14:08:32 +0100693 props.Sdk_version = module.Library.Module.deviceProperties.Sdk_version
Paul Duffin12ceb462019-12-24 20:31:31 +0000694 props.System_modules = module.Library.Module.deviceProperties.System_modules
Jiyong Parkc678ad32018-04-10 13:07:10 +0900695 props.Installable = proptools.BoolPtr(false)
Sundong Ahne6f0b052018-06-05 16:46:14 +0900696 // A droiddoc module has only one Libs property and doesn't distinguish between
697 // shared libs and static libs. So we need to add both of these libs to Libs property.
Sundong Ahn054b19a2018-10-19 13:46:09 +0900698 props.Libs = module.Library.Module.properties.Libs
699 props.Libs = append(props.Libs, module.Library.Module.properties.Static_libs...)
700 props.Aidl.Include_dirs = module.Library.Module.deviceProperties.Aidl.Include_dirs
701 props.Aidl.Local_include_dirs = module.Library.Module.deviceProperties.Aidl.Local_include_dirs
Sundong Ahn054b19a2018-10-19 13:46:09 +0900702 props.Java_version = module.Library.Module.properties.Java_version
Jiyong Parkc678ad32018-04-10 13:07:10 +0900703
Sundong Ahn054b19a2018-10-19 13:46:09 +0900704 props.Merge_annotations_dirs = module.sdkLibraryProperties.Merge_annotations_dirs
705 props.Merge_inclusion_annotations_dirs = module.sdkLibraryProperties.Merge_inclusion_annotations_dirs
706
Paul Duffin6d0886e2020-04-07 18:49:53 +0100707 droidstubsArgs := []string{}
Paul Duffin235ffff2019-12-24 10:41:30 +0000708 if len(module.sdkLibraryProperties.Api_packages) != 0 {
Paul Duffin6d0886e2020-04-07 18:49:53 +0100709 droidstubsArgs = append(droidstubsArgs, "--stub-packages "+strings.Join(module.sdkLibraryProperties.Api_packages, ":"))
Paul Duffin235ffff2019-12-24 10:41:30 +0000710 }
711 if len(module.sdkLibraryProperties.Hidden_api_packages) != 0 {
Paul Duffin6d0886e2020-04-07 18:49:53 +0100712 droidstubsArgs = append(droidstubsArgs,
Paul Duffin235ffff2019-12-24 10:41:30 +0000713 android.JoinWithPrefix(module.sdkLibraryProperties.Hidden_api_packages, " --hide-package "))
714 }
Paul Duffin6d0886e2020-04-07 18:49:53 +0100715 droidstubsArgs = append(droidstubsArgs, module.sdkLibraryProperties.Droiddoc_options...)
Paul Duffin235ffff2019-12-24 10:41:30 +0000716 disabledWarnings := []string{
717 "MissingPermission",
718 "BroadcastBehavior",
719 "HiddenSuperclass",
720 "DeprecationMismatch",
721 "UnavailableSymbol",
722 "SdkConstant",
723 "HiddenTypeParameter",
724 "Todo",
725 "Typo",
726 }
Paul Duffin6d0886e2020-04-07 18:49:53 +0100727 droidstubsArgs = append(droidstubsArgs, android.JoinWithPrefix(disabledWarnings, "--hide "))
Sundong Ahnfb2721f2018-09-17 13:23:09 +0900728
Paul Duffin1fb487d2020-04-07 18:50:10 +0100729 // Add in scope specific arguments.
730 droidstubsArgs = append(droidstubsArgs, apiScope.droidstubsArgs...)
Paul Duffin11512472019-02-11 15:55:17 +0000731 props.Arg_files = module.sdkLibraryProperties.Droiddoc_option_files
Paul Duffin6d0886e2020-04-07 18:49:53 +0100732 props.Args = proptools.StringPtr(strings.Join(droidstubsArgs, " "))
Jiyong Parkc678ad32018-04-10 13:07:10 +0900733
734 // List of APIs identified from the provided source files are created. They are later
735 // compared against to the not-yet-released (a.k.a current) list of APIs and to the
736 // last-released (a.k.a numbered) list of API.
Paul Duffind1b3a922020-01-22 11:57:20 +0000737 currentApiFileName := apiScope.apiFilePrefix + "current.txt"
738 removedApiFileName := apiScope.apiFilePrefix + "removed.txt"
Paul Duffin749f98f2019-12-30 17:23:46 +0000739 apiDir := module.getApiDir()
740 currentApiFileName = path.Join(apiDir, currentApiFileName)
741 removedApiFileName = path.Join(apiDir, removedApiFileName)
Jiyong Parkc678ad32018-04-10 13:07:10 +0900742
Jiyong Park58c518b2018-05-12 22:29:12 +0900743 // check against the not-yet-release API
744 props.Check_api.Current.Api_file = proptools.StringPtr(currentApiFileName)
745 props.Check_api.Current.Removed_api_file = proptools.StringPtr(removedApiFileName)
Jiyong Park58c518b2018-05-12 22:29:12 +0900746
Anton Hansson6478ac12020-05-02 11:19:36 +0100747 if !apiScope.unstable {
748 // check against the latest released API
Paul Duffin160fe412020-05-10 19:32:20 +0100749 latestApiFilegroupName := proptools.StringPtr(module.latestApiFilegroupName(apiScope))
750 props.Check_api.Last_released.Api_file = latestApiFilegroupName
Anton Hansson6478ac12020-05-02 11:19:36 +0100751 props.Check_api.Last_released.Removed_api_file = proptools.StringPtr(
752 module.latestRemovedApiFilegroupName(apiScope))
753 props.Check_api.Ignore_missing_latest_api = proptools.BoolPtr(true)
Paul Duffin160fe412020-05-10 19:32:20 +0100754
755 if proptools.Bool(module.sdkLibraryProperties.Api_lint.Enabled) {
756 // Enable api lint.
757 props.Check_api.Api_lint.Enabled = proptools.BoolPtr(true)
758 props.Check_api.Api_lint.New_since = latestApiFilegroupName
759
760 // If it exists then pass a lint-baseline.txt through to droidstubs.
761 baselinePath := path.Join(apiDir, apiScope.apiFilePrefix+"lint-baseline.txt")
762 baselinePathRelativeToRoot := path.Join(mctx.ModuleDir(), baselinePath)
763 paths, err := mctx.GlobWithDeps(baselinePathRelativeToRoot, nil)
764 if err != nil {
765 mctx.ModuleErrorf("error checking for presence of %s: %s", baselinePathRelativeToRoot, err)
766 }
767 if len(paths) == 1 {
768 props.Check_api.Api_lint.Baseline_file = proptools.StringPtr(baselinePath)
769 } else if len(paths) != 0 {
770 mctx.ModuleErrorf("error checking for presence of %s: expected one path, found: %v", baselinePathRelativeToRoot, paths)
771 }
772 }
Anton Hansson6478ac12020-05-02 11:19:36 +0100773 }
Jiyong Park58c518b2018-05-12 22:29:12 +0900774
Anton Hansson5fd5d242020-03-27 19:43:19 +0000775 // Dist the api txt artifact for sdk builds.
776 if !Bool(module.sdkLibraryProperties.No_dist) {
777 props.Dist.Targets = []string{"sdk", "win_sdk"}
778 props.Dist.Dest = proptools.StringPtr(fmt.Sprintf("%v.txt", module.BaseModuleName()))
779 props.Dist.Dir = proptools.StringPtr(path.Join(module.apiDistPath(apiScope), "api"))
780 }
781
Colin Cross84dfc3d2019-09-25 11:33:01 -0700782 mctx.CreateModule(DroidstubsFactory, &props)
Jiyong Parkc678ad32018-04-10 13:07:10 +0900783}
784
Jooyung Han5e9013b2020-03-10 06:23:13 +0900785func (module *SdkLibrary) DepIsInSameApex(mctx android.BaseModuleContext, dep android.Module) bool {
786 depTag := mctx.OtherModuleDependencyTag(dep)
787 if depTag == xmlPermissionsFileTag {
788 return true
789 }
790 return module.Library.DepIsInSameApex(mctx, dep)
791}
792
Jiyong Parkc678ad32018-04-10 13:07:10 +0900793// Creates the xml file that publicizes the runtime library
Paul Duffinf0229202020-04-29 16:47:28 +0100794func (module *SdkLibrary) createXmlFile(mctx android.DefaultableHookContext) {
Jiyong Parke3833882020-02-17 17:28:10 +0900795 props := struct {
Sundong Ahn0d7dff42019-12-04 12:53:44 +0900796 Name *string
Jiyong Parke3833882020-02-17 17:28:10 +0900797 Lib_name *string
Sundong Ahn0d7dff42019-12-04 12:53:44 +0900798 Soc_specific *bool
799 Device_specific *bool
800 Product_specific *bool
801 System_ext_specific *bool
Jooyung Han5e9013b2020-03-10 06:23:13 +0900802 Apex_available []string
Jiyong Parke3833882020-02-17 17:28:10 +0900803 }{
Jooyung Han5e9013b2020-03-10 06:23:13 +0900804 Name: proptools.StringPtr(module.xmlFileName()),
805 Lib_name: proptools.StringPtr(module.BaseModuleName()),
806 Apex_available: module.ApexProperties.Apex_available,
Jiyong Parkc678ad32018-04-10 13:07:10 +0900807 }
Jiyong Parke3833882020-02-17 17:28:10 +0900808
809 if module.SocSpecific() {
810 props.Soc_specific = proptools.BoolPtr(true)
811 } else if module.DeviceSpecific() {
812 props.Device_specific = proptools.BoolPtr(true)
813 } else if module.ProductSpecific() {
814 props.Product_specific = proptools.BoolPtr(true)
815 } else if module.SystemExtSpecific() {
816 props.System_ext_specific = proptools.BoolPtr(true)
817 }
818
819 mctx.CreateModule(sdkLibraryXmlFactory, &props)
Jiyong Parkc678ad32018-04-10 13:07:10 +0900820}
821
Paul Duffin50061512020-01-21 16:31:05 +0000822func PrebuiltJars(ctx android.BaseModuleContext, baseName string, s sdkSpec) android.Paths {
Jiyong Park6a927c42020-01-21 02:03:43 +0900823 var ver sdkVersion
824 var kind sdkKind
825 if s.usePrebuilt(ctx) {
826 ver = s.version
827 kind = s.kind
Jiyong Parkc678ad32018-04-10 13:07:10 +0900828 } else {
Jiyong Park6a927c42020-01-21 02:03:43 +0900829 // We don't have prebuilt SDK for the specific sdkVersion.
830 // Instead of breaking the build, fallback to use "system_current"
831 ver = sdkVersionCurrent
832 kind = sdkSystem
Sundong Ahn054b19a2018-10-19 13:46:09 +0900833 }
Jiyong Park6a927c42020-01-21 02:03:43 +0900834
835 dir := filepath.Join("prebuilts", "sdk", ver.String(), kind.String())
Paul Duffin50061512020-01-21 16:31:05 +0000836 jar := filepath.Join(dir, baseName+".jar")
Sundong Ahn054b19a2018-10-19 13:46:09 +0900837 jarPath := android.ExistentPathForSource(ctx, jar)
Sundong Ahnae418ac2019-02-28 15:01:28 +0900838 if !jarPath.Valid() {
Colin Cross07c88562020-01-07 09:34:44 -0800839 if ctx.Config().AllowMissingDependencies() {
840 return android.Paths{android.PathForSource(ctx, jar)}
841 } else {
Jiyong Park6a927c42020-01-21 02:03:43 +0900842 ctx.PropertyErrorf("sdk_library", "invalid sdk version %q, %q does not exist", s.raw, jar)
Colin Cross07c88562020-01-07 09:34:44 -0800843 }
Sundong Ahnae418ac2019-02-28 15:01:28 +0900844 return nil
845 }
Sundong Ahn054b19a2018-10-19 13:46:09 +0900846 return android.Paths{jarPath.Path()}
847}
848
Paul Duffind1b3a922020-01-22 11:57:20 +0000849func (module *SdkLibrary) sdkJars(
850 ctx android.BaseModuleContext,
851 sdkVersion sdkSpec,
852 headerJars bool) android.Paths {
853
Paul Duffin50061512020-01-21 16:31:05 +0000854 // If a specific numeric version has been requested then use prebuilt versions of the sdk.
855 if sdkVersion.version.isNumbered() {
856 return PrebuiltJars(ctx, module.BaseModuleName(), sdkVersion)
Sundong Ahn054b19a2018-10-19 13:46:09 +0900857 } else {
Paul Duffind1b3a922020-01-22 11:57:20 +0000858 if !sdkVersion.specified() {
859 if headerJars {
860 return module.Library.HeaderJars()
861 } else {
862 return module.Library.ImplementationJars()
863 }
864 }
Paul Duffin726d23c2020-01-22 16:30:37 +0000865 var apiScope *apiScope
Jiyong Park6a927c42020-01-21 02:03:43 +0900866 switch sdkVersion.kind {
867 case sdkSystem:
Paul Duffin726d23c2020-01-22 16:30:37 +0000868 apiScope = apiScopeSystem
869 case sdkTest:
870 apiScope = apiScopeTest
Jiyong Park6a927c42020-01-21 02:03:43 +0900871 case sdkPrivate:
Sundong Ahn054b19a2018-10-19 13:46:09 +0900872 return module.Library.HeaderJars()
Jiyong Park6a927c42020-01-21 02:03:43 +0900873 default:
Paul Duffin726d23c2020-01-22 16:30:37 +0000874 apiScope = apiScopePublic
Paul Duffind1b3a922020-01-22 11:57:20 +0000875 }
876
Paul Duffin726d23c2020-01-22 16:30:37 +0000877 paths := module.getScopePaths(apiScope)
Paul Duffind1b3a922020-01-22 11:57:20 +0000878 if headerJars {
879 return paths.stubsHeaderPath
880 } else {
881 return paths.stubsImplPath
Sundong Ahn054b19a2018-10-19 13:46:09 +0900882 }
Jiyong Parkc678ad32018-04-10 13:07:10 +0900883 }
884}
885
Sundong Ahn241cd372018-07-13 16:16:44 +0900886// to satisfy SdkLibraryDependency interface
Paul Duffind1b3a922020-01-22 11:57:20 +0000887func (module *SdkLibrary) SdkHeaderJars(ctx android.BaseModuleContext, sdkVersion sdkSpec) android.Paths {
888 return module.sdkJars(ctx, sdkVersion, true /*headerJars*/)
889}
890
891// to satisfy SdkLibraryDependency interface
Jiyong Park6a927c42020-01-21 02:03:43 +0900892func (module *SdkLibrary) SdkImplementationJars(ctx android.BaseModuleContext, sdkVersion sdkSpec) android.Paths {
Paul Duffind1b3a922020-01-22 11:57:20 +0000893 return module.sdkJars(ctx, sdkVersion, false /*headerJars*/)
Sundong Ahn241cd372018-07-13 16:16:44 +0900894}
895
Sundong Ahn80a87b32019-05-13 15:02:50 +0900896func (module *SdkLibrary) SetNoDist() {
897 module.sdkLibraryProperties.No_dist = proptools.BoolPtr(true)
898}
899
Colin Cross571cccf2019-02-04 11:22:08 -0800900var javaSdkLibrariesKey = android.NewOnceKey("javaSdkLibraries")
901
Jiyong Park82484c02018-04-23 21:41:26 +0900902func javaSdkLibraries(config android.Config) *[]string {
Colin Cross571cccf2019-02-04 11:22:08 -0800903 return config.Once(javaSdkLibrariesKey, func() interface{} {
Jiyong Park82484c02018-04-23 21:41:26 +0900904 return &[]string{}
905 }).(*[]string)
906}
907
Paul Duffin749f98f2019-12-30 17:23:46 +0000908func (module *SdkLibrary) getApiDir() string {
909 return proptools.StringDefault(module.sdkLibraryProperties.Api_dir, "api")
910}
911
Jiyong Parkc678ad32018-04-10 13:07:10 +0900912// For a java_sdk_library module, create internal modules for stubs, docs,
913// runtime libs and xml file. If requested, the stubs and docs are created twice
914// once for public API level and once for system API level
Paul Duffinf0229202020-04-29 16:47:28 +0100915func (module *SdkLibrary) CreateInternalModules(mctx android.DefaultableHookContext) {
916 // If the module has been disabled then don't create any child modules.
917 if !module.Enabled() {
918 return
919 }
920
Inseob Kim6e93ac92019-03-21 17:43:49 +0900921 if len(module.Library.Module.properties.Srcs) == 0 {
Inseob Kimc0907f12019-02-08 21:00:45 +0900922 mctx.PropertyErrorf("srcs", "java_sdk_library must specify srcs")
Jooyung Han58f26ab2019-12-18 15:34:32 +0900923 return
Inseob Kimc0907f12019-02-08 21:00:45 +0900924 }
925
Paul Duffin37e0b772019-12-30 17:20:10 +0000926 // If this builds against standard libraries (i.e. is not part of the core libraries)
927 // then assume it provides both system and test apis. Otherwise, assume it does not and
928 // also assume it does not contribute to the dist build.
929 sdkDep := decodeSdkDep(mctx, sdkContext(&module.Library))
930 hasSystemAndTestApis := sdkDep.hasStandardLibs()
Paul Duffin3375e352020-04-28 10:44:03 +0100931 module.sdkLibraryProperties.Generate_system_and_test_apis = hasSystemAndTestApis
Paul Duffin37e0b772019-12-30 17:20:10 +0000932 module.sdkLibraryProperties.No_dist = proptools.BoolPtr(!hasSystemAndTestApis)
933
Inseob Kim8098faa2019-03-18 10:19:51 +0900934 missing_current_api := false
935
Paul Duffin3375e352020-04-28 10:44:03 +0100936 generatedScopes := module.getGeneratedApiScopes(mctx)
Paul Duffind1b3a922020-01-22 11:57:20 +0000937
Paul Duffin749f98f2019-12-30 17:23:46 +0000938 apiDir := module.getApiDir()
Paul Duffin3375e352020-04-28 10:44:03 +0100939 for _, scope := range generatedScopes {
Inseob Kim8098faa2019-03-18 10:19:51 +0900940 for _, api := range []string{"current.txt", "removed.txt"} {
Paul Duffind1b3a922020-01-22 11:57:20 +0000941 path := path.Join(mctx.ModuleDir(), apiDir, scope.apiFilePrefix+api)
Inseob Kim8098faa2019-03-18 10:19:51 +0900942 p := android.ExistentPathForSource(mctx, path)
943 if !p.Valid() {
944 mctx.ModuleErrorf("Current api file %#v doesn't exist", path)
945 missing_current_api = true
946 }
947 }
948 }
949
950 if missing_current_api {
951 script := "build/soong/scripts/gen-java-current-api-files.sh"
952 p := android.ExistentPathForSource(mctx, script)
953
954 if !p.Valid() {
955 panic(fmt.Sprintf("script file %s doesn't exist", script))
956 }
957
958 mctx.ModuleErrorf("One or more current api files are missing. "+
959 "You can update them by:\n"+
Paul Duffin37e0b772019-12-30 17:20:10 +0000960 "%s %q %s && m update-api",
Paul Duffind1b3a922020-01-22 11:57:20 +0000961 script, filepath.Join(mctx.ModuleDir(), apiDir),
Paul Duffin3375e352020-04-28 10:44:03 +0100962 strings.Join(generatedScopes.Strings(func(s *apiScope) string { return s.apiFilePrefix }), " "))
Inseob Kim8098faa2019-03-18 10:19:51 +0900963 return
964 }
965
Paul Duffin3375e352020-04-28 10:44:03 +0100966 for _, scope := range generatedScopes {
Paul Duffind1b3a922020-01-22 11:57:20 +0000967 module.createStubsLibrary(mctx, scope)
968 module.createStubsSources(mctx, scope)
Inseob Kimc0907f12019-02-08 21:00:45 +0900969 }
970
Paul Duffin43db9be2019-12-30 17:35:49 +0000971 if !proptools.Bool(module.sdkLibraryProperties.Api_only) {
972 // for runtime
973 module.createXmlFile(mctx)
974
975 // record java_sdk_library modules so that they are exported to make
976 javaSdkLibraries := javaSdkLibraries(mctx.Config())
977 javaSdkLibrariesLock.Lock()
978 defer javaSdkLibrariesLock.Unlock()
979 *javaSdkLibraries = append(*javaSdkLibraries, module.BaseModuleName())
980 }
Inseob Kimc0907f12019-02-08 21:00:45 +0900981}
982
983func (module *SdkLibrary) InitSdkLibraryProperties() {
Sundong Ahn054b19a2018-10-19 13:46:09 +0900984 module.AddProperties(
985 &module.sdkLibraryProperties,
986 &module.Library.Module.properties,
987 &module.Library.Module.dexpreoptProperties,
988 &module.Library.Module.deviceProperties,
989 &module.Library.Module.protoProperties,
990 )
991
992 module.Library.Module.properties.Installable = proptools.BoolPtr(true)
993 module.Library.Module.deviceProperties.IsSDKLibrary = true
Inseob Kimc0907f12019-02-08 21:00:45 +0900994}
Sundong Ahn054b19a2018-10-19 13:46:09 +0900995
Jaewoong Jung4f158ee2019-07-11 10:05:35 -0700996// java_sdk_library is a special Java library that provides optional platform APIs to apps.
997// In practice, it can be viewed as a combination of several modules: 1) stubs library that clients
998// are linked against to, 2) droiddoc module that internally generates API stubs source files,
999// 3) the real runtime shared library that implements the APIs, and 4) XML file for adding
1000// the runtime lib to the classpath at runtime if requested via <uses-library>.
Inseob Kimc0907f12019-02-08 21:00:45 +09001001func SdkLibraryFactory() android.Module {
1002 module := &SdkLibrary{}
1003 module.InitSdkLibraryProperties()
Jooyung Han58f26ab2019-12-18 15:34:32 +09001004 android.InitApexModule(module)
Sundong Ahn054b19a2018-10-19 13:46:09 +09001005 InitJavaModule(module, android.HostAndDeviceSupported)
Paul Duffin3375e352020-04-28 10:44:03 +01001006
1007 // Initialize the map from scope to scope specific properties.
1008 scopeToProperties := make(map[*apiScope]*ApiScopeProperties)
1009 for _, scope := range allApiScopes {
1010 scopeToProperties[scope] = scope.scopeSpecificProperties(module)
1011 }
1012 module.scopeToProperties = scopeToProperties
1013
Paul Duffin4911a892020-04-29 23:35:13 +01001014 // Add the properties containing visibility rules so that they are checked.
1015 android.AddVisibilityProperty(module, "stubs_library_visibility", &module.sdkLibraryProperties.Stubs_library_visibility)
1016 android.AddVisibilityProperty(module, "stubs_source_visibility", &module.sdkLibraryProperties.Stubs_source_visibility)
1017
Paul Duffinf0229202020-04-29 16:47:28 +01001018 module.SetDefaultableHook(func(ctx android.DefaultableHookContext) { module.CreateInternalModules(ctx) })
Jiyong Parkc678ad32018-04-10 13:07:10 +09001019 return module
1020}
Colin Cross79c7c262019-04-17 11:11:46 -07001021
1022//
1023// SDK library prebuilts
1024//
1025
Paul Duffin56d44902020-01-31 13:36:25 +00001026// Properties associated with each api scope.
1027type sdkLibraryScopeProperties struct {
Colin Cross79c7c262019-04-17 11:11:46 -07001028 Jars []string `android:"path"`
1029
1030 Sdk_version *string
1031
Colin Cross79c7c262019-04-17 11:11:46 -07001032 // List of shared java libs that this module has dependencies to
1033 Libs []string
Paul Duffin3d1248c2020-04-09 00:10:17 +01001034
1035 // The stub sources.
1036 Stub_srcs []string `android:"path"`
Paul Duffin1fd005d2020-04-09 01:08:11 +01001037
1038 // The current.txt
1039 Current_api string `android:"path"`
1040
1041 // The removed.txt
1042 Removed_api string `android:"path"`
Colin Cross79c7c262019-04-17 11:11:46 -07001043}
1044
Paul Duffin56d44902020-01-31 13:36:25 +00001045type sdkLibraryImportProperties struct {
Paul Duffinfcfd7912020-01-31 17:54:30 +00001046 // List of shared java libs, common to all scopes, that this module has
1047 // dependencies to
1048 Libs []string
Paul Duffin56d44902020-01-31 13:36:25 +00001049}
1050
Colin Cross79c7c262019-04-17 11:11:46 -07001051type sdkLibraryImport struct {
1052 android.ModuleBase
1053 android.DefaultableModuleBase
1054 prebuilt android.Prebuilt
Paul Duffindd46f712020-02-10 13:37:10 +00001055 android.ApexModuleBase
1056 android.SdkBase
Colin Cross79c7c262019-04-17 11:11:46 -07001057
1058 properties sdkLibraryImportProperties
1059
Paul Duffin46a26a82020-04-07 19:27:04 +01001060 // Map from api scope to the scope specific property structure.
1061 scopeProperties map[*apiScope]*sdkLibraryScopeProperties
1062
Paul Duffin56d44902020-01-31 13:36:25 +00001063 commonToSdkLibraryAndImport
Colin Cross79c7c262019-04-17 11:11:46 -07001064}
1065
1066var _ SdkLibraryDependency = (*sdkLibraryImport)(nil)
1067
Paul Duffin46a26a82020-04-07 19:27:04 +01001068// The type of a structure that contains a field of type sdkLibraryScopeProperties
1069// for each apiscope in allApiScopes, e.g. something like:
1070// struct {
1071// Public sdkLibraryScopeProperties
1072// System sdkLibraryScopeProperties
1073// ...
1074// }
1075var allScopeStructType = createAllScopePropertiesStructType()
1076
1077// Dynamically create a structure type for each apiscope in allApiScopes.
1078func createAllScopePropertiesStructType() reflect.Type {
1079 var fields []reflect.StructField
1080 for _, apiScope := range allApiScopes {
1081 field := reflect.StructField{
1082 Name: apiScope.fieldName,
1083 Type: reflect.TypeOf(sdkLibraryScopeProperties{}),
1084 }
1085 fields = append(fields, field)
1086 }
1087
1088 return reflect.StructOf(fields)
1089}
1090
1091// Create an instance of the scope specific structure type and return a map
1092// from apiscope to a pointer to each scope specific field.
1093func createPropertiesInstance() (interface{}, map[*apiScope]*sdkLibraryScopeProperties) {
1094 allScopePropertiesPtr := reflect.New(allScopeStructType)
1095 allScopePropertiesStruct := allScopePropertiesPtr.Elem()
1096 scopeProperties := make(map[*apiScope]*sdkLibraryScopeProperties)
1097
1098 for _, apiScope := range allApiScopes {
1099 field := allScopePropertiesStruct.FieldByName(apiScope.fieldName)
1100 scopeProperties[apiScope] = field.Addr().Interface().(*sdkLibraryScopeProperties)
1101 }
1102
1103 return allScopePropertiesPtr.Interface(), scopeProperties
1104}
1105
Jaewoong Jung4f158ee2019-07-11 10:05:35 -07001106// java_sdk_library_import imports a prebuilt java_sdk_library.
Colin Cross79c7c262019-04-17 11:11:46 -07001107func sdkLibraryImportFactory() android.Module {
1108 module := &sdkLibraryImport{}
1109
Paul Duffin46a26a82020-04-07 19:27:04 +01001110 allScopeProperties, scopeToProperties := createPropertiesInstance()
1111 module.scopeProperties = scopeToProperties
1112 module.AddProperties(&module.properties, allScopeProperties)
Colin Cross79c7c262019-04-17 11:11:46 -07001113
Paul Duffin0bdcb272020-02-06 15:24:57 +00001114 android.InitPrebuiltModule(module, &[]string{""})
Paul Duffindd46f712020-02-10 13:37:10 +00001115 android.InitApexModule(module)
1116 android.InitSdkAwareModule(module)
Colin Cross79c7c262019-04-17 11:11:46 -07001117 InitJavaModule(module, android.HostAndDeviceSupported)
1118
1119 android.AddLoadHook(module, func(mctx android.LoadHookContext) { module.createInternalModules(mctx) })
1120 return module
1121}
1122
1123func (module *sdkLibraryImport) Prebuilt() *android.Prebuilt {
1124 return &module.prebuilt
1125}
1126
1127func (module *sdkLibraryImport) Name() string {
1128 return module.prebuilt.Name(module.ModuleBase.Name())
1129}
1130
1131func (module *sdkLibraryImport) createInternalModules(mctx android.LoadHookContext) {
Colin Cross79c7c262019-04-17 11:11:46 -07001132
Paul Duffin50061512020-01-21 16:31:05 +00001133 // If the build is configured to use prebuilts then force this to be preferred.
1134 if mctx.Config().UnbundledBuildUsePrebuiltSdks() {
1135 module.prebuilt.ForcePrefer()
1136 }
1137
Paul Duffin46a26a82020-04-07 19:27:04 +01001138 for apiScope, scopeProperties := range module.scopeProperties {
Paul Duffin56d44902020-01-31 13:36:25 +00001139 if len(scopeProperties.Jars) == 0 {
1140 continue
1141 }
1142
Paul Duffinbbb546b2020-04-09 00:07:11 +01001143 module.createJavaImportForStubs(mctx, apiScope, scopeProperties)
Paul Duffin3d1248c2020-04-09 00:10:17 +01001144
1145 module.createPrebuiltStubsSources(mctx, apiScope, scopeProperties)
Paul Duffin56d44902020-01-31 13:36:25 +00001146 }
Colin Cross79c7c262019-04-17 11:11:46 -07001147
1148 javaSdkLibraries := javaSdkLibraries(mctx.Config())
1149 javaSdkLibrariesLock.Lock()
1150 defer javaSdkLibrariesLock.Unlock()
1151 *javaSdkLibraries = append(*javaSdkLibraries, module.BaseModuleName())
1152}
1153
Paul Duffinbbb546b2020-04-09 00:07:11 +01001154func (module *sdkLibraryImport) createJavaImportForStubs(mctx android.LoadHookContext, apiScope *apiScope, scopeProperties *sdkLibraryScopeProperties) {
1155 // Creates a java import for the jar with ".stubs" suffix
1156 props := struct {
1157 Name *string
1158 Soc_specific *bool
1159 Device_specific *bool
1160 Product_specific *bool
1161 System_ext_specific *bool
1162 Sdk_version *string
1163 Libs []string
1164 Jars []string
1165 Prefer *bool
1166 }{}
1167 props.Name = proptools.StringPtr(apiScope.stubsModuleName(module.BaseModuleName()))
1168 props.Sdk_version = scopeProperties.Sdk_version
1169 // Prepend any of the libs from the legacy public properties to the libs for each of the
1170 // scopes to avoid having to duplicate them in each scope.
1171 props.Libs = append(module.properties.Libs, scopeProperties.Libs...)
1172 props.Jars = scopeProperties.Jars
1173 if module.SocSpecific() {
1174 props.Soc_specific = proptools.BoolPtr(true)
1175 } else if module.DeviceSpecific() {
1176 props.Device_specific = proptools.BoolPtr(true)
1177 } else if module.ProductSpecific() {
1178 props.Product_specific = proptools.BoolPtr(true)
1179 } else if module.SystemExtSpecific() {
1180 props.System_ext_specific = proptools.BoolPtr(true)
1181 }
1182 // If the build should use prebuilt sdks then set prefer to true on the stubs library.
1183 // That will cause the prebuilt version of the stubs to override the source version.
1184 if mctx.Config().UnbundledBuildUsePrebuiltSdks() {
1185 props.Prefer = proptools.BoolPtr(true)
1186 }
1187 mctx.CreateModule(ImportFactory, &props)
1188}
1189
Paul Duffin3d1248c2020-04-09 00:10:17 +01001190func (module *sdkLibraryImport) createPrebuiltStubsSources(mctx android.LoadHookContext, apiScope *apiScope, scopeProperties *sdkLibraryScopeProperties) {
1191 props := struct {
1192 Name *string
1193 Srcs []string
1194 }{}
1195 props.Name = proptools.StringPtr(apiScope.docsModuleName(module.BaseModuleName()))
1196 props.Srcs = scopeProperties.Stub_srcs
1197 mctx.CreateModule(PrebuiltStubsSourcesFactory, &props)
1198}
1199
Colin Cross79c7c262019-04-17 11:11:46 -07001200func (module *sdkLibraryImport) DepsMutator(ctx android.BottomUpMutatorContext) {
Paul Duffin46a26a82020-04-07 19:27:04 +01001201 for apiScope, scopeProperties := range module.scopeProperties {
Paul Duffin56d44902020-01-31 13:36:25 +00001202 if len(scopeProperties.Jars) == 0 {
1203 continue
1204 }
1205
1206 // Add dependencies to the prebuilt stubs library
1207 ctx.AddVariationDependencies(nil, apiScope.stubsTag, apiScope.stubsModuleName(module.BaseModuleName()))
1208 }
Colin Cross79c7c262019-04-17 11:11:46 -07001209}
1210
1211func (module *sdkLibraryImport) GenerateAndroidBuildActions(ctx android.ModuleContext) {
1212 // Record the paths to the prebuilt stubs library.
1213 ctx.VisitDirectDeps(func(to android.Module) {
1214 tag := ctx.OtherModuleDependencyTag(to)
1215
Paul Duffin56d44902020-01-31 13:36:25 +00001216 if lib, ok := to.(Dependency); ok {
1217 if scopeTag, ok := tag.(scopeDependencyTag); ok {
1218 apiScope := scopeTag.apiScope
1219 scopePaths := module.getScopePaths(apiScope)
1220 scopePaths.stubsHeaderPath = lib.HeaderJars()
1221 }
Colin Cross79c7c262019-04-17 11:11:46 -07001222 }
1223 })
1224}
1225
Paul Duffin56d44902020-01-31 13:36:25 +00001226func (module *sdkLibraryImport) sdkJars(
1227 ctx android.BaseModuleContext,
1228 sdkVersion sdkSpec) android.Paths {
1229
Paul Duffin50061512020-01-21 16:31:05 +00001230 // If a specific numeric version has been requested then use prebuilt versions of the sdk.
1231 if sdkVersion.version.isNumbered() {
1232 return PrebuiltJars(ctx, module.BaseModuleName(), sdkVersion)
1233 }
1234
Paul Duffin56d44902020-01-31 13:36:25 +00001235 var apiScope *apiScope
1236 switch sdkVersion.kind {
1237 case sdkSystem:
1238 apiScope = apiScopeSystem
1239 case sdkTest:
1240 apiScope = apiScopeTest
1241 default:
1242 apiScope = apiScopePublic
1243 }
1244
1245 paths := module.getScopePaths(apiScope)
1246 return paths.stubsHeaderPath
1247}
1248
Colin Cross79c7c262019-04-17 11:11:46 -07001249// to satisfy SdkLibraryDependency interface
Jiyong Park6a927c42020-01-21 02:03:43 +09001250func (module *sdkLibraryImport) SdkHeaderJars(ctx android.BaseModuleContext, sdkVersion sdkSpec) android.Paths {
Colin Cross79c7c262019-04-17 11:11:46 -07001251 // This module is just a wrapper for the prebuilt stubs.
Paul Duffin56d44902020-01-31 13:36:25 +00001252 return module.sdkJars(ctx, sdkVersion)
Colin Cross79c7c262019-04-17 11:11:46 -07001253}
1254
1255// to satisfy SdkLibraryDependency interface
Jiyong Park6a927c42020-01-21 02:03:43 +09001256func (module *sdkLibraryImport) SdkImplementationJars(ctx android.BaseModuleContext, sdkVersion sdkSpec) android.Paths {
Colin Cross79c7c262019-04-17 11:11:46 -07001257 // This module is just a wrapper for the stubs.
Paul Duffin56d44902020-01-31 13:36:25 +00001258 return module.sdkJars(ctx, sdkVersion)
Colin Cross79c7c262019-04-17 11:11:46 -07001259}
Jiyong Parke3833882020-02-17 17:28:10 +09001260
1261//
1262// java_sdk_library_xml
1263//
1264type sdkLibraryXml struct {
1265 android.ModuleBase
1266 android.DefaultableModuleBase
1267 android.ApexModuleBase
1268
1269 properties sdkLibraryXmlProperties
1270
1271 outputFilePath android.OutputPath
1272 installDirPath android.InstallPath
1273}
1274
1275type sdkLibraryXmlProperties struct {
1276 // canonical name of the lib
1277 Lib_name *string
1278}
1279
1280// java_sdk_library_xml builds the permission xml file for a java_sdk_library.
1281// Not to be used directly by users. java_sdk_library internally uses this.
1282func sdkLibraryXmlFactory() android.Module {
1283 module := &sdkLibraryXml{}
1284
1285 module.AddProperties(&module.properties)
1286
1287 android.InitApexModule(module)
1288 android.InitAndroidArchModule(module, android.DeviceSupported, android.MultilibCommon)
1289
1290 return module
1291}
1292
1293// from android.PrebuiltEtcModule
1294func (module *sdkLibraryXml) SubDir() string {
1295 return "permissions"
1296}
1297
1298// from android.PrebuiltEtcModule
1299func (module *sdkLibraryXml) OutputFile() android.OutputPath {
1300 return module.outputFilePath
1301}
1302
1303// from android.ApexModule
1304func (module *sdkLibraryXml) AvailableFor(what string) bool {
1305 return true
1306}
1307
1308func (module *sdkLibraryXml) DepsMutator(ctx android.BottomUpMutatorContext) {
1309 // do nothing
1310}
1311
1312// File path to the runtime implementation library
1313func (module *sdkLibraryXml) implPath() string {
1314 implName := proptools.String(module.properties.Lib_name)
1315 if apexName := module.ApexName(); apexName != "" {
1316 // TODO(b/146468504): ApexName() is only a soong module name, not apex name.
1317 // In most cases, this works fine. But when apex_name is set or override_apex is used
1318 // this can be wrong.
1319 return fmt.Sprintf("/apex/%s/javalib/%s.jar", apexName, implName)
1320 }
1321 partition := "system"
1322 if module.SocSpecific() {
1323 partition = "vendor"
1324 } else if module.DeviceSpecific() {
1325 partition = "odm"
1326 } else if module.ProductSpecific() {
1327 partition = "product"
1328 } else if module.SystemExtSpecific() {
1329 partition = "system_ext"
1330 }
1331 return "/" + partition + "/framework/" + implName + ".jar"
1332}
1333
1334func (module *sdkLibraryXml) GenerateAndroidBuildActions(ctx android.ModuleContext) {
1335 libName := proptools.String(module.properties.Lib_name)
1336 xmlContent := fmt.Sprintf(permissionsTemplate, libName, module.implPath())
1337
1338 module.outputFilePath = android.PathForModuleOut(ctx, libName+".xml").OutputPath
1339 rule := android.NewRuleBuilder()
1340 rule.Command().
1341 Text("/bin/bash -c \"echo -e '" + xmlContent + "'\" > ").
1342 Output(module.outputFilePath)
1343
1344 rule.Build(pctx, ctx, "java_sdk_xml", "Permission XML")
1345
1346 module.installDirPath = android.PathForModuleInstall(ctx, "etc", module.SubDir())
1347}
1348
1349func (module *sdkLibraryXml) AndroidMkEntries() []android.AndroidMkEntries {
1350 if !module.IsForPlatform() {
1351 return []android.AndroidMkEntries{android.AndroidMkEntries{
1352 Disabled: true,
1353 }}
1354 }
1355
1356 return []android.AndroidMkEntries{android.AndroidMkEntries{
1357 Class: "ETC",
1358 OutputFile: android.OptionalPathForPath(module.outputFilePath),
1359 ExtraEntries: []android.AndroidMkExtraEntriesFunc{
1360 func(entries *android.AndroidMkEntries) {
1361 entries.SetString("LOCAL_MODULE_TAGS", "optional")
1362 entries.SetString("LOCAL_MODULE_PATH", module.installDirPath.ToMakePath().String())
1363 entries.SetString("LOCAL_INSTALLED_MODULE_STEM", module.outputFilePath.Base())
1364 },
1365 },
1366 }}
1367}
Paul Duffindd46f712020-02-10 13:37:10 +00001368
1369type sdkLibrarySdkMemberType struct {
1370 android.SdkMemberTypeBase
1371}
1372
1373func (s *sdkLibrarySdkMemberType) AddDependencies(mctx android.BottomUpMutatorContext, dependencyTag blueprint.DependencyTag, names []string) {
1374 mctx.AddVariationDependencies(nil, dependencyTag, names...)
1375}
1376
1377func (s *sdkLibrarySdkMemberType) IsInstance(module android.Module) bool {
1378 _, ok := module.(*SdkLibrary)
1379 return ok
1380}
1381
1382func (s *sdkLibrarySdkMemberType) AddPrebuiltModule(ctx android.SdkMemberContext, member android.SdkMember) android.BpModule {
1383 return ctx.SnapshotBuilder().AddPrebuiltModule(member, "java_sdk_library_import")
1384}
1385
1386func (s *sdkLibrarySdkMemberType) CreateVariantPropertiesStruct() android.SdkMemberProperties {
1387 return &sdkLibrarySdkMemberProperties{}
1388}
1389
1390type sdkLibrarySdkMemberProperties struct {
1391 android.SdkMemberPropertiesBase
1392
1393 // Scope to per scope properties.
1394 Scopes map[*apiScope]scopeProperties
1395
1396 // Additional libraries that the exported stubs libraries depend upon.
1397 Libs []string
Paul Duffin3d1248c2020-04-09 00:10:17 +01001398
1399 // The Java stubs source files.
1400 Stub_srcs []string
Paul Duffindd46f712020-02-10 13:37:10 +00001401}
1402
1403type scopeProperties struct {
Paul Duffin1fd005d2020-04-09 01:08:11 +01001404 Jars android.Paths
1405 StubsSrcJar android.Path
1406 CurrentApiFile android.Path
1407 RemovedApiFile android.Path
1408 SdkVersion string
Paul Duffindd46f712020-02-10 13:37:10 +00001409}
1410
1411func (s *sdkLibrarySdkMemberProperties) PopulateFromVariant(ctx android.SdkMemberContext, variant android.Module) {
1412 sdk := variant.(*SdkLibrary)
1413
1414 s.Scopes = make(map[*apiScope]scopeProperties)
1415 for _, apiScope := range allApiScopes {
1416 paths := sdk.getScopePaths(apiScope)
1417 jars := paths.stubsImplPath
1418 if len(jars) > 0 {
1419 properties := scopeProperties{}
1420 properties.Jars = jars
1421 properties.SdkVersion = apiScope.sdkVersion
Paul Duffin3d1248c2020-04-09 00:10:17 +01001422 properties.StubsSrcJar = paths.stubsSrcJar
Paul Duffin1fd005d2020-04-09 01:08:11 +01001423 properties.CurrentApiFile = paths.currentApiFilePath
1424 properties.RemovedApiFile = paths.removedApiFilePath
Paul Duffindd46f712020-02-10 13:37:10 +00001425 s.Scopes[apiScope] = properties
1426 }
1427 }
1428
1429 s.Libs = sdk.properties.Libs
1430}
1431
1432func (s *sdkLibrarySdkMemberProperties) AddToPropertySet(ctx android.SdkMemberContext, propertySet android.BpPropertySet) {
1433 for _, apiScope := range allApiScopes {
1434 if properties, ok := s.Scopes[apiScope]; ok {
1435 scopeSet := propertySet.AddPropertySet(apiScope.name)
1436
Paul Duffin3d1248c2020-04-09 00:10:17 +01001437 scopeDir := filepath.Join("sdk_library", s.OsPrefix(), apiScope.name)
1438
Paul Duffindd46f712020-02-10 13:37:10 +00001439 var jars []string
1440 for _, p := range properties.Jars {
Paul Duffin3d1248c2020-04-09 00:10:17 +01001441 dest := filepath.Join(scopeDir, ctx.Name()+"-stubs.jar")
Paul Duffindd46f712020-02-10 13:37:10 +00001442 ctx.SnapshotBuilder().CopyToSnapshot(p, dest)
1443 jars = append(jars, dest)
1444 }
1445 scopeSet.AddProperty("jars", jars)
1446
Paul Duffin3d1248c2020-04-09 00:10:17 +01001447 // Merge the stubs source jar into the snapshot zip so that when it is unpacked
1448 // the source files are also unpacked.
1449 snapshotRelativeDir := filepath.Join(scopeDir, ctx.Name()+"_stub_sources")
1450 ctx.SnapshotBuilder().UnzipToSnapshot(properties.StubsSrcJar, snapshotRelativeDir)
1451 scopeSet.AddProperty("stub_srcs", []string{snapshotRelativeDir})
1452
Paul Duffin1fd005d2020-04-09 01:08:11 +01001453 if properties.CurrentApiFile != nil {
1454 currentApiSnapshotPath := filepath.Join(scopeDir, ctx.Name()+".txt")
1455 ctx.SnapshotBuilder().CopyToSnapshot(properties.CurrentApiFile, currentApiSnapshotPath)
1456 scopeSet.AddProperty("current_api", currentApiSnapshotPath)
1457 }
1458
1459 if properties.RemovedApiFile != nil {
1460 removedApiSnapshotPath := filepath.Join(scopeDir, ctx.Name()+"-removed.txt")
1461 ctx.SnapshotBuilder().CopyToSnapshot(properties.CurrentApiFile, removedApiSnapshotPath)
1462 scopeSet.AddProperty("removed_api", removedApiSnapshotPath)
1463 }
1464
Paul Duffindd46f712020-02-10 13:37:10 +00001465 if properties.SdkVersion != "" {
1466 scopeSet.AddProperty("sdk_version", properties.SdkVersion)
1467 }
1468 }
1469 }
1470
1471 if len(s.Libs) > 0 {
1472 propertySet.AddPropertyWithTag("libs", s.Libs, ctx.SnapshotBuilder().SdkMemberReferencePropertyTag(false))
1473 }
1474}