blob: a5aa328d06f75fa1fb2ce3a7ac30b11ef784c4f3 [file] [log] [blame]
Jiyong Parkc678ad32018-04-10 13:07:10 +09001// Copyright 2018 Google Inc. All rights reserved.
2//
3// Licensed under the Apache License, Version 2.0 (the "License");
4// you may not use this file except in compliance with the License.
5// You may obtain a copy of the License at
6//
7// http://www.apache.org/licenses/LICENSE-2.0
8//
9// Unless required by applicable law or agreed to in writing, software
10// distributed under the License is distributed on an "AS IS" BASIS,
11// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12// See the License for the specific language governing permissions and
13// limitations under the License.
14
15package java
16
17import (
Jiyong Parkc678ad32018-04-10 13:07:10 +090018 "fmt"
19 "path"
Sundong Ahn054b19a2018-10-19 13:46:09 +090020 "path/filepath"
Paul Duffin46a26a82020-04-07 19:27:04 +010021 "reflect"
Paul Duffin46dc45a2020-05-14 15:39:10 +010022 "regexp"
Jiyong Park82484c02018-04-23 21:41:26 +090023 "sort"
Jiyong Parkc678ad32018-04-10 13:07:10 +090024 "strings"
Jiyong Park82484c02018-04-23 21:41:26 +090025 "sync"
Jiyong Parkc678ad32018-04-10 13:07:10 +090026
Paul Duffind1b3a922020-01-22 11:57:20 +000027 "github.com/google/blueprint"
Jiyong Parkc678ad32018-04-10 13:07:10 +090028 "github.com/google/blueprint/proptools"
Paul Duffin46a26a82020-04-07 19:27:04 +010029
30 "android/soong/android"
Jiyong Parkc678ad32018-04-10 13:07:10 +090031)
32
Jooyung Han58f26ab2019-12-18 15:34:32 +090033const (
Paul Duffindd9d0742020-05-08 15:52:37 +010034 sdkXmlFileSuffix = ".xml"
35 permissionsTemplate = `<?xml version=\"1.0\" encoding=\"utf-8\"?>\n` +
Jooyung Han624058e2019-12-24 18:38:06 +090036 `<!-- Copyright (C) 2018 The Android Open Source Project\n` +
37 `\n` +
Jiyong Parke3833882020-02-17 17:28:10 +090038 ` Licensed under the Apache License, Version 2.0 (the \"License\");\n` +
Jooyung Han624058e2019-12-24 18:38:06 +090039 ` you may not use this file except in compliance with the License.\n` +
40 ` You may obtain a copy of the License at\n` +
41 `\n` +
42 ` http://www.apache.org/licenses/LICENSE-2.0\n` +
43 `\n` +
44 ` Unless required by applicable law or agreed to in writing, software\n` +
Jiyong Parke3833882020-02-17 17:28:10 +090045 ` distributed under the License is distributed on an \"AS IS\" BASIS,\n` +
Jooyung Han624058e2019-12-24 18:38:06 +090046 ` WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\n` +
47 ` See the License for the specific language governing permissions and\n` +
48 ` limitations under the License.\n` +
49 `-->\n` +
50 `<permissions>\n` +
Jiyong Parke3833882020-02-17 17:28:10 +090051 ` <library name=\"%s\" file=\"%s\"/>\n` +
Jooyung Han624058e2019-12-24 18:38:06 +090052 `</permissions>\n`
Jiyong Parkc678ad32018-04-10 13:07:10 +090053)
54
Paul Duffind1b3a922020-01-22 11:57:20 +000055// A tag to associated a dependency with a specific api scope.
56type scopeDependencyTag struct {
57 blueprint.BaseDependencyTag
58 name string
59 apiScope *apiScope
Paul Duffinc8782502020-04-29 20:45:27 +010060
61 // Function for extracting appropriate path information from the dependency.
62 depInfoExtractor func(paths *scopePaths, dep android.Module) error
63}
64
65// Extract tag specific information from the dependency.
66func (tag scopeDependencyTag) extractDepInfo(ctx android.ModuleContext, dep android.Module, paths *scopePaths) {
67 err := tag.depInfoExtractor(paths, dep)
68 if err != nil {
69 ctx.ModuleErrorf("has an invalid {scopeDependencyTag: %s} dependency on module %s: %s", tag.name, ctx.OtherModuleName(dep), err.Error())
70 }
Paul Duffind1b3a922020-01-22 11:57:20 +000071}
72
Paul Duffin80342d72020-06-26 22:08:43 +010073var _ android.ReplaceSourceWithPrebuilt = (*scopeDependencyTag)(nil)
74
75func (tag scopeDependencyTag) ReplaceSourceWithPrebuilt() bool {
76 return false
77}
78
Paul Duffind1b3a922020-01-22 11:57:20 +000079// Provides information about an api scope, e.g. public, system, test.
80type apiScope struct {
81 // The name of the api scope, e.g. public, system, test
82 name string
83
Paul Duffin97b53b82020-05-05 14:40:52 +010084 // The api scope that this scope extends.
85 extends *apiScope
86
Paul Duffin3375e352020-04-28 10:44:03 +010087 // The legacy enabled status for a specific scope can be dependent on other
88 // properties that have been specified on the library so it is provided by
89 // a function that can determine the status by examining those properties.
90 legacyEnabledStatus func(module *SdkLibrary) bool
91
92 // The default enabled status for non-legacy behavior, which is triggered by
93 // explicitly enabling at least one api scope.
94 defaultEnabledStatus bool
95
96 // Gets a pointer to the scope specific properties.
97 scopeSpecificProperties func(module *SdkLibrary) *ApiScopeProperties
98
Paul Duffin46a26a82020-04-07 19:27:04 +010099 // The name of the field in the dynamically created structure.
100 fieldName string
101
Paul Duffin6b836ba2020-05-13 19:19:49 +0100102 // The name of the property in the java_sdk_library_import
103 propertyName string
104
Paul Duffind1b3a922020-01-22 11:57:20 +0000105 // The tag to use to depend on the stubs library module.
106 stubsTag scopeDependencyTag
107
Paul Duffin0ff08bd2020-04-29 13:30:54 +0100108 // The tag to use to depend on the stubs source module (if separate from the API module).
109 stubsSourceTag scopeDependencyTag
110
111 // The tag to use to depend on the API file generating module (if separate from the stubs source module).
112 apiFileTag scopeDependencyTag
113
Paul Duffinc8782502020-04-29 20:45:27 +0100114 // The tag to use to depend on the stubs source and API module.
115 stubsSourceAndApiTag scopeDependencyTag
Paul Duffind1b3a922020-01-22 11:57:20 +0000116
117 // The scope specific prefix to add to the api file base of "current.txt" or "removed.txt".
118 apiFilePrefix string
119
120 // The scope specific prefix to add to the sdk library module name to construct a scope specific
121 // module name.
122 moduleSuffix string
123
Paul Duffind1b3a922020-01-22 11:57:20 +0000124 // SDK version that the stubs library is built against. Note that this is always
125 // *current. Older stubs library built with a numbered SDK version is created from
126 // the prebuilt jar.
127 sdkVersion string
Paul Duffin1fb487d2020-04-07 18:50:10 +0100128
129 // Extra arguments to pass to droidstubs for this scope.
130 droidstubsArgs []string
Anton Hansson6478ac12020-05-02 11:19:36 +0100131
Paul Duffin0ff08bd2020-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 Hansson6478ac12020-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 Duffinc8782502020-04-29 20:45:27 +0100154 name := scope.name
Paul Duffin46dc45a2020-05-14 15:39:10 +0100155 scopeByName[name] = scope
156 allScopeNames = append(allScopeNames, name)
Paul Duffin6b836ba2020-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 Duffinc8782502020-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 Duffin0ff08bd2020-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 Duffinc8782502020-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 Duffin0ff08bd2020-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 Duffinc3091c82020-05-08 14:16:20 +0100202func (scope *apiScope) stubsLibraryModuleName(baseName string) string {
Paul Duffindd9d0742020-05-08 15:52:37 +0100203 return baseName + ".stubs" + scope.moduleSuffix
Paul Duffind1b3a922020-01-22 11:57:20 +0000204}
205
Paul Duffinc8782502020-04-29 20:45:27 +0100206func (scope *apiScope) stubsSourceModuleName(baseName string) string {
Paul Duffindd9d0742020-05-08 15:52:37 +0100207 return baseName + ".stubs.source" + scope.moduleSuffix
Paul Duffind1b3a922020-01-22 11:57:20 +0000208}
209
Paul Duffin0ff08bd2020-04-29 13:30:54 +0100210func (scope *apiScope) apiModuleName(baseName string) string {
Paul Duffindd9d0742020-05-08 15:52:37 +0100211 return baseName + ".api" + scope.moduleSuffix
Paul Duffin0ff08bd2020-04-29 13:30:54 +0100212}
213
Paul Duffin3375e352020-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 Duffin46dc45a2020-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 Duffin3375e352020-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 Duffin3375e352020-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 Hansson6affb1f2020-04-28 16:47:41 +0100252 apiFilePrefix: "system-",
Paul Duffindd9d0742020-05-08 15:52:37 +0100253 moduleSuffix: ".system",
Anton Hansson6affb1f2020-04-28 16:47:41 +0100254 sdkVersion: "system_current",
Paul Duffin0d543642020-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 Duffin3375e352020-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 Hansson6affb1f2020-04-28 16:47:41 +0100264 apiFilePrefix: "test-",
Paul Duffindd9d0742020-05-08 15:52:37 +0100265 moduleSuffix: ".test",
Anton Hansson6affb1f2020-04-28 16:47:41 +0100266 sdkVersion: "test_current",
267 droidstubsArgs: []string{"-showAnnotation android.annotation.TestApi"},
Anton Hansson6478ac12020-05-02 11:19:36 +0100268 unstable: true,
Paul Duffind1b3a922020-01-22 11:57:20 +0000269 })
Paul Duffin8f265b92020-04-28 14:13:56 +0100270 apiScopeModuleLib = initApiScope(&apiScope{
Paul Duffin6b836ba2020-05-13 19:19:49 +0100271 name: "module-lib",
Paul Duffin8f265b92020-04-28 14:13:56 +0100272 extends: apiScopeSystem,
Paul Duffin0c5bae52020-06-02 13:00:08 +0100273 // The module-lib scope is disabled by default in legacy mode.
Paul Duffin8f265b92020-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 Duffin0c5bae52020-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 Duffin8f265b92020-04-28 14:13:56 +0100315 apiScopeModuleLib,
Paul Duffin0c5bae52020-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 Duffindd46f712020-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 Duffin3375e352020-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 Duffin87a05a32020-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 Duffin3375e352020-04-28 10:44:03 +0100377}
378
Jiyong Parkc678ad32018-04-10 13:07:10 +0900379type sdkLibraryProperties struct {
Paul Duffin5df79302020-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 Duffin4911a892020-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 Duffindfa131e2020-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
Sundong Ahn054b19a2018-10-19 13:46:09 +0900423 // a list of top-level directories containing files to merge qualifier annotations
424 // (i.e. those intended to be included in the stubs written) from.
425 Merge_annotations_dirs []string
426
427 // a list of top-level directories containing Java stub files to merge show/hide annotations from.
428 Merge_inclusion_annotations_dirs []string
429
430 // If set to true, the path of dist files is apistubs/core. Defaults to false.
431 Core_lib *bool
432
Sundong Ahn80a87b32019-05-13 15:02:50 +0900433 // don't create dist rules.
434 No_dist *bool `blueprint:"mutated"`
435
Paul Duffin3375e352020-04-28 10:44:03 +0100436 // indicates whether system and test apis should be generated.
437 Generate_system_and_test_apis bool `blueprint:"mutated"`
438
439 // The properties specific to the public api scope
440 //
441 // Unless explicitly specified by using public.enabled the public api scope is
442 // enabled by default in both legacy and non-legacy mode.
443 Public ApiScopeProperties
444
445 // The properties specific to the system api scope
446 //
447 // In legacy mode the system api scope is enabled by default when sdk_version
448 // is set to something other than "none".
449 //
450 // In non-legacy mode the system api scope is disabled by default.
451 System ApiScopeProperties
452
453 // The properties specific to the test api scope
454 //
455 // In legacy mode the test api scope is enabled by default when sdk_version
456 // is set to something other than "none".
457 //
458 // In non-legacy mode the test api scope is disabled by default.
459 Test ApiScopeProperties
Paul Duffin37e0b772019-12-30 17:20:10 +0000460
Paul Duffin0c5bae52020-06-02 13:00:08 +0100461 // The properties specific to the module-lib api scope
Paul Duffin8f265b92020-04-28 14:13:56 +0100462 //
Paul Duffin0c5bae52020-06-02 13:00:08 +0100463 // Unless explicitly specified by using test.enabled the module-lib api scope is
Paul Duffin8f265b92020-04-28 14:13:56 +0100464 // disabled by default.
465 Module_lib ApiScopeProperties
466
Paul Duffin0c5bae52020-06-02 13:00:08 +0100467 // The properties specific to the system-server api scope
468 //
469 // Unless explicitly specified by using test.enabled the module-lib api scope is
470 // disabled by default.
471 System_server ApiScopeProperties
472
Jiyong Park932cdfe2020-05-28 00:19:53 +0900473 // Determines if the stubs are preferred over the implementation library
474 // for linking, even when the client doesn't specify sdk_version. When this
475 // is set to true, such clients are provided with the widest API surface that
476 // this lib provides. Note however that this option doesn't affect the clients
477 // that are in the same APEX as this library. In that case, the clients are
478 // always linked with the implementation library. Default is false.
479 Default_to_stubs *bool
480
Paul Duffin160fe412020-05-10 19:32:20 +0100481 // Properties related to api linting.
482 Api_lint struct {
483 // Enable api linting.
484 Enabled *bool
485 }
486
Jiyong Parkc678ad32018-04-10 13:07:10 +0900487 // TODO: determines whether to create HTML doc or not
488 //Html_doc *bool
489}
490
Paul Duffin0f8faff2020-05-20 16:18:00 +0100491// Paths to outputs from java_sdk_library and java_sdk_library_import.
492//
493// Fields that are android.Paths are always set (during GenerateAndroidBuildActions).
494// OptionalPaths are always set by java_sdk_library but may not be set by
495// java_sdk_library_import as not all instances provide that information.
Paul Duffind1b3a922020-01-22 11:57:20 +0000496type scopePaths struct {
Paul Duffin0f8faff2020-05-20 16:18:00 +0100497 // The path (represented as Paths for convenience when returning) to the stubs header jar.
498 //
499 // That is the jar that is created by turbine.
500 stubsHeaderPath android.Paths
501
502 // The path (represented as Paths for convenience when returning) to the stubs implementation jar.
503 //
504 // This is not the implementation jar, it still only contains stubs.
505 stubsImplPath android.Paths
506
507 // The API specification file, e.g. system_current.txt.
508 currentApiFilePath android.OptionalPath
509
510 // The specification of API elements removed since the last release.
511 removedApiFilePath android.OptionalPath
512
513 // The stubs source jar.
514 stubsSrcJar android.OptionalPath
Paul Duffind1b3a922020-01-22 11:57:20 +0000515}
516
Paul Duffinc8782502020-04-29 20:45:27 +0100517func (paths *scopePaths) extractStubsLibraryInfoFromDependency(dep android.Module) error {
518 if lib, ok := dep.(Dependency); ok {
519 paths.stubsHeaderPath = lib.HeaderJars()
520 paths.stubsImplPath = lib.ImplementationJars()
521 return nil
522 } else {
523 return fmt.Errorf("expected module that implements Dependency, e.g. java_library")
524 }
525}
526
Paul Duffin0ff08bd2020-04-29 13:30:54 +0100527func (paths *scopePaths) treatDepAsApiStubsProvider(dep android.Module, action func(provider ApiStubsProvider)) error {
528 if apiStubsProvider, ok := dep.(ApiStubsProvider); ok {
529 action(apiStubsProvider)
Paul Duffinc8782502020-04-29 20:45:27 +0100530 return nil
531 } else {
532 return fmt.Errorf("expected module that implements ApiStubsProvider, e.g. droidstubs")
533 }
534}
535
Paul Duffin0f8faff2020-05-20 16:18:00 +0100536func (paths *scopePaths) treatDepAsApiStubsSrcProvider(dep android.Module, action func(provider ApiStubsSrcProvider)) error {
537 if apiStubsProvider, ok := dep.(ApiStubsSrcProvider); ok {
538 action(apiStubsProvider)
539 return nil
540 } else {
541 return fmt.Errorf("expected module that implements ApiStubsSrcProvider, e.g. droidstubs")
542 }
543}
544
Paul Duffin0ff08bd2020-04-29 13:30:54 +0100545func (paths *scopePaths) extractApiInfoFromApiStubsProvider(provider ApiStubsProvider) {
Paul Duffin0f8faff2020-05-20 16:18:00 +0100546 paths.currentApiFilePath = android.OptionalPathForPath(provider.ApiFilePath())
547 paths.removedApiFilePath = android.OptionalPathForPath(provider.RemovedApiFilePath())
Paul Duffin0ff08bd2020-04-29 13:30:54 +0100548}
549
550func (paths *scopePaths) extractApiInfoFromDep(dep android.Module) error {
551 return paths.treatDepAsApiStubsProvider(dep, func(provider ApiStubsProvider) {
552 paths.extractApiInfoFromApiStubsProvider(provider)
553 })
554}
555
Paul Duffin0f8faff2020-05-20 16:18:00 +0100556func (paths *scopePaths) extractStubsSourceInfoFromApiStubsProviders(provider ApiStubsSrcProvider) {
557 paths.stubsSrcJar = android.OptionalPathForPath(provider.StubsSrcJar())
Paul Duffin0ff08bd2020-04-29 13:30:54 +0100558}
559
560func (paths *scopePaths) extractStubsSourceInfoFromDep(dep android.Module) error {
Paul Duffin0f8faff2020-05-20 16:18:00 +0100561 return paths.treatDepAsApiStubsSrcProvider(dep, func(provider ApiStubsSrcProvider) {
Paul Duffin0ff08bd2020-04-29 13:30:54 +0100562 paths.extractStubsSourceInfoFromApiStubsProviders(provider)
563 })
564}
565
566func (paths *scopePaths) extractStubsSourceAndApiInfoFromApiStubsProvider(dep android.Module) error {
567 return paths.treatDepAsApiStubsProvider(dep, func(provider ApiStubsProvider) {
568 paths.extractApiInfoFromApiStubsProvider(provider)
569 paths.extractStubsSourceInfoFromApiStubsProviders(provider)
570 })
571}
572
573type commonToSdkLibraryAndImportProperties struct {
Paul Duffin1b1e8062020-05-08 13:44:43 +0100574 // The naming scheme to use for the components that this module creates.
575 //
Paul Duffin6c9c5fc2020-05-08 15:36:30 +0100576 // If not specified then it defaults to "default". The other allowable value is
577 // "framework-modules" which matches the scheme currently used by framework modules
578 // for the equivalent components represented as separate Soong modules.
Paul Duffin1b1e8062020-05-08 13:44:43 +0100579 //
580 // This is a temporary mechanism to simplify conversion from separate modules for each
581 // component that follow a different naming pattern to the default one.
582 //
583 // TODO(b/155480189) - Remove once naming inconsistencies have been resolved.
Paul Duffin0ff08bd2020-04-29 13:30:54 +0100584 Naming_scheme *string
Paul Duffindfa131e2020-05-15 20:37:11 +0100585
586 // Specifies whether this module can be used as an Android shared library; defaults
587 // to true.
588 //
589 // An Android shared library is one that can be referenced in a <uses-library> element
590 // in an AndroidManifest.xml.
591 Shared_library *bool
Paul Duffin0ff08bd2020-04-29 13:30:54 +0100592}
593
Paul Duffin56d44902020-01-31 13:36:25 +0000594// Common code between sdk library and sdk library import
595type commonToSdkLibraryAndImport struct {
Paul Duffinc3091c82020-05-08 14:16:20 +0100596 moduleBase *android.ModuleBase
597
Paul Duffin56d44902020-01-31 13:36:25 +0000598 scopePaths map[*apiScope]*scopePaths
Paul Duffin1b1e8062020-05-08 13:44:43 +0100599
600 namingScheme sdkLibraryComponentNamingScheme
601
Paul Duffindfa131e2020-05-15 20:37:11 +0100602 commonSdkLibraryProperties commonToSdkLibraryAndImportProperties
Paul Duffin859fe962020-05-15 10:20:31 +0100603
604 // Functionality related to this being used as a component of a java_sdk_library.
605 EmbeddableSdkLibraryComponent
Paul Duffin56d44902020-01-31 13:36:25 +0000606}
607
Paul Duffinc3091c82020-05-08 14:16:20 +0100608func (c *commonToSdkLibraryAndImport) initCommon(moduleBase *android.ModuleBase) {
609 c.moduleBase = moduleBase
Paul Duffin1b1e8062020-05-08 13:44:43 +0100610
Paul Duffindfa131e2020-05-15 20:37:11 +0100611 moduleBase.AddProperties(&c.commonSdkLibraryProperties)
Paul Duffin859fe962020-05-15 10:20:31 +0100612
613 // Initialize this as an sdk library component.
614 c.initSdkLibraryComponent(moduleBase)
Paul Duffin1b1e8062020-05-08 13:44:43 +0100615}
616
617func (c *commonToSdkLibraryAndImport) initCommonAfterDefaultsApplied(ctx android.DefaultableHookContext) bool {
Paul Duffindfa131e2020-05-15 20:37:11 +0100618 schemeProperty := proptools.StringDefault(c.commonSdkLibraryProperties.Naming_scheme, "default")
Paul Duffin1b1e8062020-05-08 13:44:43 +0100619 switch schemeProperty {
620 case "default":
621 c.namingScheme = &defaultNamingScheme{}
Paul Duffin6c9c5fc2020-05-08 15:36:30 +0100622 case "framework-modules":
623 c.namingScheme = &frameworkModulesNamingScheme{}
Paul Duffin1b1e8062020-05-08 13:44:43 +0100624 default:
625 ctx.PropertyErrorf("naming_scheme", "expected 'default' but was %q", schemeProperty)
626 return false
627 }
628
Paul Duffindfa131e2020-05-15 20:37:11 +0100629 // Only track this sdk library if this can be used as a shared library.
630 if c.sharedLibrary() {
631 // Use the name specified in the module definition as the owner.
632 c.sdkLibraryComponentProperties.SdkLibraryToImplicitlyTrack = proptools.StringPtr(c.moduleBase.BaseModuleName())
633 }
Paul Duffin859fe962020-05-15 10:20:31 +0100634
Paul Duffin1b1e8062020-05-08 13:44:43 +0100635 return true
Paul Duffinc3091c82020-05-08 14:16:20 +0100636}
637
Paul Duffineedc5d52020-06-12 17:46:39 +0100638// Module name of the runtime implementation library
639func (c *commonToSdkLibraryAndImport) implLibraryModuleName() string {
640 return c.moduleBase.BaseModuleName() + ".impl"
641}
642
643// Module name of the XML file for the lib
644func (c *commonToSdkLibraryAndImport) xmlPermissionsModuleName() string {
645 return c.moduleBase.BaseModuleName() + sdkXmlFileSuffix
646}
647
Paul Duffinc3091c82020-05-08 14:16:20 +0100648// Name of the java_library module that compiles the stubs source.
649func (c *commonToSdkLibraryAndImport) stubsLibraryModuleName(apiScope *apiScope) string {
Paul Duffin1b1e8062020-05-08 13:44:43 +0100650 return c.namingScheme.stubsLibraryModuleName(apiScope, c.moduleBase.BaseModuleName())
Paul Duffinc3091c82020-05-08 14:16:20 +0100651}
652
653// Name of the droidstubs module that generates the stubs source and may also
654// generate/check the API.
655func (c *commonToSdkLibraryAndImport) stubsSourceModuleName(apiScope *apiScope) string {
Paul Duffin1b1e8062020-05-08 13:44:43 +0100656 return c.namingScheme.stubsSourceModuleName(apiScope, c.moduleBase.BaseModuleName())
Paul Duffinc3091c82020-05-08 14:16:20 +0100657}
658
659// Name of the droidstubs module that generates/checks the API. Only used if it
660// requires different arts to the stubs source generating module.
661func (c *commonToSdkLibraryAndImport) apiModuleName(apiScope *apiScope) string {
Paul Duffin1b1e8062020-05-08 13:44:43 +0100662 return c.namingScheme.apiModuleName(apiScope, c.moduleBase.BaseModuleName())
Paul Duffinc3091c82020-05-08 14:16:20 +0100663}
664
Paul Duffin46dc45a2020-05-14 15:39:10 +0100665// The component names for different outputs of the java_sdk_library.
666//
667// They are similar to the names used for the child modules it creates
668const (
669 stubsSourceComponentName = "stubs.source"
670
671 apiTxtComponentName = "api.txt"
672
673 removedApiTxtComponentName = "removed-api.txt"
674)
675
676// A regular expression to match tags that reference a specific stubs component.
677//
678// It will only match if given a valid scope and a valid component. It is verfy strict
679// to ensure it does not accidentally match a similar looking tag that should be processed
680// by the embedded Library.
681var tagSplitter = func() *regexp.Regexp {
682 // Given a list of literal string items returns a regular expression that will
683 // match any one of the items.
684 choice := func(items ...string) string {
685 return `\Q` + strings.Join(items, `\E|\Q`) + `\E`
686 }
687
688 // Regular expression to match one of the scopes.
689 scopesRegexp := choice(allScopeNames...)
690
691 // Regular expression to match one of the components.
692 componentsRegexp := choice(stubsSourceComponentName, apiTxtComponentName, removedApiTxtComponentName)
693
694 // Regular expression to match any combination of one scope and one component.
695 return regexp.MustCompile(fmt.Sprintf(`^\.(%s)\.(%s)$`, scopesRegexp, componentsRegexp))
696}()
697
698// For OutputFileProducer interface
699//
700// .<scope>.stubs.source
701// .<scope>.api.txt
702// .<scope>.removed-api.txt
703func (c *commonToSdkLibraryAndImport) commonOutputFiles(tag string) (android.Paths, error) {
704 if groups := tagSplitter.FindStringSubmatch(tag); groups != nil {
705 scopeName := groups[1]
706 component := groups[2]
707
708 if scope, ok := scopeByName[scopeName]; ok {
709 paths := c.findScopePaths(scope)
710 if paths == nil {
711 return nil, fmt.Errorf("%q does not provide api scope %s", c.moduleBase.BaseModuleName(), scopeName)
712 }
713
714 switch component {
715 case stubsSourceComponentName:
716 if paths.stubsSrcJar.Valid() {
717 return android.Paths{paths.stubsSrcJar.Path()}, nil
718 }
719
720 case apiTxtComponentName:
721 if paths.currentApiFilePath.Valid() {
722 return android.Paths{paths.currentApiFilePath.Path()}, nil
723 }
724
725 case removedApiTxtComponentName:
726 if paths.removedApiFilePath.Valid() {
727 return android.Paths{paths.removedApiFilePath.Path()}, nil
728 }
729 }
730
731 return nil, fmt.Errorf("%s not available for api scope %s", component, scopeName)
732 } else {
733 return nil, fmt.Errorf("unknown scope %s in %s", scope, tag)
734 }
735
736 } else {
737 return nil, nil
738 }
739}
740
Paul Duffin803a9562020-05-20 11:52:25 +0100741func (c *commonToSdkLibraryAndImport) getScopePathsCreateIfNeeded(scope *apiScope) *scopePaths {
Paul Duffin56d44902020-01-31 13:36:25 +0000742 if c.scopePaths == nil {
743 c.scopePaths = make(map[*apiScope]*scopePaths)
744 }
745 paths := c.scopePaths[scope]
746 if paths == nil {
747 paths = &scopePaths{}
748 c.scopePaths[scope] = paths
749 }
750
751 return paths
752}
753
Paul Duffin803a9562020-05-20 11:52:25 +0100754func (c *commonToSdkLibraryAndImport) findScopePaths(scope *apiScope) *scopePaths {
755 if c.scopePaths == nil {
756 return nil
757 }
758
759 return c.scopePaths[scope]
760}
761
762// If this does not support the requested api scope then find the closest available
763// scope it does support. Returns nil if no such scope is available.
764func (c *commonToSdkLibraryAndImport) findClosestScopePath(scope *apiScope) *scopePaths {
765 for s := scope; s != nil; s = s.extends {
766 if paths := c.findScopePaths(s); paths != nil {
767 return paths
768 }
769 }
770
771 // This should never happen outside tests as public should be the base scope for every
772 // scope and is enabled by default.
773 return nil
774}
775
Paul Duffin23970f42020-05-20 14:20:02 +0100776func (c *commonToSdkLibraryAndImport) selectHeaderJarsForSdkVersion(ctx android.BaseModuleContext, sdkVersion sdkSpec) android.Paths {
Paul Duffinb05d4292020-05-20 12:19:10 +0100777
778 // If a specific numeric version has been requested then use prebuilt versions of the sdk.
779 if sdkVersion.version.isNumbered() {
780 return PrebuiltJars(ctx, c.moduleBase.BaseModuleName(), sdkVersion)
781 }
782
783 var apiScope *apiScope
784 switch sdkVersion.kind {
785 case sdkSystem:
786 apiScope = apiScopeSystem
Paul Duffin803a9562020-05-20 11:52:25 +0100787 case sdkModule:
788 apiScope = apiScopeModuleLib
Paul Duffinb05d4292020-05-20 12:19:10 +0100789 case sdkTest:
790 apiScope = apiScopeTest
Paul Duffin0c5bae52020-06-02 13:00:08 +0100791 case sdkSystemServer:
792 apiScope = apiScopeSystemServer
Paul Duffinb05d4292020-05-20 12:19:10 +0100793 default:
794 apiScope = apiScopePublic
795 }
796
Paul Duffin803a9562020-05-20 11:52:25 +0100797 paths := c.findClosestScopePath(apiScope)
798 if paths == nil {
799 var scopes []string
800 for _, s := range allApiScopes {
801 if c.findScopePaths(s) != nil {
802 scopes = append(scopes, s.name)
803 }
804 }
805 ctx.ModuleErrorf("requires api scope %s from %s but it only has %q available", apiScope.name, c.moduleBase.BaseModuleName(), scopes)
806 return nil
807 }
808
Paul Duffin23970f42020-05-20 14:20:02 +0100809 return paths.stubsHeaderPath
Paul Duffinb05d4292020-05-20 12:19:10 +0100810}
811
Paul Duffin859fe962020-05-15 10:20:31 +0100812func (c *commonToSdkLibraryAndImport) sdkComponentPropertiesForChildLibrary() interface{} {
813 componentProps := &struct {
814 SdkLibraryToImplicitlyTrack *string
Paul Duffindfa131e2020-05-15 20:37:11 +0100815 }{}
816
817 if c.sharedLibrary() {
Paul Duffin859fe962020-05-15 10:20:31 +0100818 // Mark the stubs library as being components of this java_sdk_library so that
819 // any app that includes code which depends (directly or indirectly) on the stubs
820 // library will have the appropriate <uses-library> invocation inserted into its
821 // manifest if necessary.
Paul Duffindfa131e2020-05-15 20:37:11 +0100822 componentProps.SdkLibraryToImplicitlyTrack = proptools.StringPtr(c.moduleBase.BaseModuleName())
Paul Duffin859fe962020-05-15 10:20:31 +0100823 }
824
825 return componentProps
826}
827
Paul Duffindfa131e2020-05-15 20:37:11 +0100828// Check if this can be used as a shared library.
829func (c *commonToSdkLibraryAndImport) sharedLibrary() bool {
830 return proptools.BoolDefault(c.commonSdkLibraryProperties.Shared_library, true)
831}
832
Paul Duffin859fe962020-05-15 10:20:31 +0100833// Properties related to the use of a module as an component of a java_sdk_library.
834type SdkLibraryComponentProperties struct {
835
836 // The name of the java_sdk_library/_import to add to a <uses-library> entry
837 // in the AndroidManifest.xml of any Android app that includes code that references
838 // this module. If not set then no java_sdk_library/_import is tracked.
839 SdkLibraryToImplicitlyTrack *string `blueprint:"mutated"`
840}
841
842// Structure to be embedded in a module struct that needs to support the
843// SdkLibraryComponentDependency interface.
844type EmbeddableSdkLibraryComponent struct {
845 sdkLibraryComponentProperties SdkLibraryComponentProperties
846}
847
848func (e *EmbeddableSdkLibraryComponent) initSdkLibraryComponent(moduleBase *android.ModuleBase) {
849 moduleBase.AddProperties(&e.sdkLibraryComponentProperties)
850}
851
852// to satisfy SdkLibraryComponentDependency
853func (e *EmbeddableSdkLibraryComponent) OptionalImplicitSdkLibrary() []string {
854 if e.sdkLibraryComponentProperties.SdkLibraryToImplicitlyTrack != nil {
855 return []string{*e.sdkLibraryComponentProperties.SdkLibraryToImplicitlyTrack}
856 }
857 return nil
858}
859
860// Implemented by modules that are (or possibly could be) a component of a java_sdk_library
861// (including the java_sdk_library) itself.
862type SdkLibraryComponentDependency interface {
863 // The optional name of the sdk library that should be implicitly added to the
864 // AndroidManifest of an app that contains code which references the sdk library.
865 //
866 // Returns an array containing 0 or 1 items rather than a *string to make it easier
867 // to append this to the list of exported sdk libraries.
868 OptionalImplicitSdkLibrary() []string
869}
870
871// Make sure that all the module types that are components of java_sdk_library/_import
872// and which can be referenced (directly or indirectly) from an android app implement
873// the SdkLibraryComponentDependency interface.
874var _ SdkLibraryComponentDependency = (*Library)(nil)
875var _ SdkLibraryComponentDependency = (*Import)(nil)
876var _ SdkLibraryComponentDependency = (*SdkLibrary)(nil)
Paul Duffineedc5d52020-06-12 17:46:39 +0100877var _ SdkLibraryComponentDependency = (*SdkLibraryImport)(nil)
Paul Duffin859fe962020-05-15 10:20:31 +0100878
879// Provides access to sdk_version related header and implentation jars.
880type SdkLibraryDependency interface {
881 SdkLibraryComponentDependency
882
883 // Get the header jars appropriate for the supplied sdk_version.
884 //
885 // These are turbine generated jars so they only change if the externals of the
886 // class changes but it does not contain and implementation or JavaDoc.
887 SdkHeaderJars(ctx android.BaseModuleContext, sdkVersion sdkSpec) android.Paths
888
889 // Get the implementation jars appropriate for the supplied sdk version.
890 //
891 // These are either the implementation jar for the whole sdk library or the implementation
892 // jars for the stubs. The latter should only be needed when generating JavaDoc as otherwise
893 // they are identical to the corresponding header jars.
894 SdkImplementationJars(ctx android.BaseModuleContext, sdkVersion sdkSpec) android.Paths
895}
896
Inseob Kimc0907f12019-02-08 21:00:45 +0900897type SdkLibrary struct {
Sundong Ahn054b19a2018-10-19 13:46:09 +0900898 Library
Jiyong Parkc678ad32018-04-10 13:07:10 +0900899
Sundong Ahn054b19a2018-10-19 13:46:09 +0900900 sdkLibraryProperties sdkLibraryProperties
Jiyong Parkc678ad32018-04-10 13:07:10 +0900901
Paul Duffin3375e352020-04-28 10:44:03 +0100902 // Map from api scope to the scope specific property structure.
903 scopeToProperties map[*apiScope]*ApiScopeProperties
904
Paul Duffin56d44902020-01-31 13:36:25 +0000905 commonToSdkLibraryAndImport
Jiyong Parkc678ad32018-04-10 13:07:10 +0900906}
907
Inseob Kimc0907f12019-02-08 21:00:45 +0900908var _ Dependency = (*SdkLibrary)(nil)
909var _ SdkLibraryDependency = (*SdkLibrary)(nil)
Colin Cross897d2ed2019-02-11 14:03:51 -0800910
Paul Duffin3375e352020-04-28 10:44:03 +0100911func (module *SdkLibrary) generateTestAndSystemScopesByDefault() bool {
912 return module.sdkLibraryProperties.Generate_system_and_test_apis
913}
914
915func (module *SdkLibrary) getGeneratedApiScopes(ctx android.EarlyModuleContext) apiScopes {
916 // Check to see if any scopes have been explicitly enabled. If any have then all
917 // must be.
918 anyScopesExplicitlyEnabled := false
919 for _, scope := range allApiScopes {
920 scopeProperties := module.scopeToProperties[scope]
921 if scopeProperties.Enabled != nil {
922 anyScopesExplicitlyEnabled = true
923 break
924 }
Paul Duffind1b3a922020-01-22 11:57:20 +0000925 }
Paul Duffin3375e352020-04-28 10:44:03 +0100926
927 var generatedScopes apiScopes
928 enabledScopes := make(map[*apiScope]struct{})
929 for _, scope := range allApiScopes {
930 scopeProperties := module.scopeToProperties[scope]
931 // If any scopes are explicitly enabled then ignore the legacy enabled status.
932 // This is to ensure that any new usages of this module type do not rely on legacy
933 // behaviour.
934 defaultEnabledStatus := false
935 if anyScopesExplicitlyEnabled {
936 defaultEnabledStatus = scope.defaultEnabledStatus
937 } else {
938 defaultEnabledStatus = scope.legacyEnabledStatus(module)
939 }
940 enabled := proptools.BoolDefault(scopeProperties.Enabled, defaultEnabledStatus)
941 if enabled {
942 enabledScopes[scope] = struct{}{}
943 generatedScopes = append(generatedScopes, scope)
944 }
945 }
946
947 // Now check to make sure that any scope that is extended by an enabled scope is also
948 // enabled.
949 for _, scope := range allApiScopes {
950 if _, ok := enabledScopes[scope]; ok {
951 extends := scope.extends
952 if extends != nil {
953 if _, ok := enabledScopes[extends]; !ok {
954 ctx.ModuleErrorf("enabled api scope %q depends on disabled scope %q", scope, extends)
955 }
956 }
957 }
958 }
959
960 return generatedScopes
Paul Duffind1b3a922020-01-22 11:57:20 +0000961}
962
Paul Duffineedc5d52020-06-12 17:46:39 +0100963type sdkLibraryComponentTag struct {
964 blueprint.BaseDependencyTag
965 name string
966}
967
968// Mark this tag so dependencies that use it are excluded from visibility enforcement.
969func (t sdkLibraryComponentTag) ExcludeFromVisibilityEnforcement() {}
970
971var xmlPermissionsFileTag = sdkLibraryComponentTag{name: "xml-permissions-file"}
Paul Duffine74ac732020-02-06 13:51:46 +0000972
Jiyong Parke3833882020-02-17 17:28:10 +0900973func IsXmlPermissionsFileDepTag(depTag blueprint.DependencyTag) bool {
Paul Duffineedc5d52020-06-12 17:46:39 +0100974 if dt, ok := depTag.(sdkLibraryComponentTag); ok {
Jiyong Parke3833882020-02-17 17:28:10 +0900975 return dt == xmlPermissionsFileTag
976 }
977 return false
978}
979
Paul Duffineedc5d52020-06-12 17:46:39 +0100980var implLibraryTag = sdkLibraryComponentTag{name: "impl-library"}
Paul Duffin5df79302020-05-16 15:52:12 +0100981
Paul Duffin44f1d842020-06-26 20:17:02 +0100982// Add the dependencies on the child modules in the component deps mutator.
983func (module *SdkLibrary) ComponentDepsMutator(ctx android.BottomUpMutatorContext) {
Paul Duffin3375e352020-04-28 10:44:03 +0100984 for _, apiScope := range module.getGeneratedApiScopes(ctx) {
Paul Duffind1b3a922020-01-22 11:57:20 +0000985 // Add dependencies to the stubs library
Paul Duffinc3091c82020-05-08 14:16:20 +0100986 ctx.AddVariationDependencies(nil, apiScope.stubsTag, module.stubsLibraryModuleName(apiScope))
Paul Duffind1b3a922020-01-22 11:57:20 +0000987
Paul Duffin0ff08bd2020-04-29 13:30:54 +0100988 // If the stubs source and API cannot be generated together then add an additional dependency on
989 // the API module.
990 if apiScope.createStubsSourceAndApiTogether {
991 // Add a dependency on the stubs source in order to access both stubs source and api information.
Paul Duffinc3091c82020-05-08 14:16:20 +0100992 ctx.AddVariationDependencies(nil, apiScope.stubsSourceAndApiTag, module.stubsSourceModuleName(apiScope))
Paul Duffin0ff08bd2020-04-29 13:30:54 +0100993 } else {
994 // Add separate dependencies on the creators of the stubs source files and the API.
Paul Duffinc3091c82020-05-08 14:16:20 +0100995 ctx.AddVariationDependencies(nil, apiScope.stubsSourceTag, module.stubsSourceModuleName(apiScope))
996 ctx.AddVariationDependencies(nil, apiScope.apiFileTag, module.apiModuleName(apiScope))
Paul Duffin0ff08bd2020-04-29 13:30:54 +0100997 }
Sundong Ahn054b19a2018-10-19 13:46:09 +0900998 }
999
Paul Duffindfa131e2020-05-15 20:37:11 +01001000 if module.requiresRuntimeImplementationLibrary() {
Paul Duffin5df79302020-05-16 15:52:12 +01001001 // Add dependency to the rule for generating the implementation library.
1002 ctx.AddDependency(module, implLibraryTag, module.implLibraryModuleName())
1003
Paul Duffindfa131e2020-05-15 20:37:11 +01001004 if module.sharedLibrary() {
1005 // Add dependency to the rule for generating the xml permissions file
Paul Duffineedc5d52020-06-12 17:46:39 +01001006 ctx.AddDependency(module, xmlPermissionsFileTag, module.xmlPermissionsModuleName())
Paul Duffindfa131e2020-05-15 20:37:11 +01001007 }
Paul Duffin44f1d842020-06-26 20:17:02 +01001008 }
1009}
Paul Duffine74ac732020-02-06 13:51:46 +00001010
Paul Duffin44f1d842020-06-26 20:17:02 +01001011// Add other dependencies as normal.
1012func (module *SdkLibrary) DepsMutator(ctx android.BottomUpMutatorContext) {
1013 if module.requiresRuntimeImplementationLibrary() {
Paul Duffindfa131e2020-05-15 20:37:11 +01001014 // Only add the deps for the library if it is actually going to be built.
1015 module.Library.deps(ctx)
1016 }
Jiyong Parkc678ad32018-04-10 13:07:10 +09001017}
1018
Paul Duffin46dc45a2020-05-14 15:39:10 +01001019func (module *SdkLibrary) OutputFiles(tag string) (android.Paths, error) {
1020 paths, err := module.commonOutputFiles(tag)
1021 if paths == nil && err == nil {
1022 return module.Library.OutputFiles(tag)
1023 } else {
1024 return paths, err
1025 }
1026}
1027
Inseob Kimc0907f12019-02-08 21:00:45 +09001028func (module *SdkLibrary) GenerateAndroidBuildActions(ctx android.ModuleContext) {
Paul Duffindfa131e2020-05-15 20:37:11 +01001029 // Only build an implementation library if required.
1030 if module.requiresRuntimeImplementationLibrary() {
Paul Duffin43db9be2019-12-30 17:35:49 +00001031 module.Library.GenerateAndroidBuildActions(ctx)
1032 }
Sundong Ahn054b19a2018-10-19 13:46:09 +09001033
Sundong Ahn57368eb2018-07-06 11:20:23 +09001034 // Record the paths to the header jars of the library (stubs and impl).
Paul Duffind1b3a922020-01-22 11:57:20 +00001035 // When this java_sdk_library is depended upon from others via "libs" property,
Jiyong Parkc678ad32018-04-10 13:07:10 +09001036 // the recorded paths will be returned depending on the link type of the caller.
1037 ctx.VisitDirectDeps(func(to android.Module) {
Jiyong Parkc678ad32018-04-10 13:07:10 +09001038 tag := ctx.OtherModuleDependencyTag(to)
1039
Paul Duffinc8782502020-04-29 20:45:27 +01001040 // Extract information from any of the scope specific dependencies.
1041 if scopeTag, ok := tag.(scopeDependencyTag); ok {
1042 apiScope := scopeTag.apiScope
Paul Duffin803a9562020-05-20 11:52:25 +01001043 scopePaths := module.getScopePathsCreateIfNeeded(apiScope)
Paul Duffinc8782502020-04-29 20:45:27 +01001044
1045 // Extract information from the dependency. The exact information extracted
1046 // is determined by the nature of the dependency which is determined by the tag.
1047 scopeTag.extractDepInfo(ctx, to, scopePaths)
Sundong Ahn20e998b2018-07-24 11:19:26 +09001048 }
Jiyong Parkc678ad32018-04-10 13:07:10 +09001049 })
1050}
1051
Jiyong Park0b0e1b92019-12-03 13:24:29 +09001052func (module *SdkLibrary) AndroidMkEntries() []android.AndroidMkEntries {
Paul Duffindfa131e2020-05-15 20:37:11 +01001053 if !module.requiresRuntimeImplementationLibrary() {
Paul Duffin43db9be2019-12-30 17:35:49 +00001054 return nil
1055 }
Jiyong Park0b0e1b92019-12-03 13:24:29 +09001056 entriesList := module.Library.AndroidMkEntries()
Yo Chiang07d75072020-06-05 17:43:19 +08001057 if module.sharedLibrary() {
1058 entries := &entriesList[0]
1059 entries.Required = append(entries.Required, module.xmlPermissionsModuleName())
1060 }
Jiyong Park0b0e1b92019-12-03 13:24:29 +09001061 return entriesList
Jiyong Park82484c02018-04-23 21:41:26 +09001062}
1063
Anton Hansson5fd5d242020-03-27 19:43:19 +00001064// The dist path of the stub artifacts
1065func (module *SdkLibrary) apiDistPath(apiScope *apiScope) string {
1066 if module.ModuleBase.Owner() != "" {
1067 return path.Join("apistubs", module.ModuleBase.Owner(), apiScope.name)
1068 } else if Bool(module.sdkLibraryProperties.Core_lib) {
1069 return path.Join("apistubs", "core", apiScope.name)
1070 } else {
1071 return path.Join("apistubs", "android", apiScope.name)
1072 }
1073}
1074
Paul Duffin12ceb462019-12-24 20:31:31 +00001075// Get the sdk version for use when compiling the stubs library.
Paul Duffin780c5f42020-05-12 15:52:55 +01001076func (module *SdkLibrary) sdkVersionForStubsLibrary(mctx android.EarlyModuleContext, apiScope *apiScope) string {
Paul Duffin87a05a32020-05-12 11:50:28 +01001077 scopeProperties := module.scopeToProperties[apiScope]
1078 if scopeProperties.Sdk_version != nil {
1079 return proptools.String(scopeProperties.Sdk_version)
1080 }
1081
Paul Duffin12ceb462019-12-24 20:31:31 +00001082 sdkDep := decodeSdkDep(mctx, sdkContext(&module.Library))
1083 if sdkDep.hasStandardLibs() {
1084 // If building against a standard sdk then use the sdk version appropriate for the scope.
Paul Duffind1b3a922020-01-22 11:57:20 +00001085 return apiScope.sdkVersion
Paul Duffin12ceb462019-12-24 20:31:31 +00001086 } else {
1087 // Otherwise, use no system module.
1088 return "none"
1089 }
1090}
1091
Paul Duffind1b3a922020-01-22 11:57:20 +00001092func (module *SdkLibrary) latestApiFilegroupName(apiScope *apiScope) string {
1093 return ":" + module.BaseModuleName() + ".api." + apiScope.name + ".latest"
Jiyong Park58c518b2018-05-12 22:29:12 +09001094}
Jiyong Parkc678ad32018-04-10 13:07:10 +09001095
Paul Duffind1b3a922020-01-22 11:57:20 +00001096func (module *SdkLibrary) latestRemovedApiFilegroupName(apiScope *apiScope) string {
1097 return ":" + module.BaseModuleName() + "-removed.api." + apiScope.name + ".latest"
Jiyong Parkc678ad32018-04-10 13:07:10 +09001098}
1099
Paul Duffin5df79302020-05-16 15:52:12 +01001100// Creates the implementation java library
1101func (module *SdkLibrary) createImplLibrary(mctx android.DefaultableHookContext) {
Paul Duffina2058f82020-06-24 16:22:38 +01001102
1103 moduleNamePtr := proptools.StringPtr(module.BaseModuleName())
1104
Paul Duffin5df79302020-05-16 15:52:12 +01001105 props := struct {
Paul Duffina2058f82020-06-24 16:22:38 +01001106 Name *string
1107 Visibility []string
1108 Instrument bool
1109 ConfigurationName *string
Paul Duffin5df79302020-05-16 15:52:12 +01001110 }{
1111 Name: proptools.StringPtr(module.implLibraryModuleName()),
1112 Visibility: module.sdkLibraryProperties.Impl_library_visibility,
Paul Duffin296cf332020-06-18 21:09:55 +01001113 // Set the instrument property to ensure it is instrumented when instrumentation is required.
1114 Instrument: true,
Paul Duffina2058f82020-06-24 16:22:38 +01001115
1116 // Make the created library behave as if it had the same name as this module.
1117 ConfigurationName: moduleNamePtr,
Paul Duffin5df79302020-05-16 15:52:12 +01001118 }
1119
1120 properties := []interface{}{
1121 &module.properties,
1122 &module.protoProperties,
1123 &module.deviceProperties,
1124 &module.dexpreoptProperties,
Colin Cross014489c2020-06-02 20:09:13 -07001125 &module.linter.properties,
Paul Duffin5df79302020-05-16 15:52:12 +01001126 &props,
1127 module.sdkComponentPropertiesForChildLibrary(),
1128 }
1129 mctx.CreateModule(LibraryFactory, properties...)
1130}
1131
Jiyong Parkc678ad32018-04-10 13:07:10 +09001132// Creates a static java library that has API stubs
Paul Duffinf0229202020-04-29 16:47:28 +01001133func (module *SdkLibrary) createStubsLibrary(mctx android.DefaultableHookContext, apiScope *apiScope) {
Jiyong Parkc678ad32018-04-10 13:07:10 +09001134 props := struct {
Paul Duffin1dbe3ca2020-05-16 09:57:59 +01001135 Name *string
1136 Visibility []string
1137 Srcs []string
1138 Installable *bool
1139 Sdk_version *string
1140 System_modules *string
1141 Patch_module *string
1142 Libs []string
1143 Compile_dex *bool
1144 Java_version *string
1145 Product_variables struct {
Jiyong Park82484c02018-04-23 21:41:26 +09001146 Pdk struct {
1147 Enabled *bool
1148 }
Jiyong Parkc678ad32018-04-10 13:07:10 +09001149 }
Sundong Ahn054b19a2018-10-19 13:46:09 +09001150 Openjdk9 struct {
1151 Srcs []string
1152 Javacflags []string
1153 }
Anton Hansson5fd5d242020-03-27 19:43:19 +00001154 Dist struct {
1155 Targets []string
1156 Dest *string
1157 Dir *string
1158 Tag *string
1159 }
Jiyong Parkc678ad32018-04-10 13:07:10 +09001160 }{}
1161
Paul Duffinc3091c82020-05-08 14:16:20 +01001162 props.Name = proptools.StringPtr(module.stubsLibraryModuleName(apiScope))
Paul Duffin4911a892020-04-29 23:35:13 +01001163
1164 // If stubs_library_visibility is not set then the created module will use the
1165 // visibility of this module.
1166 visibility := module.sdkLibraryProperties.Stubs_library_visibility
1167 props.Visibility = visibility
1168
Jiyong Parkc678ad32018-04-10 13:07:10 +09001169 // sources are generated from the droiddoc
Paul Duffinc3091c82020-05-08 14:16:20 +01001170 props.Srcs = []string{":" + module.stubsSourceModuleName(apiScope)}
Paul Duffin12ceb462019-12-24 20:31:31 +00001171 sdkVersion := module.sdkVersionForStubsLibrary(mctx, apiScope)
Paul Duffin52d398a2019-06-11 12:31:14 +01001172 props.Sdk_version = proptools.StringPtr(sdkVersion)
Paul Duffina18abc22020-05-16 18:54:24 +01001173 props.System_modules = module.deviceProperties.System_modules
1174 props.Patch_module = module.properties.Patch_module
Paul Duffin367ab912019-12-23 19:40:36 +00001175 props.Installable = proptools.BoolPtr(false)
Sundong Ahn054b19a2018-10-19 13:46:09 +09001176 props.Libs = module.sdkLibraryProperties.Stub_only_libs
Jiyong Park82484c02018-04-23 21:41:26 +09001177 props.Product_variables.Pdk.Enabled = proptools.BoolPtr(false)
Paul Duffina18abc22020-05-16 18:54:24 +01001178 props.Openjdk9.Srcs = module.properties.Openjdk9.Srcs
1179 props.Openjdk9.Javacflags = module.properties.Openjdk9.Javacflags
Anton Hansson83509b52020-05-21 09:21:57 +01001180 // We compile the stubs for 1.8 in line with the main android.jar stubs, and potential
1181 // interop with older developer tools that don't support 1.9.
1182 props.Java_version = proptools.StringPtr("1.8")
Paul Duffina18abc22020-05-16 18:54:24 +01001183 if module.deviceProperties.Compile_dex != nil {
1184 props.Compile_dex = module.deviceProperties.Compile_dex
Sundong Ahndd567f92018-07-31 17:19:11 +09001185 }
Jiyong Parkc678ad32018-04-10 13:07:10 +09001186
Anton Hansson5fd5d242020-03-27 19:43:19 +00001187 // Dist the class jar artifact for sdk builds.
1188 if !Bool(module.sdkLibraryProperties.No_dist) {
1189 props.Dist.Targets = []string{"sdk", "win_sdk"}
1190 props.Dist.Dest = proptools.StringPtr(fmt.Sprintf("%v.jar", module.BaseModuleName()))
1191 props.Dist.Dir = proptools.StringPtr(module.apiDistPath(apiScope))
1192 props.Dist.Tag = proptools.StringPtr(".jar")
1193 }
Jiyong Parkc678ad32018-04-10 13:07:10 +09001194
Paul Duffin859fe962020-05-15 10:20:31 +01001195 mctx.CreateModule(LibraryFactory, &props, module.sdkComponentPropertiesForChildLibrary())
Jiyong Parkc678ad32018-04-10 13:07:10 +09001196}
1197
Paul Duffin6d0886e2020-04-07 18:49:53 +01001198// Creates a droidstubs module that creates stubs source files from the given full source
Paul Duffinc8782502020-04-29 20:45:27 +01001199// files and also updates and checks the API specification files.
Paul Duffin0ff08bd2020-04-29 13:30:54 +01001200func (module *SdkLibrary) createStubsSourcesAndApi(mctx android.DefaultableHookContext, apiScope *apiScope, name string, createStubSources, createApi bool, scopeSpecificDroidstubsArgs []string) {
Jiyong Parkc678ad32018-04-10 13:07:10 +09001201 props := struct {
Sundong Ahn054b19a2018-10-19 13:46:09 +09001202 Name *string
Paul Duffin4911a892020-04-29 23:35:13 +01001203 Visibility []string
Sundong Ahn054b19a2018-10-19 13:46:09 +09001204 Srcs []string
1205 Installable *bool
Paul Duffin52d398a2019-06-11 12:31:14 +01001206 Sdk_version *string
Paul Duffin12ceb462019-12-24 20:31:31 +00001207 System_modules *string
Sundong Ahn054b19a2018-10-19 13:46:09 +09001208 Libs []string
Paul Duffin11512472019-02-11 15:55:17 +00001209 Arg_files []string
Sundong Ahn054b19a2018-10-19 13:46:09 +09001210 Args *string
Sundong Ahn054b19a2018-10-19 13:46:09 +09001211 Java_version *string
1212 Merge_annotations_dirs []string
1213 Merge_inclusion_annotations_dirs []string
Paul Duffin0ff08bd2020-04-29 13:30:54 +01001214 Generate_stubs *bool
Sundong Ahn054b19a2018-10-19 13:46:09 +09001215 Check_api struct {
Inseob Kim38449af2019-02-28 14:24:05 +09001216 Current ApiToCheck
1217 Last_released ApiToCheck
1218 Ignore_missing_latest_api *bool
Paul Duffin160fe412020-05-10 19:32:20 +01001219
1220 Api_lint struct {
1221 Enabled *bool
1222 New_since *string
1223 Baseline_file *string
1224 }
Jiyong Park58c518b2018-05-12 22:29:12 +09001225 }
Sundong Ahn1b92c822018-05-29 11:35:17 +09001226 Aidl struct {
1227 Include_dirs []string
1228 Local_include_dirs []string
1229 }
Anton Hansson5fd5d242020-03-27 19:43:19 +00001230 Dist struct {
1231 Targets []string
1232 Dest *string
1233 Dir *string
1234 }
Jiyong Parkc678ad32018-04-10 13:07:10 +09001235 }{}
1236
Paul Duffin7b78b4d2020-04-28 14:08:32 +01001237 // The stubs source processing uses the same compile time classpath when extracting the
1238 // API from the implementation library as it does when compiling it. i.e. the same
1239 // * sdk version
1240 // * system_modules
1241 // * libs (static_libs/libs)
Paul Duffin250e6192019-06-07 10:44:37 +01001242
Paul Duffin0ff08bd2020-04-29 13:30:54 +01001243 props.Name = proptools.StringPtr(name)
Paul Duffin4911a892020-04-29 23:35:13 +01001244
1245 // If stubs_source_visibility is not set then the created module will use the
1246 // visibility of this module.
1247 visibility := module.sdkLibraryProperties.Stubs_source_visibility
1248 props.Visibility = visibility
1249
Paul Duffina18abc22020-05-16 18:54:24 +01001250 props.Srcs = append(props.Srcs, module.properties.Srcs...)
1251 props.Sdk_version = module.deviceProperties.Sdk_version
1252 props.System_modules = module.deviceProperties.System_modules
Jiyong Parkc678ad32018-04-10 13:07:10 +09001253 props.Installable = proptools.BoolPtr(false)
Sundong Ahne6f0b052018-06-05 16:46:14 +09001254 // A droiddoc module has only one Libs property and doesn't distinguish between
1255 // shared libs and static libs. So we need to add both of these libs to Libs property.
Paul Duffina18abc22020-05-16 18:54:24 +01001256 props.Libs = module.properties.Libs
1257 props.Libs = append(props.Libs, module.properties.Static_libs...)
1258 props.Aidl.Include_dirs = module.deviceProperties.Aidl.Include_dirs
1259 props.Aidl.Local_include_dirs = module.deviceProperties.Aidl.Local_include_dirs
1260 props.Java_version = module.properties.Java_version
Jiyong Parkc678ad32018-04-10 13:07:10 +09001261
Sundong Ahn054b19a2018-10-19 13:46:09 +09001262 props.Merge_annotations_dirs = module.sdkLibraryProperties.Merge_annotations_dirs
1263 props.Merge_inclusion_annotations_dirs = module.sdkLibraryProperties.Merge_inclusion_annotations_dirs
1264
Paul Duffin6d0886e2020-04-07 18:49:53 +01001265 droidstubsArgs := []string{}
Paul Duffin235ffff2019-12-24 10:41:30 +00001266 if len(module.sdkLibraryProperties.Api_packages) != 0 {
Paul Duffin6d0886e2020-04-07 18:49:53 +01001267 droidstubsArgs = append(droidstubsArgs, "--stub-packages "+strings.Join(module.sdkLibraryProperties.Api_packages, ":"))
Paul Duffin235ffff2019-12-24 10:41:30 +00001268 }
1269 if len(module.sdkLibraryProperties.Hidden_api_packages) != 0 {
Paul Duffin6d0886e2020-04-07 18:49:53 +01001270 droidstubsArgs = append(droidstubsArgs,
Paul Duffin235ffff2019-12-24 10:41:30 +00001271 android.JoinWithPrefix(module.sdkLibraryProperties.Hidden_api_packages, " --hide-package "))
1272 }
Paul Duffin6d0886e2020-04-07 18:49:53 +01001273 droidstubsArgs = append(droidstubsArgs, module.sdkLibraryProperties.Droiddoc_options...)
Paul Duffin235ffff2019-12-24 10:41:30 +00001274 disabledWarnings := []string{
1275 "MissingPermission",
1276 "BroadcastBehavior",
1277 "HiddenSuperclass",
1278 "DeprecationMismatch",
1279 "UnavailableSymbol",
1280 "SdkConstant",
1281 "HiddenTypeParameter",
1282 "Todo",
1283 "Typo",
1284 }
Paul Duffin6d0886e2020-04-07 18:49:53 +01001285 droidstubsArgs = append(droidstubsArgs, android.JoinWithPrefix(disabledWarnings, "--hide "))
Sundong Ahnfb2721f2018-09-17 13:23:09 +09001286
Paul Duffin0ff08bd2020-04-29 13:30:54 +01001287 if !createStubSources {
1288 // Stubs are not required.
1289 props.Generate_stubs = proptools.BoolPtr(false)
1290 }
1291
Paul Duffin1fb487d2020-04-07 18:50:10 +01001292 // Add in scope specific arguments.
Paul Duffin0ff08bd2020-04-29 13:30:54 +01001293 droidstubsArgs = append(droidstubsArgs, scopeSpecificDroidstubsArgs...)
Paul Duffin11512472019-02-11 15:55:17 +00001294 props.Arg_files = module.sdkLibraryProperties.Droiddoc_option_files
Paul Duffin6d0886e2020-04-07 18:49:53 +01001295 props.Args = proptools.StringPtr(strings.Join(droidstubsArgs, " "))
Jiyong Parkc678ad32018-04-10 13:07:10 +09001296
Paul Duffin0ff08bd2020-04-29 13:30:54 +01001297 if createApi {
1298 // List of APIs identified from the provided source files are created. They are later
1299 // compared against to the not-yet-released (a.k.a current) list of APIs and to the
1300 // last-released (a.k.a numbered) list of API.
1301 currentApiFileName := apiScope.apiFilePrefix + "current.txt"
1302 removedApiFileName := apiScope.apiFilePrefix + "removed.txt"
1303 apiDir := module.getApiDir()
1304 currentApiFileName = path.Join(apiDir, currentApiFileName)
1305 removedApiFileName = path.Join(apiDir, removedApiFileName)
Jiyong Parkc678ad32018-04-10 13:07:10 +09001306
Paul Duffin0ff08bd2020-04-29 13:30:54 +01001307 // check against the not-yet-release API
1308 props.Check_api.Current.Api_file = proptools.StringPtr(currentApiFileName)
1309 props.Check_api.Current.Removed_api_file = proptools.StringPtr(removedApiFileName)
Jiyong Park58c518b2018-05-12 22:29:12 +09001310
Paul Duffin0ff08bd2020-04-29 13:30:54 +01001311 if !apiScope.unstable {
1312 // check against the latest released API
1313 latestApiFilegroupName := proptools.StringPtr(module.latestApiFilegroupName(apiScope))
1314 props.Check_api.Last_released.Api_file = latestApiFilegroupName
1315 props.Check_api.Last_released.Removed_api_file = proptools.StringPtr(
1316 module.latestRemovedApiFilegroupName(apiScope))
1317 props.Check_api.Ignore_missing_latest_api = proptools.BoolPtr(true)
Paul Duffin160fe412020-05-10 19:32:20 +01001318
Paul Duffin0ff08bd2020-04-29 13:30:54 +01001319 if proptools.Bool(module.sdkLibraryProperties.Api_lint.Enabled) {
1320 // Enable api lint.
1321 props.Check_api.Api_lint.Enabled = proptools.BoolPtr(true)
1322 props.Check_api.Api_lint.New_since = latestApiFilegroupName
Paul Duffin160fe412020-05-10 19:32:20 +01001323
Paul Duffin0ff08bd2020-04-29 13:30:54 +01001324 // If it exists then pass a lint-baseline.txt through to droidstubs.
1325 baselinePath := path.Join(apiDir, apiScope.apiFilePrefix+"lint-baseline.txt")
1326 baselinePathRelativeToRoot := path.Join(mctx.ModuleDir(), baselinePath)
1327 paths, err := mctx.GlobWithDeps(baselinePathRelativeToRoot, nil)
1328 if err != nil {
1329 mctx.ModuleErrorf("error checking for presence of %s: %s", baselinePathRelativeToRoot, err)
1330 }
1331 if len(paths) == 1 {
1332 props.Check_api.Api_lint.Baseline_file = proptools.StringPtr(baselinePath)
1333 } else if len(paths) != 0 {
1334 mctx.ModuleErrorf("error checking for presence of %s: expected one path, found: %v", baselinePathRelativeToRoot, paths)
1335 }
Paul Duffin160fe412020-05-10 19:32:20 +01001336 }
1337 }
Jiyong Park58c518b2018-05-12 22:29:12 +09001338
Paul Duffin0ff08bd2020-04-29 13:30:54 +01001339 // Dist the api txt artifact for sdk builds.
1340 if !Bool(module.sdkLibraryProperties.No_dist) {
1341 props.Dist.Targets = []string{"sdk", "win_sdk"}
1342 props.Dist.Dest = proptools.StringPtr(fmt.Sprintf("%v.txt", module.BaseModuleName()))
1343 props.Dist.Dir = proptools.StringPtr(path.Join(module.apiDistPath(apiScope), "api"))
1344 }
Anton Hansson5fd5d242020-03-27 19:43:19 +00001345 }
1346
Colin Cross84dfc3d2019-09-25 11:33:01 -07001347 mctx.CreateModule(DroidstubsFactory, &props)
Jiyong Parkc678ad32018-04-10 13:07:10 +09001348}
1349
Jooyung Han5e9013b2020-03-10 06:23:13 +09001350func (module *SdkLibrary) DepIsInSameApex(mctx android.BaseModuleContext, dep android.Module) bool {
1351 depTag := mctx.OtherModuleDependencyTag(dep)
1352 if depTag == xmlPermissionsFileTag {
1353 return true
1354 }
1355 return module.Library.DepIsInSameApex(mctx, dep)
1356}
1357
Jiyong Parkc678ad32018-04-10 13:07:10 +09001358// Creates the xml file that publicizes the runtime library
Paul Duffinf0229202020-04-29 16:47:28 +01001359func (module *SdkLibrary) createXmlFile(mctx android.DefaultableHookContext) {
Jiyong Parke3833882020-02-17 17:28:10 +09001360 props := struct {
Paul Duffin1dbe3ca2020-05-16 09:57:59 +01001361 Name *string
1362 Lib_name *string
1363 Apex_available []string
Jiyong Parke3833882020-02-17 17:28:10 +09001364 }{
Paul Duffineedc5d52020-06-12 17:46:39 +01001365 Name: proptools.StringPtr(module.xmlPermissionsModuleName()),
Jooyung Han5e9013b2020-03-10 06:23:13 +09001366 Lib_name: proptools.StringPtr(module.BaseModuleName()),
1367 Apex_available: module.ApexProperties.Apex_available,
Jiyong Parkc678ad32018-04-10 13:07:10 +09001368 }
Jiyong Parke3833882020-02-17 17:28:10 +09001369
Jiyong Parke3833882020-02-17 17:28:10 +09001370 mctx.CreateModule(sdkLibraryXmlFactory, &props)
Jiyong Parkc678ad32018-04-10 13:07:10 +09001371}
1372
Paul Duffin50061512020-01-21 16:31:05 +00001373func PrebuiltJars(ctx android.BaseModuleContext, baseName string, s sdkSpec) android.Paths {
Jiyong Park6a927c42020-01-21 02:03:43 +09001374 var ver sdkVersion
1375 var kind sdkKind
1376 if s.usePrebuilt(ctx) {
1377 ver = s.version
1378 kind = s.kind
Jiyong Parkc678ad32018-04-10 13:07:10 +09001379 } else {
Jiyong Park6a927c42020-01-21 02:03:43 +09001380 // We don't have prebuilt SDK for the specific sdkVersion.
1381 // Instead of breaking the build, fallback to use "system_current"
1382 ver = sdkVersionCurrent
1383 kind = sdkSystem
Sundong Ahn054b19a2018-10-19 13:46:09 +09001384 }
Jiyong Park6a927c42020-01-21 02:03:43 +09001385
1386 dir := filepath.Join("prebuilts", "sdk", ver.String(), kind.String())
Paul Duffin50061512020-01-21 16:31:05 +00001387 jar := filepath.Join(dir, baseName+".jar")
Sundong Ahn054b19a2018-10-19 13:46:09 +09001388 jarPath := android.ExistentPathForSource(ctx, jar)
Sundong Ahnae418ac2019-02-28 15:01:28 +09001389 if !jarPath.Valid() {
Colin Cross07c88562020-01-07 09:34:44 -08001390 if ctx.Config().AllowMissingDependencies() {
1391 return android.Paths{android.PathForSource(ctx, jar)}
1392 } else {
Jiyong Park6a927c42020-01-21 02:03:43 +09001393 ctx.PropertyErrorf("sdk_library", "invalid sdk version %q, %q does not exist", s.raw, jar)
Colin Cross07c88562020-01-07 09:34:44 -08001394 }
Sundong Ahnae418ac2019-02-28 15:01:28 +09001395 return nil
1396 }
Sundong Ahn054b19a2018-10-19 13:46:09 +09001397 return android.Paths{jarPath.Path()}
1398}
1399
Paul Duffin9b879592020-05-26 13:21:35 +01001400// Get the apex name for module, "" if it is for platform.
1401func getApexNameForModule(module android.Module) string {
1402 if apex, ok := module.(android.ApexModule); ok {
1403 return apex.ApexName()
1404 }
1405
1406 return ""
1407}
1408
1409// Check to see if the other module is within the same named APEX as this module.
1410//
1411// If either this or the other module are on the platform then this will return
1412// false.
Paul Duffineedc5d52020-06-12 17:46:39 +01001413func withinSameApexAs(module android.ApexModule, other android.Module) bool {
Paul Duffin9b879592020-05-26 13:21:35 +01001414 name := module.ApexName()
1415 return name != "" && getApexNameForModule(other) == name
1416}
1417
Paul Duffinb05d4292020-05-20 12:19:10 +01001418func (module *SdkLibrary) sdkJars(ctx android.BaseModuleContext, sdkVersion sdkSpec, headerJars bool) android.Paths {
Jiyong Park932cdfe2020-05-28 00:19:53 +09001419 // If the client doesn't set sdk_version, but if this library prefers stubs over
1420 // the impl library, let's provide the widest API surface possible. To do so,
1421 // force override sdk_version to module_current so that the closest possible API
1422 // surface could be found in selectHeaderJarsForSdkVersion
1423 if module.defaultsToStubs() && !sdkVersion.specified() {
1424 sdkVersion = sdkSpecFrom("module_current")
1425 }
Paul Duffind1b3a922020-01-22 11:57:20 +00001426
Paul Duffindaaa3322020-05-26 18:13:57 +01001427 // Only provide access to the implementation library if it is actually built.
1428 if module.requiresRuntimeImplementationLibrary() {
1429 // Check any special cases for java_sdk_library.
1430 //
1431 // Only allow access to the implementation library in the following condition:
1432 // * No sdk_version specified on the referencing module.
Paul Duffin9b879592020-05-26 13:21:35 +01001433 // * The referencing module is in the same apex as this.
Paul Duffineedc5d52020-06-12 17:46:39 +01001434 if sdkVersion.kind == sdkPrivate || withinSameApexAs(module, ctx.Module()) {
Paul Duffindaaa3322020-05-26 18:13:57 +01001435 if headerJars {
1436 return module.HeaderJars()
1437 } else {
1438 return module.ImplementationJars()
1439 }
Sundong Ahn054b19a2018-10-19 13:46:09 +09001440 }
Jiyong Parkc678ad32018-04-10 13:07:10 +09001441 }
Paul Duffinb05d4292020-05-20 12:19:10 +01001442
Paul Duffin23970f42020-05-20 14:20:02 +01001443 return module.selectHeaderJarsForSdkVersion(ctx, sdkVersion)
Jiyong Parkc678ad32018-04-10 13:07:10 +09001444}
1445
Sundong Ahn241cd372018-07-13 16:16:44 +09001446// to satisfy SdkLibraryDependency interface
Paul Duffind1b3a922020-01-22 11:57:20 +00001447func (module *SdkLibrary) SdkHeaderJars(ctx android.BaseModuleContext, sdkVersion sdkSpec) android.Paths {
1448 return module.sdkJars(ctx, sdkVersion, true /*headerJars*/)
1449}
1450
1451// to satisfy SdkLibraryDependency interface
Jiyong Park6a927c42020-01-21 02:03:43 +09001452func (module *SdkLibrary) SdkImplementationJars(ctx android.BaseModuleContext, sdkVersion sdkSpec) android.Paths {
Paul Duffind1b3a922020-01-22 11:57:20 +00001453 return module.sdkJars(ctx, sdkVersion, false /*headerJars*/)
Sundong Ahn241cd372018-07-13 16:16:44 +09001454}
1455
Sundong Ahn80a87b32019-05-13 15:02:50 +09001456func (module *SdkLibrary) SetNoDist() {
1457 module.sdkLibraryProperties.No_dist = proptools.BoolPtr(true)
1458}
1459
Colin Cross571cccf2019-02-04 11:22:08 -08001460var javaSdkLibrariesKey = android.NewOnceKey("javaSdkLibraries")
1461
Jiyong Park82484c02018-04-23 21:41:26 +09001462func javaSdkLibraries(config android.Config) *[]string {
Colin Cross571cccf2019-02-04 11:22:08 -08001463 return config.Once(javaSdkLibrariesKey, func() interface{} {
Jiyong Park82484c02018-04-23 21:41:26 +09001464 return &[]string{}
1465 }).(*[]string)
1466}
1467
Paul Duffin749f98f2019-12-30 17:23:46 +00001468func (module *SdkLibrary) getApiDir() string {
1469 return proptools.StringDefault(module.sdkLibraryProperties.Api_dir, "api")
1470}
1471
Jiyong Parkc678ad32018-04-10 13:07:10 +09001472// For a java_sdk_library module, create internal modules for stubs, docs,
1473// runtime libs and xml file. If requested, the stubs and docs are created twice
1474// once for public API level and once for system API level
Paul Duffinf0229202020-04-29 16:47:28 +01001475func (module *SdkLibrary) CreateInternalModules(mctx android.DefaultableHookContext) {
1476 // If the module has been disabled then don't create any child modules.
1477 if !module.Enabled() {
1478 return
1479 }
1480
Paul Duffina18abc22020-05-16 18:54:24 +01001481 if len(module.properties.Srcs) == 0 {
Inseob Kimc0907f12019-02-08 21:00:45 +09001482 mctx.PropertyErrorf("srcs", "java_sdk_library must specify srcs")
Jooyung Han58f26ab2019-12-18 15:34:32 +09001483 return
Inseob Kimc0907f12019-02-08 21:00:45 +09001484 }
1485
Paul Duffin37e0b772019-12-30 17:20:10 +00001486 // If this builds against standard libraries (i.e. is not part of the core libraries)
1487 // then assume it provides both system and test apis. Otherwise, assume it does not and
1488 // also assume it does not contribute to the dist build.
1489 sdkDep := decodeSdkDep(mctx, sdkContext(&module.Library))
1490 hasSystemAndTestApis := sdkDep.hasStandardLibs()
Paul Duffin3375e352020-04-28 10:44:03 +01001491 module.sdkLibraryProperties.Generate_system_and_test_apis = hasSystemAndTestApis
Paul Duffin37e0b772019-12-30 17:20:10 +00001492 module.sdkLibraryProperties.No_dist = proptools.BoolPtr(!hasSystemAndTestApis)
1493
Inseob Kim8098faa2019-03-18 10:19:51 +09001494 missing_current_api := false
1495
Paul Duffin3375e352020-04-28 10:44:03 +01001496 generatedScopes := module.getGeneratedApiScopes(mctx)
Paul Duffind1b3a922020-01-22 11:57:20 +00001497
Paul Duffin749f98f2019-12-30 17:23:46 +00001498 apiDir := module.getApiDir()
Paul Duffin3375e352020-04-28 10:44:03 +01001499 for _, scope := range generatedScopes {
Inseob Kim8098faa2019-03-18 10:19:51 +09001500 for _, api := range []string{"current.txt", "removed.txt"} {
Paul Duffind1b3a922020-01-22 11:57:20 +00001501 path := path.Join(mctx.ModuleDir(), apiDir, scope.apiFilePrefix+api)
Inseob Kim8098faa2019-03-18 10:19:51 +09001502 p := android.ExistentPathForSource(mctx, path)
1503 if !p.Valid() {
1504 mctx.ModuleErrorf("Current api file %#v doesn't exist", path)
1505 missing_current_api = true
1506 }
1507 }
1508 }
1509
1510 if missing_current_api {
1511 script := "build/soong/scripts/gen-java-current-api-files.sh"
1512 p := android.ExistentPathForSource(mctx, script)
1513
1514 if !p.Valid() {
1515 panic(fmt.Sprintf("script file %s doesn't exist", script))
1516 }
1517
1518 mctx.ModuleErrorf("One or more current api files are missing. "+
1519 "You can update them by:\n"+
Paul Duffin37e0b772019-12-30 17:20:10 +00001520 "%s %q %s && m update-api",
Paul Duffind1b3a922020-01-22 11:57:20 +00001521 script, filepath.Join(mctx.ModuleDir(), apiDir),
Paul Duffin3375e352020-04-28 10:44:03 +01001522 strings.Join(generatedScopes.Strings(func(s *apiScope) string { return s.apiFilePrefix }), " "))
Inseob Kim8098faa2019-03-18 10:19:51 +09001523 return
1524 }
1525
Paul Duffin3375e352020-04-28 10:44:03 +01001526 for _, scope := range generatedScopes {
Paul Duffin0ff08bd2020-04-29 13:30:54 +01001527 stubsSourceArgs := scope.droidstubsArgsForGeneratingStubsSource
Paul Duffinc3091c82020-05-08 14:16:20 +01001528 stubsSourceModuleName := module.stubsSourceModuleName(scope)
Paul Duffin0ff08bd2020-04-29 13:30:54 +01001529
1530 // If the args needed to generate the stubs and API are the same then they
1531 // can be generated in a single invocation of metalava, otherwise they will
1532 // need separate invocations.
1533 if scope.createStubsSourceAndApiTogether {
1534 // Use the stubs source name for legacy reasons.
1535 module.createStubsSourcesAndApi(mctx, scope, stubsSourceModuleName, true, true, stubsSourceArgs)
1536 } else {
1537 module.createStubsSourcesAndApi(mctx, scope, stubsSourceModuleName, true, false, stubsSourceArgs)
1538
1539 apiArgs := scope.droidstubsArgsForGeneratingApi
Paul Duffinc3091c82020-05-08 14:16:20 +01001540 apiName := module.apiModuleName(scope)
Paul Duffin0ff08bd2020-04-29 13:30:54 +01001541 module.createStubsSourcesAndApi(mctx, scope, apiName, false, true, apiArgs)
1542 }
1543
Paul Duffind1b3a922020-01-22 11:57:20 +00001544 module.createStubsLibrary(mctx, scope)
Inseob Kimc0907f12019-02-08 21:00:45 +09001545 }
1546
Paul Duffindfa131e2020-05-15 20:37:11 +01001547 if module.requiresRuntimeImplementationLibrary() {
Paul Duffin5df79302020-05-16 15:52:12 +01001548 // Create child module to create an implementation library.
1549 //
1550 // This temporarily creates a second implementation library that can be explicitly
1551 // referenced.
1552 //
1553 // TODO(b/156618935) - update comment once only one implementation library is created.
1554 module.createImplLibrary(mctx)
1555
Paul Duffindfa131e2020-05-15 20:37:11 +01001556 // Only create an XML permissions file that declares the library as being usable
1557 // as a shared library if required.
1558 if module.sharedLibrary() {
1559 module.createXmlFile(mctx)
1560 }
Paul Duffin43db9be2019-12-30 17:35:49 +00001561
1562 // record java_sdk_library modules so that they are exported to make
1563 javaSdkLibraries := javaSdkLibraries(mctx.Config())
1564 javaSdkLibrariesLock.Lock()
1565 defer javaSdkLibrariesLock.Unlock()
1566 *javaSdkLibraries = append(*javaSdkLibraries, module.BaseModuleName())
1567 }
Inseob Kimc0907f12019-02-08 21:00:45 +09001568}
1569
1570func (module *SdkLibrary) InitSdkLibraryProperties() {
Colin Crossce6734e2020-06-15 16:09:53 -07001571 module.addHostAndDeviceProperties()
1572 module.AddProperties(&module.sdkLibraryProperties)
Sundong Ahn054b19a2018-10-19 13:46:09 +09001573
Paul Duffin859fe962020-05-15 10:20:31 +01001574 module.initSdkLibraryComponent(&module.ModuleBase)
1575
Paul Duffina18abc22020-05-16 18:54:24 +01001576 module.properties.Installable = proptools.BoolPtr(true)
1577 module.deviceProperties.IsSDKLibrary = true
Inseob Kimc0907f12019-02-08 21:00:45 +09001578}
Sundong Ahn054b19a2018-10-19 13:46:09 +09001579
Paul Duffindfa131e2020-05-15 20:37:11 +01001580func (module *SdkLibrary) requiresRuntimeImplementationLibrary() bool {
1581 return !proptools.Bool(module.sdkLibraryProperties.Api_only)
1582}
1583
Jiyong Park932cdfe2020-05-28 00:19:53 +09001584func (module *SdkLibrary) defaultsToStubs() bool {
1585 return proptools.Bool(module.sdkLibraryProperties.Default_to_stubs)
1586}
1587
Paul Duffin1b1e8062020-05-08 13:44:43 +01001588// Defines how to name the individual component modules the sdk library creates.
1589type sdkLibraryComponentNamingScheme interface {
1590 stubsLibraryModuleName(scope *apiScope, baseName string) string
1591
1592 stubsSourceModuleName(scope *apiScope, baseName string) string
1593
1594 apiModuleName(scope *apiScope, baseName string) string
1595}
1596
1597type defaultNamingScheme struct {
1598}
1599
1600func (s *defaultNamingScheme) stubsLibraryModuleName(scope *apiScope, baseName string) string {
1601 return scope.stubsLibraryModuleName(baseName)
1602}
1603
1604func (s *defaultNamingScheme) stubsSourceModuleName(scope *apiScope, baseName string) string {
1605 return scope.stubsSourceModuleName(baseName)
1606}
1607
1608func (s *defaultNamingScheme) apiModuleName(scope *apiScope, baseName string) string {
1609 return scope.apiModuleName(baseName)
1610}
1611
1612var _ sdkLibraryComponentNamingScheme = (*defaultNamingScheme)(nil)
1613
Paul Duffin6c9c5fc2020-05-08 15:36:30 +01001614type frameworkModulesNamingScheme struct {
1615}
1616
1617func (s *frameworkModulesNamingScheme) moduleSuffix(scope *apiScope) string {
1618 suffix := scope.name
1619 if scope == apiScopeModuleLib {
1620 suffix = "module_libs_"
1621 }
1622 return suffix
1623}
1624
1625func (s *frameworkModulesNamingScheme) stubsLibraryModuleName(scope *apiScope, baseName string) string {
1626 return fmt.Sprintf("%s-stubs-%sapi", baseName, s.moduleSuffix(scope))
1627}
1628
1629func (s *frameworkModulesNamingScheme) stubsSourceModuleName(scope *apiScope, baseName string) string {
1630 return fmt.Sprintf("%s-stubs-srcs-%sapi", baseName, s.moduleSuffix(scope))
1631}
1632
1633func (s *frameworkModulesNamingScheme) apiModuleName(scope *apiScope, baseName string) string {
1634 return fmt.Sprintf("%s-api-%sapi", baseName, s.moduleSuffix(scope))
1635}
1636
1637var _ sdkLibraryComponentNamingScheme = (*frameworkModulesNamingScheme)(nil)
1638
Anton Hansson2d0c1942020-05-25 12:20:51 +01001639func moduleStubLinkType(name string) (stub bool, ret linkType) {
1640 // This suffix-based approach is fragile and could potentially mis-trigger.
1641 // TODO(b/155164730): Clean this up when modules no longer reference sdk_lib stubs directly.
1642 if strings.HasSuffix(name, ".stubs.public") || strings.HasSuffix(name, "-stubs-publicapi") {
1643 return true, javaSdk
1644 }
1645 if strings.HasSuffix(name, ".stubs.system") || strings.HasSuffix(name, "-stubs-systemapi") {
1646 return true, javaSystem
1647 }
1648 if strings.HasSuffix(name, ".stubs.module_lib") || strings.HasSuffix(name, "-stubs-module_libs_api") {
1649 return true, javaModule
1650 }
1651 if strings.HasSuffix(name, ".stubs.test") {
1652 return true, javaSystem
1653 }
1654 return false, javaPlatform
1655}
1656
Jaewoong Jung4f158ee2019-07-11 10:05:35 -07001657// java_sdk_library is a special Java library that provides optional platform APIs to apps.
1658// In practice, it can be viewed as a combination of several modules: 1) stubs library that clients
1659// are linked against to, 2) droiddoc module that internally generates API stubs source files,
1660// 3) the real runtime shared library that implements the APIs, and 4) XML file for adding
1661// the runtime lib to the classpath at runtime if requested via <uses-library>.
Inseob Kimc0907f12019-02-08 21:00:45 +09001662func SdkLibraryFactory() android.Module {
1663 module := &SdkLibrary{}
Paul Duffinc3091c82020-05-08 14:16:20 +01001664
1665 // Initialize information common between source and prebuilt.
1666 module.initCommon(&module.ModuleBase)
1667
Inseob Kimc0907f12019-02-08 21:00:45 +09001668 module.InitSdkLibraryProperties()
Jooyung Han58f26ab2019-12-18 15:34:32 +09001669 android.InitApexModule(module)
Sundong Ahn054b19a2018-10-19 13:46:09 +09001670 InitJavaModule(module, android.HostAndDeviceSupported)
Paul Duffin3375e352020-04-28 10:44:03 +01001671
1672 // Initialize the map from scope to scope specific properties.
1673 scopeToProperties := make(map[*apiScope]*ApiScopeProperties)
1674 for _, scope := range allApiScopes {
1675 scopeToProperties[scope] = scope.scopeSpecificProperties(module)
1676 }
1677 module.scopeToProperties = scopeToProperties
1678
Paul Duffin4911a892020-04-29 23:35:13 +01001679 // Add the properties containing visibility rules so that they are checked.
Paul Duffin5df79302020-05-16 15:52:12 +01001680 android.AddVisibilityProperty(module, "impl_library_visibility", &module.sdkLibraryProperties.Impl_library_visibility)
Paul Duffin4911a892020-04-29 23:35:13 +01001681 android.AddVisibilityProperty(module, "stubs_library_visibility", &module.sdkLibraryProperties.Stubs_library_visibility)
1682 android.AddVisibilityProperty(module, "stubs_source_visibility", &module.sdkLibraryProperties.Stubs_source_visibility)
1683
Paul Duffin1b1e8062020-05-08 13:44:43 +01001684 module.SetDefaultableHook(func(ctx android.DefaultableHookContext) {
Paul Duffindfa131e2020-05-15 20:37:11 +01001685 // If no implementation is required then it cannot be used as a shared library
1686 // either.
1687 if !module.requiresRuntimeImplementationLibrary() {
1688 // If shared_library has been explicitly set to true then it is incompatible
1689 // with api_only: true.
1690 if proptools.Bool(module.commonSdkLibraryProperties.Shared_library) {
1691 ctx.PropertyErrorf("api_only/shared_library", "inconsistent settings, shared_library and api_only cannot both be true")
1692 }
1693 // Set shared_library: false.
1694 module.commonSdkLibraryProperties.Shared_library = proptools.BoolPtr(false)
1695 }
1696
Paul Duffin1b1e8062020-05-08 13:44:43 +01001697 if module.initCommonAfterDefaultsApplied(ctx) {
1698 module.CreateInternalModules(ctx)
1699 }
1700 })
Jiyong Parkc678ad32018-04-10 13:07:10 +09001701 return module
1702}
Colin Cross79c7c262019-04-17 11:11:46 -07001703
1704//
1705// SDK library prebuilts
1706//
1707
Paul Duffin56d44902020-01-31 13:36:25 +00001708// Properties associated with each api scope.
1709type sdkLibraryScopeProperties struct {
Colin Cross79c7c262019-04-17 11:11:46 -07001710 Jars []string `android:"path"`
1711
1712 Sdk_version *string
1713
Colin Cross79c7c262019-04-17 11:11:46 -07001714 // List of shared java libs that this module has dependencies to
1715 Libs []string
Paul Duffin3d1248c2020-04-09 00:10:17 +01001716
Paul Duffinc8782502020-04-29 20:45:27 +01001717 // The stubs source.
Paul Duffin3d1248c2020-04-09 00:10:17 +01001718 Stub_srcs []string `android:"path"`
Paul Duffin1fd005d2020-04-09 01:08:11 +01001719
1720 // The current.txt
Paul Duffin0f8faff2020-05-20 16:18:00 +01001721 Current_api *string `android:"path"`
Paul Duffin1fd005d2020-04-09 01:08:11 +01001722
1723 // The removed.txt
Paul Duffin0f8faff2020-05-20 16:18:00 +01001724 Removed_api *string `android:"path"`
Colin Cross79c7c262019-04-17 11:11:46 -07001725}
1726
Paul Duffin56d44902020-01-31 13:36:25 +00001727type sdkLibraryImportProperties struct {
Paul Duffinfcfd7912020-01-31 17:54:30 +00001728 // List of shared java libs, common to all scopes, that this module has
1729 // dependencies to
1730 Libs []string
Paul Duffin56d44902020-01-31 13:36:25 +00001731}
1732
Paul Duffineedc5d52020-06-12 17:46:39 +01001733type SdkLibraryImport struct {
Colin Cross79c7c262019-04-17 11:11:46 -07001734 android.ModuleBase
1735 android.DefaultableModuleBase
1736 prebuilt android.Prebuilt
Paul Duffindd46f712020-02-10 13:37:10 +00001737 android.ApexModuleBase
1738 android.SdkBase
Colin Cross79c7c262019-04-17 11:11:46 -07001739
1740 properties sdkLibraryImportProperties
1741
Paul Duffin46a26a82020-04-07 19:27:04 +01001742 // Map from api scope to the scope specific property structure.
1743 scopeProperties map[*apiScope]*sdkLibraryScopeProperties
1744
Paul Duffin56d44902020-01-31 13:36:25 +00001745 commonToSdkLibraryAndImport
Paul Duffineedc5d52020-06-12 17:46:39 +01001746
1747 // The reference to the implementation library created by the source module.
1748 // Is nil if the source module does not exist.
1749 implLibraryModule *Library
1750
1751 // The reference to the xml permissions module created by the source module.
1752 // Is nil if the source module does not exist.
1753 xmlPermissionsFileModule *sdkLibraryXml
Colin Cross79c7c262019-04-17 11:11:46 -07001754}
1755
Paul Duffineedc5d52020-06-12 17:46:39 +01001756var _ SdkLibraryDependency = (*SdkLibraryImport)(nil)
Colin Cross79c7c262019-04-17 11:11:46 -07001757
Paul Duffin46a26a82020-04-07 19:27:04 +01001758// The type of a structure that contains a field of type sdkLibraryScopeProperties
1759// for each apiscope in allApiScopes, e.g. something like:
1760// struct {
1761// Public sdkLibraryScopeProperties
1762// System sdkLibraryScopeProperties
1763// ...
1764// }
1765var allScopeStructType = createAllScopePropertiesStructType()
1766
1767// Dynamically create a structure type for each apiscope in allApiScopes.
1768func createAllScopePropertiesStructType() reflect.Type {
1769 var fields []reflect.StructField
1770 for _, apiScope := range allApiScopes {
1771 field := reflect.StructField{
1772 Name: apiScope.fieldName,
1773 Type: reflect.TypeOf(sdkLibraryScopeProperties{}),
1774 }
1775 fields = append(fields, field)
1776 }
1777
1778 return reflect.StructOf(fields)
1779}
1780
1781// Create an instance of the scope specific structure type and return a map
1782// from apiscope to a pointer to each scope specific field.
1783func createPropertiesInstance() (interface{}, map[*apiScope]*sdkLibraryScopeProperties) {
1784 allScopePropertiesPtr := reflect.New(allScopeStructType)
1785 allScopePropertiesStruct := allScopePropertiesPtr.Elem()
1786 scopeProperties := make(map[*apiScope]*sdkLibraryScopeProperties)
1787
1788 for _, apiScope := range allApiScopes {
1789 field := allScopePropertiesStruct.FieldByName(apiScope.fieldName)
1790 scopeProperties[apiScope] = field.Addr().Interface().(*sdkLibraryScopeProperties)
1791 }
1792
1793 return allScopePropertiesPtr.Interface(), scopeProperties
1794}
1795
Jaewoong Jung4f158ee2019-07-11 10:05:35 -07001796// java_sdk_library_import imports a prebuilt java_sdk_library.
Colin Cross79c7c262019-04-17 11:11:46 -07001797func sdkLibraryImportFactory() android.Module {
Paul Duffineedc5d52020-06-12 17:46:39 +01001798 module := &SdkLibraryImport{}
Colin Cross79c7c262019-04-17 11:11:46 -07001799
Paul Duffin46a26a82020-04-07 19:27:04 +01001800 allScopeProperties, scopeToProperties := createPropertiesInstance()
1801 module.scopeProperties = scopeToProperties
1802 module.AddProperties(&module.properties, allScopeProperties)
Colin Cross79c7c262019-04-17 11:11:46 -07001803
Paul Duffinc3091c82020-05-08 14:16:20 +01001804 // Initialize information common between source and prebuilt.
1805 module.initCommon(&module.ModuleBase)
1806
Paul Duffin0bdcb272020-02-06 15:24:57 +00001807 android.InitPrebuiltModule(module, &[]string{""})
Paul Duffindd46f712020-02-10 13:37:10 +00001808 android.InitApexModule(module)
1809 android.InitSdkAwareModule(module)
Colin Cross79c7c262019-04-17 11:11:46 -07001810 InitJavaModule(module, android.HostAndDeviceSupported)
1811
Paul Duffin1b1e8062020-05-08 13:44:43 +01001812 module.SetDefaultableHook(func(mctx android.DefaultableHookContext) {
1813 if module.initCommonAfterDefaultsApplied(mctx) {
1814 module.createInternalModules(mctx)
1815 }
1816 })
Colin Cross79c7c262019-04-17 11:11:46 -07001817 return module
1818}
1819
Paul Duffineedc5d52020-06-12 17:46:39 +01001820func (module *SdkLibraryImport) Prebuilt() *android.Prebuilt {
Colin Cross79c7c262019-04-17 11:11:46 -07001821 return &module.prebuilt
1822}
1823
Paul Duffineedc5d52020-06-12 17:46:39 +01001824func (module *SdkLibraryImport) Name() string {
Colin Cross79c7c262019-04-17 11:11:46 -07001825 return module.prebuilt.Name(module.ModuleBase.Name())
1826}
1827
Paul Duffineedc5d52020-06-12 17:46:39 +01001828func (module *SdkLibraryImport) createInternalModules(mctx android.DefaultableHookContext) {
Colin Cross79c7c262019-04-17 11:11:46 -07001829
Paul Duffin50061512020-01-21 16:31:05 +00001830 // If the build is configured to use prebuilts then force this to be preferred.
1831 if mctx.Config().UnbundledBuildUsePrebuiltSdks() {
1832 module.prebuilt.ForcePrefer()
1833 }
1834
Paul Duffin46a26a82020-04-07 19:27:04 +01001835 for apiScope, scopeProperties := range module.scopeProperties {
Paul Duffin56d44902020-01-31 13:36:25 +00001836 if len(scopeProperties.Jars) == 0 {
1837 continue
1838 }
1839
Paul Duffinbbb546b2020-04-09 00:07:11 +01001840 module.createJavaImportForStubs(mctx, apiScope, scopeProperties)
Paul Duffin3d1248c2020-04-09 00:10:17 +01001841
Paul Duffin0f8faff2020-05-20 16:18:00 +01001842 if len(scopeProperties.Stub_srcs) > 0 {
1843 module.createPrebuiltStubsSources(mctx, apiScope, scopeProperties)
1844 }
Paul Duffin56d44902020-01-31 13:36:25 +00001845 }
Colin Cross79c7c262019-04-17 11:11:46 -07001846
1847 javaSdkLibraries := javaSdkLibraries(mctx.Config())
1848 javaSdkLibrariesLock.Lock()
1849 defer javaSdkLibrariesLock.Unlock()
1850 *javaSdkLibraries = append(*javaSdkLibraries, module.BaseModuleName())
1851}
1852
Paul Duffineedc5d52020-06-12 17:46:39 +01001853func (module *SdkLibraryImport) createJavaImportForStubs(mctx android.DefaultableHookContext, apiScope *apiScope, scopeProperties *sdkLibraryScopeProperties) {
Paul Duffinbbb546b2020-04-09 00:07:11 +01001854 // Creates a java import for the jar with ".stubs" suffix
1855 props := struct {
Paul Duffin1dbe3ca2020-05-16 09:57:59 +01001856 Name *string
1857 Sdk_version *string
1858 Libs []string
1859 Jars []string
1860 Prefer *bool
Paul Duffinbbb546b2020-04-09 00:07:11 +01001861 }{}
Paul Duffinc3091c82020-05-08 14:16:20 +01001862 props.Name = proptools.StringPtr(module.stubsLibraryModuleName(apiScope))
Paul Duffinbbb546b2020-04-09 00:07:11 +01001863 props.Sdk_version = scopeProperties.Sdk_version
1864 // Prepend any of the libs from the legacy public properties to the libs for each of the
1865 // scopes to avoid having to duplicate them in each scope.
1866 props.Libs = append(module.properties.Libs, scopeProperties.Libs...)
1867 props.Jars = scopeProperties.Jars
Paul Duffin1dbe3ca2020-05-16 09:57:59 +01001868
Paul Duffin38b57852020-05-13 16:08:09 +01001869 // The imports are preferred if the java_sdk_library_import is preferred.
1870 props.Prefer = proptools.BoolPtr(module.prebuilt.Prefer())
Paul Duffin859fe962020-05-15 10:20:31 +01001871
1872 mctx.CreateModule(ImportFactory, &props, module.sdkComponentPropertiesForChildLibrary())
Paul Duffinbbb546b2020-04-09 00:07:11 +01001873}
1874
Paul Duffineedc5d52020-06-12 17:46:39 +01001875func (module *SdkLibraryImport) createPrebuiltStubsSources(mctx android.DefaultableHookContext, apiScope *apiScope, scopeProperties *sdkLibraryScopeProperties) {
Paul Duffin3d1248c2020-04-09 00:10:17 +01001876 props := struct {
Paul Duffin38b57852020-05-13 16:08:09 +01001877 Name *string
1878 Srcs []string
1879 Prefer *bool
Paul Duffin3d1248c2020-04-09 00:10:17 +01001880 }{}
Paul Duffinc3091c82020-05-08 14:16:20 +01001881 props.Name = proptools.StringPtr(module.stubsSourceModuleName(apiScope))
Paul Duffin3d1248c2020-04-09 00:10:17 +01001882 props.Srcs = scopeProperties.Stub_srcs
1883 mctx.CreateModule(PrebuiltStubsSourcesFactory, &props)
Paul Duffin38b57852020-05-13 16:08:09 +01001884
1885 // The stubs source is preferred if the java_sdk_library_import is preferred.
1886 props.Prefer = proptools.BoolPtr(module.prebuilt.Prefer())
Paul Duffin3d1248c2020-04-09 00:10:17 +01001887}
1888
Paul Duffin44f1d842020-06-26 20:17:02 +01001889// Add the dependencies on the child module in the component deps mutator so that it
1890// creates references to the prebuilt and not the source modules.
1891func (module *SdkLibraryImport) ComponentDepsMutator(ctx android.BottomUpMutatorContext) {
Paul Duffin46a26a82020-04-07 19:27:04 +01001892 for apiScope, scopeProperties := range module.scopeProperties {
Paul Duffin56d44902020-01-31 13:36:25 +00001893 if len(scopeProperties.Jars) == 0 {
1894 continue
1895 }
1896
1897 // Add dependencies to the prebuilt stubs library
Paul Duffin44f1d842020-06-26 20:17:02 +01001898 ctx.AddVariationDependencies(nil, apiScope.stubsTag, "prebuilt_"+module.stubsLibraryModuleName(apiScope))
Paul Duffin0f8faff2020-05-20 16:18:00 +01001899
1900 if len(scopeProperties.Stub_srcs) > 0 {
1901 // Add dependencies to the prebuilt stubs source library
Paul Duffin44f1d842020-06-26 20:17:02 +01001902 ctx.AddVariationDependencies(nil, apiScope.stubsSourceTag, "prebuilt_"+module.stubsSourceModuleName(apiScope))
Paul Duffin0f8faff2020-05-20 16:18:00 +01001903 }
Paul Duffin56d44902020-01-31 13:36:25 +00001904 }
Paul Duffin44f1d842020-06-26 20:17:02 +01001905}
1906
1907// Add other dependencies as normal.
1908func (module *SdkLibraryImport) DepsMutator(ctx android.BottomUpMutatorContext) {
Paul Duffineedc5d52020-06-12 17:46:39 +01001909
1910 implName := module.implLibraryModuleName()
1911 if ctx.OtherModuleExists(implName) {
1912 ctx.AddVariationDependencies(nil, implLibraryTag, implName)
1913
1914 xmlPermissionsModuleName := module.xmlPermissionsModuleName()
1915 if module.sharedLibrary() && ctx.OtherModuleExists(xmlPermissionsModuleName) {
1916 // Add dependency to the rule for generating the xml permissions file
1917 ctx.AddDependency(module, xmlPermissionsFileTag, xmlPermissionsModuleName)
1918 }
1919 }
Colin Cross79c7c262019-04-17 11:11:46 -07001920}
1921
Paul Duffineedc5d52020-06-12 17:46:39 +01001922func (module *SdkLibraryImport) DepIsInSameApex(mctx android.BaseModuleContext, dep android.Module) bool {
1923 depTag := mctx.OtherModuleDependencyTag(dep)
1924 if depTag == xmlPermissionsFileTag {
1925 return true
1926 }
1927
1928 // None of the other dependencies of the java_sdk_library_import are in the same apex
1929 // as the one that references this module.
1930 return false
1931}
1932
Jooyung Han749dc692020-04-15 11:03:39 +09001933func (module *SdkLibraryImport) ShouldSupportSdkVersion(ctx android.BaseModuleContext, sdkVersion int) error {
1934 // we don't check prebuilt modules for sdk_version
1935 return nil
1936}
1937
Paul Duffineedc5d52020-06-12 17:46:39 +01001938func (module *SdkLibraryImport) OutputFiles(tag string) (android.Paths, error) {
Paul Duffin46dc45a2020-05-14 15:39:10 +01001939 return module.commonOutputFiles(tag)
1940}
1941
Paul Duffineedc5d52020-06-12 17:46:39 +01001942func (module *SdkLibraryImport) GenerateAndroidBuildActions(ctx android.ModuleContext) {
Paul Duffin0f8faff2020-05-20 16:18:00 +01001943 // Record the paths to the prebuilt stubs library and stubs source.
Colin Cross79c7c262019-04-17 11:11:46 -07001944 ctx.VisitDirectDeps(func(to android.Module) {
1945 tag := ctx.OtherModuleDependencyTag(to)
1946
Paul Duffin0f8faff2020-05-20 16:18:00 +01001947 // Extract information from any of the scope specific dependencies.
1948 if scopeTag, ok := tag.(scopeDependencyTag); ok {
1949 apiScope := scopeTag.apiScope
1950 scopePaths := module.getScopePathsCreateIfNeeded(apiScope)
1951
1952 // Extract information from the dependency. The exact information extracted
1953 // is determined by the nature of the dependency which is determined by the tag.
1954 scopeTag.extractDepInfo(ctx, to, scopePaths)
Paul Duffineedc5d52020-06-12 17:46:39 +01001955 } else if tag == implLibraryTag {
1956 if implLibrary, ok := to.(*Library); ok {
1957 module.implLibraryModule = implLibrary
1958 } else {
1959 ctx.ModuleErrorf("implementation library must be of type *java.Library but was %T", to)
1960 }
1961 } else if tag == xmlPermissionsFileTag {
1962 if xmlPermissionsFileModule, ok := to.(*sdkLibraryXml); ok {
1963 module.xmlPermissionsFileModule = xmlPermissionsFileModule
1964 } else {
1965 ctx.ModuleErrorf("xml permissions file module must be of type *sdkLibraryXml but was %T", to)
1966 }
Colin Cross79c7c262019-04-17 11:11:46 -07001967 }
1968 })
Paul Duffin0f8faff2020-05-20 16:18:00 +01001969
1970 // Populate the scope paths with information from the properties.
1971 for apiScope, scopeProperties := range module.scopeProperties {
1972 if len(scopeProperties.Jars) == 0 {
1973 continue
1974 }
1975
1976 paths := module.getScopePathsCreateIfNeeded(apiScope)
1977 paths.currentApiFilePath = android.OptionalPathForModuleSrc(ctx, scopeProperties.Current_api)
1978 paths.removedApiFilePath = android.OptionalPathForModuleSrc(ctx, scopeProperties.Removed_api)
1979 }
Colin Cross79c7c262019-04-17 11:11:46 -07001980}
1981
Paul Duffineedc5d52020-06-12 17:46:39 +01001982func (module *SdkLibraryImport) sdkJars(ctx android.BaseModuleContext, sdkVersion sdkSpec, headerJars bool) android.Paths {
1983
1984 // For consistency with SdkLibrary make the implementation jar available to libraries that
1985 // are within the same APEX.
1986 implLibraryModule := module.implLibraryModule
1987 if implLibraryModule != nil && withinSameApexAs(module, ctx.Module()) {
1988 if headerJars {
1989 return implLibraryModule.HeaderJars()
1990 } else {
1991 return implLibraryModule.ImplementationJars()
1992 }
1993 }
1994
Paul Duffin23970f42020-05-20 14:20:02 +01001995 return module.selectHeaderJarsForSdkVersion(ctx, sdkVersion)
Paul Duffin56d44902020-01-31 13:36:25 +00001996}
1997
Colin Cross79c7c262019-04-17 11:11:46 -07001998// to satisfy SdkLibraryDependency interface
Paul Duffineedc5d52020-06-12 17:46:39 +01001999func (module *SdkLibraryImport) SdkHeaderJars(ctx android.BaseModuleContext, sdkVersion sdkSpec) android.Paths {
Colin Cross79c7c262019-04-17 11:11:46 -07002000 // This module is just a wrapper for the prebuilt stubs.
Paul Duffineedc5d52020-06-12 17:46:39 +01002001 return module.sdkJars(ctx, sdkVersion, true)
Colin Cross79c7c262019-04-17 11:11:46 -07002002}
2003
2004// to satisfy SdkLibraryDependency interface
Paul Duffineedc5d52020-06-12 17:46:39 +01002005func (module *SdkLibraryImport) SdkImplementationJars(ctx android.BaseModuleContext, sdkVersion sdkSpec) android.Paths {
Colin Cross79c7c262019-04-17 11:11:46 -07002006 // This module is just a wrapper for the stubs.
Paul Duffineedc5d52020-06-12 17:46:39 +01002007 return module.sdkJars(ctx, sdkVersion, false)
2008}
2009
2010// to satisfy apex.javaDependency interface
2011func (module *SdkLibraryImport) DexJarBuildPath() android.Path {
2012 if module.implLibraryModule == nil {
2013 return nil
2014 } else {
2015 return module.implLibraryModule.DexJarBuildPath()
2016 }
2017}
2018
2019// to satisfy apex.javaDependency interface
2020func (module *SdkLibraryImport) JacocoReportClassesFile() android.Path {
2021 if module.implLibraryModule == nil {
2022 return nil
2023 } else {
2024 return module.implLibraryModule.JacocoReportClassesFile()
2025 }
2026}
2027
2028// to satisfy apex.javaDependency interface
2029func (module *SdkLibraryImport) Stem() string {
2030 return module.BaseModuleName()
Colin Cross79c7c262019-04-17 11:11:46 -07002031}
Jiyong Parke3833882020-02-17 17:28:10 +09002032
Paul Duffin44b481b2020-06-17 16:59:43 +01002033var _ ApexDependency = (*SdkLibraryImport)(nil)
2034
2035// to satisfy java.ApexDependency interface
2036func (module *SdkLibraryImport) HeaderJars() android.Paths {
2037 if module.implLibraryModule == nil {
2038 return nil
2039 } else {
2040 return module.implLibraryModule.HeaderJars()
2041 }
2042}
2043
2044// to satisfy java.ApexDependency interface
2045func (module *SdkLibraryImport) ImplementationAndResourcesJars() android.Paths {
2046 if module.implLibraryModule == nil {
2047 return nil
2048 } else {
2049 return module.implLibraryModule.ImplementationAndResourcesJars()
2050 }
2051}
2052
Jiyong Parke3833882020-02-17 17:28:10 +09002053//
2054// java_sdk_library_xml
2055//
2056type sdkLibraryXml struct {
2057 android.ModuleBase
2058 android.DefaultableModuleBase
2059 android.ApexModuleBase
2060
2061 properties sdkLibraryXmlProperties
2062
2063 outputFilePath android.OutputPath
2064 installDirPath android.InstallPath
2065}
2066
2067type sdkLibraryXmlProperties struct {
2068 // canonical name of the lib
2069 Lib_name *string
2070}
2071
2072// java_sdk_library_xml builds the permission xml file for a java_sdk_library.
2073// Not to be used directly by users. java_sdk_library internally uses this.
2074func sdkLibraryXmlFactory() android.Module {
2075 module := &sdkLibraryXml{}
2076
2077 module.AddProperties(&module.properties)
2078
2079 android.InitApexModule(module)
2080 android.InitAndroidArchModule(module, android.DeviceSupported, android.MultilibCommon)
2081
2082 return module
2083}
2084
2085// from android.PrebuiltEtcModule
2086func (module *sdkLibraryXml) SubDir() string {
2087 return "permissions"
2088}
2089
2090// from android.PrebuiltEtcModule
2091func (module *sdkLibraryXml) OutputFile() android.OutputPath {
2092 return module.outputFilePath
2093}
2094
2095// from android.ApexModule
2096func (module *sdkLibraryXml) AvailableFor(what string) bool {
2097 return true
2098}
2099
2100func (module *sdkLibraryXml) DepsMutator(ctx android.BottomUpMutatorContext) {
2101 // do nothing
2102}
2103
Jooyung Han749dc692020-04-15 11:03:39 +09002104func (module *sdkLibraryXml) ShouldSupportSdkVersion(ctx android.BaseModuleContext, sdkVersion int) error {
2105 // sdkLibraryXml doesn't need to be checked separately because java_sdk_library is checked
2106 return nil
2107}
2108
Jiyong Parke3833882020-02-17 17:28:10 +09002109// File path to the runtime implementation library
2110func (module *sdkLibraryXml) implPath() string {
2111 implName := proptools.String(module.properties.Lib_name)
2112 if apexName := module.ApexName(); apexName != "" {
2113 // TODO(b/146468504): ApexName() is only a soong module name, not apex name.
2114 // In most cases, this works fine. But when apex_name is set or override_apex is used
2115 // this can be wrong.
2116 return fmt.Sprintf("/apex/%s/javalib/%s.jar", apexName, implName)
2117 }
2118 partition := "system"
2119 if module.SocSpecific() {
2120 partition = "vendor"
2121 } else if module.DeviceSpecific() {
2122 partition = "odm"
2123 } else if module.ProductSpecific() {
2124 partition = "product"
2125 } else if module.SystemExtSpecific() {
2126 partition = "system_ext"
2127 }
2128 return "/" + partition + "/framework/" + implName + ".jar"
2129}
2130
2131func (module *sdkLibraryXml) GenerateAndroidBuildActions(ctx android.ModuleContext) {
2132 libName := proptools.String(module.properties.Lib_name)
2133 xmlContent := fmt.Sprintf(permissionsTemplate, libName, module.implPath())
2134
2135 module.outputFilePath = android.PathForModuleOut(ctx, libName+".xml").OutputPath
2136 rule := android.NewRuleBuilder()
2137 rule.Command().
2138 Text("/bin/bash -c \"echo -e '" + xmlContent + "'\" > ").
2139 Output(module.outputFilePath)
2140
2141 rule.Build(pctx, ctx, "java_sdk_xml", "Permission XML")
2142
2143 module.installDirPath = android.PathForModuleInstall(ctx, "etc", module.SubDir())
2144}
2145
2146func (module *sdkLibraryXml) AndroidMkEntries() []android.AndroidMkEntries {
2147 if !module.IsForPlatform() {
2148 return []android.AndroidMkEntries{android.AndroidMkEntries{
2149 Disabled: true,
2150 }}
2151 }
2152
2153 return []android.AndroidMkEntries{android.AndroidMkEntries{
2154 Class: "ETC",
2155 OutputFile: android.OptionalPathForPath(module.outputFilePath),
2156 ExtraEntries: []android.AndroidMkExtraEntriesFunc{
2157 func(entries *android.AndroidMkEntries) {
2158 entries.SetString("LOCAL_MODULE_TAGS", "optional")
2159 entries.SetString("LOCAL_MODULE_PATH", module.installDirPath.ToMakePath().String())
2160 entries.SetString("LOCAL_INSTALLED_MODULE_STEM", module.outputFilePath.Base())
2161 },
2162 },
2163 }}
2164}
Paul Duffindd46f712020-02-10 13:37:10 +00002165
2166type sdkLibrarySdkMemberType struct {
2167 android.SdkMemberTypeBase
2168}
2169
2170func (s *sdkLibrarySdkMemberType) AddDependencies(mctx android.BottomUpMutatorContext, dependencyTag blueprint.DependencyTag, names []string) {
2171 mctx.AddVariationDependencies(nil, dependencyTag, names...)
2172}
2173
2174func (s *sdkLibrarySdkMemberType) IsInstance(module android.Module) bool {
2175 _, ok := module.(*SdkLibrary)
2176 return ok
2177}
2178
2179func (s *sdkLibrarySdkMemberType) AddPrebuiltModule(ctx android.SdkMemberContext, member android.SdkMember) android.BpModule {
2180 return ctx.SnapshotBuilder().AddPrebuiltModule(member, "java_sdk_library_import")
2181}
2182
2183func (s *sdkLibrarySdkMemberType) CreateVariantPropertiesStruct() android.SdkMemberProperties {
2184 return &sdkLibrarySdkMemberProperties{}
2185}
2186
2187type sdkLibrarySdkMemberProperties struct {
2188 android.SdkMemberPropertiesBase
2189
2190 // Scope to per scope properties.
2191 Scopes map[*apiScope]scopeProperties
2192
2193 // Additional libraries that the exported stubs libraries depend upon.
2194 Libs []string
Paul Duffin3d1248c2020-04-09 00:10:17 +01002195
2196 // The Java stubs source files.
2197 Stub_srcs []string
Paul Duffinf7a64332020-05-13 16:54:55 +01002198
2199 // The naming scheme.
2200 Naming_scheme *string
Paul Duffind7eb1c22020-05-26 20:57:10 +01002201
2202 // True if the java_sdk_library_import is for a shared library, false
2203 // otherwise.
2204 Shared_library *bool
Paul Duffindd46f712020-02-10 13:37:10 +00002205}
2206
2207type scopeProperties struct {
Paul Duffin1fd005d2020-04-09 01:08:11 +01002208 Jars android.Paths
2209 StubsSrcJar android.Path
2210 CurrentApiFile android.Path
2211 RemovedApiFile android.Path
2212 SdkVersion string
Paul Duffindd46f712020-02-10 13:37:10 +00002213}
2214
2215func (s *sdkLibrarySdkMemberProperties) PopulateFromVariant(ctx android.SdkMemberContext, variant android.Module) {
2216 sdk := variant.(*SdkLibrary)
2217
2218 s.Scopes = make(map[*apiScope]scopeProperties)
2219 for _, apiScope := range allApiScopes {
Paul Duffin803a9562020-05-20 11:52:25 +01002220 paths := sdk.findScopePaths(apiScope)
2221 if paths == nil {
2222 continue
2223 }
2224
Paul Duffindd46f712020-02-10 13:37:10 +00002225 jars := paths.stubsImplPath
2226 if len(jars) > 0 {
2227 properties := scopeProperties{}
2228 properties.Jars = jars
Paul Duffin780c5f42020-05-12 15:52:55 +01002229 properties.SdkVersion = sdk.sdkVersionForStubsLibrary(ctx.SdkModuleContext(), apiScope)
Paul Duffin0f8faff2020-05-20 16:18:00 +01002230 properties.StubsSrcJar = paths.stubsSrcJar.Path()
Paul Duffin10269f12020-06-19 18:39:55 +01002231 if paths.currentApiFilePath.Valid() {
2232 properties.CurrentApiFile = paths.currentApiFilePath.Path()
2233 }
2234 if paths.removedApiFilePath.Valid() {
2235 properties.RemovedApiFile = paths.removedApiFilePath.Path()
2236 }
Paul Duffindd46f712020-02-10 13:37:10 +00002237 s.Scopes[apiScope] = properties
2238 }
2239 }
2240
2241 s.Libs = sdk.properties.Libs
Paul Duffindfa131e2020-05-15 20:37:11 +01002242 s.Naming_scheme = sdk.commonSdkLibraryProperties.Naming_scheme
Paul Duffind7eb1c22020-05-26 20:57:10 +01002243 s.Shared_library = proptools.BoolPtr(sdk.sharedLibrary())
Paul Duffindd46f712020-02-10 13:37:10 +00002244}
2245
2246func (s *sdkLibrarySdkMemberProperties) AddToPropertySet(ctx android.SdkMemberContext, propertySet android.BpPropertySet) {
Paul Duffinf7a64332020-05-13 16:54:55 +01002247 if s.Naming_scheme != nil {
2248 propertySet.AddProperty("naming_scheme", proptools.String(s.Naming_scheme))
2249 }
Paul Duffind7eb1c22020-05-26 20:57:10 +01002250 if s.Shared_library != nil {
2251 propertySet.AddProperty("shared_library", *s.Shared_library)
2252 }
Paul Duffinf7a64332020-05-13 16:54:55 +01002253
Paul Duffindd46f712020-02-10 13:37:10 +00002254 for _, apiScope := range allApiScopes {
2255 if properties, ok := s.Scopes[apiScope]; ok {
Paul Duffin6b836ba2020-05-13 19:19:49 +01002256 scopeSet := propertySet.AddPropertySet(apiScope.propertyName)
Paul Duffindd46f712020-02-10 13:37:10 +00002257
Paul Duffin3d1248c2020-04-09 00:10:17 +01002258 scopeDir := filepath.Join("sdk_library", s.OsPrefix(), apiScope.name)
2259
Paul Duffindd46f712020-02-10 13:37:10 +00002260 var jars []string
2261 for _, p := range properties.Jars {
Paul Duffin3d1248c2020-04-09 00:10:17 +01002262 dest := filepath.Join(scopeDir, ctx.Name()+"-stubs.jar")
Paul Duffindd46f712020-02-10 13:37:10 +00002263 ctx.SnapshotBuilder().CopyToSnapshot(p, dest)
2264 jars = append(jars, dest)
2265 }
2266 scopeSet.AddProperty("jars", jars)
2267
Paul Duffin3d1248c2020-04-09 00:10:17 +01002268 // Merge the stubs source jar into the snapshot zip so that when it is unpacked
2269 // the source files are also unpacked.
2270 snapshotRelativeDir := filepath.Join(scopeDir, ctx.Name()+"_stub_sources")
2271 ctx.SnapshotBuilder().UnzipToSnapshot(properties.StubsSrcJar, snapshotRelativeDir)
2272 scopeSet.AddProperty("stub_srcs", []string{snapshotRelativeDir})
2273
Paul Duffin1fd005d2020-04-09 01:08:11 +01002274 if properties.CurrentApiFile != nil {
2275 currentApiSnapshotPath := filepath.Join(scopeDir, ctx.Name()+".txt")
2276 ctx.SnapshotBuilder().CopyToSnapshot(properties.CurrentApiFile, currentApiSnapshotPath)
2277 scopeSet.AddProperty("current_api", currentApiSnapshotPath)
2278 }
2279
2280 if properties.RemovedApiFile != nil {
2281 removedApiSnapshotPath := filepath.Join(scopeDir, ctx.Name()+"-removed.txt")
Paul Duffin3dbf9fd2020-06-02 13:00:02 +01002282 ctx.SnapshotBuilder().CopyToSnapshot(properties.RemovedApiFile, removedApiSnapshotPath)
Paul Duffin1fd005d2020-04-09 01:08:11 +01002283 scopeSet.AddProperty("removed_api", removedApiSnapshotPath)
2284 }
2285
Paul Duffindd46f712020-02-10 13:37:10 +00002286 if properties.SdkVersion != "" {
2287 scopeSet.AddProperty("sdk_version", properties.SdkVersion)
2288 }
2289 }
2290 }
2291
2292 if len(s.Libs) > 0 {
2293 propertySet.AddPropertyWithTag("libs", s.Libs, ctx.SnapshotBuilder().SdkMemberReferencePropertyTag(false))
2294 }
2295}