blob: 503f1d60a50b73966535e83f43c5e766185e069b [file] [log] [blame]
Jiyong Parkc678ad32018-04-10 13:07:10 +09001// Copyright 2018 Google Inc. All rights reserved.
2//
3// Licensed under the Apache License, Version 2.0 (the "License");
4// you may not use this file except in compliance with the License.
5// You may obtain a copy of the License at
6//
7// http://www.apache.org/licenses/LICENSE-2.0
8//
9// Unless required by applicable law or agreed to in writing, software
10// distributed under the License is distributed on an "AS IS" BASIS,
11// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12// See the License for the specific language governing permissions and
13// limitations under the License.
14
15package java
16
17import (
18 "android/soong/android"
Paul Duffind1b3a922020-01-22 11:57:20 +000019
Jiyong Parkc678ad32018-04-10 13:07:10 +090020 "fmt"
Jiyong Park82484c02018-04-23 21:41:26 +090021 "io"
Jiyong Parkc678ad32018-04-10 13:07:10 +090022 "path"
Sundong Ahn054b19a2018-10-19 13:46:09 +090023 "path/filepath"
Jiyong Park82484c02018-04-23 21:41:26 +090024 "sort"
Jiyong Parkc678ad32018-04-10 13:07:10 +090025 "strings"
Jiyong Park82484c02018-04-23 21:41:26 +090026 "sync"
Jiyong Parkc678ad32018-04-10 13:07:10 +090027
Paul Duffind1b3a922020-01-22 11:57:20 +000028 "github.com/google/blueprint"
Jiyong Parkc678ad32018-04-10 13:07:10 +090029 "github.com/google/blueprint/proptools"
30)
31
Jooyung Han58f26ab2019-12-18 15:34:32 +090032const (
Jiyong Parkc678ad32018-04-10 13:07:10 +090033 sdkStubsLibrarySuffix = ".stubs"
34 sdkSystemApiSuffix = ".system"
Jiyong Parkdf130542018-04-27 16:29:21 +090035 sdkTestApiSuffix = ".test"
Jiyong Parkc678ad32018-04-10 13:07:10 +090036 sdkDocsSuffix = ".docs"
Jiyong Parkc678ad32018-04-10 13:07:10 +090037 sdkXmlFileSuffix = ".xml"
Jooyung Han624058e2019-12-24 18:38:06 +090038 permissionsTemplate = `<?xml version="1.0" encoding="utf-8"?>\n` +
39 `<!-- Copyright (C) 2018 The Android Open Source Project\n` +
40 `\n` +
41 ` Licensed under the Apache License, Version 2.0 (the "License");\n` +
42 ` you may not use this file except in compliance with the License.\n` +
43 ` You may obtain a copy of the License at\n` +
44 `\n` +
45 ` http://www.apache.org/licenses/LICENSE-2.0\n` +
46 `\n` +
47 ` Unless required by applicable law or agreed to in writing, software\n` +
48 ` distributed under the License is distributed on an "AS IS" BASIS,\n` +
49 ` WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\n` +
50 ` See the License for the specific language governing permissions and\n` +
51 ` limitations under the License.\n` +
52 `-->\n` +
53 `<permissions>\n` +
54 ` <library name="%s" file="%s"/>\n` +
55 `</permissions>\n`
Jiyong Parkc678ad32018-04-10 13:07:10 +090056)
57
Paul Duffind1b3a922020-01-22 11:57:20 +000058// A tag to associated a dependency with a specific api scope.
59type scopeDependencyTag struct {
60 blueprint.BaseDependencyTag
61 name string
62 apiScope *apiScope
63}
64
65// Provides information about an api scope, e.g. public, system, test.
66type apiScope struct {
67 // The name of the api scope, e.g. public, system, test
68 name string
69
70 // The tag to use to depend on the stubs library module.
71 stubsTag scopeDependencyTag
72
73 // The tag to use to depend on the stubs
74 apiFileTag scopeDependencyTag
75
76 // The scope specific prefix to add to the api file base of "current.txt" or "removed.txt".
77 apiFilePrefix string
78
79 // The scope specific prefix to add to the sdk library module name to construct a scope specific
80 // module name.
81 moduleSuffix string
82
83 // The suffix to add to the make variable that references the location of the api file.
84 apiFileMakeVariableSuffix string
85
86 // SDK version that the stubs library is built against. Note that this is always
87 // *current. Older stubs library built with a numbered SDK version is created from
88 // the prebuilt jar.
89 sdkVersion string
90}
91
92// Initialize a scope, creating and adding appropriate dependency tags
93func initApiScope(scope *apiScope) *apiScope {
94 //apiScope := &scope
95 scope.stubsTag = scopeDependencyTag{
96 name: scope.name + "-stubs",
97 apiScope: scope,
98 }
99 scope.apiFileTag = scopeDependencyTag{
100 name: scope.name + "-api",
101 apiScope: scope,
102 }
103 return scope
104}
105
106func (scope *apiScope) stubsModuleName(baseName string) string {
107 return baseName + sdkStubsLibrarySuffix + scope.moduleSuffix
108}
109
110func (scope *apiScope) docsModuleName(baseName string) string {
111 return baseName + sdkDocsSuffix + scope.moduleSuffix
112}
113
114type apiScopes []*apiScope
115
116func (scopes apiScopes) Strings(accessor func(*apiScope) string) []string {
117 var list []string
118 for _, scope := range scopes {
119 list = append(list, accessor(scope))
120 }
121 return list
122}
123
Jiyong Parkc678ad32018-04-10 13:07:10 +0900124var (
Paul Duffind1b3a922020-01-22 11:57:20 +0000125 apiScopePublic = initApiScope(&apiScope{
126 name: "public",
127 sdkVersion: "current",
128 })
129 apiScopeSystem = initApiScope(&apiScope{
130 name: "system",
131 apiFilePrefix: "system-",
132 moduleSuffix: sdkSystemApiSuffix,
133 apiFileMakeVariableSuffix: "_SYSTEM",
134 sdkVersion: "system_current",
135 })
136 apiScopeTest = initApiScope(&apiScope{
137 name: "test",
138 apiFilePrefix: "test-",
139 moduleSuffix: sdkTestApiSuffix,
140 apiFileMakeVariableSuffix: "_TEST",
141 sdkVersion: "test_current",
142 })
143 allApiScopes = apiScopes{
144 apiScopePublic,
145 apiScopeSystem,
146 apiScopeTest,
147 }
Jiyong Parkc678ad32018-04-10 13:07:10 +0900148)
149
Jiyong Park82484c02018-04-23 21:41:26 +0900150var (
151 javaSdkLibrariesLock sync.Mutex
152)
153
Jiyong Parkc678ad32018-04-10 13:07:10 +0900154// TODO: these are big features that are currently missing
Jiyong Park1be96912018-05-28 18:02:19 +0900155// 1) disallowing linking to the runtime shared lib
156// 2) HTML generation
Jiyong Parkc678ad32018-04-10 13:07:10 +0900157
158func init() {
Paul Duffin43dc1cc2019-12-19 11:18:54 +0000159 RegisterSdkLibraryBuildComponents(android.InitRegistrationContext)
Jiyong Parkc678ad32018-04-10 13:07:10 +0900160
Jiyong Park82484c02018-04-23 21:41:26 +0900161 android.RegisterMakeVarsProvider(pctx, func(ctx android.MakeVarsContext) {
162 javaSdkLibraries := javaSdkLibraries(ctx.Config())
163 sort.Strings(*javaSdkLibraries)
164 ctx.Strict("JAVA_SDK_LIBRARIES", strings.Join(*javaSdkLibraries, " "))
165 })
Jiyong Parkc678ad32018-04-10 13:07:10 +0900166}
167
Paul Duffin43dc1cc2019-12-19 11:18:54 +0000168func RegisterSdkLibraryBuildComponents(ctx android.RegistrationContext) {
169 ctx.RegisterModuleType("java_sdk_library", SdkLibraryFactory)
170 ctx.RegisterModuleType("java_sdk_library_import", sdkLibraryImportFactory)
171}
172
Jiyong Parkc678ad32018-04-10 13:07:10 +0900173type sdkLibraryProperties struct {
Sundong Ahnf043cf62018-06-25 16:04:37 +0900174 // List of Java libraries that will be in the classpath when building stubs
175 Stub_only_libs []string `android:"arch_variant"`
176
Paul Duffin7a586d32019-12-30 17:09:34 +0000177 // list of package names that will be documented and publicized as API.
178 // This allows the API to be restricted to a subset of the source files provided.
179 // If this is unspecified then all the source files will be treated as being part
180 // of the API.
Jiyong Parkc678ad32018-04-10 13:07:10 +0900181 Api_packages []string
182
Jiyong Park5a2c9d72018-05-01 22:25:41 +0900183 // list of package names that must be hidden from the API
184 Hidden_api_packages []string
185
Paul Duffin749f98f2019-12-30 17:23:46 +0000186 // the relative path to the directory containing the api specification files.
187 // Defaults to "api".
188 Api_dir *string
189
Paul Duffin43db9be2019-12-30 17:35:49 +0000190 // If set to true there is no runtime library.
191 Api_only *bool
192
Paul Duffin11512472019-02-11 15:55:17 +0000193 // local files that are used within user customized droiddoc options.
194 Droiddoc_option_files []string
195
196 // additional droiddoc options
197 // Available variables for substitution:
198 //
199 // $(location <label>): the path to the droiddoc_option_files with name <label>
Sundong Ahndd567f92018-07-31 17:19:11 +0900200 Droiddoc_options []string
201
Sundong Ahn054b19a2018-10-19 13:46:09 +0900202 // a list of top-level directories containing files to merge qualifier annotations
203 // (i.e. those intended to be included in the stubs written) from.
204 Merge_annotations_dirs []string
205
206 // a list of top-level directories containing Java stub files to merge show/hide annotations from.
207 Merge_inclusion_annotations_dirs []string
208
209 // If set to true, the path of dist files is apistubs/core. Defaults to false.
210 Core_lib *bool
211
Sundong Ahn80a87b32019-05-13 15:02:50 +0900212 // don't create dist rules.
213 No_dist *bool `blueprint:"mutated"`
214
Paul Duffin37e0b772019-12-30 17:20:10 +0000215 // indicates whether system and test apis should be managed.
216 Has_system_and_test_apis bool `blueprint:"mutated"`
217
Jiyong Parkc678ad32018-04-10 13:07:10 +0900218 // TODO: determines whether to create HTML doc or not
219 //Html_doc *bool
220}
221
Paul Duffind1b3a922020-01-22 11:57:20 +0000222type scopePaths struct {
223 stubsHeaderPath android.Paths
224 stubsImplPath android.Paths
225 apiFilePath android.Path
226}
227
Paul Duffin56d44902020-01-31 13:36:25 +0000228// Common code between sdk library and sdk library import
229type commonToSdkLibraryAndImport struct {
230 scopePaths map[*apiScope]*scopePaths
231}
232
233func (c *commonToSdkLibraryAndImport) getScopePaths(scope *apiScope) *scopePaths {
234 if c.scopePaths == nil {
235 c.scopePaths = make(map[*apiScope]*scopePaths)
236 }
237 paths := c.scopePaths[scope]
238 if paths == nil {
239 paths = &scopePaths{}
240 c.scopePaths[scope] = paths
241 }
242
243 return paths
244}
245
Inseob Kimc0907f12019-02-08 21:00:45 +0900246type SdkLibrary struct {
Sundong Ahn054b19a2018-10-19 13:46:09 +0900247 Library
Jiyong Parkc678ad32018-04-10 13:07:10 +0900248
Sundong Ahn054b19a2018-10-19 13:46:09 +0900249 sdkLibraryProperties sdkLibraryProperties
Jiyong Parkc678ad32018-04-10 13:07:10 +0900250
Paul Duffin56d44902020-01-31 13:36:25 +0000251 commonToSdkLibraryAndImport
Jooyung Han58f26ab2019-12-18 15:34:32 +0900252
Jooyung Han624058e2019-12-24 18:38:06 +0900253 permissionsFile android.Path
Jiyong Parkc678ad32018-04-10 13:07:10 +0900254}
255
Inseob Kimc0907f12019-02-08 21:00:45 +0900256var _ Dependency = (*SdkLibrary)(nil)
257var _ SdkLibraryDependency = (*SdkLibrary)(nil)
Colin Cross897d2ed2019-02-11 14:03:51 -0800258
Paul Duffind1b3a922020-01-22 11:57:20 +0000259func (module *SdkLibrary) getActiveApiScopes() apiScopes {
260 if module.sdkLibraryProperties.Has_system_and_test_apis {
261 return allApiScopes
262 } else {
263 return apiScopes{apiScopePublic}
264 }
265}
266
Inseob Kimc0907f12019-02-08 21:00:45 +0900267func (module *SdkLibrary) DepsMutator(ctx android.BottomUpMutatorContext) {
Jiyong Parke3ef3c82019-07-15 15:31:16 +0900268 useBuiltStubs := !ctx.Config().UnbundledBuildUsePrebuiltSdks()
Paul Duffind1b3a922020-01-22 11:57:20 +0000269 for _, apiScope := range module.getActiveApiScopes() {
270 // Add dependencies to the stubs library
Jiyong Parke3ef3c82019-07-15 15:31:16 +0900271 if useBuiltStubs {
Paul Duffind1b3a922020-01-22 11:57:20 +0000272 ctx.AddVariationDependencies(nil, apiScope.stubsTag, module.stubsName(apiScope))
Jiyong Parke3ef3c82019-07-15 15:31:16 +0900273 }
Paul Duffind1b3a922020-01-22 11:57:20 +0000274
275 ctx.AddVariationDependencies(nil, apiScope.apiFileTag, module.docsName(apiScope))
Sundong Ahn054b19a2018-10-19 13:46:09 +0900276 }
277
278 module.Library.deps(ctx)
Jiyong Parkc678ad32018-04-10 13:07:10 +0900279}
280
Inseob Kimc0907f12019-02-08 21:00:45 +0900281func (module *SdkLibrary) GenerateAndroidBuildActions(ctx android.ModuleContext) {
Paul Duffin43db9be2019-12-30 17:35:49 +0000282 // Don't build an implementation library if this is api only.
283 if !proptools.Bool(module.sdkLibraryProperties.Api_only) {
284 module.Library.GenerateAndroidBuildActions(ctx)
285 }
Sundong Ahn054b19a2018-10-19 13:46:09 +0900286
Jooyung Han624058e2019-12-24 18:38:06 +0900287 module.buildPermissionsFile(ctx)
Jooyung Han58f26ab2019-12-18 15:34:32 +0900288
Sundong Ahn57368eb2018-07-06 11:20:23 +0900289 // Record the paths to the header jars of the library (stubs and impl).
Paul Duffind1b3a922020-01-22 11:57:20 +0000290 // When this java_sdk_library is depended upon from others via "libs" property,
Jiyong Parkc678ad32018-04-10 13:07:10 +0900291 // the recorded paths will be returned depending on the link type of the caller.
292 ctx.VisitDirectDeps(func(to android.Module) {
293 otherName := ctx.OtherModuleName(to)
294 tag := ctx.OtherModuleDependencyTag(to)
295
Sundong Ahn57368eb2018-07-06 11:20:23 +0900296 if lib, ok := to.(Dependency); ok {
Paul Duffind1b3a922020-01-22 11:57:20 +0000297 if scopeTag, ok := tag.(scopeDependencyTag); ok {
298 apiScope := scopeTag.apiScope
299 scopePaths := module.getScopePaths(apiScope)
300 scopePaths.stubsHeaderPath = lib.HeaderJars()
301 scopePaths.stubsImplPath = lib.ImplementationJars()
Jiyong Parkc678ad32018-04-10 13:07:10 +0900302 }
303 }
Sundong Ahn20e998b2018-07-24 11:19:26 +0900304 if doc, ok := to.(ApiFilePath); ok {
Paul Duffind1b3a922020-01-22 11:57:20 +0000305 if scopeTag, ok := tag.(scopeDependencyTag); ok {
306 apiScope := scopeTag.apiScope
307 scopePaths := module.getScopePaths(apiScope)
308 scopePaths.apiFilePath = doc.ApiFilePath()
309 } else {
Sundong Ahn20e998b2018-07-24 11:19:26 +0900310 ctx.ModuleErrorf("depends on module %q of unknown tag %q", otherName, tag)
311 }
312 }
Jiyong Parkc678ad32018-04-10 13:07:10 +0900313 })
314}
315
Jooyung Han624058e2019-12-24 18:38:06 +0900316func (module *SdkLibrary) buildPermissionsFile(ctx android.ModuleContext) {
317 xmlContent := fmt.Sprintf(permissionsTemplate, module.BaseModuleName(), module.implPath())
318 permissionsFile := android.PathForModuleOut(ctx, module.xmlFileName())
Jooyung Han58f26ab2019-12-18 15:34:32 +0900319
Jooyung Han624058e2019-12-24 18:38:06 +0900320 ctx.Build(pctx, android.BuildParams{
321 Rule: android.WriteFile,
322 Output: permissionsFile,
323 Description: "Generating " + module.BaseModuleName() + " permissions",
324 Args: map[string]string{
325 "content": xmlContent,
326 },
327 })
Jooyung Han58f26ab2019-12-18 15:34:32 +0900328
Jooyung Han624058e2019-12-24 18:38:06 +0900329 module.permissionsFile = permissionsFile
Jooyung Han58f26ab2019-12-18 15:34:32 +0900330}
331
Jooyung Han624058e2019-12-24 18:38:06 +0900332func (module *SdkLibrary) OutputFiles(tag string) (android.Paths, error) {
333 switch tag {
334 case ".xml":
335 return android.Paths{module.permissionsFile}, nil
336 }
337 return module.Library.OutputFiles(tag)
Jooyung Han58f26ab2019-12-18 15:34:32 +0900338}
339
Jiyong Park0b0e1b92019-12-03 13:24:29 +0900340func (module *SdkLibrary) AndroidMkEntries() []android.AndroidMkEntries {
Paul Duffin43db9be2019-12-30 17:35:49 +0000341 if proptools.Bool(module.sdkLibraryProperties.Api_only) {
342 return nil
343 }
Jiyong Park0b0e1b92019-12-03 13:24:29 +0900344 entriesList := module.Library.AndroidMkEntries()
345 entries := &entriesList[0]
Jaewoong Jungb0c127c2019-08-29 14:56:03 -0700346 entries.Required = append(entries.Required, module.xmlFileName())
Sundong Ahn054b19a2018-10-19 13:46:09 +0900347
Jaewoong Jungb0c127c2019-08-29 14:56:03 -0700348 entries.ExtraFooters = []android.AndroidMkExtraFootersFunc{
349 func(w io.Writer, name, prefix, moduleDir string, entries *android.AndroidMkEntries) {
Jaewoong Jungb0c127c2019-08-29 14:56:03 -0700350 if !Bool(module.sdkLibraryProperties.No_dist) {
351 // Create a phony module that installs the impl library, for the case when this lib is
352 // in PRODUCT_PACKAGES.
353 owner := module.ModuleBase.Owner()
354 if owner == "" {
355 if Bool(module.sdkLibraryProperties.Core_lib) {
356 owner = "core"
357 } else {
358 owner = "android"
359 }
360 }
Paul Duffind1b3a922020-01-22 11:57:20 +0000361
362 // Create dist rules to install the stubs libs and api files to the dist dir
363 for _, apiScope := range module.getActiveApiScopes() {
364 if scopePaths, ok := module.scopePaths[apiScope]; ok {
365 if len(scopePaths.stubsHeaderPath) == 1 {
366 fmt.Fprintln(w, "$(call dist-for-goals,sdk win_sdk,"+
367 scopePaths.stubsImplPath.Strings()[0]+
368 ":"+path.Join("apistubs", owner, apiScope.name,
369 module.BaseModuleName()+".jar")+")")
370 }
371 if scopePaths.apiFilePath != nil {
372 fmt.Fprintln(w, "$(call dist-for-goals,sdk win_sdk,"+
373 scopePaths.apiFilePath.String()+
374 ":"+path.Join("apistubs", owner, apiScope.name, "api",
375 module.BaseModuleName()+".txt")+")")
376 }
377 }
Sundong Ahn80a87b32019-05-13 15:02:50 +0900378 }
Sundong Ahn4fd04bb2018-08-31 18:01:37 +0900379 }
Jaewoong Jungb0c127c2019-08-29 14:56:03 -0700380 },
Jiyong Park82484c02018-04-23 21:41:26 +0900381 }
Jiyong Park0b0e1b92019-12-03 13:24:29 +0900382 return entriesList
Jiyong Park82484c02018-04-23 21:41:26 +0900383}
384
Jiyong Parkc678ad32018-04-10 13:07:10 +0900385// Module name of the stubs library
Paul Duffind1b3a922020-01-22 11:57:20 +0000386func (module *SdkLibrary) stubsName(apiScope *apiScope) string {
387 return apiScope.stubsModuleName(module.BaseModuleName())
Jiyong Parkc678ad32018-04-10 13:07:10 +0900388}
389
390// Module name of the docs
Paul Duffind1b3a922020-01-22 11:57:20 +0000391func (module *SdkLibrary) docsName(apiScope *apiScope) string {
392 return apiScope.docsModuleName(module.BaseModuleName())
Jiyong Parkc678ad32018-04-10 13:07:10 +0900393}
394
395// Module name of the runtime implementation library
Inseob Kimc0907f12019-02-08 21:00:45 +0900396func (module *SdkLibrary) implName() string {
Sundong Ahn054b19a2018-10-19 13:46:09 +0900397 return module.BaseModuleName()
Jiyong Parkc678ad32018-04-10 13:07:10 +0900398}
399
400// File path to the runtime implementation library
Inseob Kimc0907f12019-02-08 21:00:45 +0900401func (module *SdkLibrary) implPath() string {
Jooyung Han58f26ab2019-12-18 15:34:32 +0900402 if apexName := module.ApexName(); apexName != "" {
403 // TODO(b/146468504): ApexName() is only a soong module name, not apex name.
404 // In most cases, this works fine. But when apex_name is set or override_apex is used
405 // this can be wrong.
406 return fmt.Sprintf("/apex/%s/javalib/%s.jar", apexName, module.implName())
407 }
Jiyong Parkc678ad32018-04-10 13:07:10 +0900408 partition := "system"
409 if module.SocSpecific() {
410 partition = "vendor"
411 } else if module.DeviceSpecific() {
412 partition = "odm"
413 } else if module.ProductSpecific() {
414 partition = "product"
Sundong Ahn0d7dff42019-12-04 12:53:44 +0900415 } else if module.SystemExtSpecific() {
416 partition = "system_ext"
Jiyong Parkc678ad32018-04-10 13:07:10 +0900417 }
418 return "/" + partition + "/framework/" + module.implName() + ".jar"
419}
420
421// Module name of the XML file for the lib
Inseob Kimc0907f12019-02-08 21:00:45 +0900422func (module *SdkLibrary) xmlFileName() string {
Jiyong Parkc678ad32018-04-10 13:07:10 +0900423 return module.BaseModuleName() + sdkXmlFileSuffix
424}
425
Paul Duffin12ceb462019-12-24 20:31:31 +0000426// Get the sdk version for use when compiling the stubs library.
Paul Duffind1b3a922020-01-22 11:57:20 +0000427func (module *SdkLibrary) sdkVersionForStubsLibrary(mctx android.LoadHookContext, apiScope *apiScope) string {
Paul Duffin12ceb462019-12-24 20:31:31 +0000428 sdkDep := decodeSdkDep(mctx, sdkContext(&module.Library))
429 if sdkDep.hasStandardLibs() {
430 // If building against a standard sdk then use the sdk version appropriate for the scope.
Paul Duffind1b3a922020-01-22 11:57:20 +0000431 return apiScope.sdkVersion
Paul Duffin12ceb462019-12-24 20:31:31 +0000432 } else {
433 // Otherwise, use no system module.
434 return "none"
435 }
436}
437
Jiyong Parkc678ad32018-04-10 13:07:10 +0900438// $(INTERNAL_PLATFORM_<apiTagName>_API_FILE) points to the generated
439// api file for the current source
440// TODO: remove this when apicheck is done in soong
Paul Duffind1b3a922020-01-22 11:57:20 +0000441func (module *SdkLibrary) apiTagName(apiScope *apiScope) string {
442 return strings.Replace(strings.ToUpper(module.BaseModuleName()), ".", "_", -1) + apiScope.apiFileMakeVariableSuffix
Jiyong Parkc678ad32018-04-10 13:07:10 +0900443}
444
Paul Duffind1b3a922020-01-22 11:57:20 +0000445func (module *SdkLibrary) latestApiFilegroupName(apiScope *apiScope) string {
446 return ":" + module.BaseModuleName() + ".api." + apiScope.name + ".latest"
Jiyong Park58c518b2018-05-12 22:29:12 +0900447}
Jiyong Parkc678ad32018-04-10 13:07:10 +0900448
Paul Duffind1b3a922020-01-22 11:57:20 +0000449func (module *SdkLibrary) latestRemovedApiFilegroupName(apiScope *apiScope) string {
450 return ":" + module.BaseModuleName() + "-removed.api." + apiScope.name + ".latest"
Jiyong Parkc678ad32018-04-10 13:07:10 +0900451}
452
453// Creates a static java library that has API stubs
Paul Duffind1b3a922020-01-22 11:57:20 +0000454func (module *SdkLibrary) createStubsLibrary(mctx android.LoadHookContext, apiScope *apiScope) {
Jiyong Parkc678ad32018-04-10 13:07:10 +0900455 props := struct {
Sundong Ahn0d7dff42019-12-04 12:53:44 +0900456 Name *string
457 Srcs []string
Paul Duffin367ab912019-12-23 19:40:36 +0000458 Installable *bool
Sundong Ahn0d7dff42019-12-04 12:53:44 +0900459 Sdk_version *string
Paul Duffin12ceb462019-12-24 20:31:31 +0000460 System_modules *string
Sundong Ahn0d7dff42019-12-04 12:53:44 +0900461 Libs []string
462 Soc_specific *bool
463 Device_specific *bool
464 Product_specific *bool
465 System_ext_specific *bool
466 Compile_dex *bool
Sundong Ahn0d7dff42019-12-04 12:53:44 +0900467 Java_version *string
468 Product_variables struct {
Jiyong Parkc678ad32018-04-10 13:07:10 +0900469 Unbundled_build struct {
470 Enabled *bool
471 }
Jiyong Park82484c02018-04-23 21:41:26 +0900472 Pdk struct {
473 Enabled *bool
474 }
Jiyong Parkc678ad32018-04-10 13:07:10 +0900475 }
Sundong Ahn054b19a2018-10-19 13:46:09 +0900476 Openjdk9 struct {
477 Srcs []string
478 Javacflags []string
479 }
Jiyong Parkc678ad32018-04-10 13:07:10 +0900480 }{}
481
Jiyong Parkdf130542018-04-27 16:29:21 +0900482 props.Name = proptools.StringPtr(module.stubsName(apiScope))
Jiyong Parkc678ad32018-04-10 13:07:10 +0900483 // sources are generated from the droiddoc
Jiyong Parkdf130542018-04-27 16:29:21 +0900484 props.Srcs = []string{":" + module.docsName(apiScope)}
Paul Duffin12ceb462019-12-24 20:31:31 +0000485 sdkVersion := module.sdkVersionForStubsLibrary(mctx, apiScope)
Paul Duffin52d398a2019-06-11 12:31:14 +0100486 props.Sdk_version = proptools.StringPtr(sdkVersion)
Paul Duffin12ceb462019-12-24 20:31:31 +0000487 props.System_modules = module.Library.Module.deviceProperties.System_modules
Paul Duffin367ab912019-12-23 19:40:36 +0000488 props.Installable = proptools.BoolPtr(false)
Sundong Ahn054b19a2018-10-19 13:46:09 +0900489 props.Libs = module.sdkLibraryProperties.Stub_only_libs
Jiyong Parkc678ad32018-04-10 13:07:10 +0900490 // Unbundled apps will use the prebult one from /prebuilts/sdk
Colin Cross10932872019-04-18 14:27:12 -0700491 if mctx.Config().UnbundledBuildUsePrebuiltSdks() {
Colin Cross2c77ceb2019-01-21 11:56:21 -0800492 props.Product_variables.Unbundled_build.Enabled = proptools.BoolPtr(false)
493 }
Jiyong Park82484c02018-04-23 21:41:26 +0900494 props.Product_variables.Pdk.Enabled = proptools.BoolPtr(false)
Sundong Ahn054b19a2018-10-19 13:46:09 +0900495 props.Openjdk9.Srcs = module.Library.Module.properties.Openjdk9.Srcs
496 props.Openjdk9.Javacflags = module.Library.Module.properties.Openjdk9.Javacflags
497 props.Java_version = module.Library.Module.properties.Java_version
498 if module.Library.Module.deviceProperties.Compile_dex != nil {
499 props.Compile_dex = module.Library.Module.deviceProperties.Compile_dex
Sundong Ahndd567f92018-07-31 17:19:11 +0900500 }
Jiyong Parkc678ad32018-04-10 13:07:10 +0900501
502 if module.SocSpecific() {
503 props.Soc_specific = proptools.BoolPtr(true)
504 } else if module.DeviceSpecific() {
505 props.Device_specific = proptools.BoolPtr(true)
506 } else if module.ProductSpecific() {
507 props.Product_specific = proptools.BoolPtr(true)
Sundong Ahn0d7dff42019-12-04 12:53:44 +0900508 } else if module.SystemExtSpecific() {
509 props.System_ext_specific = proptools.BoolPtr(true)
Jiyong Parkc678ad32018-04-10 13:07:10 +0900510 }
511
Colin Cross84dfc3d2019-09-25 11:33:01 -0700512 mctx.CreateModule(LibraryFactory, &props)
Jiyong Parkc678ad32018-04-10 13:07:10 +0900513}
514
515// Creates a droiddoc module that creates stubs source files from the given full source
516// files
Paul Duffind1b3a922020-01-22 11:57:20 +0000517func (module *SdkLibrary) createStubsSources(mctx android.LoadHookContext, apiScope *apiScope) {
Jiyong Parkc678ad32018-04-10 13:07:10 +0900518 props := struct {
Sundong Ahn054b19a2018-10-19 13:46:09 +0900519 Name *string
520 Srcs []string
521 Installable *bool
Paul Duffin52d398a2019-06-11 12:31:14 +0100522 Sdk_version *string
Paul Duffin12ceb462019-12-24 20:31:31 +0000523 System_modules *string
Sundong Ahn054b19a2018-10-19 13:46:09 +0900524 Libs []string
Paul Duffin11512472019-02-11 15:55:17 +0000525 Arg_files []string
Sundong Ahn054b19a2018-10-19 13:46:09 +0900526 Args *string
527 Api_tag_name *string
528 Api_filename *string
529 Removed_api_filename *string
Sundong Ahn054b19a2018-10-19 13:46:09 +0900530 Java_version *string
531 Merge_annotations_dirs []string
532 Merge_inclusion_annotations_dirs []string
533 Check_api struct {
Inseob Kim38449af2019-02-28 14:24:05 +0900534 Current ApiToCheck
535 Last_released ApiToCheck
536 Ignore_missing_latest_api *bool
Jiyong Park58c518b2018-05-12 22:29:12 +0900537 }
Sundong Ahn1b92c822018-05-29 11:35:17 +0900538 Aidl struct {
539 Include_dirs []string
540 Local_include_dirs []string
541 }
Jiyong Parkc678ad32018-04-10 13:07:10 +0900542 }{}
543
Paul Duffin250e6192019-06-07 10:44:37 +0100544 sdkDep := decodeSdkDep(mctx, sdkContext(&module.Library))
Paul Duffin12ceb462019-12-24 20:31:31 +0000545 // Use the platform API if standard libraries were requested, otherwise use
546 // no default libraries.
Paul Duffin52d398a2019-06-11 12:31:14 +0100547 sdkVersion := ""
548 if !sdkDep.hasStandardLibs() {
549 sdkVersion = "none"
550 }
Paul Duffin250e6192019-06-07 10:44:37 +0100551
Jiyong Parkdf130542018-04-27 16:29:21 +0900552 props.Name = proptools.StringPtr(module.docsName(apiScope))
Sundong Ahn054b19a2018-10-19 13:46:09 +0900553 props.Srcs = append(props.Srcs, module.Library.Module.properties.Srcs...)
Paul Duffin52d398a2019-06-11 12:31:14 +0100554 props.Sdk_version = proptools.StringPtr(sdkVersion)
Paul Duffin12ceb462019-12-24 20:31:31 +0000555 props.System_modules = module.Library.Module.deviceProperties.System_modules
Jiyong Parkc678ad32018-04-10 13:07:10 +0900556 props.Installable = proptools.BoolPtr(false)
Sundong Ahne6f0b052018-06-05 16:46:14 +0900557 // A droiddoc module has only one Libs property and doesn't distinguish between
558 // shared libs and static libs. So we need to add both of these libs to Libs property.
Sundong Ahn054b19a2018-10-19 13:46:09 +0900559 props.Libs = module.Library.Module.properties.Libs
560 props.Libs = append(props.Libs, module.Library.Module.properties.Static_libs...)
561 props.Aidl.Include_dirs = module.Library.Module.deviceProperties.Aidl.Include_dirs
562 props.Aidl.Local_include_dirs = module.Library.Module.deviceProperties.Aidl.Local_include_dirs
Sundong Ahn054b19a2018-10-19 13:46:09 +0900563 props.Java_version = module.Library.Module.properties.Java_version
Jiyong Parkc678ad32018-04-10 13:07:10 +0900564
Sundong Ahn054b19a2018-10-19 13:46:09 +0900565 props.Merge_annotations_dirs = module.sdkLibraryProperties.Merge_annotations_dirs
566 props.Merge_inclusion_annotations_dirs = module.sdkLibraryProperties.Merge_inclusion_annotations_dirs
567
Paul Duffin235ffff2019-12-24 10:41:30 +0000568 droiddocArgs := []string{}
569 if len(module.sdkLibraryProperties.Api_packages) != 0 {
570 droiddocArgs = append(droiddocArgs, "--stub-packages "+strings.Join(module.sdkLibraryProperties.Api_packages, ":"))
571 }
572 if len(module.sdkLibraryProperties.Hidden_api_packages) != 0 {
573 droiddocArgs = append(droiddocArgs,
574 android.JoinWithPrefix(module.sdkLibraryProperties.Hidden_api_packages, " --hide-package "))
575 }
576 droiddocArgs = append(droiddocArgs, module.sdkLibraryProperties.Droiddoc_options...)
577 disabledWarnings := []string{
578 "MissingPermission",
579 "BroadcastBehavior",
580 "HiddenSuperclass",
581 "DeprecationMismatch",
582 "UnavailableSymbol",
583 "SdkConstant",
584 "HiddenTypeParameter",
585 "Todo",
586 "Typo",
587 }
588 droiddocArgs = append(droiddocArgs, android.JoinWithPrefix(disabledWarnings, "--hide "))
Sundong Ahnfb2721f2018-09-17 13:23:09 +0900589
Jiyong Parkdf130542018-04-27 16:29:21 +0900590 switch apiScope {
591 case apiScopeSystem:
Paul Duffin235ffff2019-12-24 10:41:30 +0000592 droiddocArgs = append(droiddocArgs, "-showAnnotation android.annotation.SystemApi")
Jiyong Parkdf130542018-04-27 16:29:21 +0900593 case apiScopeTest:
Paul Duffin235ffff2019-12-24 10:41:30 +0000594 droiddocArgs = append(droiddocArgs, " -showAnnotation android.annotation.TestApi")
Jiyong Parkc678ad32018-04-10 13:07:10 +0900595 }
Paul Duffin11512472019-02-11 15:55:17 +0000596 props.Arg_files = module.sdkLibraryProperties.Droiddoc_option_files
Paul Duffin235ffff2019-12-24 10:41:30 +0000597 props.Args = proptools.StringPtr(strings.Join(droiddocArgs, " "))
Jiyong Parkc678ad32018-04-10 13:07:10 +0900598
599 // List of APIs identified from the provided source files are created. They are later
600 // compared against to the not-yet-released (a.k.a current) list of APIs and to the
601 // last-released (a.k.a numbered) list of API.
Paul Duffind1b3a922020-01-22 11:57:20 +0000602 currentApiFileName := apiScope.apiFilePrefix + "current.txt"
603 removedApiFileName := apiScope.apiFilePrefix + "removed.txt"
Paul Duffin749f98f2019-12-30 17:23:46 +0000604 apiDir := module.getApiDir()
605 currentApiFileName = path.Join(apiDir, currentApiFileName)
606 removedApiFileName = path.Join(apiDir, removedApiFileName)
Jiyong Park58c518b2018-05-12 22:29:12 +0900607 // TODO(jiyong): remove these three props
Jiyong Parkdf130542018-04-27 16:29:21 +0900608 props.Api_tag_name = proptools.StringPtr(module.apiTagName(apiScope))
Jiyong Parkc678ad32018-04-10 13:07:10 +0900609 props.Api_filename = proptools.StringPtr(currentApiFileName)
610 props.Removed_api_filename = proptools.StringPtr(removedApiFileName)
611
Jiyong Park58c518b2018-05-12 22:29:12 +0900612 // check against the not-yet-release API
613 props.Check_api.Current.Api_file = proptools.StringPtr(currentApiFileName)
614 props.Check_api.Current.Removed_api_file = proptools.StringPtr(removedApiFileName)
Jiyong Park58c518b2018-05-12 22:29:12 +0900615
616 // check against the latest released API
617 props.Check_api.Last_released.Api_file = proptools.StringPtr(
618 module.latestApiFilegroupName(apiScope))
619 props.Check_api.Last_released.Removed_api_file = proptools.StringPtr(
620 module.latestRemovedApiFilegroupName(apiScope))
Inseob Kim38449af2019-02-28 14:24:05 +0900621 props.Check_api.Ignore_missing_latest_api = proptools.BoolPtr(true)
Jiyong Park58c518b2018-05-12 22:29:12 +0900622
Colin Cross84dfc3d2019-09-25 11:33:01 -0700623 mctx.CreateModule(DroidstubsFactory, &props)
Jiyong Parkc678ad32018-04-10 13:07:10 +0900624}
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) {
Jiyong Parkc678ad32018-04-10 13:07:10 +0900628 // creates a prebuilt_etc module to actually place the xml file under
629 // <partition>/etc/permissions
630 etcProps := struct {
Sundong Ahn0d7dff42019-12-04 12:53:44 +0900631 Name *string
632 Src *string
633 Sub_dir *string
634 Soc_specific *bool
635 Device_specific *bool
636 Product_specific *bool
637 System_ext_specific *bool
Jiyong Parkc678ad32018-04-10 13:07:10 +0900638 }{}
639 etcProps.Name = proptools.StringPtr(module.xmlFileName())
Jooyung Han624058e2019-12-24 18:38:06 +0900640 etcProps.Src = proptools.StringPtr(":" + module.BaseModuleName() + "{.xml}")
Jiyong Parkc678ad32018-04-10 13:07:10 +0900641 etcProps.Sub_dir = proptools.StringPtr("permissions")
642 if module.SocSpecific() {
643 etcProps.Soc_specific = proptools.BoolPtr(true)
644 } else if module.DeviceSpecific() {
645 etcProps.Device_specific = proptools.BoolPtr(true)
646 } else if module.ProductSpecific() {
647 etcProps.Product_specific = proptools.BoolPtr(true)
Sundong Ahn0d7dff42019-12-04 12:53:44 +0900648 } else if module.SystemExtSpecific() {
649 etcProps.System_ext_specific = proptools.BoolPtr(true)
Jiyong Parkc678ad32018-04-10 13:07:10 +0900650 }
Colin Cross84dfc3d2019-09-25 11:33:01 -0700651 mctx.CreateModule(android.PrebuiltEtcFactory, &etcProps)
Jiyong Parkc678ad32018-04-10 13:07:10 +0900652}
653
Jiyong Park6a927c42020-01-21 02:03:43 +0900654func (module *SdkLibrary) PrebuiltJars(ctx android.BaseModuleContext, s sdkSpec) android.Paths {
655 var ver sdkVersion
656 var kind sdkKind
657 if s.usePrebuilt(ctx) {
658 ver = s.version
659 kind = s.kind
Jiyong Parkc678ad32018-04-10 13:07:10 +0900660 } else {
Jiyong Park6a927c42020-01-21 02:03:43 +0900661 // We don't have prebuilt SDK for the specific sdkVersion.
662 // Instead of breaking the build, fallback to use "system_current"
663 ver = sdkVersionCurrent
664 kind = sdkSystem
Sundong Ahn054b19a2018-10-19 13:46:09 +0900665 }
Jiyong Park6a927c42020-01-21 02:03:43 +0900666
667 dir := filepath.Join("prebuilts", "sdk", ver.String(), kind.String())
Sundong Ahn054b19a2018-10-19 13:46:09 +0900668 jar := filepath.Join(dir, module.BaseModuleName()+".jar")
669 jarPath := android.ExistentPathForSource(ctx, jar)
Sundong Ahnae418ac2019-02-28 15:01:28 +0900670 if !jarPath.Valid() {
Colin Cross07c88562020-01-07 09:34:44 -0800671 if ctx.Config().AllowMissingDependencies() {
672 return android.Paths{android.PathForSource(ctx, jar)}
673 } else {
Jiyong Park6a927c42020-01-21 02:03:43 +0900674 ctx.PropertyErrorf("sdk_library", "invalid sdk version %q, %q does not exist", s.raw, jar)
Colin Cross07c88562020-01-07 09:34:44 -0800675 }
Sundong Ahnae418ac2019-02-28 15:01:28 +0900676 return nil
677 }
Sundong Ahn054b19a2018-10-19 13:46:09 +0900678 return android.Paths{jarPath.Path()}
679}
680
Paul Duffind1b3a922020-01-22 11:57:20 +0000681func (module *SdkLibrary) sdkJars(
682 ctx android.BaseModuleContext,
683 sdkVersion sdkSpec,
684 headerJars bool) android.Paths {
685
Sundong Ahn054b19a2018-10-19 13:46:09 +0900686 // This module is just a wrapper for the stubs.
Colin Cross10932872019-04-18 14:27:12 -0700687 if ctx.Config().UnbundledBuildUsePrebuiltSdks() {
Sundong Ahn054b19a2018-10-19 13:46:09 +0900688 return module.PrebuiltJars(ctx, sdkVersion)
689 } else {
Paul Duffind1b3a922020-01-22 11:57:20 +0000690 if !sdkVersion.specified() {
691 if headerJars {
692 return module.Library.HeaderJars()
693 } else {
694 return module.Library.ImplementationJars()
695 }
696 }
Paul Duffin726d23c2020-01-22 16:30:37 +0000697 var apiScope *apiScope
Jiyong Park6a927c42020-01-21 02:03:43 +0900698 switch sdkVersion.kind {
699 case sdkSystem:
Paul Duffin726d23c2020-01-22 16:30:37 +0000700 apiScope = apiScopeSystem
701 case sdkTest:
702 apiScope = apiScopeTest
Jiyong Park6a927c42020-01-21 02:03:43 +0900703 case sdkPrivate:
Sundong Ahn054b19a2018-10-19 13:46:09 +0900704 return module.Library.HeaderJars()
Jiyong Park6a927c42020-01-21 02:03:43 +0900705 default:
Paul Duffin726d23c2020-01-22 16:30:37 +0000706 apiScope = apiScopePublic
Paul Duffind1b3a922020-01-22 11:57:20 +0000707 }
708
Paul Duffin726d23c2020-01-22 16:30:37 +0000709 paths := module.getScopePaths(apiScope)
Paul Duffind1b3a922020-01-22 11:57:20 +0000710 if headerJars {
711 return paths.stubsHeaderPath
712 } else {
713 return paths.stubsImplPath
Sundong Ahn054b19a2018-10-19 13:46:09 +0900714 }
Jiyong Parkc678ad32018-04-10 13:07:10 +0900715 }
716}
717
Sundong Ahn241cd372018-07-13 16:16:44 +0900718// to satisfy SdkLibraryDependency interface
Paul Duffind1b3a922020-01-22 11:57:20 +0000719func (module *SdkLibrary) SdkHeaderJars(ctx android.BaseModuleContext, sdkVersion sdkSpec) android.Paths {
720 return module.sdkJars(ctx, sdkVersion, true /*headerJars*/)
721}
722
723// to satisfy SdkLibraryDependency interface
Jiyong Park6a927c42020-01-21 02:03:43 +0900724func (module *SdkLibrary) SdkImplementationJars(ctx android.BaseModuleContext, sdkVersion sdkSpec) android.Paths {
Paul Duffind1b3a922020-01-22 11:57:20 +0000725 return module.sdkJars(ctx, sdkVersion, false /*headerJars*/)
Sundong Ahn241cd372018-07-13 16:16:44 +0900726}
727
Sundong Ahn80a87b32019-05-13 15:02:50 +0900728func (module *SdkLibrary) SetNoDist() {
729 module.sdkLibraryProperties.No_dist = proptools.BoolPtr(true)
730}
731
Colin Cross571cccf2019-02-04 11:22:08 -0800732var javaSdkLibrariesKey = android.NewOnceKey("javaSdkLibraries")
733
Jiyong Park82484c02018-04-23 21:41:26 +0900734func javaSdkLibraries(config android.Config) *[]string {
Colin Cross571cccf2019-02-04 11:22:08 -0800735 return config.Once(javaSdkLibrariesKey, func() interface{} {
Jiyong Park82484c02018-04-23 21:41:26 +0900736 return &[]string{}
737 }).(*[]string)
738}
739
Paul Duffin749f98f2019-12-30 17:23:46 +0000740func (module *SdkLibrary) getApiDir() string {
741 return proptools.StringDefault(module.sdkLibraryProperties.Api_dir, "api")
742}
743
Jiyong Parkc678ad32018-04-10 13:07:10 +0900744// For a java_sdk_library module, create internal modules for stubs, docs,
745// runtime libs and xml file. If requested, the stubs and docs are created twice
746// once for public API level and once for system API level
Colin Crossf8b860a2019-04-16 14:43:28 -0700747func (module *SdkLibrary) CreateInternalModules(mctx android.LoadHookContext) {
Inseob Kim6e93ac92019-03-21 17:43:49 +0900748 if len(module.Library.Module.properties.Srcs) == 0 {
Inseob Kimc0907f12019-02-08 21:00:45 +0900749 mctx.PropertyErrorf("srcs", "java_sdk_library must specify srcs")
Jooyung Han58f26ab2019-12-18 15:34:32 +0900750 return
Inseob Kimc0907f12019-02-08 21:00:45 +0900751 }
752
Paul Duffin37e0b772019-12-30 17:20:10 +0000753 // If this builds against standard libraries (i.e. is not part of the core libraries)
754 // then assume it provides both system and test apis. Otherwise, assume it does not and
755 // also assume it does not contribute to the dist build.
756 sdkDep := decodeSdkDep(mctx, sdkContext(&module.Library))
757 hasSystemAndTestApis := sdkDep.hasStandardLibs()
758 module.sdkLibraryProperties.Has_system_and_test_apis = hasSystemAndTestApis
759 module.sdkLibraryProperties.No_dist = proptools.BoolPtr(!hasSystemAndTestApis)
760
Inseob Kim8098faa2019-03-18 10:19:51 +0900761 missing_current_api := false
762
Paul Duffind1b3a922020-01-22 11:57:20 +0000763 activeScopes := module.getActiveApiScopes()
764
Paul Duffin749f98f2019-12-30 17:23:46 +0000765 apiDir := module.getApiDir()
Paul Duffind1b3a922020-01-22 11:57:20 +0000766 for _, scope := range activeScopes {
Inseob Kim8098faa2019-03-18 10:19:51 +0900767 for _, api := range []string{"current.txt", "removed.txt"} {
Paul Duffind1b3a922020-01-22 11:57:20 +0000768 path := path.Join(mctx.ModuleDir(), apiDir, scope.apiFilePrefix+api)
Inseob Kim8098faa2019-03-18 10:19:51 +0900769 p := android.ExistentPathForSource(mctx, path)
770 if !p.Valid() {
771 mctx.ModuleErrorf("Current api file %#v doesn't exist", path)
772 missing_current_api = true
773 }
774 }
775 }
776
777 if missing_current_api {
778 script := "build/soong/scripts/gen-java-current-api-files.sh"
779 p := android.ExistentPathForSource(mctx, script)
780
781 if !p.Valid() {
782 panic(fmt.Sprintf("script file %s doesn't exist", script))
783 }
784
785 mctx.ModuleErrorf("One or more current api files are missing. "+
786 "You can update them by:\n"+
Paul Duffin37e0b772019-12-30 17:20:10 +0000787 "%s %q %s && m update-api",
Paul Duffind1b3a922020-01-22 11:57:20 +0000788 script, filepath.Join(mctx.ModuleDir(), apiDir),
789 strings.Join(activeScopes.Strings(func(s *apiScope) string { return s.apiFilePrefix }), " "))
Inseob Kim8098faa2019-03-18 10:19:51 +0900790 return
791 }
792
Paul Duffind1b3a922020-01-22 11:57:20 +0000793 for _, scope := range activeScopes {
794 module.createStubsLibrary(mctx, scope)
795 module.createStubsSources(mctx, scope)
Inseob Kimc0907f12019-02-08 21:00:45 +0900796 }
797
Paul Duffin43db9be2019-12-30 17:35:49 +0000798 if !proptools.Bool(module.sdkLibraryProperties.Api_only) {
799 // for runtime
800 module.createXmlFile(mctx)
801
802 // record java_sdk_library modules so that they are exported to make
803 javaSdkLibraries := javaSdkLibraries(mctx.Config())
804 javaSdkLibrariesLock.Lock()
805 defer javaSdkLibrariesLock.Unlock()
806 *javaSdkLibraries = append(*javaSdkLibraries, module.BaseModuleName())
807 }
Inseob Kimc0907f12019-02-08 21:00:45 +0900808}
809
810func (module *SdkLibrary) InitSdkLibraryProperties() {
Sundong Ahn054b19a2018-10-19 13:46:09 +0900811 module.AddProperties(
812 &module.sdkLibraryProperties,
813 &module.Library.Module.properties,
814 &module.Library.Module.dexpreoptProperties,
815 &module.Library.Module.deviceProperties,
816 &module.Library.Module.protoProperties,
817 )
818
819 module.Library.Module.properties.Installable = proptools.BoolPtr(true)
820 module.Library.Module.deviceProperties.IsSDKLibrary = true
Inseob Kimc0907f12019-02-08 21:00:45 +0900821}
Sundong Ahn054b19a2018-10-19 13:46:09 +0900822
Jaewoong Jung4f158ee2019-07-11 10:05:35 -0700823// java_sdk_library is a special Java library that provides optional platform APIs to apps.
824// In practice, it can be viewed as a combination of several modules: 1) stubs library that clients
825// are linked against to, 2) droiddoc module that internally generates API stubs source files,
826// 3) the real runtime shared library that implements the APIs, and 4) XML file for adding
827// the runtime lib to the classpath at runtime if requested via <uses-library>.
Inseob Kimc0907f12019-02-08 21:00:45 +0900828func SdkLibraryFactory() android.Module {
829 module := &SdkLibrary{}
830 module.InitSdkLibraryProperties()
Jooyung Han58f26ab2019-12-18 15:34:32 +0900831 android.InitApexModule(module)
Sundong Ahn054b19a2018-10-19 13:46:09 +0900832 InitJavaModule(module, android.HostAndDeviceSupported)
Colin Crossf8b860a2019-04-16 14:43:28 -0700833 android.AddLoadHook(module, func(ctx android.LoadHookContext) { module.CreateInternalModules(ctx) })
Jiyong Parkc678ad32018-04-10 13:07:10 +0900834 return module
835}
Colin Cross79c7c262019-04-17 11:11:46 -0700836
837//
838// SDK library prebuilts
839//
840
Paul Duffin56d44902020-01-31 13:36:25 +0000841// Properties associated with each api scope.
842type sdkLibraryScopeProperties struct {
Colin Cross79c7c262019-04-17 11:11:46 -0700843 Jars []string `android:"path"`
844
845 Sdk_version *string
846
Colin Cross79c7c262019-04-17 11:11:46 -0700847 // List of shared java libs that this module has dependencies to
848 Libs []string
Colin Cross79c7c262019-04-17 11:11:46 -0700849}
850
Paul Duffin56d44902020-01-31 13:36:25 +0000851type sdkLibraryImportProperties struct {
852 // Properties associated with the public api scope.
853 Public sdkLibraryScopeProperties
854
855 // Properties associated with the system api scope.
856 System sdkLibraryScopeProperties
857
858 // Properties associated with the test api scope.
859 Test sdkLibraryScopeProperties
860}
861
Colin Cross79c7c262019-04-17 11:11:46 -0700862type sdkLibraryImport struct {
863 android.ModuleBase
864 android.DefaultableModuleBase
865 prebuilt android.Prebuilt
866
867 properties sdkLibraryImportProperties
868
Paul Duffin56d44902020-01-31 13:36:25 +0000869 // Legacy properties for the public api scope.
870 //
871 // Should use properties.Public instead.
872 legacyPublicProperties sdkLibraryScopeProperties
873
874 commonToSdkLibraryAndImport
Colin Cross79c7c262019-04-17 11:11:46 -0700875}
876
877var _ SdkLibraryDependency = (*sdkLibraryImport)(nil)
878
Jaewoong Jung4f158ee2019-07-11 10:05:35 -0700879// java_sdk_library_import imports a prebuilt java_sdk_library.
Colin Cross79c7c262019-04-17 11:11:46 -0700880func sdkLibraryImportFactory() android.Module {
881 module := &sdkLibraryImport{}
882
Paul Duffin56d44902020-01-31 13:36:25 +0000883 module.AddProperties(&module.properties, &module.legacyPublicProperties)
Colin Cross79c7c262019-04-17 11:11:46 -0700884
Paul Duffin56d44902020-01-31 13:36:25 +0000885 android.InitPrebuiltModule(module, &module.legacyPublicProperties.Jars)
Colin Cross79c7c262019-04-17 11:11:46 -0700886 InitJavaModule(module, android.HostAndDeviceSupported)
887
888 android.AddLoadHook(module, func(mctx android.LoadHookContext) { module.createInternalModules(mctx) })
889 return module
890}
891
892func (module *sdkLibraryImport) Prebuilt() *android.Prebuilt {
893 return &module.prebuilt
894}
895
896func (module *sdkLibraryImport) Name() string {
897 return module.prebuilt.Name(module.ModuleBase.Name())
898}
899
900func (module *sdkLibraryImport) createInternalModules(mctx android.LoadHookContext) {
Colin Cross79c7c262019-04-17 11:11:46 -0700901
Paul Duffin56d44902020-01-31 13:36:25 +0000902 // Prepend any of the libs from the legacy public properties to the libs for each of the
903 // scopes to avoid having to duplicate them in each scope.
904 for _, scopeProperties := range module.scopeProperties() {
905 scopeProperties.Libs = append(module.legacyPublicProperties.Libs, scopeProperties.Libs...)
Colin Cross79c7c262019-04-17 11:11:46 -0700906 }
907
Paul Duffin56d44902020-01-31 13:36:25 +0000908 if module.legacyPublicProperties.Jars != nil {
909 if module.properties.Public.Jars != nil {
910 mctx.ModuleErrorf("cannot set both `jars` and `public.jars`")
911 return
912 }
913
914 // The legacy set of properties has been used so copy them over the public properties.
915 module.properties.Public = module.legacyPublicProperties
916 }
917
918 for apiScope, scopeProperties := range module.scopeProperties() {
919 if len(scopeProperties.Jars) == 0 {
920 continue
921 }
922
923 // Creates a java import for the jar with ".stubs" suffix
924 props := struct {
925 Name *string
926 Soc_specific *bool
927 Device_specific *bool
928 Product_specific *bool
929 System_ext_specific *bool
930 Sdk_version *string
931 Libs []string
932 Jars []string
933 }{}
934
935 props.Name = proptools.StringPtr(apiScope.stubsModuleName(module.BaseModuleName()))
936 props.Sdk_version = scopeProperties.Sdk_version
937 props.Libs = scopeProperties.Libs
938 props.Jars = scopeProperties.Jars
939
940 if module.SocSpecific() {
941 props.Soc_specific = proptools.BoolPtr(true)
942 } else if module.DeviceSpecific() {
943 props.Device_specific = proptools.BoolPtr(true)
944 } else if module.ProductSpecific() {
945 props.Product_specific = proptools.BoolPtr(true)
946 } else if module.SystemExtSpecific() {
947 props.System_ext_specific = proptools.BoolPtr(true)
948 }
949
950 mctx.CreateModule(ImportFactory, &props)
951 }
Colin Cross79c7c262019-04-17 11:11:46 -0700952
953 javaSdkLibraries := javaSdkLibraries(mctx.Config())
954 javaSdkLibrariesLock.Lock()
955 defer javaSdkLibrariesLock.Unlock()
956 *javaSdkLibraries = append(*javaSdkLibraries, module.BaseModuleName())
957}
958
Paul Duffin56d44902020-01-31 13:36:25 +0000959func (module *sdkLibraryImport) scopeProperties() map[*apiScope]*sdkLibraryScopeProperties {
960 p := make(map[*apiScope]*sdkLibraryScopeProperties)
961 p[apiScopePublic] = &module.properties.Public
962 p[apiScopeSystem] = &module.properties.System
963 p[apiScopeTest] = &module.properties.Test
964 return p
965}
966
Colin Cross79c7c262019-04-17 11:11:46 -0700967func (module *sdkLibraryImport) DepsMutator(ctx android.BottomUpMutatorContext) {
Paul Duffin56d44902020-01-31 13:36:25 +0000968 for apiScope, scopeProperties := range module.scopeProperties() {
969 if len(scopeProperties.Jars) == 0 {
970 continue
971 }
972
973 // Add dependencies to the prebuilt stubs library
974 ctx.AddVariationDependencies(nil, apiScope.stubsTag, apiScope.stubsModuleName(module.BaseModuleName()))
975 }
Colin Cross79c7c262019-04-17 11:11:46 -0700976}
977
978func (module *sdkLibraryImport) GenerateAndroidBuildActions(ctx android.ModuleContext) {
979 // Record the paths to the prebuilt stubs library.
980 ctx.VisitDirectDeps(func(to android.Module) {
981 tag := ctx.OtherModuleDependencyTag(to)
982
Paul Duffin56d44902020-01-31 13:36:25 +0000983 if lib, ok := to.(Dependency); ok {
984 if scopeTag, ok := tag.(scopeDependencyTag); ok {
985 apiScope := scopeTag.apiScope
986 scopePaths := module.getScopePaths(apiScope)
987 scopePaths.stubsHeaderPath = lib.HeaderJars()
988 }
Colin Cross79c7c262019-04-17 11:11:46 -0700989 }
990 })
991}
992
Paul Duffin56d44902020-01-31 13:36:25 +0000993func (module *sdkLibraryImport) sdkJars(
994 ctx android.BaseModuleContext,
995 sdkVersion sdkSpec) android.Paths {
996
997 var apiScope *apiScope
998 switch sdkVersion.kind {
999 case sdkSystem:
1000 apiScope = apiScopeSystem
1001 case sdkTest:
1002 apiScope = apiScopeTest
1003 default:
1004 apiScope = apiScopePublic
1005 }
1006
1007 paths := module.getScopePaths(apiScope)
1008 return paths.stubsHeaderPath
1009}
1010
Colin Cross79c7c262019-04-17 11:11:46 -07001011// to satisfy SdkLibraryDependency interface
Jiyong Park6a927c42020-01-21 02:03:43 +09001012func (module *sdkLibraryImport) SdkHeaderJars(ctx android.BaseModuleContext, sdkVersion sdkSpec) android.Paths {
Colin Cross79c7c262019-04-17 11:11:46 -07001013 // This module is just a wrapper for the prebuilt stubs.
Paul Duffin56d44902020-01-31 13:36:25 +00001014 return module.sdkJars(ctx, sdkVersion)
Colin Cross79c7c262019-04-17 11:11:46 -07001015}
1016
1017// to satisfy SdkLibraryDependency interface
Jiyong Park6a927c42020-01-21 02:03:43 +09001018func (module *sdkLibraryImport) SdkImplementationJars(ctx android.BaseModuleContext, sdkVersion sdkSpec) android.Paths {
Colin Cross79c7c262019-04-17 11:11:46 -07001019 // This module is just a wrapper for the stubs.
Paul Duffin56d44902020-01-31 13:36:25 +00001020 return module.sdkJars(ctx, sdkVersion)
Colin Cross79c7c262019-04-17 11:11:46 -07001021}