blob: d9c948f8b6ee3ae8d5b8ea8b7c29923f93b6a418 [file] [log] [blame]
Jiyong Parkc678ad32018-04-10 13:07:10 +09001// Copyright 2018 Google Inc. All rights reserved.
2//
3// Licensed under the Apache License, Version 2.0 (the "License");
4// you may not use this file except in compliance with the License.
5// You may obtain a copy of the License at
6//
7// http://www.apache.org/licenses/LICENSE-2.0
8//
9// Unless required by applicable law or agreed to in writing, software
10// distributed under the License is distributed on an "AS IS" BASIS,
11// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12// See the License for the specific language governing permissions and
13// limitations under the License.
14
15package java
16
17import (
Jiyong Parkc678ad32018-04-10 13:07:10 +090018 "fmt"
19 "path"
Sundong Ahn054b19a2018-10-19 13:46:09 +090020 "path/filepath"
Paul Duffin6a2bd112020-04-07 19:27:04 +010021 "reflect"
Jiyong Park82484c02018-04-23 21:41:26 +090022 "sort"
Jiyong Parkc678ad32018-04-10 13:07:10 +090023 "strings"
Jiyong Park82484c02018-04-23 21:41:26 +090024 "sync"
Jiyong Parkc678ad32018-04-10 13:07:10 +090025
Paul Duffind1b3a922020-01-22 11:57:20 +000026 "github.com/google/blueprint"
Jiyong Parkc678ad32018-04-10 13:07:10 +090027 "github.com/google/blueprint/proptools"
Paul Duffin6a2bd112020-04-07 19:27:04 +010028
29 "android/soong/android"
Jiyong Parkc678ad32018-04-10 13:07:10 +090030)
31
Jooyung Han58f26ab2019-12-18 15:34:32 +090032const (
Jiyong Parkc678ad32018-04-10 13:07:10 +090033 sdkStubsLibrarySuffix = ".stubs"
34 sdkSystemApiSuffix = ".system"
Jiyong Parkdf130542018-04-27 16:29:21 +090035 sdkTestApiSuffix = ".test"
Paul Duffin91b883d2020-02-11 13:05:28 +000036 sdkStubsSourceSuffix = ".stubs.source"
Jiyong Parkc678ad32018-04-10 13:07:10 +090037 sdkXmlFileSuffix = ".xml"
Jiyong Parke3833882020-02-17 17:28:10 +090038 permissionsTemplate = `<?xml version=\"1.0\" encoding=\"utf-8\"?>\n` +
Jooyung Han624058e2019-12-24 18:38:06 +090039 `<!-- Copyright (C) 2018 The Android Open Source Project\n` +
40 `\n` +
Jiyong Parke3833882020-02-17 17:28:10 +090041 ` Licensed under the Apache License, Version 2.0 (the \"License\");\n` +
Jooyung Han624058e2019-12-24 18:38:06 +090042 ` you may not use this file except in compliance with the License.\n` +
43 ` You may obtain a copy of the License at\n` +
44 `\n` +
45 ` http://www.apache.org/licenses/LICENSE-2.0\n` +
46 `\n` +
47 ` Unless required by applicable law or agreed to in writing, software\n` +
Jiyong Parke3833882020-02-17 17:28:10 +090048 ` distributed under the License is distributed on an \"AS IS\" BASIS,\n` +
Jooyung Han624058e2019-12-24 18:38:06 +090049 ` WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\n` +
50 ` See the License for the specific language governing permissions and\n` +
51 ` limitations under the License.\n` +
52 `-->\n` +
53 `<permissions>\n` +
Jiyong Parke3833882020-02-17 17:28:10 +090054 ` <library name=\"%s\" file=\"%s\"/>\n` +
Jooyung Han624058e2019-12-24 18:38:06 +090055 `</permissions>\n`
Jiyong Parkc678ad32018-04-10 13:07:10 +090056)
57
Paul Duffind1b3a922020-01-22 11:57:20 +000058// A tag to associated a dependency with a specific api scope.
59type scopeDependencyTag struct {
60 blueprint.BaseDependencyTag
61 name string
62 apiScope *apiScope
63}
64
65// Provides information about an api scope, e.g. public, system, test.
66type apiScope struct {
67 // The name of the api scope, e.g. public, system, test
68 name string
69
Paul Duffin6a2bd112020-04-07 19:27:04 +010070 // The name of the field in the dynamically created structure.
71 fieldName string
72
Paul Duffind1b3a922020-01-22 11:57:20 +000073 // The tag to use to depend on the stubs library module.
74 stubsTag scopeDependencyTag
75
76 // The tag to use to depend on the stubs
77 apiFileTag scopeDependencyTag
78
79 // The scope specific prefix to add to the api file base of "current.txt" or "removed.txt".
80 apiFilePrefix string
81
82 // The scope specific prefix to add to the sdk library module name to construct a scope specific
83 // module name.
84 moduleSuffix string
85
Paul Duffind1b3a922020-01-22 11:57:20 +000086 // SDK version that the stubs library is built against. Note that this is always
87 // *current. Older stubs library built with a numbered SDK version is created from
88 // the prebuilt jar.
89 sdkVersion string
Paul Duffin3c7c3472020-04-07 18:50:10 +010090
91 // Extra arguments to pass to droidstubs for this scope.
92 droidstubsArgs []string
Anton Hansson5ff28e52020-05-02 11:19:36 +010093
94 // Whether the api scope can be treated as unstable, and should skip compat checks.
95 unstable bool
Paul Duffind1b3a922020-01-22 11:57:20 +000096}
97
98// Initialize a scope, creating and adding appropriate dependency tags
99func initApiScope(scope *apiScope) *apiScope {
Paul Duffin6a2bd112020-04-07 19:27:04 +0100100 scope.fieldName = proptools.FieldNameForProperty(scope.name)
Paul Duffind1b3a922020-01-22 11:57:20 +0000101 scope.stubsTag = scopeDependencyTag{
102 name: scope.name + "-stubs",
103 apiScope: scope,
104 }
105 scope.apiFileTag = scopeDependencyTag{
106 name: scope.name + "-api",
107 apiScope: scope,
108 }
109 return scope
110}
111
112func (scope *apiScope) stubsModuleName(baseName string) string {
113 return baseName + sdkStubsLibrarySuffix + scope.moduleSuffix
114}
115
116func (scope *apiScope) docsModuleName(baseName string) string {
Paul Duffin91b883d2020-02-11 13:05:28 +0000117 return baseName + sdkStubsSourceSuffix + scope.moduleSuffix
Paul Duffind1b3a922020-01-22 11:57:20 +0000118}
119
120type apiScopes []*apiScope
121
122func (scopes apiScopes) Strings(accessor func(*apiScope) string) []string {
123 var list []string
124 for _, scope := range scopes {
125 list = append(list, accessor(scope))
126 }
127 return list
128}
129
Jiyong Parkc678ad32018-04-10 13:07:10 +0900130var (
Paul Duffind1b3a922020-01-22 11:57:20 +0000131 apiScopePublic = initApiScope(&apiScope{
132 name: "public",
133 sdkVersion: "current",
134 })
135 apiScopeSystem = initApiScope(&apiScope{
Anton Hanssone366fff2020-04-28 16:47:41 +0100136 name: "system",
137 apiFilePrefix: "system-",
138 moduleSuffix: sdkSystemApiSuffix,
139 sdkVersion: "system_current",
140 droidstubsArgs: []string{"-showAnnotation android.annotation.SystemApi"},
Paul Duffind1b3a922020-01-22 11:57:20 +0000141 })
142 apiScopeTest = initApiScope(&apiScope{
Anton Hanssone366fff2020-04-28 16:47:41 +0100143 name: "test",
144 apiFilePrefix: "test-",
145 moduleSuffix: sdkTestApiSuffix,
146 sdkVersion: "test_current",
147 droidstubsArgs: []string{"-showAnnotation android.annotation.TestApi"},
Anton Hansson5ff28e52020-05-02 11:19:36 +0100148 unstable: true,
Paul Duffind1b3a922020-01-22 11:57:20 +0000149 })
150 allApiScopes = apiScopes{
151 apiScopePublic,
152 apiScopeSystem,
153 apiScopeTest,
154 }
Jiyong Parkc678ad32018-04-10 13:07:10 +0900155)
156
Jiyong Park82484c02018-04-23 21:41:26 +0900157var (
158 javaSdkLibrariesLock sync.Mutex
159)
160
Jiyong Parkc678ad32018-04-10 13:07:10 +0900161// TODO: these are big features that are currently missing
Jiyong Park1be96912018-05-28 18:02:19 +0900162// 1) disallowing linking to the runtime shared lib
163// 2) HTML generation
Jiyong Parkc678ad32018-04-10 13:07:10 +0900164
165func init() {
Paul Duffin43dc1cc2019-12-19 11:18:54 +0000166 RegisterSdkLibraryBuildComponents(android.InitRegistrationContext)
Jiyong Parkc678ad32018-04-10 13:07:10 +0900167
Jiyong Park82484c02018-04-23 21:41:26 +0900168 android.RegisterMakeVarsProvider(pctx, func(ctx android.MakeVarsContext) {
169 javaSdkLibraries := javaSdkLibraries(ctx.Config())
170 sort.Strings(*javaSdkLibraries)
171 ctx.Strict("JAVA_SDK_LIBRARIES", strings.Join(*javaSdkLibraries, " "))
172 })
Paul Duffin61871622020-02-10 13:37:10 +0000173
174 // Register sdk member types.
175 android.RegisterSdkMemberType(&sdkLibrarySdkMemberType{
176 android.SdkMemberTypeBase{
177 PropertyName: "java_sdk_libs",
178 SupportsSdk: true,
179 },
180 })
Jiyong Parkc678ad32018-04-10 13:07:10 +0900181}
182
Paul Duffin43dc1cc2019-12-19 11:18:54 +0000183func RegisterSdkLibraryBuildComponents(ctx android.RegistrationContext) {
184 ctx.RegisterModuleType("java_sdk_library", SdkLibraryFactory)
185 ctx.RegisterModuleType("java_sdk_library_import", sdkLibraryImportFactory)
186}
187
Jiyong Parkc678ad32018-04-10 13:07:10 +0900188type sdkLibraryProperties struct {
Sundong Ahnf043cf62018-06-25 16:04:37 +0900189 // List of Java libraries that will be in the classpath when building stubs
190 Stub_only_libs []string `android:"arch_variant"`
191
Paul Duffin7a586d32019-12-30 17:09:34 +0000192 // list of package names that will be documented and publicized as API.
193 // This allows the API to be restricted to a subset of the source files provided.
194 // If this is unspecified then all the source files will be treated as being part
195 // of the API.
Jiyong Parkc678ad32018-04-10 13:07:10 +0900196 Api_packages []string
197
Jiyong Park5a2c9d72018-05-01 22:25:41 +0900198 // list of package names that must be hidden from the API
199 Hidden_api_packages []string
200
Paul Duffin749f98f2019-12-30 17:23:46 +0000201 // the relative path to the directory containing the api specification files.
202 // Defaults to "api".
203 Api_dir *string
204
Paul Duffin43db9be2019-12-30 17:35:49 +0000205 // If set to true there is no runtime library.
206 Api_only *bool
207
Paul Duffin11512472019-02-11 15:55:17 +0000208 // local files that are used within user customized droiddoc options.
209 Droiddoc_option_files []string
210
211 // additional droiddoc options
212 // Available variables for substitution:
213 //
214 // $(location <label>): the path to the droiddoc_option_files with name <label>
Sundong Ahndd567f92018-07-31 17:19:11 +0900215 Droiddoc_options []string
216
Sundong Ahn054b19a2018-10-19 13:46:09 +0900217 // a list of top-level directories containing files to merge qualifier annotations
218 // (i.e. those intended to be included in the stubs written) from.
219 Merge_annotations_dirs []string
220
221 // a list of top-level directories containing Java stub files to merge show/hide annotations from.
222 Merge_inclusion_annotations_dirs []string
223
224 // If set to true, the path of dist files is apistubs/core. Defaults to false.
225 Core_lib *bool
226
Sundong Ahn80a87b32019-05-13 15:02:50 +0900227 // don't create dist rules.
228 No_dist *bool `blueprint:"mutated"`
229
Paul Duffin37e0b772019-12-30 17:20:10 +0000230 // indicates whether system and test apis should be managed.
231 Has_system_and_test_apis bool `blueprint:"mutated"`
232
Jiyong Parkc678ad32018-04-10 13:07:10 +0900233 // TODO: determines whether to create HTML doc or not
234 //Html_doc *bool
235}
236
Paul Duffind1b3a922020-01-22 11:57:20 +0000237type scopePaths struct {
238 stubsHeaderPath android.Paths
239 stubsImplPath android.Paths
240 apiFilePath android.Path
241}
242
Paul Duffin56d44902020-01-31 13:36:25 +0000243// Common code between sdk library and sdk library import
244type commonToSdkLibraryAndImport struct {
245 scopePaths map[*apiScope]*scopePaths
246}
247
248func (c *commonToSdkLibraryAndImport) getScopePaths(scope *apiScope) *scopePaths {
249 if c.scopePaths == nil {
250 c.scopePaths = make(map[*apiScope]*scopePaths)
251 }
252 paths := c.scopePaths[scope]
253 if paths == nil {
254 paths = &scopePaths{}
255 c.scopePaths[scope] = paths
256 }
257
258 return paths
259}
260
Inseob Kimc0907f12019-02-08 21:00:45 +0900261type SdkLibrary struct {
Sundong Ahn054b19a2018-10-19 13:46:09 +0900262 Library
Jiyong Parkc678ad32018-04-10 13:07:10 +0900263
Sundong Ahn054b19a2018-10-19 13:46:09 +0900264 sdkLibraryProperties sdkLibraryProperties
Jiyong Parkc678ad32018-04-10 13:07:10 +0900265
Paul Duffin56d44902020-01-31 13:36:25 +0000266 commonToSdkLibraryAndImport
Jiyong Parkc678ad32018-04-10 13:07:10 +0900267}
268
Inseob Kimc0907f12019-02-08 21:00:45 +0900269var _ Dependency = (*SdkLibrary)(nil)
270var _ SdkLibraryDependency = (*SdkLibrary)(nil)
Colin Cross897d2ed2019-02-11 14:03:51 -0800271
Paul Duffind1b3a922020-01-22 11:57:20 +0000272func (module *SdkLibrary) getActiveApiScopes() apiScopes {
273 if module.sdkLibraryProperties.Has_system_and_test_apis {
274 return allApiScopes
275 } else {
276 return apiScopes{apiScopePublic}
277 }
278}
279
Paul Duffine74ac732020-02-06 13:51:46 +0000280var xmlPermissionsFileTag = dependencyTag{name: "xml-permissions-file"}
281
Jiyong Parke3833882020-02-17 17:28:10 +0900282func IsXmlPermissionsFileDepTag(depTag blueprint.DependencyTag) bool {
283 if dt, ok := depTag.(dependencyTag); ok {
284 return dt == xmlPermissionsFileTag
285 }
286 return false
287}
288
Inseob Kimc0907f12019-02-08 21:00:45 +0900289func (module *SdkLibrary) DepsMutator(ctx android.BottomUpMutatorContext) {
Paul Duffind1b3a922020-01-22 11:57:20 +0000290 for _, apiScope := range module.getActiveApiScopes() {
291 // Add dependencies to the stubs library
Paul Duffin50061512020-01-21 16:31:05 +0000292 ctx.AddVariationDependencies(nil, apiScope.stubsTag, module.stubsName(apiScope))
Paul Duffind1b3a922020-01-22 11:57:20 +0000293
Paul Duffin50061512020-01-21 16:31:05 +0000294 // And the api file
Paul Duffind1b3a922020-01-22 11:57:20 +0000295 ctx.AddVariationDependencies(nil, apiScope.apiFileTag, module.docsName(apiScope))
Sundong Ahn054b19a2018-10-19 13:46:09 +0900296 }
297
Paul Duffine74ac732020-02-06 13:51:46 +0000298 if !proptools.Bool(module.sdkLibraryProperties.Api_only) {
299 // Add dependency to the rule for generating the xml permissions file
Jiyong Parke3833882020-02-17 17:28:10 +0900300 ctx.AddDependency(module, xmlPermissionsFileTag, module.xmlFileName())
Paul Duffine74ac732020-02-06 13:51:46 +0000301 }
302
Sundong Ahn054b19a2018-10-19 13:46:09 +0900303 module.Library.deps(ctx)
Jiyong Parkc678ad32018-04-10 13:07:10 +0900304}
305
Inseob Kimc0907f12019-02-08 21:00:45 +0900306func (module *SdkLibrary) GenerateAndroidBuildActions(ctx android.ModuleContext) {
Paul Duffin43db9be2019-12-30 17:35:49 +0000307 // Don't build an implementation library if this is api only.
308 if !proptools.Bool(module.sdkLibraryProperties.Api_only) {
309 module.Library.GenerateAndroidBuildActions(ctx)
310 }
Sundong Ahn054b19a2018-10-19 13:46:09 +0900311
Sundong Ahn57368eb2018-07-06 11:20:23 +0900312 // Record the paths to the header jars of the library (stubs and impl).
Paul Duffind1b3a922020-01-22 11:57:20 +0000313 // When this java_sdk_library is depended upon from others via "libs" property,
Jiyong Parkc678ad32018-04-10 13:07:10 +0900314 // the recorded paths will be returned depending on the link type of the caller.
315 ctx.VisitDirectDeps(func(to android.Module) {
316 otherName := ctx.OtherModuleName(to)
317 tag := ctx.OtherModuleDependencyTag(to)
318
Sundong Ahn57368eb2018-07-06 11:20:23 +0900319 if lib, ok := to.(Dependency); ok {
Paul Duffind1b3a922020-01-22 11:57:20 +0000320 if scopeTag, ok := tag.(scopeDependencyTag); ok {
321 apiScope := scopeTag.apiScope
322 scopePaths := module.getScopePaths(apiScope)
323 scopePaths.stubsHeaderPath = lib.HeaderJars()
324 scopePaths.stubsImplPath = lib.ImplementationJars()
Jiyong Parkc678ad32018-04-10 13:07:10 +0900325 }
326 }
Sundong Ahn20e998b2018-07-24 11:19:26 +0900327 if doc, ok := to.(ApiFilePath); ok {
Paul Duffind1b3a922020-01-22 11:57:20 +0000328 if scopeTag, ok := tag.(scopeDependencyTag); ok {
329 apiScope := scopeTag.apiScope
330 scopePaths := module.getScopePaths(apiScope)
331 scopePaths.apiFilePath = doc.ApiFilePath()
332 } else {
Sundong Ahn20e998b2018-07-24 11:19:26 +0900333 ctx.ModuleErrorf("depends on module %q of unknown tag %q", otherName, tag)
334 }
335 }
Jiyong Parkc678ad32018-04-10 13:07:10 +0900336 })
337}
338
Jiyong Park0b0e1b92019-12-03 13:24:29 +0900339func (module *SdkLibrary) AndroidMkEntries() []android.AndroidMkEntries {
Paul Duffin43db9be2019-12-30 17:35:49 +0000340 if proptools.Bool(module.sdkLibraryProperties.Api_only) {
341 return nil
342 }
Jiyong Park0b0e1b92019-12-03 13:24:29 +0900343 entriesList := module.Library.AndroidMkEntries()
344 entries := &entriesList[0]
Jaewoong Jungb0c127c2019-08-29 14:56:03 -0700345 entries.Required = append(entries.Required, module.xmlFileName())
Jiyong Park0b0e1b92019-12-03 13:24:29 +0900346 return entriesList
Jiyong Park82484c02018-04-23 21:41:26 +0900347}
348
Jiyong Parkc678ad32018-04-10 13:07:10 +0900349// Module name of the stubs library
Paul Duffind1b3a922020-01-22 11:57:20 +0000350func (module *SdkLibrary) stubsName(apiScope *apiScope) string {
351 return apiScope.stubsModuleName(module.BaseModuleName())
Jiyong Parkc678ad32018-04-10 13:07:10 +0900352}
353
354// Module name of the docs
Paul Duffind1b3a922020-01-22 11:57:20 +0000355func (module *SdkLibrary) docsName(apiScope *apiScope) string {
356 return apiScope.docsModuleName(module.BaseModuleName())
Jiyong Parkc678ad32018-04-10 13:07:10 +0900357}
358
359// Module name of the runtime implementation library
Inseob Kimc0907f12019-02-08 21:00:45 +0900360func (module *SdkLibrary) implName() string {
Sundong Ahn054b19a2018-10-19 13:46:09 +0900361 return module.BaseModuleName()
Jiyong Parkc678ad32018-04-10 13:07:10 +0900362}
363
Jiyong Parkc678ad32018-04-10 13:07:10 +0900364// Module name of the XML file for the lib
Inseob Kimc0907f12019-02-08 21:00:45 +0900365func (module *SdkLibrary) xmlFileName() string {
Jiyong Parkc678ad32018-04-10 13:07:10 +0900366 return module.BaseModuleName() + sdkXmlFileSuffix
367}
368
Anton Hansson6bb88102020-03-27 19:43:19 +0000369// The dist path of the stub artifacts
370func (module *SdkLibrary) apiDistPath(apiScope *apiScope) string {
371 if module.ModuleBase.Owner() != "" {
372 return path.Join("apistubs", module.ModuleBase.Owner(), apiScope.name)
373 } else if Bool(module.sdkLibraryProperties.Core_lib) {
374 return path.Join("apistubs", "core", apiScope.name)
375 } else {
376 return path.Join("apistubs", "android", apiScope.name)
377 }
378}
379
Paul Duffin12ceb462019-12-24 20:31:31 +0000380// Get the sdk version for use when compiling the stubs library.
Paul Duffind1b3a922020-01-22 11:57:20 +0000381func (module *SdkLibrary) sdkVersionForStubsLibrary(mctx android.LoadHookContext, apiScope *apiScope) string {
Paul Duffin12ceb462019-12-24 20:31:31 +0000382 sdkDep := decodeSdkDep(mctx, sdkContext(&module.Library))
383 if sdkDep.hasStandardLibs() {
384 // If building against a standard sdk then use the sdk version appropriate for the scope.
Paul Duffind1b3a922020-01-22 11:57:20 +0000385 return apiScope.sdkVersion
Paul Duffin12ceb462019-12-24 20:31:31 +0000386 } else {
387 // Otherwise, use no system module.
388 return "none"
389 }
390}
391
Paul Duffind1b3a922020-01-22 11:57:20 +0000392func (module *SdkLibrary) latestApiFilegroupName(apiScope *apiScope) string {
393 return ":" + module.BaseModuleName() + ".api." + apiScope.name + ".latest"
Jiyong Park58c518b2018-05-12 22:29:12 +0900394}
Jiyong Parkc678ad32018-04-10 13:07:10 +0900395
Paul Duffind1b3a922020-01-22 11:57:20 +0000396func (module *SdkLibrary) latestRemovedApiFilegroupName(apiScope *apiScope) string {
397 return ":" + module.BaseModuleName() + "-removed.api." + apiScope.name + ".latest"
Jiyong Parkc678ad32018-04-10 13:07:10 +0900398}
399
400// Creates a static java library that has API stubs
Paul Duffind1b3a922020-01-22 11:57:20 +0000401func (module *SdkLibrary) createStubsLibrary(mctx android.LoadHookContext, apiScope *apiScope) {
Jiyong Parkc678ad32018-04-10 13:07:10 +0900402 props := struct {
Sundong Ahn0d7dff42019-12-04 12:53:44 +0900403 Name *string
404 Srcs []string
Paul Duffin367ab912019-12-23 19:40:36 +0000405 Installable *bool
Sundong Ahn0d7dff42019-12-04 12:53:44 +0900406 Sdk_version *string
Paul Duffin12ceb462019-12-24 20:31:31 +0000407 System_modules *string
Paul Duffinab8da5d2020-02-07 16:12:04 +0000408 Patch_module *string
Sundong Ahn0d7dff42019-12-04 12:53:44 +0900409 Libs []string
410 Soc_specific *bool
411 Device_specific *bool
412 Product_specific *bool
413 System_ext_specific *bool
414 Compile_dex *bool
Sundong Ahn0d7dff42019-12-04 12:53:44 +0900415 Java_version *string
416 Product_variables struct {
Jiyong Park82484c02018-04-23 21:41:26 +0900417 Pdk struct {
418 Enabled *bool
419 }
Jiyong Parkc678ad32018-04-10 13:07:10 +0900420 }
Sundong Ahn054b19a2018-10-19 13:46:09 +0900421 Openjdk9 struct {
422 Srcs []string
423 Javacflags []string
424 }
Anton Hansson6bb88102020-03-27 19:43:19 +0000425 Dist struct {
426 Targets []string
427 Dest *string
428 Dir *string
429 Tag *string
430 }
Jiyong Parkc678ad32018-04-10 13:07:10 +0900431 }{}
432
Jiyong Parkdf130542018-04-27 16:29:21 +0900433 props.Name = proptools.StringPtr(module.stubsName(apiScope))
Jiyong Parkc678ad32018-04-10 13:07:10 +0900434 // sources are generated from the droiddoc
Jiyong Parkdf130542018-04-27 16:29:21 +0900435 props.Srcs = []string{":" + module.docsName(apiScope)}
Paul Duffin12ceb462019-12-24 20:31:31 +0000436 sdkVersion := module.sdkVersionForStubsLibrary(mctx, apiScope)
Paul Duffin52d398a2019-06-11 12:31:14 +0100437 props.Sdk_version = proptools.StringPtr(sdkVersion)
Paul Duffin12ceb462019-12-24 20:31:31 +0000438 props.System_modules = module.Library.Module.deviceProperties.System_modules
Paul Duffinab8da5d2020-02-07 16:12:04 +0000439 props.Patch_module = module.Library.Module.properties.Patch_module
Paul Duffin367ab912019-12-23 19:40:36 +0000440 props.Installable = proptools.BoolPtr(false)
Sundong Ahn054b19a2018-10-19 13:46:09 +0900441 props.Libs = module.sdkLibraryProperties.Stub_only_libs
Jiyong Park82484c02018-04-23 21:41:26 +0900442 props.Product_variables.Pdk.Enabled = proptools.BoolPtr(false)
Sundong Ahn054b19a2018-10-19 13:46:09 +0900443 props.Openjdk9.Srcs = module.Library.Module.properties.Openjdk9.Srcs
444 props.Openjdk9.Javacflags = module.Library.Module.properties.Openjdk9.Javacflags
445 props.Java_version = module.Library.Module.properties.Java_version
446 if module.Library.Module.deviceProperties.Compile_dex != nil {
447 props.Compile_dex = module.Library.Module.deviceProperties.Compile_dex
Sundong Ahndd567f92018-07-31 17:19:11 +0900448 }
Jiyong Parkc678ad32018-04-10 13:07:10 +0900449
450 if module.SocSpecific() {
451 props.Soc_specific = proptools.BoolPtr(true)
452 } else if module.DeviceSpecific() {
453 props.Device_specific = proptools.BoolPtr(true)
454 } else if module.ProductSpecific() {
455 props.Product_specific = proptools.BoolPtr(true)
Sundong Ahn0d7dff42019-12-04 12:53:44 +0900456 } else if module.SystemExtSpecific() {
457 props.System_ext_specific = proptools.BoolPtr(true)
Jiyong Parkc678ad32018-04-10 13:07:10 +0900458 }
Anton Hansson6bb88102020-03-27 19:43:19 +0000459 // Dist the class jar artifact for sdk builds.
460 if !Bool(module.sdkLibraryProperties.No_dist) {
461 props.Dist.Targets = []string{"sdk", "win_sdk"}
462 props.Dist.Dest = proptools.StringPtr(fmt.Sprintf("%v.jar", module.BaseModuleName()))
463 props.Dist.Dir = proptools.StringPtr(module.apiDistPath(apiScope))
464 props.Dist.Tag = proptools.StringPtr(".jar")
465 }
Jiyong Parkc678ad32018-04-10 13:07:10 +0900466
Colin Cross84dfc3d2019-09-25 11:33:01 -0700467 mctx.CreateModule(LibraryFactory, &props)
Jiyong Parkc678ad32018-04-10 13:07:10 +0900468}
469
Paul Duffincbcfcaa2020-04-07 18:49:53 +0100470// Creates a droidstubs module that creates stubs source files from the given full source
Jiyong Parkc678ad32018-04-10 13:07:10 +0900471// files
Paul Duffind1b3a922020-01-22 11:57:20 +0000472func (module *SdkLibrary) createStubsSources(mctx android.LoadHookContext, apiScope *apiScope) {
Jiyong Parkc678ad32018-04-10 13:07:10 +0900473 props := struct {
Sundong Ahn054b19a2018-10-19 13:46:09 +0900474 Name *string
475 Srcs []string
476 Installable *bool
Paul Duffin52d398a2019-06-11 12:31:14 +0100477 Sdk_version *string
Paul Duffin12ceb462019-12-24 20:31:31 +0000478 System_modules *string
Sundong Ahn054b19a2018-10-19 13:46:09 +0900479 Libs []string
Paul Duffin11512472019-02-11 15:55:17 +0000480 Arg_files []string
Sundong Ahn054b19a2018-10-19 13:46:09 +0900481 Args *string
Sundong Ahn054b19a2018-10-19 13:46:09 +0900482 Java_version *string
483 Merge_annotations_dirs []string
484 Merge_inclusion_annotations_dirs []string
485 Check_api struct {
Inseob Kim38449af2019-02-28 14:24:05 +0900486 Current ApiToCheck
487 Last_released ApiToCheck
488 Ignore_missing_latest_api *bool
Jiyong Park58c518b2018-05-12 22:29:12 +0900489 }
Sundong Ahn1b92c822018-05-29 11:35:17 +0900490 Aidl struct {
491 Include_dirs []string
492 Local_include_dirs []string
493 }
Anton Hansson6bb88102020-03-27 19:43:19 +0000494 Dist struct {
495 Targets []string
496 Dest *string
497 Dir *string
498 }
Jiyong Parkc678ad32018-04-10 13:07:10 +0900499 }{}
500
Paul Duffinda364252020-04-28 14:08:32 +0100501 // The stubs source processing uses the same compile time classpath when extracting the
502 // API from the implementation library as it does when compiling it. i.e. the same
503 // * sdk version
504 // * system_modules
505 // * libs (static_libs/libs)
Paul Duffin250e6192019-06-07 10:44:37 +0100506
Jiyong Parkdf130542018-04-27 16:29:21 +0900507 props.Name = proptools.StringPtr(module.docsName(apiScope))
Sundong Ahn054b19a2018-10-19 13:46:09 +0900508 props.Srcs = append(props.Srcs, module.Library.Module.properties.Srcs...)
Paul Duffinda364252020-04-28 14:08:32 +0100509 props.Sdk_version = module.Library.Module.deviceProperties.Sdk_version
Paul Duffin12ceb462019-12-24 20:31:31 +0000510 props.System_modules = module.Library.Module.deviceProperties.System_modules
Jiyong Parkc678ad32018-04-10 13:07:10 +0900511 props.Installable = proptools.BoolPtr(false)
Sundong Ahne6f0b052018-06-05 16:46:14 +0900512 // A droiddoc module has only one Libs property and doesn't distinguish between
513 // shared libs and static libs. So we need to add both of these libs to Libs property.
Sundong Ahn054b19a2018-10-19 13:46:09 +0900514 props.Libs = module.Library.Module.properties.Libs
515 props.Libs = append(props.Libs, module.Library.Module.properties.Static_libs...)
516 props.Aidl.Include_dirs = module.Library.Module.deviceProperties.Aidl.Include_dirs
517 props.Aidl.Local_include_dirs = module.Library.Module.deviceProperties.Aidl.Local_include_dirs
Sundong Ahn054b19a2018-10-19 13:46:09 +0900518 props.Java_version = module.Library.Module.properties.Java_version
Jiyong Parkc678ad32018-04-10 13:07:10 +0900519
Sundong Ahn054b19a2018-10-19 13:46:09 +0900520 props.Merge_annotations_dirs = module.sdkLibraryProperties.Merge_annotations_dirs
521 props.Merge_inclusion_annotations_dirs = module.sdkLibraryProperties.Merge_inclusion_annotations_dirs
522
Paul Duffincbcfcaa2020-04-07 18:49:53 +0100523 droidstubsArgs := []string{}
Paul Duffin235ffff2019-12-24 10:41:30 +0000524 if len(module.sdkLibraryProperties.Api_packages) != 0 {
Paul Duffincbcfcaa2020-04-07 18:49:53 +0100525 droidstubsArgs = append(droidstubsArgs, "--stub-packages "+strings.Join(module.sdkLibraryProperties.Api_packages, ":"))
Paul Duffin235ffff2019-12-24 10:41:30 +0000526 }
527 if len(module.sdkLibraryProperties.Hidden_api_packages) != 0 {
Paul Duffincbcfcaa2020-04-07 18:49:53 +0100528 droidstubsArgs = append(droidstubsArgs,
Paul Duffin235ffff2019-12-24 10:41:30 +0000529 android.JoinWithPrefix(module.sdkLibraryProperties.Hidden_api_packages, " --hide-package "))
530 }
Paul Duffincbcfcaa2020-04-07 18:49:53 +0100531 droidstubsArgs = append(droidstubsArgs, module.sdkLibraryProperties.Droiddoc_options...)
Paul Duffin235ffff2019-12-24 10:41:30 +0000532 disabledWarnings := []string{
533 "MissingPermission",
534 "BroadcastBehavior",
535 "HiddenSuperclass",
536 "DeprecationMismatch",
537 "UnavailableSymbol",
538 "SdkConstant",
539 "HiddenTypeParameter",
540 "Todo",
541 "Typo",
542 }
Paul Duffincbcfcaa2020-04-07 18:49:53 +0100543 droidstubsArgs = append(droidstubsArgs, android.JoinWithPrefix(disabledWarnings, "--hide "))
Sundong Ahnfb2721f2018-09-17 13:23:09 +0900544
Paul Duffin3c7c3472020-04-07 18:50:10 +0100545 // Add in scope specific arguments.
546 droidstubsArgs = append(droidstubsArgs, apiScope.droidstubsArgs...)
Paul Duffin11512472019-02-11 15:55:17 +0000547 props.Arg_files = module.sdkLibraryProperties.Droiddoc_option_files
Paul Duffincbcfcaa2020-04-07 18:49:53 +0100548 props.Args = proptools.StringPtr(strings.Join(droidstubsArgs, " "))
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 Parkc678ad32018-04-10 13:07:10 +0900558
Jiyong Park58c518b2018-05-12 22:29:12 +0900559 // check against the not-yet-release API
560 props.Check_api.Current.Api_file = proptools.StringPtr(currentApiFileName)
561 props.Check_api.Current.Removed_api_file = proptools.StringPtr(removedApiFileName)
Jiyong Park58c518b2018-05-12 22:29:12 +0900562
Anton Hansson5ff28e52020-05-02 11:19:36 +0100563 if !apiScope.unstable {
564 // check against the latest released API
565 props.Check_api.Last_released.Api_file = proptools.StringPtr(
566 module.latestApiFilegroupName(apiScope))
567 props.Check_api.Last_released.Removed_api_file = proptools.StringPtr(
568 module.latestRemovedApiFilegroupName(apiScope))
569 props.Check_api.Ignore_missing_latest_api = proptools.BoolPtr(true)
570 }
Jiyong Park58c518b2018-05-12 22:29:12 +0900571
Anton Hansson6bb88102020-03-27 19:43:19 +0000572 // Dist the api txt artifact for sdk builds.
573 if !Bool(module.sdkLibraryProperties.No_dist) {
574 props.Dist.Targets = []string{"sdk", "win_sdk"}
575 props.Dist.Dest = proptools.StringPtr(fmt.Sprintf("%v.txt", module.BaseModuleName()))
576 props.Dist.Dir = proptools.StringPtr(path.Join(module.apiDistPath(apiScope), "api"))
577 }
578
Colin Cross84dfc3d2019-09-25 11:33:01 -0700579 mctx.CreateModule(DroidstubsFactory, &props)
Jiyong Parkc678ad32018-04-10 13:07:10 +0900580}
581
Jooyung Hanb8fa86a2020-03-10 06:23:13 +0900582func (module *SdkLibrary) DepIsInSameApex(mctx android.BaseModuleContext, dep android.Module) bool {
583 depTag := mctx.OtherModuleDependencyTag(dep)
584 if depTag == xmlPermissionsFileTag {
585 return true
586 }
587 return module.Library.DepIsInSameApex(mctx, dep)
588}
589
Jiyong Parkc678ad32018-04-10 13:07:10 +0900590// Creates the xml file that publicizes the runtime library
Colin Crossf8b860a2019-04-16 14:43:28 -0700591func (module *SdkLibrary) createXmlFile(mctx android.LoadHookContext) {
Jiyong Parke3833882020-02-17 17:28:10 +0900592 props := struct {
Sundong Ahn0d7dff42019-12-04 12:53:44 +0900593 Name *string
Jiyong Parke3833882020-02-17 17:28:10 +0900594 Lib_name *string
Sundong Ahn0d7dff42019-12-04 12:53:44 +0900595 Soc_specific *bool
596 Device_specific *bool
597 Product_specific *bool
598 System_ext_specific *bool
Jooyung Hanb8fa86a2020-03-10 06:23:13 +0900599 Apex_available []string
Jiyong Parke3833882020-02-17 17:28:10 +0900600 }{
Jooyung Hanb8fa86a2020-03-10 06:23:13 +0900601 Name: proptools.StringPtr(module.xmlFileName()),
602 Lib_name: proptools.StringPtr(module.BaseModuleName()),
603 Apex_available: module.ApexProperties.Apex_available,
Jiyong Parkc678ad32018-04-10 13:07:10 +0900604 }
Jiyong Parke3833882020-02-17 17:28:10 +0900605
606 if module.SocSpecific() {
607 props.Soc_specific = proptools.BoolPtr(true)
608 } else if module.DeviceSpecific() {
609 props.Device_specific = proptools.BoolPtr(true)
610 } else if module.ProductSpecific() {
611 props.Product_specific = proptools.BoolPtr(true)
612 } else if module.SystemExtSpecific() {
613 props.System_ext_specific = proptools.BoolPtr(true)
614 }
615
616 mctx.CreateModule(sdkLibraryXmlFactory, &props)
Jiyong Parkc678ad32018-04-10 13:07:10 +0900617}
618
Paul Duffin50061512020-01-21 16:31:05 +0000619func PrebuiltJars(ctx android.BaseModuleContext, baseName string, s sdkSpec) android.Paths {
Jiyong Park6a927c42020-01-21 02:03:43 +0900620 var ver sdkVersion
621 var kind sdkKind
622 if s.usePrebuilt(ctx) {
623 ver = s.version
624 kind = s.kind
Jiyong Parkc678ad32018-04-10 13:07:10 +0900625 } else {
Jiyong Park6a927c42020-01-21 02:03:43 +0900626 // We don't have prebuilt SDK for the specific sdkVersion.
627 // Instead of breaking the build, fallback to use "system_current"
628 ver = sdkVersionCurrent
629 kind = sdkSystem
Sundong Ahn054b19a2018-10-19 13:46:09 +0900630 }
Jiyong Park6a927c42020-01-21 02:03:43 +0900631
632 dir := filepath.Join("prebuilts", "sdk", ver.String(), kind.String())
Paul Duffin50061512020-01-21 16:31:05 +0000633 jar := filepath.Join(dir, baseName+".jar")
Sundong Ahn054b19a2018-10-19 13:46:09 +0900634 jarPath := android.ExistentPathForSource(ctx, jar)
Sundong Ahnae418ac2019-02-28 15:01:28 +0900635 if !jarPath.Valid() {
Colin Cross07c88562020-01-07 09:34:44 -0800636 if ctx.Config().AllowMissingDependencies() {
637 return android.Paths{android.PathForSource(ctx, jar)}
638 } else {
Jiyong Park6a927c42020-01-21 02:03:43 +0900639 ctx.PropertyErrorf("sdk_library", "invalid sdk version %q, %q does not exist", s.raw, jar)
Colin Cross07c88562020-01-07 09:34:44 -0800640 }
Sundong Ahnae418ac2019-02-28 15:01:28 +0900641 return nil
642 }
Sundong Ahn054b19a2018-10-19 13:46:09 +0900643 return android.Paths{jarPath.Path()}
644}
645
Paul Duffind1b3a922020-01-22 11:57:20 +0000646func (module *SdkLibrary) sdkJars(
647 ctx android.BaseModuleContext,
648 sdkVersion sdkSpec,
649 headerJars bool) android.Paths {
650
Paul Duffin50061512020-01-21 16:31:05 +0000651 // If a specific numeric version has been requested then use prebuilt versions of the sdk.
652 if sdkVersion.version.isNumbered() {
653 return PrebuiltJars(ctx, module.BaseModuleName(), sdkVersion)
Sundong Ahn054b19a2018-10-19 13:46:09 +0900654 } else {
Paul Duffind1b3a922020-01-22 11:57:20 +0000655 if !sdkVersion.specified() {
656 if headerJars {
657 return module.Library.HeaderJars()
658 } else {
659 return module.Library.ImplementationJars()
660 }
661 }
Paul Duffin726d23c2020-01-22 16:30:37 +0000662 var apiScope *apiScope
Jiyong Park6a927c42020-01-21 02:03:43 +0900663 switch sdkVersion.kind {
664 case sdkSystem:
Paul Duffin726d23c2020-01-22 16:30:37 +0000665 apiScope = apiScopeSystem
666 case sdkTest:
667 apiScope = apiScopeTest
Jiyong Park6a927c42020-01-21 02:03:43 +0900668 case sdkPrivate:
Sundong Ahn054b19a2018-10-19 13:46:09 +0900669 return module.Library.HeaderJars()
Jiyong Park6a927c42020-01-21 02:03:43 +0900670 default:
Paul Duffin726d23c2020-01-22 16:30:37 +0000671 apiScope = apiScopePublic
Paul Duffind1b3a922020-01-22 11:57:20 +0000672 }
673
Paul Duffin726d23c2020-01-22 16:30:37 +0000674 paths := module.getScopePaths(apiScope)
Paul Duffind1b3a922020-01-22 11:57:20 +0000675 if headerJars {
676 return paths.stubsHeaderPath
677 } else {
678 return paths.stubsImplPath
Sundong Ahn054b19a2018-10-19 13:46:09 +0900679 }
Jiyong Parkc678ad32018-04-10 13:07:10 +0900680 }
681}
682
Sundong Ahn241cd372018-07-13 16:16:44 +0900683// to satisfy SdkLibraryDependency interface
Paul Duffind1b3a922020-01-22 11:57:20 +0000684func (module *SdkLibrary) SdkHeaderJars(ctx android.BaseModuleContext, sdkVersion sdkSpec) android.Paths {
685 return module.sdkJars(ctx, sdkVersion, true /*headerJars*/)
686}
687
688// to satisfy SdkLibraryDependency interface
Jiyong Park6a927c42020-01-21 02:03:43 +0900689func (module *SdkLibrary) SdkImplementationJars(ctx android.BaseModuleContext, sdkVersion sdkSpec) android.Paths {
Paul Duffind1b3a922020-01-22 11:57:20 +0000690 return module.sdkJars(ctx, sdkVersion, false /*headerJars*/)
Sundong Ahn241cd372018-07-13 16:16:44 +0900691}
692
Sundong Ahn80a87b32019-05-13 15:02:50 +0900693func (module *SdkLibrary) SetNoDist() {
694 module.sdkLibraryProperties.No_dist = proptools.BoolPtr(true)
695}
696
Colin Cross571cccf2019-02-04 11:22:08 -0800697var javaSdkLibrariesKey = android.NewOnceKey("javaSdkLibraries")
698
Jiyong Park82484c02018-04-23 21:41:26 +0900699func javaSdkLibraries(config android.Config) *[]string {
Colin Cross571cccf2019-02-04 11:22:08 -0800700 return config.Once(javaSdkLibrariesKey, func() interface{} {
Jiyong Park82484c02018-04-23 21:41:26 +0900701 return &[]string{}
702 }).(*[]string)
703}
704
Paul Duffin749f98f2019-12-30 17:23:46 +0000705func (module *SdkLibrary) getApiDir() string {
706 return proptools.StringDefault(module.sdkLibraryProperties.Api_dir, "api")
707}
708
Jiyong Parkc678ad32018-04-10 13:07:10 +0900709// For a java_sdk_library module, create internal modules for stubs, docs,
710// runtime libs and xml file. If requested, the stubs and docs are created twice
711// once for public API level and once for system API level
Colin Crossf8b860a2019-04-16 14:43:28 -0700712func (module *SdkLibrary) CreateInternalModules(mctx android.LoadHookContext) {
Inseob Kim6e93ac92019-03-21 17:43:49 +0900713 if len(module.Library.Module.properties.Srcs) == 0 {
Inseob Kimc0907f12019-02-08 21:00:45 +0900714 mctx.PropertyErrorf("srcs", "java_sdk_library must specify srcs")
Jooyung Han58f26ab2019-12-18 15:34:32 +0900715 return
Inseob Kimc0907f12019-02-08 21:00:45 +0900716 }
717
Paul Duffin37e0b772019-12-30 17:20:10 +0000718 // If this builds against standard libraries (i.e. is not part of the core libraries)
719 // then assume it provides both system and test apis. Otherwise, assume it does not and
720 // also assume it does not contribute to the dist build.
721 sdkDep := decodeSdkDep(mctx, sdkContext(&module.Library))
722 hasSystemAndTestApis := sdkDep.hasStandardLibs()
723 module.sdkLibraryProperties.Has_system_and_test_apis = hasSystemAndTestApis
724 module.sdkLibraryProperties.No_dist = proptools.BoolPtr(!hasSystemAndTestApis)
725
Inseob Kim8098faa2019-03-18 10:19:51 +0900726 missing_current_api := false
727
Paul Duffind1b3a922020-01-22 11:57:20 +0000728 activeScopes := module.getActiveApiScopes()
729
Paul Duffin749f98f2019-12-30 17:23:46 +0000730 apiDir := module.getApiDir()
Paul Duffind1b3a922020-01-22 11:57:20 +0000731 for _, scope := range activeScopes {
Inseob Kim8098faa2019-03-18 10:19:51 +0900732 for _, api := range []string{"current.txt", "removed.txt"} {
Paul Duffind1b3a922020-01-22 11:57:20 +0000733 path := path.Join(mctx.ModuleDir(), apiDir, scope.apiFilePrefix+api)
Inseob Kim8098faa2019-03-18 10:19:51 +0900734 p := android.ExistentPathForSource(mctx, path)
735 if !p.Valid() {
736 mctx.ModuleErrorf("Current api file %#v doesn't exist", path)
737 missing_current_api = true
738 }
739 }
740 }
741
742 if missing_current_api {
743 script := "build/soong/scripts/gen-java-current-api-files.sh"
744 p := android.ExistentPathForSource(mctx, script)
745
746 if !p.Valid() {
747 panic(fmt.Sprintf("script file %s doesn't exist", script))
748 }
749
750 mctx.ModuleErrorf("One or more current api files are missing. "+
751 "You can update them by:\n"+
Paul Duffin37e0b772019-12-30 17:20:10 +0000752 "%s %q %s && m update-api",
Paul Duffind1b3a922020-01-22 11:57:20 +0000753 script, filepath.Join(mctx.ModuleDir(), apiDir),
754 strings.Join(activeScopes.Strings(func(s *apiScope) string { return s.apiFilePrefix }), " "))
Inseob Kim8098faa2019-03-18 10:19:51 +0900755 return
756 }
757
Paul Duffind1b3a922020-01-22 11:57:20 +0000758 for _, scope := range activeScopes {
759 module.createStubsLibrary(mctx, scope)
760 module.createStubsSources(mctx, scope)
Inseob Kimc0907f12019-02-08 21:00:45 +0900761 }
762
Paul Duffin43db9be2019-12-30 17:35:49 +0000763 if !proptools.Bool(module.sdkLibraryProperties.Api_only) {
764 // for runtime
765 module.createXmlFile(mctx)
766
767 // record java_sdk_library modules so that they are exported to make
768 javaSdkLibraries := javaSdkLibraries(mctx.Config())
769 javaSdkLibrariesLock.Lock()
770 defer javaSdkLibrariesLock.Unlock()
771 *javaSdkLibraries = append(*javaSdkLibraries, module.BaseModuleName())
772 }
Inseob Kimc0907f12019-02-08 21:00:45 +0900773}
774
775func (module *SdkLibrary) InitSdkLibraryProperties() {
Sundong Ahn054b19a2018-10-19 13:46:09 +0900776 module.AddProperties(
777 &module.sdkLibraryProperties,
778 &module.Library.Module.properties,
779 &module.Library.Module.dexpreoptProperties,
780 &module.Library.Module.deviceProperties,
781 &module.Library.Module.protoProperties,
782 )
783
784 module.Library.Module.properties.Installable = proptools.BoolPtr(true)
785 module.Library.Module.deviceProperties.IsSDKLibrary = true
Inseob Kimc0907f12019-02-08 21:00:45 +0900786}
Sundong Ahn054b19a2018-10-19 13:46:09 +0900787
Jaewoong Jung4f158ee2019-07-11 10:05:35 -0700788// java_sdk_library is a special Java library that provides optional platform APIs to apps.
789// In practice, it can be viewed as a combination of several modules: 1) stubs library that clients
790// are linked against to, 2) droiddoc module that internally generates API stubs source files,
791// 3) the real runtime shared library that implements the APIs, and 4) XML file for adding
792// the runtime lib to the classpath at runtime if requested via <uses-library>.
Inseob Kimc0907f12019-02-08 21:00:45 +0900793func SdkLibraryFactory() android.Module {
794 module := &SdkLibrary{}
795 module.InitSdkLibraryProperties()
Jooyung Han58f26ab2019-12-18 15:34:32 +0900796 android.InitApexModule(module)
Sundong Ahn054b19a2018-10-19 13:46:09 +0900797 InitJavaModule(module, android.HostAndDeviceSupported)
Colin Crossf8b860a2019-04-16 14:43:28 -0700798 android.AddLoadHook(module, func(ctx android.LoadHookContext) { module.CreateInternalModules(ctx) })
Jiyong Parkc678ad32018-04-10 13:07:10 +0900799 return module
800}
Colin Cross79c7c262019-04-17 11:11:46 -0700801
802//
803// SDK library prebuilts
804//
805
Paul Duffin56d44902020-01-31 13:36:25 +0000806// Properties associated with each api scope.
807type sdkLibraryScopeProperties struct {
Colin Cross79c7c262019-04-17 11:11:46 -0700808 Jars []string `android:"path"`
809
810 Sdk_version *string
811
Colin Cross79c7c262019-04-17 11:11:46 -0700812 // List of shared java libs that this module has dependencies to
813 Libs []string
Colin Cross79c7c262019-04-17 11:11:46 -0700814}
815
Paul Duffin56d44902020-01-31 13:36:25 +0000816type sdkLibraryImportProperties struct {
Paul Duffinfcfd7912020-01-31 17:54:30 +0000817 // List of shared java libs, common to all scopes, that this module has
818 // dependencies to
819 Libs []string
Paul Duffin56d44902020-01-31 13:36:25 +0000820}
821
Colin Cross79c7c262019-04-17 11:11:46 -0700822type sdkLibraryImport struct {
823 android.ModuleBase
824 android.DefaultableModuleBase
825 prebuilt android.Prebuilt
Paul Duffin61871622020-02-10 13:37:10 +0000826 android.ApexModuleBase
827 android.SdkBase
Colin Cross79c7c262019-04-17 11:11:46 -0700828
829 properties sdkLibraryImportProperties
830
Paul Duffin6a2bd112020-04-07 19:27:04 +0100831 // Map from api scope to the scope specific property structure.
832 scopeProperties map[*apiScope]*sdkLibraryScopeProperties
833
Paul Duffin56d44902020-01-31 13:36:25 +0000834 commonToSdkLibraryAndImport
Colin Cross79c7c262019-04-17 11:11:46 -0700835}
836
837var _ SdkLibraryDependency = (*sdkLibraryImport)(nil)
838
Paul Duffin6a2bd112020-04-07 19:27:04 +0100839// The type of a structure that contains a field of type sdkLibraryScopeProperties
840// for each apiscope in allApiScopes, e.g. something like:
841// struct {
842// Public sdkLibraryScopeProperties
843// System sdkLibraryScopeProperties
844// ...
845// }
846var allScopeStructType = createAllScopePropertiesStructType()
847
848// Dynamically create a structure type for each apiscope in allApiScopes.
849func createAllScopePropertiesStructType() reflect.Type {
850 var fields []reflect.StructField
851 for _, apiScope := range allApiScopes {
852 field := reflect.StructField{
853 Name: apiScope.fieldName,
854 Type: reflect.TypeOf(sdkLibraryScopeProperties{}),
855 }
856 fields = append(fields, field)
857 }
858
859 return reflect.StructOf(fields)
860}
861
862// Create an instance of the scope specific structure type and return a map
863// from apiscope to a pointer to each scope specific field.
864func createPropertiesInstance() (interface{}, map[*apiScope]*sdkLibraryScopeProperties) {
865 allScopePropertiesPtr := reflect.New(allScopeStructType)
866 allScopePropertiesStruct := allScopePropertiesPtr.Elem()
867 scopeProperties := make(map[*apiScope]*sdkLibraryScopeProperties)
868
869 for _, apiScope := range allApiScopes {
870 field := allScopePropertiesStruct.FieldByName(apiScope.fieldName)
871 scopeProperties[apiScope] = field.Addr().Interface().(*sdkLibraryScopeProperties)
872 }
873
874 return allScopePropertiesPtr.Interface(), scopeProperties
875}
876
Jaewoong Jung4f158ee2019-07-11 10:05:35 -0700877// java_sdk_library_import imports a prebuilt java_sdk_library.
Colin Cross79c7c262019-04-17 11:11:46 -0700878func sdkLibraryImportFactory() android.Module {
879 module := &sdkLibraryImport{}
880
Paul Duffin6a2bd112020-04-07 19:27:04 +0100881 allScopeProperties, scopeToProperties := createPropertiesInstance()
882 module.scopeProperties = scopeToProperties
883 module.AddProperties(&module.properties, allScopeProperties)
Colin Cross79c7c262019-04-17 11:11:46 -0700884
Paul Duffin0bdcb272020-02-06 15:24:57 +0000885 android.InitPrebuiltModule(module, &[]string{""})
Paul Duffin61871622020-02-10 13:37:10 +0000886 android.InitApexModule(module)
887 android.InitSdkAwareModule(module)
Colin Cross79c7c262019-04-17 11:11:46 -0700888 InitJavaModule(module, android.HostAndDeviceSupported)
889
890 android.AddLoadHook(module, func(mctx android.LoadHookContext) { module.createInternalModules(mctx) })
891 return module
892}
893
894func (module *sdkLibraryImport) Prebuilt() *android.Prebuilt {
895 return &module.prebuilt
896}
897
898func (module *sdkLibraryImport) Name() string {
899 return module.prebuilt.Name(module.ModuleBase.Name())
900}
901
902func (module *sdkLibraryImport) createInternalModules(mctx android.LoadHookContext) {
Colin Cross79c7c262019-04-17 11:11:46 -0700903
Paul Duffin50061512020-01-21 16:31:05 +0000904 // If the build is configured to use prebuilts then force this to be preferred.
905 if mctx.Config().UnbundledBuildUsePrebuiltSdks() {
906 module.prebuilt.ForcePrefer()
907 }
908
Paul Duffin6a2bd112020-04-07 19:27:04 +0100909 for apiScope, scopeProperties := range module.scopeProperties {
Paul Duffin56d44902020-01-31 13:36:25 +0000910 if len(scopeProperties.Jars) == 0 {
911 continue
912 }
913
Paul Duffinf6155722020-04-09 00:07:11 +0100914 module.createJavaImportForStubs(mctx, apiScope, scopeProperties)
Paul Duffin56d44902020-01-31 13:36:25 +0000915 }
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 Duffinf6155722020-04-09 00:07:11 +0100923func (module *sdkLibraryImport) createJavaImportForStubs(mctx android.LoadHookContext, apiScope *apiScope, scopeProperties *sdkLibraryScopeProperties) {
924 // Creates a java import for the jar with ".stubs" suffix
925 props := struct {
926 Name *string
927 Soc_specific *bool
928 Device_specific *bool
929 Product_specific *bool
930 System_ext_specific *bool
931 Sdk_version *string
932 Libs []string
933 Jars []string
934 Prefer *bool
935 }{}
936 props.Name = proptools.StringPtr(apiScope.stubsModuleName(module.BaseModuleName()))
937 props.Sdk_version = scopeProperties.Sdk_version
938 // Prepend any of the libs from the legacy public properties to the libs for each of the
939 // scopes to avoid having to duplicate them in each scope.
940 props.Libs = append(module.properties.Libs, scopeProperties.Libs...)
941 props.Jars = scopeProperties.Jars
942 if module.SocSpecific() {
943 props.Soc_specific = proptools.BoolPtr(true)
944 } else if module.DeviceSpecific() {
945 props.Device_specific = proptools.BoolPtr(true)
946 } else if module.ProductSpecific() {
947 props.Product_specific = proptools.BoolPtr(true)
948 } else if module.SystemExtSpecific() {
949 props.System_ext_specific = proptools.BoolPtr(true)
950 }
951 // If the build should use prebuilt sdks then set prefer to true on the stubs library.
952 // That will cause the prebuilt version of the stubs to override the source version.
953 if mctx.Config().UnbundledBuildUsePrebuiltSdks() {
954 props.Prefer = proptools.BoolPtr(true)
955 }
956 mctx.CreateModule(ImportFactory, &props)
957}
958
Colin Cross79c7c262019-04-17 11:11:46 -0700959func (module *sdkLibraryImport) DepsMutator(ctx android.BottomUpMutatorContext) {
Paul Duffin6a2bd112020-04-07 19:27:04 +0100960 for apiScope, scopeProperties := range module.scopeProperties {
Paul Duffin56d44902020-01-31 13:36:25 +0000961 if len(scopeProperties.Jars) == 0 {
962 continue
963 }
964
965 // Add dependencies to the prebuilt stubs library
966 ctx.AddVariationDependencies(nil, apiScope.stubsTag, apiScope.stubsModuleName(module.BaseModuleName()))
967 }
Colin Cross79c7c262019-04-17 11:11:46 -0700968}
969
970func (module *sdkLibraryImport) GenerateAndroidBuildActions(ctx android.ModuleContext) {
971 // Record the paths to the prebuilt stubs library.
972 ctx.VisitDirectDeps(func(to android.Module) {
973 tag := ctx.OtherModuleDependencyTag(to)
974
Paul Duffin56d44902020-01-31 13:36:25 +0000975 if lib, ok := to.(Dependency); ok {
976 if scopeTag, ok := tag.(scopeDependencyTag); ok {
977 apiScope := scopeTag.apiScope
978 scopePaths := module.getScopePaths(apiScope)
979 scopePaths.stubsHeaderPath = lib.HeaderJars()
980 }
Colin Cross79c7c262019-04-17 11:11:46 -0700981 }
982 })
983}
984
Paul Duffin56d44902020-01-31 13:36:25 +0000985func (module *sdkLibraryImport) sdkJars(
986 ctx android.BaseModuleContext,
987 sdkVersion sdkSpec) android.Paths {
988
Paul Duffin50061512020-01-21 16:31:05 +0000989 // If a specific numeric version has been requested then use prebuilt versions of the sdk.
990 if sdkVersion.version.isNumbered() {
991 return PrebuiltJars(ctx, module.BaseModuleName(), sdkVersion)
992 }
993
Paul Duffin56d44902020-01-31 13:36:25 +0000994 var apiScope *apiScope
995 switch sdkVersion.kind {
996 case sdkSystem:
997 apiScope = apiScopeSystem
998 case sdkTest:
999 apiScope = apiScopeTest
1000 default:
1001 apiScope = apiScopePublic
1002 }
1003
1004 paths := module.getScopePaths(apiScope)
1005 return paths.stubsHeaderPath
1006}
1007
Colin Cross79c7c262019-04-17 11:11:46 -07001008// to satisfy SdkLibraryDependency interface
Jiyong Park6a927c42020-01-21 02:03:43 +09001009func (module *sdkLibraryImport) SdkHeaderJars(ctx android.BaseModuleContext, sdkVersion sdkSpec) android.Paths {
Colin Cross79c7c262019-04-17 11:11:46 -07001010 // This module is just a wrapper for the prebuilt stubs.
Paul Duffin56d44902020-01-31 13:36:25 +00001011 return module.sdkJars(ctx, sdkVersion)
Colin Cross79c7c262019-04-17 11:11:46 -07001012}
1013
1014// to satisfy SdkLibraryDependency interface
Jiyong Park6a927c42020-01-21 02:03:43 +09001015func (module *sdkLibraryImport) SdkImplementationJars(ctx android.BaseModuleContext, sdkVersion sdkSpec) android.Paths {
Colin Cross79c7c262019-04-17 11:11:46 -07001016 // This module is just a wrapper for the stubs.
Paul Duffin56d44902020-01-31 13:36:25 +00001017 return module.sdkJars(ctx, sdkVersion)
Colin Cross79c7c262019-04-17 11:11:46 -07001018}
Jiyong Parke3833882020-02-17 17:28:10 +09001019
1020//
1021// java_sdk_library_xml
1022//
1023type sdkLibraryXml struct {
1024 android.ModuleBase
1025 android.DefaultableModuleBase
1026 android.ApexModuleBase
1027
1028 properties sdkLibraryXmlProperties
1029
1030 outputFilePath android.OutputPath
1031 installDirPath android.InstallPath
1032}
1033
1034type sdkLibraryXmlProperties struct {
1035 // canonical name of the lib
1036 Lib_name *string
1037}
1038
1039// java_sdk_library_xml builds the permission xml file for a java_sdk_library.
1040// Not to be used directly by users. java_sdk_library internally uses this.
1041func sdkLibraryXmlFactory() android.Module {
1042 module := &sdkLibraryXml{}
1043
1044 module.AddProperties(&module.properties)
1045
1046 android.InitApexModule(module)
1047 android.InitAndroidArchModule(module, android.DeviceSupported, android.MultilibCommon)
1048
1049 return module
1050}
1051
1052// from android.PrebuiltEtcModule
1053func (module *sdkLibraryXml) SubDir() string {
1054 return "permissions"
1055}
1056
1057// from android.PrebuiltEtcModule
1058func (module *sdkLibraryXml) OutputFile() android.OutputPath {
1059 return module.outputFilePath
1060}
1061
1062// from android.ApexModule
1063func (module *sdkLibraryXml) AvailableFor(what string) bool {
1064 return true
1065}
1066
1067func (module *sdkLibraryXml) DepsMutator(ctx android.BottomUpMutatorContext) {
1068 // do nothing
1069}
1070
1071// File path to the runtime implementation library
1072func (module *sdkLibraryXml) implPath() string {
1073 implName := proptools.String(module.properties.Lib_name)
1074 if apexName := module.ApexName(); apexName != "" {
1075 // TODO(b/146468504): ApexName() is only a soong module name, not apex name.
1076 // In most cases, this works fine. But when apex_name is set or override_apex is used
1077 // this can be wrong.
1078 return fmt.Sprintf("/apex/%s/javalib/%s.jar", apexName, implName)
1079 }
1080 partition := "system"
1081 if module.SocSpecific() {
1082 partition = "vendor"
1083 } else if module.DeviceSpecific() {
1084 partition = "odm"
1085 } else if module.ProductSpecific() {
1086 partition = "product"
1087 } else if module.SystemExtSpecific() {
1088 partition = "system_ext"
1089 }
1090 return "/" + partition + "/framework/" + implName + ".jar"
1091}
1092
1093func (module *sdkLibraryXml) GenerateAndroidBuildActions(ctx android.ModuleContext) {
1094 libName := proptools.String(module.properties.Lib_name)
1095 xmlContent := fmt.Sprintf(permissionsTemplate, libName, module.implPath())
1096
1097 module.outputFilePath = android.PathForModuleOut(ctx, libName+".xml").OutputPath
1098 rule := android.NewRuleBuilder()
1099 rule.Command().
1100 Text("/bin/bash -c \"echo -e '" + xmlContent + "'\" > ").
1101 Output(module.outputFilePath)
1102
1103 rule.Build(pctx, ctx, "java_sdk_xml", "Permission XML")
1104
1105 module.installDirPath = android.PathForModuleInstall(ctx, "etc", module.SubDir())
1106}
1107
1108func (module *sdkLibraryXml) AndroidMkEntries() []android.AndroidMkEntries {
1109 if !module.IsForPlatform() {
1110 return []android.AndroidMkEntries{android.AndroidMkEntries{
1111 Disabled: true,
1112 }}
1113 }
1114
1115 return []android.AndroidMkEntries{android.AndroidMkEntries{
1116 Class: "ETC",
1117 OutputFile: android.OptionalPathForPath(module.outputFilePath),
1118 ExtraEntries: []android.AndroidMkExtraEntriesFunc{
1119 func(entries *android.AndroidMkEntries) {
1120 entries.SetString("LOCAL_MODULE_TAGS", "optional")
1121 entries.SetString("LOCAL_MODULE_PATH", module.installDirPath.ToMakePath().String())
1122 entries.SetString("LOCAL_INSTALLED_MODULE_STEM", module.outputFilePath.Base())
1123 },
1124 },
1125 }}
1126}
Paul Duffin61871622020-02-10 13:37:10 +00001127
1128type sdkLibrarySdkMemberType struct {
1129 android.SdkMemberTypeBase
1130}
1131
1132func (s *sdkLibrarySdkMemberType) AddDependencies(mctx android.BottomUpMutatorContext, dependencyTag blueprint.DependencyTag, names []string) {
1133 mctx.AddVariationDependencies(nil, dependencyTag, names...)
1134}
1135
1136func (s *sdkLibrarySdkMemberType) IsInstance(module android.Module) bool {
1137 _, ok := module.(*SdkLibrary)
1138 return ok
1139}
1140
1141func (s *sdkLibrarySdkMemberType) AddPrebuiltModule(ctx android.SdkMemberContext, member android.SdkMember) android.BpModule {
1142 return ctx.SnapshotBuilder().AddPrebuiltModule(member, "java_sdk_library_import")
1143}
1144
1145func (s *sdkLibrarySdkMemberType) CreateVariantPropertiesStruct() android.SdkMemberProperties {
1146 return &sdkLibrarySdkMemberProperties{}
1147}
1148
1149type sdkLibrarySdkMemberProperties struct {
1150 android.SdkMemberPropertiesBase
1151
1152 // Scope to per scope properties.
1153 Scopes map[*apiScope]scopeProperties
1154
1155 // Additional libraries that the exported stubs libraries depend upon.
1156 Libs []string
1157}
1158
1159type scopeProperties struct {
1160 Jars android.Paths
1161 SdkVersion string
1162}
1163
1164func (s *sdkLibrarySdkMemberProperties) PopulateFromVariant(ctx android.SdkMemberContext, variant android.Module) {
1165 sdk := variant.(*SdkLibrary)
1166
1167 s.Scopes = make(map[*apiScope]scopeProperties)
1168 for _, apiScope := range allApiScopes {
1169 paths := sdk.getScopePaths(apiScope)
1170 jars := paths.stubsImplPath
1171 if len(jars) > 0 {
1172 properties := scopeProperties{}
1173 properties.Jars = jars
1174 properties.SdkVersion = apiScope.sdkVersion
1175 s.Scopes[apiScope] = properties
1176 }
1177 }
1178
1179 s.Libs = sdk.properties.Libs
1180}
1181
1182func (s *sdkLibrarySdkMemberProperties) AddToPropertySet(ctx android.SdkMemberContext, propertySet android.BpPropertySet) {
1183 for _, apiScope := range allApiScopes {
1184 if properties, ok := s.Scopes[apiScope]; ok {
1185 scopeSet := propertySet.AddPropertySet(apiScope.name)
1186
1187 var jars []string
1188 for _, p := range properties.Jars {
1189 dest := filepath.Join("sdk_library", s.OsPrefix(), apiScope.name, ctx.Name()+"-stubs.jar")
1190 ctx.SnapshotBuilder().CopyToSnapshot(p, dest)
1191 jars = append(jars, dest)
1192 }
1193 scopeSet.AddProperty("jars", jars)
1194
1195 if properties.SdkVersion != "" {
1196 scopeSet.AddProperty("sdk_version", properties.SdkVersion)
1197 }
1198 }
1199 }
1200
1201 if len(s.Libs) > 0 {
1202 propertySet.AddPropertyWithTag("libs", s.Libs, ctx.SnapshotBuilder().SdkMemberReferencePropertyTag(false))
1203 }
1204}