blob: 51e680b7ff306704fa21528db264318aa8e52cc0 [file] [log] [blame]
Jiyong Parkc678ad32018-04-10 13:07:10 +09001// Copyright 2018 Google Inc. All rights reserved.
2//
3// Licensed under the Apache License, Version 2.0 (the "License");
4// you may not use this file except in compliance with the License.
5// You may obtain a copy of the License at
6//
7// http://www.apache.org/licenses/LICENSE-2.0
8//
9// Unless required by applicable law or agreed to in writing, software
10// distributed under the License is distributed on an "AS IS" BASIS,
11// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12// See the License for the specific language governing permissions and
13// limitations under the License.
14
15package java
16
17import (
Jiyong Parkc678ad32018-04-10 13:07:10 +090018 "fmt"
19 "path"
Sundong Ahn054b19a2018-10-19 13:46:09 +090020 "path/filepath"
Paul Duffin46a26a82020-04-07 19:27:04 +010021 "reflect"
Jiyong Park82484c02018-04-23 21:41:26 +090022 "sort"
Jiyong Parkc678ad32018-04-10 13:07:10 +090023 "strings"
Jiyong Park82484c02018-04-23 21:41:26 +090024 "sync"
Jiyong Parkc678ad32018-04-10 13:07:10 +090025
Paul Duffind1b3a922020-01-22 11:57:20 +000026 "github.com/google/blueprint"
Jiyong Parkc678ad32018-04-10 13:07:10 +090027 "github.com/google/blueprint/proptools"
Paul Duffin46a26a82020-04-07 19:27:04 +010028
29 "android/soong/android"
Jiyong Parkc678ad32018-04-10 13:07:10 +090030)
31
Jooyung Han58f26ab2019-12-18 15:34:32 +090032const (
Jiyong Parkc678ad32018-04-10 13:07:10 +090033 sdkStubsLibrarySuffix = ".stubs"
34 sdkSystemApiSuffix = ".system"
Jiyong Parkdf130542018-04-27 16:29:21 +090035 sdkTestApiSuffix = ".test"
Paul Duffin91b883d2020-02-11 13:05:28 +000036 sdkStubsSourceSuffix = ".stubs.source"
Jiyong Parkc678ad32018-04-10 13:07:10 +090037 sdkXmlFileSuffix = ".xml"
Jiyong Parke3833882020-02-17 17:28:10 +090038 permissionsTemplate = `<?xml version=\"1.0\" encoding=\"utf-8\"?>\n` +
Jooyung Han624058e2019-12-24 18:38:06 +090039 `<!-- Copyright (C) 2018 The Android Open Source Project\n` +
40 `\n` +
Jiyong Parke3833882020-02-17 17:28:10 +090041 ` Licensed under the Apache License, Version 2.0 (the \"License\");\n` +
Jooyung Han624058e2019-12-24 18:38:06 +090042 ` you may not use this file except in compliance with the License.\n` +
43 ` You may obtain a copy of the License at\n` +
44 `\n` +
45 ` http://www.apache.org/licenses/LICENSE-2.0\n` +
46 `\n` +
47 ` Unless required by applicable law or agreed to in writing, software\n` +
Jiyong Parke3833882020-02-17 17:28:10 +090048 ` distributed under the License is distributed on an \"AS IS\" BASIS,\n` +
Jooyung Han624058e2019-12-24 18:38:06 +090049 ` WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\n` +
50 ` See the License for the specific language governing permissions and\n` +
51 ` limitations under the License.\n` +
52 `-->\n` +
53 `<permissions>\n` +
Jiyong Parke3833882020-02-17 17:28:10 +090054 ` <library name=\"%s\" file=\"%s\"/>\n` +
Jooyung Han624058e2019-12-24 18:38:06 +090055 `</permissions>\n`
Jiyong Parkc678ad32018-04-10 13:07:10 +090056)
57
Paul Duffind1b3a922020-01-22 11:57:20 +000058// A tag to associated a dependency with a specific api scope.
59type scopeDependencyTag struct {
60 blueprint.BaseDependencyTag
61 name string
62 apiScope *apiScope
63}
64
65// Provides information about an api scope, e.g. public, system, test.
66type apiScope struct {
67 // The name of the api scope, e.g. public, system, test
68 name string
69
Paul Duffin46a26a82020-04-07 19:27:04 +010070 // The name of the field in the dynamically created structure.
71 fieldName string
72
Paul Duffind1b3a922020-01-22 11:57:20 +000073 // The tag to use to depend on the stubs library module.
74 stubsTag scopeDependencyTag
75
76 // The tag to use to depend on the stubs
77 apiFileTag scopeDependencyTag
78
79 // The scope specific prefix to add to the api file base of "current.txt" or "removed.txt".
80 apiFilePrefix string
81
82 // The scope specific prefix to add to the sdk library module name to construct a scope specific
83 // module name.
84 moduleSuffix string
85
86 // The suffix to add to the make variable that references the location of the api file.
87 apiFileMakeVariableSuffix string
88
89 // SDK version that the stubs library is built against. Note that this is always
90 // *current. Older stubs library built with a numbered SDK version is created from
91 // the prebuilt jar.
92 sdkVersion string
Paul Duffin1fb487d2020-04-07 18:50:10 +010093
94 // Extra arguments to pass to droidstubs for this scope.
95 droidstubsArgs []string
Paul Duffind1b3a922020-01-22 11:57:20 +000096}
97
98// Initialize a scope, creating and adding appropriate dependency tags
99func initApiScope(scope *apiScope) *apiScope {
Paul Duffin46a26a82020-04-07 19:27:04 +0100100 scope.fieldName = proptools.FieldNameForProperty(scope.name)
Paul Duffind1b3a922020-01-22 11:57:20 +0000101 scope.stubsTag = scopeDependencyTag{
102 name: scope.name + "-stubs",
103 apiScope: scope,
104 }
105 scope.apiFileTag = scopeDependencyTag{
106 name: scope.name + "-api",
107 apiScope: scope,
108 }
109 return scope
110}
111
112func (scope *apiScope) stubsModuleName(baseName string) string {
113 return baseName + sdkStubsLibrarySuffix + scope.moduleSuffix
114}
115
116func (scope *apiScope) docsModuleName(baseName string) string {
Paul Duffin91b883d2020-02-11 13:05:28 +0000117 return baseName + sdkStubsSourceSuffix + scope.moduleSuffix
Paul Duffind1b3a922020-01-22 11:57:20 +0000118}
119
120type apiScopes []*apiScope
121
122func (scopes apiScopes) Strings(accessor func(*apiScope) string) []string {
123 var list []string
124 for _, scope := range scopes {
125 list = append(list, accessor(scope))
126 }
127 return list
128}
129
Jiyong Parkc678ad32018-04-10 13:07:10 +0900130var (
Paul Duffind1b3a922020-01-22 11:57:20 +0000131 apiScopePublic = initApiScope(&apiScope{
132 name: "public",
133 sdkVersion: "current",
134 })
135 apiScopeSystem = initApiScope(&apiScope{
136 name: "system",
137 apiFilePrefix: "system-",
138 moduleSuffix: sdkSystemApiSuffix,
139 apiFileMakeVariableSuffix: "_SYSTEM",
140 sdkVersion: "system_current",
Paul Duffin1fb487d2020-04-07 18:50:10 +0100141 droidstubsArgs: []string{"-showAnnotation android.annotation.SystemApi"},
Paul Duffind1b3a922020-01-22 11:57:20 +0000142 })
143 apiScopeTest = initApiScope(&apiScope{
144 name: "test",
145 apiFilePrefix: "test-",
146 moduleSuffix: sdkTestApiSuffix,
147 apiFileMakeVariableSuffix: "_TEST",
148 sdkVersion: "test_current",
Paul Duffin1fb487d2020-04-07 18:50:10 +0100149 droidstubsArgs: []string{"-showAnnotation android.annotation.TestApi"},
Paul Duffind1b3a922020-01-22 11:57:20 +0000150 })
151 allApiScopes = apiScopes{
152 apiScopePublic,
153 apiScopeSystem,
154 apiScopeTest,
155 }
Jiyong Parkc678ad32018-04-10 13:07:10 +0900156)
157
Jiyong Park82484c02018-04-23 21:41:26 +0900158var (
159 javaSdkLibrariesLock sync.Mutex
160)
161
Jiyong Parkc678ad32018-04-10 13:07:10 +0900162// TODO: these are big features that are currently missing
Jiyong Park1be96912018-05-28 18:02:19 +0900163// 1) disallowing linking to the runtime shared lib
164// 2) HTML generation
Jiyong Parkc678ad32018-04-10 13:07:10 +0900165
166func init() {
Paul Duffin43dc1cc2019-12-19 11:18:54 +0000167 RegisterSdkLibraryBuildComponents(android.InitRegistrationContext)
Jiyong Parkc678ad32018-04-10 13:07:10 +0900168
Jiyong Park82484c02018-04-23 21:41:26 +0900169 android.RegisterMakeVarsProvider(pctx, func(ctx android.MakeVarsContext) {
170 javaSdkLibraries := javaSdkLibraries(ctx.Config())
171 sort.Strings(*javaSdkLibraries)
172 ctx.Strict("JAVA_SDK_LIBRARIES", strings.Join(*javaSdkLibraries, " "))
173 })
Paul Duffindd46f712020-02-10 13:37:10 +0000174
175 // Register sdk member types.
176 android.RegisterSdkMemberType(&sdkLibrarySdkMemberType{
177 android.SdkMemberTypeBase{
178 PropertyName: "java_sdk_libs",
179 SupportsSdk: true,
180 },
181 })
Jiyong Parkc678ad32018-04-10 13:07:10 +0900182}
183
Paul Duffin43dc1cc2019-12-19 11:18:54 +0000184func RegisterSdkLibraryBuildComponents(ctx android.RegistrationContext) {
185 ctx.RegisterModuleType("java_sdk_library", SdkLibraryFactory)
186 ctx.RegisterModuleType("java_sdk_library_import", sdkLibraryImportFactory)
187}
188
Jiyong Parkc678ad32018-04-10 13:07:10 +0900189type sdkLibraryProperties struct {
Sundong Ahnf043cf62018-06-25 16:04:37 +0900190 // List of Java libraries that will be in the classpath when building stubs
191 Stub_only_libs []string `android:"arch_variant"`
192
Paul Duffin7a586d32019-12-30 17:09:34 +0000193 // list of package names that will be documented and publicized as API.
194 // This allows the API to be restricted to a subset of the source files provided.
195 // If this is unspecified then all the source files will be treated as being part
196 // of the API.
Jiyong Parkc678ad32018-04-10 13:07:10 +0900197 Api_packages []string
198
Jiyong Park5a2c9d72018-05-01 22:25:41 +0900199 // list of package names that must be hidden from the API
200 Hidden_api_packages []string
201
Paul Duffin749f98f2019-12-30 17:23:46 +0000202 // the relative path to the directory containing the api specification files.
203 // Defaults to "api".
204 Api_dir *string
205
Paul Duffin43db9be2019-12-30 17:35:49 +0000206 // If set to true there is no runtime library.
207 Api_only *bool
208
Paul Duffin11512472019-02-11 15:55:17 +0000209 // local files that are used within user customized droiddoc options.
210 Droiddoc_option_files []string
211
212 // additional droiddoc options
213 // Available variables for substitution:
214 //
215 // $(location <label>): the path to the droiddoc_option_files with name <label>
Sundong Ahndd567f92018-07-31 17:19:11 +0900216 Droiddoc_options []string
217
Sundong Ahn054b19a2018-10-19 13:46:09 +0900218 // a list of top-level directories containing files to merge qualifier annotations
219 // (i.e. those intended to be included in the stubs written) from.
220 Merge_annotations_dirs []string
221
222 // a list of top-level directories containing Java stub files to merge show/hide annotations from.
223 Merge_inclusion_annotations_dirs []string
224
225 // If set to true, the path of dist files is apistubs/core. Defaults to false.
226 Core_lib *bool
227
Sundong Ahn80a87b32019-05-13 15:02:50 +0900228 // don't create dist rules.
229 No_dist *bool `blueprint:"mutated"`
230
Paul Duffin37e0b772019-12-30 17:20:10 +0000231 // indicates whether system and test apis should be managed.
232 Has_system_and_test_apis bool `blueprint:"mutated"`
233
Jiyong Parkc678ad32018-04-10 13:07:10 +0900234 // TODO: determines whether to create HTML doc or not
235 //Html_doc *bool
236}
237
Paul Duffind1b3a922020-01-22 11:57:20 +0000238type scopePaths struct {
Paul Duffin1fd005d2020-04-09 01:08:11 +0100239 stubsHeaderPath android.Paths
240 stubsImplPath android.Paths
241 currentApiFilePath android.Path
242 removedApiFilePath android.Path
243 stubsSrcJar android.Path
Paul Duffind1b3a922020-01-22 11:57:20 +0000244}
245
Paul Duffin56d44902020-01-31 13:36:25 +0000246// Common code between sdk library and sdk library import
247type commonToSdkLibraryAndImport struct {
248 scopePaths map[*apiScope]*scopePaths
249}
250
251func (c *commonToSdkLibraryAndImport) getScopePaths(scope *apiScope) *scopePaths {
252 if c.scopePaths == nil {
253 c.scopePaths = make(map[*apiScope]*scopePaths)
254 }
255 paths := c.scopePaths[scope]
256 if paths == nil {
257 paths = &scopePaths{}
258 c.scopePaths[scope] = paths
259 }
260
261 return paths
262}
263
Inseob Kimc0907f12019-02-08 21:00:45 +0900264type SdkLibrary struct {
Sundong Ahn054b19a2018-10-19 13:46:09 +0900265 Library
Jiyong Parkc678ad32018-04-10 13:07:10 +0900266
Sundong Ahn054b19a2018-10-19 13:46:09 +0900267 sdkLibraryProperties sdkLibraryProperties
Jiyong Parkc678ad32018-04-10 13:07:10 +0900268
Paul Duffin56d44902020-01-31 13:36:25 +0000269 commonToSdkLibraryAndImport
Jiyong Parkc678ad32018-04-10 13:07:10 +0900270}
271
Inseob Kimc0907f12019-02-08 21:00:45 +0900272var _ Dependency = (*SdkLibrary)(nil)
273var _ SdkLibraryDependency = (*SdkLibrary)(nil)
Colin Cross897d2ed2019-02-11 14:03:51 -0800274
Paul Duffind1b3a922020-01-22 11:57:20 +0000275func (module *SdkLibrary) getActiveApiScopes() apiScopes {
276 if module.sdkLibraryProperties.Has_system_and_test_apis {
277 return allApiScopes
278 } else {
279 return apiScopes{apiScopePublic}
280 }
281}
282
Paul Duffine74ac732020-02-06 13:51:46 +0000283var xmlPermissionsFileTag = dependencyTag{name: "xml-permissions-file"}
284
Jiyong Parke3833882020-02-17 17:28:10 +0900285func IsXmlPermissionsFileDepTag(depTag blueprint.DependencyTag) bool {
286 if dt, ok := depTag.(dependencyTag); ok {
287 return dt == xmlPermissionsFileTag
288 }
289 return false
290}
291
Inseob Kimc0907f12019-02-08 21:00:45 +0900292func (module *SdkLibrary) DepsMutator(ctx android.BottomUpMutatorContext) {
Paul Duffind1b3a922020-01-22 11:57:20 +0000293 for _, apiScope := range module.getActiveApiScopes() {
294 // Add dependencies to the stubs library
Paul Duffin50061512020-01-21 16:31:05 +0000295 ctx.AddVariationDependencies(nil, apiScope.stubsTag, module.stubsName(apiScope))
Paul Duffind1b3a922020-01-22 11:57:20 +0000296
Paul Duffin50061512020-01-21 16:31:05 +0000297 // And the api file
Paul Duffind1b3a922020-01-22 11:57:20 +0000298 ctx.AddVariationDependencies(nil, apiScope.apiFileTag, module.docsName(apiScope))
Sundong Ahn054b19a2018-10-19 13:46:09 +0900299 }
300
Paul Duffine74ac732020-02-06 13:51:46 +0000301 if !proptools.Bool(module.sdkLibraryProperties.Api_only) {
302 // Add dependency to the rule for generating the xml permissions file
Jiyong Parke3833882020-02-17 17:28:10 +0900303 ctx.AddDependency(module, xmlPermissionsFileTag, module.xmlFileName())
Paul Duffine74ac732020-02-06 13:51:46 +0000304 }
305
Sundong Ahn054b19a2018-10-19 13:46:09 +0900306 module.Library.deps(ctx)
Jiyong Parkc678ad32018-04-10 13:07:10 +0900307}
308
Inseob Kimc0907f12019-02-08 21:00:45 +0900309func (module *SdkLibrary) GenerateAndroidBuildActions(ctx android.ModuleContext) {
Paul Duffin43db9be2019-12-30 17:35:49 +0000310 // Don't build an implementation library if this is api only.
311 if !proptools.Bool(module.sdkLibraryProperties.Api_only) {
312 module.Library.GenerateAndroidBuildActions(ctx)
313 }
Sundong Ahn054b19a2018-10-19 13:46:09 +0900314
Sundong Ahn57368eb2018-07-06 11:20:23 +0900315 // Record the paths to the header jars of the library (stubs and impl).
Paul Duffind1b3a922020-01-22 11:57:20 +0000316 // When this java_sdk_library is depended upon from others via "libs" property,
Jiyong Parkc678ad32018-04-10 13:07:10 +0900317 // the recorded paths will be returned depending on the link type of the caller.
318 ctx.VisitDirectDeps(func(to android.Module) {
319 otherName := ctx.OtherModuleName(to)
320 tag := ctx.OtherModuleDependencyTag(to)
321
Sundong Ahn57368eb2018-07-06 11:20:23 +0900322 if lib, ok := to.(Dependency); ok {
Paul Duffind1b3a922020-01-22 11:57:20 +0000323 if scopeTag, ok := tag.(scopeDependencyTag); ok {
324 apiScope := scopeTag.apiScope
325 scopePaths := module.getScopePaths(apiScope)
326 scopePaths.stubsHeaderPath = lib.HeaderJars()
327 scopePaths.stubsImplPath = lib.ImplementationJars()
Jiyong Parkc678ad32018-04-10 13:07:10 +0900328 }
329 }
Paul Duffin3d1248c2020-04-09 00:10:17 +0100330 if doc, ok := to.(ApiStubsProvider); ok {
Paul Duffind1b3a922020-01-22 11:57:20 +0000331 if scopeTag, ok := tag.(scopeDependencyTag); ok {
332 apiScope := scopeTag.apiScope
333 scopePaths := module.getScopePaths(apiScope)
Paul Duffin1fd005d2020-04-09 01:08:11 +0100334 scopePaths.currentApiFilePath = doc.ApiFilePath()
335 scopePaths.removedApiFilePath = doc.RemovedApiFilePath()
Paul Duffin3d1248c2020-04-09 00:10:17 +0100336 scopePaths.stubsSrcJar = doc.StubsSrcJar()
Paul Duffind1b3a922020-01-22 11:57:20 +0000337 } else {
Sundong Ahn20e998b2018-07-24 11:19:26 +0900338 ctx.ModuleErrorf("depends on module %q of unknown tag %q", otherName, tag)
339 }
340 }
Jiyong Parkc678ad32018-04-10 13:07:10 +0900341 })
342}
343
Jiyong Park0b0e1b92019-12-03 13:24:29 +0900344func (module *SdkLibrary) AndroidMkEntries() []android.AndroidMkEntries {
Paul Duffin43db9be2019-12-30 17:35:49 +0000345 if proptools.Bool(module.sdkLibraryProperties.Api_only) {
346 return nil
347 }
Jiyong Park0b0e1b92019-12-03 13:24:29 +0900348 entriesList := module.Library.AndroidMkEntries()
349 entries := &entriesList[0]
Jaewoong Jungb0c127c2019-08-29 14:56:03 -0700350 entries.Required = append(entries.Required, module.xmlFileName())
Jiyong Park0b0e1b92019-12-03 13:24:29 +0900351 return entriesList
Jiyong Park82484c02018-04-23 21:41:26 +0900352}
353
Jiyong Parkc678ad32018-04-10 13:07:10 +0900354// Module name of the stubs library
Paul Duffind1b3a922020-01-22 11:57:20 +0000355func (module *SdkLibrary) stubsName(apiScope *apiScope) string {
356 return apiScope.stubsModuleName(module.BaseModuleName())
Jiyong Parkc678ad32018-04-10 13:07:10 +0900357}
358
359// Module name of the docs
Paul Duffind1b3a922020-01-22 11:57:20 +0000360func (module *SdkLibrary) docsName(apiScope *apiScope) string {
361 return apiScope.docsModuleName(module.BaseModuleName())
Jiyong Parkc678ad32018-04-10 13:07:10 +0900362}
363
364// Module name of the runtime implementation library
Inseob Kimc0907f12019-02-08 21:00:45 +0900365func (module *SdkLibrary) implName() string {
Sundong Ahn054b19a2018-10-19 13:46:09 +0900366 return module.BaseModuleName()
Jiyong Parkc678ad32018-04-10 13:07:10 +0900367}
368
Jiyong Parkc678ad32018-04-10 13:07:10 +0900369// Module name of the XML file for the lib
Inseob Kimc0907f12019-02-08 21:00:45 +0900370func (module *SdkLibrary) xmlFileName() string {
Jiyong Parkc678ad32018-04-10 13:07:10 +0900371 return module.BaseModuleName() + sdkXmlFileSuffix
372}
373
Anton Hansson5fd5d242020-03-27 19:43:19 +0000374// The dist path of the stub artifacts
375func (module *SdkLibrary) apiDistPath(apiScope *apiScope) string {
376 if module.ModuleBase.Owner() != "" {
377 return path.Join("apistubs", module.ModuleBase.Owner(), apiScope.name)
378 } else if Bool(module.sdkLibraryProperties.Core_lib) {
379 return path.Join("apistubs", "core", apiScope.name)
380 } else {
381 return path.Join("apistubs", "android", apiScope.name)
382 }
383}
384
Paul Duffin12ceb462019-12-24 20:31:31 +0000385// Get the sdk version for use when compiling the stubs library.
Paul Duffind1b3a922020-01-22 11:57:20 +0000386func (module *SdkLibrary) sdkVersionForStubsLibrary(mctx android.LoadHookContext, apiScope *apiScope) string {
Paul Duffin12ceb462019-12-24 20:31:31 +0000387 sdkDep := decodeSdkDep(mctx, sdkContext(&module.Library))
388 if sdkDep.hasStandardLibs() {
389 // If building against a standard sdk then use the sdk version appropriate for the scope.
Paul Duffind1b3a922020-01-22 11:57:20 +0000390 return apiScope.sdkVersion
Paul Duffin12ceb462019-12-24 20:31:31 +0000391 } else {
392 // Otherwise, use no system module.
393 return "none"
394 }
395}
396
Paul Duffind1b3a922020-01-22 11:57:20 +0000397func (module *SdkLibrary) latestApiFilegroupName(apiScope *apiScope) string {
398 return ":" + module.BaseModuleName() + ".api." + apiScope.name + ".latest"
Jiyong Park58c518b2018-05-12 22:29:12 +0900399}
Jiyong Parkc678ad32018-04-10 13:07:10 +0900400
Paul Duffind1b3a922020-01-22 11:57:20 +0000401func (module *SdkLibrary) latestRemovedApiFilegroupName(apiScope *apiScope) string {
402 return ":" + module.BaseModuleName() + "-removed.api." + apiScope.name + ".latest"
Jiyong Parkc678ad32018-04-10 13:07:10 +0900403}
404
405// Creates a static java library that has API stubs
Paul Duffind1b3a922020-01-22 11:57:20 +0000406func (module *SdkLibrary) createStubsLibrary(mctx android.LoadHookContext, apiScope *apiScope) {
Jiyong Parkc678ad32018-04-10 13:07:10 +0900407 props := struct {
Sundong Ahn0d7dff42019-12-04 12:53:44 +0900408 Name *string
409 Srcs []string
Paul Duffin367ab912019-12-23 19:40:36 +0000410 Installable *bool
Sundong Ahn0d7dff42019-12-04 12:53:44 +0900411 Sdk_version *string
Paul Duffin12ceb462019-12-24 20:31:31 +0000412 System_modules *string
Paul Duffinab8da5d2020-02-07 16:12:04 +0000413 Patch_module *string
Sundong Ahn0d7dff42019-12-04 12:53:44 +0900414 Libs []string
415 Soc_specific *bool
416 Device_specific *bool
417 Product_specific *bool
418 System_ext_specific *bool
419 Compile_dex *bool
Sundong Ahn0d7dff42019-12-04 12:53:44 +0900420 Java_version *string
421 Product_variables struct {
Jiyong Park82484c02018-04-23 21:41:26 +0900422 Pdk struct {
423 Enabled *bool
424 }
Jiyong Parkc678ad32018-04-10 13:07:10 +0900425 }
Sundong Ahn054b19a2018-10-19 13:46:09 +0900426 Openjdk9 struct {
427 Srcs []string
428 Javacflags []string
429 }
Anton Hansson5fd5d242020-03-27 19:43:19 +0000430 Dist struct {
431 Targets []string
432 Dest *string
433 Dir *string
434 Tag *string
435 }
Jiyong Parkc678ad32018-04-10 13:07:10 +0900436 }{}
437
Jiyong Parkdf130542018-04-27 16:29:21 +0900438 props.Name = proptools.StringPtr(module.stubsName(apiScope))
Jiyong Parkc678ad32018-04-10 13:07:10 +0900439 // sources are generated from the droiddoc
Jiyong Parkdf130542018-04-27 16:29:21 +0900440 props.Srcs = []string{":" + module.docsName(apiScope)}
Paul Duffin12ceb462019-12-24 20:31:31 +0000441 sdkVersion := module.sdkVersionForStubsLibrary(mctx, apiScope)
Paul Duffin52d398a2019-06-11 12:31:14 +0100442 props.Sdk_version = proptools.StringPtr(sdkVersion)
Paul Duffin12ceb462019-12-24 20:31:31 +0000443 props.System_modules = module.Library.Module.deviceProperties.System_modules
Paul Duffinab8da5d2020-02-07 16:12:04 +0000444 props.Patch_module = module.Library.Module.properties.Patch_module
Paul Duffin367ab912019-12-23 19:40:36 +0000445 props.Installable = proptools.BoolPtr(false)
Sundong Ahn054b19a2018-10-19 13:46:09 +0900446 props.Libs = module.sdkLibraryProperties.Stub_only_libs
Jiyong Park82484c02018-04-23 21:41:26 +0900447 props.Product_variables.Pdk.Enabled = proptools.BoolPtr(false)
Sundong Ahn054b19a2018-10-19 13:46:09 +0900448 props.Openjdk9.Srcs = module.Library.Module.properties.Openjdk9.Srcs
449 props.Openjdk9.Javacflags = module.Library.Module.properties.Openjdk9.Javacflags
450 props.Java_version = module.Library.Module.properties.Java_version
451 if module.Library.Module.deviceProperties.Compile_dex != nil {
452 props.Compile_dex = module.Library.Module.deviceProperties.Compile_dex
Sundong Ahndd567f92018-07-31 17:19:11 +0900453 }
Jiyong Parkc678ad32018-04-10 13:07:10 +0900454
455 if module.SocSpecific() {
456 props.Soc_specific = proptools.BoolPtr(true)
457 } else if module.DeviceSpecific() {
458 props.Device_specific = proptools.BoolPtr(true)
459 } else if module.ProductSpecific() {
460 props.Product_specific = proptools.BoolPtr(true)
Sundong Ahn0d7dff42019-12-04 12:53:44 +0900461 } else if module.SystemExtSpecific() {
462 props.System_ext_specific = proptools.BoolPtr(true)
Jiyong Parkc678ad32018-04-10 13:07:10 +0900463 }
Anton Hansson5fd5d242020-03-27 19:43:19 +0000464 // Dist the class jar artifact for sdk builds.
465 if !Bool(module.sdkLibraryProperties.No_dist) {
466 props.Dist.Targets = []string{"sdk", "win_sdk"}
467 props.Dist.Dest = proptools.StringPtr(fmt.Sprintf("%v.jar", module.BaseModuleName()))
468 props.Dist.Dir = proptools.StringPtr(module.apiDistPath(apiScope))
469 props.Dist.Tag = proptools.StringPtr(".jar")
470 }
Jiyong Parkc678ad32018-04-10 13:07:10 +0900471
Colin Cross84dfc3d2019-09-25 11:33:01 -0700472 mctx.CreateModule(LibraryFactory, &props)
Jiyong Parkc678ad32018-04-10 13:07:10 +0900473}
474
Paul Duffin6d0886e2020-04-07 18:49:53 +0100475// Creates a droidstubs module that creates stubs source files from the given full source
Jiyong Parkc678ad32018-04-10 13:07:10 +0900476// files
Paul Duffind1b3a922020-01-22 11:57:20 +0000477func (module *SdkLibrary) createStubsSources(mctx android.LoadHookContext, apiScope *apiScope) {
Jiyong Parkc678ad32018-04-10 13:07:10 +0900478 props := struct {
Sundong Ahn054b19a2018-10-19 13:46:09 +0900479 Name *string
480 Srcs []string
481 Installable *bool
Paul Duffin52d398a2019-06-11 12:31:14 +0100482 Sdk_version *string
Paul Duffin12ceb462019-12-24 20:31:31 +0000483 System_modules *string
Sundong Ahn054b19a2018-10-19 13:46:09 +0900484 Libs []string
Paul Duffin11512472019-02-11 15:55:17 +0000485 Arg_files []string
Sundong Ahn054b19a2018-10-19 13:46:09 +0900486 Args *string
Sundong Ahn054b19a2018-10-19 13:46:09 +0900487 Java_version *string
488 Merge_annotations_dirs []string
489 Merge_inclusion_annotations_dirs []string
490 Check_api struct {
Inseob Kim38449af2019-02-28 14:24:05 +0900491 Current ApiToCheck
492 Last_released ApiToCheck
493 Ignore_missing_latest_api *bool
Jiyong Park58c518b2018-05-12 22:29:12 +0900494 }
Sundong Ahn1b92c822018-05-29 11:35:17 +0900495 Aidl struct {
496 Include_dirs []string
497 Local_include_dirs []string
498 }
Anton Hansson5fd5d242020-03-27 19:43:19 +0000499 Dist struct {
500 Targets []string
501 Dest *string
502 Dir *string
503 }
Jiyong Parkc678ad32018-04-10 13:07:10 +0900504 }{}
505
Paul Duffin250e6192019-06-07 10:44:37 +0100506 sdkDep := decodeSdkDep(mctx, sdkContext(&module.Library))
Paul Duffin12ceb462019-12-24 20:31:31 +0000507 // Use the platform API if standard libraries were requested, otherwise use
508 // no default libraries.
Paul Duffin52d398a2019-06-11 12:31:14 +0100509 sdkVersion := ""
510 if !sdkDep.hasStandardLibs() {
511 sdkVersion = "none"
512 }
Paul Duffin250e6192019-06-07 10:44:37 +0100513
Jiyong Parkdf130542018-04-27 16:29:21 +0900514 props.Name = proptools.StringPtr(module.docsName(apiScope))
Sundong Ahn054b19a2018-10-19 13:46:09 +0900515 props.Srcs = append(props.Srcs, module.Library.Module.properties.Srcs...)
Paul Duffin52d398a2019-06-11 12:31:14 +0100516 props.Sdk_version = proptools.StringPtr(sdkVersion)
Paul Duffin12ceb462019-12-24 20:31:31 +0000517 props.System_modules = module.Library.Module.deviceProperties.System_modules
Jiyong Parkc678ad32018-04-10 13:07:10 +0900518 props.Installable = proptools.BoolPtr(false)
Sundong Ahne6f0b052018-06-05 16:46:14 +0900519 // A droiddoc module has only one Libs property and doesn't distinguish between
520 // shared libs and static libs. So we need to add both of these libs to Libs property.
Sundong Ahn054b19a2018-10-19 13:46:09 +0900521 props.Libs = module.Library.Module.properties.Libs
522 props.Libs = append(props.Libs, module.Library.Module.properties.Static_libs...)
523 props.Aidl.Include_dirs = module.Library.Module.deviceProperties.Aidl.Include_dirs
524 props.Aidl.Local_include_dirs = module.Library.Module.deviceProperties.Aidl.Local_include_dirs
Sundong Ahn054b19a2018-10-19 13:46:09 +0900525 props.Java_version = module.Library.Module.properties.Java_version
Jiyong Parkc678ad32018-04-10 13:07:10 +0900526
Sundong Ahn054b19a2018-10-19 13:46:09 +0900527 props.Merge_annotations_dirs = module.sdkLibraryProperties.Merge_annotations_dirs
528 props.Merge_inclusion_annotations_dirs = module.sdkLibraryProperties.Merge_inclusion_annotations_dirs
529
Paul Duffin6d0886e2020-04-07 18:49:53 +0100530 droidstubsArgs := []string{}
Paul Duffin235ffff2019-12-24 10:41:30 +0000531 if len(module.sdkLibraryProperties.Api_packages) != 0 {
Paul Duffin6d0886e2020-04-07 18:49:53 +0100532 droidstubsArgs = append(droidstubsArgs, "--stub-packages "+strings.Join(module.sdkLibraryProperties.Api_packages, ":"))
Paul Duffin235ffff2019-12-24 10:41:30 +0000533 }
534 if len(module.sdkLibraryProperties.Hidden_api_packages) != 0 {
Paul Duffin6d0886e2020-04-07 18:49:53 +0100535 droidstubsArgs = append(droidstubsArgs,
Paul Duffin235ffff2019-12-24 10:41:30 +0000536 android.JoinWithPrefix(module.sdkLibraryProperties.Hidden_api_packages, " --hide-package "))
537 }
Paul Duffin6d0886e2020-04-07 18:49:53 +0100538 droidstubsArgs = append(droidstubsArgs, module.sdkLibraryProperties.Droiddoc_options...)
Paul Duffin235ffff2019-12-24 10:41:30 +0000539 disabledWarnings := []string{
540 "MissingPermission",
541 "BroadcastBehavior",
542 "HiddenSuperclass",
543 "DeprecationMismatch",
544 "UnavailableSymbol",
545 "SdkConstant",
546 "HiddenTypeParameter",
547 "Todo",
548 "Typo",
549 }
Paul Duffin6d0886e2020-04-07 18:49:53 +0100550 droidstubsArgs = append(droidstubsArgs, android.JoinWithPrefix(disabledWarnings, "--hide "))
Sundong Ahnfb2721f2018-09-17 13:23:09 +0900551
Paul Duffin1fb487d2020-04-07 18:50:10 +0100552 // Add in scope specific arguments.
553 droidstubsArgs = append(droidstubsArgs, apiScope.droidstubsArgs...)
Paul Duffin11512472019-02-11 15:55:17 +0000554 props.Arg_files = module.sdkLibraryProperties.Droiddoc_option_files
Paul Duffin6d0886e2020-04-07 18:49:53 +0100555 props.Args = proptools.StringPtr(strings.Join(droidstubsArgs, " "))
Jiyong Parkc678ad32018-04-10 13:07:10 +0900556
557 // List of APIs identified from the provided source files are created. They are later
558 // compared against to the not-yet-released (a.k.a current) list of APIs and to the
559 // last-released (a.k.a numbered) list of API.
Paul Duffind1b3a922020-01-22 11:57:20 +0000560 currentApiFileName := apiScope.apiFilePrefix + "current.txt"
561 removedApiFileName := apiScope.apiFilePrefix + "removed.txt"
Paul Duffin749f98f2019-12-30 17:23:46 +0000562 apiDir := module.getApiDir()
563 currentApiFileName = path.Join(apiDir, currentApiFileName)
564 removedApiFileName = path.Join(apiDir, removedApiFileName)
Jiyong Parkc678ad32018-04-10 13:07:10 +0900565
Jiyong Park58c518b2018-05-12 22:29:12 +0900566 // check against the not-yet-release API
567 props.Check_api.Current.Api_file = proptools.StringPtr(currentApiFileName)
568 props.Check_api.Current.Removed_api_file = proptools.StringPtr(removedApiFileName)
Jiyong Park58c518b2018-05-12 22:29:12 +0900569
570 // check against the latest released API
571 props.Check_api.Last_released.Api_file = proptools.StringPtr(
572 module.latestApiFilegroupName(apiScope))
573 props.Check_api.Last_released.Removed_api_file = proptools.StringPtr(
574 module.latestRemovedApiFilegroupName(apiScope))
Inseob Kim38449af2019-02-28 14:24:05 +0900575 props.Check_api.Ignore_missing_latest_api = proptools.BoolPtr(true)
Jiyong Park58c518b2018-05-12 22:29:12 +0900576
Anton Hansson5fd5d242020-03-27 19:43:19 +0000577 // Dist the api txt artifact for sdk builds.
578 if !Bool(module.sdkLibraryProperties.No_dist) {
579 props.Dist.Targets = []string{"sdk", "win_sdk"}
580 props.Dist.Dest = proptools.StringPtr(fmt.Sprintf("%v.txt", module.BaseModuleName()))
581 props.Dist.Dir = proptools.StringPtr(path.Join(module.apiDistPath(apiScope), "api"))
582 }
583
Colin Cross84dfc3d2019-09-25 11:33:01 -0700584 mctx.CreateModule(DroidstubsFactory, &props)
Jiyong Parkc678ad32018-04-10 13:07:10 +0900585}
586
Jooyung Han5e9013b2020-03-10 06:23:13 +0900587func (module *SdkLibrary) DepIsInSameApex(mctx android.BaseModuleContext, dep android.Module) bool {
588 depTag := mctx.OtherModuleDependencyTag(dep)
589 if depTag == xmlPermissionsFileTag {
590 return true
591 }
592 return module.Library.DepIsInSameApex(mctx, dep)
593}
594
Jiyong Parkc678ad32018-04-10 13:07:10 +0900595// Creates the xml file that publicizes the runtime library
Colin Crossf8b860a2019-04-16 14:43:28 -0700596func (module *SdkLibrary) createXmlFile(mctx android.LoadHookContext) {
Jiyong Parke3833882020-02-17 17:28:10 +0900597 props := struct {
Sundong Ahn0d7dff42019-12-04 12:53:44 +0900598 Name *string
Jiyong Parke3833882020-02-17 17:28:10 +0900599 Lib_name *string
Sundong Ahn0d7dff42019-12-04 12:53:44 +0900600 Soc_specific *bool
601 Device_specific *bool
602 Product_specific *bool
603 System_ext_specific *bool
Jooyung Han5e9013b2020-03-10 06:23:13 +0900604 Apex_available []string
Jiyong Parke3833882020-02-17 17:28:10 +0900605 }{
Jooyung Han5e9013b2020-03-10 06:23:13 +0900606 Name: proptools.StringPtr(module.xmlFileName()),
607 Lib_name: proptools.StringPtr(module.BaseModuleName()),
608 Apex_available: module.ApexProperties.Apex_available,
Jiyong Parkc678ad32018-04-10 13:07:10 +0900609 }
Jiyong Parke3833882020-02-17 17:28:10 +0900610
611 if module.SocSpecific() {
612 props.Soc_specific = proptools.BoolPtr(true)
613 } else if module.DeviceSpecific() {
614 props.Device_specific = proptools.BoolPtr(true)
615 } else if module.ProductSpecific() {
616 props.Product_specific = proptools.BoolPtr(true)
617 } else if module.SystemExtSpecific() {
618 props.System_ext_specific = proptools.BoolPtr(true)
619 }
620
621 mctx.CreateModule(sdkLibraryXmlFactory, &props)
Jiyong Parkc678ad32018-04-10 13:07:10 +0900622}
623
Paul Duffin50061512020-01-21 16:31:05 +0000624func PrebuiltJars(ctx android.BaseModuleContext, baseName string, s sdkSpec) android.Paths {
Jiyong Park6a927c42020-01-21 02:03:43 +0900625 var ver sdkVersion
626 var kind sdkKind
627 if s.usePrebuilt(ctx) {
628 ver = s.version
629 kind = s.kind
Jiyong Parkc678ad32018-04-10 13:07:10 +0900630 } else {
Jiyong Park6a927c42020-01-21 02:03:43 +0900631 // We don't have prebuilt SDK for the specific sdkVersion.
632 // Instead of breaking the build, fallback to use "system_current"
633 ver = sdkVersionCurrent
634 kind = sdkSystem
Sundong Ahn054b19a2018-10-19 13:46:09 +0900635 }
Jiyong Park6a927c42020-01-21 02:03:43 +0900636
637 dir := filepath.Join("prebuilts", "sdk", ver.String(), kind.String())
Paul Duffin50061512020-01-21 16:31:05 +0000638 jar := filepath.Join(dir, baseName+".jar")
Sundong Ahn054b19a2018-10-19 13:46:09 +0900639 jarPath := android.ExistentPathForSource(ctx, jar)
Sundong Ahnae418ac2019-02-28 15:01:28 +0900640 if !jarPath.Valid() {
Colin Cross07c88562020-01-07 09:34:44 -0800641 if ctx.Config().AllowMissingDependencies() {
642 return android.Paths{android.PathForSource(ctx, jar)}
643 } else {
Jiyong Park6a927c42020-01-21 02:03:43 +0900644 ctx.PropertyErrorf("sdk_library", "invalid sdk version %q, %q does not exist", s.raw, jar)
Colin Cross07c88562020-01-07 09:34:44 -0800645 }
Sundong Ahnae418ac2019-02-28 15:01:28 +0900646 return nil
647 }
Sundong Ahn054b19a2018-10-19 13:46:09 +0900648 return android.Paths{jarPath.Path()}
649}
650
Paul Duffind1b3a922020-01-22 11:57:20 +0000651func (module *SdkLibrary) sdkJars(
652 ctx android.BaseModuleContext,
653 sdkVersion sdkSpec,
654 headerJars bool) android.Paths {
655
Paul Duffin50061512020-01-21 16:31:05 +0000656 // If a specific numeric version has been requested then use prebuilt versions of the sdk.
657 if sdkVersion.version.isNumbered() {
658 return PrebuiltJars(ctx, module.BaseModuleName(), sdkVersion)
Sundong Ahn054b19a2018-10-19 13:46:09 +0900659 } else {
Paul Duffind1b3a922020-01-22 11:57:20 +0000660 if !sdkVersion.specified() {
661 if headerJars {
662 return module.Library.HeaderJars()
663 } else {
664 return module.Library.ImplementationJars()
665 }
666 }
Paul Duffin726d23c2020-01-22 16:30:37 +0000667 var apiScope *apiScope
Jiyong Park6a927c42020-01-21 02:03:43 +0900668 switch sdkVersion.kind {
669 case sdkSystem:
Paul Duffin726d23c2020-01-22 16:30:37 +0000670 apiScope = apiScopeSystem
671 case sdkTest:
672 apiScope = apiScopeTest
Jiyong Park6a927c42020-01-21 02:03:43 +0900673 case sdkPrivate:
Sundong Ahn054b19a2018-10-19 13:46:09 +0900674 return module.Library.HeaderJars()
Jiyong Park6a927c42020-01-21 02:03:43 +0900675 default:
Paul Duffin726d23c2020-01-22 16:30:37 +0000676 apiScope = apiScopePublic
Paul Duffind1b3a922020-01-22 11:57:20 +0000677 }
678
Paul Duffin726d23c2020-01-22 16:30:37 +0000679 paths := module.getScopePaths(apiScope)
Paul Duffind1b3a922020-01-22 11:57:20 +0000680 if headerJars {
681 return paths.stubsHeaderPath
682 } else {
683 return paths.stubsImplPath
Sundong Ahn054b19a2018-10-19 13:46:09 +0900684 }
Jiyong Parkc678ad32018-04-10 13:07:10 +0900685 }
686}
687
Sundong Ahn241cd372018-07-13 16:16:44 +0900688// to satisfy SdkLibraryDependency interface
Paul Duffind1b3a922020-01-22 11:57:20 +0000689func (module *SdkLibrary) SdkHeaderJars(ctx android.BaseModuleContext, sdkVersion sdkSpec) android.Paths {
690 return module.sdkJars(ctx, sdkVersion, true /*headerJars*/)
691}
692
693// to satisfy SdkLibraryDependency interface
Jiyong Park6a927c42020-01-21 02:03:43 +0900694func (module *SdkLibrary) SdkImplementationJars(ctx android.BaseModuleContext, sdkVersion sdkSpec) android.Paths {
Paul Duffind1b3a922020-01-22 11:57:20 +0000695 return module.sdkJars(ctx, sdkVersion, false /*headerJars*/)
Sundong Ahn241cd372018-07-13 16:16:44 +0900696}
697
Sundong Ahn80a87b32019-05-13 15:02:50 +0900698func (module *SdkLibrary) SetNoDist() {
699 module.sdkLibraryProperties.No_dist = proptools.BoolPtr(true)
700}
701
Colin Cross571cccf2019-02-04 11:22:08 -0800702var javaSdkLibrariesKey = android.NewOnceKey("javaSdkLibraries")
703
Jiyong Park82484c02018-04-23 21:41:26 +0900704func javaSdkLibraries(config android.Config) *[]string {
Colin Cross571cccf2019-02-04 11:22:08 -0800705 return config.Once(javaSdkLibrariesKey, func() interface{} {
Jiyong Park82484c02018-04-23 21:41:26 +0900706 return &[]string{}
707 }).(*[]string)
708}
709
Paul Duffin749f98f2019-12-30 17:23:46 +0000710func (module *SdkLibrary) getApiDir() string {
711 return proptools.StringDefault(module.sdkLibraryProperties.Api_dir, "api")
712}
713
Jiyong Parkc678ad32018-04-10 13:07:10 +0900714// For a java_sdk_library module, create internal modules for stubs, docs,
715// runtime libs and xml file. If requested, the stubs and docs are created twice
716// once for public API level and once for system API level
Colin Crossf8b860a2019-04-16 14:43:28 -0700717func (module *SdkLibrary) CreateInternalModules(mctx android.LoadHookContext) {
Inseob Kim6e93ac92019-03-21 17:43:49 +0900718 if len(module.Library.Module.properties.Srcs) == 0 {
Inseob Kimc0907f12019-02-08 21:00:45 +0900719 mctx.PropertyErrorf("srcs", "java_sdk_library must specify srcs")
Jooyung Han58f26ab2019-12-18 15:34:32 +0900720 return
Inseob Kimc0907f12019-02-08 21:00:45 +0900721 }
722
Paul Duffin37e0b772019-12-30 17:20:10 +0000723 // If this builds against standard libraries (i.e. is not part of the core libraries)
724 // then assume it provides both system and test apis. Otherwise, assume it does not and
725 // also assume it does not contribute to the dist build.
726 sdkDep := decodeSdkDep(mctx, sdkContext(&module.Library))
727 hasSystemAndTestApis := sdkDep.hasStandardLibs()
728 module.sdkLibraryProperties.Has_system_and_test_apis = hasSystemAndTestApis
729 module.sdkLibraryProperties.No_dist = proptools.BoolPtr(!hasSystemAndTestApis)
730
Inseob Kim8098faa2019-03-18 10:19:51 +0900731 missing_current_api := false
732
Paul Duffind1b3a922020-01-22 11:57:20 +0000733 activeScopes := module.getActiveApiScopes()
734
Paul Duffin749f98f2019-12-30 17:23:46 +0000735 apiDir := module.getApiDir()
Paul Duffind1b3a922020-01-22 11:57:20 +0000736 for _, scope := range activeScopes {
Inseob Kim8098faa2019-03-18 10:19:51 +0900737 for _, api := range []string{"current.txt", "removed.txt"} {
Paul Duffind1b3a922020-01-22 11:57:20 +0000738 path := path.Join(mctx.ModuleDir(), apiDir, scope.apiFilePrefix+api)
Inseob Kim8098faa2019-03-18 10:19:51 +0900739 p := android.ExistentPathForSource(mctx, path)
740 if !p.Valid() {
741 mctx.ModuleErrorf("Current api file %#v doesn't exist", path)
742 missing_current_api = true
743 }
744 }
745 }
746
747 if missing_current_api {
748 script := "build/soong/scripts/gen-java-current-api-files.sh"
749 p := android.ExistentPathForSource(mctx, script)
750
751 if !p.Valid() {
752 panic(fmt.Sprintf("script file %s doesn't exist", script))
753 }
754
755 mctx.ModuleErrorf("One or more current api files are missing. "+
756 "You can update them by:\n"+
Paul Duffin37e0b772019-12-30 17:20:10 +0000757 "%s %q %s && m update-api",
Paul Duffind1b3a922020-01-22 11:57:20 +0000758 script, filepath.Join(mctx.ModuleDir(), apiDir),
759 strings.Join(activeScopes.Strings(func(s *apiScope) string { return s.apiFilePrefix }), " "))
Inseob Kim8098faa2019-03-18 10:19:51 +0900760 return
761 }
762
Paul Duffind1b3a922020-01-22 11:57:20 +0000763 for _, scope := range activeScopes {
764 module.createStubsLibrary(mctx, scope)
765 module.createStubsSources(mctx, scope)
Inseob Kimc0907f12019-02-08 21:00:45 +0900766 }
767
Paul Duffin43db9be2019-12-30 17:35:49 +0000768 if !proptools.Bool(module.sdkLibraryProperties.Api_only) {
769 // for runtime
770 module.createXmlFile(mctx)
771
772 // record java_sdk_library modules so that they are exported to make
773 javaSdkLibraries := javaSdkLibraries(mctx.Config())
774 javaSdkLibrariesLock.Lock()
775 defer javaSdkLibrariesLock.Unlock()
776 *javaSdkLibraries = append(*javaSdkLibraries, module.BaseModuleName())
777 }
Inseob Kimc0907f12019-02-08 21:00:45 +0900778}
779
780func (module *SdkLibrary) InitSdkLibraryProperties() {
Sundong Ahn054b19a2018-10-19 13:46:09 +0900781 module.AddProperties(
782 &module.sdkLibraryProperties,
783 &module.Library.Module.properties,
784 &module.Library.Module.dexpreoptProperties,
785 &module.Library.Module.deviceProperties,
786 &module.Library.Module.protoProperties,
787 )
788
789 module.Library.Module.properties.Installable = proptools.BoolPtr(true)
790 module.Library.Module.deviceProperties.IsSDKLibrary = true
Inseob Kimc0907f12019-02-08 21:00:45 +0900791}
Sundong Ahn054b19a2018-10-19 13:46:09 +0900792
Jaewoong Jung4f158ee2019-07-11 10:05:35 -0700793// java_sdk_library is a special Java library that provides optional platform APIs to apps.
794// In practice, it can be viewed as a combination of several modules: 1) stubs library that clients
795// are linked against to, 2) droiddoc module that internally generates API stubs source files,
796// 3) the real runtime shared library that implements the APIs, and 4) XML file for adding
797// the runtime lib to the classpath at runtime if requested via <uses-library>.
Inseob Kimc0907f12019-02-08 21:00:45 +0900798func SdkLibraryFactory() android.Module {
799 module := &SdkLibrary{}
800 module.InitSdkLibraryProperties()
Jooyung Han58f26ab2019-12-18 15:34:32 +0900801 android.InitApexModule(module)
Sundong Ahn054b19a2018-10-19 13:46:09 +0900802 InitJavaModule(module, android.HostAndDeviceSupported)
Colin Crossf8b860a2019-04-16 14:43:28 -0700803 android.AddLoadHook(module, func(ctx android.LoadHookContext) { module.CreateInternalModules(ctx) })
Jiyong Parkc678ad32018-04-10 13:07:10 +0900804 return module
805}
Colin Cross79c7c262019-04-17 11:11:46 -0700806
807//
808// SDK library prebuilts
809//
810
Paul Duffin56d44902020-01-31 13:36:25 +0000811// Properties associated with each api scope.
812type sdkLibraryScopeProperties struct {
Colin Cross79c7c262019-04-17 11:11:46 -0700813 Jars []string `android:"path"`
814
815 Sdk_version *string
816
Colin Cross79c7c262019-04-17 11:11:46 -0700817 // List of shared java libs that this module has dependencies to
818 Libs []string
Paul Duffin3d1248c2020-04-09 00:10:17 +0100819
820 // The stub sources.
821 Stub_srcs []string `android:"path"`
Paul Duffin1fd005d2020-04-09 01:08:11 +0100822
823 // The current.txt
824 Current_api string `android:"path"`
825
826 // The removed.txt
827 Removed_api string `android:"path"`
Colin Cross79c7c262019-04-17 11:11:46 -0700828}
829
Paul Duffin56d44902020-01-31 13:36:25 +0000830type sdkLibraryImportProperties struct {
Paul Duffinfcfd7912020-01-31 17:54:30 +0000831 // List of shared java libs, common to all scopes, that this module has
832 // dependencies to
833 Libs []string
Paul Duffin56d44902020-01-31 13:36:25 +0000834}
835
Colin Cross79c7c262019-04-17 11:11:46 -0700836type sdkLibraryImport struct {
837 android.ModuleBase
838 android.DefaultableModuleBase
839 prebuilt android.Prebuilt
Paul Duffindd46f712020-02-10 13:37:10 +0000840 android.ApexModuleBase
841 android.SdkBase
Colin Cross79c7c262019-04-17 11:11:46 -0700842
843 properties sdkLibraryImportProperties
844
Paul Duffin46a26a82020-04-07 19:27:04 +0100845 // Map from api scope to the scope specific property structure.
846 scopeProperties map[*apiScope]*sdkLibraryScopeProperties
847
Paul Duffin56d44902020-01-31 13:36:25 +0000848 commonToSdkLibraryAndImport
Colin Cross79c7c262019-04-17 11:11:46 -0700849}
850
851var _ SdkLibraryDependency = (*sdkLibraryImport)(nil)
852
Paul Duffin46a26a82020-04-07 19:27:04 +0100853// The type of a structure that contains a field of type sdkLibraryScopeProperties
854// for each apiscope in allApiScopes, e.g. something like:
855// struct {
856// Public sdkLibraryScopeProperties
857// System sdkLibraryScopeProperties
858// ...
859// }
860var allScopeStructType = createAllScopePropertiesStructType()
861
862// Dynamically create a structure type for each apiscope in allApiScopes.
863func createAllScopePropertiesStructType() reflect.Type {
864 var fields []reflect.StructField
865 for _, apiScope := range allApiScopes {
866 field := reflect.StructField{
867 Name: apiScope.fieldName,
868 Type: reflect.TypeOf(sdkLibraryScopeProperties{}),
869 }
870 fields = append(fields, field)
871 }
872
873 return reflect.StructOf(fields)
874}
875
876// Create an instance of the scope specific structure type and return a map
877// from apiscope to a pointer to each scope specific field.
878func createPropertiesInstance() (interface{}, map[*apiScope]*sdkLibraryScopeProperties) {
879 allScopePropertiesPtr := reflect.New(allScopeStructType)
880 allScopePropertiesStruct := allScopePropertiesPtr.Elem()
881 scopeProperties := make(map[*apiScope]*sdkLibraryScopeProperties)
882
883 for _, apiScope := range allApiScopes {
884 field := allScopePropertiesStruct.FieldByName(apiScope.fieldName)
885 scopeProperties[apiScope] = field.Addr().Interface().(*sdkLibraryScopeProperties)
886 }
887
888 return allScopePropertiesPtr.Interface(), scopeProperties
889}
890
Jaewoong Jung4f158ee2019-07-11 10:05:35 -0700891// java_sdk_library_import imports a prebuilt java_sdk_library.
Colin Cross79c7c262019-04-17 11:11:46 -0700892func sdkLibraryImportFactory() android.Module {
893 module := &sdkLibraryImport{}
894
Paul Duffin46a26a82020-04-07 19:27:04 +0100895 allScopeProperties, scopeToProperties := createPropertiesInstance()
896 module.scopeProperties = scopeToProperties
897 module.AddProperties(&module.properties, allScopeProperties)
Colin Cross79c7c262019-04-17 11:11:46 -0700898
Paul Duffin0bdcb272020-02-06 15:24:57 +0000899 android.InitPrebuiltModule(module, &[]string{""})
Paul Duffindd46f712020-02-10 13:37:10 +0000900 android.InitApexModule(module)
901 android.InitSdkAwareModule(module)
Colin Cross79c7c262019-04-17 11:11:46 -0700902 InitJavaModule(module, android.HostAndDeviceSupported)
903
904 android.AddLoadHook(module, func(mctx android.LoadHookContext) { module.createInternalModules(mctx) })
905 return module
906}
907
908func (module *sdkLibraryImport) Prebuilt() *android.Prebuilt {
909 return &module.prebuilt
910}
911
912func (module *sdkLibraryImport) Name() string {
913 return module.prebuilt.Name(module.ModuleBase.Name())
914}
915
916func (module *sdkLibraryImport) createInternalModules(mctx android.LoadHookContext) {
Colin Cross79c7c262019-04-17 11:11:46 -0700917
Paul Duffin50061512020-01-21 16:31:05 +0000918 // If the build is configured to use prebuilts then force this to be preferred.
919 if mctx.Config().UnbundledBuildUsePrebuiltSdks() {
920 module.prebuilt.ForcePrefer()
921 }
922
Paul Duffin46a26a82020-04-07 19:27:04 +0100923 for apiScope, scopeProperties := range module.scopeProperties {
Paul Duffin56d44902020-01-31 13:36:25 +0000924 if len(scopeProperties.Jars) == 0 {
925 continue
926 }
927
Paul Duffinbbb546b2020-04-09 00:07:11 +0100928 module.createJavaImportForStubs(mctx, apiScope, scopeProperties)
Paul Duffin3d1248c2020-04-09 00:10:17 +0100929
930 module.createPrebuiltStubsSources(mctx, apiScope, scopeProperties)
Paul Duffin56d44902020-01-31 13:36:25 +0000931 }
Colin Cross79c7c262019-04-17 11:11:46 -0700932
933 javaSdkLibraries := javaSdkLibraries(mctx.Config())
934 javaSdkLibrariesLock.Lock()
935 defer javaSdkLibrariesLock.Unlock()
936 *javaSdkLibraries = append(*javaSdkLibraries, module.BaseModuleName())
937}
938
Paul Duffinbbb546b2020-04-09 00:07:11 +0100939func (module *sdkLibraryImport) createJavaImportForStubs(mctx android.LoadHookContext, apiScope *apiScope, scopeProperties *sdkLibraryScopeProperties) {
940 // Creates a java import for the jar with ".stubs" suffix
941 props := struct {
942 Name *string
943 Soc_specific *bool
944 Device_specific *bool
945 Product_specific *bool
946 System_ext_specific *bool
947 Sdk_version *string
948 Libs []string
949 Jars []string
950 Prefer *bool
951 }{}
952 props.Name = proptools.StringPtr(apiScope.stubsModuleName(module.BaseModuleName()))
953 props.Sdk_version = scopeProperties.Sdk_version
954 // Prepend any of the libs from the legacy public properties to the libs for each of the
955 // scopes to avoid having to duplicate them in each scope.
956 props.Libs = append(module.properties.Libs, scopeProperties.Libs...)
957 props.Jars = scopeProperties.Jars
958 if module.SocSpecific() {
959 props.Soc_specific = proptools.BoolPtr(true)
960 } else if module.DeviceSpecific() {
961 props.Device_specific = proptools.BoolPtr(true)
962 } else if module.ProductSpecific() {
963 props.Product_specific = proptools.BoolPtr(true)
964 } else if module.SystemExtSpecific() {
965 props.System_ext_specific = proptools.BoolPtr(true)
966 }
967 // If the build should use prebuilt sdks then set prefer to true on the stubs library.
968 // That will cause the prebuilt version of the stubs to override the source version.
969 if mctx.Config().UnbundledBuildUsePrebuiltSdks() {
970 props.Prefer = proptools.BoolPtr(true)
971 }
972 mctx.CreateModule(ImportFactory, &props)
973}
974
Paul Duffin3d1248c2020-04-09 00:10:17 +0100975func (module *sdkLibraryImport) createPrebuiltStubsSources(mctx android.LoadHookContext, apiScope *apiScope, scopeProperties *sdkLibraryScopeProperties) {
976 props := struct {
977 Name *string
978 Srcs []string
979 }{}
980 props.Name = proptools.StringPtr(apiScope.docsModuleName(module.BaseModuleName()))
981 props.Srcs = scopeProperties.Stub_srcs
982 mctx.CreateModule(PrebuiltStubsSourcesFactory, &props)
983}
984
Colin Cross79c7c262019-04-17 11:11:46 -0700985func (module *sdkLibraryImport) DepsMutator(ctx android.BottomUpMutatorContext) {
Paul Duffin46a26a82020-04-07 19:27:04 +0100986 for apiScope, scopeProperties := range module.scopeProperties {
Paul Duffin56d44902020-01-31 13:36:25 +0000987 if len(scopeProperties.Jars) == 0 {
988 continue
989 }
990
991 // Add dependencies to the prebuilt stubs library
992 ctx.AddVariationDependencies(nil, apiScope.stubsTag, apiScope.stubsModuleName(module.BaseModuleName()))
993 }
Colin Cross79c7c262019-04-17 11:11:46 -0700994}
995
996func (module *sdkLibraryImport) GenerateAndroidBuildActions(ctx android.ModuleContext) {
997 // Record the paths to the prebuilt stubs library.
998 ctx.VisitDirectDeps(func(to android.Module) {
999 tag := ctx.OtherModuleDependencyTag(to)
1000
Paul Duffin56d44902020-01-31 13:36:25 +00001001 if lib, ok := to.(Dependency); ok {
1002 if scopeTag, ok := tag.(scopeDependencyTag); ok {
1003 apiScope := scopeTag.apiScope
1004 scopePaths := module.getScopePaths(apiScope)
1005 scopePaths.stubsHeaderPath = lib.HeaderJars()
1006 }
Colin Cross79c7c262019-04-17 11:11:46 -07001007 }
1008 })
1009}
1010
Paul Duffin56d44902020-01-31 13:36:25 +00001011func (module *sdkLibraryImport) sdkJars(
1012 ctx android.BaseModuleContext,
1013 sdkVersion sdkSpec) android.Paths {
1014
Paul Duffin50061512020-01-21 16:31:05 +00001015 // If a specific numeric version has been requested then use prebuilt versions of the sdk.
1016 if sdkVersion.version.isNumbered() {
1017 return PrebuiltJars(ctx, module.BaseModuleName(), sdkVersion)
1018 }
1019
Paul Duffin56d44902020-01-31 13:36:25 +00001020 var apiScope *apiScope
1021 switch sdkVersion.kind {
1022 case sdkSystem:
1023 apiScope = apiScopeSystem
1024 case sdkTest:
1025 apiScope = apiScopeTest
1026 default:
1027 apiScope = apiScopePublic
1028 }
1029
1030 paths := module.getScopePaths(apiScope)
1031 return paths.stubsHeaderPath
1032}
1033
Colin Cross79c7c262019-04-17 11:11:46 -07001034// to satisfy SdkLibraryDependency interface
Jiyong Park6a927c42020-01-21 02:03:43 +09001035func (module *sdkLibraryImport) SdkHeaderJars(ctx android.BaseModuleContext, sdkVersion sdkSpec) android.Paths {
Colin Cross79c7c262019-04-17 11:11:46 -07001036 // This module is just a wrapper for the prebuilt stubs.
Paul Duffin56d44902020-01-31 13:36:25 +00001037 return module.sdkJars(ctx, sdkVersion)
Colin Cross79c7c262019-04-17 11:11:46 -07001038}
1039
1040// to satisfy SdkLibraryDependency interface
Jiyong Park6a927c42020-01-21 02:03:43 +09001041func (module *sdkLibraryImport) SdkImplementationJars(ctx android.BaseModuleContext, sdkVersion sdkSpec) android.Paths {
Colin Cross79c7c262019-04-17 11:11:46 -07001042 // This module is just a wrapper for the stubs.
Paul Duffin56d44902020-01-31 13:36:25 +00001043 return module.sdkJars(ctx, sdkVersion)
Colin Cross79c7c262019-04-17 11:11:46 -07001044}
Jiyong Parke3833882020-02-17 17:28:10 +09001045
1046//
1047// java_sdk_library_xml
1048//
1049type sdkLibraryXml struct {
1050 android.ModuleBase
1051 android.DefaultableModuleBase
1052 android.ApexModuleBase
1053
1054 properties sdkLibraryXmlProperties
1055
1056 outputFilePath android.OutputPath
1057 installDirPath android.InstallPath
1058}
1059
1060type sdkLibraryXmlProperties struct {
1061 // canonical name of the lib
1062 Lib_name *string
1063}
1064
1065// java_sdk_library_xml builds the permission xml file for a java_sdk_library.
1066// Not to be used directly by users. java_sdk_library internally uses this.
1067func sdkLibraryXmlFactory() android.Module {
1068 module := &sdkLibraryXml{}
1069
1070 module.AddProperties(&module.properties)
1071
1072 android.InitApexModule(module)
1073 android.InitAndroidArchModule(module, android.DeviceSupported, android.MultilibCommon)
1074
1075 return module
1076}
1077
1078// from android.PrebuiltEtcModule
1079func (module *sdkLibraryXml) SubDir() string {
1080 return "permissions"
1081}
1082
1083// from android.PrebuiltEtcModule
1084func (module *sdkLibraryXml) OutputFile() android.OutputPath {
1085 return module.outputFilePath
1086}
1087
1088// from android.ApexModule
1089func (module *sdkLibraryXml) AvailableFor(what string) bool {
1090 return true
1091}
1092
1093func (module *sdkLibraryXml) DepsMutator(ctx android.BottomUpMutatorContext) {
1094 // do nothing
1095}
1096
1097// File path to the runtime implementation library
1098func (module *sdkLibraryXml) implPath() string {
1099 implName := proptools.String(module.properties.Lib_name)
1100 if apexName := module.ApexName(); apexName != "" {
1101 // TODO(b/146468504): ApexName() is only a soong module name, not apex name.
1102 // In most cases, this works fine. But when apex_name is set or override_apex is used
1103 // this can be wrong.
1104 return fmt.Sprintf("/apex/%s/javalib/%s.jar", apexName, implName)
1105 }
1106 partition := "system"
1107 if module.SocSpecific() {
1108 partition = "vendor"
1109 } else if module.DeviceSpecific() {
1110 partition = "odm"
1111 } else if module.ProductSpecific() {
1112 partition = "product"
1113 } else if module.SystemExtSpecific() {
1114 partition = "system_ext"
1115 }
1116 return "/" + partition + "/framework/" + implName + ".jar"
1117}
1118
1119func (module *sdkLibraryXml) GenerateAndroidBuildActions(ctx android.ModuleContext) {
1120 libName := proptools.String(module.properties.Lib_name)
1121 xmlContent := fmt.Sprintf(permissionsTemplate, libName, module.implPath())
1122
1123 module.outputFilePath = android.PathForModuleOut(ctx, libName+".xml").OutputPath
1124 rule := android.NewRuleBuilder()
1125 rule.Command().
1126 Text("/bin/bash -c \"echo -e '" + xmlContent + "'\" > ").
1127 Output(module.outputFilePath)
1128
1129 rule.Build(pctx, ctx, "java_sdk_xml", "Permission XML")
1130
1131 module.installDirPath = android.PathForModuleInstall(ctx, "etc", module.SubDir())
1132}
1133
1134func (module *sdkLibraryXml) AndroidMkEntries() []android.AndroidMkEntries {
1135 if !module.IsForPlatform() {
1136 return []android.AndroidMkEntries{android.AndroidMkEntries{
1137 Disabled: true,
1138 }}
1139 }
1140
1141 return []android.AndroidMkEntries{android.AndroidMkEntries{
1142 Class: "ETC",
1143 OutputFile: android.OptionalPathForPath(module.outputFilePath),
1144 ExtraEntries: []android.AndroidMkExtraEntriesFunc{
1145 func(entries *android.AndroidMkEntries) {
1146 entries.SetString("LOCAL_MODULE_TAGS", "optional")
1147 entries.SetString("LOCAL_MODULE_PATH", module.installDirPath.ToMakePath().String())
1148 entries.SetString("LOCAL_INSTALLED_MODULE_STEM", module.outputFilePath.Base())
1149 },
1150 },
1151 }}
1152}
Paul Duffindd46f712020-02-10 13:37:10 +00001153
1154type sdkLibrarySdkMemberType struct {
1155 android.SdkMemberTypeBase
1156}
1157
1158func (s *sdkLibrarySdkMemberType) AddDependencies(mctx android.BottomUpMutatorContext, dependencyTag blueprint.DependencyTag, names []string) {
1159 mctx.AddVariationDependencies(nil, dependencyTag, names...)
1160}
1161
1162func (s *sdkLibrarySdkMemberType) IsInstance(module android.Module) bool {
1163 _, ok := module.(*SdkLibrary)
1164 return ok
1165}
1166
1167func (s *sdkLibrarySdkMemberType) AddPrebuiltModule(ctx android.SdkMemberContext, member android.SdkMember) android.BpModule {
1168 return ctx.SnapshotBuilder().AddPrebuiltModule(member, "java_sdk_library_import")
1169}
1170
1171func (s *sdkLibrarySdkMemberType) CreateVariantPropertiesStruct() android.SdkMemberProperties {
1172 return &sdkLibrarySdkMemberProperties{}
1173}
1174
1175type sdkLibrarySdkMemberProperties struct {
1176 android.SdkMemberPropertiesBase
1177
1178 // Scope to per scope properties.
1179 Scopes map[*apiScope]scopeProperties
1180
1181 // Additional libraries that the exported stubs libraries depend upon.
1182 Libs []string
Paul Duffin3d1248c2020-04-09 00:10:17 +01001183
1184 // The Java stubs source files.
1185 Stub_srcs []string
Paul Duffindd46f712020-02-10 13:37:10 +00001186}
1187
1188type scopeProperties struct {
Paul Duffin1fd005d2020-04-09 01:08:11 +01001189 Jars android.Paths
1190 StubsSrcJar android.Path
1191 CurrentApiFile android.Path
1192 RemovedApiFile android.Path
1193 SdkVersion string
Paul Duffindd46f712020-02-10 13:37:10 +00001194}
1195
1196func (s *sdkLibrarySdkMemberProperties) PopulateFromVariant(ctx android.SdkMemberContext, variant android.Module) {
1197 sdk := variant.(*SdkLibrary)
1198
1199 s.Scopes = make(map[*apiScope]scopeProperties)
1200 for _, apiScope := range allApiScopes {
1201 paths := sdk.getScopePaths(apiScope)
1202 jars := paths.stubsImplPath
1203 if len(jars) > 0 {
1204 properties := scopeProperties{}
1205 properties.Jars = jars
1206 properties.SdkVersion = apiScope.sdkVersion
Paul Duffin3d1248c2020-04-09 00:10:17 +01001207 properties.StubsSrcJar = paths.stubsSrcJar
Paul Duffin1fd005d2020-04-09 01:08:11 +01001208 properties.CurrentApiFile = paths.currentApiFilePath
1209 properties.RemovedApiFile = paths.removedApiFilePath
Paul Duffindd46f712020-02-10 13:37:10 +00001210 s.Scopes[apiScope] = properties
1211 }
1212 }
1213
1214 s.Libs = sdk.properties.Libs
1215}
1216
1217func (s *sdkLibrarySdkMemberProperties) AddToPropertySet(ctx android.SdkMemberContext, propertySet android.BpPropertySet) {
1218 for _, apiScope := range allApiScopes {
1219 if properties, ok := s.Scopes[apiScope]; ok {
1220 scopeSet := propertySet.AddPropertySet(apiScope.name)
1221
Paul Duffin3d1248c2020-04-09 00:10:17 +01001222 scopeDir := filepath.Join("sdk_library", s.OsPrefix(), apiScope.name)
1223
Paul Duffindd46f712020-02-10 13:37:10 +00001224 var jars []string
1225 for _, p := range properties.Jars {
Paul Duffin3d1248c2020-04-09 00:10:17 +01001226 dest := filepath.Join(scopeDir, ctx.Name()+"-stubs.jar")
Paul Duffindd46f712020-02-10 13:37:10 +00001227 ctx.SnapshotBuilder().CopyToSnapshot(p, dest)
1228 jars = append(jars, dest)
1229 }
1230 scopeSet.AddProperty("jars", jars)
1231
Paul Duffin3d1248c2020-04-09 00:10:17 +01001232 // Merge the stubs source jar into the snapshot zip so that when it is unpacked
1233 // the source files are also unpacked.
1234 snapshotRelativeDir := filepath.Join(scopeDir, ctx.Name()+"_stub_sources")
1235 ctx.SnapshotBuilder().UnzipToSnapshot(properties.StubsSrcJar, snapshotRelativeDir)
1236 scopeSet.AddProperty("stub_srcs", []string{snapshotRelativeDir})
1237
Paul Duffin1fd005d2020-04-09 01:08:11 +01001238 if properties.CurrentApiFile != nil {
1239 currentApiSnapshotPath := filepath.Join(scopeDir, ctx.Name()+".txt")
1240 ctx.SnapshotBuilder().CopyToSnapshot(properties.CurrentApiFile, currentApiSnapshotPath)
1241 scopeSet.AddProperty("current_api", currentApiSnapshotPath)
1242 }
1243
1244 if properties.RemovedApiFile != nil {
1245 removedApiSnapshotPath := filepath.Join(scopeDir, ctx.Name()+"-removed.txt")
1246 ctx.SnapshotBuilder().CopyToSnapshot(properties.CurrentApiFile, removedApiSnapshotPath)
1247 scopeSet.AddProperty("removed_api", removedApiSnapshotPath)
1248 }
1249
Paul Duffindd46f712020-02-10 13:37:10 +00001250 if properties.SdkVersion != "" {
1251 scopeSet.AddProperty("sdk_version", properties.SdkVersion)
1252 }
1253 }
1254 }
1255
1256 if len(s.Libs) > 0 {
1257 propertySet.AddPropertyWithTag("libs", s.Libs, ctx.SnapshotBuilder().SdkMemberReferencePropertyTag(false))
1258 }
1259}