blob: cc1e12d144b46bb6dc82213bcc08d21f67a52656 [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
Paul Duffin80342d72020-06-26 22:08:43 +010073var _ android.ReplaceSourceWithPrebuilt = (*scopeDependencyTag)(nil)
74
75func (tag scopeDependencyTag) ReplaceSourceWithPrebuilt() bool {
76 return false
77}
78
Paul Duffind1b3a922020-01-22 11:57:20 +000079// Provides information about an api scope, e.g. public, system, test.
80type apiScope struct {
81 // The name of the api scope, e.g. public, system, test
82 name string
83
Paul Duffin51a2bee2020-05-05 14:40:52 +010084 // The api scope that this scope extends.
85 extends *apiScope
86
Paul Duffin3a254982020-04-28 10:44:03 +010087 // The legacy enabled status for a specific scope can be dependent on other
88 // properties that have been specified on the library so it is provided by
89 // a function that can determine the status by examining those properties.
90 legacyEnabledStatus func(module *SdkLibrary) bool
91
92 // The default enabled status for non-legacy behavior, which is triggered by
93 // explicitly enabling at least one api scope.
94 defaultEnabledStatus bool
95
96 // Gets a pointer to the scope specific properties.
97 scopeSpecificProperties func(module *SdkLibrary) *ApiScopeProperties
98
Paul Duffin6a2bd112020-04-07 19:27:04 +010099 // The name of the field in the dynamically created structure.
100 fieldName string
101
Paul Duffin0f270632020-05-13 19:19:49 +0100102 // The name of the property in the java_sdk_library_import
103 propertyName string
104
Paul Duffind1b3a922020-01-22 11:57:20 +0000105 // The tag to use to depend on the stubs library module.
106 stubsTag scopeDependencyTag
107
Paul Duffina377e4c2020-04-29 13:30:54 +0100108 // The tag to use to depend on the stubs source module (if separate from the API module).
109 stubsSourceTag scopeDependencyTag
110
111 // The tag to use to depend on the API file generating module (if separate from the stubs source module).
112 apiFileTag scopeDependencyTag
113
Paul Duffin5fb82132020-04-29 20:45:27 +0100114 // The tag to use to depend on the stubs source and API module.
115 stubsSourceAndApiTag scopeDependencyTag
Paul Duffind1b3a922020-01-22 11:57:20 +0000116
117 // The scope specific prefix to add to the api file base of "current.txt" or "removed.txt".
118 apiFilePrefix string
119
120 // The scope specific prefix to add to the sdk library module name to construct a scope specific
121 // module name.
122 moduleSuffix string
123
Paul Duffind1b3a922020-01-22 11:57:20 +0000124 // SDK version that the stubs library is built against. Note that this is always
125 // *current. Older stubs library built with a numbered SDK version is created from
126 // the prebuilt jar.
127 sdkVersion string
Paul Duffin3c7c3472020-04-07 18:50:10 +0100128
129 // Extra arguments to pass to droidstubs for this scope.
130 droidstubsArgs []string
Anton Hansson5ff28e52020-05-02 11:19:36 +0100131
Paul Duffina377e4c2020-04-29 13:30:54 +0100132 // The args that must be passed to droidstubs to generate the stubs source
133 // for this scope.
134 //
135 // The stubs source must include the definitions of everything that is in this
136 // api scope and all the scopes that this one extends.
137 droidstubsArgsForGeneratingStubsSource []string
138
139 // The args that must be passed to droidstubs to generate the API for this scope.
140 //
141 // The API only includes the additional members that this scope adds over the scope
142 // that it extends.
143 droidstubsArgsForGeneratingApi []string
144
145 // True if the stubs source and api can be created by the same metalava invocation.
146 createStubsSourceAndApiTogether bool
147
Anton Hansson5ff28e52020-05-02 11:19:36 +0100148 // Whether the api scope can be treated as unstable, and should skip compat checks.
149 unstable bool
Paul Duffind1b3a922020-01-22 11:57:20 +0000150}
151
152// Initialize a scope, creating and adding appropriate dependency tags
153func initApiScope(scope *apiScope) *apiScope {
Paul Duffin5fb82132020-04-29 20:45:27 +0100154 name := scope.name
Paul Duffin46fdda82020-05-14 15:39:10 +0100155 scopeByName[name] = scope
156 allScopeNames = append(allScopeNames, name)
Paul Duffin0f270632020-05-13 19:19:49 +0100157 scope.propertyName = strings.ReplaceAll(name, "-", "_")
158 scope.fieldName = proptools.FieldNameForProperty(scope.propertyName)
Paul Duffind1b3a922020-01-22 11:57:20 +0000159 scope.stubsTag = scopeDependencyTag{
Paul Duffin5fb82132020-04-29 20:45:27 +0100160 name: name + "-stubs",
161 apiScope: scope,
162 depInfoExtractor: (*scopePaths).extractStubsLibraryInfoFromDependency,
Paul Duffind1b3a922020-01-22 11:57:20 +0000163 }
Paul Duffina377e4c2020-04-29 13:30:54 +0100164 scope.stubsSourceTag = scopeDependencyTag{
165 name: name + "-stubs-source",
166 apiScope: scope,
167 depInfoExtractor: (*scopePaths).extractStubsSourceInfoFromDep,
168 }
169 scope.apiFileTag = scopeDependencyTag{
170 name: name + "-api",
171 apiScope: scope,
172 depInfoExtractor: (*scopePaths).extractApiInfoFromDep,
173 }
Paul Duffin5fb82132020-04-29 20:45:27 +0100174 scope.stubsSourceAndApiTag = scopeDependencyTag{
175 name: name + "-stubs-source-and-api",
176 apiScope: scope,
177 depInfoExtractor: (*scopePaths).extractStubsSourceAndApiInfoFromApiStubsProvider,
Paul Duffind1b3a922020-01-22 11:57:20 +0000178 }
Paul Duffina377e4c2020-04-29 13:30:54 +0100179
180 // To get the args needed to generate the stubs source append all the args from
181 // this scope and all the scopes it extends as each set of args adds additional
182 // members to the stubs.
183 var stubsSourceArgs []string
184 for s := scope; s != nil; s = s.extends {
185 stubsSourceArgs = append(stubsSourceArgs, s.droidstubsArgs...)
186 }
187 scope.droidstubsArgsForGeneratingStubsSource = stubsSourceArgs
188
189 // Currently the args needed to generate the API are the same as the args
190 // needed to add additional members.
191 apiArgs := scope.droidstubsArgs
192 scope.droidstubsArgsForGeneratingApi = apiArgs
193
194 // If the args needed to generate the stubs and API are the same then they
195 // can be generated in a single invocation of metalava, otherwise they will
196 // need separate invocations.
197 scope.createStubsSourceAndApiTogether = reflect.DeepEqual(stubsSourceArgs, apiArgs)
198
Paul Duffind1b3a922020-01-22 11:57:20 +0000199 return scope
200}
201
Paul Duffinb74ee3e2020-05-08 14:16:20 +0100202func (scope *apiScope) stubsLibraryModuleName(baseName string) string {
Paul Duffin1c094a02020-05-08 15:52:37 +0100203 return baseName + ".stubs" + scope.moduleSuffix
Paul Duffind1b3a922020-01-22 11:57:20 +0000204}
205
Paul Duffin5fb82132020-04-29 20:45:27 +0100206func (scope *apiScope) stubsSourceModuleName(baseName string) string {
Paul Duffin1c094a02020-05-08 15:52:37 +0100207 return baseName + ".stubs.source" + scope.moduleSuffix
Paul Duffind1b3a922020-01-22 11:57:20 +0000208}
209
Paul Duffina377e4c2020-04-29 13:30:54 +0100210func (scope *apiScope) apiModuleName(baseName string) string {
Paul Duffin1c094a02020-05-08 15:52:37 +0100211 return baseName + ".api" + scope.moduleSuffix
Paul Duffina377e4c2020-04-29 13:30:54 +0100212}
213
Paul Duffin3a254982020-04-28 10:44:03 +0100214func (scope *apiScope) String() string {
215 return scope.name
216}
217
Paul Duffind1b3a922020-01-22 11:57:20 +0000218type apiScopes []*apiScope
219
220func (scopes apiScopes) Strings(accessor func(*apiScope) string) []string {
221 var list []string
222 for _, scope := range scopes {
223 list = append(list, accessor(scope))
224 }
225 return list
226}
227
Jiyong Parkc678ad32018-04-10 13:07:10 +0900228var (
Paul Duffin46fdda82020-05-14 15:39:10 +0100229 scopeByName = make(map[string]*apiScope)
230 allScopeNames []string
Paul Duffind1b3a922020-01-22 11:57:20 +0000231 apiScopePublic = initApiScope(&apiScope{
Paul Duffin3a254982020-04-28 10:44:03 +0100232 name: "public",
233
234 // Public scope is enabled by default for both legacy and non-legacy modes.
235 legacyEnabledStatus: func(module *SdkLibrary) bool {
236 return true
237 },
238 defaultEnabledStatus: true,
239
240 scopeSpecificProperties: func(module *SdkLibrary) *ApiScopeProperties {
241 return &module.sdkLibraryProperties.Public
242 },
Paul Duffind1b3a922020-01-22 11:57:20 +0000243 sdkVersion: "current",
244 })
245 apiScopeSystem = initApiScope(&apiScope{
Paul Duffin3a254982020-04-28 10:44:03 +0100246 name: "system",
247 extends: apiScopePublic,
248 legacyEnabledStatus: (*SdkLibrary).generateTestAndSystemScopesByDefault,
249 scopeSpecificProperties: func(module *SdkLibrary) *ApiScopeProperties {
250 return &module.sdkLibraryProperties.System
251 },
Anton Hanssone366fff2020-04-28 16:47:41 +0100252 apiFilePrefix: "system-",
Paul Duffin1c094a02020-05-08 15:52:37 +0100253 moduleSuffix: ".system",
Anton Hanssone366fff2020-04-28 16:47:41 +0100254 sdkVersion: "system_current",
Paul Duffin991f2622020-04-29 22:18:41 +0100255 droidstubsArgs: []string{"-showAnnotation android.annotation.SystemApi\\(client=android.annotation.SystemApi.Client.PRIVILEGED_APPS\\)"},
Paul Duffind1b3a922020-01-22 11:57:20 +0000256 })
257 apiScopeTest = initApiScope(&apiScope{
Paul Duffin3a254982020-04-28 10:44:03 +0100258 name: "test",
259 extends: apiScopePublic,
260 legacyEnabledStatus: (*SdkLibrary).generateTestAndSystemScopesByDefault,
261 scopeSpecificProperties: func(module *SdkLibrary) *ApiScopeProperties {
262 return &module.sdkLibraryProperties.Test
263 },
Anton Hanssone366fff2020-04-28 16:47:41 +0100264 apiFilePrefix: "test-",
Paul Duffin1c094a02020-05-08 15:52:37 +0100265 moduleSuffix: ".test",
Anton Hanssone366fff2020-04-28 16:47:41 +0100266 sdkVersion: "test_current",
267 droidstubsArgs: []string{"-showAnnotation android.annotation.TestApi"},
Anton Hansson5ff28e52020-05-02 11:19:36 +0100268 unstable: true,
Paul Duffind1b3a922020-01-22 11:57:20 +0000269 })
Paul Duffin6d7f0a72020-04-28 14:13:56 +0100270 apiScopeModuleLib = initApiScope(&apiScope{
Paul Duffin0f270632020-05-13 19:19:49 +0100271 name: "module-lib",
Paul Duffin6d7f0a72020-04-28 14:13:56 +0100272 extends: apiScopeSystem,
Paul Duffin5a757b12020-06-02 13:00:08 +0100273 // The module-lib scope is disabled by default in legacy mode.
Paul Duffin6d7f0a72020-04-28 14:13:56 +0100274 //
275 // Enabling this would break existing usages.
276 legacyEnabledStatus: func(module *SdkLibrary) bool {
277 return false
278 },
279 scopeSpecificProperties: func(module *SdkLibrary) *ApiScopeProperties {
280 return &module.sdkLibraryProperties.Module_lib
281 },
282 apiFilePrefix: "module-lib-",
283 moduleSuffix: ".module_lib",
284 sdkVersion: "module_current",
285 droidstubsArgs: []string{
286 "--show-annotation android.annotation.SystemApi\\(client=android.annotation.SystemApi.Client.MODULE_LIBRARIES\\)",
287 },
288 })
Paul Duffin5a757b12020-06-02 13:00:08 +0100289 apiScopeSystemServer = initApiScope(&apiScope{
290 name: "system-server",
291 extends: apiScopePublic,
292 // The system-server scope is disabled by default in legacy mode.
293 //
294 // Enabling this would break existing usages.
295 legacyEnabledStatus: func(module *SdkLibrary) bool {
296 return false
297 },
298 scopeSpecificProperties: func(module *SdkLibrary) *ApiScopeProperties {
299 return &module.sdkLibraryProperties.System_server
300 },
301 apiFilePrefix: "system-server-",
302 moduleSuffix: ".system_server",
303 sdkVersion: "system_server_current",
304 droidstubsArgs: []string{
305 "--show-annotation android.annotation.SystemApi\\(client=android.annotation.SystemApi.Client.SYSTEM_SERVER\\) ",
306 "--hide-annotation android.annotation.Hide",
307 // com.android.* classes are okay in this interface"
308 "--hide InternalClasses",
309 },
310 })
Paul Duffind1b3a922020-01-22 11:57:20 +0000311 allApiScopes = apiScopes{
312 apiScopePublic,
313 apiScopeSystem,
314 apiScopeTest,
Paul Duffin6d7f0a72020-04-28 14:13:56 +0100315 apiScopeModuleLib,
Paul Duffin5a757b12020-06-02 13:00:08 +0100316 apiScopeSystemServer,
Paul Duffind1b3a922020-01-22 11:57:20 +0000317 }
Jiyong Parkc678ad32018-04-10 13:07:10 +0900318)
319
Jiyong Park82484c02018-04-23 21:41:26 +0900320var (
321 javaSdkLibrariesLock sync.Mutex
322)
323
Jiyong Parkc678ad32018-04-10 13:07:10 +0900324// TODO: these are big features that are currently missing
Jiyong Park1be96912018-05-28 18:02:19 +0900325// 1) disallowing linking to the runtime shared lib
326// 2) HTML generation
Jiyong Parkc678ad32018-04-10 13:07:10 +0900327
328func init() {
Paul Duffin43dc1cc2019-12-19 11:18:54 +0000329 RegisterSdkLibraryBuildComponents(android.InitRegistrationContext)
Jiyong Parkc678ad32018-04-10 13:07:10 +0900330
Jiyong Park82484c02018-04-23 21:41:26 +0900331 android.RegisterMakeVarsProvider(pctx, func(ctx android.MakeVarsContext) {
332 javaSdkLibraries := javaSdkLibraries(ctx.Config())
333 sort.Strings(*javaSdkLibraries)
334 ctx.Strict("JAVA_SDK_LIBRARIES", strings.Join(*javaSdkLibraries, " "))
335 })
Paul Duffin61871622020-02-10 13:37:10 +0000336
337 // Register sdk member types.
338 android.RegisterSdkMemberType(&sdkLibrarySdkMemberType{
339 android.SdkMemberTypeBase{
340 PropertyName: "java_sdk_libs",
341 SupportsSdk: true,
342 },
343 })
Jiyong Parkc678ad32018-04-10 13:07:10 +0900344}
345
Paul Duffin43dc1cc2019-12-19 11:18:54 +0000346func RegisterSdkLibraryBuildComponents(ctx android.RegistrationContext) {
347 ctx.RegisterModuleType("java_sdk_library", SdkLibraryFactory)
348 ctx.RegisterModuleType("java_sdk_library_import", sdkLibraryImportFactory)
349}
350
Paul Duffin3a254982020-04-28 10:44:03 +0100351// Properties associated with each api scope.
352type ApiScopeProperties struct {
353 // Indicates whether the api surface is generated.
354 //
355 // If this is set for any scope then all scopes must explicitly specify if they
356 // are enabled. This is to prevent new usages from depending on legacy behavior.
357 //
358 // Otherwise, if this is not set for any scope then the default behavior is
359 // scope specific so please refer to the scope specific property documentation.
360 Enabled *bool
Paul Duffin080f5ee2020-05-12 11:50:28 +0100361
362 // The sdk_version to use for building the stubs.
363 //
364 // If not specified then it will use an sdk_version determined as follows:
365 // 1) If the sdk_version specified on the java_sdk_library is none then this
366 // will be none. This is used for java_sdk_library instances that are used
367 // to create stubs that contribute to the core_current sdk version.
368 // 2) Otherwise, it is assumed that this library extends but does not contribute
369 // directly to a specific sdk_version and so this uses the sdk_version appropriate
370 // for the api scope. e.g. public will use sdk_version: current, system will use
371 // sdk_version: system_current, etc.
372 //
373 // This does not affect the sdk_version used for either generating the stubs source
374 // or the API file. They both have to use the same sdk_version as is used for
375 // compiling the implementation library.
376 Sdk_version *string
Paul Duffin3a254982020-04-28 10:44:03 +0100377}
378
Jiyong Parkc678ad32018-04-10 13:07:10 +0900379type sdkLibraryProperties struct {
Paul Duffin9d582cc2020-05-16 15:52:12 +0100380 // Visibility for impl library module. If not specified then defaults to the
381 // visibility property.
382 Impl_library_visibility []string
383
Paul Duffin344c4ee2020-04-29 23:35:13 +0100384 // Visibility for stubs library modules. If not specified then defaults to the
385 // visibility property.
386 Stubs_library_visibility []string
387
388 // Visibility for stubs source modules. If not specified then defaults to the
389 // visibility property.
390 Stubs_source_visibility []string
391
Sundong Ahnf043cf62018-06-25 16:04:37 +0900392 // List of Java libraries that will be in the classpath when building stubs
393 Stub_only_libs []string `android:"arch_variant"`
394
Paul Duffin7a586d32019-12-30 17:09:34 +0000395 // list of package names that will be documented and publicized as API.
396 // This allows the API to be restricted to a subset of the source files provided.
397 // If this is unspecified then all the source files will be treated as being part
398 // of the API.
Jiyong Parkc678ad32018-04-10 13:07:10 +0900399 Api_packages []string
400
Jiyong Park5a2c9d72018-05-01 22:25:41 +0900401 // list of package names that must be hidden from the API
402 Hidden_api_packages []string
403
Paul Duffin749f98f2019-12-30 17:23:46 +0000404 // the relative path to the directory containing the api specification files.
405 // Defaults to "api".
406 Api_dir *string
407
Paul Duffind11e78e2020-05-15 20:37:11 +0100408 // Determines whether a runtime implementation library is built; defaults to false.
409 //
410 // If true then it also prevents the module from being used as a shared module, i.e.
411 // it is as is shared_library: false, was set.
Paul Duffin43db9be2019-12-30 17:35:49 +0000412 Api_only *bool
413
Paul Duffin11512472019-02-11 15:55:17 +0000414 // local files that are used within user customized droiddoc options.
415 Droiddoc_option_files []string
416
417 // additional droiddoc options
418 // Available variables for substitution:
419 //
420 // $(location <label>): the path to the droiddoc_option_files with name <label>
Sundong Ahndd567f92018-07-31 17:19:11 +0900421 Droiddoc_options []string
422
Paul Duffin2ce1e812020-05-20 19:35:27 +0100423 // is set to true, Metalava will allow framework SDK to contain annotations.
424 Annotations_enabled *bool
425
Sundong Ahn054b19a2018-10-19 13:46:09 +0900426 // a list of top-level directories containing files to merge qualifier annotations
427 // (i.e. those intended to be included in the stubs written) from.
428 Merge_annotations_dirs []string
429
430 // a list of top-level directories containing Java stub files to merge show/hide annotations from.
431 Merge_inclusion_annotations_dirs []string
432
433 // If set to true, the path of dist files is apistubs/core. Defaults to false.
434 Core_lib *bool
435
Sundong Ahn80a87b32019-05-13 15:02:50 +0900436 // don't create dist rules.
437 No_dist *bool `blueprint:"mutated"`
438
Paul Duffin3a254982020-04-28 10:44:03 +0100439 // indicates whether system and test apis should be generated.
440 Generate_system_and_test_apis bool `blueprint:"mutated"`
441
442 // The properties specific to the public api scope
443 //
444 // Unless explicitly specified by using public.enabled the public api scope is
445 // enabled by default in both legacy and non-legacy mode.
446 Public ApiScopeProperties
447
448 // The properties specific to the system api scope
449 //
450 // In legacy mode the system api scope is enabled by default when sdk_version
451 // is set to something other than "none".
452 //
453 // In non-legacy mode the system api scope is disabled by default.
454 System ApiScopeProperties
455
456 // The properties specific to the test api scope
457 //
458 // In legacy mode the test api scope is enabled by default when sdk_version
459 // is set to something other than "none".
460 //
461 // In non-legacy mode the test api scope is disabled by default.
462 Test ApiScopeProperties
Paul Duffin37e0b772019-12-30 17:20:10 +0000463
Paul Duffin5a757b12020-06-02 13:00:08 +0100464 // The properties specific to the module-lib api scope
Paul Duffin6d7f0a72020-04-28 14:13:56 +0100465 //
Paul Duffin5a757b12020-06-02 13:00:08 +0100466 // Unless explicitly specified by using test.enabled the module-lib api scope is
Paul Duffin6d7f0a72020-04-28 14:13:56 +0100467 // disabled by default.
468 Module_lib ApiScopeProperties
469
Paul Duffin5a757b12020-06-02 13:00:08 +0100470 // The properties specific to the system-server api scope
471 //
472 // Unless explicitly specified by using test.enabled the module-lib api scope is
473 // disabled by default.
474 System_server ApiScopeProperties
475
Jiyong Park27fc4142020-05-28 00:19:53 +0900476 // Determines if the stubs are preferred over the implementation library
477 // for linking, even when the client doesn't specify sdk_version. When this
478 // is set to true, such clients are provided with the widest API surface that
479 // this lib provides. Note however that this option doesn't affect the clients
480 // that are in the same APEX as this library. In that case, the clients are
481 // always linked with the implementation library. Default is false.
482 Default_to_stubs *bool
483
Paul Duffin8986cc92020-05-10 19:32:20 +0100484 // Properties related to api linting.
485 Api_lint struct {
486 // Enable api linting.
487 Enabled *bool
488 }
489
Jiyong Parkc678ad32018-04-10 13:07:10 +0900490 // TODO: determines whether to create HTML doc or not
491 //Html_doc *bool
492}
493
Paul Duffin533f9c72020-05-20 16:18:00 +0100494// Paths to outputs from java_sdk_library and java_sdk_library_import.
495//
496// Fields that are android.Paths are always set (during GenerateAndroidBuildActions).
497// OptionalPaths are always set by java_sdk_library but may not be set by
498// java_sdk_library_import as not all instances provide that information.
Paul Duffind1b3a922020-01-22 11:57:20 +0000499type scopePaths struct {
Paul Duffin533f9c72020-05-20 16:18:00 +0100500 // The path (represented as Paths for convenience when returning) to the stubs header jar.
501 //
502 // That is the jar that is created by turbine.
503 stubsHeaderPath android.Paths
504
505 // The path (represented as Paths for convenience when returning) to the stubs implementation jar.
506 //
507 // This is not the implementation jar, it still only contains stubs.
508 stubsImplPath android.Paths
509
510 // The API specification file, e.g. system_current.txt.
511 currentApiFilePath android.OptionalPath
512
513 // The specification of API elements removed since the last release.
514 removedApiFilePath android.OptionalPath
515
516 // The stubs source jar.
517 stubsSrcJar android.OptionalPath
Paul Duffind1b3a922020-01-22 11:57:20 +0000518}
519
Paul Duffin5fb82132020-04-29 20:45:27 +0100520func (paths *scopePaths) extractStubsLibraryInfoFromDependency(dep android.Module) error {
521 if lib, ok := dep.(Dependency); ok {
522 paths.stubsHeaderPath = lib.HeaderJars()
523 paths.stubsImplPath = lib.ImplementationJars()
524 return nil
525 } else {
526 return fmt.Errorf("expected module that implements Dependency, e.g. java_library")
527 }
528}
529
Paul Duffina377e4c2020-04-29 13:30:54 +0100530func (paths *scopePaths) treatDepAsApiStubsProvider(dep android.Module, action func(provider ApiStubsProvider)) error {
531 if apiStubsProvider, ok := dep.(ApiStubsProvider); ok {
532 action(apiStubsProvider)
Paul Duffin5fb82132020-04-29 20:45:27 +0100533 return nil
534 } else {
535 return fmt.Errorf("expected module that implements ApiStubsProvider, e.g. droidstubs")
536 }
537}
538
Paul Duffin533f9c72020-05-20 16:18:00 +0100539func (paths *scopePaths) treatDepAsApiStubsSrcProvider(dep android.Module, action func(provider ApiStubsSrcProvider)) error {
540 if apiStubsProvider, ok := dep.(ApiStubsSrcProvider); ok {
541 action(apiStubsProvider)
542 return nil
543 } else {
544 return fmt.Errorf("expected module that implements ApiStubsSrcProvider, e.g. droidstubs")
545 }
546}
547
Paul Duffina377e4c2020-04-29 13:30:54 +0100548func (paths *scopePaths) extractApiInfoFromApiStubsProvider(provider ApiStubsProvider) {
Paul Duffin533f9c72020-05-20 16:18:00 +0100549 paths.currentApiFilePath = android.OptionalPathForPath(provider.ApiFilePath())
550 paths.removedApiFilePath = android.OptionalPathForPath(provider.RemovedApiFilePath())
Paul Duffina377e4c2020-04-29 13:30:54 +0100551}
552
553func (paths *scopePaths) extractApiInfoFromDep(dep android.Module) error {
554 return paths.treatDepAsApiStubsProvider(dep, func(provider ApiStubsProvider) {
555 paths.extractApiInfoFromApiStubsProvider(provider)
556 })
557}
558
Paul Duffin533f9c72020-05-20 16:18:00 +0100559func (paths *scopePaths) extractStubsSourceInfoFromApiStubsProviders(provider ApiStubsSrcProvider) {
560 paths.stubsSrcJar = android.OptionalPathForPath(provider.StubsSrcJar())
Paul Duffina377e4c2020-04-29 13:30:54 +0100561}
562
563func (paths *scopePaths) extractStubsSourceInfoFromDep(dep android.Module) error {
Paul Duffin533f9c72020-05-20 16:18:00 +0100564 return paths.treatDepAsApiStubsSrcProvider(dep, func(provider ApiStubsSrcProvider) {
Paul Duffina377e4c2020-04-29 13:30:54 +0100565 paths.extractStubsSourceInfoFromApiStubsProviders(provider)
566 })
567}
568
569func (paths *scopePaths) extractStubsSourceAndApiInfoFromApiStubsProvider(dep android.Module) error {
570 return paths.treatDepAsApiStubsProvider(dep, func(provider ApiStubsProvider) {
571 paths.extractApiInfoFromApiStubsProvider(provider)
572 paths.extractStubsSourceInfoFromApiStubsProviders(provider)
573 })
574}
575
576type commonToSdkLibraryAndImportProperties struct {
Paul Duffin1a724e62020-05-08 13:44:43 +0100577 // The naming scheme to use for the components that this module creates.
578 //
Paul Duffindef8a892020-05-08 15:36:30 +0100579 // If not specified then it defaults to "default". The other allowable value is
580 // "framework-modules" which matches the scheme currently used by framework modules
581 // for the equivalent components represented as separate Soong modules.
Paul Duffin1a724e62020-05-08 13:44:43 +0100582 //
583 // This is a temporary mechanism to simplify conversion from separate modules for each
584 // component that follow a different naming pattern to the default one.
585 //
586 // TODO(b/155480189) - Remove once naming inconsistencies have been resolved.
Paul Duffina377e4c2020-04-29 13:30:54 +0100587 Naming_scheme *string
Paul Duffind11e78e2020-05-15 20:37:11 +0100588
589 // Specifies whether this module can be used as an Android shared library; defaults
590 // to true.
591 //
592 // An Android shared library is one that can be referenced in a <uses-library> element
593 // in an AndroidManifest.xml.
594 Shared_library *bool
Paul Duffina377e4c2020-04-29 13:30:54 +0100595}
596
Paul Duffin56d44902020-01-31 13:36:25 +0000597// Common code between sdk library and sdk library import
598type commonToSdkLibraryAndImport struct {
Paul Duffinb74ee3e2020-05-08 14:16:20 +0100599 moduleBase *android.ModuleBase
600
Paul Duffin56d44902020-01-31 13:36:25 +0000601 scopePaths map[*apiScope]*scopePaths
Paul Duffin1a724e62020-05-08 13:44:43 +0100602
603 namingScheme sdkLibraryComponentNamingScheme
604
Paul Duffind11e78e2020-05-15 20:37:11 +0100605 commonSdkLibraryProperties commonToSdkLibraryAndImportProperties
Paul Duffin64e61992020-05-15 10:20:31 +0100606
607 // Functionality related to this being used as a component of a java_sdk_library.
608 EmbeddableSdkLibraryComponent
Paul Duffin56d44902020-01-31 13:36:25 +0000609}
610
Paul Duffinb74ee3e2020-05-08 14:16:20 +0100611func (c *commonToSdkLibraryAndImport) initCommon(moduleBase *android.ModuleBase) {
612 c.moduleBase = moduleBase
Paul Duffin1a724e62020-05-08 13:44:43 +0100613
Paul Duffind11e78e2020-05-15 20:37:11 +0100614 moduleBase.AddProperties(&c.commonSdkLibraryProperties)
Paul Duffin64e61992020-05-15 10:20:31 +0100615
616 // Initialize this as an sdk library component.
617 c.initSdkLibraryComponent(moduleBase)
Paul Duffin1a724e62020-05-08 13:44:43 +0100618}
619
620func (c *commonToSdkLibraryAndImport) initCommonAfterDefaultsApplied(ctx android.DefaultableHookContext) bool {
Paul Duffind11e78e2020-05-15 20:37:11 +0100621 schemeProperty := proptools.StringDefault(c.commonSdkLibraryProperties.Naming_scheme, "default")
Paul Duffin1a724e62020-05-08 13:44:43 +0100622 switch schemeProperty {
623 case "default":
624 c.namingScheme = &defaultNamingScheme{}
Paul Duffindef8a892020-05-08 15:36:30 +0100625 case "framework-modules":
626 c.namingScheme = &frameworkModulesNamingScheme{}
Paul Duffin1a724e62020-05-08 13:44:43 +0100627 default:
628 ctx.PropertyErrorf("naming_scheme", "expected 'default' but was %q", schemeProperty)
629 return false
630 }
631
Paul Duffind11e78e2020-05-15 20:37:11 +0100632 // Only track this sdk library if this can be used as a shared library.
633 if c.sharedLibrary() {
634 // Use the name specified in the module definition as the owner.
635 c.sdkLibraryComponentProperties.SdkLibraryToImplicitlyTrack = proptools.StringPtr(c.moduleBase.BaseModuleName())
636 }
Paul Duffin64e61992020-05-15 10:20:31 +0100637
Paul Duffin1a724e62020-05-08 13:44:43 +0100638 return true
Paul Duffinb74ee3e2020-05-08 14:16:20 +0100639}
640
Paul Duffineedc5d52020-06-12 17:46:39 +0100641// Module name of the runtime implementation library
642func (c *commonToSdkLibraryAndImport) implLibraryModuleName() string {
643 return c.moduleBase.BaseModuleName() + ".impl"
644}
645
646// Module name of the XML file for the lib
647func (c *commonToSdkLibraryAndImport) xmlPermissionsModuleName() string {
648 return c.moduleBase.BaseModuleName() + sdkXmlFileSuffix
649}
650
Paul Duffinb74ee3e2020-05-08 14:16:20 +0100651// Name of the java_library module that compiles the stubs source.
652func (c *commonToSdkLibraryAndImport) stubsLibraryModuleName(apiScope *apiScope) string {
Paul Duffin1a724e62020-05-08 13:44:43 +0100653 return c.namingScheme.stubsLibraryModuleName(apiScope, c.moduleBase.BaseModuleName())
Paul Duffinb74ee3e2020-05-08 14:16:20 +0100654}
655
656// Name of the droidstubs module that generates the stubs source and may also
657// generate/check the API.
658func (c *commonToSdkLibraryAndImport) stubsSourceModuleName(apiScope *apiScope) string {
Paul Duffin1a724e62020-05-08 13:44:43 +0100659 return c.namingScheme.stubsSourceModuleName(apiScope, c.moduleBase.BaseModuleName())
Paul Duffinb74ee3e2020-05-08 14:16:20 +0100660}
661
662// Name of the droidstubs module that generates/checks the API. Only used if it
663// requires different arts to the stubs source generating module.
664func (c *commonToSdkLibraryAndImport) apiModuleName(apiScope *apiScope) string {
Paul Duffin1a724e62020-05-08 13:44:43 +0100665 return c.namingScheme.apiModuleName(apiScope, c.moduleBase.BaseModuleName())
Paul Duffinb74ee3e2020-05-08 14:16:20 +0100666}
667
Paul Duffin46fdda82020-05-14 15:39:10 +0100668// The component names for different outputs of the java_sdk_library.
669//
670// They are similar to the names used for the child modules it creates
671const (
672 stubsSourceComponentName = "stubs.source"
673
674 apiTxtComponentName = "api.txt"
675
676 removedApiTxtComponentName = "removed-api.txt"
677)
678
679// A regular expression to match tags that reference a specific stubs component.
680//
681// It will only match if given a valid scope and a valid component. It is verfy strict
682// to ensure it does not accidentally match a similar looking tag that should be processed
683// by the embedded Library.
684var tagSplitter = func() *regexp.Regexp {
685 // Given a list of literal string items returns a regular expression that will
686 // match any one of the items.
687 choice := func(items ...string) string {
688 return `\Q` + strings.Join(items, `\E|\Q`) + `\E`
689 }
690
691 // Regular expression to match one of the scopes.
692 scopesRegexp := choice(allScopeNames...)
693
694 // Regular expression to match one of the components.
695 componentsRegexp := choice(stubsSourceComponentName, apiTxtComponentName, removedApiTxtComponentName)
696
697 // Regular expression to match any combination of one scope and one component.
698 return regexp.MustCompile(fmt.Sprintf(`^\.(%s)\.(%s)$`, scopesRegexp, componentsRegexp))
699}()
700
701// For OutputFileProducer interface
702//
703// .<scope>.stubs.source
704// .<scope>.api.txt
705// .<scope>.removed-api.txt
706func (c *commonToSdkLibraryAndImport) commonOutputFiles(tag string) (android.Paths, error) {
707 if groups := tagSplitter.FindStringSubmatch(tag); groups != nil {
708 scopeName := groups[1]
709 component := groups[2]
710
711 if scope, ok := scopeByName[scopeName]; ok {
712 paths := c.findScopePaths(scope)
713 if paths == nil {
714 return nil, fmt.Errorf("%q does not provide api scope %s", c.moduleBase.BaseModuleName(), scopeName)
715 }
716
717 switch component {
718 case stubsSourceComponentName:
719 if paths.stubsSrcJar.Valid() {
720 return android.Paths{paths.stubsSrcJar.Path()}, nil
721 }
722
723 case apiTxtComponentName:
724 if paths.currentApiFilePath.Valid() {
725 return android.Paths{paths.currentApiFilePath.Path()}, nil
726 }
727
728 case removedApiTxtComponentName:
729 if paths.removedApiFilePath.Valid() {
730 return android.Paths{paths.removedApiFilePath.Path()}, nil
731 }
732 }
733
734 return nil, fmt.Errorf("%s not available for api scope %s", component, scopeName)
735 } else {
736 return nil, fmt.Errorf("unknown scope %s in %s", scope, tag)
737 }
738
739 } else {
740 return nil, nil
741 }
742}
743
Paul Duffin5ae30792020-05-20 11:52:25 +0100744func (c *commonToSdkLibraryAndImport) getScopePathsCreateIfNeeded(scope *apiScope) *scopePaths {
Paul Duffin56d44902020-01-31 13:36:25 +0000745 if c.scopePaths == nil {
746 c.scopePaths = make(map[*apiScope]*scopePaths)
747 }
748 paths := c.scopePaths[scope]
749 if paths == nil {
750 paths = &scopePaths{}
751 c.scopePaths[scope] = paths
752 }
753
754 return paths
755}
756
Paul Duffin5ae30792020-05-20 11:52:25 +0100757func (c *commonToSdkLibraryAndImport) findScopePaths(scope *apiScope) *scopePaths {
758 if c.scopePaths == nil {
759 return nil
760 }
761
762 return c.scopePaths[scope]
763}
764
765// If this does not support the requested api scope then find the closest available
766// scope it does support. Returns nil if no such scope is available.
767func (c *commonToSdkLibraryAndImport) findClosestScopePath(scope *apiScope) *scopePaths {
768 for s := scope; s != nil; s = s.extends {
769 if paths := c.findScopePaths(s); paths != nil {
770 return paths
771 }
772 }
773
774 // This should never happen outside tests as public should be the base scope for every
775 // scope and is enabled by default.
776 return nil
777}
778
Paul Duffina3fb67d2020-05-20 14:20:02 +0100779func (c *commonToSdkLibraryAndImport) selectHeaderJarsForSdkVersion(ctx android.BaseModuleContext, sdkVersion sdkSpec) android.Paths {
Paul Duffin47624362020-05-20 12:19:10 +0100780
781 // If a specific numeric version has been requested then use prebuilt versions of the sdk.
782 if sdkVersion.version.isNumbered() {
783 return PrebuiltJars(ctx, c.moduleBase.BaseModuleName(), sdkVersion)
784 }
785
786 var apiScope *apiScope
787 switch sdkVersion.kind {
788 case sdkSystem:
789 apiScope = apiScopeSystem
Paul Duffin5ae30792020-05-20 11:52:25 +0100790 case sdkModule:
791 apiScope = apiScopeModuleLib
Paul Duffin47624362020-05-20 12:19:10 +0100792 case sdkTest:
793 apiScope = apiScopeTest
Paul Duffin5a757b12020-06-02 13:00:08 +0100794 case sdkSystemServer:
795 apiScope = apiScopeSystemServer
Paul Duffin47624362020-05-20 12:19:10 +0100796 default:
797 apiScope = apiScopePublic
798 }
799
Paul Duffin5ae30792020-05-20 11:52:25 +0100800 paths := c.findClosestScopePath(apiScope)
801 if paths == nil {
802 var scopes []string
803 for _, s := range allApiScopes {
804 if c.findScopePaths(s) != nil {
805 scopes = append(scopes, s.name)
806 }
807 }
808 ctx.ModuleErrorf("requires api scope %s from %s but it only has %q available", apiScope.name, c.moduleBase.BaseModuleName(), scopes)
809 return nil
810 }
811
Paul Duffina3fb67d2020-05-20 14:20:02 +0100812 return paths.stubsHeaderPath
Paul Duffin47624362020-05-20 12:19:10 +0100813}
814
Paul Duffin64e61992020-05-15 10:20:31 +0100815func (c *commonToSdkLibraryAndImport) sdkComponentPropertiesForChildLibrary() interface{} {
816 componentProps := &struct {
817 SdkLibraryToImplicitlyTrack *string
Paul Duffind11e78e2020-05-15 20:37:11 +0100818 }{}
819
820 if c.sharedLibrary() {
Paul Duffin64e61992020-05-15 10:20:31 +0100821 // Mark the stubs library as being components of this java_sdk_library so that
822 // any app that includes code which depends (directly or indirectly) on the stubs
823 // library will have the appropriate <uses-library> invocation inserted into its
824 // manifest if necessary.
Paul Duffind11e78e2020-05-15 20:37:11 +0100825 componentProps.SdkLibraryToImplicitlyTrack = proptools.StringPtr(c.moduleBase.BaseModuleName())
Paul Duffin64e61992020-05-15 10:20:31 +0100826 }
827
828 return componentProps
829}
830
Paul Duffind11e78e2020-05-15 20:37:11 +0100831// Check if this can be used as a shared library.
832func (c *commonToSdkLibraryAndImport) sharedLibrary() bool {
833 return proptools.BoolDefault(c.commonSdkLibraryProperties.Shared_library, true)
834}
835
Paul Duffin64e61992020-05-15 10:20:31 +0100836// Properties related to the use of a module as an component of a java_sdk_library.
837type SdkLibraryComponentProperties struct {
838
839 // The name of the java_sdk_library/_import to add to a <uses-library> entry
840 // in the AndroidManifest.xml of any Android app that includes code that references
841 // this module. If not set then no java_sdk_library/_import is tracked.
842 SdkLibraryToImplicitlyTrack *string `blueprint:"mutated"`
843}
844
845// Structure to be embedded in a module struct that needs to support the
846// SdkLibraryComponentDependency interface.
847type EmbeddableSdkLibraryComponent struct {
848 sdkLibraryComponentProperties SdkLibraryComponentProperties
849}
850
851func (e *EmbeddableSdkLibraryComponent) initSdkLibraryComponent(moduleBase *android.ModuleBase) {
852 moduleBase.AddProperties(&e.sdkLibraryComponentProperties)
853}
854
855// to satisfy SdkLibraryComponentDependency
856func (e *EmbeddableSdkLibraryComponent) OptionalImplicitSdkLibrary() []string {
857 if e.sdkLibraryComponentProperties.SdkLibraryToImplicitlyTrack != nil {
858 return []string{*e.sdkLibraryComponentProperties.SdkLibraryToImplicitlyTrack}
859 }
860 return nil
861}
862
863// Implemented by modules that are (or possibly could be) a component of a java_sdk_library
864// (including the java_sdk_library) itself.
865type SdkLibraryComponentDependency interface {
866 // The optional name of the sdk library that should be implicitly added to the
867 // AndroidManifest of an app that contains code which references the sdk library.
868 //
869 // Returns an array containing 0 or 1 items rather than a *string to make it easier
870 // to append this to the list of exported sdk libraries.
871 OptionalImplicitSdkLibrary() []string
872}
873
874// Make sure that all the module types that are components of java_sdk_library/_import
875// and which can be referenced (directly or indirectly) from an android app implement
876// the SdkLibraryComponentDependency interface.
877var _ SdkLibraryComponentDependency = (*Library)(nil)
878var _ SdkLibraryComponentDependency = (*Import)(nil)
879var _ SdkLibraryComponentDependency = (*SdkLibrary)(nil)
Paul Duffineedc5d52020-06-12 17:46:39 +0100880var _ SdkLibraryComponentDependency = (*SdkLibraryImport)(nil)
Paul Duffin64e61992020-05-15 10:20:31 +0100881
882// Provides access to sdk_version related header and implentation jars.
883type SdkLibraryDependency interface {
884 SdkLibraryComponentDependency
885
886 // Get the header jars appropriate for the supplied sdk_version.
887 //
888 // These are turbine generated jars so they only change if the externals of the
889 // class changes but it does not contain and implementation or JavaDoc.
890 SdkHeaderJars(ctx android.BaseModuleContext, sdkVersion sdkSpec) android.Paths
891
892 // Get the implementation jars appropriate for the supplied sdk version.
893 //
894 // These are either the implementation jar for the whole sdk library or the implementation
895 // jars for the stubs. The latter should only be needed when generating JavaDoc as otherwise
896 // they are identical to the corresponding header jars.
897 SdkImplementationJars(ctx android.BaseModuleContext, sdkVersion sdkSpec) android.Paths
898}
899
Inseob Kimc0907f12019-02-08 21:00:45 +0900900type SdkLibrary struct {
Sundong Ahn054b19a2018-10-19 13:46:09 +0900901 Library
Jiyong Parkc678ad32018-04-10 13:07:10 +0900902
Sundong Ahn054b19a2018-10-19 13:46:09 +0900903 sdkLibraryProperties sdkLibraryProperties
Jiyong Parkc678ad32018-04-10 13:07:10 +0900904
Paul Duffin3a254982020-04-28 10:44:03 +0100905 // Map from api scope to the scope specific property structure.
906 scopeToProperties map[*apiScope]*ApiScopeProperties
907
Paul Duffin56d44902020-01-31 13:36:25 +0000908 commonToSdkLibraryAndImport
Jiyong Parkc678ad32018-04-10 13:07:10 +0900909}
910
Inseob Kimc0907f12019-02-08 21:00:45 +0900911var _ Dependency = (*SdkLibrary)(nil)
912var _ SdkLibraryDependency = (*SdkLibrary)(nil)
Colin Cross897d2ed2019-02-11 14:03:51 -0800913
Paul Duffin3a254982020-04-28 10:44:03 +0100914func (module *SdkLibrary) generateTestAndSystemScopesByDefault() bool {
915 return module.sdkLibraryProperties.Generate_system_and_test_apis
916}
917
918func (module *SdkLibrary) getGeneratedApiScopes(ctx android.EarlyModuleContext) apiScopes {
919 // Check to see if any scopes have been explicitly enabled. If any have then all
920 // must be.
921 anyScopesExplicitlyEnabled := false
922 for _, scope := range allApiScopes {
923 scopeProperties := module.scopeToProperties[scope]
924 if scopeProperties.Enabled != nil {
925 anyScopesExplicitlyEnabled = true
926 break
927 }
Paul Duffind1b3a922020-01-22 11:57:20 +0000928 }
Paul Duffin3a254982020-04-28 10:44:03 +0100929
930 var generatedScopes apiScopes
931 enabledScopes := make(map[*apiScope]struct{})
932 for _, scope := range allApiScopes {
933 scopeProperties := module.scopeToProperties[scope]
934 // If any scopes are explicitly enabled then ignore the legacy enabled status.
935 // This is to ensure that any new usages of this module type do not rely on legacy
936 // behaviour.
937 defaultEnabledStatus := false
938 if anyScopesExplicitlyEnabled {
939 defaultEnabledStatus = scope.defaultEnabledStatus
940 } else {
941 defaultEnabledStatus = scope.legacyEnabledStatus(module)
942 }
943 enabled := proptools.BoolDefault(scopeProperties.Enabled, defaultEnabledStatus)
944 if enabled {
945 enabledScopes[scope] = struct{}{}
946 generatedScopes = append(generatedScopes, scope)
947 }
948 }
949
950 // Now check to make sure that any scope that is extended by an enabled scope is also
951 // enabled.
952 for _, scope := range allApiScopes {
953 if _, ok := enabledScopes[scope]; ok {
954 extends := scope.extends
955 if extends != nil {
956 if _, ok := enabledScopes[extends]; !ok {
957 ctx.ModuleErrorf("enabled api scope %q depends on disabled scope %q", scope, extends)
958 }
959 }
960 }
961 }
962
963 return generatedScopes
Paul Duffind1b3a922020-01-22 11:57:20 +0000964}
965
Paul Duffineedc5d52020-06-12 17:46:39 +0100966type sdkLibraryComponentTag struct {
967 blueprint.BaseDependencyTag
968 name string
969}
970
971// Mark this tag so dependencies that use it are excluded from visibility enforcement.
972func (t sdkLibraryComponentTag) ExcludeFromVisibilityEnforcement() {}
973
974var xmlPermissionsFileTag = sdkLibraryComponentTag{name: "xml-permissions-file"}
Paul Duffine74ac732020-02-06 13:51:46 +0000975
Jiyong Parke3833882020-02-17 17:28:10 +0900976func IsXmlPermissionsFileDepTag(depTag blueprint.DependencyTag) bool {
Paul Duffineedc5d52020-06-12 17:46:39 +0100977 if dt, ok := depTag.(sdkLibraryComponentTag); ok {
Jiyong Parke3833882020-02-17 17:28:10 +0900978 return dt == xmlPermissionsFileTag
979 }
980 return false
981}
982
Paul Duffineedc5d52020-06-12 17:46:39 +0100983var implLibraryTag = sdkLibraryComponentTag{name: "impl-library"}
Paul Duffin9d582cc2020-05-16 15:52:12 +0100984
Paul Duffin44f1d842020-06-26 20:17:02 +0100985// Add the dependencies on the child modules in the component deps mutator.
986func (module *SdkLibrary) ComponentDepsMutator(ctx android.BottomUpMutatorContext) {
Paul Duffin3a254982020-04-28 10:44:03 +0100987 for _, apiScope := range module.getGeneratedApiScopes(ctx) {
Paul Duffind1b3a922020-01-22 11:57:20 +0000988 // Add dependencies to the stubs library
Paul Duffinb74ee3e2020-05-08 14:16:20 +0100989 ctx.AddVariationDependencies(nil, apiScope.stubsTag, module.stubsLibraryModuleName(apiScope))
Paul Duffind1b3a922020-01-22 11:57:20 +0000990
Paul Duffina377e4c2020-04-29 13:30:54 +0100991 // If the stubs source and API cannot be generated together then add an additional dependency on
992 // the API module.
993 if apiScope.createStubsSourceAndApiTogether {
994 // Add a dependency on the stubs source in order to access both stubs source and api information.
Paul Duffinb74ee3e2020-05-08 14:16:20 +0100995 ctx.AddVariationDependencies(nil, apiScope.stubsSourceAndApiTag, module.stubsSourceModuleName(apiScope))
Paul Duffina377e4c2020-04-29 13:30:54 +0100996 } else {
997 // Add separate dependencies on the creators of the stubs source files and the API.
Paul Duffinb74ee3e2020-05-08 14:16:20 +0100998 ctx.AddVariationDependencies(nil, apiScope.stubsSourceTag, module.stubsSourceModuleName(apiScope))
999 ctx.AddVariationDependencies(nil, apiScope.apiFileTag, module.apiModuleName(apiScope))
Paul Duffina377e4c2020-04-29 13:30:54 +01001000 }
Sundong Ahn054b19a2018-10-19 13:46:09 +09001001 }
1002
Paul Duffind11e78e2020-05-15 20:37:11 +01001003 if module.requiresRuntimeImplementationLibrary() {
Paul Duffin9d582cc2020-05-16 15:52:12 +01001004 // Add dependency to the rule for generating the implementation library.
1005 ctx.AddDependency(module, implLibraryTag, module.implLibraryModuleName())
1006
Paul Duffind11e78e2020-05-15 20:37:11 +01001007 if module.sharedLibrary() {
1008 // Add dependency to the rule for generating the xml permissions file
Paul Duffineedc5d52020-06-12 17:46:39 +01001009 ctx.AddDependency(module, xmlPermissionsFileTag, module.xmlPermissionsModuleName())
Paul Duffind11e78e2020-05-15 20:37:11 +01001010 }
Paul Duffin44f1d842020-06-26 20:17:02 +01001011 }
1012}
Paul Duffine74ac732020-02-06 13:51:46 +00001013
Paul Duffin44f1d842020-06-26 20:17:02 +01001014// Add other dependencies as normal.
1015func (module *SdkLibrary) DepsMutator(ctx android.BottomUpMutatorContext) {
1016 if module.requiresRuntimeImplementationLibrary() {
Paul Duffind11e78e2020-05-15 20:37:11 +01001017 // Only add the deps for the library if it is actually going to be built.
1018 module.Library.deps(ctx)
1019 }
Jiyong Parkc678ad32018-04-10 13:07:10 +09001020}
1021
Paul Duffin46fdda82020-05-14 15:39:10 +01001022func (module *SdkLibrary) OutputFiles(tag string) (android.Paths, error) {
1023 paths, err := module.commonOutputFiles(tag)
1024 if paths == nil && err == nil {
1025 return module.Library.OutputFiles(tag)
1026 } else {
1027 return paths, err
1028 }
1029}
1030
Inseob Kimc0907f12019-02-08 21:00:45 +09001031func (module *SdkLibrary) GenerateAndroidBuildActions(ctx android.ModuleContext) {
Paul Duffind11e78e2020-05-15 20:37:11 +01001032 // Only build an implementation library if required.
1033 if module.requiresRuntimeImplementationLibrary() {
Paul Duffin43db9be2019-12-30 17:35:49 +00001034 module.Library.GenerateAndroidBuildActions(ctx)
1035 }
Sundong Ahn054b19a2018-10-19 13:46:09 +09001036
Sundong Ahn57368eb2018-07-06 11:20:23 +09001037 // Record the paths to the header jars of the library (stubs and impl).
Paul Duffind1b3a922020-01-22 11:57:20 +00001038 // When this java_sdk_library is depended upon from others via "libs" property,
Jiyong Parkc678ad32018-04-10 13:07:10 +09001039 // the recorded paths will be returned depending on the link type of the caller.
1040 ctx.VisitDirectDeps(func(to android.Module) {
Jiyong Parkc678ad32018-04-10 13:07:10 +09001041 tag := ctx.OtherModuleDependencyTag(to)
1042
Paul Duffin5fb82132020-04-29 20:45:27 +01001043 // Extract information from any of the scope specific dependencies.
1044 if scopeTag, ok := tag.(scopeDependencyTag); ok {
1045 apiScope := scopeTag.apiScope
Paul Duffin5ae30792020-05-20 11:52:25 +01001046 scopePaths := module.getScopePathsCreateIfNeeded(apiScope)
Paul Duffin5fb82132020-04-29 20:45:27 +01001047
1048 // Extract information from the dependency. The exact information extracted
1049 // is determined by the nature of the dependency which is determined by the tag.
1050 scopeTag.extractDepInfo(ctx, to, scopePaths)
Sundong Ahn20e998b2018-07-24 11:19:26 +09001051 }
Jiyong Parkc678ad32018-04-10 13:07:10 +09001052 })
1053}
1054
Jiyong Park0b0e1b92019-12-03 13:24:29 +09001055func (module *SdkLibrary) AndroidMkEntries() []android.AndroidMkEntries {
Paul Duffind11e78e2020-05-15 20:37:11 +01001056 if !module.requiresRuntimeImplementationLibrary() {
Paul Duffin43db9be2019-12-30 17:35:49 +00001057 return nil
1058 }
Jiyong Park0b0e1b92019-12-03 13:24:29 +09001059 entriesList := module.Library.AndroidMkEntries()
Yo Chiang07d75072020-06-05 17:43:19 +08001060 if module.sharedLibrary() {
1061 entries := &entriesList[0]
1062 entries.Required = append(entries.Required, module.xmlPermissionsModuleName())
1063 }
Jiyong Park0b0e1b92019-12-03 13:24:29 +09001064 return entriesList
Jiyong Park82484c02018-04-23 21:41:26 +09001065}
1066
Anton Hansson6bb88102020-03-27 19:43:19 +00001067// The dist path of the stub artifacts
1068func (module *SdkLibrary) apiDistPath(apiScope *apiScope) string {
1069 if module.ModuleBase.Owner() != "" {
1070 return path.Join("apistubs", module.ModuleBase.Owner(), apiScope.name)
1071 } else if Bool(module.sdkLibraryProperties.Core_lib) {
1072 return path.Join("apistubs", "core", apiScope.name)
1073 } else {
1074 return path.Join("apistubs", "android", apiScope.name)
1075 }
1076}
1077
Paul Duffin12ceb462019-12-24 20:31:31 +00001078// Get the sdk version for use when compiling the stubs library.
Paul Duffin153501f2020-05-12 15:52:55 +01001079func (module *SdkLibrary) sdkVersionForStubsLibrary(mctx android.EarlyModuleContext, apiScope *apiScope) string {
Paul Duffin080f5ee2020-05-12 11:50:28 +01001080 scopeProperties := module.scopeToProperties[apiScope]
1081 if scopeProperties.Sdk_version != nil {
1082 return proptools.String(scopeProperties.Sdk_version)
1083 }
1084
Paul Duffin12ceb462019-12-24 20:31:31 +00001085 sdkDep := decodeSdkDep(mctx, sdkContext(&module.Library))
1086 if sdkDep.hasStandardLibs() {
1087 // If building against a standard sdk then use the sdk version appropriate for the scope.
Paul Duffind1b3a922020-01-22 11:57:20 +00001088 return apiScope.sdkVersion
Paul Duffin12ceb462019-12-24 20:31:31 +00001089 } else {
1090 // Otherwise, use no system module.
1091 return "none"
1092 }
1093}
1094
Paul Duffind1b3a922020-01-22 11:57:20 +00001095func (module *SdkLibrary) latestApiFilegroupName(apiScope *apiScope) string {
1096 return ":" + module.BaseModuleName() + ".api." + apiScope.name + ".latest"
Jiyong Park58c518b2018-05-12 22:29:12 +09001097}
Jiyong Parkc678ad32018-04-10 13:07:10 +09001098
Paul Duffind1b3a922020-01-22 11:57:20 +00001099func (module *SdkLibrary) latestRemovedApiFilegroupName(apiScope *apiScope) string {
1100 return ":" + module.BaseModuleName() + "-removed.api." + apiScope.name + ".latest"
Jiyong Parkc678ad32018-04-10 13:07:10 +09001101}
1102
Paul Duffin9d582cc2020-05-16 15:52:12 +01001103// Creates the implementation java library
1104func (module *SdkLibrary) createImplLibrary(mctx android.DefaultableHookContext) {
Paul Duffinc4422102020-06-24 16:22:38 +01001105
1106 moduleNamePtr := proptools.StringPtr(module.BaseModuleName())
1107
Paul Duffin9d582cc2020-05-16 15:52:12 +01001108 props := struct {
Paul Duffinc4422102020-06-24 16:22:38 +01001109 Name *string
1110 Visibility []string
1111 Instrument bool
1112 ConfigurationName *string
Paul Duffin9d582cc2020-05-16 15:52:12 +01001113 }{
1114 Name: proptools.StringPtr(module.implLibraryModuleName()),
1115 Visibility: module.sdkLibraryProperties.Impl_library_visibility,
Paul Duffin296cf332020-06-18 21:09:55 +01001116 // Set the instrument property to ensure it is instrumented when instrumentation is required.
1117 Instrument: true,
Paul Duffinc4422102020-06-24 16:22:38 +01001118
1119 // Make the created library behave as if it had the same name as this module.
1120 ConfigurationName: moduleNamePtr,
Paul Duffin9d582cc2020-05-16 15:52:12 +01001121 }
1122
1123 properties := []interface{}{
1124 &module.properties,
1125 &module.protoProperties,
1126 &module.deviceProperties,
1127 &module.dexpreoptProperties,
Colin Cross014489c2020-06-02 20:09:13 -07001128 &module.linter.properties,
Paul Duffin9d582cc2020-05-16 15:52:12 +01001129 &props,
1130 module.sdkComponentPropertiesForChildLibrary(),
1131 }
1132 mctx.CreateModule(LibraryFactory, properties...)
1133}
1134
Jiyong Parkc678ad32018-04-10 13:07:10 +09001135// Creates a static java library that has API stubs
Paul Duffin2aaef532020-04-29 16:47:28 +01001136func (module *SdkLibrary) createStubsLibrary(mctx android.DefaultableHookContext, apiScope *apiScope) {
Jiyong Parkc678ad32018-04-10 13:07:10 +09001137 props := struct {
Paul Duffind41712d2020-05-16 09:57:59 +01001138 Name *string
1139 Visibility []string
1140 Srcs []string
1141 Installable *bool
1142 Sdk_version *string
1143 System_modules *string
1144 Patch_module *string
1145 Libs []string
1146 Compile_dex *bool
1147 Java_version *string
1148 Product_variables struct {
Jiyong Park82484c02018-04-23 21:41:26 +09001149 Pdk struct {
1150 Enabled *bool
1151 }
Jiyong Parkc678ad32018-04-10 13:07:10 +09001152 }
Sundong Ahn054b19a2018-10-19 13:46:09 +09001153 Openjdk9 struct {
1154 Srcs []string
1155 Javacflags []string
1156 }
Anton Hansson6bb88102020-03-27 19:43:19 +00001157 Dist struct {
1158 Targets []string
1159 Dest *string
1160 Dir *string
1161 Tag *string
1162 }
Jiyong Parkc678ad32018-04-10 13:07:10 +09001163 }{}
1164
Paul Duffinb74ee3e2020-05-08 14:16:20 +01001165 props.Name = proptools.StringPtr(module.stubsLibraryModuleName(apiScope))
Paul Duffin344c4ee2020-04-29 23:35:13 +01001166
1167 // If stubs_library_visibility is not set then the created module will use the
1168 // visibility of this module.
1169 visibility := module.sdkLibraryProperties.Stubs_library_visibility
1170 props.Visibility = visibility
1171
Jiyong Parkc678ad32018-04-10 13:07:10 +09001172 // sources are generated from the droiddoc
Paul Duffinb74ee3e2020-05-08 14:16:20 +01001173 props.Srcs = []string{":" + module.stubsSourceModuleName(apiScope)}
Paul Duffin12ceb462019-12-24 20:31:31 +00001174 sdkVersion := module.sdkVersionForStubsLibrary(mctx, apiScope)
Paul Duffin52d398a2019-06-11 12:31:14 +01001175 props.Sdk_version = proptools.StringPtr(sdkVersion)
Paul Duffinc5d954a2020-05-16 18:54:24 +01001176 props.System_modules = module.deviceProperties.System_modules
1177 props.Patch_module = module.properties.Patch_module
Paul Duffin367ab912019-12-23 19:40:36 +00001178 props.Installable = proptools.BoolPtr(false)
Sundong Ahn054b19a2018-10-19 13:46:09 +09001179 props.Libs = module.sdkLibraryProperties.Stub_only_libs
Paul Duffin2ce1e812020-05-20 19:35:27 +01001180 // The stub-annotations library contains special versions of the annotations
1181 // with CLASS retention policy, so that they're kept.
1182 if proptools.Bool(module.sdkLibraryProperties.Annotations_enabled) {
1183 props.Libs = append(props.Libs, "stub-annotations")
1184 }
Jiyong Park82484c02018-04-23 21:41:26 +09001185 props.Product_variables.Pdk.Enabled = proptools.BoolPtr(false)
Paul Duffinc5d954a2020-05-16 18:54:24 +01001186 props.Openjdk9.Srcs = module.properties.Openjdk9.Srcs
1187 props.Openjdk9.Javacflags = module.properties.Openjdk9.Javacflags
Anton Hanssoncf4dd4c2020-05-21 09:21:57 +01001188 // We compile the stubs for 1.8 in line with the main android.jar stubs, and potential
1189 // interop with older developer tools that don't support 1.9.
1190 props.Java_version = proptools.StringPtr("1.8")
Paul Duffinc5d954a2020-05-16 18:54:24 +01001191 if module.deviceProperties.Compile_dex != nil {
1192 props.Compile_dex = module.deviceProperties.Compile_dex
Sundong Ahndd567f92018-07-31 17:19:11 +09001193 }
Jiyong Parkc678ad32018-04-10 13:07:10 +09001194
Anton Hansson6bb88102020-03-27 19:43:19 +00001195 // Dist the class jar artifact for sdk builds.
1196 if !Bool(module.sdkLibraryProperties.No_dist) {
1197 props.Dist.Targets = []string{"sdk", "win_sdk"}
1198 props.Dist.Dest = proptools.StringPtr(fmt.Sprintf("%v.jar", module.BaseModuleName()))
1199 props.Dist.Dir = proptools.StringPtr(module.apiDistPath(apiScope))
1200 props.Dist.Tag = proptools.StringPtr(".jar")
1201 }
Jiyong Parkc678ad32018-04-10 13:07:10 +09001202
Paul Duffin64e61992020-05-15 10:20:31 +01001203 mctx.CreateModule(LibraryFactory, &props, module.sdkComponentPropertiesForChildLibrary())
Jiyong Parkc678ad32018-04-10 13:07:10 +09001204}
1205
Paul Duffincbcfcaa2020-04-07 18:49:53 +01001206// Creates a droidstubs module that creates stubs source files from the given full source
Paul Duffin5fb82132020-04-29 20:45:27 +01001207// files and also updates and checks the API specification files.
Paul Duffina377e4c2020-04-29 13:30:54 +01001208func (module *SdkLibrary) createStubsSourcesAndApi(mctx android.DefaultableHookContext, apiScope *apiScope, name string, createStubSources, createApi bool, scopeSpecificDroidstubsArgs []string) {
Jiyong Parkc678ad32018-04-10 13:07:10 +09001209 props := struct {
Sundong Ahn054b19a2018-10-19 13:46:09 +09001210 Name *string
Paul Duffin344c4ee2020-04-29 23:35:13 +01001211 Visibility []string
Sundong Ahn054b19a2018-10-19 13:46:09 +09001212 Srcs []string
1213 Installable *bool
Paul Duffin52d398a2019-06-11 12:31:14 +01001214 Sdk_version *string
Paul Duffin12ceb462019-12-24 20:31:31 +00001215 System_modules *string
Sundong Ahn054b19a2018-10-19 13:46:09 +09001216 Libs []string
Paul Duffin11512472019-02-11 15:55:17 +00001217 Arg_files []string
Sundong Ahn054b19a2018-10-19 13:46:09 +09001218 Args *string
Sundong Ahn054b19a2018-10-19 13:46:09 +09001219 Java_version *string
Paul Duffin2ce1e812020-05-20 19:35:27 +01001220 Annotations_enabled *bool
Sundong Ahn054b19a2018-10-19 13:46:09 +09001221 Merge_annotations_dirs []string
1222 Merge_inclusion_annotations_dirs []string
Paul Duffina377e4c2020-04-29 13:30:54 +01001223 Generate_stubs *bool
Sundong Ahn054b19a2018-10-19 13:46:09 +09001224 Check_api struct {
Inseob Kim38449af2019-02-28 14:24:05 +09001225 Current ApiToCheck
1226 Last_released ApiToCheck
1227 Ignore_missing_latest_api *bool
Paul Duffin8986cc92020-05-10 19:32:20 +01001228
1229 Api_lint struct {
1230 Enabled *bool
1231 New_since *string
1232 Baseline_file *string
1233 }
Jiyong Park58c518b2018-05-12 22:29:12 +09001234 }
Sundong Ahn1b92c822018-05-29 11:35:17 +09001235 Aidl struct {
1236 Include_dirs []string
1237 Local_include_dirs []string
1238 }
Anton Hansson6bb88102020-03-27 19:43:19 +00001239 Dist struct {
1240 Targets []string
1241 Dest *string
1242 Dir *string
1243 }
Jiyong Parkc678ad32018-04-10 13:07:10 +09001244 }{}
1245
Paul Duffinda364252020-04-28 14:08:32 +01001246 // The stubs source processing uses the same compile time classpath when extracting the
1247 // API from the implementation library as it does when compiling it. i.e. the same
1248 // * sdk version
1249 // * system_modules
1250 // * libs (static_libs/libs)
Paul Duffin250e6192019-06-07 10:44:37 +01001251
Paul Duffina377e4c2020-04-29 13:30:54 +01001252 props.Name = proptools.StringPtr(name)
Paul Duffin344c4ee2020-04-29 23:35:13 +01001253
1254 // If stubs_source_visibility is not set then the created module will use the
1255 // visibility of this module.
1256 visibility := module.sdkLibraryProperties.Stubs_source_visibility
1257 props.Visibility = visibility
1258
Paul Duffinc5d954a2020-05-16 18:54:24 +01001259 props.Srcs = append(props.Srcs, module.properties.Srcs...)
1260 props.Sdk_version = module.deviceProperties.Sdk_version
1261 props.System_modules = module.deviceProperties.System_modules
Jiyong Parkc678ad32018-04-10 13:07:10 +09001262 props.Installable = proptools.BoolPtr(false)
Sundong Ahne6f0b052018-06-05 16:46:14 +09001263 // A droiddoc module has only one Libs property and doesn't distinguish between
1264 // shared libs and static libs. So we need to add both of these libs to Libs property.
Paul Duffinc5d954a2020-05-16 18:54:24 +01001265 props.Libs = module.properties.Libs
1266 props.Libs = append(props.Libs, module.properties.Static_libs...)
1267 props.Aidl.Include_dirs = module.deviceProperties.Aidl.Include_dirs
1268 props.Aidl.Local_include_dirs = module.deviceProperties.Aidl.Local_include_dirs
1269 props.Java_version = module.properties.Java_version
Jiyong Parkc678ad32018-04-10 13:07:10 +09001270
Paul Duffin2ce1e812020-05-20 19:35:27 +01001271 props.Annotations_enabled = module.sdkLibraryProperties.Annotations_enabled
Sundong Ahn054b19a2018-10-19 13:46:09 +09001272 props.Merge_annotations_dirs = module.sdkLibraryProperties.Merge_annotations_dirs
1273 props.Merge_inclusion_annotations_dirs = module.sdkLibraryProperties.Merge_inclusion_annotations_dirs
1274
Paul Duffincbcfcaa2020-04-07 18:49:53 +01001275 droidstubsArgs := []string{}
Paul Duffin235ffff2019-12-24 10:41:30 +00001276 if len(module.sdkLibraryProperties.Api_packages) != 0 {
Paul Duffincbcfcaa2020-04-07 18:49:53 +01001277 droidstubsArgs = append(droidstubsArgs, "--stub-packages "+strings.Join(module.sdkLibraryProperties.Api_packages, ":"))
Paul Duffin235ffff2019-12-24 10:41:30 +00001278 }
1279 if len(module.sdkLibraryProperties.Hidden_api_packages) != 0 {
Paul Duffincbcfcaa2020-04-07 18:49:53 +01001280 droidstubsArgs = append(droidstubsArgs,
Paul Duffin235ffff2019-12-24 10:41:30 +00001281 android.JoinWithPrefix(module.sdkLibraryProperties.Hidden_api_packages, " --hide-package "))
1282 }
Paul Duffincbcfcaa2020-04-07 18:49:53 +01001283 droidstubsArgs = append(droidstubsArgs, module.sdkLibraryProperties.Droiddoc_options...)
Paul Duffin235ffff2019-12-24 10:41:30 +00001284 disabledWarnings := []string{
1285 "MissingPermission",
1286 "BroadcastBehavior",
1287 "HiddenSuperclass",
1288 "DeprecationMismatch",
1289 "UnavailableSymbol",
1290 "SdkConstant",
1291 "HiddenTypeParameter",
1292 "Todo",
1293 "Typo",
1294 }
Paul Duffincbcfcaa2020-04-07 18:49:53 +01001295 droidstubsArgs = append(droidstubsArgs, android.JoinWithPrefix(disabledWarnings, "--hide "))
Sundong Ahnfb2721f2018-09-17 13:23:09 +09001296
Paul Duffina377e4c2020-04-29 13:30:54 +01001297 if !createStubSources {
1298 // Stubs are not required.
1299 props.Generate_stubs = proptools.BoolPtr(false)
1300 }
1301
Paul Duffin3c7c3472020-04-07 18:50:10 +01001302 // Add in scope specific arguments.
Paul Duffina377e4c2020-04-29 13:30:54 +01001303 droidstubsArgs = append(droidstubsArgs, scopeSpecificDroidstubsArgs...)
Paul Duffin11512472019-02-11 15:55:17 +00001304 props.Arg_files = module.sdkLibraryProperties.Droiddoc_option_files
Paul Duffincbcfcaa2020-04-07 18:49:53 +01001305 props.Args = proptools.StringPtr(strings.Join(droidstubsArgs, " "))
Jiyong Parkc678ad32018-04-10 13:07:10 +09001306
Paul Duffina377e4c2020-04-29 13:30:54 +01001307 if createApi {
1308 // List of APIs identified from the provided source files are created. They are later
1309 // compared against to the not-yet-released (a.k.a current) list of APIs and to the
1310 // last-released (a.k.a numbered) list of API.
1311 currentApiFileName := apiScope.apiFilePrefix + "current.txt"
1312 removedApiFileName := apiScope.apiFilePrefix + "removed.txt"
1313 apiDir := module.getApiDir()
1314 currentApiFileName = path.Join(apiDir, currentApiFileName)
1315 removedApiFileName = path.Join(apiDir, removedApiFileName)
Jiyong Parkc678ad32018-04-10 13:07:10 +09001316
Paul Duffina377e4c2020-04-29 13:30:54 +01001317 // check against the not-yet-release API
1318 props.Check_api.Current.Api_file = proptools.StringPtr(currentApiFileName)
1319 props.Check_api.Current.Removed_api_file = proptools.StringPtr(removedApiFileName)
Jiyong Park58c518b2018-05-12 22:29:12 +09001320
Paul Duffina377e4c2020-04-29 13:30:54 +01001321 if !apiScope.unstable {
1322 // check against the latest released API
1323 latestApiFilegroupName := proptools.StringPtr(module.latestApiFilegroupName(apiScope))
1324 props.Check_api.Last_released.Api_file = latestApiFilegroupName
1325 props.Check_api.Last_released.Removed_api_file = proptools.StringPtr(
1326 module.latestRemovedApiFilegroupName(apiScope))
1327 props.Check_api.Ignore_missing_latest_api = proptools.BoolPtr(true)
Paul Duffin8986cc92020-05-10 19:32:20 +01001328
Paul Duffina377e4c2020-04-29 13:30:54 +01001329 if proptools.Bool(module.sdkLibraryProperties.Api_lint.Enabled) {
1330 // Enable api lint.
1331 props.Check_api.Api_lint.Enabled = proptools.BoolPtr(true)
1332 props.Check_api.Api_lint.New_since = latestApiFilegroupName
Paul Duffin8986cc92020-05-10 19:32:20 +01001333
Paul Duffina377e4c2020-04-29 13:30:54 +01001334 // If it exists then pass a lint-baseline.txt through to droidstubs.
1335 baselinePath := path.Join(apiDir, apiScope.apiFilePrefix+"lint-baseline.txt")
1336 baselinePathRelativeToRoot := path.Join(mctx.ModuleDir(), baselinePath)
1337 paths, err := mctx.GlobWithDeps(baselinePathRelativeToRoot, nil)
1338 if err != nil {
1339 mctx.ModuleErrorf("error checking for presence of %s: %s", baselinePathRelativeToRoot, err)
1340 }
1341 if len(paths) == 1 {
1342 props.Check_api.Api_lint.Baseline_file = proptools.StringPtr(baselinePath)
1343 } else if len(paths) != 0 {
1344 mctx.ModuleErrorf("error checking for presence of %s: expected one path, found: %v", baselinePathRelativeToRoot, paths)
1345 }
Paul Duffin8986cc92020-05-10 19:32:20 +01001346 }
1347 }
Jiyong Park58c518b2018-05-12 22:29:12 +09001348
Paul Duffina377e4c2020-04-29 13:30:54 +01001349 // Dist the api txt artifact for sdk builds.
1350 if !Bool(module.sdkLibraryProperties.No_dist) {
1351 props.Dist.Targets = []string{"sdk", "win_sdk"}
1352 props.Dist.Dest = proptools.StringPtr(fmt.Sprintf("%v.txt", module.BaseModuleName()))
1353 props.Dist.Dir = proptools.StringPtr(path.Join(module.apiDistPath(apiScope), "api"))
1354 }
Anton Hansson6bb88102020-03-27 19:43:19 +00001355 }
1356
Colin Cross84dfc3d2019-09-25 11:33:01 -07001357 mctx.CreateModule(DroidstubsFactory, &props)
Jiyong Parkc678ad32018-04-10 13:07:10 +09001358}
1359
Jooyung Hanb8fa86a2020-03-10 06:23:13 +09001360func (module *SdkLibrary) DepIsInSameApex(mctx android.BaseModuleContext, dep android.Module) bool {
1361 depTag := mctx.OtherModuleDependencyTag(dep)
1362 if depTag == xmlPermissionsFileTag {
1363 return true
1364 }
1365 return module.Library.DepIsInSameApex(mctx, dep)
1366}
1367
Jiyong Parkc678ad32018-04-10 13:07:10 +09001368// Creates the xml file that publicizes the runtime library
Paul Duffin2aaef532020-04-29 16:47:28 +01001369func (module *SdkLibrary) createXmlFile(mctx android.DefaultableHookContext) {
Jiyong Parke3833882020-02-17 17:28:10 +09001370 props := struct {
Paul Duffind41712d2020-05-16 09:57:59 +01001371 Name *string
1372 Lib_name *string
1373 Apex_available []string
Jiyong Parke3833882020-02-17 17:28:10 +09001374 }{
Paul Duffineedc5d52020-06-12 17:46:39 +01001375 Name: proptools.StringPtr(module.xmlPermissionsModuleName()),
Jooyung Hanb8fa86a2020-03-10 06:23:13 +09001376 Lib_name: proptools.StringPtr(module.BaseModuleName()),
1377 Apex_available: module.ApexProperties.Apex_available,
Jiyong Parkc678ad32018-04-10 13:07:10 +09001378 }
Jiyong Parke3833882020-02-17 17:28:10 +09001379
Jiyong Parke3833882020-02-17 17:28:10 +09001380 mctx.CreateModule(sdkLibraryXmlFactory, &props)
Jiyong Parkc678ad32018-04-10 13:07:10 +09001381}
1382
Paul Duffin50061512020-01-21 16:31:05 +00001383func PrebuiltJars(ctx android.BaseModuleContext, baseName string, s sdkSpec) android.Paths {
Jiyong Park6a927c42020-01-21 02:03:43 +09001384 var ver sdkVersion
1385 var kind sdkKind
1386 if s.usePrebuilt(ctx) {
1387 ver = s.version
1388 kind = s.kind
Jiyong Parkc678ad32018-04-10 13:07:10 +09001389 } else {
Jiyong Park6a927c42020-01-21 02:03:43 +09001390 // We don't have prebuilt SDK for the specific sdkVersion.
1391 // Instead of breaking the build, fallback to use "system_current"
1392 ver = sdkVersionCurrent
1393 kind = sdkSystem
Sundong Ahn054b19a2018-10-19 13:46:09 +09001394 }
Jiyong Park6a927c42020-01-21 02:03:43 +09001395
1396 dir := filepath.Join("prebuilts", "sdk", ver.String(), kind.String())
Paul Duffin50061512020-01-21 16:31:05 +00001397 jar := filepath.Join(dir, baseName+".jar")
Sundong Ahn054b19a2018-10-19 13:46:09 +09001398 jarPath := android.ExistentPathForSource(ctx, jar)
Sundong Ahnae418ac2019-02-28 15:01:28 +09001399 if !jarPath.Valid() {
Colin Cross07c88562020-01-07 09:34:44 -08001400 if ctx.Config().AllowMissingDependencies() {
1401 return android.Paths{android.PathForSource(ctx, jar)}
1402 } else {
Jiyong Park6a927c42020-01-21 02:03:43 +09001403 ctx.PropertyErrorf("sdk_library", "invalid sdk version %q, %q does not exist", s.raw, jar)
Colin Cross07c88562020-01-07 09:34:44 -08001404 }
Sundong Ahnae418ac2019-02-28 15:01:28 +09001405 return nil
1406 }
Sundong Ahn054b19a2018-10-19 13:46:09 +09001407 return android.Paths{jarPath.Path()}
1408}
1409
Paul Duffinbf19a972020-05-26 13:21:35 +01001410// Get the apex name for module, "" if it is for platform.
1411func getApexNameForModule(module android.Module) string {
1412 if apex, ok := module.(android.ApexModule); ok {
1413 return apex.ApexName()
1414 }
1415
1416 return ""
1417}
1418
1419// Check to see if the other module is within the same named APEX as this module.
1420//
1421// If either this or the other module are on the platform then this will return
1422// false.
Paul Duffineedc5d52020-06-12 17:46:39 +01001423func withinSameApexAs(module android.ApexModule, other android.Module) bool {
Paul Duffinbf19a972020-05-26 13:21:35 +01001424 name := module.ApexName()
1425 return name != "" && getApexNameForModule(other) == name
1426}
1427
Paul Duffin47624362020-05-20 12:19:10 +01001428func (module *SdkLibrary) sdkJars(ctx android.BaseModuleContext, sdkVersion sdkSpec, headerJars bool) android.Paths {
Jiyong Park27fc4142020-05-28 00:19:53 +09001429 // If the client doesn't set sdk_version, but if this library prefers stubs over
1430 // the impl library, let's provide the widest API surface possible. To do so,
1431 // force override sdk_version to module_current so that the closest possible API
1432 // surface could be found in selectHeaderJarsForSdkVersion
1433 if module.defaultsToStubs() && !sdkVersion.specified() {
1434 sdkVersion = sdkSpecFrom("module_current")
1435 }
Paul Duffind1b3a922020-01-22 11:57:20 +00001436
Paul Duffin2e7ed652020-05-26 18:13:57 +01001437 // Only provide access to the implementation library if it is actually built.
1438 if module.requiresRuntimeImplementationLibrary() {
1439 // Check any special cases for java_sdk_library.
1440 //
1441 // Only allow access to the implementation library in the following condition:
1442 // * No sdk_version specified on the referencing module.
Paul Duffinbf19a972020-05-26 13:21:35 +01001443 // * The referencing module is in the same apex as this.
Paul Duffineedc5d52020-06-12 17:46:39 +01001444 if sdkVersion.kind == sdkPrivate || withinSameApexAs(module, ctx.Module()) {
Paul Duffin2e7ed652020-05-26 18:13:57 +01001445 if headerJars {
1446 return module.HeaderJars()
1447 } else {
1448 return module.ImplementationJars()
1449 }
Sundong Ahn054b19a2018-10-19 13:46:09 +09001450 }
Jiyong Parkc678ad32018-04-10 13:07:10 +09001451 }
Paul Duffin47624362020-05-20 12:19:10 +01001452
Paul Duffina3fb67d2020-05-20 14:20:02 +01001453 return module.selectHeaderJarsForSdkVersion(ctx, sdkVersion)
Jiyong Parkc678ad32018-04-10 13:07:10 +09001454}
1455
Sundong Ahn241cd372018-07-13 16:16:44 +09001456// to satisfy SdkLibraryDependency interface
Paul Duffind1b3a922020-01-22 11:57:20 +00001457func (module *SdkLibrary) SdkHeaderJars(ctx android.BaseModuleContext, sdkVersion sdkSpec) android.Paths {
1458 return module.sdkJars(ctx, sdkVersion, true /*headerJars*/)
1459}
1460
1461// to satisfy SdkLibraryDependency interface
Jiyong Park6a927c42020-01-21 02:03:43 +09001462func (module *SdkLibrary) SdkImplementationJars(ctx android.BaseModuleContext, sdkVersion sdkSpec) android.Paths {
Paul Duffind1b3a922020-01-22 11:57:20 +00001463 return module.sdkJars(ctx, sdkVersion, false /*headerJars*/)
Sundong Ahn241cd372018-07-13 16:16:44 +09001464}
1465
Sundong Ahn80a87b32019-05-13 15:02:50 +09001466func (module *SdkLibrary) SetNoDist() {
1467 module.sdkLibraryProperties.No_dist = proptools.BoolPtr(true)
1468}
1469
Colin Cross571cccf2019-02-04 11:22:08 -08001470var javaSdkLibrariesKey = android.NewOnceKey("javaSdkLibraries")
1471
Jiyong Park82484c02018-04-23 21:41:26 +09001472func javaSdkLibraries(config android.Config) *[]string {
Colin Cross571cccf2019-02-04 11:22:08 -08001473 return config.Once(javaSdkLibrariesKey, func() interface{} {
Jiyong Park82484c02018-04-23 21:41:26 +09001474 return &[]string{}
1475 }).(*[]string)
1476}
1477
Paul Duffin749f98f2019-12-30 17:23:46 +00001478func (module *SdkLibrary) getApiDir() string {
1479 return proptools.StringDefault(module.sdkLibraryProperties.Api_dir, "api")
1480}
1481
Jiyong Parkc678ad32018-04-10 13:07:10 +09001482// For a java_sdk_library module, create internal modules for stubs, docs,
1483// runtime libs and xml file. If requested, the stubs and docs are created twice
1484// once for public API level and once for system API level
Paul Duffin2aaef532020-04-29 16:47:28 +01001485func (module *SdkLibrary) CreateInternalModules(mctx android.DefaultableHookContext) {
1486 // If the module has been disabled then don't create any child modules.
1487 if !module.Enabled() {
1488 return
1489 }
1490
Paul Duffinc5d954a2020-05-16 18:54:24 +01001491 if len(module.properties.Srcs) == 0 {
Inseob Kimc0907f12019-02-08 21:00:45 +09001492 mctx.PropertyErrorf("srcs", "java_sdk_library must specify srcs")
Jooyung Han58f26ab2019-12-18 15:34:32 +09001493 return
Inseob Kimc0907f12019-02-08 21:00:45 +09001494 }
1495
Paul Duffin37e0b772019-12-30 17:20:10 +00001496 // If this builds against standard libraries (i.e. is not part of the core libraries)
1497 // then assume it provides both system and test apis. Otherwise, assume it does not and
1498 // also assume it does not contribute to the dist build.
1499 sdkDep := decodeSdkDep(mctx, sdkContext(&module.Library))
1500 hasSystemAndTestApis := sdkDep.hasStandardLibs()
Paul Duffin3a254982020-04-28 10:44:03 +01001501 module.sdkLibraryProperties.Generate_system_and_test_apis = hasSystemAndTestApis
Paul Duffin37e0b772019-12-30 17:20:10 +00001502 module.sdkLibraryProperties.No_dist = proptools.BoolPtr(!hasSystemAndTestApis)
1503
Inseob Kim8098faa2019-03-18 10:19:51 +09001504 missing_current_api := false
1505
Paul Duffin3a254982020-04-28 10:44:03 +01001506 generatedScopes := module.getGeneratedApiScopes(mctx)
Paul Duffind1b3a922020-01-22 11:57:20 +00001507
Paul Duffin749f98f2019-12-30 17:23:46 +00001508 apiDir := module.getApiDir()
Paul Duffin3a254982020-04-28 10:44:03 +01001509 for _, scope := range generatedScopes {
Inseob Kim8098faa2019-03-18 10:19:51 +09001510 for _, api := range []string{"current.txt", "removed.txt"} {
Paul Duffind1b3a922020-01-22 11:57:20 +00001511 path := path.Join(mctx.ModuleDir(), apiDir, scope.apiFilePrefix+api)
Inseob Kim8098faa2019-03-18 10:19:51 +09001512 p := android.ExistentPathForSource(mctx, path)
1513 if !p.Valid() {
1514 mctx.ModuleErrorf("Current api file %#v doesn't exist", path)
1515 missing_current_api = true
1516 }
1517 }
1518 }
1519
1520 if missing_current_api {
1521 script := "build/soong/scripts/gen-java-current-api-files.sh"
1522 p := android.ExistentPathForSource(mctx, script)
1523
1524 if !p.Valid() {
1525 panic(fmt.Sprintf("script file %s doesn't exist", script))
1526 }
1527
1528 mctx.ModuleErrorf("One or more current api files are missing. "+
1529 "You can update them by:\n"+
Paul Duffin37e0b772019-12-30 17:20:10 +00001530 "%s %q %s && m update-api",
Paul Duffind1b3a922020-01-22 11:57:20 +00001531 script, filepath.Join(mctx.ModuleDir(), apiDir),
Paul Duffin3a254982020-04-28 10:44:03 +01001532 strings.Join(generatedScopes.Strings(func(s *apiScope) string { return s.apiFilePrefix }), " "))
Inseob Kim8098faa2019-03-18 10:19:51 +09001533 return
1534 }
1535
Paul Duffin3a254982020-04-28 10:44:03 +01001536 for _, scope := range generatedScopes {
Paul Duffina377e4c2020-04-29 13:30:54 +01001537 stubsSourceArgs := scope.droidstubsArgsForGeneratingStubsSource
Paul Duffinb74ee3e2020-05-08 14:16:20 +01001538 stubsSourceModuleName := module.stubsSourceModuleName(scope)
Paul Duffina377e4c2020-04-29 13:30:54 +01001539
1540 // If the args needed to generate the stubs and API are the same then they
1541 // can be generated in a single invocation of metalava, otherwise they will
1542 // need separate invocations.
1543 if scope.createStubsSourceAndApiTogether {
1544 // Use the stubs source name for legacy reasons.
1545 module.createStubsSourcesAndApi(mctx, scope, stubsSourceModuleName, true, true, stubsSourceArgs)
1546 } else {
1547 module.createStubsSourcesAndApi(mctx, scope, stubsSourceModuleName, true, false, stubsSourceArgs)
1548
1549 apiArgs := scope.droidstubsArgsForGeneratingApi
Paul Duffinb74ee3e2020-05-08 14:16:20 +01001550 apiName := module.apiModuleName(scope)
Paul Duffina377e4c2020-04-29 13:30:54 +01001551 module.createStubsSourcesAndApi(mctx, scope, apiName, false, true, apiArgs)
1552 }
1553
Paul Duffind1b3a922020-01-22 11:57:20 +00001554 module.createStubsLibrary(mctx, scope)
Inseob Kimc0907f12019-02-08 21:00:45 +09001555 }
1556
Paul Duffind11e78e2020-05-15 20:37:11 +01001557 if module.requiresRuntimeImplementationLibrary() {
Paul Duffin9d582cc2020-05-16 15:52:12 +01001558 // Create child module to create an implementation library.
1559 //
1560 // This temporarily creates a second implementation library that can be explicitly
1561 // referenced.
1562 //
1563 // TODO(b/156618935) - update comment once only one implementation library is created.
1564 module.createImplLibrary(mctx)
1565
Paul Duffind11e78e2020-05-15 20:37:11 +01001566 // Only create an XML permissions file that declares the library as being usable
1567 // as a shared library if required.
1568 if module.sharedLibrary() {
1569 module.createXmlFile(mctx)
1570 }
Paul Duffin43db9be2019-12-30 17:35:49 +00001571
1572 // record java_sdk_library modules so that they are exported to make
1573 javaSdkLibraries := javaSdkLibraries(mctx.Config())
1574 javaSdkLibrariesLock.Lock()
1575 defer javaSdkLibrariesLock.Unlock()
1576 *javaSdkLibraries = append(*javaSdkLibraries, module.BaseModuleName())
1577 }
Inseob Kimc0907f12019-02-08 21:00:45 +09001578}
1579
1580func (module *SdkLibrary) InitSdkLibraryProperties() {
Colin Crossce6734e2020-06-15 16:09:53 -07001581 module.addHostAndDeviceProperties()
1582 module.AddProperties(&module.sdkLibraryProperties)
Sundong Ahn054b19a2018-10-19 13:46:09 +09001583
Paul Duffin64e61992020-05-15 10:20:31 +01001584 module.initSdkLibraryComponent(&module.ModuleBase)
1585
Paul Duffinc5d954a2020-05-16 18:54:24 +01001586 module.properties.Installable = proptools.BoolPtr(true)
1587 module.deviceProperties.IsSDKLibrary = true
Inseob Kimc0907f12019-02-08 21:00:45 +09001588}
Sundong Ahn054b19a2018-10-19 13:46:09 +09001589
Paul Duffind11e78e2020-05-15 20:37:11 +01001590func (module *SdkLibrary) requiresRuntimeImplementationLibrary() bool {
1591 return !proptools.Bool(module.sdkLibraryProperties.Api_only)
1592}
1593
Jiyong Park27fc4142020-05-28 00:19:53 +09001594func (module *SdkLibrary) defaultsToStubs() bool {
1595 return proptools.Bool(module.sdkLibraryProperties.Default_to_stubs)
1596}
1597
Paul Duffin1a724e62020-05-08 13:44:43 +01001598// Defines how to name the individual component modules the sdk library creates.
1599type sdkLibraryComponentNamingScheme interface {
1600 stubsLibraryModuleName(scope *apiScope, baseName string) string
1601
1602 stubsSourceModuleName(scope *apiScope, baseName string) string
1603
1604 apiModuleName(scope *apiScope, baseName string) string
1605}
1606
1607type defaultNamingScheme struct {
1608}
1609
1610func (s *defaultNamingScheme) stubsLibraryModuleName(scope *apiScope, baseName string) string {
1611 return scope.stubsLibraryModuleName(baseName)
1612}
1613
1614func (s *defaultNamingScheme) stubsSourceModuleName(scope *apiScope, baseName string) string {
1615 return scope.stubsSourceModuleName(baseName)
1616}
1617
1618func (s *defaultNamingScheme) apiModuleName(scope *apiScope, baseName string) string {
1619 return scope.apiModuleName(baseName)
1620}
1621
1622var _ sdkLibraryComponentNamingScheme = (*defaultNamingScheme)(nil)
1623
Paul Duffindef8a892020-05-08 15:36:30 +01001624type frameworkModulesNamingScheme struct {
1625}
1626
1627func (s *frameworkModulesNamingScheme) moduleSuffix(scope *apiScope) string {
1628 suffix := scope.name
1629 if scope == apiScopeModuleLib {
1630 suffix = "module_libs_"
1631 }
1632 return suffix
1633}
1634
1635func (s *frameworkModulesNamingScheme) stubsLibraryModuleName(scope *apiScope, baseName string) string {
1636 return fmt.Sprintf("%s-stubs-%sapi", baseName, s.moduleSuffix(scope))
1637}
1638
1639func (s *frameworkModulesNamingScheme) stubsSourceModuleName(scope *apiScope, baseName string) string {
1640 return fmt.Sprintf("%s-stubs-srcs-%sapi", baseName, s.moduleSuffix(scope))
1641}
1642
1643func (s *frameworkModulesNamingScheme) apiModuleName(scope *apiScope, baseName string) string {
1644 return fmt.Sprintf("%s-api-%sapi", baseName, s.moduleSuffix(scope))
1645}
1646
1647var _ sdkLibraryComponentNamingScheme = (*frameworkModulesNamingScheme)(nil)
1648
Anton Hansson0bd88d02020-05-25 12:20:51 +01001649func moduleStubLinkType(name string) (stub bool, ret linkType) {
1650 // This suffix-based approach is fragile and could potentially mis-trigger.
1651 // TODO(b/155164730): Clean this up when modules no longer reference sdk_lib stubs directly.
1652 if strings.HasSuffix(name, ".stubs.public") || strings.HasSuffix(name, "-stubs-publicapi") {
1653 return true, javaSdk
1654 }
1655 if strings.HasSuffix(name, ".stubs.system") || strings.HasSuffix(name, "-stubs-systemapi") {
1656 return true, javaSystem
1657 }
1658 if strings.HasSuffix(name, ".stubs.module_lib") || strings.HasSuffix(name, "-stubs-module_libs_api") {
1659 return true, javaModule
1660 }
1661 if strings.HasSuffix(name, ".stubs.test") {
1662 return true, javaSystem
1663 }
1664 return false, javaPlatform
1665}
1666
Jaewoong Jung4f158ee2019-07-11 10:05:35 -07001667// java_sdk_library is a special Java library that provides optional platform APIs to apps.
1668// In practice, it can be viewed as a combination of several modules: 1) stubs library that clients
1669// are linked against to, 2) droiddoc module that internally generates API stubs source files,
1670// 3) the real runtime shared library that implements the APIs, and 4) XML file for adding
1671// the runtime lib to the classpath at runtime if requested via <uses-library>.
Inseob Kimc0907f12019-02-08 21:00:45 +09001672func SdkLibraryFactory() android.Module {
1673 module := &SdkLibrary{}
Paul Duffinb74ee3e2020-05-08 14:16:20 +01001674
1675 // Initialize information common between source and prebuilt.
1676 module.initCommon(&module.ModuleBase)
1677
Inseob Kimc0907f12019-02-08 21:00:45 +09001678 module.InitSdkLibraryProperties()
Jooyung Han58f26ab2019-12-18 15:34:32 +09001679 android.InitApexModule(module)
Sundong Ahn054b19a2018-10-19 13:46:09 +09001680 InitJavaModule(module, android.HostAndDeviceSupported)
Paul Duffin3a254982020-04-28 10:44:03 +01001681
1682 // Initialize the map from scope to scope specific properties.
1683 scopeToProperties := make(map[*apiScope]*ApiScopeProperties)
1684 for _, scope := range allApiScopes {
1685 scopeToProperties[scope] = scope.scopeSpecificProperties(module)
1686 }
1687 module.scopeToProperties = scopeToProperties
1688
Paul Duffin344c4ee2020-04-29 23:35:13 +01001689 // Add the properties containing visibility rules so that they are checked.
Paul Duffin9d582cc2020-05-16 15:52:12 +01001690 android.AddVisibilityProperty(module, "impl_library_visibility", &module.sdkLibraryProperties.Impl_library_visibility)
Paul Duffin344c4ee2020-04-29 23:35:13 +01001691 android.AddVisibilityProperty(module, "stubs_library_visibility", &module.sdkLibraryProperties.Stubs_library_visibility)
1692 android.AddVisibilityProperty(module, "stubs_source_visibility", &module.sdkLibraryProperties.Stubs_source_visibility)
1693
Paul Duffin1a724e62020-05-08 13:44:43 +01001694 module.SetDefaultableHook(func(ctx android.DefaultableHookContext) {
Paul Duffind11e78e2020-05-15 20:37:11 +01001695 // If no implementation is required then it cannot be used as a shared library
1696 // either.
1697 if !module.requiresRuntimeImplementationLibrary() {
1698 // If shared_library has been explicitly set to true then it is incompatible
1699 // with api_only: true.
1700 if proptools.Bool(module.commonSdkLibraryProperties.Shared_library) {
1701 ctx.PropertyErrorf("api_only/shared_library", "inconsistent settings, shared_library and api_only cannot both be true")
1702 }
1703 // Set shared_library: false.
1704 module.commonSdkLibraryProperties.Shared_library = proptools.BoolPtr(false)
1705 }
1706
Paul Duffin1a724e62020-05-08 13:44:43 +01001707 if module.initCommonAfterDefaultsApplied(ctx) {
1708 module.CreateInternalModules(ctx)
1709 }
1710 })
Jiyong Parkc678ad32018-04-10 13:07:10 +09001711 return module
1712}
Colin Cross79c7c262019-04-17 11:11:46 -07001713
1714//
1715// SDK library prebuilts
1716//
1717
Paul Duffin56d44902020-01-31 13:36:25 +00001718// Properties associated with each api scope.
1719type sdkLibraryScopeProperties struct {
Colin Cross79c7c262019-04-17 11:11:46 -07001720 Jars []string `android:"path"`
1721
1722 Sdk_version *string
1723
Colin Cross79c7c262019-04-17 11:11:46 -07001724 // List of shared java libs that this module has dependencies to
1725 Libs []string
Paul Duffinf488ef22020-04-09 00:10:17 +01001726
Paul Duffin5fb82132020-04-29 20:45:27 +01001727 // The stubs source.
Paul Duffinf488ef22020-04-09 00:10:17 +01001728 Stub_srcs []string `android:"path"`
Paul Duffin75dcc802020-04-09 01:08:11 +01001729
1730 // The current.txt
Paul Duffin533f9c72020-05-20 16:18:00 +01001731 Current_api *string `android:"path"`
Paul Duffin75dcc802020-04-09 01:08:11 +01001732
1733 // The removed.txt
Paul Duffin533f9c72020-05-20 16:18:00 +01001734 Removed_api *string `android:"path"`
Colin Cross79c7c262019-04-17 11:11:46 -07001735}
1736
Paul Duffin56d44902020-01-31 13:36:25 +00001737type sdkLibraryImportProperties struct {
Paul Duffinfcfd7912020-01-31 17:54:30 +00001738 // List of shared java libs, common to all scopes, that this module has
1739 // dependencies to
1740 Libs []string
Paul Duffin56d44902020-01-31 13:36:25 +00001741}
1742
Paul Duffineedc5d52020-06-12 17:46:39 +01001743type SdkLibraryImport struct {
Colin Cross79c7c262019-04-17 11:11:46 -07001744 android.ModuleBase
1745 android.DefaultableModuleBase
1746 prebuilt android.Prebuilt
Paul Duffin61871622020-02-10 13:37:10 +00001747 android.ApexModuleBase
1748 android.SdkBase
Colin Cross79c7c262019-04-17 11:11:46 -07001749
1750 properties sdkLibraryImportProperties
1751
Paul Duffin6a2bd112020-04-07 19:27:04 +01001752 // Map from api scope to the scope specific property structure.
1753 scopeProperties map[*apiScope]*sdkLibraryScopeProperties
1754
Paul Duffin56d44902020-01-31 13:36:25 +00001755 commonToSdkLibraryAndImport
Paul Duffineedc5d52020-06-12 17:46:39 +01001756
1757 // The reference to the implementation library created by the source module.
1758 // Is nil if the source module does not exist.
1759 implLibraryModule *Library
1760
1761 // The reference to the xml permissions module created by the source module.
1762 // Is nil if the source module does not exist.
1763 xmlPermissionsFileModule *sdkLibraryXml
Colin Cross79c7c262019-04-17 11:11:46 -07001764}
1765
Paul Duffineedc5d52020-06-12 17:46:39 +01001766var _ SdkLibraryDependency = (*SdkLibraryImport)(nil)
Colin Cross79c7c262019-04-17 11:11:46 -07001767
Paul Duffin6a2bd112020-04-07 19:27:04 +01001768// The type of a structure that contains a field of type sdkLibraryScopeProperties
1769// for each apiscope in allApiScopes, e.g. something like:
1770// struct {
1771// Public sdkLibraryScopeProperties
1772// System sdkLibraryScopeProperties
1773// ...
1774// }
1775var allScopeStructType = createAllScopePropertiesStructType()
1776
1777// Dynamically create a structure type for each apiscope in allApiScopes.
1778func createAllScopePropertiesStructType() reflect.Type {
1779 var fields []reflect.StructField
1780 for _, apiScope := range allApiScopes {
1781 field := reflect.StructField{
1782 Name: apiScope.fieldName,
1783 Type: reflect.TypeOf(sdkLibraryScopeProperties{}),
1784 }
1785 fields = append(fields, field)
1786 }
1787
1788 return reflect.StructOf(fields)
1789}
1790
1791// Create an instance of the scope specific structure type and return a map
1792// from apiscope to a pointer to each scope specific field.
1793func createPropertiesInstance() (interface{}, map[*apiScope]*sdkLibraryScopeProperties) {
1794 allScopePropertiesPtr := reflect.New(allScopeStructType)
1795 allScopePropertiesStruct := allScopePropertiesPtr.Elem()
1796 scopeProperties := make(map[*apiScope]*sdkLibraryScopeProperties)
1797
1798 for _, apiScope := range allApiScopes {
1799 field := allScopePropertiesStruct.FieldByName(apiScope.fieldName)
1800 scopeProperties[apiScope] = field.Addr().Interface().(*sdkLibraryScopeProperties)
1801 }
1802
1803 return allScopePropertiesPtr.Interface(), scopeProperties
1804}
1805
Jaewoong Jung4f158ee2019-07-11 10:05:35 -07001806// java_sdk_library_import imports a prebuilt java_sdk_library.
Colin Cross79c7c262019-04-17 11:11:46 -07001807func sdkLibraryImportFactory() android.Module {
Paul Duffineedc5d52020-06-12 17:46:39 +01001808 module := &SdkLibraryImport{}
Colin Cross79c7c262019-04-17 11:11:46 -07001809
Paul Duffin6a2bd112020-04-07 19:27:04 +01001810 allScopeProperties, scopeToProperties := createPropertiesInstance()
1811 module.scopeProperties = scopeToProperties
1812 module.AddProperties(&module.properties, allScopeProperties)
Colin Cross79c7c262019-04-17 11:11:46 -07001813
Paul Duffinb74ee3e2020-05-08 14:16:20 +01001814 // Initialize information common between source and prebuilt.
1815 module.initCommon(&module.ModuleBase)
1816
Paul Duffin0bdcb272020-02-06 15:24:57 +00001817 android.InitPrebuiltModule(module, &[]string{""})
Paul Duffin61871622020-02-10 13:37:10 +00001818 android.InitApexModule(module)
1819 android.InitSdkAwareModule(module)
Colin Cross79c7c262019-04-17 11:11:46 -07001820 InitJavaModule(module, android.HostAndDeviceSupported)
1821
Paul Duffin1a724e62020-05-08 13:44:43 +01001822 module.SetDefaultableHook(func(mctx android.DefaultableHookContext) {
1823 if module.initCommonAfterDefaultsApplied(mctx) {
1824 module.createInternalModules(mctx)
1825 }
1826 })
Colin Cross79c7c262019-04-17 11:11:46 -07001827 return module
1828}
1829
Paul Duffineedc5d52020-06-12 17:46:39 +01001830func (module *SdkLibraryImport) Prebuilt() *android.Prebuilt {
Colin Cross79c7c262019-04-17 11:11:46 -07001831 return &module.prebuilt
1832}
1833
Paul Duffineedc5d52020-06-12 17:46:39 +01001834func (module *SdkLibraryImport) Name() string {
Colin Cross79c7c262019-04-17 11:11:46 -07001835 return module.prebuilt.Name(module.ModuleBase.Name())
1836}
1837
Paul Duffineedc5d52020-06-12 17:46:39 +01001838func (module *SdkLibraryImport) createInternalModules(mctx android.DefaultableHookContext) {
Colin Cross79c7c262019-04-17 11:11:46 -07001839
Paul Duffin50061512020-01-21 16:31:05 +00001840 // If the build is configured to use prebuilts then force this to be preferred.
1841 if mctx.Config().UnbundledBuildUsePrebuiltSdks() {
1842 module.prebuilt.ForcePrefer()
1843 }
1844
Paul Duffin6a2bd112020-04-07 19:27:04 +01001845 for apiScope, scopeProperties := range module.scopeProperties {
Paul Duffin56d44902020-01-31 13:36:25 +00001846 if len(scopeProperties.Jars) == 0 {
1847 continue
1848 }
1849
Paul Duffinf6155722020-04-09 00:07:11 +01001850 module.createJavaImportForStubs(mctx, apiScope, scopeProperties)
Paul Duffinf488ef22020-04-09 00:10:17 +01001851
Paul Duffin533f9c72020-05-20 16:18:00 +01001852 if len(scopeProperties.Stub_srcs) > 0 {
1853 module.createPrebuiltStubsSources(mctx, apiScope, scopeProperties)
1854 }
Paul Duffin56d44902020-01-31 13:36:25 +00001855 }
Colin Cross79c7c262019-04-17 11:11:46 -07001856
1857 javaSdkLibraries := javaSdkLibraries(mctx.Config())
1858 javaSdkLibrariesLock.Lock()
1859 defer javaSdkLibrariesLock.Unlock()
1860 *javaSdkLibraries = append(*javaSdkLibraries, module.BaseModuleName())
1861}
1862
Paul Duffineedc5d52020-06-12 17:46:39 +01001863func (module *SdkLibraryImport) createJavaImportForStubs(mctx android.DefaultableHookContext, apiScope *apiScope, scopeProperties *sdkLibraryScopeProperties) {
Paul Duffinf6155722020-04-09 00:07:11 +01001864 // Creates a java import for the jar with ".stubs" suffix
1865 props := struct {
Paul Duffind41712d2020-05-16 09:57:59 +01001866 Name *string
1867 Sdk_version *string
1868 Libs []string
1869 Jars []string
1870 Prefer *bool
Paul Duffinf6155722020-04-09 00:07:11 +01001871 }{}
Paul Duffinb74ee3e2020-05-08 14:16:20 +01001872 props.Name = proptools.StringPtr(module.stubsLibraryModuleName(apiScope))
Paul Duffinf6155722020-04-09 00:07:11 +01001873 props.Sdk_version = scopeProperties.Sdk_version
1874 // Prepend any of the libs from the legacy public properties to the libs for each of the
1875 // scopes to avoid having to duplicate them in each scope.
1876 props.Libs = append(module.properties.Libs, scopeProperties.Libs...)
1877 props.Jars = scopeProperties.Jars
Paul Duffind41712d2020-05-16 09:57:59 +01001878
Paul Duffindd89a282020-05-13 16:08:09 +01001879 // The imports are preferred if the java_sdk_library_import is preferred.
1880 props.Prefer = proptools.BoolPtr(module.prebuilt.Prefer())
Paul Duffin64e61992020-05-15 10:20:31 +01001881
1882 mctx.CreateModule(ImportFactory, &props, module.sdkComponentPropertiesForChildLibrary())
Paul Duffinf6155722020-04-09 00:07:11 +01001883}
1884
Paul Duffineedc5d52020-06-12 17:46:39 +01001885func (module *SdkLibraryImport) createPrebuiltStubsSources(mctx android.DefaultableHookContext, apiScope *apiScope, scopeProperties *sdkLibraryScopeProperties) {
Paul Duffinf488ef22020-04-09 00:10:17 +01001886 props := struct {
Paul Duffindd89a282020-05-13 16:08:09 +01001887 Name *string
1888 Srcs []string
1889 Prefer *bool
Paul Duffinf488ef22020-04-09 00:10:17 +01001890 }{}
Paul Duffinb74ee3e2020-05-08 14:16:20 +01001891 props.Name = proptools.StringPtr(module.stubsSourceModuleName(apiScope))
Paul Duffinf488ef22020-04-09 00:10:17 +01001892 props.Srcs = scopeProperties.Stub_srcs
1893 mctx.CreateModule(PrebuiltStubsSourcesFactory, &props)
Paul Duffindd89a282020-05-13 16:08:09 +01001894
1895 // The stubs source is preferred if the java_sdk_library_import is preferred.
1896 props.Prefer = proptools.BoolPtr(module.prebuilt.Prefer())
Paul Duffinf488ef22020-04-09 00:10:17 +01001897}
1898
Paul Duffin44f1d842020-06-26 20:17:02 +01001899// Add the dependencies on the child module in the component deps mutator so that it
1900// creates references to the prebuilt and not the source modules.
1901func (module *SdkLibraryImport) ComponentDepsMutator(ctx android.BottomUpMutatorContext) {
Paul Duffin6a2bd112020-04-07 19:27:04 +01001902 for apiScope, scopeProperties := range module.scopeProperties {
Paul Duffin56d44902020-01-31 13:36:25 +00001903 if len(scopeProperties.Jars) == 0 {
1904 continue
1905 }
1906
1907 // Add dependencies to the prebuilt stubs library
Paul Duffin44f1d842020-06-26 20:17:02 +01001908 ctx.AddVariationDependencies(nil, apiScope.stubsTag, "prebuilt_"+module.stubsLibraryModuleName(apiScope))
Paul Duffin533f9c72020-05-20 16:18:00 +01001909
1910 if len(scopeProperties.Stub_srcs) > 0 {
1911 // Add dependencies to the prebuilt stubs source library
Paul Duffin44f1d842020-06-26 20:17:02 +01001912 ctx.AddVariationDependencies(nil, apiScope.stubsSourceTag, "prebuilt_"+module.stubsSourceModuleName(apiScope))
Paul Duffin533f9c72020-05-20 16:18:00 +01001913 }
Paul Duffin56d44902020-01-31 13:36:25 +00001914 }
Paul Duffin44f1d842020-06-26 20:17:02 +01001915}
1916
1917// Add other dependencies as normal.
1918func (module *SdkLibraryImport) DepsMutator(ctx android.BottomUpMutatorContext) {
Paul Duffineedc5d52020-06-12 17:46:39 +01001919
1920 implName := module.implLibraryModuleName()
1921 if ctx.OtherModuleExists(implName) {
1922 ctx.AddVariationDependencies(nil, implLibraryTag, implName)
1923
1924 xmlPermissionsModuleName := module.xmlPermissionsModuleName()
1925 if module.sharedLibrary() && ctx.OtherModuleExists(xmlPermissionsModuleName) {
1926 // Add dependency to the rule for generating the xml permissions file
1927 ctx.AddDependency(module, xmlPermissionsFileTag, xmlPermissionsModuleName)
1928 }
1929 }
Colin Cross79c7c262019-04-17 11:11:46 -07001930}
1931
Paul Duffineedc5d52020-06-12 17:46:39 +01001932func (module *SdkLibraryImport) DepIsInSameApex(mctx android.BaseModuleContext, dep android.Module) bool {
1933 depTag := mctx.OtherModuleDependencyTag(dep)
1934 if depTag == xmlPermissionsFileTag {
1935 return true
1936 }
1937
1938 // None of the other dependencies of the java_sdk_library_import are in the same apex
1939 // as the one that references this module.
1940 return false
1941}
1942
Jooyung Han749dc692020-04-15 11:03:39 +09001943func (module *SdkLibraryImport) ShouldSupportSdkVersion(ctx android.BaseModuleContext, sdkVersion int) error {
1944 // we don't check prebuilt modules for sdk_version
1945 return nil
1946}
1947
Paul Duffineedc5d52020-06-12 17:46:39 +01001948func (module *SdkLibraryImport) OutputFiles(tag string) (android.Paths, error) {
Paul Duffin46fdda82020-05-14 15:39:10 +01001949 return module.commonOutputFiles(tag)
1950}
1951
Paul Duffineedc5d52020-06-12 17:46:39 +01001952func (module *SdkLibraryImport) GenerateAndroidBuildActions(ctx android.ModuleContext) {
Paul Duffin533f9c72020-05-20 16:18:00 +01001953 // Record the paths to the prebuilt stubs library and stubs source.
Colin Cross79c7c262019-04-17 11:11:46 -07001954 ctx.VisitDirectDeps(func(to android.Module) {
1955 tag := ctx.OtherModuleDependencyTag(to)
1956
Paul Duffin533f9c72020-05-20 16:18:00 +01001957 // Extract information from any of the scope specific dependencies.
1958 if scopeTag, ok := tag.(scopeDependencyTag); ok {
1959 apiScope := scopeTag.apiScope
1960 scopePaths := module.getScopePathsCreateIfNeeded(apiScope)
1961
1962 // Extract information from the dependency. The exact information extracted
1963 // is determined by the nature of the dependency which is determined by the tag.
1964 scopeTag.extractDepInfo(ctx, to, scopePaths)
Paul Duffineedc5d52020-06-12 17:46:39 +01001965 } else if tag == implLibraryTag {
1966 if implLibrary, ok := to.(*Library); ok {
1967 module.implLibraryModule = implLibrary
1968 } else {
1969 ctx.ModuleErrorf("implementation library must be of type *java.Library but was %T", to)
1970 }
1971 } else if tag == xmlPermissionsFileTag {
1972 if xmlPermissionsFileModule, ok := to.(*sdkLibraryXml); ok {
1973 module.xmlPermissionsFileModule = xmlPermissionsFileModule
1974 } else {
1975 ctx.ModuleErrorf("xml permissions file module must be of type *sdkLibraryXml but was %T", to)
1976 }
Colin Cross79c7c262019-04-17 11:11:46 -07001977 }
1978 })
Paul Duffin533f9c72020-05-20 16:18:00 +01001979
1980 // Populate the scope paths with information from the properties.
1981 for apiScope, scopeProperties := range module.scopeProperties {
1982 if len(scopeProperties.Jars) == 0 {
1983 continue
1984 }
1985
1986 paths := module.getScopePathsCreateIfNeeded(apiScope)
1987 paths.currentApiFilePath = android.OptionalPathForModuleSrc(ctx, scopeProperties.Current_api)
1988 paths.removedApiFilePath = android.OptionalPathForModuleSrc(ctx, scopeProperties.Removed_api)
1989 }
Colin Cross79c7c262019-04-17 11:11:46 -07001990}
1991
Paul Duffineedc5d52020-06-12 17:46:39 +01001992func (module *SdkLibraryImport) sdkJars(ctx android.BaseModuleContext, sdkVersion sdkSpec, headerJars bool) android.Paths {
1993
1994 // For consistency with SdkLibrary make the implementation jar available to libraries that
1995 // are within the same APEX.
1996 implLibraryModule := module.implLibraryModule
1997 if implLibraryModule != nil && withinSameApexAs(module, ctx.Module()) {
1998 if headerJars {
1999 return implLibraryModule.HeaderJars()
2000 } else {
2001 return implLibraryModule.ImplementationJars()
2002 }
2003 }
2004
Paul Duffina3fb67d2020-05-20 14:20:02 +01002005 return module.selectHeaderJarsForSdkVersion(ctx, sdkVersion)
Paul Duffin56d44902020-01-31 13:36:25 +00002006}
2007
Colin Cross79c7c262019-04-17 11:11:46 -07002008// to satisfy SdkLibraryDependency interface
Paul Duffineedc5d52020-06-12 17:46:39 +01002009func (module *SdkLibraryImport) SdkHeaderJars(ctx android.BaseModuleContext, sdkVersion sdkSpec) android.Paths {
Colin Cross79c7c262019-04-17 11:11:46 -07002010 // This module is just a wrapper for the prebuilt stubs.
Paul Duffineedc5d52020-06-12 17:46:39 +01002011 return module.sdkJars(ctx, sdkVersion, true)
Colin Cross79c7c262019-04-17 11:11:46 -07002012}
2013
2014// to satisfy SdkLibraryDependency interface
Paul Duffineedc5d52020-06-12 17:46:39 +01002015func (module *SdkLibraryImport) SdkImplementationJars(ctx android.BaseModuleContext, sdkVersion sdkSpec) android.Paths {
Colin Cross79c7c262019-04-17 11:11:46 -07002016 // This module is just a wrapper for the stubs.
Paul Duffineedc5d52020-06-12 17:46:39 +01002017 return module.sdkJars(ctx, sdkVersion, false)
2018}
2019
2020// to satisfy apex.javaDependency interface
2021func (module *SdkLibraryImport) DexJarBuildPath() android.Path {
2022 if module.implLibraryModule == nil {
2023 return nil
2024 } else {
2025 return module.implLibraryModule.DexJarBuildPath()
2026 }
2027}
2028
2029// to satisfy apex.javaDependency interface
2030func (module *SdkLibraryImport) JacocoReportClassesFile() android.Path {
2031 if module.implLibraryModule == nil {
2032 return nil
2033 } else {
2034 return module.implLibraryModule.JacocoReportClassesFile()
2035 }
2036}
2037
2038// to satisfy apex.javaDependency interface
2039func (module *SdkLibraryImport) Stem() string {
2040 return module.BaseModuleName()
Colin Cross79c7c262019-04-17 11:11:46 -07002041}
Jiyong Parke3833882020-02-17 17:28:10 +09002042
Paul Duffin44b481b2020-06-17 16:59:43 +01002043var _ ApexDependency = (*SdkLibraryImport)(nil)
2044
2045// to satisfy java.ApexDependency interface
2046func (module *SdkLibraryImport) HeaderJars() android.Paths {
2047 if module.implLibraryModule == nil {
2048 return nil
2049 } else {
2050 return module.implLibraryModule.HeaderJars()
2051 }
2052}
2053
2054// to satisfy java.ApexDependency interface
2055func (module *SdkLibraryImport) ImplementationAndResourcesJars() android.Paths {
2056 if module.implLibraryModule == nil {
2057 return nil
2058 } else {
2059 return module.implLibraryModule.ImplementationAndResourcesJars()
2060 }
2061}
2062
Jiyong Parke3833882020-02-17 17:28:10 +09002063//
2064// java_sdk_library_xml
2065//
2066type sdkLibraryXml struct {
2067 android.ModuleBase
2068 android.DefaultableModuleBase
2069 android.ApexModuleBase
2070
2071 properties sdkLibraryXmlProperties
2072
2073 outputFilePath android.OutputPath
2074 installDirPath android.InstallPath
2075}
2076
2077type sdkLibraryXmlProperties struct {
2078 // canonical name of the lib
2079 Lib_name *string
2080}
2081
2082// java_sdk_library_xml builds the permission xml file for a java_sdk_library.
2083// Not to be used directly by users. java_sdk_library internally uses this.
2084func sdkLibraryXmlFactory() android.Module {
2085 module := &sdkLibraryXml{}
2086
2087 module.AddProperties(&module.properties)
2088
2089 android.InitApexModule(module)
2090 android.InitAndroidArchModule(module, android.DeviceSupported, android.MultilibCommon)
2091
2092 return module
2093}
2094
2095// from android.PrebuiltEtcModule
2096func (module *sdkLibraryXml) SubDir() string {
2097 return "permissions"
2098}
2099
2100// from android.PrebuiltEtcModule
2101func (module *sdkLibraryXml) OutputFile() android.OutputPath {
2102 return module.outputFilePath
2103}
2104
2105// from android.ApexModule
2106func (module *sdkLibraryXml) AvailableFor(what string) bool {
2107 return true
2108}
2109
2110func (module *sdkLibraryXml) DepsMutator(ctx android.BottomUpMutatorContext) {
2111 // do nothing
2112}
2113
Jooyung Han749dc692020-04-15 11:03:39 +09002114func (module *sdkLibraryXml) ShouldSupportSdkVersion(ctx android.BaseModuleContext, sdkVersion int) error {
2115 // sdkLibraryXml doesn't need to be checked separately because java_sdk_library is checked
2116 return nil
2117}
2118
Jiyong Parke3833882020-02-17 17:28:10 +09002119// File path to the runtime implementation library
2120func (module *sdkLibraryXml) implPath() string {
2121 implName := proptools.String(module.properties.Lib_name)
2122 if apexName := module.ApexName(); apexName != "" {
2123 // TODO(b/146468504): ApexName() is only a soong module name, not apex name.
2124 // In most cases, this works fine. But when apex_name is set or override_apex is used
2125 // this can be wrong.
2126 return fmt.Sprintf("/apex/%s/javalib/%s.jar", apexName, implName)
2127 }
2128 partition := "system"
2129 if module.SocSpecific() {
2130 partition = "vendor"
2131 } else if module.DeviceSpecific() {
2132 partition = "odm"
2133 } else if module.ProductSpecific() {
2134 partition = "product"
2135 } else if module.SystemExtSpecific() {
2136 partition = "system_ext"
2137 }
2138 return "/" + partition + "/framework/" + implName + ".jar"
2139}
2140
2141func (module *sdkLibraryXml) GenerateAndroidBuildActions(ctx android.ModuleContext) {
2142 libName := proptools.String(module.properties.Lib_name)
2143 xmlContent := fmt.Sprintf(permissionsTemplate, libName, module.implPath())
2144
2145 module.outputFilePath = android.PathForModuleOut(ctx, libName+".xml").OutputPath
2146 rule := android.NewRuleBuilder()
2147 rule.Command().
2148 Text("/bin/bash -c \"echo -e '" + xmlContent + "'\" > ").
2149 Output(module.outputFilePath)
2150
2151 rule.Build(pctx, ctx, "java_sdk_xml", "Permission XML")
2152
2153 module.installDirPath = android.PathForModuleInstall(ctx, "etc", module.SubDir())
2154}
2155
2156func (module *sdkLibraryXml) AndroidMkEntries() []android.AndroidMkEntries {
2157 if !module.IsForPlatform() {
2158 return []android.AndroidMkEntries{android.AndroidMkEntries{
2159 Disabled: true,
2160 }}
2161 }
2162
2163 return []android.AndroidMkEntries{android.AndroidMkEntries{
2164 Class: "ETC",
2165 OutputFile: android.OptionalPathForPath(module.outputFilePath),
2166 ExtraEntries: []android.AndroidMkExtraEntriesFunc{
2167 func(entries *android.AndroidMkEntries) {
2168 entries.SetString("LOCAL_MODULE_TAGS", "optional")
2169 entries.SetString("LOCAL_MODULE_PATH", module.installDirPath.ToMakePath().String())
2170 entries.SetString("LOCAL_INSTALLED_MODULE_STEM", module.outputFilePath.Base())
2171 },
2172 },
2173 }}
2174}
Paul Duffin61871622020-02-10 13:37:10 +00002175
2176type sdkLibrarySdkMemberType struct {
2177 android.SdkMemberTypeBase
2178}
2179
2180func (s *sdkLibrarySdkMemberType) AddDependencies(mctx android.BottomUpMutatorContext, dependencyTag blueprint.DependencyTag, names []string) {
2181 mctx.AddVariationDependencies(nil, dependencyTag, names...)
2182}
2183
2184func (s *sdkLibrarySdkMemberType) IsInstance(module android.Module) bool {
2185 _, ok := module.(*SdkLibrary)
2186 return ok
2187}
2188
2189func (s *sdkLibrarySdkMemberType) AddPrebuiltModule(ctx android.SdkMemberContext, member android.SdkMember) android.BpModule {
2190 return ctx.SnapshotBuilder().AddPrebuiltModule(member, "java_sdk_library_import")
2191}
2192
2193func (s *sdkLibrarySdkMemberType) CreateVariantPropertiesStruct() android.SdkMemberProperties {
2194 return &sdkLibrarySdkMemberProperties{}
2195}
2196
2197type sdkLibrarySdkMemberProperties struct {
2198 android.SdkMemberPropertiesBase
2199
2200 // Scope to per scope properties.
2201 Scopes map[*apiScope]scopeProperties
2202
2203 // Additional libraries that the exported stubs libraries depend upon.
2204 Libs []string
Paul Duffinf488ef22020-04-09 00:10:17 +01002205
2206 // The Java stubs source files.
2207 Stub_srcs []string
Paul Duffinf8e08b22020-05-13 16:54:55 +01002208
2209 // The naming scheme.
2210 Naming_scheme *string
Paul Duffina84756c2020-05-26 20:57:10 +01002211
2212 // True if the java_sdk_library_import is for a shared library, false
2213 // otherwise.
2214 Shared_library *bool
Paul Duffin61871622020-02-10 13:37:10 +00002215}
2216
2217type scopeProperties struct {
Paul Duffin75dcc802020-04-09 01:08:11 +01002218 Jars android.Paths
2219 StubsSrcJar android.Path
2220 CurrentApiFile android.Path
2221 RemovedApiFile android.Path
2222 SdkVersion string
Paul Duffin61871622020-02-10 13:37:10 +00002223}
2224
2225func (s *sdkLibrarySdkMemberProperties) PopulateFromVariant(ctx android.SdkMemberContext, variant android.Module) {
2226 sdk := variant.(*SdkLibrary)
2227
2228 s.Scopes = make(map[*apiScope]scopeProperties)
2229 for _, apiScope := range allApiScopes {
Paul Duffin5ae30792020-05-20 11:52:25 +01002230 paths := sdk.findScopePaths(apiScope)
2231 if paths == nil {
2232 continue
2233 }
2234
Paul Duffin61871622020-02-10 13:37:10 +00002235 jars := paths.stubsImplPath
2236 if len(jars) > 0 {
2237 properties := scopeProperties{}
2238 properties.Jars = jars
Paul Duffin153501f2020-05-12 15:52:55 +01002239 properties.SdkVersion = sdk.sdkVersionForStubsLibrary(ctx.SdkModuleContext(), apiScope)
Paul Duffin533f9c72020-05-20 16:18:00 +01002240 properties.StubsSrcJar = paths.stubsSrcJar.Path()
Paul Duffin86672f62020-06-19 18:39:55 +01002241 if paths.currentApiFilePath.Valid() {
2242 properties.CurrentApiFile = paths.currentApiFilePath.Path()
2243 }
2244 if paths.removedApiFilePath.Valid() {
2245 properties.RemovedApiFile = paths.removedApiFilePath.Path()
2246 }
Paul Duffin61871622020-02-10 13:37:10 +00002247 s.Scopes[apiScope] = properties
2248 }
2249 }
2250
2251 s.Libs = sdk.properties.Libs
Paul Duffind11e78e2020-05-15 20:37:11 +01002252 s.Naming_scheme = sdk.commonSdkLibraryProperties.Naming_scheme
Paul Duffina84756c2020-05-26 20:57:10 +01002253 s.Shared_library = proptools.BoolPtr(sdk.sharedLibrary())
Paul Duffin61871622020-02-10 13:37:10 +00002254}
2255
2256func (s *sdkLibrarySdkMemberProperties) AddToPropertySet(ctx android.SdkMemberContext, propertySet android.BpPropertySet) {
Paul Duffinf8e08b22020-05-13 16:54:55 +01002257 if s.Naming_scheme != nil {
2258 propertySet.AddProperty("naming_scheme", proptools.String(s.Naming_scheme))
2259 }
Paul Duffina84756c2020-05-26 20:57:10 +01002260 if s.Shared_library != nil {
2261 propertySet.AddProperty("shared_library", *s.Shared_library)
2262 }
Paul Duffinf8e08b22020-05-13 16:54:55 +01002263
Paul Duffin61871622020-02-10 13:37:10 +00002264 for _, apiScope := range allApiScopes {
2265 if properties, ok := s.Scopes[apiScope]; ok {
Paul Duffin0f270632020-05-13 19:19:49 +01002266 scopeSet := propertySet.AddPropertySet(apiScope.propertyName)
Paul Duffin61871622020-02-10 13:37:10 +00002267
Paul Duffinf488ef22020-04-09 00:10:17 +01002268 scopeDir := filepath.Join("sdk_library", s.OsPrefix(), apiScope.name)
2269
Paul Duffin61871622020-02-10 13:37:10 +00002270 var jars []string
2271 for _, p := range properties.Jars {
Paul Duffinf488ef22020-04-09 00:10:17 +01002272 dest := filepath.Join(scopeDir, ctx.Name()+"-stubs.jar")
Paul Duffin61871622020-02-10 13:37:10 +00002273 ctx.SnapshotBuilder().CopyToSnapshot(p, dest)
2274 jars = append(jars, dest)
2275 }
2276 scopeSet.AddProperty("jars", jars)
2277
Paul Duffinf488ef22020-04-09 00:10:17 +01002278 // Merge the stubs source jar into the snapshot zip so that when it is unpacked
2279 // the source files are also unpacked.
2280 snapshotRelativeDir := filepath.Join(scopeDir, ctx.Name()+"_stub_sources")
2281 ctx.SnapshotBuilder().UnzipToSnapshot(properties.StubsSrcJar, snapshotRelativeDir)
2282 scopeSet.AddProperty("stub_srcs", []string{snapshotRelativeDir})
2283
Paul Duffin75dcc802020-04-09 01:08:11 +01002284 if properties.CurrentApiFile != nil {
2285 currentApiSnapshotPath := filepath.Join(scopeDir, ctx.Name()+".txt")
2286 ctx.SnapshotBuilder().CopyToSnapshot(properties.CurrentApiFile, currentApiSnapshotPath)
2287 scopeSet.AddProperty("current_api", currentApiSnapshotPath)
2288 }
2289
2290 if properties.RemovedApiFile != nil {
2291 removedApiSnapshotPath := filepath.Join(scopeDir, ctx.Name()+"-removed.txt")
Paul Duffinb1787352020-06-02 13:00:02 +01002292 ctx.SnapshotBuilder().CopyToSnapshot(properties.RemovedApiFile, removedApiSnapshotPath)
Paul Duffin75dcc802020-04-09 01:08:11 +01002293 scopeSet.AddProperty("removed_api", removedApiSnapshotPath)
2294 }
2295
Paul Duffin61871622020-02-10 13:37:10 +00002296 if properties.SdkVersion != "" {
2297 scopeSet.AddProperty("sdk_version", properties.SdkVersion)
2298 }
2299 }
2300 }
2301
2302 if len(s.Libs) > 0 {
2303 propertySet.AddPropertyWithTag("libs", s.Libs, ctx.SnapshotBuilder().SdkMemberReferencePropertyTag(false))
2304 }
2305}