blob: f26f61c7703dc3efadb79d4405b0287e1e573753 [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 (
Jiyong Parkc678ad32018-04-10 13:07:10 +090018 "fmt"
19 "path"
Sundong Ahn054b19a2018-10-19 13:46:09 +090020 "path/filepath"
Paul Duffin6a2bd112020-04-07 19:27:04 +010021 "reflect"
Paul Duffin46fdda82020-05-14 15:39:10 +010022 "regexp"
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
Paul Duffind1b3a922020-01-22 11:57:20 +000027 "github.com/google/blueprint"
Jiyong Parkc678ad32018-04-10 13:07:10 +090028 "github.com/google/blueprint/proptools"
Paul Duffin6a2bd112020-04-07 19:27:04 +010029
30 "android/soong/android"
Jiyong Parkc678ad32018-04-10 13:07:10 +090031)
32
Jooyung Han58f26ab2019-12-18 15:34:32 +090033const (
Paul Duffin1c094a02020-05-08 15:52:37 +010034 sdkXmlFileSuffix = ".xml"
35 permissionsTemplate = `<?xml version=\"1.0\" encoding=\"utf-8\"?>\n` +
Jooyung Han624058e2019-12-24 18:38:06 +090036 `<!-- Copyright (C) 2018 The Android Open Source Project\n` +
37 `\n` +
Jiyong Parke3833882020-02-17 17:28:10 +090038 ` Licensed under the Apache License, Version 2.0 (the \"License\");\n` +
Jooyung Han624058e2019-12-24 18:38:06 +090039 ` you may not use this file except in compliance with the License.\n` +
40 ` You may obtain a copy of the License at\n` +
41 `\n` +
42 ` http://www.apache.org/licenses/LICENSE-2.0\n` +
43 `\n` +
44 ` Unless required by applicable law or agreed to in writing, software\n` +
Jiyong Parke3833882020-02-17 17:28:10 +090045 ` distributed under the License is distributed on an \"AS IS\" BASIS,\n` +
Jooyung Han624058e2019-12-24 18:38:06 +090046 ` WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\n` +
47 ` See the License for the specific language governing permissions and\n` +
48 ` limitations under the License.\n` +
49 `-->\n` +
50 `<permissions>\n` +
Jiyong Parke3833882020-02-17 17:28:10 +090051 ` <library name=\"%s\" file=\"%s\"/>\n` +
Jooyung Han624058e2019-12-24 18:38:06 +090052 `</permissions>\n`
Jiyong Parkc678ad32018-04-10 13:07:10 +090053)
54
Paul Duffind1b3a922020-01-22 11:57:20 +000055// A tag to associated a dependency with a specific api scope.
56type scopeDependencyTag struct {
57 blueprint.BaseDependencyTag
58 name string
59 apiScope *apiScope
Paul Duffin5fb82132020-04-29 20:45:27 +010060
61 // Function for extracting appropriate path information from the dependency.
62 depInfoExtractor func(paths *scopePaths, dep android.Module) error
63}
64
65// Extract tag specific information from the dependency.
66func (tag scopeDependencyTag) extractDepInfo(ctx android.ModuleContext, dep android.Module, paths *scopePaths) {
67 err := tag.depInfoExtractor(paths, dep)
68 if err != nil {
69 ctx.ModuleErrorf("has an invalid {scopeDependencyTag: %s} dependency on module %s: %s", tag.name, ctx.OtherModuleName(dep), err.Error())
70 }
Paul Duffind1b3a922020-01-22 11:57:20 +000071}
72
73// Provides information about an api scope, e.g. public, system, test.
74type apiScope struct {
75 // The name of the api scope, e.g. public, system, test
76 name string
77
Paul Duffin51a2bee2020-05-05 14:40:52 +010078 // The api scope that this scope extends.
79 extends *apiScope
80
Paul Duffin3a254982020-04-28 10:44:03 +010081 // The legacy enabled status for a specific scope can be dependent on other
82 // properties that have been specified on the library so it is provided by
83 // a function that can determine the status by examining those properties.
84 legacyEnabledStatus func(module *SdkLibrary) bool
85
86 // The default enabled status for non-legacy behavior, which is triggered by
87 // explicitly enabling at least one api scope.
88 defaultEnabledStatus bool
89
90 // Gets a pointer to the scope specific properties.
91 scopeSpecificProperties func(module *SdkLibrary) *ApiScopeProperties
92
Paul Duffin6a2bd112020-04-07 19:27:04 +010093 // The name of the field in the dynamically created structure.
94 fieldName string
95
Paul Duffin0f270632020-05-13 19:19:49 +010096 // The name of the property in the java_sdk_library_import
97 propertyName string
98
Paul Duffind1b3a922020-01-22 11:57:20 +000099 // The tag to use to depend on the stubs library module.
100 stubsTag scopeDependencyTag
101
Paul Duffina377e4c2020-04-29 13:30:54 +0100102 // The tag to use to depend on the stubs source module (if separate from the API module).
103 stubsSourceTag scopeDependencyTag
104
105 // The tag to use to depend on the API file generating module (if separate from the stubs source module).
106 apiFileTag scopeDependencyTag
107
Paul Duffin5fb82132020-04-29 20:45:27 +0100108 // The tag to use to depend on the stubs source and API module.
109 stubsSourceAndApiTag scopeDependencyTag
Paul Duffind1b3a922020-01-22 11:57:20 +0000110
111 // The scope specific prefix to add to the api file base of "current.txt" or "removed.txt".
112 apiFilePrefix string
113
114 // The scope specific prefix to add to the sdk library module name to construct a scope specific
115 // module name.
116 moduleSuffix string
117
Paul Duffind1b3a922020-01-22 11:57:20 +0000118 // SDK version that the stubs library is built against. Note that this is always
119 // *current. Older stubs library built with a numbered SDK version is created from
120 // the prebuilt jar.
121 sdkVersion string
Paul Duffin3c7c3472020-04-07 18:50:10 +0100122
Paul Duffin6e91aac2020-07-20 18:04:44 +0100123 // The annotation that identifies this API level, empty for the public API scope.
124 annotation string
125
Paul Duffin3c7c3472020-04-07 18:50:10 +0100126 // Extra arguments to pass to droidstubs for this scope.
Paul Duffina377e4c2020-04-29 13:30:54 +0100127 //
Paul Duffin6e91aac2020-07-20 18:04:44 +0100128 // This is not used directly but is used to construct the droidstubsArgs.
129 extraArgs []string
Paul Duffina377e4c2020-04-29 13:30:54 +0100130
Paul Duffin6e91aac2020-07-20 18:04:44 +0100131 // The args that must be passed to droidstubs to generate the API and stubs source
132 // for this scope, constructed dynamically by initApiScope().
Paul Duffina377e4c2020-04-29 13:30:54 +0100133 //
134 // The API only includes the additional members that this scope adds over the scope
135 // that it extends.
Paul Duffin6e91aac2020-07-20 18:04:44 +0100136 //
137 // The stubs source must include the definitions of everything that is in this
138 // api scope and all the scopes that this one extends.
139 droidstubsArgs []string
Paul Duffina377e4c2020-04-29 13:30:54 +0100140
Anton Hansson5ff28e52020-05-02 11:19:36 +0100141 // Whether the api scope can be treated as unstable, and should skip compat checks.
142 unstable bool
Paul Duffind1b3a922020-01-22 11:57:20 +0000143}
144
145// Initialize a scope, creating and adding appropriate dependency tags
146func initApiScope(scope *apiScope) *apiScope {
Paul Duffin5fb82132020-04-29 20:45:27 +0100147 name := scope.name
Paul Duffin46fdda82020-05-14 15:39:10 +0100148 scopeByName[name] = scope
149 allScopeNames = append(allScopeNames, name)
Paul Duffin0f270632020-05-13 19:19:49 +0100150 scope.propertyName = strings.ReplaceAll(name, "-", "_")
151 scope.fieldName = proptools.FieldNameForProperty(scope.propertyName)
Paul Duffind1b3a922020-01-22 11:57:20 +0000152 scope.stubsTag = scopeDependencyTag{
Paul Duffin5fb82132020-04-29 20:45:27 +0100153 name: name + "-stubs",
154 apiScope: scope,
155 depInfoExtractor: (*scopePaths).extractStubsLibraryInfoFromDependency,
Paul Duffind1b3a922020-01-22 11:57:20 +0000156 }
Paul Duffina377e4c2020-04-29 13:30:54 +0100157 scope.stubsSourceTag = scopeDependencyTag{
158 name: name + "-stubs-source",
159 apiScope: scope,
160 depInfoExtractor: (*scopePaths).extractStubsSourceInfoFromDep,
161 }
162 scope.apiFileTag = scopeDependencyTag{
163 name: name + "-api",
164 apiScope: scope,
165 depInfoExtractor: (*scopePaths).extractApiInfoFromDep,
166 }
Paul Duffin5fb82132020-04-29 20:45:27 +0100167 scope.stubsSourceAndApiTag = scopeDependencyTag{
168 name: name + "-stubs-source-and-api",
169 apiScope: scope,
170 depInfoExtractor: (*scopePaths).extractStubsSourceAndApiInfoFromApiStubsProvider,
Paul Duffind1b3a922020-01-22 11:57:20 +0000171 }
Paul Duffina377e4c2020-04-29 13:30:54 +0100172
173 // To get the args needed to generate the stubs source append all the args from
174 // this scope and all the scopes it extends as each set of args adds additional
175 // members to the stubs.
Paul Duffin6e91aac2020-07-20 18:04:44 +0100176 var scopeSpecificArgs []string
177 if scope.annotation != "" {
178 scopeSpecificArgs = []string{"--show-annotation", scope.annotation}
Paul Duffina377e4c2020-04-29 13:30:54 +0100179 }
Paul Duffin6e91aac2020-07-20 18:04:44 +0100180 for s := scope; s != nil; s = s.extends {
181 scopeSpecificArgs = append(scopeSpecificArgs, s.extraArgs...)
Paul Duffina377e4c2020-04-29 13:30:54 +0100182
Paul Duffin6e91aac2020-07-20 18:04:44 +0100183 // Ensure that the generated stubs includes all the API elements from the API scope
184 // that this scope extends.
185 if s != scope && s.annotation != "" {
186 scopeSpecificArgs = append(scopeSpecificArgs, "--show-for-stub-purposes-annotation", s.annotation)
187 }
188 }
Paul Duffina377e4c2020-04-29 13:30:54 +0100189
Paul Duffin6e91aac2020-07-20 18:04:44 +0100190 // Escape any special characters in the arguments. This is needed because droidstubs
191 // passes these directly to the shell command.
192 scope.droidstubsArgs = proptools.ShellEscapeList(scopeSpecificArgs)
Paul Duffina377e4c2020-04-29 13:30:54 +0100193
Paul Duffind1b3a922020-01-22 11:57:20 +0000194 return scope
195}
196
Paul Duffinb74ee3e2020-05-08 14:16:20 +0100197func (scope *apiScope) stubsLibraryModuleName(baseName string) string {
Paul Duffin1c094a02020-05-08 15:52:37 +0100198 return baseName + ".stubs" + scope.moduleSuffix
Paul Duffind1b3a922020-01-22 11:57:20 +0000199}
200
Paul Duffin5fb82132020-04-29 20:45:27 +0100201func (scope *apiScope) stubsSourceModuleName(baseName string) string {
Paul Duffin1c094a02020-05-08 15:52:37 +0100202 return baseName + ".stubs.source" + scope.moduleSuffix
Paul Duffind1b3a922020-01-22 11:57:20 +0000203}
204
Paul Duffina377e4c2020-04-29 13:30:54 +0100205func (scope *apiScope) apiModuleName(baseName string) string {
Paul Duffin1c094a02020-05-08 15:52:37 +0100206 return baseName + ".api" + scope.moduleSuffix
Paul Duffina377e4c2020-04-29 13:30:54 +0100207}
208
Paul Duffin3a254982020-04-28 10:44:03 +0100209func (scope *apiScope) String() string {
210 return scope.name
211}
212
Paul Duffind1b3a922020-01-22 11:57:20 +0000213type apiScopes []*apiScope
214
215func (scopes apiScopes) Strings(accessor func(*apiScope) string) []string {
216 var list []string
217 for _, scope := range scopes {
218 list = append(list, accessor(scope))
219 }
220 return list
221}
222
Jiyong Parkc678ad32018-04-10 13:07:10 +0900223var (
Paul Duffin46fdda82020-05-14 15:39:10 +0100224 scopeByName = make(map[string]*apiScope)
225 allScopeNames []string
Paul Duffind1b3a922020-01-22 11:57:20 +0000226 apiScopePublic = initApiScope(&apiScope{
Paul Duffin3a254982020-04-28 10:44:03 +0100227 name: "public",
228
229 // Public scope is enabled by default for both legacy and non-legacy modes.
230 legacyEnabledStatus: func(module *SdkLibrary) bool {
231 return true
232 },
233 defaultEnabledStatus: true,
234
235 scopeSpecificProperties: func(module *SdkLibrary) *ApiScopeProperties {
236 return &module.sdkLibraryProperties.Public
237 },
Paul Duffind1b3a922020-01-22 11:57:20 +0000238 sdkVersion: "current",
239 })
240 apiScopeSystem = initApiScope(&apiScope{
Paul Duffin3a254982020-04-28 10:44:03 +0100241 name: "system",
242 extends: apiScopePublic,
243 legacyEnabledStatus: (*SdkLibrary).generateTestAndSystemScopesByDefault,
244 scopeSpecificProperties: func(module *SdkLibrary) *ApiScopeProperties {
245 return &module.sdkLibraryProperties.System
246 },
Paul Duffin6e91aac2020-07-20 18:04:44 +0100247 apiFilePrefix: "system-",
248 moduleSuffix: ".system",
249 sdkVersion: "system_current",
250 annotation: "android.annotation.SystemApi(client=android.annotation.SystemApi.Client.PRIVILEGED_APPS)",
Paul Duffind1b3a922020-01-22 11:57:20 +0000251 })
252 apiScopeTest = initApiScope(&apiScope{
Paul Duffin3a254982020-04-28 10:44:03 +0100253 name: "test",
Anton Hansson21b51f12020-10-09 10:16:49 +0100254 extends: apiScopeSystem,
Paul Duffin3a254982020-04-28 10:44:03 +0100255 legacyEnabledStatus: (*SdkLibrary).generateTestAndSystemScopesByDefault,
256 scopeSpecificProperties: func(module *SdkLibrary) *ApiScopeProperties {
257 return &module.sdkLibraryProperties.Test
258 },
Paul Duffin6e91aac2020-07-20 18:04:44 +0100259 apiFilePrefix: "test-",
260 moduleSuffix: ".test",
261 sdkVersion: "test_current",
262 annotation: "android.annotation.TestApi",
263 unstable: true,
Paul Duffind1b3a922020-01-22 11:57:20 +0000264 })
Paul Duffin6d7f0a72020-04-28 14:13:56 +0100265 apiScopeModuleLib = initApiScope(&apiScope{
Paul Duffin0f270632020-05-13 19:19:49 +0100266 name: "module-lib",
Paul Duffin6d7f0a72020-04-28 14:13:56 +0100267 extends: apiScopeSystem,
Paul Duffin5a757b12020-06-02 13:00:08 +0100268 // The module-lib scope is disabled by default in legacy mode.
Paul Duffin6d7f0a72020-04-28 14:13:56 +0100269 //
270 // Enabling this would break existing usages.
271 legacyEnabledStatus: func(module *SdkLibrary) bool {
272 return false
273 },
274 scopeSpecificProperties: func(module *SdkLibrary) *ApiScopeProperties {
275 return &module.sdkLibraryProperties.Module_lib
276 },
277 apiFilePrefix: "module-lib-",
278 moduleSuffix: ".module_lib",
279 sdkVersion: "module_current",
Paul Duffin6e91aac2020-07-20 18:04:44 +0100280 annotation: "android.annotation.SystemApi(client=android.annotation.SystemApi.Client.MODULE_LIBRARIES)",
Paul Duffin6d7f0a72020-04-28 14:13:56 +0100281 })
Paul Duffin5a757b12020-06-02 13:00:08 +0100282 apiScopeSystemServer = initApiScope(&apiScope{
283 name: "system-server",
284 extends: apiScopePublic,
285 // The system-server scope is disabled by default in legacy mode.
286 //
287 // Enabling this would break existing usages.
288 legacyEnabledStatus: func(module *SdkLibrary) bool {
289 return false
290 },
291 scopeSpecificProperties: func(module *SdkLibrary) *ApiScopeProperties {
292 return &module.sdkLibraryProperties.System_server
293 },
294 apiFilePrefix: "system-server-",
295 moduleSuffix: ".system_server",
296 sdkVersion: "system_server_current",
Paul Duffin6e91aac2020-07-20 18:04:44 +0100297 annotation: "android.annotation.SystemApi(client=android.annotation.SystemApi.Client.SYSTEM_SERVER)",
298 extraArgs: []string{
299 "--hide-annotation", "android.annotation.Hide",
Paul Duffin5a757b12020-06-02 13:00:08 +0100300 // com.android.* classes are okay in this interface"
Paul Duffin6e91aac2020-07-20 18:04:44 +0100301 "--hide", "InternalClasses",
Paul Duffin5a757b12020-06-02 13:00:08 +0100302 },
303 })
Paul Duffind1b3a922020-01-22 11:57:20 +0000304 allApiScopes = apiScopes{
305 apiScopePublic,
306 apiScopeSystem,
307 apiScopeTest,
Paul Duffin6d7f0a72020-04-28 14:13:56 +0100308 apiScopeModuleLib,
Paul Duffin5a757b12020-06-02 13:00:08 +0100309 apiScopeSystemServer,
Paul Duffind1b3a922020-01-22 11:57:20 +0000310 }
Jiyong Parkc678ad32018-04-10 13:07:10 +0900311)
312
Jiyong Park82484c02018-04-23 21:41:26 +0900313var (
314 javaSdkLibrariesLock sync.Mutex
315)
316
Jiyong Parkc678ad32018-04-10 13:07:10 +0900317// TODO: these are big features that are currently missing
Jiyong Park1be96912018-05-28 18:02:19 +0900318// 1) disallowing linking to the runtime shared lib
319// 2) HTML generation
Jiyong Parkc678ad32018-04-10 13:07:10 +0900320
321func init() {
Paul Duffin43dc1cc2019-12-19 11:18:54 +0000322 RegisterSdkLibraryBuildComponents(android.InitRegistrationContext)
Jiyong Parkc678ad32018-04-10 13:07:10 +0900323
Jiyong Park82484c02018-04-23 21:41:26 +0900324 android.RegisterMakeVarsProvider(pctx, func(ctx android.MakeVarsContext) {
325 javaSdkLibraries := javaSdkLibraries(ctx.Config())
326 sort.Strings(*javaSdkLibraries)
327 ctx.Strict("JAVA_SDK_LIBRARIES", strings.Join(*javaSdkLibraries, " "))
328 })
Paul Duffin61871622020-02-10 13:37:10 +0000329
330 // Register sdk member types.
331 android.RegisterSdkMemberType(&sdkLibrarySdkMemberType{
332 android.SdkMemberTypeBase{
333 PropertyName: "java_sdk_libs",
334 SupportsSdk: true,
335 },
336 })
Jiyong Parkc678ad32018-04-10 13:07:10 +0900337}
338
Paul Duffin43dc1cc2019-12-19 11:18:54 +0000339func RegisterSdkLibraryBuildComponents(ctx android.RegistrationContext) {
340 ctx.RegisterModuleType("java_sdk_library", SdkLibraryFactory)
341 ctx.RegisterModuleType("java_sdk_library_import", sdkLibraryImportFactory)
342}
343
Paul Duffin3a254982020-04-28 10:44:03 +0100344// Properties associated with each api scope.
345type ApiScopeProperties struct {
346 // Indicates whether the api surface is generated.
347 //
348 // If this is set for any scope then all scopes must explicitly specify if they
349 // are enabled. This is to prevent new usages from depending on legacy behavior.
350 //
351 // Otherwise, if this is not set for any scope then the default behavior is
352 // scope specific so please refer to the scope specific property documentation.
353 Enabled *bool
Paul Duffin080f5ee2020-05-12 11:50:28 +0100354
355 // The sdk_version to use for building the stubs.
356 //
357 // If not specified then it will use an sdk_version determined as follows:
358 // 1) If the sdk_version specified on the java_sdk_library is none then this
359 // will be none. This is used for java_sdk_library instances that are used
360 // to create stubs that contribute to the core_current sdk version.
361 // 2) Otherwise, it is assumed that this library extends but does not contribute
362 // directly to a specific sdk_version and so this uses the sdk_version appropriate
363 // for the api scope. e.g. public will use sdk_version: current, system will use
364 // sdk_version: system_current, etc.
365 //
366 // This does not affect the sdk_version used for either generating the stubs source
367 // or the API file. They both have to use the same sdk_version as is used for
368 // compiling the implementation library.
369 Sdk_version *string
Paul Duffin3a254982020-04-28 10:44:03 +0100370}
371
Jiyong Parkc678ad32018-04-10 13:07:10 +0900372type sdkLibraryProperties struct {
Paul Duffin9d582cc2020-05-16 15:52:12 +0100373 // Visibility for impl library module. If not specified then defaults to the
374 // visibility property.
375 Impl_library_visibility []string
376
Paul Duffin344c4ee2020-04-29 23:35:13 +0100377 // Visibility for stubs library modules. If not specified then defaults to the
378 // visibility property.
379 Stubs_library_visibility []string
380
381 // Visibility for stubs source modules. If not specified then defaults to the
382 // visibility property.
383 Stubs_source_visibility []string
384
Sundong Ahnf043cf62018-06-25 16:04:37 +0900385 // List of Java libraries that will be in the classpath when building stubs
386 Stub_only_libs []string `android:"arch_variant"`
387
Paul Duffin7a586d32019-12-30 17:09:34 +0000388 // list of package names that will be documented and publicized as API.
389 // This allows the API to be restricted to a subset of the source files provided.
390 // If this is unspecified then all the source files will be treated as being part
391 // of the API.
Jiyong Parkc678ad32018-04-10 13:07:10 +0900392 Api_packages []string
393
Jiyong Park5a2c9d72018-05-01 22:25:41 +0900394 // list of package names that must be hidden from the API
395 Hidden_api_packages []string
396
Paul Duffin749f98f2019-12-30 17:23:46 +0000397 // the relative path to the directory containing the api specification files.
398 // Defaults to "api".
399 Api_dir *string
400
Paul Duffind11e78e2020-05-15 20:37:11 +0100401 // Determines whether a runtime implementation library is built; defaults to false.
402 //
403 // If true then it also prevents the module from being used as a shared module, i.e.
404 // it is as is shared_library: false, was set.
Paul Duffin43db9be2019-12-30 17:35:49 +0000405 Api_only *bool
406
Paul Duffin11512472019-02-11 15:55:17 +0000407 // local files that are used within user customized droiddoc options.
408 Droiddoc_option_files []string
409
410 // additional droiddoc options
411 // Available variables for substitution:
412 //
413 // $(location <label>): the path to the droiddoc_option_files with name <label>
Sundong Ahndd567f92018-07-31 17:19:11 +0900414 Droiddoc_options []string
415
Paul Duffin2ce1e812020-05-20 19:35:27 +0100416 // is set to true, Metalava will allow framework SDK to contain annotations.
417 Annotations_enabled *bool
418
Sundong Ahn054b19a2018-10-19 13:46:09 +0900419 // a list of top-level directories containing files to merge qualifier annotations
420 // (i.e. those intended to be included in the stubs written) from.
421 Merge_annotations_dirs []string
422
423 // a list of top-level directories containing Java stub files to merge show/hide annotations from.
424 Merge_inclusion_annotations_dirs []string
425
426 // If set to true, the path of dist files is apistubs/core. Defaults to false.
427 Core_lib *bool
428
Sundong Ahn80a87b32019-05-13 15:02:50 +0900429 // don't create dist rules.
430 No_dist *bool `blueprint:"mutated"`
431
Paul Duffin3a254982020-04-28 10:44:03 +0100432 // indicates whether system and test apis should be generated.
433 Generate_system_and_test_apis bool `blueprint:"mutated"`
434
435 // The properties specific to the public api scope
436 //
437 // Unless explicitly specified by using public.enabled the public api scope is
438 // enabled by default in both legacy and non-legacy mode.
439 Public ApiScopeProperties
440
441 // The properties specific to the system api scope
442 //
443 // In legacy mode the system api scope is enabled by default when sdk_version
444 // is set to something other than "none".
445 //
446 // In non-legacy mode the system api scope is disabled by default.
447 System ApiScopeProperties
448
449 // The properties specific to the test api scope
450 //
451 // In legacy mode the test api scope is enabled by default when sdk_version
452 // is set to something other than "none".
453 //
454 // In non-legacy mode the test api scope is disabled by default.
455 Test ApiScopeProperties
Paul Duffin37e0b772019-12-30 17:20:10 +0000456
Paul Duffin5a757b12020-06-02 13:00:08 +0100457 // The properties specific to the module-lib api scope
Paul Duffin6d7f0a72020-04-28 14:13:56 +0100458 //
Paul Duffin5a757b12020-06-02 13:00:08 +0100459 // Unless explicitly specified by using test.enabled the module-lib api scope is
Paul Duffin6d7f0a72020-04-28 14:13:56 +0100460 // disabled by default.
461 Module_lib ApiScopeProperties
462
Paul Duffin5a757b12020-06-02 13:00:08 +0100463 // The properties specific to the system-server api scope
464 //
465 // Unless explicitly specified by using test.enabled the module-lib api scope is
466 // disabled by default.
467 System_server ApiScopeProperties
468
Jiyong Park27fc4142020-05-28 00:19:53 +0900469 // Determines if the stubs are preferred over the implementation library
470 // for linking, even when the client doesn't specify sdk_version. When this
471 // is set to true, such clients are provided with the widest API surface that
472 // this lib provides. Note however that this option doesn't affect the clients
473 // that are in the same APEX as this library. In that case, the clients are
474 // always linked with the implementation library. Default is false.
475 Default_to_stubs *bool
476
Paul Duffin8986cc92020-05-10 19:32:20 +0100477 // Properties related to api linting.
478 Api_lint struct {
479 // Enable api linting.
480 Enabled *bool
481 }
482
Jiyong Parkc678ad32018-04-10 13:07:10 +0900483 // TODO: determines whether to create HTML doc or not
484 //Html_doc *bool
485}
486
Paul Duffin533f9c72020-05-20 16:18:00 +0100487// Paths to outputs from java_sdk_library and java_sdk_library_import.
488//
489// Fields that are android.Paths are always set (during GenerateAndroidBuildActions).
490// OptionalPaths are always set by java_sdk_library but may not be set by
491// java_sdk_library_import as not all instances provide that information.
Paul Duffind1b3a922020-01-22 11:57:20 +0000492type scopePaths struct {
Paul Duffin533f9c72020-05-20 16:18:00 +0100493 // The path (represented as Paths for convenience when returning) to the stubs header jar.
494 //
495 // That is the jar that is created by turbine.
496 stubsHeaderPath android.Paths
497
498 // The path (represented as Paths for convenience when returning) to the stubs implementation jar.
499 //
500 // This is not the implementation jar, it still only contains stubs.
501 stubsImplPath android.Paths
502
503 // The API specification file, e.g. system_current.txt.
504 currentApiFilePath android.OptionalPath
505
506 // The specification of API elements removed since the last release.
507 removedApiFilePath android.OptionalPath
508
509 // The stubs source jar.
510 stubsSrcJar android.OptionalPath
Paul Duffind1b3a922020-01-22 11:57:20 +0000511}
512
Paul Duffin5fb82132020-04-29 20:45:27 +0100513func (paths *scopePaths) extractStubsLibraryInfoFromDependency(dep android.Module) error {
514 if lib, ok := dep.(Dependency); ok {
515 paths.stubsHeaderPath = lib.HeaderJars()
516 paths.stubsImplPath = lib.ImplementationJars()
517 return nil
518 } else {
519 return fmt.Errorf("expected module that implements Dependency, e.g. java_library")
520 }
521}
522
Paul Duffina377e4c2020-04-29 13:30:54 +0100523func (paths *scopePaths) treatDepAsApiStubsProvider(dep android.Module, action func(provider ApiStubsProvider)) error {
524 if apiStubsProvider, ok := dep.(ApiStubsProvider); ok {
525 action(apiStubsProvider)
Paul Duffin5fb82132020-04-29 20:45:27 +0100526 return nil
527 } else {
528 return fmt.Errorf("expected module that implements ApiStubsProvider, e.g. droidstubs")
529 }
530}
531
Paul Duffin533f9c72020-05-20 16:18:00 +0100532func (paths *scopePaths) treatDepAsApiStubsSrcProvider(dep android.Module, action func(provider ApiStubsSrcProvider)) error {
533 if apiStubsProvider, ok := dep.(ApiStubsSrcProvider); ok {
534 action(apiStubsProvider)
535 return nil
536 } else {
537 return fmt.Errorf("expected module that implements ApiStubsSrcProvider, e.g. droidstubs")
538 }
539}
540
Paul Duffina377e4c2020-04-29 13:30:54 +0100541func (paths *scopePaths) extractApiInfoFromApiStubsProvider(provider ApiStubsProvider) {
Paul Duffin533f9c72020-05-20 16:18:00 +0100542 paths.currentApiFilePath = android.OptionalPathForPath(provider.ApiFilePath())
543 paths.removedApiFilePath = android.OptionalPathForPath(provider.RemovedApiFilePath())
Paul Duffina377e4c2020-04-29 13:30:54 +0100544}
545
546func (paths *scopePaths) extractApiInfoFromDep(dep android.Module) error {
547 return paths.treatDepAsApiStubsProvider(dep, func(provider ApiStubsProvider) {
548 paths.extractApiInfoFromApiStubsProvider(provider)
549 })
550}
551
Paul Duffin533f9c72020-05-20 16:18:00 +0100552func (paths *scopePaths) extractStubsSourceInfoFromApiStubsProviders(provider ApiStubsSrcProvider) {
553 paths.stubsSrcJar = android.OptionalPathForPath(provider.StubsSrcJar())
Paul Duffina377e4c2020-04-29 13:30:54 +0100554}
555
556func (paths *scopePaths) extractStubsSourceInfoFromDep(dep android.Module) error {
Paul Duffin533f9c72020-05-20 16:18:00 +0100557 return paths.treatDepAsApiStubsSrcProvider(dep, func(provider ApiStubsSrcProvider) {
Paul Duffina377e4c2020-04-29 13:30:54 +0100558 paths.extractStubsSourceInfoFromApiStubsProviders(provider)
559 })
560}
561
562func (paths *scopePaths) extractStubsSourceAndApiInfoFromApiStubsProvider(dep android.Module) error {
563 return paths.treatDepAsApiStubsProvider(dep, func(provider ApiStubsProvider) {
564 paths.extractApiInfoFromApiStubsProvider(provider)
565 paths.extractStubsSourceInfoFromApiStubsProviders(provider)
566 })
567}
568
569type commonToSdkLibraryAndImportProperties struct {
Paul Duffin1a724e62020-05-08 13:44:43 +0100570 // The naming scheme to use for the components that this module creates.
571 //
Paul Duffindef8a892020-05-08 15:36:30 +0100572 // If not specified then it defaults to "default". The other allowable value is
573 // "framework-modules" which matches the scheme currently used by framework modules
574 // for the equivalent components represented as separate Soong modules.
Paul Duffin1a724e62020-05-08 13:44:43 +0100575 //
576 // This is a temporary mechanism to simplify conversion from separate modules for each
577 // component that follow a different naming pattern to the default one.
578 //
579 // TODO(b/155480189) - Remove once naming inconsistencies have been resolved.
Paul Duffina377e4c2020-04-29 13:30:54 +0100580 Naming_scheme *string
Paul Duffind11e78e2020-05-15 20:37:11 +0100581
582 // Specifies whether this module can be used as an Android shared library; defaults
583 // to true.
584 //
585 // An Android shared library is one that can be referenced in a <uses-library> element
586 // in an AndroidManifest.xml.
587 Shared_library *bool
Paul Duffina377e4c2020-04-29 13:30:54 +0100588}
589
Paul Duffin56d44902020-01-31 13:36:25 +0000590// Common code between sdk library and sdk library import
591type commonToSdkLibraryAndImport struct {
Paul Duffinb74ee3e2020-05-08 14:16:20 +0100592 moduleBase *android.ModuleBase
593
Paul Duffin56d44902020-01-31 13:36:25 +0000594 scopePaths map[*apiScope]*scopePaths
Paul Duffin1a724e62020-05-08 13:44:43 +0100595
596 namingScheme sdkLibraryComponentNamingScheme
597
Paul Duffind11e78e2020-05-15 20:37:11 +0100598 commonSdkLibraryProperties commonToSdkLibraryAndImportProperties
Paul Duffin64e61992020-05-15 10:20:31 +0100599
600 // Functionality related to this being used as a component of a java_sdk_library.
601 EmbeddableSdkLibraryComponent
Paul Duffin56d44902020-01-31 13:36:25 +0000602}
603
Paul Duffinb74ee3e2020-05-08 14:16:20 +0100604func (c *commonToSdkLibraryAndImport) initCommon(moduleBase *android.ModuleBase) {
605 c.moduleBase = moduleBase
Paul Duffin1a724e62020-05-08 13:44:43 +0100606
Paul Duffind11e78e2020-05-15 20:37:11 +0100607 moduleBase.AddProperties(&c.commonSdkLibraryProperties)
Paul Duffin64e61992020-05-15 10:20:31 +0100608
609 // Initialize this as an sdk library component.
610 c.initSdkLibraryComponent(moduleBase)
Paul Duffin1a724e62020-05-08 13:44:43 +0100611}
612
613func (c *commonToSdkLibraryAndImport) initCommonAfterDefaultsApplied(ctx android.DefaultableHookContext) bool {
Paul Duffind11e78e2020-05-15 20:37:11 +0100614 schemeProperty := proptools.StringDefault(c.commonSdkLibraryProperties.Naming_scheme, "default")
Paul Duffin1a724e62020-05-08 13:44:43 +0100615 switch schemeProperty {
616 case "default":
617 c.namingScheme = &defaultNamingScheme{}
Paul Duffindef8a892020-05-08 15:36:30 +0100618 case "framework-modules":
619 c.namingScheme = &frameworkModulesNamingScheme{}
Paul Duffin1a724e62020-05-08 13:44:43 +0100620 default:
621 ctx.PropertyErrorf("naming_scheme", "expected 'default' but was %q", schemeProperty)
622 return false
623 }
624
Paul Duffind11e78e2020-05-15 20:37:11 +0100625 // Only track this sdk library if this can be used as a shared library.
626 if c.sharedLibrary() {
627 // Use the name specified in the module definition as the owner.
628 c.sdkLibraryComponentProperties.SdkLibraryToImplicitlyTrack = proptools.StringPtr(c.moduleBase.BaseModuleName())
629 }
Paul Duffin64e61992020-05-15 10:20:31 +0100630
Paul Duffin1a724e62020-05-08 13:44:43 +0100631 return true
Paul Duffinb74ee3e2020-05-08 14:16:20 +0100632}
633
Paul Duffinf642a312020-06-12 17:46:39 +0100634// Module name of the runtime implementation library
635func (c *commonToSdkLibraryAndImport) implLibraryModuleName() string {
636 return c.moduleBase.BaseModuleName() + ".impl"
637}
638
639// Module name of the XML file for the lib
640func (c *commonToSdkLibraryAndImport) xmlPermissionsModuleName() string {
641 return c.moduleBase.BaseModuleName() + sdkXmlFileSuffix
642}
643
Paul Duffinb74ee3e2020-05-08 14:16:20 +0100644// Name of the java_library module that compiles the stubs source.
645func (c *commonToSdkLibraryAndImport) stubsLibraryModuleName(apiScope *apiScope) string {
Paul Duffin1a724e62020-05-08 13:44:43 +0100646 return c.namingScheme.stubsLibraryModuleName(apiScope, c.moduleBase.BaseModuleName())
Paul Duffinb74ee3e2020-05-08 14:16:20 +0100647}
648
649// Name of the droidstubs module that generates the stubs source and may also
650// generate/check the API.
651func (c *commonToSdkLibraryAndImport) stubsSourceModuleName(apiScope *apiScope) string {
Paul Duffin1a724e62020-05-08 13:44:43 +0100652 return c.namingScheme.stubsSourceModuleName(apiScope, c.moduleBase.BaseModuleName())
Paul Duffinb74ee3e2020-05-08 14:16:20 +0100653}
654
655// Name of the droidstubs module that generates/checks the API. Only used if it
656// requires different arts to the stubs source generating module.
657func (c *commonToSdkLibraryAndImport) apiModuleName(apiScope *apiScope) string {
Paul Duffin1a724e62020-05-08 13:44:43 +0100658 return c.namingScheme.apiModuleName(apiScope, c.moduleBase.BaseModuleName())
Paul Duffinb74ee3e2020-05-08 14:16:20 +0100659}
660
Paul Duffin46fdda82020-05-14 15:39:10 +0100661// The component names for different outputs of the java_sdk_library.
662//
663// They are similar to the names used for the child modules it creates
664const (
665 stubsSourceComponentName = "stubs.source"
666
667 apiTxtComponentName = "api.txt"
668
669 removedApiTxtComponentName = "removed-api.txt"
670)
671
672// A regular expression to match tags that reference a specific stubs component.
673//
674// It will only match if given a valid scope and a valid component. It is verfy strict
675// to ensure it does not accidentally match a similar looking tag that should be processed
676// by the embedded Library.
677var tagSplitter = func() *regexp.Regexp {
678 // Given a list of literal string items returns a regular expression that will
679 // match any one of the items.
680 choice := func(items ...string) string {
681 return `\Q` + strings.Join(items, `\E|\Q`) + `\E`
682 }
683
684 // Regular expression to match one of the scopes.
685 scopesRegexp := choice(allScopeNames...)
686
687 // Regular expression to match one of the components.
688 componentsRegexp := choice(stubsSourceComponentName, apiTxtComponentName, removedApiTxtComponentName)
689
690 // Regular expression to match any combination of one scope and one component.
691 return regexp.MustCompile(fmt.Sprintf(`^\.(%s)\.(%s)$`, scopesRegexp, componentsRegexp))
692}()
693
694// For OutputFileProducer interface
695//
696// .<scope>.stubs.source
697// .<scope>.api.txt
698// .<scope>.removed-api.txt
699func (c *commonToSdkLibraryAndImport) commonOutputFiles(tag string) (android.Paths, error) {
700 if groups := tagSplitter.FindStringSubmatch(tag); groups != nil {
701 scopeName := groups[1]
702 component := groups[2]
703
704 if scope, ok := scopeByName[scopeName]; ok {
705 paths := c.findScopePaths(scope)
706 if paths == nil {
707 return nil, fmt.Errorf("%q does not provide api scope %s", c.moduleBase.BaseModuleName(), scopeName)
708 }
709
710 switch component {
711 case stubsSourceComponentName:
712 if paths.stubsSrcJar.Valid() {
713 return android.Paths{paths.stubsSrcJar.Path()}, nil
714 }
715
716 case apiTxtComponentName:
717 if paths.currentApiFilePath.Valid() {
718 return android.Paths{paths.currentApiFilePath.Path()}, nil
719 }
720
721 case removedApiTxtComponentName:
722 if paths.removedApiFilePath.Valid() {
723 return android.Paths{paths.removedApiFilePath.Path()}, nil
724 }
725 }
726
727 return nil, fmt.Errorf("%s not available for api scope %s", component, scopeName)
728 } else {
729 return nil, fmt.Errorf("unknown scope %s in %s", scope, tag)
730 }
731
732 } else {
733 return nil, nil
734 }
735}
736
Paul Duffin5ae30792020-05-20 11:52:25 +0100737func (c *commonToSdkLibraryAndImport) getScopePathsCreateIfNeeded(scope *apiScope) *scopePaths {
Paul Duffin56d44902020-01-31 13:36:25 +0000738 if c.scopePaths == nil {
739 c.scopePaths = make(map[*apiScope]*scopePaths)
740 }
741 paths := c.scopePaths[scope]
742 if paths == nil {
743 paths = &scopePaths{}
744 c.scopePaths[scope] = paths
745 }
746
747 return paths
748}
749
Paul Duffin5ae30792020-05-20 11:52:25 +0100750func (c *commonToSdkLibraryAndImport) findScopePaths(scope *apiScope) *scopePaths {
751 if c.scopePaths == nil {
752 return nil
753 }
754
755 return c.scopePaths[scope]
756}
757
758// If this does not support the requested api scope then find the closest available
759// scope it does support. Returns nil if no such scope is available.
760func (c *commonToSdkLibraryAndImport) findClosestScopePath(scope *apiScope) *scopePaths {
761 for s := scope; s != nil; s = s.extends {
762 if paths := c.findScopePaths(s); paths != nil {
763 return paths
764 }
765 }
766
767 // This should never happen outside tests as public should be the base scope for every
768 // scope and is enabled by default.
769 return nil
770}
771
Paul Duffina3fb67d2020-05-20 14:20:02 +0100772func (c *commonToSdkLibraryAndImport) selectHeaderJarsForSdkVersion(ctx android.BaseModuleContext, sdkVersion sdkSpec) android.Paths {
Paul Duffin47624362020-05-20 12:19:10 +0100773
774 // If a specific numeric version has been requested then use prebuilt versions of the sdk.
775 if sdkVersion.version.isNumbered() {
776 return PrebuiltJars(ctx, c.moduleBase.BaseModuleName(), sdkVersion)
777 }
778
779 var apiScope *apiScope
780 switch sdkVersion.kind {
781 case sdkSystem:
782 apiScope = apiScopeSystem
Paul Duffin5ae30792020-05-20 11:52:25 +0100783 case sdkModule:
784 apiScope = apiScopeModuleLib
Paul Duffin47624362020-05-20 12:19:10 +0100785 case sdkTest:
786 apiScope = apiScopeTest
Paul Duffin5a757b12020-06-02 13:00:08 +0100787 case sdkSystemServer:
788 apiScope = apiScopeSystemServer
Paul Duffin47624362020-05-20 12:19:10 +0100789 default:
790 apiScope = apiScopePublic
791 }
792
Paul Duffin5ae30792020-05-20 11:52:25 +0100793 paths := c.findClosestScopePath(apiScope)
794 if paths == nil {
795 var scopes []string
796 for _, s := range allApiScopes {
797 if c.findScopePaths(s) != nil {
798 scopes = append(scopes, s.name)
799 }
800 }
801 ctx.ModuleErrorf("requires api scope %s from %s but it only has %q available", apiScope.name, c.moduleBase.BaseModuleName(), scopes)
802 return nil
803 }
804
Paul Duffina3fb67d2020-05-20 14:20:02 +0100805 return paths.stubsHeaderPath
Paul Duffin47624362020-05-20 12:19:10 +0100806}
807
Paul Duffin64e61992020-05-15 10:20:31 +0100808func (c *commonToSdkLibraryAndImport) sdkComponentPropertiesForChildLibrary() interface{} {
809 componentProps := &struct {
810 SdkLibraryToImplicitlyTrack *string
Paul Duffind11e78e2020-05-15 20:37:11 +0100811 }{}
812
813 if c.sharedLibrary() {
Paul Duffin64e61992020-05-15 10:20:31 +0100814 // Mark the stubs library as being components of this java_sdk_library so that
815 // any app that includes code which depends (directly or indirectly) on the stubs
816 // library will have the appropriate <uses-library> invocation inserted into its
817 // manifest if necessary.
Paul Duffind11e78e2020-05-15 20:37:11 +0100818 componentProps.SdkLibraryToImplicitlyTrack = proptools.StringPtr(c.moduleBase.BaseModuleName())
Paul Duffin64e61992020-05-15 10:20:31 +0100819 }
820
821 return componentProps
822}
823
Paul Duffind11e78e2020-05-15 20:37:11 +0100824// Check if this can be used as a shared library.
825func (c *commonToSdkLibraryAndImport) sharedLibrary() bool {
826 return proptools.BoolDefault(c.commonSdkLibraryProperties.Shared_library, true)
827}
828
Paul Duffin64e61992020-05-15 10:20:31 +0100829// Properties related to the use of a module as an component of a java_sdk_library.
830type SdkLibraryComponentProperties struct {
831
832 // The name of the java_sdk_library/_import to add to a <uses-library> entry
833 // in the AndroidManifest.xml of any Android app that includes code that references
834 // this module. If not set then no java_sdk_library/_import is tracked.
835 SdkLibraryToImplicitlyTrack *string `blueprint:"mutated"`
836}
837
838// Structure to be embedded in a module struct that needs to support the
839// SdkLibraryComponentDependency interface.
840type EmbeddableSdkLibraryComponent struct {
841 sdkLibraryComponentProperties SdkLibraryComponentProperties
842}
843
844func (e *EmbeddableSdkLibraryComponent) initSdkLibraryComponent(moduleBase *android.ModuleBase) {
845 moduleBase.AddProperties(&e.sdkLibraryComponentProperties)
846}
847
848// to satisfy SdkLibraryComponentDependency
849func (e *EmbeddableSdkLibraryComponent) OptionalImplicitSdkLibrary() []string {
850 if e.sdkLibraryComponentProperties.SdkLibraryToImplicitlyTrack != nil {
851 return []string{*e.sdkLibraryComponentProperties.SdkLibraryToImplicitlyTrack}
852 }
853 return nil
854}
855
856// Implemented by modules that are (or possibly could be) a component of a java_sdk_library
857// (including the java_sdk_library) itself.
858type SdkLibraryComponentDependency interface {
859 // The optional name of the sdk library that should be implicitly added to the
860 // AndroidManifest of an app that contains code which references the sdk library.
861 //
862 // Returns an array containing 0 or 1 items rather than a *string to make it easier
863 // to append this to the list of exported sdk libraries.
864 OptionalImplicitSdkLibrary() []string
865}
866
867// Make sure that all the module types that are components of java_sdk_library/_import
868// and which can be referenced (directly or indirectly) from an android app implement
869// the SdkLibraryComponentDependency interface.
870var _ SdkLibraryComponentDependency = (*Library)(nil)
871var _ SdkLibraryComponentDependency = (*Import)(nil)
872var _ SdkLibraryComponentDependency = (*SdkLibrary)(nil)
Paul Duffinf642a312020-06-12 17:46:39 +0100873var _ SdkLibraryComponentDependency = (*SdkLibraryImport)(nil)
Paul Duffin64e61992020-05-15 10:20:31 +0100874
875// Provides access to sdk_version related header and implentation jars.
876type SdkLibraryDependency interface {
877 SdkLibraryComponentDependency
878
879 // Get the header jars appropriate for the supplied sdk_version.
880 //
881 // These are turbine generated jars so they only change if the externals of the
882 // class changes but it does not contain and implementation or JavaDoc.
883 SdkHeaderJars(ctx android.BaseModuleContext, sdkVersion sdkSpec) android.Paths
884
885 // Get the implementation jars appropriate for the supplied sdk version.
886 //
887 // These are either the implementation jar for the whole sdk library or the implementation
888 // jars for the stubs. The latter should only be needed when generating JavaDoc as otherwise
889 // they are identical to the corresponding header jars.
890 SdkImplementationJars(ctx android.BaseModuleContext, sdkVersion sdkSpec) android.Paths
891}
892
Inseob Kimc0907f12019-02-08 21:00:45 +0900893type SdkLibrary struct {
Sundong Ahn054b19a2018-10-19 13:46:09 +0900894 Library
Jiyong Parkc678ad32018-04-10 13:07:10 +0900895
Sundong Ahn054b19a2018-10-19 13:46:09 +0900896 sdkLibraryProperties sdkLibraryProperties
Jiyong Parkc678ad32018-04-10 13:07:10 +0900897
Paul Duffin3a254982020-04-28 10:44:03 +0100898 // Map from api scope to the scope specific property structure.
899 scopeToProperties map[*apiScope]*ApiScopeProperties
900
Paul Duffin56d44902020-01-31 13:36:25 +0000901 commonToSdkLibraryAndImport
Jiyong Parkc678ad32018-04-10 13:07:10 +0900902}
903
Inseob Kimc0907f12019-02-08 21:00:45 +0900904var _ Dependency = (*SdkLibrary)(nil)
905var _ SdkLibraryDependency = (*SdkLibrary)(nil)
Colin Cross897d2ed2019-02-11 14:03:51 -0800906
Paul Duffin3a254982020-04-28 10:44:03 +0100907func (module *SdkLibrary) generateTestAndSystemScopesByDefault() bool {
908 return module.sdkLibraryProperties.Generate_system_and_test_apis
909}
910
911func (module *SdkLibrary) getGeneratedApiScopes(ctx android.EarlyModuleContext) apiScopes {
912 // Check to see if any scopes have been explicitly enabled. If any have then all
913 // must be.
914 anyScopesExplicitlyEnabled := false
915 for _, scope := range allApiScopes {
916 scopeProperties := module.scopeToProperties[scope]
917 if scopeProperties.Enabled != nil {
918 anyScopesExplicitlyEnabled = true
919 break
920 }
Paul Duffind1b3a922020-01-22 11:57:20 +0000921 }
Paul Duffin3a254982020-04-28 10:44:03 +0100922
923 var generatedScopes apiScopes
924 enabledScopes := make(map[*apiScope]struct{})
925 for _, scope := range allApiScopes {
926 scopeProperties := module.scopeToProperties[scope]
927 // If any scopes are explicitly enabled then ignore the legacy enabled status.
928 // This is to ensure that any new usages of this module type do not rely on legacy
929 // behaviour.
930 defaultEnabledStatus := false
931 if anyScopesExplicitlyEnabled {
932 defaultEnabledStatus = scope.defaultEnabledStatus
933 } else {
934 defaultEnabledStatus = scope.legacyEnabledStatus(module)
935 }
936 enabled := proptools.BoolDefault(scopeProperties.Enabled, defaultEnabledStatus)
937 if enabled {
938 enabledScopes[scope] = struct{}{}
939 generatedScopes = append(generatedScopes, scope)
940 }
941 }
942
943 // Now check to make sure that any scope that is extended by an enabled scope is also
944 // enabled.
945 for _, scope := range allApiScopes {
946 if _, ok := enabledScopes[scope]; ok {
947 extends := scope.extends
948 if extends != nil {
949 if _, ok := enabledScopes[extends]; !ok {
950 ctx.ModuleErrorf("enabled api scope %q depends on disabled scope %q", scope, extends)
951 }
952 }
953 }
954 }
955
956 return generatedScopes
Paul Duffind1b3a922020-01-22 11:57:20 +0000957}
958
Paul Duffinf642a312020-06-12 17:46:39 +0100959type sdkLibraryComponentTag struct {
960 blueprint.BaseDependencyTag
961 name string
962}
963
964// Mark this tag so dependencies that use it are excluded from visibility enforcement.
965func (t sdkLibraryComponentTag) ExcludeFromVisibilityEnforcement() {}
966
967var xmlPermissionsFileTag = sdkLibraryComponentTag{name: "xml-permissions-file"}
Paul Duffine74ac732020-02-06 13:51:46 +0000968
Jiyong Parke3833882020-02-17 17:28:10 +0900969func IsXmlPermissionsFileDepTag(depTag blueprint.DependencyTag) bool {
Paul Duffinf642a312020-06-12 17:46:39 +0100970 if dt, ok := depTag.(sdkLibraryComponentTag); ok {
Jiyong Parke3833882020-02-17 17:28:10 +0900971 return dt == xmlPermissionsFileTag
972 }
973 return false
974}
975
Paul Duffinf642a312020-06-12 17:46:39 +0100976var implLibraryTag = sdkLibraryComponentTag{name: "impl-library"}
Paul Duffin9d582cc2020-05-16 15:52:12 +0100977
Inseob Kimc0907f12019-02-08 21:00:45 +0900978func (module *SdkLibrary) DepsMutator(ctx android.BottomUpMutatorContext) {
Paul Duffin3a254982020-04-28 10:44:03 +0100979 for _, apiScope := range module.getGeneratedApiScopes(ctx) {
Paul Duffind1b3a922020-01-22 11:57:20 +0000980 // Add dependencies to the stubs library
Paul Duffinb74ee3e2020-05-08 14:16:20 +0100981 ctx.AddVariationDependencies(nil, apiScope.stubsTag, module.stubsLibraryModuleName(apiScope))
Paul Duffind1b3a922020-01-22 11:57:20 +0000982
Paul Duffin6e91aac2020-07-20 18:04:44 +0100983 // Add a dependency on the stubs source in order to access both stubs source and api information.
984 ctx.AddVariationDependencies(nil, apiScope.stubsSourceAndApiTag, module.stubsSourceModuleName(apiScope))
Sundong Ahn054b19a2018-10-19 13:46:09 +0900985 }
986
Paul Duffind11e78e2020-05-15 20:37:11 +0100987 if module.requiresRuntimeImplementationLibrary() {
Paul Duffin9d582cc2020-05-16 15:52:12 +0100988 // Add dependency to the rule for generating the implementation library.
989 ctx.AddDependency(module, implLibraryTag, module.implLibraryModuleName())
990
Paul Duffind11e78e2020-05-15 20:37:11 +0100991 if module.sharedLibrary() {
992 // Add dependency to the rule for generating the xml permissions file
Paul Duffinf642a312020-06-12 17:46:39 +0100993 ctx.AddDependency(module, xmlPermissionsFileTag, module.xmlPermissionsModuleName())
Paul Duffind11e78e2020-05-15 20:37:11 +0100994 }
Paul Duffine74ac732020-02-06 13:51:46 +0000995
Paul Duffind11e78e2020-05-15 20:37:11 +0100996 // Only add the deps for the library if it is actually going to be built.
997 module.Library.deps(ctx)
998 }
Jiyong Parkc678ad32018-04-10 13:07:10 +0900999}
1000
Paul Duffin46fdda82020-05-14 15:39:10 +01001001func (module *SdkLibrary) OutputFiles(tag string) (android.Paths, error) {
1002 paths, err := module.commonOutputFiles(tag)
1003 if paths == nil && err == nil {
1004 return module.Library.OutputFiles(tag)
1005 } else {
1006 return paths, err
1007 }
1008}
1009
Inseob Kimc0907f12019-02-08 21:00:45 +09001010func (module *SdkLibrary) GenerateAndroidBuildActions(ctx android.ModuleContext) {
Paul Duffind11e78e2020-05-15 20:37:11 +01001011 // Only build an implementation library if required.
1012 if module.requiresRuntimeImplementationLibrary() {
Paul Duffin43db9be2019-12-30 17:35:49 +00001013 module.Library.GenerateAndroidBuildActions(ctx)
1014 }
Sundong Ahn054b19a2018-10-19 13:46:09 +09001015
Sundong Ahn57368eb2018-07-06 11:20:23 +09001016 // Record the paths to the header jars of the library (stubs and impl).
Paul Duffind1b3a922020-01-22 11:57:20 +00001017 // When this java_sdk_library is depended upon from others via "libs" property,
Jiyong Parkc678ad32018-04-10 13:07:10 +09001018 // the recorded paths will be returned depending on the link type of the caller.
1019 ctx.VisitDirectDeps(func(to android.Module) {
Jiyong Parkc678ad32018-04-10 13:07:10 +09001020 tag := ctx.OtherModuleDependencyTag(to)
1021
Paul Duffin5fb82132020-04-29 20:45:27 +01001022 // Extract information from any of the scope specific dependencies.
1023 if scopeTag, ok := tag.(scopeDependencyTag); ok {
1024 apiScope := scopeTag.apiScope
Paul Duffin5ae30792020-05-20 11:52:25 +01001025 scopePaths := module.getScopePathsCreateIfNeeded(apiScope)
Paul Duffin5fb82132020-04-29 20:45:27 +01001026
1027 // Extract information from the dependency. The exact information extracted
1028 // is determined by the nature of the dependency which is determined by the tag.
1029 scopeTag.extractDepInfo(ctx, to, scopePaths)
Sundong Ahn20e998b2018-07-24 11:19:26 +09001030 }
Jiyong Parkc678ad32018-04-10 13:07:10 +09001031 })
1032}
1033
Jiyong Park0b0e1b92019-12-03 13:24:29 +09001034func (module *SdkLibrary) AndroidMkEntries() []android.AndroidMkEntries {
Paul Duffind11e78e2020-05-15 20:37:11 +01001035 if !module.requiresRuntimeImplementationLibrary() {
Paul Duffin43db9be2019-12-30 17:35:49 +00001036 return nil
1037 }
Jiyong Park0b0e1b92019-12-03 13:24:29 +09001038 entriesList := module.Library.AndroidMkEntries()
1039 entries := &entriesList[0]
Paul Duffinf642a312020-06-12 17:46:39 +01001040 entries.Required = append(entries.Required, module.xmlPermissionsModuleName())
Jiyong Park0b0e1b92019-12-03 13:24:29 +09001041 return entriesList
Jiyong Park82484c02018-04-23 21:41:26 +09001042}
1043
Anton Hansson6bb88102020-03-27 19:43:19 +00001044// The dist path of the stub artifacts
1045func (module *SdkLibrary) apiDistPath(apiScope *apiScope) string {
1046 if module.ModuleBase.Owner() != "" {
1047 return path.Join("apistubs", module.ModuleBase.Owner(), apiScope.name)
1048 } else if Bool(module.sdkLibraryProperties.Core_lib) {
1049 return path.Join("apistubs", "core", apiScope.name)
1050 } else {
1051 return path.Join("apistubs", "android", apiScope.name)
1052 }
1053}
1054
Paul Duffin12ceb462019-12-24 20:31:31 +00001055// Get the sdk version for use when compiling the stubs library.
Paul Duffin153501f2020-05-12 15:52:55 +01001056func (module *SdkLibrary) sdkVersionForStubsLibrary(mctx android.EarlyModuleContext, apiScope *apiScope) string {
Paul Duffin080f5ee2020-05-12 11:50:28 +01001057 scopeProperties := module.scopeToProperties[apiScope]
1058 if scopeProperties.Sdk_version != nil {
1059 return proptools.String(scopeProperties.Sdk_version)
1060 }
1061
Paul Duffin12ceb462019-12-24 20:31:31 +00001062 sdkDep := decodeSdkDep(mctx, sdkContext(&module.Library))
1063 if sdkDep.hasStandardLibs() {
1064 // If building against a standard sdk then use the sdk version appropriate for the scope.
Paul Duffind1b3a922020-01-22 11:57:20 +00001065 return apiScope.sdkVersion
Paul Duffin12ceb462019-12-24 20:31:31 +00001066 } else {
1067 // Otherwise, use no system module.
1068 return "none"
1069 }
1070}
1071
Paul Duffind1b3a922020-01-22 11:57:20 +00001072func (module *SdkLibrary) latestApiFilegroupName(apiScope *apiScope) string {
1073 return ":" + module.BaseModuleName() + ".api." + apiScope.name + ".latest"
Jiyong Park58c518b2018-05-12 22:29:12 +09001074}
Jiyong Parkc678ad32018-04-10 13:07:10 +09001075
Paul Duffind1b3a922020-01-22 11:57:20 +00001076func (module *SdkLibrary) latestRemovedApiFilegroupName(apiScope *apiScope) string {
1077 return ":" + module.BaseModuleName() + "-removed.api." + apiScope.name + ".latest"
Jiyong Parkc678ad32018-04-10 13:07:10 +09001078}
1079
Anton Hansson97f83c12020-08-19 11:40:22 +01001080func childModuleVisibility(childVisibility []string) []string {
1081 if childVisibility == nil {
1082 // No child visibility set. The child will use the visibility of the sdk_library.
1083 return nil
1084 }
1085
1086 // Prepend an override to ignore the sdk_library's visibility, and rely on the child visibility.
1087 var visibility []string
1088 visibility = append(visibility, "//visibility:override")
1089 visibility = append(visibility, childVisibility...)
1090 return visibility
1091}
1092
Paul Duffin9d582cc2020-05-16 15:52:12 +01001093// Creates the implementation java library
1094func (module *SdkLibrary) createImplLibrary(mctx android.DefaultableHookContext) {
Paul Duffinc4422102020-06-24 16:22:38 +01001095 moduleNamePtr := proptools.StringPtr(module.BaseModuleName())
1096
Anton Hansson97f83c12020-08-19 11:40:22 +01001097 visibility := childModuleVisibility(module.sdkLibraryProperties.Impl_library_visibility)
1098
Paul Duffin9d582cc2020-05-16 15:52:12 +01001099 props := struct {
Paul Duffinc4422102020-06-24 16:22:38 +01001100 Name *string
1101 Visibility []string
1102 Instrument bool
1103 ConfigurationName *string
Paul Duffin9d582cc2020-05-16 15:52:12 +01001104 }{
1105 Name: proptools.StringPtr(module.implLibraryModuleName()),
Anton Hansson97f83c12020-08-19 11:40:22 +01001106 Visibility: visibility,
Paul Duffin49d3a522020-06-18 21:09:55 +01001107 // Set the instrument property to ensure it is instrumented when instrumentation is required.
1108 Instrument: true,
Paul Duffinc4422102020-06-24 16:22:38 +01001109
1110 // Make the created library behave as if it had the same name as this module.
1111 ConfigurationName: moduleNamePtr,
Paul Duffin9d582cc2020-05-16 15:52:12 +01001112 }
1113
1114 properties := []interface{}{
1115 &module.properties,
1116 &module.protoProperties,
1117 &module.deviceProperties,
Liz Kammer7727edc2020-07-09 15:16:41 -07001118 &module.dexProperties,
Paul Duffin9d582cc2020-05-16 15:52:12 +01001119 &module.dexpreoptProperties,
Colin Cross1e28e3c2020-06-02 20:09:13 -07001120 &module.linter.properties,
Paul Duffin9d582cc2020-05-16 15:52:12 +01001121 &props,
1122 module.sdkComponentPropertiesForChildLibrary(),
1123 }
1124 mctx.CreateModule(LibraryFactory, properties...)
1125}
1126
Jiyong Parkc678ad32018-04-10 13:07:10 +09001127// Creates a static java library that has API stubs
Paul Duffin2aaef532020-04-29 16:47:28 +01001128func (module *SdkLibrary) createStubsLibrary(mctx android.DefaultableHookContext, apiScope *apiScope) {
Jiyong Parkc678ad32018-04-10 13:07:10 +09001129 props := struct {
Paul Duffind41712d2020-05-16 09:57:59 +01001130 Name *string
1131 Visibility []string
1132 Srcs []string
1133 Installable *bool
1134 Sdk_version *string
1135 System_modules *string
1136 Patch_module *string
1137 Libs []string
1138 Compile_dex *bool
1139 Java_version *string
1140 Product_variables struct {
Jiyong Park82484c02018-04-23 21:41:26 +09001141 Pdk struct {
1142 Enabled *bool
1143 }
Jiyong Parkc678ad32018-04-10 13:07:10 +09001144 }
Sundong Ahn054b19a2018-10-19 13:46:09 +09001145 Openjdk9 struct {
1146 Srcs []string
1147 Javacflags []string
1148 }
Anton Hansson6bb88102020-03-27 19:43:19 +00001149 Dist struct {
1150 Targets []string
1151 Dest *string
1152 Dir *string
1153 Tag *string
1154 }
Jiyong Parkc678ad32018-04-10 13:07:10 +09001155 }{}
1156
Paul Duffinb74ee3e2020-05-08 14:16:20 +01001157 props.Name = proptools.StringPtr(module.stubsLibraryModuleName(apiScope))
Anton Hansson97f83c12020-08-19 11:40:22 +01001158 props.Visibility = childModuleVisibility(module.sdkLibraryProperties.Stubs_library_visibility)
Jiyong Parkc678ad32018-04-10 13:07:10 +09001159 // sources are generated from the droiddoc
Paul Duffinb74ee3e2020-05-08 14:16:20 +01001160 props.Srcs = []string{":" + module.stubsSourceModuleName(apiScope)}
Paul Duffin12ceb462019-12-24 20:31:31 +00001161 sdkVersion := module.sdkVersionForStubsLibrary(mctx, apiScope)
Paul Duffin52d398a2019-06-11 12:31:14 +01001162 props.Sdk_version = proptools.StringPtr(sdkVersion)
Paul Duffinc5d954a2020-05-16 18:54:24 +01001163 props.System_modules = module.deviceProperties.System_modules
1164 props.Patch_module = module.properties.Patch_module
Paul Duffin367ab912019-12-23 19:40:36 +00001165 props.Installable = proptools.BoolPtr(false)
Sundong Ahn054b19a2018-10-19 13:46:09 +09001166 props.Libs = module.sdkLibraryProperties.Stub_only_libs
Paul Duffin2ce1e812020-05-20 19:35:27 +01001167 // The stub-annotations library contains special versions of the annotations
1168 // with CLASS retention policy, so that they're kept.
1169 if proptools.Bool(module.sdkLibraryProperties.Annotations_enabled) {
1170 props.Libs = append(props.Libs, "stub-annotations")
1171 }
Jiyong Park82484c02018-04-23 21:41:26 +09001172 props.Product_variables.Pdk.Enabled = proptools.BoolPtr(false)
Paul Duffinc5d954a2020-05-16 18:54:24 +01001173 props.Openjdk9.Srcs = module.properties.Openjdk9.Srcs
1174 props.Openjdk9.Javacflags = module.properties.Openjdk9.Javacflags
Anton Hanssoncf4dd4c2020-05-21 09:21:57 +01001175 // We compile the stubs for 1.8 in line with the main android.jar stubs, and potential
1176 // interop with older developer tools that don't support 1.9.
1177 props.Java_version = proptools.StringPtr("1.8")
Liz Kammer7727edc2020-07-09 15:16:41 -07001178 if module.dexProperties.Compile_dex != nil {
1179 props.Compile_dex = module.dexProperties.Compile_dex
Sundong Ahndd567f92018-07-31 17:19:11 +09001180 }
Jiyong Parkc678ad32018-04-10 13:07:10 +09001181
Anton Hansson6bb88102020-03-27 19:43:19 +00001182 // Dist the class jar artifact for sdk builds.
1183 if !Bool(module.sdkLibraryProperties.No_dist) {
1184 props.Dist.Targets = []string{"sdk", "win_sdk"}
1185 props.Dist.Dest = proptools.StringPtr(fmt.Sprintf("%v.jar", module.BaseModuleName()))
1186 props.Dist.Dir = proptools.StringPtr(module.apiDistPath(apiScope))
1187 props.Dist.Tag = proptools.StringPtr(".jar")
1188 }
Jiyong Parkc678ad32018-04-10 13:07:10 +09001189
Paul Duffin64e61992020-05-15 10:20:31 +01001190 mctx.CreateModule(LibraryFactory, &props, module.sdkComponentPropertiesForChildLibrary())
Jiyong Parkc678ad32018-04-10 13:07:10 +09001191}
1192
Paul Duffincbcfcaa2020-04-07 18:49:53 +01001193// Creates a droidstubs module that creates stubs source files from the given full source
Paul Duffin5fb82132020-04-29 20:45:27 +01001194// files and also updates and checks the API specification files.
Paul Duffin6e91aac2020-07-20 18:04:44 +01001195func (module *SdkLibrary) createStubsSourcesAndApi(mctx android.DefaultableHookContext, apiScope *apiScope, name string, scopeSpecificDroidstubsArgs []string) {
Jiyong Parkc678ad32018-04-10 13:07:10 +09001196 props := struct {
Sundong Ahn054b19a2018-10-19 13:46:09 +09001197 Name *string
Paul Duffin344c4ee2020-04-29 23:35:13 +01001198 Visibility []string
Sundong Ahn054b19a2018-10-19 13:46:09 +09001199 Srcs []string
1200 Installable *bool
Paul Duffin52d398a2019-06-11 12:31:14 +01001201 Sdk_version *string
Paul Duffin12ceb462019-12-24 20:31:31 +00001202 System_modules *string
Sundong Ahn054b19a2018-10-19 13:46:09 +09001203 Libs []string
Paul Duffin11512472019-02-11 15:55:17 +00001204 Arg_files []string
Sundong Ahn054b19a2018-10-19 13:46:09 +09001205 Args *string
Sundong Ahn054b19a2018-10-19 13:46:09 +09001206 Java_version *string
Paul Duffin2ce1e812020-05-20 19:35:27 +01001207 Annotations_enabled *bool
Sundong Ahn054b19a2018-10-19 13:46:09 +09001208 Merge_annotations_dirs []string
1209 Merge_inclusion_annotations_dirs []string
Paul Duffina377e4c2020-04-29 13:30:54 +01001210 Generate_stubs *bool
Sundong Ahn054b19a2018-10-19 13:46:09 +09001211 Check_api struct {
Inseob Kim38449af2019-02-28 14:24:05 +09001212 Current ApiToCheck
1213 Last_released ApiToCheck
1214 Ignore_missing_latest_api *bool
Paul Duffin8986cc92020-05-10 19:32:20 +01001215
1216 Api_lint struct {
1217 Enabled *bool
1218 New_since *string
1219 Baseline_file *string
1220 }
Jiyong Park58c518b2018-05-12 22:29:12 +09001221 }
Sundong Ahn1b92c822018-05-29 11:35:17 +09001222 Aidl struct {
1223 Include_dirs []string
1224 Local_include_dirs []string
1225 }
Anton Hansson6bb88102020-03-27 19:43:19 +00001226 Dist struct {
1227 Targets []string
1228 Dest *string
1229 Dir *string
1230 }
Jiyong Parkc678ad32018-04-10 13:07:10 +09001231 }{}
1232
Paul Duffinda364252020-04-28 14:08:32 +01001233 // The stubs source processing uses the same compile time classpath when extracting the
1234 // API from the implementation library as it does when compiling it. i.e. the same
1235 // * sdk version
1236 // * system_modules
1237 // * libs (static_libs/libs)
Paul Duffin250e6192019-06-07 10:44:37 +01001238
Paul Duffina377e4c2020-04-29 13:30:54 +01001239 props.Name = proptools.StringPtr(name)
Anton Hansson97f83c12020-08-19 11:40:22 +01001240 props.Visibility = childModuleVisibility(module.sdkLibraryProperties.Stubs_source_visibility)
Paul Duffinc5d954a2020-05-16 18:54:24 +01001241 props.Srcs = append(props.Srcs, module.properties.Srcs...)
1242 props.Sdk_version = module.deviceProperties.Sdk_version
1243 props.System_modules = module.deviceProperties.System_modules
Jiyong Parkc678ad32018-04-10 13:07:10 +09001244 props.Installable = proptools.BoolPtr(false)
Sundong Ahne6f0b052018-06-05 16:46:14 +09001245 // A droiddoc module has only one Libs property and doesn't distinguish between
1246 // shared libs and static libs. So we need to add both of these libs to Libs property.
Paul Duffinc5d954a2020-05-16 18:54:24 +01001247 props.Libs = module.properties.Libs
1248 props.Libs = append(props.Libs, module.properties.Static_libs...)
1249 props.Aidl.Include_dirs = module.deviceProperties.Aidl.Include_dirs
1250 props.Aidl.Local_include_dirs = module.deviceProperties.Aidl.Local_include_dirs
1251 props.Java_version = module.properties.Java_version
Jiyong Parkc678ad32018-04-10 13:07:10 +09001252
Paul Duffin2ce1e812020-05-20 19:35:27 +01001253 props.Annotations_enabled = module.sdkLibraryProperties.Annotations_enabled
Sundong Ahn054b19a2018-10-19 13:46:09 +09001254 props.Merge_annotations_dirs = module.sdkLibraryProperties.Merge_annotations_dirs
1255 props.Merge_inclusion_annotations_dirs = module.sdkLibraryProperties.Merge_inclusion_annotations_dirs
1256
Paul Duffincbcfcaa2020-04-07 18:49:53 +01001257 droidstubsArgs := []string{}
Paul Duffin235ffff2019-12-24 10:41:30 +00001258 if len(module.sdkLibraryProperties.Api_packages) != 0 {
Paul Duffincbcfcaa2020-04-07 18:49:53 +01001259 droidstubsArgs = append(droidstubsArgs, "--stub-packages "+strings.Join(module.sdkLibraryProperties.Api_packages, ":"))
Paul Duffin235ffff2019-12-24 10:41:30 +00001260 }
1261 if len(module.sdkLibraryProperties.Hidden_api_packages) != 0 {
Paul Duffincbcfcaa2020-04-07 18:49:53 +01001262 droidstubsArgs = append(droidstubsArgs,
Paul Duffin235ffff2019-12-24 10:41:30 +00001263 android.JoinWithPrefix(module.sdkLibraryProperties.Hidden_api_packages, " --hide-package "))
1264 }
Paul Duffincbcfcaa2020-04-07 18:49:53 +01001265 droidstubsArgs = append(droidstubsArgs, module.sdkLibraryProperties.Droiddoc_options...)
Paul Duffin235ffff2019-12-24 10:41:30 +00001266 disabledWarnings := []string{
1267 "MissingPermission",
1268 "BroadcastBehavior",
1269 "HiddenSuperclass",
1270 "DeprecationMismatch",
1271 "UnavailableSymbol",
1272 "SdkConstant",
1273 "HiddenTypeParameter",
1274 "Todo",
1275 "Typo",
1276 }
Paul Duffincbcfcaa2020-04-07 18:49:53 +01001277 droidstubsArgs = append(droidstubsArgs, android.JoinWithPrefix(disabledWarnings, "--hide "))
Sundong Ahnfb2721f2018-09-17 13:23:09 +09001278
Paul Duffin3c7c3472020-04-07 18:50:10 +01001279 // Add in scope specific arguments.
Paul Duffina377e4c2020-04-29 13:30:54 +01001280 droidstubsArgs = append(droidstubsArgs, scopeSpecificDroidstubsArgs...)
Paul Duffin11512472019-02-11 15:55:17 +00001281 props.Arg_files = module.sdkLibraryProperties.Droiddoc_option_files
Paul Duffincbcfcaa2020-04-07 18:49:53 +01001282 props.Args = proptools.StringPtr(strings.Join(droidstubsArgs, " "))
Jiyong Parkc678ad32018-04-10 13:07:10 +09001283
Paul Duffin6e91aac2020-07-20 18:04:44 +01001284 // List of APIs identified from the provided source files are created. They are later
1285 // compared against to the not-yet-released (a.k.a current) list of APIs and to the
1286 // last-released (a.k.a numbered) list of API.
1287 currentApiFileName := apiScope.apiFilePrefix + "current.txt"
1288 removedApiFileName := apiScope.apiFilePrefix + "removed.txt"
1289 apiDir := module.getApiDir()
1290 currentApiFileName = path.Join(apiDir, currentApiFileName)
1291 removedApiFileName = path.Join(apiDir, removedApiFileName)
Jiyong Parkc678ad32018-04-10 13:07:10 +09001292
Paul Duffin6e91aac2020-07-20 18:04:44 +01001293 // check against the not-yet-release API
1294 props.Check_api.Current.Api_file = proptools.StringPtr(currentApiFileName)
1295 props.Check_api.Current.Removed_api_file = proptools.StringPtr(removedApiFileName)
Jiyong Park58c518b2018-05-12 22:29:12 +09001296
Paul Duffin6e91aac2020-07-20 18:04:44 +01001297 if !apiScope.unstable {
1298 // check against the latest released API
1299 latestApiFilegroupName := proptools.StringPtr(module.latestApiFilegroupName(apiScope))
1300 props.Check_api.Last_released.Api_file = latestApiFilegroupName
1301 props.Check_api.Last_released.Removed_api_file = proptools.StringPtr(
1302 module.latestRemovedApiFilegroupName(apiScope))
1303 props.Check_api.Ignore_missing_latest_api = proptools.BoolPtr(true)
Paul Duffin8986cc92020-05-10 19:32:20 +01001304
Paul Duffin6e91aac2020-07-20 18:04:44 +01001305 if proptools.Bool(module.sdkLibraryProperties.Api_lint.Enabled) {
1306 // Enable api lint.
1307 props.Check_api.Api_lint.Enabled = proptools.BoolPtr(true)
1308 props.Check_api.Api_lint.New_since = latestApiFilegroupName
Paul Duffin8986cc92020-05-10 19:32:20 +01001309
Paul Duffin6e91aac2020-07-20 18:04:44 +01001310 // If it exists then pass a lint-baseline.txt through to droidstubs.
1311 baselinePath := path.Join(apiDir, apiScope.apiFilePrefix+"lint-baseline.txt")
1312 baselinePathRelativeToRoot := path.Join(mctx.ModuleDir(), baselinePath)
1313 paths, err := mctx.GlobWithDeps(baselinePathRelativeToRoot, nil)
1314 if err != nil {
1315 mctx.ModuleErrorf("error checking for presence of %s: %s", baselinePathRelativeToRoot, err)
1316 }
1317 if len(paths) == 1 {
1318 props.Check_api.Api_lint.Baseline_file = proptools.StringPtr(baselinePath)
1319 } else if len(paths) != 0 {
1320 mctx.ModuleErrorf("error checking for presence of %s: expected one path, found: %v", baselinePathRelativeToRoot, paths)
Paul Duffin8986cc92020-05-10 19:32:20 +01001321 }
1322 }
Paul Duffin6e91aac2020-07-20 18:04:44 +01001323 }
Jiyong Park58c518b2018-05-12 22:29:12 +09001324
Paul Duffin6e91aac2020-07-20 18:04:44 +01001325 // Dist the api txt artifact for sdk builds.
1326 if !Bool(module.sdkLibraryProperties.No_dist) {
1327 props.Dist.Targets = []string{"sdk", "win_sdk"}
1328 props.Dist.Dest = proptools.StringPtr(fmt.Sprintf("%v.txt", module.BaseModuleName()))
1329 props.Dist.Dir = proptools.StringPtr(path.Join(module.apiDistPath(apiScope), "api"))
Anton Hansson6bb88102020-03-27 19:43:19 +00001330 }
1331
Colin Cross84dfc3d2019-09-25 11:33:01 -07001332 mctx.CreateModule(DroidstubsFactory, &props)
Jiyong Parkc678ad32018-04-10 13:07:10 +09001333}
1334
Jooyung Hanb8fa86a2020-03-10 06:23:13 +09001335func (module *SdkLibrary) DepIsInSameApex(mctx android.BaseModuleContext, dep android.Module) bool {
1336 depTag := mctx.OtherModuleDependencyTag(dep)
1337 if depTag == xmlPermissionsFileTag {
1338 return true
1339 }
1340 return module.Library.DepIsInSameApex(mctx, dep)
1341}
1342
Jiyong Parkc678ad32018-04-10 13:07:10 +09001343// Creates the xml file that publicizes the runtime library
Paul Duffin2aaef532020-04-29 16:47:28 +01001344func (module *SdkLibrary) createXmlFile(mctx android.DefaultableHookContext) {
Jiyong Parke3833882020-02-17 17:28:10 +09001345 props := struct {
Paul Duffind41712d2020-05-16 09:57:59 +01001346 Name *string
1347 Lib_name *string
1348 Apex_available []string
Jiyong Parke3833882020-02-17 17:28:10 +09001349 }{
Paul Duffinf642a312020-06-12 17:46:39 +01001350 Name: proptools.StringPtr(module.xmlPermissionsModuleName()),
Jooyung Hanb8fa86a2020-03-10 06:23:13 +09001351 Lib_name: proptools.StringPtr(module.BaseModuleName()),
1352 Apex_available: module.ApexProperties.Apex_available,
Jiyong Parkc678ad32018-04-10 13:07:10 +09001353 }
Jiyong Parke3833882020-02-17 17:28:10 +09001354
Jiyong Parke3833882020-02-17 17:28:10 +09001355 mctx.CreateModule(sdkLibraryXmlFactory, &props)
Jiyong Parkc678ad32018-04-10 13:07:10 +09001356}
1357
Paul Duffin50061512020-01-21 16:31:05 +00001358func PrebuiltJars(ctx android.BaseModuleContext, baseName string, s sdkSpec) android.Paths {
Jiyong Park6a927c42020-01-21 02:03:43 +09001359 var ver sdkVersion
1360 var kind sdkKind
1361 if s.usePrebuilt(ctx) {
1362 ver = s.version
1363 kind = s.kind
Jiyong Parkc678ad32018-04-10 13:07:10 +09001364 } else {
Jiyong Park6a927c42020-01-21 02:03:43 +09001365 // We don't have prebuilt SDK for the specific sdkVersion.
1366 // Instead of breaking the build, fallback to use "system_current"
1367 ver = sdkVersionCurrent
1368 kind = sdkSystem
Sundong Ahn054b19a2018-10-19 13:46:09 +09001369 }
Jiyong Park6a927c42020-01-21 02:03:43 +09001370
1371 dir := filepath.Join("prebuilts", "sdk", ver.String(), kind.String())
Paul Duffin50061512020-01-21 16:31:05 +00001372 jar := filepath.Join(dir, baseName+".jar")
Sundong Ahn054b19a2018-10-19 13:46:09 +09001373 jarPath := android.ExistentPathForSource(ctx, jar)
Sundong Ahnae418ac2019-02-28 15:01:28 +09001374 if !jarPath.Valid() {
Colin Cross07c88562020-01-07 09:34:44 -08001375 if ctx.Config().AllowMissingDependencies() {
1376 return android.Paths{android.PathForSource(ctx, jar)}
1377 } else {
Jiyong Park6a927c42020-01-21 02:03:43 +09001378 ctx.PropertyErrorf("sdk_library", "invalid sdk version %q, %q does not exist", s.raw, jar)
Colin Cross07c88562020-01-07 09:34:44 -08001379 }
Sundong Ahnae418ac2019-02-28 15:01:28 +09001380 return nil
1381 }
Sundong Ahn054b19a2018-10-19 13:46:09 +09001382 return android.Paths{jarPath.Path()}
1383}
1384
Paul Duffinbf19a972020-05-26 13:21:35 +01001385// Get the apex name for module, "" if it is for platform.
1386func getApexNameForModule(module android.Module) string {
1387 if apex, ok := module.(android.ApexModule); ok {
1388 return apex.ApexName()
1389 }
1390
1391 return ""
1392}
1393
1394// Check to see if the other module is within the same named APEX as this module.
1395//
1396// If either this or the other module are on the platform then this will return
1397// false.
Paul Duffinf642a312020-06-12 17:46:39 +01001398func withinSameApexAs(module android.ApexModule, other android.Module) bool {
Paul Duffinbf19a972020-05-26 13:21:35 +01001399 name := module.ApexName()
1400 return name != "" && getApexNameForModule(other) == name
1401}
1402
Paul Duffin47624362020-05-20 12:19:10 +01001403func (module *SdkLibrary) sdkJars(ctx android.BaseModuleContext, sdkVersion sdkSpec, headerJars bool) android.Paths {
Jiyong Park27fc4142020-05-28 00:19:53 +09001404 // If the client doesn't set sdk_version, but if this library prefers stubs over
1405 // the impl library, let's provide the widest API surface possible. To do so,
1406 // force override sdk_version to module_current so that the closest possible API
1407 // surface could be found in selectHeaderJarsForSdkVersion
1408 if module.defaultsToStubs() && !sdkVersion.specified() {
1409 sdkVersion = sdkSpecFrom("module_current")
1410 }
Paul Duffind1b3a922020-01-22 11:57:20 +00001411
Paul Duffin2e7ed652020-05-26 18:13:57 +01001412 // Only provide access to the implementation library if it is actually built.
1413 if module.requiresRuntimeImplementationLibrary() {
1414 // Check any special cases for java_sdk_library.
1415 //
1416 // Only allow access to the implementation library in the following condition:
1417 // * No sdk_version specified on the referencing module.
Paul Duffinbf19a972020-05-26 13:21:35 +01001418 // * The referencing module is in the same apex as this.
Paul Duffinf642a312020-06-12 17:46:39 +01001419 if sdkVersion.kind == sdkPrivate || withinSameApexAs(module, ctx.Module()) {
Paul Duffin2e7ed652020-05-26 18:13:57 +01001420 if headerJars {
1421 return module.HeaderJars()
1422 } else {
1423 return module.ImplementationJars()
1424 }
Sundong Ahn054b19a2018-10-19 13:46:09 +09001425 }
Jiyong Parkc678ad32018-04-10 13:07:10 +09001426 }
Paul Duffin47624362020-05-20 12:19:10 +01001427
Paul Duffina3fb67d2020-05-20 14:20:02 +01001428 return module.selectHeaderJarsForSdkVersion(ctx, sdkVersion)
Jiyong Parkc678ad32018-04-10 13:07:10 +09001429}
1430
Sundong Ahn241cd372018-07-13 16:16:44 +09001431// to satisfy SdkLibraryDependency interface
Paul Duffind1b3a922020-01-22 11:57:20 +00001432func (module *SdkLibrary) SdkHeaderJars(ctx android.BaseModuleContext, sdkVersion sdkSpec) android.Paths {
1433 return module.sdkJars(ctx, sdkVersion, true /*headerJars*/)
1434}
1435
1436// to satisfy SdkLibraryDependency interface
Jiyong Park6a927c42020-01-21 02:03:43 +09001437func (module *SdkLibrary) SdkImplementationJars(ctx android.BaseModuleContext, sdkVersion sdkSpec) android.Paths {
Paul Duffind1b3a922020-01-22 11:57:20 +00001438 return module.sdkJars(ctx, sdkVersion, false /*headerJars*/)
Sundong Ahn241cd372018-07-13 16:16:44 +09001439}
1440
Sundong Ahn80a87b32019-05-13 15:02:50 +09001441func (module *SdkLibrary) SetNoDist() {
1442 module.sdkLibraryProperties.No_dist = proptools.BoolPtr(true)
1443}
1444
Colin Cross571cccf2019-02-04 11:22:08 -08001445var javaSdkLibrariesKey = android.NewOnceKey("javaSdkLibraries")
1446
Jiyong Park82484c02018-04-23 21:41:26 +09001447func javaSdkLibraries(config android.Config) *[]string {
Colin Cross571cccf2019-02-04 11:22:08 -08001448 return config.Once(javaSdkLibrariesKey, func() interface{} {
Jiyong Park82484c02018-04-23 21:41:26 +09001449 return &[]string{}
1450 }).(*[]string)
1451}
1452
Paul Duffin749f98f2019-12-30 17:23:46 +00001453func (module *SdkLibrary) getApiDir() string {
1454 return proptools.StringDefault(module.sdkLibraryProperties.Api_dir, "api")
1455}
1456
Jiyong Parkc678ad32018-04-10 13:07:10 +09001457// For a java_sdk_library module, create internal modules for stubs, docs,
1458// runtime libs and xml file. If requested, the stubs and docs are created twice
1459// once for public API level and once for system API level
Paul Duffin2aaef532020-04-29 16:47:28 +01001460func (module *SdkLibrary) CreateInternalModules(mctx android.DefaultableHookContext) {
1461 // If the module has been disabled then don't create any child modules.
1462 if !module.Enabled() {
1463 return
1464 }
1465
Paul Duffinc5d954a2020-05-16 18:54:24 +01001466 if len(module.properties.Srcs) == 0 {
Inseob Kimc0907f12019-02-08 21:00:45 +09001467 mctx.PropertyErrorf("srcs", "java_sdk_library must specify srcs")
Jooyung Han58f26ab2019-12-18 15:34:32 +09001468 return
Inseob Kimc0907f12019-02-08 21:00:45 +09001469 }
1470
Paul Duffin37e0b772019-12-30 17:20:10 +00001471 // If this builds against standard libraries (i.e. is not part of the core libraries)
1472 // then assume it provides both system and test apis. Otherwise, assume it does not and
1473 // also assume it does not contribute to the dist build.
1474 sdkDep := decodeSdkDep(mctx, sdkContext(&module.Library))
1475 hasSystemAndTestApis := sdkDep.hasStandardLibs()
Paul Duffin3a254982020-04-28 10:44:03 +01001476 module.sdkLibraryProperties.Generate_system_and_test_apis = hasSystemAndTestApis
Paul Duffin37e0b772019-12-30 17:20:10 +00001477 module.sdkLibraryProperties.No_dist = proptools.BoolPtr(!hasSystemAndTestApis)
1478
Inseob Kim8098faa2019-03-18 10:19:51 +09001479 missing_current_api := false
1480
Paul Duffin3a254982020-04-28 10:44:03 +01001481 generatedScopes := module.getGeneratedApiScopes(mctx)
Paul Duffind1b3a922020-01-22 11:57:20 +00001482
Paul Duffin749f98f2019-12-30 17:23:46 +00001483 apiDir := module.getApiDir()
Paul Duffin3a254982020-04-28 10:44:03 +01001484 for _, scope := range generatedScopes {
Inseob Kim8098faa2019-03-18 10:19:51 +09001485 for _, api := range []string{"current.txt", "removed.txt"} {
Paul Duffind1b3a922020-01-22 11:57:20 +00001486 path := path.Join(mctx.ModuleDir(), apiDir, scope.apiFilePrefix+api)
Inseob Kim8098faa2019-03-18 10:19:51 +09001487 p := android.ExistentPathForSource(mctx, path)
1488 if !p.Valid() {
1489 mctx.ModuleErrorf("Current api file %#v doesn't exist", path)
1490 missing_current_api = true
1491 }
1492 }
1493 }
1494
1495 if missing_current_api {
1496 script := "build/soong/scripts/gen-java-current-api-files.sh"
1497 p := android.ExistentPathForSource(mctx, script)
1498
1499 if !p.Valid() {
1500 panic(fmt.Sprintf("script file %s doesn't exist", script))
1501 }
1502
1503 mctx.ModuleErrorf("One or more current api files are missing. "+
1504 "You can update them by:\n"+
Paul Duffin37e0b772019-12-30 17:20:10 +00001505 "%s %q %s && m update-api",
Paul Duffind1b3a922020-01-22 11:57:20 +00001506 script, filepath.Join(mctx.ModuleDir(), apiDir),
Paul Duffin3a254982020-04-28 10:44:03 +01001507 strings.Join(generatedScopes.Strings(func(s *apiScope) string { return s.apiFilePrefix }), " "))
Inseob Kim8098faa2019-03-18 10:19:51 +09001508 return
1509 }
1510
Paul Duffin3a254982020-04-28 10:44:03 +01001511 for _, scope := range generatedScopes {
Paul Duffin6e91aac2020-07-20 18:04:44 +01001512 // Use the stubs source name for legacy reasons.
1513 module.createStubsSourcesAndApi(mctx, scope, module.stubsSourceModuleName(scope), scope.droidstubsArgs)
Paul Duffina377e4c2020-04-29 13:30:54 +01001514
Paul Duffind1b3a922020-01-22 11:57:20 +00001515 module.createStubsLibrary(mctx, scope)
Inseob Kimc0907f12019-02-08 21:00:45 +09001516 }
1517
Paul Duffind11e78e2020-05-15 20:37:11 +01001518 if module.requiresRuntimeImplementationLibrary() {
Paul Duffin9d582cc2020-05-16 15:52:12 +01001519 // Create child module to create an implementation library.
1520 //
1521 // This temporarily creates a second implementation library that can be explicitly
1522 // referenced.
1523 //
1524 // TODO(b/156618935) - update comment once only one implementation library is created.
1525 module.createImplLibrary(mctx)
1526
Paul Duffind11e78e2020-05-15 20:37:11 +01001527 // Only create an XML permissions file that declares the library as being usable
1528 // as a shared library if required.
1529 if module.sharedLibrary() {
1530 module.createXmlFile(mctx)
1531 }
Paul Duffin43db9be2019-12-30 17:35:49 +00001532
1533 // record java_sdk_library modules so that they are exported to make
1534 javaSdkLibraries := javaSdkLibraries(mctx.Config())
1535 javaSdkLibrariesLock.Lock()
1536 defer javaSdkLibrariesLock.Unlock()
1537 *javaSdkLibraries = append(*javaSdkLibraries, module.BaseModuleName())
1538 }
Inseob Kimc0907f12019-02-08 21:00:45 +09001539}
1540
1541func (module *SdkLibrary) InitSdkLibraryProperties() {
Colin Cross1c14b4e2020-06-15 16:09:53 -07001542 module.addHostAndDeviceProperties()
1543 module.AddProperties(&module.sdkLibraryProperties)
Sundong Ahn054b19a2018-10-19 13:46:09 +09001544
Paul Duffin64e61992020-05-15 10:20:31 +01001545 module.initSdkLibraryComponent(&module.ModuleBase)
1546
Paul Duffinc5d954a2020-05-16 18:54:24 +01001547 module.properties.Installable = proptools.BoolPtr(true)
1548 module.deviceProperties.IsSDKLibrary = true
Inseob Kimc0907f12019-02-08 21:00:45 +09001549}
Sundong Ahn054b19a2018-10-19 13:46:09 +09001550
Paul Duffind11e78e2020-05-15 20:37:11 +01001551func (module *SdkLibrary) requiresRuntimeImplementationLibrary() bool {
1552 return !proptools.Bool(module.sdkLibraryProperties.Api_only)
1553}
1554
Jiyong Park27fc4142020-05-28 00:19:53 +09001555func (module *SdkLibrary) defaultsToStubs() bool {
1556 return proptools.Bool(module.sdkLibraryProperties.Default_to_stubs)
1557}
1558
Paul Duffin1a724e62020-05-08 13:44:43 +01001559// Defines how to name the individual component modules the sdk library creates.
1560type sdkLibraryComponentNamingScheme interface {
1561 stubsLibraryModuleName(scope *apiScope, baseName string) string
1562
1563 stubsSourceModuleName(scope *apiScope, baseName string) string
1564
1565 apiModuleName(scope *apiScope, baseName string) string
1566}
1567
1568type defaultNamingScheme struct {
1569}
1570
1571func (s *defaultNamingScheme) stubsLibraryModuleName(scope *apiScope, baseName string) string {
1572 return scope.stubsLibraryModuleName(baseName)
1573}
1574
1575func (s *defaultNamingScheme) stubsSourceModuleName(scope *apiScope, baseName string) string {
1576 return scope.stubsSourceModuleName(baseName)
1577}
1578
1579func (s *defaultNamingScheme) apiModuleName(scope *apiScope, baseName string) string {
1580 return scope.apiModuleName(baseName)
1581}
1582
1583var _ sdkLibraryComponentNamingScheme = (*defaultNamingScheme)(nil)
1584
Paul Duffindef8a892020-05-08 15:36:30 +01001585type frameworkModulesNamingScheme struct {
1586}
1587
1588func (s *frameworkModulesNamingScheme) moduleSuffix(scope *apiScope) string {
1589 suffix := scope.name
1590 if scope == apiScopeModuleLib {
1591 suffix = "module_libs_"
1592 }
1593 return suffix
1594}
1595
1596func (s *frameworkModulesNamingScheme) stubsLibraryModuleName(scope *apiScope, baseName string) string {
1597 return fmt.Sprintf("%s-stubs-%sapi", baseName, s.moduleSuffix(scope))
1598}
1599
1600func (s *frameworkModulesNamingScheme) stubsSourceModuleName(scope *apiScope, baseName string) string {
1601 return fmt.Sprintf("%s-stubs-srcs-%sapi", baseName, s.moduleSuffix(scope))
1602}
1603
1604func (s *frameworkModulesNamingScheme) apiModuleName(scope *apiScope, baseName string) string {
1605 return fmt.Sprintf("%s-api-%sapi", baseName, s.moduleSuffix(scope))
1606}
1607
1608var _ sdkLibraryComponentNamingScheme = (*frameworkModulesNamingScheme)(nil)
1609
Anton Hansson0bd88d02020-05-25 12:20:51 +01001610func moduleStubLinkType(name string) (stub bool, ret linkType) {
1611 // This suffix-based approach is fragile and could potentially mis-trigger.
1612 // TODO(b/155164730): Clean this up when modules no longer reference sdk_lib stubs directly.
1613 if strings.HasSuffix(name, ".stubs.public") || strings.HasSuffix(name, "-stubs-publicapi") {
1614 return true, javaSdk
1615 }
1616 if strings.HasSuffix(name, ".stubs.system") || strings.HasSuffix(name, "-stubs-systemapi") {
1617 return true, javaSystem
1618 }
1619 if strings.HasSuffix(name, ".stubs.module_lib") || strings.HasSuffix(name, "-stubs-module_libs_api") {
1620 return true, javaModule
1621 }
1622 if strings.HasSuffix(name, ".stubs.test") {
1623 return true, javaSystem
1624 }
1625 return false, javaPlatform
1626}
1627
Jaewoong Jung4f158ee2019-07-11 10:05:35 -07001628// java_sdk_library is a special Java library that provides optional platform APIs to apps.
1629// In practice, it can be viewed as a combination of several modules: 1) stubs library that clients
1630// are linked against to, 2) droiddoc module that internally generates API stubs source files,
1631// 3) the real runtime shared library that implements the APIs, and 4) XML file for adding
1632// the runtime lib to the classpath at runtime if requested via <uses-library>.
Inseob Kimc0907f12019-02-08 21:00:45 +09001633func SdkLibraryFactory() android.Module {
1634 module := &SdkLibrary{}
Paul Duffinb74ee3e2020-05-08 14:16:20 +01001635
1636 // Initialize information common between source and prebuilt.
1637 module.initCommon(&module.ModuleBase)
1638
Inseob Kimc0907f12019-02-08 21:00:45 +09001639 module.InitSdkLibraryProperties()
Jooyung Han58f26ab2019-12-18 15:34:32 +09001640 android.InitApexModule(module)
Sundong Ahn054b19a2018-10-19 13:46:09 +09001641 InitJavaModule(module, android.HostAndDeviceSupported)
Paul Duffin3a254982020-04-28 10:44:03 +01001642
1643 // Initialize the map from scope to scope specific properties.
1644 scopeToProperties := make(map[*apiScope]*ApiScopeProperties)
1645 for _, scope := range allApiScopes {
1646 scopeToProperties[scope] = scope.scopeSpecificProperties(module)
1647 }
1648 module.scopeToProperties = scopeToProperties
1649
Paul Duffin344c4ee2020-04-29 23:35:13 +01001650 // Add the properties containing visibility rules so that they are checked.
Paul Duffin9d582cc2020-05-16 15:52:12 +01001651 android.AddVisibilityProperty(module, "impl_library_visibility", &module.sdkLibraryProperties.Impl_library_visibility)
Paul Duffin344c4ee2020-04-29 23:35:13 +01001652 android.AddVisibilityProperty(module, "stubs_library_visibility", &module.sdkLibraryProperties.Stubs_library_visibility)
1653 android.AddVisibilityProperty(module, "stubs_source_visibility", &module.sdkLibraryProperties.Stubs_source_visibility)
1654
Paul Duffin1a724e62020-05-08 13:44:43 +01001655 module.SetDefaultableHook(func(ctx android.DefaultableHookContext) {
Paul Duffind11e78e2020-05-15 20:37:11 +01001656 // If no implementation is required then it cannot be used as a shared library
1657 // either.
1658 if !module.requiresRuntimeImplementationLibrary() {
1659 // If shared_library has been explicitly set to true then it is incompatible
1660 // with api_only: true.
1661 if proptools.Bool(module.commonSdkLibraryProperties.Shared_library) {
1662 ctx.PropertyErrorf("api_only/shared_library", "inconsistent settings, shared_library and api_only cannot both be true")
1663 }
1664 // Set shared_library: false.
1665 module.commonSdkLibraryProperties.Shared_library = proptools.BoolPtr(false)
1666 }
1667
Paul Duffin1a724e62020-05-08 13:44:43 +01001668 if module.initCommonAfterDefaultsApplied(ctx) {
1669 module.CreateInternalModules(ctx)
1670 }
1671 })
Jiyong Parkc678ad32018-04-10 13:07:10 +09001672 return module
1673}
Colin Cross79c7c262019-04-17 11:11:46 -07001674
1675//
1676// SDK library prebuilts
1677//
1678
Paul Duffin56d44902020-01-31 13:36:25 +00001679// Properties associated with each api scope.
1680type sdkLibraryScopeProperties struct {
Colin Cross79c7c262019-04-17 11:11:46 -07001681 Jars []string `android:"path"`
1682
1683 Sdk_version *string
1684
Colin Cross79c7c262019-04-17 11:11:46 -07001685 // List of shared java libs that this module has dependencies to
1686 Libs []string
Paul Duffinf488ef22020-04-09 00:10:17 +01001687
Paul Duffin5fb82132020-04-29 20:45:27 +01001688 // The stubs source.
Paul Duffinf488ef22020-04-09 00:10:17 +01001689 Stub_srcs []string `android:"path"`
Paul Duffin75dcc802020-04-09 01:08:11 +01001690
1691 // The current.txt
Paul Duffin533f9c72020-05-20 16:18:00 +01001692 Current_api *string `android:"path"`
Paul Duffin75dcc802020-04-09 01:08:11 +01001693
1694 // The removed.txt
Paul Duffin533f9c72020-05-20 16:18:00 +01001695 Removed_api *string `android:"path"`
Colin Cross79c7c262019-04-17 11:11:46 -07001696}
1697
Paul Duffin56d44902020-01-31 13:36:25 +00001698type sdkLibraryImportProperties struct {
Paul Duffinfcfd7912020-01-31 17:54:30 +00001699 // List of shared java libs, common to all scopes, that this module has
1700 // dependencies to
1701 Libs []string
Paul Duffin56d44902020-01-31 13:36:25 +00001702}
1703
Paul Duffinf642a312020-06-12 17:46:39 +01001704type SdkLibraryImport struct {
Colin Cross79c7c262019-04-17 11:11:46 -07001705 android.ModuleBase
1706 android.DefaultableModuleBase
1707 prebuilt android.Prebuilt
Paul Duffin61871622020-02-10 13:37:10 +00001708 android.ApexModuleBase
1709 android.SdkBase
Colin Cross79c7c262019-04-17 11:11:46 -07001710
1711 properties sdkLibraryImportProperties
1712
Paul Duffin6a2bd112020-04-07 19:27:04 +01001713 // Map from api scope to the scope specific property structure.
1714 scopeProperties map[*apiScope]*sdkLibraryScopeProperties
1715
Paul Duffin56d44902020-01-31 13:36:25 +00001716 commonToSdkLibraryAndImport
Paul Duffinf642a312020-06-12 17:46:39 +01001717
1718 // The reference to the implementation library created by the source module.
1719 // Is nil if the source module does not exist.
1720 implLibraryModule *Library
1721
1722 // The reference to the xml permissions module created by the source module.
1723 // Is nil if the source module does not exist.
1724 xmlPermissionsFileModule *sdkLibraryXml
Colin Cross79c7c262019-04-17 11:11:46 -07001725}
1726
Paul Duffinf642a312020-06-12 17:46:39 +01001727var _ SdkLibraryDependency = (*SdkLibraryImport)(nil)
Colin Cross79c7c262019-04-17 11:11:46 -07001728
Paul Duffin6a2bd112020-04-07 19:27:04 +01001729// The type of a structure that contains a field of type sdkLibraryScopeProperties
1730// for each apiscope in allApiScopes, e.g. something like:
1731// struct {
1732// Public sdkLibraryScopeProperties
1733// System sdkLibraryScopeProperties
1734// ...
1735// }
1736var allScopeStructType = createAllScopePropertiesStructType()
1737
1738// Dynamically create a structure type for each apiscope in allApiScopes.
1739func createAllScopePropertiesStructType() reflect.Type {
1740 var fields []reflect.StructField
1741 for _, apiScope := range allApiScopes {
1742 field := reflect.StructField{
1743 Name: apiScope.fieldName,
1744 Type: reflect.TypeOf(sdkLibraryScopeProperties{}),
1745 }
1746 fields = append(fields, field)
1747 }
1748
1749 return reflect.StructOf(fields)
1750}
1751
1752// Create an instance of the scope specific structure type and return a map
1753// from apiscope to a pointer to each scope specific field.
1754func createPropertiesInstance() (interface{}, map[*apiScope]*sdkLibraryScopeProperties) {
1755 allScopePropertiesPtr := reflect.New(allScopeStructType)
1756 allScopePropertiesStruct := allScopePropertiesPtr.Elem()
1757 scopeProperties := make(map[*apiScope]*sdkLibraryScopeProperties)
1758
1759 for _, apiScope := range allApiScopes {
1760 field := allScopePropertiesStruct.FieldByName(apiScope.fieldName)
1761 scopeProperties[apiScope] = field.Addr().Interface().(*sdkLibraryScopeProperties)
1762 }
1763
1764 return allScopePropertiesPtr.Interface(), scopeProperties
1765}
1766
Jaewoong Jung4f158ee2019-07-11 10:05:35 -07001767// java_sdk_library_import imports a prebuilt java_sdk_library.
Colin Cross79c7c262019-04-17 11:11:46 -07001768func sdkLibraryImportFactory() android.Module {
Paul Duffinf642a312020-06-12 17:46:39 +01001769 module := &SdkLibraryImport{}
Colin Cross79c7c262019-04-17 11:11:46 -07001770
Paul Duffin6a2bd112020-04-07 19:27:04 +01001771 allScopeProperties, scopeToProperties := createPropertiesInstance()
1772 module.scopeProperties = scopeToProperties
1773 module.AddProperties(&module.properties, allScopeProperties)
Colin Cross79c7c262019-04-17 11:11:46 -07001774
Paul Duffinb74ee3e2020-05-08 14:16:20 +01001775 // Initialize information common between source and prebuilt.
1776 module.initCommon(&module.ModuleBase)
1777
Paul Duffin0bdcb272020-02-06 15:24:57 +00001778 android.InitPrebuiltModule(module, &[]string{""})
Paul Duffin61871622020-02-10 13:37:10 +00001779 android.InitApexModule(module)
1780 android.InitSdkAwareModule(module)
Colin Cross79c7c262019-04-17 11:11:46 -07001781 InitJavaModule(module, android.HostAndDeviceSupported)
1782
Paul Duffin1a724e62020-05-08 13:44:43 +01001783 module.SetDefaultableHook(func(mctx android.DefaultableHookContext) {
1784 if module.initCommonAfterDefaultsApplied(mctx) {
1785 module.createInternalModules(mctx)
1786 }
1787 })
Colin Cross79c7c262019-04-17 11:11:46 -07001788 return module
1789}
1790
Paul Duffinf642a312020-06-12 17:46:39 +01001791func (module *SdkLibraryImport) Prebuilt() *android.Prebuilt {
Colin Cross79c7c262019-04-17 11:11:46 -07001792 return &module.prebuilt
1793}
1794
Paul Duffinf642a312020-06-12 17:46:39 +01001795func (module *SdkLibraryImport) Name() string {
Colin Cross79c7c262019-04-17 11:11:46 -07001796 return module.prebuilt.Name(module.ModuleBase.Name())
1797}
1798
Paul Duffinf642a312020-06-12 17:46:39 +01001799func (module *SdkLibraryImport) createInternalModules(mctx android.DefaultableHookContext) {
Colin Cross79c7c262019-04-17 11:11:46 -07001800
Paul Duffin50061512020-01-21 16:31:05 +00001801 // If the build is configured to use prebuilts then force this to be preferred.
1802 if mctx.Config().UnbundledBuildUsePrebuiltSdks() {
1803 module.prebuilt.ForcePrefer()
1804 }
1805
Paul Duffin6a2bd112020-04-07 19:27:04 +01001806 for apiScope, scopeProperties := range module.scopeProperties {
Paul Duffin56d44902020-01-31 13:36:25 +00001807 if len(scopeProperties.Jars) == 0 {
1808 continue
1809 }
1810
Paul Duffinf6155722020-04-09 00:07:11 +01001811 module.createJavaImportForStubs(mctx, apiScope, scopeProperties)
Paul Duffinf488ef22020-04-09 00:10:17 +01001812
Paul Duffin533f9c72020-05-20 16:18:00 +01001813 if len(scopeProperties.Stub_srcs) > 0 {
1814 module.createPrebuiltStubsSources(mctx, apiScope, scopeProperties)
1815 }
Paul Duffin56d44902020-01-31 13:36:25 +00001816 }
Colin Cross79c7c262019-04-17 11:11:46 -07001817
1818 javaSdkLibraries := javaSdkLibraries(mctx.Config())
1819 javaSdkLibrariesLock.Lock()
1820 defer javaSdkLibrariesLock.Unlock()
1821 *javaSdkLibraries = append(*javaSdkLibraries, module.BaseModuleName())
1822}
1823
Paul Duffinf642a312020-06-12 17:46:39 +01001824func (module *SdkLibraryImport) createJavaImportForStubs(mctx android.DefaultableHookContext, apiScope *apiScope, scopeProperties *sdkLibraryScopeProperties) {
Paul Duffinf6155722020-04-09 00:07:11 +01001825 // Creates a java import for the jar with ".stubs" suffix
1826 props := struct {
Paul Duffind41712d2020-05-16 09:57:59 +01001827 Name *string
1828 Sdk_version *string
1829 Libs []string
1830 Jars []string
1831 Prefer *bool
Paul Duffinf6155722020-04-09 00:07:11 +01001832 }{}
Paul Duffinb74ee3e2020-05-08 14:16:20 +01001833 props.Name = proptools.StringPtr(module.stubsLibraryModuleName(apiScope))
Paul Duffinf6155722020-04-09 00:07:11 +01001834 props.Sdk_version = scopeProperties.Sdk_version
1835 // Prepend any of the libs from the legacy public properties to the libs for each of the
1836 // scopes to avoid having to duplicate them in each scope.
1837 props.Libs = append(module.properties.Libs, scopeProperties.Libs...)
1838 props.Jars = scopeProperties.Jars
Paul Duffind41712d2020-05-16 09:57:59 +01001839
Paul Duffindd89a282020-05-13 16:08:09 +01001840 // The imports are preferred if the java_sdk_library_import is preferred.
1841 props.Prefer = proptools.BoolPtr(module.prebuilt.Prefer())
Paul Duffin64e61992020-05-15 10:20:31 +01001842
1843 mctx.CreateModule(ImportFactory, &props, module.sdkComponentPropertiesForChildLibrary())
Paul Duffinf6155722020-04-09 00:07:11 +01001844}
1845
Paul Duffinf642a312020-06-12 17:46:39 +01001846func (module *SdkLibraryImport) createPrebuiltStubsSources(mctx android.DefaultableHookContext, apiScope *apiScope, scopeProperties *sdkLibraryScopeProperties) {
Paul Duffinf488ef22020-04-09 00:10:17 +01001847 props := struct {
Paul Duffindd89a282020-05-13 16:08:09 +01001848 Name *string
1849 Srcs []string
1850 Prefer *bool
Paul Duffinf488ef22020-04-09 00:10:17 +01001851 }{}
Paul Duffinb74ee3e2020-05-08 14:16:20 +01001852 props.Name = proptools.StringPtr(module.stubsSourceModuleName(apiScope))
Paul Duffinf488ef22020-04-09 00:10:17 +01001853 props.Srcs = scopeProperties.Stub_srcs
1854 mctx.CreateModule(PrebuiltStubsSourcesFactory, &props)
Paul Duffindd89a282020-05-13 16:08:09 +01001855
1856 // The stubs source is preferred if the java_sdk_library_import is preferred.
1857 props.Prefer = proptools.BoolPtr(module.prebuilt.Prefer())
Paul Duffinf488ef22020-04-09 00:10:17 +01001858}
1859
Paul Duffinf642a312020-06-12 17:46:39 +01001860func (module *SdkLibraryImport) DepsMutator(ctx android.BottomUpMutatorContext) {
Paul Duffin6a2bd112020-04-07 19:27:04 +01001861 for apiScope, scopeProperties := range module.scopeProperties {
Paul Duffin56d44902020-01-31 13:36:25 +00001862 if len(scopeProperties.Jars) == 0 {
1863 continue
1864 }
1865
1866 // Add dependencies to the prebuilt stubs library
Paul Duffinb74ee3e2020-05-08 14:16:20 +01001867 ctx.AddVariationDependencies(nil, apiScope.stubsTag, module.stubsLibraryModuleName(apiScope))
Paul Duffin533f9c72020-05-20 16:18:00 +01001868
1869 if len(scopeProperties.Stub_srcs) > 0 {
1870 // Add dependencies to the prebuilt stubs source library
1871 ctx.AddVariationDependencies(nil, apiScope.stubsSourceTag, module.stubsSourceModuleName(apiScope))
1872 }
Paul Duffin56d44902020-01-31 13:36:25 +00001873 }
Paul Duffinf642a312020-06-12 17:46:39 +01001874
1875 implName := module.implLibraryModuleName()
1876 if ctx.OtherModuleExists(implName) {
1877 ctx.AddVariationDependencies(nil, implLibraryTag, implName)
1878
1879 xmlPermissionsModuleName := module.xmlPermissionsModuleName()
1880 if module.sharedLibrary() && ctx.OtherModuleExists(xmlPermissionsModuleName) {
1881 // Add dependency to the rule for generating the xml permissions file
1882 ctx.AddDependency(module, xmlPermissionsFileTag, xmlPermissionsModuleName)
1883 }
1884 }
Colin Cross79c7c262019-04-17 11:11:46 -07001885}
1886
Paul Duffinf642a312020-06-12 17:46:39 +01001887func (module *SdkLibraryImport) DepIsInSameApex(mctx android.BaseModuleContext, dep android.Module) bool {
1888 depTag := mctx.OtherModuleDependencyTag(dep)
1889 if depTag == xmlPermissionsFileTag {
1890 return true
1891 }
1892
1893 // None of the other dependencies of the java_sdk_library_import are in the same apex
1894 // as the one that references this module.
1895 return false
1896}
1897
1898func (module *SdkLibraryImport) OutputFiles(tag string) (android.Paths, error) {
Paul Duffin46fdda82020-05-14 15:39:10 +01001899 return module.commonOutputFiles(tag)
1900}
1901
Paul Duffinf642a312020-06-12 17:46:39 +01001902func (module *SdkLibraryImport) GenerateAndroidBuildActions(ctx android.ModuleContext) {
Paul Duffin533f9c72020-05-20 16:18:00 +01001903 // Record the paths to the prebuilt stubs library and stubs source.
Colin Cross79c7c262019-04-17 11:11:46 -07001904 ctx.VisitDirectDeps(func(to android.Module) {
1905 tag := ctx.OtherModuleDependencyTag(to)
1906
Paul Duffin533f9c72020-05-20 16:18:00 +01001907 // Extract information from any of the scope specific dependencies.
1908 if scopeTag, ok := tag.(scopeDependencyTag); ok {
1909 apiScope := scopeTag.apiScope
1910 scopePaths := module.getScopePathsCreateIfNeeded(apiScope)
1911
1912 // Extract information from the dependency. The exact information extracted
1913 // is determined by the nature of the dependency which is determined by the tag.
1914 scopeTag.extractDepInfo(ctx, to, scopePaths)
Paul Duffinf642a312020-06-12 17:46:39 +01001915 } else if tag == implLibraryTag {
1916 if implLibrary, ok := to.(*Library); ok {
1917 module.implLibraryModule = implLibrary
1918 } else {
1919 ctx.ModuleErrorf("implementation library must be of type *java.Library but was %T", to)
1920 }
1921 } else if tag == xmlPermissionsFileTag {
1922 if xmlPermissionsFileModule, ok := to.(*sdkLibraryXml); ok {
1923 module.xmlPermissionsFileModule = xmlPermissionsFileModule
1924 } else {
1925 ctx.ModuleErrorf("xml permissions file module must be of type *sdkLibraryXml but was %T", to)
1926 }
Colin Cross79c7c262019-04-17 11:11:46 -07001927 }
1928 })
Paul Duffin533f9c72020-05-20 16:18:00 +01001929
1930 // Populate the scope paths with information from the properties.
1931 for apiScope, scopeProperties := range module.scopeProperties {
1932 if len(scopeProperties.Jars) == 0 {
1933 continue
1934 }
1935
1936 paths := module.getScopePathsCreateIfNeeded(apiScope)
1937 paths.currentApiFilePath = android.OptionalPathForModuleSrc(ctx, scopeProperties.Current_api)
1938 paths.removedApiFilePath = android.OptionalPathForModuleSrc(ctx, scopeProperties.Removed_api)
1939 }
Colin Cross79c7c262019-04-17 11:11:46 -07001940}
1941
Paul Duffinf642a312020-06-12 17:46:39 +01001942func (module *SdkLibraryImport) sdkJars(ctx android.BaseModuleContext, sdkVersion sdkSpec, headerJars bool) android.Paths {
1943
1944 // For consistency with SdkLibrary make the implementation jar available to libraries that
1945 // are within the same APEX.
1946 implLibraryModule := module.implLibraryModule
1947 if implLibraryModule != nil && withinSameApexAs(module, ctx.Module()) {
1948 if headerJars {
1949 return implLibraryModule.HeaderJars()
1950 } else {
1951 return implLibraryModule.ImplementationJars()
1952 }
1953 }
1954
Paul Duffina3fb67d2020-05-20 14:20:02 +01001955 return module.selectHeaderJarsForSdkVersion(ctx, sdkVersion)
Paul Duffin56d44902020-01-31 13:36:25 +00001956}
1957
Colin Cross79c7c262019-04-17 11:11:46 -07001958// to satisfy SdkLibraryDependency interface
Paul Duffinf642a312020-06-12 17:46:39 +01001959func (module *SdkLibraryImport) SdkHeaderJars(ctx android.BaseModuleContext, sdkVersion sdkSpec) android.Paths {
Colin Cross79c7c262019-04-17 11:11:46 -07001960 // This module is just a wrapper for the prebuilt stubs.
Paul Duffinf642a312020-06-12 17:46:39 +01001961 return module.sdkJars(ctx, sdkVersion, true)
Colin Cross79c7c262019-04-17 11:11:46 -07001962}
1963
1964// to satisfy SdkLibraryDependency interface
Paul Duffinf642a312020-06-12 17:46:39 +01001965func (module *SdkLibraryImport) SdkImplementationJars(ctx android.BaseModuleContext, sdkVersion sdkSpec) android.Paths {
Colin Cross79c7c262019-04-17 11:11:46 -07001966 // This module is just a wrapper for the stubs.
Paul Duffinf642a312020-06-12 17:46:39 +01001967 return module.sdkJars(ctx, sdkVersion, false)
1968}
1969
1970// to satisfy apex.javaDependency interface
1971func (module *SdkLibraryImport) DexJar() android.Path {
1972 if module.implLibraryModule == nil {
1973 return nil
1974 } else {
1975 return module.implLibraryModule.DexJar()
1976 }
1977}
1978
1979// to satisfy apex.javaDependency interface
1980func (module *SdkLibraryImport) JacocoReportClassesFile() android.Path {
1981 if module.implLibraryModule == nil {
1982 return nil
1983 } else {
1984 return module.implLibraryModule.JacocoReportClassesFile()
1985 }
1986}
1987
1988// to satisfy apex.javaDependency interface
Colin Cross5bc17442020-07-21 20:31:17 -07001989func (module *SdkLibraryImport) LintDepSets() LintDepSets {
1990 if module.implLibraryModule == nil {
1991 return LintDepSets{}
1992 } else {
1993 return module.implLibraryModule.LintDepSets()
1994 }
1995}
1996
1997// to satisfy apex.javaDependency interface
Paul Duffinf642a312020-06-12 17:46:39 +01001998func (module *SdkLibraryImport) Stem() string {
1999 return module.BaseModuleName()
Colin Cross79c7c262019-04-17 11:11:46 -07002000}
Jiyong Parke3833882020-02-17 17:28:10 +09002001
Paul Duffin9ee66da2020-06-17 16:59:43 +01002002var _ ApexDependency = (*SdkLibraryImport)(nil)
2003
2004// to satisfy java.ApexDependency interface
2005func (module *SdkLibraryImport) HeaderJars() android.Paths {
2006 if module.implLibraryModule == nil {
2007 return nil
2008 } else {
2009 return module.implLibraryModule.HeaderJars()
2010 }
2011}
2012
2013// to satisfy java.ApexDependency interface
2014func (module *SdkLibraryImport) ImplementationAndResourcesJars() android.Paths {
2015 if module.implLibraryModule == nil {
2016 return nil
2017 } else {
2018 return module.implLibraryModule.ImplementationAndResourcesJars()
2019 }
2020}
2021
Jiyong Parke3833882020-02-17 17:28:10 +09002022//
2023// java_sdk_library_xml
2024//
2025type sdkLibraryXml struct {
2026 android.ModuleBase
2027 android.DefaultableModuleBase
2028 android.ApexModuleBase
2029
2030 properties sdkLibraryXmlProperties
2031
2032 outputFilePath android.OutputPath
2033 installDirPath android.InstallPath
2034}
2035
2036type sdkLibraryXmlProperties struct {
2037 // canonical name of the lib
2038 Lib_name *string
2039}
2040
2041// java_sdk_library_xml builds the permission xml file for a java_sdk_library.
2042// Not to be used directly by users. java_sdk_library internally uses this.
2043func sdkLibraryXmlFactory() android.Module {
2044 module := &sdkLibraryXml{}
2045
2046 module.AddProperties(&module.properties)
2047
2048 android.InitApexModule(module)
2049 android.InitAndroidArchModule(module, android.DeviceSupported, android.MultilibCommon)
2050
2051 return module
2052}
2053
2054// from android.PrebuiltEtcModule
2055func (module *sdkLibraryXml) SubDir() string {
2056 return "permissions"
2057}
2058
2059// from android.PrebuiltEtcModule
2060func (module *sdkLibraryXml) OutputFile() android.OutputPath {
2061 return module.outputFilePath
2062}
2063
2064// from android.ApexModule
2065func (module *sdkLibraryXml) AvailableFor(what string) bool {
2066 return true
2067}
2068
2069func (module *sdkLibraryXml) DepsMutator(ctx android.BottomUpMutatorContext) {
2070 // do nothing
2071}
2072
2073// File path to the runtime implementation library
2074func (module *sdkLibraryXml) implPath() string {
2075 implName := proptools.String(module.properties.Lib_name)
2076 if apexName := module.ApexName(); apexName != "" {
2077 // TODO(b/146468504): ApexName() is only a soong module name, not apex name.
2078 // In most cases, this works fine. But when apex_name is set or override_apex is used
2079 // this can be wrong.
2080 return fmt.Sprintf("/apex/%s/javalib/%s.jar", apexName, implName)
2081 }
2082 partition := "system"
2083 if module.SocSpecific() {
2084 partition = "vendor"
2085 } else if module.DeviceSpecific() {
2086 partition = "odm"
2087 } else if module.ProductSpecific() {
2088 partition = "product"
2089 } else if module.SystemExtSpecific() {
2090 partition = "system_ext"
2091 }
2092 return "/" + partition + "/framework/" + implName + ".jar"
2093}
2094
2095func (module *sdkLibraryXml) GenerateAndroidBuildActions(ctx android.ModuleContext) {
2096 libName := proptools.String(module.properties.Lib_name)
2097 xmlContent := fmt.Sprintf(permissionsTemplate, libName, module.implPath())
2098
2099 module.outputFilePath = android.PathForModuleOut(ctx, libName+".xml").OutputPath
2100 rule := android.NewRuleBuilder()
2101 rule.Command().
2102 Text("/bin/bash -c \"echo -e '" + xmlContent + "'\" > ").
2103 Output(module.outputFilePath)
2104
2105 rule.Build(pctx, ctx, "java_sdk_xml", "Permission XML")
2106
2107 module.installDirPath = android.PathForModuleInstall(ctx, "etc", module.SubDir())
2108}
2109
2110func (module *sdkLibraryXml) AndroidMkEntries() []android.AndroidMkEntries {
2111 if !module.IsForPlatform() {
2112 return []android.AndroidMkEntries{android.AndroidMkEntries{
2113 Disabled: true,
2114 }}
2115 }
2116
2117 return []android.AndroidMkEntries{android.AndroidMkEntries{
2118 Class: "ETC",
2119 OutputFile: android.OptionalPathForPath(module.outputFilePath),
2120 ExtraEntries: []android.AndroidMkExtraEntriesFunc{
2121 func(entries *android.AndroidMkEntries) {
2122 entries.SetString("LOCAL_MODULE_TAGS", "optional")
2123 entries.SetString("LOCAL_MODULE_PATH", module.installDirPath.ToMakePath().String())
2124 entries.SetString("LOCAL_INSTALLED_MODULE_STEM", module.outputFilePath.Base())
2125 },
2126 },
2127 }}
2128}
Paul Duffin61871622020-02-10 13:37:10 +00002129
2130type sdkLibrarySdkMemberType struct {
2131 android.SdkMemberTypeBase
2132}
2133
2134func (s *sdkLibrarySdkMemberType) AddDependencies(mctx android.BottomUpMutatorContext, dependencyTag blueprint.DependencyTag, names []string) {
2135 mctx.AddVariationDependencies(nil, dependencyTag, names...)
2136}
2137
2138func (s *sdkLibrarySdkMemberType) IsInstance(module android.Module) bool {
2139 _, ok := module.(*SdkLibrary)
2140 return ok
2141}
2142
2143func (s *sdkLibrarySdkMemberType) AddPrebuiltModule(ctx android.SdkMemberContext, member android.SdkMember) android.BpModule {
2144 return ctx.SnapshotBuilder().AddPrebuiltModule(member, "java_sdk_library_import")
2145}
2146
2147func (s *sdkLibrarySdkMemberType) CreateVariantPropertiesStruct() android.SdkMemberProperties {
2148 return &sdkLibrarySdkMemberProperties{}
2149}
2150
2151type sdkLibrarySdkMemberProperties struct {
2152 android.SdkMemberPropertiesBase
2153
2154 // Scope to per scope properties.
2155 Scopes map[*apiScope]scopeProperties
2156
2157 // Additional libraries that the exported stubs libraries depend upon.
2158 Libs []string
Paul Duffinf488ef22020-04-09 00:10:17 +01002159
2160 // The Java stubs source files.
2161 Stub_srcs []string
Paul Duffinf8e08b22020-05-13 16:54:55 +01002162
2163 // The naming scheme.
2164 Naming_scheme *string
Paul Duffina84756c2020-05-26 20:57:10 +01002165
2166 // True if the java_sdk_library_import is for a shared library, false
2167 // otherwise.
2168 Shared_library *bool
Paul Duffin61871622020-02-10 13:37:10 +00002169}
2170
2171type scopeProperties struct {
Paul Duffin75dcc802020-04-09 01:08:11 +01002172 Jars android.Paths
2173 StubsSrcJar android.Path
2174 CurrentApiFile android.Path
2175 RemovedApiFile android.Path
2176 SdkVersion string
Paul Duffin61871622020-02-10 13:37:10 +00002177}
2178
2179func (s *sdkLibrarySdkMemberProperties) PopulateFromVariant(ctx android.SdkMemberContext, variant android.Module) {
2180 sdk := variant.(*SdkLibrary)
2181
2182 s.Scopes = make(map[*apiScope]scopeProperties)
2183 for _, apiScope := range allApiScopes {
Paul Duffin5ae30792020-05-20 11:52:25 +01002184 paths := sdk.findScopePaths(apiScope)
2185 if paths == nil {
2186 continue
2187 }
2188
Paul Duffin61871622020-02-10 13:37:10 +00002189 jars := paths.stubsImplPath
2190 if len(jars) > 0 {
2191 properties := scopeProperties{}
2192 properties.Jars = jars
Paul Duffin153501f2020-05-12 15:52:55 +01002193 properties.SdkVersion = sdk.sdkVersionForStubsLibrary(ctx.SdkModuleContext(), apiScope)
Paul Duffin533f9c72020-05-20 16:18:00 +01002194 properties.StubsSrcJar = paths.stubsSrcJar.Path()
Paul Duffin86672f62020-06-19 18:39:55 +01002195 if paths.currentApiFilePath.Valid() {
2196 properties.CurrentApiFile = paths.currentApiFilePath.Path()
2197 }
2198 if paths.removedApiFilePath.Valid() {
2199 properties.RemovedApiFile = paths.removedApiFilePath.Path()
2200 }
Paul Duffin61871622020-02-10 13:37:10 +00002201 s.Scopes[apiScope] = properties
2202 }
2203 }
2204
2205 s.Libs = sdk.properties.Libs
Paul Duffind11e78e2020-05-15 20:37:11 +01002206 s.Naming_scheme = sdk.commonSdkLibraryProperties.Naming_scheme
Paul Duffina84756c2020-05-26 20:57:10 +01002207 s.Shared_library = proptools.BoolPtr(sdk.sharedLibrary())
Paul Duffin61871622020-02-10 13:37:10 +00002208}
2209
2210func (s *sdkLibrarySdkMemberProperties) AddToPropertySet(ctx android.SdkMemberContext, propertySet android.BpPropertySet) {
Paul Duffinf8e08b22020-05-13 16:54:55 +01002211 if s.Naming_scheme != nil {
2212 propertySet.AddProperty("naming_scheme", proptools.String(s.Naming_scheme))
2213 }
Paul Duffina84756c2020-05-26 20:57:10 +01002214 if s.Shared_library != nil {
2215 propertySet.AddProperty("shared_library", *s.Shared_library)
2216 }
Paul Duffinf8e08b22020-05-13 16:54:55 +01002217
Paul Duffin61871622020-02-10 13:37:10 +00002218 for _, apiScope := range allApiScopes {
2219 if properties, ok := s.Scopes[apiScope]; ok {
Paul Duffin0f270632020-05-13 19:19:49 +01002220 scopeSet := propertySet.AddPropertySet(apiScope.propertyName)
Paul Duffin61871622020-02-10 13:37:10 +00002221
Paul Duffinf488ef22020-04-09 00:10:17 +01002222 scopeDir := filepath.Join("sdk_library", s.OsPrefix(), apiScope.name)
2223
Paul Duffin61871622020-02-10 13:37:10 +00002224 var jars []string
2225 for _, p := range properties.Jars {
Paul Duffinf488ef22020-04-09 00:10:17 +01002226 dest := filepath.Join(scopeDir, ctx.Name()+"-stubs.jar")
Paul Duffin61871622020-02-10 13:37:10 +00002227 ctx.SnapshotBuilder().CopyToSnapshot(p, dest)
2228 jars = append(jars, dest)
2229 }
2230 scopeSet.AddProperty("jars", jars)
2231
Paul Duffinf488ef22020-04-09 00:10:17 +01002232 // Merge the stubs source jar into the snapshot zip so that when it is unpacked
2233 // the source files are also unpacked.
2234 snapshotRelativeDir := filepath.Join(scopeDir, ctx.Name()+"_stub_sources")
2235 ctx.SnapshotBuilder().UnzipToSnapshot(properties.StubsSrcJar, snapshotRelativeDir)
2236 scopeSet.AddProperty("stub_srcs", []string{snapshotRelativeDir})
2237
Paul Duffin75dcc802020-04-09 01:08:11 +01002238 if properties.CurrentApiFile != nil {
2239 currentApiSnapshotPath := filepath.Join(scopeDir, ctx.Name()+".txt")
2240 ctx.SnapshotBuilder().CopyToSnapshot(properties.CurrentApiFile, currentApiSnapshotPath)
2241 scopeSet.AddProperty("current_api", currentApiSnapshotPath)
2242 }
2243
2244 if properties.RemovedApiFile != nil {
2245 removedApiSnapshotPath := filepath.Join(scopeDir, ctx.Name()+"-removed.txt")
Paul Duffinb1787352020-06-02 13:00:02 +01002246 ctx.SnapshotBuilder().CopyToSnapshot(properties.RemovedApiFile, removedApiSnapshotPath)
Paul Duffin75dcc802020-04-09 01:08:11 +01002247 scopeSet.AddProperty("removed_api", removedApiSnapshotPath)
2248 }
2249
Paul Duffin61871622020-02-10 13:37:10 +00002250 if properties.SdkVersion != "" {
2251 scopeSet.AddProperty("sdk_version", properties.SdkVersion)
2252 }
2253 }
2254 }
2255
2256 if len(s.Libs) > 0 {
2257 propertySet.AddPropertyWithTag("libs", s.Libs, ctx.SnapshotBuilder().SdkMemberReferencePropertyTag(false))
2258 }
2259}