blob: efee7da23c97b1081b047746fe1e2d9fe2dc9d85 [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 (
18 "android/soong/android"
Paul Duffind1b3a922020-01-22 11:57:20 +000019
Jiyong Parkc678ad32018-04-10 13:07:10 +090020 "fmt"
21 "path"
Sundong Ahn054b19a2018-10-19 13:46:09 +090022 "path/filepath"
Jiyong Park82484c02018-04-23 21:41:26 +090023 "sort"
Jiyong Parkc678ad32018-04-10 13:07:10 +090024 "strings"
Jiyong Park82484c02018-04-23 21:41:26 +090025 "sync"
Jiyong Parkc678ad32018-04-10 13:07:10 +090026
Paul Duffind1b3a922020-01-22 11:57:20 +000027 "github.com/google/blueprint"
Jiyong Parkc678ad32018-04-10 13:07:10 +090028 "github.com/google/blueprint/proptools"
29)
30
Jooyung Han58f26ab2019-12-18 15:34:32 +090031const (
Jiyong Parkc678ad32018-04-10 13:07:10 +090032 sdkStubsLibrarySuffix = ".stubs"
33 sdkSystemApiSuffix = ".system"
Jiyong Parkdf130542018-04-27 16:29:21 +090034 sdkTestApiSuffix = ".test"
Paul Duffin91b883d2020-02-11 13:05:28 +000035 sdkStubsSourceSuffix = ".stubs.source"
Jiyong Parkc678ad32018-04-10 13:07:10 +090036 sdkXmlFileSuffix = ".xml"
Jiyong Parke3833882020-02-17 17:28:10 +090037 permissionsTemplate = `<?xml version=\"1.0\" encoding=\"utf-8\"?>\n` +
Jooyung Han624058e2019-12-24 18:38:06 +090038 `<!-- Copyright (C) 2018 The Android Open Source Project\n` +
39 `\n` +
Jiyong Parke3833882020-02-17 17:28:10 +090040 ` Licensed under the Apache License, Version 2.0 (the \"License\");\n` +
Jooyung Han624058e2019-12-24 18:38:06 +090041 ` you may not use this file except in compliance with the License.\n` +
42 ` You may obtain a copy of the License at\n` +
43 `\n` +
44 ` http://www.apache.org/licenses/LICENSE-2.0\n` +
45 `\n` +
46 ` Unless required by applicable law or agreed to in writing, software\n` +
Jiyong Parke3833882020-02-17 17:28:10 +090047 ` distributed under the License is distributed on an \"AS IS\" BASIS,\n` +
Jooyung Han624058e2019-12-24 18:38:06 +090048 ` WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\n` +
49 ` See the License for the specific language governing permissions and\n` +
50 ` limitations under the License.\n` +
51 `-->\n` +
52 `<permissions>\n` +
Jiyong Parke3833882020-02-17 17:28:10 +090053 ` <library name=\"%s\" file=\"%s\"/>\n` +
Jooyung Han624058e2019-12-24 18:38:06 +090054 `</permissions>\n`
Jiyong Parkc678ad32018-04-10 13:07:10 +090055)
56
Paul Duffind1b3a922020-01-22 11:57:20 +000057// A tag to associated a dependency with a specific api scope.
58type scopeDependencyTag struct {
59 blueprint.BaseDependencyTag
60 name string
61 apiScope *apiScope
62}
63
64// Provides information about an api scope, e.g. public, system, test.
65type apiScope struct {
66 // The name of the api scope, e.g. public, system, test
67 name string
68
69 // The tag to use to depend on the stubs library module.
70 stubsTag scopeDependencyTag
71
72 // The tag to use to depend on the stubs
73 apiFileTag scopeDependencyTag
74
75 // The scope specific prefix to add to the api file base of "current.txt" or "removed.txt".
76 apiFilePrefix string
77
78 // The scope specific prefix to add to the sdk library module name to construct a scope specific
79 // module name.
80 moduleSuffix string
81
82 // The suffix to add to the make variable that references the location of the api file.
83 apiFileMakeVariableSuffix string
84
85 // SDK version that the stubs library is built against. Note that this is always
86 // *current. Older stubs library built with a numbered SDK version is created from
87 // the prebuilt jar.
88 sdkVersion string
Paul Duffin1fb487d2020-04-07 18:50:10 +010089
90 // Extra arguments to pass to droidstubs for this scope.
91 droidstubsArgs []string
Paul Duffind1b3a922020-01-22 11:57:20 +000092}
93
94// Initialize a scope, creating and adding appropriate dependency tags
95func initApiScope(scope *apiScope) *apiScope {
96 //apiScope := &scope
97 scope.stubsTag = scopeDependencyTag{
98 name: scope.name + "-stubs",
99 apiScope: scope,
100 }
101 scope.apiFileTag = scopeDependencyTag{
102 name: scope.name + "-api",
103 apiScope: scope,
104 }
105 return scope
106}
107
108func (scope *apiScope) stubsModuleName(baseName string) string {
109 return baseName + sdkStubsLibrarySuffix + scope.moduleSuffix
110}
111
112func (scope *apiScope) docsModuleName(baseName string) string {
Paul Duffin91b883d2020-02-11 13:05:28 +0000113 return baseName + sdkStubsSourceSuffix + scope.moduleSuffix
Paul Duffind1b3a922020-01-22 11:57:20 +0000114}
115
116type apiScopes []*apiScope
117
118func (scopes apiScopes) Strings(accessor func(*apiScope) string) []string {
119 var list []string
120 for _, scope := range scopes {
121 list = append(list, accessor(scope))
122 }
123 return list
124}
125
Jiyong Parkc678ad32018-04-10 13:07:10 +0900126var (
Paul Duffind1b3a922020-01-22 11:57:20 +0000127 apiScopePublic = initApiScope(&apiScope{
128 name: "public",
129 sdkVersion: "current",
130 })
131 apiScopeSystem = initApiScope(&apiScope{
132 name: "system",
133 apiFilePrefix: "system-",
134 moduleSuffix: sdkSystemApiSuffix,
135 apiFileMakeVariableSuffix: "_SYSTEM",
136 sdkVersion: "system_current",
Paul Duffin1fb487d2020-04-07 18:50:10 +0100137 droidstubsArgs: []string{"-showAnnotation android.annotation.SystemApi"},
Paul Duffind1b3a922020-01-22 11:57:20 +0000138 })
139 apiScopeTest = initApiScope(&apiScope{
140 name: "test",
141 apiFilePrefix: "test-",
142 moduleSuffix: sdkTestApiSuffix,
143 apiFileMakeVariableSuffix: "_TEST",
144 sdkVersion: "test_current",
Paul Duffin1fb487d2020-04-07 18:50:10 +0100145 droidstubsArgs: []string{"-showAnnotation android.annotation.TestApi"},
Paul Duffind1b3a922020-01-22 11:57:20 +0000146 })
147 allApiScopes = apiScopes{
148 apiScopePublic,
149 apiScopeSystem,
150 apiScopeTest,
151 }
Jiyong Parkc678ad32018-04-10 13:07:10 +0900152)
153
Jiyong Park82484c02018-04-23 21:41:26 +0900154var (
155 javaSdkLibrariesLock sync.Mutex
156)
157
Jiyong Parkc678ad32018-04-10 13:07:10 +0900158// TODO: these are big features that are currently missing
Jiyong Park1be96912018-05-28 18:02:19 +0900159// 1) disallowing linking to the runtime shared lib
160// 2) HTML generation
Jiyong Parkc678ad32018-04-10 13:07:10 +0900161
162func init() {
Paul Duffin43dc1cc2019-12-19 11:18:54 +0000163 RegisterSdkLibraryBuildComponents(android.InitRegistrationContext)
Jiyong Parkc678ad32018-04-10 13:07:10 +0900164
Jiyong Park82484c02018-04-23 21:41:26 +0900165 android.RegisterMakeVarsProvider(pctx, func(ctx android.MakeVarsContext) {
166 javaSdkLibraries := javaSdkLibraries(ctx.Config())
167 sort.Strings(*javaSdkLibraries)
168 ctx.Strict("JAVA_SDK_LIBRARIES", strings.Join(*javaSdkLibraries, " "))
169 })
Jiyong Parkc678ad32018-04-10 13:07:10 +0900170}
171
Paul Duffin43dc1cc2019-12-19 11:18:54 +0000172func RegisterSdkLibraryBuildComponents(ctx android.RegistrationContext) {
173 ctx.RegisterModuleType("java_sdk_library", SdkLibraryFactory)
174 ctx.RegisterModuleType("java_sdk_library_import", sdkLibraryImportFactory)
175}
176
Jiyong Parkc678ad32018-04-10 13:07:10 +0900177type sdkLibraryProperties struct {
Sundong Ahnf043cf62018-06-25 16:04:37 +0900178 // List of Java libraries that will be in the classpath when building stubs
179 Stub_only_libs []string `android:"arch_variant"`
180
Paul Duffin7a586d32019-12-30 17:09:34 +0000181 // list of package names that will be documented and publicized as API.
182 // This allows the API to be restricted to a subset of the source files provided.
183 // If this is unspecified then all the source files will be treated as being part
184 // of the API.
Jiyong Parkc678ad32018-04-10 13:07:10 +0900185 Api_packages []string
186
Jiyong Park5a2c9d72018-05-01 22:25:41 +0900187 // list of package names that must be hidden from the API
188 Hidden_api_packages []string
189
Paul Duffin749f98f2019-12-30 17:23:46 +0000190 // the relative path to the directory containing the api specification files.
191 // Defaults to "api".
192 Api_dir *string
193
Paul Duffin43db9be2019-12-30 17:35:49 +0000194 // If set to true there is no runtime library.
195 Api_only *bool
196
Paul Duffin11512472019-02-11 15:55:17 +0000197 // local files that are used within user customized droiddoc options.
198 Droiddoc_option_files []string
199
200 // additional droiddoc options
201 // Available variables for substitution:
202 //
203 // $(location <label>): the path to the droiddoc_option_files with name <label>
Sundong Ahndd567f92018-07-31 17:19:11 +0900204 Droiddoc_options []string
205
Sundong Ahn054b19a2018-10-19 13:46:09 +0900206 // a list of top-level directories containing files to merge qualifier annotations
207 // (i.e. those intended to be included in the stubs written) from.
208 Merge_annotations_dirs []string
209
210 // a list of top-level directories containing Java stub files to merge show/hide annotations from.
211 Merge_inclusion_annotations_dirs []string
212
213 // If set to true, the path of dist files is apistubs/core. Defaults to false.
214 Core_lib *bool
215
Sundong Ahn80a87b32019-05-13 15:02:50 +0900216 // don't create dist rules.
217 No_dist *bool `blueprint:"mutated"`
218
Paul Duffin37e0b772019-12-30 17:20:10 +0000219 // indicates whether system and test apis should be managed.
220 Has_system_and_test_apis bool `blueprint:"mutated"`
221
Jiyong Parkc678ad32018-04-10 13:07:10 +0900222 // TODO: determines whether to create HTML doc or not
223 //Html_doc *bool
224}
225
Paul Duffind1b3a922020-01-22 11:57:20 +0000226type scopePaths struct {
227 stubsHeaderPath android.Paths
228 stubsImplPath android.Paths
229 apiFilePath android.Path
230}
231
Paul Duffin56d44902020-01-31 13:36:25 +0000232// Common code between sdk library and sdk library import
233type commonToSdkLibraryAndImport struct {
234 scopePaths map[*apiScope]*scopePaths
235}
236
237func (c *commonToSdkLibraryAndImport) getScopePaths(scope *apiScope) *scopePaths {
238 if c.scopePaths == nil {
239 c.scopePaths = make(map[*apiScope]*scopePaths)
240 }
241 paths := c.scopePaths[scope]
242 if paths == nil {
243 paths = &scopePaths{}
244 c.scopePaths[scope] = paths
245 }
246
247 return paths
248}
249
Inseob Kimc0907f12019-02-08 21:00:45 +0900250type SdkLibrary struct {
Sundong Ahn054b19a2018-10-19 13:46:09 +0900251 Library
Jiyong Parkc678ad32018-04-10 13:07:10 +0900252
Sundong Ahn054b19a2018-10-19 13:46:09 +0900253 sdkLibraryProperties sdkLibraryProperties
Jiyong Parkc678ad32018-04-10 13:07:10 +0900254
Paul Duffin56d44902020-01-31 13:36:25 +0000255 commonToSdkLibraryAndImport
Jiyong Parkc678ad32018-04-10 13:07:10 +0900256}
257
Inseob Kimc0907f12019-02-08 21:00:45 +0900258var _ Dependency = (*SdkLibrary)(nil)
259var _ SdkLibraryDependency = (*SdkLibrary)(nil)
Colin Cross897d2ed2019-02-11 14:03:51 -0800260
Paul Duffind1b3a922020-01-22 11:57:20 +0000261func (module *SdkLibrary) getActiveApiScopes() apiScopes {
262 if module.sdkLibraryProperties.Has_system_and_test_apis {
263 return allApiScopes
264 } else {
265 return apiScopes{apiScopePublic}
266 }
267}
268
Paul Duffine74ac732020-02-06 13:51:46 +0000269var xmlPermissionsFileTag = dependencyTag{name: "xml-permissions-file"}
270
Jiyong Parke3833882020-02-17 17:28:10 +0900271func IsXmlPermissionsFileDepTag(depTag blueprint.DependencyTag) bool {
272 if dt, ok := depTag.(dependencyTag); ok {
273 return dt == xmlPermissionsFileTag
274 }
275 return false
276}
277
Inseob Kimc0907f12019-02-08 21:00:45 +0900278func (module *SdkLibrary) DepsMutator(ctx android.BottomUpMutatorContext) {
Paul Duffind1b3a922020-01-22 11:57:20 +0000279 for _, apiScope := range module.getActiveApiScopes() {
280 // Add dependencies to the stubs library
Paul Duffin50061512020-01-21 16:31:05 +0000281 ctx.AddVariationDependencies(nil, apiScope.stubsTag, module.stubsName(apiScope))
Paul Duffind1b3a922020-01-22 11:57:20 +0000282
Paul Duffin50061512020-01-21 16:31:05 +0000283 // And the api file
Paul Duffind1b3a922020-01-22 11:57:20 +0000284 ctx.AddVariationDependencies(nil, apiScope.apiFileTag, module.docsName(apiScope))
Sundong Ahn054b19a2018-10-19 13:46:09 +0900285 }
286
Paul Duffine74ac732020-02-06 13:51:46 +0000287 if !proptools.Bool(module.sdkLibraryProperties.Api_only) {
288 // Add dependency to the rule for generating the xml permissions file
Jiyong Parke3833882020-02-17 17:28:10 +0900289 ctx.AddDependency(module, xmlPermissionsFileTag, module.xmlFileName())
Paul Duffine74ac732020-02-06 13:51:46 +0000290 }
291
Sundong Ahn054b19a2018-10-19 13:46:09 +0900292 module.Library.deps(ctx)
Jiyong Parkc678ad32018-04-10 13:07:10 +0900293}
294
Inseob Kimc0907f12019-02-08 21:00:45 +0900295func (module *SdkLibrary) GenerateAndroidBuildActions(ctx android.ModuleContext) {
Paul Duffin43db9be2019-12-30 17:35:49 +0000296 // Don't build an implementation library if this is api only.
297 if !proptools.Bool(module.sdkLibraryProperties.Api_only) {
298 module.Library.GenerateAndroidBuildActions(ctx)
299 }
Sundong Ahn054b19a2018-10-19 13:46:09 +0900300
Sundong Ahn57368eb2018-07-06 11:20:23 +0900301 // Record the paths to the header jars of the library (stubs and impl).
Paul Duffind1b3a922020-01-22 11:57:20 +0000302 // When this java_sdk_library is depended upon from others via "libs" property,
Jiyong Parkc678ad32018-04-10 13:07:10 +0900303 // the recorded paths will be returned depending on the link type of the caller.
304 ctx.VisitDirectDeps(func(to android.Module) {
305 otherName := ctx.OtherModuleName(to)
306 tag := ctx.OtherModuleDependencyTag(to)
307
Sundong Ahn57368eb2018-07-06 11:20:23 +0900308 if lib, ok := to.(Dependency); ok {
Paul Duffind1b3a922020-01-22 11:57:20 +0000309 if scopeTag, ok := tag.(scopeDependencyTag); ok {
310 apiScope := scopeTag.apiScope
311 scopePaths := module.getScopePaths(apiScope)
312 scopePaths.stubsHeaderPath = lib.HeaderJars()
313 scopePaths.stubsImplPath = lib.ImplementationJars()
Jiyong Parkc678ad32018-04-10 13:07:10 +0900314 }
315 }
Sundong Ahn20e998b2018-07-24 11:19:26 +0900316 if doc, ok := to.(ApiFilePath); ok {
Paul Duffind1b3a922020-01-22 11:57:20 +0000317 if scopeTag, ok := tag.(scopeDependencyTag); ok {
318 apiScope := scopeTag.apiScope
319 scopePaths := module.getScopePaths(apiScope)
320 scopePaths.apiFilePath = doc.ApiFilePath()
321 } else {
Sundong Ahn20e998b2018-07-24 11:19:26 +0900322 ctx.ModuleErrorf("depends on module %q of unknown tag %q", otherName, tag)
323 }
324 }
Jiyong Parkc678ad32018-04-10 13:07:10 +0900325 })
326}
327
Jiyong Park0b0e1b92019-12-03 13:24:29 +0900328func (module *SdkLibrary) AndroidMkEntries() []android.AndroidMkEntries {
Paul Duffin43db9be2019-12-30 17:35:49 +0000329 if proptools.Bool(module.sdkLibraryProperties.Api_only) {
330 return nil
331 }
Jiyong Park0b0e1b92019-12-03 13:24:29 +0900332 entriesList := module.Library.AndroidMkEntries()
333 entries := &entriesList[0]
Jaewoong Jungb0c127c2019-08-29 14:56:03 -0700334 entries.Required = append(entries.Required, module.xmlFileName())
Jiyong Park0b0e1b92019-12-03 13:24:29 +0900335 return entriesList
Jiyong Park82484c02018-04-23 21:41:26 +0900336}
337
Jiyong Parkc678ad32018-04-10 13:07:10 +0900338// Module name of the stubs library
Paul Duffind1b3a922020-01-22 11:57:20 +0000339func (module *SdkLibrary) stubsName(apiScope *apiScope) string {
340 return apiScope.stubsModuleName(module.BaseModuleName())
Jiyong Parkc678ad32018-04-10 13:07:10 +0900341}
342
343// Module name of the docs
Paul Duffind1b3a922020-01-22 11:57:20 +0000344func (module *SdkLibrary) docsName(apiScope *apiScope) string {
345 return apiScope.docsModuleName(module.BaseModuleName())
Jiyong Parkc678ad32018-04-10 13:07:10 +0900346}
347
348// Module name of the runtime implementation library
Inseob Kimc0907f12019-02-08 21:00:45 +0900349func (module *SdkLibrary) implName() string {
Sundong Ahn054b19a2018-10-19 13:46:09 +0900350 return module.BaseModuleName()
Jiyong Parkc678ad32018-04-10 13:07:10 +0900351}
352
Jiyong Parkc678ad32018-04-10 13:07:10 +0900353// Module name of the XML file for the lib
Inseob Kimc0907f12019-02-08 21:00:45 +0900354func (module *SdkLibrary) xmlFileName() string {
Jiyong Parkc678ad32018-04-10 13:07:10 +0900355 return module.BaseModuleName() + sdkXmlFileSuffix
356}
357
Anton Hansson5fd5d242020-03-27 19:43:19 +0000358// The dist path of the stub artifacts
359func (module *SdkLibrary) apiDistPath(apiScope *apiScope) string {
360 if module.ModuleBase.Owner() != "" {
361 return path.Join("apistubs", module.ModuleBase.Owner(), apiScope.name)
362 } else if Bool(module.sdkLibraryProperties.Core_lib) {
363 return path.Join("apistubs", "core", apiScope.name)
364 } else {
365 return path.Join("apistubs", "android", apiScope.name)
366 }
367}
368
Paul Duffin12ceb462019-12-24 20:31:31 +0000369// Get the sdk version for use when compiling the stubs library.
Paul Duffind1b3a922020-01-22 11:57:20 +0000370func (module *SdkLibrary) sdkVersionForStubsLibrary(mctx android.LoadHookContext, apiScope *apiScope) string {
Paul Duffin12ceb462019-12-24 20:31:31 +0000371 sdkDep := decodeSdkDep(mctx, sdkContext(&module.Library))
372 if sdkDep.hasStandardLibs() {
373 // If building against a standard sdk then use the sdk version appropriate for the scope.
Paul Duffind1b3a922020-01-22 11:57:20 +0000374 return apiScope.sdkVersion
Paul Duffin12ceb462019-12-24 20:31:31 +0000375 } else {
376 // Otherwise, use no system module.
377 return "none"
378 }
379}
380
Jiyong Parkc678ad32018-04-10 13:07:10 +0900381// $(INTERNAL_PLATFORM_<apiTagName>_API_FILE) points to the generated
382// api file for the current source
383// TODO: remove this when apicheck is done in soong
Paul Duffind1b3a922020-01-22 11:57:20 +0000384func (module *SdkLibrary) apiTagName(apiScope *apiScope) string {
385 return strings.Replace(strings.ToUpper(module.BaseModuleName()), ".", "_", -1) + apiScope.apiFileMakeVariableSuffix
Jiyong Parkc678ad32018-04-10 13:07:10 +0900386}
387
Paul Duffind1b3a922020-01-22 11:57:20 +0000388func (module *SdkLibrary) latestApiFilegroupName(apiScope *apiScope) string {
389 return ":" + module.BaseModuleName() + ".api." + apiScope.name + ".latest"
Jiyong Park58c518b2018-05-12 22:29:12 +0900390}
Jiyong Parkc678ad32018-04-10 13:07:10 +0900391
Paul Duffind1b3a922020-01-22 11:57:20 +0000392func (module *SdkLibrary) latestRemovedApiFilegroupName(apiScope *apiScope) string {
393 return ":" + module.BaseModuleName() + "-removed.api." + apiScope.name + ".latest"
Jiyong Parkc678ad32018-04-10 13:07:10 +0900394}
395
396// Creates a static java library that has API stubs
Paul Duffind1b3a922020-01-22 11:57:20 +0000397func (module *SdkLibrary) createStubsLibrary(mctx android.LoadHookContext, apiScope *apiScope) {
Jiyong Parkc678ad32018-04-10 13:07:10 +0900398 props := struct {
Sundong Ahn0d7dff42019-12-04 12:53:44 +0900399 Name *string
400 Srcs []string
Paul Duffin367ab912019-12-23 19:40:36 +0000401 Installable *bool
Sundong Ahn0d7dff42019-12-04 12:53:44 +0900402 Sdk_version *string
Paul Duffin12ceb462019-12-24 20:31:31 +0000403 System_modules *string
Paul Duffinab8da5d2020-02-07 16:12:04 +0000404 Patch_module *string
Sundong Ahn0d7dff42019-12-04 12:53:44 +0900405 Libs []string
406 Soc_specific *bool
407 Device_specific *bool
408 Product_specific *bool
409 System_ext_specific *bool
410 Compile_dex *bool
Sundong Ahn0d7dff42019-12-04 12:53:44 +0900411 Java_version *string
412 Product_variables struct {
Jiyong Park82484c02018-04-23 21:41:26 +0900413 Pdk struct {
414 Enabled *bool
415 }
Jiyong Parkc678ad32018-04-10 13:07:10 +0900416 }
Sundong Ahn054b19a2018-10-19 13:46:09 +0900417 Openjdk9 struct {
418 Srcs []string
419 Javacflags []string
420 }
Anton Hansson5fd5d242020-03-27 19:43:19 +0000421 Dist struct {
422 Targets []string
423 Dest *string
424 Dir *string
425 Tag *string
426 }
Jiyong Parkc678ad32018-04-10 13:07:10 +0900427 }{}
428
Jiyong Parkdf130542018-04-27 16:29:21 +0900429 props.Name = proptools.StringPtr(module.stubsName(apiScope))
Jiyong Parkc678ad32018-04-10 13:07:10 +0900430 // sources are generated from the droiddoc
Jiyong Parkdf130542018-04-27 16:29:21 +0900431 props.Srcs = []string{":" + module.docsName(apiScope)}
Paul Duffin12ceb462019-12-24 20:31:31 +0000432 sdkVersion := module.sdkVersionForStubsLibrary(mctx, apiScope)
Paul Duffin52d398a2019-06-11 12:31:14 +0100433 props.Sdk_version = proptools.StringPtr(sdkVersion)
Paul Duffin12ceb462019-12-24 20:31:31 +0000434 props.System_modules = module.Library.Module.deviceProperties.System_modules
Paul Duffinab8da5d2020-02-07 16:12:04 +0000435 props.Patch_module = module.Library.Module.properties.Patch_module
Paul Duffin367ab912019-12-23 19:40:36 +0000436 props.Installable = proptools.BoolPtr(false)
Sundong Ahn054b19a2018-10-19 13:46:09 +0900437 props.Libs = module.sdkLibraryProperties.Stub_only_libs
Jiyong Park82484c02018-04-23 21:41:26 +0900438 props.Product_variables.Pdk.Enabled = proptools.BoolPtr(false)
Sundong Ahn054b19a2018-10-19 13:46:09 +0900439 props.Openjdk9.Srcs = module.Library.Module.properties.Openjdk9.Srcs
440 props.Openjdk9.Javacflags = module.Library.Module.properties.Openjdk9.Javacflags
441 props.Java_version = module.Library.Module.properties.Java_version
442 if module.Library.Module.deviceProperties.Compile_dex != nil {
443 props.Compile_dex = module.Library.Module.deviceProperties.Compile_dex
Sundong Ahndd567f92018-07-31 17:19:11 +0900444 }
Jiyong Parkc678ad32018-04-10 13:07:10 +0900445
446 if module.SocSpecific() {
447 props.Soc_specific = proptools.BoolPtr(true)
448 } else if module.DeviceSpecific() {
449 props.Device_specific = proptools.BoolPtr(true)
450 } else if module.ProductSpecific() {
451 props.Product_specific = proptools.BoolPtr(true)
Sundong Ahn0d7dff42019-12-04 12:53:44 +0900452 } else if module.SystemExtSpecific() {
453 props.System_ext_specific = proptools.BoolPtr(true)
Jiyong Parkc678ad32018-04-10 13:07:10 +0900454 }
Anton Hansson5fd5d242020-03-27 19:43:19 +0000455 // Dist the class jar artifact for sdk builds.
456 if !Bool(module.sdkLibraryProperties.No_dist) {
457 props.Dist.Targets = []string{"sdk", "win_sdk"}
458 props.Dist.Dest = proptools.StringPtr(fmt.Sprintf("%v.jar", module.BaseModuleName()))
459 props.Dist.Dir = proptools.StringPtr(module.apiDistPath(apiScope))
460 props.Dist.Tag = proptools.StringPtr(".jar")
461 }
Jiyong Parkc678ad32018-04-10 13:07:10 +0900462
Colin Cross84dfc3d2019-09-25 11:33:01 -0700463 mctx.CreateModule(LibraryFactory, &props)
Jiyong Parkc678ad32018-04-10 13:07:10 +0900464}
465
Paul Duffin6d0886e2020-04-07 18:49:53 +0100466// Creates a droidstubs module that creates stubs source files from the given full source
Jiyong Parkc678ad32018-04-10 13:07:10 +0900467// files
Paul Duffind1b3a922020-01-22 11:57:20 +0000468func (module *SdkLibrary) createStubsSources(mctx android.LoadHookContext, apiScope *apiScope) {
Jiyong Parkc678ad32018-04-10 13:07:10 +0900469 props := struct {
Sundong Ahn054b19a2018-10-19 13:46:09 +0900470 Name *string
471 Srcs []string
472 Installable *bool
Paul Duffin52d398a2019-06-11 12:31:14 +0100473 Sdk_version *string
Paul Duffin12ceb462019-12-24 20:31:31 +0000474 System_modules *string
Sundong Ahn054b19a2018-10-19 13:46:09 +0900475 Libs []string
Paul Duffin11512472019-02-11 15:55:17 +0000476 Arg_files []string
Sundong Ahn054b19a2018-10-19 13:46:09 +0900477 Args *string
478 Api_tag_name *string
479 Api_filename *string
480 Removed_api_filename *string
Sundong Ahn054b19a2018-10-19 13:46:09 +0900481 Java_version *string
482 Merge_annotations_dirs []string
483 Merge_inclusion_annotations_dirs []string
484 Check_api struct {
Inseob Kim38449af2019-02-28 14:24:05 +0900485 Current ApiToCheck
486 Last_released ApiToCheck
487 Ignore_missing_latest_api *bool
Jiyong Park58c518b2018-05-12 22:29:12 +0900488 }
Sundong Ahn1b92c822018-05-29 11:35:17 +0900489 Aidl struct {
490 Include_dirs []string
491 Local_include_dirs []string
492 }
Anton Hansson5fd5d242020-03-27 19:43:19 +0000493 Dist struct {
494 Targets []string
495 Dest *string
496 Dir *string
497 }
Jiyong Parkc678ad32018-04-10 13:07:10 +0900498 }{}
499
Paul Duffin250e6192019-06-07 10:44:37 +0100500 sdkDep := decodeSdkDep(mctx, sdkContext(&module.Library))
Paul Duffin12ceb462019-12-24 20:31:31 +0000501 // Use the platform API if standard libraries were requested, otherwise use
502 // no default libraries.
Paul Duffin52d398a2019-06-11 12:31:14 +0100503 sdkVersion := ""
504 if !sdkDep.hasStandardLibs() {
505 sdkVersion = "none"
506 }
Paul Duffin250e6192019-06-07 10:44:37 +0100507
Jiyong Parkdf130542018-04-27 16:29:21 +0900508 props.Name = proptools.StringPtr(module.docsName(apiScope))
Sundong Ahn054b19a2018-10-19 13:46:09 +0900509 props.Srcs = append(props.Srcs, module.Library.Module.properties.Srcs...)
Paul Duffin52d398a2019-06-11 12:31:14 +0100510 props.Sdk_version = proptools.StringPtr(sdkVersion)
Paul Duffin12ceb462019-12-24 20:31:31 +0000511 props.System_modules = module.Library.Module.deviceProperties.System_modules
Jiyong Parkc678ad32018-04-10 13:07:10 +0900512 props.Installable = proptools.BoolPtr(false)
Sundong Ahne6f0b052018-06-05 16:46:14 +0900513 // A droiddoc module has only one Libs property and doesn't distinguish between
514 // shared libs and static libs. So we need to add both of these libs to Libs property.
Sundong Ahn054b19a2018-10-19 13:46:09 +0900515 props.Libs = module.Library.Module.properties.Libs
516 props.Libs = append(props.Libs, module.Library.Module.properties.Static_libs...)
517 props.Aidl.Include_dirs = module.Library.Module.deviceProperties.Aidl.Include_dirs
518 props.Aidl.Local_include_dirs = module.Library.Module.deviceProperties.Aidl.Local_include_dirs
Sundong Ahn054b19a2018-10-19 13:46:09 +0900519 props.Java_version = module.Library.Module.properties.Java_version
Jiyong Parkc678ad32018-04-10 13:07:10 +0900520
Sundong Ahn054b19a2018-10-19 13:46:09 +0900521 props.Merge_annotations_dirs = module.sdkLibraryProperties.Merge_annotations_dirs
522 props.Merge_inclusion_annotations_dirs = module.sdkLibraryProperties.Merge_inclusion_annotations_dirs
523
Paul Duffin6d0886e2020-04-07 18:49:53 +0100524 droidstubsArgs := []string{}
Paul Duffin235ffff2019-12-24 10:41:30 +0000525 if len(module.sdkLibraryProperties.Api_packages) != 0 {
Paul Duffin6d0886e2020-04-07 18:49:53 +0100526 droidstubsArgs = append(droidstubsArgs, "--stub-packages "+strings.Join(module.sdkLibraryProperties.Api_packages, ":"))
Paul Duffin235ffff2019-12-24 10:41:30 +0000527 }
528 if len(module.sdkLibraryProperties.Hidden_api_packages) != 0 {
Paul Duffin6d0886e2020-04-07 18:49:53 +0100529 droidstubsArgs = append(droidstubsArgs,
Paul Duffin235ffff2019-12-24 10:41:30 +0000530 android.JoinWithPrefix(module.sdkLibraryProperties.Hidden_api_packages, " --hide-package "))
531 }
Paul Duffin6d0886e2020-04-07 18:49:53 +0100532 droidstubsArgs = append(droidstubsArgs, module.sdkLibraryProperties.Droiddoc_options...)
Paul Duffin235ffff2019-12-24 10:41:30 +0000533 disabledWarnings := []string{
534 "MissingPermission",
535 "BroadcastBehavior",
536 "HiddenSuperclass",
537 "DeprecationMismatch",
538 "UnavailableSymbol",
539 "SdkConstant",
540 "HiddenTypeParameter",
541 "Todo",
542 "Typo",
543 }
Paul Duffin6d0886e2020-04-07 18:49:53 +0100544 droidstubsArgs = append(droidstubsArgs, android.JoinWithPrefix(disabledWarnings, "--hide "))
Sundong Ahnfb2721f2018-09-17 13:23:09 +0900545
Paul Duffin1fb487d2020-04-07 18:50:10 +0100546 // Add in scope specific arguments.
547 droidstubsArgs = append(droidstubsArgs, apiScope.droidstubsArgs...)
Paul Duffin11512472019-02-11 15:55:17 +0000548 props.Arg_files = module.sdkLibraryProperties.Droiddoc_option_files
Paul Duffin6d0886e2020-04-07 18:49:53 +0100549 props.Args = proptools.StringPtr(strings.Join(droidstubsArgs, " "))
Jiyong Parkc678ad32018-04-10 13:07:10 +0900550
551 // List of APIs identified from the provided source files are created. They are later
552 // compared against to the not-yet-released (a.k.a current) list of APIs and to the
553 // last-released (a.k.a numbered) list of API.
Paul Duffind1b3a922020-01-22 11:57:20 +0000554 currentApiFileName := apiScope.apiFilePrefix + "current.txt"
555 removedApiFileName := apiScope.apiFilePrefix + "removed.txt"
Paul Duffin749f98f2019-12-30 17:23:46 +0000556 apiDir := module.getApiDir()
557 currentApiFileName = path.Join(apiDir, currentApiFileName)
558 removedApiFileName = path.Join(apiDir, removedApiFileName)
Jiyong Park58c518b2018-05-12 22:29:12 +0900559 // TODO(jiyong): remove these three props
Jiyong Parkdf130542018-04-27 16:29:21 +0900560 props.Api_tag_name = proptools.StringPtr(module.apiTagName(apiScope))
Jiyong Parkc678ad32018-04-10 13:07:10 +0900561 props.Api_filename = proptools.StringPtr(currentApiFileName)
562 props.Removed_api_filename = proptools.StringPtr(removedApiFileName)
563
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
568 // check against the latest released API
569 props.Check_api.Last_released.Api_file = proptools.StringPtr(
570 module.latestApiFilegroupName(apiScope))
571 props.Check_api.Last_released.Removed_api_file = proptools.StringPtr(
572 module.latestRemovedApiFilegroupName(apiScope))
Inseob Kim38449af2019-02-28 14:24:05 +0900573 props.Check_api.Ignore_missing_latest_api = proptools.BoolPtr(true)
Jiyong Park58c518b2018-05-12 22:29:12 +0900574
Anton Hansson5fd5d242020-03-27 19:43:19 +0000575 // Dist the api txt artifact for sdk builds.
576 if !Bool(module.sdkLibraryProperties.No_dist) {
577 props.Dist.Targets = []string{"sdk", "win_sdk"}
578 props.Dist.Dest = proptools.StringPtr(fmt.Sprintf("%v.txt", module.BaseModuleName()))
579 props.Dist.Dir = proptools.StringPtr(path.Join(module.apiDistPath(apiScope), "api"))
580 }
581
Colin Cross84dfc3d2019-09-25 11:33:01 -0700582 mctx.CreateModule(DroidstubsFactory, &props)
Jiyong Parkc678ad32018-04-10 13:07:10 +0900583}
584
Jooyung Han5e9013b2020-03-10 06:23:13 +0900585func (module *SdkLibrary) DepIsInSameApex(mctx android.BaseModuleContext, dep android.Module) bool {
586 depTag := mctx.OtherModuleDependencyTag(dep)
587 if depTag == xmlPermissionsFileTag {
588 return true
589 }
590 return module.Library.DepIsInSameApex(mctx, dep)
591}
592
Jiyong Parkc678ad32018-04-10 13:07:10 +0900593// Creates the xml file that publicizes the runtime library
Colin Crossf8b860a2019-04-16 14:43:28 -0700594func (module *SdkLibrary) createXmlFile(mctx android.LoadHookContext) {
Jiyong Parke3833882020-02-17 17:28:10 +0900595 props := struct {
Sundong Ahn0d7dff42019-12-04 12:53:44 +0900596 Name *string
Jiyong Parke3833882020-02-17 17:28:10 +0900597 Lib_name *string
Sundong Ahn0d7dff42019-12-04 12:53:44 +0900598 Soc_specific *bool
599 Device_specific *bool
600 Product_specific *bool
601 System_ext_specific *bool
Jooyung Han5e9013b2020-03-10 06:23:13 +0900602 Apex_available []string
Jiyong Parke3833882020-02-17 17:28:10 +0900603 }{
Jooyung Han5e9013b2020-03-10 06:23:13 +0900604 Name: proptools.StringPtr(module.xmlFileName()),
605 Lib_name: proptools.StringPtr(module.BaseModuleName()),
606 Apex_available: module.ApexProperties.Apex_available,
Jiyong Parkc678ad32018-04-10 13:07:10 +0900607 }
Jiyong Parke3833882020-02-17 17:28:10 +0900608
609 if module.SocSpecific() {
610 props.Soc_specific = proptools.BoolPtr(true)
611 } else if module.DeviceSpecific() {
612 props.Device_specific = proptools.BoolPtr(true)
613 } else if module.ProductSpecific() {
614 props.Product_specific = proptools.BoolPtr(true)
615 } else if module.SystemExtSpecific() {
616 props.System_ext_specific = proptools.BoolPtr(true)
617 }
618
619 mctx.CreateModule(sdkLibraryXmlFactory, &props)
Jiyong Parkc678ad32018-04-10 13:07:10 +0900620}
621
Paul Duffin50061512020-01-21 16:31:05 +0000622func PrebuiltJars(ctx android.BaseModuleContext, baseName string, s sdkSpec) android.Paths {
Jiyong Park6a927c42020-01-21 02:03:43 +0900623 var ver sdkVersion
624 var kind sdkKind
625 if s.usePrebuilt(ctx) {
626 ver = s.version
627 kind = s.kind
Jiyong Parkc678ad32018-04-10 13:07:10 +0900628 } else {
Jiyong Park6a927c42020-01-21 02:03:43 +0900629 // We don't have prebuilt SDK for the specific sdkVersion.
630 // Instead of breaking the build, fallback to use "system_current"
631 ver = sdkVersionCurrent
632 kind = sdkSystem
Sundong Ahn054b19a2018-10-19 13:46:09 +0900633 }
Jiyong Park6a927c42020-01-21 02:03:43 +0900634
635 dir := filepath.Join("prebuilts", "sdk", ver.String(), kind.String())
Paul Duffin50061512020-01-21 16:31:05 +0000636 jar := filepath.Join(dir, baseName+".jar")
Sundong Ahn054b19a2018-10-19 13:46:09 +0900637 jarPath := android.ExistentPathForSource(ctx, jar)
Sundong Ahnae418ac2019-02-28 15:01:28 +0900638 if !jarPath.Valid() {
Colin Cross07c88562020-01-07 09:34:44 -0800639 if ctx.Config().AllowMissingDependencies() {
640 return android.Paths{android.PathForSource(ctx, jar)}
641 } else {
Jiyong Park6a927c42020-01-21 02:03:43 +0900642 ctx.PropertyErrorf("sdk_library", "invalid sdk version %q, %q does not exist", s.raw, jar)
Colin Cross07c88562020-01-07 09:34:44 -0800643 }
Sundong Ahnae418ac2019-02-28 15:01:28 +0900644 return nil
645 }
Sundong Ahn054b19a2018-10-19 13:46:09 +0900646 return android.Paths{jarPath.Path()}
647}
648
Paul Duffind1b3a922020-01-22 11:57:20 +0000649func (module *SdkLibrary) sdkJars(
650 ctx android.BaseModuleContext,
651 sdkVersion sdkSpec,
652 headerJars bool) android.Paths {
653
Paul Duffin50061512020-01-21 16:31:05 +0000654 // If a specific numeric version has been requested then use prebuilt versions of the sdk.
655 if sdkVersion.version.isNumbered() {
656 return PrebuiltJars(ctx, module.BaseModuleName(), sdkVersion)
Sundong Ahn054b19a2018-10-19 13:46:09 +0900657 } else {
Paul Duffind1b3a922020-01-22 11:57:20 +0000658 if !sdkVersion.specified() {
659 if headerJars {
660 return module.Library.HeaderJars()
661 } else {
662 return module.Library.ImplementationJars()
663 }
664 }
Paul Duffin726d23c2020-01-22 16:30:37 +0000665 var apiScope *apiScope
Jiyong Park6a927c42020-01-21 02:03:43 +0900666 switch sdkVersion.kind {
667 case sdkSystem:
Paul Duffin726d23c2020-01-22 16:30:37 +0000668 apiScope = apiScopeSystem
669 case sdkTest:
670 apiScope = apiScopeTest
Jiyong Park6a927c42020-01-21 02:03:43 +0900671 case sdkPrivate:
Sundong Ahn054b19a2018-10-19 13:46:09 +0900672 return module.Library.HeaderJars()
Jiyong Park6a927c42020-01-21 02:03:43 +0900673 default:
Paul Duffin726d23c2020-01-22 16:30:37 +0000674 apiScope = apiScopePublic
Paul Duffind1b3a922020-01-22 11:57:20 +0000675 }
676
Paul Duffin726d23c2020-01-22 16:30:37 +0000677 paths := module.getScopePaths(apiScope)
Paul Duffind1b3a922020-01-22 11:57:20 +0000678 if headerJars {
679 return paths.stubsHeaderPath
680 } else {
681 return paths.stubsImplPath
Sundong Ahn054b19a2018-10-19 13:46:09 +0900682 }
Jiyong Parkc678ad32018-04-10 13:07:10 +0900683 }
684}
685
Sundong Ahn241cd372018-07-13 16:16:44 +0900686// to satisfy SdkLibraryDependency interface
Paul Duffind1b3a922020-01-22 11:57:20 +0000687func (module *SdkLibrary) SdkHeaderJars(ctx android.BaseModuleContext, sdkVersion sdkSpec) android.Paths {
688 return module.sdkJars(ctx, sdkVersion, true /*headerJars*/)
689}
690
691// to satisfy SdkLibraryDependency interface
Jiyong Park6a927c42020-01-21 02:03:43 +0900692func (module *SdkLibrary) SdkImplementationJars(ctx android.BaseModuleContext, sdkVersion sdkSpec) android.Paths {
Paul Duffind1b3a922020-01-22 11:57:20 +0000693 return module.sdkJars(ctx, sdkVersion, false /*headerJars*/)
Sundong Ahn241cd372018-07-13 16:16:44 +0900694}
695
Sundong Ahn80a87b32019-05-13 15:02:50 +0900696func (module *SdkLibrary) SetNoDist() {
697 module.sdkLibraryProperties.No_dist = proptools.BoolPtr(true)
698}
699
Colin Cross571cccf2019-02-04 11:22:08 -0800700var javaSdkLibrariesKey = android.NewOnceKey("javaSdkLibraries")
701
Jiyong Park82484c02018-04-23 21:41:26 +0900702func javaSdkLibraries(config android.Config) *[]string {
Colin Cross571cccf2019-02-04 11:22:08 -0800703 return config.Once(javaSdkLibrariesKey, func() interface{} {
Jiyong Park82484c02018-04-23 21:41:26 +0900704 return &[]string{}
705 }).(*[]string)
706}
707
Paul Duffin749f98f2019-12-30 17:23:46 +0000708func (module *SdkLibrary) getApiDir() string {
709 return proptools.StringDefault(module.sdkLibraryProperties.Api_dir, "api")
710}
711
Jiyong Parkc678ad32018-04-10 13:07:10 +0900712// For a java_sdk_library module, create internal modules for stubs, docs,
713// runtime libs and xml file. If requested, the stubs and docs are created twice
714// once for public API level and once for system API level
Colin Crossf8b860a2019-04-16 14:43:28 -0700715func (module *SdkLibrary) CreateInternalModules(mctx android.LoadHookContext) {
Inseob Kim6e93ac92019-03-21 17:43:49 +0900716 if len(module.Library.Module.properties.Srcs) == 0 {
Inseob Kimc0907f12019-02-08 21:00:45 +0900717 mctx.PropertyErrorf("srcs", "java_sdk_library must specify srcs")
Jooyung Han58f26ab2019-12-18 15:34:32 +0900718 return
Inseob Kimc0907f12019-02-08 21:00:45 +0900719 }
720
Paul Duffin37e0b772019-12-30 17:20:10 +0000721 // If this builds against standard libraries (i.e. is not part of the core libraries)
722 // then assume it provides both system and test apis. Otherwise, assume it does not and
723 // also assume it does not contribute to the dist build.
724 sdkDep := decodeSdkDep(mctx, sdkContext(&module.Library))
725 hasSystemAndTestApis := sdkDep.hasStandardLibs()
726 module.sdkLibraryProperties.Has_system_and_test_apis = hasSystemAndTestApis
727 module.sdkLibraryProperties.No_dist = proptools.BoolPtr(!hasSystemAndTestApis)
728
Inseob Kim8098faa2019-03-18 10:19:51 +0900729 missing_current_api := false
730
Paul Duffind1b3a922020-01-22 11:57:20 +0000731 activeScopes := module.getActiveApiScopes()
732
Paul Duffin749f98f2019-12-30 17:23:46 +0000733 apiDir := module.getApiDir()
Paul Duffind1b3a922020-01-22 11:57:20 +0000734 for _, scope := range activeScopes {
Inseob Kim8098faa2019-03-18 10:19:51 +0900735 for _, api := range []string{"current.txt", "removed.txt"} {
Paul Duffind1b3a922020-01-22 11:57:20 +0000736 path := path.Join(mctx.ModuleDir(), apiDir, scope.apiFilePrefix+api)
Inseob Kim8098faa2019-03-18 10:19:51 +0900737 p := android.ExistentPathForSource(mctx, path)
738 if !p.Valid() {
739 mctx.ModuleErrorf("Current api file %#v doesn't exist", path)
740 missing_current_api = true
741 }
742 }
743 }
744
745 if missing_current_api {
746 script := "build/soong/scripts/gen-java-current-api-files.sh"
747 p := android.ExistentPathForSource(mctx, script)
748
749 if !p.Valid() {
750 panic(fmt.Sprintf("script file %s doesn't exist", script))
751 }
752
753 mctx.ModuleErrorf("One or more current api files are missing. "+
754 "You can update them by:\n"+
Paul Duffin37e0b772019-12-30 17:20:10 +0000755 "%s %q %s && m update-api",
Paul Duffind1b3a922020-01-22 11:57:20 +0000756 script, filepath.Join(mctx.ModuleDir(), apiDir),
757 strings.Join(activeScopes.Strings(func(s *apiScope) string { return s.apiFilePrefix }), " "))
Inseob Kim8098faa2019-03-18 10:19:51 +0900758 return
759 }
760
Paul Duffind1b3a922020-01-22 11:57:20 +0000761 for _, scope := range activeScopes {
762 module.createStubsLibrary(mctx, scope)
763 module.createStubsSources(mctx, scope)
Inseob Kimc0907f12019-02-08 21:00:45 +0900764 }
765
Paul Duffin43db9be2019-12-30 17:35:49 +0000766 if !proptools.Bool(module.sdkLibraryProperties.Api_only) {
767 // for runtime
768 module.createXmlFile(mctx)
769
770 // record java_sdk_library modules so that they are exported to make
771 javaSdkLibraries := javaSdkLibraries(mctx.Config())
772 javaSdkLibrariesLock.Lock()
773 defer javaSdkLibrariesLock.Unlock()
774 *javaSdkLibraries = append(*javaSdkLibraries, module.BaseModuleName())
775 }
Inseob Kimc0907f12019-02-08 21:00:45 +0900776}
777
778func (module *SdkLibrary) InitSdkLibraryProperties() {
Sundong Ahn054b19a2018-10-19 13:46:09 +0900779 module.AddProperties(
780 &module.sdkLibraryProperties,
781 &module.Library.Module.properties,
782 &module.Library.Module.dexpreoptProperties,
783 &module.Library.Module.deviceProperties,
784 &module.Library.Module.protoProperties,
785 )
786
787 module.Library.Module.properties.Installable = proptools.BoolPtr(true)
788 module.Library.Module.deviceProperties.IsSDKLibrary = true
Inseob Kimc0907f12019-02-08 21:00:45 +0900789}
Sundong Ahn054b19a2018-10-19 13:46:09 +0900790
Jaewoong Jung4f158ee2019-07-11 10:05:35 -0700791// java_sdk_library is a special Java library that provides optional platform APIs to apps.
792// In practice, it can be viewed as a combination of several modules: 1) stubs library that clients
793// are linked against to, 2) droiddoc module that internally generates API stubs source files,
794// 3) the real runtime shared library that implements the APIs, and 4) XML file for adding
795// the runtime lib to the classpath at runtime if requested via <uses-library>.
Inseob Kimc0907f12019-02-08 21:00:45 +0900796func SdkLibraryFactory() android.Module {
797 module := &SdkLibrary{}
798 module.InitSdkLibraryProperties()
Jooyung Han58f26ab2019-12-18 15:34:32 +0900799 android.InitApexModule(module)
Sundong Ahn054b19a2018-10-19 13:46:09 +0900800 InitJavaModule(module, android.HostAndDeviceSupported)
Colin Crossf8b860a2019-04-16 14:43:28 -0700801 android.AddLoadHook(module, func(ctx android.LoadHookContext) { module.CreateInternalModules(ctx) })
Jiyong Parkc678ad32018-04-10 13:07:10 +0900802 return module
803}
Colin Cross79c7c262019-04-17 11:11:46 -0700804
805//
806// SDK library prebuilts
807//
808
Paul Duffin56d44902020-01-31 13:36:25 +0000809// Properties associated with each api scope.
810type sdkLibraryScopeProperties struct {
Colin Cross79c7c262019-04-17 11:11:46 -0700811 Jars []string `android:"path"`
812
813 Sdk_version *string
814
Colin Cross79c7c262019-04-17 11:11:46 -0700815 // List of shared java libs that this module has dependencies to
816 Libs []string
Colin Cross79c7c262019-04-17 11:11:46 -0700817}
818
Paul Duffin56d44902020-01-31 13:36:25 +0000819type sdkLibraryImportProperties struct {
Paul Duffinfcfd7912020-01-31 17:54:30 +0000820 // List of shared java libs, common to all scopes, that this module has
821 // dependencies to
822 Libs []string
823
Paul Duffin56d44902020-01-31 13:36:25 +0000824 // Properties associated with the public api scope.
825 Public sdkLibraryScopeProperties
826
827 // Properties associated with the system api scope.
828 System sdkLibraryScopeProperties
829
830 // Properties associated with the test api scope.
831 Test sdkLibraryScopeProperties
832}
833
Colin Cross79c7c262019-04-17 11:11:46 -0700834type sdkLibraryImport struct {
835 android.ModuleBase
836 android.DefaultableModuleBase
837 prebuilt android.Prebuilt
838
839 properties sdkLibraryImportProperties
840
Paul Duffin56d44902020-01-31 13:36:25 +0000841 commonToSdkLibraryAndImport
Colin Cross79c7c262019-04-17 11:11:46 -0700842}
843
844var _ SdkLibraryDependency = (*sdkLibraryImport)(nil)
845
Jaewoong Jung4f158ee2019-07-11 10:05:35 -0700846// java_sdk_library_import imports a prebuilt java_sdk_library.
Colin Cross79c7c262019-04-17 11:11:46 -0700847func sdkLibraryImportFactory() android.Module {
848 module := &sdkLibraryImport{}
849
Paul Duffinfcfd7912020-01-31 17:54:30 +0000850 module.AddProperties(&module.properties)
Colin Cross79c7c262019-04-17 11:11:46 -0700851
Paul Duffin0bdcb272020-02-06 15:24:57 +0000852 android.InitPrebuiltModule(module, &[]string{""})
Colin Cross79c7c262019-04-17 11:11:46 -0700853 InitJavaModule(module, android.HostAndDeviceSupported)
854
855 android.AddLoadHook(module, func(mctx android.LoadHookContext) { module.createInternalModules(mctx) })
856 return module
857}
858
859func (module *sdkLibraryImport) Prebuilt() *android.Prebuilt {
860 return &module.prebuilt
861}
862
863func (module *sdkLibraryImport) Name() string {
864 return module.prebuilt.Name(module.ModuleBase.Name())
865}
866
867func (module *sdkLibraryImport) createInternalModules(mctx android.LoadHookContext) {
Colin Cross79c7c262019-04-17 11:11:46 -0700868
Paul Duffin50061512020-01-21 16:31:05 +0000869 // If the build is configured to use prebuilts then force this to be preferred.
870 if mctx.Config().UnbundledBuildUsePrebuiltSdks() {
871 module.prebuilt.ForcePrefer()
872 }
873
Paul Duffin56d44902020-01-31 13:36:25 +0000874 for apiScope, scopeProperties := range module.scopeProperties() {
875 if len(scopeProperties.Jars) == 0 {
876 continue
877 }
878
879 // Creates a java import for the jar with ".stubs" suffix
880 props := struct {
881 Name *string
882 Soc_specific *bool
883 Device_specific *bool
884 Product_specific *bool
885 System_ext_specific *bool
886 Sdk_version *string
887 Libs []string
888 Jars []string
Paul Duffin50061512020-01-21 16:31:05 +0000889 Prefer *bool
Paul Duffin56d44902020-01-31 13:36:25 +0000890 }{}
891
892 props.Name = proptools.StringPtr(apiScope.stubsModuleName(module.BaseModuleName()))
893 props.Sdk_version = scopeProperties.Sdk_version
Paul Duffinfcfd7912020-01-31 17:54:30 +0000894 // Prepend any of the libs from the legacy public properties to the libs for each of the
895 // scopes to avoid having to duplicate them in each scope.
896 props.Libs = append(module.properties.Libs, scopeProperties.Libs...)
Paul Duffin56d44902020-01-31 13:36:25 +0000897 props.Jars = scopeProperties.Jars
898
899 if module.SocSpecific() {
900 props.Soc_specific = proptools.BoolPtr(true)
901 } else if module.DeviceSpecific() {
902 props.Device_specific = proptools.BoolPtr(true)
903 } else if module.ProductSpecific() {
904 props.Product_specific = proptools.BoolPtr(true)
905 } else if module.SystemExtSpecific() {
906 props.System_ext_specific = proptools.BoolPtr(true)
907 }
908
Paul Duffin50061512020-01-21 16:31:05 +0000909 // If the build should use prebuilt sdks then set prefer to true on the stubs library.
910 // That will cause the prebuilt version of the stubs to override the source version.
911 if mctx.Config().UnbundledBuildUsePrebuiltSdks() {
912 props.Prefer = proptools.BoolPtr(true)
913 }
914
Paul Duffin56d44902020-01-31 13:36:25 +0000915 mctx.CreateModule(ImportFactory, &props)
916 }
Colin Cross79c7c262019-04-17 11:11:46 -0700917
918 javaSdkLibraries := javaSdkLibraries(mctx.Config())
919 javaSdkLibrariesLock.Lock()
920 defer javaSdkLibrariesLock.Unlock()
921 *javaSdkLibraries = append(*javaSdkLibraries, module.BaseModuleName())
922}
923
Paul Duffin56d44902020-01-31 13:36:25 +0000924func (module *sdkLibraryImport) scopeProperties() map[*apiScope]*sdkLibraryScopeProperties {
925 p := make(map[*apiScope]*sdkLibraryScopeProperties)
926 p[apiScopePublic] = &module.properties.Public
927 p[apiScopeSystem] = &module.properties.System
928 p[apiScopeTest] = &module.properties.Test
929 return p
930}
931
Colin Cross79c7c262019-04-17 11:11:46 -0700932func (module *sdkLibraryImport) DepsMutator(ctx android.BottomUpMutatorContext) {
Paul Duffin56d44902020-01-31 13:36:25 +0000933 for apiScope, scopeProperties := range module.scopeProperties() {
934 if len(scopeProperties.Jars) == 0 {
935 continue
936 }
937
938 // Add dependencies to the prebuilt stubs library
939 ctx.AddVariationDependencies(nil, apiScope.stubsTag, apiScope.stubsModuleName(module.BaseModuleName()))
940 }
Colin Cross79c7c262019-04-17 11:11:46 -0700941}
942
943func (module *sdkLibraryImport) GenerateAndroidBuildActions(ctx android.ModuleContext) {
944 // Record the paths to the prebuilt stubs library.
945 ctx.VisitDirectDeps(func(to android.Module) {
946 tag := ctx.OtherModuleDependencyTag(to)
947
Paul Duffin56d44902020-01-31 13:36:25 +0000948 if lib, ok := to.(Dependency); ok {
949 if scopeTag, ok := tag.(scopeDependencyTag); ok {
950 apiScope := scopeTag.apiScope
951 scopePaths := module.getScopePaths(apiScope)
952 scopePaths.stubsHeaderPath = lib.HeaderJars()
953 }
Colin Cross79c7c262019-04-17 11:11:46 -0700954 }
955 })
956}
957
Paul Duffin56d44902020-01-31 13:36:25 +0000958func (module *sdkLibraryImport) sdkJars(
959 ctx android.BaseModuleContext,
960 sdkVersion sdkSpec) android.Paths {
961
Paul Duffin50061512020-01-21 16:31:05 +0000962 // If a specific numeric version has been requested then use prebuilt versions of the sdk.
963 if sdkVersion.version.isNumbered() {
964 return PrebuiltJars(ctx, module.BaseModuleName(), sdkVersion)
965 }
966
Paul Duffin56d44902020-01-31 13:36:25 +0000967 var apiScope *apiScope
968 switch sdkVersion.kind {
969 case sdkSystem:
970 apiScope = apiScopeSystem
971 case sdkTest:
972 apiScope = apiScopeTest
973 default:
974 apiScope = apiScopePublic
975 }
976
977 paths := module.getScopePaths(apiScope)
978 return paths.stubsHeaderPath
979}
980
Colin Cross79c7c262019-04-17 11:11:46 -0700981// to satisfy SdkLibraryDependency interface
Jiyong Park6a927c42020-01-21 02:03:43 +0900982func (module *sdkLibraryImport) SdkHeaderJars(ctx android.BaseModuleContext, sdkVersion sdkSpec) android.Paths {
Colin Cross79c7c262019-04-17 11:11:46 -0700983 // This module is just a wrapper for the prebuilt stubs.
Paul Duffin56d44902020-01-31 13:36:25 +0000984 return module.sdkJars(ctx, sdkVersion)
Colin Cross79c7c262019-04-17 11:11:46 -0700985}
986
987// to satisfy SdkLibraryDependency interface
Jiyong Park6a927c42020-01-21 02:03:43 +0900988func (module *sdkLibraryImport) SdkImplementationJars(ctx android.BaseModuleContext, sdkVersion sdkSpec) android.Paths {
Colin Cross79c7c262019-04-17 11:11:46 -0700989 // This module is just a wrapper for the stubs.
Paul Duffin56d44902020-01-31 13:36:25 +0000990 return module.sdkJars(ctx, sdkVersion)
Colin Cross79c7c262019-04-17 11:11:46 -0700991}
Jiyong Parke3833882020-02-17 17:28:10 +0900992
993//
994// java_sdk_library_xml
995//
996type sdkLibraryXml struct {
997 android.ModuleBase
998 android.DefaultableModuleBase
999 android.ApexModuleBase
1000
1001 properties sdkLibraryXmlProperties
1002
1003 outputFilePath android.OutputPath
1004 installDirPath android.InstallPath
1005}
1006
1007type sdkLibraryXmlProperties struct {
1008 // canonical name of the lib
1009 Lib_name *string
1010}
1011
1012// java_sdk_library_xml builds the permission xml file for a java_sdk_library.
1013// Not to be used directly by users. java_sdk_library internally uses this.
1014func sdkLibraryXmlFactory() android.Module {
1015 module := &sdkLibraryXml{}
1016
1017 module.AddProperties(&module.properties)
1018
1019 android.InitApexModule(module)
1020 android.InitAndroidArchModule(module, android.DeviceSupported, android.MultilibCommon)
1021
1022 return module
1023}
1024
1025// from android.PrebuiltEtcModule
1026func (module *sdkLibraryXml) SubDir() string {
1027 return "permissions"
1028}
1029
1030// from android.PrebuiltEtcModule
1031func (module *sdkLibraryXml) OutputFile() android.OutputPath {
1032 return module.outputFilePath
1033}
1034
1035// from android.ApexModule
1036func (module *sdkLibraryXml) AvailableFor(what string) bool {
1037 return true
1038}
1039
1040func (module *sdkLibraryXml) DepsMutator(ctx android.BottomUpMutatorContext) {
1041 // do nothing
1042}
1043
1044// File path to the runtime implementation library
1045func (module *sdkLibraryXml) implPath() string {
1046 implName := proptools.String(module.properties.Lib_name)
1047 if apexName := module.ApexName(); apexName != "" {
1048 // TODO(b/146468504): ApexName() is only a soong module name, not apex name.
1049 // In most cases, this works fine. But when apex_name is set or override_apex is used
1050 // this can be wrong.
1051 return fmt.Sprintf("/apex/%s/javalib/%s.jar", apexName, implName)
1052 }
1053 partition := "system"
1054 if module.SocSpecific() {
1055 partition = "vendor"
1056 } else if module.DeviceSpecific() {
1057 partition = "odm"
1058 } else if module.ProductSpecific() {
1059 partition = "product"
1060 } else if module.SystemExtSpecific() {
1061 partition = "system_ext"
1062 }
1063 return "/" + partition + "/framework/" + implName + ".jar"
1064}
1065
1066func (module *sdkLibraryXml) GenerateAndroidBuildActions(ctx android.ModuleContext) {
1067 libName := proptools.String(module.properties.Lib_name)
1068 xmlContent := fmt.Sprintf(permissionsTemplate, libName, module.implPath())
1069
1070 module.outputFilePath = android.PathForModuleOut(ctx, libName+".xml").OutputPath
1071 rule := android.NewRuleBuilder()
1072 rule.Command().
1073 Text("/bin/bash -c \"echo -e '" + xmlContent + "'\" > ").
1074 Output(module.outputFilePath)
1075
1076 rule.Build(pctx, ctx, "java_sdk_xml", "Permission XML")
1077
1078 module.installDirPath = android.PathForModuleInstall(ctx, "etc", module.SubDir())
1079}
1080
1081func (module *sdkLibraryXml) AndroidMkEntries() []android.AndroidMkEntries {
1082 if !module.IsForPlatform() {
1083 return []android.AndroidMkEntries{android.AndroidMkEntries{
1084 Disabled: true,
1085 }}
1086 }
1087
1088 return []android.AndroidMkEntries{android.AndroidMkEntries{
1089 Class: "ETC",
1090 OutputFile: android.OptionalPathForPath(module.outputFilePath),
1091 ExtraEntries: []android.AndroidMkExtraEntriesFunc{
1092 func(entries *android.AndroidMkEntries) {
1093 entries.SetString("LOCAL_MODULE_TAGS", "optional")
1094 entries.SetString("LOCAL_MODULE_PATH", module.installDirPath.ToMakePath().String())
1095 entries.SetString("LOCAL_INSTALLED_MODULE_STEM", module.outputFilePath.Base())
1096 },
1097 },
1098 }}
1099}