blob: ccb2b8554ac3483118d5717168bbac99399df591 [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 Duffin3c7c3472020-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 Duffin3c7c3472020-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 Duffin3c7c3472020-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 Hansson6bb88102020-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
Paul Duffind1b3a922020-01-22 11:57:20 +0000381func (module *SdkLibrary) latestApiFilegroupName(apiScope *apiScope) string {
382 return ":" + module.BaseModuleName() + ".api." + apiScope.name + ".latest"
Jiyong Park58c518b2018-05-12 22:29:12 +0900383}
Jiyong Parkc678ad32018-04-10 13:07:10 +0900384
Paul Duffind1b3a922020-01-22 11:57:20 +0000385func (module *SdkLibrary) latestRemovedApiFilegroupName(apiScope *apiScope) string {
386 return ":" + module.BaseModuleName() + "-removed.api." + apiScope.name + ".latest"
Jiyong Parkc678ad32018-04-10 13:07:10 +0900387}
388
389// Creates a static java library that has API stubs
Paul Duffind1b3a922020-01-22 11:57:20 +0000390func (module *SdkLibrary) createStubsLibrary(mctx android.LoadHookContext, apiScope *apiScope) {
Jiyong Parkc678ad32018-04-10 13:07:10 +0900391 props := struct {
Sundong Ahn0d7dff42019-12-04 12:53:44 +0900392 Name *string
393 Srcs []string
Paul Duffin367ab912019-12-23 19:40:36 +0000394 Installable *bool
Sundong Ahn0d7dff42019-12-04 12:53:44 +0900395 Sdk_version *string
Paul Duffin12ceb462019-12-24 20:31:31 +0000396 System_modules *string
Paul Duffinab8da5d2020-02-07 16:12:04 +0000397 Patch_module *string
Sundong Ahn0d7dff42019-12-04 12:53:44 +0900398 Libs []string
399 Soc_specific *bool
400 Device_specific *bool
401 Product_specific *bool
402 System_ext_specific *bool
403 Compile_dex *bool
Sundong Ahn0d7dff42019-12-04 12:53:44 +0900404 Java_version *string
405 Product_variables struct {
Jiyong Park82484c02018-04-23 21:41:26 +0900406 Pdk struct {
407 Enabled *bool
408 }
Jiyong Parkc678ad32018-04-10 13:07:10 +0900409 }
Sundong Ahn054b19a2018-10-19 13:46:09 +0900410 Openjdk9 struct {
411 Srcs []string
412 Javacflags []string
413 }
Anton Hansson6bb88102020-03-27 19:43:19 +0000414 Dist struct {
415 Targets []string
416 Dest *string
417 Dir *string
418 Tag *string
419 }
Jiyong Parkc678ad32018-04-10 13:07:10 +0900420 }{}
421
Jiyong Parkdf130542018-04-27 16:29:21 +0900422 props.Name = proptools.StringPtr(module.stubsName(apiScope))
Jiyong Parkc678ad32018-04-10 13:07:10 +0900423 // sources are generated from the droiddoc
Jiyong Parkdf130542018-04-27 16:29:21 +0900424 props.Srcs = []string{":" + module.docsName(apiScope)}
Paul Duffin12ceb462019-12-24 20:31:31 +0000425 sdkVersion := module.sdkVersionForStubsLibrary(mctx, apiScope)
Paul Duffin52d398a2019-06-11 12:31:14 +0100426 props.Sdk_version = proptools.StringPtr(sdkVersion)
Paul Duffin12ceb462019-12-24 20:31:31 +0000427 props.System_modules = module.Library.Module.deviceProperties.System_modules
Paul Duffinab8da5d2020-02-07 16:12:04 +0000428 props.Patch_module = module.Library.Module.properties.Patch_module
Paul Duffin367ab912019-12-23 19:40:36 +0000429 props.Installable = proptools.BoolPtr(false)
Sundong Ahn054b19a2018-10-19 13:46:09 +0900430 props.Libs = module.sdkLibraryProperties.Stub_only_libs
Jiyong Park82484c02018-04-23 21:41:26 +0900431 props.Product_variables.Pdk.Enabled = proptools.BoolPtr(false)
Sundong Ahn054b19a2018-10-19 13:46:09 +0900432 props.Openjdk9.Srcs = module.Library.Module.properties.Openjdk9.Srcs
433 props.Openjdk9.Javacflags = module.Library.Module.properties.Openjdk9.Javacflags
434 props.Java_version = module.Library.Module.properties.Java_version
435 if module.Library.Module.deviceProperties.Compile_dex != nil {
436 props.Compile_dex = module.Library.Module.deviceProperties.Compile_dex
Sundong Ahndd567f92018-07-31 17:19:11 +0900437 }
Jiyong Parkc678ad32018-04-10 13:07:10 +0900438
439 if module.SocSpecific() {
440 props.Soc_specific = proptools.BoolPtr(true)
441 } else if module.DeviceSpecific() {
442 props.Device_specific = proptools.BoolPtr(true)
443 } else if module.ProductSpecific() {
444 props.Product_specific = proptools.BoolPtr(true)
Sundong Ahn0d7dff42019-12-04 12:53:44 +0900445 } else if module.SystemExtSpecific() {
446 props.System_ext_specific = proptools.BoolPtr(true)
Jiyong Parkc678ad32018-04-10 13:07:10 +0900447 }
Anton Hansson6bb88102020-03-27 19:43:19 +0000448 // Dist the class jar artifact for sdk builds.
449 if !Bool(module.sdkLibraryProperties.No_dist) {
450 props.Dist.Targets = []string{"sdk", "win_sdk"}
451 props.Dist.Dest = proptools.StringPtr(fmt.Sprintf("%v.jar", module.BaseModuleName()))
452 props.Dist.Dir = proptools.StringPtr(module.apiDistPath(apiScope))
453 props.Dist.Tag = proptools.StringPtr(".jar")
454 }
Jiyong Parkc678ad32018-04-10 13:07:10 +0900455
Colin Cross84dfc3d2019-09-25 11:33:01 -0700456 mctx.CreateModule(LibraryFactory, &props)
Jiyong Parkc678ad32018-04-10 13:07:10 +0900457}
458
Paul Duffincbcfcaa2020-04-07 18:49:53 +0100459// Creates a droidstubs module that creates stubs source files from the given full source
Jiyong Parkc678ad32018-04-10 13:07:10 +0900460// files
Paul Duffind1b3a922020-01-22 11:57:20 +0000461func (module *SdkLibrary) createStubsSources(mctx android.LoadHookContext, apiScope *apiScope) {
Jiyong Parkc678ad32018-04-10 13:07:10 +0900462 props := struct {
Sundong Ahn054b19a2018-10-19 13:46:09 +0900463 Name *string
464 Srcs []string
465 Installable *bool
Paul Duffin52d398a2019-06-11 12:31:14 +0100466 Sdk_version *string
Paul Duffin12ceb462019-12-24 20:31:31 +0000467 System_modules *string
Sundong Ahn054b19a2018-10-19 13:46:09 +0900468 Libs []string
Paul Duffin11512472019-02-11 15:55:17 +0000469 Arg_files []string
Sundong Ahn054b19a2018-10-19 13:46:09 +0900470 Args *string
Sundong Ahn054b19a2018-10-19 13:46:09 +0900471 Java_version *string
472 Merge_annotations_dirs []string
473 Merge_inclusion_annotations_dirs []string
474 Check_api struct {
Inseob Kim38449af2019-02-28 14:24:05 +0900475 Current ApiToCheck
476 Last_released ApiToCheck
477 Ignore_missing_latest_api *bool
Jiyong Park58c518b2018-05-12 22:29:12 +0900478 }
Sundong Ahn1b92c822018-05-29 11:35:17 +0900479 Aidl struct {
480 Include_dirs []string
481 Local_include_dirs []string
482 }
Anton Hansson6bb88102020-03-27 19:43:19 +0000483 Dist struct {
484 Targets []string
485 Dest *string
486 Dir *string
487 }
Jiyong Parkc678ad32018-04-10 13:07:10 +0900488 }{}
489
Paul Duffin250e6192019-06-07 10:44:37 +0100490 sdkDep := decodeSdkDep(mctx, sdkContext(&module.Library))
Paul Duffin12ceb462019-12-24 20:31:31 +0000491 // Use the platform API if standard libraries were requested, otherwise use
492 // no default libraries.
Paul Duffin52d398a2019-06-11 12:31:14 +0100493 sdkVersion := ""
494 if !sdkDep.hasStandardLibs() {
495 sdkVersion = "none"
496 }
Paul Duffin250e6192019-06-07 10:44:37 +0100497
Jiyong Parkdf130542018-04-27 16:29:21 +0900498 props.Name = proptools.StringPtr(module.docsName(apiScope))
Sundong Ahn054b19a2018-10-19 13:46:09 +0900499 props.Srcs = append(props.Srcs, module.Library.Module.properties.Srcs...)
Paul Duffin52d398a2019-06-11 12:31:14 +0100500 props.Sdk_version = proptools.StringPtr(sdkVersion)
Paul Duffin12ceb462019-12-24 20:31:31 +0000501 props.System_modules = module.Library.Module.deviceProperties.System_modules
Jiyong Parkc678ad32018-04-10 13:07:10 +0900502 props.Installable = proptools.BoolPtr(false)
Sundong Ahne6f0b052018-06-05 16:46:14 +0900503 // A droiddoc module has only one Libs property and doesn't distinguish between
504 // shared libs and static libs. So we need to add both of these libs to Libs property.
Sundong Ahn054b19a2018-10-19 13:46:09 +0900505 props.Libs = module.Library.Module.properties.Libs
506 props.Libs = append(props.Libs, module.Library.Module.properties.Static_libs...)
507 props.Aidl.Include_dirs = module.Library.Module.deviceProperties.Aidl.Include_dirs
508 props.Aidl.Local_include_dirs = module.Library.Module.deviceProperties.Aidl.Local_include_dirs
Sundong Ahn054b19a2018-10-19 13:46:09 +0900509 props.Java_version = module.Library.Module.properties.Java_version
Jiyong Parkc678ad32018-04-10 13:07:10 +0900510
Sundong Ahn054b19a2018-10-19 13:46:09 +0900511 props.Merge_annotations_dirs = module.sdkLibraryProperties.Merge_annotations_dirs
512 props.Merge_inclusion_annotations_dirs = module.sdkLibraryProperties.Merge_inclusion_annotations_dirs
513
Paul Duffincbcfcaa2020-04-07 18:49:53 +0100514 droidstubsArgs := []string{}
Paul Duffin235ffff2019-12-24 10:41:30 +0000515 if len(module.sdkLibraryProperties.Api_packages) != 0 {
Paul Duffincbcfcaa2020-04-07 18:49:53 +0100516 droidstubsArgs = append(droidstubsArgs, "--stub-packages "+strings.Join(module.sdkLibraryProperties.Api_packages, ":"))
Paul Duffin235ffff2019-12-24 10:41:30 +0000517 }
518 if len(module.sdkLibraryProperties.Hidden_api_packages) != 0 {
Paul Duffincbcfcaa2020-04-07 18:49:53 +0100519 droidstubsArgs = append(droidstubsArgs,
Paul Duffin235ffff2019-12-24 10:41:30 +0000520 android.JoinWithPrefix(module.sdkLibraryProperties.Hidden_api_packages, " --hide-package "))
521 }
Paul Duffincbcfcaa2020-04-07 18:49:53 +0100522 droidstubsArgs = append(droidstubsArgs, module.sdkLibraryProperties.Droiddoc_options...)
Paul Duffin235ffff2019-12-24 10:41:30 +0000523 disabledWarnings := []string{
524 "MissingPermission",
525 "BroadcastBehavior",
526 "HiddenSuperclass",
527 "DeprecationMismatch",
528 "UnavailableSymbol",
529 "SdkConstant",
530 "HiddenTypeParameter",
531 "Todo",
532 "Typo",
533 }
Paul Duffincbcfcaa2020-04-07 18:49:53 +0100534 droidstubsArgs = append(droidstubsArgs, android.JoinWithPrefix(disabledWarnings, "--hide "))
Sundong Ahnfb2721f2018-09-17 13:23:09 +0900535
Paul Duffin3c7c3472020-04-07 18:50:10 +0100536 // Add in scope specific arguments.
537 droidstubsArgs = append(droidstubsArgs, apiScope.droidstubsArgs...)
Paul Duffin11512472019-02-11 15:55:17 +0000538 props.Arg_files = module.sdkLibraryProperties.Droiddoc_option_files
Paul Duffincbcfcaa2020-04-07 18:49:53 +0100539 props.Args = proptools.StringPtr(strings.Join(droidstubsArgs, " "))
Jiyong Parkc678ad32018-04-10 13:07:10 +0900540
541 // List of APIs identified from the provided source files are created. They are later
542 // compared against to the not-yet-released (a.k.a current) list of APIs and to the
543 // last-released (a.k.a numbered) list of API.
Paul Duffind1b3a922020-01-22 11:57:20 +0000544 currentApiFileName := apiScope.apiFilePrefix + "current.txt"
545 removedApiFileName := apiScope.apiFilePrefix + "removed.txt"
Paul Duffin749f98f2019-12-30 17:23:46 +0000546 apiDir := module.getApiDir()
547 currentApiFileName = path.Join(apiDir, currentApiFileName)
548 removedApiFileName = path.Join(apiDir, removedApiFileName)
Jiyong Parkc678ad32018-04-10 13:07:10 +0900549
Jiyong Park58c518b2018-05-12 22:29:12 +0900550 // check against the not-yet-release API
551 props.Check_api.Current.Api_file = proptools.StringPtr(currentApiFileName)
552 props.Check_api.Current.Removed_api_file = proptools.StringPtr(removedApiFileName)
Jiyong Park58c518b2018-05-12 22:29:12 +0900553
554 // check against the latest released API
555 props.Check_api.Last_released.Api_file = proptools.StringPtr(
556 module.latestApiFilegroupName(apiScope))
557 props.Check_api.Last_released.Removed_api_file = proptools.StringPtr(
558 module.latestRemovedApiFilegroupName(apiScope))
Inseob Kim38449af2019-02-28 14:24:05 +0900559 props.Check_api.Ignore_missing_latest_api = proptools.BoolPtr(true)
Jiyong Park58c518b2018-05-12 22:29:12 +0900560
Anton Hansson6bb88102020-03-27 19:43:19 +0000561 // Dist the api txt artifact for sdk builds.
562 if !Bool(module.sdkLibraryProperties.No_dist) {
563 props.Dist.Targets = []string{"sdk", "win_sdk"}
564 props.Dist.Dest = proptools.StringPtr(fmt.Sprintf("%v.txt", module.BaseModuleName()))
565 props.Dist.Dir = proptools.StringPtr(path.Join(module.apiDistPath(apiScope), "api"))
566 }
567
Colin Cross84dfc3d2019-09-25 11:33:01 -0700568 mctx.CreateModule(DroidstubsFactory, &props)
Jiyong Parkc678ad32018-04-10 13:07:10 +0900569}
570
Jooyung Hanb8fa86a2020-03-10 06:23:13 +0900571func (module *SdkLibrary) DepIsInSameApex(mctx android.BaseModuleContext, dep android.Module) bool {
572 depTag := mctx.OtherModuleDependencyTag(dep)
573 if depTag == xmlPermissionsFileTag {
574 return true
575 }
576 return module.Library.DepIsInSameApex(mctx, dep)
577}
578
Jiyong Parkc678ad32018-04-10 13:07:10 +0900579// Creates the xml file that publicizes the runtime library
Colin Crossf8b860a2019-04-16 14:43:28 -0700580func (module *SdkLibrary) createXmlFile(mctx android.LoadHookContext) {
Jiyong Parke3833882020-02-17 17:28:10 +0900581 props := struct {
Sundong Ahn0d7dff42019-12-04 12:53:44 +0900582 Name *string
Jiyong Parke3833882020-02-17 17:28:10 +0900583 Lib_name *string
Sundong Ahn0d7dff42019-12-04 12:53:44 +0900584 Soc_specific *bool
585 Device_specific *bool
586 Product_specific *bool
587 System_ext_specific *bool
Jooyung Hanb8fa86a2020-03-10 06:23:13 +0900588 Apex_available []string
Jiyong Parke3833882020-02-17 17:28:10 +0900589 }{
Jooyung Hanb8fa86a2020-03-10 06:23:13 +0900590 Name: proptools.StringPtr(module.xmlFileName()),
591 Lib_name: proptools.StringPtr(module.BaseModuleName()),
592 Apex_available: module.ApexProperties.Apex_available,
Jiyong Parkc678ad32018-04-10 13:07:10 +0900593 }
Jiyong Parke3833882020-02-17 17:28:10 +0900594
595 if module.SocSpecific() {
596 props.Soc_specific = proptools.BoolPtr(true)
597 } else if module.DeviceSpecific() {
598 props.Device_specific = proptools.BoolPtr(true)
599 } else if module.ProductSpecific() {
600 props.Product_specific = proptools.BoolPtr(true)
601 } else if module.SystemExtSpecific() {
602 props.System_ext_specific = proptools.BoolPtr(true)
603 }
604
605 mctx.CreateModule(sdkLibraryXmlFactory, &props)
Jiyong Parkc678ad32018-04-10 13:07:10 +0900606}
607
Paul Duffin50061512020-01-21 16:31:05 +0000608func PrebuiltJars(ctx android.BaseModuleContext, baseName string, s sdkSpec) android.Paths {
Jiyong Park6a927c42020-01-21 02:03:43 +0900609 var ver sdkVersion
610 var kind sdkKind
611 if s.usePrebuilt(ctx) {
612 ver = s.version
613 kind = s.kind
Jiyong Parkc678ad32018-04-10 13:07:10 +0900614 } else {
Jiyong Park6a927c42020-01-21 02:03:43 +0900615 // We don't have prebuilt SDK for the specific sdkVersion.
616 // Instead of breaking the build, fallback to use "system_current"
617 ver = sdkVersionCurrent
618 kind = sdkSystem
Sundong Ahn054b19a2018-10-19 13:46:09 +0900619 }
Jiyong Park6a927c42020-01-21 02:03:43 +0900620
621 dir := filepath.Join("prebuilts", "sdk", ver.String(), kind.String())
Paul Duffin50061512020-01-21 16:31:05 +0000622 jar := filepath.Join(dir, baseName+".jar")
Sundong Ahn054b19a2018-10-19 13:46:09 +0900623 jarPath := android.ExistentPathForSource(ctx, jar)
Sundong Ahnae418ac2019-02-28 15:01:28 +0900624 if !jarPath.Valid() {
Colin Cross07c88562020-01-07 09:34:44 -0800625 if ctx.Config().AllowMissingDependencies() {
626 return android.Paths{android.PathForSource(ctx, jar)}
627 } else {
Jiyong Park6a927c42020-01-21 02:03:43 +0900628 ctx.PropertyErrorf("sdk_library", "invalid sdk version %q, %q does not exist", s.raw, jar)
Colin Cross07c88562020-01-07 09:34:44 -0800629 }
Sundong Ahnae418ac2019-02-28 15:01:28 +0900630 return nil
631 }
Sundong Ahn054b19a2018-10-19 13:46:09 +0900632 return android.Paths{jarPath.Path()}
633}
634
Paul Duffind1b3a922020-01-22 11:57:20 +0000635func (module *SdkLibrary) sdkJars(
636 ctx android.BaseModuleContext,
637 sdkVersion sdkSpec,
638 headerJars bool) android.Paths {
639
Paul Duffin50061512020-01-21 16:31:05 +0000640 // If a specific numeric version has been requested then use prebuilt versions of the sdk.
641 if sdkVersion.version.isNumbered() {
642 return PrebuiltJars(ctx, module.BaseModuleName(), sdkVersion)
Sundong Ahn054b19a2018-10-19 13:46:09 +0900643 } else {
Paul Duffind1b3a922020-01-22 11:57:20 +0000644 if !sdkVersion.specified() {
645 if headerJars {
646 return module.Library.HeaderJars()
647 } else {
648 return module.Library.ImplementationJars()
649 }
650 }
Paul Duffin726d23c2020-01-22 16:30:37 +0000651 var apiScope *apiScope
Jiyong Park6a927c42020-01-21 02:03:43 +0900652 switch sdkVersion.kind {
653 case sdkSystem:
Paul Duffin726d23c2020-01-22 16:30:37 +0000654 apiScope = apiScopeSystem
655 case sdkTest:
656 apiScope = apiScopeTest
Jiyong Park6a927c42020-01-21 02:03:43 +0900657 case sdkPrivate:
Sundong Ahn054b19a2018-10-19 13:46:09 +0900658 return module.Library.HeaderJars()
Jiyong Park6a927c42020-01-21 02:03:43 +0900659 default:
Paul Duffin726d23c2020-01-22 16:30:37 +0000660 apiScope = apiScopePublic
Paul Duffind1b3a922020-01-22 11:57:20 +0000661 }
662
Paul Duffin726d23c2020-01-22 16:30:37 +0000663 paths := module.getScopePaths(apiScope)
Paul Duffind1b3a922020-01-22 11:57:20 +0000664 if headerJars {
665 return paths.stubsHeaderPath
666 } else {
667 return paths.stubsImplPath
Sundong Ahn054b19a2018-10-19 13:46:09 +0900668 }
Jiyong Parkc678ad32018-04-10 13:07:10 +0900669 }
670}
671
Sundong Ahn241cd372018-07-13 16:16:44 +0900672// to satisfy SdkLibraryDependency interface
Paul Duffind1b3a922020-01-22 11:57:20 +0000673func (module *SdkLibrary) SdkHeaderJars(ctx android.BaseModuleContext, sdkVersion sdkSpec) android.Paths {
674 return module.sdkJars(ctx, sdkVersion, true /*headerJars*/)
675}
676
677// to satisfy SdkLibraryDependency interface
Jiyong Park6a927c42020-01-21 02:03:43 +0900678func (module *SdkLibrary) SdkImplementationJars(ctx android.BaseModuleContext, sdkVersion sdkSpec) android.Paths {
Paul Duffind1b3a922020-01-22 11:57:20 +0000679 return module.sdkJars(ctx, sdkVersion, false /*headerJars*/)
Sundong Ahn241cd372018-07-13 16:16:44 +0900680}
681
Sundong Ahn80a87b32019-05-13 15:02:50 +0900682func (module *SdkLibrary) SetNoDist() {
683 module.sdkLibraryProperties.No_dist = proptools.BoolPtr(true)
684}
685
Colin Cross571cccf2019-02-04 11:22:08 -0800686var javaSdkLibrariesKey = android.NewOnceKey("javaSdkLibraries")
687
Jiyong Park82484c02018-04-23 21:41:26 +0900688func javaSdkLibraries(config android.Config) *[]string {
Colin Cross571cccf2019-02-04 11:22:08 -0800689 return config.Once(javaSdkLibrariesKey, func() interface{} {
Jiyong Park82484c02018-04-23 21:41:26 +0900690 return &[]string{}
691 }).(*[]string)
692}
693
Paul Duffin749f98f2019-12-30 17:23:46 +0000694func (module *SdkLibrary) getApiDir() string {
695 return proptools.StringDefault(module.sdkLibraryProperties.Api_dir, "api")
696}
697
Jiyong Parkc678ad32018-04-10 13:07:10 +0900698// For a java_sdk_library module, create internal modules for stubs, docs,
699// runtime libs and xml file. If requested, the stubs and docs are created twice
700// once for public API level and once for system API level
Colin Crossf8b860a2019-04-16 14:43:28 -0700701func (module *SdkLibrary) CreateInternalModules(mctx android.LoadHookContext) {
Inseob Kim6e93ac92019-03-21 17:43:49 +0900702 if len(module.Library.Module.properties.Srcs) == 0 {
Inseob Kimc0907f12019-02-08 21:00:45 +0900703 mctx.PropertyErrorf("srcs", "java_sdk_library must specify srcs")
Jooyung Han58f26ab2019-12-18 15:34:32 +0900704 return
Inseob Kimc0907f12019-02-08 21:00:45 +0900705 }
706
Paul Duffin37e0b772019-12-30 17:20:10 +0000707 // If this builds against standard libraries (i.e. is not part of the core libraries)
708 // then assume it provides both system and test apis. Otherwise, assume it does not and
709 // also assume it does not contribute to the dist build.
710 sdkDep := decodeSdkDep(mctx, sdkContext(&module.Library))
711 hasSystemAndTestApis := sdkDep.hasStandardLibs()
712 module.sdkLibraryProperties.Has_system_and_test_apis = hasSystemAndTestApis
713 module.sdkLibraryProperties.No_dist = proptools.BoolPtr(!hasSystemAndTestApis)
714
Inseob Kim8098faa2019-03-18 10:19:51 +0900715 missing_current_api := false
716
Paul Duffind1b3a922020-01-22 11:57:20 +0000717 activeScopes := module.getActiveApiScopes()
718
Paul Duffin749f98f2019-12-30 17:23:46 +0000719 apiDir := module.getApiDir()
Paul Duffind1b3a922020-01-22 11:57:20 +0000720 for _, scope := range activeScopes {
Inseob Kim8098faa2019-03-18 10:19:51 +0900721 for _, api := range []string{"current.txt", "removed.txt"} {
Paul Duffind1b3a922020-01-22 11:57:20 +0000722 path := path.Join(mctx.ModuleDir(), apiDir, scope.apiFilePrefix+api)
Inseob Kim8098faa2019-03-18 10:19:51 +0900723 p := android.ExistentPathForSource(mctx, path)
724 if !p.Valid() {
725 mctx.ModuleErrorf("Current api file %#v doesn't exist", path)
726 missing_current_api = true
727 }
728 }
729 }
730
731 if missing_current_api {
732 script := "build/soong/scripts/gen-java-current-api-files.sh"
733 p := android.ExistentPathForSource(mctx, script)
734
735 if !p.Valid() {
736 panic(fmt.Sprintf("script file %s doesn't exist", script))
737 }
738
739 mctx.ModuleErrorf("One or more current api files are missing. "+
740 "You can update them by:\n"+
Paul Duffin37e0b772019-12-30 17:20:10 +0000741 "%s %q %s && m update-api",
Paul Duffind1b3a922020-01-22 11:57:20 +0000742 script, filepath.Join(mctx.ModuleDir(), apiDir),
743 strings.Join(activeScopes.Strings(func(s *apiScope) string { return s.apiFilePrefix }), " "))
Inseob Kim8098faa2019-03-18 10:19:51 +0900744 return
745 }
746
Paul Duffind1b3a922020-01-22 11:57:20 +0000747 for _, scope := range activeScopes {
748 module.createStubsLibrary(mctx, scope)
749 module.createStubsSources(mctx, scope)
Inseob Kimc0907f12019-02-08 21:00:45 +0900750 }
751
Paul Duffin43db9be2019-12-30 17:35:49 +0000752 if !proptools.Bool(module.sdkLibraryProperties.Api_only) {
753 // for runtime
754 module.createXmlFile(mctx)
755
756 // record java_sdk_library modules so that they are exported to make
757 javaSdkLibraries := javaSdkLibraries(mctx.Config())
758 javaSdkLibrariesLock.Lock()
759 defer javaSdkLibrariesLock.Unlock()
760 *javaSdkLibraries = append(*javaSdkLibraries, module.BaseModuleName())
761 }
Inseob Kimc0907f12019-02-08 21:00:45 +0900762}
763
764func (module *SdkLibrary) InitSdkLibraryProperties() {
Sundong Ahn054b19a2018-10-19 13:46:09 +0900765 module.AddProperties(
766 &module.sdkLibraryProperties,
767 &module.Library.Module.properties,
768 &module.Library.Module.dexpreoptProperties,
769 &module.Library.Module.deviceProperties,
770 &module.Library.Module.protoProperties,
771 )
772
773 module.Library.Module.properties.Installable = proptools.BoolPtr(true)
774 module.Library.Module.deviceProperties.IsSDKLibrary = true
Inseob Kimc0907f12019-02-08 21:00:45 +0900775}
Sundong Ahn054b19a2018-10-19 13:46:09 +0900776
Jaewoong Jung4f158ee2019-07-11 10:05:35 -0700777// java_sdk_library is a special Java library that provides optional platform APIs to apps.
778// In practice, it can be viewed as a combination of several modules: 1) stubs library that clients
779// are linked against to, 2) droiddoc module that internally generates API stubs source files,
780// 3) the real runtime shared library that implements the APIs, and 4) XML file for adding
781// the runtime lib to the classpath at runtime if requested via <uses-library>.
Inseob Kimc0907f12019-02-08 21:00:45 +0900782func SdkLibraryFactory() android.Module {
783 module := &SdkLibrary{}
784 module.InitSdkLibraryProperties()
Jooyung Han58f26ab2019-12-18 15:34:32 +0900785 android.InitApexModule(module)
Sundong Ahn054b19a2018-10-19 13:46:09 +0900786 InitJavaModule(module, android.HostAndDeviceSupported)
Colin Crossf8b860a2019-04-16 14:43:28 -0700787 android.AddLoadHook(module, func(ctx android.LoadHookContext) { module.CreateInternalModules(ctx) })
Jiyong Parkc678ad32018-04-10 13:07:10 +0900788 return module
789}
Colin Cross79c7c262019-04-17 11:11:46 -0700790
791//
792// SDK library prebuilts
793//
794
Paul Duffin56d44902020-01-31 13:36:25 +0000795// Properties associated with each api scope.
796type sdkLibraryScopeProperties struct {
Colin Cross79c7c262019-04-17 11:11:46 -0700797 Jars []string `android:"path"`
798
799 Sdk_version *string
800
Colin Cross79c7c262019-04-17 11:11:46 -0700801 // List of shared java libs that this module has dependencies to
802 Libs []string
Colin Cross79c7c262019-04-17 11:11:46 -0700803}
804
Paul Duffin56d44902020-01-31 13:36:25 +0000805type sdkLibraryImportProperties struct {
Paul Duffinfcfd7912020-01-31 17:54:30 +0000806 // List of shared java libs, common to all scopes, that this module has
807 // dependencies to
808 Libs []string
809
Paul Duffin56d44902020-01-31 13:36:25 +0000810 // Properties associated with the public api scope.
811 Public sdkLibraryScopeProperties
812
813 // Properties associated with the system api scope.
814 System sdkLibraryScopeProperties
815
816 // Properties associated with the test api scope.
817 Test sdkLibraryScopeProperties
818}
819
Colin Cross79c7c262019-04-17 11:11:46 -0700820type sdkLibraryImport struct {
821 android.ModuleBase
822 android.DefaultableModuleBase
823 prebuilt android.Prebuilt
824
825 properties sdkLibraryImportProperties
826
Paul Duffin56d44902020-01-31 13:36:25 +0000827 commonToSdkLibraryAndImport
Colin Cross79c7c262019-04-17 11:11:46 -0700828}
829
830var _ SdkLibraryDependency = (*sdkLibraryImport)(nil)
831
Jaewoong Jung4f158ee2019-07-11 10:05:35 -0700832// java_sdk_library_import imports a prebuilt java_sdk_library.
Colin Cross79c7c262019-04-17 11:11:46 -0700833func sdkLibraryImportFactory() android.Module {
834 module := &sdkLibraryImport{}
835
Paul Duffinfcfd7912020-01-31 17:54:30 +0000836 module.AddProperties(&module.properties)
Colin Cross79c7c262019-04-17 11:11:46 -0700837
Paul Duffin0bdcb272020-02-06 15:24:57 +0000838 android.InitPrebuiltModule(module, &[]string{""})
Colin Cross79c7c262019-04-17 11:11:46 -0700839 InitJavaModule(module, android.HostAndDeviceSupported)
840
841 android.AddLoadHook(module, func(mctx android.LoadHookContext) { module.createInternalModules(mctx) })
842 return module
843}
844
845func (module *sdkLibraryImport) Prebuilt() *android.Prebuilt {
846 return &module.prebuilt
847}
848
849func (module *sdkLibraryImport) Name() string {
850 return module.prebuilt.Name(module.ModuleBase.Name())
851}
852
853func (module *sdkLibraryImport) createInternalModules(mctx android.LoadHookContext) {
Colin Cross79c7c262019-04-17 11:11:46 -0700854
Paul Duffin50061512020-01-21 16:31:05 +0000855 // If the build is configured to use prebuilts then force this to be preferred.
856 if mctx.Config().UnbundledBuildUsePrebuiltSdks() {
857 module.prebuilt.ForcePrefer()
858 }
859
Paul Duffin56d44902020-01-31 13:36:25 +0000860 for apiScope, scopeProperties := range module.scopeProperties() {
861 if len(scopeProperties.Jars) == 0 {
862 continue
863 }
864
865 // Creates a java import for the jar with ".stubs" suffix
866 props := struct {
867 Name *string
868 Soc_specific *bool
869 Device_specific *bool
870 Product_specific *bool
871 System_ext_specific *bool
872 Sdk_version *string
873 Libs []string
874 Jars []string
Paul Duffin50061512020-01-21 16:31:05 +0000875 Prefer *bool
Paul Duffin56d44902020-01-31 13:36:25 +0000876 }{}
877
878 props.Name = proptools.StringPtr(apiScope.stubsModuleName(module.BaseModuleName()))
879 props.Sdk_version = scopeProperties.Sdk_version
Paul Duffinfcfd7912020-01-31 17:54:30 +0000880 // Prepend any of the libs from the legacy public properties to the libs for each of the
881 // scopes to avoid having to duplicate them in each scope.
882 props.Libs = append(module.properties.Libs, scopeProperties.Libs...)
Paul Duffin56d44902020-01-31 13:36:25 +0000883 props.Jars = scopeProperties.Jars
884
885 if module.SocSpecific() {
886 props.Soc_specific = proptools.BoolPtr(true)
887 } else if module.DeviceSpecific() {
888 props.Device_specific = proptools.BoolPtr(true)
889 } else if module.ProductSpecific() {
890 props.Product_specific = proptools.BoolPtr(true)
891 } else if module.SystemExtSpecific() {
892 props.System_ext_specific = proptools.BoolPtr(true)
893 }
894
Paul Duffin50061512020-01-21 16:31:05 +0000895 // If the build should use prebuilt sdks then set prefer to true on the stubs library.
896 // That will cause the prebuilt version of the stubs to override the source version.
897 if mctx.Config().UnbundledBuildUsePrebuiltSdks() {
898 props.Prefer = proptools.BoolPtr(true)
899 }
900
Paul Duffin56d44902020-01-31 13:36:25 +0000901 mctx.CreateModule(ImportFactory, &props)
902 }
Colin Cross79c7c262019-04-17 11:11:46 -0700903
904 javaSdkLibraries := javaSdkLibraries(mctx.Config())
905 javaSdkLibrariesLock.Lock()
906 defer javaSdkLibrariesLock.Unlock()
907 *javaSdkLibraries = append(*javaSdkLibraries, module.BaseModuleName())
908}
909
Paul Duffin56d44902020-01-31 13:36:25 +0000910func (module *sdkLibraryImport) scopeProperties() map[*apiScope]*sdkLibraryScopeProperties {
911 p := make(map[*apiScope]*sdkLibraryScopeProperties)
912 p[apiScopePublic] = &module.properties.Public
913 p[apiScopeSystem] = &module.properties.System
914 p[apiScopeTest] = &module.properties.Test
915 return p
916}
917
Colin Cross79c7c262019-04-17 11:11:46 -0700918func (module *sdkLibraryImport) DepsMutator(ctx android.BottomUpMutatorContext) {
Paul Duffin56d44902020-01-31 13:36:25 +0000919 for apiScope, scopeProperties := range module.scopeProperties() {
920 if len(scopeProperties.Jars) == 0 {
921 continue
922 }
923
924 // Add dependencies to the prebuilt stubs library
925 ctx.AddVariationDependencies(nil, apiScope.stubsTag, apiScope.stubsModuleName(module.BaseModuleName()))
926 }
Colin Cross79c7c262019-04-17 11:11:46 -0700927}
928
929func (module *sdkLibraryImport) GenerateAndroidBuildActions(ctx android.ModuleContext) {
930 // Record the paths to the prebuilt stubs library.
931 ctx.VisitDirectDeps(func(to android.Module) {
932 tag := ctx.OtherModuleDependencyTag(to)
933
Paul Duffin56d44902020-01-31 13:36:25 +0000934 if lib, ok := to.(Dependency); ok {
935 if scopeTag, ok := tag.(scopeDependencyTag); ok {
936 apiScope := scopeTag.apiScope
937 scopePaths := module.getScopePaths(apiScope)
938 scopePaths.stubsHeaderPath = lib.HeaderJars()
939 }
Colin Cross79c7c262019-04-17 11:11:46 -0700940 }
941 })
942}
943
Paul Duffin56d44902020-01-31 13:36:25 +0000944func (module *sdkLibraryImport) sdkJars(
945 ctx android.BaseModuleContext,
946 sdkVersion sdkSpec) android.Paths {
947
Paul Duffin50061512020-01-21 16:31:05 +0000948 // If a specific numeric version has been requested then use prebuilt versions of the sdk.
949 if sdkVersion.version.isNumbered() {
950 return PrebuiltJars(ctx, module.BaseModuleName(), sdkVersion)
951 }
952
Paul Duffin56d44902020-01-31 13:36:25 +0000953 var apiScope *apiScope
954 switch sdkVersion.kind {
955 case sdkSystem:
956 apiScope = apiScopeSystem
957 case sdkTest:
958 apiScope = apiScopeTest
959 default:
960 apiScope = apiScopePublic
961 }
962
963 paths := module.getScopePaths(apiScope)
964 return paths.stubsHeaderPath
965}
966
Colin Cross79c7c262019-04-17 11:11:46 -0700967// to satisfy SdkLibraryDependency interface
Jiyong Park6a927c42020-01-21 02:03:43 +0900968func (module *sdkLibraryImport) SdkHeaderJars(ctx android.BaseModuleContext, sdkVersion sdkSpec) android.Paths {
Colin Cross79c7c262019-04-17 11:11:46 -0700969 // This module is just a wrapper for the prebuilt stubs.
Paul Duffin56d44902020-01-31 13:36:25 +0000970 return module.sdkJars(ctx, sdkVersion)
Colin Cross79c7c262019-04-17 11:11:46 -0700971}
972
973// to satisfy SdkLibraryDependency interface
Jiyong Park6a927c42020-01-21 02:03:43 +0900974func (module *sdkLibraryImport) SdkImplementationJars(ctx android.BaseModuleContext, sdkVersion sdkSpec) android.Paths {
Colin Cross79c7c262019-04-17 11:11:46 -0700975 // This module is just a wrapper for the stubs.
Paul Duffin56d44902020-01-31 13:36:25 +0000976 return module.sdkJars(ctx, sdkVersion)
Colin Cross79c7c262019-04-17 11:11:46 -0700977}
Jiyong Parke3833882020-02-17 17:28:10 +0900978
979//
980// java_sdk_library_xml
981//
982type sdkLibraryXml struct {
983 android.ModuleBase
984 android.DefaultableModuleBase
985 android.ApexModuleBase
986
987 properties sdkLibraryXmlProperties
988
989 outputFilePath android.OutputPath
990 installDirPath android.InstallPath
991}
992
993type sdkLibraryXmlProperties struct {
994 // canonical name of the lib
995 Lib_name *string
996}
997
998// java_sdk_library_xml builds the permission xml file for a java_sdk_library.
999// Not to be used directly by users. java_sdk_library internally uses this.
1000func sdkLibraryXmlFactory() android.Module {
1001 module := &sdkLibraryXml{}
1002
1003 module.AddProperties(&module.properties)
1004
1005 android.InitApexModule(module)
1006 android.InitAndroidArchModule(module, android.DeviceSupported, android.MultilibCommon)
1007
1008 return module
1009}
1010
1011// from android.PrebuiltEtcModule
1012func (module *sdkLibraryXml) SubDir() string {
1013 return "permissions"
1014}
1015
1016// from android.PrebuiltEtcModule
1017func (module *sdkLibraryXml) OutputFile() android.OutputPath {
1018 return module.outputFilePath
1019}
1020
1021// from android.ApexModule
1022func (module *sdkLibraryXml) AvailableFor(what string) bool {
1023 return true
1024}
1025
1026func (module *sdkLibraryXml) DepsMutator(ctx android.BottomUpMutatorContext) {
1027 // do nothing
1028}
1029
1030// File path to the runtime implementation library
1031func (module *sdkLibraryXml) implPath() string {
1032 implName := proptools.String(module.properties.Lib_name)
1033 if apexName := module.ApexName(); apexName != "" {
1034 // TODO(b/146468504): ApexName() is only a soong module name, not apex name.
1035 // In most cases, this works fine. But when apex_name is set or override_apex is used
1036 // this can be wrong.
1037 return fmt.Sprintf("/apex/%s/javalib/%s.jar", apexName, implName)
1038 }
1039 partition := "system"
1040 if module.SocSpecific() {
1041 partition = "vendor"
1042 } else if module.DeviceSpecific() {
1043 partition = "odm"
1044 } else if module.ProductSpecific() {
1045 partition = "product"
1046 } else if module.SystemExtSpecific() {
1047 partition = "system_ext"
1048 }
1049 return "/" + partition + "/framework/" + implName + ".jar"
1050}
1051
1052func (module *sdkLibraryXml) GenerateAndroidBuildActions(ctx android.ModuleContext) {
1053 libName := proptools.String(module.properties.Lib_name)
1054 xmlContent := fmt.Sprintf(permissionsTemplate, libName, module.implPath())
1055
1056 module.outputFilePath = android.PathForModuleOut(ctx, libName+".xml").OutputPath
1057 rule := android.NewRuleBuilder()
1058 rule.Command().
1059 Text("/bin/bash -c \"echo -e '" + xmlContent + "'\" > ").
1060 Output(module.outputFilePath)
1061
1062 rule.Build(pctx, ctx, "java_sdk_xml", "Permission XML")
1063
1064 module.installDirPath = android.PathForModuleInstall(ctx, "etc", module.SubDir())
1065}
1066
1067func (module *sdkLibraryXml) AndroidMkEntries() []android.AndroidMkEntries {
1068 if !module.IsForPlatform() {
1069 return []android.AndroidMkEntries{android.AndroidMkEntries{
1070 Disabled: true,
1071 }}
1072 }
1073
1074 return []android.AndroidMkEntries{android.AndroidMkEntries{
1075 Class: "ETC",
1076 OutputFile: android.OptionalPathForPath(module.outputFilePath),
1077 ExtraEntries: []android.AndroidMkExtraEntriesFunc{
1078 func(entries *android.AndroidMkEntries) {
1079 entries.SetString("LOCAL_MODULE_TAGS", "optional")
1080 entries.SetString("LOCAL_MODULE_PATH", module.installDirPath.ToMakePath().String())
1081 entries.SetString("LOCAL_INSTALLED_MODULE_STEM", module.outputFilePath.Base())
1082 },
1083 },
1084 }}
1085}