blob: e62a71510dd4f898dc69d060aabaf9c87da74a05 [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 Duffin250e6192019-06-07 10:44:37 +0100497 sdkDep := decodeSdkDep(mctx, sdkContext(&module.Library))
Paul Duffin12ceb462019-12-24 20:31:31 +0000498 // Use the platform API if standard libraries were requested, otherwise use
499 // no default libraries.
Paul Duffin52d398a2019-06-11 12:31:14 +0100500 sdkVersion := ""
501 if !sdkDep.hasStandardLibs() {
502 sdkVersion = "none"
503 }
Paul Duffin250e6192019-06-07 10:44:37 +0100504
Jiyong Parkdf130542018-04-27 16:29:21 +0900505 props.Name = proptools.StringPtr(module.docsName(apiScope))
Sundong Ahn054b19a2018-10-19 13:46:09 +0900506 props.Srcs = append(props.Srcs, module.Library.Module.properties.Srcs...)
Paul Duffin52d398a2019-06-11 12:31:14 +0100507 props.Sdk_version = proptools.StringPtr(sdkVersion)
Paul Duffin12ceb462019-12-24 20:31:31 +0000508 props.System_modules = module.Library.Module.deviceProperties.System_modules
Jiyong Parkc678ad32018-04-10 13:07:10 +0900509 props.Installable = proptools.BoolPtr(false)
Sundong Ahne6f0b052018-06-05 16:46:14 +0900510 // A droiddoc module has only one Libs property and doesn't distinguish between
511 // shared libs and static libs. So we need to add both of these libs to Libs property.
Sundong Ahn054b19a2018-10-19 13:46:09 +0900512 props.Libs = module.Library.Module.properties.Libs
513 props.Libs = append(props.Libs, module.Library.Module.properties.Static_libs...)
514 props.Aidl.Include_dirs = module.Library.Module.deviceProperties.Aidl.Include_dirs
515 props.Aidl.Local_include_dirs = module.Library.Module.deviceProperties.Aidl.Local_include_dirs
Sundong Ahn054b19a2018-10-19 13:46:09 +0900516 props.Java_version = module.Library.Module.properties.Java_version
Jiyong Parkc678ad32018-04-10 13:07:10 +0900517
Sundong Ahn054b19a2018-10-19 13:46:09 +0900518 props.Merge_annotations_dirs = module.sdkLibraryProperties.Merge_annotations_dirs
519 props.Merge_inclusion_annotations_dirs = module.sdkLibraryProperties.Merge_inclusion_annotations_dirs
520
Paul Duffincbcfcaa2020-04-07 18:49:53 +0100521 droidstubsArgs := []string{}
Paul Duffin235ffff2019-12-24 10:41:30 +0000522 if len(module.sdkLibraryProperties.Api_packages) != 0 {
Paul Duffincbcfcaa2020-04-07 18:49:53 +0100523 droidstubsArgs = append(droidstubsArgs, "--stub-packages "+strings.Join(module.sdkLibraryProperties.Api_packages, ":"))
Paul Duffin235ffff2019-12-24 10:41:30 +0000524 }
525 if len(module.sdkLibraryProperties.Hidden_api_packages) != 0 {
Paul Duffincbcfcaa2020-04-07 18:49:53 +0100526 droidstubsArgs = append(droidstubsArgs,
Paul Duffin235ffff2019-12-24 10:41:30 +0000527 android.JoinWithPrefix(module.sdkLibraryProperties.Hidden_api_packages, " --hide-package "))
528 }
Paul Duffincbcfcaa2020-04-07 18:49:53 +0100529 droidstubsArgs = append(droidstubsArgs, module.sdkLibraryProperties.Droiddoc_options...)
Paul Duffin235ffff2019-12-24 10:41:30 +0000530 disabledWarnings := []string{
531 "MissingPermission",
532 "BroadcastBehavior",
533 "HiddenSuperclass",
534 "DeprecationMismatch",
535 "UnavailableSymbol",
536 "SdkConstant",
537 "HiddenTypeParameter",
538 "Todo",
539 "Typo",
540 }
Paul Duffincbcfcaa2020-04-07 18:49:53 +0100541 droidstubsArgs = append(droidstubsArgs, android.JoinWithPrefix(disabledWarnings, "--hide "))
Sundong Ahnfb2721f2018-09-17 13:23:09 +0900542
Paul Duffin3c7c3472020-04-07 18:50:10 +0100543 // Add in scope specific arguments.
544 droidstubsArgs = append(droidstubsArgs, apiScope.droidstubsArgs...)
Paul Duffin11512472019-02-11 15:55:17 +0000545 props.Arg_files = module.sdkLibraryProperties.Droiddoc_option_files
Paul Duffincbcfcaa2020-04-07 18:49:53 +0100546 props.Args = proptools.StringPtr(strings.Join(droidstubsArgs, " "))
Jiyong Parkc678ad32018-04-10 13:07:10 +0900547
548 // List of APIs identified from the provided source files are created. They are later
549 // compared against to the not-yet-released (a.k.a current) list of APIs and to the
550 // last-released (a.k.a numbered) list of API.
Paul Duffind1b3a922020-01-22 11:57:20 +0000551 currentApiFileName := apiScope.apiFilePrefix + "current.txt"
552 removedApiFileName := apiScope.apiFilePrefix + "removed.txt"
Paul Duffin749f98f2019-12-30 17:23:46 +0000553 apiDir := module.getApiDir()
554 currentApiFileName = path.Join(apiDir, currentApiFileName)
555 removedApiFileName = path.Join(apiDir, removedApiFileName)
Jiyong Parkc678ad32018-04-10 13:07:10 +0900556
Jiyong Park58c518b2018-05-12 22:29:12 +0900557 // check against the not-yet-release API
558 props.Check_api.Current.Api_file = proptools.StringPtr(currentApiFileName)
559 props.Check_api.Current.Removed_api_file = proptools.StringPtr(removedApiFileName)
Jiyong Park58c518b2018-05-12 22:29:12 +0900560
561 // check against the latest released API
562 props.Check_api.Last_released.Api_file = proptools.StringPtr(
563 module.latestApiFilegroupName(apiScope))
564 props.Check_api.Last_released.Removed_api_file = proptools.StringPtr(
565 module.latestRemovedApiFilegroupName(apiScope))
Inseob Kim38449af2019-02-28 14:24:05 +0900566 props.Check_api.Ignore_missing_latest_api = proptools.BoolPtr(true)
Jiyong Park58c518b2018-05-12 22:29:12 +0900567
Anton Hansson6bb88102020-03-27 19:43:19 +0000568 // Dist the api txt artifact for sdk builds.
569 if !Bool(module.sdkLibraryProperties.No_dist) {
570 props.Dist.Targets = []string{"sdk", "win_sdk"}
571 props.Dist.Dest = proptools.StringPtr(fmt.Sprintf("%v.txt", module.BaseModuleName()))
572 props.Dist.Dir = proptools.StringPtr(path.Join(module.apiDistPath(apiScope), "api"))
573 }
574
Colin Cross84dfc3d2019-09-25 11:33:01 -0700575 mctx.CreateModule(DroidstubsFactory, &props)
Jiyong Parkc678ad32018-04-10 13:07:10 +0900576}
577
Jooyung Hanb8fa86a2020-03-10 06:23:13 +0900578func (module *SdkLibrary) DepIsInSameApex(mctx android.BaseModuleContext, dep android.Module) bool {
579 depTag := mctx.OtherModuleDependencyTag(dep)
580 if depTag == xmlPermissionsFileTag {
581 return true
582 }
583 return module.Library.DepIsInSameApex(mctx, dep)
584}
585
Jiyong Parkc678ad32018-04-10 13:07:10 +0900586// Creates the xml file that publicizes the runtime library
Colin Crossf8b860a2019-04-16 14:43:28 -0700587func (module *SdkLibrary) createXmlFile(mctx android.LoadHookContext) {
Jiyong Parke3833882020-02-17 17:28:10 +0900588 props := struct {
Sundong Ahn0d7dff42019-12-04 12:53:44 +0900589 Name *string
Jiyong Parke3833882020-02-17 17:28:10 +0900590 Lib_name *string
Sundong Ahn0d7dff42019-12-04 12:53:44 +0900591 Soc_specific *bool
592 Device_specific *bool
593 Product_specific *bool
594 System_ext_specific *bool
Jooyung Hanb8fa86a2020-03-10 06:23:13 +0900595 Apex_available []string
Jiyong Parke3833882020-02-17 17:28:10 +0900596 }{
Jooyung Hanb8fa86a2020-03-10 06:23:13 +0900597 Name: proptools.StringPtr(module.xmlFileName()),
598 Lib_name: proptools.StringPtr(module.BaseModuleName()),
599 Apex_available: module.ApexProperties.Apex_available,
Jiyong Parkc678ad32018-04-10 13:07:10 +0900600 }
Jiyong Parke3833882020-02-17 17:28:10 +0900601
602 if module.SocSpecific() {
603 props.Soc_specific = proptools.BoolPtr(true)
604 } else if module.DeviceSpecific() {
605 props.Device_specific = proptools.BoolPtr(true)
606 } else if module.ProductSpecific() {
607 props.Product_specific = proptools.BoolPtr(true)
608 } else if module.SystemExtSpecific() {
609 props.System_ext_specific = proptools.BoolPtr(true)
610 }
611
612 mctx.CreateModule(sdkLibraryXmlFactory, &props)
Jiyong Parkc678ad32018-04-10 13:07:10 +0900613}
614
Paul Duffin50061512020-01-21 16:31:05 +0000615func PrebuiltJars(ctx android.BaseModuleContext, baseName string, s sdkSpec) android.Paths {
Jiyong Park6a927c42020-01-21 02:03:43 +0900616 var ver sdkVersion
617 var kind sdkKind
618 if s.usePrebuilt(ctx) {
619 ver = s.version
620 kind = s.kind
Jiyong Parkc678ad32018-04-10 13:07:10 +0900621 } else {
Jiyong Park6a927c42020-01-21 02:03:43 +0900622 // We don't have prebuilt SDK for the specific sdkVersion.
623 // Instead of breaking the build, fallback to use "system_current"
624 ver = sdkVersionCurrent
625 kind = sdkSystem
Sundong Ahn054b19a2018-10-19 13:46:09 +0900626 }
Jiyong Park6a927c42020-01-21 02:03:43 +0900627
628 dir := filepath.Join("prebuilts", "sdk", ver.String(), kind.String())
Paul Duffin50061512020-01-21 16:31:05 +0000629 jar := filepath.Join(dir, baseName+".jar")
Sundong Ahn054b19a2018-10-19 13:46:09 +0900630 jarPath := android.ExistentPathForSource(ctx, jar)
Sundong Ahnae418ac2019-02-28 15:01:28 +0900631 if !jarPath.Valid() {
Colin Cross07c88562020-01-07 09:34:44 -0800632 if ctx.Config().AllowMissingDependencies() {
633 return android.Paths{android.PathForSource(ctx, jar)}
634 } else {
Jiyong Park6a927c42020-01-21 02:03:43 +0900635 ctx.PropertyErrorf("sdk_library", "invalid sdk version %q, %q does not exist", s.raw, jar)
Colin Cross07c88562020-01-07 09:34:44 -0800636 }
Sundong Ahnae418ac2019-02-28 15:01:28 +0900637 return nil
638 }
Sundong Ahn054b19a2018-10-19 13:46:09 +0900639 return android.Paths{jarPath.Path()}
640}
641
Paul Duffind1b3a922020-01-22 11:57:20 +0000642func (module *SdkLibrary) sdkJars(
643 ctx android.BaseModuleContext,
644 sdkVersion sdkSpec,
645 headerJars bool) android.Paths {
646
Paul Duffin50061512020-01-21 16:31:05 +0000647 // If a specific numeric version has been requested then use prebuilt versions of the sdk.
648 if sdkVersion.version.isNumbered() {
649 return PrebuiltJars(ctx, module.BaseModuleName(), sdkVersion)
Sundong Ahn054b19a2018-10-19 13:46:09 +0900650 } else {
Paul Duffind1b3a922020-01-22 11:57:20 +0000651 if !sdkVersion.specified() {
652 if headerJars {
653 return module.Library.HeaderJars()
654 } else {
655 return module.Library.ImplementationJars()
656 }
657 }
Paul Duffin726d23c2020-01-22 16:30:37 +0000658 var apiScope *apiScope
Jiyong Park6a927c42020-01-21 02:03:43 +0900659 switch sdkVersion.kind {
660 case sdkSystem:
Paul Duffin726d23c2020-01-22 16:30:37 +0000661 apiScope = apiScopeSystem
662 case sdkTest:
663 apiScope = apiScopeTest
Jiyong Park6a927c42020-01-21 02:03:43 +0900664 case sdkPrivate:
Sundong Ahn054b19a2018-10-19 13:46:09 +0900665 return module.Library.HeaderJars()
Jiyong Park6a927c42020-01-21 02:03:43 +0900666 default:
Paul Duffin726d23c2020-01-22 16:30:37 +0000667 apiScope = apiScopePublic
Paul Duffind1b3a922020-01-22 11:57:20 +0000668 }
669
Paul Duffin726d23c2020-01-22 16:30:37 +0000670 paths := module.getScopePaths(apiScope)
Paul Duffind1b3a922020-01-22 11:57:20 +0000671 if headerJars {
672 return paths.stubsHeaderPath
673 } else {
674 return paths.stubsImplPath
Sundong Ahn054b19a2018-10-19 13:46:09 +0900675 }
Jiyong Parkc678ad32018-04-10 13:07:10 +0900676 }
677}
678
Sundong Ahn241cd372018-07-13 16:16:44 +0900679// to satisfy SdkLibraryDependency interface
Paul Duffind1b3a922020-01-22 11:57:20 +0000680func (module *SdkLibrary) SdkHeaderJars(ctx android.BaseModuleContext, sdkVersion sdkSpec) android.Paths {
681 return module.sdkJars(ctx, sdkVersion, true /*headerJars*/)
682}
683
684// to satisfy SdkLibraryDependency interface
Jiyong Park6a927c42020-01-21 02:03:43 +0900685func (module *SdkLibrary) SdkImplementationJars(ctx android.BaseModuleContext, sdkVersion sdkSpec) android.Paths {
Paul Duffind1b3a922020-01-22 11:57:20 +0000686 return module.sdkJars(ctx, sdkVersion, false /*headerJars*/)
Sundong Ahn241cd372018-07-13 16:16:44 +0900687}
688
Sundong Ahn80a87b32019-05-13 15:02:50 +0900689func (module *SdkLibrary) SetNoDist() {
690 module.sdkLibraryProperties.No_dist = proptools.BoolPtr(true)
691}
692
Colin Cross571cccf2019-02-04 11:22:08 -0800693var javaSdkLibrariesKey = android.NewOnceKey("javaSdkLibraries")
694
Jiyong Park82484c02018-04-23 21:41:26 +0900695func javaSdkLibraries(config android.Config) *[]string {
Colin Cross571cccf2019-02-04 11:22:08 -0800696 return config.Once(javaSdkLibrariesKey, func() interface{} {
Jiyong Park82484c02018-04-23 21:41:26 +0900697 return &[]string{}
698 }).(*[]string)
699}
700
Paul Duffin749f98f2019-12-30 17:23:46 +0000701func (module *SdkLibrary) getApiDir() string {
702 return proptools.StringDefault(module.sdkLibraryProperties.Api_dir, "api")
703}
704
Jiyong Parkc678ad32018-04-10 13:07:10 +0900705// For a java_sdk_library module, create internal modules for stubs, docs,
706// runtime libs and xml file. If requested, the stubs and docs are created twice
707// once for public API level and once for system API level
Colin Crossf8b860a2019-04-16 14:43:28 -0700708func (module *SdkLibrary) CreateInternalModules(mctx android.LoadHookContext) {
Inseob Kim6e93ac92019-03-21 17:43:49 +0900709 if len(module.Library.Module.properties.Srcs) == 0 {
Inseob Kimc0907f12019-02-08 21:00:45 +0900710 mctx.PropertyErrorf("srcs", "java_sdk_library must specify srcs")
Jooyung Han58f26ab2019-12-18 15:34:32 +0900711 return
Inseob Kimc0907f12019-02-08 21:00:45 +0900712 }
713
Paul Duffin37e0b772019-12-30 17:20:10 +0000714 // If this builds against standard libraries (i.e. is not part of the core libraries)
715 // then assume it provides both system and test apis. Otherwise, assume it does not and
716 // also assume it does not contribute to the dist build.
717 sdkDep := decodeSdkDep(mctx, sdkContext(&module.Library))
718 hasSystemAndTestApis := sdkDep.hasStandardLibs()
719 module.sdkLibraryProperties.Has_system_and_test_apis = hasSystemAndTestApis
720 module.sdkLibraryProperties.No_dist = proptools.BoolPtr(!hasSystemAndTestApis)
721
Inseob Kim8098faa2019-03-18 10:19:51 +0900722 missing_current_api := false
723
Paul Duffind1b3a922020-01-22 11:57:20 +0000724 activeScopes := module.getActiveApiScopes()
725
Paul Duffin749f98f2019-12-30 17:23:46 +0000726 apiDir := module.getApiDir()
Paul Duffind1b3a922020-01-22 11:57:20 +0000727 for _, scope := range activeScopes {
Inseob Kim8098faa2019-03-18 10:19:51 +0900728 for _, api := range []string{"current.txt", "removed.txt"} {
Paul Duffind1b3a922020-01-22 11:57:20 +0000729 path := path.Join(mctx.ModuleDir(), apiDir, scope.apiFilePrefix+api)
Inseob Kim8098faa2019-03-18 10:19:51 +0900730 p := android.ExistentPathForSource(mctx, path)
731 if !p.Valid() {
732 mctx.ModuleErrorf("Current api file %#v doesn't exist", path)
733 missing_current_api = true
734 }
735 }
736 }
737
738 if missing_current_api {
739 script := "build/soong/scripts/gen-java-current-api-files.sh"
740 p := android.ExistentPathForSource(mctx, script)
741
742 if !p.Valid() {
743 panic(fmt.Sprintf("script file %s doesn't exist", script))
744 }
745
746 mctx.ModuleErrorf("One or more current api files are missing. "+
747 "You can update them by:\n"+
Paul Duffin37e0b772019-12-30 17:20:10 +0000748 "%s %q %s && m update-api",
Paul Duffind1b3a922020-01-22 11:57:20 +0000749 script, filepath.Join(mctx.ModuleDir(), apiDir),
750 strings.Join(activeScopes.Strings(func(s *apiScope) string { return s.apiFilePrefix }), " "))
Inseob Kim8098faa2019-03-18 10:19:51 +0900751 return
752 }
753
Paul Duffind1b3a922020-01-22 11:57:20 +0000754 for _, scope := range activeScopes {
755 module.createStubsLibrary(mctx, scope)
756 module.createStubsSources(mctx, scope)
Inseob Kimc0907f12019-02-08 21:00:45 +0900757 }
758
Paul Duffin43db9be2019-12-30 17:35:49 +0000759 if !proptools.Bool(module.sdkLibraryProperties.Api_only) {
760 // for runtime
761 module.createXmlFile(mctx)
762
763 // record java_sdk_library modules so that they are exported to make
764 javaSdkLibraries := javaSdkLibraries(mctx.Config())
765 javaSdkLibrariesLock.Lock()
766 defer javaSdkLibrariesLock.Unlock()
767 *javaSdkLibraries = append(*javaSdkLibraries, module.BaseModuleName())
768 }
Inseob Kimc0907f12019-02-08 21:00:45 +0900769}
770
771func (module *SdkLibrary) InitSdkLibraryProperties() {
Sundong Ahn054b19a2018-10-19 13:46:09 +0900772 module.AddProperties(
773 &module.sdkLibraryProperties,
774 &module.Library.Module.properties,
775 &module.Library.Module.dexpreoptProperties,
776 &module.Library.Module.deviceProperties,
777 &module.Library.Module.protoProperties,
778 )
779
780 module.Library.Module.properties.Installable = proptools.BoolPtr(true)
781 module.Library.Module.deviceProperties.IsSDKLibrary = true
Inseob Kimc0907f12019-02-08 21:00:45 +0900782}
Sundong Ahn054b19a2018-10-19 13:46:09 +0900783
Jaewoong Jung4f158ee2019-07-11 10:05:35 -0700784// java_sdk_library is a special Java library that provides optional platform APIs to apps.
785// In practice, it can be viewed as a combination of several modules: 1) stubs library that clients
786// are linked against to, 2) droiddoc module that internally generates API stubs source files,
787// 3) the real runtime shared library that implements the APIs, and 4) XML file for adding
788// the runtime lib to the classpath at runtime if requested via <uses-library>.
Inseob Kimc0907f12019-02-08 21:00:45 +0900789func SdkLibraryFactory() android.Module {
790 module := &SdkLibrary{}
791 module.InitSdkLibraryProperties()
Jooyung Han58f26ab2019-12-18 15:34:32 +0900792 android.InitApexModule(module)
Sundong Ahn054b19a2018-10-19 13:46:09 +0900793 InitJavaModule(module, android.HostAndDeviceSupported)
Colin Crossf8b860a2019-04-16 14:43:28 -0700794 android.AddLoadHook(module, func(ctx android.LoadHookContext) { module.CreateInternalModules(ctx) })
Jiyong Parkc678ad32018-04-10 13:07:10 +0900795 return module
796}
Colin Cross79c7c262019-04-17 11:11:46 -0700797
798//
799// SDK library prebuilts
800//
801
Paul Duffin56d44902020-01-31 13:36:25 +0000802// Properties associated with each api scope.
803type sdkLibraryScopeProperties struct {
Colin Cross79c7c262019-04-17 11:11:46 -0700804 Jars []string `android:"path"`
805
806 Sdk_version *string
807
Colin Cross79c7c262019-04-17 11:11:46 -0700808 // List of shared java libs that this module has dependencies to
809 Libs []string
Colin Cross79c7c262019-04-17 11:11:46 -0700810}
811
Paul Duffin56d44902020-01-31 13:36:25 +0000812type sdkLibraryImportProperties struct {
Paul Duffinfcfd7912020-01-31 17:54:30 +0000813 // List of shared java libs, common to all scopes, that this module has
814 // dependencies to
815 Libs []string
Paul Duffin56d44902020-01-31 13:36:25 +0000816}
817
Colin Cross79c7c262019-04-17 11:11:46 -0700818type sdkLibraryImport struct {
819 android.ModuleBase
820 android.DefaultableModuleBase
821 prebuilt android.Prebuilt
Paul Duffin61871622020-02-10 13:37:10 +0000822 android.ApexModuleBase
823 android.SdkBase
Colin Cross79c7c262019-04-17 11:11:46 -0700824
825 properties sdkLibraryImportProperties
826
Paul Duffin6a2bd112020-04-07 19:27:04 +0100827 // Map from api scope to the scope specific property structure.
828 scopeProperties map[*apiScope]*sdkLibraryScopeProperties
829
Paul Duffin56d44902020-01-31 13:36:25 +0000830 commonToSdkLibraryAndImport
Colin Cross79c7c262019-04-17 11:11:46 -0700831}
832
833var _ SdkLibraryDependency = (*sdkLibraryImport)(nil)
834
Paul Duffin6a2bd112020-04-07 19:27:04 +0100835// The type of a structure that contains a field of type sdkLibraryScopeProperties
836// for each apiscope in allApiScopes, e.g. something like:
837// struct {
838// Public sdkLibraryScopeProperties
839// System sdkLibraryScopeProperties
840// ...
841// }
842var allScopeStructType = createAllScopePropertiesStructType()
843
844// Dynamically create a structure type for each apiscope in allApiScopes.
845func createAllScopePropertiesStructType() reflect.Type {
846 var fields []reflect.StructField
847 for _, apiScope := range allApiScopes {
848 field := reflect.StructField{
849 Name: apiScope.fieldName,
850 Type: reflect.TypeOf(sdkLibraryScopeProperties{}),
851 }
852 fields = append(fields, field)
853 }
854
855 return reflect.StructOf(fields)
856}
857
858// Create an instance of the scope specific structure type and return a map
859// from apiscope to a pointer to each scope specific field.
860func createPropertiesInstance() (interface{}, map[*apiScope]*sdkLibraryScopeProperties) {
861 allScopePropertiesPtr := reflect.New(allScopeStructType)
862 allScopePropertiesStruct := allScopePropertiesPtr.Elem()
863 scopeProperties := make(map[*apiScope]*sdkLibraryScopeProperties)
864
865 for _, apiScope := range allApiScopes {
866 field := allScopePropertiesStruct.FieldByName(apiScope.fieldName)
867 scopeProperties[apiScope] = field.Addr().Interface().(*sdkLibraryScopeProperties)
868 }
869
870 return allScopePropertiesPtr.Interface(), scopeProperties
871}
872
Jaewoong Jung4f158ee2019-07-11 10:05:35 -0700873// java_sdk_library_import imports a prebuilt java_sdk_library.
Colin Cross79c7c262019-04-17 11:11:46 -0700874func sdkLibraryImportFactory() android.Module {
875 module := &sdkLibraryImport{}
876
Paul Duffin6a2bd112020-04-07 19:27:04 +0100877 allScopeProperties, scopeToProperties := createPropertiesInstance()
878 module.scopeProperties = scopeToProperties
879 module.AddProperties(&module.properties, allScopeProperties)
Colin Cross79c7c262019-04-17 11:11:46 -0700880
Paul Duffin0bdcb272020-02-06 15:24:57 +0000881 android.InitPrebuiltModule(module, &[]string{""})
Paul Duffin61871622020-02-10 13:37:10 +0000882 android.InitApexModule(module)
883 android.InitSdkAwareModule(module)
Colin Cross79c7c262019-04-17 11:11:46 -0700884 InitJavaModule(module, android.HostAndDeviceSupported)
885
886 android.AddLoadHook(module, func(mctx android.LoadHookContext) { module.createInternalModules(mctx) })
887 return module
888}
889
890func (module *sdkLibraryImport) Prebuilt() *android.Prebuilt {
891 return &module.prebuilt
892}
893
894func (module *sdkLibraryImport) Name() string {
895 return module.prebuilt.Name(module.ModuleBase.Name())
896}
897
898func (module *sdkLibraryImport) createInternalModules(mctx android.LoadHookContext) {
Colin Cross79c7c262019-04-17 11:11:46 -0700899
Paul Duffin50061512020-01-21 16:31:05 +0000900 // If the build is configured to use prebuilts then force this to be preferred.
901 if mctx.Config().UnbundledBuildUsePrebuiltSdks() {
902 module.prebuilt.ForcePrefer()
903 }
904
Paul Duffin6a2bd112020-04-07 19:27:04 +0100905 for apiScope, scopeProperties := range module.scopeProperties {
Paul Duffin56d44902020-01-31 13:36:25 +0000906 if len(scopeProperties.Jars) == 0 {
907 continue
908 }
909
Paul Duffinf6155722020-04-09 00:07:11 +0100910 module.createJavaImportForStubs(mctx, apiScope, scopeProperties)
Paul Duffin56d44902020-01-31 13:36:25 +0000911 }
Colin Cross79c7c262019-04-17 11:11:46 -0700912
913 javaSdkLibraries := javaSdkLibraries(mctx.Config())
914 javaSdkLibrariesLock.Lock()
915 defer javaSdkLibrariesLock.Unlock()
916 *javaSdkLibraries = append(*javaSdkLibraries, module.BaseModuleName())
917}
918
Paul Duffinf6155722020-04-09 00:07:11 +0100919func (module *sdkLibraryImport) createJavaImportForStubs(mctx android.LoadHookContext, apiScope *apiScope, scopeProperties *sdkLibraryScopeProperties) {
920 // Creates a java import for the jar with ".stubs" suffix
921 props := struct {
922 Name *string
923 Soc_specific *bool
924 Device_specific *bool
925 Product_specific *bool
926 System_ext_specific *bool
927 Sdk_version *string
928 Libs []string
929 Jars []string
930 Prefer *bool
931 }{}
932 props.Name = proptools.StringPtr(apiScope.stubsModuleName(module.BaseModuleName()))
933 props.Sdk_version = scopeProperties.Sdk_version
934 // Prepend any of the libs from the legacy public properties to the libs for each of the
935 // scopes to avoid having to duplicate them in each scope.
936 props.Libs = append(module.properties.Libs, scopeProperties.Libs...)
937 props.Jars = scopeProperties.Jars
938 if module.SocSpecific() {
939 props.Soc_specific = proptools.BoolPtr(true)
940 } else if module.DeviceSpecific() {
941 props.Device_specific = proptools.BoolPtr(true)
942 } else if module.ProductSpecific() {
943 props.Product_specific = proptools.BoolPtr(true)
944 } else if module.SystemExtSpecific() {
945 props.System_ext_specific = proptools.BoolPtr(true)
946 }
947 // If the build should use prebuilt sdks then set prefer to true on the stubs library.
948 // That will cause the prebuilt version of the stubs to override the source version.
949 if mctx.Config().UnbundledBuildUsePrebuiltSdks() {
950 props.Prefer = proptools.BoolPtr(true)
951 }
952 mctx.CreateModule(ImportFactory, &props)
953}
954
Colin Cross79c7c262019-04-17 11:11:46 -0700955func (module *sdkLibraryImport) DepsMutator(ctx android.BottomUpMutatorContext) {
Paul Duffin6a2bd112020-04-07 19:27:04 +0100956 for apiScope, scopeProperties := range module.scopeProperties {
Paul Duffin56d44902020-01-31 13:36:25 +0000957 if len(scopeProperties.Jars) == 0 {
958 continue
959 }
960
961 // Add dependencies to the prebuilt stubs library
962 ctx.AddVariationDependencies(nil, apiScope.stubsTag, apiScope.stubsModuleName(module.BaseModuleName()))
963 }
Colin Cross79c7c262019-04-17 11:11:46 -0700964}
965
966func (module *sdkLibraryImport) GenerateAndroidBuildActions(ctx android.ModuleContext) {
967 // Record the paths to the prebuilt stubs library.
968 ctx.VisitDirectDeps(func(to android.Module) {
969 tag := ctx.OtherModuleDependencyTag(to)
970
Paul Duffin56d44902020-01-31 13:36:25 +0000971 if lib, ok := to.(Dependency); ok {
972 if scopeTag, ok := tag.(scopeDependencyTag); ok {
973 apiScope := scopeTag.apiScope
974 scopePaths := module.getScopePaths(apiScope)
975 scopePaths.stubsHeaderPath = lib.HeaderJars()
976 }
Colin Cross79c7c262019-04-17 11:11:46 -0700977 }
978 })
979}
980
Paul Duffin56d44902020-01-31 13:36:25 +0000981func (module *sdkLibraryImport) sdkJars(
982 ctx android.BaseModuleContext,
983 sdkVersion sdkSpec) android.Paths {
984
Paul Duffin50061512020-01-21 16:31:05 +0000985 // If a specific numeric version has been requested then use prebuilt versions of the sdk.
986 if sdkVersion.version.isNumbered() {
987 return PrebuiltJars(ctx, module.BaseModuleName(), sdkVersion)
988 }
989
Paul Duffin56d44902020-01-31 13:36:25 +0000990 var apiScope *apiScope
991 switch sdkVersion.kind {
992 case sdkSystem:
993 apiScope = apiScopeSystem
994 case sdkTest:
995 apiScope = apiScopeTest
996 default:
997 apiScope = apiScopePublic
998 }
999
1000 paths := module.getScopePaths(apiScope)
1001 return paths.stubsHeaderPath
1002}
1003
Colin Cross79c7c262019-04-17 11:11:46 -07001004// to satisfy SdkLibraryDependency interface
Jiyong Park6a927c42020-01-21 02:03:43 +09001005func (module *sdkLibraryImport) SdkHeaderJars(ctx android.BaseModuleContext, sdkVersion sdkSpec) android.Paths {
Colin Cross79c7c262019-04-17 11:11:46 -07001006 // This module is just a wrapper for the prebuilt stubs.
Paul Duffin56d44902020-01-31 13:36:25 +00001007 return module.sdkJars(ctx, sdkVersion)
Colin Cross79c7c262019-04-17 11:11:46 -07001008}
1009
1010// to satisfy SdkLibraryDependency interface
Jiyong Park6a927c42020-01-21 02:03:43 +09001011func (module *sdkLibraryImport) SdkImplementationJars(ctx android.BaseModuleContext, sdkVersion sdkSpec) android.Paths {
Colin Cross79c7c262019-04-17 11:11:46 -07001012 // This module is just a wrapper for the stubs.
Paul Duffin56d44902020-01-31 13:36:25 +00001013 return module.sdkJars(ctx, sdkVersion)
Colin Cross79c7c262019-04-17 11:11:46 -07001014}
Jiyong Parke3833882020-02-17 17:28:10 +09001015
1016//
1017// java_sdk_library_xml
1018//
1019type sdkLibraryXml struct {
1020 android.ModuleBase
1021 android.DefaultableModuleBase
1022 android.ApexModuleBase
1023
1024 properties sdkLibraryXmlProperties
1025
1026 outputFilePath android.OutputPath
1027 installDirPath android.InstallPath
1028}
1029
1030type sdkLibraryXmlProperties struct {
1031 // canonical name of the lib
1032 Lib_name *string
1033}
1034
1035// java_sdk_library_xml builds the permission xml file for a java_sdk_library.
1036// Not to be used directly by users. java_sdk_library internally uses this.
1037func sdkLibraryXmlFactory() android.Module {
1038 module := &sdkLibraryXml{}
1039
1040 module.AddProperties(&module.properties)
1041
1042 android.InitApexModule(module)
1043 android.InitAndroidArchModule(module, android.DeviceSupported, android.MultilibCommon)
1044
1045 return module
1046}
1047
1048// from android.PrebuiltEtcModule
1049func (module *sdkLibraryXml) SubDir() string {
1050 return "permissions"
1051}
1052
1053// from android.PrebuiltEtcModule
1054func (module *sdkLibraryXml) OutputFile() android.OutputPath {
1055 return module.outputFilePath
1056}
1057
1058// from android.ApexModule
1059func (module *sdkLibraryXml) AvailableFor(what string) bool {
1060 return true
1061}
1062
1063func (module *sdkLibraryXml) DepsMutator(ctx android.BottomUpMutatorContext) {
1064 // do nothing
1065}
1066
1067// File path to the runtime implementation library
1068func (module *sdkLibraryXml) implPath() string {
1069 implName := proptools.String(module.properties.Lib_name)
1070 if apexName := module.ApexName(); apexName != "" {
1071 // TODO(b/146468504): ApexName() is only a soong module name, not apex name.
1072 // In most cases, this works fine. But when apex_name is set or override_apex is used
1073 // this can be wrong.
1074 return fmt.Sprintf("/apex/%s/javalib/%s.jar", apexName, implName)
1075 }
1076 partition := "system"
1077 if module.SocSpecific() {
1078 partition = "vendor"
1079 } else if module.DeviceSpecific() {
1080 partition = "odm"
1081 } else if module.ProductSpecific() {
1082 partition = "product"
1083 } else if module.SystemExtSpecific() {
1084 partition = "system_ext"
1085 }
1086 return "/" + partition + "/framework/" + implName + ".jar"
1087}
1088
1089func (module *sdkLibraryXml) GenerateAndroidBuildActions(ctx android.ModuleContext) {
1090 libName := proptools.String(module.properties.Lib_name)
1091 xmlContent := fmt.Sprintf(permissionsTemplate, libName, module.implPath())
1092
1093 module.outputFilePath = android.PathForModuleOut(ctx, libName+".xml").OutputPath
1094 rule := android.NewRuleBuilder()
1095 rule.Command().
1096 Text("/bin/bash -c \"echo -e '" + xmlContent + "'\" > ").
1097 Output(module.outputFilePath)
1098
1099 rule.Build(pctx, ctx, "java_sdk_xml", "Permission XML")
1100
1101 module.installDirPath = android.PathForModuleInstall(ctx, "etc", module.SubDir())
1102}
1103
1104func (module *sdkLibraryXml) AndroidMkEntries() []android.AndroidMkEntries {
1105 if !module.IsForPlatform() {
1106 return []android.AndroidMkEntries{android.AndroidMkEntries{
1107 Disabled: true,
1108 }}
1109 }
1110
1111 return []android.AndroidMkEntries{android.AndroidMkEntries{
1112 Class: "ETC",
1113 OutputFile: android.OptionalPathForPath(module.outputFilePath),
1114 ExtraEntries: []android.AndroidMkExtraEntriesFunc{
1115 func(entries *android.AndroidMkEntries) {
1116 entries.SetString("LOCAL_MODULE_TAGS", "optional")
1117 entries.SetString("LOCAL_MODULE_PATH", module.installDirPath.ToMakePath().String())
1118 entries.SetString("LOCAL_INSTALLED_MODULE_STEM", module.outputFilePath.Base())
1119 },
1120 },
1121 }}
1122}
Paul Duffin61871622020-02-10 13:37:10 +00001123
1124type sdkLibrarySdkMemberType struct {
1125 android.SdkMemberTypeBase
1126}
1127
1128func (s *sdkLibrarySdkMemberType) AddDependencies(mctx android.BottomUpMutatorContext, dependencyTag blueprint.DependencyTag, names []string) {
1129 mctx.AddVariationDependencies(nil, dependencyTag, names...)
1130}
1131
1132func (s *sdkLibrarySdkMemberType) IsInstance(module android.Module) bool {
1133 _, ok := module.(*SdkLibrary)
1134 return ok
1135}
1136
1137func (s *sdkLibrarySdkMemberType) AddPrebuiltModule(ctx android.SdkMemberContext, member android.SdkMember) android.BpModule {
1138 return ctx.SnapshotBuilder().AddPrebuiltModule(member, "java_sdk_library_import")
1139}
1140
1141func (s *sdkLibrarySdkMemberType) CreateVariantPropertiesStruct() android.SdkMemberProperties {
1142 return &sdkLibrarySdkMemberProperties{}
1143}
1144
1145type sdkLibrarySdkMemberProperties struct {
1146 android.SdkMemberPropertiesBase
1147
1148 // Scope to per scope properties.
1149 Scopes map[*apiScope]scopeProperties
1150
1151 // Additional libraries that the exported stubs libraries depend upon.
1152 Libs []string
1153}
1154
1155type scopeProperties struct {
1156 Jars android.Paths
1157 SdkVersion string
1158}
1159
1160func (s *sdkLibrarySdkMemberProperties) PopulateFromVariant(ctx android.SdkMemberContext, variant android.Module) {
1161 sdk := variant.(*SdkLibrary)
1162
1163 s.Scopes = make(map[*apiScope]scopeProperties)
1164 for _, apiScope := range allApiScopes {
1165 paths := sdk.getScopePaths(apiScope)
1166 jars := paths.stubsImplPath
1167 if len(jars) > 0 {
1168 properties := scopeProperties{}
1169 properties.Jars = jars
1170 properties.SdkVersion = apiScope.sdkVersion
1171 s.Scopes[apiScope] = properties
1172 }
1173 }
1174
1175 s.Libs = sdk.properties.Libs
1176}
1177
1178func (s *sdkLibrarySdkMemberProperties) AddToPropertySet(ctx android.SdkMemberContext, propertySet android.BpPropertySet) {
1179 for _, apiScope := range allApiScopes {
1180 if properties, ok := s.Scopes[apiScope]; ok {
1181 scopeSet := propertySet.AddPropertySet(apiScope.name)
1182
1183 var jars []string
1184 for _, p := range properties.Jars {
1185 dest := filepath.Join("sdk_library", s.OsPrefix(), apiScope.name, ctx.Name()+"-stubs.jar")
1186 ctx.SnapshotBuilder().CopyToSnapshot(p, dest)
1187 jars = append(jars, dest)
1188 }
1189 scopeSet.AddProperty("jars", jars)
1190
1191 if properties.SdkVersion != "" {
1192 scopeSet.AddProperty("sdk_version", properties.SdkVersion)
1193 }
1194 }
1195 }
1196
1197 if len(s.Libs) > 0 {
1198 propertySet.AddPropertyWithTag("libs", s.Libs, ctx.SnapshotBuilder().SdkMemberReferencePropertyTag(false))
1199 }
1200}