blob: fa267953ed882ed0f8e60e758b5fa8842d4ecddb [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 Duffin6a2bd112020-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 Duffin6a2bd112020-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 Duffin6a2bd112020-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 Duffin3c7c3472020-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 Duffin6a2bd112020-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 Hanssone366fff2020-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 Hanssone366fff2020-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 Duffin61871622020-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 {
234 stubsHeaderPath android.Paths
235 stubsImplPath android.Paths
236 apiFilePath android.Path
237}
238
Paul Duffin56d44902020-01-31 13:36:25 +0000239// Common code between sdk library and sdk library import
240type commonToSdkLibraryAndImport struct {
241 scopePaths map[*apiScope]*scopePaths
242}
243
244func (c *commonToSdkLibraryAndImport) getScopePaths(scope *apiScope) *scopePaths {
245 if c.scopePaths == nil {
246 c.scopePaths = make(map[*apiScope]*scopePaths)
247 }
248 paths := c.scopePaths[scope]
249 if paths == nil {
250 paths = &scopePaths{}
251 c.scopePaths[scope] = paths
252 }
253
254 return paths
255}
256
Inseob Kimc0907f12019-02-08 21:00:45 +0900257type SdkLibrary struct {
Sundong Ahn054b19a2018-10-19 13:46:09 +0900258 Library
Jiyong Parkc678ad32018-04-10 13:07:10 +0900259
Sundong Ahn054b19a2018-10-19 13:46:09 +0900260 sdkLibraryProperties sdkLibraryProperties
Jiyong Parkc678ad32018-04-10 13:07:10 +0900261
Paul Duffin56d44902020-01-31 13:36:25 +0000262 commonToSdkLibraryAndImport
Jiyong Parkc678ad32018-04-10 13:07:10 +0900263}
264
Inseob Kimc0907f12019-02-08 21:00:45 +0900265var _ Dependency = (*SdkLibrary)(nil)
266var _ SdkLibraryDependency = (*SdkLibrary)(nil)
Colin Cross897d2ed2019-02-11 14:03:51 -0800267
Paul Duffind1b3a922020-01-22 11:57:20 +0000268func (module *SdkLibrary) getActiveApiScopes() apiScopes {
269 if module.sdkLibraryProperties.Has_system_and_test_apis {
270 return allApiScopes
271 } else {
272 return apiScopes{apiScopePublic}
273 }
274}
275
Paul Duffine74ac732020-02-06 13:51:46 +0000276var xmlPermissionsFileTag = dependencyTag{name: "xml-permissions-file"}
277
Jiyong Parke3833882020-02-17 17:28:10 +0900278func IsXmlPermissionsFileDepTag(depTag blueprint.DependencyTag) bool {
279 if dt, ok := depTag.(dependencyTag); ok {
280 return dt == xmlPermissionsFileTag
281 }
282 return false
283}
284
Inseob Kimc0907f12019-02-08 21:00:45 +0900285func (module *SdkLibrary) DepsMutator(ctx android.BottomUpMutatorContext) {
Paul Duffind1b3a922020-01-22 11:57:20 +0000286 for _, apiScope := range module.getActiveApiScopes() {
287 // Add dependencies to the stubs library
Paul Duffin50061512020-01-21 16:31:05 +0000288 ctx.AddVariationDependencies(nil, apiScope.stubsTag, module.stubsName(apiScope))
Paul Duffind1b3a922020-01-22 11:57:20 +0000289
Paul Duffin50061512020-01-21 16:31:05 +0000290 // And the api file
Paul Duffind1b3a922020-01-22 11:57:20 +0000291 ctx.AddVariationDependencies(nil, apiScope.apiFileTag, module.docsName(apiScope))
Sundong Ahn054b19a2018-10-19 13:46:09 +0900292 }
293
Paul Duffine74ac732020-02-06 13:51:46 +0000294 if !proptools.Bool(module.sdkLibraryProperties.Api_only) {
295 // Add dependency to the rule for generating the xml permissions file
Jiyong Parke3833882020-02-17 17:28:10 +0900296 ctx.AddDependency(module, xmlPermissionsFileTag, module.xmlFileName())
Paul Duffine74ac732020-02-06 13:51:46 +0000297 }
298
Sundong Ahn054b19a2018-10-19 13:46:09 +0900299 module.Library.deps(ctx)
Jiyong Parkc678ad32018-04-10 13:07:10 +0900300}
301
Inseob Kimc0907f12019-02-08 21:00:45 +0900302func (module *SdkLibrary) GenerateAndroidBuildActions(ctx android.ModuleContext) {
Paul Duffin43db9be2019-12-30 17:35:49 +0000303 // Don't build an implementation library if this is api only.
304 if !proptools.Bool(module.sdkLibraryProperties.Api_only) {
305 module.Library.GenerateAndroidBuildActions(ctx)
306 }
Sundong Ahn054b19a2018-10-19 13:46:09 +0900307
Sundong Ahn57368eb2018-07-06 11:20:23 +0900308 // Record the paths to the header jars of the library (stubs and impl).
Paul Duffind1b3a922020-01-22 11:57:20 +0000309 // When this java_sdk_library is depended upon from others via "libs" property,
Jiyong Parkc678ad32018-04-10 13:07:10 +0900310 // the recorded paths will be returned depending on the link type of the caller.
311 ctx.VisitDirectDeps(func(to android.Module) {
312 otherName := ctx.OtherModuleName(to)
313 tag := ctx.OtherModuleDependencyTag(to)
314
Sundong Ahn57368eb2018-07-06 11:20:23 +0900315 if lib, ok := to.(Dependency); ok {
Paul Duffind1b3a922020-01-22 11:57:20 +0000316 if scopeTag, ok := tag.(scopeDependencyTag); ok {
317 apiScope := scopeTag.apiScope
318 scopePaths := module.getScopePaths(apiScope)
319 scopePaths.stubsHeaderPath = lib.HeaderJars()
320 scopePaths.stubsImplPath = lib.ImplementationJars()
Jiyong Parkc678ad32018-04-10 13:07:10 +0900321 }
322 }
Sundong Ahn20e998b2018-07-24 11:19:26 +0900323 if doc, ok := to.(ApiFilePath); ok {
Paul Duffind1b3a922020-01-22 11:57:20 +0000324 if scopeTag, ok := tag.(scopeDependencyTag); ok {
325 apiScope := scopeTag.apiScope
326 scopePaths := module.getScopePaths(apiScope)
327 scopePaths.apiFilePath = doc.ApiFilePath()
328 } else {
Sundong Ahn20e998b2018-07-24 11:19:26 +0900329 ctx.ModuleErrorf("depends on module %q of unknown tag %q", otherName, tag)
330 }
331 }
Jiyong Parkc678ad32018-04-10 13:07:10 +0900332 })
333}
334
Jiyong Park0b0e1b92019-12-03 13:24:29 +0900335func (module *SdkLibrary) AndroidMkEntries() []android.AndroidMkEntries {
Paul Duffin43db9be2019-12-30 17:35:49 +0000336 if proptools.Bool(module.sdkLibraryProperties.Api_only) {
337 return nil
338 }
Jiyong Park0b0e1b92019-12-03 13:24:29 +0900339 entriesList := module.Library.AndroidMkEntries()
340 entries := &entriesList[0]
Jaewoong Jungb0c127c2019-08-29 14:56:03 -0700341 entries.Required = append(entries.Required, module.xmlFileName())
Jiyong Park0b0e1b92019-12-03 13:24:29 +0900342 return entriesList
Jiyong Park82484c02018-04-23 21:41:26 +0900343}
344
Jiyong Parkc678ad32018-04-10 13:07:10 +0900345// Module name of the stubs library
Paul Duffind1b3a922020-01-22 11:57:20 +0000346func (module *SdkLibrary) stubsName(apiScope *apiScope) string {
347 return apiScope.stubsModuleName(module.BaseModuleName())
Jiyong Parkc678ad32018-04-10 13:07:10 +0900348}
349
350// Module name of the docs
Paul Duffind1b3a922020-01-22 11:57:20 +0000351func (module *SdkLibrary) docsName(apiScope *apiScope) string {
352 return apiScope.docsModuleName(module.BaseModuleName())
Jiyong Parkc678ad32018-04-10 13:07:10 +0900353}
354
355// Module name of the runtime implementation library
Inseob Kimc0907f12019-02-08 21:00:45 +0900356func (module *SdkLibrary) implName() string {
Sundong Ahn054b19a2018-10-19 13:46:09 +0900357 return module.BaseModuleName()
Jiyong Parkc678ad32018-04-10 13:07:10 +0900358}
359
Jiyong Parkc678ad32018-04-10 13:07:10 +0900360// Module name of the XML file for the lib
Inseob Kimc0907f12019-02-08 21:00:45 +0900361func (module *SdkLibrary) xmlFileName() string {
Jiyong Parkc678ad32018-04-10 13:07:10 +0900362 return module.BaseModuleName() + sdkXmlFileSuffix
363}
364
Anton Hansson6bb88102020-03-27 19:43:19 +0000365// The dist path of the stub artifacts
366func (module *SdkLibrary) apiDistPath(apiScope *apiScope) string {
367 if module.ModuleBase.Owner() != "" {
368 return path.Join("apistubs", module.ModuleBase.Owner(), apiScope.name)
369 } else if Bool(module.sdkLibraryProperties.Core_lib) {
370 return path.Join("apistubs", "core", apiScope.name)
371 } else {
372 return path.Join("apistubs", "android", apiScope.name)
373 }
374}
375
Paul Duffin12ceb462019-12-24 20:31:31 +0000376// Get the sdk version for use when compiling the stubs library.
Paul Duffind1b3a922020-01-22 11:57:20 +0000377func (module *SdkLibrary) sdkVersionForStubsLibrary(mctx android.LoadHookContext, apiScope *apiScope) string {
Paul Duffin12ceb462019-12-24 20:31:31 +0000378 sdkDep := decodeSdkDep(mctx, sdkContext(&module.Library))
379 if sdkDep.hasStandardLibs() {
380 // If building against a standard sdk then use the sdk version appropriate for the scope.
Paul Duffind1b3a922020-01-22 11:57:20 +0000381 return apiScope.sdkVersion
Paul Duffin12ceb462019-12-24 20:31:31 +0000382 } else {
383 // Otherwise, use no system module.
384 return "none"
385 }
386}
387
Paul Duffind1b3a922020-01-22 11:57:20 +0000388func (module *SdkLibrary) latestApiFilegroupName(apiScope *apiScope) string {
389 return ":" + module.BaseModuleName() + ".api." + apiScope.name + ".latest"
Jiyong Park58c518b2018-05-12 22:29:12 +0900390}
Jiyong Parkc678ad32018-04-10 13:07:10 +0900391
Paul Duffind1b3a922020-01-22 11:57:20 +0000392func (module *SdkLibrary) latestRemovedApiFilegroupName(apiScope *apiScope) string {
393 return ":" + module.BaseModuleName() + "-removed.api." + apiScope.name + ".latest"
Jiyong Parkc678ad32018-04-10 13:07:10 +0900394}
395
396// Creates a static java library that has API stubs
Paul Duffind1b3a922020-01-22 11:57:20 +0000397func (module *SdkLibrary) createStubsLibrary(mctx android.LoadHookContext, apiScope *apiScope) {
Jiyong Parkc678ad32018-04-10 13:07:10 +0900398 props := struct {
Sundong Ahn0d7dff42019-12-04 12:53:44 +0900399 Name *string
400 Srcs []string
Paul Duffin367ab912019-12-23 19:40:36 +0000401 Installable *bool
Sundong Ahn0d7dff42019-12-04 12:53:44 +0900402 Sdk_version *string
Paul Duffin12ceb462019-12-24 20:31:31 +0000403 System_modules *string
Paul Duffinab8da5d2020-02-07 16:12:04 +0000404 Patch_module *string
Sundong Ahn0d7dff42019-12-04 12:53:44 +0900405 Libs []string
406 Soc_specific *bool
407 Device_specific *bool
408 Product_specific *bool
409 System_ext_specific *bool
410 Compile_dex *bool
Sundong Ahn0d7dff42019-12-04 12:53:44 +0900411 Java_version *string
412 Product_variables struct {
Jiyong Park82484c02018-04-23 21:41:26 +0900413 Pdk struct {
414 Enabled *bool
415 }
Jiyong Parkc678ad32018-04-10 13:07:10 +0900416 }
Sundong Ahn054b19a2018-10-19 13:46:09 +0900417 Openjdk9 struct {
418 Srcs []string
419 Javacflags []string
420 }
Anton Hansson6bb88102020-03-27 19:43:19 +0000421 Dist struct {
422 Targets []string
423 Dest *string
424 Dir *string
425 Tag *string
426 }
Jiyong Parkc678ad32018-04-10 13:07:10 +0900427 }{}
428
Jiyong Parkdf130542018-04-27 16:29:21 +0900429 props.Name = proptools.StringPtr(module.stubsName(apiScope))
Jiyong Parkc678ad32018-04-10 13:07:10 +0900430 // sources are generated from the droiddoc
Jiyong Parkdf130542018-04-27 16:29:21 +0900431 props.Srcs = []string{":" + module.docsName(apiScope)}
Paul Duffin12ceb462019-12-24 20:31:31 +0000432 sdkVersion := module.sdkVersionForStubsLibrary(mctx, apiScope)
Paul Duffin52d398a2019-06-11 12:31:14 +0100433 props.Sdk_version = proptools.StringPtr(sdkVersion)
Paul Duffin12ceb462019-12-24 20:31:31 +0000434 props.System_modules = module.Library.Module.deviceProperties.System_modules
Paul Duffinab8da5d2020-02-07 16:12:04 +0000435 props.Patch_module = module.Library.Module.properties.Patch_module
Paul Duffin367ab912019-12-23 19:40:36 +0000436 props.Installable = proptools.BoolPtr(false)
Sundong Ahn054b19a2018-10-19 13:46:09 +0900437 props.Libs = module.sdkLibraryProperties.Stub_only_libs
Jiyong Park82484c02018-04-23 21:41:26 +0900438 props.Product_variables.Pdk.Enabled = proptools.BoolPtr(false)
Sundong Ahn054b19a2018-10-19 13:46:09 +0900439 props.Openjdk9.Srcs = module.Library.Module.properties.Openjdk9.Srcs
440 props.Openjdk9.Javacflags = module.Library.Module.properties.Openjdk9.Javacflags
441 props.Java_version = module.Library.Module.properties.Java_version
442 if module.Library.Module.deviceProperties.Compile_dex != nil {
443 props.Compile_dex = module.Library.Module.deviceProperties.Compile_dex
Sundong Ahndd567f92018-07-31 17:19:11 +0900444 }
Jiyong Parkc678ad32018-04-10 13:07:10 +0900445
446 if module.SocSpecific() {
447 props.Soc_specific = proptools.BoolPtr(true)
448 } else if module.DeviceSpecific() {
449 props.Device_specific = proptools.BoolPtr(true)
450 } else if module.ProductSpecific() {
451 props.Product_specific = proptools.BoolPtr(true)
Sundong Ahn0d7dff42019-12-04 12:53:44 +0900452 } else if module.SystemExtSpecific() {
453 props.System_ext_specific = proptools.BoolPtr(true)
Jiyong Parkc678ad32018-04-10 13:07:10 +0900454 }
Anton Hansson6bb88102020-03-27 19:43:19 +0000455 // Dist the class jar artifact for sdk builds.
456 if !Bool(module.sdkLibraryProperties.No_dist) {
457 props.Dist.Targets = []string{"sdk", "win_sdk"}
458 props.Dist.Dest = proptools.StringPtr(fmt.Sprintf("%v.jar", module.BaseModuleName()))
459 props.Dist.Dir = proptools.StringPtr(module.apiDistPath(apiScope))
460 props.Dist.Tag = proptools.StringPtr(".jar")
461 }
Jiyong Parkc678ad32018-04-10 13:07:10 +0900462
Colin Cross84dfc3d2019-09-25 11:33:01 -0700463 mctx.CreateModule(LibraryFactory, &props)
Jiyong Parkc678ad32018-04-10 13:07:10 +0900464}
465
Paul Duffincbcfcaa2020-04-07 18:49:53 +0100466// Creates a droidstubs module that creates stubs source files from the given full source
Jiyong Parkc678ad32018-04-10 13:07:10 +0900467// files
Paul Duffind1b3a922020-01-22 11:57:20 +0000468func (module *SdkLibrary) createStubsSources(mctx android.LoadHookContext, apiScope *apiScope) {
Jiyong Parkc678ad32018-04-10 13:07:10 +0900469 props := struct {
Sundong Ahn054b19a2018-10-19 13:46:09 +0900470 Name *string
471 Srcs []string
472 Installable *bool
Paul Duffin52d398a2019-06-11 12:31:14 +0100473 Sdk_version *string
Paul Duffin12ceb462019-12-24 20:31:31 +0000474 System_modules *string
Sundong Ahn054b19a2018-10-19 13:46:09 +0900475 Libs []string
Paul Duffin11512472019-02-11 15:55:17 +0000476 Arg_files []string
Sundong Ahn054b19a2018-10-19 13:46:09 +0900477 Args *string
Sundong Ahn054b19a2018-10-19 13:46:09 +0900478 Java_version *string
479 Merge_annotations_dirs []string
480 Merge_inclusion_annotations_dirs []string
481 Check_api struct {
Inseob Kim38449af2019-02-28 14:24:05 +0900482 Current ApiToCheck
483 Last_released ApiToCheck
484 Ignore_missing_latest_api *bool
Jiyong Park58c518b2018-05-12 22:29:12 +0900485 }
Sundong Ahn1b92c822018-05-29 11:35:17 +0900486 Aidl struct {
487 Include_dirs []string
488 Local_include_dirs []string
489 }
Anton Hansson6bb88102020-03-27 19:43:19 +0000490 Dist struct {
491 Targets []string
492 Dest *string
493 Dir *string
494 }
Jiyong Parkc678ad32018-04-10 13:07:10 +0900495 }{}
496
Paul Duffinda364252020-04-28 14:08:32 +0100497 // The stubs source processing uses the same compile time classpath when extracting the
498 // API from the implementation library as it does when compiling it. i.e. the same
499 // * sdk version
500 // * system_modules
501 // * libs (static_libs/libs)
Paul Duffin250e6192019-06-07 10:44:37 +0100502
Jiyong Parkdf130542018-04-27 16:29:21 +0900503 props.Name = proptools.StringPtr(module.docsName(apiScope))
Sundong Ahn054b19a2018-10-19 13:46:09 +0900504 props.Srcs = append(props.Srcs, module.Library.Module.properties.Srcs...)
Paul Duffinda364252020-04-28 14:08:32 +0100505 props.Sdk_version = module.Library.Module.deviceProperties.Sdk_version
Paul Duffin12ceb462019-12-24 20:31:31 +0000506 props.System_modules = module.Library.Module.deviceProperties.System_modules
Jiyong Parkc678ad32018-04-10 13:07:10 +0900507 props.Installable = proptools.BoolPtr(false)
Sundong Ahne6f0b052018-06-05 16:46:14 +0900508 // A droiddoc module has only one Libs property and doesn't distinguish between
509 // shared libs and static libs. So we need to add both of these libs to Libs property.
Sundong Ahn054b19a2018-10-19 13:46:09 +0900510 props.Libs = module.Library.Module.properties.Libs
511 props.Libs = append(props.Libs, module.Library.Module.properties.Static_libs...)
512 props.Aidl.Include_dirs = module.Library.Module.deviceProperties.Aidl.Include_dirs
513 props.Aidl.Local_include_dirs = module.Library.Module.deviceProperties.Aidl.Local_include_dirs
Sundong Ahn054b19a2018-10-19 13:46:09 +0900514 props.Java_version = module.Library.Module.properties.Java_version
Jiyong Parkc678ad32018-04-10 13:07:10 +0900515
Sundong Ahn054b19a2018-10-19 13:46:09 +0900516 props.Merge_annotations_dirs = module.sdkLibraryProperties.Merge_annotations_dirs
517 props.Merge_inclusion_annotations_dirs = module.sdkLibraryProperties.Merge_inclusion_annotations_dirs
518
Paul Duffincbcfcaa2020-04-07 18:49:53 +0100519 droidstubsArgs := []string{}
Paul Duffin235ffff2019-12-24 10:41:30 +0000520 if len(module.sdkLibraryProperties.Api_packages) != 0 {
Paul Duffincbcfcaa2020-04-07 18:49:53 +0100521 droidstubsArgs = append(droidstubsArgs, "--stub-packages "+strings.Join(module.sdkLibraryProperties.Api_packages, ":"))
Paul Duffin235ffff2019-12-24 10:41:30 +0000522 }
523 if len(module.sdkLibraryProperties.Hidden_api_packages) != 0 {
Paul Duffincbcfcaa2020-04-07 18:49:53 +0100524 droidstubsArgs = append(droidstubsArgs,
Paul Duffin235ffff2019-12-24 10:41:30 +0000525 android.JoinWithPrefix(module.sdkLibraryProperties.Hidden_api_packages, " --hide-package "))
526 }
Paul Duffincbcfcaa2020-04-07 18:49:53 +0100527 droidstubsArgs = append(droidstubsArgs, module.sdkLibraryProperties.Droiddoc_options...)
Paul Duffin235ffff2019-12-24 10:41:30 +0000528 disabledWarnings := []string{
529 "MissingPermission",
530 "BroadcastBehavior",
531 "HiddenSuperclass",
532 "DeprecationMismatch",
533 "UnavailableSymbol",
534 "SdkConstant",
535 "HiddenTypeParameter",
536 "Todo",
537 "Typo",
538 }
Paul Duffincbcfcaa2020-04-07 18:49:53 +0100539 droidstubsArgs = append(droidstubsArgs, android.JoinWithPrefix(disabledWarnings, "--hide "))
Sundong Ahnfb2721f2018-09-17 13:23:09 +0900540
Paul Duffin3c7c3472020-04-07 18:50:10 +0100541 // Add in scope specific arguments.
542 droidstubsArgs = append(droidstubsArgs, apiScope.droidstubsArgs...)
Paul Duffin11512472019-02-11 15:55:17 +0000543 props.Arg_files = module.sdkLibraryProperties.Droiddoc_option_files
Paul Duffincbcfcaa2020-04-07 18:49:53 +0100544 props.Args = proptools.StringPtr(strings.Join(droidstubsArgs, " "))
Jiyong Parkc678ad32018-04-10 13:07:10 +0900545
546 // List of APIs identified from the provided source files are created. They are later
547 // compared against to the not-yet-released (a.k.a current) list of APIs and to the
548 // last-released (a.k.a numbered) list of API.
Paul Duffind1b3a922020-01-22 11:57:20 +0000549 currentApiFileName := apiScope.apiFilePrefix + "current.txt"
550 removedApiFileName := apiScope.apiFilePrefix + "removed.txt"
Paul Duffin749f98f2019-12-30 17:23:46 +0000551 apiDir := module.getApiDir()
552 currentApiFileName = path.Join(apiDir, currentApiFileName)
553 removedApiFileName = path.Join(apiDir, removedApiFileName)
Jiyong Parkc678ad32018-04-10 13:07:10 +0900554
Jiyong Park58c518b2018-05-12 22:29:12 +0900555 // check against the not-yet-release API
556 props.Check_api.Current.Api_file = proptools.StringPtr(currentApiFileName)
557 props.Check_api.Current.Removed_api_file = proptools.StringPtr(removedApiFileName)
Jiyong Park58c518b2018-05-12 22:29:12 +0900558
559 // check against the latest released API
560 props.Check_api.Last_released.Api_file = proptools.StringPtr(
561 module.latestApiFilegroupName(apiScope))
562 props.Check_api.Last_released.Removed_api_file = proptools.StringPtr(
563 module.latestRemovedApiFilegroupName(apiScope))
Inseob Kim38449af2019-02-28 14:24:05 +0900564 props.Check_api.Ignore_missing_latest_api = proptools.BoolPtr(true)
Jiyong Park58c518b2018-05-12 22:29:12 +0900565
Anton Hansson6bb88102020-03-27 19:43:19 +0000566 // Dist the api txt artifact for sdk builds.
567 if !Bool(module.sdkLibraryProperties.No_dist) {
568 props.Dist.Targets = []string{"sdk", "win_sdk"}
569 props.Dist.Dest = proptools.StringPtr(fmt.Sprintf("%v.txt", module.BaseModuleName()))
570 props.Dist.Dir = proptools.StringPtr(path.Join(module.apiDistPath(apiScope), "api"))
571 }
572
Colin Cross84dfc3d2019-09-25 11:33:01 -0700573 mctx.CreateModule(DroidstubsFactory, &props)
Jiyong Parkc678ad32018-04-10 13:07:10 +0900574}
575
Jooyung Hanb8fa86a2020-03-10 06:23:13 +0900576func (module *SdkLibrary) DepIsInSameApex(mctx android.BaseModuleContext, dep android.Module) bool {
577 depTag := mctx.OtherModuleDependencyTag(dep)
578 if depTag == xmlPermissionsFileTag {
579 return true
580 }
581 return module.Library.DepIsInSameApex(mctx, dep)
582}
583
Jiyong Parkc678ad32018-04-10 13:07:10 +0900584// Creates the xml file that publicizes the runtime library
Colin Crossf8b860a2019-04-16 14:43:28 -0700585func (module *SdkLibrary) createXmlFile(mctx android.LoadHookContext) {
Jiyong Parke3833882020-02-17 17:28:10 +0900586 props := struct {
Sundong Ahn0d7dff42019-12-04 12:53:44 +0900587 Name *string
Jiyong Parke3833882020-02-17 17:28:10 +0900588 Lib_name *string
Sundong Ahn0d7dff42019-12-04 12:53:44 +0900589 Soc_specific *bool
590 Device_specific *bool
591 Product_specific *bool
592 System_ext_specific *bool
Jooyung Hanb8fa86a2020-03-10 06:23:13 +0900593 Apex_available []string
Jiyong Parke3833882020-02-17 17:28:10 +0900594 }{
Jooyung Hanb8fa86a2020-03-10 06:23:13 +0900595 Name: proptools.StringPtr(module.xmlFileName()),
596 Lib_name: proptools.StringPtr(module.BaseModuleName()),
597 Apex_available: module.ApexProperties.Apex_available,
Jiyong Parkc678ad32018-04-10 13:07:10 +0900598 }
Jiyong Parke3833882020-02-17 17:28:10 +0900599
600 if module.SocSpecific() {
601 props.Soc_specific = proptools.BoolPtr(true)
602 } else if module.DeviceSpecific() {
603 props.Device_specific = proptools.BoolPtr(true)
604 } else if module.ProductSpecific() {
605 props.Product_specific = proptools.BoolPtr(true)
606 } else if module.SystemExtSpecific() {
607 props.System_ext_specific = proptools.BoolPtr(true)
608 }
609
610 mctx.CreateModule(sdkLibraryXmlFactory, &props)
Jiyong Parkc678ad32018-04-10 13:07:10 +0900611}
612
Paul Duffin50061512020-01-21 16:31:05 +0000613func PrebuiltJars(ctx android.BaseModuleContext, baseName string, s sdkSpec) android.Paths {
Jiyong Park6a927c42020-01-21 02:03:43 +0900614 var ver sdkVersion
615 var kind sdkKind
616 if s.usePrebuilt(ctx) {
617 ver = s.version
618 kind = s.kind
Jiyong Parkc678ad32018-04-10 13:07:10 +0900619 } else {
Jiyong Park6a927c42020-01-21 02:03:43 +0900620 // We don't have prebuilt SDK for the specific sdkVersion.
621 // Instead of breaking the build, fallback to use "system_current"
622 ver = sdkVersionCurrent
623 kind = sdkSystem
Sundong Ahn054b19a2018-10-19 13:46:09 +0900624 }
Jiyong Park6a927c42020-01-21 02:03:43 +0900625
626 dir := filepath.Join("prebuilts", "sdk", ver.String(), kind.String())
Paul Duffin50061512020-01-21 16:31:05 +0000627 jar := filepath.Join(dir, baseName+".jar")
Sundong Ahn054b19a2018-10-19 13:46:09 +0900628 jarPath := android.ExistentPathForSource(ctx, jar)
Sundong Ahnae418ac2019-02-28 15:01:28 +0900629 if !jarPath.Valid() {
Colin Cross07c88562020-01-07 09:34:44 -0800630 if ctx.Config().AllowMissingDependencies() {
631 return android.Paths{android.PathForSource(ctx, jar)}
632 } else {
Jiyong Park6a927c42020-01-21 02:03:43 +0900633 ctx.PropertyErrorf("sdk_library", "invalid sdk version %q, %q does not exist", s.raw, jar)
Colin Cross07c88562020-01-07 09:34:44 -0800634 }
Sundong Ahnae418ac2019-02-28 15:01:28 +0900635 return nil
636 }
Sundong Ahn054b19a2018-10-19 13:46:09 +0900637 return android.Paths{jarPath.Path()}
638}
639
Paul Duffind1b3a922020-01-22 11:57:20 +0000640func (module *SdkLibrary) sdkJars(
641 ctx android.BaseModuleContext,
642 sdkVersion sdkSpec,
643 headerJars bool) android.Paths {
644
Paul Duffin50061512020-01-21 16:31:05 +0000645 // If a specific numeric version has been requested then use prebuilt versions of the sdk.
646 if sdkVersion.version.isNumbered() {
647 return PrebuiltJars(ctx, module.BaseModuleName(), sdkVersion)
Sundong Ahn054b19a2018-10-19 13:46:09 +0900648 } else {
Paul Duffind1b3a922020-01-22 11:57:20 +0000649 if !sdkVersion.specified() {
650 if headerJars {
651 return module.Library.HeaderJars()
652 } else {
653 return module.Library.ImplementationJars()
654 }
655 }
Paul Duffin726d23c2020-01-22 16:30:37 +0000656 var apiScope *apiScope
Jiyong Park6a927c42020-01-21 02:03:43 +0900657 switch sdkVersion.kind {
658 case sdkSystem:
Paul Duffin726d23c2020-01-22 16:30:37 +0000659 apiScope = apiScopeSystem
660 case sdkTest:
661 apiScope = apiScopeTest
Jiyong Park6a927c42020-01-21 02:03:43 +0900662 case sdkPrivate:
Sundong Ahn054b19a2018-10-19 13:46:09 +0900663 return module.Library.HeaderJars()
Jiyong Park6a927c42020-01-21 02:03:43 +0900664 default:
Paul Duffin726d23c2020-01-22 16:30:37 +0000665 apiScope = apiScopePublic
Paul Duffind1b3a922020-01-22 11:57:20 +0000666 }
667
Paul Duffin726d23c2020-01-22 16:30:37 +0000668 paths := module.getScopePaths(apiScope)
Paul Duffind1b3a922020-01-22 11:57:20 +0000669 if headerJars {
670 return paths.stubsHeaderPath
671 } else {
672 return paths.stubsImplPath
Sundong Ahn054b19a2018-10-19 13:46:09 +0900673 }
Jiyong Parkc678ad32018-04-10 13:07:10 +0900674 }
675}
676
Sundong Ahn241cd372018-07-13 16:16:44 +0900677// to satisfy SdkLibraryDependency interface
Paul Duffind1b3a922020-01-22 11:57:20 +0000678func (module *SdkLibrary) SdkHeaderJars(ctx android.BaseModuleContext, sdkVersion sdkSpec) android.Paths {
679 return module.sdkJars(ctx, sdkVersion, true /*headerJars*/)
680}
681
682// to satisfy SdkLibraryDependency interface
Jiyong Park6a927c42020-01-21 02:03:43 +0900683func (module *SdkLibrary) SdkImplementationJars(ctx android.BaseModuleContext, sdkVersion sdkSpec) android.Paths {
Paul Duffind1b3a922020-01-22 11:57:20 +0000684 return module.sdkJars(ctx, sdkVersion, false /*headerJars*/)
Sundong Ahn241cd372018-07-13 16:16:44 +0900685}
686
Sundong Ahn80a87b32019-05-13 15:02:50 +0900687func (module *SdkLibrary) SetNoDist() {
688 module.sdkLibraryProperties.No_dist = proptools.BoolPtr(true)
689}
690
Colin Cross571cccf2019-02-04 11:22:08 -0800691var javaSdkLibrariesKey = android.NewOnceKey("javaSdkLibraries")
692
Jiyong Park82484c02018-04-23 21:41:26 +0900693func javaSdkLibraries(config android.Config) *[]string {
Colin Cross571cccf2019-02-04 11:22:08 -0800694 return config.Once(javaSdkLibrariesKey, func() interface{} {
Jiyong Park82484c02018-04-23 21:41:26 +0900695 return &[]string{}
696 }).(*[]string)
697}
698
Paul Duffin749f98f2019-12-30 17:23:46 +0000699func (module *SdkLibrary) getApiDir() string {
700 return proptools.StringDefault(module.sdkLibraryProperties.Api_dir, "api")
701}
702
Jiyong Parkc678ad32018-04-10 13:07:10 +0900703// For a java_sdk_library module, create internal modules for stubs, docs,
704// runtime libs and xml file. If requested, the stubs and docs are created twice
705// once for public API level and once for system API level
Colin Crossf8b860a2019-04-16 14:43:28 -0700706func (module *SdkLibrary) CreateInternalModules(mctx android.LoadHookContext) {
Inseob Kim6e93ac92019-03-21 17:43:49 +0900707 if len(module.Library.Module.properties.Srcs) == 0 {
Inseob Kimc0907f12019-02-08 21:00:45 +0900708 mctx.PropertyErrorf("srcs", "java_sdk_library must specify srcs")
Jooyung Han58f26ab2019-12-18 15:34:32 +0900709 return
Inseob Kimc0907f12019-02-08 21:00:45 +0900710 }
711
Paul Duffin37e0b772019-12-30 17:20:10 +0000712 // If this builds against standard libraries (i.e. is not part of the core libraries)
713 // then assume it provides both system and test apis. Otherwise, assume it does not and
714 // also assume it does not contribute to the dist build.
715 sdkDep := decodeSdkDep(mctx, sdkContext(&module.Library))
716 hasSystemAndTestApis := sdkDep.hasStandardLibs()
717 module.sdkLibraryProperties.Has_system_and_test_apis = hasSystemAndTestApis
718 module.sdkLibraryProperties.No_dist = proptools.BoolPtr(!hasSystemAndTestApis)
719
Inseob Kim8098faa2019-03-18 10:19:51 +0900720 missing_current_api := false
721
Paul Duffind1b3a922020-01-22 11:57:20 +0000722 activeScopes := module.getActiveApiScopes()
723
Paul Duffin749f98f2019-12-30 17:23:46 +0000724 apiDir := module.getApiDir()
Paul Duffind1b3a922020-01-22 11:57:20 +0000725 for _, scope := range activeScopes {
Inseob Kim8098faa2019-03-18 10:19:51 +0900726 for _, api := range []string{"current.txt", "removed.txt"} {
Paul Duffind1b3a922020-01-22 11:57:20 +0000727 path := path.Join(mctx.ModuleDir(), apiDir, scope.apiFilePrefix+api)
Inseob Kim8098faa2019-03-18 10:19:51 +0900728 p := android.ExistentPathForSource(mctx, path)
729 if !p.Valid() {
730 mctx.ModuleErrorf("Current api file %#v doesn't exist", path)
731 missing_current_api = true
732 }
733 }
734 }
735
736 if missing_current_api {
737 script := "build/soong/scripts/gen-java-current-api-files.sh"
738 p := android.ExistentPathForSource(mctx, script)
739
740 if !p.Valid() {
741 panic(fmt.Sprintf("script file %s doesn't exist", script))
742 }
743
744 mctx.ModuleErrorf("One or more current api files are missing. "+
745 "You can update them by:\n"+
Paul Duffin37e0b772019-12-30 17:20:10 +0000746 "%s %q %s && m update-api",
Paul Duffind1b3a922020-01-22 11:57:20 +0000747 script, filepath.Join(mctx.ModuleDir(), apiDir),
748 strings.Join(activeScopes.Strings(func(s *apiScope) string { return s.apiFilePrefix }), " "))
Inseob Kim8098faa2019-03-18 10:19:51 +0900749 return
750 }
751
Paul Duffind1b3a922020-01-22 11:57:20 +0000752 for _, scope := range activeScopes {
753 module.createStubsLibrary(mctx, scope)
754 module.createStubsSources(mctx, scope)
Inseob Kimc0907f12019-02-08 21:00:45 +0900755 }
756
Paul Duffin43db9be2019-12-30 17:35:49 +0000757 if !proptools.Bool(module.sdkLibraryProperties.Api_only) {
758 // for runtime
759 module.createXmlFile(mctx)
760
761 // record java_sdk_library modules so that they are exported to make
762 javaSdkLibraries := javaSdkLibraries(mctx.Config())
763 javaSdkLibrariesLock.Lock()
764 defer javaSdkLibrariesLock.Unlock()
765 *javaSdkLibraries = append(*javaSdkLibraries, module.BaseModuleName())
766 }
Inseob Kimc0907f12019-02-08 21:00:45 +0900767}
768
769func (module *SdkLibrary) InitSdkLibraryProperties() {
Sundong Ahn054b19a2018-10-19 13:46:09 +0900770 module.AddProperties(
771 &module.sdkLibraryProperties,
772 &module.Library.Module.properties,
773 &module.Library.Module.dexpreoptProperties,
774 &module.Library.Module.deviceProperties,
775 &module.Library.Module.protoProperties,
776 )
777
778 module.Library.Module.properties.Installable = proptools.BoolPtr(true)
779 module.Library.Module.deviceProperties.IsSDKLibrary = true
Inseob Kimc0907f12019-02-08 21:00:45 +0900780}
Sundong Ahn054b19a2018-10-19 13:46:09 +0900781
Jaewoong Jung4f158ee2019-07-11 10:05:35 -0700782// java_sdk_library is a special Java library that provides optional platform APIs to apps.
783// In practice, it can be viewed as a combination of several modules: 1) stubs library that clients
784// are linked against to, 2) droiddoc module that internally generates API stubs source files,
785// 3) the real runtime shared library that implements the APIs, and 4) XML file for adding
786// the runtime lib to the classpath at runtime if requested via <uses-library>.
Inseob Kimc0907f12019-02-08 21:00:45 +0900787func SdkLibraryFactory() android.Module {
788 module := &SdkLibrary{}
789 module.InitSdkLibraryProperties()
Jooyung Han58f26ab2019-12-18 15:34:32 +0900790 android.InitApexModule(module)
Sundong Ahn054b19a2018-10-19 13:46:09 +0900791 InitJavaModule(module, android.HostAndDeviceSupported)
Colin Crossf8b860a2019-04-16 14:43:28 -0700792 android.AddLoadHook(module, func(ctx android.LoadHookContext) { module.CreateInternalModules(ctx) })
Jiyong Parkc678ad32018-04-10 13:07:10 +0900793 return module
794}
Colin Cross79c7c262019-04-17 11:11:46 -0700795
796//
797// SDK library prebuilts
798//
799
Paul Duffin56d44902020-01-31 13:36:25 +0000800// Properties associated with each api scope.
801type sdkLibraryScopeProperties struct {
Colin Cross79c7c262019-04-17 11:11:46 -0700802 Jars []string `android:"path"`
803
804 Sdk_version *string
805
Colin Cross79c7c262019-04-17 11:11:46 -0700806 // List of shared java libs that this module has dependencies to
807 Libs []string
Colin Cross79c7c262019-04-17 11:11:46 -0700808}
809
Paul Duffin56d44902020-01-31 13:36:25 +0000810type sdkLibraryImportProperties struct {
Paul Duffinfcfd7912020-01-31 17:54:30 +0000811 // List of shared java libs, common to all scopes, that this module has
812 // dependencies to
813 Libs []string
Paul Duffin56d44902020-01-31 13:36:25 +0000814}
815
Colin Cross79c7c262019-04-17 11:11:46 -0700816type sdkLibraryImport struct {
817 android.ModuleBase
818 android.DefaultableModuleBase
819 prebuilt android.Prebuilt
Paul Duffin61871622020-02-10 13:37:10 +0000820 android.ApexModuleBase
821 android.SdkBase
Colin Cross79c7c262019-04-17 11:11:46 -0700822
823 properties sdkLibraryImportProperties
824
Paul Duffin6a2bd112020-04-07 19:27:04 +0100825 // Map from api scope to the scope specific property structure.
826 scopeProperties map[*apiScope]*sdkLibraryScopeProperties
827
Paul Duffin56d44902020-01-31 13:36:25 +0000828 commonToSdkLibraryAndImport
Colin Cross79c7c262019-04-17 11:11:46 -0700829}
830
831var _ SdkLibraryDependency = (*sdkLibraryImport)(nil)
832
Paul Duffin6a2bd112020-04-07 19:27:04 +0100833// The type of a structure that contains a field of type sdkLibraryScopeProperties
834// for each apiscope in allApiScopes, e.g. something like:
835// struct {
836// Public sdkLibraryScopeProperties
837// System sdkLibraryScopeProperties
838// ...
839// }
840var allScopeStructType = createAllScopePropertiesStructType()
841
842// Dynamically create a structure type for each apiscope in allApiScopes.
843func createAllScopePropertiesStructType() reflect.Type {
844 var fields []reflect.StructField
845 for _, apiScope := range allApiScopes {
846 field := reflect.StructField{
847 Name: apiScope.fieldName,
848 Type: reflect.TypeOf(sdkLibraryScopeProperties{}),
849 }
850 fields = append(fields, field)
851 }
852
853 return reflect.StructOf(fields)
854}
855
856// Create an instance of the scope specific structure type and return a map
857// from apiscope to a pointer to each scope specific field.
858func createPropertiesInstance() (interface{}, map[*apiScope]*sdkLibraryScopeProperties) {
859 allScopePropertiesPtr := reflect.New(allScopeStructType)
860 allScopePropertiesStruct := allScopePropertiesPtr.Elem()
861 scopeProperties := make(map[*apiScope]*sdkLibraryScopeProperties)
862
863 for _, apiScope := range allApiScopes {
864 field := allScopePropertiesStruct.FieldByName(apiScope.fieldName)
865 scopeProperties[apiScope] = field.Addr().Interface().(*sdkLibraryScopeProperties)
866 }
867
868 return allScopePropertiesPtr.Interface(), scopeProperties
869}
870
Jaewoong Jung4f158ee2019-07-11 10:05:35 -0700871// java_sdk_library_import imports a prebuilt java_sdk_library.
Colin Cross79c7c262019-04-17 11:11:46 -0700872func sdkLibraryImportFactory() android.Module {
873 module := &sdkLibraryImport{}
874
Paul Duffin6a2bd112020-04-07 19:27:04 +0100875 allScopeProperties, scopeToProperties := createPropertiesInstance()
876 module.scopeProperties = scopeToProperties
877 module.AddProperties(&module.properties, allScopeProperties)
Colin Cross79c7c262019-04-17 11:11:46 -0700878
Paul Duffin0bdcb272020-02-06 15:24:57 +0000879 android.InitPrebuiltModule(module, &[]string{""})
Paul Duffin61871622020-02-10 13:37:10 +0000880 android.InitApexModule(module)
881 android.InitSdkAwareModule(module)
Colin Cross79c7c262019-04-17 11:11:46 -0700882 InitJavaModule(module, android.HostAndDeviceSupported)
883
884 android.AddLoadHook(module, func(mctx android.LoadHookContext) { module.createInternalModules(mctx) })
885 return module
886}
887
888func (module *sdkLibraryImport) Prebuilt() *android.Prebuilt {
889 return &module.prebuilt
890}
891
892func (module *sdkLibraryImport) Name() string {
893 return module.prebuilt.Name(module.ModuleBase.Name())
894}
895
896func (module *sdkLibraryImport) createInternalModules(mctx android.LoadHookContext) {
Colin Cross79c7c262019-04-17 11:11:46 -0700897
Paul Duffin50061512020-01-21 16:31:05 +0000898 // If the build is configured to use prebuilts then force this to be preferred.
899 if mctx.Config().UnbundledBuildUsePrebuiltSdks() {
900 module.prebuilt.ForcePrefer()
901 }
902
Paul Duffin6a2bd112020-04-07 19:27:04 +0100903 for apiScope, scopeProperties := range module.scopeProperties {
Paul Duffin56d44902020-01-31 13:36:25 +0000904 if len(scopeProperties.Jars) == 0 {
905 continue
906 }
907
Paul Duffinf6155722020-04-09 00:07:11 +0100908 module.createJavaImportForStubs(mctx, apiScope, scopeProperties)
Paul Duffin56d44902020-01-31 13:36:25 +0000909 }
Colin Cross79c7c262019-04-17 11:11:46 -0700910
911 javaSdkLibraries := javaSdkLibraries(mctx.Config())
912 javaSdkLibrariesLock.Lock()
913 defer javaSdkLibrariesLock.Unlock()
914 *javaSdkLibraries = append(*javaSdkLibraries, module.BaseModuleName())
915}
916
Paul Duffinf6155722020-04-09 00:07:11 +0100917func (module *sdkLibraryImport) createJavaImportForStubs(mctx android.LoadHookContext, apiScope *apiScope, scopeProperties *sdkLibraryScopeProperties) {
918 // Creates a java import for the jar with ".stubs" suffix
919 props := struct {
920 Name *string
921 Soc_specific *bool
922 Device_specific *bool
923 Product_specific *bool
924 System_ext_specific *bool
925 Sdk_version *string
926 Libs []string
927 Jars []string
928 Prefer *bool
929 }{}
930 props.Name = proptools.StringPtr(apiScope.stubsModuleName(module.BaseModuleName()))
931 props.Sdk_version = scopeProperties.Sdk_version
932 // Prepend any of the libs from the legacy public properties to the libs for each of the
933 // scopes to avoid having to duplicate them in each scope.
934 props.Libs = append(module.properties.Libs, scopeProperties.Libs...)
935 props.Jars = scopeProperties.Jars
936 if module.SocSpecific() {
937 props.Soc_specific = proptools.BoolPtr(true)
938 } else if module.DeviceSpecific() {
939 props.Device_specific = proptools.BoolPtr(true)
940 } else if module.ProductSpecific() {
941 props.Product_specific = proptools.BoolPtr(true)
942 } else if module.SystemExtSpecific() {
943 props.System_ext_specific = proptools.BoolPtr(true)
944 }
945 // If the build should use prebuilt sdks then set prefer to true on the stubs library.
946 // That will cause the prebuilt version of the stubs to override the source version.
947 if mctx.Config().UnbundledBuildUsePrebuiltSdks() {
948 props.Prefer = proptools.BoolPtr(true)
949 }
950 mctx.CreateModule(ImportFactory, &props)
951}
952
Colin Cross79c7c262019-04-17 11:11:46 -0700953func (module *sdkLibraryImport) DepsMutator(ctx android.BottomUpMutatorContext) {
Paul Duffin6a2bd112020-04-07 19:27:04 +0100954 for apiScope, scopeProperties := range module.scopeProperties {
Paul Duffin56d44902020-01-31 13:36:25 +0000955 if len(scopeProperties.Jars) == 0 {
956 continue
957 }
958
959 // Add dependencies to the prebuilt stubs library
960 ctx.AddVariationDependencies(nil, apiScope.stubsTag, apiScope.stubsModuleName(module.BaseModuleName()))
961 }
Colin Cross79c7c262019-04-17 11:11:46 -0700962}
963
964func (module *sdkLibraryImport) GenerateAndroidBuildActions(ctx android.ModuleContext) {
965 // Record the paths to the prebuilt stubs library.
966 ctx.VisitDirectDeps(func(to android.Module) {
967 tag := ctx.OtherModuleDependencyTag(to)
968
Paul Duffin56d44902020-01-31 13:36:25 +0000969 if lib, ok := to.(Dependency); ok {
970 if scopeTag, ok := tag.(scopeDependencyTag); ok {
971 apiScope := scopeTag.apiScope
972 scopePaths := module.getScopePaths(apiScope)
973 scopePaths.stubsHeaderPath = lib.HeaderJars()
974 }
Colin Cross79c7c262019-04-17 11:11:46 -0700975 }
976 })
977}
978
Paul Duffin56d44902020-01-31 13:36:25 +0000979func (module *sdkLibraryImport) sdkJars(
980 ctx android.BaseModuleContext,
981 sdkVersion sdkSpec) android.Paths {
982
Paul Duffin50061512020-01-21 16:31:05 +0000983 // If a specific numeric version has been requested then use prebuilt versions of the sdk.
984 if sdkVersion.version.isNumbered() {
985 return PrebuiltJars(ctx, module.BaseModuleName(), sdkVersion)
986 }
987
Paul Duffin56d44902020-01-31 13:36:25 +0000988 var apiScope *apiScope
989 switch sdkVersion.kind {
990 case sdkSystem:
991 apiScope = apiScopeSystem
992 case sdkTest:
993 apiScope = apiScopeTest
994 default:
995 apiScope = apiScopePublic
996 }
997
998 paths := module.getScopePaths(apiScope)
999 return paths.stubsHeaderPath
1000}
1001
Colin Cross79c7c262019-04-17 11:11:46 -07001002// to satisfy SdkLibraryDependency interface
Jiyong Park6a927c42020-01-21 02:03:43 +09001003func (module *sdkLibraryImport) SdkHeaderJars(ctx android.BaseModuleContext, sdkVersion sdkSpec) android.Paths {
Colin Cross79c7c262019-04-17 11:11:46 -07001004 // This module is just a wrapper for the prebuilt stubs.
Paul Duffin56d44902020-01-31 13:36:25 +00001005 return module.sdkJars(ctx, sdkVersion)
Colin Cross79c7c262019-04-17 11:11:46 -07001006}
1007
1008// to satisfy SdkLibraryDependency interface
Jiyong Park6a927c42020-01-21 02:03:43 +09001009func (module *sdkLibraryImport) SdkImplementationJars(ctx android.BaseModuleContext, sdkVersion sdkSpec) android.Paths {
Colin Cross79c7c262019-04-17 11:11:46 -07001010 // This module is just a wrapper for the stubs.
Paul Duffin56d44902020-01-31 13:36:25 +00001011 return module.sdkJars(ctx, sdkVersion)
Colin Cross79c7c262019-04-17 11:11:46 -07001012}
Jiyong Parke3833882020-02-17 17:28:10 +09001013
1014//
1015// java_sdk_library_xml
1016//
1017type sdkLibraryXml struct {
1018 android.ModuleBase
1019 android.DefaultableModuleBase
1020 android.ApexModuleBase
1021
1022 properties sdkLibraryXmlProperties
1023
1024 outputFilePath android.OutputPath
1025 installDirPath android.InstallPath
1026}
1027
1028type sdkLibraryXmlProperties struct {
1029 // canonical name of the lib
1030 Lib_name *string
1031}
1032
1033// java_sdk_library_xml builds the permission xml file for a java_sdk_library.
1034// Not to be used directly by users. java_sdk_library internally uses this.
1035func sdkLibraryXmlFactory() android.Module {
1036 module := &sdkLibraryXml{}
1037
1038 module.AddProperties(&module.properties)
1039
1040 android.InitApexModule(module)
1041 android.InitAndroidArchModule(module, android.DeviceSupported, android.MultilibCommon)
1042
1043 return module
1044}
1045
1046// from android.PrebuiltEtcModule
1047func (module *sdkLibraryXml) SubDir() string {
1048 return "permissions"
1049}
1050
1051// from android.PrebuiltEtcModule
1052func (module *sdkLibraryXml) OutputFile() android.OutputPath {
1053 return module.outputFilePath
1054}
1055
1056// from android.ApexModule
1057func (module *sdkLibraryXml) AvailableFor(what string) bool {
1058 return true
1059}
1060
1061func (module *sdkLibraryXml) DepsMutator(ctx android.BottomUpMutatorContext) {
1062 // do nothing
1063}
1064
1065// File path to the runtime implementation library
1066func (module *sdkLibraryXml) implPath() string {
1067 implName := proptools.String(module.properties.Lib_name)
1068 if apexName := module.ApexName(); apexName != "" {
1069 // TODO(b/146468504): ApexName() is only a soong module name, not apex name.
1070 // In most cases, this works fine. But when apex_name is set or override_apex is used
1071 // this can be wrong.
1072 return fmt.Sprintf("/apex/%s/javalib/%s.jar", apexName, implName)
1073 }
1074 partition := "system"
1075 if module.SocSpecific() {
1076 partition = "vendor"
1077 } else if module.DeviceSpecific() {
1078 partition = "odm"
1079 } else if module.ProductSpecific() {
1080 partition = "product"
1081 } else if module.SystemExtSpecific() {
1082 partition = "system_ext"
1083 }
1084 return "/" + partition + "/framework/" + implName + ".jar"
1085}
1086
1087func (module *sdkLibraryXml) GenerateAndroidBuildActions(ctx android.ModuleContext) {
1088 libName := proptools.String(module.properties.Lib_name)
1089 xmlContent := fmt.Sprintf(permissionsTemplate, libName, module.implPath())
1090
1091 module.outputFilePath = android.PathForModuleOut(ctx, libName+".xml").OutputPath
1092 rule := android.NewRuleBuilder()
1093 rule.Command().
1094 Text("/bin/bash -c \"echo -e '" + xmlContent + "'\" > ").
1095 Output(module.outputFilePath)
1096
1097 rule.Build(pctx, ctx, "java_sdk_xml", "Permission XML")
1098
1099 module.installDirPath = android.PathForModuleInstall(ctx, "etc", module.SubDir())
1100}
1101
1102func (module *sdkLibraryXml) AndroidMkEntries() []android.AndroidMkEntries {
1103 if !module.IsForPlatform() {
1104 return []android.AndroidMkEntries{android.AndroidMkEntries{
1105 Disabled: true,
1106 }}
1107 }
1108
1109 return []android.AndroidMkEntries{android.AndroidMkEntries{
1110 Class: "ETC",
1111 OutputFile: android.OptionalPathForPath(module.outputFilePath),
1112 ExtraEntries: []android.AndroidMkExtraEntriesFunc{
1113 func(entries *android.AndroidMkEntries) {
1114 entries.SetString("LOCAL_MODULE_TAGS", "optional")
1115 entries.SetString("LOCAL_MODULE_PATH", module.installDirPath.ToMakePath().String())
1116 entries.SetString("LOCAL_INSTALLED_MODULE_STEM", module.outputFilePath.Base())
1117 },
1118 },
1119 }}
1120}
Paul Duffin61871622020-02-10 13:37:10 +00001121
1122type sdkLibrarySdkMemberType struct {
1123 android.SdkMemberTypeBase
1124}
1125
1126func (s *sdkLibrarySdkMemberType) AddDependencies(mctx android.BottomUpMutatorContext, dependencyTag blueprint.DependencyTag, names []string) {
1127 mctx.AddVariationDependencies(nil, dependencyTag, names...)
1128}
1129
1130func (s *sdkLibrarySdkMemberType) IsInstance(module android.Module) bool {
1131 _, ok := module.(*SdkLibrary)
1132 return ok
1133}
1134
1135func (s *sdkLibrarySdkMemberType) AddPrebuiltModule(ctx android.SdkMemberContext, member android.SdkMember) android.BpModule {
1136 return ctx.SnapshotBuilder().AddPrebuiltModule(member, "java_sdk_library_import")
1137}
1138
1139func (s *sdkLibrarySdkMemberType) CreateVariantPropertiesStruct() android.SdkMemberProperties {
1140 return &sdkLibrarySdkMemberProperties{}
1141}
1142
1143type sdkLibrarySdkMemberProperties struct {
1144 android.SdkMemberPropertiesBase
1145
1146 // Scope to per scope properties.
1147 Scopes map[*apiScope]scopeProperties
1148
1149 // Additional libraries that the exported stubs libraries depend upon.
1150 Libs []string
1151}
1152
1153type scopeProperties struct {
1154 Jars android.Paths
1155 SdkVersion string
1156}
1157
1158func (s *sdkLibrarySdkMemberProperties) PopulateFromVariant(ctx android.SdkMemberContext, variant android.Module) {
1159 sdk := variant.(*SdkLibrary)
1160
1161 s.Scopes = make(map[*apiScope]scopeProperties)
1162 for _, apiScope := range allApiScopes {
1163 paths := sdk.getScopePaths(apiScope)
1164 jars := paths.stubsImplPath
1165 if len(jars) > 0 {
1166 properties := scopeProperties{}
1167 properties.Jars = jars
1168 properties.SdkVersion = apiScope.sdkVersion
1169 s.Scopes[apiScope] = properties
1170 }
1171 }
1172
1173 s.Libs = sdk.properties.Libs
1174}
1175
1176func (s *sdkLibrarySdkMemberProperties) AddToPropertySet(ctx android.SdkMemberContext, propertySet android.BpPropertySet) {
1177 for _, apiScope := range allApiScopes {
1178 if properties, ok := s.Scopes[apiScope]; ok {
1179 scopeSet := propertySet.AddPropertySet(apiScope.name)
1180
1181 var jars []string
1182 for _, p := range properties.Jars {
1183 dest := filepath.Join("sdk_library", s.OsPrefix(), apiScope.name, ctx.Name()+"-stubs.jar")
1184 ctx.SnapshotBuilder().CopyToSnapshot(p, dest)
1185 jars = append(jars, dest)
1186 }
1187 scopeSet.AddProperty("jars", jars)
1188
1189 if properties.SdkVersion != "" {
1190 scopeSet.AddProperty("sdk_version", properties.SdkVersion)
1191 }
1192 }
1193 }
1194
1195 if len(s.Libs) > 0 {
1196 propertySet.AddPropertyWithTag("libs", s.Libs, ctx.SnapshotBuilder().SdkMemberReferencePropertyTag(false))
1197 }
1198}