blob: 1fccbbf5284421a1efe048af85d49007e91d9edc [file] [log] [blame]
Jiyong Parkc678ad32018-04-10 13:07:10 +09001// Copyright 2018 Google Inc. All rights reserved.
2//
3// Licensed under the Apache License, Version 2.0 (the "License");
4// you may not use this file except in compliance with the License.
5// You may obtain a copy of the License at
6//
7// http://www.apache.org/licenses/LICENSE-2.0
8//
9// Unless required by applicable law or agreed to in writing, software
10// distributed under the License is distributed on an "AS IS" BASIS,
11// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12// See the License for the specific language governing permissions and
13// limitations under the License.
14
15package java
16
17import (
Jiyong Parkc678ad32018-04-10 13:07:10 +090018 "fmt"
19 "path"
Sundong Ahn054b19a2018-10-19 13:46:09 +090020 "path/filepath"
Paul Duffin6a2bd112020-04-07 19:27:04 +010021 "reflect"
Jiyong Park82484c02018-04-23 21:41:26 +090022 "sort"
Jiyong Parkc678ad32018-04-10 13:07:10 +090023 "strings"
Jiyong Park82484c02018-04-23 21:41:26 +090024 "sync"
Jiyong Parkc678ad32018-04-10 13:07:10 +090025
Paul Duffind1b3a922020-01-22 11:57:20 +000026 "github.com/google/blueprint"
Jiyong Parkc678ad32018-04-10 13:07:10 +090027 "github.com/google/blueprint/proptools"
Paul Duffin6a2bd112020-04-07 19:27:04 +010028
29 "android/soong/android"
Jiyong Parkc678ad32018-04-10 13:07:10 +090030)
31
Jooyung Han58f26ab2019-12-18 15:34:32 +090032const (
Jiyong Parkc678ad32018-04-10 13:07:10 +090033 sdkStubsLibrarySuffix = ".stubs"
34 sdkSystemApiSuffix = ".system"
Jiyong Parkdf130542018-04-27 16:29:21 +090035 sdkTestApiSuffix = ".test"
Paul Duffin91b883d2020-02-11 13:05:28 +000036 sdkStubsSourceSuffix = ".stubs.source"
Jiyong Parkc678ad32018-04-10 13:07:10 +090037 sdkXmlFileSuffix = ".xml"
Jiyong Parke3833882020-02-17 17:28:10 +090038 permissionsTemplate = `<?xml version=\"1.0\" encoding=\"utf-8\"?>\n` +
Jooyung Han624058e2019-12-24 18:38:06 +090039 `<!-- Copyright (C) 2018 The Android Open Source Project\n` +
40 `\n` +
Jiyong Parke3833882020-02-17 17:28:10 +090041 ` Licensed under the Apache License, Version 2.0 (the \"License\");\n` +
Jooyung Han624058e2019-12-24 18:38:06 +090042 ` you may not use this file except in compliance with the License.\n` +
43 ` You may obtain a copy of the License at\n` +
44 `\n` +
45 ` http://www.apache.org/licenses/LICENSE-2.0\n` +
46 `\n` +
47 ` Unless required by applicable law or agreed to in writing, software\n` +
Jiyong Parke3833882020-02-17 17:28:10 +090048 ` distributed under the License is distributed on an \"AS IS\" BASIS,\n` +
Jooyung Han624058e2019-12-24 18:38:06 +090049 ` WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\n` +
50 ` See the License for the specific language governing permissions and\n` +
51 ` limitations under the License.\n` +
52 `-->\n` +
53 `<permissions>\n` +
Jiyong Parke3833882020-02-17 17:28:10 +090054 ` <library name=\"%s\" file=\"%s\"/>\n` +
Jooyung Han624058e2019-12-24 18:38:06 +090055 `</permissions>\n`
Jiyong Parkc678ad32018-04-10 13:07:10 +090056)
57
Paul Duffind1b3a922020-01-22 11:57:20 +000058// A tag to associated a dependency with a specific api scope.
59type scopeDependencyTag struct {
60 blueprint.BaseDependencyTag
61 name string
62 apiScope *apiScope
63}
64
65// Provides information about an api scope, e.g. public, system, test.
66type apiScope struct {
67 // The name of the api scope, e.g. public, system, test
68 name string
69
Paul Duffin6a2bd112020-04-07 19:27:04 +010070 // The name of the field in the dynamically created structure.
71 fieldName string
72
Paul Duffind1b3a922020-01-22 11:57:20 +000073 // The tag to use to depend on the stubs library module.
74 stubsTag scopeDependencyTag
75
76 // The tag to use to depend on the stubs
77 apiFileTag scopeDependencyTag
78
79 // The scope specific prefix to add to the api file base of "current.txt" or "removed.txt".
80 apiFilePrefix string
81
82 // The scope specific prefix to add to the sdk library module name to construct a scope specific
83 // module name.
84 moduleSuffix string
85
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 Duffin3c7c3472020-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 Duffin6a2bd112020-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 Duffin3c7c3472020-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 Duffin3c7c3472020-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 })
Jiyong Parkc678ad32018-04-10 13:07:10 +0900174}
175
Paul Duffin43dc1cc2019-12-19 11:18:54 +0000176func RegisterSdkLibraryBuildComponents(ctx android.RegistrationContext) {
177 ctx.RegisterModuleType("java_sdk_library", SdkLibraryFactory)
178 ctx.RegisterModuleType("java_sdk_library_import", sdkLibraryImportFactory)
179}
180
Jiyong Parkc678ad32018-04-10 13:07:10 +0900181type sdkLibraryProperties struct {
Sundong Ahnf043cf62018-06-25 16:04:37 +0900182 // List of Java libraries that will be in the classpath when building stubs
183 Stub_only_libs []string `android:"arch_variant"`
184
Paul Duffin7a586d32019-12-30 17:09:34 +0000185 // list of package names that will be documented and publicized as API.
186 // This allows the API to be restricted to a subset of the source files provided.
187 // If this is unspecified then all the source files will be treated as being part
188 // of the API.
Jiyong Parkc678ad32018-04-10 13:07:10 +0900189 Api_packages []string
190
Jiyong Park5a2c9d72018-05-01 22:25:41 +0900191 // list of package names that must be hidden from the API
192 Hidden_api_packages []string
193
Paul Duffin749f98f2019-12-30 17:23:46 +0000194 // the relative path to the directory containing the api specification files.
195 // Defaults to "api".
196 Api_dir *string
197
Paul Duffin43db9be2019-12-30 17:35:49 +0000198 // If set to true there is no runtime library.
199 Api_only *bool
200
Paul Duffin11512472019-02-11 15:55:17 +0000201 // local files that are used within user customized droiddoc options.
202 Droiddoc_option_files []string
203
204 // additional droiddoc options
205 // Available variables for substitution:
206 //
207 // $(location <label>): the path to the droiddoc_option_files with name <label>
Sundong Ahndd567f92018-07-31 17:19:11 +0900208 Droiddoc_options []string
209
Sundong Ahn054b19a2018-10-19 13:46:09 +0900210 // a list of top-level directories containing files to merge qualifier annotations
211 // (i.e. those intended to be included in the stubs written) from.
212 Merge_annotations_dirs []string
213
214 // a list of top-level directories containing Java stub files to merge show/hide annotations from.
215 Merge_inclusion_annotations_dirs []string
216
217 // If set to true, the path of dist files is apistubs/core. Defaults to false.
218 Core_lib *bool
219
Sundong Ahn80a87b32019-05-13 15:02:50 +0900220 // don't create dist rules.
221 No_dist *bool `blueprint:"mutated"`
222
Paul Duffin37e0b772019-12-30 17:20:10 +0000223 // indicates whether system and test apis should be managed.
224 Has_system_and_test_apis bool `blueprint:"mutated"`
225
Jiyong Parkc678ad32018-04-10 13:07:10 +0900226 // TODO: determines whether to create HTML doc or not
227 //Html_doc *bool
228}
229
Paul Duffind1b3a922020-01-22 11:57:20 +0000230type scopePaths struct {
231 stubsHeaderPath android.Paths
232 stubsImplPath android.Paths
233 apiFilePath android.Path
234}
235
Paul Duffin56d44902020-01-31 13:36:25 +0000236// Common code between sdk library and sdk library import
237type commonToSdkLibraryAndImport struct {
238 scopePaths map[*apiScope]*scopePaths
239}
240
241func (c *commonToSdkLibraryAndImport) getScopePaths(scope *apiScope) *scopePaths {
242 if c.scopePaths == nil {
243 c.scopePaths = make(map[*apiScope]*scopePaths)
244 }
245 paths := c.scopePaths[scope]
246 if paths == nil {
247 paths = &scopePaths{}
248 c.scopePaths[scope] = paths
249 }
250
251 return paths
252}
253
Inseob Kimc0907f12019-02-08 21:00:45 +0900254type SdkLibrary struct {
Sundong Ahn054b19a2018-10-19 13:46:09 +0900255 Library
Jiyong Parkc678ad32018-04-10 13:07:10 +0900256
Sundong Ahn054b19a2018-10-19 13:46:09 +0900257 sdkLibraryProperties sdkLibraryProperties
Jiyong Parkc678ad32018-04-10 13:07:10 +0900258
Paul Duffin56d44902020-01-31 13:36:25 +0000259 commonToSdkLibraryAndImport
Jiyong Parkc678ad32018-04-10 13:07:10 +0900260}
261
Inseob Kimc0907f12019-02-08 21:00:45 +0900262var _ Dependency = (*SdkLibrary)(nil)
263var _ SdkLibraryDependency = (*SdkLibrary)(nil)
Colin Cross897d2ed2019-02-11 14:03:51 -0800264
Paul Duffind1b3a922020-01-22 11:57:20 +0000265func (module *SdkLibrary) getActiveApiScopes() apiScopes {
266 if module.sdkLibraryProperties.Has_system_and_test_apis {
267 return allApiScopes
268 } else {
269 return apiScopes{apiScopePublic}
270 }
271}
272
Paul Duffine74ac732020-02-06 13:51:46 +0000273var xmlPermissionsFileTag = dependencyTag{name: "xml-permissions-file"}
274
Jiyong Parke3833882020-02-17 17:28:10 +0900275func IsXmlPermissionsFileDepTag(depTag blueprint.DependencyTag) bool {
276 if dt, ok := depTag.(dependencyTag); ok {
277 return dt == xmlPermissionsFileTag
278 }
279 return false
280}
281
Inseob Kimc0907f12019-02-08 21:00:45 +0900282func (module *SdkLibrary) DepsMutator(ctx android.BottomUpMutatorContext) {
Paul Duffind1b3a922020-01-22 11:57:20 +0000283 for _, apiScope := range module.getActiveApiScopes() {
284 // Add dependencies to the stubs library
Paul Duffin50061512020-01-21 16:31:05 +0000285 ctx.AddVariationDependencies(nil, apiScope.stubsTag, module.stubsName(apiScope))
Paul Duffind1b3a922020-01-22 11:57:20 +0000286
Paul Duffin50061512020-01-21 16:31:05 +0000287 // And the api file
Paul Duffind1b3a922020-01-22 11:57:20 +0000288 ctx.AddVariationDependencies(nil, apiScope.apiFileTag, module.docsName(apiScope))
Sundong Ahn054b19a2018-10-19 13:46:09 +0900289 }
290
Paul Duffine74ac732020-02-06 13:51:46 +0000291 if !proptools.Bool(module.sdkLibraryProperties.Api_only) {
292 // Add dependency to the rule for generating the xml permissions file
Jiyong Parke3833882020-02-17 17:28:10 +0900293 ctx.AddDependency(module, xmlPermissionsFileTag, module.xmlFileName())
Paul Duffine74ac732020-02-06 13:51:46 +0000294 }
295
Sundong Ahn054b19a2018-10-19 13:46:09 +0900296 module.Library.deps(ctx)
Jiyong Parkc678ad32018-04-10 13:07:10 +0900297}
298
Inseob Kimc0907f12019-02-08 21:00:45 +0900299func (module *SdkLibrary) GenerateAndroidBuildActions(ctx android.ModuleContext) {
Paul Duffin43db9be2019-12-30 17:35:49 +0000300 // Don't build an implementation library if this is api only.
301 if !proptools.Bool(module.sdkLibraryProperties.Api_only) {
302 module.Library.GenerateAndroidBuildActions(ctx)
303 }
Sundong Ahn054b19a2018-10-19 13:46:09 +0900304
Sundong Ahn57368eb2018-07-06 11:20:23 +0900305 // Record the paths to the header jars of the library (stubs and impl).
Paul Duffind1b3a922020-01-22 11:57:20 +0000306 // When this java_sdk_library is depended upon from others via "libs" property,
Jiyong Parkc678ad32018-04-10 13:07:10 +0900307 // the recorded paths will be returned depending on the link type of the caller.
308 ctx.VisitDirectDeps(func(to android.Module) {
309 otherName := ctx.OtherModuleName(to)
310 tag := ctx.OtherModuleDependencyTag(to)
311
Sundong Ahn57368eb2018-07-06 11:20:23 +0900312 if lib, ok := to.(Dependency); ok {
Paul Duffind1b3a922020-01-22 11:57:20 +0000313 if scopeTag, ok := tag.(scopeDependencyTag); ok {
314 apiScope := scopeTag.apiScope
315 scopePaths := module.getScopePaths(apiScope)
316 scopePaths.stubsHeaderPath = lib.HeaderJars()
317 scopePaths.stubsImplPath = lib.ImplementationJars()
Jiyong Parkc678ad32018-04-10 13:07:10 +0900318 }
319 }
Sundong Ahn20e998b2018-07-24 11:19:26 +0900320 if doc, ok := to.(ApiFilePath); ok {
Paul Duffind1b3a922020-01-22 11:57:20 +0000321 if scopeTag, ok := tag.(scopeDependencyTag); ok {
322 apiScope := scopeTag.apiScope
323 scopePaths := module.getScopePaths(apiScope)
324 scopePaths.apiFilePath = doc.ApiFilePath()
325 } else {
Sundong Ahn20e998b2018-07-24 11:19:26 +0900326 ctx.ModuleErrorf("depends on module %q of unknown tag %q", otherName, tag)
327 }
328 }
Jiyong Parkc678ad32018-04-10 13:07:10 +0900329 })
330}
331
Jiyong Park0b0e1b92019-12-03 13:24:29 +0900332func (module *SdkLibrary) AndroidMkEntries() []android.AndroidMkEntries {
Paul Duffin43db9be2019-12-30 17:35:49 +0000333 if proptools.Bool(module.sdkLibraryProperties.Api_only) {
334 return nil
335 }
Jiyong Park0b0e1b92019-12-03 13:24:29 +0900336 entriesList := module.Library.AndroidMkEntries()
337 entries := &entriesList[0]
Jaewoong Jungb0c127c2019-08-29 14:56:03 -0700338 entries.Required = append(entries.Required, module.xmlFileName())
Jiyong Park0b0e1b92019-12-03 13:24:29 +0900339 return entriesList
Jiyong Park82484c02018-04-23 21:41:26 +0900340}
341
Jiyong Parkc678ad32018-04-10 13:07:10 +0900342// Module name of the stubs library
Paul Duffind1b3a922020-01-22 11:57:20 +0000343func (module *SdkLibrary) stubsName(apiScope *apiScope) string {
344 return apiScope.stubsModuleName(module.BaseModuleName())
Jiyong Parkc678ad32018-04-10 13:07:10 +0900345}
346
347// Module name of the docs
Paul Duffind1b3a922020-01-22 11:57:20 +0000348func (module *SdkLibrary) docsName(apiScope *apiScope) string {
349 return apiScope.docsModuleName(module.BaseModuleName())
Jiyong Parkc678ad32018-04-10 13:07:10 +0900350}
351
352// Module name of the runtime implementation library
Inseob Kimc0907f12019-02-08 21:00:45 +0900353func (module *SdkLibrary) implName() string {
Sundong Ahn054b19a2018-10-19 13:46:09 +0900354 return module.BaseModuleName()
Jiyong Parkc678ad32018-04-10 13:07:10 +0900355}
356
Jiyong Parkc678ad32018-04-10 13:07:10 +0900357// Module name of the XML file for the lib
Inseob Kimc0907f12019-02-08 21:00:45 +0900358func (module *SdkLibrary) xmlFileName() string {
Jiyong Parkc678ad32018-04-10 13:07:10 +0900359 return module.BaseModuleName() + sdkXmlFileSuffix
360}
361
Anton Hansson6bb88102020-03-27 19:43:19 +0000362// The dist path of the stub artifacts
363func (module *SdkLibrary) apiDistPath(apiScope *apiScope) string {
364 if module.ModuleBase.Owner() != "" {
365 return path.Join("apistubs", module.ModuleBase.Owner(), apiScope.name)
366 } else if Bool(module.sdkLibraryProperties.Core_lib) {
367 return path.Join("apistubs", "core", apiScope.name)
368 } else {
369 return path.Join("apistubs", "android", apiScope.name)
370 }
371}
372
Paul Duffin12ceb462019-12-24 20:31:31 +0000373// Get the sdk version for use when compiling the stubs library.
Paul Duffind1b3a922020-01-22 11:57:20 +0000374func (module *SdkLibrary) sdkVersionForStubsLibrary(mctx android.LoadHookContext, apiScope *apiScope) string {
Paul Duffin12ceb462019-12-24 20:31:31 +0000375 sdkDep := decodeSdkDep(mctx, sdkContext(&module.Library))
376 if sdkDep.hasStandardLibs() {
377 // If building against a standard sdk then use the sdk version appropriate for the scope.
Paul Duffind1b3a922020-01-22 11:57:20 +0000378 return apiScope.sdkVersion
Paul Duffin12ceb462019-12-24 20:31:31 +0000379 } else {
380 // Otherwise, use no system module.
381 return "none"
382 }
383}
384
Paul Duffind1b3a922020-01-22 11:57:20 +0000385func (module *SdkLibrary) latestApiFilegroupName(apiScope *apiScope) string {
386 return ":" + module.BaseModuleName() + ".api." + apiScope.name + ".latest"
Jiyong Park58c518b2018-05-12 22:29:12 +0900387}
Jiyong Parkc678ad32018-04-10 13:07:10 +0900388
Paul Duffind1b3a922020-01-22 11:57:20 +0000389func (module *SdkLibrary) latestRemovedApiFilegroupName(apiScope *apiScope) string {
390 return ":" + module.BaseModuleName() + "-removed.api." + apiScope.name + ".latest"
Jiyong Parkc678ad32018-04-10 13:07:10 +0900391}
392
393// Creates a static java library that has API stubs
Paul Duffind1b3a922020-01-22 11:57:20 +0000394func (module *SdkLibrary) createStubsLibrary(mctx android.LoadHookContext, apiScope *apiScope) {
Jiyong Parkc678ad32018-04-10 13:07:10 +0900395 props := struct {
Sundong Ahn0d7dff42019-12-04 12:53:44 +0900396 Name *string
397 Srcs []string
Paul Duffin367ab912019-12-23 19:40:36 +0000398 Installable *bool
Sundong Ahn0d7dff42019-12-04 12:53:44 +0900399 Sdk_version *string
Paul Duffin12ceb462019-12-24 20:31:31 +0000400 System_modules *string
Paul Duffinab8da5d2020-02-07 16:12:04 +0000401 Patch_module *string
Sundong Ahn0d7dff42019-12-04 12:53:44 +0900402 Libs []string
403 Soc_specific *bool
404 Device_specific *bool
405 Product_specific *bool
406 System_ext_specific *bool
407 Compile_dex *bool
Sundong Ahn0d7dff42019-12-04 12:53:44 +0900408 Java_version *string
409 Product_variables struct {
Jiyong Park82484c02018-04-23 21:41:26 +0900410 Pdk struct {
411 Enabled *bool
412 }
Jiyong Parkc678ad32018-04-10 13:07:10 +0900413 }
Sundong Ahn054b19a2018-10-19 13:46:09 +0900414 Openjdk9 struct {
415 Srcs []string
416 Javacflags []string
417 }
Anton Hansson6bb88102020-03-27 19:43:19 +0000418 Dist struct {
419 Targets []string
420 Dest *string
421 Dir *string
422 Tag *string
423 }
Jiyong Parkc678ad32018-04-10 13:07:10 +0900424 }{}
425
Jiyong Parkdf130542018-04-27 16:29:21 +0900426 props.Name = proptools.StringPtr(module.stubsName(apiScope))
Jiyong Parkc678ad32018-04-10 13:07:10 +0900427 // sources are generated from the droiddoc
Jiyong Parkdf130542018-04-27 16:29:21 +0900428 props.Srcs = []string{":" + module.docsName(apiScope)}
Paul Duffin12ceb462019-12-24 20:31:31 +0000429 sdkVersion := module.sdkVersionForStubsLibrary(mctx, apiScope)
Paul Duffin52d398a2019-06-11 12:31:14 +0100430 props.Sdk_version = proptools.StringPtr(sdkVersion)
Paul Duffin12ceb462019-12-24 20:31:31 +0000431 props.System_modules = module.Library.Module.deviceProperties.System_modules
Paul Duffinab8da5d2020-02-07 16:12:04 +0000432 props.Patch_module = module.Library.Module.properties.Patch_module
Paul Duffin367ab912019-12-23 19:40:36 +0000433 props.Installable = proptools.BoolPtr(false)
Sundong Ahn054b19a2018-10-19 13:46:09 +0900434 props.Libs = module.sdkLibraryProperties.Stub_only_libs
Jiyong Park82484c02018-04-23 21:41:26 +0900435 props.Product_variables.Pdk.Enabled = proptools.BoolPtr(false)
Sundong Ahn054b19a2018-10-19 13:46:09 +0900436 props.Openjdk9.Srcs = module.Library.Module.properties.Openjdk9.Srcs
437 props.Openjdk9.Javacflags = module.Library.Module.properties.Openjdk9.Javacflags
438 props.Java_version = module.Library.Module.properties.Java_version
439 if module.Library.Module.deviceProperties.Compile_dex != nil {
440 props.Compile_dex = module.Library.Module.deviceProperties.Compile_dex
Sundong Ahndd567f92018-07-31 17:19:11 +0900441 }
Jiyong Parkc678ad32018-04-10 13:07:10 +0900442
443 if module.SocSpecific() {
444 props.Soc_specific = proptools.BoolPtr(true)
445 } else if module.DeviceSpecific() {
446 props.Device_specific = proptools.BoolPtr(true)
447 } else if module.ProductSpecific() {
448 props.Product_specific = proptools.BoolPtr(true)
Sundong Ahn0d7dff42019-12-04 12:53:44 +0900449 } else if module.SystemExtSpecific() {
450 props.System_ext_specific = proptools.BoolPtr(true)
Jiyong Parkc678ad32018-04-10 13:07:10 +0900451 }
Anton Hansson6bb88102020-03-27 19:43:19 +0000452 // Dist the class jar artifact for sdk builds.
453 if !Bool(module.sdkLibraryProperties.No_dist) {
454 props.Dist.Targets = []string{"sdk", "win_sdk"}
455 props.Dist.Dest = proptools.StringPtr(fmt.Sprintf("%v.jar", module.BaseModuleName()))
456 props.Dist.Dir = proptools.StringPtr(module.apiDistPath(apiScope))
457 props.Dist.Tag = proptools.StringPtr(".jar")
458 }
Jiyong Parkc678ad32018-04-10 13:07:10 +0900459
Colin Cross84dfc3d2019-09-25 11:33:01 -0700460 mctx.CreateModule(LibraryFactory, &props)
Jiyong Parkc678ad32018-04-10 13:07:10 +0900461}
462
Paul Duffincbcfcaa2020-04-07 18:49:53 +0100463// Creates a droidstubs module that creates stubs source files from the given full source
Jiyong Parkc678ad32018-04-10 13:07:10 +0900464// files
Paul Duffind1b3a922020-01-22 11:57:20 +0000465func (module *SdkLibrary) createStubsSources(mctx android.LoadHookContext, apiScope *apiScope) {
Jiyong Parkc678ad32018-04-10 13:07:10 +0900466 props := struct {
Sundong Ahn054b19a2018-10-19 13:46:09 +0900467 Name *string
468 Srcs []string
469 Installable *bool
Paul Duffin52d398a2019-06-11 12:31:14 +0100470 Sdk_version *string
Paul Duffin12ceb462019-12-24 20:31:31 +0000471 System_modules *string
Sundong Ahn054b19a2018-10-19 13:46:09 +0900472 Libs []string
Paul Duffin11512472019-02-11 15:55:17 +0000473 Arg_files []string
Sundong Ahn054b19a2018-10-19 13:46:09 +0900474 Args *string
Sundong Ahn054b19a2018-10-19 13:46:09 +0900475 Java_version *string
476 Merge_annotations_dirs []string
477 Merge_inclusion_annotations_dirs []string
478 Check_api struct {
Inseob Kim38449af2019-02-28 14:24:05 +0900479 Current ApiToCheck
480 Last_released ApiToCheck
481 Ignore_missing_latest_api *bool
Jiyong Park58c518b2018-05-12 22:29:12 +0900482 }
Sundong Ahn1b92c822018-05-29 11:35:17 +0900483 Aidl struct {
484 Include_dirs []string
485 Local_include_dirs []string
486 }
Anton Hansson6bb88102020-03-27 19:43:19 +0000487 Dist struct {
488 Targets []string
489 Dest *string
490 Dir *string
491 }
Jiyong Parkc678ad32018-04-10 13:07:10 +0900492 }{}
493
Paul Duffin250e6192019-06-07 10:44:37 +0100494 sdkDep := decodeSdkDep(mctx, sdkContext(&module.Library))
Paul Duffin12ceb462019-12-24 20:31:31 +0000495 // Use the platform API if standard libraries were requested, otherwise use
496 // no default libraries.
Paul Duffin52d398a2019-06-11 12:31:14 +0100497 sdkVersion := ""
498 if !sdkDep.hasStandardLibs() {
499 sdkVersion = "none"
500 }
Paul Duffin250e6192019-06-07 10:44:37 +0100501
Jiyong Parkdf130542018-04-27 16:29:21 +0900502 props.Name = proptools.StringPtr(module.docsName(apiScope))
Sundong Ahn054b19a2018-10-19 13:46:09 +0900503 props.Srcs = append(props.Srcs, module.Library.Module.properties.Srcs...)
Paul Duffin52d398a2019-06-11 12:31:14 +0100504 props.Sdk_version = proptools.StringPtr(sdkVersion)
Paul Duffin12ceb462019-12-24 20:31:31 +0000505 props.System_modules = module.Library.Module.deviceProperties.System_modules
Jiyong Parkc678ad32018-04-10 13:07:10 +0900506 props.Installable = proptools.BoolPtr(false)
Sundong Ahne6f0b052018-06-05 16:46:14 +0900507 // A droiddoc module has only one Libs property and doesn't distinguish between
508 // shared libs and static libs. So we need to add both of these libs to Libs property.
Sundong Ahn054b19a2018-10-19 13:46:09 +0900509 props.Libs = module.Library.Module.properties.Libs
510 props.Libs = append(props.Libs, module.Library.Module.properties.Static_libs...)
511 props.Aidl.Include_dirs = module.Library.Module.deviceProperties.Aidl.Include_dirs
512 props.Aidl.Local_include_dirs = module.Library.Module.deviceProperties.Aidl.Local_include_dirs
Sundong Ahn054b19a2018-10-19 13:46:09 +0900513 props.Java_version = module.Library.Module.properties.Java_version
Jiyong Parkc678ad32018-04-10 13:07:10 +0900514
Sundong Ahn054b19a2018-10-19 13:46:09 +0900515 props.Merge_annotations_dirs = module.sdkLibraryProperties.Merge_annotations_dirs
516 props.Merge_inclusion_annotations_dirs = module.sdkLibraryProperties.Merge_inclusion_annotations_dirs
517
Paul Duffincbcfcaa2020-04-07 18:49:53 +0100518 droidstubsArgs := []string{}
Paul Duffin235ffff2019-12-24 10:41:30 +0000519 if len(module.sdkLibraryProperties.Api_packages) != 0 {
Paul Duffincbcfcaa2020-04-07 18:49:53 +0100520 droidstubsArgs = append(droidstubsArgs, "--stub-packages "+strings.Join(module.sdkLibraryProperties.Api_packages, ":"))
Paul Duffin235ffff2019-12-24 10:41:30 +0000521 }
522 if len(module.sdkLibraryProperties.Hidden_api_packages) != 0 {
Paul Duffincbcfcaa2020-04-07 18:49:53 +0100523 droidstubsArgs = append(droidstubsArgs,
Paul Duffin235ffff2019-12-24 10:41:30 +0000524 android.JoinWithPrefix(module.sdkLibraryProperties.Hidden_api_packages, " --hide-package "))
525 }
Paul Duffincbcfcaa2020-04-07 18:49:53 +0100526 droidstubsArgs = append(droidstubsArgs, module.sdkLibraryProperties.Droiddoc_options...)
Paul Duffin235ffff2019-12-24 10:41:30 +0000527 disabledWarnings := []string{
528 "MissingPermission",
529 "BroadcastBehavior",
530 "HiddenSuperclass",
531 "DeprecationMismatch",
532 "UnavailableSymbol",
533 "SdkConstant",
534 "HiddenTypeParameter",
535 "Todo",
536 "Typo",
537 }
Paul Duffincbcfcaa2020-04-07 18:49:53 +0100538 droidstubsArgs = append(droidstubsArgs, android.JoinWithPrefix(disabledWarnings, "--hide "))
Sundong Ahnfb2721f2018-09-17 13:23:09 +0900539
Paul Duffin3c7c3472020-04-07 18:50:10 +0100540 // Add in scope specific arguments.
541 droidstubsArgs = append(droidstubsArgs, apiScope.droidstubsArgs...)
Paul Duffin11512472019-02-11 15:55:17 +0000542 props.Arg_files = module.sdkLibraryProperties.Droiddoc_option_files
Paul Duffincbcfcaa2020-04-07 18:49:53 +0100543 props.Args = proptools.StringPtr(strings.Join(droidstubsArgs, " "))
Jiyong Parkc678ad32018-04-10 13:07:10 +0900544
545 // List of APIs identified from the provided source files are created. They are later
546 // compared against to the not-yet-released (a.k.a current) list of APIs and to the
547 // last-released (a.k.a numbered) list of API.
Paul Duffind1b3a922020-01-22 11:57:20 +0000548 currentApiFileName := apiScope.apiFilePrefix + "current.txt"
549 removedApiFileName := apiScope.apiFilePrefix + "removed.txt"
Paul Duffin749f98f2019-12-30 17:23:46 +0000550 apiDir := module.getApiDir()
551 currentApiFileName = path.Join(apiDir, currentApiFileName)
552 removedApiFileName = path.Join(apiDir, removedApiFileName)
Jiyong Parkc678ad32018-04-10 13:07:10 +0900553
Jiyong Park58c518b2018-05-12 22:29:12 +0900554 // check against the not-yet-release API
555 props.Check_api.Current.Api_file = proptools.StringPtr(currentApiFileName)
556 props.Check_api.Current.Removed_api_file = proptools.StringPtr(removedApiFileName)
Jiyong Park58c518b2018-05-12 22:29:12 +0900557
558 // check against the latest released API
559 props.Check_api.Last_released.Api_file = proptools.StringPtr(
560 module.latestApiFilegroupName(apiScope))
561 props.Check_api.Last_released.Removed_api_file = proptools.StringPtr(
562 module.latestRemovedApiFilegroupName(apiScope))
Inseob Kim38449af2019-02-28 14:24:05 +0900563 props.Check_api.Ignore_missing_latest_api = proptools.BoolPtr(true)
Jiyong Park58c518b2018-05-12 22:29:12 +0900564
Anton Hansson6bb88102020-03-27 19:43:19 +0000565 // Dist the api txt artifact for sdk builds.
566 if !Bool(module.sdkLibraryProperties.No_dist) {
567 props.Dist.Targets = []string{"sdk", "win_sdk"}
568 props.Dist.Dest = proptools.StringPtr(fmt.Sprintf("%v.txt", module.BaseModuleName()))
569 props.Dist.Dir = proptools.StringPtr(path.Join(module.apiDistPath(apiScope), "api"))
570 }
571
Colin Cross84dfc3d2019-09-25 11:33:01 -0700572 mctx.CreateModule(DroidstubsFactory, &props)
Jiyong Parkc678ad32018-04-10 13:07:10 +0900573}
574
Jooyung Hanb8fa86a2020-03-10 06:23:13 +0900575func (module *SdkLibrary) DepIsInSameApex(mctx android.BaseModuleContext, dep android.Module) bool {
576 depTag := mctx.OtherModuleDependencyTag(dep)
577 if depTag == xmlPermissionsFileTag {
578 return true
579 }
580 return module.Library.DepIsInSameApex(mctx, dep)
581}
582
Jiyong Parkc678ad32018-04-10 13:07:10 +0900583// Creates the xml file that publicizes the runtime library
Colin Crossf8b860a2019-04-16 14:43:28 -0700584func (module *SdkLibrary) createXmlFile(mctx android.LoadHookContext) {
Jiyong Parke3833882020-02-17 17:28:10 +0900585 props := struct {
Sundong Ahn0d7dff42019-12-04 12:53:44 +0900586 Name *string
Jiyong Parke3833882020-02-17 17:28:10 +0900587 Lib_name *string
Sundong Ahn0d7dff42019-12-04 12:53:44 +0900588 Soc_specific *bool
589 Device_specific *bool
590 Product_specific *bool
591 System_ext_specific *bool
Jooyung Hanb8fa86a2020-03-10 06:23:13 +0900592 Apex_available []string
Jiyong Parke3833882020-02-17 17:28:10 +0900593 }{
Jooyung Hanb8fa86a2020-03-10 06:23:13 +0900594 Name: proptools.StringPtr(module.xmlFileName()),
595 Lib_name: proptools.StringPtr(module.BaseModuleName()),
596 Apex_available: module.ApexProperties.Apex_available,
Jiyong Parkc678ad32018-04-10 13:07:10 +0900597 }
Jiyong Parke3833882020-02-17 17:28:10 +0900598
599 if module.SocSpecific() {
600 props.Soc_specific = proptools.BoolPtr(true)
601 } else if module.DeviceSpecific() {
602 props.Device_specific = proptools.BoolPtr(true)
603 } else if module.ProductSpecific() {
604 props.Product_specific = proptools.BoolPtr(true)
605 } else if module.SystemExtSpecific() {
606 props.System_ext_specific = proptools.BoolPtr(true)
607 }
608
609 mctx.CreateModule(sdkLibraryXmlFactory, &props)
Jiyong Parkc678ad32018-04-10 13:07:10 +0900610}
611
Paul Duffin50061512020-01-21 16:31:05 +0000612func PrebuiltJars(ctx android.BaseModuleContext, baseName string, s sdkSpec) android.Paths {
Jiyong Park6a927c42020-01-21 02:03:43 +0900613 var ver sdkVersion
614 var kind sdkKind
615 if s.usePrebuilt(ctx) {
616 ver = s.version
617 kind = s.kind
Jiyong Parkc678ad32018-04-10 13:07:10 +0900618 } else {
Jiyong Park6a927c42020-01-21 02:03:43 +0900619 // We don't have prebuilt SDK for the specific sdkVersion.
620 // Instead of breaking the build, fallback to use "system_current"
621 ver = sdkVersionCurrent
622 kind = sdkSystem
Sundong Ahn054b19a2018-10-19 13:46:09 +0900623 }
Jiyong Park6a927c42020-01-21 02:03:43 +0900624
625 dir := filepath.Join("prebuilts", "sdk", ver.String(), kind.String())
Paul Duffin50061512020-01-21 16:31:05 +0000626 jar := filepath.Join(dir, baseName+".jar")
Sundong Ahn054b19a2018-10-19 13:46:09 +0900627 jarPath := android.ExistentPathForSource(ctx, jar)
Sundong Ahnae418ac2019-02-28 15:01:28 +0900628 if !jarPath.Valid() {
Colin Cross07c88562020-01-07 09:34:44 -0800629 if ctx.Config().AllowMissingDependencies() {
630 return android.Paths{android.PathForSource(ctx, jar)}
631 } else {
Jiyong Park6a927c42020-01-21 02:03:43 +0900632 ctx.PropertyErrorf("sdk_library", "invalid sdk version %q, %q does not exist", s.raw, jar)
Colin Cross07c88562020-01-07 09:34:44 -0800633 }
Sundong Ahnae418ac2019-02-28 15:01:28 +0900634 return nil
635 }
Sundong Ahn054b19a2018-10-19 13:46:09 +0900636 return android.Paths{jarPath.Path()}
637}
638
Paul Duffind1b3a922020-01-22 11:57:20 +0000639func (module *SdkLibrary) sdkJars(
640 ctx android.BaseModuleContext,
641 sdkVersion sdkSpec,
642 headerJars bool) android.Paths {
643
Paul Duffin50061512020-01-21 16:31:05 +0000644 // If a specific numeric version has been requested then use prebuilt versions of the sdk.
645 if sdkVersion.version.isNumbered() {
646 return PrebuiltJars(ctx, module.BaseModuleName(), sdkVersion)
Sundong Ahn054b19a2018-10-19 13:46:09 +0900647 } else {
Paul Duffind1b3a922020-01-22 11:57:20 +0000648 if !sdkVersion.specified() {
649 if headerJars {
650 return module.Library.HeaderJars()
651 } else {
652 return module.Library.ImplementationJars()
653 }
654 }
Paul Duffin726d23c2020-01-22 16:30:37 +0000655 var apiScope *apiScope
Jiyong Park6a927c42020-01-21 02:03:43 +0900656 switch sdkVersion.kind {
657 case sdkSystem:
Paul Duffin726d23c2020-01-22 16:30:37 +0000658 apiScope = apiScopeSystem
659 case sdkTest:
660 apiScope = apiScopeTest
Jiyong Park6a927c42020-01-21 02:03:43 +0900661 case sdkPrivate:
Sundong Ahn054b19a2018-10-19 13:46:09 +0900662 return module.Library.HeaderJars()
Jiyong Park6a927c42020-01-21 02:03:43 +0900663 default:
Paul Duffin726d23c2020-01-22 16:30:37 +0000664 apiScope = apiScopePublic
Paul Duffind1b3a922020-01-22 11:57:20 +0000665 }
666
Paul Duffin726d23c2020-01-22 16:30:37 +0000667 paths := module.getScopePaths(apiScope)
Paul Duffind1b3a922020-01-22 11:57:20 +0000668 if headerJars {
669 return paths.stubsHeaderPath
670 } else {
671 return paths.stubsImplPath
Sundong Ahn054b19a2018-10-19 13:46:09 +0900672 }
Jiyong Parkc678ad32018-04-10 13:07:10 +0900673 }
674}
675
Sundong Ahn241cd372018-07-13 16:16:44 +0900676// to satisfy SdkLibraryDependency interface
Paul Duffind1b3a922020-01-22 11:57:20 +0000677func (module *SdkLibrary) SdkHeaderJars(ctx android.BaseModuleContext, sdkVersion sdkSpec) android.Paths {
678 return module.sdkJars(ctx, sdkVersion, true /*headerJars*/)
679}
680
681// to satisfy SdkLibraryDependency interface
Jiyong Park6a927c42020-01-21 02:03:43 +0900682func (module *SdkLibrary) SdkImplementationJars(ctx android.BaseModuleContext, sdkVersion sdkSpec) android.Paths {
Paul Duffind1b3a922020-01-22 11:57:20 +0000683 return module.sdkJars(ctx, sdkVersion, false /*headerJars*/)
Sundong Ahn241cd372018-07-13 16:16:44 +0900684}
685
Sundong Ahn80a87b32019-05-13 15:02:50 +0900686func (module *SdkLibrary) SetNoDist() {
687 module.sdkLibraryProperties.No_dist = proptools.BoolPtr(true)
688}
689
Colin Cross571cccf2019-02-04 11:22:08 -0800690var javaSdkLibrariesKey = android.NewOnceKey("javaSdkLibraries")
691
Jiyong Park82484c02018-04-23 21:41:26 +0900692func javaSdkLibraries(config android.Config) *[]string {
Colin Cross571cccf2019-02-04 11:22:08 -0800693 return config.Once(javaSdkLibrariesKey, func() interface{} {
Jiyong Park82484c02018-04-23 21:41:26 +0900694 return &[]string{}
695 }).(*[]string)
696}
697
Paul Duffin749f98f2019-12-30 17:23:46 +0000698func (module *SdkLibrary) getApiDir() string {
699 return proptools.StringDefault(module.sdkLibraryProperties.Api_dir, "api")
700}
701
Jiyong Parkc678ad32018-04-10 13:07:10 +0900702// For a java_sdk_library module, create internal modules for stubs, docs,
703// runtime libs and xml file. If requested, the stubs and docs are created twice
704// once for public API level and once for system API level
Colin Crossf8b860a2019-04-16 14:43:28 -0700705func (module *SdkLibrary) CreateInternalModules(mctx android.LoadHookContext) {
Inseob Kim6e93ac92019-03-21 17:43:49 +0900706 if len(module.Library.Module.properties.Srcs) == 0 {
Inseob Kimc0907f12019-02-08 21:00:45 +0900707 mctx.PropertyErrorf("srcs", "java_sdk_library must specify srcs")
Jooyung Han58f26ab2019-12-18 15:34:32 +0900708 return
Inseob Kimc0907f12019-02-08 21:00:45 +0900709 }
710
Paul Duffin37e0b772019-12-30 17:20:10 +0000711 // If this builds against standard libraries (i.e. is not part of the core libraries)
712 // then assume it provides both system and test apis. Otherwise, assume it does not and
713 // also assume it does not contribute to the dist build.
714 sdkDep := decodeSdkDep(mctx, sdkContext(&module.Library))
715 hasSystemAndTestApis := sdkDep.hasStandardLibs()
716 module.sdkLibraryProperties.Has_system_and_test_apis = hasSystemAndTestApis
717 module.sdkLibraryProperties.No_dist = proptools.BoolPtr(!hasSystemAndTestApis)
718
Inseob Kim8098faa2019-03-18 10:19:51 +0900719 missing_current_api := false
720
Paul Duffind1b3a922020-01-22 11:57:20 +0000721 activeScopes := module.getActiveApiScopes()
722
Paul Duffin749f98f2019-12-30 17:23:46 +0000723 apiDir := module.getApiDir()
Paul Duffind1b3a922020-01-22 11:57:20 +0000724 for _, scope := range activeScopes {
Inseob Kim8098faa2019-03-18 10:19:51 +0900725 for _, api := range []string{"current.txt", "removed.txt"} {
Paul Duffind1b3a922020-01-22 11:57:20 +0000726 path := path.Join(mctx.ModuleDir(), apiDir, scope.apiFilePrefix+api)
Inseob Kim8098faa2019-03-18 10:19:51 +0900727 p := android.ExistentPathForSource(mctx, path)
728 if !p.Valid() {
729 mctx.ModuleErrorf("Current api file %#v doesn't exist", path)
730 missing_current_api = true
731 }
732 }
733 }
734
735 if missing_current_api {
736 script := "build/soong/scripts/gen-java-current-api-files.sh"
737 p := android.ExistentPathForSource(mctx, script)
738
739 if !p.Valid() {
740 panic(fmt.Sprintf("script file %s doesn't exist", script))
741 }
742
743 mctx.ModuleErrorf("One or more current api files are missing. "+
744 "You can update them by:\n"+
Paul Duffin37e0b772019-12-30 17:20:10 +0000745 "%s %q %s && m update-api",
Paul Duffind1b3a922020-01-22 11:57:20 +0000746 script, filepath.Join(mctx.ModuleDir(), apiDir),
747 strings.Join(activeScopes.Strings(func(s *apiScope) string { return s.apiFilePrefix }), " "))
Inseob Kim8098faa2019-03-18 10:19:51 +0900748 return
749 }
750
Paul Duffind1b3a922020-01-22 11:57:20 +0000751 for _, scope := range activeScopes {
752 module.createStubsLibrary(mctx, scope)
753 module.createStubsSources(mctx, scope)
Inseob Kimc0907f12019-02-08 21:00:45 +0900754 }
755
Paul Duffin43db9be2019-12-30 17:35:49 +0000756 if !proptools.Bool(module.sdkLibraryProperties.Api_only) {
757 // for runtime
758 module.createXmlFile(mctx)
759
760 // record java_sdk_library modules so that they are exported to make
761 javaSdkLibraries := javaSdkLibraries(mctx.Config())
762 javaSdkLibrariesLock.Lock()
763 defer javaSdkLibrariesLock.Unlock()
764 *javaSdkLibraries = append(*javaSdkLibraries, module.BaseModuleName())
765 }
Inseob Kimc0907f12019-02-08 21:00:45 +0900766}
767
768func (module *SdkLibrary) InitSdkLibraryProperties() {
Sundong Ahn054b19a2018-10-19 13:46:09 +0900769 module.AddProperties(
770 &module.sdkLibraryProperties,
771 &module.Library.Module.properties,
772 &module.Library.Module.dexpreoptProperties,
773 &module.Library.Module.deviceProperties,
774 &module.Library.Module.protoProperties,
775 )
776
777 module.Library.Module.properties.Installable = proptools.BoolPtr(true)
778 module.Library.Module.deviceProperties.IsSDKLibrary = true
Inseob Kimc0907f12019-02-08 21:00:45 +0900779}
Sundong Ahn054b19a2018-10-19 13:46:09 +0900780
Jaewoong Jung4f158ee2019-07-11 10:05:35 -0700781// java_sdk_library is a special Java library that provides optional platform APIs to apps.
782// In practice, it can be viewed as a combination of several modules: 1) stubs library that clients
783// are linked against to, 2) droiddoc module that internally generates API stubs source files,
784// 3) the real runtime shared library that implements the APIs, and 4) XML file for adding
785// the runtime lib to the classpath at runtime if requested via <uses-library>.
Inseob Kimc0907f12019-02-08 21:00:45 +0900786func SdkLibraryFactory() android.Module {
787 module := &SdkLibrary{}
788 module.InitSdkLibraryProperties()
Jooyung Han58f26ab2019-12-18 15:34:32 +0900789 android.InitApexModule(module)
Sundong Ahn054b19a2018-10-19 13:46:09 +0900790 InitJavaModule(module, android.HostAndDeviceSupported)
Colin Crossf8b860a2019-04-16 14:43:28 -0700791 android.AddLoadHook(module, func(ctx android.LoadHookContext) { module.CreateInternalModules(ctx) })
Jiyong Parkc678ad32018-04-10 13:07:10 +0900792 return module
793}
Colin Cross79c7c262019-04-17 11:11:46 -0700794
795//
796// SDK library prebuilts
797//
798
Paul Duffin56d44902020-01-31 13:36:25 +0000799// Properties associated with each api scope.
800type sdkLibraryScopeProperties struct {
Colin Cross79c7c262019-04-17 11:11:46 -0700801 Jars []string `android:"path"`
802
803 Sdk_version *string
804
Colin Cross79c7c262019-04-17 11:11:46 -0700805 // List of shared java libs that this module has dependencies to
806 Libs []string
Colin Cross79c7c262019-04-17 11:11:46 -0700807}
808
Paul Duffin56d44902020-01-31 13:36:25 +0000809type sdkLibraryImportProperties struct {
Paul Duffinfcfd7912020-01-31 17:54:30 +0000810 // List of shared java libs, common to all scopes, that this module has
811 // dependencies to
812 Libs []string
Paul Duffin56d44902020-01-31 13:36:25 +0000813}
814
Colin Cross79c7c262019-04-17 11:11:46 -0700815type sdkLibraryImport struct {
816 android.ModuleBase
817 android.DefaultableModuleBase
818 prebuilt android.Prebuilt
819
820 properties sdkLibraryImportProperties
821
Paul Duffin6a2bd112020-04-07 19:27:04 +0100822 // Map from api scope to the scope specific property structure.
823 scopeProperties map[*apiScope]*sdkLibraryScopeProperties
824
Paul Duffin56d44902020-01-31 13:36:25 +0000825 commonToSdkLibraryAndImport
Colin Cross79c7c262019-04-17 11:11:46 -0700826}
827
828var _ SdkLibraryDependency = (*sdkLibraryImport)(nil)
829
Paul Duffin6a2bd112020-04-07 19:27:04 +0100830// The type of a structure that contains a field of type sdkLibraryScopeProperties
831// for each apiscope in allApiScopes, e.g. something like:
832// struct {
833// Public sdkLibraryScopeProperties
834// System sdkLibraryScopeProperties
835// ...
836// }
837var allScopeStructType = createAllScopePropertiesStructType()
838
839// Dynamically create a structure type for each apiscope in allApiScopes.
840func createAllScopePropertiesStructType() reflect.Type {
841 var fields []reflect.StructField
842 for _, apiScope := range allApiScopes {
843 field := reflect.StructField{
844 Name: apiScope.fieldName,
845 Type: reflect.TypeOf(sdkLibraryScopeProperties{}),
846 }
847 fields = append(fields, field)
848 }
849
850 return reflect.StructOf(fields)
851}
852
853// Create an instance of the scope specific structure type and return a map
854// from apiscope to a pointer to each scope specific field.
855func createPropertiesInstance() (interface{}, map[*apiScope]*sdkLibraryScopeProperties) {
856 allScopePropertiesPtr := reflect.New(allScopeStructType)
857 allScopePropertiesStruct := allScopePropertiesPtr.Elem()
858 scopeProperties := make(map[*apiScope]*sdkLibraryScopeProperties)
859
860 for _, apiScope := range allApiScopes {
861 field := allScopePropertiesStruct.FieldByName(apiScope.fieldName)
862 scopeProperties[apiScope] = field.Addr().Interface().(*sdkLibraryScopeProperties)
863 }
864
865 return allScopePropertiesPtr.Interface(), scopeProperties
866}
867
Jaewoong Jung4f158ee2019-07-11 10:05:35 -0700868// java_sdk_library_import imports a prebuilt java_sdk_library.
Colin Cross79c7c262019-04-17 11:11:46 -0700869func sdkLibraryImportFactory() android.Module {
870 module := &sdkLibraryImport{}
871
Paul Duffin6a2bd112020-04-07 19:27:04 +0100872 allScopeProperties, scopeToProperties := createPropertiesInstance()
873 module.scopeProperties = scopeToProperties
874 module.AddProperties(&module.properties, allScopeProperties)
Colin Cross79c7c262019-04-17 11:11:46 -0700875
Paul Duffin0bdcb272020-02-06 15:24:57 +0000876 android.InitPrebuiltModule(module, &[]string{""})
Colin Cross79c7c262019-04-17 11:11:46 -0700877 InitJavaModule(module, android.HostAndDeviceSupported)
878
879 android.AddLoadHook(module, func(mctx android.LoadHookContext) { module.createInternalModules(mctx) })
880 return module
881}
882
883func (module *sdkLibraryImport) Prebuilt() *android.Prebuilt {
884 return &module.prebuilt
885}
886
887func (module *sdkLibraryImport) Name() string {
888 return module.prebuilt.Name(module.ModuleBase.Name())
889}
890
891func (module *sdkLibraryImport) createInternalModules(mctx android.LoadHookContext) {
Colin Cross79c7c262019-04-17 11:11:46 -0700892
Paul Duffin50061512020-01-21 16:31:05 +0000893 // If the build is configured to use prebuilts then force this to be preferred.
894 if mctx.Config().UnbundledBuildUsePrebuiltSdks() {
895 module.prebuilt.ForcePrefer()
896 }
897
Paul Duffin6a2bd112020-04-07 19:27:04 +0100898 for apiScope, scopeProperties := range module.scopeProperties {
Paul Duffin56d44902020-01-31 13:36:25 +0000899 if len(scopeProperties.Jars) == 0 {
900 continue
901 }
902
903 // Creates a java import for the jar with ".stubs" suffix
904 props := struct {
905 Name *string
906 Soc_specific *bool
907 Device_specific *bool
908 Product_specific *bool
909 System_ext_specific *bool
910 Sdk_version *string
911 Libs []string
912 Jars []string
Paul Duffin50061512020-01-21 16:31:05 +0000913 Prefer *bool
Paul Duffin56d44902020-01-31 13:36:25 +0000914 }{}
915
916 props.Name = proptools.StringPtr(apiScope.stubsModuleName(module.BaseModuleName()))
917 props.Sdk_version = scopeProperties.Sdk_version
Paul Duffinfcfd7912020-01-31 17:54:30 +0000918 // Prepend any of the libs from the legacy public properties to the libs for each of the
919 // scopes to avoid having to duplicate them in each scope.
920 props.Libs = append(module.properties.Libs, scopeProperties.Libs...)
Paul Duffin56d44902020-01-31 13:36:25 +0000921 props.Jars = scopeProperties.Jars
922
923 if module.SocSpecific() {
924 props.Soc_specific = proptools.BoolPtr(true)
925 } else if module.DeviceSpecific() {
926 props.Device_specific = proptools.BoolPtr(true)
927 } else if module.ProductSpecific() {
928 props.Product_specific = proptools.BoolPtr(true)
929 } else if module.SystemExtSpecific() {
930 props.System_ext_specific = proptools.BoolPtr(true)
931 }
932
Paul Duffin50061512020-01-21 16:31:05 +0000933 // If the build should use prebuilt sdks then set prefer to true on the stubs library.
934 // That will cause the prebuilt version of the stubs to override the source version.
935 if mctx.Config().UnbundledBuildUsePrebuiltSdks() {
936 props.Prefer = proptools.BoolPtr(true)
937 }
938
Paul Duffin56d44902020-01-31 13:36:25 +0000939 mctx.CreateModule(ImportFactory, &props)
940 }
Colin Cross79c7c262019-04-17 11:11:46 -0700941
942 javaSdkLibraries := javaSdkLibraries(mctx.Config())
943 javaSdkLibrariesLock.Lock()
944 defer javaSdkLibrariesLock.Unlock()
945 *javaSdkLibraries = append(*javaSdkLibraries, module.BaseModuleName())
946}
947
948func (module *sdkLibraryImport) DepsMutator(ctx android.BottomUpMutatorContext) {
Paul Duffin6a2bd112020-04-07 19:27:04 +0100949 for apiScope, scopeProperties := range module.scopeProperties {
Paul Duffin56d44902020-01-31 13:36:25 +0000950 if len(scopeProperties.Jars) == 0 {
951 continue
952 }
953
954 // Add dependencies to the prebuilt stubs library
955 ctx.AddVariationDependencies(nil, apiScope.stubsTag, apiScope.stubsModuleName(module.BaseModuleName()))
956 }
Colin Cross79c7c262019-04-17 11:11:46 -0700957}
958
959func (module *sdkLibraryImport) GenerateAndroidBuildActions(ctx android.ModuleContext) {
960 // Record the paths to the prebuilt stubs library.
961 ctx.VisitDirectDeps(func(to android.Module) {
962 tag := ctx.OtherModuleDependencyTag(to)
963
Paul Duffin56d44902020-01-31 13:36:25 +0000964 if lib, ok := to.(Dependency); ok {
965 if scopeTag, ok := tag.(scopeDependencyTag); ok {
966 apiScope := scopeTag.apiScope
967 scopePaths := module.getScopePaths(apiScope)
968 scopePaths.stubsHeaderPath = lib.HeaderJars()
969 }
Colin Cross79c7c262019-04-17 11:11:46 -0700970 }
971 })
972}
973
Paul Duffin56d44902020-01-31 13:36:25 +0000974func (module *sdkLibraryImport) sdkJars(
975 ctx android.BaseModuleContext,
976 sdkVersion sdkSpec) android.Paths {
977
Paul Duffin50061512020-01-21 16:31:05 +0000978 // If a specific numeric version has been requested then use prebuilt versions of the sdk.
979 if sdkVersion.version.isNumbered() {
980 return PrebuiltJars(ctx, module.BaseModuleName(), sdkVersion)
981 }
982
Paul Duffin56d44902020-01-31 13:36:25 +0000983 var apiScope *apiScope
984 switch sdkVersion.kind {
985 case sdkSystem:
986 apiScope = apiScopeSystem
987 case sdkTest:
988 apiScope = apiScopeTest
989 default:
990 apiScope = apiScopePublic
991 }
992
993 paths := module.getScopePaths(apiScope)
994 return paths.stubsHeaderPath
995}
996
Colin Cross79c7c262019-04-17 11:11:46 -0700997// to satisfy SdkLibraryDependency interface
Jiyong Park6a927c42020-01-21 02:03:43 +0900998func (module *sdkLibraryImport) SdkHeaderJars(ctx android.BaseModuleContext, sdkVersion sdkSpec) android.Paths {
Colin Cross79c7c262019-04-17 11:11:46 -0700999 // This module is just a wrapper for the prebuilt stubs.
Paul Duffin56d44902020-01-31 13:36:25 +00001000 return module.sdkJars(ctx, sdkVersion)
Colin Cross79c7c262019-04-17 11:11:46 -07001001}
1002
1003// to satisfy SdkLibraryDependency interface
Jiyong Park6a927c42020-01-21 02:03:43 +09001004func (module *sdkLibraryImport) SdkImplementationJars(ctx android.BaseModuleContext, sdkVersion sdkSpec) android.Paths {
Colin Cross79c7c262019-04-17 11:11:46 -07001005 // This module is just a wrapper for the stubs.
Paul Duffin56d44902020-01-31 13:36:25 +00001006 return module.sdkJars(ctx, sdkVersion)
Colin Cross79c7c262019-04-17 11:11:46 -07001007}
Jiyong Parke3833882020-02-17 17:28:10 +09001008
1009//
1010// java_sdk_library_xml
1011//
1012type sdkLibraryXml struct {
1013 android.ModuleBase
1014 android.DefaultableModuleBase
1015 android.ApexModuleBase
1016
1017 properties sdkLibraryXmlProperties
1018
1019 outputFilePath android.OutputPath
1020 installDirPath android.InstallPath
1021}
1022
1023type sdkLibraryXmlProperties struct {
1024 // canonical name of the lib
1025 Lib_name *string
1026}
1027
1028// java_sdk_library_xml builds the permission xml file for a java_sdk_library.
1029// Not to be used directly by users. java_sdk_library internally uses this.
1030func sdkLibraryXmlFactory() android.Module {
1031 module := &sdkLibraryXml{}
1032
1033 module.AddProperties(&module.properties)
1034
1035 android.InitApexModule(module)
1036 android.InitAndroidArchModule(module, android.DeviceSupported, android.MultilibCommon)
1037
1038 return module
1039}
1040
1041// from android.PrebuiltEtcModule
1042func (module *sdkLibraryXml) SubDir() string {
1043 return "permissions"
1044}
1045
1046// from android.PrebuiltEtcModule
1047func (module *sdkLibraryXml) OutputFile() android.OutputPath {
1048 return module.outputFilePath
1049}
1050
1051// from android.ApexModule
1052func (module *sdkLibraryXml) AvailableFor(what string) bool {
1053 return true
1054}
1055
1056func (module *sdkLibraryXml) DepsMutator(ctx android.BottomUpMutatorContext) {
1057 // do nothing
1058}
1059
1060// File path to the runtime implementation library
1061func (module *sdkLibraryXml) implPath() string {
1062 implName := proptools.String(module.properties.Lib_name)
1063 if apexName := module.ApexName(); apexName != "" {
1064 // TODO(b/146468504): ApexName() is only a soong module name, not apex name.
1065 // In most cases, this works fine. But when apex_name is set or override_apex is used
1066 // this can be wrong.
1067 return fmt.Sprintf("/apex/%s/javalib/%s.jar", apexName, implName)
1068 }
1069 partition := "system"
1070 if module.SocSpecific() {
1071 partition = "vendor"
1072 } else if module.DeviceSpecific() {
1073 partition = "odm"
1074 } else if module.ProductSpecific() {
1075 partition = "product"
1076 } else if module.SystemExtSpecific() {
1077 partition = "system_ext"
1078 }
1079 return "/" + partition + "/framework/" + implName + ".jar"
1080}
1081
1082func (module *sdkLibraryXml) GenerateAndroidBuildActions(ctx android.ModuleContext) {
1083 libName := proptools.String(module.properties.Lib_name)
1084 xmlContent := fmt.Sprintf(permissionsTemplate, libName, module.implPath())
1085
1086 module.outputFilePath = android.PathForModuleOut(ctx, libName+".xml").OutputPath
1087 rule := android.NewRuleBuilder()
1088 rule.Command().
1089 Text("/bin/bash -c \"echo -e '" + xmlContent + "'\" > ").
1090 Output(module.outputFilePath)
1091
1092 rule.Build(pctx, ctx, "java_sdk_xml", "Permission XML")
1093
1094 module.installDirPath = android.PathForModuleInstall(ctx, "etc", module.SubDir())
1095}
1096
1097func (module *sdkLibraryXml) AndroidMkEntries() []android.AndroidMkEntries {
1098 if !module.IsForPlatform() {
1099 return []android.AndroidMkEntries{android.AndroidMkEntries{
1100 Disabled: true,
1101 }}
1102 }
1103
1104 return []android.AndroidMkEntries{android.AndroidMkEntries{
1105 Class: "ETC",
1106 OutputFile: android.OptionalPathForPath(module.outputFilePath),
1107 ExtraEntries: []android.AndroidMkExtraEntriesFunc{
1108 func(entries *android.AndroidMkEntries) {
1109 entries.SetString("LOCAL_MODULE_TAGS", "optional")
1110 entries.SetString("LOCAL_MODULE_PATH", module.installDirPath.ToMakePath().String())
1111 entries.SetString("LOCAL_INSTALLED_MODULE_STEM", module.outputFilePath.Base())
1112 },
1113 },
1114 }}
1115}