blob: 94c2d13a977765f7240001e0e04c45dd4bfe1c35 [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) {
Paul Duffind1b3a922020-01-22 11:57:20 +0000268 for _, apiScope := range module.getActiveApiScopes() {
269 // Add dependencies to the stubs library
Paul Duffin50061512020-01-21 16:31:05 +0000270 ctx.AddVariationDependencies(nil, apiScope.stubsTag, module.stubsName(apiScope))
Paul Duffind1b3a922020-01-22 11:57:20 +0000271
Paul Duffin50061512020-01-21 16:31:05 +0000272 // And the api file
Paul Duffind1b3a922020-01-22 11:57:20 +0000273 ctx.AddVariationDependencies(nil, apiScope.apiFileTag, module.docsName(apiScope))
Sundong Ahn054b19a2018-10-19 13:46:09 +0900274 }
275
276 module.Library.deps(ctx)
Jiyong Parkc678ad32018-04-10 13:07:10 +0900277}
278
Inseob Kimc0907f12019-02-08 21:00:45 +0900279func (module *SdkLibrary) GenerateAndroidBuildActions(ctx android.ModuleContext) {
Paul Duffin43db9be2019-12-30 17:35:49 +0000280 // Don't build an implementation library if this is api only.
281 if !proptools.Bool(module.sdkLibraryProperties.Api_only) {
282 module.Library.GenerateAndroidBuildActions(ctx)
283 }
Sundong Ahn054b19a2018-10-19 13:46:09 +0900284
Jooyung Han624058e2019-12-24 18:38:06 +0900285 module.buildPermissionsFile(ctx)
Jooyung Han58f26ab2019-12-18 15:34:32 +0900286
Sundong Ahn57368eb2018-07-06 11:20:23 +0900287 // Record the paths to the header jars of the library (stubs and impl).
Paul Duffind1b3a922020-01-22 11:57:20 +0000288 // When this java_sdk_library is depended upon from others via "libs" property,
Jiyong Parkc678ad32018-04-10 13:07:10 +0900289 // the recorded paths will be returned depending on the link type of the caller.
290 ctx.VisitDirectDeps(func(to android.Module) {
291 otherName := ctx.OtherModuleName(to)
292 tag := ctx.OtherModuleDependencyTag(to)
293
Sundong Ahn57368eb2018-07-06 11:20:23 +0900294 if lib, ok := to.(Dependency); ok {
Paul Duffind1b3a922020-01-22 11:57:20 +0000295 if scopeTag, ok := tag.(scopeDependencyTag); ok {
296 apiScope := scopeTag.apiScope
297 scopePaths := module.getScopePaths(apiScope)
298 scopePaths.stubsHeaderPath = lib.HeaderJars()
299 scopePaths.stubsImplPath = lib.ImplementationJars()
Jiyong Parkc678ad32018-04-10 13:07:10 +0900300 }
301 }
Sundong Ahn20e998b2018-07-24 11:19:26 +0900302 if doc, ok := to.(ApiFilePath); ok {
Paul Duffind1b3a922020-01-22 11:57:20 +0000303 if scopeTag, ok := tag.(scopeDependencyTag); ok {
304 apiScope := scopeTag.apiScope
305 scopePaths := module.getScopePaths(apiScope)
306 scopePaths.apiFilePath = doc.ApiFilePath()
307 } else {
Sundong Ahn20e998b2018-07-24 11:19:26 +0900308 ctx.ModuleErrorf("depends on module %q of unknown tag %q", otherName, tag)
309 }
310 }
Jiyong Parkc678ad32018-04-10 13:07:10 +0900311 })
312}
313
Jooyung Han624058e2019-12-24 18:38:06 +0900314func (module *SdkLibrary) buildPermissionsFile(ctx android.ModuleContext) {
315 xmlContent := fmt.Sprintf(permissionsTemplate, module.BaseModuleName(), module.implPath())
316 permissionsFile := android.PathForModuleOut(ctx, module.xmlFileName())
Jooyung Han58f26ab2019-12-18 15:34:32 +0900317
Jooyung Han624058e2019-12-24 18:38:06 +0900318 ctx.Build(pctx, android.BuildParams{
319 Rule: android.WriteFile,
320 Output: permissionsFile,
321 Description: "Generating " + module.BaseModuleName() + " permissions",
322 Args: map[string]string{
323 "content": xmlContent,
324 },
325 })
Jooyung Han58f26ab2019-12-18 15:34:32 +0900326
Jooyung Han624058e2019-12-24 18:38:06 +0900327 module.permissionsFile = permissionsFile
Jooyung Han58f26ab2019-12-18 15:34:32 +0900328}
329
Jooyung Han624058e2019-12-24 18:38:06 +0900330func (module *SdkLibrary) OutputFiles(tag string) (android.Paths, error) {
331 switch tag {
332 case ".xml":
333 return android.Paths{module.permissionsFile}, nil
334 }
335 return module.Library.OutputFiles(tag)
Jooyung Han58f26ab2019-12-18 15:34:32 +0900336}
337
Jiyong Park0b0e1b92019-12-03 13:24:29 +0900338func (module *SdkLibrary) AndroidMkEntries() []android.AndroidMkEntries {
Paul Duffin43db9be2019-12-30 17:35:49 +0000339 if proptools.Bool(module.sdkLibraryProperties.Api_only) {
340 return nil
341 }
Jiyong Park0b0e1b92019-12-03 13:24:29 +0900342 entriesList := module.Library.AndroidMkEntries()
343 entries := &entriesList[0]
Jaewoong Jungb0c127c2019-08-29 14:56:03 -0700344 entries.Required = append(entries.Required, module.xmlFileName())
Sundong Ahn054b19a2018-10-19 13:46:09 +0900345
Jaewoong Jungb0c127c2019-08-29 14:56:03 -0700346 entries.ExtraFooters = []android.AndroidMkExtraFootersFunc{
347 func(w io.Writer, name, prefix, moduleDir string, entries *android.AndroidMkEntries) {
Jaewoong Jungb0c127c2019-08-29 14:56:03 -0700348 if !Bool(module.sdkLibraryProperties.No_dist) {
349 // Create a phony module that installs the impl library, for the case when this lib is
350 // in PRODUCT_PACKAGES.
351 owner := module.ModuleBase.Owner()
352 if owner == "" {
353 if Bool(module.sdkLibraryProperties.Core_lib) {
354 owner = "core"
355 } else {
356 owner = "android"
357 }
358 }
Paul Duffind1b3a922020-01-22 11:57:20 +0000359
360 // Create dist rules to install the stubs libs and api files to the dist dir
361 for _, apiScope := range module.getActiveApiScopes() {
362 if scopePaths, ok := module.scopePaths[apiScope]; ok {
363 if len(scopePaths.stubsHeaderPath) == 1 {
364 fmt.Fprintln(w, "$(call dist-for-goals,sdk win_sdk,"+
365 scopePaths.stubsImplPath.Strings()[0]+
366 ":"+path.Join("apistubs", owner, apiScope.name,
367 module.BaseModuleName()+".jar")+")")
368 }
369 if scopePaths.apiFilePath != nil {
370 fmt.Fprintln(w, "$(call dist-for-goals,sdk win_sdk,"+
371 scopePaths.apiFilePath.String()+
372 ":"+path.Join("apistubs", owner, apiScope.name, "api",
373 module.BaseModuleName()+".txt")+")")
374 }
375 }
Sundong Ahn80a87b32019-05-13 15:02:50 +0900376 }
Sundong Ahn4fd04bb2018-08-31 18:01:37 +0900377 }
Jaewoong Jungb0c127c2019-08-29 14:56:03 -0700378 },
Jiyong Park82484c02018-04-23 21:41:26 +0900379 }
Jiyong Park0b0e1b92019-12-03 13:24:29 +0900380 return entriesList
Jiyong Park82484c02018-04-23 21:41:26 +0900381}
382
Jiyong Parkc678ad32018-04-10 13:07:10 +0900383// Module name of the stubs library
Paul Duffind1b3a922020-01-22 11:57:20 +0000384func (module *SdkLibrary) stubsName(apiScope *apiScope) string {
385 return apiScope.stubsModuleName(module.BaseModuleName())
Jiyong Parkc678ad32018-04-10 13:07:10 +0900386}
387
388// Module name of the docs
Paul Duffind1b3a922020-01-22 11:57:20 +0000389func (module *SdkLibrary) docsName(apiScope *apiScope) string {
390 return apiScope.docsModuleName(module.BaseModuleName())
Jiyong Parkc678ad32018-04-10 13:07:10 +0900391}
392
393// Module name of the runtime implementation library
Inseob Kimc0907f12019-02-08 21:00:45 +0900394func (module *SdkLibrary) implName() string {
Sundong Ahn054b19a2018-10-19 13:46:09 +0900395 return module.BaseModuleName()
Jiyong Parkc678ad32018-04-10 13:07:10 +0900396}
397
398// File path to the runtime implementation library
Inseob Kimc0907f12019-02-08 21:00:45 +0900399func (module *SdkLibrary) implPath() string {
Jooyung Han58f26ab2019-12-18 15:34:32 +0900400 if apexName := module.ApexName(); apexName != "" {
401 // TODO(b/146468504): ApexName() is only a soong module name, not apex name.
402 // In most cases, this works fine. But when apex_name is set or override_apex is used
403 // this can be wrong.
404 return fmt.Sprintf("/apex/%s/javalib/%s.jar", apexName, module.implName())
405 }
Jiyong Parkc678ad32018-04-10 13:07:10 +0900406 partition := "system"
407 if module.SocSpecific() {
408 partition = "vendor"
409 } else if module.DeviceSpecific() {
410 partition = "odm"
411 } else if module.ProductSpecific() {
412 partition = "product"
Sundong Ahn0d7dff42019-12-04 12:53:44 +0900413 } else if module.SystemExtSpecific() {
414 partition = "system_ext"
Jiyong Parkc678ad32018-04-10 13:07:10 +0900415 }
416 return "/" + partition + "/framework/" + module.implName() + ".jar"
417}
418
419// Module name of the XML file for the lib
Inseob Kimc0907f12019-02-08 21:00:45 +0900420func (module *SdkLibrary) xmlFileName() string {
Jiyong Parkc678ad32018-04-10 13:07:10 +0900421 return module.BaseModuleName() + sdkXmlFileSuffix
422}
423
Paul Duffin12ceb462019-12-24 20:31:31 +0000424// Get the sdk version for use when compiling the stubs library.
Paul Duffind1b3a922020-01-22 11:57:20 +0000425func (module *SdkLibrary) sdkVersionForStubsLibrary(mctx android.LoadHookContext, apiScope *apiScope) string {
Paul Duffin12ceb462019-12-24 20:31:31 +0000426 sdkDep := decodeSdkDep(mctx, sdkContext(&module.Library))
427 if sdkDep.hasStandardLibs() {
428 // If building against a standard sdk then use the sdk version appropriate for the scope.
Paul Duffind1b3a922020-01-22 11:57:20 +0000429 return apiScope.sdkVersion
Paul Duffin12ceb462019-12-24 20:31:31 +0000430 } else {
431 // Otherwise, use no system module.
432 return "none"
433 }
434}
435
Jiyong Parkc678ad32018-04-10 13:07:10 +0900436// $(INTERNAL_PLATFORM_<apiTagName>_API_FILE) points to the generated
437// api file for the current source
438// TODO: remove this when apicheck is done in soong
Paul Duffind1b3a922020-01-22 11:57:20 +0000439func (module *SdkLibrary) apiTagName(apiScope *apiScope) string {
440 return strings.Replace(strings.ToUpper(module.BaseModuleName()), ".", "_", -1) + apiScope.apiFileMakeVariableSuffix
Jiyong Parkc678ad32018-04-10 13:07:10 +0900441}
442
Paul Duffind1b3a922020-01-22 11:57:20 +0000443func (module *SdkLibrary) latestApiFilegroupName(apiScope *apiScope) string {
444 return ":" + module.BaseModuleName() + ".api." + apiScope.name + ".latest"
Jiyong Park58c518b2018-05-12 22:29:12 +0900445}
Jiyong Parkc678ad32018-04-10 13:07:10 +0900446
Paul Duffind1b3a922020-01-22 11:57:20 +0000447func (module *SdkLibrary) latestRemovedApiFilegroupName(apiScope *apiScope) string {
448 return ":" + module.BaseModuleName() + "-removed.api." + apiScope.name + ".latest"
Jiyong Parkc678ad32018-04-10 13:07:10 +0900449}
450
451// Creates a static java library that has API stubs
Paul Duffind1b3a922020-01-22 11:57:20 +0000452func (module *SdkLibrary) createStubsLibrary(mctx android.LoadHookContext, apiScope *apiScope) {
Jiyong Parkc678ad32018-04-10 13:07:10 +0900453 props := struct {
Sundong Ahn0d7dff42019-12-04 12:53:44 +0900454 Name *string
455 Srcs []string
Paul Duffin367ab912019-12-23 19:40:36 +0000456 Installable *bool
Sundong Ahn0d7dff42019-12-04 12:53:44 +0900457 Sdk_version *string
Paul Duffin12ceb462019-12-24 20:31:31 +0000458 System_modules *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 Duffin367ab912019-12-23 19:40:36 +0000483 props.Installable = proptools.BoolPtr(false)
Sundong Ahn054b19a2018-10-19 13:46:09 +0900484 props.Libs = module.sdkLibraryProperties.Stub_only_libs
Jiyong Park82484c02018-04-23 21:41:26 +0900485 props.Product_variables.Pdk.Enabled = proptools.BoolPtr(false)
Sundong Ahn054b19a2018-10-19 13:46:09 +0900486 props.Openjdk9.Srcs = module.Library.Module.properties.Openjdk9.Srcs
487 props.Openjdk9.Javacflags = module.Library.Module.properties.Openjdk9.Javacflags
488 props.Java_version = module.Library.Module.properties.Java_version
489 if module.Library.Module.deviceProperties.Compile_dex != nil {
490 props.Compile_dex = module.Library.Module.deviceProperties.Compile_dex
Sundong Ahndd567f92018-07-31 17:19:11 +0900491 }
Jiyong Parkc678ad32018-04-10 13:07:10 +0900492
493 if module.SocSpecific() {
494 props.Soc_specific = proptools.BoolPtr(true)
495 } else if module.DeviceSpecific() {
496 props.Device_specific = proptools.BoolPtr(true)
497 } else if module.ProductSpecific() {
498 props.Product_specific = proptools.BoolPtr(true)
Sundong Ahn0d7dff42019-12-04 12:53:44 +0900499 } else if module.SystemExtSpecific() {
500 props.System_ext_specific = proptools.BoolPtr(true)
Jiyong Parkc678ad32018-04-10 13:07:10 +0900501 }
502
Colin Cross84dfc3d2019-09-25 11:33:01 -0700503 mctx.CreateModule(LibraryFactory, &props)
Jiyong Parkc678ad32018-04-10 13:07:10 +0900504}
505
506// Creates a droiddoc module that creates stubs source files from the given full source
507// files
Paul Duffind1b3a922020-01-22 11:57:20 +0000508func (module *SdkLibrary) createStubsSources(mctx android.LoadHookContext, apiScope *apiScope) {
Jiyong Parkc678ad32018-04-10 13:07:10 +0900509 props := struct {
Sundong Ahn054b19a2018-10-19 13:46:09 +0900510 Name *string
511 Srcs []string
512 Installable *bool
Paul Duffin52d398a2019-06-11 12:31:14 +0100513 Sdk_version *string
Paul Duffin12ceb462019-12-24 20:31:31 +0000514 System_modules *string
Sundong Ahn054b19a2018-10-19 13:46:09 +0900515 Libs []string
Paul Duffin11512472019-02-11 15:55:17 +0000516 Arg_files []string
Sundong Ahn054b19a2018-10-19 13:46:09 +0900517 Args *string
518 Api_tag_name *string
519 Api_filename *string
520 Removed_api_filename *string
Sundong Ahn054b19a2018-10-19 13:46:09 +0900521 Java_version *string
522 Merge_annotations_dirs []string
523 Merge_inclusion_annotations_dirs []string
524 Check_api struct {
Inseob Kim38449af2019-02-28 14:24:05 +0900525 Current ApiToCheck
526 Last_released ApiToCheck
527 Ignore_missing_latest_api *bool
Jiyong Park58c518b2018-05-12 22:29:12 +0900528 }
Sundong Ahn1b92c822018-05-29 11:35:17 +0900529 Aidl struct {
530 Include_dirs []string
531 Local_include_dirs []string
532 }
Jiyong Parkc678ad32018-04-10 13:07:10 +0900533 }{}
534
Paul Duffin250e6192019-06-07 10:44:37 +0100535 sdkDep := decodeSdkDep(mctx, sdkContext(&module.Library))
Paul Duffin12ceb462019-12-24 20:31:31 +0000536 // Use the platform API if standard libraries were requested, otherwise use
537 // no default libraries.
Paul Duffin52d398a2019-06-11 12:31:14 +0100538 sdkVersion := ""
539 if !sdkDep.hasStandardLibs() {
540 sdkVersion = "none"
541 }
Paul Duffin250e6192019-06-07 10:44:37 +0100542
Jiyong Parkdf130542018-04-27 16:29:21 +0900543 props.Name = proptools.StringPtr(module.docsName(apiScope))
Sundong Ahn054b19a2018-10-19 13:46:09 +0900544 props.Srcs = append(props.Srcs, module.Library.Module.properties.Srcs...)
Paul Duffin52d398a2019-06-11 12:31:14 +0100545 props.Sdk_version = proptools.StringPtr(sdkVersion)
Paul Duffin12ceb462019-12-24 20:31:31 +0000546 props.System_modules = module.Library.Module.deviceProperties.System_modules
Jiyong Parkc678ad32018-04-10 13:07:10 +0900547 props.Installable = proptools.BoolPtr(false)
Sundong Ahne6f0b052018-06-05 16:46:14 +0900548 // A droiddoc module has only one Libs property and doesn't distinguish between
549 // shared libs and static libs. So we need to add both of these libs to Libs property.
Sundong Ahn054b19a2018-10-19 13:46:09 +0900550 props.Libs = module.Library.Module.properties.Libs
551 props.Libs = append(props.Libs, module.Library.Module.properties.Static_libs...)
552 props.Aidl.Include_dirs = module.Library.Module.deviceProperties.Aidl.Include_dirs
553 props.Aidl.Local_include_dirs = module.Library.Module.deviceProperties.Aidl.Local_include_dirs
Sundong Ahn054b19a2018-10-19 13:46:09 +0900554 props.Java_version = module.Library.Module.properties.Java_version
Jiyong Parkc678ad32018-04-10 13:07:10 +0900555
Sundong Ahn054b19a2018-10-19 13:46:09 +0900556 props.Merge_annotations_dirs = module.sdkLibraryProperties.Merge_annotations_dirs
557 props.Merge_inclusion_annotations_dirs = module.sdkLibraryProperties.Merge_inclusion_annotations_dirs
558
Paul Duffin235ffff2019-12-24 10:41:30 +0000559 droiddocArgs := []string{}
560 if len(module.sdkLibraryProperties.Api_packages) != 0 {
561 droiddocArgs = append(droiddocArgs, "--stub-packages "+strings.Join(module.sdkLibraryProperties.Api_packages, ":"))
562 }
563 if len(module.sdkLibraryProperties.Hidden_api_packages) != 0 {
564 droiddocArgs = append(droiddocArgs,
565 android.JoinWithPrefix(module.sdkLibraryProperties.Hidden_api_packages, " --hide-package "))
566 }
567 droiddocArgs = append(droiddocArgs, module.sdkLibraryProperties.Droiddoc_options...)
568 disabledWarnings := []string{
569 "MissingPermission",
570 "BroadcastBehavior",
571 "HiddenSuperclass",
572 "DeprecationMismatch",
573 "UnavailableSymbol",
574 "SdkConstant",
575 "HiddenTypeParameter",
576 "Todo",
577 "Typo",
578 }
579 droiddocArgs = append(droiddocArgs, android.JoinWithPrefix(disabledWarnings, "--hide "))
Sundong Ahnfb2721f2018-09-17 13:23:09 +0900580
Jiyong Parkdf130542018-04-27 16:29:21 +0900581 switch apiScope {
582 case apiScopeSystem:
Paul Duffin235ffff2019-12-24 10:41:30 +0000583 droiddocArgs = append(droiddocArgs, "-showAnnotation android.annotation.SystemApi")
Jiyong Parkdf130542018-04-27 16:29:21 +0900584 case apiScopeTest:
Paul Duffin235ffff2019-12-24 10:41:30 +0000585 droiddocArgs = append(droiddocArgs, " -showAnnotation android.annotation.TestApi")
Jiyong Parkc678ad32018-04-10 13:07:10 +0900586 }
Paul Duffin11512472019-02-11 15:55:17 +0000587 props.Arg_files = module.sdkLibraryProperties.Droiddoc_option_files
Paul Duffin235ffff2019-12-24 10:41:30 +0000588 props.Args = proptools.StringPtr(strings.Join(droiddocArgs, " "))
Jiyong Parkc678ad32018-04-10 13:07:10 +0900589
590 // List of APIs identified from the provided source files are created. They are later
591 // compared against to the not-yet-released (a.k.a current) list of APIs and to the
592 // last-released (a.k.a numbered) list of API.
Paul Duffind1b3a922020-01-22 11:57:20 +0000593 currentApiFileName := apiScope.apiFilePrefix + "current.txt"
594 removedApiFileName := apiScope.apiFilePrefix + "removed.txt"
Paul Duffin749f98f2019-12-30 17:23:46 +0000595 apiDir := module.getApiDir()
596 currentApiFileName = path.Join(apiDir, currentApiFileName)
597 removedApiFileName = path.Join(apiDir, removedApiFileName)
Jiyong Park58c518b2018-05-12 22:29:12 +0900598 // TODO(jiyong): remove these three props
Jiyong Parkdf130542018-04-27 16:29:21 +0900599 props.Api_tag_name = proptools.StringPtr(module.apiTagName(apiScope))
Jiyong Parkc678ad32018-04-10 13:07:10 +0900600 props.Api_filename = proptools.StringPtr(currentApiFileName)
601 props.Removed_api_filename = proptools.StringPtr(removedApiFileName)
602
Jiyong Park58c518b2018-05-12 22:29:12 +0900603 // check against the not-yet-release API
604 props.Check_api.Current.Api_file = proptools.StringPtr(currentApiFileName)
605 props.Check_api.Current.Removed_api_file = proptools.StringPtr(removedApiFileName)
Jiyong Park58c518b2018-05-12 22:29:12 +0900606
607 // check against the latest released API
608 props.Check_api.Last_released.Api_file = proptools.StringPtr(
609 module.latestApiFilegroupName(apiScope))
610 props.Check_api.Last_released.Removed_api_file = proptools.StringPtr(
611 module.latestRemovedApiFilegroupName(apiScope))
Inseob Kim38449af2019-02-28 14:24:05 +0900612 props.Check_api.Ignore_missing_latest_api = proptools.BoolPtr(true)
Jiyong Park58c518b2018-05-12 22:29:12 +0900613
Colin Cross84dfc3d2019-09-25 11:33:01 -0700614 mctx.CreateModule(DroidstubsFactory, &props)
Jiyong Parkc678ad32018-04-10 13:07:10 +0900615}
616
Jiyong Parkc678ad32018-04-10 13:07:10 +0900617// Creates the xml file that publicizes the runtime library
Colin Crossf8b860a2019-04-16 14:43:28 -0700618func (module *SdkLibrary) createXmlFile(mctx android.LoadHookContext) {
Jiyong Parkc678ad32018-04-10 13:07:10 +0900619 // creates a prebuilt_etc module to actually place the xml file under
620 // <partition>/etc/permissions
621 etcProps := struct {
Sundong Ahn0d7dff42019-12-04 12:53:44 +0900622 Name *string
623 Src *string
624 Sub_dir *string
625 Soc_specific *bool
626 Device_specific *bool
627 Product_specific *bool
628 System_ext_specific *bool
Jiyong Parkc678ad32018-04-10 13:07:10 +0900629 }{}
630 etcProps.Name = proptools.StringPtr(module.xmlFileName())
Jooyung Han624058e2019-12-24 18:38:06 +0900631 etcProps.Src = proptools.StringPtr(":" + module.BaseModuleName() + "{.xml}")
Jiyong Parkc678ad32018-04-10 13:07:10 +0900632 etcProps.Sub_dir = proptools.StringPtr("permissions")
633 if module.SocSpecific() {
634 etcProps.Soc_specific = proptools.BoolPtr(true)
635 } else if module.DeviceSpecific() {
636 etcProps.Device_specific = proptools.BoolPtr(true)
637 } else if module.ProductSpecific() {
638 etcProps.Product_specific = proptools.BoolPtr(true)
Sundong Ahn0d7dff42019-12-04 12:53:44 +0900639 } else if module.SystemExtSpecific() {
640 etcProps.System_ext_specific = proptools.BoolPtr(true)
Jiyong Parkc678ad32018-04-10 13:07:10 +0900641 }
Colin Cross84dfc3d2019-09-25 11:33:01 -0700642 mctx.CreateModule(android.PrebuiltEtcFactory, &etcProps)
Jiyong Parkc678ad32018-04-10 13:07:10 +0900643}
644
Paul Duffin50061512020-01-21 16:31:05 +0000645func PrebuiltJars(ctx android.BaseModuleContext, baseName string, s sdkSpec) android.Paths {
Jiyong Park6a927c42020-01-21 02:03:43 +0900646 var ver sdkVersion
647 var kind sdkKind
648 if s.usePrebuilt(ctx) {
649 ver = s.version
650 kind = s.kind
Jiyong Parkc678ad32018-04-10 13:07:10 +0900651 } else {
Jiyong Park6a927c42020-01-21 02:03:43 +0900652 // We don't have prebuilt SDK for the specific sdkVersion.
653 // Instead of breaking the build, fallback to use "system_current"
654 ver = sdkVersionCurrent
655 kind = sdkSystem
Sundong Ahn054b19a2018-10-19 13:46:09 +0900656 }
Jiyong Park6a927c42020-01-21 02:03:43 +0900657
658 dir := filepath.Join("prebuilts", "sdk", ver.String(), kind.String())
Paul Duffin50061512020-01-21 16:31:05 +0000659 jar := filepath.Join(dir, baseName+".jar")
Sundong Ahn054b19a2018-10-19 13:46:09 +0900660 jarPath := android.ExistentPathForSource(ctx, jar)
Sundong Ahnae418ac2019-02-28 15:01:28 +0900661 if !jarPath.Valid() {
Colin Cross07c88562020-01-07 09:34:44 -0800662 if ctx.Config().AllowMissingDependencies() {
663 return android.Paths{android.PathForSource(ctx, jar)}
664 } else {
Jiyong Park6a927c42020-01-21 02:03:43 +0900665 ctx.PropertyErrorf("sdk_library", "invalid sdk version %q, %q does not exist", s.raw, jar)
Colin Cross07c88562020-01-07 09:34:44 -0800666 }
Sundong Ahnae418ac2019-02-28 15:01:28 +0900667 return nil
668 }
Sundong Ahn054b19a2018-10-19 13:46:09 +0900669 return android.Paths{jarPath.Path()}
670}
671
Paul Duffind1b3a922020-01-22 11:57:20 +0000672func (module *SdkLibrary) sdkJars(
673 ctx android.BaseModuleContext,
674 sdkVersion sdkSpec,
675 headerJars bool) android.Paths {
676
Paul Duffin50061512020-01-21 16:31:05 +0000677 // If a specific numeric version has been requested then use prebuilt versions of the sdk.
678 if sdkVersion.version.isNumbered() {
679 return PrebuiltJars(ctx, module.BaseModuleName(), sdkVersion)
Sundong Ahn054b19a2018-10-19 13:46:09 +0900680 } else {
Paul Duffind1b3a922020-01-22 11:57:20 +0000681 if !sdkVersion.specified() {
682 if headerJars {
683 return module.Library.HeaderJars()
684 } else {
685 return module.Library.ImplementationJars()
686 }
687 }
Paul Duffin726d23c2020-01-22 16:30:37 +0000688 var apiScope *apiScope
Jiyong Park6a927c42020-01-21 02:03:43 +0900689 switch sdkVersion.kind {
690 case sdkSystem:
Paul Duffin726d23c2020-01-22 16:30:37 +0000691 apiScope = apiScopeSystem
692 case sdkTest:
693 apiScope = apiScopeTest
Jiyong Park6a927c42020-01-21 02:03:43 +0900694 case sdkPrivate:
Sundong Ahn054b19a2018-10-19 13:46:09 +0900695 return module.Library.HeaderJars()
Jiyong Park6a927c42020-01-21 02:03:43 +0900696 default:
Paul Duffin726d23c2020-01-22 16:30:37 +0000697 apiScope = apiScopePublic
Paul Duffind1b3a922020-01-22 11:57:20 +0000698 }
699
Paul Duffin726d23c2020-01-22 16:30:37 +0000700 paths := module.getScopePaths(apiScope)
Paul Duffind1b3a922020-01-22 11:57:20 +0000701 if headerJars {
702 return paths.stubsHeaderPath
703 } else {
704 return paths.stubsImplPath
Sundong Ahn054b19a2018-10-19 13:46:09 +0900705 }
Jiyong Parkc678ad32018-04-10 13:07:10 +0900706 }
707}
708
Sundong Ahn241cd372018-07-13 16:16:44 +0900709// to satisfy SdkLibraryDependency interface
Paul Duffind1b3a922020-01-22 11:57:20 +0000710func (module *SdkLibrary) SdkHeaderJars(ctx android.BaseModuleContext, sdkVersion sdkSpec) android.Paths {
711 return module.sdkJars(ctx, sdkVersion, true /*headerJars*/)
712}
713
714// to satisfy SdkLibraryDependency interface
Jiyong Park6a927c42020-01-21 02:03:43 +0900715func (module *SdkLibrary) SdkImplementationJars(ctx android.BaseModuleContext, sdkVersion sdkSpec) android.Paths {
Paul Duffind1b3a922020-01-22 11:57:20 +0000716 return module.sdkJars(ctx, sdkVersion, false /*headerJars*/)
Sundong Ahn241cd372018-07-13 16:16:44 +0900717}
718
Sundong Ahn80a87b32019-05-13 15:02:50 +0900719func (module *SdkLibrary) SetNoDist() {
720 module.sdkLibraryProperties.No_dist = proptools.BoolPtr(true)
721}
722
Colin Cross571cccf2019-02-04 11:22:08 -0800723var javaSdkLibrariesKey = android.NewOnceKey("javaSdkLibraries")
724
Jiyong Park82484c02018-04-23 21:41:26 +0900725func javaSdkLibraries(config android.Config) *[]string {
Colin Cross571cccf2019-02-04 11:22:08 -0800726 return config.Once(javaSdkLibrariesKey, func() interface{} {
Jiyong Park82484c02018-04-23 21:41:26 +0900727 return &[]string{}
728 }).(*[]string)
729}
730
Paul Duffin749f98f2019-12-30 17:23:46 +0000731func (module *SdkLibrary) getApiDir() string {
732 return proptools.StringDefault(module.sdkLibraryProperties.Api_dir, "api")
733}
734
Jiyong Parkc678ad32018-04-10 13:07:10 +0900735// For a java_sdk_library module, create internal modules for stubs, docs,
736// runtime libs and xml file. If requested, the stubs and docs are created twice
737// once for public API level and once for system API level
Colin Crossf8b860a2019-04-16 14:43:28 -0700738func (module *SdkLibrary) CreateInternalModules(mctx android.LoadHookContext) {
Inseob Kim6e93ac92019-03-21 17:43:49 +0900739 if len(module.Library.Module.properties.Srcs) == 0 {
Inseob Kimc0907f12019-02-08 21:00:45 +0900740 mctx.PropertyErrorf("srcs", "java_sdk_library must specify srcs")
Jooyung Han58f26ab2019-12-18 15:34:32 +0900741 return
Inseob Kimc0907f12019-02-08 21:00:45 +0900742 }
743
Paul Duffin37e0b772019-12-30 17:20:10 +0000744 // If this builds against standard libraries (i.e. is not part of the core libraries)
745 // then assume it provides both system and test apis. Otherwise, assume it does not and
746 // also assume it does not contribute to the dist build.
747 sdkDep := decodeSdkDep(mctx, sdkContext(&module.Library))
748 hasSystemAndTestApis := sdkDep.hasStandardLibs()
749 module.sdkLibraryProperties.Has_system_and_test_apis = hasSystemAndTestApis
750 module.sdkLibraryProperties.No_dist = proptools.BoolPtr(!hasSystemAndTestApis)
751
Inseob Kim8098faa2019-03-18 10:19:51 +0900752 missing_current_api := false
753
Paul Duffind1b3a922020-01-22 11:57:20 +0000754 activeScopes := module.getActiveApiScopes()
755
Paul Duffin749f98f2019-12-30 17:23:46 +0000756 apiDir := module.getApiDir()
Paul Duffind1b3a922020-01-22 11:57:20 +0000757 for _, scope := range activeScopes {
Inseob Kim8098faa2019-03-18 10:19:51 +0900758 for _, api := range []string{"current.txt", "removed.txt"} {
Paul Duffind1b3a922020-01-22 11:57:20 +0000759 path := path.Join(mctx.ModuleDir(), apiDir, scope.apiFilePrefix+api)
Inseob Kim8098faa2019-03-18 10:19:51 +0900760 p := android.ExistentPathForSource(mctx, path)
761 if !p.Valid() {
762 mctx.ModuleErrorf("Current api file %#v doesn't exist", path)
763 missing_current_api = true
764 }
765 }
766 }
767
768 if missing_current_api {
769 script := "build/soong/scripts/gen-java-current-api-files.sh"
770 p := android.ExistentPathForSource(mctx, script)
771
772 if !p.Valid() {
773 panic(fmt.Sprintf("script file %s doesn't exist", script))
774 }
775
776 mctx.ModuleErrorf("One or more current api files are missing. "+
777 "You can update them by:\n"+
Paul Duffin37e0b772019-12-30 17:20:10 +0000778 "%s %q %s && m update-api",
Paul Duffind1b3a922020-01-22 11:57:20 +0000779 script, filepath.Join(mctx.ModuleDir(), apiDir),
780 strings.Join(activeScopes.Strings(func(s *apiScope) string { return s.apiFilePrefix }), " "))
Inseob Kim8098faa2019-03-18 10:19:51 +0900781 return
782 }
783
Paul Duffind1b3a922020-01-22 11:57:20 +0000784 for _, scope := range activeScopes {
785 module.createStubsLibrary(mctx, scope)
786 module.createStubsSources(mctx, scope)
Inseob Kimc0907f12019-02-08 21:00:45 +0900787 }
788
Paul Duffin43db9be2019-12-30 17:35:49 +0000789 if !proptools.Bool(module.sdkLibraryProperties.Api_only) {
790 // for runtime
791 module.createXmlFile(mctx)
792
793 // record java_sdk_library modules so that they are exported to make
794 javaSdkLibraries := javaSdkLibraries(mctx.Config())
795 javaSdkLibrariesLock.Lock()
796 defer javaSdkLibrariesLock.Unlock()
797 *javaSdkLibraries = append(*javaSdkLibraries, module.BaseModuleName())
798 }
Inseob Kimc0907f12019-02-08 21:00:45 +0900799}
800
801func (module *SdkLibrary) InitSdkLibraryProperties() {
Sundong Ahn054b19a2018-10-19 13:46:09 +0900802 module.AddProperties(
803 &module.sdkLibraryProperties,
804 &module.Library.Module.properties,
805 &module.Library.Module.dexpreoptProperties,
806 &module.Library.Module.deviceProperties,
807 &module.Library.Module.protoProperties,
808 )
809
810 module.Library.Module.properties.Installable = proptools.BoolPtr(true)
811 module.Library.Module.deviceProperties.IsSDKLibrary = true
Inseob Kimc0907f12019-02-08 21:00:45 +0900812}
Sundong Ahn054b19a2018-10-19 13:46:09 +0900813
Jaewoong Jung4f158ee2019-07-11 10:05:35 -0700814// java_sdk_library is a special Java library that provides optional platform APIs to apps.
815// In practice, it can be viewed as a combination of several modules: 1) stubs library that clients
816// are linked against to, 2) droiddoc module that internally generates API stubs source files,
817// 3) the real runtime shared library that implements the APIs, and 4) XML file for adding
818// the runtime lib to the classpath at runtime if requested via <uses-library>.
Inseob Kimc0907f12019-02-08 21:00:45 +0900819func SdkLibraryFactory() android.Module {
820 module := &SdkLibrary{}
821 module.InitSdkLibraryProperties()
Jooyung Han58f26ab2019-12-18 15:34:32 +0900822 android.InitApexModule(module)
Sundong Ahn054b19a2018-10-19 13:46:09 +0900823 InitJavaModule(module, android.HostAndDeviceSupported)
Colin Crossf8b860a2019-04-16 14:43:28 -0700824 android.AddLoadHook(module, func(ctx android.LoadHookContext) { module.CreateInternalModules(ctx) })
Jiyong Parkc678ad32018-04-10 13:07:10 +0900825 return module
826}
Colin Cross79c7c262019-04-17 11:11:46 -0700827
828//
829// SDK library prebuilts
830//
831
Paul Duffin56d44902020-01-31 13:36:25 +0000832// Properties associated with each api scope.
833type sdkLibraryScopeProperties struct {
Colin Cross79c7c262019-04-17 11:11:46 -0700834 Jars []string `android:"path"`
835
836 Sdk_version *string
837
Colin Cross79c7c262019-04-17 11:11:46 -0700838 // List of shared java libs that this module has dependencies to
839 Libs []string
Colin Cross79c7c262019-04-17 11:11:46 -0700840}
841
Paul Duffin56d44902020-01-31 13:36:25 +0000842type sdkLibraryImportProperties struct {
Paul Duffinfcfd7912020-01-31 17:54:30 +0000843 // List of shared java libs, common to all scopes, that this module has
844 // dependencies to
845 Libs []string
846
Paul Duffin56d44902020-01-31 13:36:25 +0000847 // Properties associated with the public api scope.
848 Public sdkLibraryScopeProperties
849
850 // Properties associated with the system api scope.
851 System sdkLibraryScopeProperties
852
853 // Properties associated with the test api scope.
854 Test sdkLibraryScopeProperties
855}
856
Colin Cross79c7c262019-04-17 11:11:46 -0700857type sdkLibraryImport struct {
858 android.ModuleBase
859 android.DefaultableModuleBase
860 prebuilt android.Prebuilt
861
862 properties sdkLibraryImportProperties
863
Paul Duffin56d44902020-01-31 13:36:25 +0000864 commonToSdkLibraryAndImport
Colin Cross79c7c262019-04-17 11:11:46 -0700865}
866
867var _ SdkLibraryDependency = (*sdkLibraryImport)(nil)
868
Jaewoong Jung4f158ee2019-07-11 10:05:35 -0700869// java_sdk_library_import imports a prebuilt java_sdk_library.
Colin Cross79c7c262019-04-17 11:11:46 -0700870func sdkLibraryImportFactory() android.Module {
871 module := &sdkLibraryImport{}
872
Paul Duffinfcfd7912020-01-31 17:54:30 +0000873 module.AddProperties(&module.properties)
Colin Cross79c7c262019-04-17 11:11:46 -0700874
Paul Duffinfcfd7912020-01-31 17:54:30 +0000875 android.InitPrebuiltModule(module, &[]string{})
Colin Cross79c7c262019-04-17 11:11:46 -0700876 InitJavaModule(module, android.HostAndDeviceSupported)
877
878 android.AddLoadHook(module, func(mctx android.LoadHookContext) { module.createInternalModules(mctx) })
879 return module
880}
881
882func (module *sdkLibraryImport) Prebuilt() *android.Prebuilt {
883 return &module.prebuilt
884}
885
886func (module *sdkLibraryImport) Name() string {
887 return module.prebuilt.Name(module.ModuleBase.Name())
888}
889
890func (module *sdkLibraryImport) createInternalModules(mctx android.LoadHookContext) {
Colin Cross79c7c262019-04-17 11:11:46 -0700891
Paul Duffin50061512020-01-21 16:31:05 +0000892 // If the build is configured to use prebuilts then force this to be preferred.
893 if mctx.Config().UnbundledBuildUsePrebuiltSdks() {
894 module.prebuilt.ForcePrefer()
895 }
896
Paul Duffin56d44902020-01-31 13:36:25 +0000897 for apiScope, scopeProperties := range module.scopeProperties() {
898 if len(scopeProperties.Jars) == 0 {
899 continue
900 }
901
902 // Creates a java import for the jar with ".stubs" suffix
903 props := struct {
904 Name *string
905 Soc_specific *bool
906 Device_specific *bool
907 Product_specific *bool
908 System_ext_specific *bool
909 Sdk_version *string
910 Libs []string
911 Jars []string
Paul Duffin50061512020-01-21 16:31:05 +0000912 Prefer *bool
Paul Duffin56d44902020-01-31 13:36:25 +0000913 }{}
914
915 props.Name = proptools.StringPtr(apiScope.stubsModuleName(module.BaseModuleName()))
916 props.Sdk_version = scopeProperties.Sdk_version
Paul Duffinfcfd7912020-01-31 17:54:30 +0000917 // Prepend any of the libs from the legacy public properties to the libs for each of the
918 // scopes to avoid having to duplicate them in each scope.
919 props.Libs = append(module.properties.Libs, scopeProperties.Libs...)
Paul Duffin56d44902020-01-31 13:36:25 +0000920 props.Jars = scopeProperties.Jars
921
922 if module.SocSpecific() {
923 props.Soc_specific = proptools.BoolPtr(true)
924 } else if module.DeviceSpecific() {
925 props.Device_specific = proptools.BoolPtr(true)
926 } else if module.ProductSpecific() {
927 props.Product_specific = proptools.BoolPtr(true)
928 } else if module.SystemExtSpecific() {
929 props.System_ext_specific = proptools.BoolPtr(true)
930 }
931
Paul Duffin50061512020-01-21 16:31:05 +0000932 // If the build should use prebuilt sdks then set prefer to true on the stubs library.
933 // That will cause the prebuilt version of the stubs to override the source version.
934 if mctx.Config().UnbundledBuildUsePrebuiltSdks() {
935 props.Prefer = proptools.BoolPtr(true)
936 }
937
Paul Duffin56d44902020-01-31 13:36:25 +0000938 mctx.CreateModule(ImportFactory, &props)
939 }
Colin Cross79c7c262019-04-17 11:11:46 -0700940
941 javaSdkLibraries := javaSdkLibraries(mctx.Config())
942 javaSdkLibrariesLock.Lock()
943 defer javaSdkLibrariesLock.Unlock()
944 *javaSdkLibraries = append(*javaSdkLibraries, module.BaseModuleName())
945}
946
Paul Duffin56d44902020-01-31 13:36:25 +0000947func (module *sdkLibraryImport) scopeProperties() map[*apiScope]*sdkLibraryScopeProperties {
948 p := make(map[*apiScope]*sdkLibraryScopeProperties)
949 p[apiScopePublic] = &module.properties.Public
950 p[apiScopeSystem] = &module.properties.System
951 p[apiScopeTest] = &module.properties.Test
952 return p
953}
954
Colin Cross79c7c262019-04-17 11:11:46 -0700955func (module *sdkLibraryImport) DepsMutator(ctx android.BottomUpMutatorContext) {
Paul Duffin56d44902020-01-31 13:36:25 +0000956 for apiScope, scopeProperties := range module.scopeProperties() {
957 if len(scopeProperties.Jars) == 0 {
958 continue
959 }
960
961 // Add dependencies to the prebuilt stubs library
962 ctx.AddVariationDependencies(nil, apiScope.stubsTag, apiScope.stubsModuleName(module.BaseModuleName()))
963 }
Colin Cross79c7c262019-04-17 11:11:46 -0700964}
965
966func (module *sdkLibraryImport) GenerateAndroidBuildActions(ctx android.ModuleContext) {
967 // Record the paths to the prebuilt stubs library.
968 ctx.VisitDirectDeps(func(to android.Module) {
969 tag := ctx.OtherModuleDependencyTag(to)
970
Paul Duffin56d44902020-01-31 13:36:25 +0000971 if lib, ok := to.(Dependency); ok {
972 if scopeTag, ok := tag.(scopeDependencyTag); ok {
973 apiScope := scopeTag.apiScope
974 scopePaths := module.getScopePaths(apiScope)
975 scopePaths.stubsHeaderPath = lib.HeaderJars()
976 }
Colin Cross79c7c262019-04-17 11:11:46 -0700977 }
978 })
979}
980
Paul Duffin56d44902020-01-31 13:36:25 +0000981func (module *sdkLibraryImport) sdkJars(
982 ctx android.BaseModuleContext,
983 sdkVersion sdkSpec) android.Paths {
984
Paul Duffin50061512020-01-21 16:31:05 +0000985 // If a specific numeric version has been requested then use prebuilt versions of the sdk.
986 if sdkVersion.version.isNumbered() {
987 return PrebuiltJars(ctx, module.BaseModuleName(), sdkVersion)
988 }
989
Paul Duffin56d44902020-01-31 13:36:25 +0000990 var apiScope *apiScope
991 switch sdkVersion.kind {
992 case sdkSystem:
993 apiScope = apiScopeSystem
994 case sdkTest:
995 apiScope = apiScopeTest
996 default:
997 apiScope = apiScopePublic
998 }
999
1000 paths := module.getScopePaths(apiScope)
1001 return paths.stubsHeaderPath
1002}
1003
Colin Cross79c7c262019-04-17 11:11:46 -07001004// to satisfy SdkLibraryDependency interface
Jiyong Park6a927c42020-01-21 02:03:43 +09001005func (module *sdkLibraryImport) SdkHeaderJars(ctx android.BaseModuleContext, sdkVersion sdkSpec) android.Paths {
Colin Cross79c7c262019-04-17 11:11:46 -07001006 // This module is just a wrapper for the prebuilt stubs.
Paul Duffin56d44902020-01-31 13:36:25 +00001007 return module.sdkJars(ctx, sdkVersion)
Colin Cross79c7c262019-04-17 11:11:46 -07001008}
1009
1010// to satisfy SdkLibraryDependency interface
Jiyong Park6a927c42020-01-21 02:03:43 +09001011func (module *sdkLibraryImport) SdkImplementationJars(ctx android.BaseModuleContext, sdkVersion sdkSpec) android.Paths {
Colin Cross79c7c262019-04-17 11:11:46 -07001012 // This module is just a wrapper for the stubs.
Paul Duffin56d44902020-01-31 13:36:25 +00001013 return module.sdkJars(ctx, sdkVersion)
Colin Cross79c7c262019-04-17 11:11:46 -07001014}