blob: 9b30e2ca4428ff05bf0d916e06233b77a52fa5d0 [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.
Colin Cross1184b642019-12-30 18:43:07 -0800391func (module *SdkLibrary) sdkVersionForStubsLibrary(mctx android.LoadHookContext, apiScope apiScope) string {
Paul Duffin12ceb462019-12-24 20:31:31 +0000392 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
Paul Duffin367ab912019-12-23 19:40:36 +0000449 Installable *bool
Sundong Ahn0d7dff42019-12-04 12:53:44 +0900450 Sdk_version *string
Paul Duffin12ceb462019-12-24 20:31:31 +0000451 System_modules *string
Sundong Ahn0d7dff42019-12-04 12:53:44 +0900452 Libs []string
453 Soc_specific *bool
454 Device_specific *bool
455 Product_specific *bool
456 System_ext_specific *bool
457 Compile_dex *bool
Sundong Ahn0d7dff42019-12-04 12:53:44 +0900458 Java_version *string
459 Product_variables struct {
Jiyong Parkc678ad32018-04-10 13:07:10 +0900460 Unbundled_build struct {
461 Enabled *bool
462 }
Jiyong Park82484c02018-04-23 21:41:26 +0900463 Pdk struct {
464 Enabled *bool
465 }
Jiyong Parkc678ad32018-04-10 13:07:10 +0900466 }
Sundong Ahn054b19a2018-10-19 13:46:09 +0900467 Openjdk9 struct {
468 Srcs []string
469 Javacflags []string
470 }
Jiyong Parkc678ad32018-04-10 13:07:10 +0900471 }{}
472
Jiyong Parkdf130542018-04-27 16:29:21 +0900473 props.Name = proptools.StringPtr(module.stubsName(apiScope))
Jiyong Parkc678ad32018-04-10 13:07:10 +0900474 // sources are generated from the droiddoc
Jiyong Parkdf130542018-04-27 16:29:21 +0900475 props.Srcs = []string{":" + module.docsName(apiScope)}
Paul Duffin12ceb462019-12-24 20:31:31 +0000476 sdkVersion := module.sdkVersionForStubsLibrary(mctx, apiScope)
Paul Duffin52d398a2019-06-11 12:31:14 +0100477 props.Sdk_version = proptools.StringPtr(sdkVersion)
Paul Duffin12ceb462019-12-24 20:31:31 +0000478 props.System_modules = module.Library.Module.deviceProperties.System_modules
Paul Duffin367ab912019-12-23 19:40:36 +0000479 props.Installable = proptools.BoolPtr(false)
Sundong Ahn054b19a2018-10-19 13:46:09 +0900480 props.Libs = module.sdkLibraryProperties.Stub_only_libs
Jiyong Parkc678ad32018-04-10 13:07:10 +0900481 // Unbundled apps will use the prebult one from /prebuilts/sdk
Colin Cross10932872019-04-18 14:27:12 -0700482 if mctx.Config().UnbundledBuildUsePrebuiltSdks() {
Colin Cross2c77ceb2019-01-21 11:56:21 -0800483 props.Product_variables.Unbundled_build.Enabled = proptools.BoolPtr(false)
484 }
Jiyong Park82484c02018-04-23 21:41:26 +0900485 props.Product_variables.Pdk.Enabled = proptools.BoolPtr(false)
Sundong Ahn054b19a2018-10-19 13:46:09 +0900486 props.Openjdk9.Srcs = module.Library.Module.properties.Openjdk9.Srcs
487 props.Openjdk9.Javacflags = module.Library.Module.properties.Openjdk9.Javacflags
488 props.Java_version = module.Library.Module.properties.Java_version
489 if module.Library.Module.deviceProperties.Compile_dex != nil {
490 props.Compile_dex = module.Library.Module.deviceProperties.Compile_dex
Sundong Ahndd567f92018-07-31 17:19:11 +0900491 }
Jiyong Parkc678ad32018-04-10 13:07:10 +0900492
493 if module.SocSpecific() {
494 props.Soc_specific = proptools.BoolPtr(true)
495 } else if module.DeviceSpecific() {
496 props.Device_specific = proptools.BoolPtr(true)
497 } else if module.ProductSpecific() {
498 props.Product_specific = proptools.BoolPtr(true)
Sundong Ahn0d7dff42019-12-04 12:53:44 +0900499 } else if module.SystemExtSpecific() {
500 props.System_ext_specific = proptools.BoolPtr(true)
Jiyong Parkc678ad32018-04-10 13:07:10 +0900501 }
502
Colin Cross84dfc3d2019-09-25 11:33:01 -0700503 mctx.CreateModule(LibraryFactory, &props)
Jiyong Parkc678ad32018-04-10 13:07:10 +0900504}
505
506// Creates a droiddoc module that creates stubs source files from the given full source
507// files
Paul Duffinc4cea762019-12-30 17:32:47 +0000508func (module *SdkLibrary) createStubsSources(mctx android.LoadHookContext, apiScope apiScope) {
Jiyong Parkc678ad32018-04-10 13:07:10 +0900509 props := struct {
Sundong Ahn054b19a2018-10-19 13:46:09 +0900510 Name *string
511 Srcs []string
512 Installable *bool
Paul Duffin52d398a2019-06-11 12:31:14 +0100513 Sdk_version *string
Paul Duffin12ceb462019-12-24 20:31:31 +0000514 System_modules *string
Sundong Ahn054b19a2018-10-19 13:46:09 +0900515 Libs []string
Paul Duffin11512472019-02-11 15:55:17 +0000516 Arg_files []string
Sundong Ahn054b19a2018-10-19 13:46:09 +0900517 Args *string
518 Api_tag_name *string
519 Api_filename *string
520 Removed_api_filename *string
Sundong Ahn054b19a2018-10-19 13:46:09 +0900521 Java_version *string
522 Merge_annotations_dirs []string
523 Merge_inclusion_annotations_dirs []string
524 Check_api struct {
Inseob Kim38449af2019-02-28 14:24:05 +0900525 Current ApiToCheck
526 Last_released ApiToCheck
527 Ignore_missing_latest_api *bool
Jiyong Park58c518b2018-05-12 22:29:12 +0900528 }
Sundong Ahn1b92c822018-05-29 11:35:17 +0900529 Aidl struct {
530 Include_dirs []string
531 Local_include_dirs []string
532 }
Jiyong Parkc678ad32018-04-10 13:07:10 +0900533 }{}
534
Paul Duffin250e6192019-06-07 10:44:37 +0100535 sdkDep := decodeSdkDep(mctx, sdkContext(&module.Library))
Paul Duffin12ceb462019-12-24 20:31:31 +0000536 // Use the platform API if standard libraries were requested, otherwise use
537 // no default libraries.
Paul Duffin52d398a2019-06-11 12:31:14 +0100538 sdkVersion := ""
539 if !sdkDep.hasStandardLibs() {
540 sdkVersion = "none"
541 }
Paul Duffin250e6192019-06-07 10:44:37 +0100542
Jiyong Parkdf130542018-04-27 16:29:21 +0900543 props.Name = proptools.StringPtr(module.docsName(apiScope))
Sundong Ahn054b19a2018-10-19 13:46:09 +0900544 props.Srcs = append(props.Srcs, module.Library.Module.properties.Srcs...)
Paul Duffin52d398a2019-06-11 12:31:14 +0100545 props.Sdk_version = proptools.StringPtr(sdkVersion)
Paul Duffin12ceb462019-12-24 20:31:31 +0000546 props.System_modules = module.Library.Module.deviceProperties.System_modules
Jiyong Parkc678ad32018-04-10 13:07:10 +0900547 props.Installable = proptools.BoolPtr(false)
Sundong Ahne6f0b052018-06-05 16:46:14 +0900548 // A droiddoc module has only one Libs property and doesn't distinguish between
549 // shared libs and static libs. So we need to add both of these libs to Libs property.
Sundong Ahn054b19a2018-10-19 13:46:09 +0900550 props.Libs = module.Library.Module.properties.Libs
551 props.Libs = append(props.Libs, module.Library.Module.properties.Static_libs...)
552 props.Aidl.Include_dirs = module.Library.Module.deviceProperties.Aidl.Include_dirs
553 props.Aidl.Local_include_dirs = module.Library.Module.deviceProperties.Aidl.Local_include_dirs
Sundong Ahn054b19a2018-10-19 13:46:09 +0900554 props.Java_version = module.Library.Module.properties.Java_version
Jiyong Parkc678ad32018-04-10 13:07:10 +0900555
Sundong Ahn054b19a2018-10-19 13:46:09 +0900556 props.Merge_annotations_dirs = module.sdkLibraryProperties.Merge_annotations_dirs
557 props.Merge_inclusion_annotations_dirs = module.sdkLibraryProperties.Merge_inclusion_annotations_dirs
558
Paul Duffin235ffff2019-12-24 10:41:30 +0000559 droiddocArgs := []string{}
560 if len(module.sdkLibraryProperties.Api_packages) != 0 {
561 droiddocArgs = append(droiddocArgs, "--stub-packages "+strings.Join(module.sdkLibraryProperties.Api_packages, ":"))
562 }
563 if len(module.sdkLibraryProperties.Hidden_api_packages) != 0 {
564 droiddocArgs = append(droiddocArgs,
565 android.JoinWithPrefix(module.sdkLibraryProperties.Hidden_api_packages, " --hide-package "))
566 }
567 droiddocArgs = append(droiddocArgs, module.sdkLibraryProperties.Droiddoc_options...)
568 disabledWarnings := []string{
569 "MissingPermission",
570 "BroadcastBehavior",
571 "HiddenSuperclass",
572 "DeprecationMismatch",
573 "UnavailableSymbol",
574 "SdkConstant",
575 "HiddenTypeParameter",
576 "Todo",
577 "Typo",
578 }
579 droiddocArgs = append(droiddocArgs, android.JoinWithPrefix(disabledWarnings, "--hide "))
Sundong Ahnfb2721f2018-09-17 13:23:09 +0900580
Jiyong Parkdf130542018-04-27 16:29:21 +0900581 switch apiScope {
582 case apiScopeSystem:
Paul Duffin235ffff2019-12-24 10:41:30 +0000583 droiddocArgs = append(droiddocArgs, "-showAnnotation android.annotation.SystemApi")
Jiyong Parkdf130542018-04-27 16:29:21 +0900584 case apiScopeTest:
Paul Duffin235ffff2019-12-24 10:41:30 +0000585 droiddocArgs = append(droiddocArgs, " -showAnnotation android.annotation.TestApi")
Jiyong Parkc678ad32018-04-10 13:07:10 +0900586 }
Paul Duffin11512472019-02-11 15:55:17 +0000587 props.Arg_files = module.sdkLibraryProperties.Droiddoc_option_files
Paul Duffin235ffff2019-12-24 10:41:30 +0000588 props.Args = proptools.StringPtr(strings.Join(droiddocArgs, " "))
Jiyong Parkc678ad32018-04-10 13:07:10 +0900589
590 // List of APIs identified from the provided source files are created. They are later
591 // compared against to the not-yet-released (a.k.a current) list of APIs and to the
592 // last-released (a.k.a numbered) list of API.
Jiyong Parkc678ad32018-04-10 13:07:10 +0900593 currentApiFileName := "current.txt"
594 removedApiFileName := "removed.txt"
Jiyong Parkdf130542018-04-27 16:29:21 +0900595 switch apiScope {
596 case apiScopeSystem:
Jiyong Parkc678ad32018-04-10 13:07:10 +0900597 currentApiFileName = "system-" + currentApiFileName
598 removedApiFileName = "system-" + removedApiFileName
Jiyong Parkdf130542018-04-27 16:29:21 +0900599 case apiScopeTest:
600 currentApiFileName = "test-" + currentApiFileName
601 removedApiFileName = "test-" + removedApiFileName
Jiyong Parkc678ad32018-04-10 13:07:10 +0900602 }
Paul Duffin749f98f2019-12-30 17:23:46 +0000603 apiDir := module.getApiDir()
604 currentApiFileName = path.Join(apiDir, currentApiFileName)
605 removedApiFileName = path.Join(apiDir, removedApiFileName)
Jiyong Park58c518b2018-05-12 22:29:12 +0900606 // TODO(jiyong): remove these three props
Jiyong Parkdf130542018-04-27 16:29:21 +0900607 props.Api_tag_name = proptools.StringPtr(module.apiTagName(apiScope))
Jiyong Parkc678ad32018-04-10 13:07:10 +0900608 props.Api_filename = proptools.StringPtr(currentApiFileName)
609 props.Removed_api_filename = proptools.StringPtr(removedApiFileName)
610
Jiyong Park58c518b2018-05-12 22:29:12 +0900611 // check against the not-yet-release API
612 props.Check_api.Current.Api_file = proptools.StringPtr(currentApiFileName)
613 props.Check_api.Current.Removed_api_file = proptools.StringPtr(removedApiFileName)
Jiyong Park58c518b2018-05-12 22:29:12 +0900614
615 // check against the latest released API
616 props.Check_api.Last_released.Api_file = proptools.StringPtr(
617 module.latestApiFilegroupName(apiScope))
618 props.Check_api.Last_released.Removed_api_file = proptools.StringPtr(
619 module.latestRemovedApiFilegroupName(apiScope))
Inseob Kim38449af2019-02-28 14:24:05 +0900620 props.Check_api.Ignore_missing_latest_api = proptools.BoolPtr(true)
Jiyong Park58c518b2018-05-12 22:29:12 +0900621
Colin Cross84dfc3d2019-09-25 11:33:01 -0700622 mctx.CreateModule(DroidstubsFactory, &props)
Jiyong Parkc678ad32018-04-10 13:07:10 +0900623}
624
Jiyong Parkc678ad32018-04-10 13:07:10 +0900625// Creates the xml file that publicizes the runtime library
Colin Crossf8b860a2019-04-16 14:43:28 -0700626func (module *SdkLibrary) createXmlFile(mctx android.LoadHookContext) {
Jiyong Parkc678ad32018-04-10 13:07:10 +0900627 // creates a prebuilt_etc module to actually place the xml file under
628 // <partition>/etc/permissions
629 etcProps := struct {
Sundong Ahn0d7dff42019-12-04 12:53:44 +0900630 Name *string
631 Src *string
632 Sub_dir *string
633 Soc_specific *bool
634 Device_specific *bool
635 Product_specific *bool
636 System_ext_specific *bool
Jiyong Parkc678ad32018-04-10 13:07:10 +0900637 }{}
638 etcProps.Name = proptools.StringPtr(module.xmlFileName())
Jooyung Han624058e2019-12-24 18:38:06 +0900639 etcProps.Src = proptools.StringPtr(":" + module.BaseModuleName() + "{.xml}")
Jiyong Parkc678ad32018-04-10 13:07:10 +0900640 etcProps.Sub_dir = proptools.StringPtr("permissions")
641 if module.SocSpecific() {
642 etcProps.Soc_specific = proptools.BoolPtr(true)
643 } else if module.DeviceSpecific() {
644 etcProps.Device_specific = proptools.BoolPtr(true)
645 } else if module.ProductSpecific() {
646 etcProps.Product_specific = proptools.BoolPtr(true)
Sundong Ahn0d7dff42019-12-04 12:53:44 +0900647 } else if module.SystemExtSpecific() {
648 etcProps.System_ext_specific = proptools.BoolPtr(true)
Jiyong Parkc678ad32018-04-10 13:07:10 +0900649 }
Colin Cross84dfc3d2019-09-25 11:33:01 -0700650 mctx.CreateModule(android.PrebuiltEtcFactory, &etcProps)
Jiyong Parkc678ad32018-04-10 13:07:10 +0900651}
652
Colin Cross0ea8ba82019-06-06 14:33:29 -0700653func (module *SdkLibrary) PrebuiltJars(ctx android.BaseModuleContext, sdkVersion string) android.Paths {
Sundong Ahn054b19a2018-10-19 13:46:09 +0900654 var api, v string
Paul Duffin52d398a2019-06-11 12:31:14 +0100655 if sdkVersion == "" || sdkVersion == "none" {
Sundong Ahn054b19a2018-10-19 13:46:09 +0900656 api = "system"
657 v = "current"
658 } else if strings.Contains(sdkVersion, "_") {
659 t := strings.Split(sdkVersion, "_")
660 api = t[0]
661 v = t[1]
Jiyong Parkc678ad32018-04-10 13:07:10 +0900662 } else {
Sundong Ahn054b19a2018-10-19 13:46:09 +0900663 api = "public"
664 v = sdkVersion
665 }
666 dir := filepath.Join("prebuilts", "sdk", v, api)
667 jar := filepath.Join(dir, module.BaseModuleName()+".jar")
668 jarPath := android.ExistentPathForSource(ctx, jar)
Sundong Ahnae418ac2019-02-28 15:01:28 +0900669 if !jarPath.Valid() {
Colin Cross07c88562020-01-07 09:34:44 -0800670 if ctx.Config().AllowMissingDependencies() {
671 return android.Paths{android.PathForSource(ctx, jar)}
672 } else {
673 ctx.PropertyErrorf("sdk_library", "invalid sdk version %q, %q does not exist", sdkVersion, jar)
674 }
Sundong Ahnae418ac2019-02-28 15:01:28 +0900675 return nil
676 }
Sundong Ahn054b19a2018-10-19 13:46:09 +0900677 return android.Paths{jarPath.Path()}
678}
679
680// to satisfy SdkLibraryDependency interface
Colin Cross0ea8ba82019-06-06 14:33:29 -0700681func (module *SdkLibrary) SdkHeaderJars(ctx android.BaseModuleContext, sdkVersion string) android.Paths {
Sundong Ahn054b19a2018-10-19 13:46:09 +0900682 // This module is just a wrapper for the stubs.
Colin Cross10932872019-04-18 14:27:12 -0700683 if ctx.Config().UnbundledBuildUsePrebuiltSdks() {
Sundong Ahn054b19a2018-10-19 13:46:09 +0900684 return module.PrebuiltJars(ctx, sdkVersion)
685 } else {
686 if strings.HasPrefix(sdkVersion, "system_") {
687 return module.systemApiStubsPath
688 } else if sdkVersion == "" {
689 return module.Library.HeaderJars()
690 } else {
691 return module.publicApiStubsPath
692 }
Jiyong Parkc678ad32018-04-10 13:07:10 +0900693 }
694}
695
Sundong Ahn241cd372018-07-13 16:16:44 +0900696// to satisfy SdkLibraryDependency interface
Colin Cross0ea8ba82019-06-06 14:33:29 -0700697func (module *SdkLibrary) SdkImplementationJars(ctx android.BaseModuleContext, sdkVersion string) android.Paths {
Sundong Ahn241cd372018-07-13 16:16:44 +0900698 // This module is just a wrapper for the stubs.
Colin Cross10932872019-04-18 14:27:12 -0700699 if ctx.Config().UnbundledBuildUsePrebuiltSdks() {
Sundong Ahn054b19a2018-10-19 13:46:09 +0900700 return module.PrebuiltJars(ctx, sdkVersion)
Sundong Ahn241cd372018-07-13 16:16:44 +0900701 } else {
Sundong Ahn054b19a2018-10-19 13:46:09 +0900702 if strings.HasPrefix(sdkVersion, "system_") {
703 return module.systemApiStubsImplPath
704 } else if sdkVersion == "" {
705 return module.Library.ImplementationJars()
706 } else {
707 return module.publicApiStubsImplPath
708 }
Sundong Ahn241cd372018-07-13 16:16:44 +0900709 }
710}
711
Sundong Ahn80a87b32019-05-13 15:02:50 +0900712func (module *SdkLibrary) SetNoDist() {
713 module.sdkLibraryProperties.No_dist = proptools.BoolPtr(true)
714}
715
Colin Cross571cccf2019-02-04 11:22:08 -0800716var javaSdkLibrariesKey = android.NewOnceKey("javaSdkLibraries")
717
Jiyong Park82484c02018-04-23 21:41:26 +0900718func javaSdkLibraries(config android.Config) *[]string {
Colin Cross571cccf2019-02-04 11:22:08 -0800719 return config.Once(javaSdkLibrariesKey, func() interface{} {
Jiyong Park82484c02018-04-23 21:41:26 +0900720 return &[]string{}
721 }).(*[]string)
722}
723
Paul Duffin749f98f2019-12-30 17:23:46 +0000724func (module *SdkLibrary) getApiDir() string {
725 return proptools.StringDefault(module.sdkLibraryProperties.Api_dir, "api")
726}
727
Jiyong Parkc678ad32018-04-10 13:07:10 +0900728// For a java_sdk_library module, create internal modules for stubs, docs,
729// runtime libs and xml file. If requested, the stubs and docs are created twice
730// once for public API level and once for system API level
Colin Crossf8b860a2019-04-16 14:43:28 -0700731func (module *SdkLibrary) CreateInternalModules(mctx android.LoadHookContext) {
Inseob Kim6e93ac92019-03-21 17:43:49 +0900732 if len(module.Library.Module.properties.Srcs) == 0 {
Inseob Kimc0907f12019-02-08 21:00:45 +0900733 mctx.PropertyErrorf("srcs", "java_sdk_library must specify srcs")
Jooyung Han58f26ab2019-12-18 15:34:32 +0900734 return
Inseob Kimc0907f12019-02-08 21:00:45 +0900735 }
736
Paul Duffin37e0b772019-12-30 17:20:10 +0000737 // If this builds against standard libraries (i.e. is not part of the core libraries)
738 // then assume it provides both system and test apis. Otherwise, assume it does not and
739 // also assume it does not contribute to the dist build.
740 sdkDep := decodeSdkDep(mctx, sdkContext(&module.Library))
741 hasSystemAndTestApis := sdkDep.hasStandardLibs()
742 module.sdkLibraryProperties.Has_system_and_test_apis = hasSystemAndTestApis
743 module.sdkLibraryProperties.No_dist = proptools.BoolPtr(!hasSystemAndTestApis)
744
745 scopes := []string{""}
746 if hasSystemAndTestApis {
747 scopes = append(scopes, "system-", "test-")
748 }
749
Inseob Kim8098faa2019-03-18 10:19:51 +0900750 missing_current_api := false
751
Paul Duffin749f98f2019-12-30 17:23:46 +0000752 apiDir := module.getApiDir()
Paul Duffin37e0b772019-12-30 17:20:10 +0000753 for _, scope := range scopes {
Inseob Kim8098faa2019-03-18 10:19:51 +0900754 for _, api := range []string{"current.txt", "removed.txt"} {
Paul Duffin749f98f2019-12-30 17:23:46 +0000755 path := path.Join(mctx.ModuleDir(), apiDir, scope+api)
Inseob Kim8098faa2019-03-18 10:19:51 +0900756 p := android.ExistentPathForSource(mctx, path)
757 if !p.Valid() {
758 mctx.ModuleErrorf("Current api file %#v doesn't exist", path)
759 missing_current_api = true
760 }
761 }
762 }
763
764 if missing_current_api {
765 script := "build/soong/scripts/gen-java-current-api-files.sh"
766 p := android.ExistentPathForSource(mctx, script)
767
768 if !p.Valid() {
769 panic(fmt.Sprintf("script file %s doesn't exist", script))
770 }
771
772 mctx.ModuleErrorf("One or more current api files are missing. "+
773 "You can update them by:\n"+
Paul Duffin37e0b772019-12-30 17:20:10 +0000774 "%s %q %s && m update-api",
Paul Duffin749f98f2019-12-30 17:23:46 +0000775 script, filepath.Join(mctx.ModuleDir(), apiDir), strings.Join(scopes, " "))
Inseob Kim8098faa2019-03-18 10:19:51 +0900776 return
777 }
778
Inseob Kimc0907f12019-02-08 21:00:45 +0900779 // for public API stubs
780 module.createStubsLibrary(mctx, apiScopePublic)
Paul Duffinc4cea762019-12-30 17:32:47 +0000781 module.createStubsSources(mctx, apiScopePublic)
Inseob Kimc0907f12019-02-08 21:00:45 +0900782
Paul Duffin37e0b772019-12-30 17:20:10 +0000783 if hasSystemAndTestApis {
Inseob Kimc0907f12019-02-08 21:00:45 +0900784 // for system API stubs
785 module.createStubsLibrary(mctx, apiScopeSystem)
Paul Duffinc4cea762019-12-30 17:32:47 +0000786 module.createStubsSources(mctx, apiScopeSystem)
Inseob Kimc0907f12019-02-08 21:00:45 +0900787
788 // for test API stubs
789 module.createStubsLibrary(mctx, apiScopeTest)
Paul Duffinc4cea762019-12-30 17:32:47 +0000790 module.createStubsSources(mctx, apiScopeTest)
Inseob Kimc0907f12019-02-08 21:00:45 +0900791 }
792
Paul Duffin43db9be2019-12-30 17:35:49 +0000793 if !proptools.Bool(module.sdkLibraryProperties.Api_only) {
794 // for runtime
795 module.createXmlFile(mctx)
796
797 // record java_sdk_library modules so that they are exported to make
798 javaSdkLibraries := javaSdkLibraries(mctx.Config())
799 javaSdkLibrariesLock.Lock()
800 defer javaSdkLibrariesLock.Unlock()
801 *javaSdkLibraries = append(*javaSdkLibraries, module.BaseModuleName())
802 }
Inseob Kimc0907f12019-02-08 21:00:45 +0900803}
804
805func (module *SdkLibrary) InitSdkLibraryProperties() {
Sundong Ahn054b19a2018-10-19 13:46:09 +0900806 module.AddProperties(
807 &module.sdkLibraryProperties,
808 &module.Library.Module.properties,
809 &module.Library.Module.dexpreoptProperties,
810 &module.Library.Module.deviceProperties,
811 &module.Library.Module.protoProperties,
812 )
813
814 module.Library.Module.properties.Installable = proptools.BoolPtr(true)
815 module.Library.Module.deviceProperties.IsSDKLibrary = true
Inseob Kimc0907f12019-02-08 21:00:45 +0900816}
Sundong Ahn054b19a2018-10-19 13:46:09 +0900817
Jaewoong Jung4f158ee2019-07-11 10:05:35 -0700818// java_sdk_library is a special Java library that provides optional platform APIs to apps.
819// In practice, it can be viewed as a combination of several modules: 1) stubs library that clients
820// are linked against to, 2) droiddoc module that internally generates API stubs source files,
821// 3) the real runtime shared library that implements the APIs, and 4) XML file for adding
822// the runtime lib to the classpath at runtime if requested via <uses-library>.
Inseob Kimc0907f12019-02-08 21:00:45 +0900823func SdkLibraryFactory() android.Module {
824 module := &SdkLibrary{}
825 module.InitSdkLibraryProperties()
Jooyung Han58f26ab2019-12-18 15:34:32 +0900826 android.InitApexModule(module)
Sundong Ahn054b19a2018-10-19 13:46:09 +0900827 InitJavaModule(module, android.HostAndDeviceSupported)
Colin Crossf8b860a2019-04-16 14:43:28 -0700828 android.AddLoadHook(module, func(ctx android.LoadHookContext) { module.CreateInternalModules(ctx) })
Jiyong Parkc678ad32018-04-10 13:07:10 +0900829 return module
830}
Colin Cross79c7c262019-04-17 11:11:46 -0700831
832//
833// SDK library prebuilts
834//
835
836type sdkLibraryImportProperties struct {
837 Jars []string `android:"path"`
838
839 Sdk_version *string
840
841 Installable *bool
842
843 // List of shared java libs that this module has dependencies to
844 Libs []string
845
846 // List of files to remove from the jar file(s)
847 Exclude_files []string
848
849 // List of directories to remove from the jar file(s)
850 Exclude_dirs []string
851}
852
853type sdkLibraryImport struct {
854 android.ModuleBase
855 android.DefaultableModuleBase
856 prebuilt android.Prebuilt
857
858 properties sdkLibraryImportProperties
859
860 stubsPath android.Paths
861}
862
863var _ SdkLibraryDependency = (*sdkLibraryImport)(nil)
864
Jaewoong Jung4f158ee2019-07-11 10:05:35 -0700865// java_sdk_library_import imports a prebuilt java_sdk_library.
Colin Cross79c7c262019-04-17 11:11:46 -0700866func sdkLibraryImportFactory() android.Module {
867 module := &sdkLibraryImport{}
868
869 module.AddProperties(&module.properties)
870
871 android.InitPrebuiltModule(module, &module.properties.Jars)
872 InitJavaModule(module, android.HostAndDeviceSupported)
873
874 android.AddLoadHook(module, func(mctx android.LoadHookContext) { module.createInternalModules(mctx) })
875 return module
876}
877
878func (module *sdkLibraryImport) Prebuilt() *android.Prebuilt {
879 return &module.prebuilt
880}
881
882func (module *sdkLibraryImport) Name() string {
883 return module.prebuilt.Name(module.ModuleBase.Name())
884}
885
886func (module *sdkLibraryImport) createInternalModules(mctx android.LoadHookContext) {
887 // Creates a java import for the jar with ".stubs" suffix
888 props := struct {
Sundong Ahn0d7dff42019-12-04 12:53:44 +0900889 Name *string
890 Soc_specific *bool
891 Device_specific *bool
892 Product_specific *bool
893 System_ext_specific *bool
Colin Cross79c7c262019-04-17 11:11:46 -0700894 }{}
895
896 props.Name = proptools.StringPtr(module.BaseModuleName() + sdkStubsLibrarySuffix)
897
898 if module.SocSpecific() {
899 props.Soc_specific = proptools.BoolPtr(true)
900 } else if module.DeviceSpecific() {
901 props.Device_specific = proptools.BoolPtr(true)
902 } else if module.ProductSpecific() {
903 props.Product_specific = proptools.BoolPtr(true)
Sundong Ahn0d7dff42019-12-04 12:53:44 +0900904 } else if module.SystemExtSpecific() {
905 props.System_ext_specific = proptools.BoolPtr(true)
Colin Cross79c7c262019-04-17 11:11:46 -0700906 }
907
Colin Cross84dfc3d2019-09-25 11:33:01 -0700908 mctx.CreateModule(ImportFactory, &props, &module.properties)
Colin Cross79c7c262019-04-17 11:11:46 -0700909
910 javaSdkLibraries := javaSdkLibraries(mctx.Config())
911 javaSdkLibrariesLock.Lock()
912 defer javaSdkLibrariesLock.Unlock()
913 *javaSdkLibraries = append(*javaSdkLibraries, module.BaseModuleName())
914}
915
916func (module *sdkLibraryImport) DepsMutator(ctx android.BottomUpMutatorContext) {
917 // Add dependencies to the prebuilt stubs library
918 ctx.AddVariationDependencies(nil, publicApiStubsTag, module.BaseModuleName()+sdkStubsLibrarySuffix)
919}
920
921func (module *sdkLibraryImport) GenerateAndroidBuildActions(ctx android.ModuleContext) {
922 // Record the paths to the prebuilt stubs library.
923 ctx.VisitDirectDeps(func(to android.Module) {
924 tag := ctx.OtherModuleDependencyTag(to)
925
926 switch tag {
927 case publicApiStubsTag:
928 module.stubsPath = to.(Dependency).HeaderJars()
929 }
930 })
931}
932
933// to satisfy SdkLibraryDependency interface
Colin Cross0ea8ba82019-06-06 14:33:29 -0700934func (module *sdkLibraryImport) SdkHeaderJars(ctx android.BaseModuleContext, sdkVersion string) android.Paths {
Colin Cross79c7c262019-04-17 11:11:46 -0700935 // This module is just a wrapper for the prebuilt stubs.
936 return module.stubsPath
937}
938
939// to satisfy SdkLibraryDependency interface
Colin Cross0ea8ba82019-06-06 14:33:29 -0700940func (module *sdkLibraryImport) SdkImplementationJars(ctx android.BaseModuleContext, sdkVersion string) android.Paths {
Colin Cross79c7c262019-04-17 11:11:46 -0700941 // This module is just a wrapper for the stubs.
942 return module.stubsPath
943}