blob: 0f7bbb56caddfde7eee0536a6833701aa4bf6090 [file] [log] [blame]
Jiyong Parkc678ad32018-04-10 13:07:10 +09001// Copyright 2018 Google Inc. All rights reserved.
2//
3// Licensed under the Apache License, Version 2.0 (the "License");
4// you may not use this file except in compliance with the License.
5// You may obtain a copy of the License at
6//
7// http://www.apache.org/licenses/LICENSE-2.0
8//
9// Unless required by applicable law or agreed to in writing, software
10// distributed under the License is distributed on an "AS IS" BASIS,
11// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12// See the License for the specific language governing permissions and
13// limitations under the License.
14
15package java
16
17import (
Jiyong Parkc678ad32018-04-10 13:07:10 +090018 "fmt"
19 "path"
Sundong Ahn054b19a2018-10-19 13:46:09 +090020 "path/filepath"
Paul Duffin6a2bd112020-04-07 19:27:04 +010021 "reflect"
Paul Duffin46fdda82020-05-14 15:39:10 +010022 "regexp"
Jiyong Park82484c02018-04-23 21:41:26 +090023 "sort"
Jiyong Parkc678ad32018-04-10 13:07:10 +090024 "strings"
Jiyong Park82484c02018-04-23 21:41:26 +090025 "sync"
Jiyong Parkc678ad32018-04-10 13:07:10 +090026
Paul Duffind1b3a922020-01-22 11:57:20 +000027 "github.com/google/blueprint"
Jiyong Parkc678ad32018-04-10 13:07:10 +090028 "github.com/google/blueprint/proptools"
Paul Duffin6a2bd112020-04-07 19:27:04 +010029
30 "android/soong/android"
Jiyong Parkc678ad32018-04-10 13:07:10 +090031)
32
Jooyung Han58f26ab2019-12-18 15:34:32 +090033const (
Paul Duffin1c094a02020-05-08 15:52:37 +010034 sdkXmlFileSuffix = ".xml"
35 permissionsTemplate = `<?xml version=\"1.0\" encoding=\"utf-8\"?>\n` +
Jooyung Han624058e2019-12-24 18:38:06 +090036 `<!-- Copyright (C) 2018 The Android Open Source Project\n` +
37 `\n` +
Jiyong Parke3833882020-02-17 17:28:10 +090038 ` Licensed under the Apache License, Version 2.0 (the \"License\");\n` +
Jooyung Han624058e2019-12-24 18:38:06 +090039 ` you may not use this file except in compliance with the License.\n` +
40 ` You may obtain a copy of the License at\n` +
41 `\n` +
42 ` http://www.apache.org/licenses/LICENSE-2.0\n` +
43 `\n` +
44 ` Unless required by applicable law or agreed to in writing, software\n` +
Jiyong Parke3833882020-02-17 17:28:10 +090045 ` distributed under the License is distributed on an \"AS IS\" BASIS,\n` +
Jooyung Han624058e2019-12-24 18:38:06 +090046 ` WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\n` +
47 ` See the License for the specific language governing permissions and\n` +
48 ` limitations under the License.\n` +
49 `-->\n` +
50 `<permissions>\n` +
Jiyong Parke3833882020-02-17 17:28:10 +090051 ` <library name=\"%s\" file=\"%s\"/>\n` +
Jooyung Han624058e2019-12-24 18:38:06 +090052 `</permissions>\n`
Jiyong Parkc678ad32018-04-10 13:07:10 +090053)
54
Paul Duffind1b3a922020-01-22 11:57:20 +000055// A tag to associated a dependency with a specific api scope.
56type scopeDependencyTag struct {
57 blueprint.BaseDependencyTag
58 name string
59 apiScope *apiScope
Paul Duffin5fb82132020-04-29 20:45:27 +010060
61 // Function for extracting appropriate path information from the dependency.
62 depInfoExtractor func(paths *scopePaths, dep android.Module) error
63}
64
65// Extract tag specific information from the dependency.
66func (tag scopeDependencyTag) extractDepInfo(ctx android.ModuleContext, dep android.Module, paths *scopePaths) {
67 err := tag.depInfoExtractor(paths, dep)
68 if err != nil {
69 ctx.ModuleErrorf("has an invalid {scopeDependencyTag: %s} dependency on module %s: %s", tag.name, ctx.OtherModuleName(dep), err.Error())
70 }
Paul Duffind1b3a922020-01-22 11:57:20 +000071}
72
73// Provides information about an api scope, e.g. public, system, test.
74type apiScope struct {
75 // The name of the api scope, e.g. public, system, test
76 name string
77
Paul Duffin51a2bee2020-05-05 14:40:52 +010078 // The api scope that this scope extends.
79 extends *apiScope
80
Paul Duffin3a254982020-04-28 10:44:03 +010081 // The legacy enabled status for a specific scope can be dependent on other
82 // properties that have been specified on the library so it is provided by
83 // a function that can determine the status by examining those properties.
84 legacyEnabledStatus func(module *SdkLibrary) bool
85
86 // The default enabled status for non-legacy behavior, which is triggered by
87 // explicitly enabling at least one api scope.
88 defaultEnabledStatus bool
89
90 // Gets a pointer to the scope specific properties.
91 scopeSpecificProperties func(module *SdkLibrary) *ApiScopeProperties
92
Paul Duffin6a2bd112020-04-07 19:27:04 +010093 // The name of the field in the dynamically created structure.
94 fieldName string
95
Paul Duffin0f270632020-05-13 19:19:49 +010096 // The name of the property in the java_sdk_library_import
97 propertyName string
98
Paul Duffind1b3a922020-01-22 11:57:20 +000099 // The tag to use to depend on the stubs library module.
100 stubsTag scopeDependencyTag
101
Paul Duffina377e4c2020-04-29 13:30:54 +0100102 // The tag to use to depend on the stubs source module (if separate from the API module).
103 stubsSourceTag scopeDependencyTag
104
105 // The tag to use to depend on the API file generating module (if separate from the stubs source module).
106 apiFileTag scopeDependencyTag
107
Paul Duffin5fb82132020-04-29 20:45:27 +0100108 // The tag to use to depend on the stubs source and API module.
109 stubsSourceAndApiTag scopeDependencyTag
Paul Duffind1b3a922020-01-22 11:57:20 +0000110
111 // The scope specific prefix to add to the api file base of "current.txt" or "removed.txt".
112 apiFilePrefix string
113
114 // The scope specific prefix to add to the sdk library module name to construct a scope specific
115 // module name.
116 moduleSuffix string
117
Paul Duffind1b3a922020-01-22 11:57:20 +0000118 // SDK version that the stubs library is built against. Note that this is always
119 // *current. Older stubs library built with a numbered SDK version is created from
120 // the prebuilt jar.
121 sdkVersion string
Paul Duffin3c7c3472020-04-07 18:50:10 +0100122
123 // Extra arguments to pass to droidstubs for this scope.
124 droidstubsArgs []string
Anton Hansson5ff28e52020-05-02 11:19:36 +0100125
Paul Duffina377e4c2020-04-29 13:30:54 +0100126 // The args that must be passed to droidstubs to generate the stubs source
127 // for this scope.
128 //
129 // The stubs source must include the definitions of everything that is in this
130 // api scope and all the scopes that this one extends.
131 droidstubsArgsForGeneratingStubsSource []string
132
133 // The args that must be passed to droidstubs to generate the API for this scope.
134 //
135 // The API only includes the additional members that this scope adds over the scope
136 // that it extends.
137 droidstubsArgsForGeneratingApi []string
138
139 // True if the stubs source and api can be created by the same metalava invocation.
Makoto Onuki6cc28092020-07-10 13:32:56 -0700140 // TODO(b/146727827) Now that metalava supports "API hierarchy", do we still need it?
Paul Duffina377e4c2020-04-29 13:30:54 +0100141 createStubsSourceAndApiTogether bool
142
Anton Hansson5ff28e52020-05-02 11:19:36 +0100143 // Whether the api scope can be treated as unstable, and should skip compat checks.
144 unstable bool
Paul Duffind1b3a922020-01-22 11:57:20 +0000145}
146
147// Initialize a scope, creating and adding appropriate dependency tags
148func initApiScope(scope *apiScope) *apiScope {
Paul Duffin5fb82132020-04-29 20:45:27 +0100149 name := scope.name
Paul Duffin46fdda82020-05-14 15:39:10 +0100150 scopeByName[name] = scope
151 allScopeNames = append(allScopeNames, name)
Paul Duffin0f270632020-05-13 19:19:49 +0100152 scope.propertyName = strings.ReplaceAll(name, "-", "_")
153 scope.fieldName = proptools.FieldNameForProperty(scope.propertyName)
Paul Duffind1b3a922020-01-22 11:57:20 +0000154 scope.stubsTag = scopeDependencyTag{
Paul Duffin5fb82132020-04-29 20:45:27 +0100155 name: name + "-stubs",
156 apiScope: scope,
157 depInfoExtractor: (*scopePaths).extractStubsLibraryInfoFromDependency,
Paul Duffind1b3a922020-01-22 11:57:20 +0000158 }
Paul Duffina377e4c2020-04-29 13:30:54 +0100159 scope.stubsSourceTag = scopeDependencyTag{
160 name: name + "-stubs-source",
161 apiScope: scope,
162 depInfoExtractor: (*scopePaths).extractStubsSourceInfoFromDep,
163 }
164 scope.apiFileTag = scopeDependencyTag{
165 name: name + "-api",
166 apiScope: scope,
167 depInfoExtractor: (*scopePaths).extractApiInfoFromDep,
168 }
Paul Duffin5fb82132020-04-29 20:45:27 +0100169 scope.stubsSourceAndApiTag = scopeDependencyTag{
170 name: name + "-stubs-source-and-api",
171 apiScope: scope,
172 depInfoExtractor: (*scopePaths).extractStubsSourceAndApiInfoFromApiStubsProvider,
Paul Duffind1b3a922020-01-22 11:57:20 +0000173 }
Paul Duffina377e4c2020-04-29 13:30:54 +0100174
175 // To get the args needed to generate the stubs source append all the args from
176 // this scope and all the scopes it extends as each set of args adds additional
177 // members to the stubs.
178 var stubsSourceArgs []string
179 for s := scope; s != nil; s = s.extends {
180 stubsSourceArgs = append(stubsSourceArgs, s.droidstubsArgs...)
181 }
182 scope.droidstubsArgsForGeneratingStubsSource = stubsSourceArgs
183
184 // Currently the args needed to generate the API are the same as the args
185 // needed to add additional members.
186 apiArgs := scope.droidstubsArgs
187 scope.droidstubsArgsForGeneratingApi = apiArgs
188
189 // If the args needed to generate the stubs and API are the same then they
190 // can be generated in a single invocation of metalava, otherwise they will
191 // need separate invocations.
192 scope.createStubsSourceAndApiTogether = reflect.DeepEqual(stubsSourceArgs, apiArgs)
193
Paul Duffind1b3a922020-01-22 11:57:20 +0000194 return scope
195}
196
Paul Duffinb74ee3e2020-05-08 14:16:20 +0100197func (scope *apiScope) stubsLibraryModuleName(baseName string) string {
Paul Duffin1c094a02020-05-08 15:52:37 +0100198 return baseName + ".stubs" + scope.moduleSuffix
Paul Duffind1b3a922020-01-22 11:57:20 +0000199}
200
Paul Duffin5fb82132020-04-29 20:45:27 +0100201func (scope *apiScope) stubsSourceModuleName(baseName string) string {
Paul Duffin1c094a02020-05-08 15:52:37 +0100202 return baseName + ".stubs.source" + scope.moduleSuffix
Paul Duffind1b3a922020-01-22 11:57:20 +0000203}
204
Paul Duffina377e4c2020-04-29 13:30:54 +0100205func (scope *apiScope) apiModuleName(baseName string) string {
Paul Duffin1c094a02020-05-08 15:52:37 +0100206 return baseName + ".api" + scope.moduleSuffix
Paul Duffina377e4c2020-04-29 13:30:54 +0100207}
208
Paul Duffin3a254982020-04-28 10:44:03 +0100209func (scope *apiScope) String() string {
210 return scope.name
211}
212
Paul Duffind1b3a922020-01-22 11:57:20 +0000213type apiScopes []*apiScope
214
215func (scopes apiScopes) Strings(accessor func(*apiScope) string) []string {
216 var list []string
217 for _, scope := range scopes {
218 list = append(list, accessor(scope))
219 }
220 return list
221}
222
Jiyong Parkc678ad32018-04-10 13:07:10 +0900223var (
Paul Duffin46fdda82020-05-14 15:39:10 +0100224 scopeByName = make(map[string]*apiScope)
225 allScopeNames []string
Paul Duffind1b3a922020-01-22 11:57:20 +0000226 apiScopePublic = initApiScope(&apiScope{
Paul Duffin3a254982020-04-28 10:44:03 +0100227 name: "public",
228
229 // Public scope is enabled by default for both legacy and non-legacy modes.
230 legacyEnabledStatus: func(module *SdkLibrary) bool {
231 return true
232 },
233 defaultEnabledStatus: true,
234
235 scopeSpecificProperties: func(module *SdkLibrary) *ApiScopeProperties {
236 return &module.sdkLibraryProperties.Public
237 },
Paul Duffind1b3a922020-01-22 11:57:20 +0000238 sdkVersion: "current",
239 })
240 apiScopeSystem = initApiScope(&apiScope{
Paul Duffin3a254982020-04-28 10:44:03 +0100241 name: "system",
242 extends: apiScopePublic,
243 legacyEnabledStatus: (*SdkLibrary).generateTestAndSystemScopesByDefault,
244 scopeSpecificProperties: func(module *SdkLibrary) *ApiScopeProperties {
245 return &module.sdkLibraryProperties.System
246 },
Anton Hanssone366fff2020-04-28 16:47:41 +0100247 apiFilePrefix: "system-",
Paul Duffin1c094a02020-05-08 15:52:37 +0100248 moduleSuffix: ".system",
Anton Hanssone366fff2020-04-28 16:47:41 +0100249 sdkVersion: "system_current",
Paul Duffin991f2622020-04-29 22:18:41 +0100250 droidstubsArgs: []string{"-showAnnotation android.annotation.SystemApi\\(client=android.annotation.SystemApi.Client.PRIVILEGED_APPS\\)"},
Paul Duffind1b3a922020-01-22 11:57:20 +0000251 })
252 apiScopeTest = initApiScope(&apiScope{
Paul Duffin3a254982020-04-28 10:44:03 +0100253 name: "test",
254 extends: apiScopePublic,
255 legacyEnabledStatus: (*SdkLibrary).generateTestAndSystemScopesByDefault,
256 scopeSpecificProperties: func(module *SdkLibrary) *ApiScopeProperties {
257 return &module.sdkLibraryProperties.Test
258 },
Anton Hanssone366fff2020-04-28 16:47:41 +0100259 apiFilePrefix: "test-",
Paul Duffin1c094a02020-05-08 15:52:37 +0100260 moduleSuffix: ".test",
Anton Hanssone366fff2020-04-28 16:47:41 +0100261 sdkVersion: "test_current",
262 droidstubsArgs: []string{"-showAnnotation android.annotation.TestApi"},
Anton Hansson5ff28e52020-05-02 11:19:36 +0100263 unstable: true,
Paul Duffind1b3a922020-01-22 11:57:20 +0000264 })
Paul Duffin6d7f0a72020-04-28 14:13:56 +0100265 apiScopeModuleLib = initApiScope(&apiScope{
Paul Duffin0f270632020-05-13 19:19:49 +0100266 name: "module-lib",
Paul Duffin6d7f0a72020-04-28 14:13:56 +0100267 extends: apiScopeSystem,
Paul Duffin5a757b12020-06-02 13:00:08 +0100268 // The module-lib scope is disabled by default in legacy mode.
Paul Duffin6d7f0a72020-04-28 14:13:56 +0100269 //
270 // Enabling this would break existing usages.
271 legacyEnabledStatus: func(module *SdkLibrary) bool {
272 return false
273 },
274 scopeSpecificProperties: func(module *SdkLibrary) *ApiScopeProperties {
275 return &module.sdkLibraryProperties.Module_lib
276 },
277 apiFilePrefix: "module-lib-",
278 moduleSuffix: ".module_lib",
279 sdkVersion: "module_current",
280 droidstubsArgs: []string{
281 "--show-annotation android.annotation.SystemApi\\(client=android.annotation.SystemApi.Client.MODULE_LIBRARIES\\)",
Makoto Onuki6cc28092020-07-10 13:32:56 -0700282 "--show-for-stub-purposes-annotation android.annotation.SystemApi\\(client=android.annotation.SystemApi.Client.PRIVILEGED_APPS\\)",
Paul Duffin6d7f0a72020-04-28 14:13:56 +0100283 },
284 })
Paul Duffin5a757b12020-06-02 13:00:08 +0100285 apiScopeSystemServer = initApiScope(&apiScope{
286 name: "system-server",
287 extends: apiScopePublic,
288 // The system-server scope is disabled by default in legacy mode.
289 //
290 // Enabling this would break existing usages.
291 legacyEnabledStatus: func(module *SdkLibrary) bool {
292 return false
293 },
294 scopeSpecificProperties: func(module *SdkLibrary) *ApiScopeProperties {
295 return &module.sdkLibraryProperties.System_server
296 },
297 apiFilePrefix: "system-server-",
298 moduleSuffix: ".system_server",
299 sdkVersion: "system_server_current",
300 droidstubsArgs: []string{
301 "--show-annotation android.annotation.SystemApi\\(client=android.annotation.SystemApi.Client.SYSTEM_SERVER\\) ",
302 "--hide-annotation android.annotation.Hide",
303 // com.android.* classes are okay in this interface"
304 "--hide InternalClasses",
305 },
306 })
Paul Duffind1b3a922020-01-22 11:57:20 +0000307 allApiScopes = apiScopes{
308 apiScopePublic,
309 apiScopeSystem,
310 apiScopeTest,
Paul Duffin6d7f0a72020-04-28 14:13:56 +0100311 apiScopeModuleLib,
Paul Duffin5a757b12020-06-02 13:00:08 +0100312 apiScopeSystemServer,
Paul Duffind1b3a922020-01-22 11:57:20 +0000313 }
Jiyong Parkc678ad32018-04-10 13:07:10 +0900314)
315
Jiyong Park82484c02018-04-23 21:41:26 +0900316var (
317 javaSdkLibrariesLock sync.Mutex
318)
319
Jiyong Parkc678ad32018-04-10 13:07:10 +0900320// TODO: these are big features that are currently missing
Jiyong Park1be96912018-05-28 18:02:19 +0900321// 1) disallowing linking to the runtime shared lib
322// 2) HTML generation
Jiyong Parkc678ad32018-04-10 13:07:10 +0900323
324func init() {
Paul Duffin43dc1cc2019-12-19 11:18:54 +0000325 RegisterSdkLibraryBuildComponents(android.InitRegistrationContext)
Jiyong Parkc678ad32018-04-10 13:07:10 +0900326
Jiyong Park82484c02018-04-23 21:41:26 +0900327 android.RegisterMakeVarsProvider(pctx, func(ctx android.MakeVarsContext) {
328 javaSdkLibraries := javaSdkLibraries(ctx.Config())
329 sort.Strings(*javaSdkLibraries)
330 ctx.Strict("JAVA_SDK_LIBRARIES", strings.Join(*javaSdkLibraries, " "))
331 })
Paul Duffin61871622020-02-10 13:37:10 +0000332
333 // Register sdk member types.
334 android.RegisterSdkMemberType(&sdkLibrarySdkMemberType{
335 android.SdkMemberTypeBase{
336 PropertyName: "java_sdk_libs",
337 SupportsSdk: true,
338 },
339 })
Jiyong Parkc678ad32018-04-10 13:07:10 +0900340}
341
Paul Duffin43dc1cc2019-12-19 11:18:54 +0000342func RegisterSdkLibraryBuildComponents(ctx android.RegistrationContext) {
343 ctx.RegisterModuleType("java_sdk_library", SdkLibraryFactory)
344 ctx.RegisterModuleType("java_sdk_library_import", sdkLibraryImportFactory)
345}
346
Paul Duffin3a254982020-04-28 10:44:03 +0100347// Properties associated with each api scope.
348type ApiScopeProperties struct {
349 // Indicates whether the api surface is generated.
350 //
351 // If this is set for any scope then all scopes must explicitly specify if they
352 // are enabled. This is to prevent new usages from depending on legacy behavior.
353 //
354 // Otherwise, if this is not set for any scope then the default behavior is
355 // scope specific so please refer to the scope specific property documentation.
356 Enabled *bool
Paul Duffin080f5ee2020-05-12 11:50:28 +0100357
358 // The sdk_version to use for building the stubs.
359 //
360 // If not specified then it will use an sdk_version determined as follows:
361 // 1) If the sdk_version specified on the java_sdk_library is none then this
362 // will be none. This is used for java_sdk_library instances that are used
363 // to create stubs that contribute to the core_current sdk version.
364 // 2) Otherwise, it is assumed that this library extends but does not contribute
365 // directly to a specific sdk_version and so this uses the sdk_version appropriate
366 // for the api scope. e.g. public will use sdk_version: current, system will use
367 // sdk_version: system_current, etc.
368 //
369 // This does not affect the sdk_version used for either generating the stubs source
370 // or the API file. They both have to use the same sdk_version as is used for
371 // compiling the implementation library.
372 Sdk_version *string
Paul Duffin3a254982020-04-28 10:44:03 +0100373}
374
Jiyong Parkc678ad32018-04-10 13:07:10 +0900375type sdkLibraryProperties struct {
Paul Duffin9d582cc2020-05-16 15:52:12 +0100376 // Visibility for impl library module. If not specified then defaults to the
377 // visibility property.
378 Impl_library_visibility []string
379
Paul Duffin344c4ee2020-04-29 23:35:13 +0100380 // Visibility for stubs library modules. If not specified then defaults to the
381 // visibility property.
382 Stubs_library_visibility []string
383
384 // Visibility for stubs source modules. If not specified then defaults to the
385 // visibility property.
386 Stubs_source_visibility []string
387
Sundong Ahnf043cf62018-06-25 16:04:37 +0900388 // List of Java libraries that will be in the classpath when building stubs
389 Stub_only_libs []string `android:"arch_variant"`
390
Paul Duffin7a586d32019-12-30 17:09:34 +0000391 // list of package names that will be documented and publicized as API.
392 // This allows the API to be restricted to a subset of the source files provided.
393 // If this is unspecified then all the source files will be treated as being part
394 // of the API.
Jiyong Parkc678ad32018-04-10 13:07:10 +0900395 Api_packages []string
396
Jiyong Park5a2c9d72018-05-01 22:25:41 +0900397 // list of package names that must be hidden from the API
398 Hidden_api_packages []string
399
Paul Duffin749f98f2019-12-30 17:23:46 +0000400 // the relative path to the directory containing the api specification files.
401 // Defaults to "api".
402 Api_dir *string
403
Paul Duffind11e78e2020-05-15 20:37:11 +0100404 // Determines whether a runtime implementation library is built; defaults to false.
405 //
406 // If true then it also prevents the module from being used as a shared module, i.e.
407 // it is as is shared_library: false, was set.
Paul Duffin43db9be2019-12-30 17:35:49 +0000408 Api_only *bool
409
Paul Duffin11512472019-02-11 15:55:17 +0000410 // local files that are used within user customized droiddoc options.
411 Droiddoc_option_files []string
412
413 // additional droiddoc options
414 // Available variables for substitution:
415 //
416 // $(location <label>): the path to the droiddoc_option_files with name <label>
Sundong Ahndd567f92018-07-31 17:19:11 +0900417 Droiddoc_options []string
418
Paul Duffin2ce1e812020-05-20 19:35:27 +0100419 // is set to true, Metalava will allow framework SDK to contain annotations.
420 Annotations_enabled *bool
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 Duffin3a254982020-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 Duffin5a757b12020-06-02 13:00:08 +0100460 // The properties specific to the module-lib api scope
Paul Duffin6d7f0a72020-04-28 14:13:56 +0100461 //
Paul Duffin5a757b12020-06-02 13:00:08 +0100462 // Unless explicitly specified by using test.enabled the module-lib api scope is
Paul Duffin6d7f0a72020-04-28 14:13:56 +0100463 // disabled by default.
464 Module_lib ApiScopeProperties
465
Paul Duffin5a757b12020-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 Park27fc4142020-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 Duffin8986cc92020-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 Duffin533f9c72020-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 Duffin533f9c72020-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 Duffin5fb82132020-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 Duffina377e4c2020-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 Duffin5fb82132020-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 Duffin533f9c72020-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 Duffina377e4c2020-04-29 13:30:54 +0100544func (paths *scopePaths) extractApiInfoFromApiStubsProvider(provider ApiStubsProvider) {
Paul Duffin533f9c72020-05-20 16:18:00 +0100545 paths.currentApiFilePath = android.OptionalPathForPath(provider.ApiFilePath())
546 paths.removedApiFilePath = android.OptionalPathForPath(provider.RemovedApiFilePath())
Paul Duffina377e4c2020-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 Duffin533f9c72020-05-20 16:18:00 +0100555func (paths *scopePaths) extractStubsSourceInfoFromApiStubsProviders(provider ApiStubsSrcProvider) {
556 paths.stubsSrcJar = android.OptionalPathForPath(provider.StubsSrcJar())
Paul Duffina377e4c2020-04-29 13:30:54 +0100557}
558
559func (paths *scopePaths) extractStubsSourceInfoFromDep(dep android.Module) error {
Paul Duffin533f9c72020-05-20 16:18:00 +0100560 return paths.treatDepAsApiStubsSrcProvider(dep, func(provider ApiStubsSrcProvider) {
Paul Duffina377e4c2020-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 Duffin1a724e62020-05-08 13:44:43 +0100573 // The naming scheme to use for the components that this module creates.
574 //
Paul Duffindef8a892020-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 Duffin1a724e62020-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 Duffina377e4c2020-04-29 13:30:54 +0100583 Naming_scheme *string
Paul Duffind11e78e2020-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 Duffina377e4c2020-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 Duffinb74ee3e2020-05-08 14:16:20 +0100595 moduleBase *android.ModuleBase
596
Paul Duffin56d44902020-01-31 13:36:25 +0000597 scopePaths map[*apiScope]*scopePaths
Paul Duffin1a724e62020-05-08 13:44:43 +0100598
599 namingScheme sdkLibraryComponentNamingScheme
600
Paul Duffind11e78e2020-05-15 20:37:11 +0100601 commonSdkLibraryProperties commonToSdkLibraryAndImportProperties
Paul Duffin64e61992020-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 Duffinb74ee3e2020-05-08 14:16:20 +0100607func (c *commonToSdkLibraryAndImport) initCommon(moduleBase *android.ModuleBase) {
608 c.moduleBase = moduleBase
Paul Duffin1a724e62020-05-08 13:44:43 +0100609
Paul Duffind11e78e2020-05-15 20:37:11 +0100610 moduleBase.AddProperties(&c.commonSdkLibraryProperties)
Paul Duffin64e61992020-05-15 10:20:31 +0100611
612 // Initialize this as an sdk library component.
613 c.initSdkLibraryComponent(moduleBase)
Paul Duffin1a724e62020-05-08 13:44:43 +0100614}
615
616func (c *commonToSdkLibraryAndImport) initCommonAfterDefaultsApplied(ctx android.DefaultableHookContext) bool {
Paul Duffind11e78e2020-05-15 20:37:11 +0100617 schemeProperty := proptools.StringDefault(c.commonSdkLibraryProperties.Naming_scheme, "default")
Paul Duffin1a724e62020-05-08 13:44:43 +0100618 switch schemeProperty {
619 case "default":
620 c.namingScheme = &defaultNamingScheme{}
Paul Duffindef8a892020-05-08 15:36:30 +0100621 case "framework-modules":
622 c.namingScheme = &frameworkModulesNamingScheme{}
Paul Duffin1a724e62020-05-08 13:44:43 +0100623 default:
624 ctx.PropertyErrorf("naming_scheme", "expected 'default' but was %q", schemeProperty)
625 return false
626 }
627
Paul Duffind11e78e2020-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 Duffin64e61992020-05-15 10:20:31 +0100633
Paul Duffin1a724e62020-05-08 13:44:43 +0100634 return true
Paul Duffinb74ee3e2020-05-08 14:16:20 +0100635}
636
Paul Duffinf642a312020-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 Duffinb74ee3e2020-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 Duffin1a724e62020-05-08 13:44:43 +0100649 return c.namingScheme.stubsLibraryModuleName(apiScope, c.moduleBase.BaseModuleName())
Paul Duffinb74ee3e2020-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 Duffin1a724e62020-05-08 13:44:43 +0100655 return c.namingScheme.stubsSourceModuleName(apiScope, c.moduleBase.BaseModuleName())
Paul Duffinb74ee3e2020-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 Duffin1a724e62020-05-08 13:44:43 +0100661 return c.namingScheme.apiModuleName(apiScope, c.moduleBase.BaseModuleName())
Paul Duffinb74ee3e2020-05-08 14:16:20 +0100662}
663
Paul Duffin46fdda82020-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 Duffin5ae30792020-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 Duffin5ae30792020-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 Duffina3fb67d2020-05-20 14:20:02 +0100775func (c *commonToSdkLibraryAndImport) selectHeaderJarsForSdkVersion(ctx android.BaseModuleContext, sdkVersion sdkSpec) android.Paths {
Paul Duffin47624362020-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 Duffin5ae30792020-05-20 11:52:25 +0100786 case sdkModule:
787 apiScope = apiScopeModuleLib
Paul Duffin47624362020-05-20 12:19:10 +0100788 case sdkTest:
789 apiScope = apiScopeTest
Paul Duffin5a757b12020-06-02 13:00:08 +0100790 case sdkSystemServer:
791 apiScope = apiScopeSystemServer
Paul Duffin47624362020-05-20 12:19:10 +0100792 default:
793 apiScope = apiScopePublic
794 }
795
Paul Duffin5ae30792020-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 Duffina3fb67d2020-05-20 14:20:02 +0100808 return paths.stubsHeaderPath
Paul Duffin47624362020-05-20 12:19:10 +0100809}
810
Paul Duffin64e61992020-05-15 10:20:31 +0100811func (c *commonToSdkLibraryAndImport) sdkComponentPropertiesForChildLibrary() interface{} {
812 componentProps := &struct {
813 SdkLibraryToImplicitlyTrack *string
Paul Duffind11e78e2020-05-15 20:37:11 +0100814 }{}
815
816 if c.sharedLibrary() {
Paul Duffin64e61992020-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 Duffind11e78e2020-05-15 20:37:11 +0100821 componentProps.SdkLibraryToImplicitlyTrack = proptools.StringPtr(c.moduleBase.BaseModuleName())
Paul Duffin64e61992020-05-15 10:20:31 +0100822 }
823
824 return componentProps
825}
826
Paul Duffind11e78e2020-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 Duffin64e61992020-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
852func (e *EmbeddableSdkLibraryComponent) OptionalImplicitSdkLibrary() []string {
853 if e.sdkLibraryComponentProperties.SdkLibraryToImplicitlyTrack != nil {
854 return []string{*e.sdkLibraryComponentProperties.SdkLibraryToImplicitlyTrack}
855 }
856 return nil
857}
858
859// Implemented by modules that are (or possibly could be) a component of a java_sdk_library
860// (including the java_sdk_library) itself.
861type SdkLibraryComponentDependency interface {
862 // The optional name of the sdk library that should be implicitly added to the
863 // AndroidManifest of an app that contains code which references the sdk library.
864 //
865 // Returns an array containing 0 or 1 items rather than a *string to make it easier
866 // to append this to the list of exported sdk libraries.
867 OptionalImplicitSdkLibrary() []string
868}
869
870// Make sure that all the module types that are components of java_sdk_library/_import
871// and which can be referenced (directly or indirectly) from an android app implement
872// the SdkLibraryComponentDependency interface.
873var _ SdkLibraryComponentDependency = (*Library)(nil)
874var _ SdkLibraryComponentDependency = (*Import)(nil)
875var _ SdkLibraryComponentDependency = (*SdkLibrary)(nil)
Paul Duffinf642a312020-06-12 17:46:39 +0100876var _ SdkLibraryComponentDependency = (*SdkLibraryImport)(nil)
Paul Duffin64e61992020-05-15 10:20:31 +0100877
878// Provides access to sdk_version related header and implentation jars.
879type SdkLibraryDependency interface {
880 SdkLibraryComponentDependency
881
882 // Get the header jars appropriate for the supplied sdk_version.
883 //
884 // These are turbine generated jars so they only change if the externals of the
885 // class changes but it does not contain and implementation or JavaDoc.
886 SdkHeaderJars(ctx android.BaseModuleContext, sdkVersion sdkSpec) android.Paths
887
888 // Get the implementation jars appropriate for the supplied sdk version.
889 //
890 // These are either the implementation jar for the whole sdk library or the implementation
891 // jars for the stubs. The latter should only be needed when generating JavaDoc as otherwise
892 // they are identical to the corresponding header jars.
893 SdkImplementationJars(ctx android.BaseModuleContext, sdkVersion sdkSpec) android.Paths
894}
895
Inseob Kimc0907f12019-02-08 21:00:45 +0900896type SdkLibrary struct {
Sundong Ahn054b19a2018-10-19 13:46:09 +0900897 Library
Jiyong Parkc678ad32018-04-10 13:07:10 +0900898
Sundong Ahn054b19a2018-10-19 13:46:09 +0900899 sdkLibraryProperties sdkLibraryProperties
Jiyong Parkc678ad32018-04-10 13:07:10 +0900900
Paul Duffin3a254982020-04-28 10:44:03 +0100901 // Map from api scope to the scope specific property structure.
902 scopeToProperties map[*apiScope]*ApiScopeProperties
903
Paul Duffin56d44902020-01-31 13:36:25 +0000904 commonToSdkLibraryAndImport
Jiyong Parkc678ad32018-04-10 13:07:10 +0900905}
906
Inseob Kimc0907f12019-02-08 21:00:45 +0900907var _ Dependency = (*SdkLibrary)(nil)
908var _ SdkLibraryDependency = (*SdkLibrary)(nil)
Colin Cross897d2ed2019-02-11 14:03:51 -0800909
Paul Duffin3a254982020-04-28 10:44:03 +0100910func (module *SdkLibrary) generateTestAndSystemScopesByDefault() bool {
911 return module.sdkLibraryProperties.Generate_system_and_test_apis
912}
913
914func (module *SdkLibrary) getGeneratedApiScopes(ctx android.EarlyModuleContext) apiScopes {
915 // Check to see if any scopes have been explicitly enabled. If any have then all
916 // must be.
917 anyScopesExplicitlyEnabled := false
918 for _, scope := range allApiScopes {
919 scopeProperties := module.scopeToProperties[scope]
920 if scopeProperties.Enabled != nil {
921 anyScopesExplicitlyEnabled = true
922 break
923 }
Paul Duffind1b3a922020-01-22 11:57:20 +0000924 }
Paul Duffin3a254982020-04-28 10:44:03 +0100925
926 var generatedScopes apiScopes
927 enabledScopes := make(map[*apiScope]struct{})
928 for _, scope := range allApiScopes {
929 scopeProperties := module.scopeToProperties[scope]
930 // If any scopes are explicitly enabled then ignore the legacy enabled status.
931 // This is to ensure that any new usages of this module type do not rely on legacy
932 // behaviour.
933 defaultEnabledStatus := false
934 if anyScopesExplicitlyEnabled {
935 defaultEnabledStatus = scope.defaultEnabledStatus
936 } else {
937 defaultEnabledStatus = scope.legacyEnabledStatus(module)
938 }
939 enabled := proptools.BoolDefault(scopeProperties.Enabled, defaultEnabledStatus)
940 if enabled {
941 enabledScopes[scope] = struct{}{}
942 generatedScopes = append(generatedScopes, scope)
943 }
944 }
945
946 // Now check to make sure that any scope that is extended by an enabled scope is also
947 // enabled.
948 for _, scope := range allApiScopes {
949 if _, ok := enabledScopes[scope]; ok {
950 extends := scope.extends
951 if extends != nil {
952 if _, ok := enabledScopes[extends]; !ok {
953 ctx.ModuleErrorf("enabled api scope %q depends on disabled scope %q", scope, extends)
954 }
955 }
956 }
957 }
958
959 return generatedScopes
Paul Duffind1b3a922020-01-22 11:57:20 +0000960}
961
Paul Duffinf642a312020-06-12 17:46:39 +0100962type sdkLibraryComponentTag struct {
963 blueprint.BaseDependencyTag
964 name string
965}
966
967// Mark this tag so dependencies that use it are excluded from visibility enforcement.
968func (t sdkLibraryComponentTag) ExcludeFromVisibilityEnforcement() {}
969
970var xmlPermissionsFileTag = sdkLibraryComponentTag{name: "xml-permissions-file"}
Paul Duffine74ac732020-02-06 13:51:46 +0000971
Jiyong Parke3833882020-02-17 17:28:10 +0900972func IsXmlPermissionsFileDepTag(depTag blueprint.DependencyTag) bool {
Paul Duffinf642a312020-06-12 17:46:39 +0100973 if dt, ok := depTag.(sdkLibraryComponentTag); ok {
Jiyong Parke3833882020-02-17 17:28:10 +0900974 return dt == xmlPermissionsFileTag
975 }
976 return false
977}
978
Paul Duffinf642a312020-06-12 17:46:39 +0100979var implLibraryTag = sdkLibraryComponentTag{name: "impl-library"}
Paul Duffin9d582cc2020-05-16 15:52:12 +0100980
Inseob Kimc0907f12019-02-08 21:00:45 +0900981func (module *SdkLibrary) DepsMutator(ctx android.BottomUpMutatorContext) {
Paul Duffin3a254982020-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 Duffinb74ee3e2020-05-08 14:16:20 +0100984 ctx.AddVariationDependencies(nil, apiScope.stubsTag, module.stubsLibraryModuleName(apiScope))
Paul Duffind1b3a922020-01-22 11:57:20 +0000985
Paul Duffina377e4c2020-04-29 13:30:54 +0100986 // If the stubs source and API cannot be generated together then add an additional dependency on
987 // the API module.
988 if apiScope.createStubsSourceAndApiTogether {
989 // Add a dependency on the stubs source in order to access both stubs source and api information.
Paul Duffinb74ee3e2020-05-08 14:16:20 +0100990 ctx.AddVariationDependencies(nil, apiScope.stubsSourceAndApiTag, module.stubsSourceModuleName(apiScope))
Paul Duffina377e4c2020-04-29 13:30:54 +0100991 } else {
992 // Add separate dependencies on the creators of the stubs source files and the API.
Paul Duffinb74ee3e2020-05-08 14:16:20 +0100993 ctx.AddVariationDependencies(nil, apiScope.stubsSourceTag, module.stubsSourceModuleName(apiScope))
994 ctx.AddVariationDependencies(nil, apiScope.apiFileTag, module.apiModuleName(apiScope))
Paul Duffina377e4c2020-04-29 13:30:54 +0100995 }
Sundong Ahn054b19a2018-10-19 13:46:09 +0900996 }
997
Paul Duffind11e78e2020-05-15 20:37:11 +0100998 if module.requiresRuntimeImplementationLibrary() {
Paul Duffin9d582cc2020-05-16 15:52:12 +0100999 // Add dependency to the rule for generating the implementation library.
1000 ctx.AddDependency(module, implLibraryTag, module.implLibraryModuleName())
1001
Paul Duffind11e78e2020-05-15 20:37:11 +01001002 if module.sharedLibrary() {
1003 // Add dependency to the rule for generating the xml permissions file
Paul Duffinf642a312020-06-12 17:46:39 +01001004 ctx.AddDependency(module, xmlPermissionsFileTag, module.xmlPermissionsModuleName())
Paul Duffind11e78e2020-05-15 20:37:11 +01001005 }
Paul Duffine74ac732020-02-06 13:51:46 +00001006
Paul Duffind11e78e2020-05-15 20:37:11 +01001007 // Only add the deps for the library if it is actually going to be built.
1008 module.Library.deps(ctx)
1009 }
Jiyong Parkc678ad32018-04-10 13:07:10 +09001010}
1011
Paul Duffin46fdda82020-05-14 15:39:10 +01001012func (module *SdkLibrary) OutputFiles(tag string) (android.Paths, error) {
1013 paths, err := module.commonOutputFiles(tag)
1014 if paths == nil && err == nil {
1015 return module.Library.OutputFiles(tag)
1016 } else {
1017 return paths, err
1018 }
1019}
1020
Inseob Kimc0907f12019-02-08 21:00:45 +09001021func (module *SdkLibrary) GenerateAndroidBuildActions(ctx android.ModuleContext) {
Paul Duffind11e78e2020-05-15 20:37:11 +01001022 // Only build an implementation library if required.
1023 if module.requiresRuntimeImplementationLibrary() {
Paul Duffin43db9be2019-12-30 17:35:49 +00001024 module.Library.GenerateAndroidBuildActions(ctx)
1025 }
Sundong Ahn054b19a2018-10-19 13:46:09 +09001026
Sundong Ahn57368eb2018-07-06 11:20:23 +09001027 // Record the paths to the header jars of the library (stubs and impl).
Paul Duffind1b3a922020-01-22 11:57:20 +00001028 // When this java_sdk_library is depended upon from others via "libs" property,
Jiyong Parkc678ad32018-04-10 13:07:10 +09001029 // the recorded paths will be returned depending on the link type of the caller.
1030 ctx.VisitDirectDeps(func(to android.Module) {
Jiyong Parkc678ad32018-04-10 13:07:10 +09001031 tag := ctx.OtherModuleDependencyTag(to)
1032
Paul Duffin5fb82132020-04-29 20:45:27 +01001033 // Extract information from any of the scope specific dependencies.
1034 if scopeTag, ok := tag.(scopeDependencyTag); ok {
1035 apiScope := scopeTag.apiScope
Paul Duffin5ae30792020-05-20 11:52:25 +01001036 scopePaths := module.getScopePathsCreateIfNeeded(apiScope)
Paul Duffin5fb82132020-04-29 20:45:27 +01001037
1038 // Extract information from the dependency. The exact information extracted
1039 // is determined by the nature of the dependency which is determined by the tag.
1040 scopeTag.extractDepInfo(ctx, to, scopePaths)
Sundong Ahn20e998b2018-07-24 11:19:26 +09001041 }
Jiyong Parkc678ad32018-04-10 13:07:10 +09001042 })
1043}
1044
Jiyong Park0b0e1b92019-12-03 13:24:29 +09001045func (module *SdkLibrary) AndroidMkEntries() []android.AndroidMkEntries {
Paul Duffind11e78e2020-05-15 20:37:11 +01001046 if !module.requiresRuntimeImplementationLibrary() {
Paul Duffin43db9be2019-12-30 17:35:49 +00001047 return nil
1048 }
Jiyong Park0b0e1b92019-12-03 13:24:29 +09001049 entriesList := module.Library.AndroidMkEntries()
1050 entries := &entriesList[0]
Paul Duffinf642a312020-06-12 17:46:39 +01001051 entries.Required = append(entries.Required, module.xmlPermissionsModuleName())
Jiyong Park0b0e1b92019-12-03 13:24:29 +09001052 return entriesList
Jiyong Park82484c02018-04-23 21:41:26 +09001053}
1054
Anton Hansson6bb88102020-03-27 19:43:19 +00001055// The dist path of the stub artifacts
1056func (module *SdkLibrary) apiDistPath(apiScope *apiScope) string {
1057 if module.ModuleBase.Owner() != "" {
1058 return path.Join("apistubs", module.ModuleBase.Owner(), apiScope.name)
1059 } else if Bool(module.sdkLibraryProperties.Core_lib) {
1060 return path.Join("apistubs", "core", apiScope.name)
1061 } else {
1062 return path.Join("apistubs", "android", apiScope.name)
1063 }
1064}
1065
Paul Duffin12ceb462019-12-24 20:31:31 +00001066// Get the sdk version for use when compiling the stubs library.
Paul Duffin153501f2020-05-12 15:52:55 +01001067func (module *SdkLibrary) sdkVersionForStubsLibrary(mctx android.EarlyModuleContext, apiScope *apiScope) string {
Paul Duffin080f5ee2020-05-12 11:50:28 +01001068 scopeProperties := module.scopeToProperties[apiScope]
1069 if scopeProperties.Sdk_version != nil {
1070 return proptools.String(scopeProperties.Sdk_version)
1071 }
1072
Paul Duffin12ceb462019-12-24 20:31:31 +00001073 sdkDep := decodeSdkDep(mctx, sdkContext(&module.Library))
1074 if sdkDep.hasStandardLibs() {
1075 // If building against a standard sdk then use the sdk version appropriate for the scope.
Paul Duffind1b3a922020-01-22 11:57:20 +00001076 return apiScope.sdkVersion
Paul Duffin12ceb462019-12-24 20:31:31 +00001077 } else {
1078 // Otherwise, use no system module.
1079 return "none"
1080 }
1081}
1082
Paul Duffind1b3a922020-01-22 11:57:20 +00001083func (module *SdkLibrary) latestApiFilegroupName(apiScope *apiScope) string {
1084 return ":" + module.BaseModuleName() + ".api." + apiScope.name + ".latest"
Jiyong Park58c518b2018-05-12 22:29:12 +09001085}
Jiyong Parkc678ad32018-04-10 13:07:10 +09001086
Paul Duffind1b3a922020-01-22 11:57:20 +00001087func (module *SdkLibrary) latestRemovedApiFilegroupName(apiScope *apiScope) string {
1088 return ":" + module.BaseModuleName() + "-removed.api." + apiScope.name + ".latest"
Jiyong Parkc678ad32018-04-10 13:07:10 +09001089}
1090
Anton Hansson97f83c12020-08-19 11:40:22 +01001091func childModuleVisibility(childVisibility []string) []string {
1092 if childVisibility == nil {
1093 // No child visibility set. The child will use the visibility of the sdk_library.
1094 return nil
1095 }
1096
1097 // Prepend an override to ignore the sdk_library's visibility, and rely on the child visibility.
1098 var visibility []string
1099 visibility = append(visibility, "//visibility:override")
1100 visibility = append(visibility, childVisibility...)
1101 return visibility
1102}
1103
Paul Duffin9d582cc2020-05-16 15:52:12 +01001104// Creates the implementation java library
1105func (module *SdkLibrary) createImplLibrary(mctx android.DefaultableHookContext) {
Paul Duffinc4422102020-06-24 16:22:38 +01001106 moduleNamePtr := proptools.StringPtr(module.BaseModuleName())
1107
Anton Hansson97f83c12020-08-19 11:40:22 +01001108 visibility := childModuleVisibility(module.sdkLibraryProperties.Impl_library_visibility)
1109
Paul Duffin9d582cc2020-05-16 15:52:12 +01001110 props := struct {
Paul Duffinc4422102020-06-24 16:22:38 +01001111 Name *string
1112 Visibility []string
1113 Instrument bool
1114 ConfigurationName *string
Paul Duffin9d582cc2020-05-16 15:52:12 +01001115 }{
1116 Name: proptools.StringPtr(module.implLibraryModuleName()),
Anton Hansson97f83c12020-08-19 11:40:22 +01001117 Visibility: visibility,
Paul Duffin49d3a522020-06-18 21:09:55 +01001118 // Set the instrument property to ensure it is instrumented when instrumentation is required.
1119 Instrument: true,
Paul Duffinc4422102020-06-24 16:22:38 +01001120
1121 // Make the created library behave as if it had the same name as this module.
1122 ConfigurationName: moduleNamePtr,
Paul Duffin9d582cc2020-05-16 15:52:12 +01001123 }
1124
1125 properties := []interface{}{
1126 &module.properties,
1127 &module.protoProperties,
1128 &module.deviceProperties,
Liz Kammer7727edc2020-07-09 15:16:41 -07001129 &module.dexProperties,
Paul Duffin9d582cc2020-05-16 15:52:12 +01001130 &module.dexpreoptProperties,
Colin Cross1e28e3c2020-06-02 20:09:13 -07001131 &module.linter.properties,
Paul Duffin9d582cc2020-05-16 15:52:12 +01001132 &props,
1133 module.sdkComponentPropertiesForChildLibrary(),
1134 }
1135 mctx.CreateModule(LibraryFactory, properties...)
1136}
1137
Jiyong Parkc678ad32018-04-10 13:07:10 +09001138// Creates a static java library that has API stubs
Paul Duffin2aaef532020-04-29 16:47:28 +01001139func (module *SdkLibrary) createStubsLibrary(mctx android.DefaultableHookContext, apiScope *apiScope) {
Jiyong Parkc678ad32018-04-10 13:07:10 +09001140 props := struct {
Paul Duffind41712d2020-05-16 09:57:59 +01001141 Name *string
1142 Visibility []string
1143 Srcs []string
1144 Installable *bool
1145 Sdk_version *string
1146 System_modules *string
1147 Patch_module *string
1148 Libs []string
1149 Compile_dex *bool
1150 Java_version *string
1151 Product_variables struct {
Jiyong Park82484c02018-04-23 21:41:26 +09001152 Pdk struct {
1153 Enabled *bool
1154 }
Jiyong Parkc678ad32018-04-10 13:07:10 +09001155 }
Sundong Ahn054b19a2018-10-19 13:46:09 +09001156 Openjdk9 struct {
1157 Srcs []string
1158 Javacflags []string
1159 }
Anton Hansson6bb88102020-03-27 19:43:19 +00001160 Dist struct {
1161 Targets []string
1162 Dest *string
1163 Dir *string
1164 Tag *string
1165 }
Jiyong Parkc678ad32018-04-10 13:07:10 +09001166 }{}
1167
Paul Duffinb74ee3e2020-05-08 14:16:20 +01001168 props.Name = proptools.StringPtr(module.stubsLibraryModuleName(apiScope))
Anton Hansson97f83c12020-08-19 11:40:22 +01001169 props.Visibility = childModuleVisibility(module.sdkLibraryProperties.Stubs_library_visibility)
Jiyong Parkc678ad32018-04-10 13:07:10 +09001170 // sources are generated from the droiddoc
Paul Duffinb74ee3e2020-05-08 14:16:20 +01001171 props.Srcs = []string{":" + module.stubsSourceModuleName(apiScope)}
Paul Duffin12ceb462019-12-24 20:31:31 +00001172 sdkVersion := module.sdkVersionForStubsLibrary(mctx, apiScope)
Paul Duffin52d398a2019-06-11 12:31:14 +01001173 props.Sdk_version = proptools.StringPtr(sdkVersion)
Paul Duffinc5d954a2020-05-16 18:54:24 +01001174 props.System_modules = module.deviceProperties.System_modules
1175 props.Patch_module = module.properties.Patch_module
Paul Duffin367ab912019-12-23 19:40:36 +00001176 props.Installable = proptools.BoolPtr(false)
Sundong Ahn054b19a2018-10-19 13:46:09 +09001177 props.Libs = module.sdkLibraryProperties.Stub_only_libs
Paul Duffin2ce1e812020-05-20 19:35:27 +01001178 // The stub-annotations library contains special versions of the annotations
1179 // with CLASS retention policy, so that they're kept.
1180 if proptools.Bool(module.sdkLibraryProperties.Annotations_enabled) {
1181 props.Libs = append(props.Libs, "stub-annotations")
1182 }
Jiyong Park82484c02018-04-23 21:41:26 +09001183 props.Product_variables.Pdk.Enabled = proptools.BoolPtr(false)
Paul Duffinc5d954a2020-05-16 18:54:24 +01001184 props.Openjdk9.Srcs = module.properties.Openjdk9.Srcs
1185 props.Openjdk9.Javacflags = module.properties.Openjdk9.Javacflags
Anton Hanssoncf4dd4c2020-05-21 09:21:57 +01001186 // We compile the stubs for 1.8 in line with the main android.jar stubs, and potential
1187 // interop with older developer tools that don't support 1.9.
1188 props.Java_version = proptools.StringPtr("1.8")
Liz Kammer7727edc2020-07-09 15:16:41 -07001189 if module.dexProperties.Compile_dex != nil {
1190 props.Compile_dex = module.dexProperties.Compile_dex
Sundong Ahndd567f92018-07-31 17:19:11 +09001191 }
Jiyong Parkc678ad32018-04-10 13:07:10 +09001192
Anton Hansson6bb88102020-03-27 19:43:19 +00001193 // Dist the class jar artifact for sdk builds.
1194 if !Bool(module.sdkLibraryProperties.No_dist) {
1195 props.Dist.Targets = []string{"sdk", "win_sdk"}
1196 props.Dist.Dest = proptools.StringPtr(fmt.Sprintf("%v.jar", module.BaseModuleName()))
1197 props.Dist.Dir = proptools.StringPtr(module.apiDistPath(apiScope))
1198 props.Dist.Tag = proptools.StringPtr(".jar")
1199 }
Jiyong Parkc678ad32018-04-10 13:07:10 +09001200
Paul Duffin64e61992020-05-15 10:20:31 +01001201 mctx.CreateModule(LibraryFactory, &props, module.sdkComponentPropertiesForChildLibrary())
Jiyong Parkc678ad32018-04-10 13:07:10 +09001202}
1203
Paul Duffincbcfcaa2020-04-07 18:49:53 +01001204// Creates a droidstubs module that creates stubs source files from the given full source
Paul Duffin5fb82132020-04-29 20:45:27 +01001205// files and also updates and checks the API specification files.
Paul Duffina377e4c2020-04-29 13:30:54 +01001206func (module *SdkLibrary) createStubsSourcesAndApi(mctx android.DefaultableHookContext, apiScope *apiScope, name string, createStubSources, createApi bool, scopeSpecificDroidstubsArgs []string) {
Jiyong Parkc678ad32018-04-10 13:07:10 +09001207 props := struct {
Sundong Ahn054b19a2018-10-19 13:46:09 +09001208 Name *string
Paul Duffin344c4ee2020-04-29 23:35:13 +01001209 Visibility []string
Sundong Ahn054b19a2018-10-19 13:46:09 +09001210 Srcs []string
1211 Installable *bool
Paul Duffin52d398a2019-06-11 12:31:14 +01001212 Sdk_version *string
Paul Duffin12ceb462019-12-24 20:31:31 +00001213 System_modules *string
Sundong Ahn054b19a2018-10-19 13:46:09 +09001214 Libs []string
Paul Duffin11512472019-02-11 15:55:17 +00001215 Arg_files []string
Sundong Ahn054b19a2018-10-19 13:46:09 +09001216 Args *string
Sundong Ahn054b19a2018-10-19 13:46:09 +09001217 Java_version *string
Paul Duffin2ce1e812020-05-20 19:35:27 +01001218 Annotations_enabled *bool
Sundong Ahn054b19a2018-10-19 13:46:09 +09001219 Merge_annotations_dirs []string
1220 Merge_inclusion_annotations_dirs []string
Paul Duffina377e4c2020-04-29 13:30:54 +01001221 Generate_stubs *bool
Sundong Ahn054b19a2018-10-19 13:46:09 +09001222 Check_api struct {
Inseob Kim38449af2019-02-28 14:24:05 +09001223 Current ApiToCheck
1224 Last_released ApiToCheck
1225 Ignore_missing_latest_api *bool
Paul Duffin8986cc92020-05-10 19:32:20 +01001226
1227 Api_lint struct {
1228 Enabled *bool
1229 New_since *string
1230 Baseline_file *string
1231 }
Jiyong Park58c518b2018-05-12 22:29:12 +09001232 }
Sundong Ahn1b92c822018-05-29 11:35:17 +09001233 Aidl struct {
1234 Include_dirs []string
1235 Local_include_dirs []string
1236 }
Anton Hansson6bb88102020-03-27 19:43:19 +00001237 Dist struct {
1238 Targets []string
1239 Dest *string
1240 Dir *string
1241 }
Jiyong Parkc678ad32018-04-10 13:07:10 +09001242 }{}
1243
Paul Duffinda364252020-04-28 14:08:32 +01001244 // The stubs source processing uses the same compile time classpath when extracting the
1245 // API from the implementation library as it does when compiling it. i.e. the same
1246 // * sdk version
1247 // * system_modules
1248 // * libs (static_libs/libs)
Paul Duffin250e6192019-06-07 10:44:37 +01001249
Paul Duffina377e4c2020-04-29 13:30:54 +01001250 props.Name = proptools.StringPtr(name)
Anton Hansson97f83c12020-08-19 11:40:22 +01001251 props.Visibility = childModuleVisibility(module.sdkLibraryProperties.Stubs_source_visibility)
Paul Duffinc5d954a2020-05-16 18:54:24 +01001252 props.Srcs = append(props.Srcs, module.properties.Srcs...)
1253 props.Sdk_version = module.deviceProperties.Sdk_version
1254 props.System_modules = module.deviceProperties.System_modules
Jiyong Parkc678ad32018-04-10 13:07:10 +09001255 props.Installable = proptools.BoolPtr(false)
Sundong Ahne6f0b052018-06-05 16:46:14 +09001256 // A droiddoc module has only one Libs property and doesn't distinguish between
1257 // shared libs and static libs. So we need to add both of these libs to Libs property.
Paul Duffinc5d954a2020-05-16 18:54:24 +01001258 props.Libs = module.properties.Libs
1259 props.Libs = append(props.Libs, module.properties.Static_libs...)
1260 props.Aidl.Include_dirs = module.deviceProperties.Aidl.Include_dirs
1261 props.Aidl.Local_include_dirs = module.deviceProperties.Aidl.Local_include_dirs
1262 props.Java_version = module.properties.Java_version
Jiyong Parkc678ad32018-04-10 13:07:10 +09001263
Paul Duffin2ce1e812020-05-20 19:35:27 +01001264 props.Annotations_enabled = module.sdkLibraryProperties.Annotations_enabled
Sundong Ahn054b19a2018-10-19 13:46:09 +09001265 props.Merge_annotations_dirs = module.sdkLibraryProperties.Merge_annotations_dirs
1266 props.Merge_inclusion_annotations_dirs = module.sdkLibraryProperties.Merge_inclusion_annotations_dirs
1267
Paul Duffincbcfcaa2020-04-07 18:49:53 +01001268 droidstubsArgs := []string{}
Paul Duffin235ffff2019-12-24 10:41:30 +00001269 if len(module.sdkLibraryProperties.Api_packages) != 0 {
Paul Duffincbcfcaa2020-04-07 18:49:53 +01001270 droidstubsArgs = append(droidstubsArgs, "--stub-packages "+strings.Join(module.sdkLibraryProperties.Api_packages, ":"))
Paul Duffin235ffff2019-12-24 10:41:30 +00001271 }
1272 if len(module.sdkLibraryProperties.Hidden_api_packages) != 0 {
Paul Duffincbcfcaa2020-04-07 18:49:53 +01001273 droidstubsArgs = append(droidstubsArgs,
Paul Duffin235ffff2019-12-24 10:41:30 +00001274 android.JoinWithPrefix(module.sdkLibraryProperties.Hidden_api_packages, " --hide-package "))
1275 }
Paul Duffincbcfcaa2020-04-07 18:49:53 +01001276 droidstubsArgs = append(droidstubsArgs, module.sdkLibraryProperties.Droiddoc_options...)
Paul Duffin235ffff2019-12-24 10:41:30 +00001277 disabledWarnings := []string{
1278 "MissingPermission",
1279 "BroadcastBehavior",
1280 "HiddenSuperclass",
1281 "DeprecationMismatch",
1282 "UnavailableSymbol",
1283 "SdkConstant",
1284 "HiddenTypeParameter",
1285 "Todo",
1286 "Typo",
1287 }
Paul Duffincbcfcaa2020-04-07 18:49:53 +01001288 droidstubsArgs = append(droidstubsArgs, android.JoinWithPrefix(disabledWarnings, "--hide "))
Sundong Ahnfb2721f2018-09-17 13:23:09 +09001289
Paul Duffina377e4c2020-04-29 13:30:54 +01001290 if !createStubSources {
1291 // Stubs are not required.
1292 props.Generate_stubs = proptools.BoolPtr(false)
1293 }
1294
Paul Duffin3c7c3472020-04-07 18:50:10 +01001295 // Add in scope specific arguments.
Paul Duffina377e4c2020-04-29 13:30:54 +01001296 droidstubsArgs = append(droidstubsArgs, scopeSpecificDroidstubsArgs...)
Paul Duffin11512472019-02-11 15:55:17 +00001297 props.Arg_files = module.sdkLibraryProperties.Droiddoc_option_files
Paul Duffincbcfcaa2020-04-07 18:49:53 +01001298 props.Args = proptools.StringPtr(strings.Join(droidstubsArgs, " "))
Jiyong Parkc678ad32018-04-10 13:07:10 +09001299
Paul Duffina377e4c2020-04-29 13:30:54 +01001300 if createApi {
1301 // List of APIs identified from the provided source files are created. They are later
1302 // compared against to the not-yet-released (a.k.a current) list of APIs and to the
1303 // last-released (a.k.a numbered) list of API.
1304 currentApiFileName := apiScope.apiFilePrefix + "current.txt"
1305 removedApiFileName := apiScope.apiFilePrefix + "removed.txt"
1306 apiDir := module.getApiDir()
1307 currentApiFileName = path.Join(apiDir, currentApiFileName)
1308 removedApiFileName = path.Join(apiDir, removedApiFileName)
Jiyong Parkc678ad32018-04-10 13:07:10 +09001309
Paul Duffina377e4c2020-04-29 13:30:54 +01001310 // check against the not-yet-release API
1311 props.Check_api.Current.Api_file = proptools.StringPtr(currentApiFileName)
1312 props.Check_api.Current.Removed_api_file = proptools.StringPtr(removedApiFileName)
Jiyong Park58c518b2018-05-12 22:29:12 +09001313
Paul Duffina377e4c2020-04-29 13:30:54 +01001314 if !apiScope.unstable {
1315 // check against the latest released API
1316 latestApiFilegroupName := proptools.StringPtr(module.latestApiFilegroupName(apiScope))
1317 props.Check_api.Last_released.Api_file = latestApiFilegroupName
1318 props.Check_api.Last_released.Removed_api_file = proptools.StringPtr(
1319 module.latestRemovedApiFilegroupName(apiScope))
1320 props.Check_api.Ignore_missing_latest_api = proptools.BoolPtr(true)
Paul Duffin8986cc92020-05-10 19:32:20 +01001321
Paul Duffina377e4c2020-04-29 13:30:54 +01001322 if proptools.Bool(module.sdkLibraryProperties.Api_lint.Enabled) {
1323 // Enable api lint.
1324 props.Check_api.Api_lint.Enabled = proptools.BoolPtr(true)
1325 props.Check_api.Api_lint.New_since = latestApiFilegroupName
Paul Duffin8986cc92020-05-10 19:32:20 +01001326
Paul Duffina377e4c2020-04-29 13:30:54 +01001327 // If it exists then pass a lint-baseline.txt through to droidstubs.
1328 baselinePath := path.Join(apiDir, apiScope.apiFilePrefix+"lint-baseline.txt")
1329 baselinePathRelativeToRoot := path.Join(mctx.ModuleDir(), baselinePath)
1330 paths, err := mctx.GlobWithDeps(baselinePathRelativeToRoot, nil)
1331 if err != nil {
1332 mctx.ModuleErrorf("error checking for presence of %s: %s", baselinePathRelativeToRoot, err)
1333 }
1334 if len(paths) == 1 {
1335 props.Check_api.Api_lint.Baseline_file = proptools.StringPtr(baselinePath)
1336 } else if len(paths) != 0 {
1337 mctx.ModuleErrorf("error checking for presence of %s: expected one path, found: %v", baselinePathRelativeToRoot, paths)
1338 }
Paul Duffin8986cc92020-05-10 19:32:20 +01001339 }
1340 }
Jiyong Park58c518b2018-05-12 22:29:12 +09001341
Paul Duffina377e4c2020-04-29 13:30:54 +01001342 // Dist the api txt artifact for sdk builds.
1343 if !Bool(module.sdkLibraryProperties.No_dist) {
1344 props.Dist.Targets = []string{"sdk", "win_sdk"}
1345 props.Dist.Dest = proptools.StringPtr(fmt.Sprintf("%v.txt", module.BaseModuleName()))
1346 props.Dist.Dir = proptools.StringPtr(path.Join(module.apiDistPath(apiScope), "api"))
1347 }
Anton Hansson6bb88102020-03-27 19:43:19 +00001348 }
1349
Colin Cross84dfc3d2019-09-25 11:33:01 -07001350 mctx.CreateModule(DroidstubsFactory, &props)
Jiyong Parkc678ad32018-04-10 13:07:10 +09001351}
1352
Jooyung Hanb8fa86a2020-03-10 06:23:13 +09001353func (module *SdkLibrary) DepIsInSameApex(mctx android.BaseModuleContext, dep android.Module) bool {
1354 depTag := mctx.OtherModuleDependencyTag(dep)
1355 if depTag == xmlPermissionsFileTag {
1356 return true
1357 }
1358 return module.Library.DepIsInSameApex(mctx, dep)
1359}
1360
Jiyong Parkc678ad32018-04-10 13:07:10 +09001361// Creates the xml file that publicizes the runtime library
Paul Duffin2aaef532020-04-29 16:47:28 +01001362func (module *SdkLibrary) createXmlFile(mctx android.DefaultableHookContext) {
Jiyong Parke3833882020-02-17 17:28:10 +09001363 props := struct {
Paul Duffind41712d2020-05-16 09:57:59 +01001364 Name *string
1365 Lib_name *string
1366 Apex_available []string
Jiyong Parke3833882020-02-17 17:28:10 +09001367 }{
Paul Duffinf642a312020-06-12 17:46:39 +01001368 Name: proptools.StringPtr(module.xmlPermissionsModuleName()),
Jooyung Hanb8fa86a2020-03-10 06:23:13 +09001369 Lib_name: proptools.StringPtr(module.BaseModuleName()),
1370 Apex_available: module.ApexProperties.Apex_available,
Jiyong Parkc678ad32018-04-10 13:07:10 +09001371 }
Jiyong Parke3833882020-02-17 17:28:10 +09001372
Jiyong Parke3833882020-02-17 17:28:10 +09001373 mctx.CreateModule(sdkLibraryXmlFactory, &props)
Jiyong Parkc678ad32018-04-10 13:07:10 +09001374}
1375
Paul Duffin50061512020-01-21 16:31:05 +00001376func PrebuiltJars(ctx android.BaseModuleContext, baseName string, s sdkSpec) android.Paths {
Jiyong Park6a927c42020-01-21 02:03:43 +09001377 var ver sdkVersion
1378 var kind sdkKind
1379 if s.usePrebuilt(ctx) {
1380 ver = s.version
1381 kind = s.kind
Jiyong Parkc678ad32018-04-10 13:07:10 +09001382 } else {
Jiyong Park6a927c42020-01-21 02:03:43 +09001383 // We don't have prebuilt SDK for the specific sdkVersion.
1384 // Instead of breaking the build, fallback to use "system_current"
1385 ver = sdkVersionCurrent
1386 kind = sdkSystem
Sundong Ahn054b19a2018-10-19 13:46:09 +09001387 }
Jiyong Park6a927c42020-01-21 02:03:43 +09001388
1389 dir := filepath.Join("prebuilts", "sdk", ver.String(), kind.String())
Paul Duffin50061512020-01-21 16:31:05 +00001390 jar := filepath.Join(dir, baseName+".jar")
Sundong Ahn054b19a2018-10-19 13:46:09 +09001391 jarPath := android.ExistentPathForSource(ctx, jar)
Sundong Ahnae418ac2019-02-28 15:01:28 +09001392 if !jarPath.Valid() {
Colin Cross07c88562020-01-07 09:34:44 -08001393 if ctx.Config().AllowMissingDependencies() {
1394 return android.Paths{android.PathForSource(ctx, jar)}
1395 } else {
Jiyong Park6a927c42020-01-21 02:03:43 +09001396 ctx.PropertyErrorf("sdk_library", "invalid sdk version %q, %q does not exist", s.raw, jar)
Colin Cross07c88562020-01-07 09:34:44 -08001397 }
Sundong Ahnae418ac2019-02-28 15:01:28 +09001398 return nil
1399 }
Sundong Ahn054b19a2018-10-19 13:46:09 +09001400 return android.Paths{jarPath.Path()}
1401}
1402
Paul Duffinbf19a972020-05-26 13:21:35 +01001403// Get the apex name for module, "" if it is for platform.
1404func getApexNameForModule(module android.Module) string {
1405 if apex, ok := module.(android.ApexModule); ok {
1406 return apex.ApexName()
1407 }
1408
1409 return ""
1410}
1411
1412// Check to see if the other module is within the same named APEX as this module.
1413//
1414// If either this or the other module are on the platform then this will return
1415// false.
Paul Duffinf642a312020-06-12 17:46:39 +01001416func withinSameApexAs(module android.ApexModule, other android.Module) bool {
Paul Duffinbf19a972020-05-26 13:21:35 +01001417 name := module.ApexName()
1418 return name != "" && getApexNameForModule(other) == name
1419}
1420
Paul Duffin47624362020-05-20 12:19:10 +01001421func (module *SdkLibrary) sdkJars(ctx android.BaseModuleContext, sdkVersion sdkSpec, headerJars bool) android.Paths {
Jiyong Park27fc4142020-05-28 00:19:53 +09001422 // If the client doesn't set sdk_version, but if this library prefers stubs over
1423 // the impl library, let's provide the widest API surface possible. To do so,
1424 // force override sdk_version to module_current so that the closest possible API
1425 // surface could be found in selectHeaderJarsForSdkVersion
1426 if module.defaultsToStubs() && !sdkVersion.specified() {
1427 sdkVersion = sdkSpecFrom("module_current")
1428 }
Paul Duffind1b3a922020-01-22 11:57:20 +00001429
Paul Duffin2e7ed652020-05-26 18:13:57 +01001430 // Only provide access to the implementation library if it is actually built.
1431 if module.requiresRuntimeImplementationLibrary() {
1432 // Check any special cases for java_sdk_library.
1433 //
1434 // Only allow access to the implementation library in the following condition:
1435 // * No sdk_version specified on the referencing module.
Paul Duffinbf19a972020-05-26 13:21:35 +01001436 // * The referencing module is in the same apex as this.
Paul Duffinf642a312020-06-12 17:46:39 +01001437 if sdkVersion.kind == sdkPrivate || withinSameApexAs(module, ctx.Module()) {
Paul Duffin2e7ed652020-05-26 18:13:57 +01001438 if headerJars {
1439 return module.HeaderJars()
1440 } else {
1441 return module.ImplementationJars()
1442 }
Sundong Ahn054b19a2018-10-19 13:46:09 +09001443 }
Jiyong Parkc678ad32018-04-10 13:07:10 +09001444 }
Paul Duffin47624362020-05-20 12:19:10 +01001445
Paul Duffina3fb67d2020-05-20 14:20:02 +01001446 return module.selectHeaderJarsForSdkVersion(ctx, sdkVersion)
Jiyong Parkc678ad32018-04-10 13:07:10 +09001447}
1448
Sundong Ahn241cd372018-07-13 16:16:44 +09001449// to satisfy SdkLibraryDependency interface
Paul Duffind1b3a922020-01-22 11:57:20 +00001450func (module *SdkLibrary) SdkHeaderJars(ctx android.BaseModuleContext, sdkVersion sdkSpec) android.Paths {
1451 return module.sdkJars(ctx, sdkVersion, true /*headerJars*/)
1452}
1453
1454// to satisfy SdkLibraryDependency interface
Jiyong Park6a927c42020-01-21 02:03:43 +09001455func (module *SdkLibrary) SdkImplementationJars(ctx android.BaseModuleContext, sdkVersion sdkSpec) android.Paths {
Paul Duffind1b3a922020-01-22 11:57:20 +00001456 return module.sdkJars(ctx, sdkVersion, false /*headerJars*/)
Sundong Ahn241cd372018-07-13 16:16:44 +09001457}
1458
Sundong Ahn80a87b32019-05-13 15:02:50 +09001459func (module *SdkLibrary) SetNoDist() {
1460 module.sdkLibraryProperties.No_dist = proptools.BoolPtr(true)
1461}
1462
Colin Cross571cccf2019-02-04 11:22:08 -08001463var javaSdkLibrariesKey = android.NewOnceKey("javaSdkLibraries")
1464
Jiyong Park82484c02018-04-23 21:41:26 +09001465func javaSdkLibraries(config android.Config) *[]string {
Colin Cross571cccf2019-02-04 11:22:08 -08001466 return config.Once(javaSdkLibrariesKey, func() interface{} {
Jiyong Park82484c02018-04-23 21:41:26 +09001467 return &[]string{}
1468 }).(*[]string)
1469}
1470
Paul Duffin749f98f2019-12-30 17:23:46 +00001471func (module *SdkLibrary) getApiDir() string {
1472 return proptools.StringDefault(module.sdkLibraryProperties.Api_dir, "api")
1473}
1474
Jiyong Parkc678ad32018-04-10 13:07:10 +09001475// For a java_sdk_library module, create internal modules for stubs, docs,
1476// runtime libs and xml file. If requested, the stubs and docs are created twice
1477// once for public API level and once for system API level
Paul Duffin2aaef532020-04-29 16:47:28 +01001478func (module *SdkLibrary) CreateInternalModules(mctx android.DefaultableHookContext) {
1479 // If the module has been disabled then don't create any child modules.
1480 if !module.Enabled() {
1481 return
1482 }
1483
Paul Duffinc5d954a2020-05-16 18:54:24 +01001484 if len(module.properties.Srcs) == 0 {
Inseob Kimc0907f12019-02-08 21:00:45 +09001485 mctx.PropertyErrorf("srcs", "java_sdk_library must specify srcs")
Jooyung Han58f26ab2019-12-18 15:34:32 +09001486 return
Inseob Kimc0907f12019-02-08 21:00:45 +09001487 }
1488
Paul Duffin37e0b772019-12-30 17:20:10 +00001489 // If this builds against standard libraries (i.e. is not part of the core libraries)
1490 // then assume it provides both system and test apis. Otherwise, assume it does not and
1491 // also assume it does not contribute to the dist build.
1492 sdkDep := decodeSdkDep(mctx, sdkContext(&module.Library))
1493 hasSystemAndTestApis := sdkDep.hasStandardLibs()
Paul Duffin3a254982020-04-28 10:44:03 +01001494 module.sdkLibraryProperties.Generate_system_and_test_apis = hasSystemAndTestApis
Paul Duffin37e0b772019-12-30 17:20:10 +00001495 module.sdkLibraryProperties.No_dist = proptools.BoolPtr(!hasSystemAndTestApis)
1496
Inseob Kim8098faa2019-03-18 10:19:51 +09001497 missing_current_api := false
1498
Paul Duffin3a254982020-04-28 10:44:03 +01001499 generatedScopes := module.getGeneratedApiScopes(mctx)
Paul Duffind1b3a922020-01-22 11:57:20 +00001500
Paul Duffin749f98f2019-12-30 17:23:46 +00001501 apiDir := module.getApiDir()
Paul Duffin3a254982020-04-28 10:44:03 +01001502 for _, scope := range generatedScopes {
Inseob Kim8098faa2019-03-18 10:19:51 +09001503 for _, api := range []string{"current.txt", "removed.txt"} {
Paul Duffind1b3a922020-01-22 11:57:20 +00001504 path := path.Join(mctx.ModuleDir(), apiDir, scope.apiFilePrefix+api)
Inseob Kim8098faa2019-03-18 10:19:51 +09001505 p := android.ExistentPathForSource(mctx, path)
1506 if !p.Valid() {
1507 mctx.ModuleErrorf("Current api file %#v doesn't exist", path)
1508 missing_current_api = true
1509 }
1510 }
1511 }
1512
1513 if missing_current_api {
1514 script := "build/soong/scripts/gen-java-current-api-files.sh"
1515 p := android.ExistentPathForSource(mctx, script)
1516
1517 if !p.Valid() {
1518 panic(fmt.Sprintf("script file %s doesn't exist", script))
1519 }
1520
1521 mctx.ModuleErrorf("One or more current api files are missing. "+
1522 "You can update them by:\n"+
Paul Duffin37e0b772019-12-30 17:20:10 +00001523 "%s %q %s && m update-api",
Paul Duffind1b3a922020-01-22 11:57:20 +00001524 script, filepath.Join(mctx.ModuleDir(), apiDir),
Paul Duffin3a254982020-04-28 10:44:03 +01001525 strings.Join(generatedScopes.Strings(func(s *apiScope) string { return s.apiFilePrefix }), " "))
Inseob Kim8098faa2019-03-18 10:19:51 +09001526 return
1527 }
1528
Paul Duffin3a254982020-04-28 10:44:03 +01001529 for _, scope := range generatedScopes {
Paul Duffina377e4c2020-04-29 13:30:54 +01001530 stubsSourceArgs := scope.droidstubsArgsForGeneratingStubsSource
Paul Duffinb74ee3e2020-05-08 14:16:20 +01001531 stubsSourceModuleName := module.stubsSourceModuleName(scope)
Paul Duffina377e4c2020-04-29 13:30:54 +01001532
1533 // If the args needed to generate the stubs and API are the same then they
1534 // can be generated in a single invocation of metalava, otherwise they will
1535 // need separate invocations.
1536 if scope.createStubsSourceAndApiTogether {
1537 // Use the stubs source name for legacy reasons.
1538 module.createStubsSourcesAndApi(mctx, scope, stubsSourceModuleName, true, true, stubsSourceArgs)
1539 } else {
1540 module.createStubsSourcesAndApi(mctx, scope, stubsSourceModuleName, true, false, stubsSourceArgs)
1541
1542 apiArgs := scope.droidstubsArgsForGeneratingApi
Paul Duffinb74ee3e2020-05-08 14:16:20 +01001543 apiName := module.apiModuleName(scope)
Paul Duffina377e4c2020-04-29 13:30:54 +01001544 module.createStubsSourcesAndApi(mctx, scope, apiName, false, true, apiArgs)
1545 }
1546
Paul Duffind1b3a922020-01-22 11:57:20 +00001547 module.createStubsLibrary(mctx, scope)
Inseob Kimc0907f12019-02-08 21:00:45 +09001548 }
1549
Paul Duffind11e78e2020-05-15 20:37:11 +01001550 if module.requiresRuntimeImplementationLibrary() {
Paul Duffin9d582cc2020-05-16 15:52:12 +01001551 // Create child module to create an implementation library.
1552 //
1553 // This temporarily creates a second implementation library that can be explicitly
1554 // referenced.
1555 //
1556 // TODO(b/156618935) - update comment once only one implementation library is created.
1557 module.createImplLibrary(mctx)
1558
Paul Duffind11e78e2020-05-15 20:37:11 +01001559 // Only create an XML permissions file that declares the library as being usable
1560 // as a shared library if required.
1561 if module.sharedLibrary() {
1562 module.createXmlFile(mctx)
1563 }
Paul Duffin43db9be2019-12-30 17:35:49 +00001564
1565 // record java_sdk_library modules so that they are exported to make
1566 javaSdkLibraries := javaSdkLibraries(mctx.Config())
1567 javaSdkLibrariesLock.Lock()
1568 defer javaSdkLibrariesLock.Unlock()
1569 *javaSdkLibraries = append(*javaSdkLibraries, module.BaseModuleName())
1570 }
Inseob Kimc0907f12019-02-08 21:00:45 +09001571}
1572
1573func (module *SdkLibrary) InitSdkLibraryProperties() {
Colin Cross1c14b4e2020-06-15 16:09:53 -07001574 module.addHostAndDeviceProperties()
1575 module.AddProperties(&module.sdkLibraryProperties)
Sundong Ahn054b19a2018-10-19 13:46:09 +09001576
Paul Duffin64e61992020-05-15 10:20:31 +01001577 module.initSdkLibraryComponent(&module.ModuleBase)
1578
Paul Duffinc5d954a2020-05-16 18:54:24 +01001579 module.properties.Installable = proptools.BoolPtr(true)
1580 module.deviceProperties.IsSDKLibrary = true
Inseob Kimc0907f12019-02-08 21:00:45 +09001581}
Sundong Ahn054b19a2018-10-19 13:46:09 +09001582
Paul Duffind11e78e2020-05-15 20:37:11 +01001583func (module *SdkLibrary) requiresRuntimeImplementationLibrary() bool {
1584 return !proptools.Bool(module.sdkLibraryProperties.Api_only)
1585}
1586
Jiyong Park27fc4142020-05-28 00:19:53 +09001587func (module *SdkLibrary) defaultsToStubs() bool {
1588 return proptools.Bool(module.sdkLibraryProperties.Default_to_stubs)
1589}
1590
Paul Duffin1a724e62020-05-08 13:44:43 +01001591// Defines how to name the individual component modules the sdk library creates.
1592type sdkLibraryComponentNamingScheme interface {
1593 stubsLibraryModuleName(scope *apiScope, baseName string) string
1594
1595 stubsSourceModuleName(scope *apiScope, baseName string) string
1596
1597 apiModuleName(scope *apiScope, baseName string) string
1598}
1599
1600type defaultNamingScheme struct {
1601}
1602
1603func (s *defaultNamingScheme) stubsLibraryModuleName(scope *apiScope, baseName string) string {
1604 return scope.stubsLibraryModuleName(baseName)
1605}
1606
1607func (s *defaultNamingScheme) stubsSourceModuleName(scope *apiScope, baseName string) string {
1608 return scope.stubsSourceModuleName(baseName)
1609}
1610
1611func (s *defaultNamingScheme) apiModuleName(scope *apiScope, baseName string) string {
1612 return scope.apiModuleName(baseName)
1613}
1614
1615var _ sdkLibraryComponentNamingScheme = (*defaultNamingScheme)(nil)
1616
Paul Duffindef8a892020-05-08 15:36:30 +01001617type frameworkModulesNamingScheme struct {
1618}
1619
1620func (s *frameworkModulesNamingScheme) moduleSuffix(scope *apiScope) string {
1621 suffix := scope.name
1622 if scope == apiScopeModuleLib {
1623 suffix = "module_libs_"
1624 }
1625 return suffix
1626}
1627
1628func (s *frameworkModulesNamingScheme) stubsLibraryModuleName(scope *apiScope, baseName string) string {
1629 return fmt.Sprintf("%s-stubs-%sapi", baseName, s.moduleSuffix(scope))
1630}
1631
1632func (s *frameworkModulesNamingScheme) stubsSourceModuleName(scope *apiScope, baseName string) string {
1633 return fmt.Sprintf("%s-stubs-srcs-%sapi", baseName, s.moduleSuffix(scope))
1634}
1635
1636func (s *frameworkModulesNamingScheme) apiModuleName(scope *apiScope, baseName string) string {
1637 return fmt.Sprintf("%s-api-%sapi", baseName, s.moduleSuffix(scope))
1638}
1639
1640var _ sdkLibraryComponentNamingScheme = (*frameworkModulesNamingScheme)(nil)
1641
Anton Hansson0bd88d02020-05-25 12:20:51 +01001642func moduleStubLinkType(name string) (stub bool, ret linkType) {
1643 // This suffix-based approach is fragile and could potentially mis-trigger.
1644 // TODO(b/155164730): Clean this up when modules no longer reference sdk_lib stubs directly.
1645 if strings.HasSuffix(name, ".stubs.public") || strings.HasSuffix(name, "-stubs-publicapi") {
1646 return true, javaSdk
1647 }
1648 if strings.HasSuffix(name, ".stubs.system") || strings.HasSuffix(name, "-stubs-systemapi") {
1649 return true, javaSystem
1650 }
1651 if strings.HasSuffix(name, ".stubs.module_lib") || strings.HasSuffix(name, "-stubs-module_libs_api") {
1652 return true, javaModule
1653 }
1654 if strings.HasSuffix(name, ".stubs.test") {
1655 return true, javaSystem
1656 }
1657 return false, javaPlatform
1658}
1659
Jaewoong Jung4f158ee2019-07-11 10:05:35 -07001660// java_sdk_library is a special Java library that provides optional platform APIs to apps.
1661// In practice, it can be viewed as a combination of several modules: 1) stubs library that clients
1662// are linked against to, 2) droiddoc module that internally generates API stubs source files,
1663// 3) the real runtime shared library that implements the APIs, and 4) XML file for adding
1664// the runtime lib to the classpath at runtime if requested via <uses-library>.
Inseob Kimc0907f12019-02-08 21:00:45 +09001665func SdkLibraryFactory() android.Module {
1666 module := &SdkLibrary{}
Paul Duffinb74ee3e2020-05-08 14:16:20 +01001667
1668 // Initialize information common between source and prebuilt.
1669 module.initCommon(&module.ModuleBase)
1670
Inseob Kimc0907f12019-02-08 21:00:45 +09001671 module.InitSdkLibraryProperties()
Jooyung Han58f26ab2019-12-18 15:34:32 +09001672 android.InitApexModule(module)
Sundong Ahn054b19a2018-10-19 13:46:09 +09001673 InitJavaModule(module, android.HostAndDeviceSupported)
Paul Duffin3a254982020-04-28 10:44:03 +01001674
1675 // Initialize the map from scope to scope specific properties.
1676 scopeToProperties := make(map[*apiScope]*ApiScopeProperties)
1677 for _, scope := range allApiScopes {
1678 scopeToProperties[scope] = scope.scopeSpecificProperties(module)
1679 }
1680 module.scopeToProperties = scopeToProperties
1681
Paul Duffin344c4ee2020-04-29 23:35:13 +01001682 // Add the properties containing visibility rules so that they are checked.
Paul Duffin9d582cc2020-05-16 15:52:12 +01001683 android.AddVisibilityProperty(module, "impl_library_visibility", &module.sdkLibraryProperties.Impl_library_visibility)
Paul Duffin344c4ee2020-04-29 23:35:13 +01001684 android.AddVisibilityProperty(module, "stubs_library_visibility", &module.sdkLibraryProperties.Stubs_library_visibility)
1685 android.AddVisibilityProperty(module, "stubs_source_visibility", &module.sdkLibraryProperties.Stubs_source_visibility)
1686
Paul Duffin1a724e62020-05-08 13:44:43 +01001687 module.SetDefaultableHook(func(ctx android.DefaultableHookContext) {
Paul Duffind11e78e2020-05-15 20:37:11 +01001688 // If no implementation is required then it cannot be used as a shared library
1689 // either.
1690 if !module.requiresRuntimeImplementationLibrary() {
1691 // If shared_library has been explicitly set to true then it is incompatible
1692 // with api_only: true.
1693 if proptools.Bool(module.commonSdkLibraryProperties.Shared_library) {
1694 ctx.PropertyErrorf("api_only/shared_library", "inconsistent settings, shared_library and api_only cannot both be true")
1695 }
1696 // Set shared_library: false.
1697 module.commonSdkLibraryProperties.Shared_library = proptools.BoolPtr(false)
1698 }
1699
Paul Duffin1a724e62020-05-08 13:44:43 +01001700 if module.initCommonAfterDefaultsApplied(ctx) {
1701 module.CreateInternalModules(ctx)
1702 }
1703 })
Jiyong Parkc678ad32018-04-10 13:07:10 +09001704 return module
1705}
Colin Cross79c7c262019-04-17 11:11:46 -07001706
1707//
1708// SDK library prebuilts
1709//
1710
Paul Duffin56d44902020-01-31 13:36:25 +00001711// Properties associated with each api scope.
1712type sdkLibraryScopeProperties struct {
Colin Cross79c7c262019-04-17 11:11:46 -07001713 Jars []string `android:"path"`
1714
1715 Sdk_version *string
1716
Colin Cross79c7c262019-04-17 11:11:46 -07001717 // List of shared java libs that this module has dependencies to
1718 Libs []string
Paul Duffinf488ef22020-04-09 00:10:17 +01001719
Paul Duffin5fb82132020-04-29 20:45:27 +01001720 // The stubs source.
Paul Duffinf488ef22020-04-09 00:10:17 +01001721 Stub_srcs []string `android:"path"`
Paul Duffin75dcc802020-04-09 01:08:11 +01001722
1723 // The current.txt
Paul Duffin533f9c72020-05-20 16:18:00 +01001724 Current_api *string `android:"path"`
Paul Duffin75dcc802020-04-09 01:08:11 +01001725
1726 // The removed.txt
Paul Duffin533f9c72020-05-20 16:18:00 +01001727 Removed_api *string `android:"path"`
Colin Cross79c7c262019-04-17 11:11:46 -07001728}
1729
Paul Duffin56d44902020-01-31 13:36:25 +00001730type sdkLibraryImportProperties struct {
Paul Duffinfcfd7912020-01-31 17:54:30 +00001731 // List of shared java libs, common to all scopes, that this module has
1732 // dependencies to
1733 Libs []string
Paul Duffin56d44902020-01-31 13:36:25 +00001734}
1735
Paul Duffinf642a312020-06-12 17:46:39 +01001736type SdkLibraryImport struct {
Colin Cross79c7c262019-04-17 11:11:46 -07001737 android.ModuleBase
1738 android.DefaultableModuleBase
1739 prebuilt android.Prebuilt
Paul Duffin61871622020-02-10 13:37:10 +00001740 android.ApexModuleBase
1741 android.SdkBase
Colin Cross79c7c262019-04-17 11:11:46 -07001742
1743 properties sdkLibraryImportProperties
1744
Paul Duffin6a2bd112020-04-07 19:27:04 +01001745 // Map from api scope to the scope specific property structure.
1746 scopeProperties map[*apiScope]*sdkLibraryScopeProperties
1747
Paul Duffin56d44902020-01-31 13:36:25 +00001748 commonToSdkLibraryAndImport
Paul Duffinf642a312020-06-12 17:46:39 +01001749
1750 // The reference to the implementation library created by the source module.
1751 // Is nil if the source module does not exist.
1752 implLibraryModule *Library
1753
1754 // The reference to the xml permissions module created by the source module.
1755 // Is nil if the source module does not exist.
1756 xmlPermissionsFileModule *sdkLibraryXml
Colin Cross79c7c262019-04-17 11:11:46 -07001757}
1758
Paul Duffinf642a312020-06-12 17:46:39 +01001759var _ SdkLibraryDependency = (*SdkLibraryImport)(nil)
Colin Cross79c7c262019-04-17 11:11:46 -07001760
Paul Duffin6a2bd112020-04-07 19:27:04 +01001761// The type of a structure that contains a field of type sdkLibraryScopeProperties
1762// for each apiscope in allApiScopes, e.g. something like:
1763// struct {
1764// Public sdkLibraryScopeProperties
1765// System sdkLibraryScopeProperties
1766// ...
1767// }
1768var allScopeStructType = createAllScopePropertiesStructType()
1769
1770// Dynamically create a structure type for each apiscope in allApiScopes.
1771func createAllScopePropertiesStructType() reflect.Type {
1772 var fields []reflect.StructField
1773 for _, apiScope := range allApiScopes {
1774 field := reflect.StructField{
1775 Name: apiScope.fieldName,
1776 Type: reflect.TypeOf(sdkLibraryScopeProperties{}),
1777 }
1778 fields = append(fields, field)
1779 }
1780
1781 return reflect.StructOf(fields)
1782}
1783
1784// Create an instance of the scope specific structure type and return a map
1785// from apiscope to a pointer to each scope specific field.
1786func createPropertiesInstance() (interface{}, map[*apiScope]*sdkLibraryScopeProperties) {
1787 allScopePropertiesPtr := reflect.New(allScopeStructType)
1788 allScopePropertiesStruct := allScopePropertiesPtr.Elem()
1789 scopeProperties := make(map[*apiScope]*sdkLibraryScopeProperties)
1790
1791 for _, apiScope := range allApiScopes {
1792 field := allScopePropertiesStruct.FieldByName(apiScope.fieldName)
1793 scopeProperties[apiScope] = field.Addr().Interface().(*sdkLibraryScopeProperties)
1794 }
1795
1796 return allScopePropertiesPtr.Interface(), scopeProperties
1797}
1798
Jaewoong Jung4f158ee2019-07-11 10:05:35 -07001799// java_sdk_library_import imports a prebuilt java_sdk_library.
Colin Cross79c7c262019-04-17 11:11:46 -07001800func sdkLibraryImportFactory() android.Module {
Paul Duffinf642a312020-06-12 17:46:39 +01001801 module := &SdkLibraryImport{}
Colin Cross79c7c262019-04-17 11:11:46 -07001802
Paul Duffin6a2bd112020-04-07 19:27:04 +01001803 allScopeProperties, scopeToProperties := createPropertiesInstance()
1804 module.scopeProperties = scopeToProperties
1805 module.AddProperties(&module.properties, allScopeProperties)
Colin Cross79c7c262019-04-17 11:11:46 -07001806
Paul Duffinb74ee3e2020-05-08 14:16:20 +01001807 // Initialize information common between source and prebuilt.
1808 module.initCommon(&module.ModuleBase)
1809
Paul Duffin0bdcb272020-02-06 15:24:57 +00001810 android.InitPrebuiltModule(module, &[]string{""})
Paul Duffin61871622020-02-10 13:37:10 +00001811 android.InitApexModule(module)
1812 android.InitSdkAwareModule(module)
Colin Cross79c7c262019-04-17 11:11:46 -07001813 InitJavaModule(module, android.HostAndDeviceSupported)
1814
Paul Duffin1a724e62020-05-08 13:44:43 +01001815 module.SetDefaultableHook(func(mctx android.DefaultableHookContext) {
1816 if module.initCommonAfterDefaultsApplied(mctx) {
1817 module.createInternalModules(mctx)
1818 }
1819 })
Colin Cross79c7c262019-04-17 11:11:46 -07001820 return module
1821}
1822
Paul Duffinf642a312020-06-12 17:46:39 +01001823func (module *SdkLibraryImport) Prebuilt() *android.Prebuilt {
Colin Cross79c7c262019-04-17 11:11:46 -07001824 return &module.prebuilt
1825}
1826
Paul Duffinf642a312020-06-12 17:46:39 +01001827func (module *SdkLibraryImport) Name() string {
Colin Cross79c7c262019-04-17 11:11:46 -07001828 return module.prebuilt.Name(module.ModuleBase.Name())
1829}
1830
Paul Duffinf642a312020-06-12 17:46:39 +01001831func (module *SdkLibraryImport) createInternalModules(mctx android.DefaultableHookContext) {
Colin Cross79c7c262019-04-17 11:11:46 -07001832
Paul Duffin50061512020-01-21 16:31:05 +00001833 // If the build is configured to use prebuilts then force this to be preferred.
1834 if mctx.Config().UnbundledBuildUsePrebuiltSdks() {
1835 module.prebuilt.ForcePrefer()
1836 }
1837
Paul Duffin6a2bd112020-04-07 19:27:04 +01001838 for apiScope, scopeProperties := range module.scopeProperties {
Paul Duffin56d44902020-01-31 13:36:25 +00001839 if len(scopeProperties.Jars) == 0 {
1840 continue
1841 }
1842
Paul Duffinf6155722020-04-09 00:07:11 +01001843 module.createJavaImportForStubs(mctx, apiScope, scopeProperties)
Paul Duffinf488ef22020-04-09 00:10:17 +01001844
Paul Duffin533f9c72020-05-20 16:18:00 +01001845 if len(scopeProperties.Stub_srcs) > 0 {
1846 module.createPrebuiltStubsSources(mctx, apiScope, scopeProperties)
1847 }
Paul Duffin56d44902020-01-31 13:36:25 +00001848 }
Colin Cross79c7c262019-04-17 11:11:46 -07001849
1850 javaSdkLibraries := javaSdkLibraries(mctx.Config())
1851 javaSdkLibrariesLock.Lock()
1852 defer javaSdkLibrariesLock.Unlock()
1853 *javaSdkLibraries = append(*javaSdkLibraries, module.BaseModuleName())
1854}
1855
Paul Duffinf642a312020-06-12 17:46:39 +01001856func (module *SdkLibraryImport) createJavaImportForStubs(mctx android.DefaultableHookContext, apiScope *apiScope, scopeProperties *sdkLibraryScopeProperties) {
Paul Duffinf6155722020-04-09 00:07:11 +01001857 // Creates a java import for the jar with ".stubs" suffix
1858 props := struct {
Paul Duffind41712d2020-05-16 09:57:59 +01001859 Name *string
1860 Sdk_version *string
1861 Libs []string
1862 Jars []string
1863 Prefer *bool
Paul Duffinf6155722020-04-09 00:07:11 +01001864 }{}
Paul Duffinb74ee3e2020-05-08 14:16:20 +01001865 props.Name = proptools.StringPtr(module.stubsLibraryModuleName(apiScope))
Paul Duffinf6155722020-04-09 00:07:11 +01001866 props.Sdk_version = scopeProperties.Sdk_version
1867 // Prepend any of the libs from the legacy public properties to the libs for each of the
1868 // scopes to avoid having to duplicate them in each scope.
1869 props.Libs = append(module.properties.Libs, scopeProperties.Libs...)
1870 props.Jars = scopeProperties.Jars
Paul Duffind41712d2020-05-16 09:57:59 +01001871
Paul Duffindd89a282020-05-13 16:08:09 +01001872 // The imports are preferred if the java_sdk_library_import is preferred.
1873 props.Prefer = proptools.BoolPtr(module.prebuilt.Prefer())
Paul Duffin64e61992020-05-15 10:20:31 +01001874
1875 mctx.CreateModule(ImportFactory, &props, module.sdkComponentPropertiesForChildLibrary())
Paul Duffinf6155722020-04-09 00:07:11 +01001876}
1877
Paul Duffinf642a312020-06-12 17:46:39 +01001878func (module *SdkLibraryImport) createPrebuiltStubsSources(mctx android.DefaultableHookContext, apiScope *apiScope, scopeProperties *sdkLibraryScopeProperties) {
Paul Duffinf488ef22020-04-09 00:10:17 +01001879 props := struct {
Paul Duffindd89a282020-05-13 16:08:09 +01001880 Name *string
1881 Srcs []string
1882 Prefer *bool
Paul Duffinf488ef22020-04-09 00:10:17 +01001883 }{}
Paul Duffinb74ee3e2020-05-08 14:16:20 +01001884 props.Name = proptools.StringPtr(module.stubsSourceModuleName(apiScope))
Paul Duffinf488ef22020-04-09 00:10:17 +01001885 props.Srcs = scopeProperties.Stub_srcs
1886 mctx.CreateModule(PrebuiltStubsSourcesFactory, &props)
Paul Duffindd89a282020-05-13 16:08:09 +01001887
1888 // The stubs source is preferred if the java_sdk_library_import is preferred.
1889 props.Prefer = proptools.BoolPtr(module.prebuilt.Prefer())
Paul Duffinf488ef22020-04-09 00:10:17 +01001890}
1891
Paul Duffinf642a312020-06-12 17:46:39 +01001892func (module *SdkLibraryImport) DepsMutator(ctx android.BottomUpMutatorContext) {
Paul Duffin6a2bd112020-04-07 19:27:04 +01001893 for apiScope, scopeProperties := range module.scopeProperties {
Paul Duffin56d44902020-01-31 13:36:25 +00001894 if len(scopeProperties.Jars) == 0 {
1895 continue
1896 }
1897
1898 // Add dependencies to the prebuilt stubs library
Paul Duffinb74ee3e2020-05-08 14:16:20 +01001899 ctx.AddVariationDependencies(nil, apiScope.stubsTag, module.stubsLibraryModuleName(apiScope))
Paul Duffin533f9c72020-05-20 16:18:00 +01001900
1901 if len(scopeProperties.Stub_srcs) > 0 {
1902 // Add dependencies to the prebuilt stubs source library
1903 ctx.AddVariationDependencies(nil, apiScope.stubsSourceTag, module.stubsSourceModuleName(apiScope))
1904 }
Paul Duffin56d44902020-01-31 13:36:25 +00001905 }
Paul Duffinf642a312020-06-12 17:46:39 +01001906
1907 implName := module.implLibraryModuleName()
1908 if ctx.OtherModuleExists(implName) {
1909 ctx.AddVariationDependencies(nil, implLibraryTag, implName)
1910
1911 xmlPermissionsModuleName := module.xmlPermissionsModuleName()
1912 if module.sharedLibrary() && ctx.OtherModuleExists(xmlPermissionsModuleName) {
1913 // Add dependency to the rule for generating the xml permissions file
1914 ctx.AddDependency(module, xmlPermissionsFileTag, xmlPermissionsModuleName)
1915 }
1916 }
Colin Cross79c7c262019-04-17 11:11:46 -07001917}
1918
Paul Duffinf642a312020-06-12 17:46:39 +01001919func (module *SdkLibraryImport) DepIsInSameApex(mctx android.BaseModuleContext, dep android.Module) bool {
1920 depTag := mctx.OtherModuleDependencyTag(dep)
1921 if depTag == xmlPermissionsFileTag {
1922 return true
1923 }
1924
1925 // None of the other dependencies of the java_sdk_library_import are in the same apex
1926 // as the one that references this module.
1927 return false
1928}
1929
1930func (module *SdkLibraryImport) OutputFiles(tag string) (android.Paths, error) {
Paul Duffin46fdda82020-05-14 15:39:10 +01001931 return module.commonOutputFiles(tag)
1932}
1933
Paul Duffinf642a312020-06-12 17:46:39 +01001934func (module *SdkLibraryImport) GenerateAndroidBuildActions(ctx android.ModuleContext) {
Paul Duffin533f9c72020-05-20 16:18:00 +01001935 // Record the paths to the prebuilt stubs library and stubs source.
Colin Cross79c7c262019-04-17 11:11:46 -07001936 ctx.VisitDirectDeps(func(to android.Module) {
1937 tag := ctx.OtherModuleDependencyTag(to)
1938
Paul Duffin533f9c72020-05-20 16:18:00 +01001939 // Extract information from any of the scope specific dependencies.
1940 if scopeTag, ok := tag.(scopeDependencyTag); ok {
1941 apiScope := scopeTag.apiScope
1942 scopePaths := module.getScopePathsCreateIfNeeded(apiScope)
1943
1944 // Extract information from the dependency. The exact information extracted
1945 // is determined by the nature of the dependency which is determined by the tag.
1946 scopeTag.extractDepInfo(ctx, to, scopePaths)
Paul Duffinf642a312020-06-12 17:46:39 +01001947 } else if tag == implLibraryTag {
1948 if implLibrary, ok := to.(*Library); ok {
1949 module.implLibraryModule = implLibrary
1950 } else {
1951 ctx.ModuleErrorf("implementation library must be of type *java.Library but was %T", to)
1952 }
1953 } else if tag == xmlPermissionsFileTag {
1954 if xmlPermissionsFileModule, ok := to.(*sdkLibraryXml); ok {
1955 module.xmlPermissionsFileModule = xmlPermissionsFileModule
1956 } else {
1957 ctx.ModuleErrorf("xml permissions file module must be of type *sdkLibraryXml but was %T", to)
1958 }
Colin Cross79c7c262019-04-17 11:11:46 -07001959 }
1960 })
Paul Duffin533f9c72020-05-20 16:18:00 +01001961
1962 // Populate the scope paths with information from the properties.
1963 for apiScope, scopeProperties := range module.scopeProperties {
1964 if len(scopeProperties.Jars) == 0 {
1965 continue
1966 }
1967
1968 paths := module.getScopePathsCreateIfNeeded(apiScope)
1969 paths.currentApiFilePath = android.OptionalPathForModuleSrc(ctx, scopeProperties.Current_api)
1970 paths.removedApiFilePath = android.OptionalPathForModuleSrc(ctx, scopeProperties.Removed_api)
1971 }
Colin Cross79c7c262019-04-17 11:11:46 -07001972}
1973
Paul Duffinf642a312020-06-12 17:46:39 +01001974func (module *SdkLibraryImport) sdkJars(ctx android.BaseModuleContext, sdkVersion sdkSpec, headerJars bool) android.Paths {
1975
1976 // For consistency with SdkLibrary make the implementation jar available to libraries that
1977 // are within the same APEX.
1978 implLibraryModule := module.implLibraryModule
1979 if implLibraryModule != nil && withinSameApexAs(module, ctx.Module()) {
1980 if headerJars {
1981 return implLibraryModule.HeaderJars()
1982 } else {
1983 return implLibraryModule.ImplementationJars()
1984 }
1985 }
1986
Paul Duffina3fb67d2020-05-20 14:20:02 +01001987 return module.selectHeaderJarsForSdkVersion(ctx, sdkVersion)
Paul Duffin56d44902020-01-31 13:36:25 +00001988}
1989
Colin Cross79c7c262019-04-17 11:11:46 -07001990// to satisfy SdkLibraryDependency interface
Paul Duffinf642a312020-06-12 17:46:39 +01001991func (module *SdkLibraryImport) SdkHeaderJars(ctx android.BaseModuleContext, sdkVersion sdkSpec) android.Paths {
Colin Cross79c7c262019-04-17 11:11:46 -07001992 // This module is just a wrapper for the prebuilt stubs.
Paul Duffinf642a312020-06-12 17:46:39 +01001993 return module.sdkJars(ctx, sdkVersion, true)
Colin Cross79c7c262019-04-17 11:11:46 -07001994}
1995
1996// to satisfy SdkLibraryDependency interface
Paul Duffinf642a312020-06-12 17:46:39 +01001997func (module *SdkLibraryImport) SdkImplementationJars(ctx android.BaseModuleContext, sdkVersion sdkSpec) android.Paths {
Colin Cross79c7c262019-04-17 11:11:46 -07001998 // This module is just a wrapper for the stubs.
Paul Duffinf642a312020-06-12 17:46:39 +01001999 return module.sdkJars(ctx, sdkVersion, false)
2000}
2001
2002// to satisfy apex.javaDependency interface
2003func (module *SdkLibraryImport) DexJar() android.Path {
2004 if module.implLibraryModule == nil {
2005 return nil
2006 } else {
2007 return module.implLibraryModule.DexJar()
2008 }
2009}
2010
2011// to satisfy apex.javaDependency interface
2012func (module *SdkLibraryImport) JacocoReportClassesFile() android.Path {
2013 if module.implLibraryModule == nil {
2014 return nil
2015 } else {
2016 return module.implLibraryModule.JacocoReportClassesFile()
2017 }
2018}
2019
2020// to satisfy apex.javaDependency interface
Colin Cross5bc17442020-07-21 20:31:17 -07002021func (module *SdkLibraryImport) LintDepSets() LintDepSets {
2022 if module.implLibraryModule == nil {
2023 return LintDepSets{}
2024 } else {
2025 return module.implLibraryModule.LintDepSets()
2026 }
2027}
2028
2029// to satisfy apex.javaDependency interface
Paul Duffinf642a312020-06-12 17:46:39 +01002030func (module *SdkLibraryImport) Stem() string {
2031 return module.BaseModuleName()
Colin Cross79c7c262019-04-17 11:11:46 -07002032}
Jiyong Parke3833882020-02-17 17:28:10 +09002033
Paul Duffin9ee66da2020-06-17 16:59:43 +01002034var _ ApexDependency = (*SdkLibraryImport)(nil)
2035
2036// to satisfy java.ApexDependency interface
2037func (module *SdkLibraryImport) HeaderJars() android.Paths {
2038 if module.implLibraryModule == nil {
2039 return nil
2040 } else {
2041 return module.implLibraryModule.HeaderJars()
2042 }
2043}
2044
2045// to satisfy java.ApexDependency interface
2046func (module *SdkLibraryImport) ImplementationAndResourcesJars() android.Paths {
2047 if module.implLibraryModule == nil {
2048 return nil
2049 } else {
2050 return module.implLibraryModule.ImplementationAndResourcesJars()
2051 }
2052}
2053
Jiyong Parke3833882020-02-17 17:28:10 +09002054//
2055// java_sdk_library_xml
2056//
2057type sdkLibraryXml struct {
2058 android.ModuleBase
2059 android.DefaultableModuleBase
2060 android.ApexModuleBase
2061
2062 properties sdkLibraryXmlProperties
2063
2064 outputFilePath android.OutputPath
2065 installDirPath android.InstallPath
2066}
2067
2068type sdkLibraryXmlProperties struct {
2069 // canonical name of the lib
2070 Lib_name *string
2071}
2072
2073// java_sdk_library_xml builds the permission xml file for a java_sdk_library.
2074// Not to be used directly by users. java_sdk_library internally uses this.
2075func sdkLibraryXmlFactory() android.Module {
2076 module := &sdkLibraryXml{}
2077
2078 module.AddProperties(&module.properties)
2079
2080 android.InitApexModule(module)
2081 android.InitAndroidArchModule(module, android.DeviceSupported, android.MultilibCommon)
2082
2083 return module
2084}
2085
2086// from android.PrebuiltEtcModule
2087func (module *sdkLibraryXml) SubDir() string {
2088 return "permissions"
2089}
2090
2091// from android.PrebuiltEtcModule
2092func (module *sdkLibraryXml) OutputFile() android.OutputPath {
2093 return module.outputFilePath
2094}
2095
2096// from android.ApexModule
2097func (module *sdkLibraryXml) AvailableFor(what string) bool {
2098 return true
2099}
2100
2101func (module *sdkLibraryXml) DepsMutator(ctx android.BottomUpMutatorContext) {
2102 // do nothing
2103}
2104
2105// File path to the runtime implementation library
2106func (module *sdkLibraryXml) implPath() string {
2107 implName := proptools.String(module.properties.Lib_name)
2108 if apexName := module.ApexName(); apexName != "" {
2109 // TODO(b/146468504): ApexName() is only a soong module name, not apex name.
2110 // In most cases, this works fine. But when apex_name is set or override_apex is used
2111 // this can be wrong.
2112 return fmt.Sprintf("/apex/%s/javalib/%s.jar", apexName, implName)
2113 }
2114 partition := "system"
2115 if module.SocSpecific() {
2116 partition = "vendor"
2117 } else if module.DeviceSpecific() {
2118 partition = "odm"
2119 } else if module.ProductSpecific() {
2120 partition = "product"
2121 } else if module.SystemExtSpecific() {
2122 partition = "system_ext"
2123 }
2124 return "/" + partition + "/framework/" + implName + ".jar"
2125}
2126
2127func (module *sdkLibraryXml) GenerateAndroidBuildActions(ctx android.ModuleContext) {
2128 libName := proptools.String(module.properties.Lib_name)
2129 xmlContent := fmt.Sprintf(permissionsTemplate, libName, module.implPath())
2130
2131 module.outputFilePath = android.PathForModuleOut(ctx, libName+".xml").OutputPath
2132 rule := android.NewRuleBuilder()
2133 rule.Command().
2134 Text("/bin/bash -c \"echo -e '" + xmlContent + "'\" > ").
2135 Output(module.outputFilePath)
2136
2137 rule.Build(pctx, ctx, "java_sdk_xml", "Permission XML")
2138
2139 module.installDirPath = android.PathForModuleInstall(ctx, "etc", module.SubDir())
2140}
2141
2142func (module *sdkLibraryXml) AndroidMkEntries() []android.AndroidMkEntries {
2143 if !module.IsForPlatform() {
2144 return []android.AndroidMkEntries{android.AndroidMkEntries{
2145 Disabled: true,
2146 }}
2147 }
2148
2149 return []android.AndroidMkEntries{android.AndroidMkEntries{
2150 Class: "ETC",
2151 OutputFile: android.OptionalPathForPath(module.outputFilePath),
2152 ExtraEntries: []android.AndroidMkExtraEntriesFunc{
2153 func(entries *android.AndroidMkEntries) {
2154 entries.SetString("LOCAL_MODULE_TAGS", "optional")
2155 entries.SetString("LOCAL_MODULE_PATH", module.installDirPath.ToMakePath().String())
2156 entries.SetString("LOCAL_INSTALLED_MODULE_STEM", module.outputFilePath.Base())
2157 },
2158 },
2159 }}
2160}
Paul Duffin61871622020-02-10 13:37:10 +00002161
2162type sdkLibrarySdkMemberType struct {
2163 android.SdkMemberTypeBase
2164}
2165
2166func (s *sdkLibrarySdkMemberType) AddDependencies(mctx android.BottomUpMutatorContext, dependencyTag blueprint.DependencyTag, names []string) {
2167 mctx.AddVariationDependencies(nil, dependencyTag, names...)
2168}
2169
2170func (s *sdkLibrarySdkMemberType) IsInstance(module android.Module) bool {
2171 _, ok := module.(*SdkLibrary)
2172 return ok
2173}
2174
2175func (s *sdkLibrarySdkMemberType) AddPrebuiltModule(ctx android.SdkMemberContext, member android.SdkMember) android.BpModule {
2176 return ctx.SnapshotBuilder().AddPrebuiltModule(member, "java_sdk_library_import")
2177}
2178
2179func (s *sdkLibrarySdkMemberType) CreateVariantPropertiesStruct() android.SdkMemberProperties {
2180 return &sdkLibrarySdkMemberProperties{}
2181}
2182
2183type sdkLibrarySdkMemberProperties struct {
2184 android.SdkMemberPropertiesBase
2185
2186 // Scope to per scope properties.
2187 Scopes map[*apiScope]scopeProperties
2188
2189 // Additional libraries that the exported stubs libraries depend upon.
2190 Libs []string
Paul Duffinf488ef22020-04-09 00:10:17 +01002191
2192 // The Java stubs source files.
2193 Stub_srcs []string
Paul Duffinf8e08b22020-05-13 16:54:55 +01002194
2195 // The naming scheme.
2196 Naming_scheme *string
Paul Duffina84756c2020-05-26 20:57:10 +01002197
2198 // True if the java_sdk_library_import is for a shared library, false
2199 // otherwise.
2200 Shared_library *bool
Paul Duffin61871622020-02-10 13:37:10 +00002201}
2202
2203type scopeProperties struct {
Paul Duffin75dcc802020-04-09 01:08:11 +01002204 Jars android.Paths
2205 StubsSrcJar android.Path
2206 CurrentApiFile android.Path
2207 RemovedApiFile android.Path
2208 SdkVersion string
Paul Duffin61871622020-02-10 13:37:10 +00002209}
2210
2211func (s *sdkLibrarySdkMemberProperties) PopulateFromVariant(ctx android.SdkMemberContext, variant android.Module) {
2212 sdk := variant.(*SdkLibrary)
2213
2214 s.Scopes = make(map[*apiScope]scopeProperties)
2215 for _, apiScope := range allApiScopes {
Paul Duffin5ae30792020-05-20 11:52:25 +01002216 paths := sdk.findScopePaths(apiScope)
2217 if paths == nil {
2218 continue
2219 }
2220
Paul Duffin61871622020-02-10 13:37:10 +00002221 jars := paths.stubsImplPath
2222 if len(jars) > 0 {
2223 properties := scopeProperties{}
2224 properties.Jars = jars
Paul Duffin153501f2020-05-12 15:52:55 +01002225 properties.SdkVersion = sdk.sdkVersionForStubsLibrary(ctx.SdkModuleContext(), apiScope)
Paul Duffin533f9c72020-05-20 16:18:00 +01002226 properties.StubsSrcJar = paths.stubsSrcJar.Path()
Paul Duffin86672f62020-06-19 18:39:55 +01002227 if paths.currentApiFilePath.Valid() {
2228 properties.CurrentApiFile = paths.currentApiFilePath.Path()
2229 }
2230 if paths.removedApiFilePath.Valid() {
2231 properties.RemovedApiFile = paths.removedApiFilePath.Path()
2232 }
Paul Duffin61871622020-02-10 13:37:10 +00002233 s.Scopes[apiScope] = properties
2234 }
2235 }
2236
2237 s.Libs = sdk.properties.Libs
Paul Duffind11e78e2020-05-15 20:37:11 +01002238 s.Naming_scheme = sdk.commonSdkLibraryProperties.Naming_scheme
Paul Duffina84756c2020-05-26 20:57:10 +01002239 s.Shared_library = proptools.BoolPtr(sdk.sharedLibrary())
Paul Duffin61871622020-02-10 13:37:10 +00002240}
2241
2242func (s *sdkLibrarySdkMemberProperties) AddToPropertySet(ctx android.SdkMemberContext, propertySet android.BpPropertySet) {
Paul Duffinf8e08b22020-05-13 16:54:55 +01002243 if s.Naming_scheme != nil {
2244 propertySet.AddProperty("naming_scheme", proptools.String(s.Naming_scheme))
2245 }
Paul Duffina84756c2020-05-26 20:57:10 +01002246 if s.Shared_library != nil {
2247 propertySet.AddProperty("shared_library", *s.Shared_library)
2248 }
Paul Duffinf8e08b22020-05-13 16:54:55 +01002249
Paul Duffin61871622020-02-10 13:37:10 +00002250 for _, apiScope := range allApiScopes {
2251 if properties, ok := s.Scopes[apiScope]; ok {
Paul Duffin0f270632020-05-13 19:19:49 +01002252 scopeSet := propertySet.AddPropertySet(apiScope.propertyName)
Paul Duffin61871622020-02-10 13:37:10 +00002253
Paul Duffinf488ef22020-04-09 00:10:17 +01002254 scopeDir := filepath.Join("sdk_library", s.OsPrefix(), apiScope.name)
2255
Paul Duffin61871622020-02-10 13:37:10 +00002256 var jars []string
2257 for _, p := range properties.Jars {
Paul Duffinf488ef22020-04-09 00:10:17 +01002258 dest := filepath.Join(scopeDir, ctx.Name()+"-stubs.jar")
Paul Duffin61871622020-02-10 13:37:10 +00002259 ctx.SnapshotBuilder().CopyToSnapshot(p, dest)
2260 jars = append(jars, dest)
2261 }
2262 scopeSet.AddProperty("jars", jars)
2263
Paul Duffinf488ef22020-04-09 00:10:17 +01002264 // Merge the stubs source jar into the snapshot zip so that when it is unpacked
2265 // the source files are also unpacked.
2266 snapshotRelativeDir := filepath.Join(scopeDir, ctx.Name()+"_stub_sources")
2267 ctx.SnapshotBuilder().UnzipToSnapshot(properties.StubsSrcJar, snapshotRelativeDir)
2268 scopeSet.AddProperty("stub_srcs", []string{snapshotRelativeDir})
2269
Paul Duffin75dcc802020-04-09 01:08:11 +01002270 if properties.CurrentApiFile != nil {
2271 currentApiSnapshotPath := filepath.Join(scopeDir, ctx.Name()+".txt")
2272 ctx.SnapshotBuilder().CopyToSnapshot(properties.CurrentApiFile, currentApiSnapshotPath)
2273 scopeSet.AddProperty("current_api", currentApiSnapshotPath)
2274 }
2275
2276 if properties.RemovedApiFile != nil {
2277 removedApiSnapshotPath := filepath.Join(scopeDir, ctx.Name()+"-removed.txt")
Paul Duffinb1787352020-06-02 13:00:02 +01002278 ctx.SnapshotBuilder().CopyToSnapshot(properties.RemovedApiFile, removedApiSnapshotPath)
Paul Duffin75dcc802020-04-09 01:08:11 +01002279 scopeSet.AddProperty("removed_api", removedApiSnapshotPath)
2280 }
2281
Paul Duffin61871622020-02-10 13:37:10 +00002282 if properties.SdkVersion != "" {
2283 scopeSet.AddProperty("sdk_version", properties.SdkVersion)
2284 }
2285 }
2286 }
2287
2288 if len(s.Libs) > 0 {
2289 propertySet.AddPropertyWithTag("libs", s.Libs, ctx.SnapshotBuilder().SdkMemberReferencePropertyTag(false))
2290 }
2291}