blob: e725ce9d63187645bd67b4a6461456515a0c20b8 [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) {
Jiyong Parke3ef3c82019-07-15 15:31:16 +0900271 useBuiltStubs := !ctx.Config().UnbundledBuildUsePrebuiltSdks()
Paul Duffind1b3a922020-01-22 11:57:20 +0000272 for _, apiScope := range module.getActiveApiScopes() {
273 // Add dependencies to the stubs library
Jiyong Parke3ef3c82019-07-15 15:31:16 +0900274 if useBuiltStubs {
Paul Duffind1b3a922020-01-22 11:57:20 +0000275 ctx.AddVariationDependencies(nil, apiScope.stubsTag, module.stubsName(apiScope))
Jiyong Parke3ef3c82019-07-15 15:31:16 +0900276 }
Paul Duffind1b3a922020-01-22 11:57:20 +0000277
278 ctx.AddVariationDependencies(nil, apiScope.apiFileTag, module.docsName(apiScope))
Sundong Ahn054b19a2018-10-19 13:46:09 +0900279 }
280
Paul Duffine74ac732020-02-06 13:51:46 +0000281 if !proptools.Bool(module.sdkLibraryProperties.Api_only) {
282 // Add dependency to the rule for generating the xml permissions file
283 ctx.AddDependency(module, xmlPermissionsFileTag, module.genXmlPermissionsFileName())
284 }
285
Sundong Ahn054b19a2018-10-19 13:46:09 +0900286 module.Library.deps(ctx)
Jiyong Parkc678ad32018-04-10 13:07:10 +0900287}
288
Inseob Kimc0907f12019-02-08 21:00:45 +0900289func (module *SdkLibrary) GenerateAndroidBuildActions(ctx android.ModuleContext) {
Paul Duffin43db9be2019-12-30 17:35:49 +0000290 // Don't build an implementation library if this is api only.
291 if !proptools.Bool(module.sdkLibraryProperties.Api_only) {
292 module.Library.GenerateAndroidBuildActions(ctx)
293 }
Sundong Ahn054b19a2018-10-19 13:46:09 +0900294
Sundong Ahn57368eb2018-07-06 11:20:23 +0900295 // Record the paths to the header jars of the library (stubs and impl).
Paul Duffind1b3a922020-01-22 11:57:20 +0000296 // When this java_sdk_library is depended upon from others via "libs" property,
Jiyong Parkc678ad32018-04-10 13:07:10 +0900297 // the recorded paths will be returned depending on the link type of the caller.
298 ctx.VisitDirectDeps(func(to android.Module) {
299 otherName := ctx.OtherModuleName(to)
300 tag := ctx.OtherModuleDependencyTag(to)
301
Sundong Ahn57368eb2018-07-06 11:20:23 +0900302 if lib, ok := to.(Dependency); ok {
Paul Duffind1b3a922020-01-22 11:57:20 +0000303 if scopeTag, ok := tag.(scopeDependencyTag); ok {
304 apiScope := scopeTag.apiScope
305 scopePaths := module.getScopePaths(apiScope)
306 scopePaths.stubsHeaderPath = lib.HeaderJars()
307 scopePaths.stubsImplPath = lib.ImplementationJars()
Jiyong Parkc678ad32018-04-10 13:07:10 +0900308 }
309 }
Sundong Ahn20e998b2018-07-24 11:19:26 +0900310 if doc, ok := to.(ApiFilePath); ok {
Paul Duffind1b3a922020-01-22 11:57:20 +0000311 if scopeTag, ok := tag.(scopeDependencyTag); ok {
312 apiScope := scopeTag.apiScope
313 scopePaths := module.getScopePaths(apiScope)
314 scopePaths.apiFilePath = doc.ApiFilePath()
315 } else {
Sundong Ahn20e998b2018-07-24 11:19:26 +0900316 ctx.ModuleErrorf("depends on module %q of unknown tag %q", otherName, tag)
317 }
318 }
Paul Duffine74ac732020-02-06 13:51:46 +0000319 if tag == xmlPermissionsFileTag {
320 if genRule, ok := to.(genrule.SourceFileGenerator); ok {
321 pf := genRule.GeneratedSourceFiles()
322 if len(pf) != 1 {
323 ctx.ModuleErrorf("%q failed to generate permission XML", otherName)
324 } else {
325 module.permissionsFile = pf[0]
326 }
327 } else {
328 ctx.ModuleErrorf("depends on module %q to generate xml permissions file but it does not provide any outputs", otherName)
329 }
330 }
Jiyong Parkc678ad32018-04-10 13:07:10 +0900331 })
332}
333
Jiyong Park0b0e1b92019-12-03 13:24:29 +0900334func (module *SdkLibrary) AndroidMkEntries() []android.AndroidMkEntries {
Paul Duffin43db9be2019-12-30 17:35:49 +0000335 if proptools.Bool(module.sdkLibraryProperties.Api_only) {
336 return nil
337 }
Jiyong Park0b0e1b92019-12-03 13:24:29 +0900338 entriesList := module.Library.AndroidMkEntries()
339 entries := &entriesList[0]
Jaewoong Jungb0c127c2019-08-29 14:56:03 -0700340 entries.Required = append(entries.Required, module.xmlFileName())
Sundong Ahn054b19a2018-10-19 13:46:09 +0900341
Jaewoong Jungb0c127c2019-08-29 14:56:03 -0700342 entries.ExtraFooters = []android.AndroidMkExtraFootersFunc{
343 func(w io.Writer, name, prefix, moduleDir string, entries *android.AndroidMkEntries) {
Jaewoong Jungb0c127c2019-08-29 14:56:03 -0700344 if !Bool(module.sdkLibraryProperties.No_dist) {
345 // Create a phony module that installs the impl library, for the case when this lib is
346 // in PRODUCT_PACKAGES.
347 owner := module.ModuleBase.Owner()
348 if owner == "" {
349 if Bool(module.sdkLibraryProperties.Core_lib) {
350 owner = "core"
351 } else {
352 owner = "android"
353 }
354 }
Paul Duffind1b3a922020-01-22 11:57:20 +0000355
356 // Create dist rules to install the stubs libs and api files to the dist dir
357 for _, apiScope := range module.getActiveApiScopes() {
358 if scopePaths, ok := module.scopePaths[apiScope]; ok {
359 if len(scopePaths.stubsHeaderPath) == 1 {
360 fmt.Fprintln(w, "$(call dist-for-goals,sdk win_sdk,"+
361 scopePaths.stubsImplPath.Strings()[0]+
362 ":"+path.Join("apistubs", owner, apiScope.name,
363 module.BaseModuleName()+".jar")+")")
364 }
365 if scopePaths.apiFilePath != nil {
366 fmt.Fprintln(w, "$(call dist-for-goals,sdk win_sdk,"+
367 scopePaths.apiFilePath.String()+
368 ":"+path.Join("apistubs", owner, apiScope.name, "api",
369 module.BaseModuleName()+".txt")+")")
370 }
371 }
Sundong Ahn80a87b32019-05-13 15:02:50 +0900372 }
Sundong Ahn4fd04bb2018-08-31 18:01:37 +0900373 }
Jaewoong Jungb0c127c2019-08-29 14:56:03 -0700374 },
Jiyong Park82484c02018-04-23 21:41:26 +0900375 }
Jiyong Park0b0e1b92019-12-03 13:24:29 +0900376 return entriesList
Jiyong Park82484c02018-04-23 21:41:26 +0900377}
378
Jiyong Parkc678ad32018-04-10 13:07:10 +0900379// Module name of the stubs library
Paul Duffind1b3a922020-01-22 11:57:20 +0000380func (module *SdkLibrary) stubsName(apiScope *apiScope) string {
381 return apiScope.stubsModuleName(module.BaseModuleName())
Jiyong Parkc678ad32018-04-10 13:07:10 +0900382}
383
384// Module name of the docs
Paul Duffind1b3a922020-01-22 11:57:20 +0000385func (module *SdkLibrary) docsName(apiScope *apiScope) string {
386 return apiScope.docsModuleName(module.BaseModuleName())
Jiyong Parkc678ad32018-04-10 13:07:10 +0900387}
388
389// Module name of the runtime implementation library
Inseob Kimc0907f12019-02-08 21:00:45 +0900390func (module *SdkLibrary) implName() string {
Sundong Ahn054b19a2018-10-19 13:46:09 +0900391 return module.BaseModuleName()
Jiyong Parkc678ad32018-04-10 13:07:10 +0900392}
393
394// File path to the runtime implementation library
Inseob Kimc0907f12019-02-08 21:00:45 +0900395func (module *SdkLibrary) implPath() string {
Jooyung Han58f26ab2019-12-18 15:34:32 +0900396 if apexName := module.ApexName(); apexName != "" {
397 // TODO(b/146468504): ApexName() is only a soong module name, not apex name.
398 // In most cases, this works fine. But when apex_name is set or override_apex is used
399 // this can be wrong.
400 return fmt.Sprintf("/apex/%s/javalib/%s.jar", apexName, module.implName())
401 }
Jiyong Parkc678ad32018-04-10 13:07:10 +0900402 partition := "system"
403 if module.SocSpecific() {
404 partition = "vendor"
405 } else if module.DeviceSpecific() {
406 partition = "odm"
407 } else if module.ProductSpecific() {
408 partition = "product"
Sundong Ahn0d7dff42019-12-04 12:53:44 +0900409 } else if module.SystemExtSpecific() {
410 partition = "system_ext"
Jiyong Parkc678ad32018-04-10 13:07:10 +0900411 }
412 return "/" + partition + "/framework/" + module.implName() + ".jar"
413}
414
415// Module name of the XML file for the lib
Inseob Kimc0907f12019-02-08 21:00:45 +0900416func (module *SdkLibrary) xmlFileName() string {
Jiyong Parkc678ad32018-04-10 13:07:10 +0900417 return module.BaseModuleName() + sdkXmlFileSuffix
418}
419
Paul Duffine74ac732020-02-06 13:51:46 +0000420// Module name of the rule for generating the XML permissions file
421func (module *SdkLibrary) genXmlPermissionsFileName() string {
422 return "gen-" + module.BaseModuleName() + sdkXmlFileSuffix
423}
424
Paul Duffin12ceb462019-12-24 20:31:31 +0000425// Get the sdk version for use when compiling the stubs library.
Paul Duffind1b3a922020-01-22 11:57:20 +0000426func (module *SdkLibrary) sdkVersionForStubsLibrary(mctx android.LoadHookContext, apiScope *apiScope) string {
Paul Duffin12ceb462019-12-24 20:31:31 +0000427 sdkDep := decodeSdkDep(mctx, sdkContext(&module.Library))
428 if sdkDep.hasStandardLibs() {
429 // If building against a standard sdk then use the sdk version appropriate for the scope.
Paul Duffind1b3a922020-01-22 11:57:20 +0000430 return apiScope.sdkVersion
Paul Duffin12ceb462019-12-24 20:31:31 +0000431 } else {
432 // Otherwise, use no system module.
433 return "none"
434 }
435}
436
Jiyong Parkc678ad32018-04-10 13:07:10 +0900437// $(INTERNAL_PLATFORM_<apiTagName>_API_FILE) points to the generated
438// api file for the current source
439// TODO: remove this when apicheck is done in soong
Paul Duffind1b3a922020-01-22 11:57:20 +0000440func (module *SdkLibrary) apiTagName(apiScope *apiScope) string {
441 return strings.Replace(strings.ToUpper(module.BaseModuleName()), ".", "_", -1) + apiScope.apiFileMakeVariableSuffix
Jiyong Parkc678ad32018-04-10 13:07:10 +0900442}
443
Paul Duffind1b3a922020-01-22 11:57:20 +0000444func (module *SdkLibrary) latestApiFilegroupName(apiScope *apiScope) string {
445 return ":" + module.BaseModuleName() + ".api." + apiScope.name + ".latest"
Jiyong Park58c518b2018-05-12 22:29:12 +0900446}
Jiyong Parkc678ad32018-04-10 13:07:10 +0900447
Paul Duffind1b3a922020-01-22 11:57:20 +0000448func (module *SdkLibrary) latestRemovedApiFilegroupName(apiScope *apiScope) string {
449 return ":" + module.BaseModuleName() + "-removed.api." + apiScope.name + ".latest"
Jiyong Parkc678ad32018-04-10 13:07:10 +0900450}
451
452// Creates a static java library that has API stubs
Paul Duffind1b3a922020-01-22 11:57:20 +0000453func (module *SdkLibrary) createStubsLibrary(mctx android.LoadHookContext, apiScope *apiScope) {
Jiyong Parkc678ad32018-04-10 13:07:10 +0900454 props := struct {
Sundong Ahn0d7dff42019-12-04 12:53:44 +0900455 Name *string
456 Srcs []string
Paul Duffin367ab912019-12-23 19:40:36 +0000457 Installable *bool
Sundong Ahn0d7dff42019-12-04 12:53:44 +0900458 Sdk_version *string
Paul Duffin12ceb462019-12-24 20:31:31 +0000459 System_modules *string
Sundong Ahn0d7dff42019-12-04 12:53:44 +0900460 Libs []string
461 Soc_specific *bool
462 Device_specific *bool
463 Product_specific *bool
464 System_ext_specific *bool
465 Compile_dex *bool
Sundong Ahn0d7dff42019-12-04 12:53:44 +0900466 Java_version *string
467 Product_variables struct {
Jiyong Parkc678ad32018-04-10 13:07:10 +0900468 Unbundled_build struct {
469 Enabled *bool
470 }
Jiyong Park82484c02018-04-23 21:41:26 +0900471 Pdk struct {
472 Enabled *bool
473 }
Jiyong Parkc678ad32018-04-10 13:07:10 +0900474 }
Sundong Ahn054b19a2018-10-19 13:46:09 +0900475 Openjdk9 struct {
476 Srcs []string
477 Javacflags []string
478 }
Jiyong Parkc678ad32018-04-10 13:07:10 +0900479 }{}
480
Jiyong Parkdf130542018-04-27 16:29:21 +0900481 props.Name = proptools.StringPtr(module.stubsName(apiScope))
Jiyong Parkc678ad32018-04-10 13:07:10 +0900482 // sources are generated from the droiddoc
Jiyong Parkdf130542018-04-27 16:29:21 +0900483 props.Srcs = []string{":" + module.docsName(apiScope)}
Paul Duffin12ceb462019-12-24 20:31:31 +0000484 sdkVersion := module.sdkVersionForStubsLibrary(mctx, apiScope)
Paul Duffin52d398a2019-06-11 12:31:14 +0100485 props.Sdk_version = proptools.StringPtr(sdkVersion)
Paul Duffin12ceb462019-12-24 20:31:31 +0000486 props.System_modules = module.Library.Module.deviceProperties.System_modules
Paul Duffin367ab912019-12-23 19:40:36 +0000487 props.Installable = proptools.BoolPtr(false)
Sundong Ahn054b19a2018-10-19 13:46:09 +0900488 props.Libs = module.sdkLibraryProperties.Stub_only_libs
Jiyong Parkc678ad32018-04-10 13:07:10 +0900489 // Unbundled apps will use the prebult one from /prebuilts/sdk
Colin Cross10932872019-04-18 14:27:12 -0700490 if mctx.Config().UnbundledBuildUsePrebuiltSdks() {
Colin Cross2c77ceb2019-01-21 11:56:21 -0800491 props.Product_variables.Unbundled_build.Enabled = proptools.BoolPtr(false)
492 }
Jiyong Park82484c02018-04-23 21:41:26 +0900493 props.Product_variables.Pdk.Enabled = proptools.BoolPtr(false)
Sundong Ahn054b19a2018-10-19 13:46:09 +0900494 props.Openjdk9.Srcs = module.Library.Module.properties.Openjdk9.Srcs
495 props.Openjdk9.Javacflags = module.Library.Module.properties.Openjdk9.Javacflags
496 props.Java_version = module.Library.Module.properties.Java_version
497 if module.Library.Module.deviceProperties.Compile_dex != nil {
498 props.Compile_dex = module.Library.Module.deviceProperties.Compile_dex
Sundong Ahndd567f92018-07-31 17:19:11 +0900499 }
Jiyong Parkc678ad32018-04-10 13:07:10 +0900500
501 if module.SocSpecific() {
502 props.Soc_specific = proptools.BoolPtr(true)
503 } else if module.DeviceSpecific() {
504 props.Device_specific = proptools.BoolPtr(true)
505 } else if module.ProductSpecific() {
506 props.Product_specific = proptools.BoolPtr(true)
Sundong Ahn0d7dff42019-12-04 12:53:44 +0900507 } else if module.SystemExtSpecific() {
508 props.System_ext_specific = proptools.BoolPtr(true)
Jiyong Parkc678ad32018-04-10 13:07:10 +0900509 }
510
Colin Cross84dfc3d2019-09-25 11:33:01 -0700511 mctx.CreateModule(LibraryFactory, &props)
Jiyong Parkc678ad32018-04-10 13:07:10 +0900512}
513
514// Creates a droiddoc module that creates stubs source files from the given full source
515// files
Paul Duffind1b3a922020-01-22 11:57:20 +0000516func (module *SdkLibrary) createStubsSources(mctx android.LoadHookContext, apiScope *apiScope) {
Jiyong Parkc678ad32018-04-10 13:07:10 +0900517 props := struct {
Sundong Ahn054b19a2018-10-19 13:46:09 +0900518 Name *string
519 Srcs []string
520 Installable *bool
Paul Duffin52d398a2019-06-11 12:31:14 +0100521 Sdk_version *string
Paul Duffin12ceb462019-12-24 20:31:31 +0000522 System_modules *string
Sundong Ahn054b19a2018-10-19 13:46:09 +0900523 Libs []string
Paul Duffin11512472019-02-11 15:55:17 +0000524 Arg_files []string
Sundong Ahn054b19a2018-10-19 13:46:09 +0900525 Args *string
526 Api_tag_name *string
527 Api_filename *string
528 Removed_api_filename *string
Sundong Ahn054b19a2018-10-19 13:46:09 +0900529 Java_version *string
530 Merge_annotations_dirs []string
531 Merge_inclusion_annotations_dirs []string
532 Check_api struct {
Inseob Kim38449af2019-02-28 14:24:05 +0900533 Current ApiToCheck
534 Last_released ApiToCheck
535 Ignore_missing_latest_api *bool
Jiyong Park58c518b2018-05-12 22:29:12 +0900536 }
Sundong Ahn1b92c822018-05-29 11:35:17 +0900537 Aidl struct {
538 Include_dirs []string
539 Local_include_dirs []string
540 }
Jiyong Parkc678ad32018-04-10 13:07:10 +0900541 }{}
542
Paul Duffin250e6192019-06-07 10:44:37 +0100543 sdkDep := decodeSdkDep(mctx, sdkContext(&module.Library))
Paul Duffin12ceb462019-12-24 20:31:31 +0000544 // Use the platform API if standard libraries were requested, otherwise use
545 // no default libraries.
Paul Duffin52d398a2019-06-11 12:31:14 +0100546 sdkVersion := ""
547 if !sdkDep.hasStandardLibs() {
548 sdkVersion = "none"
549 }
Paul Duffin250e6192019-06-07 10:44:37 +0100550
Jiyong Parkdf130542018-04-27 16:29:21 +0900551 props.Name = proptools.StringPtr(module.docsName(apiScope))
Sundong Ahn054b19a2018-10-19 13:46:09 +0900552 props.Srcs = append(props.Srcs, module.Library.Module.properties.Srcs...)
Paul Duffin52d398a2019-06-11 12:31:14 +0100553 props.Sdk_version = proptools.StringPtr(sdkVersion)
Paul Duffin12ceb462019-12-24 20:31:31 +0000554 props.System_modules = module.Library.Module.deviceProperties.System_modules
Jiyong Parkc678ad32018-04-10 13:07:10 +0900555 props.Installable = proptools.BoolPtr(false)
Sundong Ahne6f0b052018-06-05 16:46:14 +0900556 // A droiddoc module has only one Libs property and doesn't distinguish between
557 // shared libs and static libs. So we need to add both of these libs to Libs property.
Sundong Ahn054b19a2018-10-19 13:46:09 +0900558 props.Libs = module.Library.Module.properties.Libs
559 props.Libs = append(props.Libs, module.Library.Module.properties.Static_libs...)
560 props.Aidl.Include_dirs = module.Library.Module.deviceProperties.Aidl.Include_dirs
561 props.Aidl.Local_include_dirs = module.Library.Module.deviceProperties.Aidl.Local_include_dirs
Sundong Ahn054b19a2018-10-19 13:46:09 +0900562 props.Java_version = module.Library.Module.properties.Java_version
Jiyong Parkc678ad32018-04-10 13:07:10 +0900563
Sundong Ahn054b19a2018-10-19 13:46:09 +0900564 props.Merge_annotations_dirs = module.sdkLibraryProperties.Merge_annotations_dirs
565 props.Merge_inclusion_annotations_dirs = module.sdkLibraryProperties.Merge_inclusion_annotations_dirs
566
Paul Duffin235ffff2019-12-24 10:41:30 +0000567 droiddocArgs := []string{}
568 if len(module.sdkLibraryProperties.Api_packages) != 0 {
569 droiddocArgs = append(droiddocArgs, "--stub-packages "+strings.Join(module.sdkLibraryProperties.Api_packages, ":"))
570 }
571 if len(module.sdkLibraryProperties.Hidden_api_packages) != 0 {
572 droiddocArgs = append(droiddocArgs,
573 android.JoinWithPrefix(module.sdkLibraryProperties.Hidden_api_packages, " --hide-package "))
574 }
575 droiddocArgs = append(droiddocArgs, module.sdkLibraryProperties.Droiddoc_options...)
576 disabledWarnings := []string{
577 "MissingPermission",
578 "BroadcastBehavior",
579 "HiddenSuperclass",
580 "DeprecationMismatch",
581 "UnavailableSymbol",
582 "SdkConstant",
583 "HiddenTypeParameter",
584 "Todo",
585 "Typo",
586 }
587 droiddocArgs = append(droiddocArgs, android.JoinWithPrefix(disabledWarnings, "--hide "))
Sundong Ahnfb2721f2018-09-17 13:23:09 +0900588
Jiyong Parkdf130542018-04-27 16:29:21 +0900589 switch apiScope {
590 case apiScopeSystem:
Paul Duffin235ffff2019-12-24 10:41:30 +0000591 droiddocArgs = append(droiddocArgs, "-showAnnotation android.annotation.SystemApi")
Jiyong Parkdf130542018-04-27 16:29:21 +0900592 case apiScopeTest:
Paul Duffin235ffff2019-12-24 10:41:30 +0000593 droiddocArgs = append(droiddocArgs, " -showAnnotation android.annotation.TestApi")
Jiyong Parkc678ad32018-04-10 13:07:10 +0900594 }
Paul Duffin11512472019-02-11 15:55:17 +0000595 props.Arg_files = module.sdkLibraryProperties.Droiddoc_option_files
Paul Duffin235ffff2019-12-24 10:41:30 +0000596 props.Args = proptools.StringPtr(strings.Join(droiddocArgs, " "))
Jiyong Parkc678ad32018-04-10 13:07:10 +0900597
598 // List of APIs identified from the provided source files are created. They are later
599 // compared against to the not-yet-released (a.k.a current) list of APIs and to the
600 // last-released (a.k.a numbered) list of API.
Paul Duffind1b3a922020-01-22 11:57:20 +0000601 currentApiFileName := apiScope.apiFilePrefix + "current.txt"
602 removedApiFileName := apiScope.apiFilePrefix + "removed.txt"
Paul Duffin749f98f2019-12-30 17:23:46 +0000603 apiDir := module.getApiDir()
604 currentApiFileName = path.Join(apiDir, currentApiFileName)
605 removedApiFileName = path.Join(apiDir, removedApiFileName)
Jiyong Park58c518b2018-05-12 22:29:12 +0900606 // TODO(jiyong): remove these three props
Jiyong Parkdf130542018-04-27 16:29:21 +0900607 props.Api_tag_name = proptools.StringPtr(module.apiTagName(apiScope))
Jiyong Parkc678ad32018-04-10 13:07:10 +0900608 props.Api_filename = proptools.StringPtr(currentApiFileName)
609 props.Removed_api_filename = proptools.StringPtr(removedApiFileName)
610
Jiyong Park58c518b2018-05-12 22:29:12 +0900611 // check against the not-yet-release API
612 props.Check_api.Current.Api_file = proptools.StringPtr(currentApiFileName)
613 props.Check_api.Current.Removed_api_file = proptools.StringPtr(removedApiFileName)
Jiyong Park58c518b2018-05-12 22:29:12 +0900614
615 // check against the latest released API
616 props.Check_api.Last_released.Api_file = proptools.StringPtr(
617 module.latestApiFilegroupName(apiScope))
618 props.Check_api.Last_released.Removed_api_file = proptools.StringPtr(
619 module.latestRemovedApiFilegroupName(apiScope))
Inseob Kim38449af2019-02-28 14:24:05 +0900620 props.Check_api.Ignore_missing_latest_api = proptools.BoolPtr(true)
Jiyong Park58c518b2018-05-12 22:29:12 +0900621
Colin Cross84dfc3d2019-09-25 11:33:01 -0700622 mctx.CreateModule(DroidstubsFactory, &props)
Jiyong Parkc678ad32018-04-10 13:07:10 +0900623}
624
Paul Duffine74ac732020-02-06 13:51:46 +0000625func (module *SdkLibrary) XmlPermissionsFile() android.Path {
626 return module.permissionsFile
627}
628
629func (module *SdkLibrary) XmlPermissionsFileContent() string {
630 return fmt.Sprintf(permissionsTemplate, module.BaseModuleName(), module.implPath())
631}
632
Jiyong Parkc678ad32018-04-10 13:07:10 +0900633// Creates the xml file that publicizes the runtime library
Colin Crossf8b860a2019-04-16 14:43:28 -0700634func (module *SdkLibrary) createXmlFile(mctx android.LoadHookContext) {
Paul Duffine74ac732020-02-06 13:51:46 +0000635
636 xmlContent := module.XmlPermissionsFileContent()
637
638 genRuleName := module.genXmlPermissionsFileName()
639
640 // Create a genrule module to create the XML permissions file.
641 genRuleProps := struct {
642 Name *string
643 Cmd *string
644 Out []string
645 }{
646 Name: proptools.StringPtr(genRuleName),
647 Cmd: proptools.StringPtr("echo -e '" + xmlContent + "' > '$(out)'"),
648 Out: []string{module.xmlFileName()},
649 }
650
651 mctx.CreateModule(genrule.GenRuleFactory, &genRuleProps)
652
Jiyong Parkc678ad32018-04-10 13:07:10 +0900653 // creates a prebuilt_etc module to actually place the xml file under
654 // <partition>/etc/permissions
655 etcProps := struct {
Sundong Ahn0d7dff42019-12-04 12:53:44 +0900656 Name *string
657 Src *string
658 Sub_dir *string
659 Soc_specific *bool
660 Device_specific *bool
661 Product_specific *bool
662 System_ext_specific *bool
Jiyong Parkc678ad32018-04-10 13:07:10 +0900663 }{}
664 etcProps.Name = proptools.StringPtr(module.xmlFileName())
Paul Duffine74ac732020-02-06 13:51:46 +0000665 etcProps.Src = proptools.StringPtr(":" + genRuleName)
Jiyong Parkc678ad32018-04-10 13:07:10 +0900666 etcProps.Sub_dir = proptools.StringPtr("permissions")
667 if module.SocSpecific() {
668 etcProps.Soc_specific = proptools.BoolPtr(true)
669 } else if module.DeviceSpecific() {
670 etcProps.Device_specific = proptools.BoolPtr(true)
671 } else if module.ProductSpecific() {
672 etcProps.Product_specific = proptools.BoolPtr(true)
Sundong Ahn0d7dff42019-12-04 12:53:44 +0900673 } else if module.SystemExtSpecific() {
674 etcProps.System_ext_specific = proptools.BoolPtr(true)
Jiyong Parkc678ad32018-04-10 13:07:10 +0900675 }
Colin Cross84dfc3d2019-09-25 11:33:01 -0700676 mctx.CreateModule(android.PrebuiltEtcFactory, &etcProps)
Jiyong Parkc678ad32018-04-10 13:07:10 +0900677}
678
Jiyong Park6a927c42020-01-21 02:03:43 +0900679func (module *SdkLibrary) PrebuiltJars(ctx android.BaseModuleContext, s sdkSpec) android.Paths {
680 var ver sdkVersion
681 var kind sdkKind
682 if s.usePrebuilt(ctx) {
683 ver = s.version
684 kind = s.kind
Jiyong Parkc678ad32018-04-10 13:07:10 +0900685 } else {
Jiyong Park6a927c42020-01-21 02:03:43 +0900686 // We don't have prebuilt SDK for the specific sdkVersion.
687 // Instead of breaking the build, fallback to use "system_current"
688 ver = sdkVersionCurrent
689 kind = sdkSystem
Sundong Ahn054b19a2018-10-19 13:46:09 +0900690 }
Jiyong Park6a927c42020-01-21 02:03:43 +0900691
692 dir := filepath.Join("prebuilts", "sdk", ver.String(), kind.String())
Sundong Ahn054b19a2018-10-19 13:46:09 +0900693 jar := filepath.Join(dir, module.BaseModuleName()+".jar")
694 jarPath := android.ExistentPathForSource(ctx, jar)
Sundong Ahnae418ac2019-02-28 15:01:28 +0900695 if !jarPath.Valid() {
Colin Cross07c88562020-01-07 09:34:44 -0800696 if ctx.Config().AllowMissingDependencies() {
697 return android.Paths{android.PathForSource(ctx, jar)}
698 } else {
Jiyong Park6a927c42020-01-21 02:03:43 +0900699 ctx.PropertyErrorf("sdk_library", "invalid sdk version %q, %q does not exist", s.raw, jar)
Colin Cross07c88562020-01-07 09:34:44 -0800700 }
Sundong Ahnae418ac2019-02-28 15:01:28 +0900701 return nil
702 }
Sundong Ahn054b19a2018-10-19 13:46:09 +0900703 return android.Paths{jarPath.Path()}
704}
705
Paul Duffind1b3a922020-01-22 11:57:20 +0000706func (module *SdkLibrary) sdkJars(
707 ctx android.BaseModuleContext,
708 sdkVersion sdkSpec,
709 headerJars bool) android.Paths {
710
Paul Duffina2db18f2020-01-22 17:11:15 +0000711 // If a specific numeric version has been requested or the build is explicitly configured
712 // for it then use prebuilt versions of the sdk.
713 if sdkVersion.version.isNumbered() || ctx.Config().UnbundledBuildUsePrebuiltSdks() {
Sundong Ahn054b19a2018-10-19 13:46:09 +0900714 return module.PrebuiltJars(ctx, sdkVersion)
715 } else {
Paul Duffind1b3a922020-01-22 11:57:20 +0000716 if !sdkVersion.specified() {
717 if headerJars {
718 return module.Library.HeaderJars()
719 } else {
720 return module.Library.ImplementationJars()
721 }
722 }
Paul Duffin726d23c2020-01-22 16:30:37 +0000723 var apiScope *apiScope
Jiyong Park6a927c42020-01-21 02:03:43 +0900724 switch sdkVersion.kind {
725 case sdkSystem:
Paul Duffin726d23c2020-01-22 16:30:37 +0000726 apiScope = apiScopeSystem
727 case sdkTest:
728 apiScope = apiScopeTest
Jiyong Park6a927c42020-01-21 02:03:43 +0900729 case sdkPrivate:
Sundong Ahn054b19a2018-10-19 13:46:09 +0900730 return module.Library.HeaderJars()
Jiyong Park6a927c42020-01-21 02:03:43 +0900731 default:
Paul Duffin726d23c2020-01-22 16:30:37 +0000732 apiScope = apiScopePublic
Paul Duffind1b3a922020-01-22 11:57:20 +0000733 }
734
Paul Duffin726d23c2020-01-22 16:30:37 +0000735 paths := module.getScopePaths(apiScope)
Paul Duffind1b3a922020-01-22 11:57:20 +0000736 if headerJars {
737 return paths.stubsHeaderPath
738 } else {
739 return paths.stubsImplPath
Sundong Ahn054b19a2018-10-19 13:46:09 +0900740 }
Jiyong Parkc678ad32018-04-10 13:07:10 +0900741 }
742}
743
Sundong Ahn241cd372018-07-13 16:16:44 +0900744// to satisfy SdkLibraryDependency interface
Paul Duffind1b3a922020-01-22 11:57:20 +0000745func (module *SdkLibrary) SdkHeaderJars(ctx android.BaseModuleContext, sdkVersion sdkSpec) android.Paths {
746 return module.sdkJars(ctx, sdkVersion, true /*headerJars*/)
747}
748
749// to satisfy SdkLibraryDependency interface
Jiyong Park6a927c42020-01-21 02:03:43 +0900750func (module *SdkLibrary) SdkImplementationJars(ctx android.BaseModuleContext, sdkVersion sdkSpec) android.Paths {
Paul Duffind1b3a922020-01-22 11:57:20 +0000751 return module.sdkJars(ctx, sdkVersion, false /*headerJars*/)
Sundong Ahn241cd372018-07-13 16:16:44 +0900752}
753
Sundong Ahn80a87b32019-05-13 15:02:50 +0900754func (module *SdkLibrary) SetNoDist() {
755 module.sdkLibraryProperties.No_dist = proptools.BoolPtr(true)
756}
757
Colin Cross571cccf2019-02-04 11:22:08 -0800758var javaSdkLibrariesKey = android.NewOnceKey("javaSdkLibraries")
759
Jiyong Park82484c02018-04-23 21:41:26 +0900760func javaSdkLibraries(config android.Config) *[]string {
Colin Cross571cccf2019-02-04 11:22:08 -0800761 return config.Once(javaSdkLibrariesKey, func() interface{} {
Jiyong Park82484c02018-04-23 21:41:26 +0900762 return &[]string{}
763 }).(*[]string)
764}
765
Paul Duffin749f98f2019-12-30 17:23:46 +0000766func (module *SdkLibrary) getApiDir() string {
767 return proptools.StringDefault(module.sdkLibraryProperties.Api_dir, "api")
768}
769
Jiyong Parkc678ad32018-04-10 13:07:10 +0900770// For a java_sdk_library module, create internal modules for stubs, docs,
771// runtime libs and xml file. If requested, the stubs and docs are created twice
772// once for public API level and once for system API level
Colin Crossf8b860a2019-04-16 14:43:28 -0700773func (module *SdkLibrary) CreateInternalModules(mctx android.LoadHookContext) {
Inseob Kim6e93ac92019-03-21 17:43:49 +0900774 if len(module.Library.Module.properties.Srcs) == 0 {
Inseob Kimc0907f12019-02-08 21:00:45 +0900775 mctx.PropertyErrorf("srcs", "java_sdk_library must specify srcs")
Jooyung Han58f26ab2019-12-18 15:34:32 +0900776 return
Inseob Kimc0907f12019-02-08 21:00:45 +0900777 }
778
Paul Duffin37e0b772019-12-30 17:20:10 +0000779 // If this builds against standard libraries (i.e. is not part of the core libraries)
780 // then assume it provides both system and test apis. Otherwise, assume it does not and
781 // also assume it does not contribute to the dist build.
782 sdkDep := decodeSdkDep(mctx, sdkContext(&module.Library))
783 hasSystemAndTestApis := sdkDep.hasStandardLibs()
784 module.sdkLibraryProperties.Has_system_and_test_apis = hasSystemAndTestApis
785 module.sdkLibraryProperties.No_dist = proptools.BoolPtr(!hasSystemAndTestApis)
786
Inseob Kim8098faa2019-03-18 10:19:51 +0900787 missing_current_api := false
788
Paul Duffind1b3a922020-01-22 11:57:20 +0000789 activeScopes := module.getActiveApiScopes()
790
Paul Duffin749f98f2019-12-30 17:23:46 +0000791 apiDir := module.getApiDir()
Paul Duffind1b3a922020-01-22 11:57:20 +0000792 for _, scope := range activeScopes {
Inseob Kim8098faa2019-03-18 10:19:51 +0900793 for _, api := range []string{"current.txt", "removed.txt"} {
Paul Duffind1b3a922020-01-22 11:57:20 +0000794 path := path.Join(mctx.ModuleDir(), apiDir, scope.apiFilePrefix+api)
Inseob Kim8098faa2019-03-18 10:19:51 +0900795 p := android.ExistentPathForSource(mctx, path)
796 if !p.Valid() {
797 mctx.ModuleErrorf("Current api file %#v doesn't exist", path)
798 missing_current_api = true
799 }
800 }
801 }
802
803 if missing_current_api {
804 script := "build/soong/scripts/gen-java-current-api-files.sh"
805 p := android.ExistentPathForSource(mctx, script)
806
807 if !p.Valid() {
808 panic(fmt.Sprintf("script file %s doesn't exist", script))
809 }
810
811 mctx.ModuleErrorf("One or more current api files are missing. "+
812 "You can update them by:\n"+
Paul Duffin37e0b772019-12-30 17:20:10 +0000813 "%s %q %s && m update-api",
Paul Duffind1b3a922020-01-22 11:57:20 +0000814 script, filepath.Join(mctx.ModuleDir(), apiDir),
815 strings.Join(activeScopes.Strings(func(s *apiScope) string { return s.apiFilePrefix }), " "))
Inseob Kim8098faa2019-03-18 10:19:51 +0900816 return
817 }
818
Paul Duffind1b3a922020-01-22 11:57:20 +0000819 for _, scope := range activeScopes {
820 module.createStubsLibrary(mctx, scope)
821 module.createStubsSources(mctx, scope)
Inseob Kimc0907f12019-02-08 21:00:45 +0900822 }
823
Paul Duffin43db9be2019-12-30 17:35:49 +0000824 if !proptools.Bool(module.sdkLibraryProperties.Api_only) {
825 // for runtime
826 module.createXmlFile(mctx)
827
828 // record java_sdk_library modules so that they are exported to make
829 javaSdkLibraries := javaSdkLibraries(mctx.Config())
830 javaSdkLibrariesLock.Lock()
831 defer javaSdkLibrariesLock.Unlock()
832 *javaSdkLibraries = append(*javaSdkLibraries, module.BaseModuleName())
833 }
Inseob Kimc0907f12019-02-08 21:00:45 +0900834}
835
836func (module *SdkLibrary) InitSdkLibraryProperties() {
Sundong Ahn054b19a2018-10-19 13:46:09 +0900837 module.AddProperties(
838 &module.sdkLibraryProperties,
839 &module.Library.Module.properties,
840 &module.Library.Module.dexpreoptProperties,
841 &module.Library.Module.deviceProperties,
842 &module.Library.Module.protoProperties,
843 )
844
845 module.Library.Module.properties.Installable = proptools.BoolPtr(true)
846 module.Library.Module.deviceProperties.IsSDKLibrary = true
Inseob Kimc0907f12019-02-08 21:00:45 +0900847}
Sundong Ahn054b19a2018-10-19 13:46:09 +0900848
Jaewoong Jung4f158ee2019-07-11 10:05:35 -0700849// java_sdk_library is a special Java library that provides optional platform APIs to apps.
850// In practice, it can be viewed as a combination of several modules: 1) stubs library that clients
851// are linked against to, 2) droiddoc module that internally generates API stubs source files,
852// 3) the real runtime shared library that implements the APIs, and 4) XML file for adding
853// the runtime lib to the classpath at runtime if requested via <uses-library>.
Inseob Kimc0907f12019-02-08 21:00:45 +0900854func SdkLibraryFactory() android.Module {
855 module := &SdkLibrary{}
856 module.InitSdkLibraryProperties()
Jooyung Han58f26ab2019-12-18 15:34:32 +0900857 android.InitApexModule(module)
Sundong Ahn054b19a2018-10-19 13:46:09 +0900858 InitJavaModule(module, android.HostAndDeviceSupported)
Colin Crossf8b860a2019-04-16 14:43:28 -0700859 android.AddLoadHook(module, func(ctx android.LoadHookContext) { module.CreateInternalModules(ctx) })
Jiyong Parkc678ad32018-04-10 13:07:10 +0900860 return module
861}
Colin Cross79c7c262019-04-17 11:11:46 -0700862
863//
864// SDK library prebuilts
865//
866
Paul Duffin56d44902020-01-31 13:36:25 +0000867// Properties associated with each api scope.
868type sdkLibraryScopeProperties struct {
Colin Cross79c7c262019-04-17 11:11:46 -0700869 Jars []string `android:"path"`
870
871 Sdk_version *string
872
Colin Cross79c7c262019-04-17 11:11:46 -0700873 // List of shared java libs that this module has dependencies to
874 Libs []string
Colin Cross79c7c262019-04-17 11:11:46 -0700875}
876
Paul Duffin56d44902020-01-31 13:36:25 +0000877type sdkLibraryImportProperties struct {
Paul Duffinfcfd7912020-01-31 17:54:30 +0000878 // List of shared java libs, common to all scopes, that this module has
879 // dependencies to
880 Libs []string
881
Paul Duffin56d44902020-01-31 13:36:25 +0000882 // Properties associated with the public api scope.
883 Public sdkLibraryScopeProperties
884
885 // Properties associated with the system api scope.
886 System sdkLibraryScopeProperties
887
888 // Properties associated with the test api scope.
889 Test sdkLibraryScopeProperties
890}
891
Colin Cross79c7c262019-04-17 11:11:46 -0700892type sdkLibraryImport struct {
893 android.ModuleBase
894 android.DefaultableModuleBase
895 prebuilt android.Prebuilt
896
897 properties sdkLibraryImportProperties
898
Paul Duffin56d44902020-01-31 13:36:25 +0000899 commonToSdkLibraryAndImport
Colin Cross79c7c262019-04-17 11:11:46 -0700900}
901
902var _ SdkLibraryDependency = (*sdkLibraryImport)(nil)
903
Jaewoong Jung4f158ee2019-07-11 10:05:35 -0700904// java_sdk_library_import imports a prebuilt java_sdk_library.
Colin Cross79c7c262019-04-17 11:11:46 -0700905func sdkLibraryImportFactory() android.Module {
906 module := &sdkLibraryImport{}
907
Paul Duffinfcfd7912020-01-31 17:54:30 +0000908 module.AddProperties(&module.properties)
Colin Cross79c7c262019-04-17 11:11:46 -0700909
Paul Duffinfcfd7912020-01-31 17:54:30 +0000910 android.InitPrebuiltModule(module, &[]string{})
Colin Cross79c7c262019-04-17 11:11:46 -0700911 InitJavaModule(module, android.HostAndDeviceSupported)
912
913 android.AddLoadHook(module, func(mctx android.LoadHookContext) { module.createInternalModules(mctx) })
914 return module
915}
916
917func (module *sdkLibraryImport) Prebuilt() *android.Prebuilt {
918 return &module.prebuilt
919}
920
921func (module *sdkLibraryImport) Name() string {
922 return module.prebuilt.Name(module.ModuleBase.Name())
923}
924
925func (module *sdkLibraryImport) createInternalModules(mctx android.LoadHookContext) {
Colin Cross79c7c262019-04-17 11:11:46 -0700926
Paul Duffin56d44902020-01-31 13:36:25 +0000927 for apiScope, scopeProperties := range module.scopeProperties() {
928 if len(scopeProperties.Jars) == 0 {
929 continue
930 }
931
932 // Creates a java import for the jar with ".stubs" suffix
933 props := struct {
934 Name *string
935 Soc_specific *bool
936 Device_specific *bool
937 Product_specific *bool
938 System_ext_specific *bool
939 Sdk_version *string
940 Libs []string
941 Jars []string
942 }{}
943
944 props.Name = proptools.StringPtr(apiScope.stubsModuleName(module.BaseModuleName()))
945 props.Sdk_version = scopeProperties.Sdk_version
Paul Duffinfcfd7912020-01-31 17:54:30 +0000946 // Prepend any of the libs from the legacy public properties to the libs for each of the
947 // scopes to avoid having to duplicate them in each scope.
948 props.Libs = append(module.properties.Libs, scopeProperties.Libs...)
Paul Duffin56d44902020-01-31 13:36:25 +0000949 props.Jars = scopeProperties.Jars
950
951 if module.SocSpecific() {
952 props.Soc_specific = proptools.BoolPtr(true)
953 } else if module.DeviceSpecific() {
954 props.Device_specific = proptools.BoolPtr(true)
955 } else if module.ProductSpecific() {
956 props.Product_specific = proptools.BoolPtr(true)
957 } else if module.SystemExtSpecific() {
958 props.System_ext_specific = proptools.BoolPtr(true)
959 }
960
961 mctx.CreateModule(ImportFactory, &props)
962 }
Colin Cross79c7c262019-04-17 11:11:46 -0700963
964 javaSdkLibraries := javaSdkLibraries(mctx.Config())
965 javaSdkLibrariesLock.Lock()
966 defer javaSdkLibrariesLock.Unlock()
967 *javaSdkLibraries = append(*javaSdkLibraries, module.BaseModuleName())
968}
969
Paul Duffin56d44902020-01-31 13:36:25 +0000970func (module *sdkLibraryImport) scopeProperties() map[*apiScope]*sdkLibraryScopeProperties {
971 p := make(map[*apiScope]*sdkLibraryScopeProperties)
972 p[apiScopePublic] = &module.properties.Public
973 p[apiScopeSystem] = &module.properties.System
974 p[apiScopeTest] = &module.properties.Test
975 return p
976}
977
Colin Cross79c7c262019-04-17 11:11:46 -0700978func (module *sdkLibraryImport) DepsMutator(ctx android.BottomUpMutatorContext) {
Paul Duffin56d44902020-01-31 13:36:25 +0000979 for apiScope, scopeProperties := range module.scopeProperties() {
980 if len(scopeProperties.Jars) == 0 {
981 continue
982 }
983
984 // Add dependencies to the prebuilt stubs library
985 ctx.AddVariationDependencies(nil, apiScope.stubsTag, apiScope.stubsModuleName(module.BaseModuleName()))
986 }
Colin Cross79c7c262019-04-17 11:11:46 -0700987}
988
989func (module *sdkLibraryImport) GenerateAndroidBuildActions(ctx android.ModuleContext) {
990 // Record the paths to the prebuilt stubs library.
991 ctx.VisitDirectDeps(func(to android.Module) {
992 tag := ctx.OtherModuleDependencyTag(to)
993
Paul Duffin56d44902020-01-31 13:36:25 +0000994 if lib, ok := to.(Dependency); ok {
995 if scopeTag, ok := tag.(scopeDependencyTag); ok {
996 apiScope := scopeTag.apiScope
997 scopePaths := module.getScopePaths(apiScope)
998 scopePaths.stubsHeaderPath = lib.HeaderJars()
999 }
Colin Cross79c7c262019-04-17 11:11:46 -07001000 }
1001 })
1002}
1003
Paul Duffin56d44902020-01-31 13:36:25 +00001004func (module *sdkLibraryImport) sdkJars(
1005 ctx android.BaseModuleContext,
1006 sdkVersion sdkSpec) android.Paths {
1007
1008 var apiScope *apiScope
1009 switch sdkVersion.kind {
1010 case sdkSystem:
1011 apiScope = apiScopeSystem
1012 case sdkTest:
1013 apiScope = apiScopeTest
1014 default:
1015 apiScope = apiScopePublic
1016 }
1017
1018 paths := module.getScopePaths(apiScope)
1019 return paths.stubsHeaderPath
1020}
1021
Colin Cross79c7c262019-04-17 11:11:46 -07001022// to satisfy SdkLibraryDependency interface
Jiyong Park6a927c42020-01-21 02:03:43 +09001023func (module *sdkLibraryImport) SdkHeaderJars(ctx android.BaseModuleContext, sdkVersion sdkSpec) android.Paths {
Colin Cross79c7c262019-04-17 11:11:46 -07001024 // This module is just a wrapper for the prebuilt stubs.
Paul Duffin56d44902020-01-31 13:36:25 +00001025 return module.sdkJars(ctx, sdkVersion)
Colin Cross79c7c262019-04-17 11:11:46 -07001026}
1027
1028// to satisfy SdkLibraryDependency interface
Jiyong Park6a927c42020-01-21 02:03:43 +09001029func (module *sdkLibraryImport) SdkImplementationJars(ctx android.BaseModuleContext, sdkVersion sdkSpec) android.Paths {
Colin Cross79c7c262019-04-17 11:11:46 -07001030 // This module is just a wrapper for the stubs.
Paul Duffin56d44902020-01-31 13:36:25 +00001031 return module.sdkJars(ctx, sdkVersion)
Colin Cross79c7c262019-04-17 11:11:46 -07001032}