blob: f34459ded1c93f4c345716ba5a2c844471c9a6fe [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
89}
90
91// Initialize a scope, creating and adding appropriate dependency tags
92func initApiScope(scope *apiScope) *apiScope {
93 //apiScope := &scope
94 scope.stubsTag = scopeDependencyTag{
95 name: scope.name + "-stubs",
96 apiScope: scope,
97 }
98 scope.apiFileTag = scopeDependencyTag{
99 name: scope.name + "-api",
100 apiScope: scope,
101 }
102 return scope
103}
104
105func (scope *apiScope) stubsModuleName(baseName string) string {
106 return baseName + sdkStubsLibrarySuffix + scope.moduleSuffix
107}
108
109func (scope *apiScope) docsModuleName(baseName string) string {
Paul Duffin91b883d2020-02-11 13:05:28 +0000110 return baseName + sdkStubsSourceSuffix + scope.moduleSuffix
Paul Duffind1b3a922020-01-22 11:57:20 +0000111}
112
113type apiScopes []*apiScope
114
115func (scopes apiScopes) Strings(accessor func(*apiScope) string) []string {
116 var list []string
117 for _, scope := range scopes {
118 list = append(list, accessor(scope))
119 }
120 return list
121}
122
Jiyong Parkc678ad32018-04-10 13:07:10 +0900123var (
Paul Duffind1b3a922020-01-22 11:57:20 +0000124 apiScopePublic = initApiScope(&apiScope{
125 name: "public",
126 sdkVersion: "current",
127 })
128 apiScopeSystem = initApiScope(&apiScope{
129 name: "system",
130 apiFilePrefix: "system-",
131 moduleSuffix: sdkSystemApiSuffix,
132 apiFileMakeVariableSuffix: "_SYSTEM",
133 sdkVersion: "system_current",
134 })
135 apiScopeTest = initApiScope(&apiScope{
136 name: "test",
137 apiFilePrefix: "test-",
138 moduleSuffix: sdkTestApiSuffix,
139 apiFileMakeVariableSuffix: "_TEST",
140 sdkVersion: "test_current",
141 })
142 allApiScopes = apiScopes{
143 apiScopePublic,
144 apiScopeSystem,
145 apiScopeTest,
146 }
Jiyong Parkc678ad32018-04-10 13:07:10 +0900147)
148
Jiyong Park82484c02018-04-23 21:41:26 +0900149var (
150 javaSdkLibrariesLock sync.Mutex
151)
152
Jiyong Parkc678ad32018-04-10 13:07:10 +0900153// TODO: these are big features that are currently missing
Jiyong Park1be96912018-05-28 18:02:19 +0900154// 1) disallowing linking to the runtime shared lib
155// 2) HTML generation
Jiyong Parkc678ad32018-04-10 13:07:10 +0900156
157func init() {
Paul Duffin43dc1cc2019-12-19 11:18:54 +0000158 RegisterSdkLibraryBuildComponents(android.InitRegistrationContext)
Jiyong Parkc678ad32018-04-10 13:07:10 +0900159
Jiyong Park82484c02018-04-23 21:41:26 +0900160 android.RegisterMakeVarsProvider(pctx, func(ctx android.MakeVarsContext) {
161 javaSdkLibraries := javaSdkLibraries(ctx.Config())
162 sort.Strings(*javaSdkLibraries)
163 ctx.Strict("JAVA_SDK_LIBRARIES", strings.Join(*javaSdkLibraries, " "))
164 })
Jiyong Parkc678ad32018-04-10 13:07:10 +0900165}
166
Paul Duffin43dc1cc2019-12-19 11:18:54 +0000167func RegisterSdkLibraryBuildComponents(ctx android.RegistrationContext) {
168 ctx.RegisterModuleType("java_sdk_library", SdkLibraryFactory)
169 ctx.RegisterModuleType("java_sdk_library_import", sdkLibraryImportFactory)
170}
171
Jiyong Parkc678ad32018-04-10 13:07:10 +0900172type sdkLibraryProperties struct {
Sundong Ahnf043cf62018-06-25 16:04:37 +0900173 // List of Java libraries that will be in the classpath when building stubs
174 Stub_only_libs []string `android:"arch_variant"`
175
Paul Duffin7a586d32019-12-30 17:09:34 +0000176 // list of package names that will be documented and publicized as API.
177 // This allows the API to be restricted to a subset of the source files provided.
178 // If this is unspecified then all the source files will be treated as being part
179 // of the API.
Jiyong Parkc678ad32018-04-10 13:07:10 +0900180 Api_packages []string
181
Jiyong Park5a2c9d72018-05-01 22:25:41 +0900182 // list of package names that must be hidden from the API
183 Hidden_api_packages []string
184
Paul Duffin749f98f2019-12-30 17:23:46 +0000185 // the relative path to the directory containing the api specification files.
186 // Defaults to "api".
187 Api_dir *string
188
Paul Duffin43db9be2019-12-30 17:35:49 +0000189 // If set to true there is no runtime library.
190 Api_only *bool
191
Paul Duffin11512472019-02-11 15:55:17 +0000192 // local files that are used within user customized droiddoc options.
193 Droiddoc_option_files []string
194
195 // additional droiddoc options
196 // Available variables for substitution:
197 //
198 // $(location <label>): the path to the droiddoc_option_files with name <label>
Sundong Ahndd567f92018-07-31 17:19:11 +0900199 Droiddoc_options []string
200
Sundong Ahn054b19a2018-10-19 13:46:09 +0900201 // a list of top-level directories containing files to merge qualifier annotations
202 // (i.e. those intended to be included in the stubs written) from.
203 Merge_annotations_dirs []string
204
205 // a list of top-level directories containing Java stub files to merge show/hide annotations from.
206 Merge_inclusion_annotations_dirs []string
207
208 // If set to true, the path of dist files is apistubs/core. Defaults to false.
209 Core_lib *bool
210
Sundong Ahn80a87b32019-05-13 15:02:50 +0900211 // don't create dist rules.
212 No_dist *bool `blueprint:"mutated"`
213
Paul Duffin37e0b772019-12-30 17:20:10 +0000214 // indicates whether system and test apis should be managed.
215 Has_system_and_test_apis bool `blueprint:"mutated"`
216
Jiyong Parkc678ad32018-04-10 13:07:10 +0900217 // TODO: determines whether to create HTML doc or not
218 //Html_doc *bool
219}
220
Paul Duffind1b3a922020-01-22 11:57:20 +0000221type scopePaths struct {
222 stubsHeaderPath android.Paths
223 stubsImplPath android.Paths
224 apiFilePath android.Path
225}
226
Paul Duffin56d44902020-01-31 13:36:25 +0000227// Common code between sdk library and sdk library import
228type commonToSdkLibraryAndImport struct {
229 scopePaths map[*apiScope]*scopePaths
230}
231
232func (c *commonToSdkLibraryAndImport) getScopePaths(scope *apiScope) *scopePaths {
233 if c.scopePaths == nil {
234 c.scopePaths = make(map[*apiScope]*scopePaths)
235 }
236 paths := c.scopePaths[scope]
237 if paths == nil {
238 paths = &scopePaths{}
239 c.scopePaths[scope] = paths
240 }
241
242 return paths
243}
244
Inseob Kimc0907f12019-02-08 21:00:45 +0900245type SdkLibrary struct {
Sundong Ahn054b19a2018-10-19 13:46:09 +0900246 Library
Jiyong Parkc678ad32018-04-10 13:07:10 +0900247
Sundong Ahn054b19a2018-10-19 13:46:09 +0900248 sdkLibraryProperties sdkLibraryProperties
Jiyong Parkc678ad32018-04-10 13:07:10 +0900249
Paul Duffin56d44902020-01-31 13:36:25 +0000250 commonToSdkLibraryAndImport
Jiyong Parkc678ad32018-04-10 13:07:10 +0900251}
252
Inseob Kimc0907f12019-02-08 21:00:45 +0900253var _ Dependency = (*SdkLibrary)(nil)
254var _ SdkLibraryDependency = (*SdkLibrary)(nil)
Colin Cross897d2ed2019-02-11 14:03:51 -0800255
Paul Duffind1b3a922020-01-22 11:57:20 +0000256func (module *SdkLibrary) getActiveApiScopes() apiScopes {
257 if module.sdkLibraryProperties.Has_system_and_test_apis {
258 return allApiScopes
259 } else {
260 return apiScopes{apiScopePublic}
261 }
262}
263
Paul Duffine74ac732020-02-06 13:51:46 +0000264var xmlPermissionsFileTag = dependencyTag{name: "xml-permissions-file"}
265
Jiyong Parke3833882020-02-17 17:28:10 +0900266func IsXmlPermissionsFileDepTag(depTag blueprint.DependencyTag) bool {
267 if dt, ok := depTag.(dependencyTag); ok {
268 return dt == xmlPermissionsFileTag
269 }
270 return false
271}
272
Inseob Kimc0907f12019-02-08 21:00:45 +0900273func (module *SdkLibrary) DepsMutator(ctx android.BottomUpMutatorContext) {
Paul Duffind1b3a922020-01-22 11:57:20 +0000274 for _, apiScope := range module.getActiveApiScopes() {
275 // Add dependencies to the stubs library
Paul Duffin50061512020-01-21 16:31:05 +0000276 ctx.AddVariationDependencies(nil, apiScope.stubsTag, module.stubsName(apiScope))
Paul Duffind1b3a922020-01-22 11:57:20 +0000277
Paul Duffin50061512020-01-21 16:31:05 +0000278 // And the api file
Paul Duffind1b3a922020-01-22 11:57:20 +0000279 ctx.AddVariationDependencies(nil, apiScope.apiFileTag, module.docsName(apiScope))
Sundong Ahn054b19a2018-10-19 13:46:09 +0900280 }
281
Paul Duffine74ac732020-02-06 13:51:46 +0000282 if !proptools.Bool(module.sdkLibraryProperties.Api_only) {
283 // Add dependency to the rule for generating the xml permissions file
Jiyong Parke3833882020-02-17 17:28:10 +0900284 ctx.AddDependency(module, xmlPermissionsFileTag, module.xmlFileName())
Paul Duffine74ac732020-02-06 13:51:46 +0000285 }
286
Sundong Ahn054b19a2018-10-19 13:46:09 +0900287 module.Library.deps(ctx)
Jiyong Parkc678ad32018-04-10 13:07:10 +0900288}
289
Inseob Kimc0907f12019-02-08 21:00:45 +0900290func (module *SdkLibrary) GenerateAndroidBuildActions(ctx android.ModuleContext) {
Paul Duffin43db9be2019-12-30 17:35:49 +0000291 // Don't build an implementation library if this is api only.
292 if !proptools.Bool(module.sdkLibraryProperties.Api_only) {
293 module.Library.GenerateAndroidBuildActions(ctx)
294 }
Sundong Ahn054b19a2018-10-19 13:46:09 +0900295
Sundong Ahn57368eb2018-07-06 11:20:23 +0900296 // Record the paths to the header jars of the library (stubs and impl).
Paul Duffind1b3a922020-01-22 11:57:20 +0000297 // When this java_sdk_library is depended upon from others via "libs" property,
Jiyong Parkc678ad32018-04-10 13:07:10 +0900298 // the recorded paths will be returned depending on the link type of the caller.
299 ctx.VisitDirectDeps(func(to android.Module) {
300 otherName := ctx.OtherModuleName(to)
301 tag := ctx.OtherModuleDependencyTag(to)
302
Sundong Ahn57368eb2018-07-06 11:20:23 +0900303 if lib, ok := to.(Dependency); ok {
Paul Duffind1b3a922020-01-22 11:57:20 +0000304 if scopeTag, ok := tag.(scopeDependencyTag); ok {
305 apiScope := scopeTag.apiScope
306 scopePaths := module.getScopePaths(apiScope)
307 scopePaths.stubsHeaderPath = lib.HeaderJars()
308 scopePaths.stubsImplPath = lib.ImplementationJars()
Jiyong Parkc678ad32018-04-10 13:07:10 +0900309 }
310 }
Sundong Ahn20e998b2018-07-24 11:19:26 +0900311 if doc, ok := to.(ApiFilePath); ok {
Paul Duffind1b3a922020-01-22 11:57:20 +0000312 if scopeTag, ok := tag.(scopeDependencyTag); ok {
313 apiScope := scopeTag.apiScope
314 scopePaths := module.getScopePaths(apiScope)
315 scopePaths.apiFilePath = doc.ApiFilePath()
316 } else {
Sundong Ahn20e998b2018-07-24 11:19:26 +0900317 ctx.ModuleErrorf("depends on module %q of unknown tag %q", otherName, tag)
318 }
319 }
Jiyong Parkc678ad32018-04-10 13:07:10 +0900320 })
321}
322
Jiyong Park0b0e1b92019-12-03 13:24:29 +0900323func (module *SdkLibrary) AndroidMkEntries() []android.AndroidMkEntries {
Paul Duffin43db9be2019-12-30 17:35:49 +0000324 if proptools.Bool(module.sdkLibraryProperties.Api_only) {
325 return nil
326 }
Jiyong Park0b0e1b92019-12-03 13:24:29 +0900327 entriesList := module.Library.AndroidMkEntries()
328 entries := &entriesList[0]
Jaewoong Jungb0c127c2019-08-29 14:56:03 -0700329 entries.Required = append(entries.Required, module.xmlFileName())
Jiyong Park0b0e1b92019-12-03 13:24:29 +0900330 return entriesList
Jiyong Park82484c02018-04-23 21:41:26 +0900331}
332
Jiyong Parkc678ad32018-04-10 13:07:10 +0900333// Module name of the stubs library
Paul Duffind1b3a922020-01-22 11:57:20 +0000334func (module *SdkLibrary) stubsName(apiScope *apiScope) string {
335 return apiScope.stubsModuleName(module.BaseModuleName())
Jiyong Parkc678ad32018-04-10 13:07:10 +0900336}
337
338// Module name of the docs
Paul Duffind1b3a922020-01-22 11:57:20 +0000339func (module *SdkLibrary) docsName(apiScope *apiScope) string {
340 return apiScope.docsModuleName(module.BaseModuleName())
Jiyong Parkc678ad32018-04-10 13:07:10 +0900341}
342
343// Module name of the runtime implementation library
Inseob Kimc0907f12019-02-08 21:00:45 +0900344func (module *SdkLibrary) implName() string {
Sundong Ahn054b19a2018-10-19 13:46:09 +0900345 return module.BaseModuleName()
Jiyong Parkc678ad32018-04-10 13:07:10 +0900346}
347
Jiyong Parkc678ad32018-04-10 13:07:10 +0900348// Module name of the XML file for the lib
Inseob Kimc0907f12019-02-08 21:00:45 +0900349func (module *SdkLibrary) xmlFileName() string {
Jiyong Parkc678ad32018-04-10 13:07:10 +0900350 return module.BaseModuleName() + sdkXmlFileSuffix
351}
352
Anton Hansson6bb88102020-03-27 19:43:19 +0000353// The dist path of the stub artifacts
354func (module *SdkLibrary) apiDistPath(apiScope *apiScope) string {
355 if module.ModuleBase.Owner() != "" {
356 return path.Join("apistubs", module.ModuleBase.Owner(), apiScope.name)
357 } else if Bool(module.sdkLibraryProperties.Core_lib) {
358 return path.Join("apistubs", "core", apiScope.name)
359 } else {
360 return path.Join("apistubs", "android", apiScope.name)
361 }
362}
363
Paul Duffin12ceb462019-12-24 20:31:31 +0000364// Get the sdk version for use when compiling the stubs library.
Paul Duffind1b3a922020-01-22 11:57:20 +0000365func (module *SdkLibrary) sdkVersionForStubsLibrary(mctx android.LoadHookContext, apiScope *apiScope) string {
Paul Duffin12ceb462019-12-24 20:31:31 +0000366 sdkDep := decodeSdkDep(mctx, sdkContext(&module.Library))
367 if sdkDep.hasStandardLibs() {
368 // If building against a standard sdk then use the sdk version appropriate for the scope.
Paul Duffind1b3a922020-01-22 11:57:20 +0000369 return apiScope.sdkVersion
Paul Duffin12ceb462019-12-24 20:31:31 +0000370 } else {
371 // Otherwise, use no system module.
372 return "none"
373 }
374}
375
Paul Duffind1b3a922020-01-22 11:57:20 +0000376func (module *SdkLibrary) latestApiFilegroupName(apiScope *apiScope) string {
377 return ":" + module.BaseModuleName() + ".api." + apiScope.name + ".latest"
Jiyong Park58c518b2018-05-12 22:29:12 +0900378}
Jiyong Parkc678ad32018-04-10 13:07:10 +0900379
Paul Duffind1b3a922020-01-22 11:57:20 +0000380func (module *SdkLibrary) latestRemovedApiFilegroupName(apiScope *apiScope) string {
381 return ":" + module.BaseModuleName() + "-removed.api." + apiScope.name + ".latest"
Jiyong Parkc678ad32018-04-10 13:07:10 +0900382}
383
384// Creates a static java library that has API stubs
Paul Duffind1b3a922020-01-22 11:57:20 +0000385func (module *SdkLibrary) createStubsLibrary(mctx android.LoadHookContext, apiScope *apiScope) {
Jiyong Parkc678ad32018-04-10 13:07:10 +0900386 props := struct {
Sundong Ahn0d7dff42019-12-04 12:53:44 +0900387 Name *string
388 Srcs []string
Paul Duffin367ab912019-12-23 19:40:36 +0000389 Installable *bool
Sundong Ahn0d7dff42019-12-04 12:53:44 +0900390 Sdk_version *string
Paul Duffin12ceb462019-12-24 20:31:31 +0000391 System_modules *string
Paul Duffinab8da5d2020-02-07 16:12:04 +0000392 Patch_module *string
Sundong Ahn0d7dff42019-12-04 12:53:44 +0900393 Libs []string
394 Soc_specific *bool
395 Device_specific *bool
396 Product_specific *bool
397 System_ext_specific *bool
398 Compile_dex *bool
Sundong Ahn0d7dff42019-12-04 12:53:44 +0900399 Java_version *string
400 Product_variables struct {
Jiyong Park82484c02018-04-23 21:41:26 +0900401 Pdk struct {
402 Enabled *bool
403 }
Jiyong Parkc678ad32018-04-10 13:07:10 +0900404 }
Sundong Ahn054b19a2018-10-19 13:46:09 +0900405 Openjdk9 struct {
406 Srcs []string
407 Javacflags []string
408 }
Anton Hansson6bb88102020-03-27 19:43:19 +0000409 Dist struct {
410 Targets []string
411 Dest *string
412 Dir *string
413 Tag *string
414 }
Jiyong Parkc678ad32018-04-10 13:07:10 +0900415 }{}
416
Jiyong Parkdf130542018-04-27 16:29:21 +0900417 props.Name = proptools.StringPtr(module.stubsName(apiScope))
Jiyong Parkc678ad32018-04-10 13:07:10 +0900418 // sources are generated from the droiddoc
Jiyong Parkdf130542018-04-27 16:29:21 +0900419 props.Srcs = []string{":" + module.docsName(apiScope)}
Paul Duffin12ceb462019-12-24 20:31:31 +0000420 sdkVersion := module.sdkVersionForStubsLibrary(mctx, apiScope)
Paul Duffin52d398a2019-06-11 12:31:14 +0100421 props.Sdk_version = proptools.StringPtr(sdkVersion)
Paul Duffin12ceb462019-12-24 20:31:31 +0000422 props.System_modules = module.Library.Module.deviceProperties.System_modules
Paul Duffinab8da5d2020-02-07 16:12:04 +0000423 props.Patch_module = module.Library.Module.properties.Patch_module
Paul Duffin367ab912019-12-23 19:40:36 +0000424 props.Installable = proptools.BoolPtr(false)
Sundong Ahn054b19a2018-10-19 13:46:09 +0900425 props.Libs = module.sdkLibraryProperties.Stub_only_libs
Jiyong Park82484c02018-04-23 21:41:26 +0900426 props.Product_variables.Pdk.Enabled = proptools.BoolPtr(false)
Sundong Ahn054b19a2018-10-19 13:46:09 +0900427 props.Openjdk9.Srcs = module.Library.Module.properties.Openjdk9.Srcs
428 props.Openjdk9.Javacflags = module.Library.Module.properties.Openjdk9.Javacflags
429 props.Java_version = module.Library.Module.properties.Java_version
430 if module.Library.Module.deviceProperties.Compile_dex != nil {
431 props.Compile_dex = module.Library.Module.deviceProperties.Compile_dex
Sundong Ahndd567f92018-07-31 17:19:11 +0900432 }
Jiyong Parkc678ad32018-04-10 13:07:10 +0900433
434 if module.SocSpecific() {
435 props.Soc_specific = proptools.BoolPtr(true)
436 } else if module.DeviceSpecific() {
437 props.Device_specific = proptools.BoolPtr(true)
438 } else if module.ProductSpecific() {
439 props.Product_specific = proptools.BoolPtr(true)
Sundong Ahn0d7dff42019-12-04 12:53:44 +0900440 } else if module.SystemExtSpecific() {
441 props.System_ext_specific = proptools.BoolPtr(true)
Jiyong Parkc678ad32018-04-10 13:07:10 +0900442 }
Anton Hansson6bb88102020-03-27 19:43:19 +0000443 // Dist the class jar artifact for sdk builds.
444 if !Bool(module.sdkLibraryProperties.No_dist) {
445 props.Dist.Targets = []string{"sdk", "win_sdk"}
446 props.Dist.Dest = proptools.StringPtr(fmt.Sprintf("%v.jar", module.BaseModuleName()))
447 props.Dist.Dir = proptools.StringPtr(module.apiDistPath(apiScope))
448 props.Dist.Tag = proptools.StringPtr(".jar")
449 }
Jiyong Parkc678ad32018-04-10 13:07:10 +0900450
Colin Cross84dfc3d2019-09-25 11:33:01 -0700451 mctx.CreateModule(LibraryFactory, &props)
Jiyong Parkc678ad32018-04-10 13:07:10 +0900452}
453
Paul Duffincbcfcaa2020-04-07 18:49:53 +0100454// Creates a droidstubs module that creates stubs source files from the given full source
Jiyong Parkc678ad32018-04-10 13:07:10 +0900455// files
Paul Duffind1b3a922020-01-22 11:57:20 +0000456func (module *SdkLibrary) createStubsSources(mctx android.LoadHookContext, apiScope *apiScope) {
Jiyong Parkc678ad32018-04-10 13:07:10 +0900457 props := struct {
Sundong Ahn054b19a2018-10-19 13:46:09 +0900458 Name *string
459 Srcs []string
460 Installable *bool
Paul Duffin52d398a2019-06-11 12:31:14 +0100461 Sdk_version *string
Paul Duffin12ceb462019-12-24 20:31:31 +0000462 System_modules *string
Sundong Ahn054b19a2018-10-19 13:46:09 +0900463 Libs []string
Paul Duffin11512472019-02-11 15:55:17 +0000464 Arg_files []string
Sundong Ahn054b19a2018-10-19 13:46:09 +0900465 Args *string
Sundong Ahn054b19a2018-10-19 13:46:09 +0900466 Java_version *string
467 Merge_annotations_dirs []string
468 Merge_inclusion_annotations_dirs []string
469 Check_api struct {
Inseob Kim38449af2019-02-28 14:24:05 +0900470 Current ApiToCheck
471 Last_released ApiToCheck
472 Ignore_missing_latest_api *bool
Jiyong Park58c518b2018-05-12 22:29:12 +0900473 }
Sundong Ahn1b92c822018-05-29 11:35:17 +0900474 Aidl struct {
475 Include_dirs []string
476 Local_include_dirs []string
477 }
Anton Hansson6bb88102020-03-27 19:43:19 +0000478 Dist struct {
479 Targets []string
480 Dest *string
481 Dir *string
482 }
Jiyong Parkc678ad32018-04-10 13:07:10 +0900483 }{}
484
Paul Duffin250e6192019-06-07 10:44:37 +0100485 sdkDep := decodeSdkDep(mctx, sdkContext(&module.Library))
Paul Duffin12ceb462019-12-24 20:31:31 +0000486 // Use the platform API if standard libraries were requested, otherwise use
487 // no default libraries.
Paul Duffin52d398a2019-06-11 12:31:14 +0100488 sdkVersion := ""
489 if !sdkDep.hasStandardLibs() {
490 sdkVersion = "none"
491 }
Paul Duffin250e6192019-06-07 10:44:37 +0100492
Jiyong Parkdf130542018-04-27 16:29:21 +0900493 props.Name = proptools.StringPtr(module.docsName(apiScope))
Sundong Ahn054b19a2018-10-19 13:46:09 +0900494 props.Srcs = append(props.Srcs, module.Library.Module.properties.Srcs...)
Paul Duffin52d398a2019-06-11 12:31:14 +0100495 props.Sdk_version = proptools.StringPtr(sdkVersion)
Paul Duffin12ceb462019-12-24 20:31:31 +0000496 props.System_modules = module.Library.Module.deviceProperties.System_modules
Jiyong Parkc678ad32018-04-10 13:07:10 +0900497 props.Installable = proptools.BoolPtr(false)
Sundong Ahne6f0b052018-06-05 16:46:14 +0900498 // A droiddoc module has only one Libs property and doesn't distinguish between
499 // shared libs and static libs. So we need to add both of these libs to Libs property.
Sundong Ahn054b19a2018-10-19 13:46:09 +0900500 props.Libs = module.Library.Module.properties.Libs
501 props.Libs = append(props.Libs, module.Library.Module.properties.Static_libs...)
502 props.Aidl.Include_dirs = module.Library.Module.deviceProperties.Aidl.Include_dirs
503 props.Aidl.Local_include_dirs = module.Library.Module.deviceProperties.Aidl.Local_include_dirs
Sundong Ahn054b19a2018-10-19 13:46:09 +0900504 props.Java_version = module.Library.Module.properties.Java_version
Jiyong Parkc678ad32018-04-10 13:07:10 +0900505
Sundong Ahn054b19a2018-10-19 13:46:09 +0900506 props.Merge_annotations_dirs = module.sdkLibraryProperties.Merge_annotations_dirs
507 props.Merge_inclusion_annotations_dirs = module.sdkLibraryProperties.Merge_inclusion_annotations_dirs
508
Paul Duffincbcfcaa2020-04-07 18:49:53 +0100509 droidstubsArgs := []string{}
Paul Duffin235ffff2019-12-24 10:41:30 +0000510 if len(module.sdkLibraryProperties.Api_packages) != 0 {
Paul Duffincbcfcaa2020-04-07 18:49:53 +0100511 droidstubsArgs = append(droidstubsArgs, "--stub-packages "+strings.Join(module.sdkLibraryProperties.Api_packages, ":"))
Paul Duffin235ffff2019-12-24 10:41:30 +0000512 }
513 if len(module.sdkLibraryProperties.Hidden_api_packages) != 0 {
Paul Duffincbcfcaa2020-04-07 18:49:53 +0100514 droidstubsArgs = append(droidstubsArgs,
Paul Duffin235ffff2019-12-24 10:41:30 +0000515 android.JoinWithPrefix(module.sdkLibraryProperties.Hidden_api_packages, " --hide-package "))
516 }
Paul Duffincbcfcaa2020-04-07 18:49:53 +0100517 droidstubsArgs = append(droidstubsArgs, module.sdkLibraryProperties.Droiddoc_options...)
Paul Duffin235ffff2019-12-24 10:41:30 +0000518 disabledWarnings := []string{
519 "MissingPermission",
520 "BroadcastBehavior",
521 "HiddenSuperclass",
522 "DeprecationMismatch",
523 "UnavailableSymbol",
524 "SdkConstant",
525 "HiddenTypeParameter",
526 "Todo",
527 "Typo",
528 }
Paul Duffincbcfcaa2020-04-07 18:49:53 +0100529 droidstubsArgs = append(droidstubsArgs, android.JoinWithPrefix(disabledWarnings, "--hide "))
Sundong Ahnfb2721f2018-09-17 13:23:09 +0900530
Jiyong Parkdf130542018-04-27 16:29:21 +0900531 switch apiScope {
532 case apiScopeSystem:
Paul Duffincbcfcaa2020-04-07 18:49:53 +0100533 droidstubsArgs = append(droidstubsArgs, "-showAnnotation android.annotation.SystemApi")
Jiyong Parkdf130542018-04-27 16:29:21 +0900534 case apiScopeTest:
Paul Duffincbcfcaa2020-04-07 18:49:53 +0100535 droidstubsArgs = append(droidstubsArgs, " -showAnnotation android.annotation.TestApi")
Jiyong Parkc678ad32018-04-10 13:07:10 +0900536 }
Paul Duffin11512472019-02-11 15:55:17 +0000537 props.Arg_files = module.sdkLibraryProperties.Droiddoc_option_files
Paul Duffincbcfcaa2020-04-07 18:49:53 +0100538 props.Args = proptools.StringPtr(strings.Join(droidstubsArgs, " "))
Jiyong Parkc678ad32018-04-10 13:07:10 +0900539
540 // List of APIs identified from the provided source files are created. They are later
541 // compared against to the not-yet-released (a.k.a current) list of APIs and to the
542 // last-released (a.k.a numbered) list of API.
Paul Duffind1b3a922020-01-22 11:57:20 +0000543 currentApiFileName := apiScope.apiFilePrefix + "current.txt"
544 removedApiFileName := apiScope.apiFilePrefix + "removed.txt"
Paul Duffin749f98f2019-12-30 17:23:46 +0000545 apiDir := module.getApiDir()
546 currentApiFileName = path.Join(apiDir, currentApiFileName)
547 removedApiFileName = path.Join(apiDir, removedApiFileName)
Jiyong Parkc678ad32018-04-10 13:07:10 +0900548
Jiyong Park58c518b2018-05-12 22:29:12 +0900549 // check against the not-yet-release API
550 props.Check_api.Current.Api_file = proptools.StringPtr(currentApiFileName)
551 props.Check_api.Current.Removed_api_file = proptools.StringPtr(removedApiFileName)
Jiyong Park58c518b2018-05-12 22:29:12 +0900552
553 // check against the latest released API
554 props.Check_api.Last_released.Api_file = proptools.StringPtr(
555 module.latestApiFilegroupName(apiScope))
556 props.Check_api.Last_released.Removed_api_file = proptools.StringPtr(
557 module.latestRemovedApiFilegroupName(apiScope))
Inseob Kim38449af2019-02-28 14:24:05 +0900558 props.Check_api.Ignore_missing_latest_api = proptools.BoolPtr(true)
Jiyong Park58c518b2018-05-12 22:29:12 +0900559
Anton Hansson6bb88102020-03-27 19:43:19 +0000560 // Dist the api txt artifact for sdk builds.
561 if !Bool(module.sdkLibraryProperties.No_dist) {
562 props.Dist.Targets = []string{"sdk", "win_sdk"}
563 props.Dist.Dest = proptools.StringPtr(fmt.Sprintf("%v.txt", module.BaseModuleName()))
564 props.Dist.Dir = proptools.StringPtr(path.Join(module.apiDistPath(apiScope), "api"))
565 }
566
Colin Cross84dfc3d2019-09-25 11:33:01 -0700567 mctx.CreateModule(DroidstubsFactory, &props)
Jiyong Parkc678ad32018-04-10 13:07:10 +0900568}
569
Jooyung Hanb8fa86a2020-03-10 06:23:13 +0900570func (module *SdkLibrary) DepIsInSameApex(mctx android.BaseModuleContext, dep android.Module) bool {
571 depTag := mctx.OtherModuleDependencyTag(dep)
572 if depTag == xmlPermissionsFileTag {
573 return true
574 }
575 return module.Library.DepIsInSameApex(mctx, dep)
576}
577
Jiyong Parkc678ad32018-04-10 13:07:10 +0900578// Creates the xml file that publicizes the runtime library
Colin Crossf8b860a2019-04-16 14:43:28 -0700579func (module *SdkLibrary) createXmlFile(mctx android.LoadHookContext) {
Jiyong Parke3833882020-02-17 17:28:10 +0900580 props := struct {
Sundong Ahn0d7dff42019-12-04 12:53:44 +0900581 Name *string
Jiyong Parke3833882020-02-17 17:28:10 +0900582 Lib_name *string
Sundong Ahn0d7dff42019-12-04 12:53:44 +0900583 Soc_specific *bool
584 Device_specific *bool
585 Product_specific *bool
586 System_ext_specific *bool
Jooyung Hanb8fa86a2020-03-10 06:23:13 +0900587 Apex_available []string
Jiyong Parke3833882020-02-17 17:28:10 +0900588 }{
Jooyung Hanb8fa86a2020-03-10 06:23:13 +0900589 Name: proptools.StringPtr(module.xmlFileName()),
590 Lib_name: proptools.StringPtr(module.BaseModuleName()),
591 Apex_available: module.ApexProperties.Apex_available,
Jiyong Parkc678ad32018-04-10 13:07:10 +0900592 }
Jiyong Parke3833882020-02-17 17:28:10 +0900593
594 if module.SocSpecific() {
595 props.Soc_specific = proptools.BoolPtr(true)
596 } else if module.DeviceSpecific() {
597 props.Device_specific = proptools.BoolPtr(true)
598 } else if module.ProductSpecific() {
599 props.Product_specific = proptools.BoolPtr(true)
600 } else if module.SystemExtSpecific() {
601 props.System_ext_specific = proptools.BoolPtr(true)
602 }
603
604 mctx.CreateModule(sdkLibraryXmlFactory, &props)
Jiyong Parkc678ad32018-04-10 13:07:10 +0900605}
606
Paul Duffin50061512020-01-21 16:31:05 +0000607func PrebuiltJars(ctx android.BaseModuleContext, baseName string, s sdkSpec) android.Paths {
Jiyong Park6a927c42020-01-21 02:03:43 +0900608 var ver sdkVersion
609 var kind sdkKind
610 if s.usePrebuilt(ctx) {
611 ver = s.version
612 kind = s.kind
Jiyong Parkc678ad32018-04-10 13:07:10 +0900613 } else {
Jiyong Park6a927c42020-01-21 02:03:43 +0900614 // We don't have prebuilt SDK for the specific sdkVersion.
615 // Instead of breaking the build, fallback to use "system_current"
616 ver = sdkVersionCurrent
617 kind = sdkSystem
Sundong Ahn054b19a2018-10-19 13:46:09 +0900618 }
Jiyong Park6a927c42020-01-21 02:03:43 +0900619
620 dir := filepath.Join("prebuilts", "sdk", ver.String(), kind.String())
Paul Duffin50061512020-01-21 16:31:05 +0000621 jar := filepath.Join(dir, baseName+".jar")
Sundong Ahn054b19a2018-10-19 13:46:09 +0900622 jarPath := android.ExistentPathForSource(ctx, jar)
Sundong Ahnae418ac2019-02-28 15:01:28 +0900623 if !jarPath.Valid() {
Colin Cross07c88562020-01-07 09:34:44 -0800624 if ctx.Config().AllowMissingDependencies() {
625 return android.Paths{android.PathForSource(ctx, jar)}
626 } else {
Jiyong Park6a927c42020-01-21 02:03:43 +0900627 ctx.PropertyErrorf("sdk_library", "invalid sdk version %q, %q does not exist", s.raw, jar)
Colin Cross07c88562020-01-07 09:34:44 -0800628 }
Sundong Ahnae418ac2019-02-28 15:01:28 +0900629 return nil
630 }
Sundong Ahn054b19a2018-10-19 13:46:09 +0900631 return android.Paths{jarPath.Path()}
632}
633
Paul Duffind1b3a922020-01-22 11:57:20 +0000634func (module *SdkLibrary) sdkJars(
635 ctx android.BaseModuleContext,
636 sdkVersion sdkSpec,
637 headerJars bool) android.Paths {
638
Paul Duffin50061512020-01-21 16:31:05 +0000639 // If a specific numeric version has been requested then use prebuilt versions of the sdk.
640 if sdkVersion.version.isNumbered() {
641 return PrebuiltJars(ctx, module.BaseModuleName(), sdkVersion)
Sundong Ahn054b19a2018-10-19 13:46:09 +0900642 } else {
Paul Duffind1b3a922020-01-22 11:57:20 +0000643 if !sdkVersion.specified() {
644 if headerJars {
645 return module.Library.HeaderJars()
646 } else {
647 return module.Library.ImplementationJars()
648 }
649 }
Paul Duffin726d23c2020-01-22 16:30:37 +0000650 var apiScope *apiScope
Jiyong Park6a927c42020-01-21 02:03:43 +0900651 switch sdkVersion.kind {
652 case sdkSystem:
Paul Duffin726d23c2020-01-22 16:30:37 +0000653 apiScope = apiScopeSystem
654 case sdkTest:
655 apiScope = apiScopeTest
Jiyong Park6a927c42020-01-21 02:03:43 +0900656 case sdkPrivate:
Sundong Ahn054b19a2018-10-19 13:46:09 +0900657 return module.Library.HeaderJars()
Jiyong Park6a927c42020-01-21 02:03:43 +0900658 default:
Paul Duffin726d23c2020-01-22 16:30:37 +0000659 apiScope = apiScopePublic
Paul Duffind1b3a922020-01-22 11:57:20 +0000660 }
661
Paul Duffin726d23c2020-01-22 16:30:37 +0000662 paths := module.getScopePaths(apiScope)
Paul Duffind1b3a922020-01-22 11:57:20 +0000663 if headerJars {
664 return paths.stubsHeaderPath
665 } else {
666 return paths.stubsImplPath
Sundong Ahn054b19a2018-10-19 13:46:09 +0900667 }
Jiyong Parkc678ad32018-04-10 13:07:10 +0900668 }
669}
670
Sundong Ahn241cd372018-07-13 16:16:44 +0900671// to satisfy SdkLibraryDependency interface
Paul Duffind1b3a922020-01-22 11:57:20 +0000672func (module *SdkLibrary) SdkHeaderJars(ctx android.BaseModuleContext, sdkVersion sdkSpec) android.Paths {
673 return module.sdkJars(ctx, sdkVersion, true /*headerJars*/)
674}
675
676// to satisfy SdkLibraryDependency interface
Jiyong Park6a927c42020-01-21 02:03:43 +0900677func (module *SdkLibrary) SdkImplementationJars(ctx android.BaseModuleContext, sdkVersion sdkSpec) android.Paths {
Paul Duffind1b3a922020-01-22 11:57:20 +0000678 return module.sdkJars(ctx, sdkVersion, false /*headerJars*/)
Sundong Ahn241cd372018-07-13 16:16:44 +0900679}
680
Sundong Ahn80a87b32019-05-13 15:02:50 +0900681func (module *SdkLibrary) SetNoDist() {
682 module.sdkLibraryProperties.No_dist = proptools.BoolPtr(true)
683}
684
Colin Cross571cccf2019-02-04 11:22:08 -0800685var javaSdkLibrariesKey = android.NewOnceKey("javaSdkLibraries")
686
Jiyong Park82484c02018-04-23 21:41:26 +0900687func javaSdkLibraries(config android.Config) *[]string {
Colin Cross571cccf2019-02-04 11:22:08 -0800688 return config.Once(javaSdkLibrariesKey, func() interface{} {
Jiyong Park82484c02018-04-23 21:41:26 +0900689 return &[]string{}
690 }).(*[]string)
691}
692
Paul Duffin749f98f2019-12-30 17:23:46 +0000693func (module *SdkLibrary) getApiDir() string {
694 return proptools.StringDefault(module.sdkLibraryProperties.Api_dir, "api")
695}
696
Jiyong Parkc678ad32018-04-10 13:07:10 +0900697// For a java_sdk_library module, create internal modules for stubs, docs,
698// runtime libs and xml file. If requested, the stubs and docs are created twice
699// once for public API level and once for system API level
Colin Crossf8b860a2019-04-16 14:43:28 -0700700func (module *SdkLibrary) CreateInternalModules(mctx android.LoadHookContext) {
Inseob Kim6e93ac92019-03-21 17:43:49 +0900701 if len(module.Library.Module.properties.Srcs) == 0 {
Inseob Kimc0907f12019-02-08 21:00:45 +0900702 mctx.PropertyErrorf("srcs", "java_sdk_library must specify srcs")
Jooyung Han58f26ab2019-12-18 15:34:32 +0900703 return
Inseob Kimc0907f12019-02-08 21:00:45 +0900704 }
705
Paul Duffin37e0b772019-12-30 17:20:10 +0000706 // If this builds against standard libraries (i.e. is not part of the core libraries)
707 // then assume it provides both system and test apis. Otherwise, assume it does not and
708 // also assume it does not contribute to the dist build.
709 sdkDep := decodeSdkDep(mctx, sdkContext(&module.Library))
710 hasSystemAndTestApis := sdkDep.hasStandardLibs()
711 module.sdkLibraryProperties.Has_system_and_test_apis = hasSystemAndTestApis
712 module.sdkLibraryProperties.No_dist = proptools.BoolPtr(!hasSystemAndTestApis)
713
Inseob Kim8098faa2019-03-18 10:19:51 +0900714 missing_current_api := false
715
Paul Duffind1b3a922020-01-22 11:57:20 +0000716 activeScopes := module.getActiveApiScopes()
717
Paul Duffin749f98f2019-12-30 17:23:46 +0000718 apiDir := module.getApiDir()
Paul Duffind1b3a922020-01-22 11:57:20 +0000719 for _, scope := range activeScopes {
Inseob Kim8098faa2019-03-18 10:19:51 +0900720 for _, api := range []string{"current.txt", "removed.txt"} {
Paul Duffind1b3a922020-01-22 11:57:20 +0000721 path := path.Join(mctx.ModuleDir(), apiDir, scope.apiFilePrefix+api)
Inseob Kim8098faa2019-03-18 10:19:51 +0900722 p := android.ExistentPathForSource(mctx, path)
723 if !p.Valid() {
724 mctx.ModuleErrorf("Current api file %#v doesn't exist", path)
725 missing_current_api = true
726 }
727 }
728 }
729
730 if missing_current_api {
731 script := "build/soong/scripts/gen-java-current-api-files.sh"
732 p := android.ExistentPathForSource(mctx, script)
733
734 if !p.Valid() {
735 panic(fmt.Sprintf("script file %s doesn't exist", script))
736 }
737
738 mctx.ModuleErrorf("One or more current api files are missing. "+
739 "You can update them by:\n"+
Paul Duffin37e0b772019-12-30 17:20:10 +0000740 "%s %q %s && m update-api",
Paul Duffind1b3a922020-01-22 11:57:20 +0000741 script, filepath.Join(mctx.ModuleDir(), apiDir),
742 strings.Join(activeScopes.Strings(func(s *apiScope) string { return s.apiFilePrefix }), " "))
Inseob Kim8098faa2019-03-18 10:19:51 +0900743 return
744 }
745
Paul Duffind1b3a922020-01-22 11:57:20 +0000746 for _, scope := range activeScopes {
747 module.createStubsLibrary(mctx, scope)
748 module.createStubsSources(mctx, scope)
Inseob Kimc0907f12019-02-08 21:00:45 +0900749 }
750
Paul Duffin43db9be2019-12-30 17:35:49 +0000751 if !proptools.Bool(module.sdkLibraryProperties.Api_only) {
752 // for runtime
753 module.createXmlFile(mctx)
754
755 // record java_sdk_library modules so that they are exported to make
756 javaSdkLibraries := javaSdkLibraries(mctx.Config())
757 javaSdkLibrariesLock.Lock()
758 defer javaSdkLibrariesLock.Unlock()
759 *javaSdkLibraries = append(*javaSdkLibraries, module.BaseModuleName())
760 }
Inseob Kimc0907f12019-02-08 21:00:45 +0900761}
762
763func (module *SdkLibrary) InitSdkLibraryProperties() {
Sundong Ahn054b19a2018-10-19 13:46:09 +0900764 module.AddProperties(
765 &module.sdkLibraryProperties,
766 &module.Library.Module.properties,
767 &module.Library.Module.dexpreoptProperties,
768 &module.Library.Module.deviceProperties,
769 &module.Library.Module.protoProperties,
770 )
771
772 module.Library.Module.properties.Installable = proptools.BoolPtr(true)
773 module.Library.Module.deviceProperties.IsSDKLibrary = true
Inseob Kimc0907f12019-02-08 21:00:45 +0900774}
Sundong Ahn054b19a2018-10-19 13:46:09 +0900775
Jaewoong Jung4f158ee2019-07-11 10:05:35 -0700776// java_sdk_library is a special Java library that provides optional platform APIs to apps.
777// In practice, it can be viewed as a combination of several modules: 1) stubs library that clients
778// are linked against to, 2) droiddoc module that internally generates API stubs source files,
779// 3) the real runtime shared library that implements the APIs, and 4) XML file for adding
780// the runtime lib to the classpath at runtime if requested via <uses-library>.
Inseob Kimc0907f12019-02-08 21:00:45 +0900781func SdkLibraryFactory() android.Module {
782 module := &SdkLibrary{}
783 module.InitSdkLibraryProperties()
Jooyung Han58f26ab2019-12-18 15:34:32 +0900784 android.InitApexModule(module)
Sundong Ahn054b19a2018-10-19 13:46:09 +0900785 InitJavaModule(module, android.HostAndDeviceSupported)
Colin Crossf8b860a2019-04-16 14:43:28 -0700786 android.AddLoadHook(module, func(ctx android.LoadHookContext) { module.CreateInternalModules(ctx) })
Jiyong Parkc678ad32018-04-10 13:07:10 +0900787 return module
788}
Colin Cross79c7c262019-04-17 11:11:46 -0700789
790//
791// SDK library prebuilts
792//
793
Paul Duffin56d44902020-01-31 13:36:25 +0000794// Properties associated with each api scope.
795type sdkLibraryScopeProperties struct {
Colin Cross79c7c262019-04-17 11:11:46 -0700796 Jars []string `android:"path"`
797
798 Sdk_version *string
799
Colin Cross79c7c262019-04-17 11:11:46 -0700800 // List of shared java libs that this module has dependencies to
801 Libs []string
Colin Cross79c7c262019-04-17 11:11:46 -0700802}
803
Paul Duffin56d44902020-01-31 13:36:25 +0000804type sdkLibraryImportProperties struct {
Paul Duffinfcfd7912020-01-31 17:54:30 +0000805 // List of shared java libs, common to all scopes, that this module has
806 // dependencies to
807 Libs []string
808
Paul Duffin56d44902020-01-31 13:36:25 +0000809 // Properties associated with the public api scope.
810 Public sdkLibraryScopeProperties
811
812 // Properties associated with the system api scope.
813 System sdkLibraryScopeProperties
814
815 // Properties associated with the test api scope.
816 Test sdkLibraryScopeProperties
817}
818
Colin Cross79c7c262019-04-17 11:11:46 -0700819type sdkLibraryImport struct {
820 android.ModuleBase
821 android.DefaultableModuleBase
822 prebuilt android.Prebuilt
823
824 properties sdkLibraryImportProperties
825
Paul Duffin56d44902020-01-31 13:36:25 +0000826 commonToSdkLibraryAndImport
Colin Cross79c7c262019-04-17 11:11:46 -0700827}
828
829var _ SdkLibraryDependency = (*sdkLibraryImport)(nil)
830
Jaewoong Jung4f158ee2019-07-11 10:05:35 -0700831// java_sdk_library_import imports a prebuilt java_sdk_library.
Colin Cross79c7c262019-04-17 11:11:46 -0700832func sdkLibraryImportFactory() android.Module {
833 module := &sdkLibraryImport{}
834
Paul Duffinfcfd7912020-01-31 17:54:30 +0000835 module.AddProperties(&module.properties)
Colin Cross79c7c262019-04-17 11:11:46 -0700836
Paul Duffin0bdcb272020-02-06 15:24:57 +0000837 android.InitPrebuiltModule(module, &[]string{""})
Colin Cross79c7c262019-04-17 11:11:46 -0700838 InitJavaModule(module, android.HostAndDeviceSupported)
839
840 android.AddLoadHook(module, func(mctx android.LoadHookContext) { module.createInternalModules(mctx) })
841 return module
842}
843
844func (module *sdkLibraryImport) Prebuilt() *android.Prebuilt {
845 return &module.prebuilt
846}
847
848func (module *sdkLibraryImport) Name() string {
849 return module.prebuilt.Name(module.ModuleBase.Name())
850}
851
852func (module *sdkLibraryImport) createInternalModules(mctx android.LoadHookContext) {
Colin Cross79c7c262019-04-17 11:11:46 -0700853
Paul Duffin50061512020-01-21 16:31:05 +0000854 // If the build is configured to use prebuilts then force this to be preferred.
855 if mctx.Config().UnbundledBuildUsePrebuiltSdks() {
856 module.prebuilt.ForcePrefer()
857 }
858
Paul Duffin56d44902020-01-31 13:36:25 +0000859 for apiScope, scopeProperties := range module.scopeProperties() {
860 if len(scopeProperties.Jars) == 0 {
861 continue
862 }
863
864 // Creates a java import for the jar with ".stubs" suffix
865 props := struct {
866 Name *string
867 Soc_specific *bool
868 Device_specific *bool
869 Product_specific *bool
870 System_ext_specific *bool
871 Sdk_version *string
872 Libs []string
873 Jars []string
Paul Duffin50061512020-01-21 16:31:05 +0000874 Prefer *bool
Paul Duffin56d44902020-01-31 13:36:25 +0000875 }{}
876
877 props.Name = proptools.StringPtr(apiScope.stubsModuleName(module.BaseModuleName()))
878 props.Sdk_version = scopeProperties.Sdk_version
Paul Duffinfcfd7912020-01-31 17:54:30 +0000879 // Prepend any of the libs from the legacy public properties to the libs for each of the
880 // scopes to avoid having to duplicate them in each scope.
881 props.Libs = append(module.properties.Libs, scopeProperties.Libs...)
Paul Duffin56d44902020-01-31 13:36:25 +0000882 props.Jars = scopeProperties.Jars
883
884 if module.SocSpecific() {
885 props.Soc_specific = proptools.BoolPtr(true)
886 } else if module.DeviceSpecific() {
887 props.Device_specific = proptools.BoolPtr(true)
888 } else if module.ProductSpecific() {
889 props.Product_specific = proptools.BoolPtr(true)
890 } else if module.SystemExtSpecific() {
891 props.System_ext_specific = proptools.BoolPtr(true)
892 }
893
Paul Duffin50061512020-01-21 16:31:05 +0000894 // If the build should use prebuilt sdks then set prefer to true on the stubs library.
895 // That will cause the prebuilt version of the stubs to override the source version.
896 if mctx.Config().UnbundledBuildUsePrebuiltSdks() {
897 props.Prefer = proptools.BoolPtr(true)
898 }
899
Paul Duffin56d44902020-01-31 13:36:25 +0000900 mctx.CreateModule(ImportFactory, &props)
901 }
Colin Cross79c7c262019-04-17 11:11:46 -0700902
903 javaSdkLibraries := javaSdkLibraries(mctx.Config())
904 javaSdkLibrariesLock.Lock()
905 defer javaSdkLibrariesLock.Unlock()
906 *javaSdkLibraries = append(*javaSdkLibraries, module.BaseModuleName())
907}
908
Paul Duffin56d44902020-01-31 13:36:25 +0000909func (module *sdkLibraryImport) scopeProperties() map[*apiScope]*sdkLibraryScopeProperties {
910 p := make(map[*apiScope]*sdkLibraryScopeProperties)
911 p[apiScopePublic] = &module.properties.Public
912 p[apiScopeSystem] = &module.properties.System
913 p[apiScopeTest] = &module.properties.Test
914 return p
915}
916
Colin Cross79c7c262019-04-17 11:11:46 -0700917func (module *sdkLibraryImport) DepsMutator(ctx android.BottomUpMutatorContext) {
Paul Duffin56d44902020-01-31 13:36:25 +0000918 for apiScope, scopeProperties := range module.scopeProperties() {
919 if len(scopeProperties.Jars) == 0 {
920 continue
921 }
922
923 // Add dependencies to the prebuilt stubs library
924 ctx.AddVariationDependencies(nil, apiScope.stubsTag, apiScope.stubsModuleName(module.BaseModuleName()))
925 }
Colin Cross79c7c262019-04-17 11:11:46 -0700926}
927
928func (module *sdkLibraryImport) GenerateAndroidBuildActions(ctx android.ModuleContext) {
929 // Record the paths to the prebuilt stubs library.
930 ctx.VisitDirectDeps(func(to android.Module) {
931 tag := ctx.OtherModuleDependencyTag(to)
932
Paul Duffin56d44902020-01-31 13:36:25 +0000933 if lib, ok := to.(Dependency); ok {
934 if scopeTag, ok := tag.(scopeDependencyTag); ok {
935 apiScope := scopeTag.apiScope
936 scopePaths := module.getScopePaths(apiScope)
937 scopePaths.stubsHeaderPath = lib.HeaderJars()
938 }
Colin Cross79c7c262019-04-17 11:11:46 -0700939 }
940 })
941}
942
Paul Duffin56d44902020-01-31 13:36:25 +0000943func (module *sdkLibraryImport) sdkJars(
944 ctx android.BaseModuleContext,
945 sdkVersion sdkSpec) android.Paths {
946
Paul Duffin50061512020-01-21 16:31:05 +0000947 // If a specific numeric version has been requested then use prebuilt versions of the sdk.
948 if sdkVersion.version.isNumbered() {
949 return PrebuiltJars(ctx, module.BaseModuleName(), sdkVersion)
950 }
951
Paul Duffin56d44902020-01-31 13:36:25 +0000952 var apiScope *apiScope
953 switch sdkVersion.kind {
954 case sdkSystem:
955 apiScope = apiScopeSystem
956 case sdkTest:
957 apiScope = apiScopeTest
958 default:
959 apiScope = apiScopePublic
960 }
961
962 paths := module.getScopePaths(apiScope)
963 return paths.stubsHeaderPath
964}
965
Colin Cross79c7c262019-04-17 11:11:46 -0700966// to satisfy SdkLibraryDependency interface
Jiyong Park6a927c42020-01-21 02:03:43 +0900967func (module *sdkLibraryImport) SdkHeaderJars(ctx android.BaseModuleContext, sdkVersion sdkSpec) android.Paths {
Colin Cross79c7c262019-04-17 11:11:46 -0700968 // This module is just a wrapper for the prebuilt stubs.
Paul Duffin56d44902020-01-31 13:36:25 +0000969 return module.sdkJars(ctx, sdkVersion)
Colin Cross79c7c262019-04-17 11:11:46 -0700970}
971
972// to satisfy SdkLibraryDependency interface
Jiyong Park6a927c42020-01-21 02:03:43 +0900973func (module *sdkLibraryImport) SdkImplementationJars(ctx android.BaseModuleContext, sdkVersion sdkSpec) android.Paths {
Colin Cross79c7c262019-04-17 11:11:46 -0700974 // This module is just a wrapper for the stubs.
Paul Duffin56d44902020-01-31 13:36:25 +0000975 return module.sdkJars(ctx, sdkVersion)
Colin Cross79c7c262019-04-17 11:11:46 -0700976}
Jiyong Parke3833882020-02-17 17:28:10 +0900977
978//
979// java_sdk_library_xml
980//
981type sdkLibraryXml struct {
982 android.ModuleBase
983 android.DefaultableModuleBase
984 android.ApexModuleBase
985
986 properties sdkLibraryXmlProperties
987
988 outputFilePath android.OutputPath
989 installDirPath android.InstallPath
990}
991
992type sdkLibraryXmlProperties struct {
993 // canonical name of the lib
994 Lib_name *string
995}
996
997// java_sdk_library_xml builds the permission xml file for a java_sdk_library.
998// Not to be used directly by users. java_sdk_library internally uses this.
999func sdkLibraryXmlFactory() android.Module {
1000 module := &sdkLibraryXml{}
1001
1002 module.AddProperties(&module.properties)
1003
1004 android.InitApexModule(module)
1005 android.InitAndroidArchModule(module, android.DeviceSupported, android.MultilibCommon)
1006
1007 return module
1008}
1009
1010// from android.PrebuiltEtcModule
1011func (module *sdkLibraryXml) SubDir() string {
1012 return "permissions"
1013}
1014
1015// from android.PrebuiltEtcModule
1016func (module *sdkLibraryXml) OutputFile() android.OutputPath {
1017 return module.outputFilePath
1018}
1019
1020// from android.ApexModule
1021func (module *sdkLibraryXml) AvailableFor(what string) bool {
1022 return true
1023}
1024
1025func (module *sdkLibraryXml) DepsMutator(ctx android.BottomUpMutatorContext) {
1026 // do nothing
1027}
1028
1029// File path to the runtime implementation library
1030func (module *sdkLibraryXml) implPath() string {
1031 implName := proptools.String(module.properties.Lib_name)
1032 if apexName := module.ApexName(); apexName != "" {
1033 // TODO(b/146468504): ApexName() is only a soong module name, not apex name.
1034 // In most cases, this works fine. But when apex_name is set or override_apex is used
1035 // this can be wrong.
1036 return fmt.Sprintf("/apex/%s/javalib/%s.jar", apexName, implName)
1037 }
1038 partition := "system"
1039 if module.SocSpecific() {
1040 partition = "vendor"
1041 } else if module.DeviceSpecific() {
1042 partition = "odm"
1043 } else if module.ProductSpecific() {
1044 partition = "product"
1045 } else if module.SystemExtSpecific() {
1046 partition = "system_ext"
1047 }
1048 return "/" + partition + "/framework/" + implName + ".jar"
1049}
1050
1051func (module *sdkLibraryXml) GenerateAndroidBuildActions(ctx android.ModuleContext) {
1052 libName := proptools.String(module.properties.Lib_name)
1053 xmlContent := fmt.Sprintf(permissionsTemplate, libName, module.implPath())
1054
1055 module.outputFilePath = android.PathForModuleOut(ctx, libName+".xml").OutputPath
1056 rule := android.NewRuleBuilder()
1057 rule.Command().
1058 Text("/bin/bash -c \"echo -e '" + xmlContent + "'\" > ").
1059 Output(module.outputFilePath)
1060
1061 rule.Build(pctx, ctx, "java_sdk_xml", "Permission XML")
1062
1063 module.installDirPath = android.PathForModuleInstall(ctx, "etc", module.SubDir())
1064}
1065
1066func (module *sdkLibraryXml) AndroidMkEntries() []android.AndroidMkEntries {
1067 if !module.IsForPlatform() {
1068 return []android.AndroidMkEntries{android.AndroidMkEntries{
1069 Disabled: true,
1070 }}
1071 }
1072
1073 return []android.AndroidMkEntries{android.AndroidMkEntries{
1074 Class: "ETC",
1075 OutputFile: android.OptionalPathForPath(module.outputFilePath),
1076 ExtraEntries: []android.AndroidMkExtraEntriesFunc{
1077 func(entries *android.AndroidMkEntries) {
1078 entries.SetString("LOCAL_MODULE_TAGS", "optional")
1079 entries.SetString("LOCAL_MODULE_PATH", module.installDirPath.ToMakePath().String())
1080 entries.SetString("LOCAL_INSTALLED_MODULE_STEM", module.outputFilePath.Base())
1081 },
1082 },
1083 }}
1084}