blob: 68690c01e7859829aaa0117621c38c5b02818779 [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
Paul Duffin160fe412020-05-10 19:32:20 +0100354 // Properties related to api linting.
355 Api_lint struct {
356 // Enable api linting.
357 Enabled *bool
358 }
359
Jiyong Parkc678ad32018-04-10 13:07:10 +0900360 // TODO: determines whether to create HTML doc or not
361 //Html_doc *bool
362}
363
Paul Duffind1b3a922020-01-22 11:57:20 +0000364type scopePaths struct {
Paul Duffin1fd005d2020-04-09 01:08:11 +0100365 stubsHeaderPath android.Paths
366 stubsImplPath android.Paths
367 currentApiFilePath android.Path
368 removedApiFilePath android.Path
369 stubsSrcJar android.Path
Paul Duffind1b3a922020-01-22 11:57:20 +0000370}
371
Paul Duffinc8782502020-04-29 20:45:27 +0100372func (paths *scopePaths) extractStubsLibraryInfoFromDependency(dep android.Module) error {
373 if lib, ok := dep.(Dependency); ok {
374 paths.stubsHeaderPath = lib.HeaderJars()
375 paths.stubsImplPath = lib.ImplementationJars()
376 return nil
377 } else {
378 return fmt.Errorf("expected module that implements Dependency, e.g. java_library")
379 }
380}
381
382func (paths *scopePaths) extractStubsSourceAndApiInfoFromApiStubsProvider(dep android.Module) error {
383 if provider, ok := dep.(ApiStubsProvider); ok {
384 paths.currentApiFilePath = provider.ApiFilePath()
385 paths.removedApiFilePath = provider.RemovedApiFilePath()
386 paths.stubsSrcJar = provider.StubsSrcJar()
387 return nil
388 } else {
389 return fmt.Errorf("expected module that implements ApiStubsProvider, e.g. droidstubs")
390 }
391}
392
Paul Duffin56d44902020-01-31 13:36:25 +0000393// Common code between sdk library and sdk library import
394type commonToSdkLibraryAndImport struct {
395 scopePaths map[*apiScope]*scopePaths
396}
397
398func (c *commonToSdkLibraryAndImport) getScopePaths(scope *apiScope) *scopePaths {
399 if c.scopePaths == nil {
400 c.scopePaths = make(map[*apiScope]*scopePaths)
401 }
402 paths := c.scopePaths[scope]
403 if paths == nil {
404 paths = &scopePaths{}
405 c.scopePaths[scope] = paths
406 }
407
408 return paths
409}
410
Inseob Kimc0907f12019-02-08 21:00:45 +0900411type SdkLibrary struct {
Sundong Ahn054b19a2018-10-19 13:46:09 +0900412 Library
Jiyong Parkc678ad32018-04-10 13:07:10 +0900413
Sundong Ahn054b19a2018-10-19 13:46:09 +0900414 sdkLibraryProperties sdkLibraryProperties
Jiyong Parkc678ad32018-04-10 13:07:10 +0900415
Paul Duffin3375e352020-04-28 10:44:03 +0100416 // Map from api scope to the scope specific property structure.
417 scopeToProperties map[*apiScope]*ApiScopeProperties
418
Paul Duffin56d44902020-01-31 13:36:25 +0000419 commonToSdkLibraryAndImport
Jiyong Parkc678ad32018-04-10 13:07:10 +0900420}
421
Inseob Kimc0907f12019-02-08 21:00:45 +0900422var _ Dependency = (*SdkLibrary)(nil)
423var _ SdkLibraryDependency = (*SdkLibrary)(nil)
Colin Cross897d2ed2019-02-11 14:03:51 -0800424
Paul Duffin3375e352020-04-28 10:44:03 +0100425func (module *SdkLibrary) generateTestAndSystemScopesByDefault() bool {
426 return module.sdkLibraryProperties.Generate_system_and_test_apis
427}
428
429func (module *SdkLibrary) getGeneratedApiScopes(ctx android.EarlyModuleContext) apiScopes {
430 // Check to see if any scopes have been explicitly enabled. If any have then all
431 // must be.
432 anyScopesExplicitlyEnabled := false
433 for _, scope := range allApiScopes {
434 scopeProperties := module.scopeToProperties[scope]
435 if scopeProperties.Enabled != nil {
436 anyScopesExplicitlyEnabled = true
437 break
438 }
Paul Duffind1b3a922020-01-22 11:57:20 +0000439 }
Paul Duffin3375e352020-04-28 10:44:03 +0100440
441 var generatedScopes apiScopes
442 enabledScopes := make(map[*apiScope]struct{})
443 for _, scope := range allApiScopes {
444 scopeProperties := module.scopeToProperties[scope]
445 // If any scopes are explicitly enabled then ignore the legacy enabled status.
446 // This is to ensure that any new usages of this module type do not rely on legacy
447 // behaviour.
448 defaultEnabledStatus := false
449 if anyScopesExplicitlyEnabled {
450 defaultEnabledStatus = scope.defaultEnabledStatus
451 } else {
452 defaultEnabledStatus = scope.legacyEnabledStatus(module)
453 }
454 enabled := proptools.BoolDefault(scopeProperties.Enabled, defaultEnabledStatus)
455 if enabled {
456 enabledScopes[scope] = struct{}{}
457 generatedScopes = append(generatedScopes, scope)
458 }
459 }
460
461 // Now check to make sure that any scope that is extended by an enabled scope is also
462 // enabled.
463 for _, scope := range allApiScopes {
464 if _, ok := enabledScopes[scope]; ok {
465 extends := scope.extends
466 if extends != nil {
467 if _, ok := enabledScopes[extends]; !ok {
468 ctx.ModuleErrorf("enabled api scope %q depends on disabled scope %q", scope, extends)
469 }
470 }
471 }
472 }
473
474 return generatedScopes
Paul Duffind1b3a922020-01-22 11:57:20 +0000475}
476
Paul Duffine74ac732020-02-06 13:51:46 +0000477var xmlPermissionsFileTag = dependencyTag{name: "xml-permissions-file"}
478
Jiyong Parke3833882020-02-17 17:28:10 +0900479func IsXmlPermissionsFileDepTag(depTag blueprint.DependencyTag) bool {
480 if dt, ok := depTag.(dependencyTag); ok {
481 return dt == xmlPermissionsFileTag
482 }
483 return false
484}
485
Inseob Kimc0907f12019-02-08 21:00:45 +0900486func (module *SdkLibrary) DepsMutator(ctx android.BottomUpMutatorContext) {
Paul Duffin3375e352020-04-28 10:44:03 +0100487 for _, apiScope := range module.getGeneratedApiScopes(ctx) {
Paul Duffind1b3a922020-01-22 11:57:20 +0000488 // Add dependencies to the stubs library
Paul Duffin50061512020-01-21 16:31:05 +0000489 ctx.AddVariationDependencies(nil, apiScope.stubsTag, module.stubsName(apiScope))
Paul Duffind1b3a922020-01-22 11:57:20 +0000490
Paul Duffinc8782502020-04-29 20:45:27 +0100491 // And the stubs source and api files
492 ctx.AddVariationDependencies(nil, apiScope.stubsSourceAndApiTag, module.stubsSourceName(apiScope))
Sundong Ahn054b19a2018-10-19 13:46:09 +0900493 }
494
Paul Duffine74ac732020-02-06 13:51:46 +0000495 if !proptools.Bool(module.sdkLibraryProperties.Api_only) {
496 // Add dependency to the rule for generating the xml permissions file
Jiyong Parke3833882020-02-17 17:28:10 +0900497 ctx.AddDependency(module, xmlPermissionsFileTag, module.xmlFileName())
Paul Duffine74ac732020-02-06 13:51:46 +0000498 }
499
Sundong Ahn054b19a2018-10-19 13:46:09 +0900500 module.Library.deps(ctx)
Jiyong Parkc678ad32018-04-10 13:07:10 +0900501}
502
Inseob Kimc0907f12019-02-08 21:00:45 +0900503func (module *SdkLibrary) GenerateAndroidBuildActions(ctx android.ModuleContext) {
Paul Duffin43db9be2019-12-30 17:35:49 +0000504 // Don't build an implementation library if this is api only.
505 if !proptools.Bool(module.sdkLibraryProperties.Api_only) {
506 module.Library.GenerateAndroidBuildActions(ctx)
507 }
Sundong Ahn054b19a2018-10-19 13:46:09 +0900508
Sundong Ahn57368eb2018-07-06 11:20:23 +0900509 // Record the paths to the header jars of the library (stubs and impl).
Paul Duffind1b3a922020-01-22 11:57:20 +0000510 // When this java_sdk_library is depended upon from others via "libs" property,
Jiyong Parkc678ad32018-04-10 13:07:10 +0900511 // the recorded paths will be returned depending on the link type of the caller.
512 ctx.VisitDirectDeps(func(to android.Module) {
Jiyong Parkc678ad32018-04-10 13:07:10 +0900513 tag := ctx.OtherModuleDependencyTag(to)
514
Paul Duffinc8782502020-04-29 20:45:27 +0100515 // Extract information from any of the scope specific dependencies.
516 if scopeTag, ok := tag.(scopeDependencyTag); ok {
517 apiScope := scopeTag.apiScope
518 scopePaths := module.getScopePaths(apiScope)
519
520 // Extract information from the dependency. The exact information extracted
521 // is determined by the nature of the dependency which is determined by the tag.
522 scopeTag.extractDepInfo(ctx, to, scopePaths)
Sundong Ahn20e998b2018-07-24 11:19:26 +0900523 }
Jiyong Parkc678ad32018-04-10 13:07:10 +0900524 })
525}
526
Jiyong Park0b0e1b92019-12-03 13:24:29 +0900527func (module *SdkLibrary) AndroidMkEntries() []android.AndroidMkEntries {
Paul Duffin43db9be2019-12-30 17:35:49 +0000528 if proptools.Bool(module.sdkLibraryProperties.Api_only) {
529 return nil
530 }
Jiyong Park0b0e1b92019-12-03 13:24:29 +0900531 entriesList := module.Library.AndroidMkEntries()
532 entries := &entriesList[0]
Jaewoong Jungb0c127c2019-08-29 14:56:03 -0700533 entries.Required = append(entries.Required, module.xmlFileName())
Jiyong Park0b0e1b92019-12-03 13:24:29 +0900534 return entriesList
Jiyong Park82484c02018-04-23 21:41:26 +0900535}
536
Paul Duffinc8782502020-04-29 20:45:27 +0100537// Name of the java_library module that compiles the stubs source.
Paul Duffind1b3a922020-01-22 11:57:20 +0000538func (module *SdkLibrary) stubsName(apiScope *apiScope) string {
539 return apiScope.stubsModuleName(module.BaseModuleName())
Jiyong Parkc678ad32018-04-10 13:07:10 +0900540}
541
Paul Duffinc8782502020-04-29 20:45:27 +0100542// // Name of the droidstubs module that generates the stubs source and
543// generates/checks the API.
544func (module *SdkLibrary) stubsSourceName(apiScope *apiScope) string {
545 return apiScope.stubsSourceModuleName(module.BaseModuleName())
Jiyong Parkc678ad32018-04-10 13:07:10 +0900546}
547
548// Module name of the runtime implementation library
Inseob Kimc0907f12019-02-08 21:00:45 +0900549func (module *SdkLibrary) implName() string {
Sundong Ahn054b19a2018-10-19 13:46:09 +0900550 return module.BaseModuleName()
Jiyong Parkc678ad32018-04-10 13:07:10 +0900551}
552
Jiyong Parkc678ad32018-04-10 13:07:10 +0900553// Module name of the XML file for the lib
Inseob Kimc0907f12019-02-08 21:00:45 +0900554func (module *SdkLibrary) xmlFileName() string {
Jiyong Parkc678ad32018-04-10 13:07:10 +0900555 return module.BaseModuleName() + sdkXmlFileSuffix
556}
557
Anton Hansson5fd5d242020-03-27 19:43:19 +0000558// The dist path of the stub artifacts
559func (module *SdkLibrary) apiDistPath(apiScope *apiScope) string {
560 if module.ModuleBase.Owner() != "" {
561 return path.Join("apistubs", module.ModuleBase.Owner(), apiScope.name)
562 } else if Bool(module.sdkLibraryProperties.Core_lib) {
563 return path.Join("apistubs", "core", apiScope.name)
564 } else {
565 return path.Join("apistubs", "android", apiScope.name)
566 }
567}
568
Paul Duffin12ceb462019-12-24 20:31:31 +0000569// Get the sdk version for use when compiling the stubs library.
Paul Duffin780c5f42020-05-12 15:52:55 +0100570func (module *SdkLibrary) sdkVersionForStubsLibrary(mctx android.EarlyModuleContext, apiScope *apiScope) string {
Paul Duffin12ceb462019-12-24 20:31:31 +0000571 sdkDep := decodeSdkDep(mctx, sdkContext(&module.Library))
572 if sdkDep.hasStandardLibs() {
573 // If building against a standard sdk then use the sdk version appropriate for the scope.
Paul Duffind1b3a922020-01-22 11:57:20 +0000574 return apiScope.sdkVersion
Paul Duffin12ceb462019-12-24 20:31:31 +0000575 } else {
576 // Otherwise, use no system module.
577 return "none"
578 }
579}
580
Paul Duffind1b3a922020-01-22 11:57:20 +0000581func (module *SdkLibrary) latestApiFilegroupName(apiScope *apiScope) string {
582 return ":" + module.BaseModuleName() + ".api." + apiScope.name + ".latest"
Jiyong Park58c518b2018-05-12 22:29:12 +0900583}
Jiyong Parkc678ad32018-04-10 13:07:10 +0900584
Paul Duffind1b3a922020-01-22 11:57:20 +0000585func (module *SdkLibrary) latestRemovedApiFilegroupName(apiScope *apiScope) string {
586 return ":" + module.BaseModuleName() + "-removed.api." + apiScope.name + ".latest"
Jiyong Parkc678ad32018-04-10 13:07:10 +0900587}
588
589// Creates a static java library that has API stubs
Paul Duffinf0229202020-04-29 16:47:28 +0100590func (module *SdkLibrary) createStubsLibrary(mctx android.DefaultableHookContext, apiScope *apiScope) {
Jiyong Parkc678ad32018-04-10 13:07:10 +0900591 props := struct {
Sundong Ahn0d7dff42019-12-04 12:53:44 +0900592 Name *string
Paul Duffin4911a892020-04-29 23:35:13 +0100593 Visibility []string
Sundong Ahn0d7dff42019-12-04 12:53:44 +0900594 Srcs []string
Paul Duffin367ab912019-12-23 19:40:36 +0000595 Installable *bool
Sundong Ahn0d7dff42019-12-04 12:53:44 +0900596 Sdk_version *string
Paul Duffin12ceb462019-12-24 20:31:31 +0000597 System_modules *string
Paul Duffinab8da5d2020-02-07 16:12:04 +0000598 Patch_module *string
Sundong Ahn0d7dff42019-12-04 12:53:44 +0900599 Libs []string
600 Soc_specific *bool
601 Device_specific *bool
602 Product_specific *bool
603 System_ext_specific *bool
604 Compile_dex *bool
Sundong Ahn0d7dff42019-12-04 12:53:44 +0900605 Java_version *string
606 Product_variables struct {
Jiyong Park82484c02018-04-23 21:41:26 +0900607 Pdk struct {
608 Enabled *bool
609 }
Jiyong Parkc678ad32018-04-10 13:07:10 +0900610 }
Sundong Ahn054b19a2018-10-19 13:46:09 +0900611 Openjdk9 struct {
612 Srcs []string
613 Javacflags []string
614 }
Anton Hansson5fd5d242020-03-27 19:43:19 +0000615 Dist struct {
616 Targets []string
617 Dest *string
618 Dir *string
619 Tag *string
620 }
Jiyong Parkc678ad32018-04-10 13:07:10 +0900621 }{}
622
Jiyong Parkdf130542018-04-27 16:29:21 +0900623 props.Name = proptools.StringPtr(module.stubsName(apiScope))
Paul Duffin4911a892020-04-29 23:35:13 +0100624
625 // If stubs_library_visibility is not set then the created module will use the
626 // visibility of this module.
627 visibility := module.sdkLibraryProperties.Stubs_library_visibility
628 props.Visibility = visibility
629
Jiyong Parkc678ad32018-04-10 13:07:10 +0900630 // sources are generated from the droiddoc
Paul Duffinc8782502020-04-29 20:45:27 +0100631 props.Srcs = []string{":" + module.stubsSourceName(apiScope)}
Paul Duffin12ceb462019-12-24 20:31:31 +0000632 sdkVersion := module.sdkVersionForStubsLibrary(mctx, apiScope)
Paul Duffin52d398a2019-06-11 12:31:14 +0100633 props.Sdk_version = proptools.StringPtr(sdkVersion)
Paul Duffin12ceb462019-12-24 20:31:31 +0000634 props.System_modules = module.Library.Module.deviceProperties.System_modules
Paul Duffinab8da5d2020-02-07 16:12:04 +0000635 props.Patch_module = module.Library.Module.properties.Patch_module
Paul Duffin367ab912019-12-23 19:40:36 +0000636 props.Installable = proptools.BoolPtr(false)
Sundong Ahn054b19a2018-10-19 13:46:09 +0900637 props.Libs = module.sdkLibraryProperties.Stub_only_libs
Jiyong Park82484c02018-04-23 21:41:26 +0900638 props.Product_variables.Pdk.Enabled = proptools.BoolPtr(false)
Sundong Ahn054b19a2018-10-19 13:46:09 +0900639 props.Openjdk9.Srcs = module.Library.Module.properties.Openjdk9.Srcs
640 props.Openjdk9.Javacflags = module.Library.Module.properties.Openjdk9.Javacflags
641 props.Java_version = module.Library.Module.properties.Java_version
642 if module.Library.Module.deviceProperties.Compile_dex != nil {
643 props.Compile_dex = module.Library.Module.deviceProperties.Compile_dex
Sundong Ahndd567f92018-07-31 17:19:11 +0900644 }
Jiyong Parkc678ad32018-04-10 13:07:10 +0900645
646 if module.SocSpecific() {
647 props.Soc_specific = proptools.BoolPtr(true)
648 } else if module.DeviceSpecific() {
649 props.Device_specific = proptools.BoolPtr(true)
650 } else if module.ProductSpecific() {
651 props.Product_specific = proptools.BoolPtr(true)
Sundong Ahn0d7dff42019-12-04 12:53:44 +0900652 } else if module.SystemExtSpecific() {
653 props.System_ext_specific = proptools.BoolPtr(true)
Jiyong Parkc678ad32018-04-10 13:07:10 +0900654 }
Anton Hansson5fd5d242020-03-27 19:43:19 +0000655 // Dist the class jar artifact for sdk builds.
656 if !Bool(module.sdkLibraryProperties.No_dist) {
657 props.Dist.Targets = []string{"sdk", "win_sdk"}
658 props.Dist.Dest = proptools.StringPtr(fmt.Sprintf("%v.jar", module.BaseModuleName()))
659 props.Dist.Dir = proptools.StringPtr(module.apiDistPath(apiScope))
660 props.Dist.Tag = proptools.StringPtr(".jar")
661 }
Jiyong Parkc678ad32018-04-10 13:07:10 +0900662
Colin Cross84dfc3d2019-09-25 11:33:01 -0700663 mctx.CreateModule(LibraryFactory, &props)
Jiyong Parkc678ad32018-04-10 13:07:10 +0900664}
665
Paul Duffin6d0886e2020-04-07 18:49:53 +0100666// Creates a droidstubs module that creates stubs source files from the given full source
Paul Duffinc8782502020-04-29 20:45:27 +0100667// files and also updates and checks the API specification files.
668func (module *SdkLibrary) createStubsSourcesAndApi(mctx android.DefaultableHookContext, apiScope *apiScope) {
Jiyong Parkc678ad32018-04-10 13:07:10 +0900669 props := struct {
Sundong Ahn054b19a2018-10-19 13:46:09 +0900670 Name *string
Paul Duffin4911a892020-04-29 23:35:13 +0100671 Visibility []string
Sundong Ahn054b19a2018-10-19 13:46:09 +0900672 Srcs []string
673 Installable *bool
Paul Duffin52d398a2019-06-11 12:31:14 +0100674 Sdk_version *string
Paul Duffin12ceb462019-12-24 20:31:31 +0000675 System_modules *string
Sundong Ahn054b19a2018-10-19 13:46:09 +0900676 Libs []string
Paul Duffin11512472019-02-11 15:55:17 +0000677 Arg_files []string
Sundong Ahn054b19a2018-10-19 13:46:09 +0900678 Args *string
Sundong Ahn054b19a2018-10-19 13:46:09 +0900679 Java_version *string
680 Merge_annotations_dirs []string
681 Merge_inclusion_annotations_dirs []string
682 Check_api struct {
Inseob Kim38449af2019-02-28 14:24:05 +0900683 Current ApiToCheck
684 Last_released ApiToCheck
685 Ignore_missing_latest_api *bool
Paul Duffin160fe412020-05-10 19:32:20 +0100686
687 Api_lint struct {
688 Enabled *bool
689 New_since *string
690 Baseline_file *string
691 }
Jiyong Park58c518b2018-05-12 22:29:12 +0900692 }
Sundong Ahn1b92c822018-05-29 11:35:17 +0900693 Aidl struct {
694 Include_dirs []string
695 Local_include_dirs []string
696 }
Anton Hansson5fd5d242020-03-27 19:43:19 +0000697 Dist struct {
698 Targets []string
699 Dest *string
700 Dir *string
701 }
Jiyong Parkc678ad32018-04-10 13:07:10 +0900702 }{}
703
Paul Duffin7b78b4d2020-04-28 14:08:32 +0100704 // The stubs source processing uses the same compile time classpath when extracting the
705 // API from the implementation library as it does when compiling it. i.e. the same
706 // * sdk version
707 // * system_modules
708 // * libs (static_libs/libs)
Paul Duffin250e6192019-06-07 10:44:37 +0100709
Paul Duffinc8782502020-04-29 20:45:27 +0100710 props.Name = proptools.StringPtr(module.stubsSourceName(apiScope))
Paul Duffin4911a892020-04-29 23:35:13 +0100711
712 // If stubs_source_visibility is not set then the created module will use the
713 // visibility of this module.
714 visibility := module.sdkLibraryProperties.Stubs_source_visibility
715 props.Visibility = visibility
716
Sundong Ahn054b19a2018-10-19 13:46:09 +0900717 props.Srcs = append(props.Srcs, module.Library.Module.properties.Srcs...)
Paul Duffin7b78b4d2020-04-28 14:08:32 +0100718 props.Sdk_version = module.Library.Module.deviceProperties.Sdk_version
Paul Duffin12ceb462019-12-24 20:31:31 +0000719 props.System_modules = module.Library.Module.deviceProperties.System_modules
Jiyong Parkc678ad32018-04-10 13:07:10 +0900720 props.Installable = proptools.BoolPtr(false)
Sundong Ahne6f0b052018-06-05 16:46:14 +0900721 // A droiddoc module has only one Libs property and doesn't distinguish between
722 // shared libs and static libs. So we need to add both of these libs to Libs property.
Sundong Ahn054b19a2018-10-19 13:46:09 +0900723 props.Libs = module.Library.Module.properties.Libs
724 props.Libs = append(props.Libs, module.Library.Module.properties.Static_libs...)
725 props.Aidl.Include_dirs = module.Library.Module.deviceProperties.Aidl.Include_dirs
726 props.Aidl.Local_include_dirs = module.Library.Module.deviceProperties.Aidl.Local_include_dirs
Sundong Ahn054b19a2018-10-19 13:46:09 +0900727 props.Java_version = module.Library.Module.properties.Java_version
Jiyong Parkc678ad32018-04-10 13:07:10 +0900728
Sundong Ahn054b19a2018-10-19 13:46:09 +0900729 props.Merge_annotations_dirs = module.sdkLibraryProperties.Merge_annotations_dirs
730 props.Merge_inclusion_annotations_dirs = module.sdkLibraryProperties.Merge_inclusion_annotations_dirs
731
Paul Duffin6d0886e2020-04-07 18:49:53 +0100732 droidstubsArgs := []string{}
Paul Duffin235ffff2019-12-24 10:41:30 +0000733 if len(module.sdkLibraryProperties.Api_packages) != 0 {
Paul Duffin6d0886e2020-04-07 18:49:53 +0100734 droidstubsArgs = append(droidstubsArgs, "--stub-packages "+strings.Join(module.sdkLibraryProperties.Api_packages, ":"))
Paul Duffin235ffff2019-12-24 10:41:30 +0000735 }
736 if len(module.sdkLibraryProperties.Hidden_api_packages) != 0 {
Paul Duffin6d0886e2020-04-07 18:49:53 +0100737 droidstubsArgs = append(droidstubsArgs,
Paul Duffin235ffff2019-12-24 10:41:30 +0000738 android.JoinWithPrefix(module.sdkLibraryProperties.Hidden_api_packages, " --hide-package "))
739 }
Paul Duffin6d0886e2020-04-07 18:49:53 +0100740 droidstubsArgs = append(droidstubsArgs, module.sdkLibraryProperties.Droiddoc_options...)
Paul Duffin235ffff2019-12-24 10:41:30 +0000741 disabledWarnings := []string{
742 "MissingPermission",
743 "BroadcastBehavior",
744 "HiddenSuperclass",
745 "DeprecationMismatch",
746 "UnavailableSymbol",
747 "SdkConstant",
748 "HiddenTypeParameter",
749 "Todo",
750 "Typo",
751 }
Paul Duffin6d0886e2020-04-07 18:49:53 +0100752 droidstubsArgs = append(droidstubsArgs, android.JoinWithPrefix(disabledWarnings, "--hide "))
Sundong Ahnfb2721f2018-09-17 13:23:09 +0900753
Paul Duffin1fb487d2020-04-07 18:50:10 +0100754 // Add in scope specific arguments.
755 droidstubsArgs = append(droidstubsArgs, apiScope.droidstubsArgs...)
Paul Duffin11512472019-02-11 15:55:17 +0000756 props.Arg_files = module.sdkLibraryProperties.Droiddoc_option_files
Paul Duffin6d0886e2020-04-07 18:49:53 +0100757 props.Args = proptools.StringPtr(strings.Join(droidstubsArgs, " "))
Jiyong Parkc678ad32018-04-10 13:07:10 +0900758
759 // List of APIs identified from the provided source files are created. They are later
760 // compared against to the not-yet-released (a.k.a current) list of APIs and to the
761 // last-released (a.k.a numbered) list of API.
Paul Duffind1b3a922020-01-22 11:57:20 +0000762 currentApiFileName := apiScope.apiFilePrefix + "current.txt"
763 removedApiFileName := apiScope.apiFilePrefix + "removed.txt"
Paul Duffin749f98f2019-12-30 17:23:46 +0000764 apiDir := module.getApiDir()
765 currentApiFileName = path.Join(apiDir, currentApiFileName)
766 removedApiFileName = path.Join(apiDir, removedApiFileName)
Jiyong Parkc678ad32018-04-10 13:07:10 +0900767
Jiyong Park58c518b2018-05-12 22:29:12 +0900768 // check against the not-yet-release API
769 props.Check_api.Current.Api_file = proptools.StringPtr(currentApiFileName)
770 props.Check_api.Current.Removed_api_file = proptools.StringPtr(removedApiFileName)
Jiyong Park58c518b2018-05-12 22:29:12 +0900771
Anton Hansson6478ac12020-05-02 11:19:36 +0100772 if !apiScope.unstable {
773 // check against the latest released API
Paul Duffin160fe412020-05-10 19:32:20 +0100774 latestApiFilegroupName := proptools.StringPtr(module.latestApiFilegroupName(apiScope))
775 props.Check_api.Last_released.Api_file = latestApiFilegroupName
Anton Hansson6478ac12020-05-02 11:19:36 +0100776 props.Check_api.Last_released.Removed_api_file = proptools.StringPtr(
777 module.latestRemovedApiFilegroupName(apiScope))
778 props.Check_api.Ignore_missing_latest_api = proptools.BoolPtr(true)
Paul Duffin160fe412020-05-10 19:32:20 +0100779
780 if proptools.Bool(module.sdkLibraryProperties.Api_lint.Enabled) {
781 // Enable api lint.
782 props.Check_api.Api_lint.Enabled = proptools.BoolPtr(true)
783 props.Check_api.Api_lint.New_since = latestApiFilegroupName
784
785 // If it exists then pass a lint-baseline.txt through to droidstubs.
786 baselinePath := path.Join(apiDir, apiScope.apiFilePrefix+"lint-baseline.txt")
787 baselinePathRelativeToRoot := path.Join(mctx.ModuleDir(), baselinePath)
788 paths, err := mctx.GlobWithDeps(baselinePathRelativeToRoot, nil)
789 if err != nil {
790 mctx.ModuleErrorf("error checking for presence of %s: %s", baselinePathRelativeToRoot, err)
791 }
792 if len(paths) == 1 {
793 props.Check_api.Api_lint.Baseline_file = proptools.StringPtr(baselinePath)
794 } else if len(paths) != 0 {
795 mctx.ModuleErrorf("error checking for presence of %s: expected one path, found: %v", baselinePathRelativeToRoot, paths)
796 }
797 }
Anton Hansson6478ac12020-05-02 11:19:36 +0100798 }
Jiyong Park58c518b2018-05-12 22:29:12 +0900799
Anton Hansson5fd5d242020-03-27 19:43:19 +0000800 // Dist the api txt artifact for sdk builds.
801 if !Bool(module.sdkLibraryProperties.No_dist) {
802 props.Dist.Targets = []string{"sdk", "win_sdk"}
803 props.Dist.Dest = proptools.StringPtr(fmt.Sprintf("%v.txt", module.BaseModuleName()))
804 props.Dist.Dir = proptools.StringPtr(path.Join(module.apiDistPath(apiScope), "api"))
805 }
806
Colin Cross84dfc3d2019-09-25 11:33:01 -0700807 mctx.CreateModule(DroidstubsFactory, &props)
Jiyong Parkc678ad32018-04-10 13:07:10 +0900808}
809
Jooyung Han5e9013b2020-03-10 06:23:13 +0900810func (module *SdkLibrary) DepIsInSameApex(mctx android.BaseModuleContext, dep android.Module) bool {
811 depTag := mctx.OtherModuleDependencyTag(dep)
812 if depTag == xmlPermissionsFileTag {
813 return true
814 }
815 return module.Library.DepIsInSameApex(mctx, dep)
816}
817
Jiyong Parkc678ad32018-04-10 13:07:10 +0900818// Creates the xml file that publicizes the runtime library
Paul Duffinf0229202020-04-29 16:47:28 +0100819func (module *SdkLibrary) createXmlFile(mctx android.DefaultableHookContext) {
Jiyong Parke3833882020-02-17 17:28:10 +0900820 props := struct {
Sundong Ahn0d7dff42019-12-04 12:53:44 +0900821 Name *string
Jiyong Parke3833882020-02-17 17:28:10 +0900822 Lib_name *string
Sundong Ahn0d7dff42019-12-04 12:53:44 +0900823 Soc_specific *bool
824 Device_specific *bool
825 Product_specific *bool
826 System_ext_specific *bool
Jooyung Han5e9013b2020-03-10 06:23:13 +0900827 Apex_available []string
Jiyong Parke3833882020-02-17 17:28:10 +0900828 }{
Jooyung Han5e9013b2020-03-10 06:23:13 +0900829 Name: proptools.StringPtr(module.xmlFileName()),
830 Lib_name: proptools.StringPtr(module.BaseModuleName()),
831 Apex_available: module.ApexProperties.Apex_available,
Jiyong Parkc678ad32018-04-10 13:07:10 +0900832 }
Jiyong Parke3833882020-02-17 17:28:10 +0900833
834 if module.SocSpecific() {
835 props.Soc_specific = proptools.BoolPtr(true)
836 } else if module.DeviceSpecific() {
837 props.Device_specific = proptools.BoolPtr(true)
838 } else if module.ProductSpecific() {
839 props.Product_specific = proptools.BoolPtr(true)
840 } else if module.SystemExtSpecific() {
841 props.System_ext_specific = proptools.BoolPtr(true)
842 }
843
844 mctx.CreateModule(sdkLibraryXmlFactory, &props)
Jiyong Parkc678ad32018-04-10 13:07:10 +0900845}
846
Paul Duffin50061512020-01-21 16:31:05 +0000847func PrebuiltJars(ctx android.BaseModuleContext, baseName string, s sdkSpec) android.Paths {
Jiyong Park6a927c42020-01-21 02:03:43 +0900848 var ver sdkVersion
849 var kind sdkKind
850 if s.usePrebuilt(ctx) {
851 ver = s.version
852 kind = s.kind
Jiyong Parkc678ad32018-04-10 13:07:10 +0900853 } else {
Jiyong Park6a927c42020-01-21 02:03:43 +0900854 // We don't have prebuilt SDK for the specific sdkVersion.
855 // Instead of breaking the build, fallback to use "system_current"
856 ver = sdkVersionCurrent
857 kind = sdkSystem
Sundong Ahn054b19a2018-10-19 13:46:09 +0900858 }
Jiyong Park6a927c42020-01-21 02:03:43 +0900859
860 dir := filepath.Join("prebuilts", "sdk", ver.String(), kind.String())
Paul Duffin50061512020-01-21 16:31:05 +0000861 jar := filepath.Join(dir, baseName+".jar")
Sundong Ahn054b19a2018-10-19 13:46:09 +0900862 jarPath := android.ExistentPathForSource(ctx, jar)
Sundong Ahnae418ac2019-02-28 15:01:28 +0900863 if !jarPath.Valid() {
Colin Cross07c88562020-01-07 09:34:44 -0800864 if ctx.Config().AllowMissingDependencies() {
865 return android.Paths{android.PathForSource(ctx, jar)}
866 } else {
Jiyong Park6a927c42020-01-21 02:03:43 +0900867 ctx.PropertyErrorf("sdk_library", "invalid sdk version %q, %q does not exist", s.raw, jar)
Colin Cross07c88562020-01-07 09:34:44 -0800868 }
Sundong Ahnae418ac2019-02-28 15:01:28 +0900869 return nil
870 }
Sundong Ahn054b19a2018-10-19 13:46:09 +0900871 return android.Paths{jarPath.Path()}
872}
873
Paul Duffind1b3a922020-01-22 11:57:20 +0000874func (module *SdkLibrary) sdkJars(
875 ctx android.BaseModuleContext,
876 sdkVersion sdkSpec,
877 headerJars bool) android.Paths {
878
Paul Duffin50061512020-01-21 16:31:05 +0000879 // If a specific numeric version has been requested then use prebuilt versions of the sdk.
880 if sdkVersion.version.isNumbered() {
881 return PrebuiltJars(ctx, module.BaseModuleName(), sdkVersion)
Sundong Ahn054b19a2018-10-19 13:46:09 +0900882 } else {
Paul Duffind1b3a922020-01-22 11:57:20 +0000883 if !sdkVersion.specified() {
884 if headerJars {
885 return module.Library.HeaderJars()
886 } else {
887 return module.Library.ImplementationJars()
888 }
889 }
Paul Duffin726d23c2020-01-22 16:30:37 +0000890 var apiScope *apiScope
Jiyong Park6a927c42020-01-21 02:03:43 +0900891 switch sdkVersion.kind {
892 case sdkSystem:
Paul Duffin726d23c2020-01-22 16:30:37 +0000893 apiScope = apiScopeSystem
894 case sdkTest:
895 apiScope = apiScopeTest
Jiyong Park6a927c42020-01-21 02:03:43 +0900896 case sdkPrivate:
Sundong Ahn054b19a2018-10-19 13:46:09 +0900897 return module.Library.HeaderJars()
Jiyong Park6a927c42020-01-21 02:03:43 +0900898 default:
Paul Duffin726d23c2020-01-22 16:30:37 +0000899 apiScope = apiScopePublic
Paul Duffind1b3a922020-01-22 11:57:20 +0000900 }
901
Paul Duffin726d23c2020-01-22 16:30:37 +0000902 paths := module.getScopePaths(apiScope)
Paul Duffind1b3a922020-01-22 11:57:20 +0000903 if headerJars {
904 return paths.stubsHeaderPath
905 } else {
906 return paths.stubsImplPath
Sundong Ahn054b19a2018-10-19 13:46:09 +0900907 }
Jiyong Parkc678ad32018-04-10 13:07:10 +0900908 }
909}
910
Sundong Ahn241cd372018-07-13 16:16:44 +0900911// to satisfy SdkLibraryDependency interface
Paul Duffind1b3a922020-01-22 11:57:20 +0000912func (module *SdkLibrary) SdkHeaderJars(ctx android.BaseModuleContext, sdkVersion sdkSpec) android.Paths {
913 return module.sdkJars(ctx, sdkVersion, true /*headerJars*/)
914}
915
916// to satisfy SdkLibraryDependency interface
Jiyong Park6a927c42020-01-21 02:03:43 +0900917func (module *SdkLibrary) SdkImplementationJars(ctx android.BaseModuleContext, sdkVersion sdkSpec) android.Paths {
Paul Duffind1b3a922020-01-22 11:57:20 +0000918 return module.sdkJars(ctx, sdkVersion, false /*headerJars*/)
Sundong Ahn241cd372018-07-13 16:16:44 +0900919}
920
Sundong Ahn80a87b32019-05-13 15:02:50 +0900921func (module *SdkLibrary) SetNoDist() {
922 module.sdkLibraryProperties.No_dist = proptools.BoolPtr(true)
923}
924
Colin Cross571cccf2019-02-04 11:22:08 -0800925var javaSdkLibrariesKey = android.NewOnceKey("javaSdkLibraries")
926
Jiyong Park82484c02018-04-23 21:41:26 +0900927func javaSdkLibraries(config android.Config) *[]string {
Colin Cross571cccf2019-02-04 11:22:08 -0800928 return config.Once(javaSdkLibrariesKey, func() interface{} {
Jiyong Park82484c02018-04-23 21:41:26 +0900929 return &[]string{}
930 }).(*[]string)
931}
932
Paul Duffin749f98f2019-12-30 17:23:46 +0000933func (module *SdkLibrary) getApiDir() string {
934 return proptools.StringDefault(module.sdkLibraryProperties.Api_dir, "api")
935}
936
Jiyong Parkc678ad32018-04-10 13:07:10 +0900937// For a java_sdk_library module, create internal modules for stubs, docs,
938// runtime libs and xml file. If requested, the stubs and docs are created twice
939// once for public API level and once for system API level
Paul Duffinf0229202020-04-29 16:47:28 +0100940func (module *SdkLibrary) CreateInternalModules(mctx android.DefaultableHookContext) {
941 // If the module has been disabled then don't create any child modules.
942 if !module.Enabled() {
943 return
944 }
945
Inseob Kim6e93ac92019-03-21 17:43:49 +0900946 if len(module.Library.Module.properties.Srcs) == 0 {
Inseob Kimc0907f12019-02-08 21:00:45 +0900947 mctx.PropertyErrorf("srcs", "java_sdk_library must specify srcs")
Jooyung Han58f26ab2019-12-18 15:34:32 +0900948 return
Inseob Kimc0907f12019-02-08 21:00:45 +0900949 }
950
Paul Duffin37e0b772019-12-30 17:20:10 +0000951 // If this builds against standard libraries (i.e. is not part of the core libraries)
952 // then assume it provides both system and test apis. Otherwise, assume it does not and
953 // also assume it does not contribute to the dist build.
954 sdkDep := decodeSdkDep(mctx, sdkContext(&module.Library))
955 hasSystemAndTestApis := sdkDep.hasStandardLibs()
Paul Duffin3375e352020-04-28 10:44:03 +0100956 module.sdkLibraryProperties.Generate_system_and_test_apis = hasSystemAndTestApis
Paul Duffin37e0b772019-12-30 17:20:10 +0000957 module.sdkLibraryProperties.No_dist = proptools.BoolPtr(!hasSystemAndTestApis)
958
Inseob Kim8098faa2019-03-18 10:19:51 +0900959 missing_current_api := false
960
Paul Duffin3375e352020-04-28 10:44:03 +0100961 generatedScopes := module.getGeneratedApiScopes(mctx)
Paul Duffind1b3a922020-01-22 11:57:20 +0000962
Paul Duffin749f98f2019-12-30 17:23:46 +0000963 apiDir := module.getApiDir()
Paul Duffin3375e352020-04-28 10:44:03 +0100964 for _, scope := range generatedScopes {
Inseob Kim8098faa2019-03-18 10:19:51 +0900965 for _, api := range []string{"current.txt", "removed.txt"} {
Paul Duffind1b3a922020-01-22 11:57:20 +0000966 path := path.Join(mctx.ModuleDir(), apiDir, scope.apiFilePrefix+api)
Inseob Kim8098faa2019-03-18 10:19:51 +0900967 p := android.ExistentPathForSource(mctx, path)
968 if !p.Valid() {
969 mctx.ModuleErrorf("Current api file %#v doesn't exist", path)
970 missing_current_api = true
971 }
972 }
973 }
974
975 if missing_current_api {
976 script := "build/soong/scripts/gen-java-current-api-files.sh"
977 p := android.ExistentPathForSource(mctx, script)
978
979 if !p.Valid() {
980 panic(fmt.Sprintf("script file %s doesn't exist", script))
981 }
982
983 mctx.ModuleErrorf("One or more current api files are missing. "+
984 "You can update them by:\n"+
Paul Duffin37e0b772019-12-30 17:20:10 +0000985 "%s %q %s && m update-api",
Paul Duffind1b3a922020-01-22 11:57:20 +0000986 script, filepath.Join(mctx.ModuleDir(), apiDir),
Paul Duffin3375e352020-04-28 10:44:03 +0100987 strings.Join(generatedScopes.Strings(func(s *apiScope) string { return s.apiFilePrefix }), " "))
Inseob Kim8098faa2019-03-18 10:19:51 +0900988 return
989 }
990
Paul Duffin3375e352020-04-28 10:44:03 +0100991 for _, scope := range generatedScopes {
Paul Duffind1b3a922020-01-22 11:57:20 +0000992 module.createStubsLibrary(mctx, scope)
Paul Duffinc8782502020-04-29 20:45:27 +0100993 module.createStubsSourcesAndApi(mctx, scope)
Inseob Kimc0907f12019-02-08 21:00:45 +0900994 }
995
Paul Duffin43db9be2019-12-30 17:35:49 +0000996 if !proptools.Bool(module.sdkLibraryProperties.Api_only) {
997 // for runtime
998 module.createXmlFile(mctx)
999
1000 // record java_sdk_library modules so that they are exported to make
1001 javaSdkLibraries := javaSdkLibraries(mctx.Config())
1002 javaSdkLibrariesLock.Lock()
1003 defer javaSdkLibrariesLock.Unlock()
1004 *javaSdkLibraries = append(*javaSdkLibraries, module.BaseModuleName())
1005 }
Inseob Kimc0907f12019-02-08 21:00:45 +09001006}
1007
1008func (module *SdkLibrary) InitSdkLibraryProperties() {
Sundong Ahn054b19a2018-10-19 13:46:09 +09001009 module.AddProperties(
1010 &module.sdkLibraryProperties,
1011 &module.Library.Module.properties,
1012 &module.Library.Module.dexpreoptProperties,
1013 &module.Library.Module.deviceProperties,
1014 &module.Library.Module.protoProperties,
1015 )
1016
1017 module.Library.Module.properties.Installable = proptools.BoolPtr(true)
1018 module.Library.Module.deviceProperties.IsSDKLibrary = true
Inseob Kimc0907f12019-02-08 21:00:45 +09001019}
Sundong Ahn054b19a2018-10-19 13:46:09 +09001020
Jaewoong Jung4f158ee2019-07-11 10:05:35 -07001021// java_sdk_library is a special Java library that provides optional platform APIs to apps.
1022// In practice, it can be viewed as a combination of several modules: 1) stubs library that clients
1023// are linked against to, 2) droiddoc module that internally generates API stubs source files,
1024// 3) the real runtime shared library that implements the APIs, and 4) XML file for adding
1025// the runtime lib to the classpath at runtime if requested via <uses-library>.
Inseob Kimc0907f12019-02-08 21:00:45 +09001026func SdkLibraryFactory() android.Module {
1027 module := &SdkLibrary{}
1028 module.InitSdkLibraryProperties()
Jooyung Han58f26ab2019-12-18 15:34:32 +09001029 android.InitApexModule(module)
Sundong Ahn054b19a2018-10-19 13:46:09 +09001030 InitJavaModule(module, android.HostAndDeviceSupported)
Paul Duffin3375e352020-04-28 10:44:03 +01001031
1032 // Initialize the map from scope to scope specific properties.
1033 scopeToProperties := make(map[*apiScope]*ApiScopeProperties)
1034 for _, scope := range allApiScopes {
1035 scopeToProperties[scope] = scope.scopeSpecificProperties(module)
1036 }
1037 module.scopeToProperties = scopeToProperties
1038
Paul Duffin4911a892020-04-29 23:35:13 +01001039 // Add the properties containing visibility rules so that they are checked.
1040 android.AddVisibilityProperty(module, "stubs_library_visibility", &module.sdkLibraryProperties.Stubs_library_visibility)
1041 android.AddVisibilityProperty(module, "stubs_source_visibility", &module.sdkLibraryProperties.Stubs_source_visibility)
1042
Paul Duffinf0229202020-04-29 16:47:28 +01001043 module.SetDefaultableHook(func(ctx android.DefaultableHookContext) { module.CreateInternalModules(ctx) })
Jiyong Parkc678ad32018-04-10 13:07:10 +09001044 return module
1045}
Colin Cross79c7c262019-04-17 11:11:46 -07001046
1047//
1048// SDK library prebuilts
1049//
1050
Paul Duffin56d44902020-01-31 13:36:25 +00001051// Properties associated with each api scope.
1052type sdkLibraryScopeProperties struct {
Colin Cross79c7c262019-04-17 11:11:46 -07001053 Jars []string `android:"path"`
1054
1055 Sdk_version *string
1056
Colin Cross79c7c262019-04-17 11:11:46 -07001057 // List of shared java libs that this module has dependencies to
1058 Libs []string
Paul Duffin3d1248c2020-04-09 00:10:17 +01001059
Paul Duffinc8782502020-04-29 20:45:27 +01001060 // The stubs source.
Paul Duffin3d1248c2020-04-09 00:10:17 +01001061 Stub_srcs []string `android:"path"`
Paul Duffin1fd005d2020-04-09 01:08:11 +01001062
1063 // The current.txt
1064 Current_api string `android:"path"`
1065
1066 // The removed.txt
1067 Removed_api string `android:"path"`
Colin Cross79c7c262019-04-17 11:11:46 -07001068}
1069
Paul Duffin56d44902020-01-31 13:36:25 +00001070type sdkLibraryImportProperties struct {
Paul Duffinfcfd7912020-01-31 17:54:30 +00001071 // List of shared java libs, common to all scopes, that this module has
1072 // dependencies to
1073 Libs []string
Paul Duffin56d44902020-01-31 13:36:25 +00001074}
1075
Colin Cross79c7c262019-04-17 11:11:46 -07001076type sdkLibraryImport struct {
1077 android.ModuleBase
1078 android.DefaultableModuleBase
1079 prebuilt android.Prebuilt
Paul Duffindd46f712020-02-10 13:37:10 +00001080 android.ApexModuleBase
1081 android.SdkBase
Colin Cross79c7c262019-04-17 11:11:46 -07001082
1083 properties sdkLibraryImportProperties
1084
Paul Duffin46a26a82020-04-07 19:27:04 +01001085 // Map from api scope to the scope specific property structure.
1086 scopeProperties map[*apiScope]*sdkLibraryScopeProperties
1087
Paul Duffin56d44902020-01-31 13:36:25 +00001088 commonToSdkLibraryAndImport
Colin Cross79c7c262019-04-17 11:11:46 -07001089}
1090
1091var _ SdkLibraryDependency = (*sdkLibraryImport)(nil)
1092
Paul Duffin46a26a82020-04-07 19:27:04 +01001093// The type of a structure that contains a field of type sdkLibraryScopeProperties
1094// for each apiscope in allApiScopes, e.g. something like:
1095// struct {
1096// Public sdkLibraryScopeProperties
1097// System sdkLibraryScopeProperties
1098// ...
1099// }
1100var allScopeStructType = createAllScopePropertiesStructType()
1101
1102// Dynamically create a structure type for each apiscope in allApiScopes.
1103func createAllScopePropertiesStructType() reflect.Type {
1104 var fields []reflect.StructField
1105 for _, apiScope := range allApiScopes {
1106 field := reflect.StructField{
1107 Name: apiScope.fieldName,
1108 Type: reflect.TypeOf(sdkLibraryScopeProperties{}),
1109 }
1110 fields = append(fields, field)
1111 }
1112
1113 return reflect.StructOf(fields)
1114}
1115
1116// Create an instance of the scope specific structure type and return a map
1117// from apiscope to a pointer to each scope specific field.
1118func createPropertiesInstance() (interface{}, map[*apiScope]*sdkLibraryScopeProperties) {
1119 allScopePropertiesPtr := reflect.New(allScopeStructType)
1120 allScopePropertiesStruct := allScopePropertiesPtr.Elem()
1121 scopeProperties := make(map[*apiScope]*sdkLibraryScopeProperties)
1122
1123 for _, apiScope := range allApiScopes {
1124 field := allScopePropertiesStruct.FieldByName(apiScope.fieldName)
1125 scopeProperties[apiScope] = field.Addr().Interface().(*sdkLibraryScopeProperties)
1126 }
1127
1128 return allScopePropertiesPtr.Interface(), scopeProperties
1129}
1130
Jaewoong Jung4f158ee2019-07-11 10:05:35 -07001131// java_sdk_library_import imports a prebuilt java_sdk_library.
Colin Cross79c7c262019-04-17 11:11:46 -07001132func sdkLibraryImportFactory() android.Module {
1133 module := &sdkLibraryImport{}
1134
Paul Duffin46a26a82020-04-07 19:27:04 +01001135 allScopeProperties, scopeToProperties := createPropertiesInstance()
1136 module.scopeProperties = scopeToProperties
1137 module.AddProperties(&module.properties, allScopeProperties)
Colin Cross79c7c262019-04-17 11:11:46 -07001138
Paul Duffin0bdcb272020-02-06 15:24:57 +00001139 android.InitPrebuiltModule(module, &[]string{""})
Paul Duffindd46f712020-02-10 13:37:10 +00001140 android.InitApexModule(module)
1141 android.InitSdkAwareModule(module)
Colin Cross79c7c262019-04-17 11:11:46 -07001142 InitJavaModule(module, android.HostAndDeviceSupported)
1143
Paul Duffin6e7ecbf2020-05-08 15:01:19 +01001144 module.SetDefaultableHook(func(mctx android.DefaultableHookContext) { module.createInternalModules(mctx) })
Colin Cross79c7c262019-04-17 11:11:46 -07001145 return module
1146}
1147
1148func (module *sdkLibraryImport) Prebuilt() *android.Prebuilt {
1149 return &module.prebuilt
1150}
1151
1152func (module *sdkLibraryImport) Name() string {
1153 return module.prebuilt.Name(module.ModuleBase.Name())
1154}
1155
Paul Duffin6e7ecbf2020-05-08 15:01:19 +01001156func (module *sdkLibraryImport) createInternalModules(mctx android.DefaultableHookContext) {
Colin Cross79c7c262019-04-17 11:11:46 -07001157
Paul Duffin50061512020-01-21 16:31:05 +00001158 // If the build is configured to use prebuilts then force this to be preferred.
1159 if mctx.Config().UnbundledBuildUsePrebuiltSdks() {
1160 module.prebuilt.ForcePrefer()
1161 }
1162
Paul Duffin46a26a82020-04-07 19:27:04 +01001163 for apiScope, scopeProperties := range module.scopeProperties {
Paul Duffin56d44902020-01-31 13:36:25 +00001164 if len(scopeProperties.Jars) == 0 {
1165 continue
1166 }
1167
Paul Duffinbbb546b2020-04-09 00:07:11 +01001168 module.createJavaImportForStubs(mctx, apiScope, scopeProperties)
Paul Duffin3d1248c2020-04-09 00:10:17 +01001169
1170 module.createPrebuiltStubsSources(mctx, apiScope, scopeProperties)
Paul Duffin56d44902020-01-31 13:36:25 +00001171 }
Colin Cross79c7c262019-04-17 11:11:46 -07001172
1173 javaSdkLibraries := javaSdkLibraries(mctx.Config())
1174 javaSdkLibrariesLock.Lock()
1175 defer javaSdkLibrariesLock.Unlock()
1176 *javaSdkLibraries = append(*javaSdkLibraries, module.BaseModuleName())
1177}
1178
Paul Duffin6e7ecbf2020-05-08 15:01:19 +01001179func (module *sdkLibraryImport) createJavaImportForStubs(mctx android.DefaultableHookContext, apiScope *apiScope, scopeProperties *sdkLibraryScopeProperties) {
Paul Duffinbbb546b2020-04-09 00:07:11 +01001180 // Creates a java import for the jar with ".stubs" suffix
1181 props := struct {
1182 Name *string
1183 Soc_specific *bool
1184 Device_specific *bool
1185 Product_specific *bool
1186 System_ext_specific *bool
1187 Sdk_version *string
1188 Libs []string
1189 Jars []string
1190 Prefer *bool
1191 }{}
1192 props.Name = proptools.StringPtr(apiScope.stubsModuleName(module.BaseModuleName()))
1193 props.Sdk_version = scopeProperties.Sdk_version
1194 // Prepend any of the libs from the legacy public properties to the libs for each of the
1195 // scopes to avoid having to duplicate them in each scope.
1196 props.Libs = append(module.properties.Libs, scopeProperties.Libs...)
1197 props.Jars = scopeProperties.Jars
1198 if module.SocSpecific() {
1199 props.Soc_specific = proptools.BoolPtr(true)
1200 } else if module.DeviceSpecific() {
1201 props.Device_specific = proptools.BoolPtr(true)
1202 } else if module.ProductSpecific() {
1203 props.Product_specific = proptools.BoolPtr(true)
1204 } else if module.SystemExtSpecific() {
1205 props.System_ext_specific = proptools.BoolPtr(true)
1206 }
1207 // If the build should use prebuilt sdks then set prefer to true on the stubs library.
1208 // That will cause the prebuilt version of the stubs to override the source version.
1209 if mctx.Config().UnbundledBuildUsePrebuiltSdks() {
1210 props.Prefer = proptools.BoolPtr(true)
1211 }
1212 mctx.CreateModule(ImportFactory, &props)
1213}
1214
Paul Duffin6e7ecbf2020-05-08 15:01:19 +01001215func (module *sdkLibraryImport) createPrebuiltStubsSources(mctx android.DefaultableHookContext, apiScope *apiScope, scopeProperties *sdkLibraryScopeProperties) {
Paul Duffin3d1248c2020-04-09 00:10:17 +01001216 props := struct {
1217 Name *string
1218 Srcs []string
1219 }{}
Paul Duffinc8782502020-04-29 20:45:27 +01001220 props.Name = proptools.StringPtr(apiScope.stubsSourceModuleName(module.BaseModuleName()))
Paul Duffin3d1248c2020-04-09 00:10:17 +01001221 props.Srcs = scopeProperties.Stub_srcs
1222 mctx.CreateModule(PrebuiltStubsSourcesFactory, &props)
1223}
1224
Colin Cross79c7c262019-04-17 11:11:46 -07001225func (module *sdkLibraryImport) DepsMutator(ctx android.BottomUpMutatorContext) {
Paul Duffin46a26a82020-04-07 19:27:04 +01001226 for apiScope, scopeProperties := range module.scopeProperties {
Paul Duffin56d44902020-01-31 13:36:25 +00001227 if len(scopeProperties.Jars) == 0 {
1228 continue
1229 }
1230
1231 // Add dependencies to the prebuilt stubs library
1232 ctx.AddVariationDependencies(nil, apiScope.stubsTag, apiScope.stubsModuleName(module.BaseModuleName()))
1233 }
Colin Cross79c7c262019-04-17 11:11:46 -07001234}
1235
1236func (module *sdkLibraryImport) GenerateAndroidBuildActions(ctx android.ModuleContext) {
1237 // Record the paths to the prebuilt stubs library.
1238 ctx.VisitDirectDeps(func(to android.Module) {
1239 tag := ctx.OtherModuleDependencyTag(to)
1240
Paul Duffin56d44902020-01-31 13:36:25 +00001241 if lib, ok := to.(Dependency); ok {
1242 if scopeTag, ok := tag.(scopeDependencyTag); ok {
1243 apiScope := scopeTag.apiScope
1244 scopePaths := module.getScopePaths(apiScope)
1245 scopePaths.stubsHeaderPath = lib.HeaderJars()
1246 }
Colin Cross79c7c262019-04-17 11:11:46 -07001247 }
1248 })
1249}
1250
Paul Duffin56d44902020-01-31 13:36:25 +00001251func (module *sdkLibraryImport) sdkJars(
1252 ctx android.BaseModuleContext,
1253 sdkVersion sdkSpec) android.Paths {
1254
Paul Duffin50061512020-01-21 16:31:05 +00001255 // If a specific numeric version has been requested then use prebuilt versions of the sdk.
1256 if sdkVersion.version.isNumbered() {
1257 return PrebuiltJars(ctx, module.BaseModuleName(), sdkVersion)
1258 }
1259
Paul Duffin56d44902020-01-31 13:36:25 +00001260 var apiScope *apiScope
1261 switch sdkVersion.kind {
1262 case sdkSystem:
1263 apiScope = apiScopeSystem
1264 case sdkTest:
1265 apiScope = apiScopeTest
1266 default:
1267 apiScope = apiScopePublic
1268 }
1269
1270 paths := module.getScopePaths(apiScope)
1271 return paths.stubsHeaderPath
1272}
1273
Colin Cross79c7c262019-04-17 11:11:46 -07001274// to satisfy SdkLibraryDependency interface
Jiyong Park6a927c42020-01-21 02:03:43 +09001275func (module *sdkLibraryImport) SdkHeaderJars(ctx android.BaseModuleContext, sdkVersion sdkSpec) android.Paths {
Colin Cross79c7c262019-04-17 11:11:46 -07001276 // This module is just a wrapper for the prebuilt stubs.
Paul Duffin56d44902020-01-31 13:36:25 +00001277 return module.sdkJars(ctx, sdkVersion)
Colin Cross79c7c262019-04-17 11:11:46 -07001278}
1279
1280// to satisfy SdkLibraryDependency interface
Jiyong Park6a927c42020-01-21 02:03:43 +09001281func (module *sdkLibraryImport) SdkImplementationJars(ctx android.BaseModuleContext, sdkVersion sdkSpec) android.Paths {
Colin Cross79c7c262019-04-17 11:11:46 -07001282 // This module is just a wrapper for the stubs.
Paul Duffin56d44902020-01-31 13:36:25 +00001283 return module.sdkJars(ctx, sdkVersion)
Colin Cross79c7c262019-04-17 11:11:46 -07001284}
Jiyong Parke3833882020-02-17 17:28:10 +09001285
1286//
1287// java_sdk_library_xml
1288//
1289type sdkLibraryXml struct {
1290 android.ModuleBase
1291 android.DefaultableModuleBase
1292 android.ApexModuleBase
1293
1294 properties sdkLibraryXmlProperties
1295
1296 outputFilePath android.OutputPath
1297 installDirPath android.InstallPath
1298}
1299
1300type sdkLibraryXmlProperties struct {
1301 // canonical name of the lib
1302 Lib_name *string
1303}
1304
1305// java_sdk_library_xml builds the permission xml file for a java_sdk_library.
1306// Not to be used directly by users. java_sdk_library internally uses this.
1307func sdkLibraryXmlFactory() android.Module {
1308 module := &sdkLibraryXml{}
1309
1310 module.AddProperties(&module.properties)
1311
1312 android.InitApexModule(module)
1313 android.InitAndroidArchModule(module, android.DeviceSupported, android.MultilibCommon)
1314
1315 return module
1316}
1317
1318// from android.PrebuiltEtcModule
1319func (module *sdkLibraryXml) SubDir() string {
1320 return "permissions"
1321}
1322
1323// from android.PrebuiltEtcModule
1324func (module *sdkLibraryXml) OutputFile() android.OutputPath {
1325 return module.outputFilePath
1326}
1327
1328// from android.ApexModule
1329func (module *sdkLibraryXml) AvailableFor(what string) bool {
1330 return true
1331}
1332
1333func (module *sdkLibraryXml) DepsMutator(ctx android.BottomUpMutatorContext) {
1334 // do nothing
1335}
1336
1337// File path to the runtime implementation library
1338func (module *sdkLibraryXml) implPath() string {
1339 implName := proptools.String(module.properties.Lib_name)
1340 if apexName := module.ApexName(); apexName != "" {
1341 // TODO(b/146468504): ApexName() is only a soong module name, not apex name.
1342 // In most cases, this works fine. But when apex_name is set or override_apex is used
1343 // this can be wrong.
1344 return fmt.Sprintf("/apex/%s/javalib/%s.jar", apexName, implName)
1345 }
1346 partition := "system"
1347 if module.SocSpecific() {
1348 partition = "vendor"
1349 } else if module.DeviceSpecific() {
1350 partition = "odm"
1351 } else if module.ProductSpecific() {
1352 partition = "product"
1353 } else if module.SystemExtSpecific() {
1354 partition = "system_ext"
1355 }
1356 return "/" + partition + "/framework/" + implName + ".jar"
1357}
1358
1359func (module *sdkLibraryXml) GenerateAndroidBuildActions(ctx android.ModuleContext) {
1360 libName := proptools.String(module.properties.Lib_name)
1361 xmlContent := fmt.Sprintf(permissionsTemplate, libName, module.implPath())
1362
1363 module.outputFilePath = android.PathForModuleOut(ctx, libName+".xml").OutputPath
1364 rule := android.NewRuleBuilder()
1365 rule.Command().
1366 Text("/bin/bash -c \"echo -e '" + xmlContent + "'\" > ").
1367 Output(module.outputFilePath)
1368
1369 rule.Build(pctx, ctx, "java_sdk_xml", "Permission XML")
1370
1371 module.installDirPath = android.PathForModuleInstall(ctx, "etc", module.SubDir())
1372}
1373
1374func (module *sdkLibraryXml) AndroidMkEntries() []android.AndroidMkEntries {
1375 if !module.IsForPlatform() {
1376 return []android.AndroidMkEntries{android.AndroidMkEntries{
1377 Disabled: true,
1378 }}
1379 }
1380
1381 return []android.AndroidMkEntries{android.AndroidMkEntries{
1382 Class: "ETC",
1383 OutputFile: android.OptionalPathForPath(module.outputFilePath),
1384 ExtraEntries: []android.AndroidMkExtraEntriesFunc{
1385 func(entries *android.AndroidMkEntries) {
1386 entries.SetString("LOCAL_MODULE_TAGS", "optional")
1387 entries.SetString("LOCAL_MODULE_PATH", module.installDirPath.ToMakePath().String())
1388 entries.SetString("LOCAL_INSTALLED_MODULE_STEM", module.outputFilePath.Base())
1389 },
1390 },
1391 }}
1392}
Paul Duffindd46f712020-02-10 13:37:10 +00001393
1394type sdkLibrarySdkMemberType struct {
1395 android.SdkMemberTypeBase
1396}
1397
1398func (s *sdkLibrarySdkMemberType) AddDependencies(mctx android.BottomUpMutatorContext, dependencyTag blueprint.DependencyTag, names []string) {
1399 mctx.AddVariationDependencies(nil, dependencyTag, names...)
1400}
1401
1402func (s *sdkLibrarySdkMemberType) IsInstance(module android.Module) bool {
1403 _, ok := module.(*SdkLibrary)
1404 return ok
1405}
1406
1407func (s *sdkLibrarySdkMemberType) AddPrebuiltModule(ctx android.SdkMemberContext, member android.SdkMember) android.BpModule {
1408 return ctx.SnapshotBuilder().AddPrebuiltModule(member, "java_sdk_library_import")
1409}
1410
1411func (s *sdkLibrarySdkMemberType) CreateVariantPropertiesStruct() android.SdkMemberProperties {
1412 return &sdkLibrarySdkMemberProperties{}
1413}
1414
1415type sdkLibrarySdkMemberProperties struct {
1416 android.SdkMemberPropertiesBase
1417
1418 // Scope to per scope properties.
1419 Scopes map[*apiScope]scopeProperties
1420
1421 // Additional libraries that the exported stubs libraries depend upon.
1422 Libs []string
Paul Duffin3d1248c2020-04-09 00:10:17 +01001423
1424 // The Java stubs source files.
1425 Stub_srcs []string
Paul Duffindd46f712020-02-10 13:37:10 +00001426}
1427
1428type scopeProperties struct {
Paul Duffin1fd005d2020-04-09 01:08:11 +01001429 Jars android.Paths
1430 StubsSrcJar android.Path
1431 CurrentApiFile android.Path
1432 RemovedApiFile android.Path
1433 SdkVersion string
Paul Duffindd46f712020-02-10 13:37:10 +00001434}
1435
1436func (s *sdkLibrarySdkMemberProperties) PopulateFromVariant(ctx android.SdkMemberContext, variant android.Module) {
1437 sdk := variant.(*SdkLibrary)
1438
1439 s.Scopes = make(map[*apiScope]scopeProperties)
1440 for _, apiScope := range allApiScopes {
1441 paths := sdk.getScopePaths(apiScope)
1442 jars := paths.stubsImplPath
1443 if len(jars) > 0 {
1444 properties := scopeProperties{}
1445 properties.Jars = jars
Paul Duffin780c5f42020-05-12 15:52:55 +01001446 properties.SdkVersion = sdk.sdkVersionForStubsLibrary(ctx.SdkModuleContext(), apiScope)
Paul Duffin3d1248c2020-04-09 00:10:17 +01001447 properties.StubsSrcJar = paths.stubsSrcJar
Paul Duffin1fd005d2020-04-09 01:08:11 +01001448 properties.CurrentApiFile = paths.currentApiFilePath
1449 properties.RemovedApiFile = paths.removedApiFilePath
Paul Duffindd46f712020-02-10 13:37:10 +00001450 s.Scopes[apiScope] = properties
1451 }
1452 }
1453
1454 s.Libs = sdk.properties.Libs
1455}
1456
1457func (s *sdkLibrarySdkMemberProperties) AddToPropertySet(ctx android.SdkMemberContext, propertySet android.BpPropertySet) {
1458 for _, apiScope := range allApiScopes {
1459 if properties, ok := s.Scopes[apiScope]; ok {
1460 scopeSet := propertySet.AddPropertySet(apiScope.name)
1461
Paul Duffin3d1248c2020-04-09 00:10:17 +01001462 scopeDir := filepath.Join("sdk_library", s.OsPrefix(), apiScope.name)
1463
Paul Duffindd46f712020-02-10 13:37:10 +00001464 var jars []string
1465 for _, p := range properties.Jars {
Paul Duffin3d1248c2020-04-09 00:10:17 +01001466 dest := filepath.Join(scopeDir, ctx.Name()+"-stubs.jar")
Paul Duffindd46f712020-02-10 13:37:10 +00001467 ctx.SnapshotBuilder().CopyToSnapshot(p, dest)
1468 jars = append(jars, dest)
1469 }
1470 scopeSet.AddProperty("jars", jars)
1471
Paul Duffin3d1248c2020-04-09 00:10:17 +01001472 // Merge the stubs source jar into the snapshot zip so that when it is unpacked
1473 // the source files are also unpacked.
1474 snapshotRelativeDir := filepath.Join(scopeDir, ctx.Name()+"_stub_sources")
1475 ctx.SnapshotBuilder().UnzipToSnapshot(properties.StubsSrcJar, snapshotRelativeDir)
1476 scopeSet.AddProperty("stub_srcs", []string{snapshotRelativeDir})
1477
Paul Duffin1fd005d2020-04-09 01:08:11 +01001478 if properties.CurrentApiFile != nil {
1479 currentApiSnapshotPath := filepath.Join(scopeDir, ctx.Name()+".txt")
1480 ctx.SnapshotBuilder().CopyToSnapshot(properties.CurrentApiFile, currentApiSnapshotPath)
1481 scopeSet.AddProperty("current_api", currentApiSnapshotPath)
1482 }
1483
1484 if properties.RemovedApiFile != nil {
1485 removedApiSnapshotPath := filepath.Join(scopeDir, ctx.Name()+"-removed.txt")
1486 ctx.SnapshotBuilder().CopyToSnapshot(properties.CurrentApiFile, removedApiSnapshotPath)
1487 scopeSet.AddProperty("removed_api", removedApiSnapshotPath)
1488 }
1489
Paul Duffindd46f712020-02-10 13:37:10 +00001490 if properties.SdkVersion != "" {
1491 scopeSet.AddProperty("sdk_version", properties.SdkVersion)
1492 }
1493 }
1494 }
1495
1496 if len(s.Libs) > 0 {
1497 propertySet.AddPropertyWithTag("libs", s.Libs, ctx.SnapshotBuilder().SdkMemberReferencePropertyTag(false))
1498 }
1499}