blob: d69339bdda00e7133036dddce883ae027c95d302 [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.
Paul Duffin12ceb462019-12-24 20:31:31 +0000361func (module *SdkLibrary) sdkVersionForScope(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
Paul Duffin12ceb462019-12-24 20:31:31 +0000374// Get the sdk version for use when compiling the stubs library.
375func (module *SdkLibrary) sdkVersionForStubsLibrary(mctx android.BaseModuleContext, apiScope apiScope) string {
376 sdkDep := decodeSdkDep(mctx, sdkContext(&module.Library))
377 if sdkDep.hasStandardLibs() {
378 // If building against a standard sdk then use the sdk version appropriate for the scope.
379 return module.sdkVersionForScope(apiScope)
380 } else {
381 // Otherwise, use no system module.
382 return "none"
383 }
384}
385
Jiyong Parkc678ad32018-04-10 13:07:10 +0900386// $(INTERNAL_PLATFORM_<apiTagName>_API_FILE) points to the generated
387// api file for the current source
388// TODO: remove this when apicheck is done in soong
Inseob Kimc0907f12019-02-08 21:00:45 +0900389func (module *SdkLibrary) apiTagName(apiScope apiScope) string {
Jiyong Parkc678ad32018-04-10 13:07:10 +0900390 apiTagName := strings.Replace(strings.ToUpper(module.BaseModuleName()), ".", "_", -1)
Jiyong Parkdf130542018-04-27 16:29:21 +0900391 switch apiScope {
392 case apiScopeSystem:
Jiyong Parkc678ad32018-04-10 13:07:10 +0900393 apiTagName = apiTagName + "_SYSTEM"
Jiyong Parkdf130542018-04-27 16:29:21 +0900394 case apiScopeTest:
395 apiTagName = apiTagName + "_TEST"
Jiyong Parkc678ad32018-04-10 13:07:10 +0900396 }
397 return apiTagName
398}
399
Inseob Kimc0907f12019-02-08 21:00:45 +0900400func (module *SdkLibrary) latestApiFilegroupName(apiScope apiScope) string {
Jiyong Park58c518b2018-05-12 22:29:12 +0900401 name := ":" + module.BaseModuleName() + ".api."
Jiyong Parkdf130542018-04-27 16:29:21 +0900402 switch apiScope {
Jiyong Park58c518b2018-05-12 22:29:12 +0900403 case apiScopePublic:
404 name = name + "public"
Jiyong Parkdf130542018-04-27 16:29:21 +0900405 case apiScopeSystem:
Jiyong Park58c518b2018-05-12 22:29:12 +0900406 name = name + "system"
Jiyong Parkdf130542018-04-27 16:29:21 +0900407 case apiScopeTest:
Jiyong Park58c518b2018-05-12 22:29:12 +0900408 name = name + "test"
Jiyong Parkc678ad32018-04-10 13:07:10 +0900409 }
Jiyong Park58c518b2018-05-12 22:29:12 +0900410 name = name + ".latest"
411 return name
412}
Jiyong Parkc678ad32018-04-10 13:07:10 +0900413
Inseob Kimc0907f12019-02-08 21:00:45 +0900414func (module *SdkLibrary) latestRemovedApiFilegroupName(apiScope apiScope) string {
Jiyong Park58c518b2018-05-12 22:29:12 +0900415 name := ":" + module.BaseModuleName() + "-removed.api."
416 switch apiScope {
417 case apiScopePublic:
418 name = name + "public"
419 case apiScopeSystem:
420 name = name + "system"
421 case apiScopeTest:
422 name = name + "test"
423 }
424 name = name + ".latest"
425 return name
Jiyong Parkc678ad32018-04-10 13:07:10 +0900426}
427
428// Creates a static java library that has API stubs
Colin Crossf8b860a2019-04-16 14:43:28 -0700429func (module *SdkLibrary) createStubsLibrary(mctx android.LoadHookContext, apiScope apiScope) {
Jiyong Parkc678ad32018-04-10 13:07:10 +0900430 props := struct {
Sundong Ahn0d7dff42019-12-04 12:53:44 +0900431 Name *string
432 Srcs []string
433 Sdk_version *string
Paul Duffin12ceb462019-12-24 20:31:31 +0000434 System_modules *string
Sundong Ahn0d7dff42019-12-04 12:53:44 +0900435 Libs []string
436 Soc_specific *bool
437 Device_specific *bool
438 Product_specific *bool
439 System_ext_specific *bool
440 Compile_dex *bool
Sundong Ahn0d7dff42019-12-04 12:53:44 +0900441 Java_version *string
442 Product_variables struct {
Jiyong Parkc678ad32018-04-10 13:07:10 +0900443 Unbundled_build struct {
444 Enabled *bool
445 }
Jiyong Park82484c02018-04-23 21:41:26 +0900446 Pdk struct {
447 Enabled *bool
448 }
Jiyong Parkc678ad32018-04-10 13:07:10 +0900449 }
Sundong Ahn054b19a2018-10-19 13:46:09 +0900450 Openjdk9 struct {
451 Srcs []string
452 Javacflags []string
453 }
Jiyong Parkc678ad32018-04-10 13:07:10 +0900454 }{}
455
Jiyong Parkdf130542018-04-27 16:29:21 +0900456 props.Name = proptools.StringPtr(module.stubsName(apiScope))
Jiyong Parkc678ad32018-04-10 13:07:10 +0900457 // sources are generated from the droiddoc
Jiyong Parkdf130542018-04-27 16:29:21 +0900458 props.Srcs = []string{":" + module.docsName(apiScope)}
Paul Duffin12ceb462019-12-24 20:31:31 +0000459 sdkVersion := module.sdkVersionForStubsLibrary(mctx, apiScope)
Paul Duffin52d398a2019-06-11 12:31:14 +0100460 props.Sdk_version = proptools.StringPtr(sdkVersion)
Paul Duffin12ceb462019-12-24 20:31:31 +0000461 props.System_modules = module.Library.Module.deviceProperties.System_modules
Sundong Ahn054b19a2018-10-19 13:46:09 +0900462 props.Libs = module.sdkLibraryProperties.Stub_only_libs
Jiyong Parkc678ad32018-04-10 13:07:10 +0900463 // Unbundled apps will use the prebult one from /prebuilts/sdk
Colin Cross10932872019-04-18 14:27:12 -0700464 if mctx.Config().UnbundledBuildUsePrebuiltSdks() {
Colin Cross2c77ceb2019-01-21 11:56:21 -0800465 props.Product_variables.Unbundled_build.Enabled = proptools.BoolPtr(false)
466 }
Jiyong Park82484c02018-04-23 21:41:26 +0900467 props.Product_variables.Pdk.Enabled = proptools.BoolPtr(false)
Sundong Ahn054b19a2018-10-19 13:46:09 +0900468 props.Openjdk9.Srcs = module.Library.Module.properties.Openjdk9.Srcs
469 props.Openjdk9.Javacflags = module.Library.Module.properties.Openjdk9.Javacflags
470 props.Java_version = module.Library.Module.properties.Java_version
471 if module.Library.Module.deviceProperties.Compile_dex != nil {
472 props.Compile_dex = module.Library.Module.deviceProperties.Compile_dex
Sundong Ahndd567f92018-07-31 17:19:11 +0900473 }
Jiyong Parkc678ad32018-04-10 13:07:10 +0900474
475 if module.SocSpecific() {
476 props.Soc_specific = proptools.BoolPtr(true)
477 } else if module.DeviceSpecific() {
478 props.Device_specific = proptools.BoolPtr(true)
479 } else if module.ProductSpecific() {
480 props.Product_specific = proptools.BoolPtr(true)
Sundong Ahn0d7dff42019-12-04 12:53:44 +0900481 } else if module.SystemExtSpecific() {
482 props.System_ext_specific = proptools.BoolPtr(true)
Jiyong Parkc678ad32018-04-10 13:07:10 +0900483 }
484
Colin Cross84dfc3d2019-09-25 11:33:01 -0700485 mctx.CreateModule(LibraryFactory, &props)
Jiyong Parkc678ad32018-04-10 13:07:10 +0900486}
487
488// Creates a droiddoc module that creates stubs source files from the given full source
489// files
Paul Duffinc4cea762019-12-30 17:32:47 +0000490func (module *SdkLibrary) createStubsSources(mctx android.LoadHookContext, apiScope apiScope) {
Jiyong Parkc678ad32018-04-10 13:07:10 +0900491 props := struct {
Sundong Ahn054b19a2018-10-19 13:46:09 +0900492 Name *string
493 Srcs []string
494 Installable *bool
Paul Duffin52d398a2019-06-11 12:31:14 +0100495 Sdk_version *string
Paul Duffin12ceb462019-12-24 20:31:31 +0000496 System_modules *string
Sundong Ahn054b19a2018-10-19 13:46:09 +0900497 Libs []string
Paul Duffin11512472019-02-11 15:55:17 +0000498 Arg_files []string
Sundong Ahn054b19a2018-10-19 13:46:09 +0900499 Args *string
500 Api_tag_name *string
501 Api_filename *string
502 Removed_api_filename *string
Sundong Ahn054b19a2018-10-19 13:46:09 +0900503 Java_version *string
504 Merge_annotations_dirs []string
505 Merge_inclusion_annotations_dirs []string
506 Check_api struct {
Inseob Kim38449af2019-02-28 14:24:05 +0900507 Current ApiToCheck
508 Last_released ApiToCheck
509 Ignore_missing_latest_api *bool
Jiyong Park58c518b2018-05-12 22:29:12 +0900510 }
Sundong Ahn1b92c822018-05-29 11:35:17 +0900511 Aidl struct {
512 Include_dirs []string
513 Local_include_dirs []string
514 }
Jiyong Parkc678ad32018-04-10 13:07:10 +0900515 }{}
516
Paul Duffin250e6192019-06-07 10:44:37 +0100517 sdkDep := decodeSdkDep(mctx, sdkContext(&module.Library))
Paul Duffin12ceb462019-12-24 20:31:31 +0000518 // Use the platform API if standard libraries were requested, otherwise use
519 // no default libraries.
Paul Duffin52d398a2019-06-11 12:31:14 +0100520 sdkVersion := ""
521 if !sdkDep.hasStandardLibs() {
522 sdkVersion = "none"
523 }
Paul Duffin250e6192019-06-07 10:44:37 +0100524
Jiyong Parkdf130542018-04-27 16:29:21 +0900525 props.Name = proptools.StringPtr(module.docsName(apiScope))
Sundong Ahn054b19a2018-10-19 13:46:09 +0900526 props.Srcs = append(props.Srcs, module.Library.Module.properties.Srcs...)
Paul Duffin52d398a2019-06-11 12:31:14 +0100527 props.Sdk_version = proptools.StringPtr(sdkVersion)
Paul Duffin12ceb462019-12-24 20:31:31 +0000528 props.System_modules = module.Library.Module.deviceProperties.System_modules
Jiyong Parkc678ad32018-04-10 13:07:10 +0900529 props.Installable = proptools.BoolPtr(false)
Sundong Ahne6f0b052018-06-05 16:46:14 +0900530 // A droiddoc module has only one Libs property and doesn't distinguish between
531 // shared libs and static libs. So we need to add both of these libs to Libs property.
Sundong Ahn054b19a2018-10-19 13:46:09 +0900532 props.Libs = module.Library.Module.properties.Libs
533 props.Libs = append(props.Libs, module.Library.Module.properties.Static_libs...)
534 props.Aidl.Include_dirs = module.Library.Module.deviceProperties.Aidl.Include_dirs
535 props.Aidl.Local_include_dirs = module.Library.Module.deviceProperties.Aidl.Local_include_dirs
Sundong Ahn054b19a2018-10-19 13:46:09 +0900536 props.Java_version = module.Library.Module.properties.Java_version
Jiyong Parkc678ad32018-04-10 13:07:10 +0900537
Sundong Ahn054b19a2018-10-19 13:46:09 +0900538 props.Merge_annotations_dirs = module.sdkLibraryProperties.Merge_annotations_dirs
539 props.Merge_inclusion_annotations_dirs = module.sdkLibraryProperties.Merge_inclusion_annotations_dirs
540
Paul Duffin235ffff2019-12-24 10:41:30 +0000541 droiddocArgs := []string{}
542 if len(module.sdkLibraryProperties.Api_packages) != 0 {
543 droiddocArgs = append(droiddocArgs, "--stub-packages "+strings.Join(module.sdkLibraryProperties.Api_packages, ":"))
544 }
545 if len(module.sdkLibraryProperties.Hidden_api_packages) != 0 {
546 droiddocArgs = append(droiddocArgs,
547 android.JoinWithPrefix(module.sdkLibraryProperties.Hidden_api_packages, " --hide-package "))
548 }
549 droiddocArgs = append(droiddocArgs, module.sdkLibraryProperties.Droiddoc_options...)
550 disabledWarnings := []string{
551 "MissingPermission",
552 "BroadcastBehavior",
553 "HiddenSuperclass",
554 "DeprecationMismatch",
555 "UnavailableSymbol",
556 "SdkConstant",
557 "HiddenTypeParameter",
558 "Todo",
559 "Typo",
560 }
561 droiddocArgs = append(droiddocArgs, android.JoinWithPrefix(disabledWarnings, "--hide "))
Sundong Ahnfb2721f2018-09-17 13:23:09 +0900562
Jiyong Parkdf130542018-04-27 16:29:21 +0900563 switch apiScope {
564 case apiScopeSystem:
Paul Duffin235ffff2019-12-24 10:41:30 +0000565 droiddocArgs = append(droiddocArgs, "-showAnnotation android.annotation.SystemApi")
Jiyong Parkdf130542018-04-27 16:29:21 +0900566 case apiScopeTest:
Paul Duffin235ffff2019-12-24 10:41:30 +0000567 droiddocArgs = append(droiddocArgs, " -showAnnotation android.annotation.TestApi")
Jiyong Parkc678ad32018-04-10 13:07:10 +0900568 }
Paul Duffin11512472019-02-11 15:55:17 +0000569 props.Arg_files = module.sdkLibraryProperties.Droiddoc_option_files
Paul Duffin235ffff2019-12-24 10:41:30 +0000570 props.Args = proptools.StringPtr(strings.Join(droiddocArgs, " "))
Jiyong Parkc678ad32018-04-10 13:07:10 +0900571
572 // List of APIs identified from the provided source files are created. They are later
573 // compared against to the not-yet-released (a.k.a current) list of APIs and to the
574 // last-released (a.k.a numbered) list of API.
Jiyong Parkc678ad32018-04-10 13:07:10 +0900575 currentApiFileName := "current.txt"
576 removedApiFileName := "removed.txt"
Jiyong Parkdf130542018-04-27 16:29:21 +0900577 switch apiScope {
578 case apiScopeSystem:
Jiyong Parkc678ad32018-04-10 13:07:10 +0900579 currentApiFileName = "system-" + currentApiFileName
580 removedApiFileName = "system-" + removedApiFileName
Jiyong Parkdf130542018-04-27 16:29:21 +0900581 case apiScopeTest:
582 currentApiFileName = "test-" + currentApiFileName
583 removedApiFileName = "test-" + removedApiFileName
Jiyong Parkc678ad32018-04-10 13:07:10 +0900584 }
585 currentApiFileName = path.Join("api", currentApiFileName)
586 removedApiFileName = path.Join("api", removedApiFileName)
Jiyong Park58c518b2018-05-12 22:29:12 +0900587 // TODO(jiyong): remove these three props
Jiyong Parkdf130542018-04-27 16:29:21 +0900588 props.Api_tag_name = proptools.StringPtr(module.apiTagName(apiScope))
Jiyong Parkc678ad32018-04-10 13:07:10 +0900589 props.Api_filename = proptools.StringPtr(currentApiFileName)
590 props.Removed_api_filename = proptools.StringPtr(removedApiFileName)
591
Jiyong Park58c518b2018-05-12 22:29:12 +0900592 // check against the not-yet-release API
593 props.Check_api.Current.Api_file = proptools.StringPtr(currentApiFileName)
594 props.Check_api.Current.Removed_api_file = proptools.StringPtr(removedApiFileName)
Jiyong Park58c518b2018-05-12 22:29:12 +0900595
596 // check against the latest released API
597 props.Check_api.Last_released.Api_file = proptools.StringPtr(
598 module.latestApiFilegroupName(apiScope))
599 props.Check_api.Last_released.Removed_api_file = proptools.StringPtr(
600 module.latestRemovedApiFilegroupName(apiScope))
Inseob Kim38449af2019-02-28 14:24:05 +0900601 props.Check_api.Ignore_missing_latest_api = proptools.BoolPtr(true)
Jiyong Park58c518b2018-05-12 22:29:12 +0900602
Colin Cross84dfc3d2019-09-25 11:33:01 -0700603 mctx.CreateModule(DroidstubsFactory, &props)
Jiyong Parkc678ad32018-04-10 13:07:10 +0900604}
605
Jiyong Parkc678ad32018-04-10 13:07:10 +0900606// Creates the xml file that publicizes the runtime library
Colin Crossf8b860a2019-04-16 14:43:28 -0700607func (module *SdkLibrary) createXmlFile(mctx android.LoadHookContext) {
Jiyong Parkc678ad32018-04-10 13:07:10 +0900608 // creates a prebuilt_etc module to actually place the xml file under
609 // <partition>/etc/permissions
610 etcProps := struct {
Sundong Ahn0d7dff42019-12-04 12:53:44 +0900611 Name *string
612 Src *string
613 Sub_dir *string
614 Soc_specific *bool
615 Device_specific *bool
616 Product_specific *bool
617 System_ext_specific *bool
Jiyong Parkc678ad32018-04-10 13:07:10 +0900618 }{}
619 etcProps.Name = proptools.StringPtr(module.xmlFileName())
Jooyung Han624058e2019-12-24 18:38:06 +0900620 etcProps.Src = proptools.StringPtr(":" + module.BaseModuleName() + "{.xml}")
Jiyong Parkc678ad32018-04-10 13:07:10 +0900621 etcProps.Sub_dir = proptools.StringPtr("permissions")
622 if module.SocSpecific() {
623 etcProps.Soc_specific = proptools.BoolPtr(true)
624 } else if module.DeviceSpecific() {
625 etcProps.Device_specific = proptools.BoolPtr(true)
626 } else if module.ProductSpecific() {
627 etcProps.Product_specific = proptools.BoolPtr(true)
Sundong Ahn0d7dff42019-12-04 12:53:44 +0900628 } else if module.SystemExtSpecific() {
629 etcProps.System_ext_specific = proptools.BoolPtr(true)
Jiyong Parkc678ad32018-04-10 13:07:10 +0900630 }
Colin Cross84dfc3d2019-09-25 11:33:01 -0700631 mctx.CreateModule(android.PrebuiltEtcFactory, &etcProps)
Jiyong Parkc678ad32018-04-10 13:07:10 +0900632}
633
Colin Cross0ea8ba82019-06-06 14:33:29 -0700634func (module *SdkLibrary) PrebuiltJars(ctx android.BaseModuleContext, sdkVersion string) android.Paths {
Sundong Ahn054b19a2018-10-19 13:46:09 +0900635 var api, v string
Paul Duffin52d398a2019-06-11 12:31:14 +0100636 if sdkVersion == "" || sdkVersion == "none" {
Sundong Ahn054b19a2018-10-19 13:46:09 +0900637 api = "system"
638 v = "current"
639 } else if strings.Contains(sdkVersion, "_") {
640 t := strings.Split(sdkVersion, "_")
641 api = t[0]
642 v = t[1]
Jiyong Parkc678ad32018-04-10 13:07:10 +0900643 } else {
Sundong Ahn054b19a2018-10-19 13:46:09 +0900644 api = "public"
645 v = sdkVersion
646 }
647 dir := filepath.Join("prebuilts", "sdk", v, api)
648 jar := filepath.Join(dir, module.BaseModuleName()+".jar")
649 jarPath := android.ExistentPathForSource(ctx, jar)
Sundong Ahnae418ac2019-02-28 15:01:28 +0900650 if !jarPath.Valid() {
651 ctx.PropertyErrorf("sdk_library", "invalid sdk version %q, %q does not exist", v, jar)
652 return nil
653 }
Sundong Ahn054b19a2018-10-19 13:46:09 +0900654 return android.Paths{jarPath.Path()}
655}
656
657// to satisfy SdkLibraryDependency interface
Colin Cross0ea8ba82019-06-06 14:33:29 -0700658func (module *SdkLibrary) SdkHeaderJars(ctx android.BaseModuleContext, sdkVersion string) android.Paths {
Sundong Ahn054b19a2018-10-19 13:46:09 +0900659 // This module is just a wrapper for the stubs.
Colin Cross10932872019-04-18 14:27:12 -0700660 if ctx.Config().UnbundledBuildUsePrebuiltSdks() {
Sundong Ahn054b19a2018-10-19 13:46:09 +0900661 return module.PrebuiltJars(ctx, sdkVersion)
662 } else {
663 if strings.HasPrefix(sdkVersion, "system_") {
664 return module.systemApiStubsPath
665 } else if sdkVersion == "" {
666 return module.Library.HeaderJars()
667 } else {
668 return module.publicApiStubsPath
669 }
Jiyong Parkc678ad32018-04-10 13:07:10 +0900670 }
671}
672
Sundong Ahn241cd372018-07-13 16:16:44 +0900673// to satisfy SdkLibraryDependency interface
Colin Cross0ea8ba82019-06-06 14:33:29 -0700674func (module *SdkLibrary) SdkImplementationJars(ctx android.BaseModuleContext, sdkVersion string) android.Paths {
Sundong Ahn241cd372018-07-13 16:16:44 +0900675 // This module is just a wrapper for the stubs.
Colin Cross10932872019-04-18 14:27:12 -0700676 if ctx.Config().UnbundledBuildUsePrebuiltSdks() {
Sundong Ahn054b19a2018-10-19 13:46:09 +0900677 return module.PrebuiltJars(ctx, sdkVersion)
Sundong Ahn241cd372018-07-13 16:16:44 +0900678 } else {
Sundong Ahn054b19a2018-10-19 13:46:09 +0900679 if strings.HasPrefix(sdkVersion, "system_") {
680 return module.systemApiStubsImplPath
681 } else if sdkVersion == "" {
682 return module.Library.ImplementationJars()
683 } else {
684 return module.publicApiStubsImplPath
685 }
Sundong Ahn241cd372018-07-13 16:16:44 +0900686 }
687}
688
Sundong Ahn80a87b32019-05-13 15:02:50 +0900689func (module *SdkLibrary) SetNoDist() {
690 module.sdkLibraryProperties.No_dist = proptools.BoolPtr(true)
691}
692
Colin Cross571cccf2019-02-04 11:22:08 -0800693var javaSdkLibrariesKey = android.NewOnceKey("javaSdkLibraries")
694
Jiyong Park82484c02018-04-23 21:41:26 +0900695func javaSdkLibraries(config android.Config) *[]string {
Colin Cross571cccf2019-02-04 11:22:08 -0800696 return config.Once(javaSdkLibrariesKey, func() interface{} {
Jiyong Park82484c02018-04-23 21:41:26 +0900697 return &[]string{}
698 }).(*[]string)
699}
700
Jiyong Parkc678ad32018-04-10 13:07:10 +0900701// For a java_sdk_library module, create internal modules for stubs, docs,
702// runtime libs and xml file. If requested, the stubs and docs are created twice
703// once for public API level and once for system API level
Colin Crossf8b860a2019-04-16 14:43:28 -0700704func (module *SdkLibrary) CreateInternalModules(mctx android.LoadHookContext) {
Inseob Kim6e93ac92019-03-21 17:43:49 +0900705 if len(module.Library.Module.properties.Srcs) == 0 {
Inseob Kimc0907f12019-02-08 21:00:45 +0900706 mctx.PropertyErrorf("srcs", "java_sdk_library must specify srcs")
Jooyung Han58f26ab2019-12-18 15:34:32 +0900707 return
Inseob Kimc0907f12019-02-08 21:00:45 +0900708 }
709
Inseob Kim6e93ac92019-03-21 17:43:49 +0900710 if len(module.sdkLibraryProperties.Api_packages) == 0 {
Inseob Kimc0907f12019-02-08 21:00:45 +0900711 mctx.PropertyErrorf("api_packages", "java_sdk_library must specify api_packages")
Jooyung Han58f26ab2019-12-18 15:34:32 +0900712 return
Inseob Kimc0907f12019-02-08 21:00:45 +0900713 }
Inseob Kim8098faa2019-03-18 10:19:51 +0900714
Paul Duffin37e0b772019-12-30 17:20:10 +0000715 // If this builds against standard libraries (i.e. is not part of the core libraries)
716 // then assume it provides both system and test apis. Otherwise, assume it does not and
717 // also assume it does not contribute to the dist build.
718 sdkDep := decodeSdkDep(mctx, sdkContext(&module.Library))
719 hasSystemAndTestApis := sdkDep.hasStandardLibs()
720 module.sdkLibraryProperties.Has_system_and_test_apis = hasSystemAndTestApis
721 module.sdkLibraryProperties.No_dist = proptools.BoolPtr(!hasSystemAndTestApis)
722
723 scopes := []string{""}
724 if hasSystemAndTestApis {
725 scopes = append(scopes, "system-", "test-")
726 }
727
Inseob Kim8098faa2019-03-18 10:19:51 +0900728 missing_current_api := false
729
Paul Duffin37e0b772019-12-30 17:20:10 +0000730 for _, scope := range scopes {
Inseob Kim8098faa2019-03-18 10:19:51 +0900731 for _, api := range []string{"current.txt", "removed.txt"} {
732 path := path.Join(mctx.ModuleDir(), "api", scope+api)
733 p := android.ExistentPathForSource(mctx, path)
734 if !p.Valid() {
735 mctx.ModuleErrorf("Current api file %#v doesn't exist", path)
736 missing_current_api = true
737 }
738 }
739 }
740
741 if missing_current_api {
742 script := "build/soong/scripts/gen-java-current-api-files.sh"
743 p := android.ExistentPathForSource(mctx, script)
744
745 if !p.Valid() {
746 panic(fmt.Sprintf("script file %s doesn't exist", script))
747 }
748
749 mctx.ModuleErrorf("One or more current api files are missing. "+
750 "You can update them by:\n"+
Paul Duffin37e0b772019-12-30 17:20:10 +0000751 "%s %q %s && m update-api",
752 script, mctx.ModuleDir(), strings.Join(scopes, " "))
Inseob Kim8098faa2019-03-18 10:19:51 +0900753 return
754 }
755
Inseob Kimc0907f12019-02-08 21:00:45 +0900756 // for public API stubs
757 module.createStubsLibrary(mctx, apiScopePublic)
Paul Duffinc4cea762019-12-30 17:32:47 +0000758 module.createStubsSources(mctx, apiScopePublic)
Inseob Kimc0907f12019-02-08 21:00:45 +0900759
Paul Duffin37e0b772019-12-30 17:20:10 +0000760 if hasSystemAndTestApis {
Inseob Kimc0907f12019-02-08 21:00:45 +0900761 // for system API stubs
762 module.createStubsLibrary(mctx, apiScopeSystem)
Paul Duffinc4cea762019-12-30 17:32:47 +0000763 module.createStubsSources(mctx, apiScopeSystem)
Inseob Kimc0907f12019-02-08 21:00:45 +0900764
765 // for test API stubs
766 module.createStubsLibrary(mctx, apiScopeTest)
Paul Duffinc4cea762019-12-30 17:32:47 +0000767 module.createStubsSources(mctx, apiScopeTest)
Inseob Kimc0907f12019-02-08 21:00:45 +0900768
769 // for runtime
770 module.createXmlFile(mctx)
771 }
772
773 // record java_sdk_library modules so that they are exported to make
774 javaSdkLibraries := javaSdkLibraries(mctx.Config())
775 javaSdkLibrariesLock.Lock()
776 defer javaSdkLibrariesLock.Unlock()
777 *javaSdkLibraries = append(*javaSdkLibraries, module.BaseModuleName())
778}
779
780func (module *SdkLibrary) InitSdkLibraryProperties() {
Sundong Ahn054b19a2018-10-19 13:46:09 +0900781 module.AddProperties(
782 &module.sdkLibraryProperties,
783 &module.Library.Module.properties,
784 &module.Library.Module.dexpreoptProperties,
785 &module.Library.Module.deviceProperties,
786 &module.Library.Module.protoProperties,
787 )
788
789 module.Library.Module.properties.Installable = proptools.BoolPtr(true)
790 module.Library.Module.deviceProperties.IsSDKLibrary = true
Inseob Kimc0907f12019-02-08 21:00:45 +0900791}
Sundong Ahn054b19a2018-10-19 13:46:09 +0900792
Jaewoong Jung4f158ee2019-07-11 10:05:35 -0700793// java_sdk_library is a special Java library that provides optional platform APIs to apps.
794// In practice, it can be viewed as a combination of several modules: 1) stubs library that clients
795// are linked against to, 2) droiddoc module that internally generates API stubs source files,
796// 3) the real runtime shared library that implements the APIs, and 4) XML file for adding
797// the runtime lib to the classpath at runtime if requested via <uses-library>.
Inseob Kimc0907f12019-02-08 21:00:45 +0900798func SdkLibraryFactory() android.Module {
799 module := &SdkLibrary{}
800 module.InitSdkLibraryProperties()
Jooyung Han58f26ab2019-12-18 15:34:32 +0900801 android.InitApexModule(module)
Sundong Ahn054b19a2018-10-19 13:46:09 +0900802 InitJavaModule(module, android.HostAndDeviceSupported)
Colin Crossf8b860a2019-04-16 14:43:28 -0700803 android.AddLoadHook(module, func(ctx android.LoadHookContext) { module.CreateInternalModules(ctx) })
Jiyong Parkc678ad32018-04-10 13:07:10 +0900804 return module
805}
Colin Cross79c7c262019-04-17 11:11:46 -0700806
807//
808// SDK library prebuilts
809//
810
811type sdkLibraryImportProperties struct {
812 Jars []string `android:"path"`
813
814 Sdk_version *string
815
816 Installable *bool
817
818 // List of shared java libs that this module has dependencies to
819 Libs []string
820
821 // List of files to remove from the jar file(s)
822 Exclude_files []string
823
824 // List of directories to remove from the jar file(s)
825 Exclude_dirs []string
826}
827
828type sdkLibraryImport struct {
829 android.ModuleBase
830 android.DefaultableModuleBase
831 prebuilt android.Prebuilt
832
833 properties sdkLibraryImportProperties
834
835 stubsPath android.Paths
836}
837
838var _ SdkLibraryDependency = (*sdkLibraryImport)(nil)
839
Jaewoong Jung4f158ee2019-07-11 10:05:35 -0700840// java_sdk_library_import imports a prebuilt java_sdk_library.
Colin Cross79c7c262019-04-17 11:11:46 -0700841func sdkLibraryImportFactory() android.Module {
842 module := &sdkLibraryImport{}
843
844 module.AddProperties(&module.properties)
845
846 android.InitPrebuiltModule(module, &module.properties.Jars)
847 InitJavaModule(module, android.HostAndDeviceSupported)
848
849 android.AddLoadHook(module, func(mctx android.LoadHookContext) { module.createInternalModules(mctx) })
850 return module
851}
852
853func (module *sdkLibraryImport) Prebuilt() *android.Prebuilt {
854 return &module.prebuilt
855}
856
857func (module *sdkLibraryImport) Name() string {
858 return module.prebuilt.Name(module.ModuleBase.Name())
859}
860
861func (module *sdkLibraryImport) createInternalModules(mctx android.LoadHookContext) {
862 // Creates a java import for the jar with ".stubs" suffix
863 props := struct {
Sundong Ahn0d7dff42019-12-04 12:53:44 +0900864 Name *string
865 Soc_specific *bool
866 Device_specific *bool
867 Product_specific *bool
868 System_ext_specific *bool
Colin Cross79c7c262019-04-17 11:11:46 -0700869 }{}
870
871 props.Name = proptools.StringPtr(module.BaseModuleName() + sdkStubsLibrarySuffix)
872
873 if module.SocSpecific() {
874 props.Soc_specific = proptools.BoolPtr(true)
875 } else if module.DeviceSpecific() {
876 props.Device_specific = proptools.BoolPtr(true)
877 } else if module.ProductSpecific() {
878 props.Product_specific = proptools.BoolPtr(true)
Sundong Ahn0d7dff42019-12-04 12:53:44 +0900879 } else if module.SystemExtSpecific() {
880 props.System_ext_specific = proptools.BoolPtr(true)
Colin Cross79c7c262019-04-17 11:11:46 -0700881 }
882
Colin Cross84dfc3d2019-09-25 11:33:01 -0700883 mctx.CreateModule(ImportFactory, &props, &module.properties)
Colin Cross79c7c262019-04-17 11:11:46 -0700884
885 javaSdkLibraries := javaSdkLibraries(mctx.Config())
886 javaSdkLibrariesLock.Lock()
887 defer javaSdkLibrariesLock.Unlock()
888 *javaSdkLibraries = append(*javaSdkLibraries, module.BaseModuleName())
889}
890
891func (module *sdkLibraryImport) DepsMutator(ctx android.BottomUpMutatorContext) {
892 // Add dependencies to the prebuilt stubs library
893 ctx.AddVariationDependencies(nil, publicApiStubsTag, module.BaseModuleName()+sdkStubsLibrarySuffix)
894}
895
896func (module *sdkLibraryImport) GenerateAndroidBuildActions(ctx android.ModuleContext) {
897 // Record the paths to the prebuilt stubs library.
898 ctx.VisitDirectDeps(func(to android.Module) {
899 tag := ctx.OtherModuleDependencyTag(to)
900
901 switch tag {
902 case publicApiStubsTag:
903 module.stubsPath = to.(Dependency).HeaderJars()
904 }
905 })
906}
907
908// to satisfy SdkLibraryDependency interface
Colin Cross0ea8ba82019-06-06 14:33:29 -0700909func (module *sdkLibraryImport) SdkHeaderJars(ctx android.BaseModuleContext, sdkVersion string) android.Paths {
Colin Cross79c7c262019-04-17 11:11:46 -0700910 // This module is just a wrapper for the prebuilt stubs.
911 return module.stubsPath
912}
913
914// to satisfy SdkLibraryDependency interface
Colin Cross0ea8ba82019-06-06 14:33:29 -0700915func (module *sdkLibraryImport) SdkImplementationJars(ctx android.BaseModuleContext, sdkVersion string) android.Paths {
Colin Cross79c7c262019-04-17 11:11:46 -0700916 // This module is just a wrapper for the stubs.
917 return module.stubsPath
918}