blob: 057586f7eac4474e9d22f7c657b1ba6cb7ce1941 [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
Paul Duffin7a586d32019-12-30 17:09:34 +0000100 // list of package names that will be documented and publicized as API.
101 // This allows the API to be restricted to a subset of the source files provided.
102 // If this is unspecified then all the source files will be treated as being part
103 // of the API.
Jiyong Parkc678ad32018-04-10 13:07:10 +0900104 Api_packages []string
105
Jiyong Park5a2c9d72018-05-01 22:25:41 +0900106 // list of package names that must be hidden from the API
107 Hidden_api_packages []string
108
Paul Duffin749f98f2019-12-30 17:23:46 +0000109 // the relative path to the directory containing the api specification files.
110 // Defaults to "api".
111 Api_dir *string
112
Paul Duffin43db9be2019-12-30 17:35:49 +0000113 // If set to true there is no runtime library.
114 Api_only *bool
115
Paul Duffin11512472019-02-11 15:55:17 +0000116 // local files that are used within user customized droiddoc options.
117 Droiddoc_option_files []string
118
119 // additional droiddoc options
120 // Available variables for substitution:
121 //
122 // $(location <label>): the path to the droiddoc_option_files with name <label>
Sundong Ahndd567f92018-07-31 17:19:11 +0900123 Droiddoc_options []string
124
Sundong Ahn054b19a2018-10-19 13:46:09 +0900125 // a list of top-level directories containing files to merge qualifier annotations
126 // (i.e. those intended to be included in the stubs written) from.
127 Merge_annotations_dirs []string
128
129 // a list of top-level directories containing Java stub files to merge show/hide annotations from.
130 Merge_inclusion_annotations_dirs []string
131
132 // If set to true, the path of dist files is apistubs/core. Defaults to false.
133 Core_lib *bool
134
Sundong Ahn80a87b32019-05-13 15:02:50 +0900135 // don't create dist rules.
136 No_dist *bool `blueprint:"mutated"`
137
Paul Duffin37e0b772019-12-30 17:20:10 +0000138 // indicates whether system and test apis should be managed.
139 Has_system_and_test_apis bool `blueprint:"mutated"`
140
Jiyong Parkc678ad32018-04-10 13:07:10 +0900141 // TODO: determines whether to create HTML doc or not
142 //Html_doc *bool
143}
144
Inseob Kimc0907f12019-02-08 21:00:45 +0900145type SdkLibrary struct {
Sundong Ahn054b19a2018-10-19 13:46:09 +0900146 Library
Jiyong Parkc678ad32018-04-10 13:07:10 +0900147
Sundong Ahn054b19a2018-10-19 13:46:09 +0900148 sdkLibraryProperties sdkLibraryProperties
Jiyong Parkc678ad32018-04-10 13:07:10 +0900149
150 publicApiStubsPath android.Paths
151 systemApiStubsPath android.Paths
Jiyong Parkdf130542018-04-27 16:29:21 +0900152 testApiStubsPath android.Paths
Sundong Ahn241cd372018-07-13 16:16:44 +0900153
154 publicApiStubsImplPath android.Paths
155 systemApiStubsImplPath android.Paths
156 testApiStubsImplPath android.Paths
Sundong Ahn20e998b2018-07-24 11:19:26 +0900157
158 publicApiFilePath android.Path
159 systemApiFilePath android.Path
160 testApiFilePath android.Path
Jooyung Han58f26ab2019-12-18 15:34:32 +0900161
Jooyung Han624058e2019-12-24 18:38:06 +0900162 permissionsFile android.Path
Jiyong Parkc678ad32018-04-10 13:07:10 +0900163}
164
Inseob Kimc0907f12019-02-08 21:00:45 +0900165var _ Dependency = (*SdkLibrary)(nil)
166var _ SdkLibraryDependency = (*SdkLibrary)(nil)
Colin Cross897d2ed2019-02-11 14:03:51 -0800167
Inseob Kimc0907f12019-02-08 21:00:45 +0900168func (module *SdkLibrary) DepsMutator(ctx android.BottomUpMutatorContext) {
Jiyong Parke3ef3c82019-07-15 15:31:16 +0900169 useBuiltStubs := !ctx.Config().UnbundledBuildUsePrebuiltSdks()
Jiyong Parkc678ad32018-04-10 13:07:10 +0900170 // Add dependencies to the stubs library
Jiyong Parke3ef3c82019-07-15 15:31:16 +0900171 if useBuiltStubs {
172 ctx.AddVariationDependencies(nil, publicApiStubsTag, module.stubsName(apiScopePublic))
173 }
Colin Cross42d48b72018-08-29 14:10:52 -0700174 ctx.AddVariationDependencies(nil, publicApiFileTag, module.docsName(apiScopePublic))
Sundong Ahn054b19a2018-10-19 13:46:09 +0900175
Paul Duffin37e0b772019-12-30 17:20:10 +0000176 if module.sdkLibraryProperties.Has_system_and_test_apis {
Jiyong Parke3ef3c82019-07-15 15:31:16 +0900177 if useBuiltStubs {
178 ctx.AddVariationDependencies(nil, systemApiStubsTag, module.stubsName(apiScopeSystem))
179 ctx.AddVariationDependencies(nil, testApiStubsTag, module.stubsName(apiScopeTest))
180 }
Sundong Ahn054b19a2018-10-19 13:46:09 +0900181 ctx.AddVariationDependencies(nil, systemApiFileTag, module.docsName(apiScopeSystem))
182 ctx.AddVariationDependencies(nil, testApiFileTag, module.docsName(apiScopeTest))
Sundong Ahn054b19a2018-10-19 13:46:09 +0900183 }
184
185 module.Library.deps(ctx)
Jiyong Parkc678ad32018-04-10 13:07:10 +0900186}
187
Inseob Kimc0907f12019-02-08 21:00:45 +0900188func (module *SdkLibrary) GenerateAndroidBuildActions(ctx android.ModuleContext) {
Paul Duffin43db9be2019-12-30 17:35:49 +0000189 // Don't build an implementation library if this is api only.
190 if !proptools.Bool(module.sdkLibraryProperties.Api_only) {
191 module.Library.GenerateAndroidBuildActions(ctx)
192 }
Sundong Ahn054b19a2018-10-19 13:46:09 +0900193
Jooyung Han624058e2019-12-24 18:38:06 +0900194 module.buildPermissionsFile(ctx)
Jooyung Han58f26ab2019-12-18 15:34:32 +0900195
Sundong Ahn57368eb2018-07-06 11:20:23 +0900196 // Record the paths to the header jars of the library (stubs and impl).
Jiyong Parkc678ad32018-04-10 13:07:10 +0900197 // When this java_sdk_library is dependened from others via "libs" property,
198 // the recorded paths will be returned depending on the link type of the caller.
199 ctx.VisitDirectDeps(func(to android.Module) {
200 otherName := ctx.OtherModuleName(to)
201 tag := ctx.OtherModuleDependencyTag(to)
202
Sundong Ahn57368eb2018-07-06 11:20:23 +0900203 if lib, ok := to.(Dependency); ok {
Jiyong Parkc678ad32018-04-10 13:07:10 +0900204 switch tag {
205 case publicApiStubsTag:
Sundong Ahn57368eb2018-07-06 11:20:23 +0900206 module.publicApiStubsPath = lib.HeaderJars()
Sundong Ahn241cd372018-07-13 16:16:44 +0900207 module.publicApiStubsImplPath = lib.ImplementationJars()
Jiyong Parkc678ad32018-04-10 13:07:10 +0900208 case systemApiStubsTag:
Sundong Ahn57368eb2018-07-06 11:20:23 +0900209 module.systemApiStubsPath = lib.HeaderJars()
Sundong Ahn241cd372018-07-13 16:16:44 +0900210 module.systemApiStubsImplPath = lib.ImplementationJars()
Jiyong Parkdf130542018-04-27 16:29:21 +0900211 case testApiStubsTag:
Sundong Ahn57368eb2018-07-06 11:20:23 +0900212 module.testApiStubsPath = lib.HeaderJars()
Sundong Ahn241cd372018-07-13 16:16:44 +0900213 module.testApiStubsImplPath = lib.ImplementationJars()
Jiyong Parkc678ad32018-04-10 13:07:10 +0900214 }
215 }
Sundong Ahn20e998b2018-07-24 11:19:26 +0900216 if doc, ok := to.(ApiFilePath); ok {
217 switch tag {
218 case publicApiFileTag:
219 module.publicApiFilePath = doc.ApiFilePath()
220 case systemApiFileTag:
221 module.systemApiFilePath = doc.ApiFilePath()
222 case testApiFileTag:
223 module.testApiFilePath = doc.ApiFilePath()
224 default:
225 ctx.ModuleErrorf("depends on module %q of unknown tag %q", otherName, tag)
226 }
227 }
Jiyong Parkc678ad32018-04-10 13:07:10 +0900228 })
229}
230
Jooyung Han624058e2019-12-24 18:38:06 +0900231func (module *SdkLibrary) buildPermissionsFile(ctx android.ModuleContext) {
232 xmlContent := fmt.Sprintf(permissionsTemplate, module.BaseModuleName(), module.implPath())
233 permissionsFile := android.PathForModuleOut(ctx, module.xmlFileName())
Jooyung Han58f26ab2019-12-18 15:34:32 +0900234
Jooyung Han624058e2019-12-24 18:38:06 +0900235 ctx.Build(pctx, android.BuildParams{
236 Rule: android.WriteFile,
237 Output: permissionsFile,
238 Description: "Generating " + module.BaseModuleName() + " permissions",
239 Args: map[string]string{
240 "content": xmlContent,
241 },
242 })
Jooyung Han58f26ab2019-12-18 15:34:32 +0900243
Jooyung Han624058e2019-12-24 18:38:06 +0900244 module.permissionsFile = permissionsFile
Jooyung Han58f26ab2019-12-18 15:34:32 +0900245}
246
Jooyung Han624058e2019-12-24 18:38:06 +0900247func (module *SdkLibrary) OutputFiles(tag string) (android.Paths, error) {
248 switch tag {
249 case ".xml":
250 return android.Paths{module.permissionsFile}, nil
251 }
252 return module.Library.OutputFiles(tag)
Jooyung Han58f26ab2019-12-18 15:34:32 +0900253}
254
Jiyong Park0b0e1b92019-12-03 13:24:29 +0900255func (module *SdkLibrary) AndroidMkEntries() []android.AndroidMkEntries {
Paul Duffin43db9be2019-12-30 17:35:49 +0000256 if proptools.Bool(module.sdkLibraryProperties.Api_only) {
257 return nil
258 }
Jiyong Park0b0e1b92019-12-03 13:24:29 +0900259 entriesList := module.Library.AndroidMkEntries()
260 entries := &entriesList[0]
Jaewoong Jungb0c127c2019-08-29 14:56:03 -0700261 entries.Required = append(entries.Required, module.xmlFileName())
Sundong Ahn054b19a2018-10-19 13:46:09 +0900262
Jaewoong Jungb0c127c2019-08-29 14:56:03 -0700263 entries.ExtraFooters = []android.AndroidMkExtraFootersFunc{
264 func(w io.Writer, name, prefix, moduleDir string, entries *android.AndroidMkEntries) {
Jaewoong Jungb0c127c2019-08-29 14:56:03 -0700265 if !Bool(module.sdkLibraryProperties.No_dist) {
266 // Create a phony module that installs the impl library, for the case when this lib is
267 // in PRODUCT_PACKAGES.
268 owner := module.ModuleBase.Owner()
269 if owner == "" {
270 if Bool(module.sdkLibraryProperties.Core_lib) {
271 owner = "core"
272 } else {
273 owner = "android"
274 }
275 }
276 // Create dist rules to install the stubs libs to the dist dir
277 if len(module.publicApiStubsPath) == 1 {
278 fmt.Fprintln(w, "$(call dist-for-goals,sdk win_sdk,"+
279 module.publicApiStubsImplPath.Strings()[0]+
280 ":"+path.Join("apistubs", owner, "public",
281 module.BaseModuleName()+".jar")+")")
282 }
283 if len(module.systemApiStubsPath) == 1 {
284 fmt.Fprintln(w, "$(call dist-for-goals,sdk win_sdk,"+
285 module.systemApiStubsImplPath.Strings()[0]+
286 ":"+path.Join("apistubs", owner, "system",
287 module.BaseModuleName()+".jar")+")")
288 }
289 if len(module.testApiStubsPath) == 1 {
290 fmt.Fprintln(w, "$(call dist-for-goals,sdk win_sdk,"+
291 module.testApiStubsImplPath.Strings()[0]+
292 ":"+path.Join("apistubs", owner, "test",
293 module.BaseModuleName()+".jar")+")")
294 }
295 if module.publicApiFilePath != nil {
296 fmt.Fprintln(w, "$(call dist-for-goals,sdk win_sdk,"+
297 module.publicApiFilePath.String()+
298 ":"+path.Join("apistubs", owner, "public", "api",
299 module.BaseModuleName()+".txt")+")")
300 }
301 if module.systemApiFilePath != nil {
302 fmt.Fprintln(w, "$(call dist-for-goals,sdk win_sdk,"+
303 module.systemApiFilePath.String()+
304 ":"+path.Join("apistubs", owner, "system", "api",
305 module.BaseModuleName()+".txt")+")")
306 }
307 if module.testApiFilePath != nil {
308 fmt.Fprintln(w, "$(call dist-for-goals,sdk win_sdk,"+
309 module.testApiFilePath.String()+
310 ":"+path.Join("apistubs", owner, "test", "api",
311 module.BaseModuleName()+".txt")+")")
Sundong Ahn80a87b32019-05-13 15:02:50 +0900312 }
Sundong Ahn4fd04bb2018-08-31 18:01:37 +0900313 }
Jaewoong Jungb0c127c2019-08-29 14:56:03 -0700314 },
Jiyong Park82484c02018-04-23 21:41:26 +0900315 }
Jiyong Park0b0e1b92019-12-03 13:24:29 +0900316 return entriesList
Jiyong Park82484c02018-04-23 21:41:26 +0900317}
318
Jiyong Parkc678ad32018-04-10 13:07:10 +0900319// Module name of the stubs library
Inseob Kimc0907f12019-02-08 21:00:45 +0900320func (module *SdkLibrary) stubsName(apiScope apiScope) string {
Jiyong Parkc678ad32018-04-10 13:07:10 +0900321 stubsName := module.BaseModuleName() + sdkStubsLibrarySuffix
Jiyong Parkdf130542018-04-27 16:29:21 +0900322 switch apiScope {
323 case apiScopeSystem:
Jiyong Parkc678ad32018-04-10 13:07:10 +0900324 stubsName = stubsName + sdkSystemApiSuffix
Jiyong Parkdf130542018-04-27 16:29:21 +0900325 case apiScopeTest:
326 stubsName = stubsName + sdkTestApiSuffix
Jiyong Parkc678ad32018-04-10 13:07:10 +0900327 }
328 return stubsName
329}
330
331// Module name of the docs
Inseob Kimc0907f12019-02-08 21:00:45 +0900332func (module *SdkLibrary) docsName(apiScope apiScope) string {
Jiyong Parkc678ad32018-04-10 13:07:10 +0900333 docsName := module.BaseModuleName() + sdkDocsSuffix
Jiyong Parkdf130542018-04-27 16:29:21 +0900334 switch apiScope {
335 case apiScopeSystem:
Jiyong Parkc678ad32018-04-10 13:07:10 +0900336 docsName = docsName + sdkSystemApiSuffix
Jiyong Parkdf130542018-04-27 16:29:21 +0900337 case apiScopeTest:
338 docsName = docsName + sdkTestApiSuffix
Jiyong Parkc678ad32018-04-10 13:07:10 +0900339 }
340 return docsName
341}
342
343// Module name of the runtime implementation library
Inseob Kimc0907f12019-02-08 21:00:45 +0900344func (module *SdkLibrary) implName() string {
Sundong Ahn054b19a2018-10-19 13:46:09 +0900345 return module.BaseModuleName()
Jiyong Parkc678ad32018-04-10 13:07:10 +0900346}
347
348// File path to the runtime implementation library
Inseob Kimc0907f12019-02-08 21:00:45 +0900349func (module *SdkLibrary) implPath() string {
Jooyung Han58f26ab2019-12-18 15:34:32 +0900350 if apexName := module.ApexName(); apexName != "" {
351 // TODO(b/146468504): ApexName() is only a soong module name, not apex name.
352 // In most cases, this works fine. But when apex_name is set or override_apex is used
353 // this can be wrong.
354 return fmt.Sprintf("/apex/%s/javalib/%s.jar", apexName, module.implName())
355 }
Jiyong Parkc678ad32018-04-10 13:07:10 +0900356 partition := "system"
357 if module.SocSpecific() {
358 partition = "vendor"
359 } else if module.DeviceSpecific() {
360 partition = "odm"
361 } else if module.ProductSpecific() {
362 partition = "product"
Sundong Ahn0d7dff42019-12-04 12:53:44 +0900363 } else if module.SystemExtSpecific() {
364 partition = "system_ext"
Jiyong Parkc678ad32018-04-10 13:07:10 +0900365 }
366 return "/" + partition + "/framework/" + module.implName() + ".jar"
367}
368
369// Module name of the XML file for the lib
Inseob Kimc0907f12019-02-08 21:00:45 +0900370func (module *SdkLibrary) xmlFileName() string {
Jiyong Parkc678ad32018-04-10 13:07:10 +0900371 return module.BaseModuleName() + sdkXmlFileSuffix
372}
373
374// SDK version that the stubs library is built against. Note that this is always
375// *current. Older stubs library built with a numberd SDK version is created from
376// the prebuilt jar.
Paul Duffin12ceb462019-12-24 20:31:31 +0000377func (module *SdkLibrary) sdkVersionForScope(apiScope apiScope) string {
Jiyong Parkdf130542018-04-27 16:29:21 +0900378 switch apiScope {
379 case apiScopePublic:
380 return "current"
381 case apiScopeSystem:
Jiyong Parkc678ad32018-04-10 13:07:10 +0900382 return "system_current"
Jiyong Parkdf130542018-04-27 16:29:21 +0900383 case apiScopeTest:
384 return "test_current"
385 default:
Jiyong Parkc678ad32018-04-10 13:07:10 +0900386 return "current"
387 }
388}
389
Paul Duffin12ceb462019-12-24 20:31:31 +0000390// Get the sdk version for use when compiling the stubs library.
391func (module *SdkLibrary) sdkVersionForStubsLibrary(mctx android.BaseModuleContext, apiScope apiScope) string {
392 sdkDep := decodeSdkDep(mctx, sdkContext(&module.Library))
393 if sdkDep.hasStandardLibs() {
394 // If building against a standard sdk then use the sdk version appropriate for the scope.
395 return module.sdkVersionForScope(apiScope)
396 } else {
397 // Otherwise, use no system module.
398 return "none"
399 }
400}
401
Jiyong Parkc678ad32018-04-10 13:07:10 +0900402// $(INTERNAL_PLATFORM_<apiTagName>_API_FILE) points to the generated
403// api file for the current source
404// TODO: remove this when apicheck is done in soong
Inseob Kimc0907f12019-02-08 21:00:45 +0900405func (module *SdkLibrary) apiTagName(apiScope apiScope) string {
Jiyong Parkc678ad32018-04-10 13:07:10 +0900406 apiTagName := strings.Replace(strings.ToUpper(module.BaseModuleName()), ".", "_", -1)
Jiyong Parkdf130542018-04-27 16:29:21 +0900407 switch apiScope {
408 case apiScopeSystem:
Jiyong Parkc678ad32018-04-10 13:07:10 +0900409 apiTagName = apiTagName + "_SYSTEM"
Jiyong Parkdf130542018-04-27 16:29:21 +0900410 case apiScopeTest:
411 apiTagName = apiTagName + "_TEST"
Jiyong Parkc678ad32018-04-10 13:07:10 +0900412 }
413 return apiTagName
414}
415
Inseob Kimc0907f12019-02-08 21:00:45 +0900416func (module *SdkLibrary) latestApiFilegroupName(apiScope apiScope) string {
Jiyong Park58c518b2018-05-12 22:29:12 +0900417 name := ":" + module.BaseModuleName() + ".api."
Jiyong Parkdf130542018-04-27 16:29:21 +0900418 switch apiScope {
Jiyong Park58c518b2018-05-12 22:29:12 +0900419 case apiScopePublic:
420 name = name + "public"
Jiyong Parkdf130542018-04-27 16:29:21 +0900421 case apiScopeSystem:
Jiyong Park58c518b2018-05-12 22:29:12 +0900422 name = name + "system"
Jiyong Parkdf130542018-04-27 16:29:21 +0900423 case apiScopeTest:
Jiyong Park58c518b2018-05-12 22:29:12 +0900424 name = name + "test"
Jiyong Parkc678ad32018-04-10 13:07:10 +0900425 }
Jiyong Park58c518b2018-05-12 22:29:12 +0900426 name = name + ".latest"
427 return name
428}
Jiyong Parkc678ad32018-04-10 13:07:10 +0900429
Inseob Kimc0907f12019-02-08 21:00:45 +0900430func (module *SdkLibrary) latestRemovedApiFilegroupName(apiScope apiScope) string {
Jiyong Park58c518b2018-05-12 22:29:12 +0900431 name := ":" + module.BaseModuleName() + "-removed.api."
432 switch apiScope {
433 case apiScopePublic:
434 name = name + "public"
435 case apiScopeSystem:
436 name = name + "system"
437 case apiScopeTest:
438 name = name + "test"
439 }
440 name = name + ".latest"
441 return name
Jiyong Parkc678ad32018-04-10 13:07:10 +0900442}
443
444// Creates a static java library that has API stubs
Colin Crossf8b860a2019-04-16 14:43:28 -0700445func (module *SdkLibrary) createStubsLibrary(mctx android.LoadHookContext, apiScope apiScope) {
Jiyong Parkc678ad32018-04-10 13:07:10 +0900446 props := struct {
Sundong Ahn0d7dff42019-12-04 12:53:44 +0900447 Name *string
448 Srcs []string
449 Sdk_version *string
Paul Duffin12ceb462019-12-24 20:31:31 +0000450 System_modules *string
Sundong Ahn0d7dff42019-12-04 12:53:44 +0900451 Libs []string
452 Soc_specific *bool
453 Device_specific *bool
454 Product_specific *bool
455 System_ext_specific *bool
456 Compile_dex *bool
Sundong Ahn0d7dff42019-12-04 12:53:44 +0900457 Java_version *string
458 Product_variables struct {
Jiyong Parkc678ad32018-04-10 13:07:10 +0900459 Unbundled_build struct {
460 Enabled *bool
461 }
Jiyong Park82484c02018-04-23 21:41:26 +0900462 Pdk struct {
463 Enabled *bool
464 }
Jiyong Parkc678ad32018-04-10 13:07:10 +0900465 }
Sundong Ahn054b19a2018-10-19 13:46:09 +0900466 Openjdk9 struct {
467 Srcs []string
468 Javacflags []string
469 }
Jiyong Parkc678ad32018-04-10 13:07:10 +0900470 }{}
471
Jiyong Parkdf130542018-04-27 16:29:21 +0900472 props.Name = proptools.StringPtr(module.stubsName(apiScope))
Jiyong Parkc678ad32018-04-10 13:07:10 +0900473 // sources are generated from the droiddoc
Jiyong Parkdf130542018-04-27 16:29:21 +0900474 props.Srcs = []string{":" + module.docsName(apiScope)}
Paul Duffin12ceb462019-12-24 20:31:31 +0000475 sdkVersion := module.sdkVersionForStubsLibrary(mctx, apiScope)
Paul Duffin52d398a2019-06-11 12:31:14 +0100476 props.Sdk_version = proptools.StringPtr(sdkVersion)
Paul Duffin12ceb462019-12-24 20:31:31 +0000477 props.System_modules = module.Library.Module.deviceProperties.System_modules
Sundong Ahn054b19a2018-10-19 13:46:09 +0900478 props.Libs = module.sdkLibraryProperties.Stub_only_libs
Jiyong Parkc678ad32018-04-10 13:07:10 +0900479 // Unbundled apps will use the prebult one from /prebuilts/sdk
Colin Cross10932872019-04-18 14:27:12 -0700480 if mctx.Config().UnbundledBuildUsePrebuiltSdks() {
Colin Cross2c77ceb2019-01-21 11:56:21 -0800481 props.Product_variables.Unbundled_build.Enabled = proptools.BoolPtr(false)
482 }
Jiyong Park82484c02018-04-23 21:41:26 +0900483 props.Product_variables.Pdk.Enabled = proptools.BoolPtr(false)
Sundong Ahn054b19a2018-10-19 13:46:09 +0900484 props.Openjdk9.Srcs = module.Library.Module.properties.Openjdk9.Srcs
485 props.Openjdk9.Javacflags = module.Library.Module.properties.Openjdk9.Javacflags
486 props.Java_version = module.Library.Module.properties.Java_version
487 if module.Library.Module.deviceProperties.Compile_dex != nil {
488 props.Compile_dex = module.Library.Module.deviceProperties.Compile_dex
Sundong Ahndd567f92018-07-31 17:19:11 +0900489 }
Jiyong Parkc678ad32018-04-10 13:07:10 +0900490
491 if module.SocSpecific() {
492 props.Soc_specific = proptools.BoolPtr(true)
493 } else if module.DeviceSpecific() {
494 props.Device_specific = proptools.BoolPtr(true)
495 } else if module.ProductSpecific() {
496 props.Product_specific = proptools.BoolPtr(true)
Sundong Ahn0d7dff42019-12-04 12:53:44 +0900497 } else if module.SystemExtSpecific() {
498 props.System_ext_specific = proptools.BoolPtr(true)
Jiyong Parkc678ad32018-04-10 13:07:10 +0900499 }
500
Colin Cross84dfc3d2019-09-25 11:33:01 -0700501 mctx.CreateModule(LibraryFactory, &props)
Jiyong Parkc678ad32018-04-10 13:07:10 +0900502}
503
504// Creates a droiddoc module that creates stubs source files from the given full source
505// files
Paul Duffinc4cea762019-12-30 17:32:47 +0000506func (module *SdkLibrary) createStubsSources(mctx android.LoadHookContext, apiScope apiScope) {
Jiyong Parkc678ad32018-04-10 13:07:10 +0900507 props := struct {
Sundong Ahn054b19a2018-10-19 13:46:09 +0900508 Name *string
509 Srcs []string
510 Installable *bool
Paul Duffin52d398a2019-06-11 12:31:14 +0100511 Sdk_version *string
Paul Duffin12ceb462019-12-24 20:31:31 +0000512 System_modules *string
Sundong Ahn054b19a2018-10-19 13:46:09 +0900513 Libs []string
Paul Duffin11512472019-02-11 15:55:17 +0000514 Arg_files []string
Sundong Ahn054b19a2018-10-19 13:46:09 +0900515 Args *string
516 Api_tag_name *string
517 Api_filename *string
518 Removed_api_filename *string
Sundong Ahn054b19a2018-10-19 13:46:09 +0900519 Java_version *string
520 Merge_annotations_dirs []string
521 Merge_inclusion_annotations_dirs []string
522 Check_api struct {
Inseob Kim38449af2019-02-28 14:24:05 +0900523 Current ApiToCheck
524 Last_released ApiToCheck
525 Ignore_missing_latest_api *bool
Jiyong Park58c518b2018-05-12 22:29:12 +0900526 }
Sundong Ahn1b92c822018-05-29 11:35:17 +0900527 Aidl struct {
528 Include_dirs []string
529 Local_include_dirs []string
530 }
Jiyong Parkc678ad32018-04-10 13:07:10 +0900531 }{}
532
Paul Duffin250e6192019-06-07 10:44:37 +0100533 sdkDep := decodeSdkDep(mctx, sdkContext(&module.Library))
Paul Duffin12ceb462019-12-24 20:31:31 +0000534 // Use the platform API if standard libraries were requested, otherwise use
535 // no default libraries.
Paul Duffin52d398a2019-06-11 12:31:14 +0100536 sdkVersion := ""
537 if !sdkDep.hasStandardLibs() {
538 sdkVersion = "none"
539 }
Paul Duffin250e6192019-06-07 10:44:37 +0100540
Jiyong Parkdf130542018-04-27 16:29:21 +0900541 props.Name = proptools.StringPtr(module.docsName(apiScope))
Sundong Ahn054b19a2018-10-19 13:46:09 +0900542 props.Srcs = append(props.Srcs, module.Library.Module.properties.Srcs...)
Paul Duffin52d398a2019-06-11 12:31:14 +0100543 props.Sdk_version = proptools.StringPtr(sdkVersion)
Paul Duffin12ceb462019-12-24 20:31:31 +0000544 props.System_modules = module.Library.Module.deviceProperties.System_modules
Jiyong Parkc678ad32018-04-10 13:07:10 +0900545 props.Installable = proptools.BoolPtr(false)
Sundong Ahne6f0b052018-06-05 16:46:14 +0900546 // A droiddoc module has only one Libs property and doesn't distinguish between
547 // shared libs and static libs. So we need to add both of these libs to Libs property.
Sundong Ahn054b19a2018-10-19 13:46:09 +0900548 props.Libs = module.Library.Module.properties.Libs
549 props.Libs = append(props.Libs, module.Library.Module.properties.Static_libs...)
550 props.Aidl.Include_dirs = module.Library.Module.deviceProperties.Aidl.Include_dirs
551 props.Aidl.Local_include_dirs = module.Library.Module.deviceProperties.Aidl.Local_include_dirs
Sundong Ahn054b19a2018-10-19 13:46:09 +0900552 props.Java_version = module.Library.Module.properties.Java_version
Jiyong Parkc678ad32018-04-10 13:07:10 +0900553
Sundong Ahn054b19a2018-10-19 13:46:09 +0900554 props.Merge_annotations_dirs = module.sdkLibraryProperties.Merge_annotations_dirs
555 props.Merge_inclusion_annotations_dirs = module.sdkLibraryProperties.Merge_inclusion_annotations_dirs
556
Paul Duffin235ffff2019-12-24 10:41:30 +0000557 droiddocArgs := []string{}
558 if len(module.sdkLibraryProperties.Api_packages) != 0 {
559 droiddocArgs = append(droiddocArgs, "--stub-packages "+strings.Join(module.sdkLibraryProperties.Api_packages, ":"))
560 }
561 if len(module.sdkLibraryProperties.Hidden_api_packages) != 0 {
562 droiddocArgs = append(droiddocArgs,
563 android.JoinWithPrefix(module.sdkLibraryProperties.Hidden_api_packages, " --hide-package "))
564 }
565 droiddocArgs = append(droiddocArgs, module.sdkLibraryProperties.Droiddoc_options...)
566 disabledWarnings := []string{
567 "MissingPermission",
568 "BroadcastBehavior",
569 "HiddenSuperclass",
570 "DeprecationMismatch",
571 "UnavailableSymbol",
572 "SdkConstant",
573 "HiddenTypeParameter",
574 "Todo",
575 "Typo",
576 }
577 droiddocArgs = append(droiddocArgs, android.JoinWithPrefix(disabledWarnings, "--hide "))
Sundong Ahnfb2721f2018-09-17 13:23:09 +0900578
Jiyong Parkdf130542018-04-27 16:29:21 +0900579 switch apiScope {
580 case apiScopeSystem:
Paul Duffin235ffff2019-12-24 10:41:30 +0000581 droiddocArgs = append(droiddocArgs, "-showAnnotation android.annotation.SystemApi")
Jiyong Parkdf130542018-04-27 16:29:21 +0900582 case apiScopeTest:
Paul Duffin235ffff2019-12-24 10:41:30 +0000583 droiddocArgs = append(droiddocArgs, " -showAnnotation android.annotation.TestApi")
Jiyong Parkc678ad32018-04-10 13:07:10 +0900584 }
Paul Duffin11512472019-02-11 15:55:17 +0000585 props.Arg_files = module.sdkLibraryProperties.Droiddoc_option_files
Paul Duffin235ffff2019-12-24 10:41:30 +0000586 props.Args = proptools.StringPtr(strings.Join(droiddocArgs, " "))
Jiyong Parkc678ad32018-04-10 13:07:10 +0900587
588 // List of APIs identified from the provided source files are created. They are later
589 // compared against to the not-yet-released (a.k.a current) list of APIs and to the
590 // last-released (a.k.a numbered) list of API.
Jiyong Parkc678ad32018-04-10 13:07:10 +0900591 currentApiFileName := "current.txt"
592 removedApiFileName := "removed.txt"
Jiyong Parkdf130542018-04-27 16:29:21 +0900593 switch apiScope {
594 case apiScopeSystem:
Jiyong Parkc678ad32018-04-10 13:07:10 +0900595 currentApiFileName = "system-" + currentApiFileName
596 removedApiFileName = "system-" + removedApiFileName
Jiyong Parkdf130542018-04-27 16:29:21 +0900597 case apiScopeTest:
598 currentApiFileName = "test-" + currentApiFileName
599 removedApiFileName = "test-" + removedApiFileName
Jiyong Parkc678ad32018-04-10 13:07:10 +0900600 }
Paul Duffin749f98f2019-12-30 17:23:46 +0000601 apiDir := module.getApiDir()
602 currentApiFileName = path.Join(apiDir, currentApiFileName)
603 removedApiFileName = path.Join(apiDir, removedApiFileName)
Jiyong Park58c518b2018-05-12 22:29:12 +0900604 // TODO(jiyong): remove these three props
Jiyong Parkdf130542018-04-27 16:29:21 +0900605 props.Api_tag_name = proptools.StringPtr(module.apiTagName(apiScope))
Jiyong Parkc678ad32018-04-10 13:07:10 +0900606 props.Api_filename = proptools.StringPtr(currentApiFileName)
607 props.Removed_api_filename = proptools.StringPtr(removedApiFileName)
608
Jiyong Park58c518b2018-05-12 22:29:12 +0900609 // check against the not-yet-release API
610 props.Check_api.Current.Api_file = proptools.StringPtr(currentApiFileName)
611 props.Check_api.Current.Removed_api_file = proptools.StringPtr(removedApiFileName)
Jiyong Park58c518b2018-05-12 22:29:12 +0900612
613 // check against the latest released API
614 props.Check_api.Last_released.Api_file = proptools.StringPtr(
615 module.latestApiFilegroupName(apiScope))
616 props.Check_api.Last_released.Removed_api_file = proptools.StringPtr(
617 module.latestRemovedApiFilegroupName(apiScope))
Inseob Kim38449af2019-02-28 14:24:05 +0900618 props.Check_api.Ignore_missing_latest_api = proptools.BoolPtr(true)
Jiyong Park58c518b2018-05-12 22:29:12 +0900619
Colin Cross84dfc3d2019-09-25 11:33:01 -0700620 mctx.CreateModule(DroidstubsFactory, &props)
Jiyong Parkc678ad32018-04-10 13:07:10 +0900621}
622
Jiyong Parkc678ad32018-04-10 13:07:10 +0900623// Creates the xml file that publicizes the runtime library
Colin Crossf8b860a2019-04-16 14:43:28 -0700624func (module *SdkLibrary) createXmlFile(mctx android.LoadHookContext) {
Jiyong Parkc678ad32018-04-10 13:07:10 +0900625 // creates a prebuilt_etc module to actually place the xml file under
626 // <partition>/etc/permissions
627 etcProps := struct {
Sundong Ahn0d7dff42019-12-04 12:53:44 +0900628 Name *string
629 Src *string
630 Sub_dir *string
631 Soc_specific *bool
632 Device_specific *bool
633 Product_specific *bool
634 System_ext_specific *bool
Jiyong Parkc678ad32018-04-10 13:07:10 +0900635 }{}
636 etcProps.Name = proptools.StringPtr(module.xmlFileName())
Jooyung Han624058e2019-12-24 18:38:06 +0900637 etcProps.Src = proptools.StringPtr(":" + module.BaseModuleName() + "{.xml}")
Jiyong Parkc678ad32018-04-10 13:07:10 +0900638 etcProps.Sub_dir = proptools.StringPtr("permissions")
639 if module.SocSpecific() {
640 etcProps.Soc_specific = proptools.BoolPtr(true)
641 } else if module.DeviceSpecific() {
642 etcProps.Device_specific = proptools.BoolPtr(true)
643 } else if module.ProductSpecific() {
644 etcProps.Product_specific = proptools.BoolPtr(true)
Sundong Ahn0d7dff42019-12-04 12:53:44 +0900645 } else if module.SystemExtSpecific() {
646 etcProps.System_ext_specific = proptools.BoolPtr(true)
Jiyong Parkc678ad32018-04-10 13:07:10 +0900647 }
Colin Cross84dfc3d2019-09-25 11:33:01 -0700648 mctx.CreateModule(android.PrebuiltEtcFactory, &etcProps)
Jiyong Parkc678ad32018-04-10 13:07:10 +0900649}
650
Colin Cross0ea8ba82019-06-06 14:33:29 -0700651func (module *SdkLibrary) PrebuiltJars(ctx android.BaseModuleContext, sdkVersion string) android.Paths {
Sundong Ahn054b19a2018-10-19 13:46:09 +0900652 var api, v string
Paul Duffin52d398a2019-06-11 12:31:14 +0100653 if sdkVersion == "" || sdkVersion == "none" {
Sundong Ahn054b19a2018-10-19 13:46:09 +0900654 api = "system"
655 v = "current"
656 } else if strings.Contains(sdkVersion, "_") {
657 t := strings.Split(sdkVersion, "_")
658 api = t[0]
659 v = t[1]
Jiyong Parkc678ad32018-04-10 13:07:10 +0900660 } else {
Sundong Ahn054b19a2018-10-19 13:46:09 +0900661 api = "public"
662 v = sdkVersion
663 }
664 dir := filepath.Join("prebuilts", "sdk", v, api)
665 jar := filepath.Join(dir, module.BaseModuleName()+".jar")
666 jarPath := android.ExistentPathForSource(ctx, jar)
Sundong Ahnae418ac2019-02-28 15:01:28 +0900667 if !jarPath.Valid() {
668 ctx.PropertyErrorf("sdk_library", "invalid sdk version %q, %q does not exist", v, jar)
669 return nil
670 }
Sundong Ahn054b19a2018-10-19 13:46:09 +0900671 return android.Paths{jarPath.Path()}
672}
673
674// to satisfy SdkLibraryDependency interface
Colin Cross0ea8ba82019-06-06 14:33:29 -0700675func (module *SdkLibrary) SdkHeaderJars(ctx android.BaseModuleContext, sdkVersion string) android.Paths {
Sundong Ahn054b19a2018-10-19 13:46:09 +0900676 // This module is just a wrapper for the stubs.
Colin Cross10932872019-04-18 14:27:12 -0700677 if ctx.Config().UnbundledBuildUsePrebuiltSdks() {
Sundong Ahn054b19a2018-10-19 13:46:09 +0900678 return module.PrebuiltJars(ctx, sdkVersion)
679 } else {
680 if strings.HasPrefix(sdkVersion, "system_") {
681 return module.systemApiStubsPath
682 } else if sdkVersion == "" {
683 return module.Library.HeaderJars()
684 } else {
685 return module.publicApiStubsPath
686 }
Jiyong Parkc678ad32018-04-10 13:07:10 +0900687 }
688}
689
Sundong Ahn241cd372018-07-13 16:16:44 +0900690// to satisfy SdkLibraryDependency interface
Colin Cross0ea8ba82019-06-06 14:33:29 -0700691func (module *SdkLibrary) SdkImplementationJars(ctx android.BaseModuleContext, sdkVersion string) android.Paths {
Sundong Ahn241cd372018-07-13 16:16:44 +0900692 // This module is just a wrapper for the stubs.
Colin Cross10932872019-04-18 14:27:12 -0700693 if ctx.Config().UnbundledBuildUsePrebuiltSdks() {
Sundong Ahn054b19a2018-10-19 13:46:09 +0900694 return module.PrebuiltJars(ctx, sdkVersion)
Sundong Ahn241cd372018-07-13 16:16:44 +0900695 } else {
Sundong Ahn054b19a2018-10-19 13:46:09 +0900696 if strings.HasPrefix(sdkVersion, "system_") {
697 return module.systemApiStubsImplPath
698 } else if sdkVersion == "" {
699 return module.Library.ImplementationJars()
700 } else {
701 return module.publicApiStubsImplPath
702 }
Sundong Ahn241cd372018-07-13 16:16:44 +0900703 }
704}
705
Sundong Ahn80a87b32019-05-13 15:02:50 +0900706func (module *SdkLibrary) SetNoDist() {
707 module.sdkLibraryProperties.No_dist = proptools.BoolPtr(true)
708}
709
Colin Cross571cccf2019-02-04 11:22:08 -0800710var javaSdkLibrariesKey = android.NewOnceKey("javaSdkLibraries")
711
Jiyong Park82484c02018-04-23 21:41:26 +0900712func javaSdkLibraries(config android.Config) *[]string {
Colin Cross571cccf2019-02-04 11:22:08 -0800713 return config.Once(javaSdkLibrariesKey, func() interface{} {
Jiyong Park82484c02018-04-23 21:41:26 +0900714 return &[]string{}
715 }).(*[]string)
716}
717
Paul Duffin749f98f2019-12-30 17:23:46 +0000718func (module *SdkLibrary) getApiDir() string {
719 return proptools.StringDefault(module.sdkLibraryProperties.Api_dir, "api")
720}
721
Jiyong Parkc678ad32018-04-10 13:07:10 +0900722// For a java_sdk_library module, create internal modules for stubs, docs,
723// runtime libs and xml file. If requested, the stubs and docs are created twice
724// once for public API level and once for system API level
Colin Crossf8b860a2019-04-16 14:43:28 -0700725func (module *SdkLibrary) CreateInternalModules(mctx android.LoadHookContext) {
Inseob Kim6e93ac92019-03-21 17:43:49 +0900726 if len(module.Library.Module.properties.Srcs) == 0 {
Inseob Kimc0907f12019-02-08 21:00:45 +0900727 mctx.PropertyErrorf("srcs", "java_sdk_library must specify srcs")
Jooyung Han58f26ab2019-12-18 15:34:32 +0900728 return
Inseob Kimc0907f12019-02-08 21:00:45 +0900729 }
730
Paul Duffin37e0b772019-12-30 17:20:10 +0000731 // If this builds against standard libraries (i.e. is not part of the core libraries)
732 // then assume it provides both system and test apis. Otherwise, assume it does not and
733 // also assume it does not contribute to the dist build.
734 sdkDep := decodeSdkDep(mctx, sdkContext(&module.Library))
735 hasSystemAndTestApis := sdkDep.hasStandardLibs()
736 module.sdkLibraryProperties.Has_system_and_test_apis = hasSystemAndTestApis
737 module.sdkLibraryProperties.No_dist = proptools.BoolPtr(!hasSystemAndTestApis)
738
739 scopes := []string{""}
740 if hasSystemAndTestApis {
741 scopes = append(scopes, "system-", "test-")
742 }
743
Inseob Kim8098faa2019-03-18 10:19:51 +0900744 missing_current_api := false
745
Paul Duffin749f98f2019-12-30 17:23:46 +0000746 apiDir := module.getApiDir()
Paul Duffin37e0b772019-12-30 17:20:10 +0000747 for _, scope := range scopes {
Inseob Kim8098faa2019-03-18 10:19:51 +0900748 for _, api := range []string{"current.txt", "removed.txt"} {
Paul Duffin749f98f2019-12-30 17:23:46 +0000749 path := path.Join(mctx.ModuleDir(), apiDir, scope+api)
Inseob Kim8098faa2019-03-18 10:19:51 +0900750 p := android.ExistentPathForSource(mctx, path)
751 if !p.Valid() {
752 mctx.ModuleErrorf("Current api file %#v doesn't exist", path)
753 missing_current_api = true
754 }
755 }
756 }
757
758 if missing_current_api {
759 script := "build/soong/scripts/gen-java-current-api-files.sh"
760 p := android.ExistentPathForSource(mctx, script)
761
762 if !p.Valid() {
763 panic(fmt.Sprintf("script file %s doesn't exist", script))
764 }
765
766 mctx.ModuleErrorf("One or more current api files are missing. "+
767 "You can update them by:\n"+
Paul Duffin37e0b772019-12-30 17:20:10 +0000768 "%s %q %s && m update-api",
Paul Duffin749f98f2019-12-30 17:23:46 +0000769 script, filepath.Join(mctx.ModuleDir(), apiDir), strings.Join(scopes, " "))
Inseob Kim8098faa2019-03-18 10:19:51 +0900770 return
771 }
772
Inseob Kimc0907f12019-02-08 21:00:45 +0900773 // for public API stubs
774 module.createStubsLibrary(mctx, apiScopePublic)
Paul Duffinc4cea762019-12-30 17:32:47 +0000775 module.createStubsSources(mctx, apiScopePublic)
Inseob Kimc0907f12019-02-08 21:00:45 +0900776
Paul Duffin37e0b772019-12-30 17:20:10 +0000777 if hasSystemAndTestApis {
Inseob Kimc0907f12019-02-08 21:00:45 +0900778 // for system API stubs
779 module.createStubsLibrary(mctx, apiScopeSystem)
Paul Duffinc4cea762019-12-30 17:32:47 +0000780 module.createStubsSources(mctx, apiScopeSystem)
Inseob Kimc0907f12019-02-08 21:00:45 +0900781
782 // for test API stubs
783 module.createStubsLibrary(mctx, apiScopeTest)
Paul Duffinc4cea762019-12-30 17:32:47 +0000784 module.createStubsSources(mctx, apiScopeTest)
Inseob Kimc0907f12019-02-08 21:00:45 +0900785 }
786
Paul Duffin43db9be2019-12-30 17:35:49 +0000787 if !proptools.Bool(module.sdkLibraryProperties.Api_only) {
788 // for runtime
789 module.createXmlFile(mctx)
790
791 // record java_sdk_library modules so that they are exported to make
792 javaSdkLibraries := javaSdkLibraries(mctx.Config())
793 javaSdkLibrariesLock.Lock()
794 defer javaSdkLibrariesLock.Unlock()
795 *javaSdkLibraries = append(*javaSdkLibraries, module.BaseModuleName())
796 }
Inseob Kimc0907f12019-02-08 21:00:45 +0900797}
798
799func (module *SdkLibrary) InitSdkLibraryProperties() {
Sundong Ahn054b19a2018-10-19 13:46:09 +0900800 module.AddProperties(
801 &module.sdkLibraryProperties,
802 &module.Library.Module.properties,
803 &module.Library.Module.dexpreoptProperties,
804 &module.Library.Module.deviceProperties,
805 &module.Library.Module.protoProperties,
806 )
807
808 module.Library.Module.properties.Installable = proptools.BoolPtr(true)
809 module.Library.Module.deviceProperties.IsSDKLibrary = true
Inseob Kimc0907f12019-02-08 21:00:45 +0900810}
Sundong Ahn054b19a2018-10-19 13:46:09 +0900811
Jaewoong Jung4f158ee2019-07-11 10:05:35 -0700812// java_sdk_library is a special Java library that provides optional platform APIs to apps.
813// In practice, it can be viewed as a combination of several modules: 1) stubs library that clients
814// are linked against to, 2) droiddoc module that internally generates API stubs source files,
815// 3) the real runtime shared library that implements the APIs, and 4) XML file for adding
816// the runtime lib to the classpath at runtime if requested via <uses-library>.
Inseob Kimc0907f12019-02-08 21:00:45 +0900817func SdkLibraryFactory() android.Module {
818 module := &SdkLibrary{}
819 module.InitSdkLibraryProperties()
Jooyung Han58f26ab2019-12-18 15:34:32 +0900820 android.InitApexModule(module)
Sundong Ahn054b19a2018-10-19 13:46:09 +0900821 InitJavaModule(module, android.HostAndDeviceSupported)
Colin Crossf8b860a2019-04-16 14:43:28 -0700822 android.AddLoadHook(module, func(ctx android.LoadHookContext) { module.CreateInternalModules(ctx) })
Jiyong Parkc678ad32018-04-10 13:07:10 +0900823 return module
824}
Colin Cross79c7c262019-04-17 11:11:46 -0700825
826//
827// SDK library prebuilts
828//
829
830type sdkLibraryImportProperties struct {
831 Jars []string `android:"path"`
832
833 Sdk_version *string
834
835 Installable *bool
836
837 // List of shared java libs that this module has dependencies to
838 Libs []string
839
840 // List of files to remove from the jar file(s)
841 Exclude_files []string
842
843 // List of directories to remove from the jar file(s)
844 Exclude_dirs []string
845}
846
847type sdkLibraryImport struct {
848 android.ModuleBase
849 android.DefaultableModuleBase
850 prebuilt android.Prebuilt
851
852 properties sdkLibraryImportProperties
853
854 stubsPath android.Paths
855}
856
857var _ SdkLibraryDependency = (*sdkLibraryImport)(nil)
858
Jaewoong Jung4f158ee2019-07-11 10:05:35 -0700859// java_sdk_library_import imports a prebuilt java_sdk_library.
Colin Cross79c7c262019-04-17 11:11:46 -0700860func sdkLibraryImportFactory() android.Module {
861 module := &sdkLibraryImport{}
862
863 module.AddProperties(&module.properties)
864
865 android.InitPrebuiltModule(module, &module.properties.Jars)
866 InitJavaModule(module, android.HostAndDeviceSupported)
867
868 android.AddLoadHook(module, func(mctx android.LoadHookContext) { module.createInternalModules(mctx) })
869 return module
870}
871
872func (module *sdkLibraryImport) Prebuilt() *android.Prebuilt {
873 return &module.prebuilt
874}
875
876func (module *sdkLibraryImport) Name() string {
877 return module.prebuilt.Name(module.ModuleBase.Name())
878}
879
880func (module *sdkLibraryImport) createInternalModules(mctx android.LoadHookContext) {
881 // Creates a java import for the jar with ".stubs" suffix
882 props := struct {
Sundong Ahn0d7dff42019-12-04 12:53:44 +0900883 Name *string
884 Soc_specific *bool
885 Device_specific *bool
886 Product_specific *bool
887 System_ext_specific *bool
Colin Cross79c7c262019-04-17 11:11:46 -0700888 }{}
889
890 props.Name = proptools.StringPtr(module.BaseModuleName() + sdkStubsLibrarySuffix)
891
892 if module.SocSpecific() {
893 props.Soc_specific = proptools.BoolPtr(true)
894 } else if module.DeviceSpecific() {
895 props.Device_specific = proptools.BoolPtr(true)
896 } else if module.ProductSpecific() {
897 props.Product_specific = proptools.BoolPtr(true)
Sundong Ahn0d7dff42019-12-04 12:53:44 +0900898 } else if module.SystemExtSpecific() {
899 props.System_ext_specific = proptools.BoolPtr(true)
Colin Cross79c7c262019-04-17 11:11:46 -0700900 }
901
Colin Cross84dfc3d2019-09-25 11:33:01 -0700902 mctx.CreateModule(ImportFactory, &props, &module.properties)
Colin Cross79c7c262019-04-17 11:11:46 -0700903
904 javaSdkLibraries := javaSdkLibraries(mctx.Config())
905 javaSdkLibrariesLock.Lock()
906 defer javaSdkLibrariesLock.Unlock()
907 *javaSdkLibraries = append(*javaSdkLibraries, module.BaseModuleName())
908}
909
910func (module *sdkLibraryImport) DepsMutator(ctx android.BottomUpMutatorContext) {
911 // Add dependencies to the prebuilt stubs library
912 ctx.AddVariationDependencies(nil, publicApiStubsTag, module.BaseModuleName()+sdkStubsLibrarySuffix)
913}
914
915func (module *sdkLibraryImport) GenerateAndroidBuildActions(ctx android.ModuleContext) {
916 // Record the paths to the prebuilt stubs library.
917 ctx.VisitDirectDeps(func(to android.Module) {
918 tag := ctx.OtherModuleDependencyTag(to)
919
920 switch tag {
921 case publicApiStubsTag:
922 module.stubsPath = to.(Dependency).HeaderJars()
923 }
924 })
925}
926
927// to satisfy SdkLibraryDependency interface
Colin Cross0ea8ba82019-06-06 14:33:29 -0700928func (module *sdkLibraryImport) SdkHeaderJars(ctx android.BaseModuleContext, sdkVersion string) android.Paths {
Colin Cross79c7c262019-04-17 11:11:46 -0700929 // This module is just a wrapper for the prebuilt stubs.
930 return module.stubsPath
931}
932
933// to satisfy SdkLibraryDependency interface
Colin Cross0ea8ba82019-06-06 14:33:29 -0700934func (module *sdkLibraryImport) SdkImplementationJars(ctx android.BaseModuleContext, sdkVersion string) android.Paths {
Colin Cross79c7c262019-04-17 11:11:46 -0700935 // This module is just a wrapper for the stubs.
936 return module.stubsPath
937}