blob: aa717f95bc23ac82a23fdb0b80b1560e0d098795 [file] [log] [blame]
Jiyong Parkc678ad32018-04-10 13:07:10 +09001// Copyright 2018 Google Inc. All rights reserved.
2//
3// Licensed under the Apache License, Version 2.0 (the "License");
4// you may not use this file except in compliance with the License.
5// You may obtain a copy of the License at
6//
7// http://www.apache.org/licenses/LICENSE-2.0
8//
9// Unless required by applicable law or agreed to in writing, software
10// distributed under the License is distributed on an "AS IS" BASIS,
11// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12// See the License for the specific language governing permissions and
13// limitations under the License.
14
15package java
16
17import (
Jiyong Parkc678ad32018-04-10 13:07:10 +090018 "fmt"
19 "path"
Sundong Ahn054b19a2018-10-19 13:46:09 +090020 "path/filepath"
Paul Duffin6a2bd112020-04-07 19:27:04 +010021 "reflect"
Paul Duffin46fdda82020-05-14 15:39:10 +010022 "regexp"
Jiyong Park82484c02018-04-23 21:41:26 +090023 "sort"
Jiyong Parkc678ad32018-04-10 13:07:10 +090024 "strings"
Jiyong Park82484c02018-04-23 21:41:26 +090025 "sync"
Jiyong Parkc678ad32018-04-10 13:07:10 +090026
Paul Duffind1b3a922020-01-22 11:57:20 +000027 "github.com/google/blueprint"
Jiyong Parkc678ad32018-04-10 13:07:10 +090028 "github.com/google/blueprint/proptools"
Paul Duffin6a2bd112020-04-07 19:27:04 +010029
30 "android/soong/android"
Jiyong Parkc678ad32018-04-10 13:07:10 +090031)
32
Jooyung Han58f26ab2019-12-18 15:34:32 +090033const (
Paul Duffin1c094a02020-05-08 15:52:37 +010034 sdkXmlFileSuffix = ".xml"
35 permissionsTemplate = `<?xml version=\"1.0\" encoding=\"utf-8\"?>\n` +
Jooyung Han624058e2019-12-24 18:38:06 +090036 `<!-- Copyright (C) 2018 The Android Open Source Project\n` +
37 `\n` +
Jiyong Parke3833882020-02-17 17:28:10 +090038 ` Licensed under the Apache License, Version 2.0 (the \"License\");\n` +
Jooyung Han624058e2019-12-24 18:38:06 +090039 ` you may not use this file except in compliance with the License.\n` +
40 ` You may obtain a copy of the License at\n` +
41 `\n` +
42 ` http://www.apache.org/licenses/LICENSE-2.0\n` +
43 `\n` +
44 ` Unless required by applicable law or agreed to in writing, software\n` +
Jiyong Parke3833882020-02-17 17:28:10 +090045 ` distributed under the License is distributed on an \"AS IS\" BASIS,\n` +
Jooyung Han624058e2019-12-24 18:38:06 +090046 ` WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\n` +
47 ` See the License for the specific language governing permissions and\n` +
48 ` limitations under the License.\n` +
49 `-->\n` +
50 `<permissions>\n` +
Jiyong Parke3833882020-02-17 17:28:10 +090051 ` <library name=\"%s\" file=\"%s\"/>\n` +
Jooyung Han624058e2019-12-24 18:38:06 +090052 `</permissions>\n`
Jiyong Parkc678ad32018-04-10 13:07:10 +090053)
54
Paul Duffind1b3a922020-01-22 11:57:20 +000055// A tag to associated a dependency with a specific api scope.
56type scopeDependencyTag struct {
57 blueprint.BaseDependencyTag
58 name string
59 apiScope *apiScope
Paul Duffin5fb82132020-04-29 20:45:27 +010060
61 // Function for extracting appropriate path information from the dependency.
62 depInfoExtractor func(paths *scopePaths, dep android.Module) error
63}
64
65// Extract tag specific information from the dependency.
66func (tag scopeDependencyTag) extractDepInfo(ctx android.ModuleContext, dep android.Module, paths *scopePaths) {
67 err := tag.depInfoExtractor(paths, dep)
68 if err != nil {
69 ctx.ModuleErrorf("has an invalid {scopeDependencyTag: %s} dependency on module %s: %s", tag.name, ctx.OtherModuleName(dep), err.Error())
70 }
Paul Duffind1b3a922020-01-22 11:57:20 +000071}
72
Paul Duffin2c5454f2020-06-26 22:08:43 +010073var _ android.ReplaceSourceWithPrebuilt = (*scopeDependencyTag)(nil)
74
75func (tag scopeDependencyTag) ReplaceSourceWithPrebuilt() bool {
76 return false
77}
78
Paul Duffind1b3a922020-01-22 11:57:20 +000079// Provides information about an api scope, e.g. public, system, test.
80type apiScope struct {
81 // The name of the api scope, e.g. public, system, test
82 name string
83
Paul Duffin51a2bee2020-05-05 14:40:52 +010084 // The api scope that this scope extends.
85 extends *apiScope
86
Paul Duffin3a254982020-04-28 10:44:03 +010087 // The legacy enabled status for a specific scope can be dependent on other
88 // properties that have been specified on the library so it is provided by
89 // a function that can determine the status by examining those properties.
90 legacyEnabledStatus func(module *SdkLibrary) bool
91
92 // The default enabled status for non-legacy behavior, which is triggered by
93 // explicitly enabling at least one api scope.
94 defaultEnabledStatus bool
95
96 // Gets a pointer to the scope specific properties.
97 scopeSpecificProperties func(module *SdkLibrary) *ApiScopeProperties
98
Paul Duffin6a2bd112020-04-07 19:27:04 +010099 // The name of the field in the dynamically created structure.
100 fieldName string
101
Paul Duffin0f270632020-05-13 19:19:49 +0100102 // The name of the property in the java_sdk_library_import
103 propertyName string
104
Paul Duffind1b3a922020-01-22 11:57:20 +0000105 // The tag to use to depend on the stubs library module.
106 stubsTag scopeDependencyTag
107
Paul Duffina377e4c2020-04-29 13:30:54 +0100108 // The tag to use to depend on the stubs source module (if separate from the API module).
109 stubsSourceTag scopeDependencyTag
110
111 // The tag to use to depend on the API file generating module (if separate from the stubs source module).
112 apiFileTag scopeDependencyTag
113
Paul Duffin5fb82132020-04-29 20:45:27 +0100114 // The tag to use to depend on the stubs source and API module.
115 stubsSourceAndApiTag scopeDependencyTag
Paul Duffind1b3a922020-01-22 11:57:20 +0000116
117 // The scope specific prefix to add to the api file base of "current.txt" or "removed.txt".
118 apiFilePrefix string
119
120 // The scope specific prefix to add to the sdk library module name to construct a scope specific
121 // module name.
122 moduleSuffix string
123
Paul Duffind1b3a922020-01-22 11:57:20 +0000124 // SDK version that the stubs library is built against. Note that this is always
125 // *current. Older stubs library built with a numbered SDK version is created from
126 // the prebuilt jar.
127 sdkVersion string
Paul Duffin3c7c3472020-04-07 18:50:10 +0100128
129 // Extra arguments to pass to droidstubs for this scope.
130 droidstubsArgs []string
Anton Hansson5ff28e52020-05-02 11:19:36 +0100131
Paul Duffina377e4c2020-04-29 13:30:54 +0100132 // The args that must be passed to droidstubs to generate the stubs source
133 // for this scope.
134 //
135 // The stubs source must include the definitions of everything that is in this
136 // api scope and all the scopes that this one extends.
137 droidstubsArgsForGeneratingStubsSource []string
138
139 // The args that must be passed to droidstubs to generate the API for this scope.
140 //
141 // The API only includes the additional members that this scope adds over the scope
142 // that it extends.
143 droidstubsArgsForGeneratingApi []string
144
145 // True if the stubs source and api can be created by the same metalava invocation.
146 createStubsSourceAndApiTogether bool
147
Anton Hansson5ff28e52020-05-02 11:19:36 +0100148 // Whether the api scope can be treated as unstable, and should skip compat checks.
149 unstable bool
Paul Duffind1b3a922020-01-22 11:57:20 +0000150}
151
152// Initialize a scope, creating and adding appropriate dependency tags
153func initApiScope(scope *apiScope) *apiScope {
Paul Duffin5fb82132020-04-29 20:45:27 +0100154 name := scope.name
Paul Duffin46fdda82020-05-14 15:39:10 +0100155 scopeByName[name] = scope
156 allScopeNames = append(allScopeNames, name)
Paul Duffin0f270632020-05-13 19:19:49 +0100157 scope.propertyName = strings.ReplaceAll(name, "-", "_")
158 scope.fieldName = proptools.FieldNameForProperty(scope.propertyName)
Paul Duffind1b3a922020-01-22 11:57:20 +0000159 scope.stubsTag = scopeDependencyTag{
Paul Duffin5fb82132020-04-29 20:45:27 +0100160 name: name + "-stubs",
161 apiScope: scope,
162 depInfoExtractor: (*scopePaths).extractStubsLibraryInfoFromDependency,
Paul Duffind1b3a922020-01-22 11:57:20 +0000163 }
Paul Duffina377e4c2020-04-29 13:30:54 +0100164 scope.stubsSourceTag = scopeDependencyTag{
165 name: name + "-stubs-source",
166 apiScope: scope,
167 depInfoExtractor: (*scopePaths).extractStubsSourceInfoFromDep,
168 }
169 scope.apiFileTag = scopeDependencyTag{
170 name: name + "-api",
171 apiScope: scope,
172 depInfoExtractor: (*scopePaths).extractApiInfoFromDep,
173 }
Paul Duffin5fb82132020-04-29 20:45:27 +0100174 scope.stubsSourceAndApiTag = scopeDependencyTag{
175 name: name + "-stubs-source-and-api",
176 apiScope: scope,
177 depInfoExtractor: (*scopePaths).extractStubsSourceAndApiInfoFromApiStubsProvider,
Paul Duffind1b3a922020-01-22 11:57:20 +0000178 }
Paul Duffina377e4c2020-04-29 13:30:54 +0100179
180 // To get the args needed to generate the stubs source append all the args from
181 // this scope and all the scopes it extends as each set of args adds additional
182 // members to the stubs.
183 var stubsSourceArgs []string
184 for s := scope; s != nil; s = s.extends {
185 stubsSourceArgs = append(stubsSourceArgs, s.droidstubsArgs...)
186 }
187 scope.droidstubsArgsForGeneratingStubsSource = stubsSourceArgs
188
189 // Currently the args needed to generate the API are the same as the args
190 // needed to add additional members.
191 apiArgs := scope.droidstubsArgs
192 scope.droidstubsArgsForGeneratingApi = apiArgs
193
194 // If the args needed to generate the stubs and API are the same then they
195 // can be generated in a single invocation of metalava, otherwise they will
196 // need separate invocations.
197 scope.createStubsSourceAndApiTogether = reflect.DeepEqual(stubsSourceArgs, apiArgs)
198
Paul Duffind1b3a922020-01-22 11:57:20 +0000199 return scope
200}
201
Paul Duffinb74ee3e2020-05-08 14:16:20 +0100202func (scope *apiScope) stubsLibraryModuleName(baseName string) string {
Paul Duffin1c094a02020-05-08 15:52:37 +0100203 return baseName + ".stubs" + scope.moduleSuffix
Paul Duffind1b3a922020-01-22 11:57:20 +0000204}
205
Paul Duffin5fb82132020-04-29 20:45:27 +0100206func (scope *apiScope) stubsSourceModuleName(baseName string) string {
Paul Duffin1c094a02020-05-08 15:52:37 +0100207 return baseName + ".stubs.source" + scope.moduleSuffix
Paul Duffind1b3a922020-01-22 11:57:20 +0000208}
209
Paul Duffina377e4c2020-04-29 13:30:54 +0100210func (scope *apiScope) apiModuleName(baseName string) string {
Paul Duffin1c094a02020-05-08 15:52:37 +0100211 return baseName + ".api" + scope.moduleSuffix
Paul Duffina377e4c2020-04-29 13:30:54 +0100212}
213
Paul Duffin3a254982020-04-28 10:44:03 +0100214func (scope *apiScope) String() string {
215 return scope.name
216}
217
Paul Duffind1b3a922020-01-22 11:57:20 +0000218type apiScopes []*apiScope
219
220func (scopes apiScopes) Strings(accessor func(*apiScope) string) []string {
221 var list []string
222 for _, scope := range scopes {
223 list = append(list, accessor(scope))
224 }
225 return list
226}
227
Jiyong Parkc678ad32018-04-10 13:07:10 +0900228var (
Paul Duffin46fdda82020-05-14 15:39:10 +0100229 scopeByName = make(map[string]*apiScope)
230 allScopeNames []string
Paul Duffind1b3a922020-01-22 11:57:20 +0000231 apiScopePublic = initApiScope(&apiScope{
Paul Duffin3a254982020-04-28 10:44:03 +0100232 name: "public",
233
234 // Public scope is enabled by default for both legacy and non-legacy modes.
235 legacyEnabledStatus: func(module *SdkLibrary) bool {
236 return true
237 },
238 defaultEnabledStatus: true,
239
240 scopeSpecificProperties: func(module *SdkLibrary) *ApiScopeProperties {
241 return &module.sdkLibraryProperties.Public
242 },
Paul Duffind1b3a922020-01-22 11:57:20 +0000243 sdkVersion: "current",
244 })
245 apiScopeSystem = initApiScope(&apiScope{
Paul Duffin3a254982020-04-28 10:44:03 +0100246 name: "system",
247 extends: apiScopePublic,
248 legacyEnabledStatus: (*SdkLibrary).generateTestAndSystemScopesByDefault,
249 scopeSpecificProperties: func(module *SdkLibrary) *ApiScopeProperties {
250 return &module.sdkLibraryProperties.System
251 },
Anton Hanssone366fff2020-04-28 16:47:41 +0100252 apiFilePrefix: "system-",
Paul Duffin1c094a02020-05-08 15:52:37 +0100253 moduleSuffix: ".system",
Anton Hanssone366fff2020-04-28 16:47:41 +0100254 sdkVersion: "system_current",
Paul Duffin991f2622020-04-29 22:18:41 +0100255 droidstubsArgs: []string{"-showAnnotation android.annotation.SystemApi\\(client=android.annotation.SystemApi.Client.PRIVILEGED_APPS\\)"},
Paul Duffind1b3a922020-01-22 11:57:20 +0000256 })
257 apiScopeTest = initApiScope(&apiScope{
Paul Duffin3a254982020-04-28 10:44:03 +0100258 name: "test",
259 extends: apiScopePublic,
260 legacyEnabledStatus: (*SdkLibrary).generateTestAndSystemScopesByDefault,
261 scopeSpecificProperties: func(module *SdkLibrary) *ApiScopeProperties {
262 return &module.sdkLibraryProperties.Test
263 },
Anton Hanssone366fff2020-04-28 16:47:41 +0100264 apiFilePrefix: "test-",
Paul Duffin1c094a02020-05-08 15:52:37 +0100265 moduleSuffix: ".test",
Anton Hanssone366fff2020-04-28 16:47:41 +0100266 sdkVersion: "test_current",
267 droidstubsArgs: []string{"-showAnnotation android.annotation.TestApi"},
Anton Hansson5ff28e52020-05-02 11:19:36 +0100268 unstable: true,
Paul Duffind1b3a922020-01-22 11:57:20 +0000269 })
Paul Duffin6d7f0a72020-04-28 14:13:56 +0100270 apiScopeModuleLib = initApiScope(&apiScope{
Paul Duffin0f270632020-05-13 19:19:49 +0100271 name: "module-lib",
Paul Duffin6d7f0a72020-04-28 14:13:56 +0100272 extends: apiScopeSystem,
Paul Duffin5a757b12020-06-02 13:00:08 +0100273 // The module-lib scope is disabled by default in legacy mode.
Paul Duffin6d7f0a72020-04-28 14:13:56 +0100274 //
275 // Enabling this would break existing usages.
276 legacyEnabledStatus: func(module *SdkLibrary) bool {
277 return false
278 },
279 scopeSpecificProperties: func(module *SdkLibrary) *ApiScopeProperties {
280 return &module.sdkLibraryProperties.Module_lib
281 },
282 apiFilePrefix: "module-lib-",
283 moduleSuffix: ".module_lib",
284 sdkVersion: "module_current",
285 droidstubsArgs: []string{
286 "--show-annotation android.annotation.SystemApi\\(client=android.annotation.SystemApi.Client.MODULE_LIBRARIES\\)",
287 },
288 })
Paul Duffin5a757b12020-06-02 13:00:08 +0100289 apiScopeSystemServer = initApiScope(&apiScope{
290 name: "system-server",
291 extends: apiScopePublic,
292 // The system-server scope is disabled by default in legacy mode.
293 //
294 // Enabling this would break existing usages.
295 legacyEnabledStatus: func(module *SdkLibrary) bool {
296 return false
297 },
298 scopeSpecificProperties: func(module *SdkLibrary) *ApiScopeProperties {
299 return &module.sdkLibraryProperties.System_server
300 },
301 apiFilePrefix: "system-server-",
302 moduleSuffix: ".system_server",
303 sdkVersion: "system_server_current",
304 droidstubsArgs: []string{
305 "--show-annotation android.annotation.SystemApi\\(client=android.annotation.SystemApi.Client.SYSTEM_SERVER\\) ",
306 "--hide-annotation android.annotation.Hide",
307 // com.android.* classes are okay in this interface"
308 "--hide InternalClasses",
309 },
310 })
Paul Duffind1b3a922020-01-22 11:57:20 +0000311 allApiScopes = apiScopes{
312 apiScopePublic,
313 apiScopeSystem,
314 apiScopeTest,
Paul Duffin6d7f0a72020-04-28 14:13:56 +0100315 apiScopeModuleLib,
Paul Duffin5a757b12020-06-02 13:00:08 +0100316 apiScopeSystemServer,
Paul Duffind1b3a922020-01-22 11:57:20 +0000317 }
Jiyong Parkc678ad32018-04-10 13:07:10 +0900318)
319
Jiyong Park82484c02018-04-23 21:41:26 +0900320var (
321 javaSdkLibrariesLock sync.Mutex
322)
323
Jiyong Parkc678ad32018-04-10 13:07:10 +0900324// TODO: these are big features that are currently missing
Jiyong Park1be96912018-05-28 18:02:19 +0900325// 1) disallowing linking to the runtime shared lib
326// 2) HTML generation
Jiyong Parkc678ad32018-04-10 13:07:10 +0900327
328func init() {
Paul Duffin43dc1cc2019-12-19 11:18:54 +0000329 RegisterSdkLibraryBuildComponents(android.InitRegistrationContext)
Jiyong Parkc678ad32018-04-10 13:07:10 +0900330
Jiyong Park82484c02018-04-23 21:41:26 +0900331 android.RegisterMakeVarsProvider(pctx, func(ctx android.MakeVarsContext) {
332 javaSdkLibraries := javaSdkLibraries(ctx.Config())
333 sort.Strings(*javaSdkLibraries)
334 ctx.Strict("JAVA_SDK_LIBRARIES", strings.Join(*javaSdkLibraries, " "))
335 })
Paul Duffin61871622020-02-10 13:37:10 +0000336
337 // Register sdk member types.
338 android.RegisterSdkMemberType(&sdkLibrarySdkMemberType{
339 android.SdkMemberTypeBase{
340 PropertyName: "java_sdk_libs",
341 SupportsSdk: true,
342 },
343 })
Jiyong Parkc678ad32018-04-10 13:07:10 +0900344}
345
Paul Duffin43dc1cc2019-12-19 11:18:54 +0000346func RegisterSdkLibraryBuildComponents(ctx android.RegistrationContext) {
347 ctx.RegisterModuleType("java_sdk_library", SdkLibraryFactory)
348 ctx.RegisterModuleType("java_sdk_library_import", sdkLibraryImportFactory)
349}
350
Paul Duffin3a254982020-04-28 10:44:03 +0100351// Properties associated with each api scope.
352type ApiScopeProperties struct {
353 // Indicates whether the api surface is generated.
354 //
355 // If this is set for any scope then all scopes must explicitly specify if they
356 // are enabled. This is to prevent new usages from depending on legacy behavior.
357 //
358 // Otherwise, if this is not set for any scope then the default behavior is
359 // scope specific so please refer to the scope specific property documentation.
360 Enabled *bool
Paul Duffin080f5ee2020-05-12 11:50:28 +0100361
362 // The sdk_version to use for building the stubs.
363 //
364 // If not specified then it will use an sdk_version determined as follows:
365 // 1) If the sdk_version specified on the java_sdk_library is none then this
366 // will be none. This is used for java_sdk_library instances that are used
367 // to create stubs that contribute to the core_current sdk version.
368 // 2) Otherwise, it is assumed that this library extends but does not contribute
369 // directly to a specific sdk_version and so this uses the sdk_version appropriate
370 // for the api scope. e.g. public will use sdk_version: current, system will use
371 // sdk_version: system_current, etc.
372 //
373 // This does not affect the sdk_version used for either generating the stubs source
374 // or the API file. They both have to use the same sdk_version as is used for
375 // compiling the implementation library.
376 Sdk_version *string
Paul Duffin3a254982020-04-28 10:44:03 +0100377}
378
Jiyong Parkc678ad32018-04-10 13:07:10 +0900379type sdkLibraryProperties struct {
Paul Duffin9d582cc2020-05-16 15:52:12 +0100380 // Visibility for impl library module. If not specified then defaults to the
381 // visibility property.
382 Impl_library_visibility []string
383
Paul Duffin344c4ee2020-04-29 23:35:13 +0100384 // Visibility for stubs library modules. If not specified then defaults to the
385 // visibility property.
386 Stubs_library_visibility []string
387
388 // Visibility for stubs source modules. If not specified then defaults to the
389 // visibility property.
390 Stubs_source_visibility []string
391
Sundong Ahnf043cf62018-06-25 16:04:37 +0900392 // List of Java libraries that will be in the classpath when building stubs
393 Stub_only_libs []string `android:"arch_variant"`
394
Paul Duffin7a586d32019-12-30 17:09:34 +0000395 // list of package names that will be documented and publicized as API.
396 // This allows the API to be restricted to a subset of the source files provided.
397 // If this is unspecified then all the source files will be treated as being part
398 // of the API.
Jiyong Parkc678ad32018-04-10 13:07:10 +0900399 Api_packages []string
400
Jiyong Park5a2c9d72018-05-01 22:25:41 +0900401 // list of package names that must be hidden from the API
402 Hidden_api_packages []string
403
Paul Duffin749f98f2019-12-30 17:23:46 +0000404 // the relative path to the directory containing the api specification files.
405 // Defaults to "api".
406 Api_dir *string
407
Paul Duffind11e78e2020-05-15 20:37:11 +0100408 // Determines whether a runtime implementation library is built; defaults to false.
409 //
410 // If true then it also prevents the module from being used as a shared module, i.e.
411 // it is as is shared_library: false, was set.
Paul Duffin43db9be2019-12-30 17:35:49 +0000412 Api_only *bool
413
Paul Duffin11512472019-02-11 15:55:17 +0000414 // local files that are used within user customized droiddoc options.
415 Droiddoc_option_files []string
416
417 // additional droiddoc options
418 // Available variables for substitution:
419 //
420 // $(location <label>): the path to the droiddoc_option_files with name <label>
Sundong Ahndd567f92018-07-31 17:19:11 +0900421 Droiddoc_options []string
422
Paul Duffin2ce1e812020-05-20 19:35:27 +0100423 // is set to true, Metalava will allow framework SDK to contain annotations.
424 Annotations_enabled *bool
425
Sundong Ahn054b19a2018-10-19 13:46:09 +0900426 // a list of top-level directories containing files to merge qualifier annotations
427 // (i.e. those intended to be included in the stubs written) from.
428 Merge_annotations_dirs []string
429
430 // a list of top-level directories containing Java stub files to merge show/hide annotations from.
431 Merge_inclusion_annotations_dirs []string
432
433 // If set to true, the path of dist files is apistubs/core. Defaults to false.
434 Core_lib *bool
435
Sundong Ahn80a87b32019-05-13 15:02:50 +0900436 // don't create dist rules.
437 No_dist *bool `blueprint:"mutated"`
438
Paul Duffin3a254982020-04-28 10:44:03 +0100439 // indicates whether system and test apis should be generated.
440 Generate_system_and_test_apis bool `blueprint:"mutated"`
441
442 // The properties specific to the public api scope
443 //
444 // Unless explicitly specified by using public.enabled the public api scope is
445 // enabled by default in both legacy and non-legacy mode.
446 Public ApiScopeProperties
447
448 // The properties specific to the system api scope
449 //
450 // In legacy mode the system api scope is enabled by default when sdk_version
451 // is set to something other than "none".
452 //
453 // In non-legacy mode the system api scope is disabled by default.
454 System ApiScopeProperties
455
456 // The properties specific to the test api scope
457 //
458 // In legacy mode the test api scope is enabled by default when sdk_version
459 // is set to something other than "none".
460 //
461 // In non-legacy mode the test api scope is disabled by default.
462 Test ApiScopeProperties
Paul Duffin37e0b772019-12-30 17:20:10 +0000463
Paul Duffin5a757b12020-06-02 13:00:08 +0100464 // The properties specific to the module-lib api scope
Paul Duffin6d7f0a72020-04-28 14:13:56 +0100465 //
Paul Duffin5a757b12020-06-02 13:00:08 +0100466 // Unless explicitly specified by using test.enabled the module-lib api scope is
Paul Duffin6d7f0a72020-04-28 14:13:56 +0100467 // disabled by default.
468 Module_lib ApiScopeProperties
469
Paul Duffin5a757b12020-06-02 13:00:08 +0100470 // The properties specific to the system-server api scope
471 //
472 // Unless explicitly specified by using test.enabled the module-lib api scope is
473 // disabled by default.
474 System_server ApiScopeProperties
475
Jiyong Park27fc4142020-05-28 00:19:53 +0900476 // Determines if the stubs are preferred over the implementation library
477 // for linking, even when the client doesn't specify sdk_version. When this
478 // is set to true, such clients are provided with the widest API surface that
479 // this lib provides. Note however that this option doesn't affect the clients
480 // that are in the same APEX as this library. In that case, the clients are
481 // always linked with the implementation library. Default is false.
482 Default_to_stubs *bool
483
Paul Duffin8986cc92020-05-10 19:32:20 +0100484 // Properties related to api linting.
485 Api_lint struct {
486 // Enable api linting.
487 Enabled *bool
488 }
489
Jiyong Parkc678ad32018-04-10 13:07:10 +0900490 // TODO: determines whether to create HTML doc or not
491 //Html_doc *bool
492}
493
Paul Duffin533f9c72020-05-20 16:18:00 +0100494// Paths to outputs from java_sdk_library and java_sdk_library_import.
495//
496// Fields that are android.Paths are always set (during GenerateAndroidBuildActions).
497// OptionalPaths are always set by java_sdk_library but may not be set by
498// java_sdk_library_import as not all instances provide that information.
Paul Duffind1b3a922020-01-22 11:57:20 +0000499type scopePaths struct {
Paul Duffin533f9c72020-05-20 16:18:00 +0100500 // The path (represented as Paths for convenience when returning) to the stubs header jar.
501 //
502 // That is the jar that is created by turbine.
503 stubsHeaderPath android.Paths
504
505 // The path (represented as Paths for convenience when returning) to the stubs implementation jar.
506 //
507 // This is not the implementation jar, it still only contains stubs.
508 stubsImplPath android.Paths
509
510 // The API specification file, e.g. system_current.txt.
511 currentApiFilePath android.OptionalPath
512
513 // The specification of API elements removed since the last release.
514 removedApiFilePath android.OptionalPath
515
516 // The stubs source jar.
517 stubsSrcJar android.OptionalPath
Paul Duffind1b3a922020-01-22 11:57:20 +0000518}
519
Paul Duffin5fb82132020-04-29 20:45:27 +0100520func (paths *scopePaths) extractStubsLibraryInfoFromDependency(dep android.Module) error {
521 if lib, ok := dep.(Dependency); ok {
522 paths.stubsHeaderPath = lib.HeaderJars()
523 paths.stubsImplPath = lib.ImplementationJars()
524 return nil
525 } else {
526 return fmt.Errorf("expected module that implements Dependency, e.g. java_library")
527 }
528}
529
Paul Duffina377e4c2020-04-29 13:30:54 +0100530func (paths *scopePaths) treatDepAsApiStubsProvider(dep android.Module, action func(provider ApiStubsProvider)) error {
531 if apiStubsProvider, ok := dep.(ApiStubsProvider); ok {
532 action(apiStubsProvider)
Paul Duffin5fb82132020-04-29 20:45:27 +0100533 return nil
534 } else {
535 return fmt.Errorf("expected module that implements ApiStubsProvider, e.g. droidstubs")
536 }
537}
538
Paul Duffin533f9c72020-05-20 16:18:00 +0100539func (paths *scopePaths) treatDepAsApiStubsSrcProvider(dep android.Module, action func(provider ApiStubsSrcProvider)) error {
540 if apiStubsProvider, ok := dep.(ApiStubsSrcProvider); ok {
541 action(apiStubsProvider)
542 return nil
543 } else {
544 return fmt.Errorf("expected module that implements ApiStubsSrcProvider, e.g. droidstubs")
545 }
546}
547
Paul Duffina377e4c2020-04-29 13:30:54 +0100548func (paths *scopePaths) extractApiInfoFromApiStubsProvider(provider ApiStubsProvider) {
Paul Duffin533f9c72020-05-20 16:18:00 +0100549 paths.currentApiFilePath = android.OptionalPathForPath(provider.ApiFilePath())
550 paths.removedApiFilePath = android.OptionalPathForPath(provider.RemovedApiFilePath())
Paul Duffina377e4c2020-04-29 13:30:54 +0100551}
552
553func (paths *scopePaths) extractApiInfoFromDep(dep android.Module) error {
554 return paths.treatDepAsApiStubsProvider(dep, func(provider ApiStubsProvider) {
555 paths.extractApiInfoFromApiStubsProvider(provider)
556 })
557}
558
Paul Duffin533f9c72020-05-20 16:18:00 +0100559func (paths *scopePaths) extractStubsSourceInfoFromApiStubsProviders(provider ApiStubsSrcProvider) {
560 paths.stubsSrcJar = android.OptionalPathForPath(provider.StubsSrcJar())
Paul Duffina377e4c2020-04-29 13:30:54 +0100561}
562
563func (paths *scopePaths) extractStubsSourceInfoFromDep(dep android.Module) error {
Paul Duffin533f9c72020-05-20 16:18:00 +0100564 return paths.treatDepAsApiStubsSrcProvider(dep, func(provider ApiStubsSrcProvider) {
Paul Duffina377e4c2020-04-29 13:30:54 +0100565 paths.extractStubsSourceInfoFromApiStubsProviders(provider)
566 })
567}
568
569func (paths *scopePaths) extractStubsSourceAndApiInfoFromApiStubsProvider(dep android.Module) error {
570 return paths.treatDepAsApiStubsProvider(dep, func(provider ApiStubsProvider) {
571 paths.extractApiInfoFromApiStubsProvider(provider)
572 paths.extractStubsSourceInfoFromApiStubsProviders(provider)
573 })
574}
575
576type commonToSdkLibraryAndImportProperties struct {
Paul Duffin1a724e62020-05-08 13:44:43 +0100577 // The naming scheme to use for the components that this module creates.
578 //
Paul Duffindef8a892020-05-08 15:36:30 +0100579 // If not specified then it defaults to "default". The other allowable value is
580 // "framework-modules" which matches the scheme currently used by framework modules
581 // for the equivalent components represented as separate Soong modules.
Paul Duffin1a724e62020-05-08 13:44:43 +0100582 //
583 // This is a temporary mechanism to simplify conversion from separate modules for each
584 // component that follow a different naming pattern to the default one.
585 //
586 // TODO(b/155480189) - Remove once naming inconsistencies have been resolved.
Paul Duffina377e4c2020-04-29 13:30:54 +0100587 Naming_scheme *string
Paul Duffind11e78e2020-05-15 20:37:11 +0100588
589 // Specifies whether this module can be used as an Android shared library; defaults
590 // to true.
591 //
592 // An Android shared library is one that can be referenced in a <uses-library> element
593 // in an AndroidManifest.xml.
594 Shared_library *bool
Paul Duffina377e4c2020-04-29 13:30:54 +0100595}
596
Paul Duffin56d44902020-01-31 13:36:25 +0000597// Common code between sdk library and sdk library import
598type commonToSdkLibraryAndImport struct {
Paul Duffinb74ee3e2020-05-08 14:16:20 +0100599 moduleBase *android.ModuleBase
600
Paul Duffin56d44902020-01-31 13:36:25 +0000601 scopePaths map[*apiScope]*scopePaths
Paul Duffin1a724e62020-05-08 13:44:43 +0100602
603 namingScheme sdkLibraryComponentNamingScheme
604
Paul Duffind11e78e2020-05-15 20:37:11 +0100605 commonSdkLibraryProperties commonToSdkLibraryAndImportProperties
Paul Duffin64e61992020-05-15 10:20:31 +0100606
607 // Functionality related to this being used as a component of a java_sdk_library.
608 EmbeddableSdkLibraryComponent
Paul Duffin56d44902020-01-31 13:36:25 +0000609}
610
Paul Duffinb74ee3e2020-05-08 14:16:20 +0100611func (c *commonToSdkLibraryAndImport) initCommon(moduleBase *android.ModuleBase) {
612 c.moduleBase = moduleBase
Paul Duffin1a724e62020-05-08 13:44:43 +0100613
Paul Duffind11e78e2020-05-15 20:37:11 +0100614 moduleBase.AddProperties(&c.commonSdkLibraryProperties)
Paul Duffin64e61992020-05-15 10:20:31 +0100615
616 // Initialize this as an sdk library component.
617 c.initSdkLibraryComponent(moduleBase)
Paul Duffin1a724e62020-05-08 13:44:43 +0100618}
619
620func (c *commonToSdkLibraryAndImport) initCommonAfterDefaultsApplied(ctx android.DefaultableHookContext) bool {
Paul Duffind11e78e2020-05-15 20:37:11 +0100621 schemeProperty := proptools.StringDefault(c.commonSdkLibraryProperties.Naming_scheme, "default")
Paul Duffin1a724e62020-05-08 13:44:43 +0100622 switch schemeProperty {
623 case "default":
624 c.namingScheme = &defaultNamingScheme{}
Paul Duffindef8a892020-05-08 15:36:30 +0100625 case "framework-modules":
626 c.namingScheme = &frameworkModulesNamingScheme{}
Paul Duffin1a724e62020-05-08 13:44:43 +0100627 default:
628 ctx.PropertyErrorf("naming_scheme", "expected 'default' but was %q", schemeProperty)
629 return false
630 }
631
Paul Duffind11e78e2020-05-15 20:37:11 +0100632 // Only track this sdk library if this can be used as a shared library.
633 if c.sharedLibrary() {
634 // Use the name specified in the module definition as the owner.
635 c.sdkLibraryComponentProperties.SdkLibraryToImplicitlyTrack = proptools.StringPtr(c.moduleBase.BaseModuleName())
636 }
Paul Duffin64e61992020-05-15 10:20:31 +0100637
Paul Duffin1a724e62020-05-08 13:44:43 +0100638 return true
Paul Duffinb74ee3e2020-05-08 14:16:20 +0100639}
640
Paul Duffinf642a312020-06-12 17:46:39 +0100641// Module name of the runtime implementation library
642func (c *commonToSdkLibraryAndImport) implLibraryModuleName() string {
643 return c.moduleBase.BaseModuleName() + ".impl"
644}
645
646// Module name of the XML file for the lib
647func (c *commonToSdkLibraryAndImport) xmlPermissionsModuleName() string {
648 return c.moduleBase.BaseModuleName() + sdkXmlFileSuffix
649}
650
Paul Duffinb74ee3e2020-05-08 14:16:20 +0100651// Name of the java_library module that compiles the stubs source.
652func (c *commonToSdkLibraryAndImport) stubsLibraryModuleName(apiScope *apiScope) string {
Paul Duffin1a724e62020-05-08 13:44:43 +0100653 return c.namingScheme.stubsLibraryModuleName(apiScope, c.moduleBase.BaseModuleName())
Paul Duffinb74ee3e2020-05-08 14:16:20 +0100654}
655
656// Name of the droidstubs module that generates the stubs source and may also
657// generate/check the API.
658func (c *commonToSdkLibraryAndImport) stubsSourceModuleName(apiScope *apiScope) string {
Paul Duffin1a724e62020-05-08 13:44:43 +0100659 return c.namingScheme.stubsSourceModuleName(apiScope, c.moduleBase.BaseModuleName())
Paul Duffinb74ee3e2020-05-08 14:16:20 +0100660}
661
662// Name of the droidstubs module that generates/checks the API. Only used if it
663// requires different arts to the stubs source generating module.
664func (c *commonToSdkLibraryAndImport) apiModuleName(apiScope *apiScope) string {
Paul Duffin1a724e62020-05-08 13:44:43 +0100665 return c.namingScheme.apiModuleName(apiScope, c.moduleBase.BaseModuleName())
Paul Duffinb74ee3e2020-05-08 14:16:20 +0100666}
667
Paul Duffin46fdda82020-05-14 15:39:10 +0100668// The component names for different outputs of the java_sdk_library.
669//
670// They are similar to the names used for the child modules it creates
671const (
672 stubsSourceComponentName = "stubs.source"
673
674 apiTxtComponentName = "api.txt"
675
676 removedApiTxtComponentName = "removed-api.txt"
677)
678
679// A regular expression to match tags that reference a specific stubs component.
680//
681// It will only match if given a valid scope and a valid component. It is verfy strict
682// to ensure it does not accidentally match a similar looking tag that should be processed
683// by the embedded Library.
684var tagSplitter = func() *regexp.Regexp {
685 // Given a list of literal string items returns a regular expression that will
686 // match any one of the items.
687 choice := func(items ...string) string {
688 return `\Q` + strings.Join(items, `\E|\Q`) + `\E`
689 }
690
691 // Regular expression to match one of the scopes.
692 scopesRegexp := choice(allScopeNames...)
693
694 // Regular expression to match one of the components.
695 componentsRegexp := choice(stubsSourceComponentName, apiTxtComponentName, removedApiTxtComponentName)
696
697 // Regular expression to match any combination of one scope and one component.
698 return regexp.MustCompile(fmt.Sprintf(`^\.(%s)\.(%s)$`, scopesRegexp, componentsRegexp))
699}()
700
701// For OutputFileProducer interface
702//
703// .<scope>.stubs.source
704// .<scope>.api.txt
705// .<scope>.removed-api.txt
706func (c *commonToSdkLibraryAndImport) commonOutputFiles(tag string) (android.Paths, error) {
707 if groups := tagSplitter.FindStringSubmatch(tag); groups != nil {
708 scopeName := groups[1]
709 component := groups[2]
710
711 if scope, ok := scopeByName[scopeName]; ok {
712 paths := c.findScopePaths(scope)
713 if paths == nil {
714 return nil, fmt.Errorf("%q does not provide api scope %s", c.moduleBase.BaseModuleName(), scopeName)
715 }
716
717 switch component {
718 case stubsSourceComponentName:
719 if paths.stubsSrcJar.Valid() {
720 return android.Paths{paths.stubsSrcJar.Path()}, nil
721 }
722
723 case apiTxtComponentName:
724 if paths.currentApiFilePath.Valid() {
725 return android.Paths{paths.currentApiFilePath.Path()}, nil
726 }
727
728 case removedApiTxtComponentName:
729 if paths.removedApiFilePath.Valid() {
730 return android.Paths{paths.removedApiFilePath.Path()}, nil
731 }
732 }
733
734 return nil, fmt.Errorf("%s not available for api scope %s", component, scopeName)
735 } else {
736 return nil, fmt.Errorf("unknown scope %s in %s", scope, tag)
737 }
738
739 } else {
740 return nil, nil
741 }
742}
743
Paul Duffin5ae30792020-05-20 11:52:25 +0100744func (c *commonToSdkLibraryAndImport) getScopePathsCreateIfNeeded(scope *apiScope) *scopePaths {
Paul Duffin56d44902020-01-31 13:36:25 +0000745 if c.scopePaths == nil {
746 c.scopePaths = make(map[*apiScope]*scopePaths)
747 }
748 paths := c.scopePaths[scope]
749 if paths == nil {
750 paths = &scopePaths{}
751 c.scopePaths[scope] = paths
752 }
753
754 return paths
755}
756
Paul Duffin5ae30792020-05-20 11:52:25 +0100757func (c *commonToSdkLibraryAndImport) findScopePaths(scope *apiScope) *scopePaths {
758 if c.scopePaths == nil {
759 return nil
760 }
761
762 return c.scopePaths[scope]
763}
764
765// If this does not support the requested api scope then find the closest available
766// scope it does support. Returns nil if no such scope is available.
767func (c *commonToSdkLibraryAndImport) findClosestScopePath(scope *apiScope) *scopePaths {
768 for s := scope; s != nil; s = s.extends {
769 if paths := c.findScopePaths(s); paths != nil {
770 return paths
771 }
772 }
773
774 // This should never happen outside tests as public should be the base scope for every
775 // scope and is enabled by default.
776 return nil
777}
778
Paul Duffina3fb67d2020-05-20 14:20:02 +0100779func (c *commonToSdkLibraryAndImport) selectHeaderJarsForSdkVersion(ctx android.BaseModuleContext, sdkVersion sdkSpec) android.Paths {
Paul Duffin47624362020-05-20 12:19:10 +0100780
781 // If a specific numeric version has been requested then use prebuilt versions of the sdk.
782 if sdkVersion.version.isNumbered() {
783 return PrebuiltJars(ctx, c.moduleBase.BaseModuleName(), sdkVersion)
784 }
785
786 var apiScope *apiScope
787 switch sdkVersion.kind {
788 case sdkSystem:
789 apiScope = apiScopeSystem
Paul Duffin5ae30792020-05-20 11:52:25 +0100790 case sdkModule:
791 apiScope = apiScopeModuleLib
Paul Duffin47624362020-05-20 12:19:10 +0100792 case sdkTest:
793 apiScope = apiScopeTest
Paul Duffin5a757b12020-06-02 13:00:08 +0100794 case sdkSystemServer:
795 apiScope = apiScopeSystemServer
Paul Duffin47624362020-05-20 12:19:10 +0100796 default:
797 apiScope = apiScopePublic
798 }
799
Paul Duffin5ae30792020-05-20 11:52:25 +0100800 paths := c.findClosestScopePath(apiScope)
801 if paths == nil {
802 var scopes []string
803 for _, s := range allApiScopes {
804 if c.findScopePaths(s) != nil {
805 scopes = append(scopes, s.name)
806 }
807 }
808 ctx.ModuleErrorf("requires api scope %s from %s but it only has %q available", apiScope.name, c.moduleBase.BaseModuleName(), scopes)
809 return nil
810 }
811
Paul Duffina3fb67d2020-05-20 14:20:02 +0100812 return paths.stubsHeaderPath
Paul Duffin47624362020-05-20 12:19:10 +0100813}
814
Paul Duffin64e61992020-05-15 10:20:31 +0100815func (c *commonToSdkLibraryAndImport) sdkComponentPropertiesForChildLibrary() interface{} {
816 componentProps := &struct {
817 SdkLibraryToImplicitlyTrack *string
Paul Duffind11e78e2020-05-15 20:37:11 +0100818 }{}
819
820 if c.sharedLibrary() {
Paul Duffin64e61992020-05-15 10:20:31 +0100821 // Mark the stubs library as being components of this java_sdk_library so that
822 // any app that includes code which depends (directly or indirectly) on the stubs
823 // library will have the appropriate <uses-library> invocation inserted into its
824 // manifest if necessary.
Paul Duffind11e78e2020-05-15 20:37:11 +0100825 componentProps.SdkLibraryToImplicitlyTrack = proptools.StringPtr(c.moduleBase.BaseModuleName())
Paul Duffin64e61992020-05-15 10:20:31 +0100826 }
827
828 return componentProps
829}
830
Paul Duffind11e78e2020-05-15 20:37:11 +0100831// Check if this can be used as a shared library.
832func (c *commonToSdkLibraryAndImport) sharedLibrary() bool {
833 return proptools.BoolDefault(c.commonSdkLibraryProperties.Shared_library, true)
834}
835
Paul Duffin64e61992020-05-15 10:20:31 +0100836// Properties related to the use of a module as an component of a java_sdk_library.
837type SdkLibraryComponentProperties struct {
838
839 // The name of the java_sdk_library/_import to add to a <uses-library> entry
840 // in the AndroidManifest.xml of any Android app that includes code that references
841 // this module. If not set then no java_sdk_library/_import is tracked.
842 SdkLibraryToImplicitlyTrack *string `blueprint:"mutated"`
843}
844
845// Structure to be embedded in a module struct that needs to support the
846// SdkLibraryComponentDependency interface.
847type EmbeddableSdkLibraryComponent struct {
848 sdkLibraryComponentProperties SdkLibraryComponentProperties
849}
850
851func (e *EmbeddableSdkLibraryComponent) initSdkLibraryComponent(moduleBase *android.ModuleBase) {
852 moduleBase.AddProperties(&e.sdkLibraryComponentProperties)
853}
854
855// to satisfy SdkLibraryComponentDependency
856func (e *EmbeddableSdkLibraryComponent) OptionalImplicitSdkLibrary() []string {
857 if e.sdkLibraryComponentProperties.SdkLibraryToImplicitlyTrack != nil {
858 return []string{*e.sdkLibraryComponentProperties.SdkLibraryToImplicitlyTrack}
859 }
860 return nil
861}
862
863// Implemented by modules that are (or possibly could be) a component of a java_sdk_library
864// (including the java_sdk_library) itself.
865type SdkLibraryComponentDependency interface {
866 // The optional name of the sdk library that should be implicitly added to the
867 // AndroidManifest of an app that contains code which references the sdk library.
868 //
869 // Returns an array containing 0 or 1 items rather than a *string to make it easier
870 // to append this to the list of exported sdk libraries.
871 OptionalImplicitSdkLibrary() []string
872}
873
874// Make sure that all the module types that are components of java_sdk_library/_import
875// and which can be referenced (directly or indirectly) from an android app implement
876// the SdkLibraryComponentDependency interface.
877var _ SdkLibraryComponentDependency = (*Library)(nil)
878var _ SdkLibraryComponentDependency = (*Import)(nil)
879var _ SdkLibraryComponentDependency = (*SdkLibrary)(nil)
Paul Duffinf642a312020-06-12 17:46:39 +0100880var _ SdkLibraryComponentDependency = (*SdkLibraryImport)(nil)
Paul Duffin64e61992020-05-15 10:20:31 +0100881
882// Provides access to sdk_version related header and implentation jars.
883type SdkLibraryDependency interface {
884 SdkLibraryComponentDependency
885
886 // Get the header jars appropriate for the supplied sdk_version.
887 //
888 // These are turbine generated jars so they only change if the externals of the
889 // class changes but it does not contain and implementation or JavaDoc.
890 SdkHeaderJars(ctx android.BaseModuleContext, sdkVersion sdkSpec) android.Paths
891
892 // Get the implementation jars appropriate for the supplied sdk version.
893 //
894 // These are either the implementation jar for the whole sdk library or the implementation
895 // jars for the stubs. The latter should only be needed when generating JavaDoc as otherwise
896 // they are identical to the corresponding header jars.
897 SdkImplementationJars(ctx android.BaseModuleContext, sdkVersion sdkSpec) android.Paths
898}
899
Inseob Kimc0907f12019-02-08 21:00:45 +0900900type SdkLibrary struct {
Sundong Ahn054b19a2018-10-19 13:46:09 +0900901 Library
Jiyong Parkc678ad32018-04-10 13:07:10 +0900902
Sundong Ahn054b19a2018-10-19 13:46:09 +0900903 sdkLibraryProperties sdkLibraryProperties
Jiyong Parkc678ad32018-04-10 13:07:10 +0900904
Paul Duffin3a254982020-04-28 10:44:03 +0100905 // Map from api scope to the scope specific property structure.
906 scopeToProperties map[*apiScope]*ApiScopeProperties
907
Paul Duffin56d44902020-01-31 13:36:25 +0000908 commonToSdkLibraryAndImport
Jiyong Parkc678ad32018-04-10 13:07:10 +0900909}
910
Inseob Kimc0907f12019-02-08 21:00:45 +0900911var _ Dependency = (*SdkLibrary)(nil)
912var _ SdkLibraryDependency = (*SdkLibrary)(nil)
Colin Cross897d2ed2019-02-11 14:03:51 -0800913
Paul Duffin3a254982020-04-28 10:44:03 +0100914func (module *SdkLibrary) generateTestAndSystemScopesByDefault() bool {
915 return module.sdkLibraryProperties.Generate_system_and_test_apis
916}
917
918func (module *SdkLibrary) getGeneratedApiScopes(ctx android.EarlyModuleContext) apiScopes {
919 // Check to see if any scopes have been explicitly enabled. If any have then all
920 // must be.
921 anyScopesExplicitlyEnabled := false
922 for _, scope := range allApiScopes {
923 scopeProperties := module.scopeToProperties[scope]
924 if scopeProperties.Enabled != nil {
925 anyScopesExplicitlyEnabled = true
926 break
927 }
Paul Duffind1b3a922020-01-22 11:57:20 +0000928 }
Paul Duffin3a254982020-04-28 10:44:03 +0100929
930 var generatedScopes apiScopes
931 enabledScopes := make(map[*apiScope]struct{})
932 for _, scope := range allApiScopes {
933 scopeProperties := module.scopeToProperties[scope]
934 // If any scopes are explicitly enabled then ignore the legacy enabled status.
935 // This is to ensure that any new usages of this module type do not rely on legacy
936 // behaviour.
937 defaultEnabledStatus := false
938 if anyScopesExplicitlyEnabled {
939 defaultEnabledStatus = scope.defaultEnabledStatus
940 } else {
941 defaultEnabledStatus = scope.legacyEnabledStatus(module)
942 }
943 enabled := proptools.BoolDefault(scopeProperties.Enabled, defaultEnabledStatus)
944 if enabled {
945 enabledScopes[scope] = struct{}{}
946 generatedScopes = append(generatedScopes, scope)
947 }
948 }
949
950 // Now check to make sure that any scope that is extended by an enabled scope is also
951 // enabled.
952 for _, scope := range allApiScopes {
953 if _, ok := enabledScopes[scope]; ok {
954 extends := scope.extends
955 if extends != nil {
956 if _, ok := enabledScopes[extends]; !ok {
957 ctx.ModuleErrorf("enabled api scope %q depends on disabled scope %q", scope, extends)
958 }
959 }
960 }
961 }
962
963 return generatedScopes
Paul Duffind1b3a922020-01-22 11:57:20 +0000964}
965
Paul Duffinf642a312020-06-12 17:46:39 +0100966type sdkLibraryComponentTag struct {
967 blueprint.BaseDependencyTag
968 name string
969}
970
971// Mark this tag so dependencies that use it are excluded from visibility enforcement.
972func (t sdkLibraryComponentTag) ExcludeFromVisibilityEnforcement() {}
973
974var xmlPermissionsFileTag = sdkLibraryComponentTag{name: "xml-permissions-file"}
Paul Duffine74ac732020-02-06 13:51:46 +0000975
Jiyong Parke3833882020-02-17 17:28:10 +0900976func IsXmlPermissionsFileDepTag(depTag blueprint.DependencyTag) bool {
Paul Duffinf642a312020-06-12 17:46:39 +0100977 if dt, ok := depTag.(sdkLibraryComponentTag); ok {
Jiyong Parke3833882020-02-17 17:28:10 +0900978 return dt == xmlPermissionsFileTag
979 }
980 return false
981}
982
Paul Duffinf642a312020-06-12 17:46:39 +0100983var implLibraryTag = sdkLibraryComponentTag{name: "impl-library"}
Paul Duffin9d582cc2020-05-16 15:52:12 +0100984
Paul Duffin27cd4a52020-06-26 20:17:02 +0100985// Add the dependencies on the child modules in the component deps mutator.
986func (module *SdkLibrary) ComponentDepsMutator(ctx android.BottomUpMutatorContext) {
Paul Duffin3a254982020-04-28 10:44:03 +0100987 for _, apiScope := range module.getGeneratedApiScopes(ctx) {
Paul Duffind1b3a922020-01-22 11:57:20 +0000988 // Add dependencies to the stubs library
Paul Duffinb74ee3e2020-05-08 14:16:20 +0100989 ctx.AddVariationDependencies(nil, apiScope.stubsTag, module.stubsLibraryModuleName(apiScope))
Paul Duffind1b3a922020-01-22 11:57:20 +0000990
Paul Duffina377e4c2020-04-29 13:30:54 +0100991 // If the stubs source and API cannot be generated together then add an additional dependency on
992 // the API module.
993 if apiScope.createStubsSourceAndApiTogether {
994 // Add a dependency on the stubs source in order to access both stubs source and api information.
Paul Duffinb74ee3e2020-05-08 14:16:20 +0100995 ctx.AddVariationDependencies(nil, apiScope.stubsSourceAndApiTag, module.stubsSourceModuleName(apiScope))
Paul Duffina377e4c2020-04-29 13:30:54 +0100996 } else {
997 // Add separate dependencies on the creators of the stubs source files and the API.
Paul Duffinb74ee3e2020-05-08 14:16:20 +0100998 ctx.AddVariationDependencies(nil, apiScope.stubsSourceTag, module.stubsSourceModuleName(apiScope))
999 ctx.AddVariationDependencies(nil, apiScope.apiFileTag, module.apiModuleName(apiScope))
Paul Duffina377e4c2020-04-29 13:30:54 +01001000 }
Sundong Ahn054b19a2018-10-19 13:46:09 +09001001 }
1002
Paul Duffind11e78e2020-05-15 20:37:11 +01001003 if module.requiresRuntimeImplementationLibrary() {
Paul Duffin9d582cc2020-05-16 15:52:12 +01001004 // Add dependency to the rule for generating the implementation library.
1005 ctx.AddDependency(module, implLibraryTag, module.implLibraryModuleName())
1006
Paul Duffind11e78e2020-05-15 20:37:11 +01001007 if module.sharedLibrary() {
1008 // Add dependency to the rule for generating the xml permissions file
Paul Duffinf642a312020-06-12 17:46:39 +01001009 ctx.AddDependency(module, xmlPermissionsFileTag, module.xmlPermissionsModuleName())
Paul Duffind11e78e2020-05-15 20:37:11 +01001010 }
Paul Duffin27cd4a52020-06-26 20:17:02 +01001011 }
1012}
Paul Duffine74ac732020-02-06 13:51:46 +00001013
Paul Duffin27cd4a52020-06-26 20:17:02 +01001014// Add other dependencies as normal.
1015func (module *SdkLibrary) DepsMutator(ctx android.BottomUpMutatorContext) {
1016 if module.requiresRuntimeImplementationLibrary() {
Paul Duffind11e78e2020-05-15 20:37:11 +01001017 // Only add the deps for the library if it is actually going to be built.
1018 module.Library.deps(ctx)
1019 }
Jiyong Parkc678ad32018-04-10 13:07:10 +09001020}
1021
Paul Duffin46fdda82020-05-14 15:39:10 +01001022func (module *SdkLibrary) OutputFiles(tag string) (android.Paths, error) {
1023 paths, err := module.commonOutputFiles(tag)
1024 if paths == nil && err == nil {
1025 return module.Library.OutputFiles(tag)
1026 } else {
1027 return paths, err
1028 }
1029}
1030
Inseob Kimc0907f12019-02-08 21:00:45 +09001031func (module *SdkLibrary) GenerateAndroidBuildActions(ctx android.ModuleContext) {
Paul Duffind11e78e2020-05-15 20:37:11 +01001032 // Only build an implementation library if required.
1033 if module.requiresRuntimeImplementationLibrary() {
Paul Duffin43db9be2019-12-30 17:35:49 +00001034 module.Library.GenerateAndroidBuildActions(ctx)
1035 }
Sundong Ahn054b19a2018-10-19 13:46:09 +09001036
Sundong Ahn57368eb2018-07-06 11:20:23 +09001037 // Record the paths to the header jars of the library (stubs and impl).
Paul Duffind1b3a922020-01-22 11:57:20 +00001038 // When this java_sdk_library is depended upon from others via "libs" property,
Jiyong Parkc678ad32018-04-10 13:07:10 +09001039 // the recorded paths will be returned depending on the link type of the caller.
1040 ctx.VisitDirectDeps(func(to android.Module) {
Jiyong Parkc678ad32018-04-10 13:07:10 +09001041 tag := ctx.OtherModuleDependencyTag(to)
1042
Paul Duffin5fb82132020-04-29 20:45:27 +01001043 // Extract information from any of the scope specific dependencies.
1044 if scopeTag, ok := tag.(scopeDependencyTag); ok {
1045 apiScope := scopeTag.apiScope
Paul Duffin5ae30792020-05-20 11:52:25 +01001046 scopePaths := module.getScopePathsCreateIfNeeded(apiScope)
Paul Duffin5fb82132020-04-29 20:45:27 +01001047
1048 // Extract information from the dependency. The exact information extracted
1049 // is determined by the nature of the dependency which is determined by the tag.
1050 scopeTag.extractDepInfo(ctx, to, scopePaths)
Sundong Ahn20e998b2018-07-24 11:19:26 +09001051 }
Jiyong Parkc678ad32018-04-10 13:07:10 +09001052 })
1053}
1054
Jiyong Park0b0e1b92019-12-03 13:24:29 +09001055func (module *SdkLibrary) AndroidMkEntries() []android.AndroidMkEntries {
Paul Duffind11e78e2020-05-15 20:37:11 +01001056 if !module.requiresRuntimeImplementationLibrary() {
Paul Duffin43db9be2019-12-30 17:35:49 +00001057 return nil
1058 }
Jiyong Park0b0e1b92019-12-03 13:24:29 +09001059 entriesList := module.Library.AndroidMkEntries()
1060 entries := &entriesList[0]
Paul Duffinf642a312020-06-12 17:46:39 +01001061 entries.Required = append(entries.Required, module.xmlPermissionsModuleName())
Jiyong Park0b0e1b92019-12-03 13:24:29 +09001062 return entriesList
Jiyong Park82484c02018-04-23 21:41:26 +09001063}
1064
Anton Hansson6bb88102020-03-27 19:43:19 +00001065// The dist path of the stub artifacts
1066func (module *SdkLibrary) apiDistPath(apiScope *apiScope) string {
1067 if module.ModuleBase.Owner() != "" {
1068 return path.Join("apistubs", module.ModuleBase.Owner(), apiScope.name)
1069 } else if Bool(module.sdkLibraryProperties.Core_lib) {
1070 return path.Join("apistubs", "core", apiScope.name)
1071 } else {
1072 return path.Join("apistubs", "android", apiScope.name)
1073 }
1074}
1075
Paul Duffin12ceb462019-12-24 20:31:31 +00001076// Get the sdk version for use when compiling the stubs library.
Paul Duffin153501f2020-05-12 15:52:55 +01001077func (module *SdkLibrary) sdkVersionForStubsLibrary(mctx android.EarlyModuleContext, apiScope *apiScope) string {
Paul Duffin080f5ee2020-05-12 11:50:28 +01001078 scopeProperties := module.scopeToProperties[apiScope]
1079 if scopeProperties.Sdk_version != nil {
1080 return proptools.String(scopeProperties.Sdk_version)
1081 }
1082
Paul Duffin12ceb462019-12-24 20:31:31 +00001083 sdkDep := decodeSdkDep(mctx, sdkContext(&module.Library))
1084 if sdkDep.hasStandardLibs() {
1085 // If building against a standard sdk then use the sdk version appropriate for the scope.
Paul Duffind1b3a922020-01-22 11:57:20 +00001086 return apiScope.sdkVersion
Paul Duffin12ceb462019-12-24 20:31:31 +00001087 } else {
1088 // Otherwise, use no system module.
1089 return "none"
1090 }
1091}
1092
Paul Duffind1b3a922020-01-22 11:57:20 +00001093func (module *SdkLibrary) latestApiFilegroupName(apiScope *apiScope) string {
1094 return ":" + module.BaseModuleName() + ".api." + apiScope.name + ".latest"
Jiyong Park58c518b2018-05-12 22:29:12 +09001095}
Jiyong Parkc678ad32018-04-10 13:07:10 +09001096
Paul Duffind1b3a922020-01-22 11:57:20 +00001097func (module *SdkLibrary) latestRemovedApiFilegroupName(apiScope *apiScope) string {
1098 return ":" + module.BaseModuleName() + "-removed.api." + apiScope.name + ".latest"
Jiyong Parkc678ad32018-04-10 13:07:10 +09001099}
1100
Paul Duffin9d582cc2020-05-16 15:52:12 +01001101// Creates the implementation java library
1102func (module *SdkLibrary) createImplLibrary(mctx android.DefaultableHookContext) {
Paul Duffinc4422102020-06-24 16:22:38 +01001103
1104 moduleNamePtr := proptools.StringPtr(module.BaseModuleName())
1105
Paul Duffin9d582cc2020-05-16 15:52:12 +01001106 props := struct {
Paul Duffinc4422102020-06-24 16:22:38 +01001107 Name *string
1108 Visibility []string
1109 Instrument bool
1110 ConfigurationName *string
Paul Duffin9d582cc2020-05-16 15:52:12 +01001111 }{
1112 Name: proptools.StringPtr(module.implLibraryModuleName()),
1113 Visibility: module.sdkLibraryProperties.Impl_library_visibility,
Paul Duffin49d3a522020-06-18 21:09:55 +01001114 // Set the instrument property to ensure it is instrumented when instrumentation is required.
1115 Instrument: true,
Paul Duffinc4422102020-06-24 16:22:38 +01001116
1117 // Make the created library behave as if it had the same name as this module.
1118 ConfigurationName: moduleNamePtr,
Paul Duffin9d582cc2020-05-16 15:52:12 +01001119 }
1120
1121 properties := []interface{}{
1122 &module.properties,
1123 &module.protoProperties,
1124 &module.deviceProperties,
1125 &module.dexpreoptProperties,
Colin Cross1e28e3c2020-06-02 20:09:13 -07001126 &module.linter.properties,
Paul Duffin9d582cc2020-05-16 15:52:12 +01001127 &props,
1128 module.sdkComponentPropertiesForChildLibrary(),
1129 }
1130 mctx.CreateModule(LibraryFactory, properties...)
1131}
1132
Jiyong Parkc678ad32018-04-10 13:07:10 +09001133// Creates a static java library that has API stubs
Paul Duffin2aaef532020-04-29 16:47:28 +01001134func (module *SdkLibrary) createStubsLibrary(mctx android.DefaultableHookContext, apiScope *apiScope) {
Jiyong Parkc678ad32018-04-10 13:07:10 +09001135 props := struct {
Paul Duffind41712d2020-05-16 09:57:59 +01001136 Name *string
1137 Visibility []string
1138 Srcs []string
1139 Installable *bool
1140 Sdk_version *string
1141 System_modules *string
1142 Patch_module *string
1143 Libs []string
1144 Compile_dex *bool
1145 Java_version *string
1146 Product_variables struct {
Jiyong Park82484c02018-04-23 21:41:26 +09001147 Pdk struct {
1148 Enabled *bool
1149 }
Jiyong Parkc678ad32018-04-10 13:07:10 +09001150 }
Sundong Ahn054b19a2018-10-19 13:46:09 +09001151 Openjdk9 struct {
1152 Srcs []string
1153 Javacflags []string
1154 }
Anton Hansson6bb88102020-03-27 19:43:19 +00001155 Dist struct {
1156 Targets []string
1157 Dest *string
1158 Dir *string
1159 Tag *string
1160 }
Jiyong Parkc678ad32018-04-10 13:07:10 +09001161 }{}
1162
Paul Duffinb74ee3e2020-05-08 14:16:20 +01001163 props.Name = proptools.StringPtr(module.stubsLibraryModuleName(apiScope))
Paul Duffin344c4ee2020-04-29 23:35:13 +01001164
1165 // If stubs_library_visibility is not set then the created module will use the
1166 // visibility of this module.
1167 visibility := module.sdkLibraryProperties.Stubs_library_visibility
1168 props.Visibility = visibility
1169
Jiyong Parkc678ad32018-04-10 13:07:10 +09001170 // sources are generated from the droiddoc
Paul Duffinb74ee3e2020-05-08 14:16:20 +01001171 props.Srcs = []string{":" + module.stubsSourceModuleName(apiScope)}
Paul Duffin12ceb462019-12-24 20:31:31 +00001172 sdkVersion := module.sdkVersionForStubsLibrary(mctx, apiScope)
Paul Duffin52d398a2019-06-11 12:31:14 +01001173 props.Sdk_version = proptools.StringPtr(sdkVersion)
Paul Duffinc5d954a2020-05-16 18:54:24 +01001174 props.System_modules = module.deviceProperties.System_modules
1175 props.Patch_module = module.properties.Patch_module
Paul Duffin367ab912019-12-23 19:40:36 +00001176 props.Installable = proptools.BoolPtr(false)
Sundong Ahn054b19a2018-10-19 13:46:09 +09001177 props.Libs = module.sdkLibraryProperties.Stub_only_libs
Paul Duffin2ce1e812020-05-20 19:35:27 +01001178 // The stub-annotations library contains special versions of the annotations
1179 // with CLASS retention policy, so that they're kept.
1180 if proptools.Bool(module.sdkLibraryProperties.Annotations_enabled) {
1181 props.Libs = append(props.Libs, "stub-annotations")
1182 }
Jiyong Park82484c02018-04-23 21:41:26 +09001183 props.Product_variables.Pdk.Enabled = proptools.BoolPtr(false)
Paul Duffinc5d954a2020-05-16 18:54:24 +01001184 props.Openjdk9.Srcs = module.properties.Openjdk9.Srcs
1185 props.Openjdk9.Javacflags = module.properties.Openjdk9.Javacflags
Anton Hanssoncf4dd4c2020-05-21 09:21:57 +01001186 // We compile the stubs for 1.8 in line with the main android.jar stubs, and potential
1187 // interop with older developer tools that don't support 1.9.
1188 props.Java_version = proptools.StringPtr("1.8")
Paul Duffinc5d954a2020-05-16 18:54:24 +01001189 if module.deviceProperties.Compile_dex != nil {
1190 props.Compile_dex = module.deviceProperties.Compile_dex
Sundong Ahndd567f92018-07-31 17:19:11 +09001191 }
Jiyong Parkc678ad32018-04-10 13:07:10 +09001192
Anton Hansson6bb88102020-03-27 19:43:19 +00001193 // Dist the class jar artifact for sdk builds.
1194 if !Bool(module.sdkLibraryProperties.No_dist) {
1195 props.Dist.Targets = []string{"sdk", "win_sdk"}
1196 props.Dist.Dest = proptools.StringPtr(fmt.Sprintf("%v.jar", module.BaseModuleName()))
1197 props.Dist.Dir = proptools.StringPtr(module.apiDistPath(apiScope))
1198 props.Dist.Tag = proptools.StringPtr(".jar")
1199 }
Jiyong Parkc678ad32018-04-10 13:07:10 +09001200
Paul Duffin64e61992020-05-15 10:20:31 +01001201 mctx.CreateModule(LibraryFactory, &props, module.sdkComponentPropertiesForChildLibrary())
Jiyong Parkc678ad32018-04-10 13:07:10 +09001202}
1203
Paul Duffincbcfcaa2020-04-07 18:49:53 +01001204// Creates a droidstubs module that creates stubs source files from the given full source
Paul Duffin5fb82132020-04-29 20:45:27 +01001205// files and also updates and checks the API specification files.
Paul Duffina377e4c2020-04-29 13:30:54 +01001206func (module *SdkLibrary) createStubsSourcesAndApi(mctx android.DefaultableHookContext, apiScope *apiScope, name string, createStubSources, createApi bool, scopeSpecificDroidstubsArgs []string) {
Jiyong Parkc678ad32018-04-10 13:07:10 +09001207 props := struct {
Sundong Ahn054b19a2018-10-19 13:46:09 +09001208 Name *string
Paul Duffin344c4ee2020-04-29 23:35:13 +01001209 Visibility []string
Sundong Ahn054b19a2018-10-19 13:46:09 +09001210 Srcs []string
1211 Installable *bool
Paul Duffin52d398a2019-06-11 12:31:14 +01001212 Sdk_version *string
Paul Duffin12ceb462019-12-24 20:31:31 +00001213 System_modules *string
Sundong Ahn054b19a2018-10-19 13:46:09 +09001214 Libs []string
Paul Duffin11512472019-02-11 15:55:17 +00001215 Arg_files []string
Sundong Ahn054b19a2018-10-19 13:46:09 +09001216 Args *string
Sundong Ahn054b19a2018-10-19 13:46:09 +09001217 Java_version *string
Paul Duffin2ce1e812020-05-20 19:35:27 +01001218 Annotations_enabled *bool
Sundong Ahn054b19a2018-10-19 13:46:09 +09001219 Merge_annotations_dirs []string
1220 Merge_inclusion_annotations_dirs []string
Paul Duffina377e4c2020-04-29 13:30:54 +01001221 Generate_stubs *bool
Sundong Ahn054b19a2018-10-19 13:46:09 +09001222 Check_api struct {
Inseob Kim38449af2019-02-28 14:24:05 +09001223 Current ApiToCheck
1224 Last_released ApiToCheck
1225 Ignore_missing_latest_api *bool
Paul Duffin8986cc92020-05-10 19:32:20 +01001226
1227 Api_lint struct {
1228 Enabled *bool
1229 New_since *string
1230 Baseline_file *string
1231 }
Jiyong Park58c518b2018-05-12 22:29:12 +09001232 }
Sundong Ahn1b92c822018-05-29 11:35:17 +09001233 Aidl struct {
1234 Include_dirs []string
1235 Local_include_dirs []string
1236 }
Anton Hansson6bb88102020-03-27 19:43:19 +00001237 Dist struct {
1238 Targets []string
1239 Dest *string
1240 Dir *string
1241 }
Jiyong Parkc678ad32018-04-10 13:07:10 +09001242 }{}
1243
Paul Duffinda364252020-04-28 14:08:32 +01001244 // The stubs source processing uses the same compile time classpath when extracting the
1245 // API from the implementation library as it does when compiling it. i.e. the same
1246 // * sdk version
1247 // * system_modules
1248 // * libs (static_libs/libs)
Paul Duffin250e6192019-06-07 10:44:37 +01001249
Paul Duffina377e4c2020-04-29 13:30:54 +01001250 props.Name = proptools.StringPtr(name)
Paul Duffin344c4ee2020-04-29 23:35:13 +01001251
1252 // If stubs_source_visibility is not set then the created module will use the
1253 // visibility of this module.
1254 visibility := module.sdkLibraryProperties.Stubs_source_visibility
1255 props.Visibility = visibility
1256
Paul Duffinc5d954a2020-05-16 18:54:24 +01001257 props.Srcs = append(props.Srcs, module.properties.Srcs...)
1258 props.Sdk_version = module.deviceProperties.Sdk_version
1259 props.System_modules = module.deviceProperties.System_modules
Jiyong Parkc678ad32018-04-10 13:07:10 +09001260 props.Installable = proptools.BoolPtr(false)
Sundong Ahne6f0b052018-06-05 16:46:14 +09001261 // A droiddoc module has only one Libs property and doesn't distinguish between
1262 // shared libs and static libs. So we need to add both of these libs to Libs property.
Paul Duffinc5d954a2020-05-16 18:54:24 +01001263 props.Libs = module.properties.Libs
1264 props.Libs = append(props.Libs, module.properties.Static_libs...)
1265 props.Aidl.Include_dirs = module.deviceProperties.Aidl.Include_dirs
1266 props.Aidl.Local_include_dirs = module.deviceProperties.Aidl.Local_include_dirs
1267 props.Java_version = module.properties.Java_version
Jiyong Parkc678ad32018-04-10 13:07:10 +09001268
Paul Duffin2ce1e812020-05-20 19:35:27 +01001269 props.Annotations_enabled = module.sdkLibraryProperties.Annotations_enabled
Sundong Ahn054b19a2018-10-19 13:46:09 +09001270 props.Merge_annotations_dirs = module.sdkLibraryProperties.Merge_annotations_dirs
1271 props.Merge_inclusion_annotations_dirs = module.sdkLibraryProperties.Merge_inclusion_annotations_dirs
1272
Paul Duffincbcfcaa2020-04-07 18:49:53 +01001273 droidstubsArgs := []string{}
Paul Duffin235ffff2019-12-24 10:41:30 +00001274 if len(module.sdkLibraryProperties.Api_packages) != 0 {
Paul Duffincbcfcaa2020-04-07 18:49:53 +01001275 droidstubsArgs = append(droidstubsArgs, "--stub-packages "+strings.Join(module.sdkLibraryProperties.Api_packages, ":"))
Paul Duffin235ffff2019-12-24 10:41:30 +00001276 }
1277 if len(module.sdkLibraryProperties.Hidden_api_packages) != 0 {
Paul Duffincbcfcaa2020-04-07 18:49:53 +01001278 droidstubsArgs = append(droidstubsArgs,
Paul Duffin235ffff2019-12-24 10:41:30 +00001279 android.JoinWithPrefix(module.sdkLibraryProperties.Hidden_api_packages, " --hide-package "))
1280 }
Paul Duffincbcfcaa2020-04-07 18:49:53 +01001281 droidstubsArgs = append(droidstubsArgs, module.sdkLibraryProperties.Droiddoc_options...)
Paul Duffin235ffff2019-12-24 10:41:30 +00001282 disabledWarnings := []string{
1283 "MissingPermission",
1284 "BroadcastBehavior",
1285 "HiddenSuperclass",
1286 "DeprecationMismatch",
1287 "UnavailableSymbol",
1288 "SdkConstant",
1289 "HiddenTypeParameter",
1290 "Todo",
1291 "Typo",
1292 }
Paul Duffincbcfcaa2020-04-07 18:49:53 +01001293 droidstubsArgs = append(droidstubsArgs, android.JoinWithPrefix(disabledWarnings, "--hide "))
Sundong Ahnfb2721f2018-09-17 13:23:09 +09001294
Paul Duffina377e4c2020-04-29 13:30:54 +01001295 if !createStubSources {
1296 // Stubs are not required.
1297 props.Generate_stubs = proptools.BoolPtr(false)
1298 }
1299
Paul Duffin3c7c3472020-04-07 18:50:10 +01001300 // Add in scope specific arguments.
Paul Duffina377e4c2020-04-29 13:30:54 +01001301 droidstubsArgs = append(droidstubsArgs, scopeSpecificDroidstubsArgs...)
Paul Duffin11512472019-02-11 15:55:17 +00001302 props.Arg_files = module.sdkLibraryProperties.Droiddoc_option_files
Paul Duffincbcfcaa2020-04-07 18:49:53 +01001303 props.Args = proptools.StringPtr(strings.Join(droidstubsArgs, " "))
Jiyong Parkc678ad32018-04-10 13:07:10 +09001304
Paul Duffina377e4c2020-04-29 13:30:54 +01001305 if createApi {
1306 // List of APIs identified from the provided source files are created. They are later
1307 // compared against to the not-yet-released (a.k.a current) list of APIs and to the
1308 // last-released (a.k.a numbered) list of API.
1309 currentApiFileName := apiScope.apiFilePrefix + "current.txt"
1310 removedApiFileName := apiScope.apiFilePrefix + "removed.txt"
1311 apiDir := module.getApiDir()
1312 currentApiFileName = path.Join(apiDir, currentApiFileName)
1313 removedApiFileName = path.Join(apiDir, removedApiFileName)
Jiyong Parkc678ad32018-04-10 13:07:10 +09001314
Paul Duffina377e4c2020-04-29 13:30:54 +01001315 // check against the not-yet-release API
1316 props.Check_api.Current.Api_file = proptools.StringPtr(currentApiFileName)
1317 props.Check_api.Current.Removed_api_file = proptools.StringPtr(removedApiFileName)
Jiyong Park58c518b2018-05-12 22:29:12 +09001318
Paul Duffina377e4c2020-04-29 13:30:54 +01001319 if !apiScope.unstable {
1320 // check against the latest released API
1321 latestApiFilegroupName := proptools.StringPtr(module.latestApiFilegroupName(apiScope))
1322 props.Check_api.Last_released.Api_file = latestApiFilegroupName
1323 props.Check_api.Last_released.Removed_api_file = proptools.StringPtr(
1324 module.latestRemovedApiFilegroupName(apiScope))
1325 props.Check_api.Ignore_missing_latest_api = proptools.BoolPtr(true)
Paul Duffin8986cc92020-05-10 19:32:20 +01001326
Paul Duffina377e4c2020-04-29 13:30:54 +01001327 if proptools.Bool(module.sdkLibraryProperties.Api_lint.Enabled) {
1328 // Enable api lint.
1329 props.Check_api.Api_lint.Enabled = proptools.BoolPtr(true)
1330 props.Check_api.Api_lint.New_since = latestApiFilegroupName
Paul Duffin8986cc92020-05-10 19:32:20 +01001331
Paul Duffina377e4c2020-04-29 13:30:54 +01001332 // If it exists then pass a lint-baseline.txt through to droidstubs.
1333 baselinePath := path.Join(apiDir, apiScope.apiFilePrefix+"lint-baseline.txt")
1334 baselinePathRelativeToRoot := path.Join(mctx.ModuleDir(), baselinePath)
1335 paths, err := mctx.GlobWithDeps(baselinePathRelativeToRoot, nil)
1336 if err != nil {
1337 mctx.ModuleErrorf("error checking for presence of %s: %s", baselinePathRelativeToRoot, err)
1338 }
1339 if len(paths) == 1 {
1340 props.Check_api.Api_lint.Baseline_file = proptools.StringPtr(baselinePath)
1341 } else if len(paths) != 0 {
1342 mctx.ModuleErrorf("error checking for presence of %s: expected one path, found: %v", baselinePathRelativeToRoot, paths)
1343 }
Paul Duffin8986cc92020-05-10 19:32:20 +01001344 }
1345 }
Jiyong Park58c518b2018-05-12 22:29:12 +09001346
Paul Duffina377e4c2020-04-29 13:30:54 +01001347 // Dist the api txt artifact for sdk builds.
1348 if !Bool(module.sdkLibraryProperties.No_dist) {
1349 props.Dist.Targets = []string{"sdk", "win_sdk"}
1350 props.Dist.Dest = proptools.StringPtr(fmt.Sprintf("%v.txt", module.BaseModuleName()))
1351 props.Dist.Dir = proptools.StringPtr(path.Join(module.apiDistPath(apiScope), "api"))
1352 }
Anton Hansson6bb88102020-03-27 19:43:19 +00001353 }
1354
Colin Cross84dfc3d2019-09-25 11:33:01 -07001355 mctx.CreateModule(DroidstubsFactory, &props)
Jiyong Parkc678ad32018-04-10 13:07:10 +09001356}
1357
Jooyung Hanb8fa86a2020-03-10 06:23:13 +09001358func (module *SdkLibrary) DepIsInSameApex(mctx android.BaseModuleContext, dep android.Module) bool {
1359 depTag := mctx.OtherModuleDependencyTag(dep)
1360 if depTag == xmlPermissionsFileTag {
1361 return true
1362 }
1363 return module.Library.DepIsInSameApex(mctx, dep)
1364}
1365
Jiyong Parkc678ad32018-04-10 13:07:10 +09001366// Creates the xml file that publicizes the runtime library
Paul Duffin2aaef532020-04-29 16:47:28 +01001367func (module *SdkLibrary) createXmlFile(mctx android.DefaultableHookContext) {
Jiyong Parke3833882020-02-17 17:28:10 +09001368 props := struct {
Paul Duffind41712d2020-05-16 09:57:59 +01001369 Name *string
1370 Lib_name *string
1371 Apex_available []string
Jiyong Parke3833882020-02-17 17:28:10 +09001372 }{
Paul Duffinf642a312020-06-12 17:46:39 +01001373 Name: proptools.StringPtr(module.xmlPermissionsModuleName()),
Jooyung Hanb8fa86a2020-03-10 06:23:13 +09001374 Lib_name: proptools.StringPtr(module.BaseModuleName()),
1375 Apex_available: module.ApexProperties.Apex_available,
Jiyong Parkc678ad32018-04-10 13:07:10 +09001376 }
Jiyong Parke3833882020-02-17 17:28:10 +09001377
Jiyong Parke3833882020-02-17 17:28:10 +09001378 mctx.CreateModule(sdkLibraryXmlFactory, &props)
Jiyong Parkc678ad32018-04-10 13:07:10 +09001379}
1380
Paul Duffin50061512020-01-21 16:31:05 +00001381func PrebuiltJars(ctx android.BaseModuleContext, baseName string, s sdkSpec) android.Paths {
Jiyong Park6a927c42020-01-21 02:03:43 +09001382 var ver sdkVersion
1383 var kind sdkKind
1384 if s.usePrebuilt(ctx) {
1385 ver = s.version
1386 kind = s.kind
Jiyong Parkc678ad32018-04-10 13:07:10 +09001387 } else {
Jiyong Park6a927c42020-01-21 02:03:43 +09001388 // We don't have prebuilt SDK for the specific sdkVersion.
1389 // Instead of breaking the build, fallback to use "system_current"
1390 ver = sdkVersionCurrent
1391 kind = sdkSystem
Sundong Ahn054b19a2018-10-19 13:46:09 +09001392 }
Jiyong Park6a927c42020-01-21 02:03:43 +09001393
1394 dir := filepath.Join("prebuilts", "sdk", ver.String(), kind.String())
Paul Duffin50061512020-01-21 16:31:05 +00001395 jar := filepath.Join(dir, baseName+".jar")
Sundong Ahn054b19a2018-10-19 13:46:09 +09001396 jarPath := android.ExistentPathForSource(ctx, jar)
Sundong Ahnae418ac2019-02-28 15:01:28 +09001397 if !jarPath.Valid() {
Colin Cross07c88562020-01-07 09:34:44 -08001398 if ctx.Config().AllowMissingDependencies() {
1399 return android.Paths{android.PathForSource(ctx, jar)}
1400 } else {
Jiyong Park6a927c42020-01-21 02:03:43 +09001401 ctx.PropertyErrorf("sdk_library", "invalid sdk version %q, %q does not exist", s.raw, jar)
Colin Cross07c88562020-01-07 09:34:44 -08001402 }
Sundong Ahnae418ac2019-02-28 15:01:28 +09001403 return nil
1404 }
Sundong Ahn054b19a2018-10-19 13:46:09 +09001405 return android.Paths{jarPath.Path()}
1406}
1407
Colin Cross274a72d2020-08-11 12:17:01 -07001408// Get the apex names for module, nil if it is for platform.
1409func getApexNamesForModule(module android.Module) []string {
Paul Duffinbf19a972020-05-26 13:21:35 +01001410 if apex, ok := module.(android.ApexModule); ok {
Colin Cross274a72d2020-08-11 12:17:01 -07001411 return apex.InApexes()
Paul Duffinbf19a972020-05-26 13:21:35 +01001412 }
1413
Colin Cross274a72d2020-08-11 12:17:01 -07001414 return nil
Paul Duffinbf19a972020-05-26 13:21:35 +01001415}
1416
Colin Cross274a72d2020-08-11 12:17:01 -07001417// Check to see if the other module is within the same set of named APEXes as this module.
Paul Duffinbf19a972020-05-26 13:21:35 +01001418//
1419// If either this or the other module are on the platform then this will return
1420// false.
Colin Cross274a72d2020-08-11 12:17:01 -07001421func withinSameApexesAs(module android.ApexModule, other android.Module) bool {
1422 names := module.InApexes()
1423 return len(names) > 0 && reflect.DeepEqual(names, getApexNamesForModule(other))
Paul Duffinbf19a972020-05-26 13:21:35 +01001424}
1425
Paul Duffin47624362020-05-20 12:19:10 +01001426func (module *SdkLibrary) sdkJars(ctx android.BaseModuleContext, sdkVersion sdkSpec, headerJars bool) android.Paths {
Jiyong Park27fc4142020-05-28 00:19:53 +09001427 // If the client doesn't set sdk_version, but if this library prefers stubs over
1428 // the impl library, let's provide the widest API surface possible. To do so,
1429 // force override sdk_version to module_current so that the closest possible API
1430 // surface could be found in selectHeaderJarsForSdkVersion
1431 if module.defaultsToStubs() && !sdkVersion.specified() {
1432 sdkVersion = sdkSpecFrom("module_current")
1433 }
Paul Duffind1b3a922020-01-22 11:57:20 +00001434
Paul Duffin2e7ed652020-05-26 18:13:57 +01001435 // Only provide access to the implementation library if it is actually built.
1436 if module.requiresRuntimeImplementationLibrary() {
1437 // Check any special cases for java_sdk_library.
1438 //
1439 // Only allow access to the implementation library in the following condition:
1440 // * No sdk_version specified on the referencing module.
Paul Duffinbf19a972020-05-26 13:21:35 +01001441 // * The referencing module is in the same apex as this.
Colin Cross274a72d2020-08-11 12:17:01 -07001442 if sdkVersion.kind == sdkPrivate || withinSameApexesAs(module, ctx.Module()) {
Paul Duffin2e7ed652020-05-26 18:13:57 +01001443 if headerJars {
1444 return module.HeaderJars()
1445 } else {
1446 return module.ImplementationJars()
1447 }
Sundong Ahn054b19a2018-10-19 13:46:09 +09001448 }
Jiyong Parkc678ad32018-04-10 13:07:10 +09001449 }
Paul Duffin47624362020-05-20 12:19:10 +01001450
Paul Duffina3fb67d2020-05-20 14:20:02 +01001451 return module.selectHeaderJarsForSdkVersion(ctx, sdkVersion)
Jiyong Parkc678ad32018-04-10 13:07:10 +09001452}
1453
Sundong Ahn241cd372018-07-13 16:16:44 +09001454// to satisfy SdkLibraryDependency interface
Paul Duffind1b3a922020-01-22 11:57:20 +00001455func (module *SdkLibrary) SdkHeaderJars(ctx android.BaseModuleContext, sdkVersion sdkSpec) android.Paths {
1456 return module.sdkJars(ctx, sdkVersion, true /*headerJars*/)
1457}
1458
1459// to satisfy SdkLibraryDependency interface
Jiyong Park6a927c42020-01-21 02:03:43 +09001460func (module *SdkLibrary) SdkImplementationJars(ctx android.BaseModuleContext, sdkVersion sdkSpec) android.Paths {
Paul Duffind1b3a922020-01-22 11:57:20 +00001461 return module.sdkJars(ctx, sdkVersion, false /*headerJars*/)
Sundong Ahn241cd372018-07-13 16:16:44 +09001462}
1463
Sundong Ahn80a87b32019-05-13 15:02:50 +09001464func (module *SdkLibrary) SetNoDist() {
1465 module.sdkLibraryProperties.No_dist = proptools.BoolPtr(true)
1466}
1467
Colin Cross571cccf2019-02-04 11:22:08 -08001468var javaSdkLibrariesKey = android.NewOnceKey("javaSdkLibraries")
1469
Jiyong Park82484c02018-04-23 21:41:26 +09001470func javaSdkLibraries(config android.Config) *[]string {
Colin Cross571cccf2019-02-04 11:22:08 -08001471 return config.Once(javaSdkLibrariesKey, func() interface{} {
Jiyong Park82484c02018-04-23 21:41:26 +09001472 return &[]string{}
1473 }).(*[]string)
1474}
1475
Paul Duffin749f98f2019-12-30 17:23:46 +00001476func (module *SdkLibrary) getApiDir() string {
1477 return proptools.StringDefault(module.sdkLibraryProperties.Api_dir, "api")
1478}
1479
Jiyong Parkc678ad32018-04-10 13:07:10 +09001480// For a java_sdk_library module, create internal modules for stubs, docs,
1481// runtime libs and xml file. If requested, the stubs and docs are created twice
1482// once for public API level and once for system API level
Paul Duffin2aaef532020-04-29 16:47:28 +01001483func (module *SdkLibrary) CreateInternalModules(mctx android.DefaultableHookContext) {
1484 // If the module has been disabled then don't create any child modules.
1485 if !module.Enabled() {
1486 return
1487 }
1488
Paul Duffinc5d954a2020-05-16 18:54:24 +01001489 if len(module.properties.Srcs) == 0 {
Inseob Kimc0907f12019-02-08 21:00:45 +09001490 mctx.PropertyErrorf("srcs", "java_sdk_library must specify srcs")
Jooyung Han58f26ab2019-12-18 15:34:32 +09001491 return
Inseob Kimc0907f12019-02-08 21:00:45 +09001492 }
1493
Paul Duffin37e0b772019-12-30 17:20:10 +00001494 // If this builds against standard libraries (i.e. is not part of the core libraries)
1495 // then assume it provides both system and test apis. Otherwise, assume it does not and
1496 // also assume it does not contribute to the dist build.
1497 sdkDep := decodeSdkDep(mctx, sdkContext(&module.Library))
1498 hasSystemAndTestApis := sdkDep.hasStandardLibs()
Paul Duffin3a254982020-04-28 10:44:03 +01001499 module.sdkLibraryProperties.Generate_system_and_test_apis = hasSystemAndTestApis
Paul Duffin37e0b772019-12-30 17:20:10 +00001500 module.sdkLibraryProperties.No_dist = proptools.BoolPtr(!hasSystemAndTestApis)
1501
Inseob Kim8098faa2019-03-18 10:19:51 +09001502 missing_current_api := false
1503
Paul Duffin3a254982020-04-28 10:44:03 +01001504 generatedScopes := module.getGeneratedApiScopes(mctx)
Paul Duffind1b3a922020-01-22 11:57:20 +00001505
Paul Duffin749f98f2019-12-30 17:23:46 +00001506 apiDir := module.getApiDir()
Paul Duffin3a254982020-04-28 10:44:03 +01001507 for _, scope := range generatedScopes {
Inseob Kim8098faa2019-03-18 10:19:51 +09001508 for _, api := range []string{"current.txt", "removed.txt"} {
Paul Duffind1b3a922020-01-22 11:57:20 +00001509 path := path.Join(mctx.ModuleDir(), apiDir, scope.apiFilePrefix+api)
Inseob Kim8098faa2019-03-18 10:19:51 +09001510 p := android.ExistentPathForSource(mctx, path)
1511 if !p.Valid() {
1512 mctx.ModuleErrorf("Current api file %#v doesn't exist", path)
1513 missing_current_api = true
1514 }
1515 }
1516 }
1517
1518 if missing_current_api {
1519 script := "build/soong/scripts/gen-java-current-api-files.sh"
1520 p := android.ExistentPathForSource(mctx, script)
1521
1522 if !p.Valid() {
1523 panic(fmt.Sprintf("script file %s doesn't exist", script))
1524 }
1525
1526 mctx.ModuleErrorf("One or more current api files are missing. "+
1527 "You can update them by:\n"+
Paul Duffin37e0b772019-12-30 17:20:10 +00001528 "%s %q %s && m update-api",
Paul Duffind1b3a922020-01-22 11:57:20 +00001529 script, filepath.Join(mctx.ModuleDir(), apiDir),
Paul Duffin3a254982020-04-28 10:44:03 +01001530 strings.Join(generatedScopes.Strings(func(s *apiScope) string { return s.apiFilePrefix }), " "))
Inseob Kim8098faa2019-03-18 10:19:51 +09001531 return
1532 }
1533
Paul Duffin3a254982020-04-28 10:44:03 +01001534 for _, scope := range generatedScopes {
Paul Duffina377e4c2020-04-29 13:30:54 +01001535 stubsSourceArgs := scope.droidstubsArgsForGeneratingStubsSource
Paul Duffinb74ee3e2020-05-08 14:16:20 +01001536 stubsSourceModuleName := module.stubsSourceModuleName(scope)
Paul Duffina377e4c2020-04-29 13:30:54 +01001537
1538 // If the args needed to generate the stubs and API are the same then they
1539 // can be generated in a single invocation of metalava, otherwise they will
1540 // need separate invocations.
1541 if scope.createStubsSourceAndApiTogether {
1542 // Use the stubs source name for legacy reasons.
1543 module.createStubsSourcesAndApi(mctx, scope, stubsSourceModuleName, true, true, stubsSourceArgs)
1544 } else {
1545 module.createStubsSourcesAndApi(mctx, scope, stubsSourceModuleName, true, false, stubsSourceArgs)
1546
1547 apiArgs := scope.droidstubsArgsForGeneratingApi
Paul Duffinb74ee3e2020-05-08 14:16:20 +01001548 apiName := module.apiModuleName(scope)
Paul Duffina377e4c2020-04-29 13:30:54 +01001549 module.createStubsSourcesAndApi(mctx, scope, apiName, false, true, apiArgs)
1550 }
1551
Paul Duffind1b3a922020-01-22 11:57:20 +00001552 module.createStubsLibrary(mctx, scope)
Inseob Kimc0907f12019-02-08 21:00:45 +09001553 }
1554
Paul Duffind11e78e2020-05-15 20:37:11 +01001555 if module.requiresRuntimeImplementationLibrary() {
Paul Duffin9d582cc2020-05-16 15:52:12 +01001556 // Create child module to create an implementation library.
1557 //
1558 // This temporarily creates a second implementation library that can be explicitly
1559 // referenced.
1560 //
1561 // TODO(b/156618935) - update comment once only one implementation library is created.
1562 module.createImplLibrary(mctx)
1563
Paul Duffind11e78e2020-05-15 20:37:11 +01001564 // Only create an XML permissions file that declares the library as being usable
1565 // as a shared library if required.
1566 if module.sharedLibrary() {
1567 module.createXmlFile(mctx)
1568 }
Paul Duffin43db9be2019-12-30 17:35:49 +00001569
1570 // record java_sdk_library modules so that they are exported to make
1571 javaSdkLibraries := javaSdkLibraries(mctx.Config())
1572 javaSdkLibrariesLock.Lock()
1573 defer javaSdkLibrariesLock.Unlock()
1574 *javaSdkLibraries = append(*javaSdkLibraries, module.BaseModuleName())
1575 }
Inseob Kimc0907f12019-02-08 21:00:45 +09001576}
1577
1578func (module *SdkLibrary) InitSdkLibraryProperties() {
Colin Cross1c14b4e2020-06-15 16:09:53 -07001579 module.addHostAndDeviceProperties()
1580 module.AddProperties(&module.sdkLibraryProperties)
Sundong Ahn054b19a2018-10-19 13:46:09 +09001581
Paul Duffin64e61992020-05-15 10:20:31 +01001582 module.initSdkLibraryComponent(&module.ModuleBase)
1583
Paul Duffinc5d954a2020-05-16 18:54:24 +01001584 module.properties.Installable = proptools.BoolPtr(true)
1585 module.deviceProperties.IsSDKLibrary = true
Inseob Kimc0907f12019-02-08 21:00:45 +09001586}
Sundong Ahn054b19a2018-10-19 13:46:09 +09001587
Paul Duffind11e78e2020-05-15 20:37:11 +01001588func (module *SdkLibrary) requiresRuntimeImplementationLibrary() bool {
1589 return !proptools.Bool(module.sdkLibraryProperties.Api_only)
1590}
1591
Jiyong Park27fc4142020-05-28 00:19:53 +09001592func (module *SdkLibrary) defaultsToStubs() bool {
1593 return proptools.Bool(module.sdkLibraryProperties.Default_to_stubs)
1594}
1595
Paul Duffin1a724e62020-05-08 13:44:43 +01001596// Defines how to name the individual component modules the sdk library creates.
1597type sdkLibraryComponentNamingScheme interface {
1598 stubsLibraryModuleName(scope *apiScope, baseName string) string
1599
1600 stubsSourceModuleName(scope *apiScope, baseName string) string
1601
1602 apiModuleName(scope *apiScope, baseName string) string
1603}
1604
1605type defaultNamingScheme struct {
1606}
1607
1608func (s *defaultNamingScheme) stubsLibraryModuleName(scope *apiScope, baseName string) string {
1609 return scope.stubsLibraryModuleName(baseName)
1610}
1611
1612func (s *defaultNamingScheme) stubsSourceModuleName(scope *apiScope, baseName string) string {
1613 return scope.stubsSourceModuleName(baseName)
1614}
1615
1616func (s *defaultNamingScheme) apiModuleName(scope *apiScope, baseName string) string {
1617 return scope.apiModuleName(baseName)
1618}
1619
1620var _ sdkLibraryComponentNamingScheme = (*defaultNamingScheme)(nil)
1621
Paul Duffindef8a892020-05-08 15:36:30 +01001622type frameworkModulesNamingScheme struct {
1623}
1624
1625func (s *frameworkModulesNamingScheme) moduleSuffix(scope *apiScope) string {
1626 suffix := scope.name
1627 if scope == apiScopeModuleLib {
1628 suffix = "module_libs_"
1629 }
1630 return suffix
1631}
1632
1633func (s *frameworkModulesNamingScheme) stubsLibraryModuleName(scope *apiScope, baseName string) string {
1634 return fmt.Sprintf("%s-stubs-%sapi", baseName, s.moduleSuffix(scope))
1635}
1636
1637func (s *frameworkModulesNamingScheme) stubsSourceModuleName(scope *apiScope, baseName string) string {
1638 return fmt.Sprintf("%s-stubs-srcs-%sapi", baseName, s.moduleSuffix(scope))
1639}
1640
1641func (s *frameworkModulesNamingScheme) apiModuleName(scope *apiScope, baseName string) string {
1642 return fmt.Sprintf("%s-api-%sapi", baseName, s.moduleSuffix(scope))
1643}
1644
1645var _ sdkLibraryComponentNamingScheme = (*frameworkModulesNamingScheme)(nil)
1646
Anton Hansson0bd88d02020-05-25 12:20:51 +01001647func moduleStubLinkType(name string) (stub bool, ret linkType) {
1648 // This suffix-based approach is fragile and could potentially mis-trigger.
1649 // TODO(b/155164730): Clean this up when modules no longer reference sdk_lib stubs directly.
1650 if strings.HasSuffix(name, ".stubs.public") || strings.HasSuffix(name, "-stubs-publicapi") {
1651 return true, javaSdk
1652 }
1653 if strings.HasSuffix(name, ".stubs.system") || strings.HasSuffix(name, "-stubs-systemapi") {
1654 return true, javaSystem
1655 }
1656 if strings.HasSuffix(name, ".stubs.module_lib") || strings.HasSuffix(name, "-stubs-module_libs_api") {
1657 return true, javaModule
1658 }
1659 if strings.HasSuffix(name, ".stubs.test") {
1660 return true, javaSystem
1661 }
1662 return false, javaPlatform
1663}
1664
Jaewoong Jung4f158ee2019-07-11 10:05:35 -07001665// java_sdk_library is a special Java library that provides optional platform APIs to apps.
1666// In practice, it can be viewed as a combination of several modules: 1) stubs library that clients
1667// are linked against to, 2) droiddoc module that internally generates API stubs source files,
1668// 3) the real runtime shared library that implements the APIs, and 4) XML file for adding
1669// the runtime lib to the classpath at runtime if requested via <uses-library>.
Inseob Kimc0907f12019-02-08 21:00:45 +09001670func SdkLibraryFactory() android.Module {
1671 module := &SdkLibrary{}
Paul Duffinb74ee3e2020-05-08 14:16:20 +01001672
1673 // Initialize information common between source and prebuilt.
1674 module.initCommon(&module.ModuleBase)
1675
Inseob Kimc0907f12019-02-08 21:00:45 +09001676 module.InitSdkLibraryProperties()
Jooyung Han58f26ab2019-12-18 15:34:32 +09001677 android.InitApexModule(module)
Sundong Ahn054b19a2018-10-19 13:46:09 +09001678 InitJavaModule(module, android.HostAndDeviceSupported)
Paul Duffin3a254982020-04-28 10:44:03 +01001679
1680 // Initialize the map from scope to scope specific properties.
1681 scopeToProperties := make(map[*apiScope]*ApiScopeProperties)
1682 for _, scope := range allApiScopes {
1683 scopeToProperties[scope] = scope.scopeSpecificProperties(module)
1684 }
1685 module.scopeToProperties = scopeToProperties
1686
Paul Duffin344c4ee2020-04-29 23:35:13 +01001687 // Add the properties containing visibility rules so that they are checked.
Paul Duffin9d582cc2020-05-16 15:52:12 +01001688 android.AddVisibilityProperty(module, "impl_library_visibility", &module.sdkLibraryProperties.Impl_library_visibility)
Paul Duffin344c4ee2020-04-29 23:35:13 +01001689 android.AddVisibilityProperty(module, "stubs_library_visibility", &module.sdkLibraryProperties.Stubs_library_visibility)
1690 android.AddVisibilityProperty(module, "stubs_source_visibility", &module.sdkLibraryProperties.Stubs_source_visibility)
1691
Paul Duffin1a724e62020-05-08 13:44:43 +01001692 module.SetDefaultableHook(func(ctx android.DefaultableHookContext) {
Paul Duffind11e78e2020-05-15 20:37:11 +01001693 // If no implementation is required then it cannot be used as a shared library
1694 // either.
1695 if !module.requiresRuntimeImplementationLibrary() {
1696 // If shared_library has been explicitly set to true then it is incompatible
1697 // with api_only: true.
1698 if proptools.Bool(module.commonSdkLibraryProperties.Shared_library) {
1699 ctx.PropertyErrorf("api_only/shared_library", "inconsistent settings, shared_library and api_only cannot both be true")
1700 }
1701 // Set shared_library: false.
1702 module.commonSdkLibraryProperties.Shared_library = proptools.BoolPtr(false)
1703 }
1704
Paul Duffin1a724e62020-05-08 13:44:43 +01001705 if module.initCommonAfterDefaultsApplied(ctx) {
1706 module.CreateInternalModules(ctx)
1707 }
1708 })
Jiyong Parkc678ad32018-04-10 13:07:10 +09001709 return module
1710}
Colin Cross79c7c262019-04-17 11:11:46 -07001711
1712//
1713// SDK library prebuilts
1714//
1715
Paul Duffin56d44902020-01-31 13:36:25 +00001716// Properties associated with each api scope.
1717type sdkLibraryScopeProperties struct {
Colin Cross79c7c262019-04-17 11:11:46 -07001718 Jars []string `android:"path"`
1719
1720 Sdk_version *string
1721
Colin Cross79c7c262019-04-17 11:11:46 -07001722 // List of shared java libs that this module has dependencies to
1723 Libs []string
Paul Duffinf488ef22020-04-09 00:10:17 +01001724
Paul Duffin5fb82132020-04-29 20:45:27 +01001725 // The stubs source.
Paul Duffinf488ef22020-04-09 00:10:17 +01001726 Stub_srcs []string `android:"path"`
Paul Duffin75dcc802020-04-09 01:08:11 +01001727
1728 // The current.txt
Paul Duffin533f9c72020-05-20 16:18:00 +01001729 Current_api *string `android:"path"`
Paul Duffin75dcc802020-04-09 01:08:11 +01001730
1731 // The removed.txt
Paul Duffin533f9c72020-05-20 16:18:00 +01001732 Removed_api *string `android:"path"`
Colin Cross79c7c262019-04-17 11:11:46 -07001733}
1734
Paul Duffin56d44902020-01-31 13:36:25 +00001735type sdkLibraryImportProperties struct {
Paul Duffinfcfd7912020-01-31 17:54:30 +00001736 // List of shared java libs, common to all scopes, that this module has
1737 // dependencies to
1738 Libs []string
Paul Duffin56d44902020-01-31 13:36:25 +00001739}
1740
Paul Duffinf642a312020-06-12 17:46:39 +01001741type SdkLibraryImport struct {
Colin Cross79c7c262019-04-17 11:11:46 -07001742 android.ModuleBase
1743 android.DefaultableModuleBase
1744 prebuilt android.Prebuilt
Paul Duffin61871622020-02-10 13:37:10 +00001745 android.ApexModuleBase
1746 android.SdkBase
Colin Cross79c7c262019-04-17 11:11:46 -07001747
1748 properties sdkLibraryImportProperties
1749
Paul Duffin6a2bd112020-04-07 19:27:04 +01001750 // Map from api scope to the scope specific property structure.
1751 scopeProperties map[*apiScope]*sdkLibraryScopeProperties
1752
Paul Duffin56d44902020-01-31 13:36:25 +00001753 commonToSdkLibraryAndImport
Paul Duffinf642a312020-06-12 17:46:39 +01001754
1755 // The reference to the implementation library created by the source module.
1756 // Is nil if the source module does not exist.
1757 implLibraryModule *Library
1758
1759 // The reference to the xml permissions module created by the source module.
1760 // Is nil if the source module does not exist.
1761 xmlPermissionsFileModule *sdkLibraryXml
Colin Cross79c7c262019-04-17 11:11:46 -07001762}
1763
Paul Duffinf642a312020-06-12 17:46:39 +01001764var _ SdkLibraryDependency = (*SdkLibraryImport)(nil)
Colin Cross79c7c262019-04-17 11:11:46 -07001765
Paul Duffin6a2bd112020-04-07 19:27:04 +01001766// The type of a structure that contains a field of type sdkLibraryScopeProperties
1767// for each apiscope in allApiScopes, e.g. something like:
1768// struct {
1769// Public sdkLibraryScopeProperties
1770// System sdkLibraryScopeProperties
1771// ...
1772// }
1773var allScopeStructType = createAllScopePropertiesStructType()
1774
1775// Dynamically create a structure type for each apiscope in allApiScopes.
1776func createAllScopePropertiesStructType() reflect.Type {
1777 var fields []reflect.StructField
1778 for _, apiScope := range allApiScopes {
1779 field := reflect.StructField{
1780 Name: apiScope.fieldName,
1781 Type: reflect.TypeOf(sdkLibraryScopeProperties{}),
1782 }
1783 fields = append(fields, field)
1784 }
1785
1786 return reflect.StructOf(fields)
1787}
1788
1789// Create an instance of the scope specific structure type and return a map
1790// from apiscope to a pointer to each scope specific field.
1791func createPropertiesInstance() (interface{}, map[*apiScope]*sdkLibraryScopeProperties) {
1792 allScopePropertiesPtr := reflect.New(allScopeStructType)
1793 allScopePropertiesStruct := allScopePropertiesPtr.Elem()
1794 scopeProperties := make(map[*apiScope]*sdkLibraryScopeProperties)
1795
1796 for _, apiScope := range allApiScopes {
1797 field := allScopePropertiesStruct.FieldByName(apiScope.fieldName)
1798 scopeProperties[apiScope] = field.Addr().Interface().(*sdkLibraryScopeProperties)
1799 }
1800
1801 return allScopePropertiesPtr.Interface(), scopeProperties
1802}
1803
Jaewoong Jung4f158ee2019-07-11 10:05:35 -07001804// java_sdk_library_import imports a prebuilt java_sdk_library.
Colin Cross79c7c262019-04-17 11:11:46 -07001805func sdkLibraryImportFactory() android.Module {
Paul Duffinf642a312020-06-12 17:46:39 +01001806 module := &SdkLibraryImport{}
Colin Cross79c7c262019-04-17 11:11:46 -07001807
Paul Duffin6a2bd112020-04-07 19:27:04 +01001808 allScopeProperties, scopeToProperties := createPropertiesInstance()
1809 module.scopeProperties = scopeToProperties
1810 module.AddProperties(&module.properties, allScopeProperties)
Colin Cross79c7c262019-04-17 11:11:46 -07001811
Paul Duffinb74ee3e2020-05-08 14:16:20 +01001812 // Initialize information common between source and prebuilt.
1813 module.initCommon(&module.ModuleBase)
1814
Paul Duffin0bdcb272020-02-06 15:24:57 +00001815 android.InitPrebuiltModule(module, &[]string{""})
Paul Duffin61871622020-02-10 13:37:10 +00001816 android.InitApexModule(module)
1817 android.InitSdkAwareModule(module)
Colin Cross79c7c262019-04-17 11:11:46 -07001818 InitJavaModule(module, android.HostAndDeviceSupported)
1819
Paul Duffin1a724e62020-05-08 13:44:43 +01001820 module.SetDefaultableHook(func(mctx android.DefaultableHookContext) {
1821 if module.initCommonAfterDefaultsApplied(mctx) {
1822 module.createInternalModules(mctx)
1823 }
1824 })
Colin Cross79c7c262019-04-17 11:11:46 -07001825 return module
1826}
1827
Paul Duffinf642a312020-06-12 17:46:39 +01001828func (module *SdkLibraryImport) Prebuilt() *android.Prebuilt {
Colin Cross79c7c262019-04-17 11:11:46 -07001829 return &module.prebuilt
1830}
1831
Paul Duffinf642a312020-06-12 17:46:39 +01001832func (module *SdkLibraryImport) Name() string {
Colin Cross79c7c262019-04-17 11:11:46 -07001833 return module.prebuilt.Name(module.ModuleBase.Name())
1834}
1835
Paul Duffinf642a312020-06-12 17:46:39 +01001836func (module *SdkLibraryImport) createInternalModules(mctx android.DefaultableHookContext) {
Colin Cross79c7c262019-04-17 11:11:46 -07001837
Paul Duffin50061512020-01-21 16:31:05 +00001838 // If the build is configured to use prebuilts then force this to be preferred.
1839 if mctx.Config().UnbundledBuildUsePrebuiltSdks() {
1840 module.prebuilt.ForcePrefer()
1841 }
1842
Paul Duffin6a2bd112020-04-07 19:27:04 +01001843 for apiScope, scopeProperties := range module.scopeProperties {
Paul Duffin56d44902020-01-31 13:36:25 +00001844 if len(scopeProperties.Jars) == 0 {
1845 continue
1846 }
1847
Paul Duffinf6155722020-04-09 00:07:11 +01001848 module.createJavaImportForStubs(mctx, apiScope, scopeProperties)
Paul Duffinf488ef22020-04-09 00:10:17 +01001849
Paul Duffin533f9c72020-05-20 16:18:00 +01001850 if len(scopeProperties.Stub_srcs) > 0 {
1851 module.createPrebuiltStubsSources(mctx, apiScope, scopeProperties)
1852 }
Paul Duffin56d44902020-01-31 13:36:25 +00001853 }
Colin Cross79c7c262019-04-17 11:11:46 -07001854
1855 javaSdkLibraries := javaSdkLibraries(mctx.Config())
1856 javaSdkLibrariesLock.Lock()
1857 defer javaSdkLibrariesLock.Unlock()
1858 *javaSdkLibraries = append(*javaSdkLibraries, module.BaseModuleName())
1859}
1860
Paul Duffinf642a312020-06-12 17:46:39 +01001861func (module *SdkLibraryImport) createJavaImportForStubs(mctx android.DefaultableHookContext, apiScope *apiScope, scopeProperties *sdkLibraryScopeProperties) {
Paul Duffinf6155722020-04-09 00:07:11 +01001862 // Creates a java import for the jar with ".stubs" suffix
1863 props := struct {
Paul Duffind41712d2020-05-16 09:57:59 +01001864 Name *string
1865 Sdk_version *string
1866 Libs []string
1867 Jars []string
1868 Prefer *bool
Paul Duffinf6155722020-04-09 00:07:11 +01001869 }{}
Paul Duffinb74ee3e2020-05-08 14:16:20 +01001870 props.Name = proptools.StringPtr(module.stubsLibraryModuleName(apiScope))
Paul Duffinf6155722020-04-09 00:07:11 +01001871 props.Sdk_version = scopeProperties.Sdk_version
1872 // Prepend any of the libs from the legacy public properties to the libs for each of the
1873 // scopes to avoid having to duplicate them in each scope.
1874 props.Libs = append(module.properties.Libs, scopeProperties.Libs...)
1875 props.Jars = scopeProperties.Jars
Paul Duffind41712d2020-05-16 09:57:59 +01001876
Paul Duffindd89a282020-05-13 16:08:09 +01001877 // The imports are preferred if the java_sdk_library_import is preferred.
1878 props.Prefer = proptools.BoolPtr(module.prebuilt.Prefer())
Paul Duffin64e61992020-05-15 10:20:31 +01001879
1880 mctx.CreateModule(ImportFactory, &props, module.sdkComponentPropertiesForChildLibrary())
Paul Duffinf6155722020-04-09 00:07:11 +01001881}
1882
Paul Duffinf642a312020-06-12 17:46:39 +01001883func (module *SdkLibraryImport) createPrebuiltStubsSources(mctx android.DefaultableHookContext, apiScope *apiScope, scopeProperties *sdkLibraryScopeProperties) {
Paul Duffinf488ef22020-04-09 00:10:17 +01001884 props := struct {
Paul Duffindd89a282020-05-13 16:08:09 +01001885 Name *string
1886 Srcs []string
1887 Prefer *bool
Paul Duffinf488ef22020-04-09 00:10:17 +01001888 }{}
Paul Duffinb74ee3e2020-05-08 14:16:20 +01001889 props.Name = proptools.StringPtr(module.stubsSourceModuleName(apiScope))
Paul Duffinf488ef22020-04-09 00:10:17 +01001890 props.Srcs = scopeProperties.Stub_srcs
1891 mctx.CreateModule(PrebuiltStubsSourcesFactory, &props)
Paul Duffindd89a282020-05-13 16:08:09 +01001892
1893 // The stubs source is preferred if the java_sdk_library_import is preferred.
1894 props.Prefer = proptools.BoolPtr(module.prebuilt.Prefer())
Paul Duffinf488ef22020-04-09 00:10:17 +01001895}
1896
Paul Duffin27cd4a52020-06-26 20:17:02 +01001897// Add the dependencies on the child module in the component deps mutator so that it
1898// creates references to the prebuilt and not the source modules.
1899func (module *SdkLibraryImport) ComponentDepsMutator(ctx android.BottomUpMutatorContext) {
Paul Duffin6a2bd112020-04-07 19:27:04 +01001900 for apiScope, scopeProperties := range module.scopeProperties {
Paul Duffin56d44902020-01-31 13:36:25 +00001901 if len(scopeProperties.Jars) == 0 {
1902 continue
1903 }
1904
1905 // Add dependencies to the prebuilt stubs library
Paul Duffin27cd4a52020-06-26 20:17:02 +01001906 ctx.AddVariationDependencies(nil, apiScope.stubsTag, "prebuilt_"+module.stubsLibraryModuleName(apiScope))
Paul Duffin533f9c72020-05-20 16:18:00 +01001907
1908 if len(scopeProperties.Stub_srcs) > 0 {
1909 // Add dependencies to the prebuilt stubs source library
Paul Duffin27cd4a52020-06-26 20:17:02 +01001910 ctx.AddVariationDependencies(nil, apiScope.stubsSourceTag, "prebuilt_"+module.stubsSourceModuleName(apiScope))
Paul Duffin533f9c72020-05-20 16:18:00 +01001911 }
Paul Duffin56d44902020-01-31 13:36:25 +00001912 }
Paul Duffin27cd4a52020-06-26 20:17:02 +01001913}
1914
1915// Add other dependencies as normal.
1916func (module *SdkLibraryImport) DepsMutator(ctx android.BottomUpMutatorContext) {
Paul Duffinf642a312020-06-12 17:46:39 +01001917
1918 implName := module.implLibraryModuleName()
1919 if ctx.OtherModuleExists(implName) {
1920 ctx.AddVariationDependencies(nil, implLibraryTag, implName)
1921
1922 xmlPermissionsModuleName := module.xmlPermissionsModuleName()
1923 if module.sharedLibrary() && ctx.OtherModuleExists(xmlPermissionsModuleName) {
1924 // Add dependency to the rule for generating the xml permissions file
1925 ctx.AddDependency(module, xmlPermissionsFileTag, xmlPermissionsModuleName)
1926 }
1927 }
Colin Cross79c7c262019-04-17 11:11:46 -07001928}
1929
Paul Duffinf642a312020-06-12 17:46:39 +01001930func (module *SdkLibraryImport) DepIsInSameApex(mctx android.BaseModuleContext, dep android.Module) bool {
1931 depTag := mctx.OtherModuleDependencyTag(dep)
1932 if depTag == xmlPermissionsFileTag {
1933 return true
1934 }
1935
1936 // None of the other dependencies of the java_sdk_library_import are in the same apex
1937 // as the one that references this module.
1938 return false
1939}
1940
1941func (module *SdkLibraryImport) OutputFiles(tag string) (android.Paths, error) {
Paul Duffin46fdda82020-05-14 15:39:10 +01001942 return module.commonOutputFiles(tag)
1943}
1944
Paul Duffinf642a312020-06-12 17:46:39 +01001945func (module *SdkLibraryImport) GenerateAndroidBuildActions(ctx android.ModuleContext) {
Paul Duffin533f9c72020-05-20 16:18:00 +01001946 // Record the paths to the prebuilt stubs library and stubs source.
Colin Cross79c7c262019-04-17 11:11:46 -07001947 ctx.VisitDirectDeps(func(to android.Module) {
1948 tag := ctx.OtherModuleDependencyTag(to)
1949
Paul Duffin533f9c72020-05-20 16:18:00 +01001950 // Extract information from any of the scope specific dependencies.
1951 if scopeTag, ok := tag.(scopeDependencyTag); ok {
1952 apiScope := scopeTag.apiScope
1953 scopePaths := module.getScopePathsCreateIfNeeded(apiScope)
1954
1955 // Extract information from the dependency. The exact information extracted
1956 // is determined by the nature of the dependency which is determined by the tag.
1957 scopeTag.extractDepInfo(ctx, to, scopePaths)
Paul Duffinf642a312020-06-12 17:46:39 +01001958 } else if tag == implLibraryTag {
1959 if implLibrary, ok := to.(*Library); ok {
1960 module.implLibraryModule = implLibrary
1961 } else {
1962 ctx.ModuleErrorf("implementation library must be of type *java.Library but was %T", to)
1963 }
1964 } else if tag == xmlPermissionsFileTag {
1965 if xmlPermissionsFileModule, ok := to.(*sdkLibraryXml); ok {
1966 module.xmlPermissionsFileModule = xmlPermissionsFileModule
1967 } else {
1968 ctx.ModuleErrorf("xml permissions file module must be of type *sdkLibraryXml but was %T", to)
1969 }
Colin Cross79c7c262019-04-17 11:11:46 -07001970 }
1971 })
Paul Duffin533f9c72020-05-20 16:18:00 +01001972
1973 // Populate the scope paths with information from the properties.
1974 for apiScope, scopeProperties := range module.scopeProperties {
1975 if len(scopeProperties.Jars) == 0 {
1976 continue
1977 }
1978
1979 paths := module.getScopePathsCreateIfNeeded(apiScope)
1980 paths.currentApiFilePath = android.OptionalPathForModuleSrc(ctx, scopeProperties.Current_api)
1981 paths.removedApiFilePath = android.OptionalPathForModuleSrc(ctx, scopeProperties.Removed_api)
1982 }
Colin Cross79c7c262019-04-17 11:11:46 -07001983}
1984
Paul Duffinf642a312020-06-12 17:46:39 +01001985func (module *SdkLibraryImport) sdkJars(ctx android.BaseModuleContext, sdkVersion sdkSpec, headerJars bool) android.Paths {
1986
1987 // For consistency with SdkLibrary make the implementation jar available to libraries that
1988 // are within the same APEX.
1989 implLibraryModule := module.implLibraryModule
Colin Cross274a72d2020-08-11 12:17:01 -07001990 if implLibraryModule != nil && withinSameApexesAs(module, ctx.Module()) {
Paul Duffinf642a312020-06-12 17:46:39 +01001991 if headerJars {
1992 return implLibraryModule.HeaderJars()
1993 } else {
1994 return implLibraryModule.ImplementationJars()
1995 }
1996 }
1997
Paul Duffina3fb67d2020-05-20 14:20:02 +01001998 return module.selectHeaderJarsForSdkVersion(ctx, sdkVersion)
Paul Duffin56d44902020-01-31 13:36:25 +00001999}
2000
Colin Cross79c7c262019-04-17 11:11:46 -07002001// to satisfy SdkLibraryDependency interface
Paul Duffinf642a312020-06-12 17:46:39 +01002002func (module *SdkLibraryImport) SdkHeaderJars(ctx android.BaseModuleContext, sdkVersion sdkSpec) android.Paths {
Colin Cross79c7c262019-04-17 11:11:46 -07002003 // This module is just a wrapper for the prebuilt stubs.
Paul Duffinf642a312020-06-12 17:46:39 +01002004 return module.sdkJars(ctx, sdkVersion, true)
Colin Cross79c7c262019-04-17 11:11:46 -07002005}
2006
2007// to satisfy SdkLibraryDependency interface
Paul Duffinf642a312020-06-12 17:46:39 +01002008func (module *SdkLibraryImport) SdkImplementationJars(ctx android.BaseModuleContext, sdkVersion sdkSpec) android.Paths {
Colin Cross79c7c262019-04-17 11:11:46 -07002009 // This module is just a wrapper for the stubs.
Paul Duffinf642a312020-06-12 17:46:39 +01002010 return module.sdkJars(ctx, sdkVersion, false)
2011}
2012
2013// to satisfy apex.javaDependency interface
2014func (module *SdkLibraryImport) DexJar() android.Path {
2015 if module.implLibraryModule == nil {
2016 return nil
2017 } else {
2018 return module.implLibraryModule.DexJar()
2019 }
2020}
2021
2022// to satisfy apex.javaDependency interface
2023func (module *SdkLibraryImport) JacocoReportClassesFile() android.Path {
2024 if module.implLibraryModule == nil {
2025 return nil
2026 } else {
2027 return module.implLibraryModule.JacocoReportClassesFile()
2028 }
2029}
2030
2031// to satisfy apex.javaDependency interface
Colin Cross5bc17442020-07-21 20:31:17 -07002032func (module *SdkLibraryImport) LintDepSets() LintDepSets {
2033 if module.implLibraryModule == nil {
2034 return LintDepSets{}
2035 } else {
2036 return module.implLibraryModule.LintDepSets()
2037 }
2038}
2039
2040// to satisfy apex.javaDependency interface
Paul Duffinf642a312020-06-12 17:46:39 +01002041func (module *SdkLibraryImport) Stem() string {
2042 return module.BaseModuleName()
Colin Cross79c7c262019-04-17 11:11:46 -07002043}
Jiyong Parke3833882020-02-17 17:28:10 +09002044
Paul Duffin9ee66da2020-06-17 16:59:43 +01002045var _ ApexDependency = (*SdkLibraryImport)(nil)
2046
2047// to satisfy java.ApexDependency interface
2048func (module *SdkLibraryImport) HeaderJars() android.Paths {
2049 if module.implLibraryModule == nil {
2050 return nil
2051 } else {
2052 return module.implLibraryModule.HeaderJars()
2053 }
2054}
2055
2056// to satisfy java.ApexDependency interface
2057func (module *SdkLibraryImport) ImplementationAndResourcesJars() android.Paths {
2058 if module.implLibraryModule == nil {
2059 return nil
2060 } else {
2061 return module.implLibraryModule.ImplementationAndResourcesJars()
2062 }
2063}
2064
Jiyong Parke3833882020-02-17 17:28:10 +09002065//
2066// java_sdk_library_xml
2067//
2068type sdkLibraryXml struct {
2069 android.ModuleBase
2070 android.DefaultableModuleBase
2071 android.ApexModuleBase
2072
2073 properties sdkLibraryXmlProperties
2074
2075 outputFilePath android.OutputPath
2076 installDirPath android.InstallPath
2077}
2078
2079type sdkLibraryXmlProperties struct {
2080 // canonical name of the lib
2081 Lib_name *string
2082}
2083
2084// java_sdk_library_xml builds the permission xml file for a java_sdk_library.
2085// Not to be used directly by users. java_sdk_library internally uses this.
2086func sdkLibraryXmlFactory() android.Module {
2087 module := &sdkLibraryXml{}
2088
2089 module.AddProperties(&module.properties)
2090
2091 android.InitApexModule(module)
2092 android.InitAndroidArchModule(module, android.DeviceSupported, android.MultilibCommon)
2093
2094 return module
2095}
2096
Colin Cross274a72d2020-08-11 12:17:01 -07002097func (module *sdkLibraryXml) UniqueApexVariations() bool {
2098 // sdkLibraryXml needs a unique variation per APEX because the generated XML file contains the path to the
2099 // mounted APEX, which contains the name of the APEX.
2100 return true
2101}
2102
Jiyong Parke3833882020-02-17 17:28:10 +09002103// from android.PrebuiltEtcModule
2104func (module *sdkLibraryXml) SubDir() string {
2105 return "permissions"
2106}
2107
2108// from android.PrebuiltEtcModule
2109func (module *sdkLibraryXml) OutputFile() android.OutputPath {
2110 return module.outputFilePath
2111}
2112
2113// from android.ApexModule
2114func (module *sdkLibraryXml) AvailableFor(what string) bool {
2115 return true
2116}
2117
2118func (module *sdkLibraryXml) DepsMutator(ctx android.BottomUpMutatorContext) {
2119 // do nothing
2120}
2121
2122// File path to the runtime implementation library
2123func (module *sdkLibraryXml) implPath() string {
2124 implName := proptools.String(module.properties.Lib_name)
Colin Cross74385712020-08-13 11:24:56 -07002125 if apexName := module.ApexVariationName(); apexName != "" {
2126 // TODO(b/146468504): ApexVariationName() is only a soong module name, not apex name.
Jiyong Parke3833882020-02-17 17:28:10 +09002127 // In most cases, this works fine. But when apex_name is set or override_apex is used
2128 // this can be wrong.
2129 return fmt.Sprintf("/apex/%s/javalib/%s.jar", apexName, implName)
2130 }
2131 partition := "system"
2132 if module.SocSpecific() {
2133 partition = "vendor"
2134 } else if module.DeviceSpecific() {
2135 partition = "odm"
2136 } else if module.ProductSpecific() {
2137 partition = "product"
2138 } else if module.SystemExtSpecific() {
2139 partition = "system_ext"
2140 }
2141 return "/" + partition + "/framework/" + implName + ".jar"
2142}
2143
2144func (module *sdkLibraryXml) GenerateAndroidBuildActions(ctx android.ModuleContext) {
2145 libName := proptools.String(module.properties.Lib_name)
2146 xmlContent := fmt.Sprintf(permissionsTemplate, libName, module.implPath())
2147
2148 module.outputFilePath = android.PathForModuleOut(ctx, libName+".xml").OutputPath
2149 rule := android.NewRuleBuilder()
2150 rule.Command().
2151 Text("/bin/bash -c \"echo -e '" + xmlContent + "'\" > ").
2152 Output(module.outputFilePath)
2153
2154 rule.Build(pctx, ctx, "java_sdk_xml", "Permission XML")
2155
2156 module.installDirPath = android.PathForModuleInstall(ctx, "etc", module.SubDir())
2157}
2158
2159func (module *sdkLibraryXml) AndroidMkEntries() []android.AndroidMkEntries {
2160 if !module.IsForPlatform() {
2161 return []android.AndroidMkEntries{android.AndroidMkEntries{
2162 Disabled: true,
2163 }}
2164 }
2165
2166 return []android.AndroidMkEntries{android.AndroidMkEntries{
2167 Class: "ETC",
2168 OutputFile: android.OptionalPathForPath(module.outputFilePath),
2169 ExtraEntries: []android.AndroidMkExtraEntriesFunc{
2170 func(entries *android.AndroidMkEntries) {
2171 entries.SetString("LOCAL_MODULE_TAGS", "optional")
2172 entries.SetString("LOCAL_MODULE_PATH", module.installDirPath.ToMakePath().String())
2173 entries.SetString("LOCAL_INSTALLED_MODULE_STEM", module.outputFilePath.Base())
2174 },
2175 },
2176 }}
2177}
Paul Duffin61871622020-02-10 13:37:10 +00002178
2179type sdkLibrarySdkMemberType struct {
2180 android.SdkMemberTypeBase
2181}
2182
2183func (s *sdkLibrarySdkMemberType) AddDependencies(mctx android.BottomUpMutatorContext, dependencyTag blueprint.DependencyTag, names []string) {
2184 mctx.AddVariationDependencies(nil, dependencyTag, names...)
2185}
2186
2187func (s *sdkLibrarySdkMemberType) IsInstance(module android.Module) bool {
2188 _, ok := module.(*SdkLibrary)
2189 return ok
2190}
2191
2192func (s *sdkLibrarySdkMemberType) AddPrebuiltModule(ctx android.SdkMemberContext, member android.SdkMember) android.BpModule {
2193 return ctx.SnapshotBuilder().AddPrebuiltModule(member, "java_sdk_library_import")
2194}
2195
2196func (s *sdkLibrarySdkMemberType) CreateVariantPropertiesStruct() android.SdkMemberProperties {
2197 return &sdkLibrarySdkMemberProperties{}
2198}
2199
2200type sdkLibrarySdkMemberProperties struct {
2201 android.SdkMemberPropertiesBase
2202
2203 // Scope to per scope properties.
2204 Scopes map[*apiScope]scopeProperties
2205
2206 // Additional libraries that the exported stubs libraries depend upon.
2207 Libs []string
Paul Duffinf488ef22020-04-09 00:10:17 +01002208
2209 // The Java stubs source files.
2210 Stub_srcs []string
Paul Duffinf8e08b22020-05-13 16:54:55 +01002211
2212 // The naming scheme.
2213 Naming_scheme *string
Paul Duffina84756c2020-05-26 20:57:10 +01002214
2215 // True if the java_sdk_library_import is for a shared library, false
2216 // otherwise.
2217 Shared_library *bool
Paul Duffin61871622020-02-10 13:37:10 +00002218}
2219
2220type scopeProperties struct {
Paul Duffin75dcc802020-04-09 01:08:11 +01002221 Jars android.Paths
2222 StubsSrcJar android.Path
2223 CurrentApiFile android.Path
2224 RemovedApiFile android.Path
2225 SdkVersion string
Paul Duffin61871622020-02-10 13:37:10 +00002226}
2227
2228func (s *sdkLibrarySdkMemberProperties) PopulateFromVariant(ctx android.SdkMemberContext, variant android.Module) {
2229 sdk := variant.(*SdkLibrary)
2230
2231 s.Scopes = make(map[*apiScope]scopeProperties)
2232 for _, apiScope := range allApiScopes {
Paul Duffin5ae30792020-05-20 11:52:25 +01002233 paths := sdk.findScopePaths(apiScope)
2234 if paths == nil {
2235 continue
2236 }
2237
Paul Duffin61871622020-02-10 13:37:10 +00002238 jars := paths.stubsImplPath
2239 if len(jars) > 0 {
2240 properties := scopeProperties{}
2241 properties.Jars = jars
Paul Duffin153501f2020-05-12 15:52:55 +01002242 properties.SdkVersion = sdk.sdkVersionForStubsLibrary(ctx.SdkModuleContext(), apiScope)
Paul Duffin533f9c72020-05-20 16:18:00 +01002243 properties.StubsSrcJar = paths.stubsSrcJar.Path()
Paul Duffin86672f62020-06-19 18:39:55 +01002244 if paths.currentApiFilePath.Valid() {
2245 properties.CurrentApiFile = paths.currentApiFilePath.Path()
2246 }
2247 if paths.removedApiFilePath.Valid() {
2248 properties.RemovedApiFile = paths.removedApiFilePath.Path()
2249 }
Paul Duffin61871622020-02-10 13:37:10 +00002250 s.Scopes[apiScope] = properties
2251 }
2252 }
2253
2254 s.Libs = sdk.properties.Libs
Paul Duffind11e78e2020-05-15 20:37:11 +01002255 s.Naming_scheme = sdk.commonSdkLibraryProperties.Naming_scheme
Paul Duffina84756c2020-05-26 20:57:10 +01002256 s.Shared_library = proptools.BoolPtr(sdk.sharedLibrary())
Paul Duffin61871622020-02-10 13:37:10 +00002257}
2258
2259func (s *sdkLibrarySdkMemberProperties) AddToPropertySet(ctx android.SdkMemberContext, propertySet android.BpPropertySet) {
Paul Duffinf8e08b22020-05-13 16:54:55 +01002260 if s.Naming_scheme != nil {
2261 propertySet.AddProperty("naming_scheme", proptools.String(s.Naming_scheme))
2262 }
Paul Duffina84756c2020-05-26 20:57:10 +01002263 if s.Shared_library != nil {
2264 propertySet.AddProperty("shared_library", *s.Shared_library)
2265 }
Paul Duffinf8e08b22020-05-13 16:54:55 +01002266
Paul Duffin61871622020-02-10 13:37:10 +00002267 for _, apiScope := range allApiScopes {
2268 if properties, ok := s.Scopes[apiScope]; ok {
Paul Duffin0f270632020-05-13 19:19:49 +01002269 scopeSet := propertySet.AddPropertySet(apiScope.propertyName)
Paul Duffin61871622020-02-10 13:37:10 +00002270
Paul Duffinf488ef22020-04-09 00:10:17 +01002271 scopeDir := filepath.Join("sdk_library", s.OsPrefix(), apiScope.name)
2272
Paul Duffin61871622020-02-10 13:37:10 +00002273 var jars []string
2274 for _, p := range properties.Jars {
Paul Duffinf488ef22020-04-09 00:10:17 +01002275 dest := filepath.Join(scopeDir, ctx.Name()+"-stubs.jar")
Paul Duffin61871622020-02-10 13:37:10 +00002276 ctx.SnapshotBuilder().CopyToSnapshot(p, dest)
2277 jars = append(jars, dest)
2278 }
2279 scopeSet.AddProperty("jars", jars)
2280
Paul Duffinf488ef22020-04-09 00:10:17 +01002281 // Merge the stubs source jar into the snapshot zip so that when it is unpacked
2282 // the source files are also unpacked.
2283 snapshotRelativeDir := filepath.Join(scopeDir, ctx.Name()+"_stub_sources")
2284 ctx.SnapshotBuilder().UnzipToSnapshot(properties.StubsSrcJar, snapshotRelativeDir)
2285 scopeSet.AddProperty("stub_srcs", []string{snapshotRelativeDir})
2286
Paul Duffin75dcc802020-04-09 01:08:11 +01002287 if properties.CurrentApiFile != nil {
2288 currentApiSnapshotPath := filepath.Join(scopeDir, ctx.Name()+".txt")
2289 ctx.SnapshotBuilder().CopyToSnapshot(properties.CurrentApiFile, currentApiSnapshotPath)
2290 scopeSet.AddProperty("current_api", currentApiSnapshotPath)
2291 }
2292
2293 if properties.RemovedApiFile != nil {
2294 removedApiSnapshotPath := filepath.Join(scopeDir, ctx.Name()+"-removed.txt")
Paul Duffinb1787352020-06-02 13:00:02 +01002295 ctx.SnapshotBuilder().CopyToSnapshot(properties.RemovedApiFile, removedApiSnapshotPath)
Paul Duffin75dcc802020-04-09 01:08:11 +01002296 scopeSet.AddProperty("removed_api", removedApiSnapshotPath)
2297 }
2298
Paul Duffin61871622020-02-10 13:37:10 +00002299 if properties.SdkVersion != "" {
2300 scopeSet.AddProperty("sdk_version", properties.SdkVersion)
2301 }
2302 }
2303 }
2304
2305 if len(s.Libs) > 0 {
2306 propertySet.AddPropertyWithTag("libs", s.Libs, ctx.SnapshotBuilder().SdkMemberReferencePropertyTag(false))
2307 }
2308}