blob: def2753b8d0e9b5eee47c03fde8a8804793e6ae0 [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"
19 "android/soong/genrule"
20 "fmt"
Jiyong Park82484c02018-04-23 21:41:26 +090021 "io"
Jiyong Parkc678ad32018-04-10 13:07:10 +090022 "path"
Sundong Ahn054b19a2018-10-19 13:46:09 +090023 "path/filepath"
Jiyong Park82484c02018-04-23 21:41:26 +090024 "sort"
Jiyong Parkc678ad32018-04-10 13:07:10 +090025 "strings"
Jiyong Park82484c02018-04-23 21:41:26 +090026 "sync"
Jiyong Parkc678ad32018-04-10 13:07:10 +090027
28 "github.com/google/blueprint"
29 "github.com/google/blueprint/proptools"
30)
31
Jooyung Han58f26ab2019-12-18 15:34:32 +090032const (
Jiyong Parkc678ad32018-04-10 13:07:10 +090033 sdkStubsLibrarySuffix = ".stubs"
34 sdkSystemApiSuffix = ".system"
Jiyong Parkdf130542018-04-27 16:29:21 +090035 sdkTestApiSuffix = ".test"
Jiyong Parkc678ad32018-04-10 13:07:10 +090036 sdkDocsSuffix = ".docs"
Jiyong Parkc678ad32018-04-10 13:07:10 +090037 sdkXmlFileSuffix = ".xml"
Jooyung Han58f26ab2019-12-18 15:34:32 +090038 permissionTemplate = `<?xml version="1.0" encoding="utf-8"?>
39<!-- Copyright (C) 2018 The Android Open Source Project
40
41 Licensed under the Apache License, Version 2.0 (the "License");
42 you may not use this file except in compliance with the License.
43 You may obtain a copy of the License at
44
45 http://www.apache.org/licenses/LICENSE-2.0
46
47 Unless required by applicable law or agreed to in writing, software
48 distributed under the License is distributed on an "AS IS" BASIS,
49 WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
50 See the License for the specific language governing permissions and
51 limitations under the License.
52-->
53<permissions>
54 <library name="%s" file="%s"/>
55</permissions>
56`
Jiyong Parkc678ad32018-04-10 13:07:10 +090057)
58
59type stubsLibraryDependencyTag struct {
60 blueprint.BaseDependencyTag
61 name string
62}
63
64var (
65 publicApiStubsTag = dependencyTag{name: "public"}
66 systemApiStubsTag = dependencyTag{name: "system"}
Jiyong Parkdf130542018-04-27 16:29:21 +090067 testApiStubsTag = dependencyTag{name: "test"}
Sundong Ahn20e998b2018-07-24 11:19:26 +090068 publicApiFileTag = dependencyTag{name: "publicApi"}
69 systemApiFileTag = dependencyTag{name: "systemApi"}
70 testApiFileTag = dependencyTag{name: "testApi"}
Jiyong Parkdf130542018-04-27 16:29:21 +090071)
72
73type apiScope int
74
75const (
76 apiScopePublic apiScope = iota
77 apiScopeSystem
78 apiScopeTest
Jiyong Parkc678ad32018-04-10 13:07:10 +090079)
80
Jiyong Park82484c02018-04-23 21:41:26 +090081var (
82 javaSdkLibrariesLock sync.Mutex
83)
84
Jiyong Parkc678ad32018-04-10 13:07:10 +090085// TODO: these are big features that are currently missing
Jiyong Park1be96912018-05-28 18:02:19 +090086// 1) disallowing linking to the runtime shared lib
87// 2) HTML generation
Jiyong Parkc678ad32018-04-10 13:07:10 +090088
89func init() {
Paul Duffin43dc1cc2019-12-19 11:18:54 +000090 RegisterSdkLibraryBuildComponents(android.InitRegistrationContext)
Jiyong Parkc678ad32018-04-10 13:07:10 +090091
Jiyong Park82484c02018-04-23 21:41:26 +090092 android.RegisterMakeVarsProvider(pctx, func(ctx android.MakeVarsContext) {
93 javaSdkLibraries := javaSdkLibraries(ctx.Config())
94 sort.Strings(*javaSdkLibraries)
95 ctx.Strict("JAVA_SDK_LIBRARIES", strings.Join(*javaSdkLibraries, " "))
96 })
Jiyong Parkc678ad32018-04-10 13:07:10 +090097}
98
Paul Duffin43dc1cc2019-12-19 11:18:54 +000099func RegisterSdkLibraryBuildComponents(ctx android.RegistrationContext) {
100 ctx.RegisterModuleType("java_sdk_library", SdkLibraryFactory)
101 ctx.RegisterModuleType("java_sdk_library_import", sdkLibraryImportFactory)
102}
103
Jiyong Parkc678ad32018-04-10 13:07:10 +0900104type sdkLibraryProperties struct {
Sundong Ahnf043cf62018-06-25 16:04:37 +0900105 // List of Java libraries that will be in the classpath when building stubs
106 Stub_only_libs []string `android:"arch_variant"`
107
Jiyong Parkc678ad32018-04-10 13:07:10 +0900108 // list of package names that will be documented and publicized as API
109 Api_packages []string
110
Jiyong Park5a2c9d72018-05-01 22:25:41 +0900111 // list of package names that must be hidden from the API
112 Hidden_api_packages []string
113
Paul Duffin11512472019-02-11 15:55:17 +0000114 // local files that are used within user customized droiddoc options.
115 Droiddoc_option_files []string
116
117 // additional droiddoc options
118 // Available variables for substitution:
119 //
120 // $(location <label>): the path to the droiddoc_option_files with name <label>
Sundong Ahndd567f92018-07-31 17:19:11 +0900121 Droiddoc_options []string
122
Sundong Ahn054b19a2018-10-19 13:46:09 +0900123 // a list of top-level directories containing files to merge qualifier annotations
124 // (i.e. those intended to be included in the stubs written) from.
125 Merge_annotations_dirs []string
126
127 // a list of top-level directories containing Java stub files to merge show/hide annotations from.
128 Merge_inclusion_annotations_dirs []string
129
130 // If set to true, the path of dist files is apistubs/core. Defaults to false.
131 Core_lib *bool
132
Sundong Ahn80a87b32019-05-13 15:02:50 +0900133 // don't create dist rules.
134 No_dist *bool `blueprint:"mutated"`
135
Jiyong Parkc678ad32018-04-10 13:07:10 +0900136 // TODO: determines whether to create HTML doc or not
137 //Html_doc *bool
138}
139
Inseob Kimc0907f12019-02-08 21:00:45 +0900140type SdkLibrary struct {
Sundong Ahn054b19a2018-10-19 13:46:09 +0900141 Library
Jiyong Parkc678ad32018-04-10 13:07:10 +0900142
Sundong Ahn054b19a2018-10-19 13:46:09 +0900143 sdkLibraryProperties sdkLibraryProperties
Jiyong Parkc678ad32018-04-10 13:07:10 +0900144
145 publicApiStubsPath android.Paths
146 systemApiStubsPath android.Paths
Jiyong Parkdf130542018-04-27 16:29:21 +0900147 testApiStubsPath android.Paths
Sundong Ahn241cd372018-07-13 16:16:44 +0900148
149 publicApiStubsImplPath android.Paths
150 systemApiStubsImplPath android.Paths
151 testApiStubsImplPath android.Paths
Sundong Ahn20e998b2018-07-24 11:19:26 +0900152
153 publicApiFilePath android.Path
154 systemApiFilePath android.Path
155 testApiFilePath android.Path
Jooyung Han58f26ab2019-12-18 15:34:32 +0900156
157 permissionFile android.Path
Jiyong Parkc678ad32018-04-10 13:07:10 +0900158}
159
Inseob Kimc0907f12019-02-08 21:00:45 +0900160var _ Dependency = (*SdkLibrary)(nil)
161var _ SdkLibraryDependency = (*SdkLibrary)(nil)
Colin Cross897d2ed2019-02-11 14:03:51 -0800162
Inseob Kimc0907f12019-02-08 21:00:45 +0900163func (module *SdkLibrary) DepsMutator(ctx android.BottomUpMutatorContext) {
Jiyong Parke3ef3c82019-07-15 15:31:16 +0900164 useBuiltStubs := !ctx.Config().UnbundledBuildUsePrebuiltSdks()
Jiyong Parkc678ad32018-04-10 13:07:10 +0900165 // Add dependencies to the stubs library
Jiyong Parke3ef3c82019-07-15 15:31:16 +0900166 if useBuiltStubs {
167 ctx.AddVariationDependencies(nil, publicApiStubsTag, module.stubsName(apiScopePublic))
168 }
Colin Cross42d48b72018-08-29 14:10:52 -0700169 ctx.AddVariationDependencies(nil, publicApiFileTag, module.docsName(apiScopePublic))
Sundong Ahn054b19a2018-10-19 13:46:09 +0900170
Paul Duffin250e6192019-06-07 10:44:37 +0100171 sdkDep := decodeSdkDep(ctx, sdkContext(&module.Library))
172 if sdkDep.hasStandardLibs() {
Jiyong Parke3ef3c82019-07-15 15:31:16 +0900173 if useBuiltStubs {
174 ctx.AddVariationDependencies(nil, systemApiStubsTag, module.stubsName(apiScopeSystem))
175 ctx.AddVariationDependencies(nil, testApiStubsTag, module.stubsName(apiScopeTest))
176 }
Sundong Ahn054b19a2018-10-19 13:46:09 +0900177 ctx.AddVariationDependencies(nil, systemApiFileTag, module.docsName(apiScopeSystem))
178 ctx.AddVariationDependencies(nil, testApiFileTag, module.docsName(apiScopeTest))
Sundong Ahn054b19a2018-10-19 13:46:09 +0900179 }
180
181 module.Library.deps(ctx)
Jiyong Parkc678ad32018-04-10 13:07:10 +0900182}
183
Inseob Kimc0907f12019-02-08 21:00:45 +0900184func (module *SdkLibrary) GenerateAndroidBuildActions(ctx android.ModuleContext) {
Sundong Ahn054b19a2018-10-19 13:46:09 +0900185 module.Library.GenerateAndroidBuildActions(ctx)
186
Jooyung Han58f26ab2019-12-18 15:34:32 +0900187 if module.ApexName() != "" {
188 module.buildPermissionFile(ctx)
189 }
190
Sundong Ahn57368eb2018-07-06 11:20:23 +0900191 // Record the paths to the header jars of the library (stubs and impl).
Jiyong Parkc678ad32018-04-10 13:07:10 +0900192 // When this java_sdk_library is dependened from others via "libs" property,
193 // the recorded paths will be returned depending on the link type of the caller.
194 ctx.VisitDirectDeps(func(to android.Module) {
195 otherName := ctx.OtherModuleName(to)
196 tag := ctx.OtherModuleDependencyTag(to)
197
Sundong Ahn57368eb2018-07-06 11:20:23 +0900198 if lib, ok := to.(Dependency); ok {
Jiyong Parkc678ad32018-04-10 13:07:10 +0900199 switch tag {
200 case publicApiStubsTag:
Sundong Ahn57368eb2018-07-06 11:20:23 +0900201 module.publicApiStubsPath = lib.HeaderJars()
Sundong Ahn241cd372018-07-13 16:16:44 +0900202 module.publicApiStubsImplPath = lib.ImplementationJars()
Jiyong Parkc678ad32018-04-10 13:07:10 +0900203 case systemApiStubsTag:
Sundong Ahn57368eb2018-07-06 11:20:23 +0900204 module.systemApiStubsPath = lib.HeaderJars()
Sundong Ahn241cd372018-07-13 16:16:44 +0900205 module.systemApiStubsImplPath = lib.ImplementationJars()
Jiyong Parkdf130542018-04-27 16:29:21 +0900206 case testApiStubsTag:
Sundong Ahn57368eb2018-07-06 11:20:23 +0900207 module.testApiStubsPath = lib.HeaderJars()
Sundong Ahn241cd372018-07-13 16:16:44 +0900208 module.testApiStubsImplPath = lib.ImplementationJars()
Jiyong Parkc678ad32018-04-10 13:07:10 +0900209 }
210 }
Sundong Ahn20e998b2018-07-24 11:19:26 +0900211 if doc, ok := to.(ApiFilePath); ok {
212 switch tag {
213 case publicApiFileTag:
214 module.publicApiFilePath = doc.ApiFilePath()
215 case systemApiFileTag:
216 module.systemApiFilePath = doc.ApiFilePath()
217 case testApiFileTag:
218 module.testApiFilePath = doc.ApiFilePath()
219 default:
220 ctx.ModuleErrorf("depends on module %q of unknown tag %q", otherName, tag)
221 }
222 }
Jiyong Parkc678ad32018-04-10 13:07:10 +0900223 })
224}
225
Jooyung Han58f26ab2019-12-18 15:34:32 +0900226func (module *SdkLibrary) buildPermissionFile(ctx android.ModuleContext) {
227 xmlContent := strings.ReplaceAll(fmt.Sprintf(permissionTemplate, module.BaseModuleName(), module.implPath()), "\n", "\\n")
228 permissionFile := android.PathForModuleOut(ctx, module.xmlFileName())
229
230 rule := android.NewRuleBuilder()
231 rule.Command().Text("echo -e ").Text(proptools.ShellEscape(xmlContent)).Text(">").Output(permissionFile)
232 rule.Build(pctx, ctx, "gen_permission_xml", "Generate permission")
233
234 module.permissionFile = permissionFile
235}
236
237func (module *SdkLibrary) PermissionFile() android.Path {
238 return module.permissionFile
239}
240
Jiyong Park0b0e1b92019-12-03 13:24:29 +0900241func (module *SdkLibrary) AndroidMkEntries() []android.AndroidMkEntries {
242 entriesList := module.Library.AndroidMkEntries()
243 entries := &entriesList[0]
Jaewoong Jungb0c127c2019-08-29 14:56:03 -0700244 entries.Required = append(entries.Required, module.xmlFileName())
Sundong Ahn054b19a2018-10-19 13:46:09 +0900245
Jaewoong Jungb0c127c2019-08-29 14:56:03 -0700246 entries.ExtraFooters = []android.AndroidMkExtraFootersFunc{
247 func(w io.Writer, name, prefix, moduleDir string, entries *android.AndroidMkEntries) {
Jaewoong Jungb0c127c2019-08-29 14:56:03 -0700248 if !Bool(module.sdkLibraryProperties.No_dist) {
249 // Create a phony module that installs the impl library, for the case when this lib is
250 // in PRODUCT_PACKAGES.
251 owner := module.ModuleBase.Owner()
252 if owner == "" {
253 if Bool(module.sdkLibraryProperties.Core_lib) {
254 owner = "core"
255 } else {
256 owner = "android"
257 }
258 }
259 // Create dist rules to install the stubs libs to the dist dir
260 if len(module.publicApiStubsPath) == 1 {
261 fmt.Fprintln(w, "$(call dist-for-goals,sdk win_sdk,"+
262 module.publicApiStubsImplPath.Strings()[0]+
263 ":"+path.Join("apistubs", owner, "public",
264 module.BaseModuleName()+".jar")+")")
265 }
266 if len(module.systemApiStubsPath) == 1 {
267 fmt.Fprintln(w, "$(call dist-for-goals,sdk win_sdk,"+
268 module.systemApiStubsImplPath.Strings()[0]+
269 ":"+path.Join("apistubs", owner, "system",
270 module.BaseModuleName()+".jar")+")")
271 }
272 if len(module.testApiStubsPath) == 1 {
273 fmt.Fprintln(w, "$(call dist-for-goals,sdk win_sdk,"+
274 module.testApiStubsImplPath.Strings()[0]+
275 ":"+path.Join("apistubs", owner, "test",
276 module.BaseModuleName()+".jar")+")")
277 }
278 if module.publicApiFilePath != nil {
279 fmt.Fprintln(w, "$(call dist-for-goals,sdk win_sdk,"+
280 module.publicApiFilePath.String()+
281 ":"+path.Join("apistubs", owner, "public", "api",
282 module.BaseModuleName()+".txt")+")")
283 }
284 if module.systemApiFilePath != nil {
285 fmt.Fprintln(w, "$(call dist-for-goals,sdk win_sdk,"+
286 module.systemApiFilePath.String()+
287 ":"+path.Join("apistubs", owner, "system", "api",
288 module.BaseModuleName()+".txt")+")")
289 }
290 if module.testApiFilePath != nil {
291 fmt.Fprintln(w, "$(call dist-for-goals,sdk win_sdk,"+
292 module.testApiFilePath.String()+
293 ":"+path.Join("apistubs", owner, "test", "api",
294 module.BaseModuleName()+".txt")+")")
Sundong Ahn80a87b32019-05-13 15:02:50 +0900295 }
Sundong Ahn4fd04bb2018-08-31 18:01:37 +0900296 }
Jaewoong Jungb0c127c2019-08-29 14:56:03 -0700297 },
Jiyong Park82484c02018-04-23 21:41:26 +0900298 }
Jiyong Park0b0e1b92019-12-03 13:24:29 +0900299 return entriesList
Jiyong Park82484c02018-04-23 21:41:26 +0900300}
301
Jiyong Parkc678ad32018-04-10 13:07:10 +0900302// Module name of the stubs library
Inseob Kimc0907f12019-02-08 21:00:45 +0900303func (module *SdkLibrary) stubsName(apiScope apiScope) string {
Jiyong Parkc678ad32018-04-10 13:07:10 +0900304 stubsName := module.BaseModuleName() + sdkStubsLibrarySuffix
Jiyong Parkdf130542018-04-27 16:29:21 +0900305 switch apiScope {
306 case apiScopeSystem:
Jiyong Parkc678ad32018-04-10 13:07:10 +0900307 stubsName = stubsName + sdkSystemApiSuffix
Jiyong Parkdf130542018-04-27 16:29:21 +0900308 case apiScopeTest:
309 stubsName = stubsName + sdkTestApiSuffix
Jiyong Parkc678ad32018-04-10 13:07:10 +0900310 }
311 return stubsName
312}
313
314// Module name of the docs
Inseob Kimc0907f12019-02-08 21:00:45 +0900315func (module *SdkLibrary) docsName(apiScope apiScope) string {
Jiyong Parkc678ad32018-04-10 13:07:10 +0900316 docsName := module.BaseModuleName() + sdkDocsSuffix
Jiyong Parkdf130542018-04-27 16:29:21 +0900317 switch apiScope {
318 case apiScopeSystem:
Jiyong Parkc678ad32018-04-10 13:07:10 +0900319 docsName = docsName + sdkSystemApiSuffix
Jiyong Parkdf130542018-04-27 16:29:21 +0900320 case apiScopeTest:
321 docsName = docsName + sdkTestApiSuffix
Jiyong Parkc678ad32018-04-10 13:07:10 +0900322 }
323 return docsName
324}
325
326// Module name of the runtime implementation library
Inseob Kimc0907f12019-02-08 21:00:45 +0900327func (module *SdkLibrary) implName() string {
Sundong Ahn054b19a2018-10-19 13:46:09 +0900328 return module.BaseModuleName()
Jiyong Parkc678ad32018-04-10 13:07:10 +0900329}
330
331// File path to the runtime implementation library
Inseob Kimc0907f12019-02-08 21:00:45 +0900332func (module *SdkLibrary) implPath() string {
Jooyung Han58f26ab2019-12-18 15:34:32 +0900333 if apexName := module.ApexName(); apexName != "" {
334 // TODO(b/146468504): ApexName() is only a soong module name, not apex name.
335 // In most cases, this works fine. But when apex_name is set or override_apex is used
336 // this can be wrong.
337 return fmt.Sprintf("/apex/%s/javalib/%s.jar", apexName, module.implName())
338 }
Jiyong Parkc678ad32018-04-10 13:07:10 +0900339 partition := "system"
340 if module.SocSpecific() {
341 partition = "vendor"
342 } else if module.DeviceSpecific() {
343 partition = "odm"
344 } else if module.ProductSpecific() {
345 partition = "product"
Sundong Ahn0d7dff42019-12-04 12:53:44 +0900346 } else if module.SystemExtSpecific() {
347 partition = "system_ext"
Jiyong Parkc678ad32018-04-10 13:07:10 +0900348 }
349 return "/" + partition + "/framework/" + module.implName() + ".jar"
350}
351
352// Module name of the XML file for the lib
Inseob Kimc0907f12019-02-08 21:00:45 +0900353func (module *SdkLibrary) xmlFileName() string {
Jiyong Parkc678ad32018-04-10 13:07:10 +0900354 return module.BaseModuleName() + sdkXmlFileSuffix
355}
356
357// SDK version that the stubs library is built against. Note that this is always
358// *current. Older stubs library built with a numberd SDK version is created from
359// the prebuilt jar.
Inseob Kimc0907f12019-02-08 21:00:45 +0900360func (module *SdkLibrary) sdkVersion(apiScope apiScope) string {
Jiyong Parkdf130542018-04-27 16:29:21 +0900361 switch apiScope {
362 case apiScopePublic:
363 return "current"
364 case apiScopeSystem:
Jiyong Parkc678ad32018-04-10 13:07:10 +0900365 return "system_current"
Jiyong Parkdf130542018-04-27 16:29:21 +0900366 case apiScopeTest:
367 return "test_current"
368 default:
Jiyong Parkc678ad32018-04-10 13:07:10 +0900369 return "current"
370 }
371}
372
373// $(INTERNAL_PLATFORM_<apiTagName>_API_FILE) points to the generated
374// api file for the current source
375// TODO: remove this when apicheck is done in soong
Inseob Kimc0907f12019-02-08 21:00:45 +0900376func (module *SdkLibrary) apiTagName(apiScope apiScope) string {
Jiyong Parkc678ad32018-04-10 13:07:10 +0900377 apiTagName := strings.Replace(strings.ToUpper(module.BaseModuleName()), ".", "_", -1)
Jiyong Parkdf130542018-04-27 16:29:21 +0900378 switch apiScope {
379 case apiScopeSystem:
Jiyong Parkc678ad32018-04-10 13:07:10 +0900380 apiTagName = apiTagName + "_SYSTEM"
Jiyong Parkdf130542018-04-27 16:29:21 +0900381 case apiScopeTest:
382 apiTagName = apiTagName + "_TEST"
Jiyong Parkc678ad32018-04-10 13:07:10 +0900383 }
384 return apiTagName
385}
386
Inseob Kimc0907f12019-02-08 21:00:45 +0900387func (module *SdkLibrary) latestApiFilegroupName(apiScope apiScope) string {
Jiyong Park58c518b2018-05-12 22:29:12 +0900388 name := ":" + module.BaseModuleName() + ".api."
Jiyong Parkdf130542018-04-27 16:29:21 +0900389 switch apiScope {
Jiyong Park58c518b2018-05-12 22:29:12 +0900390 case apiScopePublic:
391 name = name + "public"
Jiyong Parkdf130542018-04-27 16:29:21 +0900392 case apiScopeSystem:
Jiyong Park58c518b2018-05-12 22:29:12 +0900393 name = name + "system"
Jiyong Parkdf130542018-04-27 16:29:21 +0900394 case apiScopeTest:
Jiyong Park58c518b2018-05-12 22:29:12 +0900395 name = name + "test"
Jiyong Parkc678ad32018-04-10 13:07:10 +0900396 }
Jiyong Park58c518b2018-05-12 22:29:12 +0900397 name = name + ".latest"
398 return name
399}
Jiyong Parkc678ad32018-04-10 13:07:10 +0900400
Inseob Kimc0907f12019-02-08 21:00:45 +0900401func (module *SdkLibrary) latestRemovedApiFilegroupName(apiScope apiScope) string {
Jiyong Park58c518b2018-05-12 22:29:12 +0900402 name := ":" + module.BaseModuleName() + "-removed.api."
403 switch apiScope {
404 case apiScopePublic:
405 name = name + "public"
406 case apiScopeSystem:
407 name = name + "system"
408 case apiScopeTest:
409 name = name + "test"
410 }
411 name = name + ".latest"
412 return name
Jiyong Parkc678ad32018-04-10 13:07:10 +0900413}
414
415// Creates a static java library that has API stubs
Colin Crossf8b860a2019-04-16 14:43:28 -0700416func (module *SdkLibrary) createStubsLibrary(mctx android.LoadHookContext, apiScope apiScope) {
Jiyong Parkc678ad32018-04-10 13:07:10 +0900417 props := struct {
Sundong Ahn0d7dff42019-12-04 12:53:44 +0900418 Name *string
419 Srcs []string
420 Sdk_version *string
421 Libs []string
422 Soc_specific *bool
423 Device_specific *bool
424 Product_specific *bool
425 System_ext_specific *bool
426 Compile_dex *bool
427 System_modules *string
428 Java_version *string
429 Product_variables struct {
Jiyong Parkc678ad32018-04-10 13:07:10 +0900430 Unbundled_build struct {
431 Enabled *bool
432 }
Jiyong Park82484c02018-04-23 21:41:26 +0900433 Pdk struct {
434 Enabled *bool
435 }
Jiyong Parkc678ad32018-04-10 13:07:10 +0900436 }
Sundong Ahn054b19a2018-10-19 13:46:09 +0900437 Openjdk9 struct {
438 Srcs []string
439 Javacflags []string
440 }
Jiyong Parkc678ad32018-04-10 13:07:10 +0900441 }{}
442
Paul Duffin52d398a2019-06-11 12:31:14 +0100443 sdkVersion := module.sdkVersion(apiScope)
Paul Duffin250e6192019-06-07 10:44:37 +0100444 sdkDep := decodeSdkDep(mctx, sdkContext(&module.Library))
Paul Duffin52d398a2019-06-11 12:31:14 +0100445 if !sdkDep.hasStandardLibs() {
446 sdkVersion = "none"
447 }
Paul Duffin250e6192019-06-07 10:44:37 +0100448
Jiyong Parkdf130542018-04-27 16:29:21 +0900449 props.Name = proptools.StringPtr(module.stubsName(apiScope))
Jiyong Parkc678ad32018-04-10 13:07:10 +0900450 // sources are generated from the droiddoc
Jiyong Parkdf130542018-04-27 16:29:21 +0900451 props.Srcs = []string{":" + module.docsName(apiScope)}
Paul Duffin52d398a2019-06-11 12:31:14 +0100452 props.Sdk_version = proptools.StringPtr(sdkVersion)
Sundong Ahn054b19a2018-10-19 13:46:09 +0900453 props.Libs = module.sdkLibraryProperties.Stub_only_libs
Jiyong Parkc678ad32018-04-10 13:07:10 +0900454 // Unbundled apps will use the prebult one from /prebuilts/sdk
Colin Cross10932872019-04-18 14:27:12 -0700455 if mctx.Config().UnbundledBuildUsePrebuiltSdks() {
Colin Cross2c77ceb2019-01-21 11:56:21 -0800456 props.Product_variables.Unbundled_build.Enabled = proptools.BoolPtr(false)
457 }
Jiyong Park82484c02018-04-23 21:41:26 +0900458 props.Product_variables.Pdk.Enabled = proptools.BoolPtr(false)
Sundong Ahn054b19a2018-10-19 13:46:09 +0900459 props.System_modules = module.Library.Module.deviceProperties.System_modules
460 props.Openjdk9.Srcs = module.Library.Module.properties.Openjdk9.Srcs
461 props.Openjdk9.Javacflags = module.Library.Module.properties.Openjdk9.Javacflags
462 props.Java_version = module.Library.Module.properties.Java_version
463 if module.Library.Module.deviceProperties.Compile_dex != nil {
464 props.Compile_dex = module.Library.Module.deviceProperties.Compile_dex
Sundong Ahndd567f92018-07-31 17:19:11 +0900465 }
Jiyong Parkc678ad32018-04-10 13:07:10 +0900466
467 if module.SocSpecific() {
468 props.Soc_specific = proptools.BoolPtr(true)
469 } else if module.DeviceSpecific() {
470 props.Device_specific = proptools.BoolPtr(true)
471 } else if module.ProductSpecific() {
472 props.Product_specific = proptools.BoolPtr(true)
Sundong Ahn0d7dff42019-12-04 12:53:44 +0900473 } else if module.SystemExtSpecific() {
474 props.System_ext_specific = proptools.BoolPtr(true)
Jiyong Parkc678ad32018-04-10 13:07:10 +0900475 }
476
Colin Cross84dfc3d2019-09-25 11:33:01 -0700477 mctx.CreateModule(LibraryFactory, &props)
Jiyong Parkc678ad32018-04-10 13:07:10 +0900478}
479
480// Creates a droiddoc module that creates stubs source files from the given full source
481// files
Colin Crossf8b860a2019-04-16 14:43:28 -0700482func (module *SdkLibrary) createDocs(mctx android.LoadHookContext, apiScope apiScope) {
Jiyong Parkc678ad32018-04-10 13:07:10 +0900483 props := struct {
Sundong Ahn054b19a2018-10-19 13:46:09 +0900484 Name *string
485 Srcs []string
486 Installable *bool
Paul Duffin52d398a2019-06-11 12:31:14 +0100487 Sdk_version *string
Sundong Ahn054b19a2018-10-19 13:46:09 +0900488 Libs []string
Paul Duffin11512472019-02-11 15:55:17 +0000489 Arg_files []string
Sundong Ahn054b19a2018-10-19 13:46:09 +0900490 Args *string
491 Api_tag_name *string
492 Api_filename *string
493 Removed_api_filename *string
Sundong Ahn054b19a2018-10-19 13:46:09 +0900494 Java_version *string
495 Merge_annotations_dirs []string
496 Merge_inclusion_annotations_dirs []string
497 Check_api struct {
Inseob Kim38449af2019-02-28 14:24:05 +0900498 Current ApiToCheck
499 Last_released ApiToCheck
500 Ignore_missing_latest_api *bool
Jiyong Park58c518b2018-05-12 22:29:12 +0900501 }
Sundong Ahn1b92c822018-05-29 11:35:17 +0900502 Aidl struct {
503 Include_dirs []string
504 Local_include_dirs []string
505 }
Jiyong Parkc678ad32018-04-10 13:07:10 +0900506 }{}
507
Paul Duffin250e6192019-06-07 10:44:37 +0100508 sdkDep := decodeSdkDep(mctx, sdkContext(&module.Library))
Paul Duffin52d398a2019-06-11 12:31:14 +0100509 sdkVersion := ""
510 if !sdkDep.hasStandardLibs() {
511 sdkVersion = "none"
512 }
Paul Duffin250e6192019-06-07 10:44:37 +0100513
Jiyong Parkdf130542018-04-27 16:29:21 +0900514 props.Name = proptools.StringPtr(module.docsName(apiScope))
Sundong Ahn054b19a2018-10-19 13:46:09 +0900515 props.Srcs = append(props.Srcs, module.Library.Module.properties.Srcs...)
Paul Duffin52d398a2019-06-11 12:31:14 +0100516 props.Sdk_version = proptools.StringPtr(sdkVersion)
Jiyong Parkc678ad32018-04-10 13:07:10 +0900517 props.Installable = proptools.BoolPtr(false)
Sundong Ahne6f0b052018-06-05 16:46:14 +0900518 // A droiddoc module has only one Libs property and doesn't distinguish between
519 // shared libs and static libs. So we need to add both of these libs to Libs property.
Sundong Ahn054b19a2018-10-19 13:46:09 +0900520 props.Libs = module.Library.Module.properties.Libs
521 props.Libs = append(props.Libs, module.Library.Module.properties.Static_libs...)
522 props.Aidl.Include_dirs = module.Library.Module.deviceProperties.Aidl.Include_dirs
523 props.Aidl.Local_include_dirs = module.Library.Module.deviceProperties.Aidl.Local_include_dirs
Sundong Ahn054b19a2018-10-19 13:46:09 +0900524 props.Java_version = module.Library.Module.properties.Java_version
Jiyong Parkc678ad32018-04-10 13:07:10 +0900525
Sundong Ahn054b19a2018-10-19 13:46:09 +0900526 props.Merge_annotations_dirs = module.sdkLibraryProperties.Merge_annotations_dirs
527 props.Merge_inclusion_annotations_dirs = module.sdkLibraryProperties.Merge_inclusion_annotations_dirs
528
529 droiddocArgs := " --stub-packages " + strings.Join(module.sdkLibraryProperties.Api_packages, ":") +
530 " " + android.JoinWithPrefix(module.sdkLibraryProperties.Hidden_api_packages, " --hide-package ") +
531 " " + android.JoinWithPrefix(module.sdkLibraryProperties.Droiddoc_options, " ") +
Sundong Ahn04ef8a32019-01-14 11:36:50 +0900532 " --hide MissingPermission --hide BroadcastBehavior " +
533 "--hide HiddenSuperclass --hide DeprecationMismatch --hide UnavailableSymbol " +
534 "--hide SdkConstant --hide HiddenTypeParameter --hide Todo --hide Typo"
Sundong Ahnfb2721f2018-09-17 13:23:09 +0900535
Jiyong Parkdf130542018-04-27 16:29:21 +0900536 switch apiScope {
537 case apiScopeSystem:
Jiyong Parkc678ad32018-04-10 13:07:10 +0900538 droiddocArgs = droiddocArgs + " -showAnnotation android.annotation.SystemApi"
Jiyong Parkdf130542018-04-27 16:29:21 +0900539 case apiScopeTest:
540 droiddocArgs = droiddocArgs + " -showAnnotation android.annotation.TestApi"
Jiyong Parkc678ad32018-04-10 13:07:10 +0900541 }
Paul Duffin11512472019-02-11 15:55:17 +0000542 props.Arg_files = module.sdkLibraryProperties.Droiddoc_option_files
Jiyong Parkc678ad32018-04-10 13:07:10 +0900543 props.Args = proptools.StringPtr(droiddocArgs)
544
545 // List of APIs identified from the provided source files are created. They are later
546 // compared against to the not-yet-released (a.k.a current) list of APIs and to the
547 // last-released (a.k.a numbered) list of API.
Jiyong Parkc678ad32018-04-10 13:07:10 +0900548 currentApiFileName := "current.txt"
549 removedApiFileName := "removed.txt"
Jiyong Parkdf130542018-04-27 16:29:21 +0900550 switch apiScope {
551 case apiScopeSystem:
Jiyong Parkc678ad32018-04-10 13:07:10 +0900552 currentApiFileName = "system-" + currentApiFileName
553 removedApiFileName = "system-" + removedApiFileName
Jiyong Parkdf130542018-04-27 16:29:21 +0900554 case apiScopeTest:
555 currentApiFileName = "test-" + currentApiFileName
556 removedApiFileName = "test-" + removedApiFileName
Jiyong Parkc678ad32018-04-10 13:07:10 +0900557 }
558 currentApiFileName = path.Join("api", currentApiFileName)
559 removedApiFileName = path.Join("api", removedApiFileName)
Jiyong Park58c518b2018-05-12 22:29:12 +0900560 // TODO(jiyong): remove these three props
Jiyong Parkdf130542018-04-27 16:29:21 +0900561 props.Api_tag_name = proptools.StringPtr(module.apiTagName(apiScope))
Jiyong Parkc678ad32018-04-10 13:07:10 +0900562 props.Api_filename = proptools.StringPtr(currentApiFileName)
563 props.Removed_api_filename = proptools.StringPtr(removedApiFileName)
564
Jiyong Park58c518b2018-05-12 22:29:12 +0900565 // check against the not-yet-release API
566 props.Check_api.Current.Api_file = proptools.StringPtr(currentApiFileName)
567 props.Check_api.Current.Removed_api_file = proptools.StringPtr(removedApiFileName)
Jiyong Park58c518b2018-05-12 22:29:12 +0900568
569 // check against the latest released API
570 props.Check_api.Last_released.Api_file = proptools.StringPtr(
571 module.latestApiFilegroupName(apiScope))
572 props.Check_api.Last_released.Removed_api_file = proptools.StringPtr(
573 module.latestRemovedApiFilegroupName(apiScope))
Inseob Kim38449af2019-02-28 14:24:05 +0900574 props.Check_api.Ignore_missing_latest_api = proptools.BoolPtr(true)
Jiyong Park58c518b2018-05-12 22:29:12 +0900575
Colin Cross84dfc3d2019-09-25 11:33:01 -0700576 mctx.CreateModule(DroidstubsFactory, &props)
Jiyong Parkc678ad32018-04-10 13:07:10 +0900577}
578
Jiyong Parkc678ad32018-04-10 13:07:10 +0900579// Creates the xml file that publicizes the runtime library
Colin Crossf8b860a2019-04-16 14:43:28 -0700580func (module *SdkLibrary) createXmlFile(mctx android.LoadHookContext) {
Jiyong Parkc678ad32018-04-10 13:07:10 +0900581
Jiyong Parkc678ad32018-04-10 13:07:10 +0900582 // genrule to generate the xml file content from the template above
583 // TODO: preserve newlines in the generate xml file. Newlines are being squashed
584 // in the ninja file. Do we need to have an external tool for this?
Jooyung Han58f26ab2019-12-18 15:34:32 +0900585 xmlContent := fmt.Sprintf(permissionTemplate, module.BaseModuleName(), module.implPath())
Jiyong Parkc678ad32018-04-10 13:07:10 +0900586 genruleProps := struct {
587 Name *string
588 Cmd *string
589 Out []string
590 }{}
591 genruleProps.Name = proptools.StringPtr(module.xmlFileName() + "-gen")
592 genruleProps.Cmd = proptools.StringPtr("echo '" + xmlContent + "' > $(out)")
593 genruleProps.Out = []string{module.xmlFileName()}
Colin Cross84dfc3d2019-09-25 11:33:01 -0700594 mctx.CreateModule(genrule.GenRuleFactory, &genruleProps)
Jiyong Parkc678ad32018-04-10 13:07:10 +0900595
596 // creates a prebuilt_etc module to actually place the xml file under
597 // <partition>/etc/permissions
598 etcProps := struct {
Sundong Ahn0d7dff42019-12-04 12:53:44 +0900599 Name *string
600 Src *string
601 Sub_dir *string
602 Soc_specific *bool
603 Device_specific *bool
604 Product_specific *bool
605 System_ext_specific *bool
Jiyong Parkc678ad32018-04-10 13:07:10 +0900606 }{}
607 etcProps.Name = proptools.StringPtr(module.xmlFileName())
Jiyong Park5a8d1be2018-04-25 22:57:34 +0900608 etcProps.Src = proptools.StringPtr(":" + module.xmlFileName() + "-gen")
Jiyong Parkc678ad32018-04-10 13:07:10 +0900609 etcProps.Sub_dir = proptools.StringPtr("permissions")
610 if module.SocSpecific() {
611 etcProps.Soc_specific = proptools.BoolPtr(true)
612 } else if module.DeviceSpecific() {
613 etcProps.Device_specific = proptools.BoolPtr(true)
614 } else if module.ProductSpecific() {
615 etcProps.Product_specific = proptools.BoolPtr(true)
Sundong Ahn0d7dff42019-12-04 12:53:44 +0900616 } else if module.SystemExtSpecific() {
617 etcProps.System_ext_specific = proptools.BoolPtr(true)
Jiyong Parkc678ad32018-04-10 13:07:10 +0900618 }
Colin Cross84dfc3d2019-09-25 11:33:01 -0700619 mctx.CreateModule(android.PrebuiltEtcFactory, &etcProps)
Jiyong Parkc678ad32018-04-10 13:07:10 +0900620}
621
Colin Cross0ea8ba82019-06-06 14:33:29 -0700622func (module *SdkLibrary) PrebuiltJars(ctx android.BaseModuleContext, sdkVersion string) android.Paths {
Sundong Ahn054b19a2018-10-19 13:46:09 +0900623 var api, v string
Paul Duffin52d398a2019-06-11 12:31:14 +0100624 if sdkVersion == "" || sdkVersion == "none" {
Sundong Ahn054b19a2018-10-19 13:46:09 +0900625 api = "system"
626 v = "current"
627 } else if strings.Contains(sdkVersion, "_") {
628 t := strings.Split(sdkVersion, "_")
629 api = t[0]
630 v = t[1]
Jiyong Parkc678ad32018-04-10 13:07:10 +0900631 } else {
Sundong Ahn054b19a2018-10-19 13:46:09 +0900632 api = "public"
633 v = sdkVersion
634 }
635 dir := filepath.Join("prebuilts", "sdk", v, api)
636 jar := filepath.Join(dir, module.BaseModuleName()+".jar")
637 jarPath := android.ExistentPathForSource(ctx, jar)
Sundong Ahnae418ac2019-02-28 15:01:28 +0900638 if !jarPath.Valid() {
639 ctx.PropertyErrorf("sdk_library", "invalid sdk version %q, %q does not exist", v, jar)
640 return nil
641 }
Sundong Ahn054b19a2018-10-19 13:46:09 +0900642 return android.Paths{jarPath.Path()}
643}
644
645// to satisfy SdkLibraryDependency interface
Colin Cross0ea8ba82019-06-06 14:33:29 -0700646func (module *SdkLibrary) SdkHeaderJars(ctx android.BaseModuleContext, sdkVersion string) android.Paths {
Sundong Ahn054b19a2018-10-19 13:46:09 +0900647 // This module is just a wrapper for the stubs.
Colin Cross10932872019-04-18 14:27:12 -0700648 if ctx.Config().UnbundledBuildUsePrebuiltSdks() {
Sundong Ahn054b19a2018-10-19 13:46:09 +0900649 return module.PrebuiltJars(ctx, sdkVersion)
650 } else {
651 if strings.HasPrefix(sdkVersion, "system_") {
652 return module.systemApiStubsPath
653 } else if sdkVersion == "" {
654 return module.Library.HeaderJars()
655 } else {
656 return module.publicApiStubsPath
657 }
Jiyong Parkc678ad32018-04-10 13:07:10 +0900658 }
659}
660
Sundong Ahn241cd372018-07-13 16:16:44 +0900661// to satisfy SdkLibraryDependency interface
Colin Cross0ea8ba82019-06-06 14:33:29 -0700662func (module *SdkLibrary) SdkImplementationJars(ctx android.BaseModuleContext, sdkVersion string) android.Paths {
Sundong Ahn241cd372018-07-13 16:16:44 +0900663 // This module is just a wrapper for the stubs.
Colin Cross10932872019-04-18 14:27:12 -0700664 if ctx.Config().UnbundledBuildUsePrebuiltSdks() {
Sundong Ahn054b19a2018-10-19 13:46:09 +0900665 return module.PrebuiltJars(ctx, sdkVersion)
Sundong Ahn241cd372018-07-13 16:16:44 +0900666 } else {
Sundong Ahn054b19a2018-10-19 13:46:09 +0900667 if strings.HasPrefix(sdkVersion, "system_") {
668 return module.systemApiStubsImplPath
669 } else if sdkVersion == "" {
670 return module.Library.ImplementationJars()
671 } else {
672 return module.publicApiStubsImplPath
673 }
Sundong Ahn241cd372018-07-13 16:16:44 +0900674 }
675}
676
Sundong Ahn80a87b32019-05-13 15:02:50 +0900677func (module *SdkLibrary) SetNoDist() {
678 module.sdkLibraryProperties.No_dist = proptools.BoolPtr(true)
679}
680
Colin Cross571cccf2019-02-04 11:22:08 -0800681var javaSdkLibrariesKey = android.NewOnceKey("javaSdkLibraries")
682
Jiyong Park82484c02018-04-23 21:41:26 +0900683func javaSdkLibraries(config android.Config) *[]string {
Colin Cross571cccf2019-02-04 11:22:08 -0800684 return config.Once(javaSdkLibrariesKey, func() interface{} {
Jiyong Park82484c02018-04-23 21:41:26 +0900685 return &[]string{}
686 }).(*[]string)
687}
688
Jiyong Parkc678ad32018-04-10 13:07:10 +0900689// For a java_sdk_library module, create internal modules for stubs, docs,
690// runtime libs and xml file. If requested, the stubs and docs are created twice
691// once for public API level and once for system API level
Colin Crossf8b860a2019-04-16 14:43:28 -0700692func (module *SdkLibrary) CreateInternalModules(mctx android.LoadHookContext) {
Inseob Kim6e93ac92019-03-21 17:43:49 +0900693 if len(module.Library.Module.properties.Srcs) == 0 {
Inseob Kimc0907f12019-02-08 21:00:45 +0900694 mctx.PropertyErrorf("srcs", "java_sdk_library must specify srcs")
Jooyung Han58f26ab2019-12-18 15:34:32 +0900695 return
Inseob Kimc0907f12019-02-08 21:00:45 +0900696 }
697
Inseob Kim6e93ac92019-03-21 17:43:49 +0900698 if len(module.sdkLibraryProperties.Api_packages) == 0 {
Inseob Kimc0907f12019-02-08 21:00:45 +0900699 mctx.PropertyErrorf("api_packages", "java_sdk_library must specify api_packages")
Jooyung Han58f26ab2019-12-18 15:34:32 +0900700 return
Inseob Kimc0907f12019-02-08 21:00:45 +0900701 }
Inseob Kim8098faa2019-03-18 10:19:51 +0900702
703 missing_current_api := false
704
705 for _, scope := range []string{"", "system-", "test-"} {
706 for _, api := range []string{"current.txt", "removed.txt"} {
707 path := path.Join(mctx.ModuleDir(), "api", scope+api)
708 p := android.ExistentPathForSource(mctx, path)
709 if !p.Valid() {
710 mctx.ModuleErrorf("Current api file %#v doesn't exist", path)
711 missing_current_api = true
712 }
713 }
714 }
715
716 if missing_current_api {
717 script := "build/soong/scripts/gen-java-current-api-files.sh"
718 p := android.ExistentPathForSource(mctx, script)
719
720 if !p.Valid() {
721 panic(fmt.Sprintf("script file %s doesn't exist", script))
722 }
723
724 mctx.ModuleErrorf("One or more current api files are missing. "+
725 "You can update them by:\n"+
726 "%s %q && m update-api", script, mctx.ModuleDir())
727 return
728 }
729
Inseob Kimc0907f12019-02-08 21:00:45 +0900730 // for public API stubs
731 module.createStubsLibrary(mctx, apiScopePublic)
732 module.createDocs(mctx, apiScopePublic)
733
Paul Duffin250e6192019-06-07 10:44:37 +0100734 sdkDep := decodeSdkDep(mctx, sdkContext(&module.Library))
735 if sdkDep.hasStandardLibs() {
Inseob Kimc0907f12019-02-08 21:00:45 +0900736 // for system API stubs
737 module.createStubsLibrary(mctx, apiScopeSystem)
738 module.createDocs(mctx, apiScopeSystem)
739
740 // for test API stubs
741 module.createStubsLibrary(mctx, apiScopeTest)
742 module.createDocs(mctx, apiScopeTest)
743
744 // for runtime
745 module.createXmlFile(mctx)
746 }
747
748 // record java_sdk_library modules so that they are exported to make
749 javaSdkLibraries := javaSdkLibraries(mctx.Config())
750 javaSdkLibrariesLock.Lock()
751 defer javaSdkLibrariesLock.Unlock()
752 *javaSdkLibraries = append(*javaSdkLibraries, module.BaseModuleName())
753}
754
755func (module *SdkLibrary) InitSdkLibraryProperties() {
Sundong Ahn054b19a2018-10-19 13:46:09 +0900756 module.AddProperties(
757 &module.sdkLibraryProperties,
758 &module.Library.Module.properties,
759 &module.Library.Module.dexpreoptProperties,
760 &module.Library.Module.deviceProperties,
761 &module.Library.Module.protoProperties,
762 )
763
764 module.Library.Module.properties.Installable = proptools.BoolPtr(true)
765 module.Library.Module.deviceProperties.IsSDKLibrary = true
Inseob Kimc0907f12019-02-08 21:00:45 +0900766}
Sundong Ahn054b19a2018-10-19 13:46:09 +0900767
Jaewoong Jung4f158ee2019-07-11 10:05:35 -0700768// java_sdk_library is a special Java library that provides optional platform APIs to apps.
769// In practice, it can be viewed as a combination of several modules: 1) stubs library that clients
770// are linked against to, 2) droiddoc module that internally generates API stubs source files,
771// 3) the real runtime shared library that implements the APIs, and 4) XML file for adding
772// the runtime lib to the classpath at runtime if requested via <uses-library>.
Inseob Kimc0907f12019-02-08 21:00:45 +0900773func SdkLibraryFactory() android.Module {
774 module := &SdkLibrary{}
775 module.InitSdkLibraryProperties()
Jooyung Han58f26ab2019-12-18 15:34:32 +0900776 android.InitApexModule(module)
Sundong Ahn054b19a2018-10-19 13:46:09 +0900777 InitJavaModule(module, android.HostAndDeviceSupported)
Colin Crossf8b860a2019-04-16 14:43:28 -0700778 android.AddLoadHook(module, func(ctx android.LoadHookContext) { module.CreateInternalModules(ctx) })
Jiyong Parkc678ad32018-04-10 13:07:10 +0900779 return module
780}
Colin Cross79c7c262019-04-17 11:11:46 -0700781
782//
783// SDK library prebuilts
784//
785
786type sdkLibraryImportProperties struct {
787 Jars []string `android:"path"`
788
789 Sdk_version *string
790
791 Installable *bool
792
793 // List of shared java libs that this module has dependencies to
794 Libs []string
795
796 // List of files to remove from the jar file(s)
797 Exclude_files []string
798
799 // List of directories to remove from the jar file(s)
800 Exclude_dirs []string
801}
802
803type sdkLibraryImport struct {
804 android.ModuleBase
805 android.DefaultableModuleBase
806 prebuilt android.Prebuilt
807
808 properties sdkLibraryImportProperties
809
810 stubsPath android.Paths
811}
812
813var _ SdkLibraryDependency = (*sdkLibraryImport)(nil)
814
Jaewoong Jung4f158ee2019-07-11 10:05:35 -0700815// java_sdk_library_import imports a prebuilt java_sdk_library.
Colin Cross79c7c262019-04-17 11:11:46 -0700816func sdkLibraryImportFactory() android.Module {
817 module := &sdkLibraryImport{}
818
819 module.AddProperties(&module.properties)
820
821 android.InitPrebuiltModule(module, &module.properties.Jars)
822 InitJavaModule(module, android.HostAndDeviceSupported)
823
824 android.AddLoadHook(module, func(mctx android.LoadHookContext) { module.createInternalModules(mctx) })
825 return module
826}
827
828func (module *sdkLibraryImport) Prebuilt() *android.Prebuilt {
829 return &module.prebuilt
830}
831
832func (module *sdkLibraryImport) Name() string {
833 return module.prebuilt.Name(module.ModuleBase.Name())
834}
835
836func (module *sdkLibraryImport) createInternalModules(mctx android.LoadHookContext) {
837 // Creates a java import for the jar with ".stubs" suffix
838 props := struct {
Sundong Ahn0d7dff42019-12-04 12:53:44 +0900839 Name *string
840 Soc_specific *bool
841 Device_specific *bool
842 Product_specific *bool
843 System_ext_specific *bool
Colin Cross79c7c262019-04-17 11:11:46 -0700844 }{}
845
846 props.Name = proptools.StringPtr(module.BaseModuleName() + sdkStubsLibrarySuffix)
847
848 if module.SocSpecific() {
849 props.Soc_specific = proptools.BoolPtr(true)
850 } else if module.DeviceSpecific() {
851 props.Device_specific = proptools.BoolPtr(true)
852 } else if module.ProductSpecific() {
853 props.Product_specific = proptools.BoolPtr(true)
Sundong Ahn0d7dff42019-12-04 12:53:44 +0900854 } else if module.SystemExtSpecific() {
855 props.System_ext_specific = proptools.BoolPtr(true)
Colin Cross79c7c262019-04-17 11:11:46 -0700856 }
857
Colin Cross84dfc3d2019-09-25 11:33:01 -0700858 mctx.CreateModule(ImportFactory, &props, &module.properties)
Colin Cross79c7c262019-04-17 11:11:46 -0700859
860 javaSdkLibraries := javaSdkLibraries(mctx.Config())
861 javaSdkLibrariesLock.Lock()
862 defer javaSdkLibrariesLock.Unlock()
863 *javaSdkLibraries = append(*javaSdkLibraries, module.BaseModuleName())
864}
865
866func (module *sdkLibraryImport) DepsMutator(ctx android.BottomUpMutatorContext) {
867 // Add dependencies to the prebuilt stubs library
868 ctx.AddVariationDependencies(nil, publicApiStubsTag, module.BaseModuleName()+sdkStubsLibrarySuffix)
869}
870
871func (module *sdkLibraryImport) GenerateAndroidBuildActions(ctx android.ModuleContext) {
872 // Record the paths to the prebuilt stubs library.
873 ctx.VisitDirectDeps(func(to android.Module) {
874 tag := ctx.OtherModuleDependencyTag(to)
875
876 switch tag {
877 case publicApiStubsTag:
878 module.stubsPath = to.(Dependency).HeaderJars()
879 }
880 })
881}
882
883// to satisfy SdkLibraryDependency interface
Colin Cross0ea8ba82019-06-06 14:33:29 -0700884func (module *sdkLibraryImport) SdkHeaderJars(ctx android.BaseModuleContext, sdkVersion string) android.Paths {
Colin Cross79c7c262019-04-17 11:11:46 -0700885 // This module is just a wrapper for the prebuilt stubs.
886 return module.stubsPath
887}
888
889// to satisfy SdkLibraryDependency interface
Colin Cross0ea8ba82019-06-06 14:33:29 -0700890func (module *sdkLibraryImport) SdkImplementationJars(ctx android.BaseModuleContext, sdkVersion string) android.Paths {
Colin Cross79c7c262019-04-17 11:11:46 -0700891 // This module is just a wrapper for the stubs.
892 return module.stubsPath
893}