blob: 52c900489356142510a3fb538dc708cd9ecde899 [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 Hansson5fd5d242020-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
Jiyong Parkc678ad32018-04-10 13:07:10 +0900376// $(INTERNAL_PLATFORM_<apiTagName>_API_FILE) points to the generated
377// api file for the current source
378// TODO: remove this when apicheck is done in soong
Paul Duffind1b3a922020-01-22 11:57:20 +0000379func (module *SdkLibrary) apiTagName(apiScope *apiScope) string {
380 return strings.Replace(strings.ToUpper(module.BaseModuleName()), ".", "_", -1) + apiScope.apiFileMakeVariableSuffix
Jiyong Parkc678ad32018-04-10 13:07:10 +0900381}
382
Paul Duffind1b3a922020-01-22 11:57:20 +0000383func (module *SdkLibrary) latestApiFilegroupName(apiScope *apiScope) string {
384 return ":" + module.BaseModuleName() + ".api." + apiScope.name + ".latest"
Jiyong Park58c518b2018-05-12 22:29:12 +0900385}
Jiyong Parkc678ad32018-04-10 13:07:10 +0900386
Paul Duffind1b3a922020-01-22 11:57:20 +0000387func (module *SdkLibrary) latestRemovedApiFilegroupName(apiScope *apiScope) string {
388 return ":" + module.BaseModuleName() + "-removed.api." + apiScope.name + ".latest"
Jiyong Parkc678ad32018-04-10 13:07:10 +0900389}
390
391// Creates a static java library that has API stubs
Paul Duffind1b3a922020-01-22 11:57:20 +0000392func (module *SdkLibrary) createStubsLibrary(mctx android.LoadHookContext, apiScope *apiScope) {
Jiyong Parkc678ad32018-04-10 13:07:10 +0900393 props := struct {
Sundong Ahn0d7dff42019-12-04 12:53:44 +0900394 Name *string
395 Srcs []string
Paul Duffin367ab912019-12-23 19:40:36 +0000396 Installable *bool
Sundong Ahn0d7dff42019-12-04 12:53:44 +0900397 Sdk_version *string
Paul Duffin12ceb462019-12-24 20:31:31 +0000398 System_modules *string
Paul Duffinab8da5d2020-02-07 16:12:04 +0000399 Patch_module *string
Sundong Ahn0d7dff42019-12-04 12:53:44 +0900400 Libs []string
401 Soc_specific *bool
402 Device_specific *bool
403 Product_specific *bool
404 System_ext_specific *bool
405 Compile_dex *bool
Sundong Ahn0d7dff42019-12-04 12:53:44 +0900406 Java_version *string
407 Product_variables struct {
Jiyong Park82484c02018-04-23 21:41:26 +0900408 Pdk struct {
409 Enabled *bool
410 }
Jiyong Parkc678ad32018-04-10 13:07:10 +0900411 }
Sundong Ahn054b19a2018-10-19 13:46:09 +0900412 Openjdk9 struct {
413 Srcs []string
414 Javacflags []string
415 }
Anton Hansson5fd5d242020-03-27 19:43:19 +0000416 Dist struct {
417 Targets []string
418 Dest *string
419 Dir *string
420 Tag *string
421 }
Jiyong Parkc678ad32018-04-10 13:07:10 +0900422 }{}
423
Jiyong Parkdf130542018-04-27 16:29:21 +0900424 props.Name = proptools.StringPtr(module.stubsName(apiScope))
Jiyong Parkc678ad32018-04-10 13:07:10 +0900425 // sources are generated from the droiddoc
Jiyong Parkdf130542018-04-27 16:29:21 +0900426 props.Srcs = []string{":" + module.docsName(apiScope)}
Paul Duffin12ceb462019-12-24 20:31:31 +0000427 sdkVersion := module.sdkVersionForStubsLibrary(mctx, apiScope)
Paul Duffin52d398a2019-06-11 12:31:14 +0100428 props.Sdk_version = proptools.StringPtr(sdkVersion)
Paul Duffin12ceb462019-12-24 20:31:31 +0000429 props.System_modules = module.Library.Module.deviceProperties.System_modules
Paul Duffinab8da5d2020-02-07 16:12:04 +0000430 props.Patch_module = module.Library.Module.properties.Patch_module
Paul Duffin367ab912019-12-23 19:40:36 +0000431 props.Installable = proptools.BoolPtr(false)
Sundong Ahn054b19a2018-10-19 13:46:09 +0900432 props.Libs = module.sdkLibraryProperties.Stub_only_libs
Jiyong Park82484c02018-04-23 21:41:26 +0900433 props.Product_variables.Pdk.Enabled = proptools.BoolPtr(false)
Sundong Ahn054b19a2018-10-19 13:46:09 +0900434 props.Openjdk9.Srcs = module.Library.Module.properties.Openjdk9.Srcs
435 props.Openjdk9.Javacflags = module.Library.Module.properties.Openjdk9.Javacflags
436 props.Java_version = module.Library.Module.properties.Java_version
437 if module.Library.Module.deviceProperties.Compile_dex != nil {
438 props.Compile_dex = module.Library.Module.deviceProperties.Compile_dex
Sundong Ahndd567f92018-07-31 17:19:11 +0900439 }
Jiyong Parkc678ad32018-04-10 13:07:10 +0900440
441 if module.SocSpecific() {
442 props.Soc_specific = proptools.BoolPtr(true)
443 } else if module.DeviceSpecific() {
444 props.Device_specific = proptools.BoolPtr(true)
445 } else if module.ProductSpecific() {
446 props.Product_specific = proptools.BoolPtr(true)
Sundong Ahn0d7dff42019-12-04 12:53:44 +0900447 } else if module.SystemExtSpecific() {
448 props.System_ext_specific = proptools.BoolPtr(true)
Jiyong Parkc678ad32018-04-10 13:07:10 +0900449 }
Anton Hansson5fd5d242020-03-27 19:43:19 +0000450 // Dist the class jar artifact for sdk builds.
451 if !Bool(module.sdkLibraryProperties.No_dist) {
452 props.Dist.Targets = []string{"sdk", "win_sdk"}
453 props.Dist.Dest = proptools.StringPtr(fmt.Sprintf("%v.jar", module.BaseModuleName()))
454 props.Dist.Dir = proptools.StringPtr(module.apiDistPath(apiScope))
455 props.Dist.Tag = proptools.StringPtr(".jar")
456 }
Jiyong Parkc678ad32018-04-10 13:07:10 +0900457
Colin Cross84dfc3d2019-09-25 11:33:01 -0700458 mctx.CreateModule(LibraryFactory, &props)
Jiyong Parkc678ad32018-04-10 13:07:10 +0900459}
460
461// Creates a droiddoc module that creates stubs source files from the given full source
462// files
Paul Duffind1b3a922020-01-22 11:57:20 +0000463func (module *SdkLibrary) createStubsSources(mctx android.LoadHookContext, apiScope *apiScope) {
Jiyong Parkc678ad32018-04-10 13:07:10 +0900464 props := struct {
Sundong Ahn054b19a2018-10-19 13:46:09 +0900465 Name *string
466 Srcs []string
467 Installable *bool
Paul Duffin52d398a2019-06-11 12:31:14 +0100468 Sdk_version *string
Paul Duffin12ceb462019-12-24 20:31:31 +0000469 System_modules *string
Sundong Ahn054b19a2018-10-19 13:46:09 +0900470 Libs []string
Paul Duffin11512472019-02-11 15:55:17 +0000471 Arg_files []string
Sundong Ahn054b19a2018-10-19 13:46:09 +0900472 Args *string
473 Api_tag_name *string
474 Api_filename *string
475 Removed_api_filename *string
Sundong Ahn054b19a2018-10-19 13:46:09 +0900476 Java_version *string
477 Merge_annotations_dirs []string
478 Merge_inclusion_annotations_dirs []string
479 Check_api struct {
Inseob Kim38449af2019-02-28 14:24:05 +0900480 Current ApiToCheck
481 Last_released ApiToCheck
482 Ignore_missing_latest_api *bool
Jiyong Park58c518b2018-05-12 22:29:12 +0900483 }
Sundong Ahn1b92c822018-05-29 11:35:17 +0900484 Aidl struct {
485 Include_dirs []string
486 Local_include_dirs []string
487 }
Anton Hansson5fd5d242020-03-27 19:43:19 +0000488 Dist struct {
489 Targets []string
490 Dest *string
491 Dir *string
492 }
Jiyong Parkc678ad32018-04-10 13:07:10 +0900493 }{}
494
Paul Duffin250e6192019-06-07 10:44:37 +0100495 sdkDep := decodeSdkDep(mctx, sdkContext(&module.Library))
Paul Duffin12ceb462019-12-24 20:31:31 +0000496 // Use the platform API if standard libraries were requested, otherwise use
497 // no default libraries.
Paul Duffin52d398a2019-06-11 12:31:14 +0100498 sdkVersion := ""
499 if !sdkDep.hasStandardLibs() {
500 sdkVersion = "none"
501 }
Paul Duffin250e6192019-06-07 10:44:37 +0100502
Jiyong Parkdf130542018-04-27 16:29:21 +0900503 props.Name = proptools.StringPtr(module.docsName(apiScope))
Sundong Ahn054b19a2018-10-19 13:46:09 +0900504 props.Srcs = append(props.Srcs, module.Library.Module.properties.Srcs...)
Paul Duffin52d398a2019-06-11 12:31:14 +0100505 props.Sdk_version = proptools.StringPtr(sdkVersion)
Paul Duffin12ceb462019-12-24 20:31:31 +0000506 props.System_modules = module.Library.Module.deviceProperties.System_modules
Jiyong Parkc678ad32018-04-10 13:07:10 +0900507 props.Installable = proptools.BoolPtr(false)
Sundong Ahne6f0b052018-06-05 16:46:14 +0900508 // A droiddoc module has only one Libs property and doesn't distinguish between
509 // shared libs and static libs. So we need to add both of these libs to Libs property.
Sundong Ahn054b19a2018-10-19 13:46:09 +0900510 props.Libs = module.Library.Module.properties.Libs
511 props.Libs = append(props.Libs, module.Library.Module.properties.Static_libs...)
512 props.Aidl.Include_dirs = module.Library.Module.deviceProperties.Aidl.Include_dirs
513 props.Aidl.Local_include_dirs = module.Library.Module.deviceProperties.Aidl.Local_include_dirs
Sundong Ahn054b19a2018-10-19 13:46:09 +0900514 props.Java_version = module.Library.Module.properties.Java_version
Jiyong Parkc678ad32018-04-10 13:07:10 +0900515
Sundong Ahn054b19a2018-10-19 13:46:09 +0900516 props.Merge_annotations_dirs = module.sdkLibraryProperties.Merge_annotations_dirs
517 props.Merge_inclusion_annotations_dirs = module.sdkLibraryProperties.Merge_inclusion_annotations_dirs
518
Paul Duffin235ffff2019-12-24 10:41:30 +0000519 droiddocArgs := []string{}
520 if len(module.sdkLibraryProperties.Api_packages) != 0 {
521 droiddocArgs = append(droiddocArgs, "--stub-packages "+strings.Join(module.sdkLibraryProperties.Api_packages, ":"))
522 }
523 if len(module.sdkLibraryProperties.Hidden_api_packages) != 0 {
524 droiddocArgs = append(droiddocArgs,
525 android.JoinWithPrefix(module.sdkLibraryProperties.Hidden_api_packages, " --hide-package "))
526 }
527 droiddocArgs = append(droiddocArgs, module.sdkLibraryProperties.Droiddoc_options...)
528 disabledWarnings := []string{
529 "MissingPermission",
530 "BroadcastBehavior",
531 "HiddenSuperclass",
532 "DeprecationMismatch",
533 "UnavailableSymbol",
534 "SdkConstant",
535 "HiddenTypeParameter",
536 "Todo",
537 "Typo",
538 }
539 droiddocArgs = append(droiddocArgs, android.JoinWithPrefix(disabledWarnings, "--hide "))
Sundong Ahnfb2721f2018-09-17 13:23:09 +0900540
Jiyong Parkdf130542018-04-27 16:29:21 +0900541 switch apiScope {
542 case apiScopeSystem:
Paul Duffin235ffff2019-12-24 10:41:30 +0000543 droiddocArgs = append(droiddocArgs, "-showAnnotation android.annotation.SystemApi")
Jiyong Parkdf130542018-04-27 16:29:21 +0900544 case apiScopeTest:
Paul Duffin235ffff2019-12-24 10:41:30 +0000545 droiddocArgs = append(droiddocArgs, " -showAnnotation android.annotation.TestApi")
Jiyong Parkc678ad32018-04-10 13:07:10 +0900546 }
Paul Duffin11512472019-02-11 15:55:17 +0000547 props.Arg_files = module.sdkLibraryProperties.Droiddoc_option_files
Paul Duffin235ffff2019-12-24 10:41:30 +0000548 props.Args = proptools.StringPtr(strings.Join(droiddocArgs, " "))
Jiyong Parkc678ad32018-04-10 13:07:10 +0900549
550 // List of APIs identified from the provided source files are created. They are later
551 // compared against to the not-yet-released (a.k.a current) list of APIs and to the
552 // last-released (a.k.a numbered) list of API.
Paul Duffind1b3a922020-01-22 11:57:20 +0000553 currentApiFileName := apiScope.apiFilePrefix + "current.txt"
554 removedApiFileName := apiScope.apiFilePrefix + "removed.txt"
Paul Duffin749f98f2019-12-30 17:23:46 +0000555 apiDir := module.getApiDir()
556 currentApiFileName = path.Join(apiDir, currentApiFileName)
557 removedApiFileName = path.Join(apiDir, removedApiFileName)
Jiyong Park58c518b2018-05-12 22:29:12 +0900558 // TODO(jiyong): remove these three props
Jiyong Parkdf130542018-04-27 16:29:21 +0900559 props.Api_tag_name = proptools.StringPtr(module.apiTagName(apiScope))
Jiyong Parkc678ad32018-04-10 13:07:10 +0900560 props.Api_filename = proptools.StringPtr(currentApiFileName)
561 props.Removed_api_filename = proptools.StringPtr(removedApiFileName)
562
Jiyong Park58c518b2018-05-12 22:29:12 +0900563 // check against the not-yet-release API
564 props.Check_api.Current.Api_file = proptools.StringPtr(currentApiFileName)
565 props.Check_api.Current.Removed_api_file = proptools.StringPtr(removedApiFileName)
Jiyong Park58c518b2018-05-12 22:29:12 +0900566
567 // check against the latest released API
568 props.Check_api.Last_released.Api_file = proptools.StringPtr(
569 module.latestApiFilegroupName(apiScope))
570 props.Check_api.Last_released.Removed_api_file = proptools.StringPtr(
571 module.latestRemovedApiFilegroupName(apiScope))
Inseob Kim38449af2019-02-28 14:24:05 +0900572 props.Check_api.Ignore_missing_latest_api = proptools.BoolPtr(true)
Jiyong Park58c518b2018-05-12 22:29:12 +0900573
Anton Hansson5fd5d242020-03-27 19:43:19 +0000574 // Dist the api txt artifact for sdk builds.
575 if !Bool(module.sdkLibraryProperties.No_dist) {
576 props.Dist.Targets = []string{"sdk", "win_sdk"}
577 props.Dist.Dest = proptools.StringPtr(fmt.Sprintf("%v.txt", module.BaseModuleName()))
578 props.Dist.Dir = proptools.StringPtr(path.Join(module.apiDistPath(apiScope), "api"))
579 }
580
Colin Cross84dfc3d2019-09-25 11:33:01 -0700581 mctx.CreateModule(DroidstubsFactory, &props)
Jiyong Parkc678ad32018-04-10 13:07:10 +0900582}
583
Jooyung Han5e9013b2020-03-10 06:23:13 +0900584func (module *SdkLibrary) DepIsInSameApex(mctx android.BaseModuleContext, dep android.Module) bool {
585 depTag := mctx.OtherModuleDependencyTag(dep)
586 if depTag == xmlPermissionsFileTag {
587 return true
588 }
589 return module.Library.DepIsInSameApex(mctx, dep)
590}
591
Jiyong Parkc678ad32018-04-10 13:07:10 +0900592// Creates the xml file that publicizes the runtime library
Colin Crossf8b860a2019-04-16 14:43:28 -0700593func (module *SdkLibrary) createXmlFile(mctx android.LoadHookContext) {
Jiyong Parke3833882020-02-17 17:28:10 +0900594 props := struct {
Sundong Ahn0d7dff42019-12-04 12:53:44 +0900595 Name *string
Jiyong Parke3833882020-02-17 17:28:10 +0900596 Lib_name *string
Sundong Ahn0d7dff42019-12-04 12:53:44 +0900597 Soc_specific *bool
598 Device_specific *bool
599 Product_specific *bool
600 System_ext_specific *bool
Jooyung Han5e9013b2020-03-10 06:23:13 +0900601 Apex_available []string
Jiyong Parke3833882020-02-17 17:28:10 +0900602 }{
Jooyung Han5e9013b2020-03-10 06:23:13 +0900603 Name: proptools.StringPtr(module.xmlFileName()),
604 Lib_name: proptools.StringPtr(module.BaseModuleName()),
605 Apex_available: module.ApexProperties.Apex_available,
Jiyong Parkc678ad32018-04-10 13:07:10 +0900606 }
Jiyong Parke3833882020-02-17 17:28:10 +0900607
608 if module.SocSpecific() {
609 props.Soc_specific = proptools.BoolPtr(true)
610 } else if module.DeviceSpecific() {
611 props.Device_specific = proptools.BoolPtr(true)
612 } else if module.ProductSpecific() {
613 props.Product_specific = proptools.BoolPtr(true)
614 } else if module.SystemExtSpecific() {
615 props.System_ext_specific = proptools.BoolPtr(true)
616 }
617
618 mctx.CreateModule(sdkLibraryXmlFactory, &props)
Jiyong Parkc678ad32018-04-10 13:07:10 +0900619}
620
Paul Duffin50061512020-01-21 16:31:05 +0000621func PrebuiltJars(ctx android.BaseModuleContext, baseName string, s sdkSpec) android.Paths {
Jiyong Park6a927c42020-01-21 02:03:43 +0900622 var ver sdkVersion
623 var kind sdkKind
624 if s.usePrebuilt(ctx) {
625 ver = s.version
626 kind = s.kind
Jiyong Parkc678ad32018-04-10 13:07:10 +0900627 } else {
Jiyong Park6a927c42020-01-21 02:03:43 +0900628 // We don't have prebuilt SDK for the specific sdkVersion.
629 // Instead of breaking the build, fallback to use "system_current"
630 ver = sdkVersionCurrent
631 kind = sdkSystem
Sundong Ahn054b19a2018-10-19 13:46:09 +0900632 }
Jiyong Park6a927c42020-01-21 02:03:43 +0900633
634 dir := filepath.Join("prebuilts", "sdk", ver.String(), kind.String())
Paul Duffin50061512020-01-21 16:31:05 +0000635 jar := filepath.Join(dir, baseName+".jar")
Sundong Ahn054b19a2018-10-19 13:46:09 +0900636 jarPath := android.ExistentPathForSource(ctx, jar)
Sundong Ahnae418ac2019-02-28 15:01:28 +0900637 if !jarPath.Valid() {
Colin Cross07c88562020-01-07 09:34:44 -0800638 if ctx.Config().AllowMissingDependencies() {
639 return android.Paths{android.PathForSource(ctx, jar)}
640 } else {
Jiyong Park6a927c42020-01-21 02:03:43 +0900641 ctx.PropertyErrorf("sdk_library", "invalid sdk version %q, %q does not exist", s.raw, jar)
Colin Cross07c88562020-01-07 09:34:44 -0800642 }
Sundong Ahnae418ac2019-02-28 15:01:28 +0900643 return nil
644 }
Sundong Ahn054b19a2018-10-19 13:46:09 +0900645 return android.Paths{jarPath.Path()}
646}
647
Paul Duffind1b3a922020-01-22 11:57:20 +0000648func (module *SdkLibrary) sdkJars(
649 ctx android.BaseModuleContext,
650 sdkVersion sdkSpec,
651 headerJars bool) android.Paths {
652
Paul Duffin50061512020-01-21 16:31:05 +0000653 // If a specific numeric version has been requested then use prebuilt versions of the sdk.
654 if sdkVersion.version.isNumbered() {
655 return PrebuiltJars(ctx, module.BaseModuleName(), sdkVersion)
Sundong Ahn054b19a2018-10-19 13:46:09 +0900656 } else {
Paul Duffind1b3a922020-01-22 11:57:20 +0000657 if !sdkVersion.specified() {
658 if headerJars {
659 return module.Library.HeaderJars()
660 } else {
661 return module.Library.ImplementationJars()
662 }
663 }
Paul Duffin726d23c2020-01-22 16:30:37 +0000664 var apiScope *apiScope
Jiyong Park6a927c42020-01-21 02:03:43 +0900665 switch sdkVersion.kind {
666 case sdkSystem:
Paul Duffin726d23c2020-01-22 16:30:37 +0000667 apiScope = apiScopeSystem
668 case sdkTest:
669 apiScope = apiScopeTest
Jiyong Park6a927c42020-01-21 02:03:43 +0900670 case sdkPrivate:
Sundong Ahn054b19a2018-10-19 13:46:09 +0900671 return module.Library.HeaderJars()
Jiyong Park6a927c42020-01-21 02:03:43 +0900672 default:
Paul Duffin726d23c2020-01-22 16:30:37 +0000673 apiScope = apiScopePublic
Paul Duffind1b3a922020-01-22 11:57:20 +0000674 }
675
Paul Duffin726d23c2020-01-22 16:30:37 +0000676 paths := module.getScopePaths(apiScope)
Paul Duffind1b3a922020-01-22 11:57:20 +0000677 if headerJars {
678 return paths.stubsHeaderPath
679 } else {
680 return paths.stubsImplPath
Sundong Ahn054b19a2018-10-19 13:46:09 +0900681 }
Jiyong Parkc678ad32018-04-10 13:07:10 +0900682 }
683}
684
Sundong Ahn241cd372018-07-13 16:16:44 +0900685// to satisfy SdkLibraryDependency interface
Paul Duffind1b3a922020-01-22 11:57:20 +0000686func (module *SdkLibrary) SdkHeaderJars(ctx android.BaseModuleContext, sdkVersion sdkSpec) android.Paths {
687 return module.sdkJars(ctx, sdkVersion, true /*headerJars*/)
688}
689
690// to satisfy SdkLibraryDependency interface
Jiyong Park6a927c42020-01-21 02:03:43 +0900691func (module *SdkLibrary) SdkImplementationJars(ctx android.BaseModuleContext, sdkVersion sdkSpec) android.Paths {
Paul Duffind1b3a922020-01-22 11:57:20 +0000692 return module.sdkJars(ctx, sdkVersion, false /*headerJars*/)
Sundong Ahn241cd372018-07-13 16:16:44 +0900693}
694
Sundong Ahn80a87b32019-05-13 15:02:50 +0900695func (module *SdkLibrary) SetNoDist() {
696 module.sdkLibraryProperties.No_dist = proptools.BoolPtr(true)
697}
698
Colin Cross571cccf2019-02-04 11:22:08 -0800699var javaSdkLibrariesKey = android.NewOnceKey("javaSdkLibraries")
700
Jiyong Park82484c02018-04-23 21:41:26 +0900701func javaSdkLibraries(config android.Config) *[]string {
Colin Cross571cccf2019-02-04 11:22:08 -0800702 return config.Once(javaSdkLibrariesKey, func() interface{} {
Jiyong Park82484c02018-04-23 21:41:26 +0900703 return &[]string{}
704 }).(*[]string)
705}
706
Paul Duffin749f98f2019-12-30 17:23:46 +0000707func (module *SdkLibrary) getApiDir() string {
708 return proptools.StringDefault(module.sdkLibraryProperties.Api_dir, "api")
709}
710
Jiyong Parkc678ad32018-04-10 13:07:10 +0900711// For a java_sdk_library module, create internal modules for stubs, docs,
712// runtime libs and xml file. If requested, the stubs and docs are created twice
713// once for public API level and once for system API level
Colin Crossf8b860a2019-04-16 14:43:28 -0700714func (module *SdkLibrary) CreateInternalModules(mctx android.LoadHookContext) {
Inseob Kim6e93ac92019-03-21 17:43:49 +0900715 if len(module.Library.Module.properties.Srcs) == 0 {
Inseob Kimc0907f12019-02-08 21:00:45 +0900716 mctx.PropertyErrorf("srcs", "java_sdk_library must specify srcs")
Jooyung Han58f26ab2019-12-18 15:34:32 +0900717 return
Inseob Kimc0907f12019-02-08 21:00:45 +0900718 }
719
Paul Duffin37e0b772019-12-30 17:20:10 +0000720 // If this builds against standard libraries (i.e. is not part of the core libraries)
721 // then assume it provides both system and test apis. Otherwise, assume it does not and
722 // also assume it does not contribute to the dist build.
723 sdkDep := decodeSdkDep(mctx, sdkContext(&module.Library))
724 hasSystemAndTestApis := sdkDep.hasStandardLibs()
725 module.sdkLibraryProperties.Has_system_and_test_apis = hasSystemAndTestApis
726 module.sdkLibraryProperties.No_dist = proptools.BoolPtr(!hasSystemAndTestApis)
727
Inseob Kim8098faa2019-03-18 10:19:51 +0900728 missing_current_api := false
729
Paul Duffind1b3a922020-01-22 11:57:20 +0000730 activeScopes := module.getActiveApiScopes()
731
Paul Duffin749f98f2019-12-30 17:23:46 +0000732 apiDir := module.getApiDir()
Paul Duffind1b3a922020-01-22 11:57:20 +0000733 for _, scope := range activeScopes {
Inseob Kim8098faa2019-03-18 10:19:51 +0900734 for _, api := range []string{"current.txt", "removed.txt"} {
Paul Duffind1b3a922020-01-22 11:57:20 +0000735 path := path.Join(mctx.ModuleDir(), apiDir, scope.apiFilePrefix+api)
Inseob Kim8098faa2019-03-18 10:19:51 +0900736 p := android.ExistentPathForSource(mctx, path)
737 if !p.Valid() {
738 mctx.ModuleErrorf("Current api file %#v doesn't exist", path)
739 missing_current_api = true
740 }
741 }
742 }
743
744 if missing_current_api {
745 script := "build/soong/scripts/gen-java-current-api-files.sh"
746 p := android.ExistentPathForSource(mctx, script)
747
748 if !p.Valid() {
749 panic(fmt.Sprintf("script file %s doesn't exist", script))
750 }
751
752 mctx.ModuleErrorf("One or more current api files are missing. "+
753 "You can update them by:\n"+
Paul Duffin37e0b772019-12-30 17:20:10 +0000754 "%s %q %s && m update-api",
Paul Duffind1b3a922020-01-22 11:57:20 +0000755 script, filepath.Join(mctx.ModuleDir(), apiDir),
756 strings.Join(activeScopes.Strings(func(s *apiScope) string { return s.apiFilePrefix }), " "))
Inseob Kim8098faa2019-03-18 10:19:51 +0900757 return
758 }
759
Paul Duffind1b3a922020-01-22 11:57:20 +0000760 for _, scope := range activeScopes {
761 module.createStubsLibrary(mctx, scope)
762 module.createStubsSources(mctx, scope)
Inseob Kimc0907f12019-02-08 21:00:45 +0900763 }
764
Paul Duffin43db9be2019-12-30 17:35:49 +0000765 if !proptools.Bool(module.sdkLibraryProperties.Api_only) {
766 // for runtime
767 module.createXmlFile(mctx)
768
769 // record java_sdk_library modules so that they are exported to make
770 javaSdkLibraries := javaSdkLibraries(mctx.Config())
771 javaSdkLibrariesLock.Lock()
772 defer javaSdkLibrariesLock.Unlock()
773 *javaSdkLibraries = append(*javaSdkLibraries, module.BaseModuleName())
774 }
Inseob Kimc0907f12019-02-08 21:00:45 +0900775}
776
777func (module *SdkLibrary) InitSdkLibraryProperties() {
Sundong Ahn054b19a2018-10-19 13:46:09 +0900778 module.AddProperties(
779 &module.sdkLibraryProperties,
780 &module.Library.Module.properties,
781 &module.Library.Module.dexpreoptProperties,
782 &module.Library.Module.deviceProperties,
783 &module.Library.Module.protoProperties,
784 )
785
786 module.Library.Module.properties.Installable = proptools.BoolPtr(true)
787 module.Library.Module.deviceProperties.IsSDKLibrary = true
Inseob Kimc0907f12019-02-08 21:00:45 +0900788}
Sundong Ahn054b19a2018-10-19 13:46:09 +0900789
Jaewoong Jung4f158ee2019-07-11 10:05:35 -0700790// java_sdk_library is a special Java library that provides optional platform APIs to apps.
791// In practice, it can be viewed as a combination of several modules: 1) stubs library that clients
792// are linked against to, 2) droiddoc module that internally generates API stubs source files,
793// 3) the real runtime shared library that implements the APIs, and 4) XML file for adding
794// the runtime lib to the classpath at runtime if requested via <uses-library>.
Inseob Kimc0907f12019-02-08 21:00:45 +0900795func SdkLibraryFactory() android.Module {
796 module := &SdkLibrary{}
797 module.InitSdkLibraryProperties()
Jooyung Han58f26ab2019-12-18 15:34:32 +0900798 android.InitApexModule(module)
Sundong Ahn054b19a2018-10-19 13:46:09 +0900799 InitJavaModule(module, android.HostAndDeviceSupported)
Colin Crossf8b860a2019-04-16 14:43:28 -0700800 android.AddLoadHook(module, func(ctx android.LoadHookContext) { module.CreateInternalModules(ctx) })
Jiyong Parkc678ad32018-04-10 13:07:10 +0900801 return module
802}
Colin Cross79c7c262019-04-17 11:11:46 -0700803
804//
805// SDK library prebuilts
806//
807
Paul Duffin56d44902020-01-31 13:36:25 +0000808// Properties associated with each api scope.
809type sdkLibraryScopeProperties struct {
Colin Cross79c7c262019-04-17 11:11:46 -0700810 Jars []string `android:"path"`
811
812 Sdk_version *string
813
Colin Cross79c7c262019-04-17 11:11:46 -0700814 // List of shared java libs that this module has dependencies to
815 Libs []string
Colin Cross79c7c262019-04-17 11:11:46 -0700816}
817
Paul Duffin56d44902020-01-31 13:36:25 +0000818type sdkLibraryImportProperties struct {
Paul Duffinfcfd7912020-01-31 17:54:30 +0000819 // List of shared java libs, common to all scopes, that this module has
820 // dependencies to
821 Libs []string
822
Paul Duffin56d44902020-01-31 13:36:25 +0000823 // Properties associated with the public api scope.
824 Public sdkLibraryScopeProperties
825
826 // Properties associated with the system api scope.
827 System sdkLibraryScopeProperties
828
829 // Properties associated with the test api scope.
830 Test sdkLibraryScopeProperties
831}
832
Colin Cross79c7c262019-04-17 11:11:46 -0700833type sdkLibraryImport struct {
834 android.ModuleBase
835 android.DefaultableModuleBase
836 prebuilt android.Prebuilt
837
838 properties sdkLibraryImportProperties
839
Paul Duffin56d44902020-01-31 13:36:25 +0000840 commonToSdkLibraryAndImport
Colin Cross79c7c262019-04-17 11:11:46 -0700841}
842
843var _ SdkLibraryDependency = (*sdkLibraryImport)(nil)
844
Jaewoong Jung4f158ee2019-07-11 10:05:35 -0700845// java_sdk_library_import imports a prebuilt java_sdk_library.
Colin Cross79c7c262019-04-17 11:11:46 -0700846func sdkLibraryImportFactory() android.Module {
847 module := &sdkLibraryImport{}
848
Paul Duffinfcfd7912020-01-31 17:54:30 +0000849 module.AddProperties(&module.properties)
Colin Cross79c7c262019-04-17 11:11:46 -0700850
Paul Duffin0bdcb272020-02-06 15:24:57 +0000851 android.InitPrebuiltModule(module, &[]string{""})
Colin Cross79c7c262019-04-17 11:11:46 -0700852 InitJavaModule(module, android.HostAndDeviceSupported)
853
854 android.AddLoadHook(module, func(mctx android.LoadHookContext) { module.createInternalModules(mctx) })
855 return module
856}
857
858func (module *sdkLibraryImport) Prebuilt() *android.Prebuilt {
859 return &module.prebuilt
860}
861
862func (module *sdkLibraryImport) Name() string {
863 return module.prebuilt.Name(module.ModuleBase.Name())
864}
865
866func (module *sdkLibraryImport) createInternalModules(mctx android.LoadHookContext) {
Colin Cross79c7c262019-04-17 11:11:46 -0700867
Paul Duffin50061512020-01-21 16:31:05 +0000868 // If the build is configured to use prebuilts then force this to be preferred.
869 if mctx.Config().UnbundledBuildUsePrebuiltSdks() {
870 module.prebuilt.ForcePrefer()
871 }
872
Paul Duffin56d44902020-01-31 13:36:25 +0000873 for apiScope, scopeProperties := range module.scopeProperties() {
874 if len(scopeProperties.Jars) == 0 {
875 continue
876 }
877
878 // Creates a java import for the jar with ".stubs" suffix
879 props := struct {
880 Name *string
881 Soc_specific *bool
882 Device_specific *bool
883 Product_specific *bool
884 System_ext_specific *bool
885 Sdk_version *string
886 Libs []string
887 Jars []string
Paul Duffin50061512020-01-21 16:31:05 +0000888 Prefer *bool
Paul Duffin56d44902020-01-31 13:36:25 +0000889 }{}
890
891 props.Name = proptools.StringPtr(apiScope.stubsModuleName(module.BaseModuleName()))
892 props.Sdk_version = scopeProperties.Sdk_version
Paul Duffinfcfd7912020-01-31 17:54:30 +0000893 // Prepend any of the libs from the legacy public properties to the libs for each of the
894 // scopes to avoid having to duplicate them in each scope.
895 props.Libs = append(module.properties.Libs, scopeProperties.Libs...)
Paul Duffin56d44902020-01-31 13:36:25 +0000896 props.Jars = scopeProperties.Jars
897
898 if module.SocSpecific() {
899 props.Soc_specific = proptools.BoolPtr(true)
900 } else if module.DeviceSpecific() {
901 props.Device_specific = proptools.BoolPtr(true)
902 } else if module.ProductSpecific() {
903 props.Product_specific = proptools.BoolPtr(true)
904 } else if module.SystemExtSpecific() {
905 props.System_ext_specific = proptools.BoolPtr(true)
906 }
907
Paul Duffin50061512020-01-21 16:31:05 +0000908 // If the build should use prebuilt sdks then set prefer to true on the stubs library.
909 // That will cause the prebuilt version of the stubs to override the source version.
910 if mctx.Config().UnbundledBuildUsePrebuiltSdks() {
911 props.Prefer = proptools.BoolPtr(true)
912 }
913
Paul Duffin56d44902020-01-31 13:36:25 +0000914 mctx.CreateModule(ImportFactory, &props)
915 }
Colin Cross79c7c262019-04-17 11:11:46 -0700916
917 javaSdkLibraries := javaSdkLibraries(mctx.Config())
918 javaSdkLibrariesLock.Lock()
919 defer javaSdkLibrariesLock.Unlock()
920 *javaSdkLibraries = append(*javaSdkLibraries, module.BaseModuleName())
921}
922
Paul Duffin56d44902020-01-31 13:36:25 +0000923func (module *sdkLibraryImport) scopeProperties() map[*apiScope]*sdkLibraryScopeProperties {
924 p := make(map[*apiScope]*sdkLibraryScopeProperties)
925 p[apiScopePublic] = &module.properties.Public
926 p[apiScopeSystem] = &module.properties.System
927 p[apiScopeTest] = &module.properties.Test
928 return p
929}
930
Colin Cross79c7c262019-04-17 11:11:46 -0700931func (module *sdkLibraryImport) DepsMutator(ctx android.BottomUpMutatorContext) {
Paul Duffin56d44902020-01-31 13:36:25 +0000932 for apiScope, scopeProperties := range module.scopeProperties() {
933 if len(scopeProperties.Jars) == 0 {
934 continue
935 }
936
937 // Add dependencies to the prebuilt stubs library
938 ctx.AddVariationDependencies(nil, apiScope.stubsTag, apiScope.stubsModuleName(module.BaseModuleName()))
939 }
Colin Cross79c7c262019-04-17 11:11:46 -0700940}
941
942func (module *sdkLibraryImport) GenerateAndroidBuildActions(ctx android.ModuleContext) {
943 // Record the paths to the prebuilt stubs library.
944 ctx.VisitDirectDeps(func(to android.Module) {
945 tag := ctx.OtherModuleDependencyTag(to)
946
Paul Duffin56d44902020-01-31 13:36:25 +0000947 if lib, ok := to.(Dependency); ok {
948 if scopeTag, ok := tag.(scopeDependencyTag); ok {
949 apiScope := scopeTag.apiScope
950 scopePaths := module.getScopePaths(apiScope)
951 scopePaths.stubsHeaderPath = lib.HeaderJars()
952 }
Colin Cross79c7c262019-04-17 11:11:46 -0700953 }
954 })
955}
956
Paul Duffin56d44902020-01-31 13:36:25 +0000957func (module *sdkLibraryImport) sdkJars(
958 ctx android.BaseModuleContext,
959 sdkVersion sdkSpec) android.Paths {
960
Paul Duffin50061512020-01-21 16:31:05 +0000961 // If a specific numeric version has been requested then use prebuilt versions of the sdk.
962 if sdkVersion.version.isNumbered() {
963 return PrebuiltJars(ctx, module.BaseModuleName(), sdkVersion)
964 }
965
Paul Duffin56d44902020-01-31 13:36:25 +0000966 var apiScope *apiScope
967 switch sdkVersion.kind {
968 case sdkSystem:
969 apiScope = apiScopeSystem
970 case sdkTest:
971 apiScope = apiScopeTest
972 default:
973 apiScope = apiScopePublic
974 }
975
976 paths := module.getScopePaths(apiScope)
977 return paths.stubsHeaderPath
978}
979
Colin Cross79c7c262019-04-17 11:11:46 -0700980// to satisfy SdkLibraryDependency interface
Jiyong Park6a927c42020-01-21 02:03:43 +0900981func (module *sdkLibraryImport) SdkHeaderJars(ctx android.BaseModuleContext, sdkVersion sdkSpec) android.Paths {
Colin Cross79c7c262019-04-17 11:11:46 -0700982 // This module is just a wrapper for the prebuilt stubs.
Paul Duffin56d44902020-01-31 13:36:25 +0000983 return module.sdkJars(ctx, sdkVersion)
Colin Cross79c7c262019-04-17 11:11:46 -0700984}
985
986// to satisfy SdkLibraryDependency interface
Jiyong Park6a927c42020-01-21 02:03:43 +0900987func (module *sdkLibraryImport) SdkImplementationJars(ctx android.BaseModuleContext, sdkVersion sdkSpec) android.Paths {
Colin Cross79c7c262019-04-17 11:11:46 -0700988 // This module is just a wrapper for the stubs.
Paul Duffin56d44902020-01-31 13:36:25 +0000989 return module.sdkJars(ctx, sdkVersion)
Colin Cross79c7c262019-04-17 11:11:46 -0700990}
Jiyong Parke3833882020-02-17 17:28:10 +0900991
992//
993// java_sdk_library_xml
994//
995type sdkLibraryXml struct {
996 android.ModuleBase
997 android.DefaultableModuleBase
998 android.ApexModuleBase
999
1000 properties sdkLibraryXmlProperties
1001
1002 outputFilePath android.OutputPath
1003 installDirPath android.InstallPath
1004}
1005
1006type sdkLibraryXmlProperties struct {
1007 // canonical name of the lib
1008 Lib_name *string
1009}
1010
1011// java_sdk_library_xml builds the permission xml file for a java_sdk_library.
1012// Not to be used directly by users. java_sdk_library internally uses this.
1013func sdkLibraryXmlFactory() android.Module {
1014 module := &sdkLibraryXml{}
1015
1016 module.AddProperties(&module.properties)
1017
1018 android.InitApexModule(module)
1019 android.InitAndroidArchModule(module, android.DeviceSupported, android.MultilibCommon)
1020
1021 return module
1022}
1023
1024// from android.PrebuiltEtcModule
1025func (module *sdkLibraryXml) SubDir() string {
1026 return "permissions"
1027}
1028
1029// from android.PrebuiltEtcModule
1030func (module *sdkLibraryXml) OutputFile() android.OutputPath {
1031 return module.outputFilePath
1032}
1033
1034// from android.ApexModule
1035func (module *sdkLibraryXml) AvailableFor(what string) bool {
1036 return true
1037}
1038
1039func (module *sdkLibraryXml) DepsMutator(ctx android.BottomUpMutatorContext) {
1040 // do nothing
1041}
1042
1043// File path to the runtime implementation library
1044func (module *sdkLibraryXml) implPath() string {
1045 implName := proptools.String(module.properties.Lib_name)
1046 if apexName := module.ApexName(); apexName != "" {
1047 // TODO(b/146468504): ApexName() is only a soong module name, not apex name.
1048 // In most cases, this works fine. But when apex_name is set or override_apex is used
1049 // this can be wrong.
1050 return fmt.Sprintf("/apex/%s/javalib/%s.jar", apexName, implName)
1051 }
1052 partition := "system"
1053 if module.SocSpecific() {
1054 partition = "vendor"
1055 } else if module.DeviceSpecific() {
1056 partition = "odm"
1057 } else if module.ProductSpecific() {
1058 partition = "product"
1059 } else if module.SystemExtSpecific() {
1060 partition = "system_ext"
1061 }
1062 return "/" + partition + "/framework/" + implName + ".jar"
1063}
1064
1065func (module *sdkLibraryXml) GenerateAndroidBuildActions(ctx android.ModuleContext) {
1066 libName := proptools.String(module.properties.Lib_name)
1067 xmlContent := fmt.Sprintf(permissionsTemplate, libName, module.implPath())
1068
1069 module.outputFilePath = android.PathForModuleOut(ctx, libName+".xml").OutputPath
1070 rule := android.NewRuleBuilder()
1071 rule.Command().
1072 Text("/bin/bash -c \"echo -e '" + xmlContent + "'\" > ").
1073 Output(module.outputFilePath)
1074
1075 rule.Build(pctx, ctx, "java_sdk_xml", "Permission XML")
1076
1077 module.installDirPath = android.PathForModuleInstall(ctx, "etc", module.SubDir())
1078}
1079
1080func (module *sdkLibraryXml) AndroidMkEntries() []android.AndroidMkEntries {
1081 if !module.IsForPlatform() {
1082 return []android.AndroidMkEntries{android.AndroidMkEntries{
1083 Disabled: true,
1084 }}
1085 }
1086
1087 return []android.AndroidMkEntries{android.AndroidMkEntries{
1088 Class: "ETC",
1089 OutputFile: android.OptionalPathForPath(module.outputFilePath),
1090 ExtraEntries: []android.AndroidMkExtraEntriesFunc{
1091 func(entries *android.AndroidMkEntries) {
1092 entries.SetString("LOCAL_MODULE_TAGS", "optional")
1093 entries.SetString("LOCAL_MODULE_PATH", module.installDirPath.ToMakePath().String())
1094 entries.SetString("LOCAL_INSTALLED_MODULE_STEM", module.outputFilePath.Base())
1095 },
1096 },
1097 }}
1098}