blob: 0ef0f23a1a1b31b261b76aad5a17d02837004cfb [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"
Jiyong Parkc678ad32018-04-10 13:07:10 +090036 sdkDocsSuffix = ".docs"
Jiyong Parkc678ad32018-04-10 13:07:10 +090037 sdkXmlFileSuffix = ".xml"
Jooyung Han624058e2019-12-24 18:38:06 +090038 permissionsTemplate = `<?xml version="1.0" encoding="utf-8"?>\n` +
39 `<!-- Copyright (C) 2018 The Android Open Source Project\n` +
40 `\n` +
41 ` Licensed under the Apache License, Version 2.0 (the "License");\n` +
42 ` 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` +
48 ` distributed under the License is distributed on an "AS IS" BASIS,\n` +
49 ` 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` +
54 ` <library name="%s" file="%s"/>\n` +
55 `</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 {
111 return baseName + sdkDocsSuffix + scope.moduleSuffix
112}
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
Inseob Kimc0907f12019-02-08 21:00:45 +0900228type SdkLibrary struct {
Sundong Ahn054b19a2018-10-19 13:46:09 +0900229 Library
Jiyong Parkc678ad32018-04-10 13:07:10 +0900230
Sundong Ahn054b19a2018-10-19 13:46:09 +0900231 sdkLibraryProperties sdkLibraryProperties
Jiyong Parkc678ad32018-04-10 13:07:10 +0900232
Paul Duffind1b3a922020-01-22 11:57:20 +0000233 scopePaths map[*apiScope]*scopePaths
Jooyung Han58f26ab2019-12-18 15:34:32 +0900234
Jooyung Han624058e2019-12-24 18:38:06 +0900235 permissionsFile android.Path
Jiyong Parkc678ad32018-04-10 13:07:10 +0900236}
237
Inseob Kimc0907f12019-02-08 21:00:45 +0900238var _ Dependency = (*SdkLibrary)(nil)
239var _ SdkLibraryDependency = (*SdkLibrary)(nil)
Colin Cross897d2ed2019-02-11 14:03:51 -0800240
Paul Duffind1b3a922020-01-22 11:57:20 +0000241func (module *SdkLibrary) getActiveApiScopes() apiScopes {
242 if module.sdkLibraryProperties.Has_system_and_test_apis {
243 return allApiScopes
244 } else {
245 return apiScopes{apiScopePublic}
246 }
247}
248
249func (module *SdkLibrary) getScopePaths(scope *apiScope) *scopePaths {
250 if module.scopePaths == nil {
251 module.scopePaths = make(map[*apiScope]*scopePaths)
252 }
253 paths := module.scopePaths[scope]
254 if paths == nil {
255 paths = &scopePaths{}
256 module.scopePaths[scope] = paths
257 }
258
259 return paths
260}
261
Inseob Kimc0907f12019-02-08 21:00:45 +0900262func (module *SdkLibrary) DepsMutator(ctx android.BottomUpMutatorContext) {
Jiyong Parke3ef3c82019-07-15 15:31:16 +0900263 useBuiltStubs := !ctx.Config().UnbundledBuildUsePrebuiltSdks()
Paul Duffind1b3a922020-01-22 11:57:20 +0000264 for _, apiScope := range module.getActiveApiScopes() {
265 // Add dependencies to the stubs library
Jiyong Parke3ef3c82019-07-15 15:31:16 +0900266 if useBuiltStubs {
Paul Duffind1b3a922020-01-22 11:57:20 +0000267 ctx.AddVariationDependencies(nil, apiScope.stubsTag, module.stubsName(apiScope))
Jiyong Parke3ef3c82019-07-15 15:31:16 +0900268 }
Paul Duffind1b3a922020-01-22 11:57:20 +0000269
270 ctx.AddVariationDependencies(nil, apiScope.apiFileTag, module.docsName(apiScope))
Sundong Ahn054b19a2018-10-19 13:46:09 +0900271 }
272
273 module.Library.deps(ctx)
Jiyong Parkc678ad32018-04-10 13:07:10 +0900274}
275
Inseob Kimc0907f12019-02-08 21:00:45 +0900276func (module *SdkLibrary) GenerateAndroidBuildActions(ctx android.ModuleContext) {
Paul Duffin43db9be2019-12-30 17:35:49 +0000277 // Don't build an implementation library if this is api only.
278 if !proptools.Bool(module.sdkLibraryProperties.Api_only) {
279 module.Library.GenerateAndroidBuildActions(ctx)
280 }
Sundong Ahn054b19a2018-10-19 13:46:09 +0900281
Jooyung Han624058e2019-12-24 18:38:06 +0900282 module.buildPermissionsFile(ctx)
Jooyung Han58f26ab2019-12-18 15:34:32 +0900283
Sundong Ahn57368eb2018-07-06 11:20:23 +0900284 // Record the paths to the header jars of the library (stubs and impl).
Paul Duffind1b3a922020-01-22 11:57:20 +0000285 // When this java_sdk_library is depended upon from others via "libs" property,
Jiyong Parkc678ad32018-04-10 13:07:10 +0900286 // the recorded paths will be returned depending on the link type of the caller.
287 ctx.VisitDirectDeps(func(to android.Module) {
288 otherName := ctx.OtherModuleName(to)
289 tag := ctx.OtherModuleDependencyTag(to)
290
Sundong Ahn57368eb2018-07-06 11:20:23 +0900291 if lib, ok := to.(Dependency); ok {
Paul Duffind1b3a922020-01-22 11:57:20 +0000292 if scopeTag, ok := tag.(scopeDependencyTag); ok {
293 apiScope := scopeTag.apiScope
294 scopePaths := module.getScopePaths(apiScope)
295 scopePaths.stubsHeaderPath = lib.HeaderJars()
296 scopePaths.stubsImplPath = lib.ImplementationJars()
Jiyong Parkc678ad32018-04-10 13:07:10 +0900297 }
298 }
Sundong Ahn20e998b2018-07-24 11:19:26 +0900299 if doc, ok := to.(ApiFilePath); ok {
Paul Duffind1b3a922020-01-22 11:57:20 +0000300 if scopeTag, ok := tag.(scopeDependencyTag); ok {
301 apiScope := scopeTag.apiScope
302 scopePaths := module.getScopePaths(apiScope)
303 scopePaths.apiFilePath = doc.ApiFilePath()
304 } else {
Sundong Ahn20e998b2018-07-24 11:19:26 +0900305 ctx.ModuleErrorf("depends on module %q of unknown tag %q", otherName, tag)
306 }
307 }
Jiyong Parkc678ad32018-04-10 13:07:10 +0900308 })
309}
310
Jooyung Han624058e2019-12-24 18:38:06 +0900311func (module *SdkLibrary) buildPermissionsFile(ctx android.ModuleContext) {
312 xmlContent := fmt.Sprintf(permissionsTemplate, module.BaseModuleName(), module.implPath())
313 permissionsFile := android.PathForModuleOut(ctx, module.xmlFileName())
Jooyung Han58f26ab2019-12-18 15:34:32 +0900314
Jooyung Han624058e2019-12-24 18:38:06 +0900315 ctx.Build(pctx, android.BuildParams{
316 Rule: android.WriteFile,
317 Output: permissionsFile,
318 Description: "Generating " + module.BaseModuleName() + " permissions",
319 Args: map[string]string{
320 "content": xmlContent,
321 },
322 })
Jooyung Han58f26ab2019-12-18 15:34:32 +0900323
Jooyung Han624058e2019-12-24 18:38:06 +0900324 module.permissionsFile = permissionsFile
Jooyung Han58f26ab2019-12-18 15:34:32 +0900325}
326
Jooyung Han624058e2019-12-24 18:38:06 +0900327func (module *SdkLibrary) OutputFiles(tag string) (android.Paths, error) {
328 switch tag {
329 case ".xml":
330 return android.Paths{module.permissionsFile}, nil
331 }
332 return module.Library.OutputFiles(tag)
Jooyung Han58f26ab2019-12-18 15:34:32 +0900333}
334
Jiyong Park0b0e1b92019-12-03 13:24:29 +0900335func (module *SdkLibrary) AndroidMkEntries() []android.AndroidMkEntries {
Paul Duffin43db9be2019-12-30 17:35:49 +0000336 if proptools.Bool(module.sdkLibraryProperties.Api_only) {
337 return nil
338 }
Jiyong Park0b0e1b92019-12-03 13:24:29 +0900339 entriesList := module.Library.AndroidMkEntries()
340 entries := &entriesList[0]
Jaewoong Jungb0c127c2019-08-29 14:56:03 -0700341 entries.Required = append(entries.Required, module.xmlFileName())
Sundong Ahn054b19a2018-10-19 13:46:09 +0900342
Jaewoong Jungb0c127c2019-08-29 14:56:03 -0700343 entries.ExtraFooters = []android.AndroidMkExtraFootersFunc{
344 func(w io.Writer, name, prefix, moduleDir string, entries *android.AndroidMkEntries) {
Jaewoong Jungb0c127c2019-08-29 14:56:03 -0700345 if !Bool(module.sdkLibraryProperties.No_dist) {
346 // Create a phony module that installs the impl library, for the case when this lib is
347 // in PRODUCT_PACKAGES.
348 owner := module.ModuleBase.Owner()
349 if owner == "" {
350 if Bool(module.sdkLibraryProperties.Core_lib) {
351 owner = "core"
352 } else {
353 owner = "android"
354 }
355 }
Paul Duffind1b3a922020-01-22 11:57:20 +0000356
357 // Create dist rules to install the stubs libs and api files to the dist dir
358 for _, apiScope := range module.getActiveApiScopes() {
359 if scopePaths, ok := module.scopePaths[apiScope]; ok {
360 if len(scopePaths.stubsHeaderPath) == 1 {
361 fmt.Fprintln(w, "$(call dist-for-goals,sdk win_sdk,"+
362 scopePaths.stubsImplPath.Strings()[0]+
363 ":"+path.Join("apistubs", owner, apiScope.name,
364 module.BaseModuleName()+".jar")+")")
365 }
366 if scopePaths.apiFilePath != nil {
367 fmt.Fprintln(w, "$(call dist-for-goals,sdk win_sdk,"+
368 scopePaths.apiFilePath.String()+
369 ":"+path.Join("apistubs", owner, apiScope.name, "api",
370 module.BaseModuleName()+".txt")+")")
371 }
372 }
Sundong Ahn80a87b32019-05-13 15:02:50 +0900373 }
Sundong Ahn4fd04bb2018-08-31 18:01:37 +0900374 }
Jaewoong Jungb0c127c2019-08-29 14:56:03 -0700375 },
Jiyong Park82484c02018-04-23 21:41:26 +0900376 }
Jiyong Park0b0e1b92019-12-03 13:24:29 +0900377 return entriesList
Jiyong Park82484c02018-04-23 21:41:26 +0900378}
379
Jiyong Parkc678ad32018-04-10 13:07:10 +0900380// Module name of the stubs library
Paul Duffind1b3a922020-01-22 11:57:20 +0000381func (module *SdkLibrary) stubsName(apiScope *apiScope) string {
382 return apiScope.stubsModuleName(module.BaseModuleName())
Jiyong Parkc678ad32018-04-10 13:07:10 +0900383}
384
385// Module name of the docs
Paul Duffind1b3a922020-01-22 11:57:20 +0000386func (module *SdkLibrary) docsName(apiScope *apiScope) string {
387 return apiScope.docsModuleName(module.BaseModuleName())
Jiyong Parkc678ad32018-04-10 13:07:10 +0900388}
389
390// Module name of the runtime implementation library
Inseob Kimc0907f12019-02-08 21:00:45 +0900391func (module *SdkLibrary) implName() string {
Sundong Ahn054b19a2018-10-19 13:46:09 +0900392 return module.BaseModuleName()
Jiyong Parkc678ad32018-04-10 13:07:10 +0900393}
394
395// File path to the runtime implementation library
Inseob Kimc0907f12019-02-08 21:00:45 +0900396func (module *SdkLibrary) implPath() string {
Jooyung Han58f26ab2019-12-18 15:34:32 +0900397 if apexName := module.ApexName(); apexName != "" {
398 // TODO(b/146468504): ApexName() is only a soong module name, not apex name.
399 // In most cases, this works fine. But when apex_name is set or override_apex is used
400 // this can be wrong.
401 return fmt.Sprintf("/apex/%s/javalib/%s.jar", apexName, module.implName())
402 }
Jiyong Parkc678ad32018-04-10 13:07:10 +0900403 partition := "system"
404 if module.SocSpecific() {
405 partition = "vendor"
406 } else if module.DeviceSpecific() {
407 partition = "odm"
408 } else if module.ProductSpecific() {
409 partition = "product"
Sundong Ahn0d7dff42019-12-04 12:53:44 +0900410 } else if module.SystemExtSpecific() {
411 partition = "system_ext"
Jiyong Parkc678ad32018-04-10 13:07:10 +0900412 }
413 return "/" + partition + "/framework/" + module.implName() + ".jar"
414}
415
416// Module name of the XML file for the lib
Inseob Kimc0907f12019-02-08 21:00:45 +0900417func (module *SdkLibrary) xmlFileName() string {
Jiyong Parkc678ad32018-04-10 13:07:10 +0900418 return module.BaseModuleName() + sdkXmlFileSuffix
419}
420
Paul Duffin12ceb462019-12-24 20:31:31 +0000421// Get the sdk version for use when compiling the stubs library.
Paul Duffind1b3a922020-01-22 11:57:20 +0000422func (module *SdkLibrary) sdkVersionForStubsLibrary(mctx android.LoadHookContext, apiScope *apiScope) string {
Paul Duffin12ceb462019-12-24 20:31:31 +0000423 sdkDep := decodeSdkDep(mctx, sdkContext(&module.Library))
424 if sdkDep.hasStandardLibs() {
425 // If building against a standard sdk then use the sdk version appropriate for the scope.
Paul Duffind1b3a922020-01-22 11:57:20 +0000426 return apiScope.sdkVersion
Paul Duffin12ceb462019-12-24 20:31:31 +0000427 } else {
428 // Otherwise, use no system module.
429 return "none"
430 }
431}
432
Jiyong Parkc678ad32018-04-10 13:07:10 +0900433// $(INTERNAL_PLATFORM_<apiTagName>_API_FILE) points to the generated
434// api file for the current source
435// TODO: remove this when apicheck is done in soong
Paul Duffind1b3a922020-01-22 11:57:20 +0000436func (module *SdkLibrary) apiTagName(apiScope *apiScope) string {
437 return strings.Replace(strings.ToUpper(module.BaseModuleName()), ".", "_", -1) + apiScope.apiFileMakeVariableSuffix
Jiyong Parkc678ad32018-04-10 13:07:10 +0900438}
439
Paul Duffind1b3a922020-01-22 11:57:20 +0000440func (module *SdkLibrary) latestApiFilegroupName(apiScope *apiScope) string {
441 return ":" + module.BaseModuleName() + ".api." + apiScope.name + ".latest"
Jiyong Park58c518b2018-05-12 22:29:12 +0900442}
Jiyong Parkc678ad32018-04-10 13:07:10 +0900443
Paul Duffind1b3a922020-01-22 11:57:20 +0000444func (module *SdkLibrary) latestRemovedApiFilegroupName(apiScope *apiScope) string {
445 return ":" + module.BaseModuleName() + "-removed.api." + apiScope.name + ".latest"
Jiyong Parkc678ad32018-04-10 13:07:10 +0900446}
447
448// Creates a static java library that has API stubs
Paul Duffind1b3a922020-01-22 11:57:20 +0000449func (module *SdkLibrary) createStubsLibrary(mctx android.LoadHookContext, apiScope *apiScope) {
Jiyong Parkc678ad32018-04-10 13:07:10 +0900450 props := struct {
Sundong Ahn0d7dff42019-12-04 12:53:44 +0900451 Name *string
452 Srcs []string
Paul Duffin367ab912019-12-23 19:40:36 +0000453 Installable *bool
Sundong Ahn0d7dff42019-12-04 12:53:44 +0900454 Sdk_version *string
Paul Duffin12ceb462019-12-24 20:31:31 +0000455 System_modules *string
Sundong Ahn0d7dff42019-12-04 12:53:44 +0900456 Libs []string
457 Soc_specific *bool
458 Device_specific *bool
459 Product_specific *bool
460 System_ext_specific *bool
461 Compile_dex *bool
Sundong Ahn0d7dff42019-12-04 12:53:44 +0900462 Java_version *string
463 Product_variables struct {
Jiyong Parkc678ad32018-04-10 13:07:10 +0900464 Unbundled_build struct {
465 Enabled *bool
466 }
Jiyong Park82484c02018-04-23 21:41:26 +0900467 Pdk struct {
468 Enabled *bool
469 }
Jiyong Parkc678ad32018-04-10 13:07:10 +0900470 }
Sundong Ahn054b19a2018-10-19 13:46:09 +0900471 Openjdk9 struct {
472 Srcs []string
473 Javacflags []string
474 }
Jiyong Parkc678ad32018-04-10 13:07:10 +0900475 }{}
476
Jiyong Parkdf130542018-04-27 16:29:21 +0900477 props.Name = proptools.StringPtr(module.stubsName(apiScope))
Jiyong Parkc678ad32018-04-10 13:07:10 +0900478 // sources are generated from the droiddoc
Jiyong Parkdf130542018-04-27 16:29:21 +0900479 props.Srcs = []string{":" + module.docsName(apiScope)}
Paul Duffin12ceb462019-12-24 20:31:31 +0000480 sdkVersion := module.sdkVersionForStubsLibrary(mctx, apiScope)
Paul Duffin52d398a2019-06-11 12:31:14 +0100481 props.Sdk_version = proptools.StringPtr(sdkVersion)
Paul Duffin12ceb462019-12-24 20:31:31 +0000482 props.System_modules = module.Library.Module.deviceProperties.System_modules
Paul Duffin367ab912019-12-23 19:40:36 +0000483 props.Installable = proptools.BoolPtr(false)
Sundong Ahn054b19a2018-10-19 13:46:09 +0900484 props.Libs = module.sdkLibraryProperties.Stub_only_libs
Jiyong Parkc678ad32018-04-10 13:07:10 +0900485 // Unbundled apps will use the prebult one from /prebuilts/sdk
Colin Cross10932872019-04-18 14:27:12 -0700486 if mctx.Config().UnbundledBuildUsePrebuiltSdks() {
Colin Cross2c77ceb2019-01-21 11:56:21 -0800487 props.Product_variables.Unbundled_build.Enabled = proptools.BoolPtr(false)
488 }
Jiyong Park82484c02018-04-23 21:41:26 +0900489 props.Product_variables.Pdk.Enabled = proptools.BoolPtr(false)
Sundong Ahn054b19a2018-10-19 13:46:09 +0900490 props.Openjdk9.Srcs = module.Library.Module.properties.Openjdk9.Srcs
491 props.Openjdk9.Javacflags = module.Library.Module.properties.Openjdk9.Javacflags
492 props.Java_version = module.Library.Module.properties.Java_version
493 if module.Library.Module.deviceProperties.Compile_dex != nil {
494 props.Compile_dex = module.Library.Module.deviceProperties.Compile_dex
Sundong Ahndd567f92018-07-31 17:19:11 +0900495 }
Jiyong Parkc678ad32018-04-10 13:07:10 +0900496
497 if module.SocSpecific() {
498 props.Soc_specific = proptools.BoolPtr(true)
499 } else if module.DeviceSpecific() {
500 props.Device_specific = proptools.BoolPtr(true)
501 } else if module.ProductSpecific() {
502 props.Product_specific = proptools.BoolPtr(true)
Sundong Ahn0d7dff42019-12-04 12:53:44 +0900503 } else if module.SystemExtSpecific() {
504 props.System_ext_specific = proptools.BoolPtr(true)
Jiyong Parkc678ad32018-04-10 13:07:10 +0900505 }
506
Colin Cross84dfc3d2019-09-25 11:33:01 -0700507 mctx.CreateModule(LibraryFactory, &props)
Jiyong Parkc678ad32018-04-10 13:07:10 +0900508}
509
510// Creates a droiddoc module that creates stubs source files from the given full source
511// files
Paul Duffind1b3a922020-01-22 11:57:20 +0000512func (module *SdkLibrary) createStubsSources(mctx android.LoadHookContext, apiScope *apiScope) {
Jiyong Parkc678ad32018-04-10 13:07:10 +0900513 props := struct {
Sundong Ahn054b19a2018-10-19 13:46:09 +0900514 Name *string
515 Srcs []string
516 Installable *bool
Paul Duffin52d398a2019-06-11 12:31:14 +0100517 Sdk_version *string
Paul Duffin12ceb462019-12-24 20:31:31 +0000518 System_modules *string
Sundong Ahn054b19a2018-10-19 13:46:09 +0900519 Libs []string
Paul Duffin11512472019-02-11 15:55:17 +0000520 Arg_files []string
Sundong Ahn054b19a2018-10-19 13:46:09 +0900521 Args *string
522 Api_tag_name *string
523 Api_filename *string
524 Removed_api_filename *string
Sundong Ahn054b19a2018-10-19 13:46:09 +0900525 Java_version *string
526 Merge_annotations_dirs []string
527 Merge_inclusion_annotations_dirs []string
528 Check_api struct {
Inseob Kim38449af2019-02-28 14:24:05 +0900529 Current ApiToCheck
530 Last_released ApiToCheck
531 Ignore_missing_latest_api *bool
Jiyong Park58c518b2018-05-12 22:29:12 +0900532 }
Sundong Ahn1b92c822018-05-29 11:35:17 +0900533 Aidl struct {
534 Include_dirs []string
535 Local_include_dirs []string
536 }
Jiyong Parkc678ad32018-04-10 13:07:10 +0900537 }{}
538
Paul Duffin250e6192019-06-07 10:44:37 +0100539 sdkDep := decodeSdkDep(mctx, sdkContext(&module.Library))
Paul Duffin12ceb462019-12-24 20:31:31 +0000540 // Use the platform API if standard libraries were requested, otherwise use
541 // no default libraries.
Paul Duffin52d398a2019-06-11 12:31:14 +0100542 sdkVersion := ""
543 if !sdkDep.hasStandardLibs() {
544 sdkVersion = "none"
545 }
Paul Duffin250e6192019-06-07 10:44:37 +0100546
Jiyong Parkdf130542018-04-27 16:29:21 +0900547 props.Name = proptools.StringPtr(module.docsName(apiScope))
Sundong Ahn054b19a2018-10-19 13:46:09 +0900548 props.Srcs = append(props.Srcs, module.Library.Module.properties.Srcs...)
Paul Duffin52d398a2019-06-11 12:31:14 +0100549 props.Sdk_version = proptools.StringPtr(sdkVersion)
Paul Duffin12ceb462019-12-24 20:31:31 +0000550 props.System_modules = module.Library.Module.deviceProperties.System_modules
Jiyong Parkc678ad32018-04-10 13:07:10 +0900551 props.Installable = proptools.BoolPtr(false)
Sundong Ahne6f0b052018-06-05 16:46:14 +0900552 // A droiddoc module has only one Libs property and doesn't distinguish between
553 // shared libs and static libs. So we need to add both of these libs to Libs property.
Sundong Ahn054b19a2018-10-19 13:46:09 +0900554 props.Libs = module.Library.Module.properties.Libs
555 props.Libs = append(props.Libs, module.Library.Module.properties.Static_libs...)
556 props.Aidl.Include_dirs = module.Library.Module.deviceProperties.Aidl.Include_dirs
557 props.Aidl.Local_include_dirs = module.Library.Module.deviceProperties.Aidl.Local_include_dirs
Sundong Ahn054b19a2018-10-19 13:46:09 +0900558 props.Java_version = module.Library.Module.properties.Java_version
Jiyong Parkc678ad32018-04-10 13:07:10 +0900559
Sundong Ahn054b19a2018-10-19 13:46:09 +0900560 props.Merge_annotations_dirs = module.sdkLibraryProperties.Merge_annotations_dirs
561 props.Merge_inclusion_annotations_dirs = module.sdkLibraryProperties.Merge_inclusion_annotations_dirs
562
Paul Duffin235ffff2019-12-24 10:41:30 +0000563 droiddocArgs := []string{}
564 if len(module.sdkLibraryProperties.Api_packages) != 0 {
565 droiddocArgs = append(droiddocArgs, "--stub-packages "+strings.Join(module.sdkLibraryProperties.Api_packages, ":"))
566 }
567 if len(module.sdkLibraryProperties.Hidden_api_packages) != 0 {
568 droiddocArgs = append(droiddocArgs,
569 android.JoinWithPrefix(module.sdkLibraryProperties.Hidden_api_packages, " --hide-package "))
570 }
571 droiddocArgs = append(droiddocArgs, module.sdkLibraryProperties.Droiddoc_options...)
572 disabledWarnings := []string{
573 "MissingPermission",
574 "BroadcastBehavior",
575 "HiddenSuperclass",
576 "DeprecationMismatch",
577 "UnavailableSymbol",
578 "SdkConstant",
579 "HiddenTypeParameter",
580 "Todo",
581 "Typo",
582 }
583 droiddocArgs = append(droiddocArgs, android.JoinWithPrefix(disabledWarnings, "--hide "))
Sundong Ahnfb2721f2018-09-17 13:23:09 +0900584
Jiyong Parkdf130542018-04-27 16:29:21 +0900585 switch apiScope {
586 case apiScopeSystem:
Paul Duffin235ffff2019-12-24 10:41:30 +0000587 droiddocArgs = append(droiddocArgs, "-showAnnotation android.annotation.SystemApi")
Jiyong Parkdf130542018-04-27 16:29:21 +0900588 case apiScopeTest:
Paul Duffin235ffff2019-12-24 10:41:30 +0000589 droiddocArgs = append(droiddocArgs, " -showAnnotation android.annotation.TestApi")
Jiyong Parkc678ad32018-04-10 13:07:10 +0900590 }
Paul Duffin11512472019-02-11 15:55:17 +0000591 props.Arg_files = module.sdkLibraryProperties.Droiddoc_option_files
Paul Duffin235ffff2019-12-24 10:41:30 +0000592 props.Args = proptools.StringPtr(strings.Join(droiddocArgs, " "))
Jiyong Parkc678ad32018-04-10 13:07:10 +0900593
594 // List of APIs identified from the provided source files are created. They are later
595 // compared against to the not-yet-released (a.k.a current) list of APIs and to the
596 // last-released (a.k.a numbered) list of API.
Paul Duffind1b3a922020-01-22 11:57:20 +0000597 currentApiFileName := apiScope.apiFilePrefix + "current.txt"
598 removedApiFileName := apiScope.apiFilePrefix + "removed.txt"
Paul Duffin749f98f2019-12-30 17:23:46 +0000599 apiDir := module.getApiDir()
600 currentApiFileName = path.Join(apiDir, currentApiFileName)
601 removedApiFileName = path.Join(apiDir, removedApiFileName)
Jiyong Park58c518b2018-05-12 22:29:12 +0900602 // TODO(jiyong): remove these three props
Jiyong Parkdf130542018-04-27 16:29:21 +0900603 props.Api_tag_name = proptools.StringPtr(module.apiTagName(apiScope))
Jiyong Parkc678ad32018-04-10 13:07:10 +0900604 props.Api_filename = proptools.StringPtr(currentApiFileName)
605 props.Removed_api_filename = proptools.StringPtr(removedApiFileName)
606
Jiyong Park58c518b2018-05-12 22:29:12 +0900607 // check against the not-yet-release API
608 props.Check_api.Current.Api_file = proptools.StringPtr(currentApiFileName)
609 props.Check_api.Current.Removed_api_file = proptools.StringPtr(removedApiFileName)
Jiyong Park58c518b2018-05-12 22:29:12 +0900610
611 // check against the latest released API
612 props.Check_api.Last_released.Api_file = proptools.StringPtr(
613 module.latestApiFilegroupName(apiScope))
614 props.Check_api.Last_released.Removed_api_file = proptools.StringPtr(
615 module.latestRemovedApiFilegroupName(apiScope))
Inseob Kim38449af2019-02-28 14:24:05 +0900616 props.Check_api.Ignore_missing_latest_api = proptools.BoolPtr(true)
Jiyong Park58c518b2018-05-12 22:29:12 +0900617
Colin Cross84dfc3d2019-09-25 11:33:01 -0700618 mctx.CreateModule(DroidstubsFactory, &props)
Jiyong Parkc678ad32018-04-10 13:07:10 +0900619}
620
Jiyong Parkc678ad32018-04-10 13:07:10 +0900621// Creates the xml file that publicizes the runtime library
Colin Crossf8b860a2019-04-16 14:43:28 -0700622func (module *SdkLibrary) createXmlFile(mctx android.LoadHookContext) {
Jiyong Parkc678ad32018-04-10 13:07:10 +0900623 // creates a prebuilt_etc module to actually place the xml file under
624 // <partition>/etc/permissions
625 etcProps := struct {
Sundong Ahn0d7dff42019-12-04 12:53:44 +0900626 Name *string
627 Src *string
628 Sub_dir *string
629 Soc_specific *bool
630 Device_specific *bool
631 Product_specific *bool
632 System_ext_specific *bool
Jiyong Parkc678ad32018-04-10 13:07:10 +0900633 }{}
634 etcProps.Name = proptools.StringPtr(module.xmlFileName())
Jooyung Han624058e2019-12-24 18:38:06 +0900635 etcProps.Src = proptools.StringPtr(":" + module.BaseModuleName() + "{.xml}")
Jiyong Parkc678ad32018-04-10 13:07:10 +0900636 etcProps.Sub_dir = proptools.StringPtr("permissions")
637 if module.SocSpecific() {
638 etcProps.Soc_specific = proptools.BoolPtr(true)
639 } else if module.DeviceSpecific() {
640 etcProps.Device_specific = proptools.BoolPtr(true)
641 } else if module.ProductSpecific() {
642 etcProps.Product_specific = proptools.BoolPtr(true)
Sundong Ahn0d7dff42019-12-04 12:53:44 +0900643 } else if module.SystemExtSpecific() {
644 etcProps.System_ext_specific = proptools.BoolPtr(true)
Jiyong Parkc678ad32018-04-10 13:07:10 +0900645 }
Colin Cross84dfc3d2019-09-25 11:33:01 -0700646 mctx.CreateModule(android.PrebuiltEtcFactory, &etcProps)
Jiyong Parkc678ad32018-04-10 13:07:10 +0900647}
648
Jiyong Park6a927c42020-01-21 02:03:43 +0900649func (module *SdkLibrary) PrebuiltJars(ctx android.BaseModuleContext, s sdkSpec) android.Paths {
650 var ver sdkVersion
651 var kind sdkKind
652 if s.usePrebuilt(ctx) {
653 ver = s.version
654 kind = s.kind
Jiyong Parkc678ad32018-04-10 13:07:10 +0900655 } else {
Jiyong Park6a927c42020-01-21 02:03:43 +0900656 // We don't have prebuilt SDK for the specific sdkVersion.
657 // Instead of breaking the build, fallback to use "system_current"
658 ver = sdkVersionCurrent
659 kind = sdkSystem
Sundong Ahn054b19a2018-10-19 13:46:09 +0900660 }
Jiyong Park6a927c42020-01-21 02:03:43 +0900661
662 dir := filepath.Join("prebuilts", "sdk", ver.String(), kind.String())
Sundong Ahn054b19a2018-10-19 13:46:09 +0900663 jar := filepath.Join(dir, module.BaseModuleName()+".jar")
664 jarPath := android.ExistentPathForSource(ctx, jar)
Sundong Ahnae418ac2019-02-28 15:01:28 +0900665 if !jarPath.Valid() {
Colin Cross07c88562020-01-07 09:34:44 -0800666 if ctx.Config().AllowMissingDependencies() {
667 return android.Paths{android.PathForSource(ctx, jar)}
668 } else {
Jiyong Park6a927c42020-01-21 02:03:43 +0900669 ctx.PropertyErrorf("sdk_library", "invalid sdk version %q, %q does not exist", s.raw, jar)
Colin Cross07c88562020-01-07 09:34:44 -0800670 }
Sundong Ahnae418ac2019-02-28 15:01:28 +0900671 return nil
672 }
Sundong Ahn054b19a2018-10-19 13:46:09 +0900673 return android.Paths{jarPath.Path()}
674}
675
Paul Duffind1b3a922020-01-22 11:57:20 +0000676func (module *SdkLibrary) sdkJars(
677 ctx android.BaseModuleContext,
678 sdkVersion sdkSpec,
679 headerJars bool) android.Paths {
680
Sundong Ahn054b19a2018-10-19 13:46:09 +0900681 // This module is just a wrapper for the stubs.
Colin Cross10932872019-04-18 14:27:12 -0700682 if ctx.Config().UnbundledBuildUsePrebuiltSdks() {
Sundong Ahn054b19a2018-10-19 13:46:09 +0900683 return module.PrebuiltJars(ctx, sdkVersion)
684 } else {
Paul Duffind1b3a922020-01-22 11:57:20 +0000685 if !sdkVersion.specified() {
686 if headerJars {
687 return module.Library.HeaderJars()
688 } else {
689 return module.Library.ImplementationJars()
690 }
691 }
692 var paths *scopePaths
Jiyong Park6a927c42020-01-21 02:03:43 +0900693 switch sdkVersion.kind {
694 case sdkSystem:
Paul Duffind1b3a922020-01-22 11:57:20 +0000695 paths = module.getScopePaths(apiScopeSystem)
Jiyong Park6a927c42020-01-21 02:03:43 +0900696 case sdkPrivate:
Sundong Ahn054b19a2018-10-19 13:46:09 +0900697 return module.Library.HeaderJars()
Jiyong Park6a927c42020-01-21 02:03:43 +0900698 default:
Paul Duffind1b3a922020-01-22 11:57:20 +0000699 paths = module.getScopePaths(apiScopePublic)
700 }
701
702 if headerJars {
703 return paths.stubsHeaderPath
704 } else {
705 return paths.stubsImplPath
Sundong Ahn054b19a2018-10-19 13:46:09 +0900706 }
Jiyong Parkc678ad32018-04-10 13:07:10 +0900707 }
708}
709
Sundong Ahn241cd372018-07-13 16:16:44 +0900710// to satisfy SdkLibraryDependency interface
Paul Duffind1b3a922020-01-22 11:57:20 +0000711func (module *SdkLibrary) SdkHeaderJars(ctx android.BaseModuleContext, sdkVersion sdkSpec) android.Paths {
712 return module.sdkJars(ctx, sdkVersion, true /*headerJars*/)
713}
714
715// to satisfy SdkLibraryDependency interface
Jiyong Park6a927c42020-01-21 02:03:43 +0900716func (module *SdkLibrary) SdkImplementationJars(ctx android.BaseModuleContext, sdkVersion sdkSpec) android.Paths {
Paul Duffind1b3a922020-01-22 11:57:20 +0000717 return module.sdkJars(ctx, sdkVersion, false /*headerJars*/)
Sundong Ahn241cd372018-07-13 16:16:44 +0900718}
719
Sundong Ahn80a87b32019-05-13 15:02:50 +0900720func (module *SdkLibrary) SetNoDist() {
721 module.sdkLibraryProperties.No_dist = proptools.BoolPtr(true)
722}
723
Colin Cross571cccf2019-02-04 11:22:08 -0800724var javaSdkLibrariesKey = android.NewOnceKey("javaSdkLibraries")
725
Jiyong Park82484c02018-04-23 21:41:26 +0900726func javaSdkLibraries(config android.Config) *[]string {
Colin Cross571cccf2019-02-04 11:22:08 -0800727 return config.Once(javaSdkLibrariesKey, func() interface{} {
Jiyong Park82484c02018-04-23 21:41:26 +0900728 return &[]string{}
729 }).(*[]string)
730}
731
Paul Duffin749f98f2019-12-30 17:23:46 +0000732func (module *SdkLibrary) getApiDir() string {
733 return proptools.StringDefault(module.sdkLibraryProperties.Api_dir, "api")
734}
735
Jiyong Parkc678ad32018-04-10 13:07:10 +0900736// For a java_sdk_library module, create internal modules for stubs, docs,
737// runtime libs and xml file. If requested, the stubs and docs are created twice
738// once for public API level and once for system API level
Colin Crossf8b860a2019-04-16 14:43:28 -0700739func (module *SdkLibrary) CreateInternalModules(mctx android.LoadHookContext) {
Inseob Kim6e93ac92019-03-21 17:43:49 +0900740 if len(module.Library.Module.properties.Srcs) == 0 {
Inseob Kimc0907f12019-02-08 21:00:45 +0900741 mctx.PropertyErrorf("srcs", "java_sdk_library must specify srcs")
Jooyung Han58f26ab2019-12-18 15:34:32 +0900742 return
Inseob Kimc0907f12019-02-08 21:00:45 +0900743 }
744
Paul Duffin37e0b772019-12-30 17:20:10 +0000745 // If this builds against standard libraries (i.e. is not part of the core libraries)
746 // then assume it provides both system and test apis. Otherwise, assume it does not and
747 // also assume it does not contribute to the dist build.
748 sdkDep := decodeSdkDep(mctx, sdkContext(&module.Library))
749 hasSystemAndTestApis := sdkDep.hasStandardLibs()
750 module.sdkLibraryProperties.Has_system_and_test_apis = hasSystemAndTestApis
751 module.sdkLibraryProperties.No_dist = proptools.BoolPtr(!hasSystemAndTestApis)
752
Inseob Kim8098faa2019-03-18 10:19:51 +0900753 missing_current_api := false
754
Paul Duffind1b3a922020-01-22 11:57:20 +0000755 activeScopes := module.getActiveApiScopes()
756
Paul Duffin749f98f2019-12-30 17:23:46 +0000757 apiDir := module.getApiDir()
Paul Duffind1b3a922020-01-22 11:57:20 +0000758 for _, scope := range activeScopes {
Inseob Kim8098faa2019-03-18 10:19:51 +0900759 for _, api := range []string{"current.txt", "removed.txt"} {
Paul Duffind1b3a922020-01-22 11:57:20 +0000760 path := path.Join(mctx.ModuleDir(), apiDir, scope.apiFilePrefix+api)
Inseob Kim8098faa2019-03-18 10:19:51 +0900761 p := android.ExistentPathForSource(mctx, path)
762 if !p.Valid() {
763 mctx.ModuleErrorf("Current api file %#v doesn't exist", path)
764 missing_current_api = true
765 }
766 }
767 }
768
769 if missing_current_api {
770 script := "build/soong/scripts/gen-java-current-api-files.sh"
771 p := android.ExistentPathForSource(mctx, script)
772
773 if !p.Valid() {
774 panic(fmt.Sprintf("script file %s doesn't exist", script))
775 }
776
777 mctx.ModuleErrorf("One or more current api files are missing. "+
778 "You can update them by:\n"+
Paul Duffin37e0b772019-12-30 17:20:10 +0000779 "%s %q %s && m update-api",
Paul Duffind1b3a922020-01-22 11:57:20 +0000780 script, filepath.Join(mctx.ModuleDir(), apiDir),
781 strings.Join(activeScopes.Strings(func(s *apiScope) string { return s.apiFilePrefix }), " "))
Inseob Kim8098faa2019-03-18 10:19:51 +0900782 return
783 }
784
Paul Duffind1b3a922020-01-22 11:57:20 +0000785 for _, scope := range activeScopes {
786 module.createStubsLibrary(mctx, scope)
787 module.createStubsSources(mctx, scope)
Inseob Kimc0907f12019-02-08 21:00:45 +0900788 }
789
Paul Duffin43db9be2019-12-30 17:35:49 +0000790 if !proptools.Bool(module.sdkLibraryProperties.Api_only) {
791 // for runtime
792 module.createXmlFile(mctx)
793
794 // record java_sdk_library modules so that they are exported to make
795 javaSdkLibraries := javaSdkLibraries(mctx.Config())
796 javaSdkLibrariesLock.Lock()
797 defer javaSdkLibrariesLock.Unlock()
798 *javaSdkLibraries = append(*javaSdkLibraries, module.BaseModuleName())
799 }
Inseob Kimc0907f12019-02-08 21:00:45 +0900800}
801
802func (module *SdkLibrary) InitSdkLibraryProperties() {
Sundong Ahn054b19a2018-10-19 13:46:09 +0900803 module.AddProperties(
804 &module.sdkLibraryProperties,
805 &module.Library.Module.properties,
806 &module.Library.Module.dexpreoptProperties,
807 &module.Library.Module.deviceProperties,
808 &module.Library.Module.protoProperties,
809 )
810
811 module.Library.Module.properties.Installable = proptools.BoolPtr(true)
812 module.Library.Module.deviceProperties.IsSDKLibrary = true
Inseob Kimc0907f12019-02-08 21:00:45 +0900813}
Sundong Ahn054b19a2018-10-19 13:46:09 +0900814
Jaewoong Jung4f158ee2019-07-11 10:05:35 -0700815// java_sdk_library is a special Java library that provides optional platform APIs to apps.
816// In practice, it can be viewed as a combination of several modules: 1) stubs library that clients
817// are linked against to, 2) droiddoc module that internally generates API stubs source files,
818// 3) the real runtime shared library that implements the APIs, and 4) XML file for adding
819// the runtime lib to the classpath at runtime if requested via <uses-library>.
Inseob Kimc0907f12019-02-08 21:00:45 +0900820func SdkLibraryFactory() android.Module {
821 module := &SdkLibrary{}
822 module.InitSdkLibraryProperties()
Jooyung Han58f26ab2019-12-18 15:34:32 +0900823 android.InitApexModule(module)
Sundong Ahn054b19a2018-10-19 13:46:09 +0900824 InitJavaModule(module, android.HostAndDeviceSupported)
Colin Crossf8b860a2019-04-16 14:43:28 -0700825 android.AddLoadHook(module, func(ctx android.LoadHookContext) { module.CreateInternalModules(ctx) })
Jiyong Parkc678ad32018-04-10 13:07:10 +0900826 return module
827}
Colin Cross79c7c262019-04-17 11:11:46 -0700828
829//
830// SDK library prebuilts
831//
832
833type sdkLibraryImportProperties struct {
834 Jars []string `android:"path"`
835
836 Sdk_version *string
837
Colin Cross79c7c262019-04-17 11:11:46 -0700838 // List of shared java libs that this module has dependencies to
839 Libs []string
Colin Cross79c7c262019-04-17 11:11:46 -0700840}
841
842type sdkLibraryImport struct {
843 android.ModuleBase
844 android.DefaultableModuleBase
845 prebuilt android.Prebuilt
846
847 properties sdkLibraryImportProperties
848
849 stubsPath android.Paths
850}
851
852var _ SdkLibraryDependency = (*sdkLibraryImport)(nil)
853
Jaewoong Jung4f158ee2019-07-11 10:05:35 -0700854// java_sdk_library_import imports a prebuilt java_sdk_library.
Colin Cross79c7c262019-04-17 11:11:46 -0700855func sdkLibraryImportFactory() android.Module {
856 module := &sdkLibraryImport{}
857
858 module.AddProperties(&module.properties)
859
860 android.InitPrebuiltModule(module, &module.properties.Jars)
861 InitJavaModule(module, android.HostAndDeviceSupported)
862
863 android.AddLoadHook(module, func(mctx android.LoadHookContext) { module.createInternalModules(mctx) })
864 return module
865}
866
867func (module *sdkLibraryImport) Prebuilt() *android.Prebuilt {
868 return &module.prebuilt
869}
870
871func (module *sdkLibraryImport) Name() string {
872 return module.prebuilt.Name(module.ModuleBase.Name())
873}
874
875func (module *sdkLibraryImport) createInternalModules(mctx android.LoadHookContext) {
876 // Creates a java import for the jar with ".stubs" suffix
877 props := struct {
Sundong Ahn0d7dff42019-12-04 12:53:44 +0900878 Name *string
879 Soc_specific *bool
880 Device_specific *bool
881 Product_specific *bool
882 System_ext_specific *bool
Colin Cross79c7c262019-04-17 11:11:46 -0700883 }{}
884
885 props.Name = proptools.StringPtr(module.BaseModuleName() + sdkStubsLibrarySuffix)
886
887 if module.SocSpecific() {
888 props.Soc_specific = proptools.BoolPtr(true)
889 } else if module.DeviceSpecific() {
890 props.Device_specific = proptools.BoolPtr(true)
891 } else if module.ProductSpecific() {
892 props.Product_specific = proptools.BoolPtr(true)
Sundong Ahn0d7dff42019-12-04 12:53:44 +0900893 } else if module.SystemExtSpecific() {
894 props.System_ext_specific = proptools.BoolPtr(true)
Colin Cross79c7c262019-04-17 11:11:46 -0700895 }
896
Colin Cross84dfc3d2019-09-25 11:33:01 -0700897 mctx.CreateModule(ImportFactory, &props, &module.properties)
Colin Cross79c7c262019-04-17 11:11:46 -0700898
899 javaSdkLibraries := javaSdkLibraries(mctx.Config())
900 javaSdkLibrariesLock.Lock()
901 defer javaSdkLibrariesLock.Unlock()
902 *javaSdkLibraries = append(*javaSdkLibraries, module.BaseModuleName())
903}
904
905func (module *sdkLibraryImport) DepsMutator(ctx android.BottomUpMutatorContext) {
906 // Add dependencies to the prebuilt stubs library
Paul Duffind1b3a922020-01-22 11:57:20 +0000907 ctx.AddVariationDependencies(nil, apiScopePublic.stubsTag, module.BaseModuleName()+sdkStubsLibrarySuffix)
Colin Cross79c7c262019-04-17 11:11:46 -0700908}
909
910func (module *sdkLibraryImport) GenerateAndroidBuildActions(ctx android.ModuleContext) {
911 // Record the paths to the prebuilt stubs library.
912 ctx.VisitDirectDeps(func(to android.Module) {
913 tag := ctx.OtherModuleDependencyTag(to)
914
915 switch tag {
Paul Duffind1b3a922020-01-22 11:57:20 +0000916 case apiScopePublic.stubsTag:
Colin Cross79c7c262019-04-17 11:11:46 -0700917 module.stubsPath = to.(Dependency).HeaderJars()
918 }
919 })
920}
921
922// to satisfy SdkLibraryDependency interface
Jiyong Park6a927c42020-01-21 02:03:43 +0900923func (module *sdkLibraryImport) SdkHeaderJars(ctx android.BaseModuleContext, sdkVersion sdkSpec) android.Paths {
Colin Cross79c7c262019-04-17 11:11:46 -0700924 // This module is just a wrapper for the prebuilt stubs.
925 return module.stubsPath
926}
927
928// to satisfy SdkLibraryDependency interface
Jiyong Park6a927c42020-01-21 02:03:43 +0900929func (module *sdkLibraryImport) SdkImplementationJars(ctx android.BaseModuleContext, sdkVersion sdkSpec) android.Paths {
Colin Cross79c7c262019-04-17 11:11:46 -0700930 // This module is just a wrapper for the stubs.
931 return module.stubsPath
932}