blob: 6fa315082792a40b1e88f4ff8b797e1dda3416be [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 Duffin46a26a82020-04-07 19:27:04 +010070 // The name of the field in the dynamically created structure.
71 fieldName string
72
Paul Duffind1b3a922020-01-22 11:57:20 +000073 // The tag to use to depend on the stubs library module.
74 stubsTag scopeDependencyTag
75
76 // The tag to use to depend on the stubs
77 apiFileTag scopeDependencyTag
78
79 // The scope specific prefix to add to the api file base of "current.txt" or "removed.txt".
80 apiFilePrefix string
81
82 // The scope specific prefix to add to the sdk library module name to construct a scope specific
83 // module name.
84 moduleSuffix string
85
Paul Duffind1b3a922020-01-22 11:57:20 +000086 // SDK version that the stubs library is built against. Note that this is always
87 // *current. Older stubs library built with a numbered SDK version is created from
88 // the prebuilt jar.
89 sdkVersion string
Paul Duffin1fb487d2020-04-07 18:50:10 +010090
91 // Extra arguments to pass to droidstubs for this scope.
92 droidstubsArgs []string
Paul Duffind1b3a922020-01-22 11:57:20 +000093}
94
95// Initialize a scope, creating and adding appropriate dependency tags
96func initApiScope(scope *apiScope) *apiScope {
Paul Duffin46a26a82020-04-07 19:27:04 +010097 scope.fieldName = proptools.FieldNameForProperty(scope.name)
Paul Duffind1b3a922020-01-22 11:57:20 +000098 scope.stubsTag = scopeDependencyTag{
99 name: scope.name + "-stubs",
100 apiScope: scope,
101 }
102 scope.apiFileTag = scopeDependencyTag{
103 name: scope.name + "-api",
104 apiScope: scope,
105 }
106 return scope
107}
108
109func (scope *apiScope) stubsModuleName(baseName string) string {
110 return baseName + sdkStubsLibrarySuffix + scope.moduleSuffix
111}
112
113func (scope *apiScope) docsModuleName(baseName string) string {
Paul Duffin91b883d2020-02-11 13:05:28 +0000114 return baseName + sdkStubsSourceSuffix + scope.moduleSuffix
Paul Duffind1b3a922020-01-22 11:57:20 +0000115}
116
117type apiScopes []*apiScope
118
119func (scopes apiScopes) Strings(accessor func(*apiScope) string) []string {
120 var list []string
121 for _, scope := range scopes {
122 list = append(list, accessor(scope))
123 }
124 return list
125}
126
Jiyong Parkc678ad32018-04-10 13:07:10 +0900127var (
Paul Duffind1b3a922020-01-22 11:57:20 +0000128 apiScopePublic = initApiScope(&apiScope{
129 name: "public",
130 sdkVersion: "current",
131 })
132 apiScopeSystem = initApiScope(&apiScope{
Anton Hansson6affb1f2020-04-28 16:47:41 +0100133 name: "system",
134 apiFilePrefix: "system-",
135 moduleSuffix: sdkSystemApiSuffix,
136 sdkVersion: "system_current",
137 droidstubsArgs: []string{"-showAnnotation android.annotation.SystemApi"},
Paul Duffind1b3a922020-01-22 11:57:20 +0000138 })
139 apiScopeTest = initApiScope(&apiScope{
Anton Hansson6affb1f2020-04-28 16:47:41 +0100140 name: "test",
141 apiFilePrefix: "test-",
142 moduleSuffix: sdkTestApiSuffix,
143 sdkVersion: "test_current",
144 droidstubsArgs: []string{"-showAnnotation android.annotation.TestApi"},
Paul Duffind1b3a922020-01-22 11:57:20 +0000145 })
146 allApiScopes = apiScopes{
147 apiScopePublic,
148 apiScopeSystem,
149 apiScopeTest,
150 }
Jiyong Parkc678ad32018-04-10 13:07:10 +0900151)
152
Jiyong Park82484c02018-04-23 21:41:26 +0900153var (
154 javaSdkLibrariesLock sync.Mutex
155)
156
Jiyong Parkc678ad32018-04-10 13:07:10 +0900157// TODO: these are big features that are currently missing
Jiyong Park1be96912018-05-28 18:02:19 +0900158// 1) disallowing linking to the runtime shared lib
159// 2) HTML generation
Jiyong Parkc678ad32018-04-10 13:07:10 +0900160
161func init() {
Paul Duffin43dc1cc2019-12-19 11:18:54 +0000162 RegisterSdkLibraryBuildComponents(android.InitRegistrationContext)
Jiyong Parkc678ad32018-04-10 13:07:10 +0900163
Jiyong Park82484c02018-04-23 21:41:26 +0900164 android.RegisterMakeVarsProvider(pctx, func(ctx android.MakeVarsContext) {
165 javaSdkLibraries := javaSdkLibraries(ctx.Config())
166 sort.Strings(*javaSdkLibraries)
167 ctx.Strict("JAVA_SDK_LIBRARIES", strings.Join(*javaSdkLibraries, " "))
168 })
Paul Duffindd46f712020-02-10 13:37:10 +0000169
170 // Register sdk member types.
171 android.RegisterSdkMemberType(&sdkLibrarySdkMemberType{
172 android.SdkMemberTypeBase{
173 PropertyName: "java_sdk_libs",
174 SupportsSdk: true,
175 },
176 })
Jiyong Parkc678ad32018-04-10 13:07:10 +0900177}
178
Paul Duffin43dc1cc2019-12-19 11:18:54 +0000179func RegisterSdkLibraryBuildComponents(ctx android.RegistrationContext) {
180 ctx.RegisterModuleType("java_sdk_library", SdkLibraryFactory)
181 ctx.RegisterModuleType("java_sdk_library_import", sdkLibraryImportFactory)
182}
183
Jiyong Parkc678ad32018-04-10 13:07:10 +0900184type sdkLibraryProperties struct {
Sundong Ahnf043cf62018-06-25 16:04:37 +0900185 // List of Java libraries that will be in the classpath when building stubs
186 Stub_only_libs []string `android:"arch_variant"`
187
Paul Duffin7a586d32019-12-30 17:09:34 +0000188 // list of package names that will be documented and publicized as API.
189 // This allows the API to be restricted to a subset of the source files provided.
190 // If this is unspecified then all the source files will be treated as being part
191 // of the API.
Jiyong Parkc678ad32018-04-10 13:07:10 +0900192 Api_packages []string
193
Jiyong Park5a2c9d72018-05-01 22:25:41 +0900194 // list of package names that must be hidden from the API
195 Hidden_api_packages []string
196
Paul Duffin749f98f2019-12-30 17:23:46 +0000197 // the relative path to the directory containing the api specification files.
198 // Defaults to "api".
199 Api_dir *string
200
Paul Duffin43db9be2019-12-30 17:35:49 +0000201 // If set to true there is no runtime library.
202 Api_only *bool
203
Paul Duffin11512472019-02-11 15:55:17 +0000204 // local files that are used within user customized droiddoc options.
205 Droiddoc_option_files []string
206
207 // additional droiddoc options
208 // Available variables for substitution:
209 //
210 // $(location <label>): the path to the droiddoc_option_files with name <label>
Sundong Ahndd567f92018-07-31 17:19:11 +0900211 Droiddoc_options []string
212
Sundong Ahn054b19a2018-10-19 13:46:09 +0900213 // a list of top-level directories containing files to merge qualifier annotations
214 // (i.e. those intended to be included in the stubs written) from.
215 Merge_annotations_dirs []string
216
217 // a list of top-level directories containing Java stub files to merge show/hide annotations from.
218 Merge_inclusion_annotations_dirs []string
219
220 // If set to true, the path of dist files is apistubs/core. Defaults to false.
221 Core_lib *bool
222
Sundong Ahn80a87b32019-05-13 15:02:50 +0900223 // don't create dist rules.
224 No_dist *bool `blueprint:"mutated"`
225
Paul Duffin37e0b772019-12-30 17:20:10 +0000226 // indicates whether system and test apis should be managed.
227 Has_system_and_test_apis bool `blueprint:"mutated"`
228
Jiyong Parkc678ad32018-04-10 13:07:10 +0900229 // TODO: determines whether to create HTML doc or not
230 //Html_doc *bool
231}
232
Paul Duffind1b3a922020-01-22 11:57:20 +0000233type scopePaths struct {
Paul Duffin1fd005d2020-04-09 01:08:11 +0100234 stubsHeaderPath android.Paths
235 stubsImplPath android.Paths
236 currentApiFilePath android.Path
237 removedApiFilePath android.Path
238 stubsSrcJar android.Path
Paul Duffind1b3a922020-01-22 11:57:20 +0000239}
240
Paul Duffin56d44902020-01-31 13:36:25 +0000241// Common code between sdk library and sdk library import
242type commonToSdkLibraryAndImport struct {
243 scopePaths map[*apiScope]*scopePaths
244}
245
246func (c *commonToSdkLibraryAndImport) getScopePaths(scope *apiScope) *scopePaths {
247 if c.scopePaths == nil {
248 c.scopePaths = make(map[*apiScope]*scopePaths)
249 }
250 paths := c.scopePaths[scope]
251 if paths == nil {
252 paths = &scopePaths{}
253 c.scopePaths[scope] = paths
254 }
255
256 return paths
257}
258
Inseob Kimc0907f12019-02-08 21:00:45 +0900259type SdkLibrary struct {
Sundong Ahn054b19a2018-10-19 13:46:09 +0900260 Library
Jiyong Parkc678ad32018-04-10 13:07:10 +0900261
Sundong Ahn054b19a2018-10-19 13:46:09 +0900262 sdkLibraryProperties sdkLibraryProperties
Jiyong Parkc678ad32018-04-10 13:07:10 +0900263
Paul Duffin56d44902020-01-31 13:36:25 +0000264 commonToSdkLibraryAndImport
Jiyong Parkc678ad32018-04-10 13:07:10 +0900265}
266
Inseob Kimc0907f12019-02-08 21:00:45 +0900267var _ Dependency = (*SdkLibrary)(nil)
268var _ SdkLibraryDependency = (*SdkLibrary)(nil)
Colin Cross897d2ed2019-02-11 14:03:51 -0800269
Paul Duffind1b3a922020-01-22 11:57:20 +0000270func (module *SdkLibrary) getActiveApiScopes() apiScopes {
271 if module.sdkLibraryProperties.Has_system_and_test_apis {
272 return allApiScopes
273 } else {
274 return apiScopes{apiScopePublic}
275 }
276}
277
Paul Duffine74ac732020-02-06 13:51:46 +0000278var xmlPermissionsFileTag = dependencyTag{name: "xml-permissions-file"}
279
Jiyong Parke3833882020-02-17 17:28:10 +0900280func IsXmlPermissionsFileDepTag(depTag blueprint.DependencyTag) bool {
281 if dt, ok := depTag.(dependencyTag); ok {
282 return dt == xmlPermissionsFileTag
283 }
284 return false
285}
286
Inseob Kimc0907f12019-02-08 21:00:45 +0900287func (module *SdkLibrary) DepsMutator(ctx android.BottomUpMutatorContext) {
Paul Duffind1b3a922020-01-22 11:57:20 +0000288 for _, apiScope := range module.getActiveApiScopes() {
289 // Add dependencies to the stubs library
Paul Duffin50061512020-01-21 16:31:05 +0000290 ctx.AddVariationDependencies(nil, apiScope.stubsTag, module.stubsName(apiScope))
Paul Duffind1b3a922020-01-22 11:57:20 +0000291
Paul Duffin50061512020-01-21 16:31:05 +0000292 // And the api file
Paul Duffind1b3a922020-01-22 11:57:20 +0000293 ctx.AddVariationDependencies(nil, apiScope.apiFileTag, module.docsName(apiScope))
Sundong Ahn054b19a2018-10-19 13:46:09 +0900294 }
295
Paul Duffine74ac732020-02-06 13:51:46 +0000296 if !proptools.Bool(module.sdkLibraryProperties.Api_only) {
297 // Add dependency to the rule for generating the xml permissions file
Jiyong Parke3833882020-02-17 17:28:10 +0900298 ctx.AddDependency(module, xmlPermissionsFileTag, module.xmlFileName())
Paul Duffine74ac732020-02-06 13:51:46 +0000299 }
300
Sundong Ahn054b19a2018-10-19 13:46:09 +0900301 module.Library.deps(ctx)
Jiyong Parkc678ad32018-04-10 13:07:10 +0900302}
303
Inseob Kimc0907f12019-02-08 21:00:45 +0900304func (module *SdkLibrary) GenerateAndroidBuildActions(ctx android.ModuleContext) {
Paul Duffin43db9be2019-12-30 17:35:49 +0000305 // Don't build an implementation library if this is api only.
306 if !proptools.Bool(module.sdkLibraryProperties.Api_only) {
307 module.Library.GenerateAndroidBuildActions(ctx)
308 }
Sundong Ahn054b19a2018-10-19 13:46:09 +0900309
Sundong Ahn57368eb2018-07-06 11:20:23 +0900310 // Record the paths to the header jars of the library (stubs and impl).
Paul Duffind1b3a922020-01-22 11:57:20 +0000311 // When this java_sdk_library is depended upon from others via "libs" property,
Jiyong Parkc678ad32018-04-10 13:07:10 +0900312 // the recorded paths will be returned depending on the link type of the caller.
313 ctx.VisitDirectDeps(func(to android.Module) {
314 otherName := ctx.OtherModuleName(to)
315 tag := ctx.OtherModuleDependencyTag(to)
316
Sundong Ahn57368eb2018-07-06 11:20:23 +0900317 if lib, ok := to.(Dependency); ok {
Paul Duffind1b3a922020-01-22 11:57:20 +0000318 if scopeTag, ok := tag.(scopeDependencyTag); ok {
319 apiScope := scopeTag.apiScope
320 scopePaths := module.getScopePaths(apiScope)
321 scopePaths.stubsHeaderPath = lib.HeaderJars()
322 scopePaths.stubsImplPath = lib.ImplementationJars()
Jiyong Parkc678ad32018-04-10 13:07:10 +0900323 }
324 }
Paul Duffin3d1248c2020-04-09 00:10:17 +0100325 if doc, ok := to.(ApiStubsProvider); ok {
Paul Duffind1b3a922020-01-22 11:57:20 +0000326 if scopeTag, ok := tag.(scopeDependencyTag); ok {
327 apiScope := scopeTag.apiScope
328 scopePaths := module.getScopePaths(apiScope)
Paul Duffin1fd005d2020-04-09 01:08:11 +0100329 scopePaths.currentApiFilePath = doc.ApiFilePath()
330 scopePaths.removedApiFilePath = doc.RemovedApiFilePath()
Paul Duffin3d1248c2020-04-09 00:10:17 +0100331 scopePaths.stubsSrcJar = doc.StubsSrcJar()
Paul Duffind1b3a922020-01-22 11:57:20 +0000332 } else {
Sundong Ahn20e998b2018-07-24 11:19:26 +0900333 ctx.ModuleErrorf("depends on module %q of unknown tag %q", otherName, tag)
334 }
335 }
Jiyong Parkc678ad32018-04-10 13:07:10 +0900336 })
337}
338
Jiyong Park0b0e1b92019-12-03 13:24:29 +0900339func (module *SdkLibrary) AndroidMkEntries() []android.AndroidMkEntries {
Paul Duffin43db9be2019-12-30 17:35:49 +0000340 if proptools.Bool(module.sdkLibraryProperties.Api_only) {
341 return nil
342 }
Jiyong Park0b0e1b92019-12-03 13:24:29 +0900343 entriesList := module.Library.AndroidMkEntries()
344 entries := &entriesList[0]
Jaewoong Jungb0c127c2019-08-29 14:56:03 -0700345 entries.Required = append(entries.Required, module.xmlFileName())
Jiyong Park0b0e1b92019-12-03 13:24:29 +0900346 return entriesList
Jiyong Park82484c02018-04-23 21:41:26 +0900347}
348
Jiyong Parkc678ad32018-04-10 13:07:10 +0900349// Module name of the stubs library
Paul Duffind1b3a922020-01-22 11:57:20 +0000350func (module *SdkLibrary) stubsName(apiScope *apiScope) string {
351 return apiScope.stubsModuleName(module.BaseModuleName())
Jiyong Parkc678ad32018-04-10 13:07:10 +0900352}
353
354// Module name of the docs
Paul Duffind1b3a922020-01-22 11:57:20 +0000355func (module *SdkLibrary) docsName(apiScope *apiScope) string {
356 return apiScope.docsModuleName(module.BaseModuleName())
Jiyong Parkc678ad32018-04-10 13:07:10 +0900357}
358
359// Module name of the runtime implementation library
Inseob Kimc0907f12019-02-08 21:00:45 +0900360func (module *SdkLibrary) implName() string {
Sundong Ahn054b19a2018-10-19 13:46:09 +0900361 return module.BaseModuleName()
Jiyong Parkc678ad32018-04-10 13:07:10 +0900362}
363
Jiyong Parkc678ad32018-04-10 13:07:10 +0900364// Module name of the XML file for the lib
Inseob Kimc0907f12019-02-08 21:00:45 +0900365func (module *SdkLibrary) xmlFileName() string {
Jiyong Parkc678ad32018-04-10 13:07:10 +0900366 return module.BaseModuleName() + sdkXmlFileSuffix
367}
368
Anton Hansson5fd5d242020-03-27 19:43:19 +0000369// The dist path of the stub artifacts
370func (module *SdkLibrary) apiDistPath(apiScope *apiScope) string {
371 if module.ModuleBase.Owner() != "" {
372 return path.Join("apistubs", module.ModuleBase.Owner(), apiScope.name)
373 } else if Bool(module.sdkLibraryProperties.Core_lib) {
374 return path.Join("apistubs", "core", apiScope.name)
375 } else {
376 return path.Join("apistubs", "android", apiScope.name)
377 }
378}
379
Paul Duffin12ceb462019-12-24 20:31:31 +0000380// Get the sdk version for use when compiling the stubs library.
Paul Duffind1b3a922020-01-22 11:57:20 +0000381func (module *SdkLibrary) sdkVersionForStubsLibrary(mctx android.LoadHookContext, apiScope *apiScope) string {
Paul Duffin12ceb462019-12-24 20:31:31 +0000382 sdkDep := decodeSdkDep(mctx, sdkContext(&module.Library))
383 if sdkDep.hasStandardLibs() {
384 // If building against a standard sdk then use the sdk version appropriate for the scope.
Paul Duffind1b3a922020-01-22 11:57:20 +0000385 return apiScope.sdkVersion
Paul Duffin12ceb462019-12-24 20:31:31 +0000386 } else {
387 // Otherwise, use no system module.
388 return "none"
389 }
390}
391
Paul Duffind1b3a922020-01-22 11:57:20 +0000392func (module *SdkLibrary) latestApiFilegroupName(apiScope *apiScope) string {
393 return ":" + module.BaseModuleName() + ".api." + apiScope.name + ".latest"
Jiyong Park58c518b2018-05-12 22:29:12 +0900394}
Jiyong Parkc678ad32018-04-10 13:07:10 +0900395
Paul Duffind1b3a922020-01-22 11:57:20 +0000396func (module *SdkLibrary) latestRemovedApiFilegroupName(apiScope *apiScope) string {
397 return ":" + module.BaseModuleName() + "-removed.api." + apiScope.name + ".latest"
Jiyong Parkc678ad32018-04-10 13:07:10 +0900398}
399
400// Creates a static java library that has API stubs
Paul Duffind1b3a922020-01-22 11:57:20 +0000401func (module *SdkLibrary) createStubsLibrary(mctx android.LoadHookContext, apiScope *apiScope) {
Jiyong Parkc678ad32018-04-10 13:07:10 +0900402 props := struct {
Sundong Ahn0d7dff42019-12-04 12:53:44 +0900403 Name *string
404 Srcs []string
Paul Duffin367ab912019-12-23 19:40:36 +0000405 Installable *bool
Sundong Ahn0d7dff42019-12-04 12:53:44 +0900406 Sdk_version *string
Paul Duffin12ceb462019-12-24 20:31:31 +0000407 System_modules *string
Paul Duffinab8da5d2020-02-07 16:12:04 +0000408 Patch_module *string
Sundong Ahn0d7dff42019-12-04 12:53:44 +0900409 Libs []string
410 Soc_specific *bool
411 Device_specific *bool
412 Product_specific *bool
413 System_ext_specific *bool
414 Compile_dex *bool
Sundong Ahn0d7dff42019-12-04 12:53:44 +0900415 Java_version *string
416 Product_variables struct {
Jiyong Park82484c02018-04-23 21:41:26 +0900417 Pdk struct {
418 Enabled *bool
419 }
Jiyong Parkc678ad32018-04-10 13:07:10 +0900420 }
Sundong Ahn054b19a2018-10-19 13:46:09 +0900421 Openjdk9 struct {
422 Srcs []string
423 Javacflags []string
424 }
Anton Hansson5fd5d242020-03-27 19:43:19 +0000425 Dist struct {
426 Targets []string
427 Dest *string
428 Dir *string
429 Tag *string
430 }
Jiyong Parkc678ad32018-04-10 13:07:10 +0900431 }{}
432
Jiyong Parkdf130542018-04-27 16:29:21 +0900433 props.Name = proptools.StringPtr(module.stubsName(apiScope))
Jiyong Parkc678ad32018-04-10 13:07:10 +0900434 // sources are generated from the droiddoc
Jiyong Parkdf130542018-04-27 16:29:21 +0900435 props.Srcs = []string{":" + module.docsName(apiScope)}
Paul Duffin12ceb462019-12-24 20:31:31 +0000436 sdkVersion := module.sdkVersionForStubsLibrary(mctx, apiScope)
Paul Duffin52d398a2019-06-11 12:31:14 +0100437 props.Sdk_version = proptools.StringPtr(sdkVersion)
Paul Duffin12ceb462019-12-24 20:31:31 +0000438 props.System_modules = module.Library.Module.deviceProperties.System_modules
Paul Duffinab8da5d2020-02-07 16:12:04 +0000439 props.Patch_module = module.Library.Module.properties.Patch_module
Paul Duffin367ab912019-12-23 19:40:36 +0000440 props.Installable = proptools.BoolPtr(false)
Sundong Ahn054b19a2018-10-19 13:46:09 +0900441 props.Libs = module.sdkLibraryProperties.Stub_only_libs
Jiyong Park82484c02018-04-23 21:41:26 +0900442 props.Product_variables.Pdk.Enabled = proptools.BoolPtr(false)
Sundong Ahn054b19a2018-10-19 13:46:09 +0900443 props.Openjdk9.Srcs = module.Library.Module.properties.Openjdk9.Srcs
444 props.Openjdk9.Javacflags = module.Library.Module.properties.Openjdk9.Javacflags
445 props.Java_version = module.Library.Module.properties.Java_version
446 if module.Library.Module.deviceProperties.Compile_dex != nil {
447 props.Compile_dex = module.Library.Module.deviceProperties.Compile_dex
Sundong Ahndd567f92018-07-31 17:19:11 +0900448 }
Jiyong Parkc678ad32018-04-10 13:07:10 +0900449
450 if module.SocSpecific() {
451 props.Soc_specific = proptools.BoolPtr(true)
452 } else if module.DeviceSpecific() {
453 props.Device_specific = proptools.BoolPtr(true)
454 } else if module.ProductSpecific() {
455 props.Product_specific = proptools.BoolPtr(true)
Sundong Ahn0d7dff42019-12-04 12:53:44 +0900456 } else if module.SystemExtSpecific() {
457 props.System_ext_specific = proptools.BoolPtr(true)
Jiyong Parkc678ad32018-04-10 13:07:10 +0900458 }
Anton Hansson5fd5d242020-03-27 19:43:19 +0000459 // Dist the class jar artifact for sdk builds.
460 if !Bool(module.sdkLibraryProperties.No_dist) {
461 props.Dist.Targets = []string{"sdk", "win_sdk"}
462 props.Dist.Dest = proptools.StringPtr(fmt.Sprintf("%v.jar", module.BaseModuleName()))
463 props.Dist.Dir = proptools.StringPtr(module.apiDistPath(apiScope))
464 props.Dist.Tag = proptools.StringPtr(".jar")
465 }
Jiyong Parkc678ad32018-04-10 13:07:10 +0900466
Colin Cross84dfc3d2019-09-25 11:33:01 -0700467 mctx.CreateModule(LibraryFactory, &props)
Jiyong Parkc678ad32018-04-10 13:07:10 +0900468}
469
Paul Duffin6d0886e2020-04-07 18:49:53 +0100470// Creates a droidstubs module that creates stubs source files from the given full source
Jiyong Parkc678ad32018-04-10 13:07:10 +0900471// files
Paul Duffind1b3a922020-01-22 11:57:20 +0000472func (module *SdkLibrary) createStubsSources(mctx android.LoadHookContext, apiScope *apiScope) {
Jiyong Parkc678ad32018-04-10 13:07:10 +0900473 props := struct {
Sundong Ahn054b19a2018-10-19 13:46:09 +0900474 Name *string
475 Srcs []string
476 Installable *bool
Paul Duffin52d398a2019-06-11 12:31:14 +0100477 Sdk_version *string
Paul Duffin12ceb462019-12-24 20:31:31 +0000478 System_modules *string
Sundong Ahn054b19a2018-10-19 13:46:09 +0900479 Libs []string
Paul Duffin11512472019-02-11 15:55:17 +0000480 Arg_files []string
Sundong Ahn054b19a2018-10-19 13:46:09 +0900481 Args *string
Sundong Ahn054b19a2018-10-19 13:46:09 +0900482 Java_version *string
483 Merge_annotations_dirs []string
484 Merge_inclusion_annotations_dirs []string
485 Check_api struct {
Inseob Kim38449af2019-02-28 14:24:05 +0900486 Current ApiToCheck
487 Last_released ApiToCheck
488 Ignore_missing_latest_api *bool
Jiyong Park58c518b2018-05-12 22:29:12 +0900489 }
Sundong Ahn1b92c822018-05-29 11:35:17 +0900490 Aidl struct {
491 Include_dirs []string
492 Local_include_dirs []string
493 }
Anton Hansson5fd5d242020-03-27 19:43:19 +0000494 Dist struct {
495 Targets []string
496 Dest *string
497 Dir *string
498 }
Jiyong Parkc678ad32018-04-10 13:07:10 +0900499 }{}
500
Paul Duffin250e6192019-06-07 10:44:37 +0100501 sdkDep := decodeSdkDep(mctx, sdkContext(&module.Library))
Paul Duffin12ceb462019-12-24 20:31:31 +0000502 // Use the platform API if standard libraries were requested, otherwise use
503 // no default libraries.
Paul Duffin52d398a2019-06-11 12:31:14 +0100504 sdkVersion := ""
505 if !sdkDep.hasStandardLibs() {
506 sdkVersion = "none"
507 }
Paul Duffin250e6192019-06-07 10:44:37 +0100508
Jiyong Parkdf130542018-04-27 16:29:21 +0900509 props.Name = proptools.StringPtr(module.docsName(apiScope))
Sundong Ahn054b19a2018-10-19 13:46:09 +0900510 props.Srcs = append(props.Srcs, module.Library.Module.properties.Srcs...)
Paul Duffin52d398a2019-06-11 12:31:14 +0100511 props.Sdk_version = proptools.StringPtr(sdkVersion)
Paul Duffin12ceb462019-12-24 20:31:31 +0000512 props.System_modules = module.Library.Module.deviceProperties.System_modules
Jiyong Parkc678ad32018-04-10 13:07:10 +0900513 props.Installable = proptools.BoolPtr(false)
Sundong Ahne6f0b052018-06-05 16:46:14 +0900514 // A droiddoc module has only one Libs property and doesn't distinguish between
515 // shared libs and static libs. So we need to add both of these libs to Libs property.
Sundong Ahn054b19a2018-10-19 13:46:09 +0900516 props.Libs = module.Library.Module.properties.Libs
517 props.Libs = append(props.Libs, module.Library.Module.properties.Static_libs...)
518 props.Aidl.Include_dirs = module.Library.Module.deviceProperties.Aidl.Include_dirs
519 props.Aidl.Local_include_dirs = module.Library.Module.deviceProperties.Aidl.Local_include_dirs
Sundong Ahn054b19a2018-10-19 13:46:09 +0900520 props.Java_version = module.Library.Module.properties.Java_version
Jiyong Parkc678ad32018-04-10 13:07:10 +0900521
Sundong Ahn054b19a2018-10-19 13:46:09 +0900522 props.Merge_annotations_dirs = module.sdkLibraryProperties.Merge_annotations_dirs
523 props.Merge_inclusion_annotations_dirs = module.sdkLibraryProperties.Merge_inclusion_annotations_dirs
524
Paul Duffin6d0886e2020-04-07 18:49:53 +0100525 droidstubsArgs := []string{}
Paul Duffin235ffff2019-12-24 10:41:30 +0000526 if len(module.sdkLibraryProperties.Api_packages) != 0 {
Paul Duffin6d0886e2020-04-07 18:49:53 +0100527 droidstubsArgs = append(droidstubsArgs, "--stub-packages "+strings.Join(module.sdkLibraryProperties.Api_packages, ":"))
Paul Duffin235ffff2019-12-24 10:41:30 +0000528 }
529 if len(module.sdkLibraryProperties.Hidden_api_packages) != 0 {
Paul Duffin6d0886e2020-04-07 18:49:53 +0100530 droidstubsArgs = append(droidstubsArgs,
Paul Duffin235ffff2019-12-24 10:41:30 +0000531 android.JoinWithPrefix(module.sdkLibraryProperties.Hidden_api_packages, " --hide-package "))
532 }
Paul Duffin6d0886e2020-04-07 18:49:53 +0100533 droidstubsArgs = append(droidstubsArgs, module.sdkLibraryProperties.Droiddoc_options...)
Paul Duffin235ffff2019-12-24 10:41:30 +0000534 disabledWarnings := []string{
535 "MissingPermission",
536 "BroadcastBehavior",
537 "HiddenSuperclass",
538 "DeprecationMismatch",
539 "UnavailableSymbol",
540 "SdkConstant",
541 "HiddenTypeParameter",
542 "Todo",
543 "Typo",
544 }
Paul Duffin6d0886e2020-04-07 18:49:53 +0100545 droidstubsArgs = append(droidstubsArgs, android.JoinWithPrefix(disabledWarnings, "--hide "))
Sundong Ahnfb2721f2018-09-17 13:23:09 +0900546
Paul Duffin1fb487d2020-04-07 18:50:10 +0100547 // Add in scope specific arguments.
548 droidstubsArgs = append(droidstubsArgs, apiScope.droidstubsArgs...)
Paul Duffin11512472019-02-11 15:55:17 +0000549 props.Arg_files = module.sdkLibraryProperties.Droiddoc_option_files
Paul Duffin6d0886e2020-04-07 18:49:53 +0100550 props.Args = proptools.StringPtr(strings.Join(droidstubsArgs, " "))
Jiyong Parkc678ad32018-04-10 13:07:10 +0900551
552 // List of APIs identified from the provided source files are created. They are later
553 // compared against to the not-yet-released (a.k.a current) list of APIs and to the
554 // last-released (a.k.a numbered) list of API.
Paul Duffind1b3a922020-01-22 11:57:20 +0000555 currentApiFileName := apiScope.apiFilePrefix + "current.txt"
556 removedApiFileName := apiScope.apiFilePrefix + "removed.txt"
Paul Duffin749f98f2019-12-30 17:23:46 +0000557 apiDir := module.getApiDir()
558 currentApiFileName = path.Join(apiDir, currentApiFileName)
559 removedApiFileName = path.Join(apiDir, removedApiFileName)
Jiyong Parkc678ad32018-04-10 13:07:10 +0900560
Jiyong Park58c518b2018-05-12 22:29:12 +0900561 // check against the not-yet-release API
562 props.Check_api.Current.Api_file = proptools.StringPtr(currentApiFileName)
563 props.Check_api.Current.Removed_api_file = proptools.StringPtr(removedApiFileName)
Jiyong Park58c518b2018-05-12 22:29:12 +0900564
565 // check against the latest released API
566 props.Check_api.Last_released.Api_file = proptools.StringPtr(
567 module.latestApiFilegroupName(apiScope))
568 props.Check_api.Last_released.Removed_api_file = proptools.StringPtr(
569 module.latestRemovedApiFilegroupName(apiScope))
Inseob Kim38449af2019-02-28 14:24:05 +0900570 props.Check_api.Ignore_missing_latest_api = proptools.BoolPtr(true)
Jiyong Park58c518b2018-05-12 22:29:12 +0900571
Anton Hansson5fd5d242020-03-27 19:43:19 +0000572 // Dist the api txt artifact for sdk builds.
573 if !Bool(module.sdkLibraryProperties.No_dist) {
574 props.Dist.Targets = []string{"sdk", "win_sdk"}
575 props.Dist.Dest = proptools.StringPtr(fmt.Sprintf("%v.txt", module.BaseModuleName()))
576 props.Dist.Dir = proptools.StringPtr(path.Join(module.apiDistPath(apiScope), "api"))
577 }
578
Colin Cross84dfc3d2019-09-25 11:33:01 -0700579 mctx.CreateModule(DroidstubsFactory, &props)
Jiyong Parkc678ad32018-04-10 13:07:10 +0900580}
581
Jooyung Han5e9013b2020-03-10 06:23:13 +0900582func (module *SdkLibrary) DepIsInSameApex(mctx android.BaseModuleContext, dep android.Module) bool {
583 depTag := mctx.OtherModuleDependencyTag(dep)
584 if depTag == xmlPermissionsFileTag {
585 return true
586 }
587 return module.Library.DepIsInSameApex(mctx, dep)
588}
589
Jiyong Parkc678ad32018-04-10 13:07:10 +0900590// Creates the xml file that publicizes the runtime library
Colin Crossf8b860a2019-04-16 14:43:28 -0700591func (module *SdkLibrary) createXmlFile(mctx android.LoadHookContext) {
Jiyong Parke3833882020-02-17 17:28:10 +0900592 props := struct {
Sundong Ahn0d7dff42019-12-04 12:53:44 +0900593 Name *string
Jiyong Parke3833882020-02-17 17:28:10 +0900594 Lib_name *string
Sundong Ahn0d7dff42019-12-04 12:53:44 +0900595 Soc_specific *bool
596 Device_specific *bool
597 Product_specific *bool
598 System_ext_specific *bool
Jooyung Han5e9013b2020-03-10 06:23:13 +0900599 Apex_available []string
Jiyong Parke3833882020-02-17 17:28:10 +0900600 }{
Jooyung Han5e9013b2020-03-10 06:23:13 +0900601 Name: proptools.StringPtr(module.xmlFileName()),
602 Lib_name: proptools.StringPtr(module.BaseModuleName()),
603 Apex_available: module.ApexProperties.Apex_available,
Jiyong Parkc678ad32018-04-10 13:07:10 +0900604 }
Jiyong Parke3833882020-02-17 17:28:10 +0900605
606 if module.SocSpecific() {
607 props.Soc_specific = proptools.BoolPtr(true)
608 } else if module.DeviceSpecific() {
609 props.Device_specific = proptools.BoolPtr(true)
610 } else if module.ProductSpecific() {
611 props.Product_specific = proptools.BoolPtr(true)
612 } else if module.SystemExtSpecific() {
613 props.System_ext_specific = proptools.BoolPtr(true)
614 }
615
616 mctx.CreateModule(sdkLibraryXmlFactory, &props)
Jiyong Parkc678ad32018-04-10 13:07:10 +0900617}
618
Paul Duffin50061512020-01-21 16:31:05 +0000619func PrebuiltJars(ctx android.BaseModuleContext, baseName string, s sdkSpec) android.Paths {
Jiyong Park6a927c42020-01-21 02:03:43 +0900620 var ver sdkVersion
621 var kind sdkKind
622 if s.usePrebuilt(ctx) {
623 ver = s.version
624 kind = s.kind
Jiyong Parkc678ad32018-04-10 13:07:10 +0900625 } else {
Jiyong Park6a927c42020-01-21 02:03:43 +0900626 // We don't have prebuilt SDK for the specific sdkVersion.
627 // Instead of breaking the build, fallback to use "system_current"
628 ver = sdkVersionCurrent
629 kind = sdkSystem
Sundong Ahn054b19a2018-10-19 13:46:09 +0900630 }
Jiyong Park6a927c42020-01-21 02:03:43 +0900631
632 dir := filepath.Join("prebuilts", "sdk", ver.String(), kind.String())
Paul Duffin50061512020-01-21 16:31:05 +0000633 jar := filepath.Join(dir, baseName+".jar")
Sundong Ahn054b19a2018-10-19 13:46:09 +0900634 jarPath := android.ExistentPathForSource(ctx, jar)
Sundong Ahnae418ac2019-02-28 15:01:28 +0900635 if !jarPath.Valid() {
Colin Cross07c88562020-01-07 09:34:44 -0800636 if ctx.Config().AllowMissingDependencies() {
637 return android.Paths{android.PathForSource(ctx, jar)}
638 } else {
Jiyong Park6a927c42020-01-21 02:03:43 +0900639 ctx.PropertyErrorf("sdk_library", "invalid sdk version %q, %q does not exist", s.raw, jar)
Colin Cross07c88562020-01-07 09:34:44 -0800640 }
Sundong Ahnae418ac2019-02-28 15:01:28 +0900641 return nil
642 }
Sundong Ahn054b19a2018-10-19 13:46:09 +0900643 return android.Paths{jarPath.Path()}
644}
645
Paul Duffind1b3a922020-01-22 11:57:20 +0000646func (module *SdkLibrary) sdkJars(
647 ctx android.BaseModuleContext,
648 sdkVersion sdkSpec,
649 headerJars bool) android.Paths {
650
Paul Duffin50061512020-01-21 16:31:05 +0000651 // If a specific numeric version has been requested then use prebuilt versions of the sdk.
652 if sdkVersion.version.isNumbered() {
653 return PrebuiltJars(ctx, module.BaseModuleName(), sdkVersion)
Sundong Ahn054b19a2018-10-19 13:46:09 +0900654 } else {
Paul Duffind1b3a922020-01-22 11:57:20 +0000655 if !sdkVersion.specified() {
656 if headerJars {
657 return module.Library.HeaderJars()
658 } else {
659 return module.Library.ImplementationJars()
660 }
661 }
Paul Duffin726d23c2020-01-22 16:30:37 +0000662 var apiScope *apiScope
Jiyong Park6a927c42020-01-21 02:03:43 +0900663 switch sdkVersion.kind {
664 case sdkSystem:
Paul Duffin726d23c2020-01-22 16:30:37 +0000665 apiScope = apiScopeSystem
666 case sdkTest:
667 apiScope = apiScopeTest
Jiyong Park6a927c42020-01-21 02:03:43 +0900668 case sdkPrivate:
Sundong Ahn054b19a2018-10-19 13:46:09 +0900669 return module.Library.HeaderJars()
Jiyong Park6a927c42020-01-21 02:03:43 +0900670 default:
Paul Duffin726d23c2020-01-22 16:30:37 +0000671 apiScope = apiScopePublic
Paul Duffind1b3a922020-01-22 11:57:20 +0000672 }
673
Paul Duffin726d23c2020-01-22 16:30:37 +0000674 paths := module.getScopePaths(apiScope)
Paul Duffind1b3a922020-01-22 11:57:20 +0000675 if headerJars {
676 return paths.stubsHeaderPath
677 } else {
678 return paths.stubsImplPath
Sundong Ahn054b19a2018-10-19 13:46:09 +0900679 }
Jiyong Parkc678ad32018-04-10 13:07:10 +0900680 }
681}
682
Sundong Ahn241cd372018-07-13 16:16:44 +0900683// to satisfy SdkLibraryDependency interface
Paul Duffind1b3a922020-01-22 11:57:20 +0000684func (module *SdkLibrary) SdkHeaderJars(ctx android.BaseModuleContext, sdkVersion sdkSpec) android.Paths {
685 return module.sdkJars(ctx, sdkVersion, true /*headerJars*/)
686}
687
688// to satisfy SdkLibraryDependency interface
Jiyong Park6a927c42020-01-21 02:03:43 +0900689func (module *SdkLibrary) SdkImplementationJars(ctx android.BaseModuleContext, sdkVersion sdkSpec) android.Paths {
Paul Duffind1b3a922020-01-22 11:57:20 +0000690 return module.sdkJars(ctx, sdkVersion, false /*headerJars*/)
Sundong Ahn241cd372018-07-13 16:16:44 +0900691}
692
Sundong Ahn80a87b32019-05-13 15:02:50 +0900693func (module *SdkLibrary) SetNoDist() {
694 module.sdkLibraryProperties.No_dist = proptools.BoolPtr(true)
695}
696
Colin Cross571cccf2019-02-04 11:22:08 -0800697var javaSdkLibrariesKey = android.NewOnceKey("javaSdkLibraries")
698
Jiyong Park82484c02018-04-23 21:41:26 +0900699func javaSdkLibraries(config android.Config) *[]string {
Colin Cross571cccf2019-02-04 11:22:08 -0800700 return config.Once(javaSdkLibrariesKey, func() interface{} {
Jiyong Park82484c02018-04-23 21:41:26 +0900701 return &[]string{}
702 }).(*[]string)
703}
704
Paul Duffin749f98f2019-12-30 17:23:46 +0000705func (module *SdkLibrary) getApiDir() string {
706 return proptools.StringDefault(module.sdkLibraryProperties.Api_dir, "api")
707}
708
Jiyong Parkc678ad32018-04-10 13:07:10 +0900709// For a java_sdk_library module, create internal modules for stubs, docs,
710// runtime libs and xml file. If requested, the stubs and docs are created twice
711// once for public API level and once for system API level
Colin Crossf8b860a2019-04-16 14:43:28 -0700712func (module *SdkLibrary) CreateInternalModules(mctx android.LoadHookContext) {
Inseob Kim6e93ac92019-03-21 17:43:49 +0900713 if len(module.Library.Module.properties.Srcs) == 0 {
Inseob Kimc0907f12019-02-08 21:00:45 +0900714 mctx.PropertyErrorf("srcs", "java_sdk_library must specify srcs")
Jooyung Han58f26ab2019-12-18 15:34:32 +0900715 return
Inseob Kimc0907f12019-02-08 21:00:45 +0900716 }
717
Paul Duffin37e0b772019-12-30 17:20:10 +0000718 // If this builds against standard libraries (i.e. is not part of the core libraries)
719 // then assume it provides both system and test apis. Otherwise, assume it does not and
720 // also assume it does not contribute to the dist build.
721 sdkDep := decodeSdkDep(mctx, sdkContext(&module.Library))
722 hasSystemAndTestApis := sdkDep.hasStandardLibs()
723 module.sdkLibraryProperties.Has_system_and_test_apis = hasSystemAndTestApis
724 module.sdkLibraryProperties.No_dist = proptools.BoolPtr(!hasSystemAndTestApis)
725
Inseob Kim8098faa2019-03-18 10:19:51 +0900726 missing_current_api := false
727
Paul Duffind1b3a922020-01-22 11:57:20 +0000728 activeScopes := module.getActiveApiScopes()
729
Paul Duffin749f98f2019-12-30 17:23:46 +0000730 apiDir := module.getApiDir()
Paul Duffind1b3a922020-01-22 11:57:20 +0000731 for _, scope := range activeScopes {
Inseob Kim8098faa2019-03-18 10:19:51 +0900732 for _, api := range []string{"current.txt", "removed.txt"} {
Paul Duffind1b3a922020-01-22 11:57:20 +0000733 path := path.Join(mctx.ModuleDir(), apiDir, scope.apiFilePrefix+api)
Inseob Kim8098faa2019-03-18 10:19:51 +0900734 p := android.ExistentPathForSource(mctx, path)
735 if !p.Valid() {
736 mctx.ModuleErrorf("Current api file %#v doesn't exist", path)
737 missing_current_api = true
738 }
739 }
740 }
741
742 if missing_current_api {
743 script := "build/soong/scripts/gen-java-current-api-files.sh"
744 p := android.ExistentPathForSource(mctx, script)
745
746 if !p.Valid() {
747 panic(fmt.Sprintf("script file %s doesn't exist", script))
748 }
749
750 mctx.ModuleErrorf("One or more current api files are missing. "+
751 "You can update them by:\n"+
Paul Duffin37e0b772019-12-30 17:20:10 +0000752 "%s %q %s && m update-api",
Paul Duffind1b3a922020-01-22 11:57:20 +0000753 script, filepath.Join(mctx.ModuleDir(), apiDir),
754 strings.Join(activeScopes.Strings(func(s *apiScope) string { return s.apiFilePrefix }), " "))
Inseob Kim8098faa2019-03-18 10:19:51 +0900755 return
756 }
757
Paul Duffind1b3a922020-01-22 11:57:20 +0000758 for _, scope := range activeScopes {
759 module.createStubsLibrary(mctx, scope)
760 module.createStubsSources(mctx, scope)
Inseob Kimc0907f12019-02-08 21:00:45 +0900761 }
762
Paul Duffin43db9be2019-12-30 17:35:49 +0000763 if !proptools.Bool(module.sdkLibraryProperties.Api_only) {
764 // for runtime
765 module.createXmlFile(mctx)
766
767 // record java_sdk_library modules so that they are exported to make
768 javaSdkLibraries := javaSdkLibraries(mctx.Config())
769 javaSdkLibrariesLock.Lock()
770 defer javaSdkLibrariesLock.Unlock()
771 *javaSdkLibraries = append(*javaSdkLibraries, module.BaseModuleName())
772 }
Inseob Kimc0907f12019-02-08 21:00:45 +0900773}
774
775func (module *SdkLibrary) InitSdkLibraryProperties() {
Sundong Ahn054b19a2018-10-19 13:46:09 +0900776 module.AddProperties(
777 &module.sdkLibraryProperties,
778 &module.Library.Module.properties,
779 &module.Library.Module.dexpreoptProperties,
780 &module.Library.Module.deviceProperties,
781 &module.Library.Module.protoProperties,
782 )
783
784 module.Library.Module.properties.Installable = proptools.BoolPtr(true)
785 module.Library.Module.deviceProperties.IsSDKLibrary = true
Inseob Kimc0907f12019-02-08 21:00:45 +0900786}
Sundong Ahn054b19a2018-10-19 13:46:09 +0900787
Jaewoong Jung4f158ee2019-07-11 10:05:35 -0700788// java_sdk_library is a special Java library that provides optional platform APIs to apps.
789// In practice, it can be viewed as a combination of several modules: 1) stubs library that clients
790// are linked against to, 2) droiddoc module that internally generates API stubs source files,
791// 3) the real runtime shared library that implements the APIs, and 4) XML file for adding
792// the runtime lib to the classpath at runtime if requested via <uses-library>.
Inseob Kimc0907f12019-02-08 21:00:45 +0900793func SdkLibraryFactory() android.Module {
794 module := &SdkLibrary{}
795 module.InitSdkLibraryProperties()
Jooyung Han58f26ab2019-12-18 15:34:32 +0900796 android.InitApexModule(module)
Sundong Ahn054b19a2018-10-19 13:46:09 +0900797 InitJavaModule(module, android.HostAndDeviceSupported)
Colin Crossf8b860a2019-04-16 14:43:28 -0700798 android.AddLoadHook(module, func(ctx android.LoadHookContext) { module.CreateInternalModules(ctx) })
Jiyong Parkc678ad32018-04-10 13:07:10 +0900799 return module
800}
Colin Cross79c7c262019-04-17 11:11:46 -0700801
802//
803// SDK library prebuilts
804//
805
Paul Duffin56d44902020-01-31 13:36:25 +0000806// Properties associated with each api scope.
807type sdkLibraryScopeProperties struct {
Colin Cross79c7c262019-04-17 11:11:46 -0700808 Jars []string `android:"path"`
809
810 Sdk_version *string
811
Colin Cross79c7c262019-04-17 11:11:46 -0700812 // List of shared java libs that this module has dependencies to
813 Libs []string
Paul Duffin3d1248c2020-04-09 00:10:17 +0100814
815 // The stub sources.
816 Stub_srcs []string `android:"path"`
Paul Duffin1fd005d2020-04-09 01:08:11 +0100817
818 // The current.txt
819 Current_api string `android:"path"`
820
821 // The removed.txt
822 Removed_api string `android:"path"`
Colin Cross79c7c262019-04-17 11:11:46 -0700823}
824
Paul Duffin56d44902020-01-31 13:36:25 +0000825type sdkLibraryImportProperties struct {
Paul Duffinfcfd7912020-01-31 17:54:30 +0000826 // List of shared java libs, common to all scopes, that this module has
827 // dependencies to
828 Libs []string
Paul Duffin56d44902020-01-31 13:36:25 +0000829}
830
Colin Cross79c7c262019-04-17 11:11:46 -0700831type sdkLibraryImport struct {
832 android.ModuleBase
833 android.DefaultableModuleBase
834 prebuilt android.Prebuilt
Paul Duffindd46f712020-02-10 13:37:10 +0000835 android.ApexModuleBase
836 android.SdkBase
Colin Cross79c7c262019-04-17 11:11:46 -0700837
838 properties sdkLibraryImportProperties
839
Paul Duffin46a26a82020-04-07 19:27:04 +0100840 // Map from api scope to the scope specific property structure.
841 scopeProperties map[*apiScope]*sdkLibraryScopeProperties
842
Paul Duffin56d44902020-01-31 13:36:25 +0000843 commonToSdkLibraryAndImport
Colin Cross79c7c262019-04-17 11:11:46 -0700844}
845
846var _ SdkLibraryDependency = (*sdkLibraryImport)(nil)
847
Paul Duffin46a26a82020-04-07 19:27:04 +0100848// The type of a structure that contains a field of type sdkLibraryScopeProperties
849// for each apiscope in allApiScopes, e.g. something like:
850// struct {
851// Public sdkLibraryScopeProperties
852// System sdkLibraryScopeProperties
853// ...
854// }
855var allScopeStructType = createAllScopePropertiesStructType()
856
857// Dynamically create a structure type for each apiscope in allApiScopes.
858func createAllScopePropertiesStructType() reflect.Type {
859 var fields []reflect.StructField
860 for _, apiScope := range allApiScopes {
861 field := reflect.StructField{
862 Name: apiScope.fieldName,
863 Type: reflect.TypeOf(sdkLibraryScopeProperties{}),
864 }
865 fields = append(fields, field)
866 }
867
868 return reflect.StructOf(fields)
869}
870
871// Create an instance of the scope specific structure type and return a map
872// from apiscope to a pointer to each scope specific field.
873func createPropertiesInstance() (interface{}, map[*apiScope]*sdkLibraryScopeProperties) {
874 allScopePropertiesPtr := reflect.New(allScopeStructType)
875 allScopePropertiesStruct := allScopePropertiesPtr.Elem()
876 scopeProperties := make(map[*apiScope]*sdkLibraryScopeProperties)
877
878 for _, apiScope := range allApiScopes {
879 field := allScopePropertiesStruct.FieldByName(apiScope.fieldName)
880 scopeProperties[apiScope] = field.Addr().Interface().(*sdkLibraryScopeProperties)
881 }
882
883 return allScopePropertiesPtr.Interface(), scopeProperties
884}
885
Jaewoong Jung4f158ee2019-07-11 10:05:35 -0700886// java_sdk_library_import imports a prebuilt java_sdk_library.
Colin Cross79c7c262019-04-17 11:11:46 -0700887func sdkLibraryImportFactory() android.Module {
888 module := &sdkLibraryImport{}
889
Paul Duffin46a26a82020-04-07 19:27:04 +0100890 allScopeProperties, scopeToProperties := createPropertiesInstance()
891 module.scopeProperties = scopeToProperties
892 module.AddProperties(&module.properties, allScopeProperties)
Colin Cross79c7c262019-04-17 11:11:46 -0700893
Paul Duffin0bdcb272020-02-06 15:24:57 +0000894 android.InitPrebuiltModule(module, &[]string{""})
Paul Duffindd46f712020-02-10 13:37:10 +0000895 android.InitApexModule(module)
896 android.InitSdkAwareModule(module)
Colin Cross79c7c262019-04-17 11:11:46 -0700897 InitJavaModule(module, android.HostAndDeviceSupported)
898
899 android.AddLoadHook(module, func(mctx android.LoadHookContext) { module.createInternalModules(mctx) })
900 return module
901}
902
903func (module *sdkLibraryImport) Prebuilt() *android.Prebuilt {
904 return &module.prebuilt
905}
906
907func (module *sdkLibraryImport) Name() string {
908 return module.prebuilt.Name(module.ModuleBase.Name())
909}
910
911func (module *sdkLibraryImport) createInternalModules(mctx android.LoadHookContext) {
Colin Cross79c7c262019-04-17 11:11:46 -0700912
Paul Duffin50061512020-01-21 16:31:05 +0000913 // If the build is configured to use prebuilts then force this to be preferred.
914 if mctx.Config().UnbundledBuildUsePrebuiltSdks() {
915 module.prebuilt.ForcePrefer()
916 }
917
Paul Duffin46a26a82020-04-07 19:27:04 +0100918 for apiScope, scopeProperties := range module.scopeProperties {
Paul Duffin56d44902020-01-31 13:36:25 +0000919 if len(scopeProperties.Jars) == 0 {
920 continue
921 }
922
Paul Duffinbbb546b2020-04-09 00:07:11 +0100923 module.createJavaImportForStubs(mctx, apiScope, scopeProperties)
Paul Duffin3d1248c2020-04-09 00:10:17 +0100924
925 module.createPrebuiltStubsSources(mctx, apiScope, scopeProperties)
Paul Duffin56d44902020-01-31 13:36:25 +0000926 }
Colin Cross79c7c262019-04-17 11:11:46 -0700927
928 javaSdkLibraries := javaSdkLibraries(mctx.Config())
929 javaSdkLibrariesLock.Lock()
930 defer javaSdkLibrariesLock.Unlock()
931 *javaSdkLibraries = append(*javaSdkLibraries, module.BaseModuleName())
932}
933
Paul Duffinbbb546b2020-04-09 00:07:11 +0100934func (module *sdkLibraryImport) createJavaImportForStubs(mctx android.LoadHookContext, apiScope *apiScope, scopeProperties *sdkLibraryScopeProperties) {
935 // Creates a java import for the jar with ".stubs" suffix
936 props := struct {
937 Name *string
938 Soc_specific *bool
939 Device_specific *bool
940 Product_specific *bool
941 System_ext_specific *bool
942 Sdk_version *string
943 Libs []string
944 Jars []string
945 Prefer *bool
946 }{}
947 props.Name = proptools.StringPtr(apiScope.stubsModuleName(module.BaseModuleName()))
948 props.Sdk_version = scopeProperties.Sdk_version
949 // Prepend any of the libs from the legacy public properties to the libs for each of the
950 // scopes to avoid having to duplicate them in each scope.
951 props.Libs = append(module.properties.Libs, scopeProperties.Libs...)
952 props.Jars = scopeProperties.Jars
953 if module.SocSpecific() {
954 props.Soc_specific = proptools.BoolPtr(true)
955 } else if module.DeviceSpecific() {
956 props.Device_specific = proptools.BoolPtr(true)
957 } else if module.ProductSpecific() {
958 props.Product_specific = proptools.BoolPtr(true)
959 } else if module.SystemExtSpecific() {
960 props.System_ext_specific = proptools.BoolPtr(true)
961 }
962 // If the build should use prebuilt sdks then set prefer to true on the stubs library.
963 // That will cause the prebuilt version of the stubs to override the source version.
964 if mctx.Config().UnbundledBuildUsePrebuiltSdks() {
965 props.Prefer = proptools.BoolPtr(true)
966 }
967 mctx.CreateModule(ImportFactory, &props)
968}
969
Paul Duffin3d1248c2020-04-09 00:10:17 +0100970func (module *sdkLibraryImport) createPrebuiltStubsSources(mctx android.LoadHookContext, apiScope *apiScope, scopeProperties *sdkLibraryScopeProperties) {
971 props := struct {
972 Name *string
973 Srcs []string
974 }{}
975 props.Name = proptools.StringPtr(apiScope.docsModuleName(module.BaseModuleName()))
976 props.Srcs = scopeProperties.Stub_srcs
977 mctx.CreateModule(PrebuiltStubsSourcesFactory, &props)
978}
979
Colin Cross79c7c262019-04-17 11:11:46 -0700980func (module *sdkLibraryImport) DepsMutator(ctx android.BottomUpMutatorContext) {
Paul Duffin46a26a82020-04-07 19:27:04 +0100981 for apiScope, scopeProperties := range module.scopeProperties {
Paul Duffin56d44902020-01-31 13:36:25 +0000982 if len(scopeProperties.Jars) == 0 {
983 continue
984 }
985
986 // Add dependencies to the prebuilt stubs library
987 ctx.AddVariationDependencies(nil, apiScope.stubsTag, apiScope.stubsModuleName(module.BaseModuleName()))
988 }
Colin Cross79c7c262019-04-17 11:11:46 -0700989}
990
991func (module *sdkLibraryImport) GenerateAndroidBuildActions(ctx android.ModuleContext) {
992 // Record the paths to the prebuilt stubs library.
993 ctx.VisitDirectDeps(func(to android.Module) {
994 tag := ctx.OtherModuleDependencyTag(to)
995
Paul Duffin56d44902020-01-31 13:36:25 +0000996 if lib, ok := to.(Dependency); ok {
997 if scopeTag, ok := tag.(scopeDependencyTag); ok {
998 apiScope := scopeTag.apiScope
999 scopePaths := module.getScopePaths(apiScope)
1000 scopePaths.stubsHeaderPath = lib.HeaderJars()
1001 }
Colin Cross79c7c262019-04-17 11:11:46 -07001002 }
1003 })
1004}
1005
Paul Duffin56d44902020-01-31 13:36:25 +00001006func (module *sdkLibraryImport) sdkJars(
1007 ctx android.BaseModuleContext,
1008 sdkVersion sdkSpec) android.Paths {
1009
Paul Duffin50061512020-01-21 16:31:05 +00001010 // If a specific numeric version has been requested then use prebuilt versions of the sdk.
1011 if sdkVersion.version.isNumbered() {
1012 return PrebuiltJars(ctx, module.BaseModuleName(), sdkVersion)
1013 }
1014
Paul Duffin56d44902020-01-31 13:36:25 +00001015 var apiScope *apiScope
1016 switch sdkVersion.kind {
1017 case sdkSystem:
1018 apiScope = apiScopeSystem
1019 case sdkTest:
1020 apiScope = apiScopeTest
1021 default:
1022 apiScope = apiScopePublic
1023 }
1024
1025 paths := module.getScopePaths(apiScope)
1026 return paths.stubsHeaderPath
1027}
1028
Colin Cross79c7c262019-04-17 11:11:46 -07001029// to satisfy SdkLibraryDependency interface
Jiyong Park6a927c42020-01-21 02:03:43 +09001030func (module *sdkLibraryImport) SdkHeaderJars(ctx android.BaseModuleContext, sdkVersion sdkSpec) android.Paths {
Colin Cross79c7c262019-04-17 11:11:46 -07001031 // This module is just a wrapper for the prebuilt stubs.
Paul Duffin56d44902020-01-31 13:36:25 +00001032 return module.sdkJars(ctx, sdkVersion)
Colin Cross79c7c262019-04-17 11:11:46 -07001033}
1034
1035// to satisfy SdkLibraryDependency interface
Jiyong Park6a927c42020-01-21 02:03:43 +09001036func (module *sdkLibraryImport) SdkImplementationJars(ctx android.BaseModuleContext, sdkVersion sdkSpec) android.Paths {
Colin Cross79c7c262019-04-17 11:11:46 -07001037 // This module is just a wrapper for the stubs.
Paul Duffin56d44902020-01-31 13:36:25 +00001038 return module.sdkJars(ctx, sdkVersion)
Colin Cross79c7c262019-04-17 11:11:46 -07001039}
Jiyong Parke3833882020-02-17 17:28:10 +09001040
1041//
1042// java_sdk_library_xml
1043//
1044type sdkLibraryXml struct {
1045 android.ModuleBase
1046 android.DefaultableModuleBase
1047 android.ApexModuleBase
1048
1049 properties sdkLibraryXmlProperties
1050
1051 outputFilePath android.OutputPath
1052 installDirPath android.InstallPath
1053}
1054
1055type sdkLibraryXmlProperties struct {
1056 // canonical name of the lib
1057 Lib_name *string
1058}
1059
1060// java_sdk_library_xml builds the permission xml file for a java_sdk_library.
1061// Not to be used directly by users. java_sdk_library internally uses this.
1062func sdkLibraryXmlFactory() android.Module {
1063 module := &sdkLibraryXml{}
1064
1065 module.AddProperties(&module.properties)
1066
1067 android.InitApexModule(module)
1068 android.InitAndroidArchModule(module, android.DeviceSupported, android.MultilibCommon)
1069
1070 return module
1071}
1072
1073// from android.PrebuiltEtcModule
1074func (module *sdkLibraryXml) SubDir() string {
1075 return "permissions"
1076}
1077
1078// from android.PrebuiltEtcModule
1079func (module *sdkLibraryXml) OutputFile() android.OutputPath {
1080 return module.outputFilePath
1081}
1082
1083// from android.ApexModule
1084func (module *sdkLibraryXml) AvailableFor(what string) bool {
1085 return true
1086}
1087
1088func (module *sdkLibraryXml) DepsMutator(ctx android.BottomUpMutatorContext) {
1089 // do nothing
1090}
1091
1092// File path to the runtime implementation library
1093func (module *sdkLibraryXml) implPath() string {
1094 implName := proptools.String(module.properties.Lib_name)
1095 if apexName := module.ApexName(); apexName != "" {
1096 // TODO(b/146468504): ApexName() is only a soong module name, not apex name.
1097 // In most cases, this works fine. But when apex_name is set or override_apex is used
1098 // this can be wrong.
1099 return fmt.Sprintf("/apex/%s/javalib/%s.jar", apexName, implName)
1100 }
1101 partition := "system"
1102 if module.SocSpecific() {
1103 partition = "vendor"
1104 } else if module.DeviceSpecific() {
1105 partition = "odm"
1106 } else if module.ProductSpecific() {
1107 partition = "product"
1108 } else if module.SystemExtSpecific() {
1109 partition = "system_ext"
1110 }
1111 return "/" + partition + "/framework/" + implName + ".jar"
1112}
1113
1114func (module *sdkLibraryXml) GenerateAndroidBuildActions(ctx android.ModuleContext) {
1115 libName := proptools.String(module.properties.Lib_name)
1116 xmlContent := fmt.Sprintf(permissionsTemplate, libName, module.implPath())
1117
1118 module.outputFilePath = android.PathForModuleOut(ctx, libName+".xml").OutputPath
1119 rule := android.NewRuleBuilder()
1120 rule.Command().
1121 Text("/bin/bash -c \"echo -e '" + xmlContent + "'\" > ").
1122 Output(module.outputFilePath)
1123
1124 rule.Build(pctx, ctx, "java_sdk_xml", "Permission XML")
1125
1126 module.installDirPath = android.PathForModuleInstall(ctx, "etc", module.SubDir())
1127}
1128
1129func (module *sdkLibraryXml) AndroidMkEntries() []android.AndroidMkEntries {
1130 if !module.IsForPlatform() {
1131 return []android.AndroidMkEntries{android.AndroidMkEntries{
1132 Disabled: true,
1133 }}
1134 }
1135
1136 return []android.AndroidMkEntries{android.AndroidMkEntries{
1137 Class: "ETC",
1138 OutputFile: android.OptionalPathForPath(module.outputFilePath),
1139 ExtraEntries: []android.AndroidMkExtraEntriesFunc{
1140 func(entries *android.AndroidMkEntries) {
1141 entries.SetString("LOCAL_MODULE_TAGS", "optional")
1142 entries.SetString("LOCAL_MODULE_PATH", module.installDirPath.ToMakePath().String())
1143 entries.SetString("LOCAL_INSTALLED_MODULE_STEM", module.outputFilePath.Base())
1144 },
1145 },
1146 }}
1147}
Paul Duffindd46f712020-02-10 13:37:10 +00001148
1149type sdkLibrarySdkMemberType struct {
1150 android.SdkMemberTypeBase
1151}
1152
1153func (s *sdkLibrarySdkMemberType) AddDependencies(mctx android.BottomUpMutatorContext, dependencyTag blueprint.DependencyTag, names []string) {
1154 mctx.AddVariationDependencies(nil, dependencyTag, names...)
1155}
1156
1157func (s *sdkLibrarySdkMemberType) IsInstance(module android.Module) bool {
1158 _, ok := module.(*SdkLibrary)
1159 return ok
1160}
1161
1162func (s *sdkLibrarySdkMemberType) AddPrebuiltModule(ctx android.SdkMemberContext, member android.SdkMember) android.BpModule {
1163 return ctx.SnapshotBuilder().AddPrebuiltModule(member, "java_sdk_library_import")
1164}
1165
1166func (s *sdkLibrarySdkMemberType) CreateVariantPropertiesStruct() android.SdkMemberProperties {
1167 return &sdkLibrarySdkMemberProperties{}
1168}
1169
1170type sdkLibrarySdkMemberProperties struct {
1171 android.SdkMemberPropertiesBase
1172
1173 // Scope to per scope properties.
1174 Scopes map[*apiScope]scopeProperties
1175
1176 // Additional libraries that the exported stubs libraries depend upon.
1177 Libs []string
Paul Duffin3d1248c2020-04-09 00:10:17 +01001178
1179 // The Java stubs source files.
1180 Stub_srcs []string
Paul Duffindd46f712020-02-10 13:37:10 +00001181}
1182
1183type scopeProperties struct {
Paul Duffin1fd005d2020-04-09 01:08:11 +01001184 Jars android.Paths
1185 StubsSrcJar android.Path
1186 CurrentApiFile android.Path
1187 RemovedApiFile android.Path
1188 SdkVersion string
Paul Duffindd46f712020-02-10 13:37:10 +00001189}
1190
1191func (s *sdkLibrarySdkMemberProperties) PopulateFromVariant(ctx android.SdkMemberContext, variant android.Module) {
1192 sdk := variant.(*SdkLibrary)
1193
1194 s.Scopes = make(map[*apiScope]scopeProperties)
1195 for _, apiScope := range allApiScopes {
1196 paths := sdk.getScopePaths(apiScope)
1197 jars := paths.stubsImplPath
1198 if len(jars) > 0 {
1199 properties := scopeProperties{}
1200 properties.Jars = jars
1201 properties.SdkVersion = apiScope.sdkVersion
Paul Duffin3d1248c2020-04-09 00:10:17 +01001202 properties.StubsSrcJar = paths.stubsSrcJar
Paul Duffin1fd005d2020-04-09 01:08:11 +01001203 properties.CurrentApiFile = paths.currentApiFilePath
1204 properties.RemovedApiFile = paths.removedApiFilePath
Paul Duffindd46f712020-02-10 13:37:10 +00001205 s.Scopes[apiScope] = properties
1206 }
1207 }
1208
1209 s.Libs = sdk.properties.Libs
1210}
1211
1212func (s *sdkLibrarySdkMemberProperties) AddToPropertySet(ctx android.SdkMemberContext, propertySet android.BpPropertySet) {
1213 for _, apiScope := range allApiScopes {
1214 if properties, ok := s.Scopes[apiScope]; ok {
1215 scopeSet := propertySet.AddPropertySet(apiScope.name)
1216
Paul Duffin3d1248c2020-04-09 00:10:17 +01001217 scopeDir := filepath.Join("sdk_library", s.OsPrefix(), apiScope.name)
1218
Paul Duffindd46f712020-02-10 13:37:10 +00001219 var jars []string
1220 for _, p := range properties.Jars {
Paul Duffin3d1248c2020-04-09 00:10:17 +01001221 dest := filepath.Join(scopeDir, ctx.Name()+"-stubs.jar")
Paul Duffindd46f712020-02-10 13:37:10 +00001222 ctx.SnapshotBuilder().CopyToSnapshot(p, dest)
1223 jars = append(jars, dest)
1224 }
1225 scopeSet.AddProperty("jars", jars)
1226
Paul Duffin3d1248c2020-04-09 00:10:17 +01001227 // Merge the stubs source jar into the snapshot zip so that when it is unpacked
1228 // the source files are also unpacked.
1229 snapshotRelativeDir := filepath.Join(scopeDir, ctx.Name()+"_stub_sources")
1230 ctx.SnapshotBuilder().UnzipToSnapshot(properties.StubsSrcJar, snapshotRelativeDir)
1231 scopeSet.AddProperty("stub_srcs", []string{snapshotRelativeDir})
1232
Paul Duffin1fd005d2020-04-09 01:08:11 +01001233 if properties.CurrentApiFile != nil {
1234 currentApiSnapshotPath := filepath.Join(scopeDir, ctx.Name()+".txt")
1235 ctx.SnapshotBuilder().CopyToSnapshot(properties.CurrentApiFile, currentApiSnapshotPath)
1236 scopeSet.AddProperty("current_api", currentApiSnapshotPath)
1237 }
1238
1239 if properties.RemovedApiFile != nil {
1240 removedApiSnapshotPath := filepath.Join(scopeDir, ctx.Name()+"-removed.txt")
1241 ctx.SnapshotBuilder().CopyToSnapshot(properties.CurrentApiFile, removedApiSnapshotPath)
1242 scopeSet.AddProperty("removed_api", removedApiSnapshotPath)
1243 }
1244
Paul Duffindd46f712020-02-10 13:37:10 +00001245 if properties.SdkVersion != "" {
1246 scopeSet.AddProperty("sdk_version", properties.SdkVersion)
1247 }
1248 }
1249 }
1250
1251 if len(s.Libs) > 0 {
1252 propertySet.AddPropertyWithTag("libs", s.Libs, ctx.SnapshotBuilder().SdkMemberReferencePropertyTag(false))
1253 }
1254}