blob: 9e3ad5bfa2f3105c634f243d40f3e107d1eede32 [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 Duffin7b78b4d2020-04-28 14:08:32 +0100501 // The stubs source processing uses the same compile time classpath when extracting the
502 // API from the implementation library as it does when compiling it. i.e. the same
503 // * sdk version
504 // * system_modules
505 // * libs (static_libs/libs)
Paul Duffin250e6192019-06-07 10:44:37 +0100506
Jiyong Parkdf130542018-04-27 16:29:21 +0900507 props.Name = proptools.StringPtr(module.docsName(apiScope))
Sundong Ahn054b19a2018-10-19 13:46:09 +0900508 props.Srcs = append(props.Srcs, module.Library.Module.properties.Srcs...)
Paul Duffin7b78b4d2020-04-28 14:08:32 +0100509 props.Sdk_version = module.Library.Module.deviceProperties.Sdk_version
Paul Duffin12ceb462019-12-24 20:31:31 +0000510 props.System_modules = module.Library.Module.deviceProperties.System_modules
Jiyong Parkc678ad32018-04-10 13:07:10 +0900511 props.Installable = proptools.BoolPtr(false)
Sundong Ahne6f0b052018-06-05 16:46:14 +0900512 // A droiddoc module has only one Libs property and doesn't distinguish between
513 // shared libs and static libs. So we need to add both of these libs to Libs property.
Sundong Ahn054b19a2018-10-19 13:46:09 +0900514 props.Libs = module.Library.Module.properties.Libs
515 props.Libs = append(props.Libs, module.Library.Module.properties.Static_libs...)
516 props.Aidl.Include_dirs = module.Library.Module.deviceProperties.Aidl.Include_dirs
517 props.Aidl.Local_include_dirs = module.Library.Module.deviceProperties.Aidl.Local_include_dirs
Sundong Ahn054b19a2018-10-19 13:46:09 +0900518 props.Java_version = module.Library.Module.properties.Java_version
Jiyong Parkc678ad32018-04-10 13:07:10 +0900519
Sundong Ahn054b19a2018-10-19 13:46:09 +0900520 props.Merge_annotations_dirs = module.sdkLibraryProperties.Merge_annotations_dirs
521 props.Merge_inclusion_annotations_dirs = module.sdkLibraryProperties.Merge_inclusion_annotations_dirs
522
Paul Duffin6d0886e2020-04-07 18:49:53 +0100523 droidstubsArgs := []string{}
Paul Duffin235ffff2019-12-24 10:41:30 +0000524 if len(module.sdkLibraryProperties.Api_packages) != 0 {
Paul Duffin6d0886e2020-04-07 18:49:53 +0100525 droidstubsArgs = append(droidstubsArgs, "--stub-packages "+strings.Join(module.sdkLibraryProperties.Api_packages, ":"))
Paul Duffin235ffff2019-12-24 10:41:30 +0000526 }
527 if len(module.sdkLibraryProperties.Hidden_api_packages) != 0 {
Paul Duffin6d0886e2020-04-07 18:49:53 +0100528 droidstubsArgs = append(droidstubsArgs,
Paul Duffin235ffff2019-12-24 10:41:30 +0000529 android.JoinWithPrefix(module.sdkLibraryProperties.Hidden_api_packages, " --hide-package "))
530 }
Paul Duffin6d0886e2020-04-07 18:49:53 +0100531 droidstubsArgs = append(droidstubsArgs, module.sdkLibraryProperties.Droiddoc_options...)
Paul Duffin235ffff2019-12-24 10:41:30 +0000532 disabledWarnings := []string{
533 "MissingPermission",
534 "BroadcastBehavior",
535 "HiddenSuperclass",
536 "DeprecationMismatch",
537 "UnavailableSymbol",
538 "SdkConstant",
539 "HiddenTypeParameter",
540 "Todo",
541 "Typo",
542 }
Paul Duffin6d0886e2020-04-07 18:49:53 +0100543 droidstubsArgs = append(droidstubsArgs, android.JoinWithPrefix(disabledWarnings, "--hide "))
Sundong Ahnfb2721f2018-09-17 13:23:09 +0900544
Paul Duffin1fb487d2020-04-07 18:50:10 +0100545 // Add in scope specific arguments.
546 droidstubsArgs = append(droidstubsArgs, apiScope.droidstubsArgs...)
Paul Duffin11512472019-02-11 15:55:17 +0000547 props.Arg_files = module.sdkLibraryProperties.Droiddoc_option_files
Paul Duffin6d0886e2020-04-07 18:49:53 +0100548 props.Args = proptools.StringPtr(strings.Join(droidstubsArgs, " "))
Jiyong Parkc678ad32018-04-10 13:07:10 +0900549
550 // List of APIs identified from the provided source files are created. They are later
551 // compared against to the not-yet-released (a.k.a current) list of APIs and to the
552 // last-released (a.k.a numbered) list of API.
Paul Duffind1b3a922020-01-22 11:57:20 +0000553 currentApiFileName := apiScope.apiFilePrefix + "current.txt"
554 removedApiFileName := apiScope.apiFilePrefix + "removed.txt"
Paul Duffin749f98f2019-12-30 17:23:46 +0000555 apiDir := module.getApiDir()
556 currentApiFileName = path.Join(apiDir, currentApiFileName)
557 removedApiFileName = path.Join(apiDir, removedApiFileName)
Jiyong Parkc678ad32018-04-10 13:07:10 +0900558
Jiyong Park58c518b2018-05-12 22:29:12 +0900559 // check against the not-yet-release API
560 props.Check_api.Current.Api_file = proptools.StringPtr(currentApiFileName)
561 props.Check_api.Current.Removed_api_file = proptools.StringPtr(removedApiFileName)
Jiyong Park58c518b2018-05-12 22:29:12 +0900562
563 // check against the latest released API
564 props.Check_api.Last_released.Api_file = proptools.StringPtr(
565 module.latestApiFilegroupName(apiScope))
566 props.Check_api.Last_released.Removed_api_file = proptools.StringPtr(
567 module.latestRemovedApiFilegroupName(apiScope))
Inseob Kim38449af2019-02-28 14:24:05 +0900568 props.Check_api.Ignore_missing_latest_api = proptools.BoolPtr(true)
Jiyong Park58c518b2018-05-12 22:29:12 +0900569
Anton Hansson5fd5d242020-03-27 19:43:19 +0000570 // Dist the api txt artifact for sdk builds.
571 if !Bool(module.sdkLibraryProperties.No_dist) {
572 props.Dist.Targets = []string{"sdk", "win_sdk"}
573 props.Dist.Dest = proptools.StringPtr(fmt.Sprintf("%v.txt", module.BaseModuleName()))
574 props.Dist.Dir = proptools.StringPtr(path.Join(module.apiDistPath(apiScope), "api"))
575 }
576
Colin Cross84dfc3d2019-09-25 11:33:01 -0700577 mctx.CreateModule(DroidstubsFactory, &props)
Jiyong Parkc678ad32018-04-10 13:07:10 +0900578}
579
Jooyung Han5e9013b2020-03-10 06:23:13 +0900580func (module *SdkLibrary) DepIsInSameApex(mctx android.BaseModuleContext, dep android.Module) bool {
581 depTag := mctx.OtherModuleDependencyTag(dep)
582 if depTag == xmlPermissionsFileTag {
583 return true
584 }
585 return module.Library.DepIsInSameApex(mctx, dep)
586}
587
Jiyong Parkc678ad32018-04-10 13:07:10 +0900588// Creates the xml file that publicizes the runtime library
Colin Crossf8b860a2019-04-16 14:43:28 -0700589func (module *SdkLibrary) createXmlFile(mctx android.LoadHookContext) {
Jiyong Parke3833882020-02-17 17:28:10 +0900590 props := struct {
Sundong Ahn0d7dff42019-12-04 12:53:44 +0900591 Name *string
Jiyong Parke3833882020-02-17 17:28:10 +0900592 Lib_name *string
Sundong Ahn0d7dff42019-12-04 12:53:44 +0900593 Soc_specific *bool
594 Device_specific *bool
595 Product_specific *bool
596 System_ext_specific *bool
Jooyung Han5e9013b2020-03-10 06:23:13 +0900597 Apex_available []string
Jiyong Parke3833882020-02-17 17:28:10 +0900598 }{
Jooyung Han5e9013b2020-03-10 06:23:13 +0900599 Name: proptools.StringPtr(module.xmlFileName()),
600 Lib_name: proptools.StringPtr(module.BaseModuleName()),
601 Apex_available: module.ApexProperties.Apex_available,
Jiyong Parkc678ad32018-04-10 13:07:10 +0900602 }
Jiyong Parke3833882020-02-17 17:28:10 +0900603
604 if module.SocSpecific() {
605 props.Soc_specific = proptools.BoolPtr(true)
606 } else if module.DeviceSpecific() {
607 props.Device_specific = proptools.BoolPtr(true)
608 } else if module.ProductSpecific() {
609 props.Product_specific = proptools.BoolPtr(true)
610 } else if module.SystemExtSpecific() {
611 props.System_ext_specific = proptools.BoolPtr(true)
612 }
613
614 mctx.CreateModule(sdkLibraryXmlFactory, &props)
Jiyong Parkc678ad32018-04-10 13:07:10 +0900615}
616
Paul Duffin50061512020-01-21 16:31:05 +0000617func PrebuiltJars(ctx android.BaseModuleContext, baseName string, s sdkSpec) android.Paths {
Jiyong Park6a927c42020-01-21 02:03:43 +0900618 var ver sdkVersion
619 var kind sdkKind
620 if s.usePrebuilt(ctx) {
621 ver = s.version
622 kind = s.kind
Jiyong Parkc678ad32018-04-10 13:07:10 +0900623 } else {
Jiyong Park6a927c42020-01-21 02:03:43 +0900624 // We don't have prebuilt SDK for the specific sdkVersion.
625 // Instead of breaking the build, fallback to use "system_current"
626 ver = sdkVersionCurrent
627 kind = sdkSystem
Sundong Ahn054b19a2018-10-19 13:46:09 +0900628 }
Jiyong Park6a927c42020-01-21 02:03:43 +0900629
630 dir := filepath.Join("prebuilts", "sdk", ver.String(), kind.String())
Paul Duffin50061512020-01-21 16:31:05 +0000631 jar := filepath.Join(dir, baseName+".jar")
Sundong Ahn054b19a2018-10-19 13:46:09 +0900632 jarPath := android.ExistentPathForSource(ctx, jar)
Sundong Ahnae418ac2019-02-28 15:01:28 +0900633 if !jarPath.Valid() {
Colin Cross07c88562020-01-07 09:34:44 -0800634 if ctx.Config().AllowMissingDependencies() {
635 return android.Paths{android.PathForSource(ctx, jar)}
636 } else {
Jiyong Park6a927c42020-01-21 02:03:43 +0900637 ctx.PropertyErrorf("sdk_library", "invalid sdk version %q, %q does not exist", s.raw, jar)
Colin Cross07c88562020-01-07 09:34:44 -0800638 }
Sundong Ahnae418ac2019-02-28 15:01:28 +0900639 return nil
640 }
Sundong Ahn054b19a2018-10-19 13:46:09 +0900641 return android.Paths{jarPath.Path()}
642}
643
Paul Duffind1b3a922020-01-22 11:57:20 +0000644func (module *SdkLibrary) sdkJars(
645 ctx android.BaseModuleContext,
646 sdkVersion sdkSpec,
647 headerJars bool) android.Paths {
648
Paul Duffin50061512020-01-21 16:31:05 +0000649 // If a specific numeric version has been requested then use prebuilt versions of the sdk.
650 if sdkVersion.version.isNumbered() {
651 return PrebuiltJars(ctx, module.BaseModuleName(), sdkVersion)
Sundong Ahn054b19a2018-10-19 13:46:09 +0900652 } else {
Paul Duffind1b3a922020-01-22 11:57:20 +0000653 if !sdkVersion.specified() {
654 if headerJars {
655 return module.Library.HeaderJars()
656 } else {
657 return module.Library.ImplementationJars()
658 }
659 }
Paul Duffin726d23c2020-01-22 16:30:37 +0000660 var apiScope *apiScope
Jiyong Park6a927c42020-01-21 02:03:43 +0900661 switch sdkVersion.kind {
662 case sdkSystem:
Paul Duffin726d23c2020-01-22 16:30:37 +0000663 apiScope = apiScopeSystem
664 case sdkTest:
665 apiScope = apiScopeTest
Jiyong Park6a927c42020-01-21 02:03:43 +0900666 case sdkPrivate:
Sundong Ahn054b19a2018-10-19 13:46:09 +0900667 return module.Library.HeaderJars()
Jiyong Park6a927c42020-01-21 02:03:43 +0900668 default:
Paul Duffin726d23c2020-01-22 16:30:37 +0000669 apiScope = apiScopePublic
Paul Duffind1b3a922020-01-22 11:57:20 +0000670 }
671
Paul Duffin726d23c2020-01-22 16:30:37 +0000672 paths := module.getScopePaths(apiScope)
Paul Duffind1b3a922020-01-22 11:57:20 +0000673 if headerJars {
674 return paths.stubsHeaderPath
675 } else {
676 return paths.stubsImplPath
Sundong Ahn054b19a2018-10-19 13:46:09 +0900677 }
Jiyong Parkc678ad32018-04-10 13:07:10 +0900678 }
679}
680
Sundong Ahn241cd372018-07-13 16:16:44 +0900681// to satisfy SdkLibraryDependency interface
Paul Duffind1b3a922020-01-22 11:57:20 +0000682func (module *SdkLibrary) SdkHeaderJars(ctx android.BaseModuleContext, sdkVersion sdkSpec) android.Paths {
683 return module.sdkJars(ctx, sdkVersion, true /*headerJars*/)
684}
685
686// to satisfy SdkLibraryDependency interface
Jiyong Park6a927c42020-01-21 02:03:43 +0900687func (module *SdkLibrary) SdkImplementationJars(ctx android.BaseModuleContext, sdkVersion sdkSpec) android.Paths {
Paul Duffind1b3a922020-01-22 11:57:20 +0000688 return module.sdkJars(ctx, sdkVersion, false /*headerJars*/)
Sundong Ahn241cd372018-07-13 16:16:44 +0900689}
690
Sundong Ahn80a87b32019-05-13 15:02:50 +0900691func (module *SdkLibrary) SetNoDist() {
692 module.sdkLibraryProperties.No_dist = proptools.BoolPtr(true)
693}
694
Colin Cross571cccf2019-02-04 11:22:08 -0800695var javaSdkLibrariesKey = android.NewOnceKey("javaSdkLibraries")
696
Jiyong Park82484c02018-04-23 21:41:26 +0900697func javaSdkLibraries(config android.Config) *[]string {
Colin Cross571cccf2019-02-04 11:22:08 -0800698 return config.Once(javaSdkLibrariesKey, func() interface{} {
Jiyong Park82484c02018-04-23 21:41:26 +0900699 return &[]string{}
700 }).(*[]string)
701}
702
Paul Duffin749f98f2019-12-30 17:23:46 +0000703func (module *SdkLibrary) getApiDir() string {
704 return proptools.StringDefault(module.sdkLibraryProperties.Api_dir, "api")
705}
706
Jiyong Parkc678ad32018-04-10 13:07:10 +0900707// For a java_sdk_library module, create internal modules for stubs, docs,
708// runtime libs and xml file. If requested, the stubs and docs are created twice
709// once for public API level and once for system API level
Colin Crossf8b860a2019-04-16 14:43:28 -0700710func (module *SdkLibrary) CreateInternalModules(mctx android.LoadHookContext) {
Inseob Kim6e93ac92019-03-21 17:43:49 +0900711 if len(module.Library.Module.properties.Srcs) == 0 {
Inseob Kimc0907f12019-02-08 21:00:45 +0900712 mctx.PropertyErrorf("srcs", "java_sdk_library must specify srcs")
Jooyung Han58f26ab2019-12-18 15:34:32 +0900713 return
Inseob Kimc0907f12019-02-08 21:00:45 +0900714 }
715
Paul Duffin37e0b772019-12-30 17:20:10 +0000716 // If this builds against standard libraries (i.e. is not part of the core libraries)
717 // then assume it provides both system and test apis. Otherwise, assume it does not and
718 // also assume it does not contribute to the dist build.
719 sdkDep := decodeSdkDep(mctx, sdkContext(&module.Library))
720 hasSystemAndTestApis := sdkDep.hasStandardLibs()
721 module.sdkLibraryProperties.Has_system_and_test_apis = hasSystemAndTestApis
722 module.sdkLibraryProperties.No_dist = proptools.BoolPtr(!hasSystemAndTestApis)
723
Inseob Kim8098faa2019-03-18 10:19:51 +0900724 missing_current_api := false
725
Paul Duffind1b3a922020-01-22 11:57:20 +0000726 activeScopes := module.getActiveApiScopes()
727
Paul Duffin749f98f2019-12-30 17:23:46 +0000728 apiDir := module.getApiDir()
Paul Duffind1b3a922020-01-22 11:57:20 +0000729 for _, scope := range activeScopes {
Inseob Kim8098faa2019-03-18 10:19:51 +0900730 for _, api := range []string{"current.txt", "removed.txt"} {
Paul Duffind1b3a922020-01-22 11:57:20 +0000731 path := path.Join(mctx.ModuleDir(), apiDir, scope.apiFilePrefix+api)
Inseob Kim8098faa2019-03-18 10:19:51 +0900732 p := android.ExistentPathForSource(mctx, path)
733 if !p.Valid() {
734 mctx.ModuleErrorf("Current api file %#v doesn't exist", path)
735 missing_current_api = true
736 }
737 }
738 }
739
740 if missing_current_api {
741 script := "build/soong/scripts/gen-java-current-api-files.sh"
742 p := android.ExistentPathForSource(mctx, script)
743
744 if !p.Valid() {
745 panic(fmt.Sprintf("script file %s doesn't exist", script))
746 }
747
748 mctx.ModuleErrorf("One or more current api files are missing. "+
749 "You can update them by:\n"+
Paul Duffin37e0b772019-12-30 17:20:10 +0000750 "%s %q %s && m update-api",
Paul Duffind1b3a922020-01-22 11:57:20 +0000751 script, filepath.Join(mctx.ModuleDir(), apiDir),
752 strings.Join(activeScopes.Strings(func(s *apiScope) string { return s.apiFilePrefix }), " "))
Inseob Kim8098faa2019-03-18 10:19:51 +0900753 return
754 }
755
Paul Duffind1b3a922020-01-22 11:57:20 +0000756 for _, scope := range activeScopes {
757 module.createStubsLibrary(mctx, scope)
758 module.createStubsSources(mctx, scope)
Inseob Kimc0907f12019-02-08 21:00:45 +0900759 }
760
Paul Duffin43db9be2019-12-30 17:35:49 +0000761 if !proptools.Bool(module.sdkLibraryProperties.Api_only) {
762 // for runtime
763 module.createXmlFile(mctx)
764
765 // record java_sdk_library modules so that they are exported to make
766 javaSdkLibraries := javaSdkLibraries(mctx.Config())
767 javaSdkLibrariesLock.Lock()
768 defer javaSdkLibrariesLock.Unlock()
769 *javaSdkLibraries = append(*javaSdkLibraries, module.BaseModuleName())
770 }
Inseob Kimc0907f12019-02-08 21:00:45 +0900771}
772
773func (module *SdkLibrary) InitSdkLibraryProperties() {
Sundong Ahn054b19a2018-10-19 13:46:09 +0900774 module.AddProperties(
775 &module.sdkLibraryProperties,
776 &module.Library.Module.properties,
777 &module.Library.Module.dexpreoptProperties,
778 &module.Library.Module.deviceProperties,
779 &module.Library.Module.protoProperties,
780 )
781
782 module.Library.Module.properties.Installable = proptools.BoolPtr(true)
783 module.Library.Module.deviceProperties.IsSDKLibrary = true
Inseob Kimc0907f12019-02-08 21:00:45 +0900784}
Sundong Ahn054b19a2018-10-19 13:46:09 +0900785
Jaewoong Jung4f158ee2019-07-11 10:05:35 -0700786// java_sdk_library is a special Java library that provides optional platform APIs to apps.
787// In practice, it can be viewed as a combination of several modules: 1) stubs library that clients
788// are linked against to, 2) droiddoc module that internally generates API stubs source files,
789// 3) the real runtime shared library that implements the APIs, and 4) XML file for adding
790// the runtime lib to the classpath at runtime if requested via <uses-library>.
Inseob Kimc0907f12019-02-08 21:00:45 +0900791func SdkLibraryFactory() android.Module {
792 module := &SdkLibrary{}
793 module.InitSdkLibraryProperties()
Jooyung Han58f26ab2019-12-18 15:34:32 +0900794 android.InitApexModule(module)
Sundong Ahn054b19a2018-10-19 13:46:09 +0900795 InitJavaModule(module, android.HostAndDeviceSupported)
Colin Crossf8b860a2019-04-16 14:43:28 -0700796 android.AddLoadHook(module, func(ctx android.LoadHookContext) { module.CreateInternalModules(ctx) })
Jiyong Parkc678ad32018-04-10 13:07:10 +0900797 return module
798}
Colin Cross79c7c262019-04-17 11:11:46 -0700799
800//
801// SDK library prebuilts
802//
803
Paul Duffin56d44902020-01-31 13:36:25 +0000804// Properties associated with each api scope.
805type sdkLibraryScopeProperties struct {
Colin Cross79c7c262019-04-17 11:11:46 -0700806 Jars []string `android:"path"`
807
808 Sdk_version *string
809
Colin Cross79c7c262019-04-17 11:11:46 -0700810 // List of shared java libs that this module has dependencies to
811 Libs []string
Paul Duffin3d1248c2020-04-09 00:10:17 +0100812
813 // The stub sources.
814 Stub_srcs []string `android:"path"`
Paul Duffin1fd005d2020-04-09 01:08:11 +0100815
816 // The current.txt
817 Current_api string `android:"path"`
818
819 // The removed.txt
820 Removed_api string `android:"path"`
Colin Cross79c7c262019-04-17 11:11:46 -0700821}
822
Paul Duffin56d44902020-01-31 13:36:25 +0000823type sdkLibraryImportProperties struct {
Paul Duffinfcfd7912020-01-31 17:54:30 +0000824 // List of shared java libs, common to all scopes, that this module has
825 // dependencies to
826 Libs []string
Paul Duffin56d44902020-01-31 13:36:25 +0000827}
828
Colin Cross79c7c262019-04-17 11:11:46 -0700829type sdkLibraryImport struct {
830 android.ModuleBase
831 android.DefaultableModuleBase
832 prebuilt android.Prebuilt
Paul Duffindd46f712020-02-10 13:37:10 +0000833 android.ApexModuleBase
834 android.SdkBase
Colin Cross79c7c262019-04-17 11:11:46 -0700835
836 properties sdkLibraryImportProperties
837
Paul Duffin46a26a82020-04-07 19:27:04 +0100838 // Map from api scope to the scope specific property structure.
839 scopeProperties map[*apiScope]*sdkLibraryScopeProperties
840
Paul Duffin56d44902020-01-31 13:36:25 +0000841 commonToSdkLibraryAndImport
Colin Cross79c7c262019-04-17 11:11:46 -0700842}
843
844var _ SdkLibraryDependency = (*sdkLibraryImport)(nil)
845
Paul Duffin46a26a82020-04-07 19:27:04 +0100846// The type of a structure that contains a field of type sdkLibraryScopeProperties
847// for each apiscope in allApiScopes, e.g. something like:
848// struct {
849// Public sdkLibraryScopeProperties
850// System sdkLibraryScopeProperties
851// ...
852// }
853var allScopeStructType = createAllScopePropertiesStructType()
854
855// Dynamically create a structure type for each apiscope in allApiScopes.
856func createAllScopePropertiesStructType() reflect.Type {
857 var fields []reflect.StructField
858 for _, apiScope := range allApiScopes {
859 field := reflect.StructField{
860 Name: apiScope.fieldName,
861 Type: reflect.TypeOf(sdkLibraryScopeProperties{}),
862 }
863 fields = append(fields, field)
864 }
865
866 return reflect.StructOf(fields)
867}
868
869// Create an instance of the scope specific structure type and return a map
870// from apiscope to a pointer to each scope specific field.
871func createPropertiesInstance() (interface{}, map[*apiScope]*sdkLibraryScopeProperties) {
872 allScopePropertiesPtr := reflect.New(allScopeStructType)
873 allScopePropertiesStruct := allScopePropertiesPtr.Elem()
874 scopeProperties := make(map[*apiScope]*sdkLibraryScopeProperties)
875
876 for _, apiScope := range allApiScopes {
877 field := allScopePropertiesStruct.FieldByName(apiScope.fieldName)
878 scopeProperties[apiScope] = field.Addr().Interface().(*sdkLibraryScopeProperties)
879 }
880
881 return allScopePropertiesPtr.Interface(), scopeProperties
882}
883
Jaewoong Jung4f158ee2019-07-11 10:05:35 -0700884// java_sdk_library_import imports a prebuilt java_sdk_library.
Colin Cross79c7c262019-04-17 11:11:46 -0700885func sdkLibraryImportFactory() android.Module {
886 module := &sdkLibraryImport{}
887
Paul Duffin46a26a82020-04-07 19:27:04 +0100888 allScopeProperties, scopeToProperties := createPropertiesInstance()
889 module.scopeProperties = scopeToProperties
890 module.AddProperties(&module.properties, allScopeProperties)
Colin Cross79c7c262019-04-17 11:11:46 -0700891
Paul Duffin0bdcb272020-02-06 15:24:57 +0000892 android.InitPrebuiltModule(module, &[]string{""})
Paul Duffindd46f712020-02-10 13:37:10 +0000893 android.InitApexModule(module)
894 android.InitSdkAwareModule(module)
Colin Cross79c7c262019-04-17 11:11:46 -0700895 InitJavaModule(module, android.HostAndDeviceSupported)
896
897 android.AddLoadHook(module, func(mctx android.LoadHookContext) { module.createInternalModules(mctx) })
898 return module
899}
900
901func (module *sdkLibraryImport) Prebuilt() *android.Prebuilt {
902 return &module.prebuilt
903}
904
905func (module *sdkLibraryImport) Name() string {
906 return module.prebuilt.Name(module.ModuleBase.Name())
907}
908
909func (module *sdkLibraryImport) createInternalModules(mctx android.LoadHookContext) {
Colin Cross79c7c262019-04-17 11:11:46 -0700910
Paul Duffin50061512020-01-21 16:31:05 +0000911 // If the build is configured to use prebuilts then force this to be preferred.
912 if mctx.Config().UnbundledBuildUsePrebuiltSdks() {
913 module.prebuilt.ForcePrefer()
914 }
915
Paul Duffin46a26a82020-04-07 19:27:04 +0100916 for apiScope, scopeProperties := range module.scopeProperties {
Paul Duffin56d44902020-01-31 13:36:25 +0000917 if len(scopeProperties.Jars) == 0 {
918 continue
919 }
920
Paul Duffinbbb546b2020-04-09 00:07:11 +0100921 module.createJavaImportForStubs(mctx, apiScope, scopeProperties)
Paul Duffin3d1248c2020-04-09 00:10:17 +0100922
923 module.createPrebuiltStubsSources(mctx, apiScope, scopeProperties)
Paul Duffin56d44902020-01-31 13:36:25 +0000924 }
Colin Cross79c7c262019-04-17 11:11:46 -0700925
926 javaSdkLibraries := javaSdkLibraries(mctx.Config())
927 javaSdkLibrariesLock.Lock()
928 defer javaSdkLibrariesLock.Unlock()
929 *javaSdkLibraries = append(*javaSdkLibraries, module.BaseModuleName())
930}
931
Paul Duffinbbb546b2020-04-09 00:07:11 +0100932func (module *sdkLibraryImport) createJavaImportForStubs(mctx android.LoadHookContext, apiScope *apiScope, scopeProperties *sdkLibraryScopeProperties) {
933 // Creates a java import for the jar with ".stubs" suffix
934 props := struct {
935 Name *string
936 Soc_specific *bool
937 Device_specific *bool
938 Product_specific *bool
939 System_ext_specific *bool
940 Sdk_version *string
941 Libs []string
942 Jars []string
943 Prefer *bool
944 }{}
945 props.Name = proptools.StringPtr(apiScope.stubsModuleName(module.BaseModuleName()))
946 props.Sdk_version = scopeProperties.Sdk_version
947 // Prepend any of the libs from the legacy public properties to the libs for each of the
948 // scopes to avoid having to duplicate them in each scope.
949 props.Libs = append(module.properties.Libs, scopeProperties.Libs...)
950 props.Jars = scopeProperties.Jars
951 if module.SocSpecific() {
952 props.Soc_specific = proptools.BoolPtr(true)
953 } else if module.DeviceSpecific() {
954 props.Device_specific = proptools.BoolPtr(true)
955 } else if module.ProductSpecific() {
956 props.Product_specific = proptools.BoolPtr(true)
957 } else if module.SystemExtSpecific() {
958 props.System_ext_specific = proptools.BoolPtr(true)
959 }
960 // If the build should use prebuilt sdks then set prefer to true on the stubs library.
961 // That will cause the prebuilt version of the stubs to override the source version.
962 if mctx.Config().UnbundledBuildUsePrebuiltSdks() {
963 props.Prefer = proptools.BoolPtr(true)
964 }
965 mctx.CreateModule(ImportFactory, &props)
966}
967
Paul Duffin3d1248c2020-04-09 00:10:17 +0100968func (module *sdkLibraryImport) createPrebuiltStubsSources(mctx android.LoadHookContext, apiScope *apiScope, scopeProperties *sdkLibraryScopeProperties) {
969 props := struct {
970 Name *string
971 Srcs []string
972 }{}
973 props.Name = proptools.StringPtr(apiScope.docsModuleName(module.BaseModuleName()))
974 props.Srcs = scopeProperties.Stub_srcs
975 mctx.CreateModule(PrebuiltStubsSourcesFactory, &props)
976}
977
Colin Cross79c7c262019-04-17 11:11:46 -0700978func (module *sdkLibraryImport) DepsMutator(ctx android.BottomUpMutatorContext) {
Paul Duffin46a26a82020-04-07 19:27:04 +0100979 for apiScope, scopeProperties := range module.scopeProperties {
Paul Duffin56d44902020-01-31 13:36:25 +0000980 if len(scopeProperties.Jars) == 0 {
981 continue
982 }
983
984 // Add dependencies to the prebuilt stubs library
985 ctx.AddVariationDependencies(nil, apiScope.stubsTag, apiScope.stubsModuleName(module.BaseModuleName()))
986 }
Colin Cross79c7c262019-04-17 11:11:46 -0700987}
988
989func (module *sdkLibraryImport) GenerateAndroidBuildActions(ctx android.ModuleContext) {
990 // Record the paths to the prebuilt stubs library.
991 ctx.VisitDirectDeps(func(to android.Module) {
992 tag := ctx.OtherModuleDependencyTag(to)
993
Paul Duffin56d44902020-01-31 13:36:25 +0000994 if lib, ok := to.(Dependency); ok {
995 if scopeTag, ok := tag.(scopeDependencyTag); ok {
996 apiScope := scopeTag.apiScope
997 scopePaths := module.getScopePaths(apiScope)
998 scopePaths.stubsHeaderPath = lib.HeaderJars()
999 }
Colin Cross79c7c262019-04-17 11:11:46 -07001000 }
1001 })
1002}
1003
Paul Duffin56d44902020-01-31 13:36:25 +00001004func (module *sdkLibraryImport) sdkJars(
1005 ctx android.BaseModuleContext,
1006 sdkVersion sdkSpec) android.Paths {
1007
Paul Duffin50061512020-01-21 16:31:05 +00001008 // If a specific numeric version has been requested then use prebuilt versions of the sdk.
1009 if sdkVersion.version.isNumbered() {
1010 return PrebuiltJars(ctx, module.BaseModuleName(), sdkVersion)
1011 }
1012
Paul Duffin56d44902020-01-31 13:36:25 +00001013 var apiScope *apiScope
1014 switch sdkVersion.kind {
1015 case sdkSystem:
1016 apiScope = apiScopeSystem
1017 case sdkTest:
1018 apiScope = apiScopeTest
1019 default:
1020 apiScope = apiScopePublic
1021 }
1022
1023 paths := module.getScopePaths(apiScope)
1024 return paths.stubsHeaderPath
1025}
1026
Colin Cross79c7c262019-04-17 11:11:46 -07001027// to satisfy SdkLibraryDependency interface
Jiyong Park6a927c42020-01-21 02:03:43 +09001028func (module *sdkLibraryImport) SdkHeaderJars(ctx android.BaseModuleContext, sdkVersion sdkSpec) android.Paths {
Colin Cross79c7c262019-04-17 11:11:46 -07001029 // This module is just a wrapper for the prebuilt stubs.
Paul Duffin56d44902020-01-31 13:36:25 +00001030 return module.sdkJars(ctx, sdkVersion)
Colin Cross79c7c262019-04-17 11:11:46 -07001031}
1032
1033// to satisfy SdkLibraryDependency interface
Jiyong Park6a927c42020-01-21 02:03:43 +09001034func (module *sdkLibraryImport) SdkImplementationJars(ctx android.BaseModuleContext, sdkVersion sdkSpec) android.Paths {
Colin Cross79c7c262019-04-17 11:11:46 -07001035 // This module is just a wrapper for the stubs.
Paul Duffin56d44902020-01-31 13:36:25 +00001036 return module.sdkJars(ctx, sdkVersion)
Colin Cross79c7c262019-04-17 11:11:46 -07001037}
Jiyong Parke3833882020-02-17 17:28:10 +09001038
1039//
1040// java_sdk_library_xml
1041//
1042type sdkLibraryXml struct {
1043 android.ModuleBase
1044 android.DefaultableModuleBase
1045 android.ApexModuleBase
1046
1047 properties sdkLibraryXmlProperties
1048
1049 outputFilePath android.OutputPath
1050 installDirPath android.InstallPath
1051}
1052
1053type sdkLibraryXmlProperties struct {
1054 // canonical name of the lib
1055 Lib_name *string
1056}
1057
1058// java_sdk_library_xml builds the permission xml file for a java_sdk_library.
1059// Not to be used directly by users. java_sdk_library internally uses this.
1060func sdkLibraryXmlFactory() android.Module {
1061 module := &sdkLibraryXml{}
1062
1063 module.AddProperties(&module.properties)
1064
1065 android.InitApexModule(module)
1066 android.InitAndroidArchModule(module, android.DeviceSupported, android.MultilibCommon)
1067
1068 return module
1069}
1070
1071// from android.PrebuiltEtcModule
1072func (module *sdkLibraryXml) SubDir() string {
1073 return "permissions"
1074}
1075
1076// from android.PrebuiltEtcModule
1077func (module *sdkLibraryXml) OutputFile() android.OutputPath {
1078 return module.outputFilePath
1079}
1080
1081// from android.ApexModule
1082func (module *sdkLibraryXml) AvailableFor(what string) bool {
1083 return true
1084}
1085
1086func (module *sdkLibraryXml) DepsMutator(ctx android.BottomUpMutatorContext) {
1087 // do nothing
1088}
1089
1090// File path to the runtime implementation library
1091func (module *sdkLibraryXml) implPath() string {
1092 implName := proptools.String(module.properties.Lib_name)
1093 if apexName := module.ApexName(); apexName != "" {
1094 // TODO(b/146468504): ApexName() is only a soong module name, not apex name.
1095 // In most cases, this works fine. But when apex_name is set or override_apex is used
1096 // this can be wrong.
1097 return fmt.Sprintf("/apex/%s/javalib/%s.jar", apexName, implName)
1098 }
1099 partition := "system"
1100 if module.SocSpecific() {
1101 partition = "vendor"
1102 } else if module.DeviceSpecific() {
1103 partition = "odm"
1104 } else if module.ProductSpecific() {
1105 partition = "product"
1106 } else if module.SystemExtSpecific() {
1107 partition = "system_ext"
1108 }
1109 return "/" + partition + "/framework/" + implName + ".jar"
1110}
1111
1112func (module *sdkLibraryXml) GenerateAndroidBuildActions(ctx android.ModuleContext) {
1113 libName := proptools.String(module.properties.Lib_name)
1114 xmlContent := fmt.Sprintf(permissionsTemplate, libName, module.implPath())
1115
1116 module.outputFilePath = android.PathForModuleOut(ctx, libName+".xml").OutputPath
1117 rule := android.NewRuleBuilder()
1118 rule.Command().
1119 Text("/bin/bash -c \"echo -e '" + xmlContent + "'\" > ").
1120 Output(module.outputFilePath)
1121
1122 rule.Build(pctx, ctx, "java_sdk_xml", "Permission XML")
1123
1124 module.installDirPath = android.PathForModuleInstall(ctx, "etc", module.SubDir())
1125}
1126
1127func (module *sdkLibraryXml) AndroidMkEntries() []android.AndroidMkEntries {
1128 if !module.IsForPlatform() {
1129 return []android.AndroidMkEntries{android.AndroidMkEntries{
1130 Disabled: true,
1131 }}
1132 }
1133
1134 return []android.AndroidMkEntries{android.AndroidMkEntries{
1135 Class: "ETC",
1136 OutputFile: android.OptionalPathForPath(module.outputFilePath),
1137 ExtraEntries: []android.AndroidMkExtraEntriesFunc{
1138 func(entries *android.AndroidMkEntries) {
1139 entries.SetString("LOCAL_MODULE_TAGS", "optional")
1140 entries.SetString("LOCAL_MODULE_PATH", module.installDirPath.ToMakePath().String())
1141 entries.SetString("LOCAL_INSTALLED_MODULE_STEM", module.outputFilePath.Base())
1142 },
1143 },
1144 }}
1145}
Paul Duffindd46f712020-02-10 13:37:10 +00001146
1147type sdkLibrarySdkMemberType struct {
1148 android.SdkMemberTypeBase
1149}
1150
1151func (s *sdkLibrarySdkMemberType) AddDependencies(mctx android.BottomUpMutatorContext, dependencyTag blueprint.DependencyTag, names []string) {
1152 mctx.AddVariationDependencies(nil, dependencyTag, names...)
1153}
1154
1155func (s *sdkLibrarySdkMemberType) IsInstance(module android.Module) bool {
1156 _, ok := module.(*SdkLibrary)
1157 return ok
1158}
1159
1160func (s *sdkLibrarySdkMemberType) AddPrebuiltModule(ctx android.SdkMemberContext, member android.SdkMember) android.BpModule {
1161 return ctx.SnapshotBuilder().AddPrebuiltModule(member, "java_sdk_library_import")
1162}
1163
1164func (s *sdkLibrarySdkMemberType) CreateVariantPropertiesStruct() android.SdkMemberProperties {
1165 return &sdkLibrarySdkMemberProperties{}
1166}
1167
1168type sdkLibrarySdkMemberProperties struct {
1169 android.SdkMemberPropertiesBase
1170
1171 // Scope to per scope properties.
1172 Scopes map[*apiScope]scopeProperties
1173
1174 // Additional libraries that the exported stubs libraries depend upon.
1175 Libs []string
Paul Duffin3d1248c2020-04-09 00:10:17 +01001176
1177 // The Java stubs source files.
1178 Stub_srcs []string
Paul Duffindd46f712020-02-10 13:37:10 +00001179}
1180
1181type scopeProperties struct {
Paul Duffin1fd005d2020-04-09 01:08:11 +01001182 Jars android.Paths
1183 StubsSrcJar android.Path
1184 CurrentApiFile android.Path
1185 RemovedApiFile android.Path
1186 SdkVersion string
Paul Duffindd46f712020-02-10 13:37:10 +00001187}
1188
1189func (s *sdkLibrarySdkMemberProperties) PopulateFromVariant(ctx android.SdkMemberContext, variant android.Module) {
1190 sdk := variant.(*SdkLibrary)
1191
1192 s.Scopes = make(map[*apiScope]scopeProperties)
1193 for _, apiScope := range allApiScopes {
1194 paths := sdk.getScopePaths(apiScope)
1195 jars := paths.stubsImplPath
1196 if len(jars) > 0 {
1197 properties := scopeProperties{}
1198 properties.Jars = jars
1199 properties.SdkVersion = apiScope.sdkVersion
Paul Duffin3d1248c2020-04-09 00:10:17 +01001200 properties.StubsSrcJar = paths.stubsSrcJar
Paul Duffin1fd005d2020-04-09 01:08:11 +01001201 properties.CurrentApiFile = paths.currentApiFilePath
1202 properties.RemovedApiFile = paths.removedApiFilePath
Paul Duffindd46f712020-02-10 13:37:10 +00001203 s.Scopes[apiScope] = properties
1204 }
1205 }
1206
1207 s.Libs = sdk.properties.Libs
1208}
1209
1210func (s *sdkLibrarySdkMemberProperties) AddToPropertySet(ctx android.SdkMemberContext, propertySet android.BpPropertySet) {
1211 for _, apiScope := range allApiScopes {
1212 if properties, ok := s.Scopes[apiScope]; ok {
1213 scopeSet := propertySet.AddPropertySet(apiScope.name)
1214
Paul Duffin3d1248c2020-04-09 00:10:17 +01001215 scopeDir := filepath.Join("sdk_library", s.OsPrefix(), apiScope.name)
1216
Paul Duffindd46f712020-02-10 13:37:10 +00001217 var jars []string
1218 for _, p := range properties.Jars {
Paul Duffin3d1248c2020-04-09 00:10:17 +01001219 dest := filepath.Join(scopeDir, ctx.Name()+"-stubs.jar")
Paul Duffindd46f712020-02-10 13:37:10 +00001220 ctx.SnapshotBuilder().CopyToSnapshot(p, dest)
1221 jars = append(jars, dest)
1222 }
1223 scopeSet.AddProperty("jars", jars)
1224
Paul Duffin3d1248c2020-04-09 00:10:17 +01001225 // Merge the stubs source jar into the snapshot zip so that when it is unpacked
1226 // the source files are also unpacked.
1227 snapshotRelativeDir := filepath.Join(scopeDir, ctx.Name()+"_stub_sources")
1228 ctx.SnapshotBuilder().UnzipToSnapshot(properties.StubsSrcJar, snapshotRelativeDir)
1229 scopeSet.AddProperty("stub_srcs", []string{snapshotRelativeDir})
1230
Paul Duffin1fd005d2020-04-09 01:08:11 +01001231 if properties.CurrentApiFile != nil {
1232 currentApiSnapshotPath := filepath.Join(scopeDir, ctx.Name()+".txt")
1233 ctx.SnapshotBuilder().CopyToSnapshot(properties.CurrentApiFile, currentApiSnapshotPath)
1234 scopeSet.AddProperty("current_api", currentApiSnapshotPath)
1235 }
1236
1237 if properties.RemovedApiFile != nil {
1238 removedApiSnapshotPath := filepath.Join(scopeDir, ctx.Name()+"-removed.txt")
1239 ctx.SnapshotBuilder().CopyToSnapshot(properties.CurrentApiFile, removedApiSnapshotPath)
1240 scopeSet.AddProperty("removed_api", removedApiSnapshotPath)
1241 }
1242
Paul Duffindd46f712020-02-10 13:37:10 +00001243 if properties.SdkVersion != "" {
1244 scopeSet.AddProperty("sdk_version", properties.SdkVersion)
1245 }
1246 }
1247 }
1248
1249 if len(s.Libs) > 0 {
1250 propertySet.AddPropertyWithTag("libs", s.Libs, ctx.SnapshotBuilder().SdkMemberReferencePropertyTag(false))
1251 }
1252}