blob: 3ce15836151887a7fe2185261b36f954680bbdc0 [file] [log] [blame]
Jiyong Parkc678ad32018-04-10 13:07:10 +09001// Copyright 2018 Google Inc. All rights reserved.
2//
3// Licensed under the Apache License, Version 2.0 (the "License");
4// you may not use this file except in compliance with the License.
5// You may obtain a copy of the License at
6//
7// http://www.apache.org/licenses/LICENSE-2.0
8//
9// Unless required by applicable law or agreed to in writing, software
10// distributed under the License is distributed on an "AS IS" BASIS,
11// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12// See the License for the specific language governing permissions and
13// limitations under the License.
14
15package java
16
17import (
Jiyong Parkc678ad32018-04-10 13:07:10 +090018 "fmt"
19 "path"
Sundong Ahn054b19a2018-10-19 13:46:09 +090020 "path/filepath"
Paul Duffin46a26a82020-04-07 19:27:04 +010021 "reflect"
Jiyong Park82484c02018-04-23 21:41:26 +090022 "sort"
Jiyong Parkc678ad32018-04-10 13:07:10 +090023 "strings"
Jiyong Park82484c02018-04-23 21:41:26 +090024 "sync"
Jiyong Parkc678ad32018-04-10 13:07:10 +090025
Paul Duffind1b3a922020-01-22 11:57:20 +000026 "github.com/google/blueprint"
Jiyong Parkc678ad32018-04-10 13:07:10 +090027 "github.com/google/blueprint/proptools"
Paul Duffin46a26a82020-04-07 19:27:04 +010028
29 "android/soong/android"
Jiyong Parkc678ad32018-04-10 13:07:10 +090030)
31
Jooyung Han58f26ab2019-12-18 15:34:32 +090032const (
Jiyong Parkc678ad32018-04-10 13:07:10 +090033 sdkStubsLibrarySuffix = ".stubs"
34 sdkSystemApiSuffix = ".system"
Jiyong Parkdf130542018-04-27 16:29:21 +090035 sdkTestApiSuffix = ".test"
Paul Duffin91b883d2020-02-11 13:05:28 +000036 sdkStubsSourceSuffix = ".stubs.source"
Jiyong Parkc678ad32018-04-10 13:07:10 +090037 sdkXmlFileSuffix = ".xml"
Jiyong Parke3833882020-02-17 17:28:10 +090038 permissionsTemplate = `<?xml version=\"1.0\" encoding=\"utf-8\"?>\n` +
Jooyung Han624058e2019-12-24 18:38:06 +090039 `<!-- Copyright (C) 2018 The Android Open Source Project\n` +
40 `\n` +
Jiyong Parke3833882020-02-17 17:28:10 +090041 ` Licensed under the Apache License, Version 2.0 (the \"License\");\n` +
Jooyung Han624058e2019-12-24 18:38:06 +090042 ` you may not use this file except in compliance with the License.\n` +
43 ` You may obtain a copy of the License at\n` +
44 `\n` +
45 ` http://www.apache.org/licenses/LICENSE-2.0\n` +
46 `\n` +
47 ` Unless required by applicable law or agreed to in writing, software\n` +
Jiyong Parke3833882020-02-17 17:28:10 +090048 ` distributed under the License is distributed on an \"AS IS\" BASIS,\n` +
Jooyung Han624058e2019-12-24 18:38:06 +090049 ` WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\n` +
50 ` See the License for the specific language governing permissions and\n` +
51 ` limitations under the License.\n` +
52 `-->\n` +
53 `<permissions>\n` +
Jiyong Parke3833882020-02-17 17:28:10 +090054 ` <library name=\"%s\" file=\"%s\"/>\n` +
Jooyung Han624058e2019-12-24 18:38:06 +090055 `</permissions>\n`
Jiyong Parkc678ad32018-04-10 13:07:10 +090056)
57
Paul Duffind1b3a922020-01-22 11:57:20 +000058// A tag to associated a dependency with a specific api scope.
59type scopeDependencyTag struct {
60 blueprint.BaseDependencyTag
61 name string
62 apiScope *apiScope
63}
64
65// Provides information about an api scope, e.g. public, system, test.
66type apiScope struct {
67 // The name of the api scope, e.g. public, system, test
68 name string
69
Paul Duffin97b53b82020-05-05 14:40:52 +010070 // The api scope that this scope extends.
71 extends *apiScope
72
Paul Duffin3375e352020-04-28 10:44:03 +010073 // The legacy enabled status for a specific scope can be dependent on other
74 // properties that have been specified on the library so it is provided by
75 // a function that can determine the status by examining those properties.
76 legacyEnabledStatus func(module *SdkLibrary) bool
77
78 // The default enabled status for non-legacy behavior, which is triggered by
79 // explicitly enabling at least one api scope.
80 defaultEnabledStatus bool
81
82 // Gets a pointer to the scope specific properties.
83 scopeSpecificProperties func(module *SdkLibrary) *ApiScopeProperties
84
Paul Duffin46a26a82020-04-07 19:27:04 +010085 // The name of the field in the dynamically created structure.
86 fieldName string
87
Paul Duffind1b3a922020-01-22 11:57:20 +000088 // The tag to use to depend on the stubs library module.
89 stubsTag scopeDependencyTag
90
91 // The tag to use to depend on the stubs
92 apiFileTag scopeDependencyTag
93
94 // The scope specific prefix to add to the api file base of "current.txt" or "removed.txt".
95 apiFilePrefix string
96
97 // The scope specific prefix to add to the sdk library module name to construct a scope specific
98 // module name.
99 moduleSuffix string
100
Paul Duffind1b3a922020-01-22 11:57:20 +0000101 // SDK version that the stubs library is built against. Note that this is always
102 // *current. Older stubs library built with a numbered SDK version is created from
103 // the prebuilt jar.
104 sdkVersion string
Paul Duffin1fb487d2020-04-07 18:50:10 +0100105
106 // Extra arguments to pass to droidstubs for this scope.
107 droidstubsArgs []string
Anton Hansson6478ac12020-05-02 11:19:36 +0100108
109 // Whether the api scope can be treated as unstable, and should skip compat checks.
110 unstable bool
Paul Duffind1b3a922020-01-22 11:57:20 +0000111}
112
113// Initialize a scope, creating and adding appropriate dependency tags
114func initApiScope(scope *apiScope) *apiScope {
Paul Duffin46a26a82020-04-07 19:27:04 +0100115 scope.fieldName = proptools.FieldNameForProperty(scope.name)
Paul Duffind1b3a922020-01-22 11:57:20 +0000116 scope.stubsTag = scopeDependencyTag{
117 name: scope.name + "-stubs",
118 apiScope: scope,
119 }
120 scope.apiFileTag = scopeDependencyTag{
121 name: scope.name + "-api",
122 apiScope: scope,
123 }
124 return scope
125}
126
127func (scope *apiScope) stubsModuleName(baseName string) string {
128 return baseName + sdkStubsLibrarySuffix + scope.moduleSuffix
129}
130
131func (scope *apiScope) docsModuleName(baseName string) string {
Paul Duffin91b883d2020-02-11 13:05:28 +0000132 return baseName + sdkStubsSourceSuffix + scope.moduleSuffix
Paul Duffind1b3a922020-01-22 11:57:20 +0000133}
134
Paul Duffin3375e352020-04-28 10:44:03 +0100135func (scope *apiScope) String() string {
136 return scope.name
137}
138
Paul Duffind1b3a922020-01-22 11:57:20 +0000139type apiScopes []*apiScope
140
141func (scopes apiScopes) Strings(accessor func(*apiScope) string) []string {
142 var list []string
143 for _, scope := range scopes {
144 list = append(list, accessor(scope))
145 }
146 return list
147}
148
Jiyong Parkc678ad32018-04-10 13:07:10 +0900149var (
Paul Duffind1b3a922020-01-22 11:57:20 +0000150 apiScopePublic = initApiScope(&apiScope{
Paul Duffin3375e352020-04-28 10:44:03 +0100151 name: "public",
152
153 // Public scope is enabled by default for both legacy and non-legacy modes.
154 legacyEnabledStatus: func(module *SdkLibrary) bool {
155 return true
156 },
157 defaultEnabledStatus: true,
158
159 scopeSpecificProperties: func(module *SdkLibrary) *ApiScopeProperties {
160 return &module.sdkLibraryProperties.Public
161 },
Paul Duffind1b3a922020-01-22 11:57:20 +0000162 sdkVersion: "current",
163 })
164 apiScopeSystem = initApiScope(&apiScope{
Paul Duffin3375e352020-04-28 10:44:03 +0100165 name: "system",
166 extends: apiScopePublic,
167 legacyEnabledStatus: (*SdkLibrary).generateTestAndSystemScopesByDefault,
168 scopeSpecificProperties: func(module *SdkLibrary) *ApiScopeProperties {
169 return &module.sdkLibraryProperties.System
170 },
Anton Hansson6affb1f2020-04-28 16:47:41 +0100171 apiFilePrefix: "system-",
172 moduleSuffix: sdkSystemApiSuffix,
173 sdkVersion: "system_current",
174 droidstubsArgs: []string{"-showAnnotation android.annotation.SystemApi"},
Paul Duffind1b3a922020-01-22 11:57:20 +0000175 })
176 apiScopeTest = initApiScope(&apiScope{
Paul Duffin3375e352020-04-28 10:44:03 +0100177 name: "test",
178 extends: apiScopePublic,
179 legacyEnabledStatus: (*SdkLibrary).generateTestAndSystemScopesByDefault,
180 scopeSpecificProperties: func(module *SdkLibrary) *ApiScopeProperties {
181 return &module.sdkLibraryProperties.Test
182 },
Anton Hansson6affb1f2020-04-28 16:47:41 +0100183 apiFilePrefix: "test-",
184 moduleSuffix: sdkTestApiSuffix,
185 sdkVersion: "test_current",
186 droidstubsArgs: []string{"-showAnnotation android.annotation.TestApi"},
Anton Hansson6478ac12020-05-02 11:19:36 +0100187 unstable: true,
Paul Duffind1b3a922020-01-22 11:57:20 +0000188 })
189 allApiScopes = apiScopes{
190 apiScopePublic,
191 apiScopeSystem,
192 apiScopeTest,
193 }
Jiyong Parkc678ad32018-04-10 13:07:10 +0900194)
195
Jiyong Park82484c02018-04-23 21:41:26 +0900196var (
197 javaSdkLibrariesLock sync.Mutex
198)
199
Jiyong Parkc678ad32018-04-10 13:07:10 +0900200// TODO: these are big features that are currently missing
Jiyong Park1be96912018-05-28 18:02:19 +0900201// 1) disallowing linking to the runtime shared lib
202// 2) HTML generation
Jiyong Parkc678ad32018-04-10 13:07:10 +0900203
204func init() {
Paul Duffin43dc1cc2019-12-19 11:18:54 +0000205 RegisterSdkLibraryBuildComponents(android.InitRegistrationContext)
Jiyong Parkc678ad32018-04-10 13:07:10 +0900206
Jiyong Park82484c02018-04-23 21:41:26 +0900207 android.RegisterMakeVarsProvider(pctx, func(ctx android.MakeVarsContext) {
208 javaSdkLibraries := javaSdkLibraries(ctx.Config())
209 sort.Strings(*javaSdkLibraries)
210 ctx.Strict("JAVA_SDK_LIBRARIES", strings.Join(*javaSdkLibraries, " "))
211 })
Paul Duffindd46f712020-02-10 13:37:10 +0000212
213 // Register sdk member types.
214 android.RegisterSdkMemberType(&sdkLibrarySdkMemberType{
215 android.SdkMemberTypeBase{
216 PropertyName: "java_sdk_libs",
217 SupportsSdk: true,
218 },
219 })
Jiyong Parkc678ad32018-04-10 13:07:10 +0900220}
221
Paul Duffin43dc1cc2019-12-19 11:18:54 +0000222func RegisterSdkLibraryBuildComponents(ctx android.RegistrationContext) {
223 ctx.RegisterModuleType("java_sdk_library", SdkLibraryFactory)
224 ctx.RegisterModuleType("java_sdk_library_import", sdkLibraryImportFactory)
225}
226
Paul Duffin3375e352020-04-28 10:44:03 +0100227// Properties associated with each api scope.
228type ApiScopeProperties struct {
229 // Indicates whether the api surface is generated.
230 //
231 // If this is set for any scope then all scopes must explicitly specify if they
232 // are enabled. This is to prevent new usages from depending on legacy behavior.
233 //
234 // Otherwise, if this is not set for any scope then the default behavior is
235 // scope specific so please refer to the scope specific property documentation.
236 Enabled *bool
237}
238
Jiyong Parkc678ad32018-04-10 13:07:10 +0900239type sdkLibraryProperties struct {
Sundong Ahnf043cf62018-06-25 16:04:37 +0900240 // List of Java libraries that will be in the classpath when building stubs
241 Stub_only_libs []string `android:"arch_variant"`
242
Paul Duffin7a586d32019-12-30 17:09:34 +0000243 // list of package names that will be documented and publicized as API.
244 // This allows the API to be restricted to a subset of the source files provided.
245 // If this is unspecified then all the source files will be treated as being part
246 // of the API.
Jiyong Parkc678ad32018-04-10 13:07:10 +0900247 Api_packages []string
248
Jiyong Park5a2c9d72018-05-01 22:25:41 +0900249 // list of package names that must be hidden from the API
250 Hidden_api_packages []string
251
Paul Duffin749f98f2019-12-30 17:23:46 +0000252 // the relative path to the directory containing the api specification files.
253 // Defaults to "api".
254 Api_dir *string
255
Paul Duffin43db9be2019-12-30 17:35:49 +0000256 // If set to true there is no runtime library.
257 Api_only *bool
258
Paul Duffin11512472019-02-11 15:55:17 +0000259 // local files that are used within user customized droiddoc options.
260 Droiddoc_option_files []string
261
262 // additional droiddoc options
263 // Available variables for substitution:
264 //
265 // $(location <label>): the path to the droiddoc_option_files with name <label>
Sundong Ahndd567f92018-07-31 17:19:11 +0900266 Droiddoc_options []string
267
Sundong Ahn054b19a2018-10-19 13:46:09 +0900268 // a list of top-level directories containing files to merge qualifier annotations
269 // (i.e. those intended to be included in the stubs written) from.
270 Merge_annotations_dirs []string
271
272 // a list of top-level directories containing Java stub files to merge show/hide annotations from.
273 Merge_inclusion_annotations_dirs []string
274
275 // If set to true, the path of dist files is apistubs/core. Defaults to false.
276 Core_lib *bool
277
Sundong Ahn80a87b32019-05-13 15:02:50 +0900278 // don't create dist rules.
279 No_dist *bool `blueprint:"mutated"`
280
Paul Duffin3375e352020-04-28 10:44:03 +0100281 // indicates whether system and test apis should be generated.
282 Generate_system_and_test_apis bool `blueprint:"mutated"`
283
284 // The properties specific to the public api scope
285 //
286 // Unless explicitly specified by using public.enabled the public api scope is
287 // enabled by default in both legacy and non-legacy mode.
288 Public ApiScopeProperties
289
290 // The properties specific to the system api scope
291 //
292 // In legacy mode the system api scope is enabled by default when sdk_version
293 // is set to something other than "none".
294 //
295 // In non-legacy mode the system api scope is disabled by default.
296 System ApiScopeProperties
297
298 // The properties specific to the test api scope
299 //
300 // In legacy mode the test api scope is enabled by default when sdk_version
301 // is set to something other than "none".
302 //
303 // In non-legacy mode the test api scope is disabled by default.
304 Test ApiScopeProperties
Paul Duffin37e0b772019-12-30 17:20:10 +0000305
Jiyong Parkc678ad32018-04-10 13:07:10 +0900306 // TODO: determines whether to create HTML doc or not
307 //Html_doc *bool
308}
309
Paul Duffind1b3a922020-01-22 11:57:20 +0000310type scopePaths struct {
Paul Duffin1fd005d2020-04-09 01:08:11 +0100311 stubsHeaderPath android.Paths
312 stubsImplPath android.Paths
313 currentApiFilePath android.Path
314 removedApiFilePath android.Path
315 stubsSrcJar android.Path
Paul Duffind1b3a922020-01-22 11:57:20 +0000316}
317
Paul Duffin56d44902020-01-31 13:36:25 +0000318// Common code between sdk library and sdk library import
319type commonToSdkLibraryAndImport struct {
320 scopePaths map[*apiScope]*scopePaths
321}
322
323func (c *commonToSdkLibraryAndImport) getScopePaths(scope *apiScope) *scopePaths {
324 if c.scopePaths == nil {
325 c.scopePaths = make(map[*apiScope]*scopePaths)
326 }
327 paths := c.scopePaths[scope]
328 if paths == nil {
329 paths = &scopePaths{}
330 c.scopePaths[scope] = paths
331 }
332
333 return paths
334}
335
Inseob Kimc0907f12019-02-08 21:00:45 +0900336type SdkLibrary struct {
Sundong Ahn054b19a2018-10-19 13:46:09 +0900337 Library
Jiyong Parkc678ad32018-04-10 13:07:10 +0900338
Sundong Ahn054b19a2018-10-19 13:46:09 +0900339 sdkLibraryProperties sdkLibraryProperties
Jiyong Parkc678ad32018-04-10 13:07:10 +0900340
Paul Duffin3375e352020-04-28 10:44:03 +0100341 // Map from api scope to the scope specific property structure.
342 scopeToProperties map[*apiScope]*ApiScopeProperties
343
Paul Duffin56d44902020-01-31 13:36:25 +0000344 commonToSdkLibraryAndImport
Jiyong Parkc678ad32018-04-10 13:07:10 +0900345}
346
Inseob Kimc0907f12019-02-08 21:00:45 +0900347var _ Dependency = (*SdkLibrary)(nil)
348var _ SdkLibraryDependency = (*SdkLibrary)(nil)
Colin Cross897d2ed2019-02-11 14:03:51 -0800349
Paul Duffin3375e352020-04-28 10:44:03 +0100350func (module *SdkLibrary) generateTestAndSystemScopesByDefault() bool {
351 return module.sdkLibraryProperties.Generate_system_and_test_apis
352}
353
354func (module *SdkLibrary) getGeneratedApiScopes(ctx android.EarlyModuleContext) apiScopes {
355 // Check to see if any scopes have been explicitly enabled. If any have then all
356 // must be.
357 anyScopesExplicitlyEnabled := false
358 for _, scope := range allApiScopes {
359 scopeProperties := module.scopeToProperties[scope]
360 if scopeProperties.Enabled != nil {
361 anyScopesExplicitlyEnabled = true
362 break
363 }
Paul Duffind1b3a922020-01-22 11:57:20 +0000364 }
Paul Duffin3375e352020-04-28 10:44:03 +0100365
366 var generatedScopes apiScopes
367 enabledScopes := make(map[*apiScope]struct{})
368 for _, scope := range allApiScopes {
369 scopeProperties := module.scopeToProperties[scope]
370 // If any scopes are explicitly enabled then ignore the legacy enabled status.
371 // This is to ensure that any new usages of this module type do not rely on legacy
372 // behaviour.
373 defaultEnabledStatus := false
374 if anyScopesExplicitlyEnabled {
375 defaultEnabledStatus = scope.defaultEnabledStatus
376 } else {
377 defaultEnabledStatus = scope.legacyEnabledStatus(module)
378 }
379 enabled := proptools.BoolDefault(scopeProperties.Enabled, defaultEnabledStatus)
380 if enabled {
381 enabledScopes[scope] = struct{}{}
382 generatedScopes = append(generatedScopes, scope)
383 }
384 }
385
386 // Now check to make sure that any scope that is extended by an enabled scope is also
387 // enabled.
388 for _, scope := range allApiScopes {
389 if _, ok := enabledScopes[scope]; ok {
390 extends := scope.extends
391 if extends != nil {
392 if _, ok := enabledScopes[extends]; !ok {
393 ctx.ModuleErrorf("enabled api scope %q depends on disabled scope %q", scope, extends)
394 }
395 }
396 }
397 }
398
399 return generatedScopes
Paul Duffind1b3a922020-01-22 11:57:20 +0000400}
401
Paul Duffine74ac732020-02-06 13:51:46 +0000402var xmlPermissionsFileTag = dependencyTag{name: "xml-permissions-file"}
403
Jiyong Parke3833882020-02-17 17:28:10 +0900404func IsXmlPermissionsFileDepTag(depTag blueprint.DependencyTag) bool {
405 if dt, ok := depTag.(dependencyTag); ok {
406 return dt == xmlPermissionsFileTag
407 }
408 return false
409}
410
Inseob Kimc0907f12019-02-08 21:00:45 +0900411func (module *SdkLibrary) DepsMutator(ctx android.BottomUpMutatorContext) {
Paul Duffin3375e352020-04-28 10:44:03 +0100412 for _, apiScope := range module.getGeneratedApiScopes(ctx) {
Paul Duffind1b3a922020-01-22 11:57:20 +0000413 // Add dependencies to the stubs library
Paul Duffin50061512020-01-21 16:31:05 +0000414 ctx.AddVariationDependencies(nil, apiScope.stubsTag, module.stubsName(apiScope))
Paul Duffind1b3a922020-01-22 11:57:20 +0000415
Paul Duffin50061512020-01-21 16:31:05 +0000416 // And the api file
Paul Duffind1b3a922020-01-22 11:57:20 +0000417 ctx.AddVariationDependencies(nil, apiScope.apiFileTag, module.docsName(apiScope))
Sundong Ahn054b19a2018-10-19 13:46:09 +0900418 }
419
Paul Duffine74ac732020-02-06 13:51:46 +0000420 if !proptools.Bool(module.sdkLibraryProperties.Api_only) {
421 // Add dependency to the rule for generating the xml permissions file
Jiyong Parke3833882020-02-17 17:28:10 +0900422 ctx.AddDependency(module, xmlPermissionsFileTag, module.xmlFileName())
Paul Duffine74ac732020-02-06 13:51:46 +0000423 }
424
Sundong Ahn054b19a2018-10-19 13:46:09 +0900425 module.Library.deps(ctx)
Jiyong Parkc678ad32018-04-10 13:07:10 +0900426}
427
Inseob Kimc0907f12019-02-08 21:00:45 +0900428func (module *SdkLibrary) GenerateAndroidBuildActions(ctx android.ModuleContext) {
Paul Duffin43db9be2019-12-30 17:35:49 +0000429 // Don't build an implementation library if this is api only.
430 if !proptools.Bool(module.sdkLibraryProperties.Api_only) {
431 module.Library.GenerateAndroidBuildActions(ctx)
432 }
Sundong Ahn054b19a2018-10-19 13:46:09 +0900433
Sundong Ahn57368eb2018-07-06 11:20:23 +0900434 // Record the paths to the header jars of the library (stubs and impl).
Paul Duffind1b3a922020-01-22 11:57:20 +0000435 // When this java_sdk_library is depended upon from others via "libs" property,
Jiyong Parkc678ad32018-04-10 13:07:10 +0900436 // the recorded paths will be returned depending on the link type of the caller.
437 ctx.VisitDirectDeps(func(to android.Module) {
438 otherName := ctx.OtherModuleName(to)
439 tag := ctx.OtherModuleDependencyTag(to)
440
Sundong Ahn57368eb2018-07-06 11:20:23 +0900441 if lib, ok := to.(Dependency); ok {
Paul Duffind1b3a922020-01-22 11:57:20 +0000442 if scopeTag, ok := tag.(scopeDependencyTag); ok {
443 apiScope := scopeTag.apiScope
444 scopePaths := module.getScopePaths(apiScope)
445 scopePaths.stubsHeaderPath = lib.HeaderJars()
446 scopePaths.stubsImplPath = lib.ImplementationJars()
Jiyong Parkc678ad32018-04-10 13:07:10 +0900447 }
448 }
Paul Duffin3d1248c2020-04-09 00:10:17 +0100449 if doc, ok := to.(ApiStubsProvider); ok {
Paul Duffind1b3a922020-01-22 11:57:20 +0000450 if scopeTag, ok := tag.(scopeDependencyTag); ok {
451 apiScope := scopeTag.apiScope
452 scopePaths := module.getScopePaths(apiScope)
Paul Duffin1fd005d2020-04-09 01:08:11 +0100453 scopePaths.currentApiFilePath = doc.ApiFilePath()
454 scopePaths.removedApiFilePath = doc.RemovedApiFilePath()
Paul Duffin3d1248c2020-04-09 00:10:17 +0100455 scopePaths.stubsSrcJar = doc.StubsSrcJar()
Paul Duffind1b3a922020-01-22 11:57:20 +0000456 } else {
Sundong Ahn20e998b2018-07-24 11:19:26 +0900457 ctx.ModuleErrorf("depends on module %q of unknown tag %q", otherName, tag)
458 }
459 }
Jiyong Parkc678ad32018-04-10 13:07:10 +0900460 })
461}
462
Jiyong Park0b0e1b92019-12-03 13:24:29 +0900463func (module *SdkLibrary) AndroidMkEntries() []android.AndroidMkEntries {
Paul Duffin43db9be2019-12-30 17:35:49 +0000464 if proptools.Bool(module.sdkLibraryProperties.Api_only) {
465 return nil
466 }
Jiyong Park0b0e1b92019-12-03 13:24:29 +0900467 entriesList := module.Library.AndroidMkEntries()
468 entries := &entriesList[0]
Jaewoong Jungb0c127c2019-08-29 14:56:03 -0700469 entries.Required = append(entries.Required, module.xmlFileName())
Jiyong Park0b0e1b92019-12-03 13:24:29 +0900470 return entriesList
Jiyong Park82484c02018-04-23 21:41:26 +0900471}
472
Jiyong Parkc678ad32018-04-10 13:07:10 +0900473// Module name of the stubs library
Paul Duffind1b3a922020-01-22 11:57:20 +0000474func (module *SdkLibrary) stubsName(apiScope *apiScope) string {
475 return apiScope.stubsModuleName(module.BaseModuleName())
Jiyong Parkc678ad32018-04-10 13:07:10 +0900476}
477
478// Module name of the docs
Paul Duffind1b3a922020-01-22 11:57:20 +0000479func (module *SdkLibrary) docsName(apiScope *apiScope) string {
480 return apiScope.docsModuleName(module.BaseModuleName())
Jiyong Parkc678ad32018-04-10 13:07:10 +0900481}
482
483// Module name of the runtime implementation library
Inseob Kimc0907f12019-02-08 21:00:45 +0900484func (module *SdkLibrary) implName() string {
Sundong Ahn054b19a2018-10-19 13:46:09 +0900485 return module.BaseModuleName()
Jiyong Parkc678ad32018-04-10 13:07:10 +0900486}
487
Jiyong Parkc678ad32018-04-10 13:07:10 +0900488// Module name of the XML file for the lib
Inseob Kimc0907f12019-02-08 21:00:45 +0900489func (module *SdkLibrary) xmlFileName() string {
Jiyong Parkc678ad32018-04-10 13:07:10 +0900490 return module.BaseModuleName() + sdkXmlFileSuffix
491}
492
Anton Hansson5fd5d242020-03-27 19:43:19 +0000493// The dist path of the stub artifacts
494func (module *SdkLibrary) apiDistPath(apiScope *apiScope) string {
495 if module.ModuleBase.Owner() != "" {
496 return path.Join("apistubs", module.ModuleBase.Owner(), apiScope.name)
497 } else if Bool(module.sdkLibraryProperties.Core_lib) {
498 return path.Join("apistubs", "core", apiScope.name)
499 } else {
500 return path.Join("apistubs", "android", apiScope.name)
501 }
502}
503
Paul Duffin12ceb462019-12-24 20:31:31 +0000504// Get the sdk version for use when compiling the stubs library.
Paul Duffinf0229202020-04-29 16:47:28 +0100505func (module *SdkLibrary) sdkVersionForStubsLibrary(mctx android.DefaultableHookContext, apiScope *apiScope) string {
Paul Duffin12ceb462019-12-24 20:31:31 +0000506 sdkDep := decodeSdkDep(mctx, sdkContext(&module.Library))
507 if sdkDep.hasStandardLibs() {
508 // If building against a standard sdk then use the sdk version appropriate for the scope.
Paul Duffind1b3a922020-01-22 11:57:20 +0000509 return apiScope.sdkVersion
Paul Duffin12ceb462019-12-24 20:31:31 +0000510 } else {
511 // Otherwise, use no system module.
512 return "none"
513 }
514}
515
Paul Duffind1b3a922020-01-22 11:57:20 +0000516func (module *SdkLibrary) latestApiFilegroupName(apiScope *apiScope) string {
517 return ":" + module.BaseModuleName() + ".api." + apiScope.name + ".latest"
Jiyong Park58c518b2018-05-12 22:29:12 +0900518}
Jiyong Parkc678ad32018-04-10 13:07:10 +0900519
Paul Duffind1b3a922020-01-22 11:57:20 +0000520func (module *SdkLibrary) latestRemovedApiFilegroupName(apiScope *apiScope) string {
521 return ":" + module.BaseModuleName() + "-removed.api." + apiScope.name + ".latest"
Jiyong Parkc678ad32018-04-10 13:07:10 +0900522}
523
524// Creates a static java library that has API stubs
Paul Duffinf0229202020-04-29 16:47:28 +0100525func (module *SdkLibrary) createStubsLibrary(mctx android.DefaultableHookContext, apiScope *apiScope) {
Jiyong Parkc678ad32018-04-10 13:07:10 +0900526 props := struct {
Sundong Ahn0d7dff42019-12-04 12:53:44 +0900527 Name *string
528 Srcs []string
Paul Duffin367ab912019-12-23 19:40:36 +0000529 Installable *bool
Sundong Ahn0d7dff42019-12-04 12:53:44 +0900530 Sdk_version *string
Paul Duffin12ceb462019-12-24 20:31:31 +0000531 System_modules *string
Paul Duffinab8da5d2020-02-07 16:12:04 +0000532 Patch_module *string
Sundong Ahn0d7dff42019-12-04 12:53:44 +0900533 Libs []string
534 Soc_specific *bool
535 Device_specific *bool
536 Product_specific *bool
537 System_ext_specific *bool
538 Compile_dex *bool
Sundong Ahn0d7dff42019-12-04 12:53:44 +0900539 Java_version *string
540 Product_variables struct {
Jiyong Park82484c02018-04-23 21:41:26 +0900541 Pdk struct {
542 Enabled *bool
543 }
Jiyong Parkc678ad32018-04-10 13:07:10 +0900544 }
Sundong Ahn054b19a2018-10-19 13:46:09 +0900545 Openjdk9 struct {
546 Srcs []string
547 Javacflags []string
548 }
Anton Hansson5fd5d242020-03-27 19:43:19 +0000549 Dist struct {
550 Targets []string
551 Dest *string
552 Dir *string
553 Tag *string
554 }
Jiyong Parkc678ad32018-04-10 13:07:10 +0900555 }{}
556
Jiyong Parkdf130542018-04-27 16:29:21 +0900557 props.Name = proptools.StringPtr(module.stubsName(apiScope))
Jiyong Parkc678ad32018-04-10 13:07:10 +0900558 // sources are generated from the droiddoc
Jiyong Parkdf130542018-04-27 16:29:21 +0900559 props.Srcs = []string{":" + module.docsName(apiScope)}
Paul Duffin12ceb462019-12-24 20:31:31 +0000560 sdkVersion := module.sdkVersionForStubsLibrary(mctx, apiScope)
Paul Duffin52d398a2019-06-11 12:31:14 +0100561 props.Sdk_version = proptools.StringPtr(sdkVersion)
Paul Duffin12ceb462019-12-24 20:31:31 +0000562 props.System_modules = module.Library.Module.deviceProperties.System_modules
Paul Duffinab8da5d2020-02-07 16:12:04 +0000563 props.Patch_module = module.Library.Module.properties.Patch_module
Paul Duffin367ab912019-12-23 19:40:36 +0000564 props.Installable = proptools.BoolPtr(false)
Sundong Ahn054b19a2018-10-19 13:46:09 +0900565 props.Libs = module.sdkLibraryProperties.Stub_only_libs
Jiyong Park82484c02018-04-23 21:41:26 +0900566 props.Product_variables.Pdk.Enabled = proptools.BoolPtr(false)
Sundong Ahn054b19a2018-10-19 13:46:09 +0900567 props.Openjdk9.Srcs = module.Library.Module.properties.Openjdk9.Srcs
568 props.Openjdk9.Javacflags = module.Library.Module.properties.Openjdk9.Javacflags
569 props.Java_version = module.Library.Module.properties.Java_version
570 if module.Library.Module.deviceProperties.Compile_dex != nil {
571 props.Compile_dex = module.Library.Module.deviceProperties.Compile_dex
Sundong Ahndd567f92018-07-31 17:19:11 +0900572 }
Jiyong Parkc678ad32018-04-10 13:07:10 +0900573
574 if module.SocSpecific() {
575 props.Soc_specific = proptools.BoolPtr(true)
576 } else if module.DeviceSpecific() {
577 props.Device_specific = proptools.BoolPtr(true)
578 } else if module.ProductSpecific() {
579 props.Product_specific = proptools.BoolPtr(true)
Sundong Ahn0d7dff42019-12-04 12:53:44 +0900580 } else if module.SystemExtSpecific() {
581 props.System_ext_specific = proptools.BoolPtr(true)
Jiyong Parkc678ad32018-04-10 13:07:10 +0900582 }
Anton Hansson5fd5d242020-03-27 19:43:19 +0000583 // Dist the class jar artifact for sdk builds.
584 if !Bool(module.sdkLibraryProperties.No_dist) {
585 props.Dist.Targets = []string{"sdk", "win_sdk"}
586 props.Dist.Dest = proptools.StringPtr(fmt.Sprintf("%v.jar", module.BaseModuleName()))
587 props.Dist.Dir = proptools.StringPtr(module.apiDistPath(apiScope))
588 props.Dist.Tag = proptools.StringPtr(".jar")
589 }
Jiyong Parkc678ad32018-04-10 13:07:10 +0900590
Colin Cross84dfc3d2019-09-25 11:33:01 -0700591 mctx.CreateModule(LibraryFactory, &props)
Jiyong Parkc678ad32018-04-10 13:07:10 +0900592}
593
Paul Duffin6d0886e2020-04-07 18:49:53 +0100594// Creates a droidstubs module that creates stubs source files from the given full source
Jiyong Parkc678ad32018-04-10 13:07:10 +0900595// files
Paul Duffinf0229202020-04-29 16:47:28 +0100596func (module *SdkLibrary) createStubsSources(mctx android.DefaultableHookContext, apiScope *apiScope) {
Jiyong Parkc678ad32018-04-10 13:07:10 +0900597 props := struct {
Sundong Ahn054b19a2018-10-19 13:46:09 +0900598 Name *string
599 Srcs []string
600 Installable *bool
Paul Duffin52d398a2019-06-11 12:31:14 +0100601 Sdk_version *string
Paul Duffin12ceb462019-12-24 20:31:31 +0000602 System_modules *string
Sundong Ahn054b19a2018-10-19 13:46:09 +0900603 Libs []string
Paul Duffin11512472019-02-11 15:55:17 +0000604 Arg_files []string
Sundong Ahn054b19a2018-10-19 13:46:09 +0900605 Args *string
Sundong Ahn054b19a2018-10-19 13:46:09 +0900606 Java_version *string
607 Merge_annotations_dirs []string
608 Merge_inclusion_annotations_dirs []string
609 Check_api struct {
Inseob Kim38449af2019-02-28 14:24:05 +0900610 Current ApiToCheck
611 Last_released ApiToCheck
612 Ignore_missing_latest_api *bool
Jiyong Park58c518b2018-05-12 22:29:12 +0900613 }
Sundong Ahn1b92c822018-05-29 11:35:17 +0900614 Aidl struct {
615 Include_dirs []string
616 Local_include_dirs []string
617 }
Anton Hansson5fd5d242020-03-27 19:43:19 +0000618 Dist struct {
619 Targets []string
620 Dest *string
621 Dir *string
622 }
Jiyong Parkc678ad32018-04-10 13:07:10 +0900623 }{}
624
Paul Duffin7b78b4d2020-04-28 14:08:32 +0100625 // The stubs source processing uses the same compile time classpath when extracting the
626 // API from the implementation library as it does when compiling it. i.e. the same
627 // * sdk version
628 // * system_modules
629 // * libs (static_libs/libs)
Paul Duffin250e6192019-06-07 10:44:37 +0100630
Jiyong Parkdf130542018-04-27 16:29:21 +0900631 props.Name = proptools.StringPtr(module.docsName(apiScope))
Sundong Ahn054b19a2018-10-19 13:46:09 +0900632 props.Srcs = append(props.Srcs, module.Library.Module.properties.Srcs...)
Paul Duffin7b78b4d2020-04-28 14:08:32 +0100633 props.Sdk_version = module.Library.Module.deviceProperties.Sdk_version
Paul Duffin12ceb462019-12-24 20:31:31 +0000634 props.System_modules = module.Library.Module.deviceProperties.System_modules
Jiyong Parkc678ad32018-04-10 13:07:10 +0900635 props.Installable = proptools.BoolPtr(false)
Sundong Ahne6f0b052018-06-05 16:46:14 +0900636 // A droiddoc module has only one Libs property and doesn't distinguish between
637 // shared libs and static libs. So we need to add both of these libs to Libs property.
Sundong Ahn054b19a2018-10-19 13:46:09 +0900638 props.Libs = module.Library.Module.properties.Libs
639 props.Libs = append(props.Libs, module.Library.Module.properties.Static_libs...)
640 props.Aidl.Include_dirs = module.Library.Module.deviceProperties.Aidl.Include_dirs
641 props.Aidl.Local_include_dirs = module.Library.Module.deviceProperties.Aidl.Local_include_dirs
Sundong Ahn054b19a2018-10-19 13:46:09 +0900642 props.Java_version = module.Library.Module.properties.Java_version
Jiyong Parkc678ad32018-04-10 13:07:10 +0900643
Sundong Ahn054b19a2018-10-19 13:46:09 +0900644 props.Merge_annotations_dirs = module.sdkLibraryProperties.Merge_annotations_dirs
645 props.Merge_inclusion_annotations_dirs = module.sdkLibraryProperties.Merge_inclusion_annotations_dirs
646
Paul Duffin6d0886e2020-04-07 18:49:53 +0100647 droidstubsArgs := []string{}
Paul Duffin235ffff2019-12-24 10:41:30 +0000648 if len(module.sdkLibraryProperties.Api_packages) != 0 {
Paul Duffin6d0886e2020-04-07 18:49:53 +0100649 droidstubsArgs = append(droidstubsArgs, "--stub-packages "+strings.Join(module.sdkLibraryProperties.Api_packages, ":"))
Paul Duffin235ffff2019-12-24 10:41:30 +0000650 }
651 if len(module.sdkLibraryProperties.Hidden_api_packages) != 0 {
Paul Duffin6d0886e2020-04-07 18:49:53 +0100652 droidstubsArgs = append(droidstubsArgs,
Paul Duffin235ffff2019-12-24 10:41:30 +0000653 android.JoinWithPrefix(module.sdkLibraryProperties.Hidden_api_packages, " --hide-package "))
654 }
Paul Duffin6d0886e2020-04-07 18:49:53 +0100655 droidstubsArgs = append(droidstubsArgs, module.sdkLibraryProperties.Droiddoc_options...)
Paul Duffin235ffff2019-12-24 10:41:30 +0000656 disabledWarnings := []string{
657 "MissingPermission",
658 "BroadcastBehavior",
659 "HiddenSuperclass",
660 "DeprecationMismatch",
661 "UnavailableSymbol",
662 "SdkConstant",
663 "HiddenTypeParameter",
664 "Todo",
665 "Typo",
666 }
Paul Duffin6d0886e2020-04-07 18:49:53 +0100667 droidstubsArgs = append(droidstubsArgs, android.JoinWithPrefix(disabledWarnings, "--hide "))
Sundong Ahnfb2721f2018-09-17 13:23:09 +0900668
Paul Duffin1fb487d2020-04-07 18:50:10 +0100669 // Add in scope specific arguments.
670 droidstubsArgs = append(droidstubsArgs, apiScope.droidstubsArgs...)
Paul Duffin11512472019-02-11 15:55:17 +0000671 props.Arg_files = module.sdkLibraryProperties.Droiddoc_option_files
Paul Duffin6d0886e2020-04-07 18:49:53 +0100672 props.Args = proptools.StringPtr(strings.Join(droidstubsArgs, " "))
Jiyong Parkc678ad32018-04-10 13:07:10 +0900673
674 // List of APIs identified from the provided source files are created. They are later
675 // compared against to the not-yet-released (a.k.a current) list of APIs and to the
676 // last-released (a.k.a numbered) list of API.
Paul Duffind1b3a922020-01-22 11:57:20 +0000677 currentApiFileName := apiScope.apiFilePrefix + "current.txt"
678 removedApiFileName := apiScope.apiFilePrefix + "removed.txt"
Paul Duffin749f98f2019-12-30 17:23:46 +0000679 apiDir := module.getApiDir()
680 currentApiFileName = path.Join(apiDir, currentApiFileName)
681 removedApiFileName = path.Join(apiDir, removedApiFileName)
Jiyong Parkc678ad32018-04-10 13:07:10 +0900682
Jiyong Park58c518b2018-05-12 22:29:12 +0900683 // check against the not-yet-release API
684 props.Check_api.Current.Api_file = proptools.StringPtr(currentApiFileName)
685 props.Check_api.Current.Removed_api_file = proptools.StringPtr(removedApiFileName)
Jiyong Park58c518b2018-05-12 22:29:12 +0900686
Anton Hansson6478ac12020-05-02 11:19:36 +0100687 if !apiScope.unstable {
688 // check against the latest released API
689 props.Check_api.Last_released.Api_file = proptools.StringPtr(
690 module.latestApiFilegroupName(apiScope))
691 props.Check_api.Last_released.Removed_api_file = proptools.StringPtr(
692 module.latestRemovedApiFilegroupName(apiScope))
693 props.Check_api.Ignore_missing_latest_api = proptools.BoolPtr(true)
694 }
Jiyong Park58c518b2018-05-12 22:29:12 +0900695
Anton Hansson5fd5d242020-03-27 19:43:19 +0000696 // Dist the api txt artifact for sdk builds.
697 if !Bool(module.sdkLibraryProperties.No_dist) {
698 props.Dist.Targets = []string{"sdk", "win_sdk"}
699 props.Dist.Dest = proptools.StringPtr(fmt.Sprintf("%v.txt", module.BaseModuleName()))
700 props.Dist.Dir = proptools.StringPtr(path.Join(module.apiDistPath(apiScope), "api"))
701 }
702
Colin Cross84dfc3d2019-09-25 11:33:01 -0700703 mctx.CreateModule(DroidstubsFactory, &props)
Jiyong Parkc678ad32018-04-10 13:07:10 +0900704}
705
Jooyung Han5e9013b2020-03-10 06:23:13 +0900706func (module *SdkLibrary) DepIsInSameApex(mctx android.BaseModuleContext, dep android.Module) bool {
707 depTag := mctx.OtherModuleDependencyTag(dep)
708 if depTag == xmlPermissionsFileTag {
709 return true
710 }
711 return module.Library.DepIsInSameApex(mctx, dep)
712}
713
Jiyong Parkc678ad32018-04-10 13:07:10 +0900714// Creates the xml file that publicizes the runtime library
Paul Duffinf0229202020-04-29 16:47:28 +0100715func (module *SdkLibrary) createXmlFile(mctx android.DefaultableHookContext) {
Jiyong Parke3833882020-02-17 17:28:10 +0900716 props := struct {
Sundong Ahn0d7dff42019-12-04 12:53:44 +0900717 Name *string
Jiyong Parke3833882020-02-17 17:28:10 +0900718 Lib_name *string
Sundong Ahn0d7dff42019-12-04 12:53:44 +0900719 Soc_specific *bool
720 Device_specific *bool
721 Product_specific *bool
722 System_ext_specific *bool
Jooyung Han5e9013b2020-03-10 06:23:13 +0900723 Apex_available []string
Jiyong Parke3833882020-02-17 17:28:10 +0900724 }{
Jooyung Han5e9013b2020-03-10 06:23:13 +0900725 Name: proptools.StringPtr(module.xmlFileName()),
726 Lib_name: proptools.StringPtr(module.BaseModuleName()),
727 Apex_available: module.ApexProperties.Apex_available,
Jiyong Parkc678ad32018-04-10 13:07:10 +0900728 }
Jiyong Parke3833882020-02-17 17:28:10 +0900729
730 if module.SocSpecific() {
731 props.Soc_specific = proptools.BoolPtr(true)
732 } else if module.DeviceSpecific() {
733 props.Device_specific = proptools.BoolPtr(true)
734 } else if module.ProductSpecific() {
735 props.Product_specific = proptools.BoolPtr(true)
736 } else if module.SystemExtSpecific() {
737 props.System_ext_specific = proptools.BoolPtr(true)
738 }
739
740 mctx.CreateModule(sdkLibraryXmlFactory, &props)
Jiyong Parkc678ad32018-04-10 13:07:10 +0900741}
742
Paul Duffin50061512020-01-21 16:31:05 +0000743func PrebuiltJars(ctx android.BaseModuleContext, baseName string, s sdkSpec) android.Paths {
Jiyong Park6a927c42020-01-21 02:03:43 +0900744 var ver sdkVersion
745 var kind sdkKind
746 if s.usePrebuilt(ctx) {
747 ver = s.version
748 kind = s.kind
Jiyong Parkc678ad32018-04-10 13:07:10 +0900749 } else {
Jiyong Park6a927c42020-01-21 02:03:43 +0900750 // We don't have prebuilt SDK for the specific sdkVersion.
751 // Instead of breaking the build, fallback to use "system_current"
752 ver = sdkVersionCurrent
753 kind = sdkSystem
Sundong Ahn054b19a2018-10-19 13:46:09 +0900754 }
Jiyong Park6a927c42020-01-21 02:03:43 +0900755
756 dir := filepath.Join("prebuilts", "sdk", ver.String(), kind.String())
Paul Duffin50061512020-01-21 16:31:05 +0000757 jar := filepath.Join(dir, baseName+".jar")
Sundong Ahn054b19a2018-10-19 13:46:09 +0900758 jarPath := android.ExistentPathForSource(ctx, jar)
Sundong Ahnae418ac2019-02-28 15:01:28 +0900759 if !jarPath.Valid() {
Colin Cross07c88562020-01-07 09:34:44 -0800760 if ctx.Config().AllowMissingDependencies() {
761 return android.Paths{android.PathForSource(ctx, jar)}
762 } else {
Jiyong Park6a927c42020-01-21 02:03:43 +0900763 ctx.PropertyErrorf("sdk_library", "invalid sdk version %q, %q does not exist", s.raw, jar)
Colin Cross07c88562020-01-07 09:34:44 -0800764 }
Sundong Ahnae418ac2019-02-28 15:01:28 +0900765 return nil
766 }
Sundong Ahn054b19a2018-10-19 13:46:09 +0900767 return android.Paths{jarPath.Path()}
768}
769
Paul Duffind1b3a922020-01-22 11:57:20 +0000770func (module *SdkLibrary) sdkJars(
771 ctx android.BaseModuleContext,
772 sdkVersion sdkSpec,
773 headerJars bool) android.Paths {
774
Paul Duffin50061512020-01-21 16:31:05 +0000775 // If a specific numeric version has been requested then use prebuilt versions of the sdk.
776 if sdkVersion.version.isNumbered() {
777 return PrebuiltJars(ctx, module.BaseModuleName(), sdkVersion)
Sundong Ahn054b19a2018-10-19 13:46:09 +0900778 } else {
Paul Duffind1b3a922020-01-22 11:57:20 +0000779 if !sdkVersion.specified() {
780 if headerJars {
781 return module.Library.HeaderJars()
782 } else {
783 return module.Library.ImplementationJars()
784 }
785 }
Paul Duffin726d23c2020-01-22 16:30:37 +0000786 var apiScope *apiScope
Jiyong Park6a927c42020-01-21 02:03:43 +0900787 switch sdkVersion.kind {
788 case sdkSystem:
Paul Duffin726d23c2020-01-22 16:30:37 +0000789 apiScope = apiScopeSystem
790 case sdkTest:
791 apiScope = apiScopeTest
Jiyong Park6a927c42020-01-21 02:03:43 +0900792 case sdkPrivate:
Sundong Ahn054b19a2018-10-19 13:46:09 +0900793 return module.Library.HeaderJars()
Jiyong Park6a927c42020-01-21 02:03:43 +0900794 default:
Paul Duffin726d23c2020-01-22 16:30:37 +0000795 apiScope = apiScopePublic
Paul Duffind1b3a922020-01-22 11:57:20 +0000796 }
797
Paul Duffin726d23c2020-01-22 16:30:37 +0000798 paths := module.getScopePaths(apiScope)
Paul Duffind1b3a922020-01-22 11:57:20 +0000799 if headerJars {
800 return paths.stubsHeaderPath
801 } else {
802 return paths.stubsImplPath
Sundong Ahn054b19a2018-10-19 13:46:09 +0900803 }
Jiyong Parkc678ad32018-04-10 13:07:10 +0900804 }
805}
806
Sundong Ahn241cd372018-07-13 16:16:44 +0900807// to satisfy SdkLibraryDependency interface
Paul Duffind1b3a922020-01-22 11:57:20 +0000808func (module *SdkLibrary) SdkHeaderJars(ctx android.BaseModuleContext, sdkVersion sdkSpec) android.Paths {
809 return module.sdkJars(ctx, sdkVersion, true /*headerJars*/)
810}
811
812// to satisfy SdkLibraryDependency interface
Jiyong Park6a927c42020-01-21 02:03:43 +0900813func (module *SdkLibrary) SdkImplementationJars(ctx android.BaseModuleContext, sdkVersion sdkSpec) android.Paths {
Paul Duffind1b3a922020-01-22 11:57:20 +0000814 return module.sdkJars(ctx, sdkVersion, false /*headerJars*/)
Sundong Ahn241cd372018-07-13 16:16:44 +0900815}
816
Sundong Ahn80a87b32019-05-13 15:02:50 +0900817func (module *SdkLibrary) SetNoDist() {
818 module.sdkLibraryProperties.No_dist = proptools.BoolPtr(true)
819}
820
Colin Cross571cccf2019-02-04 11:22:08 -0800821var javaSdkLibrariesKey = android.NewOnceKey("javaSdkLibraries")
822
Jiyong Park82484c02018-04-23 21:41:26 +0900823func javaSdkLibraries(config android.Config) *[]string {
Colin Cross571cccf2019-02-04 11:22:08 -0800824 return config.Once(javaSdkLibrariesKey, func() interface{} {
Jiyong Park82484c02018-04-23 21:41:26 +0900825 return &[]string{}
826 }).(*[]string)
827}
828
Paul Duffin749f98f2019-12-30 17:23:46 +0000829func (module *SdkLibrary) getApiDir() string {
830 return proptools.StringDefault(module.sdkLibraryProperties.Api_dir, "api")
831}
832
Jiyong Parkc678ad32018-04-10 13:07:10 +0900833// For a java_sdk_library module, create internal modules for stubs, docs,
834// runtime libs and xml file. If requested, the stubs and docs are created twice
835// once for public API level and once for system API level
Paul Duffinf0229202020-04-29 16:47:28 +0100836func (module *SdkLibrary) CreateInternalModules(mctx android.DefaultableHookContext) {
837 // If the module has been disabled then don't create any child modules.
838 if !module.Enabled() {
839 return
840 }
841
Inseob Kim6e93ac92019-03-21 17:43:49 +0900842 if len(module.Library.Module.properties.Srcs) == 0 {
Inseob Kimc0907f12019-02-08 21:00:45 +0900843 mctx.PropertyErrorf("srcs", "java_sdk_library must specify srcs")
Jooyung Han58f26ab2019-12-18 15:34:32 +0900844 return
Inseob Kimc0907f12019-02-08 21:00:45 +0900845 }
846
Paul Duffin37e0b772019-12-30 17:20:10 +0000847 // If this builds against standard libraries (i.e. is not part of the core libraries)
848 // then assume it provides both system and test apis. Otherwise, assume it does not and
849 // also assume it does not contribute to the dist build.
850 sdkDep := decodeSdkDep(mctx, sdkContext(&module.Library))
851 hasSystemAndTestApis := sdkDep.hasStandardLibs()
Paul Duffin3375e352020-04-28 10:44:03 +0100852 module.sdkLibraryProperties.Generate_system_and_test_apis = hasSystemAndTestApis
Paul Duffin37e0b772019-12-30 17:20:10 +0000853 module.sdkLibraryProperties.No_dist = proptools.BoolPtr(!hasSystemAndTestApis)
854
Inseob Kim8098faa2019-03-18 10:19:51 +0900855 missing_current_api := false
856
Paul Duffin3375e352020-04-28 10:44:03 +0100857 generatedScopes := module.getGeneratedApiScopes(mctx)
Paul Duffind1b3a922020-01-22 11:57:20 +0000858
Paul Duffin749f98f2019-12-30 17:23:46 +0000859 apiDir := module.getApiDir()
Paul Duffin3375e352020-04-28 10:44:03 +0100860 for _, scope := range generatedScopes {
Inseob Kim8098faa2019-03-18 10:19:51 +0900861 for _, api := range []string{"current.txt", "removed.txt"} {
Paul Duffind1b3a922020-01-22 11:57:20 +0000862 path := path.Join(mctx.ModuleDir(), apiDir, scope.apiFilePrefix+api)
Inseob Kim8098faa2019-03-18 10:19:51 +0900863 p := android.ExistentPathForSource(mctx, path)
864 if !p.Valid() {
865 mctx.ModuleErrorf("Current api file %#v doesn't exist", path)
866 missing_current_api = true
867 }
868 }
869 }
870
871 if missing_current_api {
872 script := "build/soong/scripts/gen-java-current-api-files.sh"
873 p := android.ExistentPathForSource(mctx, script)
874
875 if !p.Valid() {
876 panic(fmt.Sprintf("script file %s doesn't exist", script))
877 }
878
879 mctx.ModuleErrorf("One or more current api files are missing. "+
880 "You can update them by:\n"+
Paul Duffin37e0b772019-12-30 17:20:10 +0000881 "%s %q %s && m update-api",
Paul Duffind1b3a922020-01-22 11:57:20 +0000882 script, filepath.Join(mctx.ModuleDir(), apiDir),
Paul Duffin3375e352020-04-28 10:44:03 +0100883 strings.Join(generatedScopes.Strings(func(s *apiScope) string { return s.apiFilePrefix }), " "))
Inseob Kim8098faa2019-03-18 10:19:51 +0900884 return
885 }
886
Paul Duffin3375e352020-04-28 10:44:03 +0100887 for _, scope := range generatedScopes {
Paul Duffind1b3a922020-01-22 11:57:20 +0000888 module.createStubsLibrary(mctx, scope)
889 module.createStubsSources(mctx, scope)
Inseob Kimc0907f12019-02-08 21:00:45 +0900890 }
891
Paul Duffin43db9be2019-12-30 17:35:49 +0000892 if !proptools.Bool(module.sdkLibraryProperties.Api_only) {
893 // for runtime
894 module.createXmlFile(mctx)
895
896 // record java_sdk_library modules so that they are exported to make
897 javaSdkLibraries := javaSdkLibraries(mctx.Config())
898 javaSdkLibrariesLock.Lock()
899 defer javaSdkLibrariesLock.Unlock()
900 *javaSdkLibraries = append(*javaSdkLibraries, module.BaseModuleName())
901 }
Inseob Kimc0907f12019-02-08 21:00:45 +0900902}
903
904func (module *SdkLibrary) InitSdkLibraryProperties() {
Sundong Ahn054b19a2018-10-19 13:46:09 +0900905 module.AddProperties(
906 &module.sdkLibraryProperties,
907 &module.Library.Module.properties,
908 &module.Library.Module.dexpreoptProperties,
909 &module.Library.Module.deviceProperties,
910 &module.Library.Module.protoProperties,
911 )
912
913 module.Library.Module.properties.Installable = proptools.BoolPtr(true)
914 module.Library.Module.deviceProperties.IsSDKLibrary = true
Inseob Kimc0907f12019-02-08 21:00:45 +0900915}
Sundong Ahn054b19a2018-10-19 13:46:09 +0900916
Jaewoong Jung4f158ee2019-07-11 10:05:35 -0700917// java_sdk_library is a special Java library that provides optional platform APIs to apps.
918// In practice, it can be viewed as a combination of several modules: 1) stubs library that clients
919// are linked against to, 2) droiddoc module that internally generates API stubs source files,
920// 3) the real runtime shared library that implements the APIs, and 4) XML file for adding
921// the runtime lib to the classpath at runtime if requested via <uses-library>.
Inseob Kimc0907f12019-02-08 21:00:45 +0900922func SdkLibraryFactory() android.Module {
923 module := &SdkLibrary{}
924 module.InitSdkLibraryProperties()
Jooyung Han58f26ab2019-12-18 15:34:32 +0900925 android.InitApexModule(module)
Sundong Ahn054b19a2018-10-19 13:46:09 +0900926 InitJavaModule(module, android.HostAndDeviceSupported)
Paul Duffin3375e352020-04-28 10:44:03 +0100927
928 // Initialize the map from scope to scope specific properties.
929 scopeToProperties := make(map[*apiScope]*ApiScopeProperties)
930 for _, scope := range allApiScopes {
931 scopeToProperties[scope] = scope.scopeSpecificProperties(module)
932 }
933 module.scopeToProperties = scopeToProperties
934
Paul Duffinf0229202020-04-29 16:47:28 +0100935 module.SetDefaultableHook(func(ctx android.DefaultableHookContext) { module.CreateInternalModules(ctx) })
Jiyong Parkc678ad32018-04-10 13:07:10 +0900936 return module
937}
Colin Cross79c7c262019-04-17 11:11:46 -0700938
939//
940// SDK library prebuilts
941//
942
Paul Duffin56d44902020-01-31 13:36:25 +0000943// Properties associated with each api scope.
944type sdkLibraryScopeProperties struct {
Colin Cross79c7c262019-04-17 11:11:46 -0700945 Jars []string `android:"path"`
946
947 Sdk_version *string
948
Colin Cross79c7c262019-04-17 11:11:46 -0700949 // List of shared java libs that this module has dependencies to
950 Libs []string
Paul Duffin3d1248c2020-04-09 00:10:17 +0100951
952 // The stub sources.
953 Stub_srcs []string `android:"path"`
Paul Duffin1fd005d2020-04-09 01:08:11 +0100954
955 // The current.txt
956 Current_api string `android:"path"`
957
958 // The removed.txt
959 Removed_api string `android:"path"`
Colin Cross79c7c262019-04-17 11:11:46 -0700960}
961
Paul Duffin56d44902020-01-31 13:36:25 +0000962type sdkLibraryImportProperties struct {
Paul Duffinfcfd7912020-01-31 17:54:30 +0000963 // List of shared java libs, common to all scopes, that this module has
964 // dependencies to
965 Libs []string
Paul Duffin56d44902020-01-31 13:36:25 +0000966}
967
Colin Cross79c7c262019-04-17 11:11:46 -0700968type sdkLibraryImport struct {
969 android.ModuleBase
970 android.DefaultableModuleBase
971 prebuilt android.Prebuilt
Paul Duffindd46f712020-02-10 13:37:10 +0000972 android.ApexModuleBase
973 android.SdkBase
Colin Cross79c7c262019-04-17 11:11:46 -0700974
975 properties sdkLibraryImportProperties
976
Paul Duffin46a26a82020-04-07 19:27:04 +0100977 // Map from api scope to the scope specific property structure.
978 scopeProperties map[*apiScope]*sdkLibraryScopeProperties
979
Paul Duffin56d44902020-01-31 13:36:25 +0000980 commonToSdkLibraryAndImport
Colin Cross79c7c262019-04-17 11:11:46 -0700981}
982
983var _ SdkLibraryDependency = (*sdkLibraryImport)(nil)
984
Paul Duffin46a26a82020-04-07 19:27:04 +0100985// The type of a structure that contains a field of type sdkLibraryScopeProperties
986// for each apiscope in allApiScopes, e.g. something like:
987// struct {
988// Public sdkLibraryScopeProperties
989// System sdkLibraryScopeProperties
990// ...
991// }
992var allScopeStructType = createAllScopePropertiesStructType()
993
994// Dynamically create a structure type for each apiscope in allApiScopes.
995func createAllScopePropertiesStructType() reflect.Type {
996 var fields []reflect.StructField
997 for _, apiScope := range allApiScopes {
998 field := reflect.StructField{
999 Name: apiScope.fieldName,
1000 Type: reflect.TypeOf(sdkLibraryScopeProperties{}),
1001 }
1002 fields = append(fields, field)
1003 }
1004
1005 return reflect.StructOf(fields)
1006}
1007
1008// Create an instance of the scope specific structure type and return a map
1009// from apiscope to a pointer to each scope specific field.
1010func createPropertiesInstance() (interface{}, map[*apiScope]*sdkLibraryScopeProperties) {
1011 allScopePropertiesPtr := reflect.New(allScopeStructType)
1012 allScopePropertiesStruct := allScopePropertiesPtr.Elem()
1013 scopeProperties := make(map[*apiScope]*sdkLibraryScopeProperties)
1014
1015 for _, apiScope := range allApiScopes {
1016 field := allScopePropertiesStruct.FieldByName(apiScope.fieldName)
1017 scopeProperties[apiScope] = field.Addr().Interface().(*sdkLibraryScopeProperties)
1018 }
1019
1020 return allScopePropertiesPtr.Interface(), scopeProperties
1021}
1022
Jaewoong Jung4f158ee2019-07-11 10:05:35 -07001023// java_sdk_library_import imports a prebuilt java_sdk_library.
Colin Cross79c7c262019-04-17 11:11:46 -07001024func sdkLibraryImportFactory() android.Module {
1025 module := &sdkLibraryImport{}
1026
Paul Duffin46a26a82020-04-07 19:27:04 +01001027 allScopeProperties, scopeToProperties := createPropertiesInstance()
1028 module.scopeProperties = scopeToProperties
1029 module.AddProperties(&module.properties, allScopeProperties)
Colin Cross79c7c262019-04-17 11:11:46 -07001030
Paul Duffin0bdcb272020-02-06 15:24:57 +00001031 android.InitPrebuiltModule(module, &[]string{""})
Paul Duffindd46f712020-02-10 13:37:10 +00001032 android.InitApexModule(module)
1033 android.InitSdkAwareModule(module)
Colin Cross79c7c262019-04-17 11:11:46 -07001034 InitJavaModule(module, android.HostAndDeviceSupported)
1035
1036 android.AddLoadHook(module, func(mctx android.LoadHookContext) { module.createInternalModules(mctx) })
1037 return module
1038}
1039
1040func (module *sdkLibraryImport) Prebuilt() *android.Prebuilt {
1041 return &module.prebuilt
1042}
1043
1044func (module *sdkLibraryImport) Name() string {
1045 return module.prebuilt.Name(module.ModuleBase.Name())
1046}
1047
1048func (module *sdkLibraryImport) createInternalModules(mctx android.LoadHookContext) {
Colin Cross79c7c262019-04-17 11:11:46 -07001049
Paul Duffin50061512020-01-21 16:31:05 +00001050 // If the build is configured to use prebuilts then force this to be preferred.
1051 if mctx.Config().UnbundledBuildUsePrebuiltSdks() {
1052 module.prebuilt.ForcePrefer()
1053 }
1054
Paul Duffin46a26a82020-04-07 19:27:04 +01001055 for apiScope, scopeProperties := range module.scopeProperties {
Paul Duffin56d44902020-01-31 13:36:25 +00001056 if len(scopeProperties.Jars) == 0 {
1057 continue
1058 }
1059
Paul Duffinbbb546b2020-04-09 00:07:11 +01001060 module.createJavaImportForStubs(mctx, apiScope, scopeProperties)
Paul Duffin3d1248c2020-04-09 00:10:17 +01001061
1062 module.createPrebuiltStubsSources(mctx, apiScope, scopeProperties)
Paul Duffin56d44902020-01-31 13:36:25 +00001063 }
Colin Cross79c7c262019-04-17 11:11:46 -07001064
1065 javaSdkLibraries := javaSdkLibraries(mctx.Config())
1066 javaSdkLibrariesLock.Lock()
1067 defer javaSdkLibrariesLock.Unlock()
1068 *javaSdkLibraries = append(*javaSdkLibraries, module.BaseModuleName())
1069}
1070
Paul Duffinbbb546b2020-04-09 00:07:11 +01001071func (module *sdkLibraryImport) createJavaImportForStubs(mctx android.LoadHookContext, apiScope *apiScope, scopeProperties *sdkLibraryScopeProperties) {
1072 // Creates a java import for the jar with ".stubs" suffix
1073 props := struct {
1074 Name *string
1075 Soc_specific *bool
1076 Device_specific *bool
1077 Product_specific *bool
1078 System_ext_specific *bool
1079 Sdk_version *string
1080 Libs []string
1081 Jars []string
1082 Prefer *bool
1083 }{}
1084 props.Name = proptools.StringPtr(apiScope.stubsModuleName(module.BaseModuleName()))
1085 props.Sdk_version = scopeProperties.Sdk_version
1086 // Prepend any of the libs from the legacy public properties to the libs for each of the
1087 // scopes to avoid having to duplicate them in each scope.
1088 props.Libs = append(module.properties.Libs, scopeProperties.Libs...)
1089 props.Jars = scopeProperties.Jars
1090 if module.SocSpecific() {
1091 props.Soc_specific = proptools.BoolPtr(true)
1092 } else if module.DeviceSpecific() {
1093 props.Device_specific = proptools.BoolPtr(true)
1094 } else if module.ProductSpecific() {
1095 props.Product_specific = proptools.BoolPtr(true)
1096 } else if module.SystemExtSpecific() {
1097 props.System_ext_specific = proptools.BoolPtr(true)
1098 }
1099 // If the build should use prebuilt sdks then set prefer to true on the stubs library.
1100 // That will cause the prebuilt version of the stubs to override the source version.
1101 if mctx.Config().UnbundledBuildUsePrebuiltSdks() {
1102 props.Prefer = proptools.BoolPtr(true)
1103 }
1104 mctx.CreateModule(ImportFactory, &props)
1105}
1106
Paul Duffin3d1248c2020-04-09 00:10:17 +01001107func (module *sdkLibraryImport) createPrebuiltStubsSources(mctx android.LoadHookContext, apiScope *apiScope, scopeProperties *sdkLibraryScopeProperties) {
1108 props := struct {
1109 Name *string
1110 Srcs []string
1111 }{}
1112 props.Name = proptools.StringPtr(apiScope.docsModuleName(module.BaseModuleName()))
1113 props.Srcs = scopeProperties.Stub_srcs
1114 mctx.CreateModule(PrebuiltStubsSourcesFactory, &props)
1115}
1116
Colin Cross79c7c262019-04-17 11:11:46 -07001117func (module *sdkLibraryImport) DepsMutator(ctx android.BottomUpMutatorContext) {
Paul Duffin46a26a82020-04-07 19:27:04 +01001118 for apiScope, scopeProperties := range module.scopeProperties {
Paul Duffin56d44902020-01-31 13:36:25 +00001119 if len(scopeProperties.Jars) == 0 {
1120 continue
1121 }
1122
1123 // Add dependencies to the prebuilt stubs library
1124 ctx.AddVariationDependencies(nil, apiScope.stubsTag, apiScope.stubsModuleName(module.BaseModuleName()))
1125 }
Colin Cross79c7c262019-04-17 11:11:46 -07001126}
1127
1128func (module *sdkLibraryImport) GenerateAndroidBuildActions(ctx android.ModuleContext) {
1129 // Record the paths to the prebuilt stubs library.
1130 ctx.VisitDirectDeps(func(to android.Module) {
1131 tag := ctx.OtherModuleDependencyTag(to)
1132
Paul Duffin56d44902020-01-31 13:36:25 +00001133 if lib, ok := to.(Dependency); ok {
1134 if scopeTag, ok := tag.(scopeDependencyTag); ok {
1135 apiScope := scopeTag.apiScope
1136 scopePaths := module.getScopePaths(apiScope)
1137 scopePaths.stubsHeaderPath = lib.HeaderJars()
1138 }
Colin Cross79c7c262019-04-17 11:11:46 -07001139 }
1140 })
1141}
1142
Paul Duffin56d44902020-01-31 13:36:25 +00001143func (module *sdkLibraryImport) sdkJars(
1144 ctx android.BaseModuleContext,
1145 sdkVersion sdkSpec) android.Paths {
1146
Paul Duffin50061512020-01-21 16:31:05 +00001147 // If a specific numeric version has been requested then use prebuilt versions of the sdk.
1148 if sdkVersion.version.isNumbered() {
1149 return PrebuiltJars(ctx, module.BaseModuleName(), sdkVersion)
1150 }
1151
Paul Duffin56d44902020-01-31 13:36:25 +00001152 var apiScope *apiScope
1153 switch sdkVersion.kind {
1154 case sdkSystem:
1155 apiScope = apiScopeSystem
1156 case sdkTest:
1157 apiScope = apiScopeTest
1158 default:
1159 apiScope = apiScopePublic
1160 }
1161
1162 paths := module.getScopePaths(apiScope)
1163 return paths.stubsHeaderPath
1164}
1165
Colin Cross79c7c262019-04-17 11:11:46 -07001166// to satisfy SdkLibraryDependency interface
Jiyong Park6a927c42020-01-21 02:03:43 +09001167func (module *sdkLibraryImport) SdkHeaderJars(ctx android.BaseModuleContext, sdkVersion sdkSpec) android.Paths {
Colin Cross79c7c262019-04-17 11:11:46 -07001168 // This module is just a wrapper for the prebuilt stubs.
Paul Duffin56d44902020-01-31 13:36:25 +00001169 return module.sdkJars(ctx, sdkVersion)
Colin Cross79c7c262019-04-17 11:11:46 -07001170}
1171
1172// to satisfy SdkLibraryDependency interface
Jiyong Park6a927c42020-01-21 02:03:43 +09001173func (module *sdkLibraryImport) SdkImplementationJars(ctx android.BaseModuleContext, sdkVersion sdkSpec) android.Paths {
Colin Cross79c7c262019-04-17 11:11:46 -07001174 // This module is just a wrapper for the stubs.
Paul Duffin56d44902020-01-31 13:36:25 +00001175 return module.sdkJars(ctx, sdkVersion)
Colin Cross79c7c262019-04-17 11:11:46 -07001176}
Jiyong Parke3833882020-02-17 17:28:10 +09001177
1178//
1179// java_sdk_library_xml
1180//
1181type sdkLibraryXml struct {
1182 android.ModuleBase
1183 android.DefaultableModuleBase
1184 android.ApexModuleBase
1185
1186 properties sdkLibraryXmlProperties
1187
1188 outputFilePath android.OutputPath
1189 installDirPath android.InstallPath
1190}
1191
1192type sdkLibraryXmlProperties struct {
1193 // canonical name of the lib
1194 Lib_name *string
1195}
1196
1197// java_sdk_library_xml builds the permission xml file for a java_sdk_library.
1198// Not to be used directly by users. java_sdk_library internally uses this.
1199func sdkLibraryXmlFactory() android.Module {
1200 module := &sdkLibraryXml{}
1201
1202 module.AddProperties(&module.properties)
1203
1204 android.InitApexModule(module)
1205 android.InitAndroidArchModule(module, android.DeviceSupported, android.MultilibCommon)
1206
1207 return module
1208}
1209
1210// from android.PrebuiltEtcModule
1211func (module *sdkLibraryXml) SubDir() string {
1212 return "permissions"
1213}
1214
1215// from android.PrebuiltEtcModule
1216func (module *sdkLibraryXml) OutputFile() android.OutputPath {
1217 return module.outputFilePath
1218}
1219
1220// from android.ApexModule
1221func (module *sdkLibraryXml) AvailableFor(what string) bool {
1222 return true
1223}
1224
1225func (module *sdkLibraryXml) DepsMutator(ctx android.BottomUpMutatorContext) {
1226 // do nothing
1227}
1228
1229// File path to the runtime implementation library
1230func (module *sdkLibraryXml) implPath() string {
1231 implName := proptools.String(module.properties.Lib_name)
1232 if apexName := module.ApexName(); apexName != "" {
1233 // TODO(b/146468504): ApexName() is only a soong module name, not apex name.
1234 // In most cases, this works fine. But when apex_name is set or override_apex is used
1235 // this can be wrong.
1236 return fmt.Sprintf("/apex/%s/javalib/%s.jar", apexName, implName)
1237 }
1238 partition := "system"
1239 if module.SocSpecific() {
1240 partition = "vendor"
1241 } else if module.DeviceSpecific() {
1242 partition = "odm"
1243 } else if module.ProductSpecific() {
1244 partition = "product"
1245 } else if module.SystemExtSpecific() {
1246 partition = "system_ext"
1247 }
1248 return "/" + partition + "/framework/" + implName + ".jar"
1249}
1250
1251func (module *sdkLibraryXml) GenerateAndroidBuildActions(ctx android.ModuleContext) {
1252 libName := proptools.String(module.properties.Lib_name)
1253 xmlContent := fmt.Sprintf(permissionsTemplate, libName, module.implPath())
1254
1255 module.outputFilePath = android.PathForModuleOut(ctx, libName+".xml").OutputPath
1256 rule := android.NewRuleBuilder()
1257 rule.Command().
1258 Text("/bin/bash -c \"echo -e '" + xmlContent + "'\" > ").
1259 Output(module.outputFilePath)
1260
1261 rule.Build(pctx, ctx, "java_sdk_xml", "Permission XML")
1262
1263 module.installDirPath = android.PathForModuleInstall(ctx, "etc", module.SubDir())
1264}
1265
1266func (module *sdkLibraryXml) AndroidMkEntries() []android.AndroidMkEntries {
1267 if !module.IsForPlatform() {
1268 return []android.AndroidMkEntries{android.AndroidMkEntries{
1269 Disabled: true,
1270 }}
1271 }
1272
1273 return []android.AndroidMkEntries{android.AndroidMkEntries{
1274 Class: "ETC",
1275 OutputFile: android.OptionalPathForPath(module.outputFilePath),
1276 ExtraEntries: []android.AndroidMkExtraEntriesFunc{
1277 func(entries *android.AndroidMkEntries) {
1278 entries.SetString("LOCAL_MODULE_TAGS", "optional")
1279 entries.SetString("LOCAL_MODULE_PATH", module.installDirPath.ToMakePath().String())
1280 entries.SetString("LOCAL_INSTALLED_MODULE_STEM", module.outputFilePath.Base())
1281 },
1282 },
1283 }}
1284}
Paul Duffindd46f712020-02-10 13:37:10 +00001285
1286type sdkLibrarySdkMemberType struct {
1287 android.SdkMemberTypeBase
1288}
1289
1290func (s *sdkLibrarySdkMemberType) AddDependencies(mctx android.BottomUpMutatorContext, dependencyTag blueprint.DependencyTag, names []string) {
1291 mctx.AddVariationDependencies(nil, dependencyTag, names...)
1292}
1293
1294func (s *sdkLibrarySdkMemberType) IsInstance(module android.Module) bool {
1295 _, ok := module.(*SdkLibrary)
1296 return ok
1297}
1298
1299func (s *sdkLibrarySdkMemberType) AddPrebuiltModule(ctx android.SdkMemberContext, member android.SdkMember) android.BpModule {
1300 return ctx.SnapshotBuilder().AddPrebuiltModule(member, "java_sdk_library_import")
1301}
1302
1303func (s *sdkLibrarySdkMemberType) CreateVariantPropertiesStruct() android.SdkMemberProperties {
1304 return &sdkLibrarySdkMemberProperties{}
1305}
1306
1307type sdkLibrarySdkMemberProperties struct {
1308 android.SdkMemberPropertiesBase
1309
1310 // Scope to per scope properties.
1311 Scopes map[*apiScope]scopeProperties
1312
1313 // Additional libraries that the exported stubs libraries depend upon.
1314 Libs []string
Paul Duffin3d1248c2020-04-09 00:10:17 +01001315
1316 // The Java stubs source files.
1317 Stub_srcs []string
Paul Duffindd46f712020-02-10 13:37:10 +00001318}
1319
1320type scopeProperties struct {
Paul Duffin1fd005d2020-04-09 01:08:11 +01001321 Jars android.Paths
1322 StubsSrcJar android.Path
1323 CurrentApiFile android.Path
1324 RemovedApiFile android.Path
1325 SdkVersion string
Paul Duffindd46f712020-02-10 13:37:10 +00001326}
1327
1328func (s *sdkLibrarySdkMemberProperties) PopulateFromVariant(ctx android.SdkMemberContext, variant android.Module) {
1329 sdk := variant.(*SdkLibrary)
1330
1331 s.Scopes = make(map[*apiScope]scopeProperties)
1332 for _, apiScope := range allApiScopes {
1333 paths := sdk.getScopePaths(apiScope)
1334 jars := paths.stubsImplPath
1335 if len(jars) > 0 {
1336 properties := scopeProperties{}
1337 properties.Jars = jars
1338 properties.SdkVersion = apiScope.sdkVersion
Paul Duffin3d1248c2020-04-09 00:10:17 +01001339 properties.StubsSrcJar = paths.stubsSrcJar
Paul Duffin1fd005d2020-04-09 01:08:11 +01001340 properties.CurrentApiFile = paths.currentApiFilePath
1341 properties.RemovedApiFile = paths.removedApiFilePath
Paul Duffindd46f712020-02-10 13:37:10 +00001342 s.Scopes[apiScope] = properties
1343 }
1344 }
1345
1346 s.Libs = sdk.properties.Libs
1347}
1348
1349func (s *sdkLibrarySdkMemberProperties) AddToPropertySet(ctx android.SdkMemberContext, propertySet android.BpPropertySet) {
1350 for _, apiScope := range allApiScopes {
1351 if properties, ok := s.Scopes[apiScope]; ok {
1352 scopeSet := propertySet.AddPropertySet(apiScope.name)
1353
Paul Duffin3d1248c2020-04-09 00:10:17 +01001354 scopeDir := filepath.Join("sdk_library", s.OsPrefix(), apiScope.name)
1355
Paul Duffindd46f712020-02-10 13:37:10 +00001356 var jars []string
1357 for _, p := range properties.Jars {
Paul Duffin3d1248c2020-04-09 00:10:17 +01001358 dest := filepath.Join(scopeDir, ctx.Name()+"-stubs.jar")
Paul Duffindd46f712020-02-10 13:37:10 +00001359 ctx.SnapshotBuilder().CopyToSnapshot(p, dest)
1360 jars = append(jars, dest)
1361 }
1362 scopeSet.AddProperty("jars", jars)
1363
Paul Duffin3d1248c2020-04-09 00:10:17 +01001364 // Merge the stubs source jar into the snapshot zip so that when it is unpacked
1365 // the source files are also unpacked.
1366 snapshotRelativeDir := filepath.Join(scopeDir, ctx.Name()+"_stub_sources")
1367 ctx.SnapshotBuilder().UnzipToSnapshot(properties.StubsSrcJar, snapshotRelativeDir)
1368 scopeSet.AddProperty("stub_srcs", []string{snapshotRelativeDir})
1369
Paul Duffin1fd005d2020-04-09 01:08:11 +01001370 if properties.CurrentApiFile != nil {
1371 currentApiSnapshotPath := filepath.Join(scopeDir, ctx.Name()+".txt")
1372 ctx.SnapshotBuilder().CopyToSnapshot(properties.CurrentApiFile, currentApiSnapshotPath)
1373 scopeSet.AddProperty("current_api", currentApiSnapshotPath)
1374 }
1375
1376 if properties.RemovedApiFile != nil {
1377 removedApiSnapshotPath := filepath.Join(scopeDir, ctx.Name()+"-removed.txt")
1378 ctx.SnapshotBuilder().CopyToSnapshot(properties.CurrentApiFile, removedApiSnapshotPath)
1379 scopeSet.AddProperty("removed_api", removedApiSnapshotPath)
1380 }
1381
Paul Duffindd46f712020-02-10 13:37:10 +00001382 if properties.SdkVersion != "" {
1383 scopeSet.AddProperty("sdk_version", properties.SdkVersion)
1384 }
1385 }
1386 }
1387
1388 if len(s.Libs) > 0 {
1389 propertySet.AddPropertyWithTag("libs", s.Libs, ctx.SnapshotBuilder().SdkMemberReferencePropertyTag(false))
1390 }
1391}