blob: 85d5bd3b7be4fe0f48c0bf496a1a54c41fcbaf91 [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 Duffin51a2bee2020-05-05 14:40:52 +010070 // The api scope that this scope extends.
71 extends *apiScope
72
Paul Duffin6a2bd112020-04-07 19:27:04 +010073 // The name of the field in the dynamically created structure.
74 fieldName string
75
Paul Duffind1b3a922020-01-22 11:57:20 +000076 // The tag to use to depend on the stubs library module.
77 stubsTag scopeDependencyTag
78
79 // The tag to use to depend on the stubs
80 apiFileTag scopeDependencyTag
81
82 // The scope specific prefix to add to the api file base of "current.txt" or "removed.txt".
83 apiFilePrefix string
84
85 // The scope specific prefix to add to the sdk library module name to construct a scope specific
86 // module name.
87 moduleSuffix string
88
Paul Duffind1b3a922020-01-22 11:57:20 +000089 // 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
Anton Hansson5ff28e52020-05-02 11:19:36 +010096
97 // Whether the api scope can be treated as unstable, and should skip compat checks.
98 unstable bool
Paul Duffind1b3a922020-01-22 11:57:20 +000099}
100
101// Initialize a scope, creating and adding appropriate dependency tags
102func initApiScope(scope *apiScope) *apiScope {
Paul Duffin6a2bd112020-04-07 19:27:04 +0100103 scope.fieldName = proptools.FieldNameForProperty(scope.name)
Paul Duffind1b3a922020-01-22 11:57:20 +0000104 scope.stubsTag = scopeDependencyTag{
105 name: scope.name + "-stubs",
106 apiScope: scope,
107 }
108 scope.apiFileTag = scopeDependencyTag{
109 name: scope.name + "-api",
110 apiScope: scope,
111 }
112 return scope
113}
114
115func (scope *apiScope) stubsModuleName(baseName string) string {
116 return baseName + sdkStubsLibrarySuffix + scope.moduleSuffix
117}
118
119func (scope *apiScope) docsModuleName(baseName string) string {
Paul Duffin91b883d2020-02-11 13:05:28 +0000120 return baseName + sdkStubsSourceSuffix + scope.moduleSuffix
Paul Duffind1b3a922020-01-22 11:57:20 +0000121}
122
123type apiScopes []*apiScope
124
125func (scopes apiScopes) Strings(accessor func(*apiScope) string) []string {
126 var list []string
127 for _, scope := range scopes {
128 list = append(list, accessor(scope))
129 }
130 return list
131}
132
Jiyong Parkc678ad32018-04-10 13:07:10 +0900133var (
Paul Duffind1b3a922020-01-22 11:57:20 +0000134 apiScopePublic = initApiScope(&apiScope{
135 name: "public",
136 sdkVersion: "current",
137 })
138 apiScopeSystem = initApiScope(&apiScope{
Anton Hanssone366fff2020-04-28 16:47:41 +0100139 name: "system",
Paul Duffin51a2bee2020-05-05 14:40:52 +0100140 extends: apiScopePublic,
Anton Hanssone366fff2020-04-28 16:47:41 +0100141 apiFilePrefix: "system-",
142 moduleSuffix: sdkSystemApiSuffix,
143 sdkVersion: "system_current",
144 droidstubsArgs: []string{"-showAnnotation android.annotation.SystemApi"},
Paul Duffind1b3a922020-01-22 11:57:20 +0000145 })
146 apiScopeTest = initApiScope(&apiScope{
Anton Hanssone366fff2020-04-28 16:47:41 +0100147 name: "test",
Paul Duffin51a2bee2020-05-05 14:40:52 +0100148 extends: apiScopePublic,
Anton Hanssone366fff2020-04-28 16:47:41 +0100149 apiFilePrefix: "test-",
150 moduleSuffix: sdkTestApiSuffix,
151 sdkVersion: "test_current",
152 droidstubsArgs: []string{"-showAnnotation android.annotation.TestApi"},
Anton Hansson5ff28e52020-05-02 11:19:36 +0100153 unstable: true,
Paul Duffind1b3a922020-01-22 11:57:20 +0000154 })
155 allApiScopes = apiScopes{
156 apiScopePublic,
157 apiScopeSystem,
158 apiScopeTest,
159 }
Jiyong Parkc678ad32018-04-10 13:07:10 +0900160)
161
Jiyong Park82484c02018-04-23 21:41:26 +0900162var (
163 javaSdkLibrariesLock sync.Mutex
164)
165
Jiyong Parkc678ad32018-04-10 13:07:10 +0900166// TODO: these are big features that are currently missing
Jiyong Park1be96912018-05-28 18:02:19 +0900167// 1) disallowing linking to the runtime shared lib
168// 2) HTML generation
Jiyong Parkc678ad32018-04-10 13:07:10 +0900169
170func init() {
Paul Duffin43dc1cc2019-12-19 11:18:54 +0000171 RegisterSdkLibraryBuildComponents(android.InitRegistrationContext)
Jiyong Parkc678ad32018-04-10 13:07:10 +0900172
Jiyong Park82484c02018-04-23 21:41:26 +0900173 android.RegisterMakeVarsProvider(pctx, func(ctx android.MakeVarsContext) {
174 javaSdkLibraries := javaSdkLibraries(ctx.Config())
175 sort.Strings(*javaSdkLibraries)
176 ctx.Strict("JAVA_SDK_LIBRARIES", strings.Join(*javaSdkLibraries, " "))
177 })
Paul Duffin61871622020-02-10 13:37:10 +0000178
179 // Register sdk member types.
180 android.RegisterSdkMemberType(&sdkLibrarySdkMemberType{
181 android.SdkMemberTypeBase{
182 PropertyName: "java_sdk_libs",
183 SupportsSdk: true,
184 },
185 })
Jiyong Parkc678ad32018-04-10 13:07:10 +0900186}
187
Paul Duffin43dc1cc2019-12-19 11:18:54 +0000188func RegisterSdkLibraryBuildComponents(ctx android.RegistrationContext) {
189 ctx.RegisterModuleType("java_sdk_library", SdkLibraryFactory)
190 ctx.RegisterModuleType("java_sdk_library_import", sdkLibraryImportFactory)
191}
192
Jiyong Parkc678ad32018-04-10 13:07:10 +0900193type sdkLibraryProperties struct {
Sundong Ahnf043cf62018-06-25 16:04:37 +0900194 // List of Java libraries that will be in the classpath when building stubs
195 Stub_only_libs []string `android:"arch_variant"`
196
Paul Duffin7a586d32019-12-30 17:09:34 +0000197 // list of package names that will be documented and publicized as API.
198 // This allows the API to be restricted to a subset of the source files provided.
199 // If this is unspecified then all the source files will be treated as being part
200 // of the API.
Jiyong Parkc678ad32018-04-10 13:07:10 +0900201 Api_packages []string
202
Jiyong Park5a2c9d72018-05-01 22:25:41 +0900203 // list of package names that must be hidden from the API
204 Hidden_api_packages []string
205
Paul Duffin749f98f2019-12-30 17:23:46 +0000206 // the relative path to the directory containing the api specification files.
207 // Defaults to "api".
208 Api_dir *string
209
Paul Duffin43db9be2019-12-30 17:35:49 +0000210 // If set to true there is no runtime library.
211 Api_only *bool
212
Paul Duffin11512472019-02-11 15:55:17 +0000213 // local files that are used within user customized droiddoc options.
214 Droiddoc_option_files []string
215
216 // additional droiddoc options
217 // Available variables for substitution:
218 //
219 // $(location <label>): the path to the droiddoc_option_files with name <label>
Sundong Ahndd567f92018-07-31 17:19:11 +0900220 Droiddoc_options []string
221
Sundong Ahn054b19a2018-10-19 13:46:09 +0900222 // a list of top-level directories containing files to merge qualifier annotations
223 // (i.e. those intended to be included in the stubs written) from.
224 Merge_annotations_dirs []string
225
226 // a list of top-level directories containing Java stub files to merge show/hide annotations from.
227 Merge_inclusion_annotations_dirs []string
228
229 // If set to true, the path of dist files is apistubs/core. Defaults to false.
230 Core_lib *bool
231
Sundong Ahn80a87b32019-05-13 15:02:50 +0900232 // don't create dist rules.
233 No_dist *bool `blueprint:"mutated"`
234
Paul Duffin37e0b772019-12-30 17:20:10 +0000235 // indicates whether system and test apis should be managed.
236 Has_system_and_test_apis bool `blueprint:"mutated"`
237
Jiyong Parkc678ad32018-04-10 13:07:10 +0900238 // TODO: determines whether to create HTML doc or not
239 //Html_doc *bool
240}
241
Paul Duffind1b3a922020-01-22 11:57:20 +0000242type scopePaths struct {
243 stubsHeaderPath android.Paths
244 stubsImplPath android.Paths
245 apiFilePath android.Path
Paul Duffinf488ef22020-04-09 00:10:17 +0100246 stubsSrcJar android.Path
Paul Duffind1b3a922020-01-22 11:57:20 +0000247}
248
Paul Duffin56d44902020-01-31 13:36:25 +0000249// Common code between sdk library and sdk library import
250type commonToSdkLibraryAndImport struct {
251 scopePaths map[*apiScope]*scopePaths
252}
253
254func (c *commonToSdkLibraryAndImport) getScopePaths(scope *apiScope) *scopePaths {
255 if c.scopePaths == nil {
256 c.scopePaths = make(map[*apiScope]*scopePaths)
257 }
258 paths := c.scopePaths[scope]
259 if paths == nil {
260 paths = &scopePaths{}
261 c.scopePaths[scope] = paths
262 }
263
264 return paths
265}
266
Inseob Kimc0907f12019-02-08 21:00:45 +0900267type SdkLibrary struct {
Sundong Ahn054b19a2018-10-19 13:46:09 +0900268 Library
Jiyong Parkc678ad32018-04-10 13:07:10 +0900269
Sundong Ahn054b19a2018-10-19 13:46:09 +0900270 sdkLibraryProperties sdkLibraryProperties
Jiyong Parkc678ad32018-04-10 13:07:10 +0900271
Paul Duffin56d44902020-01-31 13:36:25 +0000272 commonToSdkLibraryAndImport
Jiyong Parkc678ad32018-04-10 13:07:10 +0900273}
274
Inseob Kimc0907f12019-02-08 21:00:45 +0900275var _ Dependency = (*SdkLibrary)(nil)
276var _ SdkLibraryDependency = (*SdkLibrary)(nil)
Colin Cross897d2ed2019-02-11 14:03:51 -0800277
Paul Duffind1b3a922020-01-22 11:57:20 +0000278func (module *SdkLibrary) getActiveApiScopes() apiScopes {
279 if module.sdkLibraryProperties.Has_system_and_test_apis {
280 return allApiScopes
281 } else {
282 return apiScopes{apiScopePublic}
283 }
284}
285
Paul Duffine74ac732020-02-06 13:51:46 +0000286var xmlPermissionsFileTag = dependencyTag{name: "xml-permissions-file"}
287
Jiyong Parke3833882020-02-17 17:28:10 +0900288func IsXmlPermissionsFileDepTag(depTag blueprint.DependencyTag) bool {
289 if dt, ok := depTag.(dependencyTag); ok {
290 return dt == xmlPermissionsFileTag
291 }
292 return false
293}
294
Inseob Kimc0907f12019-02-08 21:00:45 +0900295func (module *SdkLibrary) DepsMutator(ctx android.BottomUpMutatorContext) {
Paul Duffind1b3a922020-01-22 11:57:20 +0000296 for _, apiScope := range module.getActiveApiScopes() {
297 // Add dependencies to the stubs library
Paul Duffin50061512020-01-21 16:31:05 +0000298 ctx.AddVariationDependencies(nil, apiScope.stubsTag, module.stubsName(apiScope))
Paul Duffind1b3a922020-01-22 11:57:20 +0000299
Paul Duffin50061512020-01-21 16:31:05 +0000300 // And the api file
Paul Duffind1b3a922020-01-22 11:57:20 +0000301 ctx.AddVariationDependencies(nil, apiScope.apiFileTag, module.docsName(apiScope))
Sundong Ahn054b19a2018-10-19 13:46:09 +0900302 }
303
Paul Duffine74ac732020-02-06 13:51:46 +0000304 if !proptools.Bool(module.sdkLibraryProperties.Api_only) {
305 // Add dependency to the rule for generating the xml permissions file
Jiyong Parke3833882020-02-17 17:28:10 +0900306 ctx.AddDependency(module, xmlPermissionsFileTag, module.xmlFileName())
Paul Duffine74ac732020-02-06 13:51:46 +0000307 }
308
Sundong Ahn054b19a2018-10-19 13:46:09 +0900309 module.Library.deps(ctx)
Jiyong Parkc678ad32018-04-10 13:07:10 +0900310}
311
Inseob Kimc0907f12019-02-08 21:00:45 +0900312func (module *SdkLibrary) GenerateAndroidBuildActions(ctx android.ModuleContext) {
Paul Duffin43db9be2019-12-30 17:35:49 +0000313 // Don't build an implementation library if this is api only.
314 if !proptools.Bool(module.sdkLibraryProperties.Api_only) {
315 module.Library.GenerateAndroidBuildActions(ctx)
316 }
Sundong Ahn054b19a2018-10-19 13:46:09 +0900317
Sundong Ahn57368eb2018-07-06 11:20:23 +0900318 // Record the paths to the header jars of the library (stubs and impl).
Paul Duffind1b3a922020-01-22 11:57:20 +0000319 // When this java_sdk_library is depended upon from others via "libs" property,
Jiyong Parkc678ad32018-04-10 13:07:10 +0900320 // the recorded paths will be returned depending on the link type of the caller.
321 ctx.VisitDirectDeps(func(to android.Module) {
322 otherName := ctx.OtherModuleName(to)
323 tag := ctx.OtherModuleDependencyTag(to)
324
Sundong Ahn57368eb2018-07-06 11:20:23 +0900325 if lib, ok := to.(Dependency); ok {
Paul Duffind1b3a922020-01-22 11:57:20 +0000326 if scopeTag, ok := tag.(scopeDependencyTag); ok {
327 apiScope := scopeTag.apiScope
328 scopePaths := module.getScopePaths(apiScope)
329 scopePaths.stubsHeaderPath = lib.HeaderJars()
330 scopePaths.stubsImplPath = lib.ImplementationJars()
Jiyong Parkc678ad32018-04-10 13:07:10 +0900331 }
332 }
Paul Duffinf488ef22020-04-09 00:10:17 +0100333 if doc, ok := to.(ApiStubsProvider); ok {
Paul Duffind1b3a922020-01-22 11:57:20 +0000334 if scopeTag, ok := tag.(scopeDependencyTag); ok {
335 apiScope := scopeTag.apiScope
336 scopePaths := module.getScopePaths(apiScope)
337 scopePaths.apiFilePath = doc.ApiFilePath()
Paul Duffinf488ef22020-04-09 00:10:17 +0100338 scopePaths.stubsSrcJar = doc.StubsSrcJar()
Paul Duffind1b3a922020-01-22 11:57:20 +0000339 } else {
Sundong Ahn20e998b2018-07-24 11:19:26 +0900340 ctx.ModuleErrorf("depends on module %q of unknown tag %q", otherName, tag)
341 }
342 }
Jiyong Parkc678ad32018-04-10 13:07:10 +0900343 })
344}
345
Jiyong Park0b0e1b92019-12-03 13:24:29 +0900346func (module *SdkLibrary) AndroidMkEntries() []android.AndroidMkEntries {
Paul Duffin43db9be2019-12-30 17:35:49 +0000347 if proptools.Bool(module.sdkLibraryProperties.Api_only) {
348 return nil
349 }
Jiyong Park0b0e1b92019-12-03 13:24:29 +0900350 entriesList := module.Library.AndroidMkEntries()
351 entries := &entriesList[0]
Jaewoong Jungb0c127c2019-08-29 14:56:03 -0700352 entries.Required = append(entries.Required, module.xmlFileName())
Jiyong Park0b0e1b92019-12-03 13:24:29 +0900353 return entriesList
Jiyong Park82484c02018-04-23 21:41:26 +0900354}
355
Jiyong Parkc678ad32018-04-10 13:07:10 +0900356// Module name of the stubs library
Paul Duffind1b3a922020-01-22 11:57:20 +0000357func (module *SdkLibrary) stubsName(apiScope *apiScope) string {
358 return apiScope.stubsModuleName(module.BaseModuleName())
Jiyong Parkc678ad32018-04-10 13:07:10 +0900359}
360
361// Module name of the docs
Paul Duffind1b3a922020-01-22 11:57:20 +0000362func (module *SdkLibrary) docsName(apiScope *apiScope) string {
363 return apiScope.docsModuleName(module.BaseModuleName())
Jiyong Parkc678ad32018-04-10 13:07:10 +0900364}
365
366// Module name of the runtime implementation library
Inseob Kimc0907f12019-02-08 21:00:45 +0900367func (module *SdkLibrary) implName() string {
Sundong Ahn054b19a2018-10-19 13:46:09 +0900368 return module.BaseModuleName()
Jiyong Parkc678ad32018-04-10 13:07:10 +0900369}
370
Jiyong Parkc678ad32018-04-10 13:07:10 +0900371// Module name of the XML file for the lib
Inseob Kimc0907f12019-02-08 21:00:45 +0900372func (module *SdkLibrary) xmlFileName() string {
Jiyong Parkc678ad32018-04-10 13:07:10 +0900373 return module.BaseModuleName() + sdkXmlFileSuffix
374}
375
Anton Hansson6bb88102020-03-27 19:43:19 +0000376// The dist path of the stub artifacts
377func (module *SdkLibrary) apiDistPath(apiScope *apiScope) string {
378 if module.ModuleBase.Owner() != "" {
379 return path.Join("apistubs", module.ModuleBase.Owner(), apiScope.name)
380 } else if Bool(module.sdkLibraryProperties.Core_lib) {
381 return path.Join("apistubs", "core", apiScope.name)
382 } else {
383 return path.Join("apistubs", "android", apiScope.name)
384 }
385}
386
Paul Duffin12ceb462019-12-24 20:31:31 +0000387// Get the sdk version for use when compiling the stubs library.
Paul Duffin2aaef532020-04-29 16:47:28 +0100388func (module *SdkLibrary) sdkVersionForStubsLibrary(mctx android.DefaultableHookContext, apiScope *apiScope) string {
Paul Duffin12ceb462019-12-24 20:31:31 +0000389 sdkDep := decodeSdkDep(mctx, sdkContext(&module.Library))
390 if sdkDep.hasStandardLibs() {
391 // If building against a standard sdk then use the sdk version appropriate for the scope.
Paul Duffind1b3a922020-01-22 11:57:20 +0000392 return apiScope.sdkVersion
Paul Duffin12ceb462019-12-24 20:31:31 +0000393 } else {
394 // Otherwise, use no system module.
395 return "none"
396 }
397}
398
Paul Duffind1b3a922020-01-22 11:57:20 +0000399func (module *SdkLibrary) latestApiFilegroupName(apiScope *apiScope) string {
400 return ":" + module.BaseModuleName() + ".api." + apiScope.name + ".latest"
Jiyong Park58c518b2018-05-12 22:29:12 +0900401}
Jiyong Parkc678ad32018-04-10 13:07:10 +0900402
Paul Duffind1b3a922020-01-22 11:57:20 +0000403func (module *SdkLibrary) latestRemovedApiFilegroupName(apiScope *apiScope) string {
404 return ":" + module.BaseModuleName() + "-removed.api." + apiScope.name + ".latest"
Jiyong Parkc678ad32018-04-10 13:07:10 +0900405}
406
407// Creates a static java library that has API stubs
Paul Duffin2aaef532020-04-29 16:47:28 +0100408func (module *SdkLibrary) createStubsLibrary(mctx android.DefaultableHookContext, apiScope *apiScope) {
Jiyong Parkc678ad32018-04-10 13:07:10 +0900409 props := struct {
Sundong Ahn0d7dff42019-12-04 12:53:44 +0900410 Name *string
411 Srcs []string
Paul Duffin367ab912019-12-23 19:40:36 +0000412 Installable *bool
Sundong Ahn0d7dff42019-12-04 12:53:44 +0900413 Sdk_version *string
Paul Duffin12ceb462019-12-24 20:31:31 +0000414 System_modules *string
Paul Duffinab8da5d2020-02-07 16:12:04 +0000415 Patch_module *string
Sundong Ahn0d7dff42019-12-04 12:53:44 +0900416 Libs []string
417 Soc_specific *bool
418 Device_specific *bool
419 Product_specific *bool
420 System_ext_specific *bool
421 Compile_dex *bool
Sundong Ahn0d7dff42019-12-04 12:53:44 +0900422 Java_version *string
423 Product_variables struct {
Jiyong Park82484c02018-04-23 21:41:26 +0900424 Pdk struct {
425 Enabled *bool
426 }
Jiyong Parkc678ad32018-04-10 13:07:10 +0900427 }
Sundong Ahn054b19a2018-10-19 13:46:09 +0900428 Openjdk9 struct {
429 Srcs []string
430 Javacflags []string
431 }
Anton Hansson6bb88102020-03-27 19:43:19 +0000432 Dist struct {
433 Targets []string
434 Dest *string
435 Dir *string
436 Tag *string
437 }
Jiyong Parkc678ad32018-04-10 13:07:10 +0900438 }{}
439
Jiyong Parkdf130542018-04-27 16:29:21 +0900440 props.Name = proptools.StringPtr(module.stubsName(apiScope))
Jiyong Parkc678ad32018-04-10 13:07:10 +0900441 // sources are generated from the droiddoc
Jiyong Parkdf130542018-04-27 16:29:21 +0900442 props.Srcs = []string{":" + module.docsName(apiScope)}
Paul Duffin12ceb462019-12-24 20:31:31 +0000443 sdkVersion := module.sdkVersionForStubsLibrary(mctx, apiScope)
Paul Duffin52d398a2019-06-11 12:31:14 +0100444 props.Sdk_version = proptools.StringPtr(sdkVersion)
Paul Duffin12ceb462019-12-24 20:31:31 +0000445 props.System_modules = module.Library.Module.deviceProperties.System_modules
Paul Duffinab8da5d2020-02-07 16:12:04 +0000446 props.Patch_module = module.Library.Module.properties.Patch_module
Paul Duffin367ab912019-12-23 19:40:36 +0000447 props.Installable = proptools.BoolPtr(false)
Sundong Ahn054b19a2018-10-19 13:46:09 +0900448 props.Libs = module.sdkLibraryProperties.Stub_only_libs
Jiyong Park82484c02018-04-23 21:41:26 +0900449 props.Product_variables.Pdk.Enabled = proptools.BoolPtr(false)
Sundong Ahn054b19a2018-10-19 13:46:09 +0900450 props.Openjdk9.Srcs = module.Library.Module.properties.Openjdk9.Srcs
451 props.Openjdk9.Javacflags = module.Library.Module.properties.Openjdk9.Javacflags
452 props.Java_version = module.Library.Module.properties.Java_version
453 if module.Library.Module.deviceProperties.Compile_dex != nil {
454 props.Compile_dex = module.Library.Module.deviceProperties.Compile_dex
Sundong Ahndd567f92018-07-31 17:19:11 +0900455 }
Jiyong Parkc678ad32018-04-10 13:07:10 +0900456
457 if module.SocSpecific() {
458 props.Soc_specific = proptools.BoolPtr(true)
459 } else if module.DeviceSpecific() {
460 props.Device_specific = proptools.BoolPtr(true)
461 } else if module.ProductSpecific() {
462 props.Product_specific = proptools.BoolPtr(true)
Sundong Ahn0d7dff42019-12-04 12:53:44 +0900463 } else if module.SystemExtSpecific() {
464 props.System_ext_specific = proptools.BoolPtr(true)
Jiyong Parkc678ad32018-04-10 13:07:10 +0900465 }
Anton Hansson6bb88102020-03-27 19:43:19 +0000466 // Dist the class jar artifact for sdk builds.
467 if !Bool(module.sdkLibraryProperties.No_dist) {
468 props.Dist.Targets = []string{"sdk", "win_sdk"}
469 props.Dist.Dest = proptools.StringPtr(fmt.Sprintf("%v.jar", module.BaseModuleName()))
470 props.Dist.Dir = proptools.StringPtr(module.apiDistPath(apiScope))
471 props.Dist.Tag = proptools.StringPtr(".jar")
472 }
Jiyong Parkc678ad32018-04-10 13:07:10 +0900473
Colin Cross84dfc3d2019-09-25 11:33:01 -0700474 mctx.CreateModule(LibraryFactory, &props)
Jiyong Parkc678ad32018-04-10 13:07:10 +0900475}
476
Paul Duffincbcfcaa2020-04-07 18:49:53 +0100477// Creates a droidstubs module that creates stubs source files from the given full source
Jiyong Parkc678ad32018-04-10 13:07:10 +0900478// files
Paul Duffin2aaef532020-04-29 16:47:28 +0100479func (module *SdkLibrary) createStubsSources(mctx android.DefaultableHookContext, apiScope *apiScope) {
Jiyong Parkc678ad32018-04-10 13:07:10 +0900480 props := struct {
Sundong Ahn054b19a2018-10-19 13:46:09 +0900481 Name *string
482 Srcs []string
483 Installable *bool
Paul Duffin52d398a2019-06-11 12:31:14 +0100484 Sdk_version *string
Paul Duffin12ceb462019-12-24 20:31:31 +0000485 System_modules *string
Sundong Ahn054b19a2018-10-19 13:46:09 +0900486 Libs []string
Paul Duffin11512472019-02-11 15:55:17 +0000487 Arg_files []string
Sundong Ahn054b19a2018-10-19 13:46:09 +0900488 Args *string
Sundong Ahn054b19a2018-10-19 13:46:09 +0900489 Java_version *string
490 Merge_annotations_dirs []string
491 Merge_inclusion_annotations_dirs []string
492 Check_api struct {
Inseob Kim38449af2019-02-28 14:24:05 +0900493 Current ApiToCheck
494 Last_released ApiToCheck
495 Ignore_missing_latest_api *bool
Jiyong Park58c518b2018-05-12 22:29:12 +0900496 }
Sundong Ahn1b92c822018-05-29 11:35:17 +0900497 Aidl struct {
498 Include_dirs []string
499 Local_include_dirs []string
500 }
Anton Hansson6bb88102020-03-27 19:43:19 +0000501 Dist struct {
502 Targets []string
503 Dest *string
504 Dir *string
505 }
Jiyong Parkc678ad32018-04-10 13:07:10 +0900506 }{}
507
Paul Duffinda364252020-04-28 14:08:32 +0100508 // The stubs source processing uses the same compile time classpath when extracting the
509 // API from the implementation library as it does when compiling it. i.e. the same
510 // * sdk version
511 // * system_modules
512 // * libs (static_libs/libs)
Paul Duffin250e6192019-06-07 10:44:37 +0100513
Jiyong Parkdf130542018-04-27 16:29:21 +0900514 props.Name = proptools.StringPtr(module.docsName(apiScope))
Sundong Ahn054b19a2018-10-19 13:46:09 +0900515 props.Srcs = append(props.Srcs, module.Library.Module.properties.Srcs...)
Paul Duffinda364252020-04-28 14:08:32 +0100516 props.Sdk_version = module.Library.Module.deviceProperties.Sdk_version
Paul Duffin12ceb462019-12-24 20:31:31 +0000517 props.System_modules = module.Library.Module.deviceProperties.System_modules
Jiyong Parkc678ad32018-04-10 13:07:10 +0900518 props.Installable = proptools.BoolPtr(false)
Sundong Ahne6f0b052018-06-05 16:46:14 +0900519 // A droiddoc module has only one Libs property and doesn't distinguish between
520 // shared libs and static libs. So we need to add both of these libs to Libs property.
Sundong Ahn054b19a2018-10-19 13:46:09 +0900521 props.Libs = module.Library.Module.properties.Libs
522 props.Libs = append(props.Libs, module.Library.Module.properties.Static_libs...)
523 props.Aidl.Include_dirs = module.Library.Module.deviceProperties.Aidl.Include_dirs
524 props.Aidl.Local_include_dirs = module.Library.Module.deviceProperties.Aidl.Local_include_dirs
Sundong Ahn054b19a2018-10-19 13:46:09 +0900525 props.Java_version = module.Library.Module.properties.Java_version
Jiyong Parkc678ad32018-04-10 13:07:10 +0900526
Sundong Ahn054b19a2018-10-19 13:46:09 +0900527 props.Merge_annotations_dirs = module.sdkLibraryProperties.Merge_annotations_dirs
528 props.Merge_inclusion_annotations_dirs = module.sdkLibraryProperties.Merge_inclusion_annotations_dirs
529
Paul Duffincbcfcaa2020-04-07 18:49:53 +0100530 droidstubsArgs := []string{}
Paul Duffin235ffff2019-12-24 10:41:30 +0000531 if len(module.sdkLibraryProperties.Api_packages) != 0 {
Paul Duffincbcfcaa2020-04-07 18:49:53 +0100532 droidstubsArgs = append(droidstubsArgs, "--stub-packages "+strings.Join(module.sdkLibraryProperties.Api_packages, ":"))
Paul Duffin235ffff2019-12-24 10:41:30 +0000533 }
534 if len(module.sdkLibraryProperties.Hidden_api_packages) != 0 {
Paul Duffincbcfcaa2020-04-07 18:49:53 +0100535 droidstubsArgs = append(droidstubsArgs,
Paul Duffin235ffff2019-12-24 10:41:30 +0000536 android.JoinWithPrefix(module.sdkLibraryProperties.Hidden_api_packages, " --hide-package "))
537 }
Paul Duffincbcfcaa2020-04-07 18:49:53 +0100538 droidstubsArgs = append(droidstubsArgs, module.sdkLibraryProperties.Droiddoc_options...)
Paul Duffin235ffff2019-12-24 10:41:30 +0000539 disabledWarnings := []string{
540 "MissingPermission",
541 "BroadcastBehavior",
542 "HiddenSuperclass",
543 "DeprecationMismatch",
544 "UnavailableSymbol",
545 "SdkConstant",
546 "HiddenTypeParameter",
547 "Todo",
548 "Typo",
549 }
Paul Duffincbcfcaa2020-04-07 18:49:53 +0100550 droidstubsArgs = append(droidstubsArgs, android.JoinWithPrefix(disabledWarnings, "--hide "))
Sundong Ahnfb2721f2018-09-17 13:23:09 +0900551
Paul Duffin3c7c3472020-04-07 18:50:10 +0100552 // Add in scope specific arguments.
553 droidstubsArgs = append(droidstubsArgs, apiScope.droidstubsArgs...)
Paul Duffin11512472019-02-11 15:55:17 +0000554 props.Arg_files = module.sdkLibraryProperties.Droiddoc_option_files
Paul Duffincbcfcaa2020-04-07 18:49:53 +0100555 props.Args = proptools.StringPtr(strings.Join(droidstubsArgs, " "))
Jiyong Parkc678ad32018-04-10 13:07:10 +0900556
557 // List of APIs identified from the provided source files are created. They are later
558 // compared against to the not-yet-released (a.k.a current) list of APIs and to the
559 // last-released (a.k.a numbered) list of API.
Paul Duffind1b3a922020-01-22 11:57:20 +0000560 currentApiFileName := apiScope.apiFilePrefix + "current.txt"
561 removedApiFileName := apiScope.apiFilePrefix + "removed.txt"
Paul Duffin749f98f2019-12-30 17:23:46 +0000562 apiDir := module.getApiDir()
563 currentApiFileName = path.Join(apiDir, currentApiFileName)
564 removedApiFileName = path.Join(apiDir, removedApiFileName)
Jiyong Parkc678ad32018-04-10 13:07:10 +0900565
Jiyong Park58c518b2018-05-12 22:29:12 +0900566 // check against the not-yet-release API
567 props.Check_api.Current.Api_file = proptools.StringPtr(currentApiFileName)
568 props.Check_api.Current.Removed_api_file = proptools.StringPtr(removedApiFileName)
Jiyong Park58c518b2018-05-12 22:29:12 +0900569
Anton Hansson5ff28e52020-05-02 11:19:36 +0100570 if !apiScope.unstable {
571 // check against the latest released API
572 props.Check_api.Last_released.Api_file = proptools.StringPtr(
573 module.latestApiFilegroupName(apiScope))
574 props.Check_api.Last_released.Removed_api_file = proptools.StringPtr(
575 module.latestRemovedApiFilegroupName(apiScope))
576 props.Check_api.Ignore_missing_latest_api = proptools.BoolPtr(true)
577 }
Jiyong Park58c518b2018-05-12 22:29:12 +0900578
Anton Hansson6bb88102020-03-27 19:43:19 +0000579 // Dist the api txt artifact for sdk builds.
580 if !Bool(module.sdkLibraryProperties.No_dist) {
581 props.Dist.Targets = []string{"sdk", "win_sdk"}
582 props.Dist.Dest = proptools.StringPtr(fmt.Sprintf("%v.txt", module.BaseModuleName()))
583 props.Dist.Dir = proptools.StringPtr(path.Join(module.apiDistPath(apiScope), "api"))
584 }
585
Colin Cross84dfc3d2019-09-25 11:33:01 -0700586 mctx.CreateModule(DroidstubsFactory, &props)
Jiyong Parkc678ad32018-04-10 13:07:10 +0900587}
588
Jooyung Hanb8fa86a2020-03-10 06:23:13 +0900589func (module *SdkLibrary) DepIsInSameApex(mctx android.BaseModuleContext, dep android.Module) bool {
590 depTag := mctx.OtherModuleDependencyTag(dep)
591 if depTag == xmlPermissionsFileTag {
592 return true
593 }
594 return module.Library.DepIsInSameApex(mctx, dep)
595}
596
Jiyong Parkc678ad32018-04-10 13:07:10 +0900597// Creates the xml file that publicizes the runtime library
Paul Duffin2aaef532020-04-29 16:47:28 +0100598func (module *SdkLibrary) createXmlFile(mctx android.DefaultableHookContext) {
Jiyong Parke3833882020-02-17 17:28:10 +0900599 props := struct {
Sundong Ahn0d7dff42019-12-04 12:53:44 +0900600 Name *string
Jiyong Parke3833882020-02-17 17:28:10 +0900601 Lib_name *string
Sundong Ahn0d7dff42019-12-04 12:53:44 +0900602 Soc_specific *bool
603 Device_specific *bool
604 Product_specific *bool
605 System_ext_specific *bool
Jooyung Hanb8fa86a2020-03-10 06:23:13 +0900606 Apex_available []string
Jiyong Parke3833882020-02-17 17:28:10 +0900607 }{
Jooyung Hanb8fa86a2020-03-10 06:23:13 +0900608 Name: proptools.StringPtr(module.xmlFileName()),
609 Lib_name: proptools.StringPtr(module.BaseModuleName()),
610 Apex_available: module.ApexProperties.Apex_available,
Jiyong Parkc678ad32018-04-10 13:07:10 +0900611 }
Jiyong Parke3833882020-02-17 17:28:10 +0900612
613 if module.SocSpecific() {
614 props.Soc_specific = proptools.BoolPtr(true)
615 } else if module.DeviceSpecific() {
616 props.Device_specific = proptools.BoolPtr(true)
617 } else if module.ProductSpecific() {
618 props.Product_specific = proptools.BoolPtr(true)
619 } else if module.SystemExtSpecific() {
620 props.System_ext_specific = proptools.BoolPtr(true)
621 }
622
623 mctx.CreateModule(sdkLibraryXmlFactory, &props)
Jiyong Parkc678ad32018-04-10 13:07:10 +0900624}
625
Paul Duffin50061512020-01-21 16:31:05 +0000626func PrebuiltJars(ctx android.BaseModuleContext, baseName string, s sdkSpec) android.Paths {
Jiyong Park6a927c42020-01-21 02:03:43 +0900627 var ver sdkVersion
628 var kind sdkKind
629 if s.usePrebuilt(ctx) {
630 ver = s.version
631 kind = s.kind
Jiyong Parkc678ad32018-04-10 13:07:10 +0900632 } else {
Jiyong Park6a927c42020-01-21 02:03:43 +0900633 // We don't have prebuilt SDK for the specific sdkVersion.
634 // Instead of breaking the build, fallback to use "system_current"
635 ver = sdkVersionCurrent
636 kind = sdkSystem
Sundong Ahn054b19a2018-10-19 13:46:09 +0900637 }
Jiyong Park6a927c42020-01-21 02:03:43 +0900638
639 dir := filepath.Join("prebuilts", "sdk", ver.String(), kind.String())
Paul Duffin50061512020-01-21 16:31:05 +0000640 jar := filepath.Join(dir, baseName+".jar")
Sundong Ahn054b19a2018-10-19 13:46:09 +0900641 jarPath := android.ExistentPathForSource(ctx, jar)
Sundong Ahnae418ac2019-02-28 15:01:28 +0900642 if !jarPath.Valid() {
Colin Cross07c88562020-01-07 09:34:44 -0800643 if ctx.Config().AllowMissingDependencies() {
644 return android.Paths{android.PathForSource(ctx, jar)}
645 } else {
Jiyong Park6a927c42020-01-21 02:03:43 +0900646 ctx.PropertyErrorf("sdk_library", "invalid sdk version %q, %q does not exist", s.raw, jar)
Colin Cross07c88562020-01-07 09:34:44 -0800647 }
Sundong Ahnae418ac2019-02-28 15:01:28 +0900648 return nil
649 }
Sundong Ahn054b19a2018-10-19 13:46:09 +0900650 return android.Paths{jarPath.Path()}
651}
652
Paul Duffind1b3a922020-01-22 11:57:20 +0000653func (module *SdkLibrary) sdkJars(
654 ctx android.BaseModuleContext,
655 sdkVersion sdkSpec,
656 headerJars bool) android.Paths {
657
Paul Duffin50061512020-01-21 16:31:05 +0000658 // If a specific numeric version has been requested then use prebuilt versions of the sdk.
659 if sdkVersion.version.isNumbered() {
660 return PrebuiltJars(ctx, module.BaseModuleName(), sdkVersion)
Sundong Ahn054b19a2018-10-19 13:46:09 +0900661 } else {
Paul Duffind1b3a922020-01-22 11:57:20 +0000662 if !sdkVersion.specified() {
663 if headerJars {
664 return module.Library.HeaderJars()
665 } else {
666 return module.Library.ImplementationJars()
667 }
668 }
Paul Duffin726d23c2020-01-22 16:30:37 +0000669 var apiScope *apiScope
Jiyong Park6a927c42020-01-21 02:03:43 +0900670 switch sdkVersion.kind {
671 case sdkSystem:
Paul Duffin726d23c2020-01-22 16:30:37 +0000672 apiScope = apiScopeSystem
673 case sdkTest:
674 apiScope = apiScopeTest
Jiyong Park6a927c42020-01-21 02:03:43 +0900675 case sdkPrivate:
Sundong Ahn054b19a2018-10-19 13:46:09 +0900676 return module.Library.HeaderJars()
Jiyong Park6a927c42020-01-21 02:03:43 +0900677 default:
Paul Duffin726d23c2020-01-22 16:30:37 +0000678 apiScope = apiScopePublic
Paul Duffind1b3a922020-01-22 11:57:20 +0000679 }
680
Paul Duffin726d23c2020-01-22 16:30:37 +0000681 paths := module.getScopePaths(apiScope)
Paul Duffind1b3a922020-01-22 11:57:20 +0000682 if headerJars {
683 return paths.stubsHeaderPath
684 } else {
685 return paths.stubsImplPath
Sundong Ahn054b19a2018-10-19 13:46:09 +0900686 }
Jiyong Parkc678ad32018-04-10 13:07:10 +0900687 }
688}
689
Sundong Ahn241cd372018-07-13 16:16:44 +0900690// to satisfy SdkLibraryDependency interface
Paul Duffind1b3a922020-01-22 11:57:20 +0000691func (module *SdkLibrary) SdkHeaderJars(ctx android.BaseModuleContext, sdkVersion sdkSpec) android.Paths {
692 return module.sdkJars(ctx, sdkVersion, true /*headerJars*/)
693}
694
695// to satisfy SdkLibraryDependency interface
Jiyong Park6a927c42020-01-21 02:03:43 +0900696func (module *SdkLibrary) SdkImplementationJars(ctx android.BaseModuleContext, sdkVersion sdkSpec) android.Paths {
Paul Duffind1b3a922020-01-22 11:57:20 +0000697 return module.sdkJars(ctx, sdkVersion, false /*headerJars*/)
Sundong Ahn241cd372018-07-13 16:16:44 +0900698}
699
Sundong Ahn80a87b32019-05-13 15:02:50 +0900700func (module *SdkLibrary) SetNoDist() {
701 module.sdkLibraryProperties.No_dist = proptools.BoolPtr(true)
702}
703
Colin Cross571cccf2019-02-04 11:22:08 -0800704var javaSdkLibrariesKey = android.NewOnceKey("javaSdkLibraries")
705
Jiyong Park82484c02018-04-23 21:41:26 +0900706func javaSdkLibraries(config android.Config) *[]string {
Colin Cross571cccf2019-02-04 11:22:08 -0800707 return config.Once(javaSdkLibrariesKey, func() interface{} {
Jiyong Park82484c02018-04-23 21:41:26 +0900708 return &[]string{}
709 }).(*[]string)
710}
711
Paul Duffin749f98f2019-12-30 17:23:46 +0000712func (module *SdkLibrary) getApiDir() string {
713 return proptools.StringDefault(module.sdkLibraryProperties.Api_dir, "api")
714}
715
Jiyong Parkc678ad32018-04-10 13:07:10 +0900716// For a java_sdk_library module, create internal modules for stubs, docs,
717// runtime libs and xml file. If requested, the stubs and docs are created twice
718// once for public API level and once for system API level
Paul Duffin2aaef532020-04-29 16:47:28 +0100719func (module *SdkLibrary) CreateInternalModules(mctx android.DefaultableHookContext) {
720 // If the module has been disabled then don't create any child modules.
721 if !module.Enabled() {
722 return
723 }
724
Inseob Kim6e93ac92019-03-21 17:43:49 +0900725 if len(module.Library.Module.properties.Srcs) == 0 {
Inseob Kimc0907f12019-02-08 21:00:45 +0900726 mctx.PropertyErrorf("srcs", "java_sdk_library must specify srcs")
Jooyung Han58f26ab2019-12-18 15:34:32 +0900727 return
Inseob Kimc0907f12019-02-08 21:00:45 +0900728 }
729
Paul Duffin37e0b772019-12-30 17:20:10 +0000730 // If this builds against standard libraries (i.e. is not part of the core libraries)
731 // then assume it provides both system and test apis. Otherwise, assume it does not and
732 // also assume it does not contribute to the dist build.
733 sdkDep := decodeSdkDep(mctx, sdkContext(&module.Library))
734 hasSystemAndTestApis := sdkDep.hasStandardLibs()
735 module.sdkLibraryProperties.Has_system_and_test_apis = hasSystemAndTestApis
736 module.sdkLibraryProperties.No_dist = proptools.BoolPtr(!hasSystemAndTestApis)
737
Inseob Kim8098faa2019-03-18 10:19:51 +0900738 missing_current_api := false
739
Paul Duffind1b3a922020-01-22 11:57:20 +0000740 activeScopes := module.getActiveApiScopes()
741
Paul Duffin749f98f2019-12-30 17:23:46 +0000742 apiDir := module.getApiDir()
Paul Duffind1b3a922020-01-22 11:57:20 +0000743 for _, scope := range activeScopes {
Inseob Kim8098faa2019-03-18 10:19:51 +0900744 for _, api := range []string{"current.txt", "removed.txt"} {
Paul Duffind1b3a922020-01-22 11:57:20 +0000745 path := path.Join(mctx.ModuleDir(), apiDir, scope.apiFilePrefix+api)
Inseob Kim8098faa2019-03-18 10:19:51 +0900746 p := android.ExistentPathForSource(mctx, path)
747 if !p.Valid() {
748 mctx.ModuleErrorf("Current api file %#v doesn't exist", path)
749 missing_current_api = true
750 }
751 }
752 }
753
754 if missing_current_api {
755 script := "build/soong/scripts/gen-java-current-api-files.sh"
756 p := android.ExistentPathForSource(mctx, script)
757
758 if !p.Valid() {
759 panic(fmt.Sprintf("script file %s doesn't exist", script))
760 }
761
762 mctx.ModuleErrorf("One or more current api files are missing. "+
763 "You can update them by:\n"+
Paul Duffin37e0b772019-12-30 17:20:10 +0000764 "%s %q %s && m update-api",
Paul Duffind1b3a922020-01-22 11:57:20 +0000765 script, filepath.Join(mctx.ModuleDir(), apiDir),
766 strings.Join(activeScopes.Strings(func(s *apiScope) string { return s.apiFilePrefix }), " "))
Inseob Kim8098faa2019-03-18 10:19:51 +0900767 return
768 }
769
Paul Duffind1b3a922020-01-22 11:57:20 +0000770 for _, scope := range activeScopes {
771 module.createStubsLibrary(mctx, scope)
772 module.createStubsSources(mctx, scope)
Inseob Kimc0907f12019-02-08 21:00:45 +0900773 }
774
Paul Duffin43db9be2019-12-30 17:35:49 +0000775 if !proptools.Bool(module.sdkLibraryProperties.Api_only) {
776 // for runtime
777 module.createXmlFile(mctx)
778
779 // record java_sdk_library modules so that they are exported to make
780 javaSdkLibraries := javaSdkLibraries(mctx.Config())
781 javaSdkLibrariesLock.Lock()
782 defer javaSdkLibrariesLock.Unlock()
783 *javaSdkLibraries = append(*javaSdkLibraries, module.BaseModuleName())
784 }
Inseob Kimc0907f12019-02-08 21:00:45 +0900785}
786
787func (module *SdkLibrary) InitSdkLibraryProperties() {
Sundong Ahn054b19a2018-10-19 13:46:09 +0900788 module.AddProperties(
789 &module.sdkLibraryProperties,
790 &module.Library.Module.properties,
791 &module.Library.Module.dexpreoptProperties,
792 &module.Library.Module.deviceProperties,
793 &module.Library.Module.protoProperties,
794 )
795
796 module.Library.Module.properties.Installable = proptools.BoolPtr(true)
797 module.Library.Module.deviceProperties.IsSDKLibrary = true
Inseob Kimc0907f12019-02-08 21:00:45 +0900798}
Sundong Ahn054b19a2018-10-19 13:46:09 +0900799
Jaewoong Jung4f158ee2019-07-11 10:05:35 -0700800// java_sdk_library is a special Java library that provides optional platform APIs to apps.
801// In practice, it can be viewed as a combination of several modules: 1) stubs library that clients
802// are linked against to, 2) droiddoc module that internally generates API stubs source files,
803// 3) the real runtime shared library that implements the APIs, and 4) XML file for adding
804// the runtime lib to the classpath at runtime if requested via <uses-library>.
Inseob Kimc0907f12019-02-08 21:00:45 +0900805func SdkLibraryFactory() android.Module {
806 module := &SdkLibrary{}
807 module.InitSdkLibraryProperties()
Jooyung Han58f26ab2019-12-18 15:34:32 +0900808 android.InitApexModule(module)
Sundong Ahn054b19a2018-10-19 13:46:09 +0900809 InitJavaModule(module, android.HostAndDeviceSupported)
Paul Duffin2aaef532020-04-29 16:47:28 +0100810 module.SetDefaultableHook(func(ctx android.DefaultableHookContext) { module.CreateInternalModules(ctx) })
Jiyong Parkc678ad32018-04-10 13:07:10 +0900811 return module
812}
Colin Cross79c7c262019-04-17 11:11:46 -0700813
814//
815// SDK library prebuilts
816//
817
Paul Duffin56d44902020-01-31 13:36:25 +0000818// Properties associated with each api scope.
819type sdkLibraryScopeProperties struct {
Colin Cross79c7c262019-04-17 11:11:46 -0700820 Jars []string `android:"path"`
821
822 Sdk_version *string
823
Colin Cross79c7c262019-04-17 11:11:46 -0700824 // List of shared java libs that this module has dependencies to
825 Libs []string
Paul Duffinf488ef22020-04-09 00:10:17 +0100826
827 // The stub sources.
828 Stub_srcs []string `android:"path"`
Colin Cross79c7c262019-04-17 11:11:46 -0700829}
830
Paul Duffin56d44902020-01-31 13:36:25 +0000831type sdkLibraryImportProperties struct {
Paul Duffinfcfd7912020-01-31 17:54:30 +0000832 // List of shared java libs, common to all scopes, that this module has
833 // dependencies to
834 Libs []string
Paul Duffin56d44902020-01-31 13:36:25 +0000835}
836
Colin Cross79c7c262019-04-17 11:11:46 -0700837type sdkLibraryImport struct {
838 android.ModuleBase
839 android.DefaultableModuleBase
840 prebuilt android.Prebuilt
Paul Duffin61871622020-02-10 13:37:10 +0000841 android.ApexModuleBase
842 android.SdkBase
Colin Cross79c7c262019-04-17 11:11:46 -0700843
844 properties sdkLibraryImportProperties
845
Paul Duffin6a2bd112020-04-07 19:27:04 +0100846 // Map from api scope to the scope specific property structure.
847 scopeProperties map[*apiScope]*sdkLibraryScopeProperties
848
Paul Duffin56d44902020-01-31 13:36:25 +0000849 commonToSdkLibraryAndImport
Colin Cross79c7c262019-04-17 11:11:46 -0700850}
851
852var _ SdkLibraryDependency = (*sdkLibraryImport)(nil)
853
Paul Duffin6a2bd112020-04-07 19:27:04 +0100854// The type of a structure that contains a field of type sdkLibraryScopeProperties
855// for each apiscope in allApiScopes, e.g. something like:
856// struct {
857// Public sdkLibraryScopeProperties
858// System sdkLibraryScopeProperties
859// ...
860// }
861var allScopeStructType = createAllScopePropertiesStructType()
862
863// Dynamically create a structure type for each apiscope in allApiScopes.
864func createAllScopePropertiesStructType() reflect.Type {
865 var fields []reflect.StructField
866 for _, apiScope := range allApiScopes {
867 field := reflect.StructField{
868 Name: apiScope.fieldName,
869 Type: reflect.TypeOf(sdkLibraryScopeProperties{}),
870 }
871 fields = append(fields, field)
872 }
873
874 return reflect.StructOf(fields)
875}
876
877// Create an instance of the scope specific structure type and return a map
878// from apiscope to a pointer to each scope specific field.
879func createPropertiesInstance() (interface{}, map[*apiScope]*sdkLibraryScopeProperties) {
880 allScopePropertiesPtr := reflect.New(allScopeStructType)
881 allScopePropertiesStruct := allScopePropertiesPtr.Elem()
882 scopeProperties := make(map[*apiScope]*sdkLibraryScopeProperties)
883
884 for _, apiScope := range allApiScopes {
885 field := allScopePropertiesStruct.FieldByName(apiScope.fieldName)
886 scopeProperties[apiScope] = field.Addr().Interface().(*sdkLibraryScopeProperties)
887 }
888
889 return allScopePropertiesPtr.Interface(), scopeProperties
890}
891
Jaewoong Jung4f158ee2019-07-11 10:05:35 -0700892// java_sdk_library_import imports a prebuilt java_sdk_library.
Colin Cross79c7c262019-04-17 11:11:46 -0700893func sdkLibraryImportFactory() android.Module {
894 module := &sdkLibraryImport{}
895
Paul Duffin6a2bd112020-04-07 19:27:04 +0100896 allScopeProperties, scopeToProperties := createPropertiesInstance()
897 module.scopeProperties = scopeToProperties
898 module.AddProperties(&module.properties, allScopeProperties)
Colin Cross79c7c262019-04-17 11:11:46 -0700899
Paul Duffin0bdcb272020-02-06 15:24:57 +0000900 android.InitPrebuiltModule(module, &[]string{""})
Paul Duffin61871622020-02-10 13:37:10 +0000901 android.InitApexModule(module)
902 android.InitSdkAwareModule(module)
Colin Cross79c7c262019-04-17 11:11:46 -0700903 InitJavaModule(module, android.HostAndDeviceSupported)
904
905 android.AddLoadHook(module, func(mctx android.LoadHookContext) { module.createInternalModules(mctx) })
906 return module
907}
908
909func (module *sdkLibraryImport) Prebuilt() *android.Prebuilt {
910 return &module.prebuilt
911}
912
913func (module *sdkLibraryImport) Name() string {
914 return module.prebuilt.Name(module.ModuleBase.Name())
915}
916
917func (module *sdkLibraryImport) createInternalModules(mctx android.LoadHookContext) {
Colin Cross79c7c262019-04-17 11:11:46 -0700918
Paul Duffin50061512020-01-21 16:31:05 +0000919 // If the build is configured to use prebuilts then force this to be preferred.
920 if mctx.Config().UnbundledBuildUsePrebuiltSdks() {
921 module.prebuilt.ForcePrefer()
922 }
923
Paul Duffin6a2bd112020-04-07 19:27:04 +0100924 for apiScope, scopeProperties := range module.scopeProperties {
Paul Duffin56d44902020-01-31 13:36:25 +0000925 if len(scopeProperties.Jars) == 0 {
926 continue
927 }
928
Paul Duffinf6155722020-04-09 00:07:11 +0100929 module.createJavaImportForStubs(mctx, apiScope, scopeProperties)
Paul Duffinf488ef22020-04-09 00:10:17 +0100930
931 module.createPrebuiltStubsSources(mctx, apiScope, scopeProperties)
Paul Duffin56d44902020-01-31 13:36:25 +0000932 }
Colin Cross79c7c262019-04-17 11:11:46 -0700933
934 javaSdkLibraries := javaSdkLibraries(mctx.Config())
935 javaSdkLibrariesLock.Lock()
936 defer javaSdkLibrariesLock.Unlock()
937 *javaSdkLibraries = append(*javaSdkLibraries, module.BaseModuleName())
938}
939
Paul Duffinf6155722020-04-09 00:07:11 +0100940func (module *sdkLibraryImport) createJavaImportForStubs(mctx android.LoadHookContext, apiScope *apiScope, scopeProperties *sdkLibraryScopeProperties) {
941 // Creates a java import for the jar with ".stubs" suffix
942 props := struct {
943 Name *string
944 Soc_specific *bool
945 Device_specific *bool
946 Product_specific *bool
947 System_ext_specific *bool
948 Sdk_version *string
949 Libs []string
950 Jars []string
951 Prefer *bool
952 }{}
953 props.Name = proptools.StringPtr(apiScope.stubsModuleName(module.BaseModuleName()))
954 props.Sdk_version = scopeProperties.Sdk_version
955 // Prepend any of the libs from the legacy public properties to the libs for each of the
956 // scopes to avoid having to duplicate them in each scope.
957 props.Libs = append(module.properties.Libs, scopeProperties.Libs...)
958 props.Jars = scopeProperties.Jars
959 if module.SocSpecific() {
960 props.Soc_specific = proptools.BoolPtr(true)
961 } else if module.DeviceSpecific() {
962 props.Device_specific = proptools.BoolPtr(true)
963 } else if module.ProductSpecific() {
964 props.Product_specific = proptools.BoolPtr(true)
965 } else if module.SystemExtSpecific() {
966 props.System_ext_specific = proptools.BoolPtr(true)
967 }
968 // If the build should use prebuilt sdks then set prefer to true on the stubs library.
969 // That will cause the prebuilt version of the stubs to override the source version.
970 if mctx.Config().UnbundledBuildUsePrebuiltSdks() {
971 props.Prefer = proptools.BoolPtr(true)
972 }
973 mctx.CreateModule(ImportFactory, &props)
974}
975
Paul Duffinf488ef22020-04-09 00:10:17 +0100976func (module *sdkLibraryImport) createPrebuiltStubsSources(mctx android.LoadHookContext, apiScope *apiScope, scopeProperties *sdkLibraryScopeProperties) {
977 props := struct {
978 Name *string
979 Srcs []string
980 }{}
981 props.Name = proptools.StringPtr(apiScope.docsModuleName(module.BaseModuleName()))
982 props.Srcs = scopeProperties.Stub_srcs
983 mctx.CreateModule(PrebuiltStubsSourcesFactory, &props)
984}
985
Colin Cross79c7c262019-04-17 11:11:46 -0700986func (module *sdkLibraryImport) DepsMutator(ctx android.BottomUpMutatorContext) {
Paul Duffin6a2bd112020-04-07 19:27:04 +0100987 for apiScope, scopeProperties := range module.scopeProperties {
Paul Duffin56d44902020-01-31 13:36:25 +0000988 if len(scopeProperties.Jars) == 0 {
989 continue
990 }
991
992 // Add dependencies to the prebuilt stubs library
993 ctx.AddVariationDependencies(nil, apiScope.stubsTag, apiScope.stubsModuleName(module.BaseModuleName()))
994 }
Colin Cross79c7c262019-04-17 11:11:46 -0700995}
996
997func (module *sdkLibraryImport) GenerateAndroidBuildActions(ctx android.ModuleContext) {
998 // Record the paths to the prebuilt stubs library.
999 ctx.VisitDirectDeps(func(to android.Module) {
1000 tag := ctx.OtherModuleDependencyTag(to)
1001
Paul Duffin56d44902020-01-31 13:36:25 +00001002 if lib, ok := to.(Dependency); ok {
1003 if scopeTag, ok := tag.(scopeDependencyTag); ok {
1004 apiScope := scopeTag.apiScope
1005 scopePaths := module.getScopePaths(apiScope)
1006 scopePaths.stubsHeaderPath = lib.HeaderJars()
1007 }
Colin Cross79c7c262019-04-17 11:11:46 -07001008 }
1009 })
1010}
1011
Paul Duffin56d44902020-01-31 13:36:25 +00001012func (module *sdkLibraryImport) sdkJars(
1013 ctx android.BaseModuleContext,
1014 sdkVersion sdkSpec) android.Paths {
1015
Paul Duffin50061512020-01-21 16:31:05 +00001016 // If a specific numeric version has been requested then use prebuilt versions of the sdk.
1017 if sdkVersion.version.isNumbered() {
1018 return PrebuiltJars(ctx, module.BaseModuleName(), sdkVersion)
1019 }
1020
Paul Duffin56d44902020-01-31 13:36:25 +00001021 var apiScope *apiScope
1022 switch sdkVersion.kind {
1023 case sdkSystem:
1024 apiScope = apiScopeSystem
1025 case sdkTest:
1026 apiScope = apiScopeTest
1027 default:
1028 apiScope = apiScopePublic
1029 }
1030
1031 paths := module.getScopePaths(apiScope)
1032 return paths.stubsHeaderPath
1033}
1034
Colin Cross79c7c262019-04-17 11:11:46 -07001035// to satisfy SdkLibraryDependency interface
Jiyong Park6a927c42020-01-21 02:03:43 +09001036func (module *sdkLibraryImport) SdkHeaderJars(ctx android.BaseModuleContext, sdkVersion sdkSpec) android.Paths {
Colin Cross79c7c262019-04-17 11:11:46 -07001037 // This module is just a wrapper for the prebuilt stubs.
Paul Duffin56d44902020-01-31 13:36:25 +00001038 return module.sdkJars(ctx, sdkVersion)
Colin Cross79c7c262019-04-17 11:11:46 -07001039}
1040
1041// to satisfy SdkLibraryDependency interface
Jiyong Park6a927c42020-01-21 02:03:43 +09001042func (module *sdkLibraryImport) SdkImplementationJars(ctx android.BaseModuleContext, sdkVersion sdkSpec) android.Paths {
Colin Cross79c7c262019-04-17 11:11:46 -07001043 // This module is just a wrapper for the stubs.
Paul Duffin56d44902020-01-31 13:36:25 +00001044 return module.sdkJars(ctx, sdkVersion)
Colin Cross79c7c262019-04-17 11:11:46 -07001045}
Jiyong Parke3833882020-02-17 17:28:10 +09001046
1047//
1048// java_sdk_library_xml
1049//
1050type sdkLibraryXml struct {
1051 android.ModuleBase
1052 android.DefaultableModuleBase
1053 android.ApexModuleBase
1054
1055 properties sdkLibraryXmlProperties
1056
1057 outputFilePath android.OutputPath
1058 installDirPath android.InstallPath
1059}
1060
1061type sdkLibraryXmlProperties struct {
1062 // canonical name of the lib
1063 Lib_name *string
1064}
1065
1066// java_sdk_library_xml builds the permission xml file for a java_sdk_library.
1067// Not to be used directly by users. java_sdk_library internally uses this.
1068func sdkLibraryXmlFactory() android.Module {
1069 module := &sdkLibraryXml{}
1070
1071 module.AddProperties(&module.properties)
1072
1073 android.InitApexModule(module)
1074 android.InitAndroidArchModule(module, android.DeviceSupported, android.MultilibCommon)
1075
1076 return module
1077}
1078
1079// from android.PrebuiltEtcModule
1080func (module *sdkLibraryXml) SubDir() string {
1081 return "permissions"
1082}
1083
1084// from android.PrebuiltEtcModule
1085func (module *sdkLibraryXml) OutputFile() android.OutputPath {
1086 return module.outputFilePath
1087}
1088
1089// from android.ApexModule
1090func (module *sdkLibraryXml) AvailableFor(what string) bool {
1091 return true
1092}
1093
1094func (module *sdkLibraryXml) DepsMutator(ctx android.BottomUpMutatorContext) {
1095 // do nothing
1096}
1097
1098// File path to the runtime implementation library
1099func (module *sdkLibraryXml) implPath() string {
1100 implName := proptools.String(module.properties.Lib_name)
1101 if apexName := module.ApexName(); apexName != "" {
1102 // TODO(b/146468504): ApexName() is only a soong module name, not apex name.
1103 // In most cases, this works fine. But when apex_name is set or override_apex is used
1104 // this can be wrong.
1105 return fmt.Sprintf("/apex/%s/javalib/%s.jar", apexName, implName)
1106 }
1107 partition := "system"
1108 if module.SocSpecific() {
1109 partition = "vendor"
1110 } else if module.DeviceSpecific() {
1111 partition = "odm"
1112 } else if module.ProductSpecific() {
1113 partition = "product"
1114 } else if module.SystemExtSpecific() {
1115 partition = "system_ext"
1116 }
1117 return "/" + partition + "/framework/" + implName + ".jar"
1118}
1119
1120func (module *sdkLibraryXml) GenerateAndroidBuildActions(ctx android.ModuleContext) {
1121 libName := proptools.String(module.properties.Lib_name)
1122 xmlContent := fmt.Sprintf(permissionsTemplate, libName, module.implPath())
1123
1124 module.outputFilePath = android.PathForModuleOut(ctx, libName+".xml").OutputPath
1125 rule := android.NewRuleBuilder()
1126 rule.Command().
1127 Text("/bin/bash -c \"echo -e '" + xmlContent + "'\" > ").
1128 Output(module.outputFilePath)
1129
1130 rule.Build(pctx, ctx, "java_sdk_xml", "Permission XML")
1131
1132 module.installDirPath = android.PathForModuleInstall(ctx, "etc", module.SubDir())
1133}
1134
1135func (module *sdkLibraryXml) AndroidMkEntries() []android.AndroidMkEntries {
1136 if !module.IsForPlatform() {
1137 return []android.AndroidMkEntries{android.AndroidMkEntries{
1138 Disabled: true,
1139 }}
1140 }
1141
1142 return []android.AndroidMkEntries{android.AndroidMkEntries{
1143 Class: "ETC",
1144 OutputFile: android.OptionalPathForPath(module.outputFilePath),
1145 ExtraEntries: []android.AndroidMkExtraEntriesFunc{
1146 func(entries *android.AndroidMkEntries) {
1147 entries.SetString("LOCAL_MODULE_TAGS", "optional")
1148 entries.SetString("LOCAL_MODULE_PATH", module.installDirPath.ToMakePath().String())
1149 entries.SetString("LOCAL_INSTALLED_MODULE_STEM", module.outputFilePath.Base())
1150 },
1151 },
1152 }}
1153}
Paul Duffin61871622020-02-10 13:37:10 +00001154
1155type sdkLibrarySdkMemberType struct {
1156 android.SdkMemberTypeBase
1157}
1158
1159func (s *sdkLibrarySdkMemberType) AddDependencies(mctx android.BottomUpMutatorContext, dependencyTag blueprint.DependencyTag, names []string) {
1160 mctx.AddVariationDependencies(nil, dependencyTag, names...)
1161}
1162
1163func (s *sdkLibrarySdkMemberType) IsInstance(module android.Module) bool {
1164 _, ok := module.(*SdkLibrary)
1165 return ok
1166}
1167
1168func (s *sdkLibrarySdkMemberType) AddPrebuiltModule(ctx android.SdkMemberContext, member android.SdkMember) android.BpModule {
1169 return ctx.SnapshotBuilder().AddPrebuiltModule(member, "java_sdk_library_import")
1170}
1171
1172func (s *sdkLibrarySdkMemberType) CreateVariantPropertiesStruct() android.SdkMemberProperties {
1173 return &sdkLibrarySdkMemberProperties{}
1174}
1175
1176type sdkLibrarySdkMemberProperties struct {
1177 android.SdkMemberPropertiesBase
1178
1179 // Scope to per scope properties.
1180 Scopes map[*apiScope]scopeProperties
1181
1182 // Additional libraries that the exported stubs libraries depend upon.
1183 Libs []string
Paul Duffinf488ef22020-04-09 00:10:17 +01001184
1185 // The Java stubs source files.
1186 Stub_srcs []string
Paul Duffin61871622020-02-10 13:37:10 +00001187}
1188
1189type scopeProperties struct {
Paul Duffinf488ef22020-04-09 00:10:17 +01001190 Jars android.Paths
1191 StubsSrcJar android.Path
1192 SdkVersion string
Paul Duffin61871622020-02-10 13:37:10 +00001193}
1194
1195func (s *sdkLibrarySdkMemberProperties) PopulateFromVariant(ctx android.SdkMemberContext, variant android.Module) {
1196 sdk := variant.(*SdkLibrary)
1197
1198 s.Scopes = make(map[*apiScope]scopeProperties)
1199 for _, apiScope := range allApiScopes {
1200 paths := sdk.getScopePaths(apiScope)
1201 jars := paths.stubsImplPath
1202 if len(jars) > 0 {
1203 properties := scopeProperties{}
1204 properties.Jars = jars
1205 properties.SdkVersion = apiScope.sdkVersion
Paul Duffinf488ef22020-04-09 00:10:17 +01001206 properties.StubsSrcJar = paths.stubsSrcJar
Paul Duffin61871622020-02-10 13:37:10 +00001207 s.Scopes[apiScope] = properties
1208 }
1209 }
1210
1211 s.Libs = sdk.properties.Libs
1212}
1213
1214func (s *sdkLibrarySdkMemberProperties) AddToPropertySet(ctx android.SdkMemberContext, propertySet android.BpPropertySet) {
1215 for _, apiScope := range allApiScopes {
1216 if properties, ok := s.Scopes[apiScope]; ok {
1217 scopeSet := propertySet.AddPropertySet(apiScope.name)
1218
Paul Duffinf488ef22020-04-09 00:10:17 +01001219 scopeDir := filepath.Join("sdk_library", s.OsPrefix(), apiScope.name)
1220
Paul Duffin61871622020-02-10 13:37:10 +00001221 var jars []string
1222 for _, p := range properties.Jars {
Paul Duffinf488ef22020-04-09 00:10:17 +01001223 dest := filepath.Join(scopeDir, ctx.Name()+"-stubs.jar")
Paul Duffin61871622020-02-10 13:37:10 +00001224 ctx.SnapshotBuilder().CopyToSnapshot(p, dest)
1225 jars = append(jars, dest)
1226 }
1227 scopeSet.AddProperty("jars", jars)
1228
Paul Duffinf488ef22020-04-09 00:10:17 +01001229 // Merge the stubs source jar into the snapshot zip so that when it is unpacked
1230 // the source files are also unpacked.
1231 snapshotRelativeDir := filepath.Join(scopeDir, ctx.Name()+"_stub_sources")
1232 ctx.SnapshotBuilder().UnzipToSnapshot(properties.StubsSrcJar, snapshotRelativeDir)
1233 scopeSet.AddProperty("stub_srcs", []string{snapshotRelativeDir})
1234
Paul Duffin61871622020-02-10 13:37:10 +00001235 if properties.SdkVersion != "" {
1236 scopeSet.AddProperty("sdk_version", properties.SdkVersion)
1237 }
1238 }
1239 }
1240
1241 if len(s.Libs) > 0 {
1242 propertySet.AddPropertyWithTag("libs", s.Libs, ctx.SnapshotBuilder().SdkMemberReferencePropertyTag(false))
1243 }
1244}