blob: 4f4029aa34ac6f41fb5627643eea2220629f4467 [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"
Paul Duffin91b883d2020-02-11 13:05:28 +000037 sdkStubsSourceSuffix = ".stubs.source"
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 {
Paul Duffin91b883d2020-02-11 13:05:28 +0000112 return baseName + sdkStubsSourceSuffix + scope.moduleSuffix
Paul Duffind1b3a922020-01-22 11:57:20 +0000113}
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
Paul Duffinab8da5d2020-02-07 16:12:04 +0000458 Patch_module *string
Sundong Ahn0d7dff42019-12-04 12:53:44 +0900459 Libs []string
460 Soc_specific *bool
461 Device_specific *bool
462 Product_specific *bool
463 System_ext_specific *bool
464 Compile_dex *bool
Sundong Ahn0d7dff42019-12-04 12:53:44 +0900465 Java_version *string
466 Product_variables struct {
Jiyong Park82484c02018-04-23 21:41:26 +0900467 Pdk struct {
468 Enabled *bool
469 }
Jiyong Parkc678ad32018-04-10 13:07:10 +0900470 }
Sundong Ahn054b19a2018-10-19 13:46:09 +0900471 Openjdk9 struct {
472 Srcs []string
473 Javacflags []string
474 }
Jiyong Parkc678ad32018-04-10 13:07:10 +0900475 }{}
476
Jiyong Parkdf130542018-04-27 16:29:21 +0900477 props.Name = proptools.StringPtr(module.stubsName(apiScope))
Jiyong Parkc678ad32018-04-10 13:07:10 +0900478 // sources are generated from the droiddoc
Jiyong Parkdf130542018-04-27 16:29:21 +0900479 props.Srcs = []string{":" + module.docsName(apiScope)}
Paul Duffin12ceb462019-12-24 20:31:31 +0000480 sdkVersion := module.sdkVersionForStubsLibrary(mctx, apiScope)
Paul Duffin52d398a2019-06-11 12:31:14 +0100481 props.Sdk_version = proptools.StringPtr(sdkVersion)
Paul Duffin12ceb462019-12-24 20:31:31 +0000482 props.System_modules = module.Library.Module.deviceProperties.System_modules
Paul Duffinab8da5d2020-02-07 16:12:04 +0000483 props.Patch_module = module.Library.Module.properties.Patch_module
Paul Duffin367ab912019-12-23 19:40:36 +0000484 props.Installable = proptools.BoolPtr(false)
Sundong Ahn054b19a2018-10-19 13:46:09 +0900485 props.Libs = module.sdkLibraryProperties.Stub_only_libs
Jiyong Park82484c02018-04-23 21:41:26 +0900486 props.Product_variables.Pdk.Enabled = proptools.BoolPtr(false)
Sundong Ahn054b19a2018-10-19 13:46:09 +0900487 props.Openjdk9.Srcs = module.Library.Module.properties.Openjdk9.Srcs
488 props.Openjdk9.Javacflags = module.Library.Module.properties.Openjdk9.Javacflags
489 props.Java_version = module.Library.Module.properties.Java_version
490 if module.Library.Module.deviceProperties.Compile_dex != nil {
491 props.Compile_dex = module.Library.Module.deviceProperties.Compile_dex
Sundong Ahndd567f92018-07-31 17:19:11 +0900492 }
Jiyong Parkc678ad32018-04-10 13:07:10 +0900493
494 if module.SocSpecific() {
495 props.Soc_specific = proptools.BoolPtr(true)
496 } else if module.DeviceSpecific() {
497 props.Device_specific = proptools.BoolPtr(true)
498 } else if module.ProductSpecific() {
499 props.Product_specific = proptools.BoolPtr(true)
Sundong Ahn0d7dff42019-12-04 12:53:44 +0900500 } else if module.SystemExtSpecific() {
501 props.System_ext_specific = proptools.BoolPtr(true)
Jiyong Parkc678ad32018-04-10 13:07:10 +0900502 }
503
Colin Cross84dfc3d2019-09-25 11:33:01 -0700504 mctx.CreateModule(LibraryFactory, &props)
Jiyong Parkc678ad32018-04-10 13:07:10 +0900505}
506
507// Creates a droiddoc module that creates stubs source files from the given full source
508// files
Paul Duffind1b3a922020-01-22 11:57:20 +0000509func (module *SdkLibrary) createStubsSources(mctx android.LoadHookContext, apiScope *apiScope) {
Jiyong Parkc678ad32018-04-10 13:07:10 +0900510 props := struct {
Sundong Ahn054b19a2018-10-19 13:46:09 +0900511 Name *string
512 Srcs []string
513 Installable *bool
Paul Duffin52d398a2019-06-11 12:31:14 +0100514 Sdk_version *string
Paul Duffin12ceb462019-12-24 20:31:31 +0000515 System_modules *string
Sundong Ahn054b19a2018-10-19 13:46:09 +0900516 Libs []string
Paul Duffin11512472019-02-11 15:55:17 +0000517 Arg_files []string
Sundong Ahn054b19a2018-10-19 13:46:09 +0900518 Args *string
519 Api_tag_name *string
520 Api_filename *string
521 Removed_api_filename *string
Sundong Ahn054b19a2018-10-19 13:46:09 +0900522 Java_version *string
523 Merge_annotations_dirs []string
524 Merge_inclusion_annotations_dirs []string
525 Check_api struct {
Inseob Kim38449af2019-02-28 14:24:05 +0900526 Current ApiToCheck
527 Last_released ApiToCheck
528 Ignore_missing_latest_api *bool
Jiyong Park58c518b2018-05-12 22:29:12 +0900529 }
Sundong Ahn1b92c822018-05-29 11:35:17 +0900530 Aidl struct {
531 Include_dirs []string
532 Local_include_dirs []string
533 }
Jiyong Parkc678ad32018-04-10 13:07:10 +0900534 }{}
535
Paul Duffin250e6192019-06-07 10:44:37 +0100536 sdkDep := decodeSdkDep(mctx, sdkContext(&module.Library))
Paul Duffin12ceb462019-12-24 20:31:31 +0000537 // Use the platform API if standard libraries were requested, otherwise use
538 // no default libraries.
Paul Duffin52d398a2019-06-11 12:31:14 +0100539 sdkVersion := ""
540 if !sdkDep.hasStandardLibs() {
541 sdkVersion = "none"
542 }
Paul Duffin250e6192019-06-07 10:44:37 +0100543
Jiyong Parkdf130542018-04-27 16:29:21 +0900544 props.Name = proptools.StringPtr(module.docsName(apiScope))
Sundong Ahn054b19a2018-10-19 13:46:09 +0900545 props.Srcs = append(props.Srcs, module.Library.Module.properties.Srcs...)
Paul Duffin52d398a2019-06-11 12:31:14 +0100546 props.Sdk_version = proptools.StringPtr(sdkVersion)
Paul Duffin12ceb462019-12-24 20:31:31 +0000547 props.System_modules = module.Library.Module.deviceProperties.System_modules
Jiyong Parkc678ad32018-04-10 13:07:10 +0900548 props.Installable = proptools.BoolPtr(false)
Sundong Ahne6f0b052018-06-05 16:46:14 +0900549 // A droiddoc module has only one Libs property and doesn't distinguish between
550 // shared libs and static libs. So we need to add both of these libs to Libs property.
Sundong Ahn054b19a2018-10-19 13:46:09 +0900551 props.Libs = module.Library.Module.properties.Libs
552 props.Libs = append(props.Libs, module.Library.Module.properties.Static_libs...)
553 props.Aidl.Include_dirs = module.Library.Module.deviceProperties.Aidl.Include_dirs
554 props.Aidl.Local_include_dirs = module.Library.Module.deviceProperties.Aidl.Local_include_dirs
Sundong Ahn054b19a2018-10-19 13:46:09 +0900555 props.Java_version = module.Library.Module.properties.Java_version
Jiyong Parkc678ad32018-04-10 13:07:10 +0900556
Sundong Ahn054b19a2018-10-19 13:46:09 +0900557 props.Merge_annotations_dirs = module.sdkLibraryProperties.Merge_annotations_dirs
558 props.Merge_inclusion_annotations_dirs = module.sdkLibraryProperties.Merge_inclusion_annotations_dirs
559
Paul Duffin235ffff2019-12-24 10:41:30 +0000560 droiddocArgs := []string{}
561 if len(module.sdkLibraryProperties.Api_packages) != 0 {
562 droiddocArgs = append(droiddocArgs, "--stub-packages "+strings.Join(module.sdkLibraryProperties.Api_packages, ":"))
563 }
564 if len(module.sdkLibraryProperties.Hidden_api_packages) != 0 {
565 droiddocArgs = append(droiddocArgs,
566 android.JoinWithPrefix(module.sdkLibraryProperties.Hidden_api_packages, " --hide-package "))
567 }
568 droiddocArgs = append(droiddocArgs, module.sdkLibraryProperties.Droiddoc_options...)
569 disabledWarnings := []string{
570 "MissingPermission",
571 "BroadcastBehavior",
572 "HiddenSuperclass",
573 "DeprecationMismatch",
574 "UnavailableSymbol",
575 "SdkConstant",
576 "HiddenTypeParameter",
577 "Todo",
578 "Typo",
579 }
580 droiddocArgs = append(droiddocArgs, android.JoinWithPrefix(disabledWarnings, "--hide "))
Sundong Ahnfb2721f2018-09-17 13:23:09 +0900581
Jiyong Parkdf130542018-04-27 16:29:21 +0900582 switch apiScope {
583 case apiScopeSystem:
Paul Duffin235ffff2019-12-24 10:41:30 +0000584 droiddocArgs = append(droiddocArgs, "-showAnnotation android.annotation.SystemApi")
Jiyong Parkdf130542018-04-27 16:29:21 +0900585 case apiScopeTest:
Paul Duffin235ffff2019-12-24 10:41:30 +0000586 droiddocArgs = append(droiddocArgs, " -showAnnotation android.annotation.TestApi")
Jiyong Parkc678ad32018-04-10 13:07:10 +0900587 }
Paul Duffin11512472019-02-11 15:55:17 +0000588 props.Arg_files = module.sdkLibraryProperties.Droiddoc_option_files
Paul Duffin235ffff2019-12-24 10:41:30 +0000589 props.Args = proptools.StringPtr(strings.Join(droiddocArgs, " "))
Jiyong Parkc678ad32018-04-10 13:07:10 +0900590
591 // List of APIs identified from the provided source files are created. They are later
592 // compared against to the not-yet-released (a.k.a current) list of APIs and to the
593 // last-released (a.k.a numbered) list of API.
Paul Duffind1b3a922020-01-22 11:57:20 +0000594 currentApiFileName := apiScope.apiFilePrefix + "current.txt"
595 removedApiFileName := apiScope.apiFilePrefix + "removed.txt"
Paul Duffin749f98f2019-12-30 17:23:46 +0000596 apiDir := module.getApiDir()
597 currentApiFileName = path.Join(apiDir, currentApiFileName)
598 removedApiFileName = path.Join(apiDir, removedApiFileName)
Jiyong Park58c518b2018-05-12 22:29:12 +0900599 // TODO(jiyong): remove these three props
Jiyong Parkdf130542018-04-27 16:29:21 +0900600 props.Api_tag_name = proptools.StringPtr(module.apiTagName(apiScope))
Jiyong Parkc678ad32018-04-10 13:07:10 +0900601 props.Api_filename = proptools.StringPtr(currentApiFileName)
602 props.Removed_api_filename = proptools.StringPtr(removedApiFileName)
603
Jiyong Park58c518b2018-05-12 22:29:12 +0900604 // check against the not-yet-release API
605 props.Check_api.Current.Api_file = proptools.StringPtr(currentApiFileName)
606 props.Check_api.Current.Removed_api_file = proptools.StringPtr(removedApiFileName)
Jiyong Park58c518b2018-05-12 22:29:12 +0900607
608 // check against the latest released API
609 props.Check_api.Last_released.Api_file = proptools.StringPtr(
610 module.latestApiFilegroupName(apiScope))
611 props.Check_api.Last_released.Removed_api_file = proptools.StringPtr(
612 module.latestRemovedApiFilegroupName(apiScope))
Inseob Kim38449af2019-02-28 14:24:05 +0900613 props.Check_api.Ignore_missing_latest_api = proptools.BoolPtr(true)
Jiyong Park58c518b2018-05-12 22:29:12 +0900614
Colin Cross84dfc3d2019-09-25 11:33:01 -0700615 mctx.CreateModule(DroidstubsFactory, &props)
Jiyong Parkc678ad32018-04-10 13:07:10 +0900616}
617
Paul Duffine74ac732020-02-06 13:51:46 +0000618func (module *SdkLibrary) XmlPermissionsFile() android.Path {
619 return module.permissionsFile
620}
621
622func (module *SdkLibrary) XmlPermissionsFileContent() string {
623 return fmt.Sprintf(permissionsTemplate, module.BaseModuleName(), module.implPath())
624}
625
Jiyong Parkc678ad32018-04-10 13:07:10 +0900626// Creates the xml file that publicizes the runtime library
Colin Crossf8b860a2019-04-16 14:43:28 -0700627func (module *SdkLibrary) createXmlFile(mctx android.LoadHookContext) {
Paul Duffine74ac732020-02-06 13:51:46 +0000628
629 xmlContent := module.XmlPermissionsFileContent()
630
631 genRuleName := module.genXmlPermissionsFileName()
632
633 // Create a genrule module to create the XML permissions file.
634 genRuleProps := struct {
635 Name *string
636 Cmd *string
637 Out []string
638 }{
639 Name: proptools.StringPtr(genRuleName),
640 Cmd: proptools.StringPtr("echo -e '" + xmlContent + "' > '$(out)'"),
641 Out: []string{module.xmlFileName()},
642 }
643
644 mctx.CreateModule(genrule.GenRuleFactory, &genRuleProps)
645
Jiyong Parkc678ad32018-04-10 13:07:10 +0900646 // creates a prebuilt_etc module to actually place the xml file under
647 // <partition>/etc/permissions
648 etcProps := struct {
Sundong Ahn0d7dff42019-12-04 12:53:44 +0900649 Name *string
650 Src *string
651 Sub_dir *string
652 Soc_specific *bool
653 Device_specific *bool
654 Product_specific *bool
655 System_ext_specific *bool
Jiyong Parkc678ad32018-04-10 13:07:10 +0900656 }{}
657 etcProps.Name = proptools.StringPtr(module.xmlFileName())
Paul Duffine74ac732020-02-06 13:51:46 +0000658 etcProps.Src = proptools.StringPtr(":" + genRuleName)
Jiyong Parkc678ad32018-04-10 13:07:10 +0900659 etcProps.Sub_dir = proptools.StringPtr("permissions")
660 if module.SocSpecific() {
661 etcProps.Soc_specific = proptools.BoolPtr(true)
662 } else if module.DeviceSpecific() {
663 etcProps.Device_specific = proptools.BoolPtr(true)
664 } else if module.ProductSpecific() {
665 etcProps.Product_specific = proptools.BoolPtr(true)
Sundong Ahn0d7dff42019-12-04 12:53:44 +0900666 } else if module.SystemExtSpecific() {
667 etcProps.System_ext_specific = proptools.BoolPtr(true)
Jiyong Parkc678ad32018-04-10 13:07:10 +0900668 }
Colin Cross84dfc3d2019-09-25 11:33:01 -0700669 mctx.CreateModule(android.PrebuiltEtcFactory, &etcProps)
Jiyong Parkc678ad32018-04-10 13:07:10 +0900670}
671
Paul Duffin50061512020-01-21 16:31:05 +0000672func PrebuiltJars(ctx android.BaseModuleContext, baseName string, s sdkSpec) android.Paths {
Jiyong Park6a927c42020-01-21 02:03:43 +0900673 var ver sdkVersion
674 var kind sdkKind
675 if s.usePrebuilt(ctx) {
676 ver = s.version
677 kind = s.kind
Jiyong Parkc678ad32018-04-10 13:07:10 +0900678 } else {
Jiyong Park6a927c42020-01-21 02:03:43 +0900679 // We don't have prebuilt SDK for the specific sdkVersion.
680 // Instead of breaking the build, fallback to use "system_current"
681 ver = sdkVersionCurrent
682 kind = sdkSystem
Sundong Ahn054b19a2018-10-19 13:46:09 +0900683 }
Jiyong Park6a927c42020-01-21 02:03:43 +0900684
685 dir := filepath.Join("prebuilts", "sdk", ver.String(), kind.String())
Paul Duffin50061512020-01-21 16:31:05 +0000686 jar := filepath.Join(dir, baseName+".jar")
Sundong Ahn054b19a2018-10-19 13:46:09 +0900687 jarPath := android.ExistentPathForSource(ctx, jar)
Sundong Ahnae418ac2019-02-28 15:01:28 +0900688 if !jarPath.Valid() {
Colin Cross07c88562020-01-07 09:34:44 -0800689 if ctx.Config().AllowMissingDependencies() {
690 return android.Paths{android.PathForSource(ctx, jar)}
691 } else {
Jiyong Park6a927c42020-01-21 02:03:43 +0900692 ctx.PropertyErrorf("sdk_library", "invalid sdk version %q, %q does not exist", s.raw, jar)
Colin Cross07c88562020-01-07 09:34:44 -0800693 }
Sundong Ahnae418ac2019-02-28 15:01:28 +0900694 return nil
695 }
Sundong Ahn054b19a2018-10-19 13:46:09 +0900696 return android.Paths{jarPath.Path()}
697}
698
Paul Duffind1b3a922020-01-22 11:57:20 +0000699func (module *SdkLibrary) sdkJars(
700 ctx android.BaseModuleContext,
701 sdkVersion sdkSpec,
702 headerJars bool) android.Paths {
703
Paul Duffin50061512020-01-21 16:31:05 +0000704 // If a specific numeric version has been requested then use prebuilt versions of the sdk.
705 if sdkVersion.version.isNumbered() {
706 return PrebuiltJars(ctx, module.BaseModuleName(), sdkVersion)
Sundong Ahn054b19a2018-10-19 13:46:09 +0900707 } else {
Paul Duffind1b3a922020-01-22 11:57:20 +0000708 if !sdkVersion.specified() {
709 if headerJars {
710 return module.Library.HeaderJars()
711 } else {
712 return module.Library.ImplementationJars()
713 }
714 }
Paul Duffin726d23c2020-01-22 16:30:37 +0000715 var apiScope *apiScope
Jiyong Park6a927c42020-01-21 02:03:43 +0900716 switch sdkVersion.kind {
717 case sdkSystem:
Paul Duffin726d23c2020-01-22 16:30:37 +0000718 apiScope = apiScopeSystem
719 case sdkTest:
720 apiScope = apiScopeTest
Jiyong Park6a927c42020-01-21 02:03:43 +0900721 case sdkPrivate:
Sundong Ahn054b19a2018-10-19 13:46:09 +0900722 return module.Library.HeaderJars()
Jiyong Park6a927c42020-01-21 02:03:43 +0900723 default:
Paul Duffin726d23c2020-01-22 16:30:37 +0000724 apiScope = apiScopePublic
Paul Duffind1b3a922020-01-22 11:57:20 +0000725 }
726
Paul Duffin726d23c2020-01-22 16:30:37 +0000727 paths := module.getScopePaths(apiScope)
Paul Duffind1b3a922020-01-22 11:57:20 +0000728 if headerJars {
729 return paths.stubsHeaderPath
730 } else {
731 return paths.stubsImplPath
Sundong Ahn054b19a2018-10-19 13:46:09 +0900732 }
Jiyong Parkc678ad32018-04-10 13:07:10 +0900733 }
734}
735
Sundong Ahn241cd372018-07-13 16:16:44 +0900736// to satisfy SdkLibraryDependency interface
Paul Duffind1b3a922020-01-22 11:57:20 +0000737func (module *SdkLibrary) SdkHeaderJars(ctx android.BaseModuleContext, sdkVersion sdkSpec) android.Paths {
738 return module.sdkJars(ctx, sdkVersion, true /*headerJars*/)
739}
740
741// to satisfy SdkLibraryDependency interface
Jiyong Park6a927c42020-01-21 02:03:43 +0900742func (module *SdkLibrary) SdkImplementationJars(ctx android.BaseModuleContext, sdkVersion sdkSpec) android.Paths {
Paul Duffind1b3a922020-01-22 11:57:20 +0000743 return module.sdkJars(ctx, sdkVersion, false /*headerJars*/)
Sundong Ahn241cd372018-07-13 16:16:44 +0900744}
745
Sundong Ahn80a87b32019-05-13 15:02:50 +0900746func (module *SdkLibrary) SetNoDist() {
747 module.sdkLibraryProperties.No_dist = proptools.BoolPtr(true)
748}
749
Colin Cross571cccf2019-02-04 11:22:08 -0800750var javaSdkLibrariesKey = android.NewOnceKey("javaSdkLibraries")
751
Jiyong Park82484c02018-04-23 21:41:26 +0900752func javaSdkLibraries(config android.Config) *[]string {
Colin Cross571cccf2019-02-04 11:22:08 -0800753 return config.Once(javaSdkLibrariesKey, func() interface{} {
Jiyong Park82484c02018-04-23 21:41:26 +0900754 return &[]string{}
755 }).(*[]string)
756}
757
Paul Duffin749f98f2019-12-30 17:23:46 +0000758func (module *SdkLibrary) getApiDir() string {
759 return proptools.StringDefault(module.sdkLibraryProperties.Api_dir, "api")
760}
761
Jiyong Parkc678ad32018-04-10 13:07:10 +0900762// For a java_sdk_library module, create internal modules for stubs, docs,
763// runtime libs and xml file. If requested, the stubs and docs are created twice
764// once for public API level and once for system API level
Colin Crossf8b860a2019-04-16 14:43:28 -0700765func (module *SdkLibrary) CreateInternalModules(mctx android.LoadHookContext) {
Inseob Kim6e93ac92019-03-21 17:43:49 +0900766 if len(module.Library.Module.properties.Srcs) == 0 {
Inseob Kimc0907f12019-02-08 21:00:45 +0900767 mctx.PropertyErrorf("srcs", "java_sdk_library must specify srcs")
Jooyung Han58f26ab2019-12-18 15:34:32 +0900768 return
Inseob Kimc0907f12019-02-08 21:00:45 +0900769 }
770
Paul Duffin37e0b772019-12-30 17:20:10 +0000771 // If this builds against standard libraries (i.e. is not part of the core libraries)
772 // then assume it provides both system and test apis. Otherwise, assume it does not and
773 // also assume it does not contribute to the dist build.
774 sdkDep := decodeSdkDep(mctx, sdkContext(&module.Library))
775 hasSystemAndTestApis := sdkDep.hasStandardLibs()
776 module.sdkLibraryProperties.Has_system_and_test_apis = hasSystemAndTestApis
777 module.sdkLibraryProperties.No_dist = proptools.BoolPtr(!hasSystemAndTestApis)
778
Inseob Kim8098faa2019-03-18 10:19:51 +0900779 missing_current_api := false
780
Paul Duffind1b3a922020-01-22 11:57:20 +0000781 activeScopes := module.getActiveApiScopes()
782
Paul Duffin749f98f2019-12-30 17:23:46 +0000783 apiDir := module.getApiDir()
Paul Duffind1b3a922020-01-22 11:57:20 +0000784 for _, scope := range activeScopes {
Inseob Kim8098faa2019-03-18 10:19:51 +0900785 for _, api := range []string{"current.txt", "removed.txt"} {
Paul Duffind1b3a922020-01-22 11:57:20 +0000786 path := path.Join(mctx.ModuleDir(), apiDir, scope.apiFilePrefix+api)
Inseob Kim8098faa2019-03-18 10:19:51 +0900787 p := android.ExistentPathForSource(mctx, path)
788 if !p.Valid() {
789 mctx.ModuleErrorf("Current api file %#v doesn't exist", path)
790 missing_current_api = true
791 }
792 }
793 }
794
795 if missing_current_api {
796 script := "build/soong/scripts/gen-java-current-api-files.sh"
797 p := android.ExistentPathForSource(mctx, script)
798
799 if !p.Valid() {
800 panic(fmt.Sprintf("script file %s doesn't exist", script))
801 }
802
803 mctx.ModuleErrorf("One or more current api files are missing. "+
804 "You can update them by:\n"+
Paul Duffin37e0b772019-12-30 17:20:10 +0000805 "%s %q %s && m update-api",
Paul Duffind1b3a922020-01-22 11:57:20 +0000806 script, filepath.Join(mctx.ModuleDir(), apiDir),
807 strings.Join(activeScopes.Strings(func(s *apiScope) string { return s.apiFilePrefix }), " "))
Inseob Kim8098faa2019-03-18 10:19:51 +0900808 return
809 }
810
Paul Duffind1b3a922020-01-22 11:57:20 +0000811 for _, scope := range activeScopes {
812 module.createStubsLibrary(mctx, scope)
813 module.createStubsSources(mctx, scope)
Inseob Kimc0907f12019-02-08 21:00:45 +0900814 }
815
Paul Duffin43db9be2019-12-30 17:35:49 +0000816 if !proptools.Bool(module.sdkLibraryProperties.Api_only) {
817 // for runtime
818 module.createXmlFile(mctx)
819
820 // record java_sdk_library modules so that they are exported to make
821 javaSdkLibraries := javaSdkLibraries(mctx.Config())
822 javaSdkLibrariesLock.Lock()
823 defer javaSdkLibrariesLock.Unlock()
824 *javaSdkLibraries = append(*javaSdkLibraries, module.BaseModuleName())
825 }
Inseob Kimc0907f12019-02-08 21:00:45 +0900826}
827
828func (module *SdkLibrary) InitSdkLibraryProperties() {
Sundong Ahn054b19a2018-10-19 13:46:09 +0900829 module.AddProperties(
830 &module.sdkLibraryProperties,
831 &module.Library.Module.properties,
832 &module.Library.Module.dexpreoptProperties,
833 &module.Library.Module.deviceProperties,
834 &module.Library.Module.protoProperties,
835 )
836
837 module.Library.Module.properties.Installable = proptools.BoolPtr(true)
838 module.Library.Module.deviceProperties.IsSDKLibrary = true
Inseob Kimc0907f12019-02-08 21:00:45 +0900839}
Sundong Ahn054b19a2018-10-19 13:46:09 +0900840
Jaewoong Jung4f158ee2019-07-11 10:05:35 -0700841// java_sdk_library is a special Java library that provides optional platform APIs to apps.
842// In practice, it can be viewed as a combination of several modules: 1) stubs library that clients
843// are linked against to, 2) droiddoc module that internally generates API stubs source files,
844// 3) the real runtime shared library that implements the APIs, and 4) XML file for adding
845// the runtime lib to the classpath at runtime if requested via <uses-library>.
Inseob Kimc0907f12019-02-08 21:00:45 +0900846func SdkLibraryFactory() android.Module {
847 module := &SdkLibrary{}
848 module.InitSdkLibraryProperties()
Jooyung Han58f26ab2019-12-18 15:34:32 +0900849 android.InitApexModule(module)
Sundong Ahn054b19a2018-10-19 13:46:09 +0900850 InitJavaModule(module, android.HostAndDeviceSupported)
Colin Crossf8b860a2019-04-16 14:43:28 -0700851 android.AddLoadHook(module, func(ctx android.LoadHookContext) { module.CreateInternalModules(ctx) })
Jiyong Parkc678ad32018-04-10 13:07:10 +0900852 return module
853}
Colin Cross79c7c262019-04-17 11:11:46 -0700854
855//
856// SDK library prebuilts
857//
858
Paul Duffin56d44902020-01-31 13:36:25 +0000859// Properties associated with each api scope.
860type sdkLibraryScopeProperties struct {
Colin Cross79c7c262019-04-17 11:11:46 -0700861 Jars []string `android:"path"`
862
863 Sdk_version *string
864
Colin Cross79c7c262019-04-17 11:11:46 -0700865 // List of shared java libs that this module has dependencies to
866 Libs []string
Colin Cross79c7c262019-04-17 11:11:46 -0700867}
868
Paul Duffin56d44902020-01-31 13:36:25 +0000869type sdkLibraryImportProperties struct {
Paul Duffinfcfd7912020-01-31 17:54:30 +0000870 // List of shared java libs, common to all scopes, that this module has
871 // dependencies to
872 Libs []string
873
Paul Duffin56d44902020-01-31 13:36:25 +0000874 // Properties associated with the public api scope.
875 Public sdkLibraryScopeProperties
876
877 // Properties associated with the system api scope.
878 System sdkLibraryScopeProperties
879
880 // Properties associated with the test api scope.
881 Test sdkLibraryScopeProperties
882}
883
Colin Cross79c7c262019-04-17 11:11:46 -0700884type sdkLibraryImport struct {
885 android.ModuleBase
886 android.DefaultableModuleBase
887 prebuilt android.Prebuilt
888
889 properties sdkLibraryImportProperties
890
Paul Duffin56d44902020-01-31 13:36:25 +0000891 commonToSdkLibraryAndImport
Colin Cross79c7c262019-04-17 11:11:46 -0700892}
893
894var _ SdkLibraryDependency = (*sdkLibraryImport)(nil)
895
Jaewoong Jung4f158ee2019-07-11 10:05:35 -0700896// java_sdk_library_import imports a prebuilt java_sdk_library.
Colin Cross79c7c262019-04-17 11:11:46 -0700897func sdkLibraryImportFactory() android.Module {
898 module := &sdkLibraryImport{}
899
Paul Duffinfcfd7912020-01-31 17:54:30 +0000900 module.AddProperties(&module.properties)
Colin Cross79c7c262019-04-17 11:11:46 -0700901
Paul Duffin0bdcb272020-02-06 15:24:57 +0000902 android.InitPrebuiltModule(module, &[]string{""})
Colin Cross79c7c262019-04-17 11:11:46 -0700903 InitJavaModule(module, android.HostAndDeviceSupported)
904
905 android.AddLoadHook(module, func(mctx android.LoadHookContext) { module.createInternalModules(mctx) })
906 return module
907}
908
909func (module *sdkLibraryImport) Prebuilt() *android.Prebuilt {
910 return &module.prebuilt
911}
912
913func (module *sdkLibraryImport) Name() string {
914 return module.prebuilt.Name(module.ModuleBase.Name())
915}
916
917func (module *sdkLibraryImport) createInternalModules(mctx android.LoadHookContext) {
Colin Cross79c7c262019-04-17 11:11:46 -0700918
Paul Duffin50061512020-01-21 16:31:05 +0000919 // If the build is configured to use prebuilts then force this to be preferred.
920 if mctx.Config().UnbundledBuildUsePrebuiltSdks() {
921 module.prebuilt.ForcePrefer()
922 }
923
Paul Duffin56d44902020-01-31 13:36:25 +0000924 for apiScope, scopeProperties := range module.scopeProperties() {
925 if len(scopeProperties.Jars) == 0 {
926 continue
927 }
928
929 // Creates a java import for the jar with ".stubs" suffix
930 props := struct {
931 Name *string
932 Soc_specific *bool
933 Device_specific *bool
934 Product_specific *bool
935 System_ext_specific *bool
936 Sdk_version *string
937 Libs []string
938 Jars []string
Paul Duffin50061512020-01-21 16:31:05 +0000939 Prefer *bool
Paul Duffin56d44902020-01-31 13:36:25 +0000940 }{}
941
942 props.Name = proptools.StringPtr(apiScope.stubsModuleName(module.BaseModuleName()))
943 props.Sdk_version = scopeProperties.Sdk_version
Paul Duffinfcfd7912020-01-31 17:54:30 +0000944 // Prepend any of the libs from the legacy public properties to the libs for each of the
945 // scopes to avoid having to duplicate them in each scope.
946 props.Libs = append(module.properties.Libs, scopeProperties.Libs...)
Paul Duffin56d44902020-01-31 13:36:25 +0000947 props.Jars = scopeProperties.Jars
948
949 if module.SocSpecific() {
950 props.Soc_specific = proptools.BoolPtr(true)
951 } else if module.DeviceSpecific() {
952 props.Device_specific = proptools.BoolPtr(true)
953 } else if module.ProductSpecific() {
954 props.Product_specific = proptools.BoolPtr(true)
955 } else if module.SystemExtSpecific() {
956 props.System_ext_specific = proptools.BoolPtr(true)
957 }
958
Paul Duffin50061512020-01-21 16:31:05 +0000959 // If the build should use prebuilt sdks then set prefer to true on the stubs library.
960 // That will cause the prebuilt version of the stubs to override the source version.
961 if mctx.Config().UnbundledBuildUsePrebuiltSdks() {
962 props.Prefer = proptools.BoolPtr(true)
963 }
964
Paul Duffin56d44902020-01-31 13:36:25 +0000965 mctx.CreateModule(ImportFactory, &props)
966 }
Colin Cross79c7c262019-04-17 11:11:46 -0700967
968 javaSdkLibraries := javaSdkLibraries(mctx.Config())
969 javaSdkLibrariesLock.Lock()
970 defer javaSdkLibrariesLock.Unlock()
971 *javaSdkLibraries = append(*javaSdkLibraries, module.BaseModuleName())
972}
973
Paul Duffin56d44902020-01-31 13:36:25 +0000974func (module *sdkLibraryImport) scopeProperties() map[*apiScope]*sdkLibraryScopeProperties {
975 p := make(map[*apiScope]*sdkLibraryScopeProperties)
976 p[apiScopePublic] = &module.properties.Public
977 p[apiScopeSystem] = &module.properties.System
978 p[apiScopeTest] = &module.properties.Test
979 return p
980}
981
Colin Cross79c7c262019-04-17 11:11:46 -0700982func (module *sdkLibraryImport) DepsMutator(ctx android.BottomUpMutatorContext) {
Paul Duffin56d44902020-01-31 13:36:25 +0000983 for apiScope, scopeProperties := range module.scopeProperties() {
984 if len(scopeProperties.Jars) == 0 {
985 continue
986 }
987
988 // Add dependencies to the prebuilt stubs library
989 ctx.AddVariationDependencies(nil, apiScope.stubsTag, apiScope.stubsModuleName(module.BaseModuleName()))
990 }
Colin Cross79c7c262019-04-17 11:11:46 -0700991}
992
993func (module *sdkLibraryImport) GenerateAndroidBuildActions(ctx android.ModuleContext) {
994 // Record the paths to the prebuilt stubs library.
995 ctx.VisitDirectDeps(func(to android.Module) {
996 tag := ctx.OtherModuleDependencyTag(to)
997
Paul Duffin56d44902020-01-31 13:36:25 +0000998 if lib, ok := to.(Dependency); ok {
999 if scopeTag, ok := tag.(scopeDependencyTag); ok {
1000 apiScope := scopeTag.apiScope
1001 scopePaths := module.getScopePaths(apiScope)
1002 scopePaths.stubsHeaderPath = lib.HeaderJars()
1003 }
Colin Cross79c7c262019-04-17 11:11:46 -07001004 }
1005 })
1006}
1007
Paul Duffin56d44902020-01-31 13:36:25 +00001008func (module *sdkLibraryImport) sdkJars(
1009 ctx android.BaseModuleContext,
1010 sdkVersion sdkSpec) android.Paths {
1011
Paul Duffin50061512020-01-21 16:31:05 +00001012 // If a specific numeric version has been requested then use prebuilt versions of the sdk.
1013 if sdkVersion.version.isNumbered() {
1014 return PrebuiltJars(ctx, module.BaseModuleName(), sdkVersion)
1015 }
1016
Paul Duffin56d44902020-01-31 13:36:25 +00001017 var apiScope *apiScope
1018 switch sdkVersion.kind {
1019 case sdkSystem:
1020 apiScope = apiScopeSystem
1021 case sdkTest:
1022 apiScope = apiScopeTest
1023 default:
1024 apiScope = apiScopePublic
1025 }
1026
1027 paths := module.getScopePaths(apiScope)
1028 return paths.stubsHeaderPath
1029}
1030
Colin Cross79c7c262019-04-17 11:11:46 -07001031// to satisfy SdkLibraryDependency interface
Jiyong Park6a927c42020-01-21 02:03:43 +09001032func (module *sdkLibraryImport) SdkHeaderJars(ctx android.BaseModuleContext, sdkVersion sdkSpec) android.Paths {
Colin Cross79c7c262019-04-17 11:11:46 -07001033 // This module is just a wrapper for the prebuilt stubs.
Paul Duffin56d44902020-01-31 13:36:25 +00001034 return module.sdkJars(ctx, sdkVersion)
Colin Cross79c7c262019-04-17 11:11:46 -07001035}
1036
1037// to satisfy SdkLibraryDependency interface
Jiyong Park6a927c42020-01-21 02:03:43 +09001038func (module *sdkLibraryImport) SdkImplementationJars(ctx android.BaseModuleContext, sdkVersion sdkSpec) android.Paths {
Colin Cross79c7c262019-04-17 11:11:46 -07001039 // This module is just a wrapper for the stubs.
Paul Duffin56d44902020-01-31 13:36:25 +00001040 return module.sdkJars(ctx, sdkVersion)
Colin Cross79c7c262019-04-17 11:11:46 -07001041}