blob: d70f6320d890a17271230a60ccbc63a7a0692175 [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 Duffind1b3a922020-01-22 11:57:20 +0000385func (module *SdkLibrary) sdkVersionForStubsLibrary(mctx android.LoadHookContext, 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 Duffind1b3a922020-01-22 11:57:20 +0000405func (module *SdkLibrary) createStubsLibrary(mctx android.LoadHookContext, 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 Duffind1b3a922020-01-22 11:57:20 +0000476func (module *SdkLibrary) createStubsSources(mctx android.LoadHookContext, 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
Colin Crossf8b860a2019-04-16 14:43:28 -0700595func (module *SdkLibrary) createXmlFile(mctx android.LoadHookContext) {
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
Colin Crossf8b860a2019-04-16 14:43:28 -0700716func (module *SdkLibrary) CreateInternalModules(mctx android.LoadHookContext) {
Inseob Kim6e93ac92019-03-21 17:43:49 +0900717 if len(module.Library.Module.properties.Srcs) == 0 {
Inseob Kimc0907f12019-02-08 21:00:45 +0900718 mctx.PropertyErrorf("srcs", "java_sdk_library must specify srcs")
Jooyung Han58f26ab2019-12-18 15:34:32 +0900719 return
Inseob Kimc0907f12019-02-08 21:00:45 +0900720 }
721
Paul Duffin37e0b772019-12-30 17:20:10 +0000722 // If this builds against standard libraries (i.e. is not part of the core libraries)
723 // then assume it provides both system and test apis. Otherwise, assume it does not and
724 // also assume it does not contribute to the dist build.
725 sdkDep := decodeSdkDep(mctx, sdkContext(&module.Library))
726 hasSystemAndTestApis := sdkDep.hasStandardLibs()
727 module.sdkLibraryProperties.Has_system_and_test_apis = hasSystemAndTestApis
728 module.sdkLibraryProperties.No_dist = proptools.BoolPtr(!hasSystemAndTestApis)
729
Inseob Kim8098faa2019-03-18 10:19:51 +0900730 missing_current_api := false
731
Paul Duffind1b3a922020-01-22 11:57:20 +0000732 activeScopes := module.getActiveApiScopes()
733
Paul Duffin749f98f2019-12-30 17:23:46 +0000734 apiDir := module.getApiDir()
Paul Duffind1b3a922020-01-22 11:57:20 +0000735 for _, scope := range activeScopes {
Inseob Kim8098faa2019-03-18 10:19:51 +0900736 for _, api := range []string{"current.txt", "removed.txt"} {
Paul Duffind1b3a922020-01-22 11:57:20 +0000737 path := path.Join(mctx.ModuleDir(), apiDir, scope.apiFilePrefix+api)
Inseob Kim8098faa2019-03-18 10:19:51 +0900738 p := android.ExistentPathForSource(mctx, path)
739 if !p.Valid() {
740 mctx.ModuleErrorf("Current api file %#v doesn't exist", path)
741 missing_current_api = true
742 }
743 }
744 }
745
746 if missing_current_api {
747 script := "build/soong/scripts/gen-java-current-api-files.sh"
748 p := android.ExistentPathForSource(mctx, script)
749
750 if !p.Valid() {
751 panic(fmt.Sprintf("script file %s doesn't exist", script))
752 }
753
754 mctx.ModuleErrorf("One or more current api files are missing. "+
755 "You can update them by:\n"+
Paul Duffin37e0b772019-12-30 17:20:10 +0000756 "%s %q %s && m update-api",
Paul Duffind1b3a922020-01-22 11:57:20 +0000757 script, filepath.Join(mctx.ModuleDir(), apiDir),
758 strings.Join(activeScopes.Strings(func(s *apiScope) string { return s.apiFilePrefix }), " "))
Inseob Kim8098faa2019-03-18 10:19:51 +0900759 return
760 }
761
Paul Duffind1b3a922020-01-22 11:57:20 +0000762 for _, scope := range activeScopes {
763 module.createStubsLibrary(mctx, scope)
764 module.createStubsSources(mctx, scope)
Inseob Kimc0907f12019-02-08 21:00:45 +0900765 }
766
Paul Duffin43db9be2019-12-30 17:35:49 +0000767 if !proptools.Bool(module.sdkLibraryProperties.Api_only) {
768 // for runtime
769 module.createXmlFile(mctx)
770
771 // record java_sdk_library modules so that they are exported to make
772 javaSdkLibraries := javaSdkLibraries(mctx.Config())
773 javaSdkLibrariesLock.Lock()
774 defer javaSdkLibrariesLock.Unlock()
775 *javaSdkLibraries = append(*javaSdkLibraries, module.BaseModuleName())
776 }
Inseob Kimc0907f12019-02-08 21:00:45 +0900777}
778
779func (module *SdkLibrary) InitSdkLibraryProperties() {
Sundong Ahn054b19a2018-10-19 13:46:09 +0900780 module.AddProperties(
781 &module.sdkLibraryProperties,
782 &module.Library.Module.properties,
783 &module.Library.Module.dexpreoptProperties,
784 &module.Library.Module.deviceProperties,
785 &module.Library.Module.protoProperties,
786 )
787
788 module.Library.Module.properties.Installable = proptools.BoolPtr(true)
789 module.Library.Module.deviceProperties.IsSDKLibrary = true
Inseob Kimc0907f12019-02-08 21:00:45 +0900790}
Sundong Ahn054b19a2018-10-19 13:46:09 +0900791
Jaewoong Jung4f158ee2019-07-11 10:05:35 -0700792// java_sdk_library is a special Java library that provides optional platform APIs to apps.
793// In practice, it can be viewed as a combination of several modules: 1) stubs library that clients
794// are linked against to, 2) droiddoc module that internally generates API stubs source files,
795// 3) the real runtime shared library that implements the APIs, and 4) XML file for adding
796// the runtime lib to the classpath at runtime if requested via <uses-library>.
Inseob Kimc0907f12019-02-08 21:00:45 +0900797func SdkLibraryFactory() android.Module {
798 module := &SdkLibrary{}
799 module.InitSdkLibraryProperties()
Jooyung Han58f26ab2019-12-18 15:34:32 +0900800 android.InitApexModule(module)
Sundong Ahn054b19a2018-10-19 13:46:09 +0900801 InitJavaModule(module, android.HostAndDeviceSupported)
Colin Crossf8b860a2019-04-16 14:43:28 -0700802 android.AddLoadHook(module, func(ctx android.LoadHookContext) { module.CreateInternalModules(ctx) })
Jiyong Parkc678ad32018-04-10 13:07:10 +0900803 return module
804}
Colin Cross79c7c262019-04-17 11:11:46 -0700805
806//
807// SDK library prebuilts
808//
809
Paul Duffin56d44902020-01-31 13:36:25 +0000810// Properties associated with each api scope.
811type sdkLibraryScopeProperties struct {
Colin Cross79c7c262019-04-17 11:11:46 -0700812 Jars []string `android:"path"`
813
814 Sdk_version *string
815
Colin Cross79c7c262019-04-17 11:11:46 -0700816 // List of shared java libs that this module has dependencies to
817 Libs []string
Paul Duffin3d1248c2020-04-09 00:10:17 +0100818
819 // The stub sources.
820 Stub_srcs []string `android:"path"`
Paul Duffin1fd005d2020-04-09 01:08:11 +0100821
822 // The current.txt
823 Current_api string `android:"path"`
824
825 // The removed.txt
826 Removed_api string `android:"path"`
Colin Cross79c7c262019-04-17 11:11:46 -0700827}
828
Paul Duffin56d44902020-01-31 13:36:25 +0000829type sdkLibraryImportProperties struct {
Paul Duffinfcfd7912020-01-31 17:54:30 +0000830 // List of shared java libs, common to all scopes, that this module has
831 // dependencies to
832 Libs []string
Paul Duffin56d44902020-01-31 13:36:25 +0000833}
834
Colin Cross79c7c262019-04-17 11:11:46 -0700835type sdkLibraryImport struct {
836 android.ModuleBase
837 android.DefaultableModuleBase
838 prebuilt android.Prebuilt
Paul Duffindd46f712020-02-10 13:37:10 +0000839 android.ApexModuleBase
840 android.SdkBase
Colin Cross79c7c262019-04-17 11:11:46 -0700841
842 properties sdkLibraryImportProperties
843
Paul Duffin46a26a82020-04-07 19:27:04 +0100844 // Map from api scope to the scope specific property structure.
845 scopeProperties map[*apiScope]*sdkLibraryScopeProperties
846
Paul Duffin56d44902020-01-31 13:36:25 +0000847 commonToSdkLibraryAndImport
Colin Cross79c7c262019-04-17 11:11:46 -0700848}
849
850var _ SdkLibraryDependency = (*sdkLibraryImport)(nil)
851
Paul Duffin46a26a82020-04-07 19:27:04 +0100852// The type of a structure that contains a field of type sdkLibraryScopeProperties
853// for each apiscope in allApiScopes, e.g. something like:
854// struct {
855// Public sdkLibraryScopeProperties
856// System sdkLibraryScopeProperties
857// ...
858// }
859var allScopeStructType = createAllScopePropertiesStructType()
860
861// Dynamically create a structure type for each apiscope in allApiScopes.
862func createAllScopePropertiesStructType() reflect.Type {
863 var fields []reflect.StructField
864 for _, apiScope := range allApiScopes {
865 field := reflect.StructField{
866 Name: apiScope.fieldName,
867 Type: reflect.TypeOf(sdkLibraryScopeProperties{}),
868 }
869 fields = append(fields, field)
870 }
871
872 return reflect.StructOf(fields)
873}
874
875// Create an instance of the scope specific structure type and return a map
876// from apiscope to a pointer to each scope specific field.
877func createPropertiesInstance() (interface{}, map[*apiScope]*sdkLibraryScopeProperties) {
878 allScopePropertiesPtr := reflect.New(allScopeStructType)
879 allScopePropertiesStruct := allScopePropertiesPtr.Elem()
880 scopeProperties := make(map[*apiScope]*sdkLibraryScopeProperties)
881
882 for _, apiScope := range allApiScopes {
883 field := allScopePropertiesStruct.FieldByName(apiScope.fieldName)
884 scopeProperties[apiScope] = field.Addr().Interface().(*sdkLibraryScopeProperties)
885 }
886
887 return allScopePropertiesPtr.Interface(), scopeProperties
888}
889
Jaewoong Jung4f158ee2019-07-11 10:05:35 -0700890// java_sdk_library_import imports a prebuilt java_sdk_library.
Colin Cross79c7c262019-04-17 11:11:46 -0700891func sdkLibraryImportFactory() android.Module {
892 module := &sdkLibraryImport{}
893
Paul Duffin46a26a82020-04-07 19:27:04 +0100894 allScopeProperties, scopeToProperties := createPropertiesInstance()
895 module.scopeProperties = scopeToProperties
896 module.AddProperties(&module.properties, allScopeProperties)
Colin Cross79c7c262019-04-17 11:11:46 -0700897
Paul Duffin0bdcb272020-02-06 15:24:57 +0000898 android.InitPrebuiltModule(module, &[]string{""})
Paul Duffindd46f712020-02-10 13:37:10 +0000899 android.InitApexModule(module)
900 android.InitSdkAwareModule(module)
Colin Cross79c7c262019-04-17 11:11:46 -0700901 InitJavaModule(module, android.HostAndDeviceSupported)
902
903 android.AddLoadHook(module, func(mctx android.LoadHookContext) { module.createInternalModules(mctx) })
904 return module
905}
906
907func (module *sdkLibraryImport) Prebuilt() *android.Prebuilt {
908 return &module.prebuilt
909}
910
911func (module *sdkLibraryImport) Name() string {
912 return module.prebuilt.Name(module.ModuleBase.Name())
913}
914
915func (module *sdkLibraryImport) createInternalModules(mctx android.LoadHookContext) {
Colin Cross79c7c262019-04-17 11:11:46 -0700916
Paul Duffin50061512020-01-21 16:31:05 +0000917 // If the build is configured to use prebuilts then force this to be preferred.
918 if mctx.Config().UnbundledBuildUsePrebuiltSdks() {
919 module.prebuilt.ForcePrefer()
920 }
921
Paul Duffin46a26a82020-04-07 19:27:04 +0100922 for apiScope, scopeProperties := range module.scopeProperties {
Paul Duffin56d44902020-01-31 13:36:25 +0000923 if len(scopeProperties.Jars) == 0 {
924 continue
925 }
926
Paul Duffinbbb546b2020-04-09 00:07:11 +0100927 module.createJavaImportForStubs(mctx, apiScope, scopeProperties)
Paul Duffin3d1248c2020-04-09 00:10:17 +0100928
929 module.createPrebuiltStubsSources(mctx, apiScope, scopeProperties)
Paul Duffin56d44902020-01-31 13:36:25 +0000930 }
Colin Cross79c7c262019-04-17 11:11:46 -0700931
932 javaSdkLibraries := javaSdkLibraries(mctx.Config())
933 javaSdkLibrariesLock.Lock()
934 defer javaSdkLibrariesLock.Unlock()
935 *javaSdkLibraries = append(*javaSdkLibraries, module.BaseModuleName())
936}
937
Paul Duffinbbb546b2020-04-09 00:07:11 +0100938func (module *sdkLibraryImport) createJavaImportForStubs(mctx android.LoadHookContext, apiScope *apiScope, scopeProperties *sdkLibraryScopeProperties) {
939 // Creates a java import for the jar with ".stubs" suffix
940 props := struct {
941 Name *string
942 Soc_specific *bool
943 Device_specific *bool
944 Product_specific *bool
945 System_ext_specific *bool
946 Sdk_version *string
947 Libs []string
948 Jars []string
949 Prefer *bool
950 }{}
951 props.Name = proptools.StringPtr(apiScope.stubsModuleName(module.BaseModuleName()))
952 props.Sdk_version = scopeProperties.Sdk_version
953 // Prepend any of the libs from the legacy public properties to the libs for each of the
954 // scopes to avoid having to duplicate them in each scope.
955 props.Libs = append(module.properties.Libs, scopeProperties.Libs...)
956 props.Jars = scopeProperties.Jars
957 if module.SocSpecific() {
958 props.Soc_specific = proptools.BoolPtr(true)
959 } else if module.DeviceSpecific() {
960 props.Device_specific = proptools.BoolPtr(true)
961 } else if module.ProductSpecific() {
962 props.Product_specific = proptools.BoolPtr(true)
963 } else if module.SystemExtSpecific() {
964 props.System_ext_specific = proptools.BoolPtr(true)
965 }
966 // If the build should use prebuilt sdks then set prefer to true on the stubs library.
967 // That will cause the prebuilt version of the stubs to override the source version.
968 if mctx.Config().UnbundledBuildUsePrebuiltSdks() {
969 props.Prefer = proptools.BoolPtr(true)
970 }
971 mctx.CreateModule(ImportFactory, &props)
972}
973
Paul Duffin3d1248c2020-04-09 00:10:17 +0100974func (module *sdkLibraryImport) createPrebuiltStubsSources(mctx android.LoadHookContext, apiScope *apiScope, scopeProperties *sdkLibraryScopeProperties) {
975 props := struct {
976 Name *string
977 Srcs []string
978 }{}
979 props.Name = proptools.StringPtr(apiScope.docsModuleName(module.BaseModuleName()))
980 props.Srcs = scopeProperties.Stub_srcs
981 mctx.CreateModule(PrebuiltStubsSourcesFactory, &props)
982}
983
Colin Cross79c7c262019-04-17 11:11:46 -0700984func (module *sdkLibraryImport) DepsMutator(ctx android.BottomUpMutatorContext) {
Paul Duffin46a26a82020-04-07 19:27:04 +0100985 for apiScope, scopeProperties := range module.scopeProperties {
Paul Duffin56d44902020-01-31 13:36:25 +0000986 if len(scopeProperties.Jars) == 0 {
987 continue
988 }
989
990 // Add dependencies to the prebuilt stubs library
991 ctx.AddVariationDependencies(nil, apiScope.stubsTag, apiScope.stubsModuleName(module.BaseModuleName()))
992 }
Colin Cross79c7c262019-04-17 11:11:46 -0700993}
994
995func (module *sdkLibraryImport) GenerateAndroidBuildActions(ctx android.ModuleContext) {
996 // Record the paths to the prebuilt stubs library.
997 ctx.VisitDirectDeps(func(to android.Module) {
998 tag := ctx.OtherModuleDependencyTag(to)
999
Paul Duffin56d44902020-01-31 13:36:25 +00001000 if lib, ok := to.(Dependency); ok {
1001 if scopeTag, ok := tag.(scopeDependencyTag); ok {
1002 apiScope := scopeTag.apiScope
1003 scopePaths := module.getScopePaths(apiScope)
1004 scopePaths.stubsHeaderPath = lib.HeaderJars()
1005 }
Colin Cross79c7c262019-04-17 11:11:46 -07001006 }
1007 })
1008}
1009
Paul Duffin56d44902020-01-31 13:36:25 +00001010func (module *sdkLibraryImport) sdkJars(
1011 ctx android.BaseModuleContext,
1012 sdkVersion sdkSpec) android.Paths {
1013
Paul Duffin50061512020-01-21 16:31:05 +00001014 // If a specific numeric version has been requested then use prebuilt versions of the sdk.
1015 if sdkVersion.version.isNumbered() {
1016 return PrebuiltJars(ctx, module.BaseModuleName(), sdkVersion)
1017 }
1018
Paul Duffin56d44902020-01-31 13:36:25 +00001019 var apiScope *apiScope
1020 switch sdkVersion.kind {
1021 case sdkSystem:
1022 apiScope = apiScopeSystem
1023 case sdkTest:
1024 apiScope = apiScopeTest
1025 default:
1026 apiScope = apiScopePublic
1027 }
1028
1029 paths := module.getScopePaths(apiScope)
1030 return paths.stubsHeaderPath
1031}
1032
Colin Cross79c7c262019-04-17 11:11:46 -07001033// to satisfy SdkLibraryDependency interface
Jiyong Park6a927c42020-01-21 02:03:43 +09001034func (module *sdkLibraryImport) SdkHeaderJars(ctx android.BaseModuleContext, sdkVersion sdkSpec) android.Paths {
Colin Cross79c7c262019-04-17 11:11:46 -07001035 // This module is just a wrapper for the prebuilt stubs.
Paul Duffin56d44902020-01-31 13:36:25 +00001036 return module.sdkJars(ctx, sdkVersion)
Colin Cross79c7c262019-04-17 11:11:46 -07001037}
1038
1039// to satisfy SdkLibraryDependency interface
Jiyong Park6a927c42020-01-21 02:03:43 +09001040func (module *sdkLibraryImport) SdkImplementationJars(ctx android.BaseModuleContext, sdkVersion sdkSpec) android.Paths {
Colin Cross79c7c262019-04-17 11:11:46 -07001041 // This module is just a wrapper for the stubs.
Paul Duffin56d44902020-01-31 13:36:25 +00001042 return module.sdkJars(ctx, sdkVersion)
Colin Cross79c7c262019-04-17 11:11:46 -07001043}
Jiyong Parke3833882020-02-17 17:28:10 +09001044
1045//
1046// java_sdk_library_xml
1047//
1048type sdkLibraryXml struct {
1049 android.ModuleBase
1050 android.DefaultableModuleBase
1051 android.ApexModuleBase
1052
1053 properties sdkLibraryXmlProperties
1054
1055 outputFilePath android.OutputPath
1056 installDirPath android.InstallPath
1057}
1058
1059type sdkLibraryXmlProperties struct {
1060 // canonical name of the lib
1061 Lib_name *string
1062}
1063
1064// java_sdk_library_xml builds the permission xml file for a java_sdk_library.
1065// Not to be used directly by users. java_sdk_library internally uses this.
1066func sdkLibraryXmlFactory() android.Module {
1067 module := &sdkLibraryXml{}
1068
1069 module.AddProperties(&module.properties)
1070
1071 android.InitApexModule(module)
1072 android.InitAndroidArchModule(module, android.DeviceSupported, android.MultilibCommon)
1073
1074 return module
1075}
1076
1077// from android.PrebuiltEtcModule
1078func (module *sdkLibraryXml) SubDir() string {
1079 return "permissions"
1080}
1081
1082// from android.PrebuiltEtcModule
1083func (module *sdkLibraryXml) OutputFile() android.OutputPath {
1084 return module.outputFilePath
1085}
1086
1087// from android.ApexModule
1088func (module *sdkLibraryXml) AvailableFor(what string) bool {
1089 return true
1090}
1091
1092func (module *sdkLibraryXml) DepsMutator(ctx android.BottomUpMutatorContext) {
1093 // do nothing
1094}
1095
1096// File path to the runtime implementation library
1097func (module *sdkLibraryXml) implPath() string {
1098 implName := proptools.String(module.properties.Lib_name)
1099 if apexName := module.ApexName(); apexName != "" {
1100 // TODO(b/146468504): ApexName() is only a soong module name, not apex name.
1101 // In most cases, this works fine. But when apex_name is set or override_apex is used
1102 // this can be wrong.
1103 return fmt.Sprintf("/apex/%s/javalib/%s.jar", apexName, implName)
1104 }
1105 partition := "system"
1106 if module.SocSpecific() {
1107 partition = "vendor"
1108 } else if module.DeviceSpecific() {
1109 partition = "odm"
1110 } else if module.ProductSpecific() {
1111 partition = "product"
1112 } else if module.SystemExtSpecific() {
1113 partition = "system_ext"
1114 }
1115 return "/" + partition + "/framework/" + implName + ".jar"
1116}
1117
1118func (module *sdkLibraryXml) GenerateAndroidBuildActions(ctx android.ModuleContext) {
1119 libName := proptools.String(module.properties.Lib_name)
1120 xmlContent := fmt.Sprintf(permissionsTemplate, libName, module.implPath())
1121
1122 module.outputFilePath = android.PathForModuleOut(ctx, libName+".xml").OutputPath
1123 rule := android.NewRuleBuilder()
1124 rule.Command().
1125 Text("/bin/bash -c \"echo -e '" + xmlContent + "'\" > ").
1126 Output(module.outputFilePath)
1127
1128 rule.Build(pctx, ctx, "java_sdk_xml", "Permission XML")
1129
1130 module.installDirPath = android.PathForModuleInstall(ctx, "etc", module.SubDir())
1131}
1132
1133func (module *sdkLibraryXml) AndroidMkEntries() []android.AndroidMkEntries {
1134 if !module.IsForPlatform() {
1135 return []android.AndroidMkEntries{android.AndroidMkEntries{
1136 Disabled: true,
1137 }}
1138 }
1139
1140 return []android.AndroidMkEntries{android.AndroidMkEntries{
1141 Class: "ETC",
1142 OutputFile: android.OptionalPathForPath(module.outputFilePath),
1143 ExtraEntries: []android.AndroidMkExtraEntriesFunc{
1144 func(entries *android.AndroidMkEntries) {
1145 entries.SetString("LOCAL_MODULE_TAGS", "optional")
1146 entries.SetString("LOCAL_MODULE_PATH", module.installDirPath.ToMakePath().String())
1147 entries.SetString("LOCAL_INSTALLED_MODULE_STEM", module.outputFilePath.Base())
1148 },
1149 },
1150 }}
1151}
Paul Duffindd46f712020-02-10 13:37:10 +00001152
1153type sdkLibrarySdkMemberType struct {
1154 android.SdkMemberTypeBase
1155}
1156
1157func (s *sdkLibrarySdkMemberType) AddDependencies(mctx android.BottomUpMutatorContext, dependencyTag blueprint.DependencyTag, names []string) {
1158 mctx.AddVariationDependencies(nil, dependencyTag, names...)
1159}
1160
1161func (s *sdkLibrarySdkMemberType) IsInstance(module android.Module) bool {
1162 _, ok := module.(*SdkLibrary)
1163 return ok
1164}
1165
1166func (s *sdkLibrarySdkMemberType) AddPrebuiltModule(ctx android.SdkMemberContext, member android.SdkMember) android.BpModule {
1167 return ctx.SnapshotBuilder().AddPrebuiltModule(member, "java_sdk_library_import")
1168}
1169
1170func (s *sdkLibrarySdkMemberType) CreateVariantPropertiesStruct() android.SdkMemberProperties {
1171 return &sdkLibrarySdkMemberProperties{}
1172}
1173
1174type sdkLibrarySdkMemberProperties struct {
1175 android.SdkMemberPropertiesBase
1176
1177 // Scope to per scope properties.
1178 Scopes map[*apiScope]scopeProperties
1179
1180 // Additional libraries that the exported stubs libraries depend upon.
1181 Libs []string
Paul Duffin3d1248c2020-04-09 00:10:17 +01001182
1183 // The Java stubs source files.
1184 Stub_srcs []string
Paul Duffindd46f712020-02-10 13:37:10 +00001185}
1186
1187type scopeProperties struct {
Paul Duffin1fd005d2020-04-09 01:08:11 +01001188 Jars android.Paths
1189 StubsSrcJar android.Path
1190 CurrentApiFile android.Path
1191 RemovedApiFile android.Path
1192 SdkVersion string
Paul Duffindd46f712020-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 Duffin3d1248c2020-04-09 00:10:17 +01001206 properties.StubsSrcJar = paths.stubsSrcJar
Paul Duffin1fd005d2020-04-09 01:08:11 +01001207 properties.CurrentApiFile = paths.currentApiFilePath
1208 properties.RemovedApiFile = paths.removedApiFilePath
Paul Duffindd46f712020-02-10 13:37:10 +00001209 s.Scopes[apiScope] = properties
1210 }
1211 }
1212
1213 s.Libs = sdk.properties.Libs
1214}
1215
1216func (s *sdkLibrarySdkMemberProperties) AddToPropertySet(ctx android.SdkMemberContext, propertySet android.BpPropertySet) {
1217 for _, apiScope := range allApiScopes {
1218 if properties, ok := s.Scopes[apiScope]; ok {
1219 scopeSet := propertySet.AddPropertySet(apiScope.name)
1220
Paul Duffin3d1248c2020-04-09 00:10:17 +01001221 scopeDir := filepath.Join("sdk_library", s.OsPrefix(), apiScope.name)
1222
Paul Duffindd46f712020-02-10 13:37:10 +00001223 var jars []string
1224 for _, p := range properties.Jars {
Paul Duffin3d1248c2020-04-09 00:10:17 +01001225 dest := filepath.Join(scopeDir, ctx.Name()+"-stubs.jar")
Paul Duffindd46f712020-02-10 13:37:10 +00001226 ctx.SnapshotBuilder().CopyToSnapshot(p, dest)
1227 jars = append(jars, dest)
1228 }
1229 scopeSet.AddProperty("jars", jars)
1230
Paul Duffin3d1248c2020-04-09 00:10:17 +01001231 // Merge the stubs source jar into the snapshot zip so that when it is unpacked
1232 // the source files are also unpacked.
1233 snapshotRelativeDir := filepath.Join(scopeDir, ctx.Name()+"_stub_sources")
1234 ctx.SnapshotBuilder().UnzipToSnapshot(properties.StubsSrcJar, snapshotRelativeDir)
1235 scopeSet.AddProperty("stub_srcs", []string{snapshotRelativeDir})
1236
Paul Duffin1fd005d2020-04-09 01:08:11 +01001237 if properties.CurrentApiFile != nil {
1238 currentApiSnapshotPath := filepath.Join(scopeDir, ctx.Name()+".txt")
1239 ctx.SnapshotBuilder().CopyToSnapshot(properties.CurrentApiFile, currentApiSnapshotPath)
1240 scopeSet.AddProperty("current_api", currentApiSnapshotPath)
1241 }
1242
1243 if properties.RemovedApiFile != nil {
1244 removedApiSnapshotPath := filepath.Join(scopeDir, ctx.Name()+"-removed.txt")
1245 ctx.SnapshotBuilder().CopyToSnapshot(properties.CurrentApiFile, removedApiSnapshotPath)
1246 scopeSet.AddProperty("removed_api", removedApiSnapshotPath)
1247 }
1248
Paul Duffindd46f712020-02-10 13:37:10 +00001249 if properties.SdkVersion != "" {
1250 scopeSet.AddProperty("sdk_version", properties.SdkVersion)
1251 }
1252 }
1253 }
1254
1255 if len(s.Libs) > 0 {
1256 propertySet.AddPropertyWithTag("libs", s.Libs, ctx.SnapshotBuilder().SdkMemberReferencePropertyTag(false))
1257 }
1258}