blob: 59ff1254bf273d3cecb22b91fbda2ac680d4c7b0 [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
Paul Duffinc8782502020-04-29 20:45:27 +010063
64 // Function for extracting appropriate path information from the dependency.
65 depInfoExtractor func(paths *scopePaths, dep android.Module) error
66}
67
68// Extract tag specific information from the dependency.
69func (tag scopeDependencyTag) extractDepInfo(ctx android.ModuleContext, dep android.Module, paths *scopePaths) {
70 err := tag.depInfoExtractor(paths, dep)
71 if err != nil {
72 ctx.ModuleErrorf("has an invalid {scopeDependencyTag: %s} dependency on module %s: %s", tag.name, ctx.OtherModuleName(dep), err.Error())
73 }
Paul Duffind1b3a922020-01-22 11:57:20 +000074}
75
76// Provides information about an api scope, e.g. public, system, test.
77type apiScope struct {
78 // The name of the api scope, e.g. public, system, test
79 name string
80
Paul Duffin97b53b82020-05-05 14:40:52 +010081 // The api scope that this scope extends.
82 extends *apiScope
83
Paul Duffin3375e352020-04-28 10:44:03 +010084 // The legacy enabled status for a specific scope can be dependent on other
85 // properties that have been specified on the library so it is provided by
86 // a function that can determine the status by examining those properties.
87 legacyEnabledStatus func(module *SdkLibrary) bool
88
89 // The default enabled status for non-legacy behavior, which is triggered by
90 // explicitly enabling at least one api scope.
91 defaultEnabledStatus bool
92
93 // Gets a pointer to the scope specific properties.
94 scopeSpecificProperties func(module *SdkLibrary) *ApiScopeProperties
95
Paul Duffin46a26a82020-04-07 19:27:04 +010096 // The name of the field in the dynamically created structure.
97 fieldName string
98
Paul Duffind1b3a922020-01-22 11:57:20 +000099 // The tag to use to depend on the stubs library module.
100 stubsTag scopeDependencyTag
101
Paul Duffinc8782502020-04-29 20:45:27 +0100102 // The tag to use to depend on the stubs source and API module.
103 stubsSourceAndApiTag scopeDependencyTag
Paul Duffind1b3a922020-01-22 11:57:20 +0000104
105 // The scope specific prefix to add to the api file base of "current.txt" or "removed.txt".
106 apiFilePrefix string
107
108 // The scope specific prefix to add to the sdk library module name to construct a scope specific
109 // module name.
110 moduleSuffix string
111
Paul Duffind1b3a922020-01-22 11:57:20 +0000112 // SDK version that the stubs library is built against. Note that this is always
113 // *current. Older stubs library built with a numbered SDK version is created from
114 // the prebuilt jar.
115 sdkVersion string
Paul Duffin1fb487d2020-04-07 18:50:10 +0100116
117 // Extra arguments to pass to droidstubs for this scope.
118 droidstubsArgs []string
Anton Hansson6478ac12020-05-02 11:19:36 +0100119
120 // Whether the api scope can be treated as unstable, and should skip compat checks.
121 unstable bool
Paul Duffind1b3a922020-01-22 11:57:20 +0000122}
123
124// Initialize a scope, creating and adding appropriate dependency tags
125func initApiScope(scope *apiScope) *apiScope {
Paul Duffinc8782502020-04-29 20:45:27 +0100126 name := scope.name
127 scope.fieldName = proptools.FieldNameForProperty(name)
Paul Duffind1b3a922020-01-22 11:57:20 +0000128 scope.stubsTag = scopeDependencyTag{
Paul Duffinc8782502020-04-29 20:45:27 +0100129 name: name + "-stubs",
130 apiScope: scope,
131 depInfoExtractor: (*scopePaths).extractStubsLibraryInfoFromDependency,
Paul Duffind1b3a922020-01-22 11:57:20 +0000132 }
Paul Duffinc8782502020-04-29 20:45:27 +0100133 scope.stubsSourceAndApiTag = scopeDependencyTag{
134 name: name + "-stubs-source-and-api",
135 apiScope: scope,
136 depInfoExtractor: (*scopePaths).extractStubsSourceAndApiInfoFromApiStubsProvider,
Paul Duffind1b3a922020-01-22 11:57:20 +0000137 }
138 return scope
139}
140
141func (scope *apiScope) stubsModuleName(baseName string) string {
142 return baseName + sdkStubsLibrarySuffix + scope.moduleSuffix
143}
144
Paul Duffinc8782502020-04-29 20:45:27 +0100145func (scope *apiScope) stubsSourceModuleName(baseName string) string {
Paul Duffin91b883d2020-02-11 13:05:28 +0000146 return baseName + sdkStubsSourceSuffix + scope.moduleSuffix
Paul Duffind1b3a922020-01-22 11:57:20 +0000147}
148
Paul Duffin3375e352020-04-28 10:44:03 +0100149func (scope *apiScope) String() string {
150 return scope.name
151}
152
Paul Duffind1b3a922020-01-22 11:57:20 +0000153type apiScopes []*apiScope
154
155func (scopes apiScopes) Strings(accessor func(*apiScope) string) []string {
156 var list []string
157 for _, scope := range scopes {
158 list = append(list, accessor(scope))
159 }
160 return list
161}
162
Jiyong Parkc678ad32018-04-10 13:07:10 +0900163var (
Paul Duffind1b3a922020-01-22 11:57:20 +0000164 apiScopePublic = initApiScope(&apiScope{
Paul Duffin3375e352020-04-28 10:44:03 +0100165 name: "public",
166
167 // Public scope is enabled by default for both legacy and non-legacy modes.
168 legacyEnabledStatus: func(module *SdkLibrary) bool {
169 return true
170 },
171 defaultEnabledStatus: true,
172
173 scopeSpecificProperties: func(module *SdkLibrary) *ApiScopeProperties {
174 return &module.sdkLibraryProperties.Public
175 },
Paul Duffind1b3a922020-01-22 11:57:20 +0000176 sdkVersion: "current",
177 })
178 apiScopeSystem = initApiScope(&apiScope{
Paul Duffin3375e352020-04-28 10:44:03 +0100179 name: "system",
180 extends: apiScopePublic,
181 legacyEnabledStatus: (*SdkLibrary).generateTestAndSystemScopesByDefault,
182 scopeSpecificProperties: func(module *SdkLibrary) *ApiScopeProperties {
183 return &module.sdkLibraryProperties.System
184 },
Anton Hansson6affb1f2020-04-28 16:47:41 +0100185 apiFilePrefix: "system-",
186 moduleSuffix: sdkSystemApiSuffix,
187 sdkVersion: "system_current",
Paul Duffin0d543642020-04-29 22:18:41 +0100188 droidstubsArgs: []string{"-showAnnotation android.annotation.SystemApi\\(client=android.annotation.SystemApi.Client.PRIVILEGED_APPS\\)"},
Paul Duffind1b3a922020-01-22 11:57:20 +0000189 })
190 apiScopeTest = initApiScope(&apiScope{
Paul Duffin3375e352020-04-28 10:44:03 +0100191 name: "test",
192 extends: apiScopePublic,
193 legacyEnabledStatus: (*SdkLibrary).generateTestAndSystemScopesByDefault,
194 scopeSpecificProperties: func(module *SdkLibrary) *ApiScopeProperties {
195 return &module.sdkLibraryProperties.Test
196 },
Anton Hansson6affb1f2020-04-28 16:47:41 +0100197 apiFilePrefix: "test-",
198 moduleSuffix: sdkTestApiSuffix,
199 sdkVersion: "test_current",
200 droidstubsArgs: []string{"-showAnnotation android.annotation.TestApi"},
Anton Hansson6478ac12020-05-02 11:19:36 +0100201 unstable: true,
Paul Duffind1b3a922020-01-22 11:57:20 +0000202 })
Paul Duffin8f265b92020-04-28 14:13:56 +0100203 apiScopeModuleLib = initApiScope(&apiScope{
204 name: "module_lib",
205 extends: apiScopeSystem,
206 // Module_lib scope is disabled by default in legacy mode.
207 //
208 // Enabling this would break existing usages.
209 legacyEnabledStatus: func(module *SdkLibrary) bool {
210 return false
211 },
212 scopeSpecificProperties: func(module *SdkLibrary) *ApiScopeProperties {
213 return &module.sdkLibraryProperties.Module_lib
214 },
215 apiFilePrefix: "module-lib-",
216 moduleSuffix: ".module_lib",
217 sdkVersion: "module_current",
218 droidstubsArgs: []string{
219 "--show-annotation android.annotation.SystemApi\\(client=android.annotation.SystemApi.Client.MODULE_LIBRARIES\\)",
220 },
221 })
Paul Duffind1b3a922020-01-22 11:57:20 +0000222 allApiScopes = apiScopes{
223 apiScopePublic,
224 apiScopeSystem,
225 apiScopeTest,
Paul Duffin8f265b92020-04-28 14:13:56 +0100226 apiScopeModuleLib,
Paul Duffind1b3a922020-01-22 11:57:20 +0000227 }
Jiyong Parkc678ad32018-04-10 13:07:10 +0900228)
229
Jiyong Park82484c02018-04-23 21:41:26 +0900230var (
231 javaSdkLibrariesLock sync.Mutex
232)
233
Jiyong Parkc678ad32018-04-10 13:07:10 +0900234// TODO: these are big features that are currently missing
Jiyong Park1be96912018-05-28 18:02:19 +0900235// 1) disallowing linking to the runtime shared lib
236// 2) HTML generation
Jiyong Parkc678ad32018-04-10 13:07:10 +0900237
238func init() {
Paul Duffin43dc1cc2019-12-19 11:18:54 +0000239 RegisterSdkLibraryBuildComponents(android.InitRegistrationContext)
Jiyong Parkc678ad32018-04-10 13:07:10 +0900240
Jiyong Park82484c02018-04-23 21:41:26 +0900241 android.RegisterMakeVarsProvider(pctx, func(ctx android.MakeVarsContext) {
242 javaSdkLibraries := javaSdkLibraries(ctx.Config())
243 sort.Strings(*javaSdkLibraries)
244 ctx.Strict("JAVA_SDK_LIBRARIES", strings.Join(*javaSdkLibraries, " "))
245 })
Paul Duffindd46f712020-02-10 13:37:10 +0000246
247 // Register sdk member types.
248 android.RegisterSdkMemberType(&sdkLibrarySdkMemberType{
249 android.SdkMemberTypeBase{
250 PropertyName: "java_sdk_libs",
251 SupportsSdk: true,
252 },
253 })
Jiyong Parkc678ad32018-04-10 13:07:10 +0900254}
255
Paul Duffin43dc1cc2019-12-19 11:18:54 +0000256func RegisterSdkLibraryBuildComponents(ctx android.RegistrationContext) {
257 ctx.RegisterModuleType("java_sdk_library", SdkLibraryFactory)
258 ctx.RegisterModuleType("java_sdk_library_import", sdkLibraryImportFactory)
259}
260
Paul Duffin3375e352020-04-28 10:44:03 +0100261// Properties associated with each api scope.
262type ApiScopeProperties struct {
263 // Indicates whether the api surface is generated.
264 //
265 // If this is set for any scope then all scopes must explicitly specify if they
266 // are enabled. This is to prevent new usages from depending on legacy behavior.
267 //
268 // Otherwise, if this is not set for any scope then the default behavior is
269 // scope specific so please refer to the scope specific property documentation.
270 Enabled *bool
271}
272
Jiyong Parkc678ad32018-04-10 13:07:10 +0900273type sdkLibraryProperties struct {
Paul Duffin4911a892020-04-29 23:35:13 +0100274 // Visibility for stubs library modules. If not specified then defaults to the
275 // visibility property.
276 Stubs_library_visibility []string
277
278 // Visibility for stubs source modules. If not specified then defaults to the
279 // visibility property.
280 Stubs_source_visibility []string
281
Sundong Ahnf043cf62018-06-25 16:04:37 +0900282 // List of Java libraries that will be in the classpath when building stubs
283 Stub_only_libs []string `android:"arch_variant"`
284
Paul Duffin7a586d32019-12-30 17:09:34 +0000285 // list of package names that will be documented and publicized as API.
286 // This allows the API to be restricted to a subset of the source files provided.
287 // If this is unspecified then all the source files will be treated as being part
288 // of the API.
Jiyong Parkc678ad32018-04-10 13:07:10 +0900289 Api_packages []string
290
Jiyong Park5a2c9d72018-05-01 22:25:41 +0900291 // list of package names that must be hidden from the API
292 Hidden_api_packages []string
293
Paul Duffin749f98f2019-12-30 17:23:46 +0000294 // the relative path to the directory containing the api specification files.
295 // Defaults to "api".
296 Api_dir *string
297
Paul Duffin43db9be2019-12-30 17:35:49 +0000298 // If set to true there is no runtime library.
299 Api_only *bool
300
Paul Duffin11512472019-02-11 15:55:17 +0000301 // local files that are used within user customized droiddoc options.
302 Droiddoc_option_files []string
303
304 // additional droiddoc options
305 // Available variables for substitution:
306 //
307 // $(location <label>): the path to the droiddoc_option_files with name <label>
Sundong Ahndd567f92018-07-31 17:19:11 +0900308 Droiddoc_options []string
309
Sundong Ahn054b19a2018-10-19 13:46:09 +0900310 // a list of top-level directories containing files to merge qualifier annotations
311 // (i.e. those intended to be included in the stubs written) from.
312 Merge_annotations_dirs []string
313
314 // a list of top-level directories containing Java stub files to merge show/hide annotations from.
315 Merge_inclusion_annotations_dirs []string
316
317 // If set to true, the path of dist files is apistubs/core. Defaults to false.
318 Core_lib *bool
319
Sundong Ahn80a87b32019-05-13 15:02:50 +0900320 // don't create dist rules.
321 No_dist *bool `blueprint:"mutated"`
322
Paul Duffin3375e352020-04-28 10:44:03 +0100323 // indicates whether system and test apis should be generated.
324 Generate_system_and_test_apis bool `blueprint:"mutated"`
325
326 // The properties specific to the public api scope
327 //
328 // Unless explicitly specified by using public.enabled the public api scope is
329 // enabled by default in both legacy and non-legacy mode.
330 Public ApiScopeProperties
331
332 // The properties specific to the system api scope
333 //
334 // In legacy mode the system api scope is enabled by default when sdk_version
335 // is set to something other than "none".
336 //
337 // In non-legacy mode the system api scope is disabled by default.
338 System ApiScopeProperties
339
340 // The properties specific to the test api scope
341 //
342 // In legacy mode the test api scope is enabled by default when sdk_version
343 // is set to something other than "none".
344 //
345 // In non-legacy mode the test api scope is disabled by default.
346 Test ApiScopeProperties
Paul Duffin37e0b772019-12-30 17:20:10 +0000347
Paul Duffin8f265b92020-04-28 14:13:56 +0100348 // The properties specific to the module_lib api scope
349 //
350 // Unless explicitly specified by using test.enabled the module_lib api scope is
351 // disabled by default.
352 Module_lib ApiScopeProperties
353
Jiyong Parkc678ad32018-04-10 13:07:10 +0900354 // TODO: determines whether to create HTML doc or not
355 //Html_doc *bool
356}
357
Paul Duffind1b3a922020-01-22 11:57:20 +0000358type scopePaths struct {
Paul Duffin1fd005d2020-04-09 01:08:11 +0100359 stubsHeaderPath android.Paths
360 stubsImplPath android.Paths
361 currentApiFilePath android.Path
362 removedApiFilePath android.Path
363 stubsSrcJar android.Path
Paul Duffind1b3a922020-01-22 11:57:20 +0000364}
365
Paul Duffinc8782502020-04-29 20:45:27 +0100366func (paths *scopePaths) extractStubsLibraryInfoFromDependency(dep android.Module) error {
367 if lib, ok := dep.(Dependency); ok {
368 paths.stubsHeaderPath = lib.HeaderJars()
369 paths.stubsImplPath = lib.ImplementationJars()
370 return nil
371 } else {
372 return fmt.Errorf("expected module that implements Dependency, e.g. java_library")
373 }
374}
375
376func (paths *scopePaths) extractStubsSourceAndApiInfoFromApiStubsProvider(dep android.Module) error {
377 if provider, ok := dep.(ApiStubsProvider); ok {
378 paths.currentApiFilePath = provider.ApiFilePath()
379 paths.removedApiFilePath = provider.RemovedApiFilePath()
380 paths.stubsSrcJar = provider.StubsSrcJar()
381 return nil
382 } else {
383 return fmt.Errorf("expected module that implements ApiStubsProvider, e.g. droidstubs")
384 }
385}
386
Paul Duffin56d44902020-01-31 13:36:25 +0000387// Common code between sdk library and sdk library import
388type commonToSdkLibraryAndImport struct {
389 scopePaths map[*apiScope]*scopePaths
390}
391
392func (c *commonToSdkLibraryAndImport) getScopePaths(scope *apiScope) *scopePaths {
393 if c.scopePaths == nil {
394 c.scopePaths = make(map[*apiScope]*scopePaths)
395 }
396 paths := c.scopePaths[scope]
397 if paths == nil {
398 paths = &scopePaths{}
399 c.scopePaths[scope] = paths
400 }
401
402 return paths
403}
404
Inseob Kimc0907f12019-02-08 21:00:45 +0900405type SdkLibrary struct {
Sundong Ahn054b19a2018-10-19 13:46:09 +0900406 Library
Jiyong Parkc678ad32018-04-10 13:07:10 +0900407
Sundong Ahn054b19a2018-10-19 13:46:09 +0900408 sdkLibraryProperties sdkLibraryProperties
Jiyong Parkc678ad32018-04-10 13:07:10 +0900409
Paul Duffin3375e352020-04-28 10:44:03 +0100410 // Map from api scope to the scope specific property structure.
411 scopeToProperties map[*apiScope]*ApiScopeProperties
412
Paul Duffin56d44902020-01-31 13:36:25 +0000413 commonToSdkLibraryAndImport
Jiyong Parkc678ad32018-04-10 13:07:10 +0900414}
415
Inseob Kimc0907f12019-02-08 21:00:45 +0900416var _ Dependency = (*SdkLibrary)(nil)
417var _ SdkLibraryDependency = (*SdkLibrary)(nil)
Colin Cross897d2ed2019-02-11 14:03:51 -0800418
Paul Duffin3375e352020-04-28 10:44:03 +0100419func (module *SdkLibrary) generateTestAndSystemScopesByDefault() bool {
420 return module.sdkLibraryProperties.Generate_system_and_test_apis
421}
422
423func (module *SdkLibrary) getGeneratedApiScopes(ctx android.EarlyModuleContext) apiScopes {
424 // Check to see if any scopes have been explicitly enabled. If any have then all
425 // must be.
426 anyScopesExplicitlyEnabled := false
427 for _, scope := range allApiScopes {
428 scopeProperties := module.scopeToProperties[scope]
429 if scopeProperties.Enabled != nil {
430 anyScopesExplicitlyEnabled = true
431 break
432 }
Paul Duffind1b3a922020-01-22 11:57:20 +0000433 }
Paul Duffin3375e352020-04-28 10:44:03 +0100434
435 var generatedScopes apiScopes
436 enabledScopes := make(map[*apiScope]struct{})
437 for _, scope := range allApiScopes {
438 scopeProperties := module.scopeToProperties[scope]
439 // If any scopes are explicitly enabled then ignore the legacy enabled status.
440 // This is to ensure that any new usages of this module type do not rely on legacy
441 // behaviour.
442 defaultEnabledStatus := false
443 if anyScopesExplicitlyEnabled {
444 defaultEnabledStatus = scope.defaultEnabledStatus
445 } else {
446 defaultEnabledStatus = scope.legacyEnabledStatus(module)
447 }
448 enabled := proptools.BoolDefault(scopeProperties.Enabled, defaultEnabledStatus)
449 if enabled {
450 enabledScopes[scope] = struct{}{}
451 generatedScopes = append(generatedScopes, scope)
452 }
453 }
454
455 // Now check to make sure that any scope that is extended by an enabled scope is also
456 // enabled.
457 for _, scope := range allApiScopes {
458 if _, ok := enabledScopes[scope]; ok {
459 extends := scope.extends
460 if extends != nil {
461 if _, ok := enabledScopes[extends]; !ok {
462 ctx.ModuleErrorf("enabled api scope %q depends on disabled scope %q", scope, extends)
463 }
464 }
465 }
466 }
467
468 return generatedScopes
Paul Duffind1b3a922020-01-22 11:57:20 +0000469}
470
Paul Duffine74ac732020-02-06 13:51:46 +0000471var xmlPermissionsFileTag = dependencyTag{name: "xml-permissions-file"}
472
Jiyong Parke3833882020-02-17 17:28:10 +0900473func IsXmlPermissionsFileDepTag(depTag blueprint.DependencyTag) bool {
474 if dt, ok := depTag.(dependencyTag); ok {
475 return dt == xmlPermissionsFileTag
476 }
477 return false
478}
479
Inseob Kimc0907f12019-02-08 21:00:45 +0900480func (module *SdkLibrary) DepsMutator(ctx android.BottomUpMutatorContext) {
Paul Duffin3375e352020-04-28 10:44:03 +0100481 for _, apiScope := range module.getGeneratedApiScopes(ctx) {
Paul Duffind1b3a922020-01-22 11:57:20 +0000482 // Add dependencies to the stubs library
Paul Duffin50061512020-01-21 16:31:05 +0000483 ctx.AddVariationDependencies(nil, apiScope.stubsTag, module.stubsName(apiScope))
Paul Duffind1b3a922020-01-22 11:57:20 +0000484
Paul Duffinc8782502020-04-29 20:45:27 +0100485 // And the stubs source and api files
486 ctx.AddVariationDependencies(nil, apiScope.stubsSourceAndApiTag, module.stubsSourceName(apiScope))
Sundong Ahn054b19a2018-10-19 13:46:09 +0900487 }
488
Paul Duffine74ac732020-02-06 13:51:46 +0000489 if !proptools.Bool(module.sdkLibraryProperties.Api_only) {
490 // Add dependency to the rule for generating the xml permissions file
Jiyong Parke3833882020-02-17 17:28:10 +0900491 ctx.AddDependency(module, xmlPermissionsFileTag, module.xmlFileName())
Paul Duffine74ac732020-02-06 13:51:46 +0000492 }
493
Sundong Ahn054b19a2018-10-19 13:46:09 +0900494 module.Library.deps(ctx)
Jiyong Parkc678ad32018-04-10 13:07:10 +0900495}
496
Inseob Kimc0907f12019-02-08 21:00:45 +0900497func (module *SdkLibrary) GenerateAndroidBuildActions(ctx android.ModuleContext) {
Paul Duffin43db9be2019-12-30 17:35:49 +0000498 // Don't build an implementation library if this is api only.
499 if !proptools.Bool(module.sdkLibraryProperties.Api_only) {
500 module.Library.GenerateAndroidBuildActions(ctx)
501 }
Sundong Ahn054b19a2018-10-19 13:46:09 +0900502
Sundong Ahn57368eb2018-07-06 11:20:23 +0900503 // Record the paths to the header jars of the library (stubs and impl).
Paul Duffind1b3a922020-01-22 11:57:20 +0000504 // When this java_sdk_library is depended upon from others via "libs" property,
Jiyong Parkc678ad32018-04-10 13:07:10 +0900505 // the recorded paths will be returned depending on the link type of the caller.
506 ctx.VisitDirectDeps(func(to android.Module) {
Jiyong Parkc678ad32018-04-10 13:07:10 +0900507 tag := ctx.OtherModuleDependencyTag(to)
508
Paul Duffinc8782502020-04-29 20:45:27 +0100509 // Extract information from any of the scope specific dependencies.
510 if scopeTag, ok := tag.(scopeDependencyTag); ok {
511 apiScope := scopeTag.apiScope
512 scopePaths := module.getScopePaths(apiScope)
513
514 // Extract information from the dependency. The exact information extracted
515 // is determined by the nature of the dependency which is determined by the tag.
516 scopeTag.extractDepInfo(ctx, to, scopePaths)
Sundong Ahn20e998b2018-07-24 11:19:26 +0900517 }
Jiyong Parkc678ad32018-04-10 13:07:10 +0900518 })
519}
520
Jiyong Park0b0e1b92019-12-03 13:24:29 +0900521func (module *SdkLibrary) AndroidMkEntries() []android.AndroidMkEntries {
Paul Duffin43db9be2019-12-30 17:35:49 +0000522 if proptools.Bool(module.sdkLibraryProperties.Api_only) {
523 return nil
524 }
Jiyong Park0b0e1b92019-12-03 13:24:29 +0900525 entriesList := module.Library.AndroidMkEntries()
526 entries := &entriesList[0]
Jaewoong Jungb0c127c2019-08-29 14:56:03 -0700527 entries.Required = append(entries.Required, module.xmlFileName())
Jiyong Park0b0e1b92019-12-03 13:24:29 +0900528 return entriesList
Jiyong Park82484c02018-04-23 21:41:26 +0900529}
530
Paul Duffinc8782502020-04-29 20:45:27 +0100531// Name of the java_library module that compiles the stubs source.
Paul Duffind1b3a922020-01-22 11:57:20 +0000532func (module *SdkLibrary) stubsName(apiScope *apiScope) string {
533 return apiScope.stubsModuleName(module.BaseModuleName())
Jiyong Parkc678ad32018-04-10 13:07:10 +0900534}
535
Paul Duffinc8782502020-04-29 20:45:27 +0100536// // Name of the droidstubs module that generates the stubs source and
537// generates/checks the API.
538func (module *SdkLibrary) stubsSourceName(apiScope *apiScope) string {
539 return apiScope.stubsSourceModuleName(module.BaseModuleName())
Jiyong Parkc678ad32018-04-10 13:07:10 +0900540}
541
542// Module name of the runtime implementation library
Inseob Kimc0907f12019-02-08 21:00:45 +0900543func (module *SdkLibrary) implName() string {
Sundong Ahn054b19a2018-10-19 13:46:09 +0900544 return module.BaseModuleName()
Jiyong Parkc678ad32018-04-10 13:07:10 +0900545}
546
Jiyong Parkc678ad32018-04-10 13:07:10 +0900547// Module name of the XML file for the lib
Inseob Kimc0907f12019-02-08 21:00:45 +0900548func (module *SdkLibrary) xmlFileName() string {
Jiyong Parkc678ad32018-04-10 13:07:10 +0900549 return module.BaseModuleName() + sdkXmlFileSuffix
550}
551
Anton Hansson5fd5d242020-03-27 19:43:19 +0000552// The dist path of the stub artifacts
553func (module *SdkLibrary) apiDistPath(apiScope *apiScope) string {
554 if module.ModuleBase.Owner() != "" {
555 return path.Join("apistubs", module.ModuleBase.Owner(), apiScope.name)
556 } else if Bool(module.sdkLibraryProperties.Core_lib) {
557 return path.Join("apistubs", "core", apiScope.name)
558 } else {
559 return path.Join("apistubs", "android", apiScope.name)
560 }
561}
562
Paul Duffin12ceb462019-12-24 20:31:31 +0000563// Get the sdk version for use when compiling the stubs library.
Paul Duffin780c5f42020-05-12 15:52:55 +0100564func (module *SdkLibrary) sdkVersionForStubsLibrary(mctx android.EarlyModuleContext, apiScope *apiScope) string {
Paul Duffin12ceb462019-12-24 20:31:31 +0000565 sdkDep := decodeSdkDep(mctx, sdkContext(&module.Library))
566 if sdkDep.hasStandardLibs() {
567 // If building against a standard sdk then use the sdk version appropriate for the scope.
Paul Duffind1b3a922020-01-22 11:57:20 +0000568 return apiScope.sdkVersion
Paul Duffin12ceb462019-12-24 20:31:31 +0000569 } else {
570 // Otherwise, use no system module.
571 return "none"
572 }
573}
574
Paul Duffind1b3a922020-01-22 11:57:20 +0000575func (module *SdkLibrary) latestApiFilegroupName(apiScope *apiScope) string {
576 return ":" + module.BaseModuleName() + ".api." + apiScope.name + ".latest"
Jiyong Park58c518b2018-05-12 22:29:12 +0900577}
Jiyong Parkc678ad32018-04-10 13:07:10 +0900578
Paul Duffind1b3a922020-01-22 11:57:20 +0000579func (module *SdkLibrary) latestRemovedApiFilegroupName(apiScope *apiScope) string {
580 return ":" + module.BaseModuleName() + "-removed.api." + apiScope.name + ".latest"
Jiyong Parkc678ad32018-04-10 13:07:10 +0900581}
582
583// Creates a static java library that has API stubs
Paul Duffinf0229202020-04-29 16:47:28 +0100584func (module *SdkLibrary) createStubsLibrary(mctx android.DefaultableHookContext, apiScope *apiScope) {
Jiyong Parkc678ad32018-04-10 13:07:10 +0900585 props := struct {
Sundong Ahn0d7dff42019-12-04 12:53:44 +0900586 Name *string
Paul Duffin4911a892020-04-29 23:35:13 +0100587 Visibility []string
Sundong Ahn0d7dff42019-12-04 12:53:44 +0900588 Srcs []string
Paul Duffin367ab912019-12-23 19:40:36 +0000589 Installable *bool
Sundong Ahn0d7dff42019-12-04 12:53:44 +0900590 Sdk_version *string
Paul Duffin12ceb462019-12-24 20:31:31 +0000591 System_modules *string
Paul Duffinab8da5d2020-02-07 16:12:04 +0000592 Patch_module *string
Sundong Ahn0d7dff42019-12-04 12:53:44 +0900593 Libs []string
594 Soc_specific *bool
595 Device_specific *bool
596 Product_specific *bool
597 System_ext_specific *bool
598 Compile_dex *bool
Sundong Ahn0d7dff42019-12-04 12:53:44 +0900599 Java_version *string
600 Product_variables struct {
Jiyong Park82484c02018-04-23 21:41:26 +0900601 Pdk struct {
602 Enabled *bool
603 }
Jiyong Parkc678ad32018-04-10 13:07:10 +0900604 }
Sundong Ahn054b19a2018-10-19 13:46:09 +0900605 Openjdk9 struct {
606 Srcs []string
607 Javacflags []string
608 }
Anton Hansson5fd5d242020-03-27 19:43:19 +0000609 Dist struct {
610 Targets []string
611 Dest *string
612 Dir *string
613 Tag *string
614 }
Jiyong Parkc678ad32018-04-10 13:07:10 +0900615 }{}
616
Jiyong Parkdf130542018-04-27 16:29:21 +0900617 props.Name = proptools.StringPtr(module.stubsName(apiScope))
Paul Duffin4911a892020-04-29 23:35:13 +0100618
619 // If stubs_library_visibility is not set then the created module will use the
620 // visibility of this module.
621 visibility := module.sdkLibraryProperties.Stubs_library_visibility
622 props.Visibility = visibility
623
Jiyong Parkc678ad32018-04-10 13:07:10 +0900624 // sources are generated from the droiddoc
Paul Duffinc8782502020-04-29 20:45:27 +0100625 props.Srcs = []string{":" + module.stubsSourceName(apiScope)}
Paul Duffin12ceb462019-12-24 20:31:31 +0000626 sdkVersion := module.sdkVersionForStubsLibrary(mctx, apiScope)
Paul Duffin52d398a2019-06-11 12:31:14 +0100627 props.Sdk_version = proptools.StringPtr(sdkVersion)
Paul Duffin12ceb462019-12-24 20:31:31 +0000628 props.System_modules = module.Library.Module.deviceProperties.System_modules
Paul Duffinab8da5d2020-02-07 16:12:04 +0000629 props.Patch_module = module.Library.Module.properties.Patch_module
Paul Duffin367ab912019-12-23 19:40:36 +0000630 props.Installable = proptools.BoolPtr(false)
Sundong Ahn054b19a2018-10-19 13:46:09 +0900631 props.Libs = module.sdkLibraryProperties.Stub_only_libs
Jiyong Park82484c02018-04-23 21:41:26 +0900632 props.Product_variables.Pdk.Enabled = proptools.BoolPtr(false)
Sundong Ahn054b19a2018-10-19 13:46:09 +0900633 props.Openjdk9.Srcs = module.Library.Module.properties.Openjdk9.Srcs
634 props.Openjdk9.Javacflags = module.Library.Module.properties.Openjdk9.Javacflags
635 props.Java_version = module.Library.Module.properties.Java_version
636 if module.Library.Module.deviceProperties.Compile_dex != nil {
637 props.Compile_dex = module.Library.Module.deviceProperties.Compile_dex
Sundong Ahndd567f92018-07-31 17:19:11 +0900638 }
Jiyong Parkc678ad32018-04-10 13:07:10 +0900639
640 if module.SocSpecific() {
641 props.Soc_specific = proptools.BoolPtr(true)
642 } else if module.DeviceSpecific() {
643 props.Device_specific = proptools.BoolPtr(true)
644 } else if module.ProductSpecific() {
645 props.Product_specific = proptools.BoolPtr(true)
Sundong Ahn0d7dff42019-12-04 12:53:44 +0900646 } else if module.SystemExtSpecific() {
647 props.System_ext_specific = proptools.BoolPtr(true)
Jiyong Parkc678ad32018-04-10 13:07:10 +0900648 }
Anton Hansson5fd5d242020-03-27 19:43:19 +0000649 // Dist the class jar artifact for sdk builds.
650 if !Bool(module.sdkLibraryProperties.No_dist) {
651 props.Dist.Targets = []string{"sdk", "win_sdk"}
652 props.Dist.Dest = proptools.StringPtr(fmt.Sprintf("%v.jar", module.BaseModuleName()))
653 props.Dist.Dir = proptools.StringPtr(module.apiDistPath(apiScope))
654 props.Dist.Tag = proptools.StringPtr(".jar")
655 }
Jiyong Parkc678ad32018-04-10 13:07:10 +0900656
Colin Cross84dfc3d2019-09-25 11:33:01 -0700657 mctx.CreateModule(LibraryFactory, &props)
Jiyong Parkc678ad32018-04-10 13:07:10 +0900658}
659
Paul Duffin6d0886e2020-04-07 18:49:53 +0100660// Creates a droidstubs module that creates stubs source files from the given full source
Paul Duffinc8782502020-04-29 20:45:27 +0100661// files and also updates and checks the API specification files.
662func (module *SdkLibrary) createStubsSourcesAndApi(mctx android.DefaultableHookContext, apiScope *apiScope) {
Jiyong Parkc678ad32018-04-10 13:07:10 +0900663 props := struct {
Sundong Ahn054b19a2018-10-19 13:46:09 +0900664 Name *string
Paul Duffin4911a892020-04-29 23:35:13 +0100665 Visibility []string
Sundong Ahn054b19a2018-10-19 13:46:09 +0900666 Srcs []string
667 Installable *bool
Paul Duffin52d398a2019-06-11 12:31:14 +0100668 Sdk_version *string
Paul Duffin12ceb462019-12-24 20:31:31 +0000669 System_modules *string
Sundong Ahn054b19a2018-10-19 13:46:09 +0900670 Libs []string
Paul Duffin11512472019-02-11 15:55:17 +0000671 Arg_files []string
Sundong Ahn054b19a2018-10-19 13:46:09 +0900672 Args *string
Sundong Ahn054b19a2018-10-19 13:46:09 +0900673 Java_version *string
674 Merge_annotations_dirs []string
675 Merge_inclusion_annotations_dirs []string
676 Check_api struct {
Inseob Kim38449af2019-02-28 14:24:05 +0900677 Current ApiToCheck
678 Last_released ApiToCheck
679 Ignore_missing_latest_api *bool
Jiyong Park58c518b2018-05-12 22:29:12 +0900680 }
Sundong Ahn1b92c822018-05-29 11:35:17 +0900681 Aidl struct {
682 Include_dirs []string
683 Local_include_dirs []string
684 }
Anton Hansson5fd5d242020-03-27 19:43:19 +0000685 Dist struct {
686 Targets []string
687 Dest *string
688 Dir *string
689 }
Jiyong Parkc678ad32018-04-10 13:07:10 +0900690 }{}
691
Paul Duffin7b78b4d2020-04-28 14:08:32 +0100692 // The stubs source processing uses the same compile time classpath when extracting the
693 // API from the implementation library as it does when compiling it. i.e. the same
694 // * sdk version
695 // * system_modules
696 // * libs (static_libs/libs)
Paul Duffin250e6192019-06-07 10:44:37 +0100697
Paul Duffinc8782502020-04-29 20:45:27 +0100698 props.Name = proptools.StringPtr(module.stubsSourceName(apiScope))
Paul Duffin4911a892020-04-29 23:35:13 +0100699
700 // If stubs_source_visibility is not set then the created module will use the
701 // visibility of this module.
702 visibility := module.sdkLibraryProperties.Stubs_source_visibility
703 props.Visibility = visibility
704
Sundong Ahn054b19a2018-10-19 13:46:09 +0900705 props.Srcs = append(props.Srcs, module.Library.Module.properties.Srcs...)
Paul Duffin7b78b4d2020-04-28 14:08:32 +0100706 props.Sdk_version = module.Library.Module.deviceProperties.Sdk_version
Paul Duffin12ceb462019-12-24 20:31:31 +0000707 props.System_modules = module.Library.Module.deviceProperties.System_modules
Jiyong Parkc678ad32018-04-10 13:07:10 +0900708 props.Installable = proptools.BoolPtr(false)
Sundong Ahne6f0b052018-06-05 16:46:14 +0900709 // A droiddoc module has only one Libs property and doesn't distinguish between
710 // shared libs and static libs. So we need to add both of these libs to Libs property.
Sundong Ahn054b19a2018-10-19 13:46:09 +0900711 props.Libs = module.Library.Module.properties.Libs
712 props.Libs = append(props.Libs, module.Library.Module.properties.Static_libs...)
713 props.Aidl.Include_dirs = module.Library.Module.deviceProperties.Aidl.Include_dirs
714 props.Aidl.Local_include_dirs = module.Library.Module.deviceProperties.Aidl.Local_include_dirs
Sundong Ahn054b19a2018-10-19 13:46:09 +0900715 props.Java_version = module.Library.Module.properties.Java_version
Jiyong Parkc678ad32018-04-10 13:07:10 +0900716
Sundong Ahn054b19a2018-10-19 13:46:09 +0900717 props.Merge_annotations_dirs = module.sdkLibraryProperties.Merge_annotations_dirs
718 props.Merge_inclusion_annotations_dirs = module.sdkLibraryProperties.Merge_inclusion_annotations_dirs
719
Paul Duffin6d0886e2020-04-07 18:49:53 +0100720 droidstubsArgs := []string{}
Paul Duffin235ffff2019-12-24 10:41:30 +0000721 if len(module.sdkLibraryProperties.Api_packages) != 0 {
Paul Duffin6d0886e2020-04-07 18:49:53 +0100722 droidstubsArgs = append(droidstubsArgs, "--stub-packages "+strings.Join(module.sdkLibraryProperties.Api_packages, ":"))
Paul Duffin235ffff2019-12-24 10:41:30 +0000723 }
724 if len(module.sdkLibraryProperties.Hidden_api_packages) != 0 {
Paul Duffin6d0886e2020-04-07 18:49:53 +0100725 droidstubsArgs = append(droidstubsArgs,
Paul Duffin235ffff2019-12-24 10:41:30 +0000726 android.JoinWithPrefix(module.sdkLibraryProperties.Hidden_api_packages, " --hide-package "))
727 }
Paul Duffin6d0886e2020-04-07 18:49:53 +0100728 droidstubsArgs = append(droidstubsArgs, module.sdkLibraryProperties.Droiddoc_options...)
Paul Duffin235ffff2019-12-24 10:41:30 +0000729 disabledWarnings := []string{
730 "MissingPermission",
731 "BroadcastBehavior",
732 "HiddenSuperclass",
733 "DeprecationMismatch",
734 "UnavailableSymbol",
735 "SdkConstant",
736 "HiddenTypeParameter",
737 "Todo",
738 "Typo",
739 }
Paul Duffin6d0886e2020-04-07 18:49:53 +0100740 droidstubsArgs = append(droidstubsArgs, android.JoinWithPrefix(disabledWarnings, "--hide "))
Sundong Ahnfb2721f2018-09-17 13:23:09 +0900741
Paul Duffin1fb487d2020-04-07 18:50:10 +0100742 // Add in scope specific arguments.
743 droidstubsArgs = append(droidstubsArgs, apiScope.droidstubsArgs...)
Paul Duffin11512472019-02-11 15:55:17 +0000744 props.Arg_files = module.sdkLibraryProperties.Droiddoc_option_files
Paul Duffin6d0886e2020-04-07 18:49:53 +0100745 props.Args = proptools.StringPtr(strings.Join(droidstubsArgs, " "))
Jiyong Parkc678ad32018-04-10 13:07:10 +0900746
747 // List of APIs identified from the provided source files are created. They are later
748 // compared against to the not-yet-released (a.k.a current) list of APIs and to the
749 // last-released (a.k.a numbered) list of API.
Paul Duffind1b3a922020-01-22 11:57:20 +0000750 currentApiFileName := apiScope.apiFilePrefix + "current.txt"
751 removedApiFileName := apiScope.apiFilePrefix + "removed.txt"
Paul Duffin749f98f2019-12-30 17:23:46 +0000752 apiDir := module.getApiDir()
753 currentApiFileName = path.Join(apiDir, currentApiFileName)
754 removedApiFileName = path.Join(apiDir, removedApiFileName)
Jiyong Parkc678ad32018-04-10 13:07:10 +0900755
Jiyong Park58c518b2018-05-12 22:29:12 +0900756 // check against the not-yet-release API
757 props.Check_api.Current.Api_file = proptools.StringPtr(currentApiFileName)
758 props.Check_api.Current.Removed_api_file = proptools.StringPtr(removedApiFileName)
Jiyong Park58c518b2018-05-12 22:29:12 +0900759
Anton Hansson6478ac12020-05-02 11:19:36 +0100760 if !apiScope.unstable {
761 // check against the latest released API
762 props.Check_api.Last_released.Api_file = proptools.StringPtr(
763 module.latestApiFilegroupName(apiScope))
764 props.Check_api.Last_released.Removed_api_file = proptools.StringPtr(
765 module.latestRemovedApiFilegroupName(apiScope))
766 props.Check_api.Ignore_missing_latest_api = proptools.BoolPtr(true)
767 }
Jiyong Park58c518b2018-05-12 22:29:12 +0900768
Anton Hansson5fd5d242020-03-27 19:43:19 +0000769 // Dist the api txt artifact for sdk builds.
770 if !Bool(module.sdkLibraryProperties.No_dist) {
771 props.Dist.Targets = []string{"sdk", "win_sdk"}
772 props.Dist.Dest = proptools.StringPtr(fmt.Sprintf("%v.txt", module.BaseModuleName()))
773 props.Dist.Dir = proptools.StringPtr(path.Join(module.apiDistPath(apiScope), "api"))
774 }
775
Colin Cross84dfc3d2019-09-25 11:33:01 -0700776 mctx.CreateModule(DroidstubsFactory, &props)
Jiyong Parkc678ad32018-04-10 13:07:10 +0900777}
778
Jooyung Han5e9013b2020-03-10 06:23:13 +0900779func (module *SdkLibrary) DepIsInSameApex(mctx android.BaseModuleContext, dep android.Module) bool {
780 depTag := mctx.OtherModuleDependencyTag(dep)
781 if depTag == xmlPermissionsFileTag {
782 return true
783 }
784 return module.Library.DepIsInSameApex(mctx, dep)
785}
786
Jiyong Parkc678ad32018-04-10 13:07:10 +0900787// Creates the xml file that publicizes the runtime library
Paul Duffinf0229202020-04-29 16:47:28 +0100788func (module *SdkLibrary) createXmlFile(mctx android.DefaultableHookContext) {
Jiyong Parke3833882020-02-17 17:28:10 +0900789 props := struct {
Sundong Ahn0d7dff42019-12-04 12:53:44 +0900790 Name *string
Jiyong Parke3833882020-02-17 17:28:10 +0900791 Lib_name *string
Sundong Ahn0d7dff42019-12-04 12:53:44 +0900792 Soc_specific *bool
793 Device_specific *bool
794 Product_specific *bool
795 System_ext_specific *bool
Jooyung Han5e9013b2020-03-10 06:23:13 +0900796 Apex_available []string
Jiyong Parke3833882020-02-17 17:28:10 +0900797 }{
Jooyung Han5e9013b2020-03-10 06:23:13 +0900798 Name: proptools.StringPtr(module.xmlFileName()),
799 Lib_name: proptools.StringPtr(module.BaseModuleName()),
800 Apex_available: module.ApexProperties.Apex_available,
Jiyong Parkc678ad32018-04-10 13:07:10 +0900801 }
Jiyong Parke3833882020-02-17 17:28:10 +0900802
803 if module.SocSpecific() {
804 props.Soc_specific = proptools.BoolPtr(true)
805 } else if module.DeviceSpecific() {
806 props.Device_specific = proptools.BoolPtr(true)
807 } else if module.ProductSpecific() {
808 props.Product_specific = proptools.BoolPtr(true)
809 } else if module.SystemExtSpecific() {
810 props.System_ext_specific = proptools.BoolPtr(true)
811 }
812
813 mctx.CreateModule(sdkLibraryXmlFactory, &props)
Jiyong Parkc678ad32018-04-10 13:07:10 +0900814}
815
Paul Duffin50061512020-01-21 16:31:05 +0000816func PrebuiltJars(ctx android.BaseModuleContext, baseName string, s sdkSpec) android.Paths {
Jiyong Park6a927c42020-01-21 02:03:43 +0900817 var ver sdkVersion
818 var kind sdkKind
819 if s.usePrebuilt(ctx) {
820 ver = s.version
821 kind = s.kind
Jiyong Parkc678ad32018-04-10 13:07:10 +0900822 } else {
Jiyong Park6a927c42020-01-21 02:03:43 +0900823 // We don't have prebuilt SDK for the specific sdkVersion.
824 // Instead of breaking the build, fallback to use "system_current"
825 ver = sdkVersionCurrent
826 kind = sdkSystem
Sundong Ahn054b19a2018-10-19 13:46:09 +0900827 }
Jiyong Park6a927c42020-01-21 02:03:43 +0900828
829 dir := filepath.Join("prebuilts", "sdk", ver.String(), kind.String())
Paul Duffin50061512020-01-21 16:31:05 +0000830 jar := filepath.Join(dir, baseName+".jar")
Sundong Ahn054b19a2018-10-19 13:46:09 +0900831 jarPath := android.ExistentPathForSource(ctx, jar)
Sundong Ahnae418ac2019-02-28 15:01:28 +0900832 if !jarPath.Valid() {
Colin Cross07c88562020-01-07 09:34:44 -0800833 if ctx.Config().AllowMissingDependencies() {
834 return android.Paths{android.PathForSource(ctx, jar)}
835 } else {
Jiyong Park6a927c42020-01-21 02:03:43 +0900836 ctx.PropertyErrorf("sdk_library", "invalid sdk version %q, %q does not exist", s.raw, jar)
Colin Cross07c88562020-01-07 09:34:44 -0800837 }
Sundong Ahnae418ac2019-02-28 15:01:28 +0900838 return nil
839 }
Sundong Ahn054b19a2018-10-19 13:46:09 +0900840 return android.Paths{jarPath.Path()}
841}
842
Paul Duffind1b3a922020-01-22 11:57:20 +0000843func (module *SdkLibrary) sdkJars(
844 ctx android.BaseModuleContext,
845 sdkVersion sdkSpec,
846 headerJars bool) android.Paths {
847
Paul Duffin50061512020-01-21 16:31:05 +0000848 // If a specific numeric version has been requested then use prebuilt versions of the sdk.
849 if sdkVersion.version.isNumbered() {
850 return PrebuiltJars(ctx, module.BaseModuleName(), sdkVersion)
Sundong Ahn054b19a2018-10-19 13:46:09 +0900851 } else {
Paul Duffind1b3a922020-01-22 11:57:20 +0000852 if !sdkVersion.specified() {
853 if headerJars {
854 return module.Library.HeaderJars()
855 } else {
856 return module.Library.ImplementationJars()
857 }
858 }
Paul Duffin726d23c2020-01-22 16:30:37 +0000859 var apiScope *apiScope
Jiyong Park6a927c42020-01-21 02:03:43 +0900860 switch sdkVersion.kind {
861 case sdkSystem:
Paul Duffin726d23c2020-01-22 16:30:37 +0000862 apiScope = apiScopeSystem
863 case sdkTest:
864 apiScope = apiScopeTest
Jiyong Park6a927c42020-01-21 02:03:43 +0900865 case sdkPrivate:
Sundong Ahn054b19a2018-10-19 13:46:09 +0900866 return module.Library.HeaderJars()
Jiyong Park6a927c42020-01-21 02:03:43 +0900867 default:
Paul Duffin726d23c2020-01-22 16:30:37 +0000868 apiScope = apiScopePublic
Paul Duffind1b3a922020-01-22 11:57:20 +0000869 }
870
Paul Duffin726d23c2020-01-22 16:30:37 +0000871 paths := module.getScopePaths(apiScope)
Paul Duffind1b3a922020-01-22 11:57:20 +0000872 if headerJars {
873 return paths.stubsHeaderPath
874 } else {
875 return paths.stubsImplPath
Sundong Ahn054b19a2018-10-19 13:46:09 +0900876 }
Jiyong Parkc678ad32018-04-10 13:07:10 +0900877 }
878}
879
Sundong Ahn241cd372018-07-13 16:16:44 +0900880// to satisfy SdkLibraryDependency interface
Paul Duffind1b3a922020-01-22 11:57:20 +0000881func (module *SdkLibrary) SdkHeaderJars(ctx android.BaseModuleContext, sdkVersion sdkSpec) android.Paths {
882 return module.sdkJars(ctx, sdkVersion, true /*headerJars*/)
883}
884
885// to satisfy SdkLibraryDependency interface
Jiyong Park6a927c42020-01-21 02:03:43 +0900886func (module *SdkLibrary) SdkImplementationJars(ctx android.BaseModuleContext, sdkVersion sdkSpec) android.Paths {
Paul Duffind1b3a922020-01-22 11:57:20 +0000887 return module.sdkJars(ctx, sdkVersion, false /*headerJars*/)
Sundong Ahn241cd372018-07-13 16:16:44 +0900888}
889
Sundong Ahn80a87b32019-05-13 15:02:50 +0900890func (module *SdkLibrary) SetNoDist() {
891 module.sdkLibraryProperties.No_dist = proptools.BoolPtr(true)
892}
893
Colin Cross571cccf2019-02-04 11:22:08 -0800894var javaSdkLibrariesKey = android.NewOnceKey("javaSdkLibraries")
895
Jiyong Park82484c02018-04-23 21:41:26 +0900896func javaSdkLibraries(config android.Config) *[]string {
Colin Cross571cccf2019-02-04 11:22:08 -0800897 return config.Once(javaSdkLibrariesKey, func() interface{} {
Jiyong Park82484c02018-04-23 21:41:26 +0900898 return &[]string{}
899 }).(*[]string)
900}
901
Paul Duffin749f98f2019-12-30 17:23:46 +0000902func (module *SdkLibrary) getApiDir() string {
903 return proptools.StringDefault(module.sdkLibraryProperties.Api_dir, "api")
904}
905
Jiyong Parkc678ad32018-04-10 13:07:10 +0900906// For a java_sdk_library module, create internal modules for stubs, docs,
907// runtime libs and xml file. If requested, the stubs and docs are created twice
908// once for public API level and once for system API level
Paul Duffinf0229202020-04-29 16:47:28 +0100909func (module *SdkLibrary) CreateInternalModules(mctx android.DefaultableHookContext) {
910 // If the module has been disabled then don't create any child modules.
911 if !module.Enabled() {
912 return
913 }
914
Inseob Kim6e93ac92019-03-21 17:43:49 +0900915 if len(module.Library.Module.properties.Srcs) == 0 {
Inseob Kimc0907f12019-02-08 21:00:45 +0900916 mctx.PropertyErrorf("srcs", "java_sdk_library must specify srcs")
Jooyung Han58f26ab2019-12-18 15:34:32 +0900917 return
Inseob Kimc0907f12019-02-08 21:00:45 +0900918 }
919
Paul Duffin37e0b772019-12-30 17:20:10 +0000920 // If this builds against standard libraries (i.e. is not part of the core libraries)
921 // then assume it provides both system and test apis. Otherwise, assume it does not and
922 // also assume it does not contribute to the dist build.
923 sdkDep := decodeSdkDep(mctx, sdkContext(&module.Library))
924 hasSystemAndTestApis := sdkDep.hasStandardLibs()
Paul Duffin3375e352020-04-28 10:44:03 +0100925 module.sdkLibraryProperties.Generate_system_and_test_apis = hasSystemAndTestApis
Paul Duffin37e0b772019-12-30 17:20:10 +0000926 module.sdkLibraryProperties.No_dist = proptools.BoolPtr(!hasSystemAndTestApis)
927
Inseob Kim8098faa2019-03-18 10:19:51 +0900928 missing_current_api := false
929
Paul Duffin3375e352020-04-28 10:44:03 +0100930 generatedScopes := module.getGeneratedApiScopes(mctx)
Paul Duffind1b3a922020-01-22 11:57:20 +0000931
Paul Duffin749f98f2019-12-30 17:23:46 +0000932 apiDir := module.getApiDir()
Paul Duffin3375e352020-04-28 10:44:03 +0100933 for _, scope := range generatedScopes {
Inseob Kim8098faa2019-03-18 10:19:51 +0900934 for _, api := range []string{"current.txt", "removed.txt"} {
Paul Duffind1b3a922020-01-22 11:57:20 +0000935 path := path.Join(mctx.ModuleDir(), apiDir, scope.apiFilePrefix+api)
Inseob Kim8098faa2019-03-18 10:19:51 +0900936 p := android.ExistentPathForSource(mctx, path)
937 if !p.Valid() {
938 mctx.ModuleErrorf("Current api file %#v doesn't exist", path)
939 missing_current_api = true
940 }
941 }
942 }
943
944 if missing_current_api {
945 script := "build/soong/scripts/gen-java-current-api-files.sh"
946 p := android.ExistentPathForSource(mctx, script)
947
948 if !p.Valid() {
949 panic(fmt.Sprintf("script file %s doesn't exist", script))
950 }
951
952 mctx.ModuleErrorf("One or more current api files are missing. "+
953 "You can update them by:\n"+
Paul Duffin37e0b772019-12-30 17:20:10 +0000954 "%s %q %s && m update-api",
Paul Duffind1b3a922020-01-22 11:57:20 +0000955 script, filepath.Join(mctx.ModuleDir(), apiDir),
Paul Duffin3375e352020-04-28 10:44:03 +0100956 strings.Join(generatedScopes.Strings(func(s *apiScope) string { return s.apiFilePrefix }), " "))
Inseob Kim8098faa2019-03-18 10:19:51 +0900957 return
958 }
959
Paul Duffin3375e352020-04-28 10:44:03 +0100960 for _, scope := range generatedScopes {
Paul Duffind1b3a922020-01-22 11:57:20 +0000961 module.createStubsLibrary(mctx, scope)
Paul Duffinc8782502020-04-29 20:45:27 +0100962 module.createStubsSourcesAndApi(mctx, scope)
Inseob Kimc0907f12019-02-08 21:00:45 +0900963 }
964
Paul Duffin43db9be2019-12-30 17:35:49 +0000965 if !proptools.Bool(module.sdkLibraryProperties.Api_only) {
966 // for runtime
967 module.createXmlFile(mctx)
968
969 // record java_sdk_library modules so that they are exported to make
970 javaSdkLibraries := javaSdkLibraries(mctx.Config())
971 javaSdkLibrariesLock.Lock()
972 defer javaSdkLibrariesLock.Unlock()
973 *javaSdkLibraries = append(*javaSdkLibraries, module.BaseModuleName())
974 }
Inseob Kimc0907f12019-02-08 21:00:45 +0900975}
976
977func (module *SdkLibrary) InitSdkLibraryProperties() {
Sundong Ahn054b19a2018-10-19 13:46:09 +0900978 module.AddProperties(
979 &module.sdkLibraryProperties,
980 &module.Library.Module.properties,
981 &module.Library.Module.dexpreoptProperties,
982 &module.Library.Module.deviceProperties,
983 &module.Library.Module.protoProperties,
984 )
985
986 module.Library.Module.properties.Installable = proptools.BoolPtr(true)
987 module.Library.Module.deviceProperties.IsSDKLibrary = true
Inseob Kimc0907f12019-02-08 21:00:45 +0900988}
Sundong Ahn054b19a2018-10-19 13:46:09 +0900989
Jaewoong Jung4f158ee2019-07-11 10:05:35 -0700990// java_sdk_library is a special Java library that provides optional platform APIs to apps.
991// In practice, it can be viewed as a combination of several modules: 1) stubs library that clients
992// are linked against to, 2) droiddoc module that internally generates API stubs source files,
993// 3) the real runtime shared library that implements the APIs, and 4) XML file for adding
994// the runtime lib to the classpath at runtime if requested via <uses-library>.
Inseob Kimc0907f12019-02-08 21:00:45 +0900995func SdkLibraryFactory() android.Module {
996 module := &SdkLibrary{}
997 module.InitSdkLibraryProperties()
Jooyung Han58f26ab2019-12-18 15:34:32 +0900998 android.InitApexModule(module)
Sundong Ahn054b19a2018-10-19 13:46:09 +0900999 InitJavaModule(module, android.HostAndDeviceSupported)
Paul Duffin3375e352020-04-28 10:44:03 +01001000
1001 // Initialize the map from scope to scope specific properties.
1002 scopeToProperties := make(map[*apiScope]*ApiScopeProperties)
1003 for _, scope := range allApiScopes {
1004 scopeToProperties[scope] = scope.scopeSpecificProperties(module)
1005 }
1006 module.scopeToProperties = scopeToProperties
1007
Paul Duffin4911a892020-04-29 23:35:13 +01001008 // Add the properties containing visibility rules so that they are checked.
1009 android.AddVisibilityProperty(module, "stubs_library_visibility", &module.sdkLibraryProperties.Stubs_library_visibility)
1010 android.AddVisibilityProperty(module, "stubs_source_visibility", &module.sdkLibraryProperties.Stubs_source_visibility)
1011
Paul Duffinf0229202020-04-29 16:47:28 +01001012 module.SetDefaultableHook(func(ctx android.DefaultableHookContext) { module.CreateInternalModules(ctx) })
Jiyong Parkc678ad32018-04-10 13:07:10 +09001013 return module
1014}
Colin Cross79c7c262019-04-17 11:11:46 -07001015
1016//
1017// SDK library prebuilts
1018//
1019
Paul Duffin56d44902020-01-31 13:36:25 +00001020// Properties associated with each api scope.
1021type sdkLibraryScopeProperties struct {
Colin Cross79c7c262019-04-17 11:11:46 -07001022 Jars []string `android:"path"`
1023
1024 Sdk_version *string
1025
Colin Cross79c7c262019-04-17 11:11:46 -07001026 // List of shared java libs that this module has dependencies to
1027 Libs []string
Paul Duffin3d1248c2020-04-09 00:10:17 +01001028
Paul Duffinc8782502020-04-29 20:45:27 +01001029 // The stubs source.
Paul Duffin3d1248c2020-04-09 00:10:17 +01001030 Stub_srcs []string `android:"path"`
Paul Duffin1fd005d2020-04-09 01:08:11 +01001031
1032 // The current.txt
1033 Current_api string `android:"path"`
1034
1035 // The removed.txt
1036 Removed_api string `android:"path"`
Colin Cross79c7c262019-04-17 11:11:46 -07001037}
1038
Paul Duffin56d44902020-01-31 13:36:25 +00001039type sdkLibraryImportProperties struct {
Paul Duffinfcfd7912020-01-31 17:54:30 +00001040 // List of shared java libs, common to all scopes, that this module has
1041 // dependencies to
1042 Libs []string
Paul Duffin56d44902020-01-31 13:36:25 +00001043}
1044
Colin Cross79c7c262019-04-17 11:11:46 -07001045type sdkLibraryImport struct {
1046 android.ModuleBase
1047 android.DefaultableModuleBase
1048 prebuilt android.Prebuilt
Paul Duffindd46f712020-02-10 13:37:10 +00001049 android.ApexModuleBase
1050 android.SdkBase
Colin Cross79c7c262019-04-17 11:11:46 -07001051
1052 properties sdkLibraryImportProperties
1053
Paul Duffin46a26a82020-04-07 19:27:04 +01001054 // Map from api scope to the scope specific property structure.
1055 scopeProperties map[*apiScope]*sdkLibraryScopeProperties
1056
Paul Duffin56d44902020-01-31 13:36:25 +00001057 commonToSdkLibraryAndImport
Colin Cross79c7c262019-04-17 11:11:46 -07001058}
1059
1060var _ SdkLibraryDependency = (*sdkLibraryImport)(nil)
1061
Paul Duffin46a26a82020-04-07 19:27:04 +01001062// The type of a structure that contains a field of type sdkLibraryScopeProperties
1063// for each apiscope in allApiScopes, e.g. something like:
1064// struct {
1065// Public sdkLibraryScopeProperties
1066// System sdkLibraryScopeProperties
1067// ...
1068// }
1069var allScopeStructType = createAllScopePropertiesStructType()
1070
1071// Dynamically create a structure type for each apiscope in allApiScopes.
1072func createAllScopePropertiesStructType() reflect.Type {
1073 var fields []reflect.StructField
1074 for _, apiScope := range allApiScopes {
1075 field := reflect.StructField{
1076 Name: apiScope.fieldName,
1077 Type: reflect.TypeOf(sdkLibraryScopeProperties{}),
1078 }
1079 fields = append(fields, field)
1080 }
1081
1082 return reflect.StructOf(fields)
1083}
1084
1085// Create an instance of the scope specific structure type and return a map
1086// from apiscope to a pointer to each scope specific field.
1087func createPropertiesInstance() (interface{}, map[*apiScope]*sdkLibraryScopeProperties) {
1088 allScopePropertiesPtr := reflect.New(allScopeStructType)
1089 allScopePropertiesStruct := allScopePropertiesPtr.Elem()
1090 scopeProperties := make(map[*apiScope]*sdkLibraryScopeProperties)
1091
1092 for _, apiScope := range allApiScopes {
1093 field := allScopePropertiesStruct.FieldByName(apiScope.fieldName)
1094 scopeProperties[apiScope] = field.Addr().Interface().(*sdkLibraryScopeProperties)
1095 }
1096
1097 return allScopePropertiesPtr.Interface(), scopeProperties
1098}
1099
Jaewoong Jung4f158ee2019-07-11 10:05:35 -07001100// java_sdk_library_import imports a prebuilt java_sdk_library.
Colin Cross79c7c262019-04-17 11:11:46 -07001101func sdkLibraryImportFactory() android.Module {
1102 module := &sdkLibraryImport{}
1103
Paul Duffin46a26a82020-04-07 19:27:04 +01001104 allScopeProperties, scopeToProperties := createPropertiesInstance()
1105 module.scopeProperties = scopeToProperties
1106 module.AddProperties(&module.properties, allScopeProperties)
Colin Cross79c7c262019-04-17 11:11:46 -07001107
Paul Duffin0bdcb272020-02-06 15:24:57 +00001108 android.InitPrebuiltModule(module, &[]string{""})
Paul Duffindd46f712020-02-10 13:37:10 +00001109 android.InitApexModule(module)
1110 android.InitSdkAwareModule(module)
Colin Cross79c7c262019-04-17 11:11:46 -07001111 InitJavaModule(module, android.HostAndDeviceSupported)
1112
Paul Duffin6e7ecbf2020-05-08 15:01:19 +01001113 module.SetDefaultableHook(func(mctx android.DefaultableHookContext) { module.createInternalModules(mctx) })
Colin Cross79c7c262019-04-17 11:11:46 -07001114 return module
1115}
1116
1117func (module *sdkLibraryImport) Prebuilt() *android.Prebuilt {
1118 return &module.prebuilt
1119}
1120
1121func (module *sdkLibraryImport) Name() string {
1122 return module.prebuilt.Name(module.ModuleBase.Name())
1123}
1124
Paul Duffin6e7ecbf2020-05-08 15:01:19 +01001125func (module *sdkLibraryImport) createInternalModules(mctx android.DefaultableHookContext) {
Colin Cross79c7c262019-04-17 11:11:46 -07001126
Paul Duffin50061512020-01-21 16:31:05 +00001127 // If the build is configured to use prebuilts then force this to be preferred.
1128 if mctx.Config().UnbundledBuildUsePrebuiltSdks() {
1129 module.prebuilt.ForcePrefer()
1130 }
1131
Paul Duffin46a26a82020-04-07 19:27:04 +01001132 for apiScope, scopeProperties := range module.scopeProperties {
Paul Duffin56d44902020-01-31 13:36:25 +00001133 if len(scopeProperties.Jars) == 0 {
1134 continue
1135 }
1136
Paul Duffinbbb546b2020-04-09 00:07:11 +01001137 module.createJavaImportForStubs(mctx, apiScope, scopeProperties)
Paul Duffin3d1248c2020-04-09 00:10:17 +01001138
1139 module.createPrebuiltStubsSources(mctx, apiScope, scopeProperties)
Paul Duffin56d44902020-01-31 13:36:25 +00001140 }
Colin Cross79c7c262019-04-17 11:11:46 -07001141
1142 javaSdkLibraries := javaSdkLibraries(mctx.Config())
1143 javaSdkLibrariesLock.Lock()
1144 defer javaSdkLibrariesLock.Unlock()
1145 *javaSdkLibraries = append(*javaSdkLibraries, module.BaseModuleName())
1146}
1147
Paul Duffin6e7ecbf2020-05-08 15:01:19 +01001148func (module *sdkLibraryImport) createJavaImportForStubs(mctx android.DefaultableHookContext, apiScope *apiScope, scopeProperties *sdkLibraryScopeProperties) {
Paul Duffinbbb546b2020-04-09 00:07:11 +01001149 // Creates a java import for the jar with ".stubs" suffix
1150 props := struct {
1151 Name *string
1152 Soc_specific *bool
1153 Device_specific *bool
1154 Product_specific *bool
1155 System_ext_specific *bool
1156 Sdk_version *string
1157 Libs []string
1158 Jars []string
1159 Prefer *bool
1160 }{}
1161 props.Name = proptools.StringPtr(apiScope.stubsModuleName(module.BaseModuleName()))
1162 props.Sdk_version = scopeProperties.Sdk_version
1163 // Prepend any of the libs from the legacy public properties to the libs for each of the
1164 // scopes to avoid having to duplicate them in each scope.
1165 props.Libs = append(module.properties.Libs, scopeProperties.Libs...)
1166 props.Jars = scopeProperties.Jars
1167 if module.SocSpecific() {
1168 props.Soc_specific = proptools.BoolPtr(true)
1169 } else if module.DeviceSpecific() {
1170 props.Device_specific = proptools.BoolPtr(true)
1171 } else if module.ProductSpecific() {
1172 props.Product_specific = proptools.BoolPtr(true)
1173 } else if module.SystemExtSpecific() {
1174 props.System_ext_specific = proptools.BoolPtr(true)
1175 }
1176 // If the build should use prebuilt sdks then set prefer to true on the stubs library.
1177 // That will cause the prebuilt version of the stubs to override the source version.
1178 if mctx.Config().UnbundledBuildUsePrebuiltSdks() {
1179 props.Prefer = proptools.BoolPtr(true)
1180 }
1181 mctx.CreateModule(ImportFactory, &props)
1182}
1183
Paul Duffin6e7ecbf2020-05-08 15:01:19 +01001184func (module *sdkLibraryImport) createPrebuiltStubsSources(mctx android.DefaultableHookContext, apiScope *apiScope, scopeProperties *sdkLibraryScopeProperties) {
Paul Duffin3d1248c2020-04-09 00:10:17 +01001185 props := struct {
1186 Name *string
1187 Srcs []string
1188 }{}
Paul Duffinc8782502020-04-29 20:45:27 +01001189 props.Name = proptools.StringPtr(apiScope.stubsSourceModuleName(module.BaseModuleName()))
Paul Duffin3d1248c2020-04-09 00:10:17 +01001190 props.Srcs = scopeProperties.Stub_srcs
1191 mctx.CreateModule(PrebuiltStubsSourcesFactory, &props)
1192}
1193
Colin Cross79c7c262019-04-17 11:11:46 -07001194func (module *sdkLibraryImport) DepsMutator(ctx android.BottomUpMutatorContext) {
Paul Duffin46a26a82020-04-07 19:27:04 +01001195 for apiScope, scopeProperties := range module.scopeProperties {
Paul Duffin56d44902020-01-31 13:36:25 +00001196 if len(scopeProperties.Jars) == 0 {
1197 continue
1198 }
1199
1200 // Add dependencies to the prebuilt stubs library
1201 ctx.AddVariationDependencies(nil, apiScope.stubsTag, apiScope.stubsModuleName(module.BaseModuleName()))
1202 }
Colin Cross79c7c262019-04-17 11:11:46 -07001203}
1204
1205func (module *sdkLibraryImport) GenerateAndroidBuildActions(ctx android.ModuleContext) {
1206 // Record the paths to the prebuilt stubs library.
1207 ctx.VisitDirectDeps(func(to android.Module) {
1208 tag := ctx.OtherModuleDependencyTag(to)
1209
Paul Duffin56d44902020-01-31 13:36:25 +00001210 if lib, ok := to.(Dependency); ok {
1211 if scopeTag, ok := tag.(scopeDependencyTag); ok {
1212 apiScope := scopeTag.apiScope
1213 scopePaths := module.getScopePaths(apiScope)
1214 scopePaths.stubsHeaderPath = lib.HeaderJars()
1215 }
Colin Cross79c7c262019-04-17 11:11:46 -07001216 }
1217 })
1218}
1219
Paul Duffin56d44902020-01-31 13:36:25 +00001220func (module *sdkLibraryImport) sdkJars(
1221 ctx android.BaseModuleContext,
1222 sdkVersion sdkSpec) android.Paths {
1223
Paul Duffin50061512020-01-21 16:31:05 +00001224 // If a specific numeric version has been requested then use prebuilt versions of the sdk.
1225 if sdkVersion.version.isNumbered() {
1226 return PrebuiltJars(ctx, module.BaseModuleName(), sdkVersion)
1227 }
1228
Paul Duffin56d44902020-01-31 13:36:25 +00001229 var apiScope *apiScope
1230 switch sdkVersion.kind {
1231 case sdkSystem:
1232 apiScope = apiScopeSystem
1233 case sdkTest:
1234 apiScope = apiScopeTest
1235 default:
1236 apiScope = apiScopePublic
1237 }
1238
1239 paths := module.getScopePaths(apiScope)
1240 return paths.stubsHeaderPath
1241}
1242
Colin Cross79c7c262019-04-17 11:11:46 -07001243// to satisfy SdkLibraryDependency interface
Jiyong Park6a927c42020-01-21 02:03:43 +09001244func (module *sdkLibraryImport) SdkHeaderJars(ctx android.BaseModuleContext, sdkVersion sdkSpec) android.Paths {
Colin Cross79c7c262019-04-17 11:11:46 -07001245 // This module is just a wrapper for the prebuilt stubs.
Paul Duffin56d44902020-01-31 13:36:25 +00001246 return module.sdkJars(ctx, sdkVersion)
Colin Cross79c7c262019-04-17 11:11:46 -07001247}
1248
1249// to satisfy SdkLibraryDependency interface
Jiyong Park6a927c42020-01-21 02:03:43 +09001250func (module *sdkLibraryImport) SdkImplementationJars(ctx android.BaseModuleContext, sdkVersion sdkSpec) android.Paths {
Colin Cross79c7c262019-04-17 11:11:46 -07001251 // This module is just a wrapper for the stubs.
Paul Duffin56d44902020-01-31 13:36:25 +00001252 return module.sdkJars(ctx, sdkVersion)
Colin Cross79c7c262019-04-17 11:11:46 -07001253}
Jiyong Parke3833882020-02-17 17:28:10 +09001254
1255//
1256// java_sdk_library_xml
1257//
1258type sdkLibraryXml struct {
1259 android.ModuleBase
1260 android.DefaultableModuleBase
1261 android.ApexModuleBase
1262
1263 properties sdkLibraryXmlProperties
1264
1265 outputFilePath android.OutputPath
1266 installDirPath android.InstallPath
1267}
1268
1269type sdkLibraryXmlProperties struct {
1270 // canonical name of the lib
1271 Lib_name *string
1272}
1273
1274// java_sdk_library_xml builds the permission xml file for a java_sdk_library.
1275// Not to be used directly by users. java_sdk_library internally uses this.
1276func sdkLibraryXmlFactory() android.Module {
1277 module := &sdkLibraryXml{}
1278
1279 module.AddProperties(&module.properties)
1280
1281 android.InitApexModule(module)
1282 android.InitAndroidArchModule(module, android.DeviceSupported, android.MultilibCommon)
1283
1284 return module
1285}
1286
1287// from android.PrebuiltEtcModule
1288func (module *sdkLibraryXml) SubDir() string {
1289 return "permissions"
1290}
1291
1292// from android.PrebuiltEtcModule
1293func (module *sdkLibraryXml) OutputFile() android.OutputPath {
1294 return module.outputFilePath
1295}
1296
1297// from android.ApexModule
1298func (module *sdkLibraryXml) AvailableFor(what string) bool {
1299 return true
1300}
1301
1302func (module *sdkLibraryXml) DepsMutator(ctx android.BottomUpMutatorContext) {
1303 // do nothing
1304}
1305
1306// File path to the runtime implementation library
1307func (module *sdkLibraryXml) implPath() string {
1308 implName := proptools.String(module.properties.Lib_name)
1309 if apexName := module.ApexName(); apexName != "" {
1310 // TODO(b/146468504): ApexName() is only a soong module name, not apex name.
1311 // In most cases, this works fine. But when apex_name is set or override_apex is used
1312 // this can be wrong.
1313 return fmt.Sprintf("/apex/%s/javalib/%s.jar", apexName, implName)
1314 }
1315 partition := "system"
1316 if module.SocSpecific() {
1317 partition = "vendor"
1318 } else if module.DeviceSpecific() {
1319 partition = "odm"
1320 } else if module.ProductSpecific() {
1321 partition = "product"
1322 } else if module.SystemExtSpecific() {
1323 partition = "system_ext"
1324 }
1325 return "/" + partition + "/framework/" + implName + ".jar"
1326}
1327
1328func (module *sdkLibraryXml) GenerateAndroidBuildActions(ctx android.ModuleContext) {
1329 libName := proptools.String(module.properties.Lib_name)
1330 xmlContent := fmt.Sprintf(permissionsTemplate, libName, module.implPath())
1331
1332 module.outputFilePath = android.PathForModuleOut(ctx, libName+".xml").OutputPath
1333 rule := android.NewRuleBuilder()
1334 rule.Command().
1335 Text("/bin/bash -c \"echo -e '" + xmlContent + "'\" > ").
1336 Output(module.outputFilePath)
1337
1338 rule.Build(pctx, ctx, "java_sdk_xml", "Permission XML")
1339
1340 module.installDirPath = android.PathForModuleInstall(ctx, "etc", module.SubDir())
1341}
1342
1343func (module *sdkLibraryXml) AndroidMkEntries() []android.AndroidMkEntries {
1344 if !module.IsForPlatform() {
1345 return []android.AndroidMkEntries{android.AndroidMkEntries{
1346 Disabled: true,
1347 }}
1348 }
1349
1350 return []android.AndroidMkEntries{android.AndroidMkEntries{
1351 Class: "ETC",
1352 OutputFile: android.OptionalPathForPath(module.outputFilePath),
1353 ExtraEntries: []android.AndroidMkExtraEntriesFunc{
1354 func(entries *android.AndroidMkEntries) {
1355 entries.SetString("LOCAL_MODULE_TAGS", "optional")
1356 entries.SetString("LOCAL_MODULE_PATH", module.installDirPath.ToMakePath().String())
1357 entries.SetString("LOCAL_INSTALLED_MODULE_STEM", module.outputFilePath.Base())
1358 },
1359 },
1360 }}
1361}
Paul Duffindd46f712020-02-10 13:37:10 +00001362
1363type sdkLibrarySdkMemberType struct {
1364 android.SdkMemberTypeBase
1365}
1366
1367func (s *sdkLibrarySdkMemberType) AddDependencies(mctx android.BottomUpMutatorContext, dependencyTag blueprint.DependencyTag, names []string) {
1368 mctx.AddVariationDependencies(nil, dependencyTag, names...)
1369}
1370
1371func (s *sdkLibrarySdkMemberType) IsInstance(module android.Module) bool {
1372 _, ok := module.(*SdkLibrary)
1373 return ok
1374}
1375
1376func (s *sdkLibrarySdkMemberType) AddPrebuiltModule(ctx android.SdkMemberContext, member android.SdkMember) android.BpModule {
1377 return ctx.SnapshotBuilder().AddPrebuiltModule(member, "java_sdk_library_import")
1378}
1379
1380func (s *sdkLibrarySdkMemberType) CreateVariantPropertiesStruct() android.SdkMemberProperties {
1381 return &sdkLibrarySdkMemberProperties{}
1382}
1383
1384type sdkLibrarySdkMemberProperties struct {
1385 android.SdkMemberPropertiesBase
1386
1387 // Scope to per scope properties.
1388 Scopes map[*apiScope]scopeProperties
1389
1390 // Additional libraries that the exported stubs libraries depend upon.
1391 Libs []string
Paul Duffin3d1248c2020-04-09 00:10:17 +01001392
1393 // The Java stubs source files.
1394 Stub_srcs []string
Paul Duffindd46f712020-02-10 13:37:10 +00001395}
1396
1397type scopeProperties struct {
Paul Duffin1fd005d2020-04-09 01:08:11 +01001398 Jars android.Paths
1399 StubsSrcJar android.Path
1400 CurrentApiFile android.Path
1401 RemovedApiFile android.Path
1402 SdkVersion string
Paul Duffindd46f712020-02-10 13:37:10 +00001403}
1404
1405func (s *sdkLibrarySdkMemberProperties) PopulateFromVariant(ctx android.SdkMemberContext, variant android.Module) {
1406 sdk := variant.(*SdkLibrary)
1407
1408 s.Scopes = make(map[*apiScope]scopeProperties)
1409 for _, apiScope := range allApiScopes {
1410 paths := sdk.getScopePaths(apiScope)
1411 jars := paths.stubsImplPath
1412 if len(jars) > 0 {
1413 properties := scopeProperties{}
1414 properties.Jars = jars
Paul Duffin780c5f42020-05-12 15:52:55 +01001415 properties.SdkVersion = sdk.sdkVersionForStubsLibrary(ctx.SdkModuleContext(), apiScope)
Paul Duffin3d1248c2020-04-09 00:10:17 +01001416 properties.StubsSrcJar = paths.stubsSrcJar
Paul Duffin1fd005d2020-04-09 01:08:11 +01001417 properties.CurrentApiFile = paths.currentApiFilePath
1418 properties.RemovedApiFile = paths.removedApiFilePath
Paul Duffindd46f712020-02-10 13:37:10 +00001419 s.Scopes[apiScope] = properties
1420 }
1421 }
1422
1423 s.Libs = sdk.properties.Libs
1424}
1425
1426func (s *sdkLibrarySdkMemberProperties) AddToPropertySet(ctx android.SdkMemberContext, propertySet android.BpPropertySet) {
1427 for _, apiScope := range allApiScopes {
1428 if properties, ok := s.Scopes[apiScope]; ok {
1429 scopeSet := propertySet.AddPropertySet(apiScope.name)
1430
Paul Duffin3d1248c2020-04-09 00:10:17 +01001431 scopeDir := filepath.Join("sdk_library", s.OsPrefix(), apiScope.name)
1432
Paul Duffindd46f712020-02-10 13:37:10 +00001433 var jars []string
1434 for _, p := range properties.Jars {
Paul Duffin3d1248c2020-04-09 00:10:17 +01001435 dest := filepath.Join(scopeDir, ctx.Name()+"-stubs.jar")
Paul Duffindd46f712020-02-10 13:37:10 +00001436 ctx.SnapshotBuilder().CopyToSnapshot(p, dest)
1437 jars = append(jars, dest)
1438 }
1439 scopeSet.AddProperty("jars", jars)
1440
Paul Duffin3d1248c2020-04-09 00:10:17 +01001441 // Merge the stubs source jar into the snapshot zip so that when it is unpacked
1442 // the source files are also unpacked.
1443 snapshotRelativeDir := filepath.Join(scopeDir, ctx.Name()+"_stub_sources")
1444 ctx.SnapshotBuilder().UnzipToSnapshot(properties.StubsSrcJar, snapshotRelativeDir)
1445 scopeSet.AddProperty("stub_srcs", []string{snapshotRelativeDir})
1446
Paul Duffin1fd005d2020-04-09 01:08:11 +01001447 if properties.CurrentApiFile != nil {
1448 currentApiSnapshotPath := filepath.Join(scopeDir, ctx.Name()+".txt")
1449 ctx.SnapshotBuilder().CopyToSnapshot(properties.CurrentApiFile, currentApiSnapshotPath)
1450 scopeSet.AddProperty("current_api", currentApiSnapshotPath)
1451 }
1452
1453 if properties.RemovedApiFile != nil {
1454 removedApiSnapshotPath := filepath.Join(scopeDir, ctx.Name()+"-removed.txt")
1455 ctx.SnapshotBuilder().CopyToSnapshot(properties.CurrentApiFile, removedApiSnapshotPath)
1456 scopeSet.AddProperty("removed_api", removedApiSnapshotPath)
1457 }
1458
Paul Duffindd46f712020-02-10 13:37:10 +00001459 if properties.SdkVersion != "" {
1460 scopeSet.AddProperty("sdk_version", properties.SdkVersion)
1461 }
1462 }
1463 }
1464
1465 if len(s.Libs) > 0 {
1466 propertySet.AddPropertyWithTag("libs", s.Libs, ctx.SnapshotBuilder().SdkMemberReferencePropertyTag(false))
1467 }
1468}