blob: 2d0ae0a32a03c5c59cac4da5c6d25aecea88aa7e [file] [log] [blame]
Jiyong Parkc678ad32018-04-10 13:07:10 +09001// Copyright 2018 Google Inc. All rights reserved.
2//
3// Licensed under the Apache License, Version 2.0 (the "License");
4// you may not use this file except in compliance with the License.
5// You may obtain a copy of the License at
6//
7// http://www.apache.org/licenses/LICENSE-2.0
8//
9// Unless required by applicable law or agreed to in writing, software
10// distributed under the License is distributed on an "AS IS" BASIS,
11// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12// See the License for the specific language governing permissions and
13// limitations under the License.
14
15package java
16
17import (
Jiyong Parkc678ad32018-04-10 13:07:10 +090018 "fmt"
19 "path"
Sundong Ahn054b19a2018-10-19 13:46:09 +090020 "path/filepath"
Paul Duffin46a26a82020-04-07 19:27:04 +010021 "reflect"
Paul Duffin46dc45a2020-05-14 15:39:10 +010022 "regexp"
Jiyong Park82484c02018-04-23 21:41:26 +090023 "sort"
Jiyong Parkc678ad32018-04-10 13:07:10 +090024 "strings"
Jiyong Park82484c02018-04-23 21:41:26 +090025 "sync"
Jiyong Parkc678ad32018-04-10 13:07:10 +090026
Paul Duffind1b3a922020-01-22 11:57:20 +000027 "github.com/google/blueprint"
Jiyong Parkc678ad32018-04-10 13:07:10 +090028 "github.com/google/blueprint/proptools"
Paul Duffin46a26a82020-04-07 19:27:04 +010029
30 "android/soong/android"
Ulya Trafimovichdbf31662020-12-17 12:07:54 +000031 "android/soong/dexpreopt"
Jiyong Parkc678ad32018-04-10 13:07:10 +090032)
33
Jooyung Han58f26ab2019-12-18 15:34:32 +090034const (
Paul Duffindd9d0742020-05-08 15:52:37 +010035 sdkXmlFileSuffix = ".xml"
36 permissionsTemplate = `<?xml version=\"1.0\" encoding=\"utf-8\"?>\n` +
Jooyung Han624058e2019-12-24 18:38:06 +090037 `<!-- Copyright (C) 2018 The Android Open Source Project\n` +
38 `\n` +
Jiyong Parke3833882020-02-17 17:28:10 +090039 ` Licensed under the Apache License, Version 2.0 (the \"License\");\n` +
Jooyung Han624058e2019-12-24 18:38:06 +090040 ` you may not use this file except in compliance with the License.\n` +
41 ` You may obtain a copy of the License at\n` +
42 `\n` +
43 ` http://www.apache.org/licenses/LICENSE-2.0\n` +
44 `\n` +
45 ` Unless required by applicable law or agreed to in writing, software\n` +
Jiyong Parke3833882020-02-17 17:28:10 +090046 ` distributed under the License is distributed on an \"AS IS\" BASIS,\n` +
Jooyung Han624058e2019-12-24 18:38:06 +090047 ` WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\n` +
48 ` See the License for the specific language governing permissions and\n` +
49 ` limitations under the License.\n` +
50 `-->\n` +
51 `<permissions>\n` +
Jiyong Parke3833882020-02-17 17:28:10 +090052 ` <library name=\"%s\" file=\"%s\"/>\n` +
Jooyung Han624058e2019-12-24 18:38:06 +090053 `</permissions>\n`
Jiyong Parkc678ad32018-04-10 13:07:10 +090054)
55
Paul Duffind1b3a922020-01-22 11:57:20 +000056// A tag to associated a dependency with a specific api scope.
57type scopeDependencyTag struct {
58 blueprint.BaseDependencyTag
59 name string
60 apiScope *apiScope
Paul Duffinc8782502020-04-29 20:45:27 +010061
62 // Function for extracting appropriate path information from the dependency.
63 depInfoExtractor func(paths *scopePaths, dep android.Module) error
64}
65
66// Extract tag specific information from the dependency.
67func (tag scopeDependencyTag) extractDepInfo(ctx android.ModuleContext, dep android.Module, paths *scopePaths) {
68 err := tag.depInfoExtractor(paths, dep)
69 if err != nil {
70 ctx.ModuleErrorf("has an invalid {scopeDependencyTag: %s} dependency on module %s: %s", tag.name, ctx.OtherModuleName(dep), err.Error())
71 }
Paul Duffind1b3a922020-01-22 11:57:20 +000072}
73
Paul Duffin80342d72020-06-26 22:08:43 +010074var _ android.ReplaceSourceWithPrebuilt = (*scopeDependencyTag)(nil)
75
76func (tag scopeDependencyTag) ReplaceSourceWithPrebuilt() bool {
77 return false
78}
79
Paul Duffind1b3a922020-01-22 11:57:20 +000080// Provides information about an api scope, e.g. public, system, test.
81type apiScope struct {
82 // The name of the api scope, e.g. public, system, test
83 name string
84
Paul Duffin97b53b82020-05-05 14:40:52 +010085 // The api scope that this scope extends.
86 extends *apiScope
87
Paul Duffin3375e352020-04-28 10:44:03 +010088 // The legacy enabled status for a specific scope can be dependent on other
89 // properties that have been specified on the library so it is provided by
90 // a function that can determine the status by examining those properties.
91 legacyEnabledStatus func(module *SdkLibrary) bool
92
93 // The default enabled status for non-legacy behavior, which is triggered by
94 // explicitly enabling at least one api scope.
95 defaultEnabledStatus bool
96
97 // Gets a pointer to the scope specific properties.
98 scopeSpecificProperties func(module *SdkLibrary) *ApiScopeProperties
99
Paul Duffin46a26a82020-04-07 19:27:04 +0100100 // The name of the field in the dynamically created structure.
101 fieldName string
102
Paul Duffin6b836ba2020-05-13 19:19:49 +0100103 // The name of the property in the java_sdk_library_import
104 propertyName string
105
Paul Duffind1b3a922020-01-22 11:57:20 +0000106 // The tag to use to depend on the stubs library module.
107 stubsTag scopeDependencyTag
108
Paul Duffin0ff08bd2020-04-29 13:30:54 +0100109 // The tag to use to depend on the stubs source module (if separate from the API module).
110 stubsSourceTag scopeDependencyTag
111
112 // The tag to use to depend on the API file generating module (if separate from the stubs source module).
113 apiFileTag scopeDependencyTag
114
Paul Duffinc8782502020-04-29 20:45:27 +0100115 // The tag to use to depend on the stubs source and API module.
116 stubsSourceAndApiTag scopeDependencyTag
Paul Duffind1b3a922020-01-22 11:57:20 +0000117
118 // The scope specific prefix to add to the api file base of "current.txt" or "removed.txt".
119 apiFilePrefix string
120
121 // The scope specific prefix to add to the sdk library module name to construct a scope specific
122 // module name.
123 moduleSuffix string
124
Paul Duffind1b3a922020-01-22 11:57:20 +0000125 // SDK version that the stubs library is built against. Note that this is always
126 // *current. Older stubs library built with a numbered SDK version is created from
127 // the prebuilt jar.
128 sdkVersion string
Paul Duffin1fb487d2020-04-07 18:50:10 +0100129
Paul Duffin15f34ef2020-07-20 18:04:44 +0100130 // The annotation that identifies this API level, empty for the public API scope.
131 annotation string
132
Paul Duffin1fb487d2020-04-07 18:50:10 +0100133 // Extra arguments to pass to droidstubs for this scope.
Paul Duffin0ff08bd2020-04-29 13:30:54 +0100134 //
Paul Duffin15f34ef2020-07-20 18:04:44 +0100135 // This is not used directly but is used to construct the droidstubsArgs.
136 extraArgs []string
Paul Duffin0ff08bd2020-04-29 13:30:54 +0100137
Paul Duffin15f34ef2020-07-20 18:04:44 +0100138 // The args that must be passed to droidstubs to generate the API and stubs source
139 // for this scope, constructed dynamically by initApiScope().
Paul Duffin0ff08bd2020-04-29 13:30:54 +0100140 //
141 // The API only includes the additional members that this scope adds over the scope
142 // that it extends.
Paul Duffin15f34ef2020-07-20 18:04:44 +0100143 //
144 // The stubs source must include the definitions of everything that is in this
145 // api scope and all the scopes that this one extends.
146 droidstubsArgs []string
Paul Duffin0ff08bd2020-04-29 13:30:54 +0100147
Anton Hansson6478ac12020-05-02 11:19:36 +0100148 // Whether the api scope can be treated as unstable, and should skip compat checks.
149 unstable bool
Paul Duffind1b3a922020-01-22 11:57:20 +0000150}
151
152// Initialize a scope, creating and adding appropriate dependency tags
153func initApiScope(scope *apiScope) *apiScope {
Paul Duffinc8782502020-04-29 20:45:27 +0100154 name := scope.name
Paul Duffin46dc45a2020-05-14 15:39:10 +0100155 scopeByName[name] = scope
156 allScopeNames = append(allScopeNames, name)
Paul Duffin6b836ba2020-05-13 19:19:49 +0100157 scope.propertyName = strings.ReplaceAll(name, "-", "_")
158 scope.fieldName = proptools.FieldNameForProperty(scope.propertyName)
Paul Duffind1b3a922020-01-22 11:57:20 +0000159 scope.stubsTag = scopeDependencyTag{
Paul Duffinc8782502020-04-29 20:45:27 +0100160 name: name + "-stubs",
161 apiScope: scope,
162 depInfoExtractor: (*scopePaths).extractStubsLibraryInfoFromDependency,
Paul Duffind1b3a922020-01-22 11:57:20 +0000163 }
Paul Duffin0ff08bd2020-04-29 13:30:54 +0100164 scope.stubsSourceTag = scopeDependencyTag{
165 name: name + "-stubs-source",
166 apiScope: scope,
167 depInfoExtractor: (*scopePaths).extractStubsSourceInfoFromDep,
168 }
169 scope.apiFileTag = scopeDependencyTag{
170 name: name + "-api",
171 apiScope: scope,
172 depInfoExtractor: (*scopePaths).extractApiInfoFromDep,
173 }
Paul Duffinc8782502020-04-29 20:45:27 +0100174 scope.stubsSourceAndApiTag = scopeDependencyTag{
175 name: name + "-stubs-source-and-api",
176 apiScope: scope,
177 depInfoExtractor: (*scopePaths).extractStubsSourceAndApiInfoFromApiStubsProvider,
Paul Duffind1b3a922020-01-22 11:57:20 +0000178 }
Paul Duffin0ff08bd2020-04-29 13:30:54 +0100179
180 // To get the args needed to generate the stubs source append all the args from
181 // this scope and all the scopes it extends as each set of args adds additional
182 // members to the stubs.
Paul Duffin15f34ef2020-07-20 18:04:44 +0100183 var scopeSpecificArgs []string
184 if scope.annotation != "" {
185 scopeSpecificArgs = []string{"--show-annotation", scope.annotation}
Paul Duffin0ff08bd2020-04-29 13:30:54 +0100186 }
Paul Duffin15f34ef2020-07-20 18:04:44 +0100187 for s := scope; s != nil; s = s.extends {
188 scopeSpecificArgs = append(scopeSpecificArgs, s.extraArgs...)
Paul Duffin0ff08bd2020-04-29 13:30:54 +0100189
Paul Duffin15f34ef2020-07-20 18:04:44 +0100190 // Ensure that the generated stubs includes all the API elements from the API scope
191 // that this scope extends.
192 if s != scope && s.annotation != "" {
193 scopeSpecificArgs = append(scopeSpecificArgs, "--show-for-stub-purposes-annotation", s.annotation)
194 }
195 }
Paul Duffin0ff08bd2020-04-29 13:30:54 +0100196
Paul Duffin15f34ef2020-07-20 18:04:44 +0100197 // Escape any special characters in the arguments. This is needed because droidstubs
198 // passes these directly to the shell command.
199 scope.droidstubsArgs = proptools.ShellEscapeList(scopeSpecificArgs)
Paul Duffin0ff08bd2020-04-29 13:30:54 +0100200
Paul Duffind1b3a922020-01-22 11:57:20 +0000201 return scope
202}
203
Paul Duffinc3091c82020-05-08 14:16:20 +0100204func (scope *apiScope) stubsLibraryModuleName(baseName string) string {
Paul Duffindd9d0742020-05-08 15:52:37 +0100205 return baseName + ".stubs" + scope.moduleSuffix
Paul Duffind1b3a922020-01-22 11:57:20 +0000206}
207
Paul Duffinc8782502020-04-29 20:45:27 +0100208func (scope *apiScope) stubsSourceModuleName(baseName string) string {
Paul Duffindd9d0742020-05-08 15:52:37 +0100209 return baseName + ".stubs.source" + scope.moduleSuffix
Paul Duffind1b3a922020-01-22 11:57:20 +0000210}
211
Paul Duffin0ff08bd2020-04-29 13:30:54 +0100212func (scope *apiScope) apiModuleName(baseName string) string {
Paul Duffindd9d0742020-05-08 15:52:37 +0100213 return baseName + ".api" + scope.moduleSuffix
Paul Duffin0ff08bd2020-04-29 13:30:54 +0100214}
215
Paul Duffin3375e352020-04-28 10:44:03 +0100216func (scope *apiScope) String() string {
217 return scope.name
218}
219
Paul Duffind1b3a922020-01-22 11:57:20 +0000220type apiScopes []*apiScope
221
222func (scopes apiScopes) Strings(accessor func(*apiScope) string) []string {
223 var list []string
224 for _, scope := range scopes {
225 list = append(list, accessor(scope))
226 }
227 return list
228}
229
Jiyong Parkc678ad32018-04-10 13:07:10 +0900230var (
Paul Duffin46dc45a2020-05-14 15:39:10 +0100231 scopeByName = make(map[string]*apiScope)
232 allScopeNames []string
Paul Duffind1b3a922020-01-22 11:57:20 +0000233 apiScopePublic = initApiScope(&apiScope{
Paul Duffin3375e352020-04-28 10:44:03 +0100234 name: "public",
235
236 // Public scope is enabled by default for both legacy and non-legacy modes.
237 legacyEnabledStatus: func(module *SdkLibrary) bool {
238 return true
239 },
240 defaultEnabledStatus: true,
241
242 scopeSpecificProperties: func(module *SdkLibrary) *ApiScopeProperties {
243 return &module.sdkLibraryProperties.Public
244 },
Paul Duffind1b3a922020-01-22 11:57:20 +0000245 sdkVersion: "current",
246 })
247 apiScopeSystem = initApiScope(&apiScope{
Paul Duffin3375e352020-04-28 10:44:03 +0100248 name: "system",
249 extends: apiScopePublic,
250 legacyEnabledStatus: (*SdkLibrary).generateTestAndSystemScopesByDefault,
251 scopeSpecificProperties: func(module *SdkLibrary) *ApiScopeProperties {
252 return &module.sdkLibraryProperties.System
253 },
Paul Duffin15f34ef2020-07-20 18:04:44 +0100254 apiFilePrefix: "system-",
255 moduleSuffix: ".system",
256 sdkVersion: "system_current",
257 annotation: "android.annotation.SystemApi(client=android.annotation.SystemApi.Client.PRIVILEGED_APPS)",
Paul Duffind1b3a922020-01-22 11:57:20 +0000258 })
259 apiScopeTest = initApiScope(&apiScope{
Paul Duffin3375e352020-04-28 10:44:03 +0100260 name: "test",
Anton Hansson4fe970f2020-10-09 10:16:49 +0100261 extends: apiScopeSystem,
Paul Duffin3375e352020-04-28 10:44:03 +0100262 legacyEnabledStatus: (*SdkLibrary).generateTestAndSystemScopesByDefault,
263 scopeSpecificProperties: func(module *SdkLibrary) *ApiScopeProperties {
264 return &module.sdkLibraryProperties.Test
265 },
Paul Duffin15f34ef2020-07-20 18:04:44 +0100266 apiFilePrefix: "test-",
267 moduleSuffix: ".test",
268 sdkVersion: "test_current",
269 annotation: "android.annotation.TestApi",
270 unstable: true,
Paul Duffind1b3a922020-01-22 11:57:20 +0000271 })
Paul Duffin8f265b92020-04-28 14:13:56 +0100272 apiScopeModuleLib = initApiScope(&apiScope{
Paul Duffin6b836ba2020-05-13 19:19:49 +0100273 name: "module-lib",
Paul Duffin8f265b92020-04-28 14:13:56 +0100274 extends: apiScopeSystem,
Paul Duffin0c5bae52020-06-02 13:00:08 +0100275 // The module-lib scope is disabled by default in legacy mode.
Paul Duffin8f265b92020-04-28 14:13:56 +0100276 //
277 // Enabling this would break existing usages.
278 legacyEnabledStatus: func(module *SdkLibrary) bool {
279 return false
280 },
281 scopeSpecificProperties: func(module *SdkLibrary) *ApiScopeProperties {
282 return &module.sdkLibraryProperties.Module_lib
283 },
284 apiFilePrefix: "module-lib-",
285 moduleSuffix: ".module_lib",
286 sdkVersion: "module_current",
Paul Duffin15f34ef2020-07-20 18:04:44 +0100287 annotation: "android.annotation.SystemApi(client=android.annotation.SystemApi.Client.MODULE_LIBRARIES)",
Paul Duffin8f265b92020-04-28 14:13:56 +0100288 })
Paul Duffin0c5bae52020-06-02 13:00:08 +0100289 apiScopeSystemServer = initApiScope(&apiScope{
290 name: "system-server",
291 extends: apiScopePublic,
292 // The system-server scope is disabled by default in legacy mode.
293 //
294 // Enabling this would break existing usages.
295 legacyEnabledStatus: func(module *SdkLibrary) bool {
296 return false
297 },
298 scopeSpecificProperties: func(module *SdkLibrary) *ApiScopeProperties {
299 return &module.sdkLibraryProperties.System_server
300 },
301 apiFilePrefix: "system-server-",
302 moduleSuffix: ".system_server",
303 sdkVersion: "system_server_current",
Paul Duffin15f34ef2020-07-20 18:04:44 +0100304 annotation: "android.annotation.SystemApi(client=android.annotation.SystemApi.Client.SYSTEM_SERVER)",
305 extraArgs: []string{
306 "--hide-annotation", "android.annotation.Hide",
Paul Duffin0c5bae52020-06-02 13:00:08 +0100307 // com.android.* classes are okay in this interface"
Paul Duffin15f34ef2020-07-20 18:04:44 +0100308 "--hide", "InternalClasses",
Paul Duffin0c5bae52020-06-02 13:00:08 +0100309 },
310 })
Paul Duffind1b3a922020-01-22 11:57:20 +0000311 allApiScopes = apiScopes{
312 apiScopePublic,
313 apiScopeSystem,
314 apiScopeTest,
Paul Duffin8f265b92020-04-28 14:13:56 +0100315 apiScopeModuleLib,
Paul Duffin0c5bae52020-06-02 13:00:08 +0100316 apiScopeSystemServer,
Paul Duffind1b3a922020-01-22 11:57:20 +0000317 }
Jiyong Parkc678ad32018-04-10 13:07:10 +0900318)
319
Jiyong Park82484c02018-04-23 21:41:26 +0900320var (
321 javaSdkLibrariesLock sync.Mutex
322)
323
Jiyong Parkc678ad32018-04-10 13:07:10 +0900324// TODO: these are big features that are currently missing
Jiyong Park1be96912018-05-28 18:02:19 +0900325// 1) disallowing linking to the runtime shared lib
326// 2) HTML generation
Jiyong Parkc678ad32018-04-10 13:07:10 +0900327
328func init() {
Paul Duffin43dc1cc2019-12-19 11:18:54 +0000329 RegisterSdkLibraryBuildComponents(android.InitRegistrationContext)
Jiyong Parkc678ad32018-04-10 13:07:10 +0900330
Jiyong Park82484c02018-04-23 21:41:26 +0900331 android.RegisterMakeVarsProvider(pctx, func(ctx android.MakeVarsContext) {
332 javaSdkLibraries := javaSdkLibraries(ctx.Config())
333 sort.Strings(*javaSdkLibraries)
334 ctx.Strict("JAVA_SDK_LIBRARIES", strings.Join(*javaSdkLibraries, " "))
335 })
Paul Duffindd46f712020-02-10 13:37:10 +0000336
337 // Register sdk member types.
338 android.RegisterSdkMemberType(&sdkLibrarySdkMemberType{
339 android.SdkMemberTypeBase{
340 PropertyName: "java_sdk_libs",
341 SupportsSdk: true,
342 },
343 })
Jiyong Parkc678ad32018-04-10 13:07:10 +0900344}
345
Paul Duffin43dc1cc2019-12-19 11:18:54 +0000346func RegisterSdkLibraryBuildComponents(ctx android.RegistrationContext) {
347 ctx.RegisterModuleType("java_sdk_library", SdkLibraryFactory)
348 ctx.RegisterModuleType("java_sdk_library_import", sdkLibraryImportFactory)
349}
350
Paul Duffin3375e352020-04-28 10:44:03 +0100351// Properties associated with each api scope.
352type ApiScopeProperties struct {
353 // Indicates whether the api surface is generated.
354 //
355 // If this is set for any scope then all scopes must explicitly specify if they
356 // are enabled. This is to prevent new usages from depending on legacy behavior.
357 //
358 // Otherwise, if this is not set for any scope then the default behavior is
359 // scope specific so please refer to the scope specific property documentation.
360 Enabled *bool
Paul Duffin87a05a32020-05-12 11:50:28 +0100361
362 // The sdk_version to use for building the stubs.
363 //
364 // If not specified then it will use an sdk_version determined as follows:
365 // 1) If the sdk_version specified on the java_sdk_library is none then this
366 // will be none. This is used for java_sdk_library instances that are used
367 // to create stubs that contribute to the core_current sdk version.
368 // 2) Otherwise, it is assumed that this library extends but does not contribute
369 // directly to a specific sdk_version and so this uses the sdk_version appropriate
370 // for the api scope. e.g. public will use sdk_version: current, system will use
371 // sdk_version: system_current, etc.
372 //
373 // This does not affect the sdk_version used for either generating the stubs source
374 // or the API file. They both have to use the same sdk_version as is used for
375 // compiling the implementation library.
376 Sdk_version *string
Paul Duffin3375e352020-04-28 10:44:03 +0100377}
378
Jiyong Parkc678ad32018-04-10 13:07:10 +0900379type sdkLibraryProperties struct {
Paul Duffin5df79302020-05-16 15:52:12 +0100380 // Visibility for impl library module. If not specified then defaults to the
381 // visibility property.
382 Impl_library_visibility []string
383
Paul Duffin4911a892020-04-29 23:35:13 +0100384 // Visibility for stubs library modules. If not specified then defaults to the
385 // visibility property.
386 Stubs_library_visibility []string
387
388 // Visibility for stubs source modules. If not specified then defaults to the
389 // visibility property.
390 Stubs_source_visibility []string
391
Anton Hansson7f66efa2020-10-08 14:47:23 +0100392 // List of Java libraries that will be in the classpath when building the implementation lib
393 Impl_only_libs []string `android:"arch_variant"`
394
Sundong Ahnf043cf62018-06-25 16:04:37 +0900395 // List of Java libraries that will be in the classpath when building stubs
396 Stub_only_libs []string `android:"arch_variant"`
397
Paul Duffin7a586d32019-12-30 17:09:34 +0000398 // list of package names that will be documented and publicized as API.
399 // This allows the API to be restricted to a subset of the source files provided.
400 // If this is unspecified then all the source files will be treated as being part
401 // of the API.
Jiyong Parkc678ad32018-04-10 13:07:10 +0900402 Api_packages []string
403
Jiyong Park5a2c9d72018-05-01 22:25:41 +0900404 // list of package names that must be hidden from the API
405 Hidden_api_packages []string
406
Paul Duffin749f98f2019-12-30 17:23:46 +0000407 // the relative path to the directory containing the api specification files.
408 // Defaults to "api".
409 Api_dir *string
410
Paul Duffindfa131e2020-05-15 20:37:11 +0100411 // Determines whether a runtime implementation library is built; defaults to false.
412 //
413 // If true then it also prevents the module from being used as a shared module, i.e.
414 // it is as is shared_library: false, was set.
Paul Duffin43db9be2019-12-30 17:35:49 +0000415 Api_only *bool
416
Paul Duffin11512472019-02-11 15:55:17 +0000417 // local files that are used within user customized droiddoc options.
418 Droiddoc_option_files []string
419
420 // additional droiddoc options
421 // Available variables for substitution:
422 //
423 // $(location <label>): the path to the droiddoc_option_files with name <label>
Sundong Ahndd567f92018-07-31 17:19:11 +0900424 Droiddoc_options []string
425
Paul Duffine22c2ab2020-05-20 19:35:27 +0100426 // is set to true, Metalava will allow framework SDK to contain annotations.
427 Annotations_enabled *bool
428
Sundong Ahn054b19a2018-10-19 13:46:09 +0900429 // a list of top-level directories containing files to merge qualifier annotations
430 // (i.e. those intended to be included in the stubs written) from.
431 Merge_annotations_dirs []string
432
433 // a list of top-level directories containing Java stub files to merge show/hide annotations from.
434 Merge_inclusion_annotations_dirs []string
435
436 // If set to true, the path of dist files is apistubs/core. Defaults to false.
437 Core_lib *bool
438
Paul Duffin4f5c1ef2020-11-19 14:53:43 +0000439 // If set to true then don't create dist rules.
440 No_dist *bool
Sundong Ahn80a87b32019-05-13 15:02:50 +0900441
Paul Duffin31310252020-11-20 21:26:20 +0000442 // The stem for the artifacts that are copied to the dist, if not specified
443 // then defaults to the base module name.
444 //
445 // For each scope the following artifacts are copied to the apistubs/<scope>
446 // directory in the dist.
447 // * stubs impl jar -> <dist-stem>.jar
448 // * API specification file -> api/<dist-stem>.txt
449 // * Removed API specification file -> api/<dist-stem>-removed.txt
450 //
451 // Also used to construct the name of the filegroup (created by prebuilt_apis)
452 // that references the latest released API and remove API specification files.
453 // * API specification filegroup -> <dist-stem>.api.<scope>.latest
454 // * Removed API specification filegroup -> <dist-stem>-removed.api.<scope>.latest
455 Dist_stem *string
456
Anton Hanssondff2c782020-12-21 17:10:01 +0000457 // A compatibility mode that allows historical API-tracking files to not exist.
458 // Do not use.
459 Unsafe_ignore_missing_latest_api bool
460
Paul Duffin3375e352020-04-28 10:44:03 +0100461 // indicates whether system and test apis should be generated.
462 Generate_system_and_test_apis bool `blueprint:"mutated"`
463
464 // The properties specific to the public api scope
465 //
466 // Unless explicitly specified by using public.enabled the public api scope is
467 // enabled by default in both legacy and non-legacy mode.
468 Public ApiScopeProperties
469
470 // The properties specific to the system api scope
471 //
472 // In legacy mode the system api scope is enabled by default when sdk_version
473 // is set to something other than "none".
474 //
475 // In non-legacy mode the system api scope is disabled by default.
476 System ApiScopeProperties
477
478 // The properties specific to the test api scope
479 //
480 // In legacy mode the test api scope is enabled by default when sdk_version
481 // is set to something other than "none".
482 //
483 // In non-legacy mode the test api scope is disabled by default.
484 Test ApiScopeProperties
Paul Duffin37e0b772019-12-30 17:20:10 +0000485
Paul Duffin0c5bae52020-06-02 13:00:08 +0100486 // The properties specific to the module-lib api scope
Paul Duffin8f265b92020-04-28 14:13:56 +0100487 //
Paul Duffin0c5bae52020-06-02 13:00:08 +0100488 // Unless explicitly specified by using test.enabled the module-lib api scope is
Paul Duffin8f265b92020-04-28 14:13:56 +0100489 // disabled by default.
490 Module_lib ApiScopeProperties
491
Paul Duffin0c5bae52020-06-02 13:00:08 +0100492 // The properties specific to the system-server api scope
493 //
494 // Unless explicitly specified by using test.enabled the module-lib api scope is
495 // disabled by default.
496 System_server ApiScopeProperties
497
Jiyong Park932cdfe2020-05-28 00:19:53 +0900498 // Determines if the stubs are preferred over the implementation library
499 // for linking, even when the client doesn't specify sdk_version. When this
500 // is set to true, such clients are provided with the widest API surface that
501 // this lib provides. Note however that this option doesn't affect the clients
502 // that are in the same APEX as this library. In that case, the clients are
503 // always linked with the implementation library. Default is false.
504 Default_to_stubs *bool
505
Paul Duffin160fe412020-05-10 19:32:20 +0100506 // Properties related to api linting.
507 Api_lint struct {
508 // Enable api linting.
509 Enabled *bool
510 }
511
Jiyong Parkc678ad32018-04-10 13:07:10 +0900512 // TODO: determines whether to create HTML doc or not
513 //Html_doc *bool
514}
515
Paul Duffin0f8faff2020-05-20 16:18:00 +0100516// Paths to outputs from java_sdk_library and java_sdk_library_import.
517//
518// Fields that are android.Paths are always set (during GenerateAndroidBuildActions).
519// OptionalPaths are always set by java_sdk_library but may not be set by
520// java_sdk_library_import as not all instances provide that information.
Paul Duffind1b3a922020-01-22 11:57:20 +0000521type scopePaths struct {
Paul Duffin0f8faff2020-05-20 16:18:00 +0100522 // The path (represented as Paths for convenience when returning) to the stubs header jar.
523 //
524 // That is the jar that is created by turbine.
525 stubsHeaderPath android.Paths
526
527 // The path (represented as Paths for convenience when returning) to the stubs implementation jar.
528 //
529 // This is not the implementation jar, it still only contains stubs.
530 stubsImplPath android.Paths
531
532 // The API specification file, e.g. system_current.txt.
533 currentApiFilePath android.OptionalPath
534
535 // The specification of API elements removed since the last release.
536 removedApiFilePath android.OptionalPath
537
538 // The stubs source jar.
539 stubsSrcJar android.OptionalPath
Paul Duffind1b3a922020-01-22 11:57:20 +0000540}
541
Paul Duffinc8782502020-04-29 20:45:27 +0100542func (paths *scopePaths) extractStubsLibraryInfoFromDependency(dep android.Module) error {
543 if lib, ok := dep.(Dependency); ok {
544 paths.stubsHeaderPath = lib.HeaderJars()
545 paths.stubsImplPath = lib.ImplementationJars()
546 return nil
547 } else {
548 return fmt.Errorf("expected module that implements Dependency, e.g. java_library")
549 }
550}
551
Paul Duffin0ff08bd2020-04-29 13:30:54 +0100552func (paths *scopePaths) treatDepAsApiStubsProvider(dep android.Module, action func(provider ApiStubsProvider)) error {
553 if apiStubsProvider, ok := dep.(ApiStubsProvider); ok {
554 action(apiStubsProvider)
Paul Duffinc8782502020-04-29 20:45:27 +0100555 return nil
556 } else {
557 return fmt.Errorf("expected module that implements ApiStubsProvider, e.g. droidstubs")
558 }
559}
560
Paul Duffin0f8faff2020-05-20 16:18:00 +0100561func (paths *scopePaths) treatDepAsApiStubsSrcProvider(dep android.Module, action func(provider ApiStubsSrcProvider)) error {
562 if apiStubsProvider, ok := dep.(ApiStubsSrcProvider); ok {
563 action(apiStubsProvider)
564 return nil
565 } else {
566 return fmt.Errorf("expected module that implements ApiStubsSrcProvider, e.g. droidstubs")
567 }
568}
569
Paul Duffin0ff08bd2020-04-29 13:30:54 +0100570func (paths *scopePaths) extractApiInfoFromApiStubsProvider(provider ApiStubsProvider) {
Paul Duffin0f8faff2020-05-20 16:18:00 +0100571 paths.currentApiFilePath = android.OptionalPathForPath(provider.ApiFilePath())
572 paths.removedApiFilePath = android.OptionalPathForPath(provider.RemovedApiFilePath())
Paul Duffin0ff08bd2020-04-29 13:30:54 +0100573}
574
575func (paths *scopePaths) extractApiInfoFromDep(dep android.Module) error {
576 return paths.treatDepAsApiStubsProvider(dep, func(provider ApiStubsProvider) {
577 paths.extractApiInfoFromApiStubsProvider(provider)
578 })
579}
580
Paul Duffin0f8faff2020-05-20 16:18:00 +0100581func (paths *scopePaths) extractStubsSourceInfoFromApiStubsProviders(provider ApiStubsSrcProvider) {
582 paths.stubsSrcJar = android.OptionalPathForPath(provider.StubsSrcJar())
Paul Duffin0ff08bd2020-04-29 13:30:54 +0100583}
584
585func (paths *scopePaths) extractStubsSourceInfoFromDep(dep android.Module) error {
Paul Duffin0f8faff2020-05-20 16:18:00 +0100586 return paths.treatDepAsApiStubsSrcProvider(dep, func(provider ApiStubsSrcProvider) {
Paul Duffin0ff08bd2020-04-29 13:30:54 +0100587 paths.extractStubsSourceInfoFromApiStubsProviders(provider)
588 })
589}
590
591func (paths *scopePaths) extractStubsSourceAndApiInfoFromApiStubsProvider(dep android.Module) error {
592 return paths.treatDepAsApiStubsProvider(dep, func(provider ApiStubsProvider) {
593 paths.extractApiInfoFromApiStubsProvider(provider)
594 paths.extractStubsSourceInfoFromApiStubsProviders(provider)
595 })
596}
597
598type commonToSdkLibraryAndImportProperties struct {
Paul Duffin1b1e8062020-05-08 13:44:43 +0100599 // The naming scheme to use for the components that this module creates.
600 //
Paul Duffinee9ad5d2020-09-11 13:04:05 +0100601 // If not specified then it defaults to "default".
Paul Duffin1b1e8062020-05-08 13:44:43 +0100602 //
603 // This is a temporary mechanism to simplify conversion from separate modules for each
604 // component that follow a different naming pattern to the default one.
605 //
606 // TODO(b/155480189) - Remove once naming inconsistencies have been resolved.
Paul Duffin0ff08bd2020-04-29 13:30:54 +0100607 Naming_scheme *string
Paul Duffindfa131e2020-05-15 20:37:11 +0100608
609 // Specifies whether this module can be used as an Android shared library; defaults
610 // to true.
611 //
612 // An Android shared library is one that can be referenced in a <uses-library> element
613 // in an AndroidManifest.xml.
614 Shared_library *bool
Paul Duffina2ae7e02020-09-11 11:55:00 +0100615
616 // Files containing information about supported java doc tags.
617 Doctag_files []string `android:"path"`
Paul Duffin0ff08bd2020-04-29 13:30:54 +0100618}
619
Paul Duffin56d44902020-01-31 13:36:25 +0000620// Common code between sdk library and sdk library import
621type commonToSdkLibraryAndImport struct {
Paul Duffinc3091c82020-05-08 14:16:20 +0100622 moduleBase *android.ModuleBase
623
Paul Duffin56d44902020-01-31 13:36:25 +0000624 scopePaths map[*apiScope]*scopePaths
Paul Duffin1b1e8062020-05-08 13:44:43 +0100625
626 namingScheme sdkLibraryComponentNamingScheme
627
Paul Duffindfa131e2020-05-15 20:37:11 +0100628 commonSdkLibraryProperties commonToSdkLibraryAndImportProperties
Paul Duffin859fe962020-05-15 10:20:31 +0100629
Paul Duffina2ae7e02020-09-11 11:55:00 +0100630 // Paths to commonSdkLibraryProperties.Doctag_files
631 doctagPaths android.Paths
632
Paul Duffin859fe962020-05-15 10:20:31 +0100633 // Functionality related to this being used as a component of a java_sdk_library.
634 EmbeddableSdkLibraryComponent
Paul Duffin56d44902020-01-31 13:36:25 +0000635}
636
Paul Duffinc3091c82020-05-08 14:16:20 +0100637func (c *commonToSdkLibraryAndImport) initCommon(moduleBase *android.ModuleBase) {
638 c.moduleBase = moduleBase
Paul Duffin1b1e8062020-05-08 13:44:43 +0100639
Paul Duffindfa131e2020-05-15 20:37:11 +0100640 moduleBase.AddProperties(&c.commonSdkLibraryProperties)
Paul Duffin859fe962020-05-15 10:20:31 +0100641
642 // Initialize this as an sdk library component.
643 c.initSdkLibraryComponent(moduleBase)
Paul Duffin1b1e8062020-05-08 13:44:43 +0100644}
645
646func (c *commonToSdkLibraryAndImport) initCommonAfterDefaultsApplied(ctx android.DefaultableHookContext) bool {
Paul Duffindfa131e2020-05-15 20:37:11 +0100647 schemeProperty := proptools.StringDefault(c.commonSdkLibraryProperties.Naming_scheme, "default")
Paul Duffin1b1e8062020-05-08 13:44:43 +0100648 switch schemeProperty {
649 case "default":
650 c.namingScheme = &defaultNamingScheme{}
651 default:
652 ctx.PropertyErrorf("naming_scheme", "expected 'default' but was %q", schemeProperty)
653 return false
654 }
655
Paul Duffindfa131e2020-05-15 20:37:11 +0100656 // Only track this sdk library if this can be used as a shared library.
657 if c.sharedLibrary() {
658 // Use the name specified in the module definition as the owner.
659 c.sdkLibraryComponentProperties.SdkLibraryToImplicitlyTrack = proptools.StringPtr(c.moduleBase.BaseModuleName())
660 }
Paul Duffin859fe962020-05-15 10:20:31 +0100661
Paul Duffin1b1e8062020-05-08 13:44:43 +0100662 return true
Paul Duffinc3091c82020-05-08 14:16:20 +0100663}
664
Paul Duffina2ae7e02020-09-11 11:55:00 +0100665func (c *commonToSdkLibraryAndImport) generateCommonBuildActions(ctx android.ModuleContext) {
666 c.doctagPaths = android.PathsForModuleSrc(ctx, c.commonSdkLibraryProperties.Doctag_files)
667}
668
Paul Duffineedc5d52020-06-12 17:46:39 +0100669// Module name of the runtime implementation library
670func (c *commonToSdkLibraryAndImport) implLibraryModuleName() string {
671 return c.moduleBase.BaseModuleName() + ".impl"
672}
673
674// Module name of the XML file for the lib
675func (c *commonToSdkLibraryAndImport) xmlPermissionsModuleName() string {
676 return c.moduleBase.BaseModuleName() + sdkXmlFileSuffix
677}
678
Paul Duffinc3091c82020-05-08 14:16:20 +0100679// Name of the java_library module that compiles the stubs source.
680func (c *commonToSdkLibraryAndImport) stubsLibraryModuleName(apiScope *apiScope) string {
Paul Duffin1b1e8062020-05-08 13:44:43 +0100681 return c.namingScheme.stubsLibraryModuleName(apiScope, c.moduleBase.BaseModuleName())
Paul Duffinc3091c82020-05-08 14:16:20 +0100682}
683
684// Name of the droidstubs module that generates the stubs source and may also
685// generate/check the API.
686func (c *commonToSdkLibraryAndImport) stubsSourceModuleName(apiScope *apiScope) string {
Paul Duffin1b1e8062020-05-08 13:44:43 +0100687 return c.namingScheme.stubsSourceModuleName(apiScope, c.moduleBase.BaseModuleName())
Paul Duffinc3091c82020-05-08 14:16:20 +0100688}
689
690// Name of the droidstubs module that generates/checks the API. Only used if it
691// requires different arts to the stubs source generating module.
692func (c *commonToSdkLibraryAndImport) apiModuleName(apiScope *apiScope) string {
Paul Duffin1b1e8062020-05-08 13:44:43 +0100693 return c.namingScheme.apiModuleName(apiScope, c.moduleBase.BaseModuleName())
Paul Duffinc3091c82020-05-08 14:16:20 +0100694}
695
Paul Duffin46dc45a2020-05-14 15:39:10 +0100696// The component names for different outputs of the java_sdk_library.
697//
698// They are similar to the names used for the child modules it creates
699const (
700 stubsSourceComponentName = "stubs.source"
701
702 apiTxtComponentName = "api.txt"
703
704 removedApiTxtComponentName = "removed-api.txt"
705)
706
707// A regular expression to match tags that reference a specific stubs component.
708//
709// It will only match if given a valid scope and a valid component. It is verfy strict
710// to ensure it does not accidentally match a similar looking tag that should be processed
711// by the embedded Library.
712var tagSplitter = func() *regexp.Regexp {
713 // Given a list of literal string items returns a regular expression that will
714 // match any one of the items.
715 choice := func(items ...string) string {
716 return `\Q` + strings.Join(items, `\E|\Q`) + `\E`
717 }
718
719 // Regular expression to match one of the scopes.
720 scopesRegexp := choice(allScopeNames...)
721
722 // Regular expression to match one of the components.
723 componentsRegexp := choice(stubsSourceComponentName, apiTxtComponentName, removedApiTxtComponentName)
724
725 // Regular expression to match any combination of one scope and one component.
726 return regexp.MustCompile(fmt.Sprintf(`^\.(%s)\.(%s)$`, scopesRegexp, componentsRegexp))
727}()
728
729// For OutputFileProducer interface
730//
731// .<scope>.stubs.source
732// .<scope>.api.txt
733// .<scope>.removed-api.txt
734func (c *commonToSdkLibraryAndImport) commonOutputFiles(tag string) (android.Paths, error) {
735 if groups := tagSplitter.FindStringSubmatch(tag); groups != nil {
736 scopeName := groups[1]
737 component := groups[2]
738
739 if scope, ok := scopeByName[scopeName]; ok {
740 paths := c.findScopePaths(scope)
741 if paths == nil {
742 return nil, fmt.Errorf("%q does not provide api scope %s", c.moduleBase.BaseModuleName(), scopeName)
743 }
744
745 switch component {
746 case stubsSourceComponentName:
747 if paths.stubsSrcJar.Valid() {
748 return android.Paths{paths.stubsSrcJar.Path()}, nil
749 }
750
751 case apiTxtComponentName:
752 if paths.currentApiFilePath.Valid() {
753 return android.Paths{paths.currentApiFilePath.Path()}, nil
754 }
755
756 case removedApiTxtComponentName:
757 if paths.removedApiFilePath.Valid() {
758 return android.Paths{paths.removedApiFilePath.Path()}, nil
759 }
760 }
761
762 return nil, fmt.Errorf("%s not available for api scope %s", component, scopeName)
763 } else {
764 return nil, fmt.Errorf("unknown scope %s in %s", scope, tag)
765 }
766
767 } else {
Paul Duffina2ae7e02020-09-11 11:55:00 +0100768 switch tag {
769 case ".doctags":
770 if c.doctagPaths != nil {
771 return c.doctagPaths, nil
772 } else {
773 return nil, fmt.Errorf("no doctag_files specified on %s", c.moduleBase.BaseModuleName())
774 }
775 }
Paul Duffin46dc45a2020-05-14 15:39:10 +0100776 return nil, nil
777 }
778}
779
Paul Duffin803a9562020-05-20 11:52:25 +0100780func (c *commonToSdkLibraryAndImport) getScopePathsCreateIfNeeded(scope *apiScope) *scopePaths {
Paul Duffin56d44902020-01-31 13:36:25 +0000781 if c.scopePaths == nil {
782 c.scopePaths = make(map[*apiScope]*scopePaths)
783 }
784 paths := c.scopePaths[scope]
785 if paths == nil {
786 paths = &scopePaths{}
787 c.scopePaths[scope] = paths
788 }
789
790 return paths
791}
792
Paul Duffin803a9562020-05-20 11:52:25 +0100793func (c *commonToSdkLibraryAndImport) findScopePaths(scope *apiScope) *scopePaths {
794 if c.scopePaths == nil {
795 return nil
796 }
797
798 return c.scopePaths[scope]
799}
800
801// If this does not support the requested api scope then find the closest available
802// scope it does support. Returns nil if no such scope is available.
803func (c *commonToSdkLibraryAndImport) findClosestScopePath(scope *apiScope) *scopePaths {
804 for s := scope; s != nil; s = s.extends {
805 if paths := c.findScopePaths(s); paths != nil {
806 return paths
807 }
808 }
809
810 // This should never happen outside tests as public should be the base scope for every
811 // scope and is enabled by default.
812 return nil
813}
814
Paul Duffin23970f42020-05-20 14:20:02 +0100815func (c *commonToSdkLibraryAndImport) selectHeaderJarsForSdkVersion(ctx android.BaseModuleContext, sdkVersion sdkSpec) android.Paths {
Paul Duffinb05d4292020-05-20 12:19:10 +0100816
817 // If a specific numeric version has been requested then use prebuilt versions of the sdk.
818 if sdkVersion.version.isNumbered() {
819 return PrebuiltJars(ctx, c.moduleBase.BaseModuleName(), sdkVersion)
820 }
821
822 var apiScope *apiScope
823 switch sdkVersion.kind {
824 case sdkSystem:
825 apiScope = apiScopeSystem
Paul Duffin803a9562020-05-20 11:52:25 +0100826 case sdkModule:
827 apiScope = apiScopeModuleLib
Paul Duffinb05d4292020-05-20 12:19:10 +0100828 case sdkTest:
829 apiScope = apiScopeTest
Paul Duffin0c5bae52020-06-02 13:00:08 +0100830 case sdkSystemServer:
831 apiScope = apiScopeSystemServer
Paul Duffinb05d4292020-05-20 12:19:10 +0100832 default:
833 apiScope = apiScopePublic
834 }
835
Paul Duffin803a9562020-05-20 11:52:25 +0100836 paths := c.findClosestScopePath(apiScope)
837 if paths == nil {
838 var scopes []string
839 for _, s := range allApiScopes {
840 if c.findScopePaths(s) != nil {
841 scopes = append(scopes, s.name)
842 }
843 }
844 ctx.ModuleErrorf("requires api scope %s from %s but it only has %q available", apiScope.name, c.moduleBase.BaseModuleName(), scopes)
845 return nil
846 }
847
Paul Duffin23970f42020-05-20 14:20:02 +0100848 return paths.stubsHeaderPath
Paul Duffinb05d4292020-05-20 12:19:10 +0100849}
850
Paul Duffin859fe962020-05-15 10:20:31 +0100851func (c *commonToSdkLibraryAndImport) sdkComponentPropertiesForChildLibrary() interface{} {
852 componentProps := &struct {
853 SdkLibraryToImplicitlyTrack *string
Paul Duffindfa131e2020-05-15 20:37:11 +0100854 }{}
855
856 if c.sharedLibrary() {
Paul Duffin859fe962020-05-15 10:20:31 +0100857 // Mark the stubs library as being components of this java_sdk_library so that
858 // any app that includes code which depends (directly or indirectly) on the stubs
859 // library will have the appropriate <uses-library> invocation inserted into its
860 // manifest if necessary.
Paul Duffindfa131e2020-05-15 20:37:11 +0100861 componentProps.SdkLibraryToImplicitlyTrack = proptools.StringPtr(c.moduleBase.BaseModuleName())
Paul Duffin859fe962020-05-15 10:20:31 +0100862 }
863
864 return componentProps
865}
866
Paul Duffindfa131e2020-05-15 20:37:11 +0100867// Check if this can be used as a shared library.
868func (c *commonToSdkLibraryAndImport) sharedLibrary() bool {
869 return proptools.BoolDefault(c.commonSdkLibraryProperties.Shared_library, true)
870}
871
Paul Duffin859fe962020-05-15 10:20:31 +0100872// Properties related to the use of a module as an component of a java_sdk_library.
873type SdkLibraryComponentProperties struct {
874
875 // The name of the java_sdk_library/_import to add to a <uses-library> entry
876 // in the AndroidManifest.xml of any Android app that includes code that references
877 // this module. If not set then no java_sdk_library/_import is tracked.
878 SdkLibraryToImplicitlyTrack *string `blueprint:"mutated"`
879}
880
881// Structure to be embedded in a module struct that needs to support the
882// SdkLibraryComponentDependency interface.
883type EmbeddableSdkLibraryComponent struct {
884 sdkLibraryComponentProperties SdkLibraryComponentProperties
885}
886
887func (e *EmbeddableSdkLibraryComponent) initSdkLibraryComponent(moduleBase *android.ModuleBase) {
888 moduleBase.AddProperties(&e.sdkLibraryComponentProperties)
889}
890
891// to satisfy SdkLibraryComponentDependency
Ulya Trafimovich31e444e2020-08-14 17:32:16 +0100892func (e *EmbeddableSdkLibraryComponent) OptionalImplicitSdkLibrary() *string {
893 return e.sdkLibraryComponentProperties.SdkLibraryToImplicitlyTrack
Paul Duffin859fe962020-05-15 10:20:31 +0100894}
895
Ulya Trafimovich39b437b2020-09-23 16:42:35 +0100896// to satisfy SdkLibraryComponentDependency
897func (e *EmbeddableSdkLibraryComponent) OptionalSdkLibraryImplementation() *string {
898 // Currently implementation library name is the same as the SDK library name.
899 return e.sdkLibraryComponentProperties.SdkLibraryToImplicitlyTrack
900}
901
Paul Duffin859fe962020-05-15 10:20:31 +0100902// Implemented by modules that are (or possibly could be) a component of a java_sdk_library
903// (including the java_sdk_library) itself.
904type SdkLibraryComponentDependency interface {
Ulya Trafimovich31e444e2020-08-14 17:32:16 +0100905 UsesLibraryDependency
906
Paul Duffin859fe962020-05-15 10:20:31 +0100907 // The optional name of the sdk library that should be implicitly added to the
908 // AndroidManifest of an app that contains code which references the sdk library.
909 //
Ulya Trafimovich31e444e2020-08-14 17:32:16 +0100910 // Returns the name of the optional implicit SDK library or nil, if there isn't one.
911 OptionalImplicitSdkLibrary() *string
Ulya Trafimovich39b437b2020-09-23 16:42:35 +0100912
913 // The name of the implementation library for the optional SDK library or nil, if there isn't one.
914 OptionalSdkLibraryImplementation() *string
Paul Duffin859fe962020-05-15 10:20:31 +0100915}
916
917// Make sure that all the module types that are components of java_sdk_library/_import
918// and which can be referenced (directly or indirectly) from an android app implement
919// the SdkLibraryComponentDependency interface.
920var _ SdkLibraryComponentDependency = (*Library)(nil)
921var _ SdkLibraryComponentDependency = (*Import)(nil)
922var _ SdkLibraryComponentDependency = (*SdkLibrary)(nil)
Paul Duffineedc5d52020-06-12 17:46:39 +0100923var _ SdkLibraryComponentDependency = (*SdkLibraryImport)(nil)
Paul Duffin859fe962020-05-15 10:20:31 +0100924
925// Provides access to sdk_version related header and implentation jars.
926type SdkLibraryDependency interface {
927 SdkLibraryComponentDependency
928
929 // Get the header jars appropriate for the supplied sdk_version.
930 //
931 // These are turbine generated jars so they only change if the externals of the
932 // class changes but it does not contain and implementation or JavaDoc.
933 SdkHeaderJars(ctx android.BaseModuleContext, sdkVersion sdkSpec) android.Paths
934
935 // Get the implementation jars appropriate for the supplied sdk version.
936 //
937 // These are either the implementation jar for the whole sdk library or the implementation
938 // jars for the stubs. The latter should only be needed when generating JavaDoc as otherwise
939 // they are identical to the corresponding header jars.
940 SdkImplementationJars(ctx android.BaseModuleContext, sdkVersion sdkSpec) android.Paths
941}
942
Inseob Kimc0907f12019-02-08 21:00:45 +0900943type SdkLibrary struct {
Sundong Ahn054b19a2018-10-19 13:46:09 +0900944 Library
Jiyong Parkc678ad32018-04-10 13:07:10 +0900945
Sundong Ahn054b19a2018-10-19 13:46:09 +0900946 sdkLibraryProperties sdkLibraryProperties
Jiyong Parkc678ad32018-04-10 13:07:10 +0900947
Paul Duffin3375e352020-04-28 10:44:03 +0100948 // Map from api scope to the scope specific property structure.
949 scopeToProperties map[*apiScope]*ApiScopeProperties
950
Paul Duffin56d44902020-01-31 13:36:25 +0000951 commonToSdkLibraryAndImport
Jiyong Parkc678ad32018-04-10 13:07:10 +0900952}
953
Inseob Kimc0907f12019-02-08 21:00:45 +0900954var _ Dependency = (*SdkLibrary)(nil)
955var _ SdkLibraryDependency = (*SdkLibrary)(nil)
Colin Cross897d2ed2019-02-11 14:03:51 -0800956
Paul Duffin3375e352020-04-28 10:44:03 +0100957func (module *SdkLibrary) generateTestAndSystemScopesByDefault() bool {
958 return module.sdkLibraryProperties.Generate_system_and_test_apis
959}
960
961func (module *SdkLibrary) getGeneratedApiScopes(ctx android.EarlyModuleContext) apiScopes {
962 // Check to see if any scopes have been explicitly enabled. If any have then all
963 // must be.
964 anyScopesExplicitlyEnabled := false
965 for _, scope := range allApiScopes {
966 scopeProperties := module.scopeToProperties[scope]
967 if scopeProperties.Enabled != nil {
968 anyScopesExplicitlyEnabled = true
969 break
970 }
Paul Duffind1b3a922020-01-22 11:57:20 +0000971 }
Paul Duffin3375e352020-04-28 10:44:03 +0100972
973 var generatedScopes apiScopes
974 enabledScopes := make(map[*apiScope]struct{})
975 for _, scope := range allApiScopes {
976 scopeProperties := module.scopeToProperties[scope]
977 // If any scopes are explicitly enabled then ignore the legacy enabled status.
978 // This is to ensure that any new usages of this module type do not rely on legacy
979 // behaviour.
980 defaultEnabledStatus := false
981 if anyScopesExplicitlyEnabled {
982 defaultEnabledStatus = scope.defaultEnabledStatus
983 } else {
984 defaultEnabledStatus = scope.legacyEnabledStatus(module)
985 }
986 enabled := proptools.BoolDefault(scopeProperties.Enabled, defaultEnabledStatus)
987 if enabled {
988 enabledScopes[scope] = struct{}{}
989 generatedScopes = append(generatedScopes, scope)
990 }
991 }
992
993 // Now check to make sure that any scope that is extended by an enabled scope is also
994 // enabled.
995 for _, scope := range allApiScopes {
996 if _, ok := enabledScopes[scope]; ok {
997 extends := scope.extends
998 if extends != nil {
999 if _, ok := enabledScopes[extends]; !ok {
1000 ctx.ModuleErrorf("enabled api scope %q depends on disabled scope %q", scope, extends)
1001 }
1002 }
1003 }
1004 }
1005
1006 return generatedScopes
Paul Duffind1b3a922020-01-22 11:57:20 +00001007}
1008
Paul Duffineedc5d52020-06-12 17:46:39 +01001009type sdkLibraryComponentTag struct {
1010 blueprint.BaseDependencyTag
1011 name string
1012}
1013
1014// Mark this tag so dependencies that use it are excluded from visibility enforcement.
1015func (t sdkLibraryComponentTag) ExcludeFromVisibilityEnforcement() {}
1016
1017var xmlPermissionsFileTag = sdkLibraryComponentTag{name: "xml-permissions-file"}
Paul Duffine74ac732020-02-06 13:51:46 +00001018
Jiyong Parke3833882020-02-17 17:28:10 +09001019func IsXmlPermissionsFileDepTag(depTag blueprint.DependencyTag) bool {
Paul Duffineedc5d52020-06-12 17:46:39 +01001020 if dt, ok := depTag.(sdkLibraryComponentTag); ok {
Jiyong Parke3833882020-02-17 17:28:10 +09001021 return dt == xmlPermissionsFileTag
1022 }
1023 return false
1024}
1025
Paul Duffineedc5d52020-06-12 17:46:39 +01001026var implLibraryTag = sdkLibraryComponentTag{name: "impl-library"}
Paul Duffin5df79302020-05-16 15:52:12 +01001027
Paul Duffin44f1d842020-06-26 20:17:02 +01001028// Add the dependencies on the child modules in the component deps mutator.
1029func (module *SdkLibrary) ComponentDepsMutator(ctx android.BottomUpMutatorContext) {
Paul Duffin3375e352020-04-28 10:44:03 +01001030 for _, apiScope := range module.getGeneratedApiScopes(ctx) {
Paul Duffind1b3a922020-01-22 11:57:20 +00001031 // Add dependencies to the stubs library
Paul Duffinc3091c82020-05-08 14:16:20 +01001032 ctx.AddVariationDependencies(nil, apiScope.stubsTag, module.stubsLibraryModuleName(apiScope))
Paul Duffind1b3a922020-01-22 11:57:20 +00001033
Paul Duffin15f34ef2020-07-20 18:04:44 +01001034 // Add a dependency on the stubs source in order to access both stubs source and api information.
1035 ctx.AddVariationDependencies(nil, apiScope.stubsSourceAndApiTag, module.stubsSourceModuleName(apiScope))
Sundong Ahn054b19a2018-10-19 13:46:09 +09001036 }
1037
Paul Duffindfa131e2020-05-15 20:37:11 +01001038 if module.requiresRuntimeImplementationLibrary() {
Paul Duffin5df79302020-05-16 15:52:12 +01001039 // Add dependency to the rule for generating the implementation library.
1040 ctx.AddDependency(module, implLibraryTag, module.implLibraryModuleName())
1041
Paul Duffindfa131e2020-05-15 20:37:11 +01001042 if module.sharedLibrary() {
1043 // Add dependency to the rule for generating the xml permissions file
Paul Duffineedc5d52020-06-12 17:46:39 +01001044 ctx.AddDependency(module, xmlPermissionsFileTag, module.xmlPermissionsModuleName())
Paul Duffindfa131e2020-05-15 20:37:11 +01001045 }
Paul Duffin44f1d842020-06-26 20:17:02 +01001046 }
1047}
Paul Duffine74ac732020-02-06 13:51:46 +00001048
Paul Duffin44f1d842020-06-26 20:17:02 +01001049// Add other dependencies as normal.
1050func (module *SdkLibrary) DepsMutator(ctx android.BottomUpMutatorContext) {
Anton Hanssone77fccc2021-01-20 16:52:41 +00001051 var missingApiModules []string
1052 for _, apiScope := range module.getGeneratedApiScopes(ctx) {
1053 if apiScope.unstable {
1054 continue
1055 }
1056 if m := android.SrcIsModule(module.latestApiFilegroupName(apiScope)); !ctx.OtherModuleExists(m) {
1057 missingApiModules = append(missingApiModules, m)
1058 }
1059 if m := android.SrcIsModule(module.latestRemovedApiFilegroupName(apiScope)); !ctx.OtherModuleExists(m) {
1060 missingApiModules = append(missingApiModules, m)
1061 }
1062 }
1063 if len(missingApiModules) != 0 && !module.sdkLibraryProperties.Unsafe_ignore_missing_latest_api {
1064 m := module.Name() + " is missing tracking files for previously released library versions.\n"
1065 m += "You need to do one of the following:\n"
1066 m += "- Add `unsafe_ignore_missing_latest_api: true` to your blueprint (to disable compat tracking)\n"
1067 m += "- Add a set of prebuilt txt files representing the last released version of this library for compat checking.\n"
1068 m += " (the current set of API files can be used as a seed for this compatibility tracking\n"
1069 m += "\n"
1070 m += "The following filegroup modules are missing:\n "
1071 m += strings.Join(missingApiModules, "\n ") + "\n"
1072 m += "Please see the documentation of the prebuilt_apis module type (and a usage example in prebuilts/sdk) for a convenient way to generate these."
1073 ctx.ModuleErrorf(m)
1074 }
Paul Duffin44f1d842020-06-26 20:17:02 +01001075 if module.requiresRuntimeImplementationLibrary() {
Paul Duffindfa131e2020-05-15 20:37:11 +01001076 // Only add the deps for the library if it is actually going to be built.
1077 module.Library.deps(ctx)
1078 }
Jiyong Parkc678ad32018-04-10 13:07:10 +09001079}
1080
Paul Duffin46dc45a2020-05-14 15:39:10 +01001081func (module *SdkLibrary) OutputFiles(tag string) (android.Paths, error) {
1082 paths, err := module.commonOutputFiles(tag)
1083 if paths == nil && err == nil {
1084 return module.Library.OutputFiles(tag)
1085 } else {
1086 return paths, err
1087 }
1088}
1089
Inseob Kimc0907f12019-02-08 21:00:45 +09001090func (module *SdkLibrary) GenerateAndroidBuildActions(ctx android.ModuleContext) {
Paul Duffina2ae7e02020-09-11 11:55:00 +01001091 module.generateCommonBuildActions(ctx)
1092
Paul Duffindfa131e2020-05-15 20:37:11 +01001093 // Only build an implementation library if required.
1094 if module.requiresRuntimeImplementationLibrary() {
Paul Duffin43db9be2019-12-30 17:35:49 +00001095 module.Library.GenerateAndroidBuildActions(ctx)
1096 }
Sundong Ahn054b19a2018-10-19 13:46:09 +09001097
Sundong Ahn57368eb2018-07-06 11:20:23 +09001098 // Record the paths to the header jars of the library (stubs and impl).
Paul Duffind1b3a922020-01-22 11:57:20 +00001099 // When this java_sdk_library is depended upon from others via "libs" property,
Jiyong Parkc678ad32018-04-10 13:07:10 +09001100 // the recorded paths will be returned depending on the link type of the caller.
1101 ctx.VisitDirectDeps(func(to android.Module) {
Jiyong Parkc678ad32018-04-10 13:07:10 +09001102 tag := ctx.OtherModuleDependencyTag(to)
1103
Paul Duffinc8782502020-04-29 20:45:27 +01001104 // Extract information from any of the scope specific dependencies.
1105 if scopeTag, ok := tag.(scopeDependencyTag); ok {
1106 apiScope := scopeTag.apiScope
Paul Duffin803a9562020-05-20 11:52:25 +01001107 scopePaths := module.getScopePathsCreateIfNeeded(apiScope)
Paul Duffinc8782502020-04-29 20:45:27 +01001108
1109 // Extract information from the dependency. The exact information extracted
1110 // is determined by the nature of the dependency which is determined by the tag.
1111 scopeTag.extractDepInfo(ctx, to, scopePaths)
Sundong Ahn20e998b2018-07-24 11:19:26 +09001112 }
Jiyong Parkc678ad32018-04-10 13:07:10 +09001113 })
1114}
1115
Jiyong Park0b0e1b92019-12-03 13:24:29 +09001116func (module *SdkLibrary) AndroidMkEntries() []android.AndroidMkEntries {
Paul Duffindfa131e2020-05-15 20:37:11 +01001117 if !module.requiresRuntimeImplementationLibrary() {
Paul Duffin43db9be2019-12-30 17:35:49 +00001118 return nil
1119 }
Jiyong Park0b0e1b92019-12-03 13:24:29 +09001120 entriesList := module.Library.AndroidMkEntries()
Yo Chiang07d75072020-06-05 17:43:19 +08001121 if module.sharedLibrary() {
1122 entries := &entriesList[0]
1123 entries.Required = append(entries.Required, module.xmlPermissionsModuleName())
1124 }
Jiyong Park0b0e1b92019-12-03 13:24:29 +09001125 return entriesList
Jiyong Park82484c02018-04-23 21:41:26 +09001126}
1127
Anton Hansson5fd5d242020-03-27 19:43:19 +00001128// The dist path of the stub artifacts
1129func (module *SdkLibrary) apiDistPath(apiScope *apiScope) string {
1130 if module.ModuleBase.Owner() != "" {
1131 return path.Join("apistubs", module.ModuleBase.Owner(), apiScope.name)
1132 } else if Bool(module.sdkLibraryProperties.Core_lib) {
1133 return path.Join("apistubs", "core", apiScope.name)
1134 } else {
1135 return path.Join("apistubs", "android", apiScope.name)
1136 }
1137}
1138
Paul Duffin12ceb462019-12-24 20:31:31 +00001139// Get the sdk version for use when compiling the stubs library.
Paul Duffin780c5f42020-05-12 15:52:55 +01001140func (module *SdkLibrary) sdkVersionForStubsLibrary(mctx android.EarlyModuleContext, apiScope *apiScope) string {
Paul Duffin87a05a32020-05-12 11:50:28 +01001141 scopeProperties := module.scopeToProperties[apiScope]
1142 if scopeProperties.Sdk_version != nil {
1143 return proptools.String(scopeProperties.Sdk_version)
1144 }
1145
Paul Duffin12ceb462019-12-24 20:31:31 +00001146 sdkDep := decodeSdkDep(mctx, sdkContext(&module.Library))
1147 if sdkDep.hasStandardLibs() {
1148 // If building against a standard sdk then use the sdk version appropriate for the scope.
Paul Duffind1b3a922020-01-22 11:57:20 +00001149 return apiScope.sdkVersion
Paul Duffin12ceb462019-12-24 20:31:31 +00001150 } else {
1151 // Otherwise, use no system module.
1152 return "none"
1153 }
1154}
1155
Paul Duffin31310252020-11-20 21:26:20 +00001156func (module *SdkLibrary) distStem() string {
1157 return proptools.StringDefault(module.sdkLibraryProperties.Dist_stem, module.BaseModuleName())
1158}
1159
Paul Duffind1b3a922020-01-22 11:57:20 +00001160func (module *SdkLibrary) latestApiFilegroupName(apiScope *apiScope) string {
Paul Duffin31310252020-11-20 21:26:20 +00001161 return ":" + module.distStem() + ".api." + apiScope.name + ".latest"
Jiyong Park58c518b2018-05-12 22:29:12 +09001162}
Jiyong Parkc678ad32018-04-10 13:07:10 +09001163
Paul Duffind1b3a922020-01-22 11:57:20 +00001164func (module *SdkLibrary) latestRemovedApiFilegroupName(apiScope *apiScope) string {
Paul Duffin31310252020-11-20 21:26:20 +00001165 return ":" + module.distStem() + "-removed.api." + apiScope.name + ".latest"
Jiyong Parkc678ad32018-04-10 13:07:10 +09001166}
1167
Anton Hansson944e77d2020-08-19 11:40:22 +01001168func childModuleVisibility(childVisibility []string) []string {
1169 if childVisibility == nil {
1170 // No child visibility set. The child will use the visibility of the sdk_library.
1171 return nil
1172 }
1173
1174 // Prepend an override to ignore the sdk_library's visibility, and rely on the child visibility.
1175 var visibility []string
1176 visibility = append(visibility, "//visibility:override")
1177 visibility = append(visibility, childVisibility...)
1178 return visibility
1179}
1180
Paul Duffin5df79302020-05-16 15:52:12 +01001181// Creates the implementation java library
1182func (module *SdkLibrary) createImplLibrary(mctx android.DefaultableHookContext) {
Paul Duffina2058f82020-06-24 16:22:38 +01001183 moduleNamePtr := proptools.StringPtr(module.BaseModuleName())
1184
Anton Hansson944e77d2020-08-19 11:40:22 +01001185 visibility := childModuleVisibility(module.sdkLibraryProperties.Impl_library_visibility)
1186
Paul Duffin5df79302020-05-16 15:52:12 +01001187 props := struct {
Paul Duffina2058f82020-06-24 16:22:38 +01001188 Name *string
1189 Visibility []string
1190 Instrument bool
Anton Hansson7f66efa2020-10-08 14:47:23 +01001191 Libs []string
Paul Duffina2058f82020-06-24 16:22:38 +01001192 ConfigurationName *string
Paul Duffin5df79302020-05-16 15:52:12 +01001193 }{
1194 Name: proptools.StringPtr(module.implLibraryModuleName()),
Anton Hansson944e77d2020-08-19 11:40:22 +01001195 Visibility: visibility,
Paul Duffin296cf332020-06-18 21:09:55 +01001196 // Set the instrument property to ensure it is instrumented when instrumentation is required.
1197 Instrument: true,
Anton Hansson7f66efa2020-10-08 14:47:23 +01001198 // Set the impl_only libs. Note that the module's "Libs" get appended as well, via the
1199 // addition of &module.properties below.
1200 Libs: module.sdkLibraryProperties.Impl_only_libs,
Paul Duffina2058f82020-06-24 16:22:38 +01001201
1202 // Make the created library behave as if it had the same name as this module.
1203 ConfigurationName: moduleNamePtr,
Paul Duffin5df79302020-05-16 15:52:12 +01001204 }
1205
1206 properties := []interface{}{
1207 &module.properties,
1208 &module.protoProperties,
1209 &module.deviceProperties,
Liz Kammera7a64f32020-07-09 15:16:41 -07001210 &module.dexProperties,
Paul Duffin5df79302020-05-16 15:52:12 +01001211 &module.dexpreoptProperties,
Colin Cross014489c2020-06-02 20:09:13 -07001212 &module.linter.properties,
Paul Duffin5df79302020-05-16 15:52:12 +01001213 &props,
1214 module.sdkComponentPropertiesForChildLibrary(),
1215 }
1216 mctx.CreateModule(LibraryFactory, properties...)
1217}
1218
Jiyong Parkc678ad32018-04-10 13:07:10 +09001219// Creates a static java library that has API stubs
Paul Duffinf0229202020-04-29 16:47:28 +01001220func (module *SdkLibrary) createStubsLibrary(mctx android.DefaultableHookContext, apiScope *apiScope) {
Jiyong Parkc678ad32018-04-10 13:07:10 +09001221 props := struct {
Dan Willemsen9f435972020-05-28 15:28:00 -07001222 Name *string
1223 Visibility []string
1224 Srcs []string
1225 Installable *bool
1226 Sdk_version *string
1227 System_modules *string
1228 Patch_module *string
1229 Libs []string
1230 Compile_dex *bool
1231 Java_version *string
1232 Openjdk9 struct {
Sundong Ahn054b19a2018-10-19 13:46:09 +09001233 Srcs []string
1234 Javacflags []string
1235 }
Anton Hansson5fd5d242020-03-27 19:43:19 +00001236 Dist struct {
1237 Targets []string
1238 Dest *string
1239 Dir *string
1240 Tag *string
1241 }
Jiyong Parkc678ad32018-04-10 13:07:10 +09001242 }{}
1243
Paul Duffinc3091c82020-05-08 14:16:20 +01001244 props.Name = proptools.StringPtr(module.stubsLibraryModuleName(apiScope))
Anton Hansson944e77d2020-08-19 11:40:22 +01001245 props.Visibility = childModuleVisibility(module.sdkLibraryProperties.Stubs_library_visibility)
Jiyong Parkc678ad32018-04-10 13:07:10 +09001246 // sources are generated from the droiddoc
Paul Duffinc3091c82020-05-08 14:16:20 +01001247 props.Srcs = []string{":" + module.stubsSourceModuleName(apiScope)}
Paul Duffin12ceb462019-12-24 20:31:31 +00001248 sdkVersion := module.sdkVersionForStubsLibrary(mctx, apiScope)
Paul Duffin52d398a2019-06-11 12:31:14 +01001249 props.Sdk_version = proptools.StringPtr(sdkVersion)
Paul Duffina18abc22020-05-16 18:54:24 +01001250 props.System_modules = module.deviceProperties.System_modules
1251 props.Patch_module = module.properties.Patch_module
Paul Duffin367ab912019-12-23 19:40:36 +00001252 props.Installable = proptools.BoolPtr(false)
Sundong Ahn054b19a2018-10-19 13:46:09 +09001253 props.Libs = module.sdkLibraryProperties.Stub_only_libs
Paul Duffine22c2ab2020-05-20 19:35:27 +01001254 // The stub-annotations library contains special versions of the annotations
1255 // with CLASS retention policy, so that they're kept.
1256 if proptools.Bool(module.sdkLibraryProperties.Annotations_enabled) {
1257 props.Libs = append(props.Libs, "stub-annotations")
1258 }
Paul Duffina18abc22020-05-16 18:54:24 +01001259 props.Openjdk9.Srcs = module.properties.Openjdk9.Srcs
1260 props.Openjdk9.Javacflags = module.properties.Openjdk9.Javacflags
Anton Hansson83509b52020-05-21 09:21:57 +01001261 // We compile the stubs for 1.8 in line with the main android.jar stubs, and potential
1262 // interop with older developer tools that don't support 1.9.
1263 props.Java_version = proptools.StringPtr("1.8")
Liz Kammera7a64f32020-07-09 15:16:41 -07001264 if module.dexProperties.Compile_dex != nil {
1265 props.Compile_dex = module.dexProperties.Compile_dex
Sundong Ahndd567f92018-07-31 17:19:11 +09001266 }
Jiyong Parkc678ad32018-04-10 13:07:10 +09001267
Anton Hansson5fd5d242020-03-27 19:43:19 +00001268 // Dist the class jar artifact for sdk builds.
1269 if !Bool(module.sdkLibraryProperties.No_dist) {
1270 props.Dist.Targets = []string{"sdk", "win_sdk"}
Paul Duffin31310252020-11-20 21:26:20 +00001271 props.Dist.Dest = proptools.StringPtr(fmt.Sprintf("%v.jar", module.distStem()))
Anton Hansson5fd5d242020-03-27 19:43:19 +00001272 props.Dist.Dir = proptools.StringPtr(module.apiDistPath(apiScope))
1273 props.Dist.Tag = proptools.StringPtr(".jar")
1274 }
Jiyong Parkc678ad32018-04-10 13:07:10 +09001275
Paul Duffin859fe962020-05-15 10:20:31 +01001276 mctx.CreateModule(LibraryFactory, &props, module.sdkComponentPropertiesForChildLibrary())
Jiyong Parkc678ad32018-04-10 13:07:10 +09001277}
1278
Paul Duffin6d0886e2020-04-07 18:49:53 +01001279// Creates a droidstubs module that creates stubs source files from the given full source
Paul Duffinc8782502020-04-29 20:45:27 +01001280// files and also updates and checks the API specification files.
Paul Duffin15f34ef2020-07-20 18:04:44 +01001281func (module *SdkLibrary) createStubsSourcesAndApi(mctx android.DefaultableHookContext, apiScope *apiScope, name string, scopeSpecificDroidstubsArgs []string) {
Jiyong Parkc678ad32018-04-10 13:07:10 +09001282 props := struct {
Sundong Ahn054b19a2018-10-19 13:46:09 +09001283 Name *string
Paul Duffin4911a892020-04-29 23:35:13 +01001284 Visibility []string
Sundong Ahn054b19a2018-10-19 13:46:09 +09001285 Srcs []string
1286 Installable *bool
Paul Duffin52d398a2019-06-11 12:31:14 +01001287 Sdk_version *string
Paul Duffin12ceb462019-12-24 20:31:31 +00001288 System_modules *string
Sundong Ahn054b19a2018-10-19 13:46:09 +09001289 Libs []string
Paul Duffin6877e6d2020-09-25 19:59:14 +01001290 Output_javadoc_comments *bool
Paul Duffin11512472019-02-11 15:55:17 +00001291 Arg_files []string
Sundong Ahn054b19a2018-10-19 13:46:09 +09001292 Args *string
Sundong Ahn054b19a2018-10-19 13:46:09 +09001293 Java_version *string
Paul Duffine22c2ab2020-05-20 19:35:27 +01001294 Annotations_enabled *bool
Sundong Ahn054b19a2018-10-19 13:46:09 +09001295 Merge_annotations_dirs []string
1296 Merge_inclusion_annotations_dirs []string
Paul Duffin0ff08bd2020-04-29 13:30:54 +01001297 Generate_stubs *bool
Anton Hanssone87b03d2020-12-21 15:29:34 +00001298 Previous_api *string
Sundong Ahn054b19a2018-10-19 13:46:09 +09001299 Check_api struct {
Anton Hanssone6056152020-12-31 10:37:27 +00001300 Current ApiToCheck
1301 Last_released ApiToCheck
Paul Duffin160fe412020-05-10 19:32:20 +01001302
1303 Api_lint struct {
1304 Enabled *bool
1305 New_since *string
1306 Baseline_file *string
1307 }
Jiyong Park58c518b2018-05-12 22:29:12 +09001308 }
Sundong Ahn1b92c822018-05-29 11:35:17 +09001309 Aidl struct {
1310 Include_dirs []string
1311 Local_include_dirs []string
1312 }
Paul Duffin040e9062020-11-23 17:41:36 +00001313 Dists []android.Dist
Jiyong Parkc678ad32018-04-10 13:07:10 +09001314 }{}
1315
Paul Duffin7b78b4d2020-04-28 14:08:32 +01001316 // The stubs source processing uses the same compile time classpath when extracting the
1317 // API from the implementation library as it does when compiling it. i.e. the same
1318 // * sdk version
1319 // * system_modules
1320 // * libs (static_libs/libs)
Paul Duffin250e6192019-06-07 10:44:37 +01001321
Paul Duffin0ff08bd2020-04-29 13:30:54 +01001322 props.Name = proptools.StringPtr(name)
Anton Hansson944e77d2020-08-19 11:40:22 +01001323 props.Visibility = childModuleVisibility(module.sdkLibraryProperties.Stubs_source_visibility)
Paul Duffina18abc22020-05-16 18:54:24 +01001324 props.Srcs = append(props.Srcs, module.properties.Srcs...)
1325 props.Sdk_version = module.deviceProperties.Sdk_version
1326 props.System_modules = module.deviceProperties.System_modules
Jiyong Parkc678ad32018-04-10 13:07:10 +09001327 props.Installable = proptools.BoolPtr(false)
Sundong Ahne6f0b052018-06-05 16:46:14 +09001328 // A droiddoc module has only one Libs property and doesn't distinguish between
1329 // shared libs and static libs. So we need to add both of these libs to Libs property.
Paul Duffina18abc22020-05-16 18:54:24 +01001330 props.Libs = module.properties.Libs
1331 props.Libs = append(props.Libs, module.properties.Static_libs...)
1332 props.Aidl.Include_dirs = module.deviceProperties.Aidl.Include_dirs
1333 props.Aidl.Local_include_dirs = module.deviceProperties.Aidl.Local_include_dirs
1334 props.Java_version = module.properties.Java_version
Jiyong Parkc678ad32018-04-10 13:07:10 +09001335
Paul Duffine22c2ab2020-05-20 19:35:27 +01001336 props.Annotations_enabled = module.sdkLibraryProperties.Annotations_enabled
Sundong Ahn054b19a2018-10-19 13:46:09 +09001337 props.Merge_annotations_dirs = module.sdkLibraryProperties.Merge_annotations_dirs
1338 props.Merge_inclusion_annotations_dirs = module.sdkLibraryProperties.Merge_inclusion_annotations_dirs
1339
Paul Duffin6d0886e2020-04-07 18:49:53 +01001340 droidstubsArgs := []string{}
Paul Duffin235ffff2019-12-24 10:41:30 +00001341 if len(module.sdkLibraryProperties.Api_packages) != 0 {
Paul Duffin6d0886e2020-04-07 18:49:53 +01001342 droidstubsArgs = append(droidstubsArgs, "--stub-packages "+strings.Join(module.sdkLibraryProperties.Api_packages, ":"))
Paul Duffin235ffff2019-12-24 10:41:30 +00001343 }
1344 if len(module.sdkLibraryProperties.Hidden_api_packages) != 0 {
Paul Duffin6d0886e2020-04-07 18:49:53 +01001345 droidstubsArgs = append(droidstubsArgs,
Paul Duffin235ffff2019-12-24 10:41:30 +00001346 android.JoinWithPrefix(module.sdkLibraryProperties.Hidden_api_packages, " --hide-package "))
1347 }
Paul Duffin6d0886e2020-04-07 18:49:53 +01001348 droidstubsArgs = append(droidstubsArgs, module.sdkLibraryProperties.Droiddoc_options...)
Paul Duffin235ffff2019-12-24 10:41:30 +00001349 disabledWarnings := []string{
1350 "MissingPermission",
1351 "BroadcastBehavior",
1352 "HiddenSuperclass",
1353 "DeprecationMismatch",
1354 "UnavailableSymbol",
1355 "SdkConstant",
1356 "HiddenTypeParameter",
1357 "Todo",
1358 "Typo",
1359 }
Paul Duffin6d0886e2020-04-07 18:49:53 +01001360 droidstubsArgs = append(droidstubsArgs, android.JoinWithPrefix(disabledWarnings, "--hide "))
Sundong Ahnfb2721f2018-09-17 13:23:09 +09001361
Paul Duffin6877e6d2020-09-25 19:59:14 +01001362 // Output Javadoc comments for public scope.
1363 if apiScope == apiScopePublic {
1364 props.Output_javadoc_comments = proptools.BoolPtr(true)
1365 }
1366
Paul Duffin1fb487d2020-04-07 18:50:10 +01001367 // Add in scope specific arguments.
Paul Duffin0ff08bd2020-04-29 13:30:54 +01001368 droidstubsArgs = append(droidstubsArgs, scopeSpecificDroidstubsArgs...)
Paul Duffin11512472019-02-11 15:55:17 +00001369 props.Arg_files = module.sdkLibraryProperties.Droiddoc_option_files
Paul Duffin6d0886e2020-04-07 18:49:53 +01001370 props.Args = proptools.StringPtr(strings.Join(droidstubsArgs, " "))
Jiyong Parkc678ad32018-04-10 13:07:10 +09001371
Paul Duffin15f34ef2020-07-20 18:04:44 +01001372 // List of APIs identified from the provided source files are created. They are later
1373 // compared against to the not-yet-released (a.k.a current) list of APIs and to the
1374 // last-released (a.k.a numbered) list of API.
1375 currentApiFileName := apiScope.apiFilePrefix + "current.txt"
1376 removedApiFileName := apiScope.apiFilePrefix + "removed.txt"
1377 apiDir := module.getApiDir()
1378 currentApiFileName = path.Join(apiDir, currentApiFileName)
1379 removedApiFileName = path.Join(apiDir, removedApiFileName)
Jiyong Parkc678ad32018-04-10 13:07:10 +09001380
Paul Duffin15f34ef2020-07-20 18:04:44 +01001381 // check against the not-yet-release API
1382 props.Check_api.Current.Api_file = proptools.StringPtr(currentApiFileName)
1383 props.Check_api.Current.Removed_api_file = proptools.StringPtr(removedApiFileName)
Jiyong Park58c518b2018-05-12 22:29:12 +09001384
Anton Hanssone6056152020-12-31 10:37:27 +00001385 if !(apiScope.unstable || module.sdkLibraryProperties.Unsafe_ignore_missing_latest_api) {
Paul Duffin15f34ef2020-07-20 18:04:44 +01001386 // check against the latest released API
1387 latestApiFilegroupName := proptools.StringPtr(module.latestApiFilegroupName(apiScope))
Anton Hanssone87b03d2020-12-21 15:29:34 +00001388 props.Previous_api = latestApiFilegroupName
Paul Duffin15f34ef2020-07-20 18:04:44 +01001389 props.Check_api.Last_released.Api_file = latestApiFilegroupName
1390 props.Check_api.Last_released.Removed_api_file = proptools.StringPtr(
1391 module.latestRemovedApiFilegroupName(apiScope))
Paul Duffin160fe412020-05-10 19:32:20 +01001392
Paul Duffin15f34ef2020-07-20 18:04:44 +01001393 if proptools.Bool(module.sdkLibraryProperties.Api_lint.Enabled) {
1394 // Enable api lint.
1395 props.Check_api.Api_lint.Enabled = proptools.BoolPtr(true)
1396 props.Check_api.Api_lint.New_since = latestApiFilegroupName
Paul Duffin160fe412020-05-10 19:32:20 +01001397
Paul Duffin15f34ef2020-07-20 18:04:44 +01001398 // If it exists then pass a lint-baseline.txt through to droidstubs.
1399 baselinePath := path.Join(apiDir, apiScope.apiFilePrefix+"lint-baseline.txt")
1400 baselinePathRelativeToRoot := path.Join(mctx.ModuleDir(), baselinePath)
1401 paths, err := mctx.GlobWithDeps(baselinePathRelativeToRoot, nil)
1402 if err != nil {
1403 mctx.ModuleErrorf("error checking for presence of %s: %s", baselinePathRelativeToRoot, err)
1404 }
1405 if len(paths) == 1 {
1406 props.Check_api.Api_lint.Baseline_file = proptools.StringPtr(baselinePath)
1407 } else if len(paths) != 0 {
1408 mctx.ModuleErrorf("error checking for presence of %s: expected one path, found: %v", baselinePathRelativeToRoot, paths)
Paul Duffin160fe412020-05-10 19:32:20 +01001409 }
1410 }
Paul Duffin15f34ef2020-07-20 18:04:44 +01001411 }
Jiyong Park58c518b2018-05-12 22:29:12 +09001412
Paul Duffin15f34ef2020-07-20 18:04:44 +01001413 if !Bool(module.sdkLibraryProperties.No_dist) {
Paul Duffin040e9062020-11-23 17:41:36 +00001414 // Dist the api txt and removed api txt artifacts for sdk builds.
1415 distDir := proptools.StringPtr(path.Join(module.apiDistPath(apiScope), "api"))
1416 for _, p := range []struct {
1417 tag string
1418 pattern string
1419 }{
1420 {tag: ".api.txt", pattern: "%s.txt"},
1421 {tag: ".removed-api.txt", pattern: "%s-removed.txt"},
1422 } {
1423 props.Dists = append(props.Dists, android.Dist{
1424 Targets: []string{"sdk", "win_sdk"},
1425 Dir: distDir,
1426 Dest: proptools.StringPtr(fmt.Sprintf(p.pattern, module.distStem())),
1427 Tag: proptools.StringPtr(p.tag),
1428 })
1429 }
Anton Hansson5fd5d242020-03-27 19:43:19 +00001430 }
1431
Colin Cross84dfc3d2019-09-25 11:33:01 -07001432 mctx.CreateModule(DroidstubsFactory, &props)
Jiyong Parkc678ad32018-04-10 13:07:10 +09001433}
1434
Jooyung Han5e9013b2020-03-10 06:23:13 +09001435func (module *SdkLibrary) DepIsInSameApex(mctx android.BaseModuleContext, dep android.Module) bool {
1436 depTag := mctx.OtherModuleDependencyTag(dep)
1437 if depTag == xmlPermissionsFileTag {
1438 return true
1439 }
1440 return module.Library.DepIsInSameApex(mctx, dep)
1441}
1442
Jiyong Parkc678ad32018-04-10 13:07:10 +09001443// Creates the xml file that publicizes the runtime library
Paul Duffinf0229202020-04-29 16:47:28 +01001444func (module *SdkLibrary) createXmlFile(mctx android.DefaultableHookContext) {
Jiyong Parke3833882020-02-17 17:28:10 +09001445 props := struct {
Paul Duffin1dbe3ca2020-05-16 09:57:59 +01001446 Name *string
1447 Lib_name *string
1448 Apex_available []string
Jiyong Parke3833882020-02-17 17:28:10 +09001449 }{
Paul Duffineedc5d52020-06-12 17:46:39 +01001450 Name: proptools.StringPtr(module.xmlPermissionsModuleName()),
Jooyung Han5e9013b2020-03-10 06:23:13 +09001451 Lib_name: proptools.StringPtr(module.BaseModuleName()),
1452 Apex_available: module.ApexProperties.Apex_available,
Jiyong Parkc678ad32018-04-10 13:07:10 +09001453 }
Jiyong Parke3833882020-02-17 17:28:10 +09001454
Jiyong Parke3833882020-02-17 17:28:10 +09001455 mctx.CreateModule(sdkLibraryXmlFactory, &props)
Jiyong Parkc678ad32018-04-10 13:07:10 +09001456}
1457
Paul Duffin50061512020-01-21 16:31:05 +00001458func PrebuiltJars(ctx android.BaseModuleContext, baseName string, s sdkSpec) android.Paths {
Jiyong Park6a927c42020-01-21 02:03:43 +09001459 var ver sdkVersion
1460 var kind sdkKind
1461 if s.usePrebuilt(ctx) {
1462 ver = s.version
1463 kind = s.kind
Jiyong Parkc678ad32018-04-10 13:07:10 +09001464 } else {
Jiyong Park6a927c42020-01-21 02:03:43 +09001465 // We don't have prebuilt SDK for the specific sdkVersion.
1466 // Instead of breaking the build, fallback to use "system_current"
1467 ver = sdkVersionCurrent
1468 kind = sdkSystem
Sundong Ahn054b19a2018-10-19 13:46:09 +09001469 }
Jiyong Park6a927c42020-01-21 02:03:43 +09001470
1471 dir := filepath.Join("prebuilts", "sdk", ver.String(), kind.String())
Paul Duffin50061512020-01-21 16:31:05 +00001472 jar := filepath.Join(dir, baseName+".jar")
Sundong Ahn054b19a2018-10-19 13:46:09 +09001473 jarPath := android.ExistentPathForSource(ctx, jar)
Sundong Ahnae418ac2019-02-28 15:01:28 +09001474 if !jarPath.Valid() {
Colin Cross07c88562020-01-07 09:34:44 -08001475 if ctx.Config().AllowMissingDependencies() {
1476 return android.Paths{android.PathForSource(ctx, jar)}
1477 } else {
Jiyong Park6a927c42020-01-21 02:03:43 +09001478 ctx.PropertyErrorf("sdk_library", "invalid sdk version %q, %q does not exist", s.raw, jar)
Colin Cross07c88562020-01-07 09:34:44 -08001479 }
Sundong Ahnae418ac2019-02-28 15:01:28 +09001480 return nil
1481 }
Sundong Ahn054b19a2018-10-19 13:46:09 +09001482 return android.Paths{jarPath.Path()}
1483}
1484
Colin Crossaede88c2020-08-11 12:17:01 -07001485// Check to see if the other module is within the same set of named APEXes as this module.
Paul Duffin9b879592020-05-26 13:21:35 +01001486//
1487// If either this or the other module are on the platform then this will return
1488// false.
Colin Cross56a83212020-09-15 18:30:11 -07001489func withinSameApexesAs(ctx android.BaseModuleContext, other android.Module) bool {
1490 apexInfo := ctx.Provider(android.ApexInfoProvider).(android.ApexInfo)
1491 otherApexInfo := ctx.OtherModuleProvider(other, android.ApexInfoProvider).(android.ApexInfo)
1492 return len(otherApexInfo.InApexes) > 0 && reflect.DeepEqual(apexInfo.InApexes, otherApexInfo.InApexes)
Paul Duffin9b879592020-05-26 13:21:35 +01001493}
1494
Paul Duffinb05d4292020-05-20 12:19:10 +01001495func (module *SdkLibrary) sdkJars(ctx android.BaseModuleContext, sdkVersion sdkSpec, headerJars bool) android.Paths {
Jiyong Park932cdfe2020-05-28 00:19:53 +09001496 // If the client doesn't set sdk_version, but if this library prefers stubs over
1497 // the impl library, let's provide the widest API surface possible. To do so,
1498 // force override sdk_version to module_current so that the closest possible API
1499 // surface could be found in selectHeaderJarsForSdkVersion
1500 if module.defaultsToStubs() && !sdkVersion.specified() {
1501 sdkVersion = sdkSpecFrom("module_current")
1502 }
Paul Duffind1b3a922020-01-22 11:57:20 +00001503
Paul Duffindaaa3322020-05-26 18:13:57 +01001504 // Only provide access to the implementation library if it is actually built.
1505 if module.requiresRuntimeImplementationLibrary() {
1506 // Check any special cases for java_sdk_library.
1507 //
1508 // Only allow access to the implementation library in the following condition:
1509 // * No sdk_version specified on the referencing module.
Paul Duffin9b879592020-05-26 13:21:35 +01001510 // * The referencing module is in the same apex as this.
Colin Cross56a83212020-09-15 18:30:11 -07001511 if sdkVersion.kind == sdkPrivate || withinSameApexesAs(ctx, module) {
Paul Duffindaaa3322020-05-26 18:13:57 +01001512 if headerJars {
1513 return module.HeaderJars()
1514 } else {
1515 return module.ImplementationJars()
1516 }
Sundong Ahn054b19a2018-10-19 13:46:09 +09001517 }
Jiyong Parkc678ad32018-04-10 13:07:10 +09001518 }
Paul Duffinb05d4292020-05-20 12:19:10 +01001519
Paul Duffin23970f42020-05-20 14:20:02 +01001520 return module.selectHeaderJarsForSdkVersion(ctx, sdkVersion)
Jiyong Parkc678ad32018-04-10 13:07:10 +09001521}
1522
Sundong Ahn241cd372018-07-13 16:16:44 +09001523// to satisfy SdkLibraryDependency interface
Paul Duffind1b3a922020-01-22 11:57:20 +00001524func (module *SdkLibrary) SdkHeaderJars(ctx android.BaseModuleContext, sdkVersion sdkSpec) android.Paths {
1525 return module.sdkJars(ctx, sdkVersion, true /*headerJars*/)
1526}
1527
1528// to satisfy SdkLibraryDependency interface
Jiyong Park6a927c42020-01-21 02:03:43 +09001529func (module *SdkLibrary) SdkImplementationJars(ctx android.BaseModuleContext, sdkVersion sdkSpec) android.Paths {
Paul Duffind1b3a922020-01-22 11:57:20 +00001530 return module.sdkJars(ctx, sdkVersion, false /*headerJars*/)
Sundong Ahn241cd372018-07-13 16:16:44 +09001531}
1532
Colin Cross571cccf2019-02-04 11:22:08 -08001533var javaSdkLibrariesKey = android.NewOnceKey("javaSdkLibraries")
1534
Jiyong Park82484c02018-04-23 21:41:26 +09001535func javaSdkLibraries(config android.Config) *[]string {
Colin Cross571cccf2019-02-04 11:22:08 -08001536 return config.Once(javaSdkLibrariesKey, func() interface{} {
Jiyong Park82484c02018-04-23 21:41:26 +09001537 return &[]string{}
1538 }).(*[]string)
1539}
1540
Paul Duffin749f98f2019-12-30 17:23:46 +00001541func (module *SdkLibrary) getApiDir() string {
1542 return proptools.StringDefault(module.sdkLibraryProperties.Api_dir, "api")
1543}
1544
Jiyong Parkc678ad32018-04-10 13:07:10 +09001545// For a java_sdk_library module, create internal modules for stubs, docs,
1546// runtime libs and xml file. If requested, the stubs and docs are created twice
1547// once for public API level and once for system API level
Paul Duffinf0229202020-04-29 16:47:28 +01001548func (module *SdkLibrary) CreateInternalModules(mctx android.DefaultableHookContext) {
1549 // If the module has been disabled then don't create any child modules.
1550 if !module.Enabled() {
1551 return
1552 }
1553
Paul Duffina18abc22020-05-16 18:54:24 +01001554 if len(module.properties.Srcs) == 0 {
Inseob Kimc0907f12019-02-08 21:00:45 +09001555 mctx.PropertyErrorf("srcs", "java_sdk_library must specify srcs")
Jooyung Han58f26ab2019-12-18 15:34:32 +09001556 return
Inseob Kimc0907f12019-02-08 21:00:45 +09001557 }
1558
Paul Duffin37e0b772019-12-30 17:20:10 +00001559 // If this builds against standard libraries (i.e. is not part of the core libraries)
Paul Duffin4f5c1ef2020-11-19 14:53:43 +00001560 // then assume it provides both system and test apis.
Paul Duffin37e0b772019-12-30 17:20:10 +00001561 sdkDep := decodeSdkDep(mctx, sdkContext(&module.Library))
1562 hasSystemAndTestApis := sdkDep.hasStandardLibs()
Paul Duffin3375e352020-04-28 10:44:03 +01001563 module.sdkLibraryProperties.Generate_system_and_test_apis = hasSystemAndTestApis
Paul Duffin4f5c1ef2020-11-19 14:53:43 +00001564
Jaewoong Jung18aefc12020-12-21 09:11:10 -08001565 missingCurrentApi := false
Inseob Kim8098faa2019-03-18 10:19:51 +09001566
Paul Duffin3375e352020-04-28 10:44:03 +01001567 generatedScopes := module.getGeneratedApiScopes(mctx)
Paul Duffind1b3a922020-01-22 11:57:20 +00001568
Paul Duffin749f98f2019-12-30 17:23:46 +00001569 apiDir := module.getApiDir()
Paul Duffin3375e352020-04-28 10:44:03 +01001570 for _, scope := range generatedScopes {
Inseob Kim8098faa2019-03-18 10:19:51 +09001571 for _, api := range []string{"current.txt", "removed.txt"} {
Paul Duffind1b3a922020-01-22 11:57:20 +00001572 path := path.Join(mctx.ModuleDir(), apiDir, scope.apiFilePrefix+api)
Inseob Kim8098faa2019-03-18 10:19:51 +09001573 p := android.ExistentPathForSource(mctx, path)
1574 if !p.Valid() {
1575 mctx.ModuleErrorf("Current api file %#v doesn't exist", path)
Jaewoong Jung18aefc12020-12-21 09:11:10 -08001576 missingCurrentApi = true
Inseob Kim8098faa2019-03-18 10:19:51 +09001577 }
1578 }
1579 }
1580
Jaewoong Jung18aefc12020-12-21 09:11:10 -08001581 if missingCurrentApi {
Inseob Kim8098faa2019-03-18 10:19:51 +09001582 script := "build/soong/scripts/gen-java-current-api-files.sh"
1583 p := android.ExistentPathForSource(mctx, script)
1584
1585 if !p.Valid() {
1586 panic(fmt.Sprintf("script file %s doesn't exist", script))
1587 }
1588
1589 mctx.ModuleErrorf("One or more current api files are missing. "+
1590 "You can update them by:\n"+
Paul Duffin37e0b772019-12-30 17:20:10 +00001591 "%s %q %s && m update-api",
Paul Duffind1b3a922020-01-22 11:57:20 +00001592 script, filepath.Join(mctx.ModuleDir(), apiDir),
Paul Duffin3375e352020-04-28 10:44:03 +01001593 strings.Join(generatedScopes.Strings(func(s *apiScope) string { return s.apiFilePrefix }), " "))
Inseob Kim8098faa2019-03-18 10:19:51 +09001594 return
1595 }
1596
Paul Duffin3375e352020-04-28 10:44:03 +01001597 for _, scope := range generatedScopes {
Paul Duffin15f34ef2020-07-20 18:04:44 +01001598 // Use the stubs source name for legacy reasons.
1599 module.createStubsSourcesAndApi(mctx, scope, module.stubsSourceModuleName(scope), scope.droidstubsArgs)
Paul Duffin0ff08bd2020-04-29 13:30:54 +01001600
Paul Duffind1b3a922020-01-22 11:57:20 +00001601 module.createStubsLibrary(mctx, scope)
Inseob Kimc0907f12019-02-08 21:00:45 +09001602 }
1603
Paul Duffindfa131e2020-05-15 20:37:11 +01001604 if module.requiresRuntimeImplementationLibrary() {
Paul Duffin5df79302020-05-16 15:52:12 +01001605 // Create child module to create an implementation library.
1606 //
1607 // This temporarily creates a second implementation library that can be explicitly
1608 // referenced.
1609 //
1610 // TODO(b/156618935) - update comment once only one implementation library is created.
1611 module.createImplLibrary(mctx)
1612
Paul Duffindfa131e2020-05-15 20:37:11 +01001613 // Only create an XML permissions file that declares the library as being usable
1614 // as a shared library if required.
1615 if module.sharedLibrary() {
1616 module.createXmlFile(mctx)
1617 }
Paul Duffin43db9be2019-12-30 17:35:49 +00001618
1619 // record java_sdk_library modules so that they are exported to make
1620 javaSdkLibraries := javaSdkLibraries(mctx.Config())
1621 javaSdkLibrariesLock.Lock()
1622 defer javaSdkLibrariesLock.Unlock()
1623 *javaSdkLibraries = append(*javaSdkLibraries, module.BaseModuleName())
1624 }
Anton Hansson7f66efa2020-10-08 14:47:23 +01001625
1626 // Add the impl_only_libs *after* we're done using the Libs prop in submodules.
1627 module.properties.Libs = append(module.properties.Libs, module.sdkLibraryProperties.Impl_only_libs...)
Inseob Kimc0907f12019-02-08 21:00:45 +09001628}
1629
1630func (module *SdkLibrary) InitSdkLibraryProperties() {
Colin Crossce6734e2020-06-15 16:09:53 -07001631 module.addHostAndDeviceProperties()
1632 module.AddProperties(&module.sdkLibraryProperties)
Sundong Ahn054b19a2018-10-19 13:46:09 +09001633
Paul Duffin859fe962020-05-15 10:20:31 +01001634 module.initSdkLibraryComponent(&module.ModuleBase)
1635
Paul Duffina18abc22020-05-16 18:54:24 +01001636 module.properties.Installable = proptools.BoolPtr(true)
1637 module.deviceProperties.IsSDKLibrary = true
Inseob Kimc0907f12019-02-08 21:00:45 +09001638}
Sundong Ahn054b19a2018-10-19 13:46:09 +09001639
Paul Duffindfa131e2020-05-15 20:37:11 +01001640func (module *SdkLibrary) requiresRuntimeImplementationLibrary() bool {
1641 return !proptools.Bool(module.sdkLibraryProperties.Api_only)
1642}
1643
Jiyong Park932cdfe2020-05-28 00:19:53 +09001644func (module *SdkLibrary) defaultsToStubs() bool {
1645 return proptools.Bool(module.sdkLibraryProperties.Default_to_stubs)
1646}
1647
Paul Duffin1b1e8062020-05-08 13:44:43 +01001648// Defines how to name the individual component modules the sdk library creates.
1649type sdkLibraryComponentNamingScheme interface {
1650 stubsLibraryModuleName(scope *apiScope, baseName string) string
1651
1652 stubsSourceModuleName(scope *apiScope, baseName string) string
1653
1654 apiModuleName(scope *apiScope, baseName string) string
1655}
1656
1657type defaultNamingScheme struct {
1658}
1659
1660func (s *defaultNamingScheme) stubsLibraryModuleName(scope *apiScope, baseName string) string {
1661 return scope.stubsLibraryModuleName(baseName)
1662}
1663
1664func (s *defaultNamingScheme) stubsSourceModuleName(scope *apiScope, baseName string) string {
1665 return scope.stubsSourceModuleName(baseName)
1666}
1667
1668func (s *defaultNamingScheme) apiModuleName(scope *apiScope, baseName string) string {
1669 return scope.apiModuleName(baseName)
1670}
1671
1672var _ sdkLibraryComponentNamingScheme = (*defaultNamingScheme)(nil)
1673
Anton Hansson2d0c1942020-05-25 12:20:51 +01001674func moduleStubLinkType(name string) (stub bool, ret linkType) {
1675 // This suffix-based approach is fragile and could potentially mis-trigger.
1676 // TODO(b/155164730): Clean this up when modules no longer reference sdk_lib stubs directly.
1677 if strings.HasSuffix(name, ".stubs.public") || strings.HasSuffix(name, "-stubs-publicapi") {
1678 return true, javaSdk
1679 }
1680 if strings.HasSuffix(name, ".stubs.system") || strings.HasSuffix(name, "-stubs-systemapi") {
1681 return true, javaSystem
1682 }
1683 if strings.HasSuffix(name, ".stubs.module_lib") || strings.HasSuffix(name, "-stubs-module_libs_api") {
1684 return true, javaModule
1685 }
1686 if strings.HasSuffix(name, ".stubs.test") {
1687 return true, javaSystem
1688 }
1689 return false, javaPlatform
1690}
1691
Jaewoong Jung4f158ee2019-07-11 10:05:35 -07001692// java_sdk_library is a special Java library that provides optional platform APIs to apps.
1693// In practice, it can be viewed as a combination of several modules: 1) stubs library that clients
1694// are linked against to, 2) droiddoc module that internally generates API stubs source files,
1695// 3) the real runtime shared library that implements the APIs, and 4) XML file for adding
1696// the runtime lib to the classpath at runtime if requested via <uses-library>.
Inseob Kimc0907f12019-02-08 21:00:45 +09001697func SdkLibraryFactory() android.Module {
1698 module := &SdkLibrary{}
Paul Duffinc3091c82020-05-08 14:16:20 +01001699
1700 // Initialize information common between source and prebuilt.
1701 module.initCommon(&module.ModuleBase)
1702
Inseob Kimc0907f12019-02-08 21:00:45 +09001703 module.InitSdkLibraryProperties()
Jooyung Han58f26ab2019-12-18 15:34:32 +09001704 android.InitApexModule(module)
Sundong Ahn054b19a2018-10-19 13:46:09 +09001705 InitJavaModule(module, android.HostAndDeviceSupported)
Paul Duffin3375e352020-04-28 10:44:03 +01001706
1707 // Initialize the map from scope to scope specific properties.
1708 scopeToProperties := make(map[*apiScope]*ApiScopeProperties)
1709 for _, scope := range allApiScopes {
1710 scopeToProperties[scope] = scope.scopeSpecificProperties(module)
1711 }
1712 module.scopeToProperties = scopeToProperties
1713
Paul Duffin4911a892020-04-29 23:35:13 +01001714 // Add the properties containing visibility rules so that they are checked.
Paul Duffin5df79302020-05-16 15:52:12 +01001715 android.AddVisibilityProperty(module, "impl_library_visibility", &module.sdkLibraryProperties.Impl_library_visibility)
Paul Duffin4911a892020-04-29 23:35:13 +01001716 android.AddVisibilityProperty(module, "stubs_library_visibility", &module.sdkLibraryProperties.Stubs_library_visibility)
1717 android.AddVisibilityProperty(module, "stubs_source_visibility", &module.sdkLibraryProperties.Stubs_source_visibility)
1718
Paul Duffin1b1e8062020-05-08 13:44:43 +01001719 module.SetDefaultableHook(func(ctx android.DefaultableHookContext) {
Paul Duffindfa131e2020-05-15 20:37:11 +01001720 // If no implementation is required then it cannot be used as a shared library
1721 // either.
1722 if !module.requiresRuntimeImplementationLibrary() {
1723 // If shared_library has been explicitly set to true then it is incompatible
1724 // with api_only: true.
1725 if proptools.Bool(module.commonSdkLibraryProperties.Shared_library) {
1726 ctx.PropertyErrorf("api_only/shared_library", "inconsistent settings, shared_library and api_only cannot both be true")
1727 }
1728 // Set shared_library: false.
1729 module.commonSdkLibraryProperties.Shared_library = proptools.BoolPtr(false)
1730 }
1731
Paul Duffin1b1e8062020-05-08 13:44:43 +01001732 if module.initCommonAfterDefaultsApplied(ctx) {
1733 module.CreateInternalModules(ctx)
1734 }
1735 })
Jiyong Parkc678ad32018-04-10 13:07:10 +09001736 return module
1737}
Colin Cross79c7c262019-04-17 11:11:46 -07001738
1739//
1740// SDK library prebuilts
1741//
1742
Paul Duffin56d44902020-01-31 13:36:25 +00001743// Properties associated with each api scope.
1744type sdkLibraryScopeProperties struct {
Colin Cross79c7c262019-04-17 11:11:46 -07001745 Jars []string `android:"path"`
1746
1747 Sdk_version *string
1748
Colin Cross79c7c262019-04-17 11:11:46 -07001749 // List of shared java libs that this module has dependencies to
1750 Libs []string
Paul Duffin3d1248c2020-04-09 00:10:17 +01001751
Paul Duffinc8782502020-04-29 20:45:27 +01001752 // The stubs source.
Paul Duffin3d1248c2020-04-09 00:10:17 +01001753 Stub_srcs []string `android:"path"`
Paul Duffin1fd005d2020-04-09 01:08:11 +01001754
1755 // The current.txt
Paul Duffin0f8faff2020-05-20 16:18:00 +01001756 Current_api *string `android:"path"`
Paul Duffin1fd005d2020-04-09 01:08:11 +01001757
1758 // The removed.txt
Paul Duffin0f8faff2020-05-20 16:18:00 +01001759 Removed_api *string `android:"path"`
Colin Cross79c7c262019-04-17 11:11:46 -07001760}
1761
Paul Duffin56d44902020-01-31 13:36:25 +00001762type sdkLibraryImportProperties struct {
Paul Duffinfcfd7912020-01-31 17:54:30 +00001763 // List of shared java libs, common to all scopes, that this module has
1764 // dependencies to
1765 Libs []string
Paul Duffin56d44902020-01-31 13:36:25 +00001766}
1767
Paul Duffineedc5d52020-06-12 17:46:39 +01001768type SdkLibraryImport struct {
Colin Cross79c7c262019-04-17 11:11:46 -07001769 android.ModuleBase
1770 android.DefaultableModuleBase
1771 prebuilt android.Prebuilt
Paul Duffindd46f712020-02-10 13:37:10 +00001772 android.ApexModuleBase
1773 android.SdkBase
Colin Cross79c7c262019-04-17 11:11:46 -07001774
1775 properties sdkLibraryImportProperties
1776
Paul Duffin46a26a82020-04-07 19:27:04 +01001777 // Map from api scope to the scope specific property structure.
1778 scopeProperties map[*apiScope]*sdkLibraryScopeProperties
1779
Paul Duffin56d44902020-01-31 13:36:25 +00001780 commonToSdkLibraryAndImport
Paul Duffineedc5d52020-06-12 17:46:39 +01001781
1782 // The reference to the implementation library created by the source module.
1783 // Is nil if the source module does not exist.
1784 implLibraryModule *Library
1785
1786 // The reference to the xml permissions module created by the source module.
1787 // Is nil if the source module does not exist.
1788 xmlPermissionsFileModule *sdkLibraryXml
Colin Cross79c7c262019-04-17 11:11:46 -07001789}
1790
Paul Duffineedc5d52020-06-12 17:46:39 +01001791var _ SdkLibraryDependency = (*SdkLibraryImport)(nil)
Colin Cross79c7c262019-04-17 11:11:46 -07001792
Paul Duffin46a26a82020-04-07 19:27:04 +01001793// The type of a structure that contains a field of type sdkLibraryScopeProperties
1794// for each apiscope in allApiScopes, e.g. something like:
1795// struct {
1796// Public sdkLibraryScopeProperties
1797// System sdkLibraryScopeProperties
1798// ...
1799// }
1800var allScopeStructType = createAllScopePropertiesStructType()
1801
1802// Dynamically create a structure type for each apiscope in allApiScopes.
1803func createAllScopePropertiesStructType() reflect.Type {
1804 var fields []reflect.StructField
1805 for _, apiScope := range allApiScopes {
1806 field := reflect.StructField{
1807 Name: apiScope.fieldName,
1808 Type: reflect.TypeOf(sdkLibraryScopeProperties{}),
1809 }
1810 fields = append(fields, field)
1811 }
1812
1813 return reflect.StructOf(fields)
1814}
1815
1816// Create an instance of the scope specific structure type and return a map
1817// from apiscope to a pointer to each scope specific field.
1818func createPropertiesInstance() (interface{}, map[*apiScope]*sdkLibraryScopeProperties) {
1819 allScopePropertiesPtr := reflect.New(allScopeStructType)
1820 allScopePropertiesStruct := allScopePropertiesPtr.Elem()
1821 scopeProperties := make(map[*apiScope]*sdkLibraryScopeProperties)
1822
1823 for _, apiScope := range allApiScopes {
1824 field := allScopePropertiesStruct.FieldByName(apiScope.fieldName)
1825 scopeProperties[apiScope] = field.Addr().Interface().(*sdkLibraryScopeProperties)
1826 }
1827
1828 return allScopePropertiesPtr.Interface(), scopeProperties
1829}
1830
Jaewoong Jung4f158ee2019-07-11 10:05:35 -07001831// java_sdk_library_import imports a prebuilt java_sdk_library.
Colin Cross79c7c262019-04-17 11:11:46 -07001832func sdkLibraryImportFactory() android.Module {
Paul Duffineedc5d52020-06-12 17:46:39 +01001833 module := &SdkLibraryImport{}
Colin Cross79c7c262019-04-17 11:11:46 -07001834
Paul Duffin46a26a82020-04-07 19:27:04 +01001835 allScopeProperties, scopeToProperties := createPropertiesInstance()
1836 module.scopeProperties = scopeToProperties
1837 module.AddProperties(&module.properties, allScopeProperties)
Colin Cross79c7c262019-04-17 11:11:46 -07001838
Paul Duffinc3091c82020-05-08 14:16:20 +01001839 // Initialize information common between source and prebuilt.
1840 module.initCommon(&module.ModuleBase)
1841
Paul Duffin0bdcb272020-02-06 15:24:57 +00001842 android.InitPrebuiltModule(module, &[]string{""})
Paul Duffindd46f712020-02-10 13:37:10 +00001843 android.InitApexModule(module)
1844 android.InitSdkAwareModule(module)
Colin Cross79c7c262019-04-17 11:11:46 -07001845 InitJavaModule(module, android.HostAndDeviceSupported)
1846
Paul Duffin1b1e8062020-05-08 13:44:43 +01001847 module.SetDefaultableHook(func(mctx android.DefaultableHookContext) {
1848 if module.initCommonAfterDefaultsApplied(mctx) {
1849 module.createInternalModules(mctx)
1850 }
1851 })
Colin Cross79c7c262019-04-17 11:11:46 -07001852 return module
1853}
1854
Paul Duffineedc5d52020-06-12 17:46:39 +01001855func (module *SdkLibraryImport) Prebuilt() *android.Prebuilt {
Colin Cross79c7c262019-04-17 11:11:46 -07001856 return &module.prebuilt
1857}
1858
Paul Duffineedc5d52020-06-12 17:46:39 +01001859func (module *SdkLibraryImport) Name() string {
Colin Cross79c7c262019-04-17 11:11:46 -07001860 return module.prebuilt.Name(module.ModuleBase.Name())
1861}
1862
Paul Duffineedc5d52020-06-12 17:46:39 +01001863func (module *SdkLibraryImport) createInternalModules(mctx android.DefaultableHookContext) {
Colin Cross79c7c262019-04-17 11:11:46 -07001864
Paul Duffin50061512020-01-21 16:31:05 +00001865 // If the build is configured to use prebuilts then force this to be preferred.
Jeongik Cha816a23a2020-07-08 01:09:23 +09001866 if mctx.Config().AlwaysUsePrebuiltSdks() {
Paul Duffin50061512020-01-21 16:31:05 +00001867 module.prebuilt.ForcePrefer()
1868 }
1869
Paul Duffin46a26a82020-04-07 19:27:04 +01001870 for apiScope, scopeProperties := range module.scopeProperties {
Paul Duffin56d44902020-01-31 13:36:25 +00001871 if len(scopeProperties.Jars) == 0 {
1872 continue
1873 }
1874
Paul Duffinbbb546b2020-04-09 00:07:11 +01001875 module.createJavaImportForStubs(mctx, apiScope, scopeProperties)
Paul Duffin3d1248c2020-04-09 00:10:17 +01001876
Paul Duffin0f8faff2020-05-20 16:18:00 +01001877 if len(scopeProperties.Stub_srcs) > 0 {
1878 module.createPrebuiltStubsSources(mctx, apiScope, scopeProperties)
1879 }
Paul Duffin56d44902020-01-31 13:36:25 +00001880 }
Colin Cross79c7c262019-04-17 11:11:46 -07001881
1882 javaSdkLibraries := javaSdkLibraries(mctx.Config())
1883 javaSdkLibrariesLock.Lock()
1884 defer javaSdkLibrariesLock.Unlock()
1885 *javaSdkLibraries = append(*javaSdkLibraries, module.BaseModuleName())
1886}
1887
Paul Duffineedc5d52020-06-12 17:46:39 +01001888func (module *SdkLibraryImport) createJavaImportForStubs(mctx android.DefaultableHookContext, apiScope *apiScope, scopeProperties *sdkLibraryScopeProperties) {
Paul Duffinbbb546b2020-04-09 00:07:11 +01001889 // Creates a java import for the jar with ".stubs" suffix
1890 props := struct {
Paul Duffin1dbe3ca2020-05-16 09:57:59 +01001891 Name *string
1892 Sdk_version *string
1893 Libs []string
1894 Jars []string
1895 Prefer *bool
Paul Duffinbbb546b2020-04-09 00:07:11 +01001896 }{}
Paul Duffinc3091c82020-05-08 14:16:20 +01001897 props.Name = proptools.StringPtr(module.stubsLibraryModuleName(apiScope))
Paul Duffinbbb546b2020-04-09 00:07:11 +01001898 props.Sdk_version = scopeProperties.Sdk_version
1899 // Prepend any of the libs from the legacy public properties to the libs for each of the
1900 // scopes to avoid having to duplicate them in each scope.
1901 props.Libs = append(module.properties.Libs, scopeProperties.Libs...)
1902 props.Jars = scopeProperties.Jars
Paul Duffin1dbe3ca2020-05-16 09:57:59 +01001903
Paul Duffin38b57852020-05-13 16:08:09 +01001904 // The imports are preferred if the java_sdk_library_import is preferred.
1905 props.Prefer = proptools.BoolPtr(module.prebuilt.Prefer())
Paul Duffin859fe962020-05-15 10:20:31 +01001906
1907 mctx.CreateModule(ImportFactory, &props, module.sdkComponentPropertiesForChildLibrary())
Paul Duffinbbb546b2020-04-09 00:07:11 +01001908}
1909
Paul Duffineedc5d52020-06-12 17:46:39 +01001910func (module *SdkLibraryImport) createPrebuiltStubsSources(mctx android.DefaultableHookContext, apiScope *apiScope, scopeProperties *sdkLibraryScopeProperties) {
Paul Duffin3d1248c2020-04-09 00:10:17 +01001911 props := struct {
Paul Duffin38b57852020-05-13 16:08:09 +01001912 Name *string
1913 Srcs []string
1914 Prefer *bool
Paul Duffin3d1248c2020-04-09 00:10:17 +01001915 }{}
Paul Duffinc3091c82020-05-08 14:16:20 +01001916 props.Name = proptools.StringPtr(module.stubsSourceModuleName(apiScope))
Paul Duffin3d1248c2020-04-09 00:10:17 +01001917 props.Srcs = scopeProperties.Stub_srcs
1918 mctx.CreateModule(PrebuiltStubsSourcesFactory, &props)
Paul Duffin38b57852020-05-13 16:08:09 +01001919
1920 // The stubs source is preferred if the java_sdk_library_import is preferred.
1921 props.Prefer = proptools.BoolPtr(module.prebuilt.Prefer())
Paul Duffin3d1248c2020-04-09 00:10:17 +01001922}
1923
Paul Duffin44f1d842020-06-26 20:17:02 +01001924// Add the dependencies on the child module in the component deps mutator so that it
1925// creates references to the prebuilt and not the source modules.
1926func (module *SdkLibraryImport) ComponentDepsMutator(ctx android.BottomUpMutatorContext) {
Paul Duffin46a26a82020-04-07 19:27:04 +01001927 for apiScope, scopeProperties := range module.scopeProperties {
Paul Duffin56d44902020-01-31 13:36:25 +00001928 if len(scopeProperties.Jars) == 0 {
1929 continue
1930 }
1931
1932 // Add dependencies to the prebuilt stubs library
Paul Duffin44f1d842020-06-26 20:17:02 +01001933 ctx.AddVariationDependencies(nil, apiScope.stubsTag, "prebuilt_"+module.stubsLibraryModuleName(apiScope))
Paul Duffin0f8faff2020-05-20 16:18:00 +01001934
1935 if len(scopeProperties.Stub_srcs) > 0 {
1936 // Add dependencies to the prebuilt stubs source library
Paul Duffin44f1d842020-06-26 20:17:02 +01001937 ctx.AddVariationDependencies(nil, apiScope.stubsSourceTag, "prebuilt_"+module.stubsSourceModuleName(apiScope))
Paul Duffin0f8faff2020-05-20 16:18:00 +01001938 }
Paul Duffin56d44902020-01-31 13:36:25 +00001939 }
Paul Duffin44f1d842020-06-26 20:17:02 +01001940}
1941
1942// Add other dependencies as normal.
1943func (module *SdkLibraryImport) DepsMutator(ctx android.BottomUpMutatorContext) {
Paul Duffineedc5d52020-06-12 17:46:39 +01001944
1945 implName := module.implLibraryModuleName()
1946 if ctx.OtherModuleExists(implName) {
1947 ctx.AddVariationDependencies(nil, implLibraryTag, implName)
1948
1949 xmlPermissionsModuleName := module.xmlPermissionsModuleName()
1950 if module.sharedLibrary() && ctx.OtherModuleExists(xmlPermissionsModuleName) {
1951 // Add dependency to the rule for generating the xml permissions file
1952 ctx.AddDependency(module, xmlPermissionsFileTag, xmlPermissionsModuleName)
1953 }
1954 }
Colin Cross79c7c262019-04-17 11:11:46 -07001955}
1956
Jiyong Park45bf82e2020-12-15 22:29:02 +09001957var _ android.ApexModule = (*SdkLibraryImport)(nil)
1958
1959// Implements android.ApexModule
Paul Duffineedc5d52020-06-12 17:46:39 +01001960func (module *SdkLibraryImport) DepIsInSameApex(mctx android.BaseModuleContext, dep android.Module) bool {
1961 depTag := mctx.OtherModuleDependencyTag(dep)
1962 if depTag == xmlPermissionsFileTag {
1963 return true
1964 }
1965
1966 // None of the other dependencies of the java_sdk_library_import are in the same apex
1967 // as the one that references this module.
1968 return false
1969}
1970
Jiyong Park45bf82e2020-12-15 22:29:02 +09001971// Implements android.ApexModule
Dan Albertc8060532020-07-22 22:32:17 -07001972func (module *SdkLibraryImport) ShouldSupportSdkVersion(ctx android.BaseModuleContext,
1973 sdkVersion android.ApiLevel) error {
Jooyung Han749dc692020-04-15 11:03:39 +09001974 // we don't check prebuilt modules for sdk_version
1975 return nil
1976}
1977
Paul Duffineedc5d52020-06-12 17:46:39 +01001978func (module *SdkLibraryImport) OutputFiles(tag string) (android.Paths, error) {
Paul Duffin46dc45a2020-05-14 15:39:10 +01001979 return module.commonOutputFiles(tag)
1980}
1981
Paul Duffineedc5d52020-06-12 17:46:39 +01001982func (module *SdkLibraryImport) GenerateAndroidBuildActions(ctx android.ModuleContext) {
Paul Duffina2ae7e02020-09-11 11:55:00 +01001983 module.generateCommonBuildActions(ctx)
1984
Paul Duffin0f8faff2020-05-20 16:18:00 +01001985 // Record the paths to the prebuilt stubs library and stubs source.
Colin Cross79c7c262019-04-17 11:11:46 -07001986 ctx.VisitDirectDeps(func(to android.Module) {
1987 tag := ctx.OtherModuleDependencyTag(to)
1988
Paul Duffin0f8faff2020-05-20 16:18:00 +01001989 // Extract information from any of the scope specific dependencies.
1990 if scopeTag, ok := tag.(scopeDependencyTag); ok {
1991 apiScope := scopeTag.apiScope
1992 scopePaths := module.getScopePathsCreateIfNeeded(apiScope)
1993
1994 // Extract information from the dependency. The exact information extracted
1995 // is determined by the nature of the dependency which is determined by the tag.
1996 scopeTag.extractDepInfo(ctx, to, scopePaths)
Paul Duffineedc5d52020-06-12 17:46:39 +01001997 } else if tag == implLibraryTag {
1998 if implLibrary, ok := to.(*Library); ok {
1999 module.implLibraryModule = implLibrary
2000 } else {
2001 ctx.ModuleErrorf("implementation library must be of type *java.Library but was %T", to)
2002 }
2003 } else if tag == xmlPermissionsFileTag {
2004 if xmlPermissionsFileModule, ok := to.(*sdkLibraryXml); ok {
2005 module.xmlPermissionsFileModule = xmlPermissionsFileModule
2006 } else {
2007 ctx.ModuleErrorf("xml permissions file module must be of type *sdkLibraryXml but was %T", to)
2008 }
Colin Cross79c7c262019-04-17 11:11:46 -07002009 }
2010 })
Paul Duffin0f8faff2020-05-20 16:18:00 +01002011
2012 // Populate the scope paths with information from the properties.
2013 for apiScope, scopeProperties := range module.scopeProperties {
2014 if len(scopeProperties.Jars) == 0 {
2015 continue
2016 }
2017
2018 paths := module.getScopePathsCreateIfNeeded(apiScope)
2019 paths.currentApiFilePath = android.OptionalPathForModuleSrc(ctx, scopeProperties.Current_api)
2020 paths.removedApiFilePath = android.OptionalPathForModuleSrc(ctx, scopeProperties.Removed_api)
2021 }
Colin Cross79c7c262019-04-17 11:11:46 -07002022}
2023
Paul Duffineedc5d52020-06-12 17:46:39 +01002024func (module *SdkLibraryImport) sdkJars(ctx android.BaseModuleContext, sdkVersion sdkSpec, headerJars bool) android.Paths {
2025
2026 // For consistency with SdkLibrary make the implementation jar available to libraries that
2027 // are within the same APEX.
2028 implLibraryModule := module.implLibraryModule
Colin Cross56a83212020-09-15 18:30:11 -07002029 if implLibraryModule != nil && withinSameApexesAs(ctx, module) {
Paul Duffineedc5d52020-06-12 17:46:39 +01002030 if headerJars {
2031 return implLibraryModule.HeaderJars()
2032 } else {
2033 return implLibraryModule.ImplementationJars()
2034 }
2035 }
2036
Paul Duffin23970f42020-05-20 14:20:02 +01002037 return module.selectHeaderJarsForSdkVersion(ctx, sdkVersion)
Paul Duffin56d44902020-01-31 13:36:25 +00002038}
2039
Colin Cross79c7c262019-04-17 11:11:46 -07002040// to satisfy SdkLibraryDependency interface
Paul Duffineedc5d52020-06-12 17:46:39 +01002041func (module *SdkLibraryImport) SdkHeaderJars(ctx android.BaseModuleContext, sdkVersion sdkSpec) android.Paths {
Colin Cross79c7c262019-04-17 11:11:46 -07002042 // This module is just a wrapper for the prebuilt stubs.
Paul Duffineedc5d52020-06-12 17:46:39 +01002043 return module.sdkJars(ctx, sdkVersion, true)
Colin Cross79c7c262019-04-17 11:11:46 -07002044}
2045
2046// to satisfy SdkLibraryDependency interface
Paul Duffineedc5d52020-06-12 17:46:39 +01002047func (module *SdkLibraryImport) SdkImplementationJars(ctx android.BaseModuleContext, sdkVersion sdkSpec) android.Paths {
Colin Cross79c7c262019-04-17 11:11:46 -07002048 // This module is just a wrapper for the stubs.
Paul Duffineedc5d52020-06-12 17:46:39 +01002049 return module.sdkJars(ctx, sdkVersion, false)
2050}
2051
Ulya Trafimovichdbf31662020-12-17 12:07:54 +00002052// to satisfy UsesLibraryDependency interface
Paul Duffineedc5d52020-06-12 17:46:39 +01002053func (module *SdkLibraryImport) DexJarBuildPath() android.Path {
2054 if module.implLibraryModule == nil {
2055 return nil
2056 } else {
2057 return module.implLibraryModule.DexJarBuildPath()
2058 }
2059}
2060
Ulya Trafimovichdbf31662020-12-17 12:07:54 +00002061// to satisfy UsesLibraryDependency interface
Ulya Trafimovich31e444e2020-08-14 17:32:16 +01002062func (module *SdkLibraryImport) DexJarInstallPath() android.Path {
2063 if module.implLibraryModule == nil {
2064 return nil
2065 } else {
2066 return module.implLibraryModule.DexJarInstallPath()
2067 }
2068}
2069
Ulya Trafimovichdbf31662020-12-17 12:07:54 +00002070// to satisfy UsesLibraryDependency interface
2071func (module *SdkLibraryImport) ClassLoaderContexts() dexpreopt.ClassLoaderContextMap {
2072 return nil
2073}
2074
Paul Duffineedc5d52020-06-12 17:46:39 +01002075// to satisfy apex.javaDependency interface
2076func (module *SdkLibraryImport) JacocoReportClassesFile() android.Path {
2077 if module.implLibraryModule == nil {
2078 return nil
2079 } else {
2080 return module.implLibraryModule.JacocoReportClassesFile()
2081 }
2082}
2083
2084// to satisfy apex.javaDependency interface
Colin Cross08dca382020-07-21 20:31:17 -07002085func (module *SdkLibraryImport) LintDepSets() LintDepSets {
2086 if module.implLibraryModule == nil {
2087 return LintDepSets{}
2088 } else {
2089 return module.implLibraryModule.LintDepSets()
2090 }
2091}
2092
2093// to satisfy apex.javaDependency interface
Paul Duffineedc5d52020-06-12 17:46:39 +01002094func (module *SdkLibraryImport) Stem() string {
2095 return module.BaseModuleName()
Colin Cross79c7c262019-04-17 11:11:46 -07002096}
Jiyong Parke3833882020-02-17 17:28:10 +09002097
Paul Duffin44b481b2020-06-17 16:59:43 +01002098var _ ApexDependency = (*SdkLibraryImport)(nil)
2099
2100// to satisfy java.ApexDependency interface
2101func (module *SdkLibraryImport) HeaderJars() android.Paths {
2102 if module.implLibraryModule == nil {
2103 return nil
2104 } else {
2105 return module.implLibraryModule.HeaderJars()
2106 }
2107}
2108
2109// to satisfy java.ApexDependency interface
2110func (module *SdkLibraryImport) ImplementationAndResourcesJars() android.Paths {
2111 if module.implLibraryModule == nil {
2112 return nil
2113 } else {
2114 return module.implLibraryModule.ImplementationAndResourcesJars()
2115 }
2116}
2117
Jiyong Parke3833882020-02-17 17:28:10 +09002118//
2119// java_sdk_library_xml
2120//
2121type sdkLibraryXml struct {
2122 android.ModuleBase
2123 android.DefaultableModuleBase
2124 android.ApexModuleBase
2125
2126 properties sdkLibraryXmlProperties
2127
2128 outputFilePath android.OutputPath
2129 installDirPath android.InstallPath
Colin Cross56a83212020-09-15 18:30:11 -07002130
2131 hideApexVariantFromMake bool
Jiyong Parke3833882020-02-17 17:28:10 +09002132}
2133
2134type sdkLibraryXmlProperties struct {
2135 // canonical name of the lib
2136 Lib_name *string
2137}
2138
2139// java_sdk_library_xml builds the permission xml file for a java_sdk_library.
2140// Not to be used directly by users. java_sdk_library internally uses this.
2141func sdkLibraryXmlFactory() android.Module {
2142 module := &sdkLibraryXml{}
2143
2144 module.AddProperties(&module.properties)
2145
2146 android.InitApexModule(module)
2147 android.InitAndroidArchModule(module, android.DeviceSupported, android.MultilibCommon)
2148
2149 return module
2150}
2151
Colin Crossaede88c2020-08-11 12:17:01 -07002152func (module *sdkLibraryXml) UniqueApexVariations() bool {
2153 // sdkLibraryXml needs a unique variation per APEX because the generated XML file contains the path to the
2154 // mounted APEX, which contains the name of the APEX.
2155 return true
2156}
2157
Jiyong Parke3833882020-02-17 17:28:10 +09002158// from android.PrebuiltEtcModule
Jooyung Han0703fd82020-08-26 22:11:53 +09002159func (module *sdkLibraryXml) BaseDir() string {
2160 return "etc"
2161}
2162
2163// from android.PrebuiltEtcModule
Jiyong Parke3833882020-02-17 17:28:10 +09002164func (module *sdkLibraryXml) SubDir() string {
2165 return "permissions"
2166}
2167
2168// from android.PrebuiltEtcModule
2169func (module *sdkLibraryXml) OutputFile() android.OutputPath {
2170 return module.outputFilePath
2171}
2172
2173// from android.ApexModule
2174func (module *sdkLibraryXml) AvailableFor(what string) bool {
2175 return true
2176}
2177
2178func (module *sdkLibraryXml) DepsMutator(ctx android.BottomUpMutatorContext) {
2179 // do nothing
2180}
2181
Jiyong Park45bf82e2020-12-15 22:29:02 +09002182var _ android.ApexModule = (*sdkLibraryXml)(nil)
2183
2184// Implements android.ApexModule
Dan Albertc8060532020-07-22 22:32:17 -07002185func (module *sdkLibraryXml) ShouldSupportSdkVersion(ctx android.BaseModuleContext,
2186 sdkVersion android.ApiLevel) error {
Jooyung Han749dc692020-04-15 11:03:39 +09002187 // sdkLibraryXml doesn't need to be checked separately because java_sdk_library is checked
2188 return nil
2189}
2190
Jiyong Parke3833882020-02-17 17:28:10 +09002191// File path to the runtime implementation library
Colin Cross56a83212020-09-15 18:30:11 -07002192func (module *sdkLibraryXml) implPath(ctx android.ModuleContext) string {
Jiyong Parke3833882020-02-17 17:28:10 +09002193 implName := proptools.String(module.properties.Lib_name)
Colin Cross56a83212020-09-15 18:30:11 -07002194 if apexInfo := ctx.Provider(android.ApexInfoProvider).(android.ApexInfo); !apexInfo.IsForPlatform() {
Colin Crosse07f2312020-08-13 11:24:56 -07002195 // TODO(b/146468504): ApexVariationName() is only a soong module name, not apex name.
Jiyong Parke3833882020-02-17 17:28:10 +09002196 // In most cases, this works fine. But when apex_name is set or override_apex is used
2197 // this can be wrong.
Colin Cross56a83212020-09-15 18:30:11 -07002198 return fmt.Sprintf("/apex/%s/javalib/%s.jar", apexInfo.ApexVariationName, implName)
Jiyong Parke3833882020-02-17 17:28:10 +09002199 }
2200 partition := "system"
2201 if module.SocSpecific() {
2202 partition = "vendor"
2203 } else if module.DeviceSpecific() {
2204 partition = "odm"
2205 } else if module.ProductSpecific() {
2206 partition = "product"
2207 } else if module.SystemExtSpecific() {
2208 partition = "system_ext"
2209 }
2210 return "/" + partition + "/framework/" + implName + ".jar"
2211}
2212
2213func (module *sdkLibraryXml) GenerateAndroidBuildActions(ctx android.ModuleContext) {
Colin Cross56a83212020-09-15 18:30:11 -07002214 module.hideApexVariantFromMake = !ctx.Provider(android.ApexInfoProvider).(android.ApexInfo).IsForPlatform()
2215
Jiyong Parke3833882020-02-17 17:28:10 +09002216 libName := proptools.String(module.properties.Lib_name)
Colin Cross56a83212020-09-15 18:30:11 -07002217 xmlContent := fmt.Sprintf(permissionsTemplate, libName, module.implPath(ctx))
Jiyong Parke3833882020-02-17 17:28:10 +09002218
2219 module.outputFilePath = android.PathForModuleOut(ctx, libName+".xml").OutputPath
Colin Crossf1a035e2020-11-16 17:32:30 -08002220 rule := android.NewRuleBuilder(pctx, ctx)
Jiyong Parke3833882020-02-17 17:28:10 +09002221 rule.Command().
2222 Text("/bin/bash -c \"echo -e '" + xmlContent + "'\" > ").
2223 Output(module.outputFilePath)
2224
Colin Crossf1a035e2020-11-16 17:32:30 -08002225 rule.Build("java_sdk_xml", "Permission XML")
Jiyong Parke3833882020-02-17 17:28:10 +09002226
2227 module.installDirPath = android.PathForModuleInstall(ctx, "etc", module.SubDir())
2228}
2229
2230func (module *sdkLibraryXml) AndroidMkEntries() []android.AndroidMkEntries {
Colin Cross56a83212020-09-15 18:30:11 -07002231 if module.hideApexVariantFromMake {
Jiyong Parke3833882020-02-17 17:28:10 +09002232 return []android.AndroidMkEntries{android.AndroidMkEntries{
2233 Disabled: true,
2234 }}
2235 }
2236
2237 return []android.AndroidMkEntries{android.AndroidMkEntries{
2238 Class: "ETC",
2239 OutputFile: android.OptionalPathForPath(module.outputFilePath),
2240 ExtraEntries: []android.AndroidMkExtraEntriesFunc{
Colin Crossaa255532020-07-03 13:18:24 -07002241 func(ctx android.AndroidMkExtraEntriesContext, entries *android.AndroidMkEntries) {
Jiyong Parke3833882020-02-17 17:28:10 +09002242 entries.SetString("LOCAL_MODULE_TAGS", "optional")
2243 entries.SetString("LOCAL_MODULE_PATH", module.installDirPath.ToMakePath().String())
2244 entries.SetString("LOCAL_INSTALLED_MODULE_STEM", module.outputFilePath.Base())
2245 },
2246 },
2247 }}
2248}
Paul Duffindd46f712020-02-10 13:37:10 +00002249
2250type sdkLibrarySdkMemberType struct {
2251 android.SdkMemberTypeBase
2252}
2253
2254func (s *sdkLibrarySdkMemberType) AddDependencies(mctx android.BottomUpMutatorContext, dependencyTag blueprint.DependencyTag, names []string) {
2255 mctx.AddVariationDependencies(nil, dependencyTag, names...)
2256}
2257
2258func (s *sdkLibrarySdkMemberType) IsInstance(module android.Module) bool {
2259 _, ok := module.(*SdkLibrary)
2260 return ok
2261}
2262
2263func (s *sdkLibrarySdkMemberType) AddPrebuiltModule(ctx android.SdkMemberContext, member android.SdkMember) android.BpModule {
2264 return ctx.SnapshotBuilder().AddPrebuiltModule(member, "java_sdk_library_import")
2265}
2266
2267func (s *sdkLibrarySdkMemberType) CreateVariantPropertiesStruct() android.SdkMemberProperties {
2268 return &sdkLibrarySdkMemberProperties{}
2269}
2270
2271type sdkLibrarySdkMemberProperties struct {
2272 android.SdkMemberPropertiesBase
2273
2274 // Scope to per scope properties.
2275 Scopes map[*apiScope]scopeProperties
2276
2277 // Additional libraries that the exported stubs libraries depend upon.
2278 Libs []string
Paul Duffin3d1248c2020-04-09 00:10:17 +01002279
2280 // The Java stubs source files.
2281 Stub_srcs []string
Paul Duffinf7a64332020-05-13 16:54:55 +01002282
2283 // The naming scheme.
2284 Naming_scheme *string
Paul Duffind7eb1c22020-05-26 20:57:10 +01002285
2286 // True if the java_sdk_library_import is for a shared library, false
2287 // otherwise.
2288 Shared_library *bool
Paul Duffina2ae7e02020-09-11 11:55:00 +01002289
2290 // The paths to the doctag files to add to the prebuilt.
2291 Doctag_paths android.Paths
Paul Duffindd46f712020-02-10 13:37:10 +00002292}
2293
2294type scopeProperties struct {
Paul Duffin1fd005d2020-04-09 01:08:11 +01002295 Jars android.Paths
2296 StubsSrcJar android.Path
2297 CurrentApiFile android.Path
2298 RemovedApiFile android.Path
2299 SdkVersion string
Paul Duffindd46f712020-02-10 13:37:10 +00002300}
2301
2302func (s *sdkLibrarySdkMemberProperties) PopulateFromVariant(ctx android.SdkMemberContext, variant android.Module) {
2303 sdk := variant.(*SdkLibrary)
2304
2305 s.Scopes = make(map[*apiScope]scopeProperties)
2306 for _, apiScope := range allApiScopes {
Paul Duffin803a9562020-05-20 11:52:25 +01002307 paths := sdk.findScopePaths(apiScope)
2308 if paths == nil {
2309 continue
2310 }
2311
Paul Duffindd46f712020-02-10 13:37:10 +00002312 jars := paths.stubsImplPath
2313 if len(jars) > 0 {
2314 properties := scopeProperties{}
2315 properties.Jars = jars
Paul Duffin780c5f42020-05-12 15:52:55 +01002316 properties.SdkVersion = sdk.sdkVersionForStubsLibrary(ctx.SdkModuleContext(), apiScope)
Paul Duffin0f8faff2020-05-20 16:18:00 +01002317 properties.StubsSrcJar = paths.stubsSrcJar.Path()
Paul Duffin10269f12020-06-19 18:39:55 +01002318 if paths.currentApiFilePath.Valid() {
2319 properties.CurrentApiFile = paths.currentApiFilePath.Path()
2320 }
2321 if paths.removedApiFilePath.Valid() {
2322 properties.RemovedApiFile = paths.removedApiFilePath.Path()
2323 }
Paul Duffindd46f712020-02-10 13:37:10 +00002324 s.Scopes[apiScope] = properties
2325 }
2326 }
2327
2328 s.Libs = sdk.properties.Libs
Paul Duffindfa131e2020-05-15 20:37:11 +01002329 s.Naming_scheme = sdk.commonSdkLibraryProperties.Naming_scheme
Paul Duffind7eb1c22020-05-26 20:57:10 +01002330 s.Shared_library = proptools.BoolPtr(sdk.sharedLibrary())
Paul Duffina2ae7e02020-09-11 11:55:00 +01002331 s.Doctag_paths = sdk.doctagPaths
Paul Duffindd46f712020-02-10 13:37:10 +00002332}
2333
2334func (s *sdkLibrarySdkMemberProperties) AddToPropertySet(ctx android.SdkMemberContext, propertySet android.BpPropertySet) {
Paul Duffinf7a64332020-05-13 16:54:55 +01002335 if s.Naming_scheme != nil {
2336 propertySet.AddProperty("naming_scheme", proptools.String(s.Naming_scheme))
2337 }
Paul Duffind7eb1c22020-05-26 20:57:10 +01002338 if s.Shared_library != nil {
2339 propertySet.AddProperty("shared_library", *s.Shared_library)
2340 }
Paul Duffinf7a64332020-05-13 16:54:55 +01002341
Paul Duffindd46f712020-02-10 13:37:10 +00002342 for _, apiScope := range allApiScopes {
2343 if properties, ok := s.Scopes[apiScope]; ok {
Paul Duffin6b836ba2020-05-13 19:19:49 +01002344 scopeSet := propertySet.AddPropertySet(apiScope.propertyName)
Paul Duffindd46f712020-02-10 13:37:10 +00002345
Paul Duffin3d1248c2020-04-09 00:10:17 +01002346 scopeDir := filepath.Join("sdk_library", s.OsPrefix(), apiScope.name)
2347
Paul Duffindd46f712020-02-10 13:37:10 +00002348 var jars []string
2349 for _, p := range properties.Jars {
Paul Duffin3d1248c2020-04-09 00:10:17 +01002350 dest := filepath.Join(scopeDir, ctx.Name()+"-stubs.jar")
Paul Duffindd46f712020-02-10 13:37:10 +00002351 ctx.SnapshotBuilder().CopyToSnapshot(p, dest)
2352 jars = append(jars, dest)
2353 }
2354 scopeSet.AddProperty("jars", jars)
2355
Paul Duffin3d1248c2020-04-09 00:10:17 +01002356 // Merge the stubs source jar into the snapshot zip so that when it is unpacked
Paul Duffinab5ac8f2020-11-18 16:37:35 +00002357 // the source files are also unpacked.
Paul Duffin3d1248c2020-04-09 00:10:17 +01002358 snapshotRelativeDir := filepath.Join(scopeDir, ctx.Name()+"_stub_sources")
2359 ctx.SnapshotBuilder().UnzipToSnapshot(properties.StubsSrcJar, snapshotRelativeDir)
Paul Duffinab5ac8f2020-11-18 16:37:35 +00002360 scopeSet.AddProperty("stub_srcs", []string{snapshotRelativeDir})
Paul Duffin3d1248c2020-04-09 00:10:17 +01002361
Paul Duffin1fd005d2020-04-09 01:08:11 +01002362 if properties.CurrentApiFile != nil {
2363 currentApiSnapshotPath := filepath.Join(scopeDir, ctx.Name()+".txt")
2364 ctx.SnapshotBuilder().CopyToSnapshot(properties.CurrentApiFile, currentApiSnapshotPath)
2365 scopeSet.AddProperty("current_api", currentApiSnapshotPath)
2366 }
2367
2368 if properties.RemovedApiFile != nil {
2369 removedApiSnapshotPath := filepath.Join(scopeDir, ctx.Name()+"-removed.txt")
Paul Duffin3dbf9fd2020-06-02 13:00:02 +01002370 ctx.SnapshotBuilder().CopyToSnapshot(properties.RemovedApiFile, removedApiSnapshotPath)
Paul Duffin1fd005d2020-04-09 01:08:11 +01002371 scopeSet.AddProperty("removed_api", removedApiSnapshotPath)
2372 }
2373
Paul Duffindd46f712020-02-10 13:37:10 +00002374 if properties.SdkVersion != "" {
2375 scopeSet.AddProperty("sdk_version", properties.SdkVersion)
2376 }
2377 }
2378 }
2379
Paul Duffina2ae7e02020-09-11 11:55:00 +01002380 if len(s.Doctag_paths) > 0 {
2381 dests := []string{}
2382 for _, p := range s.Doctag_paths {
2383 dest := filepath.Join("doctags", p.Rel())
2384 ctx.SnapshotBuilder().CopyToSnapshot(p, dest)
2385 dests = append(dests, dest)
2386 }
2387 propertySet.AddProperty("doctag_files", dests)
2388 }
2389
Paul Duffindd46f712020-02-10 13:37:10 +00002390 if len(s.Libs) > 0 {
2391 propertySet.AddPropertyWithTag("libs", s.Libs, ctx.SnapshotBuilder().SdkMemberReferencePropertyTag(false))
2392 }
2393}