blob: f1c565fd04326e309a783c01d6f1391e093b66b7 [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 Duffine74ac732020-02-06 13:51:46 +000019 "android/soong/genrule"
Paul Duffind1b3a922020-01-22 11:57:20 +000020
Jiyong Parkc678ad32018-04-10 13:07:10 +090021 "fmt"
Jiyong Park82484c02018-04-23 21:41:26 +090022 "io"
Jiyong Parkc678ad32018-04-10 13:07:10 +090023 "path"
Sundong Ahn054b19a2018-10-19 13:46:09 +090024 "path/filepath"
Jiyong Park82484c02018-04-23 21:41:26 +090025 "sort"
Jiyong Parkc678ad32018-04-10 13:07:10 +090026 "strings"
Jiyong Park82484c02018-04-23 21:41:26 +090027 "sync"
Jiyong Parkc678ad32018-04-10 13:07:10 +090028
Paul Duffind1b3a922020-01-22 11:57:20 +000029 "github.com/google/blueprint"
Jiyong Parkc678ad32018-04-10 13:07:10 +090030 "github.com/google/blueprint/proptools"
31)
32
Jooyung Han58f26ab2019-12-18 15:34:32 +090033const (
Jiyong Parkc678ad32018-04-10 13:07:10 +090034 sdkStubsLibrarySuffix = ".stubs"
35 sdkSystemApiSuffix = ".system"
Jiyong Parkdf130542018-04-27 16:29:21 +090036 sdkTestApiSuffix = ".test"
Jiyong Parkc678ad32018-04-10 13:07:10 +090037 sdkDocsSuffix = ".docs"
Jiyong Parkc678ad32018-04-10 13:07:10 +090038 sdkXmlFileSuffix = ".xml"
Jooyung Han624058e2019-12-24 18:38:06 +090039 permissionsTemplate = `<?xml version="1.0" encoding="utf-8"?>\n` +
40 `<!-- Copyright (C) 2018 The Android Open Source Project\n` +
41 `\n` +
42 ` Licensed under the Apache License, Version 2.0 (the "License");\n` +
43 ` you may not use this file except in compliance with the License.\n` +
44 ` You may obtain a copy of the License at\n` +
45 `\n` +
46 ` http://www.apache.org/licenses/LICENSE-2.0\n` +
47 `\n` +
48 ` Unless required by applicable law or agreed to in writing, software\n` +
49 ` distributed under the License is distributed on an "AS IS" BASIS,\n` +
50 ` WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\n` +
51 ` See the License for the specific language governing permissions and\n` +
52 ` limitations under the License.\n` +
53 `-->\n` +
54 `<permissions>\n` +
55 ` <library name="%s" file="%s"/>\n` +
56 `</permissions>\n`
Jiyong Parkc678ad32018-04-10 13:07:10 +090057)
58
Paul Duffind1b3a922020-01-22 11:57:20 +000059// A tag to associated a dependency with a specific api scope.
60type scopeDependencyTag struct {
61 blueprint.BaseDependencyTag
62 name string
63 apiScope *apiScope
64}
65
66// Provides information about an api scope, e.g. public, system, test.
67type apiScope struct {
68 // The name of the api scope, e.g. public, system, test
69 name string
70
71 // The tag to use to depend on the stubs library module.
72 stubsTag scopeDependencyTag
73
74 // The tag to use to depend on the stubs
75 apiFileTag scopeDependencyTag
76
77 // The scope specific prefix to add to the api file base of "current.txt" or "removed.txt".
78 apiFilePrefix string
79
80 // The scope specific prefix to add to the sdk library module name to construct a scope specific
81 // module name.
82 moduleSuffix string
83
84 // The suffix to add to the make variable that references the location of the api file.
85 apiFileMakeVariableSuffix string
86
87 // SDK version that the stubs library is built against. Note that this is always
88 // *current. Older stubs library built with a numbered SDK version is created from
89 // the prebuilt jar.
90 sdkVersion string
91}
92
93// Initialize a scope, creating and adding appropriate dependency tags
94func initApiScope(scope *apiScope) *apiScope {
95 //apiScope := &scope
96 scope.stubsTag = scopeDependencyTag{
97 name: scope.name + "-stubs",
98 apiScope: scope,
99 }
100 scope.apiFileTag = scopeDependencyTag{
101 name: scope.name + "-api",
102 apiScope: scope,
103 }
104 return scope
105}
106
107func (scope *apiScope) stubsModuleName(baseName string) string {
108 return baseName + sdkStubsLibrarySuffix + scope.moduleSuffix
109}
110
111func (scope *apiScope) docsModuleName(baseName string) string {
112 return baseName + sdkDocsSuffix + scope.moduleSuffix
113}
114
115type apiScopes []*apiScope
116
117func (scopes apiScopes) Strings(accessor func(*apiScope) string) []string {
118 var list []string
119 for _, scope := range scopes {
120 list = append(list, accessor(scope))
121 }
122 return list
123}
124
Jiyong Parkc678ad32018-04-10 13:07:10 +0900125var (
Paul Duffind1b3a922020-01-22 11:57:20 +0000126 apiScopePublic = initApiScope(&apiScope{
127 name: "public",
128 sdkVersion: "current",
129 })
130 apiScopeSystem = initApiScope(&apiScope{
131 name: "system",
132 apiFilePrefix: "system-",
133 moduleSuffix: sdkSystemApiSuffix,
134 apiFileMakeVariableSuffix: "_SYSTEM",
135 sdkVersion: "system_current",
136 })
137 apiScopeTest = initApiScope(&apiScope{
138 name: "test",
139 apiFilePrefix: "test-",
140 moduleSuffix: sdkTestApiSuffix,
141 apiFileMakeVariableSuffix: "_TEST",
142 sdkVersion: "test_current",
143 })
144 allApiScopes = apiScopes{
145 apiScopePublic,
146 apiScopeSystem,
147 apiScopeTest,
148 }
Jiyong Parkc678ad32018-04-10 13:07:10 +0900149)
150
Jiyong Park82484c02018-04-23 21:41:26 +0900151var (
152 javaSdkLibrariesLock sync.Mutex
153)
154
Jiyong Parkc678ad32018-04-10 13:07:10 +0900155// TODO: these are big features that are currently missing
Jiyong Park1be96912018-05-28 18:02:19 +0900156// 1) disallowing linking to the runtime shared lib
157// 2) HTML generation
Jiyong Parkc678ad32018-04-10 13:07:10 +0900158
159func init() {
Paul Duffin43dc1cc2019-12-19 11:18:54 +0000160 RegisterSdkLibraryBuildComponents(android.InitRegistrationContext)
Jiyong Parkc678ad32018-04-10 13:07:10 +0900161
Jiyong Park82484c02018-04-23 21:41:26 +0900162 android.RegisterMakeVarsProvider(pctx, func(ctx android.MakeVarsContext) {
163 javaSdkLibraries := javaSdkLibraries(ctx.Config())
164 sort.Strings(*javaSdkLibraries)
165 ctx.Strict("JAVA_SDK_LIBRARIES", strings.Join(*javaSdkLibraries, " "))
166 })
Jiyong Parkc678ad32018-04-10 13:07:10 +0900167}
168
Paul Duffin43dc1cc2019-12-19 11:18:54 +0000169func RegisterSdkLibraryBuildComponents(ctx android.RegistrationContext) {
170 ctx.RegisterModuleType("java_sdk_library", SdkLibraryFactory)
171 ctx.RegisterModuleType("java_sdk_library_import", sdkLibraryImportFactory)
172}
173
Jiyong Parkc678ad32018-04-10 13:07:10 +0900174type sdkLibraryProperties struct {
Sundong Ahnf043cf62018-06-25 16:04:37 +0900175 // List of Java libraries that will be in the classpath when building stubs
176 Stub_only_libs []string `android:"arch_variant"`
177
Paul Duffin7a586d32019-12-30 17:09:34 +0000178 // list of package names that will be documented and publicized as API.
179 // This allows the API to be restricted to a subset of the source files provided.
180 // If this is unspecified then all the source files will be treated as being part
181 // of the API.
Jiyong Parkc678ad32018-04-10 13:07:10 +0900182 Api_packages []string
183
Jiyong Park5a2c9d72018-05-01 22:25:41 +0900184 // list of package names that must be hidden from the API
185 Hidden_api_packages []string
186
Paul Duffin749f98f2019-12-30 17:23:46 +0000187 // the relative path to the directory containing the api specification files.
188 // Defaults to "api".
189 Api_dir *string
190
Paul Duffin43db9be2019-12-30 17:35:49 +0000191 // If set to true there is no runtime library.
192 Api_only *bool
193
Paul Duffin11512472019-02-11 15:55:17 +0000194 // local files that are used within user customized droiddoc options.
195 Droiddoc_option_files []string
196
197 // additional droiddoc options
198 // Available variables for substitution:
199 //
200 // $(location <label>): the path to the droiddoc_option_files with name <label>
Sundong Ahndd567f92018-07-31 17:19:11 +0900201 Droiddoc_options []string
202
Sundong Ahn054b19a2018-10-19 13:46:09 +0900203 // a list of top-level directories containing files to merge qualifier annotations
204 // (i.e. those intended to be included in the stubs written) from.
205 Merge_annotations_dirs []string
206
207 // a list of top-level directories containing Java stub files to merge show/hide annotations from.
208 Merge_inclusion_annotations_dirs []string
209
210 // If set to true, the path of dist files is apistubs/core. Defaults to false.
211 Core_lib *bool
212
Sundong Ahn80a87b32019-05-13 15:02:50 +0900213 // don't create dist rules.
214 No_dist *bool `blueprint:"mutated"`
215
Paul Duffin37e0b772019-12-30 17:20:10 +0000216 // indicates whether system and test apis should be managed.
217 Has_system_and_test_apis bool `blueprint:"mutated"`
218
Jiyong Parkc678ad32018-04-10 13:07:10 +0900219 // TODO: determines whether to create HTML doc or not
220 //Html_doc *bool
221}
222
Paul Duffind1b3a922020-01-22 11:57:20 +0000223type scopePaths struct {
224 stubsHeaderPath android.Paths
225 stubsImplPath android.Paths
226 apiFilePath android.Path
227}
228
Paul Duffin56d44902020-01-31 13:36:25 +0000229// Common code between sdk library and sdk library import
230type commonToSdkLibraryAndImport struct {
231 scopePaths map[*apiScope]*scopePaths
232}
233
234func (c *commonToSdkLibraryAndImport) getScopePaths(scope *apiScope) *scopePaths {
235 if c.scopePaths == nil {
236 c.scopePaths = make(map[*apiScope]*scopePaths)
237 }
238 paths := c.scopePaths[scope]
239 if paths == nil {
240 paths = &scopePaths{}
241 c.scopePaths[scope] = paths
242 }
243
244 return paths
245}
246
Inseob Kimc0907f12019-02-08 21:00:45 +0900247type SdkLibrary struct {
Sundong Ahn054b19a2018-10-19 13:46:09 +0900248 Library
Jiyong Parkc678ad32018-04-10 13:07:10 +0900249
Sundong Ahn054b19a2018-10-19 13:46:09 +0900250 sdkLibraryProperties sdkLibraryProperties
Jiyong Parkc678ad32018-04-10 13:07:10 +0900251
Paul Duffin56d44902020-01-31 13:36:25 +0000252 commonToSdkLibraryAndImport
Jooyung Han58f26ab2019-12-18 15:34:32 +0900253
Jooyung Han624058e2019-12-24 18:38:06 +0900254 permissionsFile android.Path
Jiyong Parkc678ad32018-04-10 13:07:10 +0900255}
256
Inseob Kimc0907f12019-02-08 21:00:45 +0900257var _ Dependency = (*SdkLibrary)(nil)
258var _ SdkLibraryDependency = (*SdkLibrary)(nil)
Colin Cross897d2ed2019-02-11 14:03:51 -0800259
Paul Duffind1b3a922020-01-22 11:57:20 +0000260func (module *SdkLibrary) getActiveApiScopes() apiScopes {
261 if module.sdkLibraryProperties.Has_system_and_test_apis {
262 return allApiScopes
263 } else {
264 return apiScopes{apiScopePublic}
265 }
266}
267
Paul Duffine74ac732020-02-06 13:51:46 +0000268var xmlPermissionsFileTag = dependencyTag{name: "xml-permissions-file"}
269
Inseob Kimc0907f12019-02-08 21:00:45 +0900270func (module *SdkLibrary) DepsMutator(ctx android.BottomUpMutatorContext) {
Paul Duffind1b3a922020-01-22 11:57:20 +0000271 for _, apiScope := range module.getActiveApiScopes() {
272 // Add dependencies to the stubs library
Paul Duffin50061512020-01-21 16:31:05 +0000273 ctx.AddVariationDependencies(nil, apiScope.stubsTag, module.stubsName(apiScope))
Paul Duffind1b3a922020-01-22 11:57:20 +0000274
Paul Duffin50061512020-01-21 16:31:05 +0000275 // And the api file
Paul Duffind1b3a922020-01-22 11:57:20 +0000276 ctx.AddVariationDependencies(nil, apiScope.apiFileTag, module.docsName(apiScope))
Sundong Ahn054b19a2018-10-19 13:46:09 +0900277 }
278
Paul Duffine74ac732020-02-06 13:51:46 +0000279 if !proptools.Bool(module.sdkLibraryProperties.Api_only) {
280 // Add dependency to the rule for generating the xml permissions file
281 ctx.AddDependency(module, xmlPermissionsFileTag, module.genXmlPermissionsFileName())
282 }
283
Sundong Ahn054b19a2018-10-19 13:46:09 +0900284 module.Library.deps(ctx)
Jiyong Parkc678ad32018-04-10 13:07:10 +0900285}
286
Inseob Kimc0907f12019-02-08 21:00:45 +0900287func (module *SdkLibrary) GenerateAndroidBuildActions(ctx android.ModuleContext) {
Paul Duffin43db9be2019-12-30 17:35:49 +0000288 // Don't build an implementation library if this is api only.
289 if !proptools.Bool(module.sdkLibraryProperties.Api_only) {
290 module.Library.GenerateAndroidBuildActions(ctx)
291 }
Sundong Ahn054b19a2018-10-19 13:46:09 +0900292
Sundong Ahn57368eb2018-07-06 11:20:23 +0900293 // Record the paths to the header jars of the library (stubs and impl).
Paul Duffind1b3a922020-01-22 11:57:20 +0000294 // When this java_sdk_library is depended upon from others via "libs" property,
Jiyong Parkc678ad32018-04-10 13:07:10 +0900295 // the recorded paths will be returned depending on the link type of the caller.
296 ctx.VisitDirectDeps(func(to android.Module) {
297 otherName := ctx.OtherModuleName(to)
298 tag := ctx.OtherModuleDependencyTag(to)
299
Sundong Ahn57368eb2018-07-06 11:20:23 +0900300 if lib, ok := to.(Dependency); ok {
Paul Duffind1b3a922020-01-22 11:57:20 +0000301 if scopeTag, ok := tag.(scopeDependencyTag); ok {
302 apiScope := scopeTag.apiScope
303 scopePaths := module.getScopePaths(apiScope)
304 scopePaths.stubsHeaderPath = lib.HeaderJars()
305 scopePaths.stubsImplPath = lib.ImplementationJars()
Jiyong Parkc678ad32018-04-10 13:07:10 +0900306 }
307 }
Sundong Ahn20e998b2018-07-24 11:19:26 +0900308 if doc, ok := to.(ApiFilePath); ok {
Paul Duffind1b3a922020-01-22 11:57:20 +0000309 if scopeTag, ok := tag.(scopeDependencyTag); ok {
310 apiScope := scopeTag.apiScope
311 scopePaths := module.getScopePaths(apiScope)
312 scopePaths.apiFilePath = doc.ApiFilePath()
313 } else {
Sundong Ahn20e998b2018-07-24 11:19:26 +0900314 ctx.ModuleErrorf("depends on module %q of unknown tag %q", otherName, tag)
315 }
316 }
Paul Duffine74ac732020-02-06 13:51:46 +0000317 if tag == xmlPermissionsFileTag {
318 if genRule, ok := to.(genrule.SourceFileGenerator); ok {
319 pf := genRule.GeneratedSourceFiles()
320 if len(pf) != 1 {
321 ctx.ModuleErrorf("%q failed to generate permission XML", otherName)
322 } else {
323 module.permissionsFile = pf[0]
324 }
325 } else {
326 ctx.ModuleErrorf("depends on module %q to generate xml permissions file but it does not provide any outputs", otherName)
327 }
328 }
Jiyong Parkc678ad32018-04-10 13:07:10 +0900329 })
330}
331
Jiyong Park0b0e1b92019-12-03 13:24:29 +0900332func (module *SdkLibrary) AndroidMkEntries() []android.AndroidMkEntries {
Paul Duffin43db9be2019-12-30 17:35:49 +0000333 if proptools.Bool(module.sdkLibraryProperties.Api_only) {
334 return nil
335 }
Jiyong Park0b0e1b92019-12-03 13:24:29 +0900336 entriesList := module.Library.AndroidMkEntries()
337 entries := &entriesList[0]
Jaewoong Jungb0c127c2019-08-29 14:56:03 -0700338 entries.Required = append(entries.Required, module.xmlFileName())
Sundong Ahn054b19a2018-10-19 13:46:09 +0900339
Jaewoong Jungb0c127c2019-08-29 14:56:03 -0700340 entries.ExtraFooters = []android.AndroidMkExtraFootersFunc{
341 func(w io.Writer, name, prefix, moduleDir string, entries *android.AndroidMkEntries) {
Jaewoong Jungb0c127c2019-08-29 14:56:03 -0700342 if !Bool(module.sdkLibraryProperties.No_dist) {
343 // Create a phony module that installs the impl library, for the case when this lib is
344 // in PRODUCT_PACKAGES.
345 owner := module.ModuleBase.Owner()
346 if owner == "" {
347 if Bool(module.sdkLibraryProperties.Core_lib) {
348 owner = "core"
349 } else {
350 owner = "android"
351 }
352 }
Paul Duffind1b3a922020-01-22 11:57:20 +0000353
354 // Create dist rules to install the stubs libs and api files to the dist dir
355 for _, apiScope := range module.getActiveApiScopes() {
356 if scopePaths, ok := module.scopePaths[apiScope]; ok {
357 if len(scopePaths.stubsHeaderPath) == 1 {
358 fmt.Fprintln(w, "$(call dist-for-goals,sdk win_sdk,"+
359 scopePaths.stubsImplPath.Strings()[0]+
360 ":"+path.Join("apistubs", owner, apiScope.name,
361 module.BaseModuleName()+".jar")+")")
362 }
363 if scopePaths.apiFilePath != nil {
364 fmt.Fprintln(w, "$(call dist-for-goals,sdk win_sdk,"+
365 scopePaths.apiFilePath.String()+
366 ":"+path.Join("apistubs", owner, apiScope.name, "api",
367 module.BaseModuleName()+".txt")+")")
368 }
369 }
Sundong Ahn80a87b32019-05-13 15:02:50 +0900370 }
Sundong Ahn4fd04bb2018-08-31 18:01:37 +0900371 }
Jaewoong Jungb0c127c2019-08-29 14:56:03 -0700372 },
Jiyong Park82484c02018-04-23 21:41:26 +0900373 }
Jiyong Park0b0e1b92019-12-03 13:24:29 +0900374 return entriesList
Jiyong Park82484c02018-04-23 21:41:26 +0900375}
376
Jiyong Parkc678ad32018-04-10 13:07:10 +0900377// Module name of the stubs library
Paul Duffind1b3a922020-01-22 11:57:20 +0000378func (module *SdkLibrary) stubsName(apiScope *apiScope) string {
379 return apiScope.stubsModuleName(module.BaseModuleName())
Jiyong Parkc678ad32018-04-10 13:07:10 +0900380}
381
382// Module name of the docs
Paul Duffind1b3a922020-01-22 11:57:20 +0000383func (module *SdkLibrary) docsName(apiScope *apiScope) string {
384 return apiScope.docsModuleName(module.BaseModuleName())
Jiyong Parkc678ad32018-04-10 13:07:10 +0900385}
386
387// Module name of the runtime implementation library
Inseob Kimc0907f12019-02-08 21:00:45 +0900388func (module *SdkLibrary) implName() string {
Sundong Ahn054b19a2018-10-19 13:46:09 +0900389 return module.BaseModuleName()
Jiyong Parkc678ad32018-04-10 13:07:10 +0900390}
391
392// File path to the runtime implementation library
Inseob Kimc0907f12019-02-08 21:00:45 +0900393func (module *SdkLibrary) implPath() string {
Jooyung Han58f26ab2019-12-18 15:34:32 +0900394 if apexName := module.ApexName(); apexName != "" {
395 // TODO(b/146468504): ApexName() is only a soong module name, not apex name.
396 // In most cases, this works fine. But when apex_name is set or override_apex is used
397 // this can be wrong.
398 return fmt.Sprintf("/apex/%s/javalib/%s.jar", apexName, module.implName())
399 }
Jiyong Parkc678ad32018-04-10 13:07:10 +0900400 partition := "system"
401 if module.SocSpecific() {
402 partition = "vendor"
403 } else if module.DeviceSpecific() {
404 partition = "odm"
405 } else if module.ProductSpecific() {
406 partition = "product"
Sundong Ahn0d7dff42019-12-04 12:53:44 +0900407 } else if module.SystemExtSpecific() {
408 partition = "system_ext"
Jiyong Parkc678ad32018-04-10 13:07:10 +0900409 }
410 return "/" + partition + "/framework/" + module.implName() + ".jar"
411}
412
413// Module name of the XML file for the lib
Inseob Kimc0907f12019-02-08 21:00:45 +0900414func (module *SdkLibrary) xmlFileName() string {
Jiyong Parkc678ad32018-04-10 13:07:10 +0900415 return module.BaseModuleName() + sdkXmlFileSuffix
416}
417
Paul Duffine74ac732020-02-06 13:51:46 +0000418// Module name of the rule for generating the XML permissions file
419func (module *SdkLibrary) genXmlPermissionsFileName() string {
420 return "gen-" + module.BaseModuleName() + sdkXmlFileSuffix
421}
422
Paul Duffin12ceb462019-12-24 20:31:31 +0000423// Get the sdk version for use when compiling the stubs library.
Paul Duffind1b3a922020-01-22 11:57:20 +0000424func (module *SdkLibrary) sdkVersionForStubsLibrary(mctx android.LoadHookContext, apiScope *apiScope) string {
Paul Duffin12ceb462019-12-24 20:31:31 +0000425 sdkDep := decodeSdkDep(mctx, sdkContext(&module.Library))
426 if sdkDep.hasStandardLibs() {
427 // If building against a standard sdk then use the sdk version appropriate for the scope.
Paul Duffind1b3a922020-01-22 11:57:20 +0000428 return apiScope.sdkVersion
Paul Duffin12ceb462019-12-24 20:31:31 +0000429 } else {
430 // Otherwise, use no system module.
431 return "none"
432 }
433}
434
Jiyong Parkc678ad32018-04-10 13:07:10 +0900435// $(INTERNAL_PLATFORM_<apiTagName>_API_FILE) points to the generated
436// api file for the current source
437// TODO: remove this when apicheck is done in soong
Paul Duffind1b3a922020-01-22 11:57:20 +0000438func (module *SdkLibrary) apiTagName(apiScope *apiScope) string {
439 return strings.Replace(strings.ToUpper(module.BaseModuleName()), ".", "_", -1) + apiScope.apiFileMakeVariableSuffix
Jiyong Parkc678ad32018-04-10 13:07:10 +0900440}
441
Paul Duffind1b3a922020-01-22 11:57:20 +0000442func (module *SdkLibrary) latestApiFilegroupName(apiScope *apiScope) string {
443 return ":" + module.BaseModuleName() + ".api." + apiScope.name + ".latest"
Jiyong Park58c518b2018-05-12 22:29:12 +0900444}
Jiyong Parkc678ad32018-04-10 13:07:10 +0900445
Paul Duffind1b3a922020-01-22 11:57:20 +0000446func (module *SdkLibrary) latestRemovedApiFilegroupName(apiScope *apiScope) string {
447 return ":" + module.BaseModuleName() + "-removed.api." + apiScope.name + ".latest"
Jiyong Parkc678ad32018-04-10 13:07:10 +0900448}
449
450// Creates a static java library that has API stubs
Paul Duffind1b3a922020-01-22 11:57:20 +0000451func (module *SdkLibrary) createStubsLibrary(mctx android.LoadHookContext, apiScope *apiScope) {
Jiyong Parkc678ad32018-04-10 13:07:10 +0900452 props := struct {
Sundong Ahn0d7dff42019-12-04 12:53:44 +0900453 Name *string
454 Srcs []string
Paul Duffin367ab912019-12-23 19:40:36 +0000455 Installable *bool
Sundong Ahn0d7dff42019-12-04 12:53:44 +0900456 Sdk_version *string
Paul Duffin12ceb462019-12-24 20:31:31 +0000457 System_modules *string
Sundong Ahn0d7dff42019-12-04 12:53:44 +0900458 Libs []string
459 Soc_specific *bool
460 Device_specific *bool
461 Product_specific *bool
462 System_ext_specific *bool
463 Compile_dex *bool
Sundong Ahn0d7dff42019-12-04 12:53:44 +0900464 Java_version *string
465 Product_variables struct {
Jiyong Park82484c02018-04-23 21:41:26 +0900466 Pdk struct {
467 Enabled *bool
468 }
Jiyong Parkc678ad32018-04-10 13:07:10 +0900469 }
Sundong Ahn054b19a2018-10-19 13:46:09 +0900470 Openjdk9 struct {
471 Srcs []string
472 Javacflags []string
473 }
Jiyong Parkc678ad32018-04-10 13:07:10 +0900474 }{}
475
Jiyong Parkdf130542018-04-27 16:29:21 +0900476 props.Name = proptools.StringPtr(module.stubsName(apiScope))
Jiyong Parkc678ad32018-04-10 13:07:10 +0900477 // sources are generated from the droiddoc
Jiyong Parkdf130542018-04-27 16:29:21 +0900478 props.Srcs = []string{":" + module.docsName(apiScope)}
Paul Duffin12ceb462019-12-24 20:31:31 +0000479 sdkVersion := module.sdkVersionForStubsLibrary(mctx, apiScope)
Paul Duffin52d398a2019-06-11 12:31:14 +0100480 props.Sdk_version = proptools.StringPtr(sdkVersion)
Paul Duffin12ceb462019-12-24 20:31:31 +0000481 props.System_modules = module.Library.Module.deviceProperties.System_modules
Paul Duffin367ab912019-12-23 19:40:36 +0000482 props.Installable = proptools.BoolPtr(false)
Sundong Ahn054b19a2018-10-19 13:46:09 +0900483 props.Libs = module.sdkLibraryProperties.Stub_only_libs
Jiyong Park82484c02018-04-23 21:41:26 +0900484 props.Product_variables.Pdk.Enabled = proptools.BoolPtr(false)
Sundong Ahn054b19a2018-10-19 13:46:09 +0900485 props.Openjdk9.Srcs = module.Library.Module.properties.Openjdk9.Srcs
486 props.Openjdk9.Javacflags = module.Library.Module.properties.Openjdk9.Javacflags
487 props.Java_version = module.Library.Module.properties.Java_version
488 if module.Library.Module.deviceProperties.Compile_dex != nil {
489 props.Compile_dex = module.Library.Module.deviceProperties.Compile_dex
Sundong Ahndd567f92018-07-31 17:19:11 +0900490 }
Jiyong Parkc678ad32018-04-10 13:07:10 +0900491
492 if module.SocSpecific() {
493 props.Soc_specific = proptools.BoolPtr(true)
494 } else if module.DeviceSpecific() {
495 props.Device_specific = proptools.BoolPtr(true)
496 } else if module.ProductSpecific() {
497 props.Product_specific = proptools.BoolPtr(true)
Sundong Ahn0d7dff42019-12-04 12:53:44 +0900498 } else if module.SystemExtSpecific() {
499 props.System_ext_specific = proptools.BoolPtr(true)
Jiyong Parkc678ad32018-04-10 13:07:10 +0900500 }
501
Colin Cross84dfc3d2019-09-25 11:33:01 -0700502 mctx.CreateModule(LibraryFactory, &props)
Jiyong Parkc678ad32018-04-10 13:07:10 +0900503}
504
505// Creates a droiddoc module that creates stubs source files from the given full source
506// files
Paul Duffind1b3a922020-01-22 11:57:20 +0000507func (module *SdkLibrary) createStubsSources(mctx android.LoadHookContext, apiScope *apiScope) {
Jiyong Parkc678ad32018-04-10 13:07:10 +0900508 props := struct {
Sundong Ahn054b19a2018-10-19 13:46:09 +0900509 Name *string
510 Srcs []string
511 Installable *bool
Paul Duffin52d398a2019-06-11 12:31:14 +0100512 Sdk_version *string
Paul Duffin12ceb462019-12-24 20:31:31 +0000513 System_modules *string
Sundong Ahn054b19a2018-10-19 13:46:09 +0900514 Libs []string
Paul Duffin11512472019-02-11 15:55:17 +0000515 Arg_files []string
Sundong Ahn054b19a2018-10-19 13:46:09 +0900516 Args *string
517 Api_tag_name *string
518 Api_filename *string
519 Removed_api_filename *string
Sundong Ahn054b19a2018-10-19 13:46:09 +0900520 Java_version *string
521 Merge_annotations_dirs []string
522 Merge_inclusion_annotations_dirs []string
523 Check_api struct {
Inseob Kim38449af2019-02-28 14:24:05 +0900524 Current ApiToCheck
525 Last_released ApiToCheck
526 Ignore_missing_latest_api *bool
Jiyong Park58c518b2018-05-12 22:29:12 +0900527 }
Sundong Ahn1b92c822018-05-29 11:35:17 +0900528 Aidl struct {
529 Include_dirs []string
530 Local_include_dirs []string
531 }
Jiyong Parkc678ad32018-04-10 13:07:10 +0900532 }{}
533
Paul Duffin250e6192019-06-07 10:44:37 +0100534 sdkDep := decodeSdkDep(mctx, sdkContext(&module.Library))
Paul Duffin12ceb462019-12-24 20:31:31 +0000535 // Use the platform API if standard libraries were requested, otherwise use
536 // no default libraries.
Paul Duffin52d398a2019-06-11 12:31:14 +0100537 sdkVersion := ""
538 if !sdkDep.hasStandardLibs() {
539 sdkVersion = "none"
540 }
Paul Duffin250e6192019-06-07 10:44:37 +0100541
Jiyong Parkdf130542018-04-27 16:29:21 +0900542 props.Name = proptools.StringPtr(module.docsName(apiScope))
Sundong Ahn054b19a2018-10-19 13:46:09 +0900543 props.Srcs = append(props.Srcs, module.Library.Module.properties.Srcs...)
Paul Duffin52d398a2019-06-11 12:31:14 +0100544 props.Sdk_version = proptools.StringPtr(sdkVersion)
Paul Duffin12ceb462019-12-24 20:31:31 +0000545 props.System_modules = module.Library.Module.deviceProperties.System_modules
Jiyong Parkc678ad32018-04-10 13:07:10 +0900546 props.Installable = proptools.BoolPtr(false)
Sundong Ahne6f0b052018-06-05 16:46:14 +0900547 // A droiddoc module has only one Libs property and doesn't distinguish between
548 // shared libs and static libs. So we need to add both of these libs to Libs property.
Sundong Ahn054b19a2018-10-19 13:46:09 +0900549 props.Libs = module.Library.Module.properties.Libs
550 props.Libs = append(props.Libs, module.Library.Module.properties.Static_libs...)
551 props.Aidl.Include_dirs = module.Library.Module.deviceProperties.Aidl.Include_dirs
552 props.Aidl.Local_include_dirs = module.Library.Module.deviceProperties.Aidl.Local_include_dirs
Sundong Ahn054b19a2018-10-19 13:46:09 +0900553 props.Java_version = module.Library.Module.properties.Java_version
Jiyong Parkc678ad32018-04-10 13:07:10 +0900554
Sundong Ahn054b19a2018-10-19 13:46:09 +0900555 props.Merge_annotations_dirs = module.sdkLibraryProperties.Merge_annotations_dirs
556 props.Merge_inclusion_annotations_dirs = module.sdkLibraryProperties.Merge_inclusion_annotations_dirs
557
Paul Duffin235ffff2019-12-24 10:41:30 +0000558 droiddocArgs := []string{}
559 if len(module.sdkLibraryProperties.Api_packages) != 0 {
560 droiddocArgs = append(droiddocArgs, "--stub-packages "+strings.Join(module.sdkLibraryProperties.Api_packages, ":"))
561 }
562 if len(module.sdkLibraryProperties.Hidden_api_packages) != 0 {
563 droiddocArgs = append(droiddocArgs,
564 android.JoinWithPrefix(module.sdkLibraryProperties.Hidden_api_packages, " --hide-package "))
565 }
566 droiddocArgs = append(droiddocArgs, module.sdkLibraryProperties.Droiddoc_options...)
567 disabledWarnings := []string{
568 "MissingPermission",
569 "BroadcastBehavior",
570 "HiddenSuperclass",
571 "DeprecationMismatch",
572 "UnavailableSymbol",
573 "SdkConstant",
574 "HiddenTypeParameter",
575 "Todo",
576 "Typo",
577 }
578 droiddocArgs = append(droiddocArgs, android.JoinWithPrefix(disabledWarnings, "--hide "))
Sundong Ahnfb2721f2018-09-17 13:23:09 +0900579
Jiyong Parkdf130542018-04-27 16:29:21 +0900580 switch apiScope {
581 case apiScopeSystem:
Paul Duffin235ffff2019-12-24 10:41:30 +0000582 droiddocArgs = append(droiddocArgs, "-showAnnotation android.annotation.SystemApi")
Jiyong Parkdf130542018-04-27 16:29:21 +0900583 case apiScopeTest:
Paul Duffin235ffff2019-12-24 10:41:30 +0000584 droiddocArgs = append(droiddocArgs, " -showAnnotation android.annotation.TestApi")
Jiyong Parkc678ad32018-04-10 13:07:10 +0900585 }
Paul Duffin11512472019-02-11 15:55:17 +0000586 props.Arg_files = module.sdkLibraryProperties.Droiddoc_option_files
Paul Duffin235ffff2019-12-24 10:41:30 +0000587 props.Args = proptools.StringPtr(strings.Join(droiddocArgs, " "))
Jiyong Parkc678ad32018-04-10 13:07:10 +0900588
589 // List of APIs identified from the provided source files are created. They are later
590 // compared against to the not-yet-released (a.k.a current) list of APIs and to the
591 // last-released (a.k.a numbered) list of API.
Paul Duffind1b3a922020-01-22 11:57:20 +0000592 currentApiFileName := apiScope.apiFilePrefix + "current.txt"
593 removedApiFileName := apiScope.apiFilePrefix + "removed.txt"
Paul Duffin749f98f2019-12-30 17:23:46 +0000594 apiDir := module.getApiDir()
595 currentApiFileName = path.Join(apiDir, currentApiFileName)
596 removedApiFileName = path.Join(apiDir, removedApiFileName)
Jiyong Park58c518b2018-05-12 22:29:12 +0900597 // TODO(jiyong): remove these three props
Jiyong Parkdf130542018-04-27 16:29:21 +0900598 props.Api_tag_name = proptools.StringPtr(module.apiTagName(apiScope))
Jiyong Parkc678ad32018-04-10 13:07:10 +0900599 props.Api_filename = proptools.StringPtr(currentApiFileName)
600 props.Removed_api_filename = proptools.StringPtr(removedApiFileName)
601
Jiyong Park58c518b2018-05-12 22:29:12 +0900602 // check against the not-yet-release API
603 props.Check_api.Current.Api_file = proptools.StringPtr(currentApiFileName)
604 props.Check_api.Current.Removed_api_file = proptools.StringPtr(removedApiFileName)
Jiyong Park58c518b2018-05-12 22:29:12 +0900605
606 // check against the latest released API
607 props.Check_api.Last_released.Api_file = proptools.StringPtr(
608 module.latestApiFilegroupName(apiScope))
609 props.Check_api.Last_released.Removed_api_file = proptools.StringPtr(
610 module.latestRemovedApiFilegroupName(apiScope))
Inseob Kim38449af2019-02-28 14:24:05 +0900611 props.Check_api.Ignore_missing_latest_api = proptools.BoolPtr(true)
Jiyong Park58c518b2018-05-12 22:29:12 +0900612
Colin Cross84dfc3d2019-09-25 11:33:01 -0700613 mctx.CreateModule(DroidstubsFactory, &props)
Jiyong Parkc678ad32018-04-10 13:07:10 +0900614}
615
Paul Duffine74ac732020-02-06 13:51:46 +0000616func (module *SdkLibrary) XmlPermissionsFile() android.Path {
617 return module.permissionsFile
618}
619
620func (module *SdkLibrary) XmlPermissionsFileContent() string {
621 return fmt.Sprintf(permissionsTemplate, module.BaseModuleName(), module.implPath())
622}
623
Jiyong Parkc678ad32018-04-10 13:07:10 +0900624// Creates the xml file that publicizes the runtime library
Colin Crossf8b860a2019-04-16 14:43:28 -0700625func (module *SdkLibrary) createXmlFile(mctx android.LoadHookContext) {
Paul Duffine74ac732020-02-06 13:51:46 +0000626
627 xmlContent := module.XmlPermissionsFileContent()
628
629 genRuleName := module.genXmlPermissionsFileName()
630
631 // Create a genrule module to create the XML permissions file.
632 genRuleProps := struct {
633 Name *string
634 Cmd *string
635 Out []string
636 }{
637 Name: proptools.StringPtr(genRuleName),
638 Cmd: proptools.StringPtr("echo -e '" + xmlContent + "' > '$(out)'"),
639 Out: []string{module.xmlFileName()},
640 }
641
642 mctx.CreateModule(genrule.GenRuleFactory, &genRuleProps)
643
Jiyong Parkc678ad32018-04-10 13:07:10 +0900644 // creates a prebuilt_etc module to actually place the xml file under
645 // <partition>/etc/permissions
646 etcProps := struct {
Sundong Ahn0d7dff42019-12-04 12:53:44 +0900647 Name *string
648 Src *string
649 Sub_dir *string
650 Soc_specific *bool
651 Device_specific *bool
652 Product_specific *bool
653 System_ext_specific *bool
Jiyong Parkc678ad32018-04-10 13:07:10 +0900654 }{}
655 etcProps.Name = proptools.StringPtr(module.xmlFileName())
Paul Duffine74ac732020-02-06 13:51:46 +0000656 etcProps.Src = proptools.StringPtr(":" + genRuleName)
Jiyong Parkc678ad32018-04-10 13:07:10 +0900657 etcProps.Sub_dir = proptools.StringPtr("permissions")
658 if module.SocSpecific() {
659 etcProps.Soc_specific = proptools.BoolPtr(true)
660 } else if module.DeviceSpecific() {
661 etcProps.Device_specific = proptools.BoolPtr(true)
662 } else if module.ProductSpecific() {
663 etcProps.Product_specific = proptools.BoolPtr(true)
Sundong Ahn0d7dff42019-12-04 12:53:44 +0900664 } else if module.SystemExtSpecific() {
665 etcProps.System_ext_specific = proptools.BoolPtr(true)
Jiyong Parkc678ad32018-04-10 13:07:10 +0900666 }
Colin Cross84dfc3d2019-09-25 11:33:01 -0700667 mctx.CreateModule(android.PrebuiltEtcFactory, &etcProps)
Jiyong Parkc678ad32018-04-10 13:07:10 +0900668}
669
Paul Duffin50061512020-01-21 16:31:05 +0000670func PrebuiltJars(ctx android.BaseModuleContext, baseName string, s sdkSpec) android.Paths {
Jiyong Park6a927c42020-01-21 02:03:43 +0900671 var ver sdkVersion
672 var kind sdkKind
673 if s.usePrebuilt(ctx) {
674 ver = s.version
675 kind = s.kind
Jiyong Parkc678ad32018-04-10 13:07:10 +0900676 } else {
Jiyong Park6a927c42020-01-21 02:03:43 +0900677 // We don't have prebuilt SDK for the specific sdkVersion.
678 // Instead of breaking the build, fallback to use "system_current"
679 ver = sdkVersionCurrent
680 kind = sdkSystem
Sundong Ahn054b19a2018-10-19 13:46:09 +0900681 }
Jiyong Park6a927c42020-01-21 02:03:43 +0900682
683 dir := filepath.Join("prebuilts", "sdk", ver.String(), kind.String())
Paul Duffin50061512020-01-21 16:31:05 +0000684 jar := filepath.Join(dir, baseName+".jar")
Sundong Ahn054b19a2018-10-19 13:46:09 +0900685 jarPath := android.ExistentPathForSource(ctx, jar)
Sundong Ahnae418ac2019-02-28 15:01:28 +0900686 if !jarPath.Valid() {
Colin Cross07c88562020-01-07 09:34:44 -0800687 if ctx.Config().AllowMissingDependencies() {
688 return android.Paths{android.PathForSource(ctx, jar)}
689 } else {
Jiyong Park6a927c42020-01-21 02:03:43 +0900690 ctx.PropertyErrorf("sdk_library", "invalid sdk version %q, %q does not exist", s.raw, jar)
Colin Cross07c88562020-01-07 09:34:44 -0800691 }
Sundong Ahnae418ac2019-02-28 15:01:28 +0900692 return nil
693 }
Sundong Ahn054b19a2018-10-19 13:46:09 +0900694 return android.Paths{jarPath.Path()}
695}
696
Paul Duffind1b3a922020-01-22 11:57:20 +0000697func (module *SdkLibrary) sdkJars(
698 ctx android.BaseModuleContext,
699 sdkVersion sdkSpec,
700 headerJars bool) android.Paths {
701
Paul Duffin50061512020-01-21 16:31:05 +0000702 // If a specific numeric version has been requested then use prebuilt versions of the sdk.
703 if sdkVersion.version.isNumbered() {
704 return PrebuiltJars(ctx, module.BaseModuleName(), sdkVersion)
Sundong Ahn054b19a2018-10-19 13:46:09 +0900705 } else {
Paul Duffind1b3a922020-01-22 11:57:20 +0000706 if !sdkVersion.specified() {
707 if headerJars {
708 return module.Library.HeaderJars()
709 } else {
710 return module.Library.ImplementationJars()
711 }
712 }
Paul Duffin726d23c2020-01-22 16:30:37 +0000713 var apiScope *apiScope
Jiyong Park6a927c42020-01-21 02:03:43 +0900714 switch sdkVersion.kind {
715 case sdkSystem:
Paul Duffin726d23c2020-01-22 16:30:37 +0000716 apiScope = apiScopeSystem
717 case sdkTest:
718 apiScope = apiScopeTest
Jiyong Park6a927c42020-01-21 02:03:43 +0900719 case sdkPrivate:
Sundong Ahn054b19a2018-10-19 13:46:09 +0900720 return module.Library.HeaderJars()
Jiyong Park6a927c42020-01-21 02:03:43 +0900721 default:
Paul Duffin726d23c2020-01-22 16:30:37 +0000722 apiScope = apiScopePublic
Paul Duffind1b3a922020-01-22 11:57:20 +0000723 }
724
Paul Duffin726d23c2020-01-22 16:30:37 +0000725 paths := module.getScopePaths(apiScope)
Paul Duffind1b3a922020-01-22 11:57:20 +0000726 if headerJars {
727 return paths.stubsHeaderPath
728 } else {
729 return paths.stubsImplPath
Sundong Ahn054b19a2018-10-19 13:46:09 +0900730 }
Jiyong Parkc678ad32018-04-10 13:07:10 +0900731 }
732}
733
Sundong Ahn241cd372018-07-13 16:16:44 +0900734// to satisfy SdkLibraryDependency interface
Paul Duffind1b3a922020-01-22 11:57:20 +0000735func (module *SdkLibrary) SdkHeaderJars(ctx android.BaseModuleContext, sdkVersion sdkSpec) android.Paths {
736 return module.sdkJars(ctx, sdkVersion, true /*headerJars*/)
737}
738
739// to satisfy SdkLibraryDependency interface
Jiyong Park6a927c42020-01-21 02:03:43 +0900740func (module *SdkLibrary) SdkImplementationJars(ctx android.BaseModuleContext, sdkVersion sdkSpec) android.Paths {
Paul Duffind1b3a922020-01-22 11:57:20 +0000741 return module.sdkJars(ctx, sdkVersion, false /*headerJars*/)
Sundong Ahn241cd372018-07-13 16:16:44 +0900742}
743
Sundong Ahn80a87b32019-05-13 15:02:50 +0900744func (module *SdkLibrary) SetNoDist() {
745 module.sdkLibraryProperties.No_dist = proptools.BoolPtr(true)
746}
747
Colin Cross571cccf2019-02-04 11:22:08 -0800748var javaSdkLibrariesKey = android.NewOnceKey("javaSdkLibraries")
749
Jiyong Park82484c02018-04-23 21:41:26 +0900750func javaSdkLibraries(config android.Config) *[]string {
Colin Cross571cccf2019-02-04 11:22:08 -0800751 return config.Once(javaSdkLibrariesKey, func() interface{} {
Jiyong Park82484c02018-04-23 21:41:26 +0900752 return &[]string{}
753 }).(*[]string)
754}
755
Paul Duffin749f98f2019-12-30 17:23:46 +0000756func (module *SdkLibrary) getApiDir() string {
757 return proptools.StringDefault(module.sdkLibraryProperties.Api_dir, "api")
758}
759
Jiyong Parkc678ad32018-04-10 13:07:10 +0900760// For a java_sdk_library module, create internal modules for stubs, docs,
761// runtime libs and xml file. If requested, the stubs and docs are created twice
762// once for public API level and once for system API level
Colin Crossf8b860a2019-04-16 14:43:28 -0700763func (module *SdkLibrary) CreateInternalModules(mctx android.LoadHookContext) {
Inseob Kim6e93ac92019-03-21 17:43:49 +0900764 if len(module.Library.Module.properties.Srcs) == 0 {
Inseob Kimc0907f12019-02-08 21:00:45 +0900765 mctx.PropertyErrorf("srcs", "java_sdk_library must specify srcs")
Jooyung Han58f26ab2019-12-18 15:34:32 +0900766 return
Inseob Kimc0907f12019-02-08 21:00:45 +0900767 }
768
Paul Duffin37e0b772019-12-30 17:20:10 +0000769 // If this builds against standard libraries (i.e. is not part of the core libraries)
770 // then assume it provides both system and test apis. Otherwise, assume it does not and
771 // also assume it does not contribute to the dist build.
772 sdkDep := decodeSdkDep(mctx, sdkContext(&module.Library))
773 hasSystemAndTestApis := sdkDep.hasStandardLibs()
774 module.sdkLibraryProperties.Has_system_and_test_apis = hasSystemAndTestApis
775 module.sdkLibraryProperties.No_dist = proptools.BoolPtr(!hasSystemAndTestApis)
776
Inseob Kim8098faa2019-03-18 10:19:51 +0900777 missing_current_api := false
778
Paul Duffind1b3a922020-01-22 11:57:20 +0000779 activeScopes := module.getActiveApiScopes()
780
Paul Duffin749f98f2019-12-30 17:23:46 +0000781 apiDir := module.getApiDir()
Paul Duffind1b3a922020-01-22 11:57:20 +0000782 for _, scope := range activeScopes {
Inseob Kim8098faa2019-03-18 10:19:51 +0900783 for _, api := range []string{"current.txt", "removed.txt"} {
Paul Duffind1b3a922020-01-22 11:57:20 +0000784 path := path.Join(mctx.ModuleDir(), apiDir, scope.apiFilePrefix+api)
Inseob Kim8098faa2019-03-18 10:19:51 +0900785 p := android.ExistentPathForSource(mctx, path)
786 if !p.Valid() {
787 mctx.ModuleErrorf("Current api file %#v doesn't exist", path)
788 missing_current_api = true
789 }
790 }
791 }
792
793 if missing_current_api {
794 script := "build/soong/scripts/gen-java-current-api-files.sh"
795 p := android.ExistentPathForSource(mctx, script)
796
797 if !p.Valid() {
798 panic(fmt.Sprintf("script file %s doesn't exist", script))
799 }
800
801 mctx.ModuleErrorf("One or more current api files are missing. "+
802 "You can update them by:\n"+
Paul Duffin37e0b772019-12-30 17:20:10 +0000803 "%s %q %s && m update-api",
Paul Duffind1b3a922020-01-22 11:57:20 +0000804 script, filepath.Join(mctx.ModuleDir(), apiDir),
805 strings.Join(activeScopes.Strings(func(s *apiScope) string { return s.apiFilePrefix }), " "))
Inseob Kim8098faa2019-03-18 10:19:51 +0900806 return
807 }
808
Paul Duffind1b3a922020-01-22 11:57:20 +0000809 for _, scope := range activeScopes {
810 module.createStubsLibrary(mctx, scope)
811 module.createStubsSources(mctx, scope)
Inseob Kimc0907f12019-02-08 21:00:45 +0900812 }
813
Paul Duffin43db9be2019-12-30 17:35:49 +0000814 if !proptools.Bool(module.sdkLibraryProperties.Api_only) {
815 // for runtime
816 module.createXmlFile(mctx)
817
818 // record java_sdk_library modules so that they are exported to make
819 javaSdkLibraries := javaSdkLibraries(mctx.Config())
820 javaSdkLibrariesLock.Lock()
821 defer javaSdkLibrariesLock.Unlock()
822 *javaSdkLibraries = append(*javaSdkLibraries, module.BaseModuleName())
823 }
Inseob Kimc0907f12019-02-08 21:00:45 +0900824}
825
826func (module *SdkLibrary) InitSdkLibraryProperties() {
Sundong Ahn054b19a2018-10-19 13:46:09 +0900827 module.AddProperties(
828 &module.sdkLibraryProperties,
829 &module.Library.Module.properties,
830 &module.Library.Module.dexpreoptProperties,
831 &module.Library.Module.deviceProperties,
832 &module.Library.Module.protoProperties,
833 )
834
835 module.Library.Module.properties.Installable = proptools.BoolPtr(true)
836 module.Library.Module.deviceProperties.IsSDKLibrary = true
Inseob Kimc0907f12019-02-08 21:00:45 +0900837}
Sundong Ahn054b19a2018-10-19 13:46:09 +0900838
Jaewoong Jung4f158ee2019-07-11 10:05:35 -0700839// java_sdk_library is a special Java library that provides optional platform APIs to apps.
840// In practice, it can be viewed as a combination of several modules: 1) stubs library that clients
841// are linked against to, 2) droiddoc module that internally generates API stubs source files,
842// 3) the real runtime shared library that implements the APIs, and 4) XML file for adding
843// the runtime lib to the classpath at runtime if requested via <uses-library>.
Inseob Kimc0907f12019-02-08 21:00:45 +0900844func SdkLibraryFactory() android.Module {
845 module := &SdkLibrary{}
846 module.InitSdkLibraryProperties()
Jooyung Han58f26ab2019-12-18 15:34:32 +0900847 android.InitApexModule(module)
Sundong Ahn054b19a2018-10-19 13:46:09 +0900848 InitJavaModule(module, android.HostAndDeviceSupported)
Colin Crossf8b860a2019-04-16 14:43:28 -0700849 android.AddLoadHook(module, func(ctx android.LoadHookContext) { module.CreateInternalModules(ctx) })
Jiyong Parkc678ad32018-04-10 13:07:10 +0900850 return module
851}
Colin Cross79c7c262019-04-17 11:11:46 -0700852
853//
854// SDK library prebuilts
855//
856
Paul Duffin56d44902020-01-31 13:36:25 +0000857// Properties associated with each api scope.
858type sdkLibraryScopeProperties struct {
Colin Cross79c7c262019-04-17 11:11:46 -0700859 Jars []string `android:"path"`
860
861 Sdk_version *string
862
Colin Cross79c7c262019-04-17 11:11:46 -0700863 // List of shared java libs that this module has dependencies to
864 Libs []string
Colin Cross79c7c262019-04-17 11:11:46 -0700865}
866
Paul Duffin56d44902020-01-31 13:36:25 +0000867type sdkLibraryImportProperties struct {
Paul Duffinfcfd7912020-01-31 17:54:30 +0000868 // List of shared java libs, common to all scopes, that this module has
869 // dependencies to
870 Libs []string
871
Paul Duffin56d44902020-01-31 13:36:25 +0000872 // Properties associated with the public api scope.
873 Public sdkLibraryScopeProperties
874
875 // Properties associated with the system api scope.
876 System sdkLibraryScopeProperties
877
878 // Properties associated with the test api scope.
879 Test sdkLibraryScopeProperties
880}
881
Colin Cross79c7c262019-04-17 11:11:46 -0700882type sdkLibraryImport struct {
883 android.ModuleBase
884 android.DefaultableModuleBase
885 prebuilt android.Prebuilt
886
887 properties sdkLibraryImportProperties
888
Paul Duffin56d44902020-01-31 13:36:25 +0000889 commonToSdkLibraryAndImport
Colin Cross79c7c262019-04-17 11:11:46 -0700890}
891
892var _ SdkLibraryDependency = (*sdkLibraryImport)(nil)
893
Jaewoong Jung4f158ee2019-07-11 10:05:35 -0700894// java_sdk_library_import imports a prebuilt java_sdk_library.
Colin Cross79c7c262019-04-17 11:11:46 -0700895func sdkLibraryImportFactory() android.Module {
896 module := &sdkLibraryImport{}
897
Paul Duffinfcfd7912020-01-31 17:54:30 +0000898 module.AddProperties(&module.properties)
Colin Cross79c7c262019-04-17 11:11:46 -0700899
Paul Duffinfcfd7912020-01-31 17:54:30 +0000900 android.InitPrebuiltModule(module, &[]string{})
Colin Cross79c7c262019-04-17 11:11:46 -0700901 InitJavaModule(module, android.HostAndDeviceSupported)
902
903 android.AddLoadHook(module, func(mctx android.LoadHookContext) { module.createInternalModules(mctx) })
904 return module
905}
906
907func (module *sdkLibraryImport) Prebuilt() *android.Prebuilt {
908 return &module.prebuilt
909}
910
911func (module *sdkLibraryImport) Name() string {
912 return module.prebuilt.Name(module.ModuleBase.Name())
913}
914
915func (module *sdkLibraryImport) createInternalModules(mctx android.LoadHookContext) {
Colin Cross79c7c262019-04-17 11:11:46 -0700916
Paul Duffin50061512020-01-21 16:31:05 +0000917 // If the build is configured to use prebuilts then force this to be preferred.
918 if mctx.Config().UnbundledBuildUsePrebuiltSdks() {
919 module.prebuilt.ForcePrefer()
920 }
921
Paul Duffin56d44902020-01-31 13:36:25 +0000922 for apiScope, scopeProperties := range module.scopeProperties() {
923 if len(scopeProperties.Jars) == 0 {
924 continue
925 }
926
927 // Creates a java import for the jar with ".stubs" suffix
928 props := struct {
929 Name *string
930 Soc_specific *bool
931 Device_specific *bool
932 Product_specific *bool
933 System_ext_specific *bool
934 Sdk_version *string
935 Libs []string
936 Jars []string
Paul Duffin50061512020-01-21 16:31:05 +0000937 Prefer *bool
Paul Duffin56d44902020-01-31 13:36:25 +0000938 }{}
939
940 props.Name = proptools.StringPtr(apiScope.stubsModuleName(module.BaseModuleName()))
941 props.Sdk_version = scopeProperties.Sdk_version
Paul Duffinfcfd7912020-01-31 17:54:30 +0000942 // Prepend any of the libs from the legacy public properties to the libs for each of the
943 // scopes to avoid having to duplicate them in each scope.
944 props.Libs = append(module.properties.Libs, scopeProperties.Libs...)
Paul Duffin56d44902020-01-31 13:36:25 +0000945 props.Jars = scopeProperties.Jars
946
947 if module.SocSpecific() {
948 props.Soc_specific = proptools.BoolPtr(true)
949 } else if module.DeviceSpecific() {
950 props.Device_specific = proptools.BoolPtr(true)
951 } else if module.ProductSpecific() {
952 props.Product_specific = proptools.BoolPtr(true)
953 } else if module.SystemExtSpecific() {
954 props.System_ext_specific = proptools.BoolPtr(true)
955 }
956
Paul Duffin50061512020-01-21 16:31:05 +0000957 // If the build should use prebuilt sdks then set prefer to true on the stubs library.
958 // That will cause the prebuilt version of the stubs to override the source version.
959 if mctx.Config().UnbundledBuildUsePrebuiltSdks() {
960 props.Prefer = proptools.BoolPtr(true)
961 }
962
Paul Duffin56d44902020-01-31 13:36:25 +0000963 mctx.CreateModule(ImportFactory, &props)
964 }
Colin Cross79c7c262019-04-17 11:11:46 -0700965
966 javaSdkLibraries := javaSdkLibraries(mctx.Config())
967 javaSdkLibrariesLock.Lock()
968 defer javaSdkLibrariesLock.Unlock()
969 *javaSdkLibraries = append(*javaSdkLibraries, module.BaseModuleName())
970}
971
Paul Duffin56d44902020-01-31 13:36:25 +0000972func (module *sdkLibraryImport) scopeProperties() map[*apiScope]*sdkLibraryScopeProperties {
973 p := make(map[*apiScope]*sdkLibraryScopeProperties)
974 p[apiScopePublic] = &module.properties.Public
975 p[apiScopeSystem] = &module.properties.System
976 p[apiScopeTest] = &module.properties.Test
977 return p
978}
979
Colin Cross79c7c262019-04-17 11:11:46 -0700980func (module *sdkLibraryImport) DepsMutator(ctx android.BottomUpMutatorContext) {
Paul Duffin56d44902020-01-31 13:36:25 +0000981 for apiScope, scopeProperties := range module.scopeProperties() {
982 if len(scopeProperties.Jars) == 0 {
983 continue
984 }
985
986 // Add dependencies to the prebuilt stubs library
987 ctx.AddVariationDependencies(nil, apiScope.stubsTag, apiScope.stubsModuleName(module.BaseModuleName()))
988 }
Colin Cross79c7c262019-04-17 11:11:46 -0700989}
990
991func (module *sdkLibraryImport) GenerateAndroidBuildActions(ctx android.ModuleContext) {
992 // Record the paths to the prebuilt stubs library.
993 ctx.VisitDirectDeps(func(to android.Module) {
994 tag := ctx.OtherModuleDependencyTag(to)
995
Paul Duffin56d44902020-01-31 13:36:25 +0000996 if lib, ok := to.(Dependency); ok {
997 if scopeTag, ok := tag.(scopeDependencyTag); ok {
998 apiScope := scopeTag.apiScope
999 scopePaths := module.getScopePaths(apiScope)
1000 scopePaths.stubsHeaderPath = lib.HeaderJars()
1001 }
Colin Cross79c7c262019-04-17 11:11:46 -07001002 }
1003 })
1004}
1005
Paul Duffin56d44902020-01-31 13:36:25 +00001006func (module *sdkLibraryImport) sdkJars(
1007 ctx android.BaseModuleContext,
1008 sdkVersion sdkSpec) android.Paths {
1009
Paul Duffin50061512020-01-21 16:31:05 +00001010 // If a specific numeric version has been requested then use prebuilt versions of the sdk.
1011 if sdkVersion.version.isNumbered() {
1012 return PrebuiltJars(ctx, module.BaseModuleName(), sdkVersion)
1013 }
1014
Paul Duffin56d44902020-01-31 13:36:25 +00001015 var apiScope *apiScope
1016 switch sdkVersion.kind {
1017 case sdkSystem:
1018 apiScope = apiScopeSystem
1019 case sdkTest:
1020 apiScope = apiScopeTest
1021 default:
1022 apiScope = apiScopePublic
1023 }
1024
1025 paths := module.getScopePaths(apiScope)
1026 return paths.stubsHeaderPath
1027}
1028
Colin Cross79c7c262019-04-17 11:11:46 -07001029// to satisfy SdkLibraryDependency interface
Jiyong Park6a927c42020-01-21 02:03:43 +09001030func (module *sdkLibraryImport) SdkHeaderJars(ctx android.BaseModuleContext, sdkVersion sdkSpec) android.Paths {
Colin Cross79c7c262019-04-17 11:11:46 -07001031 // This module is just a wrapper for the prebuilt stubs.
Paul Duffin56d44902020-01-31 13:36:25 +00001032 return module.sdkJars(ctx, sdkVersion)
Colin Cross79c7c262019-04-17 11:11:46 -07001033}
1034
1035// to satisfy SdkLibraryDependency interface
Jiyong Park6a927c42020-01-21 02:03:43 +09001036func (module *sdkLibraryImport) SdkImplementationJars(ctx android.BaseModuleContext, sdkVersion sdkSpec) android.Paths {
Colin Cross79c7c262019-04-17 11:11:46 -07001037 // This module is just a wrapper for the stubs.
Paul Duffin56d44902020-01-31 13:36:25 +00001038 return module.sdkJars(ctx, sdkVersion)
Colin Cross79c7c262019-04-17 11:11:46 -07001039}