blob: f4acedb2f45177e95f390751ab04af65c87b9e56 [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 Duffin46a26a82020-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 Duffin46a26a82020-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 Duffin46a26a82020-04-07 19:27:04 +010070 // The name of the field in the dynamically created structure.
71 fieldName string
72
Paul Duffind1b3a922020-01-22 11:57:20 +000073 // The tag to use to depend on the stubs library module.
74 stubsTag scopeDependencyTag
75
76 // The tag to use to depend on the stubs
77 apiFileTag scopeDependencyTag
78
79 // The scope specific prefix to add to the api file base of "current.txt" or "removed.txt".
80 apiFilePrefix string
81
82 // The scope specific prefix to add to the sdk library module name to construct a scope specific
83 // module name.
84 moduleSuffix string
85
Paul Duffind1b3a922020-01-22 11:57:20 +000086 // SDK version that the stubs library is built against. Note that this is always
87 // *current. Older stubs library built with a numbered SDK version is created from
88 // the prebuilt jar.
89 sdkVersion string
Paul Duffin1fb487d2020-04-07 18:50:10 +010090
91 // Extra arguments to pass to droidstubs for this scope.
92 droidstubsArgs []string
Anton Hansson6478ac12020-05-02 11:19:36 +010093
94 // Whether the api scope can be treated as unstable, and should skip compat checks.
95 unstable bool
Paul Duffind1b3a922020-01-22 11:57:20 +000096}
97
98// Initialize a scope, creating and adding appropriate dependency tags
99func initApiScope(scope *apiScope) *apiScope {
Paul Duffin46a26a82020-04-07 19:27:04 +0100100 scope.fieldName = proptools.FieldNameForProperty(scope.name)
Paul Duffind1b3a922020-01-22 11:57:20 +0000101 scope.stubsTag = scopeDependencyTag{
102 name: scope.name + "-stubs",
103 apiScope: scope,
104 }
105 scope.apiFileTag = scopeDependencyTag{
106 name: scope.name + "-api",
107 apiScope: scope,
108 }
109 return scope
110}
111
112func (scope *apiScope) stubsModuleName(baseName string) string {
113 return baseName + sdkStubsLibrarySuffix + scope.moduleSuffix
114}
115
116func (scope *apiScope) docsModuleName(baseName string) string {
Paul Duffin91b883d2020-02-11 13:05:28 +0000117 return baseName + sdkStubsSourceSuffix + scope.moduleSuffix
Paul Duffind1b3a922020-01-22 11:57:20 +0000118}
119
120type apiScopes []*apiScope
121
122func (scopes apiScopes) Strings(accessor func(*apiScope) string) []string {
123 var list []string
124 for _, scope := range scopes {
125 list = append(list, accessor(scope))
126 }
127 return list
128}
129
Jiyong Parkc678ad32018-04-10 13:07:10 +0900130var (
Paul Duffind1b3a922020-01-22 11:57:20 +0000131 apiScopePublic = initApiScope(&apiScope{
132 name: "public",
133 sdkVersion: "current",
134 })
135 apiScopeSystem = initApiScope(&apiScope{
Anton Hansson6affb1f2020-04-28 16:47:41 +0100136 name: "system",
137 apiFilePrefix: "system-",
138 moduleSuffix: sdkSystemApiSuffix,
139 sdkVersion: "system_current",
140 droidstubsArgs: []string{"-showAnnotation android.annotation.SystemApi"},
Paul Duffind1b3a922020-01-22 11:57:20 +0000141 })
142 apiScopeTest = initApiScope(&apiScope{
Anton Hansson6affb1f2020-04-28 16:47:41 +0100143 name: "test",
144 apiFilePrefix: "test-",
145 moduleSuffix: sdkTestApiSuffix,
146 sdkVersion: "test_current",
147 droidstubsArgs: []string{"-showAnnotation android.annotation.TestApi"},
Anton Hansson6478ac12020-05-02 11:19:36 +0100148 unstable: true,
Paul Duffind1b3a922020-01-22 11:57:20 +0000149 })
150 allApiScopes = apiScopes{
151 apiScopePublic,
152 apiScopeSystem,
153 apiScopeTest,
154 }
Jiyong Parkc678ad32018-04-10 13:07:10 +0900155)
156
Jiyong Park82484c02018-04-23 21:41:26 +0900157var (
158 javaSdkLibrariesLock sync.Mutex
159)
160
Jiyong Parkc678ad32018-04-10 13:07:10 +0900161// TODO: these are big features that are currently missing
Jiyong Park1be96912018-05-28 18:02:19 +0900162// 1) disallowing linking to the runtime shared lib
163// 2) HTML generation
Jiyong Parkc678ad32018-04-10 13:07:10 +0900164
165func init() {
Paul Duffin43dc1cc2019-12-19 11:18:54 +0000166 RegisterSdkLibraryBuildComponents(android.InitRegistrationContext)
Jiyong Parkc678ad32018-04-10 13:07:10 +0900167
Jiyong Park82484c02018-04-23 21:41:26 +0900168 android.RegisterMakeVarsProvider(pctx, func(ctx android.MakeVarsContext) {
169 javaSdkLibraries := javaSdkLibraries(ctx.Config())
170 sort.Strings(*javaSdkLibraries)
171 ctx.Strict("JAVA_SDK_LIBRARIES", strings.Join(*javaSdkLibraries, " "))
172 })
Paul Duffindd46f712020-02-10 13:37:10 +0000173
174 // Register sdk member types.
175 android.RegisterSdkMemberType(&sdkLibrarySdkMemberType{
176 android.SdkMemberTypeBase{
177 PropertyName: "java_sdk_libs",
178 SupportsSdk: true,
179 },
180 })
Jiyong Parkc678ad32018-04-10 13:07:10 +0900181}
182
Paul Duffin43dc1cc2019-12-19 11:18:54 +0000183func RegisterSdkLibraryBuildComponents(ctx android.RegistrationContext) {
184 ctx.RegisterModuleType("java_sdk_library", SdkLibraryFactory)
185 ctx.RegisterModuleType("java_sdk_library_import", sdkLibraryImportFactory)
186}
187
Jiyong Parkc678ad32018-04-10 13:07:10 +0900188type sdkLibraryProperties struct {
Sundong Ahnf043cf62018-06-25 16:04:37 +0900189 // List of Java libraries that will be in the classpath when building stubs
190 Stub_only_libs []string `android:"arch_variant"`
191
Paul Duffin7a586d32019-12-30 17:09:34 +0000192 // list of package names that will be documented and publicized as API.
193 // This allows the API to be restricted to a subset of the source files provided.
194 // If this is unspecified then all the source files will be treated as being part
195 // of the API.
Jiyong Parkc678ad32018-04-10 13:07:10 +0900196 Api_packages []string
197
Jiyong Park5a2c9d72018-05-01 22:25:41 +0900198 // list of package names that must be hidden from the API
199 Hidden_api_packages []string
200
Paul Duffin749f98f2019-12-30 17:23:46 +0000201 // the relative path to the directory containing the api specification files.
202 // Defaults to "api".
203 Api_dir *string
204
Paul Duffin43db9be2019-12-30 17:35:49 +0000205 // If set to true there is no runtime library.
206 Api_only *bool
207
Paul Duffin11512472019-02-11 15:55:17 +0000208 // local files that are used within user customized droiddoc options.
209 Droiddoc_option_files []string
210
211 // additional droiddoc options
212 // Available variables for substitution:
213 //
214 // $(location <label>): the path to the droiddoc_option_files with name <label>
Sundong Ahndd567f92018-07-31 17:19:11 +0900215 Droiddoc_options []string
216
Sundong Ahn054b19a2018-10-19 13:46:09 +0900217 // a list of top-level directories containing files to merge qualifier annotations
218 // (i.e. those intended to be included in the stubs written) from.
219 Merge_annotations_dirs []string
220
221 // a list of top-level directories containing Java stub files to merge show/hide annotations from.
222 Merge_inclusion_annotations_dirs []string
223
224 // If set to true, the path of dist files is apistubs/core. Defaults to false.
225 Core_lib *bool
226
Sundong Ahn80a87b32019-05-13 15:02:50 +0900227 // don't create dist rules.
228 No_dist *bool `blueprint:"mutated"`
229
Paul Duffin37e0b772019-12-30 17:20:10 +0000230 // indicates whether system and test apis should be managed.
231 Has_system_and_test_apis bool `blueprint:"mutated"`
232
Jiyong Parkc678ad32018-04-10 13:07:10 +0900233 // TODO: determines whether to create HTML doc or not
234 //Html_doc *bool
235}
236
Paul Duffind1b3a922020-01-22 11:57:20 +0000237type scopePaths struct {
Paul Duffin1fd005d2020-04-09 01:08:11 +0100238 stubsHeaderPath android.Paths
239 stubsImplPath android.Paths
240 currentApiFilePath android.Path
241 removedApiFilePath android.Path
242 stubsSrcJar android.Path
Paul Duffind1b3a922020-01-22 11:57:20 +0000243}
244
Paul Duffin56d44902020-01-31 13:36:25 +0000245// Common code between sdk library and sdk library import
246type commonToSdkLibraryAndImport struct {
247 scopePaths map[*apiScope]*scopePaths
248}
249
250func (c *commonToSdkLibraryAndImport) getScopePaths(scope *apiScope) *scopePaths {
251 if c.scopePaths == nil {
252 c.scopePaths = make(map[*apiScope]*scopePaths)
253 }
254 paths := c.scopePaths[scope]
255 if paths == nil {
256 paths = &scopePaths{}
257 c.scopePaths[scope] = paths
258 }
259
260 return paths
261}
262
Inseob Kimc0907f12019-02-08 21:00:45 +0900263type SdkLibrary struct {
Sundong Ahn054b19a2018-10-19 13:46:09 +0900264 Library
Jiyong Parkc678ad32018-04-10 13:07:10 +0900265
Sundong Ahn054b19a2018-10-19 13:46:09 +0900266 sdkLibraryProperties sdkLibraryProperties
Jiyong Parkc678ad32018-04-10 13:07:10 +0900267
Paul Duffin56d44902020-01-31 13:36:25 +0000268 commonToSdkLibraryAndImport
Jiyong Parkc678ad32018-04-10 13:07:10 +0900269}
270
Inseob Kimc0907f12019-02-08 21:00:45 +0900271var _ Dependency = (*SdkLibrary)(nil)
272var _ SdkLibraryDependency = (*SdkLibrary)(nil)
Colin Cross897d2ed2019-02-11 14:03:51 -0800273
Paul Duffind1b3a922020-01-22 11:57:20 +0000274func (module *SdkLibrary) getActiveApiScopes() apiScopes {
275 if module.sdkLibraryProperties.Has_system_and_test_apis {
276 return allApiScopes
277 } else {
278 return apiScopes{apiScopePublic}
279 }
280}
281
Paul Duffine74ac732020-02-06 13:51:46 +0000282var xmlPermissionsFileTag = dependencyTag{name: "xml-permissions-file"}
283
Jiyong Parke3833882020-02-17 17:28:10 +0900284func IsXmlPermissionsFileDepTag(depTag blueprint.DependencyTag) bool {
285 if dt, ok := depTag.(dependencyTag); ok {
286 return dt == xmlPermissionsFileTag
287 }
288 return false
289}
290
Inseob Kimc0907f12019-02-08 21:00:45 +0900291func (module *SdkLibrary) DepsMutator(ctx android.BottomUpMutatorContext) {
Paul Duffind1b3a922020-01-22 11:57:20 +0000292 for _, apiScope := range module.getActiveApiScopes() {
293 // Add dependencies to the stubs library
Paul Duffin50061512020-01-21 16:31:05 +0000294 ctx.AddVariationDependencies(nil, apiScope.stubsTag, module.stubsName(apiScope))
Paul Duffind1b3a922020-01-22 11:57:20 +0000295
Paul Duffin50061512020-01-21 16:31:05 +0000296 // And the api file
Paul Duffind1b3a922020-01-22 11:57:20 +0000297 ctx.AddVariationDependencies(nil, apiScope.apiFileTag, module.docsName(apiScope))
Sundong Ahn054b19a2018-10-19 13:46:09 +0900298 }
299
Paul Duffine74ac732020-02-06 13:51:46 +0000300 if !proptools.Bool(module.sdkLibraryProperties.Api_only) {
301 // Add dependency to the rule for generating the xml permissions file
Jiyong Parke3833882020-02-17 17:28:10 +0900302 ctx.AddDependency(module, xmlPermissionsFileTag, module.xmlFileName())
Paul Duffine74ac732020-02-06 13:51:46 +0000303 }
304
Sundong Ahn054b19a2018-10-19 13:46:09 +0900305 module.Library.deps(ctx)
Jiyong Parkc678ad32018-04-10 13:07:10 +0900306}
307
Inseob Kimc0907f12019-02-08 21:00:45 +0900308func (module *SdkLibrary) GenerateAndroidBuildActions(ctx android.ModuleContext) {
Paul Duffin43db9be2019-12-30 17:35:49 +0000309 // Don't build an implementation library if this is api only.
310 if !proptools.Bool(module.sdkLibraryProperties.Api_only) {
311 module.Library.GenerateAndroidBuildActions(ctx)
312 }
Sundong Ahn054b19a2018-10-19 13:46:09 +0900313
Sundong Ahn57368eb2018-07-06 11:20:23 +0900314 // Record the paths to the header jars of the library (stubs and impl).
Paul Duffind1b3a922020-01-22 11:57:20 +0000315 // When this java_sdk_library is depended upon from others via "libs" property,
Jiyong Parkc678ad32018-04-10 13:07:10 +0900316 // the recorded paths will be returned depending on the link type of the caller.
317 ctx.VisitDirectDeps(func(to android.Module) {
318 otherName := ctx.OtherModuleName(to)
319 tag := ctx.OtherModuleDependencyTag(to)
320
Sundong Ahn57368eb2018-07-06 11:20:23 +0900321 if lib, ok := to.(Dependency); ok {
Paul Duffind1b3a922020-01-22 11:57:20 +0000322 if scopeTag, ok := tag.(scopeDependencyTag); ok {
323 apiScope := scopeTag.apiScope
324 scopePaths := module.getScopePaths(apiScope)
325 scopePaths.stubsHeaderPath = lib.HeaderJars()
326 scopePaths.stubsImplPath = lib.ImplementationJars()
Jiyong Parkc678ad32018-04-10 13:07:10 +0900327 }
328 }
Paul Duffin3d1248c2020-04-09 00:10:17 +0100329 if doc, ok := to.(ApiStubsProvider); ok {
Paul Duffind1b3a922020-01-22 11:57:20 +0000330 if scopeTag, ok := tag.(scopeDependencyTag); ok {
331 apiScope := scopeTag.apiScope
332 scopePaths := module.getScopePaths(apiScope)
Paul Duffin1fd005d2020-04-09 01:08:11 +0100333 scopePaths.currentApiFilePath = doc.ApiFilePath()
334 scopePaths.removedApiFilePath = doc.RemovedApiFilePath()
Paul Duffin3d1248c2020-04-09 00:10:17 +0100335 scopePaths.stubsSrcJar = doc.StubsSrcJar()
Paul Duffind1b3a922020-01-22 11:57:20 +0000336 } else {
Sundong Ahn20e998b2018-07-24 11:19:26 +0900337 ctx.ModuleErrorf("depends on module %q of unknown tag %q", otherName, tag)
338 }
339 }
Jiyong Parkc678ad32018-04-10 13:07:10 +0900340 })
341}
342
Jiyong Park0b0e1b92019-12-03 13:24:29 +0900343func (module *SdkLibrary) AndroidMkEntries() []android.AndroidMkEntries {
Paul Duffin43db9be2019-12-30 17:35:49 +0000344 if proptools.Bool(module.sdkLibraryProperties.Api_only) {
345 return nil
346 }
Jiyong Park0b0e1b92019-12-03 13:24:29 +0900347 entriesList := module.Library.AndroidMkEntries()
348 entries := &entriesList[0]
Jaewoong Jungb0c127c2019-08-29 14:56:03 -0700349 entries.Required = append(entries.Required, module.xmlFileName())
Jiyong Park0b0e1b92019-12-03 13:24:29 +0900350 return entriesList
Jiyong Park82484c02018-04-23 21:41:26 +0900351}
352
Jiyong Parkc678ad32018-04-10 13:07:10 +0900353// Module name of the stubs library
Paul Duffind1b3a922020-01-22 11:57:20 +0000354func (module *SdkLibrary) stubsName(apiScope *apiScope) string {
355 return apiScope.stubsModuleName(module.BaseModuleName())
Jiyong Parkc678ad32018-04-10 13:07:10 +0900356}
357
358// Module name of the docs
Paul Duffind1b3a922020-01-22 11:57:20 +0000359func (module *SdkLibrary) docsName(apiScope *apiScope) string {
360 return apiScope.docsModuleName(module.BaseModuleName())
Jiyong Parkc678ad32018-04-10 13:07:10 +0900361}
362
363// Module name of the runtime implementation library
Inseob Kimc0907f12019-02-08 21:00:45 +0900364func (module *SdkLibrary) implName() string {
Sundong Ahn054b19a2018-10-19 13:46:09 +0900365 return module.BaseModuleName()
Jiyong Parkc678ad32018-04-10 13:07:10 +0900366}
367
Jiyong Parkc678ad32018-04-10 13:07:10 +0900368// Module name of the XML file for the lib
Inseob Kimc0907f12019-02-08 21:00:45 +0900369func (module *SdkLibrary) xmlFileName() string {
Jiyong Parkc678ad32018-04-10 13:07:10 +0900370 return module.BaseModuleName() + sdkXmlFileSuffix
371}
372
Anton Hansson5fd5d242020-03-27 19:43:19 +0000373// The dist path of the stub artifacts
374func (module *SdkLibrary) apiDistPath(apiScope *apiScope) string {
375 if module.ModuleBase.Owner() != "" {
376 return path.Join("apistubs", module.ModuleBase.Owner(), apiScope.name)
377 } else if Bool(module.sdkLibraryProperties.Core_lib) {
378 return path.Join("apistubs", "core", apiScope.name)
379 } else {
380 return path.Join("apistubs", "android", apiScope.name)
381 }
382}
383
Paul Duffin12ceb462019-12-24 20:31:31 +0000384// Get the sdk version for use when compiling the stubs library.
Paul Duffinf0229202020-04-29 16:47:28 +0100385func (module *SdkLibrary) sdkVersionForStubsLibrary(mctx android.DefaultableHookContext, apiScope *apiScope) string {
Paul Duffin12ceb462019-12-24 20:31:31 +0000386 sdkDep := decodeSdkDep(mctx, sdkContext(&module.Library))
387 if sdkDep.hasStandardLibs() {
388 // If building against a standard sdk then use the sdk version appropriate for the scope.
Paul Duffind1b3a922020-01-22 11:57:20 +0000389 return apiScope.sdkVersion
Paul Duffin12ceb462019-12-24 20:31:31 +0000390 } else {
391 // Otherwise, use no system module.
392 return "none"
393 }
394}
395
Paul Duffind1b3a922020-01-22 11:57:20 +0000396func (module *SdkLibrary) latestApiFilegroupName(apiScope *apiScope) string {
397 return ":" + module.BaseModuleName() + ".api." + apiScope.name + ".latest"
Jiyong Park58c518b2018-05-12 22:29:12 +0900398}
Jiyong Parkc678ad32018-04-10 13:07:10 +0900399
Paul Duffind1b3a922020-01-22 11:57:20 +0000400func (module *SdkLibrary) latestRemovedApiFilegroupName(apiScope *apiScope) string {
401 return ":" + module.BaseModuleName() + "-removed.api." + apiScope.name + ".latest"
Jiyong Parkc678ad32018-04-10 13:07:10 +0900402}
403
404// Creates a static java library that has API stubs
Paul Duffinf0229202020-04-29 16:47:28 +0100405func (module *SdkLibrary) createStubsLibrary(mctx android.DefaultableHookContext, apiScope *apiScope) {
Jiyong Parkc678ad32018-04-10 13:07:10 +0900406 props := struct {
Sundong Ahn0d7dff42019-12-04 12:53:44 +0900407 Name *string
408 Srcs []string
Paul Duffin367ab912019-12-23 19:40:36 +0000409 Installable *bool
Sundong Ahn0d7dff42019-12-04 12:53:44 +0900410 Sdk_version *string
Paul Duffin12ceb462019-12-24 20:31:31 +0000411 System_modules *string
Paul Duffinab8da5d2020-02-07 16:12:04 +0000412 Patch_module *string
Sundong Ahn0d7dff42019-12-04 12:53:44 +0900413 Libs []string
414 Soc_specific *bool
415 Device_specific *bool
416 Product_specific *bool
417 System_ext_specific *bool
418 Compile_dex *bool
Sundong Ahn0d7dff42019-12-04 12:53:44 +0900419 Java_version *string
420 Product_variables struct {
Jiyong Park82484c02018-04-23 21:41:26 +0900421 Pdk struct {
422 Enabled *bool
423 }
Jiyong Parkc678ad32018-04-10 13:07:10 +0900424 }
Sundong Ahn054b19a2018-10-19 13:46:09 +0900425 Openjdk9 struct {
426 Srcs []string
427 Javacflags []string
428 }
Anton Hansson5fd5d242020-03-27 19:43:19 +0000429 Dist struct {
430 Targets []string
431 Dest *string
432 Dir *string
433 Tag *string
434 }
Jiyong Parkc678ad32018-04-10 13:07:10 +0900435 }{}
436
Jiyong Parkdf130542018-04-27 16:29:21 +0900437 props.Name = proptools.StringPtr(module.stubsName(apiScope))
Jiyong Parkc678ad32018-04-10 13:07:10 +0900438 // sources are generated from the droiddoc
Jiyong Parkdf130542018-04-27 16:29:21 +0900439 props.Srcs = []string{":" + module.docsName(apiScope)}
Paul Duffin12ceb462019-12-24 20:31:31 +0000440 sdkVersion := module.sdkVersionForStubsLibrary(mctx, apiScope)
Paul Duffin52d398a2019-06-11 12:31:14 +0100441 props.Sdk_version = proptools.StringPtr(sdkVersion)
Paul Duffin12ceb462019-12-24 20:31:31 +0000442 props.System_modules = module.Library.Module.deviceProperties.System_modules
Paul Duffinab8da5d2020-02-07 16:12:04 +0000443 props.Patch_module = module.Library.Module.properties.Patch_module
Paul Duffin367ab912019-12-23 19:40:36 +0000444 props.Installable = proptools.BoolPtr(false)
Sundong Ahn054b19a2018-10-19 13:46:09 +0900445 props.Libs = module.sdkLibraryProperties.Stub_only_libs
Jiyong Park82484c02018-04-23 21:41:26 +0900446 props.Product_variables.Pdk.Enabled = proptools.BoolPtr(false)
Sundong Ahn054b19a2018-10-19 13:46:09 +0900447 props.Openjdk9.Srcs = module.Library.Module.properties.Openjdk9.Srcs
448 props.Openjdk9.Javacflags = module.Library.Module.properties.Openjdk9.Javacflags
449 props.Java_version = module.Library.Module.properties.Java_version
450 if module.Library.Module.deviceProperties.Compile_dex != nil {
451 props.Compile_dex = module.Library.Module.deviceProperties.Compile_dex
Sundong Ahndd567f92018-07-31 17:19:11 +0900452 }
Jiyong Parkc678ad32018-04-10 13:07:10 +0900453
454 if module.SocSpecific() {
455 props.Soc_specific = proptools.BoolPtr(true)
456 } else if module.DeviceSpecific() {
457 props.Device_specific = proptools.BoolPtr(true)
458 } else if module.ProductSpecific() {
459 props.Product_specific = proptools.BoolPtr(true)
Sundong Ahn0d7dff42019-12-04 12:53:44 +0900460 } else if module.SystemExtSpecific() {
461 props.System_ext_specific = proptools.BoolPtr(true)
Jiyong Parkc678ad32018-04-10 13:07:10 +0900462 }
Anton Hansson5fd5d242020-03-27 19:43:19 +0000463 // Dist the class jar artifact for sdk builds.
464 if !Bool(module.sdkLibraryProperties.No_dist) {
465 props.Dist.Targets = []string{"sdk", "win_sdk"}
466 props.Dist.Dest = proptools.StringPtr(fmt.Sprintf("%v.jar", module.BaseModuleName()))
467 props.Dist.Dir = proptools.StringPtr(module.apiDistPath(apiScope))
468 props.Dist.Tag = proptools.StringPtr(".jar")
469 }
Jiyong Parkc678ad32018-04-10 13:07:10 +0900470
Colin Cross84dfc3d2019-09-25 11:33:01 -0700471 mctx.CreateModule(LibraryFactory, &props)
Jiyong Parkc678ad32018-04-10 13:07:10 +0900472}
473
Paul Duffin6d0886e2020-04-07 18:49:53 +0100474// Creates a droidstubs module that creates stubs source files from the given full source
Jiyong Parkc678ad32018-04-10 13:07:10 +0900475// files
Paul Duffinf0229202020-04-29 16:47:28 +0100476func (module *SdkLibrary) createStubsSources(mctx android.DefaultableHookContext, apiScope *apiScope) {
Jiyong Parkc678ad32018-04-10 13:07:10 +0900477 props := struct {
Sundong Ahn054b19a2018-10-19 13:46:09 +0900478 Name *string
479 Srcs []string
480 Installable *bool
Paul Duffin52d398a2019-06-11 12:31:14 +0100481 Sdk_version *string
Paul Duffin12ceb462019-12-24 20:31:31 +0000482 System_modules *string
Sundong Ahn054b19a2018-10-19 13:46:09 +0900483 Libs []string
Paul Duffin11512472019-02-11 15:55:17 +0000484 Arg_files []string
Sundong Ahn054b19a2018-10-19 13:46:09 +0900485 Args *string
Sundong Ahn054b19a2018-10-19 13:46:09 +0900486 Java_version *string
487 Merge_annotations_dirs []string
488 Merge_inclusion_annotations_dirs []string
489 Check_api struct {
Inseob Kim38449af2019-02-28 14:24:05 +0900490 Current ApiToCheck
491 Last_released ApiToCheck
492 Ignore_missing_latest_api *bool
Jiyong Park58c518b2018-05-12 22:29:12 +0900493 }
Sundong Ahn1b92c822018-05-29 11:35:17 +0900494 Aidl struct {
495 Include_dirs []string
496 Local_include_dirs []string
497 }
Anton Hansson5fd5d242020-03-27 19:43:19 +0000498 Dist struct {
499 Targets []string
500 Dest *string
501 Dir *string
502 }
Jiyong Parkc678ad32018-04-10 13:07:10 +0900503 }{}
504
Paul Duffin7b78b4d2020-04-28 14:08:32 +0100505 // The stubs source processing uses the same compile time classpath when extracting the
506 // API from the implementation library as it does when compiling it. i.e. the same
507 // * sdk version
508 // * system_modules
509 // * libs (static_libs/libs)
Paul Duffin250e6192019-06-07 10:44:37 +0100510
Jiyong Parkdf130542018-04-27 16:29:21 +0900511 props.Name = proptools.StringPtr(module.docsName(apiScope))
Sundong Ahn054b19a2018-10-19 13:46:09 +0900512 props.Srcs = append(props.Srcs, module.Library.Module.properties.Srcs...)
Paul Duffin7b78b4d2020-04-28 14:08:32 +0100513 props.Sdk_version = module.Library.Module.deviceProperties.Sdk_version
Paul Duffin12ceb462019-12-24 20:31:31 +0000514 props.System_modules = module.Library.Module.deviceProperties.System_modules
Jiyong Parkc678ad32018-04-10 13:07:10 +0900515 props.Installable = proptools.BoolPtr(false)
Sundong Ahne6f0b052018-06-05 16:46:14 +0900516 // A droiddoc module has only one Libs property and doesn't distinguish between
517 // shared libs and static libs. So we need to add both of these libs to Libs property.
Sundong Ahn054b19a2018-10-19 13:46:09 +0900518 props.Libs = module.Library.Module.properties.Libs
519 props.Libs = append(props.Libs, module.Library.Module.properties.Static_libs...)
520 props.Aidl.Include_dirs = module.Library.Module.deviceProperties.Aidl.Include_dirs
521 props.Aidl.Local_include_dirs = module.Library.Module.deviceProperties.Aidl.Local_include_dirs
Sundong Ahn054b19a2018-10-19 13:46:09 +0900522 props.Java_version = module.Library.Module.properties.Java_version
Jiyong Parkc678ad32018-04-10 13:07:10 +0900523
Sundong Ahn054b19a2018-10-19 13:46:09 +0900524 props.Merge_annotations_dirs = module.sdkLibraryProperties.Merge_annotations_dirs
525 props.Merge_inclusion_annotations_dirs = module.sdkLibraryProperties.Merge_inclusion_annotations_dirs
526
Paul Duffin6d0886e2020-04-07 18:49:53 +0100527 droidstubsArgs := []string{}
Paul Duffin235ffff2019-12-24 10:41:30 +0000528 if len(module.sdkLibraryProperties.Api_packages) != 0 {
Paul Duffin6d0886e2020-04-07 18:49:53 +0100529 droidstubsArgs = append(droidstubsArgs, "--stub-packages "+strings.Join(module.sdkLibraryProperties.Api_packages, ":"))
Paul Duffin235ffff2019-12-24 10:41:30 +0000530 }
531 if len(module.sdkLibraryProperties.Hidden_api_packages) != 0 {
Paul Duffin6d0886e2020-04-07 18:49:53 +0100532 droidstubsArgs = append(droidstubsArgs,
Paul Duffin235ffff2019-12-24 10:41:30 +0000533 android.JoinWithPrefix(module.sdkLibraryProperties.Hidden_api_packages, " --hide-package "))
534 }
Paul Duffin6d0886e2020-04-07 18:49:53 +0100535 droidstubsArgs = append(droidstubsArgs, module.sdkLibraryProperties.Droiddoc_options...)
Paul Duffin235ffff2019-12-24 10:41:30 +0000536 disabledWarnings := []string{
537 "MissingPermission",
538 "BroadcastBehavior",
539 "HiddenSuperclass",
540 "DeprecationMismatch",
541 "UnavailableSymbol",
542 "SdkConstant",
543 "HiddenTypeParameter",
544 "Todo",
545 "Typo",
546 }
Paul Duffin6d0886e2020-04-07 18:49:53 +0100547 droidstubsArgs = append(droidstubsArgs, android.JoinWithPrefix(disabledWarnings, "--hide "))
Sundong Ahnfb2721f2018-09-17 13:23:09 +0900548
Paul Duffin1fb487d2020-04-07 18:50:10 +0100549 // Add in scope specific arguments.
550 droidstubsArgs = append(droidstubsArgs, apiScope.droidstubsArgs...)
Paul Duffin11512472019-02-11 15:55:17 +0000551 props.Arg_files = module.sdkLibraryProperties.Droiddoc_option_files
Paul Duffin6d0886e2020-04-07 18:49:53 +0100552 props.Args = proptools.StringPtr(strings.Join(droidstubsArgs, " "))
Jiyong Parkc678ad32018-04-10 13:07:10 +0900553
554 // List of APIs identified from the provided source files are created. They are later
555 // compared against to the not-yet-released (a.k.a current) list of APIs and to the
556 // last-released (a.k.a numbered) list of API.
Paul Duffind1b3a922020-01-22 11:57:20 +0000557 currentApiFileName := apiScope.apiFilePrefix + "current.txt"
558 removedApiFileName := apiScope.apiFilePrefix + "removed.txt"
Paul Duffin749f98f2019-12-30 17:23:46 +0000559 apiDir := module.getApiDir()
560 currentApiFileName = path.Join(apiDir, currentApiFileName)
561 removedApiFileName = path.Join(apiDir, removedApiFileName)
Jiyong Parkc678ad32018-04-10 13:07:10 +0900562
Jiyong Park58c518b2018-05-12 22:29:12 +0900563 // check against the not-yet-release API
564 props.Check_api.Current.Api_file = proptools.StringPtr(currentApiFileName)
565 props.Check_api.Current.Removed_api_file = proptools.StringPtr(removedApiFileName)
Jiyong Park58c518b2018-05-12 22:29:12 +0900566
Anton Hansson6478ac12020-05-02 11:19:36 +0100567 if !apiScope.unstable {
568 // check against the latest released API
569 props.Check_api.Last_released.Api_file = proptools.StringPtr(
570 module.latestApiFilegroupName(apiScope))
571 props.Check_api.Last_released.Removed_api_file = proptools.StringPtr(
572 module.latestRemovedApiFilegroupName(apiScope))
573 props.Check_api.Ignore_missing_latest_api = proptools.BoolPtr(true)
574 }
Jiyong Park58c518b2018-05-12 22:29:12 +0900575
Anton Hansson5fd5d242020-03-27 19:43:19 +0000576 // Dist the api txt artifact for sdk builds.
577 if !Bool(module.sdkLibraryProperties.No_dist) {
578 props.Dist.Targets = []string{"sdk", "win_sdk"}
579 props.Dist.Dest = proptools.StringPtr(fmt.Sprintf("%v.txt", module.BaseModuleName()))
580 props.Dist.Dir = proptools.StringPtr(path.Join(module.apiDistPath(apiScope), "api"))
581 }
582
Colin Cross84dfc3d2019-09-25 11:33:01 -0700583 mctx.CreateModule(DroidstubsFactory, &props)
Jiyong Parkc678ad32018-04-10 13:07:10 +0900584}
585
Jooyung Han5e9013b2020-03-10 06:23:13 +0900586func (module *SdkLibrary) DepIsInSameApex(mctx android.BaseModuleContext, dep android.Module) bool {
587 depTag := mctx.OtherModuleDependencyTag(dep)
588 if depTag == xmlPermissionsFileTag {
589 return true
590 }
591 return module.Library.DepIsInSameApex(mctx, dep)
592}
593
Jiyong Parkc678ad32018-04-10 13:07:10 +0900594// Creates the xml file that publicizes the runtime library
Paul Duffinf0229202020-04-29 16:47:28 +0100595func (module *SdkLibrary) createXmlFile(mctx android.DefaultableHookContext) {
Jiyong Parke3833882020-02-17 17:28:10 +0900596 props := struct {
Sundong Ahn0d7dff42019-12-04 12:53:44 +0900597 Name *string
Jiyong Parke3833882020-02-17 17:28:10 +0900598 Lib_name *string
Sundong Ahn0d7dff42019-12-04 12:53:44 +0900599 Soc_specific *bool
600 Device_specific *bool
601 Product_specific *bool
602 System_ext_specific *bool
Jooyung Han5e9013b2020-03-10 06:23:13 +0900603 Apex_available []string
Jiyong Parke3833882020-02-17 17:28:10 +0900604 }{
Jooyung Han5e9013b2020-03-10 06:23:13 +0900605 Name: proptools.StringPtr(module.xmlFileName()),
606 Lib_name: proptools.StringPtr(module.BaseModuleName()),
607 Apex_available: module.ApexProperties.Apex_available,
Jiyong Parkc678ad32018-04-10 13:07:10 +0900608 }
Jiyong Parke3833882020-02-17 17:28:10 +0900609
610 if module.SocSpecific() {
611 props.Soc_specific = proptools.BoolPtr(true)
612 } else if module.DeviceSpecific() {
613 props.Device_specific = proptools.BoolPtr(true)
614 } else if module.ProductSpecific() {
615 props.Product_specific = proptools.BoolPtr(true)
616 } else if module.SystemExtSpecific() {
617 props.System_ext_specific = proptools.BoolPtr(true)
618 }
619
620 mctx.CreateModule(sdkLibraryXmlFactory, &props)
Jiyong Parkc678ad32018-04-10 13:07:10 +0900621}
622
Paul Duffin50061512020-01-21 16:31:05 +0000623func PrebuiltJars(ctx android.BaseModuleContext, baseName string, s sdkSpec) android.Paths {
Jiyong Park6a927c42020-01-21 02:03:43 +0900624 var ver sdkVersion
625 var kind sdkKind
626 if s.usePrebuilt(ctx) {
627 ver = s.version
628 kind = s.kind
Jiyong Parkc678ad32018-04-10 13:07:10 +0900629 } else {
Jiyong Park6a927c42020-01-21 02:03:43 +0900630 // We don't have prebuilt SDK for the specific sdkVersion.
631 // Instead of breaking the build, fallback to use "system_current"
632 ver = sdkVersionCurrent
633 kind = sdkSystem
Sundong Ahn054b19a2018-10-19 13:46:09 +0900634 }
Jiyong Park6a927c42020-01-21 02:03:43 +0900635
636 dir := filepath.Join("prebuilts", "sdk", ver.String(), kind.String())
Paul Duffin50061512020-01-21 16:31:05 +0000637 jar := filepath.Join(dir, baseName+".jar")
Sundong Ahn054b19a2018-10-19 13:46:09 +0900638 jarPath := android.ExistentPathForSource(ctx, jar)
Sundong Ahnae418ac2019-02-28 15:01:28 +0900639 if !jarPath.Valid() {
Colin Cross07c88562020-01-07 09:34:44 -0800640 if ctx.Config().AllowMissingDependencies() {
641 return android.Paths{android.PathForSource(ctx, jar)}
642 } else {
Jiyong Park6a927c42020-01-21 02:03:43 +0900643 ctx.PropertyErrorf("sdk_library", "invalid sdk version %q, %q does not exist", s.raw, jar)
Colin Cross07c88562020-01-07 09:34:44 -0800644 }
Sundong Ahnae418ac2019-02-28 15:01:28 +0900645 return nil
646 }
Sundong Ahn054b19a2018-10-19 13:46:09 +0900647 return android.Paths{jarPath.Path()}
648}
649
Paul Duffind1b3a922020-01-22 11:57:20 +0000650func (module *SdkLibrary) sdkJars(
651 ctx android.BaseModuleContext,
652 sdkVersion sdkSpec,
653 headerJars bool) android.Paths {
654
Paul Duffin50061512020-01-21 16:31:05 +0000655 // If a specific numeric version has been requested then use prebuilt versions of the sdk.
656 if sdkVersion.version.isNumbered() {
657 return PrebuiltJars(ctx, module.BaseModuleName(), sdkVersion)
Sundong Ahn054b19a2018-10-19 13:46:09 +0900658 } else {
Paul Duffind1b3a922020-01-22 11:57:20 +0000659 if !sdkVersion.specified() {
660 if headerJars {
661 return module.Library.HeaderJars()
662 } else {
663 return module.Library.ImplementationJars()
664 }
665 }
Paul Duffin726d23c2020-01-22 16:30:37 +0000666 var apiScope *apiScope
Jiyong Park6a927c42020-01-21 02:03:43 +0900667 switch sdkVersion.kind {
668 case sdkSystem:
Paul Duffin726d23c2020-01-22 16:30:37 +0000669 apiScope = apiScopeSystem
670 case sdkTest:
671 apiScope = apiScopeTest
Jiyong Park6a927c42020-01-21 02:03:43 +0900672 case sdkPrivate:
Sundong Ahn054b19a2018-10-19 13:46:09 +0900673 return module.Library.HeaderJars()
Jiyong Park6a927c42020-01-21 02:03:43 +0900674 default:
Paul Duffin726d23c2020-01-22 16:30:37 +0000675 apiScope = apiScopePublic
Paul Duffind1b3a922020-01-22 11:57:20 +0000676 }
677
Paul Duffin726d23c2020-01-22 16:30:37 +0000678 paths := module.getScopePaths(apiScope)
Paul Duffind1b3a922020-01-22 11:57:20 +0000679 if headerJars {
680 return paths.stubsHeaderPath
681 } else {
682 return paths.stubsImplPath
Sundong Ahn054b19a2018-10-19 13:46:09 +0900683 }
Jiyong Parkc678ad32018-04-10 13:07:10 +0900684 }
685}
686
Sundong Ahn241cd372018-07-13 16:16:44 +0900687// to satisfy SdkLibraryDependency interface
Paul Duffind1b3a922020-01-22 11:57:20 +0000688func (module *SdkLibrary) SdkHeaderJars(ctx android.BaseModuleContext, sdkVersion sdkSpec) android.Paths {
689 return module.sdkJars(ctx, sdkVersion, true /*headerJars*/)
690}
691
692// to satisfy SdkLibraryDependency interface
Jiyong Park6a927c42020-01-21 02:03:43 +0900693func (module *SdkLibrary) SdkImplementationJars(ctx android.BaseModuleContext, sdkVersion sdkSpec) android.Paths {
Paul Duffind1b3a922020-01-22 11:57:20 +0000694 return module.sdkJars(ctx, sdkVersion, false /*headerJars*/)
Sundong Ahn241cd372018-07-13 16:16:44 +0900695}
696
Sundong Ahn80a87b32019-05-13 15:02:50 +0900697func (module *SdkLibrary) SetNoDist() {
698 module.sdkLibraryProperties.No_dist = proptools.BoolPtr(true)
699}
700
Colin Cross571cccf2019-02-04 11:22:08 -0800701var javaSdkLibrariesKey = android.NewOnceKey("javaSdkLibraries")
702
Jiyong Park82484c02018-04-23 21:41:26 +0900703func javaSdkLibraries(config android.Config) *[]string {
Colin Cross571cccf2019-02-04 11:22:08 -0800704 return config.Once(javaSdkLibrariesKey, func() interface{} {
Jiyong Park82484c02018-04-23 21:41:26 +0900705 return &[]string{}
706 }).(*[]string)
707}
708
Paul Duffin749f98f2019-12-30 17:23:46 +0000709func (module *SdkLibrary) getApiDir() string {
710 return proptools.StringDefault(module.sdkLibraryProperties.Api_dir, "api")
711}
712
Jiyong Parkc678ad32018-04-10 13:07:10 +0900713// For a java_sdk_library module, create internal modules for stubs, docs,
714// runtime libs and xml file. If requested, the stubs and docs are created twice
715// once for public API level and once for system API level
Paul Duffinf0229202020-04-29 16:47:28 +0100716func (module *SdkLibrary) CreateInternalModules(mctx android.DefaultableHookContext) {
717 // If the module has been disabled then don't create any child modules.
718 if !module.Enabled() {
719 return
720 }
721
Inseob Kim6e93ac92019-03-21 17:43:49 +0900722 if len(module.Library.Module.properties.Srcs) == 0 {
Inseob Kimc0907f12019-02-08 21:00:45 +0900723 mctx.PropertyErrorf("srcs", "java_sdk_library must specify srcs")
Jooyung Han58f26ab2019-12-18 15:34:32 +0900724 return
Inseob Kimc0907f12019-02-08 21:00:45 +0900725 }
726
Paul Duffin37e0b772019-12-30 17:20:10 +0000727 // If this builds against standard libraries (i.e. is not part of the core libraries)
728 // then assume it provides both system and test apis. Otherwise, assume it does not and
729 // also assume it does not contribute to the dist build.
730 sdkDep := decodeSdkDep(mctx, sdkContext(&module.Library))
731 hasSystemAndTestApis := sdkDep.hasStandardLibs()
732 module.sdkLibraryProperties.Has_system_and_test_apis = hasSystemAndTestApis
733 module.sdkLibraryProperties.No_dist = proptools.BoolPtr(!hasSystemAndTestApis)
734
Inseob Kim8098faa2019-03-18 10:19:51 +0900735 missing_current_api := false
736
Paul Duffind1b3a922020-01-22 11:57:20 +0000737 activeScopes := module.getActiveApiScopes()
738
Paul Duffin749f98f2019-12-30 17:23:46 +0000739 apiDir := module.getApiDir()
Paul Duffind1b3a922020-01-22 11:57:20 +0000740 for _, scope := range activeScopes {
Inseob Kim8098faa2019-03-18 10:19:51 +0900741 for _, api := range []string{"current.txt", "removed.txt"} {
Paul Duffind1b3a922020-01-22 11:57:20 +0000742 path := path.Join(mctx.ModuleDir(), apiDir, scope.apiFilePrefix+api)
Inseob Kim8098faa2019-03-18 10:19:51 +0900743 p := android.ExistentPathForSource(mctx, path)
744 if !p.Valid() {
745 mctx.ModuleErrorf("Current api file %#v doesn't exist", path)
746 missing_current_api = true
747 }
748 }
749 }
750
751 if missing_current_api {
752 script := "build/soong/scripts/gen-java-current-api-files.sh"
753 p := android.ExistentPathForSource(mctx, script)
754
755 if !p.Valid() {
756 panic(fmt.Sprintf("script file %s doesn't exist", script))
757 }
758
759 mctx.ModuleErrorf("One or more current api files are missing. "+
760 "You can update them by:\n"+
Paul Duffin37e0b772019-12-30 17:20:10 +0000761 "%s %q %s && m update-api",
Paul Duffind1b3a922020-01-22 11:57:20 +0000762 script, filepath.Join(mctx.ModuleDir(), apiDir),
763 strings.Join(activeScopes.Strings(func(s *apiScope) string { return s.apiFilePrefix }), " "))
Inseob Kim8098faa2019-03-18 10:19:51 +0900764 return
765 }
766
Paul Duffind1b3a922020-01-22 11:57:20 +0000767 for _, scope := range activeScopes {
768 module.createStubsLibrary(mctx, scope)
769 module.createStubsSources(mctx, scope)
Inseob Kimc0907f12019-02-08 21:00:45 +0900770 }
771
Paul Duffin43db9be2019-12-30 17:35:49 +0000772 if !proptools.Bool(module.sdkLibraryProperties.Api_only) {
773 // for runtime
774 module.createXmlFile(mctx)
775
776 // record java_sdk_library modules so that they are exported to make
777 javaSdkLibraries := javaSdkLibraries(mctx.Config())
778 javaSdkLibrariesLock.Lock()
779 defer javaSdkLibrariesLock.Unlock()
780 *javaSdkLibraries = append(*javaSdkLibraries, module.BaseModuleName())
781 }
Inseob Kimc0907f12019-02-08 21:00:45 +0900782}
783
784func (module *SdkLibrary) InitSdkLibraryProperties() {
Sundong Ahn054b19a2018-10-19 13:46:09 +0900785 module.AddProperties(
786 &module.sdkLibraryProperties,
787 &module.Library.Module.properties,
788 &module.Library.Module.dexpreoptProperties,
789 &module.Library.Module.deviceProperties,
790 &module.Library.Module.protoProperties,
791 )
792
793 module.Library.Module.properties.Installable = proptools.BoolPtr(true)
794 module.Library.Module.deviceProperties.IsSDKLibrary = true
Inseob Kimc0907f12019-02-08 21:00:45 +0900795}
Sundong Ahn054b19a2018-10-19 13:46:09 +0900796
Jaewoong Jung4f158ee2019-07-11 10:05:35 -0700797// java_sdk_library is a special Java library that provides optional platform APIs to apps.
798// In practice, it can be viewed as a combination of several modules: 1) stubs library that clients
799// are linked against to, 2) droiddoc module that internally generates API stubs source files,
800// 3) the real runtime shared library that implements the APIs, and 4) XML file for adding
801// the runtime lib to the classpath at runtime if requested via <uses-library>.
Inseob Kimc0907f12019-02-08 21:00:45 +0900802func SdkLibraryFactory() android.Module {
803 module := &SdkLibrary{}
804 module.InitSdkLibraryProperties()
Jooyung Han58f26ab2019-12-18 15:34:32 +0900805 android.InitApexModule(module)
Sundong Ahn054b19a2018-10-19 13:46:09 +0900806 InitJavaModule(module, android.HostAndDeviceSupported)
Paul Duffinf0229202020-04-29 16:47:28 +0100807 module.SetDefaultableHook(func(ctx android.DefaultableHookContext) { module.CreateInternalModules(ctx) })
Jiyong Parkc678ad32018-04-10 13:07:10 +0900808 return module
809}
Colin Cross79c7c262019-04-17 11:11:46 -0700810
811//
812// SDK library prebuilts
813//
814
Paul Duffin56d44902020-01-31 13:36:25 +0000815// Properties associated with each api scope.
816type sdkLibraryScopeProperties struct {
Colin Cross79c7c262019-04-17 11:11:46 -0700817 Jars []string `android:"path"`
818
819 Sdk_version *string
820
Colin Cross79c7c262019-04-17 11:11:46 -0700821 // List of shared java libs that this module has dependencies to
822 Libs []string
Paul Duffin3d1248c2020-04-09 00:10:17 +0100823
824 // The stub sources.
825 Stub_srcs []string `android:"path"`
Paul Duffin1fd005d2020-04-09 01:08:11 +0100826
827 // The current.txt
828 Current_api string `android:"path"`
829
830 // The removed.txt
831 Removed_api string `android:"path"`
Colin Cross79c7c262019-04-17 11:11:46 -0700832}
833
Paul Duffin56d44902020-01-31 13:36:25 +0000834type sdkLibraryImportProperties struct {
Paul Duffinfcfd7912020-01-31 17:54:30 +0000835 // List of shared java libs, common to all scopes, that this module has
836 // dependencies to
837 Libs []string
Paul Duffin56d44902020-01-31 13:36:25 +0000838}
839
Colin Cross79c7c262019-04-17 11:11:46 -0700840type sdkLibraryImport struct {
841 android.ModuleBase
842 android.DefaultableModuleBase
843 prebuilt android.Prebuilt
Paul Duffindd46f712020-02-10 13:37:10 +0000844 android.ApexModuleBase
845 android.SdkBase
Colin Cross79c7c262019-04-17 11:11:46 -0700846
847 properties sdkLibraryImportProperties
848
Paul Duffin46a26a82020-04-07 19:27:04 +0100849 // Map from api scope to the scope specific property structure.
850 scopeProperties map[*apiScope]*sdkLibraryScopeProperties
851
Paul Duffin56d44902020-01-31 13:36:25 +0000852 commonToSdkLibraryAndImport
Colin Cross79c7c262019-04-17 11:11:46 -0700853}
854
855var _ SdkLibraryDependency = (*sdkLibraryImport)(nil)
856
Paul Duffin46a26a82020-04-07 19:27:04 +0100857// The type of a structure that contains a field of type sdkLibraryScopeProperties
858// for each apiscope in allApiScopes, e.g. something like:
859// struct {
860// Public sdkLibraryScopeProperties
861// System sdkLibraryScopeProperties
862// ...
863// }
864var allScopeStructType = createAllScopePropertiesStructType()
865
866// Dynamically create a structure type for each apiscope in allApiScopes.
867func createAllScopePropertiesStructType() reflect.Type {
868 var fields []reflect.StructField
869 for _, apiScope := range allApiScopes {
870 field := reflect.StructField{
871 Name: apiScope.fieldName,
872 Type: reflect.TypeOf(sdkLibraryScopeProperties{}),
873 }
874 fields = append(fields, field)
875 }
876
877 return reflect.StructOf(fields)
878}
879
880// Create an instance of the scope specific structure type and return a map
881// from apiscope to a pointer to each scope specific field.
882func createPropertiesInstance() (interface{}, map[*apiScope]*sdkLibraryScopeProperties) {
883 allScopePropertiesPtr := reflect.New(allScopeStructType)
884 allScopePropertiesStruct := allScopePropertiesPtr.Elem()
885 scopeProperties := make(map[*apiScope]*sdkLibraryScopeProperties)
886
887 for _, apiScope := range allApiScopes {
888 field := allScopePropertiesStruct.FieldByName(apiScope.fieldName)
889 scopeProperties[apiScope] = field.Addr().Interface().(*sdkLibraryScopeProperties)
890 }
891
892 return allScopePropertiesPtr.Interface(), scopeProperties
893}
894
Jaewoong Jung4f158ee2019-07-11 10:05:35 -0700895// java_sdk_library_import imports a prebuilt java_sdk_library.
Colin Cross79c7c262019-04-17 11:11:46 -0700896func sdkLibraryImportFactory() android.Module {
897 module := &sdkLibraryImport{}
898
Paul Duffin46a26a82020-04-07 19:27:04 +0100899 allScopeProperties, scopeToProperties := createPropertiesInstance()
900 module.scopeProperties = scopeToProperties
901 module.AddProperties(&module.properties, allScopeProperties)
Colin Cross79c7c262019-04-17 11:11:46 -0700902
Paul Duffin0bdcb272020-02-06 15:24:57 +0000903 android.InitPrebuiltModule(module, &[]string{""})
Paul Duffindd46f712020-02-10 13:37:10 +0000904 android.InitApexModule(module)
905 android.InitSdkAwareModule(module)
Colin Cross79c7c262019-04-17 11:11:46 -0700906 InitJavaModule(module, android.HostAndDeviceSupported)
907
908 android.AddLoadHook(module, func(mctx android.LoadHookContext) { module.createInternalModules(mctx) })
909 return module
910}
911
912func (module *sdkLibraryImport) Prebuilt() *android.Prebuilt {
913 return &module.prebuilt
914}
915
916func (module *sdkLibraryImport) Name() string {
917 return module.prebuilt.Name(module.ModuleBase.Name())
918}
919
920func (module *sdkLibraryImport) createInternalModules(mctx android.LoadHookContext) {
Colin Cross79c7c262019-04-17 11:11:46 -0700921
Paul Duffin50061512020-01-21 16:31:05 +0000922 // If the build is configured to use prebuilts then force this to be preferred.
923 if mctx.Config().UnbundledBuildUsePrebuiltSdks() {
924 module.prebuilt.ForcePrefer()
925 }
926
Paul Duffin46a26a82020-04-07 19:27:04 +0100927 for apiScope, scopeProperties := range module.scopeProperties {
Paul Duffin56d44902020-01-31 13:36:25 +0000928 if len(scopeProperties.Jars) == 0 {
929 continue
930 }
931
Paul Duffinbbb546b2020-04-09 00:07:11 +0100932 module.createJavaImportForStubs(mctx, apiScope, scopeProperties)
Paul Duffin3d1248c2020-04-09 00:10:17 +0100933
934 module.createPrebuiltStubsSources(mctx, apiScope, scopeProperties)
Paul Duffin56d44902020-01-31 13:36:25 +0000935 }
Colin Cross79c7c262019-04-17 11:11:46 -0700936
937 javaSdkLibraries := javaSdkLibraries(mctx.Config())
938 javaSdkLibrariesLock.Lock()
939 defer javaSdkLibrariesLock.Unlock()
940 *javaSdkLibraries = append(*javaSdkLibraries, module.BaseModuleName())
941}
942
Paul Duffinbbb546b2020-04-09 00:07:11 +0100943func (module *sdkLibraryImport) createJavaImportForStubs(mctx android.LoadHookContext, apiScope *apiScope, scopeProperties *sdkLibraryScopeProperties) {
944 // Creates a java import for the jar with ".stubs" suffix
945 props := struct {
946 Name *string
947 Soc_specific *bool
948 Device_specific *bool
949 Product_specific *bool
950 System_ext_specific *bool
951 Sdk_version *string
952 Libs []string
953 Jars []string
954 Prefer *bool
955 }{}
956 props.Name = proptools.StringPtr(apiScope.stubsModuleName(module.BaseModuleName()))
957 props.Sdk_version = scopeProperties.Sdk_version
958 // Prepend any of the libs from the legacy public properties to the libs for each of the
959 // scopes to avoid having to duplicate them in each scope.
960 props.Libs = append(module.properties.Libs, scopeProperties.Libs...)
961 props.Jars = scopeProperties.Jars
962 if module.SocSpecific() {
963 props.Soc_specific = proptools.BoolPtr(true)
964 } else if module.DeviceSpecific() {
965 props.Device_specific = proptools.BoolPtr(true)
966 } else if module.ProductSpecific() {
967 props.Product_specific = proptools.BoolPtr(true)
968 } else if module.SystemExtSpecific() {
969 props.System_ext_specific = proptools.BoolPtr(true)
970 }
971 // If the build should use prebuilt sdks then set prefer to true on the stubs library.
972 // That will cause the prebuilt version of the stubs to override the source version.
973 if mctx.Config().UnbundledBuildUsePrebuiltSdks() {
974 props.Prefer = proptools.BoolPtr(true)
975 }
976 mctx.CreateModule(ImportFactory, &props)
977}
978
Paul Duffin3d1248c2020-04-09 00:10:17 +0100979func (module *sdkLibraryImport) createPrebuiltStubsSources(mctx android.LoadHookContext, apiScope *apiScope, scopeProperties *sdkLibraryScopeProperties) {
980 props := struct {
981 Name *string
982 Srcs []string
983 }{}
984 props.Name = proptools.StringPtr(apiScope.docsModuleName(module.BaseModuleName()))
985 props.Srcs = scopeProperties.Stub_srcs
986 mctx.CreateModule(PrebuiltStubsSourcesFactory, &props)
987}
988
Colin Cross79c7c262019-04-17 11:11:46 -0700989func (module *sdkLibraryImport) DepsMutator(ctx android.BottomUpMutatorContext) {
Paul Duffin46a26a82020-04-07 19:27:04 +0100990 for apiScope, scopeProperties := range module.scopeProperties {
Paul Duffin56d44902020-01-31 13:36:25 +0000991 if len(scopeProperties.Jars) == 0 {
992 continue
993 }
994
995 // Add dependencies to the prebuilt stubs library
996 ctx.AddVariationDependencies(nil, apiScope.stubsTag, apiScope.stubsModuleName(module.BaseModuleName()))
997 }
Colin Cross79c7c262019-04-17 11:11:46 -0700998}
999
1000func (module *sdkLibraryImport) GenerateAndroidBuildActions(ctx android.ModuleContext) {
1001 // Record the paths to the prebuilt stubs library.
1002 ctx.VisitDirectDeps(func(to android.Module) {
1003 tag := ctx.OtherModuleDependencyTag(to)
1004
Paul Duffin56d44902020-01-31 13:36:25 +00001005 if lib, ok := to.(Dependency); ok {
1006 if scopeTag, ok := tag.(scopeDependencyTag); ok {
1007 apiScope := scopeTag.apiScope
1008 scopePaths := module.getScopePaths(apiScope)
1009 scopePaths.stubsHeaderPath = lib.HeaderJars()
1010 }
Colin Cross79c7c262019-04-17 11:11:46 -07001011 }
1012 })
1013}
1014
Paul Duffin56d44902020-01-31 13:36:25 +00001015func (module *sdkLibraryImport) sdkJars(
1016 ctx android.BaseModuleContext,
1017 sdkVersion sdkSpec) android.Paths {
1018
Paul Duffin50061512020-01-21 16:31:05 +00001019 // If a specific numeric version has been requested then use prebuilt versions of the sdk.
1020 if sdkVersion.version.isNumbered() {
1021 return PrebuiltJars(ctx, module.BaseModuleName(), sdkVersion)
1022 }
1023
Paul Duffin56d44902020-01-31 13:36:25 +00001024 var apiScope *apiScope
1025 switch sdkVersion.kind {
1026 case sdkSystem:
1027 apiScope = apiScopeSystem
1028 case sdkTest:
1029 apiScope = apiScopeTest
1030 default:
1031 apiScope = apiScopePublic
1032 }
1033
1034 paths := module.getScopePaths(apiScope)
1035 return paths.stubsHeaderPath
1036}
1037
Colin Cross79c7c262019-04-17 11:11:46 -07001038// to satisfy SdkLibraryDependency interface
Jiyong Park6a927c42020-01-21 02:03:43 +09001039func (module *sdkLibraryImport) SdkHeaderJars(ctx android.BaseModuleContext, sdkVersion sdkSpec) android.Paths {
Colin Cross79c7c262019-04-17 11:11:46 -07001040 // This module is just a wrapper for the prebuilt stubs.
Paul Duffin56d44902020-01-31 13:36:25 +00001041 return module.sdkJars(ctx, sdkVersion)
Colin Cross79c7c262019-04-17 11:11:46 -07001042}
1043
1044// to satisfy SdkLibraryDependency interface
Jiyong Park6a927c42020-01-21 02:03:43 +09001045func (module *sdkLibraryImport) SdkImplementationJars(ctx android.BaseModuleContext, sdkVersion sdkSpec) android.Paths {
Colin Cross79c7c262019-04-17 11:11:46 -07001046 // This module is just a wrapper for the stubs.
Paul Duffin56d44902020-01-31 13:36:25 +00001047 return module.sdkJars(ctx, sdkVersion)
Colin Cross79c7c262019-04-17 11:11:46 -07001048}
Jiyong Parke3833882020-02-17 17:28:10 +09001049
1050//
1051// java_sdk_library_xml
1052//
1053type sdkLibraryXml struct {
1054 android.ModuleBase
1055 android.DefaultableModuleBase
1056 android.ApexModuleBase
1057
1058 properties sdkLibraryXmlProperties
1059
1060 outputFilePath android.OutputPath
1061 installDirPath android.InstallPath
1062}
1063
1064type sdkLibraryXmlProperties struct {
1065 // canonical name of the lib
1066 Lib_name *string
1067}
1068
1069// java_sdk_library_xml builds the permission xml file for a java_sdk_library.
1070// Not to be used directly by users. java_sdk_library internally uses this.
1071func sdkLibraryXmlFactory() android.Module {
1072 module := &sdkLibraryXml{}
1073
1074 module.AddProperties(&module.properties)
1075
1076 android.InitApexModule(module)
1077 android.InitAndroidArchModule(module, android.DeviceSupported, android.MultilibCommon)
1078
1079 return module
1080}
1081
1082// from android.PrebuiltEtcModule
1083func (module *sdkLibraryXml) SubDir() string {
1084 return "permissions"
1085}
1086
1087// from android.PrebuiltEtcModule
1088func (module *sdkLibraryXml) OutputFile() android.OutputPath {
1089 return module.outputFilePath
1090}
1091
1092// from android.ApexModule
1093func (module *sdkLibraryXml) AvailableFor(what string) bool {
1094 return true
1095}
1096
1097func (module *sdkLibraryXml) DepsMutator(ctx android.BottomUpMutatorContext) {
1098 // do nothing
1099}
1100
1101// File path to the runtime implementation library
1102func (module *sdkLibraryXml) implPath() string {
1103 implName := proptools.String(module.properties.Lib_name)
1104 if apexName := module.ApexName(); apexName != "" {
1105 // TODO(b/146468504): ApexName() is only a soong module name, not apex name.
1106 // In most cases, this works fine. But when apex_name is set or override_apex is used
1107 // this can be wrong.
1108 return fmt.Sprintf("/apex/%s/javalib/%s.jar", apexName, implName)
1109 }
1110 partition := "system"
1111 if module.SocSpecific() {
1112 partition = "vendor"
1113 } else if module.DeviceSpecific() {
1114 partition = "odm"
1115 } else if module.ProductSpecific() {
1116 partition = "product"
1117 } else if module.SystemExtSpecific() {
1118 partition = "system_ext"
1119 }
1120 return "/" + partition + "/framework/" + implName + ".jar"
1121}
1122
1123func (module *sdkLibraryXml) GenerateAndroidBuildActions(ctx android.ModuleContext) {
1124 libName := proptools.String(module.properties.Lib_name)
1125 xmlContent := fmt.Sprintf(permissionsTemplate, libName, module.implPath())
1126
1127 module.outputFilePath = android.PathForModuleOut(ctx, libName+".xml").OutputPath
1128 rule := android.NewRuleBuilder()
1129 rule.Command().
1130 Text("/bin/bash -c \"echo -e '" + xmlContent + "'\" > ").
1131 Output(module.outputFilePath)
1132
1133 rule.Build(pctx, ctx, "java_sdk_xml", "Permission XML")
1134
1135 module.installDirPath = android.PathForModuleInstall(ctx, "etc", module.SubDir())
1136}
1137
1138func (module *sdkLibraryXml) AndroidMkEntries() []android.AndroidMkEntries {
1139 if !module.IsForPlatform() {
1140 return []android.AndroidMkEntries{android.AndroidMkEntries{
1141 Disabled: true,
1142 }}
1143 }
1144
1145 return []android.AndroidMkEntries{android.AndroidMkEntries{
1146 Class: "ETC",
1147 OutputFile: android.OptionalPathForPath(module.outputFilePath),
1148 ExtraEntries: []android.AndroidMkExtraEntriesFunc{
1149 func(entries *android.AndroidMkEntries) {
1150 entries.SetString("LOCAL_MODULE_TAGS", "optional")
1151 entries.SetString("LOCAL_MODULE_PATH", module.installDirPath.ToMakePath().String())
1152 entries.SetString("LOCAL_INSTALLED_MODULE_STEM", module.outputFilePath.Base())
1153 },
1154 },
1155 }}
1156}
Paul Duffindd46f712020-02-10 13:37:10 +00001157
1158type sdkLibrarySdkMemberType struct {
1159 android.SdkMemberTypeBase
1160}
1161
1162func (s *sdkLibrarySdkMemberType) AddDependencies(mctx android.BottomUpMutatorContext, dependencyTag blueprint.DependencyTag, names []string) {
1163 mctx.AddVariationDependencies(nil, dependencyTag, names...)
1164}
1165
1166func (s *sdkLibrarySdkMemberType) IsInstance(module android.Module) bool {
1167 _, ok := module.(*SdkLibrary)
1168 return ok
1169}
1170
1171func (s *sdkLibrarySdkMemberType) AddPrebuiltModule(ctx android.SdkMemberContext, member android.SdkMember) android.BpModule {
1172 return ctx.SnapshotBuilder().AddPrebuiltModule(member, "java_sdk_library_import")
1173}
1174
1175func (s *sdkLibrarySdkMemberType) CreateVariantPropertiesStruct() android.SdkMemberProperties {
1176 return &sdkLibrarySdkMemberProperties{}
1177}
1178
1179type sdkLibrarySdkMemberProperties struct {
1180 android.SdkMemberPropertiesBase
1181
1182 // Scope to per scope properties.
1183 Scopes map[*apiScope]scopeProperties
1184
1185 // Additional libraries that the exported stubs libraries depend upon.
1186 Libs []string
Paul Duffin3d1248c2020-04-09 00:10:17 +01001187
1188 // The Java stubs source files.
1189 Stub_srcs []string
Paul Duffindd46f712020-02-10 13:37:10 +00001190}
1191
1192type scopeProperties struct {
Paul Duffin1fd005d2020-04-09 01:08:11 +01001193 Jars android.Paths
1194 StubsSrcJar android.Path
1195 CurrentApiFile android.Path
1196 RemovedApiFile android.Path
1197 SdkVersion string
Paul Duffindd46f712020-02-10 13:37:10 +00001198}
1199
1200func (s *sdkLibrarySdkMemberProperties) PopulateFromVariant(ctx android.SdkMemberContext, variant android.Module) {
1201 sdk := variant.(*SdkLibrary)
1202
1203 s.Scopes = make(map[*apiScope]scopeProperties)
1204 for _, apiScope := range allApiScopes {
1205 paths := sdk.getScopePaths(apiScope)
1206 jars := paths.stubsImplPath
1207 if len(jars) > 0 {
1208 properties := scopeProperties{}
1209 properties.Jars = jars
1210 properties.SdkVersion = apiScope.sdkVersion
Paul Duffin3d1248c2020-04-09 00:10:17 +01001211 properties.StubsSrcJar = paths.stubsSrcJar
Paul Duffin1fd005d2020-04-09 01:08:11 +01001212 properties.CurrentApiFile = paths.currentApiFilePath
1213 properties.RemovedApiFile = paths.removedApiFilePath
Paul Duffindd46f712020-02-10 13:37:10 +00001214 s.Scopes[apiScope] = properties
1215 }
1216 }
1217
1218 s.Libs = sdk.properties.Libs
1219}
1220
1221func (s *sdkLibrarySdkMemberProperties) AddToPropertySet(ctx android.SdkMemberContext, propertySet android.BpPropertySet) {
1222 for _, apiScope := range allApiScopes {
1223 if properties, ok := s.Scopes[apiScope]; ok {
1224 scopeSet := propertySet.AddPropertySet(apiScope.name)
1225
Paul Duffin3d1248c2020-04-09 00:10:17 +01001226 scopeDir := filepath.Join("sdk_library", s.OsPrefix(), apiScope.name)
1227
Paul Duffindd46f712020-02-10 13:37:10 +00001228 var jars []string
1229 for _, p := range properties.Jars {
Paul Duffin3d1248c2020-04-09 00:10:17 +01001230 dest := filepath.Join(scopeDir, ctx.Name()+"-stubs.jar")
Paul Duffindd46f712020-02-10 13:37:10 +00001231 ctx.SnapshotBuilder().CopyToSnapshot(p, dest)
1232 jars = append(jars, dest)
1233 }
1234 scopeSet.AddProperty("jars", jars)
1235
Paul Duffin3d1248c2020-04-09 00:10:17 +01001236 // Merge the stubs source jar into the snapshot zip so that when it is unpacked
1237 // the source files are also unpacked.
1238 snapshotRelativeDir := filepath.Join(scopeDir, ctx.Name()+"_stub_sources")
1239 ctx.SnapshotBuilder().UnzipToSnapshot(properties.StubsSrcJar, snapshotRelativeDir)
1240 scopeSet.AddProperty("stub_srcs", []string{snapshotRelativeDir})
1241
Paul Duffin1fd005d2020-04-09 01:08:11 +01001242 if properties.CurrentApiFile != nil {
1243 currentApiSnapshotPath := filepath.Join(scopeDir, ctx.Name()+".txt")
1244 ctx.SnapshotBuilder().CopyToSnapshot(properties.CurrentApiFile, currentApiSnapshotPath)
1245 scopeSet.AddProperty("current_api", currentApiSnapshotPath)
1246 }
1247
1248 if properties.RemovedApiFile != nil {
1249 removedApiSnapshotPath := filepath.Join(scopeDir, ctx.Name()+"-removed.txt")
1250 ctx.SnapshotBuilder().CopyToSnapshot(properties.CurrentApiFile, removedApiSnapshotPath)
1251 scopeSet.AddProperty("removed_api", removedApiSnapshotPath)
1252 }
1253
Paul Duffindd46f712020-02-10 13:37:10 +00001254 if properties.SdkVersion != "" {
1255 scopeSet.AddProperty("sdk_version", properties.SdkVersion)
1256 }
1257 }
1258 }
1259
1260 if len(s.Libs) > 0 {
1261 propertySet.AddPropertyWithTag("libs", s.Libs, ctx.SnapshotBuilder().SdkMemberReferencePropertyTag(false))
1262 }
1263}