blob: 3a6c5deb363ff1eebd24e2c8b033b4f7a52cc867 [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 Duffin51a2bee2020-05-05 14:40:52 +010070 // The api scope that this scope extends.
71 extends *apiScope
72
Paul Duffin6a2bd112020-04-07 19:27:04 +010073 // The name of the field in the dynamically created structure.
74 fieldName string
75
Paul Duffind1b3a922020-01-22 11:57:20 +000076 // The tag to use to depend on the stubs library module.
77 stubsTag scopeDependencyTag
78
79 // The tag to use to depend on the stubs
80 apiFileTag scopeDependencyTag
81
82 // The scope specific prefix to add to the api file base of "current.txt" or "removed.txt".
83 apiFilePrefix string
84
85 // The scope specific prefix to add to the sdk library module name to construct a scope specific
86 // module name.
87 moduleSuffix string
88
Paul Duffind1b3a922020-01-22 11:57:20 +000089 // 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 Duffin3c7c3472020-04-07 18:50:10 +010093
94 // Extra arguments to pass to droidstubs for this scope.
95 droidstubsArgs []string
Anton Hansson5ff28e52020-05-02 11:19:36 +010096
97 // Whether the api scope can be treated as unstable, and should skip compat checks.
98 unstable bool
Paul Duffind1b3a922020-01-22 11:57:20 +000099}
100
101// Initialize a scope, creating and adding appropriate dependency tags
102func initApiScope(scope *apiScope) *apiScope {
Paul Duffin6a2bd112020-04-07 19:27:04 +0100103 scope.fieldName = proptools.FieldNameForProperty(scope.name)
Paul Duffind1b3a922020-01-22 11:57:20 +0000104 scope.stubsTag = scopeDependencyTag{
105 name: scope.name + "-stubs",
106 apiScope: scope,
107 }
108 scope.apiFileTag = scopeDependencyTag{
109 name: scope.name + "-api",
110 apiScope: scope,
111 }
112 return scope
113}
114
115func (scope *apiScope) stubsModuleName(baseName string) string {
116 return baseName + sdkStubsLibrarySuffix + scope.moduleSuffix
117}
118
119func (scope *apiScope) docsModuleName(baseName string) string {
Paul Duffin91b883d2020-02-11 13:05:28 +0000120 return baseName + sdkStubsSourceSuffix + scope.moduleSuffix
Paul Duffind1b3a922020-01-22 11:57:20 +0000121}
122
123type apiScopes []*apiScope
124
125func (scopes apiScopes) Strings(accessor func(*apiScope) string) []string {
126 var list []string
127 for _, scope := range scopes {
128 list = append(list, accessor(scope))
129 }
130 return list
131}
132
Jiyong Parkc678ad32018-04-10 13:07:10 +0900133var (
Paul Duffind1b3a922020-01-22 11:57:20 +0000134 apiScopePublic = initApiScope(&apiScope{
135 name: "public",
136 sdkVersion: "current",
137 })
138 apiScopeSystem = initApiScope(&apiScope{
Anton Hanssone366fff2020-04-28 16:47:41 +0100139 name: "system",
Paul Duffin51a2bee2020-05-05 14:40:52 +0100140 extends: apiScopePublic,
Anton Hanssone366fff2020-04-28 16:47:41 +0100141 apiFilePrefix: "system-",
142 moduleSuffix: sdkSystemApiSuffix,
143 sdkVersion: "system_current",
144 droidstubsArgs: []string{"-showAnnotation android.annotation.SystemApi"},
Paul Duffind1b3a922020-01-22 11:57:20 +0000145 })
146 apiScopeTest = initApiScope(&apiScope{
Anton Hanssone366fff2020-04-28 16:47:41 +0100147 name: "test",
Paul Duffin51a2bee2020-05-05 14:40:52 +0100148 extends: apiScopePublic,
Anton Hanssone366fff2020-04-28 16:47:41 +0100149 apiFilePrefix: "test-",
150 moduleSuffix: sdkTestApiSuffix,
151 sdkVersion: "test_current",
152 droidstubsArgs: []string{"-showAnnotation android.annotation.TestApi"},
Anton Hansson5ff28e52020-05-02 11:19:36 +0100153 unstable: true,
Paul Duffind1b3a922020-01-22 11:57:20 +0000154 })
155 allApiScopes = apiScopes{
156 apiScopePublic,
157 apiScopeSystem,
158 apiScopeTest,
159 }
Jiyong Parkc678ad32018-04-10 13:07:10 +0900160)
161
Jiyong Park82484c02018-04-23 21:41:26 +0900162var (
163 javaSdkLibrariesLock sync.Mutex
164)
165
Jiyong Parkc678ad32018-04-10 13:07:10 +0900166// TODO: these are big features that are currently missing
Jiyong Park1be96912018-05-28 18:02:19 +0900167// 1) disallowing linking to the runtime shared lib
168// 2) HTML generation
Jiyong Parkc678ad32018-04-10 13:07:10 +0900169
170func init() {
Paul Duffin43dc1cc2019-12-19 11:18:54 +0000171 RegisterSdkLibraryBuildComponents(android.InitRegistrationContext)
Jiyong Parkc678ad32018-04-10 13:07:10 +0900172
Jiyong Park82484c02018-04-23 21:41:26 +0900173 android.RegisterMakeVarsProvider(pctx, func(ctx android.MakeVarsContext) {
174 javaSdkLibraries := javaSdkLibraries(ctx.Config())
175 sort.Strings(*javaSdkLibraries)
176 ctx.Strict("JAVA_SDK_LIBRARIES", strings.Join(*javaSdkLibraries, " "))
177 })
Paul Duffin61871622020-02-10 13:37:10 +0000178
179 // Register sdk member types.
180 android.RegisterSdkMemberType(&sdkLibrarySdkMemberType{
181 android.SdkMemberTypeBase{
182 PropertyName: "java_sdk_libs",
183 SupportsSdk: true,
184 },
185 })
Jiyong Parkc678ad32018-04-10 13:07:10 +0900186}
187
Paul Duffin43dc1cc2019-12-19 11:18:54 +0000188func RegisterSdkLibraryBuildComponents(ctx android.RegistrationContext) {
189 ctx.RegisterModuleType("java_sdk_library", SdkLibraryFactory)
190 ctx.RegisterModuleType("java_sdk_library_import", sdkLibraryImportFactory)
191}
192
Jiyong Parkc678ad32018-04-10 13:07:10 +0900193type sdkLibraryProperties struct {
Sundong Ahnf043cf62018-06-25 16:04:37 +0900194 // List of Java libraries that will be in the classpath when building stubs
195 Stub_only_libs []string `android:"arch_variant"`
196
Paul Duffin7a586d32019-12-30 17:09:34 +0000197 // list of package names that will be documented and publicized as API.
198 // This allows the API to be restricted to a subset of the source files provided.
199 // If this is unspecified then all the source files will be treated as being part
200 // of the API.
Jiyong Parkc678ad32018-04-10 13:07:10 +0900201 Api_packages []string
202
Jiyong Park5a2c9d72018-05-01 22:25:41 +0900203 // list of package names that must be hidden from the API
204 Hidden_api_packages []string
205
Paul Duffin749f98f2019-12-30 17:23:46 +0000206 // the relative path to the directory containing the api specification files.
207 // Defaults to "api".
208 Api_dir *string
209
Paul Duffin43db9be2019-12-30 17:35:49 +0000210 // If set to true there is no runtime library.
211 Api_only *bool
212
Paul Duffin11512472019-02-11 15:55:17 +0000213 // local files that are used within user customized droiddoc options.
214 Droiddoc_option_files []string
215
216 // additional droiddoc options
217 // Available variables for substitution:
218 //
219 // $(location <label>): the path to the droiddoc_option_files with name <label>
Sundong Ahndd567f92018-07-31 17:19:11 +0900220 Droiddoc_options []string
221
Sundong Ahn054b19a2018-10-19 13:46:09 +0900222 // a list of top-level directories containing files to merge qualifier annotations
223 // (i.e. those intended to be included in the stubs written) from.
224 Merge_annotations_dirs []string
225
226 // a list of top-level directories containing Java stub files to merge show/hide annotations from.
227 Merge_inclusion_annotations_dirs []string
228
229 // If set to true, the path of dist files is apistubs/core. Defaults to false.
230 Core_lib *bool
231
Sundong Ahn80a87b32019-05-13 15:02:50 +0900232 // don't create dist rules.
233 No_dist *bool `blueprint:"mutated"`
234
Paul Duffin37e0b772019-12-30 17:20:10 +0000235 // indicates whether system and test apis should be managed.
236 Has_system_and_test_apis bool `blueprint:"mutated"`
237
Jiyong Parkc678ad32018-04-10 13:07:10 +0900238 // TODO: determines whether to create HTML doc or not
239 //Html_doc *bool
240}
241
Paul Duffind1b3a922020-01-22 11:57:20 +0000242type scopePaths struct {
243 stubsHeaderPath android.Paths
244 stubsImplPath android.Paths
245 apiFilePath android.Path
246}
247
Paul Duffin56d44902020-01-31 13:36:25 +0000248// Common code between sdk library and sdk library import
249type commonToSdkLibraryAndImport struct {
250 scopePaths map[*apiScope]*scopePaths
251}
252
253func (c *commonToSdkLibraryAndImport) getScopePaths(scope *apiScope) *scopePaths {
254 if c.scopePaths == nil {
255 c.scopePaths = make(map[*apiScope]*scopePaths)
256 }
257 paths := c.scopePaths[scope]
258 if paths == nil {
259 paths = &scopePaths{}
260 c.scopePaths[scope] = paths
261 }
262
263 return paths
264}
265
Inseob Kimc0907f12019-02-08 21:00:45 +0900266type SdkLibrary struct {
Sundong Ahn054b19a2018-10-19 13:46:09 +0900267 Library
Jiyong Parkc678ad32018-04-10 13:07:10 +0900268
Sundong Ahn054b19a2018-10-19 13:46:09 +0900269 sdkLibraryProperties sdkLibraryProperties
Jiyong Parkc678ad32018-04-10 13:07:10 +0900270
Paul Duffin56d44902020-01-31 13:36:25 +0000271 commonToSdkLibraryAndImport
Jiyong Parkc678ad32018-04-10 13:07:10 +0900272}
273
Inseob Kimc0907f12019-02-08 21:00:45 +0900274var _ Dependency = (*SdkLibrary)(nil)
275var _ SdkLibraryDependency = (*SdkLibrary)(nil)
Colin Cross897d2ed2019-02-11 14:03:51 -0800276
Paul Duffind1b3a922020-01-22 11:57:20 +0000277func (module *SdkLibrary) getActiveApiScopes() apiScopes {
278 if module.sdkLibraryProperties.Has_system_and_test_apis {
279 return allApiScopes
280 } else {
281 return apiScopes{apiScopePublic}
282 }
283}
284
Paul Duffine74ac732020-02-06 13:51:46 +0000285var xmlPermissionsFileTag = dependencyTag{name: "xml-permissions-file"}
286
Jiyong Parke3833882020-02-17 17:28:10 +0900287func IsXmlPermissionsFileDepTag(depTag blueprint.DependencyTag) bool {
288 if dt, ok := depTag.(dependencyTag); ok {
289 return dt == xmlPermissionsFileTag
290 }
291 return false
292}
293
Inseob Kimc0907f12019-02-08 21:00:45 +0900294func (module *SdkLibrary) DepsMutator(ctx android.BottomUpMutatorContext) {
Paul Duffind1b3a922020-01-22 11:57:20 +0000295 for _, apiScope := range module.getActiveApiScopes() {
296 // Add dependencies to the stubs library
Paul Duffin50061512020-01-21 16:31:05 +0000297 ctx.AddVariationDependencies(nil, apiScope.stubsTag, module.stubsName(apiScope))
Paul Duffind1b3a922020-01-22 11:57:20 +0000298
Paul Duffin50061512020-01-21 16:31:05 +0000299 // And the api file
Paul Duffind1b3a922020-01-22 11:57:20 +0000300 ctx.AddVariationDependencies(nil, apiScope.apiFileTag, module.docsName(apiScope))
Sundong Ahn054b19a2018-10-19 13:46:09 +0900301 }
302
Paul Duffine74ac732020-02-06 13:51:46 +0000303 if !proptools.Bool(module.sdkLibraryProperties.Api_only) {
304 // Add dependency to the rule for generating the xml permissions file
Jiyong Parke3833882020-02-17 17:28:10 +0900305 ctx.AddDependency(module, xmlPermissionsFileTag, module.xmlFileName())
Paul Duffine74ac732020-02-06 13:51:46 +0000306 }
307
Sundong Ahn054b19a2018-10-19 13:46:09 +0900308 module.Library.deps(ctx)
Jiyong Parkc678ad32018-04-10 13:07:10 +0900309}
310
Inseob Kimc0907f12019-02-08 21:00:45 +0900311func (module *SdkLibrary) GenerateAndroidBuildActions(ctx android.ModuleContext) {
Paul Duffin43db9be2019-12-30 17:35:49 +0000312 // Don't build an implementation library if this is api only.
313 if !proptools.Bool(module.sdkLibraryProperties.Api_only) {
314 module.Library.GenerateAndroidBuildActions(ctx)
315 }
Sundong Ahn054b19a2018-10-19 13:46:09 +0900316
Sundong Ahn57368eb2018-07-06 11:20:23 +0900317 // Record the paths to the header jars of the library (stubs and impl).
Paul Duffind1b3a922020-01-22 11:57:20 +0000318 // When this java_sdk_library is depended upon from others via "libs" property,
Jiyong Parkc678ad32018-04-10 13:07:10 +0900319 // the recorded paths will be returned depending on the link type of the caller.
320 ctx.VisitDirectDeps(func(to android.Module) {
321 otherName := ctx.OtherModuleName(to)
322 tag := ctx.OtherModuleDependencyTag(to)
323
Sundong Ahn57368eb2018-07-06 11:20:23 +0900324 if lib, ok := to.(Dependency); ok {
Paul Duffind1b3a922020-01-22 11:57:20 +0000325 if scopeTag, ok := tag.(scopeDependencyTag); ok {
326 apiScope := scopeTag.apiScope
327 scopePaths := module.getScopePaths(apiScope)
328 scopePaths.stubsHeaderPath = lib.HeaderJars()
329 scopePaths.stubsImplPath = lib.ImplementationJars()
Jiyong Parkc678ad32018-04-10 13:07:10 +0900330 }
331 }
Sundong Ahn20e998b2018-07-24 11:19:26 +0900332 if doc, ok := to.(ApiFilePath); ok {
Paul Duffind1b3a922020-01-22 11:57:20 +0000333 if scopeTag, ok := tag.(scopeDependencyTag); ok {
334 apiScope := scopeTag.apiScope
335 scopePaths := module.getScopePaths(apiScope)
336 scopePaths.apiFilePath = doc.ApiFilePath()
337 } 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 Hansson6bb88102020-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 Hansson6bb88102020-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 Hansson6bb88102020-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 Duffincbcfcaa2020-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 Hansson6bb88102020-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 Duffinda364252020-04-28 14:08:32 +0100506 // The stubs source processing uses the same compile time classpath when extracting the
507 // API from the implementation library as it does when compiling it. i.e. the same
508 // * sdk version
509 // * system_modules
510 // * libs (static_libs/libs)
Paul Duffin250e6192019-06-07 10:44:37 +0100511
Jiyong Parkdf130542018-04-27 16:29:21 +0900512 props.Name = proptools.StringPtr(module.docsName(apiScope))
Sundong Ahn054b19a2018-10-19 13:46:09 +0900513 props.Srcs = append(props.Srcs, module.Library.Module.properties.Srcs...)
Paul Duffinda364252020-04-28 14:08:32 +0100514 props.Sdk_version = module.Library.Module.deviceProperties.Sdk_version
Paul Duffin12ceb462019-12-24 20:31:31 +0000515 props.System_modules = module.Library.Module.deviceProperties.System_modules
Jiyong Parkc678ad32018-04-10 13:07:10 +0900516 props.Installable = proptools.BoolPtr(false)
Sundong Ahne6f0b052018-06-05 16:46:14 +0900517 // A droiddoc module has only one Libs property and doesn't distinguish between
518 // shared libs and static libs. So we need to add both of these libs to Libs property.
Sundong Ahn054b19a2018-10-19 13:46:09 +0900519 props.Libs = module.Library.Module.properties.Libs
520 props.Libs = append(props.Libs, module.Library.Module.properties.Static_libs...)
521 props.Aidl.Include_dirs = module.Library.Module.deviceProperties.Aidl.Include_dirs
522 props.Aidl.Local_include_dirs = module.Library.Module.deviceProperties.Aidl.Local_include_dirs
Sundong Ahn054b19a2018-10-19 13:46:09 +0900523 props.Java_version = module.Library.Module.properties.Java_version
Jiyong Parkc678ad32018-04-10 13:07:10 +0900524
Sundong Ahn054b19a2018-10-19 13:46:09 +0900525 props.Merge_annotations_dirs = module.sdkLibraryProperties.Merge_annotations_dirs
526 props.Merge_inclusion_annotations_dirs = module.sdkLibraryProperties.Merge_inclusion_annotations_dirs
527
Paul Duffincbcfcaa2020-04-07 18:49:53 +0100528 droidstubsArgs := []string{}
Paul Duffin235ffff2019-12-24 10:41:30 +0000529 if len(module.sdkLibraryProperties.Api_packages) != 0 {
Paul Duffincbcfcaa2020-04-07 18:49:53 +0100530 droidstubsArgs = append(droidstubsArgs, "--stub-packages "+strings.Join(module.sdkLibraryProperties.Api_packages, ":"))
Paul Duffin235ffff2019-12-24 10:41:30 +0000531 }
532 if len(module.sdkLibraryProperties.Hidden_api_packages) != 0 {
Paul Duffincbcfcaa2020-04-07 18:49:53 +0100533 droidstubsArgs = append(droidstubsArgs,
Paul Duffin235ffff2019-12-24 10:41:30 +0000534 android.JoinWithPrefix(module.sdkLibraryProperties.Hidden_api_packages, " --hide-package "))
535 }
Paul Duffincbcfcaa2020-04-07 18:49:53 +0100536 droidstubsArgs = append(droidstubsArgs, module.sdkLibraryProperties.Droiddoc_options...)
Paul Duffin235ffff2019-12-24 10:41:30 +0000537 disabledWarnings := []string{
538 "MissingPermission",
539 "BroadcastBehavior",
540 "HiddenSuperclass",
541 "DeprecationMismatch",
542 "UnavailableSymbol",
543 "SdkConstant",
544 "HiddenTypeParameter",
545 "Todo",
546 "Typo",
547 }
Paul Duffincbcfcaa2020-04-07 18:49:53 +0100548 droidstubsArgs = append(droidstubsArgs, android.JoinWithPrefix(disabledWarnings, "--hide "))
Sundong Ahnfb2721f2018-09-17 13:23:09 +0900549
Paul Duffin3c7c3472020-04-07 18:50:10 +0100550 // Add in scope specific arguments.
551 droidstubsArgs = append(droidstubsArgs, apiScope.droidstubsArgs...)
Paul Duffin11512472019-02-11 15:55:17 +0000552 props.Arg_files = module.sdkLibraryProperties.Droiddoc_option_files
Paul Duffincbcfcaa2020-04-07 18:49:53 +0100553 props.Args = proptools.StringPtr(strings.Join(droidstubsArgs, " "))
Jiyong Parkc678ad32018-04-10 13:07:10 +0900554
555 // List of APIs identified from the provided source files are created. They are later
556 // compared against to the not-yet-released (a.k.a current) list of APIs and to the
557 // last-released (a.k.a numbered) list of API.
Paul Duffind1b3a922020-01-22 11:57:20 +0000558 currentApiFileName := apiScope.apiFilePrefix + "current.txt"
559 removedApiFileName := apiScope.apiFilePrefix + "removed.txt"
Paul Duffin749f98f2019-12-30 17:23:46 +0000560 apiDir := module.getApiDir()
561 currentApiFileName = path.Join(apiDir, currentApiFileName)
562 removedApiFileName = path.Join(apiDir, removedApiFileName)
Jiyong Parkc678ad32018-04-10 13:07:10 +0900563
Jiyong Park58c518b2018-05-12 22:29:12 +0900564 // check against the not-yet-release API
565 props.Check_api.Current.Api_file = proptools.StringPtr(currentApiFileName)
566 props.Check_api.Current.Removed_api_file = proptools.StringPtr(removedApiFileName)
Jiyong Park58c518b2018-05-12 22:29:12 +0900567
Anton Hansson5ff28e52020-05-02 11:19:36 +0100568 if !apiScope.unstable {
569 // check against the latest released API
570 props.Check_api.Last_released.Api_file = proptools.StringPtr(
571 module.latestApiFilegroupName(apiScope))
572 props.Check_api.Last_released.Removed_api_file = proptools.StringPtr(
573 module.latestRemovedApiFilegroupName(apiScope))
574 props.Check_api.Ignore_missing_latest_api = proptools.BoolPtr(true)
575 }
Jiyong Park58c518b2018-05-12 22:29:12 +0900576
Anton Hansson6bb88102020-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 Hanb8fa86a2020-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 Hanb8fa86a2020-03-10 06:23:13 +0900604 Apex_available []string
Jiyong Parke3833882020-02-17 17:28:10 +0900605 }{
Jooyung Hanb8fa86a2020-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
Colin Cross79c7c262019-04-17 11:11:46 -0700819}
820
Paul Duffin56d44902020-01-31 13:36:25 +0000821type sdkLibraryImportProperties struct {
Paul Duffinfcfd7912020-01-31 17:54:30 +0000822 // List of shared java libs, common to all scopes, that this module has
823 // dependencies to
824 Libs []string
Paul Duffin56d44902020-01-31 13:36:25 +0000825}
826
Colin Cross79c7c262019-04-17 11:11:46 -0700827type sdkLibraryImport struct {
828 android.ModuleBase
829 android.DefaultableModuleBase
830 prebuilt android.Prebuilt
Paul Duffin61871622020-02-10 13:37:10 +0000831 android.ApexModuleBase
832 android.SdkBase
Colin Cross79c7c262019-04-17 11:11:46 -0700833
834 properties sdkLibraryImportProperties
835
Paul Duffin6a2bd112020-04-07 19:27:04 +0100836 // Map from api scope to the scope specific property structure.
837 scopeProperties map[*apiScope]*sdkLibraryScopeProperties
838
Paul Duffin56d44902020-01-31 13:36:25 +0000839 commonToSdkLibraryAndImport
Colin Cross79c7c262019-04-17 11:11:46 -0700840}
841
842var _ SdkLibraryDependency = (*sdkLibraryImport)(nil)
843
Paul Duffin6a2bd112020-04-07 19:27:04 +0100844// The type of a structure that contains a field of type sdkLibraryScopeProperties
845// for each apiscope in allApiScopes, e.g. something like:
846// struct {
847// Public sdkLibraryScopeProperties
848// System sdkLibraryScopeProperties
849// ...
850// }
851var allScopeStructType = createAllScopePropertiesStructType()
852
853// Dynamically create a structure type for each apiscope in allApiScopes.
854func createAllScopePropertiesStructType() reflect.Type {
855 var fields []reflect.StructField
856 for _, apiScope := range allApiScopes {
857 field := reflect.StructField{
858 Name: apiScope.fieldName,
859 Type: reflect.TypeOf(sdkLibraryScopeProperties{}),
860 }
861 fields = append(fields, field)
862 }
863
864 return reflect.StructOf(fields)
865}
866
867// Create an instance of the scope specific structure type and return a map
868// from apiscope to a pointer to each scope specific field.
869func createPropertiesInstance() (interface{}, map[*apiScope]*sdkLibraryScopeProperties) {
870 allScopePropertiesPtr := reflect.New(allScopeStructType)
871 allScopePropertiesStruct := allScopePropertiesPtr.Elem()
872 scopeProperties := make(map[*apiScope]*sdkLibraryScopeProperties)
873
874 for _, apiScope := range allApiScopes {
875 field := allScopePropertiesStruct.FieldByName(apiScope.fieldName)
876 scopeProperties[apiScope] = field.Addr().Interface().(*sdkLibraryScopeProperties)
877 }
878
879 return allScopePropertiesPtr.Interface(), scopeProperties
880}
881
Jaewoong Jung4f158ee2019-07-11 10:05:35 -0700882// java_sdk_library_import imports a prebuilt java_sdk_library.
Colin Cross79c7c262019-04-17 11:11:46 -0700883func sdkLibraryImportFactory() android.Module {
884 module := &sdkLibraryImport{}
885
Paul Duffin6a2bd112020-04-07 19:27:04 +0100886 allScopeProperties, scopeToProperties := createPropertiesInstance()
887 module.scopeProperties = scopeToProperties
888 module.AddProperties(&module.properties, allScopeProperties)
Colin Cross79c7c262019-04-17 11:11:46 -0700889
Paul Duffin0bdcb272020-02-06 15:24:57 +0000890 android.InitPrebuiltModule(module, &[]string{""})
Paul Duffin61871622020-02-10 13:37:10 +0000891 android.InitApexModule(module)
892 android.InitSdkAwareModule(module)
Colin Cross79c7c262019-04-17 11:11:46 -0700893 InitJavaModule(module, android.HostAndDeviceSupported)
894
895 android.AddLoadHook(module, func(mctx android.LoadHookContext) { module.createInternalModules(mctx) })
896 return module
897}
898
899func (module *sdkLibraryImport) Prebuilt() *android.Prebuilt {
900 return &module.prebuilt
901}
902
903func (module *sdkLibraryImport) Name() string {
904 return module.prebuilt.Name(module.ModuleBase.Name())
905}
906
907func (module *sdkLibraryImport) createInternalModules(mctx android.LoadHookContext) {
Colin Cross79c7c262019-04-17 11:11:46 -0700908
Paul Duffin50061512020-01-21 16:31:05 +0000909 // If the build is configured to use prebuilts then force this to be preferred.
910 if mctx.Config().UnbundledBuildUsePrebuiltSdks() {
911 module.prebuilt.ForcePrefer()
912 }
913
Paul Duffin6a2bd112020-04-07 19:27:04 +0100914 for apiScope, scopeProperties := range module.scopeProperties {
Paul Duffin56d44902020-01-31 13:36:25 +0000915 if len(scopeProperties.Jars) == 0 {
916 continue
917 }
918
Paul Duffinf6155722020-04-09 00:07:11 +0100919 module.createJavaImportForStubs(mctx, apiScope, scopeProperties)
Paul Duffin56d44902020-01-31 13:36:25 +0000920 }
Colin Cross79c7c262019-04-17 11:11:46 -0700921
922 javaSdkLibraries := javaSdkLibraries(mctx.Config())
923 javaSdkLibrariesLock.Lock()
924 defer javaSdkLibrariesLock.Unlock()
925 *javaSdkLibraries = append(*javaSdkLibraries, module.BaseModuleName())
926}
927
Paul Duffinf6155722020-04-09 00:07:11 +0100928func (module *sdkLibraryImport) createJavaImportForStubs(mctx android.LoadHookContext, apiScope *apiScope, scopeProperties *sdkLibraryScopeProperties) {
929 // Creates a java import for the jar with ".stubs" suffix
930 props := struct {
931 Name *string
932 Soc_specific *bool
933 Device_specific *bool
934 Product_specific *bool
935 System_ext_specific *bool
936 Sdk_version *string
937 Libs []string
938 Jars []string
939 Prefer *bool
940 }{}
941 props.Name = proptools.StringPtr(apiScope.stubsModuleName(module.BaseModuleName()))
942 props.Sdk_version = scopeProperties.Sdk_version
943 // Prepend any of the libs from the legacy public properties to the libs for each of the
944 // scopes to avoid having to duplicate them in each scope.
945 props.Libs = append(module.properties.Libs, scopeProperties.Libs...)
946 props.Jars = scopeProperties.Jars
947 if module.SocSpecific() {
948 props.Soc_specific = proptools.BoolPtr(true)
949 } else if module.DeviceSpecific() {
950 props.Device_specific = proptools.BoolPtr(true)
951 } else if module.ProductSpecific() {
952 props.Product_specific = proptools.BoolPtr(true)
953 } else if module.SystemExtSpecific() {
954 props.System_ext_specific = proptools.BoolPtr(true)
955 }
956 // If the build should use prebuilt sdks then set prefer to true on the stubs library.
957 // That will cause the prebuilt version of the stubs to override the source version.
958 if mctx.Config().UnbundledBuildUsePrebuiltSdks() {
959 props.Prefer = proptools.BoolPtr(true)
960 }
961 mctx.CreateModule(ImportFactory, &props)
962}
963
Colin Cross79c7c262019-04-17 11:11:46 -0700964func (module *sdkLibraryImport) DepsMutator(ctx android.BottomUpMutatorContext) {
Paul Duffin6a2bd112020-04-07 19:27:04 +0100965 for apiScope, scopeProperties := range module.scopeProperties {
Paul Duffin56d44902020-01-31 13:36:25 +0000966 if len(scopeProperties.Jars) == 0 {
967 continue
968 }
969
970 // Add dependencies to the prebuilt stubs library
971 ctx.AddVariationDependencies(nil, apiScope.stubsTag, apiScope.stubsModuleName(module.BaseModuleName()))
972 }
Colin Cross79c7c262019-04-17 11:11:46 -0700973}
974
975func (module *sdkLibraryImport) GenerateAndroidBuildActions(ctx android.ModuleContext) {
976 // Record the paths to the prebuilt stubs library.
977 ctx.VisitDirectDeps(func(to android.Module) {
978 tag := ctx.OtherModuleDependencyTag(to)
979
Paul Duffin56d44902020-01-31 13:36:25 +0000980 if lib, ok := to.(Dependency); ok {
981 if scopeTag, ok := tag.(scopeDependencyTag); ok {
982 apiScope := scopeTag.apiScope
983 scopePaths := module.getScopePaths(apiScope)
984 scopePaths.stubsHeaderPath = lib.HeaderJars()
985 }
Colin Cross79c7c262019-04-17 11:11:46 -0700986 }
987 })
988}
989
Paul Duffin56d44902020-01-31 13:36:25 +0000990func (module *sdkLibraryImport) sdkJars(
991 ctx android.BaseModuleContext,
992 sdkVersion sdkSpec) android.Paths {
993
Paul Duffin50061512020-01-21 16:31:05 +0000994 // If a specific numeric version has been requested then use prebuilt versions of the sdk.
995 if sdkVersion.version.isNumbered() {
996 return PrebuiltJars(ctx, module.BaseModuleName(), sdkVersion)
997 }
998
Paul Duffin56d44902020-01-31 13:36:25 +0000999 var apiScope *apiScope
1000 switch sdkVersion.kind {
1001 case sdkSystem:
1002 apiScope = apiScopeSystem
1003 case sdkTest:
1004 apiScope = apiScopeTest
1005 default:
1006 apiScope = apiScopePublic
1007 }
1008
1009 paths := module.getScopePaths(apiScope)
1010 return paths.stubsHeaderPath
1011}
1012
Colin Cross79c7c262019-04-17 11:11:46 -07001013// to satisfy SdkLibraryDependency interface
Jiyong Park6a927c42020-01-21 02:03:43 +09001014func (module *sdkLibraryImport) SdkHeaderJars(ctx android.BaseModuleContext, sdkVersion sdkSpec) android.Paths {
Colin Cross79c7c262019-04-17 11:11:46 -07001015 // This module is just a wrapper for the prebuilt stubs.
Paul Duffin56d44902020-01-31 13:36:25 +00001016 return module.sdkJars(ctx, sdkVersion)
Colin Cross79c7c262019-04-17 11:11:46 -07001017}
1018
1019// to satisfy SdkLibraryDependency interface
Jiyong Park6a927c42020-01-21 02:03:43 +09001020func (module *sdkLibraryImport) SdkImplementationJars(ctx android.BaseModuleContext, sdkVersion sdkSpec) android.Paths {
Colin Cross79c7c262019-04-17 11:11:46 -07001021 // This module is just a wrapper for the stubs.
Paul Duffin56d44902020-01-31 13:36:25 +00001022 return module.sdkJars(ctx, sdkVersion)
Colin Cross79c7c262019-04-17 11:11:46 -07001023}
Jiyong Parke3833882020-02-17 17:28:10 +09001024
1025//
1026// java_sdk_library_xml
1027//
1028type sdkLibraryXml struct {
1029 android.ModuleBase
1030 android.DefaultableModuleBase
1031 android.ApexModuleBase
1032
1033 properties sdkLibraryXmlProperties
1034
1035 outputFilePath android.OutputPath
1036 installDirPath android.InstallPath
1037}
1038
1039type sdkLibraryXmlProperties struct {
1040 // canonical name of the lib
1041 Lib_name *string
1042}
1043
1044// java_sdk_library_xml builds the permission xml file for a java_sdk_library.
1045// Not to be used directly by users. java_sdk_library internally uses this.
1046func sdkLibraryXmlFactory() android.Module {
1047 module := &sdkLibraryXml{}
1048
1049 module.AddProperties(&module.properties)
1050
1051 android.InitApexModule(module)
1052 android.InitAndroidArchModule(module, android.DeviceSupported, android.MultilibCommon)
1053
1054 return module
1055}
1056
1057// from android.PrebuiltEtcModule
1058func (module *sdkLibraryXml) SubDir() string {
1059 return "permissions"
1060}
1061
1062// from android.PrebuiltEtcModule
1063func (module *sdkLibraryXml) OutputFile() android.OutputPath {
1064 return module.outputFilePath
1065}
1066
1067// from android.ApexModule
1068func (module *sdkLibraryXml) AvailableFor(what string) bool {
1069 return true
1070}
1071
1072func (module *sdkLibraryXml) DepsMutator(ctx android.BottomUpMutatorContext) {
1073 // do nothing
1074}
1075
1076// File path to the runtime implementation library
1077func (module *sdkLibraryXml) implPath() string {
1078 implName := proptools.String(module.properties.Lib_name)
1079 if apexName := module.ApexName(); apexName != "" {
1080 // TODO(b/146468504): ApexName() is only a soong module name, not apex name.
1081 // In most cases, this works fine. But when apex_name is set or override_apex is used
1082 // this can be wrong.
1083 return fmt.Sprintf("/apex/%s/javalib/%s.jar", apexName, implName)
1084 }
1085 partition := "system"
1086 if module.SocSpecific() {
1087 partition = "vendor"
1088 } else if module.DeviceSpecific() {
1089 partition = "odm"
1090 } else if module.ProductSpecific() {
1091 partition = "product"
1092 } else if module.SystemExtSpecific() {
1093 partition = "system_ext"
1094 }
1095 return "/" + partition + "/framework/" + implName + ".jar"
1096}
1097
1098func (module *sdkLibraryXml) GenerateAndroidBuildActions(ctx android.ModuleContext) {
1099 libName := proptools.String(module.properties.Lib_name)
1100 xmlContent := fmt.Sprintf(permissionsTemplate, libName, module.implPath())
1101
1102 module.outputFilePath = android.PathForModuleOut(ctx, libName+".xml").OutputPath
1103 rule := android.NewRuleBuilder()
1104 rule.Command().
1105 Text("/bin/bash -c \"echo -e '" + xmlContent + "'\" > ").
1106 Output(module.outputFilePath)
1107
1108 rule.Build(pctx, ctx, "java_sdk_xml", "Permission XML")
1109
1110 module.installDirPath = android.PathForModuleInstall(ctx, "etc", module.SubDir())
1111}
1112
1113func (module *sdkLibraryXml) AndroidMkEntries() []android.AndroidMkEntries {
1114 if !module.IsForPlatform() {
1115 return []android.AndroidMkEntries{android.AndroidMkEntries{
1116 Disabled: true,
1117 }}
1118 }
1119
1120 return []android.AndroidMkEntries{android.AndroidMkEntries{
1121 Class: "ETC",
1122 OutputFile: android.OptionalPathForPath(module.outputFilePath),
1123 ExtraEntries: []android.AndroidMkExtraEntriesFunc{
1124 func(entries *android.AndroidMkEntries) {
1125 entries.SetString("LOCAL_MODULE_TAGS", "optional")
1126 entries.SetString("LOCAL_MODULE_PATH", module.installDirPath.ToMakePath().String())
1127 entries.SetString("LOCAL_INSTALLED_MODULE_STEM", module.outputFilePath.Base())
1128 },
1129 },
1130 }}
1131}
Paul Duffin61871622020-02-10 13:37:10 +00001132
1133type sdkLibrarySdkMemberType struct {
1134 android.SdkMemberTypeBase
1135}
1136
1137func (s *sdkLibrarySdkMemberType) AddDependencies(mctx android.BottomUpMutatorContext, dependencyTag blueprint.DependencyTag, names []string) {
1138 mctx.AddVariationDependencies(nil, dependencyTag, names...)
1139}
1140
1141func (s *sdkLibrarySdkMemberType) IsInstance(module android.Module) bool {
1142 _, ok := module.(*SdkLibrary)
1143 return ok
1144}
1145
1146func (s *sdkLibrarySdkMemberType) AddPrebuiltModule(ctx android.SdkMemberContext, member android.SdkMember) android.BpModule {
1147 return ctx.SnapshotBuilder().AddPrebuiltModule(member, "java_sdk_library_import")
1148}
1149
1150func (s *sdkLibrarySdkMemberType) CreateVariantPropertiesStruct() android.SdkMemberProperties {
1151 return &sdkLibrarySdkMemberProperties{}
1152}
1153
1154type sdkLibrarySdkMemberProperties struct {
1155 android.SdkMemberPropertiesBase
1156
1157 // Scope to per scope properties.
1158 Scopes map[*apiScope]scopeProperties
1159
1160 // Additional libraries that the exported stubs libraries depend upon.
1161 Libs []string
1162}
1163
1164type scopeProperties struct {
1165 Jars android.Paths
1166 SdkVersion string
1167}
1168
1169func (s *sdkLibrarySdkMemberProperties) PopulateFromVariant(ctx android.SdkMemberContext, variant android.Module) {
1170 sdk := variant.(*SdkLibrary)
1171
1172 s.Scopes = make(map[*apiScope]scopeProperties)
1173 for _, apiScope := range allApiScopes {
1174 paths := sdk.getScopePaths(apiScope)
1175 jars := paths.stubsImplPath
1176 if len(jars) > 0 {
1177 properties := scopeProperties{}
1178 properties.Jars = jars
1179 properties.SdkVersion = apiScope.sdkVersion
1180 s.Scopes[apiScope] = properties
1181 }
1182 }
1183
1184 s.Libs = sdk.properties.Libs
1185}
1186
1187func (s *sdkLibrarySdkMemberProperties) AddToPropertySet(ctx android.SdkMemberContext, propertySet android.BpPropertySet) {
1188 for _, apiScope := range allApiScopes {
1189 if properties, ok := s.Scopes[apiScope]; ok {
1190 scopeSet := propertySet.AddPropertySet(apiScope.name)
1191
1192 var jars []string
1193 for _, p := range properties.Jars {
1194 dest := filepath.Join("sdk_library", s.OsPrefix(), apiScope.name, ctx.Name()+"-stubs.jar")
1195 ctx.SnapshotBuilder().CopyToSnapshot(p, dest)
1196 jars = append(jars, dest)
1197 }
1198 scopeSet.AddProperty("jars", jars)
1199
1200 if properties.SdkVersion != "" {
1201 scopeSet.AddProperty("sdk_version", properties.SdkVersion)
1202 }
1203 }
1204 }
1205
1206 if len(s.Libs) > 0 {
1207 propertySet.AddPropertyWithTag("libs", s.Libs, ctx.SnapshotBuilder().SdkMemberReferencePropertyTag(false))
1208 }
1209}