blob: a478a7d80739ddb20aca527ccf534a411f83908a [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
86 // The suffix to add to the make variable that references the location of the api file.
87 apiFileMakeVariableSuffix string
88
89 // SDK version that the stubs library is built against. Note that this is always
90 // *current. Older stubs library built with a numbered SDK version is created from
91 // the prebuilt jar.
92 sdkVersion string
Paul Duffin3c7c3472020-04-07 18:50:10 +010093
94 // Extra arguments to pass to droidstubs for this scope.
95 droidstubsArgs []string
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{
136 name: "system",
137 apiFilePrefix: "system-",
138 moduleSuffix: sdkSystemApiSuffix,
139 apiFileMakeVariableSuffix: "_SYSTEM",
140 sdkVersion: "system_current",
Paul Duffin3c7c3472020-04-07 18:50:10 +0100141 droidstubsArgs: []string{"-showAnnotation android.annotation.SystemApi"},
Paul Duffind1b3a922020-01-22 11:57:20 +0000142 })
143 apiScopeTest = initApiScope(&apiScope{
144 name: "test",
145 apiFilePrefix: "test-",
146 moduleSuffix: sdkTestApiSuffix,
147 apiFileMakeVariableSuffix: "_TEST",
148 sdkVersion: "test_current",
Paul Duffin3c7c3472020-04-07 18:50:10 +0100149 droidstubsArgs: []string{"-showAnnotation android.annotation.TestApi"},
Paul Duffind1b3a922020-01-22 11:57:20 +0000150 })
151 allApiScopes = apiScopes{
152 apiScopePublic,
153 apiScopeSystem,
154 apiScopeTest,
155 }
Jiyong Parkc678ad32018-04-10 13:07:10 +0900156)
157
Jiyong Park82484c02018-04-23 21:41:26 +0900158var (
159 javaSdkLibrariesLock sync.Mutex
160)
161
Jiyong Parkc678ad32018-04-10 13:07:10 +0900162// TODO: these are big features that are currently missing
Jiyong Park1be96912018-05-28 18:02:19 +0900163// 1) disallowing linking to the runtime shared lib
164// 2) HTML generation
Jiyong Parkc678ad32018-04-10 13:07:10 +0900165
166func init() {
Paul Duffin43dc1cc2019-12-19 11:18:54 +0000167 RegisterSdkLibraryBuildComponents(android.InitRegistrationContext)
Jiyong Parkc678ad32018-04-10 13:07:10 +0900168
Jiyong Park82484c02018-04-23 21:41:26 +0900169 android.RegisterMakeVarsProvider(pctx, func(ctx android.MakeVarsContext) {
170 javaSdkLibraries := javaSdkLibraries(ctx.Config())
171 sort.Strings(*javaSdkLibraries)
172 ctx.Strict("JAVA_SDK_LIBRARIES", strings.Join(*javaSdkLibraries, " "))
173 })
Paul Duffin61871622020-02-10 13:37:10 +0000174
175 // Register sdk member types.
176 android.RegisterSdkMemberType(&sdkLibrarySdkMemberType{
177 android.SdkMemberTypeBase{
178 PropertyName: "java_sdk_libs",
179 SupportsSdk: true,
180 },
181 })
Jiyong Parkc678ad32018-04-10 13:07:10 +0900182}
183
Paul Duffin43dc1cc2019-12-19 11:18:54 +0000184func RegisterSdkLibraryBuildComponents(ctx android.RegistrationContext) {
185 ctx.RegisterModuleType("java_sdk_library", SdkLibraryFactory)
186 ctx.RegisterModuleType("java_sdk_library_import", sdkLibraryImportFactory)
187}
188
Jiyong Parkc678ad32018-04-10 13:07:10 +0900189type sdkLibraryProperties struct {
Sundong Ahnf043cf62018-06-25 16:04:37 +0900190 // List of Java libraries that will be in the classpath when building stubs
191 Stub_only_libs []string `android:"arch_variant"`
192
Paul Duffin7a586d32019-12-30 17:09:34 +0000193 // list of package names that will be documented and publicized as API.
194 // This allows the API to be restricted to a subset of the source files provided.
195 // If this is unspecified then all the source files will be treated as being part
196 // of the API.
Jiyong Parkc678ad32018-04-10 13:07:10 +0900197 Api_packages []string
198
Jiyong Park5a2c9d72018-05-01 22:25:41 +0900199 // list of package names that must be hidden from the API
200 Hidden_api_packages []string
201
Paul Duffin749f98f2019-12-30 17:23:46 +0000202 // the relative path to the directory containing the api specification files.
203 // Defaults to "api".
204 Api_dir *string
205
Paul Duffin43db9be2019-12-30 17:35:49 +0000206 // If set to true there is no runtime library.
207 Api_only *bool
208
Paul Duffin11512472019-02-11 15:55:17 +0000209 // local files that are used within user customized droiddoc options.
210 Droiddoc_option_files []string
211
212 // additional droiddoc options
213 // Available variables for substitution:
214 //
215 // $(location <label>): the path to the droiddoc_option_files with name <label>
Sundong Ahndd567f92018-07-31 17:19:11 +0900216 Droiddoc_options []string
217
Sundong Ahn054b19a2018-10-19 13:46:09 +0900218 // a list of top-level directories containing files to merge qualifier annotations
219 // (i.e. those intended to be included in the stubs written) from.
220 Merge_annotations_dirs []string
221
222 // a list of top-level directories containing Java stub files to merge show/hide annotations from.
223 Merge_inclusion_annotations_dirs []string
224
225 // If set to true, the path of dist files is apistubs/core. Defaults to false.
226 Core_lib *bool
227
Sundong Ahn80a87b32019-05-13 15:02:50 +0900228 // don't create dist rules.
229 No_dist *bool `blueprint:"mutated"`
230
Paul Duffin37e0b772019-12-30 17:20:10 +0000231 // indicates whether system and test apis should be managed.
232 Has_system_and_test_apis bool `blueprint:"mutated"`
233
Jiyong Parkc678ad32018-04-10 13:07:10 +0900234 // TODO: determines whether to create HTML doc or not
235 //Html_doc *bool
236}
237
Paul Duffind1b3a922020-01-22 11:57:20 +0000238type scopePaths struct {
239 stubsHeaderPath android.Paths
240 stubsImplPath android.Paths
241 apiFilePath android.Path
242}
243
Paul Duffin56d44902020-01-31 13:36:25 +0000244// Common code between sdk library and sdk library import
245type commonToSdkLibraryAndImport struct {
246 scopePaths map[*apiScope]*scopePaths
247}
248
249func (c *commonToSdkLibraryAndImport) getScopePaths(scope *apiScope) *scopePaths {
250 if c.scopePaths == nil {
251 c.scopePaths = make(map[*apiScope]*scopePaths)
252 }
253 paths := c.scopePaths[scope]
254 if paths == nil {
255 paths = &scopePaths{}
256 c.scopePaths[scope] = paths
257 }
258
259 return paths
260}
261
Inseob Kimc0907f12019-02-08 21:00:45 +0900262type SdkLibrary struct {
Sundong Ahn054b19a2018-10-19 13:46:09 +0900263 Library
Jiyong Parkc678ad32018-04-10 13:07:10 +0900264
Sundong Ahn054b19a2018-10-19 13:46:09 +0900265 sdkLibraryProperties sdkLibraryProperties
Jiyong Parkc678ad32018-04-10 13:07:10 +0900266
Paul Duffin56d44902020-01-31 13:36:25 +0000267 commonToSdkLibraryAndImport
Jiyong Parkc678ad32018-04-10 13:07:10 +0900268}
269
Inseob Kimc0907f12019-02-08 21:00:45 +0900270var _ Dependency = (*SdkLibrary)(nil)
271var _ SdkLibraryDependency = (*SdkLibrary)(nil)
Colin Cross897d2ed2019-02-11 14:03:51 -0800272
Paul Duffind1b3a922020-01-22 11:57:20 +0000273func (module *SdkLibrary) getActiveApiScopes() apiScopes {
274 if module.sdkLibraryProperties.Has_system_and_test_apis {
275 return allApiScopes
276 } else {
277 return apiScopes{apiScopePublic}
278 }
279}
280
Paul Duffine74ac732020-02-06 13:51:46 +0000281var xmlPermissionsFileTag = dependencyTag{name: "xml-permissions-file"}
282
Jiyong Parke3833882020-02-17 17:28:10 +0900283func IsXmlPermissionsFileDepTag(depTag blueprint.DependencyTag) bool {
284 if dt, ok := depTag.(dependencyTag); ok {
285 return dt == xmlPermissionsFileTag
286 }
287 return false
288}
289
Inseob Kimc0907f12019-02-08 21:00:45 +0900290func (module *SdkLibrary) DepsMutator(ctx android.BottomUpMutatorContext) {
Paul Duffind1b3a922020-01-22 11:57:20 +0000291 for _, apiScope := range module.getActiveApiScopes() {
292 // Add dependencies to the stubs library
Paul Duffin50061512020-01-21 16:31:05 +0000293 ctx.AddVariationDependencies(nil, apiScope.stubsTag, module.stubsName(apiScope))
Paul Duffind1b3a922020-01-22 11:57:20 +0000294
Paul Duffin50061512020-01-21 16:31:05 +0000295 // And the api file
Paul Duffind1b3a922020-01-22 11:57:20 +0000296 ctx.AddVariationDependencies(nil, apiScope.apiFileTag, module.docsName(apiScope))
Sundong Ahn054b19a2018-10-19 13:46:09 +0900297 }
298
Paul Duffine74ac732020-02-06 13:51:46 +0000299 if !proptools.Bool(module.sdkLibraryProperties.Api_only) {
300 // Add dependency to the rule for generating the xml permissions file
Jiyong Parke3833882020-02-17 17:28:10 +0900301 ctx.AddDependency(module, xmlPermissionsFileTag, module.xmlFileName())
Paul Duffine74ac732020-02-06 13:51:46 +0000302 }
303
Sundong Ahn054b19a2018-10-19 13:46:09 +0900304 module.Library.deps(ctx)
Jiyong Parkc678ad32018-04-10 13:07:10 +0900305}
306
Inseob Kimc0907f12019-02-08 21:00:45 +0900307func (module *SdkLibrary) GenerateAndroidBuildActions(ctx android.ModuleContext) {
Paul Duffin43db9be2019-12-30 17:35:49 +0000308 // Don't build an implementation library if this is api only.
309 if !proptools.Bool(module.sdkLibraryProperties.Api_only) {
310 module.Library.GenerateAndroidBuildActions(ctx)
311 }
Sundong Ahn054b19a2018-10-19 13:46:09 +0900312
Sundong Ahn57368eb2018-07-06 11:20:23 +0900313 // Record the paths to the header jars of the library (stubs and impl).
Paul Duffind1b3a922020-01-22 11:57:20 +0000314 // When this java_sdk_library is depended upon from others via "libs" property,
Jiyong Parkc678ad32018-04-10 13:07:10 +0900315 // the recorded paths will be returned depending on the link type of the caller.
316 ctx.VisitDirectDeps(func(to android.Module) {
317 otherName := ctx.OtherModuleName(to)
318 tag := ctx.OtherModuleDependencyTag(to)
319
Sundong Ahn57368eb2018-07-06 11:20:23 +0900320 if lib, ok := to.(Dependency); ok {
Paul Duffind1b3a922020-01-22 11:57:20 +0000321 if scopeTag, ok := tag.(scopeDependencyTag); ok {
322 apiScope := scopeTag.apiScope
323 scopePaths := module.getScopePaths(apiScope)
324 scopePaths.stubsHeaderPath = lib.HeaderJars()
325 scopePaths.stubsImplPath = lib.ImplementationJars()
Jiyong Parkc678ad32018-04-10 13:07:10 +0900326 }
327 }
Sundong Ahn20e998b2018-07-24 11:19:26 +0900328 if doc, ok := to.(ApiFilePath); ok {
Paul Duffind1b3a922020-01-22 11:57:20 +0000329 if scopeTag, ok := tag.(scopeDependencyTag); ok {
330 apiScope := scopeTag.apiScope
331 scopePaths := module.getScopePaths(apiScope)
332 scopePaths.apiFilePath = doc.ApiFilePath()
333 } else {
Sundong Ahn20e998b2018-07-24 11:19:26 +0900334 ctx.ModuleErrorf("depends on module %q of unknown tag %q", otherName, tag)
335 }
336 }
Jiyong Parkc678ad32018-04-10 13:07:10 +0900337 })
338}
339
Jiyong Park0b0e1b92019-12-03 13:24:29 +0900340func (module *SdkLibrary) AndroidMkEntries() []android.AndroidMkEntries {
Paul Duffin43db9be2019-12-30 17:35:49 +0000341 if proptools.Bool(module.sdkLibraryProperties.Api_only) {
342 return nil
343 }
Jiyong Park0b0e1b92019-12-03 13:24:29 +0900344 entriesList := module.Library.AndroidMkEntries()
345 entries := &entriesList[0]
Jaewoong Jungb0c127c2019-08-29 14:56:03 -0700346 entries.Required = append(entries.Required, module.xmlFileName())
Jiyong Park0b0e1b92019-12-03 13:24:29 +0900347 return entriesList
Jiyong Park82484c02018-04-23 21:41:26 +0900348}
349
Jiyong Parkc678ad32018-04-10 13:07:10 +0900350// Module name of the stubs library
Paul Duffind1b3a922020-01-22 11:57:20 +0000351func (module *SdkLibrary) stubsName(apiScope *apiScope) string {
352 return apiScope.stubsModuleName(module.BaseModuleName())
Jiyong Parkc678ad32018-04-10 13:07:10 +0900353}
354
355// Module name of the docs
Paul Duffind1b3a922020-01-22 11:57:20 +0000356func (module *SdkLibrary) docsName(apiScope *apiScope) string {
357 return apiScope.docsModuleName(module.BaseModuleName())
Jiyong Parkc678ad32018-04-10 13:07:10 +0900358}
359
360// Module name of the runtime implementation library
Inseob Kimc0907f12019-02-08 21:00:45 +0900361func (module *SdkLibrary) implName() string {
Sundong Ahn054b19a2018-10-19 13:46:09 +0900362 return module.BaseModuleName()
Jiyong Parkc678ad32018-04-10 13:07:10 +0900363}
364
Jiyong Parkc678ad32018-04-10 13:07:10 +0900365// Module name of the XML file for the lib
Inseob Kimc0907f12019-02-08 21:00:45 +0900366func (module *SdkLibrary) xmlFileName() string {
Jiyong Parkc678ad32018-04-10 13:07:10 +0900367 return module.BaseModuleName() + sdkXmlFileSuffix
368}
369
Anton Hansson6bb88102020-03-27 19:43:19 +0000370// The dist path of the stub artifacts
371func (module *SdkLibrary) apiDistPath(apiScope *apiScope) string {
372 if module.ModuleBase.Owner() != "" {
373 return path.Join("apistubs", module.ModuleBase.Owner(), apiScope.name)
374 } else if Bool(module.sdkLibraryProperties.Core_lib) {
375 return path.Join("apistubs", "core", apiScope.name)
376 } else {
377 return path.Join("apistubs", "android", apiScope.name)
378 }
379}
380
Paul Duffin12ceb462019-12-24 20:31:31 +0000381// Get the sdk version for use when compiling the stubs library.
Paul Duffind1b3a922020-01-22 11:57:20 +0000382func (module *SdkLibrary) sdkVersionForStubsLibrary(mctx android.LoadHookContext, apiScope *apiScope) string {
Paul Duffin12ceb462019-12-24 20:31:31 +0000383 sdkDep := decodeSdkDep(mctx, sdkContext(&module.Library))
384 if sdkDep.hasStandardLibs() {
385 // If building against a standard sdk then use the sdk version appropriate for the scope.
Paul Duffind1b3a922020-01-22 11:57:20 +0000386 return apiScope.sdkVersion
Paul Duffin12ceb462019-12-24 20:31:31 +0000387 } else {
388 // Otherwise, use no system module.
389 return "none"
390 }
391}
392
Paul Duffind1b3a922020-01-22 11:57:20 +0000393func (module *SdkLibrary) latestApiFilegroupName(apiScope *apiScope) string {
394 return ":" + module.BaseModuleName() + ".api." + apiScope.name + ".latest"
Jiyong Park58c518b2018-05-12 22:29:12 +0900395}
Jiyong Parkc678ad32018-04-10 13:07:10 +0900396
Paul Duffind1b3a922020-01-22 11:57:20 +0000397func (module *SdkLibrary) latestRemovedApiFilegroupName(apiScope *apiScope) string {
398 return ":" + module.BaseModuleName() + "-removed.api." + apiScope.name + ".latest"
Jiyong Parkc678ad32018-04-10 13:07:10 +0900399}
400
401// Creates a static java library that has API stubs
Paul Duffind1b3a922020-01-22 11:57:20 +0000402func (module *SdkLibrary) createStubsLibrary(mctx android.LoadHookContext, apiScope *apiScope) {
Jiyong Parkc678ad32018-04-10 13:07:10 +0900403 props := struct {
Sundong Ahn0d7dff42019-12-04 12:53:44 +0900404 Name *string
405 Srcs []string
Paul Duffin367ab912019-12-23 19:40:36 +0000406 Installable *bool
Sundong Ahn0d7dff42019-12-04 12:53:44 +0900407 Sdk_version *string
Paul Duffin12ceb462019-12-24 20:31:31 +0000408 System_modules *string
Paul Duffinab8da5d2020-02-07 16:12:04 +0000409 Patch_module *string
Sundong Ahn0d7dff42019-12-04 12:53:44 +0900410 Libs []string
411 Soc_specific *bool
412 Device_specific *bool
413 Product_specific *bool
414 System_ext_specific *bool
415 Compile_dex *bool
Sundong Ahn0d7dff42019-12-04 12:53:44 +0900416 Java_version *string
417 Product_variables struct {
Jiyong Park82484c02018-04-23 21:41:26 +0900418 Pdk struct {
419 Enabled *bool
420 }
Jiyong Parkc678ad32018-04-10 13:07:10 +0900421 }
Sundong Ahn054b19a2018-10-19 13:46:09 +0900422 Openjdk9 struct {
423 Srcs []string
424 Javacflags []string
425 }
Anton Hansson6bb88102020-03-27 19:43:19 +0000426 Dist struct {
427 Targets []string
428 Dest *string
429 Dir *string
430 Tag *string
431 }
Jiyong Parkc678ad32018-04-10 13:07:10 +0900432 }{}
433
Jiyong Parkdf130542018-04-27 16:29:21 +0900434 props.Name = proptools.StringPtr(module.stubsName(apiScope))
Jiyong Parkc678ad32018-04-10 13:07:10 +0900435 // sources are generated from the droiddoc
Jiyong Parkdf130542018-04-27 16:29:21 +0900436 props.Srcs = []string{":" + module.docsName(apiScope)}
Paul Duffin12ceb462019-12-24 20:31:31 +0000437 sdkVersion := module.sdkVersionForStubsLibrary(mctx, apiScope)
Paul Duffin52d398a2019-06-11 12:31:14 +0100438 props.Sdk_version = proptools.StringPtr(sdkVersion)
Paul Duffin12ceb462019-12-24 20:31:31 +0000439 props.System_modules = module.Library.Module.deviceProperties.System_modules
Paul Duffinab8da5d2020-02-07 16:12:04 +0000440 props.Patch_module = module.Library.Module.properties.Patch_module
Paul Duffin367ab912019-12-23 19:40:36 +0000441 props.Installable = proptools.BoolPtr(false)
Sundong Ahn054b19a2018-10-19 13:46:09 +0900442 props.Libs = module.sdkLibraryProperties.Stub_only_libs
Jiyong Park82484c02018-04-23 21:41:26 +0900443 props.Product_variables.Pdk.Enabled = proptools.BoolPtr(false)
Sundong Ahn054b19a2018-10-19 13:46:09 +0900444 props.Openjdk9.Srcs = module.Library.Module.properties.Openjdk9.Srcs
445 props.Openjdk9.Javacflags = module.Library.Module.properties.Openjdk9.Javacflags
446 props.Java_version = module.Library.Module.properties.Java_version
447 if module.Library.Module.deviceProperties.Compile_dex != nil {
448 props.Compile_dex = module.Library.Module.deviceProperties.Compile_dex
Sundong Ahndd567f92018-07-31 17:19:11 +0900449 }
Jiyong Parkc678ad32018-04-10 13:07:10 +0900450
451 if module.SocSpecific() {
452 props.Soc_specific = proptools.BoolPtr(true)
453 } else if module.DeviceSpecific() {
454 props.Device_specific = proptools.BoolPtr(true)
455 } else if module.ProductSpecific() {
456 props.Product_specific = proptools.BoolPtr(true)
Sundong Ahn0d7dff42019-12-04 12:53:44 +0900457 } else if module.SystemExtSpecific() {
458 props.System_ext_specific = proptools.BoolPtr(true)
Jiyong Parkc678ad32018-04-10 13:07:10 +0900459 }
Anton Hansson6bb88102020-03-27 19:43:19 +0000460 // Dist the class jar artifact for sdk builds.
461 if !Bool(module.sdkLibraryProperties.No_dist) {
462 props.Dist.Targets = []string{"sdk", "win_sdk"}
463 props.Dist.Dest = proptools.StringPtr(fmt.Sprintf("%v.jar", module.BaseModuleName()))
464 props.Dist.Dir = proptools.StringPtr(module.apiDistPath(apiScope))
465 props.Dist.Tag = proptools.StringPtr(".jar")
466 }
Jiyong Parkc678ad32018-04-10 13:07:10 +0900467
Colin Cross84dfc3d2019-09-25 11:33:01 -0700468 mctx.CreateModule(LibraryFactory, &props)
Jiyong Parkc678ad32018-04-10 13:07:10 +0900469}
470
Paul Duffincbcfcaa2020-04-07 18:49:53 +0100471// Creates a droidstubs module that creates stubs source files from the given full source
Jiyong Parkc678ad32018-04-10 13:07:10 +0900472// files
Paul Duffind1b3a922020-01-22 11:57:20 +0000473func (module *SdkLibrary) createStubsSources(mctx android.LoadHookContext, apiScope *apiScope) {
Jiyong Parkc678ad32018-04-10 13:07:10 +0900474 props := struct {
Sundong Ahn054b19a2018-10-19 13:46:09 +0900475 Name *string
476 Srcs []string
477 Installable *bool
Paul Duffin52d398a2019-06-11 12:31:14 +0100478 Sdk_version *string
Paul Duffin12ceb462019-12-24 20:31:31 +0000479 System_modules *string
Sundong Ahn054b19a2018-10-19 13:46:09 +0900480 Libs []string
Paul Duffin11512472019-02-11 15:55:17 +0000481 Arg_files []string
Sundong Ahn054b19a2018-10-19 13:46:09 +0900482 Args *string
Sundong Ahn054b19a2018-10-19 13:46:09 +0900483 Java_version *string
484 Merge_annotations_dirs []string
485 Merge_inclusion_annotations_dirs []string
486 Check_api struct {
Inseob Kim38449af2019-02-28 14:24:05 +0900487 Current ApiToCheck
488 Last_released ApiToCheck
489 Ignore_missing_latest_api *bool
Jiyong Park58c518b2018-05-12 22:29:12 +0900490 }
Sundong Ahn1b92c822018-05-29 11:35:17 +0900491 Aidl struct {
492 Include_dirs []string
493 Local_include_dirs []string
494 }
Anton Hansson6bb88102020-03-27 19:43:19 +0000495 Dist struct {
496 Targets []string
497 Dest *string
498 Dir *string
499 }
Jiyong Parkc678ad32018-04-10 13:07:10 +0900500 }{}
501
Paul Duffin250e6192019-06-07 10:44:37 +0100502 sdkDep := decodeSdkDep(mctx, sdkContext(&module.Library))
Paul Duffin12ceb462019-12-24 20:31:31 +0000503 // Use the platform API if standard libraries were requested, otherwise use
504 // no default libraries.
Paul Duffin52d398a2019-06-11 12:31:14 +0100505 sdkVersion := ""
506 if !sdkDep.hasStandardLibs() {
507 sdkVersion = "none"
508 }
Paul Duffin250e6192019-06-07 10:44:37 +0100509
Jiyong Parkdf130542018-04-27 16:29:21 +0900510 props.Name = proptools.StringPtr(module.docsName(apiScope))
Sundong Ahn054b19a2018-10-19 13:46:09 +0900511 props.Srcs = append(props.Srcs, module.Library.Module.properties.Srcs...)
Paul Duffin52d398a2019-06-11 12:31:14 +0100512 props.Sdk_version = proptools.StringPtr(sdkVersion)
Paul Duffin12ceb462019-12-24 20:31:31 +0000513 props.System_modules = module.Library.Module.deviceProperties.System_modules
Jiyong Parkc678ad32018-04-10 13:07:10 +0900514 props.Installable = proptools.BoolPtr(false)
Sundong Ahne6f0b052018-06-05 16:46:14 +0900515 // A droiddoc module has only one Libs property and doesn't distinguish between
516 // shared libs and static libs. So we need to add both of these libs to Libs property.
Sundong Ahn054b19a2018-10-19 13:46:09 +0900517 props.Libs = module.Library.Module.properties.Libs
518 props.Libs = append(props.Libs, module.Library.Module.properties.Static_libs...)
519 props.Aidl.Include_dirs = module.Library.Module.deviceProperties.Aidl.Include_dirs
520 props.Aidl.Local_include_dirs = module.Library.Module.deviceProperties.Aidl.Local_include_dirs
Sundong Ahn054b19a2018-10-19 13:46:09 +0900521 props.Java_version = module.Library.Module.properties.Java_version
Jiyong Parkc678ad32018-04-10 13:07:10 +0900522
Sundong Ahn054b19a2018-10-19 13:46:09 +0900523 props.Merge_annotations_dirs = module.sdkLibraryProperties.Merge_annotations_dirs
524 props.Merge_inclusion_annotations_dirs = module.sdkLibraryProperties.Merge_inclusion_annotations_dirs
525
Paul Duffincbcfcaa2020-04-07 18:49:53 +0100526 droidstubsArgs := []string{}
Paul Duffin235ffff2019-12-24 10:41:30 +0000527 if len(module.sdkLibraryProperties.Api_packages) != 0 {
Paul Duffincbcfcaa2020-04-07 18:49:53 +0100528 droidstubsArgs = append(droidstubsArgs, "--stub-packages "+strings.Join(module.sdkLibraryProperties.Api_packages, ":"))
Paul Duffin235ffff2019-12-24 10:41:30 +0000529 }
530 if len(module.sdkLibraryProperties.Hidden_api_packages) != 0 {
Paul Duffincbcfcaa2020-04-07 18:49:53 +0100531 droidstubsArgs = append(droidstubsArgs,
Paul Duffin235ffff2019-12-24 10:41:30 +0000532 android.JoinWithPrefix(module.sdkLibraryProperties.Hidden_api_packages, " --hide-package "))
533 }
Paul Duffincbcfcaa2020-04-07 18:49:53 +0100534 droidstubsArgs = append(droidstubsArgs, module.sdkLibraryProperties.Droiddoc_options...)
Paul Duffin235ffff2019-12-24 10:41:30 +0000535 disabledWarnings := []string{
536 "MissingPermission",
537 "BroadcastBehavior",
538 "HiddenSuperclass",
539 "DeprecationMismatch",
540 "UnavailableSymbol",
541 "SdkConstant",
542 "HiddenTypeParameter",
543 "Todo",
544 "Typo",
545 }
Paul Duffincbcfcaa2020-04-07 18:49:53 +0100546 droidstubsArgs = append(droidstubsArgs, android.JoinWithPrefix(disabledWarnings, "--hide "))
Sundong Ahnfb2721f2018-09-17 13:23:09 +0900547
Paul Duffin3c7c3472020-04-07 18:50:10 +0100548 // Add in scope specific arguments.
549 droidstubsArgs = append(droidstubsArgs, apiScope.droidstubsArgs...)
Paul Duffin11512472019-02-11 15:55:17 +0000550 props.Arg_files = module.sdkLibraryProperties.Droiddoc_option_files
Paul Duffincbcfcaa2020-04-07 18:49:53 +0100551 props.Args = proptools.StringPtr(strings.Join(droidstubsArgs, " "))
Jiyong Parkc678ad32018-04-10 13:07:10 +0900552
553 // List of APIs identified from the provided source files are created. They are later
554 // compared against to the not-yet-released (a.k.a current) list of APIs and to the
555 // last-released (a.k.a numbered) list of API.
Paul Duffind1b3a922020-01-22 11:57:20 +0000556 currentApiFileName := apiScope.apiFilePrefix + "current.txt"
557 removedApiFileName := apiScope.apiFilePrefix + "removed.txt"
Paul Duffin749f98f2019-12-30 17:23:46 +0000558 apiDir := module.getApiDir()
559 currentApiFileName = path.Join(apiDir, currentApiFileName)
560 removedApiFileName = path.Join(apiDir, removedApiFileName)
Jiyong Parkc678ad32018-04-10 13:07:10 +0900561
Jiyong Park58c518b2018-05-12 22:29:12 +0900562 // check against the not-yet-release API
563 props.Check_api.Current.Api_file = proptools.StringPtr(currentApiFileName)
564 props.Check_api.Current.Removed_api_file = proptools.StringPtr(removedApiFileName)
Jiyong Park58c518b2018-05-12 22:29:12 +0900565
566 // check against the latest released API
567 props.Check_api.Last_released.Api_file = proptools.StringPtr(
568 module.latestApiFilegroupName(apiScope))
569 props.Check_api.Last_released.Removed_api_file = proptools.StringPtr(
570 module.latestRemovedApiFilegroupName(apiScope))
Inseob Kim38449af2019-02-28 14:24:05 +0900571 props.Check_api.Ignore_missing_latest_api = proptools.BoolPtr(true)
Jiyong Park58c518b2018-05-12 22:29:12 +0900572
Anton Hansson6bb88102020-03-27 19:43:19 +0000573 // Dist the api txt artifact for sdk builds.
574 if !Bool(module.sdkLibraryProperties.No_dist) {
575 props.Dist.Targets = []string{"sdk", "win_sdk"}
576 props.Dist.Dest = proptools.StringPtr(fmt.Sprintf("%v.txt", module.BaseModuleName()))
577 props.Dist.Dir = proptools.StringPtr(path.Join(module.apiDistPath(apiScope), "api"))
578 }
579
Colin Cross84dfc3d2019-09-25 11:33:01 -0700580 mctx.CreateModule(DroidstubsFactory, &props)
Jiyong Parkc678ad32018-04-10 13:07:10 +0900581}
582
Jooyung Hanb8fa86a2020-03-10 06:23:13 +0900583func (module *SdkLibrary) DepIsInSameApex(mctx android.BaseModuleContext, dep android.Module) bool {
584 depTag := mctx.OtherModuleDependencyTag(dep)
585 if depTag == xmlPermissionsFileTag {
586 return true
587 }
588 return module.Library.DepIsInSameApex(mctx, dep)
589}
590
Jiyong Parkc678ad32018-04-10 13:07:10 +0900591// Creates the xml file that publicizes the runtime library
Colin Crossf8b860a2019-04-16 14:43:28 -0700592func (module *SdkLibrary) createXmlFile(mctx android.LoadHookContext) {
Jiyong Parke3833882020-02-17 17:28:10 +0900593 props := struct {
Sundong Ahn0d7dff42019-12-04 12:53:44 +0900594 Name *string
Jiyong Parke3833882020-02-17 17:28:10 +0900595 Lib_name *string
Sundong Ahn0d7dff42019-12-04 12:53:44 +0900596 Soc_specific *bool
597 Device_specific *bool
598 Product_specific *bool
599 System_ext_specific *bool
Jooyung Hanb8fa86a2020-03-10 06:23:13 +0900600 Apex_available []string
Jiyong Parke3833882020-02-17 17:28:10 +0900601 }{
Jooyung Hanb8fa86a2020-03-10 06:23:13 +0900602 Name: proptools.StringPtr(module.xmlFileName()),
603 Lib_name: proptools.StringPtr(module.BaseModuleName()),
604 Apex_available: module.ApexProperties.Apex_available,
Jiyong Parkc678ad32018-04-10 13:07:10 +0900605 }
Jiyong Parke3833882020-02-17 17:28:10 +0900606
607 if module.SocSpecific() {
608 props.Soc_specific = proptools.BoolPtr(true)
609 } else if module.DeviceSpecific() {
610 props.Device_specific = proptools.BoolPtr(true)
611 } else if module.ProductSpecific() {
612 props.Product_specific = proptools.BoolPtr(true)
613 } else if module.SystemExtSpecific() {
614 props.System_ext_specific = proptools.BoolPtr(true)
615 }
616
617 mctx.CreateModule(sdkLibraryXmlFactory, &props)
Jiyong Parkc678ad32018-04-10 13:07:10 +0900618}
619
Paul Duffin50061512020-01-21 16:31:05 +0000620func PrebuiltJars(ctx android.BaseModuleContext, baseName string, s sdkSpec) android.Paths {
Jiyong Park6a927c42020-01-21 02:03:43 +0900621 var ver sdkVersion
622 var kind sdkKind
623 if s.usePrebuilt(ctx) {
624 ver = s.version
625 kind = s.kind
Jiyong Parkc678ad32018-04-10 13:07:10 +0900626 } else {
Jiyong Park6a927c42020-01-21 02:03:43 +0900627 // We don't have prebuilt SDK for the specific sdkVersion.
628 // Instead of breaking the build, fallback to use "system_current"
629 ver = sdkVersionCurrent
630 kind = sdkSystem
Sundong Ahn054b19a2018-10-19 13:46:09 +0900631 }
Jiyong Park6a927c42020-01-21 02:03:43 +0900632
633 dir := filepath.Join("prebuilts", "sdk", ver.String(), kind.String())
Paul Duffin50061512020-01-21 16:31:05 +0000634 jar := filepath.Join(dir, baseName+".jar")
Sundong Ahn054b19a2018-10-19 13:46:09 +0900635 jarPath := android.ExistentPathForSource(ctx, jar)
Sundong Ahnae418ac2019-02-28 15:01:28 +0900636 if !jarPath.Valid() {
Colin Cross07c88562020-01-07 09:34:44 -0800637 if ctx.Config().AllowMissingDependencies() {
638 return android.Paths{android.PathForSource(ctx, jar)}
639 } else {
Jiyong Park6a927c42020-01-21 02:03:43 +0900640 ctx.PropertyErrorf("sdk_library", "invalid sdk version %q, %q does not exist", s.raw, jar)
Colin Cross07c88562020-01-07 09:34:44 -0800641 }
Sundong Ahnae418ac2019-02-28 15:01:28 +0900642 return nil
643 }
Sundong Ahn054b19a2018-10-19 13:46:09 +0900644 return android.Paths{jarPath.Path()}
645}
646
Paul Duffind1b3a922020-01-22 11:57:20 +0000647func (module *SdkLibrary) sdkJars(
648 ctx android.BaseModuleContext,
649 sdkVersion sdkSpec,
650 headerJars bool) android.Paths {
651
Paul Duffin50061512020-01-21 16:31:05 +0000652 // If a specific numeric version has been requested then use prebuilt versions of the sdk.
653 if sdkVersion.version.isNumbered() {
654 return PrebuiltJars(ctx, module.BaseModuleName(), sdkVersion)
Sundong Ahn054b19a2018-10-19 13:46:09 +0900655 } else {
Paul Duffind1b3a922020-01-22 11:57:20 +0000656 if !sdkVersion.specified() {
657 if headerJars {
658 return module.Library.HeaderJars()
659 } else {
660 return module.Library.ImplementationJars()
661 }
662 }
Paul Duffin726d23c2020-01-22 16:30:37 +0000663 var apiScope *apiScope
Jiyong Park6a927c42020-01-21 02:03:43 +0900664 switch sdkVersion.kind {
665 case sdkSystem:
Paul Duffin726d23c2020-01-22 16:30:37 +0000666 apiScope = apiScopeSystem
667 case sdkTest:
668 apiScope = apiScopeTest
Jiyong Park6a927c42020-01-21 02:03:43 +0900669 case sdkPrivate:
Sundong Ahn054b19a2018-10-19 13:46:09 +0900670 return module.Library.HeaderJars()
Jiyong Park6a927c42020-01-21 02:03:43 +0900671 default:
Paul Duffin726d23c2020-01-22 16:30:37 +0000672 apiScope = apiScopePublic
Paul Duffind1b3a922020-01-22 11:57:20 +0000673 }
674
Paul Duffin726d23c2020-01-22 16:30:37 +0000675 paths := module.getScopePaths(apiScope)
Paul Duffind1b3a922020-01-22 11:57:20 +0000676 if headerJars {
677 return paths.stubsHeaderPath
678 } else {
679 return paths.stubsImplPath
Sundong Ahn054b19a2018-10-19 13:46:09 +0900680 }
Jiyong Parkc678ad32018-04-10 13:07:10 +0900681 }
682}
683
Sundong Ahn241cd372018-07-13 16:16:44 +0900684// to satisfy SdkLibraryDependency interface
Paul Duffind1b3a922020-01-22 11:57:20 +0000685func (module *SdkLibrary) SdkHeaderJars(ctx android.BaseModuleContext, sdkVersion sdkSpec) android.Paths {
686 return module.sdkJars(ctx, sdkVersion, true /*headerJars*/)
687}
688
689// to satisfy SdkLibraryDependency interface
Jiyong Park6a927c42020-01-21 02:03:43 +0900690func (module *SdkLibrary) SdkImplementationJars(ctx android.BaseModuleContext, sdkVersion sdkSpec) android.Paths {
Paul Duffind1b3a922020-01-22 11:57:20 +0000691 return module.sdkJars(ctx, sdkVersion, false /*headerJars*/)
Sundong Ahn241cd372018-07-13 16:16:44 +0900692}
693
Sundong Ahn80a87b32019-05-13 15:02:50 +0900694func (module *SdkLibrary) SetNoDist() {
695 module.sdkLibraryProperties.No_dist = proptools.BoolPtr(true)
696}
697
Colin Cross571cccf2019-02-04 11:22:08 -0800698var javaSdkLibrariesKey = android.NewOnceKey("javaSdkLibraries")
699
Jiyong Park82484c02018-04-23 21:41:26 +0900700func javaSdkLibraries(config android.Config) *[]string {
Colin Cross571cccf2019-02-04 11:22:08 -0800701 return config.Once(javaSdkLibrariesKey, func() interface{} {
Jiyong Park82484c02018-04-23 21:41:26 +0900702 return &[]string{}
703 }).(*[]string)
704}
705
Paul Duffin749f98f2019-12-30 17:23:46 +0000706func (module *SdkLibrary) getApiDir() string {
707 return proptools.StringDefault(module.sdkLibraryProperties.Api_dir, "api")
708}
709
Jiyong Parkc678ad32018-04-10 13:07:10 +0900710// For a java_sdk_library module, create internal modules for stubs, docs,
711// runtime libs and xml file. If requested, the stubs and docs are created twice
712// once for public API level and once for system API level
Colin Crossf8b860a2019-04-16 14:43:28 -0700713func (module *SdkLibrary) CreateInternalModules(mctx android.LoadHookContext) {
Inseob Kim6e93ac92019-03-21 17:43:49 +0900714 if len(module.Library.Module.properties.Srcs) == 0 {
Inseob Kimc0907f12019-02-08 21:00:45 +0900715 mctx.PropertyErrorf("srcs", "java_sdk_library must specify srcs")
Jooyung Han58f26ab2019-12-18 15:34:32 +0900716 return
Inseob Kimc0907f12019-02-08 21:00:45 +0900717 }
718
Paul Duffin37e0b772019-12-30 17:20:10 +0000719 // If this builds against standard libraries (i.e. is not part of the core libraries)
720 // then assume it provides both system and test apis. Otherwise, assume it does not and
721 // also assume it does not contribute to the dist build.
722 sdkDep := decodeSdkDep(mctx, sdkContext(&module.Library))
723 hasSystemAndTestApis := sdkDep.hasStandardLibs()
724 module.sdkLibraryProperties.Has_system_and_test_apis = hasSystemAndTestApis
725 module.sdkLibraryProperties.No_dist = proptools.BoolPtr(!hasSystemAndTestApis)
726
Inseob Kim8098faa2019-03-18 10:19:51 +0900727 missing_current_api := false
728
Paul Duffind1b3a922020-01-22 11:57:20 +0000729 activeScopes := module.getActiveApiScopes()
730
Paul Duffin749f98f2019-12-30 17:23:46 +0000731 apiDir := module.getApiDir()
Paul Duffind1b3a922020-01-22 11:57:20 +0000732 for _, scope := range activeScopes {
Inseob Kim8098faa2019-03-18 10:19:51 +0900733 for _, api := range []string{"current.txt", "removed.txt"} {
Paul Duffind1b3a922020-01-22 11:57:20 +0000734 path := path.Join(mctx.ModuleDir(), apiDir, scope.apiFilePrefix+api)
Inseob Kim8098faa2019-03-18 10:19:51 +0900735 p := android.ExistentPathForSource(mctx, path)
736 if !p.Valid() {
737 mctx.ModuleErrorf("Current api file %#v doesn't exist", path)
738 missing_current_api = true
739 }
740 }
741 }
742
743 if missing_current_api {
744 script := "build/soong/scripts/gen-java-current-api-files.sh"
745 p := android.ExistentPathForSource(mctx, script)
746
747 if !p.Valid() {
748 panic(fmt.Sprintf("script file %s doesn't exist", script))
749 }
750
751 mctx.ModuleErrorf("One or more current api files are missing. "+
752 "You can update them by:\n"+
Paul Duffin37e0b772019-12-30 17:20:10 +0000753 "%s %q %s && m update-api",
Paul Duffind1b3a922020-01-22 11:57:20 +0000754 script, filepath.Join(mctx.ModuleDir(), apiDir),
755 strings.Join(activeScopes.Strings(func(s *apiScope) string { return s.apiFilePrefix }), " "))
Inseob Kim8098faa2019-03-18 10:19:51 +0900756 return
757 }
758
Paul Duffind1b3a922020-01-22 11:57:20 +0000759 for _, scope := range activeScopes {
760 module.createStubsLibrary(mctx, scope)
761 module.createStubsSources(mctx, scope)
Inseob Kimc0907f12019-02-08 21:00:45 +0900762 }
763
Paul Duffin43db9be2019-12-30 17:35:49 +0000764 if !proptools.Bool(module.sdkLibraryProperties.Api_only) {
765 // for runtime
766 module.createXmlFile(mctx)
767
768 // record java_sdk_library modules so that they are exported to make
769 javaSdkLibraries := javaSdkLibraries(mctx.Config())
770 javaSdkLibrariesLock.Lock()
771 defer javaSdkLibrariesLock.Unlock()
772 *javaSdkLibraries = append(*javaSdkLibraries, module.BaseModuleName())
773 }
Inseob Kimc0907f12019-02-08 21:00:45 +0900774}
775
776func (module *SdkLibrary) InitSdkLibraryProperties() {
Sundong Ahn054b19a2018-10-19 13:46:09 +0900777 module.AddProperties(
778 &module.sdkLibraryProperties,
779 &module.Library.Module.properties,
780 &module.Library.Module.dexpreoptProperties,
781 &module.Library.Module.deviceProperties,
782 &module.Library.Module.protoProperties,
783 )
784
785 module.Library.Module.properties.Installable = proptools.BoolPtr(true)
786 module.Library.Module.deviceProperties.IsSDKLibrary = true
Inseob Kimc0907f12019-02-08 21:00:45 +0900787}
Sundong Ahn054b19a2018-10-19 13:46:09 +0900788
Jaewoong Jung4f158ee2019-07-11 10:05:35 -0700789// java_sdk_library is a special Java library that provides optional platform APIs to apps.
790// In practice, it can be viewed as a combination of several modules: 1) stubs library that clients
791// are linked against to, 2) droiddoc module that internally generates API stubs source files,
792// 3) the real runtime shared library that implements the APIs, and 4) XML file for adding
793// the runtime lib to the classpath at runtime if requested via <uses-library>.
Inseob Kimc0907f12019-02-08 21:00:45 +0900794func SdkLibraryFactory() android.Module {
795 module := &SdkLibrary{}
796 module.InitSdkLibraryProperties()
Jooyung Han58f26ab2019-12-18 15:34:32 +0900797 android.InitApexModule(module)
Sundong Ahn054b19a2018-10-19 13:46:09 +0900798 InitJavaModule(module, android.HostAndDeviceSupported)
Colin Crossf8b860a2019-04-16 14:43:28 -0700799 android.AddLoadHook(module, func(ctx android.LoadHookContext) { module.CreateInternalModules(ctx) })
Jiyong Parkc678ad32018-04-10 13:07:10 +0900800 return module
801}
Colin Cross79c7c262019-04-17 11:11:46 -0700802
803//
804// SDK library prebuilts
805//
806
Paul Duffin56d44902020-01-31 13:36:25 +0000807// Properties associated with each api scope.
808type sdkLibraryScopeProperties struct {
Colin Cross79c7c262019-04-17 11:11:46 -0700809 Jars []string `android:"path"`
810
811 Sdk_version *string
812
Colin Cross79c7c262019-04-17 11:11:46 -0700813 // List of shared java libs that this module has dependencies to
814 Libs []string
Colin Cross79c7c262019-04-17 11:11:46 -0700815}
816
Paul Duffin56d44902020-01-31 13:36:25 +0000817type sdkLibraryImportProperties struct {
Paul Duffinfcfd7912020-01-31 17:54:30 +0000818 // List of shared java libs, common to all scopes, that this module has
819 // dependencies to
820 Libs []string
Paul Duffin56d44902020-01-31 13:36:25 +0000821}
822
Colin Cross79c7c262019-04-17 11:11:46 -0700823type sdkLibraryImport struct {
824 android.ModuleBase
825 android.DefaultableModuleBase
826 prebuilt android.Prebuilt
Paul Duffin61871622020-02-10 13:37:10 +0000827 android.ApexModuleBase
828 android.SdkBase
Colin Cross79c7c262019-04-17 11:11:46 -0700829
830 properties sdkLibraryImportProperties
831
Paul Duffin6a2bd112020-04-07 19:27:04 +0100832 // Map from api scope to the scope specific property structure.
833 scopeProperties map[*apiScope]*sdkLibraryScopeProperties
834
Paul Duffin56d44902020-01-31 13:36:25 +0000835 commonToSdkLibraryAndImport
Colin Cross79c7c262019-04-17 11:11:46 -0700836}
837
838var _ SdkLibraryDependency = (*sdkLibraryImport)(nil)
839
Paul Duffin6a2bd112020-04-07 19:27:04 +0100840// The type of a structure that contains a field of type sdkLibraryScopeProperties
841// for each apiscope in allApiScopes, e.g. something like:
842// struct {
843// Public sdkLibraryScopeProperties
844// System sdkLibraryScopeProperties
845// ...
846// }
847var allScopeStructType = createAllScopePropertiesStructType()
848
849// Dynamically create a structure type for each apiscope in allApiScopes.
850func createAllScopePropertiesStructType() reflect.Type {
851 var fields []reflect.StructField
852 for _, apiScope := range allApiScopes {
853 field := reflect.StructField{
854 Name: apiScope.fieldName,
855 Type: reflect.TypeOf(sdkLibraryScopeProperties{}),
856 }
857 fields = append(fields, field)
858 }
859
860 return reflect.StructOf(fields)
861}
862
863// Create an instance of the scope specific structure type and return a map
864// from apiscope to a pointer to each scope specific field.
865func createPropertiesInstance() (interface{}, map[*apiScope]*sdkLibraryScopeProperties) {
866 allScopePropertiesPtr := reflect.New(allScopeStructType)
867 allScopePropertiesStruct := allScopePropertiesPtr.Elem()
868 scopeProperties := make(map[*apiScope]*sdkLibraryScopeProperties)
869
870 for _, apiScope := range allApiScopes {
871 field := allScopePropertiesStruct.FieldByName(apiScope.fieldName)
872 scopeProperties[apiScope] = field.Addr().Interface().(*sdkLibraryScopeProperties)
873 }
874
875 return allScopePropertiesPtr.Interface(), scopeProperties
876}
877
Jaewoong Jung4f158ee2019-07-11 10:05:35 -0700878// java_sdk_library_import imports a prebuilt java_sdk_library.
Colin Cross79c7c262019-04-17 11:11:46 -0700879func sdkLibraryImportFactory() android.Module {
880 module := &sdkLibraryImport{}
881
Paul Duffin6a2bd112020-04-07 19:27:04 +0100882 allScopeProperties, scopeToProperties := createPropertiesInstance()
883 module.scopeProperties = scopeToProperties
884 module.AddProperties(&module.properties, allScopeProperties)
Colin Cross79c7c262019-04-17 11:11:46 -0700885
Paul Duffin0bdcb272020-02-06 15:24:57 +0000886 android.InitPrebuiltModule(module, &[]string{""})
Paul Duffin61871622020-02-10 13:37:10 +0000887 android.InitApexModule(module)
888 android.InitSdkAwareModule(module)
Colin Cross79c7c262019-04-17 11:11:46 -0700889 InitJavaModule(module, android.HostAndDeviceSupported)
890
891 android.AddLoadHook(module, func(mctx android.LoadHookContext) { module.createInternalModules(mctx) })
892 return module
893}
894
895func (module *sdkLibraryImport) Prebuilt() *android.Prebuilt {
896 return &module.prebuilt
897}
898
899func (module *sdkLibraryImport) Name() string {
900 return module.prebuilt.Name(module.ModuleBase.Name())
901}
902
903func (module *sdkLibraryImport) createInternalModules(mctx android.LoadHookContext) {
Colin Cross79c7c262019-04-17 11:11:46 -0700904
Paul Duffin50061512020-01-21 16:31:05 +0000905 // If the build is configured to use prebuilts then force this to be preferred.
906 if mctx.Config().UnbundledBuildUsePrebuiltSdks() {
907 module.prebuilt.ForcePrefer()
908 }
909
Paul Duffin6a2bd112020-04-07 19:27:04 +0100910 for apiScope, scopeProperties := range module.scopeProperties {
Paul Duffin56d44902020-01-31 13:36:25 +0000911 if len(scopeProperties.Jars) == 0 {
912 continue
913 }
914
Paul Duffinf6155722020-04-09 00:07:11 +0100915 module.createJavaImportForStubs(mctx, apiScope, scopeProperties)
Paul Duffin56d44902020-01-31 13:36:25 +0000916 }
Colin Cross79c7c262019-04-17 11:11:46 -0700917
918 javaSdkLibraries := javaSdkLibraries(mctx.Config())
919 javaSdkLibrariesLock.Lock()
920 defer javaSdkLibrariesLock.Unlock()
921 *javaSdkLibraries = append(*javaSdkLibraries, module.BaseModuleName())
922}
923
Paul Duffinf6155722020-04-09 00:07:11 +0100924func (module *sdkLibraryImport) createJavaImportForStubs(mctx android.LoadHookContext, apiScope *apiScope, scopeProperties *sdkLibraryScopeProperties) {
925 // Creates a java import for the jar with ".stubs" suffix
926 props := struct {
927 Name *string
928 Soc_specific *bool
929 Device_specific *bool
930 Product_specific *bool
931 System_ext_specific *bool
932 Sdk_version *string
933 Libs []string
934 Jars []string
935 Prefer *bool
936 }{}
937 props.Name = proptools.StringPtr(apiScope.stubsModuleName(module.BaseModuleName()))
938 props.Sdk_version = scopeProperties.Sdk_version
939 // Prepend any of the libs from the legacy public properties to the libs for each of the
940 // scopes to avoid having to duplicate them in each scope.
941 props.Libs = append(module.properties.Libs, scopeProperties.Libs...)
942 props.Jars = scopeProperties.Jars
943 if module.SocSpecific() {
944 props.Soc_specific = proptools.BoolPtr(true)
945 } else if module.DeviceSpecific() {
946 props.Device_specific = proptools.BoolPtr(true)
947 } else if module.ProductSpecific() {
948 props.Product_specific = proptools.BoolPtr(true)
949 } else if module.SystemExtSpecific() {
950 props.System_ext_specific = proptools.BoolPtr(true)
951 }
952 // If the build should use prebuilt sdks then set prefer to true on the stubs library.
953 // That will cause the prebuilt version of the stubs to override the source version.
954 if mctx.Config().UnbundledBuildUsePrebuiltSdks() {
955 props.Prefer = proptools.BoolPtr(true)
956 }
957 mctx.CreateModule(ImportFactory, &props)
958}
959
Colin Cross79c7c262019-04-17 11:11:46 -0700960func (module *sdkLibraryImport) DepsMutator(ctx android.BottomUpMutatorContext) {
Paul Duffin6a2bd112020-04-07 19:27:04 +0100961 for apiScope, scopeProperties := range module.scopeProperties {
Paul Duffin56d44902020-01-31 13:36:25 +0000962 if len(scopeProperties.Jars) == 0 {
963 continue
964 }
965
966 // Add dependencies to the prebuilt stubs library
967 ctx.AddVariationDependencies(nil, apiScope.stubsTag, apiScope.stubsModuleName(module.BaseModuleName()))
968 }
Colin Cross79c7c262019-04-17 11:11:46 -0700969}
970
971func (module *sdkLibraryImport) GenerateAndroidBuildActions(ctx android.ModuleContext) {
972 // Record the paths to the prebuilt stubs library.
973 ctx.VisitDirectDeps(func(to android.Module) {
974 tag := ctx.OtherModuleDependencyTag(to)
975
Paul Duffin56d44902020-01-31 13:36:25 +0000976 if lib, ok := to.(Dependency); ok {
977 if scopeTag, ok := tag.(scopeDependencyTag); ok {
978 apiScope := scopeTag.apiScope
979 scopePaths := module.getScopePaths(apiScope)
980 scopePaths.stubsHeaderPath = lib.HeaderJars()
981 }
Colin Cross79c7c262019-04-17 11:11:46 -0700982 }
983 })
984}
985
Paul Duffin56d44902020-01-31 13:36:25 +0000986func (module *sdkLibraryImport) sdkJars(
987 ctx android.BaseModuleContext,
988 sdkVersion sdkSpec) android.Paths {
989
Paul Duffin50061512020-01-21 16:31:05 +0000990 // If a specific numeric version has been requested then use prebuilt versions of the sdk.
991 if sdkVersion.version.isNumbered() {
992 return PrebuiltJars(ctx, module.BaseModuleName(), sdkVersion)
993 }
994
Paul Duffin56d44902020-01-31 13:36:25 +0000995 var apiScope *apiScope
996 switch sdkVersion.kind {
997 case sdkSystem:
998 apiScope = apiScopeSystem
999 case sdkTest:
1000 apiScope = apiScopeTest
1001 default:
1002 apiScope = apiScopePublic
1003 }
1004
1005 paths := module.getScopePaths(apiScope)
1006 return paths.stubsHeaderPath
1007}
1008
Colin Cross79c7c262019-04-17 11:11:46 -07001009// to satisfy SdkLibraryDependency interface
Jiyong Park6a927c42020-01-21 02:03:43 +09001010func (module *sdkLibraryImport) SdkHeaderJars(ctx android.BaseModuleContext, sdkVersion sdkSpec) android.Paths {
Colin Cross79c7c262019-04-17 11:11:46 -07001011 // This module is just a wrapper for the prebuilt stubs.
Paul Duffin56d44902020-01-31 13:36:25 +00001012 return module.sdkJars(ctx, sdkVersion)
Colin Cross79c7c262019-04-17 11:11:46 -07001013}
1014
1015// to satisfy SdkLibraryDependency interface
Jiyong Park6a927c42020-01-21 02:03:43 +09001016func (module *sdkLibraryImport) SdkImplementationJars(ctx android.BaseModuleContext, sdkVersion sdkSpec) android.Paths {
Colin Cross79c7c262019-04-17 11:11:46 -07001017 // This module is just a wrapper for the stubs.
Paul Duffin56d44902020-01-31 13:36:25 +00001018 return module.sdkJars(ctx, sdkVersion)
Colin Cross79c7c262019-04-17 11:11:46 -07001019}
Jiyong Parke3833882020-02-17 17:28:10 +09001020
1021//
1022// java_sdk_library_xml
1023//
1024type sdkLibraryXml struct {
1025 android.ModuleBase
1026 android.DefaultableModuleBase
1027 android.ApexModuleBase
1028
1029 properties sdkLibraryXmlProperties
1030
1031 outputFilePath android.OutputPath
1032 installDirPath android.InstallPath
1033}
1034
1035type sdkLibraryXmlProperties struct {
1036 // canonical name of the lib
1037 Lib_name *string
1038}
1039
1040// java_sdk_library_xml builds the permission xml file for a java_sdk_library.
1041// Not to be used directly by users. java_sdk_library internally uses this.
1042func sdkLibraryXmlFactory() android.Module {
1043 module := &sdkLibraryXml{}
1044
1045 module.AddProperties(&module.properties)
1046
1047 android.InitApexModule(module)
1048 android.InitAndroidArchModule(module, android.DeviceSupported, android.MultilibCommon)
1049
1050 return module
1051}
1052
1053// from android.PrebuiltEtcModule
1054func (module *sdkLibraryXml) SubDir() string {
1055 return "permissions"
1056}
1057
1058// from android.PrebuiltEtcModule
1059func (module *sdkLibraryXml) OutputFile() android.OutputPath {
1060 return module.outputFilePath
1061}
1062
1063// from android.ApexModule
1064func (module *sdkLibraryXml) AvailableFor(what string) bool {
1065 return true
1066}
1067
1068func (module *sdkLibraryXml) DepsMutator(ctx android.BottomUpMutatorContext) {
1069 // do nothing
1070}
1071
1072// File path to the runtime implementation library
1073func (module *sdkLibraryXml) implPath() string {
1074 implName := proptools.String(module.properties.Lib_name)
1075 if apexName := module.ApexName(); apexName != "" {
1076 // TODO(b/146468504): ApexName() is only a soong module name, not apex name.
1077 // In most cases, this works fine. But when apex_name is set or override_apex is used
1078 // this can be wrong.
1079 return fmt.Sprintf("/apex/%s/javalib/%s.jar", apexName, implName)
1080 }
1081 partition := "system"
1082 if module.SocSpecific() {
1083 partition = "vendor"
1084 } else if module.DeviceSpecific() {
1085 partition = "odm"
1086 } else if module.ProductSpecific() {
1087 partition = "product"
1088 } else if module.SystemExtSpecific() {
1089 partition = "system_ext"
1090 }
1091 return "/" + partition + "/framework/" + implName + ".jar"
1092}
1093
1094func (module *sdkLibraryXml) GenerateAndroidBuildActions(ctx android.ModuleContext) {
1095 libName := proptools.String(module.properties.Lib_name)
1096 xmlContent := fmt.Sprintf(permissionsTemplate, libName, module.implPath())
1097
1098 module.outputFilePath = android.PathForModuleOut(ctx, libName+".xml").OutputPath
1099 rule := android.NewRuleBuilder()
1100 rule.Command().
1101 Text("/bin/bash -c \"echo -e '" + xmlContent + "'\" > ").
1102 Output(module.outputFilePath)
1103
1104 rule.Build(pctx, ctx, "java_sdk_xml", "Permission XML")
1105
1106 module.installDirPath = android.PathForModuleInstall(ctx, "etc", module.SubDir())
1107}
1108
1109func (module *sdkLibraryXml) AndroidMkEntries() []android.AndroidMkEntries {
1110 if !module.IsForPlatform() {
1111 return []android.AndroidMkEntries{android.AndroidMkEntries{
1112 Disabled: true,
1113 }}
1114 }
1115
1116 return []android.AndroidMkEntries{android.AndroidMkEntries{
1117 Class: "ETC",
1118 OutputFile: android.OptionalPathForPath(module.outputFilePath),
1119 ExtraEntries: []android.AndroidMkExtraEntriesFunc{
1120 func(entries *android.AndroidMkEntries) {
1121 entries.SetString("LOCAL_MODULE_TAGS", "optional")
1122 entries.SetString("LOCAL_MODULE_PATH", module.installDirPath.ToMakePath().String())
1123 entries.SetString("LOCAL_INSTALLED_MODULE_STEM", module.outputFilePath.Base())
1124 },
1125 },
1126 }}
1127}
Paul Duffin61871622020-02-10 13:37:10 +00001128
1129type sdkLibrarySdkMemberType struct {
1130 android.SdkMemberTypeBase
1131}
1132
1133func (s *sdkLibrarySdkMemberType) AddDependencies(mctx android.BottomUpMutatorContext, dependencyTag blueprint.DependencyTag, names []string) {
1134 mctx.AddVariationDependencies(nil, dependencyTag, names...)
1135}
1136
1137func (s *sdkLibrarySdkMemberType) IsInstance(module android.Module) bool {
1138 _, ok := module.(*SdkLibrary)
1139 return ok
1140}
1141
1142func (s *sdkLibrarySdkMemberType) AddPrebuiltModule(ctx android.SdkMemberContext, member android.SdkMember) android.BpModule {
1143 return ctx.SnapshotBuilder().AddPrebuiltModule(member, "java_sdk_library_import")
1144}
1145
1146func (s *sdkLibrarySdkMemberType) CreateVariantPropertiesStruct() android.SdkMemberProperties {
1147 return &sdkLibrarySdkMemberProperties{}
1148}
1149
1150type sdkLibrarySdkMemberProperties struct {
1151 android.SdkMemberPropertiesBase
1152
1153 // Scope to per scope properties.
1154 Scopes map[*apiScope]scopeProperties
1155
1156 // Additional libraries that the exported stubs libraries depend upon.
1157 Libs []string
1158}
1159
1160type scopeProperties struct {
1161 Jars android.Paths
1162 SdkVersion string
1163}
1164
1165func (s *sdkLibrarySdkMemberProperties) PopulateFromVariant(ctx android.SdkMemberContext, variant android.Module) {
1166 sdk := variant.(*SdkLibrary)
1167
1168 s.Scopes = make(map[*apiScope]scopeProperties)
1169 for _, apiScope := range allApiScopes {
1170 paths := sdk.getScopePaths(apiScope)
1171 jars := paths.stubsImplPath
1172 if len(jars) > 0 {
1173 properties := scopeProperties{}
1174 properties.Jars = jars
1175 properties.SdkVersion = apiScope.sdkVersion
1176 s.Scopes[apiScope] = properties
1177 }
1178 }
1179
1180 s.Libs = sdk.properties.Libs
1181}
1182
1183func (s *sdkLibrarySdkMemberProperties) AddToPropertySet(ctx android.SdkMemberContext, propertySet android.BpPropertySet) {
1184 for _, apiScope := range allApiScopes {
1185 if properties, ok := s.Scopes[apiScope]; ok {
1186 scopeSet := propertySet.AddPropertySet(apiScope.name)
1187
1188 var jars []string
1189 for _, p := range properties.Jars {
1190 dest := filepath.Join("sdk_library", s.OsPrefix(), apiScope.name, ctx.Name()+"-stubs.jar")
1191 ctx.SnapshotBuilder().CopyToSnapshot(p, dest)
1192 jars = append(jars, dest)
1193 }
1194 scopeSet.AddProperty("jars", jars)
1195
1196 if properties.SdkVersion != "" {
1197 scopeSet.AddProperty("sdk_version", properties.SdkVersion)
1198 }
1199 }
1200 }
1201
1202 if len(s.Libs) > 0 {
1203 propertySet.AddPropertyWithTag("libs", s.Libs, ctx.SnapshotBuilder().SdkMemberReferencePropertyTag(false))
1204 }
1205}