blob: 2cb8f728d6a6b95f5177f38fff6e61572903b1b5 [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"
Jiyong Parkc678ad32018-04-10 13:07:10 +090019 "fmt"
Jiyong Park82484c02018-04-23 21:41:26 +090020 "io"
Jiyong Parkc678ad32018-04-10 13:07:10 +090021 "path"
Sundong Ahn054b19a2018-10-19 13:46:09 +090022 "path/filepath"
Jiyong Park82484c02018-04-23 21:41:26 +090023 "sort"
Jiyong Parkc678ad32018-04-10 13:07:10 +090024 "strings"
Jiyong Park82484c02018-04-23 21:41:26 +090025 "sync"
Jiyong Parkc678ad32018-04-10 13:07:10 +090026
Jiyong Parkc678ad32018-04-10 13:07:10 +090027 "github.com/google/blueprint/proptools"
28)
29
Jooyung Han58f26ab2019-12-18 15:34:32 +090030const (
Jiyong Parkc678ad32018-04-10 13:07:10 +090031 sdkStubsLibrarySuffix = ".stubs"
32 sdkSystemApiSuffix = ".system"
Jiyong Parkdf130542018-04-27 16:29:21 +090033 sdkTestApiSuffix = ".test"
Jiyong Parkc678ad32018-04-10 13:07:10 +090034 sdkDocsSuffix = ".docs"
Jiyong Parkc678ad32018-04-10 13:07:10 +090035 sdkXmlFileSuffix = ".xml"
Jooyung Han624058e2019-12-24 18:38:06 +090036 permissionsTemplate = `<?xml version="1.0" encoding="utf-8"?>\n` +
37 `<!-- Copyright (C) 2018 The Android Open Source Project\n` +
38 `\n` +
39 ` Licensed under the Apache License, Version 2.0 (the "License");\n` +
40 ` you may not use this file except in compliance with the License.\n` +
41 ` You may obtain a copy of the License at\n` +
42 `\n` +
43 ` http://www.apache.org/licenses/LICENSE-2.0\n` +
44 `\n` +
45 ` Unless required by applicable law or agreed to in writing, software\n` +
46 ` distributed under the License is distributed on an "AS IS" BASIS,\n` +
47 ` WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\n` +
48 ` See the License for the specific language governing permissions and\n` +
49 ` limitations under the License.\n` +
50 `-->\n` +
51 `<permissions>\n` +
52 ` <library name="%s" file="%s"/>\n` +
53 `</permissions>\n`
Jiyong Parkc678ad32018-04-10 13:07:10 +090054)
55
Jiyong Parkc678ad32018-04-10 13:07:10 +090056var (
57 publicApiStubsTag = dependencyTag{name: "public"}
58 systemApiStubsTag = dependencyTag{name: "system"}
Jiyong Parkdf130542018-04-27 16:29:21 +090059 testApiStubsTag = dependencyTag{name: "test"}
Sundong Ahn20e998b2018-07-24 11:19:26 +090060 publicApiFileTag = dependencyTag{name: "publicApi"}
61 systemApiFileTag = dependencyTag{name: "systemApi"}
62 testApiFileTag = dependencyTag{name: "testApi"}
Jiyong Parkdf130542018-04-27 16:29:21 +090063)
64
65type apiScope int
66
67const (
68 apiScopePublic apiScope = iota
69 apiScopeSystem
70 apiScopeTest
Jiyong Parkc678ad32018-04-10 13:07:10 +090071)
72
Jiyong Park82484c02018-04-23 21:41:26 +090073var (
74 javaSdkLibrariesLock sync.Mutex
75)
76
Jiyong Parkc678ad32018-04-10 13:07:10 +090077// TODO: these are big features that are currently missing
Jiyong Park1be96912018-05-28 18:02:19 +090078// 1) disallowing linking to the runtime shared lib
79// 2) HTML generation
Jiyong Parkc678ad32018-04-10 13:07:10 +090080
81func init() {
Paul Duffin43dc1cc2019-12-19 11:18:54 +000082 RegisterSdkLibraryBuildComponents(android.InitRegistrationContext)
Jiyong Parkc678ad32018-04-10 13:07:10 +090083
Jiyong Park82484c02018-04-23 21:41:26 +090084 android.RegisterMakeVarsProvider(pctx, func(ctx android.MakeVarsContext) {
85 javaSdkLibraries := javaSdkLibraries(ctx.Config())
86 sort.Strings(*javaSdkLibraries)
87 ctx.Strict("JAVA_SDK_LIBRARIES", strings.Join(*javaSdkLibraries, " "))
88 })
Jiyong Parkc678ad32018-04-10 13:07:10 +090089}
90
Paul Duffin43dc1cc2019-12-19 11:18:54 +000091func RegisterSdkLibraryBuildComponents(ctx android.RegistrationContext) {
92 ctx.RegisterModuleType("java_sdk_library", SdkLibraryFactory)
93 ctx.RegisterModuleType("java_sdk_library_import", sdkLibraryImportFactory)
94}
95
Jiyong Parkc678ad32018-04-10 13:07:10 +090096type sdkLibraryProperties struct {
Sundong Ahnf043cf62018-06-25 16:04:37 +090097 // List of Java libraries that will be in the classpath when building stubs
98 Stub_only_libs []string `android:"arch_variant"`
99
Jiyong Parkc678ad32018-04-10 13:07:10 +0900100 // list of package names that will be documented and publicized as API
101 Api_packages []string
102
Jiyong Park5a2c9d72018-05-01 22:25:41 +0900103 // list of package names that must be hidden from the API
104 Hidden_api_packages []string
105
Paul Duffin11512472019-02-11 15:55:17 +0000106 // local files that are used within user customized droiddoc options.
107 Droiddoc_option_files []string
108
109 // additional droiddoc options
110 // Available variables for substitution:
111 //
112 // $(location <label>): the path to the droiddoc_option_files with name <label>
Sundong Ahndd567f92018-07-31 17:19:11 +0900113 Droiddoc_options []string
114
Sundong Ahn054b19a2018-10-19 13:46:09 +0900115 // a list of top-level directories containing files to merge qualifier annotations
116 // (i.e. those intended to be included in the stubs written) from.
117 Merge_annotations_dirs []string
118
119 // a list of top-level directories containing Java stub files to merge show/hide annotations from.
120 Merge_inclusion_annotations_dirs []string
121
122 // If set to true, the path of dist files is apistubs/core. Defaults to false.
123 Core_lib *bool
124
Sundong Ahn80a87b32019-05-13 15:02:50 +0900125 // don't create dist rules.
126 No_dist *bool `blueprint:"mutated"`
127
Paul Duffin37e0b772019-12-30 17:20:10 +0000128 // indicates whether system and test apis should be managed.
129 Has_system_and_test_apis bool `blueprint:"mutated"`
130
Jiyong Parkc678ad32018-04-10 13:07:10 +0900131 // TODO: determines whether to create HTML doc or not
132 //Html_doc *bool
133}
134
Inseob Kimc0907f12019-02-08 21:00:45 +0900135type SdkLibrary struct {
Sundong Ahn054b19a2018-10-19 13:46:09 +0900136 Library
Jiyong Parkc678ad32018-04-10 13:07:10 +0900137
Sundong Ahn054b19a2018-10-19 13:46:09 +0900138 sdkLibraryProperties sdkLibraryProperties
Jiyong Parkc678ad32018-04-10 13:07:10 +0900139
140 publicApiStubsPath android.Paths
141 systemApiStubsPath android.Paths
Jiyong Parkdf130542018-04-27 16:29:21 +0900142 testApiStubsPath android.Paths
Sundong Ahn241cd372018-07-13 16:16:44 +0900143
144 publicApiStubsImplPath android.Paths
145 systemApiStubsImplPath android.Paths
146 testApiStubsImplPath android.Paths
Sundong Ahn20e998b2018-07-24 11:19:26 +0900147
148 publicApiFilePath android.Path
149 systemApiFilePath android.Path
150 testApiFilePath android.Path
Jooyung Han58f26ab2019-12-18 15:34:32 +0900151
Jooyung Han624058e2019-12-24 18:38:06 +0900152 permissionsFile android.Path
Jiyong Parkc678ad32018-04-10 13:07:10 +0900153}
154
Inseob Kimc0907f12019-02-08 21:00:45 +0900155var _ Dependency = (*SdkLibrary)(nil)
156var _ SdkLibraryDependency = (*SdkLibrary)(nil)
Colin Cross897d2ed2019-02-11 14:03:51 -0800157
Inseob Kimc0907f12019-02-08 21:00:45 +0900158func (module *SdkLibrary) DepsMutator(ctx android.BottomUpMutatorContext) {
Jiyong Parke3ef3c82019-07-15 15:31:16 +0900159 useBuiltStubs := !ctx.Config().UnbundledBuildUsePrebuiltSdks()
Jiyong Parkc678ad32018-04-10 13:07:10 +0900160 // Add dependencies to the stubs library
Jiyong Parke3ef3c82019-07-15 15:31:16 +0900161 if useBuiltStubs {
162 ctx.AddVariationDependencies(nil, publicApiStubsTag, module.stubsName(apiScopePublic))
163 }
Colin Cross42d48b72018-08-29 14:10:52 -0700164 ctx.AddVariationDependencies(nil, publicApiFileTag, module.docsName(apiScopePublic))
Sundong Ahn054b19a2018-10-19 13:46:09 +0900165
Paul Duffin37e0b772019-12-30 17:20:10 +0000166 if module.sdkLibraryProperties.Has_system_and_test_apis {
Jiyong Parke3ef3c82019-07-15 15:31:16 +0900167 if useBuiltStubs {
168 ctx.AddVariationDependencies(nil, systemApiStubsTag, module.stubsName(apiScopeSystem))
169 ctx.AddVariationDependencies(nil, testApiStubsTag, module.stubsName(apiScopeTest))
170 }
Sundong Ahn054b19a2018-10-19 13:46:09 +0900171 ctx.AddVariationDependencies(nil, systemApiFileTag, module.docsName(apiScopeSystem))
172 ctx.AddVariationDependencies(nil, testApiFileTag, module.docsName(apiScopeTest))
Sundong Ahn054b19a2018-10-19 13:46:09 +0900173 }
174
175 module.Library.deps(ctx)
Jiyong Parkc678ad32018-04-10 13:07:10 +0900176}
177
Inseob Kimc0907f12019-02-08 21:00:45 +0900178func (module *SdkLibrary) GenerateAndroidBuildActions(ctx android.ModuleContext) {
Sundong Ahn054b19a2018-10-19 13:46:09 +0900179 module.Library.GenerateAndroidBuildActions(ctx)
180
Jooyung Han624058e2019-12-24 18:38:06 +0900181 module.buildPermissionsFile(ctx)
Jooyung Han58f26ab2019-12-18 15:34:32 +0900182
Sundong Ahn57368eb2018-07-06 11:20:23 +0900183 // Record the paths to the header jars of the library (stubs and impl).
Jiyong Parkc678ad32018-04-10 13:07:10 +0900184 // When this java_sdk_library is dependened from others via "libs" property,
185 // the recorded paths will be returned depending on the link type of the caller.
186 ctx.VisitDirectDeps(func(to android.Module) {
187 otherName := ctx.OtherModuleName(to)
188 tag := ctx.OtherModuleDependencyTag(to)
189
Sundong Ahn57368eb2018-07-06 11:20:23 +0900190 if lib, ok := to.(Dependency); ok {
Jiyong Parkc678ad32018-04-10 13:07:10 +0900191 switch tag {
192 case publicApiStubsTag:
Sundong Ahn57368eb2018-07-06 11:20:23 +0900193 module.publicApiStubsPath = lib.HeaderJars()
Sundong Ahn241cd372018-07-13 16:16:44 +0900194 module.publicApiStubsImplPath = lib.ImplementationJars()
Jiyong Parkc678ad32018-04-10 13:07:10 +0900195 case systemApiStubsTag:
Sundong Ahn57368eb2018-07-06 11:20:23 +0900196 module.systemApiStubsPath = lib.HeaderJars()
Sundong Ahn241cd372018-07-13 16:16:44 +0900197 module.systemApiStubsImplPath = lib.ImplementationJars()
Jiyong Parkdf130542018-04-27 16:29:21 +0900198 case testApiStubsTag:
Sundong Ahn57368eb2018-07-06 11:20:23 +0900199 module.testApiStubsPath = lib.HeaderJars()
Sundong Ahn241cd372018-07-13 16:16:44 +0900200 module.testApiStubsImplPath = lib.ImplementationJars()
Jiyong Parkc678ad32018-04-10 13:07:10 +0900201 }
202 }
Sundong Ahn20e998b2018-07-24 11:19:26 +0900203 if doc, ok := to.(ApiFilePath); ok {
204 switch tag {
205 case publicApiFileTag:
206 module.publicApiFilePath = doc.ApiFilePath()
207 case systemApiFileTag:
208 module.systemApiFilePath = doc.ApiFilePath()
209 case testApiFileTag:
210 module.testApiFilePath = doc.ApiFilePath()
211 default:
212 ctx.ModuleErrorf("depends on module %q of unknown tag %q", otherName, tag)
213 }
214 }
Jiyong Parkc678ad32018-04-10 13:07:10 +0900215 })
216}
217
Jooyung Han624058e2019-12-24 18:38:06 +0900218func (module *SdkLibrary) buildPermissionsFile(ctx android.ModuleContext) {
219 xmlContent := fmt.Sprintf(permissionsTemplate, module.BaseModuleName(), module.implPath())
220 permissionsFile := android.PathForModuleOut(ctx, module.xmlFileName())
Jooyung Han58f26ab2019-12-18 15:34:32 +0900221
Jooyung Han624058e2019-12-24 18:38:06 +0900222 ctx.Build(pctx, android.BuildParams{
223 Rule: android.WriteFile,
224 Output: permissionsFile,
225 Description: "Generating " + module.BaseModuleName() + " permissions",
226 Args: map[string]string{
227 "content": xmlContent,
228 },
229 })
Jooyung Han58f26ab2019-12-18 15:34:32 +0900230
Jooyung Han624058e2019-12-24 18:38:06 +0900231 module.permissionsFile = permissionsFile
Jooyung Han58f26ab2019-12-18 15:34:32 +0900232}
233
Jooyung Han624058e2019-12-24 18:38:06 +0900234func (module *SdkLibrary) OutputFiles(tag string) (android.Paths, error) {
235 switch tag {
236 case ".xml":
237 return android.Paths{module.permissionsFile}, nil
238 }
239 return module.Library.OutputFiles(tag)
Jooyung Han58f26ab2019-12-18 15:34:32 +0900240}
241
Jiyong Park0b0e1b92019-12-03 13:24:29 +0900242func (module *SdkLibrary) AndroidMkEntries() []android.AndroidMkEntries {
243 entriesList := module.Library.AndroidMkEntries()
244 entries := &entriesList[0]
Jaewoong Jungb0c127c2019-08-29 14:56:03 -0700245 entries.Required = append(entries.Required, module.xmlFileName())
Sundong Ahn054b19a2018-10-19 13:46:09 +0900246
Jaewoong Jungb0c127c2019-08-29 14:56:03 -0700247 entries.ExtraFooters = []android.AndroidMkExtraFootersFunc{
248 func(w io.Writer, name, prefix, moduleDir string, entries *android.AndroidMkEntries) {
Jaewoong Jungb0c127c2019-08-29 14:56:03 -0700249 if !Bool(module.sdkLibraryProperties.No_dist) {
250 // Create a phony module that installs the impl library, for the case when this lib is
251 // in PRODUCT_PACKAGES.
252 owner := module.ModuleBase.Owner()
253 if owner == "" {
254 if Bool(module.sdkLibraryProperties.Core_lib) {
255 owner = "core"
256 } else {
257 owner = "android"
258 }
259 }
260 // Create dist rules to install the stubs libs to the dist dir
261 if len(module.publicApiStubsPath) == 1 {
262 fmt.Fprintln(w, "$(call dist-for-goals,sdk win_sdk,"+
263 module.publicApiStubsImplPath.Strings()[0]+
264 ":"+path.Join("apistubs", owner, "public",
265 module.BaseModuleName()+".jar")+")")
266 }
267 if len(module.systemApiStubsPath) == 1 {
268 fmt.Fprintln(w, "$(call dist-for-goals,sdk win_sdk,"+
269 module.systemApiStubsImplPath.Strings()[0]+
270 ":"+path.Join("apistubs", owner, "system",
271 module.BaseModuleName()+".jar")+")")
272 }
273 if len(module.testApiStubsPath) == 1 {
274 fmt.Fprintln(w, "$(call dist-for-goals,sdk win_sdk,"+
275 module.testApiStubsImplPath.Strings()[0]+
276 ":"+path.Join("apistubs", owner, "test",
277 module.BaseModuleName()+".jar")+")")
278 }
279 if module.publicApiFilePath != nil {
280 fmt.Fprintln(w, "$(call dist-for-goals,sdk win_sdk,"+
281 module.publicApiFilePath.String()+
282 ":"+path.Join("apistubs", owner, "public", "api",
283 module.BaseModuleName()+".txt")+")")
284 }
285 if module.systemApiFilePath != nil {
286 fmt.Fprintln(w, "$(call dist-for-goals,sdk win_sdk,"+
287 module.systemApiFilePath.String()+
288 ":"+path.Join("apistubs", owner, "system", "api",
289 module.BaseModuleName()+".txt")+")")
290 }
291 if module.testApiFilePath != nil {
292 fmt.Fprintln(w, "$(call dist-for-goals,sdk win_sdk,"+
293 module.testApiFilePath.String()+
294 ":"+path.Join("apistubs", owner, "test", "api",
295 module.BaseModuleName()+".txt")+")")
Sundong Ahn80a87b32019-05-13 15:02:50 +0900296 }
Sundong Ahn4fd04bb2018-08-31 18:01:37 +0900297 }
Jaewoong Jungb0c127c2019-08-29 14:56:03 -0700298 },
Jiyong Park82484c02018-04-23 21:41:26 +0900299 }
Jiyong Park0b0e1b92019-12-03 13:24:29 +0900300 return entriesList
Jiyong Park82484c02018-04-23 21:41:26 +0900301}
302
Jiyong Parkc678ad32018-04-10 13:07:10 +0900303// Module name of the stubs library
Inseob Kimc0907f12019-02-08 21:00:45 +0900304func (module *SdkLibrary) stubsName(apiScope apiScope) string {
Jiyong Parkc678ad32018-04-10 13:07:10 +0900305 stubsName := module.BaseModuleName() + sdkStubsLibrarySuffix
Jiyong Parkdf130542018-04-27 16:29:21 +0900306 switch apiScope {
307 case apiScopeSystem:
Jiyong Parkc678ad32018-04-10 13:07:10 +0900308 stubsName = stubsName + sdkSystemApiSuffix
Jiyong Parkdf130542018-04-27 16:29:21 +0900309 case apiScopeTest:
310 stubsName = stubsName + sdkTestApiSuffix
Jiyong Parkc678ad32018-04-10 13:07:10 +0900311 }
312 return stubsName
313}
314
315// Module name of the docs
Inseob Kimc0907f12019-02-08 21:00:45 +0900316func (module *SdkLibrary) docsName(apiScope apiScope) string {
Jiyong Parkc678ad32018-04-10 13:07:10 +0900317 docsName := module.BaseModuleName() + sdkDocsSuffix
Jiyong Parkdf130542018-04-27 16:29:21 +0900318 switch apiScope {
319 case apiScopeSystem:
Jiyong Parkc678ad32018-04-10 13:07:10 +0900320 docsName = docsName + sdkSystemApiSuffix
Jiyong Parkdf130542018-04-27 16:29:21 +0900321 case apiScopeTest:
322 docsName = docsName + sdkTestApiSuffix
Jiyong Parkc678ad32018-04-10 13:07:10 +0900323 }
324 return docsName
325}
326
327// Module name of the runtime implementation library
Inseob Kimc0907f12019-02-08 21:00:45 +0900328func (module *SdkLibrary) implName() string {
Sundong Ahn054b19a2018-10-19 13:46:09 +0900329 return module.BaseModuleName()
Jiyong Parkc678ad32018-04-10 13:07:10 +0900330}
331
332// File path to the runtime implementation library
Inseob Kimc0907f12019-02-08 21:00:45 +0900333func (module *SdkLibrary) implPath() string {
Jooyung Han58f26ab2019-12-18 15:34:32 +0900334 if apexName := module.ApexName(); apexName != "" {
335 // TODO(b/146468504): ApexName() is only a soong module name, not apex name.
336 // In most cases, this works fine. But when apex_name is set or override_apex is used
337 // this can be wrong.
338 return fmt.Sprintf("/apex/%s/javalib/%s.jar", apexName, module.implName())
339 }
Jiyong Parkc678ad32018-04-10 13:07:10 +0900340 partition := "system"
341 if module.SocSpecific() {
342 partition = "vendor"
343 } else if module.DeviceSpecific() {
344 partition = "odm"
345 } else if module.ProductSpecific() {
346 partition = "product"
Sundong Ahn0d7dff42019-12-04 12:53:44 +0900347 } else if module.SystemExtSpecific() {
348 partition = "system_ext"
Jiyong Parkc678ad32018-04-10 13:07:10 +0900349 }
350 return "/" + partition + "/framework/" + module.implName() + ".jar"
351}
352
353// Module name of the XML file for the lib
Inseob Kimc0907f12019-02-08 21:00:45 +0900354func (module *SdkLibrary) xmlFileName() string {
Jiyong Parkc678ad32018-04-10 13:07:10 +0900355 return module.BaseModuleName() + sdkXmlFileSuffix
356}
357
358// SDK version that the stubs library is built against. Note that this is always
359// *current. Older stubs library built with a numberd SDK version is created from
360// the prebuilt jar.
Inseob Kimc0907f12019-02-08 21:00:45 +0900361func (module *SdkLibrary) sdkVersion(apiScope apiScope) string {
Jiyong Parkdf130542018-04-27 16:29:21 +0900362 switch apiScope {
363 case apiScopePublic:
364 return "current"
365 case apiScopeSystem:
Jiyong Parkc678ad32018-04-10 13:07:10 +0900366 return "system_current"
Jiyong Parkdf130542018-04-27 16:29:21 +0900367 case apiScopeTest:
368 return "test_current"
369 default:
Jiyong Parkc678ad32018-04-10 13:07:10 +0900370 return "current"
371 }
372}
373
374// $(INTERNAL_PLATFORM_<apiTagName>_API_FILE) points to the generated
375// api file for the current source
376// TODO: remove this when apicheck is done in soong
Inseob Kimc0907f12019-02-08 21:00:45 +0900377func (module *SdkLibrary) apiTagName(apiScope apiScope) string {
Jiyong Parkc678ad32018-04-10 13:07:10 +0900378 apiTagName := strings.Replace(strings.ToUpper(module.BaseModuleName()), ".", "_", -1)
Jiyong Parkdf130542018-04-27 16:29:21 +0900379 switch apiScope {
380 case apiScopeSystem:
Jiyong Parkc678ad32018-04-10 13:07:10 +0900381 apiTagName = apiTagName + "_SYSTEM"
Jiyong Parkdf130542018-04-27 16:29:21 +0900382 case apiScopeTest:
383 apiTagName = apiTagName + "_TEST"
Jiyong Parkc678ad32018-04-10 13:07:10 +0900384 }
385 return apiTagName
386}
387
Inseob Kimc0907f12019-02-08 21:00:45 +0900388func (module *SdkLibrary) latestApiFilegroupName(apiScope apiScope) string {
Jiyong Park58c518b2018-05-12 22:29:12 +0900389 name := ":" + module.BaseModuleName() + ".api."
Jiyong Parkdf130542018-04-27 16:29:21 +0900390 switch apiScope {
Jiyong Park58c518b2018-05-12 22:29:12 +0900391 case apiScopePublic:
392 name = name + "public"
Jiyong Parkdf130542018-04-27 16:29:21 +0900393 case apiScopeSystem:
Jiyong Park58c518b2018-05-12 22:29:12 +0900394 name = name + "system"
Jiyong Parkdf130542018-04-27 16:29:21 +0900395 case apiScopeTest:
Jiyong Park58c518b2018-05-12 22:29:12 +0900396 name = name + "test"
Jiyong Parkc678ad32018-04-10 13:07:10 +0900397 }
Jiyong Park58c518b2018-05-12 22:29:12 +0900398 name = name + ".latest"
399 return name
400}
Jiyong Parkc678ad32018-04-10 13:07:10 +0900401
Inseob Kimc0907f12019-02-08 21:00:45 +0900402func (module *SdkLibrary) latestRemovedApiFilegroupName(apiScope apiScope) string {
Jiyong Park58c518b2018-05-12 22:29:12 +0900403 name := ":" + module.BaseModuleName() + "-removed.api."
404 switch apiScope {
405 case apiScopePublic:
406 name = name + "public"
407 case apiScopeSystem:
408 name = name + "system"
409 case apiScopeTest:
410 name = name + "test"
411 }
412 name = name + ".latest"
413 return name
Jiyong Parkc678ad32018-04-10 13:07:10 +0900414}
415
416// Creates a static java library that has API stubs
Colin Crossf8b860a2019-04-16 14:43:28 -0700417func (module *SdkLibrary) createStubsLibrary(mctx android.LoadHookContext, apiScope apiScope) {
Jiyong Parkc678ad32018-04-10 13:07:10 +0900418 props := struct {
Sundong Ahn0d7dff42019-12-04 12:53:44 +0900419 Name *string
420 Srcs []string
421 Sdk_version *string
422 Libs []string
423 Soc_specific *bool
424 Device_specific *bool
425 Product_specific *bool
426 System_ext_specific *bool
427 Compile_dex *bool
428 System_modules *string
429 Java_version *string
430 Product_variables struct {
Jiyong Parkc678ad32018-04-10 13:07:10 +0900431 Unbundled_build struct {
432 Enabled *bool
433 }
Jiyong Park82484c02018-04-23 21:41:26 +0900434 Pdk struct {
435 Enabled *bool
436 }
Jiyong Parkc678ad32018-04-10 13:07:10 +0900437 }
Sundong Ahn054b19a2018-10-19 13:46:09 +0900438 Openjdk9 struct {
439 Srcs []string
440 Javacflags []string
441 }
Jiyong Parkc678ad32018-04-10 13:07:10 +0900442 }{}
443
Paul Duffin52d398a2019-06-11 12:31:14 +0100444 sdkVersion := module.sdkVersion(apiScope)
Paul Duffin250e6192019-06-07 10:44:37 +0100445 sdkDep := decodeSdkDep(mctx, sdkContext(&module.Library))
Paul Duffin52d398a2019-06-11 12:31:14 +0100446 if !sdkDep.hasStandardLibs() {
447 sdkVersion = "none"
448 }
Paul Duffin250e6192019-06-07 10:44:37 +0100449
Jiyong Parkdf130542018-04-27 16:29:21 +0900450 props.Name = proptools.StringPtr(module.stubsName(apiScope))
Jiyong Parkc678ad32018-04-10 13:07:10 +0900451 // sources are generated from the droiddoc
Jiyong Parkdf130542018-04-27 16:29:21 +0900452 props.Srcs = []string{":" + module.docsName(apiScope)}
Paul Duffin52d398a2019-06-11 12:31:14 +0100453 props.Sdk_version = proptools.StringPtr(sdkVersion)
Sundong Ahn054b19a2018-10-19 13:46:09 +0900454 props.Libs = module.sdkLibraryProperties.Stub_only_libs
Jiyong Parkc678ad32018-04-10 13:07:10 +0900455 // Unbundled apps will use the prebult one from /prebuilts/sdk
Colin Cross10932872019-04-18 14:27:12 -0700456 if mctx.Config().UnbundledBuildUsePrebuiltSdks() {
Colin Cross2c77ceb2019-01-21 11:56:21 -0800457 props.Product_variables.Unbundled_build.Enabled = proptools.BoolPtr(false)
458 }
Jiyong Park82484c02018-04-23 21:41:26 +0900459 props.Product_variables.Pdk.Enabled = proptools.BoolPtr(false)
Sundong Ahn054b19a2018-10-19 13:46:09 +0900460 props.System_modules = module.Library.Module.deviceProperties.System_modules
461 props.Openjdk9.Srcs = module.Library.Module.properties.Openjdk9.Srcs
462 props.Openjdk9.Javacflags = module.Library.Module.properties.Openjdk9.Javacflags
463 props.Java_version = module.Library.Module.properties.Java_version
464 if module.Library.Module.deviceProperties.Compile_dex != nil {
465 props.Compile_dex = module.Library.Module.deviceProperties.Compile_dex
Sundong Ahndd567f92018-07-31 17:19:11 +0900466 }
Jiyong Parkc678ad32018-04-10 13:07:10 +0900467
468 if module.SocSpecific() {
469 props.Soc_specific = proptools.BoolPtr(true)
470 } else if module.DeviceSpecific() {
471 props.Device_specific = proptools.BoolPtr(true)
472 } else if module.ProductSpecific() {
473 props.Product_specific = proptools.BoolPtr(true)
Sundong Ahn0d7dff42019-12-04 12:53:44 +0900474 } else if module.SystemExtSpecific() {
475 props.System_ext_specific = proptools.BoolPtr(true)
Jiyong Parkc678ad32018-04-10 13:07:10 +0900476 }
477
Colin Cross84dfc3d2019-09-25 11:33:01 -0700478 mctx.CreateModule(LibraryFactory, &props)
Jiyong Parkc678ad32018-04-10 13:07:10 +0900479}
480
481// Creates a droiddoc module that creates stubs source files from the given full source
482// files
Paul Duffinc4cea762019-12-30 17:32:47 +0000483func (module *SdkLibrary) createStubsSources(mctx android.LoadHookContext, apiScope apiScope) {
Jiyong Parkc678ad32018-04-10 13:07:10 +0900484 props := struct {
Sundong Ahn054b19a2018-10-19 13:46:09 +0900485 Name *string
486 Srcs []string
487 Installable *bool
Paul Duffin52d398a2019-06-11 12:31:14 +0100488 Sdk_version *string
Sundong Ahn054b19a2018-10-19 13:46:09 +0900489 Libs []string
Paul Duffin11512472019-02-11 15:55:17 +0000490 Arg_files []string
Sundong Ahn054b19a2018-10-19 13:46:09 +0900491 Args *string
492 Api_tag_name *string
493 Api_filename *string
494 Removed_api_filename *string
Sundong Ahn054b19a2018-10-19 13:46:09 +0900495 Java_version *string
496 Merge_annotations_dirs []string
497 Merge_inclusion_annotations_dirs []string
498 Check_api struct {
Inseob Kim38449af2019-02-28 14:24:05 +0900499 Current ApiToCheck
500 Last_released ApiToCheck
501 Ignore_missing_latest_api *bool
Jiyong Park58c518b2018-05-12 22:29:12 +0900502 }
Sundong Ahn1b92c822018-05-29 11:35:17 +0900503 Aidl struct {
504 Include_dirs []string
505 Local_include_dirs []string
506 }
Jiyong Parkc678ad32018-04-10 13:07:10 +0900507 }{}
508
Paul Duffin250e6192019-06-07 10:44:37 +0100509 sdkDep := decodeSdkDep(mctx, sdkContext(&module.Library))
Paul Duffin52d398a2019-06-11 12:31:14 +0100510 sdkVersion := ""
511 if !sdkDep.hasStandardLibs() {
512 sdkVersion = "none"
513 }
Paul Duffin250e6192019-06-07 10:44:37 +0100514
Jiyong Parkdf130542018-04-27 16:29:21 +0900515 props.Name = proptools.StringPtr(module.docsName(apiScope))
Sundong Ahn054b19a2018-10-19 13:46:09 +0900516 props.Srcs = append(props.Srcs, module.Library.Module.properties.Srcs...)
Paul Duffin52d398a2019-06-11 12:31:14 +0100517 props.Sdk_version = proptools.StringPtr(sdkVersion)
Jiyong Parkc678ad32018-04-10 13:07:10 +0900518 props.Installable = proptools.BoolPtr(false)
Sundong Ahne6f0b052018-06-05 16:46:14 +0900519 // A droiddoc module has only one Libs property and doesn't distinguish between
520 // shared libs and static libs. So we need to add both of these libs to Libs property.
Sundong Ahn054b19a2018-10-19 13:46:09 +0900521 props.Libs = module.Library.Module.properties.Libs
522 props.Libs = append(props.Libs, module.Library.Module.properties.Static_libs...)
523 props.Aidl.Include_dirs = module.Library.Module.deviceProperties.Aidl.Include_dirs
524 props.Aidl.Local_include_dirs = module.Library.Module.deviceProperties.Aidl.Local_include_dirs
Sundong Ahn054b19a2018-10-19 13:46:09 +0900525 props.Java_version = module.Library.Module.properties.Java_version
Jiyong Parkc678ad32018-04-10 13:07:10 +0900526
Sundong Ahn054b19a2018-10-19 13:46:09 +0900527 props.Merge_annotations_dirs = module.sdkLibraryProperties.Merge_annotations_dirs
528 props.Merge_inclusion_annotations_dirs = module.sdkLibraryProperties.Merge_inclusion_annotations_dirs
529
Paul Duffin235ffff2019-12-24 10:41:30 +0000530 droiddocArgs := []string{}
531 if len(module.sdkLibraryProperties.Api_packages) != 0 {
532 droiddocArgs = append(droiddocArgs, "--stub-packages "+strings.Join(module.sdkLibraryProperties.Api_packages, ":"))
533 }
534 if len(module.sdkLibraryProperties.Hidden_api_packages) != 0 {
535 droiddocArgs = append(droiddocArgs,
536 android.JoinWithPrefix(module.sdkLibraryProperties.Hidden_api_packages, " --hide-package "))
537 }
538 droiddocArgs = append(droiddocArgs, module.sdkLibraryProperties.Droiddoc_options...)
539 disabledWarnings := []string{
540 "MissingPermission",
541 "BroadcastBehavior",
542 "HiddenSuperclass",
543 "DeprecationMismatch",
544 "UnavailableSymbol",
545 "SdkConstant",
546 "HiddenTypeParameter",
547 "Todo",
548 "Typo",
549 }
550 droiddocArgs = append(droiddocArgs, android.JoinWithPrefix(disabledWarnings, "--hide "))
Sundong Ahnfb2721f2018-09-17 13:23:09 +0900551
Jiyong Parkdf130542018-04-27 16:29:21 +0900552 switch apiScope {
553 case apiScopeSystem:
Paul Duffin235ffff2019-12-24 10:41:30 +0000554 droiddocArgs = append(droiddocArgs, "-showAnnotation android.annotation.SystemApi")
Jiyong Parkdf130542018-04-27 16:29:21 +0900555 case apiScopeTest:
Paul Duffin235ffff2019-12-24 10:41:30 +0000556 droiddocArgs = append(droiddocArgs, " -showAnnotation android.annotation.TestApi")
Jiyong Parkc678ad32018-04-10 13:07:10 +0900557 }
Paul Duffin11512472019-02-11 15:55:17 +0000558 props.Arg_files = module.sdkLibraryProperties.Droiddoc_option_files
Paul Duffin235ffff2019-12-24 10:41:30 +0000559 props.Args = proptools.StringPtr(strings.Join(droiddocArgs, " "))
Jiyong Parkc678ad32018-04-10 13:07:10 +0900560
561 // List of APIs identified from the provided source files are created. They are later
562 // compared against to the not-yet-released (a.k.a current) list of APIs and to the
563 // last-released (a.k.a numbered) list of API.
Jiyong Parkc678ad32018-04-10 13:07:10 +0900564 currentApiFileName := "current.txt"
565 removedApiFileName := "removed.txt"
Jiyong Parkdf130542018-04-27 16:29:21 +0900566 switch apiScope {
567 case apiScopeSystem:
Jiyong Parkc678ad32018-04-10 13:07:10 +0900568 currentApiFileName = "system-" + currentApiFileName
569 removedApiFileName = "system-" + removedApiFileName
Jiyong Parkdf130542018-04-27 16:29:21 +0900570 case apiScopeTest:
571 currentApiFileName = "test-" + currentApiFileName
572 removedApiFileName = "test-" + removedApiFileName
Jiyong Parkc678ad32018-04-10 13:07:10 +0900573 }
574 currentApiFileName = path.Join("api", currentApiFileName)
575 removedApiFileName = path.Join("api", removedApiFileName)
Jiyong Park58c518b2018-05-12 22:29:12 +0900576 // TODO(jiyong): remove these three props
Jiyong Parkdf130542018-04-27 16:29:21 +0900577 props.Api_tag_name = proptools.StringPtr(module.apiTagName(apiScope))
Jiyong Parkc678ad32018-04-10 13:07:10 +0900578 props.Api_filename = proptools.StringPtr(currentApiFileName)
579 props.Removed_api_filename = proptools.StringPtr(removedApiFileName)
580
Jiyong Park58c518b2018-05-12 22:29:12 +0900581 // check against the not-yet-release API
582 props.Check_api.Current.Api_file = proptools.StringPtr(currentApiFileName)
583 props.Check_api.Current.Removed_api_file = proptools.StringPtr(removedApiFileName)
Jiyong Park58c518b2018-05-12 22:29:12 +0900584
585 // check against the latest released API
586 props.Check_api.Last_released.Api_file = proptools.StringPtr(
587 module.latestApiFilegroupName(apiScope))
588 props.Check_api.Last_released.Removed_api_file = proptools.StringPtr(
589 module.latestRemovedApiFilegroupName(apiScope))
Inseob Kim38449af2019-02-28 14:24:05 +0900590 props.Check_api.Ignore_missing_latest_api = proptools.BoolPtr(true)
Jiyong Park58c518b2018-05-12 22:29:12 +0900591
Colin Cross84dfc3d2019-09-25 11:33:01 -0700592 mctx.CreateModule(DroidstubsFactory, &props)
Jiyong Parkc678ad32018-04-10 13:07:10 +0900593}
594
Jiyong Parkc678ad32018-04-10 13:07:10 +0900595// Creates the xml file that publicizes the runtime library
Colin Crossf8b860a2019-04-16 14:43:28 -0700596func (module *SdkLibrary) createXmlFile(mctx android.LoadHookContext) {
Jiyong Parkc678ad32018-04-10 13:07:10 +0900597 // creates a prebuilt_etc module to actually place the xml file under
598 // <partition>/etc/permissions
599 etcProps := struct {
Sundong Ahn0d7dff42019-12-04 12:53:44 +0900600 Name *string
601 Src *string
602 Sub_dir *string
603 Soc_specific *bool
604 Device_specific *bool
605 Product_specific *bool
606 System_ext_specific *bool
Jiyong Parkc678ad32018-04-10 13:07:10 +0900607 }{}
608 etcProps.Name = proptools.StringPtr(module.xmlFileName())
Jooyung Han624058e2019-12-24 18:38:06 +0900609 etcProps.Src = proptools.StringPtr(":" + module.BaseModuleName() + "{.xml}")
Jiyong Parkc678ad32018-04-10 13:07:10 +0900610 etcProps.Sub_dir = proptools.StringPtr("permissions")
611 if module.SocSpecific() {
612 etcProps.Soc_specific = proptools.BoolPtr(true)
613 } else if module.DeviceSpecific() {
614 etcProps.Device_specific = proptools.BoolPtr(true)
615 } else if module.ProductSpecific() {
616 etcProps.Product_specific = proptools.BoolPtr(true)
Sundong Ahn0d7dff42019-12-04 12:53:44 +0900617 } else if module.SystemExtSpecific() {
618 etcProps.System_ext_specific = proptools.BoolPtr(true)
Jiyong Parkc678ad32018-04-10 13:07:10 +0900619 }
Colin Cross84dfc3d2019-09-25 11:33:01 -0700620 mctx.CreateModule(android.PrebuiltEtcFactory, &etcProps)
Jiyong Parkc678ad32018-04-10 13:07:10 +0900621}
622
Colin Cross0ea8ba82019-06-06 14:33:29 -0700623func (module *SdkLibrary) PrebuiltJars(ctx android.BaseModuleContext, sdkVersion string) android.Paths {
Sundong Ahn054b19a2018-10-19 13:46:09 +0900624 var api, v string
Paul Duffin52d398a2019-06-11 12:31:14 +0100625 if sdkVersion == "" || sdkVersion == "none" {
Sundong Ahn054b19a2018-10-19 13:46:09 +0900626 api = "system"
627 v = "current"
628 } else if strings.Contains(sdkVersion, "_") {
629 t := strings.Split(sdkVersion, "_")
630 api = t[0]
631 v = t[1]
Jiyong Parkc678ad32018-04-10 13:07:10 +0900632 } else {
Sundong Ahn054b19a2018-10-19 13:46:09 +0900633 api = "public"
634 v = sdkVersion
635 }
636 dir := filepath.Join("prebuilts", "sdk", v, api)
637 jar := filepath.Join(dir, module.BaseModuleName()+".jar")
638 jarPath := android.ExistentPathForSource(ctx, jar)
Sundong Ahnae418ac2019-02-28 15:01:28 +0900639 if !jarPath.Valid() {
640 ctx.PropertyErrorf("sdk_library", "invalid sdk version %q, %q does not exist", v, jar)
641 return nil
642 }
Sundong Ahn054b19a2018-10-19 13:46:09 +0900643 return android.Paths{jarPath.Path()}
644}
645
646// to satisfy SdkLibraryDependency interface
Colin Cross0ea8ba82019-06-06 14:33:29 -0700647func (module *SdkLibrary) SdkHeaderJars(ctx android.BaseModuleContext, sdkVersion string) android.Paths {
Sundong Ahn054b19a2018-10-19 13:46:09 +0900648 // This module is just a wrapper for the stubs.
Colin Cross10932872019-04-18 14:27:12 -0700649 if ctx.Config().UnbundledBuildUsePrebuiltSdks() {
Sundong Ahn054b19a2018-10-19 13:46:09 +0900650 return module.PrebuiltJars(ctx, sdkVersion)
651 } else {
652 if strings.HasPrefix(sdkVersion, "system_") {
653 return module.systemApiStubsPath
654 } else if sdkVersion == "" {
655 return module.Library.HeaderJars()
656 } else {
657 return module.publicApiStubsPath
658 }
Jiyong Parkc678ad32018-04-10 13:07:10 +0900659 }
660}
661
Sundong Ahn241cd372018-07-13 16:16:44 +0900662// to satisfy SdkLibraryDependency interface
Colin Cross0ea8ba82019-06-06 14:33:29 -0700663func (module *SdkLibrary) SdkImplementationJars(ctx android.BaseModuleContext, sdkVersion string) android.Paths {
Sundong Ahn241cd372018-07-13 16:16:44 +0900664 // This module is just a wrapper for the stubs.
Colin Cross10932872019-04-18 14:27:12 -0700665 if ctx.Config().UnbundledBuildUsePrebuiltSdks() {
Sundong Ahn054b19a2018-10-19 13:46:09 +0900666 return module.PrebuiltJars(ctx, sdkVersion)
Sundong Ahn241cd372018-07-13 16:16:44 +0900667 } else {
Sundong Ahn054b19a2018-10-19 13:46:09 +0900668 if strings.HasPrefix(sdkVersion, "system_") {
669 return module.systemApiStubsImplPath
670 } else if sdkVersion == "" {
671 return module.Library.ImplementationJars()
672 } else {
673 return module.publicApiStubsImplPath
674 }
Sundong Ahn241cd372018-07-13 16:16:44 +0900675 }
676}
677
Sundong Ahn80a87b32019-05-13 15:02:50 +0900678func (module *SdkLibrary) SetNoDist() {
679 module.sdkLibraryProperties.No_dist = proptools.BoolPtr(true)
680}
681
Colin Cross571cccf2019-02-04 11:22:08 -0800682var javaSdkLibrariesKey = android.NewOnceKey("javaSdkLibraries")
683
Jiyong Park82484c02018-04-23 21:41:26 +0900684func javaSdkLibraries(config android.Config) *[]string {
Colin Cross571cccf2019-02-04 11:22:08 -0800685 return config.Once(javaSdkLibrariesKey, func() interface{} {
Jiyong Park82484c02018-04-23 21:41:26 +0900686 return &[]string{}
687 }).(*[]string)
688}
689
Jiyong Parkc678ad32018-04-10 13:07:10 +0900690// For a java_sdk_library module, create internal modules for stubs, docs,
691// runtime libs and xml file. If requested, the stubs and docs are created twice
692// once for public API level and once for system API level
Colin Crossf8b860a2019-04-16 14:43:28 -0700693func (module *SdkLibrary) CreateInternalModules(mctx android.LoadHookContext) {
Inseob Kim6e93ac92019-03-21 17:43:49 +0900694 if len(module.Library.Module.properties.Srcs) == 0 {
Inseob Kimc0907f12019-02-08 21:00:45 +0900695 mctx.PropertyErrorf("srcs", "java_sdk_library must specify srcs")
Jooyung Han58f26ab2019-12-18 15:34:32 +0900696 return
Inseob Kimc0907f12019-02-08 21:00:45 +0900697 }
698
Inseob Kim6e93ac92019-03-21 17:43:49 +0900699 if len(module.sdkLibraryProperties.Api_packages) == 0 {
Inseob Kimc0907f12019-02-08 21:00:45 +0900700 mctx.PropertyErrorf("api_packages", "java_sdk_library must specify api_packages")
Jooyung Han58f26ab2019-12-18 15:34:32 +0900701 return
Inseob Kimc0907f12019-02-08 21:00:45 +0900702 }
Inseob Kim8098faa2019-03-18 10:19:51 +0900703
Paul Duffin37e0b772019-12-30 17:20:10 +0000704 // If this builds against standard libraries (i.e. is not part of the core libraries)
705 // then assume it provides both system and test apis. Otherwise, assume it does not and
706 // also assume it does not contribute to the dist build.
707 sdkDep := decodeSdkDep(mctx, sdkContext(&module.Library))
708 hasSystemAndTestApis := sdkDep.hasStandardLibs()
709 module.sdkLibraryProperties.Has_system_and_test_apis = hasSystemAndTestApis
710 module.sdkLibraryProperties.No_dist = proptools.BoolPtr(!hasSystemAndTestApis)
711
712 scopes := []string{""}
713 if hasSystemAndTestApis {
714 scopes = append(scopes, "system-", "test-")
715 }
716
Inseob Kim8098faa2019-03-18 10:19:51 +0900717 missing_current_api := false
718
Paul Duffin37e0b772019-12-30 17:20:10 +0000719 for _, scope := range scopes {
Inseob Kim8098faa2019-03-18 10:19:51 +0900720 for _, api := range []string{"current.txt", "removed.txt"} {
721 path := path.Join(mctx.ModuleDir(), "api", scope+api)
722 p := android.ExistentPathForSource(mctx, path)
723 if !p.Valid() {
724 mctx.ModuleErrorf("Current api file %#v doesn't exist", path)
725 missing_current_api = true
726 }
727 }
728 }
729
730 if missing_current_api {
731 script := "build/soong/scripts/gen-java-current-api-files.sh"
732 p := android.ExistentPathForSource(mctx, script)
733
734 if !p.Valid() {
735 panic(fmt.Sprintf("script file %s doesn't exist", script))
736 }
737
738 mctx.ModuleErrorf("One or more current api files are missing. "+
739 "You can update them by:\n"+
Paul Duffin37e0b772019-12-30 17:20:10 +0000740 "%s %q %s && m update-api",
741 script, mctx.ModuleDir(), strings.Join(scopes, " "))
Inseob Kim8098faa2019-03-18 10:19:51 +0900742 return
743 }
744
Inseob Kimc0907f12019-02-08 21:00:45 +0900745 // for public API stubs
746 module.createStubsLibrary(mctx, apiScopePublic)
Paul Duffinc4cea762019-12-30 17:32:47 +0000747 module.createStubsSources(mctx, apiScopePublic)
Inseob Kimc0907f12019-02-08 21:00:45 +0900748
Paul Duffin37e0b772019-12-30 17:20:10 +0000749 if hasSystemAndTestApis {
Inseob Kimc0907f12019-02-08 21:00:45 +0900750 // for system API stubs
751 module.createStubsLibrary(mctx, apiScopeSystem)
Paul Duffinc4cea762019-12-30 17:32:47 +0000752 module.createStubsSources(mctx, apiScopeSystem)
Inseob Kimc0907f12019-02-08 21:00:45 +0900753
754 // for test API stubs
755 module.createStubsLibrary(mctx, apiScopeTest)
Paul Duffinc4cea762019-12-30 17:32:47 +0000756 module.createStubsSources(mctx, apiScopeTest)
Inseob Kimc0907f12019-02-08 21:00:45 +0900757
758 // for runtime
759 module.createXmlFile(mctx)
760 }
761
762 // record java_sdk_library modules so that they are exported to make
763 javaSdkLibraries := javaSdkLibraries(mctx.Config())
764 javaSdkLibrariesLock.Lock()
765 defer javaSdkLibrariesLock.Unlock()
766 *javaSdkLibraries = append(*javaSdkLibraries, module.BaseModuleName())
767}
768
769func (module *SdkLibrary) InitSdkLibraryProperties() {
Sundong Ahn054b19a2018-10-19 13:46:09 +0900770 module.AddProperties(
771 &module.sdkLibraryProperties,
772 &module.Library.Module.properties,
773 &module.Library.Module.dexpreoptProperties,
774 &module.Library.Module.deviceProperties,
775 &module.Library.Module.protoProperties,
776 )
777
778 module.Library.Module.properties.Installable = proptools.BoolPtr(true)
779 module.Library.Module.deviceProperties.IsSDKLibrary = true
Inseob Kimc0907f12019-02-08 21:00:45 +0900780}
Sundong Ahn054b19a2018-10-19 13:46:09 +0900781
Jaewoong Jung4f158ee2019-07-11 10:05:35 -0700782// java_sdk_library is a special Java library that provides optional platform APIs to apps.
783// In practice, it can be viewed as a combination of several modules: 1) stubs library that clients
784// are linked against to, 2) droiddoc module that internally generates API stubs source files,
785// 3) the real runtime shared library that implements the APIs, and 4) XML file for adding
786// the runtime lib to the classpath at runtime if requested via <uses-library>.
Inseob Kimc0907f12019-02-08 21:00:45 +0900787func SdkLibraryFactory() android.Module {
788 module := &SdkLibrary{}
789 module.InitSdkLibraryProperties()
Jooyung Han58f26ab2019-12-18 15:34:32 +0900790 android.InitApexModule(module)
Sundong Ahn054b19a2018-10-19 13:46:09 +0900791 InitJavaModule(module, android.HostAndDeviceSupported)
Colin Crossf8b860a2019-04-16 14:43:28 -0700792 android.AddLoadHook(module, func(ctx android.LoadHookContext) { module.CreateInternalModules(ctx) })
Jiyong Parkc678ad32018-04-10 13:07:10 +0900793 return module
794}
Colin Cross79c7c262019-04-17 11:11:46 -0700795
796//
797// SDK library prebuilts
798//
799
800type sdkLibraryImportProperties struct {
801 Jars []string `android:"path"`
802
803 Sdk_version *string
804
805 Installable *bool
806
807 // List of shared java libs that this module has dependencies to
808 Libs []string
809
810 // List of files to remove from the jar file(s)
811 Exclude_files []string
812
813 // List of directories to remove from the jar file(s)
814 Exclude_dirs []string
815}
816
817type sdkLibraryImport struct {
818 android.ModuleBase
819 android.DefaultableModuleBase
820 prebuilt android.Prebuilt
821
822 properties sdkLibraryImportProperties
823
824 stubsPath android.Paths
825}
826
827var _ SdkLibraryDependency = (*sdkLibraryImport)(nil)
828
Jaewoong Jung4f158ee2019-07-11 10:05:35 -0700829// java_sdk_library_import imports a prebuilt java_sdk_library.
Colin Cross79c7c262019-04-17 11:11:46 -0700830func sdkLibraryImportFactory() android.Module {
831 module := &sdkLibraryImport{}
832
833 module.AddProperties(&module.properties)
834
835 android.InitPrebuiltModule(module, &module.properties.Jars)
836 InitJavaModule(module, android.HostAndDeviceSupported)
837
838 android.AddLoadHook(module, func(mctx android.LoadHookContext) { module.createInternalModules(mctx) })
839 return module
840}
841
842func (module *sdkLibraryImport) Prebuilt() *android.Prebuilt {
843 return &module.prebuilt
844}
845
846func (module *sdkLibraryImport) Name() string {
847 return module.prebuilt.Name(module.ModuleBase.Name())
848}
849
850func (module *sdkLibraryImport) createInternalModules(mctx android.LoadHookContext) {
851 // Creates a java import for the jar with ".stubs" suffix
852 props := struct {
Sundong Ahn0d7dff42019-12-04 12:53:44 +0900853 Name *string
854 Soc_specific *bool
855 Device_specific *bool
856 Product_specific *bool
857 System_ext_specific *bool
Colin Cross79c7c262019-04-17 11:11:46 -0700858 }{}
859
860 props.Name = proptools.StringPtr(module.BaseModuleName() + sdkStubsLibrarySuffix)
861
862 if module.SocSpecific() {
863 props.Soc_specific = proptools.BoolPtr(true)
864 } else if module.DeviceSpecific() {
865 props.Device_specific = proptools.BoolPtr(true)
866 } else if module.ProductSpecific() {
867 props.Product_specific = proptools.BoolPtr(true)
Sundong Ahn0d7dff42019-12-04 12:53:44 +0900868 } else if module.SystemExtSpecific() {
869 props.System_ext_specific = proptools.BoolPtr(true)
Colin Cross79c7c262019-04-17 11:11:46 -0700870 }
871
Colin Cross84dfc3d2019-09-25 11:33:01 -0700872 mctx.CreateModule(ImportFactory, &props, &module.properties)
Colin Cross79c7c262019-04-17 11:11:46 -0700873
874 javaSdkLibraries := javaSdkLibraries(mctx.Config())
875 javaSdkLibrariesLock.Lock()
876 defer javaSdkLibrariesLock.Unlock()
877 *javaSdkLibraries = append(*javaSdkLibraries, module.BaseModuleName())
878}
879
880func (module *sdkLibraryImport) DepsMutator(ctx android.BottomUpMutatorContext) {
881 // Add dependencies to the prebuilt stubs library
882 ctx.AddVariationDependencies(nil, publicApiStubsTag, module.BaseModuleName()+sdkStubsLibrarySuffix)
883}
884
885func (module *sdkLibraryImport) GenerateAndroidBuildActions(ctx android.ModuleContext) {
886 // Record the paths to the prebuilt stubs library.
887 ctx.VisitDirectDeps(func(to android.Module) {
888 tag := ctx.OtherModuleDependencyTag(to)
889
890 switch tag {
891 case publicApiStubsTag:
892 module.stubsPath = to.(Dependency).HeaderJars()
893 }
894 })
895}
896
897// to satisfy SdkLibraryDependency interface
Colin Cross0ea8ba82019-06-06 14:33:29 -0700898func (module *sdkLibraryImport) SdkHeaderJars(ctx android.BaseModuleContext, sdkVersion string) android.Paths {
Colin Cross79c7c262019-04-17 11:11:46 -0700899 // This module is just a wrapper for the prebuilt stubs.
900 return module.stubsPath
901}
902
903// to satisfy SdkLibraryDependency interface
Colin Cross0ea8ba82019-06-06 14:33:29 -0700904func (module *sdkLibraryImport) SdkImplementationJars(ctx android.BaseModuleContext, sdkVersion string) android.Paths {
Colin Cross79c7c262019-04-17 11:11:46 -0700905 // This module is just a wrapper for the stubs.
906 return module.stubsPath
907}