blob: a8edf1d559866331c1522f4c86fa559ac36df36a [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
Jiyong Parkc678ad32018-04-10 13:07:10 +0900584// Creates the xml file that publicizes the runtime library
Colin Crossf8b860a2019-04-16 14:43:28 -0700585func (module *SdkLibrary) createXmlFile(mctx android.LoadHookContext) {
Jiyong Parke3833882020-02-17 17:28:10 +0900586 props := struct {
Sundong Ahn0d7dff42019-12-04 12:53:44 +0900587 Name *string
Jiyong Parke3833882020-02-17 17:28:10 +0900588 Lib_name *string
Sundong Ahn0d7dff42019-12-04 12:53:44 +0900589 Soc_specific *bool
590 Device_specific *bool
591 Product_specific *bool
592 System_ext_specific *bool
Jiyong Parke3833882020-02-17 17:28:10 +0900593 }{
594 Name: proptools.StringPtr(module.xmlFileName()),
595 Lib_name: proptools.StringPtr(module.BaseModuleName()),
Jiyong Parkc678ad32018-04-10 13:07:10 +0900596 }
Jiyong Parke3833882020-02-17 17:28:10 +0900597
598 if module.SocSpecific() {
599 props.Soc_specific = proptools.BoolPtr(true)
600 } else if module.DeviceSpecific() {
601 props.Device_specific = proptools.BoolPtr(true)
602 } else if module.ProductSpecific() {
603 props.Product_specific = proptools.BoolPtr(true)
604 } else if module.SystemExtSpecific() {
605 props.System_ext_specific = proptools.BoolPtr(true)
606 }
607
608 mctx.CreateModule(sdkLibraryXmlFactory, &props)
Jiyong Parkc678ad32018-04-10 13:07:10 +0900609}
610
Paul Duffin50061512020-01-21 16:31:05 +0000611func PrebuiltJars(ctx android.BaseModuleContext, baseName string, s sdkSpec) android.Paths {
Jiyong Park6a927c42020-01-21 02:03:43 +0900612 var ver sdkVersion
613 var kind sdkKind
614 if s.usePrebuilt(ctx) {
615 ver = s.version
616 kind = s.kind
Jiyong Parkc678ad32018-04-10 13:07:10 +0900617 } else {
Jiyong Park6a927c42020-01-21 02:03:43 +0900618 // We don't have prebuilt SDK for the specific sdkVersion.
619 // Instead of breaking the build, fallback to use "system_current"
620 ver = sdkVersionCurrent
621 kind = sdkSystem
Sundong Ahn054b19a2018-10-19 13:46:09 +0900622 }
Jiyong Park6a927c42020-01-21 02:03:43 +0900623
624 dir := filepath.Join("prebuilts", "sdk", ver.String(), kind.String())
Paul Duffin50061512020-01-21 16:31:05 +0000625 jar := filepath.Join(dir, baseName+".jar")
Sundong Ahn054b19a2018-10-19 13:46:09 +0900626 jarPath := android.ExistentPathForSource(ctx, jar)
Sundong Ahnae418ac2019-02-28 15:01:28 +0900627 if !jarPath.Valid() {
Colin Cross07c88562020-01-07 09:34:44 -0800628 if ctx.Config().AllowMissingDependencies() {
629 return android.Paths{android.PathForSource(ctx, jar)}
630 } else {
Jiyong Park6a927c42020-01-21 02:03:43 +0900631 ctx.PropertyErrorf("sdk_library", "invalid sdk version %q, %q does not exist", s.raw, jar)
Colin Cross07c88562020-01-07 09:34:44 -0800632 }
Sundong Ahnae418ac2019-02-28 15:01:28 +0900633 return nil
634 }
Sundong Ahn054b19a2018-10-19 13:46:09 +0900635 return android.Paths{jarPath.Path()}
636}
637
Paul Duffind1b3a922020-01-22 11:57:20 +0000638func (module *SdkLibrary) sdkJars(
639 ctx android.BaseModuleContext,
640 sdkVersion sdkSpec,
641 headerJars bool) android.Paths {
642
Paul Duffin50061512020-01-21 16:31:05 +0000643 // If a specific numeric version has been requested then use prebuilt versions of the sdk.
644 if sdkVersion.version.isNumbered() {
645 return PrebuiltJars(ctx, module.BaseModuleName(), sdkVersion)
Sundong Ahn054b19a2018-10-19 13:46:09 +0900646 } else {
Paul Duffind1b3a922020-01-22 11:57:20 +0000647 if !sdkVersion.specified() {
648 if headerJars {
649 return module.Library.HeaderJars()
650 } else {
651 return module.Library.ImplementationJars()
652 }
653 }
Paul Duffin726d23c2020-01-22 16:30:37 +0000654 var apiScope *apiScope
Jiyong Park6a927c42020-01-21 02:03:43 +0900655 switch sdkVersion.kind {
656 case sdkSystem:
Paul Duffin726d23c2020-01-22 16:30:37 +0000657 apiScope = apiScopeSystem
658 case sdkTest:
659 apiScope = apiScopeTest
Jiyong Park6a927c42020-01-21 02:03:43 +0900660 case sdkPrivate:
Sundong Ahn054b19a2018-10-19 13:46:09 +0900661 return module.Library.HeaderJars()
Jiyong Park6a927c42020-01-21 02:03:43 +0900662 default:
Paul Duffin726d23c2020-01-22 16:30:37 +0000663 apiScope = apiScopePublic
Paul Duffind1b3a922020-01-22 11:57:20 +0000664 }
665
Paul Duffin726d23c2020-01-22 16:30:37 +0000666 paths := module.getScopePaths(apiScope)
Paul Duffind1b3a922020-01-22 11:57:20 +0000667 if headerJars {
668 return paths.stubsHeaderPath
669 } else {
670 return paths.stubsImplPath
Sundong Ahn054b19a2018-10-19 13:46:09 +0900671 }
Jiyong Parkc678ad32018-04-10 13:07:10 +0900672 }
673}
674
Sundong Ahn241cd372018-07-13 16:16:44 +0900675// to satisfy SdkLibraryDependency interface
Paul Duffind1b3a922020-01-22 11:57:20 +0000676func (module *SdkLibrary) SdkHeaderJars(ctx android.BaseModuleContext, sdkVersion sdkSpec) android.Paths {
677 return module.sdkJars(ctx, sdkVersion, true /*headerJars*/)
678}
679
680// to satisfy SdkLibraryDependency interface
Jiyong Park6a927c42020-01-21 02:03:43 +0900681func (module *SdkLibrary) SdkImplementationJars(ctx android.BaseModuleContext, sdkVersion sdkSpec) android.Paths {
Paul Duffind1b3a922020-01-22 11:57:20 +0000682 return module.sdkJars(ctx, sdkVersion, false /*headerJars*/)
Sundong Ahn241cd372018-07-13 16:16:44 +0900683}
684
Sundong Ahn80a87b32019-05-13 15:02:50 +0900685func (module *SdkLibrary) SetNoDist() {
686 module.sdkLibraryProperties.No_dist = proptools.BoolPtr(true)
687}
688
Colin Cross571cccf2019-02-04 11:22:08 -0800689var javaSdkLibrariesKey = android.NewOnceKey("javaSdkLibraries")
690
Jiyong Park82484c02018-04-23 21:41:26 +0900691func javaSdkLibraries(config android.Config) *[]string {
Colin Cross571cccf2019-02-04 11:22:08 -0800692 return config.Once(javaSdkLibrariesKey, func() interface{} {
Jiyong Park82484c02018-04-23 21:41:26 +0900693 return &[]string{}
694 }).(*[]string)
695}
696
Paul Duffin749f98f2019-12-30 17:23:46 +0000697func (module *SdkLibrary) getApiDir() string {
698 return proptools.StringDefault(module.sdkLibraryProperties.Api_dir, "api")
699}
700
Jiyong Parkc678ad32018-04-10 13:07:10 +0900701// For a java_sdk_library module, create internal modules for stubs, docs,
702// runtime libs and xml file. If requested, the stubs and docs are created twice
703// once for public API level and once for system API level
Colin Crossf8b860a2019-04-16 14:43:28 -0700704func (module *SdkLibrary) CreateInternalModules(mctx android.LoadHookContext) {
Inseob Kim6e93ac92019-03-21 17:43:49 +0900705 if len(module.Library.Module.properties.Srcs) == 0 {
Inseob Kimc0907f12019-02-08 21:00:45 +0900706 mctx.PropertyErrorf("srcs", "java_sdk_library must specify srcs")
Jooyung Han58f26ab2019-12-18 15:34:32 +0900707 return
Inseob Kimc0907f12019-02-08 21:00:45 +0900708 }
709
Paul Duffin37e0b772019-12-30 17:20:10 +0000710 // If this builds against standard libraries (i.e. is not part of the core libraries)
711 // then assume it provides both system and test apis. Otherwise, assume it does not and
712 // also assume it does not contribute to the dist build.
713 sdkDep := decodeSdkDep(mctx, sdkContext(&module.Library))
714 hasSystemAndTestApis := sdkDep.hasStandardLibs()
715 module.sdkLibraryProperties.Has_system_and_test_apis = hasSystemAndTestApis
716 module.sdkLibraryProperties.No_dist = proptools.BoolPtr(!hasSystemAndTestApis)
717
Inseob Kim8098faa2019-03-18 10:19:51 +0900718 missing_current_api := false
719
Paul Duffind1b3a922020-01-22 11:57:20 +0000720 activeScopes := module.getActiveApiScopes()
721
Paul Duffin749f98f2019-12-30 17:23:46 +0000722 apiDir := module.getApiDir()
Paul Duffind1b3a922020-01-22 11:57:20 +0000723 for _, scope := range activeScopes {
Inseob Kim8098faa2019-03-18 10:19:51 +0900724 for _, api := range []string{"current.txt", "removed.txt"} {
Paul Duffind1b3a922020-01-22 11:57:20 +0000725 path := path.Join(mctx.ModuleDir(), apiDir, scope.apiFilePrefix+api)
Inseob Kim8098faa2019-03-18 10:19:51 +0900726 p := android.ExistentPathForSource(mctx, path)
727 if !p.Valid() {
728 mctx.ModuleErrorf("Current api file %#v doesn't exist", path)
729 missing_current_api = true
730 }
731 }
732 }
733
734 if missing_current_api {
735 script := "build/soong/scripts/gen-java-current-api-files.sh"
736 p := android.ExistentPathForSource(mctx, script)
737
738 if !p.Valid() {
739 panic(fmt.Sprintf("script file %s doesn't exist", script))
740 }
741
742 mctx.ModuleErrorf("One or more current api files are missing. "+
743 "You can update them by:\n"+
Paul Duffin37e0b772019-12-30 17:20:10 +0000744 "%s %q %s && m update-api",
Paul Duffind1b3a922020-01-22 11:57:20 +0000745 script, filepath.Join(mctx.ModuleDir(), apiDir),
746 strings.Join(activeScopes.Strings(func(s *apiScope) string { return s.apiFilePrefix }), " "))
Inseob Kim8098faa2019-03-18 10:19:51 +0900747 return
748 }
749
Paul Duffind1b3a922020-01-22 11:57:20 +0000750 for _, scope := range activeScopes {
751 module.createStubsLibrary(mctx, scope)
752 module.createStubsSources(mctx, scope)
Inseob Kimc0907f12019-02-08 21:00:45 +0900753 }
754
Paul Duffin43db9be2019-12-30 17:35:49 +0000755 if !proptools.Bool(module.sdkLibraryProperties.Api_only) {
756 // for runtime
757 module.createXmlFile(mctx)
758
759 // record java_sdk_library modules so that they are exported to make
760 javaSdkLibraries := javaSdkLibraries(mctx.Config())
761 javaSdkLibrariesLock.Lock()
762 defer javaSdkLibrariesLock.Unlock()
763 *javaSdkLibraries = append(*javaSdkLibraries, module.BaseModuleName())
764 }
Inseob Kimc0907f12019-02-08 21:00:45 +0900765}
766
767func (module *SdkLibrary) InitSdkLibraryProperties() {
Sundong Ahn054b19a2018-10-19 13:46:09 +0900768 module.AddProperties(
769 &module.sdkLibraryProperties,
770 &module.Library.Module.properties,
771 &module.Library.Module.dexpreoptProperties,
772 &module.Library.Module.deviceProperties,
773 &module.Library.Module.protoProperties,
774 )
775
776 module.Library.Module.properties.Installable = proptools.BoolPtr(true)
777 module.Library.Module.deviceProperties.IsSDKLibrary = true
Inseob Kimc0907f12019-02-08 21:00:45 +0900778}
Sundong Ahn054b19a2018-10-19 13:46:09 +0900779
Jaewoong Jung4f158ee2019-07-11 10:05:35 -0700780// java_sdk_library is a special Java library that provides optional platform APIs to apps.
781// In practice, it can be viewed as a combination of several modules: 1) stubs library that clients
782// are linked against to, 2) droiddoc module that internally generates API stubs source files,
783// 3) the real runtime shared library that implements the APIs, and 4) XML file for adding
784// the runtime lib to the classpath at runtime if requested via <uses-library>.
Inseob Kimc0907f12019-02-08 21:00:45 +0900785func SdkLibraryFactory() android.Module {
786 module := &SdkLibrary{}
787 module.InitSdkLibraryProperties()
Jooyung Han58f26ab2019-12-18 15:34:32 +0900788 android.InitApexModule(module)
Sundong Ahn054b19a2018-10-19 13:46:09 +0900789 InitJavaModule(module, android.HostAndDeviceSupported)
Colin Crossf8b860a2019-04-16 14:43:28 -0700790 android.AddLoadHook(module, func(ctx android.LoadHookContext) { module.CreateInternalModules(ctx) })
Jiyong Parkc678ad32018-04-10 13:07:10 +0900791 return module
792}
Colin Cross79c7c262019-04-17 11:11:46 -0700793
794//
795// SDK library prebuilts
796//
797
Paul Duffin56d44902020-01-31 13:36:25 +0000798// Properties associated with each api scope.
799type sdkLibraryScopeProperties struct {
Colin Cross79c7c262019-04-17 11:11:46 -0700800 Jars []string `android:"path"`
801
802 Sdk_version *string
803
Colin Cross79c7c262019-04-17 11:11:46 -0700804 // List of shared java libs that this module has dependencies to
805 Libs []string
Colin Cross79c7c262019-04-17 11:11:46 -0700806}
807
Paul Duffin56d44902020-01-31 13:36:25 +0000808type sdkLibraryImportProperties struct {
Paul Duffinfcfd7912020-01-31 17:54:30 +0000809 // List of shared java libs, common to all scopes, that this module has
810 // dependencies to
811 Libs []string
812
Paul Duffin56d44902020-01-31 13:36:25 +0000813 // Properties associated with the public api scope.
814 Public sdkLibraryScopeProperties
815
816 // Properties associated with the system api scope.
817 System sdkLibraryScopeProperties
818
819 // Properties associated with the test api scope.
820 Test sdkLibraryScopeProperties
821}
822
Colin Cross79c7c262019-04-17 11:11:46 -0700823type sdkLibraryImport struct {
824 android.ModuleBase
825 android.DefaultableModuleBase
826 prebuilt android.Prebuilt
827
828 properties sdkLibraryImportProperties
829
Paul Duffin56d44902020-01-31 13:36:25 +0000830 commonToSdkLibraryAndImport
Colin Cross79c7c262019-04-17 11:11:46 -0700831}
832
833var _ SdkLibraryDependency = (*sdkLibraryImport)(nil)
834
Jaewoong Jung4f158ee2019-07-11 10:05:35 -0700835// java_sdk_library_import imports a prebuilt java_sdk_library.
Colin Cross79c7c262019-04-17 11:11:46 -0700836func sdkLibraryImportFactory() android.Module {
837 module := &sdkLibraryImport{}
838
Paul Duffinfcfd7912020-01-31 17:54:30 +0000839 module.AddProperties(&module.properties)
Colin Cross79c7c262019-04-17 11:11:46 -0700840
Paul Duffin0bdcb272020-02-06 15:24:57 +0000841 android.InitPrebuiltModule(module, &[]string{""})
Colin Cross79c7c262019-04-17 11:11:46 -0700842 InitJavaModule(module, android.HostAndDeviceSupported)
843
844 android.AddLoadHook(module, func(mctx android.LoadHookContext) { module.createInternalModules(mctx) })
845 return module
846}
847
848func (module *sdkLibraryImport) Prebuilt() *android.Prebuilt {
849 return &module.prebuilt
850}
851
852func (module *sdkLibraryImport) Name() string {
853 return module.prebuilt.Name(module.ModuleBase.Name())
854}
855
856func (module *sdkLibraryImport) createInternalModules(mctx android.LoadHookContext) {
Colin Cross79c7c262019-04-17 11:11:46 -0700857
Paul Duffin50061512020-01-21 16:31:05 +0000858 // If the build is configured to use prebuilts then force this to be preferred.
859 if mctx.Config().UnbundledBuildUsePrebuiltSdks() {
860 module.prebuilt.ForcePrefer()
861 }
862
Paul Duffin56d44902020-01-31 13:36:25 +0000863 for apiScope, scopeProperties := range module.scopeProperties() {
864 if len(scopeProperties.Jars) == 0 {
865 continue
866 }
867
868 // Creates a java import for the jar with ".stubs" suffix
869 props := struct {
870 Name *string
871 Soc_specific *bool
872 Device_specific *bool
873 Product_specific *bool
874 System_ext_specific *bool
875 Sdk_version *string
876 Libs []string
877 Jars []string
Paul Duffin50061512020-01-21 16:31:05 +0000878 Prefer *bool
Paul Duffin56d44902020-01-31 13:36:25 +0000879 }{}
880
881 props.Name = proptools.StringPtr(apiScope.stubsModuleName(module.BaseModuleName()))
882 props.Sdk_version = scopeProperties.Sdk_version
Paul Duffinfcfd7912020-01-31 17:54:30 +0000883 // Prepend any of the libs from the legacy public properties to the libs for each of the
884 // scopes to avoid having to duplicate them in each scope.
885 props.Libs = append(module.properties.Libs, scopeProperties.Libs...)
Paul Duffin56d44902020-01-31 13:36:25 +0000886 props.Jars = scopeProperties.Jars
887
888 if module.SocSpecific() {
889 props.Soc_specific = proptools.BoolPtr(true)
890 } else if module.DeviceSpecific() {
891 props.Device_specific = proptools.BoolPtr(true)
892 } else if module.ProductSpecific() {
893 props.Product_specific = proptools.BoolPtr(true)
894 } else if module.SystemExtSpecific() {
895 props.System_ext_specific = proptools.BoolPtr(true)
896 }
897
Paul Duffin50061512020-01-21 16:31:05 +0000898 // If the build should use prebuilt sdks then set prefer to true on the stubs library.
899 // That will cause the prebuilt version of the stubs to override the source version.
900 if mctx.Config().UnbundledBuildUsePrebuiltSdks() {
901 props.Prefer = proptools.BoolPtr(true)
902 }
903
Paul Duffin56d44902020-01-31 13:36:25 +0000904 mctx.CreateModule(ImportFactory, &props)
905 }
Colin Cross79c7c262019-04-17 11:11:46 -0700906
907 javaSdkLibraries := javaSdkLibraries(mctx.Config())
908 javaSdkLibrariesLock.Lock()
909 defer javaSdkLibrariesLock.Unlock()
910 *javaSdkLibraries = append(*javaSdkLibraries, module.BaseModuleName())
911}
912
Paul Duffin56d44902020-01-31 13:36:25 +0000913func (module *sdkLibraryImport) scopeProperties() map[*apiScope]*sdkLibraryScopeProperties {
914 p := make(map[*apiScope]*sdkLibraryScopeProperties)
915 p[apiScopePublic] = &module.properties.Public
916 p[apiScopeSystem] = &module.properties.System
917 p[apiScopeTest] = &module.properties.Test
918 return p
919}
920
Colin Cross79c7c262019-04-17 11:11:46 -0700921func (module *sdkLibraryImport) DepsMutator(ctx android.BottomUpMutatorContext) {
Paul Duffin56d44902020-01-31 13:36:25 +0000922 for apiScope, scopeProperties := range module.scopeProperties() {
923 if len(scopeProperties.Jars) == 0 {
924 continue
925 }
926
927 // Add dependencies to the prebuilt stubs library
928 ctx.AddVariationDependencies(nil, apiScope.stubsTag, apiScope.stubsModuleName(module.BaseModuleName()))
929 }
Colin Cross79c7c262019-04-17 11:11:46 -0700930}
931
932func (module *sdkLibraryImport) GenerateAndroidBuildActions(ctx android.ModuleContext) {
933 // Record the paths to the prebuilt stubs library.
934 ctx.VisitDirectDeps(func(to android.Module) {
935 tag := ctx.OtherModuleDependencyTag(to)
936
Paul Duffin56d44902020-01-31 13:36:25 +0000937 if lib, ok := to.(Dependency); ok {
938 if scopeTag, ok := tag.(scopeDependencyTag); ok {
939 apiScope := scopeTag.apiScope
940 scopePaths := module.getScopePaths(apiScope)
941 scopePaths.stubsHeaderPath = lib.HeaderJars()
942 }
Colin Cross79c7c262019-04-17 11:11:46 -0700943 }
944 })
945}
946
Paul Duffin56d44902020-01-31 13:36:25 +0000947func (module *sdkLibraryImport) sdkJars(
948 ctx android.BaseModuleContext,
949 sdkVersion sdkSpec) android.Paths {
950
Paul Duffin50061512020-01-21 16:31:05 +0000951 // If a specific numeric version has been requested then use prebuilt versions of the sdk.
952 if sdkVersion.version.isNumbered() {
953 return PrebuiltJars(ctx, module.BaseModuleName(), sdkVersion)
954 }
955
Paul Duffin56d44902020-01-31 13:36:25 +0000956 var apiScope *apiScope
957 switch sdkVersion.kind {
958 case sdkSystem:
959 apiScope = apiScopeSystem
960 case sdkTest:
961 apiScope = apiScopeTest
962 default:
963 apiScope = apiScopePublic
964 }
965
966 paths := module.getScopePaths(apiScope)
967 return paths.stubsHeaderPath
968}
969
Colin Cross79c7c262019-04-17 11:11:46 -0700970// to satisfy SdkLibraryDependency interface
Jiyong Park6a927c42020-01-21 02:03:43 +0900971func (module *sdkLibraryImport) SdkHeaderJars(ctx android.BaseModuleContext, sdkVersion sdkSpec) android.Paths {
Colin Cross79c7c262019-04-17 11:11:46 -0700972 // This module is just a wrapper for the prebuilt stubs.
Paul Duffin56d44902020-01-31 13:36:25 +0000973 return module.sdkJars(ctx, sdkVersion)
Colin Cross79c7c262019-04-17 11:11:46 -0700974}
975
976// to satisfy SdkLibraryDependency interface
Jiyong Park6a927c42020-01-21 02:03:43 +0900977func (module *sdkLibraryImport) SdkImplementationJars(ctx android.BaseModuleContext, sdkVersion sdkSpec) android.Paths {
Colin Cross79c7c262019-04-17 11:11:46 -0700978 // This module is just a wrapper for the stubs.
Paul Duffin56d44902020-01-31 13:36:25 +0000979 return module.sdkJars(ctx, sdkVersion)
Colin Cross79c7c262019-04-17 11:11:46 -0700980}
Jiyong Parke3833882020-02-17 17:28:10 +0900981
982//
983// java_sdk_library_xml
984//
985type sdkLibraryXml struct {
986 android.ModuleBase
987 android.DefaultableModuleBase
988 android.ApexModuleBase
989
990 properties sdkLibraryXmlProperties
991
992 outputFilePath android.OutputPath
993 installDirPath android.InstallPath
994}
995
996type sdkLibraryXmlProperties struct {
997 // canonical name of the lib
998 Lib_name *string
999}
1000
1001// java_sdk_library_xml builds the permission xml file for a java_sdk_library.
1002// Not to be used directly by users. java_sdk_library internally uses this.
1003func sdkLibraryXmlFactory() android.Module {
1004 module := &sdkLibraryXml{}
1005
1006 module.AddProperties(&module.properties)
1007
1008 android.InitApexModule(module)
1009 android.InitAndroidArchModule(module, android.DeviceSupported, android.MultilibCommon)
1010
1011 return module
1012}
1013
1014// from android.PrebuiltEtcModule
1015func (module *sdkLibraryXml) SubDir() string {
1016 return "permissions"
1017}
1018
1019// from android.PrebuiltEtcModule
1020func (module *sdkLibraryXml) OutputFile() android.OutputPath {
1021 return module.outputFilePath
1022}
1023
1024// from android.ApexModule
1025func (module *sdkLibraryXml) AvailableFor(what string) bool {
1026 return true
1027}
1028
1029func (module *sdkLibraryXml) DepsMutator(ctx android.BottomUpMutatorContext) {
1030 // do nothing
1031}
1032
1033// File path to the runtime implementation library
1034func (module *sdkLibraryXml) implPath() string {
1035 implName := proptools.String(module.properties.Lib_name)
1036 if apexName := module.ApexName(); apexName != "" {
1037 // TODO(b/146468504): ApexName() is only a soong module name, not apex name.
1038 // In most cases, this works fine. But when apex_name is set or override_apex is used
1039 // this can be wrong.
1040 return fmt.Sprintf("/apex/%s/javalib/%s.jar", apexName, implName)
1041 }
1042 partition := "system"
1043 if module.SocSpecific() {
1044 partition = "vendor"
1045 } else if module.DeviceSpecific() {
1046 partition = "odm"
1047 } else if module.ProductSpecific() {
1048 partition = "product"
1049 } else if module.SystemExtSpecific() {
1050 partition = "system_ext"
1051 }
1052 return "/" + partition + "/framework/" + implName + ".jar"
1053}
1054
1055func (module *sdkLibraryXml) GenerateAndroidBuildActions(ctx android.ModuleContext) {
1056 libName := proptools.String(module.properties.Lib_name)
1057 xmlContent := fmt.Sprintf(permissionsTemplate, libName, module.implPath())
1058
1059 module.outputFilePath = android.PathForModuleOut(ctx, libName+".xml").OutputPath
1060 rule := android.NewRuleBuilder()
1061 rule.Command().
1062 Text("/bin/bash -c \"echo -e '" + xmlContent + "'\" > ").
1063 Output(module.outputFilePath)
1064
1065 rule.Build(pctx, ctx, "java_sdk_xml", "Permission XML")
1066
1067 module.installDirPath = android.PathForModuleInstall(ctx, "etc", module.SubDir())
1068}
1069
1070func (module *sdkLibraryXml) AndroidMkEntries() []android.AndroidMkEntries {
1071 if !module.IsForPlatform() {
1072 return []android.AndroidMkEntries{android.AndroidMkEntries{
1073 Disabled: true,
1074 }}
1075 }
1076
1077 return []android.AndroidMkEntries{android.AndroidMkEntries{
1078 Class: "ETC",
1079 OutputFile: android.OptionalPathForPath(module.outputFilePath),
1080 ExtraEntries: []android.AndroidMkExtraEntriesFunc{
1081 func(entries *android.AndroidMkEntries) {
1082 entries.SetString("LOCAL_MODULE_TAGS", "optional")
1083 entries.SetString("LOCAL_MODULE_PATH", module.installDirPath.ToMakePath().String())
1084 entries.SetString("LOCAL_INSTALLED_MODULE_STEM", module.outputFilePath.Base())
1085 },
1086 },
1087 }}
1088}