blob: 6f939a92d76aca4f61d09456dc21750130d922fc [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 Duffin46a26a82020-04-07 19:27:04 +010021 "reflect"
Paul Duffin46dc45a2020-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 Duffin46a26a82020-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 Duffindd9d0742020-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 Duffinc8782502020-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
Paul Duffin80342d72020-06-26 22:08:43 +010073var _ android.ReplaceSourceWithPrebuilt = (*scopeDependencyTag)(nil)
74
75func (tag scopeDependencyTag) ReplaceSourceWithPrebuilt() bool {
76 return false
77}
78
Paul Duffind1b3a922020-01-22 11:57:20 +000079// Provides information about an api scope, e.g. public, system, test.
80type apiScope struct {
81 // The name of the api scope, e.g. public, system, test
82 name string
83
Paul Duffin97b53b82020-05-05 14:40:52 +010084 // The api scope that this scope extends.
85 extends *apiScope
86
Paul Duffin3375e352020-04-28 10:44:03 +010087 // The legacy enabled status for a specific scope can be dependent on other
88 // properties that have been specified on the library so it is provided by
89 // a function that can determine the status by examining those properties.
90 legacyEnabledStatus func(module *SdkLibrary) bool
91
92 // The default enabled status for non-legacy behavior, which is triggered by
93 // explicitly enabling at least one api scope.
94 defaultEnabledStatus bool
95
96 // Gets a pointer to the scope specific properties.
97 scopeSpecificProperties func(module *SdkLibrary) *ApiScopeProperties
98
Paul Duffin46a26a82020-04-07 19:27:04 +010099 // The name of the field in the dynamically created structure.
100 fieldName string
101
Paul Duffin6b836ba2020-05-13 19:19:49 +0100102 // The name of the property in the java_sdk_library_import
103 propertyName string
104
Paul Duffind1b3a922020-01-22 11:57:20 +0000105 // The tag to use to depend on the stubs library module.
106 stubsTag scopeDependencyTag
107
Paul Duffin0ff08bd2020-04-29 13:30:54 +0100108 // The tag to use to depend on the stubs source module (if separate from the API module).
109 stubsSourceTag scopeDependencyTag
110
111 // The tag to use to depend on the API file generating module (if separate from the stubs source module).
112 apiFileTag scopeDependencyTag
113
Paul Duffinc8782502020-04-29 20:45:27 +0100114 // The tag to use to depend on the stubs source and API module.
115 stubsSourceAndApiTag scopeDependencyTag
Paul Duffind1b3a922020-01-22 11:57:20 +0000116
117 // The scope specific prefix to add to the api file base of "current.txt" or "removed.txt".
118 apiFilePrefix string
119
120 // The scope specific prefix to add to the sdk library module name to construct a scope specific
121 // module name.
122 moduleSuffix string
123
Paul Duffind1b3a922020-01-22 11:57:20 +0000124 // SDK version that the stubs library is built against. Note that this is always
125 // *current. Older stubs library built with a numbered SDK version is created from
126 // the prebuilt jar.
127 sdkVersion string
Paul Duffin1fb487d2020-04-07 18:50:10 +0100128
Paul Duffin15f34ef2020-07-20 18:04:44 +0100129 // The annotation that identifies this API level, empty for the public API scope.
130 annotation string
131
Paul Duffin1fb487d2020-04-07 18:50:10 +0100132 // Extra arguments to pass to droidstubs for this scope.
Paul Duffin0ff08bd2020-04-29 13:30:54 +0100133 //
Paul Duffin15f34ef2020-07-20 18:04:44 +0100134 // This is not used directly but is used to construct the droidstubsArgs.
135 extraArgs []string
Paul Duffin0ff08bd2020-04-29 13:30:54 +0100136
Paul Duffin15f34ef2020-07-20 18:04:44 +0100137 // The args that must be passed to droidstubs to generate the API and stubs source
138 // for this scope, constructed dynamically by initApiScope().
Paul Duffin0ff08bd2020-04-29 13:30:54 +0100139 //
140 // The API only includes the additional members that this scope adds over the scope
141 // that it extends.
Paul Duffin15f34ef2020-07-20 18:04:44 +0100142 //
143 // The stubs source must include the definitions of everything that is in this
144 // api scope and all the scopes that this one extends.
145 droidstubsArgs []string
Paul Duffin0ff08bd2020-04-29 13:30:54 +0100146
Anton Hansson6478ac12020-05-02 11:19:36 +0100147 // Whether the api scope can be treated as unstable, and should skip compat checks.
148 unstable bool
Paul Duffind1b3a922020-01-22 11:57:20 +0000149}
150
151// Initialize a scope, creating and adding appropriate dependency tags
152func initApiScope(scope *apiScope) *apiScope {
Paul Duffinc8782502020-04-29 20:45:27 +0100153 name := scope.name
Paul Duffin46dc45a2020-05-14 15:39:10 +0100154 scopeByName[name] = scope
155 allScopeNames = append(allScopeNames, name)
Paul Duffin6b836ba2020-05-13 19:19:49 +0100156 scope.propertyName = strings.ReplaceAll(name, "-", "_")
157 scope.fieldName = proptools.FieldNameForProperty(scope.propertyName)
Paul Duffind1b3a922020-01-22 11:57:20 +0000158 scope.stubsTag = scopeDependencyTag{
Paul Duffinc8782502020-04-29 20:45:27 +0100159 name: name + "-stubs",
160 apiScope: scope,
161 depInfoExtractor: (*scopePaths).extractStubsLibraryInfoFromDependency,
Paul Duffind1b3a922020-01-22 11:57:20 +0000162 }
Paul Duffin0ff08bd2020-04-29 13:30:54 +0100163 scope.stubsSourceTag = scopeDependencyTag{
164 name: name + "-stubs-source",
165 apiScope: scope,
166 depInfoExtractor: (*scopePaths).extractStubsSourceInfoFromDep,
167 }
168 scope.apiFileTag = scopeDependencyTag{
169 name: name + "-api",
170 apiScope: scope,
171 depInfoExtractor: (*scopePaths).extractApiInfoFromDep,
172 }
Paul Duffinc8782502020-04-29 20:45:27 +0100173 scope.stubsSourceAndApiTag = scopeDependencyTag{
174 name: name + "-stubs-source-and-api",
175 apiScope: scope,
176 depInfoExtractor: (*scopePaths).extractStubsSourceAndApiInfoFromApiStubsProvider,
Paul Duffind1b3a922020-01-22 11:57:20 +0000177 }
Paul Duffin0ff08bd2020-04-29 13:30:54 +0100178
179 // To get the args needed to generate the stubs source append all the args from
180 // this scope and all the scopes it extends as each set of args adds additional
181 // members to the stubs.
Paul Duffin15f34ef2020-07-20 18:04:44 +0100182 var scopeSpecificArgs []string
183 if scope.annotation != "" {
184 scopeSpecificArgs = []string{"--show-annotation", scope.annotation}
Paul Duffin0ff08bd2020-04-29 13:30:54 +0100185 }
Paul Duffin15f34ef2020-07-20 18:04:44 +0100186 for s := scope; s != nil; s = s.extends {
187 scopeSpecificArgs = append(scopeSpecificArgs, s.extraArgs...)
Paul Duffin0ff08bd2020-04-29 13:30:54 +0100188
Paul Duffin15f34ef2020-07-20 18:04:44 +0100189 // Ensure that the generated stubs includes all the API elements from the API scope
190 // that this scope extends.
191 if s != scope && s.annotation != "" {
192 scopeSpecificArgs = append(scopeSpecificArgs, "--show-for-stub-purposes-annotation", s.annotation)
193 }
194 }
Paul Duffin0ff08bd2020-04-29 13:30:54 +0100195
Paul Duffin15f34ef2020-07-20 18:04:44 +0100196 // Escape any special characters in the arguments. This is needed because droidstubs
197 // passes these directly to the shell command.
198 scope.droidstubsArgs = proptools.ShellEscapeList(scopeSpecificArgs)
Paul Duffin0ff08bd2020-04-29 13:30:54 +0100199
Paul Duffind1b3a922020-01-22 11:57:20 +0000200 return scope
201}
202
Paul Duffinc3091c82020-05-08 14:16:20 +0100203func (scope *apiScope) stubsLibraryModuleName(baseName string) string {
Paul Duffindd9d0742020-05-08 15:52:37 +0100204 return baseName + ".stubs" + scope.moduleSuffix
Paul Duffind1b3a922020-01-22 11:57:20 +0000205}
206
Paul Duffinc8782502020-04-29 20:45:27 +0100207func (scope *apiScope) stubsSourceModuleName(baseName string) string {
Paul Duffindd9d0742020-05-08 15:52:37 +0100208 return baseName + ".stubs.source" + scope.moduleSuffix
Paul Duffind1b3a922020-01-22 11:57:20 +0000209}
210
Paul Duffin0ff08bd2020-04-29 13:30:54 +0100211func (scope *apiScope) apiModuleName(baseName string) string {
Paul Duffindd9d0742020-05-08 15:52:37 +0100212 return baseName + ".api" + scope.moduleSuffix
Paul Duffin0ff08bd2020-04-29 13:30:54 +0100213}
214
Paul Duffin3375e352020-04-28 10:44:03 +0100215func (scope *apiScope) String() string {
216 return scope.name
217}
218
Paul Duffind1b3a922020-01-22 11:57:20 +0000219type apiScopes []*apiScope
220
221func (scopes apiScopes) Strings(accessor func(*apiScope) string) []string {
222 var list []string
223 for _, scope := range scopes {
224 list = append(list, accessor(scope))
225 }
226 return list
227}
228
Jiyong Parkc678ad32018-04-10 13:07:10 +0900229var (
Paul Duffin46dc45a2020-05-14 15:39:10 +0100230 scopeByName = make(map[string]*apiScope)
231 allScopeNames []string
Paul Duffind1b3a922020-01-22 11:57:20 +0000232 apiScopePublic = initApiScope(&apiScope{
Paul Duffin3375e352020-04-28 10:44:03 +0100233 name: "public",
234
235 // Public scope is enabled by default for both legacy and non-legacy modes.
236 legacyEnabledStatus: func(module *SdkLibrary) bool {
237 return true
238 },
239 defaultEnabledStatus: true,
240
241 scopeSpecificProperties: func(module *SdkLibrary) *ApiScopeProperties {
242 return &module.sdkLibraryProperties.Public
243 },
Paul Duffind1b3a922020-01-22 11:57:20 +0000244 sdkVersion: "current",
245 })
246 apiScopeSystem = initApiScope(&apiScope{
Paul Duffin3375e352020-04-28 10:44:03 +0100247 name: "system",
248 extends: apiScopePublic,
249 legacyEnabledStatus: (*SdkLibrary).generateTestAndSystemScopesByDefault,
250 scopeSpecificProperties: func(module *SdkLibrary) *ApiScopeProperties {
251 return &module.sdkLibraryProperties.System
252 },
Paul Duffin15f34ef2020-07-20 18:04:44 +0100253 apiFilePrefix: "system-",
254 moduleSuffix: ".system",
255 sdkVersion: "system_current",
256 annotation: "android.annotation.SystemApi(client=android.annotation.SystemApi.Client.PRIVILEGED_APPS)",
Paul Duffind1b3a922020-01-22 11:57:20 +0000257 })
258 apiScopeTest = initApiScope(&apiScope{
Paul Duffin3375e352020-04-28 10:44:03 +0100259 name: "test",
260 extends: apiScopePublic,
261 legacyEnabledStatus: (*SdkLibrary).generateTestAndSystemScopesByDefault,
262 scopeSpecificProperties: func(module *SdkLibrary) *ApiScopeProperties {
263 return &module.sdkLibraryProperties.Test
264 },
Paul Duffin15f34ef2020-07-20 18:04:44 +0100265 apiFilePrefix: "test-",
266 moduleSuffix: ".test",
267 sdkVersion: "test_current",
268 annotation: "android.annotation.TestApi",
269 unstable: true,
Paul Duffind1b3a922020-01-22 11:57:20 +0000270 })
Paul Duffin8f265b92020-04-28 14:13:56 +0100271 apiScopeModuleLib = initApiScope(&apiScope{
Paul Duffin6b836ba2020-05-13 19:19:49 +0100272 name: "module-lib",
Paul Duffin8f265b92020-04-28 14:13:56 +0100273 extends: apiScopeSystem,
Paul Duffin0c5bae52020-06-02 13:00:08 +0100274 // The module-lib scope is disabled by default in legacy mode.
Paul Duffin8f265b92020-04-28 14:13:56 +0100275 //
276 // Enabling this would break existing usages.
277 legacyEnabledStatus: func(module *SdkLibrary) bool {
278 return false
279 },
280 scopeSpecificProperties: func(module *SdkLibrary) *ApiScopeProperties {
281 return &module.sdkLibraryProperties.Module_lib
282 },
283 apiFilePrefix: "module-lib-",
284 moduleSuffix: ".module_lib",
285 sdkVersion: "module_current",
Paul Duffin15f34ef2020-07-20 18:04:44 +0100286 annotation: "android.annotation.SystemApi(client=android.annotation.SystemApi.Client.MODULE_LIBRARIES)",
Paul Duffin8f265b92020-04-28 14:13:56 +0100287 })
Paul Duffin0c5bae52020-06-02 13:00:08 +0100288 apiScopeSystemServer = initApiScope(&apiScope{
289 name: "system-server",
290 extends: apiScopePublic,
291 // The system-server scope is disabled by default in legacy mode.
292 //
293 // Enabling this would break existing usages.
294 legacyEnabledStatus: func(module *SdkLibrary) bool {
295 return false
296 },
297 scopeSpecificProperties: func(module *SdkLibrary) *ApiScopeProperties {
298 return &module.sdkLibraryProperties.System_server
299 },
300 apiFilePrefix: "system-server-",
301 moduleSuffix: ".system_server",
302 sdkVersion: "system_server_current",
Paul Duffin15f34ef2020-07-20 18:04:44 +0100303 annotation: "android.annotation.SystemApi(client=android.annotation.SystemApi.Client.SYSTEM_SERVER)",
304 extraArgs: []string{
305 "--hide-annotation", "android.annotation.Hide",
Paul Duffin0c5bae52020-06-02 13:00:08 +0100306 // com.android.* classes are okay in this interface"
Paul Duffin15f34ef2020-07-20 18:04:44 +0100307 "--hide", "InternalClasses",
Paul Duffin0c5bae52020-06-02 13:00:08 +0100308 },
309 })
Paul Duffind1b3a922020-01-22 11:57:20 +0000310 allApiScopes = apiScopes{
311 apiScopePublic,
312 apiScopeSystem,
313 apiScopeTest,
Paul Duffin8f265b92020-04-28 14:13:56 +0100314 apiScopeModuleLib,
Paul Duffin0c5bae52020-06-02 13:00:08 +0100315 apiScopeSystemServer,
Paul Duffind1b3a922020-01-22 11:57:20 +0000316 }
Jiyong Parkc678ad32018-04-10 13:07:10 +0900317)
318
Jiyong Park82484c02018-04-23 21:41:26 +0900319var (
320 javaSdkLibrariesLock sync.Mutex
321)
322
Jiyong Parkc678ad32018-04-10 13:07:10 +0900323// TODO: these are big features that are currently missing
Jiyong Park1be96912018-05-28 18:02:19 +0900324// 1) disallowing linking to the runtime shared lib
325// 2) HTML generation
Jiyong Parkc678ad32018-04-10 13:07:10 +0900326
327func init() {
Paul Duffin43dc1cc2019-12-19 11:18:54 +0000328 RegisterSdkLibraryBuildComponents(android.InitRegistrationContext)
Jiyong Parkc678ad32018-04-10 13:07:10 +0900329
Jiyong Park82484c02018-04-23 21:41:26 +0900330 android.RegisterMakeVarsProvider(pctx, func(ctx android.MakeVarsContext) {
331 javaSdkLibraries := javaSdkLibraries(ctx.Config())
332 sort.Strings(*javaSdkLibraries)
333 ctx.Strict("JAVA_SDK_LIBRARIES", strings.Join(*javaSdkLibraries, " "))
334 })
Paul Duffindd46f712020-02-10 13:37:10 +0000335
336 // Register sdk member types.
337 android.RegisterSdkMemberType(&sdkLibrarySdkMemberType{
338 android.SdkMemberTypeBase{
339 PropertyName: "java_sdk_libs",
340 SupportsSdk: true,
341 },
342 })
Jiyong Parkc678ad32018-04-10 13:07:10 +0900343}
344
Paul Duffin43dc1cc2019-12-19 11:18:54 +0000345func RegisterSdkLibraryBuildComponents(ctx android.RegistrationContext) {
346 ctx.RegisterModuleType("java_sdk_library", SdkLibraryFactory)
347 ctx.RegisterModuleType("java_sdk_library_import", sdkLibraryImportFactory)
348}
349
Paul Duffin3375e352020-04-28 10:44:03 +0100350// Properties associated with each api scope.
351type ApiScopeProperties struct {
352 // Indicates whether the api surface is generated.
353 //
354 // If this is set for any scope then all scopes must explicitly specify if they
355 // are enabled. This is to prevent new usages from depending on legacy behavior.
356 //
357 // Otherwise, if this is not set for any scope then the default behavior is
358 // scope specific so please refer to the scope specific property documentation.
359 Enabled *bool
Paul Duffin87a05a32020-05-12 11:50:28 +0100360
361 // The sdk_version to use for building the stubs.
362 //
363 // If not specified then it will use an sdk_version determined as follows:
364 // 1) If the sdk_version specified on the java_sdk_library is none then this
365 // will be none. This is used for java_sdk_library instances that are used
366 // to create stubs that contribute to the core_current sdk version.
367 // 2) Otherwise, it is assumed that this library extends but does not contribute
368 // directly to a specific sdk_version and so this uses the sdk_version appropriate
369 // for the api scope. e.g. public will use sdk_version: current, system will use
370 // sdk_version: system_current, etc.
371 //
372 // This does not affect the sdk_version used for either generating the stubs source
373 // or the API file. They both have to use the same sdk_version as is used for
374 // compiling the implementation library.
375 Sdk_version *string
Paul Duffin3375e352020-04-28 10:44:03 +0100376}
377
Jiyong Parkc678ad32018-04-10 13:07:10 +0900378type sdkLibraryProperties struct {
Paul Duffin5df79302020-05-16 15:52:12 +0100379 // Visibility for impl library module. If not specified then defaults to the
380 // visibility property.
381 Impl_library_visibility []string
382
Paul Duffin4911a892020-04-29 23:35:13 +0100383 // Visibility for stubs library modules. If not specified then defaults to the
384 // visibility property.
385 Stubs_library_visibility []string
386
387 // Visibility for stubs source modules. If not specified then defaults to the
388 // visibility property.
389 Stubs_source_visibility []string
390
Sundong Ahnf043cf62018-06-25 16:04:37 +0900391 // List of Java libraries that will be in the classpath when building stubs
392 Stub_only_libs []string `android:"arch_variant"`
393
Paul Duffin7a586d32019-12-30 17:09:34 +0000394 // list of package names that will be documented and publicized as API.
395 // This allows the API to be restricted to a subset of the source files provided.
396 // If this is unspecified then all the source files will be treated as being part
397 // of the API.
Jiyong Parkc678ad32018-04-10 13:07:10 +0900398 Api_packages []string
399
Jiyong Park5a2c9d72018-05-01 22:25:41 +0900400 // list of package names that must be hidden from the API
401 Hidden_api_packages []string
402
Paul Duffin749f98f2019-12-30 17:23:46 +0000403 // the relative path to the directory containing the api specification files.
404 // Defaults to "api".
405 Api_dir *string
406
Paul Duffindfa131e2020-05-15 20:37:11 +0100407 // Determines whether a runtime implementation library is built; defaults to false.
408 //
409 // If true then it also prevents the module from being used as a shared module, i.e.
410 // it is as is shared_library: false, was set.
Paul Duffin43db9be2019-12-30 17:35:49 +0000411 Api_only *bool
412
Paul Duffin11512472019-02-11 15:55:17 +0000413 // local files that are used within user customized droiddoc options.
414 Droiddoc_option_files []string
415
416 // additional droiddoc options
417 // Available variables for substitution:
418 //
419 // $(location <label>): the path to the droiddoc_option_files with name <label>
Sundong Ahndd567f92018-07-31 17:19:11 +0900420 Droiddoc_options []string
421
Sundong Ahn054b19a2018-10-19 13:46:09 +0900422 // a list of top-level directories containing files to merge qualifier annotations
423 // (i.e. those intended to be included in the stubs written) from.
424 Merge_annotations_dirs []string
425
426 // a list of top-level directories containing Java stub files to merge show/hide annotations from.
427 Merge_inclusion_annotations_dirs []string
428
429 // If set to true, the path of dist files is apistubs/core. Defaults to false.
430 Core_lib *bool
431
Sundong Ahn80a87b32019-05-13 15:02:50 +0900432 // don't create dist rules.
433 No_dist *bool `blueprint:"mutated"`
434
Paul Duffin3375e352020-04-28 10:44:03 +0100435 // indicates whether system and test apis should be generated.
436 Generate_system_and_test_apis bool `blueprint:"mutated"`
437
438 // The properties specific to the public api scope
439 //
440 // Unless explicitly specified by using public.enabled the public api scope is
441 // enabled by default in both legacy and non-legacy mode.
442 Public ApiScopeProperties
443
444 // The properties specific to the system api scope
445 //
446 // In legacy mode the system api scope is enabled by default when sdk_version
447 // is set to something other than "none".
448 //
449 // In non-legacy mode the system api scope is disabled by default.
450 System ApiScopeProperties
451
452 // The properties specific to the test api scope
453 //
454 // In legacy mode the test api scope is enabled by default when sdk_version
455 // is set to something other than "none".
456 //
457 // In non-legacy mode the test api scope is disabled by default.
458 Test ApiScopeProperties
Paul Duffin37e0b772019-12-30 17:20:10 +0000459
Paul Duffin0c5bae52020-06-02 13:00:08 +0100460 // The properties specific to the module-lib api scope
Paul Duffin8f265b92020-04-28 14:13:56 +0100461 //
Paul Duffin0c5bae52020-06-02 13:00:08 +0100462 // Unless explicitly specified by using test.enabled the module-lib api scope is
Paul Duffin8f265b92020-04-28 14:13:56 +0100463 // disabled by default.
464 Module_lib ApiScopeProperties
465
Paul Duffin0c5bae52020-06-02 13:00:08 +0100466 // The properties specific to the system-server api scope
467 //
468 // Unless explicitly specified by using test.enabled the module-lib api scope is
469 // disabled by default.
470 System_server ApiScopeProperties
471
Jiyong Park932cdfe2020-05-28 00:19:53 +0900472 // Determines if the stubs are preferred over the implementation library
473 // for linking, even when the client doesn't specify sdk_version. When this
474 // is set to true, such clients are provided with the widest API surface that
475 // this lib provides. Note however that this option doesn't affect the clients
476 // that are in the same APEX as this library. In that case, the clients are
477 // always linked with the implementation library. Default is false.
478 Default_to_stubs *bool
479
Paul Duffin160fe412020-05-10 19:32:20 +0100480 // Properties related to api linting.
481 Api_lint struct {
482 // Enable api linting.
483 Enabled *bool
484 }
485
Jiyong Parkc678ad32018-04-10 13:07:10 +0900486 // TODO: determines whether to create HTML doc or not
487 //Html_doc *bool
488}
489
Paul Duffin0f8faff2020-05-20 16:18:00 +0100490// Paths to outputs from java_sdk_library and java_sdk_library_import.
491//
492// Fields that are android.Paths are always set (during GenerateAndroidBuildActions).
493// OptionalPaths are always set by java_sdk_library but may not be set by
494// java_sdk_library_import as not all instances provide that information.
Paul Duffind1b3a922020-01-22 11:57:20 +0000495type scopePaths struct {
Paul Duffin0f8faff2020-05-20 16:18:00 +0100496 // The path (represented as Paths for convenience when returning) to the stubs header jar.
497 //
498 // That is the jar that is created by turbine.
499 stubsHeaderPath android.Paths
500
501 // The path (represented as Paths for convenience when returning) to the stubs implementation jar.
502 //
503 // This is not the implementation jar, it still only contains stubs.
504 stubsImplPath android.Paths
505
506 // The API specification file, e.g. system_current.txt.
507 currentApiFilePath android.OptionalPath
508
509 // The specification of API elements removed since the last release.
510 removedApiFilePath android.OptionalPath
511
512 // The stubs source jar.
513 stubsSrcJar android.OptionalPath
Paul Duffind1b3a922020-01-22 11:57:20 +0000514}
515
Paul Duffinc8782502020-04-29 20:45:27 +0100516func (paths *scopePaths) extractStubsLibraryInfoFromDependency(dep android.Module) error {
517 if lib, ok := dep.(Dependency); ok {
518 paths.stubsHeaderPath = lib.HeaderJars()
519 paths.stubsImplPath = lib.ImplementationJars()
520 return nil
521 } else {
522 return fmt.Errorf("expected module that implements Dependency, e.g. java_library")
523 }
524}
525
Paul Duffin0ff08bd2020-04-29 13:30:54 +0100526func (paths *scopePaths) treatDepAsApiStubsProvider(dep android.Module, action func(provider ApiStubsProvider)) error {
527 if apiStubsProvider, ok := dep.(ApiStubsProvider); ok {
528 action(apiStubsProvider)
Paul Duffinc8782502020-04-29 20:45:27 +0100529 return nil
530 } else {
531 return fmt.Errorf("expected module that implements ApiStubsProvider, e.g. droidstubs")
532 }
533}
534
Paul Duffin0f8faff2020-05-20 16:18:00 +0100535func (paths *scopePaths) treatDepAsApiStubsSrcProvider(dep android.Module, action func(provider ApiStubsSrcProvider)) error {
536 if apiStubsProvider, ok := dep.(ApiStubsSrcProvider); ok {
537 action(apiStubsProvider)
538 return nil
539 } else {
540 return fmt.Errorf("expected module that implements ApiStubsSrcProvider, e.g. droidstubs")
541 }
542}
543
Paul Duffin0ff08bd2020-04-29 13:30:54 +0100544func (paths *scopePaths) extractApiInfoFromApiStubsProvider(provider ApiStubsProvider) {
Paul Duffin0f8faff2020-05-20 16:18:00 +0100545 paths.currentApiFilePath = android.OptionalPathForPath(provider.ApiFilePath())
546 paths.removedApiFilePath = android.OptionalPathForPath(provider.RemovedApiFilePath())
Paul Duffin0ff08bd2020-04-29 13:30:54 +0100547}
548
549func (paths *scopePaths) extractApiInfoFromDep(dep android.Module) error {
550 return paths.treatDepAsApiStubsProvider(dep, func(provider ApiStubsProvider) {
551 paths.extractApiInfoFromApiStubsProvider(provider)
552 })
553}
554
Paul Duffin0f8faff2020-05-20 16:18:00 +0100555func (paths *scopePaths) extractStubsSourceInfoFromApiStubsProviders(provider ApiStubsSrcProvider) {
556 paths.stubsSrcJar = android.OptionalPathForPath(provider.StubsSrcJar())
Paul Duffin0ff08bd2020-04-29 13:30:54 +0100557}
558
559func (paths *scopePaths) extractStubsSourceInfoFromDep(dep android.Module) error {
Paul Duffin0f8faff2020-05-20 16:18:00 +0100560 return paths.treatDepAsApiStubsSrcProvider(dep, func(provider ApiStubsSrcProvider) {
Paul Duffin0ff08bd2020-04-29 13:30:54 +0100561 paths.extractStubsSourceInfoFromApiStubsProviders(provider)
562 })
563}
564
565func (paths *scopePaths) extractStubsSourceAndApiInfoFromApiStubsProvider(dep android.Module) error {
566 return paths.treatDepAsApiStubsProvider(dep, func(provider ApiStubsProvider) {
567 paths.extractApiInfoFromApiStubsProvider(provider)
568 paths.extractStubsSourceInfoFromApiStubsProviders(provider)
569 })
570}
571
572type commonToSdkLibraryAndImportProperties struct {
Paul Duffin1b1e8062020-05-08 13:44:43 +0100573 // The naming scheme to use for the components that this module creates.
574 //
Paul Duffin6c9c5fc2020-05-08 15:36:30 +0100575 // If not specified then it defaults to "default". The other allowable value is
576 // "framework-modules" which matches the scheme currently used by framework modules
577 // for the equivalent components represented as separate Soong modules.
Paul Duffin1b1e8062020-05-08 13:44:43 +0100578 //
579 // This is a temporary mechanism to simplify conversion from separate modules for each
580 // component that follow a different naming pattern to the default one.
581 //
582 // TODO(b/155480189) - Remove once naming inconsistencies have been resolved.
Paul Duffin0ff08bd2020-04-29 13:30:54 +0100583 Naming_scheme *string
Paul Duffindfa131e2020-05-15 20:37:11 +0100584
585 // Specifies whether this module can be used as an Android shared library; defaults
586 // to true.
587 //
588 // An Android shared library is one that can be referenced in a <uses-library> element
589 // in an AndroidManifest.xml.
590 Shared_library *bool
Paul Duffin0ff08bd2020-04-29 13:30:54 +0100591}
592
Paul Duffin56d44902020-01-31 13:36:25 +0000593// Common code between sdk library and sdk library import
594type commonToSdkLibraryAndImport struct {
Paul Duffinc3091c82020-05-08 14:16:20 +0100595 moduleBase *android.ModuleBase
596
Paul Duffin56d44902020-01-31 13:36:25 +0000597 scopePaths map[*apiScope]*scopePaths
Paul Duffin1b1e8062020-05-08 13:44:43 +0100598
599 namingScheme sdkLibraryComponentNamingScheme
600
Paul Duffindfa131e2020-05-15 20:37:11 +0100601 commonSdkLibraryProperties commonToSdkLibraryAndImportProperties
Paul Duffin859fe962020-05-15 10:20:31 +0100602
603 // Functionality related to this being used as a component of a java_sdk_library.
604 EmbeddableSdkLibraryComponent
Paul Duffin56d44902020-01-31 13:36:25 +0000605}
606
Paul Duffinc3091c82020-05-08 14:16:20 +0100607func (c *commonToSdkLibraryAndImport) initCommon(moduleBase *android.ModuleBase) {
608 c.moduleBase = moduleBase
Paul Duffin1b1e8062020-05-08 13:44:43 +0100609
Paul Duffindfa131e2020-05-15 20:37:11 +0100610 moduleBase.AddProperties(&c.commonSdkLibraryProperties)
Paul Duffin859fe962020-05-15 10:20:31 +0100611
612 // Initialize this as an sdk library component.
613 c.initSdkLibraryComponent(moduleBase)
Paul Duffin1b1e8062020-05-08 13:44:43 +0100614}
615
616func (c *commonToSdkLibraryAndImport) initCommonAfterDefaultsApplied(ctx android.DefaultableHookContext) bool {
Paul Duffindfa131e2020-05-15 20:37:11 +0100617 schemeProperty := proptools.StringDefault(c.commonSdkLibraryProperties.Naming_scheme, "default")
Paul Duffin1b1e8062020-05-08 13:44:43 +0100618 switch schemeProperty {
619 case "default":
620 c.namingScheme = &defaultNamingScheme{}
Paul Duffin6c9c5fc2020-05-08 15:36:30 +0100621 case "framework-modules":
622 c.namingScheme = &frameworkModulesNamingScheme{}
Paul Duffin1b1e8062020-05-08 13:44:43 +0100623 default:
624 ctx.PropertyErrorf("naming_scheme", "expected 'default' but was %q", schemeProperty)
625 return false
626 }
627
Paul Duffindfa131e2020-05-15 20:37:11 +0100628 // Only track this sdk library if this can be used as a shared library.
629 if c.sharedLibrary() {
630 // Use the name specified in the module definition as the owner.
631 c.sdkLibraryComponentProperties.SdkLibraryToImplicitlyTrack = proptools.StringPtr(c.moduleBase.BaseModuleName())
632 }
Paul Duffin859fe962020-05-15 10:20:31 +0100633
Paul Duffin1b1e8062020-05-08 13:44:43 +0100634 return true
Paul Duffinc3091c82020-05-08 14:16:20 +0100635}
636
Paul Duffineedc5d52020-06-12 17:46:39 +0100637// Module name of the runtime implementation library
638func (c *commonToSdkLibraryAndImport) implLibraryModuleName() string {
639 return c.moduleBase.BaseModuleName() + ".impl"
640}
641
642// Module name of the XML file for the lib
643func (c *commonToSdkLibraryAndImport) xmlPermissionsModuleName() string {
644 return c.moduleBase.BaseModuleName() + sdkXmlFileSuffix
645}
646
Paul Duffinc3091c82020-05-08 14:16:20 +0100647// Name of the java_library module that compiles the stubs source.
648func (c *commonToSdkLibraryAndImport) stubsLibraryModuleName(apiScope *apiScope) string {
Paul Duffin1b1e8062020-05-08 13:44:43 +0100649 return c.namingScheme.stubsLibraryModuleName(apiScope, c.moduleBase.BaseModuleName())
Paul Duffinc3091c82020-05-08 14:16:20 +0100650}
651
652// Name of the droidstubs module that generates the stubs source and may also
653// generate/check the API.
654func (c *commonToSdkLibraryAndImport) stubsSourceModuleName(apiScope *apiScope) string {
Paul Duffin1b1e8062020-05-08 13:44:43 +0100655 return c.namingScheme.stubsSourceModuleName(apiScope, c.moduleBase.BaseModuleName())
Paul Duffinc3091c82020-05-08 14:16:20 +0100656}
657
658// Name of the droidstubs module that generates/checks the API. Only used if it
659// requires different arts to the stubs source generating module.
660func (c *commonToSdkLibraryAndImport) apiModuleName(apiScope *apiScope) string {
Paul Duffin1b1e8062020-05-08 13:44:43 +0100661 return c.namingScheme.apiModuleName(apiScope, c.moduleBase.BaseModuleName())
Paul Duffinc3091c82020-05-08 14:16:20 +0100662}
663
Paul Duffin46dc45a2020-05-14 15:39:10 +0100664// The component names for different outputs of the java_sdk_library.
665//
666// They are similar to the names used for the child modules it creates
667const (
668 stubsSourceComponentName = "stubs.source"
669
670 apiTxtComponentName = "api.txt"
671
672 removedApiTxtComponentName = "removed-api.txt"
673)
674
675// A regular expression to match tags that reference a specific stubs component.
676//
677// It will only match if given a valid scope and a valid component. It is verfy strict
678// to ensure it does not accidentally match a similar looking tag that should be processed
679// by the embedded Library.
680var tagSplitter = func() *regexp.Regexp {
681 // Given a list of literal string items returns a regular expression that will
682 // match any one of the items.
683 choice := func(items ...string) string {
684 return `\Q` + strings.Join(items, `\E|\Q`) + `\E`
685 }
686
687 // Regular expression to match one of the scopes.
688 scopesRegexp := choice(allScopeNames...)
689
690 // Regular expression to match one of the components.
691 componentsRegexp := choice(stubsSourceComponentName, apiTxtComponentName, removedApiTxtComponentName)
692
693 // Regular expression to match any combination of one scope and one component.
694 return regexp.MustCompile(fmt.Sprintf(`^\.(%s)\.(%s)$`, scopesRegexp, componentsRegexp))
695}()
696
697// For OutputFileProducer interface
698//
699// .<scope>.stubs.source
700// .<scope>.api.txt
701// .<scope>.removed-api.txt
702func (c *commonToSdkLibraryAndImport) commonOutputFiles(tag string) (android.Paths, error) {
703 if groups := tagSplitter.FindStringSubmatch(tag); groups != nil {
704 scopeName := groups[1]
705 component := groups[2]
706
707 if scope, ok := scopeByName[scopeName]; ok {
708 paths := c.findScopePaths(scope)
709 if paths == nil {
710 return nil, fmt.Errorf("%q does not provide api scope %s", c.moduleBase.BaseModuleName(), scopeName)
711 }
712
713 switch component {
714 case stubsSourceComponentName:
715 if paths.stubsSrcJar.Valid() {
716 return android.Paths{paths.stubsSrcJar.Path()}, nil
717 }
718
719 case apiTxtComponentName:
720 if paths.currentApiFilePath.Valid() {
721 return android.Paths{paths.currentApiFilePath.Path()}, nil
722 }
723
724 case removedApiTxtComponentName:
725 if paths.removedApiFilePath.Valid() {
726 return android.Paths{paths.removedApiFilePath.Path()}, nil
727 }
728 }
729
730 return nil, fmt.Errorf("%s not available for api scope %s", component, scopeName)
731 } else {
732 return nil, fmt.Errorf("unknown scope %s in %s", scope, tag)
733 }
734
735 } else {
736 return nil, nil
737 }
738}
739
Paul Duffin803a9562020-05-20 11:52:25 +0100740func (c *commonToSdkLibraryAndImport) getScopePathsCreateIfNeeded(scope *apiScope) *scopePaths {
Paul Duffin56d44902020-01-31 13:36:25 +0000741 if c.scopePaths == nil {
742 c.scopePaths = make(map[*apiScope]*scopePaths)
743 }
744 paths := c.scopePaths[scope]
745 if paths == nil {
746 paths = &scopePaths{}
747 c.scopePaths[scope] = paths
748 }
749
750 return paths
751}
752
Paul Duffin803a9562020-05-20 11:52:25 +0100753func (c *commonToSdkLibraryAndImport) findScopePaths(scope *apiScope) *scopePaths {
754 if c.scopePaths == nil {
755 return nil
756 }
757
758 return c.scopePaths[scope]
759}
760
761// If this does not support the requested api scope then find the closest available
762// scope it does support. Returns nil if no such scope is available.
763func (c *commonToSdkLibraryAndImport) findClosestScopePath(scope *apiScope) *scopePaths {
764 for s := scope; s != nil; s = s.extends {
765 if paths := c.findScopePaths(s); paths != nil {
766 return paths
767 }
768 }
769
770 // This should never happen outside tests as public should be the base scope for every
771 // scope and is enabled by default.
772 return nil
773}
774
Paul Duffin23970f42020-05-20 14:20:02 +0100775func (c *commonToSdkLibraryAndImport) selectHeaderJarsForSdkVersion(ctx android.BaseModuleContext, sdkVersion sdkSpec) android.Paths {
Paul Duffinb05d4292020-05-20 12:19:10 +0100776
777 // If a specific numeric version has been requested then use prebuilt versions of the sdk.
778 if sdkVersion.version.isNumbered() {
779 return PrebuiltJars(ctx, c.moduleBase.BaseModuleName(), sdkVersion)
780 }
781
782 var apiScope *apiScope
783 switch sdkVersion.kind {
784 case sdkSystem:
785 apiScope = apiScopeSystem
Paul Duffin803a9562020-05-20 11:52:25 +0100786 case sdkModule:
787 apiScope = apiScopeModuleLib
Paul Duffinb05d4292020-05-20 12:19:10 +0100788 case sdkTest:
789 apiScope = apiScopeTest
Paul Duffin0c5bae52020-06-02 13:00:08 +0100790 case sdkSystemServer:
791 apiScope = apiScopeSystemServer
Paul Duffinb05d4292020-05-20 12:19:10 +0100792 default:
793 apiScope = apiScopePublic
794 }
795
Paul Duffin803a9562020-05-20 11:52:25 +0100796 paths := c.findClosestScopePath(apiScope)
797 if paths == nil {
798 var scopes []string
799 for _, s := range allApiScopes {
800 if c.findScopePaths(s) != nil {
801 scopes = append(scopes, s.name)
802 }
803 }
804 ctx.ModuleErrorf("requires api scope %s from %s but it only has %q available", apiScope.name, c.moduleBase.BaseModuleName(), scopes)
805 return nil
806 }
807
Paul Duffin23970f42020-05-20 14:20:02 +0100808 return paths.stubsHeaderPath
Paul Duffinb05d4292020-05-20 12:19:10 +0100809}
810
Paul Duffin859fe962020-05-15 10:20:31 +0100811func (c *commonToSdkLibraryAndImport) sdkComponentPropertiesForChildLibrary() interface{} {
812 componentProps := &struct {
813 SdkLibraryToImplicitlyTrack *string
Paul Duffindfa131e2020-05-15 20:37:11 +0100814 }{}
815
816 if c.sharedLibrary() {
Paul Duffin859fe962020-05-15 10:20:31 +0100817 // Mark the stubs library as being components of this java_sdk_library so that
818 // any app that includes code which depends (directly or indirectly) on the stubs
819 // library will have the appropriate <uses-library> invocation inserted into its
820 // manifest if necessary.
Paul Duffindfa131e2020-05-15 20:37:11 +0100821 componentProps.SdkLibraryToImplicitlyTrack = proptools.StringPtr(c.moduleBase.BaseModuleName())
Paul Duffin859fe962020-05-15 10:20:31 +0100822 }
823
824 return componentProps
825}
826
Paul Duffindfa131e2020-05-15 20:37:11 +0100827// Check if this can be used as a shared library.
828func (c *commonToSdkLibraryAndImport) sharedLibrary() bool {
829 return proptools.BoolDefault(c.commonSdkLibraryProperties.Shared_library, true)
830}
831
Paul Duffin859fe962020-05-15 10:20:31 +0100832// Properties related to the use of a module as an component of a java_sdk_library.
833type SdkLibraryComponentProperties struct {
834
835 // The name of the java_sdk_library/_import to add to a <uses-library> entry
836 // in the AndroidManifest.xml of any Android app that includes code that references
837 // this module. If not set then no java_sdk_library/_import is tracked.
838 SdkLibraryToImplicitlyTrack *string `blueprint:"mutated"`
839}
840
841// Structure to be embedded in a module struct that needs to support the
842// SdkLibraryComponentDependency interface.
843type EmbeddableSdkLibraryComponent struct {
844 sdkLibraryComponentProperties SdkLibraryComponentProperties
845}
846
847func (e *EmbeddableSdkLibraryComponent) initSdkLibraryComponent(moduleBase *android.ModuleBase) {
848 moduleBase.AddProperties(&e.sdkLibraryComponentProperties)
849}
850
851// to satisfy SdkLibraryComponentDependency
Ulya Trafimovich31e444e2020-08-14 17:32:16 +0100852func (e *EmbeddableSdkLibraryComponent) OptionalImplicitSdkLibrary() *string {
853 return e.sdkLibraryComponentProperties.SdkLibraryToImplicitlyTrack
Paul Duffin859fe962020-05-15 10:20:31 +0100854}
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 {
Ulya Trafimovich31e444e2020-08-14 17:32:16 +0100859 UsesLibraryDependency
860
Paul Duffin859fe962020-05-15 10:20:31 +0100861 // The optional name of the sdk library that should be implicitly added to the
862 // AndroidManifest of an app that contains code which references the sdk library.
863 //
Ulya Trafimovich31e444e2020-08-14 17:32:16 +0100864 // Returns the name of the optional implicit SDK library or nil, if there isn't one.
865 OptionalImplicitSdkLibrary() *string
Paul Duffin859fe962020-05-15 10:20:31 +0100866}
867
868// Make sure that all the module types that are components of java_sdk_library/_import
869// and which can be referenced (directly or indirectly) from an android app implement
870// the SdkLibraryComponentDependency interface.
871var _ SdkLibraryComponentDependency = (*Library)(nil)
872var _ SdkLibraryComponentDependency = (*Import)(nil)
873var _ SdkLibraryComponentDependency = (*SdkLibrary)(nil)
Paul Duffineedc5d52020-06-12 17:46:39 +0100874var _ SdkLibraryComponentDependency = (*SdkLibraryImport)(nil)
Paul Duffin859fe962020-05-15 10:20:31 +0100875
876// Provides access to sdk_version related header and implentation jars.
877type SdkLibraryDependency interface {
878 SdkLibraryComponentDependency
Ulya Trafimovich31e444e2020-08-14 17:32:16 +0100879 UsesLibraryDependency
Paul Duffin859fe962020-05-15 10:20:31 +0100880
881 // Get the header jars appropriate for the supplied sdk_version.
882 //
883 // These are turbine generated jars so they only change if the externals of the
884 // class changes but it does not contain and implementation or JavaDoc.
885 SdkHeaderJars(ctx android.BaseModuleContext, sdkVersion sdkSpec) android.Paths
886
887 // Get the implementation jars appropriate for the supplied sdk version.
888 //
889 // These are either the implementation jar for the whole sdk library or the implementation
890 // jars for the stubs. The latter should only be needed when generating JavaDoc as otherwise
891 // they are identical to the corresponding header jars.
892 SdkImplementationJars(ctx android.BaseModuleContext, sdkVersion sdkSpec) android.Paths
893}
894
Inseob Kimc0907f12019-02-08 21:00:45 +0900895type SdkLibrary struct {
Sundong Ahn054b19a2018-10-19 13:46:09 +0900896 Library
Jiyong Parkc678ad32018-04-10 13:07:10 +0900897
Sundong Ahn054b19a2018-10-19 13:46:09 +0900898 sdkLibraryProperties sdkLibraryProperties
Jiyong Parkc678ad32018-04-10 13:07:10 +0900899
Paul Duffin3375e352020-04-28 10:44:03 +0100900 // Map from api scope to the scope specific property structure.
901 scopeToProperties map[*apiScope]*ApiScopeProperties
902
Paul Duffin56d44902020-01-31 13:36:25 +0000903 commonToSdkLibraryAndImport
Jiyong Parkc678ad32018-04-10 13:07:10 +0900904}
905
Inseob Kimc0907f12019-02-08 21:00:45 +0900906var _ Dependency = (*SdkLibrary)(nil)
907var _ SdkLibraryDependency = (*SdkLibrary)(nil)
Colin Cross897d2ed2019-02-11 14:03:51 -0800908
Paul Duffin3375e352020-04-28 10:44:03 +0100909func (module *SdkLibrary) generateTestAndSystemScopesByDefault() bool {
910 return module.sdkLibraryProperties.Generate_system_and_test_apis
911}
912
913func (module *SdkLibrary) getGeneratedApiScopes(ctx android.EarlyModuleContext) apiScopes {
914 // Check to see if any scopes have been explicitly enabled. If any have then all
915 // must be.
916 anyScopesExplicitlyEnabled := false
917 for _, scope := range allApiScopes {
918 scopeProperties := module.scopeToProperties[scope]
919 if scopeProperties.Enabled != nil {
920 anyScopesExplicitlyEnabled = true
921 break
922 }
Paul Duffind1b3a922020-01-22 11:57:20 +0000923 }
Paul Duffin3375e352020-04-28 10:44:03 +0100924
925 var generatedScopes apiScopes
926 enabledScopes := make(map[*apiScope]struct{})
927 for _, scope := range allApiScopes {
928 scopeProperties := module.scopeToProperties[scope]
929 // If any scopes are explicitly enabled then ignore the legacy enabled status.
930 // This is to ensure that any new usages of this module type do not rely on legacy
931 // behaviour.
932 defaultEnabledStatus := false
933 if anyScopesExplicitlyEnabled {
934 defaultEnabledStatus = scope.defaultEnabledStatus
935 } else {
936 defaultEnabledStatus = scope.legacyEnabledStatus(module)
937 }
938 enabled := proptools.BoolDefault(scopeProperties.Enabled, defaultEnabledStatus)
939 if enabled {
940 enabledScopes[scope] = struct{}{}
941 generatedScopes = append(generatedScopes, scope)
942 }
943 }
944
945 // Now check to make sure that any scope that is extended by an enabled scope is also
946 // enabled.
947 for _, scope := range allApiScopes {
948 if _, ok := enabledScopes[scope]; ok {
949 extends := scope.extends
950 if extends != nil {
951 if _, ok := enabledScopes[extends]; !ok {
952 ctx.ModuleErrorf("enabled api scope %q depends on disabled scope %q", scope, extends)
953 }
954 }
955 }
956 }
957
958 return generatedScopes
Paul Duffind1b3a922020-01-22 11:57:20 +0000959}
960
Paul Duffineedc5d52020-06-12 17:46:39 +0100961type sdkLibraryComponentTag struct {
962 blueprint.BaseDependencyTag
963 name string
964}
965
966// Mark this tag so dependencies that use it are excluded from visibility enforcement.
967func (t sdkLibraryComponentTag) ExcludeFromVisibilityEnforcement() {}
968
969var xmlPermissionsFileTag = sdkLibraryComponentTag{name: "xml-permissions-file"}
Paul Duffine74ac732020-02-06 13:51:46 +0000970
Jiyong Parke3833882020-02-17 17:28:10 +0900971func IsXmlPermissionsFileDepTag(depTag blueprint.DependencyTag) bool {
Paul Duffineedc5d52020-06-12 17:46:39 +0100972 if dt, ok := depTag.(sdkLibraryComponentTag); ok {
Jiyong Parke3833882020-02-17 17:28:10 +0900973 return dt == xmlPermissionsFileTag
974 }
975 return false
976}
977
Paul Duffineedc5d52020-06-12 17:46:39 +0100978var implLibraryTag = sdkLibraryComponentTag{name: "impl-library"}
Paul Duffin5df79302020-05-16 15:52:12 +0100979
Paul Duffin44f1d842020-06-26 20:17:02 +0100980// Add the dependencies on the child modules in the component deps mutator.
981func (module *SdkLibrary) ComponentDepsMutator(ctx android.BottomUpMutatorContext) {
Paul Duffin3375e352020-04-28 10:44:03 +0100982 for _, apiScope := range module.getGeneratedApiScopes(ctx) {
Paul Duffind1b3a922020-01-22 11:57:20 +0000983 // Add dependencies to the stubs library
Paul Duffinc3091c82020-05-08 14:16:20 +0100984 ctx.AddVariationDependencies(nil, apiScope.stubsTag, module.stubsLibraryModuleName(apiScope))
Paul Duffind1b3a922020-01-22 11:57:20 +0000985
Paul Duffin15f34ef2020-07-20 18:04:44 +0100986 // Add a dependency on the stubs source in order to access both stubs source and api information.
987 ctx.AddVariationDependencies(nil, apiScope.stubsSourceAndApiTag, module.stubsSourceModuleName(apiScope))
Sundong Ahn054b19a2018-10-19 13:46:09 +0900988 }
989
Paul Duffindfa131e2020-05-15 20:37:11 +0100990 if module.requiresRuntimeImplementationLibrary() {
Paul Duffin5df79302020-05-16 15:52:12 +0100991 // Add dependency to the rule for generating the implementation library.
992 ctx.AddDependency(module, implLibraryTag, module.implLibraryModuleName())
993
Paul Duffindfa131e2020-05-15 20:37:11 +0100994 if module.sharedLibrary() {
995 // Add dependency to the rule for generating the xml permissions file
Paul Duffineedc5d52020-06-12 17:46:39 +0100996 ctx.AddDependency(module, xmlPermissionsFileTag, module.xmlPermissionsModuleName())
Paul Duffindfa131e2020-05-15 20:37:11 +0100997 }
Paul Duffin44f1d842020-06-26 20:17:02 +0100998 }
999}
Paul Duffine74ac732020-02-06 13:51:46 +00001000
Paul Duffin44f1d842020-06-26 20:17:02 +01001001// Add other dependencies as normal.
1002func (module *SdkLibrary) DepsMutator(ctx android.BottomUpMutatorContext) {
1003 if module.requiresRuntimeImplementationLibrary() {
Paul Duffindfa131e2020-05-15 20:37:11 +01001004 // Only add the deps for the library if it is actually going to be built.
1005 module.Library.deps(ctx)
1006 }
Jiyong Parkc678ad32018-04-10 13:07:10 +09001007}
1008
Paul Duffin46dc45a2020-05-14 15:39:10 +01001009func (module *SdkLibrary) OutputFiles(tag string) (android.Paths, error) {
1010 paths, err := module.commonOutputFiles(tag)
1011 if paths == nil && err == nil {
1012 return module.Library.OutputFiles(tag)
1013 } else {
1014 return paths, err
1015 }
1016}
1017
Inseob Kimc0907f12019-02-08 21:00:45 +09001018func (module *SdkLibrary) GenerateAndroidBuildActions(ctx android.ModuleContext) {
Paul Duffindfa131e2020-05-15 20:37:11 +01001019 // Only build an implementation library if required.
1020 if module.requiresRuntimeImplementationLibrary() {
Paul Duffin43db9be2019-12-30 17:35:49 +00001021 module.Library.GenerateAndroidBuildActions(ctx)
1022 }
Sundong Ahn054b19a2018-10-19 13:46:09 +09001023
Sundong Ahn57368eb2018-07-06 11:20:23 +09001024 // Record the paths to the header jars of the library (stubs and impl).
Paul Duffind1b3a922020-01-22 11:57:20 +00001025 // When this java_sdk_library is depended upon from others via "libs" property,
Jiyong Parkc678ad32018-04-10 13:07:10 +09001026 // the recorded paths will be returned depending on the link type of the caller.
1027 ctx.VisitDirectDeps(func(to android.Module) {
Jiyong Parkc678ad32018-04-10 13:07:10 +09001028 tag := ctx.OtherModuleDependencyTag(to)
1029
Paul Duffinc8782502020-04-29 20:45:27 +01001030 // Extract information from any of the scope specific dependencies.
1031 if scopeTag, ok := tag.(scopeDependencyTag); ok {
1032 apiScope := scopeTag.apiScope
Paul Duffin803a9562020-05-20 11:52:25 +01001033 scopePaths := module.getScopePathsCreateIfNeeded(apiScope)
Paul Duffinc8782502020-04-29 20:45:27 +01001034
1035 // Extract information from the dependency. The exact information extracted
1036 // is determined by the nature of the dependency which is determined by the tag.
1037 scopeTag.extractDepInfo(ctx, to, scopePaths)
Sundong Ahn20e998b2018-07-24 11:19:26 +09001038 }
Jiyong Parkc678ad32018-04-10 13:07:10 +09001039 })
1040}
1041
Jiyong Park0b0e1b92019-12-03 13:24:29 +09001042func (module *SdkLibrary) AndroidMkEntries() []android.AndroidMkEntries {
Paul Duffindfa131e2020-05-15 20:37:11 +01001043 if !module.requiresRuntimeImplementationLibrary() {
Paul Duffin43db9be2019-12-30 17:35:49 +00001044 return nil
1045 }
Jiyong Park0b0e1b92019-12-03 13:24:29 +09001046 entriesList := module.Library.AndroidMkEntries()
Yo Chiang07d75072020-06-05 17:43:19 +08001047 if module.sharedLibrary() {
1048 entries := &entriesList[0]
1049 entries.Required = append(entries.Required, module.xmlPermissionsModuleName())
1050 }
Jiyong Park0b0e1b92019-12-03 13:24:29 +09001051 return entriesList
Jiyong Park82484c02018-04-23 21:41:26 +09001052}
1053
Anton Hansson5fd5d242020-03-27 19:43:19 +00001054// The dist path of the stub artifacts
1055func (module *SdkLibrary) apiDistPath(apiScope *apiScope) string {
1056 if module.ModuleBase.Owner() != "" {
1057 return path.Join("apistubs", module.ModuleBase.Owner(), apiScope.name)
1058 } else if Bool(module.sdkLibraryProperties.Core_lib) {
1059 return path.Join("apistubs", "core", apiScope.name)
1060 } else {
1061 return path.Join("apistubs", "android", apiScope.name)
1062 }
1063}
1064
Paul Duffin12ceb462019-12-24 20:31:31 +00001065// Get the sdk version for use when compiling the stubs library.
Paul Duffin780c5f42020-05-12 15:52:55 +01001066func (module *SdkLibrary) sdkVersionForStubsLibrary(mctx android.EarlyModuleContext, apiScope *apiScope) string {
Paul Duffin87a05a32020-05-12 11:50:28 +01001067 scopeProperties := module.scopeToProperties[apiScope]
1068 if scopeProperties.Sdk_version != nil {
1069 return proptools.String(scopeProperties.Sdk_version)
1070 }
1071
Paul Duffin12ceb462019-12-24 20:31:31 +00001072 sdkDep := decodeSdkDep(mctx, sdkContext(&module.Library))
1073 if sdkDep.hasStandardLibs() {
1074 // If building against a standard sdk then use the sdk version appropriate for the scope.
Paul Duffind1b3a922020-01-22 11:57:20 +00001075 return apiScope.sdkVersion
Paul Duffin12ceb462019-12-24 20:31:31 +00001076 } else {
1077 // Otherwise, use no system module.
1078 return "none"
1079 }
1080}
1081
Paul Duffind1b3a922020-01-22 11:57:20 +00001082func (module *SdkLibrary) latestApiFilegroupName(apiScope *apiScope) string {
1083 return ":" + module.BaseModuleName() + ".api." + apiScope.name + ".latest"
Jiyong Park58c518b2018-05-12 22:29:12 +09001084}
Jiyong Parkc678ad32018-04-10 13:07:10 +09001085
Paul Duffind1b3a922020-01-22 11:57:20 +00001086func (module *SdkLibrary) latestRemovedApiFilegroupName(apiScope *apiScope) string {
1087 return ":" + module.BaseModuleName() + "-removed.api." + apiScope.name + ".latest"
Jiyong Parkc678ad32018-04-10 13:07:10 +09001088}
1089
Paul Duffin5df79302020-05-16 15:52:12 +01001090// Creates the implementation java library
1091func (module *SdkLibrary) createImplLibrary(mctx android.DefaultableHookContext) {
Paul Duffina2058f82020-06-24 16:22:38 +01001092
1093 moduleNamePtr := proptools.StringPtr(module.BaseModuleName())
1094
Paul Duffin5df79302020-05-16 15:52:12 +01001095 props := struct {
Paul Duffina2058f82020-06-24 16:22:38 +01001096 Name *string
1097 Visibility []string
1098 Instrument bool
1099 ConfigurationName *string
Paul Duffin5df79302020-05-16 15:52:12 +01001100 }{
1101 Name: proptools.StringPtr(module.implLibraryModuleName()),
1102 Visibility: module.sdkLibraryProperties.Impl_library_visibility,
Paul Duffin296cf332020-06-18 21:09:55 +01001103 // Set the instrument property to ensure it is instrumented when instrumentation is required.
1104 Instrument: true,
Paul Duffina2058f82020-06-24 16:22:38 +01001105
1106 // Make the created library behave as if it had the same name as this module.
1107 ConfigurationName: moduleNamePtr,
Paul Duffin5df79302020-05-16 15:52:12 +01001108 }
1109
1110 properties := []interface{}{
1111 &module.properties,
1112 &module.protoProperties,
1113 &module.deviceProperties,
Liz Kammera7a64f32020-07-09 15:16:41 -07001114 &module.dexProperties,
Paul Duffin5df79302020-05-16 15:52:12 +01001115 &module.dexpreoptProperties,
Colin Cross014489c2020-06-02 20:09:13 -07001116 &module.linter.properties,
Paul Duffin5df79302020-05-16 15:52:12 +01001117 &props,
1118 module.sdkComponentPropertiesForChildLibrary(),
1119 }
1120 mctx.CreateModule(LibraryFactory, properties...)
1121}
1122
Jiyong Parkc678ad32018-04-10 13:07:10 +09001123// Creates a static java library that has API stubs
Paul Duffinf0229202020-04-29 16:47:28 +01001124func (module *SdkLibrary) createStubsLibrary(mctx android.DefaultableHookContext, apiScope *apiScope) {
Jiyong Parkc678ad32018-04-10 13:07:10 +09001125 props := struct {
Dan Willemsen9f435972020-05-28 15:28:00 -07001126 Name *string
1127 Visibility []string
1128 Srcs []string
1129 Installable *bool
1130 Sdk_version *string
1131 System_modules *string
1132 Patch_module *string
1133 Libs []string
1134 Compile_dex *bool
1135 Java_version *string
1136 Openjdk9 struct {
Sundong Ahn054b19a2018-10-19 13:46:09 +09001137 Srcs []string
1138 Javacflags []string
1139 }
Anton Hansson5fd5d242020-03-27 19:43:19 +00001140 Dist struct {
1141 Targets []string
1142 Dest *string
1143 Dir *string
1144 Tag *string
1145 }
Jiyong Parkc678ad32018-04-10 13:07:10 +09001146 }{}
1147
Paul Duffinc3091c82020-05-08 14:16:20 +01001148 props.Name = proptools.StringPtr(module.stubsLibraryModuleName(apiScope))
Paul Duffin4911a892020-04-29 23:35:13 +01001149
1150 // If stubs_library_visibility is not set then the created module will use the
1151 // visibility of this module.
1152 visibility := module.sdkLibraryProperties.Stubs_library_visibility
1153 props.Visibility = visibility
1154
Jiyong Parkc678ad32018-04-10 13:07:10 +09001155 // sources are generated from the droiddoc
Paul Duffinc3091c82020-05-08 14:16:20 +01001156 props.Srcs = []string{":" + module.stubsSourceModuleName(apiScope)}
Paul Duffin12ceb462019-12-24 20:31:31 +00001157 sdkVersion := module.sdkVersionForStubsLibrary(mctx, apiScope)
Paul Duffin52d398a2019-06-11 12:31:14 +01001158 props.Sdk_version = proptools.StringPtr(sdkVersion)
Paul Duffina18abc22020-05-16 18:54:24 +01001159 props.System_modules = module.deviceProperties.System_modules
1160 props.Patch_module = module.properties.Patch_module
Paul Duffin367ab912019-12-23 19:40:36 +00001161 props.Installable = proptools.BoolPtr(false)
Sundong Ahn054b19a2018-10-19 13:46:09 +09001162 props.Libs = module.sdkLibraryProperties.Stub_only_libs
Paul Duffina18abc22020-05-16 18:54:24 +01001163 props.Openjdk9.Srcs = module.properties.Openjdk9.Srcs
1164 props.Openjdk9.Javacflags = module.properties.Openjdk9.Javacflags
Anton Hansson83509b52020-05-21 09:21:57 +01001165 // We compile the stubs for 1.8 in line with the main android.jar stubs, and potential
1166 // interop with older developer tools that don't support 1.9.
1167 props.Java_version = proptools.StringPtr("1.8")
Liz Kammera7a64f32020-07-09 15:16:41 -07001168 if module.dexProperties.Compile_dex != nil {
1169 props.Compile_dex = module.dexProperties.Compile_dex
Sundong Ahndd567f92018-07-31 17:19:11 +09001170 }
Jiyong Parkc678ad32018-04-10 13:07:10 +09001171
Anton Hansson5fd5d242020-03-27 19:43:19 +00001172 // Dist the class jar artifact for sdk builds.
1173 if !Bool(module.sdkLibraryProperties.No_dist) {
1174 props.Dist.Targets = []string{"sdk", "win_sdk"}
1175 props.Dist.Dest = proptools.StringPtr(fmt.Sprintf("%v.jar", module.BaseModuleName()))
1176 props.Dist.Dir = proptools.StringPtr(module.apiDistPath(apiScope))
1177 props.Dist.Tag = proptools.StringPtr(".jar")
1178 }
Jiyong Parkc678ad32018-04-10 13:07:10 +09001179
Paul Duffin859fe962020-05-15 10:20:31 +01001180 mctx.CreateModule(LibraryFactory, &props, module.sdkComponentPropertiesForChildLibrary())
Jiyong Parkc678ad32018-04-10 13:07:10 +09001181}
1182
Paul Duffin6d0886e2020-04-07 18:49:53 +01001183// Creates a droidstubs module that creates stubs source files from the given full source
Paul Duffinc8782502020-04-29 20:45:27 +01001184// files and also updates and checks the API specification files.
Paul Duffin15f34ef2020-07-20 18:04:44 +01001185func (module *SdkLibrary) createStubsSourcesAndApi(mctx android.DefaultableHookContext, apiScope *apiScope, name string, scopeSpecificDroidstubsArgs []string) {
Jiyong Parkc678ad32018-04-10 13:07:10 +09001186 props := struct {
Sundong Ahn054b19a2018-10-19 13:46:09 +09001187 Name *string
Paul Duffin4911a892020-04-29 23:35:13 +01001188 Visibility []string
Sundong Ahn054b19a2018-10-19 13:46:09 +09001189 Srcs []string
1190 Installable *bool
Paul Duffin52d398a2019-06-11 12:31:14 +01001191 Sdk_version *string
Paul Duffin12ceb462019-12-24 20:31:31 +00001192 System_modules *string
Sundong Ahn054b19a2018-10-19 13:46:09 +09001193 Libs []string
Paul Duffin11512472019-02-11 15:55:17 +00001194 Arg_files []string
Sundong Ahn054b19a2018-10-19 13:46:09 +09001195 Args *string
Sundong Ahn054b19a2018-10-19 13:46:09 +09001196 Java_version *string
1197 Merge_annotations_dirs []string
1198 Merge_inclusion_annotations_dirs []string
Paul Duffin0ff08bd2020-04-29 13:30:54 +01001199 Generate_stubs *bool
Sundong Ahn054b19a2018-10-19 13:46:09 +09001200 Check_api struct {
Inseob Kim38449af2019-02-28 14:24:05 +09001201 Current ApiToCheck
1202 Last_released ApiToCheck
1203 Ignore_missing_latest_api *bool
Paul Duffin160fe412020-05-10 19:32:20 +01001204
1205 Api_lint struct {
1206 Enabled *bool
1207 New_since *string
1208 Baseline_file *string
1209 }
Jiyong Park58c518b2018-05-12 22:29:12 +09001210 }
Sundong Ahn1b92c822018-05-29 11:35:17 +09001211 Aidl struct {
1212 Include_dirs []string
1213 Local_include_dirs []string
1214 }
Anton Hansson5fd5d242020-03-27 19:43:19 +00001215 Dist struct {
1216 Targets []string
1217 Dest *string
1218 Dir *string
1219 }
Jiyong Parkc678ad32018-04-10 13:07:10 +09001220 }{}
1221
Paul Duffin7b78b4d2020-04-28 14:08:32 +01001222 // The stubs source processing uses the same compile time classpath when extracting the
1223 // API from the implementation library as it does when compiling it. i.e. the same
1224 // * sdk version
1225 // * system_modules
1226 // * libs (static_libs/libs)
Paul Duffin250e6192019-06-07 10:44:37 +01001227
Paul Duffin0ff08bd2020-04-29 13:30:54 +01001228 props.Name = proptools.StringPtr(name)
Paul Duffin4911a892020-04-29 23:35:13 +01001229
1230 // If stubs_source_visibility is not set then the created module will use the
1231 // visibility of this module.
1232 visibility := module.sdkLibraryProperties.Stubs_source_visibility
1233 props.Visibility = visibility
1234
Paul Duffina18abc22020-05-16 18:54:24 +01001235 props.Srcs = append(props.Srcs, module.properties.Srcs...)
1236 props.Sdk_version = module.deviceProperties.Sdk_version
1237 props.System_modules = module.deviceProperties.System_modules
Jiyong Parkc678ad32018-04-10 13:07:10 +09001238 props.Installable = proptools.BoolPtr(false)
Sundong Ahne6f0b052018-06-05 16:46:14 +09001239 // A droiddoc module has only one Libs property and doesn't distinguish between
1240 // shared libs and static libs. So we need to add both of these libs to Libs property.
Paul Duffina18abc22020-05-16 18:54:24 +01001241 props.Libs = module.properties.Libs
1242 props.Libs = append(props.Libs, module.properties.Static_libs...)
1243 props.Aidl.Include_dirs = module.deviceProperties.Aidl.Include_dirs
1244 props.Aidl.Local_include_dirs = module.deviceProperties.Aidl.Local_include_dirs
1245 props.Java_version = module.properties.Java_version
Jiyong Parkc678ad32018-04-10 13:07:10 +09001246
Sundong Ahn054b19a2018-10-19 13:46:09 +09001247 props.Merge_annotations_dirs = module.sdkLibraryProperties.Merge_annotations_dirs
1248 props.Merge_inclusion_annotations_dirs = module.sdkLibraryProperties.Merge_inclusion_annotations_dirs
1249
Paul Duffin6d0886e2020-04-07 18:49:53 +01001250 droidstubsArgs := []string{}
Paul Duffin235ffff2019-12-24 10:41:30 +00001251 if len(module.sdkLibraryProperties.Api_packages) != 0 {
Paul Duffin6d0886e2020-04-07 18:49:53 +01001252 droidstubsArgs = append(droidstubsArgs, "--stub-packages "+strings.Join(module.sdkLibraryProperties.Api_packages, ":"))
Paul Duffin235ffff2019-12-24 10:41:30 +00001253 }
1254 if len(module.sdkLibraryProperties.Hidden_api_packages) != 0 {
Paul Duffin6d0886e2020-04-07 18:49:53 +01001255 droidstubsArgs = append(droidstubsArgs,
Paul Duffin235ffff2019-12-24 10:41:30 +00001256 android.JoinWithPrefix(module.sdkLibraryProperties.Hidden_api_packages, " --hide-package "))
1257 }
Paul Duffin6d0886e2020-04-07 18:49:53 +01001258 droidstubsArgs = append(droidstubsArgs, module.sdkLibraryProperties.Droiddoc_options...)
Paul Duffin235ffff2019-12-24 10:41:30 +00001259 disabledWarnings := []string{
1260 "MissingPermission",
1261 "BroadcastBehavior",
1262 "HiddenSuperclass",
1263 "DeprecationMismatch",
1264 "UnavailableSymbol",
1265 "SdkConstant",
1266 "HiddenTypeParameter",
1267 "Todo",
1268 "Typo",
1269 }
Paul Duffin6d0886e2020-04-07 18:49:53 +01001270 droidstubsArgs = append(droidstubsArgs, android.JoinWithPrefix(disabledWarnings, "--hide "))
Sundong Ahnfb2721f2018-09-17 13:23:09 +09001271
Paul Duffin1fb487d2020-04-07 18:50:10 +01001272 // Add in scope specific arguments.
Paul Duffin0ff08bd2020-04-29 13:30:54 +01001273 droidstubsArgs = append(droidstubsArgs, scopeSpecificDroidstubsArgs...)
Paul Duffin11512472019-02-11 15:55:17 +00001274 props.Arg_files = module.sdkLibraryProperties.Droiddoc_option_files
Paul Duffin6d0886e2020-04-07 18:49:53 +01001275 props.Args = proptools.StringPtr(strings.Join(droidstubsArgs, " "))
Jiyong Parkc678ad32018-04-10 13:07:10 +09001276
Paul Duffin15f34ef2020-07-20 18:04:44 +01001277 // List of APIs identified from the provided source files are created. They are later
1278 // compared against to the not-yet-released (a.k.a current) list of APIs and to the
1279 // last-released (a.k.a numbered) list of API.
1280 currentApiFileName := apiScope.apiFilePrefix + "current.txt"
1281 removedApiFileName := apiScope.apiFilePrefix + "removed.txt"
1282 apiDir := module.getApiDir()
1283 currentApiFileName = path.Join(apiDir, currentApiFileName)
1284 removedApiFileName = path.Join(apiDir, removedApiFileName)
Jiyong Parkc678ad32018-04-10 13:07:10 +09001285
Paul Duffin15f34ef2020-07-20 18:04:44 +01001286 // check against the not-yet-release API
1287 props.Check_api.Current.Api_file = proptools.StringPtr(currentApiFileName)
1288 props.Check_api.Current.Removed_api_file = proptools.StringPtr(removedApiFileName)
Jiyong Park58c518b2018-05-12 22:29:12 +09001289
Paul Duffin15f34ef2020-07-20 18:04:44 +01001290 if !apiScope.unstable {
1291 // check against the latest released API
1292 latestApiFilegroupName := proptools.StringPtr(module.latestApiFilegroupName(apiScope))
1293 props.Check_api.Last_released.Api_file = latestApiFilegroupName
1294 props.Check_api.Last_released.Removed_api_file = proptools.StringPtr(
1295 module.latestRemovedApiFilegroupName(apiScope))
1296 props.Check_api.Ignore_missing_latest_api = proptools.BoolPtr(true)
Paul Duffin160fe412020-05-10 19:32:20 +01001297
Paul Duffin15f34ef2020-07-20 18:04:44 +01001298 if proptools.Bool(module.sdkLibraryProperties.Api_lint.Enabled) {
1299 // Enable api lint.
1300 props.Check_api.Api_lint.Enabled = proptools.BoolPtr(true)
1301 props.Check_api.Api_lint.New_since = latestApiFilegroupName
Paul Duffin160fe412020-05-10 19:32:20 +01001302
Paul Duffin15f34ef2020-07-20 18:04:44 +01001303 // If it exists then pass a lint-baseline.txt through to droidstubs.
1304 baselinePath := path.Join(apiDir, apiScope.apiFilePrefix+"lint-baseline.txt")
1305 baselinePathRelativeToRoot := path.Join(mctx.ModuleDir(), baselinePath)
1306 paths, err := mctx.GlobWithDeps(baselinePathRelativeToRoot, nil)
1307 if err != nil {
1308 mctx.ModuleErrorf("error checking for presence of %s: %s", baselinePathRelativeToRoot, err)
1309 }
1310 if len(paths) == 1 {
1311 props.Check_api.Api_lint.Baseline_file = proptools.StringPtr(baselinePath)
1312 } else if len(paths) != 0 {
1313 mctx.ModuleErrorf("error checking for presence of %s: expected one path, found: %v", baselinePathRelativeToRoot, paths)
Paul Duffin160fe412020-05-10 19:32:20 +01001314 }
1315 }
Paul Duffin15f34ef2020-07-20 18:04:44 +01001316 }
Jiyong Park58c518b2018-05-12 22:29:12 +09001317
Paul Duffin15f34ef2020-07-20 18:04:44 +01001318 // Dist the api txt artifact for sdk builds.
1319 if !Bool(module.sdkLibraryProperties.No_dist) {
1320 props.Dist.Targets = []string{"sdk", "win_sdk"}
1321 props.Dist.Dest = proptools.StringPtr(fmt.Sprintf("%v.txt", module.BaseModuleName()))
1322 props.Dist.Dir = proptools.StringPtr(path.Join(module.apiDistPath(apiScope), "api"))
Anton Hansson5fd5d242020-03-27 19:43:19 +00001323 }
1324
Colin Cross84dfc3d2019-09-25 11:33:01 -07001325 mctx.CreateModule(DroidstubsFactory, &props)
Jiyong Parkc678ad32018-04-10 13:07:10 +09001326}
1327
Jooyung Han5e9013b2020-03-10 06:23:13 +09001328func (module *SdkLibrary) DepIsInSameApex(mctx android.BaseModuleContext, dep android.Module) bool {
1329 depTag := mctx.OtherModuleDependencyTag(dep)
1330 if depTag == xmlPermissionsFileTag {
1331 return true
1332 }
1333 return module.Library.DepIsInSameApex(mctx, dep)
1334}
1335
Jiyong Parkc678ad32018-04-10 13:07:10 +09001336// Creates the xml file that publicizes the runtime library
Paul Duffinf0229202020-04-29 16:47:28 +01001337func (module *SdkLibrary) createXmlFile(mctx android.DefaultableHookContext) {
Jiyong Parke3833882020-02-17 17:28:10 +09001338 props := struct {
Paul Duffin1dbe3ca2020-05-16 09:57:59 +01001339 Name *string
1340 Lib_name *string
1341 Apex_available []string
Jiyong Parke3833882020-02-17 17:28:10 +09001342 }{
Paul Duffineedc5d52020-06-12 17:46:39 +01001343 Name: proptools.StringPtr(module.xmlPermissionsModuleName()),
Jooyung Han5e9013b2020-03-10 06:23:13 +09001344 Lib_name: proptools.StringPtr(module.BaseModuleName()),
1345 Apex_available: module.ApexProperties.Apex_available,
Jiyong Parkc678ad32018-04-10 13:07:10 +09001346 }
Jiyong Parke3833882020-02-17 17:28:10 +09001347
Jiyong Parke3833882020-02-17 17:28:10 +09001348 mctx.CreateModule(sdkLibraryXmlFactory, &props)
Jiyong Parkc678ad32018-04-10 13:07:10 +09001349}
1350
Paul Duffin50061512020-01-21 16:31:05 +00001351func PrebuiltJars(ctx android.BaseModuleContext, baseName string, s sdkSpec) android.Paths {
Jiyong Park6a927c42020-01-21 02:03:43 +09001352 var ver sdkVersion
1353 var kind sdkKind
1354 if s.usePrebuilt(ctx) {
1355 ver = s.version
1356 kind = s.kind
Jiyong Parkc678ad32018-04-10 13:07:10 +09001357 } else {
Jiyong Park6a927c42020-01-21 02:03:43 +09001358 // We don't have prebuilt SDK for the specific sdkVersion.
1359 // Instead of breaking the build, fallback to use "system_current"
1360 ver = sdkVersionCurrent
1361 kind = sdkSystem
Sundong Ahn054b19a2018-10-19 13:46:09 +09001362 }
Jiyong Park6a927c42020-01-21 02:03:43 +09001363
1364 dir := filepath.Join("prebuilts", "sdk", ver.String(), kind.String())
Paul Duffin50061512020-01-21 16:31:05 +00001365 jar := filepath.Join(dir, baseName+".jar")
Sundong Ahn054b19a2018-10-19 13:46:09 +09001366 jarPath := android.ExistentPathForSource(ctx, jar)
Sundong Ahnae418ac2019-02-28 15:01:28 +09001367 if !jarPath.Valid() {
Colin Cross07c88562020-01-07 09:34:44 -08001368 if ctx.Config().AllowMissingDependencies() {
1369 return android.Paths{android.PathForSource(ctx, jar)}
1370 } else {
Jiyong Park6a927c42020-01-21 02:03:43 +09001371 ctx.PropertyErrorf("sdk_library", "invalid sdk version %q, %q does not exist", s.raw, jar)
Colin Cross07c88562020-01-07 09:34:44 -08001372 }
Sundong Ahnae418ac2019-02-28 15:01:28 +09001373 return nil
1374 }
Sundong Ahn054b19a2018-10-19 13:46:09 +09001375 return android.Paths{jarPath.Path()}
1376}
1377
Pete Bentley0c7b26e2020-08-18 13:44:59 +00001378// Get the apex name for module, "" if it is for platform.
1379func getApexNameForModule(module android.Module) string {
Paul Duffin9b879592020-05-26 13:21:35 +01001380 if apex, ok := module.(android.ApexModule); ok {
Pete Bentley0c7b26e2020-08-18 13:44:59 +00001381 return apex.ApexVariationName()
Paul Duffin9b879592020-05-26 13:21:35 +01001382 }
1383
Pete Bentley0c7b26e2020-08-18 13:44:59 +00001384 return ""
Paul Duffin9b879592020-05-26 13:21:35 +01001385}
1386
Pete Bentley0c7b26e2020-08-18 13:44:59 +00001387// Check to see if the other module is within the same named APEX as this module.
Paul Duffin9b879592020-05-26 13:21:35 +01001388//
1389// If either this or the other module are on the platform then this will return
1390// false.
Pete Bentley0c7b26e2020-08-18 13:44:59 +00001391func withinSameApexAs(module android.ApexModule, other android.Module) bool {
1392 name := module.ApexVariationName()
1393 return name != "" && getApexNameForModule(other) == name
Paul Duffin9b879592020-05-26 13:21:35 +01001394}
1395
Paul Duffinb05d4292020-05-20 12:19:10 +01001396func (module *SdkLibrary) sdkJars(ctx android.BaseModuleContext, sdkVersion sdkSpec, headerJars bool) android.Paths {
Jiyong Park932cdfe2020-05-28 00:19:53 +09001397 // If the client doesn't set sdk_version, but if this library prefers stubs over
1398 // the impl library, let's provide the widest API surface possible. To do so,
1399 // force override sdk_version to module_current so that the closest possible API
1400 // surface could be found in selectHeaderJarsForSdkVersion
1401 if module.defaultsToStubs() && !sdkVersion.specified() {
1402 sdkVersion = sdkSpecFrom("module_current")
1403 }
Paul Duffind1b3a922020-01-22 11:57:20 +00001404
Paul Duffindaaa3322020-05-26 18:13:57 +01001405 // Only provide access to the implementation library if it is actually built.
1406 if module.requiresRuntimeImplementationLibrary() {
1407 // Check any special cases for java_sdk_library.
1408 //
1409 // Only allow access to the implementation library in the following condition:
1410 // * No sdk_version specified on the referencing module.
Paul Duffin9b879592020-05-26 13:21:35 +01001411 // * The referencing module is in the same apex as this.
Pete Bentley0c7b26e2020-08-18 13:44:59 +00001412 if sdkVersion.kind == sdkPrivate || withinSameApexAs(module, ctx.Module()) {
Paul Duffindaaa3322020-05-26 18:13:57 +01001413 if headerJars {
1414 return module.HeaderJars()
1415 } else {
1416 return module.ImplementationJars()
1417 }
Sundong Ahn054b19a2018-10-19 13:46:09 +09001418 }
Jiyong Parkc678ad32018-04-10 13:07:10 +09001419 }
Paul Duffinb05d4292020-05-20 12:19:10 +01001420
Paul Duffin23970f42020-05-20 14:20:02 +01001421 return module.selectHeaderJarsForSdkVersion(ctx, sdkVersion)
Jiyong Parkc678ad32018-04-10 13:07:10 +09001422}
1423
Sundong Ahn241cd372018-07-13 16:16:44 +09001424// to satisfy SdkLibraryDependency interface
Paul Duffind1b3a922020-01-22 11:57:20 +00001425func (module *SdkLibrary) SdkHeaderJars(ctx android.BaseModuleContext, sdkVersion sdkSpec) android.Paths {
1426 return module.sdkJars(ctx, sdkVersion, true /*headerJars*/)
1427}
1428
1429// to satisfy SdkLibraryDependency interface
Jiyong Park6a927c42020-01-21 02:03:43 +09001430func (module *SdkLibrary) SdkImplementationJars(ctx android.BaseModuleContext, sdkVersion sdkSpec) android.Paths {
Paul Duffind1b3a922020-01-22 11:57:20 +00001431 return module.sdkJars(ctx, sdkVersion, false /*headerJars*/)
Sundong Ahn241cd372018-07-13 16:16:44 +09001432}
1433
Sundong Ahn80a87b32019-05-13 15:02:50 +09001434func (module *SdkLibrary) SetNoDist() {
1435 module.sdkLibraryProperties.No_dist = proptools.BoolPtr(true)
1436}
1437
Colin Cross571cccf2019-02-04 11:22:08 -08001438var javaSdkLibrariesKey = android.NewOnceKey("javaSdkLibraries")
1439
Jiyong Park82484c02018-04-23 21:41:26 +09001440func javaSdkLibraries(config android.Config) *[]string {
Colin Cross571cccf2019-02-04 11:22:08 -08001441 return config.Once(javaSdkLibrariesKey, func() interface{} {
Jiyong Park82484c02018-04-23 21:41:26 +09001442 return &[]string{}
1443 }).(*[]string)
1444}
1445
Paul Duffin749f98f2019-12-30 17:23:46 +00001446func (module *SdkLibrary) getApiDir() string {
1447 return proptools.StringDefault(module.sdkLibraryProperties.Api_dir, "api")
1448}
1449
Jiyong Parkc678ad32018-04-10 13:07:10 +09001450// For a java_sdk_library module, create internal modules for stubs, docs,
1451// runtime libs and xml file. If requested, the stubs and docs are created twice
1452// once for public API level and once for system API level
Paul Duffinf0229202020-04-29 16:47:28 +01001453func (module *SdkLibrary) CreateInternalModules(mctx android.DefaultableHookContext) {
1454 // If the module has been disabled then don't create any child modules.
1455 if !module.Enabled() {
1456 return
1457 }
1458
Paul Duffina18abc22020-05-16 18:54:24 +01001459 if len(module.properties.Srcs) == 0 {
Inseob Kimc0907f12019-02-08 21:00:45 +09001460 mctx.PropertyErrorf("srcs", "java_sdk_library must specify srcs")
Jooyung Han58f26ab2019-12-18 15:34:32 +09001461 return
Inseob Kimc0907f12019-02-08 21:00:45 +09001462 }
1463
Paul Duffin37e0b772019-12-30 17:20:10 +00001464 // If this builds against standard libraries (i.e. is not part of the core libraries)
1465 // then assume it provides both system and test apis. Otherwise, assume it does not and
1466 // also assume it does not contribute to the dist build.
1467 sdkDep := decodeSdkDep(mctx, sdkContext(&module.Library))
1468 hasSystemAndTestApis := sdkDep.hasStandardLibs()
Paul Duffin3375e352020-04-28 10:44:03 +01001469 module.sdkLibraryProperties.Generate_system_and_test_apis = hasSystemAndTestApis
Paul Duffin37e0b772019-12-30 17:20:10 +00001470 module.sdkLibraryProperties.No_dist = proptools.BoolPtr(!hasSystemAndTestApis)
1471
Inseob Kim8098faa2019-03-18 10:19:51 +09001472 missing_current_api := false
1473
Paul Duffin3375e352020-04-28 10:44:03 +01001474 generatedScopes := module.getGeneratedApiScopes(mctx)
Paul Duffind1b3a922020-01-22 11:57:20 +00001475
Paul Duffin749f98f2019-12-30 17:23:46 +00001476 apiDir := module.getApiDir()
Paul Duffin3375e352020-04-28 10:44:03 +01001477 for _, scope := range generatedScopes {
Inseob Kim8098faa2019-03-18 10:19:51 +09001478 for _, api := range []string{"current.txt", "removed.txt"} {
Paul Duffind1b3a922020-01-22 11:57:20 +00001479 path := path.Join(mctx.ModuleDir(), apiDir, scope.apiFilePrefix+api)
Inseob Kim8098faa2019-03-18 10:19:51 +09001480 p := android.ExistentPathForSource(mctx, path)
1481 if !p.Valid() {
1482 mctx.ModuleErrorf("Current api file %#v doesn't exist", path)
1483 missing_current_api = true
1484 }
1485 }
1486 }
1487
1488 if missing_current_api {
1489 script := "build/soong/scripts/gen-java-current-api-files.sh"
1490 p := android.ExistentPathForSource(mctx, script)
1491
1492 if !p.Valid() {
1493 panic(fmt.Sprintf("script file %s doesn't exist", script))
1494 }
1495
1496 mctx.ModuleErrorf("One or more current api files are missing. "+
1497 "You can update them by:\n"+
Paul Duffin37e0b772019-12-30 17:20:10 +00001498 "%s %q %s && m update-api",
Paul Duffind1b3a922020-01-22 11:57:20 +00001499 script, filepath.Join(mctx.ModuleDir(), apiDir),
Paul Duffin3375e352020-04-28 10:44:03 +01001500 strings.Join(generatedScopes.Strings(func(s *apiScope) string { return s.apiFilePrefix }), " "))
Inseob Kim8098faa2019-03-18 10:19:51 +09001501 return
1502 }
1503
Paul Duffin3375e352020-04-28 10:44:03 +01001504 for _, scope := range generatedScopes {
Paul Duffin15f34ef2020-07-20 18:04:44 +01001505 // Use the stubs source name for legacy reasons.
1506 module.createStubsSourcesAndApi(mctx, scope, module.stubsSourceModuleName(scope), scope.droidstubsArgs)
Paul Duffin0ff08bd2020-04-29 13:30:54 +01001507
Paul Duffind1b3a922020-01-22 11:57:20 +00001508 module.createStubsLibrary(mctx, scope)
Inseob Kimc0907f12019-02-08 21:00:45 +09001509 }
1510
Paul Duffindfa131e2020-05-15 20:37:11 +01001511 if module.requiresRuntimeImplementationLibrary() {
Paul Duffin5df79302020-05-16 15:52:12 +01001512 // Create child module to create an implementation library.
1513 //
1514 // This temporarily creates a second implementation library that can be explicitly
1515 // referenced.
1516 //
1517 // TODO(b/156618935) - update comment once only one implementation library is created.
1518 module.createImplLibrary(mctx)
1519
Paul Duffindfa131e2020-05-15 20:37:11 +01001520 // Only create an XML permissions file that declares the library as being usable
1521 // as a shared library if required.
1522 if module.sharedLibrary() {
1523 module.createXmlFile(mctx)
1524 }
Paul Duffin43db9be2019-12-30 17:35:49 +00001525
1526 // record java_sdk_library modules so that they are exported to make
1527 javaSdkLibraries := javaSdkLibraries(mctx.Config())
1528 javaSdkLibrariesLock.Lock()
1529 defer javaSdkLibrariesLock.Unlock()
1530 *javaSdkLibraries = append(*javaSdkLibraries, module.BaseModuleName())
1531 }
Inseob Kimc0907f12019-02-08 21:00:45 +09001532}
1533
1534func (module *SdkLibrary) InitSdkLibraryProperties() {
Colin Crossce6734e2020-06-15 16:09:53 -07001535 module.addHostAndDeviceProperties()
1536 module.AddProperties(&module.sdkLibraryProperties)
Sundong Ahn054b19a2018-10-19 13:46:09 +09001537
Paul Duffin859fe962020-05-15 10:20:31 +01001538 module.initSdkLibraryComponent(&module.ModuleBase)
1539
Paul Duffina18abc22020-05-16 18:54:24 +01001540 module.properties.Installable = proptools.BoolPtr(true)
1541 module.deviceProperties.IsSDKLibrary = true
Inseob Kimc0907f12019-02-08 21:00:45 +09001542}
Sundong Ahn054b19a2018-10-19 13:46:09 +09001543
Paul Duffindfa131e2020-05-15 20:37:11 +01001544func (module *SdkLibrary) requiresRuntimeImplementationLibrary() bool {
1545 return !proptools.Bool(module.sdkLibraryProperties.Api_only)
1546}
1547
Jiyong Park932cdfe2020-05-28 00:19:53 +09001548func (module *SdkLibrary) defaultsToStubs() bool {
1549 return proptools.Bool(module.sdkLibraryProperties.Default_to_stubs)
1550}
1551
Paul Duffin1b1e8062020-05-08 13:44:43 +01001552// Defines how to name the individual component modules the sdk library creates.
1553type sdkLibraryComponentNamingScheme interface {
1554 stubsLibraryModuleName(scope *apiScope, baseName string) string
1555
1556 stubsSourceModuleName(scope *apiScope, baseName string) string
1557
1558 apiModuleName(scope *apiScope, baseName string) string
1559}
1560
1561type defaultNamingScheme struct {
1562}
1563
1564func (s *defaultNamingScheme) stubsLibraryModuleName(scope *apiScope, baseName string) string {
1565 return scope.stubsLibraryModuleName(baseName)
1566}
1567
1568func (s *defaultNamingScheme) stubsSourceModuleName(scope *apiScope, baseName string) string {
1569 return scope.stubsSourceModuleName(baseName)
1570}
1571
1572func (s *defaultNamingScheme) apiModuleName(scope *apiScope, baseName string) string {
1573 return scope.apiModuleName(baseName)
1574}
1575
1576var _ sdkLibraryComponentNamingScheme = (*defaultNamingScheme)(nil)
1577
Paul Duffin6c9c5fc2020-05-08 15:36:30 +01001578type frameworkModulesNamingScheme struct {
1579}
1580
1581func (s *frameworkModulesNamingScheme) moduleSuffix(scope *apiScope) string {
1582 suffix := scope.name
1583 if scope == apiScopeModuleLib {
1584 suffix = "module_libs_"
1585 }
1586 return suffix
1587}
1588
1589func (s *frameworkModulesNamingScheme) stubsLibraryModuleName(scope *apiScope, baseName string) string {
1590 return fmt.Sprintf("%s-stubs-%sapi", baseName, s.moduleSuffix(scope))
1591}
1592
1593func (s *frameworkModulesNamingScheme) stubsSourceModuleName(scope *apiScope, baseName string) string {
1594 return fmt.Sprintf("%s-stubs-srcs-%sapi", baseName, s.moduleSuffix(scope))
1595}
1596
1597func (s *frameworkModulesNamingScheme) apiModuleName(scope *apiScope, baseName string) string {
1598 return fmt.Sprintf("%s-api-%sapi", baseName, s.moduleSuffix(scope))
1599}
1600
1601var _ sdkLibraryComponentNamingScheme = (*frameworkModulesNamingScheme)(nil)
1602
Anton Hansson2d0c1942020-05-25 12:20:51 +01001603func moduleStubLinkType(name string) (stub bool, ret linkType) {
1604 // This suffix-based approach is fragile and could potentially mis-trigger.
1605 // TODO(b/155164730): Clean this up when modules no longer reference sdk_lib stubs directly.
1606 if strings.HasSuffix(name, ".stubs.public") || strings.HasSuffix(name, "-stubs-publicapi") {
1607 return true, javaSdk
1608 }
1609 if strings.HasSuffix(name, ".stubs.system") || strings.HasSuffix(name, "-stubs-systemapi") {
1610 return true, javaSystem
1611 }
1612 if strings.HasSuffix(name, ".stubs.module_lib") || strings.HasSuffix(name, "-stubs-module_libs_api") {
1613 return true, javaModule
1614 }
1615 if strings.HasSuffix(name, ".stubs.test") {
1616 return true, javaSystem
1617 }
1618 return false, javaPlatform
1619}
1620
Jaewoong Jung4f158ee2019-07-11 10:05:35 -07001621// java_sdk_library is a special Java library that provides optional platform APIs to apps.
1622// In practice, it can be viewed as a combination of several modules: 1) stubs library that clients
1623// are linked against to, 2) droiddoc module that internally generates API stubs source files,
1624// 3) the real runtime shared library that implements the APIs, and 4) XML file for adding
1625// the runtime lib to the classpath at runtime if requested via <uses-library>.
Inseob Kimc0907f12019-02-08 21:00:45 +09001626func SdkLibraryFactory() android.Module {
1627 module := &SdkLibrary{}
Paul Duffinc3091c82020-05-08 14:16:20 +01001628
1629 // Initialize information common between source and prebuilt.
1630 module.initCommon(&module.ModuleBase)
1631
Inseob Kimc0907f12019-02-08 21:00:45 +09001632 module.InitSdkLibraryProperties()
Jooyung Han58f26ab2019-12-18 15:34:32 +09001633 android.InitApexModule(module)
Sundong Ahn054b19a2018-10-19 13:46:09 +09001634 InitJavaModule(module, android.HostAndDeviceSupported)
Paul Duffin3375e352020-04-28 10:44:03 +01001635
1636 // Initialize the map from scope to scope specific properties.
1637 scopeToProperties := make(map[*apiScope]*ApiScopeProperties)
1638 for _, scope := range allApiScopes {
1639 scopeToProperties[scope] = scope.scopeSpecificProperties(module)
1640 }
1641 module.scopeToProperties = scopeToProperties
1642
Paul Duffin4911a892020-04-29 23:35:13 +01001643 // Add the properties containing visibility rules so that they are checked.
Paul Duffin5df79302020-05-16 15:52:12 +01001644 android.AddVisibilityProperty(module, "impl_library_visibility", &module.sdkLibraryProperties.Impl_library_visibility)
Paul Duffin4911a892020-04-29 23:35:13 +01001645 android.AddVisibilityProperty(module, "stubs_library_visibility", &module.sdkLibraryProperties.Stubs_library_visibility)
1646 android.AddVisibilityProperty(module, "stubs_source_visibility", &module.sdkLibraryProperties.Stubs_source_visibility)
1647
Paul Duffin1b1e8062020-05-08 13:44:43 +01001648 module.SetDefaultableHook(func(ctx android.DefaultableHookContext) {
Paul Duffindfa131e2020-05-15 20:37:11 +01001649 // If no implementation is required then it cannot be used as a shared library
1650 // either.
1651 if !module.requiresRuntimeImplementationLibrary() {
1652 // If shared_library has been explicitly set to true then it is incompatible
1653 // with api_only: true.
1654 if proptools.Bool(module.commonSdkLibraryProperties.Shared_library) {
1655 ctx.PropertyErrorf("api_only/shared_library", "inconsistent settings, shared_library and api_only cannot both be true")
1656 }
1657 // Set shared_library: false.
1658 module.commonSdkLibraryProperties.Shared_library = proptools.BoolPtr(false)
1659 }
1660
Paul Duffin1b1e8062020-05-08 13:44:43 +01001661 if module.initCommonAfterDefaultsApplied(ctx) {
1662 module.CreateInternalModules(ctx)
1663 }
1664 })
Jiyong Parkc678ad32018-04-10 13:07:10 +09001665 return module
1666}
Colin Cross79c7c262019-04-17 11:11:46 -07001667
1668//
1669// SDK library prebuilts
1670//
1671
Paul Duffin56d44902020-01-31 13:36:25 +00001672// Properties associated with each api scope.
1673type sdkLibraryScopeProperties struct {
Colin Cross79c7c262019-04-17 11:11:46 -07001674 Jars []string `android:"path"`
1675
1676 Sdk_version *string
1677
Colin Cross79c7c262019-04-17 11:11:46 -07001678 // List of shared java libs that this module has dependencies to
1679 Libs []string
Paul Duffin3d1248c2020-04-09 00:10:17 +01001680
Paul Duffinc8782502020-04-29 20:45:27 +01001681 // The stubs source.
Paul Duffin3d1248c2020-04-09 00:10:17 +01001682 Stub_srcs []string `android:"path"`
Paul Duffin1fd005d2020-04-09 01:08:11 +01001683
1684 // The current.txt
Paul Duffin0f8faff2020-05-20 16:18:00 +01001685 Current_api *string `android:"path"`
Paul Duffin1fd005d2020-04-09 01:08:11 +01001686
1687 // The removed.txt
Paul Duffin0f8faff2020-05-20 16:18:00 +01001688 Removed_api *string `android:"path"`
Colin Cross79c7c262019-04-17 11:11:46 -07001689}
1690
Paul Duffin56d44902020-01-31 13:36:25 +00001691type sdkLibraryImportProperties struct {
Paul Duffinfcfd7912020-01-31 17:54:30 +00001692 // List of shared java libs, common to all scopes, that this module has
1693 // dependencies to
1694 Libs []string
Paul Duffin56d44902020-01-31 13:36:25 +00001695}
1696
Paul Duffineedc5d52020-06-12 17:46:39 +01001697type SdkLibraryImport struct {
Colin Cross79c7c262019-04-17 11:11:46 -07001698 android.ModuleBase
1699 android.DefaultableModuleBase
1700 prebuilt android.Prebuilt
Paul Duffindd46f712020-02-10 13:37:10 +00001701 android.ApexModuleBase
1702 android.SdkBase
Colin Cross79c7c262019-04-17 11:11:46 -07001703
1704 properties sdkLibraryImportProperties
1705
Paul Duffin46a26a82020-04-07 19:27:04 +01001706 // Map from api scope to the scope specific property structure.
1707 scopeProperties map[*apiScope]*sdkLibraryScopeProperties
1708
Paul Duffin56d44902020-01-31 13:36:25 +00001709 commonToSdkLibraryAndImport
Paul Duffineedc5d52020-06-12 17:46:39 +01001710
1711 // The reference to the implementation library created by the source module.
1712 // Is nil if the source module does not exist.
1713 implLibraryModule *Library
1714
1715 // The reference to the xml permissions module created by the source module.
1716 // Is nil if the source module does not exist.
1717 xmlPermissionsFileModule *sdkLibraryXml
Colin Cross79c7c262019-04-17 11:11:46 -07001718}
1719
Paul Duffineedc5d52020-06-12 17:46:39 +01001720var _ SdkLibraryDependency = (*SdkLibraryImport)(nil)
Colin Cross79c7c262019-04-17 11:11:46 -07001721
Paul Duffin46a26a82020-04-07 19:27:04 +01001722// The type of a structure that contains a field of type sdkLibraryScopeProperties
1723// for each apiscope in allApiScopes, e.g. something like:
1724// struct {
1725// Public sdkLibraryScopeProperties
1726// System sdkLibraryScopeProperties
1727// ...
1728// }
1729var allScopeStructType = createAllScopePropertiesStructType()
1730
1731// Dynamically create a structure type for each apiscope in allApiScopes.
1732func createAllScopePropertiesStructType() reflect.Type {
1733 var fields []reflect.StructField
1734 for _, apiScope := range allApiScopes {
1735 field := reflect.StructField{
1736 Name: apiScope.fieldName,
1737 Type: reflect.TypeOf(sdkLibraryScopeProperties{}),
1738 }
1739 fields = append(fields, field)
1740 }
1741
1742 return reflect.StructOf(fields)
1743}
1744
1745// Create an instance of the scope specific structure type and return a map
1746// from apiscope to a pointer to each scope specific field.
1747func createPropertiesInstance() (interface{}, map[*apiScope]*sdkLibraryScopeProperties) {
1748 allScopePropertiesPtr := reflect.New(allScopeStructType)
1749 allScopePropertiesStruct := allScopePropertiesPtr.Elem()
1750 scopeProperties := make(map[*apiScope]*sdkLibraryScopeProperties)
1751
1752 for _, apiScope := range allApiScopes {
1753 field := allScopePropertiesStruct.FieldByName(apiScope.fieldName)
1754 scopeProperties[apiScope] = field.Addr().Interface().(*sdkLibraryScopeProperties)
1755 }
1756
1757 return allScopePropertiesPtr.Interface(), scopeProperties
1758}
1759
Jaewoong Jung4f158ee2019-07-11 10:05:35 -07001760// java_sdk_library_import imports a prebuilt java_sdk_library.
Colin Cross79c7c262019-04-17 11:11:46 -07001761func sdkLibraryImportFactory() android.Module {
Paul Duffineedc5d52020-06-12 17:46:39 +01001762 module := &SdkLibraryImport{}
Colin Cross79c7c262019-04-17 11:11:46 -07001763
Paul Duffin46a26a82020-04-07 19:27:04 +01001764 allScopeProperties, scopeToProperties := createPropertiesInstance()
1765 module.scopeProperties = scopeToProperties
1766 module.AddProperties(&module.properties, allScopeProperties)
Colin Cross79c7c262019-04-17 11:11:46 -07001767
Paul Duffinc3091c82020-05-08 14:16:20 +01001768 // Initialize information common between source and prebuilt.
1769 module.initCommon(&module.ModuleBase)
1770
Paul Duffin0bdcb272020-02-06 15:24:57 +00001771 android.InitPrebuiltModule(module, &[]string{""})
Paul Duffindd46f712020-02-10 13:37:10 +00001772 android.InitApexModule(module)
1773 android.InitSdkAwareModule(module)
Colin Cross79c7c262019-04-17 11:11:46 -07001774 InitJavaModule(module, android.HostAndDeviceSupported)
1775
Paul Duffin1b1e8062020-05-08 13:44:43 +01001776 module.SetDefaultableHook(func(mctx android.DefaultableHookContext) {
1777 if module.initCommonAfterDefaultsApplied(mctx) {
1778 module.createInternalModules(mctx)
1779 }
1780 })
Colin Cross79c7c262019-04-17 11:11:46 -07001781 return module
1782}
1783
Paul Duffineedc5d52020-06-12 17:46:39 +01001784func (module *SdkLibraryImport) Prebuilt() *android.Prebuilt {
Colin Cross79c7c262019-04-17 11:11:46 -07001785 return &module.prebuilt
1786}
1787
Paul Duffineedc5d52020-06-12 17:46:39 +01001788func (module *SdkLibraryImport) Name() string {
Colin Cross79c7c262019-04-17 11:11:46 -07001789 return module.prebuilt.Name(module.ModuleBase.Name())
1790}
1791
Paul Duffineedc5d52020-06-12 17:46:39 +01001792func (module *SdkLibraryImport) createInternalModules(mctx android.DefaultableHookContext) {
Colin Cross79c7c262019-04-17 11:11:46 -07001793
Paul Duffin50061512020-01-21 16:31:05 +00001794 // If the build is configured to use prebuilts then force this to be preferred.
Jeongik Cha816a23a2020-07-08 01:09:23 +09001795 if mctx.Config().AlwaysUsePrebuiltSdks() {
Paul Duffin50061512020-01-21 16:31:05 +00001796 module.prebuilt.ForcePrefer()
1797 }
1798
Paul Duffin46a26a82020-04-07 19:27:04 +01001799 for apiScope, scopeProperties := range module.scopeProperties {
Paul Duffin56d44902020-01-31 13:36:25 +00001800 if len(scopeProperties.Jars) == 0 {
1801 continue
1802 }
1803
Paul Duffinbbb546b2020-04-09 00:07:11 +01001804 module.createJavaImportForStubs(mctx, apiScope, scopeProperties)
Paul Duffin3d1248c2020-04-09 00:10:17 +01001805
Paul Duffin0f8faff2020-05-20 16:18:00 +01001806 if len(scopeProperties.Stub_srcs) > 0 {
1807 module.createPrebuiltStubsSources(mctx, apiScope, scopeProperties)
1808 }
Paul Duffin56d44902020-01-31 13:36:25 +00001809 }
Colin Cross79c7c262019-04-17 11:11:46 -07001810
1811 javaSdkLibraries := javaSdkLibraries(mctx.Config())
1812 javaSdkLibrariesLock.Lock()
1813 defer javaSdkLibrariesLock.Unlock()
1814 *javaSdkLibraries = append(*javaSdkLibraries, module.BaseModuleName())
1815}
1816
Paul Duffineedc5d52020-06-12 17:46:39 +01001817func (module *SdkLibraryImport) createJavaImportForStubs(mctx android.DefaultableHookContext, apiScope *apiScope, scopeProperties *sdkLibraryScopeProperties) {
Paul Duffinbbb546b2020-04-09 00:07:11 +01001818 // Creates a java import for the jar with ".stubs" suffix
1819 props := struct {
Paul Duffin1dbe3ca2020-05-16 09:57:59 +01001820 Name *string
1821 Sdk_version *string
1822 Libs []string
1823 Jars []string
1824 Prefer *bool
Paul Duffinbbb546b2020-04-09 00:07:11 +01001825 }{}
Paul Duffinc3091c82020-05-08 14:16:20 +01001826 props.Name = proptools.StringPtr(module.stubsLibraryModuleName(apiScope))
Paul Duffinbbb546b2020-04-09 00:07:11 +01001827 props.Sdk_version = scopeProperties.Sdk_version
1828 // Prepend any of the libs from the legacy public properties to the libs for each of the
1829 // scopes to avoid having to duplicate them in each scope.
1830 props.Libs = append(module.properties.Libs, scopeProperties.Libs...)
1831 props.Jars = scopeProperties.Jars
Paul Duffin1dbe3ca2020-05-16 09:57:59 +01001832
Paul Duffin38b57852020-05-13 16:08:09 +01001833 // The imports are preferred if the java_sdk_library_import is preferred.
1834 props.Prefer = proptools.BoolPtr(module.prebuilt.Prefer())
Paul Duffin859fe962020-05-15 10:20:31 +01001835
1836 mctx.CreateModule(ImportFactory, &props, module.sdkComponentPropertiesForChildLibrary())
Paul Duffinbbb546b2020-04-09 00:07:11 +01001837}
1838
Paul Duffineedc5d52020-06-12 17:46:39 +01001839func (module *SdkLibraryImport) createPrebuiltStubsSources(mctx android.DefaultableHookContext, apiScope *apiScope, scopeProperties *sdkLibraryScopeProperties) {
Paul Duffin3d1248c2020-04-09 00:10:17 +01001840 props := struct {
Paul Duffin38b57852020-05-13 16:08:09 +01001841 Name *string
1842 Srcs []string
1843 Prefer *bool
Paul Duffin3d1248c2020-04-09 00:10:17 +01001844 }{}
Paul Duffinc3091c82020-05-08 14:16:20 +01001845 props.Name = proptools.StringPtr(module.stubsSourceModuleName(apiScope))
Paul Duffin3d1248c2020-04-09 00:10:17 +01001846 props.Srcs = scopeProperties.Stub_srcs
1847 mctx.CreateModule(PrebuiltStubsSourcesFactory, &props)
Paul Duffin38b57852020-05-13 16:08:09 +01001848
1849 // The stubs source is preferred if the java_sdk_library_import is preferred.
1850 props.Prefer = proptools.BoolPtr(module.prebuilt.Prefer())
Paul Duffin3d1248c2020-04-09 00:10:17 +01001851}
1852
Paul Duffin44f1d842020-06-26 20:17:02 +01001853// Add the dependencies on the child module in the component deps mutator so that it
1854// creates references to the prebuilt and not the source modules.
1855func (module *SdkLibraryImport) ComponentDepsMutator(ctx android.BottomUpMutatorContext) {
Paul Duffin46a26a82020-04-07 19:27:04 +01001856 for apiScope, scopeProperties := range module.scopeProperties {
Paul Duffin56d44902020-01-31 13:36:25 +00001857 if len(scopeProperties.Jars) == 0 {
1858 continue
1859 }
1860
1861 // Add dependencies to the prebuilt stubs library
Paul Duffin44f1d842020-06-26 20:17:02 +01001862 ctx.AddVariationDependencies(nil, apiScope.stubsTag, "prebuilt_"+module.stubsLibraryModuleName(apiScope))
Paul Duffin0f8faff2020-05-20 16:18:00 +01001863
1864 if len(scopeProperties.Stub_srcs) > 0 {
1865 // Add dependencies to the prebuilt stubs source library
Paul Duffin44f1d842020-06-26 20:17:02 +01001866 ctx.AddVariationDependencies(nil, apiScope.stubsSourceTag, "prebuilt_"+module.stubsSourceModuleName(apiScope))
Paul Duffin0f8faff2020-05-20 16:18:00 +01001867 }
Paul Duffin56d44902020-01-31 13:36:25 +00001868 }
Paul Duffin44f1d842020-06-26 20:17:02 +01001869}
1870
1871// Add other dependencies as normal.
1872func (module *SdkLibraryImport) DepsMutator(ctx android.BottomUpMutatorContext) {
Paul Duffineedc5d52020-06-12 17:46:39 +01001873
1874 implName := module.implLibraryModuleName()
1875 if ctx.OtherModuleExists(implName) {
1876 ctx.AddVariationDependencies(nil, implLibraryTag, implName)
1877
1878 xmlPermissionsModuleName := module.xmlPermissionsModuleName()
1879 if module.sharedLibrary() && ctx.OtherModuleExists(xmlPermissionsModuleName) {
1880 // Add dependency to the rule for generating the xml permissions file
1881 ctx.AddDependency(module, xmlPermissionsFileTag, xmlPermissionsModuleName)
1882 }
1883 }
Colin Cross79c7c262019-04-17 11:11:46 -07001884}
1885
Paul Duffineedc5d52020-06-12 17:46:39 +01001886func (module *SdkLibraryImport) DepIsInSameApex(mctx android.BaseModuleContext, dep android.Module) bool {
1887 depTag := mctx.OtherModuleDependencyTag(dep)
1888 if depTag == xmlPermissionsFileTag {
1889 return true
1890 }
1891
1892 // None of the other dependencies of the java_sdk_library_import are in the same apex
1893 // as the one that references this module.
1894 return false
1895}
1896
Jooyung Han749dc692020-04-15 11:03:39 +09001897func (module *SdkLibraryImport) ShouldSupportSdkVersion(ctx android.BaseModuleContext, sdkVersion int) error {
1898 // we don't check prebuilt modules for sdk_version
1899 return nil
1900}
1901
Paul Duffineedc5d52020-06-12 17:46:39 +01001902func (module *SdkLibraryImport) OutputFiles(tag string) (android.Paths, error) {
Paul Duffin46dc45a2020-05-14 15:39:10 +01001903 return module.commonOutputFiles(tag)
1904}
1905
Paul Duffineedc5d52020-06-12 17:46:39 +01001906func (module *SdkLibraryImport) GenerateAndroidBuildActions(ctx android.ModuleContext) {
Paul Duffin0f8faff2020-05-20 16:18:00 +01001907 // Record the paths to the prebuilt stubs library and stubs source.
Colin Cross79c7c262019-04-17 11:11:46 -07001908 ctx.VisitDirectDeps(func(to android.Module) {
1909 tag := ctx.OtherModuleDependencyTag(to)
1910
Paul Duffin0f8faff2020-05-20 16:18:00 +01001911 // Extract information from any of the scope specific dependencies.
1912 if scopeTag, ok := tag.(scopeDependencyTag); ok {
1913 apiScope := scopeTag.apiScope
1914 scopePaths := module.getScopePathsCreateIfNeeded(apiScope)
1915
1916 // Extract information from the dependency. The exact information extracted
1917 // is determined by the nature of the dependency which is determined by the tag.
1918 scopeTag.extractDepInfo(ctx, to, scopePaths)
Paul Duffineedc5d52020-06-12 17:46:39 +01001919 } else if tag == implLibraryTag {
1920 if implLibrary, ok := to.(*Library); ok {
1921 module.implLibraryModule = implLibrary
1922 } else {
1923 ctx.ModuleErrorf("implementation library must be of type *java.Library but was %T", to)
1924 }
1925 } else if tag == xmlPermissionsFileTag {
1926 if xmlPermissionsFileModule, ok := to.(*sdkLibraryXml); ok {
1927 module.xmlPermissionsFileModule = xmlPermissionsFileModule
1928 } else {
1929 ctx.ModuleErrorf("xml permissions file module must be of type *sdkLibraryXml but was %T", to)
1930 }
Colin Cross79c7c262019-04-17 11:11:46 -07001931 }
1932 })
Paul Duffin0f8faff2020-05-20 16:18:00 +01001933
1934 // Populate the scope paths with information from the properties.
1935 for apiScope, scopeProperties := range module.scopeProperties {
1936 if len(scopeProperties.Jars) == 0 {
1937 continue
1938 }
1939
1940 paths := module.getScopePathsCreateIfNeeded(apiScope)
1941 paths.currentApiFilePath = android.OptionalPathForModuleSrc(ctx, scopeProperties.Current_api)
1942 paths.removedApiFilePath = android.OptionalPathForModuleSrc(ctx, scopeProperties.Removed_api)
1943 }
Colin Cross79c7c262019-04-17 11:11:46 -07001944}
1945
Paul Duffineedc5d52020-06-12 17:46:39 +01001946func (module *SdkLibraryImport) sdkJars(ctx android.BaseModuleContext, sdkVersion sdkSpec, headerJars bool) android.Paths {
1947
1948 // For consistency with SdkLibrary make the implementation jar available to libraries that
1949 // are within the same APEX.
1950 implLibraryModule := module.implLibraryModule
Pete Bentley0c7b26e2020-08-18 13:44:59 +00001951 if implLibraryModule != nil && withinSameApexAs(module, ctx.Module()) {
Paul Duffineedc5d52020-06-12 17:46:39 +01001952 if headerJars {
1953 return implLibraryModule.HeaderJars()
1954 } else {
1955 return implLibraryModule.ImplementationJars()
1956 }
1957 }
1958
Paul Duffin23970f42020-05-20 14:20:02 +01001959 return module.selectHeaderJarsForSdkVersion(ctx, sdkVersion)
Paul Duffin56d44902020-01-31 13:36:25 +00001960}
1961
Colin Cross79c7c262019-04-17 11:11:46 -07001962// to satisfy SdkLibraryDependency interface
Paul Duffineedc5d52020-06-12 17:46:39 +01001963func (module *SdkLibraryImport) SdkHeaderJars(ctx android.BaseModuleContext, sdkVersion sdkSpec) android.Paths {
Colin Cross79c7c262019-04-17 11:11:46 -07001964 // This module is just a wrapper for the prebuilt stubs.
Paul Duffineedc5d52020-06-12 17:46:39 +01001965 return module.sdkJars(ctx, sdkVersion, true)
Colin Cross79c7c262019-04-17 11:11:46 -07001966}
1967
1968// to satisfy SdkLibraryDependency interface
Paul Duffineedc5d52020-06-12 17:46:39 +01001969func (module *SdkLibraryImport) SdkImplementationJars(ctx android.BaseModuleContext, sdkVersion sdkSpec) android.Paths {
Colin Cross79c7c262019-04-17 11:11:46 -07001970 // This module is just a wrapper for the stubs.
Paul Duffineedc5d52020-06-12 17:46:39 +01001971 return module.sdkJars(ctx, sdkVersion, false)
1972}
1973
Ulya Trafimovich31e444e2020-08-14 17:32:16 +01001974// to satisfy SdkLibraryDependency interface
Paul Duffineedc5d52020-06-12 17:46:39 +01001975func (module *SdkLibraryImport) DexJarBuildPath() android.Path {
1976 if module.implLibraryModule == nil {
1977 return nil
1978 } else {
1979 return module.implLibraryModule.DexJarBuildPath()
1980 }
1981}
1982
Ulya Trafimovich31e444e2020-08-14 17:32:16 +01001983// to satisfy SdkLibraryDependency interface
1984func (module *SdkLibraryImport) DexJarInstallPath() android.Path {
1985 if module.implLibraryModule == nil {
1986 return nil
1987 } else {
1988 return module.implLibraryModule.DexJarInstallPath()
1989 }
1990}
1991
Paul Duffineedc5d52020-06-12 17:46:39 +01001992// to satisfy apex.javaDependency interface
1993func (module *SdkLibraryImport) JacocoReportClassesFile() android.Path {
1994 if module.implLibraryModule == nil {
1995 return nil
1996 } else {
1997 return module.implLibraryModule.JacocoReportClassesFile()
1998 }
1999}
2000
2001// to satisfy apex.javaDependency interface
Colin Cross08dca382020-07-21 20:31:17 -07002002func (module *SdkLibraryImport) LintDepSets() LintDepSets {
2003 if module.implLibraryModule == nil {
2004 return LintDepSets{}
2005 } else {
2006 return module.implLibraryModule.LintDepSets()
2007 }
2008}
2009
2010// to satisfy apex.javaDependency interface
Paul Duffineedc5d52020-06-12 17:46:39 +01002011func (module *SdkLibraryImport) Stem() string {
2012 return module.BaseModuleName()
Colin Cross79c7c262019-04-17 11:11:46 -07002013}
Jiyong Parke3833882020-02-17 17:28:10 +09002014
Paul Duffin44b481b2020-06-17 16:59:43 +01002015var _ ApexDependency = (*SdkLibraryImport)(nil)
2016
2017// to satisfy java.ApexDependency interface
2018func (module *SdkLibraryImport) HeaderJars() android.Paths {
2019 if module.implLibraryModule == nil {
2020 return nil
2021 } else {
2022 return module.implLibraryModule.HeaderJars()
2023 }
2024}
2025
2026// to satisfy java.ApexDependency interface
2027func (module *SdkLibraryImport) ImplementationAndResourcesJars() android.Paths {
2028 if module.implLibraryModule == nil {
2029 return nil
2030 } else {
2031 return module.implLibraryModule.ImplementationAndResourcesJars()
2032 }
2033}
2034
Jiyong Parke3833882020-02-17 17:28:10 +09002035//
2036// java_sdk_library_xml
2037//
2038type sdkLibraryXml struct {
2039 android.ModuleBase
2040 android.DefaultableModuleBase
2041 android.ApexModuleBase
2042
2043 properties sdkLibraryXmlProperties
2044
2045 outputFilePath android.OutputPath
2046 installDirPath android.InstallPath
2047}
2048
2049type sdkLibraryXmlProperties struct {
2050 // canonical name of the lib
2051 Lib_name *string
2052}
2053
2054// java_sdk_library_xml builds the permission xml file for a java_sdk_library.
2055// Not to be used directly by users. java_sdk_library internally uses this.
2056func sdkLibraryXmlFactory() android.Module {
2057 module := &sdkLibraryXml{}
2058
2059 module.AddProperties(&module.properties)
2060
2061 android.InitApexModule(module)
2062 android.InitAndroidArchModule(module, android.DeviceSupported, android.MultilibCommon)
2063
2064 return module
2065}
2066
2067// from android.PrebuiltEtcModule
2068func (module *sdkLibraryXml) SubDir() string {
2069 return "permissions"
2070}
2071
2072// from android.PrebuiltEtcModule
2073func (module *sdkLibraryXml) OutputFile() android.OutputPath {
2074 return module.outputFilePath
2075}
2076
2077// from android.ApexModule
2078func (module *sdkLibraryXml) AvailableFor(what string) bool {
2079 return true
2080}
2081
2082func (module *sdkLibraryXml) DepsMutator(ctx android.BottomUpMutatorContext) {
2083 // do nothing
2084}
2085
Jooyung Han749dc692020-04-15 11:03:39 +09002086func (module *sdkLibraryXml) ShouldSupportSdkVersion(ctx android.BaseModuleContext, sdkVersion int) error {
2087 // sdkLibraryXml doesn't need to be checked separately because java_sdk_library is checked
2088 return nil
2089}
2090
Jiyong Parke3833882020-02-17 17:28:10 +09002091// File path to the runtime implementation library
2092func (module *sdkLibraryXml) implPath() string {
2093 implName := proptools.String(module.properties.Lib_name)
Colin Crosse07f2312020-08-13 11:24:56 -07002094 if apexName := module.ApexVariationName(); apexName != "" {
2095 // TODO(b/146468504): ApexVariationName() is only a soong module name, not apex name.
Jiyong Parke3833882020-02-17 17:28:10 +09002096 // In most cases, this works fine. But when apex_name is set or override_apex is used
2097 // this can be wrong.
2098 return fmt.Sprintf("/apex/%s/javalib/%s.jar", apexName, implName)
2099 }
2100 partition := "system"
2101 if module.SocSpecific() {
2102 partition = "vendor"
2103 } else if module.DeviceSpecific() {
2104 partition = "odm"
2105 } else if module.ProductSpecific() {
2106 partition = "product"
2107 } else if module.SystemExtSpecific() {
2108 partition = "system_ext"
2109 }
2110 return "/" + partition + "/framework/" + implName + ".jar"
2111}
2112
2113func (module *sdkLibraryXml) GenerateAndroidBuildActions(ctx android.ModuleContext) {
2114 libName := proptools.String(module.properties.Lib_name)
2115 xmlContent := fmt.Sprintf(permissionsTemplate, libName, module.implPath())
2116
2117 module.outputFilePath = android.PathForModuleOut(ctx, libName+".xml").OutputPath
2118 rule := android.NewRuleBuilder()
2119 rule.Command().
2120 Text("/bin/bash -c \"echo -e '" + xmlContent + "'\" > ").
2121 Output(module.outputFilePath)
2122
2123 rule.Build(pctx, ctx, "java_sdk_xml", "Permission XML")
2124
2125 module.installDirPath = android.PathForModuleInstall(ctx, "etc", module.SubDir())
2126}
2127
2128func (module *sdkLibraryXml) AndroidMkEntries() []android.AndroidMkEntries {
2129 if !module.IsForPlatform() {
2130 return []android.AndroidMkEntries{android.AndroidMkEntries{
2131 Disabled: true,
2132 }}
2133 }
2134
2135 return []android.AndroidMkEntries{android.AndroidMkEntries{
2136 Class: "ETC",
2137 OutputFile: android.OptionalPathForPath(module.outputFilePath),
2138 ExtraEntries: []android.AndroidMkExtraEntriesFunc{
2139 func(entries *android.AndroidMkEntries) {
2140 entries.SetString("LOCAL_MODULE_TAGS", "optional")
2141 entries.SetString("LOCAL_MODULE_PATH", module.installDirPath.ToMakePath().String())
2142 entries.SetString("LOCAL_INSTALLED_MODULE_STEM", module.outputFilePath.Base())
2143 },
2144 },
2145 }}
2146}
Paul Duffindd46f712020-02-10 13:37:10 +00002147
2148type sdkLibrarySdkMemberType struct {
2149 android.SdkMemberTypeBase
2150}
2151
2152func (s *sdkLibrarySdkMemberType) AddDependencies(mctx android.BottomUpMutatorContext, dependencyTag blueprint.DependencyTag, names []string) {
2153 mctx.AddVariationDependencies(nil, dependencyTag, names...)
2154}
2155
2156func (s *sdkLibrarySdkMemberType) IsInstance(module android.Module) bool {
2157 _, ok := module.(*SdkLibrary)
2158 return ok
2159}
2160
2161func (s *sdkLibrarySdkMemberType) AddPrebuiltModule(ctx android.SdkMemberContext, member android.SdkMember) android.BpModule {
2162 return ctx.SnapshotBuilder().AddPrebuiltModule(member, "java_sdk_library_import")
2163}
2164
2165func (s *sdkLibrarySdkMemberType) CreateVariantPropertiesStruct() android.SdkMemberProperties {
2166 return &sdkLibrarySdkMemberProperties{}
2167}
2168
2169type sdkLibrarySdkMemberProperties struct {
2170 android.SdkMemberPropertiesBase
2171
2172 // Scope to per scope properties.
2173 Scopes map[*apiScope]scopeProperties
2174
2175 // Additional libraries that the exported stubs libraries depend upon.
2176 Libs []string
Paul Duffin3d1248c2020-04-09 00:10:17 +01002177
2178 // The Java stubs source files.
2179 Stub_srcs []string
Paul Duffinf7a64332020-05-13 16:54:55 +01002180
2181 // The naming scheme.
2182 Naming_scheme *string
Paul Duffind7eb1c22020-05-26 20:57:10 +01002183
2184 // True if the java_sdk_library_import is for a shared library, false
2185 // otherwise.
2186 Shared_library *bool
Paul Duffindd46f712020-02-10 13:37:10 +00002187}
2188
2189type scopeProperties struct {
Paul Duffin1fd005d2020-04-09 01:08:11 +01002190 Jars android.Paths
2191 StubsSrcJar android.Path
2192 CurrentApiFile android.Path
2193 RemovedApiFile android.Path
2194 SdkVersion string
Paul Duffindd46f712020-02-10 13:37:10 +00002195}
2196
2197func (s *sdkLibrarySdkMemberProperties) PopulateFromVariant(ctx android.SdkMemberContext, variant android.Module) {
2198 sdk := variant.(*SdkLibrary)
2199
2200 s.Scopes = make(map[*apiScope]scopeProperties)
2201 for _, apiScope := range allApiScopes {
Paul Duffin803a9562020-05-20 11:52:25 +01002202 paths := sdk.findScopePaths(apiScope)
2203 if paths == nil {
2204 continue
2205 }
2206
Paul Duffindd46f712020-02-10 13:37:10 +00002207 jars := paths.stubsImplPath
2208 if len(jars) > 0 {
2209 properties := scopeProperties{}
2210 properties.Jars = jars
Paul Duffin780c5f42020-05-12 15:52:55 +01002211 properties.SdkVersion = sdk.sdkVersionForStubsLibrary(ctx.SdkModuleContext(), apiScope)
Paul Duffin0f8faff2020-05-20 16:18:00 +01002212 properties.StubsSrcJar = paths.stubsSrcJar.Path()
Paul Duffin10269f12020-06-19 18:39:55 +01002213 if paths.currentApiFilePath.Valid() {
2214 properties.CurrentApiFile = paths.currentApiFilePath.Path()
2215 }
2216 if paths.removedApiFilePath.Valid() {
2217 properties.RemovedApiFile = paths.removedApiFilePath.Path()
2218 }
Paul Duffindd46f712020-02-10 13:37:10 +00002219 s.Scopes[apiScope] = properties
2220 }
2221 }
2222
2223 s.Libs = sdk.properties.Libs
Paul Duffindfa131e2020-05-15 20:37:11 +01002224 s.Naming_scheme = sdk.commonSdkLibraryProperties.Naming_scheme
Paul Duffind7eb1c22020-05-26 20:57:10 +01002225 s.Shared_library = proptools.BoolPtr(sdk.sharedLibrary())
Paul Duffindd46f712020-02-10 13:37:10 +00002226}
2227
2228func (s *sdkLibrarySdkMemberProperties) AddToPropertySet(ctx android.SdkMemberContext, propertySet android.BpPropertySet) {
Paul Duffinf7a64332020-05-13 16:54:55 +01002229 if s.Naming_scheme != nil {
2230 propertySet.AddProperty("naming_scheme", proptools.String(s.Naming_scheme))
2231 }
Paul Duffind7eb1c22020-05-26 20:57:10 +01002232 if s.Shared_library != nil {
2233 propertySet.AddProperty("shared_library", *s.Shared_library)
2234 }
Paul Duffinf7a64332020-05-13 16:54:55 +01002235
Paul Duffindd46f712020-02-10 13:37:10 +00002236 for _, apiScope := range allApiScopes {
2237 if properties, ok := s.Scopes[apiScope]; ok {
Paul Duffin6b836ba2020-05-13 19:19:49 +01002238 scopeSet := propertySet.AddPropertySet(apiScope.propertyName)
Paul Duffindd46f712020-02-10 13:37:10 +00002239
Paul Duffin3d1248c2020-04-09 00:10:17 +01002240 scopeDir := filepath.Join("sdk_library", s.OsPrefix(), apiScope.name)
2241
Paul Duffindd46f712020-02-10 13:37:10 +00002242 var jars []string
2243 for _, p := range properties.Jars {
Paul Duffin3d1248c2020-04-09 00:10:17 +01002244 dest := filepath.Join(scopeDir, ctx.Name()+"-stubs.jar")
Paul Duffindd46f712020-02-10 13:37:10 +00002245 ctx.SnapshotBuilder().CopyToSnapshot(p, dest)
2246 jars = append(jars, dest)
2247 }
2248 scopeSet.AddProperty("jars", jars)
2249
Paul Duffin3d1248c2020-04-09 00:10:17 +01002250 // Merge the stubs source jar into the snapshot zip so that when it is unpacked
2251 // the source files are also unpacked.
2252 snapshotRelativeDir := filepath.Join(scopeDir, ctx.Name()+"_stub_sources")
2253 ctx.SnapshotBuilder().UnzipToSnapshot(properties.StubsSrcJar, snapshotRelativeDir)
2254 scopeSet.AddProperty("stub_srcs", []string{snapshotRelativeDir})
2255
Paul Duffin1fd005d2020-04-09 01:08:11 +01002256 if properties.CurrentApiFile != nil {
2257 currentApiSnapshotPath := filepath.Join(scopeDir, ctx.Name()+".txt")
2258 ctx.SnapshotBuilder().CopyToSnapshot(properties.CurrentApiFile, currentApiSnapshotPath)
2259 scopeSet.AddProperty("current_api", currentApiSnapshotPath)
2260 }
2261
2262 if properties.RemovedApiFile != nil {
2263 removedApiSnapshotPath := filepath.Join(scopeDir, ctx.Name()+"-removed.txt")
Paul Duffin3dbf9fd2020-06-02 13:00:02 +01002264 ctx.SnapshotBuilder().CopyToSnapshot(properties.RemovedApiFile, removedApiSnapshotPath)
Paul Duffin1fd005d2020-04-09 01:08:11 +01002265 scopeSet.AddProperty("removed_api", removedApiSnapshotPath)
2266 }
2267
Paul Duffindd46f712020-02-10 13:37:10 +00002268 if properties.SdkVersion != "" {
2269 scopeSet.AddProperty("sdk_version", properties.SdkVersion)
2270 }
2271 }
2272 }
2273
2274 if len(s.Libs) > 0 {
2275 propertySet.AddPropertyWithTag("libs", s.Libs, ctx.SnapshotBuilder().SdkMemberReferencePropertyTag(false))
2276 }
2277}