blob: 9b6d57557631700eda0b6508dcdf056fbf06e03e [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
86 // The suffix to add to the make variable that references the location of the api file.
87 apiFileMakeVariableSuffix string
88
89 // SDK version that the stubs library is built against. Note that this is always
90 // *current. Older stubs library built with a numbered SDK version is created from
91 // the prebuilt jar.
92 sdkVersion string
Paul Duffin1fb487d2020-04-07 18:50:10 +010093
94 // Extra arguments to pass to droidstubs for this scope.
95 droidstubsArgs []string
Paul Duffind1b3a922020-01-22 11:57:20 +000096}
97
98// Initialize a scope, creating and adding appropriate dependency tags
99func initApiScope(scope *apiScope) *apiScope {
Paul 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{
136 name: "system",
137 apiFilePrefix: "system-",
138 moduleSuffix: sdkSystemApiSuffix,
139 apiFileMakeVariableSuffix: "_SYSTEM",
140 sdkVersion: "system_current",
Paul Duffin1fb487d2020-04-07 18:50:10 +0100141 droidstubsArgs: []string{"-showAnnotation android.annotation.SystemApi"},
Paul Duffind1b3a922020-01-22 11:57:20 +0000142 })
143 apiScopeTest = initApiScope(&apiScope{
144 name: "test",
145 apiFilePrefix: "test-",
146 moduleSuffix: sdkTestApiSuffix,
147 apiFileMakeVariableSuffix: "_TEST",
148 sdkVersion: "test_current",
Paul Duffin1fb487d2020-04-07 18:50:10 +0100149 droidstubsArgs: []string{"-showAnnotation android.annotation.TestApi"},
Paul Duffind1b3a922020-01-22 11:57:20 +0000150 })
151 allApiScopes = apiScopes{
152 apiScopePublic,
153 apiScopeSystem,
154 apiScopeTest,
155 }
Jiyong Parkc678ad32018-04-10 13:07:10 +0900156)
157
Jiyong Park82484c02018-04-23 21:41:26 +0900158var (
159 javaSdkLibrariesLock sync.Mutex
160)
161
Jiyong Parkc678ad32018-04-10 13:07:10 +0900162// TODO: these are big features that are currently missing
Jiyong Park1be96912018-05-28 18:02:19 +0900163// 1) disallowing linking to the runtime shared lib
164// 2) HTML generation
Jiyong Parkc678ad32018-04-10 13:07:10 +0900165
166func init() {
Paul Duffin43dc1cc2019-12-19 11:18:54 +0000167 RegisterSdkLibraryBuildComponents(android.InitRegistrationContext)
Jiyong Parkc678ad32018-04-10 13:07:10 +0900168
Jiyong Park82484c02018-04-23 21:41:26 +0900169 android.RegisterMakeVarsProvider(pctx, func(ctx android.MakeVarsContext) {
170 javaSdkLibraries := javaSdkLibraries(ctx.Config())
171 sort.Strings(*javaSdkLibraries)
172 ctx.Strict("JAVA_SDK_LIBRARIES", strings.Join(*javaSdkLibraries, " "))
173 })
Paul Duffindd46f712020-02-10 13:37:10 +0000174
175 // Register sdk member types.
176 android.RegisterSdkMemberType(&sdkLibrarySdkMemberType{
177 android.SdkMemberTypeBase{
178 PropertyName: "java_sdk_libs",
179 SupportsSdk: true,
180 },
181 })
Jiyong Parkc678ad32018-04-10 13:07:10 +0900182}
183
Paul Duffin43dc1cc2019-12-19 11:18:54 +0000184func RegisterSdkLibraryBuildComponents(ctx android.RegistrationContext) {
185 ctx.RegisterModuleType("java_sdk_library", SdkLibraryFactory)
186 ctx.RegisterModuleType("java_sdk_library_import", sdkLibraryImportFactory)
187}
188
Jiyong Parkc678ad32018-04-10 13:07:10 +0900189type sdkLibraryProperties struct {
Sundong Ahnf043cf62018-06-25 16:04:37 +0900190 // List of Java libraries that will be in the classpath when building stubs
191 Stub_only_libs []string `android:"arch_variant"`
192
Paul Duffin7a586d32019-12-30 17:09:34 +0000193 // list of package names that will be documented and publicized as API.
194 // This allows the API to be restricted to a subset of the source files provided.
195 // If this is unspecified then all the source files will be treated as being part
196 // of the API.
Jiyong Parkc678ad32018-04-10 13:07:10 +0900197 Api_packages []string
198
Jiyong Park5a2c9d72018-05-01 22:25:41 +0900199 // list of package names that must be hidden from the API
200 Hidden_api_packages []string
201
Paul Duffin749f98f2019-12-30 17:23:46 +0000202 // the relative path to the directory containing the api specification files.
203 // Defaults to "api".
204 Api_dir *string
205
Paul Duffin43db9be2019-12-30 17:35:49 +0000206 // If set to true there is no runtime library.
207 Api_only *bool
208
Paul Duffin11512472019-02-11 15:55:17 +0000209 // local files that are used within user customized droiddoc options.
210 Droiddoc_option_files []string
211
212 // additional droiddoc options
213 // Available variables for substitution:
214 //
215 // $(location <label>): the path to the droiddoc_option_files with name <label>
Sundong Ahndd567f92018-07-31 17:19:11 +0900216 Droiddoc_options []string
217
Sundong Ahn054b19a2018-10-19 13:46:09 +0900218 // a list of top-level directories containing files to merge qualifier annotations
219 // (i.e. those intended to be included in the stubs written) from.
220 Merge_annotations_dirs []string
221
222 // a list of top-level directories containing Java stub files to merge show/hide annotations from.
223 Merge_inclusion_annotations_dirs []string
224
225 // If set to true, the path of dist files is apistubs/core. Defaults to false.
226 Core_lib *bool
227
Sundong Ahn80a87b32019-05-13 15:02:50 +0900228 // don't create dist rules.
229 No_dist *bool `blueprint:"mutated"`
230
Paul Duffin37e0b772019-12-30 17:20:10 +0000231 // indicates whether system and test apis should be managed.
232 Has_system_and_test_apis bool `blueprint:"mutated"`
233
Jiyong Parkc678ad32018-04-10 13:07:10 +0900234 // TODO: determines whether to create HTML doc or not
235 //Html_doc *bool
236}
237
Paul Duffind1b3a922020-01-22 11:57:20 +0000238type scopePaths struct {
239 stubsHeaderPath android.Paths
240 stubsImplPath android.Paths
241 apiFilePath android.Path
Paul Duffin3d1248c2020-04-09 00:10:17 +0100242 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)
333 scopePaths.apiFilePath = doc.ApiFilePath()
Paul Duffin3d1248c2020-04-09 00:10:17 +0100334 scopePaths.stubsSrcJar = doc.StubsSrcJar()
Paul Duffind1b3a922020-01-22 11:57:20 +0000335 } else {
Sundong Ahn20e998b2018-07-24 11:19:26 +0900336 ctx.ModuleErrorf("depends on module %q of unknown tag %q", otherName, tag)
337 }
338 }
Jiyong Parkc678ad32018-04-10 13:07:10 +0900339 })
340}
341
Jiyong Park0b0e1b92019-12-03 13:24:29 +0900342func (module *SdkLibrary) AndroidMkEntries() []android.AndroidMkEntries {
Paul Duffin43db9be2019-12-30 17:35:49 +0000343 if proptools.Bool(module.sdkLibraryProperties.Api_only) {
344 return nil
345 }
Jiyong Park0b0e1b92019-12-03 13:24:29 +0900346 entriesList := module.Library.AndroidMkEntries()
347 entries := &entriesList[0]
Jaewoong Jungb0c127c2019-08-29 14:56:03 -0700348 entries.Required = append(entries.Required, module.xmlFileName())
Jiyong Park0b0e1b92019-12-03 13:24:29 +0900349 return entriesList
Jiyong Park82484c02018-04-23 21:41:26 +0900350}
351
Jiyong Parkc678ad32018-04-10 13:07:10 +0900352// Module name of the stubs library
Paul Duffind1b3a922020-01-22 11:57:20 +0000353func (module *SdkLibrary) stubsName(apiScope *apiScope) string {
354 return apiScope.stubsModuleName(module.BaseModuleName())
Jiyong Parkc678ad32018-04-10 13:07:10 +0900355}
356
357// Module name of the docs
Paul Duffind1b3a922020-01-22 11:57:20 +0000358func (module *SdkLibrary) docsName(apiScope *apiScope) string {
359 return apiScope.docsModuleName(module.BaseModuleName())
Jiyong Parkc678ad32018-04-10 13:07:10 +0900360}
361
362// Module name of the runtime implementation library
Inseob Kimc0907f12019-02-08 21:00:45 +0900363func (module *SdkLibrary) implName() string {
Sundong Ahn054b19a2018-10-19 13:46:09 +0900364 return module.BaseModuleName()
Jiyong Parkc678ad32018-04-10 13:07:10 +0900365}
366
Jiyong Parkc678ad32018-04-10 13:07:10 +0900367// Module name of the XML file for the lib
Inseob Kimc0907f12019-02-08 21:00:45 +0900368func (module *SdkLibrary) xmlFileName() string {
Jiyong Parkc678ad32018-04-10 13:07:10 +0900369 return module.BaseModuleName() + sdkXmlFileSuffix
370}
371
Anton Hansson5fd5d242020-03-27 19:43:19 +0000372// The dist path of the stub artifacts
373func (module *SdkLibrary) apiDistPath(apiScope *apiScope) string {
374 if module.ModuleBase.Owner() != "" {
375 return path.Join("apistubs", module.ModuleBase.Owner(), apiScope.name)
376 } else if Bool(module.sdkLibraryProperties.Core_lib) {
377 return path.Join("apistubs", "core", apiScope.name)
378 } else {
379 return path.Join("apistubs", "android", apiScope.name)
380 }
381}
382
Paul Duffin12ceb462019-12-24 20:31:31 +0000383// Get the sdk version for use when compiling the stubs library.
Paul Duffind1b3a922020-01-22 11:57:20 +0000384func (module *SdkLibrary) sdkVersionForStubsLibrary(mctx android.LoadHookContext, apiScope *apiScope) string {
Paul Duffin12ceb462019-12-24 20:31:31 +0000385 sdkDep := decodeSdkDep(mctx, sdkContext(&module.Library))
386 if sdkDep.hasStandardLibs() {
387 // If building against a standard sdk then use the sdk version appropriate for the scope.
Paul Duffind1b3a922020-01-22 11:57:20 +0000388 return apiScope.sdkVersion
Paul Duffin12ceb462019-12-24 20:31:31 +0000389 } else {
390 // Otherwise, use no system module.
391 return "none"
392 }
393}
394
Jiyong Parkc678ad32018-04-10 13:07:10 +0900395// $(INTERNAL_PLATFORM_<apiTagName>_API_FILE) points to the generated
396// api file for the current source
397// TODO: remove this when apicheck is done in soong
Paul Duffind1b3a922020-01-22 11:57:20 +0000398func (module *SdkLibrary) apiTagName(apiScope *apiScope) string {
399 return strings.Replace(strings.ToUpper(module.BaseModuleName()), ".", "_", -1) + apiScope.apiFileMakeVariableSuffix
Jiyong Parkc678ad32018-04-10 13:07:10 +0900400}
401
Paul Duffind1b3a922020-01-22 11:57:20 +0000402func (module *SdkLibrary) latestApiFilegroupName(apiScope *apiScope) string {
403 return ":" + module.BaseModuleName() + ".api." + apiScope.name + ".latest"
Jiyong Park58c518b2018-05-12 22:29:12 +0900404}
Jiyong Parkc678ad32018-04-10 13:07:10 +0900405
Paul Duffind1b3a922020-01-22 11:57:20 +0000406func (module *SdkLibrary) latestRemovedApiFilegroupName(apiScope *apiScope) string {
407 return ":" + module.BaseModuleName() + "-removed.api." + apiScope.name + ".latest"
Jiyong Parkc678ad32018-04-10 13:07:10 +0900408}
409
410// Creates a static java library that has API stubs
Paul Duffind1b3a922020-01-22 11:57:20 +0000411func (module *SdkLibrary) createStubsLibrary(mctx android.LoadHookContext, apiScope *apiScope) {
Jiyong Parkc678ad32018-04-10 13:07:10 +0900412 props := struct {
Sundong Ahn0d7dff42019-12-04 12:53:44 +0900413 Name *string
414 Srcs []string
Paul Duffin367ab912019-12-23 19:40:36 +0000415 Installable *bool
Sundong Ahn0d7dff42019-12-04 12:53:44 +0900416 Sdk_version *string
Paul Duffin12ceb462019-12-24 20:31:31 +0000417 System_modules *string
Paul Duffinab8da5d2020-02-07 16:12:04 +0000418 Patch_module *string
Sundong Ahn0d7dff42019-12-04 12:53:44 +0900419 Libs []string
420 Soc_specific *bool
421 Device_specific *bool
422 Product_specific *bool
423 System_ext_specific *bool
424 Compile_dex *bool
Sundong Ahn0d7dff42019-12-04 12:53:44 +0900425 Java_version *string
426 Product_variables struct {
Jiyong Park82484c02018-04-23 21:41:26 +0900427 Pdk struct {
428 Enabled *bool
429 }
Jiyong Parkc678ad32018-04-10 13:07:10 +0900430 }
Sundong Ahn054b19a2018-10-19 13:46:09 +0900431 Openjdk9 struct {
432 Srcs []string
433 Javacflags []string
434 }
Anton Hansson5fd5d242020-03-27 19:43:19 +0000435 Dist struct {
436 Targets []string
437 Dest *string
438 Dir *string
439 Tag *string
440 }
Jiyong Parkc678ad32018-04-10 13:07:10 +0900441 }{}
442
Jiyong Parkdf130542018-04-27 16:29:21 +0900443 props.Name = proptools.StringPtr(module.stubsName(apiScope))
Jiyong Parkc678ad32018-04-10 13:07:10 +0900444 // sources are generated from the droiddoc
Jiyong Parkdf130542018-04-27 16:29:21 +0900445 props.Srcs = []string{":" + module.docsName(apiScope)}
Paul Duffin12ceb462019-12-24 20:31:31 +0000446 sdkVersion := module.sdkVersionForStubsLibrary(mctx, apiScope)
Paul Duffin52d398a2019-06-11 12:31:14 +0100447 props.Sdk_version = proptools.StringPtr(sdkVersion)
Paul Duffin12ceb462019-12-24 20:31:31 +0000448 props.System_modules = module.Library.Module.deviceProperties.System_modules
Paul Duffinab8da5d2020-02-07 16:12:04 +0000449 props.Patch_module = module.Library.Module.properties.Patch_module
Paul Duffin367ab912019-12-23 19:40:36 +0000450 props.Installable = proptools.BoolPtr(false)
Sundong Ahn054b19a2018-10-19 13:46:09 +0900451 props.Libs = module.sdkLibraryProperties.Stub_only_libs
Jiyong Park82484c02018-04-23 21:41:26 +0900452 props.Product_variables.Pdk.Enabled = proptools.BoolPtr(false)
Sundong Ahn054b19a2018-10-19 13:46:09 +0900453 props.Openjdk9.Srcs = module.Library.Module.properties.Openjdk9.Srcs
454 props.Openjdk9.Javacflags = module.Library.Module.properties.Openjdk9.Javacflags
455 props.Java_version = module.Library.Module.properties.Java_version
456 if module.Library.Module.deviceProperties.Compile_dex != nil {
457 props.Compile_dex = module.Library.Module.deviceProperties.Compile_dex
Sundong Ahndd567f92018-07-31 17:19:11 +0900458 }
Jiyong Parkc678ad32018-04-10 13:07:10 +0900459
460 if module.SocSpecific() {
461 props.Soc_specific = proptools.BoolPtr(true)
462 } else if module.DeviceSpecific() {
463 props.Device_specific = proptools.BoolPtr(true)
464 } else if module.ProductSpecific() {
465 props.Product_specific = proptools.BoolPtr(true)
Sundong Ahn0d7dff42019-12-04 12:53:44 +0900466 } else if module.SystemExtSpecific() {
467 props.System_ext_specific = proptools.BoolPtr(true)
Jiyong Parkc678ad32018-04-10 13:07:10 +0900468 }
Anton Hansson5fd5d242020-03-27 19:43:19 +0000469 // Dist the class jar artifact for sdk builds.
470 if !Bool(module.sdkLibraryProperties.No_dist) {
471 props.Dist.Targets = []string{"sdk", "win_sdk"}
472 props.Dist.Dest = proptools.StringPtr(fmt.Sprintf("%v.jar", module.BaseModuleName()))
473 props.Dist.Dir = proptools.StringPtr(module.apiDistPath(apiScope))
474 props.Dist.Tag = proptools.StringPtr(".jar")
475 }
Jiyong Parkc678ad32018-04-10 13:07:10 +0900476
Colin Cross84dfc3d2019-09-25 11:33:01 -0700477 mctx.CreateModule(LibraryFactory, &props)
Jiyong Parkc678ad32018-04-10 13:07:10 +0900478}
479
Paul Duffin6d0886e2020-04-07 18:49:53 +0100480// Creates a droidstubs module that creates stubs source files from the given full source
Jiyong Parkc678ad32018-04-10 13:07:10 +0900481// files
Paul Duffind1b3a922020-01-22 11:57:20 +0000482func (module *SdkLibrary) createStubsSources(mctx android.LoadHookContext, apiScope *apiScope) {
Jiyong Parkc678ad32018-04-10 13:07:10 +0900483 props := struct {
Sundong Ahn054b19a2018-10-19 13:46:09 +0900484 Name *string
485 Srcs []string
486 Installable *bool
Paul Duffin52d398a2019-06-11 12:31:14 +0100487 Sdk_version *string
Paul Duffin12ceb462019-12-24 20:31:31 +0000488 System_modules *string
Sundong Ahn054b19a2018-10-19 13:46:09 +0900489 Libs []string
Paul Duffin11512472019-02-11 15:55:17 +0000490 Arg_files []string
Sundong Ahn054b19a2018-10-19 13:46:09 +0900491 Args *string
492 Api_tag_name *string
493 Api_filename *string
494 Removed_api_filename *string
Sundong Ahn054b19a2018-10-19 13:46:09 +0900495 Java_version *string
496 Merge_annotations_dirs []string
497 Merge_inclusion_annotations_dirs []string
498 Check_api struct {
Inseob Kim38449af2019-02-28 14:24:05 +0900499 Current ApiToCheck
500 Last_released ApiToCheck
501 Ignore_missing_latest_api *bool
Jiyong Park58c518b2018-05-12 22:29:12 +0900502 }
Sundong Ahn1b92c822018-05-29 11:35:17 +0900503 Aidl struct {
504 Include_dirs []string
505 Local_include_dirs []string
506 }
Anton Hansson5fd5d242020-03-27 19:43:19 +0000507 Dist struct {
508 Targets []string
509 Dest *string
510 Dir *string
511 }
Jiyong Parkc678ad32018-04-10 13:07:10 +0900512 }{}
513
Paul Duffin250e6192019-06-07 10:44:37 +0100514 sdkDep := decodeSdkDep(mctx, sdkContext(&module.Library))
Paul Duffin12ceb462019-12-24 20:31:31 +0000515 // Use the platform API if standard libraries were requested, otherwise use
516 // no default libraries.
Paul Duffin52d398a2019-06-11 12:31:14 +0100517 sdkVersion := ""
518 if !sdkDep.hasStandardLibs() {
519 sdkVersion = "none"
520 }
Paul Duffin250e6192019-06-07 10:44:37 +0100521
Jiyong Parkdf130542018-04-27 16:29:21 +0900522 props.Name = proptools.StringPtr(module.docsName(apiScope))
Sundong Ahn054b19a2018-10-19 13:46:09 +0900523 props.Srcs = append(props.Srcs, module.Library.Module.properties.Srcs...)
Paul Duffin52d398a2019-06-11 12:31:14 +0100524 props.Sdk_version = proptools.StringPtr(sdkVersion)
Paul Duffin12ceb462019-12-24 20:31:31 +0000525 props.System_modules = module.Library.Module.deviceProperties.System_modules
Jiyong Parkc678ad32018-04-10 13:07:10 +0900526 props.Installable = proptools.BoolPtr(false)
Sundong Ahne6f0b052018-06-05 16:46:14 +0900527 // A droiddoc module has only one Libs property and doesn't distinguish between
528 // shared libs and static libs. So we need to add both of these libs to Libs property.
Sundong Ahn054b19a2018-10-19 13:46:09 +0900529 props.Libs = module.Library.Module.properties.Libs
530 props.Libs = append(props.Libs, module.Library.Module.properties.Static_libs...)
531 props.Aidl.Include_dirs = module.Library.Module.deviceProperties.Aidl.Include_dirs
532 props.Aidl.Local_include_dirs = module.Library.Module.deviceProperties.Aidl.Local_include_dirs
Sundong Ahn054b19a2018-10-19 13:46:09 +0900533 props.Java_version = module.Library.Module.properties.Java_version
Jiyong Parkc678ad32018-04-10 13:07:10 +0900534
Sundong Ahn054b19a2018-10-19 13:46:09 +0900535 props.Merge_annotations_dirs = module.sdkLibraryProperties.Merge_annotations_dirs
536 props.Merge_inclusion_annotations_dirs = module.sdkLibraryProperties.Merge_inclusion_annotations_dirs
537
Paul Duffin6d0886e2020-04-07 18:49:53 +0100538 droidstubsArgs := []string{}
Paul Duffin235ffff2019-12-24 10:41:30 +0000539 if len(module.sdkLibraryProperties.Api_packages) != 0 {
Paul Duffin6d0886e2020-04-07 18:49:53 +0100540 droidstubsArgs = append(droidstubsArgs, "--stub-packages "+strings.Join(module.sdkLibraryProperties.Api_packages, ":"))
Paul Duffin235ffff2019-12-24 10:41:30 +0000541 }
542 if len(module.sdkLibraryProperties.Hidden_api_packages) != 0 {
Paul Duffin6d0886e2020-04-07 18:49:53 +0100543 droidstubsArgs = append(droidstubsArgs,
Paul Duffin235ffff2019-12-24 10:41:30 +0000544 android.JoinWithPrefix(module.sdkLibraryProperties.Hidden_api_packages, " --hide-package "))
545 }
Paul Duffin6d0886e2020-04-07 18:49:53 +0100546 droidstubsArgs = append(droidstubsArgs, module.sdkLibraryProperties.Droiddoc_options...)
Paul Duffin235ffff2019-12-24 10:41:30 +0000547 disabledWarnings := []string{
548 "MissingPermission",
549 "BroadcastBehavior",
550 "HiddenSuperclass",
551 "DeprecationMismatch",
552 "UnavailableSymbol",
553 "SdkConstant",
554 "HiddenTypeParameter",
555 "Todo",
556 "Typo",
557 }
Paul Duffin6d0886e2020-04-07 18:49:53 +0100558 droidstubsArgs = append(droidstubsArgs, android.JoinWithPrefix(disabledWarnings, "--hide "))
Sundong Ahnfb2721f2018-09-17 13:23:09 +0900559
Paul Duffin1fb487d2020-04-07 18:50:10 +0100560 // Add in scope specific arguments.
561 droidstubsArgs = append(droidstubsArgs, apiScope.droidstubsArgs...)
Paul Duffin11512472019-02-11 15:55:17 +0000562 props.Arg_files = module.sdkLibraryProperties.Droiddoc_option_files
Paul Duffin6d0886e2020-04-07 18:49:53 +0100563 props.Args = proptools.StringPtr(strings.Join(droidstubsArgs, " "))
Jiyong Parkc678ad32018-04-10 13:07:10 +0900564
565 // List of APIs identified from the provided source files are created. They are later
566 // compared against to the not-yet-released (a.k.a current) list of APIs and to the
567 // last-released (a.k.a numbered) list of API.
Paul Duffind1b3a922020-01-22 11:57:20 +0000568 currentApiFileName := apiScope.apiFilePrefix + "current.txt"
569 removedApiFileName := apiScope.apiFilePrefix + "removed.txt"
Paul Duffin749f98f2019-12-30 17:23:46 +0000570 apiDir := module.getApiDir()
571 currentApiFileName = path.Join(apiDir, currentApiFileName)
572 removedApiFileName = path.Join(apiDir, removedApiFileName)
Jiyong Park58c518b2018-05-12 22:29:12 +0900573 // TODO(jiyong): remove these three props
Jiyong Parkdf130542018-04-27 16:29:21 +0900574 props.Api_tag_name = proptools.StringPtr(module.apiTagName(apiScope))
Jiyong Parkc678ad32018-04-10 13:07:10 +0900575 props.Api_filename = proptools.StringPtr(currentApiFileName)
576 props.Removed_api_filename = proptools.StringPtr(removedApiFileName)
577
Jiyong Park58c518b2018-05-12 22:29:12 +0900578 // check against the not-yet-release API
579 props.Check_api.Current.Api_file = proptools.StringPtr(currentApiFileName)
580 props.Check_api.Current.Removed_api_file = proptools.StringPtr(removedApiFileName)
Jiyong Park58c518b2018-05-12 22:29:12 +0900581
582 // check against the latest released API
583 props.Check_api.Last_released.Api_file = proptools.StringPtr(
584 module.latestApiFilegroupName(apiScope))
585 props.Check_api.Last_released.Removed_api_file = proptools.StringPtr(
586 module.latestRemovedApiFilegroupName(apiScope))
Inseob Kim38449af2019-02-28 14:24:05 +0900587 props.Check_api.Ignore_missing_latest_api = proptools.BoolPtr(true)
Jiyong Park58c518b2018-05-12 22:29:12 +0900588
Anton Hansson5fd5d242020-03-27 19:43:19 +0000589 // Dist the api txt artifact for sdk builds.
590 if !Bool(module.sdkLibraryProperties.No_dist) {
591 props.Dist.Targets = []string{"sdk", "win_sdk"}
592 props.Dist.Dest = proptools.StringPtr(fmt.Sprintf("%v.txt", module.BaseModuleName()))
593 props.Dist.Dir = proptools.StringPtr(path.Join(module.apiDistPath(apiScope), "api"))
594 }
595
Colin Cross84dfc3d2019-09-25 11:33:01 -0700596 mctx.CreateModule(DroidstubsFactory, &props)
Jiyong Parkc678ad32018-04-10 13:07:10 +0900597}
598
Jooyung Han5e9013b2020-03-10 06:23:13 +0900599func (module *SdkLibrary) DepIsInSameApex(mctx android.BaseModuleContext, dep android.Module) bool {
600 depTag := mctx.OtherModuleDependencyTag(dep)
601 if depTag == xmlPermissionsFileTag {
602 return true
603 }
604 return module.Library.DepIsInSameApex(mctx, dep)
605}
606
Jiyong Parkc678ad32018-04-10 13:07:10 +0900607// Creates the xml file that publicizes the runtime library
Colin Crossf8b860a2019-04-16 14:43:28 -0700608func (module *SdkLibrary) createXmlFile(mctx android.LoadHookContext) {
Jiyong Parke3833882020-02-17 17:28:10 +0900609 props := struct {
Sundong Ahn0d7dff42019-12-04 12:53:44 +0900610 Name *string
Jiyong Parke3833882020-02-17 17:28:10 +0900611 Lib_name *string
Sundong Ahn0d7dff42019-12-04 12:53:44 +0900612 Soc_specific *bool
613 Device_specific *bool
614 Product_specific *bool
615 System_ext_specific *bool
Jooyung Han5e9013b2020-03-10 06:23:13 +0900616 Apex_available []string
Jiyong Parke3833882020-02-17 17:28:10 +0900617 }{
Jooyung Han5e9013b2020-03-10 06:23:13 +0900618 Name: proptools.StringPtr(module.xmlFileName()),
619 Lib_name: proptools.StringPtr(module.BaseModuleName()),
620 Apex_available: module.ApexProperties.Apex_available,
Jiyong Parkc678ad32018-04-10 13:07:10 +0900621 }
Jiyong Parke3833882020-02-17 17:28:10 +0900622
623 if module.SocSpecific() {
624 props.Soc_specific = proptools.BoolPtr(true)
625 } else if module.DeviceSpecific() {
626 props.Device_specific = proptools.BoolPtr(true)
627 } else if module.ProductSpecific() {
628 props.Product_specific = proptools.BoolPtr(true)
629 } else if module.SystemExtSpecific() {
630 props.System_ext_specific = proptools.BoolPtr(true)
631 }
632
633 mctx.CreateModule(sdkLibraryXmlFactory, &props)
Jiyong Parkc678ad32018-04-10 13:07:10 +0900634}
635
Paul Duffin50061512020-01-21 16:31:05 +0000636func PrebuiltJars(ctx android.BaseModuleContext, baseName string, s sdkSpec) android.Paths {
Jiyong Park6a927c42020-01-21 02:03:43 +0900637 var ver sdkVersion
638 var kind sdkKind
639 if s.usePrebuilt(ctx) {
640 ver = s.version
641 kind = s.kind
Jiyong Parkc678ad32018-04-10 13:07:10 +0900642 } else {
Jiyong Park6a927c42020-01-21 02:03:43 +0900643 // We don't have prebuilt SDK for the specific sdkVersion.
644 // Instead of breaking the build, fallback to use "system_current"
645 ver = sdkVersionCurrent
646 kind = sdkSystem
Sundong Ahn054b19a2018-10-19 13:46:09 +0900647 }
Jiyong Park6a927c42020-01-21 02:03:43 +0900648
649 dir := filepath.Join("prebuilts", "sdk", ver.String(), kind.String())
Paul Duffin50061512020-01-21 16:31:05 +0000650 jar := filepath.Join(dir, baseName+".jar")
Sundong Ahn054b19a2018-10-19 13:46:09 +0900651 jarPath := android.ExistentPathForSource(ctx, jar)
Sundong Ahnae418ac2019-02-28 15:01:28 +0900652 if !jarPath.Valid() {
Colin Cross07c88562020-01-07 09:34:44 -0800653 if ctx.Config().AllowMissingDependencies() {
654 return android.Paths{android.PathForSource(ctx, jar)}
655 } else {
Jiyong Park6a927c42020-01-21 02:03:43 +0900656 ctx.PropertyErrorf("sdk_library", "invalid sdk version %q, %q does not exist", s.raw, jar)
Colin Cross07c88562020-01-07 09:34:44 -0800657 }
Sundong Ahnae418ac2019-02-28 15:01:28 +0900658 return nil
659 }
Sundong Ahn054b19a2018-10-19 13:46:09 +0900660 return android.Paths{jarPath.Path()}
661}
662
Paul Duffind1b3a922020-01-22 11:57:20 +0000663func (module *SdkLibrary) sdkJars(
664 ctx android.BaseModuleContext,
665 sdkVersion sdkSpec,
666 headerJars bool) android.Paths {
667
Paul Duffin50061512020-01-21 16:31:05 +0000668 // If a specific numeric version has been requested then use prebuilt versions of the sdk.
669 if sdkVersion.version.isNumbered() {
670 return PrebuiltJars(ctx, module.BaseModuleName(), sdkVersion)
Sundong Ahn054b19a2018-10-19 13:46:09 +0900671 } else {
Paul Duffind1b3a922020-01-22 11:57:20 +0000672 if !sdkVersion.specified() {
673 if headerJars {
674 return module.Library.HeaderJars()
675 } else {
676 return module.Library.ImplementationJars()
677 }
678 }
Paul Duffin726d23c2020-01-22 16:30:37 +0000679 var apiScope *apiScope
Jiyong Park6a927c42020-01-21 02:03:43 +0900680 switch sdkVersion.kind {
681 case sdkSystem:
Paul Duffin726d23c2020-01-22 16:30:37 +0000682 apiScope = apiScopeSystem
683 case sdkTest:
684 apiScope = apiScopeTest
Jiyong Park6a927c42020-01-21 02:03:43 +0900685 case sdkPrivate:
Sundong Ahn054b19a2018-10-19 13:46:09 +0900686 return module.Library.HeaderJars()
Jiyong Park6a927c42020-01-21 02:03:43 +0900687 default:
Paul Duffin726d23c2020-01-22 16:30:37 +0000688 apiScope = apiScopePublic
Paul Duffind1b3a922020-01-22 11:57:20 +0000689 }
690
Paul Duffin726d23c2020-01-22 16:30:37 +0000691 paths := module.getScopePaths(apiScope)
Paul Duffind1b3a922020-01-22 11:57:20 +0000692 if headerJars {
693 return paths.stubsHeaderPath
694 } else {
695 return paths.stubsImplPath
Sundong Ahn054b19a2018-10-19 13:46:09 +0900696 }
Jiyong Parkc678ad32018-04-10 13:07:10 +0900697 }
698}
699
Sundong Ahn241cd372018-07-13 16:16:44 +0900700// to satisfy SdkLibraryDependency interface
Paul Duffind1b3a922020-01-22 11:57:20 +0000701func (module *SdkLibrary) SdkHeaderJars(ctx android.BaseModuleContext, sdkVersion sdkSpec) android.Paths {
702 return module.sdkJars(ctx, sdkVersion, true /*headerJars*/)
703}
704
705// to satisfy SdkLibraryDependency interface
Jiyong Park6a927c42020-01-21 02:03:43 +0900706func (module *SdkLibrary) SdkImplementationJars(ctx android.BaseModuleContext, sdkVersion sdkSpec) android.Paths {
Paul Duffind1b3a922020-01-22 11:57:20 +0000707 return module.sdkJars(ctx, sdkVersion, false /*headerJars*/)
Sundong Ahn241cd372018-07-13 16:16:44 +0900708}
709
Sundong Ahn80a87b32019-05-13 15:02:50 +0900710func (module *SdkLibrary) SetNoDist() {
711 module.sdkLibraryProperties.No_dist = proptools.BoolPtr(true)
712}
713
Colin Cross571cccf2019-02-04 11:22:08 -0800714var javaSdkLibrariesKey = android.NewOnceKey("javaSdkLibraries")
715
Jiyong Park82484c02018-04-23 21:41:26 +0900716func javaSdkLibraries(config android.Config) *[]string {
Colin Cross571cccf2019-02-04 11:22:08 -0800717 return config.Once(javaSdkLibrariesKey, func() interface{} {
Jiyong Park82484c02018-04-23 21:41:26 +0900718 return &[]string{}
719 }).(*[]string)
720}
721
Paul Duffin749f98f2019-12-30 17:23:46 +0000722func (module *SdkLibrary) getApiDir() string {
723 return proptools.StringDefault(module.sdkLibraryProperties.Api_dir, "api")
724}
725
Jiyong Parkc678ad32018-04-10 13:07:10 +0900726// For a java_sdk_library module, create internal modules for stubs, docs,
727// runtime libs and xml file. If requested, the stubs and docs are created twice
728// once for public API level and once for system API level
Colin Crossf8b860a2019-04-16 14:43:28 -0700729func (module *SdkLibrary) CreateInternalModules(mctx android.LoadHookContext) {
Inseob Kim6e93ac92019-03-21 17:43:49 +0900730 if len(module.Library.Module.properties.Srcs) == 0 {
Inseob Kimc0907f12019-02-08 21:00:45 +0900731 mctx.PropertyErrorf("srcs", "java_sdk_library must specify srcs")
Jooyung Han58f26ab2019-12-18 15:34:32 +0900732 return
Inseob Kimc0907f12019-02-08 21:00:45 +0900733 }
734
Paul Duffin37e0b772019-12-30 17:20:10 +0000735 // If this builds against standard libraries (i.e. is not part of the core libraries)
736 // then assume it provides both system and test apis. Otherwise, assume it does not and
737 // also assume it does not contribute to the dist build.
738 sdkDep := decodeSdkDep(mctx, sdkContext(&module.Library))
739 hasSystemAndTestApis := sdkDep.hasStandardLibs()
740 module.sdkLibraryProperties.Has_system_and_test_apis = hasSystemAndTestApis
741 module.sdkLibraryProperties.No_dist = proptools.BoolPtr(!hasSystemAndTestApis)
742
Inseob Kim8098faa2019-03-18 10:19:51 +0900743 missing_current_api := false
744
Paul Duffind1b3a922020-01-22 11:57:20 +0000745 activeScopes := module.getActiveApiScopes()
746
Paul Duffin749f98f2019-12-30 17:23:46 +0000747 apiDir := module.getApiDir()
Paul Duffind1b3a922020-01-22 11:57:20 +0000748 for _, scope := range activeScopes {
Inseob Kim8098faa2019-03-18 10:19:51 +0900749 for _, api := range []string{"current.txt", "removed.txt"} {
Paul Duffind1b3a922020-01-22 11:57:20 +0000750 path := path.Join(mctx.ModuleDir(), apiDir, scope.apiFilePrefix+api)
Inseob Kim8098faa2019-03-18 10:19:51 +0900751 p := android.ExistentPathForSource(mctx, path)
752 if !p.Valid() {
753 mctx.ModuleErrorf("Current api file %#v doesn't exist", path)
754 missing_current_api = true
755 }
756 }
757 }
758
759 if missing_current_api {
760 script := "build/soong/scripts/gen-java-current-api-files.sh"
761 p := android.ExistentPathForSource(mctx, script)
762
763 if !p.Valid() {
764 panic(fmt.Sprintf("script file %s doesn't exist", script))
765 }
766
767 mctx.ModuleErrorf("One or more current api files are missing. "+
768 "You can update them by:\n"+
Paul Duffin37e0b772019-12-30 17:20:10 +0000769 "%s %q %s && m update-api",
Paul Duffind1b3a922020-01-22 11:57:20 +0000770 script, filepath.Join(mctx.ModuleDir(), apiDir),
771 strings.Join(activeScopes.Strings(func(s *apiScope) string { return s.apiFilePrefix }), " "))
Inseob Kim8098faa2019-03-18 10:19:51 +0900772 return
773 }
774
Paul Duffind1b3a922020-01-22 11:57:20 +0000775 for _, scope := range activeScopes {
776 module.createStubsLibrary(mctx, scope)
777 module.createStubsSources(mctx, scope)
Inseob Kimc0907f12019-02-08 21:00:45 +0900778 }
779
Paul Duffin43db9be2019-12-30 17:35:49 +0000780 if !proptools.Bool(module.sdkLibraryProperties.Api_only) {
781 // for runtime
782 module.createXmlFile(mctx)
783
784 // record java_sdk_library modules so that they are exported to make
785 javaSdkLibraries := javaSdkLibraries(mctx.Config())
786 javaSdkLibrariesLock.Lock()
787 defer javaSdkLibrariesLock.Unlock()
788 *javaSdkLibraries = append(*javaSdkLibraries, module.BaseModuleName())
789 }
Inseob Kimc0907f12019-02-08 21:00:45 +0900790}
791
792func (module *SdkLibrary) InitSdkLibraryProperties() {
Sundong Ahn054b19a2018-10-19 13:46:09 +0900793 module.AddProperties(
794 &module.sdkLibraryProperties,
795 &module.Library.Module.properties,
796 &module.Library.Module.dexpreoptProperties,
797 &module.Library.Module.deviceProperties,
798 &module.Library.Module.protoProperties,
799 )
800
801 module.Library.Module.properties.Installable = proptools.BoolPtr(true)
802 module.Library.Module.deviceProperties.IsSDKLibrary = true
Inseob Kimc0907f12019-02-08 21:00:45 +0900803}
Sundong Ahn054b19a2018-10-19 13:46:09 +0900804
Jaewoong Jung4f158ee2019-07-11 10:05:35 -0700805// java_sdk_library is a special Java library that provides optional platform APIs to apps.
806// In practice, it can be viewed as a combination of several modules: 1) stubs library that clients
807// are linked against to, 2) droiddoc module that internally generates API stubs source files,
808// 3) the real runtime shared library that implements the APIs, and 4) XML file for adding
809// the runtime lib to the classpath at runtime if requested via <uses-library>.
Inseob Kimc0907f12019-02-08 21:00:45 +0900810func SdkLibraryFactory() android.Module {
811 module := &SdkLibrary{}
812 module.InitSdkLibraryProperties()
Jooyung Han58f26ab2019-12-18 15:34:32 +0900813 android.InitApexModule(module)
Sundong Ahn054b19a2018-10-19 13:46:09 +0900814 InitJavaModule(module, android.HostAndDeviceSupported)
Colin Crossf8b860a2019-04-16 14:43:28 -0700815 android.AddLoadHook(module, func(ctx android.LoadHookContext) { module.CreateInternalModules(ctx) })
Jiyong Parkc678ad32018-04-10 13:07:10 +0900816 return module
817}
Colin Cross79c7c262019-04-17 11:11:46 -0700818
819//
820// SDK library prebuilts
821//
822
Paul Duffin56d44902020-01-31 13:36:25 +0000823// Properties associated with each api scope.
824type sdkLibraryScopeProperties struct {
Colin Cross79c7c262019-04-17 11:11:46 -0700825 Jars []string `android:"path"`
826
827 Sdk_version *string
828
Colin Cross79c7c262019-04-17 11:11:46 -0700829 // List of shared java libs that this module has dependencies to
830 Libs []string
Paul Duffin3d1248c2020-04-09 00:10:17 +0100831
832 // The stub sources.
833 Stub_srcs []string `android:"path"`
Colin Cross79c7c262019-04-17 11:11:46 -0700834}
835
Paul Duffin56d44902020-01-31 13:36:25 +0000836type sdkLibraryImportProperties struct {
Paul Duffinfcfd7912020-01-31 17:54:30 +0000837 // List of shared java libs, common to all scopes, that this module has
838 // dependencies to
839 Libs []string
Paul Duffin56d44902020-01-31 13:36:25 +0000840}
841
Colin Cross79c7c262019-04-17 11:11:46 -0700842type sdkLibraryImport struct {
843 android.ModuleBase
844 android.DefaultableModuleBase
845 prebuilt android.Prebuilt
Paul Duffindd46f712020-02-10 13:37:10 +0000846 android.ApexModuleBase
847 android.SdkBase
Colin Cross79c7c262019-04-17 11:11:46 -0700848
849 properties sdkLibraryImportProperties
850
Paul Duffin46a26a82020-04-07 19:27:04 +0100851 // Map from api scope to the scope specific property structure.
852 scopeProperties map[*apiScope]*sdkLibraryScopeProperties
853
Paul Duffin56d44902020-01-31 13:36:25 +0000854 commonToSdkLibraryAndImport
Colin Cross79c7c262019-04-17 11:11:46 -0700855}
856
857var _ SdkLibraryDependency = (*sdkLibraryImport)(nil)
858
Paul Duffin46a26a82020-04-07 19:27:04 +0100859// The type of a structure that contains a field of type sdkLibraryScopeProperties
860// for each apiscope in allApiScopes, e.g. something like:
861// struct {
862// Public sdkLibraryScopeProperties
863// System sdkLibraryScopeProperties
864// ...
865// }
866var allScopeStructType = createAllScopePropertiesStructType()
867
868// Dynamically create a structure type for each apiscope in allApiScopes.
869func createAllScopePropertiesStructType() reflect.Type {
870 var fields []reflect.StructField
871 for _, apiScope := range allApiScopes {
872 field := reflect.StructField{
873 Name: apiScope.fieldName,
874 Type: reflect.TypeOf(sdkLibraryScopeProperties{}),
875 }
876 fields = append(fields, field)
877 }
878
879 return reflect.StructOf(fields)
880}
881
882// Create an instance of the scope specific structure type and return a map
883// from apiscope to a pointer to each scope specific field.
884func createPropertiesInstance() (interface{}, map[*apiScope]*sdkLibraryScopeProperties) {
885 allScopePropertiesPtr := reflect.New(allScopeStructType)
886 allScopePropertiesStruct := allScopePropertiesPtr.Elem()
887 scopeProperties := make(map[*apiScope]*sdkLibraryScopeProperties)
888
889 for _, apiScope := range allApiScopes {
890 field := allScopePropertiesStruct.FieldByName(apiScope.fieldName)
891 scopeProperties[apiScope] = field.Addr().Interface().(*sdkLibraryScopeProperties)
892 }
893
894 return allScopePropertiesPtr.Interface(), scopeProperties
895}
896
Jaewoong Jung4f158ee2019-07-11 10:05:35 -0700897// java_sdk_library_import imports a prebuilt java_sdk_library.
Colin Cross79c7c262019-04-17 11:11:46 -0700898func sdkLibraryImportFactory() android.Module {
899 module := &sdkLibraryImport{}
900
Paul Duffin46a26a82020-04-07 19:27:04 +0100901 allScopeProperties, scopeToProperties := createPropertiesInstance()
902 module.scopeProperties = scopeToProperties
903 module.AddProperties(&module.properties, allScopeProperties)
Colin Cross79c7c262019-04-17 11:11:46 -0700904
Paul Duffin0bdcb272020-02-06 15:24:57 +0000905 android.InitPrebuiltModule(module, &[]string{""})
Paul Duffindd46f712020-02-10 13:37:10 +0000906 android.InitApexModule(module)
907 android.InitSdkAwareModule(module)
Colin Cross79c7c262019-04-17 11:11:46 -0700908 InitJavaModule(module, android.HostAndDeviceSupported)
909
910 android.AddLoadHook(module, func(mctx android.LoadHookContext) { module.createInternalModules(mctx) })
911 return module
912}
913
914func (module *sdkLibraryImport) Prebuilt() *android.Prebuilt {
915 return &module.prebuilt
916}
917
918func (module *sdkLibraryImport) Name() string {
919 return module.prebuilt.Name(module.ModuleBase.Name())
920}
921
922func (module *sdkLibraryImport) createInternalModules(mctx android.LoadHookContext) {
Colin Cross79c7c262019-04-17 11:11:46 -0700923
Paul Duffin50061512020-01-21 16:31:05 +0000924 // If the build is configured to use prebuilts then force this to be preferred.
925 if mctx.Config().UnbundledBuildUsePrebuiltSdks() {
926 module.prebuilt.ForcePrefer()
927 }
928
Paul Duffin46a26a82020-04-07 19:27:04 +0100929 for apiScope, scopeProperties := range module.scopeProperties {
Paul Duffin56d44902020-01-31 13:36:25 +0000930 if len(scopeProperties.Jars) == 0 {
931 continue
932 }
933
Paul Duffinbbb546b2020-04-09 00:07:11 +0100934 module.createJavaImportForStubs(mctx, apiScope, scopeProperties)
Paul Duffin3d1248c2020-04-09 00:10:17 +0100935
936 module.createPrebuiltStubsSources(mctx, apiScope, scopeProperties)
Paul Duffin56d44902020-01-31 13:36:25 +0000937 }
Colin Cross79c7c262019-04-17 11:11:46 -0700938
939 javaSdkLibraries := javaSdkLibraries(mctx.Config())
940 javaSdkLibrariesLock.Lock()
941 defer javaSdkLibrariesLock.Unlock()
942 *javaSdkLibraries = append(*javaSdkLibraries, module.BaseModuleName())
943}
944
Paul Duffinbbb546b2020-04-09 00:07:11 +0100945func (module *sdkLibraryImport) createJavaImportForStubs(mctx android.LoadHookContext, apiScope *apiScope, scopeProperties *sdkLibraryScopeProperties) {
946 // Creates a java import for the jar with ".stubs" suffix
947 props := struct {
948 Name *string
949 Soc_specific *bool
950 Device_specific *bool
951 Product_specific *bool
952 System_ext_specific *bool
953 Sdk_version *string
954 Libs []string
955 Jars []string
956 Prefer *bool
957 }{}
958 props.Name = proptools.StringPtr(apiScope.stubsModuleName(module.BaseModuleName()))
959 props.Sdk_version = scopeProperties.Sdk_version
960 // Prepend any of the libs from the legacy public properties to the libs for each of the
961 // scopes to avoid having to duplicate them in each scope.
962 props.Libs = append(module.properties.Libs, scopeProperties.Libs...)
963 props.Jars = scopeProperties.Jars
964 if module.SocSpecific() {
965 props.Soc_specific = proptools.BoolPtr(true)
966 } else if module.DeviceSpecific() {
967 props.Device_specific = proptools.BoolPtr(true)
968 } else if module.ProductSpecific() {
969 props.Product_specific = proptools.BoolPtr(true)
970 } else if module.SystemExtSpecific() {
971 props.System_ext_specific = proptools.BoolPtr(true)
972 }
973 // If the build should use prebuilt sdks then set prefer to true on the stubs library.
974 // That will cause the prebuilt version of the stubs to override the source version.
975 if mctx.Config().UnbundledBuildUsePrebuiltSdks() {
976 props.Prefer = proptools.BoolPtr(true)
977 }
978 mctx.CreateModule(ImportFactory, &props)
979}
980
Paul Duffin3d1248c2020-04-09 00:10:17 +0100981func (module *sdkLibraryImport) createPrebuiltStubsSources(mctx android.LoadHookContext, apiScope *apiScope, scopeProperties *sdkLibraryScopeProperties) {
982 props := struct {
983 Name *string
984 Srcs []string
985 }{}
986 props.Name = proptools.StringPtr(apiScope.docsModuleName(module.BaseModuleName()))
987 props.Srcs = scopeProperties.Stub_srcs
988 mctx.CreateModule(PrebuiltStubsSourcesFactory, &props)
989}
990
Colin Cross79c7c262019-04-17 11:11:46 -0700991func (module *sdkLibraryImport) DepsMutator(ctx android.BottomUpMutatorContext) {
Paul Duffin46a26a82020-04-07 19:27:04 +0100992 for apiScope, scopeProperties := range module.scopeProperties {
Paul Duffin56d44902020-01-31 13:36:25 +0000993 if len(scopeProperties.Jars) == 0 {
994 continue
995 }
996
997 // Add dependencies to the prebuilt stubs library
998 ctx.AddVariationDependencies(nil, apiScope.stubsTag, apiScope.stubsModuleName(module.BaseModuleName()))
999 }
Colin Cross79c7c262019-04-17 11:11:46 -07001000}
1001
1002func (module *sdkLibraryImport) GenerateAndroidBuildActions(ctx android.ModuleContext) {
1003 // Record the paths to the prebuilt stubs library.
1004 ctx.VisitDirectDeps(func(to android.Module) {
1005 tag := ctx.OtherModuleDependencyTag(to)
1006
Paul Duffin56d44902020-01-31 13:36:25 +00001007 if lib, ok := to.(Dependency); ok {
1008 if scopeTag, ok := tag.(scopeDependencyTag); ok {
1009 apiScope := scopeTag.apiScope
1010 scopePaths := module.getScopePaths(apiScope)
1011 scopePaths.stubsHeaderPath = lib.HeaderJars()
1012 }
Colin Cross79c7c262019-04-17 11:11:46 -07001013 }
1014 })
1015}
1016
Paul Duffin56d44902020-01-31 13:36:25 +00001017func (module *sdkLibraryImport) sdkJars(
1018 ctx android.BaseModuleContext,
1019 sdkVersion sdkSpec) android.Paths {
1020
Paul Duffin50061512020-01-21 16:31:05 +00001021 // If a specific numeric version has been requested then use prebuilt versions of the sdk.
1022 if sdkVersion.version.isNumbered() {
1023 return PrebuiltJars(ctx, module.BaseModuleName(), sdkVersion)
1024 }
1025
Paul Duffin56d44902020-01-31 13:36:25 +00001026 var apiScope *apiScope
1027 switch sdkVersion.kind {
1028 case sdkSystem:
1029 apiScope = apiScopeSystem
1030 case sdkTest:
1031 apiScope = apiScopeTest
1032 default:
1033 apiScope = apiScopePublic
1034 }
1035
1036 paths := module.getScopePaths(apiScope)
1037 return paths.stubsHeaderPath
1038}
1039
Colin Cross79c7c262019-04-17 11:11:46 -07001040// to satisfy SdkLibraryDependency interface
Jiyong Park6a927c42020-01-21 02:03:43 +09001041func (module *sdkLibraryImport) SdkHeaderJars(ctx android.BaseModuleContext, sdkVersion sdkSpec) android.Paths {
Colin Cross79c7c262019-04-17 11:11:46 -07001042 // This module is just a wrapper for the prebuilt stubs.
Paul Duffin56d44902020-01-31 13:36:25 +00001043 return module.sdkJars(ctx, sdkVersion)
Colin Cross79c7c262019-04-17 11:11:46 -07001044}
1045
1046// to satisfy SdkLibraryDependency interface
Jiyong Park6a927c42020-01-21 02:03:43 +09001047func (module *sdkLibraryImport) SdkImplementationJars(ctx android.BaseModuleContext, sdkVersion sdkSpec) android.Paths {
Colin Cross79c7c262019-04-17 11:11:46 -07001048 // This module is just a wrapper for the stubs.
Paul Duffin56d44902020-01-31 13:36:25 +00001049 return module.sdkJars(ctx, sdkVersion)
Colin Cross79c7c262019-04-17 11:11:46 -07001050}
Jiyong Parke3833882020-02-17 17:28:10 +09001051
1052//
1053// java_sdk_library_xml
1054//
1055type sdkLibraryXml struct {
1056 android.ModuleBase
1057 android.DefaultableModuleBase
1058 android.ApexModuleBase
1059
1060 properties sdkLibraryXmlProperties
1061
1062 outputFilePath android.OutputPath
1063 installDirPath android.InstallPath
1064}
1065
1066type sdkLibraryXmlProperties struct {
1067 // canonical name of the lib
1068 Lib_name *string
1069}
1070
1071// java_sdk_library_xml builds the permission xml file for a java_sdk_library.
1072// Not to be used directly by users. java_sdk_library internally uses this.
1073func sdkLibraryXmlFactory() android.Module {
1074 module := &sdkLibraryXml{}
1075
1076 module.AddProperties(&module.properties)
1077
1078 android.InitApexModule(module)
1079 android.InitAndroidArchModule(module, android.DeviceSupported, android.MultilibCommon)
1080
1081 return module
1082}
1083
1084// from android.PrebuiltEtcModule
1085func (module *sdkLibraryXml) SubDir() string {
1086 return "permissions"
1087}
1088
1089// from android.PrebuiltEtcModule
1090func (module *sdkLibraryXml) OutputFile() android.OutputPath {
1091 return module.outputFilePath
1092}
1093
1094// from android.ApexModule
1095func (module *sdkLibraryXml) AvailableFor(what string) bool {
1096 return true
1097}
1098
1099func (module *sdkLibraryXml) DepsMutator(ctx android.BottomUpMutatorContext) {
1100 // do nothing
1101}
1102
1103// File path to the runtime implementation library
1104func (module *sdkLibraryXml) implPath() string {
1105 implName := proptools.String(module.properties.Lib_name)
1106 if apexName := module.ApexName(); apexName != "" {
1107 // TODO(b/146468504): ApexName() is only a soong module name, not apex name.
1108 // In most cases, this works fine. But when apex_name is set or override_apex is used
1109 // this can be wrong.
1110 return fmt.Sprintf("/apex/%s/javalib/%s.jar", apexName, implName)
1111 }
1112 partition := "system"
1113 if module.SocSpecific() {
1114 partition = "vendor"
1115 } else if module.DeviceSpecific() {
1116 partition = "odm"
1117 } else if module.ProductSpecific() {
1118 partition = "product"
1119 } else if module.SystemExtSpecific() {
1120 partition = "system_ext"
1121 }
1122 return "/" + partition + "/framework/" + implName + ".jar"
1123}
1124
1125func (module *sdkLibraryXml) GenerateAndroidBuildActions(ctx android.ModuleContext) {
1126 libName := proptools.String(module.properties.Lib_name)
1127 xmlContent := fmt.Sprintf(permissionsTemplate, libName, module.implPath())
1128
1129 module.outputFilePath = android.PathForModuleOut(ctx, libName+".xml").OutputPath
1130 rule := android.NewRuleBuilder()
1131 rule.Command().
1132 Text("/bin/bash -c \"echo -e '" + xmlContent + "'\" > ").
1133 Output(module.outputFilePath)
1134
1135 rule.Build(pctx, ctx, "java_sdk_xml", "Permission XML")
1136
1137 module.installDirPath = android.PathForModuleInstall(ctx, "etc", module.SubDir())
1138}
1139
1140func (module *sdkLibraryXml) AndroidMkEntries() []android.AndroidMkEntries {
1141 if !module.IsForPlatform() {
1142 return []android.AndroidMkEntries{android.AndroidMkEntries{
1143 Disabled: true,
1144 }}
1145 }
1146
1147 return []android.AndroidMkEntries{android.AndroidMkEntries{
1148 Class: "ETC",
1149 OutputFile: android.OptionalPathForPath(module.outputFilePath),
1150 ExtraEntries: []android.AndroidMkExtraEntriesFunc{
1151 func(entries *android.AndroidMkEntries) {
1152 entries.SetString("LOCAL_MODULE_TAGS", "optional")
1153 entries.SetString("LOCAL_MODULE_PATH", module.installDirPath.ToMakePath().String())
1154 entries.SetString("LOCAL_INSTALLED_MODULE_STEM", module.outputFilePath.Base())
1155 },
1156 },
1157 }}
1158}
Paul Duffindd46f712020-02-10 13:37:10 +00001159
1160type sdkLibrarySdkMemberType struct {
1161 android.SdkMemberTypeBase
1162}
1163
1164func (s *sdkLibrarySdkMemberType) AddDependencies(mctx android.BottomUpMutatorContext, dependencyTag blueprint.DependencyTag, names []string) {
1165 mctx.AddVariationDependencies(nil, dependencyTag, names...)
1166}
1167
1168func (s *sdkLibrarySdkMemberType) IsInstance(module android.Module) bool {
1169 _, ok := module.(*SdkLibrary)
1170 return ok
1171}
1172
1173func (s *sdkLibrarySdkMemberType) AddPrebuiltModule(ctx android.SdkMemberContext, member android.SdkMember) android.BpModule {
1174 return ctx.SnapshotBuilder().AddPrebuiltModule(member, "java_sdk_library_import")
1175}
1176
1177func (s *sdkLibrarySdkMemberType) CreateVariantPropertiesStruct() android.SdkMemberProperties {
1178 return &sdkLibrarySdkMemberProperties{}
1179}
1180
1181type sdkLibrarySdkMemberProperties struct {
1182 android.SdkMemberPropertiesBase
1183
1184 // Scope to per scope properties.
1185 Scopes map[*apiScope]scopeProperties
1186
1187 // Additional libraries that the exported stubs libraries depend upon.
1188 Libs []string
Paul Duffin3d1248c2020-04-09 00:10:17 +01001189
1190 // The Java stubs source files.
1191 Stub_srcs []string
Paul Duffindd46f712020-02-10 13:37:10 +00001192}
1193
1194type scopeProperties struct {
Paul Duffin3d1248c2020-04-09 00:10:17 +01001195 Jars android.Paths
1196 StubsSrcJar 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 Duffindd46f712020-02-10 13:37:10 +00001212 s.Scopes[apiScope] = properties
1213 }
1214 }
1215
1216 s.Libs = sdk.properties.Libs
1217}
1218
1219func (s *sdkLibrarySdkMemberProperties) AddToPropertySet(ctx android.SdkMemberContext, propertySet android.BpPropertySet) {
1220 for _, apiScope := range allApiScopes {
1221 if properties, ok := s.Scopes[apiScope]; ok {
1222 scopeSet := propertySet.AddPropertySet(apiScope.name)
1223
Paul Duffin3d1248c2020-04-09 00:10:17 +01001224 scopeDir := filepath.Join("sdk_library", s.OsPrefix(), apiScope.name)
1225
Paul Duffindd46f712020-02-10 13:37:10 +00001226 var jars []string
1227 for _, p := range properties.Jars {
Paul Duffin3d1248c2020-04-09 00:10:17 +01001228 dest := filepath.Join(scopeDir, ctx.Name()+"-stubs.jar")
Paul Duffindd46f712020-02-10 13:37:10 +00001229 ctx.SnapshotBuilder().CopyToSnapshot(p, dest)
1230 jars = append(jars, dest)
1231 }
1232 scopeSet.AddProperty("jars", jars)
1233
Paul Duffin3d1248c2020-04-09 00:10:17 +01001234 // Merge the stubs source jar into the snapshot zip so that when it is unpacked
1235 // the source files are also unpacked.
1236 snapshotRelativeDir := filepath.Join(scopeDir, ctx.Name()+"_stub_sources")
1237 ctx.SnapshotBuilder().UnzipToSnapshot(properties.StubsSrcJar, snapshotRelativeDir)
1238 scopeSet.AddProperty("stub_srcs", []string{snapshotRelativeDir})
1239
Paul Duffindd46f712020-02-10 13:37:10 +00001240 if properties.SdkVersion != "" {
1241 scopeSet.AddProperty("sdk_version", properties.SdkVersion)
1242 }
1243 }
1244 }
1245
1246 if len(s.Libs) > 0 {
1247 propertySet.AddPropertyWithTag("libs", s.Libs, ctx.SnapshotBuilder().SdkMemberReferencePropertyTag(false))
1248 }
1249}