blob: 6921114bf666283a248205cfee111fcb56fcdf1b [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 (
18 "android/soong/android"
Paul Duffind1b3a922020-01-22 11:57:20 +000019
Jiyong Parkc678ad32018-04-10 13:07:10 +090020 "fmt"
Jiyong Park82484c02018-04-23 21:41:26 +090021 "io"
Jiyong Parkc678ad32018-04-10 13:07:10 +090022 "path"
Sundong Ahn054b19a2018-10-19 13:46:09 +090023 "path/filepath"
Jiyong Park82484c02018-04-23 21:41:26 +090024 "sort"
Jiyong Parkc678ad32018-04-10 13:07:10 +090025 "strings"
Jiyong Park82484c02018-04-23 21:41:26 +090026 "sync"
Jiyong Parkc678ad32018-04-10 13:07:10 +090027
Paul Duffind1b3a922020-01-22 11:57:20 +000028 "github.com/google/blueprint"
Jiyong Parkc678ad32018-04-10 13:07:10 +090029 "github.com/google/blueprint/proptools"
30)
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
70 // The tag to use to depend on the stubs library module.
71 stubsTag scopeDependencyTag
72
73 // The tag to use to depend on the stubs
74 apiFileTag scopeDependencyTag
75
76 // The scope specific prefix to add to the api file base of "current.txt" or "removed.txt".
77 apiFilePrefix string
78
79 // The scope specific prefix to add to the sdk library module name to construct a scope specific
80 // module name.
81 moduleSuffix string
82
83 // The suffix to add to the make variable that references the location of the api file.
84 apiFileMakeVariableSuffix string
85
86 // 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
90}
91
92// Initialize a scope, creating and adding appropriate dependency tags
93func initApiScope(scope *apiScope) *apiScope {
94 //apiScope := &scope
95 scope.stubsTag = scopeDependencyTag{
96 name: scope.name + "-stubs",
97 apiScope: scope,
98 }
99 scope.apiFileTag = scopeDependencyTag{
100 name: scope.name + "-api",
101 apiScope: scope,
102 }
103 return scope
104}
105
106func (scope *apiScope) stubsModuleName(baseName string) string {
107 return baseName + sdkStubsLibrarySuffix + scope.moduleSuffix
108}
109
110func (scope *apiScope) docsModuleName(baseName string) string {
Paul Duffin91b883d2020-02-11 13:05:28 +0000111 return baseName + sdkStubsSourceSuffix + scope.moduleSuffix
Paul Duffind1b3a922020-01-22 11:57:20 +0000112}
113
114type apiScopes []*apiScope
115
116func (scopes apiScopes) Strings(accessor func(*apiScope) string) []string {
117 var list []string
118 for _, scope := range scopes {
119 list = append(list, accessor(scope))
120 }
121 return list
122}
123
Jiyong Parkc678ad32018-04-10 13:07:10 +0900124var (
Paul Duffind1b3a922020-01-22 11:57:20 +0000125 apiScopePublic = initApiScope(&apiScope{
126 name: "public",
127 sdkVersion: "current",
128 })
129 apiScopeSystem = initApiScope(&apiScope{
130 name: "system",
131 apiFilePrefix: "system-",
132 moduleSuffix: sdkSystemApiSuffix,
133 apiFileMakeVariableSuffix: "_SYSTEM",
134 sdkVersion: "system_current",
135 })
136 apiScopeTest = initApiScope(&apiScope{
137 name: "test",
138 apiFilePrefix: "test-",
139 moduleSuffix: sdkTestApiSuffix,
140 apiFileMakeVariableSuffix: "_TEST",
141 sdkVersion: "test_current",
142 })
143 allApiScopes = apiScopes{
144 apiScopePublic,
145 apiScopeSystem,
146 apiScopeTest,
147 }
Jiyong Parkc678ad32018-04-10 13:07:10 +0900148)
149
Jiyong Park82484c02018-04-23 21:41:26 +0900150var (
151 javaSdkLibrariesLock sync.Mutex
152)
153
Jiyong Parkc678ad32018-04-10 13:07:10 +0900154// TODO: these are big features that are currently missing
Jiyong Park1be96912018-05-28 18:02:19 +0900155// 1) disallowing linking to the runtime shared lib
156// 2) HTML generation
Jiyong Parkc678ad32018-04-10 13:07:10 +0900157
158func init() {
Paul Duffin43dc1cc2019-12-19 11:18:54 +0000159 RegisterSdkLibraryBuildComponents(android.InitRegistrationContext)
Jiyong Parkc678ad32018-04-10 13:07:10 +0900160
Jiyong Park82484c02018-04-23 21:41:26 +0900161 android.RegisterMakeVarsProvider(pctx, func(ctx android.MakeVarsContext) {
162 javaSdkLibraries := javaSdkLibraries(ctx.Config())
163 sort.Strings(*javaSdkLibraries)
164 ctx.Strict("JAVA_SDK_LIBRARIES", strings.Join(*javaSdkLibraries, " "))
165 })
Jiyong Parkc678ad32018-04-10 13:07:10 +0900166}
167
Paul Duffin43dc1cc2019-12-19 11:18:54 +0000168func RegisterSdkLibraryBuildComponents(ctx android.RegistrationContext) {
169 ctx.RegisterModuleType("java_sdk_library", SdkLibraryFactory)
170 ctx.RegisterModuleType("java_sdk_library_import", sdkLibraryImportFactory)
171}
172
Jiyong Parkc678ad32018-04-10 13:07:10 +0900173type sdkLibraryProperties struct {
Sundong Ahnf043cf62018-06-25 16:04:37 +0900174 // List of Java libraries that will be in the classpath when building stubs
175 Stub_only_libs []string `android:"arch_variant"`
176
Paul Duffin7a586d32019-12-30 17:09:34 +0000177 // list of package names that will be documented and publicized as API.
178 // This allows the API to be restricted to a subset of the source files provided.
179 // If this is unspecified then all the source files will be treated as being part
180 // of the API.
Jiyong Parkc678ad32018-04-10 13:07:10 +0900181 Api_packages []string
182
Jiyong Park5a2c9d72018-05-01 22:25:41 +0900183 // list of package names that must be hidden from the API
184 Hidden_api_packages []string
185
Paul Duffin749f98f2019-12-30 17:23:46 +0000186 // the relative path to the directory containing the api specification files.
187 // Defaults to "api".
188 Api_dir *string
189
Paul Duffin43db9be2019-12-30 17:35:49 +0000190 // If set to true there is no runtime library.
191 Api_only *bool
192
Paul Duffin11512472019-02-11 15:55:17 +0000193 // local files that are used within user customized droiddoc options.
194 Droiddoc_option_files []string
195
196 // additional droiddoc options
197 // Available variables for substitution:
198 //
199 // $(location <label>): the path to the droiddoc_option_files with name <label>
Sundong Ahndd567f92018-07-31 17:19:11 +0900200 Droiddoc_options []string
201
Sundong Ahn054b19a2018-10-19 13:46:09 +0900202 // a list of top-level directories containing files to merge qualifier annotations
203 // (i.e. those intended to be included in the stubs written) from.
204 Merge_annotations_dirs []string
205
206 // a list of top-level directories containing Java stub files to merge show/hide annotations from.
207 Merge_inclusion_annotations_dirs []string
208
209 // If set to true, the path of dist files is apistubs/core. Defaults to false.
210 Core_lib *bool
211
Sundong Ahn80a87b32019-05-13 15:02:50 +0900212 // don't create dist rules.
213 No_dist *bool `blueprint:"mutated"`
214
Paul Duffin37e0b772019-12-30 17:20:10 +0000215 // indicates whether system and test apis should be managed.
216 Has_system_and_test_apis bool `blueprint:"mutated"`
217
Jiyong Parkc678ad32018-04-10 13:07:10 +0900218 // TODO: determines whether to create HTML doc or not
219 //Html_doc *bool
220}
221
Paul Duffind1b3a922020-01-22 11:57:20 +0000222type scopePaths struct {
223 stubsHeaderPath android.Paths
224 stubsImplPath android.Paths
225 apiFilePath android.Path
226}
227
Paul Duffin56d44902020-01-31 13:36:25 +0000228// Common code between sdk library and sdk library import
229type commonToSdkLibraryAndImport struct {
230 scopePaths map[*apiScope]*scopePaths
231}
232
233func (c *commonToSdkLibraryAndImport) getScopePaths(scope *apiScope) *scopePaths {
234 if c.scopePaths == nil {
235 c.scopePaths = make(map[*apiScope]*scopePaths)
236 }
237 paths := c.scopePaths[scope]
238 if paths == nil {
239 paths = &scopePaths{}
240 c.scopePaths[scope] = paths
241 }
242
243 return paths
244}
245
Inseob Kimc0907f12019-02-08 21:00:45 +0900246type SdkLibrary struct {
Sundong Ahn054b19a2018-10-19 13:46:09 +0900247 Library
Jiyong Parkc678ad32018-04-10 13:07:10 +0900248
Sundong Ahn054b19a2018-10-19 13:46:09 +0900249 sdkLibraryProperties sdkLibraryProperties
Jiyong Parkc678ad32018-04-10 13:07:10 +0900250
Paul Duffin56d44902020-01-31 13:36:25 +0000251 commonToSdkLibraryAndImport
Jiyong Parkc678ad32018-04-10 13:07:10 +0900252}
253
Inseob Kimc0907f12019-02-08 21:00:45 +0900254var _ Dependency = (*SdkLibrary)(nil)
255var _ SdkLibraryDependency = (*SdkLibrary)(nil)
Colin Cross897d2ed2019-02-11 14:03:51 -0800256
Paul Duffind1b3a922020-01-22 11:57:20 +0000257func (module *SdkLibrary) getActiveApiScopes() apiScopes {
258 if module.sdkLibraryProperties.Has_system_and_test_apis {
259 return allApiScopes
260 } else {
261 return apiScopes{apiScopePublic}
262 }
263}
264
Paul Duffine74ac732020-02-06 13:51:46 +0000265var xmlPermissionsFileTag = dependencyTag{name: "xml-permissions-file"}
266
Jiyong Parke3833882020-02-17 17:28:10 +0900267func IsXmlPermissionsFileDepTag(depTag blueprint.DependencyTag) bool {
268 if dt, ok := depTag.(dependencyTag); ok {
269 return dt == xmlPermissionsFileTag
270 }
271 return false
272}
273
Inseob Kimc0907f12019-02-08 21:00:45 +0900274func (module *SdkLibrary) DepsMutator(ctx android.BottomUpMutatorContext) {
Paul Duffind1b3a922020-01-22 11:57:20 +0000275 for _, apiScope := range module.getActiveApiScopes() {
276 // Add dependencies to the stubs library
Paul Duffin50061512020-01-21 16:31:05 +0000277 ctx.AddVariationDependencies(nil, apiScope.stubsTag, module.stubsName(apiScope))
Paul Duffind1b3a922020-01-22 11:57:20 +0000278
Paul Duffin50061512020-01-21 16:31:05 +0000279 // And the api file
Paul Duffind1b3a922020-01-22 11:57:20 +0000280 ctx.AddVariationDependencies(nil, apiScope.apiFileTag, module.docsName(apiScope))
Sundong Ahn054b19a2018-10-19 13:46:09 +0900281 }
282
Paul Duffine74ac732020-02-06 13:51:46 +0000283 if !proptools.Bool(module.sdkLibraryProperties.Api_only) {
284 // Add dependency to the rule for generating the xml permissions file
Jiyong Parke3833882020-02-17 17:28:10 +0900285 ctx.AddDependency(module, xmlPermissionsFileTag, module.xmlFileName())
Paul Duffine74ac732020-02-06 13:51:46 +0000286 }
287
Sundong Ahn054b19a2018-10-19 13:46:09 +0900288 module.Library.deps(ctx)
Jiyong Parkc678ad32018-04-10 13:07:10 +0900289}
290
Inseob Kimc0907f12019-02-08 21:00:45 +0900291func (module *SdkLibrary) GenerateAndroidBuildActions(ctx android.ModuleContext) {
Paul Duffin43db9be2019-12-30 17:35:49 +0000292 // Don't build an implementation library if this is api only.
293 if !proptools.Bool(module.sdkLibraryProperties.Api_only) {
294 module.Library.GenerateAndroidBuildActions(ctx)
295 }
Sundong Ahn054b19a2018-10-19 13:46:09 +0900296
Sundong Ahn57368eb2018-07-06 11:20:23 +0900297 // Record the paths to the header jars of the library (stubs and impl).
Paul Duffind1b3a922020-01-22 11:57:20 +0000298 // When this java_sdk_library is depended upon from others via "libs" property,
Jiyong Parkc678ad32018-04-10 13:07:10 +0900299 // the recorded paths will be returned depending on the link type of the caller.
300 ctx.VisitDirectDeps(func(to android.Module) {
301 otherName := ctx.OtherModuleName(to)
302 tag := ctx.OtherModuleDependencyTag(to)
303
Sundong Ahn57368eb2018-07-06 11:20:23 +0900304 if lib, ok := to.(Dependency); ok {
Paul Duffind1b3a922020-01-22 11:57:20 +0000305 if scopeTag, ok := tag.(scopeDependencyTag); ok {
306 apiScope := scopeTag.apiScope
307 scopePaths := module.getScopePaths(apiScope)
308 scopePaths.stubsHeaderPath = lib.HeaderJars()
309 scopePaths.stubsImplPath = lib.ImplementationJars()
Jiyong Parkc678ad32018-04-10 13:07:10 +0900310 }
311 }
Sundong Ahn20e998b2018-07-24 11:19:26 +0900312 if doc, ok := to.(ApiFilePath); ok {
Paul Duffind1b3a922020-01-22 11:57:20 +0000313 if scopeTag, ok := tag.(scopeDependencyTag); ok {
314 apiScope := scopeTag.apiScope
315 scopePaths := module.getScopePaths(apiScope)
316 scopePaths.apiFilePath = doc.ApiFilePath()
317 } else {
Sundong Ahn20e998b2018-07-24 11:19:26 +0900318 ctx.ModuleErrorf("depends on module %q of unknown tag %q", otherName, tag)
319 }
320 }
Jiyong Parkc678ad32018-04-10 13:07:10 +0900321 })
322}
323
Jiyong Park0b0e1b92019-12-03 13:24:29 +0900324func (module *SdkLibrary) AndroidMkEntries() []android.AndroidMkEntries {
Paul Duffin43db9be2019-12-30 17:35:49 +0000325 if proptools.Bool(module.sdkLibraryProperties.Api_only) {
326 return nil
327 }
Jiyong Park0b0e1b92019-12-03 13:24:29 +0900328 entriesList := module.Library.AndroidMkEntries()
329 entries := &entriesList[0]
Jaewoong Jungb0c127c2019-08-29 14:56:03 -0700330 entries.Required = append(entries.Required, module.xmlFileName())
Sundong Ahn054b19a2018-10-19 13:46:09 +0900331
Jaewoong Jungb0c127c2019-08-29 14:56:03 -0700332 entries.ExtraFooters = []android.AndroidMkExtraFootersFunc{
333 func(w io.Writer, name, prefix, moduleDir string, entries *android.AndroidMkEntries) {
Jaewoong Jungb0c127c2019-08-29 14:56:03 -0700334 if !Bool(module.sdkLibraryProperties.No_dist) {
335 // Create a phony module that installs the impl library, for the case when this lib is
336 // in PRODUCT_PACKAGES.
337 owner := module.ModuleBase.Owner()
338 if owner == "" {
339 if Bool(module.sdkLibraryProperties.Core_lib) {
340 owner = "core"
341 } else {
342 owner = "android"
343 }
344 }
Paul Duffind1b3a922020-01-22 11:57:20 +0000345
346 // Create dist rules to install the stubs libs and api files to the dist dir
347 for _, apiScope := range module.getActiveApiScopes() {
348 if scopePaths, ok := module.scopePaths[apiScope]; ok {
349 if len(scopePaths.stubsHeaderPath) == 1 {
350 fmt.Fprintln(w, "$(call dist-for-goals,sdk win_sdk,"+
351 scopePaths.stubsImplPath.Strings()[0]+
352 ":"+path.Join("apistubs", owner, apiScope.name,
353 module.BaseModuleName()+".jar")+")")
354 }
355 if scopePaths.apiFilePath != nil {
356 fmt.Fprintln(w, "$(call dist-for-goals,sdk win_sdk,"+
357 scopePaths.apiFilePath.String()+
358 ":"+path.Join("apistubs", owner, apiScope.name, "api",
359 module.BaseModuleName()+".txt")+")")
360 }
361 }
Sundong Ahn80a87b32019-05-13 15:02:50 +0900362 }
Sundong Ahn4fd04bb2018-08-31 18:01:37 +0900363 }
Jaewoong Jungb0c127c2019-08-29 14:56:03 -0700364 },
Jiyong Park82484c02018-04-23 21:41:26 +0900365 }
Jiyong Park0b0e1b92019-12-03 13:24:29 +0900366 return entriesList
Jiyong Park82484c02018-04-23 21:41:26 +0900367}
368
Jiyong Parkc678ad32018-04-10 13:07:10 +0900369// Module name of the stubs library
Paul Duffind1b3a922020-01-22 11:57:20 +0000370func (module *SdkLibrary) stubsName(apiScope *apiScope) string {
371 return apiScope.stubsModuleName(module.BaseModuleName())
Jiyong Parkc678ad32018-04-10 13:07:10 +0900372}
373
374// Module name of the docs
Paul Duffind1b3a922020-01-22 11:57:20 +0000375func (module *SdkLibrary) docsName(apiScope *apiScope) string {
376 return apiScope.docsModuleName(module.BaseModuleName())
Jiyong Parkc678ad32018-04-10 13:07:10 +0900377}
378
379// Module name of the runtime implementation library
Inseob Kimc0907f12019-02-08 21:00:45 +0900380func (module *SdkLibrary) implName() string {
Sundong Ahn054b19a2018-10-19 13:46:09 +0900381 return module.BaseModuleName()
Jiyong Parkc678ad32018-04-10 13:07:10 +0900382}
383
Jiyong Parkc678ad32018-04-10 13:07:10 +0900384// Module name of the XML file for the lib
Inseob Kimc0907f12019-02-08 21:00:45 +0900385func (module *SdkLibrary) xmlFileName() string {
Jiyong Parkc678ad32018-04-10 13:07:10 +0900386 return module.BaseModuleName() + sdkXmlFileSuffix
387}
388
Paul Duffin12ceb462019-12-24 20:31:31 +0000389// Get the sdk version for use when compiling the stubs library.
Paul Duffind1b3a922020-01-22 11:57:20 +0000390func (module *SdkLibrary) sdkVersionForStubsLibrary(mctx android.LoadHookContext, apiScope *apiScope) string {
Paul Duffin12ceb462019-12-24 20:31:31 +0000391 sdkDep := decodeSdkDep(mctx, sdkContext(&module.Library))
392 if sdkDep.hasStandardLibs() {
393 // If building against a standard sdk then use the sdk version appropriate for the scope.
Paul Duffind1b3a922020-01-22 11:57:20 +0000394 return apiScope.sdkVersion
Paul Duffin12ceb462019-12-24 20:31:31 +0000395 } else {
396 // Otherwise, use no system module.
397 return "none"
398 }
399}
400
Jiyong Parkc678ad32018-04-10 13:07:10 +0900401// $(INTERNAL_PLATFORM_<apiTagName>_API_FILE) points to the generated
402// api file for the current source
403// TODO: remove this when apicheck is done in soong
Paul Duffind1b3a922020-01-22 11:57:20 +0000404func (module *SdkLibrary) apiTagName(apiScope *apiScope) string {
405 return strings.Replace(strings.ToUpper(module.BaseModuleName()), ".", "_", -1) + apiScope.apiFileMakeVariableSuffix
Jiyong Parkc678ad32018-04-10 13:07:10 +0900406}
407
Paul Duffind1b3a922020-01-22 11:57:20 +0000408func (module *SdkLibrary) latestApiFilegroupName(apiScope *apiScope) string {
409 return ":" + module.BaseModuleName() + ".api." + apiScope.name + ".latest"
Jiyong Park58c518b2018-05-12 22:29:12 +0900410}
Jiyong Parkc678ad32018-04-10 13:07:10 +0900411
Paul Duffind1b3a922020-01-22 11:57:20 +0000412func (module *SdkLibrary) latestRemovedApiFilegroupName(apiScope *apiScope) string {
413 return ":" + module.BaseModuleName() + "-removed.api." + apiScope.name + ".latest"
Jiyong Parkc678ad32018-04-10 13:07:10 +0900414}
415
416// Creates a static java library that has API stubs
Paul Duffind1b3a922020-01-22 11:57:20 +0000417func (module *SdkLibrary) createStubsLibrary(mctx android.LoadHookContext, apiScope *apiScope) {
Jiyong Parkc678ad32018-04-10 13:07:10 +0900418 props := struct {
Sundong Ahn0d7dff42019-12-04 12:53:44 +0900419 Name *string
420 Srcs []string
Paul Duffin367ab912019-12-23 19:40:36 +0000421 Installable *bool
Sundong Ahn0d7dff42019-12-04 12:53:44 +0900422 Sdk_version *string
Paul Duffin12ceb462019-12-24 20:31:31 +0000423 System_modules *string
Paul Duffinab8da5d2020-02-07 16:12:04 +0000424 Patch_module *string
Sundong Ahn0d7dff42019-12-04 12:53:44 +0900425 Libs []string
426 Soc_specific *bool
427 Device_specific *bool
428 Product_specific *bool
429 System_ext_specific *bool
430 Compile_dex *bool
Sundong Ahn0d7dff42019-12-04 12:53:44 +0900431 Java_version *string
432 Product_variables struct {
Jiyong Park82484c02018-04-23 21:41:26 +0900433 Pdk struct {
434 Enabled *bool
435 }
Jiyong Parkc678ad32018-04-10 13:07:10 +0900436 }
Sundong Ahn054b19a2018-10-19 13:46:09 +0900437 Openjdk9 struct {
438 Srcs []string
439 Javacflags []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 }
469
Colin Cross84dfc3d2019-09-25 11:33:01 -0700470 mctx.CreateModule(LibraryFactory, &props)
Jiyong Parkc678ad32018-04-10 13:07:10 +0900471}
472
473// Creates a droiddoc module that creates stubs source files from the given full source
474// files
Paul Duffind1b3a922020-01-22 11:57:20 +0000475func (module *SdkLibrary) createStubsSources(mctx android.LoadHookContext, apiScope *apiScope) {
Jiyong Parkc678ad32018-04-10 13:07:10 +0900476 props := struct {
Sundong Ahn054b19a2018-10-19 13:46:09 +0900477 Name *string
478 Srcs []string
479 Installable *bool
Paul Duffin52d398a2019-06-11 12:31:14 +0100480 Sdk_version *string
Paul Duffin12ceb462019-12-24 20:31:31 +0000481 System_modules *string
Sundong Ahn054b19a2018-10-19 13:46:09 +0900482 Libs []string
Paul Duffin11512472019-02-11 15:55:17 +0000483 Arg_files []string
Sundong Ahn054b19a2018-10-19 13:46:09 +0900484 Args *string
485 Api_tag_name *string
486 Api_filename *string
487 Removed_api_filename *string
Sundong Ahn054b19a2018-10-19 13:46:09 +0900488 Java_version *string
489 Merge_annotations_dirs []string
490 Merge_inclusion_annotations_dirs []string
491 Check_api struct {
Inseob Kim38449af2019-02-28 14:24:05 +0900492 Current ApiToCheck
493 Last_released ApiToCheck
494 Ignore_missing_latest_api *bool
Jiyong Park58c518b2018-05-12 22:29:12 +0900495 }
Sundong Ahn1b92c822018-05-29 11:35:17 +0900496 Aidl struct {
497 Include_dirs []string
498 Local_include_dirs []string
499 }
Jiyong Parkc678ad32018-04-10 13:07:10 +0900500 }{}
501
Paul Duffin250e6192019-06-07 10:44:37 +0100502 sdkDep := decodeSdkDep(mctx, sdkContext(&module.Library))
Paul Duffin12ceb462019-12-24 20:31:31 +0000503 // Use the platform API if standard libraries were requested, otherwise use
504 // no default libraries.
Paul Duffin52d398a2019-06-11 12:31:14 +0100505 sdkVersion := ""
506 if !sdkDep.hasStandardLibs() {
507 sdkVersion = "none"
508 }
Paul Duffin250e6192019-06-07 10:44:37 +0100509
Jiyong Parkdf130542018-04-27 16:29:21 +0900510 props.Name = proptools.StringPtr(module.docsName(apiScope))
Sundong Ahn054b19a2018-10-19 13:46:09 +0900511 props.Srcs = append(props.Srcs, module.Library.Module.properties.Srcs...)
Paul Duffin52d398a2019-06-11 12:31:14 +0100512 props.Sdk_version = proptools.StringPtr(sdkVersion)
Paul Duffin12ceb462019-12-24 20:31:31 +0000513 props.System_modules = module.Library.Module.deviceProperties.System_modules
Jiyong Parkc678ad32018-04-10 13:07:10 +0900514 props.Installable = proptools.BoolPtr(false)
Sundong Ahne6f0b052018-06-05 16:46:14 +0900515 // A droiddoc module has only one Libs property and doesn't distinguish between
516 // shared libs and static libs. So we need to add both of these libs to Libs property.
Sundong Ahn054b19a2018-10-19 13:46:09 +0900517 props.Libs = module.Library.Module.properties.Libs
518 props.Libs = append(props.Libs, module.Library.Module.properties.Static_libs...)
519 props.Aidl.Include_dirs = module.Library.Module.deviceProperties.Aidl.Include_dirs
520 props.Aidl.Local_include_dirs = module.Library.Module.deviceProperties.Aidl.Local_include_dirs
Sundong Ahn054b19a2018-10-19 13:46:09 +0900521 props.Java_version = module.Library.Module.properties.Java_version
Jiyong Parkc678ad32018-04-10 13:07:10 +0900522
Sundong Ahn054b19a2018-10-19 13:46:09 +0900523 props.Merge_annotations_dirs = module.sdkLibraryProperties.Merge_annotations_dirs
524 props.Merge_inclusion_annotations_dirs = module.sdkLibraryProperties.Merge_inclusion_annotations_dirs
525
Paul Duffin235ffff2019-12-24 10:41:30 +0000526 droiddocArgs := []string{}
527 if len(module.sdkLibraryProperties.Api_packages) != 0 {
528 droiddocArgs = append(droiddocArgs, "--stub-packages "+strings.Join(module.sdkLibraryProperties.Api_packages, ":"))
529 }
530 if len(module.sdkLibraryProperties.Hidden_api_packages) != 0 {
531 droiddocArgs = append(droiddocArgs,
532 android.JoinWithPrefix(module.sdkLibraryProperties.Hidden_api_packages, " --hide-package "))
533 }
534 droiddocArgs = append(droiddocArgs, module.sdkLibraryProperties.Droiddoc_options...)
535 disabledWarnings := []string{
536 "MissingPermission",
537 "BroadcastBehavior",
538 "HiddenSuperclass",
539 "DeprecationMismatch",
540 "UnavailableSymbol",
541 "SdkConstant",
542 "HiddenTypeParameter",
543 "Todo",
544 "Typo",
545 }
546 droiddocArgs = append(droiddocArgs, android.JoinWithPrefix(disabledWarnings, "--hide "))
Sundong Ahnfb2721f2018-09-17 13:23:09 +0900547
Jiyong Parkdf130542018-04-27 16:29:21 +0900548 switch apiScope {
549 case apiScopeSystem:
Paul Duffin235ffff2019-12-24 10:41:30 +0000550 droiddocArgs = append(droiddocArgs, "-showAnnotation android.annotation.SystemApi")
Jiyong Parkdf130542018-04-27 16:29:21 +0900551 case apiScopeTest:
Paul Duffin235ffff2019-12-24 10:41:30 +0000552 droiddocArgs = append(droiddocArgs, " -showAnnotation android.annotation.TestApi")
Jiyong Parkc678ad32018-04-10 13:07:10 +0900553 }
Paul Duffin11512472019-02-11 15:55:17 +0000554 props.Arg_files = module.sdkLibraryProperties.Droiddoc_option_files
Paul Duffin235ffff2019-12-24 10:41:30 +0000555 props.Args = proptools.StringPtr(strings.Join(droiddocArgs, " "))
Jiyong Parkc678ad32018-04-10 13:07:10 +0900556
557 // List of APIs identified from the provided source files are created. They are later
558 // compared against to the not-yet-released (a.k.a current) list of APIs and to the
559 // last-released (a.k.a numbered) list of API.
Paul Duffind1b3a922020-01-22 11:57:20 +0000560 currentApiFileName := apiScope.apiFilePrefix + "current.txt"
561 removedApiFileName := apiScope.apiFilePrefix + "removed.txt"
Paul Duffin749f98f2019-12-30 17:23:46 +0000562 apiDir := module.getApiDir()
563 currentApiFileName = path.Join(apiDir, currentApiFileName)
564 removedApiFileName = path.Join(apiDir, removedApiFileName)
Jiyong Park58c518b2018-05-12 22:29:12 +0900565 // TODO(jiyong): remove these three props
Jiyong Parkdf130542018-04-27 16:29:21 +0900566 props.Api_tag_name = proptools.StringPtr(module.apiTagName(apiScope))
Jiyong Parkc678ad32018-04-10 13:07:10 +0900567 props.Api_filename = proptools.StringPtr(currentApiFileName)
568 props.Removed_api_filename = proptools.StringPtr(removedApiFileName)
569
Jiyong Park58c518b2018-05-12 22:29:12 +0900570 // check against the not-yet-release API
571 props.Check_api.Current.Api_file = proptools.StringPtr(currentApiFileName)
572 props.Check_api.Current.Removed_api_file = proptools.StringPtr(removedApiFileName)
Jiyong Park58c518b2018-05-12 22:29:12 +0900573
574 // check against the latest released API
575 props.Check_api.Last_released.Api_file = proptools.StringPtr(
576 module.latestApiFilegroupName(apiScope))
577 props.Check_api.Last_released.Removed_api_file = proptools.StringPtr(
578 module.latestRemovedApiFilegroupName(apiScope))
Inseob Kim38449af2019-02-28 14:24:05 +0900579 props.Check_api.Ignore_missing_latest_api = proptools.BoolPtr(true)
Jiyong Park58c518b2018-05-12 22:29:12 +0900580
Colin Cross84dfc3d2019-09-25 11:33:01 -0700581 mctx.CreateModule(DroidstubsFactory, &props)
Jiyong Parkc678ad32018-04-10 13:07:10 +0900582}
583
Jooyung Han5e9013b2020-03-10 06:23:13 +0900584func (module *SdkLibrary) DepIsInSameApex(mctx android.BaseModuleContext, dep android.Module) bool {
585 depTag := mctx.OtherModuleDependencyTag(dep)
586 if depTag == xmlPermissionsFileTag {
587 return true
588 }
589 return module.Library.DepIsInSameApex(mctx, dep)
590}
591
Jiyong Parkc678ad32018-04-10 13:07:10 +0900592// Creates the xml file that publicizes the runtime library
Colin Crossf8b860a2019-04-16 14:43:28 -0700593func (module *SdkLibrary) createXmlFile(mctx android.LoadHookContext) {
Jiyong Parke3833882020-02-17 17:28:10 +0900594 props := struct {
Sundong Ahn0d7dff42019-12-04 12:53:44 +0900595 Name *string
Jiyong Parke3833882020-02-17 17:28:10 +0900596 Lib_name *string
Sundong Ahn0d7dff42019-12-04 12:53:44 +0900597 Soc_specific *bool
598 Device_specific *bool
599 Product_specific *bool
600 System_ext_specific *bool
Jooyung Han5e9013b2020-03-10 06:23:13 +0900601 Apex_available []string
Jiyong Parke3833882020-02-17 17:28:10 +0900602 }{
Jooyung Han5e9013b2020-03-10 06:23:13 +0900603 Name: proptools.StringPtr(module.xmlFileName()),
604 Lib_name: proptools.StringPtr(module.BaseModuleName()),
605 Apex_available: module.ApexProperties.Apex_available,
Jiyong Parkc678ad32018-04-10 13:07:10 +0900606 }
Jiyong Parke3833882020-02-17 17:28:10 +0900607
608 if module.SocSpecific() {
609 props.Soc_specific = proptools.BoolPtr(true)
610 } else if module.DeviceSpecific() {
611 props.Device_specific = proptools.BoolPtr(true)
612 } else if module.ProductSpecific() {
613 props.Product_specific = proptools.BoolPtr(true)
614 } else if module.SystemExtSpecific() {
615 props.System_ext_specific = proptools.BoolPtr(true)
616 }
617
618 mctx.CreateModule(sdkLibraryXmlFactory, &props)
Jiyong Parkc678ad32018-04-10 13:07:10 +0900619}
620
Paul Duffin50061512020-01-21 16:31:05 +0000621func PrebuiltJars(ctx android.BaseModuleContext, baseName string, s sdkSpec) android.Paths {
Jiyong Park6a927c42020-01-21 02:03:43 +0900622 var ver sdkVersion
623 var kind sdkKind
624 if s.usePrebuilt(ctx) {
625 ver = s.version
626 kind = s.kind
Jiyong Parkc678ad32018-04-10 13:07:10 +0900627 } else {
Jiyong Park6a927c42020-01-21 02:03:43 +0900628 // We don't have prebuilt SDK for the specific sdkVersion.
629 // Instead of breaking the build, fallback to use "system_current"
630 ver = sdkVersionCurrent
631 kind = sdkSystem
Sundong Ahn054b19a2018-10-19 13:46:09 +0900632 }
Jiyong Park6a927c42020-01-21 02:03:43 +0900633
634 dir := filepath.Join("prebuilts", "sdk", ver.String(), kind.String())
Paul Duffin50061512020-01-21 16:31:05 +0000635 jar := filepath.Join(dir, baseName+".jar")
Sundong Ahn054b19a2018-10-19 13:46:09 +0900636 jarPath := android.ExistentPathForSource(ctx, jar)
Sundong Ahnae418ac2019-02-28 15:01:28 +0900637 if !jarPath.Valid() {
Colin Cross07c88562020-01-07 09:34:44 -0800638 if ctx.Config().AllowMissingDependencies() {
639 return android.Paths{android.PathForSource(ctx, jar)}
640 } else {
Jiyong Park6a927c42020-01-21 02:03:43 +0900641 ctx.PropertyErrorf("sdk_library", "invalid sdk version %q, %q does not exist", s.raw, jar)
Colin Cross07c88562020-01-07 09:34:44 -0800642 }
Sundong Ahnae418ac2019-02-28 15:01:28 +0900643 return nil
644 }
Sundong Ahn054b19a2018-10-19 13:46:09 +0900645 return android.Paths{jarPath.Path()}
646}
647
Paul Duffind1b3a922020-01-22 11:57:20 +0000648func (module *SdkLibrary) sdkJars(
649 ctx android.BaseModuleContext,
650 sdkVersion sdkSpec,
651 headerJars bool) android.Paths {
652
Paul Duffin50061512020-01-21 16:31:05 +0000653 // If a specific numeric version has been requested then use prebuilt versions of the sdk.
654 if sdkVersion.version.isNumbered() {
655 return PrebuiltJars(ctx, module.BaseModuleName(), sdkVersion)
Sundong Ahn054b19a2018-10-19 13:46:09 +0900656 } else {
Paul Duffind1b3a922020-01-22 11:57:20 +0000657 if !sdkVersion.specified() {
658 if headerJars {
659 return module.Library.HeaderJars()
660 } else {
661 return module.Library.ImplementationJars()
662 }
663 }
Paul Duffin726d23c2020-01-22 16:30:37 +0000664 var apiScope *apiScope
Jiyong Park6a927c42020-01-21 02:03:43 +0900665 switch sdkVersion.kind {
666 case sdkSystem:
Paul Duffin726d23c2020-01-22 16:30:37 +0000667 apiScope = apiScopeSystem
668 case sdkTest:
669 apiScope = apiScopeTest
Jiyong Park6a927c42020-01-21 02:03:43 +0900670 case sdkPrivate:
Sundong Ahn054b19a2018-10-19 13:46:09 +0900671 return module.Library.HeaderJars()
Jiyong Park6a927c42020-01-21 02:03:43 +0900672 default:
Paul Duffin726d23c2020-01-22 16:30:37 +0000673 apiScope = apiScopePublic
Paul Duffind1b3a922020-01-22 11:57:20 +0000674 }
675
Paul Duffin726d23c2020-01-22 16:30:37 +0000676 paths := module.getScopePaths(apiScope)
Paul Duffind1b3a922020-01-22 11:57:20 +0000677 if headerJars {
678 return paths.stubsHeaderPath
679 } else {
680 return paths.stubsImplPath
Sundong Ahn054b19a2018-10-19 13:46:09 +0900681 }
Jiyong Parkc678ad32018-04-10 13:07:10 +0900682 }
683}
684
Sundong Ahn241cd372018-07-13 16:16:44 +0900685// to satisfy SdkLibraryDependency interface
Paul Duffind1b3a922020-01-22 11:57:20 +0000686func (module *SdkLibrary) SdkHeaderJars(ctx android.BaseModuleContext, sdkVersion sdkSpec) android.Paths {
687 return module.sdkJars(ctx, sdkVersion, true /*headerJars*/)
688}
689
690// to satisfy SdkLibraryDependency interface
Jiyong Park6a927c42020-01-21 02:03:43 +0900691func (module *SdkLibrary) SdkImplementationJars(ctx android.BaseModuleContext, sdkVersion sdkSpec) android.Paths {
Paul Duffind1b3a922020-01-22 11:57:20 +0000692 return module.sdkJars(ctx, sdkVersion, false /*headerJars*/)
Sundong Ahn241cd372018-07-13 16:16:44 +0900693}
694
Sundong Ahn80a87b32019-05-13 15:02:50 +0900695func (module *SdkLibrary) SetNoDist() {
696 module.sdkLibraryProperties.No_dist = proptools.BoolPtr(true)
697}
698
Colin Cross571cccf2019-02-04 11:22:08 -0800699var javaSdkLibrariesKey = android.NewOnceKey("javaSdkLibraries")
700
Jiyong Park82484c02018-04-23 21:41:26 +0900701func javaSdkLibraries(config android.Config) *[]string {
Colin Cross571cccf2019-02-04 11:22:08 -0800702 return config.Once(javaSdkLibrariesKey, func() interface{} {
Jiyong Park82484c02018-04-23 21:41:26 +0900703 return &[]string{}
704 }).(*[]string)
705}
706
Paul Duffin749f98f2019-12-30 17:23:46 +0000707func (module *SdkLibrary) getApiDir() string {
708 return proptools.StringDefault(module.sdkLibraryProperties.Api_dir, "api")
709}
710
Jiyong Parkc678ad32018-04-10 13:07:10 +0900711// For a java_sdk_library module, create internal modules for stubs, docs,
712// runtime libs and xml file. If requested, the stubs and docs are created twice
713// once for public API level and once for system API level
Colin Crossf8b860a2019-04-16 14:43:28 -0700714func (module *SdkLibrary) CreateInternalModules(mctx android.LoadHookContext) {
Inseob Kim6e93ac92019-03-21 17:43:49 +0900715 if len(module.Library.Module.properties.Srcs) == 0 {
Inseob Kimc0907f12019-02-08 21:00:45 +0900716 mctx.PropertyErrorf("srcs", "java_sdk_library must specify srcs")
Jooyung Han58f26ab2019-12-18 15:34:32 +0900717 return
Inseob Kimc0907f12019-02-08 21:00:45 +0900718 }
719
Paul Duffin37e0b772019-12-30 17:20:10 +0000720 // If this builds against standard libraries (i.e. is not part of the core libraries)
721 // then assume it provides both system and test apis. Otherwise, assume it does not and
722 // also assume it does not contribute to the dist build.
723 sdkDep := decodeSdkDep(mctx, sdkContext(&module.Library))
724 hasSystemAndTestApis := sdkDep.hasStandardLibs()
725 module.sdkLibraryProperties.Has_system_and_test_apis = hasSystemAndTestApis
726 module.sdkLibraryProperties.No_dist = proptools.BoolPtr(!hasSystemAndTestApis)
727
Inseob Kim8098faa2019-03-18 10:19:51 +0900728 missing_current_api := false
729
Paul Duffind1b3a922020-01-22 11:57:20 +0000730 activeScopes := module.getActiveApiScopes()
731
Paul Duffin749f98f2019-12-30 17:23:46 +0000732 apiDir := module.getApiDir()
Paul Duffind1b3a922020-01-22 11:57:20 +0000733 for _, scope := range activeScopes {
Inseob Kim8098faa2019-03-18 10:19:51 +0900734 for _, api := range []string{"current.txt", "removed.txt"} {
Paul Duffind1b3a922020-01-22 11:57:20 +0000735 path := path.Join(mctx.ModuleDir(), apiDir, scope.apiFilePrefix+api)
Inseob Kim8098faa2019-03-18 10:19:51 +0900736 p := android.ExistentPathForSource(mctx, path)
737 if !p.Valid() {
738 mctx.ModuleErrorf("Current api file %#v doesn't exist", path)
739 missing_current_api = true
740 }
741 }
742 }
743
744 if missing_current_api {
745 script := "build/soong/scripts/gen-java-current-api-files.sh"
746 p := android.ExistentPathForSource(mctx, script)
747
748 if !p.Valid() {
749 panic(fmt.Sprintf("script file %s doesn't exist", script))
750 }
751
752 mctx.ModuleErrorf("One or more current api files are missing. "+
753 "You can update them by:\n"+
Paul Duffin37e0b772019-12-30 17:20:10 +0000754 "%s %q %s && m update-api",
Paul Duffind1b3a922020-01-22 11:57:20 +0000755 script, filepath.Join(mctx.ModuleDir(), apiDir),
756 strings.Join(activeScopes.Strings(func(s *apiScope) string { return s.apiFilePrefix }), " "))
Inseob Kim8098faa2019-03-18 10:19:51 +0900757 return
758 }
759
Paul Duffind1b3a922020-01-22 11:57:20 +0000760 for _, scope := range activeScopes {
761 module.createStubsLibrary(mctx, scope)
762 module.createStubsSources(mctx, scope)
Inseob Kimc0907f12019-02-08 21:00:45 +0900763 }
764
Paul Duffin43db9be2019-12-30 17:35:49 +0000765 if !proptools.Bool(module.sdkLibraryProperties.Api_only) {
766 // for runtime
767 module.createXmlFile(mctx)
768
769 // record java_sdk_library modules so that they are exported to make
770 javaSdkLibraries := javaSdkLibraries(mctx.Config())
771 javaSdkLibrariesLock.Lock()
772 defer javaSdkLibrariesLock.Unlock()
773 *javaSdkLibraries = append(*javaSdkLibraries, module.BaseModuleName())
774 }
Inseob Kimc0907f12019-02-08 21:00:45 +0900775}
776
777func (module *SdkLibrary) InitSdkLibraryProperties() {
Sundong Ahn054b19a2018-10-19 13:46:09 +0900778 module.AddProperties(
779 &module.sdkLibraryProperties,
780 &module.Library.Module.properties,
781 &module.Library.Module.dexpreoptProperties,
782 &module.Library.Module.deviceProperties,
783 &module.Library.Module.protoProperties,
784 )
785
786 module.Library.Module.properties.Installable = proptools.BoolPtr(true)
787 module.Library.Module.deviceProperties.IsSDKLibrary = true
Inseob Kimc0907f12019-02-08 21:00:45 +0900788}
Sundong Ahn054b19a2018-10-19 13:46:09 +0900789
Jaewoong Jung4f158ee2019-07-11 10:05:35 -0700790// java_sdk_library is a special Java library that provides optional platform APIs to apps.
791// In practice, it can be viewed as a combination of several modules: 1) stubs library that clients
792// are linked against to, 2) droiddoc module that internally generates API stubs source files,
793// 3) the real runtime shared library that implements the APIs, and 4) XML file for adding
794// the runtime lib to the classpath at runtime if requested via <uses-library>.
Inseob Kimc0907f12019-02-08 21:00:45 +0900795func SdkLibraryFactory() android.Module {
796 module := &SdkLibrary{}
797 module.InitSdkLibraryProperties()
Jooyung Han58f26ab2019-12-18 15:34:32 +0900798 android.InitApexModule(module)
Sundong Ahn054b19a2018-10-19 13:46:09 +0900799 InitJavaModule(module, android.HostAndDeviceSupported)
Colin Crossf8b860a2019-04-16 14:43:28 -0700800 android.AddLoadHook(module, func(ctx android.LoadHookContext) { module.CreateInternalModules(ctx) })
Jiyong Parkc678ad32018-04-10 13:07:10 +0900801 return module
802}
Colin Cross79c7c262019-04-17 11:11:46 -0700803
804//
805// SDK library prebuilts
806//
807
Paul Duffin56d44902020-01-31 13:36:25 +0000808// Properties associated with each api scope.
809type sdkLibraryScopeProperties struct {
Colin Cross79c7c262019-04-17 11:11:46 -0700810 Jars []string `android:"path"`
811
812 Sdk_version *string
813
Colin Cross79c7c262019-04-17 11:11:46 -0700814 // List of shared java libs that this module has dependencies to
815 Libs []string
Colin Cross79c7c262019-04-17 11:11:46 -0700816}
817
Paul Duffin56d44902020-01-31 13:36:25 +0000818type sdkLibraryImportProperties struct {
Paul Duffinfcfd7912020-01-31 17:54:30 +0000819 // List of shared java libs, common to all scopes, that this module has
820 // dependencies to
821 Libs []string
822
Paul Duffin56d44902020-01-31 13:36:25 +0000823 // Properties associated with the public api scope.
824 Public sdkLibraryScopeProperties
825
826 // Properties associated with the system api scope.
827 System sdkLibraryScopeProperties
828
829 // Properties associated with the test api scope.
830 Test sdkLibraryScopeProperties
831}
832
Colin Cross79c7c262019-04-17 11:11:46 -0700833type sdkLibraryImport struct {
834 android.ModuleBase
835 android.DefaultableModuleBase
836 prebuilt android.Prebuilt
837
838 properties sdkLibraryImportProperties
839
Paul Duffin56d44902020-01-31 13:36:25 +0000840 commonToSdkLibraryAndImport
Colin Cross79c7c262019-04-17 11:11:46 -0700841}
842
843var _ SdkLibraryDependency = (*sdkLibraryImport)(nil)
844
Jaewoong Jung4f158ee2019-07-11 10:05:35 -0700845// java_sdk_library_import imports a prebuilt java_sdk_library.
Colin Cross79c7c262019-04-17 11:11:46 -0700846func sdkLibraryImportFactory() android.Module {
847 module := &sdkLibraryImport{}
848
Paul Duffinfcfd7912020-01-31 17:54:30 +0000849 module.AddProperties(&module.properties)
Colin Cross79c7c262019-04-17 11:11:46 -0700850
Paul Duffin0bdcb272020-02-06 15:24:57 +0000851 android.InitPrebuiltModule(module, &[]string{""})
Colin Cross79c7c262019-04-17 11:11:46 -0700852 InitJavaModule(module, android.HostAndDeviceSupported)
853
854 android.AddLoadHook(module, func(mctx android.LoadHookContext) { module.createInternalModules(mctx) })
855 return module
856}
857
858func (module *sdkLibraryImport) Prebuilt() *android.Prebuilt {
859 return &module.prebuilt
860}
861
862func (module *sdkLibraryImport) Name() string {
863 return module.prebuilt.Name(module.ModuleBase.Name())
864}
865
866func (module *sdkLibraryImport) createInternalModules(mctx android.LoadHookContext) {
Colin Cross79c7c262019-04-17 11:11:46 -0700867
Paul Duffin50061512020-01-21 16:31:05 +0000868 // If the build is configured to use prebuilts then force this to be preferred.
869 if mctx.Config().UnbundledBuildUsePrebuiltSdks() {
870 module.prebuilt.ForcePrefer()
871 }
872
Paul Duffin56d44902020-01-31 13:36:25 +0000873 for apiScope, scopeProperties := range module.scopeProperties() {
874 if len(scopeProperties.Jars) == 0 {
875 continue
876 }
877
878 // Creates a java import for the jar with ".stubs" suffix
879 props := struct {
880 Name *string
881 Soc_specific *bool
882 Device_specific *bool
883 Product_specific *bool
884 System_ext_specific *bool
885 Sdk_version *string
886 Libs []string
887 Jars []string
Paul Duffin50061512020-01-21 16:31:05 +0000888 Prefer *bool
Paul Duffin56d44902020-01-31 13:36:25 +0000889 }{}
890
891 props.Name = proptools.StringPtr(apiScope.stubsModuleName(module.BaseModuleName()))
892 props.Sdk_version = scopeProperties.Sdk_version
Paul Duffinfcfd7912020-01-31 17:54:30 +0000893 // Prepend any of the libs from the legacy public properties to the libs for each of the
894 // scopes to avoid having to duplicate them in each scope.
895 props.Libs = append(module.properties.Libs, scopeProperties.Libs...)
Paul Duffin56d44902020-01-31 13:36:25 +0000896 props.Jars = scopeProperties.Jars
897
898 if module.SocSpecific() {
899 props.Soc_specific = proptools.BoolPtr(true)
900 } else if module.DeviceSpecific() {
901 props.Device_specific = proptools.BoolPtr(true)
902 } else if module.ProductSpecific() {
903 props.Product_specific = proptools.BoolPtr(true)
904 } else if module.SystemExtSpecific() {
905 props.System_ext_specific = proptools.BoolPtr(true)
906 }
907
Paul Duffin50061512020-01-21 16:31:05 +0000908 // If the build should use prebuilt sdks then set prefer to true on the stubs library.
909 // That will cause the prebuilt version of the stubs to override the source version.
910 if mctx.Config().UnbundledBuildUsePrebuiltSdks() {
911 props.Prefer = proptools.BoolPtr(true)
912 }
913
Paul Duffin56d44902020-01-31 13:36:25 +0000914 mctx.CreateModule(ImportFactory, &props)
915 }
Colin Cross79c7c262019-04-17 11:11:46 -0700916
917 javaSdkLibraries := javaSdkLibraries(mctx.Config())
918 javaSdkLibrariesLock.Lock()
919 defer javaSdkLibrariesLock.Unlock()
920 *javaSdkLibraries = append(*javaSdkLibraries, module.BaseModuleName())
921}
922
Paul Duffin56d44902020-01-31 13:36:25 +0000923func (module *sdkLibraryImport) scopeProperties() map[*apiScope]*sdkLibraryScopeProperties {
924 p := make(map[*apiScope]*sdkLibraryScopeProperties)
925 p[apiScopePublic] = &module.properties.Public
926 p[apiScopeSystem] = &module.properties.System
927 p[apiScopeTest] = &module.properties.Test
928 return p
929}
930
Colin Cross79c7c262019-04-17 11:11:46 -0700931func (module *sdkLibraryImport) DepsMutator(ctx android.BottomUpMutatorContext) {
Paul Duffin56d44902020-01-31 13:36:25 +0000932 for apiScope, scopeProperties := range module.scopeProperties() {
933 if len(scopeProperties.Jars) == 0 {
934 continue
935 }
936
937 // Add dependencies to the prebuilt stubs library
938 ctx.AddVariationDependencies(nil, apiScope.stubsTag, apiScope.stubsModuleName(module.BaseModuleName()))
939 }
Colin Cross79c7c262019-04-17 11:11:46 -0700940}
941
942func (module *sdkLibraryImport) GenerateAndroidBuildActions(ctx android.ModuleContext) {
943 // Record the paths to the prebuilt stubs library.
944 ctx.VisitDirectDeps(func(to android.Module) {
945 tag := ctx.OtherModuleDependencyTag(to)
946
Paul Duffin56d44902020-01-31 13:36:25 +0000947 if lib, ok := to.(Dependency); ok {
948 if scopeTag, ok := tag.(scopeDependencyTag); ok {
949 apiScope := scopeTag.apiScope
950 scopePaths := module.getScopePaths(apiScope)
951 scopePaths.stubsHeaderPath = lib.HeaderJars()
952 }
Colin Cross79c7c262019-04-17 11:11:46 -0700953 }
954 })
955}
956
Paul Duffin56d44902020-01-31 13:36:25 +0000957func (module *sdkLibraryImport) sdkJars(
958 ctx android.BaseModuleContext,
959 sdkVersion sdkSpec) android.Paths {
960
Paul Duffin50061512020-01-21 16:31:05 +0000961 // If a specific numeric version has been requested then use prebuilt versions of the sdk.
962 if sdkVersion.version.isNumbered() {
963 return PrebuiltJars(ctx, module.BaseModuleName(), sdkVersion)
964 }
965
Paul Duffin56d44902020-01-31 13:36:25 +0000966 var apiScope *apiScope
967 switch sdkVersion.kind {
968 case sdkSystem:
969 apiScope = apiScopeSystem
970 case sdkTest:
971 apiScope = apiScopeTest
972 default:
973 apiScope = apiScopePublic
974 }
975
976 paths := module.getScopePaths(apiScope)
977 return paths.stubsHeaderPath
978}
979
Colin Cross79c7c262019-04-17 11:11:46 -0700980// to satisfy SdkLibraryDependency interface
Jiyong Park6a927c42020-01-21 02:03:43 +0900981func (module *sdkLibraryImport) SdkHeaderJars(ctx android.BaseModuleContext, sdkVersion sdkSpec) android.Paths {
Colin Cross79c7c262019-04-17 11:11:46 -0700982 // This module is just a wrapper for the prebuilt stubs.
Paul Duffin56d44902020-01-31 13:36:25 +0000983 return module.sdkJars(ctx, sdkVersion)
Colin Cross79c7c262019-04-17 11:11:46 -0700984}
985
986// to satisfy SdkLibraryDependency interface
Jiyong Park6a927c42020-01-21 02:03:43 +0900987func (module *sdkLibraryImport) SdkImplementationJars(ctx android.BaseModuleContext, sdkVersion sdkSpec) android.Paths {
Colin Cross79c7c262019-04-17 11:11:46 -0700988 // This module is just a wrapper for the stubs.
Paul Duffin56d44902020-01-31 13:36:25 +0000989 return module.sdkJars(ctx, sdkVersion)
Colin Cross79c7c262019-04-17 11:11:46 -0700990}
Jiyong Parke3833882020-02-17 17:28:10 +0900991
992//
993// java_sdk_library_xml
994//
995type sdkLibraryXml struct {
996 android.ModuleBase
997 android.DefaultableModuleBase
998 android.ApexModuleBase
999
1000 properties sdkLibraryXmlProperties
1001
1002 outputFilePath android.OutputPath
1003 installDirPath android.InstallPath
1004}
1005
1006type sdkLibraryXmlProperties struct {
1007 // canonical name of the lib
1008 Lib_name *string
1009}
1010
1011// java_sdk_library_xml builds the permission xml file for a java_sdk_library.
1012// Not to be used directly by users. java_sdk_library internally uses this.
1013func sdkLibraryXmlFactory() android.Module {
1014 module := &sdkLibraryXml{}
1015
1016 module.AddProperties(&module.properties)
1017
1018 android.InitApexModule(module)
1019 android.InitAndroidArchModule(module, android.DeviceSupported, android.MultilibCommon)
1020
1021 return module
1022}
1023
1024// from android.PrebuiltEtcModule
1025func (module *sdkLibraryXml) SubDir() string {
1026 return "permissions"
1027}
1028
1029// from android.PrebuiltEtcModule
1030func (module *sdkLibraryXml) OutputFile() android.OutputPath {
1031 return module.outputFilePath
1032}
1033
1034// from android.ApexModule
1035func (module *sdkLibraryXml) AvailableFor(what string) bool {
1036 return true
1037}
1038
1039func (module *sdkLibraryXml) DepsMutator(ctx android.BottomUpMutatorContext) {
1040 // do nothing
1041}
1042
1043// File path to the runtime implementation library
1044func (module *sdkLibraryXml) implPath() string {
1045 implName := proptools.String(module.properties.Lib_name)
1046 if apexName := module.ApexName(); apexName != "" {
1047 // TODO(b/146468504): ApexName() is only a soong module name, not apex name.
1048 // In most cases, this works fine. But when apex_name is set or override_apex is used
1049 // this can be wrong.
1050 return fmt.Sprintf("/apex/%s/javalib/%s.jar", apexName, implName)
1051 }
1052 partition := "system"
1053 if module.SocSpecific() {
1054 partition = "vendor"
1055 } else if module.DeviceSpecific() {
1056 partition = "odm"
1057 } else if module.ProductSpecific() {
1058 partition = "product"
1059 } else if module.SystemExtSpecific() {
1060 partition = "system_ext"
1061 }
1062 return "/" + partition + "/framework/" + implName + ".jar"
1063}
1064
1065func (module *sdkLibraryXml) GenerateAndroidBuildActions(ctx android.ModuleContext) {
1066 libName := proptools.String(module.properties.Lib_name)
1067 xmlContent := fmt.Sprintf(permissionsTemplate, libName, module.implPath())
1068
1069 module.outputFilePath = android.PathForModuleOut(ctx, libName+".xml").OutputPath
1070 rule := android.NewRuleBuilder()
1071 rule.Command().
1072 Text("/bin/bash -c \"echo -e '" + xmlContent + "'\" > ").
1073 Output(module.outputFilePath)
1074
1075 rule.Build(pctx, ctx, "java_sdk_xml", "Permission XML")
1076
1077 module.installDirPath = android.PathForModuleInstall(ctx, "etc", module.SubDir())
1078}
1079
1080func (module *sdkLibraryXml) AndroidMkEntries() []android.AndroidMkEntries {
1081 if !module.IsForPlatform() {
1082 return []android.AndroidMkEntries{android.AndroidMkEntries{
1083 Disabled: true,
1084 }}
1085 }
1086
1087 return []android.AndroidMkEntries{android.AndroidMkEntries{
1088 Class: "ETC",
1089 OutputFile: android.OptionalPathForPath(module.outputFilePath),
1090 ExtraEntries: []android.AndroidMkExtraEntriesFunc{
1091 func(entries *android.AndroidMkEntries) {
1092 entries.SetString("LOCAL_MODULE_TAGS", "optional")
1093 entries.SetString("LOCAL_MODULE_PATH", module.installDirPath.ToMakePath().String())
1094 entries.SetString("LOCAL_INSTALLED_MODULE_STEM", module.outputFilePath.Base())
1095 },
1096 },
1097 }}
1098}